CNF: Silence pointer fix-up message unless gc debugging is enabled
[ghc.git] / rts / LinkerInternals.h
1 /* -----------------------------------------------------------------------------
2 *
3 * (c) The GHC Team, 2000
4 *
5 * RTS Object Linker
6 *
7 * ---------------------------------------------------------------------------*/
8
9 #pragma once
10
11 #include "Rts.h"
12 #include "Hash.h"
13
14 #if RTS_LINKER_USE_MMAP
15 #include <sys/mman.h>
16 #endif
17
18 #include "BeginPrivate.h"
19
20 typedef void SymbolAddr;
21 typedef char SymbolName;
22
23 /* See Linker.c Note [runtime-linker-phases] */
24 typedef enum {
25 OBJECT_LOADED,
26 OBJECT_NEEDED,
27 OBJECT_RESOLVED,
28 OBJECT_UNLOADED,
29 OBJECT_DONT_RESOLVE
30 } OStatus;
31
32 /* Indication of section kinds for loaded objects. Needed by
33 the GC for deciding whether or not a pointer on the stack
34 is a code pointer.
35 */
36 typedef
37 enum { SECTIONKIND_CODE_OR_RODATA,
38 SECTIONKIND_RWDATA,
39 SECTIONKIND_INIT_ARRAY,
40 SECTIONKIND_OTHER,
41 SECTIONKIND_NOINFOAVAIL }
42 SectionKind;
43
44 typedef
45 enum { SECTION_NOMEM,
46 SECTION_M32,
47 SECTION_MMAP,
48 SECTION_MALLOC,
49 }
50 SectionAlloc;
51
52 /*
53 * Note [No typedefs for customizable types]
54 * Some pointer-to-struct types are defined opaquely
55 * first, and customized later to architecture/ABI-specific
56 * instantiations. Having the usual
57 * typedef struct _Foo {...} Foo;
58 * wrappers is hard to get right with older versions of GCC,
59 * so just have a
60 * struct Foo {...};
61 * and always refer to it with the 'struct' qualifier.
62 */
63
64 typedef
65 struct _Section {
66 void* start; /* actual start of section in memory */
67 StgWord size; /* actual size of section in memory */
68 SectionKind kind;
69 SectionAlloc alloc;
70
71 /*
72 * The following fields are relevant for SECTION_MMAP sections only
73 */
74 StgWord mapped_offset; /* offset from the image of mapped_start */
75 void* mapped_start; /* start of mmap() block */
76 StgWord mapped_size; /* size of mmap() block */
77
78 /* A customizable type to augment the Section type.
79 * See Note [No typedefs for customizable types]
80 */
81 struct SectionFormatInfo* info;
82 }
83 Section;
84
85 typedef
86 struct _ProddableBlock {
87 void* start;
88 int size;
89 struct _ProddableBlock* next;
90 }
91 ProddableBlock;
92
93 /*
94 * We must keep track of the StablePtrs that are created for foreign
95 * exports by constructor functions when the module is loaded, so that
96 * we can free them again when the module is unloaded. If we don't do
97 * this, then the StablePtr will keep the module alive indefinitely.
98 */
99 typedef struct ForeignExportStablePtr_ {
100 StgStablePtr stable_ptr;
101 struct ForeignExportStablePtr_ *next;
102 } ForeignExportStablePtr;
103
104 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH) \
105 || defined(arm_HOST_ARCH)
106 /* ios currently uses adjacent got tables, and no symbol extras */
107 #if !defined(ios_HOST_OS)
108 #define NEED_SYMBOL_EXTRAS 1
109 #endif /* ios_HOST_OS */
110 #endif
111
112 /* iOS Simulator however, needs symbol extras for now (#13678) */
113 #if defined(ios_HOST_OS) && defined(x86_64_HOST_ARCH)
114 #define NEED_SYMBOL_EXTRAS 1
115 #endif
116
117 /* Jump Islands are sniplets of machine code required for relative
118 * address relocations on the PowerPC, x86_64 and ARM.
119 */
120 typedef struct {
121 #if defined(powerpc_HOST_ARCH)
122 struct {
123 short lis_r12, hi_addr;
124 short ori_r12_r12, lo_addr;
125 long mtctr_r12;
126 long bctr;
127 } jumpIsland;
128 #elif defined(x86_64_HOST_ARCH)
129 uint64_t addr;
130 uint8_t jumpIsland[6];
131 #elif defined(arm_HOST_ARCH)
132 uint8_t jumpIsland[16];
133 #endif
134 } SymbolExtra;
135
136
137 /* Top-level structure for an object module. One of these is allocated
138 * for each object file in use.
139 */
140 typedef struct _ObjectCode {
141 OStatus status;
142 pathchar *fileName;
143 int fileSize; /* also mapped image size when using mmap() */
144 char* formatName; /* eg "ELF32", "DLL", "COFF", etc. */
145
146 /* If this object is a member of an archive, archiveMemberName is
147 * like "libarchive.a(object.o)". Otherwise it's NULL.
148 */
149 char* archiveMemberName;
150
151 /* An array containing ptrs to all the symbol names copied from
152 this object into the global symbol hash table. This is so that
153 we know which parts of the latter mapping to nuke when this
154 object is removed from the system. */
155 char** symbols;
156 int n_symbols;
157
158 /* ptr to mem containing the object file image */
159 char* image;
160
161 /* A customizable type, that formats can use to augment ObjectCode
162 * See Note [No typedefs for customizable types]
163 */
164 struct ObjectCodeFormatInfo* info;
165
166 /* non-zero if the object file was mmap'd, otherwise malloc'd */
167 int imageMapped;
168
169 /* flag used when deciding whether to unload an object file */
170 int referenced;
171
172 /* record by how much image has been deliberately misaligned
173 after allocation, so that we can use realloc */
174 int misalignment;
175
176 /* The section-kind entries for this object module. Linked
177 list. */
178 int n_sections;
179 Section* sections;
180
181 /* Allow a chain of these things */
182 struct _ObjectCode * next;
183
184 /* SANITY CHECK ONLY: a list of the only memory regions which may
185 safely be prodded during relocation. Any attempt to prod
186 outside one of these is an error in the linker. */
187 ProddableBlock* proddables;
188
189 #if defined(ia64_HOST_ARCH)
190 /* Procedure Linkage Table for this object */
191 void *plt;
192 unsigned int pltIndex;
193 #endif
194
195 #if defined(NEED_SYMBOL_EXTRAS)
196 SymbolExtra *symbol_extras;
197 unsigned long first_symbol_extra;
198 unsigned long n_symbol_extras;
199 #endif
200
201 ForeignExportStablePtr *stable_ptrs;
202
203 /* Holds the list of symbols in the .o file which
204 require extra information.*/
205 HashTable *extraInfos;
206
207 } ObjectCode;
208
209 #define OC_INFORMATIVE_FILENAME(OC) \
210 ( (OC)->archiveMemberName ? \
211 (OC)->archiveMemberName : \
212 (OC)->fileName \
213 )
214
215 extern ObjectCode *objects;
216 extern ObjectCode *unloaded_objects;
217
218 #if defined(THREADED_RTS)
219 extern Mutex linker_mutex;
220 extern Mutex linker_unloaded_mutex;
221 #endif
222
223 /* Type of the initializer */
224 typedef void (*init_t) (int argc, char **argv, char **env);
225
226 /* SymbolInfo tracks a symbol's address, the object code from which
227 it originated, and whether or not it's weak.
228
229 RtsSymbolInfo is used to track the state of the symbols currently
230 loaded or to be loaded by the Linker.
231
232 Where the information in the `ObjectCode` is used to track the
233 original status of the symbol inside the `ObjectCode`.
234
235 A weak symbol that has been used will still be marked as weak
236 in the `ObjectCode` but in the `RtsSymbolInfo` it won't be.
237 */
238 typedef struct _RtsSymbolInfo {
239 SymbolAddr* value;
240 ObjectCode *owner;
241 HsBool weak;
242 } RtsSymbolInfo;
243
244 void exitLinker( void );
245
246 void freeObjectCode (ObjectCode *oc);
247 SymbolAddr* loadSymbol(SymbolName *lbl, RtsSymbolInfo *pinfo);
248
249 void *mmapForLinker (size_t bytes, uint32_t flags, int fd, int offset);
250
251 void addProddableBlock ( ObjectCode* oc, void* start, int size );
252 void checkProddableBlock (ObjectCode *oc, void *addr, size_t size );
253 void freeProddableBlocks (ObjectCode *oc);
254
255 void addSection (Section *s, SectionKind kind, SectionAlloc alloc,
256 void* start, StgWord size, StgWord mapped_offset,
257 void* mapped_start, StgWord mapped_size);
258
259 HsBool ghciLookupSymbolInfo(HashTable *table,
260 const SymbolName* key, RtsSymbolInfo **result);
261
262 int ghciInsertSymbolTable(
263 pathchar* obj_name,
264 HashTable *table,
265 const SymbolName* key,
266 SymbolAddr* data,
267 HsBool weak,
268 ObjectCode *owner);
269
270 /* lock-free version of lookupSymbol */
271 SymbolAddr* lookupSymbol_ (SymbolName* lbl);
272
273 extern /*Str*/HashTable *symhash;
274
275
276 /*************************************************
277 * Various bits of configuration
278 *************************************************/
279
280 /* PowerPC and ARM have relative branch instructions with only 24 bit
281 * displacements and therefore need jump islands contiguous with each object
282 * code module.
283 */
284 #if defined(powerpc_HOST_ARCH)
285 #define SHORT_REL_BRANCH 1
286 #endif
287 #if defined(arm_HOST_ARCH)
288 #define SHORT_REL_BRANCH 1
289 #endif
290
291 #if (RTS_LINKER_USE_MMAP && defined(SHORT_REL_BRANCH) && defined(linux_HOST_OS))
292 #define USE_CONTIGUOUS_MMAP 1
293 #else
294 #define USE_CONTIGUOUS_MMAP 0
295 #endif
296
297 HsInt isAlreadyLoaded( pathchar *path );
298 HsInt loadOc( ObjectCode* oc );
299 ObjectCode* mkOc( pathchar *path, char *image, int imageSize,
300 bool mapped, char *archiveMemberName,
301 int misalignment
302 );
303
304 #if defined(mingw32_HOST_OS)
305 /* We use myindex to calculate array addresses, rather than
306 simply doing the normal subscript thing. That's because
307 some of the above structs have sizes which are not
308 a whole number of words. GCC rounds their sizes up to a
309 whole number of words, which means that the address calcs
310 arising from using normal C indexing or pointer arithmetic
311 are just plain wrong. Sigh.
312 */
313 INLINE_HEADER unsigned char *
314 myindex ( int scale, void* base, int index )
315 {
316 return
317 ((unsigned char*)base) + scale * index;
318 }
319
320 // Defined in linker/PEi386.c
321 char *cstring_from_section_name(
322 unsigned char* name,
323 unsigned char* strtab);
324 #endif /* mingw32_HOST_OS */
325
326 /* MAP_ANONYMOUS is MAP_ANON on some systems,
327 e.g. OS X (before Sierra), OpenBSD etc */
328 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
329 #define MAP_ANONYMOUS MAP_ANON
330 #endif
331
332 /* Which object file format are we targetting? */
333 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) \
334 || defined(linux_android_HOST_OS) \
335 || defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) \
336 || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) \
337 || defined(openbsd_HOST_OS) || defined(gnu_HOST_OS)
338 # define OBJFORMAT_ELF
339 # include "linker/ElfTypes.h"
340 #elif defined (mingw32_HOST_OS)
341 # define OBJFORMAT_PEi386
342 struct SectionFormatInfo { void* placeholder; };
343 struct ObjectCodeFormatInfo { void* placeholder; };
344 #elif defined(darwin_HOST_OS) || defined(ios_HOST_OS)
345 # define OBJFORMAT_MACHO
346 # include "linker/MachOTypes.h"
347 #else
348 #error "Unknown OBJECT_FORMAT for HOST_OS"
349 #endif
350
351 /* In order to simplify control flow a bit, some references to mmap-related
352 definitions are blocked off by a C-level if statement rather than a CPP-level
353 #if statement. Since those are dead branches when !RTS_LINKER_USE_MMAP, we
354 just stub out the relevant symbols here
355 */
356 #if !RTS_LINKER_USE_MMAP
357 #define munmap(x,y) /* nothing */
358 #define MAP_ANONYMOUS 0
359 #endif
360
361 #include "EndPrivate.h"