-
-// -------------------------------------------------------------------------------------------------
-// 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 reloc_segment(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 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) {
- pad = ((ofile->o_header.tsize+secalign) & ~secalign); // TEXT segment size plus padding
- textoffset += (ofile->o_header.tsize + (pad - ofile->o_header.tsize));
- if (vflag > 1) { // Verbose mode information
- printf("reloc_segment(%s, TEXT) : No Relocation Data\n", ofile->o_name);
- }
- 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) {
- pad = ((ofile->o_header.dsize+secalign) & ~secalign); // DATA segment size plus padding
- dataoffset += (ofile->o_header.dsize + (pad - ofile->o_header.dsize));
- pad = ((ofile->o_header.bsize+secalign) & ~secalign); // BSS segment size plus padding
- bssoffset += (ofile->o_header.bsize + (pad - ofile->o_header.bsize));
- if (vflag > 1) { // Verbose mode information
- printf("reloc_segment(%s, DATA) : No Relocation Data\n", ofile->o_name);
- }
- return 0;
- }
-
- // Verbose mode information
- if (vflag > 1) {
- printf("reloc_segment(%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;
-
- // 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
-
- // 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 = ost_lookup(sym);
- newdata = getlong(ost + ((ssidx-1) * 12) + 8);
- }
-
- // Obtain the existing long word segment data and flip words if the relocation flags
- // indicate it relates to a RISC MOVEI instruction
- olddata = getlong(sptr + 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 = glblreloc ? ((hptr->h_type & 0x0E000000) >> 16) : (rflg & 0xFFFFFF00);
- swcond = (rflg & 0xFFFFFF00);
- if (!glblreloc) {
- switch (swcond) {
- case 0x00000200: // Absolute Value
- break;
- case 0x00000400: // TEXT segment relocation record
- if (!glblreloc)
- if (flag == T_TEXT) newdata = tbase + textoffset + olddata;
- else newdata = tbase + dataoffset + olddata;
- break;
- case 0x00000600: // DATA segment relocation record
- if (!glblreloc) newdata = dbase + dataoffset + (olddata - ofile->o_header.tsize);
- break;
- case 0x00000800: // BSS segment relocation record
- if (!glblreloc) 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);
-
- putlong(sptr + addr, newdata);
-
- }
- else if (relreloc) {
- putword(sptr + addr, newdata - tbase - addr - ofile->o_tbase);
- }
-
- rptr += 8; // Point to the next relocation record
- }
-
- // Update the COF segment offset allowing for the phrase padding.
- if (flag == T_TEXT) {
- pad = ((ofile->o_header.tsize+secalign) & ~secalign); // TEXT segment plus padding
- textoffset += (ofile->o_header.tsize + (pad - ofile->o_header.tsize));
- } else {
- pad = ((ofile->o_header.dsize+secalign) & ~secalign); // DATA segment plus padding
- dataoffset += (ofile->o_header.dsize + (pad - ofile->o_header.dsize));
- pad = ((ofile->o_header.bsize+secalign) & ~secalign); // BSS segment plus padding
- bssoffset += (ofile->o_header.bsize + (pad - ofile->o_header.bsize));
- }
-
- return 0; // Return value, should always be zero
+
+
+//
+// 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 reloc_segment(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 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)
+ {
+ // 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);
+
+ 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)
+ {
+ // 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("reloc_segment(%s, DATA) : No Relocation Data\n", ofile->o_name);
+
+ return 0;
+ }
+
+ // Verbose mode information
+ if (vflag > 1)
+ {
+ printf("reloc_segment(%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
+
+ // 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 = ost_lookup(sym);
+ newdata = getlong(ost + ((ssidx - 1) * 12) + 8);
+ }
+
+ // Obtain the existing long word segment data and flip words if the
+ // relocation flags indicate it relates to a RISC MOVEI instruction
+ olddata = getlong(sptr + 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 = glblreloc ? ((hptr->h_type & 0x0E000000) >> 16) : (rflg & 0xFFFFFF00);
+ swcond = (rflg & 0xFFFFFF00);
+
+ if (!glblreloc)
+ {
+ switch (swcond)
+ {
+ case 0x00000200: // Absolute Value
+ break;
+ 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
+ }
+
+ break;
+ case 0x00000600: // DATA segment relocation record
+ if (!glblreloc)
+ newdata = dbase + dataoffset + (olddata - ofile->o_header.tsize);
+
+ break;
+ case 0x00000800: // BSS segment relocation record
+ if (!glblreloc)
+ 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);
+
+ 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("reloc_segment($%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.
+ if (flag == T_TEXT)
+ {
+ // TEXT segment plus padding
+ pad = ((ofile->o_header.tsize + secalign) & ~secalign);
+ textoffset += (ofile->o_header.tsize + (pad - ofile->o_header.tsize));
+ }
+ else
+ {
+ // 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;