]> git.sur5r.net Git - cc65/blobdiff - src/ld65/objfile.c
Removed unneeded include files.
[cc65] / src / ld65 / objfile.c
index af7f18bfffe7441ec2a0950c25e89950e0a6c6e3..24a9afda81bb3f64c67623acacb23f9493b49f19 100644 (file)
@@ -6,10 +6,10 @@
 /*                                                                          */
 /*                                                                          */
 /*                                                                          */
-/* (C) 1998    Ullrich von Bassewitz                                        */
-/*             Wacholderweg 14                                              */
-/*             D-70597 Stuttgart                                            */
-/* EMail:      uz@musoftware.de                                             */
+/* (C) 1998-2012, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                          */
 /*                                                                          */
 /* This software is provided 'as-is', without any expressed or implied      */
 
 
 #include <string.h>
-#include <errno.h>
-#include <time.h>
-#include <sys/stat.h>
 
-#include "../common/xmalloc.h"
+/* common */
+#include "fname.h"
+#include "xmalloc.h"
 
+/* ld65 */
+#include "asserts.h"
 #include "dbgsyms.h"
 #include "error.h"
 #include "exports.h"
+#include "fileinfo.h"
 #include "fileio.h"
+#include "lineinfo.h"
 #include "objdata.h"
-#include "segments.h"
 #include "objfile.h"
+#include "scopes.h"
+#include "segments.h"
+#include "spool.h"
 
 
 
 
 
 
-static const char* GetModule (const char* Name)
-/* Get a module name from the file name */
+static unsigned GetModule (const char* Name)
+/* Get a module name index from the file name */
 {
     /* Make a module name from the file name */
-    const char* Module = Name + strlen (Name);
-    while (Module > Name) {
-       --Module;
-       if (*Module == '/' || *Module == '\\') {
-           ++Module;
-           break;
-       }
-    }
+    const char* Module = FindName (Name);
     if (*Module == 0) {
        Error ("Cannot make module name from `%s'", Name);
     }
-    return Module;
+    return GetStringId (Module);
 }
 
 
@@ -81,96 +79,227 @@ static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
 {
     H->Version   = Read16 (Obj);
     if (H->Version != OBJ_VERSION) {
-               Error ("Object file `%s' has wrong version", Name);
+               Error ("Object file `%s' has wrong version, expected %08X, got %08X",
+               Name, OBJ_VERSION, H->Version);
+    }
+    H->Flags       = Read16 (Obj);
+    H->OptionOffs   = Read32 (Obj);
+    H->OptionSize   = Read32 (Obj);
+    H->FileOffs     = Read32 (Obj);
+    H->FileSize     = Read32 (Obj);
+    H->SegOffs     = Read32 (Obj);
+    H->SegSize     = Read32 (Obj);
+    H->ImportOffs   = Read32 (Obj);
+    H->ImportSize   = Read32 (Obj);
+    H->ExportOffs   = Read32 (Obj);
+    H->ExportSize   = Read32 (Obj);
+    H->DbgSymOffs   = Read32 (Obj);
+    H->DbgSymSize   = Read32 (Obj);
+    H->LineInfoOffs = Read32 (Obj);
+    H->LineInfoSize = Read32 (Obj);
+    H->StrPoolOffs  = Read32 (Obj);
+    H->StrPoolSize  = Read32 (Obj);
+    H->AssertOffs   = Read32 (Obj);
+    H->AssertSize   = Read32 (Obj);
+    H->ScopeOffs    = Read32 (Obj);
+    H->ScopeSize    = Read32 (Obj);
+    H->SpanOffs     = Read32 (Obj);
+    H->SpanSize     = Read32 (Obj);
+}
+
+
+
+void ObjReadFiles (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the files list from a file at the given position */
+{
+    unsigned I;
+    unsigned FileCount;
+
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the data */
+    FileCount  = ReadVar (F);
+    CollGrow (&O->Files, FileCount);
+    for (I = 0; I < FileCount; ++I) {
+               CollAppend (&O->Files, ReadFileInfo (F, O));
+    }
+}
+
+
+
+void ObjReadSections (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the section data from a file at the given position */
+{
+    unsigned I;
+    unsigned SectionCount;
+
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the data */
+    SectionCount = ReadVar (F);
+    CollGrow (&O->Sections, SectionCount);
+    for (I = 0; I < SectionCount; ++I) {
+               CollAppend (&O->Sections, ReadSection (F, O));
+    }
+}
+
+
+
+void ObjReadImports (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the imports from a file at the given position */
+{
+    unsigned I;
+    unsigned ImportCount;
+
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the data */
+    ImportCount = ReadVar (F);
+    CollGrow (&O->Imports, ImportCount);
+    for (I = 0; I < ImportCount; ++I) {
+               CollAppend (&O->Imports, ReadImport (F, O));
+    }
+}
+
+
+
+void ObjReadExports (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the exports from a file at the given position */
+{
+    unsigned I;
+    unsigned ExportCount;
+
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the data */
+    ExportCount = ReadVar (F);
+    CollGrow (&O->Exports, ExportCount);
+    for (I = 0; I < ExportCount; ++I) {
+               CollAppend (&O->Exports, ReadExport (F, O));
     }
-    H->Flags     = Read16 (Obj);
-    H->OptionOffs = Read32 (Obj);
-    H->OptionSize = Read32 (Obj);
-    H->FileOffs   = Read32 (Obj);
-    H->FileSize   = Read32 (Obj);
-    H->SegOffs   = Read32 (Obj);
-    H->SegSize   = Read32 (Obj);
-    H->ImportOffs = Read32 (Obj);
-    H->ImportSize = Read32 (Obj);
-    H->ExportOffs = Read32 (Obj);
-    H->ExportSize = Read32 (Obj);
-    H->DbgSymOffs = Read32 (Obj);
-    H->DbgSymSize = Read32 (Obj);
 }
 
 
 
-void ObjReadFiles (FILE* F, ObjData* O)
-/* Read the files list from a file at the current position */
+void ObjReadDbgSyms (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the debug symbols from a file at the given position */
 {
     unsigned I;
+    unsigned DbgSymCount;
+
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the asm debug symbols */
+    DbgSymCount = ReadVar (F);
+    CollGrow (&O->DbgSyms, DbgSymCount);
+    for (I = 0; I < DbgSymCount; ++I) {
+       CollAppend (&O->DbgSyms, ReadDbgSym (F, O, I));
+    }
 
-    O->FileCount  = Read8 (F);
-    O->Files      = xmalloc (O->FileCount * sizeof (char*));
-    for (I = 0; I < O->FileCount; ++I) {
-               /* Skip MTime and size */
-               Read32 (F);
-               Read32 (F);
-               /* Read the filename */
-               O->Files [I] = ReadMallocedStr (F);
+    /* Read the hll debug symbols */
+    DbgSymCount = ReadVar (F);
+    CollGrow (&O->HLLDbgSyms, DbgSymCount);
+    for (I = 0; I < DbgSymCount; ++I) {
+               CollAppend (&O->HLLDbgSyms, ReadHLLDbgSym (F, O, I));
     }
 }
 
 
 
-void ObjReadImports (FILE* F, ObjData* O)
-/* Read the imports from a file at the current position */
+void ObjReadLineInfos (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the line infos from a file at the given position */
 {
     unsigned I;
+    unsigned LineInfoCount;
 
-    O->ImportCount = Read16 (F);
-    O->Imports     = xmalloc (O->ImportCount * sizeof (Import*));
-    for (I = 0; I < O->ImportCount; ++I) {
-       O->Imports [I] = ReadImport (F, O);
-       InsertImport (O->Imports [I]);
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the data */
+    LineInfoCount = ReadVar (F);
+    CollGrow (&O->LineInfos, LineInfoCount);
+    for (I = 0; I < LineInfoCount; ++I) {
+               CollAppend (&O->LineInfos, ReadLineInfo (F, O));
     }
 }
 
 
 
-void ObjReadExports (FILE* F, ObjData* O)
-/* Read the exports from a file at the current position */
+void ObjReadStrPool (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the string pool from a file at the given position */
 {
     unsigned I;
 
-    O->ExportCount = Read16 (F);
-    O->Exports     = xmalloc (O->ExportCount * sizeof (Export*));
-    for (I = 0; I < O->ExportCount; ++I) {
-       O->Exports [I] = ReadExport (F, O);
-       InsertExport (O->Exports [I]);
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the data */
+    O->StringCount = ReadVar (F);
+    O->Strings     = xmalloc (O->StringCount * sizeof (O->Strings[0]));
+    for (I = 0; I < O->StringCount; ++I) {
+        O->Strings[I] = ReadStr (F);
     }
 }
 
 
 
-void ObjReadDbgSyms (FILE* F, ObjData* O)
-/* Read the debug symbols from a file at the current position */
+void ObjReadAssertions (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the assertions from a file at the given offset */
 {
     unsigned I;
+    unsigned AssertionCount;
 
-    O->DbgSymCount = Read16 (F);
-    O->DbgSyms    = xmalloc (O->DbgSymCount * sizeof (DbgSym*));
-    for (I = 0; I < O->DbgSymCount; ++I) {
-       O->DbgSyms [I] = ReadDbgSym (F, O);
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the data */
+    AssertionCount = ReadVar (F);
+    CollGrow (&O->Assertions, AssertionCount);
+    for (I = 0; I < AssertionCount; ++I) {
+        CollAppend (&O->Assertions, ReadAssertion (F, O));
     }
 }
 
 
 
-void ObjReadSections (FILE* F, ObjData* O)
-/* Read the section data from a file at the current position */
+void ObjReadScopes (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the scope table from a file at the given offset */
 {
     unsigned I;
+    unsigned ScopeCount;
+
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
 
-    O->SectionCount = Read8 (F);
-    O->Sections     = xmalloc (O->SectionCount * sizeof (Section*));
-    for (I = 0; I < O->SectionCount; ++I) {
-       O->Sections [I] = ReadSection (F, O);
+    /* Read the data */
+    ScopeCount = ReadVar (F);
+    CollGrow (&O->Scopes, ScopeCount);
+    for (I = 0; I < ScopeCount; ++I) {
+        CollAppend (&O->Scopes,  ReadScope (F, O, I));
+    }
+}
+
+
+
+void ObjReadSpans (FILE* F, unsigned long Pos, ObjData* O)
+/* Read the span table from a file at the given offset */
+{
+    unsigned I;
+    unsigned SpanCount;
+
+    /* Seek to the correct position */
+    FileSetPos (F, Pos);
+
+    /* Read the data */
+    SpanCount = ReadVar (F);
+    CollGrow (&O->Spans, SpanCount);
+    for (I = 0; I < SpanCount; ++I) {
+        CollAppend (&O->Spans,  ReadSpan (F, O, I));
     }
 }
 
@@ -189,38 +318,61 @@ void ObjAdd (FILE* Obj, const char* Name)
     ObjReadHeader (Obj, &O->Header, Name);
 
     /* Initialize the object module data structure */
-    O->Name              = xstrdup (GetModule (Name));
-    O->Flags             = OBJ_HAVEDATA;
+    O->Name  = GetModule (Name);
+
+    /* Read the string pool from the object file */
+    ObjReadStrPool (Obj, O->Header.StrPoolOffs, O);
 
     /* Read the files list from the object file */
-    fseek (Obj, O->Header.FileOffs, SEEK_SET);
-    ObjReadFiles (Obj, O);
+    ObjReadFiles (Obj, O->Header.FileOffs, O);
+
+    /* Read the line infos from the object file */
+    ObjReadLineInfos (Obj, O->Header.LineInfoOffs, O);
 
     /* Read the imports list from the object file */
-    fseek (Obj, O->Header.ImportOffs, SEEK_SET);
-    ObjReadImports (Obj, O);
+    ObjReadImports (Obj, O->Header.ImportOffs, O);
 
     /* Read the object file exports and insert them into the exports list */
-    fseek (Obj, O->Header.ExportOffs, SEEK_SET);
-    ObjReadExports (Obj, O);
+    ObjReadExports (Obj, O->Header.ExportOffs, O);
 
     /* Read the object debug symbols from the object file */
-    fseek (Obj, O->Header.DbgSymOffs, SEEK_SET);
-    ObjReadDbgSyms (Obj, O);
+    ObjReadDbgSyms (Obj, O->Header.DbgSymOffs, O);
+
+    /* Read the assertions from the object file */
+    ObjReadAssertions (Obj, O->Header.AssertOffs, O);
 
-    /* Read the segment list from the object file. This must be last, since
+    /* Read the segment list from the object file. This must be late, since
      * the expressions stored in the code may reference segments or imported
      * symbols.
      */
-    fseek (Obj, O->Header.SegOffs, SEEK_SET);
-    ObjReadSections (Obj, O);
+    ObjReadSections (Obj, O->Header.SegOffs, O);
+
+    /* Read the scope table from the object file. Scopes reference segments, so
+     * we must read them after the sections.
+     */
+    ObjReadScopes (Obj, O->Header.ScopeOffs, O);
+
+    /* Read the spans from the object file */
+    ObjReadSpans (Obj, O->Header.SpanOffs, O);
 
     /* Mark this object file as needed */
-    O->Flags |= OBJ_REF | OBJ_HAVEDATA;
+    O->Flags |= OBJ_REF;
 
     /* Done, close the file (we read it only, so no error check) */
     fclose (Obj);
+
+    /* Insert the imports and exports to the global lists */
+    InsertObjGlobals (O);
+
+    /* Insert the object into the list of all used object files */
+    InsertObjData (O);
+
+    /* All references to strings are now resolved, so we can delete the module
+     * string pool.
+     */
+    FreeObjStrings (O);
 }
 
 
 
+