]> git.sur5r.net Git - cc65/blobdiff - src/ld65/bin.c
Segment OVERLAY renamed to REPLACE. Bugfix for read-only segments. Formatting.
[cc65] / src / ld65 / bin.c
index 61ccb5aa254362713afe17c074fe6debc41f8158..c94ac9319a5ea6fdbd4b841afb1000a3829b7936 100644 (file)
@@ -1,15 +1,15 @@
 /*****************************************************************************/
 /*                                                                           */
-/*                                  bin.c                                   */
+/*                                   bin.c                                   */
 /*                                                                           */
-/*                 Module to handle the raw binary format                   */
+/*                  Module to handle the raw binary format                   */
 /*                                                                           */
 /*                                                                           */
 /*                                                                           */
-/* (C) 1999-2005 Ullrich von Bassewitz                                       */
-/*               Römerstrasse 52                                             */
-/*               D-70794 Filderstadt                                         */
-/* EMail:        uz@cc65.org                                                 */
+/* (C) 1999-2012, Ullrich von Bassewitz                                      */
+/*                Roemerstrasse 52                                           */
+/*                D-70794 Filderstadt                                        */
+/* EMail:         uz@cc65.org                                                */
 /*                                                                           */
 /*                                                                           */
 /* This software is provided 'as-is', without any expressed or implied       */
@@ -38,6 +38,7 @@
 #include <errno.h>
 
 /* common */
+#include "alignment.h"
 #include "print.h"
 #include "xmalloc.h"
 
 #include "global.h"
 #include "fileio.h"
 #include "lineinfo.h"
+#include "memarea.h"
 #include "segments.h"
 #include "spool.h"
 
 
 
 /*****************************************************************************/
-/*                                          Data                                    */
+/*                                   Data                                    */
 /*****************************************************************************/
 
 
 
 struct BinDesc {
-    unsigned   Undef;          /* Count of undefined externals */
-    FILE*      F;              /* Output file */
-    const char* Filename;      /* Name of output file */
+    unsigned    Undef;          /* Count of undefined externals */
+    FILE*       F;              /* Output file */
+    const char* Filename;       /* Name of output file */
 };
 
 
 
 /*****************************************************************************/
-/*                                          Code                                    */
+/*                                   Code                                    */
 /*****************************************************************************/
 
 
@@ -82,8 +84,8 @@ BinDesc* NewBinDesc (void)
     BinDesc* D = xmalloc (sizeof (BinDesc));
 
     /* Initialize the fields */
-    D->Undef   = 0;
-    D->F       = 0;
+    D->Undef    = 0;
+    D->F        = 0;
     D->Filename = 0;
 
     /* Return the created struct */
@@ -101,11 +103,11 @@ void FreeBinDesc (BinDesc* D)
 
 
 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
-                             unsigned long Offs attribute ((unused)),
-                             void* Data)
+                              unsigned long Offs attribute ((unused)),
+                              void* Data)
 /* Called from SegWrite for an expression. Evaluate the expression, check the
- * range and write the expression value to the file.
- */
+** range and write the expression value to the file.
+*/
 {
     /* There's a predefined function to handle constant expressions */
     return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
@@ -129,30 +131,37 @@ static void PrintNumVal (const char* Name, unsigned long V)
 
 
 
-static void BinWriteMem (BinDesc* D, Memory* M)
+static void BinWriteMem (BinDesc* D, MemoryArea* M)
 /* Write the segments of one memory area to a file */
 {
+    unsigned I;
+
     /* Get the start address of this memory area */
     unsigned long Addr = M->Start;
 
-    /* Get a pointer to the first segment node */
-    MemListNode* N = M->SegList;
-    while (N) {
+    /* Debugging: Check that the file offset is correct */
+    if (ftell (D->F) != (long)M->FileOffs) {
+        Internal ("Invalid file offset for memory area %s: %ld/%lu",
+                  GetString (M->Name), ftell (D->F), M->FileOffs);
+    }
+
+    /* Walk over all segments in this memory area */
+    for (I = 0; I < CollCount (&M->SegList); ++I) {
 
-       int DoWrite;
+        int DoWrite;
 
-       /* Get the segment from the list node */
-       SegDesc* S = N->Seg;
+        /* Get the segment */
+        SegDesc* S = CollAtUnchecked (&M->SegList, I);
 
-       /* Keep the user happy */
-               Print (stdout, 1, "    Writing `%s'\n", GetString (S->Name));
+        /* Keep the user happy */
+        Print (stdout, 1, "    Writing `%s'\n", GetString (S->Name));
 
-       /* Writes do only occur in the load area and not for BSS segments */
-               DoWrite = (S->Flags & SF_BSS) == 0      &&      /* No BSS segment */
-                  S->Load == M                 &&      /* LOAD segment */
-                  S->Seg->Dumped == 0;                 /* Not already written */
+        /* Writes do only occur in the load area and not for BSS segments */
+        DoWrite = (S->Flags & SF_BSS) == 0      &&      /* No BSS segment */
+                   S->Load == M                 &&      /* LOAD segment */
+                   S->Seg->Dumped == 0;                 /* Not already written */
 
-       /* Output debugging stuff */
+        /* Output debugging stuff */
         PrintBoolVal ("bss", S->Flags & SF_BSS);
         PrintBoolVal ("LoadArea", S->Load == M);
         PrintBoolVal ("Dumped", S->Seg->Dumped);
@@ -160,28 +169,18 @@ static void BinWriteMem (BinDesc* D, Memory* M)
         PrintNumVal  ("Address", Addr);
         PrintNumVal  ("FileOffs", (unsigned long) ftell (D->F));
 
-       /* Check if we would need an alignment */
-       if (S->Seg->Align > S->Align) {
-           /* Segment itself requires larger alignment than configured
-            * in the linker.
-            */
-           Warning ("Segment `%s' in module `%s' requires larger alignment",
-                    GetString (S->Name), GetObjFileName (S->Seg->AlignObj));
-       }
-
         /* If this is the run memory area, we must apply run alignment. If
-         * this is not the run memory area but the load memory area (which
-         * means that both are different), we must apply load alignment.
-         * Beware: DoWrite may be true even if this is the run memory area,
-         * because it may be also the load memory area.
-         */
+        ** this is not the run memory area but the load memory area (which
+        ** means that both are different), we must apply load alignment.
+        ** Beware: DoWrite may be true even if this is the run memory area,
+        ** because it may be also the load memory area.
+        */
         if (S->Run == M) {
 
             /* Handle ALIGN and OFFSET/START */
             if (S->Flags & SF_ALIGN) {
                 /* Align the address */
-                unsigned long Val = (0x01UL << S->Align) - 1;
-                unsigned long NewAddr = (Addr + Val) & ~Val;
+                unsigned long NewAddr = AlignAddr (Addr, S->RunAlignment);
                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
                     WriteMult (D->F, M->FillVal, NewAddr - Addr);
                     PrintNumVal ("SF_ALIGN", NewAddr - Addr);
@@ -194,8 +193,13 @@ static void BinWriteMem (BinDesc* D, Memory* M)
                     NewAddr += M->Start;
                 }
                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
-                    WriteMult (D->F, M->FillVal, NewAddr-Addr);
-                    PrintNumVal ("SF_OFFSET", NewAddr - Addr);
+                    /* Seek in "overlay" segments */
+                    if (S->Flags & SF_REPLACE) {
+                        fseek (D->F, NewAddr - M->Start, SEEK_SET);
+                    } else {
+                        WriteMult (D->F, M->FillVal, NewAddr-Addr);
+                        PrintNumVal ("SF_OFFSET", NewAddr - Addr);
+                    }
                 }
                 Addr = NewAddr;
             }
@@ -205,10 +209,9 @@ static void BinWriteMem (BinDesc* D, Memory* M)
             /* Handle ALIGN_LOAD */
             if (S->Flags & SF_ALIGN_LOAD) {
                 /* Align the address */
-                unsigned long Val = (0x01UL << S->AlignLoad) - 1;
-                unsigned long NewAddr = (Addr + Val) & ~Val;
+                unsigned long NewAddr = AlignAddr (Addr, S->LoadAlignment);
                 if (DoWrite || (M->Flags & MF_FILL) != 0) {
-                    WriteMult (D->F, M->FillVal, NewAddr-Addr);
+                    WriteMult (D->F, M->FillVal, NewAddr - Addr);
                     PrintNumVal ("SF_ALIGN_LOAD", NewAddr - Addr);
                 }
                 Addr = NewAddr;
@@ -216,36 +219,31 @@ static void BinWriteMem (BinDesc* D, Memory* M)
 
         }
 
-       /* Now write the segment to disk if it is not a BSS type segment and
-        * if the memory area is the load area.
-        */
-               if (DoWrite) {
+        /* Now write the segment to disk if it is not a BSS type segment and
+        ** if the memory area is the load area.
+        */
+        if (DoWrite) {
             unsigned long P = ftell (D->F);
-           RelocLineInfo (S->Seg);
-            S->Seg->FillVal = M->FillVal;
-           SegWrite (D->F, S->Seg, BinWriteExpr, D);
+            SegWrite (D->Filename, D->F, S->Seg, BinWriteExpr, D);
             PrintNumVal ("Wrote", (unsigned long) (ftell (D->F) - P));
-       } else if (M->Flags & MF_FILL) {
-           WriteMult (D->F, M->FillVal, S->Seg->Size);
+        } else if (M->Flags & MF_FILL) {
+            WriteMult (D->F, S->Seg->FillVal, S->Seg->Size);
             PrintNumVal ("Filled", (unsigned long) S->Seg->Size);
-       }
-
-       /* If this was the load memory area, mark the segment as dumped */
-       if (S->Load == M) {
-           S->Seg->Dumped = 1;
-               }
+        }
 
-       /* Calculate the new address */
-       Addr += S->Seg->Size;
+        /* If this was the load memory area, mark the segment as dumped */
+        if (S->Load == M) {
+            S->Seg->Dumped = 1;
+        }
 
-       /* Next segment node */
-       N = N->Next;
+        /* Calculate the new address */
+        Addr += S->Seg->Size;
     }
 
     /* If a fill was requested, fill the remaining space */
     if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size) {
         unsigned long ToFill = M->Size - M->FillLevel;
-               Print (stdout, 2, "    Filling 0x%lx bytes with 0x%02x\n",
+        Print (stdout, 2, "    Filling 0x%lx bytes with 0x%02x\n",
                ToFill, M->FillVal);
         WriteMult (D->F, M->FillVal, ToFill);
         M->FillLevel = M->Size;
@@ -258,9 +256,9 @@ static int BinUnresolved (unsigned Name attribute ((unused)), void* D)
 /* Called if an unresolved symbol is encountered */
 {
     /* Unresolved symbols are an error in binary format. Bump the counter
-     * and return zero telling the caller that the symbol is indeed
-     * unresolved.
-     */
+    ** and return zero telling the caller that the symbol is indeed
+    ** unresolved.
+    */
     ((BinDesc*) D)->Undef++;
     return 0;
 }
@@ -270,48 +268,44 @@ static int BinUnresolved (unsigned Name attribute ((unused)), void* D)
 void BinWriteTarget (BinDesc* D, struct File* F)
 /* Write a binary output file */
 {
-    Memory* M;
+    unsigned I;
 
     /* Place the filename in the control structure */
     D->Filename = GetString (F->Name);
 
     /* Check for unresolved symbols. The function BinUnresolved is called
-     * if we get an unresolved symbol.
-     */
-    D->Undef = 0;              /* Reset the counter */
+    ** if we get an unresolved symbol.
+    */
+    D->Undef = 0;               /* Reset the counter */
     CheckUnresolvedImports (BinUnresolved, D);
     if (D->Undef > 0) {
-       /* We had unresolved symbols, cannot create output file */
-               Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
+        /* We had unresolved symbols, cannot create output file */
+        Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
     }
 
     /* Open the file */
     D->F = fopen (D->Filename, "wb");
     if (D->F == 0) {
-       Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
+        Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
     }
 
     /* Keep the user happy */
     Print (stdout, 1, "Opened `%s'...\n", D->Filename);
 
     /* Dump all memory areas */
-    M = F->MemList;
-    while (M) {
-       Print (stdout, 1, "  Dumping `%s'\n", GetString (M->Name));
-       BinWriteMem (D, M);
-       M = M->FNext;
+    for (I = 0; I < CollCount (&F->MemoryAreas); ++I) {
+        /* Get this entry */
+        MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, I);
+        Print (stdout, 1, "  Dumping `%s'\n", GetString (M->Name));
+        BinWriteMem (D, M);
     }
 
     /* Close the file */
     if (fclose (D->F) != 0) {
-       Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
+        Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
     }
 
     /* Reset the file and filename */
     D->F        = 0;
     D->Filename = 0;
 }
-
-
-
-