1 /*****************************************************************************/
5 /* Library data structures and helpers for the ar65 archiver */
9 /* (C) 1998-2013, 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 /* Name of the library file */
66 const char* LibName = 0;
68 /* File descriptor for the library file */
72 /* The library header */
73 static LibHeader Header = {
82 /*****************************************************************************/
83 /* Writing file data structures */
84 /*****************************************************************************/
88 static void ReadHeader (void)
89 /* Read the header of a library file */
91 /* Seek to position zero */
92 fseek (Lib, 0, SEEK_SET);
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);
99 Header.Version = Read16 (Lib);
100 if (Header.Version != LIB_VERSION) {
101 Error ("Wrong data version in `%s'", LibName);
103 Header.Flags = Read16 (Lib);
104 Header.IndexOffs = Read32 (Lib);
109 static void ReadIndexEntry (void)
110 /* 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);
125 static void ReadIndex (void)
126 /* Read the index of a library file */
130 /* Seek to the start of the index */
131 fseek (Lib, Header.IndexOffs, SEEK_SET);
133 /* Read the object file count and calculate the cross ref size */
134 Count = ReadVar (Lib);
136 /* Read all entries in the index */
141 /* Read basic object file data from the actual entries */
142 for (I = 0; I < CollCount (&ObjPool); ++I) {
144 /* Get the object file entry */
145 ObjData* O = CollAtUnchecked (&ObjPool, I);
148 ObjReadData (Lib, O);
154 /*****************************************************************************/
155 /* Writing file data structures */
156 /*****************************************************************************/
160 static void WriteHeader (void)
161 /* Write the header to the library file */
163 /* Seek to position zero */
164 fseek (NewLib, 0, SEEK_SET);
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);
175 static void WriteIndexEntry (const ObjData* O)
176 /* Write one index entry */
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);
188 static void WriteIndex (void)
189 /* Write the index of a library file */
193 /* Sync I/O in case the last operation was a read */
194 fseek (NewLib, 0, SEEK_CUR);
196 /* Remember the current offset in the header */
197 Header.IndexOffs = ftell (NewLib);
199 /* Write the object file count */
200 WriteVar (NewLib, CollCount (&ObjPool));
202 /* Write the object files */
203 for (I = 0; I < CollCount (&ObjPool); ++I) {
204 WriteIndexEntry (CollConstAt (&ObjPool, I));
210 /*****************************************************************************/
211 /* High level stuff */
212 /*****************************************************************************/
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
222 /* Remember the name */
223 LibName = xstrdup (Name);
225 /* Open the existing library for reading */
226 Lib = fopen (Name, "rb");
229 /* File does not exist */
231 Error ("Library `%s' does not exist", Name);
233 /* Announce the library's creation if ar65 is verbose. */
235 "%s: Library `%s' will be created.\n", ProgName, Name);
240 /* We have an existing file: Read the header */
243 /* Now read the existing index */
249 /* Create the temporary library */
252 Error ("Cannot create temporary file: %s", strerror (errno));
255 /* Write a dummy header to the temp file */
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.
267 unsigned char Buf [4096];
269 /* Remember the position */
270 unsigned long Pos = ftell (NewLib);
274 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
275 ReadData (F, Buf, Count);
276 WriteData (NewLib, Buf, Count);
280 /* Return the start position */
286 void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
287 /* Copy data from the library file into another file */
289 unsigned char Buf [4096];
291 /* Seek to the correct position */
292 fseek (Lib, Pos, SEEK_SET);
296 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
297 ReadData (Lib, Buf, Count);
298 WriteData (F, Buf, Count);
305 static void LibCheckExports (ObjData* O)
306 /* Insert all exports from the given object file into the global list
307 * checking for duplicates.
312 /* Let the user know what we do */
313 Print (stdout, 2, "Module `%s' (%u exports):\n", O->Name, CollCount (&O->Exports));
315 /* Insert the exports into the global table */
316 for (I = 0; I < CollCount (&O->Exports); ++I) {
318 /* Get the name of the export */
319 const char* Name = CollConstAt (&O->Exports, I);
321 /* Insert the name into the hash table */
322 Print (stdout, 2, " %s\n", Name);
330 /* Write remaining data, close both files and copy the temp file to the old
334 /* Do we have a temporary library? */
338 unsigned char Buf [4096];
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.
345 for (I = 0; I < CollCount (&ObjPool); ++I) {
347 /* Get a pointer to the object */
348 ObjData* O = CollAtUnchecked (&ObjPool, I);
350 /* Check exports, make global export table */
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;
363 /* Write the index */
366 /* Write the updated header */
370 if (Lib && fclose (Lib) != 0) {
371 Error ("Error closing library: %s", strerror (errno));
374 /* Reopen the library and truncate it */
375 Lib = fopen (LibName, "wb");
377 Error ("Cannot open library `%s' for writing: %s",
378 LibName, strerror (errno));
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));
390 /* Close both files */
391 if (Lib && fclose (Lib) != 0) {
392 Error ("Problem closing `%s': %s", LibName, strerror (errno));
394 if (NewLib && fclose (NewLib) != 0) {
395 Error ("Problem closing temporary library file: %s", strerror (errno));