1 /*****************************************************************************/
5 /* Handling object file data for the ld65 linker */
9 /* (C) 1998-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
15 /* This software is provided 'as-is', without any expressed or implied */
16 /* warranty. In no event will the authors be held liable for any damages */
17 /* arising from the use of this software. */
19 /* Permission is granted to anyone to use this software for any purpose, */
20 /* including commercial applications, and to alter it and redistribute it */
21 /* freely, subject to the following restrictions: */
23 /* 1. The origin of this software must not be misrepresented; you must not */
24 /* claim that you wrote the original software. If you use this software */
25 /* in a product, an acknowledgment in the product documentation would be */
26 /* appreciated but is not required. */
27 /* 2. Altered source versions must be plainly marked as such, and must not */
28 /* be misrepresented as being the original software. */
29 /* 3. This notice may not be removed or altered from any source */
32 /*****************************************************************************/
51 /*****************************************************************************/
53 /*****************************************************************************/
57 /* Collection containing used ObjData objects */
58 Collection ObjDataList = STATIC_COLLECTION_INITIALIZER;
62 /*****************************************************************************/
64 /*****************************************************************************/
68 ObjData* NewObjData (void)
69 /* Allocate a new structure on the heap, insert it into the list, return it */
72 ObjData* O = xmalloc (sizeof (ObjData));
74 /* Initialize the data */
76 O->Name = INVALID_STRING_ID;
77 O->LibName = INVALID_STRING_ID;
82 O->Exports = EmptyCollection;
84 O->Imports = EmptyCollection;
86 O->DbgSyms = EmptyCollection;
88 O->LineInfos = EmptyCollection;
91 O->AssertionCount = 0;
92 O->Assertions = EmptyCollection;
94 O->Scopes = EmptyCollection;
96 /* Return the new entry */
102 void FreeObjData (ObjData* O)
103 /* Free an ObjData object. NOTE: This function works only for unused object
104 * data, that is, ObjData objects that aren't used because they aren't
110 /* Unused ObjData do only have the string pool, Exports and Imports. */
111 for (I = 0; I < CollCount (&O->Exports); ++I) {
112 FreeExport (CollAt (&O->Exports, I));
114 DoneCollection (&O->Exports);
115 for (I = 0; I < CollCount (&O->Imports); ++I) {
116 FreeImport (CollAt (&O->Imports, I));
118 DoneCollection (&O->Imports);
119 DoneCollection (&O->DbgSyms);
120 DoneCollection (&O->LineInfos);
122 DoneCollection (&O->Assertions);
123 DoneCollection (&O->Scopes);
129 void FreeObjStrings (ObjData* O)
130 /* Free the module string data. Used once the object file is loaded completely
131 * when all strings are converted to global strings.
140 void InsertObjData (ObjData* O)
141 /* Insert the ObjData object into the collection of used ObjData objects. */
143 CollAppend (&ObjDataList, O);
148 void InsertObjGlobals (ObjData* O)
149 /* Insert imports and exports from the object file into the global import and
155 /* Insert exports and imports */
156 for (I = 0; I < CollCount (&O->Exports); ++I) {
157 InsertExport (CollAt (&O->Exports, I));
159 for (I = 0; I < CollCount (&O->Imports); ++I) {
160 InsertImport (CollAt (&O->Imports, I));
166 unsigned MakeGlobalStringId (const ObjData* O, unsigned Index)
167 /* Convert a local string id into a global one and return it. */
169 if (Index >= O->StringCount) {
170 Error ("Invalid string index (%u) in module `%s'",
171 Index, GetObjFileName (O));
173 return O->Strings[Index];
178 const char* GetObjFileName (const ObjData* O)
179 /* Get the name of the object file. Return "[linker generated]" if the object
183 return O? GetString (O->Name) : "[linker generated]";
188 const char* GetSourceFileName (const ObjData* O, unsigned Index)
189 /* Get the name of the source file with the given index. If O is NULL, return
190 * "[linker generated]" as the file name.
193 /* Check if we have an object file */
197 return "[linker generated]";
201 /* Check the parameter */
202 if (Index >= CollCount (&O->Files)) {
203 /* Error() will terminate the program */
204 Warning ("Invalid file index (%u) in module `%s' (input file corrupt?)",
205 Index, GetObjFileName (O));
206 return "[invalid]"; /* ### */
210 /* Get a pointer to the file info struct */
211 const FileInfo* FI = CollConstAt (&O->Files, Index);
213 /* Return the name */
214 return GetString (FI->Name);