[comp.unix] a.out and "ld -r"

polar@top.cis.syr.edu (Polar Humenn) (12/27/90)

exuse me if this has already been posted..  I didn't see it come through.
--------
I have a question about the SUN-4 Unix loader (ld).
I am writing an assembler for a certain research project of mine,
the details are unimportant here.

My problem is trying to make Internal symbol references when I construct
the "a.out" file so that "ld" understands them.

This is a very simplistic attempt.  I use only symbols and 32 bit relocation
addresses, no fancy pc relative or address relative offsets and the hoard
of other options outlined in "man 5 a.out".

Let us say I have,

main:	push_structure    whammy
        push_closure     foo
        execute          fine
whammy:	push_constant    12
        push_constant    13
        enter_structure   2

Let us say that all instructions and operands each take 1 word, 32 bits.

let me define a couple of functions for clarity

unsigned addto_nlist( name, type, other , desc, value )
	This function adds a symbol to the nlist. and assigns the corresponding
	structure fields.  Only type and value are really important here.
	(I think!) It returns the index into the symbol string.
	
void     addto_reloc_text( addr, index, r_extern, type, addend )
	This function adds relocation information.  I only relocate by symbols
	so the index is always the index of the symbol.
	
So, I assemble in this order

-- main : push_structure whammy
(void) addto_nlist( "main", N_TEXT | N_EXT, 0, 0, 0x00000000 )

idx = addto_nlist( "whammy", N_UNDF | N_EXT, 0, 0, 0)
      addto_reloc_text( 0x00000004, idx, N_EXT, RELOC_32, 0 )

-- push_closure foo
idx = addto_nlist("foo", N_UNDF | N_EXT, 0, 0, 0)
	  addto_reloc_text( 0x0000000c, idx, N_EXT, RELOC_32, 0 )

-- execute fine
idx = addto_nlist("fine", N_UNDF | N_EXT, 0, 0, 0)
      addto_reloc_text( 0x00000010, idx, N_EXT, RELOC_32, 0)
	  
-- whammy: push_constant 12
(void) addto_nlist("whammy", N_TEXT | NEXT, 0, 0, 0x00000018 )

As you may be able to tell from this gobitly gook is that I end up
with an a.out file such that "nm" looks like:
00000000 T main
         U whammy
         U foo
         U fine
00000018 T whammy

Then I run "ld -r" on the a.out file and the symbols get resolved, 
and I end up with:
00000000 T main
         U foo
         U fine
00000018 T whammy

This is fine and great.  Now, what I would like to do is make "whammy" an 
internal (local) symbol that will disappear.  The documentation states that 
if I prefix the name with a 'L' it will dissapear after being resolved.  
However, I tried this and I get error messages relating to not being able 
to find the symbol.  I have tried all sorts of combinations of specifying 
and not specifying N_EXT in the nlist and relocation information.  However, 
no luck.  It appears the only way to get these symbols to resolve is to 
declare them all N_EXT, the way I should you above.

That is unless, I am not understanding something.  I don't have the code 
for the linker, so I can't analyze what it is doing internally.

Basically, I want the loader "ld -r" to resolve my predesignated local symbols 
and then strip them from the exported nlist.  It appears to me right now, that
the only way I can do this is to reslove them in my assembler first, which would
really suck since *I know* "ld" has much quicker and streamlined algorithms for 
doing such things (many years of improvements). i.e. Much better than I 
would care to spend the time to write myself.

Can anyone help me into understanding the loader?
Please email me directly.
Thanks!!!
-Polar    polar@top.cis.syr.edu