]> git.sur5r.net Git - cc65/blob - src/ar65/library.c
cffb85cd1a2a6798837def07b6b15e3bb334ed2a
[cc65] / src / ar65 / library.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 library.c                                 */
4 /*                                                                           */
5 /*         Library data structures and helpers for the ar65 archiver         */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2011, Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 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 "libdefs.h"
43 #include "print.h"
44 #include "symdefs.h"
45 #include "xmalloc.h"
46
47 /* ar65 */
48 #include "error.h"
49 #include "exports.h"
50 #include "fileio.h"
51 #include "global.h"
52 #include "library.h"
53 #include "objdata.h"
54 #include "objfile.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Data                                    */
60 /*****************************************************************************/
61
62
63
64 /* File descriptor for the library file */
65 FILE*                   NewLib = 0;
66 static FILE*            Lib = 0;
67 static const char*      LibName = 0;
68
69 /* The library header */
70 static LibHeader        Header = {
71     LIB_MAGIC,
72     LIB_VERSION,
73     0,
74     0
75 };
76
77
78
79 /*****************************************************************************/
80 /*                       Writing file data structures                        */
81 /*****************************************************************************/
82
83
84
85 static void ReadHeader (void)
86 /* Read the header of a library file */
87 {
88     /* Seek to position zero */
89     fseek (Lib, 0, SEEK_SET);
90
91     /* Read the header fields, checking magic and version */
92     Header.Magic   = Read32 (Lib);
93     if (Header.Magic != LIB_MAGIC) {
94         Error ("`%s' is not a valid library file", LibName);
95     }
96     Header.Version = Read16 (Lib);
97     if (Header.Version != LIB_VERSION) {
98         Error ("Wrong data version in `%s'", LibName);
99     }
100     Header.Flags   = Read16 (Lib);
101     Header.IndexOffs = Read32 (Lib);
102 }
103
104
105
106 static void ReadIndexEntry (void)
107 /* Read one entry in the index */
108 {
109     /* Create a new entry and insert it into the list */
110     ObjData* O  = NewObjData ();
111
112     /* Module name/flags/MTime/Start/Size */
113     O->Name     = ReadStr (Lib);
114     O->Flags    = Read16 (Lib);
115     O->MTime    = Read32 (Lib);
116     O->Start    = Read32 (Lib);
117     O->Size     = Read32 (Lib);
118 }
119
120
121
122 static void ReadIndex (void)
123 /* Read the index of a library file */
124 {
125     unsigned Count, I;
126
127     /* Seek to the start of the index */
128     fseek (Lib, Header.IndexOffs, SEEK_SET);
129
130     /* Read the object file count and calculate the cross ref size */
131     Count = ReadVar (Lib);
132
133     /* Read all entries in the index */
134     while (Count--) {
135         ReadIndexEntry ();
136     }
137
138     /* Read basic object file data from the actual entries */
139     for (I = 0; I < CollCount (&ObjPool); ++I) {
140
141         /* Get the object file entry */
142         ObjData* O = CollAtUnchecked (&ObjPool, I);
143
144         /* Read data */
145         ObjReadData (Lib, O);
146     }
147 }
148
149
150
151 /*****************************************************************************/
152 /*                       Writing file data structures                        */
153 /*****************************************************************************/
154
155
156
157 static void WriteHeader (void)
158 /* Write the header to the library file */
159 {
160     /* Seek to position zero */
161     fseek (NewLib, 0, SEEK_SET);
162
163     /* Write the header fields */
164     Write32 (NewLib, Header.Magic);
165     Write16 (NewLib, Header.Version);
166     Write16 (NewLib, Header.Flags);
167     Write32 (NewLib, Header.IndexOffs);
168 }
169
170
171
172 static void WriteIndexEntry (const ObjData* O)
173 /* Write one index entry */
174 {
175     /* Module name/flags/MTime/start/size */
176     WriteStr (NewLib, O->Name);
177     Write16  (NewLib, O->Flags & ~OBJ_HAVEDATA);
178     Write32  (NewLib, O->MTime);
179     Write32  (NewLib, O->Start);
180     Write32  (NewLib, O->Size);
181 }
182
183
184
185 static void WriteIndex (void)
186 /* Write the index of a library file */
187 {
188     unsigned I;
189
190     /* Sync I/O in case the last operation was a read */
191     fseek (NewLib, 0, SEEK_CUR);
192
193     /* Remember the current offset in the header */
194     Header.IndexOffs = ftell (NewLib);
195
196     /* Write the object file count */
197     WriteVar (NewLib, CollCount (&ObjPool));
198
199     /* Write the object files */
200     for (I = 0; I < CollCount (&ObjPool); ++I) {
201         WriteIndexEntry (CollConstAt (&ObjPool, I));
202     }
203 }
204
205
206
207 /*****************************************************************************/
208 /*                             High level stuff                              */
209 /*****************************************************************************/
210
211
212
213 void LibOpen (const char* Name, int MustExist, int NeedTemp)
214 /* Open an existing library and a temporary copy. If MustExist is true, the
215  * old library is expected to exist. If NeedTemp is true, a temporary library
216  * is created.
217  */
218 {
219     /* Remember the name */
220     LibName = xstrdup (Name);
221
222     /* Open the existing library for reading */
223     Lib = fopen (Name, "rb");
224     if (Lib == 0) {
225
226         /* File does not exist */
227         if (MustExist) {
228             Error ("Library `%s' does not exist", Name);
229         } else {
230             Warning ("Library `%s' not found - will be created", Name);
231         }
232
233     } else {
234
235         /* We have an existing file: Read the header */
236         ReadHeader ();
237
238         /* Now read the existing index */
239         ReadIndex ();
240
241     }
242
243     if (NeedTemp) {
244         /* Create the temporary library */
245         NewLib = tmpfile ();
246         if (NewLib == 0) {
247             Error ("Cannot create temporary file: %s", strerror (errno));
248         }
249
250         /* Write a dummy header to the temp file */
251         WriteHeader ();
252     }
253 }
254
255
256
257 unsigned long LibCopyTo (FILE* F, unsigned long Bytes)
258 /* Copy data from F to the temp library file, return the start position in
259  * the temporary library file.
260  */
261 {
262     unsigned char Buf [4096];
263
264     /* Remember the position */
265     unsigned long Pos = ftell (NewLib);
266
267     /* Copy loop */
268     while (Bytes) {
269         unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
270         ReadData (F, Buf, Count);
271         WriteData (NewLib, Buf, Count);
272         Bytes -= Count;
273     }
274
275     /* Return the start position */
276     return Pos;
277 }
278
279
280
281 void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
282 /* Copy data from the library file into another file */
283 {
284     unsigned char Buf [4096];
285
286     /* Seek to the correct position */
287     fseek (Lib, Pos, SEEK_SET);
288
289     /* Copy loop */
290     while (Bytes) {
291         unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
292         ReadData (Lib, Buf, Count);
293         WriteData (F, Buf, Count);
294         Bytes -= Count;
295     }
296 }
297
298
299
300 static void LibCheckExports (ObjData* O)
301 /* Insert all exports from the given object file into the global list
302  * checking for duplicates.
303  */
304 {
305     unsigned I;
306
307     /* Let the user know what we do */
308     Print (stdout, 1, "Module `%s' (%u exports):\n", O->Name, CollCount (&O->Exports));
309
310     /* Insert the exports into the global table */
311     for (I = 0; I < CollCount (&O->Exports); ++I) {
312
313         /* Get the name of the export */
314         const char* Name = CollConstAt (&O->Exports, I);
315
316         /* Insert the name into the hash table */
317         Print (stdout, 1, "  %s\n", Name);
318         ExpInsert (Name, O);
319     }
320 }
321
322
323
324 void LibClose (void)
325 /* Write remaining data, close both files and copy the temp file to the old
326  * filename
327  */
328 {
329     /* Do we have a temporary library? */
330     if (NewLib) {
331
332         unsigned I;
333         unsigned char Buf [4096];
334         size_t Count;
335
336         /* Walk through the object file list, inserting exports into the
337          * export list checking for duplicates. Copy any data that is still
338          * in the old library into the new one.
339          */
340         for (I = 0; I < CollCount (&ObjPool); ++I) {
341
342             /* Get a pointer to the object */
343             ObjData* O = CollAtUnchecked (&ObjPool, I);
344
345             /* Check exports, make global export table */
346             LibCheckExports (O);
347
348             /* Copy data if needed */
349             if ((O->Flags & OBJ_HAVEDATA) == 0) {
350                 /* Data is still in the old library */
351                 fseek (Lib, O->Start, SEEK_SET);
352                 O->Start = ftell (NewLib);
353                 LibCopyTo (Lib, O->Size);
354                 O->Flags |= OBJ_HAVEDATA;
355             }
356         }
357
358         /* Write the index */
359         WriteIndex ();
360
361         /* Write the updated header */
362         WriteHeader ();
363
364         /* Close the file */
365         if (Lib && fclose (Lib) != 0) {
366             Error ("Error closing library: %s", strerror (errno));
367         }
368
369         /* Reopen the library and truncate it */
370         Lib = fopen (LibName, "wb");
371         if (Lib == 0) {
372             Error ("Cannot open library `%s' for writing: %s",
373                    LibName, strerror (errno));
374         }
375
376         /* Copy the new library to the new one */
377         fseek (NewLib, 0, SEEK_SET);
378         while ((Count = fread (Buf, 1, sizeof (Buf), NewLib)) != 0) {
379             if (fwrite (Buf, 1, Count, Lib) != Count) {
380                 Error ("Cannot write to `%s': %s", LibName, strerror (errno));
381             }
382         }
383     }
384
385     /* Close both files */
386     if (Lib && fclose (Lib) != 0) {
387         Error ("Problem closing `%s': %s", LibName, strerror (errno));
388     }
389     if (NewLib && fclose (NewLib) != 0) {
390         Error ("Problem closing temporary library file: %s", strerror (errno));
391     }                                      
392 }
393
394
395