]> git.sur5r.net Git - cc65/blob - src/ld65/objfile.c
26d7132e77c371a6c256fdc4a846cd3aa3aafa11
[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     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 #include <time.h>
39 #include <sys/stat.h>
40
41 /* common */
42 #include "xmalloc.h"
43
44 /* ld65 */
45 #include "dbgsyms.h"
46 #include "error.h"
47 #include "exports.h"
48 #include "fileio.h"
49 #include "objdata.h"
50 #include "segments.h"
51 #include "objfile.h"
52
53
54
55 /*****************************************************************************/
56 /*                                   Code                                    */
57 /*****************************************************************************/
58
59
60
61 static const char* GetModule (const char* Name)
62 /* Get a module name from the file name */
63 {
64     /* Make a module name from the file name */
65     const char* Module = Name + strlen (Name);
66     while (Module > Name) {
67         --Module;
68         if (*Module == '/' || *Module == '\\') {
69             ++Module;
70             break;
71         }
72     }
73     if (*Module == 0) {
74         Error ("Cannot make module name from `%s'", Name);
75     }
76     return Module;
77 }
78
79
80
81 static void ObjReadHeader (FILE* Obj, ObjHeader* H, const char* Name)
82 /* Read the header of the object file checking the signature */
83 {
84     H->Version    = Read16 (Obj);
85     if (H->Version != OBJ_VERSION) {
86         Error ("Object file `%s' has wrong version", Name);
87     }
88     H->Flags      = Read16 (Obj);
89     H->OptionOffs = Read32 (Obj);
90     H->OptionSize = Read32 (Obj);
91     H->FileOffs   = Read32 (Obj);
92     H->FileSize   = Read32 (Obj);
93     H->SegOffs    = Read32 (Obj);
94     H->SegSize    = Read32 (Obj);
95     H->ImportOffs = Read32 (Obj);
96     H->ImportSize = Read32 (Obj);
97     H->ExportOffs = Read32 (Obj);
98     H->ExportSize = Read32 (Obj);
99     H->DbgSymOffs = Read32 (Obj);
100     H->DbgSymSize = Read32 (Obj);
101 }
102
103
104
105 void ObjReadFiles (FILE* F, ObjData* O)
106 /* Read the files list from a file at the current position */
107 {
108     unsigned I;
109
110     O->FileCount  = ReadVar (F);
111     O->Files      = xmalloc (O->FileCount * sizeof (char*));
112     for (I = 0; I < O->FileCount; ++I) {
113         /* Skip MTime and size */
114         Read32 (F);
115         Read32 (F);
116         /* Read the filename */
117         O->Files [I] = ReadStr (F);
118     }
119 }
120
121
122
123 void ObjReadImports (FILE* F, ObjData* O)
124 /* Read the imports from a file at the current position */
125 {
126     unsigned I;
127
128     O->ImportCount = ReadVar (F);
129     O->Imports     = xmalloc (O->ImportCount * sizeof (Import*));
130     for (I = 0; I < O->ImportCount; ++I) {
131         O->Imports [I] = ReadImport (F, O);
132         InsertImport (O->Imports [I]);
133     }
134 }
135
136
137
138 void ObjReadExports (FILE* F, ObjData* O)
139 /* Read the exports from a file at the current position */
140 {
141     unsigned I;
142
143     O->ExportCount = ReadVar (F);
144     O->Exports     = xmalloc (O->ExportCount * sizeof (Export*));
145     for (I = 0; I < O->ExportCount; ++I) {
146         O->Exports [I] = ReadExport (F, O);
147         InsertExport (O->Exports [I]);
148     }
149 }
150
151
152
153 void ObjReadDbgSyms (FILE* F, ObjData* O)
154 /* Read the debug symbols from a file at the current position */
155 {
156     unsigned I;
157
158     O->DbgSymCount = ReadVar (F);
159     O->DbgSyms     = xmalloc (O->DbgSymCount * sizeof (DbgSym*));
160     for (I = 0; I < O->DbgSymCount; ++I) {
161         O->DbgSyms [I] = ReadDbgSym (F, O);
162     }
163 }
164
165
166
167 void ObjReadSections (FILE* F, ObjData* O)
168 /* Read the section data from a file at the current position */
169 {
170     unsigned I;
171
172     O->SectionCount = ReadVar (F);
173     O->Sections     = xmalloc (O->SectionCount * sizeof (Section*));
174     for (I = 0; I < O->SectionCount; ++I) {
175         O->Sections [I] = ReadSection (F, O);
176     }
177 }
178
179
180
181 void ObjAdd (FILE* Obj, const char* Name)
182 /* Add an object file to the module list */
183 {
184     /* Create a new structure for the object file data */
185     ObjData* O = NewObjData ();
186
187     /* The magic was already read and checked, so set it in the header */
188     O->Header.Magic = OBJ_MAGIC;
189
190     /* Read and check the header */
191     ObjReadHeader (Obj, &O->Header, Name);
192
193     /* Initialize the object module data structure */
194     O->Name       = xstrdup (GetModule (Name));
195     O->Flags      = OBJ_HAVEDATA;
196
197     /* Read the files list from the object file */
198     fseek (Obj, O->Header.FileOffs, SEEK_SET);
199     ObjReadFiles (Obj, O);
200
201     /* Read the imports list from the object file */
202     fseek (Obj, O->Header.ImportOffs, SEEK_SET);
203     ObjReadImports (Obj, O);
204
205     /* Read the object file exports and insert them into the exports list */
206     fseek (Obj, O->Header.ExportOffs, SEEK_SET);
207     ObjReadExports (Obj, O);
208
209     /* Read the object debug symbols from the object file */
210     fseek (Obj, O->Header.DbgSymOffs, SEEK_SET);
211     ObjReadDbgSyms (Obj, O);
212
213     /* Read the segment list from the object file. This must be last, since
214      * the expressions stored in the code may reference segments or imported
215      * symbols.
216      */
217     fseek (Obj, O->Header.SegOffs, SEEK_SET);
218     ObjReadSections (Obj, O);
219
220     /* Mark this object file as needed */
221     O->Flags |= OBJ_REF | OBJ_HAVEDATA;
222
223     /* Done, close the file (we read it only, so no error check) */
224     fclose (Obj);
225 }
226
227
228
229