]> git.sur5r.net Git - cc65/blob - src/ld65/bin.c
017b5a15cf8139e8cdeef6d6d32c0264aa4a4ccf
[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 "global.h"
46 #include "error.h"
47 #include "fileio.h"
48 #include "lineinfo.h"
49 #include "segments.h"
50 #include "exports.h"
51 #include "config.h"
52 #include "expr.h"
53 #include "bin.h"
54
55
56
57 /*****************************************************************************/
58 /*                                   Data                                    */
59 /*****************************************************************************/
60
61
62
63 struct BinDesc {
64     unsigned    Undef;          /* Count of undefined externals */
65     FILE*       F;              /* Output file */
66     const char* Filename;       /* Name of output file */
67 };
68
69
70
71 /*****************************************************************************/
72 /*                                   Code                                    */
73 /*****************************************************************************/
74
75
76
77 BinDesc* NewBinDesc (void)
78 /* Create a new binary format descriptor */
79 {
80     /* Allocate memory for a new BinDesc struct */
81     BinDesc* D = xmalloc (sizeof (BinDesc));
82
83     /* Initialize the fields */
84     D->Undef    = 0;
85     D->F        = 0;
86     D->Filename = 0;
87
88     /* Return the created struct */
89     return D;
90 }
91
92
93
94 void FreeBinDesc (BinDesc* D)
95 /* Free a binary format descriptor */
96 {
97     xfree (D);
98 }
99
100
101
102 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
103                               unsigned long Offs attribute ((unused)),
104                               void* Data)
105 /* Called from SegWrite for an expression. Evaluate the expression, check the
106  * range and write the expression value to the file.
107  */
108 {
109     /* There's a predefined function to handle constant expressions */
110     return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
111 }
112
113
114
115 static void PrintBoolVal (const char* Name, int B)
116 /* Print a boolean value for debugging */
117 {
118     printf ("      %s = %s\n", Name, B? "true" : "false");
119 }
120
121
122
123 static void BinWriteMem (BinDesc* D, Memory* M)
124 /* Write the segments of one memory area to a file */
125 {
126     /* Get the start address of this memory area */
127     unsigned long Addr = M->Start;
128
129     /* Get a pointer to the first segment node */
130     MemListNode* N = M->SegList;
131     while (N) {
132
133         int DoWrite;
134
135         /* Get the segment from the list node */
136         SegDesc* S = N->Seg;
137
138         /* Keep the user happy */
139         Print (stdout, 1, "    Writing `%s'\n", S->Name);
140
141         /* Writes do only occur in the load area and not for BSS segments */
142         DoWrite = (S->Flags & SF_BSS) == 0      &&      /* No BSS segment */
143                    S->Load == M                 &&      /* LOAD segment */
144                    S->Seg->Dumped == 0;                 /* Not already written */
145
146         /* Output the DoWrite flag for debugging */
147         if (Verbosity > 1) {
148             PrintBoolVal ("bss", S->Flags & SF_BSS);
149             PrintBoolVal ("LoadArea", S->Load == M);
150             PrintBoolVal ("Dumped", S->Seg->Dumped);
151             PrintBoolVal ("DoWrite", DoWrite);
152         }
153
154         /* Check if we would need an alignment */
155         if (S->Seg->Align > S->Align) {
156             /* Segment itself requires larger alignment than configured
157              * in the linker.
158              */
159             Warning ("Segment `%s' in module `%s' requires larger alignment",
160                      S->Name, GetObjFileName (S->Seg->AlignObj));
161         }
162
163         /* Handle ALIGN and OFFSET/START */
164         if (S->Flags & SF_ALIGN) {
165             /* Align the address */
166             unsigned long Val, NewAddr;
167             Val = (0x01UL << S->Align) - 1;
168             NewAddr = (Addr + Val) & ~Val;
169             if (DoWrite) {
170                 WriteMult (D->F, M->FillVal, NewAddr-Addr);
171             }
172             Addr = NewAddr;
173             /* Remember the fill value for the segment */
174             S->Seg->FillVal = M->FillVal;
175         } else if (S->Flags & (SF_OFFSET | SF_START)) {
176             unsigned long NewAddr = S->Addr;
177             if (S->Flags & SF_OFFSET) {
178                 /* It's an offset, not a fixed address, make an address */
179                 NewAddr += M->Start;
180             }
181             if (DoWrite) {
182                 WriteMult (D->F, M->FillVal, NewAddr-Addr);
183             }
184             Addr = NewAddr;
185         }
186
187         /* Now write the segment to disk if it is not a BSS type segment and
188          * if the memory area is the load area.
189          */
190         if (DoWrite) {
191             RelocLineInfo (S->Seg);
192             SegWrite (D->F, S->Seg, BinWriteExpr, D);
193         } else if (M->Flags & MF_FILL) {
194             WriteMult (D->F, M->FillVal, S->Seg->Size);
195         }
196
197         /* If this was the load memory area, mark the segment as dumped */
198         if (S->Load == M) {
199             S->Seg->Dumped = 1;
200         }
201
202         /* Calculate the new address */
203         Addr += S->Seg->Size;
204
205         /* Next segment node */
206         N = N->Next;
207     }
208
209     /* If a fill was requested, fill the remaining space */
210     if (M->Flags & MF_FILL) {
211         while (M->FillLevel < M->Size) {
212             Write8 (D->F, M->FillVal);
213             ++M->FillLevel;
214         }
215     }
216 }
217
218
219
220 static int BinUnresolved (const char* Name attribute ((unused)), void* D)
221 /* Called if an unresolved symbol is encountered */
222 {
223     /* Unresolved symbols are an error in binary format. Bump the counter
224      * and return zero telling the caller that the symbol is indeed
225      * unresolved.
226      */
227     ((BinDesc*) D)->Undef++;
228     return 0;
229 }
230
231
232
233 void BinWriteTarget (BinDesc* D, struct File* F)
234 /* Write a binary output file */
235 {
236     Memory* M;
237
238     /* Place the filename in the control structure */
239     D->Filename = F->Name;
240
241     /* Check for unresolved symbols. The function BinUnresolved is called
242      * if we get an unresolved symbol.
243      */
244     D->Undef = 0;               /* Reset the counter */
245     CheckExports (BinUnresolved, D);
246     if (D->Undef > 0) {
247         /* We had unresolved symbols, cannot create output file */
248         Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
249     }
250
251     /* Open the file */
252     D->F = fopen (F->Name, "wb");
253     if (D->F == 0) {
254         Error ("Cannot open `%s': %s", F->Name, strerror (errno));
255     }
256
257     /* Keep the user happy */
258     Print (stdout, 1, "Opened `%s'...\n", F->Name);
259
260     /* Dump all memory areas */
261     M = F->MemList;
262     while (M) {
263         Print (stdout, 1, "  Dumping `%s'\n", M->Name);
264         BinWriteMem (D, M);
265         M = M->FNext;
266     }
267
268     /* Close the file */
269     if (fclose (D->F) != 0) {
270         Error ("Cannot write to `%s': %s", F->Name, strerror (errno));
271     }
272
273     /* Reset the file and filename */
274     D->F        = 0;
275     D->Filename = 0;
276 }
277
278
279