]> git.sur5r.net Git - cc65/blob - src/ar65/library.c
Added a new option --dep-target to the compiler. This option allows to set the
[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-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstraße 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 "bitops.h"
42 #include "exprdefs.h"
43 #include "filepos.h"
44 #include "libdefs.h"
45 #include "print.h"
46 #include "symdefs.h"
47 #include "xmalloc.h"
48
49 /* ar65 */
50 #include "error.h"
51 #include "global.h"
52 #include "fileio.h"
53 #include "objdata.h"
54 #include "exports.h"
55 #include "library.h"
56
57
58
59 /*****************************************************************************/
60 /*                                   Data                                    */
61 /*****************************************************************************/
62
63
64
65 /* File descriptor for the library file */
66 FILE*                   NewLib = 0;
67 static FILE*            Lib = 0;
68 static const char*      LibName = 0;
69
70 /* The library header */
71 static LibHeader        Header = {
72     LIB_MAGIC,
73     LIB_VERSION,
74     0,
75     0
76 };
77
78
79
80 /*****************************************************************************/
81 /*                       Writing file data structures                        */
82 /*****************************************************************************/
83
84
85
86 static void ReadHeader (void)
87 /* Read the header of a library file */
88 {
89     /* Seek to position zero */
90     fseek (Lib, 0, SEEK_SET);
91
92     /* Read the header fields, checking magic and version */
93     Header.Magic   = Read32 (Lib);
94     if (Header.Magic != LIB_MAGIC) {
95         Error ("`%s' is not a valid library file", LibName);
96     }
97     Header.Version = Read16 (Lib);
98     if (Header.Version != LIB_VERSION) {
99         Error ("Wrong data version in `%s'", LibName);
100     }
101     Header.Flags   = Read16 (Lib);
102     Header.IndexOffs = Read32 (Lib);
103 }
104
105
106
107 static void ReadIndexEntry (void)
108 /* Read one entry in the index */
109 {
110     unsigned I;
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     /* Strings */
123     O->StringCount = ReadVar (Lib);
124     O->Strings     = xmalloc (O->StringCount * sizeof (char*));
125     for (I = 0; I < O->StringCount; ++I) {
126         O->Strings[I] = ReadStr (Lib);
127     }
128
129     /* Exports */
130     O->ExportSize = ReadVar (Lib);
131     O->Exports    = xmalloc (O->ExportSize);
132     ReadData (Lib, O->Exports, O->ExportSize);
133
134     /* Imports */
135     O->ImportSize = ReadVar (Lib);
136     O->Imports    = xmalloc (O->ImportSize);
137     ReadData (Lib, O->Imports, O->ImportSize);
138 }
139
140
141
142 static void ReadIndex (void)
143 /* Read the index of a library file */
144 {
145     unsigned Count;
146
147     /* Seek to the start of the index */
148     fseek (Lib, Header.IndexOffs, SEEK_SET);
149
150     /* Read the object file count and calculate the cross ref size */
151     Count = ReadVar (Lib);
152
153     /* Read all entries in the index */
154     while (Count--) {
155         ReadIndexEntry ();
156     }
157 }
158
159
160
161 /*****************************************************************************/
162 /*                       Writing file data structures                        */
163 /*****************************************************************************/
164
165
166
167 static void WriteHeader (void)
168 /* Write the header to the library file */
169 {
170     /* Seek to position zero */
171     fseek (NewLib, 0, SEEK_SET);
172
173     /* Write the header fields */
174     Write32 (NewLib, Header.Magic);
175     Write16 (NewLib, Header.Version);
176     Write16 (NewLib, Header.Flags);
177     Write32 (NewLib, Header.IndexOffs);
178 }
179
180
181
182 static void WriteIndexEntry (ObjData* O)
183 /* Write one index entry */
184 {
185     unsigned I;
186
187     /* Module name/flags/MTime/start/size */
188     WriteStr (NewLib, O->Name);
189     Write16  (NewLib, O->Flags & ~OBJ_HAVEDATA);
190     Write32  (NewLib, O->MTime);
191     Write32  (NewLib, O->Start);
192     Write32  (NewLib, O->Size);
193
194     /* Strings */
195     WriteVar (NewLib, O->StringCount);
196     for (I = 0; I < O->StringCount; ++I) {
197         WriteStr (NewLib, O->Strings[I]);
198     }
199
200     /* Exports */
201     WriteVar (NewLib, O->ExportSize);
202     WriteData (NewLib, O->Exports, O->ExportSize);
203
204     /* Imports */
205     WriteVar (NewLib, O->ImportSize);
206     WriteData (NewLib, O->Imports, O->ImportSize);
207 }
208
209
210
211 static void WriteIndex (void)
212 /* Write the index of a library file */
213 {
214     ObjData* O;
215
216     /* Sync I/O in case the last operation was a read */
217     fseek (NewLib, 0, SEEK_CUR);
218
219     /* Remember the current offset in the header */
220     Header.IndexOffs = ftell (NewLib);
221
222     /* Write the object file count */
223     WriteVar (NewLib, ObjCount);
224
225     /* Write the object files */
226     O = ObjRoot;
227     while (O) {
228         WriteIndexEntry (O);
229         O = O->Next;
230     }
231 }
232
233
234
235 /*****************************************************************************/
236 /*                             High level stuff                              */
237 /*****************************************************************************/
238
239
240
241 void LibOpen (const char* Name, int MustExist, int NeedTemp)
242 /* Open an existing library and a temporary copy. If MustExist is true, the
243  * old library is expected to exist. If NeedTemp is true, a temporary library
244  * is created.
245  */
246 {
247     /* Remember the name */
248     LibName = xstrdup (Name);
249
250     /* Open the existing library for reading */
251     Lib = fopen (Name, "rb");
252     if (Lib == 0) {
253
254         /* File does not exist */
255         if (MustExist) {
256             Error ("Library `%s' does not exist", Name);
257         } else {
258             Warning ("Library `%s' not found - will be created", Name);
259         }
260
261     } else {
262
263         /* We have an existing file: Read the header */
264         ReadHeader ();
265
266         /* Now read the existing index */
267         ReadIndex ();
268
269     }
270
271     if (NeedTemp) {
272         /* Create the temporary library */
273         NewLib = tmpfile ();
274         if (NewLib == 0) {
275             Error ("Cannot create temporary file: %s", strerror (errno));
276         }
277
278         /* Write a dummy header to the temp file */
279         WriteHeader ();
280     }
281 }
282
283
284
285 unsigned long LibCopyTo (FILE* F, unsigned long Bytes)
286 /* Copy data from F to the temp library file, return the start position in
287  * the temporary library file.
288  */
289 {
290     unsigned char Buf [4096];
291
292     /* Remember the position */
293     unsigned long Pos = ftell (NewLib);
294
295     /* Copy loop */
296     while (Bytes) {
297         unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
298         ReadData (F, Buf, Count);
299         WriteData (NewLib, Buf, Count);
300         Bytes -= Count;
301     }
302
303     /* Return the start position */
304     return Pos;
305 }
306
307
308
309 void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
310 /* Copy data from the library file into another file */
311 {
312     unsigned char Buf [4096];
313
314     /* Seek to the correct position */
315     fseek (Lib, Pos, SEEK_SET);
316
317     /* Copy loop */
318     while (Bytes) {
319         unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
320         ReadData (Lib, Buf, Count);
321         WriteData (F, Buf, Count);
322         Bytes -= Count;
323     }
324 }
325
326
327
328 static unsigned long GetVar (unsigned char** Buf)
329 /* Get a variable sized value from Buf */
330 {
331     unsigned char C;
332     unsigned long V = 0;
333     unsigned Shift = 0;
334     do {
335         /* Read one byte */
336         C = **Buf;
337         ++(*Buf);
338         /* Add this char to the value */
339         V |= ((unsigned long)(C & 0x7F)) << Shift;
340         /* Next value */
341         Shift += 7;
342     } while (C & 0x80);
343
344     /* Return the result */
345     return V;
346 }
347
348
349
350 static void SkipExpr (unsigned char** Buf)
351 /* Skip an expression in Buf */
352 {
353     /* Get the operation and skip it */
354     unsigned char Op = **Buf;
355     ++(*Buf);
356
357     /* Filter leaf nodes */
358     switch (Op) {
359
360         case EXPR_NULL:
361             return;
362
363         case EXPR_LITERAL:
364             /* 32 bit literal value */
365             *Buf += 4;
366             return;
367
368         case EXPR_SYMBOL:
369             /* Variable seized symbol index */
370             (void) GetVar (Buf);
371             return;
372
373         case EXPR_SECTION:
374             /* 8 bit segment number */
375             *Buf += 1;
376             return;
377     }
378
379     /* What's left are unary and binary nodes */
380     SkipExpr (Buf);             /* Skip left */
381     SkipExpr (Buf);             /* Skip right */
382 }
383
384
385
386 static void SkipFilePos (unsigned char** Buf)
387 /* Skip a file position in Buf */
388 {
389     (void) GetVar (Buf);        /* Line */
390     (void) GetVar (Buf);        /* Col */
391     (void) GetVar (Buf);        /* Name */
392 }
393
394
395
396 static void LibCheckExports (ObjData* O)
397 /* Insert all exports from the given object file into the global list
398  * checking for duplicates.
399  */
400 {
401     /* Get a pointer to the buffer */
402     unsigned char* Exports = O->Exports;
403
404     /* Get the export count */
405     unsigned Count = GetVar (&Exports);
406
407     /* Read the exports */
408     Print (stdout, 1, "Module `%s' (%u exports):\n", O->Name, Count);
409     while (Count--) {
410
411         const char*     Name;
412
413         /* Get the export tag and skip the address size */
414         unsigned char Type = *Exports++;
415         ++Exports;
416
417         /* condes decls may follow */
418         Exports += GET_EXP_CONDES_COUNT (Type);
419
420         /* Next thing is index of name of symbol */
421         Name = GetObjString (O, GetVar (&Exports));
422
423         /* Skip value of symbol */
424         if (Type & EXP_EXPR) {
425             /* Expression tree */
426             SkipExpr (&Exports);
427         } else {
428             /* Constant 32 bit value */
429             Exports += 4;
430         }
431
432         /* Skip the position */
433         SkipFilePos (&Exports);
434
435         /* Insert the name into the hash table */
436         Print (stdout, 1, "  %s\n", Name);
437         ExpInsert (Name, O->Index);
438     }
439 }
440
441
442
443 void LibClose (void)
444 /* Write remaining data, close both files and copy the temp file to the old
445  * filename
446  */
447 {
448     /* Do we have a temporary library? */
449     if (NewLib) {
450
451         unsigned I;
452         unsigned char Buf [4096];
453         size_t Count;
454
455         /* Index the object files and make an array containing the objects */
456         MakeObjPool ();
457
458         /* Walk through the object file list, inserting exports into the
459          * export list checking for duplicates. Copy any data that is still
460          * in the old library into the new one.
461          */
462         for (I = 0; I < ObjCount; ++I) {
463
464             /* Get a pointer to the object */
465             ObjData* O = ObjPool [I];
466
467             /* Check exports, make global export table */
468             LibCheckExports (O);
469
470             /* Copy data if needed */
471             if ((O->Flags & OBJ_HAVEDATA) == 0) {
472                 /* Data is still in the old library */
473                 fseek (Lib, O->Start, SEEK_SET);
474                 O->Start = ftell (NewLib);
475                 LibCopyTo (Lib, O->Size);
476                 O->Flags |= OBJ_HAVEDATA;
477             }
478         }
479
480         /* Write the index */
481         WriteIndex ();
482
483         /* Write the updated header */
484         WriteHeader ();
485
486         /* Close the file */
487         if (Lib && fclose (Lib) != 0) {
488             Error ("Error closing library: %s", strerror (errno));
489         }
490
491         /* Reopen the library and truncate it */
492         Lib = fopen (LibName, "wb");
493         if (Lib == 0) {
494             Error ("Cannot open library `%s' for writing: %s",
495                    LibName, strerror (errno));
496         }
497
498         /* Copy the new library to the new one */
499         fseek (NewLib, 0, SEEK_SET);
500         while ((Count = fread (Buf, 1, sizeof (Buf), NewLib)) != 0) {
501             if (fwrite (Buf, 1, Count, Lib) != Count) {
502                 Error ("Cannot write to `%s': %s", LibName, strerror (errno));
503             }
504         }
505     }
506
507     /* Close both files */
508     if (Lib && fclose (Lib) != 0) {
509         Error ("Problem closing `%s': %s", LibName, strerror (errno));
510     }
511     if (NewLib && fclose (NewLib) != 0) {
512         Error ("Problem closing temporary library file: %s", strerror (errno));
513     }
514 }
515
516
517