1 /*****************************************************************************/
5 /* Object file handling for the ld65 linker */
9 /* (C) 1998-2012, Ullrich von Bassewitz */
10 /* Roemerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 static unsigned GetModule (const char* Name)
65 /* Get a module name index from the file name */
67 /* Make a module name from the file name */
68 const char* Module = FindName (Name);
70 Error ("Cannot make module name from `%s'", Name);
72 return GetStringId (Module);
77 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
78 /* Read the header of the object file checking the signature */
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);
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);
112 void ObjReadFiles (FILE* F, unsigned long Pos, ObjData* O)
113 /* Read the files list from a file at the given position */
118 /* Seek to the correct position */
122 FileCount = ReadVar (F);
123 CollGrow (&O->Files, FileCount);
124 for (I = 0; I < FileCount; ++I) {
125 CollAppend (&O->Files, ReadFileInfo (F, O));
131 void ObjReadSections (FILE* F, unsigned long Pos, ObjData* O)
132 /* Read the section data from a file at the given position */
135 unsigned SectionCount;
137 /* Seek to the correct position */
141 SectionCount = ReadVar (F);
142 CollGrow (&O->Sections, SectionCount);
143 for (I = 0; I < SectionCount; ++I) {
144 CollAppend (&O->Sections, ReadSection (F, O));
150 void ObjReadImports (FILE* F, unsigned long Pos, ObjData* O)
151 /* Read the imports from a file at the given position */
154 unsigned ImportCount;
156 /* Seek to the correct position */
160 ImportCount = ReadVar (F);
161 CollGrow (&O->Imports, ImportCount);
162 for (I = 0; I < ImportCount; ++I) {
163 CollAppend (&O->Imports, ReadImport (F, O));
169 void ObjReadExports (FILE* F, unsigned long Pos, ObjData* O)
170 /* Read the exports from a file at the given position */
173 unsigned ExportCount;
175 /* Seek to the correct position */
179 ExportCount = ReadVar (F);
180 CollGrow (&O->Exports, ExportCount);
181 for (I = 0; I < ExportCount; ++I) {
182 CollAppend (&O->Exports, ReadExport (F, O));
188 void ObjReadDbgSyms (FILE* F, unsigned long Pos, ObjData* O)
189 /* Read the debug symbols from a file at the given position */
192 unsigned DbgSymCount;
194 /* Seek to the correct position */
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));
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));
214 void ObjReadLineInfos (FILE* F, unsigned long Pos, ObjData* O)
215 /* Read the line infos from a file at the given position */
218 unsigned LineInfoCount;
220 /* Seek to the correct position */
224 LineInfoCount = ReadVar (F);
225 CollGrow (&O->LineInfos, LineInfoCount);
226 for (I = 0; I < LineInfoCount; ++I) {
227 CollAppend (&O->LineInfos, ReadLineInfo (F, O));
233 void ObjReadStrPool (FILE* F, unsigned long Pos, ObjData* O)
234 /* Read the string pool from a file at the given position */
238 /* Seek to the correct position */
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);
251 void ObjReadAssertions (FILE* F, unsigned long Pos, ObjData* O)
252 /* Read the assertions from a file at the given offset */
255 unsigned AssertionCount;
257 /* Seek to the correct position */
261 AssertionCount = ReadVar (F);
262 CollGrow (&O->Assertions, AssertionCount);
263 for (I = 0; I < AssertionCount; ++I) {
264 CollAppend (&O->Assertions, ReadAssertion (F, O));
270 void ObjReadScopes (FILE* F, unsigned long Pos, ObjData* O)
271 /* Read the scope table from a file at the given offset */
276 /* Seek to the correct position */
280 ScopeCount = ReadVar (F);
281 CollGrow (&O->Scopes, ScopeCount);
282 for (I = 0; I < ScopeCount; ++I) {
283 CollAppend (&O->Scopes, ReadScope (F, O, I));
289 void ObjReadSpans (FILE* F, unsigned long Pos, ObjData* O)
290 /* Read the span table from a file at the given offset */
295 /* Seek to the correct position */
299 SpanCount = ReadVar (F);
300 CollGrow (&O->Spans, SpanCount);
301 for (I = 0; I < SpanCount; ++I) {
302 CollAppend (&O->Spans, ReadSpan (F, O, I));
308 void ObjAdd (FILE* Obj, const char* Name)
309 /* Add an object file to the module list */
311 /* Create a new structure for the object file data */
312 ObjData* O = NewObjData ();
314 /* The magic was already read and checked, so set it in the header */
315 O->Header.Magic = OBJ_MAGIC;
317 /* Read and check the header */
318 ObjReadHeader (Obj, &O->Header, Name);
320 /* Initialize the object module data structure */
321 O->Name = GetModule (Name);
323 /* Read the string pool from the object file */
324 ObjReadStrPool (Obj, O->Header.StrPoolOffs, O);
326 /* Read the files list from the object file */
327 ObjReadFiles (Obj, O->Header.FileOffs, O);
329 /* Read the line infos from the object file */
330 ObjReadLineInfos (Obj, O->Header.LineInfoOffs, O);
332 /* Read the imports list from the object file */
333 ObjReadImports (Obj, O->Header.ImportOffs, O);
335 /* Read the object file exports and insert them into the exports list */
336 ObjReadExports (Obj, O->Header.ExportOffs, O);
338 /* Read the object debug symbols from the object file */
339 ObjReadDbgSyms (Obj, O->Header.DbgSymOffs, O);
341 /* Read the assertions from the object file */
342 ObjReadAssertions (Obj, O->Header.AssertOffs, O);
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
348 ObjReadSections (Obj, O->Header.SegOffs, O);
350 /* Read the scope table from the object file. Scopes reference segments, so
351 * we must read them after the sections.
353 ObjReadScopes (Obj, O->Header.ScopeOffs, O);
355 /* Read the spans from the object file */
356 ObjReadSpans (Obj, O->Header.SpanOffs, O);
358 /* Mark this object file as needed */
361 /* Done, close the file (we read it only, so no error check) */
364 /* Insert the imports and exports to the global lists */
365 InsertObjGlobals (O);
367 /* Insert the object into the list of all used object files */
370 /* All references to strings are now resolved, so we can delete the module