1 /*****************************************************************************/
5 /* Library data structures and helpers for the ar65 archiver */
9 /* (C) 1998-2010, Ullrich von Bassewitz */
10 /* Roemerstrasse 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 /*****************************************************************************/
59 /*****************************************************************************/
61 /*****************************************************************************/
65 /* File descriptor for the library file */
68 static const char* LibName = 0;
70 /* The library header */
71 static LibHeader Header = {
80 /*****************************************************************************/
81 /* Writing file data structures */
82 /*****************************************************************************/
86 static void ReadHeader (void)
87 /* Read the header of a library file */
89 /* Seek to position zero */
90 fseek (Lib, 0, SEEK_SET);
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);
97 Header.Version = Read16 (Lib);
98 if (Header.Version != LIB_VERSION) {
99 Error ("Wrong data version in `%s'", LibName);
101 Header.Flags = Read16 (Lib);
102 Header.IndexOffs = Read32 (Lib);
107 static void ReadIndexEntry (void)
108 /* Read one entry in the index */
112 /* Create a new entry and insert it into the list */
113 ObjData* O = NewObjData ();
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);
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);
130 O->ImportSize = ReadVar (Lib);
131 O->Imports = xmalloc (O->ImportSize);
132 ReadData (Lib, O->Imports, O->ImportSize);
135 O->ExportSize = ReadVar (Lib);
136 O->Exports = xmalloc (O->ExportSize);
137 ReadData (Lib, O->Exports, O->ExportSize);
142 static void ReadIndex (void)
143 /* Read the index of a library file */
147 /* Seek to the start of the index */
148 fseek (Lib, Header.IndexOffs, SEEK_SET);
150 /* Read the object file count and calculate the cross ref size */
151 Count = ReadVar (Lib);
153 /* Read all entries in the index */
161 /*****************************************************************************/
162 /* Writing file data structures */
163 /*****************************************************************************/
167 static void WriteHeader (void)
168 /* Write the header to the library file */
170 /* Seek to position zero */
171 fseek (NewLib, 0, SEEK_SET);
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);
182 static void WriteIndexEntry (ObjData* O)
183 /* Write one index entry */
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);
195 WriteVar (NewLib, O->StringCount);
196 for (I = 0; I < O->StringCount; ++I) {
197 WriteStr (NewLib, O->Strings[I]);
201 WriteVar (NewLib, O->ImportSize);
202 WriteData (NewLib, O->Imports, O->ImportSize);
205 WriteVar (NewLib, O->ExportSize);
206 WriteData (NewLib, O->Exports, O->ExportSize);
211 static void WriteIndex (void)
212 /* Write the index of a library file */
216 /* Sync I/O in case the last operation was a read */
217 fseek (NewLib, 0, SEEK_CUR);
219 /* Remember the current offset in the header */
220 Header.IndexOffs = ftell (NewLib);
222 /* Write the object file count */
223 WriteVar (NewLib, ObjCount);
225 /* Write the object files */
235 /*****************************************************************************/
236 /* High level stuff */
237 /*****************************************************************************/
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
247 /* Remember the name */
248 LibName = xstrdup (Name);
250 /* Open the existing library for reading */
251 Lib = fopen (Name, "rb");
254 /* File does not exist */
256 Error ("Library `%s' does not exist", Name);
258 Warning ("Library `%s' not found - will be created", Name);
263 /* We have an existing file: Read the header */
266 /* Now read the existing index */
272 /* Create the temporary library */
275 Error ("Cannot create temporary file: %s", strerror (errno));
278 /* Write a dummy header to the temp file */
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.
290 unsigned char Buf [4096];
292 /* Remember the position */
293 unsigned long Pos = ftell (NewLib);
297 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
298 ReadData (F, Buf, Count);
299 WriteData (NewLib, Buf, Count);
303 /* Return the start position */
309 void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
310 /* Copy data from the library file into another file */
312 unsigned char Buf [4096];
314 /* Seek to the correct position */
315 fseek (Lib, Pos, SEEK_SET);
319 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
320 ReadData (Lib, Buf, Count);
321 WriteData (F, Buf, Count);
328 static unsigned long GetVar (unsigned char** Buf)
329 /* Get a variable sized value from Buf */
338 /* Add this char to the value */
339 V |= ((unsigned long)(C & 0x7F)) << Shift;
344 /* Return the result */
350 static void SkipExpr (unsigned char** Buf)
351 /* Skip an expression in Buf */
353 /* Get the operation and skip it */
354 unsigned char Op = **Buf;
357 /* Filter leaf nodes */
364 /* 32 bit literal value */
369 /* Variable seized symbol index */
374 /* 8 bit segment number */
379 /* What's left are unary and binary nodes */
380 SkipExpr (Buf); /* Skip left */
381 SkipExpr (Buf); /* Skip right */
386 static void SkipFilePos (unsigned char** Buf)
387 /* Skip a file position in Buf */
389 (void) GetVar (Buf); /* Line */
390 (void) GetVar (Buf); /* Col */
391 (void) GetVar (Buf); /* Name */
396 static void LibCheckExports (ObjData* O)
397 /* Insert all exports from the given object file into the global list
398 * checking for duplicates.
401 /* Get a pointer to the buffer */
402 unsigned char* Exports = O->Exports;
404 /* Get the export count */
405 unsigned Count = GetVar (&Exports);
407 /* Read the exports */
408 Print (stdout, 1, "Module `%s' (%u exports):\n", O->Name, Count);
413 /* Get the export tag and skip the address size */
414 unsigned Type = GetVar (&Exports);
417 /* condes decls may follow */
418 Exports += SYM_GET_CONDES_COUNT (Type);
420 /* Next thing is index of name of symbol */
421 Name = GetObjString (O, GetVar (&Exports));
423 /* Skip value of symbol */
424 if (SYM_IS_EXPR (Type)) {
425 /* Expression tree */
428 /* Constant 32 bit value */
432 /* Skip the position */
433 SkipFilePos (&Exports);
435 /* Insert the name into the hash table */
436 Print (stdout, 1, " %s\n", Name);
437 ExpInsert (Name, O->Index);
444 /* Write remaining data, close both files and copy the temp file to the old
448 /* Do we have a temporary library? */
452 unsigned char Buf [4096];
455 /* Index the object files and make an array containing the objects */
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.
462 for (I = 0; I < ObjCount; ++I) {
464 /* Get a pointer to the object */
465 ObjData* O = ObjPool [I];
467 /* Check exports, make global export table */
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;
480 /* Write the index */
483 /* Write the updated header */
487 if (Lib && fclose (Lib) != 0) {
488 Error ("Error closing library: %s", strerror (errno));
491 /* Reopen the library and truncate it */
492 Lib = fopen (LibName, "wb");
494 Error ("Cannot open library `%s' for writing: %s",
495 LibName, strerror (errno));
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));
507 /* Close both files */
508 if (Lib && fclose (Lib) != 0) {
509 Error ("Problem closing `%s': %s", LibName, strerror (errno));
511 if (NewLib && fclose (NewLib) != 0) {
512 Error ("Problem closing temporary library file: %s", strerror (errno));