1 /*****************************************************************************/
5 /* Library data structures and helpers for the ld65 linker */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
66 static char* LibName = 0;
67 static unsigned ModuleCount = 0;
68 static ObjData** Index = 0;
72 /*****************************************************************************/
73 /* Reading file data structures */
74 /*****************************************************************************/
78 static void LibReadObjHeader (ObjData* O)
79 /* Read the header of the object file checking the signature */
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);
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);
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);
112 static ObjData* ReadIndexEntry (void)
113 /* Read one entry in the index */
117 /* Create a new entry and insert it into the list */
118 ObjData* O = NewObjData ();
120 /* Module name/flags/MTime/Start/Size */
121 O->Name = ReadStr (Lib);
122 O->Flags = Read16 (Lib);
123 Read32 (Lib); /* Skip MTime */
124 O->Start = Read32 (Lib);
125 Read32 (Lib); /* Skip Size */
127 /* Read the string pool */
128 ObjReadStrPool (Lib, O);
130 /* Skip the export size, then read the exports */
131 (void) ReadVar (Lib);
132 O->ExportCount = ReadVar (Lib);
133 O->Exports = xmalloc (O->ExportCount * sizeof (Export*));
134 for (I = 0; I < O->ExportCount; ++I) {
135 O->Exports[I] = ReadExport (Lib, O);
138 /* Skip the import size, then read the imports */
139 (void) ReadVar (Lib);
140 O->ImportCount = ReadVar (Lib);
141 O->Imports = xmalloc (O->ImportCount * sizeof (Import*));
142 for (I = 0; I < O->ImportCount; ++I) {
143 O->Imports[I] = ReadImport (Lib, O);
152 static void ReadIndex (void)
153 /* Read the index of a library file */
157 /* Read the object file count and allocate memory */
158 ModuleCount = ReadVar (Lib);
159 Index = xmalloc (ModuleCount * sizeof (ObjData*));
161 /* Read all entries in the index */
162 for (I = 0; I < ModuleCount; ++I) {
163 Index[I] = ReadIndexEntry ();
169 /*****************************************************************************/
170 /* High level stuff */
171 /*****************************************************************************/
175 static void LibCheckExports (ObjData* O)
176 /* Check if the exports from this file can satisfy any import requests. If so,
177 * insert the imports and exports from this file and mark the file as added.
182 /* Check all exports */
183 for (I = 0; I < O->ExportCount; ++I) {
184 if (IsUnresolved (O->Exports[I]->Name)) {
185 /* We need this module */
191 /* If we need this module, insert the imports and exports */
192 if (O->Flags & OBJ_REF) {
193 /* Insert the exports */
194 for (I = 0; I < O->ExportCount; ++I) {
195 InsertExport (O->Exports[I]);
197 /* Insert the imports */
198 for (I = 0; I < O->ImportCount; ++I) {
199 InsertImport (O->Imports[I]);
206 void LibAdd (FILE* F, const char* Name)
207 /* Add files from the library to the list if there are references that could
215 /* Store the parameters, so they're visible for other routines */
217 LibName = xstrdup (Name);
219 /* Read the remaining header fields (magic is already read) */
220 Header.Magic = LIB_MAGIC;
221 Header.Version = Read16 (Lib);
222 if (Header.Version != LIB_VERSION) {
223 Error ("Wrong data version in `%s'", Name);
225 Header.Flags = Read16 (Lib);
226 Header.IndexOffs = Read32 (Lib);
228 /* Seek to the index position and read the index */
229 fseek (Lib, Header.IndexOffs, SEEK_SET);
232 /* Walk through all library modules and check for each module if there
233 * are unresolved externals in existing modules that may be resolved
234 * by adding the module. Repeat this step until no more object files
239 for (I = 0; I < ModuleCount; ++I) {
240 ObjData* O = Index [I];
241 if ((O->Flags & OBJ_REF) == 0) {
243 if (O->Flags & OBJ_REF) {
244 /* The routine added the file */
251 /* Add the files list and sections for all requested modules */
252 for (I = 0; I < ModuleCount; ++I) {
253 ObjData* O = Index [I];
254 if (O->Flags & OBJ_REF) {
256 /* Seek to the start of the object file and read the header */
257 fseek (Lib, O->Start, SEEK_SET);
258 LibReadObjHeader (O);
260 /* Seek to the start of the files list and read the files list */
261 fseek (Lib, O->Start + O->Header.FileOffs, SEEK_SET);
262 ObjReadFiles (Lib, O);
264 /* Seek to the start of the debug info and read the debug info */
265 fseek (Lib, O->Start + O->Header.DbgSymOffs, SEEK_SET);
266 ObjReadDbgSyms (Lib, O);
268 /* Seek to the start of the line infos and read them */
269 fseek (Lib, O->Start + O->Header.LineInfoOffs, SEEK_SET);
270 ObjReadLineInfos (Lib, O);
272 /* Seek to the start of the segment list and read the segments.
273 * This must be last, since the data here may reference other
276 fseek (Lib, O->Start + O->Header.SegOffs, SEEK_SET);
277 ObjReadSections (Lib, O);
279 /* We have the data now */
280 O->Flags |= OBJ_HAVEDATA;
284 /* Add a pointer to the library name */
285 O->LibName = LibName;
288 /* Done. Close the file, release allocated memory */