1 /*****************************************************************************/
5 /* Object file handling for the ar65 archiver */
9 /* (C) 1998-2003 Ullrich von Bassewitz */
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 /*****************************************************************************/
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);
110 H->LineInfoOffs = Read32 (Obj);
111 H->LineInfoSize = Read32 (Obj);
112 H->StrPoolOffs = Read32 (Obj);
113 H->StrPoolSize = Read32 (Obj);
114 H->AssertOffs = Read32 (Obj);
115 H->AssertSize = Read32 (Obj);
116 H->ScopeOffs = Read32 (Obj);
117 H->ScopeSize = Read32 (Obj);
122 void ObjWriteHeader (FILE* Obj, ObjHeader* H)
123 /* Write the header of the object file */
125 Write32 (Obj, H->Magic);
126 Write16 (Obj, H->Version);
127 Write16 (Obj, H->Flags);
128 Write32 (Obj, H->OptionOffs);
129 Write32 (Obj, H->OptionSize);
130 Write32 (Obj, H->FileOffs);
131 Write32 (Obj, H->FileSize);
132 Write32 (Obj, H->SegOffs);
133 Write32 (Obj, H->SegSize);
134 Write32 (Obj, H->ImportOffs);
135 Write32 (Obj, H->ImportSize);
136 Write32 (Obj, H->ExportOffs);
137 Write32 (Obj, H->ExportSize);
138 Write32 (Obj, H->DbgSymOffs);
139 Write32 (Obj, H->DbgSymSize);
140 Write32 (Obj, H->LineInfoOffs);
141 Write32 (Obj, H->LineInfoSize);
142 Write32 (Obj, H->StrPoolOffs);
143 Write32 (Obj, H->StrPoolSize);
144 Write32 (Obj, H->AssertOffs);
145 Write32 (Obj, H->AssertSize);
146 Write32 (Obj, H->ScopeOffs);
147 Write32 (Obj, H->ScopeSize);
152 void ObjAdd (const char* Name)
153 /* Add an object file to the library */
161 /* Open the object file */
162 FILE* Obj = fopen (Name, "rb");
164 Error ("Could not open `%s': %s", Name, strerror (errno));
167 /* Get the modification time of the object file */
168 if (fstat (fileno (Obj), &StatBuf) != 0) {
169 Error ("Cannot stat object file `%s': %s", Name, strerror (errno));
172 /* Read and check the header */
173 ObjReadHeader (Obj, &H, Name);
175 /* Make a module name from the file name */
176 Module = GetModule (Name);
178 /* Check if we already have a module with this name */
179 O = FindObjData (Module);
181 /* Not found, create a new entry */
184 /* Found - check the file modification times of the internal copy
185 * and the external one.
187 if (difftime ((time_t)O->MTime, StatBuf.st_mtime) > 0.0) {
188 Warning ("Replacing module `%s' by older version", O->Name);
192 /* Initialize the object module data structure */
193 O->Name = xstrdup (Module);
194 O->Flags = OBJ_HAVEDATA;
195 O->MTime = StatBuf.st_mtime;
196 O->ImportSize = H.ImportSize;
197 O->Imports = xmalloc (O->ImportSize);
198 O->ExportSize = H.ExportSize;
199 O->Exports = xmalloc (O->ExportSize);
201 /* Read imports and exports */
202 fseek (Obj, H.ImportOffs, SEEK_SET);
203 ReadData (Obj, O->Imports, O->ImportSize);
204 fseek (Obj, H.ExportOffs, SEEK_SET);
205 ReadData (Obj, O->Exports, O->ExportSize);
207 /* Read the string pool */
208 fseek (Obj, H.StrPoolOffs, SEEK_SET);
209 O->StringCount = ReadVar (Obj);
210 O->Strings = xmalloc (O->StringCount * sizeof (char*));
211 for (I = 0; I < O->StringCount; ++I) {
212 O->Strings[I] = ReadStr (Obj);
215 /* Skip the object file header */
216 O->Start = ftell (NewLib);
217 fseek (NewLib, OBJ_HDR_SIZE, SEEK_CUR);
219 /* Copy the remaining sections */
220 fseek (Obj, H.DbgSymOffs, SEEK_SET);
221 H.DbgSymOffs = LibCopyTo (Obj, H.DbgSymSize) - O->Start;
222 fseek (Obj, H.OptionOffs, SEEK_SET);
223 H.OptionOffs = LibCopyTo (Obj, H.OptionSize) - O->Start;
224 fseek (Obj, H.SegOffs, SEEK_SET);
225 H.SegOffs = LibCopyTo (Obj, H.SegSize) - O->Start;
226 fseek (Obj, H.FileOffs, SEEK_SET);
227 H.FileOffs = LibCopyTo (Obj, H.FileSize) - O->Start;
228 fseek (Obj, H.LineInfoOffs, SEEK_SET);
229 H.LineInfoOffs = LibCopyTo (Obj, H.LineInfoSize) - O->Start;
230 fseek (Obj, H.AssertOffs, SEEK_SET);
231 H.AssertOffs = LibCopyTo (Obj, H.AssertSize) - O->Start;
232 fseek (Obj, H.ScopeOffs, SEEK_SET);
233 H.ScopeOffs = LibCopyTo (Obj, H.ScopeSize) - O->Start;
235 /* Calculate the amount of data written */
236 O->Size = ftell (NewLib) - O->Start;
238 /* Clear the remaining header fields */
239 H.ImportOffs = H.ImportSize = 0;
240 H.ExportOffs = H.ExportSize = 0;
241 H.StrPoolOffs = H.StrPoolSize = 0;
243 /* Seek back and write the updated header */
244 fseek (NewLib, O->Start, SEEK_SET);
245 ObjWriteHeader (NewLib, &H);
247 /* Now seek again to end of file */
248 fseek (NewLib, 0, SEEK_END);
250 /* Done, close the file (we read it only, so no error check) */
256 void ObjExtract (const char* Name)
257 /* Extract a module from the library */
259 unsigned long ImportStart;
260 unsigned long ExportStart;
261 unsigned long StrPoolStart;
262 unsigned long StrPoolSize;
268 /* Make a module name from the file name */
269 const char* Module = GetModule (Name);
271 /* Try to find the module in the library */
272 ObjData* O = FindObjData (Module);
274 /* Bail out if the module does not exist */
276 Error ("Module `%s' not found in library", Module);
279 /* Open the output file */
280 Obj = fopen (Name, "w+b");
282 Error ("Cannot open target file `%s': %s", Name, strerror (errno));
285 /* Copy anything to the new file that has no special handling */
286 LibCopyFrom (O->Start, O->Size, Obj);
288 /* Write imports and exports */
289 ImportStart = ftell (Obj);
290 WriteData (Obj, O->Imports, O->ImportSize);
291 ExportStart = ftell (Obj);
292 WriteData (Obj, O->Exports, O->ExportSize);
294 /* Write the string pool */
295 StrPoolStart = ftell (Obj);
296 WriteVar (Obj, O->StringCount);
297 for (I = 0; I < O->StringCount; ++I) {
298 WriteStr (Obj, O->Strings[I]);
300 StrPoolSize = ftell (Obj) - StrPoolStart;
302 /* Seek back and read the header */
303 fseek (Obj, 0, SEEK_SET);
304 ObjReadHeader (Obj, &H, Name);
306 /* Update the header fields */
307 H.ImportOffs = ImportStart;
308 H.ImportSize = O->ImportSize;
309 H.ExportOffs = ExportStart;
310 H.ExportSize = O->ExportSize;
311 H.StrPoolOffs = StrPoolStart;
312 H.StrPoolSize = StrPoolSize;
314 /* Write the changed header */
315 fseek (Obj, 0, SEEK_SET);
316 ObjWriteHeader (Obj, &H);
319 if (fclose (Obj) != 0) {
320 Error ("Problem closing object file `%s': %s", Name, strerror (errno));
323 /* Set access and modification time */
325 U.modtime = O->MTime;
326 if (utime (Name, &U) != 0) {
327 Error ("Cannot set mod time on `%s': %s", Name, strerror (errno));