Page 1 of 1

linker: so much padding between code and data

Posted: Sun Jul 29, 2007 1:35 pm
by sancho1980
hi

i was playing around trying to turn a hla (high level assembly: ) program into a binary file. i compiled the file with

Code: Select all

hla -c test.hla
which gives me test.o. i can do an objdump on this which gives me:

Code: Select all

sancho@Kiste:~/assembly/boot$ objdump --disassemble-all test.o
test.o:     file format elf32-i386

Disassembly of section .data:

00000000 <.data>:
   0:   07                      pop    %es
   1:   00 00                   add    %al,(%eax)
   3:   00 ff                   add    %bh,%bh
   5:   ff                      (bad)  
   6:   ff                      (bad)  
   7:   ff                      .byte 0xff
Disassembly of section .text:

00000000 <main>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   83 ec 04                sub    $0x4,%esp
   6:   50                      push   %eax
   7:   50                      push   %eax
   8:   50                      push   %eax
   9:   c7 45 fc 03 00 00 00    movl   $0x3,0xfffffffc(%ebp)
  10:   58                      pop    %eax
  11:   58                      pop    %eax
  12:   58                      pop    %eax
  13:   89 ec                   mov    %ebp,%esp
  15:   5d                      pop    %ebp
  16:   c3                      ret
You can see that in the data section, I have declared and initialised an integer containing 7. This is all okay. My problem arises when I try to link this:

Code: Select all

sancho@Kiste:~/assembly/boot$ ld test.o -o test.bin -e main --oformat binary
and then

Code: Select all

sancho@Kiste:~/assembly/boot$ ndisasm -b 32 test.bin
gives me:

Code: Select all

00000000  55                push ebp
00000001  89E5              mov ebp,esp
00000003  83EC04            sub esp,byte +0x4
00000006  50                push eax
00000007  50                push eax
00000008  50                push eax
00000009  C745FC03000000    mov dword [ebp-0x4],0x3
00000010  58                pop eax
00000011  58                pop eax
00000012  58                pop eax
00000013  89EC              mov esp,ebp
00000015  5D                pop ebp
00000016  C3                ret
00000017  0000              add [eax],al
00000019  0000              add [eax],al
...MANY LINES CONTAINING ZEROES
0000101B  0000              add [eax],al
0000101D  0000              add [eax],al
0000101F  0007              add [edi],al
00001021  0000              add [eax],al
00001023  00FF              add bh,bh
00001025  FF                db 0xFF
00001026  FF                db 0xFF
00001027  FF                db 0xFF
The code ends at address 16. You can see here that my variable is placed at address 0000101F. There is a huge gap between code and data containing all zeroes. How can I link to a binary where the data is placed just after the code? I know that with the -Tdata somevalue option I can place the data at a fixed place, but this is not what Im looking for because I dont know how much space exaclty the code is going to consume...

Posted: Sun Jul 29, 2007 2:06 pm
by AJ
Hi,

Looks like stuff is being page-aligned, I think? Zero padding is the only way to do this in flat binaries.

Try using something like elf format, which will take less space on disk.

Cheers,
Adam

Posted: Sun Jul 29, 2007 2:33 pm
by sancho1980
seems like that was the right tip: -n option turns page alignment off

Posted: Mon Jul 30, 2007 1:45 am
by AJ
sancho1980 wrote:seems like that was the right tip
You seem surprised :twisted: