2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* library.c */
|
|
|
|
|
/* */
|
|
|
|
|
/* Library data structures and helpers for the ld65 linker */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
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 <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
2000-08-02 13:23:06 +00:00
|
|
|
|
/* common */
|
|
|
|
|
#include "exprdefs.h"
|
|
|
|
|
#include "filepos.h"
|
|
|
|
|
#include "libdefs.h"
|
|
|
|
|
#include "objdefs.h"
|
|
|
|
|
#include "symdefs.h"
|
|
|
|
|
#include "xmalloc.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2000-08-02 13:23:06 +00:00
|
|
|
|
/* ld65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "error.h"
|
2000-06-14 09:57:42 +00:00
|
|
|
|
#include "exports.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "fileio.h"
|
2003-06-05 16:50:01 +00:00
|
|
|
|
#include "library.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
#include "objdata.h"
|
|
|
|
|
#include "objfile.h"
|
2003-06-05 16:50:01 +00:00
|
|
|
|
#include "spool.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2000-08-02 13:23:06 +00:00
|
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Library data */
|
|
|
|
|
static FILE* Lib = 0;
|
|
|
|
|
static unsigned ModuleCount = 0;
|
|
|
|
|
static ObjData** Index = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2003-06-05 16:50:01 +00:00
|
|
|
|
/* Reading file data structures */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
static void LibReadObjHeader (ObjData* O, const char* LibName)
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Read the header of the object file checking the signature */
|
|
|
|
|
{
|
2001-05-29 07:39:46 +00:00
|
|
|
|
O->Header.Magic = Read32 (Lib);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
if (O->Header.Magic != OBJ_MAGIC) {
|
2001-05-29 07:39:46 +00:00
|
|
|
|
Error ("Object file `%s' in library `%s' is invalid",
|
2000-11-20 21:56:48 +00:00
|
|
|
|
GetObjFileName (O), LibName);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
2001-05-29 07:39:46 +00:00
|
|
|
|
O->Header.Version = Read16 (Lib);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
if (O->Header.Version != OBJ_VERSION) {
|
|
|
|
|
Error ("Object file `%s' in library `%s' has wrong version",
|
2000-11-20 21:56:48 +00:00
|
|
|
|
GetObjFileName (O), LibName);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
2001-05-29 07:39:46 +00:00
|
|
|
|
O->Header.Flags = Read16 (Lib);
|
|
|
|
|
O->Header.OptionOffs = Read32 (Lib);
|
|
|
|
|
O->Header.OptionSize = Read32 (Lib);
|
|
|
|
|
O->Header.FileOffs = Read32 (Lib);
|
|
|
|
|
O->Header.FileSize = Read32 (Lib);
|
|
|
|
|
O->Header.SegOffs = Read32 (Lib);
|
|
|
|
|
O->Header.SegSize = Read32 (Lib);
|
|
|
|
|
O->Header.ImportOffs = Read32 (Lib);
|
|
|
|
|
O->Header.ImportSize = Read32 (Lib);
|
|
|
|
|
O->Header.ExportOffs = Read32 (Lib);
|
|
|
|
|
O->Header.ExportSize = Read32 (Lib);
|
|
|
|
|
O->Header.DbgSymOffs = Read32 (Lib);
|
|
|
|
|
O->Header.DbgSymSize = Read32 (Lib);
|
|
|
|
|
O->Header.LineInfoOffs = Read32 (Lib);
|
|
|
|
|
O->Header.LineInfoSize = Read32 (Lib);
|
2003-05-25 17:57:50 +00:00
|
|
|
|
O->Header.StrPoolOffs = Read32 (Lib);
|
|
|
|
|
O->Header.StrPoolSize = Read32 (Lib);
|
2003-06-06 12:45:19 +00:00
|
|
|
|
O->Header.AssertOffs = Read32 (Lib);
|
|
|
|
|
O->Header.AssertSize = Read32 (Lib);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static ObjData* ReadIndexEntry (void)
|
|
|
|
|
/* Read one entry in the index */
|
|
|
|
|
{
|
|
|
|
|
/* Create a new entry and insert it into the list */
|
|
|
|
|
ObjData* O = NewObjData ();
|
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
/* Module name */
|
2003-06-06 06:50:27 +00:00
|
|
|
|
O->Name = ReadStr (Lib);
|
2003-06-05 16:50:01 +00:00
|
|
|
|
|
|
|
|
|
/* Module flags/MTime/Start/Size */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
O->Flags = Read16 (Lib);
|
2003-06-06 06:50:27 +00:00
|
|
|
|
O->MTime = Read32 (Lib);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
O->Start = Read32 (Lib);
|
|
|
|
|
Read32 (Lib); /* Skip Size */
|
|
|
|
|
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* Read the string pool */
|
2003-06-06 12:45:19 +00:00
|
|
|
|
ObjReadStrPool (Lib, FileGetPos (Lib), O);
|
2003-05-25 17:57:50 +00:00
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* Skip the export size, then read the exports */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
(void) ReadVar (Lib);
|
2003-06-06 12:45:19 +00:00
|
|
|
|
ObjReadExports (Lib, FileGetPos (Lib), O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Skip the import size, then read the imports */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
(void) ReadVar (Lib);
|
2003-06-06 12:45:19 +00:00
|
|
|
|
ObjReadImports (Lib, FileGetPos (Lib), O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Done */
|
|
|
|
|
return O;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ReadIndex (void)
|
|
|
|
|
/* Read the index of a library file */
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
|
|
/* Read the object file count and allocate memory */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
ModuleCount = ReadVar (Lib);
|
2003-06-06 12:45:19 +00:00
|
|
|
|
Index = xmalloc (ModuleCount * sizeof (Index[0]));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Read all entries in the index */
|
|
|
|
|
for (I = 0; I < ModuleCount; ++I) {
|
2003-05-25 17:57:50 +00:00
|
|
|
|
Index[I] = ReadIndexEntry ();
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* High level stuff */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void LibCheckExports (ObjData* O)
|
|
|
|
|
/* Check if the exports from this file can satisfy any import requests. If so,
|
|
|
|
|
* insert the imports and exports from this file and mark the file as added.
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
|
|
/* Check all exports */
|
|
|
|
|
for (I = 0; I < O->ExportCount; ++I) {
|
2003-05-25 17:57:50 +00:00
|
|
|
|
if (IsUnresolved (O->Exports[I]->Name)) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
/* We need this module */
|
|
|
|
|
O->Flags |= OBJ_REF;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* If we need this module, insert the imports and exports */
|
|
|
|
|
if (O->Flags & OBJ_REF) {
|
2003-06-06 12:45:19 +00:00
|
|
|
|
InsertObjGlobals (O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LibAdd (FILE* F, const char* Name)
|
|
|
|
|
/* Add files from the library to the list if there are references that could
|
|
|
|
|
* be satisfied.
|
|
|
|
|
*/
|
|
|
|
|
{
|
2003-06-05 16:50:01 +00:00
|
|
|
|
unsigned LibName;
|
|
|
|
|
int HaveAdditions;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
unsigned I;
|
|
|
|
|
LibHeader Header;
|
|
|
|
|
|
|
|
|
|
/* Store the parameters, so they're visible for other routines */
|
|
|
|
|
Lib = F;
|
2003-06-05 16:50:01 +00:00
|
|
|
|
LibName = GetStringId (Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Read the remaining header fields (magic is already read) */
|
|
|
|
|
Header.Magic = LIB_MAGIC;
|
|
|
|
|
Header.Version = Read16 (Lib);
|
|
|
|
|
if (Header.Version != LIB_VERSION) {
|
2003-05-25 17:57:50 +00:00
|
|
|
|
Error ("Wrong data version in `%s'", Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
Header.Flags = Read16 (Lib);
|
|
|
|
|
Header.IndexOffs = Read32 (Lib);
|
|
|
|
|
|
|
|
|
|
/* Seek to the index position and read the index */
|
|
|
|
|
fseek (Lib, Header.IndexOffs, SEEK_SET);
|
|
|
|
|
ReadIndex ();
|
|
|
|
|
|
|
|
|
|
/* Walk through all library modules and check for each module if there
|
|
|
|
|
* are unresolved externals in existing modules that may be resolved
|
|
|
|
|
* by adding the module. Repeat this step until no more object files
|
|
|
|
|
* were added.
|
|
|
|
|
*/
|
|
|
|
|
do {
|
2003-06-05 16:50:01 +00:00
|
|
|
|
HaveAdditions = 0;
|
|
|
|
|
for (I = 0; I < ModuleCount; ++I) {
|
|
|
|
|
ObjData* O = Index [I];
|
|
|
|
|
if ((O->Flags & OBJ_REF) == 0) {
|
|
|
|
|
LibCheckExports (O);
|
|
|
|
|
if (O->Flags & OBJ_REF) {
|
|
|
|
|
/* The routine added the file */
|
|
|
|
|
HaveAdditions = 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} while (HaveAdditions);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
/* Add the files list and sections for all requested modules */
|
|
|
|
|
for (I = 0; I < ModuleCount; ++I) {
|
2003-06-05 16:50:01 +00:00
|
|
|
|
|
|
|
|
|
/* Get the object data */
|
2001-05-29 07:39:46 +00:00
|
|
|
|
ObjData* O = Index [I];
|
2003-06-05 16:50:01 +00:00
|
|
|
|
|
|
|
|
|
/* Is this object file referenced? */
|
2001-05-29 07:39:46 +00:00
|
|
|
|
if (O->Flags & OBJ_REF) {
|
|
|
|
|
|
|
|
|
|
/* Seek to the start of the object file and read the header */
|
|
|
|
|
fseek (Lib, O->Start, SEEK_SET);
|
2003-06-05 16:50:01 +00:00
|
|
|
|
LibReadObjHeader (O, Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-29 07:39:46 +00:00
|
|
|
|
/* Seek to the start of the files list and read the files list */
|
2003-06-06 12:45:19 +00:00
|
|
|
|
ObjReadFiles (Lib, O->Start + O->Header.FileOffs, O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-29 07:39:46 +00:00
|
|
|
|
/* Seek to the start of the debug info and read the debug info */
|
2003-06-06 12:45:19 +00:00
|
|
|
|
ObjReadDbgSyms (Lib, O->Start + O->Header.DbgSymOffs, O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-29 07:39:46 +00:00
|
|
|
|
/* Seek to the start of the line infos and read them */
|
2003-06-06 12:45:19 +00:00
|
|
|
|
ObjReadLineInfos (Lib, O->Start + O->Header.LineInfoOffs, O);
|
|
|
|
|
|
|
|
|
|
/* Read the assertions from the object file */
|
|
|
|
|
ObjReadAssertions (Lib, O->Start + O->Header.AssertOffs, O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2001-05-29 07:39:46 +00:00
|
|
|
|
/* Seek to the start of the segment list and read the segments.
|
|
|
|
|
* This must be last, since the data here may reference other
|
|
|
|
|
* stuff.
|
|
|
|
|
*/
|
2003-06-06 12:45:19 +00:00
|
|
|
|
ObjReadSections (Lib, O->Start + O->Header.SegOffs, O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
/* Add a pointer to the library name */
|
|
|
|
|
O->LibName = LibName;
|
|
|
|
|
|
|
|
|
|
/* All references to strings are now resolved, so we can delete
|
|
|
|
|
* the module string pool.
|
|
|
|
|
*/
|
|
|
|
|
FreeObjStrings (O);
|
|
|
|
|
|
|
|
|
|
/* Insert the object into the list of all used object files */
|
|
|
|
|
InsertObjData (O);
|
|
|
|
|
|
|
|
|
|
} else {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
/* Unreferenced object file, remove it */
|
|
|
|
|
FreeObjData (O);
|
2003-06-04 12:40:14 +00:00
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Done. Close the file, release allocated memory */
|
|
|
|
|
fclose (F);
|
2000-06-14 09:57:42 +00:00
|
|
|
|
xfree (Index);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
Lib = 0;
|
|
|
|
|
ModuleCount = 0;
|
|
|
|
|
Index = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|