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;
67 static const char* NewLibName = 0;
69 /* File descriptor for the library file */
71 static FILE* NewLib = 0;
73 /* The library header */
74 static LibHeader Header = {
83 /*****************************************************************************/
84 /* Writing file data structures */
85 /*****************************************************************************/
89 static void ReadHeader (void)
90 /* Read the header of a library file */
92 /* Seek to position zero */
93 fseek (Lib, 0, SEEK_SET);
95 /* Read the header fields, checking magic and version */
96 Header.Magic = Read32 (Lib);
97 if (Header.Magic != LIB_MAGIC) {
98 Error ("`%s' is not a valid library file", LibName);
100 Header.Version = Read16 (Lib);
101 if (Header.Version != LIB_VERSION) {
102 Error ("Wrong data version in `%s'", LibName);
104 Header.Flags = Read16 (Lib);
105 Header.IndexOffs = Read32 (Lib);
110 static void ReadIndexEntry (void)
111 /* Read one entry in the index */
113 /* Create a new entry and insert it into the list */
114 ObjData* O = NewObjData ();
116 /* Module name/flags/MTime/Start/Size */
117 O->Name = ReadStr (Lib);
118 O->Flags = Read16 (Lib);
119 O->MTime = Read32 (Lib);
120 O->Start = Read32 (Lib);
121 O->Size = Read32 (Lib);
126 static void ReadIndex (void)
127 /* Read the index of a library file */
131 /* Seek to the start of the index */
132 fseek (Lib, Header.IndexOffs, SEEK_SET);
134 /* Read the object file count and calculate the cross ref size */
135 Count = ReadVar (Lib);
137 /* Read all entries in the index */
142 /* Read basic object file data from the actual entries */
143 for (I = 0; I < CollCount (&ObjPool); ++I) {
145 /* Get the object file entry */
146 ObjData* O = CollAtUnchecked (&ObjPool, I);
149 ObjReadData (Lib, O);
155 /*****************************************************************************/
156 /* Writing file data structures */
157 /*****************************************************************************/
161 static void WriteHeader (void)
162 /* Write the header to the library file */
164 /* Seek to position zero */
165 fseek (NewLib, 0, SEEK_SET);
167 /* Write the header fields */
168 Write32 (NewLib, Header.Magic);
169 Write16 (NewLib, Header.Version);
170 Write16 (NewLib, Header.Flags);
171 Write32 (NewLib, Header.IndexOffs);
176 static void WriteIndexEntry (const ObjData* O)
177 /* Write one index entry */
179 /* Module name/flags/MTime/start/size */
180 WriteStr (NewLib, O->Name);
181 Write16 (NewLib, O->Flags & ~OBJ_HAVEDATA);
182 Write32 (NewLib, O->MTime);
183 Write32 (NewLib, O->Start);
184 Write32 (NewLib, O->Size);
189 static void WriteIndex (void)
190 /* Write the index of a library file */
194 /* Sync I/O in case the last operation was a read */
195 fseek (NewLib, 0, SEEK_CUR);
197 /* Remember the current offset in the header */
198 Header.IndexOffs = ftell (NewLib);
200 /* Write the object file count */
201 WriteVar (NewLib, CollCount (&ObjPool));
203 /* Write the object files */
204 for (I = 0; I < CollCount (&ObjPool); ++I) {
205 WriteIndexEntry (CollConstAt (&ObjPool, I));
211 /*****************************************************************************/
212 /* High level stuff */
213 /*****************************************************************************/
217 void LibOpen (const char* Name, int MustExist, int NeedTemp)
218 /* Open an existing library and a temporary copy. If MustExist is true, the
219 * old library is expected to exist. If NeedTemp is true, a temporary library
223 /* Remember the name */
224 LibName = xstrdup (Name);
226 /* Open the existing library for reading */
227 Lib = fopen (Name, "rb");
230 /* File does not exist */
232 Error ("Library `%s' does not exist", Name);
234 /* Announce the library's creation if ar65 is verbose. */
236 "%s: Library `%s' will be created.\n", ProgName, Name);
241 /* We have an existing file: Read the header */
244 /* Now read the existing index */
251 /* Create the temporary library name */
252 NewLibName = tempnam (NULL, NULL);
253 if (NewLibName == 0) {
254 Error ("Cannot create temporary library file name: %s", strerror (errno));
257 /* Create the temporary library */
258 NewLib = fopen (NewLibName, "w+b");
260 Error ("Cannot create temporary library file: %s", strerror (errno));
263 /* Write a dummy header to the temp file */
270 unsigned long LibCopyTo (FILE* F, unsigned long Bytes)
271 /* Copy data from F to the temp library file, return the start position in
272 * the temporary library file.
275 unsigned char Buf [4096];
277 /* Remember the position */
278 unsigned long Pos = ftell (NewLib);
282 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
283 ReadData (F, Buf, Count);
284 WriteData (NewLib, Buf, Count);
288 /* Return the start position */
294 void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
295 /* Copy data from the library file into another file */
297 unsigned char Buf [4096];
299 /* Seek to the correct position */
300 fseek (Lib, Pos, SEEK_SET);
304 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
305 ReadData (Lib, Buf, Count);
306 WriteData (F, Buf, Count);
313 static void LibCheckExports (ObjData* O)
314 /* Insert all exports from the given object file into the global list
315 * checking for duplicates.
320 /* Let the user know what we do */
321 Print (stdout, 2, "Module `%s' (%u exports):\n", O->Name, CollCount (&O->Exports));
323 /* Insert the exports into the global table */
324 for (I = 0; I < CollCount (&O->Exports); ++I) {
326 /* Get the name of the export */
327 const char* Name = CollConstAt (&O->Exports, I);
329 /* Insert the name into the hash table */
330 Print (stdout, 2, " %s\n", Name);
338 /* Write remaining data, close both files and copy the temp file to the old
342 /* Do we have a temporary library? */
346 unsigned char Buf [4096];
349 /* Walk through the object file list, inserting exports into the
350 * export list checking for duplicates. Copy any data that is still
351 * in the old library into the new one.
353 for (I = 0; I < CollCount (&ObjPool); ++I) {
355 /* Get a pointer to the object */
356 ObjData* O = CollAtUnchecked (&ObjPool, I);
358 /* Check exports, make global export table */
361 /* Copy data if needed */
362 if ((O->Flags & OBJ_HAVEDATA) == 0) {
363 /* Data is still in the old library */
364 fseek (Lib, O->Start, SEEK_SET);
365 O->Start = ftell (NewLib);
366 LibCopyTo (Lib, O->Size);
367 O->Flags |= OBJ_HAVEDATA;
371 /* Write the index */
374 /* Write the updated header */
378 if (Lib && fclose (Lib) != 0) {
379 Error ("Error closing library: %s", strerror (errno));
382 /* Reopen the library and truncate it */
383 Lib = fopen (LibName, "wb");
385 Error ("Cannot open library `%s' for writing: %s",
386 LibName, strerror (errno));
389 /* Copy the new library to the new one */
390 fseek (NewLib, 0, SEEK_SET);
391 while ((Count = fread (Buf, 1, sizeof (Buf), NewLib)) != 0) {
392 if (fwrite (Buf, 1, Count, Lib) != Count) {
393 Error ("Cannot write to `%s': %s", LibName, strerror (errno));
398 /* Close both files */
399 if (Lib && fclose (Lib) != 0) {
400 Error ("Problem closing `%s': %s", LibName, strerror (errno));
402 if (NewLib && fclose (NewLib) != 0) {
403 Error ("Problem closing temporary library file: %s", strerror (errno));
405 if (NewLibName && remove (NewLibName) != 0) {
406 Error ("Problem deleting temporary library file: %s", strerror (errno));