]> git.sur5r.net Git - cc65/blob - src/ld65/objdata.c
c962c5164cb047a8c7c16899d32f96d1fab9551c
[cc65] / src / ld65 / objdata.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 objdata.c                                 */
4 /*                                                                           */
5 /*               Handling object file data for the ld65 linker               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 1998-2003 Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
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
38 /* common */
39 #include "check.h"
40 #include "xmalloc.h"
41
42 /* ld65 */
43 #include "error.h"
44 #include "exports.h"
45 #include "fileinfo.h"
46 #include "objdata.h"
47 #include "spool.h"
48
49
50
51 /*****************************************************************************/
52 /*                                   Data                                    */
53 /*****************************************************************************/
54
55
56
57 /* Collection containing used ObjData objects */
58 Collection       ObjDataList = STATIC_COLLECTION_INITIALIZER;
59
60
61
62 /*****************************************************************************/
63 /*                                   Code                                    */
64 /*****************************************************************************/
65
66
67
68 ObjData* NewObjData (void)
69 /* Allocate a new structure on the heap, insert it into the list, return it */
70 {
71     /* Allocate memory */
72     ObjData* O = xmalloc (sizeof (ObjData));
73
74     /* Initialize the data */
75     O->Next             = 0;
76     O->Name             = INVALID_STRING_ID;
77     O->LibName          = INVALID_STRING_ID;
78     O->MTime            = 0;
79     O->Flags            = 0;
80     O->Start            = 0;
81     O->ExportCount      = 0;
82     O->Exports          = 0;
83     O->ImportCount      = 0;
84     O->Imports          = 0;
85     O->DbgSymCount      = 0;
86     O->DbgSyms          = 0;
87     O->LineInfoCount    = 0;
88     O->LineInfos        = 0;
89     O->StringCount      = 0;
90     O->Strings          = 0;
91
92     /* Return the new entry */
93     return O;
94 }
95
96
97
98 void FreeObjData (ObjData* O)
99 /* Free an ObjData object. NOTE: This function works only for unused object
100  * data, that is, ObjData objects that aren't used because they aren't
101  * referenced.
102  */
103 {
104     /* Unused ObjData do only have the string pool, Exports and Imports. */
105     while (O->ExportCount) {
106         FreeExport (O->Exports[--O->ExportCount]);
107     }
108     xfree (O->Exports);
109     while (O->ImportCount) {
110         FreeImport (O->Imports[--O->ImportCount]);
111     }
112     xfree (O->Imports);
113     xfree (O->Strings);
114     xfree (O);
115 }
116
117
118
119 void FreeObjStrings (ObjData* O)
120 /* Free the module string data. Used once the object file is loaded completely
121  * when all strings are converted to global strings.
122  */
123 {
124     xfree (O->Strings);
125     O->Strings = 0;
126 }
127
128
129
130 void InsertObjData (ObjData* O)
131 /* Insert the ObjData object into the collection of used ObjData objects. */
132 {
133     CollAppend (&ObjDataList, O);
134 }
135
136
137
138 unsigned MakeGlobalStringId (const ObjData* O, unsigned Index)
139 /* Convert a local string id into a global one and return it. */
140 {
141     if (Index >= O->StringCount) {
142         Error ("Invalid string index (%u) in module `%s'",
143                Index, GetObjFileName (O));
144     }
145     return O->Strings[Index];
146 }
147
148
149
150 const char* GetObjFileName (const ObjData* O)
151 /* Get the name of the object file. Return "[linker generated]" if the object
152  * file is NULL.
153  */
154 {
155     return O? GetString (O->Name) : "[linker generated]";
156 }
157
158
159
160 const char* GetSourceFileName (const ObjData* O, unsigned Index)
161 /* Get the name of the source file with the given index. If O is NULL, return
162  * "[linker generated]" as the file name.
163  */
164 {
165     /* Check if we have an object file */
166     if (O == 0) {
167
168         /* No object file */
169         return "[linker generated]";
170
171     } else {
172
173         /* Check the parameter */
174         PRECONDITION (Index < O->FileCount);
175
176         /* Return the name */
177         return GetString (O->Files[Index]->Name);
178
179     }
180 }
181
182
183
184