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_ForeignObj SymX(mkForeignObjzh_fast)
137 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
138 SymX(makeStableNamezh_fast) \
139 SymX(finalizzeWeakzh_fast)
141 /* These are not available in GUM!!! -- HWL */
142 #define Maybe_ForeignObj
143 #define Maybe_Stable_Names
146 #if !defined (mingw32_HOST_OS)
147 #define RTS_POSIX_ONLY_SYMBOLS \
148 SymX(stg_sig_install) \
152 #if defined (cygwin32_HOST_OS)
153 #define RTS_MINGW_ONLY_SYMBOLS /**/
154 /* Don't have the ability to read import libs / archives, so
155 * we have to stupidly list a lot of what libcygwin.a
158 #define RTS_CYGWIN_ONLY_SYMBOLS \
236 #elif !defined(mingw32_HOST_OS)
237 #define RTS_MINGW_ONLY_SYMBOLS /**/
238 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
239 #else /* defined(mingw32_HOST_OS) */
240 #define RTS_POSIX_ONLY_SYMBOLS /**/
241 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
243 /* Extra syms gen'ed by mingw-2's gcc-3.2: */
245 #define RTS_MINGW_EXTRA_SYMS \
246 Sym(_imp____mb_cur_max) \
249 #define RTS_MINGW_EXTRA_SYMS
252 /* These are statically linked from the mingw libraries into the ghc
253 executable, so we have to employ this hack. */
254 #define RTS_MINGW_ONLY_SYMBOLS \
255 SymX(asyncReadzh_fast) \
256 SymX(asyncWritezh_fast) \
257 SymX(asyncDoProczh_fast) \
269 SymX(getservbyname) \
270 SymX(getservbyport) \
271 SymX(getprotobynumber) \
272 SymX(getprotobyname) \
273 SymX(gethostbyname) \
274 SymX(gethostbyaddr) \
308 SymX(rts_InstallConsoleEvent) \
309 SymX(rts_ConsoleHandlerDone) \
311 Sym(_imp___timezone) \
320 RTS_MINGW_EXTRA_SYMS \
325 # define MAIN_CAP_SYM SymX(MainCapability)
327 # define MAIN_CAP_SYM
330 #if !defined(mingw32_HOST_OS)
331 #define RTS_USER_SIGNALS_SYMBOLS \
332 SymX(startSignalHandler) \
333 SymX(setIOManagerPipe)
335 #define RTS_USER_SIGNALS_SYMBOLS /* nothing */
338 #ifdef TABLES_NEXT_TO_CODE
339 #define RTS_RET_SYMBOLS /* nothing */
341 #define RTS_RET_SYMBOLS \
342 SymX(stg_enter_ret) \
343 SymX(stg_gc_fun_ret) \
351 SymX(stg_ap_pv_ret) \
352 SymX(stg_ap_pp_ret) \
353 SymX(stg_ap_ppv_ret) \
354 SymX(stg_ap_ppp_ret) \
355 SymX(stg_ap_pppv_ret) \
356 SymX(stg_ap_pppp_ret) \
357 SymX(stg_ap_ppppp_ret) \
358 SymX(stg_ap_pppppp_ret)
361 #define RTS_SYMBOLS \
365 SymX(stg_enter_info) \
366 SymX(stg_gc_void_info) \
367 SymX(__stg_gc_enter_1) \
368 SymX(stg_gc_noregs) \
369 SymX(stg_gc_unpt_r1_info) \
370 SymX(stg_gc_unpt_r1) \
371 SymX(stg_gc_unbx_r1_info) \
372 SymX(stg_gc_unbx_r1) \
373 SymX(stg_gc_f1_info) \
375 SymX(stg_gc_d1_info) \
377 SymX(stg_gc_l1_info) \
380 SymX(stg_gc_fun_info) \
382 SymX(stg_gc_gen_info) \
383 SymX(stg_gc_gen_hp) \
385 SymX(stg_gen_yield) \
386 SymX(stg_yield_noregs) \
387 SymX(stg_yield_to_interpreter) \
388 SymX(stg_gen_block) \
389 SymX(stg_block_noregs) \
391 SymX(stg_block_takemvar) \
392 SymX(stg_block_putmvar) \
393 SymX(stg_seq_frame_info) \
395 SymX(MallocFailHook) \
397 SymX(OutOfHeapHook) \
398 SymX(StackOverflowHook) \
399 SymX(__encodeDouble) \
400 SymX(__encodeFloat) \
404 SymX(__gmpz_cmp_si) \
405 SymX(__gmpz_cmp_ui) \
406 SymX(__gmpz_get_si) \
407 SymX(__gmpz_get_ui) \
408 SymX(__int_encodeDouble) \
409 SymX(__int_encodeFloat) \
410 SymX(andIntegerzh_fast) \
411 SymX(atomicallyzh_fast) \
415 SymX(blockAsyncExceptionszh_fast) \
417 SymX(catchRetryzh_fast) \
418 SymX(catchSTMzh_fast) \
419 SymX(closure_flags) \
421 SymX(cmpIntegerzh_fast) \
422 SymX(cmpIntegerIntzh_fast) \
423 SymX(complementIntegerzh_fast) \
424 SymX(createAdjustor) \
425 SymX(decodeDoublezh_fast) \
426 SymX(decodeFloatzh_fast) \
429 SymX(deRefWeakzh_fast) \
430 SymX(deRefStablePtrzh_fast) \
431 SymX(divExactIntegerzh_fast) \
432 SymX(divModIntegerzh_fast) \
435 SymX(forkOS_createThread) \
436 SymX(freeHaskellFunctionPtr) \
437 SymX(freeStablePtr) \
438 SymX(gcdIntegerzh_fast) \
439 SymX(gcdIntegerIntzh_fast) \
440 SymX(gcdIntzh_fast) \
449 SymX(hs_perform_gc) \
450 SymX(hs_free_stable_ptr) \
451 SymX(hs_free_fun_ptr) \
453 SymX(int2Integerzh_fast) \
454 SymX(integer2Intzh_fast) \
455 SymX(integer2Wordzh_fast) \
456 SymX(isCurrentThreadBoundzh_fast) \
457 SymX(isDoubleDenormalized) \
458 SymX(isDoubleInfinite) \
460 SymX(isDoubleNegativeZero) \
461 SymX(isEmptyMVarzh_fast) \
462 SymX(isFloatDenormalized) \
463 SymX(isFloatInfinite) \
465 SymX(isFloatNegativeZero) \
466 SymX(killThreadzh_fast) \
469 SymX(makeStablePtrzh_fast) \
470 SymX(minusIntegerzh_fast) \
471 SymX(mkApUpd0zh_fast) \
472 SymX(myThreadIdzh_fast) \
473 SymX(labelThreadzh_fast) \
474 SymX(newArrayzh_fast) \
475 SymX(newBCOzh_fast) \
476 SymX(newByteArrayzh_fast) \
477 SymX_redirect(newCAF, newDynCAF) \
478 SymX(newMVarzh_fast) \
479 SymX(newMutVarzh_fast) \
480 SymX(newTVarzh_fast) \
481 SymX(atomicModifyMutVarzh_fast) \
482 SymX(newPinnedByteArrayzh_fast) \
483 SymX(orIntegerzh_fast) \
485 SymX(performMajorGC) \
486 SymX(plusIntegerzh_fast) \
489 SymX(putMVarzh_fast) \
490 SymX(quotIntegerzh_fast) \
491 SymX(quotRemIntegerzh_fast) \
493 SymX(raiseIOzh_fast) \
494 SymX(readTVarzh_fast) \
495 SymX(remIntegerzh_fast) \
496 SymX(resetNonBlockingFd) \
501 SymX(rts_checkSchedStatus) \
504 SymX(rts_evalLazyIO) \
505 SymX(rts_evalStableIO) \
509 SymX(rts_getDouble) \
514 SymX(rts_getFunPtr) \
515 SymX(rts_getStablePtr) \
516 SymX(rts_getThreadId) \
518 SymX(rts_getWord32) \
531 SymX(rts_mkStablePtr) \
539 SymX(rtsSupportsBoundThreads) \
541 SymX(__hscore_get_saved_termios) \
542 SymX(__hscore_set_saved_termios) \
544 SymX(startupHaskell) \
545 SymX(shutdownHaskell) \
546 SymX(shutdownHaskellAndExit) \
547 SymX(stable_ptr_table) \
548 SymX(stackOverflow) \
549 SymX(stg_CAF_BLACKHOLE_info) \
550 SymX(awakenBlockedQueue) \
551 SymX(stg_CHARLIKE_closure) \
552 SymX(stg_EMPTY_MVAR_info) \
553 SymX(stg_IND_STATIC_info) \
554 SymX(stg_INTLIKE_closure) \
555 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
556 SymX(stg_MUT_ARR_PTRS_FROZEN0_info) \
557 SymX(stg_WEAK_info) \
558 SymX(stg_ap_0_info) \
559 SymX(stg_ap_v_info) \
560 SymX(stg_ap_f_info) \
561 SymX(stg_ap_d_info) \
562 SymX(stg_ap_l_info) \
563 SymX(stg_ap_n_info) \
564 SymX(stg_ap_p_info) \
565 SymX(stg_ap_pv_info) \
566 SymX(stg_ap_pp_info) \
567 SymX(stg_ap_ppv_info) \
568 SymX(stg_ap_ppp_info) \
569 SymX(stg_ap_pppv_info) \
570 SymX(stg_ap_pppp_info) \
571 SymX(stg_ap_ppppp_info) \
572 SymX(stg_ap_pppppp_info) \
573 SymX(stg_ap_1_upd_info) \
574 SymX(stg_ap_2_upd_info) \
575 SymX(stg_ap_3_upd_info) \
576 SymX(stg_ap_4_upd_info) \
577 SymX(stg_ap_5_upd_info) \
578 SymX(stg_ap_6_upd_info) \
579 SymX(stg_ap_7_upd_info) \
581 SymX(stg_sel_0_upd_info) \
582 SymX(stg_sel_10_upd_info) \
583 SymX(stg_sel_11_upd_info) \
584 SymX(stg_sel_12_upd_info) \
585 SymX(stg_sel_13_upd_info) \
586 SymX(stg_sel_14_upd_info) \
587 SymX(stg_sel_15_upd_info) \
588 SymX(stg_sel_1_upd_info) \
589 SymX(stg_sel_2_upd_info) \
590 SymX(stg_sel_3_upd_info) \
591 SymX(stg_sel_4_upd_info) \
592 SymX(stg_sel_5_upd_info) \
593 SymX(stg_sel_6_upd_info) \
594 SymX(stg_sel_7_upd_info) \
595 SymX(stg_sel_8_upd_info) \
596 SymX(stg_sel_9_upd_info) \
597 SymX(stg_upd_frame_info) \
598 SymX(suspendThread) \
599 SymX(takeMVarzh_fast) \
600 SymX(timesIntegerzh_fast) \
601 SymX(tryPutMVarzh_fast) \
602 SymX(tryTakeMVarzh_fast) \
603 SymX(unblockAsyncExceptionszh_fast) \
605 SymX(unsafeThawArrayzh_fast) \
606 SymX(waitReadzh_fast) \
607 SymX(waitWritezh_fast) \
608 SymX(word2Integerzh_fast) \
609 SymX(writeTVarzh_fast) \
610 SymX(xorIntegerzh_fast) \
612 RTS_USER_SIGNALS_SYMBOLS
614 #ifdef SUPPORT_LONG_LONGS
615 #define RTS_LONG_LONG_SYMS \
616 SymX(int64ToIntegerzh_fast) \
617 SymX(word64ToIntegerzh_fast)
619 #define RTS_LONG_LONG_SYMS /* nothing */
622 // 64-bit support functions in libgcc.a
623 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
624 #define RTS_LIBGCC_SYMBOLS \
634 #elif defined(ia64_HOST_ARCH)
635 #define RTS_LIBGCC_SYMBOLS \
643 #define RTS_LIBGCC_SYMBOLS
646 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
647 // Symbols that don't have a leading underscore
648 // on Mac OS X. They have to receive special treatment,
649 // see machoInitSymbolsWithoutUnderscore()
650 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
655 /* entirely bogus claims about types of these symbols */
656 #define Sym(vvv) extern void vvv(void);
657 #define SymX(vvv) /**/
658 #define SymX_redirect(vvv,xxx) /**/
662 RTS_POSIX_ONLY_SYMBOLS
663 RTS_MINGW_ONLY_SYMBOLS
664 RTS_CYGWIN_ONLY_SYMBOLS
670 #ifdef LEADING_UNDERSCORE
671 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
673 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
676 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
678 #define SymX(vvv) Sym(vvv)
680 // SymX_redirect allows us to redirect references to one symbol to
681 // another symbol. See newCAF/newDynCAF for an example.
682 #define SymX_redirect(vvv,xxx) \
683 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
686 static RtsSymbolVal rtsSyms[] = {
690 RTS_POSIX_ONLY_SYMBOLS
691 RTS_MINGW_ONLY_SYMBOLS
692 RTS_CYGWIN_ONLY_SYMBOLS
694 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
695 // dyld stub code contains references to this,
696 // but it should never be called because we treat
697 // lazy pointers as nonlazy.
698 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
700 { 0, 0 } /* sentinel */
703 /* -----------------------------------------------------------------------------
704 * Insert symbols into hash tables, checking for duplicates.
706 static void ghciInsertStrHashTable ( char* obj_name,
712 if (lookupHashTable(table, (StgWord)key) == NULL)
714 insertStrHashTable(table, (StgWord)key, data);
719 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
721 "whilst processing object file\n"
723 "This could be caused by:\n"
724 " * Loading two different object files which export the same symbol\n"
725 " * Specifying the same object file twice on the GHCi command line\n"
726 " * An incorrect `package.conf' entry, causing some object to be\n"
728 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
737 /* -----------------------------------------------------------------------------
738 * initialize the object linker
742 static int linker_init_done = 0 ;
744 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
745 static void *dl_prog_handle;
748 /* dlopen(NULL,..) doesn't work so we grab libc explicitly */
749 #if defined(openbsd_HOST_OS)
750 static void *dl_libc_handle;
758 /* Make initLinker idempotent, so we can call it
759 before evey relevant operation; that means we
760 don't need to initialise the linker separately */
761 if (linker_init_done == 1) { return; } else {
762 linker_init_done = 1;
765 symhash = allocStrHashTable();
767 /* populate the symbol table with stuff from the RTS */
768 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
769 ghciInsertStrHashTable("(GHCi built-in symbols)",
770 symhash, sym->lbl, sym->addr);
772 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
773 machoInitSymbolsWithoutUnderscore();
776 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
777 # if defined(RTLD_DEFAULT)
778 dl_prog_handle = RTLD_DEFAULT;
780 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
781 # if defined(openbsd_HOST_OS)
782 dl_libc_handle = dlopen("libc.so", RTLD_LAZY);
784 # endif /* RTLD_DEFAULT */
788 /* -----------------------------------------------------------------------------
789 * Loading DLL or .so dynamic libraries
790 * -----------------------------------------------------------------------------
792 * Add a DLL from which symbols may be found. In the ELF case, just
793 * do RTLD_GLOBAL-style add, so no further messing around needs to
794 * happen in order that symbols in the loaded .so are findable --
795 * lookupSymbol() will subsequently see them by dlsym on the program's
796 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
798 * In the PEi386 case, open the DLLs and put handles to them in a
799 * linked list. When looking for a symbol, try all handles in the
800 * list. This means that we need to load even DLLs that are guaranteed
801 * to be in the ghc.exe image already, just so we can get a handle
802 * to give to loadSymbol, so that we can find the symbols. For such
803 * libraries, the LoadLibrary call should be a no-op except for returning
808 #if defined(OBJFORMAT_PEi386)
809 /* A record for storing handles into DLLs. */
814 struct _OpenedDLL* next;
819 /* A list thereof. */
820 static OpenedDLL* opened_dlls = NULL;
824 addDLL( char *dll_name )
826 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
827 /* ------------------- ELF DLL loader ------------------- */
833 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
836 /* dlopen failed; return a ptr to the error msg. */
838 if (errmsg == NULL) errmsg = "addDLL: unknown error";
845 # elif defined(OBJFORMAT_PEi386)
846 /* ------------------- Win32 DLL loader ------------------- */
854 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
856 /* See if we've already got it, and ignore if so. */
857 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
858 if (0 == strcmp(o_dll->name, dll_name))
862 /* The file name has no suffix (yet) so that we can try
863 both foo.dll and foo.drv
865 The documentation for LoadLibrary says:
866 If no file name extension is specified in the lpFileName
867 parameter, the default library extension .dll is
868 appended. However, the file name string can include a trailing
869 point character (.) to indicate that the module name has no
872 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
873 sprintf(buf, "%s.DLL", dll_name);
874 instance = LoadLibrary(buf);
875 if (instance == NULL) {
876 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
877 instance = LoadLibrary(buf);
878 if (instance == NULL) {
881 /* LoadLibrary failed; return a ptr to the error msg. */
882 return "addDLL: unknown error";
887 /* Add this DLL to the list of DLLs in which to search for symbols. */
888 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
889 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
890 strcpy(o_dll->name, dll_name);
891 o_dll->instance = instance;
892 o_dll->next = opened_dlls;
897 barf("addDLL: not implemented on this platform");
901 /* -----------------------------------------------------------------------------
902 * lookup a symbol in the hash table
905 lookupSymbol( char *lbl )
909 ASSERT(symhash != NULL);
910 val = lookupStrHashTable(symhash, lbl);
913 # if defined(OBJFORMAT_ELF)
914 # if defined(openbsd_HOST_OS)
915 val = dlsym(dl_prog_handle, lbl);
916 return (val != NULL) ? val : dlsym(dl_libc_handle,lbl);
917 # elif defined(x86_64_HOST_ARCH)
918 val = dlsym(dl_prog_handle, lbl);
919 if (val >= (void *)0x80000000) {
921 new_val = x86_64_high_symbol(lbl, val);
922 IF_DEBUG(linker,debugBelch("lookupSymbol: relocating out of range symbol: %s = %p, now %p\n", lbl, val, new_val));
927 # else /* not openbsd */
928 return dlsym(dl_prog_handle, lbl);
930 # elif defined(OBJFORMAT_MACHO)
931 if(NSIsSymbolNameDefined(lbl)) {
932 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
933 return NSAddressOfSymbol(symbol);
937 # elif defined(OBJFORMAT_PEi386)
940 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
941 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
943 /* HACK: if the name has an initial underscore, try stripping
944 it off & look that up first. I've yet to verify whether there's
945 a Rule that governs whether an initial '_' *should always* be
946 stripped off when mapping from import lib name to the DLL name.
948 sym = GetProcAddress(o_dll->instance, (lbl+1));
950 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
954 sym = GetProcAddress(o_dll->instance, lbl);
956 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
971 __attribute((unused))
973 lookupLocalSymbol( ObjectCode* oc, char *lbl )
977 val = lookupStrHashTable(oc->lochash, lbl);
987 /* -----------------------------------------------------------------------------
988 * Debugging aid: look in GHCi's object symbol tables for symbols
989 * within DELTA bytes of the specified address, and show their names.
992 void ghci_enquire ( char* addr );
994 void ghci_enquire ( char* addr )
999 const int DELTA = 64;
1004 for (oc = objects; oc; oc = oc->next) {
1005 for (i = 0; i < oc->n_symbols; i++) {
1006 sym = oc->symbols[i];
1007 if (sym == NULL) continue;
1008 // debugBelch("enquire %p %p\n", sym, oc->lochash);
1010 if (oc->lochash != NULL) {
1011 a = lookupStrHashTable(oc->lochash, sym);
1014 a = lookupStrHashTable(symhash, sym);
1017 // debugBelch("ghci_enquire: can't find %s\n", sym);
1019 else if (addr-DELTA <= a && a <= addr+DELTA) {
1020 debugBelch("%p + %3d == `%s'\n", addr, a - addr, sym);
1027 #ifdef ia64_HOST_ARCH
1028 static unsigned int PLTSize(void);
1031 /* -----------------------------------------------------------------------------
1032 * Load an obj (populate the global symbol table, but don't resolve yet)
1034 * Returns: 1 if ok, 0 on error.
1037 loadObj( char *path )
1044 void *map_addr = NULL;
1051 /* debugBelch("loadObj %s\n", path ); */
1053 /* Check that we haven't already loaded this object.
1054 Ignore requests to load multiple times */
1058 for (o = objects; o; o = o->next) {
1059 if (0 == strcmp(o->fileName, path)) {
1061 break; /* don't need to search further */
1065 IF_DEBUG(linker, debugBelch(
1066 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1067 "same object file twice:\n"
1069 "GHCi will ignore this, but be warned.\n"
1071 return 1; /* success */
1075 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
1077 # if defined(OBJFORMAT_ELF)
1078 oc->formatName = "ELF";
1079 # elif defined(OBJFORMAT_PEi386)
1080 oc->formatName = "PEi386";
1081 # elif defined(OBJFORMAT_MACHO)
1082 oc->formatName = "Mach-O";
1085 barf("loadObj: not implemented on this platform");
1088 r = stat(path, &st);
1089 if (r == -1) { return 0; }
1091 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1092 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1093 strcpy(oc->fileName, path);
1095 oc->fileSize = st.st_size;
1097 oc->sections = NULL;
1098 oc->lochash = allocStrHashTable();
1099 oc->proddables = NULL;
1101 /* chain it onto the list of objects */
1106 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1108 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1110 #if defined(openbsd_HOST_OS)
1111 fd = open(path, O_RDONLY, S_IRUSR);
1113 fd = open(path, O_RDONLY);
1116 barf("loadObj: can't open `%s'", path);
1118 pagesize = getpagesize();
1120 #ifdef ia64_HOST_ARCH
1121 /* The PLT needs to be right before the object */
1122 n = ROUND_UP(PLTSize(), pagesize);
1123 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1124 if (oc->plt == MAP_FAILED)
1125 barf("loadObj: can't allocate PLT");
1128 map_addr = oc->plt + n;
1131 n = ROUND_UP(oc->fileSize, pagesize);
1133 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
1134 * small memory model on this architecture (see gcc docs,
1137 #ifdef x86_64_HOST_ARCH
1138 #define EXTRA_MAP_FLAGS MAP_32BIT
1140 #define EXTRA_MAP_FLAGS 0
1143 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE,
1144 MAP_PRIVATE|EXTRA_MAP_FLAGS, fd, 0);
1145 if (oc->image == MAP_FAILED)
1146 barf("loadObj: can't map `%s'", path);
1150 #else /* !USE_MMAP */
1152 /* load the image into memory */
1153 f = fopen(path, "rb");
1155 barf("loadObj: can't read `%s'", path);
1157 #ifdef darwin_HOST_OS
1158 // In a Mach-O .o file, all sections can and will be misaligned
1159 // if the total size of the headers is not a multiple of the
1160 // desired alignment. This is fine for .o files that only serve
1161 // as input for the static linker, but it's not fine for us,
1162 // as SSE (used by gcc for floating point) and Altivec require
1163 // 16-byte alignment.
1164 // We calculate the correct alignment from the header before
1165 // reading the file, and then we misalign oc->image on purpose so
1166 // that the actual sections end up aligned again.
1167 misalignment = machoGetMisalignment(f);
1172 oc->image = stgMallocBytes(oc->fileSize + misalignment, "loadObj(image)");
1173 oc->image += misalignment;
1175 n = fread ( oc->image, 1, oc->fileSize, f );
1176 if (n != oc->fileSize)
1177 barf("loadObj: error whilst reading `%s'", path);
1181 #endif /* USE_MMAP */
1183 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1184 r = ocAllocateJumpIslands_MachO ( oc );
1185 if (!r) { return r; }
1186 # elif defined(OBJFORMAT_ELF) && defined(powerpc_HOST_ARCH)
1187 r = ocAllocateJumpIslands_ELF ( oc );
1188 if (!r) { return r; }
1191 /* verify the in-memory image */
1192 # if defined(OBJFORMAT_ELF)
1193 r = ocVerifyImage_ELF ( oc );
1194 # elif defined(OBJFORMAT_PEi386)
1195 r = ocVerifyImage_PEi386 ( oc );
1196 # elif defined(OBJFORMAT_MACHO)
1197 r = ocVerifyImage_MachO ( oc );
1199 barf("loadObj: no verify method");
1201 if (!r) { return r; }
1203 /* build the symbol list for this image */
1204 # if defined(OBJFORMAT_ELF)
1205 r = ocGetNames_ELF ( oc );
1206 # elif defined(OBJFORMAT_PEi386)
1207 r = ocGetNames_PEi386 ( oc );
1208 # elif defined(OBJFORMAT_MACHO)
1209 r = ocGetNames_MachO ( oc );
1211 barf("loadObj: no getNames method");
1213 if (!r) { return r; }
1215 /* loaded, but not resolved yet */
1216 oc->status = OBJECT_LOADED;
1221 /* -----------------------------------------------------------------------------
1222 * resolve all the currently unlinked objects in memory
1224 * Returns: 1 if ok, 0 on error.
1234 for (oc = objects; oc; oc = oc->next) {
1235 if (oc->status != OBJECT_RESOLVED) {
1236 # if defined(OBJFORMAT_ELF)
1237 r = ocResolve_ELF ( oc );
1238 # elif defined(OBJFORMAT_PEi386)
1239 r = ocResolve_PEi386 ( oc );
1240 # elif defined(OBJFORMAT_MACHO)
1241 r = ocResolve_MachO ( oc );
1243 barf("resolveObjs: not implemented on this platform");
1245 if (!r) { return r; }
1246 oc->status = OBJECT_RESOLVED;
1252 /* -----------------------------------------------------------------------------
1253 * delete an object from the pool
1256 unloadObj( char *path )
1258 ObjectCode *oc, *prev;
1260 ASSERT(symhash != NULL);
1261 ASSERT(objects != NULL);
1266 for (oc = objects; oc; prev = oc, oc = oc->next) {
1267 if (!strcmp(oc->fileName,path)) {
1269 /* Remove all the mappings for the symbols within this
1274 for (i = 0; i < oc->n_symbols; i++) {
1275 if (oc->symbols[i] != NULL) {
1276 removeStrHashTable(symhash, oc->symbols[i], NULL);
1284 prev->next = oc->next;
1287 /* We're going to leave this in place, in case there are
1288 any pointers from the heap into it: */
1289 /* stgFree(oc->image); */
1290 stgFree(oc->fileName);
1291 stgFree(oc->symbols);
1292 stgFree(oc->sections);
1293 /* The local hash table should have been freed at the end
1294 of the ocResolve_ call on it. */
1295 ASSERT(oc->lochash == NULL);
1301 errorBelch("unloadObj: can't find `%s' to unload", path);
1305 /* -----------------------------------------------------------------------------
1306 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1307 * which may be prodded during relocation, and abort if we try and write
1308 * outside any of these.
1310 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1313 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1314 /* debugBelch("aPB %p %p %d\n", oc, start, size); */
1318 pb->next = oc->proddables;
1319 oc->proddables = pb;
1322 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1325 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1326 char* s = (char*)(pb->start);
1327 char* e = s + pb->size - 1;
1328 char* a = (char*)addr;
1329 /* Assumes that the biggest fixup involves a 4-byte write. This
1330 probably needs to be changed to 8 (ie, +7) on 64-bit
1332 if (a >= s && (a+3) <= e) return;
1334 barf("checkProddableBlock: invalid fixup in runtime linker");
1337 /* -----------------------------------------------------------------------------
1338 * Section management.
1340 static void addSection ( ObjectCode* oc, SectionKind kind,
1341 void* start, void* end )
1343 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1347 s->next = oc->sections;
1350 debugBelch("addSection: %p-%p (size %d), kind %d\n",
1351 start, ((char*)end)-1, end - start + 1, kind );
1356 /* --------------------------------------------------------------------------
1357 * PowerPC specifics (jump islands)
1358 * ------------------------------------------------------------------------*/
1360 #if defined(powerpc_HOST_ARCH)
1363 ocAllocateJumpIslands
1365 Allocate additional space at the end of the object file image to make room
1368 PowerPC relative branch instructions have a 24 bit displacement field.
1369 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
1370 If a particular imported symbol is outside this range, we have to redirect
1371 the jump to a short piece of new code that just loads the 32bit absolute
1372 address and jumps there.
1373 This function just allocates space for one 16 byte ppcJumpIsland for every
1374 undefined symbol in the object file. The code for the islands is filled in by
1375 makeJumpIsland below.
1378 static int ocAllocateJumpIslands( ObjectCode* oc, int count, int first )
1387 // round up to the nearest 4
1388 aligned = (oc->fileSize + 3) & ~3;
1391 #ifndef linux_HOST_OS /* mremap is a linux extension */
1392 #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined
1395 pagesize = getpagesize();
1396 n = ROUND_UP( oc->fileSize, pagesize );
1397 m = ROUND_UP( aligned + sizeof (ppcJumpIsland) * count, pagesize );
1399 /* The effect of this mremap() call is only the ensure that we have
1400 * a sufficient number of virtually contiguous pages. As returned from
1401 * mremap, the pages past the end of the file are not backed. We give
1402 * them a backing by using MAP_FIXED to map in anonymous pages.
1404 if( (oc->image = mremap( oc->image, n, m, MREMAP_MAYMOVE )) == MAP_FAILED )
1406 errorBelch( "Unable to mremap for Jump Islands\n" );
1410 if( mmap( oc->image + n, m - n, PROT_READ | PROT_WRITE | PROT_EXEC,
1411 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0 ) == MAP_FAILED )
1413 errorBelch( "Unable to mmap( MAP_FIXED ) for Jump Islands\n" );
1418 oc->image = stgReallocBytes( oc->image,
1419 aligned + sizeof (ppcJumpIsland) * count,
1420 "ocAllocateJumpIslands" );
1421 #endif /* USE_MMAP */
1423 oc->jump_islands = (ppcJumpIsland *) (oc->image + aligned);
1424 memset( oc->jump_islands, 0, sizeof (ppcJumpIsland) * count );
1427 oc->jump_islands = NULL;
1429 oc->island_start_symbol = first;
1430 oc->n_islands = count;
1435 static unsigned long makeJumpIsland( ObjectCode* oc,
1436 unsigned long symbolNumber,
1437 unsigned long target )
1439 ppcJumpIsland *island;
1441 if( symbolNumber < oc->island_start_symbol ||
1442 symbolNumber - oc->island_start_symbol > oc->n_islands)
1445 island = &oc->jump_islands[symbolNumber - oc->island_start_symbol];
1447 // lis r12, hi16(target)
1448 island->lis_r12 = 0x3d80;
1449 island->hi_addr = target >> 16;
1451 // ori r12, r12, lo16(target)
1452 island->ori_r12_r12 = 0x618c;
1453 island->lo_addr = target & 0xffff;
1456 island->mtctr_r12 = 0x7d8903a6;
1459 island->bctr = 0x4e800420;
1461 return (unsigned long) island;
1465 ocFlushInstructionCache
1467 Flush the data & instruction caches.
1468 Because the PPC has split data/instruction caches, we have to
1469 do that whenever we modify code at runtime.
1472 static void ocFlushInstructionCache( ObjectCode *oc )
1474 int n = (oc->fileSize + sizeof( ppcJumpIsland ) * oc->n_islands + 3) / 4;
1475 unsigned long *p = (unsigned long *) oc->image;
1479 __asm__ volatile ( "dcbf 0,%0\n\t"
1487 __asm__ volatile ( "sync\n\t"
1493 /* --------------------------------------------------------------------------
1494 * PEi386 specifics (Win32 targets)
1495 * ------------------------------------------------------------------------*/
1497 /* The information for this linker comes from
1498 Microsoft Portable Executable
1499 and Common Object File Format Specification
1500 revision 5.1 January 1998
1501 which SimonM says comes from the MS Developer Network CDs.
1503 It can be found there (on older CDs), but can also be found
1506 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1508 (this is Rev 6.0 from February 1999).
1510 Things move, so if that fails, try searching for it via
1512 http://www.google.com/search?q=PE+COFF+specification
1514 The ultimate reference for the PE format is the Winnt.h
1515 header file that comes with the Platform SDKs; as always,
1516 implementations will drift wrt their documentation.
1518 A good background article on the PE format is Matt Pietrek's
1519 March 1994 article in Microsoft System Journal (MSJ)
1520 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1521 Win32 Portable Executable File Format." The info in there
1522 has recently been updated in a two part article in
1523 MSDN magazine, issues Feb and March 2002,
1524 "Inside Windows: An In-Depth Look into the Win32 Portable
1525 Executable File Format"
1527 John Levine's book "Linkers and Loaders" contains useful
1532 #if defined(OBJFORMAT_PEi386)
1536 typedef unsigned char UChar;
1537 typedef unsigned short UInt16;
1538 typedef unsigned int UInt32;
1545 UInt16 NumberOfSections;
1546 UInt32 TimeDateStamp;
1547 UInt32 PointerToSymbolTable;
1548 UInt32 NumberOfSymbols;
1549 UInt16 SizeOfOptionalHeader;
1550 UInt16 Characteristics;
1554 #define sizeof_COFF_header 20
1561 UInt32 VirtualAddress;
1562 UInt32 SizeOfRawData;
1563 UInt32 PointerToRawData;
1564 UInt32 PointerToRelocations;
1565 UInt32 PointerToLinenumbers;
1566 UInt16 NumberOfRelocations;
1567 UInt16 NumberOfLineNumbers;
1568 UInt32 Characteristics;
1572 #define sizeof_COFF_section 40
1579 UInt16 SectionNumber;
1582 UChar NumberOfAuxSymbols;
1586 #define sizeof_COFF_symbol 18
1591 UInt32 VirtualAddress;
1592 UInt32 SymbolTableIndex;
1597 #define sizeof_COFF_reloc 10
1600 /* From PE spec doc, section 3.3.2 */
1601 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1602 windows.h -- for the same purpose, but I want to know what I'm
1604 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1605 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1606 #define MYIMAGE_FILE_DLL 0x2000
1607 #define MYIMAGE_FILE_SYSTEM 0x1000
1608 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1609 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1610 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1612 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1613 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1614 #define MYIMAGE_SYM_CLASS_STATIC 3
1615 #define MYIMAGE_SYM_UNDEFINED 0
1617 /* From PE spec doc, section 4.1 */
1618 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1619 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1620 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1622 /* From PE spec doc, section 5.2.1 */
1623 #define MYIMAGE_REL_I386_DIR32 0x0006
1624 #define MYIMAGE_REL_I386_REL32 0x0014
1627 /* We use myindex to calculate array addresses, rather than
1628 simply doing the normal subscript thing. That's because
1629 some of the above structs have sizes which are not
1630 a whole number of words. GCC rounds their sizes up to a
1631 whole number of words, which means that the address calcs
1632 arising from using normal C indexing or pointer arithmetic
1633 are just plain wrong. Sigh.
1636 myindex ( int scale, void* base, int index )
1639 ((UChar*)base) + scale * index;
1644 printName ( UChar* name, UChar* strtab )
1646 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1647 UInt32 strtab_offset = * (UInt32*)(name+4);
1648 debugBelch("%s", strtab + strtab_offset );
1651 for (i = 0; i < 8; i++) {
1652 if (name[i] == 0) break;
1653 debugBelch("%c", name[i] );
1660 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1662 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1663 UInt32 strtab_offset = * (UInt32*)(name+4);
1664 strncpy ( dst, strtab+strtab_offset, dstSize );
1670 if (name[i] == 0) break;
1680 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1683 /* If the string is longer than 8 bytes, look in the
1684 string table for it -- this will be correctly zero terminated.
1686 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1687 UInt32 strtab_offset = * (UInt32*)(name+4);
1688 return ((UChar*)strtab) + strtab_offset;
1690 /* Otherwise, if shorter than 8 bytes, return the original,
1691 which by defn is correctly terminated.
1693 if (name[7]==0) return name;
1694 /* The annoying case: 8 bytes. Copy into a temporary
1695 (which is never freed ...)
1697 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1699 strncpy(newstr,name,8);
1705 /* Just compares the short names (first 8 chars) */
1706 static COFF_section *
1707 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1711 = (COFF_header*)(oc->image);
1712 COFF_section* sectab
1714 ((UChar*)(oc->image))
1715 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1717 for (i = 0; i < hdr->NumberOfSections; i++) {
1720 COFF_section* section_i
1722 myindex ( sizeof_COFF_section, sectab, i );
1723 n1 = (UChar*) &(section_i->Name);
1725 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1726 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1727 n1[6]==n2[6] && n1[7]==n2[7])
1736 zapTrailingAtSign ( UChar* sym )
1738 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1740 if (sym[0] == 0) return;
1742 while (sym[i] != 0) i++;
1745 while (j > 0 && my_isdigit(sym[j])) j--;
1746 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1752 ocVerifyImage_PEi386 ( ObjectCode* oc )
1757 COFF_section* sectab;
1758 COFF_symbol* symtab;
1760 /* debugBelch("\nLOADING %s\n", oc->fileName); */
1761 hdr = (COFF_header*)(oc->image);
1762 sectab = (COFF_section*) (
1763 ((UChar*)(oc->image))
1764 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1766 symtab = (COFF_symbol*) (
1767 ((UChar*)(oc->image))
1768 + hdr->PointerToSymbolTable
1770 strtab = ((UChar*)symtab)
1771 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1773 if (hdr->Machine != 0x14c) {
1774 errorBelch("%s: Not x86 PEi386", oc->fileName);
1777 if (hdr->SizeOfOptionalHeader != 0) {
1778 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
1781 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1782 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1783 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1784 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1785 errorBelch("%s: Not a PEi386 object file", oc->fileName);
1788 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1789 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1790 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
1792 (int)(hdr->Characteristics));
1795 /* If the string table size is way crazy, this might indicate that
1796 there are more than 64k relocations, despite claims to the
1797 contrary. Hence this test. */
1798 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
1800 if ( (*(UInt32*)strtab) > 600000 ) {
1801 /* Note that 600k has no special significance other than being
1802 big enough to handle the almost-2MB-sized lumps that
1803 constitute HSwin32*.o. */
1804 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
1809 /* No further verification after this point; only debug printing. */
1811 IF_DEBUG(linker, i=1);
1812 if (i == 0) return 1;
1814 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1815 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1816 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1819 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1820 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1821 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1822 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1823 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1824 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1825 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1827 /* Print the section table. */
1829 for (i = 0; i < hdr->NumberOfSections; i++) {
1831 COFF_section* sectab_i
1833 myindex ( sizeof_COFF_section, sectab, i );
1840 printName ( sectab_i->Name, strtab );
1850 sectab_i->VirtualSize,
1851 sectab_i->VirtualAddress,
1852 sectab_i->SizeOfRawData,
1853 sectab_i->PointerToRawData,
1854 sectab_i->NumberOfRelocations,
1855 sectab_i->PointerToRelocations,
1856 sectab_i->PointerToRawData
1858 reltab = (COFF_reloc*) (
1859 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1862 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1863 /* If the relocation field (a short) has overflowed, the
1864 * real count can be found in the first reloc entry.
1866 * See Section 4.1 (last para) of the PE spec (rev6.0).
1868 COFF_reloc* rel = (COFF_reloc*)
1869 myindex ( sizeof_COFF_reloc, reltab, 0 );
1870 noRelocs = rel->VirtualAddress;
1873 noRelocs = sectab_i->NumberOfRelocations;
1877 for (; j < noRelocs; j++) {
1879 COFF_reloc* rel = (COFF_reloc*)
1880 myindex ( sizeof_COFF_reloc, reltab, j );
1882 " type 0x%-4x vaddr 0x%-8x name `",
1884 rel->VirtualAddress );
1885 sym = (COFF_symbol*)
1886 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1887 /* Hmm..mysterious looking offset - what's it for? SOF */
1888 printName ( sym->Name, strtab -10 );
1895 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
1896 debugBelch("---START of string table---\n");
1897 for (i = 4; i < *(Int32*)strtab; i++) {
1899 debugBelch("\n"); else
1900 debugBelch("%c", strtab[i] );
1902 debugBelch("--- END of string table---\n");
1907 COFF_symbol* symtab_i;
1908 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1909 symtab_i = (COFF_symbol*)
1910 myindex ( sizeof_COFF_symbol, symtab, i );
1916 printName ( symtab_i->Name, strtab );
1925 (Int32)(symtab_i->SectionNumber),
1926 (UInt32)symtab_i->Type,
1927 (UInt32)symtab_i->StorageClass,
1928 (UInt32)symtab_i->NumberOfAuxSymbols
1930 i += symtab_i->NumberOfAuxSymbols;
1940 ocGetNames_PEi386 ( ObjectCode* oc )
1943 COFF_section* sectab;
1944 COFF_symbol* symtab;
1951 hdr = (COFF_header*)(oc->image);
1952 sectab = (COFF_section*) (
1953 ((UChar*)(oc->image))
1954 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1956 symtab = (COFF_symbol*) (
1957 ((UChar*)(oc->image))
1958 + hdr->PointerToSymbolTable
1960 strtab = ((UChar*)(oc->image))
1961 + hdr->PointerToSymbolTable
1962 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1964 /* Allocate space for any (local, anonymous) .bss sections. */
1966 for (i = 0; i < hdr->NumberOfSections; i++) {
1968 COFF_section* sectab_i
1970 myindex ( sizeof_COFF_section, sectab, i );
1971 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1972 if (sectab_i->VirtualSize == 0) continue;
1973 /* This is a non-empty .bss section. Allocate zeroed space for
1974 it, and set its PointerToRawData field such that oc->image +
1975 PointerToRawData == addr_of_zeroed_space. */
1976 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1977 "ocGetNames_PEi386(anonymous bss)");
1978 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1979 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1980 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
1983 /* Copy section information into the ObjectCode. */
1985 for (i = 0; i < hdr->NumberOfSections; i++) {
1991 = SECTIONKIND_OTHER;
1992 COFF_section* sectab_i
1994 myindex ( sizeof_COFF_section, sectab, i );
1995 IF_DEBUG(linker, debugBelch("section name = %s\n", sectab_i->Name ));
1998 /* I'm sure this is the Right Way to do it. However, the
1999 alternative of testing the sectab_i->Name field seems to
2000 work ok with Cygwin.
2002 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
2003 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
2004 kind = SECTIONKIND_CODE_OR_RODATA;
2007 if (0==strcmp(".text",sectab_i->Name) ||
2008 0==strcmp(".rdata",sectab_i->Name)||
2009 0==strcmp(".rodata",sectab_i->Name))
2010 kind = SECTIONKIND_CODE_OR_RODATA;
2011 if (0==strcmp(".data",sectab_i->Name) ||
2012 0==strcmp(".bss",sectab_i->Name))
2013 kind = SECTIONKIND_RWDATA;
2015 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
2016 sz = sectab_i->SizeOfRawData;
2017 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
2019 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
2020 end = start + sz - 1;
2022 if (kind == SECTIONKIND_OTHER
2023 /* Ignore sections called which contain stabs debugging
2025 && 0 != strcmp(".stab", sectab_i->Name)
2026 && 0 != strcmp(".stabstr", sectab_i->Name)
2027 /* ignore constructor section for now */
2028 && 0 != strcmp(".ctors", sectab_i->Name)
2030 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", sectab_i->Name, oc->fileName);
2034 if (kind != SECTIONKIND_OTHER && end >= start) {
2035 addSection(oc, kind, start, end);
2036 addProddableBlock(oc, start, end - start + 1);
2040 /* Copy exported symbols into the ObjectCode. */
2042 oc->n_symbols = hdr->NumberOfSymbols;
2043 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2044 "ocGetNames_PEi386(oc->symbols)");
2045 /* Call me paranoid; I don't care. */
2046 for (i = 0; i < oc->n_symbols; i++)
2047 oc->symbols[i] = NULL;
2051 COFF_symbol* symtab_i;
2052 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2053 symtab_i = (COFF_symbol*)
2054 myindex ( sizeof_COFF_symbol, symtab, i );
2058 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
2059 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
2060 /* This symbol is global and defined, viz, exported */
2061 /* for MYIMAGE_SYMCLASS_EXTERNAL
2062 && !MYIMAGE_SYM_UNDEFINED,
2063 the address of the symbol is:
2064 address of relevant section + offset in section
2066 COFF_section* sectabent
2067 = (COFF_section*) myindex ( sizeof_COFF_section,
2069 symtab_i->SectionNumber-1 );
2070 addr = ((UChar*)(oc->image))
2071 + (sectabent->PointerToRawData
2075 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
2076 && symtab_i->Value > 0) {
2077 /* This symbol isn't in any section at all, ie, global bss.
2078 Allocate zeroed space for it. */
2079 addr = stgCallocBytes(1, symtab_i->Value,
2080 "ocGetNames_PEi386(non-anonymous bss)");
2081 addSection(oc, SECTIONKIND_RWDATA, addr,
2082 ((UChar*)addr) + symtab_i->Value - 1);
2083 addProddableBlock(oc, addr, symtab_i->Value);
2084 /* debugBelch("BSS section at 0x%x\n", addr); */
2087 if (addr != NULL ) {
2088 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
2089 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
2090 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
2091 ASSERT(i >= 0 && i < oc->n_symbols);
2092 /* cstring_from_COFF_symbol_name always succeeds. */
2093 oc->symbols[i] = sname;
2094 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
2098 "IGNORING symbol %d\n"
2102 printName ( symtab_i->Name, strtab );
2111 (Int32)(symtab_i->SectionNumber),
2112 (UInt32)symtab_i->Type,
2113 (UInt32)symtab_i->StorageClass,
2114 (UInt32)symtab_i->NumberOfAuxSymbols
2119 i += symtab_i->NumberOfAuxSymbols;
2128 ocResolve_PEi386 ( ObjectCode* oc )
2131 COFF_section* sectab;
2132 COFF_symbol* symtab;
2142 /* ToDo: should be variable-sized? But is at least safe in the
2143 sense of buffer-overrun-proof. */
2145 /* debugBelch("resolving for %s\n", oc->fileName); */
2147 hdr = (COFF_header*)(oc->image);
2148 sectab = (COFF_section*) (
2149 ((UChar*)(oc->image))
2150 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2152 symtab = (COFF_symbol*) (
2153 ((UChar*)(oc->image))
2154 + hdr->PointerToSymbolTable
2156 strtab = ((UChar*)(oc->image))
2157 + hdr->PointerToSymbolTable
2158 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2160 for (i = 0; i < hdr->NumberOfSections; i++) {
2161 COFF_section* sectab_i
2163 myindex ( sizeof_COFF_section, sectab, i );
2166 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2169 /* Ignore sections called which contain stabs debugging
2171 if (0 == strcmp(".stab", sectab_i->Name)
2172 || 0 == strcmp(".stabstr", sectab_i->Name)
2173 || 0 == strcmp(".ctors", sectab_i->Name))
2176 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2177 /* If the relocation field (a short) has overflowed, the
2178 * real count can be found in the first reloc entry.
2180 * See Section 4.1 (last para) of the PE spec (rev6.0).
2182 * Nov2003 update: the GNU linker still doesn't correctly
2183 * handle the generation of relocatable object files with
2184 * overflown relocations. Hence the output to warn of potential
2187 COFF_reloc* rel = (COFF_reloc*)
2188 myindex ( sizeof_COFF_reloc, reltab, 0 );
2189 noRelocs = rel->VirtualAddress;
2190 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
2194 noRelocs = sectab_i->NumberOfRelocations;
2199 for (; j < noRelocs; j++) {
2201 COFF_reloc* reltab_j
2203 myindex ( sizeof_COFF_reloc, reltab, j );
2205 /* the location to patch */
2207 ((UChar*)(oc->image))
2208 + (sectab_i->PointerToRawData
2209 + reltab_j->VirtualAddress
2210 - sectab_i->VirtualAddress )
2212 /* the existing contents of pP */
2214 /* the symbol to connect to */
2215 sym = (COFF_symbol*)
2216 myindex ( sizeof_COFF_symbol,
2217 symtab, reltab_j->SymbolTableIndex );
2220 "reloc sec %2d num %3d: type 0x%-4x "
2221 "vaddr 0x%-8x name `",
2223 (UInt32)reltab_j->Type,
2224 reltab_j->VirtualAddress );
2225 printName ( sym->Name, strtab );
2226 debugBelch("'\n" ));
2228 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
2229 COFF_section* section_sym
2230 = findPEi386SectionCalled ( oc, sym->Name );
2232 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
2235 S = ((UInt32)(oc->image))
2236 + (section_sym->PointerToRawData
2239 copyName ( sym->Name, strtab, symbol, 1000-1 );
2240 (void*)S = lookupLocalSymbol( oc, symbol );
2241 if ((void*)S != NULL) goto foundit;
2242 (void*)S = lookupSymbol( symbol );
2243 if ((void*)S != NULL) goto foundit;
2244 zapTrailingAtSign ( symbol );
2245 (void*)S = lookupLocalSymbol( oc, symbol );
2246 if ((void*)S != NULL) goto foundit;
2247 (void*)S = lookupSymbol( symbol );
2248 if ((void*)S != NULL) goto foundit;
2249 /* Newline first because the interactive linker has printed "linking..." */
2250 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
2254 checkProddableBlock(oc, pP);
2255 switch (reltab_j->Type) {
2256 case MYIMAGE_REL_I386_DIR32:
2259 case MYIMAGE_REL_I386_REL32:
2260 /* Tricky. We have to insert a displacement at
2261 pP which, when added to the PC for the _next_
2262 insn, gives the address of the target (S).
2263 Problem is to know the address of the next insn
2264 when we only know pP. We assume that this
2265 literal field is always the last in the insn,
2266 so that the address of the next insn is pP+4
2267 -- hence the constant 4.
2268 Also I don't know if A should be added, but so
2269 far it has always been zero.
2271 SOF 05/2005: 'A' (old contents of *pP) have been observed
2272 to contain values other than zero (the 'wx' object file
2273 that came with wxhaskell-0.9.4; dunno how it was compiled..).
2274 So, add displacement to old value instead of asserting
2275 A to be zero. Fixes wxhaskell-related crashes, and no other
2276 ill effects have been observed.
2278 Update: the reason why we're seeing these more elaborate
2279 relocations is due to a switch in how the NCG compiles SRTs
2280 and offsets to them from info tables. SRTs live in .(ro)data,
2281 while info tables live in .text, causing GAS to emit REL32/DISP32
2282 relocations with non-zero values. Adding the displacement is
2283 the right thing to do.
2285 *pP = S - ((UInt32)pP) - 4 + A;
2288 debugBelch("%s: unhandled PEi386 relocation type %d",
2289 oc->fileName, reltab_j->Type);
2296 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
2300 #endif /* defined(OBJFORMAT_PEi386) */
2303 /* --------------------------------------------------------------------------
2305 * ------------------------------------------------------------------------*/
2307 #if defined(OBJFORMAT_ELF)
2312 #if defined(sparc_HOST_ARCH)
2313 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2314 #elif defined(i386_HOST_ARCH)
2315 # define ELF_TARGET_386 /* Used inside <elf.h> */
2316 #elif defined(x86_64_HOST_ARCH)
2317 # define ELF_TARGET_X64_64
2319 #elif defined (ia64_HOST_ARCH)
2320 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2322 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2323 # define ELF_NEED_GOT /* needs Global Offset Table */
2324 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2327 #if !defined(openbsd_HOST_OS)
2330 /* openbsd elf has things in different places, with diff names */
2331 #include <elf_abi.h>
2332 #include <machine/reloc.h>
2333 #define R_386_32 RELOC_32
2334 #define R_386_PC32 RELOC_PC32
2338 * Define a set of types which can be used for both ELF32 and ELF64
2342 #define ELFCLASS ELFCLASS64
2343 #define Elf_Addr Elf64_Addr
2344 #define Elf_Word Elf64_Word
2345 #define Elf_Sword Elf64_Sword
2346 #define Elf_Ehdr Elf64_Ehdr
2347 #define Elf_Phdr Elf64_Phdr
2348 #define Elf_Shdr Elf64_Shdr
2349 #define Elf_Sym Elf64_Sym
2350 #define Elf_Rel Elf64_Rel
2351 #define Elf_Rela Elf64_Rela
2352 #define ELF_ST_TYPE ELF64_ST_TYPE
2353 #define ELF_ST_BIND ELF64_ST_BIND
2354 #define ELF_R_TYPE ELF64_R_TYPE
2355 #define ELF_R_SYM ELF64_R_SYM
2357 #define ELFCLASS ELFCLASS32
2358 #define Elf_Addr Elf32_Addr
2359 #define Elf_Word Elf32_Word
2360 #define Elf_Sword Elf32_Sword
2361 #define Elf_Ehdr Elf32_Ehdr
2362 #define Elf_Phdr Elf32_Phdr
2363 #define Elf_Shdr Elf32_Shdr
2364 #define Elf_Sym Elf32_Sym
2365 #define Elf_Rel Elf32_Rel
2366 #define Elf_Rela Elf32_Rela
2368 #define ELF_ST_TYPE ELF32_ST_TYPE
2371 #define ELF_ST_BIND ELF32_ST_BIND
2374 #define ELF_R_TYPE ELF32_R_TYPE
2377 #define ELF_R_SYM ELF32_R_SYM
2383 * Functions to allocate entries in dynamic sections. Currently we simply
2384 * preallocate a large number, and we don't check if a entry for the given
2385 * target already exists (a linear search is too slow). Ideally these
2386 * entries would be associated with symbols.
2389 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2390 #define GOT_SIZE 0x20000
2391 #define FUNCTION_TABLE_SIZE 0x10000
2392 #define PLT_SIZE 0x08000
2395 static Elf_Addr got[GOT_SIZE];
2396 static unsigned int gotIndex;
2397 static Elf_Addr gp_val = (Elf_Addr)got;
2400 allocateGOTEntry(Elf_Addr target)
2404 if (gotIndex >= GOT_SIZE)
2405 barf("Global offset table overflow");
2407 entry = &got[gotIndex++];
2409 return (Elf_Addr)entry;
2413 #ifdef ELF_FUNCTION_DESC
2419 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2420 static unsigned int functionTableIndex;
2423 allocateFunctionDesc(Elf_Addr target)
2425 FunctionDesc *entry;
2427 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2428 barf("Function table overflow");
2430 entry = &functionTable[functionTableIndex++];
2432 entry->gp = (Elf_Addr)gp_val;
2433 return (Elf_Addr)entry;
2437 copyFunctionDesc(Elf_Addr target)
2439 FunctionDesc *olddesc = (FunctionDesc *)target;
2440 FunctionDesc *newdesc;
2442 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2443 newdesc->gp = olddesc->gp;
2444 return (Elf_Addr)newdesc;
2449 #ifdef ia64_HOST_ARCH
2450 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2451 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2453 static unsigned char plt_code[] =
2455 /* taken from binutils bfd/elfxx-ia64.c */
2456 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2457 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2458 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2459 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2460 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2461 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2464 /* If we can't get to the function descriptor via gp, take a local copy of it */
2465 #define PLT_RELOC(code, target) { \
2466 Elf64_Sxword rel_value = target - gp_val; \
2467 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2468 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2470 ia64_reloc_gprel22((Elf_Addr)code, target); \
2475 unsigned char code[sizeof(plt_code)];
2479 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2481 PLTEntry *plt = (PLTEntry *)oc->plt;
2484 if (oc->pltIndex >= PLT_SIZE)
2485 barf("Procedure table overflow");
2487 entry = &plt[oc->pltIndex++];
2488 memcpy(entry->code, plt_code, sizeof(entry->code));
2489 PLT_RELOC(entry->code, target);
2490 return (Elf_Addr)entry;
2496 return (PLT_SIZE * sizeof(PLTEntry));
2501 #if x86_64_HOST_ARCH
2502 // On x86_64, 32-bit relocations are often used, which requires that
2503 // we can resolve a symbol to a 32-bit offset. However, shared
2504 // libraries are placed outside the 2Gb area, which leaves us with a
2505 // problem when we need to give a 32-bit offset to a symbol in a
2508 // For a function symbol, we can allocate a bounce sequence inside the
2509 // 2Gb area and resolve the symbol to this. The bounce sequence is
2510 // simply a long jump instruction to the real location of the symbol.
2512 // For data references, we're screwed.
2515 unsigned char jmp[8]; /* 6 byte instruction: jmpq *0x00000002(%rip) */
2519 #define X86_64_BB_SIZE 1024
2521 static x86_64_bounce *x86_64_bounce_buffer = NULL;
2522 static nat x86_64_bb_next_off;
2525 x86_64_high_symbol( char *lbl, void *addr )
2527 x86_64_bounce *bounce;
2529 if ( x86_64_bounce_buffer == NULL ||
2530 x86_64_bb_next_off >= X86_64_BB_SIZE ) {
2531 x86_64_bounce_buffer =
2532 mmap(NULL, X86_64_BB_SIZE * sizeof(x86_64_bounce),
2533 PROT_EXEC|PROT_READ|PROT_WRITE,
2534 MAP_PRIVATE|MAP_32BIT|MAP_ANONYMOUS, -1, 0);
2535 if (x86_64_bounce_buffer == MAP_FAILED) {
2536 barf("x86_64_high_symbol: mmap failed");
2538 x86_64_bb_next_off = 0;
2540 bounce = &x86_64_bounce_buffer[x86_64_bb_next_off];
2541 bounce->jmp[0] = 0xff;
2542 bounce->jmp[1] = 0x25;
2543 bounce->jmp[2] = 0x02;
2544 bounce->jmp[3] = 0x00;
2545 bounce->jmp[4] = 0x00;
2546 bounce->jmp[5] = 0x00;
2547 bounce->addr = addr;
2548 x86_64_bb_next_off++;
2550 IF_DEBUG(linker, debugBelch("x86_64: allocated bounce entry for %s->%p at %p\n",
2551 lbl, addr, bounce));
2553 insertStrHashTable(symhash, lbl, bounce);
2560 * Generic ELF functions
2564 findElfSection ( void* objImage, Elf_Word sh_type )
2566 char* ehdrC = (char*)objImage;
2567 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2568 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2569 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2573 for (i = 0; i < ehdr->e_shnum; i++) {
2574 if (shdr[i].sh_type == sh_type
2575 /* Ignore the section header's string table. */
2576 && i != ehdr->e_shstrndx
2577 /* Ignore string tables named .stabstr, as they contain
2579 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2581 ptr = ehdrC + shdr[i].sh_offset;
2588 #if defined(ia64_HOST_ARCH)
2590 findElfSegment ( void* objImage, Elf_Addr vaddr )
2592 char* ehdrC = (char*)objImage;
2593 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2594 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2595 Elf_Addr segaddr = 0;
2598 for (i = 0; i < ehdr->e_phnum; i++) {
2599 segaddr = phdr[i].p_vaddr;
2600 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2608 ocVerifyImage_ELF ( ObjectCode* oc )
2612 int i, j, nent, nstrtab, nsymtabs;
2616 char* ehdrC = (char*)(oc->image);
2617 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2619 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2620 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2621 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2622 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2623 errorBelch("%s: not an ELF object", oc->fileName);
2627 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2628 errorBelch("%s: unsupported ELF format", oc->fileName);
2632 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2633 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
2635 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2636 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
2638 errorBelch("%s: unknown endiannness", oc->fileName);
2642 if (ehdr->e_type != ET_REL) {
2643 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
2646 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
2648 IF_DEBUG(linker,debugBelch( "Architecture is " ));
2649 switch (ehdr->e_machine) {
2650 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
2651 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
2653 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
2655 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
2657 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
2659 default: IF_DEBUG(linker,debugBelch( "unknown" ));
2660 errorBelch("%s: unknown architecture", oc->fileName);
2664 IF_DEBUG(linker,debugBelch(
2665 "\nSection header table: start %d, n_entries %d, ent_size %d\n",
2666 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2668 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2670 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2672 if (ehdr->e_shstrndx == SHN_UNDEF) {
2673 errorBelch("%s: no section header string table", oc->fileName);
2676 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
2678 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2681 for (i = 0; i < ehdr->e_shnum; i++) {
2682 IF_DEBUG(linker,debugBelch("%2d: ", i ));
2683 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
2684 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
2685 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
2686 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
2687 ehdrC + shdr[i].sh_offset,
2688 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2690 if (shdr[i].sh_type == SHT_REL) {
2691 IF_DEBUG(linker,debugBelch("Rel " ));
2692 } else if (shdr[i].sh_type == SHT_RELA) {
2693 IF_DEBUG(linker,debugBelch("RelA " ));
2695 IF_DEBUG(linker,debugBelch(" "));
2698 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
2702 IF_DEBUG(linker,debugBelch( "\nString tables" ));
2705 for (i = 0; i < ehdr->e_shnum; i++) {
2706 if (shdr[i].sh_type == SHT_STRTAB
2707 /* Ignore the section header's string table. */
2708 && i != ehdr->e_shstrndx
2709 /* Ignore string tables named .stabstr, as they contain
2711 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2713 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
2714 strtab = ehdrC + shdr[i].sh_offset;
2719 errorBelch("%s: no string tables, or too many", oc->fileName);
2724 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
2725 for (i = 0; i < ehdr->e_shnum; i++) {
2726 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2727 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
2729 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2730 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2731 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%d rem)\n",
2733 shdr[i].sh_size % sizeof(Elf_Sym)
2735 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2736 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
2739 for (j = 0; j < nent; j++) {
2740 IF_DEBUG(linker,debugBelch(" %2d ", j ));
2741 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
2742 (int)stab[j].st_shndx,
2743 (int)stab[j].st_size,
2744 (char*)stab[j].st_value ));
2746 IF_DEBUG(linker,debugBelch("type=" ));
2747 switch (ELF_ST_TYPE(stab[j].st_info)) {
2748 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
2749 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
2750 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
2751 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
2752 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
2753 default: IF_DEBUG(linker,debugBelch("? " )); break;
2755 IF_DEBUG(linker,debugBelch(" " ));
2757 IF_DEBUG(linker,debugBelch("bind=" ));
2758 switch (ELF_ST_BIND(stab[j].st_info)) {
2759 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
2760 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
2761 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
2762 default: IF_DEBUG(linker,debugBelch("? " )); break;
2764 IF_DEBUG(linker,debugBelch(" " ));
2766 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
2770 if (nsymtabs == 0) {
2771 errorBelch("%s: didn't find any symbol tables", oc->fileName);
2778 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
2782 if (hdr->sh_type == SHT_PROGBITS
2783 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
2784 /* .text-style section */
2785 return SECTIONKIND_CODE_OR_RODATA;
2788 if (hdr->sh_type == SHT_PROGBITS
2789 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
2790 /* .data-style section */
2791 return SECTIONKIND_RWDATA;
2794 if (hdr->sh_type == SHT_PROGBITS
2795 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
2796 /* .rodata-style section */
2797 return SECTIONKIND_CODE_OR_RODATA;
2800 if (hdr->sh_type == SHT_NOBITS
2801 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
2802 /* .bss-style section */
2804 return SECTIONKIND_RWDATA;
2807 return SECTIONKIND_OTHER;
2812 ocGetNames_ELF ( ObjectCode* oc )
2817 char* ehdrC = (char*)(oc->image);
2818 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2819 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2820 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2822 ASSERT(symhash != NULL);
2825 errorBelch("%s: no strtab", oc->fileName);
2830 for (i = 0; i < ehdr->e_shnum; i++) {
2831 /* Figure out what kind of section it is. Logic derived from
2832 Figure 1.14 ("Special Sections") of the ELF document
2833 ("Portable Formats Specification, Version 1.1"). */
2835 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
2837 if (is_bss && shdr[i].sh_size > 0) {
2838 /* This is a non-empty .bss section. Allocate zeroed space for
2839 it, and set its .sh_offset field such that
2840 ehdrC + .sh_offset == addr_of_zeroed_space. */
2841 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2842 "ocGetNames_ELF(BSS)");
2843 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2845 debugBelch("BSS section at 0x%x, size %d\n",
2846 zspace, shdr[i].sh_size);
2850 /* fill in the section info */
2851 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2852 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2853 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2854 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2857 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2859 /* copy stuff into this module's object symbol table */
2860 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2861 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2863 oc->n_symbols = nent;
2864 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2865 "ocGetNames_ELF(oc->symbols)");
2867 for (j = 0; j < nent; j++) {
2869 char isLocal = FALSE; /* avoids uninit-var warning */
2871 char* nm = strtab + stab[j].st_name;
2872 int secno = stab[j].st_shndx;
2874 /* Figure out if we want to add it; if so, set ad to its
2875 address. Otherwise leave ad == NULL. */
2877 if (secno == SHN_COMMON) {
2879 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2881 debugBelch("COMMON symbol, size %d name %s\n",
2882 stab[j].st_size, nm);
2884 /* Pointless to do addProddableBlock() for this area,
2885 since the linker should never poke around in it. */
2888 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2889 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2891 /* and not an undefined symbol */
2892 && stab[j].st_shndx != SHN_UNDEF
2893 /* and not in a "special section" */
2894 && stab[j].st_shndx < SHN_LORESERVE
2896 /* and it's a not a section or string table or anything silly */
2897 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2898 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2899 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2902 /* Section 0 is the undefined section, hence > and not >=. */
2903 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2905 if (shdr[secno].sh_type == SHT_NOBITS) {
2906 debugBelch(" BSS symbol, size %d off %d name %s\n",
2907 stab[j].st_size, stab[j].st_value, nm);
2910 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2911 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2914 #ifdef ELF_FUNCTION_DESC
2915 /* dlsym() and the initialisation table both give us function
2916 * descriptors, so to be consistent we store function descriptors
2917 * in the symbol table */
2918 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2919 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2921 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s",
2922 ad, oc->fileName, nm ));
2927 /* And the decision is ... */
2931 oc->symbols[j] = nm;
2934 /* Ignore entirely. */
2936 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2940 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
2941 strtab + stab[j].st_name ));
2944 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2945 (int)ELF_ST_BIND(stab[j].st_info),
2946 (int)ELF_ST_TYPE(stab[j].st_info),
2947 (int)stab[j].st_shndx,
2948 strtab + stab[j].st_name
2951 oc->symbols[j] = NULL;
2960 /* Do ELF relocations which lack an explicit addend. All x86-linux
2961 relocations appear to be of this form. */
2963 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2964 Elf_Shdr* shdr, int shnum,
2965 Elf_Sym* stab, char* strtab )
2970 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2971 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2972 int target_shndx = shdr[shnum].sh_info;
2973 int symtab_shndx = shdr[shnum].sh_link;
2975 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2976 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2977 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
2978 target_shndx, symtab_shndx ));
2980 /* Skip sections that we're not interested in. */
2983 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
2984 if (kind == SECTIONKIND_OTHER) {
2985 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
2990 for (j = 0; j < nent; j++) {
2991 Elf_Addr offset = rtab[j].r_offset;
2992 Elf_Addr info = rtab[j].r_info;
2994 Elf_Addr P = ((Elf_Addr)targ) + offset;
2995 Elf_Word* pP = (Elf_Word*)P;
3001 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
3002 j, (void*)offset, (void*)info ));
3004 IF_DEBUG(linker,debugBelch( " ZERO" ));
3007 Elf_Sym sym = stab[ELF_R_SYM(info)];
3008 /* First see if it is a local symbol. */
3009 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3010 /* Yes, so we can get the address directly from the ELF symbol
3012 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3014 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3015 + stab[ELF_R_SYM(info)].st_value);
3018 /* No, so look up the name in our global table. */
3019 symbol = strtab + sym.st_name;
3020 S_tmp = lookupSymbol( symbol );
3021 S = (Elf_Addr)S_tmp;
3024 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3027 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
3030 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
3031 (void*)P, (void*)S, (void*)A ));
3032 checkProddableBlock ( oc, pP );
3036 switch (ELF_R_TYPE(info)) {
3037 # ifdef i386_HOST_ARCH
3038 case R_386_32: *pP = value; break;
3039 case R_386_PC32: *pP = value - P; break;
3042 errorBelch("%s: unhandled ELF relocation(Rel) type %d\n",
3043 oc->fileName, ELF_R_TYPE(info));
3051 /* Do ELF relocations for which explicit addends are supplied.
3052 sparc-solaris relocations appear to be of this form. */
3054 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
3055 Elf_Shdr* shdr, int shnum,
3056 Elf_Sym* stab, char* strtab )
3061 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
3062 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
3063 int target_shndx = shdr[shnum].sh_info;
3064 int symtab_shndx = shdr[shnum].sh_link;
3066 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3067 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
3068 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3069 target_shndx, symtab_shndx ));
3071 for (j = 0; j < nent; j++) {
3072 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
3073 /* This #ifdef only serves to avoid unused-var warnings. */
3074 Elf_Addr offset = rtab[j].r_offset;
3075 Elf_Addr P = targ + offset;
3077 Elf_Addr info = rtab[j].r_info;
3078 Elf_Addr A = rtab[j].r_addend;
3082 # if defined(sparc_HOST_ARCH)
3083 Elf_Word* pP = (Elf_Word*)P;
3085 # elif defined(ia64_HOST_ARCH)
3086 Elf64_Xword *pP = (Elf64_Xword *)P;
3088 # elif defined(powerpc_HOST_ARCH)
3092 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
3093 j, (void*)offset, (void*)info,
3096 IF_DEBUG(linker,debugBelch( " ZERO" ));
3099 Elf_Sym sym = stab[ELF_R_SYM(info)];
3100 /* First see if it is a local symbol. */
3101 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3102 /* Yes, so we can get the address directly from the ELF symbol
3104 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3106 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3107 + stab[ELF_R_SYM(info)].st_value);
3108 #ifdef ELF_FUNCTION_DESC
3109 /* Make a function descriptor for this function */
3110 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
3111 S = allocateFunctionDesc(S + A);
3116 /* No, so look up the name in our global table. */
3117 symbol = strtab + sym.st_name;
3118 S_tmp = lookupSymbol( symbol );
3119 S = (Elf_Addr)S_tmp;
3121 #ifdef ELF_FUNCTION_DESC
3122 /* If a function, already a function descriptor - we would
3123 have to copy it to add an offset. */
3124 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
3125 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
3129 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3132 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
3135 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
3136 (void*)P, (void*)S, (void*)A ));
3137 /* checkProddableBlock ( oc, (void*)P ); */
3141 switch (ELF_R_TYPE(info)) {
3142 # if defined(sparc_HOST_ARCH)
3143 case R_SPARC_WDISP30:
3144 w1 = *pP & 0xC0000000;
3145 w2 = (Elf_Word)((value - P) >> 2);
3146 ASSERT((w2 & 0xC0000000) == 0);
3151 w1 = *pP & 0xFFC00000;
3152 w2 = (Elf_Word)(value >> 10);
3153 ASSERT((w2 & 0xFFC00000) == 0);
3159 w2 = (Elf_Word)(value & 0x3FF);
3160 ASSERT((w2 & ~0x3FF) == 0);
3164 /* According to the Sun documentation:
3166 This relocation type resembles R_SPARC_32, except it refers to an
3167 unaligned word. That is, the word to be relocated must be treated
3168 as four separate bytes with arbitrary alignment, not as a word
3169 aligned according to the architecture requirements.
3171 (JRS: which means that freeloading on the R_SPARC_32 case
3172 is probably wrong, but hey ...)
3176 w2 = (Elf_Word)value;
3179 # elif defined(ia64_HOST_ARCH)
3180 case R_IA64_DIR64LSB:
3181 case R_IA64_FPTR64LSB:
3184 case R_IA64_PCREL64LSB:
3187 case R_IA64_SEGREL64LSB:
3188 addr = findElfSegment(ehdrC, value);
3191 case R_IA64_GPREL22:
3192 ia64_reloc_gprel22(P, value);
3194 case R_IA64_LTOFF22:
3195 case R_IA64_LTOFF22X:
3196 case R_IA64_LTOFF_FPTR22:
3197 addr = allocateGOTEntry(value);
3198 ia64_reloc_gprel22(P, addr);
3200 case R_IA64_PCREL21B:
3201 ia64_reloc_pcrel21(P, S, oc);
3204 /* This goes with R_IA64_LTOFF22X and points to the load to
3205 * convert into a move. We don't implement relaxation. */
3207 # elif defined(powerpc_HOST_ARCH)
3208 case R_PPC_ADDR16_LO:
3209 *(Elf32_Half*) P = value;
3212 case R_PPC_ADDR16_HI:
3213 *(Elf32_Half*) P = value >> 16;
3216 case R_PPC_ADDR16_HA:
3217 *(Elf32_Half*) P = (value + 0x8000) >> 16;
3221 *(Elf32_Word *) P = value;
3225 *(Elf32_Word *) P = value - P;
3231 if( delta << 6 >> 6 != delta )
3233 value = makeJumpIsland( oc, ELF_R_SYM(info), value );
3236 if( value == 0 || delta << 6 >> 6 != delta )
3238 barf( "Unable to make ppcJumpIsland for #%d",
3244 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
3245 | (delta & 0x3fffffc);
3249 #if x86_64_HOST_ARCH
3251 *(Elf64_Xword *)P = value;
3256 StgInt64 off = value - P;
3257 if (off >= 0x7fffffffL || off < -0x80000000L) {
3258 barf("R_X86_64_PC32 relocation out of range: %s = %p",
3261 *(Elf64_Word *)P = (Elf64_Word)off;
3266 if (value >= 0x7fffffffL) {
3267 barf("R_X86_64_32 relocation out of range: %s = %p\n",
3270 *(Elf64_Word *)P = (Elf64_Word)value;
3274 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
3275 barf("R_X86_64_32S relocation out of range: %s = %p\n",
3278 *(Elf64_Sword *)P = (Elf64_Sword)value;
3283 errorBelch("%s: unhandled ELF relocation(RelA) type %d\n",
3284 oc->fileName, ELF_R_TYPE(info));
3293 ocResolve_ELF ( ObjectCode* oc )
3297 Elf_Sym* stab = NULL;
3298 char* ehdrC = (char*)(oc->image);
3299 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
3300 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3302 /* first find "the" symbol table */
3303 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
3305 /* also go find the string table */
3306 strtab = findElfSection ( ehdrC, SHT_STRTAB );
3308 if (stab == NULL || strtab == NULL) {
3309 errorBelch("%s: can't find string or symbol table", oc->fileName);
3313 /* Process the relocation sections. */
3314 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
3315 if (shdr[shnum].sh_type == SHT_REL) {
3316 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
3317 shnum, stab, strtab );
3321 if (shdr[shnum].sh_type == SHT_RELA) {
3322 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
3323 shnum, stab, strtab );
3328 /* Free the local symbol table; we won't need it again. */
3329 freeHashTable(oc->lochash, NULL);
3332 #if defined(powerpc_HOST_ARCH)
3333 ocFlushInstructionCache( oc );
3341 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
3342 * at the front. The following utility functions pack and unpack instructions, and
3343 * take care of the most common relocations.
3346 #ifdef ia64_HOST_ARCH
3349 ia64_extract_instruction(Elf64_Xword *target)
3352 int slot = (Elf_Addr)target & 3;
3353 target = (Elf_Addr)target & ~3;
3361 return ((w1 >> 5) & 0x1ffffffffff);
3363 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
3367 barf("ia64_extract_instruction: invalid slot %p", target);
3372 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
3374 int slot = (Elf_Addr)target & 3;
3375 target = (Elf_Addr)target & ~3;
3380 *target |= value << 5;
3383 *target |= value << 46;
3384 *(target+1) |= value >> 18;
3387 *(target+1) |= value << 23;
3393 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
3395 Elf64_Xword instruction;
3396 Elf64_Sxword rel_value;
3398 rel_value = value - gp_val;
3399 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
3400 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
3402 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3403 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
3404 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
3405 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
3406 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3407 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3411 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
3413 Elf64_Xword instruction;
3414 Elf64_Sxword rel_value;
3417 entry = allocatePLTEntry(value, oc);
3419 rel_value = (entry >> 4) - (target >> 4);
3420 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
3421 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
3423 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3424 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
3425 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3426 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3432 * PowerPC ELF specifics
3435 #ifdef powerpc_HOST_ARCH
3437 static int ocAllocateJumpIslands_ELF( ObjectCode *oc )
3443 ehdr = (Elf_Ehdr *) oc->image;
3444 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
3446 for( i = 0; i < ehdr->e_shnum; i++ )
3447 if( shdr[i].sh_type == SHT_SYMTAB )
3450 if( i == ehdr->e_shnum )
3452 errorBelch( "This ELF file contains no symtab" );
3456 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
3458 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
3459 shdr[i].sh_entsize, sizeof( Elf_Sym ) );
3464 return ocAllocateJumpIslands( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
3467 #endif /* powerpc */
3471 /* --------------------------------------------------------------------------
3473 * ------------------------------------------------------------------------*/
3475 #if defined(OBJFORMAT_MACHO)
3478 Support for MachO linking on Darwin/MacOS X
3479 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3481 I hereby formally apologize for the hackish nature of this code.
3482 Things that need to be done:
3483 *) implement ocVerifyImage_MachO
3484 *) add still more sanity checks.
3487 #ifdef powerpc_HOST_ARCH
3488 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3490 struct mach_header *header = (struct mach_header *) oc->image;
3491 struct load_command *lc = (struct load_command *) (header + 1);
3494 for( i = 0; i < header->ncmds; i++ )
3496 if( lc->cmd == LC_SYMTAB )
3498 // Find out the first and last undefined external
3499 // symbol, so we don't have to allocate too many
3501 struct symtab_command *symLC = (struct symtab_command *) lc;
3502 unsigned min = symLC->nsyms, max = 0;
3503 struct nlist *nlist =
3504 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
3506 for(i=0;i<symLC->nsyms;i++)
3508 if(nlist[i].n_type & N_STAB)
3510 else if(nlist[i].n_type & N_EXT)
3512 if((nlist[i].n_type & N_TYPE) == N_UNDF
3513 && (nlist[i].n_value == 0))
3523 return ocAllocateJumpIslands(oc, max - min + 1, min);
3528 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3530 return ocAllocateJumpIslands(oc,0,0);
3534 static int ocVerifyImage_MachO(ObjectCode* oc STG_UNUSED)
3536 // FIXME: do some verifying here
3540 static int resolveImports(
3543 struct symtab_command *symLC,
3544 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3545 unsigned long *indirectSyms,
3546 struct nlist *nlist)
3550 for(i=0;i*4<sect->size;i++)
3552 // according to otool, reserved1 contains the first index into the indirect symbol table
3553 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3554 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3557 if((symbol->n_type & N_TYPE) == N_UNDF
3558 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3559 addr = (void*) (symbol->n_value);
3560 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3563 addr = lookupSymbol(nm);
3566 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3570 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3571 ((void**)(image + sect->offset))[i] = addr;
3577 static unsigned long relocateAddress(
3580 struct section* sections,
3581 unsigned long address)
3584 for(i = 0; i < nSections; i++)
3586 if(sections[i].addr <= address
3587 && address < sections[i].addr + sections[i].size)
3589 return (unsigned long)oc->image
3590 + sections[i].offset + address - sections[i].addr;
3593 barf("Invalid Mach-O file:"
3594 "Address out of bounds while relocating object file");
3598 static int relocateSection(
3601 struct symtab_command *symLC, struct nlist *nlist,
3602 int nSections, struct section* sections, struct section *sect)
3604 struct relocation_info *relocs;
3607 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3609 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3611 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
3613 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
3617 relocs = (struct relocation_info*) (image + sect->reloff);
3621 if(relocs[i].r_address & R_SCATTERED)
3623 struct scattered_relocation_info *scat =
3624 (struct scattered_relocation_info*) &relocs[i];
3628 if(scat->r_length == 2)
3630 unsigned long word = 0;
3631 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3632 checkProddableBlock(oc,wordPtr);
3634 // Note on relocation types:
3635 // i386 uses the GENERIC_RELOC_* types,
3636 // while ppc uses special PPC_RELOC_* types.
3637 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
3638 // in both cases, all others are different.
3639 // Therefore, we use GENERIC_RELOC_VANILLA
3640 // and GENERIC_RELOC_PAIR instead of the PPC variants,
3641 // and use #ifdefs for the other types.
3643 // Step 1: Figure out what the relocated value should be
3644 if(scat->r_type == GENERIC_RELOC_VANILLA)
3646 word = *wordPtr + (unsigned long) relocateAddress(
3653 #ifdef powerpc_HOST_ARCH
3654 else if(scat->r_type == PPC_RELOC_SECTDIFF
3655 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3656 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3657 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3659 else if(scat->r_type == GENERIC_RELOC_SECTDIFF)
3662 struct scattered_relocation_info *pair =
3663 (struct scattered_relocation_info*) &relocs[i+1];
3665 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
3666 barf("Invalid Mach-O file: "
3667 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
3669 word = (unsigned long)
3670 (relocateAddress(oc, nSections, sections, scat->r_value)
3671 - relocateAddress(oc, nSections, sections, pair->r_value));
3674 #ifdef powerpc_HOST_ARCH
3675 else if(scat->r_type == PPC_RELOC_HI16
3676 || scat->r_type == PPC_RELOC_LO16
3677 || scat->r_type == PPC_RELOC_HA16
3678 || scat->r_type == PPC_RELOC_LO14)
3679 { // these are generated by label+offset things
3680 struct relocation_info *pair = &relocs[i+1];
3681 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
3682 barf("Invalid Mach-O file: "
3683 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
3685 if(scat->r_type == PPC_RELOC_LO16)
3687 word = ((unsigned short*) wordPtr)[1];
3688 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3690 else if(scat->r_type == PPC_RELOC_LO14)
3692 barf("Unsupported Relocation: PPC_RELOC_LO14");
3693 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
3694 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3696 else if(scat->r_type == PPC_RELOC_HI16)
3698 word = ((unsigned short*) wordPtr)[1] << 16;
3699 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3701 else if(scat->r_type == PPC_RELOC_HA16)
3703 word = ((unsigned short*) wordPtr)[1] << 16;
3704 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3708 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
3715 continue; // ignore the others
3717 #ifdef powerpc_HOST_ARCH
3718 if(scat->r_type == GENERIC_RELOC_VANILLA
3719 || scat->r_type == PPC_RELOC_SECTDIFF)
3721 if(scat->r_type == GENERIC_RELOC_VANILLA
3722 || scat->r_type == GENERIC_RELOC_SECTDIFF)
3727 #ifdef powerpc_HOST_ARCH
3728 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
3730 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3732 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
3734 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3736 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
3738 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3739 + ((word & (1<<15)) ? 1 : 0);
3745 continue; // FIXME: I hope it's OK to ignore all the others.
3749 struct relocation_info *reloc = &relocs[i];
3750 if(reloc->r_pcrel && !reloc->r_extern)
3753 if(reloc->r_length == 2)
3755 unsigned long word = 0;
3756 #ifdef powerpc_HOST_ARCH
3757 unsigned long jumpIsland = 0;
3758 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
3759 // to avoid warning and to catch
3763 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3764 checkProddableBlock(oc,wordPtr);
3766 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3770 #ifdef powerpc_HOST_ARCH
3771 else if(reloc->r_type == PPC_RELOC_LO16)
3773 word = ((unsigned short*) wordPtr)[1];
3774 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3776 else if(reloc->r_type == PPC_RELOC_HI16)
3778 word = ((unsigned short*) wordPtr)[1] << 16;
3779 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3781 else if(reloc->r_type == PPC_RELOC_HA16)
3783 word = ((unsigned short*) wordPtr)[1] << 16;
3784 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3786 else if(reloc->r_type == PPC_RELOC_BR24)
3789 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
3793 if(!reloc->r_extern)
3796 sections[reloc->r_symbolnum-1].offset
3797 - sections[reloc->r_symbolnum-1].addr
3804 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3805 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3806 void *symbolAddress = lookupSymbol(nm);
3809 errorBelch("\nunknown symbol `%s'", nm);
3815 #ifdef powerpc_HOST_ARCH
3816 // In the .o file, this should be a relative jump to NULL
3817 // and we'll change it to a relative jump to the symbol
3818 ASSERT(-word == reloc->r_address);
3819 jumpIsland = makeJumpIsland(oc,reloc->r_symbolnum,(unsigned long) symbolAddress);
3822 offsetToJumpIsland = word + jumpIsland
3823 - (((long)image) + sect->offset - sect->addr);
3826 word += (unsigned long) symbolAddress
3827 - (((long)image) + sect->offset - sect->addr);
3831 word += (unsigned long) symbolAddress;
3835 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3840 #ifdef powerpc_HOST_ARCH
3841 else if(reloc->r_type == PPC_RELOC_LO16)
3843 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3846 else if(reloc->r_type == PPC_RELOC_HI16)
3848 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3851 else if(reloc->r_type == PPC_RELOC_HA16)
3853 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3854 + ((word & (1<<15)) ? 1 : 0);
3857 else if(reloc->r_type == PPC_RELOC_BR24)
3859 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3861 // The branch offset is too large.
3862 // Therefore, we try to use a jump island.
3865 barf("unconditional relative branch out of range: "
3866 "no jump island available");
3869 word = offsetToJumpIsland;
3870 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3871 barf("unconditional relative branch out of range: "
3872 "jump island out of range");
3874 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3879 barf("\nunknown relocation %d",reloc->r_type);
3886 static int ocGetNames_MachO(ObjectCode* oc)
3888 char *image = (char*) oc->image;
3889 struct mach_header *header = (struct mach_header*) image;
3890 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3891 unsigned i,curSymbol = 0;
3892 struct segment_command *segLC = NULL;
3893 struct section *sections;
3894 struct symtab_command *symLC = NULL;
3895 struct nlist *nlist;
3896 unsigned long commonSize = 0;
3897 char *commonStorage = NULL;
3898 unsigned long commonCounter;
3900 for(i=0;i<header->ncmds;i++)
3902 if(lc->cmd == LC_SEGMENT)
3903 segLC = (struct segment_command*) lc;
3904 else if(lc->cmd == LC_SYMTAB)
3905 symLC = (struct symtab_command*) lc;
3906 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3909 sections = (struct section*) (segLC+1);
3910 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
3913 for(i=0;i<segLC->nsects;i++)
3915 if(sections[i].size == 0)
3918 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
3920 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
3921 "ocGetNames_MachO(common symbols)");
3922 sections[i].offset = zeroFillArea - image;
3925 if(!strcmp(sections[i].sectname,"__text"))
3926 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3927 (void*) (image + sections[i].offset),
3928 (void*) (image + sections[i].offset + sections[i].size));
3929 else if(!strcmp(sections[i].sectname,"__const"))
3930 addSection(oc, SECTIONKIND_RWDATA,
3931 (void*) (image + sections[i].offset),
3932 (void*) (image + sections[i].offset + sections[i].size));
3933 else if(!strcmp(sections[i].sectname,"__data"))
3934 addSection(oc, SECTIONKIND_RWDATA,
3935 (void*) (image + sections[i].offset),
3936 (void*) (image + sections[i].offset + sections[i].size));
3937 else if(!strcmp(sections[i].sectname,"__bss")
3938 || !strcmp(sections[i].sectname,"__common"))
3939 addSection(oc, SECTIONKIND_RWDATA,
3940 (void*) (image + sections[i].offset),
3941 (void*) (image + sections[i].offset + sections[i].size));
3943 addProddableBlock(oc, (void*) (image + sections[i].offset),
3947 // count external symbols defined here
3951 for(i=0;i<symLC->nsyms;i++)
3953 if(nlist[i].n_type & N_STAB)
3955 else if(nlist[i].n_type & N_EXT)
3957 if((nlist[i].n_type & N_TYPE) == N_UNDF
3958 && (nlist[i].n_value != 0))
3960 commonSize += nlist[i].n_value;
3963 else if((nlist[i].n_type & N_TYPE) == N_SECT)
3968 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3969 "ocGetNames_MachO(oc->symbols)");
3973 for(i=0;i<symLC->nsyms;i++)
3975 if(nlist[i].n_type & N_STAB)
3977 else if((nlist[i].n_type & N_TYPE) == N_SECT)
3979 if(nlist[i].n_type & N_EXT)
3981 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3982 ghciInsertStrHashTable(oc->fileName, symhash, nm,
3984 + sections[nlist[i].n_sect-1].offset
3985 - sections[nlist[i].n_sect-1].addr
3986 + nlist[i].n_value);
3987 oc->symbols[curSymbol++] = nm;
3991 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3992 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm,
3994 + sections[nlist[i].n_sect-1].offset
3995 - sections[nlist[i].n_sect-1].addr
3996 + nlist[i].n_value);
4002 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
4003 commonCounter = (unsigned long)commonStorage;
4006 for(i=0;i<symLC->nsyms;i++)
4008 if((nlist[i].n_type & N_TYPE) == N_UNDF
4009 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
4011 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4012 unsigned long sz = nlist[i].n_value;
4014 nlist[i].n_value = commonCounter;
4016 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4017 (void*)commonCounter);
4018 oc->symbols[curSymbol++] = nm;
4020 commonCounter += sz;
4027 static int ocResolve_MachO(ObjectCode* oc)
4029 char *image = (char*) oc->image;
4030 struct mach_header *header = (struct mach_header*) image;
4031 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
4033 struct segment_command *segLC = NULL;
4034 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
4035 struct symtab_command *symLC = NULL;
4036 struct dysymtab_command *dsymLC = NULL;
4037 struct nlist *nlist;
4039 for(i=0;i<header->ncmds;i++)
4041 if(lc->cmd == LC_SEGMENT)
4042 segLC = (struct segment_command*) lc;
4043 else if(lc->cmd == LC_SYMTAB)
4044 symLC = (struct symtab_command*) lc;
4045 else if(lc->cmd == LC_DYSYMTAB)
4046 dsymLC = (struct dysymtab_command*) lc;
4047 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4050 sections = (struct section*) (segLC+1);
4051 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4054 for(i=0;i<segLC->nsects;i++)
4056 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
4057 la_ptrs = §ions[i];
4058 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
4059 nl_ptrs = §ions[i];
4060 else if(!strcmp(sections[i].sectname,"__la_sym_ptr2"))
4061 la_ptrs = §ions[i];
4062 else if(!strcmp(sections[i].sectname,"__la_sym_ptr3"))
4063 la_ptrs = §ions[i];
4068 unsigned long *indirectSyms
4069 = (unsigned long*) (image + dsymLC->indirectsymoff);
4072 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
4075 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
4079 for(i=0;i<segLC->nsects;i++)
4081 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
4085 /* Free the local symbol table; we won't need it again. */
4086 freeHashTable(oc->lochash, NULL);
4089 #if defined (powerpc_HOST_ARCH)
4090 ocFlushInstructionCache( oc );
4096 #ifdef powerpc_HOST_ARCH
4098 * The Mach-O object format uses leading underscores. But not everywhere.
4099 * There is a small number of runtime support functions defined in
4100 * libcc_dynamic.a whose name does not have a leading underscore.
4101 * As a consequence, we can't get their address from C code.
4102 * We have to use inline assembler just to take the address of a function.
4106 static void machoInitSymbolsWithoutUnderscore()
4108 extern void* symbolsWithoutUnderscore[];
4109 void **p = symbolsWithoutUnderscore;
4110 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
4114 __asm__ volatile(".long " # x);
4116 RTS_MACHO_NOUNDERLINE_SYMBOLS
4118 __asm__ volatile(".text");
4122 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
4124 RTS_MACHO_NOUNDERLINE_SYMBOLS
4131 * Figure out by how much to shift the entire Mach-O file in memory
4132 * when loading so that its single segment ends up 16-byte-aligned
4134 static int machoGetMisalignment( FILE * f )
4136 struct mach_header header;
4139 fread(&header, sizeof(header), 1, f);
4142 if(header.magic != MH_MAGIC)
4145 misalignment = (header.sizeofcmds + sizeof(header))
4148 return misalignment ? (16 - misalignment) : 0;