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/fname.h"
42 #include "../common/objdefs.h"
50 /*****************************************************************************/
52 /*****************************************************************************/
59 /* Default extension */
62 /* Header structure */
63 static ObjHeader Header = {
70 /*****************************************************************************/
71 /* Internally used functions */
72 /*****************************************************************************/
76 static void ObjWriteError (void)
77 /* Called on a write error. Will try to close and remove the file, then
78 * print a fatal error.
81 /* Remember the error */
84 /* Force a close of the file, ignoring errors */
87 /* Try to remove the file, also ignoring errors */
90 /* Now abort with a fatal error */
91 Fatal (FAT_CANNOT_WRITE_OUTPUT, OutFile, strerror (Error));
96 static void ObjWriteHeader (void)
97 /* Write the object file header to the current file position */
99 ObjWrite32 (Header.Magic);
100 ObjWrite16 (Header.Version);
101 ObjWrite16 (Header.Flags);
102 ObjWrite32 (Header.OptionOffs);
103 ObjWrite32 (Header.OptionSize);
104 ObjWrite32 (Header.FileOffs);
105 ObjWrite32 (Header.FileSize);
106 ObjWrite32 (Header.SegOffs);
107 ObjWrite32 (Header.SegSize);
108 ObjWrite32 (Header.ImportOffs);
109 ObjWrite32 (Header.ImportSize);
110 ObjWrite32 (Header.ExportOffs);
111 ObjWrite32 (Header.ExportSize);
112 ObjWrite32 (Header.DbgSymOffs);
113 ObjWrite32 (Header.DbgSymSize);
118 /*****************************************************************************/
120 /*****************************************************************************/
125 /* Open the object file for writing, write a dummy header */
127 /* Do we have a name for the output file? */
129 /* We don't have an output name explicitly given, construct one from
130 * the name of the input file.
132 OutFile = MakeFilename (InFile, OBJ_EXT);
135 /* Create the output file */
136 F = fopen (OutFile, "w+b");
138 Fatal (FAT_CANNOT_OPEN_OUTPUT, OutFile, strerror (errno));
141 /* Write a dummy header */
148 /* Write an update header and close the object file. */
150 /* Go back to the beginning */
151 if (fseek (F, 0, SEEK_SET) != 0) {
155 /* If we have debug infos, set the flag in the header */
157 Header.Flags |= OBJ_FLAGS_DBGINFO;
160 /* Write the updated header */
164 if (fclose (F) != 0) {
171 void ObjWrite8 (unsigned V)
172 /* Write an 8 bit value to the file */
174 if (putc (V, F) == EOF) {
181 void ObjWrite16 (unsigned V)
182 /* Write a 16 bit value to the file */
190 void ObjWrite24 (unsigned long V)
191 /* Write a 24 bit value to the file */
200 void ObjWrite32 (unsigned long V)
201 /* Write a 32 bit value to the file */
211 void ObjWriteStr (const char* S)
212 /* Write a string to the object file */
214 unsigned Len = strlen (S);
216 Internal ("String too long in ObjWriteStr");
219 /* Write the string with a length byte preceeded (this is easier for
220 * the reading routine than the C format since the length is known in
223 ObjWrite8 ((unsigned char) Len);
224 ObjWriteData (S, Len);
229 void ObjWriteData (const void* Data, unsigned Size)
230 /* Write literal data to the file */
232 if (fwrite (Data, 1, Size, F) != Size) {
239 void ObjWritePos (const FilePos* Pos)
240 /* Write a file position to the object file */
242 /* Write the line number as 24 bit value to save one byte */
243 ObjWrite24 (Pos->Line);
244 ObjWrite8 (Pos->Col);
245 if (Pos->Name == 0) {
246 /* Position is outside file scope, use the main file instead */
249 ObjWrite8 (Pos->Name - 1);
255 void ObjStartOptions (void)
256 /* Mark the start of the option section */
258 Header.OptionOffs = ftell (F);
263 void ObjEndOptions (void)
264 /* Mark the end of the option section */
266 Header.OptionSize = ftell (F) - Header.OptionOffs;
271 void ObjStartFiles (void)
272 /* Mark the start of the files section */
274 Header.FileOffs = ftell (F);
279 void ObjEndFiles (void)
280 /* Mark the end of the files section */
282 Header.FileSize = ftell (F) - Header.FileOffs;
287 void ObjStartSegments (void)
288 /* Mark the start of the segment section */
290 Header.SegOffs = ftell (F);
295 void ObjEndSegments (void)
296 /* Mark the end of the segment section */
298 Header.SegSize = ftell (F) - Header.SegOffs;
303 void ObjStartImports (void)
304 /* Mark the start of the import section */
306 Header.ImportOffs = ftell (F);
311 void ObjEndImports (void)
312 /* Mark the end of the import section */
314 Header.ImportSize = ftell (F) - Header.ImportOffs;
319 void ObjStartExports (void)
320 /* Mark the start of the export section */
322 Header.ExportOffs = ftell (F);
327 void ObjEndExports (void)
328 /* Mark the end of the export section */
330 Header.ExportSize = ftell (F) - Header.ExportOffs;
335 void ObjStartDbgSyms (void)
336 /* Mark the start of the debug symbol section */
338 Header.DbgSymOffs = ftell (F);
343 void ObjEndDbgSyms (void)
344 /* Mark the end of the debug symbol section */
346 Header.DbgSymSize = ftell (F) - Header.DbgSymOffs;