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