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