OSDev.org
https://forum.osdev.org/

ELF file .text section file offset
https://forum.osdev.org/viewtopic.php?f=1&t=31438
Page 1 of 1

Author:  alexhoppus [ Sun Mar 19, 2017 2:00 am ]
Post subject:  ELF file .text section file offset

Hey.
I use this https://github.com/alexhoppus/rpios/blob/master/user/Makefile Makefile to build this https://github.com/alexhoppus/rpios/blob/master/user/app1.c application with the use of this https://github.com/alexhoppus/rpios/blob/master/user/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:
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:
user (master) $ hexdump -C ./app1 | less

Code:
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/26834470/controlling-file-offset-in-linking

Author:  alexfru [ Sun Mar 19, 2017 2:53 am ]
Post subject:  Re: ELF file .text section file offset

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.

Author:  lkurusa [ Sun Mar 19, 2017 12:35 pm ]
Post subject:  Re: ELF file .text section file offset

What is in your ".text.boot" section?

Author:  zaval [ Sun Mar 19, 2017 3:40 pm ]
Post subject:  Re: ELF file .text section file offset

show your readelf -l output

Author:  bzt [ Tue Mar 21, 2017 1:36 pm ]
Post subject:  Re: ELF file .text section file offset

I had the same issue. It's --nmagic you need. The GNU linker documentation does not mention it, but it also sets alignment.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/