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