]> git.sur5r.net Git - cc65/blob - src/ld65/bin.c
New module strstack
[cc65] / src / ld65 / bin.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   bin.c                                   */
4 /*                                                                           */
5 /*                  Module to handle the raw binary format                   */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1999-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39
40 /* common */
41 #include "print.h"
42 #include "xmalloc.h"
43
44 /* ld65 */
45 #include "bin.h"
46 #include "config.h"
47 #include "exports.h"
48 #include "expr.h"
49 #include "error.h"
50 #include "global.h"
51 #include "fileio.h"
52 #include "lineinfo.h"
53 #include "segments.h"
54 #include "spool.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 struct BinDesc {
65     unsigned    Undef;          /* Count of undefined externals */
66     FILE*       F;              /* Output file */
67     const char* Filename;       /* Name of output file */
68 };
69
70
71
72 /*****************************************************************************/
73 /*                                   Code                                    */
74 /*****************************************************************************/
75
76
77
78 BinDesc* NewBinDesc (void)
79 /* Create a new binary format descriptor */
80 {
81     /* Allocate memory for a new BinDesc struct */
82     BinDesc* D = xmalloc (sizeof (BinDesc));
83
84     /* Initialize the fields */
85     D->Undef    = 0;
86     D->F        = 0;
87     D->Filename = 0;
88
89     /* Return the created struct */
90     return D;
91 }
92
93
94
95 void FreeBinDesc (BinDesc* D)
96 /* Free a binary format descriptor */
97 {
98     xfree (D);
99 }
100
101
102
103 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
104                               unsigned long Offs attribute ((unused)),
105                               void* Data)
106 /* Called from SegWrite for an expression. Evaluate the expression, check the
107  * range and write the expression value to the file.
108  */
109 {
110     /* There's a predefined function to handle constant expressions */
111     return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
112 }
113
114
115
116 static void PrintBoolVal (const char* Name, int B)
117 /* Print a boolean value for debugging */
118 {
119     printf ("      %s = %s\n", Name, B? "true" : "false");
120 }
121
122
123
124 static void BinWriteMem (BinDesc* D, Memory* M)
125 /* Write the segments of one memory area to a file */
126 {
127     /* Get the start address of this memory area */
128     unsigned long Addr = M->Start;
129
130     /* Get a pointer to the first segment node */
131     MemListNode* N = M->SegList;
132     while (N) {
133
134         int DoWrite;
135
136         /* Get the segment from the list node */
137         SegDesc* S = N->Seg;
138
139         /* Keep the user happy */
140         Print (stdout, 1, "    Writing `%s'\n", GetString (S->Name));
141
142         /* Writes do only occur in the load area and not for BSS segments */
143         DoWrite = (S->Flags & SF_BSS) == 0      &&      /* No BSS segment */
144                    S->Load == M                 &&      /* LOAD segment */
145                    S->Seg->Dumped == 0;                 /* Not already written */
146
147         /* Output the DoWrite flag for debugging */
148         if (Verbosity > 1) {
149             PrintBoolVal ("bss", S->Flags & SF_BSS);
150             PrintBoolVal ("LoadArea", S->Load == M);
151             PrintBoolVal ("Dumped", S->Seg->Dumped);
152             PrintBoolVal ("DoWrite", DoWrite);
153         }
154
155         /* Check if we would need an alignment */
156         if (S->Seg->Align > S->Align) {
157             /* Segment itself requires larger alignment than configured
158              * in the linker.
159              */
160             Warning ("Segment `%s' in module `%s' requires larger alignment",
161                      GetString (S->Name), GetObjFileName (S->Seg->AlignObj));
162         }
163
164         /* Handle ALIGN and OFFSET/START */
165         if (S->Flags & SF_ALIGN) {
166             /* Align the address */
167             unsigned long Val, NewAddr;
168             Val = (0x01UL << S->Align) - 1;
169             NewAddr = (Addr + Val) & ~Val;
170             if (DoWrite) {
171                 WriteMult (D->F, M->FillVal, NewAddr-Addr);
172             }
173             Addr = NewAddr;
174             /* Remember the fill value for the segment */
175             S->Seg->FillVal = M->FillVal;
176         } else if (S->Flags & (SF_OFFSET | SF_START)) {
177             unsigned long NewAddr = S->Addr;
178             if (S->Flags & SF_OFFSET) {
179                 /* It's an offset, not a fixed address, make an address */
180                 NewAddr += M->Start;
181             }
182             if (DoWrite) {
183                 WriteMult (D->F, M->FillVal, NewAddr-Addr);
184             }
185             Addr = NewAddr;
186         }
187
188         /* Now write the segment to disk if it is not a BSS type segment and
189          * if the memory area is the load area.
190          */
191         if (DoWrite) {
192             RelocLineInfo (S->Seg);
193             SegWrite (D->F, S->Seg, BinWriteExpr, D);
194         } else if (M->Flags & MF_FILL) {
195             WriteMult (D->F, M->FillVal, S->Seg->Size);
196         }
197
198         /* If this was the load memory area, mark the segment as dumped */
199         if (S->Load == M) {
200             S->Seg->Dumped = 1;
201         }
202
203         /* Calculate the new address */
204         Addr += S->Seg->Size;
205
206         /* Next segment node */
207         N = N->Next;
208     }
209
210     /* If a fill was requested, fill the remaining space */
211     if (M->Flags & MF_FILL) {
212         while (M->FillLevel < M->Size) {
213             Write8 (D->F, M->FillVal);
214             ++M->FillLevel;
215         }
216     }
217 }
218
219
220
221 static int BinUnresolved (unsigned Name attribute ((unused)), void* D)
222 /* Called if an unresolved symbol is encountered */
223 {
224     /* Unresolved symbols are an error in binary format. Bump the counter
225      * and return zero telling the caller that the symbol is indeed
226      * unresolved.
227      */
228     ((BinDesc*) D)->Undef++;
229     return 0;
230 }
231
232                                    
233
234 void BinWriteTarget (BinDesc* D, struct File* F)
235 /* Write a binary output file */
236 {
237     Memory* M;
238
239     /* Place the filename in the control structure */
240     D->Filename = GetString (F->Name);
241
242     /* Check for unresolved symbols. The function BinUnresolved is called
243      * if we get an unresolved symbol.
244      */
245     D->Undef = 0;               /* Reset the counter */
246     CheckExports (BinUnresolved, D);
247     if (D->Undef > 0) {
248         /* We had unresolved symbols, cannot create output file */
249         Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
250     }
251
252     /* Open the file */
253     D->F = fopen (D->Filename, "wb");
254     if (D->F == 0) {
255         Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
256     }
257
258     /* Keep the user happy */
259     Print (stdout, 1, "Opened `%s'...\n", D->Filename);
260
261     /* Dump all memory areas */
262     M = F->MemList;
263     while (M) {
264         Print (stdout, 1, "  Dumping `%s'\n", GetString (M->Name));
265         BinWriteMem (D, M);
266         M = M->FNext;
267     }
268
269     /* Close the file */
270     if (fclose (D->F) != 0) {
271         Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
272     }
273
274     /* Reset the file and filename */
275     D->F        = 0;
276     D->Filename = 0;
277 }
278
279
280
281