]> git.sur5r.net Git - cc65/blob - src/ar65/objfile.c
Allow to set character translations at compile time
[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-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
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 }
113
114
115
116 void ObjWriteHeader (FILE* Obj, ObjHeader* H)
117 /* Write the header of the object file */
118 {
119     Write32 (Obj, H->Magic);
120     Write16 (Obj, H->Version);
121     Write16 (Obj, H->Flags);
122     Write32 (Obj, H->OptionOffs);
123     Write32 (Obj, H->OptionSize);
124     Write32 (Obj, H->FileOffs);
125     Write32 (Obj, H->FileSize);
126     Write32 (Obj, H->SegOffs);
127     Write32 (Obj, H->SegSize);
128     Write32 (Obj, H->ImportOffs);
129     Write32 (Obj, H->ImportSize);
130     Write32 (Obj, H->ExportOffs);
131     Write32 (Obj, H->ExportSize);
132     Write32 (Obj, H->DbgSymOffs);
133     Write32 (Obj, H->DbgSymSize);
134     Write32 (Obj, H->LineInfoOffs);
135     Write32 (Obj, H->LineInfoSize);
136 }
137
138
139
140 void ObjAdd (const char* Name)
141 /* Add an object file to the library */
142 {
143     struct stat StatBuf;
144     const char* Module;
145     ObjHeader H;
146     ObjData* O;
147
148     /* Open the object file */
149     FILE* Obj = fopen (Name, "rb");
150     if (Obj == 0) {
151         Error ("Could not open `%s': %s", Name, strerror (errno));
152     }
153
154     /* Get the modification time of the object file */
155     if (fstat (fileno (Obj), &StatBuf) != 0) {
156         Error ("Cannot stat object file `%s': %s", Name, strerror (errno));
157     }
158
159     /* Read and check the header */
160     ObjReadHeader (Obj, &H, Name);
161
162     /* Make a module name from the file name */
163     Module = GetModule (Name);
164
165     /* Check if we already have a module with this name */
166     O = FindObjData (Module);
167     if (O == 0) {
168         /* Not found, create a new entry */
169         O = NewObjData ();
170     } else {
171         /* Found - check the file modification times of the internal copy
172          * and the external one.
173          */
174         if (difftime ((time_t)O->MTime, StatBuf.st_mtime) > 0.0) {
175             Warning ("Replacing module `%s' by older version", O->Name);
176         }
177     }
178
179     /* Initialize the object module data structure */
180     O->Name       = xstrdup (Module);
181     O->Flags      = OBJ_HAVEDATA;
182     O->MTime      = StatBuf.st_mtime;
183     O->ImportSize = H.ImportSize;
184     O->Imports    = xmalloc (O->ImportSize);
185     O->ExportSize = H.ExportSize;
186     O->Exports    = xmalloc (O->ExportSize);
187
188     /* Read imports and exports */
189     fseek (Obj, H.ImportOffs, SEEK_SET);
190     ReadData (Obj, O->Imports, O->ImportSize);
191     fseek (Obj, H.ExportOffs, SEEK_SET);
192     ReadData (Obj, O->Exports, O->ExportSize);
193
194     /* Skip the object file header */
195     O->Start = ftell (NewLib);
196     fseek (NewLib, OBJ_HDR_SIZE, SEEK_CUR);
197
198     /* Copy the remaining sections */
199     fseek (Obj, H.DbgSymOffs, SEEK_SET);
200     H.DbgSymOffs = LibCopyTo (Obj, H.DbgSymSize) - O->Start;
201     fseek (Obj, H.OptionOffs, SEEK_SET);
202     H.OptionOffs = LibCopyTo (Obj, H.OptionSize) - O->Start;
203     fseek (Obj, H.SegOffs, SEEK_SET);
204     H.SegOffs = LibCopyTo (Obj, H.SegSize) - O->Start;
205     fseek (Obj, H.FileOffs, SEEK_SET);
206     H.FileOffs = LibCopyTo (Obj, H.FileSize) - O->Start;
207     fseek (Obj, H.LineInfoOffs, SEEK_SET);
208     H.LineInfoOffs = LibCopyTo (Obj, H.LineInfoSize) - O->Start;
209
210     /* Calculate the amount of data written */
211     O->Size = ftell (NewLib) - O->Start;
212
213     /* Clear the remaining header fields */
214     H.ImportOffs = H.ImportSize = 0;
215     H.ExportOffs = H.ExportSize = 0;
216
217     /* Seek back and write the updated header */
218     fseek (NewLib, O->Start, SEEK_SET);
219     ObjWriteHeader (NewLib, &H);
220
221     /* Now seek again to end of file */
222     fseek (NewLib, 0, SEEK_END);
223
224     /* Done, close the file (we read it only, so no error check) */
225     fclose (Obj);
226 }
227
228
229
230 void ObjExtract (const char* Name)
231 /* Extract a module from the library */
232 {
233     unsigned long ImportStart;
234     unsigned long ExportStart;
235     struct utimbuf U;
236     ObjHeader H;
237     FILE* Obj;
238
239
240     /* Make a module name from the file name */
241     const char* Module = GetModule (Name);
242
243     /* Try to find the module in the library */
244     ObjData* O = FindObjData (Module);
245
246     /* Bail out if the module does not exist */
247     if (O == 0) {
248         Error ("Module `%s' not found in library", Module);
249     }
250
251     /* Open the output file */
252     Obj = fopen (Name, "w+b");
253     if (Obj == 0) {
254         Error ("Cannot open target file `%s': %s", Name, strerror (errno));
255     }
256
257     /* Copy the first four segments including the header to the new file */
258     LibCopyFrom (O->Start, O->Size, Obj);
259
260     /* Write imports and exports */
261     ImportStart = ftell (Obj);
262     WriteData (Obj, O->Imports, O->ImportSize);
263     ExportStart = ftell (Obj);
264     WriteData (Obj, O->Exports, O->ExportSize);
265
266     /* Seek back and read the header */
267     fseek (Obj, 0, SEEK_SET);
268     ObjReadHeader (Obj, &H, Name);
269
270     /* Update the header fields */
271     H.ImportOffs = ImportStart;
272     H.ImportSize = O->ImportSize;
273     H.ExportOffs = ExportStart;
274     H.ExportSize = O->ExportSize;
275
276     /* Write the changed header */
277     fseek (Obj, 0, SEEK_SET);
278     ObjWriteHeader (Obj, &H);
279
280     /* Close the file */
281     if (fclose (Obj) != 0) {
282         Error ("Problem closing object file `%s': %s", Name, strerror (errno));
283     }
284
285     /* Set access and modification time */
286     U.actime = O->MTime;
287     U.modtime = O->MTime;
288     if (utime (Name, &U) != 0) {
289         Error ("Cannot set mod time on `%s': %s", Name, strerror (errno));
290     }
291 }
292
293
294