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