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