]> git.sur5r.net Git - cc65/blob - src/ar65/objfile.c
e165fc1671064236aea5e3347770ac51a80ddbb5
[cc65] / src / ar65 / objfile.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 objfile.c                                 */
4 /*                                                                           */
5 /*                Object file handling for the ar65 archiver                 */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
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.                                    */
18 /*                                                                           */
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:                            */
22 /*                                                                           */
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              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <string.h>
37 #include <errno.h>
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>
41 #else
42 #  include <sys/types.h>                /* FreeBSD needs this */
43 #  include <utime.h>
44 #endif
45 #include <time.h>
46 #include <sys/stat.h>
47
48 /* common */
49 #include "xmalloc.h"
50
51 /* ar65 */
52 #include "error.h"
53 #include "objdata.h"
54 #include "fileio.h"
55 #include "library.h"
56 #include "objfile.h"
57
58
59
60 /*****************************************************************************/
61 /*                                   Code                                    */
62 /*****************************************************************************/
63
64
65
66 static const char* GetModule (const char* Name)
67 /* Get a module name from the file name */
68 {
69     /* Make a module name from the file name */
70     const char* Module = Name + strlen (Name);
71     while (Module > Name) {
72         --Module;
73         if (*Module == '/' || *Module == '\\') {
74             ++Module;
75             break;
76         }
77     }
78     if (*Module == 0) {
79         Error ("Cannot make module name from `%s'", Name);
80     }
81     return Module;
82 }
83
84
85
86 void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
87 /* Read the header of the object file checking the signature */
88 {
89     H->Magic      = Read32 (Obj);
90     if (H->Magic != OBJ_MAGIC) {
91         Error ("`%s' is not an object file", Name);
92     }
93     H->Version    = Read16 (Obj);
94     if (H->Version != OBJ_VERSION) {
95         Error ("Object file `%s' has wrong version", Name);
96     }
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 }
117
118
119
120 void ObjWriteHeader (FILE* Obj, ObjHeader* H)
121 /* Write the header of the object file */
122 {
123     Write32 (Obj, H->Magic);
124     Write16 (Obj, H->Version);
125     Write16 (Obj, H->Flags);
126     Write32 (Obj, H->OptionOffs);
127     Write32 (Obj, H->OptionSize);
128     Write32 (Obj, H->FileOffs);
129     Write32 (Obj, H->FileSize);
130     Write32 (Obj, H->SegOffs);
131     Write32 (Obj, H->SegSize);
132     Write32 (Obj, H->ImportOffs);
133     Write32 (Obj, H->ImportSize);
134     Write32 (Obj, H->ExportOffs);
135     Write32 (Obj, H->ExportSize);
136     Write32 (Obj, H->DbgSymOffs);
137     Write32 (Obj, H->DbgSymSize);
138     Write32 (Obj, H->LineInfoOffs);
139     Write32 (Obj, H->LineInfoSize);
140     Write32 (Obj, H->StrPoolOffs);
141     Write32 (Obj, H->StrPoolSize);
142     Write32 (Obj, H->AssertOffs);
143     Write32 (Obj, H->AssertSize);
144 }
145
146
147
148 void ObjAdd (const char* Name)
149 /* Add an object file to the library */
150 {
151     struct stat StatBuf;
152     const char* Module;
153     ObjHeader H;
154     ObjData* O;
155     unsigned I;
156
157     /* Open the object file */
158     FILE* Obj = fopen (Name, "rb");
159     if (Obj == 0) {
160         Error ("Could not open `%s': %s", Name, strerror (errno));
161     }
162
163     /* Get the modification time of the object file */
164     if (fstat (fileno (Obj), &StatBuf) != 0) {
165         Error ("Cannot stat object file `%s': %s", Name, strerror (errno));
166     }
167
168     /* Read and check the header */
169     ObjReadHeader (Obj, &H, Name);
170
171     /* Make a module name from the file name */
172     Module = GetModule (Name);
173
174     /* Check if we already have a module with this name */
175     O = FindObjData (Module);
176     if (O == 0) {
177         /* Not found, create a new entry */
178         O = NewObjData ();
179     } else {
180         /* Found - check the file modification times of the internal copy
181          * and the external one.
182          */
183         if (difftime ((time_t)O->MTime, StatBuf.st_mtime) > 0.0) {
184             Warning ("Replacing module `%s' by older version", O->Name);
185         }
186     }
187
188     /* Initialize the object module data structure */
189     O->Name       = xstrdup (Module);
190     O->Flags      = OBJ_HAVEDATA;
191     O->MTime      = StatBuf.st_mtime;
192     O->ImportSize = H.ImportSize;
193     O->Imports    = xmalloc (O->ImportSize);
194     O->ExportSize = H.ExportSize;
195     O->Exports    = xmalloc (O->ExportSize);
196
197     /* Read imports and exports */
198     fseek (Obj, H.ImportOffs, SEEK_SET);
199     ReadData (Obj, O->Imports, O->ImportSize);
200     fseek (Obj, H.ExportOffs, SEEK_SET);
201     ReadData (Obj, O->Exports, O->ExportSize);
202
203     /* Read the string pool */
204     fseek (Obj, H.StrPoolOffs, SEEK_SET);
205     O->StringCount = ReadVar (Obj);
206     O->Strings     = xmalloc (O->StringCount * sizeof (char*));
207     for (I = 0; I < O->StringCount; ++I) {
208         O->Strings[I] = ReadStr (Obj);
209     }
210
211     /* Skip the object file header */
212     O->Start = ftell (NewLib);
213     fseek (NewLib, OBJ_HDR_SIZE, SEEK_CUR);
214
215     /* Copy the remaining sections */
216     fseek (Obj, H.DbgSymOffs, SEEK_SET);
217     H.DbgSymOffs = LibCopyTo (Obj, H.DbgSymSize) - O->Start;
218     fseek (Obj, H.OptionOffs, SEEK_SET);
219     H.OptionOffs = LibCopyTo (Obj, H.OptionSize) - O->Start;
220     fseek (Obj, H.SegOffs, SEEK_SET);
221     H.SegOffs = LibCopyTo (Obj, H.SegSize) - O->Start;
222     fseek (Obj, H.FileOffs, SEEK_SET);
223     H.FileOffs = LibCopyTo (Obj, H.FileSize) - O->Start;
224     fseek (Obj, H.LineInfoOffs, SEEK_SET);
225     H.LineInfoOffs = LibCopyTo (Obj, H.LineInfoSize) - O->Start;
226     fseek (Obj, H.AssertOffs, SEEK_SET);
227     H.AssertOffs = LibCopyTo (Obj, H.AssertSize) - O->Start;
228
229     /* Calculate the amount of data written */
230     O->Size = ftell (NewLib) - O->Start;
231
232     /* Clear the remaining header fields */
233     H.ImportOffs  = H.ImportSize  = 0;
234     H.ExportOffs  = H.ExportSize  = 0;
235     H.StrPoolOffs = H.StrPoolSize = 0;
236
237     /* Seek back and write the updated header */
238     fseek (NewLib, O->Start, SEEK_SET);
239     ObjWriteHeader (NewLib, &H);
240
241     /* Now seek again to end of file */
242     fseek (NewLib, 0, SEEK_END);
243
244     /* Done, close the file (we read it only, so no error check) */
245     fclose (Obj);
246 }
247
248
249
250 void ObjExtract (const char* Name)
251 /* Extract a module from the library */
252 {
253     unsigned long ImportStart;
254     unsigned long ExportStart;
255     unsigned long StrPoolStart;
256     unsigned long StrPoolSize;
257     struct utimbuf U;
258     ObjHeader H;
259     FILE* Obj;
260     unsigned I;
261
262     /* Make a module name from the file name */
263     const char* Module = GetModule (Name);
264
265     /* Try to find the module in the library */
266     ObjData* O = FindObjData (Module);
267
268     /* Bail out if the module does not exist */
269     if (O == 0) {
270         Error ("Module `%s' not found in library", Module);
271     }
272
273     /* Open the output file */
274     Obj = fopen (Name, "w+b");
275     if (Obj == 0) {
276         Error ("Cannot open target file `%s': %s", Name, strerror (errno));
277     }
278
279     /* Copy anything to the new file that has no special handling */
280     LibCopyFrom (O->Start, O->Size, Obj);
281
282     /* Write imports and exports */
283     ImportStart = ftell (Obj);
284     WriteData (Obj, O->Imports, O->ImportSize);
285     ExportStart = ftell (Obj);
286     WriteData (Obj, O->Exports, O->ExportSize);
287
288     /* Write the string pool */
289     StrPoolStart = ftell (Obj);
290     WriteVar (Obj, O->StringCount);
291     for (I = 0; I < O->StringCount; ++I) {
292         WriteStr (Obj, O->Strings[I]);
293     }
294     StrPoolSize = ftell (Obj) - StrPoolStart;
295
296     /* Seek back and read the header */
297     fseek (Obj, 0, SEEK_SET);
298     ObjReadHeader (Obj, &H, Name);
299
300     /* Update the header fields */
301     H.ImportOffs  = ImportStart;
302     H.ImportSize  = O->ImportSize;
303     H.ExportOffs  = ExportStart;
304     H.ExportSize  = O->ExportSize;
305     H.StrPoolOffs = StrPoolStart;
306     H.StrPoolSize = StrPoolSize;
307
308     /* Write the changed header */
309     fseek (Obj, 0, SEEK_SET);
310     ObjWriteHeader (Obj, &H);
311
312     /* Close the file */
313     if (fclose (Obj) != 0) {
314         Error ("Problem closing object file `%s': %s", Name, strerror (errno));
315     }
316
317     /* Set access and modification time */
318     U.actime = O->MTime;
319     U.modtime = O->MTime;
320     if (utime (Name, &U) != 0) {
321         Error ("Cannot set mod time on `%s': %s", Name, strerror (errno));
322     }
323 }
324
325
326