2 // RLN - Reboot's Linker for the Atari Jaguar Console System
3 // RLN.C - Application Code
4 // Copyright (C) 199x, Allan K. Pratt, 2014 Reboot & Friends
9 unsigned errflag = 0; // Error flag, goes TRUE on error
10 unsigned waitflag = 0; // Wait for any keypress flag
11 unsigned versflag = 0; // Version banner has been shown flag
12 unsigned aflag = 0; // Absolute linking flag
13 unsigned bflag = 0; // Don't remove mulitply def locals flag
14 unsigned cflag = 0; // COF executable
15 unsigned dflag = 0; // Wait for key after link flag
16 unsigned gflag = 0; // Source level debug include flag
17 unsigned lflag = 0; // Add local symbols to output flag
18 unsigned mflag = 0; // Produce symbol load map flag
19 unsigned oflag = 0; // Output filename specified
20 unsigned rflag = 0; // Segment alignment size flag
21 unsigned sflag = 0; // Output only global symbols
22 unsigned vflag = 0; // Verbose flag
23 unsigned wflag = 0; // Show warnings flag
24 unsigned zflag = 0; // Suppress banner flag
25 unsigned pflag, uflag; // Unimplemented flags
26 unsigned hd = 0; // Index of next file handle to fill
27 unsigned secalign = 7; // Section Alignment (8=phrase)
28 unsigned tbase = 0; // TEXT base address
29 unsigned dbase = 0; // DATA base address
30 unsigned bbase = 0; // BSS base address
31 unsigned textoffset = 0; // COF TEXT segment offset
32 unsigned dataoffset = 0; // COF DATA segment offset
33 unsigned bssoffset = 0; // COF BSS segment offset
34 unsigned displaybanner = 1; // Display version banner
35 unsigned symoffset = 0; // Symbol table offset in output file
36 unsigned dosymi = 0; // Dosym() processing iterator
37 unsigned dbgsymbase = 0; // Debug symbol base address
38 //unsigned symtrunc = 0; // Symbol truncation -i and -ii
39 int noheaderflag = 0; // No header flag for ABS files
40 int hflags; // Value of the arg to -h option
41 int ttype, dtype, btype; // Type flag: 0, -1, -2, -3, -4
42 int tval, dval, bval; // Values of these abs bases
43 int hflag[NHANDLES]; // True for include files
44 int handle[NHANDLES]; // Open file handles
45 int textsize, datasize, bsssize; // Cumulative segment sizes
46 char libdir[FARGSIZE * 3]; // Library directory to search
47 char ofile[FARGSIZE]; // Output file name (.o)
48 char * name[NHANDLES]; // Associated file names
49 char * cmdlnexec = NULL; // Executable name - pointer to ARGV[0]
50 char * hsym1[SYMLEN]; // First symbol for include files
51 char * hsym2[SYMLEN]; // Second symbol for include files
52 struct OFILE * plist = NULL; // Object image list pointer
53 struct OFILE * plast; // Last object image list pointer
54 struct OFILE * olist = NULL; // Pointer to first object file in list
55 struct OFILE * olast; // Pointer to last object file in list
56 char obj_fname[16384][FNLEN]; // Object file names
57 unsigned obj_segsize[16384][3]; // Object file seg sizes; TEXT,DATA,BSS
58 unsigned obj_index = 0; // Object file index/count
61 struct HREC * htable[NBUCKETS]; // Hash table
62 struct HREC * unresolved = NULL; // Pointer to unresolved hash list
63 char * ost; // Output symbol table
64 char * ost_ptr; // Output symbol table; current pointer
65 char * ost_end; // Output symbol table; end pointer
66 char * oststr; // Output string table
67 char * oststr_ptr; // Output string table; current pointer
68 char * oststr_end; // Output string table; end pointer
69 int ost_index = 0; // Index of next ost addition
70 int endian; // Processor endianess
71 uint8_t nullStr[1] = "\x00"; // Empty string
72 struct HREC * arSymbol = NULL; // Pointer to AR symbol table
74 // Some human readable defines for endianess
75 #define ENDIANNESS_BIG 1
76 #define ENDIANNESS_LITTLE 0
78 // Function prototypes
79 struct HREC * LookupHREC(char *);
80 char * path_tail(char *);
81 void display_help(void);
82 void display_version(void);
86 // Get a long word from memory
88 uint32_t getlong(uint8_t * src)
91 uint8_t * out = (uint8_t *)&temp;
93 if (endian == ENDIANNESS_BIG)
113 // Put a long word into memory
115 void putlong(uint8_t * dest, uint32_t val)
117 *dest++ = (uint8_t)(val >> 24);
118 *dest++ = (uint8_t)(val >> 16);
119 *dest++ = (uint8_t)(val >> 8);
120 *dest = (uint8_t)val;
125 // Get a word from memory
127 uint16_t getword(uint8_t * src)
130 uint8_t * out = (uint8_t *)&temp;
132 if (endian == ENDIANNESS_BIG)
148 // Put a word into memory
150 void putword(uint8_t * dest, uint16_t val)
152 *dest++ = (uint8_t)(val >> 8);
153 *dest = (uint8_t)val;
158 // Find passed in file's length in bytes
159 // N.B.: This also resets the file's pointer to the start of the file
161 long FileSize(int fd)
163 long size = lseek(fd, 0, SEEK_END);
164 lseek(fd, 0, SEEK_SET);
171 // For this object file, add symbols to the output symbol table after
172 // relocating them. Returns TRUE if OSTLookup returns an error (-1).
174 int DoSymbol(struct OFILE * ofile)
176 int type; // Symbol type
177 long value; // Symbol value
178 int index; // Symbol index
180 struct HREC * hptr; // Hash table pointer for globl/extrn
181 char sym[SYMLEN]; // String for symbol name/hash search
183 // Point to first symbol record in the object file
184 char * symptr = (ofile->o_image + 32
185 + ofile->o_header.tsize
186 + ofile->o_header.dsize
187 + ofile->o_header.absrel.reloc.tsize
188 + ofile->o_header.absrel.reloc.dsize);
190 // Point to end of symbol record in the object file
191 char * symend = symptr + ofile->o_header.ssize;
193 // Search through object segment size table to accumulated segment sizes to
194 // ensure the correct offsets are used in the resulting COF file.
196 unsigned tsegoffset = 0, dsegoffset = 0, bsegoffset = 0;
198 // Search for object file name
199 for(j=0; j<(int)obj_index; j++)
201 if (!strcmp(ofile->o_name, obj_fname[j]))
203 // Object file name found
208 // Accumulate segment sizes
209 tsegoffset += obj_segsize[j][0];
210 dsegoffset += obj_segsize[j][1];
211 bsegoffset += obj_segsize[j][2];
216 printf("DoSymbol(): Cannot get object file segment size: %s\n", ofile->o_name);
220 // Process each record in the symbol table
221 for(; symptr!=symend; symptr+=12)
223 index = getlong(symptr + 0); // Obtain symbol string index
224 type = getlong(symptr + 4); // Obtain symbol type
225 value = getlong(symptr + 8); // Obtain symbol value
227 // Global/External symbols have a pre-processing stage
228 if (type & 0x01000000)
230 // Obtain the string table index for the relocation symbol, look
231 // for it in the globals hash table to obtain information on that
232 // symbol. For the hash calculation to work correctly it must be
233 // placed in a 'clean' string before looking it up.
234 memset(sym, 0, SYMLEN);
235 strcpy(sym, symend + index);
236 hptr = LookupHREC(sym);
240 printf("DoSymbol() : Cannot determine symbol : %s\n", sym);
244 // Search through object segment size table to obtain segment sizes
245 // for the object that has the required external/global as a local
246 // symbol. As each object is interrogated the segment sizes are
247 // accumulated to ensure the correct offsets are used in the
248 // resulting COF file. This is effectively 'done again' only as we
249 // are working with a different object file.
251 tsegoffset = dsegoffset = bsegoffset = 0;
253 // Search for object filename
254 for(j=0; j<(int)obj_index; j++)
256 if (!strcmp(hptr->h_ofile->o_name, obj_fname[j]))
258 ssidx = j; // Symbol object filename
262 // Accumulate segment sizes
263 tsegoffset += obj_segsize[j][0];
264 dsegoffset += obj_segsize[j][1];
265 bsegoffset += obj_segsize[j][2];
270 printf("DoSymbol() : Cannot get object file segment size : %s\n", ofile->o_name);
274 // Update type with global type
277 // Remove external flag if absolute
278 if (type == (T_EXT | T_ABS))
281 // If the global/external has a value then update that value in
282 // accordance with the segment sizes of the object file it
286 switch (hptr->h_type & 0x0E000000)
290 value = hptr->h_value;
293 value = hptr->h_value - hptr->h_ofile->o_header.tsize;
296 value = hptr->h_value
297 - (hptr->h_ofile->o_header.tsize
298 + hptr->h_ofile->o_header.dsize);
304 // Process and update the value dependent on whether the symbol is a
305 // debug symbol or not
306 // N.B.: These are currently not supported
307 if (type & 0xF0000000)
310 // Set the correct debug symbol base address (TEXT segment)
313 for(j=0; (unsigned)j<dosymi; j++)
314 dbgsymbase += obj_segsize[j][0];
316 switch (type & 0xFF000000)
319 value = tval + dbgsymbase;
324 value = tval + dbgsymbase + value;
329 putlong(symptr + 8, value);
334 // Now make modifications to the symbol value, local or global,
335 // based on the segment sizes of the object file currently being
337 switch (type & T_SEG)
342 value = tbase + tsegoffset + value;
343 putlong(symptr + 8, value);
347 value = dbase + dsegoffset + value;
349 value = dbase + dsegoffset + (value
350 - ofile->o_header.tsize);
352 putlong(symptr + 8, value);
356 value = bbase + bsegoffset + value;
358 value = bbase + bsegoffset
359 + (value - (ofile->o_header.tsize
360 + ofile->o_header.dsize));
362 putlong(symptr + 8, value);
369 // Add to output symbol table if global/extern, or local flag is set
370 if (lflag || !islocal(type))
372 index = OSTAdd(symend + index, type, value);
379 // Increment DoSymbol() processsing
387 // Free up hash memory
389 void FreeHashes(void)
393 for(i=0; i<NBUCKETS; i++)
395 struct HREC * hptr = htable[i];
399 struct HREC * htemp = hptr->h_next;
408 // Add all global and external symbols to the output symbol table
409 // [This is confusing--is it adding globals or locals? common == local!
410 // but then again, we see this in the header:
411 // #define T_COMMON (T_GLOBAL | T_EXTERN)]
418 for(i=0; i<NBUCKETS; i++)
420 for(hptr=htable[i]; hptr!=NULL; hptr=hptr->h_next)
422 //NO! if (iscommon(hptr->h_type))
423 if (isglobal(hptr->h_type))// || isextern(hptr->h_type))
425 // Skip if in *.a file... (does nothing)
426 //if (hptr->h_ofile->isArchiveFile)
429 if (hptr->h_type == (T_EXT | T_ABS))
430 hptr->h_type = T_ABS; // Absolutes *can't* be externals
432 if (OSTAdd(hptr->h_sym, hptr->h_type, hptr->h_value) == -1)
443 // Add a Symbol's Name, Type, and Value to the OST.
444 // Return the Index of the Symbol in OST, or -1 for Error.
446 int OSTAdd(char * name, int type, long value)
448 int ost_offset_p, ost_offset_e = 0; // OST table offsets for position calcs
449 int slen = 0; // Symbol string length
450 int ostresult; // OST index result
454 // If the OST or OST String Table has not been initialised then do so
457 if ((ost = malloc(OST_BLOCK)) == NULL)
459 printf("OST memory allocation error (stringtable).\n");
463 ost_ptr = ost; // Set OST start pointer
464 ost_end = ost + OST_BLOCK; // Set OST end pointer
466 if ((oststr = malloc(OST_BLOCK)) == NULL)
468 printf("OST memory allocation error (string).\n");
472 putlong(oststr, 0x00000004); // Just null long for now
473 oststr_ptr = oststr + 4; // Skip size of str table long (incl null long)
474 putlong(oststr_ptr, 0x00000000); // Null terminating long
475 oststr_end = oststr + OST_BLOCK;
479 // If next symbol record exceeds current allocation then expand symbol
481 ost_offset_p = (ost_ptr - ost);
482 ost_offset_e = (ost_end - ost);
485 if ((ost_ptr + 12) > ost_end)
487 #warning "!!! Bad logic in realloc call !!!"
488 if ((ost = realloc(ost, (ost_end + OST_BLOCK))) == NULL)
490 printf("OST memory reallocation error.\n");
494 ost_ptr = ost + ost_offset_p;
495 ost_end = (ost + ost_offset_e) + OST_BLOCK;
498 ost_offset_p = (oststr_ptr - oststr);
499 ost_offset_e = (oststr_end - oststr);
501 if ((oststr_ptr + (slen + 1 + 4)) > oststr_end)
503 #warning "!!! Bad logic in realloc call !!!"
504 if ((oststr = realloc(oststr, (oststr_end + OST_BLOCK))) == NULL)
506 printf("OSTSTR memory reallocation error.\n");
510 oststr_ptr = oststr + ost_offset_p;
511 oststr_end = (oststr + ost_offset_e) + OST_BLOCK;
515 // If this is a debug symbol and the include debug symbol flag (-g) is not
516 // set then do nothing
517 if ((type & 0xF0000000) && !gflag)
523 ostresult = OSTLookup(name); // Get symbol index in OST
525 // If the symbol is in the output symbol table and the bflag is set
526 // (don't remove multiply defined locals) and this is not an
527 // external/global symbol *** OR *** the symbol is not in the output
528 // symbol table then add it.
529 if (((ostresult != -1) && bflag && !(type & 0x01000000))
530 || ((ostresult != -1) && gflag && (type & 0xF0000000)) || (ostresult == -1))
532 if ((type & 0xF0000000) == 0x40000000)
533 putlong(ost_ptr, 0x00000000); // Zero string table offset for dbg line
535 putlong(ost_ptr, (oststr_ptr - oststr)); // String table offset of symbol string
537 putlong(ost_ptr + 4, type );
538 putlong(ost_ptr + 8, value);
541 // If the symbol type is anything but a debug line information
542 // symbol then write the symbol string to the string table
543 if ((type & 0xF0000000) != 0x40000000)
545 strcpy(oststr_ptr, name); // Put symbol name in string table
546 *(oststr_ptr + slen) = '\0'; // Add null terminating character
547 oststr_ptr += (slen + 1);
548 putlong(oststr_ptr, 0x00000000); // Null terminating long
549 putlong(oststr, (oststr_ptr - oststr)); // Update size of string table
554 printf("OSTAdd: (%s), type=$%08X, val=$%08X\n", name, type, value);
561 // Not sure about this as it could affect return indices. Needed to stop
568 // Return the index of a symbol in the output symbol table
570 int OSTLookup(char * sym)
573 int stro = 4; // Offset in string table
575 for(i=0; i<ost_index; i++)
577 if (strcmp(oststr + stro, sym) == 0)
580 stro += strlen(oststr + stro) + 1;
588 // Add unresolved externs to the output symbol table
590 int DoUnresolved(void)
592 struct HREC * hptr = unresolved;
594 // Add to OST while unresolved list is valid
597 if (OSTAdd(hptr->h_sym, T_EXT, 0L) == -1)
601 printf("DoUnresolved(): added %s\n", hptr->h_sym);
603 struct HREC * htemp = hptr->h_next;
614 // Update object file TEXT and DATA segments based on relocation records. Take
615 // in an OFILE header and flag (T_TEXT, T_DATA) to process. Return (0) is
616 // successful or non-zero (1) if failed.
618 int RelocateSegment(struct OFILE * ofile, int flag)
620 char * symtab; // Start of symbol table
621 char * symbols; // Start of symbols
622 char * sptr; // Start of segment data
623 char * rptr; // Start of segment relocation records
624 unsigned symidx; // Offset to symbol
625 unsigned addr; // Relocation address
626 unsigned rflg; // Relocation flags
627 unsigned olddata; // Old segment data at reloc address
628 unsigned newdata = 0; // New segment data at reloc address
629 unsigned pad; // Temporary to calculate phrase padding
631 char sym[SYMLEN]; // String for symbol name/hash search
632 int ssidx; // Segment size table index
633 unsigned glblreloc; // Global relocation flag
634 unsigned absreloc; // Absolute relocation flag
635 unsigned relreloc; // Relative relocation flag
636 unsigned swcond; // Switch statement condition
637 unsigned relocsize; // Relocation record size
639 // If there is no TEXT relocation data for the selected object file segment
640 // then update the COF TEXT segment offset allowing for the phrase padding
641 if ((flag == T_TEXT) && !ofile->o_header.absrel.reloc.tsize)
643 // TEXT segment size plus padding
644 pad = ((ofile->o_header.tsize + secalign) & ~secalign);
645 textoffset += (ofile->o_header.tsize + (pad - ofile->o_header.tsize));
648 printf("RelocateSegment(%s, TEXT) : No Relocation Data\n", ofile->o_name);
653 // If there is no DATA relocation data for the selected object file segment
654 // then update the COF DATA and BSS segment offsets allowing for the phrase
656 if ((flag == T_DATA) && !ofile->o_header.absrel.reloc.dsize)
658 // DATA segment size plus padding
659 pad = ((ofile->o_header.dsize + secalign) & ~secalign);
660 dataoffset += (ofile->o_header.dsize + (pad - ofile->o_header.dsize));
661 // BSS segment size plus padding
662 pad = ((ofile->o_header.bsize + secalign) & ~secalign);
663 bssoffset += (ofile->o_header.bsize + (pad - ofile->o_header.bsize));
666 printf("RelocateSegment(%s, DATA) : No Relocation Data\n", ofile->o_name);
671 // Verbose mode information
674 printf("RelocateSegment(%s, %s) : Processing Relocation Data\n",
675 ofile->o_name, flag == T_DATA ? "DATA" : "TEXT");
678 // Obtain pointer to start of symbol table
679 symtab = (ofile->o_image + 32 + ofile->o_header.tsize
680 + ofile->o_header.dsize
681 + ofile->o_header.absrel.reloc.tsize
682 + ofile->o_header.absrel.reloc.dsize);
684 // Obtain pointer to start of symbols
685 symbols = symtab + ofile->o_header.ssize;
687 // Obtain pointer to start of TEXT segment
688 sptr = ofile->o_image + 32;
690 // Obtain pointer to start of TEXT relocation records
691 rptr = sptr + (ofile->o_header.tsize + ofile->o_header.dsize);
693 relocsize = ofile->o_header.absrel.reloc.tsize;
696 printf("RELOCSIZE :: %d Records = %d\n", relocsize, relocsize / 8);
698 // Update pointers if DATA relocation records are being processed
701 sptr += ofile->o_header.tsize; // Start of DATA segment
702 rptr += ofile->o_header.absrel.reloc.tsize; // Start of DATA relocation records
703 relocsize = ofile->o_header.absrel.reloc.dsize;
706 // Process each relocation record for the TEXT segment
707 for(i=0; i<(int)relocsize; i+=8)
709 // Obtain both the relocation address and the relocation flags from the
711 addr = getlong(rptr);
712 rflg = getlong(rptr + 4);
713 glblreloc = (rflg & 0x00000010 ? 1 : 0);// Set global relocation flag
714 absreloc = (rflg & 0x00000040 ? 1 : 0); // Set absolute relocation flag
715 relreloc = (rflg & 0x000000A0 ? 1 : 0); // Set relative relocation flag
717 // Additional processing required for global relocations
720 // Obtain the string table index for the relocation symbol, look
721 // for it in the globals hash table to obtain information on that
722 // symbol. For the hash calculation to work correctly it must be
723 // placed in a 'clean' string before looking it up.
724 symidx = getlong(symtab + ((rflg >> 8) * 12));
725 memset(sym, 0, SYMLEN);
726 strcpy(sym, symbols + symidx);
727 olddata = newdata = 0; // Initialise old and new segment data
728 ssidx = OSTLookup(sym);
729 newdata = getlong(ost + ((ssidx - 1) * 12) + 8);
732 // Obtain the existing long word segment data and flip words if the
733 // relocation flags indicate it relates to a RISC MOVEI instruction
734 olddata = getlong(sptr + addr);
737 olddata = _SWAPWORD(olddata);
739 // Process record dependant on segment it relates to; TEXT, DATA or
740 // BSS. Construct a new relocated segment long word based on the
741 // required segment base address, the segment data offset in the
742 // resulting COF file and the offsets from the incoming object file.
743 swcond = (rflg & 0xFFFFFF00);
749 case 0x00000200: // Absolute Value
751 case 0x00000400: // TEXT segment relocation record
752 if (flag == T_TEXT) // Is this a TEXT section record?
753 newdata = tbase + textoffset + olddata;
755 newdata = tbase + dataoffset + olddata; // Nope, must be DATA section
758 case 0x00000600: // DATA segment relocation record
759 newdata = dbase + dataoffset
760 + (olddata - ofile->o_header.tsize);
763 case 0x00000800: // BSS segment relocation record
764 newdata = bbase + bssoffset
765 + (olddata - (ofile->o_header.tsize
766 + ofile->o_header.dsize));
777 // Set absolute (long) or relative (word) address of symbol
780 // Flip the new long word segment data if the relocation record
781 // indicated a RISC MOVEI instruction and place the resulting data
782 // back in the COF segment
784 newdata = _SWAPWORD(newdata);
786 putlong(sptr + addr, newdata);
790 putword(sptr + addr, newdata - tbase - addr - ofile->o_tbase);
793 // Shamus: Let's output some info to aid in debugging this crap
800 sprintf(ssiString, " [ssi:%i]", ssidx);
802 printf("RelocateSegment($%08X): %s, $%08X: $%08X => $%08X%s\n", rflg, (glblreloc ? sym : "(LOCAL)"), addr, olddata, getlong(sptr + addr), ssiString);
805 rptr += 8; // Point to the next relocation record
808 // Update the COF segment offset allowing for the phrase padding.
811 // TEXT segment plus padding
812 pad = ((ofile->o_header.tsize + secalign) & ~secalign);
813 textoffset += (ofile->o_header.tsize + (pad - ofile->o_header.tsize));
817 // DATA segment plus padding
818 pad = ((ofile->o_header.dsize + secalign) & ~secalign);
819 dataoffset += (ofile->o_header.dsize + (pad - ofile->o_header.dsize));
820 // BSS segment plus padding
821 pad = ((ofile->o_header.bsize + secalign) & ~secalign);
822 bssoffset += (ofile->o_header.bsize + (pad - ofile->o_header.bsize));
825 // Return value, should always be zero
831 // Add a path character to the end of string 's' if it doesn't already end with
832 // one. The last occurrance of '/' or '\' in the string is assumed to be the
835 // This is fucking shit. We know what the path delimiter is, its FUCKING
836 // DEFINED IN THE FUCKING HEADER. FOR FUCKS SAKE. AND YES, HOPE TO GOD THERE'S
837 // ENOUGH SPACE IN THE PASSED IN BUFFER TO HOLD THE EXTRA CHARACTER!
839 void AppendPathDelimiter(char * s)
842 // And hope to God that there's enough space in the buffer...
847 if (*s == '/' || *s == '\\')
861 int length = strlen(s);
863 if (s[length - 1] != PATH_DELIMITER)
865 s[length] = PATH_DELIMITER;
866 s[length + 1] = 0; // BUFFER OVERFLOW!!!! FFFFFFFFUUUUUUUUUUUU
873 // Try to open "name", "name.o", "${libdir}name", "${libdir}name.o". Return the
874 // handle of the file successfully opened. p_name is updated to point to a
875 // malloc()'ed string which is the name which actually got opened. p_name will
876 // return unchanged if the file can't be found.
878 int TryOpenFile(char ** p_name)
880 char * name = *p_name;
882 // Note that libdir will be an empty string if there is none specified
883 char * tmpbuf = malloc(strlen(name) + strlen(libdir) + 3);
887 printf("TryOpenFile() : out of memory\n");
891 strcpy(tmpbuf, name);
892 int hasdot = (strrchr(tmpbuf, '.') > strrchr(tmpbuf, PATH_DELIMITER));
893 // Try to open file as passed first
894 int fd = open(tmpbuf, _OPEN_FLAGS);
901 // Try to open file with '.o' added
902 strcat(tmpbuf, ".o");
903 fd = open(tmpbuf, _OPEN_FLAGS);
909 // Try the libdir only if the name isn't already anchored
910 // Shamus: WTH, this makes no sense... Why the ':'? Is this a Macintosh??
911 // if (*name != '/' && *name != '\\' && !strchr(name, ':'))
912 if ((*name != PATH_DELIMITER) && (strchr(name, ':') == NULL))
914 strcpy(tmpbuf, libdir);
915 // Add a trailing path char if there isn't one already
916 AppendPathDelimiter(tmpbuf);
917 strcat(tmpbuf, name);
919 if ((fd = open(tmpbuf, _OPEN_FLAGS)) >= 0)
924 strcat(tmpbuf, ".o");
926 if ((fd = open(tmpbuf, _OPEN_FLAGS)) >= 0)
931 // Couldn't open file at all
934 // There are worse things... :-P
936 tmpbuf = realloc(tmpbuf, strlen(tmpbuf) + 1);
940 printf("TryOpenFile() : out of memory\n");
950 // Archive File Use, Needs to be Removed [Shamus: NON!]
952 void put_name(struct OFILE * p)
954 int flag = *(p->o_arname);
955 printf("%s%s%s", (flag ? (char *)(p->o_arname) : ""), (flag ? ":" : ""), p->o_name);
960 // Collect file names and handles in a buffer so there is less disk activity.
961 // Call DoFile with flag FALSE for normal object files and archives.
962 // Call it with flag TRUE and a symbol name for include files (-i).
964 int DoFile(char * fname, int incFlag, char * sym)
966 // Verbose information
969 printf("DoFile() : `%s' %s", fname, incFlag ? "INCLUDE" : "NORMAL");
972 printf(" symbol %s", sym);
977 // Reached maximum file handles
984 // Attempt to open input file
985 int fd = TryOpenFile(&fname);
989 printf("Cannot find input module %s\n", fname);
993 // The file is open; save its info in the handle and name arrays
995 name[hd] = fname; // This is the name from TryOpenFile()
1001 int temp = strlen(sym); // Get symbol length
1003 // 100 chars is max length of a symbol
1010 // Malloc enough space for two symbols, then build the second one.
1011 // Second one may be one character longer than first
1012 if ((hsym1[hd] = malloc((long)temp + 1)) == NULL
1013 || (hsym2[hd] = malloc((long)temp + 2)) == NULL)
1015 printf("DoFile() : out of memory for include-file symbols\n");
1019 strcpy(hsym1[hd], sym);
1020 strcpy(hsym2[hd], sym);
1026 printf("Last char of %s is already 'x': choose another name\n", sym);
1030 hsym2[hd][99] = 'x';
1034 hsym2[hd][temp] = 'x';
1035 hsym2[hd][temp+1] = '\0';
1039 // Increment next handle index
1047 // Pad TEXT or DATA segment to the requested boundary
1049 int segmentpad(FILE * fd, long segsize, int value)
1051 // long padsize; // Number of pad bytes needed
1052 int i; // Good 'ol iterator
1053 char padarray[32]; // Array of padding bytes
1054 char * padptr; // Pointer to array
1056 // Determine the number of padding bytes that are needed
1057 long padsize = (segsize + secalign) & ~secalign;
1058 padsize = padsize - segsize;
1060 // Fill pad array if padding is required
1067 putword(padptr, value);
1071 symoffset += padsize;
1073 // Write padding bytes
1074 if (fwrite(padarray, padsize, 1, fd) != 1)
1083 // Write the output file
1085 int write_ofile(struct OHEADER * header)
1087 FILE * fd; // File descriptor
1088 unsigned osize; // Object segment size
1089 struct OFILE * otemp; // Object file pointer
1090 int i, j; // Iterators
1091 char himage[0x168]; // Header image (COF = 0xA8)
1092 unsigned tsoff, dsoff, bsoff; // Segment offset values
1093 unsigned index, type, value; // Symbol table index, type and value
1094 short abstype; // ABS symbol type
1095 char symbol[14]; // Symbol record for ABS files
1096 int slen; // Symbol string length
1098 symoffset = 0; // Initialise symbol offset
1100 // Add correct output extension if none
1101 if (strchr(ofile, '.') == NULL)
1104 strcat(ofile, ".cof"); // COF files
1105 else if (aflag && !cflag)
1106 strcat(ofile, ".abs"); // ABS files
1108 strcat(ofile, ".o"); // Object files (partial linking etc)
1111 fd = fopen(ofile, "wb"); // Attempt to open output file
1115 printf("Can't open output file %s\n", ofile); // Error opening output file
1119 // Build the output file header
1120 // Absolute (COF) header
1123 tsoff = dsoff = bsoff = 0xA8; // Initialises segment offsets
1125 // Process each object file segment size to obtain a cumulative segment
1126 // size for both the TEXT and DATA segments
1127 for(i=0; i<(int)obj_index; i++)
1129 dsoff += obj_segsize[i][0]; // Adding TEXT segment sizes
1130 bsoff += obj_segsize[i][0] + obj_segsize[i][1]; // Adding TEXT and DATA segment sizes
1133 // Currently this only builds a COF absolute file. Conditionals and
1134 // additional code will need to be added for ABS and partial linking.
1136 // Build the COF_HDR
1137 putword(himage + 0, 0x0150 ); // Magic Number (0x0150)
1138 putword(himage + 2, 0x0003 ); // Sections Number (3)
1139 putlong(himage + 4, 0x00000000 ); // Date (0L)
1140 putlong(himage + 8, dsoff + header->dsize); // Offset to Symbols Section
1141 putlong(himage + 12, ost_index); // Number of Symbols
1142 putword(himage + 16, 0x001C ); // Size of RUN_HDR (0x1C)
1143 putword(himage + 18, 0x0003 ); // Executable Flags (3)
1145 // Build the RUN_HDR
1146 putlong(himage + 20, 0x00000107 ); // Magic/vstamp
1147 putlong(himage + 24, header->tsize ); // TEXT size in bytes
1148 putlong(himage + 28, header->dsize ); // DATA size in bytes
1149 putlong(himage + 32, header->bsize ); // BSS size in bytes
1150 putlong(himage + 36, tbase ); // Start of executable, normally @TEXT
1151 putlong(himage + 40, tbase ); // @TEXT
1152 putlong(himage + 44, dbase ); // @DATA
1154 // Build the TEXT SEC_HDR
1155 putlong(himage + 48, 0x2E746578 );
1156 putlong(himage + 52, 0x74000000 ); // ".text"
1157 putlong(himage + 56, tbase ); // TEXT START
1158 putlong(himage + 60, tbase ); // TEXT START
1159 putlong(himage + 64, header->tsize ); // TEXT size in bytes
1160 putlong(himage + 68, tsoff ); // Offset to section data in file
1161 putlong(himage + 72, 0x00000000 ); // Offset to section reloc in file (0L)
1162 putlong(himage + 76, 0x00000000 ); // Offset to debug lines structures (0L)
1163 putlong(himage + 80, 0x00000000 ); // Nreloc/nlnno (0L)
1164 putlong(himage + 84, 0x00000020 ); // SEC_FLAGS: STYP_TEXT
1166 // Build the DATA SEC_HDR
1167 putlong(himage + 88, 0x2E646174 );
1168 putlong(himage + 92, 0x61000000 ); // ".data"
1169 putlong(himage + 96, dbase ); // DATA START
1170 putlong(himage + 100, dbase ); // DATA START
1171 putlong(himage + 104, header->dsize ); // DATA size in bytes
1172 putlong(himage + 108, dsoff ); // Offset to section data in file
1173 putlong(himage + 112, 0x00000000 ); // Offset to section reloc in file (0L)
1174 putlong(himage + 116, 0x00000000 ); // Offset to debugging lines structures (0L)
1175 putlong(himage + 120, 0x00000000 ); // Nreloc/nlnno (0L)
1176 putlong(himage + 124, 0x00000040 ); // SEC_FLAGS: STYP_DATA
1178 // Build the BSS SEC_HDR
1179 putlong(himage + 128, 0x2E627373 );
1180 putlong(himage + 132, 0x00000000 ); // ".bss"
1181 putlong(himage + 136, bbase ); // BSS START
1182 putlong(himage + 140, bbase ); // BSS START
1183 putlong(himage + 144, header->bsize ); // BSS size in bytes
1184 putlong(himage + 148, bsoff ); // Offset to section data in file
1185 putlong(himage + 152, 0x00000000 ); // Offset to section reloc in file (0L)
1186 putlong(himage + 156, 0x00000000 ); // Offset to debugging lines structures (0L)
1187 putlong(himage + 160, 0x00000000 ); // Nreloc/nlnno (0L)
1188 putlong(himage + 164, 0x00000080 ); // SEC_FLAGS: STYP_BSS
1190 symoffset = 168; // Update symbol offset
1192 // Absolute (ABS) header
1195 // Build the ABS header
1196 putword(himage + 0, 0x601B ); // Magic Number (0x601B)
1197 putlong(himage + 2, header->tsize ); // TEXT segment size
1198 putlong(himage + 6, header->dsize ); // DATA segment size
1199 putlong(himage + 10, header->bsize ); // BSS segment size
1200 putlong(himage + 14, ost_index * 14 ); // Symbol table size (?)
1201 putlong(himage + 18, 0x00000000 ); //
1202 putlong(himage + 22, tbase ); // TEXT base address
1203 putword(himage + 26, 0xFFFF ); // Flags (?)
1204 putlong(himage + 28, dbase ); // DATA base address
1205 putlong(himage + 32, bbase ); // BSS base address
1207 symoffset = 36; // Update symbol offset
1210 // Write the header, but not if noheaderflag
1211 // Absolute (ABS) header
1215 if (fwrite(himage, 36, 1, fd) != 1)
1218 // Absolute (COF) header
1221 if (fwrite(himage, 168, 1, fd) != 1)
1225 // Write the TEXT segment of each object file
1226 for(otemp=olist; otemp!=NULL; otemp=otemp->o_next)
1228 osize = otemp->o_header.tsize;
1230 // Write only if segment has size
1234 printf("Writing TEXT Segment of %s\n", otemp->o_name);
1236 if (fwrite(otemp->o_image + 32, osize, 1, fd) != 1)
1239 // Pad to required alignment boundary
1240 if (segmentpad(fd, osize, 0x0000))
1247 // Write the DATA segment of each object file
1248 for(otemp=olist; otemp!=NULL; otemp=otemp->o_next)
1250 osize = otemp->o_header.dsize;
1252 // Write only if the segment has size
1256 printf("Writing DATA Segment of %s\n", otemp->o_name);
1258 if (fwrite((otemp->o_image + 32 + otemp->o_header.tsize), osize, 1, fd) != 1)
1261 // Pad to required alignment boundary
1262 if (segmentpad(fd, osize, 0))
1271 // Write the symbols table and string table
1272 // Absolute (COF) symbol/string table
1277 if (fwrite(ost, (ost_ptr - ost), 1, fd) != 1) goto werror;
1278 if (fwrite(oststr, (oststr_ptr - oststr), 1, fd) != 1) goto werror;
1281 // Absolute (ABS) symbol/string table
1284 // The symbol and string table have been created as part of the
1285 // DoSymbol() function and the output symbol and string tables are in
1286 // COF format. For an ABS file we need to process through this to
1287 // create the 14 character long combined symbol and string table.
1288 // Format of symbol table in ABS: AAAAAAAATTVVVV, where (A)=STRING,
1289 // (T)=TYPE & (V)=VALUE
1291 for(i=0; i<ost_index; i++)
1293 memset(symbol, 0, 14); // Initialise symbol record
1294 abstype = 0; // Initialise ABS symbol type
1295 slen = 0; // Initialise symbol string length
1296 index = getlong(ost + (i * 12)); // Get symbol index
1297 type = getlong((ost + (i * 12)) + 4); // Get symbol type
1299 if (type & 0xF0000000)
1300 continue; // Not doing debug symbols
1302 value = getlong((ost + (i * 12)) + 8); // Get symbol value
1303 slen = strlen(oststr + index);
1305 // Get symbol string (maximum 8 chars)
1309 *(symbol + j) = *(oststr + index + j);
1313 for(j=0; j<slen; j++)
1314 *(symbol + j) = *(oststr + index + j);
1317 // Modify to ABS symbol type
1320 case 0x02000000: abstype = (short)ABST_DEFINED; break;
1321 case 0x04000000: abstype = (short)ABST_DEFINED | ABST_TEXT; break;
1322 case 0x05000000: abstype = (short)ABST_DEFINED | ABST_GLOBAL | ABST_TEXT; break;
1323 case 0x06000000: abstype = (short)ABST_DEFINED | ABST_DATA; break;
1324 case 0x07000000: abstype = (short)ABST_DEFINED | ABST_GLOBAL | ABST_DATA; break;
1325 case 0x08000000: abstype = (short)ABST_DEFINED | ABST_BSS; break;
1326 case 0x09000000: abstype = (short)ABST_DEFINED | ABST_GLOBAL | ABST_BSS; break;
1328 printf("write_ofile: abs, cannot determine symbol type\n");
1333 putword(symbol + 8, abstype); // Write back new ABS type
1334 putlong(symbol + 10, value); // Write back value
1336 if (fwrite(symbol, 14, 1, fd) != 1) goto werror; // Write symbol record
1344 printf("Close error on output file %s\n",ofile);
1351 printf("Write error on output file %s\n", ofile);
1352 fclose(fd); // Try to close output file anyway
1358 // Display the Symbol Load Map
1360 int write_map(struct OHEADER * header)
1362 unsigned i, o; // Inner and outer loop iterators
1363 unsigned c; // Column number
1364 unsigned index; // Symbol string index
1365 unsigned type; // Symbol type
1366 unsigned value; // Symbol value
1367 char * symbol; // Symbol string value
1370 return 0; // Return if no symbols to map
1372 printf("LOAD MAP\n\n");
1374 // Outer loop for each of the symbol areas to map out;
1375 // 0 = NON-RELOCATABLE SYMBOLS
1376 // 1 = TEXT-SEGMENT RELOCATABLE SYMBOLS
1377 // 2 = DATA-SEGMENT RELOCATABLE SYMBOLS
1378 // 3 = BSS-SEGMENT RELOCATABLE SYMBOLS
1381 // Display the correct map header for the symbols being processed
1384 case 0: printf("NON-RELOCATABLE SYMBOLS\n\n"); break;
1385 case 1: printf("TEXT-SEGMENT RELOCATABLE SYMBOLS\n\n"); break;
1386 case 2: printf("DATA-SEGMENT RELOCATABLE SYMBOLS\n\n"); break;
1387 case 3: printf("BSS-SEGMENT RELOCATABLE SYMBOLS\n\n"); break;
1390 c = 0; // Initialise column number
1392 // Inner loop to process each record in the symbol table
1393 for(i=0; i<(unsigned)ost_index; i++)
1395 index = getlong(ost + (i * 12)); // Get symbol string index
1396 type = getlong(ost + (i * 12) + 4); // Get symbol type
1397 value = getlong(ost + (i * 12) + 8); // Get symbol value
1398 symbol = oststr + index; // Get symbol string
1400 // Display only three columns
1407 // If local symbols not included and the type is local then go to
1408 // next symbol record
1409 if (!lflag & !(type & 0x01000000))
1412 // Output each symbol to the display, dependant on type
1416 // Non-relocatable symbols
1417 if (type == 0x02000000 || type == 0x03000000)
1419 printf("%-8s %c %08X ", symbol, (type & 0x01000000) ? 'G' : 'L', value);
1425 // TEXT segment relocatable symbols
1426 if (type == 0x04000000 || type == 0x05000000)
1428 printf("%-8s %c %08X ", symbol, (type & 0x01000000) ? 'G' : 'L', value);
1434 // DATA segment relocatble symbols
1435 if (type == 0x06000000 || type == 0x07000000)
1437 printf("%-8s %c %08X ", symbol, (type & 0x01000000) ? 'G' : 'L', value);
1443 // BSS segment relocatable symbols
1444 if (type == 0x08000000 || type == 0x09000000)
1446 printf("%-8s %c %08X ", symbol, (type & 0x01000000) ? 'G' : 'L', value);
1462 // Convert ASCII to hexadecimal
1464 //int atolx(char * string, long * value)
1465 int atolx(char * string, int * value)
1469 while (isxdigit(*string))
1471 if (isdigit(*string))
1473 *value = (*value << 4) + (*string++ - '0');
1477 if (isupper(*string))
1478 *string = tolower(*string);
1480 *value = (*value << 4) + ((*string++ - 'a') + 10);
1484 if (*string != '\0')
1486 printf("Invalid hexadecimal value");
1495 // Stuff the (long) value of a string into the value argument. RETURNS TRUE if
1496 // the string doesn't parse. Parses only as a hex string.
1498 int getval(char * string, int * value)
1500 return atolx(string, value);
1505 // Create one big .o file from the images already in memory, returning a
1506 // pointer to an OHEADER. Note that the oheader is just the header for the
1507 // output (plus some other information). The text, data, and fixups are all
1508 // still in the ofile images hanging off the global `olist'.
1510 struct OHEADER * make_ofile()
1512 unsigned tptr, dptr, bptr; // Bases in runtime model
1513 int ret = 0; // Return value
1514 struct OFILE * otemp, * oprev, * ohold; // Object file list pointers
1515 struct OHEADER * header; // Output header pointer
1517 // Initialize cumulative segment sizes
1518 textsize = datasize = bsssize = 0;
1520 // For each object file, accumulate the sizes of the segments but remove
1521 // those object files which are unused
1522 oprev = NULL; // Init previous obj file list ptr
1523 otemp = olist; // Set temp pointer to object file list
1525 while (otemp != NULL)
1527 // UNUSED !!!!! (it's !O_ARCHIVE, so this code executes.)
1528 if (!(otemp->o_flags & O_ARCHIVE))
1530 if ((otemp->o_flags & O_USED) == 0)
1534 printf("Unused object file ");
1536 printf(" discarded.\n");
1540 olist = otemp->o_next;
1542 oprev->o_next = otemp->o_next;
1546 if (!ohold->isArchiveFile)
1547 free(ohold->o_image);
1553 // Increment total of segment sizes ensuring requested
1555 textsize += (otemp->o_header.tsize + secalign) & ~secalign;
1556 datasize += (otemp->o_header.dsize + secalign) & ~secalign;
1557 bsssize += (otemp->o_header.bsize + secalign) & ~secalign;
1562 // Go to next object file list pointer
1563 otemp = otemp->o_next;
1566 // Update base addresses and create symbols _TEXT_E, _DATA_E and _BSS_E
1568 OSTAdd("_TEXT_E", 0x05000000, tval + textsize);
1572 // DATA follows TEXT
1573 dbase = tval + textsize;
1574 OSTAdd("_DATA_E", 0x07000000, tval + textsize + datasize);
1579 bbase = tval + textsize + datasize;
1580 OSTAdd("_BSS_E", 0x09000000, tval + textsize + datasize + bsssize);
1584 // BSS is independant of DATA
1586 OSTAdd("_BSS_E", 0x09000000, bval + bsssize);
1591 // DATA is independant of TEXT
1593 OSTAdd("_DATA_E", 0x07000000, dval + datasize);
1598 bbase = dval + datasize;
1599 OSTAdd("_BSS_E", 0x09000000, dval + datasize + bsssize);
1603 // BSS is independant of DATA
1605 OSTAdd("_BSS_E", 0x09000000, bval + bsssize);
1609 // Place each unresolved symbol in the output symbol table
1613 // Initialise base addresses
1614 tptr = dptr = bptr = 0;
1616 // For each file, relocate its symbols and add them to the output symbol
1621 while (otemp != NULL)
1623 otemp->o_tbase = tptr;
1625 // Do rest only for non-ARCHIVE markers
1626 if (!(otemp->o_flags & O_ARCHIVE))
1628 otemp->o_dbase = dptr;
1629 otemp->o_bbase = bptr;
1630 tptr += (otemp->o_header.tsize + secalign) & ~secalign;
1631 dptr += (otemp->o_header.dsize + secalign) & ~secalign;
1632 bptr += (otemp->o_header.bsize + secalign) & ~secalign;
1635 // For each symbol, (conditionally) add it to the ost
1636 // For ARCHIVE markers, this adds the symbol for the file & returns
1637 // (Shamus: N.B. it does no such thing ATM)
1638 if (DoSymbol(otemp))
1641 if (otemp->o_flags & O_ARCHIVE)
1643 // Now that the archive is marked, remove it from list
1645 olist = otemp->o_next;
1647 oprev->o_next = otemp->o_next;
1652 if (!ohold->isArchiveFile)
1653 free(ohold->o_image);
1662 otemp = otemp->o_next;
1665 // Places all the externs, globals etc into the output symbol table
1666 if (DoCommon() == -1)
1669 // Create a new output file header
1670 header = new_oheader();
1674 printf("make_ofile: out of memory!\n");
1678 // Fill in the output header. Does not match the actual output but values
1679 // used as reference
1680 header->magic = 0x0150; // COF magic number
1681 header->tsize = textsize; // TEXT segment size
1682 header->dsize = datasize; // DATA segment size
1683 header->bsize = bsssize; // BSS segment size
1684 header->ssize = (ost_ptr - ost); // Symbol table size
1685 header->ostbase = ost; // Output symbol table base address
1687 // For each object file, relocate its TEXT and DATA segments. OR the result
1688 // into ret so all files get moved (and errors reported) before returning
1689 // with the error condition
1690 for(otemp=olist; otemp!=NULL; otemp=otemp->o_next)
1692 if (!(otemp->o_flags & O_ARCHIVE))
1694 ret |= RelocateSegment(otemp, T_TEXT); // TEXT segment relocations
1695 ret |= RelocateSegment(otemp, T_DATA); // DATA segment relocations
1699 // Done with global symbol hash tables
1702 return (ret ? (struct OHEADER *)NULL : header);
1707 // Add symbol to hash list
1709 int AddSymbolToHashList(struct HREC ** hptr, char * sym, struct OFILE * ofile,
1710 long value, int type)
1712 struct HREC * htemp = new_hrec();
1716 printf("Out of memory\n");
1720 // Shamus: Moar testing...
1723 printf("AddSymbolToHashList(): hptr=$%08X, sym=\"%s\", ofile=$%08X, value=$%X, type=$%X\n", hptr, sym, ofile, value, type);
1726 // Populate hash record
1727 memset(htemp->h_sym, 0, SYMLEN);
1728 strcpy(htemp->h_sym, sym);
1729 htemp->h_ofile = ofile;
1730 htemp->h_value = value;
1731 htemp->h_type = type;
1733 // Add new hash to the front of the list (hence the ** for hptr)
1734 htemp->h_next = *hptr;
1742 // Add symbol to the unresolved symbols hash table (really, it's a linked list)
1744 int AddUnresolvedSymbol(char * sym, struct OFILE * ofile)
1747 printf("AddUnresolvedSymbol(%s, %s)\n", sym, ofile->o_name);
1749 return AddSymbolToHashList(&unresolved, sym, ofile, 0L, 0);
1754 // Remove the HREC from the unresolved symbol list, and pass back a pointer
1755 // to the spot where the HREC was.
1757 struct HREC * RemoveUnresolvedSymbol(struct HREC * hrec)
1759 struct HREC * ptr = unresolved;
1760 struct HREC * previous = NULL;
1762 while ((ptr != hrec) && (ptr != NULL))
1772 struct HREC * next = ptr->h_next;
1774 // Remove the head if nothing previous, otherwise, remove what we found
1775 if (previous == NULL)
1778 previous->h_next = next;
1786 // Add symbol to the unresolved symbols hash table
1788 int AddARSymbol(char * sym, struct OFILE * ofile)
1791 printf("AddARSymbol(%s, %s)\n", sym, ofile->o_name);
1793 return AddSymbolToHashList(&arSymbol, sym, ofile, 0L, 0);
1798 // Generate hash value from the 1st 15 characters of the symbol modulo the
1799 // number of buckets in the hash.
1801 int GetHash(char * s)
1803 // For this to be consistent, the symbol MUST be zeroed out beforehand!
1805 memset(c, 0, SYMLEN);
1808 int i = (c[0] + c[1] + c[2] + c[3] + c[4] + c[5] + c[6] + c[7] + c[8]
1809 + c[9] + c[10] + c[11] + c[12] + c[13] + c[14]) % NBUCKETS;
1815 // Lookup a symbol in the hash table.
1816 // Returns either a pointer to the HREC or NULL if not found.
1818 struct HREC * LookupHREC(char * symbol)
1820 struct HREC * hptr = htable[GetHash(symbol)];
1822 while (hptr != NULL)
1824 //This is utter failure...
1825 // if (symcmp(symbol, hptr->h_sym)) <-- left here for giggles :D - LinkoVitch
1826 // Return hash record pointer if found
1827 if (strcmp(symbol, hptr->h_sym) == 0)
1830 hptr = hptr->h_next;
1838 // Lookup a symbol in the AR symbol table.
1839 // Returns either a pointer to the HREC or NULL if not found.
1841 struct HREC * LookupARHREC(char * symbol)
1843 struct HREC * hptr = arSymbol;//htable[GetHash(symbol)];
1845 while (hptr != NULL)
1847 // Return hash record pointer if found
1848 if (strcmp(symbol, hptr->h_sym) == 0)
1851 hptr = hptr->h_next;
1859 // Add symbol to the hash table if it doesn't already exist, otherwise decide
1860 // what to do since it's already defined in another unit.
1862 int DealWithSymbol(char * sym, long type, long value, struct OFILE * ofile)
1866 printf("DealWithSymbol(%s,%s,%lx,", sym, ofile->o_name, value);
1867 printf("%x,%s)\n", (unsigned int)type, (isglobal(type) ? "GLOBAL" : "COMMON"));
1870 struct HREC * hptr = LookupHREC(sym);
1873 return AddSymbolToHashList(&htable[GetHash(sym)], sym, ofile, value, type);
1875 // Symbol is already in table... Figure out how to handle!
1876 if (iscommon(type) && !iscommon(hptr->h_type))
1878 // Mismatch: global came first; warn and keep the global one
1881 printf("Warning: %s: global from ", sym);
1882 put_name(hptr->h_ofile);
1883 printf(" used, common from ");
1885 printf(" discarded.\n");
1888 putword(sym + 8, ABST_EXTERN);
1889 putlong(sym + 10, 0L);
1891 else if (iscommon(hptr->h_type) && !iscommon(type))
1893 // Mismatch: common came first; warn and keep the global one
1896 printf("Warning: %s: global from ", sym);
1898 printf(" used, common from ");
1899 put_name(hptr->h_ofile);
1900 printf(" discarded.\n");
1903 hptr->h_type = type;
1904 hptr->h_ofile = ofile;
1905 hptr->h_value = value;
1907 // They're both global [WRONG! Passed in one is global]
1908 // else if (!iscommon(type))
1909 //is this now right??
1910 else if (!iscommon(type) && !iscommon(hptr->h_type))
1912 // Global exported by another ofile; warn and make this one extern
1915 printf("Duplicate symbol %s: ", sym);
1916 put_name(hptr->h_ofile);
1919 printf(" discarded\n");
1922 putword(sym + 8, ABST_EXTERN);
1924 // They're both common
1927 if (hptr->h_value < value)
1929 hptr->h_value = value;
1930 hptr->h_ofile = ofile;
1939 // Add the imported symbols from this file to unresolved, and the global and
1940 // common (???) symbols to the exported hash table.
1942 // Change old-style commons (type == T_EXTERN, value != 0) to new-style ones
1943 // (type == (T_GLOBAL | T_EXTERN)). [??? O_o]
1945 int AddSymbols(struct OFILE * Ofile)
1947 struct HREC * hptr; // Hash record pointer
1948 char symbol[SYMLEN];
1951 printf("Add symbols for file %s\n", Ofile->o_name);
1953 // Get base pointer, start of sym fixups
1954 char * ptr = Ofile->o_image + 32
1955 + Ofile->o_header.tsize
1956 + Ofile->o_header.dsize
1957 + Ofile->o_header.absrel.reloc.tsize
1958 + Ofile->o_header.absrel.reloc.dsize;
1959 char * sfix = ptr; // Set symbol fixup pointer
1960 char * sstr = sfix + Ofile->o_header.ssize; // Set symbol table pointer
1961 long nsymbols = Ofile->o_header.ssize / 12; // Obtain number of symbols
1965 long index = getlong(sfix); // Get symbol string index
1966 long type = getlong(sfix + 4); // Get symbol type
1967 long value = getlong(sfix + 8); // Get symbol value
1968 memset(symbol, 0, SYMLEN);
1969 strcpy(symbol, sstr + index);
1971 if ((Ofile->isArchiveFile) && !(Ofile->o_flags & O_USED))
1973 if ((type & T_EXT) && (type & (T_SEG | T_ABS)))
1974 if (AddARSymbol(symbol, Ofile))
1977 else if (type == T_EXT)
1979 // External symbal that is *not* in the current unit
1980 hptr = LookupHREC(symbol);
1983 hptr->h_ofile->o_flags |= O_USED; // Mark .o file as used
1984 // Otherwise add to unresolved list
1985 else if (AddUnresolvedSymbol(symbol, Ofile))
1986 return 1; // Error if addition failed
1988 else if ((type & T_EXT) && (type & (T_SEG | T_ABS)))
1990 // Symbol in the current unit that is also EXPORTED
1991 if (DealWithSymbol(symbol, type, value, Ofile))
1992 return 1; // Error if addition failed
1995 sfix += 12; // Increment symbol fixup pointer
1996 nsymbols--; // Decrement num of symbols to process
1999 // Success loading symbols
2005 // Process object file for symbols
2007 int DoItem(struct OFILE * obj)
2009 // Allocate memory for object record ptr
2010 struct OFILE * Ofile = new_ofile();
2014 printf("Out of memory while processing %s\n", obj->o_name);
2018 // Starting after all pathnames, etc., copy .o file name to Ofile
2019 char * temp = path_tail(obj->o_name);
2021 // Check filename length
2022 if (strlen(temp) > FNLEN - 1)
2024 printf("File name too long: %s\n", temp);
2028 // Check archive name length
2029 if (strlen(obj->o_arname) > FNLEN - 1)
2031 printf("Archive name too long: %s\n", obj->o_arname);
2035 strcpy(Ofile->o_name, temp); // Store filename
2036 strcpy(Ofile->o_arname, obj->o_arname); // Store archive name
2038 // Initialise object record information
2039 Ofile->o_next = NULL;
2043 Ofile->o_flags = obj->o_flags;
2044 Ofile->o_image = obj->o_image;
2045 Ofile->isArchiveFile = obj->isArchiveFile;
2046 char * ptr = obj->o_image;
2048 // Don't do anything if this is just an ARCHIVE marker, just add the file
2050 // (Shamus: N.B. it does no such ARCHIVE thing ATM)
2051 if (!(obj->o_flags & O_ARCHIVE))
2053 Ofile->o_header.magic = getlong(ptr);
2054 Ofile->o_header.tsize = getlong(ptr + 4);
2055 Ofile->o_header.dsize = getlong(ptr + 8);
2056 Ofile->o_header.bsize = getlong(ptr + 12);
2057 Ofile->o_header.ssize = getlong(ptr + 16);
2058 Ofile->o_header.absrel.reloc.tsize = getlong(ptr + 24);
2059 Ofile->o_header.absrel.reloc.dsize = getlong(ptr + 28);
2061 // Round BSS off to alignment boundary
2062 Ofile->o_header.bsize = (Ofile->o_header.bsize + secalign) & ~secalign;
2064 if ((Ofile->o_header.dsize & 7) && wflag)
2066 printf("Warning: data segment size of ");
2068 printf(" is not a phrase multiple\n");
2071 // Check for odd segment sizes
2072 if ((Ofile->o_header.tsize & 1) || (Ofile->o_header.dsize & 1)
2073 || (Ofile->o_header.bsize & 1))
2075 printf("Error: odd-sized segment in ");
2077 printf("; link aborted.\n");
2081 if (AddSymbols(Ofile))
2085 // Add this file to the olist
2089 olast->o_next = Ofile;
2097 // Handle items in processing list.
2099 // After loading all objects, archives & include files, we now go and process
2100 // each item on the processing list (plist). Once this is done, we go through
2101 // any unresolved symbols left and see if they have shown up.
2103 int ProcessLists(void)
2105 // Process object file list first (adds symbols from each unit & creates
2107 while (plist != NULL)
2112 struct OFILE * ptemp = plist;
2113 plist = plist->o_next;
2119 // Process the unresolved symbols list. This may involve pulling in symbols
2120 // from any included .a units. Such units are lazy linked by default; we
2121 // generally don't want everything they provide, just what's refenced.
2122 for(uptr=unresolved; uptr!=NULL; )
2125 printf("LookupHREC(%s) => ", uptr->h_sym);
2127 struct HREC * htemp = LookupHREC(uptr->h_sym);
2131 // Found it in the symbol table!
2133 printf("%s in %s (=$%06X)\n", (isglobal(htemp->h_type) ? "global" : "common"), htemp->h_ofile->o_name, htemp->h_value);
2135 // Mark the .o unit that the symbol is in as seen & remove from the
2137 htemp->h_ofile->o_flags |= O_USED;
2138 uptr = RemoveUnresolvedSymbol(uptr);
2145 // Check to see if the unresolved symbol is on the AR symbol list.
2146 htemp = LookupARHREC(uptr->h_sym);
2148 // If the unresolved symbol is in a .o unit that is unused, we can
2149 // drop it; same if the unresolved symbol is in the exported AR
2150 // symbol list. Otherwise, go to the next unresolved symbol.
2151 if (!(uptr->h_ofile->o_flags & O_USED) || (htemp != NULL))
2152 uptr = RemoveUnresolvedSymbol(uptr);
2154 uptr = uptr->h_next;
2156 // Now that we've possibly deleted the symbol from unresolved list
2157 // that was also in the AR list, we add the symbols from this .o
2158 // unit to the symbol table, mark the .o unit as used, and restart
2159 // scanning the unresolved list as there is a good possibility that
2160 // the symbols in the unit we're adding has unresolved symbols as
2164 htemp->h_ofile->o_flags |= O_USED;
2165 AddSymbols(htemp->h_ofile);
2171 // Show files used if the user requests it.
2174 printf("Files used:\n");
2175 struct OFILE * filePtr = olist;
2177 while (filePtr != NULL)
2179 if (filePtr->o_flags & O_USED)
2181 printf(" %s%s%s\n", filePtr->o_name, (filePtr->isArchiveFile ? ":" : ""), (filePtr->isArchiveFile ? filePtr->o_arname : nullStr));
2184 filePtr = filePtr->o_next;
2193 // Extract filename from path
2195 char * path_tail(char * name)
2197 char * temp = strrchr(name, PATH_DELIMITER);
2199 // Return what was passed in if path delimiter was not found
2208 // Add input file to processing list
2210 int AddToProcessingList(char * ptr, char * fname, char * arname, uint8_t arFile)
2214 // First time object record allocation
2215 plist = new_ofile();
2220 // Next object record allocation
2221 plast->o_next = new_ofile();
2222 plast = plast->o_next;
2227 printf("Out of memory.\n"); // Error if memory allocation fails
2231 fname = path_tail(fname);
2232 arname = path_tail(arname);
2234 if (strlen(fname) > FNLEN - 1)
2236 // Error on excessive filename length
2237 printf("File name too long: %s (sorry!)\n", fname);
2241 if (strlen(arname) > FNLEN - 1)
2243 // Error on excessive filename length
2244 printf("AR file name too long: %s (sorry!)\n", arname);
2248 strcpy(plast->o_name, fname); // Store filename sans path
2249 strcpy(plast->o_arname, arname); // Store archive name sans path
2250 plast->o_image = ptr; // Store data pointer
2251 plast->o_flags = (arFile ? 0 : O_USED); // File is used if NOT in archive
2252 plast->o_next = NULL; // Initialise next record pointer
2253 plast->isArchiveFile = arFile; // Shamus: Temp until can sort it out
2255 return 0; // Return without errors
2260 // Process in binary include files and add them to the processing list. This
2261 // routine takes in the binary file and creates an 'object' file in memory.
2262 // Sym1/Sym2 point to the start and end of data.
2264 // Image size for include files is:
2265 // Header ....... 32 bytes
2266 // Data ......... dsize
2267 // Sym Fixups ... 2 * 12 bytes
2268 // Symbol Size .. 4 bytes (Value to include symbols and terminating null)
2269 // Symbols ...... (strlen(sym1) + 1) + (strlen(sym2) + 1)
2270 // Terminate .... 4 bytes (0x00000000)
2272 int LoadInclude(char * fname, int handle, char * sym1, char * sym2, int segment)
2274 long fsize, dsize, size; // File, DATA segment and image sizes
2275 char * ptr, * sptr; // Data pointers
2277 int sym1len = 0; // Symbol 1 length
2278 int sym2len = 0; // Symbol 2 length
2279 unsigned symtype = 0;
2281 fsize = FileSize(handle); // Get size of include file
2282 dsize = (fsize + secalign) & ~secalign; // Round up to a alignment boundary
2284 sym1len = strlen(sym1) + 1; // Get sym1 length + null termination
2285 sym2len = strlen(sym2) + 1; // Get sym2 length + null termination
2287 size = 32 + dsize + 24 + 4 + sym1len + sym2len + 4;
2289 // Use calloc so the header & fixups initialize to zero
2290 // Allocate object image memory
2291 if ((ptr = calloc(size, 1)) == NULL)
2293 printf("Out of memory while including %s\n", fname);
2298 // Read in binary data
2299 if (read(handle, ptr + 32, fsize) != fsize)
2301 printf("File read error on %s\n", fname);
2309 strcpy(obj_fname[obj_index], path_tail(fname));
2311 // Build this image's dummy header
2312 putlong(ptr, 0x00000107); // Magic number
2316 putlong(ptr+4, dsize); // Text size
2317 putlong(ptr+8, 0L); // Data size
2318 symtype = 0x05000000;
2319 obj_segsize[obj_index][0] = dsize;
2320 obj_segsize[obj_index][1] = 0;
2321 obj_segsize[obj_index][2] = 0;
2325 putlong(ptr+4, 0L); // Text size
2326 putlong(ptr+8, dsize); // Data size
2327 symtype = 0x07000000;
2328 obj_segsize[obj_index][0] = 0;
2329 obj_segsize[obj_index][1] = dsize;
2330 obj_segsize[obj_index][2] = 0;
2333 obj_index++; // Increment object count
2335 putlong(ptr+12, 0L); // BSS size
2336 putlong(ptr+16, 24); // Symbol table size
2337 putlong(ptr+20, 0L); // Entry point
2338 putlong(ptr+24, 0L); // TEXT relocation size
2339 putlong(ptr+28, 0L); // DATA relocation size
2341 sptr = ptr + 32 + dsize; // Set sptr to symbol table location
2343 putlong(sptr, 4L); // String offset of symbol1
2344 putlong(sptr+4, symtype); // Symbol type
2345 putlong(sptr+8, 0x00000000); // Symbol has no value (START)
2346 putlong(sptr+12, 4L + (sym2len - 1)); // String offset of symbol2
2347 putlong(sptr+16, symtype); // Symbol type
2348 putlong(sptr+20, dsize); // Symbol is data size (END)
2350 sptr = ptr + 32 + dsize + 24; // Set sptr to symbol table size loc
2352 putlong(sptr, sym1len + 4L); // Size of symbol table
2354 sptr = ptr + 32 + dsize + 24 + 4; // Set sptr to symbol table location
2356 for(i=0; i<(sym1len-1); i++) // Write symbol1 to string table
2359 sptr += (sym1len - 1); // Step past symbol string
2360 *sptr = '\0'; // Terminate symbol string
2361 sptr += 1; // Step past termination
2363 for(i=0; i<(sym2len-1); i++) // Write symbol2 to string table
2366 sptr += (sym2len - 1); // Step past symbol string
2367 *sptr = '\0'; // Terminate symbol string
2368 sptr += 1; // Step past termination
2370 putlong(sptr, 0L); // Terminating long for object file
2372 return AddToProcessingList(ptr, fname, nullStr, 0);
2377 // Takes a file name, gets in its image, puts it on plist. The image may
2378 // already be in memory: If so, the ptr arg is non-null. If so, the file is
2379 // already closed. Note that the file is already open (from DoFile()). RETURNS
2380 // a pointer to the OFILE structure for this file, so you can diddle its flags
2381 // (DoFile sets O_USED for files on the command line).
2383 int LoadObject(char * fname, int fd, char * ptr)
2387 long size = FileSize(fd);
2389 // Allocate memory for file data
2394 printf("Out of memory while processing %s\n", fname);
2399 // Read in file data
2400 if (read(fd, ptr, size) != size)
2402 printf("File read error on %s\n", fname);
2408 // SCPCD : get the name of the file instead of all pathname
2409 strcpy(obj_fname[obj_index], path_tail(fname));
2410 obj_segsize[obj_index][0] = (getlong(ptr + 4) + secalign) & ~secalign;
2411 obj_segsize[obj_index][1] = (getlong(ptr + 8) + secalign) & ~secalign;
2412 obj_segsize[obj_index][2] = (getlong(ptr + 12) + secalign) & ~secalign;
2415 close(fd); // Close file
2418 // Now add this image to the list of pending ofiles (plist)
2419 // This routine is shared by LoadInclude after it builds the image
2420 return AddToProcessingList(ptr, fname, nullStr, 0);
2425 // What it says on the tin: check for a .o suffix on the passed in string
2427 uint8_t HasDotOSuffix(char * s)
2429 char * temp = strrchr(s, '.');
2431 if ((temp == NULL) || (strncmp(temp, ".o", 2) != 0))
2439 // Process an ar archive file (*.a)
2441 int LoadArchive(char * fname, int fd)
2443 // Read in the archive file to memory and process
2444 long size = FileSize(fd);
2445 char * ptr = malloc(size);
2446 char * endPtr = ptr + size;
2447 char * longFilenames = NULL;
2451 printf("Out of memory while processing %s\n", fname);
2456 if (read(fd, ptr, size) != size)
2458 printf("File read error on %s\n", fname);
2466 // Save the pointer for later...
2467 arPtr[arIndex++] = ptr;
2468 char objName[FNLEN];
2471 //printf("\nProcessing AR file \"%s\"...\n", fname);
2474 // Loop through all objects in the archive and process them
2477 memset(objName, 0, 17);
2482 // if ((ptr[i] == '/') || (ptr[i] == ' '))
2483 if ((ptr[i] == ' ') && (i != 0))
2489 objName[i] = ptr[i];
2494 if (ptr[48 + i] == ' ')
2500 objSize[i] = ptr[48 + i];
2503 // Check to see if a long filename was requested
2504 if (objName[0] == 0x20)
2506 uint32_t fnSize = atoi(objName + 1);
2508 if (longFilenames != NULL)
2511 char * currentFilename = longFilenames + fnSize;
2513 while (*currentFilename != 0x0A)
2514 objName[i++] = *currentFilename++;
2520 if ((strncmp(objName, "ARFILENAMES/", 12) == 0) || (strncmp(objName, "//", 2) == 0))
2522 longFilenames = ptr + 60;
2524 else if (HasDotOSuffix(objName))
2526 //printf("Processing object \"%s\" (size == %i, obj_index == %i)...\n", objName, atoi(objSize), obj_index);
2527 strcpy(obj_fname[obj_index], objName);
2528 obj_segsize[obj_index][0] = (getlong(ptr + 60 + 4) + secalign) & ~secalign;
2529 obj_segsize[obj_index][1] = (getlong(ptr + 60 + 8) + secalign) & ~secalign;
2530 obj_segsize[obj_index][2] = (getlong(ptr + 60 + 12) + secalign) & ~secalign;
2533 if (AddToProcessingList(ptr + 60, objName, fname, 1))
2537 uint32_t size = atoi(objSize);
2538 size += (size & 0x01 ? 1 : 0);
2541 while (ptr < endPtr);
2548 // Process files (*.o, *.a) passed in on the command line
2550 int ProcessFiles(void)
2553 char magic[8]; // Magic header number (4 bytes for *.o, 8 for *.a)
2555 // Process all file handles
2556 for(i=0; i<(int)hd; i++)
2558 // Verbose mode information
2560 printf("Read file %s%s\n", name[i], (hflag[i] ? " (include)" : ""));
2564 // Attempt to read file magic number (OBJECT/ARCHIVE FILES)
2565 if (read(handle[i], magic, 8) != 8)
2567 printf("Error reading file %s\n", name[i]);
2572 lseek(handle[i], 0L, 0); // Reset to start of input file
2574 // Look for RMAC/MAC/GCC ($20107) object files
2575 if ((getlong(magic) == 0x00000107) || (getlong(magic) == 0x00020107))
2577 // Process input object file
2578 if (LoadObject(name[i], handle[i], 0L))
2581 // Otherwise, look for an object archive file
2582 else if (strncmp(magic, "!<arch>\x0A", 8) == 0)
2584 if (LoadArchive(name[i], handle[i]))
2589 // Close file and error
2590 printf("%s is not a supported object or archive file\n", name[i]);
2598 // If hflag[i] is 1, include this in the data segment; if 2, put it
2600 if (LoadInclude(name[i], handle[i], hsym1[i], hsym2[i], hflag[i] - 1))
2605 // Free include, symbol & object handles
2606 for(i=0; i<(int)hd; i++)
2617 // Reset next handle indicator
2624 // Load newargv with pointers to arguments found in the buffer
2626 int parse(char * buf, char * newargv[])
2631 printf("begin parsing\n");
2635 while (*buf && strchr(",\t\n\r\14 ", *buf))
2639 if (*buf == '\0' || *buf == 26)
2643 printf("No commands in command file\n");
2652 /* test for comment */
2655 /* found a comment; skip to next \n and start over */
2656 while (*buf && *buf != '\n')
2664 printf("Too many arguments in command file\n");
2670 while (!strchr(",\t\n\r\14 ", *buf))
2672 if (*buf == '\0' || *buf == 26)
2674 printf("Finished parsing %d args\n", i);
2684 printf("argv[%d] = \"%s\"\n", i, newargv[i]);
2692 // Process in a Link Command File
2694 int docmdfile(char * fname)
2696 int fd; // File descriptor
2697 unsigned size; // Command file size
2698 char * ptr; // Pointer
2699 int newargc; // New argument count
2700 char * (*newargv)[]; // New argument value array
2702 // Verbose information
2704 printf("docmdfile(%s)\n", fname);
2706 // Allocate memory for new argument values
2707 newargv = malloc((long)(sizeof(char *) * MAXARGS));
2711 printf("Out of memory.\n");
2715 // Attempt to open and read in the command file
2718 if ((fd = open(fname, _OPEN_FLAGS)) < 0)
2720 printf("Cannot open command file %s.\n", fname);
2724 size = FileSize(fd);
2726 if ((ptr = malloc(size + 1)) == NULL)
2728 printf("Out of memory.\n");
2733 if (read(fd, ptr, size) != (int)size)
2735 printf("Read error on command file %s.\n", fname);
2740 *(ptr + size) = 0; // Null terminate the buffer
2745 printf("No command filename specified\n");
2749 // Parse the command file
2750 if ((newargc = parse(ptr, *newargv)) == -1)
2755 // Process the inputted flags
2756 if (doargs(newargc, *newargv))
2758 printf("docmdfile: doargs returns TRUE\n");
2770 // Take an Argument List and Parse the Command Line
2772 int doargs(int argc, char * argv[])
2774 int i = 1; // Iterator
2775 int c; // Command line character
2776 char * ifile, * isym; // File name and symbol name for -i
2778 // Parse through option switches & files
2781 // Process command line switches
2782 if (argv[i][0] == '-')
2786 printf("Illegal option argument: %s\n\n", argv[i]);
2791 c = argv[i++][1]; // Get next character in command line
2793 // Process command line switch
2796 case '?': // Display usage information
2803 case 'A': // Set absolute linking on
2809 printf("Not enough arguments to -a\n");
2813 aflag = 1; // Set abs link flag
2814 // Segment order is TEXT, DATA, BSS
2815 // Text segment can be 'r', 'x' or a value
2818 if ((*argv[i] == 'r' || *argv[i] == 'R') && !argv[i][1])
2820 ttype = -1; // TEXT segment is relocatable
2822 else if ((*argv[i] == 'x' || *argv[i] == 'X'))
2824 printf("Error in text-segment address: cannot be contiguous\n");
2827 else if ((ttype = 0), getval(argv[i], &tval))
2829 printf("Error in text-segment address: %s is not 'r', 'x' or an address.", argv[i]);
2834 // Data segment can be 'r', 'x' or a value
2837 if ((*argv[i] == 'r' || *argv[i] == 'R') && !argv[i][1])
2839 dtype = -1; // DATA segment is relocatable
2841 else if ((*argv[i] == 'x' || *argv[i] == 'X'))
2843 dtype = -2; // DATA follows TEXT
2845 else if ((dtype = 0), getval(argv[i],&dval))
2847 printf("Error in data-segment address: %s is not 'r', 'x' or an address.", argv[i]);
2854 // BSS segment can be 'r', 'x' or a value
2855 if ((*argv[i] == 'r' || *argv[i] == 'R') && !argv[i][1])
2857 btype = -1; // BSS segment is relocatable
2859 else if ((*argv[i] == 'x' || *argv[i] == 'X'))
2861 btype = -3; // BSS follows DATA
2863 else if ((btype = 0), getval(argv[i],&bval))
2865 printf("Error in bss-segment address: %s is not 'r', 'x[td]', or an address.", argv[i]);
2872 case 'B': // Don't remove muliply defined locals
2879 case 'C': // Process a command file
2882 printf("Not enough arguments to -c\n");
2886 if (docmdfile(argv[i++]))
2893 case 'D': // Wait for "return" before exiting
2901 case 'E': // Output COFF (absolute only)
2905 case 'G': // Output source level debugging
2906 printf("\'g\' flag not currently implemented\n");
2909 if (gflag) warn('g', 1);
2914 case 'I': // Include binary file
2917 printf("Not enough arguments to -i\n");
2924 // handle -ii (No truncation)
2925 if ((argv[i-3][2] == 'i') || (argv[i-3][2] == 'I'))
2928 printf("warning: (-ii) COFF format output not specified\n");
2930 // handle -i (Truncation)
2933 if (strlen(isym) > 7)
2937 // Place include files in the DATA segment only
2938 if (DoFile(ifile, DSTSEG_D, isym))
2943 case 'L': // Add local symbols
2950 case 'M': // Produce load symbol map
2957 case 'N': // Output no header to .abs file
2964 case 'O': // Specify an output file
2972 printf("No output filename following -o switch\n");
2976 if (strlen(argv[i]) > FARGSIZE - 5)
2978 printf("Output file name too long (sorry!)\n");
2982 strcpy(ofile, argv[i++]);
2985 case 'R': // Section alignment size
2991 switch (argv[i-1][2])
2993 case 'w': case 'W': secalign = 1; break; // Word alignment
2994 case 'l': case 'L': secalign = 3; break; // Long alignment
2995 case 'p': case 'P': secalign = 7; break; // Phrase alignment
2996 case 'd': case 'D': secalign = 15; break; // Double phrase alignment
2997 case 'q': case 'Q': secalign = 31; break; // Quad phrase alignment
2998 default: secalign = 7; break; // Default phrase alignment
3003 case 'S': // Output only global symbols
3010 case 'V': // Verbose information
3011 if (!vflag && !versflag)
3019 case 'W': // Show warnings flag
3026 case 'Z': // Suppress banner flag
3033 printf("unknown option argument `%c'\n", c);
3039 // Not a switch, then process as a file
3040 if (DoFile(argv[i++], 0, NULL))
3045 if (!oflag && vflag)
3047 strcpy(ofile, "output");
3048 printf("Output file is %s[.ext]\n", ofile);
3052 printf("Output file is %s\n", ofile);
3057 // No problems encountered
3063 // Display Version Information
3065 void display_version(void)
3067 if (displaybanner)// && vflag)
3069 printf("\nReboot's Linker for Atari Jaguar\n");
3070 printf("Copyright (c) 199x Allan K. Pratt, 2014 Reboot\n");
3071 printf("V%i.%i.%i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
3077 // Display Command Line Help
3079 void display_help(void)
3082 printf(" %s [-options] file(s)\n", cmdlnexec);
3084 printf("Options:\n");
3085 printf(" -? or -h display usage information\n");
3086 printf(" -a <text> <data> <bss> output absolute file\n");
3087 printf(" hex value: segment address\n");
3088 printf(" r: relocatable segment\n");
3089 printf(" x: contiguous segment\n");
3090 printf(" -b don't remove multiply defined local labels\n");
3091 printf(" -c <fname> add contents of <fname> to command line\n");
3092 printf(" -d wait for key after link\n");
3093 printf(" -e output COF absolute file\n");
3094 printf(" -g output source-level debugging\n");
3095 printf(" -i <fname> <label> incbin <fname> and set <label>\n");
3096 printf(" -l add local symbols\n");
3097 printf(" -m produce load symbols map\n");
3098 printf(" -n output no file header to .abs file\n");
3099 printf(" -o <fname> set output name\n");
3100 printf(" -r<size> section alignment size\n");
3101 printf(" w: word (2 bytes)\n");
3102 printf(" l: long (4 bytes)\n");
3103 printf(" p: phrase (8 bytes, default alignment)\n");
3104 printf(" d: double phrase (16 bytes)\n");
3105 printf(" q: quad phrase (32 bytes)\n");
3106 printf(" -s output only global symbols\n");
3107 printf(" -v set verbose mode\n");
3108 printf(" -w show linker warnings\n");
3109 printf(" -z suppress banner\n");
3121 // Display link status if verbose mode
3123 printf("Link %s.\n", errflag ? "aborted" : "complete");
3125 // Wait for return key if requested
3128 printf("\nPress the [RETURN] key to continue. ");
3129 fgets(tempbuf, 128, stdin);
3137 // Determine Processor Endianess
3139 int get_endianess(void)
3142 char * p = (char *)&i;
3145 return ENDIANNESS_LITTLE;
3147 return ENDIANNESS_BIG;
3152 // Application Code Starts Here
3154 int main(int argc, char * argv[])
3156 char * s = NULL; // String pointer for "getenv"
3157 struct HREC * utemp; // Temporary hash record pointer
3158 struct OHEADER * header; // Pointer to output header structure
3160 endian = get_endianess(); // Get processor endianess
3161 cmdlnexec = argv[0]; // Obtain executable name
3162 s = getenv("RLNPATH");
3164 if (s) // Attempt to obtain env variable
3165 strcpy(libdir, s); // Store it if found
3167 // Parse the command line
3168 if (doargs(argc, argv))
3174 if (!zflag && !vflag)
3176 display_version(); // Display version information
3177 versflag = 1; // We've dumped the version banner
3180 // Load in specified files/objects and add to processing list
3187 // Work in items in processing list & deal with unresolved list
3194 // Check that there is something to link
3201 // Report unresolved externals
3202 if (unresolved != NULL)
3204 printf("UNRESOLVED SYMBOLS\n");
3206 // Don't list them if two -u's or more
3211 while (utemp != NULL)
3213 printf("\t%s (",utemp->h_sym);
3214 put_name(utemp->h_ofile);
3216 utemp = utemp->h_next;
3227 // Make one output file from input objs
3228 header = make_ofile();
3239 printf("TO DO:Partial linking\n");
3242 // Relocatable linking
3245 printf("TO DO:Relocatable linking\n");
3253 printf("Absolute linking ");
3262 printf("Header magic is 0x%04X\n", (unsigned int)header->magic);
3264 if (write_ofile(header))
3268 // Display the loaded symbols map
3270 if (write_map(header))
3273 // Display segment size summary
3277 printf("+---------+----------+----------+----------+\n");
3278 printf("| Segment | TEXT | DATA | BSS |\n");
3279 printf("| Sizes |----------+----------+----------|\n");
3280 printf("| (Hex) | %8X | %8X | %8X |\n", (unsigned int)header->tsize, (unsigned int)header->dsize, (unsigned int)header->bsize);
3281 printf("+---------+----------+----------+----------+\n\n");
3284 //printf("BIG_ENDIAN = %i, LITTLE_ENDIAN = %i\n", BIG_ENDIAN, LITTLE_ENDIAN);