]> git.sur5r.net Git - cc65/blob - src/ld65/objdata.c
89f8f8cd5130e4058d36bbe0e0b5ca53efb8bf4a
[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-2010, 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 "objdata.h"
47 #include "spool.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* Collection containing used ObjData objects */
58 Collection       ObjDataList = STATIC_COLLECTION_INITIALIZER;
59
60
61
62 /*****************************************************************************/
63 /*                                   Code                                    */
64 /*****************************************************************************/
65
66
67
68 ObjData* NewObjData (void)
69 /* Allocate a new structure on the heap, insert it into the list, return it */
70 {
71     /* Allocate memory */
72     ObjData* O = xmalloc (sizeof (ObjData));
73
74     /* Initialize the data */
75     O->Next             = 0;
76     O->Name             = INVALID_STRING_ID;
77     O->LibName          = INVALID_STRING_ID;
78     O->MTime            = 0;
79     O->Start            = 0;
80     O->Flags            = 0;
81     O->FileCount        = 0;
82     O->Files            = EmptyCollection;
83     O->SectionCount     = 0;
84     O->Sections         = EmptyCollection;                
85     O->ExportCount      = 0;
86     O->Exports          = EmptyCollection;
87     O->ImportCount      = 0;
88     O->Imports          = EmptyCollection;
89     O->DbgSymCount      = 0;
90     O->DbgSyms          = EmptyCollection;
91     O->LineInfoCount    = 0;
92     O->LineInfos        = EmptyCollection;
93     O->StringCount      = 0;
94     O->Strings          = 0;
95     O->AssertionCount   = 0;
96     O->Assertions       = EmptyCollection;
97     O->ScopeCount       = 0;
98     O->Scopes           = EmptyCollection;
99
100     /* Return the new entry */
101     return O;
102 }
103
104
105
106 void FreeObjData (ObjData* O)
107 /* Free an ObjData object. NOTE: This function works only for unused object
108  * data, that is, ObjData objects that aren't used because they aren't
109  * referenced.
110  */
111 {
112     unsigned I;
113
114     /* Unused ObjData do only have the string pool, Exports and Imports. */
115     for (I = 0; I < CollCount (&O->Exports); ++I) {
116         FreeExport (CollAt (&O->Exports, I));
117     }
118     DoneCollection (&O->Exports);
119     for (I = 0; I < CollCount (&O->Imports); ++I) {
120         FreeImport (CollAt (&O->Imports, I));
121     }
122     DoneCollection (&O->Imports);
123     DoneCollection (&O->DbgSyms);
124     DoneCollection (&O->LineInfos);
125     xfree (O->Strings);
126     DoneCollection (&O->Assertions);
127     DoneCollection (&O->Scopes);
128     xfree (O);
129 }
130
131
132
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.
136  */
137 {
138     xfree (O->Strings);
139     O->Strings = 0;
140 }
141
142
143
144 void InsertObjData (ObjData* O)
145 /* Insert the ObjData object into the collection of used ObjData objects. */
146 {
147     CollAppend (&ObjDataList, O);
148 }
149
150
151
152 void InsertObjGlobals (ObjData* O)
153 /* Insert imports and exports from the object file into the global import and
154  * export lists.
155  */
156 {
157     unsigned I;
158
159     /* Insert exports and imports */
160     for (I = 0; I < CollCount (&O->Exports); ++I) {
161         InsertExport (CollAt (&O->Exports, I));
162     }
163     for (I = 0; I < CollCount (&O->Imports); ++I) {
164         InsertImport (CollAt (&O->Imports, I));
165     }
166 }
167
168
169
170 unsigned MakeGlobalStringId (const ObjData* O, unsigned Index)
171 /* Convert a local string id into a global one and return it. */
172 {
173     if (Index >= O->StringCount) {
174         Error ("Invalid string index (%u) in module `%s'",
175                Index, GetObjFileName (O));
176     }
177     return O->Strings[Index];
178 }
179
180
181
182 const char* GetObjFileName (const ObjData* O)
183 /* Get the name of the object file. Return "[linker generated]" if the object
184  * file is NULL.
185  */
186 {
187     return O? GetString (O->Name) : "[linker generated]";
188 }
189
190
191
192 const char* GetSourceFileName (const ObjData* O, unsigned Index)
193 /* Get the name of the source file with the given index. If O is NULL, return
194  * "[linker generated]" as the file name.
195  */
196 {
197     /* Check if we have an object file */
198     if (O == 0) {
199
200         /* No object file */
201         return "[linker generated]";
202
203     } else {
204
205         /* Check the parameter */
206         if (Index >= CollCount (&O->Files)) {
207             /* Error() will terminate the program */
208             Warning ("Invalid file index (%u) in module `%s' (input file corrupt?)",
209                    Index, GetObjFileName (O));
210             return "[invalid]";         /* ### */
211
212         } else {
213
214             /* Get a pointer to the file info struct */
215             const FileInfo* FI = CollConstAt (&O->Files, Index);
216
217             /* Return the name */
218             return GetString (FI->Name);
219
220         }
221     }
222 }
223
224
225
226