1 /*****************************************************************************/
5 /* Object file writing routines for the ca65 macroassembler */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
61 /* Default extension */
64 /* Header structure */
65 static ObjHeader Header = {
66 OBJ_MAGIC, /* 32: Magic number */
67 OBJ_VERSION, /* 16: Version number */
69 0, /* 32: Offset to option table */
70 0, /* 32: Size of options */
71 0, /* 32: Offset to file table */
72 0, /* 32: Size of files */
73 0, /* 32: Offset to segment table */
74 0, /* 32: Size of segment table */
75 0, /* 32: Offset to import list */
76 0, /* 32: Size of import list */
77 0, /* 32: Offset to export list */
78 0, /* 32: Size of export list */
79 0, /* 32: Offset to list of debug symbols */
80 0, /* 32: Size of debug symbols */
81 0, /* 32: Offset to list of line infos */
82 0, /* 32: Size of line infos */
83 0, /* 32: Offset to string pool */
84 0, /* 32: Size of string pool */
85 0, /* 32: Offset to assertion table */
86 0 /* 32: Size of assertion table */
91 /*****************************************************************************/
92 /* Internally used functions */
93 /*****************************************************************************/
97 static void ObjWriteError (void)
98 /* Called on a write error. Will try to close and remove the file, then
99 * print a fatal error.
102 /* Remember the error */
105 /* Force a close of the file, ignoring errors */
108 /* Try to remove the file, also ignoring errors */
111 /* Now abort with a fatal error */
112 Fatal ("Cannot write to output file `%s': %s", OutFile, strerror (Error));
117 static void ObjWriteHeader (void)
118 /* Write the object file header to the current file position */
120 ObjWrite32 (Header.Magic);
121 ObjWrite16 (Header.Version);
122 ObjWrite16 (Header.Flags);
123 ObjWrite32 (Header.OptionOffs);
124 ObjWrite32 (Header.OptionSize);
125 ObjWrite32 (Header.FileOffs);
126 ObjWrite32 (Header.FileSize);
127 ObjWrite32 (Header.SegOffs);
128 ObjWrite32 (Header.SegSize);
129 ObjWrite32 (Header.ImportOffs);
130 ObjWrite32 (Header.ImportSize);
131 ObjWrite32 (Header.ExportOffs);
132 ObjWrite32 (Header.ExportSize);
133 ObjWrite32 (Header.DbgSymOffs);
134 ObjWrite32 (Header.DbgSymSize);
135 ObjWrite32 (Header.LineInfoOffs);
136 ObjWrite32 (Header.LineInfoSize);
137 ObjWrite32 (Header.StrPoolOffs);
138 ObjWrite32 (Header.StrPoolSize);
139 ObjWrite32 (Header.AssertOffs);
140 ObjWrite32 (Header.AssertSize);
145 /*****************************************************************************/
147 /*****************************************************************************/
152 /* Open the object file for writing, write a dummy header */
154 /* Do we have a name for the output file? */
156 /* We don't have an output name explicitly given, construct one from
157 * the name of the input file.
159 OutFile = MakeFilename (InFile, OBJ_EXT);
162 /* Create the output file */
163 F = fopen (OutFile, "w+b");
165 Fatal ("Cannot open output file `%s': %s", OutFile, strerror (errno));
168 /* Write a dummy header */
175 /* Write an update header and close the object file. */
177 /* Go back to the beginning */
178 if (fseek (F, 0, SEEK_SET) != 0) {
182 /* If we have debug infos, set the flag in the header */
184 Header.Flags |= OBJ_FLAGS_DBGINFO;
187 /* Write the updated header */
191 if (fclose (F) != 0) {
198 unsigned long ObjGetFilePos (void)
199 /* Get the current file position */
201 long Pos = ftell (F);
210 void ObjSetFilePos (unsigned long Pos)
211 /* Set the file position */
213 if (fseek (F, Pos, SEEK_SET) != 0) {
220 void ObjWrite8 (unsigned V)
221 /* Write an 8 bit value to the file */
223 if (putc (V, F) == EOF) {
230 void ObjWrite16 (unsigned V)
231 /* Write a 16 bit value to the file */
239 void ObjWrite24 (unsigned long V)
240 /* Write a 24 bit value to the file */
249 void ObjWrite32 (unsigned long V)
250 /* Write a 32 bit value to the file */
260 void ObjWriteVar (unsigned long V)
261 /* Write a variable sized value to the file in special encoding */
263 /* We will write the value to the file in 7 bit chunks. If the 8th bit
264 * is clear, we're done, if it is set, another chunk follows. This will
265 * allow us to encode smaller values with less bytes, at the expense of
266 * needing 5 bytes if a 32 bit value is written to file.
269 unsigned char C = (V & 0x7F);
280 void ObjWriteStr (const char* S)
281 /* Write a string to the object file */
283 unsigned Len = strlen (S);
285 /* Write the string with the length preceeded (this is easier for
286 * the reading routine than the C format since the length is known in
290 ObjWriteData (S, Len);
295 void ObjWriteData (const void* Data, unsigned Size)
296 /* Write literal data to the file */
298 if (fwrite (Data, 1, Size, F) != Size) {
305 void ObjWritePos (const FilePos* Pos)
306 /* Write a file position to the object file */
308 /* Write the data entries */
309 ObjWriteVar (Pos->Line);
310 ObjWriteVar (Pos->Col);
311 if (Pos->Name == 0) {
312 /* Position is outside file scope, use the main file instead */
315 ObjWriteVar (Pos->Name - 1);
321 void ObjStartOptions (void)
322 /* Mark the start of the option section */
324 Header.OptionOffs = ftell (F);
329 void ObjEndOptions (void)
330 /* Mark the end of the option section */
332 Header.OptionSize = ftell (F) - Header.OptionOffs;
337 void ObjStartFiles (void)
338 /* Mark the start of the files section */
340 Header.FileOffs = ftell (F);
345 void ObjEndFiles (void)
346 /* Mark the end of the files section */
348 Header.FileSize = ftell (F) - Header.FileOffs;
353 void ObjStartSegments (void)
354 /* Mark the start of the segment section */
356 Header.SegOffs = ftell (F);
361 void ObjEndSegments (void)
362 /* Mark the end of the segment section */
364 Header.SegSize = ftell (F) - Header.SegOffs;
369 void ObjStartImports (void)
370 /* Mark the start of the import section */
372 Header.ImportOffs = ftell (F);
377 void ObjEndImports (void)
378 /* Mark the end of the import section */
380 Header.ImportSize = ftell (F) - Header.ImportOffs;
385 void ObjStartExports (void)
386 /* Mark the start of the export section */
388 Header.ExportOffs = ftell (F);
393 void ObjEndExports (void)
394 /* Mark the end of the export section */
396 Header.ExportSize = ftell (F) - Header.ExportOffs;
401 void ObjStartDbgSyms (void)
402 /* Mark the start of the debug symbol section */
404 Header.DbgSymOffs = ftell (F);
409 void ObjEndDbgSyms (void)
410 /* Mark the end of the debug symbol section */
412 Header.DbgSymSize = ftell (F) - Header.DbgSymOffs;
417 void ObjStartLineInfos (void)
418 /* Mark the start of the line info section */
420 Header.LineInfoOffs = ftell (F);
425 void ObjEndLineInfos (void)
426 /* Mark the end of the line info section */
428 Header.LineInfoSize = ftell (F) - Header.LineInfoOffs;
433 void ObjStartStrPool (void)
434 /* Mark the start of the string pool section */
436 Header.StrPoolOffs = ftell (F);
441 void ObjEndStrPool (void)
442 /* Mark the end of the string pool section */
444 Header.StrPoolSize = ftell (F) - Header.StrPoolOffs;
449 void ObjStartAssertions (void)
450 /* Mark the start of the assertion table */
452 Header.AssertOffs = ftell (F);
457 void ObjEndAssertions (void)
458 /* Mark the end of the assertion table */
460 Header.AssertSize = ftell (F) - Header.AssertOffs;