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