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 /*****************************************************************************/
55 /*****************************************************************************/
57 /*****************************************************************************/
62 unsigned Undef; /* Count of undefined externals */
63 FILE* F; /* Output file */
64 const char* Filename; /* Name of output file */
69 /*****************************************************************************/
71 /*****************************************************************************/
75 BinDesc* NewBinDesc (void)
76 /* Create a new binary format descriptor */
78 /* Allocate memory for a new BinDesc struct */
79 BinDesc* D = xmalloc (sizeof (BinDesc));
81 /* Initialize the fields */
86 /* Return the created struct */
92 void FreeBinDesc (BinDesc* D)
93 /* Free a binary format descriptor */
100 static unsigned BinWriteExpr (ExprNode* E, int Signed, unsigned Size,
101 unsigned long Offs, void* Data)
102 /* Called from SegWrite for an expression. Evaluate the expression, check the
103 * range and write the expression value to the file.
106 /* There's a predefined function to handle constant expressions */
107 return SegWriteConstExpr (((BinDesc*)Data)->F, E, Signed, Size);
112 static void BinWriteMem (BinDesc* D, Memory* M)
113 /* Write the segments of one memory area to a file */
115 /* Get the start address of this memory area */
116 unsigned long Addr = M->Start;
118 /* Get a pointer to the first segment node */
119 MemListNode* N = M->SegList;
124 /* Get the segment from the list node */
127 /* Keep the user happy */
129 printf (" Writing `%s'\n", S->Name);
132 /* Writes do only occur in the load area and not for BSS segments */
133 DoWrite = (S->Flags & SF_BSS) == 0 && /* No BSS segment */
134 S->Load == M && /* LOAD segment */
135 S->Seg->Dumped == 0; /* Not already written */
137 /* Check if we would need an alignment */
138 if (S->Seg->Align > S->Align) {
139 /* Segment itself requires larger alignment than configured
142 Warning ("Segment `%s' in module `%s' requires larger alignment",
143 S->Name, GetObjFileName (S->Seg->AlignObj));
146 /* Handle ALIGN and OFFSET/START */
147 if (S->Flags & SF_ALIGN) {
148 /* Align the address */
149 unsigned long Val, NewAddr;
150 Val = (0x01UL << S->Align) - 1;
151 NewAddr = (Addr + Val) & ~Val;
153 WriteMult (D->F, M->FillVal, NewAddr-Addr);
156 /* Remember the fill value for the segment */
157 S->Seg->FillVal = M->FillVal;
158 } else if (S->Flags & (SF_OFFSET | SF_START)) {
159 unsigned long NewAddr = S->Addr;
160 if (S->Flags & SF_OFFSET) {
161 /* It's an offset, not a fixed address, make an address */
165 WriteMult (D->F, M->FillVal, NewAddr-Addr);
170 /* Now write the segment to disk if it is not a BSS type segment and
171 * if the memory area is the load area.
174 SegWrite (D->F, S->Seg, BinWriteExpr, D);
175 } else if (M->Flags & MF_FILL) {
176 WriteMult (D->F, M->FillVal, S->Seg->Size);
180 /* Calculate the new address */
181 Addr += S->Seg->Size;
183 /* Next segment node */
187 /* If a fill was requested, fill the remaining space */
188 if (M->Flags & MF_FILL) {
189 while (M->FillLevel < M->Size) {
190 Write8 (D->F, M->FillVal);
198 static int BinUnresolved (const char* Name, void* D)
199 /* Called if an unresolved symbol is encountered */
201 /* Unresolved symbols are an error in binary format. Bump the counter
202 * and return zero telling the caller that the symbol is indeed
205 ((BinDesc*) D)->Undef++;
211 void BinWriteTarget (BinDesc* D, struct File* F)
212 /* Write a binary output file */
216 /* Place the filename in the control structure */
217 D->Filename = F->Name;
219 /* Check for unresolved symbols. The function BinUnresolved is called
220 * if we get an unresolved symbol.
222 D->Undef = 0; /* Reset the counter */
223 CheckExports (BinUnresolved, D);
225 /* We had unresolved symbols, cannot create output file */
226 Error ("%u unresolved external(s) found - cannot create output file", D->Undef);
230 D->F = fopen (F->Name, "wb");
232 Error ("Cannot open `%s': %s", F->Name, strerror (errno));
235 /* Keep the user happy */
237 printf ("Opened `%s'...\n", F->Name);
240 /* Dump all memory areas */
244 printf (" Dumping `%s'\n", M->Name);
251 if (fclose (D->F) != 0) {
252 Error ("Cannot write to `%s': %s", F->Name, strerror (errno));
255 /* Reset the file and filename */