]> git.sur5r.net Git - cc65/blob - src/ld65/objfile.c
Added assertions
[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 "asserts.h"
48 #include "dbgsyms.h"
49 #include "error.h"
50 #include "exports.h"
51 #include "fileinfo.h"
52 #include "fileio.h"
53 #include "lineinfo.h"
54 #include "objdata.h"
55 #include "objfile.h"
56 #include "segments.h"
57 #include "spool.h"
58
59
60
61 /*****************************************************************************/
62 /*                                   Code                                    */
63 /*****************************************************************************/
64
65
66
67 static unsigned GetModule (const char* Name)
68 /* Get a module name index from the file name */
69 {
70     /* Make a module name from the file name */
71     const char* Module = FindName (Name);
72     if (*Module == 0) {
73         Error ("Cannot make module name from `%s'", Name);
74     }
75     return GetStringId (Module);
76 }
77
78
79
80 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
81 /* Read the header of the object file checking the signature */
82 {
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);
87     }
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 }
108
109
110
111 void ObjReadFiles (FILE* F, unsigned long Pos, ObjData* O)
112 /* Read the files list from a file at the given position */
113 {
114     unsigned I;
115
116     /* Seek to the correct position */
117     FileSetPos (F, Pos);
118
119     /* Read the data */
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);
124     }
125 }
126
127
128
129 void ObjReadSections (FILE* F, unsigned long Pos, ObjData* O)
130 /* Read the section data from a file at the given position */
131 {
132     unsigned I;
133
134     /* Seek to the correct position */
135     FileSetPos (F, Pos);
136
137     /* Read the data */
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);
142     }
143 }
144
145
146
147 void ObjReadImports (FILE* F, unsigned long Pos, ObjData* O)
148 /* Read the imports from a file at the given position */
149 {
150     unsigned I;
151
152     /* Seek to the correct position */
153     FileSetPos (F, Pos);
154
155     /* Read the data */
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);
160     }
161 }
162
163
164
165 void ObjReadExports (FILE* F, unsigned long Pos, ObjData* O)
166 /* Read the exports from a file at the given position */
167 {
168     unsigned I;
169
170     /* Seek to the correct position */
171     FileSetPos (F, Pos);
172
173     /* Read the data */
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);
178     }
179 }
180
181
182
183 void ObjReadDbgSyms (FILE* F, unsigned long Pos, ObjData* O)
184 /* Read the debug symbols from a file at the given position */
185 {
186     unsigned I;
187
188     /* Seek to the correct position */
189     FileSetPos (F, Pos);
190
191     /* Read the data */
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);
196     }
197 }
198
199
200
201 void ObjReadLineInfos (FILE* F, unsigned long Pos, ObjData* O)
202 /* Read the line infos from a file at the given position */
203 {
204     unsigned I;
205
206     /* Seek to the correct position */
207     FileSetPos (F, Pos);
208
209     /* Read the data */
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);
214     }
215 }
216
217
218
219 void ObjReadStrPool (FILE* F, unsigned long Pos, ObjData* O)
220 /* Read the string pool from a file at the given position */
221 {
222     unsigned I;
223
224     /* Seek to the correct position */
225     FileSetPos (F, Pos);
226
227     /* Read the data */
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);
232     }
233 }
234
235
236
237 void ObjReadAssertions (FILE* F, unsigned long Pos, ObjData* O)
238 /* Read the assertions from a file at the given offset */
239 {
240     unsigned I;
241
242     /* Seek to the correct position */
243     FileSetPos (F, Pos);
244
245     /* Read the data */
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);
250     }
251 }
252
253
254
255 void ObjAdd (FILE* Obj, const char* Name)
256 /* Add an object file to the module list */
257 {
258     /* Create a new structure for the object file data */
259     ObjData* O = NewObjData ();
260
261     /* The magic was already read and checked, so set it in the header */
262     O->Header.Magic = OBJ_MAGIC;
263
264     /* Read and check the header */
265     ObjReadHeader (Obj, &O->Header, Name);
266
267     /* Initialize the object module data structure */
268     O->Name  = GetModule (Name);
269
270     /* Read the string pool from the object file */
271     ObjReadStrPool (Obj, O->Header.StrPoolOffs, O);
272
273     /* Read the files list from the object file */
274     ObjReadFiles (Obj, O->Header.FileOffs, O);
275
276     /* Read the imports list from the object file */
277     ObjReadImports (Obj, O->Header.ImportOffs, O);
278
279     /* Read the object file exports and insert them into the exports list */
280     ObjReadExports (Obj, O->Header.ExportOffs, O);
281
282     /* Read the object debug symbols from the object file */
283     ObjReadDbgSyms (Obj, O->Header.DbgSymOffs, O);
284
285     /* Read the line infos from the object file */
286     ObjReadLineInfos (Obj, O->Header.LineInfoOffs, O);
287
288     /* Read the assertions from the object file */
289     ObjReadAssertions (Obj, O->Header.AssertOffs, O);
290
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
293      * symbols.
294      */
295     ObjReadSections (Obj, O->Header.SegOffs, O);
296
297     /* Mark this object file as needed */
298     O->Flags |= OBJ_REF;
299
300     /* Done, close the file (we read it only, so no error check) */
301     fclose (Obj);
302
303     /* Insert the imports and exports to the global lists */
304     InsertObjGlobals (O);
305
306     /* Insert the object into the list of all used object files */
307     InsertObjData (O);
308
309     /* All references to strings are now resolved, so we can delete the module
310      * string pool.
311      */
312     FreeObjStrings (O);
313 }
314
315
316
317