]> git.sur5r.net Git - cc65/blobdiff - src/ar65/library.c
Did some renaming and cleanup: Renamed EXPR_SEGMENT to EXPR_SECTION, since
[cc65] / src / ar65 / library.c
index 6fd3e7d466cac912d2500193052d9c6a4ec47d6b..1925d6818907d5af1161c1d1485c2baeb13c6ad2 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1998     Ullrich von Bassewitz                                        */
-/*              Wacholderweg 14                                              */
-/*              D-70597 Stuttgart                                            */
-/* EMail:       uz@musoftware.de                                             */
+/* (C) 1998-2000 Ullrich von Bassewitz                                       */
+/*               Wacholderweg 14                                             */
+/*               D-70597 Stuttgart                                           */
+/* EMail:        uz@musoftware.de                                            */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
 #include <string.h>
 #include <errno.h>
 
-#include "../common/libdefs.h"
-#include "../common/symdefs.h"
-#include "../common/exprdefs.h"
-#include "../common/filepos.h"
-#include "../common/bitops.h"
+/* common */
+#include "bitops.h"
+#include "exprdefs.h"
+#include "filepos.h"
+#include "libdefs.h"
+#include "print.h"
+#include "symdefs.h"
+#include "xmalloc.h"
 
-#include "mem.h"
+/* ar65 */
 #include "error.h"
 #include "global.h"
 #include "fileio.h"
@@ -67,7 +70,8 @@ static const char*    LibName = 0;
 /* The library header */
 static LibHeader               Header = {
     LIB_MAGIC,
-    LIB_VERSION
+    LIB_VERSION,
+    0, 0
 };
 
 
@@ -114,12 +118,12 @@ static void ReadIndexEntry (void)
 
     /* Exports */
     O->ExportSize = Read16 (Lib);
-    O->Exports    = Xmalloc (O->ExportSize);
+    O->Exports    = xmalloc (O->ExportSize);
     ReadData (Lib, O->Exports, O->ExportSize);
 
     /* Imports */
     O->ImportSize = Read16 (Lib);
-    O->Imports    = Xmalloc (O->ImportSize);
+    O->Imports    = xmalloc (O->ImportSize);
     ReadData (Lib, O->Imports, O->ImportSize);
 }
 
@@ -223,7 +227,7 @@ void LibOpen (const char* Name, int MustExist, int NeedTemp)
  */
 {
     /* Remember the name */
-    LibName = StrDup (Name);
+    LibName = xstrdup (Name);
 
     /* Open the existing library for reading */
     Lib = fopen (Name, "rb");
@@ -303,6 +307,28 @@ void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
 
 
 
+static unsigned long GetVar (unsigned char** Buf)
+/* Get a variable sized value from Buf */
+{
+    unsigned char C;
+    unsigned long V = 0;
+    unsigned Shift = 0;
+    do {
+       /* Read one byte */
+               C = **Buf;
+       ++(*Buf);
+       /* Add this char to the value */
+       V |= ((unsigned long)(C & 0x7F)) << Shift;
+       /* Next value */
+       Shift += 7;
+    } while (C & 0x80);
+
+    /* Return the result */
+    return V;
+}
+
+
+
 static void SkipExpr (unsigned char** Buf)
 /* Skip an expression in Buf */
 {
@@ -326,7 +352,7 @@ static void SkipExpr (unsigned char** Buf)
            *Buf += 2;
            return;
 
-        case EXPR_SEGMENT:
+        case EXPR_SECTION:
            /* 8 bit segment number */
            *Buf += 1;
            return;
@@ -339,34 +365,44 @@ static void SkipExpr (unsigned char** Buf)
 
 
 
+static void SkipFilePos (unsigned char** Buf)
+/* Skip a file position in Buf */
+{
+    (void) GetVar (Buf);       /* Line */
+    (void) GetVar (Buf);       /* Col */
+    (void) GetVar (Buf);       /* Name */
+}
+
+
+
 static void LibCheckExports (ObjData* O)
 /* Insert all exports from the given object file into the global list
  * checking for duplicates.
  */
 {
-    char Name [256];
-
     /* Get a pointer to the buffer */
     unsigned char* Exports = O->Exports;
 
-    /* First two bytes are export count */
-    unsigned Lo = *Exports++;
-    unsigned Hi = *Exports++;
-    unsigned Count = (Hi << 8) + Lo;
+    /* Get the export count */
+    unsigned Count = GetVar (&Exports);
 
     /* Read the exports */
-    if (Verbose > 1) {
-        printf ("Module `%s' (%u exports):\n", O->Name, Count);
-    }
+    Print (stdout, 1, "Module `%s' (%u exports):\n", O->Name, Count);
     while (Count--) {
 
-       unsigned char Len;
+       unsigned char   Tag;
+       unsigned        Len;
+       char*           Name;
 
        /* Get the export tag */
-       unsigned char Tag = *Exports++;
+       Tag = *Exports++;
+
+       /* condes decls may follow */
+       Exports += GET_EXP_CONDES_COUNT (Tag);
 
                /* Next thing is name of symbol */
-       Len = *Exports++;
+       Len = GetVar (&Exports);
+       Name = xmalloc (Len + 1);
        memcpy (Name, Exports, Len);
        Name [Len] = '\0';
        Exports += Len;
@@ -381,13 +417,14 @@ static void LibCheckExports (ObjData* O)
        }
 
        /* Skip the position */
-       Exports += POS_SIZE;
+               SkipFilePos (&Exports);
 
        /* Insert the name into the hash table */
-       if (Verbose > 1) {
-               printf ("  %s\n", Name);
-       }
+       Print (stdout, 1, "  %s\n", Name);
        ExpInsert (Name, O->Index);
+
+       /* Free the name */
+       xfree (Name);
     }
 }