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