ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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)
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.
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
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.