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