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 = {
72 /*****************************************************************************/
73 /* Internally used functions */
74 /*****************************************************************************/
78 static void ObjWriteError (void)
79 /* Called on a write error. Will try to close and remove the file, then
80 * print a fatal error.
83 /* Remember the error */
86 /* Force a close of the file, ignoring errors */
89 /* Try to remove the file, also ignoring errors */
92 /* Now abort with a fatal error */
93 Fatal (FAT_CANNOT_WRITE_OUTPUT, OutFile, strerror (Error));
98 static void ObjWriteHeader (void)
99 /* Write the object file header to the current file position */
101 ObjWrite32 (Header.Magic);
102 ObjWrite16 (Header.Version);
103 ObjWrite16 (Header.Flags);
104 ObjWrite32 (Header.OptionOffs);
105 ObjWrite32 (Header.OptionSize);
106 ObjWrite32 (Header.FileOffs);
107 ObjWrite32 (Header.FileSize);
108 ObjWrite32 (Header.SegOffs);
109 ObjWrite32 (Header.SegSize);
110 ObjWrite32 (Header.ImportOffs);
111 ObjWrite32 (Header.ImportSize);
112 ObjWrite32 (Header.ExportOffs);
113 ObjWrite32 (Header.ExportSize);
114 ObjWrite32 (Header.DbgSymOffs);
115 ObjWrite32 (Header.DbgSymSize);
116 ObjWrite32 (Header.LineInfoOffs);
117 ObjWrite32 (Header.LineInfoSize);
122 /*****************************************************************************/
124 /*****************************************************************************/
129 /* Open the object file for writing, write a dummy header */
131 /* Do we have a name for the output file? */
133 /* We don't have an output name explicitly given, construct one from
134 * the name of the input file.
136 OutFile = MakeFilename (InFile, OBJ_EXT);
139 /* Create the output file */
140 F = fopen (OutFile, "w+b");
142 Fatal (FAT_CANNOT_OPEN_OUTPUT, OutFile, strerror (errno));
145 /* Write a dummy header */
152 /* Write an update header and close the object file. */
154 /* Go back to the beginning */
155 if (fseek (F, 0, SEEK_SET) != 0) {
159 /* If we have debug infos, set the flag in the header */
161 Header.Flags |= OBJ_FLAGS_DBGINFO;
164 /* Write the updated header */
168 if (fclose (F) != 0) {
175 void ObjWrite8 (unsigned V)
176 /* Write an 8 bit value to the file */
178 if (putc (V, F) == EOF) {
185 void ObjWrite16 (unsigned V)
186 /* Write a 16 bit value to the file */
194 void ObjWrite24 (unsigned long V)
195 /* Write a 24 bit value to the file */
204 void ObjWrite32 (unsigned long V)
205 /* Write a 32 bit value to the file */
215 void ObjWriteVar (unsigned long V)
216 /* Write a variable sized value to the file in special encoding */
218 /* We will write the value to the file in 7 bit chunks. If the 8th bit
219 * is clear, we're done, if it is set, another chunk follows. This will
220 * allow us to encode smaller values with less bytes, at the expense of
221 * needing 5 bytes if a 32 bit value is written to file.
224 unsigned char C = (V & 0x7F);
235 void ObjWriteStr (const char* S)
236 /* Write a string to the object file */
238 unsigned Len = strlen (S);
240 /* Write the string with the length preceeded (this is easier for
241 * the reading routine than the C format since the length is known in
245 ObjWriteData (S, Len);
250 void ObjWriteData (const void* Data, unsigned Size)
251 /* Write literal data to the file */
253 if (fwrite (Data, 1, Size, F) != Size) {
260 void ObjWritePos (const FilePos* Pos)
261 /* Write a file position to the object file */
263 /* Write the data entries */
264 ObjWriteVar (Pos->Line);
265 ObjWriteVar (Pos->Col);
266 if (Pos->Name == 0) {
267 /* Position is outside file scope, use the main file instead */
270 ObjWriteVar (Pos->Name - 1);
276 void ObjStartOptions (void)
277 /* Mark the start of the option section */
279 Header.OptionOffs = ftell (F);
284 void ObjEndOptions (void)
285 /* Mark the end of the option section */
287 Header.OptionSize = ftell (F) - Header.OptionOffs;
292 void ObjStartFiles (void)
293 /* Mark the start of the files section */
295 Header.FileOffs = ftell (F);
300 void ObjEndFiles (void)
301 /* Mark the end of the files section */
303 Header.FileSize = ftell (F) - Header.FileOffs;
308 void ObjStartSegments (void)
309 /* Mark the start of the segment section */
311 Header.SegOffs = ftell (F);
316 void ObjEndSegments (void)
317 /* Mark the end of the segment section */
319 Header.SegSize = ftell (F) - Header.SegOffs;
324 void ObjStartImports (void)
325 /* Mark the start of the import section */
327 Header.ImportOffs = ftell (F);
332 void ObjEndImports (void)
333 /* Mark the end of the import section */
335 Header.ImportSize = ftell (F) - Header.ImportOffs;
340 void ObjStartExports (void)
341 /* Mark the start of the export section */
343 Header.ExportOffs = ftell (F);
348 void ObjEndExports (void)
349 /* Mark the end of the export section */
351 Header.ExportSize = ftell (F) - Header.ExportOffs;
356 void ObjStartDbgSyms (void)
357 /* Mark the start of the debug symbol section */
359 Header.DbgSymOffs = ftell (F);
364 void ObjEndDbgSyms (void)
365 /* Mark the end of the debug symbol section */
367 Header.DbgSymSize = ftell (F) - Header.DbgSymOffs;
372 void ObjStartLineInfos (void)
373 /* Mark the start of the line info section */
375 Header.LineInfoOffs = ftell (F);
380 void ObjEndLineInfos (void)
381 /* Mark the end of the line info section */
383 Header.LineInfoSize = ftell (F) - Header.LineInfoOffs;