]> git.sur5r.net Git - cc65/blob - src/ld65/objfile.c
Use collections in the object file structure instead of managing the items
[cc65] / src / ld65 / objfile.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 objfile.c                                 */
4 /*                                                                           */
5 /*                 Object file handling 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 #include <errno.h>
38 #include <time.h>
39 #include <sys/types.h>          /* EMX needs this */
40 #include <sys/stat.h>
41
42 /* common */
43 #include "fname.h"
44 #include "xmalloc.h"
45
46 /* ld65 */
47 #include "asserts.h"
48 #include "dbgsyms.h"
49 #include "error.h"
50 #include "exports.h"
51 #include "fileinfo.h"
52 #include "fileio.h"
53 #include "lineinfo.h"
54 #include "objdata.h"
55 #include "objfile.h"
56 #include "segments.h"
57 #include "spool.h"
58
59
60
61 /*****************************************************************************/
62 /*                                   Code                                    */
63 /*****************************************************************************/
64
65
66
67 static unsigned GetModule (const char* Name)
68 /* Get a module name index from the file name */
69 {
70     /* Make a module name from the file name */
71     const char* Module = FindName (Name);
72     if (*Module == 0) {
73         Error ("Cannot make module name from `%s'", Name);
74     }
75     return GetStringId (Module);
76 }
77
78
79
80 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
81 /* Read the header of the object file checking the signature */
82 {
83     H->Version    = Read16 (Obj);
84     if (H->Version != OBJ_VERSION) {
85         Error ("Object file `%s' has wrong version, expected %08X, got %08X",
86                Name, OBJ_VERSION, H->Version);
87     }
88     H->Flags        = Read16 (Obj);
89     H->OptionOffs   = Read32 (Obj);
90     H->OptionSize   = Read32 (Obj);
91     H->FileOffs     = Read32 (Obj);
92     H->FileSize     = Read32 (Obj);
93     H->SegOffs      = Read32 (Obj);
94     H->SegSize      = Read32 (Obj);
95     H->ImportOffs   = Read32 (Obj);
96     H->ImportSize   = Read32 (Obj);
97     H->ExportOffs   = Read32 (Obj);
98     H->ExportSize   = Read32 (Obj);
99     H->DbgSymOffs   = Read32 (Obj);
100     H->DbgSymSize   = Read32 (Obj);
101     H->LineInfoOffs = Read32 (Obj);
102     H->LineInfoSize = Read32 (Obj);
103     H->StrPoolOffs  = Read32 (Obj);
104     H->StrPoolSize  = Read32 (Obj);
105     H->AssertOffs   = Read32 (Obj);
106     H->AssertSize   = Read32 (Obj);
107     H->ScopeOffs    = Read32 (Obj);
108     H->ScopeSize    = Read32 (Obj);
109 }
110
111
112
113 void ObjReadFiles (FILE* F, unsigned long Pos, ObjData* O)
114 /* Read the files list from a file at the given position */
115 {
116     unsigned I;
117
118     /* Seek to the correct position */
119     FileSetPos (F, Pos);
120
121     /* Read the data */
122     O->FileCount  = ReadVar (F);
123     CollGrow (&O->Files, O->FileCount);
124     for (I = 0; I < O->FileCount; ++I) {
125         CollAppend (&O->Files, ReadFileInfo (F, O));
126     }
127 }
128
129
130
131 void ObjReadSections (FILE* F, unsigned long Pos, ObjData* O)
132 /* Read the section data from a file at the given position */
133 {
134     unsigned I;
135
136     /* Seek to the correct position */
137     FileSetPos (F, Pos);
138
139     /* Read the data */
140     O->SectionCount = ReadVar (F);
141     CollGrow (&O->Sections, O->SectionCount);
142     for (I = 0; I < O->SectionCount; ++I) {
143         CollAppend (&O->Sections, ReadSection (F, O));
144     }
145 }
146
147
148
149 void ObjReadImports (FILE* F, unsigned long Pos, ObjData* O)
150 /* Read the imports from a file at the given position */
151 {
152     unsigned I;
153
154     /* Seek to the correct position */
155     FileSetPos (F, Pos);
156
157     /* Read the data */
158     O->ImportCount = ReadVar (F);
159     CollGrow (&O->Imports, O->ImportCount);
160     for (I = 0; I < O->ImportCount; ++I) {
161         CollAppend (&O->Imports, ReadImport (F, O));
162     }
163 }
164
165
166
167 void ObjReadExports (FILE* F, unsigned long Pos, ObjData* O)
168 /* Read the exports from a file at the given position */
169 {
170     unsigned I;
171
172     /* Seek to the correct position */
173     FileSetPos (F, Pos);
174
175     /* Read the data */
176     O->ExportCount = ReadVar (F);
177     CollGrow (&O->Exports, O->ExportCount);
178     for (I = 0; I < O->ExportCount; ++I) {
179         CollAppend (&O->Exports, ReadExport (F, O));
180     }
181 }
182
183
184
185 void ObjReadDbgSyms (FILE* F, unsigned long Pos, ObjData* O)
186 /* Read the debug symbols from a file at the given position */
187 {
188     unsigned I;
189
190     /* Seek to the correct position */
191     FileSetPos (F, Pos);
192
193     /* Read the data */
194     O->DbgSymCount = ReadVar (F);
195     CollGrow (&O->DbgSyms, O->DbgSymCount);
196     for (I = 0; I < O->DbgSymCount; ++I) {
197         CollAppend (&O->DbgSyms, ReadDbgSym (F, O));
198     }
199 }
200
201
202
203 void ObjReadLineInfos (FILE* F, unsigned long Pos, ObjData* O)
204 /* Read the line infos from a file at the given position */
205 {
206     unsigned I;
207
208     /* Seek to the correct position */
209     FileSetPos (F, Pos);
210
211     /* Read the data */
212     O->LineInfoCount = ReadVar (F);
213     CollGrow (&O->LineInfos, O->LineInfoCount);
214     for (I = 0; I < O->LineInfoCount; ++I) {
215         CollAppend (&O->LineInfos, ReadLineInfo (F, O));
216     }
217 }
218
219
220
221 void ObjReadStrPool (FILE* F, unsigned long Pos, ObjData* O)
222 /* Read the string pool from a file at the given position */
223 {
224     unsigned I;
225
226     /* Seek to the correct position */
227     FileSetPos (F, Pos);
228
229     /* Read the data */
230     O->StringCount = ReadVar (F);
231     O->Strings     = xmalloc (O->StringCount * sizeof (O->Strings[0]));
232     for (I = 0; I < O->StringCount; ++I) {
233         O->Strings[I] = ReadStr (F);
234     }
235 }
236
237
238
239 void ObjReadAssertions (FILE* F, unsigned long Pos, ObjData* O)
240 /* Read the assertions from a file at the given offset */
241 {
242     unsigned I;
243
244     /* Seek to the correct position */
245     FileSetPos (F, Pos);
246
247     /* Read the data */
248     O->AssertionCount = ReadVar (F);
249     CollGrow (&O->Assertions, O->AssertionCount);
250     for (I = 0; I < O->AssertionCount; ++I) {
251         CollAppend (&O->Assertions, ReadAssertion (F, O));
252     }
253 }
254
255
256
257 void ObjReadScopes (FILE* F, unsigned long Pos, ObjData* O)
258 /* Read the scope table from a file at the given offset */
259 {
260     unsigned I;
261
262     /* Seek to the correct position */
263     FileSetPos (F, Pos);
264
265     /* Read the data */
266     O->ScopeCount = ReadVar (F);
267     CollGrow (&O->Scopes, O->ScopeCount);
268     for (I = 0; I < O->ScopeCount; ++I) {
269         CollAppend (&O->Scopes, 0);     /* ReadScope (F, O); ### not implemented */
270     }
271 }
272
273
274
275 void ObjAdd (FILE* Obj, const char* Name)
276 /* Add an object file to the module list */
277 {
278     /* Create a new structure for the object file data */
279     ObjData* O = NewObjData ();
280
281     /* The magic was already read and checked, so set it in the header */
282     O->Header.Magic = OBJ_MAGIC;
283
284     /* Read and check the header */
285     ObjReadHeader (Obj, &O->Header, Name);
286
287     /* Initialize the object module data structure */
288     O->Name  = GetModule (Name);
289
290     /* Read the string pool from the object file */
291     ObjReadStrPool (Obj, O->Header.StrPoolOffs, O);
292
293     /* Read the files list from the object file */
294     ObjReadFiles (Obj, O->Header.FileOffs, O);
295
296     /* Read the imports list from the object file */
297     ObjReadImports (Obj, O->Header.ImportOffs, O);
298
299     /* Read the object file exports and insert them into the exports list */
300     ObjReadExports (Obj, O->Header.ExportOffs, O);
301
302     /* Read the object debug symbols from the object file */
303     ObjReadDbgSyms (Obj, O->Header.DbgSymOffs, O);
304
305     /* Read the line infos from the object file */
306     ObjReadLineInfos (Obj, O->Header.LineInfoOffs, O);
307
308     /* Read the assertions from the object file */
309     ObjReadAssertions (Obj, O->Header.AssertOffs, O);
310
311     /* Read the scope table from the object file */
312     ObjReadScopes (Obj, O->Header.ScopeOffs, O);
313
314     /* Read the segment list from the object file. This must be last, since
315      * the expressions stored in the code may reference segments or imported
316      * symbols.
317      */
318     ObjReadSections (Obj, O->Header.SegOffs, O);
319
320     /* Mark this object file as needed */
321     O->Flags |= OBJ_REF;
322
323     /* Done, close the file (we read it only, so no error check) */
324     fclose (Obj);
325
326     /* Insert the imports and exports to the global lists */
327     InsertObjGlobals (O);
328
329     /* Insert the object into the list of all used object files */
330     InsertObjData (O);
331
332     /* All references to strings are now resolved, so we can delete the module
333      * string pool.
334      */
335     FreeObjStrings (O);
336 }
337
338
339
340