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