1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2000-2004
7 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
13 /* Linux needs _GNU_SOURCE to get RTLD_DEFAULT from <dlfcn.h> and
14 MREMAP_MAYMOVE from <sys/mman.h>.
23 #include "sm/Storage.h"
26 #include "LinkerInternals.h"
29 #include "StgPrimFloat.h" // for __int_encodeFloat etc.
32 #if !defined(mingw32_HOST_OS)
33 #include "posix/Signals.h"
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
45 #ifdef HAVE_SYS_STAT_H
49 #if defined(HAVE_DLFCN_H)
53 #if defined(cygwin32_HOST_OS)
58 #ifdef HAVE_SYS_TIME_H
62 #include <sys/fcntl.h>
63 #include <sys/termios.h>
64 #include <sys/utime.h>
65 #include <sys/utsname.h>
69 #if defined(ia64_HOST_ARCH) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
74 #if defined(linux_HOST_OS) || defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
82 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
83 # define OBJFORMAT_ELF
84 # include <regex.h> // regex is already used by dlopen() so this is OK
85 // to use here without requiring an additional lib
86 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
87 # define OBJFORMAT_PEi386
90 #elif defined(darwin_HOST_OS)
91 # define OBJFORMAT_MACHO
92 # include <mach-o/loader.h>
93 # include <mach-o/nlist.h>
94 # include <mach-o/reloc.h>
95 #if !defined(HAVE_DLFCN_H)
96 # include <mach-o/dyld.h>
98 #if defined(powerpc_HOST_ARCH)
99 # include <mach-o/ppc/reloc.h>
101 #if defined(x86_64_HOST_ARCH)
102 # include <mach-o/x86_64/reloc.h>
106 /* Hash table mapping symbol names to Symbol */
107 static /*Str*/HashTable *symhash;
109 /* Hash table mapping symbol names to StgStablePtr */
110 static /*Str*/HashTable *stablehash;
112 /* List of currently loaded objects */
113 ObjectCode *objects = NULL; /* initially empty */
115 #if defined(OBJFORMAT_ELF)
116 static int ocVerifyImage_ELF ( ObjectCode* oc );
117 static int ocGetNames_ELF ( ObjectCode* oc );
118 static int ocResolve_ELF ( ObjectCode* oc );
119 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
120 static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc );
122 #elif defined(OBJFORMAT_PEi386)
123 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
124 static int ocGetNames_PEi386 ( ObjectCode* oc );
125 static int ocResolve_PEi386 ( ObjectCode* oc );
126 static void *lookupSymbolInDLLs ( unsigned char *lbl );
127 static void zapTrailingAtSign ( unsigned char *sym );
128 #elif defined(OBJFORMAT_MACHO)
129 static int ocVerifyImage_MachO ( ObjectCode* oc );
130 static int ocGetNames_MachO ( ObjectCode* oc );
131 static int ocResolve_MachO ( ObjectCode* oc );
133 static int machoGetMisalignment( FILE * );
134 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
135 static int ocAllocateSymbolExtras_MachO ( ObjectCode* oc );
137 #ifdef powerpc_HOST_ARCH
138 static void machoInitSymbolsWithoutUnderscore( void );
142 /* on x86_64 we have a problem with relocating symbol references in
143 * code that was compiled without -fPIC. By default, the small memory
144 * model is used, which assumes that symbol references can fit in a
145 * 32-bit slot. The system dynamic linker makes this work for
146 * references to shared libraries by either (a) allocating a jump
147 * table slot for code references, or (b) moving the symbol at load
148 * time (and copying its contents, if necessary) for data references.
150 * We unfortunately can't tell whether symbol references are to code
151 * or data. So for now we assume they are code (the vast majority
152 * are), and allocate jump-table slots. Unfortunately this will
153 * SILENTLY generate crashing code for data references. This hack is
154 * enabled by X86_64_ELF_NONPIC_HACK.
156 * One workaround is to use shared Haskell libraries. This is
157 * coming. Another workaround is to keep the static libraries but
158 * compile them with -fPIC, because that will generate PIC references
159 * to data which can be relocated. The PIC code is still too green to
160 * do this systematically, though.
163 * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
165 * Naming Scheme for Symbol Macros
167 * SymI_*: symbol is internal to the RTS. It resides in an object
168 * file/library that is statically.
169 * SymE_*: symbol is external to the RTS library. It might be linked
172 * Sym*_HasProto : the symbol prototype is imported in an include file
173 * or defined explicitly
174 * Sym*_NeedsProto: the symbol is undefined and we add a dummy
175 * default proto extern void sym(void);
177 #define X86_64_ELF_NONPIC_HACK 1
179 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
180 * small memory model on this architecture (see gcc docs,
183 * MAP_32BIT not available on OpenBSD/amd64
185 #if defined(x86_64_HOST_ARCH) && defined(MAP_32BIT)
186 #define TRY_MAP_32BIT MAP_32BIT
188 #define TRY_MAP_32BIT 0
192 * Due to the small memory model (see above), on x86_64 we have to map
193 * all our non-PIC object files into the low 2Gb of the address space
194 * (why 2Gb and not 4Gb? Because all addresses must be reachable
195 * using a 32-bit signed PC-relative offset). On Linux we can do this
196 * using the MAP_32BIT flag to mmap(), however on other OSs
197 * (e.g. *BSD, see #2063, and also on Linux inside Xen, see #2512), we
198 * can't do this. So on these systems, we have to pick a base address
199 * in the low 2Gb of the address space and try to allocate memory from
202 * We pick a default address based on the OS, but also make this
203 * configurable via an RTS flag (+RTS -xm)
205 #if defined(x86_64_HOST_ARCH)
207 #if defined(MAP_32BIT)
208 // Try to use MAP_32BIT
209 #define MMAP_32BIT_BASE_DEFAULT 0
212 #define MMAP_32BIT_BASE_DEFAULT 0x40000000
215 static void *mmap_32bit_base = (void *)MMAP_32BIT_BASE_DEFAULT;
218 /* MAP_ANONYMOUS is MAP_ANON on some systems, e.g. OpenBSD */
219 #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
220 #define MAP_ANONYMOUS MAP_ANON
223 /* -----------------------------------------------------------------------------
224 * Built-in symbols from the RTS
227 typedef struct _RtsSymbolVal {
232 #define Maybe_Stable_Names SymI_HasProto(stg_mkWeakzh) \
233 SymI_HasProto(stg_mkWeakForeignEnvzh) \
234 SymI_HasProto(stg_makeStableNamezh) \
235 SymI_HasProto(stg_finalizzeWeakzh)
237 #if !defined (mingw32_HOST_OS)
238 #define RTS_POSIX_ONLY_SYMBOLS \
239 SymI_HasProto(__hscore_get_saved_termios) \
240 SymI_HasProto(__hscore_set_saved_termios) \
241 SymI_HasProto(shutdownHaskellAndSignal) \
242 SymI_HasProto(lockFile) \
243 SymI_HasProto(unlockFile) \
244 SymI_HasProto(signal_handlers) \
245 SymI_HasProto(stg_sig_install) \
246 SymI_NeedsProto(nocldstop)
249 #if defined (cygwin32_HOST_OS)
250 #define RTS_MINGW_ONLY_SYMBOLS /**/
251 /* Don't have the ability to read import libs / archives, so
252 * we have to stupidly list a lot of what libcygwin.a
255 #define RTS_CYGWIN_ONLY_SYMBOLS \
256 SymI_HasProto(regfree) \
257 SymI_HasProto(regexec) \
258 SymI_HasProto(regerror) \
259 SymI_HasProto(regcomp) \
260 SymI_HasProto(__errno) \
261 SymI_HasProto(access) \
262 SymI_HasProto(chmod) \
263 SymI_HasProto(chdir) \
264 SymI_HasProto(close) \
265 SymI_HasProto(creat) \
267 SymI_HasProto(dup2) \
268 SymI_HasProto(fstat) \
269 SymI_HasProto(fcntl) \
270 SymI_HasProto(getcwd) \
271 SymI_HasProto(getenv) \
272 SymI_HasProto(lseek) \
273 SymI_HasProto(open) \
274 SymI_HasProto(fpathconf) \
275 SymI_HasProto(pathconf) \
276 SymI_HasProto(stat) \
278 SymI_HasProto(tanh) \
279 SymI_HasProto(cosh) \
280 SymI_HasProto(sinh) \
281 SymI_HasProto(atan) \
282 SymI_HasProto(acos) \
283 SymI_HasProto(asin) \
289 SymI_HasProto(sqrt) \
290 SymI_HasProto(localtime_r) \
291 SymI_HasProto(gmtime_r) \
292 SymI_HasProto(mktime) \
293 SymI_NeedsProto(_imp___tzname) \
294 SymI_HasProto(gettimeofday) \
295 SymI_HasProto(timezone) \
296 SymI_HasProto(tcgetattr) \
297 SymI_HasProto(tcsetattr) \
298 SymI_HasProto(memcpy) \
299 SymI_HasProto(memmove) \
300 SymI_HasProto(realloc) \
301 SymI_HasProto(malloc) \
302 SymI_HasProto(free) \
303 SymI_HasProto(fork) \
304 SymI_HasProto(lstat) \
305 SymI_HasProto(isatty) \
306 SymI_HasProto(mkdir) \
307 SymI_HasProto(opendir) \
308 SymI_HasProto(readdir) \
309 SymI_HasProto(rewinddir) \
310 SymI_HasProto(closedir) \
311 SymI_HasProto(link) \
312 SymI_HasProto(mkfifo) \
313 SymI_HasProto(pipe) \
314 SymI_HasProto(read) \
315 SymI_HasProto(rename) \
316 SymI_HasProto(rmdir) \
317 SymI_HasProto(select) \
318 SymI_HasProto(system) \
319 SymI_HasProto(write) \
320 SymI_HasProto(strcmp) \
321 SymI_HasProto(strcpy) \
322 SymI_HasProto(strncpy) \
323 SymI_HasProto(strerror) \
324 SymI_HasProto(sigaddset) \
325 SymI_HasProto(sigemptyset) \
326 SymI_HasProto(sigprocmask) \
327 SymI_HasProto(umask) \
328 SymI_HasProto(uname) \
329 SymI_HasProto(unlink) \
330 SymI_HasProto(utime) \
331 SymI_HasProto(waitpid)
333 #elif !defined(mingw32_HOST_OS)
334 #define RTS_MINGW_ONLY_SYMBOLS /**/
335 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
336 #else /* defined(mingw32_HOST_OS) */
337 #define RTS_POSIX_ONLY_SYMBOLS /**/
338 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
340 /* Extra syms gen'ed by mingw-2's gcc-3.2: */
342 #define RTS_MINGW_EXTRA_SYMS \
343 SymI_NeedsProto(_imp____mb_cur_max) \
344 SymI_NeedsProto(_imp___pctype)
346 #define RTS_MINGW_EXTRA_SYMS
349 #if HAVE_GETTIMEOFDAY
350 #define RTS_MINGW_GETTIMEOFDAY_SYM SymI_NeedsProto(gettimeofday)
352 #define RTS_MINGW_GETTIMEOFDAY_SYM /**/
355 #if HAVE___MINGW_VFPRINTF
356 #define RTS___MINGW_VFPRINTF_SYM SymI_HasProto(__mingw_vfprintf)
358 #define RTS___MINGW_VFPRINTF_SYM /**/
361 /* These are statically linked from the mingw libraries into the ghc
362 executable, so we have to employ this hack. */
363 #define RTS_MINGW_ONLY_SYMBOLS \
364 SymI_HasProto(stg_asyncReadzh) \
365 SymI_HasProto(stg_asyncWritezh) \
366 SymI_HasProto(stg_asyncDoProczh) \
367 SymI_HasProto(memset) \
368 SymI_HasProto(inet_ntoa) \
369 SymI_HasProto(inet_addr) \
370 SymI_HasProto(htonl) \
371 SymI_HasProto(recvfrom) \
372 SymI_HasProto(listen) \
373 SymI_HasProto(bind) \
374 SymI_HasProto(shutdown) \
375 SymI_HasProto(connect) \
376 SymI_HasProto(htons) \
377 SymI_HasProto(ntohs) \
378 SymI_HasProto(getservbyname) \
379 SymI_HasProto(getservbyport) \
380 SymI_HasProto(getprotobynumber) \
381 SymI_HasProto(getprotobyname) \
382 SymI_HasProto(gethostbyname) \
383 SymI_HasProto(gethostbyaddr) \
384 SymI_HasProto(gethostname) \
385 SymI_HasProto(strcpy) \
386 SymI_HasProto(strncpy) \
387 SymI_HasProto(abort) \
388 SymI_NeedsProto(_alloca) \
389 SymI_NeedsProto(isxdigit) \
390 SymI_NeedsProto(isupper) \
391 SymI_NeedsProto(ispunct) \
392 SymI_NeedsProto(islower) \
393 SymI_NeedsProto(isspace) \
394 SymI_NeedsProto(isprint) \
395 SymI_NeedsProto(isdigit) \
396 SymI_NeedsProto(iscntrl) \
397 SymI_NeedsProto(isalpha) \
398 SymI_NeedsProto(isalnum) \
399 SymI_NeedsProto(isascii) \
400 RTS___MINGW_VFPRINTF_SYM \
401 SymI_HasProto(strcmp) \
402 SymI_HasProto(memmove) \
403 SymI_HasProto(realloc) \
404 SymI_HasProto(malloc) \
406 SymI_HasProto(tanh) \
407 SymI_HasProto(cosh) \
408 SymI_HasProto(sinh) \
409 SymI_HasProto(atan) \
410 SymI_HasProto(acos) \
411 SymI_HasProto(asin) \
417 SymI_HasProto(sqrt) \
418 SymI_HasProto(powf) \
419 SymI_HasProto(tanhf) \
420 SymI_HasProto(coshf) \
421 SymI_HasProto(sinhf) \
422 SymI_HasProto(atanf) \
423 SymI_HasProto(acosf) \
424 SymI_HasProto(asinf) \
425 SymI_HasProto(tanf) \
426 SymI_HasProto(cosf) \
427 SymI_HasProto(sinf) \
428 SymI_HasProto(expf) \
429 SymI_HasProto(logf) \
430 SymI_HasProto(sqrtf) \
432 SymI_HasProto(erfc) \
433 SymI_HasProto(erff) \
434 SymI_HasProto(erfcf) \
435 SymI_HasProto(memcpy) \
436 SymI_HasProto(rts_InstallConsoleEvent) \
437 SymI_HasProto(rts_ConsoleHandlerDone) \
438 SymI_NeedsProto(mktime) \
439 SymI_NeedsProto(_imp___timezone) \
440 SymI_NeedsProto(_imp___tzname) \
441 SymI_NeedsProto(_imp__tzname) \
442 SymI_NeedsProto(_imp___iob) \
443 SymI_NeedsProto(_imp___osver) \
444 SymI_NeedsProto(localtime) \
445 SymI_NeedsProto(gmtime) \
446 SymI_NeedsProto(opendir) \
447 SymI_NeedsProto(readdir) \
448 SymI_NeedsProto(rewinddir) \
449 RTS_MINGW_EXTRA_SYMS \
450 RTS_MINGW_GETTIMEOFDAY_SYM \
451 SymI_NeedsProto(closedir)
454 #if defined(darwin_TARGET_OS) && HAVE_PRINTF_LDBLSTUB
455 #define RTS_DARWIN_ONLY_SYMBOLS \
456 SymI_NeedsProto(asprintf$LDBLStub) \
457 SymI_NeedsProto(err$LDBLStub) \
458 SymI_NeedsProto(errc$LDBLStub) \
459 SymI_NeedsProto(errx$LDBLStub) \
460 SymI_NeedsProto(fprintf$LDBLStub) \
461 SymI_NeedsProto(fscanf$LDBLStub) \
462 SymI_NeedsProto(fwprintf$LDBLStub) \
463 SymI_NeedsProto(fwscanf$LDBLStub) \
464 SymI_NeedsProto(printf$LDBLStub) \
465 SymI_NeedsProto(scanf$LDBLStub) \
466 SymI_NeedsProto(snprintf$LDBLStub) \
467 SymI_NeedsProto(sprintf$LDBLStub) \
468 SymI_NeedsProto(sscanf$LDBLStub) \
469 SymI_NeedsProto(strtold$LDBLStub) \
470 SymI_NeedsProto(swprintf$LDBLStub) \
471 SymI_NeedsProto(swscanf$LDBLStub) \
472 SymI_NeedsProto(syslog$LDBLStub) \
473 SymI_NeedsProto(vasprintf$LDBLStub) \
474 SymI_NeedsProto(verr$LDBLStub) \
475 SymI_NeedsProto(verrc$LDBLStub) \
476 SymI_NeedsProto(verrx$LDBLStub) \
477 SymI_NeedsProto(vfprintf$LDBLStub) \
478 SymI_NeedsProto(vfscanf$LDBLStub) \
479 SymI_NeedsProto(vfwprintf$LDBLStub) \
480 SymI_NeedsProto(vfwscanf$LDBLStub) \
481 SymI_NeedsProto(vprintf$LDBLStub) \
482 SymI_NeedsProto(vscanf$LDBLStub) \
483 SymI_NeedsProto(vsnprintf$LDBLStub) \
484 SymI_NeedsProto(vsprintf$LDBLStub) \
485 SymI_NeedsProto(vsscanf$LDBLStub) \
486 SymI_NeedsProto(vswprintf$LDBLStub) \
487 SymI_NeedsProto(vswscanf$LDBLStub) \
488 SymI_NeedsProto(vsyslog$LDBLStub) \
489 SymI_NeedsProto(vwarn$LDBLStub) \
490 SymI_NeedsProto(vwarnc$LDBLStub) \
491 SymI_NeedsProto(vwarnx$LDBLStub) \
492 SymI_NeedsProto(vwprintf$LDBLStub) \
493 SymI_NeedsProto(vwscanf$LDBLStub) \
494 SymI_NeedsProto(warn$LDBLStub) \
495 SymI_NeedsProto(warnc$LDBLStub) \
496 SymI_NeedsProto(warnx$LDBLStub) \
497 SymI_NeedsProto(wcstold$LDBLStub) \
498 SymI_NeedsProto(wprintf$LDBLStub) \
499 SymI_NeedsProto(wscanf$LDBLStub)
501 #define RTS_DARWIN_ONLY_SYMBOLS
505 # define MAIN_CAP_SYM SymI_HasProto(MainCapability)
507 # define MAIN_CAP_SYM
510 #if !defined(mingw32_HOST_OS)
511 #define RTS_USER_SIGNALS_SYMBOLS \
512 SymI_HasProto(setIOManagerPipe) \
513 SymI_HasProto(ioManagerWakeup) \
514 SymI_HasProto(ioManagerSync) \
515 SymI_HasProto(blockUserSignals) \
516 SymI_HasProto(unblockUserSignals)
518 #define RTS_USER_SIGNALS_SYMBOLS \
519 SymI_HasProto(ioManagerWakeup) \
520 SymI_HasProto(sendIOManagerEvent) \
521 SymI_HasProto(readIOManagerEvent) \
522 SymI_HasProto(getIOManagerEvent) \
523 SymI_HasProto(console_handler)
526 #define RTS_LIBFFI_SYMBOLS \
527 SymE_NeedsProto(ffi_prep_cif) \
528 SymE_NeedsProto(ffi_call) \
529 SymE_NeedsProto(ffi_type_void) \
530 SymE_NeedsProto(ffi_type_float) \
531 SymE_NeedsProto(ffi_type_double) \
532 SymE_NeedsProto(ffi_type_sint64) \
533 SymE_NeedsProto(ffi_type_uint64) \
534 SymE_NeedsProto(ffi_type_sint32) \
535 SymE_NeedsProto(ffi_type_uint32) \
536 SymE_NeedsProto(ffi_type_sint16) \
537 SymE_NeedsProto(ffi_type_uint16) \
538 SymE_NeedsProto(ffi_type_sint8) \
539 SymE_NeedsProto(ffi_type_uint8) \
540 SymE_NeedsProto(ffi_type_pointer)
542 #ifdef TABLES_NEXT_TO_CODE
543 #define RTS_RET_SYMBOLS /* nothing */
545 #define RTS_RET_SYMBOLS \
546 SymI_HasProto(stg_enter_ret) \
547 SymI_HasProto(stg_gc_fun_ret) \
548 SymI_HasProto(stg_ap_v_ret) \
549 SymI_HasProto(stg_ap_f_ret) \
550 SymI_HasProto(stg_ap_d_ret) \
551 SymI_HasProto(stg_ap_l_ret) \
552 SymI_HasProto(stg_ap_n_ret) \
553 SymI_HasProto(stg_ap_p_ret) \
554 SymI_HasProto(stg_ap_pv_ret) \
555 SymI_HasProto(stg_ap_pp_ret) \
556 SymI_HasProto(stg_ap_ppv_ret) \
557 SymI_HasProto(stg_ap_ppp_ret) \
558 SymI_HasProto(stg_ap_pppv_ret) \
559 SymI_HasProto(stg_ap_pppp_ret) \
560 SymI_HasProto(stg_ap_ppppp_ret) \
561 SymI_HasProto(stg_ap_pppppp_ret)
564 /* Modules compiled with -ticky may mention ticky counters */
565 /* This list should marry up with the one in $(TOP)/includes/stg/Ticky.h */
566 #define RTS_TICKY_SYMBOLS \
567 SymI_NeedsProto(ticky_entry_ctrs) \
568 SymI_NeedsProto(top_ct) \
570 SymI_HasProto(ENT_VIA_NODE_ctr) \
571 SymI_HasProto(ENT_STATIC_THK_ctr) \
572 SymI_HasProto(ENT_DYN_THK_ctr) \
573 SymI_HasProto(ENT_STATIC_FUN_DIRECT_ctr) \
574 SymI_HasProto(ENT_DYN_FUN_DIRECT_ctr) \
575 SymI_HasProto(ENT_STATIC_CON_ctr) \
576 SymI_HasProto(ENT_DYN_CON_ctr) \
577 SymI_HasProto(ENT_STATIC_IND_ctr) \
578 SymI_HasProto(ENT_DYN_IND_ctr) \
579 SymI_HasProto(ENT_PERM_IND_ctr) \
580 SymI_HasProto(ENT_PAP_ctr) \
581 SymI_HasProto(ENT_AP_ctr) \
582 SymI_HasProto(ENT_AP_STACK_ctr) \
583 SymI_HasProto(ENT_BH_ctr) \
584 SymI_HasProto(UNKNOWN_CALL_ctr) \
585 SymI_HasProto(SLOW_CALL_v_ctr) \
586 SymI_HasProto(SLOW_CALL_f_ctr) \
587 SymI_HasProto(SLOW_CALL_d_ctr) \
588 SymI_HasProto(SLOW_CALL_l_ctr) \
589 SymI_HasProto(SLOW_CALL_n_ctr) \
590 SymI_HasProto(SLOW_CALL_p_ctr) \
591 SymI_HasProto(SLOW_CALL_pv_ctr) \
592 SymI_HasProto(SLOW_CALL_pp_ctr) \
593 SymI_HasProto(SLOW_CALL_ppv_ctr) \
594 SymI_HasProto(SLOW_CALL_ppp_ctr) \
595 SymI_HasProto(SLOW_CALL_pppv_ctr) \
596 SymI_HasProto(SLOW_CALL_pppp_ctr) \
597 SymI_HasProto(SLOW_CALL_ppppp_ctr) \
598 SymI_HasProto(SLOW_CALL_pppppp_ctr) \
599 SymI_HasProto(SLOW_CALL_OTHER_ctr) \
600 SymI_HasProto(ticky_slow_call_unevald) \
601 SymI_HasProto(SLOW_CALL_ctr) \
602 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_ctr) \
603 SymI_HasProto(MULTI_CHUNK_SLOW_CALL_CHUNKS_ctr) \
604 SymI_HasProto(KNOWN_CALL_ctr) \
605 SymI_HasProto(KNOWN_CALL_TOO_FEW_ARGS_ctr) \
606 SymI_HasProto(KNOWN_CALL_EXTRA_ARGS_ctr) \
607 SymI_HasProto(SLOW_CALL_FUN_TOO_FEW_ctr) \
608 SymI_HasProto(SLOW_CALL_FUN_CORRECT_ctr) \
609 SymI_HasProto(SLOW_CALL_FUN_TOO_MANY_ctr) \
610 SymI_HasProto(SLOW_CALL_PAP_TOO_FEW_ctr) \
611 SymI_HasProto(SLOW_CALL_PAP_CORRECT_ctr) \
612 SymI_HasProto(SLOW_CALL_PAP_TOO_MANY_ctr) \
613 SymI_HasProto(SLOW_CALL_UNEVALD_ctr) \
614 SymI_HasProto(UPDF_OMITTED_ctr) \
615 SymI_HasProto(UPDF_PUSHED_ctr) \
616 SymI_HasProto(CATCHF_PUSHED_ctr) \
617 SymI_HasProto(UPDF_RCC_PUSHED_ctr) \
618 SymI_HasProto(UPDF_RCC_OMITTED_ctr) \
619 SymI_HasProto(UPD_SQUEEZED_ctr) \
620 SymI_HasProto(UPD_CON_IN_NEW_ctr) \
621 SymI_HasProto(UPD_CON_IN_PLACE_ctr) \
622 SymI_HasProto(UPD_PAP_IN_NEW_ctr) \
623 SymI_HasProto(UPD_PAP_IN_PLACE_ctr) \
624 SymI_HasProto(ALLOC_HEAP_ctr) \
625 SymI_HasProto(ALLOC_HEAP_tot) \
626 SymI_HasProto(ALLOC_FUN_ctr) \
627 SymI_HasProto(ALLOC_FUN_adm) \
628 SymI_HasProto(ALLOC_FUN_gds) \
629 SymI_HasProto(ALLOC_FUN_slp) \
630 SymI_HasProto(UPD_NEW_IND_ctr) \
631 SymI_HasProto(UPD_NEW_PERM_IND_ctr) \
632 SymI_HasProto(UPD_OLD_IND_ctr) \
633 SymI_HasProto(UPD_OLD_PERM_IND_ctr) \
634 SymI_HasProto(UPD_BH_UPDATABLE_ctr) \
635 SymI_HasProto(UPD_BH_SINGLE_ENTRY_ctr) \
636 SymI_HasProto(UPD_CAF_BH_UPDATABLE_ctr) \
637 SymI_HasProto(UPD_CAF_BH_SINGLE_ENTRY_ctr) \
638 SymI_HasProto(GC_SEL_ABANDONED_ctr) \
639 SymI_HasProto(GC_SEL_MINOR_ctr) \
640 SymI_HasProto(GC_SEL_MAJOR_ctr) \
641 SymI_HasProto(GC_FAILED_PROMOTION_ctr) \
642 SymI_HasProto(ALLOC_UP_THK_ctr) \
643 SymI_HasProto(ALLOC_SE_THK_ctr) \
644 SymI_HasProto(ALLOC_THK_adm) \
645 SymI_HasProto(ALLOC_THK_gds) \
646 SymI_HasProto(ALLOC_THK_slp) \
647 SymI_HasProto(ALLOC_CON_ctr) \
648 SymI_HasProto(ALLOC_CON_adm) \
649 SymI_HasProto(ALLOC_CON_gds) \
650 SymI_HasProto(ALLOC_CON_slp) \
651 SymI_HasProto(ALLOC_TUP_ctr) \
652 SymI_HasProto(ALLOC_TUP_adm) \
653 SymI_HasProto(ALLOC_TUP_gds) \
654 SymI_HasProto(ALLOC_TUP_slp) \
655 SymI_HasProto(ALLOC_BH_ctr) \
656 SymI_HasProto(ALLOC_BH_adm) \
657 SymI_HasProto(ALLOC_BH_gds) \
658 SymI_HasProto(ALLOC_BH_slp) \
659 SymI_HasProto(ALLOC_PRIM_ctr) \
660 SymI_HasProto(ALLOC_PRIM_adm) \
661 SymI_HasProto(ALLOC_PRIM_gds) \
662 SymI_HasProto(ALLOC_PRIM_slp) \
663 SymI_HasProto(ALLOC_PAP_ctr) \
664 SymI_HasProto(ALLOC_PAP_adm) \
665 SymI_HasProto(ALLOC_PAP_gds) \
666 SymI_HasProto(ALLOC_PAP_slp) \
667 SymI_HasProto(ALLOC_TSO_ctr) \
668 SymI_HasProto(ALLOC_TSO_adm) \
669 SymI_HasProto(ALLOC_TSO_gds) \
670 SymI_HasProto(ALLOC_TSO_slp) \
671 SymI_HasProto(RET_NEW_ctr) \
672 SymI_HasProto(RET_OLD_ctr) \
673 SymI_HasProto(RET_UNBOXED_TUP_ctr) \
674 SymI_HasProto(RET_SEMI_loads_avoided)
677 // On most platforms, the garbage collector rewrites references
678 // to small integer and char objects to a set of common, shared ones.
680 // We don't do this when compiling to Windows DLLs at the moment because
681 // it doesn't support cross package data references well.
683 #if defined(__PIC__) && defined(mingw32_HOST_OS)
684 #define RTS_INTCHAR_SYMBOLS
686 #define RTS_INTCHAR_SYMBOLS \
687 SymI_HasProto(stg_CHARLIKE_closure) \
688 SymI_HasProto(stg_INTLIKE_closure)
692 #define RTS_SYMBOLS \
695 SymI_HasProto(StgReturn) \
696 SymI_HasProto(stg_enter_info) \
697 SymI_HasProto(stg_gc_void_info) \
698 SymI_HasProto(__stg_gc_enter_1) \
699 SymI_HasProto(stg_gc_noregs) \
700 SymI_HasProto(stg_gc_unpt_r1_info) \
701 SymI_HasProto(stg_gc_unpt_r1) \
702 SymI_HasProto(stg_gc_unbx_r1_info) \
703 SymI_HasProto(stg_gc_unbx_r1) \
704 SymI_HasProto(stg_gc_f1_info) \
705 SymI_HasProto(stg_gc_f1) \
706 SymI_HasProto(stg_gc_d1_info) \
707 SymI_HasProto(stg_gc_d1) \
708 SymI_HasProto(stg_gc_l1_info) \
709 SymI_HasProto(stg_gc_l1) \
710 SymI_HasProto(__stg_gc_fun) \
711 SymI_HasProto(stg_gc_fun_info) \
712 SymI_HasProto(stg_gc_gen) \
713 SymI_HasProto(stg_gc_gen_info) \
714 SymI_HasProto(stg_gc_gen_hp) \
715 SymI_HasProto(stg_gc_ut) \
716 SymI_HasProto(stg_gen_yield) \
717 SymI_HasProto(stg_yield_noregs) \
718 SymI_HasProto(stg_yield_to_interpreter) \
719 SymI_HasProto(stg_gen_block) \
720 SymI_HasProto(stg_block_noregs) \
721 SymI_HasProto(stg_block_1) \
722 SymI_HasProto(stg_block_takemvar) \
723 SymI_HasProto(stg_block_putmvar) \
725 SymI_HasProto(MallocFailHook) \
726 SymI_HasProto(OnExitHook) \
727 SymI_HasProto(OutOfHeapHook) \
728 SymI_HasProto(StackOverflowHook) \
729 SymI_HasProto(addDLL) \
730 SymI_HasProto(__int_encodeDouble) \
731 SymI_HasProto(__word_encodeDouble) \
732 SymI_HasProto(__2Int_encodeDouble) \
733 SymI_HasProto(__int_encodeFloat) \
734 SymI_HasProto(__word_encodeFloat) \
735 SymI_HasProto(stg_atomicallyzh) \
736 SymI_HasProto(barf) \
737 SymI_HasProto(debugBelch) \
738 SymI_HasProto(errorBelch) \
739 SymI_HasProto(sysErrorBelch) \
740 SymI_HasProto(stg_asyncExceptionsBlockedzh) \
741 SymI_HasProto(stg_blockAsyncExceptionszh) \
742 SymI_HasProto(stg_catchzh) \
743 SymI_HasProto(stg_catchRetryzh) \
744 SymI_HasProto(stg_catchSTMzh) \
745 SymI_HasProto(stg_checkzh) \
746 SymI_HasProto(closure_flags) \
747 SymI_HasProto(cmp_thread) \
748 SymI_HasProto(createAdjustor) \
749 SymI_HasProto(stg_decodeDoublezu2Intzh) \
750 SymI_HasProto(stg_decodeFloatzuIntzh) \
751 SymI_HasProto(defaultsHook) \
752 SymI_HasProto(stg_delayzh) \
753 SymI_HasProto(stg_deRefWeakzh) \
754 SymI_HasProto(stg_deRefStablePtrzh) \
755 SymI_HasProto(dirty_MUT_VAR) \
756 SymI_HasProto(stg_forkzh) \
757 SymI_HasProto(stg_forkOnzh) \
758 SymI_HasProto(forkProcess) \
759 SymI_HasProto(forkOS_createThread) \
760 SymI_HasProto(freeHaskellFunctionPtr) \
761 SymI_HasProto(getOrSetTypeableStore) \
762 SymI_HasProto(getOrSetGHCConcSignalHandlerStore) \
763 SymI_HasProto(getOrSetGHCConcPendingEventsStore) \
764 SymI_HasProto(getOrSetGHCConcPendingDelaysStore) \
765 SymI_HasProto(getOrSetGHCConcIOManagerThreadStore) \
766 SymI_HasProto(getOrSetGHCConcProddingStore) \
767 SymI_HasProto(genSymZh) \
768 SymI_HasProto(genericRaise) \
769 SymI_HasProto(getProgArgv) \
770 SymI_HasProto(getFullProgArgv) \
771 SymI_HasProto(getStablePtr) \
772 SymI_HasProto(hs_init) \
773 SymI_HasProto(hs_exit) \
774 SymI_HasProto(hs_set_argv) \
775 SymI_HasProto(hs_add_root) \
776 SymI_HasProto(hs_perform_gc) \
777 SymI_HasProto(hs_free_stable_ptr) \
778 SymI_HasProto(hs_free_fun_ptr) \
779 SymI_HasProto(hs_hpc_rootModule) \
780 SymI_HasProto(hs_hpc_module) \
781 SymI_HasProto(initLinker) \
782 SymI_HasProto(stg_unpackClosurezh) \
783 SymI_HasProto(stg_getApStackValzh) \
784 SymI_HasProto(stg_getSparkzh) \
785 SymI_HasProto(stg_isCurrentThreadBoundzh) \
786 SymI_HasProto(stg_isEmptyMVarzh) \
787 SymI_HasProto(stg_killThreadzh) \
788 SymI_HasProto(loadObj) \
789 SymI_HasProto(insertStableSymbol) \
790 SymI_HasProto(insertSymbol) \
791 SymI_HasProto(lookupSymbol) \
792 SymI_HasProto(stg_makeStablePtrzh) \
793 SymI_HasProto(stg_mkApUpd0zh) \
794 SymI_HasProto(stg_myThreadIdzh) \
795 SymI_HasProto(stg_labelThreadzh) \
796 SymI_HasProto(stg_newArrayzh) \
797 SymI_HasProto(stg_newBCOzh) \
798 SymI_HasProto(stg_newByteArrayzh) \
799 SymI_HasProto_redirect(newCAF, newDynCAF) \
800 SymI_HasProto(stg_newMVarzh) \
801 SymI_HasProto(stg_newMutVarzh) \
802 SymI_HasProto(stg_newTVarzh) \
803 SymI_HasProto(stg_noDuplicatezh) \
804 SymI_HasProto(stg_atomicModifyMutVarzh) \
805 SymI_HasProto(stg_newPinnedByteArrayzh) \
806 SymI_HasProto(stg_newAlignedPinnedByteArrayzh) \
807 SymI_HasProto(newSpark) \
808 SymI_HasProto(performGC) \
809 SymI_HasProto(performMajorGC) \
810 SymI_HasProto(prog_argc) \
811 SymI_HasProto(prog_argv) \
812 SymI_HasProto(stg_putMVarzh) \
813 SymI_HasProto(stg_raisezh) \
814 SymI_HasProto(stg_raiseIOzh) \
815 SymI_HasProto(stg_readTVarzh) \
816 SymI_HasProto(stg_readTVarIOzh) \
817 SymI_HasProto(resumeThread) \
818 SymI_HasProto(resolveObjs) \
819 SymI_HasProto(stg_retryzh) \
820 SymI_HasProto(rts_apply) \
821 SymI_HasProto(rts_checkSchedStatus) \
822 SymI_HasProto(rts_eval) \
823 SymI_HasProto(rts_evalIO) \
824 SymI_HasProto(rts_evalLazyIO) \
825 SymI_HasProto(rts_evalStableIO) \
826 SymI_HasProto(rts_eval_) \
827 SymI_HasProto(rts_getBool) \
828 SymI_HasProto(rts_getChar) \
829 SymI_HasProto(rts_getDouble) \
830 SymI_HasProto(rts_getFloat) \
831 SymI_HasProto(rts_getInt) \
832 SymI_HasProto(rts_getInt8) \
833 SymI_HasProto(rts_getInt16) \
834 SymI_HasProto(rts_getInt32) \
835 SymI_HasProto(rts_getInt64) \
836 SymI_HasProto(rts_getPtr) \
837 SymI_HasProto(rts_getFunPtr) \
838 SymI_HasProto(rts_getStablePtr) \
839 SymI_HasProto(rts_getThreadId) \
840 SymI_HasProto(rts_getWord) \
841 SymI_HasProto(rts_getWord8) \
842 SymI_HasProto(rts_getWord16) \
843 SymI_HasProto(rts_getWord32) \
844 SymI_HasProto(rts_getWord64) \
845 SymI_HasProto(rts_lock) \
846 SymI_HasProto(rts_mkBool) \
847 SymI_HasProto(rts_mkChar) \
848 SymI_HasProto(rts_mkDouble) \
849 SymI_HasProto(rts_mkFloat) \
850 SymI_HasProto(rts_mkInt) \
851 SymI_HasProto(rts_mkInt8) \
852 SymI_HasProto(rts_mkInt16) \
853 SymI_HasProto(rts_mkInt32) \
854 SymI_HasProto(rts_mkInt64) \
855 SymI_HasProto(rts_mkPtr) \
856 SymI_HasProto(rts_mkFunPtr) \
857 SymI_HasProto(rts_mkStablePtr) \
858 SymI_HasProto(rts_mkString) \
859 SymI_HasProto(rts_mkWord) \
860 SymI_HasProto(rts_mkWord8) \
861 SymI_HasProto(rts_mkWord16) \
862 SymI_HasProto(rts_mkWord32) \
863 SymI_HasProto(rts_mkWord64) \
864 SymI_HasProto(rts_unlock) \
865 SymI_HasProto(rts_unsafeGetMyCapability) \
866 SymI_HasProto(rtsSupportsBoundThreads) \
867 SymI_HasProto(setProgArgv) \
868 SymI_HasProto(startupHaskell) \
869 SymI_HasProto(shutdownHaskell) \
870 SymI_HasProto(shutdownHaskellAndExit) \
871 SymI_HasProto(stable_ptr_table) \
872 SymI_HasProto(stackOverflow) \
873 SymI_HasProto(stg_CAF_BLACKHOLE_info) \
874 SymI_HasProto(__stg_EAGER_BLACKHOLE_info) \
875 SymI_HasProto(startTimer) \
876 SymI_HasProto(stg_MVAR_CLEAN_info) \
877 SymI_HasProto(stg_MVAR_DIRTY_info) \
878 SymI_HasProto(stg_IND_STATIC_info) \
879 SymI_HasProto(stg_ARR_WORDS_info) \
880 SymI_HasProto(stg_MUT_ARR_PTRS_DIRTY_info) \
881 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN_info) \
882 SymI_HasProto(stg_MUT_ARR_PTRS_FROZEN0_info) \
883 SymI_HasProto(stg_WEAK_info) \
884 SymI_HasProto(stg_ap_v_info) \
885 SymI_HasProto(stg_ap_f_info) \
886 SymI_HasProto(stg_ap_d_info) \
887 SymI_HasProto(stg_ap_l_info) \
888 SymI_HasProto(stg_ap_n_info) \
889 SymI_HasProto(stg_ap_p_info) \
890 SymI_HasProto(stg_ap_pv_info) \
891 SymI_HasProto(stg_ap_pp_info) \
892 SymI_HasProto(stg_ap_ppv_info) \
893 SymI_HasProto(stg_ap_ppp_info) \
894 SymI_HasProto(stg_ap_pppv_info) \
895 SymI_HasProto(stg_ap_pppp_info) \
896 SymI_HasProto(stg_ap_ppppp_info) \
897 SymI_HasProto(stg_ap_pppppp_info) \
898 SymI_HasProto(stg_ap_0_fast) \
899 SymI_HasProto(stg_ap_v_fast) \
900 SymI_HasProto(stg_ap_f_fast) \
901 SymI_HasProto(stg_ap_d_fast) \
902 SymI_HasProto(stg_ap_l_fast) \
903 SymI_HasProto(stg_ap_n_fast) \
904 SymI_HasProto(stg_ap_p_fast) \
905 SymI_HasProto(stg_ap_pv_fast) \
906 SymI_HasProto(stg_ap_pp_fast) \
907 SymI_HasProto(stg_ap_ppv_fast) \
908 SymI_HasProto(stg_ap_ppp_fast) \
909 SymI_HasProto(stg_ap_pppv_fast) \
910 SymI_HasProto(stg_ap_pppp_fast) \
911 SymI_HasProto(stg_ap_ppppp_fast) \
912 SymI_HasProto(stg_ap_pppppp_fast) \
913 SymI_HasProto(stg_ap_1_upd_info) \
914 SymI_HasProto(stg_ap_2_upd_info) \
915 SymI_HasProto(stg_ap_3_upd_info) \
916 SymI_HasProto(stg_ap_4_upd_info) \
917 SymI_HasProto(stg_ap_5_upd_info) \
918 SymI_HasProto(stg_ap_6_upd_info) \
919 SymI_HasProto(stg_ap_7_upd_info) \
920 SymI_HasProto(stg_exit) \
921 SymI_HasProto(stg_sel_0_upd_info) \
922 SymI_HasProto(stg_sel_10_upd_info) \
923 SymI_HasProto(stg_sel_11_upd_info) \
924 SymI_HasProto(stg_sel_12_upd_info) \
925 SymI_HasProto(stg_sel_13_upd_info) \
926 SymI_HasProto(stg_sel_14_upd_info) \
927 SymI_HasProto(stg_sel_15_upd_info) \
928 SymI_HasProto(stg_sel_1_upd_info) \
929 SymI_HasProto(stg_sel_2_upd_info) \
930 SymI_HasProto(stg_sel_3_upd_info) \
931 SymI_HasProto(stg_sel_4_upd_info) \
932 SymI_HasProto(stg_sel_5_upd_info) \
933 SymI_HasProto(stg_sel_6_upd_info) \
934 SymI_HasProto(stg_sel_7_upd_info) \
935 SymI_HasProto(stg_sel_8_upd_info) \
936 SymI_HasProto(stg_sel_9_upd_info) \
937 SymI_HasProto(stg_upd_frame_info) \
938 SymI_HasProto(suspendThread) \
939 SymI_HasProto(stg_takeMVarzh) \
940 SymI_HasProto(stg_threadStatuszh) \
941 SymI_HasProto(stg_tryPutMVarzh) \
942 SymI_HasProto(stg_tryTakeMVarzh) \
943 SymI_HasProto(stg_unblockAsyncExceptionszh) \
944 SymI_HasProto(unloadObj) \
945 SymI_HasProto(stg_unsafeThawArrayzh) \
946 SymI_HasProto(stg_waitReadzh) \
947 SymI_HasProto(stg_waitWritezh) \
948 SymI_HasProto(stg_writeTVarzh) \
949 SymI_HasProto(stg_yieldzh) \
950 SymI_NeedsProto(stg_interp_constr_entry) \
951 SymI_HasProto(alloc_blocks_lim) \
953 SymI_HasProto(allocate) \
954 SymI_HasProto(allocateExec) \
955 SymI_HasProto(freeExec) \
956 SymI_HasProto(getAllocations) \
957 SymI_HasProto(revertCAFs) \
958 SymI_HasProto(RtsFlags) \
959 SymI_NeedsProto(rts_breakpoint_io_action) \
960 SymI_NeedsProto(rts_stop_next_breakpoint) \
961 SymI_NeedsProto(rts_stop_on_exception) \
962 SymI_HasProto(stopTimer) \
963 SymI_HasProto(n_capabilities) \
964 SymI_HasProto(stg_traceCcszh) \
965 SymI_HasProto(stg_traceEventzh) \
966 RTS_USER_SIGNALS_SYMBOLS \
970 // 64-bit support functions in libgcc.a
971 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
972 #define RTS_LIBGCC_SYMBOLS \
973 SymI_NeedsProto(__divdi3) \
974 SymI_NeedsProto(__udivdi3) \
975 SymI_NeedsProto(__moddi3) \
976 SymI_NeedsProto(__umoddi3) \
977 SymI_NeedsProto(__muldi3) \
978 SymI_NeedsProto(__ashldi3) \
979 SymI_NeedsProto(__ashrdi3) \
980 SymI_NeedsProto(__lshrdi3) \
981 SymI_NeedsProto(__eprintf)
983 #define RTS_LIBGCC_SYMBOLS
986 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
987 // Symbols that don't have a leading underscore
988 // on Mac OS X. They have to receive special treatment,
989 // see machoInitSymbolsWithoutUnderscore()
990 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
991 SymI_NeedsProto(saveFP) \
992 SymI_NeedsProto(restFP)
995 /* entirely bogus claims about types of these symbols */
996 #define SymI_NeedsProto(vvv) extern void vvv(void);
997 #if defined(__PIC__) && defined(mingw32_TARGET_OS)
998 #define SymE_HasProto(vvv) SymE_HasProto(vvv);
999 #define SymE_NeedsProto(vvv) extern void _imp__ ## vvv (void);
1001 #define SymE_NeedsProto(vvv) SymI_NeedsProto(vvv);
1002 #define SymE_HasProto(vvv) SymI_HasProto(vvv)
1004 #define SymI_HasProto(vvv) /**/
1005 #define SymI_HasProto_redirect(vvv,xxx) /**/
1008 RTS_POSIX_ONLY_SYMBOLS
1009 RTS_MINGW_ONLY_SYMBOLS
1010 RTS_CYGWIN_ONLY_SYMBOLS
1011 RTS_DARWIN_ONLY_SYMBOLS
1014 #undef SymI_NeedsProto
1015 #undef SymI_HasProto
1016 #undef SymI_HasProto_redirect
1017 #undef SymE_HasProto
1018 #undef SymE_NeedsProto
1020 #ifdef LEADING_UNDERSCORE
1021 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
1023 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
1026 #define SymI_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1028 #define SymE_HasProto(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1029 (void*)DLL_IMPORT_DATA_REF(vvv) },
1031 #define SymI_NeedsProto(vvv) SymI_HasProto(vvv)
1032 #define SymE_NeedsProto(vvv) SymE_HasProto(vvv)
1034 // SymI_HasProto_redirect allows us to redirect references to one symbol to
1035 // another symbol. See newCAF/newDynCAF for an example.
1036 #define SymI_HasProto_redirect(vvv,xxx) \
1037 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
1040 static RtsSymbolVal rtsSyms[] = {
1043 RTS_POSIX_ONLY_SYMBOLS
1044 RTS_MINGW_ONLY_SYMBOLS
1045 RTS_CYGWIN_ONLY_SYMBOLS
1046 RTS_DARWIN_ONLY_SYMBOLS
1049 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
1050 // dyld stub code contains references to this,
1051 // but it should never be called because we treat
1052 // lazy pointers as nonlazy.
1053 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
1055 { 0, 0 } /* sentinel */
1060 /* -----------------------------------------------------------------------------
1061 * Insert symbols into hash tables, checking for duplicates.
1064 static void ghciInsertStrHashTable ( char* obj_name,
1070 if (lookupHashTable(table, (StgWord)key) == NULL)
1072 insertStrHashTable(table, (StgWord)key, data);
1077 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
1079 "whilst processing object file\n"
1081 "This could be caused by:\n"
1082 " * Loading two different object files which export the same symbol\n"
1083 " * Specifying the same object file twice on the GHCi command line\n"
1084 " * An incorrect `package.conf' entry, causing some object to be\n"
1086 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
1093 /* -----------------------------------------------------------------------------
1094 * initialize the object linker
1098 static int linker_init_done = 0 ;
1100 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1101 static void *dl_prog_handle;
1102 static regex_t re_invalid;
1103 static regex_t re_realso;
1104 static void initLinkerCleanup( void );
1106 static Mutex dl_mutex; // mutex to protect dlopen/dlerror critical section
1116 /* Make initLinker idempotent, so we can call it
1117 before evey relevant operation; that means we
1118 don't need to initialise the linker separately */
1119 if (linker_init_done == 1) { return; } else {
1120 linker_init_done = 1;
1124 initMutex(&dl_mutex);
1126 stablehash = allocStrHashTable();
1127 symhash = allocStrHashTable();
1129 /* populate the symbol table with stuff from the RTS */
1130 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
1131 ghciInsertStrHashTable("(GHCi built-in symbols)",
1132 symhash, sym->lbl, sym->addr);
1134 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1135 machoInitSymbolsWithoutUnderscore();
1138 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1139 # if defined(RTLD_DEFAULT)
1140 dl_prog_handle = RTLD_DEFAULT;
1142 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
1143 # endif /* RTLD_DEFAULT */
1145 compileResult = regcomp(&re_invalid,
1146 "(([^ \t()])+\\.so([^ \t:()])*):([ \t])*invalid ELF header",
1148 ASSERT( compileResult == 0 );
1149 compileResult = regcomp(&re_realso,
1150 "GROUP *\\( *(([^ )])+)",
1152 ASSERT( compileResult == 0 );
1155 #if defined(x86_64_HOST_ARCH)
1156 if (RtsFlags.MiscFlags.linkerMemBase != 0) {
1157 // User-override for mmap_32bit_base
1158 mmap_32bit_base = (void*)RtsFlags.MiscFlags.linkerMemBase;
1162 #if defined(mingw32_HOST_OS)
1164 * These two libraries cause problems when added to the static link,
1165 * but are necessary for resolving symbols in GHCi, hence we load
1166 * them manually here.
1174 exitLinker( void ) {
1175 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1176 if (linker_init_done == 1) {
1177 regfree(&re_invalid);
1178 regfree(&re_realso);
1180 closeMutex(&dl_mutex);
1186 /* -----------------------------------------------------------------------------
1187 * Loading DLL or .so dynamic libraries
1188 * -----------------------------------------------------------------------------
1190 * Add a DLL from which symbols may be found. In the ELF case, just
1191 * do RTLD_GLOBAL-style add, so no further messing around needs to
1192 * happen in order that symbols in the loaded .so are findable --
1193 * lookupSymbol() will subsequently see them by dlsym on the program's
1194 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
1196 * In the PEi386 case, open the DLLs and put handles to them in a
1197 * linked list. When looking for a symbol, try all handles in the
1198 * list. This means that we need to load even DLLs that are guaranteed
1199 * to be in the ghc.exe image already, just so we can get a handle
1200 * to give to loadSymbol, so that we can find the symbols. For such
1201 * libraries, the LoadLibrary call should be a no-op except for returning
1206 #if defined(OBJFORMAT_PEi386)
1207 /* A record for storing handles into DLLs. */
1212 struct _OpenedDLL* next;
1217 /* A list thereof. */
1218 static OpenedDLL* opened_dlls = NULL;
1221 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1224 internal_dlopen(const char *dll_name)
1227 char *errmsg, *errmsg_copy;
1229 // omitted: RTLD_NOW
1230 // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html
1232 debugBelch("internal_dlopen: dll_name = '%s'\n", dll_name));
1234 //-------------- Begin critical section ------------------
1235 // This critical section is necessary because dlerror() is not
1236 // required to be reentrant (see POSIX -- IEEE Std 1003.1-2008)
1237 // Also, the error message returned must be copied to preserve it
1240 ACQUIRE_LOCK(&dl_mutex);
1241 hdl = dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL);
1245 /* dlopen failed; return a ptr to the error msg. */
1247 if (errmsg == NULL) errmsg = "addDLL: unknown error";
1248 errmsg_copy = stgMallocBytes(strlen(errmsg)+1, "addDLL");
1249 strcpy(errmsg_copy, errmsg);
1250 errmsg = errmsg_copy;
1252 RELEASE_LOCK(&dl_mutex);
1253 //--------------- End critical section -------------------
1260 addDLL( char *dll_name )
1262 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
1263 /* ------------------- ELF DLL loader ------------------- */
1266 regmatch_t match[NMATCH];
1269 size_t match_length;
1270 #define MAXLINE 1000
1276 IF_DEBUG(linker, debugBelch("addDLL: dll_name = '%s'\n", dll_name));
1277 errmsg = internal_dlopen(dll_name);
1279 if (errmsg == NULL) {
1283 // GHC Trac ticket #2615
1284 // On some systems (e.g., Gentoo Linux) dynamic files (e.g. libc.so)
1285 // contain linker scripts rather than ELF-format object code. This
1286 // code handles the situation by recognizing the real object code
1287 // file name given in the linker script.
1289 // If an "invalid ELF header" error occurs, it is assumed that the
1290 // .so file contains a linker script instead of ELF object code.
1291 // In this case, the code looks for the GROUP ( ... ) linker
1292 // directive. If one is found, the first file name inside the
1293 // parentheses is treated as the name of a dynamic library and the
1294 // code attempts to dlopen that file. If this is also unsuccessful,
1295 // an error message is returned.
1297 // see if the error message is due to an invalid ELF header
1298 IF_DEBUG(linker, debugBelch("errmsg = '%s'\n", errmsg));
1299 result = regexec(&re_invalid, errmsg, (size_t) NMATCH, match, 0);
1300 IF_DEBUG(linker, debugBelch("result = %i\n", result));
1302 // success -- try to read the named file as a linker script
1303 match_length = (size_t) stg_min((match[1].rm_eo - match[1].rm_so),
1305 strncpy(line, (errmsg+(match[1].rm_so)),match_length);
1306 line[match_length] = '\0'; // make sure string is null-terminated
1307 IF_DEBUG(linker, debugBelch ("file name = '%s'\n", line));
1308 if ((fp = fopen(line, "r")) == NULL) {
1309 return errmsg; // return original error if open fails
1311 // try to find a GROUP ( ... ) command
1312 while (fgets(line, MAXLINE, fp) != NULL) {
1313 IF_DEBUG(linker, debugBelch("input line = %s", line));
1314 if (regexec(&re_realso, line, (size_t) NMATCH, match, 0) == 0) {
1315 // success -- try to dlopen the first named file
1316 IF_DEBUG(linker, debugBelch("match%s\n",""));
1317 line[match[1].rm_eo] = '\0';
1318 errmsg = internal_dlopen(line+match[1].rm_so);
1321 // if control reaches here, no GROUP ( ... ) directive was found
1322 // and the original error message is returned to the caller
1328 # elif defined(OBJFORMAT_PEi386)
1329 /* ------------------- Win32 DLL loader ------------------- */
1337 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
1339 /* See if we've already got it, and ignore if so. */
1340 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1341 if (0 == strcmp(o_dll->name, dll_name))
1345 /* The file name has no suffix (yet) so that we can try
1346 both foo.dll and foo.drv
1348 The documentation for LoadLibrary says:
1349 If no file name extension is specified in the lpFileName
1350 parameter, the default library extension .dll is
1351 appended. However, the file name string can include a trailing
1352 point character (.) to indicate that the module name has no
1355 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
1356 sprintf(buf, "%s.DLL", dll_name);
1357 instance = LoadLibrary(buf);
1358 if (instance == NULL) {
1359 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1360 // KAA: allow loading of drivers (like winspool.drv)
1361 sprintf(buf, "%s.DRV", dll_name);
1362 instance = LoadLibrary(buf);
1363 if (instance == NULL) {
1364 if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error;
1365 // #1883: allow loading of unix-style libfoo.dll DLLs
1366 sprintf(buf, "lib%s.DLL", dll_name);
1367 instance = LoadLibrary(buf);
1368 if (instance == NULL) {
1375 /* Add this DLL to the list of DLLs in which to search for symbols. */
1376 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
1377 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
1378 strcpy(o_dll->name, dll_name);
1379 o_dll->instance = instance;
1380 o_dll->next = opened_dlls;
1381 opened_dlls = o_dll;
1387 sysErrorBelch(dll_name);
1389 /* LoadLibrary failed; return a ptr to the error msg. */
1390 return "addDLL: could not load DLL";
1393 barf("addDLL: not implemented on this platform");
1397 /* -----------------------------------------------------------------------------
1398 * insert a stable symbol in the hash table
1402 insertStableSymbol(char* obj_name, char* key, StgPtr p)
1404 ghciInsertStrHashTable(obj_name, stablehash, key, getStablePtr(p));
1408 /* -----------------------------------------------------------------------------
1409 * insert a symbol in the hash table
1412 insertSymbol(char* obj_name, char* key, void* data)
1414 ghciInsertStrHashTable(obj_name, symhash, key, data);
1417 /* -----------------------------------------------------------------------------
1418 * lookup a symbol in the hash table
1421 lookupSymbol( char *lbl )
1425 ASSERT(symhash != NULL);
1426 val = lookupStrHashTable(symhash, lbl);
1429 # if defined(OBJFORMAT_ELF)
1430 return dlsym(dl_prog_handle, lbl);
1431 # elif defined(OBJFORMAT_MACHO)
1433 /* On OS X 10.3 and later, we use dlsym instead of the old legacy
1436 HACK: On OS X, global symbols are prefixed with an underscore.
1437 However, dlsym wants us to omit the leading underscore from the
1438 symbol name. For now, we simply strip it off here (and ONLY
1441 ASSERT(lbl[0] == '_');
1442 return dlsym(dl_prog_handle, lbl+1);
1444 if(NSIsSymbolNameDefined(lbl)) {
1445 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
1446 return NSAddressOfSymbol(symbol);
1450 # endif /* HAVE_DLFCN_H */
1451 # elif defined(OBJFORMAT_PEi386)
1454 sym = lookupSymbolInDLLs(lbl);
1455 if (sym != NULL) { return sym; };
1457 // Also try looking up the symbol without the @N suffix. Some
1458 // DLLs have the suffixes on their symbols, some don't.
1459 zapTrailingAtSign ( lbl );
1460 sym = lookupSymbolInDLLs(lbl);
1461 if (sym != NULL) { return sym; };
1473 /* -----------------------------------------------------------------------------
1474 * Debugging aid: look in GHCi's object symbol tables for symbols
1475 * within DELTA bytes of the specified address, and show their names.
1478 void ghci_enquire ( char* addr );
1480 void ghci_enquire ( char* addr )
1485 const int DELTA = 64;
1490 for (oc = objects; oc; oc = oc->next) {
1491 for (i = 0; i < oc->n_symbols; i++) {
1492 sym = oc->symbols[i];
1493 if (sym == NULL) continue;
1496 a = lookupStrHashTable(symhash, sym);
1499 // debugBelch("ghci_enquire: can't find %s\n", sym);
1501 else if (addr-DELTA <= a && a <= addr+DELTA) {
1502 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1510 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1513 mmapForLinker (size_t bytes, nat flags, int fd)
1515 void *map_addr = NULL;
1518 static nat fixed = 0;
1520 pagesize = getpagesize();
1521 size = ROUND_UP(bytes, pagesize);
1523 #if defined(x86_64_HOST_ARCH)
1526 if (mmap_32bit_base != 0) {
1527 map_addr = mmap_32bit_base;
1531 result = mmap(map_addr, size, PROT_EXEC|PROT_READ|PROT_WRITE,
1532 MAP_PRIVATE|TRY_MAP_32BIT|fixed|flags, fd, 0);
1534 if (result == MAP_FAILED) {
1535 sysErrorBelch("mmap %lu bytes at %p",(lnat)size,map_addr);
1536 errorBelch("Try specifying an address with +RTS -xm<addr> -RTS");
1537 stg_exit(EXIT_FAILURE);
1540 #if defined(x86_64_HOST_ARCH)
1541 if (mmap_32bit_base != 0) {
1542 if (result == map_addr) {
1543 mmap_32bit_base = (StgWord8*)map_addr + size;
1545 if ((W_)result > 0x80000000) {
1546 // oops, we were given memory over 2Gb
1547 #if defined(freebsd_HOST_OS) || defined(dragonfly_HOST_OS)
1548 // Some platforms require MAP_FIXED. This is normally
1549 // a bad idea, because MAP_FIXED will overwrite
1550 // existing mappings.
1551 munmap(result,size);
1555 barf("loadObj: failed to mmap() memory below 2Gb; asked for %lu bytes at %p. Try specifying an address with +RTS -xm<addr> -RTS", size, map_addr, result);
1558 // hmm, we were given memory somewhere else, but it's
1559 // still under 2Gb so we can use it. Next time, ask
1560 // for memory right after the place we just got some
1561 mmap_32bit_base = (StgWord8*)result + size;
1565 if ((W_)result > 0x80000000) {
1566 // oops, we were given memory over 2Gb
1567 // ... try allocating memory somewhere else?;
1568 debugTrace(DEBUG_linker,"MAP_32BIT didn't work; gave us %lu bytes at 0x%p", bytes, result);
1569 munmap(result, size);
1571 // Set a base address and try again... (guess: 1Gb)
1572 mmap_32bit_base = (void*)0x40000000;
1582 /* -----------------------------------------------------------------------------
1583 * Load an obj (populate the global symbol table, but don't resolve yet)
1585 * Returns: 1 if ok, 0 on error.
1588 loadObj( char *path )
1600 /* debugBelch("loadObj %s\n", path ); */
1602 /* Check that we haven't already loaded this object.
1603 Ignore requests to load multiple times */
1607 for (o = objects; o; o = o->next) {
1608 if (0 == strcmp(o->fileName, path)) {
1610 break; /* don't need to search further */
1614 IF_DEBUG(linker, debugBelch(
1615 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1616 "same object file twice:\n"
1618 "GHCi will ignore this, but be warned.\n"
1620 return 1; /* success */
1624 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
1626 # if defined(OBJFORMAT_ELF)
1627 oc->formatName = "ELF";
1628 # elif defined(OBJFORMAT_PEi386)
1629 oc->formatName = "PEi386";
1630 # elif defined(OBJFORMAT_MACHO)
1631 oc->formatName = "Mach-O";
1634 barf("loadObj: not implemented on this platform");
1637 r = stat(path, &st);
1638 if (r == -1) { return 0; }
1640 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1641 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1642 strcpy(oc->fileName, path);
1644 oc->fileSize = st.st_size;
1646 oc->sections = NULL;
1647 oc->proddables = NULL;
1649 /* chain it onto the list of objects */
1654 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1656 #if defined(openbsd_HOST_OS)
1657 fd = open(path, O_RDONLY, S_IRUSR);
1659 fd = open(path, O_RDONLY);
1662 barf("loadObj: can't open `%s'", path);
1664 oc->image = mmapForLinker(oc->fileSize, 0, fd);
1668 #else /* !USE_MMAP */
1669 /* load the image into memory */
1670 f = fopen(path, "rb");
1672 barf("loadObj: can't read `%s'", path);
1674 # if defined(mingw32_HOST_OS)
1675 // TODO: We would like to use allocateExec here, but allocateExec
1676 // cannot currently allocate blocks large enough.
1677 oc->image = VirtualAlloc(NULL, oc->fileSize, MEM_RESERVE | MEM_COMMIT,
1678 PAGE_EXECUTE_READWRITE);
1679 # elif defined(darwin_HOST_OS)
1680 // In a Mach-O .o file, all sections can and will be misaligned
1681 // if the total size of the headers is not a multiple of the
1682 // desired alignment. This is fine for .o files that only serve
1683 // as input for the static linker, but it's not fine for us,
1684 // as SSE (used by gcc for floating point) and Altivec require
1685 // 16-byte alignment.
1686 // We calculate the correct alignment from the header before
1687 // reading the file, and then we misalign oc->image on purpose so
1688 // that the actual sections end up aligned again.
1689 oc->misalignment = machoGetMisalignment(f);
1690 oc->image = stgMallocBytes(oc->fileSize + oc->misalignment, "loadObj(image)");
1691 oc->image += oc->misalignment;
1693 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
1698 n = fread ( oc->image, 1, oc->fileSize, f );
1699 if (n != oc->fileSize)
1700 barf("loadObj: error whilst reading `%s'", path);
1703 #endif /* USE_MMAP */
1705 # if defined(OBJFORMAT_MACHO) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
1706 r = ocAllocateSymbolExtras_MachO ( oc );
1707 if (!r) { return r; }
1708 # elif defined(OBJFORMAT_ELF) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH))
1709 r = ocAllocateSymbolExtras_ELF ( oc );
1710 if (!r) { return r; }
1713 /* verify the in-memory image */
1714 # if defined(OBJFORMAT_ELF)
1715 r = ocVerifyImage_ELF ( oc );
1716 # elif defined(OBJFORMAT_PEi386)
1717 r = ocVerifyImage_PEi386 ( oc );
1718 # elif defined(OBJFORMAT_MACHO)
1719 r = ocVerifyImage_MachO ( oc );
1721 barf("loadObj: no verify method");
1723 if (!r) { return r; }
1725 /* build the symbol list for this image */
1726 # if defined(OBJFORMAT_ELF)
1727 r = ocGetNames_ELF ( oc );
1728 # elif defined(OBJFORMAT_PEi386)
1729 r = ocGetNames_PEi386 ( oc );
1730 # elif defined(OBJFORMAT_MACHO)
1731 r = ocGetNames_MachO ( oc );
1733 barf("loadObj: no getNames method");
1735 if (!r) { return r; }
1737 /* loaded, but not resolved yet */
1738 oc->status = OBJECT_LOADED;
1743 /* -----------------------------------------------------------------------------
1744 * resolve all the currently unlinked objects in memory
1746 * Returns: 1 if ok, 0 on error.
1756 for (oc = objects; oc; oc = oc->next) {
1757 if (oc->status != OBJECT_RESOLVED) {
1758 # if defined(OBJFORMAT_ELF)
1759 r = ocResolve_ELF ( oc );
1760 # elif defined(OBJFORMAT_PEi386)
1761 r = ocResolve_PEi386 ( oc );
1762 # elif defined(OBJFORMAT_MACHO)
1763 r = ocResolve_MachO ( oc );
1765 barf("resolveObjs: not implemented on this platform");
1767 if (!r) { return r; }
1768 oc->status = OBJECT_RESOLVED;
1774 /* -----------------------------------------------------------------------------
1775 * delete an object from the pool
1778 unloadObj( char *path )
1780 ObjectCode *oc, *prev;
1782 ASSERT(symhash != NULL);
1783 ASSERT(objects != NULL);
1788 for (oc = objects; oc; prev = oc, oc = oc->next) {
1789 if (!strcmp(oc->fileName,path)) {
1791 /* Remove all the mappings for the symbols within this
1796 for (i = 0; i < oc->n_symbols; i++) {
1797 if (oc->symbols[i] != NULL) {
1798 removeStrHashTable(symhash, oc->symbols[i], NULL);
1806 prev->next = oc->next;
1809 // We're going to leave this in place, in case there are
1810 // any pointers from the heap into it:
1811 // #ifdef mingw32_HOST_OS
1812 // VirtualFree(oc->image);
1814 // stgFree(oc->image);
1816 stgFree(oc->fileName);
1817 stgFree(oc->symbols);
1818 stgFree(oc->sections);
1824 errorBelch("unloadObj: can't find `%s' to unload", path);
1828 /* -----------------------------------------------------------------------------
1829 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1830 * which may be prodded during relocation, and abort if we try and write
1831 * outside any of these.
1833 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1836 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1837 /* debugBelch("aPB %p %p %d\n", oc, start, size); */
1841 pb->next = oc->proddables;
1842 oc->proddables = pb;
1845 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1848 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1849 char* s = (char*)(pb->start);
1850 char* e = s + pb->size - 1;
1851 char* a = (char*)addr;
1852 /* Assumes that the biggest fixup involves a 4-byte write. This
1853 probably needs to be changed to 8 (ie, +7) on 64-bit
1855 if (a >= s && (a+3) <= e) return;
1857 barf("checkProddableBlock: invalid fixup in runtime linker");
1860 /* -----------------------------------------------------------------------------
1861 * Section management.
1863 static void addSection ( ObjectCode* oc, SectionKind kind,
1864 void* start, void* end )
1866 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1870 s->next = oc->sections;
1873 debugBelch("addSection: %p-%p (size %d), kind %d\n",
1874 start, ((char*)end)-1, end - start + 1, kind );
1879 /* --------------------------------------------------------------------------
1881 * This is about allocating a small chunk of memory for every symbol in the
1882 * object file. We make sure that the SymboLExtras are always "in range" of
1883 * limited-range PC-relative instructions on various platforms by allocating
1884 * them right next to the object code itself.
1887 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1890 ocAllocateSymbolExtras
1892 Allocate additional space at the end of the object file image to make room
1893 for jump islands (powerpc, x86_64) and GOT entries (x86_64).
1895 PowerPC relative branch instructions have a 24 bit displacement field.
1896 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
1897 If a particular imported symbol is outside this range, we have to redirect
1898 the jump to a short piece of new code that just loads the 32bit absolute
1899 address and jumps there.
1900 On x86_64, PC-relative jumps and PC-relative accesses to the GOT are limited
1903 This function just allocates space for one SymbolExtra for every
1904 undefined symbol in the object file. The code for the jump islands is
1905 filled in by makeSymbolExtra below.
1908 static int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
1915 int misalignment = 0;
1916 #ifdef darwin_HOST_OS
1917 misalignment = oc->misalignment;
1923 // round up to the nearest 4
1924 aligned = (oc->fileSize + 3) & ~3;
1927 pagesize = getpagesize();
1928 n = ROUND_UP( oc->fileSize, pagesize );
1929 m = ROUND_UP( aligned + sizeof (SymbolExtra) * count, pagesize );
1931 /* we try to use spare space at the end of the last page of the
1932 * image for the jump islands, but if there isn't enough space
1933 * then we have to map some (anonymously, remembering MAP_32BIT).
1935 if( m > n ) // we need to allocate more pages
1937 oc->symbol_extras = mmapForLinker(sizeof(SymbolExtra) * count,
1942 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
1945 oc->image -= misalignment;
1946 oc->image = stgReallocBytes( oc->image,
1948 aligned + sizeof (SymbolExtra) * count,
1949 "ocAllocateSymbolExtras" );
1950 oc->image += misalignment;
1952 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
1953 #endif /* USE_MMAP */
1955 memset( oc->symbol_extras, 0, sizeof (SymbolExtra) * count );
1958 oc->symbol_extras = NULL;
1960 oc->first_symbol_extra = first;
1961 oc->n_symbol_extras = count;
1966 static SymbolExtra* makeSymbolExtra( ObjectCode* oc,
1967 unsigned long symbolNumber,
1968 unsigned long target )
1972 ASSERT( symbolNumber >= oc->first_symbol_extra
1973 && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
1975 extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
1977 #ifdef powerpc_HOST_ARCH
1978 // lis r12, hi16(target)
1979 extra->jumpIsland.lis_r12 = 0x3d80;
1980 extra->jumpIsland.hi_addr = target >> 16;
1982 // ori r12, r12, lo16(target)
1983 extra->jumpIsland.ori_r12_r12 = 0x618c;
1984 extra->jumpIsland.lo_addr = target & 0xffff;
1987 extra->jumpIsland.mtctr_r12 = 0x7d8903a6;
1990 extra->jumpIsland.bctr = 0x4e800420;
1992 #ifdef x86_64_HOST_ARCH
1994 static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
1995 extra->addr = target;
1996 memcpy(extra->jumpIsland, jmp, 6);
2004 /* --------------------------------------------------------------------------
2005 * PowerPC specifics (instruction cache flushing)
2006 * ------------------------------------------------------------------------*/
2008 #ifdef powerpc_TARGET_ARCH
2010 ocFlushInstructionCache
2012 Flush the data & instruction caches.
2013 Because the PPC has split data/instruction caches, we have to
2014 do that whenever we modify code at runtime.
2017 static void ocFlushInstructionCache( ObjectCode *oc )
2019 int n = (oc->fileSize + sizeof( SymbolExtra ) * oc->n_symbol_extras + 3) / 4;
2020 unsigned long *p = (unsigned long *) oc->image;
2024 __asm__ volatile ( "dcbf 0,%0\n\t"
2032 __asm__ volatile ( "sync\n\t"
2038 /* --------------------------------------------------------------------------
2039 * PEi386 specifics (Win32 targets)
2040 * ------------------------------------------------------------------------*/
2042 /* The information for this linker comes from
2043 Microsoft Portable Executable
2044 and Common Object File Format Specification
2045 revision 5.1 January 1998
2046 which SimonM says comes from the MS Developer Network CDs.
2048 It can be found there (on older CDs), but can also be found
2051 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
2053 (this is Rev 6.0 from February 1999).
2055 Things move, so if that fails, try searching for it via
2057 http://www.google.com/search?q=PE+COFF+specification
2059 The ultimate reference for the PE format is the Winnt.h
2060 header file that comes with the Platform SDKs; as always,
2061 implementations will drift wrt their documentation.
2063 A good background article on the PE format is Matt Pietrek's
2064 March 1994 article in Microsoft System Journal (MSJ)
2065 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
2066 Win32 Portable Executable File Format." The info in there
2067 has recently been updated in a two part article in
2068 MSDN magazine, issues Feb and March 2002,
2069 "Inside Windows: An In-Depth Look into the Win32 Portable
2070 Executable File Format"
2072 John Levine's book "Linkers and Loaders" contains useful
2077 #if defined(OBJFORMAT_PEi386)
2081 typedef unsigned char UChar;
2082 typedef unsigned short UInt16;
2083 typedef unsigned int UInt32;
2090 UInt16 NumberOfSections;
2091 UInt32 TimeDateStamp;
2092 UInt32 PointerToSymbolTable;
2093 UInt32 NumberOfSymbols;
2094 UInt16 SizeOfOptionalHeader;
2095 UInt16 Characteristics;
2099 #define sizeof_COFF_header 20
2106 UInt32 VirtualAddress;
2107 UInt32 SizeOfRawData;
2108 UInt32 PointerToRawData;
2109 UInt32 PointerToRelocations;
2110 UInt32 PointerToLinenumbers;
2111 UInt16 NumberOfRelocations;
2112 UInt16 NumberOfLineNumbers;
2113 UInt32 Characteristics;
2117 #define sizeof_COFF_section 40
2124 UInt16 SectionNumber;
2127 UChar NumberOfAuxSymbols;
2131 #define sizeof_COFF_symbol 18
2136 UInt32 VirtualAddress;
2137 UInt32 SymbolTableIndex;
2142 #define sizeof_COFF_reloc 10
2145 /* From PE spec doc, section 3.3.2 */
2146 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
2147 windows.h -- for the same purpose, but I want to know what I'm
2149 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
2150 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
2151 #define MYIMAGE_FILE_DLL 0x2000
2152 #define MYIMAGE_FILE_SYSTEM 0x1000
2153 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
2154 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
2155 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
2157 /* From PE spec doc, section 5.4.2 and 5.4.4 */
2158 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
2159 #define MYIMAGE_SYM_CLASS_STATIC 3
2160 #define MYIMAGE_SYM_UNDEFINED 0
2162 /* From PE spec doc, section 4.1 */
2163 #define MYIMAGE_SCN_CNT_CODE 0x00000020
2164 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
2165 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
2167 /* From PE spec doc, section 5.2.1 */
2168 #define MYIMAGE_REL_I386_DIR32 0x0006
2169 #define MYIMAGE_REL_I386_REL32 0x0014
2172 /* We use myindex to calculate array addresses, rather than
2173 simply doing the normal subscript thing. That's because
2174 some of the above structs have sizes which are not
2175 a whole number of words. GCC rounds their sizes up to a
2176 whole number of words, which means that the address calcs
2177 arising from using normal C indexing or pointer arithmetic
2178 are just plain wrong. Sigh.
2181 myindex ( int scale, void* base, int index )
2184 ((UChar*)base) + scale * index;
2189 printName ( UChar* name, UChar* strtab )
2191 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2192 UInt32 strtab_offset = * (UInt32*)(name+4);
2193 debugBelch("%s", strtab + strtab_offset );
2196 for (i = 0; i < 8; i++) {
2197 if (name[i] == 0) break;
2198 debugBelch("%c", name[i] );
2205 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
2207 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2208 UInt32 strtab_offset = * (UInt32*)(name+4);
2209 strncpy ( dst, strtab+strtab_offset, dstSize );
2215 if (name[i] == 0) break;
2225 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
2228 /* If the string is longer than 8 bytes, look in the
2229 string table for it -- this will be correctly zero terminated.
2231 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
2232 UInt32 strtab_offset = * (UInt32*)(name+4);
2233 return ((UChar*)strtab) + strtab_offset;
2235 /* Otherwise, if shorter than 8 bytes, return the original,
2236 which by defn is correctly terminated.
2238 if (name[7]==0) return name;
2239 /* The annoying case: 8 bytes. Copy into a temporary
2240 (which is never freed ...)
2242 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
2244 strncpy(newstr,name,8);
2250 /* Just compares the short names (first 8 chars) */
2251 static COFF_section *
2252 findPEi386SectionCalled ( ObjectCode* oc, char* name )
2256 = (COFF_header*)(oc->image);
2257 COFF_section* sectab
2259 ((UChar*)(oc->image))
2260 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2262 for (i = 0; i < hdr->NumberOfSections; i++) {
2265 COFF_section* section_i
2267 myindex ( sizeof_COFF_section, sectab, i );
2268 n1 = (UChar*) &(section_i->Name);
2270 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
2271 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
2272 n1[6]==n2[6] && n1[7]==n2[7])
2281 zapTrailingAtSign ( UChar* sym )
2283 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
2285 if (sym[0] == 0) return;
2287 while (sym[i] != 0) i++;
2290 while (j > 0 && my_isdigit(sym[j])) j--;
2291 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
2296 lookupSymbolInDLLs ( UChar *lbl )
2301 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
2302 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
2304 if (lbl[0] == '_') {
2305 /* HACK: if the name has an initial underscore, try stripping
2306 it off & look that up first. I've yet to verify whether there's
2307 a Rule that governs whether an initial '_' *should always* be
2308 stripped off when mapping from import lib name to the DLL name.
2310 sym = GetProcAddress(o_dll->instance, (lbl+1));
2312 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
2316 sym = GetProcAddress(o_dll->instance, lbl);
2318 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
2327 ocVerifyImage_PEi386 ( ObjectCode* oc )
2332 COFF_section* sectab;
2333 COFF_symbol* symtab;
2335 /* debugBelch("\nLOADING %s\n", oc->fileName); */
2336 hdr = (COFF_header*)(oc->image);
2337 sectab = (COFF_section*) (
2338 ((UChar*)(oc->image))
2339 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2341 symtab = (COFF_symbol*) (
2342 ((UChar*)(oc->image))
2343 + hdr->PointerToSymbolTable
2345 strtab = ((UChar*)symtab)
2346 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2348 if (hdr->Machine != 0x14c) {
2349 errorBelch("%s: Not x86 PEi386", oc->fileName);
2352 if (hdr->SizeOfOptionalHeader != 0) {
2353 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
2356 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
2357 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
2358 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
2359 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
2360 errorBelch("%s: Not a PEi386 object file", oc->fileName);
2363 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
2364 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
2365 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
2367 (int)(hdr->Characteristics));
2370 /* If the string table size is way crazy, this might indicate that
2371 there are more than 64k relocations, despite claims to the
2372 contrary. Hence this test. */
2373 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
2375 if ( (*(UInt32*)strtab) > 600000 ) {
2376 /* Note that 600k has no special significance other than being
2377 big enough to handle the almost-2MB-sized lumps that
2378 constitute HSwin32*.o. */
2379 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
2384 /* No further verification after this point; only debug printing. */
2386 IF_DEBUG(linker, i=1);
2387 if (i == 0) return 1;
2389 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
2390 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
2391 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
2394 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
2395 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
2396 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
2397 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
2398 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
2399 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
2400 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
2402 /* Print the section table. */
2404 for (i = 0; i < hdr->NumberOfSections; i++) {
2406 COFF_section* sectab_i
2408 myindex ( sizeof_COFF_section, sectab, i );
2415 printName ( sectab_i->Name, strtab );
2425 sectab_i->VirtualSize,
2426 sectab_i->VirtualAddress,
2427 sectab_i->SizeOfRawData,
2428 sectab_i->PointerToRawData,
2429 sectab_i->NumberOfRelocations,
2430 sectab_i->PointerToRelocations,
2431 sectab_i->PointerToRawData
2433 reltab = (COFF_reloc*) (
2434 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2437 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2438 /* If the relocation field (a short) has overflowed, the
2439 * real count can be found in the first reloc entry.
2441 * See Section 4.1 (last para) of the PE spec (rev6.0).
2443 COFF_reloc* rel = (COFF_reloc*)
2444 myindex ( sizeof_COFF_reloc, reltab, 0 );
2445 noRelocs = rel->VirtualAddress;
2448 noRelocs = sectab_i->NumberOfRelocations;
2452 for (; j < noRelocs; j++) {
2454 COFF_reloc* rel = (COFF_reloc*)
2455 myindex ( sizeof_COFF_reloc, reltab, j );
2457 " type 0x%-4x vaddr 0x%-8x name `",
2459 rel->VirtualAddress );
2460 sym = (COFF_symbol*)
2461 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
2462 /* Hmm..mysterious looking offset - what's it for? SOF */
2463 printName ( sym->Name, strtab -10 );
2470 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
2471 debugBelch("---START of string table---\n");
2472 for (i = 4; i < *(Int32*)strtab; i++) {
2474 debugBelch("\n"); else
2475 debugBelch("%c", strtab[i] );
2477 debugBelch("--- END of string table---\n");
2482 COFF_symbol* symtab_i;
2483 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2484 symtab_i = (COFF_symbol*)
2485 myindex ( sizeof_COFF_symbol, symtab, i );
2491 printName ( symtab_i->Name, strtab );
2500 (Int32)(symtab_i->SectionNumber),
2501 (UInt32)symtab_i->Type,
2502 (UInt32)symtab_i->StorageClass,
2503 (UInt32)symtab_i->NumberOfAuxSymbols
2505 i += symtab_i->NumberOfAuxSymbols;
2515 ocGetNames_PEi386 ( ObjectCode* oc )
2518 COFF_section* sectab;
2519 COFF_symbol* symtab;
2526 hdr = (COFF_header*)(oc->image);
2527 sectab = (COFF_section*) (
2528 ((UChar*)(oc->image))
2529 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2531 symtab = (COFF_symbol*) (
2532 ((UChar*)(oc->image))
2533 + hdr->PointerToSymbolTable
2535 strtab = ((UChar*)(oc->image))
2536 + hdr->PointerToSymbolTable
2537 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2539 /* Allocate space for any (local, anonymous) .bss sections. */
2541 for (i = 0; i < hdr->NumberOfSections; i++) {
2544 COFF_section* sectab_i
2546 myindex ( sizeof_COFF_section, sectab, i );
2547 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
2548 /* sof 10/05: the PE spec text isn't too clear regarding what
2549 * the SizeOfRawData field is supposed to hold for object
2550 * file sections containing just uninitialized data -- for executables,
2551 * it is supposed to be zero; unclear what it's supposed to be
2552 * for object files. However, VirtualSize is guaranteed to be
2553 * zero for object files, which definitely suggests that SizeOfRawData
2554 * will be non-zero (where else would the size of this .bss section be
2555 * stored?) Looking at the COFF_section info for incoming object files,
2556 * this certainly appears to be the case.
2558 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
2559 * object files up until now. This turned out to bite us with ghc-6.4.1's use
2560 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
2561 * variable decls into to the .bss section. (The specific function in Q which
2562 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
2564 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
2565 /* This is a non-empty .bss section. Allocate zeroed space for
2566 it, and set its PointerToRawData field such that oc->image +
2567 PointerToRawData == addr_of_zeroed_space. */
2568 bss_sz = sectab_i->VirtualSize;
2569 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
2570 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
2571 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
2572 addProddableBlock(oc, zspace, bss_sz);
2573 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
2576 /* Copy section information into the ObjectCode. */
2578 for (i = 0; i < hdr->NumberOfSections; i++) {
2584 = SECTIONKIND_OTHER;
2585 COFF_section* sectab_i
2587 myindex ( sizeof_COFF_section, sectab, i );
2588 IF_DEBUG(linker, debugBelch("section name = %s\n", sectab_i->Name ));
2591 /* I'm sure this is the Right Way to do it. However, the
2592 alternative of testing the sectab_i->Name field seems to
2593 work ok with Cygwin.
2595 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
2596 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
2597 kind = SECTIONKIND_CODE_OR_RODATA;
2600 if (0==strcmp(".text",sectab_i->Name) ||
2601 0==strcmp(".rdata",sectab_i->Name)||
2602 0==strcmp(".rodata",sectab_i->Name))
2603 kind = SECTIONKIND_CODE_OR_RODATA;
2604 if (0==strcmp(".data",sectab_i->Name) ||
2605 0==strcmp(".bss",sectab_i->Name))
2606 kind = SECTIONKIND_RWDATA;
2608 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
2609 sz = sectab_i->SizeOfRawData;
2610 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
2612 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
2613 end = start + sz - 1;
2615 if (kind == SECTIONKIND_OTHER
2616 /* Ignore sections called which contain stabs debugging
2618 && 0 != strcmp(".stab", sectab_i->Name)
2619 && 0 != strcmp(".stabstr", sectab_i->Name)
2620 /* ignore constructor section for now */
2621 && 0 != strcmp(".ctors", sectab_i->Name)
2622 /* ignore section generated from .ident */
2623 && 0!= strcmp("/4", sectab_i->Name)
2624 /* ignore unknown section that appeared in gcc 3.4.5(?) */
2625 && 0!= strcmp(".reloc", sectab_i->Name)
2627 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", sectab_i->Name, oc->fileName);
2631 if (kind != SECTIONKIND_OTHER && end >= start) {
2632 addSection(oc, kind, start, end);
2633 addProddableBlock(oc, start, end - start + 1);
2637 /* Copy exported symbols into the ObjectCode. */
2639 oc->n_symbols = hdr->NumberOfSymbols;
2640 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2641 "ocGetNames_PEi386(oc->symbols)");
2642 /* Call me paranoid; I don't care. */
2643 for (i = 0; i < oc->n_symbols; i++)
2644 oc->symbols[i] = NULL;
2648 COFF_symbol* symtab_i;
2649 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2650 symtab_i = (COFF_symbol*)
2651 myindex ( sizeof_COFF_symbol, symtab, i );
2655 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
2656 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
2657 /* This symbol is global and defined, viz, exported */
2658 /* for MYIMAGE_SYMCLASS_EXTERNAL
2659 && !MYIMAGE_SYM_UNDEFINED,
2660 the address of the symbol is:
2661 address of relevant section + offset in section
2663 COFF_section* sectabent
2664 = (COFF_section*) myindex ( sizeof_COFF_section,
2666 symtab_i->SectionNumber-1 );
2667 addr = ((UChar*)(oc->image))
2668 + (sectabent->PointerToRawData
2672 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
2673 && symtab_i->Value > 0) {
2674 /* This symbol isn't in any section at all, ie, global bss.
2675 Allocate zeroed space for it. */
2676 addr = stgCallocBytes(1, symtab_i->Value,
2677 "ocGetNames_PEi386(non-anonymous bss)");
2678 addSection(oc, SECTIONKIND_RWDATA, addr,
2679 ((UChar*)addr) + symtab_i->Value - 1);
2680 addProddableBlock(oc, addr, symtab_i->Value);
2681 /* debugBelch("BSS section at 0x%x\n", addr); */
2684 if (addr != NULL ) {
2685 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
2686 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
2687 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
2688 ASSERT(i >= 0 && i < oc->n_symbols);
2689 /* cstring_from_COFF_symbol_name always succeeds. */
2690 oc->symbols[i] = sname;
2691 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
2695 "IGNORING symbol %d\n"
2699 printName ( symtab_i->Name, strtab );
2708 (Int32)(symtab_i->SectionNumber),
2709 (UInt32)symtab_i->Type,
2710 (UInt32)symtab_i->StorageClass,
2711 (UInt32)symtab_i->NumberOfAuxSymbols
2716 i += symtab_i->NumberOfAuxSymbols;
2725 ocResolve_PEi386 ( ObjectCode* oc )
2728 COFF_section* sectab;
2729 COFF_symbol* symtab;
2739 /* ToDo: should be variable-sized? But is at least safe in the
2740 sense of buffer-overrun-proof. */
2742 /* debugBelch("resolving for %s\n", oc->fileName); */
2744 hdr = (COFF_header*)(oc->image);
2745 sectab = (COFF_section*) (
2746 ((UChar*)(oc->image))
2747 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2749 symtab = (COFF_symbol*) (
2750 ((UChar*)(oc->image))
2751 + hdr->PointerToSymbolTable
2753 strtab = ((UChar*)(oc->image))
2754 + hdr->PointerToSymbolTable
2755 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2757 for (i = 0; i < hdr->NumberOfSections; i++) {
2758 COFF_section* sectab_i
2760 myindex ( sizeof_COFF_section, sectab, i );
2763 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2766 /* Ignore sections called which contain stabs debugging
2768 if (0 == strcmp(".stab", sectab_i->Name)
2769 || 0 == strcmp(".stabstr", sectab_i->Name)
2770 || 0 == strcmp(".ctors", sectab_i->Name))
2773 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2774 /* If the relocation field (a short) has overflowed, the
2775 * real count can be found in the first reloc entry.
2777 * See Section 4.1 (last para) of the PE spec (rev6.0).
2779 * Nov2003 update: the GNU linker still doesn't correctly
2780 * handle the generation of relocatable object files with
2781 * overflown relocations. Hence the output to warn of potential
2784 COFF_reloc* rel = (COFF_reloc*)
2785 myindex ( sizeof_COFF_reloc, reltab, 0 );
2786 noRelocs = rel->VirtualAddress;
2788 /* 10/05: we now assume (and check for) a GNU ld that is capable
2789 * of handling object files with (>2^16) of relocs.
2792 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
2797 noRelocs = sectab_i->NumberOfRelocations;
2802 for (; j < noRelocs; j++) {
2804 COFF_reloc* reltab_j
2806 myindex ( sizeof_COFF_reloc, reltab, j );
2808 /* the location to patch */
2810 ((UChar*)(oc->image))
2811 + (sectab_i->PointerToRawData
2812 + reltab_j->VirtualAddress
2813 - sectab_i->VirtualAddress )
2815 /* the existing contents of pP */
2817 /* the symbol to connect to */
2818 sym = (COFF_symbol*)
2819 myindex ( sizeof_COFF_symbol,
2820 symtab, reltab_j->SymbolTableIndex );
2823 "reloc sec %2d num %3d: type 0x%-4x "
2824 "vaddr 0x%-8x name `",
2826 (UInt32)reltab_j->Type,
2827 reltab_j->VirtualAddress );
2828 printName ( sym->Name, strtab );
2829 debugBelch("'\n" ));
2831 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
2832 COFF_section* section_sym
2833 = findPEi386SectionCalled ( oc, sym->Name );
2835 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
2838 S = ((UInt32)(oc->image))
2839 + (section_sym->PointerToRawData
2842 copyName ( sym->Name, strtab, symbol, 1000-1 );
2843 S = (UInt32) lookupSymbol( symbol );
2844 if ((void*)S != NULL) goto foundit;
2845 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
2849 checkProddableBlock(oc, pP);
2850 switch (reltab_j->Type) {
2851 case MYIMAGE_REL_I386_DIR32:
2854 case MYIMAGE_REL_I386_REL32:
2855 /* Tricky. We have to insert a displacement at
2856 pP which, when added to the PC for the _next_
2857 insn, gives the address of the target (S).
2858 Problem is to know the address of the next insn
2859 when we only know pP. We assume that this
2860 literal field is always the last in the insn,
2861 so that the address of the next insn is pP+4
2862 -- hence the constant 4.
2863 Also I don't know if A should be added, but so
2864 far it has always been zero.
2866 SOF 05/2005: 'A' (old contents of *pP) have been observed
2867 to contain values other than zero (the 'wx' object file
2868 that came with wxhaskell-0.9.4; dunno how it was compiled..).
2869 So, add displacement to old value instead of asserting
2870 A to be zero. Fixes wxhaskell-related crashes, and no other
2871 ill effects have been observed.
2873 Update: the reason why we're seeing these more elaborate
2874 relocations is due to a switch in how the NCG compiles SRTs
2875 and offsets to them from info tables. SRTs live in .(ro)data,
2876 while info tables live in .text, causing GAS to emit REL32/DISP32
2877 relocations with non-zero values. Adding the displacement is
2878 the right thing to do.
2880 *pP = S - ((UInt32)pP) - 4 + A;
2883 debugBelch("%s: unhandled PEi386 relocation type %d",
2884 oc->fileName, reltab_j->Type);
2891 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
2895 #endif /* defined(OBJFORMAT_PEi386) */
2898 /* --------------------------------------------------------------------------
2900 * ------------------------------------------------------------------------*/
2902 #if defined(OBJFORMAT_ELF)
2907 #if defined(sparc_HOST_ARCH)
2908 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2909 #elif defined(i386_HOST_ARCH)
2910 # define ELF_TARGET_386 /* Used inside <elf.h> */
2911 #elif defined(x86_64_HOST_ARCH)
2912 # define ELF_TARGET_X64_64
2916 #if !defined(openbsd_HOST_OS)
2919 /* openbsd elf has things in different places, with diff names */
2920 # include <elf_abi.h>
2921 # include <machine/reloc.h>
2922 # define R_386_32 RELOC_32
2923 # define R_386_PC32 RELOC_PC32
2926 /* If elf.h doesn't define it */
2927 # ifndef R_X86_64_PC64
2928 # define R_X86_64_PC64 24
2932 * Define a set of types which can be used for both ELF32 and ELF64
2936 #define ELFCLASS ELFCLASS64
2937 #define Elf_Addr Elf64_Addr
2938 #define Elf_Word Elf64_Word
2939 #define Elf_Sword Elf64_Sword
2940 #define Elf_Ehdr Elf64_Ehdr
2941 #define Elf_Phdr Elf64_Phdr
2942 #define Elf_Shdr Elf64_Shdr
2943 #define Elf_Sym Elf64_Sym
2944 #define Elf_Rel Elf64_Rel
2945 #define Elf_Rela Elf64_Rela
2946 #define ELF_ST_TYPE ELF64_ST_TYPE
2947 #define ELF_ST_BIND ELF64_ST_BIND
2948 #define ELF_R_TYPE ELF64_R_TYPE
2949 #define ELF_R_SYM ELF64_R_SYM
2951 #define ELFCLASS ELFCLASS32
2952 #define Elf_Addr Elf32_Addr
2953 #define Elf_Word Elf32_Word
2954 #define Elf_Sword Elf32_Sword
2955 #define Elf_Ehdr Elf32_Ehdr
2956 #define Elf_Phdr Elf32_Phdr
2957 #define Elf_Shdr Elf32_Shdr
2958 #define Elf_Sym Elf32_Sym
2959 #define Elf_Rel Elf32_Rel
2960 #define Elf_Rela Elf32_Rela
2962 #define ELF_ST_TYPE ELF32_ST_TYPE
2965 #define ELF_ST_BIND ELF32_ST_BIND
2968 #define ELF_R_TYPE ELF32_R_TYPE
2971 #define ELF_R_SYM ELF32_R_SYM
2977 * Functions to allocate entries in dynamic sections. Currently we simply
2978 * preallocate a large number, and we don't check if a entry for the given
2979 * target already exists (a linear search is too slow). Ideally these
2980 * entries would be associated with symbols.
2983 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2984 #define GOT_SIZE 0x20000
2985 #define FUNCTION_TABLE_SIZE 0x10000
2986 #define PLT_SIZE 0x08000
2989 static Elf_Addr got[GOT_SIZE];
2990 static unsigned int gotIndex;
2991 static Elf_Addr gp_val = (Elf_Addr)got;
2994 allocateGOTEntry(Elf_Addr target)
2998 if (gotIndex >= GOT_SIZE)
2999 barf("Global offset table overflow");
3001 entry = &got[gotIndex++];
3003 return (Elf_Addr)entry;
3007 #ifdef ELF_FUNCTION_DESC
3013 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
3014 static unsigned int functionTableIndex;
3017 allocateFunctionDesc(Elf_Addr target)
3019 FunctionDesc *entry;
3021 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
3022 barf("Function table overflow");
3024 entry = &functionTable[functionTableIndex++];
3026 entry->gp = (Elf_Addr)gp_val;
3027 return (Elf_Addr)entry;
3031 copyFunctionDesc(Elf_Addr target)
3033 FunctionDesc *olddesc = (FunctionDesc *)target;
3034 FunctionDesc *newdesc;
3036 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
3037 newdesc->gp = olddesc->gp;
3038 return (Elf_Addr)newdesc;
3045 unsigned char code[sizeof(plt_code)];
3049 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
3051 PLTEntry *plt = (PLTEntry *)oc->plt;
3054 if (oc->pltIndex >= PLT_SIZE)
3055 barf("Procedure table overflow");
3057 entry = &plt[oc->pltIndex++];
3058 memcpy(entry->code, plt_code, sizeof(entry->code));
3059 PLT_RELOC(entry->code, target);
3060 return (Elf_Addr)entry;
3066 return (PLT_SIZE * sizeof(PLTEntry));
3072 * Generic ELF functions
3076 findElfSection ( void* objImage, Elf_Word sh_type )
3078 char* ehdrC = (char*)objImage;
3079 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3080 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
3081 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3085 for (i = 0; i < ehdr->e_shnum; i++) {
3086 if (shdr[i].sh_type == sh_type
3087 /* Ignore the section header's string table. */
3088 && i != ehdr->e_shstrndx
3089 /* Ignore string tables named .stabstr, as they contain
3091 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3093 ptr = ehdrC + shdr[i].sh_offset;
3101 ocVerifyImage_ELF ( ObjectCode* oc )
3105 int i, j, nent, nstrtab, nsymtabs;
3109 char* ehdrC = (char*)(oc->image);
3110 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3112 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
3113 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
3114 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
3115 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
3116 errorBelch("%s: not an ELF object", oc->fileName);
3120 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
3121 errorBelch("%s: unsupported ELF format", oc->fileName);
3125 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
3126 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
3128 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
3129 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
3131 errorBelch("%s: unknown endiannness", oc->fileName);
3135 if (ehdr->e_type != ET_REL) {
3136 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
3139 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
3141 IF_DEBUG(linker,debugBelch( "Architecture is " ));
3142 switch (ehdr->e_machine) {
3143 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
3144 #ifdef EM_SPARC32PLUS
3145 case EM_SPARC32PLUS:
3147 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
3149 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
3151 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
3153 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
3154 #elif defined(EM_AMD64)
3155 case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break;
3157 default: IF_DEBUG(linker,debugBelch( "unknown" ));
3158 errorBelch("%s: unknown architecture (e_machine == %d)"
3159 , oc->fileName, ehdr->e_machine);
3163 IF_DEBUG(linker,debugBelch(
3164 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
3165 (long)ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
3167 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
3169 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3171 if (ehdr->e_shstrndx == SHN_UNDEF) {
3172 errorBelch("%s: no section header string table", oc->fileName);
3175 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
3177 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3180 for (i = 0; i < ehdr->e_shnum; i++) {
3181 IF_DEBUG(linker,debugBelch("%2d: ", i ));
3182 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
3183 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
3184 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
3185 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
3186 ehdrC + shdr[i].sh_offset,
3187 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
3189 if (shdr[i].sh_type == SHT_REL) {
3190 IF_DEBUG(linker,debugBelch("Rel " ));
3191 } else if (shdr[i].sh_type == SHT_RELA) {
3192 IF_DEBUG(linker,debugBelch("RelA " ));
3194 IF_DEBUG(linker,debugBelch(" "));
3197 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
3201 IF_DEBUG(linker,debugBelch( "\nString tables" ));
3204 for (i = 0; i < ehdr->e_shnum; i++) {
3205 if (shdr[i].sh_type == SHT_STRTAB
3206 /* Ignore the section header's string table. */
3207 && i != ehdr->e_shstrndx
3208 /* Ignore string tables named .stabstr, as they contain
3210 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
3212 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
3213 strtab = ehdrC + shdr[i].sh_offset;
3218 errorBelch("%s: no string tables, or too many", oc->fileName);
3223 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
3224 for (i = 0; i < ehdr->e_shnum; i++) {
3225 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3226 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
3228 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3229 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3230 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
3232 (long)shdr[i].sh_size % sizeof(Elf_Sym)
3234 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
3235 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
3238 for (j = 0; j < nent; j++) {
3239 IF_DEBUG(linker,debugBelch(" %2d ", j ));
3240 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
3241 (int)stab[j].st_shndx,
3242 (int)stab[j].st_size,
3243 (char*)stab[j].st_value ));
3245 IF_DEBUG(linker,debugBelch("type=" ));
3246 switch (ELF_ST_TYPE(stab[j].st_info)) {
3247 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
3248 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
3249 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
3250 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
3251 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
3252 default: IF_DEBUG(linker,debugBelch("? " )); break;
3254 IF_DEBUG(linker,debugBelch(" " ));
3256 IF_DEBUG(linker,debugBelch("bind=" ));
3257 switch (ELF_ST_BIND(stab[j].st_info)) {
3258 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
3259 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
3260 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
3261 default: IF_DEBUG(linker,debugBelch("? " )); break;
3263 IF_DEBUG(linker,debugBelch(" " ));
3265 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
3269 if (nsymtabs == 0) {
3270 errorBelch("%s: didn't find any symbol tables", oc->fileName);
3277 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
3281 if (hdr->sh_type == SHT_PROGBITS
3282 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
3283 /* .text-style section */
3284 return SECTIONKIND_CODE_OR_RODATA;
3287 if (hdr->sh_type == SHT_PROGBITS
3288 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3289 /* .data-style section */
3290 return SECTIONKIND_RWDATA;
3293 if (hdr->sh_type == SHT_PROGBITS
3294 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
3295 /* .rodata-style section */
3296 return SECTIONKIND_CODE_OR_RODATA;
3299 if (hdr->sh_type == SHT_NOBITS
3300 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
3301 /* .bss-style section */
3303 return SECTIONKIND_RWDATA;
3306 return SECTIONKIND_OTHER;
3311 ocGetNames_ELF ( ObjectCode* oc )
3316 char* ehdrC = (char*)(oc->image);
3317 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
3318 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
3319 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3321 ASSERT(symhash != NULL);
3324 errorBelch("%s: no strtab", oc->fileName);
3329 for (i = 0; i < ehdr->e_shnum; i++) {
3330 /* Figure out what kind of section it is. Logic derived from
3331 Figure 1.14 ("Special Sections") of the ELF document
3332 ("Portable Formats Specification, Version 1.1"). */
3334 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
3336 if (is_bss && shdr[i].sh_size > 0) {
3337 /* This is a non-empty .bss section. Allocate zeroed space for
3338 it, and set its .sh_offset field such that
3339 ehdrC + .sh_offset == addr_of_zeroed_space. */
3340 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
3341 "ocGetNames_ELF(BSS)");
3342 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
3344 debugBelch("BSS section at 0x%x, size %d\n",
3345 zspace, shdr[i].sh_size);
3349 /* fill in the section info */
3350 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
3351 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
3352 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
3353 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
3356 if (shdr[i].sh_type != SHT_SYMTAB) continue;
3358 /* copy stuff into this module's object symbol table */
3359 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
3360 nent = shdr[i].sh_size / sizeof(Elf_Sym);
3362 oc->n_symbols = nent;
3363 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3364 "ocGetNames_ELF(oc->symbols)");
3366 for (j = 0; j < nent; j++) {
3368 char isLocal = FALSE; /* avoids uninit-var warning */
3370 char* nm = strtab + stab[j].st_name;
3371 int secno = stab[j].st_shndx;
3373 /* Figure out if we want to add it; if so, set ad to its
3374 address. Otherwise leave ad == NULL. */
3376 if (secno == SHN_COMMON) {
3378 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
3380 debugBelch("COMMON symbol, size %d name %s\n",
3381 stab[j].st_size, nm);
3383 /* Pointless to do addProddableBlock() for this area,
3384 since the linker should never poke around in it. */
3387 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
3388 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
3390 /* and not an undefined symbol */
3391 && stab[j].st_shndx != SHN_UNDEF
3392 /* and not in a "special section" */
3393 && stab[j].st_shndx < SHN_LORESERVE
3395 /* and it's a not a section or string table or anything silly */
3396 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
3397 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
3398 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
3401 /* Section 0 is the undefined section, hence > and not >=. */
3402 ASSERT(secno > 0 && secno < ehdr->e_shnum);
3404 if (shdr[secno].sh_type == SHT_NOBITS) {
3405 debugBelch(" BSS symbol, size %d off %d name %s\n",
3406 stab[j].st_size, stab[j].st_value, nm);
3409 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
3410 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
3413 #ifdef ELF_FUNCTION_DESC
3414 /* dlsym() and the initialisation table both give us function
3415 * descriptors, so to be consistent we store function descriptors
3416 * in the symbol table */
3417 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
3418 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
3420 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s\n",
3421 ad, oc->fileName, nm ));
3426 /* And the decision is ... */
3430 oc->symbols[j] = nm;
3433 /* Ignore entirely. */
3435 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3439 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3440 strtab + stab[j].st_name ));
3443 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
3444 (int)ELF_ST_BIND(stab[j].st_info),
3445 (int)ELF_ST_TYPE(stab[j].st_info),
3446 (int)stab[j].st_shndx,
3447 strtab + stab[j].st_name
3450 oc->symbols[j] = NULL;
3459 /* Do ELF relocations which lack an explicit addend. All x86-linux
3460 relocations appear to be of this form. */
3462 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3463 Elf_Shdr* shdr, int shnum,
3464 Elf_Sym* stab, char* strtab )
3469 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3470 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3471 int target_shndx = shdr[shnum].sh_info;
3472 int symtab_shndx = shdr[shnum].sh_link;
3474 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3475 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3476 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3477 target_shndx, symtab_shndx ));
3479 /* Skip sections that we're not interested in. */
3482 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3483 if (kind == SECTIONKIND_OTHER) {
3484 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
3489 for (j = 0; j < nent; j++) {
3490 Elf_Addr offset = rtab[j].r_offset;
3491 Elf_Addr info = rtab[j].r_info;
3493 Elf_Addr P = ((Elf_Addr)targ) + offset;
3494 Elf_Word* pP = (Elf_Word*)P;
3499 StgStablePtr stablePtr;
3502 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
3503 j, (void*)offset, (void*)info ));
3505 IF_DEBUG(linker,debugBelch( " ZERO" ));
3508 Elf_Sym sym = stab[ELF_R_SYM(info)];
3509 /* First see if it is a local symbol. */
3510 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3511 /* Yes, so we can get the address directly from the ELF symbol
3513 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3515 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3516 + stab[ELF_R_SYM(info)].st_value);
3519 symbol = strtab + sym.st_name;
3520 stablePtr = (StgStablePtr)lookupHashTable(stablehash, (StgWord)symbol);
3521 if (NULL == stablePtr) {
3522 /* No, so look up the name in our global table. */
3523 S_tmp = lookupSymbol( symbol );
3524 S = (Elf_Addr)S_tmp;
3526 stableVal = deRefStablePtr( stablePtr );
3528 S = (Elf_Addr)S_tmp;
3532 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3535 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
3538 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
3539 (void*)P, (void*)S, (void*)A ));
3540 checkProddableBlock ( oc, pP );
3544 switch (ELF_R_TYPE(info)) {
3545 # ifdef i386_HOST_ARCH
3546 case R_386_32: *pP = value; break;
3547 case R_386_PC32: *pP = value - P; break;
3550 errorBelch("%s: unhandled ELF relocation(Rel) type %lu\n",
3551 oc->fileName, (lnat)ELF_R_TYPE(info));
3559 /* Do ELF relocations for which explicit addends are supplied.
3560 sparc-solaris relocations appear to be of this form. */
3562 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
3563 Elf_Shdr* shdr, int shnum,
3564 Elf_Sym* stab, char* strtab )
3567 char *symbol = NULL;
3569 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
3570 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
3571 int target_shndx = shdr[shnum].sh_info;
3572 int symtab_shndx = shdr[shnum].sh_link;
3574 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3575 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
3576 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3577 target_shndx, symtab_shndx ));
3579 for (j = 0; j < nent; j++) {
3580 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
3581 /* This #ifdef only serves to avoid unused-var warnings. */
3582 Elf_Addr offset = rtab[j].r_offset;
3583 Elf_Addr P = targ + offset;
3585 Elf_Addr info = rtab[j].r_info;
3586 Elf_Addr A = rtab[j].r_addend;
3590 # if defined(sparc_HOST_ARCH)
3591 Elf_Word* pP = (Elf_Word*)P;
3593 # elif defined(powerpc_HOST_ARCH)
3597 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
3598 j, (void*)offset, (void*)info,
3601 IF_DEBUG(linker,debugBelch( " ZERO" ));
3604 Elf_Sym sym = stab[ELF_R_SYM(info)];
3605 /* First see if it is a local symbol. */
3606 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3607 /* Yes, so we can get the address directly from the ELF symbol
3609 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3611 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3612 + stab[ELF_R_SYM(info)].st_value);
3613 #ifdef ELF_FUNCTION_DESC
3614 /* Make a function descriptor for this function */
3615 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
3616 S = allocateFunctionDesc(S + A);
3621 /* No, so look up the name in our global table. */
3622 symbol = strtab + sym.st_name;
3623 S_tmp = lookupSymbol( symbol );
3624 S = (Elf_Addr)S_tmp;
3626 #ifdef ELF_FUNCTION_DESC
3627 /* If a function, already a function descriptor - we would
3628 have to copy it to add an offset. */
3629 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
3630 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
3634 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3637 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
3640 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
3641 (void*)P, (void*)S, (void*)A ));
3642 /* checkProddableBlock ( oc, (void*)P ); */
3646 switch (ELF_R_TYPE(info)) {
3647 # if defined(sparc_HOST_ARCH)
3648 case R_SPARC_WDISP30:
3649 w1 = *pP & 0xC0000000;
3650 w2 = (Elf_Word)((value - P) >> 2);
3651 ASSERT((w2 & 0xC0000000) == 0);
3656 w1 = *pP & 0xFFC00000;
3657 w2 = (Elf_Word)(value >> 10);
3658 ASSERT((w2 & 0xFFC00000) == 0);
3664 w2 = (Elf_Word)(value & 0x3FF);
3665 ASSERT((w2 & ~0x3FF) == 0);
3670 /* According to the Sun documentation:
3672 This relocation type resembles R_SPARC_32, except it refers to an
3673 unaligned word. That is, the word to be relocated must be treated
3674 as four separate bytes with arbitrary alignment, not as a word
3675 aligned according to the architecture requirements.
3678 w2 = (Elf_Word)value;
3680 // SPARC doesn't do misaligned writes of 32 bit words,
3681 // so we have to do this one byte-at-a-time.
3682 char *pPc = (char*)pP;
3683 pPc[0] = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
3684 pPc[1] = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
3685 pPc[2] = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
3686 pPc[3] = (char) ((Elf_Word)(w2 & 0x000000ff));
3690 w2 = (Elf_Word)value;
3693 # elif defined(powerpc_HOST_ARCH)
3694 case R_PPC_ADDR16_LO:
3695 *(Elf32_Half*) P = value;
3698 case R_PPC_ADDR16_HI:
3699 *(Elf32_Half*) P = value >> 16;
3702 case R_PPC_ADDR16_HA:
3703 *(Elf32_Half*) P = (value + 0x8000) >> 16;
3707 *(Elf32_Word *) P = value;
3711 *(Elf32_Word *) P = value - P;
3717 if( delta << 6 >> 6 != delta )
3719 value = (Elf_Addr) (&makeSymbolExtra( oc, ELF_R_SYM(info), value )
3723 if( value == 0 || delta << 6 >> 6 != delta )
3725 barf( "Unable to make SymbolExtra for #%d",
3731 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
3732 | (delta & 0x3fffffc);
3736 #if x86_64_HOST_ARCH
3738 *(Elf64_Xword *)P = value;
3743 StgInt64 off = value - P;
3744 if (off >= 0x7fffffffL || off < -0x80000000L) {
3745 #if X86_64_ELF_NONPIC_HACK
3746 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
3748 off = pltAddress + A - P;
3750 barf("R_X86_64_PC32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
3751 symbol, off, oc->fileName );
3754 *(Elf64_Word *)P = (Elf64_Word)off;
3760 StgInt64 off = value - P;
3761 *(Elf64_Word *)P = (Elf64_Word)off;
3766 if (value >= 0x7fffffffL) {
3767 #if X86_64_ELF_NONPIC_HACK
3768 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
3770 value = pltAddress + A;
3772 barf("R_X86_64_32 relocation out of range: %s = %p\nRecompile %s with -fPIC.",
3773 symbol, value, oc->fileName );
3776 *(Elf64_Word *)P = (Elf64_Word)value;
3780 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
3781 #if X86_64_ELF_NONPIC_HACK
3782 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
3784 value = pltAddress + A;
3786 barf("R_X86_64_32S relocation out of range: %s = %p\nRecompile %s with -fPIC.",
3787 symbol, value, oc->fileName );
3790 *(Elf64_Sword *)P = (Elf64_Sword)value;
3793 case R_X86_64_GOTPCREL:
3795 StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
3796 StgInt64 off = gotAddress + A - P;
3797 *(Elf64_Word *)P = (Elf64_Word)off;
3801 case R_X86_64_PLT32:
3803 StgInt64 off = value - P;
3804 if (off >= 0x7fffffffL || off < -0x80000000L) {
3805 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
3807 off = pltAddress + A - P;
3809 *(Elf64_Word *)P = (Elf64_Word)off;
3815 errorBelch("%s: unhandled ELF relocation(RelA) type %lu\n",
3816 oc->fileName, (lnat)ELF_R_TYPE(info));
3825 ocResolve_ELF ( ObjectCode* oc )
3829 Elf_Sym* stab = NULL;
3830 char* ehdrC = (char*)(oc->image);
3831 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
3832 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3834 /* first find "the" symbol table */
3835 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
3837 /* also go find the string table */
3838 strtab = findElfSection ( ehdrC, SHT_STRTAB );
3840 if (stab == NULL || strtab == NULL) {
3841 errorBelch("%s: can't find string or symbol table", oc->fileName);
3845 /* Process the relocation sections. */
3846 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
3847 if (shdr[shnum].sh_type == SHT_REL) {
3848 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
3849 shnum, stab, strtab );
3853 if (shdr[shnum].sh_type == SHT_RELA) {
3854 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
3855 shnum, stab, strtab );
3860 #if defined(powerpc_HOST_ARCH)
3861 ocFlushInstructionCache( oc );
3868 * PowerPC & X86_64 ELF specifics
3871 #if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
3873 static int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
3879 ehdr = (Elf_Ehdr *) oc->image;
3880 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
3882 for( i = 0; i < ehdr->e_shnum; i++ )
3883 if( shdr[i].sh_type == SHT_SYMTAB )
3886 if( i == ehdr->e_shnum )
3888 errorBelch( "This ELF file contains no symtab" );
3892 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
3894 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
3895 (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
3900 return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
3903 #endif /* powerpc */
3907 /* --------------------------------------------------------------------------
3909 * ------------------------------------------------------------------------*/
3911 #if defined(OBJFORMAT_MACHO)
3914 Support for MachO linking on Darwin/MacOS X
3915 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3917 I hereby formally apologize for the hackish nature of this code.
3918 Things that need to be done:
3919 *) implement ocVerifyImage_MachO
3920 *) add still more sanity checks.
3923 #if x86_64_HOST_ARCH || powerpc64_HOST_ARCH
3924 #define mach_header mach_header_64
3925 #define segment_command segment_command_64
3926 #define section section_64
3927 #define nlist nlist_64
3930 #ifdef powerpc_HOST_ARCH
3931 static int ocAllocateSymbolExtras_MachO(ObjectCode* oc)
3933 struct mach_header *header = (struct mach_header *) oc->image;
3934 struct load_command *lc = (struct load_command *) (header + 1);
3937 for( i = 0; i < header->ncmds; i++ )
3939 if( lc->cmd == LC_SYMTAB )
3941 // Find out the first and last undefined external
3942 // symbol, so we don't have to allocate too many
3944 struct symtab_command *symLC = (struct symtab_command *) lc;
3945 unsigned min = symLC->nsyms, max = 0;
3946 struct nlist *nlist =
3947 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
3949 for(i=0;i<symLC->nsyms;i++)
3951 if(nlist[i].n_type & N_STAB)
3953 else if(nlist[i].n_type & N_EXT)
3955 if((nlist[i].n_type & N_TYPE) == N_UNDF
3956 && (nlist[i].n_value == 0))
3966 return ocAllocateSymbolExtras(oc, max - min + 1, min);
3971 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3973 return ocAllocateSymbolExtras(oc,0,0);
3976 #ifdef x86_64_HOST_ARCH
3977 static int ocAllocateSymbolExtras_MachO(ObjectCode* oc)
3979 struct mach_header *header = (struct mach_header *) oc->image;
3980 struct load_command *lc = (struct load_command *) (header + 1);
3983 for( i = 0; i < header->ncmds; i++ )
3985 if( lc->cmd == LC_SYMTAB )
3987 // Just allocate one entry for every symbol
3988 struct symtab_command *symLC = (struct symtab_command *) lc;
3990 return ocAllocateSymbolExtras(oc, symLC->nsyms, 0);
3993 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3995 return ocAllocateSymbolExtras(oc,0,0);
3999 static int ocVerifyImage_MachO(ObjectCode* oc)
4001 char *image = (char*) oc->image;
4002 struct mach_header *header = (struct mach_header*) image;
4004 #if x86_64_TARGET_ARCH || powerpc64_TARGET_ARCH
4005 if(header->magic != MH_MAGIC_64)
4008 if(header->magic != MH_MAGIC)
4011 // FIXME: do some more verifying here
4015 static int resolveImports(
4018 struct symtab_command *symLC,
4019 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
4020 unsigned long *indirectSyms,
4021 struct nlist *nlist)
4024 size_t itemSize = 4;
4027 int isJumpTable = 0;
4028 if(!strcmp(sect->sectname,"__jump_table"))
4032 ASSERT(sect->reserved2 == itemSize);
4036 for(i=0; i*itemSize < sect->size;i++)
4038 // according to otool, reserved1 contains the first index into the indirect symbol table
4039 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
4040 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4043 if((symbol->n_type & N_TYPE) == N_UNDF
4044 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
4045 addr = (void*) (symbol->n_value);
4047 addr = lookupSymbol(nm);
4050 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
4058 checkProddableBlock(oc,image + sect->offset + i*itemSize);
4059 *(image + sect->offset + i*itemSize) = 0xe9; // jmp
4060 *(unsigned*)(image + sect->offset + i*itemSize + 1)
4061 = (char*)addr - (image + sect->offset + i*itemSize + 5);
4066 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
4067 ((void**)(image + sect->offset))[i] = addr;
4074 static unsigned long relocateAddress(
4077 struct section* sections,
4078 unsigned long address)
4081 for(i = 0; i < nSections; i++)
4083 if(sections[i].addr <= address
4084 && address < sections[i].addr + sections[i].size)
4086 return (unsigned long)oc->image
4087 + sections[i].offset + address - sections[i].addr;
4090 barf("Invalid Mach-O file:"
4091 "Address out of bounds while relocating object file");
4095 static int relocateSection(
4098 struct symtab_command *symLC, struct nlist *nlist,
4099 int nSections, struct section* sections, struct section *sect)
4101 struct relocation_info *relocs;
4104 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
4106 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
4108 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
4110 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
4114 relocs = (struct relocation_info*) (image + sect->reloff);
4118 #ifdef x86_64_HOST_ARCH
4119 struct relocation_info *reloc = &relocs[i];
4121 char *thingPtr = image + sect->offset + reloc->r_address;
4123 /* We shouldn't need to initialise this, but gcc on OS X 64 bit
4124 complains that it may be used uninitialized if we don't */
4127 int type = reloc->r_type;
4129 checkProddableBlock(oc,thingPtr);
4130 switch(reloc->r_length)
4133 thing = *(uint8_t*)thingPtr;
4134 baseValue = (uint64_t)thingPtr + 1;
4137 thing = *(uint16_t*)thingPtr;
4138 baseValue = (uint64_t)thingPtr + 2;
4141 thing = *(uint32_t*)thingPtr;
4142 baseValue = (uint64_t)thingPtr + 4;
4145 thing = *(uint64_t*)thingPtr;
4146 baseValue = (uint64_t)thingPtr + 8;
4149 barf("Unknown size.");
4152 if(type == X86_64_RELOC_GOT
4153 || type == X86_64_RELOC_GOT_LOAD)
4155 ASSERT(reloc->r_extern);
4156 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, value)->addr;
4158 type = X86_64_RELOC_SIGNED;
4160 else if(reloc->r_extern)
4162 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4163 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4164 if(symbol->n_value == 0)
4165 value = (uint64_t) lookupSymbol(nm);
4167 value = relocateAddress(oc, nSections, sections,
4172 value = sections[reloc->r_symbolnum-1].offset
4173 - sections[reloc->r_symbolnum-1].addr
4177 if(type == X86_64_RELOC_BRANCH)
4179 if((int32_t)(value - baseValue) != (int64_t)(value - baseValue))
4181 ASSERT(reloc->r_extern);
4182 value = (uint64_t) &makeSymbolExtra(oc, reloc->r_symbolnum, value)
4185 ASSERT((int32_t)(value - baseValue) == (int64_t)(value - baseValue));
4186 type = X86_64_RELOC_SIGNED;
4191 case X86_64_RELOC_UNSIGNED:
4192 ASSERT(!reloc->r_pcrel);
4195 case X86_64_RELOC_SIGNED:
4196 ASSERT(reloc->r_pcrel);
4197 thing += value - baseValue;
4199 case X86_64_RELOC_SUBTRACTOR:
4200 ASSERT(!reloc->r_pcrel);
4204 barf("unkown relocation");
4207 switch(reloc->r_length)
4210 *(uint8_t*)thingPtr = thing;
4213 *(uint16_t*)thingPtr = thing;
4216 *(uint32_t*)thingPtr = thing;
4219 *(uint64_t*)thingPtr = thing;
4223 if(relocs[i].r_address & R_SCATTERED)
4225 struct scattered_relocation_info *scat =
4226 (struct scattered_relocation_info*) &relocs[i];
4230 if(scat->r_length == 2)
4232 unsigned long word = 0;
4233 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
4234 checkProddableBlock(oc,wordPtr);
4236 // Note on relocation types:
4237 // i386 uses the GENERIC_RELOC_* types,
4238 // while ppc uses special PPC_RELOC_* types.
4239 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
4240 // in both cases, all others are different.
4241 // Therefore, we use GENERIC_RELOC_VANILLA
4242 // and GENERIC_RELOC_PAIR instead of the PPC variants,
4243 // and use #ifdefs for the other types.
4245 // Step 1: Figure out what the relocated value should be
4246 if(scat->r_type == GENERIC_RELOC_VANILLA)
4248 word = *wordPtr + (unsigned long) relocateAddress(
4255 #ifdef powerpc_HOST_ARCH
4256 else if(scat->r_type == PPC_RELOC_SECTDIFF
4257 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
4258 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
4259 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
4261 else if(scat->r_type == GENERIC_RELOC_SECTDIFF
4262 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4265 struct scattered_relocation_info *pair =
4266 (struct scattered_relocation_info*) &relocs[i+1];
4268 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
4269 barf("Invalid Mach-O file: "
4270 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
4272 word = (unsigned long)
4273 (relocateAddress(oc, nSections, sections, scat->r_value)
4274 - relocateAddress(oc, nSections, sections, pair->r_value));
4277 #ifdef powerpc_HOST_ARCH
4278 else if(scat->r_type == PPC_RELOC_HI16
4279 || scat->r_type == PPC_RELOC_LO16
4280 || scat->r_type == PPC_RELOC_HA16
4281 || scat->r_type == PPC_RELOC_LO14)
4282 { // these are generated by label+offset things
4283 struct relocation_info *pair = &relocs[i+1];
4284 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
4285 barf("Invalid Mach-O file: "
4286 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
4288 if(scat->r_type == PPC_RELOC_LO16)
4290 word = ((unsigned short*) wordPtr)[1];
4291 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4293 else if(scat->r_type == PPC_RELOC_LO14)
4295 barf("Unsupported Relocation: PPC_RELOC_LO14");
4296 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
4297 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4299 else if(scat->r_type == PPC_RELOC_HI16)
4301 word = ((unsigned short*) wordPtr)[1] << 16;
4302 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4304 else if(scat->r_type == PPC_RELOC_HA16)
4306 word = ((unsigned short*) wordPtr)[1] << 16;
4307 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4311 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
4319 barf ("Don't know how to handle this Mach-O "
4320 "scattered relocation entry: "
4321 "object file %s; entry type %ld; "
4323 oc->fileName, scat->r_type, scat->r_address);
4327 #ifdef powerpc_HOST_ARCH
4328 if(scat->r_type == GENERIC_RELOC_VANILLA
4329 || scat->r_type == PPC_RELOC_SECTDIFF)
4331 if(scat->r_type == GENERIC_RELOC_VANILLA
4332 || scat->r_type == GENERIC_RELOC_SECTDIFF
4333 || scat->r_type == GENERIC_RELOC_LOCAL_SECTDIFF)
4338 #ifdef powerpc_HOST_ARCH
4339 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
4341 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
4343 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
4345 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
4347 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
4349 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
4350 + ((word & (1<<15)) ? 1 : 0);
4356 barf("Can't handle Mach-O scattered relocation entry "
4357 "with this r_length tag: "
4358 "object file %s; entry type %ld; "
4359 "r_length tag %ld; address %#lx\n",
4360 oc->fileName, scat->r_type, scat->r_length,
4365 else /* scat->r_pcrel */
4367 barf("Don't know how to handle *PC-relative* Mach-O "
4368 "scattered relocation entry: "
4369 "object file %s; entry type %ld; address %#lx\n",
4370 oc->fileName, scat->r_type, scat->r_address);
4375 else /* !(relocs[i].r_address & R_SCATTERED) */
4377 struct relocation_info *reloc = &relocs[i];
4378 if(reloc->r_pcrel && !reloc->r_extern)
4381 if(reloc->r_length == 2)
4383 unsigned long word = 0;
4384 #ifdef powerpc_HOST_ARCH
4385 unsigned long jumpIsland = 0;
4386 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
4387 // to avoid warning and to catch
4391 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
4392 checkProddableBlock(oc,wordPtr);
4394 if(reloc->r_type == GENERIC_RELOC_VANILLA)
4398 #ifdef powerpc_HOST_ARCH
4399 else if(reloc->r_type == PPC_RELOC_LO16)
4401 word = ((unsigned short*) wordPtr)[1];
4402 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
4404 else if(reloc->r_type == PPC_RELOC_HI16)
4406 word = ((unsigned short*) wordPtr)[1] << 16;
4407 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
4409 else if(reloc->r_type == PPC_RELOC_HA16)
4411 word = ((unsigned short*) wordPtr)[1] << 16;
4412 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
4414 else if(reloc->r_type == PPC_RELOC_BR24)
4417 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
4422 barf("Can't handle this Mach-O relocation entry "
4424 "object file %s; entry type %ld; address %#lx\n",
4425 oc->fileName, reloc->r_type, reloc->r_address);
4429 if(!reloc->r_extern)
4432 sections[reloc->r_symbolnum-1].offset
4433 - sections[reloc->r_symbolnum-1].addr
4440 struct nlist *symbol = &nlist[reloc->r_symbolnum];
4441 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
4442 void *symbolAddress = lookupSymbol(nm);
4445 errorBelch("\nunknown symbol `%s'", nm);
4451 #ifdef powerpc_HOST_ARCH
4452 // In the .o file, this should be a relative jump to NULL
4453 // and we'll change it to a relative jump to the symbol
4454 ASSERT(word + reloc->r_address == 0);
4455 jumpIsland = (unsigned long)
4456 &makeSymbolExtra(oc,
4458 (unsigned long) symbolAddress)
4462 offsetToJumpIsland = word + jumpIsland
4463 - (((long)image) + sect->offset - sect->addr);
4466 word += (unsigned long) symbolAddress
4467 - (((long)image) + sect->offset - sect->addr);
4471 word += (unsigned long) symbolAddress;
4475 if(reloc->r_type == GENERIC_RELOC_VANILLA)
4480 #ifdef powerpc_HOST_ARCH
4481 else if(reloc->r_type == PPC_RELOC_LO16)
4483 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
4486 else if(reloc->r_type == PPC_RELOC_HI16)
4488 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
4491 else if(reloc->r_type == PPC_RELOC_HA16)
4493 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
4494 + ((word & (1<<15)) ? 1 : 0);
4497 else if(reloc->r_type == PPC_RELOC_BR24)
4499 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
4501 // The branch offset is too large.
4502 // Therefore, we try to use a jump island.
4505 barf("unconditional relative branch out of range: "
4506 "no jump island available");
4509 word = offsetToJumpIsland;
4510 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
4511 barf("unconditional relative branch out of range: "
4512 "jump island out of range");
4514 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
4521 barf("Can't handle Mach-O relocation entry (not scattered) "
4522 "with this r_length tag: "
4523 "object file %s; entry type %ld; "
4524 "r_length tag %ld; address %#lx\n",
4525 oc->fileName, reloc->r_type, reloc->r_length,
4535 static int ocGetNames_MachO(ObjectCode* oc)
4537 char *image = (char*) oc->image;
4538 struct mach_header *header = (struct mach_header*) image;
4539 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
4540 unsigned i,curSymbol = 0;
4541 struct segment_command *segLC = NULL;
4542 struct section *sections;
4543 struct symtab_command *symLC = NULL;
4544 struct nlist *nlist;
4545 unsigned long commonSize = 0;
4546 char *commonStorage = NULL;
4547 unsigned long commonCounter;
4549 for(i=0;i<header->ncmds;i++)
4551 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
4552 segLC = (struct segment_command*) lc;
4553 else if(lc->cmd == LC_SYMTAB)
4554 symLC = (struct symtab_command*) lc;
4555 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4558 sections = (struct section*) (segLC+1);
4559 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4563 barf("ocGetNames_MachO: no segment load command");
4565 for(i=0;i<segLC->nsects;i++)
4567 if(sections[i].size == 0)
4570 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
4572 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
4573 "ocGetNames_MachO(common symbols)");
4574 sections[i].offset = zeroFillArea - image;
4577 if(!strcmp(sections[i].sectname,"__text"))
4578 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
4579 (void*) (image + sections[i].offset),
4580 (void*) (image + sections[i].offset + sections[i].size));
4581 else if(!strcmp(sections[i].sectname,"__const"))
4582 addSection(oc, SECTIONKIND_RWDATA,
4583 (void*) (image + sections[i].offset),
4584 (void*) (image + sections[i].offset + sections[i].size));
4585 else if(!strcmp(sections[i].sectname,"__data"))
4586 addSection(oc, SECTIONKIND_RWDATA,
4587 (void*) (image + sections[i].offset),
4588 (void*) (image + sections[i].offset + sections[i].size));
4589 else if(!strcmp(sections[i].sectname,"__bss")
4590 || !strcmp(sections[i].sectname,"__common"))
4591 addSection(oc, SECTIONKIND_RWDATA,
4592 (void*) (image + sections[i].offset),
4593 (void*) (image + sections[i].offset + sections[i].size));
4595 addProddableBlock(oc, (void*) (image + sections[i].offset),
4599 // count external symbols defined here
4603 for(i=0;i<symLC->nsyms;i++)
4605 if(nlist[i].n_type & N_STAB)
4607 else if(nlist[i].n_type & N_EXT)
4609 if((nlist[i].n_type & N_TYPE) == N_UNDF
4610 && (nlist[i].n_value != 0))
4612 commonSize += nlist[i].n_value;
4615 else if((nlist[i].n_type & N_TYPE) == N_SECT)
4620 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
4621 "ocGetNames_MachO(oc->symbols)");
4625 for(i=0;i<symLC->nsyms;i++)
4627 if(nlist[i].n_type & N_STAB)
4629 else if((nlist[i].n_type & N_TYPE) == N_SECT)
4631 if(nlist[i].n_type & N_EXT)
4633 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4634 if((nlist[i].n_desc & N_WEAK_DEF) && lookupSymbol(nm))
4635 ; // weak definition, and we already have a definition
4638 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4640 + sections[nlist[i].n_sect-1].offset
4641 - sections[nlist[i].n_sect-1].addr
4642 + nlist[i].n_value);
4643 oc->symbols[curSymbol++] = nm;
4650 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
4651 commonCounter = (unsigned long)commonStorage;
4654 for(i=0;i<symLC->nsyms;i++)
4656 if((nlist[i].n_type & N_TYPE) == N_UNDF
4657 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
4659 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4660 unsigned long sz = nlist[i].n_value;
4662 nlist[i].n_value = commonCounter;
4664 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4665 (void*)commonCounter);
4666 oc->symbols[curSymbol++] = nm;
4668 commonCounter += sz;
4675 static int ocResolve_MachO(ObjectCode* oc)
4677 char *image = (char*) oc->image;
4678 struct mach_header *header = (struct mach_header*) image;
4679 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
4681 struct segment_command *segLC = NULL;
4682 struct section *sections;
4683 struct symtab_command *symLC = NULL;
4684 struct dysymtab_command *dsymLC = NULL;
4685 struct nlist *nlist;
4687 for(i=0;i<header->ncmds;i++)
4689 if(lc->cmd == LC_SEGMENT || lc->cmd == LC_SEGMENT_64)
4690 segLC = (struct segment_command*) lc;
4691 else if(lc->cmd == LC_SYMTAB)
4692 symLC = (struct symtab_command*) lc;
4693 else if(lc->cmd == LC_DYSYMTAB)
4694 dsymLC = (struct dysymtab_command*) lc;
4695 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4698 sections = (struct section*) (segLC+1);
4699 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4704 unsigned long *indirectSyms
4705 = (unsigned long*) (image + dsymLC->indirectsymoff);
4707 for(i=0;i<segLC->nsects;i++)
4709 if( !strcmp(sections[i].sectname,"__la_symbol_ptr")
4710 || !strcmp(sections[i].sectname,"__la_sym_ptr2")
4711 || !strcmp(sections[i].sectname,"__la_sym_ptr3"))
4713 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
4716 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr")
4717 || !strcmp(sections[i].sectname,"__pointers"))
4719 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
4722 else if(!strcmp(sections[i].sectname,"__jump_table"))
4724 if(!resolveImports(oc,image,symLC,§ions[i],indirectSyms,nlist))
4730 for(i=0;i<segLC->nsects;i++)
4732 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
4736 #if defined (powerpc_HOST_ARCH)
4737 ocFlushInstructionCache( oc );
4743 #ifdef powerpc_HOST_ARCH
4745 * The Mach-O object format uses leading underscores. But not everywhere.
4746 * There is a small number of runtime support functions defined in
4747 * libcc_dynamic.a whose name does not have a leading underscore.
4748 * As a consequence, we can't get their address from C code.
4749 * We have to use inline assembler just to take the address of a function.
4753 static void machoInitSymbolsWithoutUnderscore()
4755 extern void* symbolsWithoutUnderscore[];
4756 void **p = symbolsWithoutUnderscore;
4757 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
4759 #undef SymI_NeedsProto
4760 #define SymI_NeedsProto(x) \
4761 __asm__ volatile(".long " # x);
4763 RTS_MACHO_NOUNDERLINE_SYMBOLS
4765 __asm__ volatile(".text");
4767 #undef SymI_NeedsProto
4768 #define SymI_NeedsProto(x) \
4769 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
4771 RTS_MACHO_NOUNDERLINE_SYMBOLS
4773 #undef SymI_NeedsProto
4778 * Figure out by how much to shift the entire Mach-O file in memory
4779 * when loading so that its single segment ends up 16-byte-aligned
4781 static int machoGetMisalignment( FILE * f )
4783 struct mach_header header;
4786 fread(&header, sizeof(header), 1, f);
4789 #if x86_64_TARGET_ARCH || powerpc64_TARGET_ARCH
4790 if(header.magic != MH_MAGIC_64)
4793 if(header.magic != MH_MAGIC)
4797 misalignment = (header.sizeofcmds + sizeof(header))
4800 return misalignment ? (16 - misalignment) : 0;