[comp.unix.wizards] ld

rajeevc@mipos2.intel.com (Rajeev Chandrasekhar) (01/29/89)

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

Thanks much,

Rajeev

Rajeev Chandrasekhar
Intel Corp            >> theres someone in my head, and its not me << 
2625, Walsh Ave MS SC4-59                      (408) 765-4632
Santa Clara, CA 95051  {hplabs,oliveb}!intelca!mipos2!rajeevc                      

ji@corto.inria.fr (John Ioannidis - Altair) (01/31/89)

In article <3505@mipos3.intel.com> rajeevc@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@cs.columbia.edu> (preferred), or <ji@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

gwyn@smoke.BRL.MIL (Doug Gwyn ) (01/31/89)

In article <3505@mipos3.intel.com> rajeevc@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.

Perhaps you forgot to perform the runtime startup actions correctly?
Basically if you're building code to run as a user process you should
use "cc" to link the code instead of trying to use "ld" directly.
That way all the environmental details will be taken care of automatically.