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 */
58 /*****************************************************************************/
60 /*****************************************************************************/
64 static const char* GetModule (const char* Name)
65 /* Get a module name from the file name */
67 /* Make a module name from the file name */
68 const char* Module = Name + strlen (Name);
69 while (Module > Name) {
71 if (*Module == '/' || *Module == '\\') {
77 Error ("Cannot make module name from `%s'", Name);
84 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
85 /* Read the header of the object file checking the signature */
87 H->Version = Read16 (Obj);
88 if (H->Version != OBJ_VERSION) {
89 Error ("Object file `%s' has wrong version, expected %08X, got %08X",
90 Name, OBJ_VERSION, H->Version);
92 H->Flags = Read16 (Obj);
93 H->OptionOffs = Read32 (Obj);
94 H->OptionSize = Read32 (Obj);
95 H->FileOffs = Read32 (Obj);
96 H->FileSize = Read32 (Obj);
97 H->SegOffs = Read32 (Obj);
98 H->SegSize = Read32 (Obj);
99 H->ImportOffs = Read32 (Obj);
100 H->ImportSize = Read32 (Obj);
101 H->ExportOffs = Read32 (Obj);
102 H->ExportSize = Read32 (Obj);
103 H->DbgSymOffs = Read32 (Obj);
104 H->DbgSymSize = Read32 (Obj);
105 H->LineInfoOffs = Read32 (Obj);
106 H->LineInfoSize = Read32 (Obj);
107 H->StrPoolOffs = Read32 (Obj);
108 H->StrPoolSize = Read32 (Obj);
113 void ObjReadFiles (FILE* F, ObjData* O)
114 /* Read the files list from a file at the current position */
118 O->FileCount = ReadVar (F);
119 O->Files = xmalloc (O->FileCount * sizeof (FileInfo*));
120 for (I = 0; I < O->FileCount; ++I) {
121 O->Files[I] = ReadFileInfo (F, O);
127 void ObjReadImports (FILE* F, ObjData* O)
128 /* Read the imports from a file at the current position */
132 O->ImportCount = ReadVar (F);
133 O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
134 for (I = 0; I < O->ImportCount; ++I) {
135 O->Imports [I] = ReadImport (F, O);
136 InsertImport (O->Imports [I]);
142 void ObjReadExports (FILE* F, ObjData* O)
143 /* Read the exports from a file at the current position */
147 O->ExportCount = ReadVar (F);
148 O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
149 for (I = 0; I < O->ExportCount; ++I) {
150 O->Exports [I] = ReadExport (F, O);
151 InsertExport (O->Exports [I]);
157 void ObjReadDbgSyms (FILE* F, ObjData* O)
158 /* Read the debug symbols from a file at the current position */
162 O->DbgSymCount = ReadVar (F);
163 O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (DbgSym*));
164 for (I = 0; I < O->DbgSymCount; ++I) {
165 O->DbgSyms [I] = ReadDbgSym (F, O);
171 void ObjReadLineInfos (FILE* F, ObjData* O)
172 /* Read the line infos from a file at the current position */
176 O->LineInfoCount = ReadVar (F);
177 O->LineInfos = xmalloc (O->LineInfoCount * sizeof (LineInfo*));
178 for (I = 0; I < O->LineInfoCount; ++I) {
179 O->LineInfos[I] = ReadLineInfo (F, O);
185 void ObjReadStrPool (FILE* F, ObjData* O)
186 /* Read the string pool from a file at the current position */
189 O->StringCount = ReadVar (F);
190 O->Strings = xmalloc (O->StringCount * sizeof (char*));
191 for (I = 0; I < O->StringCount; ++I) {
192 O->Strings[I] = ReadStr (F);
198 void ObjReadSections (FILE* F, ObjData* O)
199 /* Read the section data from a file at the current position */
203 O->SectionCount = ReadVar (F);
204 O->Sections = xmalloc (O->SectionCount * sizeof (Section*));
205 for (I = 0; I < O->SectionCount; ++I) {
206 O->Sections [I] = ReadSection (F, O);
212 void ObjAdd (FILE* Obj, const char* Name)
213 /* Add an object file to the module list */
215 /* Create a new structure for the object file data */
216 ObjData* O = NewObjData ();
218 /* The magic was already read and checked, so set it in the header */
219 O->Header.Magic = OBJ_MAGIC;
221 /* Read and check the header */
222 ObjReadHeader (Obj, &O->Header, Name);
224 /* Initialize the object module data structure */
225 O->Name = xstrdup (GetModule (Name));
226 O->Flags = OBJ_HAVEDATA;
228 /* Read the string pool from the object file */
229 fseek (Obj, O->Header.StrPoolOffs, SEEK_SET);
230 ObjReadStrPool (Obj, O);
232 /* Read the files list from the object file */
233 fseek (Obj, O->Header.FileOffs, SEEK_SET);
234 ObjReadFiles (Obj, O);
236 /* Read the imports list from the object file */
237 fseek (Obj, O->Header.ImportOffs, SEEK_SET);
238 ObjReadImports (Obj, O);
240 /* Read the object file exports and insert them into the exports list */
241 fseek (Obj, O->Header.ExportOffs, SEEK_SET);
242 ObjReadExports (Obj, O);
244 /* Read the object debug symbols from the object file */
245 fseek (Obj, O->Header.DbgSymOffs, SEEK_SET);
246 ObjReadDbgSyms (Obj, O);
248 /* Read the line infos from the object file */
249 fseek (Obj, O->Header.LineInfoOffs, SEEK_SET);
250 ObjReadLineInfos (Obj, O);
252 /* Read the segment list from the object file. This must be last, since
253 * the expressions stored in the code may reference segments or imported
256 fseek (Obj, O->Header.SegOffs, SEEK_SET);
257 ObjReadSections (Obj, O);
259 /* Mark this object file as needed */
260 O->Flags |= OBJ_REF | OBJ_HAVEDATA;
262 /* Done, close the file (we read it only, so no error check) */