1 /*****************************************************************************/
5 /* Object file handling for the ld65 linker */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
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 /*****************************************************************************/
39 #include <sys/types.h> /* EMX needs this */
61 /*****************************************************************************/
63 /*****************************************************************************/
67 static unsigned GetModule (const char* Name)
68 /* Get a module name index from the file name */
70 /* Make a module name from the file name */
71 const char* Module = FindName (Name);
73 Error ("Cannot make module name from `%s'", Name);
75 return GetStringId (Module);
80 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
81 /* Read the header of the object file checking the signature */
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);
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);
113 void ObjReadFiles (FILE* F, unsigned long Pos, ObjData* O)
114 /* Read the files list from a file at the given position */
118 /* Seek to the correct position */
122 O->FileCount = ReadVar (F);
123 O->Files = xmalloc (O->FileCount * sizeof (O->Files[0]));
124 for (I = 0; I < O->FileCount; ++I) {
125 O->Files[I] = 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 */
136 /* Seek to the correct position */
140 O->SectionCount = ReadVar (F);
141 O->Sections = xmalloc (O->SectionCount * sizeof (O->Sections[0]));
142 for (I = 0; I < O->SectionCount; ++I) {
143 O->Sections [I] = ReadSection (F, O);
149 void ObjReadImports (FILE* F, unsigned long Pos, ObjData* O)
150 /* Read the imports from a file at the given position */
154 /* Seek to the correct position */
158 O->ImportCount = ReadVar (F);
159 O->Imports = xmalloc (O->ImportCount * sizeof (O->Imports[0]));
160 for (I = 0; I < O->ImportCount; ++I) {
161 O->Imports [I] = ReadImport (F, O);
167 void ObjReadExports (FILE* F, unsigned long Pos, ObjData* O)
168 /* Read the exports from a file at the given position */
172 /* Seek to the correct position */
176 O->ExportCount = ReadVar (F);
177 O->Exports = xmalloc (O->ExportCount * sizeof (O->Exports[0]));
178 for (I = 0; I < O->ExportCount; ++I) {
179 O->Exports [I] = ReadExport (F, O);
185 void ObjReadDbgSyms (FILE* F, unsigned long Pos, ObjData* O)
186 /* Read the debug symbols from a file at the given position */
190 /* Seek to the correct position */
194 O->DbgSymCount = ReadVar (F);
195 O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (O->DbgSyms[0]));
196 for (I = 0; I < O->DbgSymCount; ++I) {
197 O->DbgSyms [I] = ReadDbgSym (F, O);
203 void ObjReadLineInfos (FILE* F, unsigned long Pos, ObjData* O)
204 /* Read the line infos from a file at the given position */
208 /* Seek to the correct position */
212 O->LineInfoCount = ReadVar (F);
213 O->LineInfos = xmalloc (O->LineInfoCount * sizeof (O->LineInfos[0]));
214 for (I = 0; I < O->LineInfoCount; ++I) {
215 O->LineInfos[I] = ReadLineInfo (F, O);
221 void ObjReadStrPool (FILE* F, unsigned long Pos, ObjData* O)
222 /* Read the string pool from a file at the given position */
226 /* Seek to the correct position */
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);
239 void ObjReadAssertions (FILE* F, unsigned long Pos, ObjData* O)
240 /* Read the assertions from a file at the given offset */
244 /* Seek to the correct position */
248 O->AssertionCount = ReadVar (F);
249 O->Assertions = xmalloc (O->AssertionCount * sizeof (O->Assertions[0]));
250 for (I = 0; I < O->AssertionCount; ++I) {
251 O->Assertions[I] = ReadAssertion (F, O);
257 void ObjReadScopes (FILE* F, unsigned long Pos, ObjData* O)
258 /* Read the scope table from a file at the given offset */
262 /* Seek to the correct position */
266 O->ScopeCount = ReadVar (F);
267 O->Scopes = xmalloc (O->ScopeCount * sizeof (O->Scopes[0]));
268 for (I = 0; I < O->ScopeCount; ++I) {
269 O->Scopes[I] = 0; /* ReadScope (F, O); ### not implemented */
275 void ObjAdd (FILE* Obj, const char* Name)
276 /* Add an object file to the module list */
278 /* Create a new structure for the object file data */
279 ObjData* O = NewObjData ();
281 /* The magic was already read and checked, so set it in the header */
282 O->Header.Magic = OBJ_MAGIC;
284 /* Read and check the header */
285 ObjReadHeader (Obj, &O->Header, Name);
287 /* Initialize the object module data structure */
288 O->Name = GetModule (Name);
290 /* Read the string pool from the object file */
291 ObjReadStrPool (Obj, O->Header.StrPoolOffs, O);
293 /* Read the files list from the object file */
294 ObjReadFiles (Obj, O->Header.FileOffs, O);
296 /* Read the imports list from the object file */
297 ObjReadImports (Obj, O->Header.ImportOffs, O);
299 /* Read the object file exports and insert them into the exports list */
300 ObjReadExports (Obj, O->Header.ExportOffs, O);
302 /* Read the object debug symbols from the object file */
303 ObjReadDbgSyms (Obj, O->Header.DbgSymOffs, O);
305 /* Read the line infos from the object file */
306 ObjReadLineInfos (Obj, O->Header.LineInfoOffs, O);
308 /* Read the assertions from the object file */
309 ObjReadAssertions (Obj, O->Header.AssertOffs, O);
311 /* Read the scope table from the object file */
312 ObjReadScopes (Obj, O->Header.ScopeOffs, O);
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
318 ObjReadSections (Obj, O->Header.SegOffs, O);
320 /* Mark this object file as needed */
323 /* Done, close the file (we read it only, so no error check) */
326 /* Insert the imports and exports to the global lists */
327 InsertObjGlobals (O);
329 /* Insert the object into the list of all used object files */
332 /* All references to strings are now resolved, so we can delete the module