1 /*****************************************************************************/
5 /* Library data structures and helpers for the ar65 archiver */
9 /* (C) 1998-2000 Ullrich von Bassewitz */
11 /* D-70597 Stuttgart */
12 /* EMail: uz@musoftware.de */
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 = {
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);
120 O->ExportSize = Read16 (Lib);
121 O->Exports = xmalloc (O->ExportSize);
122 ReadData (Lib, O->Exports, O->ExportSize);
125 O->ImportSize = Read16 (Lib);
126 O->Imports = xmalloc (O->ImportSize);
127 ReadData (Lib, O->Imports, O->ImportSize);
132 static void ReadIndex (void)
133 /* Read the index of a library file */
137 /* Seek to the start of the index */
138 fseek (Lib, Header.IndexOffs, SEEK_SET);
140 /* Read the object file count and calculate the cross ref size */
141 Count = Read16 (Lib);
143 /* Read all entries in the index */
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 (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);
183 Write16 (NewLib, O->ExportSize);
184 WriteData (NewLib, O->Exports, O->ExportSize);
187 Write16 (NewLib, O->ImportSize);
188 WriteData (NewLib, O->Imports, O->ImportSize);
193 static void WriteIndex (void)
194 /* Write the index of a library file */
198 /* Sync I/O in case the last operation was a read */
199 fseek (NewLib, 0, SEEK_CUR);
201 /* Remember the current offset in the header */
202 Header.IndexOffs = ftell (NewLib);
204 /* Write the object file count */
205 Write16 (NewLib, ObjCount);
207 /* Write the object files */
217 /*****************************************************************************/
218 /* High level stuff */
219 /*****************************************************************************/
223 void LibOpen (const char* Name, int MustExist, int NeedTemp)
224 /* Open an existing library and a temporary copy. If MustExist is true, the
225 * old library is expected to exist. If NeedTemp is true, a temporary library
229 /* Remember the name */
230 LibName = xstrdup (Name);
232 /* Open the existing library for reading */
233 Lib = fopen (Name, "rb");
236 /* File does not exist */
238 Error ("Library `%s' does not exist", Name);
240 Warning ("Library `%s' not found - will be created", Name);
245 /* We have an existing file: Read the header */
248 /* Now read the existing index */
254 /* Create the temporary library */
257 Error ("Cannot create temporary file: %s", strerror (errno));
260 /* Write a dummy header to the temp file */
267 unsigned long LibCopyTo (FILE* F, unsigned long Bytes)
268 /* Copy data from F to the temp library file, return the start position in
269 * the temporary library file.
272 unsigned char Buf [4096];
274 /* Remember the position */
275 unsigned long Pos = ftell (NewLib);
279 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
280 ReadData (F, Buf, Count);
281 WriteData (NewLib, Buf, Count);
285 /* Return the start position */
291 void LibCopyFrom (unsigned long Pos, unsigned long Bytes, FILE* F)
292 /* Copy data from the library file into another file */
294 unsigned char Buf [4096];
296 /* Seek to the correct position */
297 fseek (Lib, Pos, SEEK_SET);
301 unsigned Count = (Bytes > sizeof (Buf))? sizeof (Buf) : Bytes;
302 ReadData (Lib, Buf, Count);
303 WriteData (F, Buf, Count);
310 static unsigned long GetVar (unsigned char** Buf)
311 /* Get a variable sized value from Buf */
320 /* Add this char to the value */
321 V |= ((unsigned long)(C & 0x7F)) << Shift;
326 /* Return the result */
332 static void SkipExpr (unsigned char** Buf)
333 /* Skip an expression in Buf */
335 /* Get the operation and skip it */
336 unsigned char Op = **Buf;
339 /* Filter leaf nodes */
346 /* 32 bit literal value */
351 /* 16 bit symbol index */
356 /* 8 bit segment number */
361 /* What's left are unary and binary nodes */
362 SkipExpr (Buf); /* Skip left */
363 SkipExpr (Buf); /* Skip right */
368 static void SkipFilePos (unsigned char** Buf)
369 /* Skip a file position in Buf */
371 (void) GetVar (Buf); /* Line */
372 (void) GetVar (Buf); /* Col */
373 (void) GetVar (Buf); /* Name */
378 static void LibCheckExports (ObjData* O)
379 /* Insert all exports from the given object file into the global list
380 * checking for duplicates.
383 /* Get a pointer to the buffer */
384 unsigned char* Exports = O->Exports;
386 /* Get the export count */
387 unsigned Count = GetVar (&Exports);
389 /* Read the exports */
390 Print (stdout, 1, "Module `%s' (%u exports):\n", O->Name, Count);
397 /* Get the export tag */
400 /* condes decls may follow */
401 Exports += GET_EXP_CONDES_COUNT (Tag);
403 /* Next thing is name of symbol */
404 Len = GetVar (&Exports);
405 Name = xmalloc (Len + 1);
406 memcpy (Name, Exports, Len);
410 /* Skip value of symbol */
411 if (Tag & EXP_EXPR) {
412 /* Expression tree */
415 /* Constant 32 bit value */
419 /* Skip the position */
420 SkipFilePos (&Exports);
422 /* Insert the name into the hash table */
423 Print (stdout, 1, " %s\n", Name);
424 ExpInsert (Name, O->Index);
434 /* Write remaining data, close both files and copy the temp file to the old
438 /* Do we have a temporary library? */
442 unsigned char Buf [4096];
445 /* Index the object files and make an array containing the objects */
448 /* Walk through the object file list, inserting exports into the
449 * export list checking for duplicates. Copy any data that is still
450 * in the old library into the new one.
452 for (I = 0; I < ObjCount; ++I) {
454 /* Get a pointer to the object */
455 ObjData* O = ObjPool [I];
457 /* Check exports, make global export table */
460 /* Copy data if needed */
461 if ((O->Flags & OBJ_HAVEDATA) == 0) {
462 /* Data is still in the old library */
463 fseek (Lib, O->Start, SEEK_SET);
464 O->Start = ftell (NewLib);
465 LibCopyTo (Lib, O->Size);
466 O->Flags |= OBJ_HAVEDATA;
470 /* Write the index */
473 /* Write the updated header */
477 if (Lib && fclose (Lib) != 0) {
478 Error ("Error closing library: %s", strerror (errno));
481 /* Reopen the library and truncate it */
482 Lib = fopen (LibName, "wb");
484 Error ("Cannot open library `%s' for writing: %s",
485 LibName, strerror (errno));
488 /* Copy the new library to the new one */
489 fseek (NewLib, 0, SEEK_SET);
490 while ((Count = fread (Buf, 1, sizeof (Buf), NewLib)) != 0) {
491 if (fwrite (Buf, 1, Count, Lib) != Count) {
492 Error ("Cannot write to `%s': %s", LibName, strerror (errno));
497 /* Close both files */
498 if (Lib && fclose (Lib) != 0) {
499 Error ("Problem closing `%s': %s", LibName, strerror (errno));
501 if (NewLib && fclose (NewLib) != 0) {
502 Error ("Problem closing temporary library file: %s", strerror (errno));