1 /*****************************************************************************/
5 /* Object file writing routines for the ca65 macroassembler */
9 /* (C) 1998-2000 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 /*****************************************************************************/
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);
120 /*****************************************************************************/
122 /*****************************************************************************/
127 /* Open the object file for writing, write a dummy header */
129 /* Do we have a name for the output file? */
131 /* We don't have an output name explicitly given, construct one from
132 * the name of the input file.
134 OutFile = MakeFilename (InFile, OBJ_EXT);
137 /* Create the output file */
138 F = fopen (OutFile, "w+b");
140 Fatal (FAT_CANNOT_OPEN_OUTPUT, OutFile, strerror (errno));
143 /* Write a dummy header */
150 /* Write an update header and close the object file. */
152 /* Go back to the beginning */
153 if (fseek (F, 0, SEEK_SET) != 0) {
157 /* If we have debug infos, set the flag in the header */
159 Header.Flags |= OBJ_FLAGS_DBGINFO;
162 /* Write the updated header */
166 if (fclose (F) != 0) {
173 void ObjWrite8 (unsigned V)
174 /* Write an 8 bit value to the file */
176 if (putc (V, F) == EOF) {
183 void ObjWrite16 (unsigned V)
184 /* Write a 16 bit value to the file */
192 void ObjWrite24 (unsigned long V)
193 /* Write a 24 bit value to the file */
202 void ObjWrite32 (unsigned long V)
203 /* Write a 32 bit value to the file */
213 void ObjWriteVar (unsigned long V)
214 /* Write a variable sized value to the file in special encoding */
216 /* We will write the value to the file in 7 bit chunks. If the 8th bit
217 * is clear, we're done, if it is set, another chunk follows. This will
218 * allow us to encode smaller values with less bytes, at the expense of
219 * needing 5 bytes if a 32 bit value is written to file.
222 unsigned char C = (V & 0x7F);
233 void ObjWriteStr (const char* S)
234 /* Write a string to the object file */
236 unsigned Len = strlen (S);
238 /* Write the string with the length preceeded (this is easier for
239 * the reading routine than the C format since the length is known in
243 ObjWriteData (S, Len);
248 void ObjWriteData (const void* Data, unsigned Size)
249 /* Write literal data to the file */
251 if (fwrite (Data, 1, Size, F) != Size) {
258 void ObjWritePos (const FilePos* Pos)
259 /* Write a file position to the object file */
261 /* Write the data entries */
262 ObjWriteVar (Pos->Line);
263 ObjWriteVar (Pos->Col);
264 if (Pos->Name == 0) {
265 /* Position is outside file scope, use the main file instead */
268 ObjWriteVar (Pos->Name - 1);
274 void ObjStartOptions (void)
275 /* Mark the start of the option section */
277 Header.OptionOffs = ftell (F);
282 void ObjEndOptions (void)
283 /* Mark the end of the option section */
285 Header.OptionSize = ftell (F) - Header.OptionOffs;
290 void ObjStartFiles (void)
291 /* Mark the start of the files section */
293 Header.FileOffs = ftell (F);
298 void ObjEndFiles (void)
299 /* Mark the end of the files section */
301 Header.FileSize = ftell (F) - Header.FileOffs;
306 void ObjStartSegments (void)
307 /* Mark the start of the segment section */
309 Header.SegOffs = ftell (F);
314 void ObjEndSegments (void)
315 /* Mark the end of the segment section */
317 Header.SegSize = ftell (F) - Header.SegOffs;
322 void ObjStartImports (void)
323 /* Mark the start of the import section */
325 Header.ImportOffs = ftell (F);
330 void ObjEndImports (void)
331 /* Mark the end of the import section */
333 Header.ImportSize = ftell (F) - Header.ImportOffs;
338 void ObjStartExports (void)
339 /* Mark the start of the export section */
341 Header.ExportOffs = ftell (F);
346 void ObjEndExports (void)
347 /* Mark the end of the export section */
349 Header.ExportSize = ftell (F) - Header.ExportOffs;
354 void ObjStartDbgSyms (void)
355 /* Mark the start of the debug symbol section */
357 Header.DbgSymOffs = ftell (F);
362 void ObjEndDbgSyms (void)
363 /* Mark the end of the debug symbol section */
365 Header.DbgSymSize = ftell (F) - Header.DbgSymOffs;