2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
2003-11-28 22:12:14 +00:00
|
|
|
/* */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* library.c */
|
2003-11-28 22:12:14 +00:00
|
|
|
/* */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Library data structures and helpers for the ld65 linker */
|
2003-11-28 22:12:14 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2011-01-27 22:43:33 +00:00
|
|
|
/* (C) 1998-2011, Ullrich von Bassewitz */
|
2010-07-30 20:58:51 +00:00
|
|
|
/* Roemerstrasse 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 */
|
2011-01-27 22:43:33 +00:00
|
|
|
#include "coll.h"
|
2000-08-02 13:23:06 +00:00
|
|
|
#include "exprdefs.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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2011-01-27 22:43:33 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Library data structure */
|
|
|
|
typedef struct Library Library;
|
|
|
|
struct Library {
|
2011-08-07 11:33:28 +00:00
|
|
|
unsigned Id; /* Id of library */
|
2005-03-26 16:26:11 +00:00
|
|
|
unsigned Name; /* String id of the name */
|
|
|
|
FILE* F; /* Open file stream */
|
|
|
|
LibHeader Header; /* Library header */
|
2011-01-27 22:43:33 +00:00
|
|
|
Collection Modules; /* Modules */
|
2005-03-26 16:26:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* List of open libraries */
|
|
|
|
static Collection OpenLibs = STATIC_COLLECTION_INITIALIZER;
|
|
|
|
|
2011-08-07 11:33:28 +00:00
|
|
|
/* List of used libraries */
|
2011-08-08 17:15:18 +00:00
|
|
|
static Collection LibraryList = STATIC_COLLECTION_INITIALIZER;
|
2011-08-07 11:33:28 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Flag for library grouping */
|
|
|
|
static int Grouping = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
/* struct Library */
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static Library* NewLibrary (FILE* F, const char* Name)
|
|
|
|
/* Create a new Library structure and return it */
|
|
|
|
{
|
|
|
|
/* Allocate memory */
|
|
|
|
Library* L = xmalloc (sizeof (*L));
|
|
|
|
|
|
|
|
/* Initialize the fields */
|
2011-08-07 11:33:28 +00:00
|
|
|
L->Id = ~0U;
|
2005-03-26 16:26:11 +00:00
|
|
|
L->Name = GetStringId (Name);
|
|
|
|
L->F = F;
|
2011-01-27 22:43:33 +00:00
|
|
|
L->Modules = EmptyCollection;
|
2005-03-26 16:26:11 +00:00
|
|
|
|
|
|
|
/* Return the new struct */
|
|
|
|
return L;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-07 11:33:28 +00:00
|
|
|
static void CloseLibrary (Library* L)
|
|
|
|
/* Close a library file and remove the list of modules */
|
2005-03-26 16:26:11 +00:00
|
|
|
{
|
|
|
|
/* Close the library file */
|
|
|
|
if (fclose (L->F) != 0) {
|
|
|
|
Error ("Error closing `%s': %s", GetString (L->Name), strerror (errno));
|
|
|
|
}
|
2011-08-07 11:33:28 +00:00
|
|
|
L->F = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void FreeLibrary (Library* L)
|
|
|
|
/* Free a library structure */
|
|
|
|
{
|
|
|
|
/* Close the library */
|
|
|
|
CloseLibrary (L);
|
2005-03-26 16:26:11 +00:00
|
|
|
|
|
|
|
/* Free the module index */
|
2011-01-27 22:43:33 +00:00
|
|
|
DoneCollection (&L->Modules);
|
2005-03-26 16:26:11 +00:00
|
|
|
|
|
|
|
/* Free the library structure */
|
|
|
|
xfree (L);
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2003-06-05 16:50:01 +00:00
|
|
|
/* Reading file data structures */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
static void LibSeek (Library* L, unsigned long Offs)
|
|
|
|
/* Do a seek in the library checking for errors */
|
|
|
|
{
|
|
|
|
if (fseek (L->F, Offs, SEEK_SET) != 0) {
|
|
|
|
Error ("Seek error in `%s' (%lu): %s",
|
|
|
|
GetString (L->Name), Offs, strerror (errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void LibReadHeader (Library* L)
|
|
|
|
/* Read a library header */
|
|
|
|
{
|
|
|
|
/* Read the remaining header fields (magic is already read) */
|
|
|
|
L->Header.Magic = LIB_MAGIC;
|
|
|
|
L->Header.Version = Read16 (L->F);
|
|
|
|
if (L->Header.Version != LIB_VERSION) {
|
|
|
|
Error ("Wrong data version in `%s'", GetString (L->Name));
|
|
|
|
}
|
|
|
|
L->Header.Flags = Read16 (L->F);
|
|
|
|
L->Header.IndexOffs = Read32 (L->F);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void LibReadObjHeader (Library* L, ObjData* O)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Read the header of the object file checking the signature */
|
|
|
|
{
|
2005-03-26 16:26:11 +00:00
|
|
|
O->Header.Magic = Read32 (L->F);
|
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",
|
2005-03-26 16:26:11 +00:00
|
|
|
GetObjFileName (O), GetString (L->Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2005-03-26 16:26:11 +00:00
|
|
|
O->Header.Version = Read16 (L->F);
|
2000-05-28 13:40:48 +00:00
|
|
|
if (O->Header.Version != OBJ_VERSION) {
|
|
|
|
Error ("Object file `%s' in library `%s' has wrong version",
|
2005-03-26 16:26:11 +00:00
|
|
|
GetObjFileName (O), GetString (L->Name));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2005-03-26 16:26:11 +00:00
|
|
|
O->Header.Flags = Read16 (L->F);
|
|
|
|
O->Header.OptionOffs = Read32 (L->F);
|
|
|
|
O->Header.OptionSize = Read32 (L->F);
|
|
|
|
O->Header.FileOffs = Read32 (L->F);
|
|
|
|
O->Header.FileSize = Read32 (L->F);
|
|
|
|
O->Header.SegOffs = Read32 (L->F);
|
|
|
|
O->Header.SegSize = Read32 (L->F);
|
|
|
|
O->Header.ImportOffs = Read32 (L->F);
|
|
|
|
O->Header.ImportSize = Read32 (L->F);
|
|
|
|
O->Header.ExportOffs = Read32 (L->F);
|
|
|
|
O->Header.ExportSize = Read32 (L->F);
|
|
|
|
O->Header.DbgSymOffs = Read32 (L->F);
|
|
|
|
O->Header.DbgSymSize = Read32 (L->F);
|
|
|
|
O->Header.LineInfoOffs = Read32 (L->F);
|
|
|
|
O->Header.LineInfoSize = Read32 (L->F);
|
|
|
|
O->Header.StrPoolOffs = Read32 (L->F);
|
|
|
|
O->Header.StrPoolSize = Read32 (L->F);
|
|
|
|
O->Header.AssertOffs = Read32 (L->F);
|
|
|
|
O->Header.AssertSize = Read32 (L->F);
|
|
|
|
O->Header.ScopeOffs = Read32 (L->F);
|
|
|
|
O->Header.ScopeSize = Read32 (L->F);
|
2011-08-21 19:08:23 +00:00
|
|
|
O->Header.SpanOffs = Read32 (L->F);
|
|
|
|
O->Header.SpanSize = Read32 (L->F);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
static ObjData* ReadIndexEntry (Library* L)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Read one entry in the index */
|
|
|
|
{
|
|
|
|
/* Create a new entry and insert it into the list */
|
|
|
|
ObjData* O = NewObjData ();
|
|
|
|
|
2011-01-28 16:03:55 +00:00
|
|
|
/* Remember from which library this module is */
|
2011-08-07 11:33:28 +00:00
|
|
|
O->Lib = L;
|
2011-01-28 16:03:55 +00:00
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
/* Module name */
|
2005-03-26 16:26:11 +00:00
|
|
|
O->Name = ReadStr (L->F);
|
2003-06-05 16:50:01 +00:00
|
|
|
|
|
|
|
/* Module flags/MTime/Start/Size */
|
2005-03-26 16:26:11 +00:00
|
|
|
O->Flags = Read16 (L->F);
|
|
|
|
O->MTime = Read32 (L->F);
|
|
|
|
O->Start = Read32 (L->F);
|
2011-08-21 19:08:23 +00:00
|
|
|
Read32 (L->F); /* Skip Size */
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2011-01-28 16:03:55 +00:00
|
|
|
/* Done */
|
|
|
|
return O;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void ReadBasicData (Library* L, ObjData* O)
|
|
|
|
/* Read basic data for an object file that is necessary to resolve external
|
|
|
|
* references.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* Seek to the start of the object file and read the header */
|
|
|
|
LibSeek (L, O->Start);
|
|
|
|
LibReadObjHeader (L, O);
|
|
|
|
|
2003-05-25 17:57:50 +00:00
|
|
|
/* Read the string pool */
|
2011-01-28 16:03:55 +00:00
|
|
|
ObjReadStrPool (L->F, O->Start + O->Header.StrPoolOffs, O);
|
2003-05-25 17:57:50 +00:00
|
|
|
|
2011-01-28 16:03:55 +00:00
|
|
|
/* Read the files list */
|
|
|
|
ObjReadFiles (L->F, O->Start + O->Header.FileOffs, O);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2011-08-21 19:08:23 +00:00
|
|
|
/* Read the spans */
|
|
|
|
ObjReadSpans (L->F, O->Start + O->Header.SpanOffs, O);
|
|
|
|
|
2011-01-28 16:03:55 +00:00
|
|
|
/* Read the line infos */
|
|
|
|
ObjReadLineInfos (L->F, O->Start + O->Header.LineInfoOffs, O);
|
2010-11-08 21:52:24 +00:00
|
|
|
|
2011-01-28 16:03:55 +00:00
|
|
|
/* Read the imports */
|
|
|
|
ObjReadImports (L->F, O->Start + O->Header.ImportOffs, O);
|
|
|
|
|
|
|
|
/* Read the exports */
|
|
|
|
ObjReadExports (L->F, O->Start + O->Header.ExportOffs, O);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
static void LibReadIndex (Library* L)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Read the index of a library file */
|
|
|
|
{
|
2011-01-28 16:03:55 +00:00
|
|
|
unsigned ModuleCount, I;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Seek to the start of the index */
|
|
|
|
LibSeek (L, L->Header.IndexOffs);
|
|
|
|
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Read the object file count and allocate memory */
|
2011-01-27 22:43:33 +00:00
|
|
|
ModuleCount = ReadVar (L->F);
|
|
|
|
CollGrow (&L->Modules, ModuleCount);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read all entries in the index */
|
2011-01-27 22:43:33 +00:00
|
|
|
while (ModuleCount--) {
|
|
|
|
CollAppend (&L->Modules, ReadIndexEntry (L));
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
2011-01-28 16:03:55 +00:00
|
|
|
|
|
|
|
/* Walk over the index and read basic data for all object files in the
|
|
|
|
* library.
|
|
|
|
*/
|
|
|
|
for (I = 0; I < CollCount (&L->Modules); ++I) {
|
|
|
|
ReadBasicData (L, CollAtUnchecked (&L->Modules, I));
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2005-03-26 16:26:11 +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 */
|
2010-07-30 20:58:51 +00:00
|
|
|
for (I = 0; I < CollCount (&O->Exports); ++I) {
|
|
|
|
const Export* E = CollConstAt (&O->Exports, I);
|
|
|
|
if (IsUnresolved (E->Name)) {
|
|
|
|
/* We need this module, insert the imports and exports */
|
|
|
|
O->Flags |= OBJ_REF;
|
|
|
|
InsertObjGlobals (O);
|
|
|
|
break;
|
2005-03-26 16:26:11 +00:00
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
static void LibOpen (FILE* F, const char* Name)
|
|
|
|
/* Open the library for use */
|
2000-05-28 13:40:48 +00:00
|
|
|
{
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Create a new library structure */
|
|
|
|
Library* L = NewLibrary (F, Name);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Read the remaining header fields (magic is already read) */
|
2005-03-26 16:26:11 +00:00
|
|
|
LibReadHeader (L);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Seek to the index position and read the index */
|
2005-03-26 16:26:11 +00:00
|
|
|
LibReadIndex (L);
|
|
|
|
|
|
|
|
/* Add the library to the list of open libraries */
|
|
|
|
CollAppend (&OpenLibs, L);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void LibResolve (void)
|
|
|
|
/* Resolve all externals from the list of all currently open libraries */
|
|
|
|
{
|
|
|
|
unsigned I, J;
|
|
|
|
unsigned Additions;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Walk repeatedly over all open libraries until there's nothing more
|
|
|
|
* to add.
|
2000-05-28 13:40:48 +00:00
|
|
|
*/
|
|
|
|
do {
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
Additions = 0;
|
|
|
|
|
|
|
|
/* Walk over all libraries */
|
|
|
|
for (I = 0; I < CollCount (&OpenLibs); ++I) {
|
2003-06-05 16:50:01 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Get the next library */
|
|
|
|
Library* L = CollAt (&OpenLibs, I);
|
2003-06-05 16:50:01 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Walk through all modules in this library and check for each
|
|
|
|
* module if there are unresolved externals in existing modules
|
|
|
|
* that may be resolved by adding the module.
|
|
|
|
*/
|
2011-01-27 22:43:33 +00:00
|
|
|
for (J = 0; J < CollCount (&L->Modules); ++J) {
|
2005-03-26 16:26:11 +00:00
|
|
|
|
|
|
|
/* Get the next module */
|
2011-01-27 22:43:33 +00:00
|
|
|
ObjData* O = CollAtUnchecked (&L->Modules, J);
|
2005-03-26 16:26:11 +00:00
|
|
|
|
|
|
|
/* We only need to check this module if it wasn't added before */
|
|
|
|
if ((O->Flags & OBJ_REF) == 0) {
|
|
|
|
LibCheckExports (O);
|
|
|
|
if (O->Flags & OBJ_REF) {
|
|
|
|
/* The routine added the file */
|
|
|
|
++Additions;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-05-29 07:39:46 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
} while (Additions > 0);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* We do know now which modules must be added, so we can load the data
|
|
|
|
* for these modues into memory. Since we're walking over all modules
|
|
|
|
* anyway, we will also remove data for unneeded modules.
|
|
|
|
*/
|
|
|
|
for (I = 0; I < CollCount (&OpenLibs); ++I) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Get the next library */
|
|
|
|
Library* L = CollAt (&OpenLibs, I);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Walk over all modules in this library and add the files list and
|
|
|
|
* sections for all referenced modules.
|
|
|
|
*/
|
2011-08-07 11:33:28 +00:00
|
|
|
J = 0;
|
|
|
|
while (J < CollCount (&L->Modules)) {
|
2003-06-06 12:45:19 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Get the object data */
|
2011-01-27 22:43:33 +00:00
|
|
|
ObjData* O = CollAtUnchecked (&L->Modules, J);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Is this object file referenced? */
|
|
|
|
if (O->Flags & OBJ_REF) {
|
2003-11-28 22:12:14 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Seek to the start of the debug info and read the debug info */
|
|
|
|
ObjReadDbgSyms (L->F, O->Start + O->Header.DbgSymOffs, O);
|
|
|
|
|
|
|
|
/* Read the assertions from the object file */
|
|
|
|
ObjReadAssertions (L->F, O->Start + O->Header.AssertOffs, O);
|
|
|
|
|
|
|
|
/* Seek to the start of the segment list and read the segments.
|
2011-08-04 13:20:45 +00:00
|
|
|
* This must be late, since the data here may reference other
|
2005-03-26 16:26:11 +00:00
|
|
|
* stuff.
|
|
|
|
*/
|
|
|
|
ObjReadSections (L->F, O->Start + O->Header.SegOffs, O);
|
|
|
|
|
2011-08-06 12:32:10 +00:00
|
|
|
/* Read the scope table from the object file. Scopes reference
|
2011-08-04 13:20:45 +00:00
|
|
|
* segments, so we must read them after the sections.
|
|
|
|
*/
|
|
|
|
ObjReadScopes (L->F, O->Start + O->Header.ScopeOffs, O);
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* All references to strings are now resolved, so we can delete
|
|
|
|
* the module string pool.
|
|
|
|
*/
|
|
|
|
FreeObjStrings (O);
|
2003-06-05 16:50:01 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Insert the object into the list of all used object files */
|
|
|
|
InsertObjData (O);
|
2003-06-05 16:50:01 +00:00
|
|
|
|
2011-08-07 11:33:28 +00:00
|
|
|
/* Process next object file in library */
|
|
|
|
++J;
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
} else {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* Unreferenced object file, remove it */
|
|
|
|
FreeObjData (O);
|
2011-08-07 11:33:28 +00:00
|
|
|
CollDelete (&L->Modules, J);
|
2003-06-04 12:40:14 +00:00
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
}
|
2003-06-05 16:50:01 +00:00
|
|
|
}
|
2005-03-26 16:26:11 +00:00
|
|
|
|
2011-08-07 11:33:28 +00:00
|
|
|
/* If we have referenced modules in this library, assign it an id
|
|
|
|
* (which is the index in the library collection) and keep it.
|
|
|
|
*/
|
|
|
|
if (CollCount (&L->Modules) > 0) {
|
|
|
|
CloseLibrary (L);
|
2011-08-08 17:15:18 +00:00
|
|
|
L->Id = CollCount (&LibraryList);
|
2011-08-08 17:18:52 +00:00
|
|
|
CollAppend (&LibraryList, L);
|
2011-08-07 11:33:28 +00:00
|
|
|
} else {
|
|
|
|
/* Delete the library */
|
|
|
|
FreeLibrary (L);
|
|
|
|
CollDelete (&OpenLibs, I);
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
2005-03-26 16:26:11 +00:00
|
|
|
/* We're done with all open libraries, clear the OpenLibs collection */
|
|
|
|
CollDeleteAll (&OpenLibs);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LibAdd (FILE* F, const char* Name)
|
|
|
|
/* Add files from the library to the list if there are references that could
|
|
|
|
* be satisfied.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* Add the library to the list of open libraries */
|
|
|
|
LibOpen (F, Name);
|
|
|
|
|
|
|
|
/* If there is no library group open, just resolve all open symbols and
|
|
|
|
* close the library. Otherwise we will do nothing because resolving will
|
|
|
|
* be done when the group is closed.
|
|
|
|
*/
|
|
|
|
if (!Grouping) {
|
|
|
|
LibResolve ();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LibStartGroup (void)
|
|
|
|
/* Start a library group. Objects within a library group may reference each
|
|
|
|
* other, and libraries are searched repeatedly until all references are
|
|
|
|
* satisfied.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* We cannot already have a group open */
|
|
|
|
if (Grouping) {
|
|
|
|
Error ("There's already a library group open");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start a new group */
|
|
|
|
Grouping = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LibEndGroup (void)
|
|
|
|
/* End a library group and resolve all open references. Objects within a
|
|
|
|
* library group may reference each other, and libraries are searched
|
|
|
|
* repeatedly until all references are satisfied.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* We must have a library group open */
|
|
|
|
if (!Grouping) {
|
|
|
|
Error ("There's no library group open");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resolve symbols, end the group */
|
|
|
|
LibResolve ();
|
|
|
|
Grouping = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void LibCheckGroup (void)
|
|
|
|
/* Check if there are open library groups */
|
|
|
|
{
|
|
|
|
if (Grouping) {
|
|
|
|
Error ("Library group was never closed");
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-07 11:33:28 +00:00
|
|
|
const char* GetLibFileName (const Library* L)
|
|
|
|
/* Get the name of a library */
|
|
|
|
{
|
|
|
|
return GetString (L->Name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned GetLibId (const Library* L)
|
|
|
|
/* Get the id of a library file. */
|
|
|
|
{
|
|
|
|
return L->Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-08 17:15:18 +00:00
|
|
|
unsigned LibraryCount (void)
|
|
|
|
/* Return the total number of libraries */
|
|
|
|
{
|
|
|
|
return CollCount (&LibraryList);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-07 11:33:28 +00:00
|
|
|
void PrintDbgLibraries (FILE* F)
|
|
|
|
/* Output the libraries to a debug info file */
|
|
|
|
{
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
/* Output information about all libraries */
|
2011-08-08 17:15:18 +00:00
|
|
|
for (I = 0; I < CollCount (&LibraryList); ++I) {
|
2011-08-07 11:33:28 +00:00
|
|
|
/* Get the library */
|
2011-08-08 17:15:18 +00:00
|
|
|
const Library* L = CollAtUnchecked (&LibraryList, I);
|
2011-08-07 11:33:28 +00:00
|
|
|
|
|
|
|
/* Output the info */
|
2011-08-08 17:18:52 +00:00
|
|
|
fprintf (F, "lib\tid=%u,name=\"%s\"\n", L->Id, GetString (L->Name));
|
2011-08-07 11:33:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|