Restructuring the object file format

git-svn-id: svn://svn.cc65.org/cc65/trunk@2196 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
cuz
2003-06-03 22:19:46 +00:00
parent 0f8add2112
commit 4937cd236f
18 changed files with 380 additions and 299 deletions

View File

@@ -34,6 +34,7 @@
/* common */
#include "segdefs.h"
#include "xmalloc.h"
/* ld65 */
@@ -48,11 +49,21 @@
Fragment* NewFragment (unsigned char Type, unsigned long Size, Section* S)
Fragment* NewFragment (unsigned char Type, unsigned Size, Section* S)
/* Create a new fragment and insert it into the section S */
{
Fragment* F;
/* Calculate the size of the memory block. LitBuf is only needed if the
* fragment contains literal data.
*/
unsigned FragSize = sizeof (Fragment) - 1;
if (Type == FRAG_LITERAL) {
FragSize += Size;
}
/* Allocate memory */
Fragment* F = xmalloc (sizeof (Fragment) - 1 + Size);
F = xmalloc (FragSize);
/* Initialize the data */
F->Next = 0;
@@ -61,8 +72,7 @@ Fragment* NewFragment (unsigned char Type, unsigned long Size, Section* S)
F->Expr = 0;
InitFilePos (&F->Pos);
F->LI = 0;
F->WarnExpr = 0;
F->ErrorExpr = 0;
F->Check = 0;
F->Type = Type;
/* Insert the code fragment into the section */

View File

@@ -60,17 +60,25 @@ struct Section;
/* Fragment check expression */
typedef struct CheckExpr CheckExpr;
struct CheckExpr {
struct CheckExpr* Next; /* Next check expression */
struct ExprNode* Expr; /* The expression itself */
unsigned Action; /* Action to take if the check fails */
unsigned Message; /* Message number */
};
/* Fragment structure */
typedef struct Fragment Fragment;
struct Fragment {
Fragment* Next; /* Next fragment in list */
struct ObjData* Obj; /* Source of fragment */
unsigned long Size; /* Size of data/expression */
unsigned Size; /* Size of data/expression */
struct ExprNode* Expr; /* Expression if FRAG_EXPR */
FilePos Pos; /* File position in source */
struct LineInfo* LI; /* Additional line info */
struct ExprNode* WarnExpr; /* Print warning if expr true */
struct ExprNode* ErrorExpr; /* Print error if expr true */
CheckExpr* Check; /* Single linked list of expressions */
unsigned char Type; /* Type of fragment */
unsigned char LitBuf [1]; /* Dynamically alloc'ed literal buffer */
};
@@ -83,7 +91,7 @@ struct Fragment {
Fragment* NewFragment (unsigned char Type, unsigned long Size, struct Section* S);
Fragment* NewFragment (unsigned char Type, unsigned Size, struct Section* S);
/* Create a new fragment and insert it into the section S */

View File

@@ -42,6 +42,7 @@ OBJS = bin.o \
objfile.o \
scanner.o \
segments.o \
spool.o \
tgtcfg.o
# -----------------------------------------------------------------------------

View File

@@ -67,6 +67,7 @@ OBJS = bin.obj \
objfile.obj \
scanner.obj \
segments.obj \
spool.obj \
tgtcfg.obj
LIBS = ..\common\common.lib

View File

@@ -398,9 +398,9 @@ static O65RelocTab* NewO65RelocTab (void)
O65RelocTab* R = xmalloc (sizeof (O65RelocTab));
/* Initialize the data */
R->Size = RELOC_BLOCKSIZE;
R->Size = 0;
R->Fill = 0;
R->Buf = xmalloc (RELOC_BLOCKSIZE);
R->Buf = 0;
/* Return the created struct */
return R;

View File

@@ -211,27 +211,25 @@ Section* NewSection (Segment* Seg, unsigned char Align, unsigned char Type)
Section* ReadSection (FILE* F, ObjData* O)
/* Read a section from a file */
{
char* Name;
unsigned long Size;
char* Name;
unsigned Size;
unsigned char Align;
unsigned char Type;
Segment* S;
Section* Sec;
unsigned FragCount;
Segment* S;
Section* Sec;
/* Read the name */
Name = ReadStr (F);
/* Read the segment data */
(void) Read32 (F); /* File size of data */
Name = ReadStr (F); /* Segment name */
Size = Read32 (F); /* Size of data */
Align = Read8 (F); /* Alignment */
Type = Read8 (F); /* Segment type */
FragCount = ReadVar (F); /* Number of fragments */
/* Read the size */
Size = Read32 (F);
/* Read the alignment */
Align = Read8 (F);
/* Read the segment type */
Type = Read8 (F);
/* Print some data */
Print (stdout, 2, "Module `%s': Found segment `%s', size = %lu, align = %u, type = %u\n",
Print (stdout, 2, "Module `%s': Found segment `%s', size = %u, align = %u, type = %u\n",
GetObjFileName (O), Name, Size, Align, Type);
/* Get the segment for this section */
@@ -251,7 +249,7 @@ Section* ReadSection (FILE* F, ObjData* O)
}
/* Start reading fragments from the file and insert them into the section . */
while (Size) {
while (FragCount--) {
Fragment* Frag;
unsigned LineInfoIndex;
@@ -261,24 +259,21 @@ Section* ReadSection (FILE* F, ObjData* O)
/* Extract the check mask from the type */
unsigned char Check = Type & FRAG_CHECKMASK;
Type &= ~FRAG_CHECKMASK;
unsigned char Bytes = Type & FRAG_BYTEMASK;
Type &= FRAG_TYPEMASK;
/* Handle the different fragment types */
switch (Type) {
case FRAG_LITERAL:
Frag = NewFragment (Type, ReadVar (F), Sec);
ReadData (F, Frag->LitBuf, Frag->Size);
break;
case FRAG_EXPR8:
case FRAG_EXPR16:
case FRAG_EXPR24:
case FRAG_EXPR32:
case FRAG_SEXPR8:
case FRAG_SEXPR16:
case FRAG_SEXPR24:
case FRAG_SEXPR32:
Frag = NewFragment (Type & FRAG_TYPEMASK, Type & FRAG_BYTEMASK, Sec);
case FRAG_EXPR:
case FRAG_SEXPR:
Frag = NewFragment (Type, Bytes, Sec);
Frag->Expr = ReadExpr (F, O);
break;
case FRAG_FILL:
@@ -293,28 +288,17 @@ Section* ReadSection (FILE* F, ObjData* O)
return 0;
}
/* Now read the fragment data */
switch (Frag->Type) {
/* A list of check expressions may follow */
if (Check) {
case FRAG_LITERAL:
/* Literal data */
ReadData (F, Frag->LitBuf, Frag->Size);
break;
/* Read the number of expressions that follow */
unsigned Count = ReadVar (F);
case FRAG_EXPR:
case FRAG_SEXPR:
/* An expression */
Frag->Expr = ReadExpr (F, O);
break;
}
/* A check expression may follow */
if (Check & FRAG_CHECK_WARN) {
Frag->WarnExpr = ReadExpr (F, O);
}
if (Check & FRAG_CHECK_ERROR) {
Frag->ErrorExpr = ReadExpr (F, O);
/* Read the expressions */
CheckExpr* Last = 0;
while (Count--) {
/* ### */
}
}
/* Read the file position of the fragment */
@@ -339,10 +323,6 @@ Section* ReadSection (FILE* F, ObjData* O)
/* Remember the module we had this fragment from */
Frag->Obj = O;
/* Next one */
CHECK (Size >= Frag->Size);
Size -= Frag->Size;
}
/* Return the section */
@@ -410,7 +390,7 @@ void SegDump (void)
switch (F->Type) {
case FRAG_LITERAL:
printf (" Literal (%lu bytes):", F->Size);
printf (" Literal (%u bytes):", F->Size);
Count = F->Size;
Data = F->LitBuf;
I = 100;
@@ -426,21 +406,21 @@ void SegDump (void)
break;
case FRAG_EXPR:
printf (" Expression (%lu bytes):\n", F->Size);
printf (" Expression (%u bytes):\n", F->Size);
printf (" ");
DumpExpr (F->Expr);
break;
case FRAG_SEXPR:
printf (" Signed expression (%lu bytes):\n", F->Size);
printf (" Signed expression (%u bytes):\n", F->Size);
printf (" ");
DumpExpr (F->Expr);
break;
case FRAG_FILL:
printf (" Empty space (%lu bytes)\n", F->Size);
printf (" Empty space (%u bytes)\n", F->Size);
break;
default:
Internal ("Invalid fragment type: %02X", F->Type);
}
@@ -518,7 +498,7 @@ void SegWrite (FILE* Tgt, Segment* S, SegWriteFunc F, void* Data)
/* Loop over all fragments in this section */
Frag = Sec->FragRoot;
while (Frag) {
/* Do fragment alignment checks */

56
src/ld65/spool.c Normal file
View File

@@ -0,0 +1,56 @@
/*****************************************************************************/
/* */
/* spool.c */
/* */
/* Id and message pool for the ld65 linker */
/* */
/* */
/* */
/* (C) 2003 Ullrich von Bassewitz */
/* R<>merstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
/* */
/* */
/* 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. */
/* */
/*****************************************************************************/
/* ld65 */
#include "spool.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
StringPool StrPool = STATIC_STRINGPOOL_INITIALIZER;
/*****************************************************************************/
/* Code */
/*****************************************************************************/

80
src/ld65/spool.h Normal file
View File

@@ -0,0 +1,80 @@
/*****************************************************************************/
/* */
/* spool.h */
/* */
/* Id and message pool for the ld65 linker */
/* */
/* */
/* */
/* (C) 2003 Ullrich von Bassewitz */
/* R<>merstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
/* */
/* */
/* 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. */
/* */
/*****************************************************************************/
#ifndef SPOOL_H
#define SPOOL_H
/* common */
#include "strpool.h"
/*****************************************************************************/
/* Data */
/*****************************************************************************/
extern StringPool StrPool;
/*****************************************************************************/
/* Code */
/*****************************************************************************/
#if defined(HAVE_INLINE)
INLINE unsigned GetStringId (const char* S)
/* Return the id of the given string */
{
return SP_Add (&StrPool, S);
}
#else
# define GetStringId(S) SP_Add (&StrPool, (S))
#endif
/* End of spool.h */
#endif