Linker: some extra debugging / logging
[ghc.git] / rts / LinkerInternals.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2000
4 *
5 * RTS Object Linker
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #ifndef LINKERINTERNALS_H
10 #define LINKERINTERNALS_H
11
12 #include "Rts.h"
13 #include "Hash.h"
14
15 /* See Linker.c Note [runtime-linker-phases] */
16 typedef enum {
17 OBJECT_LOADED,
18 OBJECT_NEEDED,
19 OBJECT_RESOLVED,
20 OBJECT_UNLOADED,
21 OBJECT_DONT_RESOLVE
22 } OStatus;
23
24 /* Indication of section kinds for loaded objects. Needed by
25 the GC for deciding whether or not a pointer on the stack
26 is a code pointer.
27 */
28 typedef
29 enum { SECTIONKIND_CODE_OR_RODATA,
30 SECTIONKIND_RWDATA,
31 SECTIONKIND_INIT_ARRAY,
32 SECTIONKIND_OTHER,
33 SECTIONKIND_NOINFOAVAIL }
34 SectionKind;
35
36 typedef
37 enum { SECTION_NOMEM,
38 SECTION_M32,
39 SECTION_MMAP,
40 SECTION_MALLOC,
41 }
42 SectionAlloc;
43
44 typedef
45 struct _Section {
46 void* start; /* actual start of section in memory */
47 StgWord size; /* actual size of section in memory */
48 SectionKind kind;
49 SectionAlloc alloc;
50
51 /*
52 * The following fields are relevant for SECTION_MMAP sections only
53 */
54 StgWord mapped_offset; /* offset from the image of mapped_start */
55 void* mapped_start; /* start of mmap() block */
56 StgWord mapped_size; /* size of mmap() block */
57 }
58 Section;
59
60 typedef
61 struct _ProddableBlock {
62 void* start;
63 int size;
64 struct _ProddableBlock* next;
65 }
66 ProddableBlock;
67
68 /*
69 * We must keep track of the StablePtrs that are created for foreign
70 * exports by constructor functions when the module is loaded, so that
71 * we can free them again when the module is unloaded. If we don't do
72 * this, then the StablePtr will keep the module alive indefinitely.
73 */
74 typedef struct ForeignExportStablePtr_ {
75 StgStablePtr stable_ptr;
76 struct ForeignExportStablePtr_ *next;
77 } ForeignExportStablePtr;
78
79 #if powerpc_HOST_ARCH || x86_64_HOST_ARCH || arm_HOST_ARCH
80 #define NEED_SYMBOL_EXTRAS 1
81 #endif
82
83 /* Jump Islands are sniplets of machine code required for relative
84 * address relocations on the PowerPC, x86_64 and ARM.
85 */
86 typedef struct {
87 #ifdef powerpc_HOST_ARCH
88 struct {
89 short lis_r12, hi_addr;
90 short ori_r12_r12, lo_addr;
91 long mtctr_r12;
92 long bctr;
93 } jumpIsland;
94 #elif x86_64_HOST_ARCH
95 uint64_t addr;
96 uint8_t jumpIsland[6];
97 #elif arm_HOST_ARCH
98 uint8_t jumpIsland[16];
99 #endif
100 } SymbolExtra;
101
102
103 /* Top-level structure for an object module. One of these is allocated
104 * for each object file in use.
105 */
106 typedef struct _ObjectCode {
107 OStatus status;
108 pathchar *fileName;
109 int fileSize; /* also mapped image size when using mmap() */
110 char* formatName; /* eg "ELF32", "DLL", "COFF", etc. */
111
112 /* If this object is a member of an archive, archiveMemberName is
113 * like "libarchive.a(object.o)". Otherwise it's NULL.
114 */
115 char* archiveMemberName;
116
117 /* An array containing ptrs to all the symbol names copied from
118 this object into the global symbol hash table. This is so that
119 we know which parts of the latter mapping to nuke when this
120 object is removed from the system. */
121 char** symbols;
122 int n_symbols;
123
124 /* ptr to mem containing the object file image */
125 char* image;
126 /* non-zero if the object file was mmap'd, otherwise malloc'd */
127 int imageMapped;
128
129 /* flag used when deciding whether to unload an object file */
130 int referenced;
131
132 /* record by how much image has been deliberately misaligned
133 after allocation, so that we can use realloc */
134 int misalignment;
135
136 /* The section-kind entries for this object module. Linked
137 list. */
138 int n_sections;
139 Section* sections;
140
141 /* Allow a chain of these things */
142 struct _ObjectCode * next;
143
144 /* SANITY CHECK ONLY: a list of the only memory regions which may
145 safely be prodded during relocation. Any attempt to prod
146 outside one of these is an error in the linker. */
147 ProddableBlock* proddables;
148
149 #ifdef ia64_HOST_ARCH
150 /* Procedure Linkage Table for this object */
151 void *plt;
152 unsigned int pltIndex;
153 #endif
154
155 #if NEED_SYMBOL_EXTRAS
156 SymbolExtra *symbol_extras;
157 unsigned long first_symbol_extra;
158 unsigned long n_symbol_extras;
159 #endif
160
161 ForeignExportStablePtr *stable_ptrs;
162
163 /* Holds the list of symbols in the .o file which
164 require extra information.*/
165 HashTable *extraInfos;
166
167 } ObjectCode;
168
169 #define OC_INFORMATIVE_FILENAME(OC) \
170 ( (OC)->archiveMemberName ? \
171 (OC)->archiveMemberName : \
172 (OC)->fileName \
173 )
174
175 extern ObjectCode *objects;
176 extern ObjectCode *unloaded_objects;
177
178 #ifdef THREADED_RTS
179 extern Mutex linker_mutex;
180 extern Mutex linker_unloaded_mutex;
181 #endif
182
183 void exitLinker( void );
184
185 void freeObjectCode (ObjectCode *oc);
186
187 #if defined(mingw32_HOST_OS)
188
189 typedef unsigned char UChar;
190 typedef unsigned short UInt16;
191 typedef short Int16;
192 typedef unsigned int UInt32;
193 typedef int Int32;
194 typedef unsigned long long int UInt64;
195
196
197 typedef
198 struct {
199 UInt16 Machine;
200 UInt16 NumberOfSections;
201 UInt32 TimeDateStamp;
202 UInt32 PointerToSymbolTable;
203 UInt32 NumberOfSymbols;
204 UInt16 SizeOfOptionalHeader;
205 UInt16 Characteristics;
206 }
207 COFF_header;
208
209 #define sizeof_COFF_header 20
210
211 /* Section 7.1 PE Specification */
212 typedef
213 struct {
214 UInt16 Sig1;
215 UInt16 Sig2;
216 UInt16 Version;
217 UInt16 Machine;
218 UInt32 TimeDateStamp;
219 UInt32 SizeOfData;
220 UInt16 Ordinal;
221 UInt16 Type_NameType_Reserved;
222 }
223 COFF_import_header;
224
225 #define sizeof_COFF_import_Header 20
226
227 typedef
228 struct {
229 UChar Name[8];
230 UInt32 VirtualSize;
231 UInt32 VirtualAddress;
232 UInt32 SizeOfRawData;
233 UInt32 PointerToRawData;
234 UInt32 PointerToRelocations;
235 UInt32 PointerToLinenumbers;
236 UInt16 NumberOfRelocations;
237 UInt16 NumberOfLineNumbers;
238 UInt32 Characteristics;
239 }
240 COFF_section;
241
242 #define sizeof_COFF_section 40
243
244
245 typedef
246 struct {
247 UChar Name[8];
248 UInt32 Value;
249 Int16 SectionNumber;
250 UInt16 Type;
251 UChar StorageClass;
252 UChar NumberOfAuxSymbols;
253 }
254 COFF_symbol;
255
256 #define sizeof_COFF_symbol 18
257
258
259 typedef
260 struct {
261 UInt32 VirtualAddress;
262 UInt32 SymbolTableIndex;
263 UInt16 Type;
264 }
265 COFF_reloc;
266
267 #define sizeof_COFF_reloc 10
268
269 /* From PE spec doc, section 3.3.2 */
270 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
271 windows.h -- for the same purpose, but I want to know what I'm
272 getting, here. */
273 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
274 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
275 #define MYIMAGE_FILE_DLL 0x2000
276 #define MYIMAGE_FILE_SYSTEM 0x1000
277 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
278 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
279 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
280
281 /* From PE spec doc, section 5.4.2 and 5.4.4 */
282 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
283 #define MYIMAGE_SYM_CLASS_STATIC 3
284 #define MYIMAGE_SYM_UNDEFINED 0
285 #define MYIMAGE_SYM_CLASS_SECTION 104
286 #define MYIMAGE_SYM_CLASS_WEAK_EXTERNAL 105
287
288 /* From PE spec doc, section 3.1 */
289 #define MYIMAGE_SCN_CNT_CODE 0x00000020
290 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
291 #define MYIMAGE_SCN_CNT_UNINITIALIZED_DATA 0x00000080
292 #define MYIMAGE_SCN_LNK_COMDAT 0x00001000
293 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
294 #define MYIMAGE_SCN_LNK_REMOVE 0x00000800
295 #define MYIMAGE_SCN_MEM_DISCARDABLE 0x02000000
296
297 /* From PE spec doc, section 5.2.1 */
298 #define MYIMAGE_REL_I386_DIR32 0x0006
299 #define MYIMAGE_REL_I386_DIR32NB 0x0007
300 #define MYIMAGE_REL_I386_REL32 0x0014
301
302 #endif /* OBJFORMAT_PEi386 */
303
304 #endif /* LINKERINTERNALS_H */