[Slackers] REL/Linker files

Steve Judd sjudd at ffd2.com
Wed May 31 13:00:08 CDT 2006


Hmmm, that last explanation was pretty bad.  I'd like a do-over.

REL/Linker file structure:

A REL file is a RELocatable module containing both code and information
for relocating the code.  Consider a statement like

	lda LABEL,x	;or jsr LABEL, etc.

There are four cases of interest for LABEL:

Case 1: EQUated label (e.g. LABEL = $C000).  Nothing needs to be done,
since the value is absolute, not relative.

Case 2: LABEL is contained within the module.  At link time, we need to
store the actual value in the code.  To do so, we need two pieces of
information:

	- The location of LABEL in the "lda LABEL,x" statement (the
	  location to fix up at link time)
	- The offset of LABEL within the module (the address).

At link time, the module is loaded to some address ADDR.  The linker
then computes the correct label value as ADDR+Offset, and stores this
correct value at the appropriate location.

In a REL file, the offset is stored in the location (e.g. lda OFFSET,x
above), and a two-byte address is stored at the end of the file to
indicate locations within the file that need fixing.

Case 3: LABEL is an ENT value.  Because the label is within the code we
still need to do case 2 above, but in addition the label needs to be
stored in the REL file along with its offset, for use by other programs.

Case 4: LABEL is an EXT value.  In this case a dummy value is assembled
into the file, and the variable and its location are stored within the REL
file.  At link time, after the linker has computed all the correct ENT
labels it can store the proper EXT values.

Example:

	REL

blah equ $ff
var1 ext

start   ent
	lda blah	; lda $ff
	sta var1	; sta $xxxx
	jsr test	; jsr $0009	test = org+9
	rts		; rts
test	inc $d020	; inc $d020
	rts		; rts

table at end of code:
			; 06 00		relative address to be fixed
			; start 00 00	ENT labels (name and offset)
			; var1 03 00	EXT labels (name and fix offset)

Note: There are several exceptions to the above rules, such as

	lda #>label

and so forth, and with Slang it's more complicated for things like arrays.
But the above gives the idea of what a REL file looks like.  Full details
are in the linker source code.

One final detail: the REL file structure goes like

	code
	offset table
	two bytes: size of offset table
	ENT table
	two bytes: size of ent table
	EXT table
	two bytes: size of ext table
	two byte ID

Using this structure, the linker can load the file to the correct address
and then work backwards from the end of the file to fix stuff up.

Now You Know.

-S

P.S. This was all from memory but I think it's correct.


More information about the Slackers mailing list