Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why segments in elf file can overlap

simple C file:

#include <stdio.h>
int main(){
    printf("Hello World");
    return 0;
}

after compile the code, using readelf -a a.out, elf info is follow:

elf program headers, segment info

Questions:

  1. several sections appear in different segments, like interp section both in 2nd and 3rd segment. how can a section appear in more than one segments?
  2. the 2nd segment's address is from 0x8048134 but 3rd LOAD segment starts from 0x8048000 with 0x004d0 memsize. then the two segment overlap? How can two segments overlap in memory?
  3. why the program header's offset and viraddr must be congurent modulo the page size?
like image 565
bluesea Avatar asked Oct 08 '22 16:10

bluesea


1 Answers

You may have garbage in the section table or it may be missing completely. All that matters to the dynamic loader is the segment table (program headers), and even then, only the PT_LOAD segments should not overlap*. The other kinds of segments (INTERP, DYNAMIC etc) provide additional info for the loader and usually refer to some parts of the LOAD segments.

*Here's what the spec says:

PT_LOAD The array element specifies a loadable segment, described by p_filesz and p_memsz. The bytes from the file are mapped to the beginning of the memory segment. If the segment's memory size (p_memsz) is larger than the file size (p_filesz), the "extra" bytes are defined to hold the value 0 and to follow the segment's initialized area. The file size may not be larger than the memory size. Loadable segment entries in the program header table appear in ascending order, sorted on the p_vaddr member.

As you can see, there's no mention of overlapping, so it does not seem to be forbidden, although I don't think I've seen any files with overlapping PT_LOAD segments.

like image 183
Igor Skochinsky Avatar answered Oct 12 '22 13:10

Igor Skochinsky