1 /*****************************************************************************/
5 /* Object file writing routines for the ca65 macroassembler */
9 /* (C) 1998-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 /*****************************************************************************/
52 /*****************************************************************************/
54 /*****************************************************************************/
61 /* Default extension */
64 /* Header structure */
65 static ObjHeader Header = {
68 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
73 /*****************************************************************************/
74 /* Internally used functions */
75 /*****************************************************************************/
79 static void ObjWriteError (void)
80 /* Called on a write error. Will try to close and remove the file, then
81 * print a fatal error.
84 /* Remember the error */
87 /* Force a close of the file, ignoring errors */
90 /* Try to remove the file, also ignoring errors */
93 /* Now abort with a fatal error */
94 Fatal (FAT_CANNOT_WRITE_OUTPUT, OutFile, strerror (Error));
99 static void ObjWriteHeader (void)
100 /* Write the object file header to the current file position */
102 ObjWrite32 (Header.Magic);
103 ObjWrite16 (Header.Version);
104 ObjWrite16 (Header.Flags);
105 ObjWrite32 (Header.OptionOffs);
106 ObjWrite32 (Header.OptionSize);
107 ObjWrite32 (Header.FileOffs);
108 ObjWrite32 (Header.FileSize);
109 ObjWrite32 (Header.SegOffs);
110 ObjWrite32 (Header.SegSize);
111 ObjWrite32 (Header.ImportOffs);
112 ObjWrite32 (Header.ImportSize);
113 ObjWrite32 (Header.ExportOffs);
114 ObjWrite32 (Header.ExportSize);
115 ObjWrite32 (Header.DbgSymOffs);
116 ObjWrite32 (Header.DbgSymSize);
117 ObjWrite32 (Header.LineInfoOffs);
118 ObjWrite32 (Header.LineInfoSize);
123 /*****************************************************************************/
125 /*****************************************************************************/
130 /* Open the object file for writing, write a dummy header */
132 /* Do we have a name for the output file? */
134 /* We don't have an output name explicitly given, construct one from
135 * the name of the input file.
137 OutFile = MakeFilename (InFile, OBJ_EXT);
140 /* Create the output file */
141 F = fopen (OutFile, "w+b");
143 Fatal (FAT_CANNOT_OPEN_OUTPUT, OutFile, strerror (errno));
146 /* Write a dummy header */
153 /* Write an update header and close the object file. */
155 /* Go back to the beginning */
156 if (fseek (F, 0, SEEK_SET) != 0) {
160 /* If we have debug infos, set the flag in the header */
162 Header.Flags |= OBJ_FLAGS_DBGINFO;
165 /* Write the updated header */
169 if (fclose (F) != 0) {
176 void ObjWrite8 (unsigned V)
177 /* Write an 8 bit value to the file */
179 if (putc (V, F) == EOF) {
186 void ObjWrite16 (unsigned V)
187 /* Write a 16 bit value to the file */
195 void ObjWrite24 (unsigned long V)
196 /* Write a 24 bit value to the file */
205 void ObjWrite32 (unsigned long V)
206 /* Write a 32 bit value to the file */
216 void ObjWriteVar (unsigned long V)
217 /* Write a variable sized value to the file in special encoding */
219 /* We will write the value to the file in 7 bit chunks. If the 8th bit
220 * is clear, we're done, if it is set, another chunk follows. This will
221 * allow us to encode smaller values with less bytes, at the expense of
222 * needing 5 bytes if a 32 bit value is written to file.
225 unsigned char C = (V & 0x7F);
236 void ObjWriteStr (const char* S)
237 /* Write a string to the object file */
239 unsigned Len = strlen (S);
241 /* Write the string with the length preceeded (this is easier for
242 * the reading routine than the C format since the length is known in
246 ObjWriteData (S, Len);
251 void ObjWriteData (const void* Data, unsigned Size)
252 /* Write literal data to the file */
254 if (fwrite (Data, 1, Size, F) != Size) {
261 void ObjWritePos (const FilePos* Pos)
262 /* Write a file position to the object file */
264 /* Write the data entries */
265 ObjWriteVar (Pos->Line);
266 ObjWriteVar (Pos->Col);
267 if (Pos->Name == 0) {
268 /* Position is outside file scope, use the main file instead */
271 ObjWriteVar (Pos->Name - 1);
277 void ObjStartOptions (void)
278 /* Mark the start of the option section */
280 Header.OptionOffs = ftell (F);
285 void ObjEndOptions (void)
286 /* Mark the end of the option section */
288 Header.OptionSize = ftell (F) - Header.OptionOffs;
293 void ObjStartFiles (void)
294 /* Mark the start of the files section */
296 Header.FileOffs = ftell (F);
301 void ObjEndFiles (void)
302 /* Mark the end of the files section */
304 Header.FileSize = ftell (F) - Header.FileOffs;
309 void ObjStartSegments (void)
310 /* Mark the start of the segment section */
312 Header.SegOffs = ftell (F);
317 void ObjEndSegments (void)
318 /* Mark the end of the segment section */
320 Header.SegSize = ftell (F) - Header.SegOffs;
325 void ObjStartImports (void)
326 /* Mark the start of the import section */
328 Header.ImportOffs = ftell (F);
333 void ObjEndImports (void)
334 /* Mark the end of the import section */
336 Header.ImportSize = ftell (F) - Header.ImportOffs;
341 void ObjStartExports (void)
342 /* Mark the start of the export section */
344 Header.ExportOffs = ftell (F);
349 void ObjEndExports (void)
350 /* Mark the end of the export section */
352 Header.ExportSize = ftell (F) - Header.ExportOffs;
357 void ObjStartDbgSyms (void)
358 /* Mark the start of the debug symbol section */
360 Header.DbgSymOffs = ftell (F);
365 void ObjEndDbgSyms (void)
366 /* Mark the end of the debug symbol section */
368 Header.DbgSymSize = ftell (F) - Header.DbgSymOffs;
373 void ObjStartLineInfos (void)
374 /* Mark the start of the line info section */
376 Header.LineInfoOffs = ftell (F);
381 void ObjEndLineInfos (void)
382 /* Mark the end of the line info section */
384 Header.LineInfoSize = ftell (F) - Header.LineInfoOffs;