Added a method to write variable sized unsigned values. Use this method for

all sorts of things in the object files. This does not only make the object
files smaller, but does also remove several limits (strings may be longer
than 255 bytes, several counters no longer have 8 or 16 bit limits).


git-svn-id: svn://svn.cc65.org/cc65/trunk@260 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2000-08-02 13:23:06 +00:00
parent 85d8b3badf
commit 097a01094e
29 changed files with 423 additions and 295 deletions

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -35,9 +35,11 @@
#include <string.h> #include <string.h>
#include "../common/hashstr.h" /* common */
#include "../common/xmalloc.h" #include "hashstr.h"
#include "xmalloc.h"
/* ar65 */
#include "error.h" #include "error.h"
#include "objdata.h" #include "objdata.h"
#include "exports.h" #include "exports.h"
@@ -112,7 +114,7 @@ void ExpInsert (const char* Name, unsigned Module)
/* Duplicate entry */ /* Duplicate entry */
Warning ("External symbol `%s' in module `%s' is duplicated in " Warning ("External symbol `%s' in module `%s' is duplicated in "
"module `%s'", "module `%s'",
Name, GetObjName (L->Module), GetObjName (Module)); Name, GetObjName (L->Module), GetObjName (Module));
} }
if (L->Next == 0) { if (L->Next == 0) {
break; break;
@@ -147,4 +149,4 @@ int ExpFind (const char* Name)

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -35,8 +35,10 @@
#include <string.h> #include <string.h>
#include "../common/xmalloc.h" /* common */
#include "xmalloc.h"
/* ar65 */
#include "error.h" #include "error.h"
#include "fileio.h" #include "fileio.h"
@@ -78,14 +80,31 @@ void Write32 (FILE* F, unsigned long Val)
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) void WriteStr (FILE* F, const char* S)
/* Write a string to the file */ /* Write a string to the file */
{ {
unsigned Len = strlen (S); unsigned Len = strlen (S);
if (Len > 255) { WriteVar (F, Len);
Internal ("String too long");
}
Write8 (F, (unsigned char) Len);
WriteData (F, S, Len); WriteData (F, S, Len);
} }
@@ -133,11 +152,35 @@ unsigned long Read32 (FILE* F)
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) char* ReadStr (FILE* F)
/* Read a string from the file (the memory will be malloc'ed) */ /* Read a string from the file (the memory will be malloc'ed) */
{ {
/* Read the length byte */ /* Read the length */
unsigned Len = Read8 (F); unsigned Len = ReadVar (F);
/* Allocate memory and read the string itself */ /* Allocate memory and read the string itself */
char* S = xmalloc (Len + 1); char* S = xmalloc (Len + 1);

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -57,6 +57,9 @@ void Write16 (FILE* F, unsigned Val);
void Write32 (FILE* F, unsigned long Val); void Write32 (FILE* F, unsigned long Val);
/* Write a 32 bit value to the file */ /* 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); void WriteStr (FILE* F, const char* S);
/* Write a string to the file */ /* Write a string to the file */
@@ -72,6 +75,9 @@ unsigned Read16 (FILE* F);
unsigned long Read32 (FILE* F); unsigned long Read32 (FILE* F);
/* Read a 32 bit value from the file */ /* 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); char* ReadStr (FILE* F);
/* Read a string from the file (the memory will be malloc'ed) */ /* Read a string from the file (the memory will be malloc'ed) */

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -37,13 +37,15 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "../common/bitops.h" /* common */
#include "../common/exprdefs.h" #include "bitops.h"
#include "../common/filepos.h" #include "exprdefs.h"
#include "../common/libdefs.h" #include "filepos.h"
#include "../common/symdefs.h" #include "libdefs.h"
#include "../common/xmalloc.h" #include "symdefs.h"
#include "xmalloc.h"
/* ar65 */
#include "error.h" #include "error.h"
#include "global.h" #include "global.h"
#include "fileio.h" #include "fileio.h"
@@ -303,6 +305,28 @@ void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
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) static void SkipExpr (unsigned char** Buf)
/* Skip an expression in Buf */ /* Skip an expression in Buf */
{ {
@@ -339,20 +363,26 @@ static void SkipExpr (unsigned char** 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) static void LibCheckExports (ObjData* O)
/* Insert all exports from the given object file into the global list /* Insert all exports from the given object file into the global list
* checking for duplicates. * checking for duplicates.
*/ */
{ {
char Name [256];
/* Get a pointer to the buffer */ /* Get a pointer to the buffer */
unsigned char* Exports = O->Exports; unsigned char* Exports = O->Exports;
/* First two bytes are export count */ /* Get the export count */
unsigned Lo = *Exports++; unsigned Count = GetVar (&Exports);
unsigned Hi = *Exports++;
unsigned Count = (Hi << 8) + Lo;
/* Read the exports */ /* Read the exports */
if (Verbose > 1) { if (Verbose > 1) {
@@ -360,13 +390,12 @@ static void LibCheckExports (ObjData* O)
} }
while (Count--) { while (Count--) {
unsigned char Len;
/* Get the export tag */ /* Get the export tag */
unsigned char Tag = *Exports++; unsigned char Tag = *Exports++;
/* Next thing is name of symbol */ /* Next thing is name of symbol */
Len = *Exports++; unsigned Len = GetVar (&Exports);
char* Name = xmalloc (Len + 1);
memcpy (Name, Exports, Len); memcpy (Name, Exports, Len);
Name [Len] = '\0'; Name [Len] = '\0';
Exports += Len; Exports += Len;
@@ -381,13 +410,16 @@ static void LibCheckExports (ObjData* O)
} }
/* Skip the position */ /* Skip the position */
Exports += POS_SIZE; SkipFilePos (&Exports);
/* Insert the name into the hash table */ /* Insert the name into the hash table */
if (Verbose > 1) { if (Verbose > 1) {
printf (" %s\n", Name); printf (" %s\n", Name);
} }
ExpInsert (Name, O->Index); ExpInsert (Name, O->Index);
/* Free the name */
xfree (Name);
} }
} }

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -38,7 +38,7 @@
/* common */ /* common */
#include "check.h" #include "check.h"
#include "xmalloc.h" #include "xmalloc.h"
/* ar65 */ /* ar65 */
#include "error.h" #include "error.h"
#include "objdata.h" #include "objdata.h"
@@ -189,7 +189,7 @@ void MakeObjPool (void)
/* Set the pool pointer */ /* Set the pool pointer */
ObjPool [Index] = O; ObjPool [Index] = O;
/* Next object */ /* Next object */
++Index; ++Index;
O = O->Next; O = O->Next;

View File

@@ -81,3 +81,19 @@ void DbgInfoFile (void)
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);
}

View File

@@ -45,7 +45,13 @@
void DbgInfoFile (void); 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 */
@@ -56,4 +62,4 @@ void DbgInfoFile (void);

View File

@@ -119,7 +119,7 @@ void WriteFiles (void)
ObjStartFiles (); ObjStartFiles ();
/* Write the file count */ /* Write the file count */
ObjWrite16 (FileCount); ObjWriteVar (FileCount);
/* Write the file data */ /* Write the file data */
for (I = 0; I < FileCount; ++I) { for (I = 0; I < FileCount; ++I) {

View File

@@ -486,19 +486,8 @@ static void WriteOneSeg (Segment* Seg)
Size += F->Len; Size += F->Len;
F = F->Next; F = F->Next;
} }
if (Size < 0x100) { ObjWrite8 (FRAG_LITERAL);
ObjWrite8 (FRAG_LITERAL8); ObjWriteVar (Size);
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);
}
/* Now write the literal data */ /* Now write the literal data */
F = Frag; F = Frag;
@@ -533,7 +522,7 @@ static void WriteOneSeg (Segment* Seg)
case FRAG_FILL: case FRAG_FILL:
ObjWrite8 (FRAG_FILL); ObjWrite8 (FRAG_FILL);
ObjWrite16 (Frag->Len); ObjWriteVar (Frag->Len);
break; break;
default: default:
@@ -560,7 +549,7 @@ void WriteSegments (void)
ObjStartSegments (); ObjStartSegments ();
/* First thing is segment count */ /* First thing is segment count */
ObjWrite8 (SegmentCount); ObjWriteVar (SegmentCount);
/* Now walk through all segments and write them to the object file */ /* Now walk through all segments and write them to the object file */
Seg = SegmentList; Seg = SegmentList;
@@ -578,7 +567,7 @@ void WriteSegments (void)
/*****************************************************************************/ /*****************************************************************************/
/* Code */ /* Code */
/*****************************************************************************/ /*****************************************************************************/

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -41,7 +41,7 @@
/* common */ /* common */
#include "fname.h" #include "fname.h"
#include "objdefs.h" #include "objdefs.h"
/* ca65 */ /* ca65 */
#include "global.h" #include "global.h"
#include "error.h" #include "error.h"
@@ -210,19 +210,36 @@ void ObjWrite32 (unsigned long V)
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) void ObjWriteStr (const char* S)
/* Write a string to the object file */ /* Write a string to the object file */
{ {
unsigned Len = strlen (S); 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 * the reading routine than the C format since the length is known in
* advance). * advance).
*/ */
ObjWrite8 ((unsigned char) Len); ObjWriteVar (Len);
ObjWriteData (S, Len); ObjWriteData (S, Len);
} }
@@ -241,15 +258,15 @@ void ObjWriteData (const void* Data, unsigned Size)
void ObjWritePos (const FilePos* Pos) void ObjWritePos (const FilePos* Pos)
/* Write a file position to the object file */ /* Write a file position to the object file */
{ {
/* Write the line number as 24 bit value to save one byte */ /* Write the data entries */
ObjWrite24 (Pos->Line); ObjWriteVar (Pos->Line);
ObjWrite8 (Pos->Col); ObjWriteVar (Pos->Col);
if (Pos->Name == 0) { if (Pos->Name == 0) {
/* Position is outside file scope, use the main file instead */ /* Position is outside file scope, use the main file instead */
ObjWrite8 (0); ObjWriteVar (0);
} else { } else {
ObjWrite8 (Pos->Name - 1); ObjWriteVar (Pos->Name - 1);
} }
} }

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -38,7 +38,8 @@
#include "../common/filepos.h" /* common */
#include "filepos.h"
@@ -65,6 +66,9 @@ void ObjWrite24 (unsigned long V);
void ObjWrite32 (unsigned long V); void ObjWrite32 (unsigned long V);
/* Write a 32 bit value to the file */ /* 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); void ObjWriteStr (const char* S);
/* Write a string to the object file */ /* Write a string to the object file */

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -33,9 +33,11 @@
#include "../common/optdefs.h" /* common */
#include "../common/xmalloc.h" #include "optdefs.h"
#include "xmalloc.h"
/* ca65 */
#include "error.h" #include "error.h"
#include "objfile.h" #include "objfile.h"
#include "options.h" #include "options.h"
@@ -164,7 +166,7 @@ void WriteOptions (void)
ObjStartOptions (); ObjStartOptions ();
/* Write the option count */ /* Write the option count */
ObjWrite16 (OptCount); ObjWriteVar (OptCount);
/* Walk through the list and write the options */ /* Walk through the list and write the options */
O = OptRoot; O = OptRoot;

View File

@@ -370,14 +370,15 @@ static void DoDbg (void)
/* Skip the subkey */ /* Skip the subkey */
NextTok (); NextTok ();
/* Parameters are separated by a comma */ /* Parameters are separated by a comma */
ConsumeComma (); ConsumeComma ();
/* Check the key and dispatch to a handler */ /* Check the key and dispatch to a handler */
switch (Key) { switch (Key) {
case 0: DbgInfoFile (); break; case 0: DbgInfoFile (); break;
case 1: case 1: DbgInfoLine (); break;
case 2: DbgInfoSym (); break;
default: ErrorSkip (ERR_SYNTAX); break; default: ErrorSkip (ERR_SYNTAX); break;
} }
} }

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -978,12 +978,12 @@ void WriteImports (void)
/* Write the imports list to the object file */ /* Write the imports list to the object file */
{ {
SymEntry* S; SymEntry* S;
/* Tell the object file module that we're about to start the imports */ /* Tell the object file module that we're about to start the imports */
ObjStartImports (); ObjStartImports ();
/* Write the import count to the list */ /* Write the import count to the list */
ObjWrite16 (ImportCount); ObjWriteVar (ImportCount);
/* Walk throught list and write all imports to the file */ /* Walk throught list and write all imports to the file */
S = SymList; S = SymList;
@@ -1015,7 +1015,7 @@ void WriteExports (void)
ObjStartExports (); ObjStartExports ();
/* Write the export count to the list */ /* Write the export count to the list */
ObjWrite16 (ExportCount); ObjWriteVar (ExportCount);
/* Walk throught list and write all exports to the file */ /* Walk throught list and write all exports to the file */
S = SymList; S = SymList;
@@ -1076,13 +1076,8 @@ void WriteDbgSyms (void)
S = S->List; S = S->List;
} }
/* Safety check */
if (Count > 0xFFFF) {
Fatal (FAT_TOO_MANY_SYMBOLS);
}
/* Write the symbol count to the list */ /* Write the symbol count to the list */
ObjWrite16 (Count); ObjWriteVar (Count);
/* Walk through list and write all symbols to the file */ /* Walk through list and write all symbols to the file */
S = SymList; S = SymList;
@@ -1118,7 +1113,7 @@ void WriteDbgSyms (void)
} else { } else {
/* No debug symbols */ /* No debug symbols */
ObjWrite16 (0); ObjWriteVar (0);
} }

View File

@@ -44,15 +44,12 @@
/* Size of position in file */
#define POS_SIZE 5
/* Type of a file position */ /* Type of a file position */
typedef struct FilePos_ FilePos; typedef struct FilePos_ FilePos;
struct FilePos_ { struct FilePos_ {
unsigned long Line; /* Line */ unsigned long Line; /* Line */
unsigned char Col; /* Column */ unsigned Col; /* Column */
unsigned char Name; /* File */ unsigned Name; /* File */
}; };

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -55,10 +55,6 @@
#define FRAG_BYTEMASK 0x07 /* Mask for byte count */ #define FRAG_BYTEMASK 0x07 /* Mask for byte count */
#define FRAG_LITERAL 0x00 /* Literal data */ #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_EXPR 0x08 /* Expression */
#define FRAG_EXPR8 0x09 /* 8 bit expression */ #define FRAG_EXPR8 0x09 /* 8 bit expression */

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -39,7 +39,7 @@
#include "check.h" #include "check.h"
#include "symdefs.h" #include "symdefs.h"
#include "xmalloc.h" #include "xmalloc.h"
/* ld65 */ /* ld65 */
#include "global.h" #include "global.h"
#include "error.h" #include "error.h"
@@ -69,14 +69,11 @@ static DbgSym* DbgSymPool [256];
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 */ /* Create a new DbgSym and return it */
{ {
/* Get the length of the symbol name */
unsigned Len = strlen (Name);
/* Allocate memory */ /* Allocate memory */
DbgSym* D = xmalloc (sizeof (DbgSym) + Len); DbgSym* D = xmalloc (sizeof (DbgSym));
/* Initialize the fields */ /* Initialize the fields */
D->Next = 0; D->Next = 0;
@@ -84,8 +81,7 @@ static DbgSym* NewDbgSym (unsigned char Type, const char* Name, ObjData* O)
D->Obj = O; D->Obj = O;
D->Expr = 0; D->Expr = 0;
D->Type = Type; D->Type = Type;
memcpy (D->Name, Name, Len); D->Name = 0;
D->Name [Len] = '\0';
/* Return the new entry */ /* Return the new entry */
return D; return D;
@@ -143,17 +139,16 @@ DbgSym* ReadDbgSym (FILE* F, ObjData* O)
/* Read a debug symbol from a file, insert and return it */ /* Read a debug symbol from a file, insert and return it */
{ {
unsigned char Type; unsigned char Type;
char Name [256];
DbgSym* D; DbgSym* D;
/* Read the type */ /* Read the type */
Type = Read8 (F); Type = Read8 (F);
/* Read the name */ /* Create a new debug symbol */
ReadStr (F, Name); D = NewDbgSym (Type, O);
/* Create a new export */ /* Read and assign the name */
D = NewDbgSym (Type, Name, O); D->Name = ReadStr (F);
/* Read the value */ /* Read the value */
if (Type & EXP_EXPR) { if (Type & EXP_EXPR) {

View File

@@ -40,9 +40,11 @@
#include <stdio.h> #include <stdio.h>
#include "../common/exprdefs.h" /* common */
#include "../common/filepos.h" #include "exprdefs.h"
#include "filepos.h"
/* ld65 */
#include "objdata.h" #include "objdata.h"
@@ -62,7 +64,7 @@ struct DbgSym_ {
FilePos Pos; /* File position of definition */ FilePos Pos; /* File position of definition */
ExprNode* Expr; /* Expression (0 if not def'd) */ ExprNode* Expr; /* Expression (0 if not def'd) */
unsigned char Type; /* Type of symbol */ unsigned char Type; /* Type of symbol */
char Name [1]; /* Name - dynamically allocated */ char* Name; /* Name - dynamically allocated */
}; };
@@ -90,3 +92,4 @@ void PrintDbgSymLabels (ObjData* O, FILE* F);

View File

@@ -42,7 +42,7 @@
#include "hashstr.h" #include "hashstr.h"
#include "symdefs.h" #include "symdefs.h"
#include "xmalloc.h" #include "xmalloc.h"
/* ld65 */ /* ld65 */
#include "global.h" #include "global.h"
#include "error.h" #include "error.h"
@@ -175,7 +175,7 @@ Import* ReadImport (FILE* F, ObjData* Obj)
I = NewImport (Type, Obj); I = NewImport (Type, Obj);
/* Read the name */ /* Read the name */
I->V.Name = ReadMallocedStr (F); I->V.Name = ReadStr (F);
/* Read the file position */ /* Read the file position */
ReadFilePos (F, &I->Pos); ReadFilePos (F, &I->Pos);
@@ -195,22 +195,23 @@ Import* ReadImport (FILE* F, ObjData* Obj)
static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj) static Export* NewExport (unsigned char Type, const char* Name, ObjData* Obj)
/* Create a new export and initialize it */ /* Create a new export and initialize it */
{ {
/* Get the length of the symbol name */
unsigned Len = strlen (Name);
/* Allocate memory */ /* Allocate memory */
Export* E = xmalloc (sizeof (Export) + Len); Export* E = xmalloc (sizeof (Export));
/* Initialize the fields */ /* Initialize the fields */
E->Next = 0; E->Next = 0;
E->Flags = 0; E->Flags = 0;
E->Obj = Obj; E->Obj = Obj;
E->ImpCount = 0; E->ImpCount = 0;
E->ImpList = 0; E->ImpList = 0;
E->Expr = 0; E->Expr = 0;
E->Type = Type; E->Type = Type;
memcpy (E->Name, Name, Len); if (Name) {
E->Name [Len] = '\0'; E->Name = xstrdup (Name);
} else {
/* Name will get added later */
E->Name = 0;
}
/* Return the new entry */ /* Return the new entry */
return E; return E;
@@ -250,7 +251,7 @@ void InsertExport (Export* E)
if (Last) { if (Last) {
Last->Next = E; Last->Next = E;
} else { } else {
HashTab [HashVal] = E; HashTab [HashVal] = E;
} }
ImpOpen -= E->ImpCount; /* Decrease open imports now */ ImpOpen -= E->ImpCount; /* Decrease open imports now */
xfree (L); xfree (L);
@@ -285,17 +286,16 @@ Export* ReadExport (FILE* F, ObjData* O)
/* Read an export from a file */ /* Read an export from a file */
{ {
unsigned char Type; unsigned char Type;
char Name [256];
Export* E; Export* E;
/* Read the type */ /* Read the type */
Type = Read8 (F); Type = Read8 (F);
/* Read the name */ /* Create a new export without a name */
ReadStr (F, Name); E = NewExport (Type, 0, O);
/* Create a new export */ /* Read the name */
E = NewExport (Type, Name, O); E->Name = ReadStr (F);
/* Read the value */ /* Read the value */
if (Type & EXP_EXPR) { if (Type & EXP_EXPR) {

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -40,9 +40,11 @@
#include <stdio.h> #include <stdio.h>
#include "../common/exprdefs.h" /* common */
#include "../common/filepos.h" #include "exprdefs.h"
#include "filepos.h"
/* ld65 */
#include "objdata.h" #include "objdata.h"
#include "config.h" #include "config.h"
@@ -80,7 +82,7 @@ struct Export_ {
FilePos Pos; /* File position of definition */ FilePos Pos; /* File position of definition */
ExprNode* Expr; /* Expression (0 if not def'd) */ ExprNode* Expr; /* Expression (0 if not def'd) */
unsigned char Type; /* Type of export */ unsigned char Type; /* Type of export */
char Name [1]; /* Name - dynamically allocated */ char* Name; /* Name - dynamically allocated */
}; };

View File

@@ -35,8 +35,10 @@
#include <string.h> #include <string.h>
#include "../common/xmalloc.h" /* common */
#include "xmalloc.h"
/* ld65 */
#include "error.h" #include "error.h"
#include "fileio.h" #include "fileio.h"
@@ -117,14 +119,31 @@ void WriteVal (FILE* F, unsigned long Val, unsigned Size)
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) void WriteStr (FILE* F, const char* S)
/* Write a string to the file */ /* Write a string to the file */
{ {
unsigned Len = strlen (S); unsigned Len = strlen (S);
if (Len > 255) { WriteVar (F, Len);
Internal ("String too long");
}
Write8 (F, (unsigned char) Len);
WriteData (F, S, Len); WriteData (F, S, Len);
} }
@@ -210,37 +229,43 @@ long Read32Signed (FILE* F)
char* ReadStr (FILE* F, char* Str) unsigned long ReadVar (FILE* F)
/* Read a string from the file. Str must hold 256 chars at max */ /* Read a variable size value from the file */
{ {
/* Read the length byte */ /* The value was written to the file in 7 bit chunks LSB first. If there
unsigned Len = Read8 (F); * 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);
/* Read the string itself */ /* Return the value read */
ReadData (F, Str, Len); return V;
/* Terminate the string and return it */
Str [Len] = '\0';
return Str;
} }
char* ReadMallocedStr (FILE* F) char* ReadStr (FILE* F)
/* Read a string from the file into a malloced area */ /* Read a string from the file (the memory will be malloc'ed) */
{ {
/* Read the length byte */ /* Read the length */
unsigned Len = Read8 (F); unsigned Len = ReadVar (F);
/* Allocate memory */ /* Allocate memory and read the string itself */
char* Str = xmalloc (Len + 1); char* S = xmalloc (Len + 1);
ReadData (F, S, Len);
/* Read the string itself */
ReadData (F, Str, Len);
/* Terminate the string and return it */ /* Terminate the string and return it */
Str [Len] = '\0'; S [Len] = '\0';
return Str; return S;
} }
@@ -248,10 +273,10 @@ char* ReadMallocedStr (FILE* F)
FilePos* ReadFilePos (FILE* F, FilePos* Pos) FilePos* ReadFilePos (FILE* F, FilePos* Pos)
/* Read a file position from the file */ /* Read a file position from the file */
{ {
/* The line number is encoded as 24 bit value to save some space */ /* Read the data fields */
Pos->Line = Read24 (F); Pos->Line = ReadVar (F);
Pos->Col = Read8 (F); Pos->Col = ReadVar (F);
Pos->Name = Read8 (F); Pos->Name = ReadVar (F);
return Pos; return Pos;
} }
@@ -268,4 +293,4 @@ void* ReadData (FILE* F, void* Data, unsigned Size)

View File

@@ -40,7 +40,8 @@
#include <stdio.h> #include <stdio.h>
#include "../common/filepos.h" /* common */
#include "filepos.h"
@@ -65,6 +66,9 @@ void Write32 (FILE* F, unsigned long Val);
void WriteVal (FILE* F, unsigned long Val, unsigned Size); void WriteVal (FILE* F, unsigned long Val, unsigned Size);
/* Write a value of the given size to the output file */ /* 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); void WriteStr (FILE* F, const char* S);
/* Write a string to the file */ /* Write a string to the file */
@@ -89,10 +93,10 @@ unsigned long Read32 (FILE* F);
long Read32Signed (FILE* F); long Read32Signed (FILE* F);
/* Read a 32 bit value from the file. Sign extend the value. */ /* Read a 32 bit value from the file. Sign extend the value. */
char* ReadStr (FILE* F, char* Str); unsigned long ReadVar (FILE* F);
/* Read a string from the file. Str must hold 256 chars at max */ /* 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 */ /* Read a string from the file into a malloced area */
FilePos* ReadFilePos (FILE* F, FilePos* Pos); FilePos* ReadFilePos (FILE* F, FilePos* Pos);

View File

@@ -6,10 +6,10 @@
/* */ /* */
/* */ /* */
/* */ /* */
/* (C) 1998 Ullrich von Bassewitz */ /* (C) 1998-2000 Ullrich von Bassewitz */
/* Wacholderweg 14 */ /* Wacholderweg 14 */
/* D-70597 Stuttgart */ /* D-70597 Stuttgart */
/* EMail: uz@musoftware.de */ /* EMail: uz@musoftware.de */
/* */ /* */
/* */ /* */
/* This software is provided 'as-is', without any expressed or implied */ /* This software is provided 'as-is', without any expressed or implied */
@@ -37,13 +37,15 @@
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include "../common/exprdefs.h" /* common */
#include "../common/filepos.h" #include "exprdefs.h"
#include "../common/libdefs.h" #include "filepos.h"
#include "../common/objdefs.h" #include "libdefs.h"
#include "../common/symdefs.h" #include "objdefs.h"
#include "../common/xmalloc.h" #include "symdefs.h"
#include "xmalloc.h"
/* ld65 */
#include "error.h" #include "error.h"
#include "exports.h" #include "exports.h"
#include "fileio.h" #include "fileio.h"
@@ -54,7 +56,7 @@
/*****************************************************************************/ /*****************************************************************************/
/* Data */ /* Data */
/*****************************************************************************/ /*****************************************************************************/
@@ -111,7 +113,7 @@ static ObjData* ReadIndexEntry (void)
ObjData* O = NewObjData (); ObjData* O = NewObjData ();
/* Module name/flags/MTime/Start/Size */ /* Module name/flags/MTime/Start/Size */
O->Name = ReadMallocedStr (Lib); O->Name = ReadStr (Lib);
O->Flags = Read16 (Lib); O->Flags = Read16 (Lib);
Read32 (Lib); /* Skip MTime */ Read32 (Lib); /* Skip MTime */
O->Start = Read32 (Lib); O->Start = Read32 (Lib);
@@ -119,7 +121,7 @@ static ObjData* ReadIndexEntry (void)
/* Skip the export size, then read the exports */ /* Skip the export size, then read the exports */
Read16 (Lib); Read16 (Lib);
O->ExportCount = Read16 (Lib); O->ExportCount = ReadVar (Lib);
O->Exports = xmalloc (O->ExportCount * sizeof (Export*)); O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
for (I = 0; I < O->ExportCount; ++I) { for (I = 0; I < O->ExportCount; ++I) {
O->Exports [I] = ReadExport (Lib, O); O->Exports [I] = ReadExport (Lib, O);
@@ -127,7 +129,7 @@ static ObjData* ReadIndexEntry (void)
/* Skip the import size, then read the imports */ /* Skip the import size, then read the imports */
Read16 (Lib); Read16 (Lib);
O->ImportCount = Read16 (Lib); O->ImportCount = ReadVar (Lib);
O->Imports = xmalloc (O->ImportCount * sizeof (Import*)); O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
for (I = 0; I < O->ImportCount; ++I) { for (I = 0; I < O->ImportCount; ++I) {
O->Imports [I] = ReadImport (Lib, O); O->Imports [I] = ReadImport (Lib, O);

View File

@@ -38,7 +38,8 @@
#include "../common/objdefs.h" /* common */
#include "objdefs.h"
@@ -104,3 +105,4 @@ void FreeObjData (ObjData* O);

View File

@@ -40,7 +40,7 @@
/* common */ /* common */
#include "xmalloc.h" #include "xmalloc.h"
/* ld65 */ /* ld65 */
#include "dbgsyms.h" #include "dbgsyms.h"
#include "error.h" #include "error.h"
@@ -107,14 +107,14 @@ void ObjReadFiles (FILE* F, ObjData* O)
{ {
unsigned I; unsigned I;
O->FileCount = Read16 (F); O->FileCount = ReadVar (F);
O->Files = xmalloc (O->FileCount * sizeof (char*)); O->Files = xmalloc (O->FileCount * sizeof (char*));
for (I = 0; I < O->FileCount; ++I) { for (I = 0; I < O->FileCount; ++I) {
/* Skip MTime and size */ /* Skip MTime and size */
Read32 (F); Read32 (F);
Read32 (F); Read32 (F);
/* Read the filename */ /* Read the filename */
O->Files [I] = ReadMallocedStr (F); O->Files [I] = ReadStr (F);
} }
} }
@@ -125,7 +125,7 @@ void ObjReadImports (FILE* F, ObjData* O)
{ {
unsigned I; unsigned I;
O->ImportCount = Read16 (F); O->ImportCount = ReadVar (F);
O->Imports = xmalloc (O->ImportCount * sizeof (Import*)); O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
for (I = 0; I < O->ImportCount; ++I) { for (I = 0; I < O->ImportCount; ++I) {
O->Imports [I] = ReadImport (F, O); O->Imports [I] = ReadImport (F, O);
@@ -140,7 +140,7 @@ void ObjReadExports (FILE* F, ObjData* O)
{ {
unsigned I; unsigned I;
O->ExportCount = Read16 (F); O->ExportCount = ReadVar (F);
O->Exports = xmalloc (O->ExportCount * sizeof (Export*)); O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
for (I = 0; I < O->ExportCount; ++I) { for (I = 0; I < O->ExportCount; ++I) {
O->Exports [I] = ReadExport (F, O); O->Exports [I] = ReadExport (F, O);
@@ -155,7 +155,7 @@ void ObjReadDbgSyms (FILE* F, ObjData* O)
{ {
unsigned I; unsigned I;
O->DbgSymCount = Read16 (F); O->DbgSymCount = ReadVar (F);
O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (DbgSym*)); O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (DbgSym*));
for (I = 0; I < O->DbgSymCount; ++I) { for (I = 0; I < O->DbgSymCount; ++I) {
O->DbgSyms [I] = ReadDbgSym (F, O); O->DbgSyms [I] = ReadDbgSym (F, O);
@@ -169,7 +169,7 @@ void ObjReadSections (FILE* F, ObjData* O)
{ {
unsigned I; unsigned I;
O->SectionCount = Read8 (F); O->SectionCount = ReadVar (F);
O->Sections = xmalloc (O->SectionCount * sizeof (Section*)); O->Sections = xmalloc (O->SectionCount * sizeof (Section*));
for (I = 0; I < O->SectionCount; ++I) { for (I = 0; I < O->SectionCount; ++I) {
O->Sections [I] = ReadSection (F, O); O->Sections [I] = ReadSection (F, O);
@@ -226,3 +226,4 @@ void ObjAdd (FILE* Obj, const char* Name)

View File

@@ -215,7 +215,7 @@ Section* ReadSection (FILE* F, ObjData* O)
/* Read a section from a file */ /* Read a section from a file */
{ {
unsigned HashVal; unsigned HashVal;
char Name [256]; char* Name;
unsigned long Size; unsigned long Size;
unsigned char Align; unsigned char Align;
unsigned char Type; unsigned char Type;
@@ -223,7 +223,7 @@ Section* ReadSection (FILE* F, ObjData* O)
Section* Sec; Section* Sec;
/* Read the name */ /* Read the name */
ReadStr (F, Name); Name = ReadStr (F);
/* Read the size */ /* Read the size */
Size = Read32 (F); Size = Read32 (F);
@@ -237,7 +237,7 @@ Section* ReadSection (FILE* F, ObjData* O)
/* Print some data */ /* Print some data */
if (Verbose > 1) { if (Verbose > 1) {
printf ("Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n", 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 */ /* Create a hash over the name and try to locate the segment in the table */
@@ -254,6 +254,9 @@ Section* ReadSection (FILE* F, ObjData* O)
HashTab [HashVal] = S; HashTab [HashVal] = S;
} }
/* We have the segment and don't need the name any longer */
xfree (Name);
/* Allocate the section we will return later */ /* Allocate the section we will return later */
Sec = NewSection (S, Align, Type); Sec = NewSection (S, Align, Type);
@@ -281,20 +284,8 @@ Section* ReadSection (FILE* F, ObjData* O)
/* Handle the different fragment types */ /* Handle the different fragment types */
switch (Type) { switch (Type) {
case FRAG_LITERAL8: case FRAG_LITERAL:
Frag = NewFragment (FRAG_LITERAL, Read8 (F), Sec); Frag = NewFragment (Type, ReadVar (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);
break; break;
case FRAG_EXPR8: case FRAG_EXPR8:
@@ -310,7 +301,7 @@ Section* ReadSection (FILE* F, ObjData* O)
case FRAG_FILL: case FRAG_FILL:
/* Will allocate memory, but we don't care... */ /* Will allocate memory, but we don't care... */
Frag = NewFragment (FRAG_FILL, Read16 (F), Sec); Frag = NewFragment (Type, ReadVar (F), Sec);
break; break;
default: default:

View File

@@ -143,20 +143,8 @@ static unsigned SkipFragment (FILE* F)
/* Handle the different fragment types */ /* Handle the different fragment types */
switch (Type) { switch (Type) {
case FRAG_LITERAL8: case FRAG_LITERAL:
Size = Read8 (F); Size = ReadVar (F);
break;
case FRAG_LITERAL16:
Size = Read16 (F);
break;
case FRAG_LITERAL24:
Size = Read24 (F);
break;
case FRAG_LITERAL32:
Size = Read32 (F);
break; break;
case FRAG_EXPR8: case FRAG_EXPR8:
@@ -171,7 +159,7 @@ static unsigned SkipFragment (FILE* F)
break; break;
case FRAG_FILL: case FRAG_FILL:
Size = Read16 (F); Size = ReadVar (F);
break; break;
default: default:
@@ -277,7 +265,7 @@ void DumpObjOptions (FILE* F, unsigned long Offset)
printf (" Options:\n"); printf (" Options:\n");
/* Read the number of options and print it */ /* Read the number of options and print it */
Count = Read16 (F); Count = ReadVar (F);
printf (" Count:%27u\n", Count); printf (" Count:%27u\n", Count);
/* Read and print all options */ /* Read and print all options */
@@ -313,7 +301,7 @@ void DumpObjOptions (FILE* F, unsigned long Offset)
switch (ArgType) { switch (ArgType) {
case OPT_ARGSTR: case OPT_ARGSTR:
ArgStr = ReadMallocedStr (F); ArgStr = ReadStr (F);
ArgLen = strlen (ArgStr); ArgLen = strlen (ArgStr);
printf (" Data:%*s\"%s\"\n", 24-ArgLen, "", ArgStr); printf (" Data:%*s\"%s\"\n", 24-ArgLen, "", ArgStr);
xfree (ArgStr); xfree (ArgStr);
@@ -361,7 +349,7 @@ void DumpObjFiles (FILE* F, unsigned long Offset)
printf (" Files:\n"); printf (" Files:\n");
/* Read the number of files and print it */ /* Read the number of files and print it */
Count = Read16 (F); Count = ReadVar (F);
printf (" Count:%27u\n", Count); printf (" Count:%27u\n", Count);
/* Read and print all files */ /* Read and print all files */
@@ -370,7 +358,7 @@ void DumpObjFiles (FILE* F, unsigned long Offset)
/* Read the data for one file */ /* Read the data for one file */
unsigned long MTime = Read32 (F); unsigned long MTime = Read32 (F);
unsigned long Size = Read32 (F); unsigned long Size = Read32 (F);
char* Name = ReadMallocedStr (F); char* Name = ReadStr (F);
unsigned Len = strlen (Name); unsigned Len = strlen (Name);
/* Print the header */ /* Print the header */
@@ -409,14 +397,14 @@ void DumpObjSegments (FILE* F, unsigned long Offset)
printf (" Segments:\n"); printf (" Segments:\n");
/* Read the number of segments and print it */ /* Read the number of segments and print it */
Count = Read8 (F); Count = ReadVar (F);
printf (" Count:%27u\n", Count); printf (" Count:%27u\n", Count);
/* Read and print all segments */ /* Read and print all segments */
for (I = 0; I < Count; ++I) { for (I = 0; I < Count; ++I) {
/* Read the data for one segments */ /* Read the data for one segments */
char* Name = ReadMallocedStr (F); char* Name = ReadStr (F);
unsigned Len = strlen (Name); unsigned Len = strlen (Name);
unsigned long Size = Read32 (F); unsigned long Size = Read32 (F);
unsigned Align = (1U << Read8 (F)); unsigned Align = (1U << Read8 (F));
@@ -484,7 +472,7 @@ void DumpObjImports (FILE* F, unsigned long Offset)
printf (" Imports:\n"); printf (" Imports:\n");
/* Read the number of imports and print it */ /* Read the number of imports and print it */
Count = Read16 (F); Count = ReadVar (F);
printf (" Count:%27u\n", Count); printf (" Count:%27u\n", Count);
/* Read and print all imports */ /* Read and print all imports */
@@ -494,7 +482,7 @@ void DumpObjImports (FILE* F, unsigned long Offset)
/* Read the data for one import */ /* Read the data for one import */
unsigned char Type = Read8 (F); unsigned char Type = Read8 (F);
char* Name = ReadMallocedStr (F); char* Name = ReadStr (F);
unsigned Len = strlen (Name); unsigned Len = strlen (Name);
ReadFilePos (F, &Pos); ReadFilePos (F, &Pos);
@@ -540,7 +528,7 @@ void DumpObjExports (FILE* F, unsigned long Offset)
printf (" Exports:\n"); printf (" Exports:\n");
/* Read the number of exports and print it */ /* Read the number of exports and print it */
Count = Read16 (F); Count = ReadVar (F);
printf (" Count:%27u\n", Count); printf (" Count:%27u\n", Count);
/* Read and print all exports */ /* Read and print all exports */
@@ -552,7 +540,7 @@ void DumpObjExports (FILE* F, unsigned long Offset)
/* Read the data for one export */ /* Read the data for one export */
unsigned char Type = Read8 (F); unsigned char Type = Read8 (F);
char* Name = ReadMallocedStr (F); char* Name = ReadStr (F);
unsigned Len = strlen (Name); unsigned Len = strlen (Name);
if (Type & EXP_EXPR) { if (Type & EXP_EXPR) {
SkipExpr (F); SkipExpr (F);
@@ -617,19 +605,19 @@ void DumpObjDbgSyms (FILE* F, unsigned long Offset)
} }
/* Read the number of exports and print it */ /* Read the number of exports and print it */
Count = Read16 (F); Count = ReadVar (F);
printf (" Count:%27u\n", Count); printf (" Count:%27u\n", Count);
/* Read and print all debug symbols */ /* Read and print all debug symbols */
for (I = 0; I < Count; ++I) { for (I = 0; I < Count; ++I) {
unsigned long Value = 0; unsigned long Value = 0;
int HaveValue; int HaveValue;
const char* TypeDesc; const char* TypeDesc;
/* Read the data for one symbol */ /* Read the data for one symbol */
unsigned char Type = Read8 (F); unsigned char Type = Read8 (F);
char* Name = ReadMallocedStr (F); char* Name = ReadStr (F);
unsigned Len = strlen (Name); unsigned Len = strlen (Name);
if (Type & EXP_EXPR) { if (Type & EXP_EXPR) {
SkipExpr (F); SkipExpr (F);
@@ -666,3 +654,4 @@ void DumpObjDbgSyms (FILE* F, unsigned long Offset)

View File

@@ -67,7 +67,7 @@ unsigned Read8 (FILE* F)
int C = getc (F); int C = getc (F);
if (C == EOF) { if (C == EOF) {
Error ("Read error (file corrupt?)"); Error ("Read error (file corrupt?)");
} }
return C; return C;
} }
@@ -121,27 +121,35 @@ long Read32Signed (FILE* F)
char* ReadStr (FILE* F, char* Str) unsigned long ReadVar (FILE* F)
/* Read a string from the file. Str must hold 256 chars at max */ /* Read a variable size value from the file */
{ {
/* Read the length byte */ /* The value was written to the file in 7 bit chunks LSB first. If there
unsigned Len = Read8 (F); * 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);
/* Read the string itself */ /* Return the value read */
ReadData (F, Str, Len); return V;
/* Terminate the string and return it */
Str [Len] = '\0';
return Str;
} }
char* ReadMallocedStr (FILE* F) char* ReadStr (FILE* F)
/* Read a string from the file into a malloced area */ /* Read a string from the file into a malloced area */
{ {
/* Read the length byte */ /* Read the length */
unsigned Len = Read8 (F); unsigned Len = ReadVar (F);
/* Allocate memory */ /* Allocate memory */
char* Str = xmalloc (Len + 1); char* Str = xmalloc (Len + 1);
@@ -159,10 +167,10 @@ char* ReadMallocedStr (FILE* F)
FilePos* ReadFilePos (FILE* F, FilePos* Pos) FilePos* ReadFilePos (FILE* F, FilePos* Pos)
/* Read a file position from the file */ /* Read a file position from the file */
{ {
/* The line number is encoded as 24 bit value to save some space */ /* Read the data fields */
Pos->Line = Read24 (F); Pos->Line = ReadVar (F);
Pos->Col = Read8 (F); Pos->Col = ReadVar (F);
Pos->Name = Read8 (F); Pos->Name = ReadVar (F);
return Pos; return Pos;
} }

View File

@@ -70,10 +70,10 @@ unsigned long Read32 (FILE* F);
long Read32Signed (FILE* F); long Read32Signed (FILE* F);
/* Read a 32 bit value from the file. Sign extend the value. */ /* Read a 32 bit value from the file. Sign extend the value. */
char* ReadStr (FILE* F, char* Str); unsigned long ReadVar (FILE* F);
/* Read a string from the file. Str must hold 256 chars at max */ /* 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 */ /* Read a string from the file into a malloced area */
FilePos* ReadFilePos (FILE* F, FilePos* Pos); FilePos* ReadFilePos (FILE* F, FilePos* Pos);