]> git.sur5r.net Git - cc65/blob - src/ld65/objfile.c
313977973fdfc0b67b9efa68b94dca17ab7a309f
[cc65] / src / ld65 / objfile.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 objfile.c                                 */
4 /*                                                                           */
5 /*                 Object file handling for the ld65 linker                  */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37 #include <errno.h>
38 #include <time.h>
39 #include <sys/types.h>          /* EMX needs this */
40 #include <sys/stat.h>
41
42 /* common */
43 #include "fname.h"
44 #include "xmalloc.h"
45
46 /* ld65 */
47 #include "dbgsyms.h"
48 #include "error.h"
49 #include "exports.h"
50 #include "fileinfo.h"
51 #include "fileio.h"
52 #include "lineinfo.h"
53 #include "objdata.h"
54 #include "objfile.h"
55 #include "segments.h"
56 #include "spool.h"
57
58
59
60 /*****************************************************************************/
61 /*                                   Code                                    */
62 /*****************************************************************************/
63
64
65
66 static unsigned GetModule (const char* Name)
67 /* Get a module name index from the file name */
68 {
69     /* Make a module name from the file name */
70     const char* Module = FindName (Name);
71     if (*Module == 0) {
72         Error ("Cannot make module name from `%s'", Name);
73     }
74     return GetStringId (Module);
75 }
76
77
78
79 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
80 /* Read the header of the object file checking the signature */
81 {
82     H->Version    = Read16 (Obj);
83     if (H->Version != OBJ_VERSION) {
84         Error ("Object file `%s' has wrong version, expected %08X, got %08X",
85                Name, OBJ_VERSION, H->Version);
86     }
87     H->Flags        = Read16 (Obj);
88     H->OptionOffs   = Read32 (Obj);
89     H->OptionSize   = Read32 (Obj);
90     H->FileOffs     = Read32 (Obj);
91     H->FileSize     = Read32 (Obj);
92     H->SegOffs      = Read32 (Obj);
93     H->SegSize      = Read32 (Obj);
94     H->ImportOffs   = Read32 (Obj);
95     H->ImportSize   = Read32 (Obj);
96     H->ExportOffs   = Read32 (Obj);
97     H->ExportSize   = Read32 (Obj);
98     H->DbgSymOffs   = Read32 (Obj);
99     H->DbgSymSize   = Read32 (Obj);
100     H->LineInfoOffs = Read32 (Obj);
101     H->LineInfoSize = Read32 (Obj);
102     H->StrPoolOffs  = Read32 (Obj);
103     H->StrPoolSize  = Read32 (Obj);
104 }
105
106
107
108 void ObjReadFiles (FILE* F, ObjData* O)
109 /* Read the files list from a file at the current position */
110 {
111     unsigned I;
112
113     O->FileCount  = ReadVar (F);
114     O->Files      = xmalloc (O->FileCount * sizeof (O->Files[0]));
115     for (I = 0; I < O->FileCount; ++I) {
116         O->Files[I] = ReadFileInfo (F, O);
117     }
118 }
119
120
121
122 void ObjReadImports (FILE* F, ObjData* O)
123 /* Read the imports from a file at the current position */
124 {
125     unsigned I;
126
127     O->ImportCount = ReadVar (F);
128     O->Imports     = xmalloc (O->ImportCount * sizeof (O->Imports[0]));
129     for (I = 0; I < O->ImportCount; ++I) {
130         O->Imports [I] = ReadImport (F, O);
131         InsertImport (O->Imports [I]);
132     }
133 }
134
135
136
137 void ObjReadExports (FILE* F, ObjData* O)
138 /* Read the exports from a file at the current position */
139 {
140     unsigned I;
141
142     O->ExportCount = ReadVar (F);
143     O->Exports     = xmalloc (O->ExportCount * sizeof (O->Exports[0]));
144     for (I = 0; I < O->ExportCount; ++I) {
145         O->Exports [I] = ReadExport (F, O);
146         InsertExport (O->Exports [I]);
147     }
148 }
149
150
151
152 void ObjReadDbgSyms (FILE* F, ObjData* O)
153 /* Read the debug symbols from a file at the current position */
154 {
155     unsigned I;
156
157     O->DbgSymCount = ReadVar (F);
158     O->DbgSyms     = xmalloc (O->DbgSymCount * sizeof (O->DbgSyms[0]));
159     for (I = 0; I < O->DbgSymCount; ++I) {
160         O->DbgSyms [I] = ReadDbgSym (F, O);
161     }
162 }
163
164
165
166 void ObjReadLineInfos (FILE* F, ObjData* O)
167 /* Read the line infos from a file at the current position */
168 {
169     unsigned I;
170
171     O->LineInfoCount = ReadVar (F);
172     O->LineInfos     = xmalloc (O->LineInfoCount * sizeof (O->LineInfos[0]));
173     for (I = 0; I < O->LineInfoCount; ++I) {
174         O->LineInfos[I] = ReadLineInfo (F, O);
175     }
176 }
177
178
179
180 void ObjReadStrPool (FILE* F, ObjData* O)
181 /* Read the string pool from a file at the current position */
182 {
183     unsigned I;
184
185     O->StringCount = ReadVar (F);
186     O->Strings     = xmalloc (O->StringCount * sizeof (O->Strings[0]));
187     for (I = 0; I < O->StringCount; ++I) {
188         O->Strings[I] = ReadStr (F);
189     }
190 }
191
192
193
194 void ObjReadSections (FILE* F, ObjData* O)
195 /* Read the section data from a file at the current position */
196 {
197     unsigned I;
198
199     O->SectionCount = ReadVar (F);
200     O->Sections     = xmalloc (O->SectionCount * sizeof (O->Sections[0]));
201     for (I = 0; I < O->SectionCount; ++I) {
202         O->Sections [I] = ReadSection (F, O);
203     }
204 }
205
206
207
208 void ObjAdd (FILE* Obj, const char* Name)
209 /* Add an object file to the module list */
210 {
211     /* Create a new structure for the object file data */
212     ObjData* O = NewObjData ();
213
214     /* The magic was already read and checked, so set it in the header */
215     O->Header.Magic = OBJ_MAGIC;
216
217     /* Read and check the header */
218     ObjReadHeader (Obj, &O->Header, Name);
219
220     /* Initialize the object module data structure */
221     O->Name  = GetModule (Name);
222
223     /* Read the string pool from the object file */
224     fseek (Obj, O->Header.StrPoolOffs, SEEK_SET);
225     ObjReadStrPool (Obj, O);
226
227     /* Read the files list from the object file */
228     fseek (Obj, O->Header.FileOffs, SEEK_SET);
229     ObjReadFiles (Obj, O);
230
231     /* Read the imports list from the object file */
232     fseek (Obj, O->Header.ImportOffs, SEEK_SET);
233     ObjReadImports (Obj, O);
234
235     /* Read the object file exports and insert them into the exports list */
236     fseek (Obj, O->Header.ExportOffs, SEEK_SET);
237     ObjReadExports (Obj, O);
238
239     /* Read the object debug symbols from the object file */
240     fseek (Obj, O->Header.DbgSymOffs, SEEK_SET);
241     ObjReadDbgSyms (Obj, O);
242
243     /* Read the line infos from the object file */
244     fseek (Obj, O->Header.LineInfoOffs, SEEK_SET);
245     ObjReadLineInfos (Obj, O);
246
247     /* Read the segment list from the object file. This must be last, since
248      * the expressions stored in the code may reference segments or imported
249      * symbols.
250      */
251     fseek (Obj, O->Header.SegOffs, SEEK_SET);
252     ObjReadSections (Obj, O);
253
254     /* Mark this object file as needed */
255     O->Flags |= OBJ_REF;
256
257     /* Done, close the file (we read it only, so no error check) */
258     fclose (Obj);
259
260     /* All references to strings are now resolved, so we can delete the module
261      * string pool.
262      */
263     FreeObjStrings (O);
264
265     /* Insert the object into the list of all used object files */
266     InsertObjData (O);
267 }
268
269
270
271