1 /*****************************************************************************/
5 /* Handling object file data for the ld65 linker */
9 /* (C) 1998-2011, 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;
84 O->Files = EmptyCollection;
85 O->Sections = EmptyCollection;
86 O->Exports = EmptyCollection;
87 O->Imports = EmptyCollection;
88 O->DbgSyms = EmptyCollection;
89 O->LineInfos = EmptyCollection;
92 O->Assertions = EmptyCollection;
93 O->Scopes = EmptyCollection;
95 /* Return the new entry */
101 void FreeObjData (ObjData* O)
102 /* Free an ObjData object. NOTE: This function works only for unused object
103 * data, that is, ObjData objects that aren't used because they aren't
109 DoneCollection (&O->Files);
110 DoneCollection (&O->Sections);
111 for (I = 0; I < CollCount (&O->Exports); ++I) {
112 FreeExport (CollAtUnchecked (&O->Exports, I));
114 DoneCollection (&O->Exports);
115 for (I = 0; I < CollCount (&O->Imports); ++I) {
116 FreeImport (CollAtUnchecked (&O->Imports, I));
118 DoneCollection (&O->Imports);
119 DoneCollection (&O->DbgSyms);
121 for (I = 0; I < CollCount (&O->LineInfos); ++I) {
122 FreeLineInfo (CollAtUnchecked (&O->LineInfos, I));
124 DoneCollection (&O->LineInfos);
126 DoneCollection (&O->Assertions);
127 DoneCollection (&O->Scopes);
133 void FreeObjStrings (ObjData* O)
134 /* Free the module string data. Used once the object file is loaded completely
135 * when all strings are converted to global strings.
144 void InsertObjData (ObjData* O)
145 /* Insert the ObjData object into the collection of used ObjData objects. */
147 CollAppend (&ObjDataList, O);
152 void InsertObjGlobals (ObjData* O)
153 /* Insert imports and exports from the object file into the global import and
159 /* Insert exports and imports */
160 for (I = 0; I < CollCount (&O->Exports); ++I) {
161 InsertExport (CollAt (&O->Exports, I));
163 for (I = 0; I < CollCount (&O->Imports); ++I) {
164 InsertImport (CollAt (&O->Imports, I));
170 unsigned MakeGlobalStringId (const ObjData* O, unsigned Index)
171 /* Convert a local string id into a global one and return it. */
173 if (Index >= O->StringCount) {
174 Error ("Invalid string index (%u) in module `%s'",
175 Index, GetObjFileName (O));
177 return O->Strings[Index];
182 const char* GetObjFileName (const ObjData* O)
183 /* Get the name of the object file. Return "[linker generated]" if the object
187 return O? GetString (O->Name) : "[linker generated]";
192 struct Section* GetObjSection (ObjData* O, unsigned Id)
193 /* Get a section from an object file checking for a valid index */
195 if (Id >= CollCount (&O->Sections)) {
196 Error ("Invalid section index (%u) in module `%s'",
197 Id, GetObjFileName (O));
199 return CollAtUnchecked (&O->Sections, Id);
204 struct Scope* GetObjScope (ObjData* O, unsigned Id)
205 /* Get a scope from an object file checking for a valid index */
207 if (Id >= CollCount (&O->Scopes)) {
208 Error ("Invalid scope index (%u) in module `%s'",
209 Id, GetObjFileName (O));
211 return CollAtUnchecked (&O->Scopes, Id);