1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2000
7 * ---------------------------------------------------------------------------*/
14 #if RTS_LINKER_USE_MMAP
18 #include "BeginPrivate.h"
20 typedef void SymbolAddr
;
21 typedef char SymbolName
;
23 /* See Linker.c Note [runtime-linker-phases] */
32 /* Indication of section kinds for loaded objects. Needed by
33 the GC for deciding whether or not a pointer on the stack
35 See Note [BFD import library].
38 enum { /* Section is code or readonly. e.g. .text or .r(o)data. */
39 SECTIONKIND_CODE_OR_RODATA
,
40 /* Section contains read/write data. e.g. .data. */
42 /* Static initializer section. e.g. .ctors. */
43 SECTIONKIND_INIT_ARRAY
,
44 /* We don't know what the section is and don't care. */
46 /* Section belongs to an import section group. e.g. .idata$. */
48 /* Section defines an import library entry, e.g. idata$7. */
49 SECTIONKIND_IMPORT_LIBRARY
,
50 SECTIONKIND_NOINFOAVAIL
63 * Note [No typedefs for customizable types]
64 * Some pointer-to-struct types are defined opaquely
65 * first, and customized later to architecture/ABI-specific
66 * instantiations. Having the usual
67 * typedef struct _Foo {...} Foo;
68 * wrappers is hard to get right with older versions of GCC,
71 * and always refer to it with the 'struct' qualifier.
76 void* start
; /* actual start of section in memory */
77 StgWord size
; /* actual size of section in memory */
82 * The following fields are relevant for SECTION_MMAP sections only
84 StgWord mapped_offset
; /* offset from the image of mapped_start */
85 void* mapped_start
; /* start of mmap() block */
86 StgWord mapped_size
; /* size of mmap() block */
88 /* A customizable type to augment the Section type.
89 * See Note [No typedefs for customizable types]
91 struct SectionFormatInfo
* info
;
96 struct _ProddableBlock
{
99 struct _ProddableBlock
* next
;
104 * We must keep track of the StablePtrs that are created for foreign
105 * exports by constructor functions when the module is loaded, so that
106 * we can free them again when the module is unloaded. If we don't do
107 * this, then the StablePtr will keep the module alive indefinitely.
109 typedef struct ForeignExportStablePtr_
{
110 StgStablePtr stable_ptr
;
111 struct ForeignExportStablePtr_
*next
;
112 } ForeignExportStablePtr
;
114 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
115 #define NEED_SYMBOL_EXTRAS 1
118 /* Jump Islands are sniplets of machine code required for relative
119 * address relocations on the PowerPC, x86_64 and ARM.
122 #if defined(powerpc_HOST_ARCH)
124 short lis_r12
, hi_addr
;
125 short ori_r12_r12
, lo_addr
;
129 #elif defined(x86_64_HOST_ARCH)
131 uint8_t jumpIsland
[6];
132 #elif defined(arm_HOST_ARCH)
133 uint8_t jumpIsland
[16];
138 /* Top-level structure for an object module. One of these is allocated
139 * for each object file in use.
141 typedef struct _ObjectCode
{
144 int fileSize
; /* also mapped image size when using mmap() */
145 char* formatName
; /* eg "ELF32", "DLL", "COFF", etc. */
147 /* If this object is a member of an archive, archiveMemberName is
148 * like "libarchive.a(object.o)". Otherwise it's NULL.
150 char* archiveMemberName
;
152 /* An array containing ptrs to all the symbol names copied from
153 this object into the global symbol hash table. This is so that
154 we know which parts of the latter mapping to nuke when this
155 object is removed from the system. */
159 /* ptr to mem containing the object file image */
162 /* A customizable type, that formats can use to augment ObjectCode
163 * See Note [No typedefs for customizable types]
165 struct ObjectCodeFormatInfo
* info
;
167 /* non-zero if the object file was mmap'd, otherwise malloc'd */
170 /* flag used when deciding whether to unload an object file */
173 /* record by how much image has been deliberately misaligned
174 after allocation, so that we can use realloc */
177 /* The section-kind entries for this object module. Linked
182 /* Allow a chain of these things */
183 struct _ObjectCode
* next
;
185 /* SANITY CHECK ONLY: a list of the only memory regions which may
186 safely be prodded during relocation. Any attempt to prod
187 outside one of these is an error in the linker. */
188 ProddableBlock
* proddables
;
190 #if defined(ia64_HOST_ARCH)
191 /* Procedure Linkage Table for this object */
193 unsigned int pltIndex
;
196 #if defined(NEED_SYMBOL_EXTRAS)
197 SymbolExtra
*symbol_extras
;
198 unsigned long first_symbol_extra
;
199 unsigned long n_symbol_extras
;
202 ForeignExportStablePtr
*stable_ptrs
;
204 /* Holds the list of symbols in the .o file which
205 require extra information.*/
206 HashTable
*extraInfos
;
210 #define OC_INFORMATIVE_FILENAME(OC) \
211 ( (OC)->archiveMemberName ? \
212 (OC)->archiveMemberName : \
216 extern ObjectCode
*objects
;
217 extern ObjectCode
*unloaded_objects
;
219 #if defined(THREADED_RTS)
220 extern Mutex linker_mutex
;
221 extern Mutex linker_unloaded_mutex
;
224 /* Type of the initializer */
225 typedef void (*init_t
) (int argc
, char **argv
, char **env
);
227 /* SymbolInfo tracks a symbol's address, the object code from which
228 it originated, and whether or not it's weak.
230 RtsSymbolInfo is used to track the state of the symbols currently
231 loaded or to be loaded by the Linker.
233 Where the information in the `ObjectCode` is used to track the
234 original status of the symbol inside the `ObjectCode`.
236 A weak symbol that has been used will still be marked as weak
237 in the `ObjectCode` but in the `RtsSymbolInfo` it won't be.
239 typedef struct _RtsSymbolInfo
{
245 void exitLinker( void );
247 void freeObjectCode (ObjectCode
*oc
);
248 SymbolAddr
* loadSymbol(SymbolName
*lbl
, RtsSymbolInfo
*pinfo
);
250 void *mmapForLinker (size_t bytes
, uint32_t flags
, int fd
, int offset
);
252 void addProddableBlock ( ObjectCode
* oc
, void* start
, int size
);
253 void checkProddableBlock (ObjectCode
*oc
, void *addr
, size_t size
);
254 void freeProddableBlocks (ObjectCode
*oc
);
256 void addSection (Section
*s
, SectionKind kind
, SectionAlloc alloc
,
257 void* start
, StgWord size
, StgWord mapped_offset
,
258 void* mapped_start
, StgWord mapped_size
);
260 HsBool
ghciLookupSymbolInfo(HashTable
*table
,
261 const SymbolName
* key
, RtsSymbolInfo
**result
);
263 int ghciInsertSymbolTable(
266 const SymbolName
* key
,
271 /* lock-free version of lookupSymbol */
272 SymbolAddr
* lookupSymbol_ (SymbolName
* lbl
);
274 extern /*Str*/HashTable
*symhash
;
277 /*************************************************
278 * Various bits of configuration
279 *************************************************/
281 /* PowerPC and ARM have relative branch instructions with only 24 bit
282 * displacements and therefore need jump islands contiguous with each object
285 #if defined(powerpc_HOST_ARCH)
286 #define SHORT_REL_BRANCH 1
288 #if defined(arm_HOST_ARCH)
289 #define SHORT_REL_BRANCH 1
292 #if (RTS_LINKER_USE_MMAP && defined(SHORT_REL_BRANCH) && defined(linux_HOST_OS))
293 #define USE_CONTIGUOUS_MMAP 1
295 #define USE_CONTIGUOUS_MMAP 0
298 HsInt
isAlreadyLoaded( pathchar
*path
);
299 HsInt
loadOc( ObjectCode
* oc
);
300 ObjectCode
* mkOc( pathchar
*path
, char *image
, int imageSize
,
301 bool mapped
, char *archiveMemberName
,
305 #if defined(mingw32_HOST_OS)
306 /* We use myindex to calculate array addresses, rather than
307 simply doing the normal subscript thing. That's because
308 some of the above structs have sizes which are not
309 a whole number of words. GCC rounds their sizes up to a
310 whole number of words, which means that the address calcs
311 arising from using normal C indexing or pointer arithmetic
312 are just plain wrong. Sigh.
314 INLINE_HEADER
unsigned char *
315 myindex ( int scale
, void* base
, int index
)
318 ((unsigned char*)base
) + scale
* index
;
321 // Defined in linker/PEi386.c
322 char *cstring_from_section_name(
324 unsigned char* strtab
);
325 #endif /* mingw32_HOST_OS */
327 /* MAP_ANONYMOUS is MAP_ANON on some systems,
328 e.g. OS X (before Sierra), OpenBSD etc */
329 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
330 #define MAP_ANONYMOUS MAP_ANON
333 /* Which object file format are we targetting? */
334 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) \
335 || defined(linux_android_HOST_OS) \
336 || defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) \
337 || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) \
338 || defined(openbsd_HOST_OS) || defined(gnu_HOST_OS)
339 # define OBJFORMAT_ELF
340 # include "linker/ElfTypes.h"
341 #elif defined (mingw32_HOST_OS)
342 # define OBJFORMAT_PEi386
343 struct SectionFormatInfo
{ void* placeholder
; };
344 struct ObjectCodeFormatInfo
{ void* placeholder
; };
345 #elif defined(darwin_HOST_OS) || defined(ios_HOST_OS)
346 # define OBJFORMAT_MACHO
347 # include "linker/MachOTypes.h"
349 #error "Unknown OBJECT_FORMAT for HOST_OS"
352 /* In order to simplify control flow a bit, some references to mmap-related
353 definitions are blocked off by a C-level if statement rather than a CPP-level
354 #if statement. Since those are dead branches when !RTS_LINKER_USE_MMAP, we
355 just stub out the relevant symbols here
357 #if !RTS_LINKER_USE_MMAP
358 #define munmap(x,y) /* nothing */
359 #define MAP_ANONYMOUS 0
362 #include "EndPrivate.h"