]> git.sur5r.net Git - cc65/blob - src/ld65/library.c
Fragment cleanup, more string pool use
[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-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 <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 "library.h"
53 #include "objdata.h"
54 #include "objfile.h"
55 #include "spool.h"
56
57
58
59 /*****************************************************************************/
60 /*                                   Data                                    */
61 /*****************************************************************************/
62
63
64
65 /* Library data */
66 static FILE*            Lib             = 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, const char* LibName)
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",
84                GetObjFileName (O), LibName);
85     }
86     O->Header.Version = Read16 (Lib);
87     if (O->Header.Version != OBJ_VERSION) {
88         Error ("Object file `%s' in library `%s' has wrong version",
89                GetObjFileName (O), LibName);
90     }
91     O->Header.Flags        = Read16 (Lib);
92     O->Header.OptionOffs   = Read32 (Lib);
93     O->Header.OptionSize   = Read32 (Lib);
94     O->Header.FileOffs     = Read32 (Lib);
95     O->Header.FileSize     = Read32 (Lib);
96     O->Header.SegOffs      = Read32 (Lib);
97     O->Header.SegSize      = Read32 (Lib);
98     O->Header.ImportOffs   = Read32 (Lib);
99     O->Header.ImportSize   = Read32 (Lib);
100     O->Header.ExportOffs   = Read32 (Lib);
101     O->Header.ExportSize   = Read32 (Lib);
102     O->Header.DbgSymOffs   = Read32 (Lib);
103     O->Header.DbgSymSize   = Read32 (Lib);
104     O->Header.LineInfoOffs = Read32 (Lib);
105     O->Header.LineInfoSize = Read32 (Lib);
106     O->Header.StrPoolOffs  = Read32 (Lib);
107     O->Header.StrPoolSize  = Read32 (Lib);
108 }
109
110
111
112 static ObjData* ReadIndexEntry (void)
113 /* Read one entry in the index */
114 {
115     unsigned I;
116
117     /* Create a new entry and insert it into the list */
118     ObjData* O  = NewObjData ();
119
120     /* Module name */
121     O->Name = ReadStr (Lib);
122
123     /* Module flags/MTime/Start/Size */
124     O->Flags    = Read16 (Lib);
125     O->MTime    = Read32 (Lib);
126     O->Start    = Read32 (Lib);
127     Read32 (Lib);                       /* Skip Size */
128
129     /* Read the string pool */
130     ObjReadStrPool (Lib, O);
131
132     /* Skip the export size, then read the exports */
133     (void) ReadVar (Lib);
134     O->ExportCount = ReadVar (Lib);
135     O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
136     for (I = 0; I < O->ExportCount; ++I) {
137         O->Exports[I] = ReadExport (Lib, O);
138     }
139
140     /* Skip the import size, then read the imports */
141     (void) ReadVar (Lib);
142     O->ImportCount = ReadVar (Lib);
143     O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
144     for (I = 0; I < O->ImportCount; ++I) {
145         O->Imports[I] = ReadImport (Lib, O);
146     }
147
148     /* Done */
149     return O;
150 }
151
152
153
154 static void ReadIndex (void)
155 /* Read the index of a library file */
156 {
157     unsigned I;
158
159     /* Read the object file count and allocate memory */
160     ModuleCount = ReadVar (Lib);
161     Index = xmalloc (ModuleCount * sizeof (ObjData*));
162
163     /* Read all entries in the index */
164     for (I = 0; I < ModuleCount; ++I) {
165         Index[I] = ReadIndexEntry ();
166     }
167 }
168
169
170
171 /*****************************************************************************/
172 /*                             High level stuff                              */
173 /*****************************************************************************/
174
175
176
177 static void LibCheckExports (ObjData* O)
178 /* Check if the exports from this file can satisfy any import requests. If so,
179  * insert the imports and exports from this file and mark the file as added.
180  */
181 {
182     unsigned I;
183
184     /* Check all exports */
185     for (I = 0; I < O->ExportCount; ++I) {
186         if (IsUnresolved (O->Exports[I]->Name)) {
187             /* We need this module */
188             O->Flags |= OBJ_REF;
189             break;
190         }
191     }
192
193     /* If we need this module, insert the imports and exports */
194     if (O->Flags & OBJ_REF) {
195         /* Insert the exports */
196         for (I = 0; I < O->ExportCount; ++I) {
197             InsertExport (O->Exports[I]);
198         }
199         /* Insert the imports */
200         for (I = 0; I < O->ImportCount; ++I) {
201             InsertImport (O->Imports[I]);
202         }
203     }
204 }
205
206
207
208 void LibAdd (FILE* F, const char* Name)
209 /* Add files from the library to the list if there are references that could
210  * be satisfied.
211  */
212 {
213     unsigned LibName;
214     int HaveAdditions;
215     unsigned I;
216     LibHeader Header;
217
218     /* Store the parameters, so they're visible for other routines */
219     Lib     = F;
220     LibName = GetStringId (Name);
221
222     /* Read the remaining header fields (magic is already read) */
223     Header.Magic   = LIB_MAGIC;
224     Header.Version = Read16 (Lib);
225     if (Header.Version != LIB_VERSION) {
226         Error ("Wrong data version in `%s'", Name);
227     }
228     Header.Flags   = Read16 (Lib);
229     Header.IndexOffs = Read32 (Lib);
230
231     /* Seek to the index position and read the index */
232     fseek (Lib, Header.IndexOffs, SEEK_SET);
233     ReadIndex ();
234
235     /* Walk through all library modules and check for each module if there
236      * are unresolved externals in existing modules that may be resolved
237      * by adding the module. Repeat this step until no more object files
238      * were added.
239      */
240     do {
241         HaveAdditions = 0;
242         for (I = 0; I < ModuleCount; ++I) {
243             ObjData* O = Index [I];
244             if ((O->Flags & OBJ_REF) == 0) {
245                 LibCheckExports (O);
246                 if (O->Flags & OBJ_REF) {
247                     /* The routine added the file */
248                     HaveAdditions = 1;
249                 }
250             }
251         }
252     } while (HaveAdditions);
253
254     /* Add the files list and sections for all requested modules */
255     for (I = 0; I < ModuleCount; ++I) {
256
257         /* Get the object data */
258         ObjData* O = Index [I];
259
260         /* Is this object file referenced? */
261         if (O->Flags & OBJ_REF) {
262
263             /* Seek to the start of the object file and read the header */
264             fseek (Lib, O->Start, SEEK_SET);
265             LibReadObjHeader (O, Name);
266
267             /* Seek to the start of the files list and read the files list */
268             fseek (Lib, O->Start + O->Header.FileOffs, SEEK_SET);
269             ObjReadFiles (Lib, O);
270
271             /* Seek to the start of the debug info and read the debug info */
272             fseek (Lib, O->Start + O->Header.DbgSymOffs, SEEK_SET);
273             ObjReadDbgSyms (Lib, O);
274
275             /* Seek to the start of the line infos and read them */
276             fseek (Lib, O->Start + O->Header.LineInfoOffs, SEEK_SET);
277             ObjReadLineInfos (Lib, O);
278
279             /* Seek to the start of the segment list and read the segments.
280              * This must be last, since the data here may reference other
281              * stuff.
282              */
283             fseek (Lib, O->Start + O->Header.SegOffs, SEEK_SET);
284             ObjReadSections (Lib, O);
285
286             /* Add a pointer to the library name */
287             O->LibName = LibName;
288
289             /* All references to strings are now resolved, so we can delete
290              * the module string pool.
291              */
292             FreeObjStrings (O);
293
294             /* Insert the object into the list of all used object files */
295             InsertObjData (O);
296
297         } else {
298
299             /* Unreferenced object file, remove it */
300             FreeObjData (O);
301
302         }
303     }
304
305     /* Done. Close the file, release allocated memory */
306     fclose (F);
307     xfree (Index);
308     Lib         = 0;
309     ModuleCount = 0;
310     Index       = 0;
311 }
312
313
314