]> git.sur5r.net Git - cc65/blob - src/ld65/library.c
Added a method to write variable sized unsigned values. Use this method for
[cc65] / src / ld65 / library.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 library.c                                 */
4 /*                                                                           */
5 /*          Library data structures and helpers for the ld65 linker          */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2000 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 <stdio.h>
37 #include <string.h>
38 #include <errno.h>
39
40 /* common */
41 #include "exprdefs.h"
42 #include "filepos.h"
43 #include "libdefs.h"
44 #include "objdefs.h"
45 #include "symdefs.h"
46 #include "xmalloc.h"
47
48 /* ld65 */
49 #include "error.h"
50 #include "exports.h"
51 #include "fileio.h"
52 #include "objdata.h"
53 #include "objfile.h"
54 #include "library.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 /* Library data */
65 static FILE*            Lib             = 0;
66 static char*            LibName         = 0;
67 static unsigned         ModuleCount     = 0;
68 static ObjData**        Index           = 0;
69
70
71
72 /*****************************************************************************/
73 /*                       Reading file data structures                        */
74 /*****************************************************************************/
75
76
77
78 static void LibReadObjHeader (ObjData* O)
79 /* Read the header of the object file checking the signature */
80 {
81     O->Header.Magic      = Read32 (Lib);
82     if (O->Header.Magic != OBJ_MAGIC) {
83         Error ("Object file `%s' in library `%s' is invalid", O->Name, LibName);
84     }
85     O->Header.Version    = Read16 (Lib);
86     if (O->Header.Version != OBJ_VERSION) {
87         Error ("Object file `%s' in library `%s' has wrong version",
88                O->Name, LibName);
89     }
90     O->Header.Flags      = Read16 (Lib);
91     O->Header.OptionOffs = Read32 (Lib);
92     O->Header.OptionSize = Read32 (Lib);
93     O->Header.FileOffs   = Read32 (Lib);
94     O->Header.FileSize   = Read32 (Lib);
95     O->Header.SegOffs    = Read32 (Lib);
96     O->Header.SegSize    = Read32 (Lib);
97     O->Header.ImportOffs = Read32 (Lib);
98     O->Header.ImportSize = Read32 (Lib);
99     O->Header.ExportOffs = Read32 (Lib);
100     O->Header.ExportSize = Read32 (Lib);
101     O->Header.DbgSymOffs = Read32 (Lib);
102     O->Header.DbgSymSize = Read32 (Lib);
103 }
104
105
106
107 static ObjData* ReadIndexEntry (void)
108 /* Read one entry in the index */
109 {
110     unsigned I;
111
112     /* Create a new entry and insert it into the list */
113     ObjData* O  = NewObjData ();
114
115     /* Module name/flags/MTime/Start/Size */
116     O->Name     = ReadStr (Lib);
117     O->Flags    = Read16 (Lib);
118     Read32 (Lib);                       /* Skip MTime */
119     O->Start    = Read32 (Lib);
120     Read32 (Lib);                       /* Skip Size */
121
122     /* Skip the export size, then read the exports */
123     Read16 (Lib);
124     O->ExportCount = ReadVar (Lib);
125     O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
126     for (I = 0; I < O->ExportCount; ++I) {
127         O->Exports [I] = ReadExport (Lib, O);
128     }
129
130     /* Skip the import size, then read the imports */
131     Read16 (Lib);
132     O->ImportCount = ReadVar (Lib);
133     O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
134     for (I = 0; I < O->ImportCount; ++I) {
135         O->Imports [I] = ReadImport (Lib, O);
136     }
137
138     /* Done */
139     return O;
140 }
141
142
143
144 static void ReadIndex (void)
145 /* Read the index of a library file */
146 {
147     unsigned I;
148
149     /* Read the object file count and allocate memory */
150     ModuleCount = Read16 (Lib);
151     Index = xmalloc (ModuleCount * sizeof (ObjData*));
152
153     /* Read all entries in the index */
154     for (I = 0; I < ModuleCount; ++I) {
155         Index [I] = ReadIndexEntry ();
156     }
157 }
158
159
160
161 /*****************************************************************************/
162 /*                             High level stuff                              */
163 /*****************************************************************************/
164
165
166
167 static void LibCheckExports (ObjData* O)
168 /* Check if the exports from this file can satisfy any import requests. If so,
169  * insert the imports and exports from this file and mark the file as added.
170  */
171 {
172     unsigned I;
173
174     /* Check all exports */
175     for (I = 0; I < O->ExportCount; ++I) {
176         if (IsUnresolved (O->Exports [I]->Name)) {
177             /* We need this module */
178             O->Flags |= OBJ_REF;
179             break;
180         }
181     }
182
183     /* If we need this module, insert the imports and exports */
184     if (O->Flags & OBJ_REF) {
185         /* Insert the exports */
186         for (I = 0; I < O->ExportCount; ++I) {
187             InsertExport (O->Exports [I]);
188         }
189         /* Insert the imports */
190         for (I = 0; I < O->ImportCount; ++I) {
191             InsertImport (O->Imports [I]);
192         }
193     }
194 }
195
196
197
198 void LibAdd (FILE* F, const char* Name)
199 /* Add files from the library to the list if there are references that could
200  * be satisfied.
201  */
202 {
203     int Add;
204     unsigned I;
205     LibHeader Header;
206
207     /* Store the parameters, so they're visible for other routines */
208     Lib     = F;
209     LibName = xstrdup (Name);
210
211     /* Read the remaining header fields (magic is already read) */
212     Header.Magic   = LIB_MAGIC;
213     Header.Version = Read16 (Lib);
214     if (Header.Version != LIB_VERSION) {
215         Error ("Wrong data version in `%s'", Name);
216     }
217     Header.Flags   = Read16 (Lib);
218     Header.IndexOffs = Read32 (Lib);
219
220     /* Seek to the index position and read the index */
221     fseek (Lib, Header.IndexOffs, SEEK_SET);
222     ReadIndex ();
223
224     /* Walk through all library modules and check for each module if there
225      * are unresolved externals in existing modules that may be resolved
226      * by adding the module. Repeat this step until no more object files
227      * were added.
228      */
229     do {
230         Add = 0;
231         for (I = 0; I < ModuleCount; ++I) {
232             ObjData* O = Index [I];
233             if ((O->Flags & OBJ_REF) == 0) {
234                 LibCheckExports (O);
235                 if (O->Flags & OBJ_REF) {
236                     /* The routine added the file */
237                     Add = 1;
238                 }
239             }
240         }
241     } while (Add);
242
243     /* Add the files list and sections for all requested modules */
244     for (I = 0; I < ModuleCount; ++I) {
245         ObjData* O = Index [I];
246         if (O->Flags & OBJ_REF) {
247
248             /* Seek to the start of the object file and read the header */
249             fseek (Lib, O->Start, SEEK_SET);
250             LibReadObjHeader (O);
251
252             /* Seek to the start of the files list and read the files list */
253             fseek (Lib, O->Start + O->Header.FileOffs, SEEK_SET);
254             ObjReadFiles (Lib, O);
255
256             /* Seek to the start of the segment list and read the segments */
257             fseek (Lib, O->Start + O->Header.SegOffs, SEEK_SET);
258             ObjReadSections (Lib, O);
259
260             /* Seek to the start of the debug info and read the debug info */
261             fseek (Lib, O->Start + O->Header.DbgSymOffs, SEEK_SET);
262             ObjReadDbgSyms (Lib, O);
263
264             /* We have the data now */
265             O->Flags |= OBJ_HAVEDATA;
266
267         }
268
269         /* Add a pointer to the library name */
270         O->LibName = LibName;
271     }
272
273     /* Done. Close the file, release allocated memory */
274     fclose (F);
275     xfree (Index);
276     Lib         = 0;
277     LibName     = 0;
278     ModuleCount = 0;
279     Index       = 0;
280 }
281
282
283
284
285
286
287