LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-30-2009, 01:22 AM   #1
101b147
LQ Newbie
 
Registered: Jan 2009
Distribution: Fedora
Posts: 24

Rep: Reputation: 15
segfault when linking with ld


hi guys. i am having trouble using the linker ld. here is what i did:

Quote:
gcc -c teste.c -o teste.o -g
ld --dynamic-linker /lib/ld-linux.so.2 -lc teste.o -o teste -e main
the above works fine, but when i run the program, here is what i get:

Quote:
aaa
Segmentation fault
running it from gdb, i get this:

Quote:
(gdb) start
Breakpoint 1 at 0x80481b5: file teste.c, line 4.
Starting program: /home/leecher/prog/assembly/teste
main () at teste.c:4
4 puts("aaa");
(gdb) step
aaa
5 return 0;
(gdb) step
6 }
(gdb) step
0x00000001 in ?? ()
(gdb) step
Cannot find bounds of current function
(gdb) step
Cannot find bounds of current function
(gdb) step
Cannot find bounds of current function
(gdb) continue
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x00000001 in ?? ()
(gdb)

here is the program:
Quote:
#include<stdio.h>

int main(){
puts("aaa");
return 0;
}
 
Old 09-30-2009, 01:25 AM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 170Reputation: 170
why not just let gcc do the linking
also why are you passing ld so many param's
 
Old 09-30-2009, 01:28 AM   #3
101b147
LQ Newbie
 
Registered: Jan 2009
Distribution: Fedora
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by smeezekitty View Post
why not just let gcc do the linking
also why are you passing ld so many param's
i need to use the linker. this program was just an test to try it, but i'll have to actually use the linker later with my project.

btw, when i use ld and as to assemble and link assembly code, it works,; but i tried gcc -S to generate the assembly and then use as on the above program, but i got the same error.
 
Old 09-30-2009, 01:38 AM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 170Reputation: 170
try a link without --dynamic-linker /lib/ld-linux.so.2
if still no go your libraries are broken and you need to replace them
 
Old 09-30-2009, 02:01 AM   #5
101b147
LQ Newbie
 
Registered: Jan 2009
Distribution: Fedora
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by smeezekitty View Post
try a link without --dynamic-linker /lib/ld-linux.so.2
if still no go your libraries are broken and you need to replace them

Quote:
leecher@darkstar:~/prog/assembly$ ld -o teste teste.o -lc -e main
leecher@darkstar:~/prog/assembly$ ./teste
-bash: ./teste: No such file or directory
leecher@darkstar:~/prog/assembly$

i don't think my libraries are broken, i got a fresh install from slackware 13.0. the only thing i did was recompile the kernel

besides, this works fine:

hello.s

Quote:
.section .data
helloworld:
.ascii "hello\n\0"
.section .text
.globl _start
_start:
pushl $helloworld
call printf

pushl $0
call exit
Quote:
as hello.s -o hello.o
ld --dynamic-linker /lib/ld-linux.so.2 -o hello hello.o -lc
no segfault with this assembly code. but if i try to generate assembly from the c code, i still get segfault
 
Old 09-30-2009, 02:22 AM   #6
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 170Reputation: 170
maybe gcc is broken in that case
 
Old 09-30-2009, 02:28 AM   #7
101b147
LQ Newbie
 
Registered: Jan 2009
Distribution: Fedora
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by smeezekitty View Post
maybe gcc is broken in that case
don't think so, i can compile the program normally. i just can't link anually. maybe gcc passes some parameters for the linker and i don't know what they are
 
Old 09-30-2009, 02:47 AM   #8
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 170Reputation: 170
i tired it on windows without /lib/ld-linux.so.2
and got teste.exe stopped working in other words it crashed
 
Old 09-30-2009, 02:48 AM   #9
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 170Reputation: 170
wait a minute
instead of -e main try -e _main
 
Old 09-30-2009, 04:28 AM   #10
101b147
LQ Newbie
 
Registered: Jan 2009
Distribution: Fedora
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by smeezekitty View Post
wait a minute
instead of -e main try -e _main
Quote:
leecher@darkstar:~/prog/assembly$ ld --dynamic-linker /lib/ld-linux.so.2 -lc teste.o -o teste -e _main
ld: warning: cannot find entry symbol _main; defaulting to 00000000080481a4
segfault again. i looked at the assembly generated by gcc, and there is no _main there, just main
 
Old 09-30-2009, 04:34 AM   #11
carbonfiber
Member
 
Registered: Sep 2009
Location: Sparta
Posts: 237

Rep: Reputation: 46
Code:
ld --dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /usr/lib/crtn.o -lc teste.o -o teste
P.S.: gcc -v <...>

Last edited by carbonfiber; 09-30-2009 at 04:37 AM.
 
Old 09-30-2009, 08:59 AM   #12
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,076

Rep: Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110Reputation: 1110
I think carbonfiber gave you the right answer, but a little light on explanation.

In an ordinary C program, the main() function is not actually the entry point. Code runs before main to set up various aspects of the environment in which main runs, then it calls main. When main returns that code exits.

The OP seems to be trying to make main be the entry point. The example seems to be simple enough (but I'm not sure) to run correctly without any of the work normally done before main(). But if main() is the entry point, rather than called by the startup routine, main() has nowhere to return to and cannot return. It could instead exit by calling exit().

In this example, I think exiting by calling exit() instead of returning would fix the problem. In other examples you might really need the startup code.

The gcc -v suggested by carbonfiber gets gcc to show you the ld command it would use. If you want to invoke ld yourself but with the same results as having gcc invoke ld, you can copy important details (such as the startup module) from the command gcc would have used.
 
Old 09-30-2009, 12:23 PM   #13
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,227

Rep: Reputation: 170Reputation: 170
hmmm i overlooked that meaning just dont use the -e switch
 
Old 10-03-2009, 01:41 PM   #14
101b147
LQ Newbie
 
Registered: Jan 2009
Distribution: Fedora
Posts: 24

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by johnsfine View Post
I think carbonfiber gave you the right answer, but a little light on explanation.

In an ordinary C program, the main() function is not actually the entry point. Code runs before main to set up various aspects of the environment in which main runs, then it calls main. When main returns that code exits.

The OP seems to be trying to make main be the entry point. The example seems to be simple enough (but I'm not sure) to run correctly without any of the work normally done before main(). But if main() is the entry point, rather than called by the startup routine, main() has nowhere to return to and cannot return. It could instead exit by calling exit().

In this example, I think exiting by calling exit() instead of returning would fix the problem. In other examples you might really need the startup code.

The gcc -v suggested by carbonfiber gets gcc to show you the ld command it would use. If you want to invoke ld yourself but with the same results as having gcc invoke ld, you can copy important details (such as the startup module) from the command gcc would have used.
you are right. i replaced return with exit and i didn't get segfault. besides, i ran gcc -v, and it appears it invokes some programs that reside in its own folder. right now, i am a little without time, so i won't try these programs, but thx anyway.

Last edited by 101b147; 10-03-2009 at 01:43 PM.
 
  


Reply

Tags
fault, ld, linker, segmentation fault


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in using both load time linking and runtime linking durgaprasad_j Linux - General 0 08-01-2006 03:49 AM
Realplayer SEGFAULT! Please Help! seran Linux - General 0 05-27-2005 08:48 AM
segfault in XLoadQueryFont() rexdp Linux - Software 0 05-13-2005 05:08 PM
MEncoder segfault Hammett Linux - Software 0 11-01-2003 07:13 AM
Evolution SegFault granitepoint Linux - Software 0 01-11-2003 11:11 PM


All times are GMT -5. The time now is 07:11 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration