2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
|
|
|
/* objdata.c */
|
|
|
|
/* */
|
|
|
|
/* Handling object file data for the ld65 linker */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2011-01-24 22:39:07 +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 <string.h>
|
|
|
|
|
2000-11-20 21:56:48 +00:00
|
|
|
/* common */
|
|
|
|
#include "check.h"
|
|
|
|
#include "xmalloc.h"
|
2000-06-14 09:57:42 +00:00
|
|
|
|
2000-11-20 21:56:48 +00:00
|
|
|
/* ld65 */
|
2000-05-28 13:40:48 +00:00
|
|
|
#include "error.h"
|
2003-06-05 16:50:01 +00:00
|
|
|
#include "exports.h"
|
2001-05-23 21:32:57 +00:00
|
|
|
#include "fileinfo.h"
|
2011-08-07 13:15:24 +00:00
|
|
|
#include "library.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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2010-07-30 20:58:51 +00:00
|
|
|
/* Data */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
/* Collection containing used ObjData objects */
|
|
|
|
Collection ObjDataList = STATIC_COLLECTION_INITIALIZER;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2010-07-30 20:58:51 +00:00
|
|
|
/* Code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ObjData* NewObjData (void)
|
|
|
|
/* Allocate a new structure on the heap, insert it into the list, return it */
|
|
|
|
{
|
|
|
|
/* Allocate memory */
|
2000-06-14 09:57:42 +00:00
|
|
|
ObjData* O = xmalloc (sizeof (ObjData));
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Initialize the data */
|
|
|
|
O->Next = 0;
|
2003-06-05 16:50:01 +00:00
|
|
|
O->Name = INVALID_STRING_ID;
|
2011-08-07 11:33:28 +00:00
|
|
|
O->Lib = 0;
|
2003-06-05 16:50:01 +00:00
|
|
|
O->MTime = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
O->Start = 0;
|
2010-07-31 09:44:25 +00:00
|
|
|
O->Flags = 0;
|
2011-08-05 13:09:13 +00:00
|
|
|
O->SymBaseId = 0;
|
|
|
|
O->ScopeBaseId = 0;
|
2011-08-14 19:24:03 +00:00
|
|
|
O->SpanBaseId = 0;
|
2010-07-31 09:44:25 +00:00
|
|
|
O->Files = EmptyCollection;
|
2010-11-12 14:17:35 +00:00
|
|
|
O->Sections = EmptyCollection;
|
2010-07-30 20:58:51 +00:00
|
|
|
O->Exports = EmptyCollection;
|
|
|
|
O->Imports = EmptyCollection;
|
|
|
|
O->DbgSyms = EmptyCollection;
|
|
|
|
O->LineInfos = EmptyCollection;
|
2003-05-25 17:57:50 +00:00
|
|
|
O->StringCount = 0;
|
|
|
|
O->Strings = 0;
|
2010-07-30 20:58:51 +00:00
|
|
|
O->Assertions = EmptyCollection;
|
|
|
|
O->Scopes = EmptyCollection;
|
2011-08-14 19:24:03 +00:00
|
|
|
O->Spans = EmptyCollection;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
/* Return the new entry */
|
|
|
|
return O;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
void FreeObjData (ObjData* O)
|
|
|
|
/* Free an ObjData object. NOTE: This function works only for unused object
|
|
|
|
* data, that is, ObjData objects that aren't used because they aren't
|
|
|
|
* referenced.
|
|
|
|
*/
|
|
|
|
{
|
2010-07-30 20:58:51 +00:00
|
|
|
unsigned I;
|
|
|
|
|
2011-08-07 13:15:24 +00:00
|
|
|
for (I = 0; I < CollCount (&O->Files); ++I) {
|
|
|
|
CollDeleteItem (&((FileInfo*) CollAtUnchecked (&O->Files, I))->Modules, O);
|
|
|
|
}
|
2011-08-02 15:00:33 +00:00
|
|
|
DoneCollection (&O->Files);
|
|
|
|
DoneCollection (&O->Sections);
|
2010-07-30 20:58:51 +00:00
|
|
|
for (I = 0; I < CollCount (&O->Exports); ++I) {
|
2011-08-01 20:49:59 +00:00
|
|
|
FreeExport (CollAtUnchecked (&O->Exports, I));
|
2003-06-05 16:50:01 +00:00
|
|
|
}
|
2010-07-30 20:58:51 +00:00
|
|
|
DoneCollection (&O->Exports);
|
|
|
|
for (I = 0; I < CollCount (&O->Imports); ++I) {
|
2011-08-01 20:49:59 +00:00
|
|
|
FreeImport (CollAtUnchecked (&O->Imports, I));
|
2003-06-05 16:50:01 +00:00
|
|
|
}
|
2010-07-30 20:58:51 +00:00
|
|
|
DoneCollection (&O->Imports);
|
|
|
|
DoneCollection (&O->DbgSyms);
|
2011-08-02 15:00:33 +00:00
|
|
|
|
2011-08-01 20:49:59 +00:00
|
|
|
for (I = 0; I < CollCount (&O->LineInfos); ++I) {
|
|
|
|
FreeLineInfo (CollAtUnchecked (&O->LineInfos, I));
|
|
|
|
}
|
2010-07-30 20:58:51 +00:00
|
|
|
DoneCollection (&O->LineInfos);
|
2003-06-06 06:50:27 +00:00
|
|
|
xfree (O->Strings);
|
2010-07-30 20:58:51 +00:00
|
|
|
DoneCollection (&O->Assertions);
|
|
|
|
DoneCollection (&O->Scopes);
|
2011-08-14 19:24:03 +00:00
|
|
|
for (I = 0; I < CollCount (&O->Spans); ++I) {
|
|
|
|
FreeSpan (CollAtUnchecked (&O->Spans, I));
|
|
|
|
}
|
|
|
|
DoneCollection (&O->Spans);
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
xfree (O);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
void FreeObjStrings (ObjData* O)
|
|
|
|
/* Free the module string data. Used once the object file is loaded completely
|
|
|
|
* when all strings are converted to global strings.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
xfree (O->Strings);
|
|
|
|
O->Strings = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-05 16:50:01 +00:00
|
|
|
void InsertObjData (ObjData* O)
|
|
|
|
/* Insert the ObjData object into the collection of used ObjData objects. */
|
|
|
|
{
|
|
|
|
CollAppend (&ObjDataList, O);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-06 12:45:19 +00:00
|
|
|
void InsertObjGlobals (ObjData* O)
|
|
|
|
/* Insert imports and exports from the object file into the global import and
|
|
|
|
* export lists.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
/* Insert exports and imports */
|
2010-07-30 20:58:51 +00:00
|
|
|
for (I = 0; I < CollCount (&O->Exports); ++I) {
|
|
|
|
InsertExport (CollAt (&O->Exports, I));
|
2003-06-06 12:45:19 +00:00
|
|
|
}
|
2010-07-30 20:58:51 +00:00
|
|
|
for (I = 0; I < CollCount (&O->Imports); ++I) {
|
|
|
|
InsertImport (CollAt (&O->Imports, I));
|
2003-06-06 12:45:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-06-04 12:40:14 +00:00
|
|
|
unsigned MakeGlobalStringId (const ObjData* O, unsigned Index)
|
|
|
|
/* Convert a local string id into a global one and return it. */
|
|
|
|
{
|
|
|
|
if (Index >= O->StringCount) {
|
|
|
|
Error ("Invalid string index (%u) in module `%s'",
|
|
|
|
Index, GetObjFileName (O));
|
|
|
|
}
|
2003-06-06 06:50:27 +00:00
|
|
|
return O->Strings[Index];
|
2003-06-04 12:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-11-20 21:56:48 +00:00
|
|
|
const char* GetObjFileName (const ObjData* O)
|
2000-11-23 19:14:15 +00:00
|
|
|
/* Get the name of the object file. Return "[linker generated]" if the object
|
2000-11-20 21:56:48 +00:00
|
|
|
* file is NULL.
|
|
|
|
*/
|
|
|
|
{
|
2003-06-05 16:50:01 +00:00
|
|
|
return O? GetString (O->Name) : "[linker generated]";
|
2000-11-20 21:56:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-04 13:14:26 +00:00
|
|
|
struct Section* GetObjSection (ObjData* O, unsigned Id)
|
|
|
|
/* Get a section from an object file checking for a valid index */
|
|
|
|
{
|
|
|
|
if (Id >= CollCount (&O->Sections)) {
|
|
|
|
Error ("Invalid section index (%u) in module `%s'",
|
|
|
|
Id, GetObjFileName (O));
|
|
|
|
}
|
|
|
|
return CollAtUnchecked (&O->Sections, Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-16 13:58:59 +00:00
|
|
|
struct Import* GetObjImport (ObjData* O, unsigned Id)
|
|
|
|
/* Get an import from an object file checking for a valid index */
|
|
|
|
{
|
|
|
|
if (Id >= CollCount (&O->Imports)) {
|
|
|
|
Error ("Invalid import index (%u) in module `%s'",
|
|
|
|
Id, GetObjFileName (O));
|
|
|
|
}
|
|
|
|
return CollAtUnchecked (&O->Imports, Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct Export* GetObjExport (ObjData* O, unsigned Id)
|
|
|
|
/* Get an export from an object file checking for a valid index */
|
|
|
|
{
|
|
|
|
if (Id >= CollCount (&O->Exports)) {
|
|
|
|
Error ("Invalid export index (%u) in module `%s'",
|
|
|
|
Id, GetObjFileName (O));
|
|
|
|
}
|
|
|
|
return CollAtUnchecked (&O->Exports, Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-04 13:14:26 +00:00
|
|
|
struct Scope* GetObjScope (ObjData* O, unsigned Id)
|
|
|
|
/* Get a scope from an object file checking for a valid index */
|
|
|
|
{
|
|
|
|
if (Id >= CollCount (&O->Scopes)) {
|
|
|
|
Error ("Invalid scope index (%u) in module `%s'",
|
|
|
|
Id, GetObjFileName (O));
|
|
|
|
}
|
|
|
|
return CollAtUnchecked (&O->Scopes, Id);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-08 17:15:18 +00:00
|
|
|
unsigned ObjDataCount (void)
|
|
|
|
/* Return the total number of modules */
|
|
|
|
{
|
|
|
|
return CollCount (&ObjDataList);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-08-07 13:15:24 +00:00
|
|
|
void PrintDbgModules (FILE* F)
|
|
|
|
/* Output the modules to a debug info file */
|
|
|
|
{
|
|
|
|
unsigned I;
|
|
|
|
|
|
|
|
/* Output modules */
|
|
|
|
for (I = 0; I < CollCount (&ObjDataList); ++I) {
|
|
|
|
|
|
|
|
/* Get this object file */
|
|
|
|
const ObjData* O = CollConstAt (&ObjDataList, I);
|
|
|
|
|
|
|
|
/* The main source file is the one at index zero */
|
|
|
|
const FileInfo* Source = CollConstAt (&O->Files, 0);
|
|
|
|
|
|
|
|
/* Output the module line */
|
|
|
|
fprintf (F,
|
|
|
|
"mod\tid=%u,name=\"%s\",file=%u",
|
|
|
|
I,
|
|
|
|
GetObjFileName (O),
|
|
|
|
Source->Id);
|
|
|
|
|
|
|
|
/* Add library if any */
|
|
|
|
if (O->Lib != 0) {
|
|
|
|
fprintf (F, ",lib=%u", GetLibId (O->Lib));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Terminate the output line */
|
|
|
|
fputc ('\n', F);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|