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