1 /*****************************************************************************/
5 /* Module to handle the raw binary format */
9 /* (C) 1999-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
41 #include "alignment.h"
60 /*****************************************************************************/
62 /*****************************************************************************/
67 unsigned Undef; /* Count of undefined externals */
68 FILE* F; /* Output file */
69 const char* Filename; /* Name of output file */
74 /*****************************************************************************/
76 /*****************************************************************************/
80 BinDesc* NewBinDesc (void)
81 /* Create a new binary format descriptor */
83 /* Allocate memory for a new BinDesc struct */
84 BinDesc* D = xmalloc (sizeof (BinDesc));
86 /* Initialize the fields */
91 /* Return the created struct */
97 void FreeBinDesc (BinDesc* D)
98 /* Free a binary format descriptor */
105 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
106 unsigned long Offs attribute ((unused)),
108 /* Called from SegWrite for an expression. Evaluate the expression, check the
109 ** range and write the expression value to the file.
112 /* There's a predefined function to handle constant expressions */
113 return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
118 static void PrintBoolVal (const char* Name, int B)
119 /* Print a boolean value for debugging */
121 Print (stdout, 2, " %s = %s\n", Name, B? "true" : "false");
126 static void PrintNumVal (const char* Name, unsigned long V)
127 /* Print a numerical value for debugging */
129 Print (stdout, 2, " %s = 0x%lx\n", Name, V);
134 static void BinWriteMem (BinDesc* D, MemoryArea* M)
135 /* Write the segments of one memory area to a file */
139 /* Get the start address of this memory area */
140 unsigned long Addr = M->Start;
142 /* Debugging: Check that the file offset is correct */
143 if (ftell (D->F) != (long)M->FileOffs) {
144 Internal ("Invalid file offset for memory area %s: %ld/%lu",
145 GetString (M->Name), ftell (D->F), M->FileOffs);
148 /* Walk over all segments in this memory area */
149 for (I = 0; I < CollCount (&M->SegList); ++I) {
153 /* Get the segment */
154 SegDesc* S = CollAtUnchecked (&M->SegList, I);
156 /* Keep the user happy */
157 Print (stdout, 1, " Writing `%s'\n", GetString (S->Name));
159 /* Writes do only occur in the load area and not for BSS segments */
160 DoWrite = (S->Flags & SF_BSS) == 0 && /* No BSS segment */
161 S->Load == M && /* LOAD segment */
162 S->Seg->Dumped == 0; /* Not already written */
164 /* Output debugging stuff */
165 PrintBoolVal ("bss", S->Flags & SF_BSS);
166 PrintBoolVal ("LoadArea", S->Load == M);
167 PrintBoolVal ("Dumped", S->Seg->Dumped);
168 PrintBoolVal ("DoWrite", DoWrite);
169 PrintNumVal ("Address", Addr);
170 PrintNumVal ("FileOffs", (unsigned long) ftell (D->F));
172 /* If this is the run memory area, we must apply run alignment. If
173 ** this is not the run memory area but the load memory area (which
174 ** means that both are different), we must apply load alignment.
175 ** Beware: DoWrite may be true even if this is the run memory area,
176 ** because it may be also the load memory area.
180 /* Handle ALIGN and OFFSET/START */
181 if (S->Flags & SF_ALIGN) {
182 /* Align the address */
183 unsigned long NewAddr = AlignAddr (Addr, S->RunAlignment);
184 if (DoWrite || (M->Flags & MF_FILL) != 0) {
185 WriteMult (D->F, M->FillVal, NewAddr - Addr);
186 PrintNumVal ("SF_ALIGN", NewAddr - Addr);
189 } else if (S->Flags & (SF_OFFSET | SF_START)) {
190 unsigned long NewAddr = S->Addr;
191 if (S->Flags & SF_OFFSET) {
192 /* It's an offset, not a fixed address, make an address */
195 if (DoWrite || (M->Flags & MF_FILL) != 0) {
196 WriteMult (D->F, M->FillVal, NewAddr-Addr);
197 PrintNumVal ("SF_OFFSET", NewAddr - Addr);
202 } else if (S->Load == M) {
204 /* Handle ALIGN_LOAD */
205 if (S->Flags & SF_ALIGN_LOAD) {
206 /* Align the address */
207 unsigned long NewAddr = AlignAddr (Addr, S->LoadAlignment);
208 if (DoWrite || (M->Flags & MF_FILL) != 0) {
209 WriteMult (D->F, M->FillVal, NewAddr - Addr);
210 PrintNumVal ("SF_ALIGN_LOAD", NewAddr - Addr);
217 /* Now write the segment to disk if it is not a BSS type segment and
218 ** if the memory area is the load area.
221 unsigned long P = ftell (D->F);
222 SegWrite (D->Filename, D->F, S->Seg, BinWriteExpr, D);
223 PrintNumVal ("Wrote", (unsigned long) (ftell (D->F) - P));
224 } else if (M->Flags & MF_FILL) {
225 WriteMult (D->F, S->Seg->FillVal, S->Seg->Size);
226 PrintNumVal ("Filled", (unsigned long) S->Seg->Size);
229 /* If this was the load memory area, mark the segment as dumped */
234 /* Calculate the new address */
235 Addr += S->Seg->Size;
238 /* If a fill was requested, fill the remaining space */
239 if ((M->Flags & MF_FILL) != 0 && M->FillLevel < M->Size) {
240 unsigned long ToFill = M->Size - M->FillLevel;
241 Print (stdout, 2, " Filling 0x%lx bytes with 0x%02x\n",
243 WriteMult (D->F, M->FillVal, ToFill);
244 M->FillLevel = M->Size;
250 static int BinUnresolved (unsigned Name attribute ((unused)), void* D)
251 /* Called if an unresolved symbol is encountered */
253 /* Unresolved symbols are an error in binary format. Bump the counter
254 ** and return zero telling the caller that the symbol is indeed
257 ((BinDesc*) D)->Undef++;
263 void BinWriteTarget (BinDesc* D, struct File* F)
264 /* Write a binary output file */
268 /* Place the filename in the control structure */
269 D->Filename = GetString (F->Name);
271 /* Check for unresolved symbols. The function BinUnresolved is called
272 ** if we get an unresolved symbol.
274 D->Undef = 0; /* Reset the counter */
275 CheckUnresolvedImports (BinUnresolved, D);
277 /* We had unresolved symbols, cannot create output file */
278 Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
282 D->F = fopen (D->Filename, "wb");
284 Error ("Cannot open `%s': %s", D->Filename, strerror (errno));
287 /* Keep the user happy */
288 Print (stdout, 1, "Opened `%s'...\n", D->Filename);
290 /* Dump all memory areas */
291 for (I = 0; I < CollCount (&F->MemoryAreas); ++I) {
293 MemoryArea* M = CollAtUnchecked (&F->MemoryAreas, I);
294 Print (stdout, 1, " Dumping `%s'\n", GetString (M->Name));
299 if (fclose (D->F) != 0) {
300 Error ("Cannot write to `%s': %s", D->Filename, strerror (errno));
303 /* Reset the file and filename */