]> git.sur5r.net Git - cc65/blob - src/ld65/objfile.c
Allow to set character translations at compile time
[cc65] / src / ld65 / objfile.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 objfile.c                                 */
4 /*                                                                           */
5 /*                 Object file handling for the ld65 linker                  */
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 #include <time.h>
39 #include <sys/types.h>          /* EMX needs this */
40 #include <sys/stat.h>
41
42 /* common */
43 #include "xmalloc.h"
44
45 /* ld65 */
46 #include "dbgsyms.h"
47 #include "error.h"
48 #include "exports.h"
49 #include "fileinfo.h"
50 #include "fileio.h"
51 #include "lineinfo.h"
52 #include "objdata.h"
53 #include "segments.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 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
85 /* Read the header of the object file checking the signature */
86 {
87     H->Version    = Read16 (Obj);
88     if (H->Version != OBJ_VERSION) {
89         Error ("Object file `%s' has wrong version", Name);
90     }
91     H->Flags        = Read16 (Obj);
92     H->OptionOffs   = Read32 (Obj);
93     H->OptionSize   = Read32 (Obj);
94     H->FileOffs     = Read32 (Obj);
95     H->FileSize     = Read32 (Obj);
96     H->SegOffs      = Read32 (Obj);
97     H->SegSize      = Read32 (Obj);
98     H->ImportOffs   = Read32 (Obj);
99     H->ImportSize   = Read32 (Obj);
100     H->ExportOffs   = Read32 (Obj);
101     H->ExportSize   = Read32 (Obj);
102     H->DbgSymOffs   = Read32 (Obj);
103     H->DbgSymSize   = Read32 (Obj);
104     H->LineInfoOffs = Read32 (Obj);
105     H->LineInfoSize = Read32 (Obj);
106 }
107
108
109
110 void ObjReadFiles (FILE* F, ObjData* O)
111 /* Read the files list from a file at the current position */
112 {
113     unsigned I;
114
115     O->FileCount  = ReadVar (F);
116     O->Files      = xmalloc (O->FileCount * sizeof (FileInfo*));
117     for (I = 0; I < O->FileCount; ++I) {
118         O->Files[I] = ReadFileInfo (F, O);
119     }
120 }
121
122
123
124 void ObjReadImports (FILE* F, ObjData* O)
125 /* Read the imports from a file at the current position */
126 {
127     unsigned I;
128
129     O->ImportCount = ReadVar (F);
130     O->Imports     = xmalloc (O->ImportCount * sizeof (Import*));
131     for (I = 0; I < O->ImportCount; ++I) {
132         O->Imports [I] = ReadImport (F, O);
133         InsertImport (O->Imports [I]);
134     }
135 }
136
137
138
139 void ObjReadExports (FILE* F, ObjData* O)
140 /* Read the exports from a file at the current position */
141 {
142     unsigned I;
143
144     O->ExportCount = ReadVar (F);
145     O->Exports     = xmalloc (O->ExportCount * sizeof (Export*));
146     for (I = 0; I < O->ExportCount; ++I) {
147         O->Exports [I] = ReadExport (F, O);
148         InsertExport (O->Exports [I]);
149     }
150 }
151
152
153
154 void ObjReadDbgSyms (FILE* F, ObjData* O)
155 /* Read the debug symbols from a file at the current position */
156 {
157     unsigned I;
158
159     O->DbgSymCount = ReadVar (F);
160     O->DbgSyms     = xmalloc (O->DbgSymCount * sizeof (DbgSym*));
161     for (I = 0; I < O->DbgSymCount; ++I) {
162         O->DbgSyms [I] = ReadDbgSym (F, O);
163     }
164 }
165
166
167
168 void ObjReadLineInfos (FILE* F, ObjData* O)
169 /* Read the line infos from a file at the current position */
170 {
171     unsigned I;
172
173     O->LineInfoCount = ReadVar (F);
174     O->LineInfos     = xmalloc (O->LineInfoCount * sizeof (LineInfo*));
175     for (I = 0; I < O->LineInfoCount; ++I) {
176         O->LineInfos[I] = ReadLineInfo (F, O);
177     }
178 }
179
180
181
182 void ObjReadSections (FILE* F, ObjData* O)
183 /* Read the section data from a file at the current position */
184 {
185     unsigned I;
186
187     O->SectionCount = ReadVar (F);
188     O->Sections     = xmalloc (O->SectionCount * sizeof (Section*));
189     for (I = 0; I < O->SectionCount; ++I) {
190         O->Sections [I] = ReadSection (F, O);
191     }
192 }
193
194
195
196 void ObjAdd (FILE* Obj, const char* Name)
197 /* Add an object file to the module list */
198 {
199     /* Create a new structure for the object file data */
200     ObjData* O = NewObjData ();
201
202     /* The magic was already read and checked, so set it in the header */
203     O->Header.Magic = OBJ_MAGIC;
204
205     /* Read and check the header */
206     ObjReadHeader (Obj, &O->Header, Name);
207
208     /* Initialize the object module data structure */
209     O->Name  = xstrdup (GetModule (Name));
210     O->Flags = OBJ_HAVEDATA;
211
212     /* Read the files list from the object file */
213     fseek (Obj, O->Header.FileOffs, SEEK_SET);
214     ObjReadFiles (Obj, O);
215
216     /* Read the imports list from the object file */
217     fseek (Obj, O->Header.ImportOffs, SEEK_SET);
218     ObjReadImports (Obj, O);
219
220     /* Read the object file exports and insert them into the exports list */
221     fseek (Obj, O->Header.ExportOffs, SEEK_SET);
222     ObjReadExports (Obj, O);
223
224     /* Read the object debug symbols from the object file */
225     fseek (Obj, O->Header.DbgSymOffs, SEEK_SET);
226     ObjReadDbgSyms (Obj, O);
227
228     /* Read the line infos from the object file */
229     fseek (Obj, O->Header.LineInfoOffs, SEEK_SET);
230     ObjReadLineInfos (Obj, O);
231
232     /* Read the segment list from the object file. This must be last, since
233      * the expressions stored in the code may reference segments or imported
234      * symbols.
235      */
236     fseek (Obj, O->Header.SegOffs, SEEK_SET);
237     ObjReadSections (Obj, O);
238
239     /* Mark this object file as needed */
240     O->Flags |= OBJ_REF | OBJ_HAVEDATA;
241
242     /* Done, close the file (we read it only, so no error check) */
243     fclose (Obj);
244 }
245
246
247
248