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>.
25 #include "LinkerInternals.h"
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
41 #if defined(HAVE_DLFCN_H)
45 #if defined(cygwin32_HOST_OS)
50 #ifdef HAVE_SYS_TIME_H
54 #include <sys/fcntl.h>
55 #include <sys/termios.h>
56 #include <sys/utime.h>
57 #include <sys/utsname.h>
61 #if defined(ia64_HOST_ARCH) || defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS)
66 #if defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS)
74 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
75 # define OBJFORMAT_ELF
76 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
77 # define OBJFORMAT_PEi386
80 #elif defined(darwin_HOST_OS)
81 # define OBJFORMAT_MACHO
82 # include <mach-o/loader.h>
83 # include <mach-o/nlist.h>
84 # include <mach-o/reloc.h>
85 # include <mach-o/dyld.h>
86 #if defined(powerpc_HOST_ARCH)
87 # include <mach-o/ppc/reloc.h>
91 /* Hash table mapping symbol names to Symbol */
92 static /*Str*/HashTable *symhash;
94 /* List of currently loaded objects */
95 ObjectCode *objects = NULL; /* initially empty */
97 #if defined(OBJFORMAT_ELF)
98 static int ocVerifyImage_ELF ( ObjectCode* oc );
99 static int ocGetNames_ELF ( ObjectCode* oc );
100 static int ocResolve_ELF ( ObjectCode* oc );
101 #if defined(powerpc_HOST_ARCH)
102 static int ocAllocateJumpIslands_ELF ( ObjectCode* oc );
104 #elif defined(OBJFORMAT_PEi386)
105 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
106 static int ocGetNames_PEi386 ( ObjectCode* oc );
107 static int ocResolve_PEi386 ( ObjectCode* oc );
108 #elif defined(OBJFORMAT_MACHO)
109 static int ocVerifyImage_MachO ( ObjectCode* oc );
110 static int ocGetNames_MachO ( ObjectCode* oc );
111 static int ocResolve_MachO ( ObjectCode* oc );
113 static int machoGetMisalignment( FILE * );
114 #ifdef powerpc_HOST_ARCH
115 static int ocAllocateJumpIslands_MachO ( ObjectCode* oc );
116 static void machoInitSymbolsWithoutUnderscore( void );
120 #if defined(x86_64_HOST_ARCH)
121 static void*x86_64_high_symbol( char *lbl, void *addr );
124 /* -----------------------------------------------------------------------------
125 * Built-in symbols from the RTS
128 typedef struct _RtsSymbolVal {
135 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
136 SymX(makeStableNamezh_fast) \
137 SymX(finalizzeWeakzh_fast)
139 /* These are not available in GUM!!! -- HWL */
140 #define Maybe_Stable_Names
143 #if !defined (mingw32_HOST_OS)
144 #define RTS_POSIX_ONLY_SYMBOLS \
145 SymX(stg_sig_install) \
149 #if defined (cygwin32_HOST_OS)
150 #define RTS_MINGW_ONLY_SYMBOLS /**/
151 /* Don't have the ability to read import libs / archives, so
152 * we have to stupidly list a lot of what libcygwin.a
155 #define RTS_CYGWIN_ONLY_SYMBOLS \
233 #elif !defined(mingw32_HOST_OS)
234 #define RTS_MINGW_ONLY_SYMBOLS /**/
235 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
236 #else /* defined(mingw32_HOST_OS) */
237 #define RTS_POSIX_ONLY_SYMBOLS /**/
238 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
240 /* Extra syms gen'ed by mingw-2's gcc-3.2: */
242 #define RTS_MINGW_EXTRA_SYMS \
243 Sym(_imp____mb_cur_max) \
246 #define RTS_MINGW_EXTRA_SYMS
249 /* These are statically linked from the mingw libraries into the ghc
250 executable, so we have to employ this hack. */
251 #define RTS_MINGW_ONLY_SYMBOLS \
252 SymX(asyncReadzh_fast) \
253 SymX(asyncWritezh_fast) \
254 SymX(asyncDoProczh_fast) \
266 SymX(getservbyname) \
267 SymX(getservbyport) \
268 SymX(getprotobynumber) \
269 SymX(getprotobyname) \
270 SymX(gethostbyname) \
271 SymX(gethostbyaddr) \
305 SymX(rts_InstallConsoleEvent) \
306 SymX(rts_ConsoleHandlerDone) \
308 Sym(_imp___timezone) \
317 RTS_MINGW_EXTRA_SYMS \
321 #if defined(darwin_TARGET_OS) && HAVE_PRINTF_LDBLSTUB
322 #define RTS_DARWIN_ONLY_SYMBOLS \
323 Sym(asprintf$LDBLStub) \
327 Sym(fprintf$LDBLStub) \
328 Sym(fscanf$LDBLStub) \
329 Sym(fwprintf$LDBLStub) \
330 Sym(fwscanf$LDBLStub) \
331 Sym(printf$LDBLStub) \
332 Sym(scanf$LDBLStub) \
333 Sym(snprintf$LDBLStub) \
334 Sym(sprintf$LDBLStub) \
335 Sym(sscanf$LDBLStub) \
336 Sym(strtold$LDBLStub) \
337 Sym(swprintf$LDBLStub) \
338 Sym(swscanf$LDBLStub) \
339 Sym(syslog$LDBLStub) \
340 Sym(vasprintf$LDBLStub) \
342 Sym(verrc$LDBLStub) \
343 Sym(verrx$LDBLStub) \
344 Sym(vfprintf$LDBLStub) \
345 Sym(vfscanf$LDBLStub) \
346 Sym(vfwprintf$LDBLStub) \
347 Sym(vfwscanf$LDBLStub) \
348 Sym(vprintf$LDBLStub) \
349 Sym(vscanf$LDBLStub) \
350 Sym(vsnprintf$LDBLStub) \
351 Sym(vsprintf$LDBLStub) \
352 Sym(vsscanf$LDBLStub) \
353 Sym(vswprintf$LDBLStub) \
354 Sym(vswscanf$LDBLStub) \
355 Sym(vsyslog$LDBLStub) \
356 Sym(vwarn$LDBLStub) \
357 Sym(vwarnc$LDBLStub) \
358 Sym(vwarnx$LDBLStub) \
359 Sym(vwprintf$LDBLStub) \
360 Sym(vwscanf$LDBLStub) \
362 Sym(warnc$LDBLStub) \
363 Sym(warnx$LDBLStub) \
364 Sym(wcstold$LDBLStub) \
365 Sym(wprintf$LDBLStub) \
368 #define RTS_DARWIN_ONLY_SYMBOLS
372 # define MAIN_CAP_SYM SymX(MainCapability)
374 # define MAIN_CAP_SYM
377 #if !defined(mingw32_HOST_OS)
378 #define RTS_USER_SIGNALS_SYMBOLS \
379 SymX(setIOManagerPipe)
381 #define RTS_USER_SIGNALS_SYMBOLS /* nothing */
384 #ifdef TABLES_NEXT_TO_CODE
385 #define RTS_RET_SYMBOLS /* nothing */
387 #define RTS_RET_SYMBOLS \
388 SymX(stg_enter_ret) \
389 SymX(stg_gc_fun_ret) \
397 SymX(stg_ap_pv_ret) \
398 SymX(stg_ap_pp_ret) \
399 SymX(stg_ap_ppv_ret) \
400 SymX(stg_ap_ppp_ret) \
401 SymX(stg_ap_pppv_ret) \
402 SymX(stg_ap_pppp_ret) \
403 SymX(stg_ap_ppppp_ret) \
404 SymX(stg_ap_pppppp_ret)
407 #define RTS_SYMBOLS \
410 SymX(stg_enter_info) \
411 SymX(stg_gc_void_info) \
412 SymX(__stg_gc_enter_1) \
413 SymX(stg_gc_noregs) \
414 SymX(stg_gc_unpt_r1_info) \
415 SymX(stg_gc_unpt_r1) \
416 SymX(stg_gc_unbx_r1_info) \
417 SymX(stg_gc_unbx_r1) \
418 SymX(stg_gc_f1_info) \
420 SymX(stg_gc_d1_info) \
422 SymX(stg_gc_l1_info) \
425 SymX(stg_gc_fun_info) \
427 SymX(stg_gc_gen_info) \
428 SymX(stg_gc_gen_hp) \
430 SymX(stg_gen_yield) \
431 SymX(stg_yield_noregs) \
432 SymX(stg_yield_to_interpreter) \
433 SymX(stg_gen_block) \
434 SymX(stg_block_noregs) \
436 SymX(stg_block_takemvar) \
437 SymX(stg_block_putmvar) \
438 SymX(stg_seq_frame_info) \
440 SymX(MallocFailHook) \
442 SymX(OutOfHeapHook) \
443 SymX(StackOverflowHook) \
444 SymX(__encodeDouble) \
445 SymX(__encodeFloat) \
449 SymX(__gmpz_cmp_si) \
450 SymX(__gmpz_cmp_ui) \
451 SymX(__gmpz_get_si) \
452 SymX(__gmpz_get_ui) \
453 SymX(__int_encodeDouble) \
454 SymX(__int_encodeFloat) \
455 SymX(andIntegerzh_fast) \
456 SymX(atomicallyzh_fast) \
460 SymX(blockAsyncExceptionszh_fast) \
462 SymX(catchRetryzh_fast) \
463 SymX(catchSTMzh_fast) \
464 SymX(closure_flags) \
466 SymX(cmpIntegerzh_fast) \
467 SymX(cmpIntegerIntzh_fast) \
468 SymX(complementIntegerzh_fast) \
469 SymX(createAdjustor) \
470 SymX(decodeDoublezh_fast) \
471 SymX(decodeFloatzh_fast) \
474 SymX(deRefWeakzh_fast) \
475 SymX(deRefStablePtrzh_fast) \
476 SymX(divExactIntegerzh_fast) \
477 SymX(divModIntegerzh_fast) \
480 SymX(forkOS_createThread) \
481 SymX(freeHaskellFunctionPtr) \
482 SymX(freeStablePtr) \
483 SymX(gcdIntegerzh_fast) \
484 SymX(gcdIntegerIntzh_fast) \
485 SymX(gcdIntzh_fast) \
494 SymX(hs_perform_gc) \
495 SymX(hs_free_stable_ptr) \
496 SymX(hs_free_fun_ptr) \
498 SymX(int2Integerzh_fast) \
499 SymX(integer2Intzh_fast) \
500 SymX(integer2Wordzh_fast) \
501 SymX(isCurrentThreadBoundzh_fast) \
502 SymX(isDoubleDenormalized) \
503 SymX(isDoubleInfinite) \
505 SymX(isDoubleNegativeZero) \
506 SymX(isEmptyMVarzh_fast) \
507 SymX(isFloatDenormalized) \
508 SymX(isFloatInfinite) \
510 SymX(isFloatNegativeZero) \
511 SymX(killThreadzh_fast) \
514 SymX(makeStablePtrzh_fast) \
515 SymX(minusIntegerzh_fast) \
516 SymX(mkApUpd0zh_fast) \
517 SymX(myThreadIdzh_fast) \
518 SymX(labelThreadzh_fast) \
519 SymX(newArrayzh_fast) \
520 SymX(newBCOzh_fast) \
521 SymX(newByteArrayzh_fast) \
522 SymX_redirect(newCAF, newDynCAF) \
523 SymX(newMVarzh_fast) \
524 SymX(newMutVarzh_fast) \
525 SymX(newTVarzh_fast) \
526 SymX(atomicModifyMutVarzh_fast) \
527 SymX(newPinnedByteArrayzh_fast) \
528 SymX(orIntegerzh_fast) \
530 SymX(performMajorGC) \
531 SymX(plusIntegerzh_fast) \
534 SymX(putMVarzh_fast) \
535 SymX(quotIntegerzh_fast) \
536 SymX(quotRemIntegerzh_fast) \
538 SymX(raiseIOzh_fast) \
539 SymX(readTVarzh_fast) \
540 SymX(remIntegerzh_fast) \
541 SymX(resetNonBlockingFd) \
546 SymX(rts_checkSchedStatus) \
549 SymX(rts_evalLazyIO) \
550 SymX(rts_evalStableIO) \
554 SymX(rts_getDouble) \
559 SymX(rts_getFunPtr) \
560 SymX(rts_getStablePtr) \
561 SymX(rts_getThreadId) \
563 SymX(rts_getWord32) \
576 SymX(rts_mkStablePtr) \
584 SymX(rtsSupportsBoundThreads) \
585 SymX(__hscore_get_saved_termios) \
586 SymX(__hscore_set_saved_termios) \
588 SymX(startupHaskell) \
589 SymX(shutdownHaskell) \
590 SymX(shutdownHaskellAndExit) \
591 SymX(stable_ptr_table) \
592 SymX(stackOverflow) \
593 SymX(stg_CAF_BLACKHOLE_info) \
594 SymX(awakenBlockedQueue) \
595 SymX(stg_CHARLIKE_closure) \
596 SymX(stg_EMPTY_MVAR_info) \
597 SymX(stg_IND_STATIC_info) \
598 SymX(stg_INTLIKE_closure) \
599 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
600 SymX(stg_MUT_ARR_PTRS_FROZEN0_info) \
601 SymX(stg_WEAK_info) \
602 SymX(stg_ap_0_info) \
603 SymX(stg_ap_v_info) \
604 SymX(stg_ap_f_info) \
605 SymX(stg_ap_d_info) \
606 SymX(stg_ap_l_info) \
607 SymX(stg_ap_n_info) \
608 SymX(stg_ap_p_info) \
609 SymX(stg_ap_pv_info) \
610 SymX(stg_ap_pp_info) \
611 SymX(stg_ap_ppv_info) \
612 SymX(stg_ap_ppp_info) \
613 SymX(stg_ap_pppv_info) \
614 SymX(stg_ap_pppp_info) \
615 SymX(stg_ap_ppppp_info) \
616 SymX(stg_ap_pppppp_info) \
617 SymX(stg_ap_1_upd_info) \
618 SymX(stg_ap_2_upd_info) \
619 SymX(stg_ap_3_upd_info) \
620 SymX(stg_ap_4_upd_info) \
621 SymX(stg_ap_5_upd_info) \
622 SymX(stg_ap_6_upd_info) \
623 SymX(stg_ap_7_upd_info) \
625 SymX(stg_sel_0_upd_info) \
626 SymX(stg_sel_10_upd_info) \
627 SymX(stg_sel_11_upd_info) \
628 SymX(stg_sel_12_upd_info) \
629 SymX(stg_sel_13_upd_info) \
630 SymX(stg_sel_14_upd_info) \
631 SymX(stg_sel_15_upd_info) \
632 SymX(stg_sel_1_upd_info) \
633 SymX(stg_sel_2_upd_info) \
634 SymX(stg_sel_3_upd_info) \
635 SymX(stg_sel_4_upd_info) \
636 SymX(stg_sel_5_upd_info) \
637 SymX(stg_sel_6_upd_info) \
638 SymX(stg_sel_7_upd_info) \
639 SymX(stg_sel_8_upd_info) \
640 SymX(stg_sel_9_upd_info) \
641 SymX(stg_upd_frame_info) \
642 SymX(suspendThread) \
643 SymX(takeMVarzh_fast) \
644 SymX(timesIntegerzh_fast) \
645 SymX(tryPutMVarzh_fast) \
646 SymX(tryTakeMVarzh_fast) \
647 SymX(unblockAsyncExceptionszh_fast) \
649 SymX(unsafeThawArrayzh_fast) \
650 SymX(waitReadzh_fast) \
651 SymX(waitWritezh_fast) \
652 SymX(word2Integerzh_fast) \
653 SymX(writeTVarzh_fast) \
654 SymX(xorIntegerzh_fast) \
656 RTS_USER_SIGNALS_SYMBOLS
658 #ifdef SUPPORT_LONG_LONGS
659 #define RTS_LONG_LONG_SYMS \
660 SymX(int64ToIntegerzh_fast) \
661 SymX(word64ToIntegerzh_fast)
663 #define RTS_LONG_LONG_SYMS /* nothing */
666 // 64-bit support functions in libgcc.a
667 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
668 #define RTS_LIBGCC_SYMBOLS \
678 #elif defined(ia64_HOST_ARCH)
679 #define RTS_LIBGCC_SYMBOLS \
687 #define RTS_LIBGCC_SYMBOLS
690 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
691 // Symbols that don't have a leading underscore
692 // on Mac OS X. They have to receive special treatment,
693 // see machoInitSymbolsWithoutUnderscore()
694 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
699 /* entirely bogus claims about types of these symbols */
700 #define Sym(vvv) extern void vvv(void);
701 #define SymX(vvv) /**/
702 #define SymX_redirect(vvv,xxx) /**/
706 RTS_POSIX_ONLY_SYMBOLS
707 RTS_MINGW_ONLY_SYMBOLS
708 RTS_CYGWIN_ONLY_SYMBOLS
709 RTS_DARWIN_ONLY_SYMBOLS
715 #ifdef LEADING_UNDERSCORE
716 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
718 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
721 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
723 #define SymX(vvv) Sym(vvv)
725 // SymX_redirect allows us to redirect references to one symbol to
726 // another symbol. See newCAF/newDynCAF for an example.
727 #define SymX_redirect(vvv,xxx) \
728 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
731 static RtsSymbolVal rtsSyms[] = {
735 RTS_POSIX_ONLY_SYMBOLS
736 RTS_MINGW_ONLY_SYMBOLS
737 RTS_CYGWIN_ONLY_SYMBOLS
739 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
740 // dyld stub code contains references to this,
741 // but it should never be called because we treat
742 // lazy pointers as nonlazy.
743 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
745 { 0, 0 } /* sentinel */
748 /* -----------------------------------------------------------------------------
749 * Insert symbols into hash tables, checking for duplicates.
751 static void ghciInsertStrHashTable ( char* obj_name,
757 if (lookupHashTable(table, (StgWord)key) == NULL)
759 insertStrHashTable(table, (StgWord)key, data);
764 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
766 "whilst processing object file\n"
768 "This could be caused by:\n"
769 " * Loading two different object files which export the same symbol\n"
770 " * Specifying the same object file twice on the GHCi command line\n"
771 " * An incorrect `package.conf' entry, causing some object to be\n"
773 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
782 /* -----------------------------------------------------------------------------
783 * initialize the object linker
787 static int linker_init_done = 0 ;
789 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
790 static void *dl_prog_handle;
793 /* dlopen(NULL,..) doesn't work so we grab libc explicitly */
794 #if defined(openbsd_HOST_OS)
795 static void *dl_libc_handle;
803 /* Make initLinker idempotent, so we can call it
804 before evey relevant operation; that means we
805 don't need to initialise the linker separately */
806 if (linker_init_done == 1) { return; } else {
807 linker_init_done = 1;
810 symhash = allocStrHashTable();
812 /* populate the symbol table with stuff from the RTS */
813 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
814 ghciInsertStrHashTable("(GHCi built-in symbols)",
815 symhash, sym->lbl, sym->addr);
817 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
818 machoInitSymbolsWithoutUnderscore();
821 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
822 # if defined(RTLD_DEFAULT)
823 dl_prog_handle = RTLD_DEFAULT;
825 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
826 # if defined(openbsd_HOST_OS)
827 dl_libc_handle = dlopen("libc.so", RTLD_LAZY);
829 # endif /* RTLD_DEFAULT */
833 /* -----------------------------------------------------------------------------
834 * Loading DLL or .so dynamic libraries
835 * -----------------------------------------------------------------------------
837 * Add a DLL from which symbols may be found. In the ELF case, just
838 * do RTLD_GLOBAL-style add, so no further messing around needs to
839 * happen in order that symbols in the loaded .so are findable --
840 * lookupSymbol() will subsequently see them by dlsym on the program's
841 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
843 * In the PEi386 case, open the DLLs and put handles to them in a
844 * linked list. When looking for a symbol, try all handles in the
845 * list. This means that we need to load even DLLs that are guaranteed
846 * to be in the ghc.exe image already, just so we can get a handle
847 * to give to loadSymbol, so that we can find the symbols. For such
848 * libraries, the LoadLibrary call should be a no-op except for returning
853 #if defined(OBJFORMAT_PEi386)
854 /* A record for storing handles into DLLs. */
859 struct _OpenedDLL* next;
864 /* A list thereof. */
865 static OpenedDLL* opened_dlls = NULL;
869 addDLL( char *dll_name )
871 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
872 /* ------------------- ELF DLL loader ------------------- */
878 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
881 /* dlopen failed; return a ptr to the error msg. */
883 if (errmsg == NULL) errmsg = "addDLL: unknown error";
890 # elif defined(OBJFORMAT_PEi386)
891 /* ------------------- Win32 DLL loader ------------------- */
899 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
901 /* See if we've already got it, and ignore if so. */
902 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
903 if (0 == strcmp(o_dll->name, dll_name))
907 /* The file name has no suffix (yet) so that we can try
908 both foo.dll and foo.drv
910 The documentation for LoadLibrary says:
911 If no file name extension is specified in the lpFileName
912 parameter, the default library extension .dll is
913 appended. However, the file name string can include a trailing
914 point character (.) to indicate that the module name has no
917 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
918 sprintf(buf, "%s.DLL", dll_name);
919 instance = LoadLibrary(buf);
920 if (instance == NULL) {
921 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
922 instance = LoadLibrary(buf);
923 if (instance == NULL) {
926 /* LoadLibrary failed; return a ptr to the error msg. */
927 return "addDLL: unknown error";
932 /* Add this DLL to the list of DLLs in which to search for symbols. */
933 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
934 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
935 strcpy(o_dll->name, dll_name);
936 o_dll->instance = instance;
937 o_dll->next = opened_dlls;
942 barf("addDLL: not implemented on this platform");
946 /* -----------------------------------------------------------------------------
947 * lookup a symbol in the hash table
950 lookupSymbol( char *lbl )
954 ASSERT(symhash != NULL);
955 val = lookupStrHashTable(symhash, lbl);
958 # if defined(OBJFORMAT_ELF)
959 # if defined(openbsd_HOST_OS)
960 val = dlsym(dl_prog_handle, lbl);
961 return (val != NULL) ? val : dlsym(dl_libc_handle,lbl);
962 # elif defined(x86_64_HOST_ARCH)
963 val = dlsym(dl_prog_handle, lbl);
964 if (val >= (void *)0x80000000) {
966 new_val = x86_64_high_symbol(lbl, val);
967 IF_DEBUG(linker,debugBelch("lookupSymbol: relocating out of range symbol: %s = %p, now %p\n", lbl, val, new_val));
972 # else /* not openbsd */
973 return dlsym(dl_prog_handle, lbl);
975 # elif defined(OBJFORMAT_MACHO)
976 if(NSIsSymbolNameDefined(lbl)) {
977 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
978 return NSAddressOfSymbol(symbol);
982 # elif defined(OBJFORMAT_PEi386)
985 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
986 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
988 /* HACK: if the name has an initial underscore, try stripping
989 it off & look that up first. I've yet to verify whether there's
990 a Rule that governs whether an initial '_' *should always* be
991 stripped off when mapping from import lib name to the DLL name.
993 sym = GetProcAddress(o_dll->instance, (lbl+1));
995 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
999 sym = GetProcAddress(o_dll->instance, lbl);
1001 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
1016 __attribute((unused))
1018 lookupLocalSymbol( ObjectCode* oc, char *lbl )
1022 val = lookupStrHashTable(oc->lochash, lbl);
1032 /* -----------------------------------------------------------------------------
1033 * Debugging aid: look in GHCi's object symbol tables for symbols
1034 * within DELTA bytes of the specified address, and show their names.
1037 void ghci_enquire ( char* addr );
1039 void ghci_enquire ( char* addr )
1044 const int DELTA = 64;
1049 for (oc = objects; oc; oc = oc->next) {
1050 for (i = 0; i < oc->n_symbols; i++) {
1051 sym = oc->symbols[i];
1052 if (sym == NULL) continue;
1053 // debugBelch("enquire %p %p\n", sym, oc->lochash);
1055 if (oc->lochash != NULL) {
1056 a = lookupStrHashTable(oc->lochash, sym);
1059 a = lookupStrHashTable(symhash, sym);
1062 // debugBelch("ghci_enquire: can't find %s\n", sym);
1064 else if (addr-DELTA <= a && a <= addr+DELTA) {
1065 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1072 #ifdef ia64_HOST_ARCH
1073 static unsigned int PLTSize(void);
1076 /* -----------------------------------------------------------------------------
1077 * Load an obj (populate the global symbol table, but don't resolve yet)
1079 * Returns: 1 if ok, 0 on error.
1082 loadObj( char *path )
1089 void *map_addr = NULL;
1096 /* debugBelch("loadObj %s\n", path ); */
1098 /* Check that we haven't already loaded this object.
1099 Ignore requests to load multiple times */
1103 for (o = objects; o; o = o->next) {
1104 if (0 == strcmp(o->fileName, path)) {
1106 break; /* don't need to search further */
1110 IF_DEBUG(linker, debugBelch(
1111 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1112 "same object file twice:\n"
1114 "GHCi will ignore this, but be warned.\n"
1116 return 1; /* success */
1120 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
1122 # if defined(OBJFORMAT_ELF)
1123 oc->formatName = "ELF";
1124 # elif defined(OBJFORMAT_PEi386)
1125 oc->formatName = "PEi386";
1126 # elif defined(OBJFORMAT_MACHO)
1127 oc->formatName = "Mach-O";
1130 barf("loadObj: not implemented on this platform");
1133 r = stat(path, &st);
1134 if (r == -1) { return 0; }
1136 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1137 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1138 strcpy(oc->fileName, path);
1140 oc->fileSize = st.st_size;
1142 oc->sections = NULL;
1143 oc->lochash = allocStrHashTable();
1144 oc->proddables = NULL;
1146 /* chain it onto the list of objects */
1151 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1153 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1155 #if defined(openbsd_HOST_OS)
1156 fd = open(path, O_RDONLY, S_IRUSR);
1158 fd = open(path, O_RDONLY);
1161 barf("loadObj: can't open `%s'", path);
1163 pagesize = getpagesize();
1165 #ifdef ia64_HOST_ARCH
1166 /* The PLT needs to be right before the object */
1167 n = ROUND_UP(PLTSize(), pagesize);
1168 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1169 if (oc->plt == MAP_FAILED)
1170 barf("loadObj: can't allocate PLT");
1173 map_addr = oc->plt + n;
1176 n = ROUND_UP(oc->fileSize, pagesize);
1178 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
1179 * small memory model on this architecture (see gcc docs,
1182 #ifdef x86_64_HOST_ARCH
1183 #define EXTRA_MAP_FLAGS MAP_32BIT
1185 #define EXTRA_MAP_FLAGS 0
1188 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE,
1189 MAP_PRIVATE|EXTRA_MAP_FLAGS, fd, 0);
1190 if (oc->image == MAP_FAILED)
1191 barf("loadObj: can't map `%s'", path);
1195 #else /* !USE_MMAP */
1197 /* load the image into memory */
1198 f = fopen(path, "rb");
1200 barf("loadObj: can't read `%s'", path);
1202 #ifdef darwin_HOST_OS
1203 // In a Mach-O .o file, all sections can and will be misaligned
1204 // if the total size of the headers is not a multiple of the
1205 // desired alignment. This is fine for .o files that only serve
1206 // as input for the static linker, but it's not fine for us,
1207 // as SSE (used by gcc for floating point) and Altivec require
1208 // 16-byte alignment.
1209 // We calculate the correct alignment from the header before
1210 // reading the file, and then we misalign oc->image on purpose so
1211 // that the actual sections end up aligned again.
1212 misalignment = machoGetMisalignment(f);
1213 oc->misalignment = misalignment;
1218 oc->image = stgMallocBytes(oc->fileSize + misalignment, "loadObj(image)");
1219 oc->image += misalignment;
1221 n = fread ( oc->image, 1, oc->fileSize, f );
1222 if (n != oc->fileSize)
1223 barf("loadObj: error whilst reading `%s'", path);
1227 #endif /* USE_MMAP */
1229 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1230 r = ocAllocateJumpIslands_MachO ( oc );
1231 if (!r) { return r; }
1232 # elif defined(OBJFORMAT_ELF) && defined(powerpc_HOST_ARCH)
1233 r = ocAllocateJumpIslands_ELF ( oc );
1234 if (!r) { return r; }
1237 /* verify the in-memory image */
1238 # if defined(OBJFORMAT_ELF)
1239 r = ocVerifyImage_ELF ( oc );
1240 # elif defined(OBJFORMAT_PEi386)
1241 r = ocVerifyImage_PEi386 ( oc );
1242 # elif defined(OBJFORMAT_MACHO)
1243 r = ocVerifyImage_MachO ( oc );
1245 barf("loadObj: no verify method");
1247 if (!r) { return r; }
1249 /* build the symbol list for this image */
1250 # if defined(OBJFORMAT_ELF)
1251 r = ocGetNames_ELF ( oc );
1252 # elif defined(OBJFORMAT_PEi386)
1253 r = ocGetNames_PEi386 ( oc );
1254 # elif defined(OBJFORMAT_MACHO)
1255 r = ocGetNames_MachO ( oc );
1257 barf("loadObj: no getNames method");
1259 if (!r) { return r; }
1261 /* loaded, but not resolved yet */
1262 oc->status = OBJECT_LOADED;
1267 /* -----------------------------------------------------------------------------
1268 * resolve all the currently unlinked objects in memory
1270 * Returns: 1 if ok, 0 on error.
1280 for (oc = objects; oc; oc = oc->next) {
1281 if (oc->status != OBJECT_RESOLVED) {
1282 # if defined(OBJFORMAT_ELF)
1283 r = ocResolve_ELF ( oc );
1284 # elif defined(OBJFORMAT_PEi386)
1285 r = ocResolve_PEi386 ( oc );
1286 # elif defined(OBJFORMAT_MACHO)
1287 r = ocResolve_MachO ( oc );
1289 barf("resolveObjs: not implemented on this platform");
1291 if (!r) { return r; }
1292 oc->status = OBJECT_RESOLVED;
1298 /* -----------------------------------------------------------------------------
1299 * delete an object from the pool
1302 unloadObj( char *path )
1304 ObjectCode *oc, *prev;
1306 ASSERT(symhash != NULL);
1307 ASSERT(objects != NULL);
1312 for (oc = objects; oc; prev = oc, oc = oc->next) {
1313 if (!strcmp(oc->fileName,path)) {
1315 /* Remove all the mappings for the symbols within this
1320 for (i = 0; i < oc->n_symbols; i++) {
1321 if (oc->symbols[i] != NULL) {
1322 removeStrHashTable(symhash, oc->symbols[i], NULL);
1330 prev->next = oc->next;
1333 /* We're going to leave this in place, in case there are
1334 any pointers from the heap into it: */
1335 /* stgFree(oc->image); */
1336 stgFree(oc->fileName);
1337 stgFree(oc->symbols);
1338 stgFree(oc->sections);
1339 /* The local hash table should have been freed at the end
1340 of the ocResolve_ call on it. */
1341 ASSERT(oc->lochash == NULL);
1347 errorBelch("unloadObj: can't find `%s' to unload", path);
1351 /* -----------------------------------------------------------------------------
1352 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1353 * which may be prodded during relocation, and abort if we try and write
1354 * outside any of these.
1356 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1359 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1360 /* debugBelch("aPB %p %p %d\n", oc, start, size); */
1364 pb->next = oc->proddables;
1365 oc->proddables = pb;
1368 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1371 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1372 char* s = (char*)(pb->start);
1373 char* e = s + pb->size - 1;
1374 char* a = (char*)addr;
1375 /* Assumes that the biggest fixup involves a 4-byte write. This
1376 probably needs to be changed to 8 (ie, +7) on 64-bit
1378 if (a >= s && (a+3) <= e) return;
1380 barf("checkProddableBlock: invalid fixup in runtime linker");
1383 /* -----------------------------------------------------------------------------
1384 * Section management.
1386 static void addSection ( ObjectCode* oc, SectionKind kind,
1387 void* start, void* end )
1389 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1393 s->next = oc->sections;
1396 debugBelch("addSection: %p-%p (size %d), kind %d\n",
1397 start, ((char*)end)-1, end - start + 1, kind );
1402 /* --------------------------------------------------------------------------
1403 * PowerPC specifics (jump islands)
1404 * ------------------------------------------------------------------------*/
1406 #if defined(powerpc_HOST_ARCH)
1409 ocAllocateJumpIslands
1411 Allocate additional space at the end of the object file image to make room
1414 PowerPC relative branch instructions have a 24 bit displacement field.
1415 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
1416 If a particular imported symbol is outside this range, we have to redirect
1417 the jump to a short piece of new code that just loads the 32bit absolute
1418 address and jumps there.
1419 This function just allocates space for one 16 byte ppcJumpIsland for every
1420 undefined symbol in the object file. The code for the islands is filled in by
1421 makeJumpIsland below.
1424 static int ocAllocateJumpIslands( ObjectCode* oc, int count, int first )
1430 int misalignment = 0;
1432 misalignment = oc->misalignment;
1437 // round up to the nearest 4
1438 aligned = (oc->fileSize + 3) & ~3;
1441 #ifndef linux_HOST_OS /* mremap is a linux extension */
1442 #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined
1445 pagesize = getpagesize();
1446 n = ROUND_UP( oc->fileSize, pagesize );
1447 m = ROUND_UP( aligned + sizeof (ppcJumpIsland) * count, pagesize );
1449 /* The effect of this mremap() call is only the ensure that we have
1450 * a sufficient number of virtually contiguous pages. As returned from
1451 * mremap, the pages past the end of the file are not backed. We give
1452 * them a backing by using MAP_FIXED to map in anonymous pages.
1454 if( (oc->image = mremap( oc->image, n, m, MREMAP_MAYMOVE )) == MAP_FAILED )
1456 errorBelch( "Unable to mremap for Jump Islands\n" );
1460 if( mmap( oc->image + n, m - n, PROT_READ | PROT_WRITE | PROT_EXEC,
1461 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0 ) == MAP_FAILED )
1463 errorBelch( "Unable to mmap( MAP_FIXED ) for Jump Islands\n" );
1468 oc->image -= misalignment;
1469 oc->image = stgReallocBytes( oc->image,
1471 aligned + sizeof (ppcJumpIsland) * count,
1472 "ocAllocateJumpIslands" );
1473 oc->image += misalignment;
1474 #endif /* USE_MMAP */
1476 oc->jump_islands = (ppcJumpIsland *) (oc->image + aligned);
1477 memset( oc->jump_islands, 0, sizeof (ppcJumpIsland) * count );
1480 oc->jump_islands = NULL;
1482 oc->island_start_symbol = first;
1483 oc->n_islands = count;
1488 static unsigned long makeJumpIsland( ObjectCode* oc,
1489 unsigned long symbolNumber,
1490 unsigned long target )
1492 ppcJumpIsland *island;
1494 if( symbolNumber < oc->island_start_symbol ||
1495 symbolNumber - oc->island_start_symbol > oc->n_islands)
1498 island = &oc->jump_islands[symbolNumber - oc->island_start_symbol];
1500 // lis r12, hi16(target)
1501 island->lis_r12 = 0x3d80;
1502 island->hi_addr = target >> 16;
1504 // ori r12, r12, lo16(target)
1505 island->ori_r12_r12 = 0x618c;
1506 island->lo_addr = target & 0xffff;
1509 island->mtctr_r12 = 0x7d8903a6;
1512 island->bctr = 0x4e800420;
1514 return (unsigned long) island;
1518 ocFlushInstructionCache
1520 Flush the data & instruction caches.
1521 Because the PPC has split data/instruction caches, we have to
1522 do that whenever we modify code at runtime.
1525 static void ocFlushInstructionCache( ObjectCode *oc )
1527 int n = (oc->fileSize + sizeof( ppcJumpIsland ) * oc->n_islands + 3) / 4;
1528 unsigned long *p = (unsigned long *) oc->image;
1532 __asm__ volatile ( "dcbf 0,%0\n\t"
1540 __asm__ volatile ( "sync\n\t"
1546 /* --------------------------------------------------------------------------
1547 * PEi386 specifics (Win32 targets)
1548 * ------------------------------------------------------------------------*/
1550 /* The information for this linker comes from
1551 Microsoft Portable Executable
1552 and Common Object File Format Specification
1553 revision 5.1 January 1998
1554 which SimonM says comes from the MS Developer Network CDs.
1556 It can be found there (on older CDs), but can also be found
1559 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1561 (this is Rev 6.0 from February 1999).
1563 Things move, so if that fails, try searching for it via
1565 http://www.google.com/search?q=PE+COFF+specification
1567 The ultimate reference for the PE format is the Winnt.h
1568 header file that comes with the Platform SDKs; as always,
1569 implementations will drift wrt their documentation.
1571 A good background article on the PE format is Matt Pietrek's
1572 March 1994 article in Microsoft System Journal (MSJ)
1573 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1574 Win32 Portable Executable File Format." The info in there
1575 has recently been updated in a two part article in
1576 MSDN magazine, issues Feb and March 2002,
1577 "Inside Windows: An In-Depth Look into the Win32 Portable
1578 Executable File Format"
1580 John Levine's book "Linkers and Loaders" contains useful
1585 #if defined(OBJFORMAT_PEi386)
1589 typedef unsigned char UChar;
1590 typedef unsigned short UInt16;
1591 typedef unsigned int UInt32;
1598 UInt16 NumberOfSections;
1599 UInt32 TimeDateStamp;
1600 UInt32 PointerToSymbolTable;
1601 UInt32 NumberOfSymbols;
1602 UInt16 SizeOfOptionalHeader;
1603 UInt16 Characteristics;
1607 #define sizeof_COFF_header 20
1614 UInt32 VirtualAddress;
1615 UInt32 SizeOfRawData;
1616 UInt32 PointerToRawData;
1617 UInt32 PointerToRelocations;
1618 UInt32 PointerToLinenumbers;
1619 UInt16 NumberOfRelocations;
1620 UInt16 NumberOfLineNumbers;
1621 UInt32 Characteristics;
1625 #define sizeof_COFF_section 40
1632 UInt16 SectionNumber;
1635 UChar NumberOfAuxSymbols;
1639 #define sizeof_COFF_symbol 18
1644 UInt32 VirtualAddress;
1645 UInt32 SymbolTableIndex;
1650 #define sizeof_COFF_reloc 10
1653 /* From PE spec doc, section 3.3.2 */
1654 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1655 windows.h -- for the same purpose, but I want to know what I'm
1657 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1658 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1659 #define MYIMAGE_FILE_DLL 0x2000
1660 #define MYIMAGE_FILE_SYSTEM 0x1000
1661 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1662 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1663 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1665 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1666 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1667 #define MYIMAGE_SYM_CLASS_STATIC 3
1668 #define MYIMAGE_SYM_UNDEFINED 0
1670 /* From PE spec doc, section 4.1 */
1671 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1672 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1673 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1675 /* From PE spec doc, section 5.2.1 */
1676 #define MYIMAGE_REL_I386_DIR32 0x0006
1677 #define MYIMAGE_REL_I386_REL32 0x0014
1680 /* We use myindex to calculate array addresses, rather than
1681 simply doing the normal subscript thing. That's because
1682 some of the above structs have sizes which are not
1683 a whole number of words. GCC rounds their sizes up to a
1684 whole number of words, which means that the address calcs
1685 arising from using normal C indexing or pointer arithmetic
1686 are just plain wrong. Sigh.
1689 myindex ( int scale, void* base, int index )
1692 ((UChar*)base) + scale * index;
1697 printName ( UChar* name, UChar* strtab )
1699 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1700 UInt32 strtab_offset = * (UInt32*)(name+4);
1701 debugBelch("%s", strtab + strtab_offset );
1704 for (i = 0; i < 8; i++) {
1705 if (name[i] == 0) break;
1706 debugBelch("%c", name[i] );
1713 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1715 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1716 UInt32 strtab_offset = * (UInt32*)(name+4);
1717 strncpy ( dst, strtab+strtab_offset, dstSize );
1723 if (name[i] == 0) break;
1733 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1736 /* If the string is longer than 8 bytes, look in the
1737 string table for it -- this will be correctly zero terminated.
1739 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1740 UInt32 strtab_offset = * (UInt32*)(name+4);
1741 return ((UChar*)strtab) + strtab_offset;
1743 /* Otherwise, if shorter than 8 bytes, return the original,
1744 which by defn is correctly terminated.
1746 if (name[7]==0) return name;
1747 /* The annoying case: 8 bytes. Copy into a temporary
1748 (which is never freed ...)
1750 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1752 strncpy(newstr,name,8);
1758 /* Just compares the short names (first 8 chars) */
1759 static COFF_section *
1760 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1764 = (COFF_header*)(oc->image);
1765 COFF_section* sectab
1767 ((UChar*)(oc->image))
1768 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1770 for (i = 0; i < hdr->NumberOfSections; i++) {
1773 COFF_section* section_i
1775 myindex ( sizeof_COFF_section, sectab, i );
1776 n1 = (UChar*) &(section_i->Name);
1778 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1779 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1780 n1[6]==n2[6] && n1[7]==n2[7])
1789 zapTrailingAtSign ( UChar* sym )
1791 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1793 if (sym[0] == 0) return;
1795 while (sym[i] != 0) i++;
1798 while (j > 0 && my_isdigit(sym[j])) j--;
1799 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1805 ocVerifyImage_PEi386 ( ObjectCode* oc )
1810 COFF_section* sectab;
1811 COFF_symbol* symtab;
1813 /* debugBelch("\nLOADING %s\n", oc->fileName); */
1814 hdr = (COFF_header*)(oc->image);
1815 sectab = (COFF_section*) (
1816 ((UChar*)(oc->image))
1817 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1819 symtab = (COFF_symbol*) (
1820 ((UChar*)(oc->image))
1821 + hdr->PointerToSymbolTable
1823 strtab = ((UChar*)symtab)
1824 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1826 if (hdr->Machine != 0x14c) {
1827 errorBelch("%s: Not x86 PEi386", oc->fileName);
1830 if (hdr->SizeOfOptionalHeader != 0) {
1831 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
1834 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1835 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1836 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1837 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1838 errorBelch("%s: Not a PEi386 object file", oc->fileName);
1841 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1842 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1843 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
1845 (int)(hdr->Characteristics));
1848 /* If the string table size is way crazy, this might indicate that
1849 there are more than 64k relocations, despite claims to the
1850 contrary. Hence this test. */
1851 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
1853 if ( (*(UInt32*)strtab) > 600000 ) {
1854 /* Note that 600k has no special significance other than being
1855 big enough to handle the almost-2MB-sized lumps that
1856 constitute HSwin32*.o. */
1857 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
1862 /* No further verification after this point; only debug printing. */
1864 IF_DEBUG(linker, i=1);
1865 if (i == 0) return 1;
1867 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1868 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1869 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1872 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1873 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1874 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1875 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1876 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1877 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1878 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1880 /* Print the section table. */
1882 for (i = 0; i < hdr->NumberOfSections; i++) {
1884 COFF_section* sectab_i
1886 myindex ( sizeof_COFF_section, sectab, i );
1893 printName ( sectab_i->Name, strtab );
1903 sectab_i->VirtualSize,
1904 sectab_i->VirtualAddress,
1905 sectab_i->SizeOfRawData,
1906 sectab_i->PointerToRawData,
1907 sectab_i->NumberOfRelocations,
1908 sectab_i->PointerToRelocations,
1909 sectab_i->PointerToRawData
1911 reltab = (COFF_reloc*) (
1912 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1915 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1916 /* If the relocation field (a short) has overflowed, the
1917 * real count can be found in the first reloc entry.
1919 * See Section 4.1 (last para) of the PE spec (rev6.0).
1921 COFF_reloc* rel = (COFF_reloc*)
1922 myindex ( sizeof_COFF_reloc, reltab, 0 );
1923 noRelocs = rel->VirtualAddress;
1926 noRelocs = sectab_i->NumberOfRelocations;
1930 for (; j < noRelocs; j++) {
1932 COFF_reloc* rel = (COFF_reloc*)
1933 myindex ( sizeof_COFF_reloc, reltab, j );
1935 " type 0x%-4x vaddr 0x%-8x name `",
1937 rel->VirtualAddress );
1938 sym = (COFF_symbol*)
1939 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1940 /* Hmm..mysterious looking offset - what's it for? SOF */
1941 printName ( sym->Name, strtab -10 );
1948 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
1949 debugBelch("---START of string table---\n");
1950 for (i = 4; i < *(Int32*)strtab; i++) {
1952 debugBelch("\n"); else
1953 debugBelch("%c", strtab[i] );
1955 debugBelch("--- END of string table---\n");
1960 COFF_symbol* symtab_i;
1961 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1962 symtab_i = (COFF_symbol*)
1963 myindex ( sizeof_COFF_symbol, symtab, i );
1969 printName ( symtab_i->Name, strtab );
1978 (Int32)(symtab_i->SectionNumber),
1979 (UInt32)symtab_i->Type,
1980 (UInt32)symtab_i->StorageClass,
1981 (UInt32)symtab_i->NumberOfAuxSymbols
1983 i += symtab_i->NumberOfAuxSymbols;
1993 ocGetNames_PEi386 ( ObjectCode* oc )
1996 COFF_section* sectab;
1997 COFF_symbol* symtab;
2004 hdr = (COFF_header*)(oc->image);
2005 sectab = (COFF_section*) (
2006 ((UChar*)(oc->image))
2007 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2009 symtab = (COFF_symbol*) (
2010 ((UChar*)(oc->image))
2011 + hdr->PointerToSymbolTable
2013 strtab = ((UChar*)(oc->image))
2014 + hdr->PointerToSymbolTable
2015 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2017 /* Allocate space for any (local, anonymous) .bss sections. */
2019 for (i = 0; i < hdr->NumberOfSections; i++) {
2022 COFF_section* sectab_i
2024 myindex ( sizeof_COFF_section, sectab, i );
2025 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
2026 /* sof 10/05: the PE spec text isn't too clear regarding what
2027 * the SizeOfRawData field is supposed to hold for object
2028 * file sections containing just uninitialized data -- for executables,
2029 * it is supposed to be zero; unclear what it's supposed to be
2030 * for object files. However, VirtualSize is guaranteed to be
2031 * zero for object files, which definitely suggests that SizeOfRawData
2032 * will be non-zero (where else would the size of this .bss section be
2033 * stored?) Looking at the COFF_section info for incoming object files,
2034 * this certainly appears to be the case.
2036 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
2037 * object files up until now. This turned out to bite us with ghc-6.4.1's use
2038 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
2039 * variable decls into to the .bss section. (The specific function in Q which
2040 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
2042 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
2043 /* This is a non-empty .bss section. Allocate zeroed space for
2044 it, and set its PointerToRawData field such that oc->image +
2045 PointerToRawData == addr_of_zeroed_space. */
2046 bss_sz = sectab_i->VirtualSize;
2047 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
2048 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
2049 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
2050 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
2051 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
2054 /* Copy section information into the ObjectCode. */
2056 for (i = 0; i < hdr->NumberOfSections; i++) {
2062 = SECTIONKIND_OTHER;
2063 COFF_section* sectab_i
2065 myindex ( sizeof_COFF_section, sectab, i );
2066 IF_DEBUG(linker, debugBelch("section name = %s\n", sectab_i->Name ));
2069 /* I'm sure this is the Right Way to do it. However, the
2070 alternative of testing the sectab_i->Name field seems to
2071 work ok with Cygwin.
2073 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
2074 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
2075 kind = SECTIONKIND_CODE_OR_RODATA;
2078 if (0==strcmp(".text",sectab_i->Name) ||
2079 0==strcmp(".rdata",sectab_i->Name)||
2080 0==strcmp(".rodata",sectab_i->Name))
2081 kind = SECTIONKIND_CODE_OR_RODATA;
2082 if (0==strcmp(".data",sectab_i->Name) ||
2083 0==strcmp(".bss",sectab_i->Name))
2084 kind = SECTIONKIND_RWDATA;
2086 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
2087 sz = sectab_i->SizeOfRawData;
2088 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
2090 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
2091 end = start + sz - 1;
2093 if (kind == SECTIONKIND_OTHER
2094 /* Ignore sections called which contain stabs debugging
2096 && 0 != strcmp(".stab", sectab_i->Name)
2097 && 0 != strcmp(".stabstr", sectab_i->Name)
2098 /* ignore constructor section for now */
2099 && 0 != strcmp(".ctors", sectab_i->Name)
2101 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", sectab_i->Name, oc->fileName);
2105 if (kind != SECTIONKIND_OTHER && end >= start) {
2106 addSection(oc, kind, start, end);
2107 addProddableBlock(oc, start, end - start + 1);
2111 /* Copy exported symbols into the ObjectCode. */
2113 oc->n_symbols = hdr->NumberOfSymbols;
2114 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2115 "ocGetNames_PEi386(oc->symbols)");
2116 /* Call me paranoid; I don't care. */
2117 for (i = 0; i < oc->n_symbols; i++)
2118 oc->symbols[i] = NULL;
2122 COFF_symbol* symtab_i;
2123 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2124 symtab_i = (COFF_symbol*)
2125 myindex ( sizeof_COFF_symbol, symtab, i );
2129 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
2130 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
2131 /* This symbol is global and defined, viz, exported */
2132 /* for MYIMAGE_SYMCLASS_EXTERNAL
2133 && !MYIMAGE_SYM_UNDEFINED,
2134 the address of the symbol is:
2135 address of relevant section + offset in section
2137 COFF_section* sectabent
2138 = (COFF_section*) myindex ( sizeof_COFF_section,
2140 symtab_i->SectionNumber-1 );
2141 addr = ((UChar*)(oc->image))
2142 + (sectabent->PointerToRawData
2146 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
2147 && symtab_i->Value > 0) {
2148 /* This symbol isn't in any section at all, ie, global bss.
2149 Allocate zeroed space for it. */
2150 addr = stgCallocBytes(1, symtab_i->Value,
2151 "ocGetNames_PEi386(non-anonymous bss)");
2152 addSection(oc, SECTIONKIND_RWDATA, addr,
2153 ((UChar*)addr) + symtab_i->Value - 1);
2154 addProddableBlock(oc, addr, symtab_i->Value);
2155 /* debugBelch("BSS section at 0x%x\n", addr); */
2158 if (addr != NULL ) {
2159 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
2160 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
2161 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
2162 ASSERT(i >= 0 && i < oc->n_symbols);
2163 /* cstring_from_COFF_symbol_name always succeeds. */
2164 oc->symbols[i] = sname;
2165 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
2169 "IGNORING symbol %d\n"
2173 printName ( symtab_i->Name, strtab );
2182 (Int32)(symtab_i->SectionNumber),
2183 (UInt32)symtab_i->Type,
2184 (UInt32)symtab_i->StorageClass,
2185 (UInt32)symtab_i->NumberOfAuxSymbols
2190 i += symtab_i->NumberOfAuxSymbols;
2199 ocResolve_PEi386 ( ObjectCode* oc )
2202 COFF_section* sectab;
2203 COFF_symbol* symtab;
2213 /* ToDo: should be variable-sized? But is at least safe in the
2214 sense of buffer-overrun-proof. */
2216 /* debugBelch("resolving for %s\n", oc->fileName); */
2218 hdr = (COFF_header*)(oc->image);
2219 sectab = (COFF_section*) (
2220 ((UChar*)(oc->image))
2221 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2223 symtab = (COFF_symbol*) (
2224 ((UChar*)(oc->image))
2225 + hdr->PointerToSymbolTable
2227 strtab = ((UChar*)(oc->image))
2228 + hdr->PointerToSymbolTable
2229 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2231 for (i = 0; i < hdr->NumberOfSections; i++) {
2232 COFF_section* sectab_i
2234 myindex ( sizeof_COFF_section, sectab, i );
2237 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2240 /* Ignore sections called which contain stabs debugging
2242 if (0 == strcmp(".stab", sectab_i->Name)
2243 || 0 == strcmp(".stabstr", sectab_i->Name)
2244 || 0 == strcmp(".ctors", sectab_i->Name))
2247 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2248 /* If the relocation field (a short) has overflowed, the
2249 * real count can be found in the first reloc entry.
2251 * See Section 4.1 (last para) of the PE spec (rev6.0).
2253 * Nov2003 update: the GNU linker still doesn't correctly
2254 * handle the generation of relocatable object files with
2255 * overflown relocations. Hence the output to warn of potential
2258 COFF_reloc* rel = (COFF_reloc*)
2259 myindex ( sizeof_COFF_reloc, reltab, 0 );
2260 noRelocs = rel->VirtualAddress;
2261 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
2265 noRelocs = sectab_i->NumberOfRelocations;
2270 for (; j < noRelocs; j++) {
2272 COFF_reloc* reltab_j
2274 myindex ( sizeof_COFF_reloc, reltab, j );
2276 /* the location to patch */
2278 ((UChar*)(oc->image))
2279 + (sectab_i->PointerToRawData
2280 + reltab_j->VirtualAddress
2281 - sectab_i->VirtualAddress )
2283 /* the existing contents of pP */
2285 /* the symbol to connect to */
2286 sym = (COFF_symbol*)
2287 myindex ( sizeof_COFF_symbol,
2288 symtab, reltab_j->SymbolTableIndex );
2291 "reloc sec %2d num %3d: type 0x%-4x "
2292 "vaddr 0x%-8x name `",
2294 (UInt32)reltab_j->Type,
2295 reltab_j->VirtualAddress );
2296 printName ( sym->Name, strtab );
2297 debugBelch("'\n" ));
2299 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
2300 COFF_section* section_sym
2301 = findPEi386SectionCalled ( oc, sym->Name );
2303 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
2306 S = ((UInt32)(oc->image))
2307 + (section_sym->PointerToRawData
2310 copyName ( sym->Name, strtab, symbol, 1000-1 );
2311 (void*)S = lookupLocalSymbol( oc, symbol );
2312 if ((void*)S != NULL) goto foundit;
2313 (void*)S = lookupSymbol( symbol );
2314 if ((void*)S != NULL) goto foundit;
2315 zapTrailingAtSign ( symbol );
2316 (void*)S = lookupLocalSymbol( oc, symbol );
2317 if ((void*)S != NULL) goto foundit;
2318 (void*)S = lookupSymbol( symbol );
2319 if ((void*)S != NULL) goto foundit;
2320 /* Newline first because the interactive linker has printed "linking..." */
2321 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
2325 checkProddableBlock(oc, pP);
2326 switch (reltab_j->Type) {
2327 case MYIMAGE_REL_I386_DIR32:
2330 case MYIMAGE_REL_I386_REL32:
2331 /* Tricky. We have to insert a displacement at
2332 pP which, when added to the PC for the _next_
2333 insn, gives the address of the target (S).
2334 Problem is to know the address of the next insn
2335 when we only know pP. We assume that this
2336 literal field is always the last in the insn,
2337 so that the address of the next insn is pP+4
2338 -- hence the constant 4.
2339 Also I don't know if A should be added, but so
2340 far it has always been zero.
2342 SOF 05/2005: 'A' (old contents of *pP) have been observed
2343 to contain values other than zero (the 'wx' object file
2344 that came with wxhaskell-0.9.4; dunno how it was compiled..).
2345 So, add displacement to old value instead of asserting
2346 A to be zero. Fixes wxhaskell-related crashes, and no other
2347 ill effects have been observed.
2349 Update: the reason why we're seeing these more elaborate
2350 relocations is due to a switch in how the NCG compiles SRTs
2351 and offsets to them from info tables. SRTs live in .(ro)data,
2352 while info tables live in .text, causing GAS to emit REL32/DISP32
2353 relocations with non-zero values. Adding the displacement is
2354 the right thing to do.
2356 *pP = S - ((UInt32)pP) - 4 + A;
2359 debugBelch("%s: unhandled PEi386 relocation type %d",
2360 oc->fileName, reltab_j->Type);
2367 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
2371 #endif /* defined(OBJFORMAT_PEi386) */
2374 /* --------------------------------------------------------------------------
2376 * ------------------------------------------------------------------------*/
2378 #if defined(OBJFORMAT_ELF)
2383 #if defined(sparc_HOST_ARCH)
2384 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2385 #elif defined(i386_HOST_ARCH)
2386 # define ELF_TARGET_386 /* Used inside <elf.h> */
2387 #elif defined(x86_64_HOST_ARCH)
2388 # define ELF_TARGET_X64_64
2390 #elif defined (ia64_HOST_ARCH)
2391 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2393 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2394 # define ELF_NEED_GOT /* needs Global Offset Table */
2395 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2398 #if !defined(openbsd_HOST_OS)
2401 /* openbsd elf has things in different places, with diff names */
2402 #include <elf_abi.h>
2403 #include <machine/reloc.h>
2404 #define R_386_32 RELOC_32
2405 #define R_386_PC32 RELOC_PC32
2409 * Define a set of types which can be used for both ELF32 and ELF64
2413 #define ELFCLASS ELFCLASS64
2414 #define Elf_Addr Elf64_Addr
2415 #define Elf_Word Elf64_Word
2416 #define Elf_Sword Elf64_Sword
2417 #define Elf_Ehdr Elf64_Ehdr
2418 #define Elf_Phdr Elf64_Phdr
2419 #define Elf_Shdr Elf64_Shdr
2420 #define Elf_Sym Elf64_Sym
2421 #define Elf_Rel Elf64_Rel
2422 #define Elf_Rela Elf64_Rela
2423 #define ELF_ST_TYPE ELF64_ST_TYPE
2424 #define ELF_ST_BIND ELF64_ST_BIND
2425 #define ELF_R_TYPE ELF64_R_TYPE
2426 #define ELF_R_SYM ELF64_R_SYM
2428 #define ELFCLASS ELFCLASS32
2429 #define Elf_Addr Elf32_Addr
2430 #define Elf_Word Elf32_Word
2431 #define Elf_Sword Elf32_Sword
2432 #define Elf_Ehdr Elf32_Ehdr
2433 #define Elf_Phdr Elf32_Phdr
2434 #define Elf_Shdr Elf32_Shdr
2435 #define Elf_Sym Elf32_Sym
2436 #define Elf_Rel Elf32_Rel
2437 #define Elf_Rela Elf32_Rela
2439 #define ELF_ST_TYPE ELF32_ST_TYPE
2442 #define ELF_ST_BIND ELF32_ST_BIND
2445 #define ELF_R_TYPE ELF32_R_TYPE
2448 #define ELF_R_SYM ELF32_R_SYM
2454 * Functions to allocate entries in dynamic sections. Currently we simply
2455 * preallocate a large number, and we don't check if a entry for the given
2456 * target already exists (a linear search is too slow). Ideally these
2457 * entries would be associated with symbols.
2460 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2461 #define GOT_SIZE 0x20000
2462 #define FUNCTION_TABLE_SIZE 0x10000
2463 #define PLT_SIZE 0x08000
2466 static Elf_Addr got[GOT_SIZE];
2467 static unsigned int gotIndex;
2468 static Elf_Addr gp_val = (Elf_Addr)got;
2471 allocateGOTEntry(Elf_Addr target)
2475 if (gotIndex >= GOT_SIZE)
2476 barf("Global offset table overflow");
2478 entry = &got[gotIndex++];
2480 return (Elf_Addr)entry;
2484 #ifdef ELF_FUNCTION_DESC
2490 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2491 static unsigned int functionTableIndex;
2494 allocateFunctionDesc(Elf_Addr target)
2496 FunctionDesc *entry;
2498 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2499 barf("Function table overflow");
2501 entry = &functionTable[functionTableIndex++];
2503 entry->gp = (Elf_Addr)gp_val;
2504 return (Elf_Addr)entry;
2508 copyFunctionDesc(Elf_Addr target)
2510 FunctionDesc *olddesc = (FunctionDesc *)target;
2511 FunctionDesc *newdesc;
2513 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2514 newdesc->gp = olddesc->gp;
2515 return (Elf_Addr)newdesc;
2520 #ifdef ia64_HOST_ARCH
2521 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2522 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2524 static unsigned char plt_code[] =
2526 /* taken from binutils bfd/elfxx-ia64.c */
2527 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2528 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2529 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2530 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2531 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2532 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2535 /* If we can't get to the function descriptor via gp, take a local copy of it */
2536 #define PLT_RELOC(code, target) { \
2537 Elf64_Sxword rel_value = target - gp_val; \
2538 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2539 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2541 ia64_reloc_gprel22((Elf_Addr)code, target); \
2546 unsigned char code[sizeof(plt_code)];
2550 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2552 PLTEntry *plt = (PLTEntry *)oc->plt;
2555 if (oc->pltIndex >= PLT_SIZE)
2556 barf("Procedure table overflow");
2558 entry = &plt[oc->pltIndex++];
2559 memcpy(entry->code, plt_code, sizeof(entry->code));
2560 PLT_RELOC(entry->code, target);
2561 return (Elf_Addr)entry;
2567 return (PLT_SIZE * sizeof(PLTEntry));
2572 #if x86_64_HOST_ARCH
2573 // On x86_64, 32-bit relocations are often used, which requires that
2574 // we can resolve a symbol to a 32-bit offset. However, shared
2575 // libraries are placed outside the 2Gb area, which leaves us with a
2576 // problem when we need to give a 32-bit offset to a symbol in a
2579 // For a function symbol, we can allocate a bounce sequence inside the
2580 // 2Gb area and resolve the symbol to this. The bounce sequence is
2581 // simply a long jump instruction to the real location of the symbol.
2583 // For data references, we're screwed.
2586 unsigned char jmp[8]; /* 6 byte instruction: jmpq *0x00000002(%rip) */
2590 #define X86_64_BB_SIZE 1024
2592 static x86_64_bounce *x86_64_bounce_buffer = NULL;
2593 static nat x86_64_bb_next_off;
2596 x86_64_high_symbol( char *lbl, void *addr )
2598 x86_64_bounce *bounce;
2600 if ( x86_64_bounce_buffer == NULL ||
2601 x86_64_bb_next_off >= X86_64_BB_SIZE ) {
2602 x86_64_bounce_buffer =
2603 mmap(NULL, X86_64_BB_SIZE * sizeof(x86_64_bounce),
2604 PROT_EXEC|PROT_READ|PROT_WRITE,
2605 MAP_PRIVATE|MAP_32BIT|MAP_ANONYMOUS, -1, 0);
2606 if (x86_64_bounce_buffer == MAP_FAILED) {
2607 barf("x86_64_high_symbol: mmap failed");
2609 x86_64_bb_next_off = 0;
2611 bounce = &x86_64_bounce_buffer[x86_64_bb_next_off];
2612 bounce->jmp[0] = 0xff;
2613 bounce->jmp[1] = 0x25;
2614 bounce->jmp[2] = 0x02;
2615 bounce->jmp[3] = 0x00;
2616 bounce->jmp[4] = 0x00;
2617 bounce->jmp[5] = 0x00;
2618 bounce->addr = addr;
2619 x86_64_bb_next_off++;
2621 IF_DEBUG(linker, debugBelch("x86_64: allocated bounce entry for %s->%p at %p\n",
2622 lbl, addr, bounce));
2624 insertStrHashTable(symhash, lbl, bounce);
2631 * Generic ELF functions
2635 findElfSection ( void* objImage, Elf_Word sh_type )
2637 char* ehdrC = (char*)objImage;
2638 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2639 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2640 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2644 for (i = 0; i < ehdr->e_shnum; i++) {
2645 if (shdr[i].sh_type == sh_type
2646 /* Ignore the section header's string table. */
2647 && i != ehdr->e_shstrndx
2648 /* Ignore string tables named .stabstr, as they contain
2650 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2652 ptr = ehdrC + shdr[i].sh_offset;
2659 #if defined(ia64_HOST_ARCH)
2661 findElfSegment ( void* objImage, Elf_Addr vaddr )
2663 char* ehdrC = (char*)objImage;
2664 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2665 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2666 Elf_Addr segaddr = 0;
2669 for (i = 0; i < ehdr->e_phnum; i++) {
2670 segaddr = phdr[i].p_vaddr;
2671 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2679 ocVerifyImage_ELF ( ObjectCode* oc )
2683 int i, j, nent, nstrtab, nsymtabs;
2687 char* ehdrC = (char*)(oc->image);
2688 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2690 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2691 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2692 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2693 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2694 errorBelch("%s: not an ELF object", oc->fileName);
2698 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2699 errorBelch("%s: unsupported ELF format", oc->fileName);
2703 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2704 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
2706 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2707 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
2709 errorBelch("%s: unknown endiannness", oc->fileName);
2713 if (ehdr->e_type != ET_REL) {
2714 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
2717 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
2719 IF_DEBUG(linker,debugBelch( "Architecture is " ));
2720 switch (ehdr->e_machine) {
2721 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
2722 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
2724 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
2726 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
2728 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
2730 default: IF_DEBUG(linker,debugBelch( "unknown" ));
2731 errorBelch("%s: unknown architecture", oc->fileName);
2735 IF_DEBUG(linker,debugBelch(
2736 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
2737 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2739 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2741 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2743 if (ehdr->e_shstrndx == SHN_UNDEF) {
2744 errorBelch("%s: no section header string table", oc->fileName);
2747 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
2749 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2752 for (i = 0; i < ehdr->e_shnum; i++) {
2753 IF_DEBUG(linker,debugBelch("%2d: ", i ));
2754 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
2755 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
2756 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
2757 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
2758 ehdrC + shdr[i].sh_offset,
2759 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2761 if (shdr[i].sh_type == SHT_REL) {
2762 IF_DEBUG(linker,debugBelch("Rel " ));
2763 } else if (shdr[i].sh_type == SHT_RELA) {
2764 IF_DEBUG(linker,debugBelch("RelA " ));
2766 IF_DEBUG(linker,debugBelch(" "));
2769 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
2773 IF_DEBUG(linker,debugBelch( "\nString tables" ));
2776 for (i = 0; i < ehdr->e_shnum; i++) {
2777 if (shdr[i].sh_type == SHT_STRTAB
2778 /* Ignore the section header's string table. */
2779 && i != ehdr->e_shstrndx
2780 /* Ignore string tables named .stabstr, as they contain
2782 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2784 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
2785 strtab = ehdrC + shdr[i].sh_offset;
2790 errorBelch("%s: no string tables, or too many", oc->fileName);
2795 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
2796 for (i = 0; i < ehdr->e_shnum; i++) {
2797 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2798 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
2800 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2801 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2802 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
2804 shdr[i].sh_size % sizeof(Elf_Sym)
2806 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2807 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
2810 for (j = 0; j < nent; j++) {
2811 IF_DEBUG(linker,debugBelch(" %2d ", j ));
2812 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
2813 (int)stab[j].st_shndx,
2814 (int)stab[j].st_size,
2815 (char*)stab[j].st_value ));
2817 IF_DEBUG(linker,debugBelch("type=" ));
2818 switch (ELF_ST_TYPE(stab[j].st_info)) {
2819 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
2820 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
2821 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
2822 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
2823 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
2824 default: IF_DEBUG(linker,debugBelch("? " )); break;
2826 IF_DEBUG(linker,debugBelch(" " ));
2828 IF_DEBUG(linker,debugBelch("bind=" ));
2829 switch (ELF_ST_BIND(stab[j].st_info)) {
2830 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
2831 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
2832 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
2833 default: IF_DEBUG(linker,debugBelch("? " )); break;
2835 IF_DEBUG(linker,debugBelch(" " ));
2837 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
2841 if (nsymtabs == 0) {
2842 errorBelch("%s: didn't find any symbol tables", oc->fileName);
2849 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
2853 if (hdr->sh_type == SHT_PROGBITS
2854 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
2855 /* .text-style section */
2856 return SECTIONKIND_CODE_OR_RODATA;
2859 if (hdr->sh_type == SHT_PROGBITS
2860 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
2861 /* .data-style section */
2862 return SECTIONKIND_RWDATA;
2865 if (hdr->sh_type == SHT_PROGBITS
2866 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
2867 /* .rodata-style section */
2868 return SECTIONKIND_CODE_OR_RODATA;
2871 if (hdr->sh_type == SHT_NOBITS
2872 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
2873 /* .bss-style section */
2875 return SECTIONKIND_RWDATA;
2878 return SECTIONKIND_OTHER;
2883 ocGetNames_ELF ( ObjectCode* oc )
2888 char* ehdrC = (char*)(oc->image);
2889 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2890 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2891 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2893 ASSERT(symhash != NULL);
2896 errorBelch("%s: no strtab", oc->fileName);
2901 for (i = 0; i < ehdr->e_shnum; i++) {
2902 /* Figure out what kind of section it is. Logic derived from
2903 Figure 1.14 ("Special Sections") of the ELF document
2904 ("Portable Formats Specification, Version 1.1"). */
2906 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
2908 if (is_bss && shdr[i].sh_size > 0) {
2909 /* This is a non-empty .bss section. Allocate zeroed space for
2910 it, and set its .sh_offset field such that
2911 ehdrC + .sh_offset == addr_of_zeroed_space. */
2912 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2913 "ocGetNames_ELF(BSS)");
2914 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2916 debugBelch("BSS section at 0x%x, size %d\n",
2917 zspace, shdr[i].sh_size);
2921 /* fill in the section info */
2922 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2923 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2924 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2925 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2928 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2930 /* copy stuff into this module's object symbol table */
2931 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2932 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2934 oc->n_symbols = nent;
2935 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2936 "ocGetNames_ELF(oc->symbols)");
2938 for (j = 0; j < nent; j++) {
2940 char isLocal = FALSE; /* avoids uninit-var warning */
2942 char* nm = strtab + stab[j].st_name;
2943 int secno = stab[j].st_shndx;
2945 /* Figure out if we want to add it; if so, set ad to its
2946 address. Otherwise leave ad == NULL. */
2948 if (secno == SHN_COMMON) {
2950 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2952 debugBelch("COMMON symbol, size %d name %s\n",
2953 stab[j].st_size, nm);
2955 /* Pointless to do addProddableBlock() for this area,
2956 since the linker should never poke around in it. */
2959 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2960 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2962 /* and not an undefined symbol */
2963 && stab[j].st_shndx != SHN_UNDEF
2964 /* and not in a "special section" */
2965 && stab[j].st_shndx < SHN_LORESERVE
2967 /* and it's a not a section or string table or anything silly */
2968 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2969 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2970 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2973 /* Section 0 is the undefined section, hence > and not >=. */
2974 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2976 if (shdr[secno].sh_type == SHT_NOBITS) {
2977 debugBelch(" BSS symbol, size %d off %d name %s\n",
2978 stab[j].st_size, stab[j].st_value, nm);
2981 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2982 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2985 #ifdef ELF_FUNCTION_DESC
2986 /* dlsym() and the initialisation table both give us function
2987 * descriptors, so to be consistent we store function descriptors
2988 * in the symbol table */
2989 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2990 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2992 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s",
2993 ad, oc->fileName, nm ));
2998 /* And the decision is ... */
3002 oc->symbols[j] = nm;
3005 /* Ignore entirely. */
3007 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3011 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3012 strtab + stab[j].st_name ));
3015 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
3016 (int)ELF_ST_BIND(stab[j].st_info),
3017 (int)ELF_ST_TYPE(stab[j].st_info),
3018 (int)stab[j].st_shndx,
3019 strtab + stab[j].st_name
3022 oc->symbols[j] = NULL;
3031 /* Do ELF relocations which lack an explicit addend. All x86-linux
3032 relocations appear to be of this form. */
3034 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3035 Elf_Shdr* shdr, int shnum,
3036 Elf_Sym* stab, char* strtab )
3041 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3042 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3043 int target_shndx = shdr[shnum].sh_info;
3044 int symtab_shndx = shdr[shnum].sh_link;
3046 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3047 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3048 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3049 target_shndx, symtab_shndx ));
3051 /* Skip sections that we're not interested in. */
3054 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3055 if (kind == SECTIONKIND_OTHER) {
3056 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
3061 for (j = 0; j < nent; j++) {
3062 Elf_Addr offset = rtab[j].r_offset;
3063 Elf_Addr info = rtab[j].r_info;
3065 Elf_Addr P = ((Elf_Addr)targ) + offset;
3066 Elf_Word* pP = (Elf_Word*)P;
3072 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
3073 j, (void*)offset, (void*)info ));
3075 IF_DEBUG(linker,debugBelch( " ZERO" ));
3078 Elf_Sym sym = stab[ELF_R_SYM(info)];
3079 /* First see if it is a local symbol. */
3080 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3081 /* Yes, so we can get the address directly from the ELF symbol
3083 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3085 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3086 + stab[ELF_R_SYM(info)].st_value);
3089 /* No, so look up the name in our global table. */
3090 symbol = strtab + sym.st_name;
3091 S_tmp = lookupSymbol( symbol );
3092 S = (Elf_Addr)S_tmp;
3095 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3098 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
3101 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
3102 (void*)P, (void*)S, (void*)A ));
3103 checkProddableBlock ( oc, pP );
3107 switch (ELF_R_TYPE(info)) {
3108 # ifdef i386_HOST_ARCH
3109 case R_386_32: *pP = value; break;
3110 case R_386_PC32: *pP = value - P; break;
3113 errorBelch("%s: unhandled ELF relocation(Rel) type %ld\n",
3114 oc->fileName, ELF_R_TYPE(info));
3122 /* Do ELF relocations for which explicit addends are supplied.
3123 sparc-solaris relocations appear to be of this form. */
3125 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
3126 Elf_Shdr* shdr, int shnum,
3127 Elf_Sym* stab, char* strtab )
3130 char *symbol = NULL;
3132 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
3133 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
3134 int target_shndx = shdr[shnum].sh_info;
3135 int symtab_shndx = shdr[shnum].sh_link;
3137 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3138 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
3139 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3140 target_shndx, symtab_shndx ));
3142 for (j = 0; j < nent; j++) {
3143 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
3144 /* This #ifdef only serves to avoid unused-var warnings. */
3145 Elf_Addr offset = rtab[j].r_offset;
3146 Elf_Addr P = targ + offset;
3148 Elf_Addr info = rtab[j].r_info;
3149 Elf_Addr A = rtab[j].r_addend;
3153 # if defined(sparc_HOST_ARCH)
3154 Elf_Word* pP = (Elf_Word*)P;
3156 # elif defined(ia64_HOST_ARCH)
3157 Elf64_Xword *pP = (Elf64_Xword *)P;
3159 # elif defined(powerpc_HOST_ARCH)
3163 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
3164 j, (void*)offset, (void*)info,
3167 IF_DEBUG(linker,debugBelch( " ZERO" ));
3170 Elf_Sym sym = stab[ELF_R_SYM(info)];
3171 /* First see if it is a local symbol. */
3172 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3173 /* Yes, so we can get the address directly from the ELF symbol
3175 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3177 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3178 + stab[ELF_R_SYM(info)].st_value);
3179 #ifdef ELF_FUNCTION_DESC
3180 /* Make a function descriptor for this function */
3181 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
3182 S = allocateFunctionDesc(S + A);
3187 /* No, so look up the name in our global table. */
3188 symbol = strtab + sym.st_name;
3189 S_tmp = lookupSymbol( symbol );
3190 S = (Elf_Addr)S_tmp;
3192 #ifdef ELF_FUNCTION_DESC
3193 /* If a function, already a function descriptor - we would
3194 have to copy it to add an offset. */
3195 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
3196 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
3200 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3203 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
3206 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
3207 (void*)P, (void*)S, (void*)A ));
3208 /* checkProddableBlock ( oc, (void*)P ); */
3212 switch (ELF_R_TYPE(info)) {
3213 # if defined(sparc_HOST_ARCH)
3214 case R_SPARC_WDISP30:
3215 w1 = *pP & 0xC0000000;
3216 w2 = (Elf_Word)((value - P) >> 2);
3217 ASSERT((w2 & 0xC0000000) == 0);
3222 w1 = *pP & 0xFFC00000;
3223 w2 = (Elf_Word)(value >> 10);
3224 ASSERT((w2 & 0xFFC00000) == 0);
3230 w2 = (Elf_Word)(value & 0x3FF);
3231 ASSERT((w2 & ~0x3FF) == 0);
3235 /* According to the Sun documentation:
3237 This relocation type resembles R_SPARC_32, except it refers to an
3238 unaligned word. That is, the word to be relocated must be treated
3239 as four separate bytes with arbitrary alignment, not as a word
3240 aligned according to the architecture requirements.
3242 (JRS: which means that freeloading on the R_SPARC_32 case
3243 is probably wrong, but hey ...)
3247 w2 = (Elf_Word)value;
3250 # elif defined(ia64_HOST_ARCH)
3251 case R_IA64_DIR64LSB:
3252 case R_IA64_FPTR64LSB:
3255 case R_IA64_PCREL64LSB:
3258 case R_IA64_SEGREL64LSB:
3259 addr = findElfSegment(ehdrC, value);
3262 case R_IA64_GPREL22:
3263 ia64_reloc_gprel22(P, value);
3265 case R_IA64_LTOFF22:
3266 case R_IA64_LTOFF22X:
3267 case R_IA64_LTOFF_FPTR22:
3268 addr = allocateGOTEntry(value);
3269 ia64_reloc_gprel22(P, addr);
3271 case R_IA64_PCREL21B:
3272 ia64_reloc_pcrel21(P, S, oc);
3275 /* This goes with R_IA64_LTOFF22X and points to the load to
3276 * convert into a move. We don't implement relaxation. */
3278 # elif defined(powerpc_HOST_ARCH)
3279 case R_PPC_ADDR16_LO:
3280 *(Elf32_Half*) P = value;
3283 case R_PPC_ADDR16_HI:
3284 *(Elf32_Half*) P = value >> 16;
3287 case R_PPC_ADDR16_HA:
3288 *(Elf32_Half*) P = (value + 0x8000) >> 16;
3292 *(Elf32_Word *) P = value;
3296 *(Elf32_Word *) P = value - P;
3302 if( delta << 6 >> 6 != delta )
3304 value = makeJumpIsland( oc, ELF_R_SYM(info), value );
3307 if( value == 0 || delta << 6 >> 6 != delta )
3309 barf( "Unable to make ppcJumpIsland for #%d",
3315 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
3316 | (delta & 0x3fffffc);
3320 #if x86_64_HOST_ARCH
3322 *(Elf64_Xword *)P = value;
3327 StgInt64 off = value - P;
3328 if (off >= 0x7fffffffL || off < -0x80000000L) {
3329 barf("R_X86_64_PC32 relocation out of range: %s = %p",
3332 *(Elf64_Word *)P = (Elf64_Word)off;
3337 if (value >= 0x7fffffffL) {
3338 barf("R_X86_64_32 relocation out of range: %s = %p\n",
3341 *(Elf64_Word *)P = (Elf64_Word)value;
3345 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
3346 barf("R_X86_64_32S relocation out of range: %s = %p\n",
3349 *(Elf64_Sword *)P = (Elf64_Sword)value;
3354 errorBelch("%s: unhandled ELF relocation(RelA) type %ld\n",
3355 oc->fileName, ELF_R_TYPE(info));
3364 ocResolve_ELF ( ObjectCode* oc )
3368 Elf_Sym* stab = NULL;
3369 char* ehdrC = (char*)(oc->image);
3370 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
3371 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3373 /* first find "the" symbol table */
3374 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
3376 /* also go find the string table */
3377 strtab = findElfSection ( ehdrC, SHT_STRTAB );
3379 if (stab == NULL || strtab == NULL) {
3380 errorBelch("%s: can't find string or symbol table", oc->fileName);
3384 /* Process the relocation sections. */
3385 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
3386 if (shdr[shnum].sh_type == SHT_REL) {
3387 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
3388 shnum, stab, strtab );
3392 if (shdr[shnum].sh_type == SHT_RELA) {
3393 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
3394 shnum, stab, strtab );
3399 /* Free the local symbol table; we won't need it again. */
3400 freeHashTable(oc->lochash, NULL);
3403 #if defined(powerpc_HOST_ARCH)
3404 ocFlushInstructionCache( oc );
3412 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
3413 * at the front. The following utility functions pack and unpack instructions, and
3414 * take care of the most common relocations.
3417 #ifdef ia64_HOST_ARCH
3420 ia64_extract_instruction(Elf64_Xword *target)
3423 int slot = (Elf_Addr)target & 3;
3424 target = (Elf_Addr)target & ~3;
3432 return ((w1 >> 5) & 0x1ffffffffff);
3434 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
3438 barf("ia64_extract_instruction: invalid slot %p", target);
3443 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
3445 int slot = (Elf_Addr)target & 3;
3446 target = (Elf_Addr)target & ~3;
3451 *target |= value << 5;
3454 *target |= value << 46;
3455 *(target+1) |= value >> 18;
3458 *(target+1) |= value << 23;
3464 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
3466 Elf64_Xword instruction;
3467 Elf64_Sxword rel_value;
3469 rel_value = value - gp_val;
3470 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
3471 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
3473 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3474 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
3475 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
3476 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
3477 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3478 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3482 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
3484 Elf64_Xword instruction;
3485 Elf64_Sxword rel_value;
3488 entry = allocatePLTEntry(value, oc);
3490 rel_value = (entry >> 4) - (target >> 4);
3491 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
3492 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
3494 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3495 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
3496 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3497 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3503 * PowerPC ELF specifics
3506 #ifdef powerpc_HOST_ARCH
3508 static int ocAllocateJumpIslands_ELF( ObjectCode *oc )
3514 ehdr = (Elf_Ehdr *) oc->image;
3515 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
3517 for( i = 0; i < ehdr->e_shnum; i++ )
3518 if( shdr[i].sh_type == SHT_SYMTAB )
3521 if( i == ehdr->e_shnum )
3523 errorBelch( "This ELF file contains no symtab" );
3527 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
3529 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
3530 shdr[i].sh_entsize, sizeof( Elf_Sym ) );
3535 return ocAllocateJumpIslands( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
3538 #endif /* powerpc */
3542 /* --------------------------------------------------------------------------
3544 * ------------------------------------------------------------------------*/
3546 #if defined(OBJFORMAT_MACHO)
3549 Support for MachO linking on Darwin/MacOS X
3550 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3552 I hereby formally apologize for the hackish nature of this code.
3553 Things that need to be done:
3554 *) implement ocVerifyImage_MachO
3555 *) add still more sanity checks.
3558 #ifdef powerpc_HOST_ARCH
3559 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3561 struct mach_header *header = (struct mach_header *) oc->image;
3562 struct load_command *lc = (struct load_command *) (header + 1);
3565 for( i = 0; i < header->ncmds; i++ )
3567 if( lc->cmd == LC_SYMTAB )
3569 // Find out the first and last undefined external
3570 // symbol, so we don't have to allocate too many
3572 struct symtab_command *symLC = (struct symtab_command *) lc;
3573 unsigned min = symLC->nsyms, max = 0;
3574 struct nlist *nlist =
3575 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
3577 for(i=0;i<symLC->nsyms;i++)
3579 if(nlist[i].n_type & N_STAB)
3581 else if(nlist[i].n_type & N_EXT)
3583 if((nlist[i].n_type & N_TYPE) == N_UNDF
3584 && (nlist[i].n_value == 0))
3594 return ocAllocateJumpIslands(oc, max - min + 1, min);
3599 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3601 return ocAllocateJumpIslands(oc,0,0);
3605 static int ocVerifyImage_MachO(ObjectCode* oc STG_UNUSED)
3607 // FIXME: do some verifying here
3611 static int resolveImports(
3614 struct symtab_command *symLC,
3615 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3616 unsigned long *indirectSyms,
3617 struct nlist *nlist)
3621 for(i=0;i*4<sect->size;i++)
3623 // according to otool, reserved1 contains the first index into the indirect symbol table
3624 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3625 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3628 if((symbol->n_type & N_TYPE) == N_UNDF
3629 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3630 addr = (void*) (symbol->n_value);
3631 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3634 addr = lookupSymbol(nm);
3637 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3641 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3642 ((void**)(image + sect->offset))[i] = addr;
3648 static unsigned long relocateAddress(
3651 struct section* sections,
3652 unsigned long address)
3655 for(i = 0; i < nSections; i++)
3657 if(sections[i].addr <= address
3658 && address < sections[i].addr + sections[i].size)
3660 return (unsigned long)oc->image
3661 + sections[i].offset + address - sections[i].addr;
3664 barf("Invalid Mach-O file:"
3665 "Address out of bounds while relocating object file");
3669 static int relocateSection(
3672 struct symtab_command *symLC, struct nlist *nlist,
3673 int nSections, struct section* sections, struct section *sect)
3675 struct relocation_info *relocs;
3678 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3680 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3682 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
3684 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
3688 relocs = (struct relocation_info*) (image + sect->reloff);
3692 if(relocs[i].r_address & R_SCATTERED)
3694 struct scattered_relocation_info *scat =
3695 (struct scattered_relocation_info*) &relocs[i];
3699 if(scat->r_length == 2)
3701 unsigned long word = 0;
3702 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3703 checkProddableBlock(oc,wordPtr);
3705 // Note on relocation types:
3706 // i386 uses the GENERIC_RELOC_* types,
3707 // while ppc uses special PPC_RELOC_* types.
3708 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
3709 // in both cases, all others are different.
3710 // Therefore, we use GENERIC_RELOC_VANILLA
3711 // and GENERIC_RELOC_PAIR instead of the PPC variants,
3712 // and use #ifdefs for the other types.
3714 // Step 1: Figure out what the relocated value should be
3715 if(scat->r_type == GENERIC_RELOC_VANILLA)
3717 word = *wordPtr + (unsigned long) relocateAddress(
3724 #ifdef powerpc_HOST_ARCH
3725 else if(scat->r_type == PPC_RELOC_SECTDIFF
3726 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3727 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3728 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3730 else if(scat->r_type == GENERIC_RELOC_SECTDIFF)
3733 struct scattered_relocation_info *pair =
3734 (struct scattered_relocation_info*) &relocs[i+1];
3736 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
3737 barf("Invalid Mach-O file: "
3738 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
3740 word = (unsigned long)
3741 (relocateAddress(oc, nSections, sections, scat->r_value)
3742 - relocateAddress(oc, nSections, sections, pair->r_value));
3745 #ifdef powerpc_HOST_ARCH
3746 else if(scat->r_type == PPC_RELOC_HI16
3747 || scat->r_type == PPC_RELOC_LO16
3748 || scat->r_type == PPC_RELOC_HA16
3749 || scat->r_type == PPC_RELOC_LO14)
3750 { // these are generated by label+offset things
3751 struct relocation_info *pair = &relocs[i+1];
3752 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
3753 barf("Invalid Mach-O file: "
3754 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
3756 if(scat->r_type == PPC_RELOC_LO16)
3758 word = ((unsigned short*) wordPtr)[1];
3759 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3761 else if(scat->r_type == PPC_RELOC_LO14)
3763 barf("Unsupported Relocation: PPC_RELOC_LO14");
3764 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
3765 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3767 else if(scat->r_type == PPC_RELOC_HI16)
3769 word = ((unsigned short*) wordPtr)[1] << 16;
3770 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3772 else if(scat->r_type == PPC_RELOC_HA16)
3774 word = ((unsigned short*) wordPtr)[1] << 16;
3775 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3779 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
3786 continue; // ignore the others
3788 #ifdef powerpc_HOST_ARCH
3789 if(scat->r_type == GENERIC_RELOC_VANILLA
3790 || scat->r_type == PPC_RELOC_SECTDIFF)
3792 if(scat->r_type == GENERIC_RELOC_VANILLA
3793 || scat->r_type == GENERIC_RELOC_SECTDIFF)
3798 #ifdef powerpc_HOST_ARCH
3799 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
3801 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3803 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
3805 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3807 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
3809 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3810 + ((word & (1<<15)) ? 1 : 0);
3816 continue; // FIXME: I hope it's OK to ignore all the others.
3820 struct relocation_info *reloc = &relocs[i];
3821 if(reloc->r_pcrel && !reloc->r_extern)
3824 if(reloc->r_length == 2)
3826 unsigned long word = 0;
3827 #ifdef powerpc_HOST_ARCH
3828 unsigned long jumpIsland = 0;
3829 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
3830 // to avoid warning and to catch
3834 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3835 checkProddableBlock(oc,wordPtr);
3837 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3841 #ifdef powerpc_HOST_ARCH
3842 else if(reloc->r_type == PPC_RELOC_LO16)
3844 word = ((unsigned short*) wordPtr)[1];
3845 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3847 else if(reloc->r_type == PPC_RELOC_HI16)
3849 word = ((unsigned short*) wordPtr)[1] << 16;
3850 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3852 else if(reloc->r_type == PPC_RELOC_HA16)
3854 word = ((unsigned short*) wordPtr)[1] << 16;
3855 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3857 else if(reloc->r_type == PPC_RELOC_BR24)
3860 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
3864 if(!reloc->r_extern)
3867 sections[reloc->r_symbolnum-1].offset
3868 - sections[reloc->r_symbolnum-1].addr
3875 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3876 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3877 void *symbolAddress = lookupSymbol(nm);
3880 errorBelch("\nunknown symbol `%s'", nm);
3886 #ifdef powerpc_HOST_ARCH
3887 // In the .o file, this should be a relative jump to NULL
3888 // and we'll change it to a relative jump to the symbol
3889 ASSERT(-word == reloc->r_address);
3890 jumpIsland = makeJumpIsland(oc,reloc->r_symbolnum,(unsigned long) symbolAddress);
3893 offsetToJumpIsland = word + jumpIsland
3894 - (((long)image) + sect->offset - sect->addr);
3897 word += (unsigned long) symbolAddress
3898 - (((long)image) + sect->offset - sect->addr);
3902 word += (unsigned long) symbolAddress;
3906 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3911 #ifdef powerpc_HOST_ARCH
3912 else if(reloc->r_type == PPC_RELOC_LO16)
3914 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3917 else if(reloc->r_type == PPC_RELOC_HI16)
3919 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3922 else if(reloc->r_type == PPC_RELOC_HA16)
3924 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3925 + ((word & (1<<15)) ? 1 : 0);
3928 else if(reloc->r_type == PPC_RELOC_BR24)
3930 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3932 // The branch offset is too large.
3933 // Therefore, we try to use a jump island.
3936 barf("unconditional relative branch out of range: "
3937 "no jump island available");
3940 word = offsetToJumpIsland;
3941 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3942 barf("unconditional relative branch out of range: "
3943 "jump island out of range");
3945 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3950 barf("\nunknown relocation %d",reloc->r_type);
3957 static int ocGetNames_MachO(ObjectCode* oc)
3959 char *image = (char*) oc->image;
3960 struct mach_header *header = (struct mach_header*) image;
3961 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3962 unsigned i,curSymbol = 0;
3963 struct segment_command *segLC = NULL;
3964 struct section *sections;
3965 struct symtab_command *symLC = NULL;
3966 struct nlist *nlist;
3967 unsigned long commonSize = 0;
3968 char *commonStorage = NULL;
3969 unsigned long commonCounter;
3971 for(i=0;i<header->ncmds;i++)
3973 if(lc->cmd == LC_SEGMENT)
3974 segLC = (struct segment_command*) lc;
3975 else if(lc->cmd == LC_SYMTAB)
3976 symLC = (struct symtab_command*) lc;
3977 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3980 sections = (struct section*) (segLC+1);
3981 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
3984 for(i=0;i<segLC->nsects;i++)
3986 if(sections[i].size == 0)
3989 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
3991 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
3992 "ocGetNames_MachO(common symbols)");
3993 sections[i].offset = zeroFillArea - image;
3996 if(!strcmp(sections[i].sectname,"__text"))
3997 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3998 (void*) (image + sections[i].offset),
3999 (void*) (image + sections[i].offset + sections[i].size));
4000 else if(!strcmp(sections[i].sectname,"__const"))
4001 addSection(oc, SECTIONKIND_RWDATA,
4002 (void*) (image + sections[i].offset),
4003 (void*) (image + sections[i].offset + sections[i].size));
4004 else if(!strcmp(sections[i].sectname,"__data"))
4005 addSection(oc, SECTIONKIND_RWDATA,
4006 (void*) (image + sections[i].offset),
4007 (void*) (image + sections[i].offset + sections[i].size));
4008 else if(!strcmp(sections[i].sectname,"__bss")
4009 || !strcmp(sections[i].sectname,"__common"))
4010 addSection(oc, SECTIONKIND_RWDATA,
4011 (void*) (image + sections[i].offset),
4012 (void*) (image + sections[i].offset + sections[i].size));
4014 addProddableBlock(oc, (void*) (image + sections[i].offset),
4018 // count external symbols defined here
4022 for(i=0;i<symLC->nsyms;i++)
4024 if(nlist[i].n_type & N_STAB)
4026 else if(nlist[i].n_type & N_EXT)
4028 if((nlist[i].n_type & N_TYPE) == N_UNDF
4029 && (nlist[i].n_value != 0))
4031 commonSize += nlist[i].n_value;
4034 else if((nlist[i].n_type & N_TYPE) == N_SECT)
4039 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
4040 "ocGetNames_MachO(oc->symbols)");
4044 for(i=0;i<symLC->nsyms;i++)
4046 if(nlist[i].n_type & N_STAB)
4048 else if((nlist[i].n_type & N_TYPE) == N_SECT)
4050 if(nlist[i].n_type & N_EXT)
4052 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4053 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4055 + sections[nlist[i].n_sect-1].offset
4056 - sections[nlist[i].n_sect-1].addr
4057 + nlist[i].n_value);
4058 oc->symbols[curSymbol++] = nm;
4062 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4063 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm,
4065 + sections[nlist[i].n_sect-1].offset
4066 - sections[nlist[i].n_sect-1].addr
4067 + nlist[i].n_value);
4073 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
4074 commonCounter = (unsigned long)commonStorage;
4077 for(i=0;i<symLC->nsyms;i++)
4079 if((nlist[i].n_type & N_TYPE) == N_UNDF
4080 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
4082 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4083 unsigned long sz = nlist[i].n_value;
4085 nlist[i].n_value = commonCounter;
4087 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4088 (void*)commonCounter);
4089 oc->symbols[curSymbol++] = nm;
4091 commonCounter += sz;
4098 static int ocResolve_MachO(ObjectCode* oc)
4100 char *image = (char*) oc->image;
4101 struct mach_header *header = (struct mach_header*) image;
4102 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
4104 struct segment_command *segLC = NULL;
4105 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
4106 struct symtab_command *symLC = NULL;
4107 struct dysymtab_command *dsymLC = NULL;
4108 struct nlist *nlist;
4110 for(i=0;i<header->ncmds;i++)
4112 if(lc->cmd == LC_SEGMENT)
4113 segLC = (struct segment_command*) lc;
4114 else if(lc->cmd == LC_SYMTAB)
4115 symLC = (struct symtab_command*) lc;
4116 else if(lc->cmd == LC_DYSYMTAB)
4117 dsymLC = (struct dysymtab_command*) lc;
4118 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4121 sections = (struct section*) (segLC+1);
4122 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4125 for(i=0;i<segLC->nsects;i++)
4127 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
4128 la_ptrs = §ions[i];
4129 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
4130 nl_ptrs = §ions[i];
4131 else if(!strcmp(sections[i].sectname,"__la_sym_ptr2"))
4132 la_ptrs = §ions[i];
4133 else if(!strcmp(sections[i].sectname,"__la_sym_ptr3"))
4134 la_ptrs = §ions[i];
4139 unsigned long *indirectSyms
4140 = (unsigned long*) (image + dsymLC->indirectsymoff);
4143 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
4146 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
4150 for(i=0;i<segLC->nsects;i++)
4152 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
4156 /* Free the local symbol table; we won't need it again. */
4157 freeHashTable(oc->lochash, NULL);
4160 #if defined (powerpc_HOST_ARCH)
4161 ocFlushInstructionCache( oc );
4167 #ifdef powerpc_HOST_ARCH
4169 * The Mach-O object format uses leading underscores. But not everywhere.
4170 * There is a small number of runtime support functions defined in
4171 * libcc_dynamic.a whose name does not have a leading underscore.
4172 * As a consequence, we can't get their address from C code.
4173 * We have to use inline assembler just to take the address of a function.
4177 static void machoInitSymbolsWithoutUnderscore()
4179 extern void* symbolsWithoutUnderscore[];
4180 void **p = symbolsWithoutUnderscore;
4181 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
4185 __asm__ volatile(".long " # x);
4187 RTS_MACHO_NOUNDERLINE_SYMBOLS
4189 __asm__ volatile(".text");
4193 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
4195 RTS_MACHO_NOUNDERLINE_SYMBOLS
4202 * Figure out by how much to shift the entire Mach-O file in memory
4203 * when loading so that its single segment ends up 16-byte-aligned
4205 static int machoGetMisalignment( FILE * f )
4207 struct mach_header header;
4210 fread(&header, sizeof(header), 1, f);
4213 if(header.magic != MH_MAGIC)
4216 misalignment = (header.sizeofcmds + sizeof(header))
4219 return misalignment ? (16 - misalignment) : 0;