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