diff options
author | H. Peter Anvin <hpa@zytor.com> | 2005-01-15 22:15:51 +0000 |
---|---|---|
committer | H. Peter Anvin <hpa@zytor.com> | 2005-01-15 22:15:51 +0000 |
commit | e2c80181b6a6338f0381fc9c44fae32d8b8a20fc (patch) | |
tree | f9919ba3c0489d886c9a79e73257ef6a4584aafc /rdoff | |
parent | 5180bc8a598aa5bff7bfb3726771e5bf348e61a2 (diff) | |
download | nasm-e2c80181b6a6338f0381fc9c44fae32d8b8a20fc.tar.gz nasm-e2c80181b6a6338f0381fc9c44fae32d8b8a20fc.tar.bz2 nasm-e2c80181b6a6338f0381fc9c44fae32d8b8a20fc.zip |
Apply Nindent to all .c and .h files
Diffstat (limited to 'rdoff')
-rw-r--r-- | rdoff/collectn.c | 20 | ||||
-rw-r--r-- | rdoff/collectn.h | 10 | ||||
-rw-r--r-- | rdoff/hash.c | 146 | ||||
-rw-r--r-- | rdoff/hash.h | 3 | ||||
-rw-r--r-- | rdoff/ldrdf.c | 1705 | ||||
-rw-r--r-- | rdoff/ldsegs.h | 15 | ||||
-rw-r--r-- | rdoff/rdf2bin.c | 143 | ||||
-rw-r--r-- | rdoff/rdf2ihx.c | 320 | ||||
-rw-r--r-- | rdoff/rdfdump.c | 459 | ||||
-rw-r--r-- | rdoff/rdflib.c | 611 | ||||
-rw-r--r-- | rdoff/rdfload.c | 221 | ||||
-rw-r--r-- | rdoff/rdfload.h | 14 | ||||
-rw-r--r-- | rdoff/rdlar.c | 542 | ||||
-rw-r--r-- | rdoff/rdlar.h | 20 | ||||
-rw-r--r-- | rdoff/rdlib.c | 375 | ||||
-rw-r--r-- | rdoff/rdlib.h | 19 | ||||
-rw-r--r-- | rdoff/rdoff.c | 517 | ||||
-rw-r--r-- | rdoff/rdoff.h | 183 | ||||
-rw-r--r-- | rdoff/rdx.c | 35 | ||||
-rw-r--r-- | rdoff/segtab.c | 138 | ||||
-rw-r--r-- | rdoff/segtab.h | 4 | ||||
-rw-r--r-- | rdoff/symtab.c | 149 | ||||
-rw-r--r-- | rdoff/symtab.h | 16 |
23 files changed, 2840 insertions, 2825 deletions
diff --git a/rdoff/collectn.c b/rdoff/collectn.c index 71dc10e..ad43d85 100644 --- a/rdoff/collectn.c +++ b/rdoff/collectn.c @@ -12,19 +12,19 @@ void collection_init(Collection * c) int i; for (i = 0; i < 32; i++) - c->p[i] = NULL; + 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; + index -= 32; + if (c->next == NULL) { + c->next = malloc(sizeof(Collection)); + collection_init(c->next); + } + c = c->next; } return &(c->p[index]); } @@ -34,11 +34,11 @@ void collection_reset(Collection * c) int i; if (c->next) { - collection_reset(c->next); - free(c->next); + collection_reset(c->next); + free(c->next); } c->next = NULL; for (i = 0; i < 32; i++) - c->p[i] = NULL; + c->p[i] = NULL; } diff --git a/rdoff/collectn.h b/rdoff/collectn.h index dc4f7cc..52647d1 100644 --- a/rdoff/collectn.h +++ b/rdoff/collectn.h @@ -10,13 +10,13 @@ #define _COLLECTN_H typedef struct tagCollection { - void *p[32]; /* array of pointers to objects */ - + 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); +void collection_init(Collection * c); +void **colln(Collection * c, int index); +void collection_reset(Collection * c); #endif diff --git a/rdoff/hash.c b/rdoff/hash.c index 7f28f12..933754a 100644 --- a/rdoff/hash.c +++ b/rdoff/hash.c @@ -13,82 +13,82 @@ typedef unsigned int crc32; const crc32 consttab[] = { - 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, - 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, - 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, - 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, - 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, - 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, - 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, - 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, - 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, - 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, - 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, - 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, - 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, - 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, - 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, - 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, - 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, - 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, - 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, - 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, - 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, - 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, - 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, - 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, - 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, - 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, - 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, - 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, - 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, - 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, - 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, - 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, - 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, - 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, - 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, - 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, - 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, - 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, - 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, - 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, - 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, - 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, - 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, - 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, - 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, - 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, - 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, - 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, - 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, - 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, - 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, - 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, - 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, - 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, - 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, - 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, - 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, - 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, - 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, - 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, - 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, - 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, - 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, - 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D }; + 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, + 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, + 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, + 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, + 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, + 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, + 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, + 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, + 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, + 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, + 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, + 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, + 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, + 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, + 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, + 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, + 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, + 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, + 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, + 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, + 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, + 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, + 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, + 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, + 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, + 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, + 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, + 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, + 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, + 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, + 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, + 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, + 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, + 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, + 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, + 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, + 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, + 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, + 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, + 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, + 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, + 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, + 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, + 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, + 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, + 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, + 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, + 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, + 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, + 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, + 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, + 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, + 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, + 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, + 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, + 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, + 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, + 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, + 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, + 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, + 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, + 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, + 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, + 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D +}; -unsigned -hash (const char *name) +unsigned hash(const char *name) { - register const char *n; - register crc32 hashval = 0xffffffff; + register const char *n; + register crc32 hashval = 0xffffffff; - for (n=name; *n ; n++) - hashval = (hashval>>8) ^ consttab[(hashval^*n) & 0xff]; - - hashval ^= 0xffffffff; + for (n = name; *n; n++) + hashval = (hashval >> 8) ^ consttab[(hashval ^ *n) & 0xff]; - return (hashval); + hashval ^= 0xffffffff; + + return (hashval); } diff --git a/rdoff/hash.h b/rdoff/hash.h index 2ccf9b9..cda4f61 100644 --- a/rdoff/hash.h +++ b/rdoff/hash.h @@ -8,5 +8,4 @@ * distributed in the NASM archive. */ -unsigned hash (const char* name); - +unsigned hash(const char *name); diff --git a/rdoff/ldrdf.c b/rdoff/ldrdf.c index 22e077e..977aeb8 100644 --- a/rdoff/ldrdf.c +++ b/rdoff/ldrdf.c @@ -45,20 +45,19 @@ */ struct segment_infonode { - int dest_seg; /* output segment to be placed into, -1 to - skip linking this segment */ - long reloc; /* segment's relocation factor */ + int dest_seg; /* output segment to be placed into, -1 to + skip linking this segment */ + long reloc; /* segment's relocation factor */ }; - struct modulenode { - rdffile f; /* the RDOFF file structure */ - struct segment_infonode seginfo[RDF_MAXSEGS]; /* what are we doing - with each segment? */ - void * header; - char * name; - struct modulenode * next; - long bss_reloc; + rdffile f; /* the RDOFF file structure */ + struct segment_infonode seginfo[RDF_MAXSEGS]; /* what are we doing + with each segment? */ + void *header; + char *name; + struct modulenode *next; + long bss_reloc; }; #include "ldsegs.h" @@ -70,64 +69,63 @@ struct modulenode { * Function prototypes of private utility functions */ -void processmodule(const char * filename, struct modulenode * mod); -int allocnewseg(uint16 type,uint16 reserved); -int findsegment(uint16 type,uint16 reserved); -void symtab_add(const char * symbol, int segment, long offset); -int symtab_get(const char * symbol, int * segment, long * offset); +void processmodule(const char *filename, struct modulenode *mod); +int allocnewseg(uint16 type, uint16 reserved); +int findsegment(uint16 type, uint16 reserved); +void symtab_add(const char *symbol, int segment, long offset); +int symtab_get(const char *symbol, int *segment, long *offset); /* ========================================================================= * Global data structures. */ /* a linked list of modules that will be included in the output */ -struct modulenode * modules = NULL; -struct modulenode * lastmodule = NULL; +struct modulenode *modules = NULL; +struct modulenode *lastmodule = NULL; /* a linked list of libraries to be searched for unresolved imported symbols */ -struct librarynode * libraries = NULL; -struct librarynode * lastlib = NULL; +struct librarynode *libraries = NULL; +struct librarynode *lastlib = NULL; /* the symbol table */ -void * symtab = NULL; +void *symtab = NULL; /* objects search path */ -char * objpath = NULL; +char *objpath = NULL; /* libraries search path */ -char * libpath = NULL; +char *libpath = NULL; /* file to embed as a generic record */ -char * generic_rec_file = NULL; +char *generic_rec_file = NULL; /* error file */ -static FILE * error_file; +static FILE *error_file; /* the header of the output file, built up stage by stage */ -rdf_headerbuf * newheader = NULL; +rdf_headerbuf *newheader = NULL; /* The current state of segment allocation, including information about * which output segment numbers have been allocated, and their types and * amount of data which has already been allocated inside them. */ -struct SegmentHeaderRec outputseg[RDF_MAXSEGS]; -int nsegs = 0; -long bss_length; +struct SegmentHeaderRec outputseg[RDF_MAXSEGS]; +int nsegs = 0; +long bss_length; /* global options which affect how the program behaves */ struct ldrdfoptions { - int verbose; - int align; - int dynalink; - int strip; - int respfile; - int stderr_redir; - int objpath; - int libpath; + int verbose; + int align; + int dynalink; + int strip; + int respfile; + int stderr_redir; + int objpath; + int libpath; } options; -int errorcount = 0; /* determines main program exit status */ - +int errorcount = 0; /* determines main program exit status */ /* ========================================================================= * Utility functions @@ -149,14 +147,13 @@ void initsegments() outputseg[1].number = 1; outputseg[1].reserved = 0; outputseg[1].length = 0; - outputseg[2].type = 0xFFFF; /* reserved segment type */ + outputseg[2].type = 0xFFFF; /* reserved segment type */ outputseg[2].number = 2; outputseg[2].reserved = 0; outputseg[2].length = 0; bss_length = 0; } - /* * loadmodule * @@ -164,29 +161,29 @@ void initsegments() * each segment it contains (including determining destination segments and * relocation factors for segments that are kept). */ -void loadmodule(const char * filename) +void loadmodule(const char *filename) { if (options.verbose) - printf("loading `%s'\n", filename); + printf("loading `%s'\n", filename); /* allocate a new module entry on the end of the modules list */ if (!modules) { - modules = malloc (sizeof(*modules)); - lastmodule = modules; + modules = malloc(sizeof(*modules)); + lastmodule = modules; } else { - lastmodule->next = malloc (sizeof(*modules)); - lastmodule = lastmodule->next; + lastmodule->next = malloc(sizeof(*modules)); + lastmodule = lastmodule->next; } if (!lastmodule) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); } /* open the file using 'rdfopen', which returns nonzero on error */ if (rdfopen(&lastmodule->f, filename) != 0) { - rdfperror("ldrdf", filename); - exit(1); + rdfperror("ldrdf", filename); + exit(1); } /* @@ -201,7 +198,6 @@ void loadmodule(const char * filename) processmodule(filename, lastmodule); } - /* * processmodule() * @@ -211,82 +207,83 @@ void loadmodule(const char * filename) * (b) is fairly easy, because we're now keeping track of how big each * segment in our output file is... */ -void processmodule(const char * filename, struct modulenode * mod) +void processmodule(const char *filename, struct modulenode *mod) { struct segconfig sconf; - int seg, outseg; - void * header; - rdfheaderrec * hr; - long bssamount = 0; - int bss_was_referenced = 0; + int seg, outseg; + void *header; + rdfheaderrec *hr; + long bssamount = 0; + int bss_was_referenced = 0; for (seg = 0; seg < mod->f.nsegs; seg++) { - /* - * get the segment configuration for this type from the segment - * table. getsegconfig() is a macro, defined in ldsegs.h. - */ - getsegconfig(sconf, mod->f.seg[seg].type); - - if (options.verbose > 1) { - printf ("%s %04x [%04x:%10s] ", filename, mod->f.seg[seg].number, - mod->f.seg[seg].type, sconf.typedesc); - } - /* - * sconf->dowhat tells us what to do with a segment of this type. - */ - switch (sconf.dowhat) { - case SEG_IGNORE: - /* - * Set destination segment to -1, to indicate that this segment - * should be ignored for the purpose of output, ie it is left - * out of the linked executable. - */ - mod->seginfo[seg].dest_seg = -1; - if (options.verbose > 1) printf("IGNORED\n"); - break; - - case SEG_NEWSEG: - /* - * The configuration tells us to create a new segment for - * each occurrence of this segment type. - */ - outseg = allocnewseg(sconf.mergetype, - mod->f.seg[seg].reserved); - mod->seginfo[seg].dest_seg = outseg; - mod->seginfo[seg].reloc = 0; - outputseg[outseg].length = mod->f.seg[seg].length; - if (options.verbose > 1) - printf ("=> %04x:%08lx (+%04lx)\n", outseg, - mod->seginfo[seg].reloc, - mod->f.seg[seg].length); - break; - - case SEG_MERGE: - /* - * The configuration tells us to merge the segment with - * a previously existing segment of type 'sconf.mergetype', - * if one exists. Otherwise a new segment is created. - * This is handled transparently by 'findsegment()'. - */ - outseg = findsegment(sconf.mergetype, - mod->f.seg[seg].reserved); - mod->seginfo[seg].dest_seg = outseg; - - /* - * We need to add alignment to these segments. - */ - if (outputseg[outseg].length % options.align != 0) - outputseg[outseg].length += - options.align - (outputseg[outseg].length % options.align); - - mod->seginfo[seg].reloc = outputseg[outseg].length; - outputseg[outseg].length += mod->f.seg[seg].length; - - if (options.verbose > 1) - printf ("=> %04x:%08lx (+%04lx)\n", outseg, - mod->seginfo[seg].reloc, - mod->f.seg[seg].length); - } + /* + * get the segment configuration for this type from the segment + * table. getsegconfig() is a macro, defined in ldsegs.h. + */ + getsegconfig(sconf, mod->f.seg[seg].type); + + if (options.verbose > 1) { + printf("%s %04x [%04x:%10s] ", filename, + mod->f.seg[seg].number, mod->f.seg[seg].type, + sconf.typedesc); + } + /* + * sconf->dowhat tells us what to do with a segment of this type. + */ + switch (sconf.dowhat) { + case SEG_IGNORE: + /* + * Set destination segment to -1, to indicate that this segment + * should be ignored for the purpose of output, ie it is left + * out of the linked executable. + */ + mod->seginfo[seg].dest_seg = -1; + if (options.verbose > 1) + printf("IGNORED\n"); + break; + + case SEG_NEWSEG: + /* + * The configuration tells us to create a new segment for + * each occurrence of this segment type. + */ + outseg = allocnewseg(sconf.mergetype, + mod->f.seg[seg].reserved); + mod->seginfo[seg].dest_seg = outseg; + mod->seginfo[seg].reloc = 0; + outputseg[outseg].length = mod->f.seg[seg].length; + if (options.verbose > 1) + printf("=> %04x:%08lx (+%04lx)\n", outseg, + mod->seginfo[seg].reloc, mod->f.seg[seg].length); + break; + + case SEG_MERGE: + /* + * The configuration tells us to merge the segment with + * a previously existing segment of type 'sconf.mergetype', + * if one exists. Otherwise a new segment is created. + * This is handled transparently by 'findsegment()'. + */ + outseg = findsegment(sconf.mergetype, + mod->f.seg[seg].reserved); + mod->seginfo[seg].dest_seg = outseg; + + /* + * We need to add alignment to these segments. + */ + if (outputseg[outseg].length % options.align != 0) + outputseg[outseg].length += + options.align - + (outputseg[outseg].length % options.align); + + mod->seginfo[seg].reloc = outputseg[outseg].length; + outputseg[outseg].length += mod->f.seg[seg].length; + + if (options.verbose > 1) + printf("=> %04x:%08lx (+%04lx)\n", outseg, + mod->seginfo[seg].reloc, mod->f.seg[seg].length); + } } @@ -296,89 +293,92 @@ void processmodule(const char * filename, struct modulenode * mod) */ header = malloc(mod->f.header_len); if (!header) { - fprintf(stderr, "ldrdf: not enough memory\n"); - exit(1); + fprintf(stderr, "ldrdf: not enough memory\n"); + exit(1); } if (rdfloadseg(&mod->f, RDOFF_HEADER, header)) { - rdfperror("ldrdf", filename); - exit(1); + rdfperror("ldrdf", filename); + exit(1); } - while ((hr = rdfgetheaderrec (&mod->f))) { - switch(hr->type) { - case RDFREC_IMPORT: /* imported symbol */ - case RDFREC_FARIMPORT: - /* Define with seg = -1 */ - symtab_add(hr->i.label, -1, 0); - break; - - case RDFREC_GLOBAL: { /* exported symbol */ - int destseg; - long destreloc; - - if (hr->e.segment == 2) { - bss_was_referenced = 1; - destreloc = bss_length; - if (destreloc % options.align != 0) - destreloc += options.align - (destreloc % options.align); - destseg = 2; + while ((hr = rdfgetheaderrec(&mod->f))) { + switch (hr->type) { + case RDFREC_IMPORT: /* imported symbol */ + case RDFREC_FARIMPORT: + /* Define with seg = -1 */ + symtab_add(hr->i.label, -1, 0); + break; + + case RDFREC_GLOBAL:{ /* exported symbol */ + int destseg; + long destreloc; + + if (hr->e.segment == 2) { + bss_was_referenced = 1; + destreloc = bss_length; + if (destreloc % options.align != 0) + destreloc += + options.align - (destreloc % options.align); + destseg = 2; } else { - if ((destseg = mod->seginfo[(int)hr->e.segment].dest_seg) == -1) - continue; - destreloc = mod->seginfo[(int)hr->e.segment].reloc; + if ((destseg = + mod->seginfo[(int)hr->e.segment].dest_seg) == -1) + continue; + destreloc = mod->seginfo[(int)hr->e.segment].reloc; } - symtab_add(hr->e.label, destseg, destreloc + hr->e.offset); - break; - } - - case RDFREC_BSS: /* BSS reservation */ - /* - * first, amalgamate all BSS reservations in this module - * into one, because we allow this in the output format. - */ - bssamount += hr->b.amount; - break; - - case RDFREC_COMMON: { /* Common variable */ - symtabEnt *ste = symtabFind(symtab, hr->c.label); - - /* Is the symbol already in the table? */ - if (ste) break; - - /* Align the variable */ - if (bss_length % hr->c.align != 0) - bss_length += hr->c.align - (bss_length % hr->c.align); - if (options.verbose > 1) { - printf ("%s %04x common '%s' => 0002:%08lx (+%04lx)\n", - filename, hr->c.segment, hr->c.label, bss_length, hr->c.size); - } - - symtab_add(hr->c.label, 2, bss_length); - mod->bss_reloc = bss_length; - bss_length += hr->c.size; - break; - } - } + symtab_add(hr->e.label, destseg, destreloc + hr->e.offset); + break; + } + + case RDFREC_BSS: /* BSS reservation */ + /* + * first, amalgamate all BSS reservations in this module + * into one, because we allow this in the output format. + */ + bssamount += hr->b.amount; + break; + + case RDFREC_COMMON:{ /* Common variable */ + symtabEnt *ste = symtabFind(symtab, hr->c.label); + + /* Is the symbol already in the table? */ + if (ste) + break; + + /* Align the variable */ + if (bss_length % hr->c.align != 0) + bss_length += hr->c.align - (bss_length % hr->c.align); + if (options.verbose > 1) { + printf("%s %04x common '%s' => 0002:%08lx (+%04lx)\n", + filename, hr->c.segment, hr->c.label, + bss_length, hr->c.size); + } + + symtab_add(hr->c.label, 2, bss_length); + mod->bss_reloc = bss_length; + bss_length += hr->c.size; + break; + } + } } - + if (bssamount != 0 || bss_was_referenced) { - /* - * handle the BSS segment - first pad the existing bss length - * to the correct alignment, then store the length in bss_reloc - * for this module. Then add this module's BSS length onto - * bss_length. - */ - if (bss_length % options.align != 0) - bss_length += options.align - (bss_length % options.align); - - mod->bss_reloc = bss_length; - if (options.verbose > 1) { - printf ("%s 0002 [ BSS] => 0002:%08lx (+%04lx)\n", - filename, bss_length, bssamount); - } - bss_length += bssamount; + /* + * handle the BSS segment - first pad the existing bss length + * to the correct alignment, then store the length in bss_reloc + * for this module. Then add this module's BSS length onto + * bss_length. + */ + if (bss_length % options.align != 0) + bss_length += options.align - (bss_length % options.align); + + mod->bss_reloc = bss_length; + if (options.verbose > 1) { + printf("%s 0002 [ BSS] => 0002:%08lx (+%04lx)\n", + filename, bss_length, bssamount); + } + bss_length += bssamount; } - #ifdef STINGY_MEMORY /* * we free the header buffer here, to save memory later. @@ -392,7 +392,6 @@ void processmodule(const char * filename, struct modulenode * mod) } - /* * Return 1 if a given module is in the list, 0 otherwise. */ @@ -400,14 +399,14 @@ int lookformodule(const char *name) { struct modulenode *curr = modules; - while(curr) { - if (!strcmp(name, curr->name)) return 1; - curr = curr->next; + while (curr) { + if (!strcmp(name, curr->name)) + return 1; + curr = curr->next; } return 0; } - /* * allocnewseg() * findsegment() @@ -418,7 +417,7 @@ int lookformodule(const char *name) * a segment of the type requested, and if one isn't found allocates a * new one. */ -int allocnewseg(uint16 type,uint16 reserved) +int allocnewseg(uint16 type, uint16 reserved) { outputseg[nsegs].type = type; outputseg[nsegs].number = nsegs; @@ -430,17 +429,17 @@ int allocnewseg(uint16 type,uint16 reserved) return nsegs++; } -int findsegment(uint16 type,uint16 reserved) +int findsegment(uint16 type, uint16 reserved) { int i; for (i = 0; i < nsegs; i++) - if (outputseg[i].type == type) return i; + if (outputseg[i].type == type) + return i; - return allocnewseg(type,reserved); + return allocnewseg(type, reserved); } - /* * symtab_add() * @@ -458,41 +457,43 @@ int findsegment(uint16 type,uint16 reserved) * routine won't change a previously existing symbol. It will change * to segment = -2 only if the segment was previously < 0. */ -void symtab_add(const char * symbol, int segment, long offset) +void symtab_add(const char *symbol, int segment, long offset) { - symtabEnt * ste; + symtabEnt *ste; ste = symtabFind(symtab, symbol); if (ste) { - if (ste->segment >= 0) { - /* - * symbol previously defined - */ - if (segment < 0) return; - fprintf (error_file, "warning: `%s' redefined\n", symbol); - return; - } - - /* - * somebody wanted the symbol, and put an undefined symbol - * marker into the table - */ - if (segment == -1) return; - /* - * we have more information now - update the symbol's entry - */ - ste->segment = segment; - ste->offset = offset; - ste->flags = 0; - return; + if (ste->segment >= 0) { + /* + * symbol previously defined + */ + if (segment < 0) + return; + fprintf(error_file, "warning: `%s' redefined\n", symbol); + return; + } + + /* + * somebody wanted the symbol, and put an undefined symbol + * marker into the table + */ + if (segment == -1) + return; + /* + * we have more information now - update the symbol's entry + */ + ste->segment = segment; + ste->offset = offset; + ste->flags = 0; + return; } /* * this is the first declaration of this symbol */ ste = malloc(sizeof(symtabEnt)); if (!ste) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); } ste->name = strdup(symbol); ste->segment = segment; @@ -508,57 +509,55 @@ void symtab_add(const char * symbol, int segment, long offset) * are assumed to have -1:0 associated. Returns 1 if the symbol was * successfully located. */ -int symtab_get(const char * symbol, int * segment, long * offset) +int symtab_get(const char *symbol, int *segment, long *offset) { - symtabEnt * ste = symtabFind(symtab, symbol); + symtabEnt *ste = symtabFind(symtab, symbol); if (!ste) { - *segment = -1; - *offset = 0; - return 0; + *segment = -1; + *offset = 0; + return 0; } else { - *segment = ste->segment; - *offset = ste->offset; - return 1; + *segment = ste->segment; + *offset = ste->offset; + return 1; } } - /* * add_library() * * checks that a library can be opened and is in the correct format, * then adds it to the linked list of libraries. */ -void add_library(const char * name) +void add_library(const char *name) { if (rdl_verify(name)) { - rdl_perror("ldrdf", name); - errorcount++; - return; + rdl_perror("ldrdf", name); + errorcount++; + return; } if (!libraries) { - lastlib = libraries = malloc(sizeof(*libraries)); - if (! libraries) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); - } + lastlib = libraries = malloc(sizeof(*libraries)); + if (!libraries) { + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); + } } else { - lastlib->next = malloc(sizeof(*libraries)); - if (!lastlib->next) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); - } - lastlib = lastlib->next; + lastlib->next = malloc(sizeof(*libraries)); + if (!lastlib->next) { + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); + } + lastlib = lastlib->next; } lastlib->next = NULL; if (rdl_open(lastlib, name)) { - rdl_perror("ldrdf", name); - errorcount++; - return; + rdl_perror("ldrdf", name); + errorcount++; + return; } } - /* * search_libraries() * @@ -571,102 +570,104 @@ void add_library(const char * name) */ int search_libraries() { - struct librarynode * cur; + struct librarynode *cur; rdffile f; - int i; - void * header; - int segment; - long offset; - int doneanything = 0, pass = 1, keepfile; - rdfheaderrec * hr; + int i; + void *header; + int segment; + long offset; + int doneanything = 0, pass = 1, keepfile; + rdfheaderrec *hr; cur = libraries; while (cur) { - if (options.verbose > 2) - printf("scanning library `%s', pass %d...\n", cur->name, pass); - - for (i = 0; rdl_openmodule(cur, i, &f) == 0; i++) { - if (pass == 2 && lookformodule(f.name)) continue; - - if (options.verbose > 3) - printf(" looking in module `%s'\n", f.name); - - header = malloc(f.header_len); - if (!header) { - fprintf(stderr, "ldrdf: not enough memory\n"); - exit(1); - } - if (rdfloadseg(&f, RDOFF_HEADER, header)) { - rdfperror("ldrdf", f.name); - errorcount++; - return 0; - } - - keepfile = 0; - - while ((hr = rdfgetheaderrec (&f))) { + if (options.verbose > 2) + printf("scanning library `%s', pass %d...\n", cur->name, pass); + + for (i = 0; rdl_openmodule(cur, i, &f) == 0; i++) { + if (pass == 2 && lookformodule(f.name)) + continue; + + if (options.verbose > 3) + printf(" looking in module `%s'\n", f.name); + + header = malloc(f.header_len); + if (!header) { + fprintf(stderr, "ldrdf: not enough memory\n"); + exit(1); + } + if (rdfloadseg(&f, RDOFF_HEADER, header)) { + rdfperror("ldrdf", f.name); + errorcount++; + return 0; + } + + keepfile = 0; + + while ((hr = rdfgetheaderrec(&f))) { /* We're only interested in exports, so skip others */ - if (hr->type != RDFREC_GLOBAL) continue; - - /* - * If the symbol is marked as SYM_GLOBAL, somebody will be - * definitely interested in it.. - */ - if ((hr->e.flags & SYM_GLOBAL) == 0) { - /* - * otherwise the symbol is just public. Find it in - * the symbol table. If the symbol isn't defined, we - * aren't interested, so go on to the next. - * If it is defined as anything but -1, we're also not - * interested. But if it is defined as -1, insert this - * module into the list of modules to use, and go - * immediately on to the next module... - */ - if (!symtab_get(hr->e.label, &segment, &offset) || segment != -1) - continue; - } - - doneanything = 1; - keepfile = 1; - - /* - * as there are undefined symbols, we can assume that - * there are modules on the module list by the time - * we get here. - */ - lastmodule->next = malloc(sizeof(*lastmodule->next)); - if (!lastmodule->next) { - fprintf(stderr, "ldrdf: not enough memory\n"); - exit(1); - } - lastmodule = lastmodule->next; - memcpy(&lastmodule->f, &f, sizeof(f)); - lastmodule->name = strdup(f.name); + if (hr->type != RDFREC_GLOBAL) + continue; + + /* + * If the symbol is marked as SYM_GLOBAL, somebody will be + * definitely interested in it.. + */ + if ((hr->e.flags & SYM_GLOBAL) == 0) { + /* + * otherwise the symbol is just public. Find it in + * the symbol table. If the symbol isn't defined, we + * aren't interested, so go on to the next. + * If it is defined as anything but -1, we're also not + * interested. But if it is defined as -1, insert this + * module into the list of modules to use, and go + * immediately on to the next module... + */ + if (!symtab_get(hr->e.label, &segment, &offset) + || segment != -1) + continue; + } + + doneanything = 1; + keepfile = 1; + + /* + * as there are undefined symbols, we can assume that + * there are modules on the module list by the time + * we get here. + */ + lastmodule->next = malloc(sizeof(*lastmodule->next)); + if (!lastmodule->next) { + fprintf(stderr, "ldrdf: not enough memory\n"); + exit(1); + } + lastmodule = lastmodule->next; + memcpy(&lastmodule->f, &f, sizeof(f)); + lastmodule->name = strdup(f.name); lastmodule->next = NULL; - processmodule(f.name, lastmodule); - break; - } - if (!keepfile) { - free(f.name); - f.name = NULL; - f.fp = NULL; + processmodule(f.name, lastmodule); + break; + } + if (!keepfile) { + free(f.name); + f.name = NULL; + f.fp = NULL; } - } - if (rdl_error != 0 && rdl_error != RDL_ENOTFOUND) - rdl_perror("ldrdf", cur->name); - - cur = cur->next; - if (cur == NULL && pass == 1) { - cur = libraries; - pass++; - } + } + if (rdl_error != 0 && rdl_error != RDL_ENOTFOUND) + rdl_perror("ldrdf", cur->name); + + cur = cur->next; + if (cur == NULL && pass == 1) { + cur = libraries; + pass++; + } } return doneanything; } - /* * write_output() * @@ -674,28 +675,28 @@ int search_libraries() * all the modules into a single output module, and then writes this to a * file. */ -void write_output(const char * filename) +void write_output(const char *filename) { - FILE * f; - rdf_headerbuf * rdfheader; - struct modulenode * cur; - int i, availableseg, seg, localseg, isrelative; - void * header; - rdfheaderrec * hr, newrec; - symtabEnt * se; - segtab segs; - long offset; - byte * data; - - if ((f = fopen(filename, "wb"))==NULL) { - fprintf(stderr, "ldrdf: couldn't open %s for output\n", filename); - exit(1); + FILE *f; + rdf_headerbuf *rdfheader; + struct modulenode *cur; + int i, availableseg, seg, localseg, isrelative; + void *header; + rdfheaderrec *hr, newrec; + symtabEnt *se; + segtab segs; + long offset; + byte *data; + + if ((f = fopen(filename, "wb")) == NULL) { + fprintf(stderr, "ldrdf: couldn't open %s for output\n", filename); + exit(1); } - if ((rdfheader=rdfnewheader())==NULL) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); + if ((rdfheader = rdfnewheader()) == NULL) { + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); } - + /* * If '-g' option was given, first record in output file will be a * `generic' record, filled with a given file content. @@ -703,31 +704,35 @@ void write_output(const char * filename) * compliant kernels. */ if (generic_rec_file) { - FILE *ff; - - if (options.verbose) - printf("\nadding generic record from binary file %s\n", generic_rec_file); - + FILE *ff; + + if (options.verbose) + printf("\nadding generic record from binary file %s\n", + generic_rec_file); + hr = (rdfheaderrec *) malloc(sizeof(struct GenericRec)); - if ((ff = fopen(generic_rec_file, "r")) == NULL) { - fprintf(stderr, "ldrdf: couldn't open %s for input\n", generic_rec_file); - exit(1); - } - i = fread(hr->g.data, 1, sizeof(hr->g.data), ff); - fseek(ff, 0, SEEK_END); - if (ftell(ff) > sizeof(hr->g.data)) { - fprintf (error_file, "warning: maximum generic record size is %d, rest of file ignored\n", sizeof(hr->g.data)); - } - fclose(ff); - + if ((ff = fopen(generic_rec_file, "r")) == NULL) { + fprintf(stderr, "ldrdf: couldn't open %s for input\n", + generic_rec_file); + exit(1); + } + i = fread(hr->g.data, 1, sizeof(hr->g.data), ff); + fseek(ff, 0, SEEK_END); + if (ftell(ff) > sizeof(hr->g.data)) { + fprintf(error_file, + "warning: maximum generic record size is %d, rest of file ignored\n", + sizeof(hr->g.data)); + } + fclose(ff); + hr->g.type = 0; hr->g.reclen = i; - rdfaddheader(rdfheader, hr); + rdfaddheader(rdfheader, hr); free(hr); } if (options.verbose) - printf ("\nbuilding output module (%d segments)\n", nsegs); + printf("\nbuilding output module (%d segments)\n", nsegs); /* * Allocate the memory for the segments. We may be better off @@ -736,13 +741,14 @@ void write_output(const char * filename) * And you could always use DJGPP... */ for (i = 0; i < nsegs; i++) { - outputseg[i].data=NULL; - if(!outputseg[i].length) continue; - outputseg[i].data = malloc(outputseg[i].length); - if (!outputseg[i].data) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); - } + outputseg[i].data = NULL; + if (!outputseg[i].length) + continue; + outputseg[i].data = malloc(outputseg[i].length); + if (!outputseg[i].data) { + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); + } } /* @@ -754,302 +760,314 @@ void write_output(const char * filename) /* * Step through the modules, performing required actions on each one */ - for (cur = modules; cur; cur=cur->next) { - /* - * Read the actual segment contents into the correct places in - * the newly allocated segments - */ - - for (i = 0; i < cur->f.nsegs; i++) { - int dest = cur->seginfo[i].dest_seg; - - if (dest == -1) continue; - if (rdfloadseg(&cur->f, i, - outputseg[dest].data + cur->seginfo[i].reloc)) { - rdfperror("ldrdf", cur->name); - exit(1); - } - } - - /* - * Perform fixups, and add new header records where required - */ - - header = malloc(cur->f.header_len); - if (!header) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); - } - - if (cur->f.header_loc) - rdfheaderrewind(&cur->f); - else - if (rdfloadseg(&cur->f, RDOFF_HEADER, header)) { - rdfperror("ldrdf", cur->name); - exit(1); - } - - /* - * we need to create a local segment number -> location - * table for the segments in this module. - */ - init_seglocations(&segs); - for (i = 0; i < cur->f.nsegs; i++) { - add_seglocation(&segs, cur->f.seg[i].number, - cur->seginfo[i].dest_seg, cur->seginfo[i].reloc); - } - /* - * and the BSS segment (doh!) - */ - add_seglocation(&segs, 2, 2, cur->bss_reloc); - - while ((hr = rdfgetheaderrec(&cur->f))) { - switch(hr->type) { - case RDFREC_RELOC: /* relocation record - need to do a fixup */ - /* - * First correct the offset stored in the segment from - * the start of the segment (which may well have changed). - * - * To do this we add to the number stored the relocation - * factor associated with the segment that contains the - * target segment. - * - * The relocation could be a relative relocation, in which - * case we have to first subtract the amount we've relocated - * the containing segment by. - */ - if (!get_seglocation(&segs, hr->r.refseg, &seg, &offset)) { - fprintf(stderr, "%s: reloc to undefined segment %04x\n", - cur->name, (int) hr->r.refseg); - errorcount++; - break; - } - - isrelative = (hr->r.segment & RDOFF_RELATIVEMASK) == RDOFF_RELATIVEMASK; - hr->r.segment &= (RDOFF_RELATIVEMASK-1); - - if (hr->r.segment == 2 || - (localseg = rdffindsegment(&cur->f, hr->r.segment)) == -1) { - fprintf(stderr, "%s: reloc from %s segment (%d)\n", - cur->name, - hr->r.segment == 2 ? "BSS" : "unknown", - hr->r.segment); - errorcount++; - break; - } - - if (hr->r.length != 1 && hr->r.length != 2 && - hr->r.length != 4 ) { - fprintf(stderr, "%s: nonstandard length reloc " - "(%d bytes)\n", cur->name, hr->r.length); - errorcount++; - break; - } - - /* - * okay, now the relocation is in the segment pointed to by - * cur->seginfo[localseg], and we know everything else is - * okay to go ahead and do the relocation - */ - data = outputseg[cur->seginfo[localseg].dest_seg].data; - data += cur->seginfo[localseg].reloc + hr->r.offset; - - /* - * data now points to the reference that needs - * relocation. Calculate the relocation factor. - * Factor is: - * offset of referred object in segment [in offset] - * (- relocation of localseg, if ref is relative) - * For simplicity, the result is stored in 'offset'. - * Then add 'offset' onto the value at data. - */ - - if (isrelative) - offset -= cur->seginfo[localseg].reloc; - switch (hr->r.length) { - case 1: - offset += *data; - if (offset < -127 || offset > 128) - fprintf(error_file, "warning: relocation out of range " - "at %s(%02x:%08lx)\n", cur->name, - (int)hr->r.segment, hr->r.offset); - *data = (char) offset; - break; - case 2: - offset += * (short *)data; - if (offset < -32767 || offset > 32768) - fprintf(error_file, "warning: relocation out of range " - "at %s(%02x:%08lx)\n", cur->name, - (int)hr->r.segment, hr->r.offset); - * (short *)data = (short) offset; - break; - case 4: - * (long *)data += offset; - /* we can't easily detect overflow on this one */ - break; - } - - /* - * If the relocation was relative between two symbols in - * the same segment, then we're done. - * - * Otherwise, we need to output a new relocation record - * with the references updated segment and offset... - */ - if (!isrelative || cur->seginfo[localseg].dest_seg != seg) { - hr->r.segment = cur->seginfo[localseg].dest_seg; - hr->r.offset += cur->seginfo[localseg].reloc; - hr->r.refseg = seg; - if (isrelative) - hr->r.segment += RDOFF_RELATIVEMASK; - rdfaddheader(rdfheader, hr); - } - break; - - case RDFREC_IMPORT: /* import symbol */ - case RDFREC_FARIMPORT: - /* - * scan the global symbol table for the symbol - * and associate its location with the segment number - * for this module - */ - se = symtabFind(symtab, hr->i.label); - if (!se || se->segment == -1) { - if (!options.dynalink && !(hr->i.flags & SYM_IMPORT)) { - fprintf(error_file, "error: unresolved reference to `%s'" - " in module `%s'\n", hr->i.label, cur->name); - errorcount++; - } - /* - * we need to allocate a segment number for this - * symbol, and store it in the symbol table for - * future reference - */ - if (!se) { - se=malloc(sizeof(*se)); - if (!se) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); - } - se->name = strdup(hr->i.label); - se->flags = 0; - se->segment = availableseg++; - se->offset = 0; - symtabInsert(symtab, se); - } - else { - se->segment = availableseg++; - se->offset = 0; - } - /* - * output a header record that imports it to the - * recently allocated segment number... - */ - newrec = *hr; - newrec.i.segment = se->segment; - rdfaddheader(rdfheader, &newrec); - } - - add_seglocation(&segs, hr->i.segment, se->segment, se->offset); - break; - - case RDFREC_GLOBAL: /* export symbol */ - /* - * need to insert an export for this symbol into the new - * header, unless we're stripping symbols. Even if we're - * stripping, put the symbol if it's marked as SYM_GLOBAL. - */ - if (options.strip && !(hr->e.flags & SYM_GLOBAL)) - break; - - if (hr->e.segment == 2) { - seg = 2; - offset = cur->bss_reloc; - } - else { - localseg = rdffindsegment(&cur->f, hr->e.segment); - if (localseg == -1) { - fprintf(stderr, "%s: exported symbol `%s' from " - "unrecognised segment\n", cur->name, - hr->e.label); - errorcount++; - break; - } - offset = cur->seginfo[localseg].reloc; - seg = cur->seginfo[localseg].dest_seg; - } - - hr->e.segment = seg; - hr->e.offset += offset; - rdfaddheader(rdfheader, hr); - break; - - case RDFREC_MODNAME: /* module name */ - /* - * Insert module name record if export symbols - * are not stripped. - * If module name begins with '$' - insert it anyway. - */ - if (options.strip && hr->m.modname[0] != '$') break; - rdfaddheader(rdfheader, hr); - break; - - case RDFREC_DLL: /* DLL name */ - /* - * Insert DLL name if it begins with '$' - */ - if (hr->d.libname[0] != '$') break; - rdfaddheader(rdfheader, hr); - break; - - case RDFREC_SEGRELOC: /* segment fixup */ - /* - * modify the segment numbers if necessary, and - * pass straight through to the output module header - * - * *** FIXME *** - */ - if (hr->r.segment == 2) { - fprintf(stderr, "%s: segment fixup in BSS section\n", - cur->name); - errorcount++; - break; - } - localseg = rdffindsegment(&cur->f, hr->r.segment); - if (localseg == -1) { - fprintf(stderr, "%s: segment fixup in unrecognised" - " segment (%d)\n", cur->name, hr->r.segment); - errorcount++; - break; - } - hr->r.segment = cur->seginfo[localseg].dest_seg; - hr->r.offset += cur->seginfo[localseg].reloc; - - if (!get_seglocation(&segs, hr->r.refseg, &seg, &offset)) { - fprintf(stderr, "%s: segment fixup to undefined " - "segment %04x\n", cur->name, (int)hr->r.refseg); - errorcount++; - break; - } - hr->r.refseg = seg; - rdfaddheader(rdfheader, hr); - break; - - case RDFREC_COMMON: /* Common variable */ - /* Is this symbol already in the table? */ - se = symtabFind(symtab, hr->c.label); - if (!se) { - printf("%s is not in symtab yet\n", hr->c.label); - break; - } - /* Add segment location */ - add_seglocation(&segs, hr->c.segment, se->segment, se->offset); - break; - } - } - - free(header); - done_seglocations(&segs); + for (cur = modules; cur; cur = cur->next) { + /* + * Read the actual segment contents into the correct places in + * the newly allocated segments + */ + + for (i = 0; i < cur->f.nsegs; i++) { + int dest = cur->seginfo[i].dest_seg; + + if (dest == -1) + continue; + if (rdfloadseg(&cur->f, i, + outputseg[dest].data + cur->seginfo[i].reloc)) { + rdfperror("ldrdf", cur->name); + exit(1); + } + } + + /* + * Perform fixups, and add new header records where required + */ + + header = malloc(cur->f.header_len); + if (!header) { + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); + } + + if (cur->f.header_loc) + rdfheaderrewind(&cur->f); + else if (rdfloadseg(&cur->f, RDOFF_HEADER, header)) { + rdfperror("ldrdf", cur->name); + exit(1); + } + + /* + * we need to create a local segment number -> location + * table for the segments in this module. + */ + init_seglocations(&segs); + for (i = 0; i < cur->f.nsegs; i++) { + add_seglocation(&segs, cur->f.seg[i].number, + cur->seginfo[i].dest_seg, + cur->seginfo[i].reloc); + } + /* + * and the BSS segment (doh!) + */ + add_seglocation(&segs, 2, 2, cur->bss_reloc); + + while ((hr = rdfgetheaderrec(&cur->f))) { + switch (hr->type) { + case RDFREC_RELOC: /* relocation record - need to do a fixup */ + /* + * First correct the offset stored in the segment from + * the start of the segment (which may well have changed). + * + * To do this we add to the number stored the relocation + * factor associated with the segment that contains the + * target segment. + * + * The relocation could be a relative relocation, in which + * case we have to first subtract the amount we've relocated + * the containing segment by. + */ + if (!get_seglocation(&segs, hr->r.refseg, &seg, &offset)) { + fprintf(stderr, + "%s: reloc to undefined segment %04x\n", + cur->name, (int)hr->r.refseg); + errorcount++; + break; + } + + isrelative = + (hr->r.segment & RDOFF_RELATIVEMASK) == + RDOFF_RELATIVEMASK; + hr->r.segment &= (RDOFF_RELATIVEMASK - 1); + + if (hr->r.segment == 2 || + (localseg = + rdffindsegment(&cur->f, hr->r.segment)) == -1) { + fprintf(stderr, "%s: reloc from %s segment (%d)\n", + cur->name, + hr->r.segment == 2 ? "BSS" : "unknown", + hr->r.segment); + errorcount++; + break; + } + + if (hr->r.length != 1 && hr->r.length != 2 && + hr->r.length != 4) { + fprintf(stderr, "%s: nonstandard length reloc " + "(%d bytes)\n", cur->name, hr->r.length); + errorcount++; + break; + } + + /* + * okay, now the relocation is in the segment pointed to by + * cur->seginfo[localseg], and we know everything else is + * okay to go ahead and do the relocation + */ + data = outputseg[cur->seginfo[localseg].dest_seg].data; + data += cur->seginfo[localseg].reloc + hr->r.offset; + + /* + * data now points to the reference that needs + * relocation. Calculate the relocation factor. + * Factor is: + * offset of referred object in segment [in offset] + * (- relocation of localseg, if ref is relative) + * For simplicity, the result is stored in 'offset'. + * Then add 'offset' onto the value at data. + */ + + if (isrelative) + offset -= cur->seginfo[localseg].reloc; + switch (hr->r.length) { + case 1: + offset += *data; + if (offset < -127 || offset > 128) + fprintf(error_file, + "warning: relocation out of range " + "at %s(%02x:%08lx)\n", cur->name, + (int)hr->r.segment, hr->r.offset); + *data = (char)offset; + break; + case 2: + offset += *(short *)data; + if (offset < -32767 || offset > 32768) + fprintf(error_file, + "warning: relocation out of range " + "at %s(%02x:%08lx)\n", cur->name, + (int)hr->r.segment, hr->r.offset); + *(short *)data = (short)offset; + break; + case 4: + *(long *)data += offset; + /* we can't easily detect overflow on this one */ + break; + } + + /* + * If the relocation was relative between two symbols in + * the same segment, then we're done. + * + * Otherwise, we need to output a new relocation record + * with the references updated segment and offset... + */ + if (!isrelative || cur->seginfo[localseg].dest_seg != seg) { + hr->r.segment = cur->seginfo[localseg].dest_seg; + hr->r.offset += cur->seginfo[localseg].reloc; + hr->r.refseg = seg; + if (isrelative) + hr->r.segment += RDOFF_RELATIVEMASK; + rdfaddheader(rdfheader, hr); + } + break; + + case RDFREC_IMPORT: /* import symbol */ + case RDFREC_FARIMPORT: + /* + * scan the global symbol table for the symbol + * and associate its location with the segment number + * for this module + */ + se = symtabFind(symtab, hr->i.label); + if (!se || se->segment == -1) { + if (!options.dynalink && !(hr->i.flags & SYM_IMPORT)) { + fprintf(error_file, + "error: unresolved reference to `%s'" + " in module `%s'\n", hr->i.label, + cur->name); + errorcount++; + } + /* + * we need to allocate a segment number for this + * symbol, and store it in the symbol table for + * future reference + */ + if (!se) { + se = malloc(sizeof(*se)); + if (!se) { + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); + } + se->name = strdup(hr->i.label); + se->flags = 0; + se->segment = availableseg++; + se->offset = 0; + symtabInsert(symtab, se); + } else { + se->segment = availableseg++; + se->offset = 0; + } + /* + * output a header record that imports it to the + * recently allocated segment number... + */ + newrec = *hr; + newrec.i.segment = se->segment; + rdfaddheader(rdfheader, &newrec); + } + + add_seglocation(&segs, hr->i.segment, se->segment, + se->offset); + break; + + case RDFREC_GLOBAL: /* export symbol */ + /* + * need to insert an export for this symbol into the new + * header, unless we're stripping symbols. Even if we're + * stripping, put the symbol if it's marked as SYM_GLOBAL. + */ + if (options.strip && !(hr->e.flags & SYM_GLOBAL)) + break; + + if (hr->e.segment == 2) { + seg = 2; + offset = cur->bss_reloc; + } else { + localseg = rdffindsegment(&cur->f, hr->e.segment); + if (localseg == -1) { + fprintf(stderr, "%s: exported symbol `%s' from " + "unrecognised segment\n", cur->name, + hr->e.label); + errorcount++; + break; + } + offset = cur->seginfo[localseg].reloc; + seg = cur->seginfo[localseg].dest_seg; + } + + hr->e.segment = seg; + hr->e.offset += offset; + rdfaddheader(rdfheader, hr); + break; + + case RDFREC_MODNAME: /* module name */ + /* + * Insert module name record if export symbols + * are not stripped. + * If module name begins with '$' - insert it anyway. + */ + if (options.strip && hr->m.modname[0] != '$') + break; + rdfaddheader(rdfheader, hr); + break; + + case RDFREC_DLL: /* DLL name */ + /* + * Insert DLL name if it begins with '$' + */ + if (hr->d.libname[0] != '$') + break; + rdfaddheader(rdfheader, hr); + break; + + case RDFREC_SEGRELOC: /* segment fixup */ + /* + * modify the segment numbers if necessary, and + * pass straight through to the output module header + * + * *** FIXME *** + */ + if (hr->r.segment == 2) { + fprintf(stderr, "%s: segment fixup in BSS section\n", + cur->name); + errorcount++; + break; + } + localseg = rdffindsegment(&cur->f, hr->r.segment); + if (localseg == -1) { + fprintf(stderr, "%s: segment fixup in unrecognised" + " segment (%d)\n", cur->name, hr->r.segment); + errorcount++; + break; + } + hr->r.segment = cur->seginfo[localseg].dest_seg; + hr->r.offset += cur->seginfo[localseg].reloc; + + if (!get_seglocation(&segs, hr->r.refseg, &seg, &offset)) { + fprintf(stderr, "%s: segment fixup to undefined " + "segment %04x\n", cur->name, + (int)hr->r.refseg); + errorcount++; + break; + } + hr->r.refseg = seg; + rdfaddheader(rdfheader, hr); + break; + + case RDFREC_COMMON: /* Common variable */ + /* Is this symbol already in the table? */ + se = symtabFind(symtab, hr->c.label); + if (!se) { + printf("%s is not in symtab yet\n", hr->c.label); + break; + } + /* Add segment location */ + add_seglocation(&segs, hr->c.segment, se->segment, + se->offset); + break; + } + } + + free(header); + done_seglocations(&segs); } @@ -1065,33 +1083,35 @@ void write_output(const char * filename) * Write the header */ for (i = 0; i < nsegs; i++) { - if (i == 2) continue; - rdfaddsegment (rdfheader, outputseg[i].length); + if (i == 2) + continue; + rdfaddsegment(rdfheader, outputseg[i].length); } - + rdfwriteheader(f, rdfheader); rdfdoneheader(rdfheader); - + /* * Step through the segments, one at a time, writing out into * the output file */ for (i = 0; i < nsegs; i++) { - uint16 s; - long l; - - if (i == 2) continue; - - s = translateshort(outputseg[i].type); - fwrite(&s, 2, 1, f); - s = translateshort(outputseg[i].number); - fwrite(&s, 2, 1, f); - s = translateshort(outputseg[i].reserved); - fwrite(&s, 2, 1, f); - l = translatelong(outputseg[i].length); - fwrite(&l, 4, 1, f); - - fwrite(outputseg[i].data, outputseg[i].length, 1, f); + uint16 s; + long l; + + if (i == 2) + continue; + + s = translateshort(outputseg[i].type); + fwrite(&s, 2, 1, f); + s = translateshort(outputseg[i].number); + fwrite(&s, 2, 1, f); + s = translateshort(outputseg[i].reserved); + fwrite(&s, 2, 1, f); + l = translatelong(outputseg[i].length); + fwrite(&l, 4, 1, f); + + fwrite(outputseg[i].data, outputseg[i].length, 1, f); } fwrite("\0\0\0\0\0\0\0\0\0\0", 10, 1, f); @@ -1118,11 +1138,11 @@ void usage() exit(0); } -int main(int argc, char ** argv) +int main(int argc, char **argv) { - char * outname = "aout.rdf"; - int moduleloaded = 0; - char *respstrings[128] = {0, }; + char *outname = "aout.rdf"; + int moduleloaded = 0; + char *respstrings[128] = { 0, }; options.verbose = 0; options.align = 16; @@ -1132,166 +1152,177 @@ int main(int argc, char ** argv) error_file = stderr; argc--, argv++; - if (argc == 0) usage(); + if (argc == 0) + usage(); while (argc && *argv && **argv == '-' && argv[0][1] != 'l') { - switch(argv[0][1]) { - case 'r': - printf("ldrdf (linker for RDF files) version " LDRDF_VERSION "\n"); - printf("RDOFF2 revision %s\n", RDOFF2_REVISION); - exit(0); - case 'v': - if (argv[0][2] == '=') { - options.verbose = argv[0][3] - '0'; - if (options.verbose < 0 || options.verbose > 9) { - fprintf(stderr, "ldrdf: verbosity level must be a number" - " between 0 and 9\n"); - exit(1); - } - } - else - options.verbose++; - break; - case 'a': - options.align = atoi(argv[1]); - if (options.align <= 0) { - fprintf(stderr, - "ldrdf: -a expects a positive number argument\n"); - exit(1); - } - argv++, argc--; - break; - case 's': - options.strip = 1; - break; - case 'd': - if (argv[0][2] == 'y') options.dynalink = 1; - break; - case 'o': - outname = argv[1]; - argv++, argc--; - break; - case 'j': - if (!objpath) { - options.objpath = 1; - objpath = argv[1]; - argv++, argc--; - break; - } else { - fprintf(stderr,"ldrdf: more than one objects search path specified\n"); - exit(1); - } - case 'L': - if (!libpath) { - options.libpath = 1; - libpath = argv[1]; - argv++, argc--; - break; - } else { - fprintf(stderr,"ldrdf: more than one libraries search path specified\n"); - exit(1); - } - case '@': { - int i=0; - char buf[256]; - FILE *f; - - options.respfile = 1; - if (argv[1] != NULL) f = fopen(argv[1],"r"); - else { - fprintf(stderr,"ldrdf: no response file name specified\n"); - exit(1); - } - - if (f == NULL) { - fprintf(stderr,"ldrdf: unable to open response file\n"); - exit(1); - } - - argv++, argc--; - while (fgets(buf, sizeof(buf), f) != NULL) { - char *p; - if (buf[0]=='\n') continue; - if ((p = strchr(buf,'\n')) != NULL) *p = '\0'; - if (i >= 128) { - fprintf(stderr,"ldrdf: too many input files\n"); - exit(1); - } - *(respstrings + i) = newstr(buf); - argc++, i++; - } + switch (argv[0][1]) { + case 'r': + printf("ldrdf (linker for RDF files) version " LDRDF_VERSION + "\n"); + printf("RDOFF2 revision %s\n", RDOFF2_REVISION); + exit(0); + case 'v': + if (argv[0][2] == '=') { + options.verbose = argv[0][3] - '0'; + if (options.verbose < 0 || options.verbose > 9) { + fprintf(stderr, + "ldrdf: verbosity level must be a number" + " between 0 and 9\n"); + exit(1); + } + } else + options.verbose++; + break; + case 'a': + options.align = atoi(argv[1]); + if (options.align <= 0) { + fprintf(stderr, + "ldrdf: -a expects a positive number argument\n"); + exit(1); + } + argv++, argc--; + break; + case 's': + options.strip = 1; break; - } - case '2': + case 'd': + if (argv[0][2] == 'y') + options.dynalink = 1; + break; + case 'o': + outname = argv[1]; + argv++, argc--; + break; + case 'j': + if (!objpath) { + options.objpath = 1; + objpath = argv[1]; + argv++, argc--; + break; + } else { + fprintf(stderr, + "ldrdf: more than one objects search path specified\n"); + exit(1); + } + case 'L': + if (!libpath) { + options.libpath = 1; + libpath = argv[1]; + argv++, argc--; + break; + } else { + fprintf(stderr, + "ldrdf: more than one libraries search path specified\n"); + exit(1); + } + case '@':{ + int i = 0; + char buf[256]; + FILE *f; + + options.respfile = 1; + if (argv[1] != NULL) + f = fopen(argv[1], "r"); + else { + fprintf(stderr, + "ldrdf: no response file name specified\n"); + exit(1); + } + + if (f == NULL) { + fprintf(stderr, + "ldrdf: unable to open response file\n"); + exit(1); + } + + argv++, argc--; + while (fgets(buf, sizeof(buf), f) != NULL) { + char *p; + if (buf[0] == '\n') + continue; + if ((p = strchr(buf, '\n')) != NULL) + *p = '\0'; + if (i >= 128) { + fprintf(stderr, "ldrdf: too many input files\n"); + exit(1); + } + *(respstrings + i) = newstr(buf); + argc++, i++; + } + break; + } + case '2': options.stderr_redir = 1; - error_file = stdout; - break; - case 'g': - generic_rec_file = argv[1]; - argv++, argc--; - break; - default: - usage(); - } - argv++, argc--; + error_file = stdout; + break; + case 'g': + generic_rec_file = argv[1]; + argv++, argc--; + break; + default: + usage(); + } + argv++, argc--; } if (options.verbose > 4) { - printf("ldrdf invoked with options:\n"); - printf(" section alignment: %d bytes\n", options.align); - printf(" output name: `%s'\n", outname); - if (options.strip) - printf(" strip symbols\n"); - if (options.dynalink) - printf(" Unix-style dynamic linking\n"); + printf("ldrdf invoked with options:\n"); + printf(" section alignment: %d bytes\n", options.align); + printf(" output name: `%s'\n", outname); + if (options.strip) + printf(" strip symbols\n"); + if (options.dynalink) + printf(" Unix-style dynamic linking\n"); if (options.objpath) printf(" objects search path: %s\n", objpath); if (options.libpath) printf(" libraries search path: %s\n", libpath); - printf("\n"); + printf("\n"); } symtab = symtabNew(); initsegments(); if (!symtab) { - fprintf(stderr, "ldrdf: out of memory\n"); - exit(1); + fprintf(stderr, "ldrdf: out of memory\n"); + exit(1); } while (argc) { - if (!*argv) argv = respstrings; - if (!*argv) break; - if (!strncmp(*argv, "-l", 2)) { - if(libpath && (argv[0][2] != '/')) - add_library(newstrcat(libpath,*argv + 2)); + if (!*argv) + argv = respstrings; + if (!*argv) + break; + if (!strncmp(*argv, "-l", 2)) { + if (libpath && (argv[0][2] != '/')) + add_library(newstrcat(libpath, *argv + 2)); else - add_library(*argv + 2); + add_library(*argv + 2); } else { - if(objpath && (argv[0][0] != '/')) - loadmodule(newstrcat(objpath, *argv)); - else - loadmodule(*argv); - moduleloaded = 1; - } - argv++, argc--; + if (objpath && (argv[0][0] != '/')) + loadmodule(newstrcat(objpath, *argv)); + else + loadmodule(*argv); + moduleloaded = 1; + } + argv++, argc--; } - if (! moduleloaded) { - printf("ldrdf: nothing to do. ldrdf -h for usage\n"); - return 0; + if (!moduleloaded) { + printf("ldrdf: nothing to do. ldrdf -h for usage\n"); + return 0; } search_libraries(); - if (options.verbose > 2) - { - printf ("symbol table:\n"); - symtabDump(symtab, stdout); + if (options.verbose > 2) { + printf("symbol table:\n"); + symtabDump(symtab, stdout); } write_output(outname); - if (errorcount > 0) exit(1); + if (errorcount > 0) + exit(1); return 0; } - diff --git a/rdoff/ldsegs.h b/rdoff/ldsegs.h index 8b516fa..2a8ae88 100644 --- a/rdoff/ldsegs.h +++ b/rdoff/ldsegs.h @@ -8,12 +8,12 @@ #endif struct segconfig { - UI16 typelow, typehi;/* range of seg nos for which this is valid */ - char * typedesc; /* a description of the segment type */ - UI16 dowhat; /* one of the SEG_xxxx values below */ - UI16 mergetype; /* if SEG_MERGE what type segment do we merge with? - 0 -> same type of segment. This type is also - used with SEG_NEWSEG. */ + UI16 typelow, typehi; /* range of seg nos for which this is valid */ + char *typedesc; /* a description of the segment type */ + UI16 dowhat; /* one of the SEG_xxxx values below */ + UI16 mergetype; /* if SEG_MERGE what type segment do we merge with? + 0 -> same type of segment. This type is also + used with SEG_NEWSEG. */ }; #define SEG_IGNORE 0 @@ -33,7 +33,8 @@ struct segconfig sconft[SEGCONFIGMAX] = { {0x0020, 0x0FFF, "reserved(MOSCOW)", 1, 0}, {0x1000, 0x7FFF, "reserved(system dependant)", 1, 0}, {0x8000, 0xFFFE, "reserved(other)", 1, 0}, - {0xFFFF, 0xFFFF, "invalid segment", 0, 0}}; + {0xFFFF, 0xFFFF, "invalid segment", 0, 0} +}; #define getsegconfig(target,number) \ { \ diff --git a/rdoff/rdf2bin.c b/rdoff/rdf2bin.c index aee6afb..ac51c0e 100644 --- a/rdoff/rdf2bin.c +++ b/rdoff/rdf2bin.c @@ -12,124 +12,121 @@ long origin = 0; int align = 16; -char *getfilename(char * pathname) +char *getfilename(char *pathname) { - char * lastslash = pathname - 1; - char * i = pathname; + char *lastslash = pathname - 1; + char *i = pathname; - while ( *i ) { - if (*i == '/') lastslash = i; - i++; + 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, bsspad=0; + rdfmodule *m; + int tmp; + FILE *of; + char *padding; + int codepad, datapad, bsspad = 0; 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; + 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; + 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 if (! strcmp(*argv,"-b")) { - argv++, argc--; - bsspad = readnum(*argv, &tmp); - if (tmp) { - fprintf(stderr,"rdf2bin: invalid parameter: %s\n",*argv); - return 1; - } - } else - break; - - argv++, argc--; + 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 if (!strcmp(*argv, "-b")) { + argv++, argc--; + bsspad = 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; + puts("rdf2bin: required parameter missing"); + return -1; } m = rdfload(*argv); - if (! m) { - rdfperror("rdf2bin",*argv); - return 1; + if (!m) { + rdfperror("rdf2bin", *argv); + return 1; } - printf("relocating %s: origin=%lx, align=%d\n",*argv,origin,align); + printf("relocating %s: origin=%lx, align=%d\n", *argv, origin, align); m->textrel = origin; m->datarel = origin + m->f.seg[0].length; if (m->datarel % align != 0) { - codepad = align - (m->datarel % align); - m->datarel += codepad; - } - else - codepad = 0; + codepad = align - (m->datarel % align); + m->datarel += codepad; + } else + codepad = 0; m->bssrel = m->datarel + m->f.seg[1].length; if (m->bssrel % align != 0) { - datapad = align - (m->bssrel % align); - m->bssrel += datapad; - } - else - datapad = 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); + m->textrel, m->datarel, m->bssrel); rdf_relocate(m); argv++; - of = fopen(*argv,"wb"); + of = fopen(*argv, "wb"); if (!of) { - fprintf(stderr,"rdf2bin: could not open output file %s\n",*argv); - return 1; + 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; + fprintf(stderr, "rdf2bin: out of memory\n"); + return 1; } - if (fwrite(m->t,1,m->f.seg[0].length,of) != m->f.seg[0].length || - fwrite(padding,1,codepad,of) != codepad || - fwrite(m->d,1,m->f.seg[1].length,of) != m->f.seg[1].length) - { - fprintf(stderr,"rdf2bin: error writing to %s\n", *argv); - return 1; + if (fwrite(m->t, 1, m->f.seg[0].length, of) != m->f.seg[0].length || + fwrite(padding, 1, codepad, of) != codepad || + fwrite(m->d, 1, m->f.seg[1].length, of) != m->f.seg[1].length) { + fprintf(stderr, "rdf2bin: error writing to %s\n", *argv); + return 1; } - + if (bsspad) { - void *p = calloc(bsspad-=(m->bssrel - origin),1); - fwrite(p,1,bsspad,of); + void *p = calloc(bsspad -= (m->bssrel - origin), 1); + fwrite(p, 1, bsspad, of); } fclose(of); diff --git a/rdoff/rdf2ihx.c b/rdoff/rdf2ihx.c index 43e5c72..512af55 100644 --- a/rdoff/rdf2ihx.c +++ b/rdoff/rdf2ihx.c @@ -17,174 +17,174 @@ int align = 16; /* This function writes a single n-byte data record to of. Maximum value for n is 255. */ -static int write_data_record(FILE *of, int ofs, int nbytes, - unsigned char *data) +static int write_data_record(FILE * of, int ofs, int nbytes, + unsigned char *data) { - int i, iofs; - unsigned int checksum; - - iofs = ofs; - fprintf(of, ":%02X%04X00", nbytes, ofs); - checksum = 0; - for (i=0; i<nbytes; i++) { - fprintf(of, "%02X", data[i]); - ofs++; - checksum += data[i]; - } - checksum = checksum + /* current checksum */ - nbytes + /* RECLEN (one byte) */ - ((iofs >> 8) & 0xff) + /* high byte of load offset */ - (iofs & 0xff); /* low byte of load offset */ - checksum = ~checksum + 1; - fprintf(of, "%02X\n", checksum&0xff); - return(ofs); + int i, iofs; + unsigned int checksum; + + iofs = ofs; + fprintf(of, ":%02X%04X00", nbytes, ofs); + checksum = 0; + for (i = 0; i < nbytes; i++) { + fprintf(of, "%02X", data[i]); + ofs++; + checksum += data[i]; + } + checksum = checksum + /* current checksum */ + nbytes + /* RECLEN (one byte) */ + ((iofs >> 8) & 0xff) + /* high byte of load offset */ + (iofs & 0xff); /* low byte of load offset */ + checksum = ~checksum + 1; + fprintf(of, "%02X\n", checksum & 0xff); + return (ofs); } int main(int argc, char **argv) { - rdfmodule *m; - int tmp; - FILE *of; - char *padding; - unsigned char *segbin[2]; - int pad[2], segn, ofs, i; - long segaddr; - unsigned int checksum; - symtabEnt *s; - - if (argc < 2) { - puts("Usage: rdf2ihx [-o relocation-origin] [-p segment-alignment] " - "input-file output-file"); - return(1); - } - - argv++, argc--; - - while (argc > 2) { - if (strcmp(*argv,"-o") == 0) { - argv++, argc--; - origin = readnum(*argv, &tmp); - if (tmp) { - fprintf(stderr,"rdf2ihx: invalid parameter: %s\n",*argv); - return 1; - } - } else if (strcmp(*argv,"-p") == 0) { - argv++, argc--; - align = readnum(*argv, &tmp); - if (tmp) { - fprintf(stderr,"rdf2ihx: invalid parameter: %s\n",*argv); - return 1; - } - } else - break; + rdfmodule *m; + int tmp; + FILE *of; + char *padding; + unsigned char *segbin[2]; + int pad[2], segn, ofs, i; + long segaddr; + unsigned int checksum; + symtabEnt *s; + + if (argc < 2) { + puts("Usage: rdf2ihx [-o relocation-origin] [-p segment-alignment] " "input-file output-file"); + return (1); + } + argv++, argc--; - } - if (argc < 2) { - puts("rdf2ihx: required parameter missing"); - return -1; - } - m = rdfload(*argv); - - if (!m) { - rdfperror("rdf2ihx",*argv); - return 1; - } - printf("relocating %s: origin=%lx, align=%d\n",*argv, origin, align); - - m->textrel = origin; - m->datarel = origin + m->f.seg[0].length; - if (m->datarel % align != 0) { - pad[0] = align - (m->datarel % align); - m->datarel += pad[0]; - } else { - pad[0] = 0; - } - - m->bssrel = m->datarel + m->f.seg[1].length; - if (m->bssrel % align != 0) { - pad[1] = align - (m->bssrel % align); - m->bssrel += pad[1]; - } else { - pad[1] = 0; - } - - printf("code: %08lx\ndata: %08lx\nbss: %08lx\n", - m->textrel, m->datarel, m->bssrel); - - rdf_relocate(m); - - argv++; - - of = fopen(*argv,"w"); - if (!of) { - fprintf(stderr,"rdf2ihx: could not open output file %s\n",*argv); - return(1); - } - - padding = malloc(align); - if (!padding) { - fprintf(stderr,"rdf2ihx: out of memory\n"); - return(1); - } - - /* write extended segment address record */ - fprintf(of, ":02000002"); /* Record mark, reclen, load offset & rectyp - fields for ext. seg. address record */ - segaddr = ((origin >> 16) & 0xffff); /* segment address */ - fprintf(of, "%04X", (unsigned int)(segaddr & 0xffff)); - checksum = 0x02 + /* reclen */ - 0x0000 + /* Load Offset */ - 0x02 + /* Rectyp */ - (segaddr & 0xff) + /* USBA low */ - ((segaddr >> 8) & 0xff); /* USBA high */ - checksum = ~checksum + 1; /* two's-complement the checksum */ - fprintf(of, "%02X\n", checksum & 0xff); - - /* See if there's a '_main' symbol in the symbol table */ - if ((s=symtabFind(m->symtab, "_main")) == NULL) { - printf("No _main symbol found, no start segment address record added\n"); - } else { - printf("_main symbol found at %04x:%04x\n", s->segment, - (unsigned int)(s->offset & 0xffff)); - /* Create a start segment address record for the _main symbol. */ - segaddr = ((s->segment & 0xffff) << 16) + ((s->offset) & 0xffff); - fprintf(of, ":04000003"); /* Record mark, reclen, load offset & rectyp - fields for start seg. addr. record */ - fprintf(of, "%08lX", segaddr); /* CS/IP field */ - checksum = 0x04 + /* reclen */ - 0x0000 + /* load offset */ - 0x03 + /* Rectyp */ - (segaddr & 0xff) + /* low-low byte of segaddr */ - ((segaddr >> 8) & 0xff) + /* low-high byte of segaddr */ - ((segaddr >> 16) & 0xff) + /* high-low byte of segaddr */ - ((segaddr >> 24) & 0xff); /* high-high byte of segaddr */ - checksum = ~checksum + 1; /* two's complement */ + + while (argc > 2) { + if (strcmp(*argv, "-o") == 0) { + argv++, argc--; + origin = readnum(*argv, &tmp); + if (tmp) { + fprintf(stderr, "rdf2ihx: invalid parameter: %s\n", *argv); + return 1; + } + } else if (strcmp(*argv, "-p") == 0) { + argv++, argc--; + align = readnum(*argv, &tmp); + if (tmp) { + fprintf(stderr, "rdf2ihx: invalid parameter: %s\n", *argv); + return 1; + } + } else + break; + argv++, argc--; + } + if (argc < 2) { + puts("rdf2ihx: required parameter missing"); + return -1; + } + m = rdfload(*argv); + + if (!m) { + rdfperror("rdf2ihx", *argv); + return 1; + } + printf("relocating %s: origin=%lx, align=%d\n", *argv, origin, align); + + m->textrel = origin; + m->datarel = origin + m->f.seg[0].length; + if (m->datarel % align != 0) { + pad[0] = align - (m->datarel % align); + m->datarel += pad[0]; + } else { + pad[0] = 0; + } + + m->bssrel = m->datarel + m->f.seg[1].length; + if (m->bssrel % align != 0) { + pad[1] = align - (m->bssrel % align); + m->bssrel += pad[1]; + } else { + pad[1] = 0; + } + + printf("code: %08lx\ndata: %08lx\nbss: %08lx\n", + m->textrel, m->datarel, m->bssrel); + + rdf_relocate(m); + + argv++; + + of = fopen(*argv, "w"); + if (!of) { + fprintf(stderr, "rdf2ihx: could not open output file %s\n", *argv); + return (1); + } + + padding = malloc(align); + if (!padding) { + fprintf(stderr, "rdf2ihx: out of memory\n"); + return (1); + } + + /* write extended segment address record */ + fprintf(of, ":02000002"); /* Record mark, reclen, load offset & rectyp + fields for ext. seg. address record */ + segaddr = ((origin >> 16) & 0xffff); /* segment address */ + fprintf(of, "%04X", (unsigned int)(segaddr & 0xffff)); + checksum = 0x02 + /* reclen */ + 0x0000 + /* Load Offset */ + 0x02 + /* Rectyp */ + (segaddr & 0xff) + /* USBA low */ + ((segaddr >> 8) & 0xff); /* USBA high */ + checksum = ~checksum + 1; /* two's-complement the checksum */ fprintf(of, "%02X\n", checksum & 0xff); - } - - /* Now it's time to write data records from the code and data segments in. - This current version doesn't check for segment overflow; proper behavior - should be to output a segment address record for the code and data - segments. Something to do. */ - ofs = 0; - segbin[0] = m->t; - segbin[1] = m->d; - for (segn=0; segn<2; segn++) { - int mod, adr; - - if (m->f.seg[segn].length == 0) - continue; - for (i=0; i+15<m->f.seg[segn].length; i+=16) { - ofs = write_data_record(of, ofs, 16, &segbin[segn][i]); + + /* See if there's a '_main' symbol in the symbol table */ + if ((s = symtabFind(m->symtab, "_main")) == NULL) { + printf + ("No _main symbol found, no start segment address record added\n"); + } else { + printf("_main symbol found at %04x:%04x\n", s->segment, + (unsigned int)(s->offset & 0xffff)); + /* Create a start segment address record for the _main symbol. */ + segaddr = ((s->segment & 0xffff) << 16) + ((s->offset) & 0xffff); + fprintf(of, ":04000003"); /* Record mark, reclen, load offset & rectyp + fields for start seg. addr. record */ + fprintf(of, "%08lX", segaddr); /* CS/IP field */ + checksum = 0x04 + /* reclen */ + 0x0000 + /* load offset */ + 0x03 + /* Rectyp */ + (segaddr & 0xff) + /* low-low byte of segaddr */ + ((segaddr >> 8) & 0xff) + /* low-high byte of segaddr */ + ((segaddr >> 16) & 0xff) + /* high-low byte of segaddr */ + ((segaddr >> 24) & 0xff); /* high-high byte of segaddr */ + checksum = ~checksum + 1; /* two's complement */ + fprintf(of, "%02X\n", checksum & 0xff); } - if ((mod=m->f.seg[segn].length & 0x000f) != 0) { - adr = m->f.seg[segn].length & 0xfff0; - ofs = write_data_record(of, ofs, mod, &segbin[segn][adr]); + + /* Now it's time to write data records from the code and data segments in. + This current version doesn't check for segment overflow; proper behavior + should be to output a segment address record for the code and data + segments. Something to do. */ + ofs = 0; + segbin[0] = m->t; + segbin[1] = m->d; + for (segn = 0; segn < 2; segn++) { + int mod, adr; + + if (m->f.seg[segn].length == 0) + continue; + for (i = 0; i + 15 < m->f.seg[segn].length; i += 16) { + ofs = write_data_record(of, ofs, 16, &segbin[segn][i]); + } + if ((mod = m->f.seg[segn].length & 0x000f) != 0) { + adr = m->f.seg[segn].length & 0xfff0; + ofs = write_data_record(of, ofs, mod, &segbin[segn][adr]); + } } - } - /* output an end of file record */ - fprintf(of, ":00000001FF\n"); + /* output an end of file record */ + fprintf(of, ":00000001FF\n"); - fclose(of); - return 0; + fclose(of); + return 0; } diff --git a/rdoff/rdfdump.c b/rdoff/rdfdump.c index 626fd2a..de2bb59 100644 --- a/rdoff/rdfdump.c +++ b/rdoff/rdfdump.c @@ -22,139 +22,143 @@ void print_header(long length, int rdf_version) uint16 rs; while (length > 0) { - fread(&t, 1, 1, infile); - if (rdf_version >= 2) { - fread(&reclen, 1, 1, infile); - } - switch (t) { - case RDFREC_GENERIC: /* generic record */ - printf(" generic record (length=%d)\n", (int) reclen); - fseek(infile, reclen, SEEK_CUR); - break; - - case RDFREC_RELOC: /* relocation record */ - case RDFREC_SEGRELOC: /* segment relocation */ - fread(&s, 1, 1, infile); - fread(&o, 4, 1, infile); - fread(&l, 1, 1, infile); - fread(&rs, 2, 1, infile); - printf(" %s: location (%04x:%08lx), length %d, " - "referred seg %04x\n", t == 1 ? "relocation" : "seg relocation", - (int) s, translatelong(o), (int) l, translateshort(rs)); - if (rdf_version >= 2 && reclen != 8) - printf(" warning: reclen != 8\n"); - if (rdf_version == 1) - length -= 9; - if (rdf_version == 1 && t == 6) - printf(" warning: seg relocation not supported in RDOFF1\n"); - break; - - case RDFREC_IMPORT: /* import record */ - case RDFREC_FARIMPORT: /* import far symbol */ - fread(&flags, 1, 1, infile); - fread(&rs, 2, 1, infile); - ll = 0; - - if (rdf_version == 1) { - do { - fread(&buf[ll], 1, 1, infile); - } while (buf[ll++]); - } else { - for (; ll < reclen - 3; ll++) - fread(&buf[ll], 1, 1, infile); - } - - if (t == 7) - printf("far "); - printf((flags & SYM_IMPORT) ? " import" : " extern"); - if (flags & SYM_FUNCTION) - printf(" proc"); - if (flags & SYM_DATA) - printf(" data"); - printf(": segment %04x = %s\n", translateshort(rs), buf); - if (rdf_version == 1) - length -= ll + 3; - if (rdf_version == 1 && t == 7) - printf(" warning: far import not supported in RDOFF1\n"); - break; - - case RDFREC_GLOBAL: /* export record */ - fread(&flags, 1, 1, infile); - fread(&s, 1, 1, infile); - fread(&o, 4, 1, infile); - ll = 0; - - if (rdf_version == 1) { - do { - fread(&buf[ll], 1, 1, infile); - } while (buf[ll++]); - } else { - for (; ll < reclen - 6; ll++) - fread(&buf[ll], 1, 1, infile); - } - printf((flags & SYM_GLOBAL) ? " export" : " public"); - if (flags & SYM_FUNCTION) - printf(" proc"); - if (flags & SYM_DATA) - printf(" data"); - printf(": (%04x:%08lx) = %s\n", (int) s, translatelong(o), buf); - if (rdf_version == 1) - length -= ll + 6; - break; - - case RDFREC_DLL: /* DLL and Module records */ - case RDFREC_MODNAME: - ll = 0; - if (rdf_version == 1) { - do { - fread(&buf[ll], 1, 1, infile); - } while (buf[ll++]); - } else { - for (; ll < reclen; ll++) - fread(&buf[ll], 1, 1, infile); - } - if (t == 4) - printf(" dll: %s\n", buf); - else - printf(" module: %s\n", buf); - if (rdf_version == 1) - length -= ll + 1; - break; - - case RDFREC_BSS: /* BSS reservation */ - fread(&ll, 4, 1, infile); - printf(" bss reservation: %08lx bytes\n", translatelong(ll)); - if (rdf_version == 1) - length -= 5; - if (rdf_version > 1 && reclen != 4) - printf(" warning: reclen != 4\n"); - break; - - case RDFREC_COMMON: { - unsigned short seg, align; - unsigned long size; - - fread(&seg, 2, 1, infile); - fread(&size, 4, 1, infile); - fread(&align, 2, 1, infile); - for (ll = 0; ll < reclen - 8; ll++) - fread(buf + ll, 1, 1, infile); - printf(" common: segment %04x = %s, %ld:%d\n", translateshort(seg), - buf, translatelong(size), translateshort(align)); - break; - } - - default: - printf(" unrecognized record (type %d", (int) t); - if (rdf_version > 1) { - printf(", length %d", (int) reclen); - fseek(infile, reclen, SEEK_CUR); - } else - length--; - printf(")\n"); - } - if (rdf_version != 1) - length -= 2 + reclen; + fread(&t, 1, 1, infile); + if (rdf_version >= 2) { + fread(&reclen, 1, 1, infile); + } + switch (t) { + case RDFREC_GENERIC: /* generic record */ + printf(" generic record (length=%d)\n", (int)reclen); + fseek(infile, reclen, SEEK_CUR); + break; + + case RDFREC_RELOC: /* relocation record */ + case RDFREC_SEGRELOC: /* segment relocation */ + fread(&s, 1, 1, infile); + fread(&o, 4, 1, infile); + fread(&l, 1, 1, infile); + fread(&rs, 2, 1, infile); + printf(" %s: location (%04x:%08lx), length %d, " + "referred seg %04x\n", + t == 1 ? "relocation" : "seg relocation", (int)s, + translatelong(o), (int)l, translateshort(rs)); + if (rdf_version >= 2 && reclen != 8) + printf(" warning: reclen != 8\n"); + if (rdf_version == 1) + length -= 9; + if (rdf_version == 1 && t == 6) + printf + (" warning: seg relocation not supported in RDOFF1\n"); + break; + + case RDFREC_IMPORT: /* import record */ + case RDFREC_FARIMPORT: /* import far symbol */ + fread(&flags, 1, 1, infile); + fread(&rs, 2, 1, infile); + ll = 0; + + if (rdf_version == 1) { + do { + fread(&buf[ll], 1, 1, infile); + } while (buf[ll++]); + } else { + for (; ll < reclen - 3; ll++) + fread(&buf[ll], 1, 1, infile); + } + + if (t == 7) + printf("far "); + printf((flags & SYM_IMPORT) ? " import" : " extern"); + if (flags & SYM_FUNCTION) + printf(" proc"); + if (flags & SYM_DATA) + printf(" data"); + printf(": segment %04x = %s\n", translateshort(rs), buf); + if (rdf_version == 1) + length -= ll + 3; + if (rdf_version == 1 && t == 7) + printf + (" warning: far import not supported in RDOFF1\n"); + break; + + case RDFREC_GLOBAL: /* export record */ + fread(&flags, 1, 1, infile); + fread(&s, 1, 1, infile); + fread(&o, 4, 1, infile); + ll = 0; + + if (rdf_version == 1) { + do { + fread(&buf[ll], 1, 1, infile); + } while (buf[ll++]); + } else { + for (; ll < reclen - 6; ll++) + fread(&buf[ll], 1, 1, infile); + } + printf((flags & SYM_GLOBAL) ? " export" : " public"); + if (flags & SYM_FUNCTION) + printf(" proc"); + if (flags & SYM_DATA) + printf(" data"); + printf(": (%04x:%08lx) = %s\n", (int)s, translatelong(o), buf); + if (rdf_version == 1) + length -= ll + 6; + break; + + case RDFREC_DLL: /* DLL and Module records */ + case RDFREC_MODNAME: + ll = 0; + if (rdf_version == 1) { + do { + fread(&buf[ll], 1, 1, infile); + } while (buf[ll++]); + } else { + for (; ll < reclen; ll++) + fread(&buf[ll], 1, 1, infile); + } + if (t == 4) + printf(" dll: %s\n", buf); + else + printf(" module: %s\n", buf); + if (rdf_version == 1) + length -= ll + 1; + break; + + case RDFREC_BSS: /* BSS reservation */ + fread(&ll, 4, 1, infile); + printf(" bss reservation: %08lx bytes\n", translatelong(ll)); + if (rdf_version == 1) + length -= 5; + if (rdf_version > 1 && reclen != 4) + printf(" warning: reclen != 4\n"); + break; + + case RDFREC_COMMON:{ + unsigned short seg, align; + unsigned long size; + + fread(&seg, 2, 1, infile); + fread(&size, 4, 1, infile); + fread(&align, 2, 1, infile); + for (ll = 0; ll < reclen - 8; ll++) + fread(buf + ll, 1, 1, infile); + printf(" common: segment %04x = %s, %ld:%d\n", + translateshort(seg), buf, translatelong(size), + translateshort(align)); + break; + } + + default: + printf(" unrecognized record (type %d", (int)t); + if (rdf_version > 1) { + printf(", length %d", (int)reclen); + fseek(infile, reclen, SEEK_CUR); + } else + length--; + printf(")\n"); + } + if (rdf_version != 1) + length -= 2 + reclen; } } @@ -175,45 +179,46 @@ int main(int argc, char **argv) printf("RDOFF dump utility, version %s\n", PROGRAM_VERSION); printf("RDOFF2 revision %s\n", RDOFF2_REVISION); puts("Copyright (c) 1996,99 Julian R Hall\n" - "Improvements and fixes (c) 2002-2004 RET & COM Research."); + "Improvements and fixes (c) 2002-2004 RET & COM Research."); if (argc < 2) { - fputs("Usage: rdfdump [-v] <filename>\n", stderr); - exit(1); + 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++; + 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\n", argv[1]); - exit(1); + fprintf(stderr, "rdfdump: Could not open %s\n", 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); + 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] > '2') { - fprintf(stderr, "rdfdump: unknown RDOFF version '%c'\n", id[5]); - exit(1); + fprintf(stderr, "rdfdump: unknown RDOFF version '%c'\n", id[5]); + exit(1); } version = id[5] - '0'; if (version > 1) { - fread(&l, 4, 1, infile); - objectlength = translatelong(l); - printf("Object content size: %ld bytes\n", objectlength); + fread(&l, 4, 1, infile); + objectlength = translatelong(l); + printf("Object content size: %ld bytes\n", objectlength); } fread(&l, 4, 1, infile); @@ -222,81 +227,85 @@ int main(int argc, char **argv) print_header(headerlength, version); if (version == 1) { - 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"); - } + 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"); + } } else { - do { - fread(&s, 2, 1, infile); - s = translateshort(s); - if (!s) { - printf("\nNULL segment\n"); - foundnullsegment = 1; - break; - } - printf("\nSegment:\n Type = %04X (%s)\n", (int) s, translatesegmenttype(s)); - nsegments++; - - fread(&s, 2, 1, infile); - printf(" Number = %04X\n", (int) translateshort(s)); - fread(&s, 2, 1, infile); - printf(" Resrvd = %04X\n", (int) translateshort(s)); - fread(&l, 4, 1, infile); - l = translatelong(l); - printf(" Length = %ld bytes\n", l); - segmentcontentlength += 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"); - } while (!feof(infile)); - if (!foundnullsegment) - printf("\nWarning: unexpected end of file - " "NULL segment not found\n"); - - printf("\nTotal number of segments: %d\n", nsegments); - printf("Total segment content length: %ld bytes\n", segmentcontentlength); - - /* calculate what the total object content length should have been */ - l = segmentcontentlength + 10 * (nsegments + 1) + headerlength + 4; - if (l != objectlength) - printf("Warning: actual object length (%ld) != " "stored object length (%ld)\n", l, objectlength); + do { + fread(&s, 2, 1, infile); + s = translateshort(s); + if (!s) { + printf("\nNULL segment\n"); + foundnullsegment = 1; + break; + } + printf("\nSegment:\n Type = %04X (%s)\n", (int)s, + translatesegmenttype(s)); + nsegments++; + + fread(&s, 2, 1, infile); + printf(" Number = %04X\n", (int)translateshort(s)); + fread(&s, 2, 1, infile); + printf(" Resrvd = %04X\n", (int)translateshort(s)); + fread(&l, 4, 1, infile); + l = translatelong(l); + printf(" Length = %ld bytes\n", l); + segmentcontentlength += 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"); + } while (!feof(infile)); + if (!foundnullsegment) + printf("\nWarning: unexpected end of file - " + "NULL segment not found\n"); + + printf("\nTotal number of segments: %d\n", nsegments); + printf("Total segment content length: %ld bytes\n", + segmentcontentlength); + + /* calculate what the total object content length should have been */ + l = segmentcontentlength + 10 * (nsegments + 1) + headerlength + 4; + if (l != objectlength) + printf("Warning: actual object length (%ld) != " + "stored object length (%ld)\n", l, objectlength); } fclose(infile); return 0; diff --git a/rdoff/rdflib.c b/rdoff/rdflib.c index 41f92d2..cd5861c 100644 --- a/rdoff/rdflib.c +++ b/rdoff/rdflib.c @@ -39,29 +39,28 @@ * 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" - " x - extract (module-name filename)\n" - " r - replace (module-name filename)\n" - " d - delete (module-name)\n" - " t - list\n"; - +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" + " x - extract (module-name filename)\n" + " r - replace (module-name filename)\n" + " d - delete (module-name)\n" " t - list\n"; + /* Library signature */ const char *rdl_signature = "RDLIB2", *sig_modname = ".sig"; char **_argv; -#define _ENDIANNESS 0 /* 0 for little, 1 for big */ +#define _ENDIANNESS 0 /* 0 for little, 1 for big */ -static void longtolocal(long * l) +static void longtolocal(long *l) { #if _ENDIANNESS unsigned char t; - unsigned char * p = (unsigned char *) l; + unsigned char *p = (unsigned char *)l; t = p[0]; p[0] = p[3]; @@ -72,54 +71,47 @@ static void longtolocal(long * l) #endif } -char copybytes(FILE *fp, FILE *fp2, int n) +char copybytes(FILE * fp, FILE * fp2, int n) { int i, t = 0; - for (i = 0 ; i < n; i++ ) - { - t = fgetc(fp); - if (t == EOF) - { - fprintf(stderr,"rdflib: premature end of file in '%s'\n", - _argv[2]); - exit(1); - } - if (fp2) - if (fputc(t, fp2) == EOF) - { - fprintf(stderr,"rdflib: write error\n"); - exit(1); - } + for (i = 0; i < n; i++) { + t = fgetc(fp); + if (t == EOF) { + fprintf(stderr, "rdflib: premature end of file in '%s'\n", + _argv[2]); + exit(1); + } + if (fp2) + if (fputc(t, fp2) == EOF) { + fprintf(stderr, "rdflib: write error\n"); + exit(1); + } } - return (char) t; /* return last char read */ + return (char)t; /* return last char read */ } -long copylong(FILE *fp, FILE *fp2) +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,"rdflib: premature end of file in '%s'\n", - _argv[2]); - exit(1); - } - if (fp2) - if (fputc(t, fp2) == EOF) - { - fprintf(stderr,"rdflib: write error\n"); - exit(1); - } - *p++ = t; + 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, "rdflib: premature end of file in '%s'\n", + _argv[2]); + exit(1); + } + if (fp2) + if (fputc(t, fp2) == EOF) { + fprintf(stderr, "rdflib: write error\n"); + exit(1); + } + *p++ = t; } - longtolocal (&l); + longtolocal(&l); return l; } @@ -134,274 +126,265 @@ int main(int argc, char **argv) _argv = argv; - if (argc < 3 || !strncmp(argv[1],"-h",2) || !strncmp(argv[1],"--h",3)) - { - printf(usage); - exit(1); + 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,"rdflib: could not open '%s'\n",argv[2]); - perror("rdflib"); - exit(1); - } - fwrite(sig_modname, 1, strlen(sig_modname)+1, fp); - fwrite(rdl_signature, 1, strlen(rdl_signature), fp); - l = sizeof(t = time(NULL)); - fwrite(&l, sizeof(l), 1, fp); - fwrite(&t, 1, l, fp); - fclose(fp); - break; - - case 'a': /* add module */ - if (argc < 5) { - fprintf(stderr,"rdflib: required parameter missing\n"); - exit(1); - } - fp = fopen(argv[2],"ab"); - if (! fp) - { - fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]); - perror("rdflib"); - exit(1); - } - - fp2 = fopen(argv[3],"rb"); - if (! fp2) - { - fprintf(stderr,"rdflib: could not open '%s'\n",argv[3]); - perror("rdflib"); - exit(1); - } - - p = argv[4]; - do { - if ( fputc(*p,fp) == EOF ) { - fprintf(stderr,"rdflib: write error\n"); - exit(1); - } - } while (*p++); - - while (! feof (fp2) ) { - i = fgetc (fp2); - if (i == EOF) { - break; - } - - if ( fputc(i, fp) == EOF ) { - fprintf(stderr,"rdflib: write error\n"); - exit(1); - } - } - fclose(fp2); - fclose(fp); - break; + switch (argv[1][0]) { + case 'c': /* create library */ + fp = fopen(argv[2], "wb"); + if (!fp) { + fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]); + perror("rdflib"); + exit(1); + } + fwrite(sig_modname, 1, strlen(sig_modname) + 1, fp); + fwrite(rdl_signature, 1, strlen(rdl_signature), fp); + l = sizeof(t = time(NULL)); + fwrite(&l, sizeof(l), 1, fp); + fwrite(&t, 1, l, fp); + fclose(fp); + break; + + case 'a': /* add module */ + if (argc < 5) { + fprintf(stderr, "rdflib: required parameter missing\n"); + exit(1); + } + fp = fopen(argv[2], "ab"); + if (!fp) { + fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]); + perror("rdflib"); + exit(1); + } + + fp2 = fopen(argv[3], "rb"); + if (!fp2) { + fprintf(stderr, "rdflib: could not open '%s'\n", argv[3]); + perror("rdflib"); + exit(1); + } + + p = argv[4]; + do { + if (fputc(*p, fp) == EOF) { + fprintf(stderr, "rdflib: write error\n"); + exit(1); + } + } while (*p++); + + while (!feof(fp2)) { + i = fgetc(fp2); + if (i == EOF) { + break; + } + + if (fputc(i, fp) == EOF) { + fprintf(stderr, "rdflib: write error\n"); + exit(1); + } + } + fclose(fp2); + fclose(fp); + break; case 'x': - if (argc < 5) { - fprintf(stderr, "rdflib: required parameter missing\n"); - exit(1); - } + if (argc < 5) { + fprintf(stderr, "rdflib: required parameter missing\n"); + exit(1); + } case 't': - fp = fopen(argv[2],"rb"); - if (! fp) - { - fprintf(stderr,"rdflib: could not open '%s'\n",argv[2]); - perror("rdflib"); - exit(1); - } - - fp2 = NULL; - while (! feof(fp) ) { - /* read name */ - p = buf; - while( ( *(p++) = (char) fgetc(fp) ) ) - if (feof(fp)) break; - - if (feof(fp)) break; - - fp2 = NULL; - if (argv[1][0] == 'x') { - /* check against desired name */ - if (! strcmp(buf,argv[3]) ) - { - fp2 = fopen(argv[4],"wb"); - if (! fp2) - { - fprintf(stderr,"rdflib: could not open '%s'\n",argv[4]); - perror("rdflib"); - exit(1); - } - } - } - else - printf("%-40s ", buf); - - /* step over the RDOFF file, extracting type information for - * the listing, and copying it if fp2 != NULL */ - - if (buf[0] == '.') { - - if (argv[1][0] == 't') - for (i = 0; i < 6; i++) - printf("%c", copybytes(fp,fp2,1)); - else - copybytes(fp,fp2,6); - - l = copylong(fp,fp2); - - if (argv[1][0] == 't') printf(" %ld bytes content\n", l); - - copybytes(fp,fp2,l); - } - else if ((c=copybytes(fp,fp2,6)) >= '2') /* version 2 or above */ - { - l = copylong(fp,fp2); - - if (argv[1][0] == 't') - printf("RDOFF%c %ld bytes content\n", c, l); - copybytes(fp,fp2, l); /* entire object */ - } - else - { - if (argv[1][0] == 't') - printf("RDOFF1\n"); - /* - * version 1 object, so we don't have an object content - * length field. - */ - 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 if (argv[1][0] == 'x') - { - fprintf(stderr,"rdflib: module '%s' not found in '%s'\n", - argv[3],argv[2]); - exit(1); - } - break; - - case 'r': /* replace module */ - argc--; - case 'd': /* delete module */ - if (argc < 4) { - fprintf(stderr, "rdflib: required parameter missing\n"); - exit(1); - } - - fp = fopen(argv[2],"rb"); - if (! fp) - { - fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]); - perror("rdflib"); - exit(1); - } - - if (argv[1][0] == 'r') { - fp2 = fopen(argv[4],"rb"); - if (! fp2) - { - fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]); - perror("rdflib"); - exit(1); - } - } - - fptmp = tmpfile(); - if (! fptmp) - { - fprintf(stderr,"rdflib: could not open temporary file\n"); - perror("rdflib"); - exit(1); - } - - /* copy library into temporary file */ - fseek(fp, 0, SEEK_END); /* get file length */ - l = ftell(fp); - fseek(fp, 0, SEEK_SET); - copybytes(fp, fptmp, l); - rewind(fptmp); - freopen(argv[2], "wb", fp); - - while (! feof(fptmp) ) { - /* read name */ - p = buf; - while( ( *(p++) = (char) fgetc(fptmp) ) ) - if (feof(fptmp)) break; - - if (feof(fptmp)) break; - - /* check against desired name */ - if (! strcmp(buf, argv[3]) ) { - fread(p=rdbuf, 1, sizeof(rdbuf), fptmp); - l = *(long*)(p+6); - fseek(fptmp, l, SEEK_CUR); - break; - } else { - fwrite(buf, 1, strlen(buf)+1, fp); /* module name */ - if ((c=copybytes(fptmp, fp, 6)) >= '2') { - l = copylong(fptmp, fp); /* version 2 or above */ - copybytes(fptmp, fp, l); /* entire object */ - } - } - } - - if (argv[1][0] == 'r') { - /* copy new module into library */ - p = argv[3]; - do { - if ( fputc(*p, fp) == EOF ) { - fprintf(stderr, "rdflib: write error\n"); - exit(1); - } - } while (*p++); - - while (! feof (fp2) ) { - i = fgetc (fp2); - if (i == EOF) { - break; - } - if ( fputc(i, fp) == EOF ) { - fprintf(stderr, "rdflib: write error\n"); - exit(1); - } - } - fclose(fp2); - } - - /* copy rest of library if any */ - while (! feof (fptmp) ) { - i = fgetc (fptmp); - if (i == EOF) { - break; - } - - if ( fputc(i, fp) == EOF ) { - fprintf(stderr,"rdflib: write error\n"); - exit(1); - } - } - - fclose(fp); - fclose(fptmp); - break; + fp = fopen(argv[2], "rb"); + if (!fp) { + fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]); + perror("rdflib"); + exit(1); + } + + fp2 = NULL; + while (!feof(fp)) { + /* read name */ + p = buf; + while ((*(p++) = (char)fgetc(fp))) + if (feof(fp)) + break; + + if (feof(fp)) + break; + + fp2 = NULL; + if (argv[1][0] == 'x') { + /* check against desired name */ + if (!strcmp(buf, argv[3])) { + fp2 = fopen(argv[4], "wb"); + if (!fp2) { + fprintf(stderr, "rdflib: could not open '%s'\n", + argv[4]); + perror("rdflib"); + exit(1); + } + } + } else + printf("%-40s ", buf); + + /* step over the RDOFF file, extracting type information for + * the listing, and copying it if fp2 != NULL */ + + if (buf[0] == '.') { + + if (argv[1][0] == 't') + for (i = 0; i < 6; i++) + printf("%c", copybytes(fp, fp2, 1)); + else + copybytes(fp, fp2, 6); + + l = copylong(fp, fp2); + + if (argv[1][0] == 't') + printf(" %ld bytes content\n", l); + + copybytes(fp, fp2, l); + } else if ((c = copybytes(fp, fp2, 6)) >= '2') { /* version 2 or above */ + l = copylong(fp, fp2); + + if (argv[1][0] == 't') + printf("RDOFF%c %ld bytes content\n", c, l); + copybytes(fp, fp2, l); /* entire object */ + } else { + if (argv[1][0] == 't') + printf("RDOFF1\n"); + /* + * version 1 object, so we don't have an object content + * length field. + */ + 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 if (argv[1][0] == 'x') { + fprintf(stderr, "rdflib: module '%s' not found in '%s'\n", + argv[3], argv[2]); + exit(1); + } + break; + + case 'r': /* replace module */ + argc--; + case 'd': /* delete module */ + if (argc < 4) { + fprintf(stderr, "rdflib: required parameter missing\n"); + exit(1); + } + + fp = fopen(argv[2], "rb"); + if (!fp) { + fprintf(stderr, "rdflib: could not open '%s'\n", argv[2]); + perror("rdflib"); + exit(1); + } + + if (argv[1][0] == 'r') { + fp2 = fopen(argv[4], "rb"); + if (!fp2) { + fprintf(stderr, "rdflib: could not open '%s'\n", argv[4]); + perror("rdflib"); + exit(1); + } + } + + fptmp = tmpfile(); + if (!fptmp) { + fprintf(stderr, "rdflib: could not open temporary file\n"); + perror("rdflib"); + exit(1); + } + + /* copy library into temporary file */ + fseek(fp, 0, SEEK_END); /* get file length */ + l = ftell(fp); + fseek(fp, 0, SEEK_SET); + copybytes(fp, fptmp, l); + rewind(fptmp); + freopen(argv[2], "wb", fp); + + while (!feof(fptmp)) { + /* read name */ + p = buf; + while ((*(p++) = (char)fgetc(fptmp))) + if (feof(fptmp)) + break; + + if (feof(fptmp)) + break; + + /* check against desired name */ + if (!strcmp(buf, argv[3])) { + fread(p = rdbuf, 1, sizeof(rdbuf), fptmp); + l = *(long *)(p + 6); + fseek(fptmp, l, SEEK_CUR); + break; + } else { + fwrite(buf, 1, strlen(buf) + 1, fp); /* module name */ + if ((c = copybytes(fptmp, fp, 6)) >= '2') { + l = copylong(fptmp, fp); /* version 2 or above */ + copybytes(fptmp, fp, l); /* entire object */ + } + } + } + + if (argv[1][0] == 'r') { + /* copy new module into library */ + p = argv[3]; + do { + if (fputc(*p, fp) == EOF) { + fprintf(stderr, "rdflib: write error\n"); + exit(1); + } + } while (*p++); + + while (!feof(fp2)) { + i = fgetc(fp2); + if (i == EOF) { + break; + } + if (fputc(i, fp) == EOF) { + fprintf(stderr, "rdflib: write error\n"); + exit(1); + } + } + fclose(fp2); + } + + /* copy rest of library if any */ + while (!feof(fptmp)) { + i = fgetc(fptmp); + if (i == EOF) { + break; + } + + if (fputc(i, fp) == EOF) { + fprintf(stderr, "rdflib: write error\n"); + exit(1); + } + } + + fclose(fp); + fclose(fptmp); + break; default: - fprintf(stderr,"rdflib: command '%c' not recognized\n", - argv[1][0]); - exit(1); + fprintf(stderr, "rdflib: command '%c' not recognized\n", + argv[1][0]); + exit(1); } return 0; } diff --git a/rdoff/rdfload.c b/rdoff/rdfload.c index 2f8bf6b..e5da423 100644 --- a/rdoff/rdfload.c +++ b/rdoff/rdfload.c @@ -27,57 +27,58 @@ extern int rdf_errno; -rdfmodule * rdfload(const char *filename) +rdfmodule *rdfload(const char *filename) { - rdfmodule * f; - long bsslength = 0; - char * hdr; + rdfmodule *f; + long bsslength = 0; + char *hdr; rdfheaderrec *r; f = malloc(sizeof(rdfmodule)); if (f == NULL) { - rdf_errno = RDF_ERR_NOMEM; - return NULL; + rdf_errno = RDF_ERR_NOMEM; + return NULL; } f->symtab = symtabNew(); if (!f->symtab) { - free(f); - rdf_errno = RDF_ERR_NOMEM; - return NULL; + free(f); + rdf_errno = RDF_ERR_NOMEM; + return NULL; } /* open the file */ - if ( rdfopen( &(f->f), filename ) ) { - free(f); - return NULL; + if (rdfopen(&(f->f), filename)) { + free(f); + return NULL; } /* read in text and data segments, and header */ - - f->t = malloc (f->f.seg[0].length); - f->d = malloc (f->f.seg[1].length); /* BSS seg allocated later */ - hdr = malloc (f->f.header_len); - - if (! f->t || ! f->d || !hdr) { - rdf_errno = RDF_ERR_NOMEM; - rdfclose(&f->f); - if (f->t) free(f->t); - if (f->d) free(f->d); - free(f); - return NULL; + + f->t = malloc(f->f.seg[0].length); + f->d = malloc(f->f.seg[1].length); /* BSS seg allocated later */ + hdr = malloc(f->f.header_len); + + if (!f->t || !f->d || !hdr) { + rdf_errno = RDF_ERR_NOMEM; + 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; + 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); @@ -85,96 +86,100 @@ rdfmodule * rdfload(const char *filename) /* Allocate BSS segment; step through header and count BSS records */ while ((r = rdfgetheaderrec(&f->f))) { - if (r->type == 5) - bsslength += r->b.amount; + if (r->type == 5) + bsslength += r->b.amount; } - f->b = malloc ( bsslength ); + f->b = malloc(bsslength); if (bsslength && (!f->b)) { - free(f->t); - free(f->d); - free(f); - free(hdr); - rdf_errno = RDF_ERR_NOMEM; - return NULL; + free(f->t); + free(f->d); + free(f); + free(hdr); + rdf_errno = RDF_ERR_NOMEM; + return NULL; } - rdfheaderrewind (&f->f); + rdfheaderrewind(&f->f); f->textrel = (long)f->t; f->datarel = (long)f->d; - f->bssrel = (long)f->b; + 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); + 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 - continue; /* relocation not in a loaded segment */ - - /* 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: - *(uint16 *)(seg + r->r.offset) += (uint16) 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; - - case 6: /* segment relocation */ - fprintf(stderr, "%s: segment relocation not supported by this " - "loader\n", m->f.name); - return 1; - } - } + 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 + continue; /* relocation not in a loaded segment */ + + /* 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: + *(uint16 *) (seg + r->r.offset) += (uint16) 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; + + case 6: /* segment relocation */ + fprintf(stderr, "%s: segment relocation not supported by this " + "loader\n", m->f.name); + return 1; + } + } return 0; } diff --git a/rdoff/rdfload.h b/rdoff/rdfload.h index 3889a4e..34446b6 100644 --- a/rdoff/rdfload.h +++ b/rdoff/rdfload.h @@ -17,15 +17,15 @@ #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; + 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); +rdfmodule *rdfload(const char *filename); int rdf_relocate(rdfmodule * m); #endif diff --git a/rdoff/rdlar.c b/rdoff/rdlar.c index cdbd154..53c12c8 100644 --- a/rdoff/rdlar.c +++ b/rdoff/rdlar.c @@ -2,7 +2,7 @@ * rdlar.c - new librarian/archiver for RDOFF2. * Copyright (c) 2002 RET & COM Research. */ - + #include <stdio.h> #include <stdlib.h> #include <ctype.h> @@ -35,19 +35,19 @@ struct { bool odate; bool fresh; int verbose; -} options = { 0, 0, 0, 0, 0, 0 }; - -#define _ENDIANNESS 0 /* 0 for little, 1 for big */ +} options = { +0, 0, 0, 0, 0, 0}; +#define _ENDIANNESS 0 /* 0 for little, 1 for big */ /* * Convert long to little endian (if we were compiled on big-endian machine) */ -static void longtolocal(long * l) +static void longtolocal(long *l) { #if _ENDIANNESS unsigned char t; - unsigned char * p = (unsigned char *) l; + unsigned char *p = (unsigned char *)l; t = p[0]; p[0] = p[3]; @@ -65,112 +65,111 @@ void show_version(void) { puts("New RDOFF2 librarian/archiver, version " PROGRAM_VERSION "\n" "Copyright (c) 2002 RET & COM Research.\n" - "This program is free software and distributed under GPL (version 2 or later);\n" - "see http://www.gnu.org/copyleft/gpl.html for details."); + "This program is free software and distributed under GPL (version 2 or later);\n" + "see http://www.gnu.org/copyleft/gpl.html for details."); } - /* * Print usage instructions */ void usage(void) { printf("Usage: %s [-]{%s}[%s] libfile [module-name] [files]\n", - progname, commands, modifiers); + progname, commands, modifiers); puts(" commands:\n" " a - add module(s) to the library\n" - " d - delete module(s) from the library\n" - " n - create the library\n" - " r - replace module(s)\n" - " t - display contents of library\n" - " x - extract module(s)\n" - " command specific modifiers:\n" - " o - preserve original dates\n" - " u - only replace modules that are newer than library contents\n" - " generic modifiers:\n" - " c - do not warn if the library had to be created\n" - " f - use file name as a module name\n" - " v - be verbose\n" - " V - display version information"); + " d - delete module(s) from the library\n" + " n - create the library\n" + " r - replace module(s)\n" + " t - display contents of library\n" + " x - extract module(s)\n" + " command specific modifiers:\n" + " o - preserve original dates\n" + " u - only replace modules that are newer than library contents\n" + " generic modifiers:\n" + " c - do not warn if the library had to be created\n" + " f - use file name as a module name\n" + " v - be verbose\n" + " V - display version information"); } - /* * Print an error message and exit */ void error_exit(int errcode, bool useperror, const char *fmt, ...) { va_list ap; - + fprintf(stderr, "%s: ", progname); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); putc('\n', stderr); - if (useperror) perror(progname); + if (useperror) + perror(progname); exit(errcode); } - /* * Fill in and write a header */ -void put_header(struct rdlm_hdr *hdr, FILE *libfp, char *modname) +void put_header(struct rdlm_hdr *hdr, FILE * libfp, char *modname) { int n = 0; - + hdr->hdrsize = sizeof(*hdr); - if (modname) hdr->hdrsize += (n = strlen(modname) + 1); - if (libfp == NULL) return; + if (modname) + hdr->hdrsize += (n = strlen(modname) + 1); + if (libfp == NULL) + return; if (fwrite(hdr, 1, sizeof(*hdr), libfp) != sizeof(*hdr) || - (modname && (fwrite(modname, 1, n, libfp) != n))) - error_exit(3, TRUE, "could not write header"); + (modname && (fwrite(modname, 1, n, libfp) != n))) + error_exit(3, TRUE, "could not write header"); } /* * Copy n bytes from one file to another and return last character read. */ -char copybytes(FILE *fp, FILE *fp2, int n) +char copybytes(FILE * fp, FILE * fp2, int n) { int i, t = 0; - for (i = 0 ; i < n; i++) { - t = fgetc(fp); - if (t == EOF) - error_exit(1, FALSE, "premature end of file in '%s'", _argv[2]); - if (fp2) - if (fputc(t, fp2) == EOF) - error_exit(1, FALSE, "write error"); + for (i = 0; i < n; i++) { + t = fgetc(fp); + if (t == EOF) + error_exit(1, FALSE, "premature end of file in '%s'", + _argv[2]); + if (fp2) + if (fputc(t, fp2) == EOF) + error_exit(1, FALSE, "write error"); } - return (char) t; + return (char)t; } - /* * Copy unsigned long from one file to another. * Return local presentation of long. */ -long copylong(FILE *fp, FILE *fp2) +long copylong(FILE * fp, FILE * fp2) { long l; - int i,t; - unsigned char * p = (unsigned char *) &l; - - - for (i = 0 ; i < 4; i++ ) { - t = fgetc(fp); - if (t == EOF) - error_exit(1, FALSE, "premature end of file in '%s'", _argv[2]); - if (fp2) - if (fputc(t, fp2) == EOF) - error_exit(1, FALSE, "write error"); - *p++ = t; + int i, t; + unsigned char *p = (unsigned char *)&l; + + for (i = 0; i < 4; i++) { + t = fgetc(fp); + if (t == EOF) + error_exit(1, FALSE, "premature end of file in '%s'", + _argv[2]); + if (fp2) + if (fputc(t, fp2) == EOF) + error_exit(1, FALSE, "write error"); + *p++ = t; } - longtolocal (&l); + longtolocal(&l); return l; } - /* * Create a new library */ @@ -186,60 +185,59 @@ int create_library(char *libname) hdr.group = getgid(); hdr.mode = umask(022); hdr.size = 0; - + libfp = fopen(libname, "wb"); if (!libfp) - error_exit(1, TRUE, "could not open '%s'\n", libname); - + error_exit(1, TRUE, "could not open '%s'\n", libname); + /* Write library header */ put_header(&hdr, libfp, NULL); - + fclose(libfp); return TRUE; } - /* * Add a module to the library */ -int add_module(FILE *libfp, const char *fname, char *modname) +int add_module(FILE * libfp, const char *fname, char *modname) { FILE *modfp; struct rdlm_hdr hdr = { RDLMMAG, 0, 0, 0, 0, 0, 0 }; struct stat finfo; int i; - + if (options.verbose) - fprintf(stderr, "adding module %s\n", modname); + fprintf(stderr, "adding module %s\n", modname); /* Initialize some fields in the module header */ if (stat(fname, &finfo) < 0) - error_exit(1, TRUE, "could not stat '%s'", fname); + error_exit(1, TRUE, "could not stat '%s'", fname); hdr.date = finfo.st_mtime; hdr.owner = finfo.st_uid; hdr.group = finfo.st_gid; - hdr.size = finfo.st_size; + hdr.size = finfo.st_size; modfp = fopen(fname, "rb"); if (!modfp) - error_exit(1, TRUE, "could not open '%s'", fname); + error_exit(1, TRUE, "could not open '%s'", fname); /* Write module header */ put_header(&hdr, libfp, modname); - /* Put the module itself */ - while (! feof(modfp)) { - i = fgetc(modfp); - if (i == EOF) break; - if (fputc(i, libfp) == EOF ) - error_exit(1, FALSE, "write error"); + /* Put the module itself */ + while (!feof(modfp)) { + i = fgetc(modfp); + if (i == EOF) + break; + if (fputc(i, libfp) == EOF) + error_exit(1, FALSE, "write error"); } - + fclose(modfp); return TRUE; } - /* * Main */ @@ -254,208 +252,212 @@ int main(int argc, char *argv[]) progname = argv[0]; _argv = argv; - + if (argc < 2) { - usage(); - exit(1); + usage(); + exit(1); } /* Check whether some modifiers were specified */ for (i = 1; i < strlen(argv[1]); i++) { - switch (c = argv[1][i]) { - case 'c': - options.createok = TRUE; - break; - case 'f': - options.usefname = TRUE; - break; - case 'l': - options.align = TRUE; - break; - case 'o': - options.odate = TRUE; - break; - case 'u': - options.fresh = TRUE; - break; - case 'v': - options.verbose++; - break; - case 'V': - show_version(); - exit(0); - default: - if (strchr(commands, c) == NULL) - error_exit(2, FALSE, "invalid command or modifier '%c'", c); - } + switch (c = argv[1][i]) { + case 'c': + options.createok = TRUE; + break; + case 'f': + options.usefname = TRUE; + break; + case 'l': + options.align = TRUE; + break; + case 'o': + options.odate = TRUE; + break; + case 'u': + options.fresh = TRUE; + break; + case 'v': + options.verbose++; + break; + case 'V': + show_version(); + exit(0); + default: + if (strchr(commands, c) == NULL) + error_exit(2, FALSE, "invalid command or modifier '%c'", + c); + } } - + if (argc < 3) - error_exit(2, FALSE, "missing library name"); + error_exit(2, FALSE, "missing library name"); /* Process the command */ - if (argv[1][0] == '-') argv[1]++; - switch(c = argv[1][0]) { - case 'a': /* add a module */ - if (argc < 4 || (!options.usefname && argc != 5)) - error_exit(2, FALSE, "invalid number of arguments"); - - /* Check if a library already exists. If not - create it */ - if (access(argv[2], F_OK) < 0) { - if (!options.createok) - fprintf(stderr, "creating library %s\n", argv[2]); - create_library(argv[2]); - } - - libfp = fopen(argv[2], "ab"); - if (!libfp) - error_exit(1, TRUE, "could not open '%s'", argv[2]); - - if (!options.usefname) - add_module(libfp, argv[4], argv[3]); - else - for (i = 3; i < argc; i++) - add_module(libfp, argv[i], argv[i]); - - fclose(libfp); - break; - - case 'n': /* create library */ - create_library(argv[2]); - break; - - case 'x': /* extract module(s) */ - if (!options.usefname) - argc--; - if (argc < 4) - error_exit(2, FALSE, "required parameter missing"); - p = options.usefname ? argv[3] : argv[4]; - case 't': /* list library contents */ - libfp = fopen(argv[2],"rb"); - if (!libfp) - error_exit(1, TRUE, "could not open '%s'\n", argv[2]); - - /* Read library header */ - if (fread(&hdr, 1, sizeof(hdr), libfp) != sizeof(hdr) || - hdr.magic != RDLAMAG) - error_exit(1, FALSE, "invalid library format"); - - /* Walk through the library looking for requested module */ - while (!feof(libfp)) { - /* Read module header */ - i = fread(&hdr, 1, sizeof(hdr), libfp); - if (feof(libfp)) break; - if (i != sizeof(hdr) || hdr.magic != RDLMMAG) - error_exit(1, FALSE, "invalid module header"); - /* Read module name */ - i = hdr.hdrsize - sizeof(hdr); - if (i > sizeof(buf) || - fread(buf, 1, i, libfp) != i) - error_exit(1, FALSE, "invalid module name"); - if (c == 'x') { - /* Check against desired name */ - if (!strcmp(buf, argv[3])) { - if (options.verbose) - fprintf(stderr, "extracting module %s to file %s\n", - buf, p); - modfp = fopen(p, "wb"); - if (!modfp) - error_exit(1, TRUE, "could not open '%s'", p); - } - } else { - printf("%-40s ", buf); - if (options.verbose) { - printf("%ld bytes", hdr.size); - } - putchar('\n'); - } - - copybytes(libfp, modfp, hdr.size); - if (modfp) break; - } - - fclose(libfp); - if (modfp) - fclose(modfp); - else if (c == 'x') - error_exit(1, FALSE, "module '%s' not found in '%s'", - argv[3], argv[2]); - break; - - case 'r': /* replace module(s) */ - argc--; - if (stat(argv[4], &finfo) < 0) - error_exit(1, TRUE, "could not stat '%s'", argv[4]); - case 'd': /* delete module(s) */ - if (argc < 4) - error_exit(2, FALSE, "required parameter missing"); - - libfp = fopen(argv[2], "rb"); - if (!libfp) - error_exit(1, TRUE, "could not open '%s'", argv[2]); - - /* Copy the library into a temporary file */ - tmpfp = tmpfile(); - if (!tmpfp) - error_exit(1, TRUE, "could not open temporary file"); - - stat(argv[2], &finfo); - copybytes(libfp, tmpfp, finfo.st_size); - rewind(tmpfp); - freopen(argv[2], "wb", libfp); - - /* Read library header and write it to a new file */ - if (fread(&hdr, 1, sizeof(hdr), tmpfp) != sizeof(hdr) || - hdr.magic != RDLAMAG) - error_exit(1, FALSE, "invalid library format"); - put_header(&hdr, libfp, NULL); - - /* Walk through the library looking for requested module */ - while (!feof(tmpfp)) { - /* Read module header */ - if (fread(&hdr, 1, sizeof(hdr), tmpfp) != sizeof(hdr) || - hdr.magic != RDLMMAG) - error_exit(1, FALSE, "invalid module header"); - /* Read module name */ - i = hdr.hdrsize - sizeof(hdr); - if (i > sizeof(buf) || - fread(buf, 1, i, tmpfp) != i) - error_exit(1, FALSE, "invalid module name"); - /* Check against desired name */ - if (!strcmp(buf, argv[3]) && - (c == 'd' || !options.odate || finfo.st_mtime <= hdr.date)) { - if (options.verbose) - fprintf(stderr, "deleting module %s\n", buf); - fseek(tmpfp, hdr.size, SEEK_CUR); - break; - } else { - put_header(&hdr, libfp, buf); - copybytes(tmpfp, libfp, hdr.size); - } - } - - if (c == 'r') { - /* Copy new module into library */ - p = options.usefname ? argv[4] : argv[3]; - add_module(libfp, argv[4], p); - } - - /* Copy rest of library if any */ - while (!feof(tmpfp)) { - if ((i = fgetc(tmpfp)) == EOF) - break; - - if (fputc(i, libfp) == EOF) - error_exit(1, FALSE, "write error"); - } - - fclose(libfp); - fclose(tmpfp); - break; - - default: - error_exit(2, FALSE, "invalid command '%c'\n", c); + if (argv[1][0] == '-') + argv[1]++; + switch (c = argv[1][0]) { + case 'a': /* add a module */ + if (argc < 4 || (!options.usefname && argc != 5)) + error_exit(2, FALSE, "invalid number of arguments"); + + /* Check if a library already exists. If not - create it */ + if (access(argv[2], F_OK) < 0) { + if (!options.createok) + fprintf(stderr, "creating library %s\n", argv[2]); + create_library(argv[2]); + } + + libfp = fopen(argv[2], "ab"); + if (!libfp) + error_exit(1, TRUE, "could not open '%s'", argv[2]); + + if (!options.usefname) + add_module(libfp, argv[4], argv[3]); + else + for (i = 3; i < argc; i++) + add_module(libfp, argv[i], argv[i]); + + fclose(libfp); + break; + + case 'n': /* create library */ + create_library(argv[2]); + break; + + case 'x': /* extract module(s) */ + if (!options.usefname) + argc--; + if (argc < 4) + error_exit(2, FALSE, "required parameter missing"); + p = options.usefname ? argv[3] : argv[4]; + case 't': /* list library contents */ + libfp = fopen(argv[2], "rb"); + if (!libfp) + error_exit(1, TRUE, "could not open '%s'\n", argv[2]); + + /* Read library header */ + if (fread(&hdr, 1, sizeof(hdr), libfp) != sizeof(hdr) || + hdr.magic != RDLAMAG) + error_exit(1, FALSE, "invalid library format"); + + /* Walk through the library looking for requested module */ + while (!feof(libfp)) { + /* Read module header */ + i = fread(&hdr, 1, sizeof(hdr), libfp); + if (feof(libfp)) + break; + if (i != sizeof(hdr) || hdr.magic != RDLMMAG) + error_exit(1, FALSE, "invalid module header"); + /* Read module name */ + i = hdr.hdrsize - sizeof(hdr); + if (i > sizeof(buf) || fread(buf, 1, i, libfp) != i) + error_exit(1, FALSE, "invalid module name"); + if (c == 'x') { + /* Check against desired name */ + if (!strcmp(buf, argv[3])) { + if (options.verbose) + fprintf(stderr, + "extracting module %s to file %s\n", buf, + p); + modfp = fopen(p, "wb"); + if (!modfp) + error_exit(1, TRUE, "could not open '%s'", p); + } + } else { + printf("%-40s ", buf); + if (options.verbose) { + printf("%ld bytes", hdr.size); + } + putchar('\n'); + } + + copybytes(libfp, modfp, hdr.size); + if (modfp) + break; + } + + fclose(libfp); + if (modfp) + fclose(modfp); + else if (c == 'x') + error_exit(1, FALSE, "module '%s' not found in '%s'", + argv[3], argv[2]); + break; + + case 'r': /* replace module(s) */ + argc--; + if (stat(argv[4], &finfo) < 0) + error_exit(1, TRUE, "could not stat '%s'", argv[4]); + case 'd': /* delete module(s) */ + if (argc < 4) + error_exit(2, FALSE, "required parameter missing"); + + libfp = fopen(argv[2], "rb"); + if (!libfp) + error_exit(1, TRUE, "could not open '%s'", argv[2]); + + /* Copy the library into a temporary file */ + tmpfp = tmpfile(); + if (!tmpfp) + error_exit(1, TRUE, "could not open temporary file"); + + stat(argv[2], &finfo); + copybytes(libfp, tmpfp, finfo.st_size); + rewind(tmpfp); + freopen(argv[2], "wb", libfp); + + /* Read library header and write it to a new file */ + if (fread(&hdr, 1, sizeof(hdr), tmpfp) != sizeof(hdr) || + hdr.magic != RDLAMAG) + error_exit(1, FALSE, "invalid library format"); + put_header(&hdr, libfp, NULL); + + /* Walk through the library looking for requested module */ + while (!feof(tmpfp)) { + /* Read module header */ + if (fread(&hdr, 1, sizeof(hdr), tmpfp) != sizeof(hdr) || + hdr.magic != RDLMMAG) + error_exit(1, FALSE, "invalid module header"); + /* Read module name */ + i = hdr.hdrsize - sizeof(hdr); + if (i > sizeof(buf) || fread(buf, 1, i, tmpfp) != i) + error_exit(1, FALSE, "invalid module name"); + /* Check against desired name */ + if (!strcmp(buf, argv[3]) && + (c == 'd' || !options.odate + || finfo.st_mtime <= hdr.date)) { + if (options.verbose) + fprintf(stderr, "deleting module %s\n", buf); + fseek(tmpfp, hdr.size, SEEK_CUR); + break; + } else { + put_header(&hdr, libfp, buf); + copybytes(tmpfp, libfp, hdr.size); + } + } + + if (c == 'r') { + /* Copy new module into library */ + p = options.usefname ? argv[4] : argv[3]; + add_module(libfp, argv[4], p); + } + + /* Copy rest of library if any */ + while (!feof(tmpfp)) { + if ((i = fgetc(tmpfp)) == EOF) + break; + + if (fputc(i, libfp) == EOF) + error_exit(1, FALSE, "write error"); + } + + fclose(libfp); + fclose(tmpfp); + break; + + default: + error_exit(2, FALSE, "invalid command '%c'\n", c); } - + return 0; } diff --git a/rdoff/rdlar.h b/rdoff/rdlar.h index 0a93888..bf9e84f 100644 --- a/rdoff/rdlar.h +++ b/rdoff/rdlar.h @@ -11,19 +11,19 @@ #define getgid() 0 #endif -#define RDLAMAG 0x414C4452 /* Archive magic */ -#define RDLMMAG 0x4D4C4452 /* Member magic */ +#define RDLAMAG 0x414C4452 /* Archive magic */ +#define RDLMMAG 0x4D4C4452 /* Member magic */ -#define MAXMODNAMELEN 256 /* Maximum length of module name */ +#define MAXMODNAMELEN 256 /* Maximum length of module name */ struct rdlm_hdr { - unsigned long magic; /* Must be RDLAMAG */ - unsigned long hdrsize; /* Header size + sizeof(module_name) */ - unsigned long date; /* Creation date */ - unsigned long owner; /* UID */ - unsigned long group; /* GID */ - unsigned long mode; /* File mode */ - unsigned long size; /* File size */ + unsigned long magic; /* Must be RDLAMAG */ + unsigned long hdrsize; /* Header size + sizeof(module_name) */ + unsigned long date; /* Creation date */ + unsigned long owner; /* UID */ + unsigned long group; /* GID */ + unsigned long mode; /* File mode */ + unsigned long size; /* File size */ /* NULL-terminated module name immediately follows */ }; diff --git a/rdoff/rdlib.c b/rdoff/rdlib.c index 97f682f..28a2bdf 100644 --- a/rdoff/rdlib.c +++ b/rdoff/rdlib.c @@ -1,7 +1,7 @@ /* * rdlib.c - routines for manipulating RDOFF libraries (.rdl) */ - + #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -17,68 +17,69 @@ int rdl_error = 0; char *rdl_errors[5] = { - "no error","could not open file", "invalid file structure", + "no error", "could not open file", "invalid file structure", "file contains modules of an unsupported RDOFF version", "module not found" }; -int rdl_verify(const char * filename) +int rdl_verify(const char *filename) { - FILE * fp = fopen(filename, "rb"); - char buf[257]; - int i; - long length; + FILE *fp = fopen(filename, "rb"); + char buf[257]; + int i; + long length; static char lastverified[256]; static int lastresult = -1; if (lastresult != -1 && !strcmp(filename, lastverified)) - return lastresult; + return lastresult; strcpy(lastverified, filename); if (!fp) - return (rdl_error = lastresult = 1); - - while (!feof(fp)) - { - i = 0; - - while (fread(buf + i,1,1,fp) == 1 && buf[i] && i < 257) - i++; - if (feof(fp)) break; - - if (buf[0] == '.') { - /* - * A special module, eg a signature block or a directory. - * Format of such a module is defined to be: - * six char type identifier - * long count bytes content - * content - * so we can handle it uniformaly with RDOFF2 modules. - */ - fread(buf, 6, 1, fp); - buf[6] = 0; - /* Currently, nothing useful to do with signature block.. */ - } else { - fread(buf, 6, 1, fp); - buf[6] = 0; - if (strncmp(buf, "RDOFF", 5)) { - return rdl_error = lastresult = 2; - } else if (buf[5] != '2') { - return rdl_error = lastresult = 3; - } - } - fread(&length, 4, 1, fp); - fseek(fp, length, SEEK_CUR); /* skip over the module */ + return (rdl_error = lastresult = 1); + + while (!feof(fp)) { + i = 0; + + while (fread(buf + i, 1, 1, fp) == 1 && buf[i] && i < 257) + i++; + if (feof(fp)) + break; + + if (buf[0] == '.') { + /* + * A special module, eg a signature block or a directory. + * Format of such a module is defined to be: + * six char type identifier + * long count bytes content + * content + * so we can handle it uniformaly with RDOFF2 modules. + */ + fread(buf, 6, 1, fp); + buf[6] = 0; + /* Currently, nothing useful to do with signature block.. */ + } else { + fread(buf, 6, 1, fp); + buf[6] = 0; + if (strncmp(buf, "RDOFF", 5)) { + return rdl_error = lastresult = 2; + } else if (buf[5] != '2') { + return rdl_error = lastresult = 3; + } + } + fread(&length, 4, 1, fp); + fseek(fp, length, SEEK_CUR); /* skip over the module */ } fclose(fp); - return lastresult = 0; /* library in correct format */ + return lastresult = 0; /* library in correct format */ } -int rdl_open (struct librarynode * lib, const char * name) +int rdl_open(struct librarynode *lib, const char *name) { int i = rdl_verify(name); - if (i) return i; + if (i) + return i; lib->fp = NULL; lib->name = strdup(name); @@ -87,182 +88,174 @@ int rdl_open (struct librarynode * lib, const char * name) return 0; } -void rdl_close (struct librarynode * lib) +void rdl_close(struct librarynode *lib) { if (lib->fp) - fclose(lib->fp); + fclose(lib->fp); free(lib->name); } -int rdl_searchlib (struct librarynode * lib, - const char * label, rdffile * f) +int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f) { - char buf[512]; - int i, t; - void * hdr; - rdfheaderrec * r; - long l; + char buf[512]; + int i, t; + void *hdr; + rdfheaderrec *r; + long l; 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) ) - { - /* - * read the module name from the file, and prepend - * the library name and '.' to it. - */ - strcpy(buf, lib->name); - - i = strlen(lib->name); - buf[i++] = '.'; t = i; - while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 512) - i++; - - buf[i] = 0; - - if (feof(lib->fp)) break; - if (!strcmp(buf + t, ".dir")) /* skip over directory */ - { - fread (&l, 4, 1, lib->fp); - fseek (lib->fp, l, SEEK_CUR); - continue; - } - /* - * open the RDOFF module - */ - if ( rdfopenhere(f,lib->fp,&lib->referenced,buf) ) { - rdl_error = 16 * rdf_errno; - return 0; - } - /* - * read in the header, and scan for exported symbols - */ - 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->eof_offset; - rdfclose(f); - fseek(lib->fp,i,SEEK_SET); + 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)) { + /* + * read the module name from the file, and prepend + * the library name and '.' to it. + */ + strcpy(buf, lib->name); + + i = strlen(lib->name); + buf[i++] = '.'; + t = i; + while (fread(buf + i, 1, 1, lib->fp) == 1 && buf[i] && i < 512) + i++; + + buf[i] = 0; + + if (feof(lib->fp)) + break; + if (!strcmp(buf + t, ".dir")) { /* skip over directory */ + fread(&l, 4, 1, lib->fp); + fseek(lib->fp, l, SEEK_CUR); + continue; + } + /* + * open the RDOFF module + */ + if (rdfopenhere(f, lib->fp, &lib->referenced, buf)) { + rdl_error = 16 * rdf_errno; + return 0; + } + /* + * read in the header, and scan for exported symbols + */ + 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->eof_offset; + rdfclose(f); + fseek(lib->fp, i, SEEK_SET); } /* * close the file if nobody else is using it */ - lib->referenced --; - if (! lib->referenced) - { - fclose(lib->fp); - lib->fp = NULL; + lib->referenced--; + if (!lib->referenced) { + fclose(lib->fp); + lib->fp = NULL; } return 0; } -int rdl_openmodule (struct librarynode * lib, int moduleno, rdffile * f) +int rdl_openmodule(struct librarynode *lib, int moduleno, rdffile * f) { - char buf[512]; - int i, cmod, t; - long length; + char buf[512]; + int i, cmod, t; + long length; lib->referenced++; - if (!lib->fp) - { - lib->fp = fopen(lib->name, "rb"); - if (!lib->fp) { - lib->referenced--; - return (rdl_error = 1); - } - } - else - rewind(lib->fp); + if (!lib->fp) { + lib->fp = fopen(lib->name, "rb"); + if (!lib->fp) { + lib->referenced--; + return (rdl_error = 1); + } + } else + rewind(lib->fp); cmod = -1; - while (!feof(lib->fp)) - { - strcpy(buf, lib->name); - i = strlen(buf); - buf[i++] = '.'; t = i; - while (fread(buf + i,1,1,lib->fp) == 1 && buf[i] && i < 512) - i++; - buf[i] = 0; - if (feof(lib->fp)) break; - - if (buf[t] != '.') /* special module - not counted in the numbering */ - cmod++; /* of RDOFF modules - must be referred to by name */ - - if (cmod == moduleno) { - rdl_error = 16 * - rdfopenhere(f, lib->fp, &lib->referenced, buf); - lib->referenced--; - if (!lib->referenced) { - fclose(lib->fp); - lib->fp = NULL; - } - return rdl_error; - } - - fread(buf, 6, 1, lib->fp); - buf[6] = 0; - if (buf[t] == '.') { - /* do nothing */ - } - else if (strncmp(buf, "RDOFF", 5)) { - if (! --lib->referenced) { - fclose(lib->fp); - lib->fp = NULL; - } - return rdl_error = 2; - } - else if (buf[5] != '2') { - if (! --lib->referenced) { - fclose(lib->fp); - lib->fp = NULL; - } - return rdl_error = 3; - } - - fread(&length, 4, 1, lib->fp); - fseek(lib->fp, length, SEEK_CUR); /* skip over the module */ + while (!feof(lib->fp)) { + strcpy(buf, lib->name); + i = strlen(buf); + buf[i++] = '.'; + t = i; + while (fread(buf + i, 1, 1, lib->fp) == 1 && buf[i] && i < 512) + i++; + buf[i] = 0; + if (feof(lib->fp)) + break; + + if (buf[t] != '.') /* special module - not counted in the numbering */ + cmod++; /* of RDOFF modules - must be referred to by name */ + + if (cmod == moduleno) { + rdl_error = 16 * + rdfopenhere(f, lib->fp, &lib->referenced, buf); + lib->referenced--; + if (!lib->referenced) { + fclose(lib->fp); + lib->fp = NULL; + } + return rdl_error; + } + + fread(buf, 6, 1, lib->fp); + buf[6] = 0; + if (buf[t] == '.') { + /* do nothing */ + } else if (strncmp(buf, "RDOFF", 5)) { + if (!--lib->referenced) { + fclose(lib->fp); + lib->fp = NULL; + } + return rdl_error = 2; + } else if (buf[5] != '2') { + if (!--lib->referenced) { + fclose(lib->fp); + lib->fp = NULL; + } + return rdl_error = 3; + } + + fread(&length, 4, 1, lib->fp); + fseek(lib->fp, length, SEEK_CUR); /* skip over the module */ } - if (! --lib->referenced) { - fclose(lib->fp); - lib->fp = NULL; + if (!--lib->referenced) { + fclose(lib->fp); + lib->fp = NULL; } - return rdl_error = 4; /* module not found */ + return rdl_error = 4; /* module not found */ } void rdl_perror(const char *apname, const char *filename) { if (rdl_error >= 16) - rdfperror(apname, filename); + rdfperror(apname, filename); else - fprintf(stderr,"%s:%s:%s\n",apname,filename,rdl_errors[rdl_error]); + fprintf(stderr, "%s:%s:%s\n", apname, filename, + rdl_errors[rdl_error]); } diff --git a/rdoff/rdlib.h b/rdoff/rdlib.h index 6255024..36438bd 100644 --- a/rdoff/rdlib.h +++ b/rdoff/rdlib.h @@ -2,15 +2,13 @@ * rdlib.h Functions for manipulating libraries 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. */ + 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; #define RDL_EOPEN 1 @@ -18,10 +16,9 @@ extern int rdl_error; #define RDL_EVERSION 3 #define RDL_ENOTFOUND 4 -int rdl_verify (const char * filename); -int rdl_open (struct librarynode * lib, const char * filename); -int rdl_searchlib (struct librarynode * lib, - const char * label, rdffile * f); -int rdl_openmodule (struct librarynode * lib, int module, rdffile * f); +int rdl_verify(const char *filename); +int rdl_open(struct librarynode *lib, const char *filename); +int rdl_searchlib(struct librarynode *lib, const char *label, rdffile * f); +int rdl_openmodule(struct librarynode *lib, int module, rdffile * f); void rdl_perror(const char *apname, const char *filename); diff --git a/rdoff/rdoff.c b/rdoff/rdoff.c index bfdb9cc..8f19702 100644 --- a/rdoff/rdoff.c +++ b/rdoff/rdoff.c @@ -41,87 +41,89 @@ * how long it is). * ======================================================================== */ - -memorybuffer * newmembuf() +memorybuffer *newmembuf() { - memorybuffer * t; + memorybuffer *t; t = malloc(sizeof(memorybuffer)); - if (!t) return NULL; + if (!t) + return NULL; t->length = 0; t->next = NULL; return t; } -void membufwrite(memorybuffer *const b, void *data, int bytes) +void membufwrite(memorybuffer * const b, void *data, int bytes) { uint16 w; long l; - if (b->next) { /* memory buffer full - use next buffer */ - membufwrite(b->next,data,bytes); - return; + 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); - return; + || (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); + return; } - 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; + 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 = * (uint16 *) data ; - b->buffer[b->length++] = w & 0xFF; - w >>= 8 ; - b->buffer[b->length++] = w & 0xFF; - break; + w = *(uint16 *) 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; + while (bytes--) { + b->buffer[b->length++] = *(*(unsigned char **)&data); + + (*(unsigned char **)&data)++; + } + break; } } -void membufdump(memorybuffer *b,FILE *fp) +void membufdump(memorybuffer * b, FILE * fp) { - if (!b) return; + if (!b) + return; + + fwrite(b->buffer, 1, b->length, fp); - fwrite (b->buffer, 1, b->length, fp); - - membufdump(b->next,fp); + membufdump(b->next, fp); } -int membuflength(memorybuffer *b) +int membuflength(memorybuffer * b) { - if (!b) return 0; + if (!b) + return 0; return b->length + membuflength(b->next); } -void freemembuf(memorybuffer *b) +void freemembuf(memorybuffer * b) { - if (!b) return; + if (!b) + return; freemembuf(b->next); free(b); } @@ -135,7 +137,7 @@ void freemembuf(memorybuffer *b) * * translate from little endian to local representation */ -long translatelong(long in) +long translatelong(long in) { long r; unsigned char *i; @@ -149,11 +151,11 @@ long translatelong(long in) return r; } -uint16 translateshort(uint16 in) +uint16 translateshort(uint16 in) { uint16 r; - unsigned char * i; - + unsigned char *i; + i = (unsigned char *)∈ r = (i[1] << 8) + i[0]; @@ -171,17 +173,17 @@ static char *knownsegtypes[8] = { char *translatesegmenttype(uint16 type) { if (type < 8) - return knownsegtypes[type]; + return knownsegtypes[type]; if (type < 0x0020) - return "reserved"; + return "reserved"; if (type < 0x1000) - return "reserved - Moscow"; + return "reserved - Moscow"; if (type < 0x8000) - return "reserved - system dependant"; + return "reserved - system dependant"; if (type < 0xFFFF) - return "reserved - other"; + return "reserved - other"; if (type == 0xFFFF) - return "invalid type code"; + return "invalid type code"; return "type code out of range"; } @@ -190,17 +192,17 @@ const char *RDOFFId = RDOFF2_SIGNATURE; /* Error messages. Must correspond to the codes defined in rdoff.h */ const char *rdf_errors[11] = { - /* 0 */ "no error occurred", - /* 1 */ "could not open file", - /* 2 */ "invalid file format", - /* 3 */ "error reading file", - /* 4 */ "unknown error", - /* 5 */ "header not read", - /* 6 */ "out of memory", - /* 7 */ "RDOFF v1 not supported", - /* 8 */ "unknown extended header record", - /* 9 */ "header record of known type but unknown length", - /* 10 */ "no such segment" + /* 0 */ "no error occurred", + /* 1 */ "could not open file", + /* 2 */ "invalid file format", + /* 3 */ "error reading file", + /* 4 */ "unknown error", + /* 5 */ "header not read", + /* 6 */ "out of memory", + /* 7 */ "RDOFF v1 not supported", + /* 8 */ "unknown extended header record", + /* 9 */ "header record of known type but unknown length", + /* 10 */ "no such segment" }; int rdf_errno = 0; @@ -209,18 +211,18 @@ int rdf_errno = 0; The library functions ======================================================================== */ -int rdfopen(rdffile *f, const char *name) +int rdfopen(rdffile * f, const char *name) { - FILE * fp; + FILE *fp; - fp = fopen(name,"rb"); + fp = fopen(name, "rb"); if (!fp) - return rdf_errno = RDF_ERR_OPEN; + return rdf_errno = RDF_ERR_OPEN; - return rdfopenhere(f,fp,NULL,name); + return rdfopenhere(f, fp, NULL, name); } -int rdfopenhere(rdffile *f, FILE *fp, int *refcount, const char *name) +int rdfopenhere(rdffile * f, FILE * fp, int *refcount, const char *name) { char buf[8]; long initpos; @@ -229,84 +231,87 @@ int rdfopenhere(rdffile *f, FILE *fp, int *refcount, const char *name) 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)); + 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 */ + fread(buf, 6, 1, f->fp); /* read header */ buf[6] = 0; - if (strcmp(buf,RDOFFId)) { - fclose(f->fp); - if (!strcmp(buf,"RDOFF1")) - return rdf_errno = RDF_ERR_VER; - return rdf_errno = RDF_ERR_FORMAT; + if (strcmp(buf, RDOFFId)) { + fclose(f->fp); + if (!strcmp(buf, "RDOFF1")) + return rdf_errno = RDF_ERR_VER; + return rdf_errno = RDF_ERR_FORMAT; } - if (fread(&l,1,4,f->fp) != 4 || fread(&f->header_len,1,4,f->fp) != 4) { - fclose(f->fp); - return rdf_errno = RDF_ERR_READ; + if (fread(&l, 1, 4, f->fp) != 4 + || fread(&f->header_len, 1, 4, f->fp) != 4) { + fclose(f->fp); + return rdf_errno = RDF_ERR_READ; } f->header_ofs = ftell(f->fp); f->eof_offset = f->header_ofs + translatelong(l) - 4; - if (fseek(f->fp,f->header_len,SEEK_CUR)) { - fclose(f->fp); - return rdf_errno = RDF_ERR_FORMAT; /* seek past end of file...? */ + if (fseek(f->fp, f->header_len, SEEK_CUR)) { + fclose(f->fp); + return rdf_errno = RDF_ERR_FORMAT; /* seek past end of file...? */ } - if (fread(&s,1,2,f->fp) != 2) { - fclose(f->fp); - return rdf_errno = RDF_ERR_READ; + if (fread(&s, 1, 2, f->fp) != 2) { + fclose(f->fp); + return rdf_errno = RDF_ERR_READ; } f->nsegs = 0; while (s != 0) { - f->seg[f->nsegs].type = s; - if (fread(&f->seg[f->nsegs].number,1,2,f->fp) != 2 || - fread(&f->seg[f->nsegs].reserved,1,2,f->fp) != 2 || - fread(&f->seg[f->nsegs].length,1,4,f->fp) != 4) { - fclose(f->fp); - return rdf_errno = RDF_ERR_READ; - } - - f->seg[f->nsegs].offset = ftell(f->fp); - if (fseek(f->fp,f->seg[f->nsegs].length,SEEK_CUR)) { - fclose(f->fp); - return rdf_errno = RDF_ERR_FORMAT; - } - f->nsegs++; - - if (fread(&s,1,2,f->fp) != 2) { - fclose(f->fp); - return rdf_errno = RDF_ERR_READ; - } + f->seg[f->nsegs].type = s; + if (fread(&f->seg[f->nsegs].number, 1, 2, f->fp) != 2 || + fread(&f->seg[f->nsegs].reserved, 1, 2, f->fp) != 2 || + fread(&f->seg[f->nsegs].length, 1, 4, f->fp) != 4) { + fclose(f->fp); + return rdf_errno = RDF_ERR_READ; + } + + f->seg[f->nsegs].offset = ftell(f->fp); + if (fseek(f->fp, f->seg[f->nsegs].length, SEEK_CUR)) { + fclose(f->fp); + return rdf_errno = RDF_ERR_FORMAT; + } + f->nsegs++; + + if (fread(&s, 1, 2, f->fp) != 2) { + fclose(f->fp); + return rdf_errno = RDF_ERR_READ; + } } - if (f->eof_offset != ftell(f->fp) + 8) { /* +8 = skip null segment header */ - fprintf(stderr, "warning: eof_offset [%ld] and actual eof offset " - "[%ld] don't match\n", f->eof_offset, ftell(f->fp) + 8); + if (f->eof_offset != ftell(f->fp) + 8) { /* +8 = skip null segment header */ + fprintf(stderr, "warning: eof_offset [%ld] and actual eof offset " + "[%ld] don't match\n", f->eof_offset, ftell(f->fp) + 8); } - fseek(f->fp,initpos,SEEK_SET); + fseek(f->fp, initpos, SEEK_SET); f->header_loc = NULL; f->name = newstr(name); f->refcount = refcount; - if (refcount) (*refcount)++; + if (refcount) + (*refcount)++; return RDF_OK; } -int rdfclose(rdffile *f) +int rdfclose(rdffile * f) { - if (! f->refcount || ! --(*f->refcount)) { - fclose(f->fp); - f->fp = NULL; + if (!f->refcount || !--(*f->refcount)) { + fclose(f->fp); + f->fp = NULL; } free(f->name); @@ -318,9 +323,9 @@ int rdfclose(rdffile *f) */ void rdfperror(const char *app, const char *name) { - fprintf(stderr,"%s:%s: %s\n",app,name,rdf_errors[rdf_errno]); + fprintf(stderr, "%s:%s: %s\n", app, name, rdf_errors[rdf_errno]); if (rdf_errno == RDF_ERR_OPEN || rdf_errno == RDF_ERR_READ) { - perror(app); + perror(app); } } @@ -332,39 +337,40 @@ int rdffindsegment(rdffile * f, int segno) { int i; for (i = 0; i < f->nsegs; i++) - if (f->seg[i].number == segno) return i; + if (f->seg[i].number == segno) + return i; return -1; } /* * Load the segment. Returns status. */ -int rdfloadseg(rdffile *f,int segment,void *buffer) +int rdfloadseg(rdffile * f, int segment, void *buffer) { long fpos, slen; - switch(segment) { - case RDOFF_HEADER: - fpos = f->header_ofs; - slen = f->header_len; - f->header_loc = (byte *)buffer; - f->header_fp = 0; - break; - default: - if (segment < f->nsegs) { - fpos = f->seg[segment].offset; - slen = f->seg[segment].length; - f->seg[segment].data = (byte *)buffer; - } else { - return rdf_errno = RDF_ERR_SEGMENT; - } + switch (segment) { + case RDOFF_HEADER: + fpos = f->header_ofs; + slen = f->header_len; + f->header_loc = (byte *) buffer; + f->header_fp = 0; + break; + default: + if (segment < f->nsegs) { + fpos = f->seg[segment].offset; + slen = f->seg[segment].length; + f->seg[segment].data = (byte *) buffer; + } else { + return rdf_errno = RDF_ERR_SEGMENT; + } } - if (fseek(f->fp,fpos,SEEK_SET)) - return rdf_errno = RDF_ERR_UNKNOWN; - - if (fread(buffer,1,slen,f->fp) != slen) - return rdf_errno = RDF_ERR_READ; + if (fseek(f->fp, fpos, SEEK_SET)) + return rdf_errno = RDF_ERR_UNKNOWN; + + if (fread(buffer, 1, slen, f->fp) != slen) + return rdf_errno = RDF_ERR_READ; return RDF_OK; } @@ -389,78 +395,79 @@ int rdfloadseg(rdffile *f,int segment,void *buffer) * Read a header record. * Returns the address of record, or NULL in case of error. */ -rdfheaderrec *rdfgetheaderrec(rdffile *f) +rdfheaderrec *rdfgetheaderrec(rdffile * f) { static rdfheaderrec r; int i; if (!f->header_loc) { - rdf_errno = RDF_ERR_HEADER; - return NULL; + rdf_errno = RDF_ERR_HEADER; + return NULL; } - if (f->header_fp >= f->header_len) return 0; + if (f->header_fp >= f->header_len) + return 0; RI8(r.type); RI8(r.g.reclen); - switch(r.type) { - case RDFREC_RELOC: /* Relocation record */ - case RDFREC_SEGRELOC: - if (r.r.reclen != 8) { - rdf_errno = RDF_ERR_RECLEN; - return NULL; - } - RI8(r.r.segment); - RI32(r.r.offset); - RI8(r.r.length); - RI16(r.r.refseg); - break; - - case RDFREC_IMPORT: /* Imported symbol record */ - case RDFREC_FARIMPORT: - RI8(r.i.flags); - RI16(r.i.segment); - RS(r.i.label, EXIM_LABEL_MAX); - break; - - case RDFREC_GLOBAL: /* Exported symbol record */ - RI8(r.e.flags); - RI8(r.e.segment); - RI32(r.e.offset); - RS(r.e.label, EXIM_LABEL_MAX); - break; - - case RDFREC_DLL: /* DLL record */ - RS(r.d.libname, MODLIB_NAME_MAX); - break; - - case RDFREC_BSS: /* BSS reservation record */ - if (r.r.reclen != 4) { - rdf_errno = RDF_ERR_RECLEN; - return NULL; - } - RI32(r.b.amount); - break; - - case RDFREC_MODNAME: /* Module name record */ - RS(r.m.modname, MODLIB_NAME_MAX); - break; - - case RDFREC_COMMON: /* Common variable */ - RI16(r.c.segment); - RI32(r.c.size); - RI16(r.c.align); - RS(r.c.label, EXIM_LABEL_MAX); - break; - - default: + switch (r.type) { + case RDFREC_RELOC: /* Relocation record */ + case RDFREC_SEGRELOC: + if (r.r.reclen != 8) { + rdf_errno = RDF_ERR_RECLEN; + return NULL; + } + RI8(r.r.segment); + RI32(r.r.offset); + RI8(r.r.length); + RI16(r.r.refseg); + break; + + case RDFREC_IMPORT: /* Imported symbol record */ + case RDFREC_FARIMPORT: + RI8(r.i.flags); + RI16(r.i.segment); + RS(r.i.label, EXIM_LABEL_MAX); + break; + + case RDFREC_GLOBAL: /* Exported symbol record */ + RI8(r.e.flags); + RI8(r.e.segment); + RI32(r.e.offset); + RS(r.e.label, EXIM_LABEL_MAX); + break; + + case RDFREC_DLL: /* DLL record */ + RS(r.d.libname, MODLIB_NAME_MAX); + break; + + case RDFREC_BSS: /* BSS reservation record */ + if (r.r.reclen != 4) { + rdf_errno = RDF_ERR_RECLEN; + return NULL; + } + RI32(r.b.amount); + break; + + case RDFREC_MODNAME: /* Module name record */ + RS(r.m.modname, MODLIB_NAME_MAX); + break; + + case RDFREC_COMMON: /* Common variable */ + RI16(r.c.segment); + RI32(r.c.size); + RI16(r.c.align); + RS(r.c.label, EXIM_LABEL_MAX); + break; + + default: #ifdef STRICT_ERRORS - rdf_errno = RDF_ERR_RECTYPE; /* unknown header record */ - return NULL; + rdf_errno = RDF_ERR_RECTYPE; /* unknown header record */ + return NULL; #else - for (i = 0; i < r.g.reclen; i++) - RI8(r.g.data[i]); + for (i = 0; i < r.g.reclen; i++) + RI8(r.g.data[i]); #endif } return &r; @@ -468,17 +475,17 @@ rdfheaderrec *rdfgetheaderrec(rdffile *f) /* * Rewind to the beginning of the file - */ -void rdfheaderrewind(rdffile *f) + */ +void rdfheaderrewind(rdffile * f) { f->header_fp = 0; } - -rdf_headerbuf * rdfnewheader(void) +rdf_headerbuf *rdfnewheader(void) { - rdf_headerbuf * hb = malloc(sizeof(rdf_headerbuf)); - if (hb == NULL) return NULL; + rdf_headerbuf *hb = malloc(sizeof(rdf_headerbuf)); + if (hb == NULL) + return NULL; hb->buf = newmembuf(); hb->nsegments = 0; @@ -492,81 +499,81 @@ int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r) #ifndef STRICT_ERRORS int i; #endif - membufwrite(h->buf,&r->type,1); - membufwrite(h->buf,&r->g.reclen,1); + membufwrite(h->buf, &r->type, 1); + membufwrite(h->buf, &r->g.reclen, 1); switch (r->type) { - case RDFREC_GENERIC: /* generic */ - membufwrite(h->buf, &r->g.data, r->g.reclen); - break; + case RDFREC_GENERIC: /* generic */ + membufwrite(h->buf, &r->g.data, r->g.reclen); + break; case RDFREC_RELOC: case RDFREC_SEGRELOC: - membufwrite(h->buf,&r->r.segment,1); - membufwrite(h->buf,&r->r.offset,-4); - membufwrite(h->buf,&r->r.length,1); - membufwrite(h->buf,&r->r.refseg,-2); /* 9 bytes written */ - break; + membufwrite(h->buf, &r->r.segment, 1); + membufwrite(h->buf, &r->r.offset, -4); + membufwrite(h->buf, &r->r.length, 1); + membufwrite(h->buf, &r->r.refseg, -2); /* 9 bytes written */ + break; - case RDFREC_IMPORT: /* import */ + case RDFREC_IMPORT: /* import */ case RDFREC_FARIMPORT: - membufwrite(h->buf,&r->i.flags,1); - membufwrite(h->buf,&r->i.segment,-2); - membufwrite(h->buf,&r->i.label,strlen(r->i.label) + 1); - break ; - - case RDFREC_GLOBAL: /* export */ - membufwrite(h->buf,&r->e.flags,1); - membufwrite(h->buf,&r->e.segment,1); - membufwrite(h->buf,&r->e.offset,-4); - membufwrite(h->buf,&r->e.label,strlen(r->e.label) + 1); - break ; - - case RDFREC_DLL: /* DLL */ - membufwrite(h->buf,&r->d.libname,strlen(r->d.libname) + 1); - break ; - - case RDFREC_BSS: /* BSS */ - membufwrite(h->buf,&r->b.amount,-4); - break ; - - case RDFREC_MODNAME: /* Module name */ - membufwrite(h->buf,&r->m.modname,strlen(r->m.modname) + 1); - break ; - + membufwrite(h->buf, &r->i.flags, 1); + membufwrite(h->buf, &r->i.segment, -2); + membufwrite(h->buf, &r->i.label, strlen(r->i.label) + 1); + break; + + case RDFREC_GLOBAL: /* export */ + membufwrite(h->buf, &r->e.flags, 1); + membufwrite(h->buf, &r->e.segment, 1); + membufwrite(h->buf, &r->e.offset, -4); + membufwrite(h->buf, &r->e.label, strlen(r->e.label) + 1); + break; + + case RDFREC_DLL: /* DLL */ + membufwrite(h->buf, &r->d.libname, strlen(r->d.libname) + 1); + break; + + case RDFREC_BSS: /* BSS */ + membufwrite(h->buf, &r->b.amount, -4); + break; + + case RDFREC_MODNAME: /* Module name */ + membufwrite(h->buf, &r->m.modname, strlen(r->m.modname) + 1); + break; + default: #ifdef STRICT_ERRORS - return rdf_errno = RDF_ERR_RECTYPE; + return rdf_errno = RDF_ERR_RECTYPE; #else - for (i = 0; i < r->g.reclen; i++) - membufwrite(h->buf, r->g.data[i], 1); + for (i = 0; i < r->g.reclen; i++) + membufwrite(h->buf, r->g.data[i], 1); #endif } return 0; } -int rdfaddsegment(rdf_headerbuf *h, long seglength) +int rdfaddsegment(rdf_headerbuf * h, long seglength) { - h->nsegments ++; + h->nsegments++; h->seglength += seglength; return 0; } int rdfwriteheader(FILE * fp, rdf_headerbuf * h) { - long l, l2; + long l, l2; - fwrite (RDOFFId, 1, strlen(RDOFFId), fp) ; + fwrite(RDOFFId, 1, strlen(RDOFFId), fp); - l = membuflength (h->buf); - l2 = l + 14 + 10*h->nsegments + h->seglength; + l = membuflength(h->buf); + l2 = l + 14 + 10 * h->nsegments + h->seglength; l = translatelong(l); l2 = translatelong(l2); - fwrite (&l2, 4, 1, fp); /* object length */ - fwrite (&l, 4, 1, fp); /* header length */ + fwrite(&l2, 4, 1, fp); /* object length */ + fwrite(&l, 4, 1, fp); /* header length */ membufdump(h->buf, fp); - return 0; /* no error handling in here... CHANGE THIS! */ + return 0; /* no error handling in here... CHANGE THIS! */ } void rdfdoneheader(rdf_headerbuf * h) diff --git a/rdoff/rdoff.h b/rdoff/rdoff.h index d6c54b2..68f156b 100644 --- a/rdoff/rdoff.h +++ b/rdoff/rdoff.h @@ -10,7 +10,7 @@ * as acknowledgement is given in an appropriate manner to its authors, * with instructions of how to obtain a copy via ftp. */ - + #ifndef _RDOFF_H #define _RDOFF_H @@ -40,7 +40,6 @@ typedef unsigned int bool; /* Maximum number of segments that we can handle in one file */ #define RDF_MAXSEGS 64 - /* Record types that may present the RDOFF header */ #define RDFREC_GENERIC 0 #define RDFREC_RELOC 1 @@ -53,7 +52,6 @@ typedef unsigned int bool; #define RDFREC_MODNAME 8 #define RDFREC_COMMON 10 - /* * Generic record - contains the type and length field, plus a 128 byte * char array 'data' @@ -68,80 +66,80 @@ struct GenericRec { * Relocation record */ struct RelocRec { - byte type; /* must be 1 */ - byte reclen; /* content length */ - byte 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 */ - byte length; /* 1 2 or 4 bytes */ - uint16 refseg; /* segment to which reference refers to */ + byte type; /* must be 1 */ + byte reclen; /* content length */ + byte 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 */ + byte length; /* 1 2 or 4 bytes */ + uint16 refseg; /* segment to which reference refers to */ }; /* * Extern/import record */ struct ImportRec { - byte type; /* must be 2 */ - byte reclen; /* content length */ - byte flags; /* SYM_* flags (see below) */ - uint16 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[EXIM_LABEL_MAX]; /* zero terminated, should be written to file - until the zero, but not after it */ + byte type; /* must be 2 */ + byte reclen; /* content length */ + byte flags; /* SYM_* flags (see below) */ + uint16 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[EXIM_LABEL_MAX]; /* zero terminated, should be written to file + until the zero, but not after it */ }; /* * Public/export record */ struct ExportRec { - byte type; /* must be 3 */ - byte reclen; /* content length */ - byte flags; /* SYM_* flags (see below) */ - byte segment; /* segment referred to (0/1/2) */ - long offset; /* offset within segment */ - char label[EXIM_LABEL_MAX]; /* zero terminated as in import */ + byte type; /* must be 3 */ + byte reclen; /* content length */ + byte flags; /* SYM_* flags (see below) */ + byte segment; /* segment referred to (0/1/2) */ + long offset; /* offset within segment */ + char label[EXIM_LABEL_MAX]; /* zero terminated as in import */ }; /* * DLL record */ struct DLLRec { - byte type; /* must be 4 */ - byte reclen; /* content length */ - char libname[MODLIB_NAME_MAX]; /* name of library to link with at load time */ + byte type; /* must be 4 */ + byte reclen; /* content length */ + char libname[MODLIB_NAME_MAX]; /* name of library to link with at load time */ }; /* * BSS record */ struct BSSRec { - byte type; /* must be 5 */ - byte reclen; /* content length */ - long amount; /* number of bytes BSS to reserve */ + byte type; /* must be 5 */ + byte reclen; /* content length */ + long amount; /* number of bytes BSS to reserve */ }; /* * Module name record */ struct ModRec { - byte type; /* must be 8 */ - byte reclen; /* content length */ - char modname[MODLIB_NAME_MAX]; /* module name */ + byte type; /* must be 8 */ + byte reclen; /* content length */ + char modname[MODLIB_NAME_MAX]; /* module name */ }; /* * Common variable record */ struct CommonRec { - byte type; /* must be 10 */ - byte reclen; /* equals 7+label length */ - uint16 segment; /* segment number */ - long size; /* size of common variable */ - uint16 align; /* alignment (power of two) */ - char label[EXIM_LABEL_MAX]; /* zero terminated as in import */ + byte type; /* must be 10 */ + byte reclen; /* equals 7+label length */ + uint16 segment; /* segment number */ + long size; /* size of common variable */ + uint16 align; /* alignment (power of two) */ + char label[EXIM_LABEL_MAX]; /* zero terminated as in import */ }; /* Flags for ExportRec */ @@ -150,7 +148,6 @@ struct CommonRec { #define SYM_GLOBAL 4 #define SYM_IMPORT 8 - /*** The following part is used only by the utilities *************************/ #ifdef RDOFF_UTILS @@ -161,15 +158,15 @@ extern char *strdup(const char *); #endif typedef union RDFHeaderRec { - char type; /* invariant throughout all below */ - struct GenericRec g; /* type 0 */ - struct RelocRec r; /* type == 1 / 6 */ - struct ImportRec i; /* type == 2 / 7 */ - struct ExportRec e; /* type == 3 */ - struct DLLRec d; /* type == 4 */ - struct BSSRec b; /* type == 5 */ - struct ModRec m; /* type == 8 */ - struct CommonRec c; /* type == 10 */ + char type; /* invariant throughout all below */ + struct GenericRec g; /* type 0 */ + struct RelocRec r; /* type == 1 / 6 */ + struct ImportRec i; /* type == 2 / 7 */ + struct ExportRec e; /* type == 3 */ + struct DLLRec d; /* type == 4 */ + struct BSSRec b; /* type == 5 */ + struct ModRec m; /* type == 8 */ + struct CommonRec c; /* type == 10 */ } rdfheaderrec; struct SegmentHeaderRec { @@ -177,34 +174,34 @@ struct SegmentHeaderRec { uint16 type; uint16 number; uint16 reserved; - long length; + long length; /* information built up here */ - long offset; - byte *data; /* pointer to segment data if it exists in memory */ + long offset; + byte *data; /* pointer to segment data if it exists in memory */ }; 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 */ + 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 header_ofs; + long header_ofs; - byte *header_loc; /* keep location of header */ - long header_fp; /* current location within header for reading */ + byte *header_loc; /* keep location of header */ + long header_fp; /* current location within header for reading */ struct SegmentHeaderRec seg[RDF_MAXSEGS]; - int nsegs; + int nsegs; - long eof_offset; /* offset of the first byte beyond the end of this - module */ + long eof_offset; /* offset of the first byte beyond the end of this + module */ - char *name; /* name of module in libraries */ - int *refcount; /* pointer to reference count on file, or NULL */ + 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 */ +#define BUF_BLOCK_LEN 4088 /* selected to match page size (4096) + * on 80x86 machines for efficiency */ typedef struct memorybuffer { int length; byte buffer[BUF_BLOCK_LEN]; @@ -212,9 +209,9 @@ typedef struct memorybuffer { } memorybuffer; typedef struct { - memorybuffer * buf; /* buffer containing header records */ - int nsegments; /* number of segments to be written */ - long seglength; /* total length of all the segments */ + memorybuffer *buf; /* buffer containing header records */ + int nsegments; /* number of segments to be written */ + long seglength; /* total length of all the segments */ } rdf_headerbuf; /* segments used by RDOFF, understood by rdoffloadseg */ @@ -230,17 +227,17 @@ extern int rdf_errno; /* rdf_errno can hold these error codes */ enum { - /* 0 */ RDF_OK, - /* 1 */ RDF_ERR_OPEN, - /* 2 */ RDF_ERR_FORMAT, - /* 3 */ RDF_ERR_READ, - /* 4 */ RDF_ERR_UNKNOWN, - /* 5 */ RDF_ERR_HEADER, - /* 6 */ RDF_ERR_NOMEM, - /* 7 */ RDF_ERR_VER, - /* 8 */ RDF_ERR_RECTYPE, - /* 9 */ RDF_ERR_RECLEN, - /* 10 */ RDF_ERR_SEGMENT + /* 0 */ RDF_OK, + /* 1 */ RDF_ERR_OPEN, + /* 2 */ RDF_ERR_FORMAT, + /* 3 */ RDF_ERR_READ, + /* 4 */ RDF_ERR_UNKNOWN, + /* 5 */ RDF_ERR_HEADER, + /* 6 */ RDF_ERR_NOMEM, + /* 7 */ RDF_ERR_VER, + /* 8 */ RDF_ERR_RECTYPE, + /* 9 */ RDF_ERR_RECLEN, + /* 10 */ RDF_ERR_SEGMENT }; /* utility functions */ @@ -249,14 +246,14 @@ uint16 translateshort(uint16 in); char *translatesegmenttype(uint16 type); /* RDOFF file manipulation functions */ -int rdfopen(rdffile *f,const char *name); -int rdfopenhere(rdffile *f, FILE *fp, int *refcount, const char *name); -int rdfclose(rdffile *f); +int rdfopen(rdffile * f, const char *name); +int rdfopenhere(rdffile * f, FILE * fp, int *refcount, const char *name); +int rdfclose(rdffile * f); int rdffindsegment(rdffile * f, int segno); -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); +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, @@ -266,11 +263,11 @@ void rdfperror(const char *app,const char *name); to a file, and then rdfdoneheader to dispose of the header */ rdf_headerbuf *rdfnewheader(void); -int rdfaddheader(rdf_headerbuf *h,rdfheaderrec *r); -int rdfaddsegment(rdf_headerbuf *h, long seglength); -int rdfwriteheader(FILE *fp,rdf_headerbuf *h); -void rdfdoneheader(rdf_headerbuf *h); +int rdfaddheader(rdf_headerbuf * h, rdfheaderrec * r); +int rdfaddsegment(rdf_headerbuf * h, long seglength); +int rdfwriteheader(FILE * fp, rdf_headerbuf * h); +void rdfdoneheader(rdf_headerbuf * h); -#endif /* RDOFF_UTILS */ +#endif /* RDOFF_UTILS */ -#endif /* _RDOFF_H */ +#endif /* _RDOFF_H */ diff --git a/rdoff/rdx.c b/rdoff/rdx.c index 778b29f..82210a6 100644 --- a/rdoff/rdx.c +++ b/rdoff/rdx.c @@ -18,39 +18,40 @@ #include "rdfload.h" #include "symtab.h" -typedef int (*main_fn) (int,char**); /* Main function prototype */ +typedef int (*main_fn) (int, char **); /* Main function prototype */ int main(int argc, char **argv) { - rdfmodule * m; - main_fn code; - symtabEnt * s; + rdfmodule *m; + main_fn code; + symtabEnt *s; if (argc < 2) { - puts("usage: rdx <rdoff-executable> [params]\n"); - exit(255); + puts("usage: rdx <rdoff-executable> [params]\n"); + exit(255); } m = rdfload(argv[1]); - if (! m) { - rdfperror("rdx",argv[1]); - exit(255); + if (!m) { + rdfperror("rdx", 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... */ + 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); + 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 */ + argv++, argc--; /* remove 'rdx' from command line */ - return code(argc,argv); /* execute */ + return code(argc, argv); /* execute */ } diff --git a/rdoff/segtab.c b/rdoff/segtab.c index f3168fc..ed26aea 100644 --- a/rdoff/segtab.c +++ b/rdoff/segtab.c @@ -7,8 +7,8 @@ struct segtabnode { int destseg; long offset; - struct segtabnode * left; - struct segtabnode * right; + struct segtabnode *left; + struct segtabnode *right; /* * counts of how many are left or right, for use in reorganising * the tree @@ -30,93 +30,90 @@ struct segtabnode { * implementation: we build a binary tree. */ - void init_seglocations(segtab * root) { *root = NULL; } -void descend_tree_add(struct segtabnode * * node, - int localseg, int destseg, long offset) +void descend_tree_add(struct segtabnode **node, + int localseg, int destseg, long offset) { - struct segtabnode * n; + struct segtabnode *n; if (*node == NULL) { - *node = malloc (sizeof (**node)); - if (!*node) { - fprintf(stderr, "segment table: out of memory\n"); - exit(1); - } - (*node)->localseg = localseg; - (*node)->offset = offset; - (*node)->left = NULL; - (*node)->leftcount = 0; - (*node)->right = NULL; - (*node)->rightcount = 0; - (*node)->destseg = destseg; - return; + *node = malloc(sizeof(**node)); + if (!*node) { + fprintf(stderr, "segment table: out of memory\n"); + exit(1); + } + (*node)->localseg = localseg; + (*node)->offset = offset; + (*node)->left = NULL; + (*node)->leftcount = 0; + (*node)->right = NULL; + (*node)->rightcount = 0; + (*node)->destseg = destseg; + return; } - if (localseg < (*node)->localseg) - { - (*node)->leftcount++; - descend_tree_add(&(*node)->left, localseg, destseg, offset); - - if ((*node)->leftcount > (*node)->rightcount + 2) { - n = * node; - *node = n->left; - n->left = (*node)->right; - n->leftcount = (*node)->rightcount; - (*node)->right = n; - (*node)->rightcount = n->leftcount + n->rightcount + 1; - } - } - else - { - (*node)->rightcount++; - descend_tree_add(&(*node)->right, localseg, destseg, offset); - - if ((*node)->rightcount > (*node)->leftcount + 2) { - n = * node; - *node = n->right; - n->right= (*node)->left; - n->rightcount = (*node)->leftcount; - (*node)->left = n; - (*node)->leftcount = n->leftcount + n->rightcount + 1; - } + if (localseg < (*node)->localseg) { + (*node)->leftcount++; + descend_tree_add(&(*node)->left, localseg, destseg, offset); + + if ((*node)->leftcount > (*node)->rightcount + 2) { + n = *node; + *node = n->left; + n->left = (*node)->right; + n->leftcount = (*node)->rightcount; + (*node)->right = n; + (*node)->rightcount = n->leftcount + n->rightcount + 1; + } + } else { + (*node)->rightcount++; + descend_tree_add(&(*node)->right, localseg, destseg, offset); + + if ((*node)->rightcount > (*node)->leftcount + 2) { + n = *node; + *node = n->right; + n->right = (*node)->left; + n->rightcount = (*node)->leftcount; + (*node)->left = n; + (*node)->leftcount = n->leftcount + n->rightcount + 1; + } } } void add_seglocation(segtab * root, int localseg, int destseg, long offset) { - descend_tree_add((struct segtabnode **) root, localseg, destseg, offset); + descend_tree_add((struct segtabnode **)root, localseg, destseg, + offset); } -int get_seglocation(segtab * root, int localseg, int * destseg, long * offset) +int get_seglocation(segtab * root, int localseg, int *destseg, + long *offset) { - struct segtabnode * n = (struct segtabnode *) *root; - - while (n && n->localseg != localseg) - { - if (localseg < n->localseg) - n = n->left; - else - n = n->right; + struct segtabnode *n = (struct segtabnode *)*root; + + while (n && n->localseg != localseg) { + if (localseg < n->localseg) + n = n->left; + else + n = n->right; } if (n) { - *destseg = n->destseg; - *offset = n->offset; - return 1; - } - else - return 0; + *destseg = n->destseg; + *offset = n->offset; + return 1; + } else + return 0; } -void freenode(struct segtabnode * n) +void freenode(struct segtabnode *n) { - if (!n) return; - freenode (n->left); - freenode (n->right); + if (!n) + return; + freenode(n->left); + freenode(n->right); free(n); } @@ -127,16 +124,17 @@ void done_seglocations(segtab * root) } #if 0 -void printnode(int i, struct segtabnode * n) +void printnode(int i, struct segtabnode *n) { - if (!n) return; + if (!n) + return; printnode(i + 1, n->left); - printf ("%*s%d %d %ld\n", i, "", n->localseg, n->destseg, n->offset); + printf("%*s%d %d %ld\n", i, "", n->localseg, n->destseg, n->offset); printnode(i + 1, n->right); } void printtable() { - printnode(0,root); + printnode(0, root); } #endif diff --git a/rdoff/segtab.h b/rdoff/segtab.h index 7e8e193..bd29357 100644 --- a/rdoff/segtab.h +++ b/rdoff/segtab.h @@ -1,6 +1,6 @@ -typedef void * segtab; +typedef void *segtab; void init_seglocations(segtab * r); void add_seglocation(segtab * r, int localseg, int destseg, long offset); -int get_seglocation(segtab * r, int localseg, int * destseg, long * offset); +int get_seglocation(segtab * r, int localseg, int *destseg, long *offset); void done_seglocations(segtab * r); diff --git a/rdoff/symtab.c b/rdoff/symtab.c index 3037963..8d8ac62 100644 --- a/rdoff/symtab.c +++ b/rdoff/symtab.c @@ -21,110 +21,107 @@ /* Private data types */ typedef struct tagSymtabNode { - struct tagSymtabNode * next; - symtabEnt ent; + struct tagSymtabNode *next; + symtabEnt ent; } symtabNode; -typedef symtabNode *(symtabTab[SYMTABSIZE]); +typedef symtabNode *(symtabTab[SYMTABSIZE]); -typedef symtabTab *symtab; +typedef symtabTab *symtab; /* ------------------------------------- */ -void * -symtabNew(void) +void *symtabNew(void) { - symtab mytab; + symtab mytab; - mytab = (symtabTab *) calloc(SYMTABSIZE ,sizeof(symtabNode *)); - if (mytab == NULL) { - fprintf(stderr,"symtab: out of memory\n"); - exit(3); - } + mytab = (symtabTab *) calloc(SYMTABSIZE, sizeof(symtabNode *)); + if (mytab == NULL) { + fprintf(stderr, "symtab: out of memory\n"); + exit(3); + } - return mytab; + return mytab; } /* ------------------------------------- */ -void -symtabDone(void *stab) +void symtabDone(void *stab) { - symtab mytab = (symtab)stab; - int i; - symtabNode *this, *next; + symtab mytab = (symtab) stab; + int i; + symtabNode *this, *next; - for (i=0; i < SYMTABSIZE; ++i) { + for (i = 0; i < SYMTABSIZE; ++i) { - for (this = (*mytab)[i]; this; this=next) - { next = this->next; free (this); } + for (this = (*mytab)[i]; this; this = next) { + next = this->next; + free(this); + } - } - free (*mytab); + } + free(*mytab); } /* ------------------------------------- */ -void -symtabInsert(void *stab, symtabEnt *ent) +void symtabInsert(void *stab, symtabEnt * ent) { - symtab mytab = (symtab) stab; - symtabNode *node; - int slot; + symtab mytab = (symtab) stab; + symtabNode *node; + int slot; - node = malloc(sizeof(symtabNode)); - if (node == NULL) { - fprintf(stderr,"symtab: out of memory\n"); - exit(3); - } + node = malloc(sizeof(symtabNode)); + if (node == NULL) { + fprintf(stderr, "symtab: out of memory\n"); + exit(3); + } - slot = slotnum(ent->name); + slot = slotnum(ent->name); - node->ent = *ent; - node->next = (*mytab)[slot]; - (*mytab)[slot] = node; + node->ent = *ent; + node->next = (*mytab)[slot]; + (*mytab)[slot] = node; } /* ------------------------------------- */ -symtabEnt * -symtabFind(void *stab, const char *name) +symtabEnt *symtabFind(void *stab, const char *name) { - symtab mytab = (symtab) stab; - int slot = slotnum(name); - symtabNode *node = (*mytab)[slot]; - - while (node) { - if (!strcmp(node->ent.name,name)) { - return &(node->ent); - } - node = node->next; - } - - return NULL; + symtab mytab = (symtab) stab; + int slot = slotnum(name); + symtabNode *node = (*mytab)[slot]; + + while (node) { + if (!strcmp(node->ent.name, name)) { + return &(node->ent); + } + node = node->next; + } + + return NULL; } /* ------------------------------------- */ -void -symtabDump(void *stab, FILE* of) +void symtabDump(void *stab, FILE * of) { - symtab mytab = (symtab)stab; - int i; - char *SegNames[3]={"code","data","bss"}; - - fprintf(of, "Symbol table is ...\n"); - for (i=0; i < SYMTABSIZE; ++i) { - symtabNode *l = (symtabNode *)(*mytab)[i]; - - if (l) { - fprintf(of, " ... slot %d ...\n", i); - } - while(l) { - if ((l->ent.segment) == -1) { - fprintf(of,"%-32s Unresolved reference\n",l->ent.name); - } else { - fprintf(of, "%-32s %s:%08lx (%ld)\n",l->ent.name, - SegNames[l->ent.segment], - l->ent.offset, l->ent.flags); - } - l = l->next; - } - } - fprintf(of, "........... end of Symbol table.\n"); + symtab mytab = (symtab) stab; + int i; + char *SegNames[3] = { "code", "data", "bss" }; + + fprintf(of, "Symbol table is ...\n"); + for (i = 0; i < SYMTABSIZE; ++i) { + symtabNode *l = (symtabNode *) (*mytab)[i]; + + if (l) { + fprintf(of, " ... slot %d ...\n", i); + } + while (l) { + if ((l->ent.segment) == -1) { + fprintf(of, "%-32s Unresolved reference\n", l->ent.name); + } else { + fprintf(of, "%-32s %s:%08lx (%ld)\n", l->ent.name, + SegNames[l->ent.segment], + l->ent.offset, l->ent.flags); + } + l = l->next; + } + } + fprintf(of, "........... end of Symbol table.\n"); } diff --git a/rdoff/symtab.h b/rdoff/symtab.h index c1fe031..5bd9bcb 100644 --- a/rdoff/symtab.h +++ b/rdoff/symtab.h @@ -7,16 +7,14 @@ */ typedef struct { - char *name; - int segment; - long offset; - long flags; + char *name; + int segment; + long offset; + long flags; } symtabEnt; void *symtabNew(void); void symtabDone(void *symtab); -void symtabInsert(void *symtab,symtabEnt *ent); -symtabEnt *symtabFind(void *symtab,const char *name); -void symtabDump(void *symtab,FILE *of); - - +void symtabInsert(void *symtab, symtabEnt * ent); +symtabEnt *symtabFind(void *symtab, const char *name); +void symtabDump(void *symtab, FILE * of); |