1 /*****************************************************************************/
5 /* Object file handling for the ar65 archiver */
9 /* (C) 1998 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 /*****************************************************************************/
38 #if defined(__WATCOMC__) || defined(_MSC_VER) || defined(__MINGW32__)
39 /* The Windows compilers have the file in the wrong directory */
40 # include <sys/utime.h>
42 # include <sys/types.h> /* FreeBSD needs this */
60 /*****************************************************************************/
62 /*****************************************************************************/
66 static const char* GetModule (const char* Name)
67 /* Get a module name from the file name */
69 /* Make a module name from the file name */
70 const char* Module = Name + strlen (Name);
71 while (Module > Name) {
73 if (*Module == '/' || *Module == '\\') {
79 Error ("Cannot make module name from `%s'", Name);
86 void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
87 /* Read the header of the object file checking the signature */
89 H->Magic = Read32 (Obj);
90 if (H->Magic != OBJ_MAGIC) {
91 Error ("`%s' is not an object file", Name);
93 H->Version = Read16 (Obj);
94 if (H->Version != OBJ_VERSION) {
95 Error ("Object file `%s' has wrong version", Name);
97 H->Flags = Read16 (Obj);
98 H->OptionOffs = Read32 (Obj);
99 H->OptionSize = Read32 (Obj);
100 H->FileOffs = Read32 (Obj);
101 H->FileSize = Read32 (Obj);
102 H->SegOffs = Read32 (Obj);
103 H->SegSize = Read32 (Obj);
104 H->ImportOffs = Read32 (Obj);
105 H->ImportSize = Read32 (Obj);
106 H->ExportOffs = Read32 (Obj);
107 H->ExportSize = Read32 (Obj);
108 H->DbgSymOffs = Read32 (Obj);
109 H->DbgSymSize = Read32 (Obj);
114 void ObjWriteHeader (FILE* Obj, ObjHeader* H)
115 /* Write the header of the object file */
117 Write32 (Obj, H->Magic);
118 Write16 (Obj, H->Version);
119 Write16 (Obj, H->Flags);
120 Write32 (Obj, H->OptionOffs);
121 Write32 (Obj, H->OptionSize);
122 Write32 (Obj, H->FileOffs);
123 Write32 (Obj, H->FileSize);
124 Write32 (Obj, H->SegOffs);
125 Write32 (Obj, H->SegSize);
126 Write32 (Obj, H->ImportOffs);
127 Write32 (Obj, H->ImportSize);
128 Write32 (Obj, H->ExportOffs);
129 Write32 (Obj, H->ExportSize);
130 Write32 (Obj, H->DbgSymOffs);
131 Write32 (Obj, H->DbgSymSize);
136 void ObjAdd (const char* Name)
137 /* Add an object file to the library */
144 /* Open the object file */
145 FILE* Obj = fopen (Name, "rb");
147 Error ("Could not open `%s': %s", Name, strerror (errno));
150 /* Get the modification time of the object file */
151 if (fstat (fileno (Obj), &StatBuf) != 0) {
152 Error ("Cannot stat object file `%s': %s", Name, strerror (errno));
155 /* Read and check the header */
156 ObjReadHeader (Obj, &H, Name);
158 /* Make a module name from the file name */
159 Module = GetModule (Name);
161 /* Check if we already have a module with this name */
162 O = FindObjData (Module);
164 /* Not found, create a new entry */
167 /* Found - check the file modification times of the internal copy
168 * and the external one.
170 if (difftime ((time_t)O->MTime, StatBuf.st_mtime) > 0.0) {
171 Warning ("Replacing module `%s' by older version", O->Name);
175 /* Initialize the object module data structure */
176 O->Name = xstrdup (Module);
177 O->Flags = OBJ_HAVEDATA;
178 O->MTime = StatBuf.st_mtime;
179 O->ImportSize = H.ImportSize;
180 O->Imports = xmalloc (O->ImportSize);
181 O->ExportSize = H.ExportSize;
182 O->Exports = xmalloc (O->ExportSize);
184 /* Read imports and exports */
185 fseek (Obj, H.ImportOffs, SEEK_SET);
186 ReadData (Obj, O->Imports, O->ImportSize);
187 fseek (Obj, H.ExportOffs, SEEK_SET);
188 ReadData (Obj, O->Exports, O->ExportSize);
190 /* Skip the object file header */
191 O->Start = ftell (NewLib);
192 fseek (NewLib, OBJ_HDR_SIZE, SEEK_CUR);
194 /* Copy the remaining sections */
195 fseek (Obj, H.DbgSymOffs, SEEK_SET);
196 H.DbgSymOffs = LibCopyTo (Obj, H.DbgSymSize) - O->Start;
197 fseek (Obj, H.OptionOffs, SEEK_SET);
198 H.OptionOffs = LibCopyTo (Obj, H.OptionSize) - O->Start;
199 fseek (Obj, H.SegOffs, SEEK_SET);
200 H.SegOffs = LibCopyTo (Obj, H.SegSize) - O->Start;
201 fseek (Obj, H.FileOffs, SEEK_SET);
202 H.FileOffs = LibCopyTo (Obj, H.FileSize) - O->Start;
204 /* Calculate the amount of data written */
205 O->Size = ftell (NewLib) - O->Start;
207 /* Clear the remaining header fields */
208 H.ImportOffs = H.ImportSize = 0;
209 H.ExportOffs = H.ExportSize = 0;
211 /* Seek back and write the updated header */
212 fseek (NewLib, O->Start, SEEK_SET);
213 ObjWriteHeader (NewLib, &H);
215 /* Now seek again to end of file */
216 fseek (NewLib, 0, SEEK_END);
218 /* Done, close the file (we read it only, so no error check) */
224 void ObjExtract (const char* Name)
225 /* Extract a module from the library */
227 unsigned long ImportStart;
228 unsigned long ExportStart;
234 /* Make a module name from the file name */
235 const char* Module = GetModule (Name);
237 /* Try to find the module in the library */
238 ObjData* O = FindObjData (Module);
240 /* Bail out if the module does not exist */
242 Error ("Module `%s' not found in library", Module);
245 /* Open the output file */
246 Obj = fopen (Name, "w+b");
248 Error ("Cannot open target file `%s': %s", Name, strerror (errno));
251 /* Copy the first four segments including the header to the new file */
252 LibCopyFrom (O->Start, O->Size, Obj);
254 /* Write imports and exports */
255 ImportStart = ftell (Obj);
256 WriteData (Obj, O->Imports, O->ImportSize);
257 ExportStart = ftell (Obj);
258 WriteData (Obj, O->Exports, O->ExportSize);
260 /* Seek back and read the header */
261 fseek (Obj, 0, SEEK_SET);
262 ObjReadHeader (Obj, &H, Name);
264 /* Update the header fields */
265 H.ImportOffs = ImportStart;
266 H.ImportSize = O->ImportSize;
267 H.ExportOffs = ExportStart;
268 H.ExportSize = O->ExportSize;
270 /* Write the changed header */
271 fseek (Obj, 0, SEEK_SET);
272 ObjWriteHeader (Obj, &H);
275 if (fclose (Obj) != 0) {
276 Error ("Problem closing object file `%s': %s", Name, strerror (errno));
279 /* Set access and modification time */
281 U.modtime = O->MTime;
282 if (utime (Name, &U) != 0) {
283 Error ("Cannot set mod time on `%s': %s", Name, strerror (errno));