2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* exports.c */
|
|
|
|
|
/* */
|
2000-11-02 22:11:25 +00:00
|
|
|
|
/* Exports handling for the ld65 linker */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* (C) 1998-2003 Ullrich von Bassewitz */
|
|
|
|
|
/* R<>merstrasse 52 */
|
|
|
|
|
/* D-70794 Filderstadt */
|
|
|
|
|
/* EMail: uz@cc65.org */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
|
/* arising from the use of this software. */
|
|
|
|
|
/* */
|
|
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
|
/* freely, subject to the following restrictions: */
|
|
|
|
|
/* */
|
|
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
|
/* distribution. */
|
|
|
|
|
/* */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2000-08-01 15:17:43 +00:00
|
|
|
|
/* common */
|
|
|
|
|
#include "check.h"
|
2000-10-30 20:48:11 +00:00
|
|
|
|
#include "coll.h"
|
2000-08-01 15:17:43 +00:00
|
|
|
|
#include "hashstr.h"
|
|
|
|
|
#include "symdefs.h"
|
|
|
|
|
#include "xmalloc.h"
|
2000-08-02 13:23:06 +00:00
|
|
|
|
|
2000-08-01 15:17:43 +00:00
|
|
|
|
/* ld65 */
|
2000-11-20 15:22:57 +00:00
|
|
|
|
#include "condes.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "error.h"
|
2003-06-04 12:40:14 +00:00
|
|
|
|
#include "exports.h"
|
|
|
|
|
#include "expr.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "fileio.h"
|
2000-11-20 15:22:57 +00:00
|
|
|
|
#include "global.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "objdata.h"
|
2003-06-04 12:40:14 +00:00
|
|
|
|
#include "spool.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2000-10-30 20:48:11 +00:00
|
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Hash table */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
#define HASHTAB_MASK 0x0FFFU
|
|
|
|
|
#define HASHTAB_SIZE (HASHTAB_MASK + 1)
|
|
|
|
|
static Export* HashTab[HASHTAB_SIZE];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Import management variables */
|
2000-10-30 20:48:11 +00:00
|
|
|
|
static unsigned ImpCount = 0; /* Import count */
|
|
|
|
|
static unsigned ImpOpen = 0; /* Count of open imports */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Export management variables */
|
2000-10-30 20:48:11 +00:00
|
|
|
|
static unsigned ExpCount = 0; /* Export count */
|
|
|
|
|
static Export** ExpPool = 0; /* Exports array */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
/* Defines for the flags in Import */
|
|
|
|
|
#define IMP_INLIST 0x0001U /* Import is in exports list */
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Defines for the flags in Export */
|
2003-06-05 16:50:01 +00:00
|
|
|
|
#define EXP_INLIST 0x0001U /* Export is in exports list */
|
|
|
|
|
#define EXP_USERMARK 0x0002U /* User setable flag */
|
2000-10-30 20:48:11 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2000-11-03 09:31:18 +00:00
|
|
|
|
/* Import handling */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
static Export* NewExport (unsigned char Type, unsigned Name, ObjData* Obj);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Create a new export and initialize it */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Import* NewImport (unsigned char Type, ObjData* Obj)
|
|
|
|
|
/* Create a new import and initialize it */
|
|
|
|
|
{
|
|
|
|
|
/* Allocate memory */
|
2000-06-14 09:57:42 +00:00
|
|
|
|
Import* I = xmalloc (sizeof (Import));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Initialize the fields */
|
|
|
|
|
I->Next = 0;
|
|
|
|
|
I->Obj = Obj;
|
2003-06-04 12:40:14 +00:00
|
|
|
|
I->Exp = 0;
|
|
|
|
|
I->Name = INVALID_STRING_ID;
|
2003-06-05 16:50:01 +00:00
|
|
|
|
I->Flags = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
I->Type = Type;
|
|
|
|
|
|
|
|
|
|
/* Return the new structure */
|
|
|
|
|
return I;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void InsertImport (Import* I)
|
|
|
|
|
/* Insert an import into the table */
|
|
|
|
|
{
|
|
|
|
|
Export* E;
|
|
|
|
|
|
|
|
|
|
/* As long as the import is not inserted, V.Name is valid */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
unsigned Name = I->Name;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Create a hash value for the given name */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
unsigned Hash = (Name & HASHTAB_MASK);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Search through the list in that slot and print matching duplicates */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
if (HashTab[Hash] == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* The slot is empty, we need to insert a dummy export */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
E = HashTab[Hash] = NewExport (0, Name, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
++ExpCount;
|
|
|
|
|
} else {
|
2003-06-04 12:40:14 +00:00
|
|
|
|
E = HashTab [Hash];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
while (1) {
|
2003-06-04 12:40:14 +00:00
|
|
|
|
if (E->Name == Name) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* We have an entry, L points to it */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
break;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
if (E->Next == 0) {
|
|
|
|
|
/* End of list an entry not found, insert a dummy */
|
|
|
|
|
E->Next = NewExport (0, Name, 0);
|
2003-05-25 17:57:50 +00:00
|
|
|
|
E = E->Next; /* Point to dummy */
|
|
|
|
|
++ExpCount; /* One export more */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
break;
|
|
|
|
|
} else {
|
|
|
|
|
E = E->Next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Ok, E now points to a valid exports entry for the given import. Insert
|
|
|
|
|
* the import into the imports list and update the counters.
|
|
|
|
|
*/
|
2003-06-04 12:40:14 +00:00
|
|
|
|
I->Exp = E;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
I->Next = E->ImpList;
|
|
|
|
|
E->ImpList = I;
|
|
|
|
|
E->ImpCount++;
|
2003-05-25 17:57:50 +00:00
|
|
|
|
++ImpCount; /* Total import count */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
if (E->Expr == 0) {
|
|
|
|
|
/* This is a dummy export */
|
|
|
|
|
++ImpOpen;
|
|
|
|
|
}
|
2003-06-05 16:50:01 +00:00
|
|
|
|
|
|
|
|
|
/* Mark the import so we know it's in the list */
|
|
|
|
|
I->Flags |= IMP_INLIST;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void FreeImport (Import* I)
|
|
|
|
|
/* Free an import. NOTE: This won't remove the import from the exports table,
|
|
|
|
|
* so it may only be called for unused imports (imports from modules that
|
|
|
|
|
* aren't referenced).
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
/* Safety */
|
|
|
|
|
PRECONDITION ((I->Flags & IMP_INLIST) == 0);
|
|
|
|
|
|
|
|
|
|
/* Free the struct */
|
|
|
|
|
xfree (I);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Import* ReadImport (FILE* F, ObjData* Obj)
|
|
|
|
|
/* Read an import from a file and return it */
|
|
|
|
|
{
|
|
|
|
|
Import* I;
|
|
|
|
|
|
|
|
|
|
/* Read the import type and check it */
|
|
|
|
|
unsigned char Type = Read8 (F);
|
|
|
|
|
if (Type != IMP_ZP && Type != IMP_ABS) {
|
2000-11-20 21:56:48 +00:00
|
|
|
|
Error ("Unknown import type in module `%s': %02X",
|
|
|
|
|
GetObjFileName (Obj), Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Create a new import */
|
|
|
|
|
I = NewImport (Type, Obj);
|
|
|
|
|
|
|
|
|
|
/* Read the name */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
I->Name = MakeGlobalStringId (Obj, ReadVar (F));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Read the file position */
|
|
|
|
|
ReadFilePos (F, &I->Pos);
|
|
|
|
|
|
|
|
|
|
/* Return the new import */
|
|
|
|
|
return I;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Code */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
static Export* NewExport (unsigned char Type, unsigned Name, ObjData* Obj)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Create a new export and initialize it */
|
|
|
|
|
{
|
|
|
|
|
/* Allocate memory */
|
2000-08-02 13:23:06 +00:00
|
|
|
|
Export* E = xmalloc (sizeof (Export));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Initialize the fields */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
E->Name = Name;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
E->Next = 0;
|
2000-11-20 15:22:57 +00:00
|
|
|
|
E->Flags = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
E->Obj = Obj;
|
|
|
|
|
E->ImpCount = 0;
|
|
|
|
|
E->ImpList = 0;
|
|
|
|
|
E->Expr = 0;
|
|
|
|
|
E->Type = Type;
|
2000-11-20 15:22:57 +00:00
|
|
|
|
memset (E->ConDes, 0, sizeof (E->ConDes));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Return the new entry */
|
|
|
|
|
return E;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
void FreeExport (Export* E)
|
|
|
|
|
/* Free an export. NOTE: This won't remove the export from the exports table,
|
|
|
|
|
* so it may only be called for unused exports (exports from modules that
|
|
|
|
|
* aren't referenced).
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
/* Safety */
|
|
|
|
|
PRECONDITION ((E->Flags & EXP_INLIST) == 0);
|
|
|
|
|
|
|
|
|
|
/* Free the export expression */
|
|
|
|
|
FreeExpr (E->Expr);
|
|
|
|
|
|
|
|
|
|
/* Free the struct */
|
|
|
|
|
xfree (E);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
void InsertExport (Export* E)
|
|
|
|
|
/* Insert an exported identifier and check if it's already in the list */
|
|
|
|
|
{
|
|
|
|
|
Export* L;
|
|
|
|
|
Export* Last;
|
|
|
|
|
Import* Imp;
|
2003-06-04 12:40:14 +00:00
|
|
|
|
unsigned Hash;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
/* Mark the export as inserted */
|
|
|
|
|
E->Flags |= EXP_INLIST;
|
|
|
|
|
|
2000-11-20 15:22:57 +00:00
|
|
|
|
/* Insert the export into any condes tables if needed */
|
|
|
|
|
if (IS_EXP_CONDES (E->Type)) {
|
|
|
|
|
ConDesAddExport (E);
|
2000-10-30 20:48:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Create a hash value for the given name */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Hash = (E->Name & HASHTAB_MASK);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Search through the list in that slot */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
if (HashTab[Hash] == 0) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* The slot is empty */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
HashTab[Hash] = E;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
++ExpCount;
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
Last = 0;
|
2003-06-04 12:40:14 +00:00
|
|
|
|
L = HashTab[Hash];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
do {
|
2003-06-05 16:50:01 +00:00
|
|
|
|
if (L->Name == E->Name) {
|
2003-06-04 12:40:14 +00:00
|
|
|
|
/* This may be an unresolved external */
|
|
|
|
|
if (L->Expr == 0) {
|
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
/* This *is* an unresolved external. Use the actual export
|
|
|
|
|
* in E instead of the dummy one in L.
|
|
|
|
|
*/
|
2003-06-04 12:40:14 +00:00
|
|
|
|
E->Next = L->Next;
|
|
|
|
|
E->ImpCount = L->ImpCount;
|
|
|
|
|
E->ImpList = L->ImpList;
|
2003-06-05 16:50:01 +00:00
|
|
|
|
if (Last) {
|
|
|
|
|
Last->Next = E;
|
|
|
|
|
} else {
|
2003-06-04 12:40:14 +00:00
|
|
|
|
HashTab[Hash] = E;
|
2003-06-05 16:50:01 +00:00
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
ImpOpen -= E->ImpCount; /* Decrease open imports now */
|
2003-06-05 16:50:01 +00:00
|
|
|
|
xfree (L);
|
|
|
|
|
/* We must run through the import list and change the
|
2000-11-03 09:31:18 +00:00
|
|
|
|
* export pointer now.
|
2003-06-05 16:50:01 +00:00
|
|
|
|
*/
|
|
|
|
|
Imp = E->ImpList;
|
|
|
|
|
while (Imp) {
|
|
|
|
|
Imp->Exp = E;
|
|
|
|
|
Imp = Imp->Next;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
/* Duplicate entry, ignore it */
|
|
|
|
|
Warning ("Duplicate external identifier: `%s'",
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetString (L->Name));
|
2003-06-05 16:50:01 +00:00
|
|
|
|
}
|
|
|
|
|
return;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
Last = L;
|
|
|
|
|
L = L->Next;
|
|
|
|
|
|
|
|
|
|
} while (L);
|
|
|
|
|
|
|
|
|
|
/* Insert export at end of queue */
|
|
|
|
|
Last->Next = E;
|
|
|
|
|
++ExpCount;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Export* ReadExport (FILE* F, ObjData* O)
|
|
|
|
|
/* Read an export from a file */
|
|
|
|
|
{
|
|
|
|
|
unsigned char Type;
|
2000-11-20 15:22:57 +00:00
|
|
|
|
unsigned ConDesCount;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
Export* E;
|
|
|
|
|
|
|
|
|
|
/* Read the type */
|
|
|
|
|
Type = Read8 (F);
|
|
|
|
|
|
2000-08-02 13:23:06 +00:00
|
|
|
|
/* Create a new export without a name */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
E = NewExport (Type, INVALID_STRING_ID, O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-11-20 15:22:57 +00:00
|
|
|
|
/* Read the constructor/destructor decls if we have any */
|
|
|
|
|
ConDesCount = GET_EXP_CONDES_COUNT (Type);
|
|
|
|
|
if (ConDesCount > 0) {
|
|
|
|
|
|
|
|
|
|
unsigned char ConDes[CD_TYPE_COUNT];
|
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
|
|
/* Read the data into temp storage */
|
|
|
|
|
ReadData (F, ConDes, ConDesCount);
|
|
|
|
|
|
|
|
|
|
/* Re-order the data. In the file, each decl is encoded into a byte
|
|
|
|
|
* which contains the type and the priority. In memory, we will use
|
|
|
|
|
* an array of types which contain the priority. This array was
|
|
|
|
|
* cleared by the constructor (NewExport), so we must only set the
|
|
|
|
|
* fields that contain values.
|
|
|
|
|
*/
|
|
|
|
|
for (I = 0; I < ConDesCount; ++I) {
|
|
|
|
|
unsigned ConDesType = CD_GET_TYPE (ConDes[I]);
|
|
|
|
|
unsigned ConDesPrio = CD_GET_PRIO (ConDes[I]);
|
|
|
|
|
E->ConDes[ConDesType] = ConDesPrio;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2000-08-02 13:23:06 +00:00
|
|
|
|
/* Read the name */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
E->Name = MakeGlobalStringId (O, ReadVar (F));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Read the value */
|
2000-10-30 20:48:11 +00:00
|
|
|
|
if (IS_EXP_EXPR (Type)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
E->Expr = ReadExpr (F, O);
|
|
|
|
|
} else {
|
|
|
|
|
E->Expr = LiteralExpr (Read32 (F), O);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Last is the file position where the definition was done */
|
|
|
|
|
ReadFilePos (F, &E->Pos);
|
|
|
|
|
|
|
|
|
|
/* Return the new export */
|
|
|
|
|
return E;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Export* CreateConstExport (unsigned Name, long Value)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Create an export for a literal date */
|
|
|
|
|
{
|
|
|
|
|
/* Create a new export */
|
2001-09-08 22:09:30 +00:00
|
|
|
|
Export* E = NewExport (EXP_ABS | EXP_CONST | EXP_EQUATE, Name, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Assign the value */
|
|
|
|
|
E->Expr = LiteralExpr (Value, 0);
|
|
|
|
|
|
|
|
|
|
/* Insert the export */
|
|
|
|
|
InsertExport (E);
|
|
|
|
|
|
|
|
|
|
/* Return the new export */
|
|
|
|
|
return E;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Export* CreateMemoryExport (unsigned Name, Memory* Mem, unsigned long Offs)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Create an relative export for a memory area offset */
|
|
|
|
|
{
|
|
|
|
|
/* Create a new export */
|
2001-09-08 22:09:30 +00:00
|
|
|
|
Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Assign the value */
|
2002-12-14 22:57:00 +00:00
|
|
|
|
E->Expr = MemoryExpr (Mem, Offs, 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Insert the export */
|
|
|
|
|
InsertExport (E);
|
|
|
|
|
|
|
|
|
|
/* Return the new export */
|
|
|
|
|
return E;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Export* CreateSegmentExport (unsigned Name, Segment* Seg, unsigned long Offs)
|
2002-12-14 22:57:00 +00:00
|
|
|
|
/* Create a relative export to a segment */
|
2000-11-20 21:56:48 +00:00
|
|
|
|
{
|
|
|
|
|
/* Create a new export */
|
2001-09-08 22:09:30 +00:00
|
|
|
|
Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
|
2000-11-20 21:56:48 +00:00
|
|
|
|
|
|
|
|
|
/* Assign the value */
|
2002-12-14 22:57:00 +00:00
|
|
|
|
E->Expr = SegmentExpr (Seg, Offs, 0);
|
|
|
|
|
|
|
|
|
|
/* Insert the export */
|
|
|
|
|
InsertExport (E);
|
|
|
|
|
|
|
|
|
|
/* Return the new export */
|
|
|
|
|
return E;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Export* CreateSectionExport (unsigned Name, Section* Sec, unsigned long Offs)
|
2002-12-14 22:57:00 +00:00
|
|
|
|
/* Create a relative export to a section */
|
|
|
|
|
{
|
|
|
|
|
/* Create a new export */
|
|
|
|
|
Export* E = NewExport (EXP_ABS | EXP_EXPR | EXP_LABEL, Name, 0);
|
|
|
|
|
|
|
|
|
|
/* Assign the value */
|
|
|
|
|
E->Expr = SectionExpr (Sec, Offs, 0);
|
2000-11-20 21:56:48 +00:00
|
|
|
|
|
|
|
|
|
/* Insert the export */
|
|
|
|
|
InsertExport (E);
|
|
|
|
|
|
|
|
|
|
/* Return the new export */
|
|
|
|
|
return E;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Export* FindExport (unsigned Name)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Check for an identifier in the list. Return 0 if not found, otherwise
|
2001-12-01 17:14:12 +00:00
|
|
|
|
* return a pointer to the export.
|
2000-05-28 13:40:48 +00:00
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
/* Get a pointer to the list with the symbols hash value */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Export* L = HashTab[Name & HASHTAB_MASK];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
while (L) {
|
|
|
|
|
/* Search through the list in that slot */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
if (L->Name == Name) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Entry found */
|
|
|
|
|
return L;
|
|
|
|
|
}
|
|
|
|
|
L = L->Next;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Not found */
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
|
int IsUnresolved (unsigned Name)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Check if this symbol is an unresolved export */
|
|
|
|
|
{
|
|
|
|
|
/* Find the export */
|
2001-12-01 17:14:12 +00:00
|
|
|
|
return IsUnresolvedExport (FindExport (Name));
|
|
|
|
|
}
|
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-12-01 17:14:12 +00:00
|
|
|
|
|
|
|
|
|
int IsUnresolvedExport (const Export* E)
|
|
|
|
|
/* Return true if the given export is unresolved */
|
|
|
|
|
{
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Check if it's unresolved */
|
|
|
|
|
return E != 0 && E->Expr == 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int IsConstExport (const Export* E)
|
|
|
|
|
/* Return true if the expression associated with this export is const */
|
|
|
|
|
{
|
|
|
|
|
if (E->Expr == 0) {
|
|
|
|
|
/* External symbols cannot be const */
|
|
|
|
|
return 0;
|
|
|
|
|
} else {
|
|
|
|
|
return IsConstExpr (E->Expr);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
long GetExportVal (const Export* E)
|
|
|
|
|
/* Get the value of this export */
|
|
|
|
|
{
|
|
|
|
|
if (E->Expr == 0) {
|
|
|
|
|
/* OOPS */
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Internal ("`%s' is an undefined external", GetString (E->Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
return GetExprVal (E->Expr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-10-30 20:48:11 +00:00
|
|
|
|
static void CheckSymType (const Export* E)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Check the types for one export */
|
|
|
|
|
{
|
|
|
|
|
/* External with matching imports */
|
|
|
|
|
Import* Imp = E->ImpList;
|
2000-10-30 20:48:11 +00:00
|
|
|
|
int ZP = IS_EXP_ZP (E->Type);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
while (Imp) {
|
2000-10-30 20:48:11 +00:00
|
|
|
|
if (ZP != IS_IMP_ZP (Imp->Type)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Export is ZP, import is abs or the other way round */
|
|
|
|
|
if (E->Obj) {
|
2000-10-30 20:48:11 +00:00
|
|
|
|
/* User defined export */
|
|
|
|
|
Warning ("Type mismatch for `%s', export in "
|
2000-05-28 13:40:48 +00:00
|
|
|
|
"%s(%lu), import in %s(%lu)",
|
2003-06-05 16:50:01 +00:00
|
|
|
|
GetString (E->Name),
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetSourceFileName (E->Obj, Imp->Pos.Name),
|
2003-06-05 16:50:01 +00:00
|
|
|
|
E->Pos.Line,
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetSourceFileName (Imp->Obj, Imp->Pos.Name),
|
2000-05-28 13:40:48 +00:00
|
|
|
|
Imp->Pos.Line);
|
|
|
|
|
} else {
|
|
|
|
|
/* Export created by the linker */
|
|
|
|
|
Warning ("Type mismatch for `%s', imported from %s(%lu)",
|
2003-06-05 16:50:01 +00:00
|
|
|
|
GetString (E->Name),
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetSourceFileName (Imp->Obj, Imp->Pos.Name),
|
2000-05-28 13:40:48 +00:00
|
|
|
|
Imp->Pos.Line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Imp = Imp->Next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckSymTypes (void)
|
|
|
|
|
/* Check for symbol tape mismatches */
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
|
|
/* Print all open imports */
|
|
|
|
|
for (I = 0; I < ExpCount; ++I) {
|
2000-10-30 20:48:11 +00:00
|
|
|
|
const Export* E = ExpPool [I];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
if (E->Expr != 0 && E->ImpCount > 0) {
|
|
|
|
|
/* External with matching imports */
|
|
|
|
|
CheckSymType (E);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void PrintUnresolved (ExpCheckFunc F, void* Data)
|
|
|
|
|
/* Print a list of unresolved symbols. On unresolved symbols, F is
|
|
|
|
|
* called (see the comments on ExpCheckFunc in the data section).
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
|
|
/* Print all open imports */
|
|
|
|
|
for (I = 0; I < ExpCount; ++I) {
|
|
|
|
|
Export* E = ExpPool [I];
|
|
|
|
|
if (E->Expr == 0 && E->ImpCount > 0 && F (E->Name, Data) == 0) {
|
|
|
|
|
/* Unresolved external */
|
|
|
|
|
Import* Imp = E->ImpList;
|
|
|
|
|
fprintf (stderr,
|
|
|
|
|
"Unresolved external `%s' referenced in:\n",
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetString (E->Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
while (Imp) {
|
2000-11-20 21:56:48 +00:00
|
|
|
|
const char* Name = GetSourceFileName (Imp->Obj, Imp->Pos.Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
fprintf (stderr, " %s(%lu)\n", Name, Imp->Pos.Line);
|
|
|
|
|
Imp = Imp->Next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int CmpExpName (const void* K1, const void* K2)
|
|
|
|
|
/* Compare function for qsort */
|
|
|
|
|
{
|
2003-06-05 16:50:01 +00:00
|
|
|
|
return strcmp (GetString ((*(Export**)K1)->Name),
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetString ((*(Export**)K2)->Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void CreateExportPool (void)
|
|
|
|
|
/* Create an array with pointer to all exports */
|
|
|
|
|
{
|
|
|
|
|
unsigned I, J;
|
|
|
|
|
|
|
|
|
|
/* Allocate memory */
|
|
|
|
|
if (ExpPool) {
|
2000-06-14 09:57:42 +00:00
|
|
|
|
xfree (ExpPool);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
2000-06-14 09:57:42 +00:00
|
|
|
|
ExpPool = xmalloc (ExpCount * sizeof (Export*));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Walk through the list and insert the exports */
|
|
|
|
|
for (I = 0, J = 0; I < sizeof (HashTab) / sizeof (HashTab [0]); ++I) {
|
2003-06-04 12:40:14 +00:00
|
|
|
|
Export* E = HashTab[I];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
while (E) {
|
|
|
|
|
CHECK (J < ExpCount);
|
2003-06-04 12:40:14 +00:00
|
|
|
|
ExpPool[J++] = E;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
E = E->Next;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Sort them by name */
|
|
|
|
|
qsort (ExpPool, ExpCount, sizeof (Export*), CmpExpName);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CheckExports (ExpCheckFunc F, void* Data)
|
|
|
|
|
/* Check if there are any unresolved symbols. On unresolved symbols, F is
|
|
|
|
|
* called (see the comments on ExpCheckFunc in the data section).
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
/* Create an export pool */
|
|
|
|
|
CreateExportPool ();
|
|
|
|
|
|
|
|
|
|
/* Check for symbol type mismatches */
|
|
|
|
|
CheckSymTypes ();
|
|
|
|
|
|
|
|
|
|
/* Check for unresolved externals (check here for special bin formats) */
|
|
|
|
|
if (ImpOpen != 0) {
|
|
|
|
|
/* Print all open imports */
|
|
|
|
|
PrintUnresolved (F, Data);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintExportMap (FILE* F)
|
|
|
|
|
/* Print an export map to the given file */
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
|
|
|
|
unsigned Count;
|
|
|
|
|
|
|
|
|
|
/* Print all exports */
|
|
|
|
|
Count = 0;
|
|
|
|
|
for (I = 0; I < ExpCount; ++I) {
|
2000-10-30 20:48:11 +00:00
|
|
|
|
const Export* E = ExpPool [I];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Print unreferenced symbols only if explictly requested */
|
2000-11-20 21:56:48 +00:00
|
|
|
|
if (VerboseMap || E->ImpCount > 0 || IS_EXP_CONDES (E->Type)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
fprintf (F,
|
2001-09-08 22:09:30 +00:00
|
|
|
|
"%-25s %06lX %c%c%c%c ",
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetString (E->Name),
|
2000-10-30 20:48:11 +00:00
|
|
|
|
GetExportVal (E),
|
|
|
|
|
E->ImpCount? 'R' : ' ',
|
2001-09-08 22:09:30 +00:00
|
|
|
|
IS_EXP_LABEL (E->Type)? 'L' : 'E',
|
2000-10-30 20:48:11 +00:00
|
|
|
|
IS_EXP_ZP (E->Type)? 'Z' : ' ',
|
2000-11-20 15:22:57 +00:00
|
|
|
|
IS_EXP_CONDES (E->Type)? 'I' : ' ');
|
2000-05-28 13:40:48 +00:00
|
|
|
|
if (++Count == 2) {
|
2000-10-30 20:48:11 +00:00
|
|
|
|
Count = 0;
|
|
|
|
|
fprintf (F, "\n");
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fprintf (F, "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintImportMap (FILE* F)
|
|
|
|
|
/* Print an import map to the given file */
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
2000-10-30 20:48:11 +00:00
|
|
|
|
const Import* Imp;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Loop over all exports */
|
|
|
|
|
for (I = 0; I < ExpCount; ++I) {
|
|
|
|
|
|
|
|
|
|
/* Get the export */
|
2000-10-30 20:48:11 +00:00
|
|
|
|
const Export* Exp = ExpPool [I];
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Print the symbol only if there are imports, or if a verbose map
|
|
|
|
|
* file is requested.
|
|
|
|
|
*/
|
|
|
|
|
if (VerboseMap || Exp->ImpCount > 0) {
|
|
|
|
|
|
|
|
|
|
/* Print the export */
|
|
|
|
|
fprintf (F,
|
2000-10-30 20:48:11 +00:00
|
|
|
|
"%s (%s):\n",
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetString (Exp->Name),
|
2000-11-20 21:56:48 +00:00
|
|
|
|
GetObjFileName (Exp->Obj));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Print all imports for this symbol */
|
|
|
|
|
Imp = Exp->ImpList;
|
|
|
|
|
while (Imp) {
|
|
|
|
|
|
2000-10-30 20:48:11 +00:00
|
|
|
|
/* Print the import */
|
|
|
|
|
fprintf (F,
|
|
|
|
|
" %-25s %s(%lu)\n",
|
2000-11-20 21:56:48 +00:00
|
|
|
|
GetObjFileName (Imp->Obj),
|
|
|
|
|
GetSourceFileName (Imp->Obj, Imp->Pos.Name),
|
2000-10-30 20:48:11 +00:00
|
|
|
|
Imp->Pos.Line);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-10-30 20:48:11 +00:00
|
|
|
|
/* Next import */
|
|
|
|
|
Imp = Imp->Next;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fprintf (F, "\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void PrintExportLabels (FILE* F)
|
|
|
|
|
/* Print the exports in a VICE label file */
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
|
|
/* Print all exports */
|
|
|
|
|
for (I = 0; I < ExpCount; ++I) {
|
2000-10-30 20:48:11 +00:00
|
|
|
|
const Export* E = ExpPool [I];
|
2003-06-04 12:40:14 +00:00
|
|
|
|
fprintf (F, "al %06lX .%s\n", GetExportVal (E), GetString (E->Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void MarkExport (Export* E)
|
|
|
|
|
/* Mark the export */
|
|
|
|
|
{
|
|
|
|
|
E->Flags |= EXP_USERMARK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void UnmarkExport (Export* E)
|
|
|
|
|
/* Remove the mark from the export */
|
|
|
|
|
{
|
|
|
|
|
E->Flags &= ~EXP_USERMARK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int ExportHasMark (Export* E)
|
|
|
|
|
/* Return true if the export has a mark */
|
|
|
|
|
{
|
|
|
|
|
return (E->Flags & EXP_USERMARK) != 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void CircularRefError (const Export* E)
|
|
|
|
|
/* Print an error about a circular reference using to define the given export */
|
|
|
|
|
{
|
|
|
|
|
Error ("Circular reference for symbol `%s', %s(%lu)",
|
2003-06-05 16:50:01 +00:00
|
|
|
|
GetString (E->Name),
|
2003-06-04 12:40:14 +00:00
|
|
|
|
GetSourceFileName (E->Obj, E->Pos.Name),
|
|
|
|
|
E->Pos.Line);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
|