1 /*****************************************************************************/
5 /* Module to handle the raw binary format */
9 /* (C) 1999-2001 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
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 /*****************************************************************************/
57 /*****************************************************************************/
59 /*****************************************************************************/
64 unsigned Undef; /* Count of undefined externals */
65 FILE* F; /* Output file */
66 const char* Filename; /* Name of output file */
71 /*****************************************************************************/
73 /*****************************************************************************/
77 BinDesc* NewBinDesc (void)
78 /* Create a new binary format descriptor */
80 /* Allocate memory for a new BinDesc struct */
81 BinDesc* D = xmalloc (sizeof (BinDesc));
83 /* Initialize the fields */
88 /* Return the created struct */
94 void FreeBinDesc (BinDesc* D)
95 /* Free a binary format descriptor */
102 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
103 unsigned long Offs attribute ((unused)),
105 /* Called from SegWrite for an expression. Evaluate the expression, check the
106 * range and write the expression value to the file.
109 /* There's a predefined function to handle constant expressions */
110 return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
115 static void PrintBoolVal (const char* Name, int B)
116 /* Print a boolean value for debugging */
118 printf (" %s = %s\n", Name, B? "true" : "false");
123 static void BinWriteMem (BinDesc* D, Memory* M)
124 /* Write the segments of one memory area to a file */
126 /* Get the start address of this memory area */
127 unsigned long Addr = M->Start;
129 /* Get a pointer to the first segment node */
130 MemListNode* N = M->SegList;
135 /* Get the segment from the list node */
138 /* Keep the user happy */
139 Print (stdout, 1, " Writing `%s'\n", S->Name);
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 */
146 /* Output the DoWrite flag for debugging */
148 PrintBoolVal ("bss", S->Flags & SF_BSS);
149 PrintBoolVal ("LoadArea", S->Load == M);
150 PrintBoolVal ("Dumped", S->Seg->Dumped);
151 PrintBoolVal ("DoWrite", DoWrite);
154 /* Check if we would need an alignment */
155 if (S->Seg->Align > S->Align) {
156 /* Segment itself requires larger alignment than configured
159 Warning ("Segment `%s' in module `%s' requires larger alignment",
160 S->Name, GetObjFileName (S->Seg->AlignObj));
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;
170 WriteMult (D->F, M->FillVal, NewAddr-Addr);
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 */
182 WriteMult (D->F, M->FillVal, NewAddr-Addr);
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.
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);
197 /* If this was the load memory area, mark the segment as dumped */
202 /* Calculate the new address */
203 Addr += S->Seg->Size;
205 /* Next segment node */
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);
220 static int BinUnresolved (const char* Name attribute ((unused)), void* D)
221 /* Called if an unresolved symbol is encountered */
223 /* Unresolved symbols are an error in binary format. Bump the counter
224 * and return zero telling the caller that the symbol is indeed
227 ((BinDesc*) D)->Undef++;
233 void BinWriteTarget (BinDesc* D, struct File* F)
234 /* Write a binary output file */
238 /* Place the filename in the control structure */
239 D->Filename = F->Name;
241 /* Check for unresolved symbols. The function BinUnresolved is called
242 * if we get an unresolved symbol.
244 D->Undef = 0; /* Reset the counter */
245 CheckExports (BinUnresolved, D);
247 /* We had unresolved symbols, cannot create output file */
248 Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
252 D->F = fopen (F->Name, "wb");
254 Error ("Cannot open `%s': %s", F->Name, strerror (errno));
257 /* Keep the user happy */
258 Print (stdout, 1, "Opened `%s'...\n", F->Name);
260 /* Dump all memory areas */
263 Print (stdout, 1, " Dumping `%s'\n", M->Name);
269 if (fclose (D->F) != 0) {
270 Error ("Cannot write to `%s': %s", F->Name, strerror (errno));
273 /* Reset the file and filename */