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