Mark the symbol that is the name of a scope with the size of that scope

(previously only the scope itself had that size).
Pass the size of symbols through the object file to the linker. Bump the
object file version and adjust object file reading tools (od65, ar65) to this
change.
Read the size in the linker and output it in the debug info.
Bump the minor version number of the debug info. Read the size and allow to
access it via the API. Do better version checking for the debug info and try
to be smarter when encountering unknown keywords to improve support for newer
minor versions.


git-svn-id: svn://svn.cc65.org/cc65/trunk@5057 b7a2c559-68d2-44c3-8de9-860c34a00d81
This commit is contained in:
uz
2011-06-13 18:43:50 +00:00
parent 7c2104c3cb
commit 795d9e1a1c
22 changed files with 349 additions and 170 deletions

View File

@@ -65,7 +65,7 @@ void DoEnum (void)
int Anon = (CurTok.Tok != TOK_IDENT);
if (!Anon) {
/* Enter a new scope, then skip the name */
SymEnterLevel (&CurTok.SVal, ST_ENUM, ADDR_SIZE_ABS);
SymEnterLevel (&CurTok.SVal, ST_ENUM, ADDR_SIZE_ABS, 0);
NextTok ();
}

View File

@@ -479,7 +479,7 @@ static void OptListBytes (const char* Opt, const char* Arg)
static void OptListing (const char* Opt, const char* Arg)
/* Create a listing file */
{
{
/* Since the meaning of -l and --listing has changed, print an error if
* the filename is empty or begins with the option char.
*/
@@ -868,7 +868,7 @@ int main (int argc, char* argv [])
/* Enter the base lexical level. We must do that here, since we may
* define symbols using -D.
*/
SymEnterLevel (&GlobalNameSpace, ST_GLOBAL, ADDR_SIZE_DEFAULT);
SymEnterLevel (&GlobalNameSpace, ST_GLOBAL, ADDR_SIZE_DEFAULT, 0);
/* Initialize the line infos. Must be done here, since we need line infos
* for symbol definitions.

View File

@@ -1508,12 +1508,12 @@ static void DoProc (void)
{
StrBuf Name = STATIC_STRBUF_INITIALIZER;
unsigned char AddrSize;
SymEntry* Sym = 0;
if (CurTok.Tok == TOK_IDENT) {
SymEntry* Sym;
/* The new scope has a name. Remember it. */
/* The new scope has a name. Remember it. */
SB_Copy (&Name, &CurTok.SVal);
/* Search for the symbol, generate a new one if needed */
@@ -1538,7 +1538,7 @@ static void DoProc (void)
}
/* Enter a new scope */
SymEnterLevel (&Name, ST_PROC, AddrSize);
SymEnterLevel (&Name, ST_PROC, AddrSize, Sym);
/* Free memory for Name */
SB_Done (&Name);
@@ -1665,7 +1665,7 @@ static void DoScope (void)
AddrSize = OptionalAddrSize ();
/* Enter the new scope */
SymEnterLevel (&Name, ST_SCOPE, AddrSize);
SymEnterLevel (&Name, ST_SCOPE, AddrSize, 0);
/* Free memory for Name */
SB_Done (&Name);

View File

@@ -6,7 +6,7 @@
/* */
/* */
/* */
/* (C) 2003-2008, Ullrich von Bassewit */
/* (C) 2003-2011, Ullrich von Bassewit */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
@@ -44,7 +44,7 @@
/*****************************************************************************/
/* Data */
/* Data */
/*****************************************************************************/
@@ -52,7 +52,7 @@
/* The name of the symbol used to encode the size. The name of this entry is
* choosen so that it cannot be accessed by the user.
*/
const StrBuf SizeEntryName = LIT_STRBUF_INITIALIZER (".size");
static const StrBuf SizeEntryName = LIT_STRBUF_INITIALIZER (".size");
@@ -62,6 +62,36 @@ const StrBuf SizeEntryName = LIT_STRBUF_INITIALIZER (".size");
int IsSizeOfSymbol (const SymEntry* Sym)
/* Return true if the given symbol is the one that encodes the size of some
* entity. Sym may also be a NULL pointer in which case false is returned.
*/
{
return (Sym != 0 && SB_Compare (GetSymName (Sym), &SizeEntryName) == 0);
}
SymEntry* FindSizeOfScope (SymTable* Scope)
/* Get the size of a scope. The function returns the symbol table entry that
* encodes the size or NULL if there is no such entry.
*/
{
return SymFind (Scope, &SizeEntryName, SYM_FIND_EXISTING);
}
SymEntry* FindSizeOfSymbol (SymEntry* Sym)
/* Get the size of a symbol table entry. The function returns the symbol table
* entry that encodes the size of the symbol or NULL if there is no such entry.
*/
{
return SymFindLocal (Sym, &SizeEntryName, SYM_FIND_EXISTING);
}
SymEntry* GetSizeOfScope (SymTable* Scope)
/* Get the size of a scope. The function returns the symbol table entry that
* encodes the size, and will create a new entry if it does not exist.

View File

@@ -6,7 +6,7 @@
/* */
/* */
/* */
/* (C) 2003-2008, Ullrich von Bassewitz */
/* (C) 2003-2011, Ullrich von Bassewitz */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
@@ -35,7 +35,7 @@
#ifndef SIZEOF_H
#define SIZEOF_H
/* common */
@@ -54,22 +54,27 @@ struct SymTable;
/*****************************************************************************/
/* Data */
/*****************************************************************************/
extern const StrBuf SizeEntryName; /* Contains name of symbol with size */
/*****************************************************************************/
/* Code */
/*****************************************************************************/
int IsSizeOfSymbol (const struct SymEntry* Sym);
/* Return true if the given symbol is the one that encodes the size of some
* entity. Sym may also be a NULL pointer in which case false is returned.
*/
struct SymEntry* FindSizeOfScope (struct SymTable* Scope);
/* Get the size of a scope. The function returns the symbol table entry that
* encodes the size or NULL if there is no such entry.
*/
struct SymEntry* FindSizeOfSymbol (struct SymEntry* Sym);
/* Get the size of a symbol table entry. The function returns the symbol table
* entry that encodes the size of the symbol or NULL if there is no such entry.
*/
struct SymEntry* GetSizeOfScope (struct SymTable* Scope);
/* Get the size of a scope. The function returns the symbol table entry that
* encodes the size, and will create a new entry if it does not exist.

View File

@@ -106,7 +106,7 @@ static long DoStructInternal (long Offs, unsigned Type)
int Anon = (CurTok.Tok != TOK_IDENT);
if (!Anon) {
/* Enter a new scope, then skip the name */
SymEnterLevel (&CurTok.SVal, ST_STRUCT, ADDR_SIZE_ABS);
SymEnterLevel (&CurTok.SVal, ST_STRUCT, ADDR_SIZE_ABS, 0);
NextTok ();
/* Start at zero offset in the new scope */
Offs = 0;
@@ -116,8 +116,8 @@ static long DoStructInternal (long Offs, unsigned Type)
ConsumeSep ();
/* Read until end of struct */
while (CurTok.Tok != TOK_ENDSTRUCT &&
CurTok.Tok != TOK_ENDUNION &&
while (CurTok.Tok != TOK_ENDSTRUCT &&
CurTok.Tok != TOK_ENDUNION &&
CurTok.Tok != TOK_EOF) {
long MemberSize;
@@ -263,12 +263,12 @@ static long DoStructInternal (long Offs, unsigned Type)
long GetStructSize (SymTable* Struct)
/* Get the size of a struct or union */
{
SymEntry* Sym = SymFind (Struct, &SizeEntryName, SYM_FIND_EXISTING);
if (Sym == 0) {
SymEntry* SizeSym = FindSizeOfScope (Struct);
if (SizeSym == 0) {
Error ("Size of struct/union is unknown");
return 0;
} else {
return GetSymVal (Sym);
return GetSymVal (SizeSym);
}
}

View File

@@ -112,6 +112,7 @@ static SymTable* NewSymTable (SymTable* Parent, const StrBuf* Name)
S->Left = 0;
S->Right = 0;
S->Childs = 0;
S->OwnerSym = 0;
S->SegRanges = AUTO_COLLECTION_INITIALIZER;
S->Flags = ST_NONE;
S->AddrSize = ADDR_SIZE_DEFAULT;
@@ -160,7 +161,7 @@ static SymTable* NewSymTable (SymTable* Parent, const StrBuf* Name)
} else {
T->Right = S;
break;
}
}
} else {
/* Duplicate scope name */
Internal ("Duplicate scope name: `%m%p'", Name);
@@ -181,7 +182,8 @@ static SymTable* NewSymTable (SymTable* Parent, const StrBuf* Name)
void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type, unsigned char AddrSize)
void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type,
unsigned char AddrSize, SymEntry* OwnerSym)
/* Enter a new lexical level */
{
/* Map a default address size to something real */
@@ -207,10 +209,11 @@ void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type, unsigned char A
CurrentScope = RootScope = NewSymTable (0, ScopeName);
}
/* Mark the scope as defined and set type and address size */
/* Mark the scope as defined and set type, address size and owner symbol */
CurrentScope->Flags |= ST_DEFINED;
CurrentScope->AddrSize = AddrSize;
CurrentScope->Type = Type;
CurrentScope->OwnerSym = OwnerSym;
/* If this is a scope that allows to emit data into segments, add segment
* ranges for all currently existing segments. Doing this for just a few
@@ -235,11 +238,16 @@ void SymLeaveLevel (void)
/* If we have segment ranges, the first one is the segment that was
* active, when the scope was opened. Set the size of the scope to the
* number of data bytes emitted into this segment.
* number of data bytes emitted into this segment. If we have an owner
* symbol set the size of this symbol, too.
*/
if (CollCount (&CurrentScope->SegRanges) > 0) {
const SegRange* R = CollAtUnchecked (&CurrentScope->SegRanges, 0);
DefSizeOfScope (CurrentScope, GetSegRangeSize (R));
unsigned long Size = GetSegRangeSize (R);
DefSizeOfScope (CurrentScope, Size);
if (CurrentScope->OwnerSym) {
DefSizeOfSymbol (CurrentScope->OwnerSym, Size);
}
}
/* Leave the scope */
@@ -502,7 +510,7 @@ static void SymCheckUndefined (SymEntry* S)
/* The symbol is definitely undefined */
if (S->Flags & SF_EXPORT) {
/* We will not auto-import an export */
LIError (&S->LineInfos,
LIError (&S->LineInfos,
"Exported symbol `%m%p' was never defined",
GetSymName (S));
} else {
@@ -513,8 +521,8 @@ static void SymCheckUndefined (SymEntry* S)
S->AddrSize = CodeAddrSize;
} else {
/* Error */
LIError (&S->LineInfos,
"Symbol `%m%p' is undefined",
LIError (&S->LineInfos,
"Symbol `%m%p' is undefined",
GetSymName (S));
}
}
@@ -727,6 +735,15 @@ void WriteExports (void)
long ConstVal;
unsigned ExprMask = GetSymInfoFlags (S, &ConstVal);
/* Check if this symbol has a size. If so, remember it in the
* flags.
*/
long Size;
SymEntry* SizeSym = FindSizeOfSymbol (S);
if (SizeSym != 0 && SymIsConst (SizeSym, &Size)) {
ExprMask |= SYM_SIZE;
}
/* Count the number of ConDes types */
for (Type = 0; Type < CD_TYPE_COUNT; ++Type) {
if (S->ConDesPrio[Type] != CD_PRIO_NONE) {
@@ -760,6 +777,11 @@ void WriteExports (void)
WriteExpr (S->Expr);
}
/* If the symbol has a size, write it to the file */
if (SYM_HAS_SIZE (ExprMask)) {
ObjWriteVar (Size);
}
/* Write the line infos */
WriteLineInfo (&S->LineInfos);
}
@@ -788,7 +810,8 @@ void WriteDbgSyms (void)
Count = 0;
S = SymList;
while (S) {
if ((S->Flags & SF_DBGINFOMASK) == SF_DBGINFOVAL) {
if ((S->Flags & SF_DBGINFOMASK) == SF_DBGINFOVAL &&
!IsSizeOfSymbol (S)) {
S->DebugSymId = Count++;
}
S = S->List;
@@ -797,15 +820,27 @@ void WriteDbgSyms (void)
/* Write the symbol count to the list */
ObjWriteVar (Count);
/* Walk through list and write all symbols to the file */
/* Walk through list and write all symbols to the file. Ignore size
* symbols.
*/
S = SymList;
while (S) {
if ((S->Flags & SF_DBGINFOMASK) == SF_DBGINFOVAL) {
if ((S->Flags & SF_DBGINFOMASK) == SF_DBGINFOVAL &&
!IsSizeOfSymbol (S)) {
/* Get the expression bits and the value */
long ConstVal;
unsigned ExprMask = GetSymInfoFlags (S, &ConstVal);
/* Check if this symbol has a size. If so, remember it in the
* flags.
*/
long Size;
SymEntry* SizeSym = FindSizeOfSymbol (S);
if (SizeSym != 0 && SymIsConst (SizeSym, &Size)) {
ExprMask |= SYM_SIZE;
}
/* Write the type */
ObjWriteVar (ExprMask);
@@ -824,6 +859,11 @@ void WriteDbgSyms (void)
WriteExpr (S->Expr);
}
/* If the symbol has a size, write it to the file */
if (SYM_HAS_SIZE (ExprMask)) {
ObjWriteVar (Size);
}
/* Write the line infos */
WriteLineInfo (&S->LineInfos);
}

View File

@@ -6,7 +6,7 @@
/* */
/* */
/* */
/* (C) 1998-2010, Ullrich von Bassewitz */
/* (C) 1998-2011, Ullrich von Bassewitz */
/* Roemerstrasse 52 */
/* D-70794 Filderstadt */
/* EMail: uz@cc65.org */
@@ -73,12 +73,13 @@ enum {
/* A symbol table */
typedef struct SymTable SymTable;
struct SymTable {
struct SymTable {
SymTable* Next; /* Pointer to next table in list */
SymTable* Left; /* Pointer to smaller entry */
SymTable* Right; /* Pointer to greater entry */
SymTable* Parent; /* Link to enclosing scope if any */
SymTable* Childs; /* Pointer to child scopes */
SymEntry* OwnerSym; /* Symbol that "owns" the scope */
Collection SegRanges; /* Segment ranges for this scope */
unsigned Id; /* Scope id */
unsigned short Flags; /* Symbol table flags */
@@ -103,7 +104,8 @@ extern SymTable* RootScope; /* Root symbol table */
void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type, unsigned char AddrSize);
void SymEnterLevel (const StrBuf* ScopeName, unsigned char Type,
unsigned char AddrSize, SymEntry* OwnerSym);
/* Enter a new lexical level */
void SymLeaveLevel (void);