+//
+// RLN - Reboot's Linker for the Atari Jaguar console system
+// Copyright (C) 199x, Allan K. Pratt, 2014-2018 Reboot & Friends
+//
+
+#include "rln.h"
+//#include <assert.h>
+
+unsigned errflag = 0; // Error flag, goes TRUE on error
+unsigned waitflag = 0; // Wait for any keypress flag
+unsigned versflag = 0; // Version banner has been shown flag
+unsigned aflag = 0; // Absolute linking flag
+unsigned bflag = 0; // Don't remove mulitply def locals flag
+unsigned cflag = 0; // COF executable
+unsigned dflag = 0; // Wait for key after link flag
+unsigned gflag = 0; // Source level debug include flag
+unsigned lflag = 0; // Add local symbols to output flag
+unsigned mflag = 0; // Produce symbol load map flag
+unsigned oflag = 0; // Output filename specified
+unsigned rflag = 0; // Segment alignment size flag
+unsigned sflag = 0; // Output only global symbols
+unsigned vflag = 0; // Verbose flag
+unsigned wflag = 0; // Show warnings flag
+unsigned zflag = 0; // Suppress banner flag
+unsigned pflag = 0, uflag = 0; // Unimplemented flags (partial link, don't abort on unresolved symbols)
+unsigned hd = 0; // Index of next file handle to fill
+unsigned secalign = 7; // Section Alignment (8=phrase)
+unsigned tbase = 0; // TEXT base address
+unsigned dbase = 0; // DATA base address
+unsigned bbase = 0; // BSS base address
+unsigned textoffset = 0; // COF TEXT segment offset
+unsigned dataoffset = 0; // COF DATA segment offset
+unsigned bssoffset = 0; // COF BSS segment offset
+unsigned displaybanner = 1; // Display version banner
+unsigned symoffset = 0; // Symbol table offset in output file
+unsigned dbgsymbase = 0; // Debug symbol base address
+int noheaderflag = 0; // No header flag for ABS files
+int hflags; // Value of the arg to -h option
+int ttype, dtype, btype; // Type flag: 0, -1, -2, -3, -4
+int tval, dval, bval; // Values of these abs bases
+int hflag[NHANDLES]; // True for include files
+int handle[NHANDLES]; // Open file handles
+int textsize, datasize, bsssize; // Cumulative segment sizes
+char libdir[FARGSIZE * 3]; // Library directory to search
+char ofile[FARGSIZE]; // Output file name (.o)
+char * name[NHANDLES]; // Associated file names
+char * cmdlnexec = NULL; // Executable name - pointer to ARGV[0]
+char * hsym1[SYMLEN]; // First symbol for include files
+char * hsym2[SYMLEN]; // Second symbol for include files
+struct OFILE * plist = NULL; // Object image list pointer
+struct OFILE * plast; // Last object image list pointer
+struct OFILE * olist = NULL; // Pointer to first object file in list
+struct OFILE * olast; // Pointer to last object file in list
+char * arPtr[512];
+uint32_t arIndex = 0;
+struct HREC * htable[NBUCKETS]; // Hash table
+struct HREC * unresolved = NULL; // Pointer to unresolved hash list
+char * ost; // Output symbol table
+char * ost_ptr; // Output symbol table; current pointer
+char * ost_end; // Output symbol table; end pointer
+char * oststr; // Output string table
+char * oststr_ptr; // Output string table; current pointer
+char * oststr_end; // Output string table; end pointer
+int ost_index = 0; // Index of next ost addition
+uint8_t nullStr[1] = "\x00"; // Empty string
+struct HREC * arSymbol = NULL; // Pointer to AR symbol table
+
+
+// Function prototypes
+struct HREC * LookupHREC(char *);
+char * PathTail(char *);
+void ShowHelp(void);
+void ShowVersion(void);
+int OSTLookup(char * sym);
+int OSTAdd(char * name, int type, long value);
+int ProcessFiles(void);
+int doargs(int argc, char * argv[]);
+
+
+//
+// Get a long word from memory
+//
+static inline uint32_t GetLong(uint8_t * src)
+{
+ return (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
+}
+
+
+//
+// Put a long word into memory
+//
+static inline void PutLong(uint8_t * dest, uint32_t val)
+{
+ *dest++ = (uint8_t)(val >> 24);
+ *dest++ = (uint8_t)(val >> 16);
+ *dest++ = (uint8_t)(val >> 8);
+ *dest = (uint8_t)val;
+}
+
+
+//
+// Get a word from memory
+//
+static inline uint16_t GetWord(uint8_t * src)
+{
+ return (src[0] << 8) | src[1];
+}
+
+
+//
+// Put a word into memory
+//
+static inline void PutWord(uint8_t * dest, uint16_t val)
+{
+ *dest++ = (uint8_t)(val >> 8);
+ *dest = (uint8_t)val;
+}
+
+
+//
+// Find passed in file's length in bytes
+// N.B.: This also resets the file's pointer to the start of the file
+//
+long FileSize(int fd)
+{
+ long size = lseek(fd, 0, SEEK_END);
+ lseek(fd, 0, SEEK_SET);
+
+ return size;
+}
+
+
+//
+// For this object file, add symbols to the output symbol table after
+// relocating them. Returns TRUE if OSTLookup returns an error (-1).
+//
+int DoSymbols(struct OFILE * ofile)
+{
+ int type;
+ long value;
+ int index;
+ int j;
+ struct HREC * hptr;
+ uint32_t tsoSave, dsoSave, bsoSave;
+
+ // Point to first symbol record in the object file
+ char * symptr = (ofile->o_image + 32
+ + ofile->o_header.tsize
+ + ofile->o_header.dsize
+ + ofile->o_header.absrel.reloc.tsize
+ + ofile->o_header.absrel.reloc.dsize);
+
+ // Point to end of symbol record in the object file
+ char * symend = symptr + ofile->o_header.ssize;
+
+ uint32_t tsegoffset = ofile->segBase[TEXT];
+ uint32_t dsegoffset = ofile->segBase[DATA];
+ uint32_t bsegoffset = ofile->segBase[BSS];
+
+ // Save segment vars, so we can restore them if needed
+ tsoSave = tsegoffset, dsoSave = dsegoffset, bsoSave = bsegoffset;
+
+ // Process each record in the object's symbol table
+ for(; symptr!=symend; symptr+=12)
+ {
+ index = GetLong(symptr + 0); // Obtain symbol string index
+ type = GetLong(symptr + 4); // Obtain symbol type
+ value = GetLong(symptr + 8); // Obtain symbol value
+
+ // Global/External symbols have a pre-processing stage
+ // N.B.: This destroys the t/d/bsegoffset discovered above. So if a
+ // local symbol follows a global/exported one, it gets wrong
+ // info! [Should be FIXED now]
+ if (type & 0x01000000)
+ {
+ // Obtain the string table index for the relocation symbol, look
+ // for it in the globals hash table to obtain information on that
+ // symbol.
+ hptr = LookupHREC(symend + index);
+
+ if (hptr == NULL)
+ {
+ // Try to find it in the OST
+ int ostIndex = OSTLookup(symend + index);
+
+ if (ostIndex == -1)
+ {
+ printf("DoSymbols(): Symbol not found in hash table: '%s' (%s)\n", symend + index, ofile->o_name);
+ return 1;
+ }
+
+ if (vflag > 1)
+ printf("DoSymbols(): Skipping symbol '%s' (%s) found in OST...\n", symend + index, ofile->o_name);
+
+ // If the symbol is not in any .a or .o units, it must be one
+ // of the injected ones (_TEXT_E, _DATA_E, or _BSS_E), so skip
+ // it [or maybe not? In verbose mode, we see nothing...!]
+ continue;
+ }
+
+ tsegoffset = hptr->h_ofile->segBase[TEXT];
+ dsegoffset = hptr->h_ofile->segBase[DATA];
+ bsegoffset = hptr->h_ofile->segBase[BSS];
+
+ // Update type with global type
+ type = hptr->h_type;
+
+ // Remove global flag if absolute
+ if (type == (T_GLBL | T_ABS))
+ type = T_ABS;
+
+ // If the global/external has a value then update that value in
+ // accordance with the segment sizes of the object file it
+ // originates from
+ if (hptr->h_value)
+ {
+ switch (hptr->h_type & 0x0E000000)
+ {
+ case T_ABS:
+ case T_TEXT:
+ value = hptr->h_value;
+ break;
+ case T_DATA:
+ value = hptr->h_value - hptr->h_ofile->o_header.tsize;
+ break;
+ case T_BSS:
+ value = hptr->h_value
+ - (hptr->h_ofile->o_header.tsize
+ + hptr->h_ofile->o_header.dsize);
+ break;
+ default:
+ if (vflag > 1)
+ printf("DoSymbols: No adjustment made for symbol: %s (%s) = %X\n", symend + index, ofile->o_name, hptr->h_value);
+ }
+ }
+ }
+ // If *not* a global/external, use the info from passed in object
+ else
+ tsegoffset = tsoSave, dsegoffset = dsoSave, bsegoffset = bsoSave;
+
+ // Process and update the value dependent on whether the symbol is a
+ // debug symbol or not
+ // N.B.: Debug symbols are currently not supported
+ if (type & 0xF0000000)
+ {
+ // DEBUG SYMBOL
+ // Set the correct debug symbol base address (TEXT segment)
+#if 0
+ dbgsymbase = 0;
+
+ for(j=0; (unsigned)j<dosymi; j++)
+ dbgsymbase += obj_segsize[j][0];
+#else
+ dbgsymbase = ofile->segBase[TEXT];
+#endif
+
+ switch (type & 0xFF000000)
+ {
+ case 0x64000000:
+ value = tval + dbgsymbase;
+ break;
+ case 0x44000000:
+ case 0x46000000:
+ case 0x48000000:
+ value = tval + dbgsymbase + value;
+ default:
+ break;
+ }
+
+ PutLong(symptr + 8, value);
+ }
+ else
+ {
+ // NON-DEBUG SYMBOL
+ // Now make modifications to the symbol value, local or global,
+ // based on the segment sizes of the object file currently being
+ // processed.
+ switch (type & T_SEG)
+ {
+ case T_ABS:
+ break;
+ case T_TEXT:
+ value = tbase + tsegoffset + value;
+ PutLong(symptr + 8, value);
+ break;
+ case T_DATA:
+ if (type & T_GLBL)
+ value = dbase + dsegoffset + value;
+ else
+ value = dbase + dsegoffset + (value
+ - ofile->o_header.tsize);
+
+ PutLong(symptr + 8, value);
+ break;
+ case T_BSS:
+ if (type & T_GLBL)
+ value = bbase + bsegoffset + value;
+ else
+ value = bbase + bsegoffset
+ + (value - (ofile->o_header.tsize
+ + ofile->o_header.dsize));
+
+ PutLong(symptr + 8, value);
+ break;
+ default:
+ break;
+ }
+ }
+
+ // Add to output symbol table if global/extern, or local flag is set
+ if (isglobal(type) || lflag)
+ {
+ if (vflag > 1)
+ printf("DoSymbols: Adding symbol: %s (%s) to OST...\n", symend + index, ofile->o_name);
+
+ index = OSTAdd(symend + index, type, value);
+
+ if (index == -1)
+ {
+ printf("DoSymbols(): Failed to add symbol '%s' to OST!\n", symend + index);
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+
+//
+// Free up hash memory
+//
+void FreeHashes(void)
+{
+ int i;
+
+ for(i=0; i<NBUCKETS; i++)
+ {
+ struct HREC * hptr = htable[i];
+
+ while (hptr)
+ {
+ struct HREC * htemp = hptr->h_next;
+ free(hptr);
+ hptr = htemp;
+ }
+ }
+}
+
+
+//
+// Add all global and external symbols to the output symbol table
+// [This is confusing--is it adding globals or locals? common == local!
+// but then again, we see this in the header:
+// #define T_COMMON (T_GLOBAL | T_EXTERN) but that could be just bullshit.]
+//
+// Common symbols have a different number in the "value" field of the symbol
+// table (!0) than purely external symbols do (0). So you have to look at the
+// type (T_GLBL) *and* the value to determine if it's a common symbol.
+//
+long DoCommon(void)
+{
+ struct HREC * hptr;
+ int i;
+
+ for(i=0; i<NBUCKETS; i++)
+ {
+ for(hptr=htable[i]; hptr!=NULL; hptr=hptr->h_next)
+ {
+//NO! if (iscommon(hptr->h_type))
+ if (isglobal(hptr->h_type))// || isextern(hptr->h_type))
+ {
+// Skip if in *.a file... (does nothing)
+//if (hptr->h_ofile->isArchiveFile)
+// continue;
+
+//Is this true? Couldn't an absolute be exported???
+ if (hptr->h_type == (T_GLBL | T_ABS))
+ hptr->h_type = T_ABS; // Absolutes *can't* be externals
+
+ if (OSTAdd(hptr->h_sym, hptr->h_type, hptr->h_value) == -1)
+ return -1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+
+//
+// Add a symbol's name, type, and value to the OST.
+// Returns the index of the symbol in OST, or -1 for error.
+//
+int OSTAdd(char * name, int type, long value)
+{
+ int ost_offset_p, ost_offset_e = 0; // OST table offsets for position calcs
+ int ostresult; // OST index result
+ int slen = strlen(name);
+
+ // If the OST or OST string table has not been initialised then do so
+ if (ost_index == 0)
+ {
+ ost = malloc(OST_BLOCK);
+ oststr = malloc(OST_BLOCK);
+
+ if (ost == NULL)
+ {
+ printf("OST memory allocation error.\n");
+ return -1;
+ }
+
+ if (oststr == NULL)
+ {
+ printf("OSTSTR memory allocation error.\n");
+ return -1;
+ }
+
+ ost_ptr = ost; // Set OST start pointer
+ ost_end = ost + OST_BLOCK; // Set OST end pointer
+
+ PutLong(oststr, 0x00000004); // Just null long for now
+ oststr_ptr = oststr + 4; // Skip size of str table long (incl null long)
+ PutLong(oststr_ptr, 0x00000000); // Null terminating long
+ oststr_end = oststr + OST_BLOCK;
+ }
+ else
+ {
+ // If next symbol record exceeds current allocation then expand symbol
+ // table and/or symbol string table.
+ ost_offset_p = (ost_ptr - ost);
+ ost_offset_e = (ost_end - ost);
+
+ // 3 x uint32_t (12 bytes)
+ if ((ost_ptr + 12) > ost_end)
+ {
+ // We want to allocate the current size of the OST + another block.
+ ost = realloc(ost, ost_offset_e + OST_BLOCK);
+
+ if (ost == NULL)
+ {
+ printf("OST memory reallocation error.\n");
+ return -1;
+ }
+
+ ost_ptr = ost + ost_offset_p;
+ ost_end = (ost + ost_offset_e) + OST_BLOCK;
+ }
+
+ ost_offset_p = (oststr_ptr - oststr);
+ ost_offset_e = (oststr_end - oststr);
+
+ // string length + terminating NULL + uint32_t (terminal long)
+ if ((oststr_ptr + (slen + 1 + 4)) > oststr_end)
+ {
+ oststr = realloc(oststr, ost_offset_e + OST_BLOCK);
+
+ if (oststr == NULL)
+ {
+ printf("OSTSTR memory reallocation error.\n");
+ return -1;
+ }
+
+ oststr_ptr = oststr + ost_offset_p;
+ oststr_end = (oststr + ost_offset_e) + OST_BLOCK;
+ }
+ }
+
+ // If this is a debug symbol and the include debug symbol flag (-g) is not
+ // set then do nothing
+ if ((type & 0xF0000000) && !gflag)
+ {
+ // Do nothing
+ return 0;
+ }
+
+ // Get symbol index in OST, if any (-1 if not found)
+ ostresult = OSTLookup(name);
+
+ // If the symbol is in the output symbol table and the bflag is set
+ // (don't remove multiply defined locals) and this is not an
+ // external/global symbol *** OR *** the symbol is not in the output
+ // symbol table then add it.
+ if (((ostresult != -1) && bflag && !(type & 0x01000000))
+ || ((ostresult != -1) && gflag && (type & 0xF0000000))
+ || (ostresult == -1))
+ {
+ if ((type & 0xF0000000) == 0x40000000)
+ PutLong(ost_ptr, 0x00000000); // Zero string table offset for dbg line
+ else
+ PutLong(ost_ptr, (oststr_ptr - oststr)); // String table offset of symbol string
+
+ PutLong(ost_ptr + 4, type);
+ PutLong(ost_ptr + 8, value);
+ ost_ptr += 12;
+
+ // If the symbol type is anything but a debug line information
+ // symbol then write the symbol string to the string table
+ if ((type & 0xF0000000) != 0x40000000)
+ {
+ strcpy(oststr_ptr, name); // Put symbol name in string table
+ *(oststr_ptr + slen) = '\0'; // Add null terminating character
+ oststr_ptr += (slen + 1);
+ PutLong(oststr_ptr, 0x00000000); // Null terminating long
+ PutLong(oststr, (oststr_ptr - oststr)); // Update size of string table
+ }
+
+ if (vflag > 1)
+ printf("OSTAdd: (%s), type=$%08X, val=$%08lX\n", name, type, value);
+
+// is ost_index pointing one past?
+// does this return the same regardless of if its ++n or n++?
+// no. it returns the value of ost_index *before* it's incremented.
+ return ++ost_index;
+ }
+
+ return ostresult;
+}
+
+
+//
+// Return the index of a symbol in the output symbol table
+// N.B.: This is a 1-based index! (though there's no real reason for it to be)
+//
+int OSTLookup(char * sym)
+{
+ int i;
+ int stro = 4; // Offset in string table
+
+ for(i=0; i<ost_index; i++)
+ {
+ if (strcmp(oststr + stro, sym) == 0)
+ return i + 1;
+
+ stro += strlen(oststr + stro) + 1;
+ }
+
+ return -1;
+}
+
+
+//
+// Add unresolved externs to the output symbol table
+// N.B.: Only adds unresolved symbols *if* they're not already in the OST
+//
+int DoUnresolved(void)
+{
+ struct HREC * hptr = unresolved;
+
+ // Add to OST while unresolved list is valid
+ while (hptr != NULL)
+ {
+ if (OSTAdd(hptr->h_sym, T_GLBL, 0L) == -1)
+ return 1;
+
+ if (vflag > 1)
+ printf("DoUnresolved(): '%s' (%s:$%08X) in OST\n", hptr->h_sym, hptr->h_ofile->o_name, hptr->h_type);
+
+ struct HREC * htemp = hptr->h_next;
+ free(hptr);
+ hptr = htemp;
+ }
+
+ unresolved = NULL;
+ return 0;
+}
+
+
+//
+// Update object file TEXT and DATA segments based on relocation records. Take
+// in an OFILE header and flag (T_TEXT, T_DATA) to process. Return (0) is
+// successful or non-zero (1) if failed.
+//
+int RelocateSegment(struct OFILE * ofile, int flag)
+{
+ char * symtab; // Start of symbol table
+ char * symbols; // Start of symbols
+ char * sptr; // Start of segment data
+ char * rptr; // Start of segment relocation records
+ unsigned symidx; // Offset to symbol
+ unsigned addr; // Relocation address
+ unsigned rflg; // Relocation flags
+ unsigned olddata; // Old segment data at reloc address
+ unsigned newdata = 0; // New segment data at reloc address
+ unsigned pad; // Temporary to calculate phrase padding
+ int i; // Iterator
+ char sym[SYMLEN]; // String for symbol name/hash search
+ int ssidx; // Segment size table index
+ unsigned glblreloc; // Global relocation flag
+ unsigned absreloc; // Absolute relocation flag
+ unsigned relreloc; // Relative relocation flag
+ unsigned wordreloc; // Relocate word only flag
+ unsigned opreloc; // Relocate OP data address only flag
+ unsigned swcond; // Switch statement condition
+ unsigned relocsize; // Relocation record size
+ unsigned saveBits; // OP data leftover bits save
+ unsigned saveBits2;
+
+ // If there is no TEXT relocation data for the selected object file segment
+ // then update the COF TEXT segment offset allowing for the phrase padding
+ if ((flag == T_TEXT) && !ofile->o_header.absrel.reloc.tsize)
+ {
+ // SCPCD : we should not increment the textoffset before the end of processing the object file, else data section will point to wrong textoffset
+ return 0;
+ }
+
+ // If there is no DATA relocation data for the selected object file segment
+ // then update the COF DATA and BSS segment offsets allowing for the phrase
+ // padding
+ if ((flag == T_DATA) && !ofile->o_header.absrel.reloc.dsize)
+ {
+ // SCPCD : the T_DATA is the last section of the file, we can now increment the textoffset, dataoffset and bssoffset
+
+ // TEXT segment size plus padding
+ pad = ((ofile->o_header.tsize + secalign) & ~secalign);
+ textoffset += (ofile->o_header.tsize + (pad - ofile->o_header.tsize));
+
+ if (vflag > 1)
+ printf("RelocateSegment(%s, TEXT) : No relocation data\n", ofile->o_name);
+
+ // DATA segment size plus padding
+ pad = ((ofile->o_header.dsize + secalign) & ~secalign);
+ dataoffset += (ofile->o_header.dsize + (pad - ofile->o_header.dsize));
+
+ // BSS segment size plus padding
+ pad = ((ofile->o_header.bsize + secalign) & ~secalign);
+ bssoffset += (ofile->o_header.bsize + (pad - ofile->o_header.bsize));
+
+ if (vflag > 1)
+ printf("RelocateSegment(%s, DATA) : No relocation data\n", ofile->o_name);
+
+ return 0;
+ }
+
+ if (vflag > 1)
+ printf("RelocateSegment(%s, %s) : Processing Relocation Data\n",
+ ofile->o_name, flag == T_DATA ? "DATA" : "TEXT");
+
+ // Obtain pointer to start of symbol table
+ symtab = (ofile->o_image + 32 + ofile->o_header.tsize
+ + ofile->o_header.dsize
+ + ofile->o_header.absrel.reloc.tsize
+ + ofile->o_header.absrel.reloc.dsize);
+
+ // Obtain pointer to start of symbols
+ symbols = symtab + ofile->o_header.ssize;
+
+ // Obtain pointer to start of TEXT segment
+ sptr = ofile->o_image + 32;
+
+ // Obtain pointer to start of TEXT relocation records
+ rptr = sptr + (ofile->o_header.tsize + ofile->o_header.dsize);
+
+ relocsize = ofile->o_header.absrel.reloc.tsize;
+
+ if (vflag)
+ printf("RELOCSIZE :: %d Records = %d\n", relocsize, relocsize / 8);
+
+ // Update pointers if DATA relocation records are being processed
+ if (flag == T_DATA)
+ {
+ sptr += ofile->o_header.tsize; // Start of DATA segment
+ rptr += ofile->o_header.absrel.reloc.tsize; // Start of DATA relocation records
+ relocsize = ofile->o_header.absrel.reloc.dsize;
+ }
+
+ // Process each relocation record for the TEXT segment
+ for(i=0; i<(int)relocsize; i+=8)
+ {
+ // Obtain both the relocation address and the relocation flags from the
+ // object file image
+ addr = GetLong(rptr);
+ rflg = GetLong(rptr + 4);
+ glblreloc = (rflg & 0x00000010 ? 1 : 0);// Set global relocation flag
+ absreloc = (rflg & 0x00000040 ? 1 : 0); // Set absolute relocation flag
+ relreloc = (rflg & 0x000000A0 ? 1 : 0); // Set relative relocation flag
+ wordreloc = (rflg & 0x00000002 ? 1 : 0); // Set word relocation flag
+ opreloc = (rflg & 0x00000004 ? 1 : 0); // Set OP relocation flag
+
+ // Additional processing required for global relocations
+ if (glblreloc)
+ {
+ // Obtain the string table index for the relocation symbol, look
+ // for it in the globals hash table to obtain information on that
+ // symbol. For the hash calculation to work correctly it must be
+ // placed in a 'clean' string before looking it up.
+ symidx = GetLong(symtab + ((rflg >> 8) * 12));
+ memset(sym, 0, SYMLEN);
+ strcpy(sym, symbols + symidx);
+ olddata = newdata = 0; // Initialise old and new segment data
+ ssidx = OSTLookup(sym);
+ newdata = GetLong(ost + ((ssidx - 1) * 12) + 8);
+ }
+
+ // Obtain the existing long word (or word) segment data and flip words
+ // if the relocation flags indicate it relates to a RISC MOVEI
+ // instruction
+ olddata = (wordreloc ? GetWord(sptr + addr) : GetLong(sptr + addr));
+
+ // If it's a OP QUAD relocation, get the data out of the DATA bits.
+ // Note that because we can't afford to lose the bottom 3 bits of the
+ // relocation record, we lose 3 off the top--which means the maximum
+ // this can store is $1FFFF8 (vs. $FFFFF8).
+ if (opreloc)
+ {
+ saveBits2 = (GetLong(sptr + addr + 8) & 0xE0000000) >> 8; // Upper 3 of data addr
+ saveBits = olddata & 0x7FF;
+ olddata = (olddata & 0xFFFFF800) >> 11;
+ olddata |= saveBits2; // Restore upper 3 bits of data addr
+ }
+
+ if (rflg & 0x01)
+ olddata = _SWAPWORD(olddata);
+
+ // Process record dependant on segment it relates to; TEXT, DATA or
+ // BSS. Construct a new relocated segment long word based on the
+ // required segment base address, the segment data offset in the
+ // resulting COF file and the offsets from the incoming object file.
+ swcond = (rflg & 0xFFFFFF00);
+
+ if (!glblreloc)
+ {
+ switch (swcond)
+ {
+ case 0x00000200: // Absolute Value
+ break;
+ case 0x00000400: // TEXT segment relocation record
+ // SCPCD : the symbol point to a text segment, we should use the textoffset
+ newdata = tbase + textoffset + olddata;
+
+ break;
+ case 0x00000600: // DATA segment relocation record
+ newdata = dbase + dataoffset
+ + (olddata - ofile->o_header.tsize);
+
+ break;
+ case 0x00000800: // BSS segment relocation record
+ newdata = bbase + bssoffset
+ + (olddata - (ofile->o_header.tsize
+ + ofile->o_header.dsize));
+
+ break;
+ }
+ }
+ else
+ {
+ if (!relreloc)
+ newdata += olddata;
+ }
+
+ // Set absolute (long) or relative (word) address of symbol
+ if (absreloc)
+ {
+ // Flip the new long word segment data if the relocation record
+ // indicated a RISC MOVEI instruction and place the resulting data
+ // back in the COF segment
+ if (rflg & 0x01)
+ newdata = _SWAPWORD(newdata);
+
+ if (wordreloc)
+ PutWord(sptr + addr, newdata);
+ else if (opreloc)
+ {
+ if (vflag > 1)
+ printf("OP reloc: oldata=$%X, newdata=$%X\n", olddata, newdata);
+
+ newdata = ((newdata & 0x00FFFFF8) << 8) | saveBits;
+ PutLong(sptr + addr, newdata);
+ // Strip out extraneous data hitchhikers from 2nd phrase...
+ newdata = GetLong(sptr + addr + 8) & 0x007FFFFF;
+ PutLong(sptr + addr + 8, newdata);
+ }
+ else
+ PutLong(sptr + addr, newdata);
+ }
+ else if (relreloc)
+ {
+ PutWord(sptr + addr, newdata - tbase - addr - ofile->o_tbase);
+ }
+
+ // Shamus: Let's output some info to aid in debugging this crap
+ if (vflag > 1)
+ {
+ char ssiString[128];
+ ssiString[0] = 0;
+
+ if (glblreloc)
+ sprintf(ssiString, " [ssi:%i]", ssidx);
+
+ printf("RelocateSegment($%08X): %s, $%08X: $%08X => $%08X%s\n", rflg, (glblreloc ? sym : "(LOCAL)"), addr, olddata, GetLong(sptr + addr), ssiString);
+ }
+
+ rptr += 8; // Point to the next relocation record
+ }
+
+ // Update the COF segment offset allowing for the phrase padding.
+ // SCPCD : we should not increment the textoffset before the end of processing the object file, else data section will point to wrong textoffset
+ if (flag == T_DATA)
+ {
+ // TEXT segment plus padding
+ pad = ((ofile->o_header.tsize + secalign) & ~secalign);
+ textoffset += (ofile->o_header.tsize + (pad - ofile->o_header.tsize));
+
+ // DATA segment plus padding
+ pad = ((ofile->o_header.dsize + secalign) & ~secalign);
+ dataoffset += (ofile->o_header.dsize + (pad - ofile->o_header.dsize));
+
+ // BSS segment plus padding
+ pad = ((ofile->o_header.bsize + secalign) & ~secalign);
+ bssoffset += (ofile->o_header.bsize + (pad - ofile->o_header.bsize));
+ }
+
+ // Return value, should always be zero
+ return 0;
+}
+
+
+//
+// Add a path character to the end of string 's' if it doesn't already end with
+// one. The last occurrance of '/' or '\' in the string is assumed to be the
+// path character.
+//
+// This is fucking shit. We know what the path delimiter is, its FUCKING
+// DEFINED IN THE FUCKING HEADER. FOR FUCKS SAKE. AND YES, HOPE TO GOD THERE'S
+// ENOUGH SPACE IN THE PASSED IN BUFFER TO HOLD THE EXTRA CHARACTER!
+//
+void AppendPathDelimiter(char * s)
+{
+#if 0
+ // And hope to God that there's enough space in the buffer...
+ char pathchar = 0;
+
+ while (*s)
+ {
+ if (*s == '/' || *s == '\\')
+ pathchar = *s;
+
+ s++;
+ }
+
+ s--;
+
+ if (*s == pathchar)
+ return;
+
+ *++s = pathchar;
+ *++s = 0;
+#else
+ int length = strlen(s);
+
+ if (s[length - 1] != PATH_DELIMITER)
+ {
+ s[length] = PATH_DELIMITER;
+ s[length + 1] = 0; // BUFFER OVERFLOW!!!! FFFFFFFFUUUUUUUUUUUU
+ }
+#endif
+}
+
+
+//
+// Try to open "name", "name.o", "${libdir}name", "${libdir}name.o". Return the
+// handle of the file successfully opened. p_name is updated to point to a
+// malloc()'ed string which is the name which actually got opened. p_name will
+// return unchanged if the file can't be found.
+//
+int TryOpenFile(char ** p_name)
+{
+ char * name = *p_name;
+
+ // Note that libdir will be an empty string if there is none specified
+ char * tmpbuf = malloc(strlen(name) + strlen(libdir) + 3);
+
+ if (tmpbuf == NULL)
+ {
+ printf("TryOpenFile() : out of memory\n");
+ return -1;
+ }
+
+ strcpy(tmpbuf, name);
+ int hasdot = (strrchr(tmpbuf, '.') > strrchr(tmpbuf, PATH_DELIMITER));
+ // Try to open file as passed first
+ int fd = open(tmpbuf, _OPEN_FLAGS);
+
+ if (fd >= 0)
+ goto ok;
+
+ if (!hasdot)
+ {
+ // Try to open file with '.o' added
+ strcat(tmpbuf, ".o");
+ fd = open(tmpbuf, _OPEN_FLAGS);
+
+ if (fd >= 0)
+ goto ok;
+ }
+
+ // Try the libdir only if the name isn't already anchored
+ // Shamus: WTH, this makes no sense... Why the ':'? Is this a Macintosh??
+// if (*name != '/' && *name != '\\' && !strchr(name, ':'))
+ if ((*name != PATH_DELIMITER) && (strchr(name, ':') == NULL))
+ {
+ strcpy(tmpbuf, libdir);
+ // Add a trailing path char if there isn't one already
+ AppendPathDelimiter(tmpbuf);
+ strcat(tmpbuf, name);
+
+ if ((fd = open(tmpbuf, _OPEN_FLAGS)) >= 0)
+ goto ok;
+
+ if (!hasdot)
+ {
+ strcat(tmpbuf, ".o");
+
+ if ((fd = open(tmpbuf, _OPEN_FLAGS)) >= 0)
+ goto ok;
+ }
+ }
+
+ // Couldn't open file at all
+ return -1;
+
+// There are worse things... :-P
+ok:
+ tmpbuf = realloc(tmpbuf, strlen(tmpbuf) + 1);
+
+ if (tmpbuf == NULL)
+ {
+ printf("TryOpenFile() : out of memory\n");
+ return -1;
+ }
+
+ *p_name = tmpbuf;
+ return fd;
+}
+
+
+//
+// What it says on the tin
+//
+void WriteARName(struct OFILE * p)
+{
+ int flag = *(p->o_arname);
+ printf("%s%s%s", (flag ? (char *)(p->o_arname) : ""), (flag ? ":" : ""), p->o_name);
+}
+
+
+//
+// Collect file names and handles in a buffer so there is less disk activity.
+// Call DoFile with flag FALSE for normal object files and archives.
+// Call it with flag TRUE and a symbol name for include files (-i).
+//
+int DoFile(char * fname, int incFlag, char * sym)
+{
+ // Verbose information
+ if (vflag)
+ {
+ printf("DoFile() : `%s' %s", fname, incFlag ? "INCLUDE" : "NORMAL");
+
+ if (incFlag)
+ printf(" symbol %s", sym);
+
+ printf("\n");
+ }
+
+ // Reached maximum file handles
+ if (hd == NHANDLES)
+ {
+ if (ProcessFiles())
+ return 1;
+ }
+
+ // Attempt to open input file
+ int fd = TryOpenFile(&fname);
+
+ if (fd < 0)
+ {
+ printf("Cannot find input module %s\n", fname);
+ return 1;
+ }
+
+ // The file is open; save its info in the handle and name arrays
+ handle[hd] = fd;
+ name[hd] = fname; // This is the name from TryOpenFile()
+ hflag[hd] = incFlag;
+
+ // Include files
+ if (incFlag)
+ {
+ int temp = strlen(sym); // Get symbol length
+
+ // 100 chars is max length of a symbol
+ if (temp > 99)
+ {
+ sym[99] = '\0';
+ temp = 99;
+ }
+
+ // Malloc enough space for two symbols, then build the second one.
+ // Second one may be one character longer than first
+ if ((hsym1[hd] = malloc((long)temp + 1)) == NULL
+ || (hsym2[hd] = malloc((long)temp + 2)) == NULL)
+ {
+ printf("DoFile() : out of memory for include-file symbols\n");
+ return 1;
+ }
+
+ strcpy(hsym1[hd], sym);
+ strcpy(hsym2[hd], sym);
+
+ if (temp == 99)
+ {
+ if (sym[99] == 'x')
+ {
+ printf("Last char of %s is already 'x': choose another name\n", sym);
+ return 1;
+ }
+
+ hsym2[hd][99] = 'x';
+ }
+ else
+ {
+ hsym2[hd][temp] = 'x';
+ hsym2[hd][temp+1] = '\0';
+ }
+ }
+
+ // Increment next handle index
+ hd++;
+ // No problems
+ return 0;
+}
+
+
+//
+// Pad TEXT or DATA segment to the requested boundary
+//
+int PadSegment(FILE * fd, long segsize, int value)
+{
+ int i;
+ char padarray[32];
+ char * padptr;
+
+ // Determine the number of padding bytes that are needed
+ long padsize = (segsize + secalign) & ~secalign;
+ padsize = padsize - segsize;
+
+ // Fill pad array if padding is required
+ if (padsize)
+ {
+ padptr = padarray;
+
+ for(i=0; i<16; i++)
+ {
+ PutWord(padptr, value);
+ padptr += 2;
+ }
+
+ symoffset += padsize;
+
+ // Write padding bytes
+ if (fwrite(padarray, padsize, 1, fd) != 1)
+ return 1;
+ }
+
+ return 0;
+}
+
+
+//
+// Write the output file
+//
+int WriteOutputFile(struct OHEADER * header)
+{
+ unsigned osize; // Object segment size
+ struct OFILE * otemp; // Object file pointer
+ int i, j; // Iterators
+ char himage[0x168]; // Header image (COF = 0xA8)
+ uint32_t tsoff, dsoff, bsoff; // Segment offset values
+ unsigned index, type, value; // Symbol table index, type and value
+ short abstype; // ABS symbol type
+ char symbol[14]; // Symbol record for ABS files
+ int slen; // Symbol string length
+
+ symoffset = 0; // Initialise symbol offset
+
+ // Add correct output extension if none
+ if (strchr(ofile, '.') == NULL)
+ {
+ if (aflag && cflag)
+ strcat(ofile, ".cof"); // COF files (a type of abs)
+ else if (aflag && !cflag)
+ strcat(ofile, ".abs"); // ABS files
+ else
+ strcat(ofile, ".o"); // Object files (partial linking, etc)
+ }
+
+ FILE * fd = fopen(ofile, "wb"); // Attempt to open output file
+
+ if (!fd)
+ {
+ printf("Can't open output file %s\n", ofile);
+ return 1;
+ }
+
+ // Build the output file header
+ // Absolute (COF) header
+ if (cflag)
+ {
+ tsoff = dsoff = bsoff = 0xA8; // Initialises segment offsets
+
+ // Process each object file segment size to obtain a cumulative segment
+ // size for both the TEXT and DATA segments
+ for(otemp=olist; otemp!=NULL; otemp=otemp->o_next)
+ {
+ dsoff += otemp->segSize[TEXT];
+ bsoff += otemp->segSize[TEXT] + otemp->segSize[DATA];
+ }
+
+ // Currently this only builds a COF absolute file. Conditionals and
+ // additional code will need to be added for ABS and partial linking.
+
+ // Build the COF_HDR
+ PutWord(himage + 0, 0x0150 ); // Magic Number (0x0150)
+ PutWord(himage + 2, 0x0003 ); // Sections Number (3)
+ PutLong(himage + 4, 0x00000000 ); // Date (0L)
+ PutLong(himage + 8, dsoff + header->dsize); // Offset to Symbols Section
+ PutLong(himage + 12, ost_index); // Number of Symbols
+ PutWord(himage + 16, 0x001C ); // Size of RUN_HDR (0x1C)
+ PutWord(himage + 18, 0x0003 ); // Executable Flags (3)
+
+ // Build the RUN_HDR
+ PutLong(himage + 20, 0x00000107 ); // Magic/vstamp
+ PutLong(himage + 24, header->tsize ); // TEXT size in bytes
+ PutLong(himage + 28, header->dsize ); // DATA size in bytes
+ PutLong(himage + 32, header->bsize ); // BSS size in bytes
+ PutLong(himage + 36, tbase ); // Start of executable, normally @TEXT
+ PutLong(himage + 40, tbase ); // @TEXT
+ PutLong(himage + 44, dbase ); // @DATA
+
+ // Build the TEXT SEC_HDR
+ PutLong(himage + 48, 0x2E746578 );
+ PutLong(himage + 52, 0x74000000 ); // ".text"
+ PutLong(himage + 56, tbase ); // TEXT START
+ PutLong(himage + 60, tbase ); // TEXT START
+ PutLong(himage + 64, header->tsize ); // TEXT size in bytes
+ PutLong(himage + 68, tsoff ); // Offset to section data in file
+ PutLong(himage + 72, 0x00000000 ); // Offset to section reloc in file (0L)
+ PutLong(himage + 76, 0x00000000 ); // Offset to debug lines structures (0L)
+ PutLong(himage + 80, 0x00000000 ); // Nreloc/nlnno (0L)
+ PutLong(himage + 84, 0x00000020 ); // SEC_FLAGS: STYP_TEXT
+
+ // Build the DATA SEC_HDR
+ PutLong(himage + 88, 0x2E646174 );
+ PutLong(himage + 92, 0x61000000 ); // ".data"
+ PutLong(himage + 96, dbase ); // DATA START
+ PutLong(himage + 100, dbase ); // DATA START
+ PutLong(himage + 104, header->dsize ); // DATA size in bytes
+ PutLong(himage + 108, dsoff ); // Offset to section data in file
+ PutLong(himage + 112, 0x00000000 ); // Offset to section reloc in file (0L)
+ PutLong(himage + 116, 0x00000000 ); // Offset to debugging lines structures (0L)
+ PutLong(himage + 120, 0x00000000 ); // Nreloc/nlnno (0L)
+ PutLong(himage + 124, 0x00000040 ); // SEC_FLAGS: STYP_DATA
+
+ // Build the BSS SEC_HDR
+ PutLong(himage + 128, 0x2E627373 );
+ PutLong(himage + 132, 0x00000000 ); // ".bss"
+ PutLong(himage + 136, bbase ); // BSS START
+ PutLong(himage + 140, bbase ); // BSS START
+ PutLong(himage + 144, header->bsize ); // BSS size in bytes
+ PutLong(himage + 148, bsoff ); // Offset to section data in file
+ PutLong(himage + 152, 0x00000000 ); // Offset to section reloc in file (0L)
+ PutLong(himage + 156, 0x00000000 ); // Offset to debugging lines structures (0L)
+ PutLong(himage + 160, 0x00000000 ); // Nreloc/nlnno (0L)
+ PutLong(himage + 164, 0x00000080 ); // SEC_FLAGS: STYP_BSS
+
+ symoffset = 168; // Update symbol offset
+ }
+ // Absolute (ABS) header
+ else
+ {
+ // Build the ABS header
+ PutWord(himage + 0, 0x601B ); // Magic Number (0x601B)
+ PutLong(himage + 2, header->tsize ); // TEXT segment size
+ PutLong(himage + 6, header->dsize ); // DATA segment size
+ PutLong(himage + 10, header->bsize ); // BSS segment size
+ PutLong(himage + 14, ost_index * 14 ); // Symbol table size (?)
+ PutLong(himage + 18, 0x00000000 ); //
+ PutLong(himage + 22, tbase ); // TEXT base address
+ PutWord(himage + 26, 0xFFFF ); // Flags (?)
+ PutLong(himage + 28, dbase ); // DATA base address
+ PutLong(himage + 32, bbase ); // BSS base address
+
+ symoffset = 36; // Update symbol offset
+ }
+
+ // Write the header, but not if noheaderflag
+ if (!noheaderflag)
+ {
+ // Absolute (ABS) header
+ if (!cflag)
+ {
+ if (fwrite(himage, 36, 1, fd) != 1)
+ goto werror;
+ }
+ // Absolute (COF) header
+ else
+ {
+ if (fwrite(himage, 168, 1, fd) != 1)
+ goto werror;
+ }
+ }
+
+ // Write the TEXT segment of each object file
+ for(otemp=olist; otemp!=NULL; otemp=otemp->o_next)
+ {
+ osize = otemp->o_header.tsize;
+
+ // Write only if segment has size
+ if (osize)
+ {
+ if (vflag > 1)
+ printf("Writing TEXT Segment of %s\n", otemp->o_name);
+
+ if (fwrite(otemp->o_image + 32, osize, 1, fd) != 1)
+ goto werror;
+
+ // Pad to required alignment boundary
+ if (PadSegment(fd, osize, 0x0000))
+ goto werror;
+
+ symoffset += osize;
+ }
+ }
+
+ // Write the DATA segment of each object file
+ for(otemp=olist; otemp!=NULL; otemp=otemp->o_next)
+ {
+ osize = otemp->o_header.dsize;
+
+ // Write only if the segment has size
+ if (osize)
+ {
+ if (vflag > 1)
+ printf("Writing DATA Segment of %s\n", otemp->o_name);
+
+ if (fwrite((otemp->o_image + 32 + otemp->o_header.tsize), osize, 1, fd) != 1)
+ goto werror;
+
+ // Pad to required alignment boundary
+ if (PadSegment(fd, osize, 0))
+ goto werror;
+
+ symoffset += osize;
+ }
+ }
+
+//wha? if (!noheaderflag)
+ // This isn't quite right, but it's closer...
+ // (the l and s flags tell the linker to output local & global symbols
+ // in the symbol table, so it seems there's some other thing that's a
+ // default set somewhere. Not 100% sure. Setting -s kills -l, BTW...)
+ if (lflag || sflag)
+ {
+ // Write the symbols table and string table
+ // Absolute (COF) symbol/string table
+ if (cflag)
+ {
+ if (header->ssize)
+ {
+ if (fwrite(ost, (ost_ptr - ost), 1, fd) != 1)
+ goto werror;
+
+ if (fwrite(oststr, (oststr_ptr - oststr), 1, fd) != 1)
+ goto werror;
+ }
+ }
+ // Absolute (ABS) symbol/string table
+ else
+ {
+ // The symbol and string table have been created as part of the
+ // DoSymbols() function and the output symbol and string tables are
+ // in COF format. For an ABS file we need to process through this
+ // to create the 14 character long combined symbol and string
+ // table. Format of symbol table in ABS: AAAAAAAATTVVVV, where
+ // (A)=STRING, (T)=TYPE & (V)=VALUE
+
+ for(i=0; i<ost_index; i++)
+ {
+ memset(symbol, 0, 14); // Initialise symbol record
+ abstype = 0; // Initialise ABS symbol type
+ slen = 0; // Initialise symbol string length
+ index = GetLong(ost + (i * 12)); // Get symbol index
+ type = GetLong((ost + (i * 12)) + 4); // Get symbol type
+
+ // Skip debug symbols
+ if (type & 0xF0000000)
+ continue;
+
+ // Get symbol value
+ value = GetLong((ost + (i * 12)) + 8);
+ slen = strlen(oststr + index);
+
+ // Get symbol string (maximum 8 chars)
+ if (slen > 8)
+ {
+ for(j=0; j<8; j++)
+ *(symbol + j) = *(oststr + index + j);
+ }
+ else
+ {
+ for(j=0; j<slen; j++)
+ *(symbol + j) = *(oststr + index + j);
+ }
+
+ // Modify to ABS symbol type
+ switch (type)
+ {
+ case 0x02000000: abstype = (short)ABST_DEFINED; break;
+ case 0x04000000: abstype = (short)ABST_DEFINED | ABST_TEXT; break;
+ case 0x05000000: abstype = (short)ABST_DEFINED | ABST_GLOBAL | ABST_TEXT; break;
+ case 0x06000000: abstype = (short)ABST_DEFINED | ABST_DATA; break;
+ case 0x07000000: abstype = (short)ABST_DEFINED | ABST_GLOBAL | ABST_DATA; break;
+ case 0x08000000: abstype = (short)ABST_DEFINED | ABST_BSS; break;
+ case 0x09000000: abstype = (short)ABST_DEFINED | ABST_GLOBAL | ABST_BSS; break;
+ default:
+ printf("warning (WriteOutputFile): ABS, cannot determine symbol type ($%08X) [%s]\n", type, symbol);
+// type = 0;
+ break;
+ }
+
+ PutWord(symbol + 8, abstype); // Write back new ABS type
+ PutLong(symbol + 10, value); // Write back value
+
+ // Write symbol record
+ if (fwrite(symbol, 14, 1, fd) != 1)
+ goto werror;
+ }
+ }
+ }
+
+ if (fclose(fd))
+ {
+ printf("Close error on output file %s\n", ofile);
+ return 1;
+ }
+
+ return 0;
+
+werror:
+ printf("Write error on output file %s\n", ofile);
+ fclose(fd); // Try to close output file anyway
+ return 1;
+}
+
+
+//
+// Display the symbol load map
+//
+int ShowSymbolLoadMap(struct OHEADER * header)
+{
+ unsigned i, o; // Inner and outer loop iterators
+ unsigned c; // Column number
+ unsigned index; // Symbol string index
+ unsigned type; // Symbol type
+ unsigned value; // Symbol value
+ char * symbol; // Symbol string value
+
+ if (ost_index == 0)
+ return 0; // Return if no symbols to map
+
+ printf("LOAD MAP\n\n");
+
+ // Outer loop for each of the symbol areas to map out;
+ // 0 = NON-RELOCATABLE SYMBOLS
+ // 1 = TEXT-SEGMENT RELOCATABLE SYMBOLS
+ // 2 = DATA-SEGMENT RELOCATABLE SYMBOLS
+ // 3 = BSS-SEGMENT RELOCATABLE SYMBOLS
+ for(o=0; o<4; o++)
+ {
+ // Display the correct map header for the symbols being processed
+ switch (o)
+ {
+ case 0: printf("NON-RELOCATABLE SYMBOLS\n\n"); break;
+ case 1: printf("TEXT-SEGMENT RELOCATABLE SYMBOLS\n\n"); break;
+ case 2: printf("DATA-SEGMENT RELOCATABLE SYMBOLS\n\n"); break;
+ case 3: printf("BSS-SEGMENT RELOCATABLE SYMBOLS\n\n"); break;
+ }
+
+ c = 0; // Initialise column number
+
+ // Inner loop to process each record in the symbol table
+ for(i=0; i<(unsigned)ost_index; i++)
+ {
+ index = GetLong(ost + (i * 12)); // Get symbol string index
+ type = GetLong(ost + (i * 12) + 4); // Get symbol type
+ value = GetLong(ost + (i * 12) + 8); // Get symbol value
+ symbol = oststr + index; // Get symbol string
+
+ // Display only three columns
+ if (c == 3)
+ {
+ printf("\n");
+ c = 0;
+ }
+
+ // If local symbols not included and the type is local then go to
+ // next symbol record
+ if (!lflag & !(type & 0x01000000))
+ continue;
+
+ // Output each symbol to the display, dependant on type
+ switch (o)
+ {
+ case 0:
+ // Non-relocatable symbols
+ if (type == 0x02000000 || type == 0x03000000)
+ {
+ printf("%-8s %c %08X ", symbol, (type & 0x01000000) ? 'G' : 'L', value);
+ c++;
+ }
+
+ break;
+ case 1:
+ // TEXT segment relocatable symbols
+ if (type == 0x04000000 || type == 0x05000000)
+ {
+ printf("%-8s %c %08X ", symbol, (type & 0x01000000) ? 'G' : 'L', value);
+ c++;
+ }
+
+ break;
+ case 2:
+ // DATA segment relocatble symbols
+ if (type == 0x06000000 || type == 0x07000000)
+ {
+ printf("%-8s %c %08X ", symbol, (type & 0x01000000) ? 'G' : 'L', value);
+ c++;
+ }
+
+ break;
+ case 3:
+ // BSS segment relocatable symbols
+ if (type == 0x08000000 || type == 0x09000000)
+ {
+ printf("%-8s %c %08X ", symbol, (type & 0x01000000) ? 'G' : 'L', value);
+ c++;
+ }
+
+ break;
+ }
+ }
+
+ printf("\n\n");
+ }
+
+ return 0;
+}
+
+
+//
+// Stuff the (long) value of a string into the value argument. RETURNS TRUE if
+// the string doesn't parse. Parses only as a hexadecimal string.
+//
+int GetHexValue(char * string, int * value)
+{
+ *value = 0;
+
+ while (isxdigit(*string))
+ {
+ if (isdigit(*string))
+ {
+ *value = (*value << 4) + (*string++ - '0');
+ }
+ else
+ {
+ if (isupper(*string))
+ *string = tolower(*string);