diff options
author | H. Peter Anvin <hpa@zytor.com> | 2002-04-30 21:08:42 +0000 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2002-04-30 21:08:42 +0000 |
commit | 005b39c856917f060973f8d07c98d111b67d0a7f (patch) | |
tree | 47f03e86ef15c8deebf35096f358adca27892850 /rdoff | |
parent | 9a633fa3b9b5c772daf4cbca89093bfcd387359b (diff) | |
download | nasm-005b39c856917f060973f8d07c98d111b67d0a7f.tar.gz nasm-005b39c856917f060973f8d07c98d111b67d0a7f.tar.bz2 nasm-005b39c856917f060973f8d07c98d111b67d0a7f.zip |
NASM 0.98.26
Diffstat (limited to 'rdoff')
-rw-r--r-- | rdoff/Changes | 63 | ||||
-rw-r--r-- | rdoff/Mkfiles/Makefile.dj | 75 | ||||
-rw-r--r-- | rdoff/Mkfiles/Makefile.emx | 76 | ||||
-rw-r--r-- | rdoff/Mkfiles/Makefile.sc | 56 | ||||
-rw-r--r-- | rdoff/Mkfiles/Makefile.unx | 75 | ||||
-rw-r--r-- | rdoff/Mkfiles/README | 4 | ||||
-rw-r--r-- | rdoff/ldrdf1.c | 728 | ||||
-rw-r--r-- | rdoff/test/makelib | 14 | ||||
-rw-r--r-- | rdoff/v1/README | 5 | ||||
-rw-r--r-- | rdoff/v1/collectn.c | 40 | ||||
-rw-r--r-- | rdoff/v1/collectn.h | 22 | ||||
-rw-r--r-- | rdoff/v1/ldrdf.c | 728 | ||||
-rw-r--r-- | rdoff/v1/rdf.doc | 99 | ||||
-rw-r--r-- | rdoff/v1/rdf2bin.c | 125 | ||||
-rw-r--r-- | rdoff/v1/rdfdump.c | 167 | ||||
-rw-r--r-- | rdoff/v1/rdflib.c | 235 | ||||
-rw-r--r-- | rdoff/v1/rdfload.c | 173 | ||||
-rw-r--r-- | rdoff/v1/rdfload.h | 29 | ||||
-rw-r--r-- | rdoff/v1/rdlib.c | 88 | ||||
-rw-r--r-- | rdoff/v1/rdlib.h | 18 | ||||
-rw-r--r-- | rdoff/v1/rdoff.c | 397 | ||||
-rw-r--r-- | rdoff/v1/rdoff.h | 118 | ||||
-rw-r--r-- | rdoff/v1/rdoff.txt | 114 | ||||
-rw-r--r-- | rdoff/v1/rdx.c | 61 | ||||
-rw-r--r-- | rdoff/v1/symtab.c | 80 | ||||
-rw-r--r-- | rdoff/v1/symtab.h | 22 |
26 files changed, 286 insertions, 3326 deletions
diff --git a/rdoff/Changes b/rdoff/Changes deleted file mode 100644 index 34163a9..0000000 --- a/rdoff/Changes +++ /dev/null @@ -1,63 +0,0 @@ -Differences between RDOFF versions 1 & 2 -======================================== - -This document is designed primarily for people maintaining code which -uses RDOFF version 1, and would like to upgrade that code to work -with version 2. - -The main changes are summarised here: - -Overall format -============== - -The overall format has changed somewhat since version 1, in order -to make RDOFF more flexible. After the file type identifier (which -has been changed to 'RDOFF2', obviously), there is now a 4 byte -integer describing the length of the object module. This allows -multiple objects to be concatenated, while the loader can easily -build an index of the locations of each object. This isn't as -pointless as it sounds; I'm using RDOFF in a microkernel operating -system, and this is the ideal way of loading multiple driver modules -at boot time. - -There are also no longer a fixed number of segments; instead there -is a list of segments, immediately following the header. -Each segment is preceded by a 10 byte header giving information about -that segment. This header has the following format: - -Length Description -2 Type -2 Number -2 Reserved -4 Length - -'Type' is a number describing what sort of segment it is (eg text, data, -comment, debug info). See 'rdoff2.txt' for a list of the segment types. -'Number' is the number used to refer to the segment in the header records. -Not all segments will be loaded; it is only intended that one code -and one data segment will be loaded into memory. It is possible, however, -for a loaded segment to contain a reference to an unloaded segment. -This is an error, and should be flagged at load time. Or maybe you should -load the segment... its up to you, really. - -The segment's data immediately follows the end of the segment header. - -HEADER RECORDS -============== - -All of the header records have changed in this version, but not -substantially. Each record type has had a content-length code added, -a single byte immediately following the type byte. This contains the -length of the rest of the record (excluding the type and length bytes, -but including the terminating nulls on any strings in the record). - -There are two new record types, Segment Relocation (6), and FAR import (7). -The record formats are identical to Relocation (1) and import (2). They are -only of real use on systems using segmented architectures. Systems using -a flat model should treat FAR import (7) exactly the same as an import (2), -and should either flag segment relocation as an error, or attempt to figure -out whether it is a reference to a code or data symbol, and set the value -referenced to the according selector value. I am opting for the former -approach, and would recommend that others working on 32 bit flat systems -do the same. - diff --git a/rdoff/Mkfiles/Makefile.dj b/rdoff/Mkfiles/Makefile.dj new file mode 100644 index 0000000..2099739 --- /dev/null +++ b/rdoff/Mkfiles/Makefile.dj @@ -0,0 +1,75 @@ +# Generated automatically from Makefile.in by configure. +# +# Auto-configuring Makefile for RDOFF object file utils; part of the +# Netwide Assembler +# +# The Netwide Assembler is copyright (C) 1996 Simon Tatham and +# Julian Hall. All rights reserved. The software is +# redistributable under the licence given in the file "Licence" +# distributed in the NASM archive. + +# You may need to adjust these values. + +prefix = /djgpp +CC = gcc -s +CFLAGS = -O2 -I.. + +# You _shouldn't_ need to adjust anything below this line. + +exec_prefix = ${prefix} +bindir = ${exec_prefix}/bin +mandir = ${prefix}/man + +INSTALL = /usr/bin/install -c +INSTALL_PROGRAM = ${INSTALL} +INSTALL_DATA = ${INSTALL} -m 644 +LN_S = ln -s + +LDRDFLIBS = rdoff.o ../nasmlib.o symtab.o hash.o collectn.o rdlib.o segtab.o +RDXLIBS = rdoff.o rdfload.o symtab.o hash.o collectn.o + +.c.o: + $(CC) -c $(CFLAGS) $*.c + +all: rdfdump ldrdf rdx rdflib rdf2bin rdf2com + +rdfdump: rdfdump.o + $(CC) -o rdfdump rdfdump.o + +ldrdf: ldrdf.o $(LDRDFLIBS) + $(CC) -o ldrdf ldrdf.o $(LDRDFLIBS) +rdx: rdx.o $(RDXLIBS) + $(CC) -o rdx rdx.o $(RDXLIBS) +rdflib: rdflib.o + $(CC) -o rdflib rdflib.o +rdf2bin: rdf2bin.o $(RDXLIBS) nasmlib.o + $(CC) -o rdf2bin rdf2bin.o $(RDXLIBS) nasmlib.o +rdf2com: + $(LN_S) rdf2bin rdf2com + +rdf2bin.o: rdf2bin.c +rdfdump.o: rdfdump.c +rdoff.o: rdoff.c rdoff.h +ldrdf.o: ldrdf.c rdoff.h ../nasmlib.h symtab.h collectn.h rdlib.h +symtab.o: symtab.c symtab.h hash.h +collectn.o: collectn.c collectn.h +rdx.o: rdx.c rdoff.h rdfload.h symtab.h +rdfload.o: rdfload.c rdfload.h rdoff.h collectn.h symtab.h +rdlib.o: rdlib.c rdlib.h +rdflib.o: rdflib.c +hash.o: hash.c hash.h +segtab.o: segtab.c segtab.h + +nasmlib.o: ../nasmlib.c ../nasmlib.h ../names.c ../nasm.h + $(CC) -c $(CFLAGS) ../nasmlib.c + +clean: + rm -f *.o rdfdump ldrdf rdx rdflib rdf2bin rdf2com + +install: rdfdump ldrdf rdx rdflib rdf2bin rdf2com + $(INSTALL_PROGRAM) rdfdump $(bindir)/rdfdump + $(INSTALL_PROGRAM) ldrdf $(bindir)/ldrdf + $(INSTALL_PROGRAM) rdx $(bindir)/rdx + $(INSTALL_PROGRAM) rdflib $(bindir)/rdflib + $(INSTALL_PROGRAM) rdf2bin $(bindir)/rdf2bin + cd $(bindir); $(LN_S) rdf2bin rdf2com diff --git a/rdoff/Mkfiles/Makefile.emx b/rdoff/Mkfiles/Makefile.emx new file mode 100644 index 0000000..fbaa934 --- /dev/null +++ b/rdoff/Mkfiles/Makefile.emx @@ -0,0 +1,76 @@ +# Generated automatically from Makefile.in by configure. +# $Id$ +# +# Auto-configuring Makefile for RDOFF object file utils; part of the +# Netwide Assembler +# +# The Netwide Assembler is copyright (C) 1996 Simon Tatham and +# Julian Hall. All rights reserved. The software is +# redistributable under the licence given in the file "Licence" +# distributed in the NASM archive. + +top_srcdir = .. +srcdir = . +prefix = /usr/local +exec_prefix = ${prefix} +bindir = ${exec_prefix}/bin +mandir = ${prefix}/man + +CC = gcc +CFLAGS = -s -Zomf -O2 -fomit-frame-pointer -Wall -ansi -pedantic -I$(srcdir) -I$(top_srcdir) +LDFLAGS = -s -Zomf -Zexe -Zcrtdll +LIBS = -lgcc + +INSTALL = .././install-sh -c +INSTALL_PROGRAM = ${INSTALL} +INSTALL_DATA = ${INSTALL} -m 644 + +LDRDFLIBS = rdoff.o nasmlib.o symtab.o collectn.o rdlib.o segtab.o hash.o +RDXLIBS = rdoff.o rdfload.o symtab.o collectn.o hash.o + +.c.o: + $(CC) -c $(CFLAGS) -o $@ $< + +all: rdfdump ldrdf rdx rdflib rdf2bin + +rdfdump: rdfdump.o + $(CC) $(LDFLAGS) -o rdfdump rdfdump.o $(LIBS) +ldrdf: ldrdf.o $(LDRDFLIBS) + $(CC) $(LDFLAGS) -o ldrdf ldrdf.o $(LDRDFLIBS) $(LIBS) +rdx: rdx.o $(RDXLIBS) + $(CC) $(LDFLAGS) -o rdx rdx.o $(RDXLIBS) $(LIBS) +rdflib: rdflib.o + $(CC) $(LDFLAGS) -o rdflib rdflib.o $(LIBS) +rdf2bin: rdf2bin.o $(RDXLIBS) nasmlib.o + $(CC) $(LDFLAGS) -o rdf2bin rdf2bin.o $(RDXLIBS) nasmlib.o $(LIBS) + +rdf2bin.o: rdf2bin.c +rdfdump.o: rdfdump.c +rdoff.o: rdoff.c rdoff.h +ldrdf.o: ldrdf.c rdoff.h $(top_srcdir)/nasmlib.h symtab.h collectn.h rdlib.h +symtab.o: symtab.c symtab.h +collectn.o: collectn.c collectn.h +rdx.o: rdx.c rdoff.h rdfload.h symtab.h +rdfload.o: rdfload.c rdfload.h rdoff.h collectn.h symtab.h +rdlib.o: rdlib.c rdlib.h +rdflib.o: rdflib.c +segtab.o: segtab.c + +nasmlib.o: $(top_srcdir)/nasmlib.c + $(CC) -c $(CFLAGS) -o $@ $(top_srcdir)/nasmlib.c + +clean: + rm -f *.o rdfdump ldrdf rdx rdflib rdf2bin rdf2com + +spotless: clean + rm -f Makefile + +distclean: spotless + +install: rdfdump ldrdf rdx rdflib rdf2bin rdf2com + $(INSTALL_PROGRAM) rdfdump $(INSTALLROOT)$(bindir)/rdfdump + $(INSTALL_PROGRAM) ldrdf $(INSTALLROOT)$(bindir)/ldrdf + $(INSTALL_PROGRAM) rdx $(INSTALLROOT)$(bindir)/rdx + $(INSTALL_PROGRAM) rdflib $(INSTALLROOT)$(bindir)/rdflib + $(INSTALL_PROGRAM) rdf2bin $(INSTALLROOT)$(bindir)/rdf2bin + cd $(INSTALLROOT)$(bindir) && rm -f rdf2com && $(LN_S) rdf2bin rdf2com diff --git a/rdoff/Mkfiles/Makefile.sc b/rdoff/Mkfiles/Makefile.sc new file mode 100644 index 0000000..7b45fe7 --- /dev/null +++ b/rdoff/Mkfiles/Makefile.sc @@ -0,0 +1,56 @@ +# Makefile for RDOFF object file utils; part of the Netwide Assembler +# +# The Netwide Assembler is copyright (C) 1996 Simon Tatham and +# Julian Hall. All rights reserved. The software is +# redistributable under the licence given in the file "Licence" +# distributed in the NASM archive. +# +# This Makefile is designed for use under Unix (probably fairly +# portably). + +CC = sc +CCFLAGS = -I..\ -c -a1 -mn -Nc -w2 -w7 -o+time -5 +LINK = link +LINKFLAGS = /noi /exet:NT /su:console + +OBJ=obj +EXE=.exe + +NASMLIB = ..\nasmlib.$(OBJ) +NASMLIB_H = ..\nasmlib.h +LDRDFLIBS = rdoff.$(OBJ) $(NASMLIB) symtab.$(OBJ) collectn.$(OBJ) rdlib.$(OBJ) +RDXLIBS = rdoff.$(OBJ) rdfload.$(OBJ) symtab.$(OBJ) collectn.$(OBJ) + +.c.$(OBJ): + $(CC) $(CCFLAGS) $*.c + +all : rdfdump$(EXE) ldrdf$(EXE) rdx$(EXE) rdflib$(EXE) rdf2bin$(EXE) rdf2com$(EXE) + +rdfdump$(EXE) : rdfdump.$(OBJ) + $(LINK) $(LINKFLAGS) rdfdump.$(OBJ), rdfdump$(EXE); +ldrdf$(EXE) : ldrdf.$(OBJ) $(LDRDFLIBS) + $(LINK) $(LINKFLAGS) ldrdf.$(OBJ) $(LDRDFLIBS), ldrdf$(EXE); +rdx$(EXE) : rdx.$(OBJ) $(RDXLIBS) + $(LINK) $(LINKFLAGS) rdx.$(OBJ) $(RDXLIBS), rdx$(EXE); +rdflib$(EXE) : rdflib.$(OBJ) + $(LINK) $(LINKFLAGS) rdflib.$(OBJ), rdflib$(EXE); +rdf2bin$(EXE) : rdf2bin.$(OBJ) $(RDXLIBS) $(NASMLIB) + $(LINK) $(LINKFLAGS) rdf2bin.$(OBJ) $(RDXLIBS) $(NASMLIB), rdf2bin$(EXE); +rdf2com$(EXE) : rdf2bin$(EXE) + copy rdf2bin$(EXE) rdf2com$(EXE) + +rdf2bin.$(OBJ) : rdf2bin.c +rdfdump.$(OBJ) : rdfdump.c +rdoff.$(OBJ) : rdoff.c rdoff.h +ldrdf.$(OBJ) : ldrdf.c rdoff.h $(NASMLIB_H) symtab.h collectn.h rdlib.h +symtab.$(OBJ) : symtab.c symtab.h +collectn.$(OBJ) : collectn.c collectn.h +rdx.$(OBJ) : rdx.c rdoff.h rdfload.h symtab.h +rdfload.$(OBJ) : rdfload.c rdfload.h rdoff.h collectn.h symtab.h +rdlib.$(OBJ) : rdlib.c rdlib.h +rdflib.$(OBJ) : rdflib.c + +clean : + del *.$(OBJ) rdfdump$(EXE) ldrdf$(EXE) rdx$(EXE) rdflib$(EXE) rdf2bin$(EXE) + + diff --git a/rdoff/Mkfiles/Makefile.unx b/rdoff/Mkfiles/Makefile.unx new file mode 100644 index 0000000..89d439f --- /dev/null +++ b/rdoff/Mkfiles/Makefile.unx @@ -0,0 +1,75 @@ +# Generated automatically from Makefile.in by configure. +# +# Auto-configuring Makefile for RDOFF object file utils; part of the +# Netwide Assembler +# +# The Netwide Assembler is copyright (C) 1996 Simon Tatham and +# Julian Hall. All rights reserved. The software is +# redistributable under the licence given in the file "Licence" +# distributed in the NASM archive. + +# You may need to adjust these values. + +prefix = /usr/local +CC = cc +CFLAGS = -O -I.. + +# You _shouldn't_ need to adjust anything below this line. + +exec_prefix = ${prefix} +bindir = ${exec_prefix}/bin +mandir = ${prefix}/man + +INSTALL = /usr/bin/install -c +INSTALL_PROGRAM = ${INSTALL} +INSTALL_DATA = ${INSTALL} -m 644 +LN_S = ln -s + +LDRDFLIBS = rdoff.o ../nasmlib.o symtab.o hash.o collectn.o rdlib.o segtab.o +RDXLIBS = rdoff.o rdfload.o symtab.o hash.o collectn.o + +.c.o: + $(CC) -c $(CFLAGS) $*.c + +all: rdfdump ldrdf rdx rdflib rdf2bin rdf2com + +rdfdump: rdfdump.o + $(CC) -o rdfdump rdfdump.o + +ldrdf: ldrdf.o $(LDRDFLIBS) + $(CC) -o ldrdf ldrdf.o $(LDRDFLIBS) +rdx: rdx.o $(RDXLIBS) + $(CC) -o rdx rdx.o $(RDXLIBS) +rdflib: rdflib.o + $(CC) -o rdflib rdflib.o +rdf2bin: rdf2bin.o $(RDXLIBS) nasmlib.o + $(CC) -o rdf2bin rdf2bin.o $(RDXLIBS) nasmlib.o +rdf2com: + $(LN_S) rdf2bin rdf2com + +rdf2bin.o: rdf2bin.c +rdfdump.o: rdfdump.c +rdoff.o: rdoff.c rdoff.h +ldrdf.o: ldrdf.c rdoff.h ../nasmlib.h symtab.h collectn.h rdlib.h +symtab.o: symtab.c symtab.h hash.h +collectn.o: collectn.c collectn.h +rdx.o: rdx.c rdoff.h rdfload.h symtab.h +rdfload.o: rdfload.c rdfload.h rdoff.h collectn.h symtab.h +rdlib.o: rdlib.c rdlib.h +rdflib.o: rdflib.c +hash.o: hash.c hash.h +segtab.o: segtab.c segtab.h + +nasmlib.o: ../nasmlib.c ../nasmlib.h ../names.c ../nasm.h + $(CC) -c $(CFLAGS) ../nasmlib.c + +clean: + rm -f *.o rdfdump ldrdf rdx rdflib rdf2bin rdf2com + +install: rdfdump ldrdf rdx rdflib rdf2bin rdf2com + $(INSTALL_PROGRAM) rdfdump $(bindir)/rdfdump + $(INSTALL_PROGRAM) ldrdf $(bindir)/ldrdf + $(INSTALL_PROGRAM) rdx $(bindir)/rdx + $(INSTALL_PROGRAM) rdflib $(bindir)/rdflib + $(INSTALL_PROGRAM) rdf2bin $(bindir)/rdf2bin + cd $(bindir); $(LN_S) rdf2bin rdf2com diff --git a/rdoff/Mkfiles/README b/rdoff/Mkfiles/README new file mode 100644 index 0000000..7e68499 --- /dev/null +++ b/rdoff/Mkfiles/README @@ -0,0 +1,4 @@ +These are pre-created Makefiles for various platforms, use them if +GNU autoconf/automake packages are not supported on your system. + +Copy appropriate Makefile to ../Makefile and run make. diff --git a/rdoff/ldrdf1.c b/rdoff/ldrdf1.c deleted file mode 100644 index 9e4a215..0000000 --- a/rdoff/ldrdf1.c +++ /dev/null @@ -1,728 +0,0 @@ -/* ldrdf.c RDOFF Object File linker/loader main program - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - */ - -/* TODO: Make the system skip a module (other than the first) if none - * of the other specified modules contain a reference to it. - * May require the system to make an extra pass of the modules to be - * loaded eliminating those that aren't required. - * - * Support all the existing documented options... - * - * Support libaries (.a files - requires a 'ranlib' type utility) - * (I think I've got this working, so I've upped the version) - * - * -s option to strip resolved symbols from exports. (Could make this an - * external utility) - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "rdoff.h" -#include "nasmlib.h" -#include "symtab.h" -#include "collectn.h" -#include "rdlib.h" - -#define LDRDF_VERSION "0.30" - -/* global variables - those to set options: */ - -int verbose = 0; /* reflects setting of command line switch */ -int align = 16; -int errors = 0; /* set by functions to cause halt after current - stage of processing */ - -/* the linked list of modules that must be loaded & linked */ - -struct modulenode { - rdffile f; /* the file */ - long coderel; /* module's code relocation factor */ - long datarel; /* module's data relocation factor */ - long bssrel; /* module's bss data reloc. factor */ - void * header; /* header location, if loaded */ - char * name; /* filename */ - struct modulenode *next; -}; - -#define newstr(str) strcpy(malloc(strlen(str) + 1),str) -#define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1)+strlen(s2)+1),s1),s2) - - -struct modulenode *modules = NULL,*lastmodule = NULL; - -/* the linked list of libraries to be searched for missing imported - symbols */ - -struct librarynode * libraries = NULL, * lastlib = NULL; - -void *symtab; /* The symbol table */ - -rdf_headerbuf * newheader ; /* New header to be written to output */ - -/* loadmodule - find the characteristics of a module and add it to the - * list of those being linked together */ - -void loadmodule(char *filename) -{ - struct modulenode *prev; - if (! modules) { - modules = malloc(sizeof(struct modulenode)); - lastmodule = modules; - prev = NULL; - } - else { - lastmodule->next = malloc(sizeof(struct modulenode)); - prev = lastmodule; - lastmodule = lastmodule->next; - } - - if (! lastmodule) { - fputs("ldrdf: not enough memory\n",stderr); - exit(1); - } - - if (rdfopen(&lastmodule->f,filename)) { - rdfperror("ldrdf",filename); - exit(1); - } - - lastmodule->header = NULL; /* header hasn't been loaded */ - lastmodule->name = filename; - lastmodule->next = NULL; - - if (prev) { - lastmodule->coderel = prev->coderel + prev->f.code_len; - if (lastmodule->coderel % align != 0) - lastmodule->coderel += align - (lastmodule->coderel % align); - lastmodule->datarel = prev->datarel + prev->f.data_len; - if (lastmodule->datarel % align != 0) - lastmodule->datarel += align - (lastmodule->datarel % align); - } - else { - lastmodule->coderel = 0; - lastmodule->datarel = 0; - } - - if (verbose) - printf("%s code = %08lx (+%04lx), data = %08lx (+%04lx)\n",filename, - lastmodule->coderel,lastmodule->f.code_len, - lastmodule->datarel,lastmodule->f.data_len); - - lastmodule->header = malloc(lastmodule->f.header_len); - if (!lastmodule->header) { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - - if (rdfloadseg(&lastmodule->f,RDOFF_HEADER,lastmodule->header)) - { - rdfperror("ldrdf",filename); - exit(1); - } -} - -/* load_library add a library to list of libraries to search - * for undefined symbols - */ - -void load_library(char * name) -{ - if (verbose) - printf("adding library %s to search path\n",name); - - if (! lastlib) { - lastlib = libraries = malloc(sizeof(struct librarynode)); - } - else - { - lastlib->next = malloc(sizeof(struct librarynode)); - lastlib = lastlib->next; - } - - if (! lastlib) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); - } - strcpy (lastlib->name = malloc (1+strlen(name)), name); - lastlib->fp = NULL; - lastlib->referenced = 0; - lastlib->next = NULL; -} - - -/* build_symbols() step through each module's header, and locate - * exported symbols, placing them in a global table - */ - -long bsslength; - -void mod_addsymbols(struct modulenode * mod) -{ - rdfheaderrec *r; - symtabEnt e; - long cbBss; - - mod->bssrel = bsslength; - cbBss = 0; - rdfheaderrewind(&mod->f); - while ((r = rdfgetheaderrec(&mod->f))) - { - - if (r->type == 5) /* Allocate BSS */ - cbBss += r->b.amount; - - if (r->type != 3) continue; /* ignore all but export recs */ - - e.segment = r->e.segment; - e.offset = r->e.offset + - (e.segment == 0 ? mod->coderel : /* 0 -> code */ - e.segment == 1 ? mod->datarel : /* 1 -> data */ - mod->bssrel) ; /* 2 -> bss */ - - e.flags = 0; - e.name = malloc(strlen(r->e.label) + 1); - if (! e.name) - { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - strcpy(e.name,r->e.label); - symtabInsert(symtab,&e); - } - bsslength += cbBss; -} - -void build_symbols() -{ - struct modulenode *mod; - - if (verbose) printf("building global symbol table:\n"); - newheader = rdfnewheader(); - - symtab = symtabNew(); - bsslength = 0; /* keep track of location of BSS symbols */ - - for (mod = modules; mod; mod = mod->next) - { - mod_addsymbols( mod ); - } - if (verbose) - { - symtabDump(symtab,stdout); - printf("BSS length = %ld bytes\n\n",bsslength); - } -} - - -/* scan_libraries() search through headers of modules for undefined - * symbols, and scan libraries for those symbols, - * adding library modules found to list of modules - * to load. */ - -void scan_libraries(void) -{ - struct modulenode * mod, * nm; - struct librarynode * lib; - rdfheaderrec * r; - int found; - char * tmp; - - if (verbose) printf("Scanning libraries for unresolved symbols...\n"); - - mod = modules; - - while (mod) - { - rdfheaderrewind(&mod->f); - - while ((r = rdfgetheaderrec(&mod->f))) - { - if (r->type != 2) continue; /* not an import record */ - if ( symtabFind (symtab,r->i.label) ) - continue; /* symbol already defined */ - - /* okay, we have an undefined symbol... step through - the libraries now */ - if (verbose >= 2) { - printf("undefined symbol '%s'...",r->i.label); - fflush(stdout); - } - - lib = libraries; - found = 0; - - tmp = newstr(r->i.label); - while (! found && lib) - { - /* move this to an outer loop...! */ - nm = malloc(sizeof(struct modulenode)); - - if (rdl_searchlib(lib,tmp,&nm->f)) - { /* found a module in the library */ - - /* create a modulenode for it */ - - if (! nm) { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - - nm->name = newstrcat(lib->name,nm->f.name); - if (verbose >= 2) printf("found in '%s'\n",nm->name); - - nm->coderel = lastmodule->coderel + lastmodule->f.code_len; - if (nm->coderel % align != 0) - nm->coderel += align - (nm->coderel % align); - - nm->datarel = lastmodule->datarel + lastmodule->f.data_len; - if (nm->datarel % align != 0) - nm->datarel += align - (nm->datarel % align); - - nm->header = malloc(nm->f.header_len); - if (! nm->header) - { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - - if (rdfloadseg(&nm->f,RDOFF_HEADER,nm->header)) - { - rdfperror("ldrdf",nm->name); - exit(1); - } - - nm->next = NULL; - found = 1; - lastmodule->next = nm; - lastmodule = nm; - - if (verbose) - printf("%s code = %08lx (+%04lx), data = %08lx " - "(+%04lx)\n",lastmodule->name, - lastmodule->coderel,lastmodule->f.code_len, - lastmodule->datarel,lastmodule->f.data_len); - - /* add the module's info to the symbol table */ - mod_addsymbols(nm); - } - else - { - if (rdl_error) { - rdl_perror("ldrdf",lib->name); - exit(1); - } - free(nm); - } - lib = lib->next; - } - free(tmp); - if (!found && verbose >= 2) printf("not found\n"); - } - mod = mod->next; - } -} - -/* load_segments() allocates memory for & loads the code & data segs - * from the RDF modules - */ - -char *text,*data; -long textlength,datalength; - -void load_segments(void) -{ - struct modulenode *mod; - - if (!modules) { - fprintf(stderr,"ldrdf: nothing to do\n"); - exit(0); - } - if (!lastmodule) { - fprintf(stderr,"ldrdf: panic: module list exists, but lastmodule=NULL\n"); - exit(3); - } - - if (verbose) - printf("loading modules into memory\n"); - - /* The following stops 16 bit DOS from crashing whilst attempting to - work using segments > 64K */ - if (sizeof(int) == 2) { /* expect a 'code has no effect' warning on 32 bit - platforms... */ - if (lastmodule->coderel + lastmodule->f.code_len > 65535 || - lastmodule->datarel + lastmodule->f.data_len > 65535) { - fprintf(stderr,"ldrdf: segment length has exceeded 64K; use a 32 bit " - "version.\nldrdf: code size = %05lx, data size = %05lx\n", - lastmodule->coderel + lastmodule->f.code_len, - lastmodule->datarel + lastmodule->f.data_len); - exit(1); - } - } - - text = malloc(textlength = lastmodule->coderel + lastmodule->f.code_len); - data = malloc(datalength = lastmodule->datarel + lastmodule->f.data_len); - - if (!text || !data) { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - - mod = modules; - while (mod) { /* load the segments for each module */ - if (verbose >= 2) printf(" loading %s\n",mod->name); - if (rdfloadseg(&mod->f,RDOFF_CODE,&text[mod->coderel]) || - rdfloadseg(&mod->f,RDOFF_DATA,&data[mod->datarel])) { - rdfperror("ldrdf",mod->name); - exit(1); - } - rdfclose(&mod->f); /* close file; segments remain */ - mod = mod->next; - } -} - -/* link_segments() step through relocation records in each module's - * header, fixing up references. - */ - -void link_segments(void) -{ - struct modulenode *mod; - Collection imports; - symtabEnt *s; - long rel,relto; - char *seg; - rdfheaderrec *r; - int bRelative; - - if (verbose) printf("linking segments\n"); - - collection_init(&imports); - - for (mod = modules; mod; mod = mod->next) { - if (verbose >= 2) printf("* processing %s\n",mod->name); - rdfheaderrewind(&mod->f); - while((r = rdfgetheaderrec(&mod->f))) { - if (verbose >= 3) printf("record type: %d\n",r->type); - switch(r->type) { - case 1: /* relocation record */ - if (r->r.segment >= 64) { /* Relative relocation; */ - bRelative = 1; /* need to find location relative */ - r->r.segment -= 64; /* to start of this segment */ - relto = r->r.segment == 0 ? mod->coderel : mod->datarel; - } - else - { - bRelative = 0; /* non-relative - need to relocate - * at load time */ - relto = 0; /* placate optimiser warnings */ - } - - /* calculate absolute offset of reference, not rel to beginning of - segment */ - r->r.offset += r->r.segment == 0 ? mod->coderel : mod->datarel; - - /* calculate the relocation factor to apply to the operand - - the base address of one of this modules segments if referred - segment is 0 - 2, or the address of an imported symbol - otherwise. */ - - if (r->r.refseg == 0) rel = mod->coderel; - else if (r->r.refseg == 1) rel = mod->datarel; - else if (r->r.refseg == 2) rel = mod->bssrel; - else { /* cross module link - find reference */ - s = *colln(&imports,r->r.refseg - 2); - if (!s) { - fprintf(stderr,"ldrdf: link to undefined segment %04x in" - " %s:%d\n", r->r.refseg,mod->name,r->r.segment); - errors = 1; - break; - } - rel = s->offset; - - r->r.refseg = s->segment; /* change referred segment, - so that new header is - correct */ - } - - if (bRelative) /* Relative - subtract current segment start */ - rel -= relto; - else - { /* Add new relocation header */ - rdfaddheader(newheader,r); - } - - /* Work out which segment we're making changes to ... */ - if (r->r.segment == 0) seg = text; - else if (r->r.segment == 1) seg = data; - else { - fprintf(stderr,"ldrdf: relocation in unknown segment %d in " - "%s\n", r->r.segment,mod->name); - errors = 1; - break; - } - - /* Add the relocation factor to the datum specified: */ - - if (verbose >= 3) - printf(" - relocating %d:%08lx by %08lx\n",r->r.segment, - r->r.offset,rel); - - /**** The following code is non-portable. Rewrite it... ****/ - switch(r->r.length) { - case 1: - seg[r->r.offset] += (char) rel; - break; - case 2: - *(int16 *)(seg + r->r.offset) += (int16) rel; - break; - case 4: - *(long *)(seg + r->r.offset) += rel; - break; - } - break; - - case 2: /* import record */ - s = symtabFind(symtab, r->i.label); - if (s == NULL) { - /* Need to add support for dynamic linkage */ - fprintf(stderr,"ldrdf: undefined symbol %s in module %s\n", - r->i.label,mod->name); - errors = 1; - } - else - { - *colln(&imports,r->i.segment - 2) = s; - if (verbose >= 2) - printf("imported %s as %04x\n", r->i.label, r->i.segment); - } - break; - - case 3: /* export; dump to output new version */ - s = symtabFind(symtab, r->e.label); - if (! s) { - fprintf(stderr,"ldrdf: internal error - undefined symbol %s " - "exported in header of '%s'\n",r->e.label,mod->name); - continue; - } - r->e.offset = s->offset; - rdfaddheader(newheader,r); - break; - - case 4: /* DLL record */ - rdfaddheader(newheader,r); /* copy straight to output */ - break; - } - } - if (rdf_errno != 0) { - rdfperror("ldrdf",mod->name); - exit(1); - } - collection_reset(&imports); - } -} - -/* write_output() write linked program out to a file */ - -void write_output(char *filename) -{ - FILE * fp; - rdfheaderrec r; - - if (verbose) printf("writing output to '%s'\n",filename); - - fp = fopen(filename,"wb"); - if (! fp) - { - fprintf(stderr,"ldrdf: could not open '%s' for writing\n",filename); - exit(1); - } - - - /* add BSS length count to header... */ - if (bsslength) - { - r.type = 5; - r.b.amount = bsslength; - rdfaddheader(newheader,&r); - } - - /* Write header */ - rdfwriteheader(fp,newheader); - rdfdoneheader(newheader); - newheader = NULL; - - /* Write text */ - if (fwrite(&textlength,1,4,fp) != 4 - || fwrite(text,1,textlength,fp) !=textlength) - { - fprintf(stderr,"ldrdf: error writing %s\n",filename); - exit(1); - } - - /* Write data */ - if (fwrite(&datalength,1,4,fp) != 4 || - fwrite(data,1,datalength,fp) != datalength) - { - fprintf (stderr,"ldrdf: error writing %s\n", filename); - exit(1); - } - fclose(fp); -} - - -/* main program: interpret command line, and pass parameters on to - * individual module loaders & the linker - * - * Command line format: - * ldrdf [-o outfile | -x] [-r xxxx] [-v] [--] infile [infile ...] - * - * Default action is to output a file named 'aout.rdx'. -x specifies - * that the linked object program should be executed, rather than - * written to a file. -r specifies that the object program should - * be prelocated at address 'xxxx'. This option cannot be used - * in conjunction with -x. - */ - -const char *usagemsg = "usage:\n" -" ldrdf [-o outfile | -x] [-a x] [-v] [-p x] [--] infile [infile ...]\n" -" [-l<libname> ...]\n\n" -" ldrdf -h displays this message\n" -" ldrdf -r displays version information\n\n" -" -o selects output filename (default is aout.rdx)\n" -" -x causes ldrdx to link & execute rather than write to file\n" -" -a x causes object program to be statically relocated to address 'x'\n" -" -v turns on verbose mode\n" -" -p x causes segments to be aligned (padded) to x byte boundaries\n" -" (default is 16 bytes)\n" -" -l<name> causes 'name' to be linked in as a library. Note no search is\n" -" performed - the entire pathname MUST be specified.\n"; - -void usage(void) -{ - fputs(usagemsg,stderr); -} - -int main(int argc,char **argv) -{ - char *ofilename = "aout.rdx"; - long relocateaddr = -1; /* -1 if no relocation is to occur */ - int execute = 0; /* 1 to execute after linking, 0 otherwise */ - int procsw = 1; /* set to 0 by '--' */ - int tmp; - - if (argc == 1) { - usage(); - exit(1); - } - - /* process command line switches, and add modules specified to linked list - of modules, keeping track of total memory required to load them */ - - while(argv++,--argc) { - if (procsw && !strcmp(*argv,"-h")) { /* Help command */ - usage(); exit(1); - } - else if (procsw && !strcmp(*argv,"-r")) { - printf("ldrdf version %s (%s) (%s)\n",LDRDF_VERSION,_RDOFF_H, - sizeof(int) == 2 ? "16 bit" : "32 bit"); - exit(1); - } - else if (procsw && !strcmp(*argv,"-o")) { - ofilename = *++argv; - --argc; - if (execute) { - fprintf(stderr,"ldrdf: -o and -x switches incompatible\n"); - exit(1); - } - if (verbose > 1) printf("output filename set to '%s'\n",ofilename); - } - else if (procsw && !strcmp(*argv,"-x")) { - execute++; - if (verbose > 1) printf("will execute linked object\n"); - } - else if (procsw && !strcmp(*argv,"-a")) { - relocateaddr = readnum(*++argv,&tmp); - --argc; - if (tmp) { - fprintf(stderr,"ldrdf: error in parameter to '-a' switch: '%s'\n", - *argv); - exit(1); - } - if (execute) { - fprintf(stderr,"ldrdf: -a and -x switches incompatible\n"); - exit(1); - } - if (verbose) printf("will relocate to %08lx\n",relocateaddr); - } - else if (procsw && !strcmp(*argv,"-v")) { - verbose++; - if (verbose == 1) printf("verbose mode selected\n"); - } - else if (procsw && !strcmp(*argv,"-p")) { - align = readnum(*++argv,&tmp); - --argc; - if (tmp) { - fprintf(stderr,"ldrdf: error in parameter to '-p' switch: '%s'\n", - *argv); - exit(1); - } - if (align != 1 && align != 2 && align != 4 && align != 8 && align != 16 - && align != 32 && align != 256) { - fprintf(stderr,"ldrdf: %d is an invalid alignment factor - must be" - "1,2,4,8,16 or 256\n",align); - exit(1); - } - if (verbose > 1) printf("alignment %d selected\n",align); - } - else if (procsw && !strncmp(*argv,"-l",2)) { - load_library(*argv + 2); - } - else if (procsw && !strcmp(*argv,"--")) { - procsw = 0; - } - else { /* is a filename */ - if (verbose > 1) printf("processing module %s\n",*argv); - loadmodule(*argv); - } - } - - /* we should be scanning for unresolved references, and removing - unreferenced modules from the list of modules here, so that - we know about the final size once libraries have been linked in */ - - build_symbols(); /* build a global symbol table... */ - - scan_libraries(); /* check for imported symbols not in table, - and ensure the relevant library modules - are loaded */ - - load_segments(); /* having calculated size of reqd segments, load - each rdoff module's segments into memory */ - - link_segments(); /* step through each module's header, and resolve - references to the global symbol table. - This also does local address fixups. */ - - if (errors) { - fprintf(stderr,"ldrdf: there were errors - aborted\n"); - exit(errors); - } - if (execute) { - fprintf(stderr,"ldrdf: module execution not yet supported\n"); - exit(1); - } - if (relocateaddr != -1) { - fprintf(stderr,"ldrdf: static relocation not yet supported\n"); - exit(1); - } - - write_output(ofilename); - return 0; -} diff --git a/rdoff/test/makelib b/rdoff/test/makelib deleted file mode 100644 index baa4676..0000000 --- a/rdoff/test/makelib +++ /dev/null @@ -1,14 +0,0 @@ - -LIBNAME=$1; -shift; - -if [ "$LIBNAME" = "" ]; then - echo 'Usage: makelib <library name> <module> [...]' -fi - -rdflib c $LIBNAME - -for FILE in $*; do - rdflib a $LIBNAME $FILE $FILE -done - diff --git a/rdoff/v1/README b/rdoff/v1/README deleted file mode 100644 index 56ad81b..0000000 --- a/rdoff/v1/README +++ /dev/null @@ -1,5 +0,0 @@ -This directory contains programs for working with RDOFF version 1 object -files. RDOFF version 1 is no longer supported - you should now be using -RDOFF2. If you are working with your own code, the changes you will need -to make are very simple, and are outlined in the document Changes in the -nasm/rdoff directory. diff --git a/rdoff/v1/collectn.c b/rdoff/v1/collectn.c deleted file mode 100644 index c265c95..0000000 --- a/rdoff/v1/collectn.c +++ /dev/null @@ -1,40 +0,0 @@ -/* collectn.c Implements variable length pointer arrays [collections] - * - * This file is public domain. - */ - -#include "collectn.h" -#include <stdlib.h> - -void collection_init(Collection * c) -{ - int i; - - for (i = 0; i < 32; i++) c->p[i] = NULL; - c->next = NULL; -} - -void ** colln(Collection * c, int index) -{ - while (index >= 32) { - index -= 32; - if (c->next == NULL) { - c->next = malloc(sizeof(Collection)); - collection_init(c->next); - } - c = c->next; - } - return &(c->p[index]); -} - -void collection_reset(Collection *c) -{ - int i; - if (c->next) { - collection_reset(c->next); - free(c->next); - } - - c->next = NULL; - for (i = 0; i < 32; i++) c->p[i] = NULL; -} diff --git a/rdoff/v1/collectn.h b/rdoff/v1/collectn.h deleted file mode 100644 index 2dc786e..0000000 --- a/rdoff/v1/collectn.h +++ /dev/null @@ -1,22 +0,0 @@ -/* collectn.h Header file for 'collection' abstract data type - * - * This file is public domain, and does not come under the NASM license. - * It, along with 'collectn.c' implements what is basically a variable - * length array (of pointers) - */ - -#ifndef _COLLECTN_H -#define _COLLECTN_H - -typedef struct tagCollection { - void *p[32]; /* array of pointers to objects */ - - struct tagCollection *next; -} Collection; - -void collection_init(Collection * c); -void ** colln(Collection * c, int index); -void collection_reset(Collection * c); - -#endif - diff --git a/rdoff/v1/ldrdf.c b/rdoff/v1/ldrdf.c deleted file mode 100644 index 9e4a215..0000000 --- a/rdoff/v1/ldrdf.c +++ /dev/null @@ -1,728 +0,0 @@ -/* ldrdf.c RDOFF Object File linker/loader main program - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - */ - -/* TODO: Make the system skip a module (other than the first) if none - * of the other specified modules contain a reference to it. - * May require the system to make an extra pass of the modules to be - * loaded eliminating those that aren't required. - * - * Support all the existing documented options... - * - * Support libaries (.a files - requires a 'ranlib' type utility) - * (I think I've got this working, so I've upped the version) - * - * -s option to strip resolved symbols from exports. (Could make this an - * external utility) - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -#include "rdoff.h" -#include "nasmlib.h" -#include "symtab.h" -#include "collectn.h" -#include "rdlib.h" - -#define LDRDF_VERSION "0.30" - -/* global variables - those to set options: */ - -int verbose = 0; /* reflects setting of command line switch */ -int align = 16; -int errors = 0; /* set by functions to cause halt after current - stage of processing */ - -/* the linked list of modules that must be loaded & linked */ - -struct modulenode { - rdffile f; /* the file */ - long coderel; /* module's code relocation factor */ - long datarel; /* module's data relocation factor */ - long bssrel; /* module's bss data reloc. factor */ - void * header; /* header location, if loaded */ - char * name; /* filename */ - struct modulenode *next; -}; - -#define newstr(str) strcpy(malloc(strlen(str) + 1),str) -#define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1)+strlen(s2)+1),s1),s2) - - -struct modulenode *modules = NULL,*lastmodule = NULL; - -/* the linked list of libraries to be searched for missing imported - symbols */ - -struct librarynode * libraries = NULL, * lastlib = NULL; - -void *symtab; /* The symbol table */ - -rdf_headerbuf * newheader ; /* New header to be written to output */ - -/* loadmodule - find the characteristics of a module and add it to the - * list of those being linked together */ - -void loadmodule(char *filename) -{ - struct modulenode *prev; - if (! modules) { - modules = malloc(sizeof(struct modulenode)); - lastmodule = modules; - prev = NULL; - } - else { - lastmodule->next = malloc(sizeof(struct modulenode)); - prev = lastmodule; - lastmodule = lastmodule->next; - } - - if (! lastmodule) { - fputs("ldrdf: not enough memory\n",stderr); - exit(1); - } - - if (rdfopen(&lastmodule->f,filename)) { - rdfperror("ldrdf",filename); - exit(1); - } - - lastmodule->header = NULL; /* header hasn't been loaded */ - lastmodule->name = filename; - lastmodule->next = NULL; - - if (prev) { - lastmodule->coderel = prev->coderel + prev->f.code_len; - if (lastmodule->coderel % align != 0) - lastmodule->coderel += align - (lastmodule->coderel % align); - lastmodule->datarel = prev->datarel + prev->f.data_len; - if (lastmodule->datarel % align != 0) - lastmodule->datarel += align - (lastmodule->datarel % align); - } - else { - lastmodule->coderel = 0; - lastmodule->datarel = 0; - } - - if (verbose) - printf("%s code = %08lx (+%04lx), data = %08lx (+%04lx)\n",filename, - lastmodule->coderel,lastmodule->f.code_len, - lastmodule->datarel,lastmodule->f.data_len); - - lastmodule->header = malloc(lastmodule->f.header_len); - if (!lastmodule->header) { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - - if (rdfloadseg(&lastmodule->f,RDOFF_HEADER,lastmodule->header)) - { - rdfperror("ldrdf",filename); - exit(1); - } -} - -/* load_library add a library to list of libraries to search - * for undefined symbols - */ - -void load_library(char * name) -{ - if (verbose) - printf("adding library %s to search path\n",name); - - if (! lastlib) { - lastlib = libraries = malloc(sizeof(struct librarynode)); - } - else - { - lastlib->next = malloc(sizeof(struct librarynode)); - lastlib = lastlib->next; - } - - if (! lastlib) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); - } - strcpy (lastlib->name = malloc (1+strlen(name)), name); - lastlib->fp = NULL; - lastlib->referenced = 0; - lastlib->next = NULL; -} - - -/* build_symbols() step through each module's header, and locate - * exported symbols, placing them in a global table - */ - -long bsslength; - -void mod_addsymbols(struct modulenode * mod) -{ - rdfheaderrec *r; - symtabEnt e; - long cbBss; - - mod->bssrel = bsslength; - cbBss = 0; - rdfheaderrewind(&mod->f); - while ((r = rdfgetheaderrec(&mod->f))) - { - - if (r->type == 5) /* Allocate BSS */ - cbBss += r->b.amount; - - if (r->type != 3) continue; /* ignore all but export recs */ - - e.segment = r->e.segment; - e.offset = r->e.offset + - (e.segment == 0 ? mod->coderel : /* 0 -> code */ - e.segment == 1 ? mod->datarel : /* 1 -> data */ - mod->bssrel) ; /* 2 -> bss */ - - e.flags = 0; - e.name = malloc(strlen(r->e.label) + 1); - if (! e.name) - { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - strcpy(e.name,r->e.label); - symtabInsert(symtab,&e); - } - bsslength += cbBss; -} - -void build_symbols() -{ - struct modulenode *mod; - - if (verbose) printf("building global symbol table:\n"); - newheader = rdfnewheader(); - - symtab = symtabNew(); - bsslength = 0; /* keep track of location of BSS symbols */ - - for (mod = modules; mod; mod = mod->next) - { - mod_addsymbols( mod ); - } - if (verbose) - { - symtabDump(symtab,stdout); - printf("BSS length = %ld bytes\n\n",bsslength); - } -} - - -/* scan_libraries() search through headers of modules for undefined - * symbols, and scan libraries for those symbols, - * adding library modules found to list of modules - * to load. */ - -void scan_libraries(void) -{ - struct modulenode * mod, * nm; - struct librarynode * lib; - rdfheaderrec * r; - int found; - char * tmp; - - if (verbose) printf("Scanning libraries for unresolved symbols...\n"); - - mod = modules; - - while (mod) - { - rdfheaderrewind(&mod->f); - - while ((r = rdfgetheaderrec(&mod->f))) - { - if (r->type != 2) continue; /* not an import record */ - if ( symtabFind (symtab,r->i.label) ) - continue; /* symbol already defined */ - - /* okay, we have an undefined symbol... step through - the libraries now */ - if (verbose >= 2) { - printf("undefined symbol '%s'...",r->i.label); - fflush(stdout); - } - - lib = libraries; - found = 0; - - tmp = newstr(r->i.label); - while (! found && lib) - { - /* move this to an outer loop...! */ - nm = malloc(sizeof(struct modulenode)); - - if (rdl_searchlib(lib,tmp,&nm->f)) - { /* found a module in the library */ - - /* create a modulenode for it */ - - if (! nm) { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - - nm->name = newstrcat(lib->name,nm->f.name); - if (verbose >= 2) printf("found in '%s'\n",nm->name); - - nm->coderel = lastmodule->coderel + lastmodule->f.code_len; - if (nm->coderel % align != 0) - nm->coderel += align - (nm->coderel % align); - - nm->datarel = lastmodule->datarel + lastmodule->f.data_len; - if (nm->datarel % align != 0) - nm->datarel += align - (nm->datarel % align); - - nm->header = malloc(nm->f.header_len); - if (! nm->header) - { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - - if (rdfloadseg(&nm->f,RDOFF_HEADER,nm->header)) - { - rdfperror("ldrdf",nm->name); - exit(1); - } - - nm->next = NULL; - found = 1; - lastmodule->next = nm; - lastmodule = nm; - - if (verbose) - printf("%s code = %08lx (+%04lx), data = %08lx " - "(+%04lx)\n",lastmodule->name, - lastmodule->coderel,lastmodule->f.code_len, - lastmodule->datarel,lastmodule->f.data_len); - - /* add the module's info to the symbol table */ - mod_addsymbols(nm); - } - else - { - if (rdl_error) { - rdl_perror("ldrdf",lib->name); - exit(1); - } - free(nm); - } - lib = lib->next; - } - free(tmp); - if (!found && verbose >= 2) printf("not found\n"); - } - mod = mod->next; - } -} - -/* load_segments() allocates memory for & loads the code & data segs - * from the RDF modules - */ - -char *text,*data; -long textlength,datalength; - -void load_segments(void) -{ - struct modulenode *mod; - - if (!modules) { - fprintf(stderr,"ldrdf: nothing to do\n"); - exit(0); - } - if (!lastmodule) { - fprintf(stderr,"ldrdf: panic: module list exists, but lastmodule=NULL\n"); - exit(3); - } - - if (verbose) - printf("loading modules into memory\n"); - - /* The following stops 16 bit DOS from crashing whilst attempting to - work using segments > 64K */ - if (sizeof(int) == 2) { /* expect a 'code has no effect' warning on 32 bit - platforms... */ - if (lastmodule->coderel + lastmodule->f.code_len > 65535 || - lastmodule->datarel + lastmodule->f.data_len > 65535) { - fprintf(stderr,"ldrdf: segment length has exceeded 64K; use a 32 bit " - "version.\nldrdf: code size = %05lx, data size = %05lx\n", - lastmodule->coderel + lastmodule->f.code_len, - lastmodule->datarel + lastmodule->f.data_len); - exit(1); - } - } - - text = malloc(textlength = lastmodule->coderel + lastmodule->f.code_len); - data = malloc(datalength = lastmodule->datarel + lastmodule->f.data_len); - - if (!text || !data) { - fprintf(stderr,"ldrdf: out of memory\n"); - exit(1); - } - - mod = modules; - while (mod) { /* load the segments for each module */ - if (verbose >= 2) printf(" loading %s\n",mod->name); - if (rdfloadseg(&mod->f,RDOFF_CODE,&text[mod->coderel]) || - rdfloadseg(&mod->f,RDOFF_DATA,&data[mod->datarel])) { - rdfperror("ldrdf",mod->name); - exit(1); - } - rdfclose(&mod->f); /* close file; segments remain */ - mod = mod->next; - } -} - -/* link_segments() step through relocation records in each module's - * header, fixing up references. - */ - -void link_segments(void) -{ - struct modulenode *mod; - Collection imports; - symtabEnt *s; - long rel,relto; - char *seg; - rdfheaderrec *r; - int bRelative; - - if (verbose) printf("linking segments\n"); - - collection_init(&imports); - - for (mod = modules; mod; mod = mod->next) { - if (verbose >= 2) printf("* processing %s\n",mod->name); - rdfheaderrewind(&mod->f); - while((r = rdfgetheaderrec(&mod->f))) { - if (verbose >= 3) printf("record type: %d\n",r->type); - switch(r->type) { - case 1: /* relocation record */ - if (r->r.segment >= 64) { /* Relative relocation; */ - bRelative = 1; /* need to find location relative */ - r->r.segment -= 64; /* to start of this segment */ - relto = r->r.segment == 0 ? mod->coderel : mod->datarel; - } - else - { - bRelative = 0; /* non-relative - need to relocate - * at load time */ - relto = 0; /* placate optimiser warnings */ - } - - /* calculate absolute offset of reference, not rel to beginning of - segment */ - r->r.offset += r->r.segment == 0 ? mod->coderel : mod->datarel; - - /* calculate the relocation factor to apply to the operand - - the base address of one of this modules segments if referred - segment is 0 - 2, or the address of an imported symbol - otherwise. */ - - if (r->r.refseg == 0) rel = mod->coderel; - else if (r->r.refseg == 1) rel = mod->datarel; - else if (r->r.refseg == 2) rel = mod->bssrel; - else { /* cross module link - find reference */ - s = *colln(&imports,r->r.refseg - 2); - if (!s) { - fprintf(stderr,"ldrdf: link to undefined segment %04x in" - " %s:%d\n", r->r.refseg,mod->name,r->r.segment); - errors = 1; - break; - } - rel = s->offset; - - r->r.refseg = s->segment; /* change referred segment, - so that new header is - correct */ - } - - if (bRelative) /* Relative - subtract current segment start */ - rel -= relto; - else - { /* Add new relocation header */ - rdfaddheader(newheader,r); - } - - /* Work out which segment we're making changes to ... */ - if (r->r.segment == 0) seg = text; - else if (r->r.segment == 1) seg = data; - else { - fprintf(stderr,"ldrdf: relocation in unknown segment %d in " - "%s\n", r->r.segment,mod->name); - errors = 1; - break; - } - - /* Add the relocation factor to the datum specified: */ - - if (verbose >= 3) - printf(" - relocating %d:%08lx by %08lx\n",r->r.segment, - r->r.offset,rel); - - /**** The following code is non-portable. Rewrite it... ****/ - switch(r->r.length) { - case 1: - seg[r->r.offset] += (char) rel; - break; - case 2: - *(int16 *)(seg + r->r.offset) += (int16) rel; - break; - case 4: - *(long *)(seg + r->r.offset) += rel; - break; - } - break; - - case 2: /* import record */ - s = symtabFind(symtab, r->i.label); - if (s == NULL) { - /* Need to add support for dynamic linkage */ - fprintf(stderr,"ldrdf: undefined symbol %s in module %s\n", - r->i.label,mod->name); - errors = 1; - } - else - { - *colln(&imports,r->i.segment - 2) = s; - if (verbose >= 2) - printf("imported %s as %04x\n", r->i.label, r->i.segment); - } - break; - - case 3: /* export; dump to output new version */ - s = symtabFind(symtab, r->e.label); - if (! s) { - fprintf(stderr,"ldrdf: internal error - undefined symbol %s " - "exported in header of '%s'\n",r->e.label,mod->name); - continue; - } - r->e.offset = s->offset; - rdfaddheader(newheader,r); - break; - - case 4: /* DLL record */ - rdfaddheader(newheader,r); /* copy straight to output */ - break; - } - } - if (rdf_errno != 0) { - rdfperror("ldrdf",mod->name); - exit(1); - } - collection_reset(&imports); - } -} - -/* write_output() write linked program out to a file */ - -void write_output(char *filename) -{ - FILE * fp; - rdfheaderrec r; - - if (verbose) printf("writing output to '%s'\n",filename); - - fp = fopen(filename,"wb"); - if (! fp) - { - fprintf(stderr,"ldrdf: could not open '%s' for writing\n",filename); - exit(1); - } - - - /* add BSS length count to header... */ - if (bsslength) - { - r.type = 5; - r.b.amount = bsslength; - rdfaddheader(newheader,&r); - } - - /* Write header */ - rdfwriteheader(fp,newheader); - rdfdoneheader(newheader); - newheader = NULL; - - /* Write text */ - if (fwrite(&textlength,1,4,fp) != 4 - || fwrite(text,1,textlength,fp) !=textlength) - { - fprintf(stderr,"ldrdf: error writing %s\n",filename); - exit(1); - } - - /* Write data */ - if (fwrite(&datalength,1,4,fp) != 4 || - fwrite(data,1,datalength,fp) != datalength) - { - fprintf (stderr,"ldrdf: error writing %s\n", filename); - exit(1); - } - fclose(fp); -} - - -/* main program: interpret command line, and pass parameters on to - * individual module loaders & the linker - * - * Command line format: - * ldrdf [-o outfile | -x] [-r xxxx] [-v] [--] infile [infile ...] - * - * Default action is to output a file named 'aout.rdx'. -x specifies - * that the linked object program should be executed, rather than - * written to a file. -r specifies that the object program should - * be prelocated at address 'xxxx'. This option cannot be used - * in conjunction with -x. - */ - -const char *usagemsg = "usage:\n" -" ldrdf [-o outfile | -x] [-a x] [-v] [-p x] [--] infile [infile ...]\n" -" [-l<libname> ...]\n\n" -" ldrdf -h displays this message\n" -" ldrdf -r displays version information\n\n" -" -o selects output filename (default is aout.rdx)\n" -" -x causes ldrdx to link & execute rather than write to file\n" -" -a x causes object program to be statically relocated to address 'x'\n" -" -v turns on verbose mode\n" -" -p x causes segments to be aligned (padded) to x byte boundaries\n" -" (default is 16 bytes)\n" -" -l<name> causes 'name' to be linked in as a library. Note no search is\n" -" performed - the entire pathname MUST be specified.\n"; - -void usage(void) -{ - fputs(usagemsg,stderr); -} - -int main(int argc,char **argv) -{ - char *ofilename = "aout.rdx"; - long relocateaddr = -1; /* -1 if no relocation is to occur */ - int execute = 0; /* 1 to execute after linking, 0 otherwise */ - int procsw = 1; /* set to 0 by '--' */ - int tmp; - - if (argc == 1) { - usage(); - exit(1); - } - - /* process command line switches, and add modules specified to linked list - of modules, keeping track of total memory required to load them */ - - while(argv++,--argc) { - if (procsw && !strcmp(*argv,"-h")) { /* Help command */ - usage(); exit(1); - } - else if (procsw && !strcmp(*argv,"-r")) { - printf("ldrdf version %s (%s) (%s)\n",LDRDF_VERSION,_RDOFF_H, - sizeof(int) == 2 ? "16 bit" : "32 bit"); - exit(1); - } - else if (procsw && !strcmp(*argv,"-o")) { - ofilename = *++argv; - --argc; - if (execute) { - fprintf(stderr,"ldrdf: -o and -x switches incompatible\n"); - exit(1); - } - if (verbose > 1) printf("output filename set to '%s'\n",ofilename); - } - else if (procsw && !strcmp(*argv,"-x")) { - execute++; - if (verbose > 1) printf("will execute linked object\n"); - } - else if (procsw && !strcmp(*argv,"-a")) { - relocateaddr = readnum(*++argv,&tmp); - --argc; - if (tmp) { - fprintf(stderr,"ldrdf: error in parameter to '-a' switch: '%s'\n", - *argv); - exit(1); - } - if (execute) { - fprintf(stderr,"ldrdf: -a and -x switches incompatible\n"); - exit(1); - } - if (verbose) printf("will relocate to %08lx\n",relocateaddr); - } - else if (procsw && !strcmp(*argv,"-v")) { - verbose++; - if (verbose == 1) printf("verbose mode selected\n"); - } - else if (procsw && !strcmp(*argv,"-p")) { - align = readnum(*++argv,&tmp); - --argc; - if (tmp) { - fprintf(stderr,"ldrdf: error in parameter to '-p' switch: '%s'\n", - *argv); - exit(1); - } - if (align != 1 && align != 2 && align != 4 && align != 8 && align != 16 - && align != 32 && align != 256) { - fprintf(stderr,"ldrdf: %d is an invalid alignment factor - must be" - "1,2,4,8,16 or 256\n",align); - exit(1); - } - if (verbose > 1) printf("alignment %d selected\n",align); - } - else if (procsw && !strncmp(*argv,"-l",2)) { - load_library(*argv + 2); - } - else if (procsw && !strcmp(*argv,"--")) { - procsw = 0; - } - else { /* is a filename */ - if (verbose > 1) printf("processing module %s\n",*argv); - loadmodule(*argv); - } - } - - /* we should be scanning for unresolved references, and removing - unreferenced modules from the list of modules here, so that - we know about the final size once libraries have been linked in */ - - build_symbols(); /* build a global symbol table... */ - - scan_libraries(); /* check for imported symbols not in table, - and ensure the relevant library modules - are loaded */ - - load_segments(); /* having calculated size of reqd segments, load - each rdoff module's segments into memory */ - - link_segments(); /* step through each module's header, and resolve - references to the global symbol table. - This also does local address fixups. */ - - if (errors) { - fprintf(stderr,"ldrdf: there were errors - aborted\n"); - exit(errors); - } - if (execute) { - fprintf(stderr,"ldrdf: module execution not yet supported\n"); - exit(1); - } - if (relocateaddr != -1) { - fprintf(stderr,"ldrdf: static relocation not yet supported\n"); - exit(1); - } - - write_output(ofilename); - return 0; -} diff --git a/rdoff/v1/rdf.doc b/rdoff/v1/rdf.doc deleted file mode 100644 index 300c2bc..0000000 --- a/rdoff/v1/rdf.doc +++ /dev/null @@ -1,99 +0,0 @@ -RDOFF: Relocatable Dynamically-linked Object File Format -======================================================== - -RDOFF was designed initially to test the object-file production -interface to NASM. It soon became apparent that it could be enhanced -for use in serious applications due to its simplicity; code to load -and execute an RDOFF object module is very simple. It also contains -enhancements to allow it to be linked with a dynamic link library at -either run- or load- time, depending on how complex you wish to make -your loader. - -The RDOFF format (version 1.1, as produced by NASM v0.91) is defined -as follows: - -The first six bytes of the file contain the string 'RDOFF1'. Other -versions of the format may contain other last characters other than -'1' - all little endian versions of the file will always contain an -ASCII character with value greater than 32. If RDOFF is used on a -big-endian machine at some point in the future, the version will be -encoded in decimal rather than ASCII, so will be below 32. - -All multi-byte fields follwing this are encoded in either little- or -big-endian format depending on the system described by this version -information. Object files should be encoded in the endianness of -their target machine; files of incorrect endianness will be rejected -by the loader - this means that loaders do not need to convert -endianness, as RDOFF has been designed with simplicity of loading at -the forefront of the design requirements. - -The next 4 byte field is the length of the header in bytes. The -header consists of a sequence of variable length records. Each -record's type is identified by the first byte of the record. Record -types 1-4 are currently supported. Record type 5 will be added in -the near future, when I implement BSS segments. Record type 6 may be -to do with debugging, when I get debugging implemented. - -Type 1: Relocation -================== - -Offset Length Description -0 1 Type (contains 1) -1 1 Segment that contains reference (0 = text, 1 = data) - Add 64 to this number to indicate a relative linkage - to an external symbol (see notes) -2 4 Offset of reference -6 1 Length of reference (1,2 or 4 bytes) -7 2 Segment to which reference is made (0 = text, 1 = - data, 2 = BSS [when implemented]) others are external - symbols. - -Total length = 9 bytes - -Type 2: Symbol Import -===================== - -0 1 Type (2) -1 2 Segment number that will be used in references to this - symbol. -3 ? Null terminated string containing label (up to 32 - chars) to match against exports in linkage. - -Type 3: Symbol Export -===================== - -0 1 Type (3) -1 1 Segment containing object to be exported (0/1/2) -2 4 Offset within segment -6 ? Null terminate string containing label to export (32 - char maximum length) - -Type 4: Dynamic Link Library -============================ - -0 1 Type (4) -1 ? Library name (up to 128 chars) - -Type 5: Reserve BSS -=================== - -0 1 Type (5) -1 4 Amount of BSS space to reserve in bytes - -Total length: 5 bytes - ------------------------------------------------------------------------------ - -Following the header is the text (code) segment. This is preceded by -a 4-byte integer, which is its length in bytes. This is followed by -the length of the data segment (also 4 bytes), and finally the data -segment. - -Notes -===== - -Relative linking: The number stored at the address is offset -required from the imported symbol, with the address of the end of -the instruction subtracted from it. This means that the linker can -simply add the address of the label relative to the beginning of the -current segment to it. diff --git a/rdoff/v1/rdf2bin.c b/rdoff/v1/rdf2bin.c deleted file mode 100644 index 97b45b4..0000000 --- a/rdoff/v1/rdf2bin.c +++ /dev/null @@ -1,125 +0,0 @@ -/* rdf2bin: convert an RDOFF object file to flat binary */ - -#include <stdlib.h> -#include <stdio.h> -#include <string.h> - -#include "rdfload.h" -#include "rdoff.h" -#include "nasmlib.h" - -long origin = 0; -int align = 16; - -char *getfilename(char * pathname) -{ - char * lastslash = pathname - 1; - char * i = pathname; - - while ( *i ) { - if (*i == '/') lastslash = i; - i++; - } - return lastslash + 1; -} - -int main(int argc, char **argv) -{ - rdfmodule * m; - int tmp; - FILE *of; - char * padding; - int codepad, datapad; - - if (argc < 2) { - puts("Usage: rdf2bin [-o relocation-origin] [-p segment-alignment] " - "input-file output-file"); - puts(" rdf2com [-p segment-alignment] input-file output-file"); - return 1; - } - - if (! nasm_stricmp(getfilename(*argv),"rdf2com")) { - origin = 0x100; - } - argv++, argc--; - - while (argc > 2) { - if (! strcmp(*argv,"-o")) { - argv++, argc--; - origin = readnum(*argv, &tmp); - if (tmp) { - fprintf(stderr,"rdf2bin: invalid parameter: %s\n",*argv); - return 1; - } - } else if (! strcmp(*argv,"-p")) { - argv++, argc--; - align = readnum(*argv, &tmp); - if (tmp) { - fprintf(stderr,"rdf2bin: invalid parameter: %s\n",*argv); - return 1; - } - } else - break; - - argv++, argc--; - } - if (argc < 2) { - puts("rdf2bin: required parameter missing"); - return -1; - } - m = rdfload(*argv); - - if (! m) - { - rdfperror("rdf2bin",*argv); - return 1; - } - printf("relocating %s: origin=%lx, align=%d\n",*argv,origin,align); - - m->textrel = origin; - m->datarel = origin + m->f.code_len; - if (m->datarel % align != 0) { - codepad = align - (m->datarel % align); - m->datarel += codepad; - } - else - codepad = 0; - - m->bssrel = m->datarel + m->f.data_len; - if (m->bssrel % align != 0) { - datapad = align - (m->bssrel % align); - m->bssrel += datapad; - } - else - datapad = 0; - - printf("code: %08lx\ndata: %08lx\nbss: %08lx\n", - m->textrel, m->datarel, m->bssrel); - - rdf_relocate(m); - - argv++; - - of = fopen(*argv,"wb"); - if (!of) { - fprintf(stderr,"rdf2bin: could not open output file %s\n",*argv); - return 1; - } - - padding = malloc(align); - if (!padding) { - fprintf(stderr,"rdf2bin: out of memory\n"); - return 1; - } - - if (fwrite(m->t,1,m->f.code_len,of) != m->f.code_len || - fwrite(padding,1,codepad,of) != codepad || - fwrite(m->d,1,m->f.data_len,of) != m->f.data_len) - { - fprintf(stderr,"rdf2bin: error writing to %s\n", *argv); - return 1; - } - - fclose(of); - return 0; -} diff --git a/rdoff/v1/rdfdump.c b/rdoff/v1/rdfdump.c deleted file mode 100644 index 080c2e7..0000000 --- a/rdoff/v1/rdfdump.c +++ /dev/null @@ -1,167 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> - -FILE *infile; - -long translatelong(long in) { /* translate from little endian to - local representation */ - long r; - unsigned char *i; - - i = (unsigned char *)∈ - r = i[3]; - r = (r << 8) + i[2]; - r = (r << 8) + i[1]; - r = (r << 8) + *i; - - return r; -} - -int translateshort(short in) { - int r; - unsigned char *i; - - i = (unsigned char *)∈ - r = (i[1] << 8) + *i; - - return r; -} -void print_header(long length) { - char buf[129],t,s,l; - long o,ll; - short rs; - - while (length > 0) { - fread(&t,1,1,infile); - switch(t) { - case 1: /* relocation record */ - fread(&s,1,1,infile); - fread(&o,4,1,infile); - fread(&l,1,1,infile); - fread(&rs,2,1,infile); - printf(" relocation: location (%04x:%08lx), length %d, " - "referred seg %04x\n",(int)s,translatelong(o),(int)l, - translateshort(rs)); - length -= 9; - break; - case 2: /* import record */ - fread(&rs,2,1,infile); - ll = 0; - do { - fread(&buf[ll],1,1,infile); - } while (buf[ll++]); - printf(" import: segment %04x = %s\n",translateshort(rs),buf); - length -= ll + 3; - break; - case 3: /* export record */ - fread(&s,1,1,infile); - fread(&o,4,1,infile); - ll = 0; - do { - fread(&buf[ll],1,1,infile); - } while (buf[ll++]); - printf(" export: (%04x:%08lx) = %s\n",(int)s,translatelong(o),buf); - length -= ll + 6; - break; - case 4: /* DLL record */ - ll = 0; - do { - fread(&buf[ll],1,1,infile); - } while (buf[ll++]); - printf(" dll: %s\n",buf); - length -= ll + 1; - break; - case 5: /* BSS reservation */ - fread(&ll,4,1,infile); - printf(" bss reservation: %08lx bytes\n",translatelong(ll)); - length -= 5; - break; - default: - printf(" unrecognised record (type %d)\n",(int)t); - length --; - } - } -} - -int main(int argc,char **argv) { - char id[7]; - long l; - int verbose = 0; - long offset; - - puts("RDOFF Dump utility v1.1 (C) Copyright 1996 Julian R Hall"); - - if (argc < 2) { - fputs("Usage: rdfdump [-v] <filename>\n",stderr); - exit(1); - } - - if (! strcmp (argv[1], "-v") ) - { - verbose = 1; - if (argc < 3) - { - fputs("required parameter missing\n",stderr); - exit(1); - } - argv++; - } - - infile = fopen(argv[1],"rb"); - if (! infile) { - fprintf(stderr,"rdfdump: Could not open %s",argv[1]); - exit(1); - } - - fread(id,6,1,infile); - if (strncmp(id,"RDOFF",5)) { - fputs("rdfdump: File does not contain valid RDOFF header\n",stderr); - exit(1); - } - - printf("File %s: RDOFF version %c\n\n",argv[1],id[5]); - if (id[5] < '1' || id[5] > '1') { - fprintf(stderr,"rdfdump: unknown RDOFF version '%c'\n",id[5]); - exit(1); - } - - fread(&l,4,1,infile); - l = translatelong(l); - printf("Header (%ld bytes):\n",l); - print_header(l); - - fread(&l,4,1,infile); - l = translatelong(l); - printf("\nText segment length = %ld bytes\n",l); - offset = 0; - while(l--) { - fread(id,1,1,infile); - if (verbose) { - if (offset % 16 == 0) - printf("\n%08lx ", offset); - printf(" %02x",(int) (unsigned char)id[0]); - offset++; - } - } - if (verbose) printf("\n\n"); - - fread(&l,4,1,infile); - l = translatelong(l); - printf("Data segment length = %ld bytes\n",l); - - if (verbose) - { - offset = 0; - while (l--) { - fread(id,1,1,infile); - if (offset % 16 == 0) - printf("\n%08lx ", offset); - printf(" %02x",(int) (unsigned char) id[0]); - offset++; - } - printf("\n"); - } - fclose(infile); - return 0; -} diff --git a/rdoff/v1/rdflib.c b/rdoff/v1/rdflib.c deleted file mode 100644 index 5846562..0000000 --- a/rdoff/v1/rdflib.c +++ /dev/null @@ -1,235 +0,0 @@ -/* rdflib - manipulate RDOFF library files (.rdl) */ - -/* an rdoff library is simply a sequence of RDOFF object files, each - preceded by the name of the module, an ASCII string of up to 255 - characters, terminated by a zero. There may be an optional - directory placed on the end of the file. The format of the - directory will be 'RDL' followed by a version number, followed by - the length of the directory, and then the directory, the format of - which has not yet been designed. */ - -#include <stdio.h> -#include <errno.h> -#include <string.h> - -/* functions supported: - create a library (no extra operands required) - add a module from a library (requires filename and name to give mod.) - remove a module from a library (requires given name) - extract a module from the library (requires given name and filename) - list modules */ - -const char *usage = - "usage:\n" - " rdflib x libname [extra operands]\n\n" - " where x is one of:\n" - " c - create library\n" - " a - add module (operands = filename module-name)\n" - " r - remove (module-name)\n" - " x - extract (module-name filename)\n" - " t - list\n"; - -char **_argv; - -#define _ENDIANNESS 0 /* 0 for little, 1 for big */ - -static void longtolocal(long * l) -{ -#if _ENDIANNESS - unsigned char t; - unsigned char * p = (unsigned char *) l; - - t = p[0]; - p[0] = p[3]; - p[3] = t; - t = p[1]; - p[1] = p[2]; - p[2] = p[1]; -#endif -} - -void copybytes(FILE *fp, FILE *fp2, int n) -{ - int i,t; - - for (i = 0 ; i < n; i++ ) - { - t = fgetc(fp); - if (t == EOF) - { - fprintf(stderr,"ldrdf: premature end of file in '%s'\n", - _argv[2]); - exit(1); - } - if (fp2) - if (fputc(t, fp2) == EOF) - { - fprintf(stderr,"ldrdf: write error\n"); - exit(1); - } - } -} - -long copylong(FILE *fp, FILE *fp2) -{ - long l; - int i,t; - unsigned char * p = (unsigned char *) &l; - - - for (i = 0 ; i < 4; i++ ) /* skip magic no */ - { - t = fgetc(fp); - if (t == EOF) - { - fprintf(stderr,"ldrdf: premature end of file in '%s'\n", - _argv[2]); - exit(1); - } - if (fp2) - if (fputc(t, fp2) == EOF) - { - fprintf(stderr,"ldrdf: write error\n"); - exit(1); - } - *p++ = t; - } - longtolocal (&l); - return l; -} - -int main(int argc, char **argv) -{ - FILE *fp, *fp2; - char *p, buf[256]; - int i; - - _argv = argv; - - if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3)) - { - printf(usage); - exit(1); - } - - switch(argv[1][0]) - { - case 'c': /* create library */ - fp = fopen(argv[2],"wb"); - if (! fp) { - fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]); - perror("ldrdf"); - exit(1); - } - fclose(fp); - break; - - case 'a': /* add module */ - if (argc < 5) { - fprintf(stderr,"ldrdf: required parameter missing\n"); - exit(1); - } - fp = fopen(argv[2],"ab"); - if (! fp) - { - fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]); - perror("ldrdf"); - exit(1); - } - - fp2 = fopen(argv[3],"rb"); - if (! fp) - { - fprintf(stderr,"ldrdf: could not open '%s'\n",argv[3]); - perror("ldrdf"); - exit(1); - } - - p = argv[4]; - do { - if ( fputc(*p,fp) == EOF ) { - fprintf(stderr,"ldrdf: write error\n"); - exit(1); - } - } while (*p++); - - while (! feof (fp2) ) { - i = fgetc (fp2); - if (i == EOF) { - break; - } - - if ( fputc(i, fp) == EOF ) { - fprintf(stderr,"ldrdf: write error\n"); - exit(1); - } - } - fclose(fp2); - fclose(fp); - break; - - case 'x': - if (argc < 5) { - fprintf(stderr,"ldrdf: required parameter missing\n"); - exit(1); - } - - fp = fopen(argv[2],"rb"); - if (! fp) - { - fprintf(stderr,"ldrdf: could not open '%s'\n",argv[2]); - perror("ldrdf"); - exit(1); - } - - fp2 = NULL; - while (! feof(fp) ) { - /* read name */ - p = buf; - while( ( *(p++) = (char) fgetc(fp) ) ) - if (feof(fp)) break; - - if (feof(fp)) break; - - /* check against desired name */ - if (! strcmp(buf,argv[3]) ) - { - fp2 = fopen(argv[4],"wb"); - if (! fp2) - { - fprintf(stderr,"ldrdf: could not open '%s'\n", argv[4]); - perror("ldrdf"); - exit(1); - } - } - else - fp2 = NULL; - - /* step over the RDOFF file, copying it if fp2 != NULL */ - copybytes(fp,fp2,6); /* magic number */ - copybytes(fp,fp2, copylong(fp,fp2)); /* header */ - copybytes(fp,fp2, copylong(fp,fp2)); /* text */ - copybytes(fp,fp2, copylong(fp,fp2)); /* data */ - - if (fp2) - break; - } - fclose(fp); - if (fp2) - fclose(fp2); - else - { - fprintf(stderr,"ldrdf: module '%s' not found in '%s'\n", - argv[3],argv[2]); - exit(1); - } - break; - - default: - fprintf(stderr,"ldrdf: command '%c' not recognised\n", - argv[1][0]); - exit(1); - } - return 0; -} - diff --git a/rdoff/v1/rdfload.c b/rdoff/v1/rdfload.c deleted file mode 100644 index b848344..0000000 --- a/rdoff/v1/rdfload.c +++ /dev/null @@ -1,173 +0,0 @@ -/* rdfload.c RDOFF Object File loader library - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - * - * Permission to use this file in your own projects is granted, as long - * as acknowledgement is given in an appropriate manner to its authors, - * with instructions of how to obtain a copy via ftp. - */ - -#include <stdlib.h> -#include <stdio.h> - -#include "rdfload.h" -#include "symtab.h" -#include "rdoff.h" -#include "collectn.h" - -extern int rdf_errno; - -rdfmodule * rdfload(const char *filename) -{ - rdfmodule * f = malloc(sizeof(rdfmodule)); - long bsslength = 0; - char * hdr; - rdfheaderrec *r; - - if (f == NULL) - { - rdf_errno = 6; /* out of memory */ - return NULL; - } - - f->symtab = symtabNew(); - if (!f->symtab) - { - free(f); - rdf_errno = 6; - return NULL; - } - - /* open the file */ - if ( rdfopen( &(f->f), filename ) ) { - free(f); - return NULL; - } - - /* read in text and data segments, and header */ - - f->t = malloc (f->f.code_len); - f->d = malloc (f->f.data_len); /* BSS seg allocated later */ - hdr = malloc (f->f.header_len); - - if (! f->t || ! f->d || !hdr) { - rdf_errno = 6; - rdfclose(&f->f); - if (f->t) free(f->t); - if (f->d) free(f->d); - free(f); - return NULL; - } - - if ( rdfloadseg (&f->f,RDOFF_HEADER,hdr) || - rdfloadseg (&f->f,RDOFF_CODE,f->t) || - rdfloadseg (&f->f,RDOFF_DATA,f->d) ) - { - rdfclose(&f->f); - free(f->t); - free(f->d); - free(f); - free(hdr); - return NULL; - } - - rdfclose(&f->f); - - /* Allocate BSS segment; step through header and count BSS records */ - - while ( ( r = rdfgetheaderrec (&f->f) ) ) - { - if (r->type == 5) - bsslength += r->b.amount; - } - - f->b = malloc ( bsslength ); - if (! f->b ) - { - free(f->t); - free(f->d); - free(f); - free(hdr); - rdf_errno = 6; - return NULL; - } - - rdfheaderrewind (&f->f); - - f->textrel = (long)f->t; - f->datarel = (long)f->d; - f->bssrel = (long)f->b; - - return f; -} - -int rdf_relocate(rdfmodule * m) -{ - rdfheaderrec * r; - Collection imports; - symtabEnt e; - long rel; - unsigned char * seg; - - rdfheaderrewind ( & m->f ); - collection_init(&imports); - - while ( (r = rdfgetheaderrec ( & m->f ) ) ) - { - switch (r->type) - { - case 1: /* Relocation record */ - - /* calculate relocation factor */ - - if (r->r.refseg == 0) rel = m->textrel; - else if (r->r.refseg == 1) rel = m->datarel; - else if (r->r.refseg == 2) rel = m->bssrel; - else - /* We currently do not support load-time linkage. - This should be added some time soon... */ - - return 1; /* return error code */ - - if ((r->r.segment & 63) == 0) seg = m->t; - else if ((r->r.segment & 63) == 1) seg = m->d; - else - return 1; - - /* it doesn't matter in this case that the code is non-portable, - as the entire concept of executing a module like this is - non-portable */ - switch(r->r.length) { - case 1: - seg[r->r.offset] += (char) rel; - break; - case 2: - *(int16 *)(seg + r->r.offset) += (int16) rel; - break; - case 4: - *(long *)(seg + r->r.offset) += rel; - break; - } - break; - - case 3: /* export record - add to symtab */ - e.segment = r->e.segment; - e.offset = r->e.offset + - (e.segment == 0 ? m->textrel : /* 0 -> code */ - e.segment == 1 ? m->datarel : /* 1 -> data */ - m->bssrel) ; /* 2 -> bss */ - e.flags = 0; - e.name = malloc(strlen(r->e.label) + 1); - if (! e.name) - return 1; - - strcpy(e.name,r->e.label); - symtabInsert(m->symtab,&e); - break; - } - } - return 0; -} diff --git a/rdoff/v1/rdfload.h b/rdoff/v1/rdfload.h deleted file mode 100644 index 5e264b9..0000000 --- a/rdoff/v1/rdfload.h +++ /dev/null @@ -1,29 +0,0 @@ -/* rdfload.h RDOFF Object File loader library header file - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - * - * See the file 'rdfload.c' for special license information for this - * file. - */ - -#ifndef _RDFLOAD_H -#define _RDFLOAD_H - -#include "rdoff.h" - -typedef struct RDFModuleStruct { - rdffile f; /* file structure */ - unsigned char * t, * d, * b; /* text, data, and bss segments */ - long textrel; - long datarel; - long bssrel; - void * symtab; -} rdfmodule; - -rdfmodule * rdfload(const char * filename); -int rdf_relocate(rdfmodule * m); - -#endif diff --git a/rdoff/v1/rdlib.c b/rdoff/v1/rdlib.c deleted file mode 100644 index bc8d1e3..0000000 --- a/rdoff/v1/rdlib.c +++ /dev/null @@ -1,88 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -#include "rdoff.h" -#include "rdlib.h" - -int rdl_error = 0; - -char *rdl_errors[3] = { - "no error","could not open file", "invalid file structure", -}; - -int rdl_searchlib (struct librarynode * lib, - const char * label, rdffile * f) -{ - char buf[257]; - int i; - void * hdr; - rdfheaderrec * r; - - rdl_error = 0; - lib->referenced ++; - - if (! lib->fp) - { - lib->fp = fopen(lib->name,"rb"); - - if (! lib->fp) { - rdl_error = 1; - return 0; - } - } - else - rewind(lib->fp); - - while (! feof(lib->fp) ) - { - i = 1; - while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 257) - i++; - buf[0] = ':'; - - if (feof(lib->fp)) break; - - if ( rdfopenhere(f,lib->fp,&lib->referenced,buf) ) { - rdl_error = 2; - return 0; - } - - hdr = malloc(f->header_len); - rdfloadseg(f,RDOFF_HEADER,hdr); - - while ((r = rdfgetheaderrec(f))) - { - if (r->type != 3) /* not an export */ - continue; - - if (! strcmp(r->e.label, label) ) /* match! */ - { - free(hdr); /* reset to 'just open' */ - f->header_loc = NULL; /* state... */ - f->header_fp = 0; - return 1; - } - } - - /* find start of next module... */ - i = f->data_ofs + f->data_len; - rdfclose(f); - fseek(lib->fp,i,SEEK_SET); - } - - lib->referenced --; - if (! lib->referenced) - { - fclose(lib->fp); - lib->fp = NULL; - } - return 0; -} - -void rdl_perror(const char *apname, const char *filename) -{ - fprintf(stderr,"%s:%s:%s\n",apname,filename,rdl_errors[rdl_error]); -} - - - diff --git a/rdoff/v1/rdlib.h b/rdoff/v1/rdlib.h deleted file mode 100644 index 94592ce..0000000 --- a/rdoff/v1/rdlib.h +++ /dev/null @@ -1,18 +0,0 @@ -/* rdlib.h Functions for manipulating librarys of RDOFF object files */ - - -struct librarynode { - char * name; - FILE * fp; /* initialised to NULL - always check*/ - int referenced; /* & open if required. Close afterwards */ - struct librarynode * next; /* if ! referenced. */ -}; - - -extern int rdl_error; - -int rdl_searchlib (struct librarynode * lib, - const char * label, rdffile * f); -void rdl_perror(const char *apname, const char *filename); - - diff --git a/rdoff/v1/rdoff.c b/rdoff/v1/rdoff.c deleted file mode 100644 index 96620ec..0000000 --- a/rdoff/v1/rdoff.c +++ /dev/null @@ -1,397 +0,0 @@ -/* rdoff.c library of routines for manipulating rdoff files - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - */ - -/* TODO: The functions in this module assume they are running - * on a little-endian machine. This should be fixed to - * make it portable. - */ - -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <errno.h> - -#include "rdoff.h" - -#define newstr(str) strcpy(malloc(strlen(str) + 1),str) -#define newstrcat(s1,s2) strcat(strcpy(malloc(strlen(s1) + strlen(s2) + 1), \ - s1),s2) - -/* ======================================================================== - * Code for memory buffers (for delayed writing of header until we know - * how long it is). - * ======================================================================== */ - - -memorybuffer * newmembuf(){ - memorybuffer * t; - - t = malloc(sizeof(memorybuffer)); - - t->length = 0; - t->next = NULL; - return t; -} - -void membufwrite(memorybuffer *b, void *data, int bytes) { - int16 w; - long l; - - if (b->next) { /* memory buffer full - use next buffer */ - membufwrite(b->next,data,bytes); - return; - } - if ((bytes < 0 && b->length - bytes > BUF_BLOCK_LEN) - || (bytes > 0 && b->length + bytes > BUF_BLOCK_LEN)) { - - /* buffer full and no next allocated... allocate and initialise next - * buffer */ - - b->next = newmembuf(); - membufwrite(b->next,data,bytes); - } - - switch(bytes) { - case -4: /* convert to little-endian */ - l = * (long *) data ; - b->buffer[b->length++] = l & 0xFF; - l >>= 8 ; - b->buffer[b->length++] = l & 0xFF; - l >>= 8 ; - b->buffer[b->length++] = l & 0xFF; - l >>= 8 ; - b->buffer[b->length++] = l & 0xFF; - break; - - case -2: - w = * (int16 *) data ; - b->buffer[b->length++] = w & 0xFF; - w >>= 8 ; - b->buffer[b->length++] = w & 0xFF; - break; - - default: - while(bytes--) { - b->buffer[b->length++] = *(* (unsigned char **) &data); - - (* (unsigned char **) &data)++ ; - } - break; - } -} - -void membufdump(memorybuffer *b,FILE *fp) -{ - if (!b) return; - - fwrite (b->buffer, 1, b->length, fp); - - membufdump(b->next,fp); -} - -int membuflength(memorybuffer *b) -{ - if (!b) return 0; - return b->length + membuflength(b->next); -} - -void freemembuf(memorybuffer *b) -{ - if (!b) return; - freemembuf(b->next); - free(b); -} - -/* ========================================================================= - General purpose routines and variables used by the library functions - ========================================================================= */ - -long translatelong(long in) { /* translate from little endian to - local representation */ - long r; - unsigned char *i; - - i = (unsigned char *)∈ - r = i[3]; - r = (r << 8) + i[2]; - r = (r << 8) + i[1]; - r = (r << 8) + *i; - - return r; -} - -const char *RDOFFId = "RDOFF1"; /* written to the start of RDOFF files */ - -const char *rdf_errors[7] = { - "no error occurred","could not open file","invalid file format", - "error reading file","unknown error","header not read", - "out of memory"}; - -int rdf_errno = 0; - -/* ======================================================================== - The library functions - ======================================================================== */ - -int rdfopen(rdffile *f, const char *name) -{ - FILE * fp; - - fp = fopen(name,"rb"); - if (!fp) return rdf_errno = 1; /* error 1: file open error */ - - return rdfopenhere(f,fp,NULL,""); -} - -int rdfopenhere(rdffile *f, FILE *fp, int *refcount, char *name) -{ - char buf[8]; - long initpos; - - if (translatelong(0x01020304) != 0x01020304) - { /* fix this to be portable! */ - fputs("*** this program requires a little endian machine\n",stderr); - fprintf(stderr,"01020304h = %08lxh\n",translatelong(0x01020304)); - exit(3); - } - - f->fp = fp; - initpos = ftell(fp); - - fread(buf,6,1,f->fp); /* read header */ - buf[6] = 0; - - if (strcmp(buf,RDOFFId)) { - fclose(f->fp); - return rdf_errno = 2; /* error 2: invalid file format */ - } - - if (fread(&f->header_len,1,4,f->fp) != 4) { - fclose(f->fp); - return rdf_errno = 3; /* error 3: file read error */ - } - - f->header_ofs = ftell(f->fp); - - if (fseek(f->fp,f->header_len,SEEK_CUR)) { - fclose(f->fp); - return rdf_errno = 2; /* seek past end of file...? */ - } - - if (fread(&f->code_len,1,4,f->fp) != 4) { - fclose(f->fp); - return rdf_errno = 3; - } - - f->code_ofs = ftell(f->fp); - if (fseek(f->fp,f->code_len,SEEK_CUR)) { - fclose(f->fp); - return rdf_errno = 2; - } - - if (fread(&f->data_len,1,4,f->fp) != 4) { - fclose(f->fp); - return rdf_errno = 3; - } - - f->data_ofs = ftell(f->fp); - fseek(f->fp,initpos,SEEK_SET); - f->header_loc = NULL; - - f->name = newstr(name); - f->refcount = refcount; - if (refcount) (*refcount)++; - return 0; -} - -int rdfclose(rdffile *f) -{ - if (! f->refcount || ! *--f->refcount) - fclose(f->fp); - free(f->name); - - return 0; -} - -void rdfperror(const char *app,const char *name) -{ - fprintf(stderr,"%s:%s: %s\n",app,name,rdf_errors[rdf_errno]); - if (rdf_errno == 1 || rdf_errno == 3) - { - perror(app); - } - -} - -int rdfloadseg(rdffile *f,int segment,void *buffer) -{ - long fpos; - long slen; - - switch(segment) { - case RDOFF_HEADER: - fpos = f->header_ofs; - slen = f->header_len; - f->header_loc = (char *)buffer; - f->header_fp = 0; - break; - case RDOFF_CODE: - fpos = f->code_ofs; - slen = f->code_len; - break; - case RDOFF_DATA: - fpos = f->data_ofs; - slen = f->data_len; - break; - default: - fpos = 0; - slen = 0; - } - - if (fseek(f->fp,fpos,SEEK_SET)) - return rdf_errno = 4; - - if (fread(buffer,1,slen,f->fp) != slen) - return rdf_errno = 3; - - return 0; -} - -/* Macros for reading integers from header in memory */ - -#define RI8(v) v = f->header_loc[f->header_fp++] -#define RI16(v) { v = (f->header_loc[f->header_fp] + \ - (f->header_loc[f->header_fp+1] << 8)); \ - f->header_fp += 2; } - -#define RI32(v) { v = (f->header_loc[f->header_fp] + \ - (f->header_loc[f->header_fp+1] << 8) + \ - (f->header_loc[f->header_fp+2] << 16) + \ - (f->header_loc[f->header_fp+3] << 24)); \ - f->header_fp += 4; } - -#define RS(str,max) { for(i=0;i<max;i++){\ - RI8(str[i]); if (!str[i]) break;} str[i]=0; } - -rdfheaderrec *rdfgetheaderrec(rdffile *f) -{ - static rdfheaderrec r; - int i; - - if (!f->header_loc) { - rdf_errno = 5; - return NULL; - } - - if (f->header_fp >= f->header_len) return 0; - - RI8(r.type); - switch(r.type) { - case 1: /* Relocation record */ - RI8(r.r.segment); - RI32(r.r.offset); - RI8(r.r.length); - RI16(r.r.refseg); - break; - - case 2: /* Imported symbol record */ - RI16(r.i.segment); - RS(r.i.label,32); - break; - - case 3: /* Exported symbol record */ - RI8(r.e.segment); - RI32(r.e.offset); - RS(r.e.label,32); - break; - - case 4: /* DLL record */ - RS(r.d.libname,127); - break; - - case 5: /* BSS reservation record */ - RI32(r.b.amount); - break; - - default: - rdf_errno = 2; /* invalid file */ - return NULL; - } - return &r; -} - -void rdfheaderrewind(rdffile *f) -{ - f->header_fp = 0; -} - - -rdf_headerbuf * rdfnewheader(void) -{ - return newmembuf(); -} - -int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r) -{ - switch (r->type) - { - case 1: - membufwrite(h,&r->type,1); - membufwrite(h,&r->r.segment,1); - membufwrite(h,&r->r.offset,-4); - membufwrite(h,&r->r.length,1); - membufwrite(h,&r->r.refseg,-2); /* 9 bytes written */ - break; - - case 2: /* import */ - membufwrite(h,&r->type,1); - membufwrite(h,&r->i.segment,-2); - membufwrite(h,&r->i.label,strlen(r->i.label) + 1); - break ; - - case 3: /* export */ - membufwrite(h,&r->type,1); - membufwrite(h,&r->e.segment,1); - membufwrite(h,&r->e.offset,-4); - membufwrite(h,&r->e.label,strlen(r->e.label) + 1); - break ; - - case 4: /* DLL */ - membufwrite(h,&r->type,1); - membufwrite(h,&r->d.libname,strlen(r->d.libname) + 1); - break ; - - case 5: /* BSS */ - membufwrite(h,&r->type,1); - membufwrite(h,&r->b.amount,-4); - break ; - - default: - return (rdf_errno = 2); - } - return 0; -} - -int rdfwriteheader(FILE * fp, rdf_headerbuf * h) -{ - long l; - - fwrite (RDOFFId, 1, strlen(RDOFFId), fp) ; - - l = translatelong ( membuflength (h) ); - fwrite (&l, 4, 1, fp); - - membufdump(h, fp); - - return 0; /* no error handling in here... CHANGE THIS! */ -} - -void rdfdoneheader(rdf_headerbuf * h) -{ - freemembuf(h); -} diff --git a/rdoff/v1/rdoff.h b/rdoff/v1/rdoff.h deleted file mode 100644 index 0f74b80..0000000 --- a/rdoff/v1/rdoff.h +++ /dev/null @@ -1,118 +0,0 @@ -/* rdoff.h RDOFF Object File manipulation routines header file - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - */ - -#ifndef _RDOFF_H -#define _RDOFF_H "RDOFF1 support routines v0.1" - -typedef short int16; /* not sure if this will be required to be altered - at all... best to typedef it just in case */ - -/* the records that can be found in the RDOFF header */ - -struct RelocRec { - char type; /* must be 1 */ - char segment; /* only 0 for code, or 1 for data supported, - but add 64 for relative refs (ie do not require - reloc @ loadtime, only linkage) */ - long offset; /* from start of segment in which reference is loc'd */ - char length; /* 1 2 or 4 bytes */ - int16 refseg; /* segment to which reference refers to */ -}; - -struct ImportRec { - char type; /* must be 2 */ - int16 segment; /* segment number allocated to the label for reloc - records - label is assumed to be at offset zero - in this segment, so linker must fix up with offset - of segment and of offset within segment */ - char label[33]; /* zero terminated... should be written to file until - the zero, but not after it - max len = 32 chars */ -}; - -struct ExportRec { - char type; /* must be 3 */ - char segment; /* segment referred to (0/1) */ - long offset; /* offset within segment */ - char label[33]; /* zero terminated as above. max len = 32 chars */ -}; - -struct DLLRec { - char type; /* must be 4 */ - char libname[128]; /* name of library to link with at load time */ -}; - -struct BSSRec { - char type; /* must be 5 */ - long amount; /* number of bytes BSS to reserve */ -}; - -typedef union RDFHeaderRec { - char type; /* invariant throughout all below */ - struct RelocRec r; /* type == 1 */ - struct ImportRec i; /* type == 2 */ - struct ExportRec e; /* type == 3 */ - struct DLLRec d; /* type == 4 */ - struct BSSRec b; /* type == 5 */ -} rdfheaderrec; - -typedef struct RDFFileInfo { - FILE *fp; /* file descriptor; must be open to use this struct */ - int rdoff_ver; /* should be 1; any higher => not guaranteed to work */ - long header_len; - long code_len; - long data_len; - long header_ofs; - long code_ofs; - long data_ofs; - char *header_loc; /* keep location of header */ - long header_fp; /* current location within header for reading */ - char *name; /* name of module in libraries */ - int *refcount; /* pointer to reference count on file, or NULL */ -} rdffile; - -#define BUF_BLOCK_LEN 4088 /* selected to match page size (4096) - * on 80x86 machines for efficiency */ -typedef struct memorybuffer { - int length; - char buffer[BUF_BLOCK_LEN]; - struct memorybuffer *next; -} memorybuffer; - -typedef memorybuffer rdf_headerbuf; - -/* segments used by RDOFF, understood by rdoffloadseg */ -#define RDOFF_CODE 0 -#define RDOFF_DATA 1 -#define RDOFF_HEADER -1 -/* mask for 'segment' in relocation records to find if relative relocation */ -#define RDOFF_RELATIVEMASK 64 -/* mask to find actual segment value in relocation records */ -#define RDOFF_SEGMENTMASK 63 - -extern int rdf_errno; - -/* RDOFF file manipulation functions */ -int rdfopen(rdffile *f,const char *name); -int rdfopenhere(rdffile *f, FILE *fp, int *refcount, char *name); -int rdfclose(rdffile *f); -int rdfloadseg(rdffile *f,int segment,void *buffer); -rdfheaderrec *rdfgetheaderrec(rdffile *f); /* returns static storage */ -void rdfheaderrewind(rdffile *f); /* back to start of header */ -void rdfperror(const char *app,const char *name); - -/* functions to write a new RDOFF header to a file - - use rdfnewheader to allocate a header, rdfaddheader to add records to it, - rdfwriteheader to write 'RDOFF1', length of header, and the header itself - to a file, and then rdfdoneheader to dispose of the header */ - -rdf_headerbuf *rdfnewheader(void); -int rdfaddheader(rdf_headerbuf *h,rdfheaderrec *r); -int rdfwriteheader(FILE *fp,rdf_headerbuf *h); -void rdfdoneheader(rdf_headerbuf *h); - -#endif /* _RDOFF_H */ diff --git a/rdoff/v1/rdoff.txt b/rdoff/v1/rdoff.txt deleted file mode 100644 index 7ee86d6..0000000 --- a/rdoff/v1/rdoff.txt +++ /dev/null @@ -1,114 +0,0 @@ -The RDOFF version 1.1 Object File Format -======================================== - -I seem to keep writing this document... I don't know what keeps -happening to it. Anyway, this one will hopefully stay around for a -while. - -RDOFF is a relocatable object file format whose design goals were -mainly to keep it simple, so that an RDOFF object can be loaded and -executed by a very small piece of code (primarily so that it can be -used by the microkernel of an operating system to store system -modules, which can then go on to load and execute more complex object -files, eg ELF, if so desired), yet still be able to be cope with -everything required by the operating system; linkage of multiple -modules together (possibly with automatic loading of new libraries -that are referred to by the object) at load time, allowing static or -dynamic linking as required by the application. - -The overall format of the file is summarised in this table: - -Length (bytes) Description - 6 Contains the string 'RDOFF1' (little-endian targets), - or 'RDOFF' followed by the single byte 0x01 - (big-endian targets). - 4 Length of the header section - ? Header section (see above for length) - 4 Length of code section (.text) - ? Code section - 4 Length of data section (.data) - ? Data section - -Segments are referred to as numbers. Imported labels are implicitly -at offset zero from a segment; each is assigned a segment number when -it is imported. Segments in the object file itself are numbered: - 0 - text segemnt - 1 - data segment - 2 - bss segment - -The header consists of a sequence of records, each of which is -preceded by a byte to represent its type. - -These records are one of the following types: - -1: Relocation Record --------------------- - - This record points to an address that will need either - relocation or linkage to an external segment when the object - is loaded or linked. - - Length Description - 1 Type identifier (must be 1) - 1 Segment number (0 or 1) plus 64 if the reference is - relative (and thus does not require relocation with - the base of the code, only by the difference between - the start of this segment, and the segment referred to - (see below) - 4 Offset from start of segment of item requiring reloc. - 1 Length of item (1, 2, or 4 bytes...) - 2 Segment number to which reference is made. - -2: Import Symbol Record ------------------------ - - This record defines a segment to start at the location of a - named symbol; this symbol may need to be fetched from an - external library. - - Length Description - 1 Type identifier (must be 2) - 2 Segment number to allocate - ? String containing label (null terminated, max length = - 32 chars) - -3: Export Symbol Record ------------------------ - - This record defines a symbol, to which external modules can - link using the above record type. - - Length Description - 1 Type identifier (must be 3) - 1 Segment containing symbol (0,1 or 2) - 4 Offset of symbol within segment - ? String containing label (null terminated, max length = - 32 chars) - -4: Import Library Record ------------------------- - - This record tells the loader that an extra library should be - loaded and linked to the module at either load- or run-time - (load time is easier, run-time is good, though...) - - Length Description - 1 Type identifier (must be 4) - ? Name of library (null terminated string, max len = 128) - -5: Reserve BSS Bytes --------------------- - - This record tells the loader how much memory to reserve after - the executable code loaded from the object file for the BSS - segment (referred to as segment number 2). - A loader can safely assume that there will only be one of - these records per module, but the linker probably cannot... - NASM will only output one, but other utilities may be written - that do, and future versions of NASM may output more than one. - - Length Description - 1 Type identifier (must be 5) - 4 Number of bytes to reserve - - diff --git a/rdoff/v1/rdx.c b/rdoff/v1/rdx.c deleted file mode 100644 index 28ffc42..0000000 --- a/rdoff/v1/rdx.c +++ /dev/null @@ -1,61 +0,0 @@ -/* rdx.c RDOFF Object File loader program - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - */ - -/* note: most of the actual work of this program is done by the modules - "rdfload.c", which loads and relocates the object file, and by "rdoff.c", - which contains general purpose routines to manipulate RDOFF object - files. You can use these files in your own program to load RDOFF objects - and execute the code in them in a similar way to what is shown here. */ - -#include <stdio.h> -#include <stdlib.h> - -#include "rdfload.h" -#include "rdoff.h" -#include "symtab.h" - -typedef int (*main_fn) (int,char**); /* Main function prototype */ - -int main(int argc, char **argv) -{ - rdfmodule * m; - main_fn code; - symtabEnt * s; - - if (argc < 2) - { - puts("usage: rdf <rdoff-executable> [params]\n"); - exit(255); - } - - m = rdfload(argv[1]); - - if (! m) - { - rdfperror("rdf",argv[1]); - exit(255); - } - - rdf_relocate(m); /* in this instance, the default relocation - values will work fine, but they may need changing - in other cases... */ - - s = symtabFind(m->symtab, "_main"); - if (! s) - { - fprintf(stderr,"rdx: could not find symbol '_main' in '%s'\n",argv[1]); - exit(255); - } - - code = (main_fn) s->offset; - - argv++, argc--; /* remove 'rdx' from command line */ - - return code(argc,argv); /* execute */ -} - diff --git a/rdoff/v1/symtab.c b/rdoff/v1/symtab.c deleted file mode 100644 index 3fc363e..0000000 --- a/rdoff/v1/symtab.c +++ /dev/null @@ -1,80 +0,0 @@ -/* symtab.c Routines to maintain and manipulate a symbol table - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - */ -#include <stdio.h> -#include <stdlib.h> - -#include "symtab.h" - -/* TODO: Implement a hash table, not this stupid implementation which - is too slow to be of practical use */ - -/* Private data types */ - -typedef struct tagSymtab { - symtabEnt ent; - struct tagSymtab * next; -} symtabList; - -typedef symtabList * _symtab; - -void *symtabNew(void) -{ - void *p = malloc(sizeof(_symtab)); - if (p == NULL) { - fprintf(stderr,"symtab: out of memory\n"); - exit(3); - } - *(_symtab *)p = NULL; - - return p; -} - -void symtabDone(void *symtab) -{ - /* DO SOMETHING HERE! */ -} - -void symtabInsert(void *symtab,symtabEnt *ent) -{ - symtabList *l = malloc(sizeof(symtabList)); - - if (l == NULL) { - fprintf(stderr,"symtab: out of memory\n"); - exit(3); - } - - l->ent = *ent; - l->next = *(_symtab *)symtab; - *(_symtab *)symtab = l; -} - -symtabEnt *symtabFind(void *symtab,char *name) -{ - symtabList *l = *(_symtab *)symtab; - - while (l) { - if (!strcmp(l->ent.name,name)) { - return &(l->ent); - } - l = l->next; - } - return NULL; -} - -void symtabDump(void *symtab,FILE *of) -{ - symtabList *l = *(_symtab *)symtab; - - while(l) { - fprintf(of,"%32s %s:%08lx (%ld)\n",l->ent.name, - l->ent.segment ? "data" : "code" , - l->ent.offset, l->ent.flags); - l = l->next; - } -} - diff --git a/rdoff/v1/symtab.h b/rdoff/v1/symtab.h deleted file mode 100644 index 5780d44..0000000 --- a/rdoff/v1/symtab.h +++ /dev/null @@ -1,22 +0,0 @@ -/* symtab.h Header file for symbol table manipulation routines - * - * The Netwide Assembler is copyright (C) 1996 Simon Tatham and - * Julian Hall. All rights reserved. The software is - * redistributable under the licence given in the file "Licence" - * distributed in the NASM archive. - */ - -typedef struct { - char *name; - long segment; - long offset; - long flags; -} symtabEnt; - -void *symtabNew(void); -void symtabDone(void *symtab); -void symtabInsert(void *symtab,symtabEnt *ent); -symtabEnt *symtabFind(void *symtab,char *name); -void symtabDump(void *symtab,FILE *of); - - |