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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
09-30-2009, 01:22 AM
|
#1
|
|
LQ Newbie
Registered: Jan 2009
Distribution: Fedora
Posts: 24
Rep:
|
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:
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;
}
|
|
|
|
|
09-30-2009, 01:25 AM
|
#2
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
Rep: 
|
why not just let gcc do the linking
also why are you passing ld so many param's
|
|
|
|
09-30-2009, 01:28 AM
|
#3
|
|
LQ Newbie
Registered: Jan 2009
Distribution: Fedora
Posts: 24
Original Poster
Rep:
|
Quote:
Originally Posted by smeezekitty
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.
|
|
|
|
09-30-2009, 01:38 AM
|
#4
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
Rep: 
|
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
|
|
|
|
09-30-2009, 02:01 AM
|
#5
|
|
LQ Newbie
Registered: Jan 2009
Distribution: Fedora
Posts: 24
Original Poster
Rep:
|
Quote:
Originally Posted by smeezekitty
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
|
|
|
|
09-30-2009, 02:22 AM
|
#6
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
Rep: 
|
maybe gcc is broken in that case
|
|
|
|
09-30-2009, 02:28 AM
|
#7
|
|
LQ Newbie
Registered: Jan 2009
Distribution: Fedora
Posts: 24
Original Poster
Rep:
|
Quote:
Originally Posted by smeezekitty
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
|
|
|
|
09-30-2009, 02:47 AM
|
#8
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
Rep: 
|
i tired it on windows without /lib/ld-linux.so.2
and got teste.exe stopped working in other words it crashed
|
|
|
|
09-30-2009, 02:48 AM
|
#9
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
Rep: 
|
wait a minute
instead of -e main try -e _main
|
|
|
|
09-30-2009, 04:28 AM
|
#10
|
|
LQ Newbie
Registered: Jan 2009
Distribution: Fedora
Posts: 24
Original Poster
Rep:
|
Quote:
Originally Posted by smeezekitty
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
|
|
|
|
09-30-2009, 04:34 AM
|
#11
|
|
Member
Registered: Sep 2009
Location: Sparta
Posts: 237
Rep:
|
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.
|
|
|
|
09-30-2009, 08:59 AM
|
#12
|
|
Senior Member
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,668
|
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.
|
|
|
|
09-30-2009, 12:23 PM
|
#13
|
|
Senior Member
Registered: Sep 2009
Location: Washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,136
Rep: 
|
hmmm i overlooked that meaning just dont use the -e switch
|
|
|
|
10-03-2009, 01:41 PM
|
#14
|
|
LQ Newbie
Registered: Jan 2009
Distribution: Fedora
Posts: 24
Original Poster
Rep:
|
Quote:
Originally Posted by johnsfine
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 05:20 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|