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