1 /*****************************************************************************/
5 /* Object file handling for the ld65 linker */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 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 /*****************************************************************************/
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);
111 void ObjReadFiles (FILE* F, unsigned long Pos, ObjData* O)
112 /* Read the files list from a file at the given position */
116 /* Seek to the correct position */
120 O->FileCount = ReadVar (F);
121 O->Files = xmalloc (O->FileCount * sizeof (O->Files[0]));
122 for (I = 0; I < O->FileCount; ++I) {
123 O->Files[I] = ReadFileInfo (F, O);
129 void ObjReadSections (FILE* F, unsigned long Pos, ObjData* O)
130 /* Read the section data from a file at the given position */
134 /* Seek to the correct position */
138 O->SectionCount = ReadVar (F);
139 O->Sections = xmalloc (O->SectionCount * sizeof (O->Sections[0]));
140 for (I = 0; I < O->SectionCount; ++I) {
141 O->Sections [I] = ReadSection (F, O);
147 void ObjReadImports (FILE* F, unsigned long Pos, ObjData* O)
148 /* Read the imports from a file at the given position */
152 /* Seek to the correct position */
156 O->ImportCount = ReadVar (F);
157 O->Imports = xmalloc (O->ImportCount * sizeof (O->Imports[0]));
158 for (I = 0; I < O->ImportCount; ++I) {
159 O->Imports [I] = ReadImport (F, O);
165 void ObjReadExports (FILE* F, unsigned long Pos, ObjData* O)
166 /* Read the exports from a file at the given position */
170 /* Seek to the correct position */
174 O->ExportCount = ReadVar (F);
175 O->Exports = xmalloc (O->ExportCount * sizeof (O->Exports[0]));
176 for (I = 0; I < O->ExportCount; ++I) {
177 O->Exports [I] = ReadExport (F, O);
183 void ObjReadDbgSyms (FILE* F, unsigned long Pos, ObjData* O)
184 /* Read the debug symbols from a file at the given position */
188 /* Seek to the correct position */
192 O->DbgSymCount = ReadVar (F);
193 O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (O->DbgSyms[0]));
194 for (I = 0; I < O->DbgSymCount; ++I) {
195 O->DbgSyms [I] = ReadDbgSym (F, O);
201 void ObjReadLineInfos (FILE* F, unsigned long Pos, ObjData* O)
202 /* Read the line infos from a file at the given position */
206 /* Seek to the correct position */
210 O->LineInfoCount = ReadVar (F);
211 O->LineInfos = xmalloc (O->LineInfoCount * sizeof (O->LineInfos[0]));
212 for (I = 0; I < O->LineInfoCount; ++I) {
213 O->LineInfos[I] = ReadLineInfo (F, O);
219 void ObjReadStrPool (FILE* F, unsigned long Pos, ObjData* O)
220 /* Read the string pool from a file at the given position */
224 /* Seek to the correct position */
228 O->StringCount = ReadVar (F);
229 O->Strings = xmalloc (O->StringCount * sizeof (O->Strings[0]));
230 for (I = 0; I < O->StringCount; ++I) {
231 O->Strings[I] = ReadStr (F);
237 void ObjReadAssertions (FILE* F, unsigned long Pos, ObjData* O)
238 /* Read the assertions from a file at the given offset */
242 /* Seek to the correct position */
246 O->AssertionCount = ReadVar (F);
247 O->Assertions = xmalloc (O->AssertionCount * sizeof (O->Assertions[0]));
248 for (I = 0; I < O->AssertionCount; ++I) {
249 O->Assertions[I] = ReadAssertion (F, O);
255 void ObjAdd (FILE* Obj, const char* Name)
256 /* Add an object file to the module list */
258 /* Create a new structure for the object file data */
259 ObjData* O = NewObjData ();
261 /* The magic was already read and checked, so set it in the header */
262 O->Header.Magic = OBJ_MAGIC;
264 /* Read and check the header */
265 ObjReadHeader (Obj, &O->Header, Name);
267 /* Initialize the object module data structure */
268 O->Name = GetModule (Name);
270 /* Read the string pool from the object file */
271 ObjReadStrPool (Obj, O->Header.StrPoolOffs, O);
273 /* Read the files list from the object file */
274 ObjReadFiles (Obj, O->Header.FileOffs, O);
276 /* Read the imports list from the object file */
277 ObjReadImports (Obj, O->Header.ImportOffs, O);
279 /* Read the object file exports and insert them into the exports list */
280 ObjReadExports (Obj, O->Header.ExportOffs, O);
282 /* Read the object debug symbols from the object file */
283 ObjReadDbgSyms (Obj, O->Header.DbgSymOffs, O);
285 /* Read the line infos from the object file */
286 ObjReadLineInfos (Obj, O->Header.LineInfoOffs, O);
288 /* Read the assertions from the object file */
289 ObjReadAssertions (Obj, O->Header.AssertOffs, O);
291 /* Read the segment list from the object file. This must be last, since
292 * the expressions stored in the code may reference segments or imported
295 ObjReadSections (Obj, O->Header.SegOffs, O);
297 /* Mark this object file as needed */
300 /* Done, close the file (we read it only, so no error check) */
303 /* Insert the imports and exports to the global lists */
304 InsertObjGlobals (O);
306 /* Insert the object into the list of all used object files */
309 /* All references to strings are now resolved, so we can delete the module