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 */
87 0, /* 32: Offset into scope table */
88 0, /* 32: Size of scope table */
93 /*****************************************************************************/
94 /* Internally used functions */
95 /*****************************************************************************/
99 static void ObjWriteError (void)
100 /* Called on a write error. Will try to close and remove the file, then
101 * print a fatal error.
104 /* Remember the error */
107 /* Force a close of the file, ignoring errors */
110 /* Try to remove the file, also ignoring errors */
113 /* Now abort with a fatal error */
114 Fatal ("Cannot write to output file `%s': %s", OutFile, strerror (Error));
119 static void ObjWriteHeader (void)
120 /* Write the object file header to the current file position */
122 ObjWrite32 (Header.Magic);
123 ObjWrite16 (Header.Version);
124 ObjWrite16 (Header.Flags);
125 ObjWrite32 (Header.OptionOffs);
126 ObjWrite32 (Header.OptionSize);
127 ObjWrite32 (Header.FileOffs);
128 ObjWrite32 (Header.FileSize);
129 ObjWrite32 (Header.SegOffs);
130 ObjWrite32 (Header.SegSize);
131 ObjWrite32 (Header.ImportOffs);
132 ObjWrite32 (Header.ImportSize);
133 ObjWrite32 (Header.ExportOffs);
134 ObjWrite32 (Header.ExportSize);
135 ObjWrite32 (Header.DbgSymOffs);
136 ObjWrite32 (Header.DbgSymSize);
137 ObjWrite32 (Header.LineInfoOffs);
138 ObjWrite32 (Header.LineInfoSize);
139 ObjWrite32 (Header.StrPoolOffs);
140 ObjWrite32 (Header.StrPoolSize);
141 ObjWrite32 (Header.AssertOffs);
142 ObjWrite32 (Header.AssertSize);
143 ObjWrite32 (Header.ScopeOffs);
144 ObjWrite32 (Header.ScopeSize);
149 /*****************************************************************************/
151 /*****************************************************************************/
156 /* Open the object file for writing, write a dummy header */
158 /* Do we have a name for the output file? */
160 /* We don't have an output name explicitly given, construct one from
161 * the name of the input file.
163 OutFile = MakeFilename (InFile, OBJ_EXT);
166 /* Create the output file */
167 F = fopen (OutFile, "w+b");
169 Fatal ("Cannot open output file `%s': %s", OutFile, strerror (errno));
172 /* Write a dummy header */
179 /* Write an update header and close the object file. */
181 /* Go back to the beginning */
182 if (fseek (F, 0, SEEK_SET) != 0) {
186 /* If we have debug infos, set the flag in the header */
188 Header.Flags |= OBJ_FLAGS_DBGINFO;
191 /* Write the updated header */
195 if (fclose (F) != 0) {
202 unsigned long ObjGetFilePos (void)
203 /* Get the current file position */
205 long Pos = ftell (F);
214 void ObjSetFilePos (unsigned long Pos)
215 /* Set the file position */
217 if (fseek (F, Pos, SEEK_SET) != 0) {
224 void ObjWrite8 (unsigned V)
225 /* Write an 8 bit value to the file */
227 if (putc (V, F) == EOF) {
234 void ObjWrite16 (unsigned V)
235 /* Write a 16 bit value to the file */
243 void ObjWrite24 (unsigned long V)
244 /* Write a 24 bit value to the file */
253 void ObjWrite32 (unsigned long V)
254 /* Write a 32 bit value to the file */
264 void ObjWriteVar (unsigned long V)
265 /* Write a variable sized value to the file in special encoding */
267 /* We will write the value to the file in 7 bit chunks. If the 8th bit
268 * is clear, we're done, if it is set, another chunk follows. This will
269 * allow us to encode smaller values with less bytes, at the expense of
270 * needing 5 bytes if a 32 bit value is written to file.
273 unsigned char C = (V & 0x7F);
284 void ObjWriteStr (const char* S)
285 /* Write a string to the object file */
287 unsigned Len = strlen (S);
289 /* Write the string with the length preceeded (this is easier for
290 * the reading routine than the C format since the length is known in
294 ObjWriteData (S, Len);
299 void ObjWriteData (const void* Data, unsigned Size)
300 /* Write literal data to the file */
302 if (fwrite (Data, 1, Size, F) != Size) {
309 void ObjWritePos (const FilePos* Pos)
310 /* Write a file position to the object file */
312 /* Write the data entries */
313 ObjWriteVar (Pos->Line);
314 ObjWriteVar (Pos->Col);
315 if (Pos->Name == 0) {
316 /* Position is outside file scope, use the main file instead */
319 ObjWriteVar (Pos->Name - 1);
325 void ObjStartOptions (void)
326 /* Mark the start of the option section */
328 Header.OptionOffs = ftell (F);
333 void ObjEndOptions (void)
334 /* Mark the end of the option section */
336 Header.OptionSize = ftell (F) - Header.OptionOffs;
341 void ObjStartFiles (void)
342 /* Mark the start of the files section */
344 Header.FileOffs = ftell (F);
349 void ObjEndFiles (void)
350 /* Mark the end of the files section */
352 Header.FileSize = ftell (F) - Header.FileOffs;
357 void ObjStartSegments (void)
358 /* Mark the start of the segment section */
360 Header.SegOffs = ftell (F);
365 void ObjEndSegments (void)
366 /* Mark the end of the segment section */
368 Header.SegSize = ftell (F) - Header.SegOffs;
373 void ObjStartImports (void)
374 /* Mark the start of the import section */
376 Header.ImportOffs = ftell (F);
381 void ObjEndImports (void)
382 /* Mark the end of the import section */
384 Header.ImportSize = ftell (F) - Header.ImportOffs;
389 void ObjStartExports (void)
390 /* Mark the start of the export section */
392 Header.ExportOffs = ftell (F);
397 void ObjEndExports (void)
398 /* Mark the end of the export section */
400 Header.ExportSize = ftell (F) - Header.ExportOffs;
405 void ObjStartDbgSyms (void)
406 /* Mark the start of the debug symbol section */
408 Header.DbgSymOffs = ftell (F);
413 void ObjEndDbgSyms (void)
414 /* Mark the end of the debug symbol section */
416 Header.DbgSymSize = ftell (F) - Header.DbgSymOffs;
421 void ObjStartLineInfos (void)
422 /* Mark the start of the line info section */
424 Header.LineInfoOffs = ftell (F);
429 void ObjEndLineInfos (void)
430 /* Mark the end of the line info section */
432 Header.LineInfoSize = ftell (F) - Header.LineInfoOffs;
437 void ObjStartStrPool (void)
438 /* Mark the start of the string pool section */
440 Header.StrPoolOffs = ftell (F);
445 void ObjEndStrPool (void)
446 /* Mark the end of the string pool section */
448 Header.StrPoolSize = ftell (F) - Header.StrPoolOffs;
453 void ObjStartAssertions (void)
454 /* Mark the start of the assertion table */
456 Header.AssertOffs = ftell (F);
461 void ObjEndAssertions (void)
462 /* Mark the end of the assertion table */
464 Header.AssertSize = ftell (F) - Header.AssertOffs;
469 void ObjStartScopes (void)
470 /* Mark the start of the scope table */
472 Header.ScopeOffs = ftell (F);
477 void ObjEndScopes (void)
478 /* Mark the end of the scope table */
480 Header.ScopeSize = ftell (F) - Header.ScopeOffs;