1 /*****************************************************************************/
5 /* Object file writing routines for the ca65 macroassembler */
9 /* (C) 1998 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 /*****************************************************************************/
41 #include "../common/objdefs.h"
51 /*****************************************************************************/
53 /*****************************************************************************/
60 /* Default extension */
63 /* Header structure */
64 static ObjHeader Header = {
71 /*****************************************************************************/
72 /* Internally used functions */
73 /*****************************************************************************/
77 static void ObjWriteError (void)
78 /* Called on a write error. Will try to close and remove the file, then
79 * print a fatal error.
82 /* Remember the error */
85 /* Force a close of the file, ignoring errors */
88 /* Try to remove the file, also ignoring errors */
91 /* Now abort with a fatal error */
92 Fatal (FAT_CANNOT_WRITE_OUTPUT, OutFile, strerror (Error));
97 static void ObjWriteHeader (void)
98 /* Write the object file header to the current file position */
100 ObjWrite32 (Header.Magic);
101 ObjWrite16 (Header.Version);
102 ObjWrite16 (Header.Flags);
103 ObjWrite32 (Header.OptionOffs);
104 ObjWrite32 (Header.OptionSize);
105 ObjWrite32 (Header.FileOffs);
106 ObjWrite32 (Header.FileSize);
107 ObjWrite32 (Header.SegOffs);
108 ObjWrite32 (Header.SegSize);
109 ObjWrite32 (Header.ImportOffs);
110 ObjWrite32 (Header.ImportSize);
111 ObjWrite32 (Header.ExportOffs);
112 ObjWrite32 (Header.ExportSize);
113 ObjWrite32 (Header.DbgSymOffs);
114 ObjWrite32 (Header.DbgSymSize);
119 /*****************************************************************************/
121 /*****************************************************************************/
126 /* Open the object file for writing, write a dummy header */
128 /* Do we have a name for the output file? */
130 /* We don't have an output name explicitly given, construct one from
131 * the name of the input file.
133 OutFile = MakeFilename (InFile, OBJ_EXT);
136 /* Create the output file */
137 F = fopen (OutFile, "w+b");
139 Fatal (FAT_CANNOT_OPEN_OUTPUT, OutFile, strerror (errno));
142 /* Write a dummy header */
149 /* Write an update header and close the object file. */
151 /* Go back to the beginning */
152 if (fseek (F, 0, SEEK_SET) != 0) {
156 /* If we have debug infos, set the flag in the header */
158 Header.Flags |= OBJ_FLAGS_DBGINFO;
161 /* Write the updated header */
165 if (fclose (F) != 0) {
172 void ObjWrite8 (unsigned char V)
173 /* Write an 8 bit value to the file */
175 if (putc (V, F) == EOF) {
182 void ObjWrite16 (unsigned V)
183 /* Write a 16 bit value to the file */
191 void ObjWrite24 (unsigned long V)
192 /* Write a 24 bit value to the file */
201 void ObjWrite32 (unsigned long V)
202 /* Write a 32 bit value to the file */
212 void ObjWriteStr (const char* S)
213 /* Write a string to the object file */
215 unsigned Len = strlen (S);
217 Internal ("String too long in ObjWriteStr");
220 /* Write the string with a length byte preceeded (this is easier for
221 * the reading routine than the C format since the length is known in
224 ObjWrite8 ((unsigned char) Len);
225 ObjWriteData (S, Len);
230 void ObjWriteData (const void* Data, unsigned Size)
231 /* Write literal data to the file */
233 if (fwrite (Data, 1, Size, F) != Size) {
240 void ObjWritePos (const FilePos* Pos)
241 /* Write a file position to the object file */
243 /* Write the line number as 24 bit value to save one byte */
244 ObjWrite24 (Pos->Line);
245 ObjWrite8 (Pos->Col);
246 if (Pos->Name == 0) {
247 /* Position is outside file scope, use the main file instead */
250 ObjWrite8 (Pos->Name - 1);
256 void ObjStartOptions (void)
257 /* Mark the start of the option section */
259 Header.OptionOffs = ftell (F);
264 void ObjEndOptions (void)
265 /* Mark the end of the option section */
267 Header.OptionSize = ftell (F) - Header.OptionOffs;
272 void ObjStartFiles (void)
273 /* Mark the start of the files section */
275 Header.FileOffs = ftell (F);
280 void ObjEndFiles (void)
281 /* Mark the end of the files section */
283 Header.FileSize = ftell (F) - Header.FileOffs;
288 void ObjStartSegments (void)
289 /* Mark the start of the segment section */
291 Header.SegOffs = ftell (F);
296 void ObjEndSegments (void)
297 /* Mark the end of the segment section */
299 Header.SegSize = ftell (F) - Header.SegOffs;
304 void ObjStartImports (void)
305 /* Mark the start of the import section */
307 Header.ImportOffs = ftell (F);
312 void ObjEndImports (void)
313 /* Mark the end of the import section */
315 Header.ImportSize = ftell (F) - Header.ImportOffs;
320 void ObjStartExports (void)
321 /* Mark the start of the export section */
323 Header.ExportOffs = ftell (F);
328 void ObjEndExports (void)
329 /* Mark the end of the export section */
331 Header.ExportSize = ftell (F) - Header.ExportOffs;
336 void ObjStartDbgSyms (void)
337 /* Mark the start of the debug symbol section */
339 Header.DbgSymOffs = ftell (F);
344 void ObjEndDbgSyms (void)
345 /* Mark the end of the debug symbol section */
347 Header.DbgSymSize = ftell (F) - Header.DbgSymOffs;