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