1 /*****************************************************************************/
5 /* Module to handle the raw binary format */
9 /* (C) 1999-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 /*****************************************************************************/
40 #include "../common/xmalloc.h"
53 /*****************************************************************************/
55 /*****************************************************************************/
60 unsigned Undef; /* Count of undefined externals */
61 FILE* F; /* Output file */
62 const char* Filename; /* Name of output file */
67 /*****************************************************************************/
69 /*****************************************************************************/
73 BinDesc* NewBinDesc (void)
74 /* Create a new binary format descriptor */
76 /* Allocate memory for a new BinDesc struct */
77 BinDesc* D = xmalloc (sizeof (BinDesc));
79 /* Initialize the fields */
84 /* Return the created struct */
90 void FreeBinDesc (BinDesc* D)
91 /* Free a binary format descriptor */
98 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
99 unsigned long Offs, void* Data)
100 /* Called from SegWrite for an expression. Evaluate the expression, check the
101 * range and write the expression value to the file.
104 /* There's a predefined function to handle constant expressions */
105 return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
110 static void BinWriteMem (BinDesc* D, Memory* M)
111 /* Write the segments of one memory area to a file */
113 /* Get the start address of this memory area */
114 unsigned long Addr = M->Start;
116 /* Get a pointer to the first segment node */
117 MemListNode* N = M->SegList;
122 /* Get the segment from the list node */
125 /* Keep the user happy */
127 printf (" Writing `%s'\n", S->Name);
130 /* Writes do only occur in the load area and not for BSS segments */
131 DoWrite = (S->Flags & SF_BSS) == 0 && /* No BSS segment */
132 S->Load == M && /* LOAD segment */
133 S->Seg->Dumped == 0; /* Not already written */
135 /* Check if we would need an alignment */
136 if (S->Seg->Align > S->Align) {
137 /* Segment itself requires larger alignment than configured
140 Warning ("Segment `%s' in module `%s' requires larger alignment",
141 S->Name, S->Seg->AlignObj->Name);
144 /* Handle ALIGN and OFFSET/START */
145 if (S->Flags & SF_ALIGN) {
146 /* Align the address */
147 unsigned long Val, NewAddr;
148 Val = (0x01UL << S->Align) - 1;
149 NewAddr = (Addr + Val) & ~Val;
151 WriteMult (D->F, M->FillVal, NewAddr-Addr);
154 /* Remember the fill value for the segment */
155 S->Seg->FillVal = M->FillVal;
156 } else if (S->Flags & (SF_OFFSET | SF_START)) {
157 unsigned long NewAddr = S->Addr;
158 if (S->Flags & SF_OFFSET) {
159 /* It's an offset, not a fixed address, make an address */
163 WriteMult (D->F, M->FillVal, NewAddr-Addr);
168 /* Now write the segment to disk if it is not a BSS type segment and
169 * if the memory area is the load area.
172 SegWrite (D->F, S->Seg, BinWriteExpr, D);
173 } else if (M->Flags & MF_FILL) {
174 WriteMult (D->F, M->FillVal, S->Seg->Size);
178 /* Calculate the new address */
179 Addr += S->Seg->Size;
181 /* Next segment node */
185 /* If a fill was requested, fill the remaining space */
186 if (M->Flags & MF_FILL) {
187 while (M->FillLevel < M->Size) {
188 Write8 (D->F, M->FillVal);
196 static int BinUnresolved (const char* Name, void* D)
197 /* Called if an unresolved symbol is encountered */
199 /* Unresolved symbols are an error in binary format. Bump the counter
200 * and return zero telling the caller that the symbol is indeed
203 ((BinDesc*) D)->Undef++;
209 void BinWriteTarget (BinDesc* D, struct File_* F)
210 /* Write a binary output file */
214 /* Place the filename in the control structure */
215 D->Filename = F->Name;
217 /* Check for unresolved symbols. The function BinUnresolved is called
218 * if we get an unresolved symbol.
220 D->Undef = 0; /* Reset the counter */
221 CheckExports (BinUnresolved, D);
223 /* We had unresolved symbols, cannot create output file */
224 Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
228 D->F = fopen (F->Name, "wb");
230 Error ("Cannot open `%s': %s", F->Name, strerror (errno));
233 /* Keep the user happy */
235 printf ("Opened `%s'...\n", F->Name);
238 /* Dump all memory areas */
242 printf (" Dumping `%s'\n", M->Name);
249 if (fclose (D->F) != 0) {
250 Error ("Cannot write to `%s': %s", F->Name, strerror (errno));
253 /* Reset the file and filename */