2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* objfile.c */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Object file writing routines for the ca65 macroassembler */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2011-08-21 19:08:23 +00:00
|
|
|
/* (C) 1998-2011, Ullrich von Bassewitz */
|
|
|
|
/* Roemerstrasse 52 */
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
/* */
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
/* */
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
|
|
|
/* */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2000-07-30 21:27:37 +00:00
|
|
|
/* common */
|
|
|
|
#include "fname.h"
|
|
|
|
#include "objdefs.h"
|
2000-08-02 13:23:06 +00:00
|
|
|
|
2000-07-30 21:27:37 +00:00
|
|
|
/* ca65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "global.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "objfile.h"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* File descriptor */
|
|
|
|
static FILE* F = 0;
|
|
|
|
|
|
|
|
/* Default extension */
|
2013-05-09 11:56:54 +00:00
|
|
|
#define OBJ_EXT ".o"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Header structure */
|
|
|
|
static ObjHeader Header = {
|
2003-05-25 17:57:50 +00:00
|
|
|
OBJ_MAGIC, /* 32: Magic number */
|
|
|
|
OBJ_VERSION, /* 16: Version number */
|
|
|
|
0, /* 16: flags */
|
|
|
|
0, /* 32: Offset to option table */
|
|
|
|
0, /* 32: Size of options */
|
|
|
|
0, /* 32: Offset to file table */
|
|
|
|
0, /* 32: Size of files */
|
|
|
|
0, /* 32: Offset to segment table */
|
|
|
|
0, /* 32: Size of segment table */
|
|
|
|
0, /* 32: Offset to import list */
|
|
|
|
0, /* 32: Size of import list */
|
|
|
|
0, /* 32: Offset to export list */
|
|
|
|
0, /* 32: Size of export list */
|
|
|
|
0, /* 32: Offset to list of debug symbols */
|
|
|
|
0, /* 32: Size of debug symbols */
|
|
|
|
0, /* 32: Offset to list of line infos */
|
|
|
|
0, /* 32: Size of line infos */
|
|
|
|
0, /* 32: Offset to string pool */
|
2003-06-06 12:45:19 +00:00
|
|
|
0, /* 32: Size of string pool */
|
|
|
|
0, /* 32: Offset to assertion table */
|
2003-11-28 22:12:14 +00:00
|
|
|
0, /* 32: Size of assertion table */
|
|
|
|
0, /* 32: Offset into scope table */
|
|
|
|
0, /* 32: Size of scope table */
|
2011-08-21 19:08:23 +00:00
|
|
|
0, /* 32: Offset into span table */
|
|
|
|
0, /* 32: Size of span table */
|
2000-05-28 13:40:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Internally used functions */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ObjWriteError (void)
|
|
|
|
/* Called on a write error. Will try to close and remove the file, then
|
2014-06-30 09:10:35 +00:00
|
|
|
** print a fatal error.
|
|
|
|
*/
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
|
|
|
/* Remember the error */
|
|
|
|
int Error = errno;
|
|
|
|
|
|
|
|
/* Force a close of the file, ignoring errors */
|
|
|
|
fclose (F);
|
|
|
|
|
|
|
|
/* Try to remove the file, also ignoring errors */
|
|
|
|
remove (OutFile);
|
|
|
|
|
|
|
|
/* Now abort with a fatal error */
|
2019-01-05 19:57:12 +00:00
|
|
|
Fatal ("Cannot write to output file '%s': %s", OutFile, strerror (Error));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ObjWriteHeader (void)
|
|
|
|
/* Write the object file header to the current file position */
|
|
|
|
{
|
|
|
|
ObjWrite32 (Header.Magic);
|
|
|
|
ObjWrite16 (Header.Version);
|
|
|
|
ObjWrite16 (Header.Flags);
|
|
|
|
ObjWrite32 (Header.OptionOffs);
|
|
|
|
ObjWrite32 (Header.OptionSize);
|
|
|
|
ObjWrite32 (Header.FileOffs);
|
|
|
|
ObjWrite32 (Header.FileSize);
|
|
|
|
ObjWrite32 (Header.SegOffs);
|
|
|
|
ObjWrite32 (Header.SegSize);
|
|
|
|
ObjWrite32 (Header.ImportOffs);
|
|
|
|
ObjWrite32 (Header.ImportSize);
|
|
|
|
ObjWrite32 (Header.ExportOffs);
|
|
|
|
ObjWrite32 (Header.ExportSize);
|
|
|
|
ObjWrite32 (Header.DbgSymOffs);
|
|
|
|
ObjWrite32 (Header.DbgSymSize);
|
2001-05-23 19:03:40 +00:00
|
|
|
ObjWrite32 (Header.LineInfoOffs);
|
|
|
|
ObjWrite32 (Header.LineInfoSize);
|
2003-05-25 17:57:50 +00:00
|
|
|
ObjWrite32 (Header.StrPoolOffs);
|
|
|
|
ObjWrite32 (Header.StrPoolSize);
|
2003-06-06 12:45:19 +00:00
|
|
|
ObjWrite32 (Header.AssertOffs);
|
|
|
|
ObjWrite32 (Header.AssertSize);
|
2003-11-28 22:12:14 +00:00
|
|
|
ObjWrite32 (Header.ScopeOffs);
|
|
|
|
ObjWrite32 (Header.ScopeSize);
|
2011-08-21 19:08:23 +00:00
|
|
|
ObjWrite32 (Header.SpanOffs);
|
|
|
|
ObjWrite32 (Header.SpanSize);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjOpen (void)
|
|
|
|
/* Open the object file for writing, write a dummy header */
|
|
|
|
{
|
|
|
|
/* Do we have a name for the output file? */
|
|
|
|
if (OutFile == 0) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* We don't have an output name explicitly given, construct one from
|
2014-06-30 09:10:35 +00:00
|
|
|
** the name of the input file.
|
|
|
|
*/
|
2013-05-09 11:56:54 +00:00
|
|
|
OutFile = MakeFilename (InFile, OBJ_EXT);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the output file */
|
|
|
|
F = fopen (OutFile, "w+b");
|
|
|
|
if (F == 0) {
|
2019-01-05 19:57:12 +00:00
|
|
|
Fatal ("Cannot open output file '%s': %s", OutFile, strerror (errno));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write a dummy header */
|
|
|
|
ObjWriteHeader ();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjClose (void)
|
|
|
|
/* Write an update header and close the object file. */
|
|
|
|
{
|
|
|
|
/* Go back to the beginning */
|
|
|
|
if (fseek (F, 0, SEEK_SET) != 0) {
|
2013-05-09 11:56:54 +00:00
|
|
|
ObjWriteError ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have debug infos, set the flag in the header */
|
|
|
|
if (DbgSyms) {
|
|
|
|
Header.Flags |= OBJ_FLAGS_DBGINFO;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the updated header */
|
|
|
|
ObjWriteHeader ();
|
|
|
|
|
|
|
|
/* Close the file */
|
|
|
|
if (fclose (F) != 0) {
|
2013-05-09 11:56:54 +00:00
|
|
|
ObjWriteError ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-03 22:19:46 +00:00
|
|
|
unsigned long ObjGetFilePos (void)
|
|
|
|
/* Get the current file position */
|
|
|
|
{
|
|
|
|
long Pos = ftell (F);
|
|
|
|
if (Pos < 0) {
|
|
|
|
ObjWriteError ();
|
|
|
|
}
|
|
|
|
return Pos;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjSetFilePos (unsigned long Pos)
|
|
|
|
/* Set the file position */
|
|
|
|
{
|
|
|
|
if (fseek (F, Pos, SEEK_SET) != 0) {
|
|
|
|
ObjWriteError ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-08 21:02:46 +00:00
|
|
|
void ObjWrite8 (unsigned V)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Write an 8 bit value to the file */
|
|
|
|
{
|
|
|
|
if (putc (V, F) == EOF) {
|
2013-05-09 11:56:54 +00:00
|
|
|
ObjWriteError ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjWrite16 (unsigned V)
|
|
|
|
/* Write a 16 bit value to the file */
|
|
|
|
{
|
|
|
|
ObjWrite8 (V);
|
|
|
|
ObjWrite8 (V >> 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjWrite24 (unsigned long V)
|
|
|
|
/* Write a 24 bit value to the file */
|
|
|
|
{
|
|
|
|
ObjWrite8 (V);
|
|
|
|
ObjWrite8 (V >> 8);
|
|
|
|
ObjWrite8 (V >> 16);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjWrite32 (unsigned long V)
|
|
|
|
/* Write a 32 bit value to the file */
|
|
|
|
{
|
|
|
|
ObjWrite8 (V);
|
|
|
|
ObjWrite8 (V >> 8);
|
|
|
|
ObjWrite8 (V >> 16);
|
|
|
|
ObjWrite8 (V >> 24);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-08-02 13:23:06 +00:00
|
|
|
void ObjWriteVar (unsigned long V)
|
|
|
|
/* Write a variable sized value to the file in special encoding */
|
|
|
|
{
|
|
|
|
/* We will write the value to the file in 7 bit chunks. If the 8th bit
|
2014-06-30 09:10:35 +00:00
|
|
|
** is clear, we're done, if it is set, another chunk follows. This will
|
|
|
|
** allow us to encode smaller values with less bytes, at the expense of
|
|
|
|
** needing 5 bytes if a 32 bit value is written to file.
|
|
|
|
*/
|
2000-08-02 13:23:06 +00:00
|
|
|
do {
|
2013-05-09 11:56:54 +00:00
|
|
|
unsigned char C = (V & 0x7F);
|
|
|
|
V >>= 7;
|
|
|
|
if (V) {
|
|
|
|
C |= 0x80;
|
|
|
|
}
|
|
|
|
ObjWrite8 (C);
|
2000-08-02 13:23:06 +00:00
|
|
|
} while (V != 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
void ObjWriteStr (const char* S)
|
|
|
|
/* Write a string to the object file */
|
|
|
|
{
|
|
|
|
unsigned Len = strlen (S);
|
|
|
|
|
2000-08-02 13:23:06 +00:00
|
|
|
/* Write the string with the length preceeded (this is easier for
|
2014-06-30 09:10:35 +00:00
|
|
|
** the reading routine than the C format since the length is known in
|
|
|
|
** advance).
|
|
|
|
*/
|
2000-08-02 13:23:06 +00:00
|
|
|
ObjWriteVar (Len);
|
2000-05-28 13:40:48 +00:00
|
|
|
ObjWriteData (S, Len);
|
2001-09-15 11:59:30 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2008-03-31 20:54:45 +00:00
|
|
|
void ObjWriteBuf (const StrBuf* S)
|
|
|
|
/* Write a string to the object file */
|
|
|
|
{
|
|
|
|
/* Write the string with the length preceeded (this is easier for
|
2014-06-30 09:10:35 +00:00
|
|
|
** the reading routine than the C format since the length is known in
|
|
|
|
** advance).
|
|
|
|
*/
|
2008-03-31 20:54:45 +00:00
|
|
|
ObjWriteVar (SB_GetLen (S));
|
2011-08-21 19:08:23 +00:00
|
|
|
ObjWriteData (SB_GetConstBuf (S), SB_GetLen (S));
|
2008-03-31 20:54:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
void ObjWriteData (const void* Data, unsigned Size)
|
|
|
|
/* Write literal data to the file */
|
|
|
|
{
|
|
|
|
if (fwrite (Data, 1, Size, F) != Size) {
|
2013-05-09 11:56:54 +00:00
|
|
|
ObjWriteError ();
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjWritePos (const FilePos* Pos)
|
|
|
|
/* Write a file position to the object file */
|
|
|
|
{
|
2000-08-02 13:23:06 +00:00
|
|
|
/* Write the data entries */
|
|
|
|
ObjWriteVar (Pos->Line);
|
|
|
|
ObjWriteVar (Pos->Col);
|
2000-05-28 13:40:48 +00:00
|
|
|
if (Pos->Name == 0) {
|
2013-05-09 11:56:54 +00:00
|
|
|
/* Position is outside file scope, use the main file instead */
|
|
|
|
ObjWriteVar (0);
|
2000-05-28 13:40:48 +00:00
|
|
|
} else {
|
2000-08-02 13:23:06 +00:00
|
|
|
ObjWriteVar (Pos->Name - 1);
|
2001-05-23 08:51:48 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjStartOptions (void)
|
|
|
|
/* Mark the start of the option section */
|
|
|
|
{
|
|
|
|
Header.OptionOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndOptions (void)
|
|
|
|
/* Mark the end of the option section */
|
|
|
|
{
|
|
|
|
Header.OptionSize = ftell (F) - Header.OptionOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjStartFiles (void)
|
|
|
|
/* Mark the start of the files section */
|
|
|
|
{
|
|
|
|
Header.FileOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndFiles (void)
|
|
|
|
/* Mark the end of the files section */
|
|
|
|
{
|
|
|
|
Header.FileSize = ftell (F) - Header.FileOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjStartSegments (void)
|
|
|
|
/* Mark the start of the segment section */
|
|
|
|
{
|
|
|
|
Header.SegOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndSegments (void)
|
|
|
|
/* Mark the end of the segment section */
|
|
|
|
{
|
|
|
|
Header.SegSize = ftell (F) - Header.SegOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjStartImports (void)
|
|
|
|
/* Mark the start of the import section */
|
|
|
|
{
|
|
|
|
Header.ImportOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndImports (void)
|
|
|
|
/* Mark the end of the import section */
|
|
|
|
{
|
|
|
|
Header.ImportSize = ftell (F) - Header.ImportOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjStartExports (void)
|
|
|
|
/* Mark the start of the export section */
|
|
|
|
{
|
|
|
|
Header.ExportOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndExports (void)
|
|
|
|
/* Mark the end of the export section */
|
|
|
|
{
|
|
|
|
Header.ExportSize = ftell (F) - Header.ExportOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjStartDbgSyms (void)
|
|
|
|
/* Mark the start of the debug symbol section */
|
|
|
|
{
|
|
|
|
Header.DbgSymOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndDbgSyms (void)
|
|
|
|
/* Mark the end of the debug symbol section */
|
|
|
|
{
|
|
|
|
Header.DbgSymSize = ftell (F) - Header.DbgSymOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-05-23 08:51:48 +00:00
|
|
|
void ObjStartLineInfos (void)
|
|
|
|
/* Mark the start of the line info section */
|
|
|
|
{
|
2001-05-23 19:03:40 +00:00
|
|
|
Header.LineInfoOffs = ftell (F);
|
2001-05-23 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndLineInfos (void)
|
|
|
|
/* Mark the end of the line info section */
|
|
|
|
{
|
2001-05-23 19:03:40 +00:00
|
|
|
Header.LineInfoSize = ftell (F) - Header.LineInfoOffs;
|
2001-05-23 08:51:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-05-25 17:57:50 +00:00
|
|
|
void ObjStartStrPool (void)
|
|
|
|
/* Mark the start of the string pool section */
|
|
|
|
{
|
|
|
|
Header.StrPoolOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndStrPool (void)
|
|
|
|
/* Mark the end of the string pool section */
|
|
|
|
{
|
|
|
|
Header.StrPoolSize = ftell (F) - Header.StrPoolOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-06 12:45:19 +00:00
|
|
|
void ObjStartAssertions (void)
|
|
|
|
/* Mark the start of the assertion table */
|
|
|
|
{
|
|
|
|
Header.AssertOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndAssertions (void)
|
|
|
|
/* Mark the end of the assertion table */
|
|
|
|
{
|
|
|
|
Header.AssertSize = ftell (F) - Header.AssertOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-11-28 22:12:14 +00:00
|
|
|
void ObjStartScopes (void)
|
|
|
|
/* Mark the start of the scope table */
|
|
|
|
{
|
|
|
|
Header.ScopeOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndScopes (void)
|
|
|
|
/* Mark the end of the scope table */
|
|
|
|
{
|
|
|
|
Header.ScopeSize = ftell (F) - Header.ScopeOffs;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-21 19:08:23 +00:00
|
|
|
void ObjStartSpans (void)
|
|
|
|
/* Mark the start of the span table */
|
|
|
|
{
|
|
|
|
Header.SpanOffs = ftell (F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void ObjEndSpans (void)
|
|
|
|
/* Mark the end of the span table */
|
|
|
|
{
|
|
|
|
Header.SpanSize = ftell (F) - Header.SpanOffs;
|
|
|
|
}
|