]> git.sur5r.net Git - cc65/blob - src/ar65/objfile.c
Move stuff into the common directory
[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     Ullrich von Bassewitz                                        */
10 /*              Wacholderweg 14                                              */
11 /*              D-70597 Stuttgart                                            */
12 /* EMail:       uz@musoftware.de                                             */
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)
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 #include "../common/xmalloc.h"
49
50 #include "error.h"
51 #include "objdata.h"
52 #include "fileio.h"
53 #include "library.h"
54 #include "objfile.h"
55
56
57
58 /*****************************************************************************/
59 /*                                   Code                                    */
60 /*****************************************************************************/
61
62
63
64 static const char* GetModule (const char* Name)
65 /* Get a module name from the file name */
66 {
67     /* Make a module name from the file name */
68     const char* Module = Name + strlen (Name);
69     while (Module > Name) {
70         --Module;
71         if (*Module == '/' || *Module == '\\') {
72             ++Module;
73             break;
74         }
75     }
76     if (*Module == 0) {
77         Error ("Cannot make module name from `%s'", Name);
78     }
79     return Module;
80 }
81
82
83
84 void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
85 /* Read the header of the object file checking the signature */
86 {
87     H->Magic      = Read32 (Obj);
88     if (H->Magic != OBJ_MAGIC) {
89         Error ("`%s' is not an object file", Name);
90     }
91     H->Version    = Read16 (Obj);
92     if (H->Version != OBJ_VERSION) {
93         Error ("Object file `%s' has wrong version", Name);
94     }
95     H->Flags      = Read16 (Obj);
96     H->OptionOffs = Read32 (Obj);
97     H->OptionSize = Read32 (Obj);
98     H->FileOffs   = Read32 (Obj);
99     H->FileSize   = Read32 (Obj);
100     H->SegOffs    = Read32 (Obj);
101     H->SegSize    = Read32 (Obj);
102     H->ImportOffs = Read32 (Obj);
103     H->ImportSize = Read32 (Obj);
104     H->ExportOffs = Read32 (Obj);
105     H->ExportSize = Read32 (Obj);
106     H->DbgSymOffs = Read32 (Obj);
107     H->DbgSymSize = Read32 (Obj);
108 }
109
110
111
112 void ObjWriteHeader (FILE* Obj, ObjHeader* H)
113 /* Write the header of the object file */
114 {
115     Write32 (Obj, H->Magic);
116     Write16 (Obj, H->Version);
117     Write16 (Obj, H->Flags);
118     Write32 (Obj, H->OptionOffs);
119     Write32 (Obj, H->OptionSize);
120     Write32 (Obj, H->FileOffs);
121     Write32 (Obj, H->FileSize);
122     Write32 (Obj, H->SegOffs);
123     Write32 (Obj, H->SegSize);
124     Write32 (Obj, H->ImportOffs);
125     Write32 (Obj, H->ImportSize);
126     Write32 (Obj, H->ExportOffs);
127     Write32 (Obj, H->ExportSize);
128     Write32 (Obj, H->DbgSymOffs);
129     Write32 (Obj, H->DbgSymSize);
130 }
131
132
133
134 void ObjAdd (const char* Name)
135 /* Add an object file to the library */
136 {
137     struct stat StatBuf;
138     const char* Module;
139     ObjHeader H;
140     ObjData* O;
141
142     /* Open the object file */
143     FILE* Obj = fopen (Name, "rb");
144     if (Obj == 0) {
145         Error ("Could not open `%s': %s", Name, strerror (errno));
146     }
147
148     /* Get the modification time of the object file */
149     if (fstat (fileno (Obj), &StatBuf) != 0) {
150         Error ("Cannot stat object file `%s': %s", Name, strerror (errno));
151     }
152
153     /* Read and check the header */
154     ObjReadHeader (Obj, &H, Name);
155
156     /* Make a module name from the file name */
157     Module = GetModule (Name);
158
159     /* Check if we already have a module with this name */
160     O = FindObjData (Module);
161     if (O == 0) {
162         /* Not found, create a new entry */
163         O = NewObjData ();
164     } else {
165         /* Found - check the file modification times of the internal copy
166          * and the external one.
167          */
168         if (difftime ((time_t)O->MTime, StatBuf.st_mtime) > 0.0) {
169             Warning ("Replacing module `%s' by older version", O->Name);
170         }
171     }
172
173     /* Initialize the object module data structure */
174     O->Name       = xstrdup (Module);
175     O->Flags      = OBJ_HAVEDATA;
176     O->MTime      = StatBuf.st_mtime;
177     O->ImportSize = H.ImportSize;
178     O->Imports    = xmalloc (O->ImportSize);
179     O->ExportSize = H.ExportSize;
180     O->Exports    = xmalloc (O->ExportSize);
181
182     /* Read imports and exports */
183     fseek (Obj, H.ImportOffs, SEEK_SET);
184     ReadData (Obj, O->Imports, O->ImportSize);
185     fseek (Obj, H.ExportOffs, SEEK_SET);
186     ReadData (Obj, O->Exports, O->ExportSize);
187
188     /* Skip the object file header */
189     O->Start = ftell (NewLib);
190     fseek (NewLib, OBJ_HDR_SIZE, SEEK_CUR);
191
192     /* Copy the remaining sections */
193     fseek (Obj, H.DbgSymOffs, SEEK_SET);
194     H.DbgSymOffs = LibCopyTo (Obj, H.DbgSymSize) - O->Start;
195     fseek (Obj, H.OptionOffs, SEEK_SET);
196     H.OptionOffs = LibCopyTo (Obj, H.OptionSize) - O->Start;
197     fseek (Obj, H.SegOffs, SEEK_SET);
198     H.SegOffs = LibCopyTo (Obj, H.SegSize) - O->Start;
199     fseek (Obj, H.FileOffs, SEEK_SET);
200     H.FileOffs = LibCopyTo (Obj, H.FileSize) - O->Start;
201
202     /* Calculate the amount of data written */
203     O->Size = ftell (NewLib) - O->Start;
204
205     /* Clear the remaining header fields */
206     H.ImportOffs = H.ImportSize = 0;
207     H.ExportOffs = H.ExportSize = 0;
208
209     /* Seek back and write the updated header */
210     fseek (NewLib, O->Start, SEEK_SET);
211     ObjWriteHeader (NewLib, &H);
212
213     /* Now seek again to end of file */
214     fseek (NewLib, 0, SEEK_END);
215
216     /* Done, close the file (we read it only, so no error check) */
217     fclose (Obj);
218 }
219
220
221
222 void ObjExtract (const char* Name)
223 /* Extract a module from the library */
224 {
225     unsigned long ImportStart;
226     unsigned long ExportStart;
227     struct utimbuf U;
228     ObjHeader H;
229     FILE* Obj;
230
231
232     /* Make a module name from the file name */
233     const char* Module = GetModule (Name);
234
235     /* Try to find the module in the library */
236     ObjData* O = FindObjData (Module);
237
238     /* Bail out if the module does not exist */
239     if (O == 0) {
240         Error ("Module `%s' not found in library", Module);
241     }
242
243     /* Open the output file */
244     Obj = fopen (Name, "w+b");
245     if (Obj == 0) {
246         Error ("Cannot open target file `%s': %s", Name, strerror (errno));
247     }
248
249     /* Copy the first four segments including the header to the new file */
250     LibCopyFrom (O->Start, O->Size, Obj);
251
252     /* Write imports and exports */
253     ImportStart = ftell (Obj);
254     WriteData (Obj, O->Imports, O->ImportSize);
255     ExportStart = ftell (Obj);
256     WriteData (Obj, O->Exports, O->ExportSize);
257
258     /* Seek back and read the header */
259     fseek (Obj, 0, SEEK_SET);
260     ObjReadHeader (Obj, &H, Name);
261
262     /* Update the header fields */
263     H.ImportOffs = ImportStart;
264     H.ImportSize = O->ImportSize;
265     H.ExportOffs = ExportStart;
266     H.ExportSize = O->ExportSize;
267
268     /* Write the changed header */
269     fseek (Obj, 0, SEEK_SET);
270     ObjWriteHeader (Obj, &H);
271
272     /* Close the file */
273     if (fclose (Obj) != 0) {
274         Error ("Problem closing object file `%s': %s", Name, strerror (errno));
275     }
276
277     /* Set access and modification time */
278     U.actime = O->MTime;
279     U.modtime = O->MTime;
280     if (utime (Name, &U) != 0) {
281         Error ("Cannot set mod time on `%s': %s", Name, strerror (errno));
282     }
283 }
284
285
286
287
288
289