+ return 0;
+}
+
+
+//
+// Add the imported symbols from this file to unresolved, and the global and
+// common (???) symbols to the exported hash table.
+//
+// Change old-style commons (type == T_EXTERN, value != 0) to new-style ones
+// (type == (T_GLOBAL | T_EXTERN)). [??? O_o]
+//
+int AddSymbols(struct OFILE * Ofile)
+{
+ struct HREC * hptr; // Hash record pointer
+ char symbol[SYMLEN];
+
+ if (vflag > 1)
+ printf("Add symbols for file %s\n", Ofile->o_name);
+
+ // Get base pointer, start of sym fixups
+ char * ptr = Ofile->o_image + 32
+ + Ofile->o_header.tsize
+ + Ofile->o_header.dsize
+ + Ofile->o_header.absrel.reloc.tsize
+ + Ofile->o_header.absrel.reloc.dsize;
+ 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
+
+ while (nsymbols)
+ {
+ 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);
+
+ if ((Ofile->isArchiveFile) && !(Ofile->o_flags & O_USED))
+ {
+ if ((type & T_EXT) && (type & (T_SEG | T_ABS)))
+ if (AddARSymbol(symbol, Ofile))
+ return 1;
+ }
+ else if (type == T_EXT)
+ {
+ // External symbal that is *not* in the current unit
+ hptr = LookupHREC(symbol);
+
+ 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
+ }
+
+ // Success loading symbols
+ return 0;
+}
+
+
+//
+// Process object file for symbols
+//
+int DoItem(struct OFILE * obj)
+{
+ // Allocate memory for object record ptr
+ struct OFILE * Ofile = new_ofile();
+
+ if (Ofile == NULL)
+ {
+ printf("Out of memory while processing %s\n", obj->o_name);
+ return 1;
+ }
+
+ // Starting after all pathnames, etc., copy .o file name to Ofile
+ char * temp = path_tail(obj->o_name);
+
+ // Check filename length
+ if (strlen(temp) > FNLEN - 1)
+ {
+ printf("File name too long: %s\n", temp);
+ return 1;
+ }
+
+ // Check archive name length
+ if (strlen(obj->o_arname) > FNLEN - 1)
+ {
+ printf("Archive name too long: %s\n", obj->o_arname);
+ return 1;
+ }
+
+ strcpy(Ofile->o_name, temp); // Store filename
+ strcpy(Ofile->o_arname, obj->o_arname); // Store archive name
+
+ // Initialise object record information
+ Ofile->o_next = NULL;
+ Ofile->o_tbase = 0;
+ Ofile->o_dbase = 0;
+ Ofile->o_bbase = 0;
+ Ofile->o_flags = obj->o_flags;
+ Ofile->o_image = obj->o_image;
+ Ofile->isArchiveFile = obj->isArchiveFile;
+ char * ptr = obj->o_image;
+
+ // Don't do anything if this is just an ARCHIVE marker, just add the file
+ // to the olist
+ // (Shamus: N.B. it does no such ARCHIVE thing ATM)
+ if (!(obj->o_flags & O_ARCHIVE))
+ {
+ Ofile->o_header.magic = getlong(ptr);
+ 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;
+
+ if ((Ofile->o_header.dsize & 7) && wflag)
+ {
+ printf("Warning: data segment size of ");
+ put_name(Ofile);
+ printf(" is not a phrase multiple\n");
+ }
+
+ // Check for odd segment sizes
+ if ((Ofile->o_header.tsize & 1) || (Ofile->o_header.dsize & 1)
+ || (Ofile->o_header.bsize & 1))
+ {
+ printf("Error: odd-sized segment in ");
+ put_name(Ofile);
+ printf("; link aborted.\n");
+ return 1;
+ }
+
+ if (AddSymbols(Ofile))
+ return 1;
+ }
+
+ // Add this file to the olist
+ if (olist == NULL)
+ olist = Ofile;
+ else
+ olast->o_next = Ofile;
+
+ olast = Ofile;
+ return 0;
+}
+
+
+//
+// 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.
+//
+int ProcessLists(void)
+{
+ // Process object file list first (adds symbols from each unit & creates
+ // the olist)
+ while (plist != NULL)
+ {
+ if (DoItem(plist))
+ return 1;
+
+ struct OFILE * ptemp = plist;
+ plist = plist->o_next;
+ free(ptemp);
+ }
+
+ 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 refenced.
+ for(uptr=unresolved; uptr!=NULL; )
+ {
+ if (vflag > 1)
+ printf("LookupHREC(%s) => ", uptr->h_sym);
+
+ struct HREC * htemp = LookupHREC(uptr->h_sym);
+
+ if (htemp != NULL)
+ {
+ // Found it in the symbol table!
+ if (vflag > 1)
+ printf("%s in %s (=$%06X)\n", (isglobal(htemp->h_type) ? "global" : "common"), htemp->h_ofile->o_name, htemp->h_value);
+
+ // Mark the .o unit that the symbol is in as seen & remove from the
+ // unresolved list
+ htemp->h_ofile->o_flags |= O_USED;
+ uptr = RemoveUnresolvedSymbol(uptr);
+ }
+ else
+ {
+ if (vflag > 1)
+ printf("NULL\n");
+
+ // 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);
+ else
+ 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)
+ {
+ htemp->h_ofile->o_flags |= O_USED;
+ AddSymbols(htemp->h_ofile);
+ uptr = unresolved;
+ }
+ }
+ }
+
+ // Show files used if the user requests it.
+ if (vflag > 1)
+ {
+ printf("Files used:\n");
+ struct OFILE * filePtr = olist;
+
+ while (filePtr != NULL)
+ {
+ 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;
+ }
+ }
+
+ return 0;
+}
+
+
+//
+// Extract filename from path
+//
+char * path_tail(char * name)
+{
+ char * temp = strrchr(name, PATH_DELIMITER);
+
+ // Return what was passed in if path delimiter was not found
+ if (temp == NULL)
+ return name;
+
+ return temp + 1;
+}
+
+
+//
+// Add input file to processing list
+//
+int AddToProcessingList(char * ptr, char * fname, char * arname, uint8_t arFile)
+{
+ if (plist == NULL)
+ {
+ // First time object record allocation
+ plist = new_ofile();
+ plast = plist;
+ }
+ else
+ {
+ // Next object record allocation
+ plast->o_next = new_ofile();
+ plast = plast->o_next;
+ }
+
+ if (plast == NULL)
+ {
+ printf("Out of memory.\n"); // Error if memory allocation fails
+ return 1;
+ }
+
+ fname = path_tail(fname);
+ arname = path_tail(arname);
+
+ if (strlen(fname) > FNLEN - 1)
+ {
+ // Error on excessive filename length
+ printf("File name too long: %s (sorry!)\n", fname);
+ return 1;
+ }
+
+ 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
+ plast->o_image = ptr; // Store data pointer
+ plast->o_flags = (arFile ? 0 : O_USED); // File is used if NOT in archive
+ plast->o_next = NULL; // Initialise next record pointer
+ plast->isArchiveFile = arFile; // Shamus: Temp until can sort it out
+
+ return 0; // Return without errors
+}
+
+
+//
+// Process in binary include files and add them to the processing list. This
+// routine takes in the binary file and creates an 'object' file in memory.
+// Sym1/Sym2 point to the start and end of data.
+//
+// Image size for include files is:
+// Header ....... 32 bytes
+// Data ......... dsize
+// Sym Fixups ... 2 * 12 bytes
+// Symbol Size .. 4 bytes (Value to include symbols and terminating null)
+// Symbols ...... (strlen(sym1) + 1) + (strlen(sym2) + 1)
+// Terminate .... 4 bytes (0x00000000)
+//
+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
+ unsigned symtype = 0;
+
+ 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;
+
+ // Use calloc so the header & fixups initialize to zero
+ // Allocate object image memory
+ if ((ptr = calloc(size, 1)) == NULL)
+ {
+ printf("Out of memory while including %s\n", fname);
+ close(handle);
+ return 1;
+ }
+
+ // Read in binary data
+ if (read(handle, ptr + 32, fsize) != fsize)
+ {
+ printf("File read error on %s\n", fname);
+ close(handle);
+ free(ptr);
+ return 1;
+ }
+
+ close(handle);
+
+ strcpy(obj_fname[obj_index], path_tail(fname));
+
+ // Build this image's dummy header
+ putlong(ptr, 0x00000107); // Magic number
+
+ if (segment)
+ {
+ putlong(ptr+4, dsize); // Text size
+ putlong(ptr+8, 0L); // Data size
+ symtype = 0x05000000;
+ obj_segsize[obj_index][0] = dsize;
+ obj_segsize[obj_index][1] = 0;
+ obj_segsize[obj_index][2] = 0;
+ }
+ else
+ {
+ putlong(ptr+4, 0L); // Text size
+ putlong(ptr+8, dsize); // Data size
+ symtype = 0x07000000;
+ obj_segsize[obj_index][0] = 0;
+ obj_segsize[obj_index][1] = dsize;
+ obj_segsize[obj_index][2] = 0;
+ }
+
+ obj_index++; // Increment object count
+
+ putlong(ptr+12, 0L); // BSS size
+ putlong(ptr+16, 24); // Symbol table size
+ putlong(ptr+20, 0L); // Entry point
+ putlong(ptr+24, 0L); // TEXT relocation size
+ putlong(ptr+28, 0L); // DATA relocation size
+
+ sptr = ptr + 32 + dsize; // Set sptr to symbol table location
+
+ putlong(sptr, 4L); // String offset of symbol1
+ putlong(sptr+4, symtype); // Symbol type
+ putlong(sptr+8, 0x00000000); // Symbol has no value (START)
+ putlong(sptr+12, 4L + (sym2len - 1)); // String offset of symbol2
+ putlong(sptr+16, symtype); // Symbol type
+ putlong(sptr+20, dsize); // Symbol is data size (END)
+
+ sptr = ptr + 32 + dsize + 24; // Set sptr to symbol table size loc
+
+ putlong(sptr, sym1len + 4L); // Size of symbol table
+
+ sptr = ptr + 32 + dsize + 24 + 4; // Set sptr to symbol table location
+
+ for(i=0; i<(sym1len-1); i++) // Write symbol1 to string table
+ sptr[i] = *sym1++;
+
+ sptr += (sym1len - 1); // Step past symbol string
+ *sptr = '\0'; // Terminate symbol string
+ sptr += 1; // Step past termination
+
+ for(i=0; i<(sym2len-1); i++) // Write symbol2 to string table
+ sptr[i] = *sym2++;
+
+ sptr += (sym2len - 1); // Step past symbol string
+ *sptr = '\0'; // Terminate symbol string
+ sptr += 1; // Step past termination
+
+ putlong(sptr, 0L); // Terminating long for object file
+
+ return AddToProcessingList(ptr, fname, nullStr, 0);
+}
+
+
+//
+// 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
+// already closed. Note that the file is already open (from DoFile()). RETURNS
+// a pointer to the OFILE structure for this file, so you can diddle its flags
+// (DoFile sets O_USED for files on the command line).
+//
+int LoadObject(char * fname, int fd, char * ptr)
+{
+ if (ptr == NULL)
+ {
+ long size = FileSize(fd);
+
+ // Allocate memory for file data
+ ptr = malloc(size);
+
+ if (ptr == NULL)
+ {
+ printf("Out of memory while processing %s\n", fname);
+ close(fd);
+ return 1;
+ }
+
+ // Read in file data
+ if (read(fd, ptr, size) != size)
+ {
+ printf("File read error on %s\n", fname);
+ close(fd);
+ free(ptr);
+ return 1;
+ }
+
+ // SCPCD : get the name of the file instead of all pathname
+ 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;
+ obj_segsize[obj_index][2] = (getlong(ptr + 12) + secalign) & ~secalign;
+ obj_index++;
+
+ close(fd); // Close file
+ }
+
+ // Now add this image to the list of pending ofiles (plist)
+ // 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;
+}
+
+
+//
+// Process an ar archive file (*.a)
+//
+int LoadArchive(char * fname, int fd)
+{
+ // Read in the archive file to memory and process
+ long size = FileSize(fd);
+ char * ptr = malloc(size);
+ char * endPtr = ptr + size;
+ char * longFilenames = NULL;
+
+ if (ptr == NULL)
+ {
+ printf("Out of memory while processing %s\n", fname);
+ close(fd);
+ return 1;
+ }
+
+ if (read(fd, ptr, size) != size)
+ {
+ printf("File read error on %s\n", fname);
+ close(fd);
+ free(ptr);
+ return 1;
+ }
+
+ close(fd);
+
+ // Save the pointer for later...
+ arPtr[arIndex++] = ptr;
+ char objName[FNLEN];
+ char objSize[11];
+ int i;
+//printf("\nProcessing AR file \"%s\"...\n", fname);
+ ptr += 8;
+
+ // Loop through all objects in the archive and process them
+ do
+ {
+ memset(objName, 0, 17);
+ objSize[10] = 0;
+
+ for(i=0; i<16; i++)
+ {
+// if ((ptr[i] == '/') || (ptr[i] == ' '))
+ if ((ptr[i] == ' ') && (i != 0))
+ {
+ objName[i] = 0;
+ break;
+ }
+
+ objName[i] = ptr[i];
+ }
+
+ for(i=0; i<10; i++)
+ {
+ if (ptr[48 + i] == ' ')
+ {
+ objSize[i] = 0;
+ break;
+ }
+
+ objSize[i] = ptr[48 + i];
+ }
+
+ // 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++;
+
+ if (AddToProcessingList(ptr + 60, objName, fname, 1))
+ return 1;
+ }
+
+ uint32_t size = atoi(objSize);
+ size += (size & 0x01 ? 1 : 0);
+ ptr += 60 + size;
+ }
+ while (ptr < endPtr);
+
+ return 0;
+}
+
+
+//
+// Process files (*.o, *.a) passed in on the command line
+//
+int ProcessFiles(void)
+{
+ int i;
+ char magic[8]; // Magic header number (4 bytes for *.o, 8 for *.a)
+
+ // Process all file handles
+ for(i=0; i<(int)hd; i++)
+ {
+ // Verbose mode information
+ if (vflag == 1)
+ printf("Read file %s%s\n", name[i], (hflag[i] ? " (include)" : ""));
+
+ if (!hflag[i])
+ {
+ // Attempt to read file magic number (OBJECT/ARCHIVE FILES)
+ if (read(handle[i], magic, 8) != 8)
+ {
+ printf("Error reading file %s\n", name[i]);
+ close(handle[i]);
+ return 1;
+ }
+
+ lseek(handle[i], 0L, 0); // Reset to start of input file
+
+ // Look for RMAC/MAC/GCC ($20107) object files
+ if ((getlong(magic) == 0x00000107) || (getlong(magic) == 0x00020107))
+ {
+ // Process input object file
+ if (LoadObject(name[i], handle[i], 0L))
+ return 1;
+ }
+ // Otherwise, look for an object archive file
+ else if (strncmp(magic, "!<arch>\x0A", 8) == 0)
+ {
+ if (LoadArchive(name[i], handle[i]))
+ return 1;
+ }
+ else
+ {
+ // Close file and error
+ printf("%s is not a supported object or archive file\n", name[i]);
+ close(handle[i]);
+ return 1;
+ }
+ }
+ else
+ {
+ // INCLUDE FILES
+ // If hflag[i] is 1, include this in the data segment; if 2, put it
+ // in text segment
+ if (LoadInclude(name[i], handle[i], hsym1[i], hsym2[i], hflag[i] - 1))
+ return 1;
+ }
+ }
+
+ // Free include, symbol & object handles
+ for(i=0; i<(int)hd; i++)
+ {
+ free(name[i]);