Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
assembler programmes segfaulting when linked with ld
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Gentoo on AMD64
View previous topic :: View next topic  
Author Message
blublub
n00b
n00b


Joined: 27 Dec 2004
Posts: 12

PostPosted: Fri Apr 08, 2005 3:43 pm    Post subject: assembler programmes segfaulting when linked with ld Reply with quote

Hello all!
I have trubbles with assembler programs linked with ld.

If i assemble this simple program
Code:

BITS 64
section .text
global _start

_start:
mov rax, 123
ret

with "yasm -f elf -m amd64 foo.asm -o bar.o" and link it with "ld bar.o -o foo" the
program will segfault.
If I rename the function "_start" to "main" and link it with "gcc bar.o -o foo" it works.
I'm having this problem with every program linked with ld.

Any infos how to solve this problem?
Thanks.
Back to top
View user's profile Send private message
Corona688
Veteran
Veteran


Joined: 10 Jan 2004
Posts: 1204

PostPosted: Fri Apr 08, 2005 9:06 pm    Post subject: Reply with quote

It's pretty counterintuitive, but the gist of it is this: Process entry points are not function calls.

Instead of something else "calling" your function, an entirely new memory space is created and populated with data and code from your executable. The CPU is then set to the default entry point and just left to run. There's just nothing there for your code to return to, and the only way to break out of this private universe is with system calls. The main() interface that gcc provides compiled C programs with is actually a convenience library, which makes the appropriate system calls when main returns, something along the line of this:
Code:
                mov     eax, 1
                mov     ebx, 42 
                int     0x80


This tutorial explains these things in much greater detail, hopefully the x86 assembly will be readily translatable to amd64.
_________________
Petition for Better 64-bit ATI Drivers - Sign Here
http://www.petitiononline.com/atipet/petition.html
Back to top
View user's profile Send private message
blublub
n00b
n00b


Joined: 27 Dec 2004
Posts: 12

PostPosted: Fri Apr 08, 2005 10:01 pm    Post subject: Reply with quote

Thanks for the infos and the very interessting site. The real problem was that i had to add
"section .data", otherwise even your code segfaulted. But why? I don't use any variables.

Another question: On my system I can find "/usr/include/asm-i386/unistd.h" and "/usr/include/asm-x86_64/unistd.h". In both files the syscall numbers are defined. But I only can use the definitions in "asm-i386/unistd.h" though it is a x86_64 binary.

In "asm-i386/unistd.h" __NR_exit is defined as 0 in "asm-x86_64/unistd".h it is defined as 60.
So whats the file "asm-x86_64/unistd" for?

Thanks.
Back to top
View user's profile Send private message
Corona688
Veteran
Veteran


Joined: 10 Jan 2004
Posts: 1204

PostPosted: Sat Apr 09, 2005 12:22 am    Post subject: Reply with quote

blublub wrote:
Thanks for the infos and the very interessting site. The real problem was that i had to add
"section .data", otherwise even your code segfaulted. But why? I don't use any variables.
I can only guess... maybye without a data section you don't get a stack, either? int calls do lots of pushes.
Quote:
Another question: On my system I can find "/usr/include/asm-i386/unistd.h" and "/usr/include/asm-x86_64/unistd.h". In both files the syscall numbers are defined. But I only can use the definitions in "asm-i386/unistd.h" though it is a x86_64 binary.

In "asm-i386/unistd.h" __NR_exit is defined as 0 in "asm-x86_64/unistd".h it is defined as 60.
So whats the file "asm-x86_64/unistd" for?
No idea what's going on there, all I can say is that syscall numbers under linux seem to be a real mess. A lot of contortions are necessary to get the correct ones even in C sometimes.
_________________
Petition for Better 64-bit ATI Drivers - Sign Here
http://www.petitiononline.com/atipet/petition.html
Back to top
View user's profile Send private message
blublub
n00b
n00b


Joined: 27 Dec 2004
Posts: 12

PostPosted: Sat Apr 09, 2005 10:08 am    Post subject: Reply with quote

Thank you very much.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Gentoo on AMD64 All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum