ld

John Ioannidis - Altair ji at corto.inria.fr
Tue Jan 31 02:02:16 AEST 1989


In article <3505 at mipos3.intel.com> rajeevc at mipos2.intel.com (Rajeev Chandrasekhar) writes:
>When using ld to create an executable with only one object
>with no external calls, I get an segmentation fault. I
>am running SunOs 4.0.
>
>The executable created seems to be OK , if I disassemble it,
>but the segmentation fault message bothers me. Anybody
>know about this .... 
>
Assume the simplest possible program with no external references:
	main()
	{
	}

which gcc will translate to
	linkw	a6,#0
	unlk	a6
	rts

Are you doing something like this?

	$ cc -c foo.c
	$ ld foo.o

This won't work for a variety of reasons, but even the correct invocation:
	
	$ ld -Bstatic -e _main foo.o

it still won't work for one simple reason: foo.o tries to `terminate' by
executing an rts instruction, and it has no place to return to (all there
is in the stack is the arguments vector, the environment etc, but there is
no way to `return' to the kernel, which was what actually gave us control.

The way to terminate a process is, of course, the _exit() system call
(notice the _). SO, you either have to include the following two
lines just before the end of your program:
	asm("pea 1");
	asm("trap #0");
or link with /lib/crt0.o and /lib/libc.a:
	$ ld -Bstatic -e start /lib/crt0.o foo.o -lc
but I guess that's what you wanted to avoid in the first place. 

What good is a program that does not call anything else, anyway?

Hope all this helps,

/ji

#include <appropriate disclaimers>

In-Real-Life: John Ioannidis
E-Mail-To: <ji at cs.columbia.edu> (preferred), or <ji at walkuere.altair.fr>
P-Mail-To: GIP-Altair, Dom de Voluceau BP105, Rocquencourt 78153 Le Chesnay, FR
V-Mail-To: +33 1 39635227, +33 1 39635417

                ... It's all greek to me



More information about the Comp.unix.wizards mailing list