Loading an ELF file, padding at the end of the .text section

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Loading an ELF file, padding at the end of the .text section

Post by Luns »

According to page 2-8 of the ELF specification, there's supposed to be padding at the end of the text section and beginning of the data section. From what I understand, the memory after the end of .text but before the next page is supposed to be filled with a copy of the first bit of .data, and the memory from that page break to the start of .data is filled with the last bit of .text. E.g., if .text ends at 0x00401234, memory 0x00401235-0x00402000 is filled with the first 3532 bytes of .data. Then, if .data starts at 0x00402345, 0x00402000-0x00402345 is filled with the last 857 bytes of the text section. Correct?

If that is correct, what should happen if the .data section were to start right at a page break, such as 0x00402000? Should there be data padding at the end of the text section, and no text padding before the data section?

Here's the specification I'm looking at, in there are others with different page numbers: http://www.skyfree.org/linux/references/ELF_Format.pdf.

Thanks in advance,
Luns
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Loading an ELF file, padding at the end of the .text sec

Post by gerryg400 »

What they're saying is that if the text in the ELF file ends within a page (rather than the end of a page) and/or the data in the ELF file begins within a page (rather than at the beginning of the page) there may be a page in the ELF file that contains both text and data.

You will (or might if you use demand loading) need to load that page twice. Once in the text segment of your program and again in the data segment of the program. When the page is mapped in the text segment only the first part of it (the text part) will be accessed as code. The copy of the page that is mapped in the data segment will be accessed as data and hopefully the first part (the text part) won't be modified.

If the segments are page aligned, this is just a special case where there is no mixing of text and data in a page.

Berkus is correct. If you don't mmap your executables and load with memcpy, you can just zero or fill with NOPs the piece (text or data) that you don't need. In this case you will be using 2 physical pages to represent the page in the ELF file that contains both code and data.
If a trainstation is where trains stop, what is a workstation ?
Luns
Member
Member
Posts: 56
Joined: Sun May 01, 2011 12:15 am

Re: Loading an ELF file, padding at the end of the .text sec

Post by Luns »

Alright, thank you both for clarifying that for me.
Post Reply