1 /*****************************************************************************/
5 /* Library data structures and helpers for the ar65 archiver */
9 /* (C) 1998-2011, 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 /*****************************************************************************/
58 /*****************************************************************************/
60 /*****************************************************************************/
64 /* File descriptor for the library file */
67 static const char* LibName = 0;
69 /* The library header */
70 static LibHeader Header = {
79 /*****************************************************************************/
80 /* Writing file data structures */
81 /*****************************************************************************/
85 static void ReadHeader (void)
86 /* Read the header of a library file */
88 /* Seek to position zero */
89 fseek (Lib, 0, SEEK_SET);
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);
96 Header.Version = Read16 (Lib);
97 if (Header.Version != LIB_VERSION) {
98 Error ("Wrong data version in `%s'", LibName);
100 Header.Flags = Read16 (Lib);
101 Header.IndexOffs = Read32 (Lib);
106 static void ReadIndexEntry (void)
107 /* Read one entry in the index */
109 /* Create a new entry and insert it into the list */
110 ObjData* O = NewObjData ();
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);
122 static void ReadIndex (void)
123 /* Read the index of a library file */
127 /* Seek to the start of the index */
128 fseek (Lib, Header.IndexOffs, SEEK_SET);
130 /* Read the object file count and calculate the cross ref size */
131 Count = ReadVar (Lib);
133 /* Read all entries in the index */
138 /* Read basic object file data from the actual entries */
139 for (I = 0; I < CollCount (&ObjPool); ++I) {
141 /* Get the object file entry */
142 ObjData* O = CollAtUnchecked (&ObjPool, I);
145 ObjReadData (Lib, O);
151 /*****************************************************************************/
152 /* Writing file data structures */
153 /*****************************************************************************/
157 static void WriteHeader (void)
158 /* Write the header to the library file */
160 /* Seek to position zero */
161 fseek (NewLib, 0, SEEK_SET);
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);
172 static void WriteIndexEntry (const ObjData* O)
173 /* Write one index entry */
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);
185 static void WriteIndex (void)
186 /* Write the index of a library file */
190 /* Sync I/O in case the last operation was a read */
191 fseek (NewLib, 0, SEEK_CUR);
193 /* Remember the current offset in the header */
194 Header.IndexOffs = ftell (NewLib);
196 /* Write the object file count */
197 WriteVar (NewLib, CollCount (&ObjPool));
199 /* Write the object files */
200 for (I = 0; I < CollCount (&ObjPool); ++I) {
201 WriteIndexEntry (CollConstAt (&ObjPool, I));
207 /*****************************************************************************/
208 /* High level stuff */
209 /*****************************************************************************/
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
219 /* Remember the name */
220 LibName = xstrdup (Name);
222 /* Open the existing library for reading */
223 Lib = fopen (Name, "rb");
226 /* File does not exist */
228 Error ("Library `%s' does not exist", Name);
230 Warning ("Library `%s' not found - will be created", Name);
235 /* We have an existing file: Read the header */
238 /* Now read the existing index */
244 /* Create the temporary library */
247 Error ("Cannot create temporary file: %s", strerror (errno));
250 /* Write a dummy header to the temp file */
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.
262 unsigned char Buf [4096];
264 /* Remember the position */
265 unsigned long Pos = ftell (NewLib);
269 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
270 ReadData (F, Buf, Count);
271 WriteData (NewLib, Buf, Count);
275 /* Return the start position */
281 void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
282 /* Copy data from the library file into another file */
284 unsigned char Buf [4096];
286 /* Seek to the correct position */
287 fseek (Lib, Pos, SEEK_SET);
291 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
292 ReadData (Lib, Buf, Count);
293 WriteData (F, Buf, Count);
300 static void LibCheckExports (ObjData* O)
301 /* Insert all exports from the given object file into the global list
302 * checking for duplicates.
307 /* Let the user know what we do */
308 Print (stdout, 1, "Module `%s' (%u exports):\n", O->Name, CollCount (&O->Exports));
310 /* Insert the exports into the global table */
311 for (I = 0; I < CollCount (&O->Exports); ++I) {
313 /* Get the name of the export */
314 const char* Name = CollConstAt (&O->Exports, I);
316 /* Insert the name into the hash table */
317 Print (stdout, 1, " %s\n", Name);
325 /* Write remaining data, close both files and copy the temp file to the old
329 /* Do we have a temporary library? */
333 unsigned char Buf [4096];
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.
340 for (I = 0; I < CollCount (&ObjPool); ++I) {
342 /* Get a pointer to the object */
343 ObjData* O = CollAtUnchecked (&ObjPool, I);
345 /* Check exports, make global export table */
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;
358 /* Write the index */
361 /* Write the updated header */
365 if (Lib && fclose (Lib) != 0) {
366 Error ("Error closing library: %s", strerror (errno));
369 /* Reopen the library and truncate it */
370 Lib = fopen (LibName, "wb");
372 Error ("Cannot open library `%s' for writing: %s",
373 LibName, strerror (errno));
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));
385 /* Close both files */
386 if (Lib && fclose (Lib) != 0) {
387 Error ("Problem closing `%s': %s", LibName, strerror (errno));
389 if (NewLib && fclose (NewLib) != 0) {
390 Error ("Problem closing temporary library file: %s", strerror (errno));