1 /*****************************************************************************/
5 /* Object file handling for the ld65 linker */
9 /* (C) 1998 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 */
56 /*****************************************************************************/
58 /*****************************************************************************/
62 static const char* GetModule (const char* Name)
63 /* Get a module name from the file name */
65 /* Make a module name from the file name */
66 const char* Module = Name + strlen (Name);
67 while (Module > Name) {
69 if (*Module == '/' || *Module == '\\') {
75 Error ("Cannot make module name from `%s'", Name);
82 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
83 /* Read the header of the object file checking the signature */
85 H->Version = Read16 (Obj);
86 if (H->Version != OBJ_VERSION) {
87 Error ("Object file `%s' has wrong version", Name);
89 H->Flags = Read16 (Obj);
90 H->OptionOffs = Read32 (Obj);
91 H->OptionSize = Read32 (Obj);
92 H->FileOffs = Read32 (Obj);
93 H->FileSize = Read32 (Obj);
94 H->SegOffs = Read32 (Obj);
95 H->SegSize = Read32 (Obj);
96 H->ImportOffs = Read32 (Obj);
97 H->ImportSize = Read32 (Obj);
98 H->ExportOffs = Read32 (Obj);
99 H->ExportSize = Read32 (Obj);
100 H->DbgSymOffs = Read32 (Obj);
101 H->DbgSymSize = Read32 (Obj);
106 void ObjReadFiles (FILE* F, ObjData* O)
107 /* Read the files list from a file at the current position */
111 O->FileCount = ReadVar (F);
112 O->Files = xmalloc (O->FileCount * sizeof (char*));
113 for (I = 0; I < O->FileCount; ++I) {
114 /* Skip MTime and size */
117 /* Read the filename */
118 O->Files [I] = ReadStr (F);
124 void ObjReadImports (FILE* F, ObjData* O)
125 /* Read the imports from a file at the current position */
129 O->ImportCount = ReadVar (F);
130 O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
131 for (I = 0; I < O->ImportCount; ++I) {
132 O->Imports [I] = ReadImport (F, O);
133 InsertImport (O->Imports [I]);
139 void ObjReadExports (FILE* F, ObjData* O)
140 /* Read the exports from a file at the current position */
144 O->ExportCount = ReadVar (F);
145 O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
146 for (I = 0; I < O->ExportCount; ++I) {
147 O->Exports [I] = ReadExport (F, O);
148 InsertExport (O->Exports [I]);
154 void ObjReadDbgSyms (FILE* F, ObjData* O)
155 /* Read the debug symbols from a file at the current position */
159 O->DbgSymCount = ReadVar (F);
160 O->DbgSyms = xmalloc (O->DbgSymCount * sizeof (DbgSym*));
161 for (I = 0; I < O->DbgSymCount; ++I) {
162 O->DbgSyms [I] = ReadDbgSym (F, O);
168 void ObjReadSections (FILE* F, ObjData* O)
169 /* Read the section data from a file at the current position */
173 O->SectionCount = ReadVar (F);
174 O->Sections = xmalloc (O->SectionCount * sizeof (Section*));
175 for (I = 0; I < O->SectionCount; ++I) {
176 O->Sections [I] = ReadSection (F, O);
182 void ObjAdd (FILE* Obj, const char* Name)
183 /* Add an object file to the module list */
185 /* Create a new structure for the object file data */
186 ObjData* O = NewObjData ();
188 /* The magic was already read and checked, so set it in the header */
189 O->Header.Magic = OBJ_MAGIC;
191 /* Read and check the header */
192 ObjReadHeader (Obj, &O->Header, Name);
194 /* Initialize the object module data structure */
195 O->Name = xstrdup (GetModule (Name));
196 O->Flags = OBJ_HAVEDATA;
198 /* Read the files list from the object file */
199 fseek (Obj, O->Header.FileOffs, SEEK_SET);
200 ObjReadFiles (Obj, O);
202 /* Read the imports list from the object file */
203 fseek (Obj, O->Header.ImportOffs, SEEK_SET);
204 ObjReadImports (Obj, O);
206 /* Read the object file exports and insert them into the exports list */
207 fseek (Obj, O->Header.ExportOffs, SEEK_SET);
208 ObjReadExports (Obj, O);
210 /* Read the object debug symbols from the object file */
211 fseek (Obj, O->Header.DbgSymOffs, SEEK_SET);
212 ObjReadDbgSyms (Obj, O);
214 /* Read the segment list from the object file. This must be last, since
215 * the expressions stored in the code may reference segments or imported
218 fseek (Obj, O->Header.SegOffs, SEEK_SET);
219 ObjReadSections (Obj, O);
221 /* Mark this object file as needed */
222 O->Flags |= OBJ_REF | OBJ_HAVEDATA;
224 /* Done, close the file (we read it only, so no error check) */