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