LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   segfault when linking with ld (https://www.linuxquestions.org/questions/programming-9/segfault-when-linking-with-ld-758599/)

101b147 09-30-2009 01:22 AM

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;
}

smeezekitty 09-30-2009 01:25 AM

why not just let gcc do the linking
also why are you passing ld so many param's

101b147 09-30-2009 01:28 AM

Quote:

Originally Posted by smeezekitty (Post 3701421)
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.

smeezekitty 09-30-2009 01:38 AM

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

101b147 09-30-2009 02:01 AM

Quote:

Originally Posted by smeezekitty (Post 3701435)
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

smeezekitty 09-30-2009 02:22 AM

maybe gcc is broken in that case

101b147 09-30-2009 02:28 AM

Quote:

Originally Posted by smeezekitty (Post 3701486)
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

smeezekitty 09-30-2009 02:47 AM

i tired it on windows without /lib/ld-linux.so.2
and got teste.exe stopped working in other words it crashed

smeezekitty 09-30-2009 02:48 AM

wait a minute
instead of -e main try -e _main

101b147 09-30-2009 04:28 AM

Quote:

Originally Posted by smeezekitty (Post 3701507)
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

carbonfiber 09-30-2009 04:34 AM

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 <...>

johnsfine 09-30-2009 08:59 AM

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.

smeezekitty 09-30-2009 12:23 PM

hmmm i overlooked that meaning just dont use the -e switch

101b147 10-03-2009 01:41 PM

Quote:

Originally Posted by johnsfine (Post 3701825)
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.


All times are GMT -5. The time now is 09:19 AM.