]> Shamusworld >> Repos - rln/blobdiff - rln.c
Handle absolute BSS segment location of 'xt'
[rln] / rln.c
diff --git a/rln.c b/rln.c
index 1cbf9545efc88fbb39ad2d7f2fe6041fea454f59..78ed41053bbc2a6f8b43d5a68c50f186987f2818 100644 (file)
--- a/rln.c
+++ b/rln.c
@@ -1,6 +1,6 @@
 //
-// RLN - Reboot's Linker for the Atari Jaguar console system
-// Copyright (C) 199x, Allan K. Pratt, 2014-2017 Reboot & Friends
+// RLN - Renamed Linker for the Atari Jaguar console system
+// Copyright (C) 199x, Allan K. Pratt, 2014-2021 Reboot & Friends
 //
 
 #include "rln.h"
@@ -71,6 +71,10 @@ struct HREC * LookupHREC(char *);
 char * PathTail(char *);
 void ShowHelp(void);
 void ShowVersion(void);
+int OSTLookup(char * sym);
+int OSTAdd(char * name, int type, long value);
+int ProcessFiles(void);
+int doargs(int argc, char * argv[]);
 
 
 //
@@ -586,8 +590,11 @@ int RelocateSegment(struct OFILE * ofile, int flag)
        unsigned absreloc;              // Absolute relocation flag
        unsigned relreloc;              // Relative relocation flag
        unsigned wordreloc;             // Relocate word only flag
+       unsigned opreloc;               // Relocate OP data address only flag
        unsigned swcond;                // Switch statement condition
        unsigned relocsize;             // Relocation record size
+       unsigned saveBits;              // OP data leftover bits save
+       unsigned saveBits2;
 
        // 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
@@ -668,6 +675,7 @@ int RelocateSegment(struct OFILE * ofile, int flag)
                absreloc  = (rflg & 0x00000040 ? 1 : 0); // Set absolute relocation flag
                relreloc  = (rflg & 0x000000A0 ? 1 : 0); // Set relative relocation flag
                wordreloc = (rflg & 0x00000002 ? 1 : 0); // Set word relocation flag
+               opreloc   = (rflg & 0x00000004 ? 1 : 0); // Set OP relocation flag
 
                // Additional processing required for global relocations
                if (glblreloc)
@@ -689,6 +697,18 @@ int RelocateSegment(struct OFILE * ofile, int flag)
                // instruction
                olddata = (wordreloc ? GetWord(sptr + addr) : GetLong(sptr + addr));
 
+               // If it's a OP QUAD relocation, get the data out of the DATA bits.
+               // Note that because we can't afford to lose the bottom 3 bits of the
+               // relocation record, we lose 3 off the top--which means the maximum
+               // this can store is $1FFFF8 (vs. $FFFFF8).
+               if (opreloc)
+               {
+                       saveBits2 = (GetLong(sptr + addr + 8) & 0xE0000000) >> 8; // Upper 3 of data addr
+                       saveBits = olddata & 0x7FF;
+                       olddata = (olddata & 0xFFFFF800) >> 11;
+                       olddata |= saveBits2; // Restore upper 3 bits of data addr
+               }
+
                if (rflg & 0x01)
                        olddata = _SWAPWORD(olddata);
 
@@ -706,7 +726,7 @@ int RelocateSegment(struct OFILE * ofile, int flag)
                                break;
                        case 0x00000400:          // TEXT segment relocation record
                                // SCPCD : the symbol point to a text segment, we should use the textoffset
-                                       newdata = tbase + textoffset + olddata;
+                               newdata = tbase + textoffset + olddata;
 
                                break;
                        case 0x00000600:          // DATA segment relocation record
@@ -739,6 +759,17 @@ int RelocateSegment(struct OFILE * ofile, int flag)
 
                        if (wordreloc)
                                PutWord(sptr + addr, newdata);
+                       else if (opreloc)
+                       {
+                               if (vflag > 1)
+                                       printf("OP reloc: oldata=$%X, newdata=$%X\n", olddata, newdata);
+
+                               newdata = ((newdata & 0x00FFFFF8) << 8) | saveBits;
+                               PutLong(sptr + addr, newdata);
+                               // Strip out extraneous data hitchhikers from 2nd phrase...
+                               newdata = GetLong(sptr + addr + 8) & 0x007FFFFF;
+                               PutLong(sptr + addr + 8, newdata);
+                       }
                        else
                                PutLong(sptr + addr, newdata);
                }
@@ -1541,11 +1572,19 @@ struct OHEADER * MakeOutputObject()
                dbase = dval;
 
                if (!bval)
-                       // BSS follows DATA
-                       bbase = dval + datasize;
+               {
+                       if (btype == -2)
+                               // BSS follows TEXT
+                               bbase = tval +  textsize;
+                       else
+                               // BSS follows DATA
+                               bbase = dval + datasize;
+               }
                else
+               {
                        // BSS is independent of DATA
                        bbase = bval;
+               }
        }
 
        // Inject segment end labels, for C compilers that expect this shite
@@ -2417,7 +2456,8 @@ int LoadArchive(char * fname, int fd)
                }
 
                // Check to see if a long filename was requested
-               if (objName[0] == 0x20)
+               // N.B.: " " is for GNU archives, and "/" is for BSD archives
+               if ((objName[0] == 0x20) || (objName[0] == '/'))
                {
                        uint32_t fnSize = atoi(objName + 1);
 
@@ -2777,9 +2817,33 @@ int doargs(int argc, char * argv[])
                                }
                                else if ((*argv[i] == 'x' || *argv[i] == 'X'))
                                {
-                                       btype = -3;                     // BSS follows DATA
+                                       switch (argv[i][1])
+                                       {
+                                               case 'd': case 'D': case '\0':
+                                                       btype = -3;     // BSS follows DATA
+                                                       break;
+
+                                               case 't': case 'T':
+                                                       btype = -2;     // BSS follows TEXT
+                                                       if (btype == dtype)
+                                                       {
+                                                               printf("Error in bss-segment address: data-segment also follows text\n");
+                                                               return 1;
+                                                       }
+                                                       break;
+
+                                               default:
+                                                       btype = -4;     // Error
+                                                       break;
+                                       }
                                }
                                else if (GetHexValue(argv[i], &bval))
+                               {
+                                       btype = -4;
+                                       return 1;
+                               }
+
+                               if (btype == -4)
                                {
                                        printf("Error in bss-segment address: %s is not 'r', 'x[td]', or an address.", argv[i]);
                                        return 1;
@@ -2819,6 +2883,10 @@ int doargs(int argc, char * argv[])
                        case 'e':
                        case 'E':                                       // Output COFF (absolute only)
                                cflag = 1;
+
+                               if (noheaderflag)
+                                       printf("Warning: -e overridden by -n, output will be headerless\n");
+
                                break;
                        case 'g':
                        case 'G':                                       // Output source level debugging
@@ -2877,6 +2945,9 @@ int doargs(int argc, char * argv[])
                                if (noheaderflag)
                                        warn('n', 1);
 
+                               if (cflag)
+                                       printf("Warning: -e overridden by -n, output will be headerless\n");
+
                                noheaderflag = 1;
                                break;
                        case 'o':
@@ -2945,6 +3016,22 @@ int doargs(int argc, char * argv[])
 
                                wflag = 1;
                                break;
+                       case 'y':
+                       case 'Y':
+                               if (i >= argc)
+                               {
+                                       printf("No directory filename following -y switch\n");
+                                       return 1;
+                               }
+
+                               if (strlen(argv[i]) > FARGSIZE * 3)
+                               {
+                                       printf("Directory file name too long (sorry!)\n");
+                                       return 1;
+                               }
+
+                               strcpy(libdir, argv[i++]);
+                               break;
                        case 'z':
                        case 'Z':                                       // Suppress banner flag
                                if (zflag)
@@ -2995,8 +3082,8 @@ void ShowVersion(void)
                "| '__| | '_  \\\n"
                "| |  | | | | |\n"
                "|_|  |_|_| |_|\n"
-               "\nReboot's Linker for Atari Jaguar\n"
-               "Copyright (c) 199x Allan K. Pratt, 2014-2017 Reboot\n"
+               "\nRenamed Linker for Atari Jaguar\n"
+               "Copyright (c) 199x Allan K. Pratt, 2014-2021 Reboot & Friends\n"
                "V%i.%i.%i %s (%s)\n\n", MAJOR, MINOR, PATCH, __DATE__, PLATFORM);
        }
 }
@@ -3012,10 +3099,13 @@ void ShowHelp(void)
        printf("\n");
        printf("Options:\n");
        printf("   -? or -h                display usage information\n");
-       printf("   -a <text> <data> <bss>  output absolute file\n");
+       printf("   -a <text> <data> <bss>  output absolute file (default: ABS)\n");
        printf("                           hex value: segment address\n");
        printf("                           r: relocatable segment\n");
-       printf("                           x: contiguous segment\n");
+       printf("                           x[dt]: contiguous segment\n");
+       printf("                           for contiguous bss:\n");
+       printf("                             d(default): follows data segment\n");
+       printf("                             t:          follows text segment\n");
        printf("   -b                      don't remove multiply defined local labels\n");
        printf("   -c <fname>              add contents of <fname> to command line\n");
        printf("   -d                      wait for key after link\n");
@@ -3025,7 +3115,7 @@ void ShowHelp(void)
        printf("   -ii <fname> <label>     incbin <fname> and set <label> (no truncation)\n");
        printf("   -l                      add local symbols\n");
        printf("   -m                      produce load symbols map\n");
-       printf("   -n                      output no file header to absolute file\n");
+       printf("   -n                      output no file header to absolute file (overrides -e)\n");
        printf("   -o <fname>              set output name\n");
        printf("   -r<size>                section alignment size\n");
        printf("                           w: word (2 bytes)\n");
@@ -3037,6 +3127,7 @@ void ShowHelp(void)
        printf("   -u                      allow unresolved symbols (experimental)\n");
        printf("   -v                      set verbose mode\n");
        printf("   -w                      show linker warnings\n");
+       printf("   -y <fname>              set include path (also set by RLNPATH)\n");
        printf("   -z                      suppress banner\n");
        printf("\n");
 }
@@ -3083,6 +3174,21 @@ int main(int argc, char * argv[])
                ExitLinker();
        }
 
+       // Check to see if include paths actually exist
+       if (strlen(libdir) > 0)
+       {
+               DIR * test = opendir(libdir);
+
+               if (test == NULL)
+               {
+                       printf("Invalid include path: %s\n", libdir);
+                       errflag = 1;
+                       ExitLinker();
+               }
+
+               closedir(test);
+       }
+
        if (!zflag && !vflag)
        {
                ShowVersion();                          // Display version information