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
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 obj_fname[2048][FNLEN]; // Object file names
-unsigned obj_segsize[2048][3]; // Object file seg sizes; TEXT,DATA,BSS
+char obj_fname[16384][FNLEN]; // Object file names
+unsigned obj_segsize[16384][3]; // Object file seg sizes; TEXT,DATA,BSS
unsigned obj_index = 0; // Object file index/count
char * arPtr[512];
uint32_t arIndex = 0;
struct HREC * htable[NBUCKETS]; // Hash table
struct HREC * unresolved = NULL; // Pointer to unresolved hash list
unsigned obj_index = 0; // Object file index/count
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 * ost; // Output symbol table
char * ost_ptr; // Output symbol table; current pointer
char * ost_end; // Output symbol table; end pointer
char * oststr_end; // Output string table; end pointer
int ost_index = 0; // Index of next ost addition
int endian; // Processor endianess
char * oststr_end; // Output string table; end pointer
int ost_index = 0; // Index of next ost addition
int endian; // Processor endianess
- int ssidx; // Segment size table index
- unsigned tsegoffset; // Cumulative TEXT segment offset
- unsigned dsegoffset; // Cumulative DATA segment offset
- unsigned bsegoffset; // Cumulative BSS segment offset
struct HREC * hptr; // Hash table pointer for globl/extrn
char sym[SYMLEN]; // String for symbol name/hash search
// Point to first symbol record in the object file
struct HREC * hptr; // Hash table pointer for globl/extrn
char sym[SYMLEN]; // String for symbol name/hash search
// Point to first symbol record in the object file
+ 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
+ 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
// Search through object segment size table to accumulated segment sizes to
// ensure the correct offsets are used in the resulting COF file.
// Search through object segment size table to accumulated segment sizes to
// ensure the correct offsets are used in the resulting COF file.
// Search for object file name
for(j=0; j<(int)obj_index; j++)
{
if (!strcmp(ofile->o_name, obj_fname[j]))
{
// Search for object file name
for(j=0; j<(int)obj_index; j++)
{
if (!strcmp(ofile->o_name, obj_fname[j]))
{
- index = getlong(symptr + 0); // Obtain symbol string index
- type = getlong(symptr + 4); // Obtain symbol type
- value = getlong(symptr + 8); // Obtain symbol value
+ 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
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
// Global/External symbols have a pre-processing stage
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
// accumulated to ensure the correct offsets are used in the
// resulting COF file. This is effectively 'done again' only as we
// are working with a different object file.
// accumulated to ensure the correct offsets are used in the
// resulting COF file. This is effectively 'done again' only as we
// are working with a different object file.
- if (islocal(type) || isglobal(type))
- {
- if ((index = ost_add(symend + index, type, value)) == -1)
- return 1;
- }
- else
- {
- // Belongs in OST, but don't look it up yet
- index = -1;
- }
+ index = OSTAdd(symend + index, type, value);
+
+ if (index == -1)
+ return 1;
-// Add all Global and External Symbols to the Output Symbol Table
+// 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.]
for(i=0; i<NBUCKETS; i++)
{
for(hptr=htable[i]; hptr!=NULL; hptr=hptr->h_next)
{
//NO! if (iscommon(hptr->h_type))
for(i=0; i<NBUCKETS; i++)
{
for(hptr=htable[i]; hptr!=NULL; hptr=hptr->h_next)
{
//NO! if (iscommon(hptr->h_type))
- if (hptr->h_type == 0x03000000)
- hptr->h_type = 0x02000000; // Absolutes can't be externals
+// Skip if in *.a file... (does nothing)
+//if (hptr->h_ofile->isArchiveFile)
+// continue;
+
+ if (hptr->h_type == (T_EXT | T_ABS))
+ hptr->h_type = T_ABS; // Absolutes *can't* be externals
// Add a Symbol's Name, Type, and Value to the OST.
// Return the Index of the Symbol in OST, or -1 for Error.
//
// Add a Symbol's Name, Type, and Value to the OST.
// Return the Index of the Symbol in OST, or -1 for Error.
//
- int ost_offset_p, ost_offset_e = 0; // OST table offsets for position calcs
- int slen = 0; // Symbol string length
- int ostresult; // OST index result
+ int ost_offset_p, ost_offset_e = 0; // OST table offsets for position calcs
+ int slen = 0; // Symbol string length
+ int ostresult; // OST index result
// 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
// 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
- printf("ost_add: (%s), type=$%08X, val=$%08X\n", name, type, value);
+ printf("OSTAdd: (%s), type=$%08X, val=$%08X\n", name, type, value);
- htemp = hptr->h_next; // Temporarily get ptr to next record
- free(hptr); // Free current record
- hptr = htemp; // Make next record ptr, current
+ struct HREC * htemp = hptr->h_next;
+ free(hptr);
+ hptr = htemp;
// in an OFILE header and flag (T_TEXT, T_DATA) to process. Return (0) is
// successful or non-zero (1) if failed.
//
// in an OFILE header and flag (T_TEXT, T_DATA) to process. Return (0) is
// successful or non-zero (1) if failed.
//
ofile->o_name, flag == T_DATA ? "DATA" : "TEXT");
}
// Obtain pointer to start of symbol table
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);
+ 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);
- 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
+ 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
symidx = getlong(symtab + ((rflg >> 8) * 12));
memset(sym, 0, SYMLEN);
strcpy(sym, symbols + symidx);
symidx = getlong(symtab + ((rflg >> 8) * 12));
memset(sym, 0, SYMLEN);
strcpy(sym, symbols + symidx);
// 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.
// 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.
- case 0x00000400: // TEXT segment relocation record
-//AARRRGGGGHHHHH! does the else belong to the 1st 'if' or the 2nd?
-// can we trust the indention to tell the truth here???
-// Braces were not here, so if something breaks, try pairing the else to the 1st 'if'...
- if (!glblreloc)
- {
- if (flag == T_TEXT) // Is this a TEXT section record?
- newdata = tbase + textoffset + olddata;
- else
- newdata = tbase + dataoffset + olddata; // Nope, must be DATA section
- }
+ case 0x00000400: // TEXT segment relocation record
+ if (flag == T_TEXT) // Is this a TEXT section record?
+ newdata = tbase + textoffset + olddata;
+ else
+ newdata = tbase + dataoffset + olddata; // Nope, must be DATA section
- case 0x00000600: // DATA segment relocation record
- if (!glblreloc)
- newdata = dbase + dataoffset + (olddata - ofile->o_header.tsize);
+ case 0x00000600: // DATA segment relocation record
+ newdata = dbase + dataoffset
+ + (olddata - ofile->o_header.tsize);
- case 0x00000800: // BSS segment relocation record
- if (!glblreloc)
- newdata = bbase + bssoffset
- + (olddata - (ofile->o_header.tsize + ofile->o_header.dsize));
+ case 0x00000800: // BSS segment relocation record
+ newdata = bbase + bssoffset
+ + (olddata - (ofile->o_header.tsize
+ + ofile->o_header.dsize));
- printf("reloc_segment($%08X): %s, $%08X: $%08X => $%08X%s\n", rflg, (glblreloc ? sym : "(LOCAL)"), addr, olddata, getlong(sptr + addr), ssiString);
+ printf("RelocateSegment($%08X): %s, $%08X: $%08X => $%08X%s\n", rflg, (glblreloc ? sym : "(LOCAL)"), addr, olddata, getlong(sptr + addr), ssiString);
+// 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)
// malloc()'ed string which is the name which actually got opened. p_name will
// return unchanged if the file can't be found.
//
// malloc()'ed string which is the name which actually got opened. p_name will
// return unchanged if the file can't be found.
//
- hasdot = ((lastdot = strrchr(tmpbuf, '.')) > strrchr(tmpbuf, '/'))
- && (lastdot > strrchr(tmpbuf, '\\'));
+ int hasdot = (strrchr(tmpbuf, '.') > strrchr(tmpbuf, PATH_DELIMITER));
+ // Try to open file as passed first
+ int fd = open(tmpbuf, _OPEN_FLAGS);
- if (*name != '/' && *name != '\\' && !strchr(name, ':'))
+ // Shamus: WTH, this makes no sense... Why the ':'? Is this a Macintosh??
+// if (*name != '/' && *name != '\\' && !strchr(name, ':'))
+ if ((*name != PATH_DELIMITER) && (strchr(name, ':') == NULL))
int i; // Good 'ol iterator
char padarray[32]; // Array of padding bytes
char * padptr; // Pointer to array
// Determine the number of padding bytes that are needed
int i; // Good 'ol iterator
char padarray[32]; // Array of padding bytes
char * padptr; // Pointer to array
// Determine the number of padding bytes that are needed
- if (fwrite(padarray, padsize, 1, fd) != 1) // Write padding bytes
+ // Write padding bytes
+ if (fwrite(padarray, padsize, 1, fd) != 1)
- 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)
+ 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)
- 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
+ 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 );
// 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
+ 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 );
// 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
+ 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
- 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
+ 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
- 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
+ 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
- if (fwrite(ost, (ost_ptr - ost), 1, fd) != 1) goto werror;
- if (fwrite(oststr, (oststr_ptr - oststr), 1, fd) != 1) goto werror;
+ 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
}
}
// Absolute (ABS) symbol/string table
else
{
// The symbol and string table have been created as part of the
- // dosym() 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
+ // DoSymbol() 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
- memset(symbol, 0, 14); // Initialise symbol record
- abstype = 0; // Initialise ABS symbol type
- slen = 0; // Initialise symbol string length
+ 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
index = getlong(ost + (i * 12)); // Get symbol index
type = getlong((ost + (i * 12)) + 4); // Get symbol type
- putword(symbol + 8, abstype); // Write back new ABS type
- putlong(symbol + 10, value); // Write back value
+ putword(symbol + 8, abstype); // Write back new ABS type
+ putlong(symbol + 10, value); // Write back value
- if (fwrite(symbol, 14, 1, fd) != 1) goto werror; // Write symbol record
+ // Write symbol record
+ if (fwrite(symbol, 14, 1, fd) != 1)
+ goto werror;
-//int atolx(char * string, long * value)
-int atolx(char * string, int * value)
+int GetHexValue(char * string, int * value)
-//
-// Stuff the (long) value of a string into the value argument. RETURNS TRUE if
-// the string doesn't parse. Parses only as a hex string.
-//
-int getval(char * string, int * value)
-{
- return atolx(string, value);
-}
-
-
//
// Create one big .o file from the images already in memory, returning a
// pointer to an OHEADER. Note that the oheader is just the header for the
//
// Create one big .o file from the images already in memory, returning a
// pointer to an OHEADER. Note that the oheader is just the header for the
{
unsigned tptr, dptr, bptr; // Bases in runtime model
int ret = 0; // Return value
{
unsigned tptr, dptr, bptr; // Bases in runtime model
int ret = 0; // Return value
// For each object file, accumulate the sizes of the segments but remove
// those object files which are unused
// For each object file, accumulate the sizes of the segments but remove
// those object files which are unused
- oprev = NULL; // Init previous obj file list ptr
- otemp = olist; // Set temp pointer to object file list
+ struct OFILE * oprev = NULL; // Init previous obj file list ptr
+ struct OFILE * otemp = olist; // Set temp pointer to object file list
+ int i = 0;
+// free(ohold);
+
+ // Also need to remove them from the obj_* tables too :-P
+ // N.B.: Would probably be worthwhile to remove crap like this
+ // and stuff it into the OFILE structure...
+ if (wflag)
+ printf("»-> removing %s... (index == %i, len == %i)\n", obj_fname[i], i, obj_index - 1);
+
+ int k;
+
+ for(k=i; k<obj_index-1; k++)
+ {
+ memcpy(obj_fname[k], obj_fname[k + 1], FNLEN);
+ obj_segsize[k][0] = obj_segsize[k + 1][0];
+ obj_segsize[k][1] = obj_segsize[k + 1][1];
+ obj_segsize[k][2] = obj_segsize[k + 1][2];
+ }
+
+ obj_index--;
+ i--;
textsize += (otemp->o_header.tsize + secalign) & ~secalign;
datasize += (otemp->o_header.dsize + secalign) & ~secalign;
bsssize += (otemp->o_header.bsize + secalign) & ~secalign;
textsize += (otemp->o_header.tsize + secalign) & ~secalign;
datasize += (otemp->o_header.dsize + secalign) & ~secalign;
bsssize += (otemp->o_header.bsize + secalign) & ~secalign;
// For each symbol, (conditionally) add it to the ost
// For ARCHIVE markers, this adds the symbol for the file & returns
// For each symbol, (conditionally) add it to the ost
// For ARCHIVE markers, this adds the symbol for the file & returns
- header->magic = 0x0150; // COF magic number
- header->tsize = textsize; // TEXT segment size
- header->dsize = datasize; // DATA segment size
- header->bsize = bsssize; // BSS segment size
- header->ssize = (ost_ptr - ost); // Symbol table size
- header->ostbase = ost; // Output symbol table base address
+ header->magic = 0x0150; // COF magic number
+ header->tsize = textsize; // TEXT segment size
+ header->dsize = datasize; // DATA segment size
+ header->bsize = bsssize; // BSS segment size
+ header->ssize = (ost_ptr - ost); // Symbol table size
+ header->ostbase = ost; // Output symbol table base address
// For each object file, relocate its TEXT and DATA segments. OR the result
// into ret so all files get moved (and errors reported) before returning
// For each object file, relocate its TEXT and DATA segments. OR the result
// into ret so all files get moved (and errors reported) before returning
- ret |= reloc_segment(otemp, T_TEXT); // TEXT segment relocations
- ret |= reloc_segment(otemp, T_DATA); // DATA segment relocations
+ ret |= RelocateSegment(otemp, T_TEXT); // TEXT segment relocations
+ ret |= RelocateSegment(otemp, T_DATA); // DATA segment relocations
-int add_to_hlist(struct HREC ** hptr, char * sym, struct OFILE * ofile, long value, int type)
+int AddSymbolToHashList(struct HREC ** hptr, char * sym, struct OFILE * ofile,
+ long value, int type)
- printf("add_to_hlist(): hptr=$%08X, sym=\"%s\", ofile=$%08X, value=$%X, type=$%X\n", hptr, sym, ofile, value, type);
+ printf("AddSymbolToHashList(): hptr=$%08X, sym=\"%s\", ofile=$%08X, value=$%X, type=$%X\n", hptr, sym, ofile, value, type);
- for(i=0; i<SYMLEN; i++)
- htemp->h_sym[i] = '\0';
-
- strcpy(htemp->h_sym, sym); // Populate hash record
+ // Populate hash record
+ memset(htemp->h_sym, 0, SYMLEN);
+ strcpy(htemp->h_sym, sym);
- printf("add_unresolved(%s, %s)\n", sym, ofile->o_name);
+ printf("AddUnresolvedSymbol(%s, %s)\n", sym, ofile->o_name);
- return add_to_hlist(&unresolved, sym, ofile, 0L, 0);
+ return AddSymbolToHashList(&unresolved, sym, ofile, 0L, 0);
- int i = (s[0] + s[1] + s[2] + s[3] + s[4] + s[5] + s[6] + s[7] + s[8]
- + s[9] + s[10] + s[11] + s[12] + s[13] + s[14]) % NBUCKETS;
- return i;
+ struct HREC * ptr = unresolved;
+ struct HREC * previous = NULL;
+
+ while ((ptr != hrec) && (ptr != NULL))
+ {
+ previous = ptr;
+ ptr = ptr->h_next;
+ }
+
+ // Not found...!
+ if (ptr == NULL)
+ return NULL;
+
+ struct HREC * next = ptr->h_next;
+
+ // Remove the head if nothing previous, otherwise, remove what we found
+ if (previous == NULL)
+ unresolved = next;
+ else
+ previous->h_next = next;
+
+ free(ptr);
+ return next;
+}
+
+
+//
+// Add symbol to the unresolved symbols hash table
+//
+int AddARSymbol(char * sym, struct OFILE * ofile)
+{
+ if (vflag > 1)
+ printf("AddARSymbol(%s, %s)\n", sym, ofile->o_name);
+
+ return AddSymbolToHashList(&arSymbol, sym, ofile, 0L, 0);
- struct HREC * hptr = htable[DoHash(sym)]; // Hash index to record based on sym
- char symbol[SYMLEN]; // Temporary symbol storage
+ // For this to be consistent, the symbol MUST be zeroed out beforehand!
+ char c[SYMLEN];
+ memset(c, 0, SYMLEN);
+ strcpy(c, s);
+
+ int i = (c[0] + c[1] + c[2] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8]
+ + c[9] + c[10] + c[11] + c[12] + c[13] + c[14]) % NBUCKETS;
+ return i;
+}
- memset(symbol, 0, SYMLEN); // Clean string for comparison
- strcpy(symbol, sym);
+
+//
+// Lookup a symbol in the hash table.
+// Returns either a pointer to the HREC or NULL if not found.
+//
+struct HREC * LookupHREC(char * symbol)
+{
+ struct HREC * hptr = htable[GetHash(symbol)];
while (hptr != NULL)
{
//This is utter failure...
// if (symcmp(symbol, hptr->h_sym)) <-- left here for giggles :D - LinkoVitch
while (hptr != NULL)
{
//This is utter failure...
// if (symcmp(symbol, hptr->h_sym)) <-- left here for giggles :D - LinkoVitch
- printf("hash_add(%s,%s,%lx,", sym, ofile->o_name, value);
- printf("%x,%s)\n", (unsigned int)type, (flag ? "GLOBAL" : "COMMON"));
+ // Return hash record pointer if found
+ if (strcmp(symbol, hptr->h_sym) == 0)
+ return hptr;
+
+ hptr = hptr->h_next;
- if ((hptr = lookup(sym)) == NULL)
+ return NULL;
+}
+
+
+//
+// Add symbol to the hash table if it doesn't already exist, otherwise decide
+// what to do since it's already defined in another unit.
+//
+int DealWithSymbol(char * sym, long type, long value, struct OFILE * ofile)
+{
+ if (vflag > 1)
- return add_to_hlist(&htable[DoHash(sym)], sym, ofile, value, type);
+ printf("DealWithSymbol(%s,%s,%lx,", sym, ofile->o_name, value);
+ printf("%x,%s)\n", (unsigned int)type, (isglobal(type) ? "GLOBAL" : "COMMON"));
+ struct HREC * hptr = LookupHREC(sym);
+
+ if (hptr == NULL)
+ return AddSymbolToHashList(&htable[GetHash(sym)], sym, ofile, value, type);
+
+ // Symbol is already in table... Figure out how to handle!
if (iscommon(type) && !iscommon(hptr->h_type))
{
// Mismatch: global came first; warn and keep the global one
if (iscommon(type) && !iscommon(hptr->h_type))
{
// Mismatch: global came first; warn and keep the global one
- long nsymbols; // Number of symbols in object file
- char * ptr; // Object data base pointer
- char * sfix; // Symbol fixup table pointer
- char * sstr; // Symbol string table pointer
- long index; // String index
- long type; // Symbol type
- long value; // Symbol value
struct HREC * hptr; // Hash record pointer
char symbol[SYMLEN];
if (vflag > 1)
printf("Add symbols for file %s\n", Ofile->o_name);
struct HREC * hptr; // Hash record pointer
char symbol[SYMLEN];
if (vflag > 1)
printf("Add symbols for file %s\n", Ofile->o_name);
- sfix = ptr; // Set symbol fixup pointer
- sstr = sfix + Ofile->o_header.ssize; // Set symbol table pointer
- nsymbols = Ofile->o_header.ssize / 12; // Obtain number of symbols
+ char * sfix = ptr; // Set symbol fixup pointer
+ char * sstr = sfix + Ofile->o_header.ssize; // Set symbol table pointer
+ long nsymbols = Ofile->o_header.ssize / 12; // Obtain number of symbols
- index = getlong(sfix); // Get symbol string index
- type = getlong(sfix + 4); // Get symbol type
- value = getlong(sfix + 8); // Get symbol value
+ long index = getlong(sfix); // Get symbol string index
+ long type = getlong(sfix + 4); // Get symbol type
+ long value = getlong(sfix + 8); // Get symbol value
memset(symbol, 0, SYMLEN);
strcpy(symbol, sstr + index);
memset(symbol, 0, SYMLEN);
strcpy(symbol, sstr + index);
- if ((type - T_EXT))
- {
- // Then add to hash table
- if (hash_add(symbol, type, value, Ofile))
- {
- return 1; // Error if addition failed
- }
- }
- else
- {
- // If value is zero and in hash table
- if ((hptr = lookup(symbol)) != NULL)
- {
- hptr->h_ofile->o_flags |= O_USED; // Mark symbol as used
- }
- // Otherwise add to unresolved list
- else if (add_unresolved(symbol, Ofile))
- {
- return 1; // Error if addition failed
- }
- }
+ if ((type & T_EXT) && (type & (T_SEG | T_ABS)))
+ if (AddARSymbol(symbol, Ofile))
+ return 1;
- sfix += 12; // Increment symbol fixup pointer
- nsymbols--; // Decrement num of symbols to process
+ if (hptr != NULL)
+ hptr->h_ofile->o_flags |= O_USED; // Mark .o file as used
+ // Otherwise add to unresolved list
+ else if (AddUnresolvedSymbol(symbol, Ofile))
+ return 1; // Error if addition failed
+ }
+ else if ((type & T_EXT) && (type & (T_SEG | T_ABS)))
+ {
+ // Symbol in the current unit that is also EXPORTED
+ if (DealWithSymbol(symbol, type, value, Ofile))
+ return 1; // Error if addition failed
+ }
+
+ sfix += 12; // Increment symbol fixup pointer
+ nsymbols--; // Decrement num of symbols to process
- Ofile->o_flags = flags;
- Ofile->o_image = ptr;
- Ofile->isArchiveFile = 0;
+ Ofile->o_flags = obj->o_flags;
+ Ofile->o_image = obj->o_image;
+ Ofile->isArchiveFile = obj->isArchiveFile;
+ char * ptr = obj->o_image;
- Ofile->o_header.tsize = getlong(ptr+4);
- Ofile->o_header.dsize = getlong(ptr+8);
- Ofile->o_header.bsize = getlong(ptr+12);
- Ofile->o_header.ssize = getlong(ptr+16);
- Ofile->o_header.absrel.reloc.tsize = getlong(ptr+24);
- Ofile->o_header.absrel.reloc.dsize = getlong(ptr+28);
+ Ofile->o_header.tsize = getlong(ptr + 4);
+ Ofile->o_header.dsize = getlong(ptr + 8);
+ Ofile->o_header.bsize = getlong(ptr + 12);
+ Ofile->o_header.ssize = getlong(ptr + 16);
+ Ofile->o_header.absrel.reloc.tsize = getlong(ptr + 24);
+ Ofile->o_header.absrel.reloc.dsize = getlong(ptr + 28);
// Round BSS off to alignment boundary
Ofile->o_header.bsize = (Ofile->o_header.bsize + secalign) & ~secalign;
// Round BSS off to alignment boundary
Ofile->o_header.bsize = (Ofile->o_header.bsize + secalign) & ~secalign;
-// Remove elements from unresolved list which are resolvable
+// Handle items in processing list.
+//
+// After loading all objects, archives & include files, we now go and process
+// each item on the processing list (plist). Once this is done, we go through
+// any unresolved symbols left and see if they have shown up.
- struct HREC * uptr; // Unresolved hash record pointer
- struct HREC * prev = NULL; // Previous hash record pointer
- struct HREC * htemp; // Temporary hash record pointer
- struct OFILE * ptemp; // Temporary object file record pointer
-
- // Process object file list
+ // Process object file list first (adds symbols from each unit & creates
+ // the olist)
- // Process unresolved list
+ struct HREC * uptr;
+
+ // Process the unresolved symbols list. This may involve pulling in symbols
+ // from any included .a units. Such units are lazy linked by default; we
+ // generally don't want everything they provide, just what's referenced.
- printf("%s in %s (=$%06X)\n", isglobal(htemp->h_type) ? "global" : "common", htemp->h_ofile->o_name, htemp->h_value);
+ printf("%s in %s (=$%06X)\n", (isglobal(htemp->h_type) ? "global" : "common"), htemp->h_ofile->o_name, htemp->h_value);
- if (prev == NULL)
- {
- unresolved = uptr->h_next;
- free(uptr);
- uptr = unresolved;
- }
+ // Check to see if the unresolved symbol is on the AR symbol list.
+ htemp = LookupARHREC(uptr->h_sym);
+
+ // If the unresolved symbol is in a .o unit that is unused, we can
+ // drop it; same if the unresolved symbol is in the exported AR
+ // symbol list. Otherwise, go to the next unresolved symbol.
+ if (!(uptr->h_ofile->o_flags & O_USED) || (htemp != NULL))
+ uptr = RemoveUnresolvedSymbol(uptr);
+ uptr = uptr->h_next;
+
+ // Now that we've possibly deleted the symbol from unresolved list
+ // that was also in the AR list, we add the symbols from this .o
+ // unit to the symbol table, mark the .o unit as used, and restart
+ // scanning the unresolved list as there is a good possibility that
+ // the symbols in the unit we're adding has unresolved symbols as
+ // well.
+ if (htemp != NULL)
- printf("NULL\n");
- prev = uptr;
- uptr = uptr->h_next;
+ if (filePtr->o_flags & O_USED)
+ {
+ printf(" %s%s%s\n", filePtr->o_name, (filePtr->isArchiveFile ? ":" : ""), (filePtr->isArchiveFile ? filePtr->o_arname : nullStr));
+ }
+
+ filePtr = filePtr->o_next;
-int AddToProcessingList(char * ptr, char * fname, uint8_t arFile)
+int AddToProcessingList(char * ptr, char * fname, char * arname, uint8_t arFile)
{
// Error on excessive filename length
printf("File name too long: %s (sorry!)\n", fname);
return 1;
}
{
// Error on excessive filename length
printf("File name too long: %s (sorry!)\n", fname);
return 1;
}
- strcpy(plast->o_name, path_tail(fname)); // Store filename, not path
- *plast->o_arname = 0; // No archive name for this file
+ if (strlen(arname) > FNLEN - 1)
+ {
+ // Error on excessive filename length
+ printf("AR file name too long: %s (sorry!)\n", arname);
+ return 1;
+ }
+
+ strcpy(plast->o_name, fname); // Store filename sans path
+ strcpy(plast->o_arname, arname); // Store archive name sans path
// routine takes in the binary file and creates an 'object' file in memory.
// Sym1/Sym2 point to the start and end of data.
//
// routine takes in the binary file and creates an 'object' file in memory.
// Sym1/Sym2 point to the start and end of data.
//
-int doinclude(char * fname, int handle, char * sym1, char * sym2, int segment)
+int LoadInclude(char * fname, int handle, char * sym1, char * sym2, int segment)
{
long fsize, dsize, size; // File, DATA segment and image sizes
char * ptr, * sptr; // Data pointers
{
long fsize, dsize, size; // File, DATA segment and image sizes
char * ptr, * sptr; // Data pointers
}
//
// Takes a file name, gets in its image, puts it on plist. The image may
// already be in memory: If so, the ptr arg is non-null. If so, the file is
}
//
// Takes a file name, gets in its image, puts it on plist. The image may
// already be in memory: If so, the ptr arg is non-null. If so, the file is
strcpy(obj_fname[obj_index], path_tail(fname));
obj_segsize[obj_index][0] = (getlong(ptr + 4) + secalign) & ~secalign;
obj_segsize[obj_index][1] = (getlong(ptr + 8) + secalign) & ~secalign;
strcpy(obj_fname[obj_index], path_tail(fname));
obj_segsize[obj_index][0] = (getlong(ptr + 4) + secalign) & ~secalign;
obj_segsize[obj_index][1] = (getlong(ptr + 8) + secalign) & ~secalign;
- // This routine is shared by doinclude after it builds the image
- return AddToProcessingList(ptr, fname, 0);
+ // This routine is shared by LoadInclude after it builds the image
+ return AddToProcessingList(ptr, fname, nullStr, 0);
+
+//
+// What it says on the tin: check for a .o suffix on the passed in string
+//
+uint8_t HasDotOSuffix(char * s)
+{
+ char * temp = strrchr(s, '.');
+
+ if ((temp == NULL) || (strncmp(temp, ".o", 2) != 0))
+ return 0;
+
+ return 1;
+}
+
+
{
// Read in the archive file to memory and process
long size = FileSize(fd);
char * ptr = malloc(size);
char * endPtr = ptr + size;
{
// Read in the archive file to memory and process
long size = FileSize(fd);
char * ptr = malloc(size);
char * endPtr = ptr + size;
+ // Check to see if a long filename was requested
+ if (objName[0] == 0x20)
+ {
+ uint32_t fnSize = atoi(objName + 1);
+
+ if (longFilenames != NULL)
+ {
+ i = 0;
+ char * currentFilename = longFilenames + fnSize;
+
+ while (*currentFilename != 0x0A)
+ objName[i++] = *currentFilename++;
+
+ objName[i] = 0;
+ }
+ }
+
+ if ((strncmp(objName, "ARFILENAMES/", 12) == 0) || (strncmp(objName, "//", 2) == 0))
+ longFilenames = ptr + 60;
+ }
+ else if (HasDotOSuffix(objName))
+ {
+//printf("Processing object \"%s\" (size == %i, obj_index == %i)...\n", objName, atoi(objSize), obj_index);
strcpy(obj_fname[obj_index], objName);
obj_segsize[obj_index][0] = (getlong(ptr + 60 + 4) + secalign) & ~secalign;
obj_segsize[obj_index][1] = (getlong(ptr + 60 + 8) + secalign) & ~secalign;
obj_segsize[obj_index][2] = (getlong(ptr + 60 + 12) + secalign) & ~secalign;
obj_index++;
strcpy(obj_fname[obj_index], objName);
obj_segsize[obj_index][0] = (getlong(ptr + 60 + 4) + secalign) & ~secalign;
obj_segsize[obj_index][1] = (getlong(ptr + 60 + 8) + secalign) & ~secalign;
obj_segsize[obj_index][2] = (getlong(ptr + 60 + 12) + secalign) & ~secalign;
obj_index++;
- if (AddToProcessingList(ptr + 60, objName, 1))
+ if (AddToProcessingList(ptr + 60, objName, fname, 1))
- // Attempt to read file magic number
- // OBJECT/ARCHIVE FILES
- if (read(handle[i], magic, 4) != 4)
+ // Attempt to read file magic number (OBJECT/ARCHIVE FILES)
+ if (read(handle[i], magic, 8) != 8)
- if (doinclude(name[i], handle[i], hsym1[i], hsym2[i], hflag[i] - 1))
+ if (LoadInclude(name[i], handle[i], hsym1[i], hsym2[i], hflag[i] - 1))
{
printf("Error in bss-segment address: %s is not 'r', 'x[td]', or an address.", argv[i]);
return 1;
{
printf("Error in bss-segment address: %s is not 'r', 'x[td]', or an address.", argv[i]);
return 1;