ld ignores the specified entry point and address location?

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
thxbb12
Posts: 23
Joined: Sun Aug 30, 2015 10:47 am

ld ignores the specified entry point and address location?

Post by thxbb12 »

I must be missing something, because I'm going crazy with ld at the moment.
Per the ld documentation, one can specify the entry point of the produced binary with the -e option as well as the location of various sections with --section-start.
Well... it appears it doesn't work as described in the doc, or I'm clearly missing something.

The app.s assembly file below defines a bss segment for data, followed by the text segment where I have 3 functions. The entry point (entrypoint) is defined at the end on purpose:

Code: Select all

global func1
global func2
global entrypoint

section .bss:
msg db "Greetings from the void", 0

section .text:

func1:
    mov     eax,0x1
    ret

func2:
    mov     eax,0x2
    ret

entrypoint:
    mov     eax,0xDEADBEEF
    ret
Here is the makefile that produces the app.elf binary:

Code: Select all

app.elf: app.o
	ld -e entrypoint --section-start=.text=0 --section-start=.bss=1000 -melf_i386 app.o -o app.elf

app.o: app.s
	nasm -f elf32 app.s -o app.o

clean:
	rm -f *.o *.elf
I pass the following 3 options to ld:
-e entrypoint : to tell ld the "entrypoint" label is the actual entry point of the program
--section-start=.text=0 to tell ld the code section should be located at address 0x0
--section-start=.bss=1000 to tell ld the bss section (data) should be located at address 0x1000

However, that's not what I'm seeing when dumping the elf content with "objdump -D app.elf":

Code: Select all

app.elf:     file format elf32-i386


Disassembly of section .bss::

00000000 <msg>:
   0:	47                   	inc    %edi
   1:	72 65                	jb     68 <entrypoint+0x44>
   3:	65                   	gs
   4:	74 69                	je     6f <entrypoint+0x4b>
   6:	6e                   	outsb  %ds:(%esi),(%dx)
   7:	67 73 20             	addr16 jae 2a <entrypoint+0x6>
   a:	66                   	data16
   b:	72 6f                	jb     7c <entrypoint+0x58>
   d:	6d                   	insl   (%dx),%es:(%edi)
   e:	20 74 68 65          	and    %dh,0x65(%eax,%ebp,2)
  12:	20 76 6f             	and    %dh,0x6f(%esi)
  15:	69                   	.byte 0x69
  16:	64                   	fs
	...

Disassembly of section .text::

00000018 <func1>:
  18:	b8 01 00 00 00       	mov    $0x1,%eax
  1d:	c3                   	ret    

0000001e <func2>:
  1e:	b8 02 00 00 00       	mov    $0x2,%eax
  23:	c3                   	ret    

00000024 <entrypoint>:
  24:	b8 ef be ad de       	mov    $0xdeadbeef,%eax
  29:	c3                   	ret    
I tried specifying different section names to ld, such .bss/.text and bss/text, but it still doesn't locate the data and code at the right addresses.
Can someone tell me what's going on or what I don't understand here?
Thanks!
Florent
Icee
Member
Member
Posts: 100
Joined: Wed Jan 08, 2014 8:41 am
Location: Moscow, Russia

Re: ld ignores the specified entry point and address locatio

Post by Icee »

You have an extra colon after "section .text" and "section .bss", so you end up with sections named ".text:" and ".bss:", as objdump clearly shows. That's why ld can't find them.
thxbb12
Posts: 23
Joined: Sun Aug 30, 2015 10:47 am

Re: ld ignores the specified entry point and address locatio

Post by thxbb12 »

Ugh.. what a stupid mistake.
That was it, thanks :-)
Florent
thxbb12
Posts: 23
Joined: Sun Aug 30, 2015 10:47 am

Re: ld ignores the specified entry point and address locatio

Post by thxbb12 »

Actually I wrote my reply a bit too soon.
There was indeed an issue with the trailing colon. However, the issue is still present.

Asm source:

Code: Select all

global func1
global func2
global entrypoint

section .data
msg db "Greetings from the void", 0

section .text

func1:
    mov     eax,0x1
    ret

func2:
    mov     eax,0x2
    ret

entrypoint:
    mov     eax,0xDEADBEEF
    ret
Command line passed to the linker:

Code: Select all

ld -e entrypoint --section-start=.text=0 --section-start=.data=1000 -melf_i386 app.o -o app.elf
Resulting sections' dump:

Code: Select all

app.elf:     file format elf32-i386

Disassembly of section .text:

00000000 <func1>:
   0:	b8 01 00 00 00       	mov    $0x1,%eax
   5:	c3                   	ret    

00000006 <func2>:
   6:	b8 02 00 00 00       	mov    $0x2,%eax
   b:	c3                   	ret    

0000000c <entrypoint>:
   c:	b8 ef be ad de       	mov    $0xdeadbeef,%eax
  11:	c3                   	ret    

Disassembly of section .data:

00001000 <msg>:
    1000:	47                   	inc    %edi
    1001:	72 65                	jb     1068 <__bss_start+0x50>
    1003:	65                   	gs
    1004:	74 69                	je     106f <__bss_start+0x57>
    1006:	6e                   	outsb  %ds:(%esi),(%dx)
    1007:	67 73 20             	addr16 jae 102a <__bss_start+0x12>
    100a:	66                   	data16
    100b:	72 6f                	jb     107c <__bss_start+0x64>
    100d:	6d                   	insl   (%dx),%es:(%edi)
    100e:	20 74 68 65          	and    %dh,0x65(%eax,%ebp,2)
    1012:	20 76 6f             	and    %dh,0x6f(%esi)
    1015:	69                   	.byte 0x69
    1016:	64                   	fs
	...
The .data section is correctly located at 0x1000. The .text section starts indeed at 0x0.
However, the entry point is "func1" when it should be "entrypoint" due to the -e paramter.
I also tried to write a ld script with the same settings, but the result is the same.
Florent
Icee
Member
Member
Posts: 100
Joined: Wed Jan 08, 2014 8:41 am
Location: Moscow, Russia

Re: ld ignores the specified entry point and address locatio

Post by Icee »

thxbb12 wrote:However, the entry point is "func1" when it should be "entrypoint" due to the -e paramter.
Where do you draw that conclusion from? Learn how to use the tools you're using. Entry point has nothing to do with the order symbols appear in a section. You can verify that the entry point has been set correctly by the linker by using objdump -f.
thxbb12
Posts: 23
Joined: Sun Aug 30, 2015 10:47 am

Re: ld ignores the specified entry point and address locatio

Post by thxbb12 »

I had assumed it was located in 0. Thanks for the info.
Florent
Post Reply