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