Remove redundant SOURCE import
[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 /* Additional memory that is preallocated and contiguous with image
197 which can be used used to relocate bss sections. */
198 char* bssBegin;
199 char* bssEnd;
200
201 ForeignExportStablePtr *stable_ptrs;
202
203 /* Holds the list of symbols in the .o file which
204 require extra information.*/
205 HashTable *extraInfos;
206
207 } ObjectCode;
208
209 #define OC_INFORMATIVE_FILENAME(OC) \
210 ( (OC)->archiveMemberName ? \
211 (OC)->archiveMemberName : \
212 (OC)->fileName \
213 )
214
215 extern ObjectCode *objects;
216 extern ObjectCode *unloaded_objects;
217
218 #if defined(THREADED_RTS)
219 extern Mutex linker_mutex;
220 extern Mutex linker_unloaded_mutex;
221 #endif
222
223 /* Type of the initializer */
224 typedef void (*init_t) (int argc, char **argv, char **env);
225
226 /* SymbolInfo tracks a symbol's address, the object code from which
227 it originated, and whether or not it's weak.
228
229 RtsSymbolInfo is used to track the state of the symbols currently
230 loaded or to be loaded by the Linker.
231
232 Where the information in the `ObjectCode` is used to track the
233 original status of the symbol inside the `ObjectCode`.
234
235 A weak symbol that has been used will still be marked as weak
236 in the `ObjectCode` but in the `RtsSymbolInfo` it won't be.
237 */
238 typedef struct _RtsSymbolInfo {
239 SymbolAddr* value;
240 ObjectCode *owner;
241 HsBool weak;
242 } RtsSymbolInfo;
243
244 void exitLinker( void );
245
246 void freeObjectCode (ObjectCode *oc);
247 SymbolAddr* loadSymbol(SymbolName *lbl, RtsSymbolInfo *pinfo);
248
249 void *mmapForLinker (size_t bytes, uint32_t flags, int fd, int offset);
250
251 void addProddableBlock ( ObjectCode* oc, void* start, int size );
252 void checkProddableBlock (ObjectCode *oc, void *addr, size_t size );
253 void freeProddableBlocks (ObjectCode *oc);
254
255 void addSection (Section *s, SectionKind kind, SectionAlloc alloc,
256 void* start, StgWord size, StgWord mapped_offset,
257 void* mapped_start, StgWord mapped_size);
258
259 HsBool ghciLookupSymbolInfo(HashTable *table,
260 const SymbolName* key, RtsSymbolInfo **result);
261
262 int ghciInsertSymbolTable(
263 pathchar* obj_name,
264 HashTable *table,
265 const SymbolName* key,
266 SymbolAddr* data,
267 HsBool weak,
268 ObjectCode *owner);
269
270 /* lock-free version of lookupSymbol */
271 SymbolAddr* lookupSymbol_ (SymbolName* lbl);
272
273 extern /*Str*/HashTable *symhash;
274
275 pathchar*
276 resolveSymbolAddr (pathchar* buffer, int size,
277 SymbolAddr* symbol, uintptr_t* top);
278
279 /*************************************************
280 * Various bits of configuration
281 *************************************************/
282
283 /* PowerPC and ARM have relative branch instructions with only 24 bit
284 * displacements and therefore need jump islands contiguous with each object
285 * code module.
286 */
287 #if defined(powerpc_HOST_ARCH)
288 #define SHORT_REL_BRANCH 1
289 #endif
290 #if defined(arm_HOST_ARCH)
291 #define SHORT_REL_BRANCH 1
292 #endif
293
294 #if (RTS_LINKER_USE_MMAP && defined(SHORT_REL_BRANCH) && defined(linux_HOST_OS))
295 #define USE_CONTIGUOUS_MMAP 1
296 #else
297 #define USE_CONTIGUOUS_MMAP 0
298 #endif
299
300 HsInt isAlreadyLoaded( pathchar *path );
301 HsInt loadOc( ObjectCode* oc );
302 ObjectCode* mkOc( pathchar *path, char *image, int imageSize,
303 bool mapped, char *archiveMemberName,
304 int misalignment
305 );
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 # include "linker/ElfTypes.h"
321 #elif defined (mingw32_HOST_OS)
322 # define OBJFORMAT_PEi386
323 # include "linker/PEi386Types.h"
324 #elif defined(darwin_HOST_OS) || defined(ios_HOST_OS)
325 # define OBJFORMAT_MACHO
326 # include "linker/MachOTypes.h"
327 #else
328 #error "Unknown OBJECT_FORMAT for HOST_OS"
329 #endif
330
331 /* In order to simplify control flow a bit, some references to mmap-related
332 definitions are blocked off by a C-level if statement rather than a CPP-level
333 #if statement. Since those are dead branches when !RTS_LINKER_USE_MMAP, we
334 just stub out the relevant symbols here
335 */
336 #if !RTS_LINKER_USE_MMAP
337 #define munmap(x,y) /* nothing */
338 #define MAP_ANONYMOUS 0
339 #endif
340
341 #include "EndPrivate.h"