]> git.sur5r.net Git - cc65/blob - src/ld65/objdata.c
1635c14a07fa0263e276ecf3f709fcc03a2be79d
[cc65] / src / ld65 / objdata.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 objdata.c                                 */
4 /*                                                                           */
5 /*               Handling object file data for the ld65 linker               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* ld65 */
43 #include "error.h"
44 #include "exports.h"
45 #include "fileinfo.h"
46 #include "library.h"
47 #include "objdata.h"
48 #include "spool.h"
49
50
51
52 /*****************************************************************************/
53 /*                                   Data                                    */
54 /*****************************************************************************/
55
56
57
58 /* Collection containing used ObjData objects */
59 Collection       ObjDataList = STATIC_COLLECTION_INITIALIZER;
60
61
62
63 /*****************************************************************************/
64 /*                                   Code                                    */
65 /*****************************************************************************/
66
67
68
69 ObjData* NewObjData (void)
70 /* Allocate a new structure on the heap, insert it into the list, return it */
71 {
72     /* Allocate memory */
73     ObjData* O = xmalloc (sizeof (ObjData));
74
75     /* Initialize the data */
76     O->Next             = 0;
77     O->Name             = INVALID_STRING_ID;
78     O->Lib              = 0;
79     O->MTime            = 0;
80     O->Start            = 0;
81     O->Flags            = 0;
82     O->SymBaseId        = 0;
83     O->ScopeBaseId      = 0;
84     O->Files            = EmptyCollection;
85     O->Sections         = EmptyCollection;
86     O->Exports          = EmptyCollection;
87     O->Imports          = EmptyCollection;
88     O->DbgSyms          = EmptyCollection;
89     O->LineInfos        = EmptyCollection;
90     O->StringCount      = 0;
91     O->Strings          = 0;
92     O->Assertions       = EmptyCollection;
93     O->Scopes           = EmptyCollection;
94
95     /* Return the new entry */
96     return O;
97 }
98
99
100
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
104  * referenced.
105  */
106 {
107     unsigned I;
108
109     for (I = 0; I < CollCount (&O->Files); ++I) {
110         CollDeleteItem (&((FileInfo*) CollAtUnchecked (&O->Files, I))->Modules, O);
111     }
112     DoneCollection (&O->Files);
113     DoneCollection (&O->Sections);
114     for (I = 0; I < CollCount (&O->Exports); ++I) {
115         FreeExport (CollAtUnchecked (&O->Exports, I));
116     }
117     DoneCollection (&O->Exports);
118     for (I = 0; I < CollCount (&O->Imports); ++I) {
119         FreeImport (CollAtUnchecked (&O->Imports, I));
120     }
121     DoneCollection (&O->Imports);
122     DoneCollection (&O->DbgSyms);
123
124     for (I = 0; I < CollCount (&O->LineInfos); ++I) {
125         FreeLineInfo (CollAtUnchecked (&O->LineInfos, I));
126     }
127     DoneCollection (&O->LineInfos);
128     xfree (O->Strings);
129     DoneCollection (&O->Assertions);
130     DoneCollection (&O->Scopes);
131     xfree (O);
132 }
133
134
135
136 void FreeObjStrings (ObjData* O)
137 /* Free the module string data. Used once the object file is loaded completely
138  * when all strings are converted to global strings.
139  */
140 {
141     xfree (O->Strings);
142     O->Strings = 0;
143 }
144
145
146
147 void InsertObjData (ObjData* O)
148 /* Insert the ObjData object into the collection of used ObjData objects. */
149 {
150     CollAppend (&ObjDataList, O);
151 }
152
153
154
155 void InsertObjGlobals (ObjData* O)
156 /* Insert imports and exports from the object file into the global import and
157  * export lists.
158  */
159 {
160     unsigned I;
161
162     /* Insert exports and imports */
163     for (I = 0; I < CollCount (&O->Exports); ++I) {
164         InsertExport (CollAt (&O->Exports, I));
165     }
166     for (I = 0; I < CollCount (&O->Imports); ++I) {
167         InsertImport (CollAt (&O->Imports, I));
168     }
169 }
170
171
172
173 unsigned MakeGlobalStringId (const ObjData* O, unsigned Index)
174 /* Convert a local string id into a global one and return it. */
175 {
176     if (Index >= O->StringCount) {
177         Error ("Invalid string index (%u) in module `%s'",
178                Index, GetObjFileName (O));
179     }
180     return O->Strings[Index];
181 }
182
183
184
185 const char* GetObjFileName (const ObjData* O)
186 /* Get the name of the object file. Return "[linker generated]" if the object
187  * file is NULL.
188  */
189 {
190     return O? GetString (O->Name) : "[linker generated]";
191 }
192
193
194
195 struct Section* GetObjSection (ObjData* O, unsigned Id)
196 /* Get a section from an object file checking for a valid index */
197 {
198     if (Id >= CollCount (&O->Sections)) {
199         Error ("Invalid section index (%u) in module `%s'",
200                Id, GetObjFileName (O));
201     }
202     return CollAtUnchecked (&O->Sections, Id);
203 }
204
205
206
207 struct Scope* GetObjScope (ObjData* O, unsigned Id)
208 /* Get a scope from an object file checking for a valid index */
209 {
210     if (Id >= CollCount (&O->Scopes)) {
211         Error ("Invalid scope index (%u) in module `%s'",
212                Id, GetObjFileName (O));
213     }
214     return CollAtUnchecked (&O->Scopes, Id);
215 }
216
217
218
219 void PrintDbgModules (FILE* F)
220 /* Output the modules to a debug info file */
221 {
222     unsigned I;
223
224     /* Output modules */
225     for (I = 0; I < CollCount (&ObjDataList); ++I) {
226
227         /* Get this object file */
228         const ObjData* O = CollConstAt (&ObjDataList, I);
229
230         /* The main source file is the one at index zero */
231         const FileInfo* Source = CollConstAt (&O->Files, 0);
232
233         /* Output the module line */
234         fprintf (F,
235                  "mod\tid=%u,name=\"%s\",file=%u",
236                  I,
237                  GetObjFileName (O),
238                  Source->Id);
239
240         /* Add library if any */
241         if (O->Lib != 0) {
242             fprintf (F, ",lib=%u", GetLibId (O->Lib));
243         }
244
245         /* Terminate the output line */
246         fputc ('\n', F);
247     }
248
249 }
250
251
252