/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include <string.h>
-#include "../common/hashstr.h"
-#include "../common/xmalloc.h"
-
+/* common */
+#include "hashstr.h"
+#include "xmalloc.h"
+
+/* ar65 */
#include "error.h"
#include "objdata.h"
#include "exports.h"
/* Duplicate entry */
Warning ("External symbol `%s' in module `%s' is duplicated in "
"module `%s'",
- Name, GetObjName (L->Module), GetObjName (Module));
+ Name, GetObjName (L->Module), GetObjName (Module));
}
if (L->Next == 0) {
break;
-
+
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include <string.h>
-#include "../common/xmalloc.h"
+/* common */
+#include "xmalloc.h"
+/* ar65 */
#include "error.h"
#include "fileio.h"
+void WriteVar (FILE* F, 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
+ * 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.
+ */
+ do {
+ unsigned char C = (V & 0x7F);
+ V >>= 7;
+ if (V) {
+ C |= 0x80;
+ }
+ Write8 (F, C);
+ } while (V != 0);
+}
+
+
+
void WriteStr (FILE* F, const char* S)
/* Write a string to the file */
{
unsigned Len = strlen (S);
- if (Len > 255) {
- Internal ("String too long");
- }
- Write8 (F, (unsigned char) Len);
+ WriteVar (F, Len);
WriteData (F, S, Len);
}
+unsigned long ReadVar (FILE* F)
+/* Read a variable size value from the file */
+{
+ /* The value was written to the file in 7 bit chunks LSB first. If there
+ * are more bytes, bit 8 is set, otherwise it is clear.
+ */
+ unsigned char C;
+ unsigned long V = 0;
+ unsigned Shift = 0;
+ do {
+ /* Read one byte */
+ C = Read8 (F);
+ /* Encode it into the target value */
+ V |= ((unsigned long)(C & 0x7F)) << Shift;
+ /* Next value */
+ Shift += 7;
+ } while (C & 0x80);
+
+ /* Return the value read */
+ return V;
+}
+
+
+
char* ReadStr (FILE* F)
/* Read a string from the file (the memory will be malloc'ed) */
{
- /* Read the length byte */
- unsigned Len = Read8 (F);
+ /* Read the length */
+ unsigned Len = ReadVar (F);
/* Allocate memory and read the string itself */
char* S = xmalloc (Len + 1);
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
void Write32 (FILE* F, unsigned long Val);
/* Write a 32 bit value to the file */
+void WriteVar (FILE* F, unsigned long V);
+/* Write a variable sized value to the file in special encoding */
+
void WriteStr (FILE* F, const char* S);
/* Write a string to the file */
unsigned long Read32 (FILE* F);
/* Read a 32 bit value from the file */
+unsigned long ReadVar (FILE* F);
+/* Read a variable size value from the file */
+
char* ReadStr (FILE* F);
/* Read a string from the file (the memory will be malloc'ed) */
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include <string.h>
#include <errno.h>
-#include "../common/bitops.h"
-#include "../common/exprdefs.h"
-#include "../common/filepos.h"
-#include "../common/libdefs.h"
-#include "../common/symdefs.h"
-#include "../common/xmalloc.h"
+/* common */
+#include "bitops.h"
+#include "exprdefs.h"
+#include "filepos.h"
+#include "libdefs.h"
+#include "symdefs.h"
+#include "xmalloc.h"
+/* ar65 */
#include "error.h"
#include "global.h"
#include "fileio.h"
+static unsigned long GetVar (unsigned char** Buf)
+/* Get a variable sized value from Buf */
+{
+ unsigned char C;
+ unsigned long V = 0;
+ unsigned Shift = 0;
+ do {
+ /* Read one byte */
+ C = **Buf;
+ ++(*Buf);
+ /* Add this char to the value */
+ V |= ((unsigned long)(C & 0x7F)) << Shift;
+ /* Next value */
+ Shift += 7;
+ } while (C & 0x80);
+
+ /* Return the result */
+ return V;
+}
+
+
+
static void SkipExpr (unsigned char** Buf)
/* Skip an expression in Buf */
{
+static void SkipFilePos (unsigned char** Buf)
+/* Skip a file position in Buf */
+{
+ (void) GetVar (Buf); /* Line */
+ (void) GetVar (Buf); /* Col */
+ (void) GetVar (Buf); /* Name */
+}
+
+
+
static void LibCheckExports (ObjData* O)
/* Insert all exports from the given object file into the global list
* checking for duplicates.
*/
{
- char Name [256];
-
/* Get a pointer to the buffer */
unsigned char* Exports = O->Exports;
- /* First two bytes are export count */
- unsigned Lo = *Exports++;
- unsigned Hi = *Exports++;
- unsigned Count = (Hi << 8) + Lo;
+ /* Get the export count */
+ unsigned Count = GetVar (&Exports);
/* Read the exports */
if (Verbose > 1) {
}
while (Count--) {
- unsigned char Len;
-
/* Get the export tag */
unsigned char Tag = *Exports++;
/* Next thing is name of symbol */
- Len = *Exports++;
+ unsigned Len = GetVar (&Exports);
+ char* Name = xmalloc (Len + 1);
memcpy (Name, Exports, Len);
Name [Len] = '\0';
Exports += Len;
}
/* Skip the position */
- Exports += POS_SIZE;
+ SkipFilePos (&Exports);
/* Insert the name into the hash table */
if (Verbose > 1) {
printf (" %s\n", Name);
}
ExpInsert (Name, O->Index);
+
+ /* Free the name */
+ xfree (Name);
}
}
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* common */
#include "check.h"
#include "xmalloc.h"
-
+
/* ar65 */
#include "error.h"
#include "objdata.h"
/* Set the pool pointer */
ObjPool [Index] = O;
-
+
/* Next object */
++Index;
O = O->Next;
+void DbgInfoLine (void)
+/* Parse and handle LINE subcommand of the .dbg pseudo instruction */
+{
+ ErrorSkip (ERR_NOT_IMPLEMENTED);
+}
+
+
+
+void DbgInfoSym (void)
+/* Parse and handle SYM subcommand of the .dbg pseudo instruction */
+{
+ ErrorSkip (ERR_NOT_IMPLEMENTED);
+}
+
+
+
void DbgInfoFile (void);
-/* Parse and handle the .REPEAT statement */
+/* Parse and handle FILE subcommand of the .dbg pseudo instruction */
+
+void DbgInfoLine (void);
+/* Parse and handle LINE subcommand of the .dbg pseudo instruction */
+
+void DbgInfoSym (void);
+/* Parse and handle SYM subcommand of the .dbg pseudo instruction */
-
+
ObjStartFiles ();
/* Write the file count */
- ObjWrite16 (FileCount);
+ ObjWriteVar (FileCount);
/* Write the file data */
for (I = 0; I < FileCount; ++I) {
Size += F->Len;
F = F->Next;
}
- if (Size < 0x100) {
- ObjWrite8 (FRAG_LITERAL8);
- ObjWrite8 (Size);
- } else if (Size < 0x10000) {
- ObjWrite8 (FRAG_LITERAL16);
- ObjWrite16 (Size);
- } else if (Size < 0x1000000) {
- ObjWrite8 (FRAG_LITERAL24);
- ObjWrite24 (Size);
- } else {
- ObjWrite8 (FRAG_LITERAL32);
- ObjWrite32 (Size);
- }
+ ObjWrite8 (FRAG_LITERAL);
+ ObjWriteVar (Size);
/* Now write the literal data */
F = Frag;
case FRAG_FILL:
ObjWrite8 (FRAG_FILL);
- ObjWrite16 (Frag->Len);
+ ObjWriteVar (Frag->Len);
break;
default:
ObjStartSegments ();
/* First thing is segment count */
- ObjWrite8 (SegmentCount);
+ ObjWriteVar (SegmentCount);
/* Now walk through all segments and write them to the object file */
Seg = SegmentList;
/*****************************************************************************/
-/* Code */
+/* Code */
/*****************************************************************************/
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* common */
#include "fname.h"
#include "objdefs.h"
-
+
/* ca65 */
#include "global.h"
#include "error.h"
+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
+ * 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.
+ */
+ do {
+ unsigned char C = (V & 0x7F);
+ V >>= 7;
+ if (V) {
+ C |= 0x80;
+ }
+ ObjWrite8 (C);
+ } while (V != 0);
+}
+
+
+
void ObjWriteStr (const char* S)
/* Write a string to the object file */
{
unsigned Len = strlen (S);
- if (Len > 255) {
- Internal ("String too long in ObjWriteStr");
- }
- /* Write the string with a length byte preceeded (this is easier for
+ /* Write the string with the length preceeded (this is easier for
* the reading routine than the C format since the length is known in
* advance).
*/
- ObjWrite8 ((unsigned char) Len);
+ ObjWriteVar (Len);
ObjWriteData (S, Len);
}
void ObjWritePos (const FilePos* Pos)
/* Write a file position to the object file */
{
- /* Write the line number as 24 bit value to save one byte */
- ObjWrite24 (Pos->Line);
- ObjWrite8 (Pos->Col);
+ /* Write the data entries */
+ ObjWriteVar (Pos->Line);
+ ObjWriteVar (Pos->Col);
if (Pos->Name == 0) {
- /* Position is outside file scope, use the main file instead */
- ObjWrite8 (0);
+ /* Position is outside file scope, use the main file instead */
+ ObjWriteVar (0);
} else {
- ObjWrite8 (Pos->Name - 1);
- }
+ ObjWriteVar (Pos->Name - 1);
+ }
}
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
-#include "../common/filepos.h"
+/* common */
+#include "filepos.h"
void ObjWrite32 (unsigned long V);
/* Write a 32 bit value to the file */
+
+void ObjWriteVar (unsigned long V);
+/* Write a variable sized value to the file in special encoding */
void ObjWriteStr (const char* S);
/* Write a string to the object file */
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
-#include "../common/optdefs.h"
-#include "../common/xmalloc.h"
+/* common */
+#include "optdefs.h"
+#include "xmalloc.h"
+/* ca65 */
#include "error.h"
#include "objfile.h"
#include "options.h"
ObjStartOptions ();
/* Write the option count */
- ObjWrite16 (OptCount);
+ ObjWriteVar (OptCount);
/* Walk through the list and write the options */
O = OptRoot;
/* Skip the subkey */
NextTok ();
-
+
/* Parameters are separated by a comma */
ConsumeComma ();
/* Check the key and dispatch to a handler */
switch (Key) {
case 0: DbgInfoFile (); break;
- case 1:
+ case 1: DbgInfoLine (); break;
+ case 2: DbgInfoSym (); break;
default: ErrorSkip (ERR_SYNTAX); break;
}
}
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
/* Write the imports list to the object file */
{
SymEntry* S;
-
+
/* Tell the object file module that we're about to start the imports */
ObjStartImports ();
/* Write the import count to the list */
- ObjWrite16 (ImportCount);
+ ObjWriteVar (ImportCount);
/* Walk throught list and write all imports to the file */
S = SymList;
ObjStartExports ();
/* Write the export count to the list */
- ObjWrite16 (ExportCount);
+ ObjWriteVar (ExportCount);
/* Walk throught list and write all exports to the file */
S = SymList;
S = S->List;
}
- /* Safety check */
- if (Count > 0xFFFF) {
- Fatal (FAT_TOO_MANY_SYMBOLS);
- }
-
/* Write the symbol count to the list */
- ObjWrite16 (Count);
+ ObjWriteVar (Count);
/* Walk through list and write all symbols to the file */
S = SymList;
} else {
/* No debug symbols */
- ObjWrite16 (0);
+ ObjWriteVar (0);
}
-/* Size of position in file */
-#define POS_SIZE 5
-
/* Type of a file position */
typedef struct FilePos_ FilePos;
struct FilePos_ {
unsigned long Line; /* Line */
- unsigned char Col; /* Column */
- unsigned char Name; /* File */
+ unsigned Col; /* Column */
+ unsigned Name; /* File */
};
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#define FRAG_BYTEMASK 0x07 /* Mask for byte count */
#define FRAG_LITERAL 0x00 /* Literal data */
-#define FRAG_LITERAL8 0x01 /* Literal data with 8 bit length */
-#define FRAG_LITERAL16 0x02 /* Literal data with 16 bit length */
-#define FRAG_LITERAL24 0x03 /* Literal data with 24 bit length */
-#define FRAG_LITERAL32 0x04 /* Literal data with 32 bit length */
#define FRAG_EXPR 0x08 /* Expression */
#define FRAG_EXPR8 0x09 /* 8 bit expression */
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include "check.h"
#include "symdefs.h"
#include "xmalloc.h"
-
+
/* ld65 */
#include "global.h"
#include "error.h"
-static DbgSym* NewDbgSym (unsigned char Type, const char* Name, ObjData* O)
+static DbgSym* NewDbgSym (unsigned char Type, ObjData* O)
/* Create a new DbgSym and return it */
{
- /* Get the length of the symbol name */
- unsigned Len = strlen (Name);
-
/* Allocate memory */
- DbgSym* D = xmalloc (sizeof (DbgSym) + Len);
+ DbgSym* D = xmalloc (sizeof (DbgSym));
/* Initialize the fields */
D->Next = 0;
D->Obj = O;
D->Expr = 0;
D->Type = Type;
- memcpy (D->Name, Name, Len);
- D->Name [Len] = '\0';
+ D->Name = 0;
/* Return the new entry */
return D;
/* Read a debug symbol from a file, insert and return it */
{
unsigned char Type;
- char Name [256];
DbgSym* D;
/* Read the type */
Type = Read8 (F);
- /* Read the name */
- ReadStr (F, Name);
+ /* Create a new debug symbol */
+ D = NewDbgSym (Type, O);
- /* Create a new export */
- D = NewDbgSym (Type, Name, O);
+ /* Read and assign the name */
+ D->Name = ReadStr (F);
/* Read the value */
if (Type & EXP_EXPR) {
#include <stdio.h>
-#include "../common/exprdefs.h"
-#include "../common/filepos.h"
-
+/* common */
+#include "exprdefs.h"
+#include "filepos.h"
+
+/* ld65 */
#include "objdata.h"
FilePos Pos; /* File position of definition */
ExprNode* Expr; /* Expression (0 if not def'd) */
unsigned char Type; /* Type of symbol */
- char Name [1]; /* Name - dynamically allocated */
+ char* Name; /* Name - dynamically allocated */
};
+
#include "hashstr.h"
#include "symdefs.h"
#include "xmalloc.h"
-
+
/* ld65 */
#include "global.h"
#include "error.h"
I = NewImport (Type, Obj);
/* Read the name */
- I->V.Name = ReadMallocedStr (F);
+ I->V.Name = ReadStr (F);
/* Read the file position */
ReadFilePos (F, &I->Pos);
static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
/* Create a new export and initialize it */
{
- /* Get the length of the symbol name */
- unsigned Len = strlen (Name);
-
/* Allocate memory */
- Export* E = xmalloc (sizeof (Export) + Len);
+ Export* E = xmalloc (sizeof (Export));
/* Initialize the fields */
E->Next = 0;
- E->Flags = 0;
+ E->Flags = 0;
E->Obj = Obj;
E->ImpCount = 0;
E->ImpList = 0;
E->Expr = 0;
E->Type = Type;
- memcpy (E->Name, Name, Len);
- E->Name [Len] = '\0';
+ if (Name) {
+ E->Name = xstrdup (Name);
+ } else {
+ /* Name will get added later */
+ E->Name = 0;
+ }
/* Return the new entry */
return E;
if (Last) {
Last->Next = E;
} else {
- HashTab [HashVal] = E;
+ HashTab [HashVal] = E;
}
ImpOpen -= E->ImpCount; /* Decrease open imports now */
xfree (L);
/* Read an export from a file */
{
unsigned char Type;
- char Name [256];
Export* E;
/* Read the type */
Type = Read8 (F);
- /* Read the name */
- ReadStr (F, Name);
+ /* Create a new export without a name */
+ E = NewExport (Type, 0, O);
- /* Create a new export */
- E = NewExport (Type, Name, O);
+ /* Read the name */
+ E->Name = ReadStr (F);
/* Read the value */
if (Type & EXP_EXPR) {
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include <stdio.h>
-#include "../common/exprdefs.h"
-#include "../common/filepos.h"
-
+/* common */
+#include "exprdefs.h"
+#include "filepos.h"
+
+/* ld65 */
#include "objdata.h"
#include "config.h"
FilePos Pos; /* File position of definition */
ExprNode* Expr; /* Expression (0 if not def'd) */
unsigned char Type; /* Type of export */
- char Name [1]; /* Name - dynamically allocated */
+ char* Name; /* Name - dynamically allocated */
};
#include <string.h>
-#include "../common/xmalloc.h"
+/* common */
+#include "xmalloc.h"
+/* ld65 */
#include "error.h"
#include "fileio.h"
+void WriteVar (FILE* F, 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
+ * 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.
+ */
+ do {
+ unsigned char C = (V & 0x7F);
+ V >>= 7;
+ if (V) {
+ C |= 0x80;
+ }
+ Write8 (F, C);
+ } while (V != 0);
+}
+
+
+
void WriteStr (FILE* F, const char* S)
/* Write a string to the file */
{
unsigned Len = strlen (S);
- if (Len > 255) {
- Internal ("String too long");
- }
- Write8 (F, (unsigned char) Len);
+ WriteVar (F, Len);
WriteData (F, S, Len);
}
-char* ReadStr (FILE* F, char* Str)
-/* Read a string from the file. Str must hold 256 chars at max */
+unsigned long ReadVar (FILE* F)
+/* Read a variable size value from the file */
{
- /* Read the length byte */
- unsigned Len = Read8 (F);
-
- /* Read the string itself */
- ReadData (F, Str, Len);
-
- /* Terminate the string and return it */
- Str [Len] = '\0';
- return Str;
+ /* The value was written to the file in 7 bit chunks LSB first. If there
+ * are more bytes, bit 8 is set, otherwise it is clear.
+ */
+ unsigned char C;
+ unsigned long V = 0;
+ unsigned Shift = 0;
+ do {
+ /* Read one byte */
+ C = Read8 (F);
+ /* Encode it into the target value */
+ V |= ((unsigned long)(C & 0x7F)) << Shift;
+ /* Next value */
+ Shift += 7;
+ } while (C & 0x80);
+
+ /* Return the value read */
+ return V;
}
-char* ReadMallocedStr (FILE* F)
-/* Read a string from the file into a malloced area */
+char* ReadStr (FILE* F)
+/* Read a string from the file (the memory will be malloc'ed) */
{
- /* Read the length byte */
- unsigned Len = Read8 (F);
+ /* Read the length */
+ unsigned Len = ReadVar (F);
- /* Allocate memory */
- char* Str = xmalloc (Len + 1);
-
- /* Read the string itself */
- ReadData (F, Str, Len);
+ /* Allocate memory and read the string itself */
+ char* S = xmalloc (Len + 1);
+ ReadData (F, S, Len);
/* Terminate the string and return it */
- Str [Len] = '\0';
- return Str;
+ S [Len] = '\0';
+ return S;
}
FilePos* ReadFilePos (FILE* F, FilePos* Pos)
/* Read a file position from the file */
{
- /* The line number is encoded as 24 bit value to save some space */
- Pos->Line = Read24 (F);
- Pos->Col = Read8 (F);
- Pos->Name = Read8 (F);
+ /* Read the data fields */
+ Pos->Line = ReadVar (F);
+ Pos->Col = ReadVar (F);
+ Pos->Name = ReadVar (F);
return Pos;
}
-
+
#include <stdio.h>
-#include "../common/filepos.h"
+/* common */
+#include "filepos.h"
void WriteVal (FILE* F, unsigned long Val, unsigned Size);
/* Write a value of the given size to the output file */
+void WriteVar (FILE* F, unsigned long V);
+/* Write a variable sized value to the file in special encoding */
+
void WriteStr (FILE* F, const char* S);
/* Write a string to the file */
long Read32Signed (FILE* F);
/* Read a 32 bit value from the file. Sign extend the value. */
-char* ReadStr (FILE* F, char* Str);
-/* Read a string from the file. Str must hold 256 chars at max */
+unsigned long ReadVar (FILE* F);
+/* Read a variable size value from the file */
-char* ReadMallocedStr (FILE* F);
+char* ReadStr (FILE* F);
/* Read a string from the file into a malloced area */
FilePos* ReadFilePos (FILE* F, FilePos* Pos);
/* */
/* */
/* */
-/* (C) 1998 Ullrich von Bassewitz */
-/* Wacholderweg 14 */
-/* D-70597 Stuttgart */
-/* EMail: uz@musoftware.de */
+/* (C) 1998-2000 Ullrich von Bassewitz */
+/* Wacholderweg 14 */
+/* D-70597 Stuttgart */
+/* EMail: uz@musoftware.de */
/* */
/* */
/* This software is provided 'as-is', without any expressed or implied */
#include <string.h>
#include <errno.h>
-#include "../common/exprdefs.h"
-#include "../common/filepos.h"
-#include "../common/libdefs.h"
-#include "../common/objdefs.h"
-#include "../common/symdefs.h"
-#include "../common/xmalloc.h"
+/* common */
+#include "exprdefs.h"
+#include "filepos.h"
+#include "libdefs.h"
+#include "objdefs.h"
+#include "symdefs.h"
+#include "xmalloc.h"
+/* ld65 */
#include "error.h"
#include "exports.h"
#include "fileio.h"
/*****************************************************************************/
-/* Data */
+/* Data */
/*****************************************************************************/
ObjData* O = NewObjData ();
/* Module name/flags/MTime/Start/Size */
- O->Name = ReadMallocedStr (Lib);
+ O->Name = ReadStr (Lib);
O->Flags = Read16 (Lib);
Read32 (Lib); /* Skip MTime */
O->Start = Read32 (Lib);
/* Skip the export size, then read the exports */
Read16 (Lib);
- O->ExportCount = Read16 (Lib);
+ O->ExportCount = ReadVar (Lib);
O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
for (I = 0; I < O->ExportCount; ++I) {
O->Exports [I] = ReadExport (Lib, O);
/* Skip the import size, then read the imports */
Read16 (Lib);
- O->ImportCount = Read16 (Lib);
+ O->ImportCount = ReadVar (Lib);
O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
for (I = 0; I < O->ImportCount; ++I) {
O->Imports [I] = ReadImport (Lib, O);
-#include "../common/objdefs.h"
+/* common */
+#include "objdefs.h"
+
/* common */
#include "xmalloc.h"
-
+
/* ld65 */
#include "dbgsyms.h"
#include "error.h"
{
unsigned I;
- O->FileCount = Read16 (F);
+ O->FileCount = ReadVar (F);
O->Files = xmalloc (O->FileCount * sizeof (char*));
for (I = 0; I < O->FileCount; ++I) {
/* Skip MTime and size */
Read32 (F);
Read32 (F);
/* Read the filename */
- O->Files [I] = ReadMallocedStr (F);
+ O->Files [I] = ReadStr (F);
}
}
{
unsigned I;
- O->ImportCount = Read16 (F);
+ O->ImportCount = ReadVar (F);
O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
for (I = 0; I < O->ImportCount; ++I) {
O->Imports [I] = ReadImport (F, O);
{
unsigned I;
- O->ExportCount = Read16 (F);
+ O->ExportCount = ReadVar (F);
O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
for (I = 0; I < O->ExportCount; ++I) {
O->Exports [I] = ReadExport (F, O);
{
unsigned I;
- O->DbgSymCount = Read16 (F);
+ O->DbgSymCount = ReadVar (F);
O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (DbgSym*));
for (I = 0; I < O->DbgSymCount; ++I) {
O->DbgSyms [I] = ReadDbgSym (F, O);
{
unsigned I;
- O->SectionCount = Read8 (F);
+ O->SectionCount = ReadVar (F);
O->Sections = xmalloc (O->SectionCount * sizeof (Section*));
for (I = 0; I < O->SectionCount; ++I) {
O->Sections [I] = ReadSection (F, O);
+
/* Read a section from a file */
{
unsigned HashVal;
- char Name [256];
+ char* Name;
unsigned long Size;
unsigned char Align;
unsigned char Type;
Section* Sec;
/* Read the name */
- ReadStr (F, Name);
+ Name = ReadStr (F);
/* Read the size */
Size = Read32 (F);
/* Print some data */
if (Verbose > 1) {
printf ("Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n",
- O->Name, Name, Size, Align, Type);
+ O->Name, Name, Size, Align, Type);
}
/* Create a hash over the name and try to locate the segment in the table */
HashTab [HashVal] = S;
}
+ /* We have the segment and don't need the name any longer */
+ xfree (Name);
+
/* Allocate the section we will return later */
Sec = NewSection (S, Align, Type);
/* Handle the different fragment types */
switch (Type) {
- case FRAG_LITERAL8:
- Frag = NewFragment (FRAG_LITERAL, Read8 (F), Sec);
- break;
-
- case FRAG_LITERAL16:
- Frag = NewFragment (FRAG_LITERAL, Read16 (F), Sec);
- break;
-
- case FRAG_LITERAL24:
- Frag = NewFragment (FRAG_LITERAL, Read24 (F), Sec);
- break;
-
- case FRAG_LITERAL32:
- Frag = NewFragment (FRAG_LITERAL, Read32 (F), Sec);
+ case FRAG_LITERAL:
+ Frag = NewFragment (Type, ReadVar (F), Sec);
break;
case FRAG_EXPR8:
case FRAG_FILL:
/* Will allocate memory, but we don't care... */
- Frag = NewFragment (FRAG_FILL, Read16 (F), Sec);
+ Frag = NewFragment (Type, ReadVar (F), Sec);
break;
default:
/* Handle the different fragment types */
switch (Type) {
- case FRAG_LITERAL8:
- Size = Read8 (F);
- break;
-
- case FRAG_LITERAL16:
- Size = Read16 (F);
- break;
-
- case FRAG_LITERAL24:
- Size = Read24 (F);
- break;
-
- case FRAG_LITERAL32:
- Size = Read32 (F);
+ case FRAG_LITERAL:
+ Size = ReadVar (F);
break;
case FRAG_EXPR8:
break;
case FRAG_FILL:
- Size = Read16 (F);
+ Size = ReadVar (F);
break;
default:
printf (" Options:\n");
/* Read the number of options and print it */
- Count = Read16 (F);
+ Count = ReadVar (F);
printf (" Count:%27u\n", Count);
/* Read and print all options */
switch (ArgType) {
case OPT_ARGSTR:
- ArgStr = ReadMallocedStr (F);
+ ArgStr = ReadStr (F);
ArgLen = strlen (ArgStr);
printf (" Data:%*s\"%s\"\n", 24-ArgLen, "", ArgStr);
xfree (ArgStr);
printf (" Files:\n");
/* Read the number of files and print it */
- Count = Read16 (F);
+ Count = ReadVar (F);
printf (" Count:%27u\n", Count);
/* Read and print all files */
/* Read the data for one file */
unsigned long MTime = Read32 (F);
unsigned long Size = Read32 (F);
- char* Name = ReadMallocedStr (F);
+ char* Name = ReadStr (F);
unsigned Len = strlen (Name);
/* Print the header */
printf (" Segments:\n");
/* Read the number of segments and print it */
- Count = Read8 (F);
+ Count = ReadVar (F);
printf (" Count:%27u\n", Count);
/* Read and print all segments */
for (I = 0; I < Count; ++I) {
/* Read the data for one segments */
- char* Name = ReadMallocedStr (F);
+ char* Name = ReadStr (F);
unsigned Len = strlen (Name);
unsigned long Size = Read32 (F);
unsigned Align = (1U << Read8 (F));
printf (" Imports:\n");
/* Read the number of imports and print it */
- Count = Read16 (F);
+ Count = ReadVar (F);
printf (" Count:%27u\n", Count);
/* Read and print all imports */
/* Read the data for one import */
unsigned char Type = Read8 (F);
- char* Name = ReadMallocedStr (F);
+ char* Name = ReadStr (F);
unsigned Len = strlen (Name);
ReadFilePos (F, &Pos);
printf (" Exports:\n");
/* Read the number of exports and print it */
- Count = Read16 (F);
+ Count = ReadVar (F);
printf (" Count:%27u\n", Count);
/* Read and print all exports */
/* Read the data for one export */
unsigned char Type = Read8 (F);
- char* Name = ReadMallocedStr (F);
+ char* Name = ReadStr (F);
unsigned Len = strlen (Name);
if (Type & EXP_EXPR) {
SkipExpr (F);
}
/* Read the number of exports and print it */
- Count = Read16 (F);
+ Count = ReadVar (F);
printf (" Count:%27u\n", Count);
/* Read and print all debug symbols */
for (I = 0; I < Count; ++I) {
unsigned long Value = 0;
- int HaveValue;
+ int HaveValue;
const char* TypeDesc;
/* Read the data for one symbol */
unsigned char Type = Read8 (F);
- char* Name = ReadMallocedStr (F);
+ char* Name = ReadStr (F);
unsigned Len = strlen (Name);
if (Type & EXP_EXPR) {
SkipExpr (F);
+
int C = getc (F);
if (C == EOF) {
Error ("Read error (file corrupt?)");
- }
+ }
return C;
}
-char* ReadStr (FILE* F, char* Str)
-/* Read a string from the file. Str must hold 256 chars at max */
+unsigned long ReadVar (FILE* F)
+/* Read a variable size value from the file */
{
- /* Read the length byte */
- unsigned Len = Read8 (F);
-
- /* Read the string itself */
- ReadData (F, Str, Len);
-
- /* Terminate the string and return it */
- Str [Len] = '\0';
- return Str;
+ /* The value was written to the file in 7 bit chunks LSB first. If there
+ * are more bytes, bit 8 is set, otherwise it is clear.
+ */
+ unsigned char C;
+ unsigned long V = 0;
+ unsigned Shift = 0;
+ do {
+ /* Read one byte */
+ C = Read8 (F);
+ /* Encode it into the target value */
+ V |= ((unsigned long)(C & 0x7F)) << Shift;
+ /* Next value */
+ Shift += 7;
+ } while (C & 0x80);
+
+ /* Return the value read */
+ return V;
}
-char* ReadMallocedStr (FILE* F)
+char* ReadStr (FILE* F)
/* Read a string from the file into a malloced area */
{
- /* Read the length byte */
- unsigned Len = Read8 (F);
+ /* Read the length */
+ unsigned Len = ReadVar (F);
/* Allocate memory */
char* Str = xmalloc (Len + 1);
FilePos* ReadFilePos (FILE* F, FilePos* Pos)
/* Read a file position from the file */
{
- /* The line number is encoded as 24 bit value to save some space */
- Pos->Line = Read24 (F);
- Pos->Col = Read8 (F);
- Pos->Name = Read8 (F);
+ /* Read the data fields */
+ Pos->Line = ReadVar (F);
+ Pos->Col = ReadVar (F);
+ Pos->Name = ReadVar (F);
return Pos;
}
long Read32Signed (FILE* F);
/* Read a 32 bit value from the file. Sign extend the value. */
-char* ReadStr (FILE* F, char* Str);
-/* Read a string from the file. Str must hold 256 chars at max */
+unsigned long ReadVar (FILE* F);
+/* Read a variable size value from the file */
-char* ReadMallocedStr (FILE* F);
+char* ReadStr (FILE* F);
/* Read a string from the file into a malloced area */
FilePos* ReadFilePos (FILE* F, FilePos* Pos);