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