2000-05-28 13:40:48 +00:00
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* */
|
|
|
|
|
/* objdata.c */
|
|
|
|
|
/* */
|
|
|
|
|
/* Handling object file data for the ld65 linker */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
|
|
|
|
/* */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* (C) 1998-2003 Ullrich von Bassewitz */
|
2003-11-09 12:12:02 +00:00
|
|
|
|
/* R<>merstra<72>e 52 */
|
2003-05-25 17:57:50 +00:00
|
|
|
|
/* D-70794 Filderstadt */
|
2001-05-23 19:03:40 +00:00
|
|
|
|
/* 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"
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Data */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
/* Code */
|
|
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
O->LibName = INVALID_STRING_ID;
|
|
|
|
|
O->MTime = 0;
|
2000-05-28 13:40:48 +00:00
|
|
|
|
O->Flags = 0;
|
|
|
|
|
O->Start = 0;
|
|
|
|
|
O->ExportCount = 0;
|
|
|
|
|
O->Exports = 0;
|
|
|
|
|
O->ImportCount = 0;
|
|
|
|
|
O->Imports = 0;
|
|
|
|
|
O->DbgSymCount = 0;
|
|
|
|
|
O->DbgSyms = 0;
|
2001-05-23 19:03:40 +00:00
|
|
|
|
O->LineInfoCount = 0;
|
|
|
|
|
O->LineInfos = 0;
|
2003-05-25 17:57:50 +00:00
|
|
|
|
O->StringCount = 0;
|
|
|
|
|
O->Strings = 0;
|
2003-06-06 12:45:19 +00:00
|
|
|
|
O->AssertionCount = 0;
|
|
|
|
|
O->Assertions = 0;
|
2003-11-28 22:12:14 +00:00
|
|
|
|
O->ScopeCount = 0;
|
|
|
|
|
O->Scopes = 0;
|
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.
|
|
|
|
|
*/
|
|
|
|
|
{
|
|
|
|
|
/* Unused ObjData do only have the string pool, Exports and Imports. */
|
|
|
|
|
while (O->ExportCount) {
|
|
|
|
|
FreeExport (O->Exports[--O->ExportCount]);
|
|
|
|
|
}
|
|
|
|
|
xfree (O->Exports);
|
|
|
|
|
while (O->ImportCount) {
|
|
|
|
|
FreeImport (O->Imports[--O->ImportCount]);
|
|
|
|
|
}
|
|
|
|
|
xfree (O->Imports);
|
2003-06-06 06:50:27 +00:00
|
|
|
|
xfree (O->Strings);
|
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 */
|
|
|
|
|
for (I = 0; I < O->ExportCount; ++I) {
|
|
|
|
|
InsertExport (O->Exports[I]);
|
|
|
|
|
}
|
|
|
|
|
for (I = 0; I < O->ImportCount; ++I) {
|
|
|
|
|
InsertImport (O->Imports[I]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const char* GetSourceFileName (const ObjData* O, unsigned Index)
|
|
|
|
|
/* Get the name of the source file with the given index. If O is NULL, return
|
2000-11-23 19:14:15 +00:00
|
|
|
|
* "[linker generated]" as the file name.
|
2000-11-20 21:56:48 +00:00
|
|
|
|
*/
|
2000-11-23 19:14:15 +00:00
|
|
|
|
{
|
2000-11-20 21:56:48 +00:00
|
|
|
|
/* Check if we have an object file */
|
|
|
|
|
if (O == 0) {
|
2000-11-23 19:14:15 +00:00
|
|
|
|
|
|
|
|
|
/* No object file */
|
|
|
|
|
return "[linker generated]";
|
2000-11-20 21:56:48 +00:00
|
|
|
|
|
|
|
|
|
} else {
|
2000-11-23 19:14:15 +00:00
|
|
|
|
|
2000-11-20 21:56:48 +00:00
|
|
|
|
/* Check the parameter */
|
2003-11-09 12:12:02 +00:00
|
|
|
|
if (Index >= O->FileCount) {
|
2003-11-28 22:12:14 +00:00
|
|
|
|
/* Error() will terminate the program */
|
|
|
|
|
Warning ("Invalid file index (%u) in module `%s' (input file corrupt?)",
|
2003-11-09 12:12:02 +00:00
|
|
|
|
Index, GetObjFileName (O));
|
2003-11-28 22:12:14 +00:00
|
|
|
|
return "[invalid]"; /* ### */
|
2003-11-09 12:12:02 +00:00
|
|
|
|
}
|
2000-11-23 19:14:15 +00:00
|
|
|
|
|
2000-11-20 21:56:48 +00:00
|
|
|
|
/* Return the name */
|
2003-06-04 15:40:32 +00:00
|
|
|
|
return GetString (O->Files[Index]->Name);
|
2000-11-20 21:56:48 +00:00
|
|
|
|
|
2001-05-23 19:03:40 +00:00
|
|
|
|
}
|
2000-11-20 21:56:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|