Page 1 of 1

ELF file .text section file offset

Posted: Sun Mar 19, 2017 2:00 am
by alexhoppus
Hey.
I use this https://github.com/alexhoppus/rpios/blo ... r/Makefile Makefile to build this https://github.com/alexhoppus/rpios/blo ... ser/app1.c application with the use of this https://github.com/alexhoppus/rpios/blo ... /linker.ld linker script. After building it turned out that the size of this app is abnormally huge, currently I tested it on Ubuntu 16.04/14.04 and for different versions of arm-none-eabi-* toolchain it gives 70K/35K appropriately. The reason of such a big size of ELF file is that .text section is placed inside ELF file with a big offset:

Code: Select all

user (master) $ arm-linux-gnueabi-readelf --sections app1 | grep .text
  [ 1] .text             PROGBITS        00800000 008000 000078 00  AX  0   0  4
As you can see in this case .text section is located at offset 0x8000 inside ELF file. The space between ELF header and text section is filled with zeros.

Code: Select all

user (master) $ hexdump -C ./app1 | less

Code: Select all

00000000  7f 45 4c 46 01 01 01 00  00 00 00 00 00 00 00 00  |.ELF............|
00000010  02 00 28 00 01 00 00 00  00 00 80 00 34 00 00 00  |..(.........4...|
00000020  5c 81 00 00 02 02 00 05  34 00 20 00 01 00 28 00  |\.......4. ...(.|
00000030  09 00 06 00 01 00 00 00  00 80 00 00 00 00 80 00  |................|
00000040  00 00 80 00 b8 00 00 00  b8 00 00 00 05 00 00 00  |................|
00000050  00 80 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
00008000  00 00 a0 e3 00 10 a0 e3  1a 00 00 ea 04 40 2d e5  |.............@-.|
00008010  03 40 a0 e1 02 30 a0 e1  01 20 a0 e1 00 10 a0 e1  |[email protected]... ......|
00008020  01 00 a0 e3 00 00 00 ef  04 40 9d e4 1e ff 2f e1  |.........@..../.|
00008030  04 40 2d e5 03 40 a0 e1  02 30 a0 e1 01 20 a0 e1  |.@[email protected]... ..|
I use this application from inside of my self-written OS and this app is linked as a data blob with the kernel image. The kernel is loaded using UART bootloader that's why the final image size is critical. Also as you have guessed i use ELF loader for apps so preserving ELF format is important for me. The question is - why this text section offset is so huge, what is the reason for this? And how can i control it?
Actually, i have found the similar question on SO, but it is unanswered http://stackoverflow.com/questions/2683 ... in-linking

Re: ELF file .text section file offset

Posted: Sun Mar 19, 2017 2:53 am
by alexfru
I have not tried these, but there seem to be a few clues: If you're really concerned about the size, then either you should use a different format or simply compress the file.

Re: ELF file .text section file offset

Posted: Sun Mar 19, 2017 12:35 pm
by lkurusa
What is in your ".text.boot" section?

Re: ELF file .text section file offset

Posted: Sun Mar 19, 2017 3:40 pm
by zaval
show your readelf -l output

Re: ELF file .text section file offset

Posted: Tue Mar 21, 2017 1:36 pm
by bzt
I had the same issue. It's --nmagic you need. The GNU linker documentation does not mention it, but it also sets alignment.