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