unsigned errflag = 0; // Error flag, goes TRUE on error
unsigned waitflag = 0; // Wait for any keypress flag
unsigned errflag = 0; // Error flag, goes TRUE on error
unsigned waitflag = 0; // Wait for any keypress flag
unsigned vflag = 0; // Verbose flag
unsigned wflag = 0; // Show warnings flag
unsigned zflag = 0; // Suppress banner flag
unsigned vflag = 0; // Verbose flag
unsigned wflag = 0; // Show warnings flag
unsigned zflag = 0; // Suppress banner flag
unsigned hd = 0; // Index of next file handle to fill
unsigned secalign = 7; // Section Alignment (8=phrase)
unsigned tbase = 0; // TEXT base address
unsigned hd = 0; // Index of next file handle to fill
unsigned secalign = 7; // Section Alignment (8=phrase)
unsigned tbase = 0; // TEXT base address
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
- char * symptr; // Symbol pointer
- char * symend; // Symbol end pointer
- int type; // Symbol type
- long value; // Symbol value
- int index; // Symbol index
- int j; // Iterator
- 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
+ int type;
+ long value;
+ int index;
+ int j;
+ struct HREC * hptr;
+ unsigned tsoSave, dsoSave, bsoSave;
+ 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]))
{
- // Process each record in the symbol table
+ // 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
- 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
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
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. For the hash calculation to work correctly it must be
- // placed in a 'clean' string before looking it up.
- memset(sym, 0, SYMLEN);
- strcpy(sym, symend + index);
- hptr = lookup(sym);
+ // symbol.
+ hptr = LookupHREC(symend + index);
- printf("dosym() : Cannot determine symbol : %s\n", sym);
- return 1;
+ // 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 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
+ continue;
}
// Search through object segment size table to obtain segment sizes
// for the object that has the required external/global as a local
// symbol. As each object is interrogated the segment sizes are
// accumulated to ensure the correct offsets are used in the
}
// Search through object segment size table to obtain segment sizes
// for the object that has the required external/global as a local
// symbol. As each object is interrogated the segment sizes are
// accumulated to ensure the correct offsets are used in the
- printf("dosym() : Cannot get object file segment size : %s\n",
- ofile->o_name);
+ printf("DoSymbols(): Object file missing from obj_fname: '%s:%s' symbol: '%s' (%s)\n",
+ hptr->h_ofile->o_name, hptr->h_ofile->o_arname,
+ symend + index, ofile->o_name);
- // Add to output symbol table
- if (lflag || !islocal(type))
+ // Add to output symbol table if global/extern, or local flag is set
+ if (isglobal(type) || lflag)
- if (islocal(type) || isglobal(type))
- {
- if ((index = ost_add(symend + index, type, value)) == -1)
- return 1;
- }
- else
+ index = OSTAdd(symend + index, type, value);
+
+ if (index == -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 (ost_add(hptr->h_sym, hptr->h_type, hptr->h_value) == -1)
+//Is this true? Couldn't an absolute be exported???
+ if (hptr->h_type == (T_EXT | T_ABS))
+ hptr->h_type = T_ABS; // Absolutes *can't* be externals
+
+ if (OSTAdd(hptr->h_sym, hptr->h_type, hptr->h_value) == -1)
-// 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.
+// Returns 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
-
- slen = strlen(name);
+ int ost_offset_p, ost_offset_e = 0; // OST table offsets for position calcs
+ int ostresult; // OST index result
+ int slen = strlen(name);
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
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
-#warning "!!! Bad logic in realloc call !!!"
- if ((ost = realloc(ost, (ost_end + OST_BLOCK))) == NULL)
+ // We want to allocate the current size of the OST + another block.
+ ost = realloc(ost, ost_offset_e + OST_BLOCK);
+
+ if (ost == NULL)
- // 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
+ // Get symbol index in OST, if any (-1 if not found)
+ ostresult = OSTLookup(name);
- putlong(ost_ptr + 4, type );
- putlong(ost_ptr + 8, value);
- ost_ptr += 12;
+ // 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
- // 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
- }
+ putlong(ost_ptr + 4, type);
+ putlong(ost_ptr + 8, value);
+ ost_ptr += 12;
- if (vflag > 1)
- {
- printf("ost_add: (%s), type=$%08X, val=$%08X\n", name, type, value);
- }
+ // 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
+ }
- 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.
//
- 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 swcond; // Switch statement condition
- unsigned relocsize; // Relocation record size
+ 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 swcond; // Switch statement condition
+ unsigned relocsize; // Relocation record size
// 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)
{
// 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)
{
- // 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("reloc_segment(%s, TEXT) : No Relocation Data\n", ofile->o_name);
-
+ // SCPCD : we should not increment the textoffset before the end of processing the object file, else data section will point to wrong textoffset
+ // 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));
// DATA segment size plus padding
pad = ((ofile->o_header.dsize + secalign) & ~secalign);
dataoffset += (ofile->o_header.dsize + (pad - ofile->o_header.dsize));
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
+ // SCPCD : the symbol point to a text segment, we should use the textoffset
+ newdata = tbase + textoffset + olddata;
- 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);
{
// TEXT segment plus padding
pad = ((ofile->o_header.tsize + secalign) & ~secalign);
textoffset += (ofile->o_header.tsize + (pad - ofile->o_header.tsize));
{
// 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));
// DATA segment plus padding
pad = ((ofile->o_header.dsize + secalign) & ~secalign);
dataoffset += (ofile->o_header.dsize + (pad - ofile->o_header.dsize));
+// 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))
- 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
+ // 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
- 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
case 0x08000000: abstype = (short)ABST_DEFINED | ABST_BSS; break;
case 0x09000000: abstype = (short)ABST_DEFINED | ABST_GLOBAL | ABST_BSS; break;
default:
case 0x08000000: abstype = (short)ABST_DEFINED | ABST_BSS; break;
case 0x09000000: abstype = (short)ABST_DEFINED | ABST_GLOBAL | ABST_BSS; break;
default:
- 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
- struct OFILE * otemp, * oprev, * ohold; // Object file list pointers
- struct OHEADER * header; // Output header pointer
+ unsigned tptr, dptr, bptr; // Bases in runtime model
+ int ret = 0; // Return value
+ struct OHEADER * header; // Output header pointer
// Initialize cumulative segment sizes
textsize = datasize = bsssize = 0;
// For each object file, accumulate the sizes of the segments but remove
// those object files which are unused
// Initialize cumulative segment sizes
textsize = datasize = bsssize = 0;
// 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;
+ // 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;
+ OSTAdd("_TEXT_E", 0x05000000, tbase + textsize);
+ OSTAdd("_DATA_E", 0x07000000, dbase + datasize);
+ OSTAdd("_BSS_E", 0x09000000, bbase + bsssize);
+
// 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;
- struct HREC * hptr = htable[DoHash(sym)]; // Hash index to record based on sym
- char symbol[SYMLEN]; // Temporary symbol storage
+ if (vflag > 1)
+ printf("AddARSymbol(%s, %s)\n", sym, ofile->o_name);
+
+ return AddSymbolToHashList(&arSymbol, sym, ofile, 0L, 0);
+}
- memset(symbol, 0, SYMLEN); // Clean string for comparison
- strcpy(symbol, sym);
+
+//
+// Generate hash value from the 1st 15 characters of the symbol modulo the
+// number of buckets in the hash.
+//
+int GetHash(char * s)
+{
+ // For this to be consistent, the symbol MUST be zeroed out beforehand!
+ // N.B.: strncpy() pads zeroes for us, if the symbol is less than 15 chars.
+ char c[15];
+ strncpy(c, s, 15);
+
+ 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;
+}
+
+
+//
+// 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
- struct HREC * hptr;
- int flag = !iscommon(type);
-
- if (vflag > 1)
- {
- printf("hash_add(%s,%s,%lx,", sym, ofile->o_name, value);
- printf("%x,%s)\n", (unsigned int)type, (flag ? "GLOBAL" : "COMMON"));
- }
-
- if ((hptr = lookup(sym)) == NULL)
- {
- return add_to_hlist(&htable[DoHash(sym)], sym, ofile, value, type);
- }
+ struct HREC * hptr = arSymbol;//htable[GetHash(symbol)];
- // Already there!
- if (iscommon(type) && !iscommon(hptr->h_type))
- {
- // Mismatch: global came first; warn and keep the global one
- if (wflag)
- {
- printf("Warning: %s: global from ", sym);
- put_name(hptr->h_ofile);
- printf(" used, common from ");
- put_name(ofile);
- printf(" discarded.\n");
- }
-
- putword(sym + 8, ABST_EXTERN);
- putlong(sym + 10, 0L);
- }
- else if (iscommon(hptr->h_type) && !iscommon(type))
- {
- // Mismatch: common came first; warn and keep the global one
- if (wflag)
- {
- printf("Warning: %s: global from ", sym);
- put_name(ofile);
- printf(" used, common from ");
- put_name(hptr->h_ofile);
- printf(" discarded.\n");
- }
-
- hptr->h_type = type;
- hptr->h_ofile = ofile;
- hptr->h_value = value;
- }
- // They're both global
- else if (flag)
- // Global exported by another ofile; warn and make this one extern
- if (wflag)
- {
- printf("Duplicate symbol %s: ", sym);
- put_name(hptr->h_ofile);
- printf(" used, ");
- put_name(ofile);
- printf(" discarded\n");
- }
+ // Return hash record pointer if found
+ if (strcmp(symbol, hptr->h_sym) == 0)
+ return hptr;
- 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
- 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
- memset(symbol, 0, SYMLEN);
- strcpy(symbol, sstr + index);
+ long index = getlong(sfix); // Get symbol string index
+ long type = getlong(sfix + 4); // Get symbol type
+ long value = getlong(sfix + 8); // Get symbol value
- // If this is a global/external
- if (type & T_EXT)
+ if ((Ofile->isArchiveFile) && !(Ofile->o_flags & O_USED))
+ {
+ if ((type & T_EXT) && (type & (T_SEG | T_ABS)))
+ if (AddARSymbol(sstr + index, Ofile))
+ return 1;
+ }
+ else if (type == T_EXT)
- // Then add to hash table
- if (hash_add(symbol, type, value, Ofile))
+ // Check for built-in externals...
+ if ((strcmp(sstr + index, "_TEXT_E") != 0)
+ && (strcmp(sstr + index, "_DATA_E") != 0)
+ && (strcmp(sstr + index, "_BSS_E") != 0))
+ }
+ else if ((type & T_EXT) && (type & (T_SEG | T_ABS)))
+ {
+#if 0
+ // Symbol in the current unit that is also EXPORTED
+ if (DealWithSymbol(sstr + index, type, value, Ofile))
+ return 1; // Error if addition failed
+#else
+ hptr = LookupHREC(sstr + index);
+
+ // Symbol isn't in the table, so try to add it:
+ if (hptr == NULL)
+ {
+ if (AddSymbolToHashList(&htable[GetHash(sstr + index)],
+ sstr + index, Ofile, value, type))
+ return 1;
+ }
- // If value is zero and in hash table
- if ((hptr = lookup(symbol)) != NULL)
+ // Symbol already exists, decide what to do about it
+ if (iscommon(hptr->h_type))
- hptr->h_ofile->o_flags |= O_USED; // Mark symbol as used
+ // Mismatch: common came first; warn and keep the global
+ if (wflag)
+ {
+ printf("Warning: %s: global from ", sstr + index);
+ put_name(Ofile);
+ printf(" used, common from ");
+ put_name(hptr->h_ofile);
+ printf(" discarded.\n");
+ }
+
+ hptr->h_ofile = Ofile;
+ hptr->h_type = type;
+ hptr->h_value = value;
- return 1; // Error if addition failed
+ // Global exported by another ofile; warn and make this one
+ // extern
+ if (wflag)
+ {
+ printf("Duplicate symbol %s: ", sstr + index);
+ put_name(hptr->h_ofile);
+ printf(" used, ");
+ put_name(Ofile);
+ printf(" discarded\n");
+ }
+
+ // Set the global in this unit to pure external
+ // (is this a good idea? what if the other one is a ref to
+ // this one???)
+ putlong(sfix + 4, T_EXT);
- 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;
+// 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.
+//
+int ProcessLists(void)
- 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;
char * temp = strrchr(name, PATH_DELIMITER);
// Return what was passed in if path delimiter was not found
char * temp = strrchr(name, PATH_DELIMITER);
// Return what was passed in if path delimiter was not found
-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
- int i; // Iterators
- int sym1len = 0; // Symbol 1 length
- int sym2len = 0; // Symbol 2 length
+ char * ptr, * sptr;
+ int i;
- fsize = FileSize(handle); // Get size of include file
- dsize = (fsize + secalign) & ~secalign; // Round up to a alignment boundary
-
- sym1len = strlen(sym1) + 1; // Get sym1 length + null termination
- sym2len = strlen(sym2) + 1; // Get sym2 length + null termination
-
- size = 32 + dsize + 24 + 4 + sym1len + sym2len + 4;
+ long fsize = FileSize(handle); // Get size of include file
+ long dsize = (fsize + secalign) & ~secalign; // Align size to boundary
+ int sym1len = strlen(sym1) + 1; // Get sym1 length + null termination
+ int sym2len = strlen(sym2) + 1; // Get sym2 length + null termination
+ long size = 32 + dsize + 24 + 4 + sym1len + sym2len + 4;
}
//
// 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))
+
+ // Strip off any trailing forward slash at end of object name
+ int lastChar = strlen(objName) - 1;
+
+ if (objName[lastChar] == '/')
+ objName[lastChar] = 0;
+
+//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)
- // Look for RMAC/MAC/GCC ($20107) object files
- if ((getlong(magic) == 0x00000107) || (getlong(magic) == 0x00020107))
+ // Look for RMAC/MAC/GCC (a.out) object files
+ if ((getlong(magic) & 0xFFFF) == 0x0107)
- 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))
// BSS segment can be 'r', 'x' or a value
if ((*argv[i] == 'r' || *argv[i] == 'R') && !argv[i][1])
// BSS segment can be 'r', 'x' or a value
if ((*argv[i] == 'r' || *argv[i] == 'R') && !argv[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;
printf(" d: double phrase (16 bytes)\n");
printf(" q: quad phrase (32 bytes)\n");
printf(" -s output only global symbols\n");
printf(" d: double phrase (16 bytes)\n");
printf(" q: quad phrase (32 bytes)\n");
printf(" -s output only global symbols\n");
printf(" -v set verbose mode\n");
printf(" -w show linker warnings\n");
printf(" -z suppress banner\n");
printf(" -v set verbose mode\n");
printf(" -w show linker warnings\n");
printf(" -z suppress banner\n");
if (s) // Attempt to obtain env variable
strcpy(libdir, s); // Store it if found
if (s) // Attempt to obtain env variable
strcpy(libdir, s); // Store it if found