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"
31 #ifdef HAVE_SYS_TYPES_H
32 #include <sys/types.h>
38 #ifdef HAVE_SYS_STAT_H
42 #if defined(HAVE_DLFCN_H)
46 #if defined(cygwin32_HOST_OS)
51 #ifdef HAVE_SYS_TIME_H
55 #include <sys/fcntl.h>
56 #include <sys/termios.h>
57 #include <sys/utime.h>
58 #include <sys/utsname.h>
62 #if defined(ia64_HOST_ARCH) || defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS)
67 #if defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS)
75 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
76 # define OBJFORMAT_ELF
77 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
78 # define OBJFORMAT_PEi386
81 #elif defined(darwin_HOST_OS)
82 # define OBJFORMAT_MACHO
83 # include <mach-o/loader.h>
84 # include <mach-o/nlist.h>
85 # include <mach-o/reloc.h>
86 # include <mach-o/dyld.h>
87 #if defined(powerpc_HOST_ARCH)
88 # include <mach-o/ppc/reloc.h>
92 /* Hash table mapping symbol names to Symbol */
93 static /*Str*/HashTable *symhash;
95 /* List of currently loaded objects */
96 ObjectCode *objects = NULL; /* initially empty */
98 #if defined(OBJFORMAT_ELF)
99 static int ocVerifyImage_ELF ( ObjectCode* oc );
100 static int ocGetNames_ELF ( ObjectCode* oc );
101 static int ocResolve_ELF ( ObjectCode* oc );
102 #if defined(powerpc_HOST_ARCH)
103 static int ocAllocateJumpIslands_ELF ( ObjectCode* oc );
105 #elif defined(OBJFORMAT_PEi386)
106 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
107 static int ocGetNames_PEi386 ( ObjectCode* oc );
108 static int ocResolve_PEi386 ( ObjectCode* oc );
109 #elif defined(OBJFORMAT_MACHO)
110 static int ocVerifyImage_MachO ( ObjectCode* oc );
111 static int ocGetNames_MachO ( ObjectCode* oc );
112 static int ocResolve_MachO ( ObjectCode* oc );
114 static int machoGetMisalignment( FILE * );
115 #ifdef powerpc_HOST_ARCH
116 static int ocAllocateJumpIslands_MachO ( ObjectCode* oc );
117 static void machoInitSymbolsWithoutUnderscore( void );
121 #if defined(x86_64_HOST_ARCH)
122 static void*x86_64_high_symbol( char *lbl, void *addr );
125 /* -----------------------------------------------------------------------------
126 * Built-in symbols from the RTS
129 typedef struct _RtsSymbolVal {
136 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
137 SymX(makeStableNamezh_fast) \
138 SymX(finalizzeWeakzh_fast)
140 /* These are not available in GUM!!! -- HWL */
141 #define Maybe_Stable_Names
144 #if !defined (mingw32_HOST_OS)
145 #define RTS_POSIX_ONLY_SYMBOLS \
146 SymX(signal_handlers) \
147 SymX(stg_sig_install) \
151 #if defined (cygwin32_HOST_OS)
152 #define RTS_MINGW_ONLY_SYMBOLS /**/
153 /* Don't have the ability to read import libs / archives, so
154 * we have to stupidly list a lot of what libcygwin.a
157 #define RTS_CYGWIN_ONLY_SYMBOLS \
235 #elif !defined(mingw32_HOST_OS)
236 #define RTS_MINGW_ONLY_SYMBOLS /**/
237 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
238 #else /* defined(mingw32_HOST_OS) */
239 #define RTS_POSIX_ONLY_SYMBOLS /**/
240 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
242 /* Extra syms gen'ed by mingw-2's gcc-3.2: */
244 #define RTS_MINGW_EXTRA_SYMS \
245 Sym(_imp____mb_cur_max) \
248 #define RTS_MINGW_EXTRA_SYMS
251 /* These are statically linked from the mingw libraries into the ghc
252 executable, so we have to employ this hack. */
253 #define RTS_MINGW_ONLY_SYMBOLS \
254 SymX(asyncReadzh_fast) \
255 SymX(asyncWritezh_fast) \
256 SymX(asyncDoProczh_fast) \
268 SymX(getservbyname) \
269 SymX(getservbyport) \
270 SymX(getprotobynumber) \
271 SymX(getprotobyname) \
272 SymX(gethostbyname) \
273 SymX(gethostbyaddr) \
320 SymX(rts_InstallConsoleEvent) \
321 SymX(rts_ConsoleHandlerDone) \
323 Sym(_imp___timezone) \
332 RTS_MINGW_EXTRA_SYMS \
336 #if defined(darwin_TARGET_OS) && HAVE_PRINTF_LDBLSTUB
337 #define RTS_DARWIN_ONLY_SYMBOLS \
338 Sym(asprintf$LDBLStub) \
342 Sym(fprintf$LDBLStub) \
343 Sym(fscanf$LDBLStub) \
344 Sym(fwprintf$LDBLStub) \
345 Sym(fwscanf$LDBLStub) \
346 Sym(printf$LDBLStub) \
347 Sym(scanf$LDBLStub) \
348 Sym(snprintf$LDBLStub) \
349 Sym(sprintf$LDBLStub) \
350 Sym(sscanf$LDBLStub) \
351 Sym(strtold$LDBLStub) \
352 Sym(swprintf$LDBLStub) \
353 Sym(swscanf$LDBLStub) \
354 Sym(syslog$LDBLStub) \
355 Sym(vasprintf$LDBLStub) \
357 Sym(verrc$LDBLStub) \
358 Sym(verrx$LDBLStub) \
359 Sym(vfprintf$LDBLStub) \
360 Sym(vfscanf$LDBLStub) \
361 Sym(vfwprintf$LDBLStub) \
362 Sym(vfwscanf$LDBLStub) \
363 Sym(vprintf$LDBLStub) \
364 Sym(vscanf$LDBLStub) \
365 Sym(vsnprintf$LDBLStub) \
366 Sym(vsprintf$LDBLStub) \
367 Sym(vsscanf$LDBLStub) \
368 Sym(vswprintf$LDBLStub) \
369 Sym(vswscanf$LDBLStub) \
370 Sym(vsyslog$LDBLStub) \
371 Sym(vwarn$LDBLStub) \
372 Sym(vwarnc$LDBLStub) \
373 Sym(vwarnx$LDBLStub) \
374 Sym(vwprintf$LDBLStub) \
375 Sym(vwscanf$LDBLStub) \
377 Sym(warnc$LDBLStub) \
378 Sym(warnx$LDBLStub) \
379 Sym(wcstold$LDBLStub) \
380 Sym(wprintf$LDBLStub) \
383 #define RTS_DARWIN_ONLY_SYMBOLS
387 # define MAIN_CAP_SYM SymX(MainCapability)
389 # define MAIN_CAP_SYM
392 #if !defined(mingw32_HOST_OS)
393 #define RTS_USER_SIGNALS_SYMBOLS \
394 SymX(setIOManagerPipe)
396 #define RTS_USER_SIGNALS_SYMBOLS /* nothing */
399 #ifdef TABLES_NEXT_TO_CODE
400 #define RTS_RET_SYMBOLS /* nothing */
402 #define RTS_RET_SYMBOLS \
403 SymX(stg_enter_ret) \
404 SymX(stg_gc_fun_ret) \
412 SymX(stg_ap_pv_ret) \
413 SymX(stg_ap_pp_ret) \
414 SymX(stg_ap_ppv_ret) \
415 SymX(stg_ap_ppp_ret) \
416 SymX(stg_ap_pppv_ret) \
417 SymX(stg_ap_pppp_ret) \
418 SymX(stg_ap_ppppp_ret) \
419 SymX(stg_ap_pppppp_ret)
422 #define RTS_SYMBOLS \
425 SymX(stg_enter_info) \
426 SymX(stg_gc_void_info) \
427 SymX(__stg_gc_enter_1) \
428 SymX(stg_gc_noregs) \
429 SymX(stg_gc_unpt_r1_info) \
430 SymX(stg_gc_unpt_r1) \
431 SymX(stg_gc_unbx_r1_info) \
432 SymX(stg_gc_unbx_r1) \
433 SymX(stg_gc_f1_info) \
435 SymX(stg_gc_d1_info) \
437 SymX(stg_gc_l1_info) \
440 SymX(stg_gc_fun_info) \
442 SymX(stg_gc_gen_info) \
443 SymX(stg_gc_gen_hp) \
445 SymX(stg_gen_yield) \
446 SymX(stg_yield_noregs) \
447 SymX(stg_yield_to_interpreter) \
448 SymX(stg_gen_block) \
449 SymX(stg_block_noregs) \
451 SymX(stg_block_takemvar) \
452 SymX(stg_block_putmvar) \
453 SymX(stg_seq_frame_info) \
455 SymX(MallocFailHook) \
457 SymX(OutOfHeapHook) \
458 SymX(StackOverflowHook) \
459 SymX(__encodeDouble) \
460 SymX(__encodeFloat) \
464 SymX(__gmpz_cmp_si) \
465 SymX(__gmpz_cmp_ui) \
466 SymX(__gmpz_get_si) \
467 SymX(__gmpz_get_ui) \
468 SymX(__int_encodeDouble) \
469 SymX(__int_encodeFloat) \
470 SymX(andIntegerzh_fast) \
471 SymX(atomicallyzh_fast) \
475 SymX(blockAsyncExceptionszh_fast) \
477 SymX(catchRetryzh_fast) \
478 SymX(catchSTMzh_fast) \
479 SymX(closure_flags) \
481 SymX(cmpIntegerzh_fast) \
482 SymX(cmpIntegerIntzh_fast) \
483 SymX(complementIntegerzh_fast) \
484 SymX(createAdjustor) \
485 SymX(decodeDoublezh_fast) \
486 SymX(decodeFloatzh_fast) \
489 SymX(deRefWeakzh_fast) \
490 SymX(deRefStablePtrzh_fast) \
491 SymX(dirty_MUT_VAR) \
492 SymX(divExactIntegerzh_fast) \
493 SymX(divModIntegerzh_fast) \
496 SymX(forkOS_createThread) \
497 SymX(freeHaskellFunctionPtr) \
498 SymX(freeStablePtr) \
499 SymX(gcdIntegerzh_fast) \
500 SymX(gcdIntegerIntzh_fast) \
501 SymX(gcdIntzh_fast) \
510 SymX(hs_perform_gc) \
511 SymX(hs_free_stable_ptr) \
512 SymX(hs_free_fun_ptr) \
514 SymX(int2Integerzh_fast) \
515 SymX(integer2Intzh_fast) \
516 SymX(integer2Wordzh_fast) \
517 SymX(isCurrentThreadBoundzh_fast) \
518 SymX(isDoubleDenormalized) \
519 SymX(isDoubleInfinite) \
521 SymX(isDoubleNegativeZero) \
522 SymX(isEmptyMVarzh_fast) \
523 SymX(isFloatDenormalized) \
524 SymX(isFloatInfinite) \
526 SymX(isFloatNegativeZero) \
527 SymX(killThreadzh_fast) \
530 SymX(makeStablePtrzh_fast) \
531 SymX(minusIntegerzh_fast) \
532 SymX(mkApUpd0zh_fast) \
533 SymX(myThreadIdzh_fast) \
534 SymX(labelThreadzh_fast) \
535 SymX(newArrayzh_fast) \
536 SymX(newBCOzh_fast) \
537 SymX(newByteArrayzh_fast) \
538 SymX_redirect(newCAF, newDynCAF) \
539 SymX(newMVarzh_fast) \
540 SymX(newMutVarzh_fast) \
541 SymX(newTVarzh_fast) \
542 SymX(atomicModifyMutVarzh_fast) \
543 SymX(newPinnedByteArrayzh_fast) \
545 SymX(orIntegerzh_fast) \
547 SymX(performMajorGC) \
548 SymX(plusIntegerzh_fast) \
551 SymX(putMVarzh_fast) \
552 SymX(quotIntegerzh_fast) \
553 SymX(quotRemIntegerzh_fast) \
555 SymX(raiseIOzh_fast) \
556 SymX(readTVarzh_fast) \
557 SymX(remIntegerzh_fast) \
558 SymX(resetNonBlockingFd) \
563 SymX(rts_checkSchedStatus) \
566 SymX(rts_evalLazyIO) \
567 SymX(rts_evalStableIO) \
571 SymX(rts_getDouble) \
576 SymX(rts_getFunPtr) \
577 SymX(rts_getStablePtr) \
578 SymX(rts_getThreadId) \
580 SymX(rts_getWord32) \
593 SymX(rts_mkStablePtr) \
601 SymX(rtsSupportsBoundThreads) \
602 SymX(__hscore_get_saved_termios) \
603 SymX(__hscore_set_saved_termios) \
605 SymX(startupHaskell) \
606 SymX(shutdownHaskell) \
607 SymX(shutdownHaskellAndExit) \
608 SymX(stable_ptr_table) \
609 SymX(stackOverflow) \
610 SymX(stg_CAF_BLACKHOLE_info) \
611 SymX(awakenBlockedQueue) \
612 SymX(stg_CHARLIKE_closure) \
613 SymX(stg_EMPTY_MVAR_info) \
614 SymX(stg_IND_STATIC_info) \
615 SymX(stg_INTLIKE_closure) \
616 SymX(stg_MUT_ARR_PTRS_DIRTY_info) \
617 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
618 SymX(stg_MUT_ARR_PTRS_FROZEN0_info) \
619 SymX(stg_WEAK_info) \
620 SymX(stg_ap_0_info) \
621 SymX(stg_ap_v_info) \
622 SymX(stg_ap_f_info) \
623 SymX(stg_ap_d_info) \
624 SymX(stg_ap_l_info) \
625 SymX(stg_ap_n_info) \
626 SymX(stg_ap_p_info) \
627 SymX(stg_ap_pv_info) \
628 SymX(stg_ap_pp_info) \
629 SymX(stg_ap_ppv_info) \
630 SymX(stg_ap_ppp_info) \
631 SymX(stg_ap_pppv_info) \
632 SymX(stg_ap_pppp_info) \
633 SymX(stg_ap_ppppp_info) \
634 SymX(stg_ap_pppppp_info) \
635 SymX(stg_ap_1_upd_info) \
636 SymX(stg_ap_2_upd_info) \
637 SymX(stg_ap_3_upd_info) \
638 SymX(stg_ap_4_upd_info) \
639 SymX(stg_ap_5_upd_info) \
640 SymX(stg_ap_6_upd_info) \
641 SymX(stg_ap_7_upd_info) \
643 SymX(stg_sel_0_upd_info) \
644 SymX(stg_sel_10_upd_info) \
645 SymX(stg_sel_11_upd_info) \
646 SymX(stg_sel_12_upd_info) \
647 SymX(stg_sel_13_upd_info) \
648 SymX(stg_sel_14_upd_info) \
649 SymX(stg_sel_15_upd_info) \
650 SymX(stg_sel_1_upd_info) \
651 SymX(stg_sel_2_upd_info) \
652 SymX(stg_sel_3_upd_info) \
653 SymX(stg_sel_4_upd_info) \
654 SymX(stg_sel_5_upd_info) \
655 SymX(stg_sel_6_upd_info) \
656 SymX(stg_sel_7_upd_info) \
657 SymX(stg_sel_8_upd_info) \
658 SymX(stg_sel_9_upd_info) \
659 SymX(stg_upd_frame_info) \
660 SymX(suspendThread) \
661 SymX(takeMVarzh_fast) \
662 SymX(timesIntegerzh_fast) \
663 SymX(tryPutMVarzh_fast) \
664 SymX(tryTakeMVarzh_fast) \
665 SymX(unblockAsyncExceptionszh_fast) \
667 SymX(unsafeThawArrayzh_fast) \
668 SymX(waitReadzh_fast) \
669 SymX(waitWritezh_fast) \
670 SymX(word2Integerzh_fast) \
671 SymX(writeTVarzh_fast) \
672 SymX(xorIntegerzh_fast) \
674 SymX(stg_interp_constr_entry) \
675 SymX(stg_interp_constr1_entry) \
676 SymX(stg_interp_constr2_entry) \
677 SymX(stg_interp_constr3_entry) \
678 SymX(stg_interp_constr4_entry) \
679 SymX(stg_interp_constr5_entry) \
680 SymX(stg_interp_constr6_entry) \
681 SymX(stg_interp_constr7_entry) \
682 SymX(stg_interp_constr8_entry) \
683 SymX(stgMallocBytesRWX) \
684 SymX(getAllocations) \
687 RTS_USER_SIGNALS_SYMBOLS
689 #ifdef SUPPORT_LONG_LONGS
690 #define RTS_LONG_LONG_SYMS \
691 SymX(int64ToIntegerzh_fast) \
692 SymX(word64ToIntegerzh_fast)
694 #define RTS_LONG_LONG_SYMS /* nothing */
697 // 64-bit support functions in libgcc.a
698 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
699 #define RTS_LIBGCC_SYMBOLS \
709 #elif defined(ia64_HOST_ARCH)
710 #define RTS_LIBGCC_SYMBOLS \
718 #define RTS_LIBGCC_SYMBOLS
721 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
722 // Symbols that don't have a leading underscore
723 // on Mac OS X. They have to receive special treatment,
724 // see machoInitSymbolsWithoutUnderscore()
725 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
730 /* entirely bogus claims about types of these symbols */
731 #define Sym(vvv) extern void vvv(void);
732 #define SymX(vvv) /**/
733 #define SymX_redirect(vvv,xxx) /**/
737 RTS_POSIX_ONLY_SYMBOLS
738 RTS_MINGW_ONLY_SYMBOLS
739 RTS_CYGWIN_ONLY_SYMBOLS
740 RTS_DARWIN_ONLY_SYMBOLS
746 #ifdef LEADING_UNDERSCORE
747 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
749 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
752 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
754 #define SymX(vvv) Sym(vvv)
756 // SymX_redirect allows us to redirect references to one symbol to
757 // another symbol. See newCAF/newDynCAF for an example.
758 #define SymX_redirect(vvv,xxx) \
759 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
762 static RtsSymbolVal rtsSyms[] = {
766 RTS_POSIX_ONLY_SYMBOLS
767 RTS_MINGW_ONLY_SYMBOLS
768 RTS_CYGWIN_ONLY_SYMBOLS
770 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
771 // dyld stub code contains references to this,
772 // but it should never be called because we treat
773 // lazy pointers as nonlazy.
774 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
776 { 0, 0 } /* sentinel */
779 /* -----------------------------------------------------------------------------
780 * Insert symbols into hash tables, checking for duplicates.
782 static void ghciInsertStrHashTable ( char* obj_name,
788 if (lookupHashTable(table, (StgWord)key) == NULL)
790 insertStrHashTable(table, (StgWord)key, data);
795 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
797 "whilst processing object file\n"
799 "This could be caused by:\n"
800 " * Loading two different object files which export the same symbol\n"
801 " * Specifying the same object file twice on the GHCi command line\n"
802 " * An incorrect `package.conf' entry, causing some object to be\n"
804 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
813 /* -----------------------------------------------------------------------------
814 * initialize the object linker
818 static int linker_init_done = 0 ;
820 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
821 static void *dl_prog_handle;
824 /* dlopen(NULL,..) doesn't work so we grab libc explicitly */
825 #if defined(openbsd_HOST_OS)
826 static void *dl_libc_handle;
834 /* Make initLinker idempotent, so we can call it
835 before evey relevant operation; that means we
836 don't need to initialise the linker separately */
837 if (linker_init_done == 1) { return; } else {
838 linker_init_done = 1;
841 symhash = allocStrHashTable();
843 /* populate the symbol table with stuff from the RTS */
844 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
845 ghciInsertStrHashTable("(GHCi built-in symbols)",
846 symhash, sym->lbl, sym->addr);
848 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
849 machoInitSymbolsWithoutUnderscore();
852 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
853 # if defined(RTLD_DEFAULT)
854 dl_prog_handle = RTLD_DEFAULT;
856 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
857 # if defined(openbsd_HOST_OS)
858 dl_libc_handle = dlopen("libc.so", RTLD_LAZY);
860 # endif /* RTLD_DEFAULT */
864 /* -----------------------------------------------------------------------------
865 * Loading DLL or .so dynamic libraries
866 * -----------------------------------------------------------------------------
868 * Add a DLL from which symbols may be found. In the ELF case, just
869 * do RTLD_GLOBAL-style add, so no further messing around needs to
870 * happen in order that symbols in the loaded .so are findable --
871 * lookupSymbol() will subsequently see them by dlsym on the program's
872 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
874 * In the PEi386 case, open the DLLs and put handles to them in a
875 * linked list. When looking for a symbol, try all handles in the
876 * list. This means that we need to load even DLLs that are guaranteed
877 * to be in the ghc.exe image already, just so we can get a handle
878 * to give to loadSymbol, so that we can find the symbols. For such
879 * libraries, the LoadLibrary call should be a no-op except for returning
884 #if defined(OBJFORMAT_PEi386)
885 /* A record for storing handles into DLLs. */
890 struct _OpenedDLL* next;
895 /* A list thereof. */
896 static OpenedDLL* opened_dlls = NULL;
900 addDLL( char *dll_name )
902 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
903 /* ------------------- ELF DLL loader ------------------- */
909 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
912 /* dlopen failed; return a ptr to the error msg. */
914 if (errmsg == NULL) errmsg = "addDLL: unknown error";
921 # elif defined(OBJFORMAT_PEi386)
922 /* ------------------- Win32 DLL loader ------------------- */
930 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
932 /* See if we've already got it, and ignore if so. */
933 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
934 if (0 == strcmp(o_dll->name, dll_name))
938 /* The file name has no suffix (yet) so that we can try
939 both foo.dll and foo.drv
941 The documentation for LoadLibrary says:
942 If no file name extension is specified in the lpFileName
943 parameter, the default library extension .dll is
944 appended. However, the file name string can include a trailing
945 point character (.) to indicate that the module name has no
948 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
949 sprintf(buf, "%s.DLL", dll_name);
950 instance = LoadLibrary(buf);
951 if (instance == NULL) {
952 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
953 instance = LoadLibrary(buf);
954 if (instance == NULL) {
957 /* LoadLibrary failed; return a ptr to the error msg. */
958 return "addDLL: unknown error";
963 /* Add this DLL to the list of DLLs in which to search for symbols. */
964 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
965 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
966 strcpy(o_dll->name, dll_name);
967 o_dll->instance = instance;
968 o_dll->next = opened_dlls;
973 barf("addDLL: not implemented on this platform");
977 /* -----------------------------------------------------------------------------
978 * lookup a symbol in the hash table
981 lookupSymbol( char *lbl )
985 ASSERT(symhash != NULL);
986 val = lookupStrHashTable(symhash, lbl);
989 # if defined(OBJFORMAT_ELF)
990 # if defined(openbsd_HOST_OS)
991 val = dlsym(dl_prog_handle, lbl);
992 return (val != NULL) ? val : dlsym(dl_libc_handle,lbl);
993 # elif defined(x86_64_HOST_ARCH)
994 val = dlsym(dl_prog_handle, lbl);
995 if (val >= (void *)0x80000000) {
997 new_val = x86_64_high_symbol(lbl, val);
998 IF_DEBUG(linker,debugBelch("lookupSymbol: relocating out of range symbol: %s = %p, now %p\n", lbl, val, new_val));
1003 # else /* not openbsd */
1004 return dlsym(dl_prog_handle, lbl);
1006 # elif defined(OBJFORMAT_MACHO)
1007 if(NSIsSymbolNameDefined(lbl)) {
1008 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
1009 return NSAddressOfSymbol(symbol);
1013 # elif defined(OBJFORMAT_PEi386)
1016 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1017 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
1018 if (lbl[0] == '_') {
1019 /* HACK: if the name has an initial underscore, try stripping
1020 it off & look that up first. I've yet to verify whether there's
1021 a Rule that governs whether an initial '_' *should always* be
1022 stripped off when mapping from import lib name to the DLL name.
1024 sym = GetProcAddress(o_dll->instance, (lbl+1));
1026 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
1030 sym = GetProcAddress(o_dll->instance, lbl);
1032 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
1047 __attribute((unused))
1049 lookupLocalSymbol( ObjectCode* oc, char *lbl )
1053 val = lookupStrHashTable(oc->lochash, lbl);
1063 /* -----------------------------------------------------------------------------
1064 * Debugging aid: look in GHCi's object symbol tables for symbols
1065 * within DELTA bytes of the specified address, and show their names.
1068 void ghci_enquire ( char* addr );
1070 void ghci_enquire ( char* addr )
1075 const int DELTA = 64;
1080 for (oc = objects; oc; oc = oc->next) {
1081 for (i = 0; i < oc->n_symbols; i++) {
1082 sym = oc->symbols[i];
1083 if (sym == NULL) continue;
1084 // debugBelch("enquire %p %p\n", sym, oc->lochash);
1086 if (oc->lochash != NULL) {
1087 a = lookupStrHashTable(oc->lochash, sym);
1090 a = lookupStrHashTable(symhash, sym);
1093 // debugBelch("ghci_enquire: can't find %s\n", sym);
1095 else if (addr-DELTA <= a && a <= addr+DELTA) {
1096 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1103 #ifdef ia64_HOST_ARCH
1104 static unsigned int PLTSize(void);
1107 /* -----------------------------------------------------------------------------
1108 * Load an obj (populate the global symbol table, but don't resolve yet)
1110 * Returns: 1 if ok, 0 on error.
1113 loadObj( char *path )
1120 void *map_addr = NULL;
1127 /* debugBelch("loadObj %s\n", path ); */
1129 /* Check that we haven't already loaded this object.
1130 Ignore requests to load multiple times */
1134 for (o = objects; o; o = o->next) {
1135 if (0 == strcmp(o->fileName, path)) {
1137 break; /* don't need to search further */
1141 IF_DEBUG(linker, debugBelch(
1142 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1143 "same object file twice:\n"
1145 "GHCi will ignore this, but be warned.\n"
1147 return 1; /* success */
1151 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
1153 # if defined(OBJFORMAT_ELF)
1154 oc->formatName = "ELF";
1155 # elif defined(OBJFORMAT_PEi386)
1156 oc->formatName = "PEi386";
1157 # elif defined(OBJFORMAT_MACHO)
1158 oc->formatName = "Mach-O";
1161 barf("loadObj: not implemented on this platform");
1164 r = stat(path, &st);
1165 if (r == -1) { return 0; }
1167 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1168 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1169 strcpy(oc->fileName, path);
1171 oc->fileSize = st.st_size;
1173 oc->sections = NULL;
1174 oc->lochash = allocStrHashTable();
1175 oc->proddables = NULL;
1177 /* chain it onto the list of objects */
1182 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1184 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1186 #if defined(openbsd_HOST_OS)
1187 fd = open(path, O_RDONLY, S_IRUSR);
1189 fd = open(path, O_RDONLY);
1192 barf("loadObj: can't open `%s'", path);
1194 pagesize = getpagesize();
1196 #ifdef ia64_HOST_ARCH
1197 /* The PLT needs to be right before the object */
1198 n = ROUND_UP(PLTSize(), pagesize);
1199 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1200 if (oc->plt == MAP_FAILED)
1201 barf("loadObj: can't allocate PLT");
1204 map_addr = oc->plt + n;
1207 n = ROUND_UP(oc->fileSize, pagesize);
1209 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
1210 * small memory model on this architecture (see gcc docs,
1213 #ifdef x86_64_HOST_ARCH
1214 #define EXTRA_MAP_FLAGS MAP_32BIT
1216 #define EXTRA_MAP_FLAGS 0
1219 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE,
1220 MAP_PRIVATE|EXTRA_MAP_FLAGS, fd, 0);
1221 if (oc->image == MAP_FAILED)
1222 barf("loadObj: can't map `%s'", path);
1226 #else /* !USE_MMAP */
1228 /* load the image into memory */
1229 f = fopen(path, "rb");
1231 barf("loadObj: can't read `%s'", path);
1233 #ifdef darwin_HOST_OS
1234 // In a Mach-O .o file, all sections can and will be misaligned
1235 // if the total size of the headers is not a multiple of the
1236 // desired alignment. This is fine for .o files that only serve
1237 // as input for the static linker, but it's not fine for us,
1238 // as SSE (used by gcc for floating point) and Altivec require
1239 // 16-byte alignment.
1240 // We calculate the correct alignment from the header before
1241 // reading the file, and then we misalign oc->image on purpose so
1242 // that the actual sections end up aligned again.
1243 misalignment = machoGetMisalignment(f);
1244 oc->misalignment = misalignment;
1249 oc->image = stgMallocBytes(oc->fileSize + misalignment, "loadObj(image)");
1250 oc->image += misalignment;
1252 n = fread ( oc->image, 1, oc->fileSize, f );
1253 if (n != oc->fileSize)
1254 barf("loadObj: error whilst reading `%s'", path);
1258 #endif /* USE_MMAP */
1260 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1261 r = ocAllocateJumpIslands_MachO ( oc );
1262 if (!r) { return r; }
1263 # elif defined(OBJFORMAT_ELF) && defined(powerpc_HOST_ARCH)
1264 r = ocAllocateJumpIslands_ELF ( oc );
1265 if (!r) { return r; }
1268 /* verify the in-memory image */
1269 # if defined(OBJFORMAT_ELF)
1270 r = ocVerifyImage_ELF ( oc );
1271 # elif defined(OBJFORMAT_PEi386)
1272 r = ocVerifyImage_PEi386 ( oc );
1273 # elif defined(OBJFORMAT_MACHO)
1274 r = ocVerifyImage_MachO ( oc );
1276 barf("loadObj: no verify method");
1278 if (!r) { return r; }
1280 /* build the symbol list for this image */
1281 # if defined(OBJFORMAT_ELF)
1282 r = ocGetNames_ELF ( oc );
1283 # elif defined(OBJFORMAT_PEi386)
1284 r = ocGetNames_PEi386 ( oc );
1285 # elif defined(OBJFORMAT_MACHO)
1286 r = ocGetNames_MachO ( oc );
1288 barf("loadObj: no getNames method");
1290 if (!r) { return r; }
1292 /* loaded, but not resolved yet */
1293 oc->status = OBJECT_LOADED;
1298 /* -----------------------------------------------------------------------------
1299 * resolve all the currently unlinked objects in memory
1301 * Returns: 1 if ok, 0 on error.
1311 for (oc = objects; oc; oc = oc->next) {
1312 if (oc->status != OBJECT_RESOLVED) {
1313 # if defined(OBJFORMAT_ELF)
1314 r = ocResolve_ELF ( oc );
1315 # elif defined(OBJFORMAT_PEi386)
1316 r = ocResolve_PEi386 ( oc );
1317 # elif defined(OBJFORMAT_MACHO)
1318 r = ocResolve_MachO ( oc );
1320 barf("resolveObjs: not implemented on this platform");
1322 if (!r) { return r; }
1323 oc->status = OBJECT_RESOLVED;
1329 /* -----------------------------------------------------------------------------
1330 * delete an object from the pool
1333 unloadObj( char *path )
1335 ObjectCode *oc, *prev;
1337 ASSERT(symhash != NULL);
1338 ASSERT(objects != NULL);
1343 for (oc = objects; oc; prev = oc, oc = oc->next) {
1344 if (!strcmp(oc->fileName,path)) {
1346 /* Remove all the mappings for the symbols within this
1351 for (i = 0; i < oc->n_symbols; i++) {
1352 if (oc->symbols[i] != NULL) {
1353 removeStrHashTable(symhash, oc->symbols[i], NULL);
1361 prev->next = oc->next;
1364 /* We're going to leave this in place, in case there are
1365 any pointers from the heap into it: */
1366 /* stgFree(oc->image); */
1367 stgFree(oc->fileName);
1368 stgFree(oc->symbols);
1369 stgFree(oc->sections);
1370 /* The local hash table should have been freed at the end
1371 of the ocResolve_ call on it. */
1372 ASSERT(oc->lochash == NULL);
1378 errorBelch("unloadObj: can't find `%s' to unload", path);
1382 /* -----------------------------------------------------------------------------
1383 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1384 * which may be prodded during relocation, and abort if we try and write
1385 * outside any of these.
1387 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1390 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1391 /* debugBelch("aPB %p %p %d\n", oc, start, size); */
1395 pb->next = oc->proddables;
1396 oc->proddables = pb;
1399 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1402 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1403 char* s = (char*)(pb->start);
1404 char* e = s + pb->size - 1;
1405 char* a = (char*)addr;
1406 /* Assumes that the biggest fixup involves a 4-byte write. This
1407 probably needs to be changed to 8 (ie, +7) on 64-bit
1409 if (a >= s && (a+3) <= e) return;
1411 barf("checkProddableBlock: invalid fixup in runtime linker");
1414 /* -----------------------------------------------------------------------------
1415 * Section management.
1417 static void addSection ( ObjectCode* oc, SectionKind kind,
1418 void* start, void* end )
1420 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1424 s->next = oc->sections;
1427 debugBelch("addSection: %p-%p (size %d), kind %d\n",
1428 start, ((char*)end)-1, end - start + 1, kind );
1433 /* --------------------------------------------------------------------------
1434 * PowerPC specifics (jump islands)
1435 * ------------------------------------------------------------------------*/
1437 #if defined(powerpc_HOST_ARCH)
1440 ocAllocateJumpIslands
1442 Allocate additional space at the end of the object file image to make room
1445 PowerPC relative branch instructions have a 24 bit displacement field.
1446 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
1447 If a particular imported symbol is outside this range, we have to redirect
1448 the jump to a short piece of new code that just loads the 32bit absolute
1449 address and jumps there.
1450 This function just allocates space for one 16 byte ppcJumpIsland for every
1451 undefined symbol in the object file. The code for the islands is filled in by
1452 makeJumpIsland below.
1455 static int ocAllocateJumpIslands( ObjectCode* oc, int count, int first )
1461 int misalignment = 0;
1463 misalignment = oc->misalignment;
1468 // round up to the nearest 4
1469 aligned = (oc->fileSize + 3) & ~3;
1472 #ifndef linux_HOST_OS /* mremap is a linux extension */
1473 #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined
1476 pagesize = getpagesize();
1477 n = ROUND_UP( oc->fileSize, pagesize );
1478 m = ROUND_UP( aligned + sizeof (ppcJumpIsland) * count, pagesize );
1480 /* If we have a half-page-size file and map one page of it then
1481 * the part of the page after the size of the file remains accessible.
1482 * If, however, we map in 2 pages, the 2nd page is not accessible
1483 * and will give a "Bus Error" on access. To get around this, we check
1484 * if we need any extra pages for the jump islands and map them in
1485 * anonymously. We must check that we actually require extra pages
1486 * otherwise the attempt to mmap 0 pages of anonymous memory will
1492 /* The effect of this mremap() call is only the ensure that we have
1493 * a sufficient number of virtually contiguous pages. As returned from
1494 * mremap, the pages past the end of the file are not backed. We give
1495 * them a backing by using MAP_FIXED to map in anonymous pages.
1497 oc->image = mremap( oc->image, n, m, MREMAP_MAYMOVE );
1499 if( oc->image == MAP_FAILED )
1501 errorBelch( "Unable to mremap for Jump Islands\n" );
1505 if( mmap( oc->image + n, m - n, PROT_READ | PROT_WRITE | PROT_EXEC,
1506 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0 ) == MAP_FAILED )
1508 errorBelch( "Unable to mmap( MAP_FIXED ) for Jump Islands\n" );
1514 oc->image -= misalignment;
1515 oc->image = stgReallocBytes( oc->image,
1517 aligned + sizeof (ppcJumpIsland) * count,
1518 "ocAllocateJumpIslands" );
1519 oc->image += misalignment;
1520 #endif /* USE_MMAP */
1522 oc->jump_islands = (ppcJumpIsland *) (oc->image + aligned);
1523 memset( oc->jump_islands, 0, sizeof (ppcJumpIsland) * count );
1526 oc->jump_islands = NULL;
1528 oc->island_start_symbol = first;
1529 oc->n_islands = count;
1534 static unsigned long makeJumpIsland( ObjectCode* oc,
1535 unsigned long symbolNumber,
1536 unsigned long target )
1538 ppcJumpIsland *island;
1540 if( symbolNumber < oc->island_start_symbol ||
1541 symbolNumber - oc->island_start_symbol > oc->n_islands)
1544 island = &oc->jump_islands[symbolNumber - oc->island_start_symbol];
1546 // lis r12, hi16(target)
1547 island->lis_r12 = 0x3d80;
1548 island->hi_addr = target >> 16;
1550 // ori r12, r12, lo16(target)
1551 island->ori_r12_r12 = 0x618c;
1552 island->lo_addr = target & 0xffff;
1555 island->mtctr_r12 = 0x7d8903a6;
1558 island->bctr = 0x4e800420;
1560 return (unsigned long) island;
1564 ocFlushInstructionCache
1566 Flush the data & instruction caches.
1567 Because the PPC has split data/instruction caches, we have to
1568 do that whenever we modify code at runtime.
1571 static void ocFlushInstructionCache( ObjectCode *oc )
1573 int n = (oc->fileSize + sizeof( ppcJumpIsland ) * oc->n_islands + 3) / 4;
1574 unsigned long *p = (unsigned long *) oc->image;
1578 __asm__ volatile ( "dcbf 0,%0\n\t"
1586 __asm__ volatile ( "sync\n\t"
1592 /* --------------------------------------------------------------------------
1593 * PEi386 specifics (Win32 targets)
1594 * ------------------------------------------------------------------------*/
1596 /* The information for this linker comes from
1597 Microsoft Portable Executable
1598 and Common Object File Format Specification
1599 revision 5.1 January 1998
1600 which SimonM says comes from the MS Developer Network CDs.
1602 It can be found there (on older CDs), but can also be found
1605 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1607 (this is Rev 6.0 from February 1999).
1609 Things move, so if that fails, try searching for it via
1611 http://www.google.com/search?q=PE+COFF+specification
1613 The ultimate reference for the PE format is the Winnt.h
1614 header file that comes with the Platform SDKs; as always,
1615 implementations will drift wrt their documentation.
1617 A good background article on the PE format is Matt Pietrek's
1618 March 1994 article in Microsoft System Journal (MSJ)
1619 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1620 Win32 Portable Executable File Format." The info in there
1621 has recently been updated in a two part article in
1622 MSDN magazine, issues Feb and March 2002,
1623 "Inside Windows: An In-Depth Look into the Win32 Portable
1624 Executable File Format"
1626 John Levine's book "Linkers and Loaders" contains useful
1631 #if defined(OBJFORMAT_PEi386)
1635 typedef unsigned char UChar;
1636 typedef unsigned short UInt16;
1637 typedef unsigned int UInt32;
1644 UInt16 NumberOfSections;
1645 UInt32 TimeDateStamp;
1646 UInt32 PointerToSymbolTable;
1647 UInt32 NumberOfSymbols;
1648 UInt16 SizeOfOptionalHeader;
1649 UInt16 Characteristics;
1653 #define sizeof_COFF_header 20
1660 UInt32 VirtualAddress;
1661 UInt32 SizeOfRawData;
1662 UInt32 PointerToRawData;
1663 UInt32 PointerToRelocations;
1664 UInt32 PointerToLinenumbers;
1665 UInt16 NumberOfRelocations;
1666 UInt16 NumberOfLineNumbers;
1667 UInt32 Characteristics;
1671 #define sizeof_COFF_section 40
1678 UInt16 SectionNumber;
1681 UChar NumberOfAuxSymbols;
1685 #define sizeof_COFF_symbol 18
1690 UInt32 VirtualAddress;
1691 UInt32 SymbolTableIndex;
1696 #define sizeof_COFF_reloc 10
1699 /* From PE spec doc, section 3.3.2 */
1700 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1701 windows.h -- for the same purpose, but I want to know what I'm
1703 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1704 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1705 #define MYIMAGE_FILE_DLL 0x2000
1706 #define MYIMAGE_FILE_SYSTEM 0x1000
1707 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1708 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1709 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1711 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1712 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1713 #define MYIMAGE_SYM_CLASS_STATIC 3
1714 #define MYIMAGE_SYM_UNDEFINED 0
1716 /* From PE spec doc, section 4.1 */
1717 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1718 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1719 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1721 /* From PE spec doc, section 5.2.1 */
1722 #define MYIMAGE_REL_I386_DIR32 0x0006
1723 #define MYIMAGE_REL_I386_REL32 0x0014
1726 /* We use myindex to calculate array addresses, rather than
1727 simply doing the normal subscript thing. That's because
1728 some of the above structs have sizes which are not
1729 a whole number of words. GCC rounds their sizes up to a
1730 whole number of words, which means that the address calcs
1731 arising from using normal C indexing or pointer arithmetic
1732 are just plain wrong. Sigh.
1735 myindex ( int scale, void* base, int index )
1738 ((UChar*)base) + scale * index;
1743 printName ( UChar* name, UChar* strtab )
1745 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1746 UInt32 strtab_offset = * (UInt32*)(name+4);
1747 debugBelch("%s", strtab + strtab_offset );
1750 for (i = 0; i < 8; i++) {
1751 if (name[i] == 0) break;
1752 debugBelch("%c", name[i] );
1759 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1761 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1762 UInt32 strtab_offset = * (UInt32*)(name+4);
1763 strncpy ( dst, strtab+strtab_offset, dstSize );
1769 if (name[i] == 0) break;
1779 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1782 /* If the string is longer than 8 bytes, look in the
1783 string table for it -- this will be correctly zero terminated.
1785 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1786 UInt32 strtab_offset = * (UInt32*)(name+4);
1787 return ((UChar*)strtab) + strtab_offset;
1789 /* Otherwise, if shorter than 8 bytes, return the original,
1790 which by defn is correctly terminated.
1792 if (name[7]==0) return name;
1793 /* The annoying case: 8 bytes. Copy into a temporary
1794 (which is never freed ...)
1796 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1798 strncpy(newstr,name,8);
1804 /* Just compares the short names (first 8 chars) */
1805 static COFF_section *
1806 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1810 = (COFF_header*)(oc->image);
1811 COFF_section* sectab
1813 ((UChar*)(oc->image))
1814 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1816 for (i = 0; i < hdr->NumberOfSections; i++) {
1819 COFF_section* section_i
1821 myindex ( sizeof_COFF_section, sectab, i );
1822 n1 = (UChar*) &(section_i->Name);
1824 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1825 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1826 n1[6]==n2[6] && n1[7]==n2[7])
1835 zapTrailingAtSign ( UChar* sym )
1837 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1839 if (sym[0] == 0) return;
1841 while (sym[i] != 0) i++;
1844 while (j > 0 && my_isdigit(sym[j])) j--;
1845 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1851 ocVerifyImage_PEi386 ( ObjectCode* oc )
1856 COFF_section* sectab;
1857 COFF_symbol* symtab;
1859 /* debugBelch("\nLOADING %s\n", oc->fileName); */
1860 hdr = (COFF_header*)(oc->image);
1861 sectab = (COFF_section*) (
1862 ((UChar*)(oc->image))
1863 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1865 symtab = (COFF_symbol*) (
1866 ((UChar*)(oc->image))
1867 + hdr->PointerToSymbolTable
1869 strtab = ((UChar*)symtab)
1870 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1872 if (hdr->Machine != 0x14c) {
1873 errorBelch("%s: Not x86 PEi386", oc->fileName);
1876 if (hdr->SizeOfOptionalHeader != 0) {
1877 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
1880 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1881 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1882 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1883 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1884 errorBelch("%s: Not a PEi386 object file", oc->fileName);
1887 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1888 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1889 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
1891 (int)(hdr->Characteristics));
1894 /* If the string table size is way crazy, this might indicate that
1895 there are more than 64k relocations, despite claims to the
1896 contrary. Hence this test. */
1897 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
1899 if ( (*(UInt32*)strtab) > 600000 ) {
1900 /* Note that 600k has no special significance other than being
1901 big enough to handle the almost-2MB-sized lumps that
1902 constitute HSwin32*.o. */
1903 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
1908 /* No further verification after this point; only debug printing. */
1910 IF_DEBUG(linker, i=1);
1911 if (i == 0) return 1;
1913 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1914 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1915 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1918 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1919 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1920 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1921 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1922 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1923 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1924 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1926 /* Print the section table. */
1928 for (i = 0; i < hdr->NumberOfSections; i++) {
1930 COFF_section* sectab_i
1932 myindex ( sizeof_COFF_section, sectab, i );
1939 printName ( sectab_i->Name, strtab );
1949 sectab_i->VirtualSize,
1950 sectab_i->VirtualAddress,
1951 sectab_i->SizeOfRawData,
1952 sectab_i->PointerToRawData,
1953 sectab_i->NumberOfRelocations,
1954 sectab_i->PointerToRelocations,
1955 sectab_i->PointerToRawData
1957 reltab = (COFF_reloc*) (
1958 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1961 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1962 /* If the relocation field (a short) has overflowed, the
1963 * real count can be found in the first reloc entry.
1965 * See Section 4.1 (last para) of the PE spec (rev6.0).
1967 COFF_reloc* rel = (COFF_reloc*)
1968 myindex ( sizeof_COFF_reloc, reltab, 0 );
1969 noRelocs = rel->VirtualAddress;
1972 noRelocs = sectab_i->NumberOfRelocations;
1976 for (; j < noRelocs; j++) {
1978 COFF_reloc* rel = (COFF_reloc*)
1979 myindex ( sizeof_COFF_reloc, reltab, j );
1981 " type 0x%-4x vaddr 0x%-8x name `",
1983 rel->VirtualAddress );
1984 sym = (COFF_symbol*)
1985 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1986 /* Hmm..mysterious looking offset - what's it for? SOF */
1987 printName ( sym->Name, strtab -10 );
1994 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
1995 debugBelch("---START of string table---\n");
1996 for (i = 4; i < *(Int32*)strtab; i++) {
1998 debugBelch("\n"); else
1999 debugBelch("%c", strtab[i] );
2001 debugBelch("--- END of string table---\n");
2006 COFF_symbol* symtab_i;
2007 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2008 symtab_i = (COFF_symbol*)
2009 myindex ( sizeof_COFF_symbol, symtab, i );
2015 printName ( symtab_i->Name, strtab );
2024 (Int32)(symtab_i->SectionNumber),
2025 (UInt32)symtab_i->Type,
2026 (UInt32)symtab_i->StorageClass,
2027 (UInt32)symtab_i->NumberOfAuxSymbols
2029 i += symtab_i->NumberOfAuxSymbols;
2039 ocGetNames_PEi386 ( ObjectCode* oc )
2042 COFF_section* sectab;
2043 COFF_symbol* symtab;
2050 hdr = (COFF_header*)(oc->image);
2051 sectab = (COFF_section*) (
2052 ((UChar*)(oc->image))
2053 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2055 symtab = (COFF_symbol*) (
2056 ((UChar*)(oc->image))
2057 + hdr->PointerToSymbolTable
2059 strtab = ((UChar*)(oc->image))
2060 + hdr->PointerToSymbolTable
2061 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2063 /* Allocate space for any (local, anonymous) .bss sections. */
2065 for (i = 0; i < hdr->NumberOfSections; i++) {
2068 COFF_section* sectab_i
2070 myindex ( sizeof_COFF_section, sectab, i );
2071 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
2072 /* sof 10/05: the PE spec text isn't too clear regarding what
2073 * the SizeOfRawData field is supposed to hold for object
2074 * file sections containing just uninitialized data -- for executables,
2075 * it is supposed to be zero; unclear what it's supposed to be
2076 * for object files. However, VirtualSize is guaranteed to be
2077 * zero for object files, which definitely suggests that SizeOfRawData
2078 * will be non-zero (where else would the size of this .bss section be
2079 * stored?) Looking at the COFF_section info for incoming object files,
2080 * this certainly appears to be the case.
2082 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
2083 * object files up until now. This turned out to bite us with ghc-6.4.1's use
2084 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
2085 * variable decls into to the .bss section. (The specific function in Q which
2086 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
2088 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
2089 /* This is a non-empty .bss section. Allocate zeroed space for
2090 it, and set its PointerToRawData field such that oc->image +
2091 PointerToRawData == addr_of_zeroed_space. */
2092 bss_sz = sectab_i->VirtualSize;
2093 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
2094 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
2095 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
2096 addProddableBlock(oc, zspace, bss_sz);
2097 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
2100 /* Copy section information into the ObjectCode. */
2102 for (i = 0; i < hdr->NumberOfSections; i++) {
2108 = SECTIONKIND_OTHER;
2109 COFF_section* sectab_i
2111 myindex ( sizeof_COFF_section, sectab, i );
2112 IF_DEBUG(linker, debugBelch("section name = %s\n", sectab_i->Name ));
2115 /* I'm sure this is the Right Way to do it. However, the
2116 alternative of testing the sectab_i->Name field seems to
2117 work ok with Cygwin.
2119 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
2120 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
2121 kind = SECTIONKIND_CODE_OR_RODATA;
2124 if (0==strcmp(".text",sectab_i->Name) ||
2125 0==strcmp(".rdata",sectab_i->Name)||
2126 0==strcmp(".rodata",sectab_i->Name))
2127 kind = SECTIONKIND_CODE_OR_RODATA;
2128 if (0==strcmp(".data",sectab_i->Name) ||
2129 0==strcmp(".bss",sectab_i->Name))
2130 kind = SECTIONKIND_RWDATA;
2132 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
2133 sz = sectab_i->SizeOfRawData;
2134 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
2136 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
2137 end = start + sz - 1;
2139 if (kind == SECTIONKIND_OTHER
2140 /* Ignore sections called which contain stabs debugging
2142 && 0 != strcmp(".stab", sectab_i->Name)
2143 && 0 != strcmp(".stabstr", sectab_i->Name)
2144 /* ignore constructor section for now */
2145 && 0 != strcmp(".ctors", sectab_i->Name)
2147 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", sectab_i->Name, oc->fileName);
2151 if (kind != SECTIONKIND_OTHER && end >= start) {
2152 addSection(oc, kind, start, end);
2153 addProddableBlock(oc, start, end - start + 1);
2157 /* Copy exported symbols into the ObjectCode. */
2159 oc->n_symbols = hdr->NumberOfSymbols;
2160 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2161 "ocGetNames_PEi386(oc->symbols)");
2162 /* Call me paranoid; I don't care. */
2163 for (i = 0; i < oc->n_symbols; i++)
2164 oc->symbols[i] = NULL;
2168 COFF_symbol* symtab_i;
2169 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2170 symtab_i = (COFF_symbol*)
2171 myindex ( sizeof_COFF_symbol, symtab, i );
2175 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
2176 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
2177 /* This symbol is global and defined, viz, exported */
2178 /* for MYIMAGE_SYMCLASS_EXTERNAL
2179 && !MYIMAGE_SYM_UNDEFINED,
2180 the address of the symbol is:
2181 address of relevant section + offset in section
2183 COFF_section* sectabent
2184 = (COFF_section*) myindex ( sizeof_COFF_section,
2186 symtab_i->SectionNumber-1 );
2187 addr = ((UChar*)(oc->image))
2188 + (sectabent->PointerToRawData
2192 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
2193 && symtab_i->Value > 0) {
2194 /* This symbol isn't in any section at all, ie, global bss.
2195 Allocate zeroed space for it. */
2196 addr = stgCallocBytes(1, symtab_i->Value,
2197 "ocGetNames_PEi386(non-anonymous bss)");
2198 addSection(oc, SECTIONKIND_RWDATA, addr,
2199 ((UChar*)addr) + symtab_i->Value - 1);
2200 addProddableBlock(oc, addr, symtab_i->Value);
2201 /* debugBelch("BSS section at 0x%x\n", addr); */
2204 if (addr != NULL ) {
2205 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
2206 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
2207 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
2208 ASSERT(i >= 0 && i < oc->n_symbols);
2209 /* cstring_from_COFF_symbol_name always succeeds. */
2210 oc->symbols[i] = sname;
2211 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
2215 "IGNORING symbol %d\n"
2219 printName ( symtab_i->Name, strtab );
2228 (Int32)(symtab_i->SectionNumber),
2229 (UInt32)symtab_i->Type,
2230 (UInt32)symtab_i->StorageClass,
2231 (UInt32)symtab_i->NumberOfAuxSymbols
2236 i += symtab_i->NumberOfAuxSymbols;
2245 ocResolve_PEi386 ( ObjectCode* oc )
2248 COFF_section* sectab;
2249 COFF_symbol* symtab;
2259 /* ToDo: should be variable-sized? But is at least safe in the
2260 sense of buffer-overrun-proof. */
2262 /* debugBelch("resolving for %s\n", oc->fileName); */
2264 hdr = (COFF_header*)(oc->image);
2265 sectab = (COFF_section*) (
2266 ((UChar*)(oc->image))
2267 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2269 symtab = (COFF_symbol*) (
2270 ((UChar*)(oc->image))
2271 + hdr->PointerToSymbolTable
2273 strtab = ((UChar*)(oc->image))
2274 + hdr->PointerToSymbolTable
2275 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2277 for (i = 0; i < hdr->NumberOfSections; i++) {
2278 COFF_section* sectab_i
2280 myindex ( sizeof_COFF_section, sectab, i );
2283 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2286 /* Ignore sections called which contain stabs debugging
2288 if (0 == strcmp(".stab", sectab_i->Name)
2289 || 0 == strcmp(".stabstr", sectab_i->Name)
2290 || 0 == strcmp(".ctors", sectab_i->Name))
2293 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2294 /* If the relocation field (a short) has overflowed, the
2295 * real count can be found in the first reloc entry.
2297 * See Section 4.1 (last para) of the PE spec (rev6.0).
2299 * Nov2003 update: the GNU linker still doesn't correctly
2300 * handle the generation of relocatable object files with
2301 * overflown relocations. Hence the output to warn of potential
2304 COFF_reloc* rel = (COFF_reloc*)
2305 myindex ( sizeof_COFF_reloc, reltab, 0 );
2306 noRelocs = rel->VirtualAddress;
2308 /* 10/05: we now assume (and check for) a GNU ld that is capable
2309 * of handling object files with (>2^16) of relocs.
2312 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
2317 noRelocs = sectab_i->NumberOfRelocations;
2322 for (; j < noRelocs; j++) {
2324 COFF_reloc* reltab_j
2326 myindex ( sizeof_COFF_reloc, reltab, j );
2328 /* the location to patch */
2330 ((UChar*)(oc->image))
2331 + (sectab_i->PointerToRawData
2332 + reltab_j->VirtualAddress
2333 - sectab_i->VirtualAddress )
2335 /* the existing contents of pP */
2337 /* the symbol to connect to */
2338 sym = (COFF_symbol*)
2339 myindex ( sizeof_COFF_symbol,
2340 symtab, reltab_j->SymbolTableIndex );
2343 "reloc sec %2d num %3d: type 0x%-4x "
2344 "vaddr 0x%-8x name `",
2346 (UInt32)reltab_j->Type,
2347 reltab_j->VirtualAddress );
2348 printName ( sym->Name, strtab );
2349 debugBelch("'\n" ));
2351 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
2352 COFF_section* section_sym
2353 = findPEi386SectionCalled ( oc, sym->Name );
2355 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
2358 S = ((UInt32)(oc->image))
2359 + (section_sym->PointerToRawData
2362 copyName ( sym->Name, strtab, symbol, 1000-1 );
2363 (void*)S = lookupLocalSymbol( oc, symbol );
2364 if ((void*)S != NULL) goto foundit;
2365 (void*)S = lookupSymbol( symbol );
2366 if ((void*)S != NULL) goto foundit;
2367 zapTrailingAtSign ( symbol );
2368 (void*)S = lookupLocalSymbol( oc, symbol );
2369 if ((void*)S != NULL) goto foundit;
2370 (void*)S = lookupSymbol( symbol );
2371 if ((void*)S != NULL) goto foundit;
2372 /* Newline first because the interactive linker has printed "linking..." */
2373 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
2377 checkProddableBlock(oc, pP);
2378 switch (reltab_j->Type) {
2379 case MYIMAGE_REL_I386_DIR32:
2382 case MYIMAGE_REL_I386_REL32:
2383 /* Tricky. We have to insert a displacement at
2384 pP which, when added to the PC for the _next_
2385 insn, gives the address of the target (S).
2386 Problem is to know the address of the next insn
2387 when we only know pP. We assume that this
2388 literal field is always the last in the insn,
2389 so that the address of the next insn is pP+4
2390 -- hence the constant 4.
2391 Also I don't know if A should be added, but so
2392 far it has always been zero.
2394 SOF 05/2005: 'A' (old contents of *pP) have been observed
2395 to contain values other than zero (the 'wx' object file
2396 that came with wxhaskell-0.9.4; dunno how it was compiled..).
2397 So, add displacement to old value instead of asserting
2398 A to be zero. Fixes wxhaskell-related crashes, and no other
2399 ill effects have been observed.
2401 Update: the reason why we're seeing these more elaborate
2402 relocations is due to a switch in how the NCG compiles SRTs
2403 and offsets to them from info tables. SRTs live in .(ro)data,
2404 while info tables live in .text, causing GAS to emit REL32/DISP32
2405 relocations with non-zero values. Adding the displacement is
2406 the right thing to do.
2408 *pP = S - ((UInt32)pP) - 4 + A;
2411 debugBelch("%s: unhandled PEi386 relocation type %d",
2412 oc->fileName, reltab_j->Type);
2419 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
2423 #endif /* defined(OBJFORMAT_PEi386) */
2426 /* --------------------------------------------------------------------------
2428 * ------------------------------------------------------------------------*/
2430 #if defined(OBJFORMAT_ELF)
2435 #if defined(sparc_HOST_ARCH)
2436 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2437 #elif defined(i386_HOST_ARCH)
2438 # define ELF_TARGET_386 /* Used inside <elf.h> */
2439 #elif defined(x86_64_HOST_ARCH)
2440 # define ELF_TARGET_X64_64
2442 #elif defined (ia64_HOST_ARCH)
2443 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2445 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2446 # define ELF_NEED_GOT /* needs Global Offset Table */
2447 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2450 #if !defined(openbsd_HOST_OS)
2453 /* openbsd elf has things in different places, with diff names */
2454 #include <elf_abi.h>
2455 #include <machine/reloc.h>
2456 #define R_386_32 RELOC_32
2457 #define R_386_PC32 RELOC_PC32
2461 * Define a set of types which can be used for both ELF32 and ELF64
2465 #define ELFCLASS ELFCLASS64
2466 #define Elf_Addr Elf64_Addr
2467 #define Elf_Word Elf64_Word
2468 #define Elf_Sword Elf64_Sword
2469 #define Elf_Ehdr Elf64_Ehdr
2470 #define Elf_Phdr Elf64_Phdr
2471 #define Elf_Shdr Elf64_Shdr
2472 #define Elf_Sym Elf64_Sym
2473 #define Elf_Rel Elf64_Rel
2474 #define Elf_Rela Elf64_Rela
2475 #define ELF_ST_TYPE ELF64_ST_TYPE
2476 #define ELF_ST_BIND ELF64_ST_BIND
2477 #define ELF_R_TYPE ELF64_R_TYPE
2478 #define ELF_R_SYM ELF64_R_SYM
2480 #define ELFCLASS ELFCLASS32
2481 #define Elf_Addr Elf32_Addr
2482 #define Elf_Word Elf32_Word
2483 #define Elf_Sword Elf32_Sword
2484 #define Elf_Ehdr Elf32_Ehdr
2485 #define Elf_Phdr Elf32_Phdr
2486 #define Elf_Shdr Elf32_Shdr
2487 #define Elf_Sym Elf32_Sym
2488 #define Elf_Rel Elf32_Rel
2489 #define Elf_Rela Elf32_Rela
2491 #define ELF_ST_TYPE ELF32_ST_TYPE
2494 #define ELF_ST_BIND ELF32_ST_BIND
2497 #define ELF_R_TYPE ELF32_R_TYPE
2500 #define ELF_R_SYM ELF32_R_SYM
2506 * Functions to allocate entries in dynamic sections. Currently we simply
2507 * preallocate a large number, and we don't check if a entry for the given
2508 * target already exists (a linear search is too slow). Ideally these
2509 * entries would be associated with symbols.
2512 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2513 #define GOT_SIZE 0x20000
2514 #define FUNCTION_TABLE_SIZE 0x10000
2515 #define PLT_SIZE 0x08000
2518 static Elf_Addr got[GOT_SIZE];
2519 static unsigned int gotIndex;
2520 static Elf_Addr gp_val = (Elf_Addr)got;
2523 allocateGOTEntry(Elf_Addr target)
2527 if (gotIndex >= GOT_SIZE)
2528 barf("Global offset table overflow");
2530 entry = &got[gotIndex++];
2532 return (Elf_Addr)entry;
2536 #ifdef ELF_FUNCTION_DESC
2542 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2543 static unsigned int functionTableIndex;
2546 allocateFunctionDesc(Elf_Addr target)
2548 FunctionDesc *entry;
2550 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2551 barf("Function table overflow");
2553 entry = &functionTable[functionTableIndex++];
2555 entry->gp = (Elf_Addr)gp_val;
2556 return (Elf_Addr)entry;
2560 copyFunctionDesc(Elf_Addr target)
2562 FunctionDesc *olddesc = (FunctionDesc *)target;
2563 FunctionDesc *newdesc;
2565 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2566 newdesc->gp = olddesc->gp;
2567 return (Elf_Addr)newdesc;
2572 #ifdef ia64_HOST_ARCH
2573 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2574 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2576 static unsigned char plt_code[] =
2578 /* taken from binutils bfd/elfxx-ia64.c */
2579 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2580 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2581 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2582 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2583 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2584 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2587 /* If we can't get to the function descriptor via gp, take a local copy of it */
2588 #define PLT_RELOC(code, target) { \
2589 Elf64_Sxword rel_value = target - gp_val; \
2590 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2591 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2593 ia64_reloc_gprel22((Elf_Addr)code, target); \
2598 unsigned char code[sizeof(plt_code)];
2602 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2604 PLTEntry *plt = (PLTEntry *)oc->plt;
2607 if (oc->pltIndex >= PLT_SIZE)
2608 barf("Procedure table overflow");
2610 entry = &plt[oc->pltIndex++];
2611 memcpy(entry->code, plt_code, sizeof(entry->code));
2612 PLT_RELOC(entry->code, target);
2613 return (Elf_Addr)entry;
2619 return (PLT_SIZE * sizeof(PLTEntry));
2624 #if x86_64_HOST_ARCH
2625 // On x86_64, 32-bit relocations are often used, which requires that
2626 // we can resolve a symbol to a 32-bit offset. However, shared
2627 // libraries are placed outside the 2Gb area, which leaves us with a
2628 // problem when we need to give a 32-bit offset to a symbol in a
2631 // For a function symbol, we can allocate a bounce sequence inside the
2632 // 2Gb area and resolve the symbol to this. The bounce sequence is
2633 // simply a long jump instruction to the real location of the symbol.
2635 // For data references, we're screwed.
2638 unsigned char jmp[8]; /* 6 byte instruction: jmpq *0x00000002(%rip) */
2642 #define X86_64_BB_SIZE 1024
2644 static x86_64_bounce *x86_64_bounce_buffer = NULL;
2645 static nat x86_64_bb_next_off;
2648 x86_64_high_symbol( char *lbl, void *addr )
2650 x86_64_bounce *bounce;
2652 if ( x86_64_bounce_buffer == NULL ||
2653 x86_64_bb_next_off >= X86_64_BB_SIZE ) {
2654 x86_64_bounce_buffer =
2655 mmap(NULL, X86_64_BB_SIZE * sizeof(x86_64_bounce),
2656 PROT_EXEC|PROT_READ|PROT_WRITE,
2657 MAP_PRIVATE|MAP_32BIT|MAP_ANONYMOUS, -1, 0);
2658 if (x86_64_bounce_buffer == MAP_FAILED) {
2659 barf("x86_64_high_symbol: mmap failed");
2661 x86_64_bb_next_off = 0;
2663 bounce = &x86_64_bounce_buffer[x86_64_bb_next_off];
2664 bounce->jmp[0] = 0xff;
2665 bounce->jmp[1] = 0x25;
2666 bounce->jmp[2] = 0x02;
2667 bounce->jmp[3] = 0x00;
2668 bounce->jmp[4] = 0x00;
2669 bounce->jmp[5] = 0x00;
2670 bounce->addr = addr;
2671 x86_64_bb_next_off++;
2673 IF_DEBUG(linker, debugBelch("x86_64: allocated bounce entry for %s->%p at %p\n",
2674 lbl, addr, bounce));
2676 insertStrHashTable(symhash, lbl, bounce);
2683 * Generic ELF functions
2687 findElfSection ( void* objImage, Elf_Word sh_type )
2689 char* ehdrC = (char*)objImage;
2690 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2691 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2692 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2696 for (i = 0; i < ehdr->e_shnum; i++) {
2697 if (shdr[i].sh_type == sh_type
2698 /* Ignore the section header's string table. */
2699 && i != ehdr->e_shstrndx
2700 /* Ignore string tables named .stabstr, as they contain
2702 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2704 ptr = ehdrC + shdr[i].sh_offset;
2711 #if defined(ia64_HOST_ARCH)
2713 findElfSegment ( void* objImage, Elf_Addr vaddr )
2715 char* ehdrC = (char*)objImage;
2716 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2717 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2718 Elf_Addr segaddr = 0;
2721 for (i = 0; i < ehdr->e_phnum; i++) {
2722 segaddr = phdr[i].p_vaddr;
2723 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2731 ocVerifyImage_ELF ( ObjectCode* oc )
2735 int i, j, nent, nstrtab, nsymtabs;
2739 char* ehdrC = (char*)(oc->image);
2740 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2742 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2743 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2744 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2745 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2746 errorBelch("%s: not an ELF object", oc->fileName);
2750 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2751 errorBelch("%s: unsupported ELF format", oc->fileName);
2755 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2756 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
2758 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2759 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
2761 errorBelch("%s: unknown endiannness", oc->fileName);
2765 if (ehdr->e_type != ET_REL) {
2766 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
2769 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
2771 IF_DEBUG(linker,debugBelch( "Architecture is " ));
2772 switch (ehdr->e_machine) {
2773 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
2774 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
2776 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
2778 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
2780 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
2782 default: IF_DEBUG(linker,debugBelch( "unknown" ));
2783 errorBelch("%s: unknown architecture", oc->fileName);
2787 IF_DEBUG(linker,debugBelch(
2788 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
2789 (long)ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2791 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2793 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2795 if (ehdr->e_shstrndx == SHN_UNDEF) {
2796 errorBelch("%s: no section header string table", oc->fileName);
2799 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
2801 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2804 for (i = 0; i < ehdr->e_shnum; i++) {
2805 IF_DEBUG(linker,debugBelch("%2d: ", i ));
2806 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
2807 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
2808 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
2809 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
2810 ehdrC + shdr[i].sh_offset,
2811 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2813 if (shdr[i].sh_type == SHT_REL) {
2814 IF_DEBUG(linker,debugBelch("Rel " ));
2815 } else if (shdr[i].sh_type == SHT_RELA) {
2816 IF_DEBUG(linker,debugBelch("RelA " ));
2818 IF_DEBUG(linker,debugBelch(" "));
2821 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
2825 IF_DEBUG(linker,debugBelch( "\nString tables" ));
2828 for (i = 0; i < ehdr->e_shnum; i++) {
2829 if (shdr[i].sh_type == SHT_STRTAB
2830 /* Ignore the section header's string table. */
2831 && i != ehdr->e_shstrndx
2832 /* Ignore string tables named .stabstr, as they contain
2834 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2836 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
2837 strtab = ehdrC + shdr[i].sh_offset;
2842 errorBelch("%s: no string tables, or too many", oc->fileName);
2847 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
2848 for (i = 0; i < ehdr->e_shnum; i++) {
2849 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2850 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
2852 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2853 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2854 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
2856 (long)shdr[i].sh_size % sizeof(Elf_Sym)
2858 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2859 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
2862 for (j = 0; j < nent; j++) {
2863 IF_DEBUG(linker,debugBelch(" %2d ", j ));
2864 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
2865 (int)stab[j].st_shndx,
2866 (int)stab[j].st_size,
2867 (char*)stab[j].st_value ));
2869 IF_DEBUG(linker,debugBelch("type=" ));
2870 switch (ELF_ST_TYPE(stab[j].st_info)) {
2871 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
2872 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
2873 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
2874 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
2875 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
2876 default: IF_DEBUG(linker,debugBelch("? " )); break;
2878 IF_DEBUG(linker,debugBelch(" " ));
2880 IF_DEBUG(linker,debugBelch("bind=" ));
2881 switch (ELF_ST_BIND(stab[j].st_info)) {
2882 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
2883 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
2884 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
2885 default: IF_DEBUG(linker,debugBelch("? " )); break;
2887 IF_DEBUG(linker,debugBelch(" " ));
2889 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
2893 if (nsymtabs == 0) {
2894 errorBelch("%s: didn't find any symbol tables", oc->fileName);
2901 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
2905 if (hdr->sh_type == SHT_PROGBITS
2906 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
2907 /* .text-style section */
2908 return SECTIONKIND_CODE_OR_RODATA;
2911 if (hdr->sh_type == SHT_PROGBITS
2912 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
2913 /* .data-style section */
2914 return SECTIONKIND_RWDATA;
2917 if (hdr->sh_type == SHT_PROGBITS
2918 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
2919 /* .rodata-style section */
2920 return SECTIONKIND_CODE_OR_RODATA;
2923 if (hdr->sh_type == SHT_NOBITS
2924 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
2925 /* .bss-style section */
2927 return SECTIONKIND_RWDATA;
2930 return SECTIONKIND_OTHER;
2935 ocGetNames_ELF ( ObjectCode* oc )
2940 char* ehdrC = (char*)(oc->image);
2941 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2942 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2943 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2945 ASSERT(symhash != NULL);
2948 errorBelch("%s: no strtab", oc->fileName);
2953 for (i = 0; i < ehdr->e_shnum; i++) {
2954 /* Figure out what kind of section it is. Logic derived from
2955 Figure 1.14 ("Special Sections") of the ELF document
2956 ("Portable Formats Specification, Version 1.1"). */
2958 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
2960 if (is_bss && shdr[i].sh_size > 0) {
2961 /* This is a non-empty .bss section. Allocate zeroed space for
2962 it, and set its .sh_offset field such that
2963 ehdrC + .sh_offset == addr_of_zeroed_space. */
2964 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2965 "ocGetNames_ELF(BSS)");
2966 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2968 debugBelch("BSS section at 0x%x, size %d\n",
2969 zspace, shdr[i].sh_size);
2973 /* fill in the section info */
2974 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2975 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2976 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2977 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2980 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2982 /* copy stuff into this module's object symbol table */
2983 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2984 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2986 oc->n_symbols = nent;
2987 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2988 "ocGetNames_ELF(oc->symbols)");
2990 for (j = 0; j < nent; j++) {
2992 char isLocal = FALSE; /* avoids uninit-var warning */
2994 char* nm = strtab + stab[j].st_name;
2995 int secno = stab[j].st_shndx;
2997 /* Figure out if we want to add it; if so, set ad to its
2998 address. Otherwise leave ad == NULL. */
3000 if (secno == SHN_COMMON) {
3002 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
3004 debugBelch("COMMON symbol, size %d name %s\n",
3005 stab[j].st_size, nm);
3007 /* Pointless to do addProddableBlock() for this area,
3008 since the linker should never poke around in it. */
3011 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
3012 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
3014 /* and not an undefined symbol */
3015 && stab[j].st_shndx != SHN_UNDEF
3016 /* and not in a "special section" */
3017 && stab[j].st_shndx < SHN_LORESERVE
3019 /* and it's a not a section or string table or anything silly */
3020 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
3021 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
3022 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
3025 /* Section 0 is the undefined section, hence > and not >=. */
3026 ASSERT(secno > 0 && secno < ehdr->e_shnum);
3028 if (shdr[secno].sh_type == SHT_NOBITS) {
3029 debugBelch(" BSS symbol, size %d off %d name %s\n",
3030 stab[j].st_size, stab[j].st_value, nm);
3033 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
3034 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
3037 #ifdef ELF_FUNCTION_DESC
3038 /* dlsym() and the initialisation table both give us function
3039 * descriptors, so to be consistent we store function descriptors
3040 * in the symbol table */
3041 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
3042 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
3044 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s",
3045 ad, oc->fileName, nm ));
3050 /* And the decision is ... */
3054 oc->symbols[j] = nm;
3057 /* Ignore entirely. */
3059 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3063 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3064 strtab + stab[j].st_name ));
3067 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
3068 (int)ELF_ST_BIND(stab[j].st_info),
3069 (int)ELF_ST_TYPE(stab[j].st_info),
3070 (int)stab[j].st_shndx,
3071 strtab + stab[j].st_name
3074 oc->symbols[j] = NULL;
3083 /* Do ELF relocations which lack an explicit addend. All x86-linux
3084 relocations appear to be of this form. */
3086 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3087 Elf_Shdr* shdr, int shnum,
3088 Elf_Sym* stab, char* strtab )
3093 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3094 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3095 int target_shndx = shdr[shnum].sh_info;
3096 int symtab_shndx = shdr[shnum].sh_link;
3098 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3099 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3100 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3101 target_shndx, symtab_shndx ));
3103 /* Skip sections that we're not interested in. */
3106 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3107 if (kind == SECTIONKIND_OTHER) {
3108 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
3113 for (j = 0; j < nent; j++) {
3114 Elf_Addr offset = rtab[j].r_offset;
3115 Elf_Addr info = rtab[j].r_info;
3117 Elf_Addr P = ((Elf_Addr)targ) + offset;
3118 Elf_Word* pP = (Elf_Word*)P;
3124 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
3125 j, (void*)offset, (void*)info ));
3127 IF_DEBUG(linker,debugBelch( " ZERO" ));
3130 Elf_Sym sym = stab[ELF_R_SYM(info)];
3131 /* First see if it is a local symbol. */
3132 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3133 /* Yes, so we can get the address directly from the ELF symbol
3135 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3137 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3138 + stab[ELF_R_SYM(info)].st_value);
3141 /* No, so look up the name in our global table. */
3142 symbol = strtab + sym.st_name;
3143 S_tmp = lookupSymbol( symbol );
3144 S = (Elf_Addr)S_tmp;
3147 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3150 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
3153 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
3154 (void*)P, (void*)S, (void*)A ));
3155 checkProddableBlock ( oc, pP );
3159 switch (ELF_R_TYPE(info)) {
3160 # ifdef i386_HOST_ARCH
3161 case R_386_32: *pP = value; break;
3162 case R_386_PC32: *pP = value - P; break;
3165 errorBelch("%s: unhandled ELF relocation(Rel) type %lu\n",
3166 oc->fileName, (lnat)ELF_R_TYPE(info));
3174 /* Do ELF relocations for which explicit addends are supplied.
3175 sparc-solaris relocations appear to be of this form. */
3177 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
3178 Elf_Shdr* shdr, int shnum,
3179 Elf_Sym* stab, char* strtab )
3182 char *symbol = NULL;
3184 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
3185 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
3186 int target_shndx = shdr[shnum].sh_info;
3187 int symtab_shndx = shdr[shnum].sh_link;
3189 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3190 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
3191 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3192 target_shndx, symtab_shndx ));
3194 for (j = 0; j < nent; j++) {
3195 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
3196 /* This #ifdef only serves to avoid unused-var warnings. */
3197 Elf_Addr offset = rtab[j].r_offset;
3198 Elf_Addr P = targ + offset;
3200 Elf_Addr info = rtab[j].r_info;
3201 Elf_Addr A = rtab[j].r_addend;
3205 # if defined(sparc_HOST_ARCH)
3206 Elf_Word* pP = (Elf_Word*)P;
3208 # elif defined(ia64_HOST_ARCH)
3209 Elf64_Xword *pP = (Elf64_Xword *)P;
3211 # elif defined(powerpc_HOST_ARCH)
3215 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
3216 j, (void*)offset, (void*)info,
3219 IF_DEBUG(linker,debugBelch( " ZERO" ));
3222 Elf_Sym sym = stab[ELF_R_SYM(info)];
3223 /* First see if it is a local symbol. */
3224 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3225 /* Yes, so we can get the address directly from the ELF symbol
3227 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3229 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3230 + stab[ELF_R_SYM(info)].st_value);
3231 #ifdef ELF_FUNCTION_DESC
3232 /* Make a function descriptor for this function */
3233 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
3234 S = allocateFunctionDesc(S + A);
3239 /* No, so look up the name in our global table. */
3240 symbol = strtab + sym.st_name;
3241 S_tmp = lookupSymbol( symbol );
3242 S = (Elf_Addr)S_tmp;
3244 #ifdef ELF_FUNCTION_DESC
3245 /* If a function, already a function descriptor - we would
3246 have to copy it to add an offset. */
3247 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
3248 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
3252 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3255 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
3258 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
3259 (void*)P, (void*)S, (void*)A ));
3260 /* checkProddableBlock ( oc, (void*)P ); */
3264 switch (ELF_R_TYPE(info)) {
3265 # if defined(sparc_HOST_ARCH)
3266 case R_SPARC_WDISP30:
3267 w1 = *pP & 0xC0000000;
3268 w2 = (Elf_Word)((value - P) >> 2);
3269 ASSERT((w2 & 0xC0000000) == 0);
3274 w1 = *pP & 0xFFC00000;
3275 w2 = (Elf_Word)(value >> 10);
3276 ASSERT((w2 & 0xFFC00000) == 0);
3282 w2 = (Elf_Word)(value & 0x3FF);
3283 ASSERT((w2 & ~0x3FF) == 0);
3287 /* According to the Sun documentation:
3289 This relocation type resembles R_SPARC_32, except it refers to an
3290 unaligned word. That is, the word to be relocated must be treated
3291 as four separate bytes with arbitrary alignment, not as a word
3292 aligned according to the architecture requirements.
3294 (JRS: which means that freeloading on the R_SPARC_32 case
3295 is probably wrong, but hey ...)
3299 w2 = (Elf_Word)value;
3302 # elif defined(ia64_HOST_ARCH)
3303 case R_IA64_DIR64LSB:
3304 case R_IA64_FPTR64LSB:
3307 case R_IA64_PCREL64LSB:
3310 case R_IA64_SEGREL64LSB:
3311 addr = findElfSegment(ehdrC, value);
3314 case R_IA64_GPREL22:
3315 ia64_reloc_gprel22(P, value);
3317 case R_IA64_LTOFF22:
3318 case R_IA64_LTOFF22X:
3319 case R_IA64_LTOFF_FPTR22:
3320 addr = allocateGOTEntry(value);
3321 ia64_reloc_gprel22(P, addr);
3323 case R_IA64_PCREL21B:
3324 ia64_reloc_pcrel21(P, S, oc);
3327 /* This goes with R_IA64_LTOFF22X and points to the load to
3328 * convert into a move. We don't implement relaxation. */
3330 # elif defined(powerpc_HOST_ARCH)
3331 case R_PPC_ADDR16_LO:
3332 *(Elf32_Half*) P = value;
3335 case R_PPC_ADDR16_HI:
3336 *(Elf32_Half*) P = value >> 16;
3339 case R_PPC_ADDR16_HA:
3340 *(Elf32_Half*) P = (value + 0x8000) >> 16;
3344 *(Elf32_Word *) P = value;
3348 *(Elf32_Word *) P = value - P;
3354 if( delta << 6 >> 6 != delta )
3356 value = makeJumpIsland( oc, ELF_R_SYM(info), value );
3359 if( value == 0 || delta << 6 >> 6 != delta )
3361 barf( "Unable to make ppcJumpIsland for #%d",
3367 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
3368 | (delta & 0x3fffffc);
3372 #if x86_64_HOST_ARCH
3374 *(Elf64_Xword *)P = value;
3379 StgInt64 off = value - P;
3380 if (off >= 0x7fffffffL || off < -0x80000000L) {
3381 barf("R_X86_64_PC32 relocation out of range: %s = %p",
3384 *(Elf64_Word *)P = (Elf64_Word)off;
3389 if (value >= 0x7fffffffL) {
3390 barf("R_X86_64_32 relocation out of range: %s = %p\n",
3393 *(Elf64_Word *)P = (Elf64_Word)value;
3397 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
3398 barf("R_X86_64_32S relocation out of range: %s = %p\n",
3401 *(Elf64_Sword *)P = (Elf64_Sword)value;
3406 errorBelch("%s: unhandled ELF relocation(RelA) type %lu\n",
3407 oc->fileName, (lnat)ELF_R_TYPE(info));
3416 ocResolve_ELF ( ObjectCode* oc )
3420 Elf_Sym* stab = NULL;
3421 char* ehdrC = (char*)(oc->image);
3422 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
3423 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3425 /* first find "the" symbol table */
3426 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
3428 /* also go find the string table */
3429 strtab = findElfSection ( ehdrC, SHT_STRTAB );
3431 if (stab == NULL || strtab == NULL) {
3432 errorBelch("%s: can't find string or symbol table", oc->fileName);
3436 /* Process the relocation sections. */
3437 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
3438 if (shdr[shnum].sh_type == SHT_REL) {
3439 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
3440 shnum, stab, strtab );
3444 if (shdr[shnum].sh_type == SHT_RELA) {
3445 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
3446 shnum, stab, strtab );
3451 /* Free the local symbol table; we won't need it again. */
3452 freeHashTable(oc->lochash, NULL);
3455 #if defined(powerpc_HOST_ARCH)
3456 ocFlushInstructionCache( oc );
3464 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
3465 * at the front. The following utility functions pack and unpack instructions, and
3466 * take care of the most common relocations.
3469 #ifdef ia64_HOST_ARCH
3472 ia64_extract_instruction(Elf64_Xword *target)
3475 int slot = (Elf_Addr)target & 3;
3476 target = (Elf_Addr)target & ~3;
3484 return ((w1 >> 5) & 0x1ffffffffff);
3486 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
3490 barf("ia64_extract_instruction: invalid slot %p", target);
3495 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
3497 int slot = (Elf_Addr)target & 3;
3498 target = (Elf_Addr)target & ~3;
3503 *target |= value << 5;
3506 *target |= value << 46;
3507 *(target+1) |= value >> 18;
3510 *(target+1) |= value << 23;
3516 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
3518 Elf64_Xword instruction;
3519 Elf64_Sxword rel_value;
3521 rel_value = value - gp_val;
3522 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
3523 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
3525 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3526 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
3527 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
3528 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
3529 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3530 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3534 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
3536 Elf64_Xword instruction;
3537 Elf64_Sxword rel_value;
3540 entry = allocatePLTEntry(value, oc);
3542 rel_value = (entry >> 4) - (target >> 4);
3543 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
3544 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
3546 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3547 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
3548 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3549 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3555 * PowerPC ELF specifics
3558 #ifdef powerpc_HOST_ARCH
3560 static int ocAllocateJumpIslands_ELF( ObjectCode *oc )
3566 ehdr = (Elf_Ehdr *) oc->image;
3567 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
3569 for( i = 0; i < ehdr->e_shnum; i++ )
3570 if( shdr[i].sh_type == SHT_SYMTAB )
3573 if( i == ehdr->e_shnum )
3575 errorBelch( "This ELF file contains no symtab" );
3579 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
3581 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
3582 shdr[i].sh_entsize, sizeof( Elf_Sym ) );
3587 return ocAllocateJumpIslands( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
3590 #endif /* powerpc */
3594 /* --------------------------------------------------------------------------
3596 * ------------------------------------------------------------------------*/
3598 #if defined(OBJFORMAT_MACHO)
3601 Support for MachO linking on Darwin/MacOS X
3602 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3604 I hereby formally apologize for the hackish nature of this code.
3605 Things that need to be done:
3606 *) implement ocVerifyImage_MachO
3607 *) add still more sanity checks.
3610 #ifdef powerpc_HOST_ARCH
3611 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3613 struct mach_header *header = (struct mach_header *) oc->image;
3614 struct load_command *lc = (struct load_command *) (header + 1);
3617 for( i = 0; i < header->ncmds; i++ )
3619 if( lc->cmd == LC_SYMTAB )
3621 // Find out the first and last undefined external
3622 // symbol, so we don't have to allocate too many
3624 struct symtab_command *symLC = (struct symtab_command *) lc;
3625 unsigned min = symLC->nsyms, max = 0;
3626 struct nlist *nlist =
3627 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
3629 for(i=0;i<symLC->nsyms;i++)
3631 if(nlist[i].n_type & N_STAB)
3633 else if(nlist[i].n_type & N_EXT)
3635 if((nlist[i].n_type & N_TYPE) == N_UNDF
3636 && (nlist[i].n_value == 0))
3646 return ocAllocateJumpIslands(oc, max - min + 1, min);
3651 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3653 return ocAllocateJumpIslands(oc,0,0);
3657 static int ocVerifyImage_MachO(ObjectCode* oc STG_UNUSED)
3659 // FIXME: do some verifying here
3663 static int resolveImports(
3666 struct symtab_command *symLC,
3667 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3668 unsigned long *indirectSyms,
3669 struct nlist *nlist)
3673 for(i=0;i*4<sect->size;i++)
3675 // according to otool, reserved1 contains the first index into the indirect symbol table
3676 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3677 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3680 if((symbol->n_type & N_TYPE) == N_UNDF
3681 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3682 addr = (void*) (symbol->n_value);
3683 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3686 addr = lookupSymbol(nm);
3689 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3693 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3694 ((void**)(image + sect->offset))[i] = addr;
3700 static unsigned long relocateAddress(
3703 struct section* sections,
3704 unsigned long address)
3707 for(i = 0; i < nSections; i++)
3709 if(sections[i].addr <= address
3710 && address < sections[i].addr + sections[i].size)
3712 return (unsigned long)oc->image
3713 + sections[i].offset + address - sections[i].addr;
3716 barf("Invalid Mach-O file:"
3717 "Address out of bounds while relocating object file");
3721 static int relocateSection(
3724 struct symtab_command *symLC, struct nlist *nlist,
3725 int nSections, struct section* sections, struct section *sect)
3727 struct relocation_info *relocs;
3730 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3732 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3734 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
3736 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
3740 relocs = (struct relocation_info*) (image + sect->reloff);
3744 if(relocs[i].r_address & R_SCATTERED)
3746 struct scattered_relocation_info *scat =
3747 (struct scattered_relocation_info*) &relocs[i];
3751 if(scat->r_length == 2)
3753 unsigned long word = 0;
3754 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3755 checkProddableBlock(oc,wordPtr);
3757 // Note on relocation types:
3758 // i386 uses the GENERIC_RELOC_* types,
3759 // while ppc uses special PPC_RELOC_* types.
3760 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
3761 // in both cases, all others are different.
3762 // Therefore, we use GENERIC_RELOC_VANILLA
3763 // and GENERIC_RELOC_PAIR instead of the PPC variants,
3764 // and use #ifdefs for the other types.
3766 // Step 1: Figure out what the relocated value should be
3767 if(scat->r_type == GENERIC_RELOC_VANILLA)
3769 word = *wordPtr + (unsigned long) relocateAddress(
3776 #ifdef powerpc_HOST_ARCH
3777 else if(scat->r_type == PPC_RELOC_SECTDIFF
3778 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3779 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3780 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3782 else if(scat->r_type == GENERIC_RELOC_SECTDIFF)
3785 struct scattered_relocation_info *pair =
3786 (struct scattered_relocation_info*) &relocs[i+1];
3788 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
3789 barf("Invalid Mach-O file: "
3790 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
3792 word = (unsigned long)
3793 (relocateAddress(oc, nSections, sections, scat->r_value)
3794 - relocateAddress(oc, nSections, sections, pair->r_value));
3797 #ifdef powerpc_HOST_ARCH
3798 else if(scat->r_type == PPC_RELOC_HI16
3799 || scat->r_type == PPC_RELOC_LO16
3800 || scat->r_type == PPC_RELOC_HA16
3801 || scat->r_type == PPC_RELOC_LO14)
3802 { // these are generated by label+offset things
3803 struct relocation_info *pair = &relocs[i+1];
3804 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
3805 barf("Invalid Mach-O file: "
3806 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
3808 if(scat->r_type == PPC_RELOC_LO16)
3810 word = ((unsigned short*) wordPtr)[1];
3811 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3813 else if(scat->r_type == PPC_RELOC_LO14)
3815 barf("Unsupported Relocation: PPC_RELOC_LO14");
3816 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
3817 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3819 else if(scat->r_type == PPC_RELOC_HI16)
3821 word = ((unsigned short*) wordPtr)[1] << 16;
3822 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3824 else if(scat->r_type == PPC_RELOC_HA16)
3826 word = ((unsigned short*) wordPtr)[1] << 16;
3827 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3831 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
3838 continue; // ignore the others
3840 #ifdef powerpc_HOST_ARCH
3841 if(scat->r_type == GENERIC_RELOC_VANILLA
3842 || scat->r_type == PPC_RELOC_SECTDIFF)
3844 if(scat->r_type == GENERIC_RELOC_VANILLA
3845 || scat->r_type == GENERIC_RELOC_SECTDIFF)
3850 #ifdef powerpc_HOST_ARCH
3851 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
3853 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3855 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
3857 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3859 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
3861 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3862 + ((word & (1<<15)) ? 1 : 0);
3868 continue; // FIXME: I hope it's OK to ignore all the others.
3872 struct relocation_info *reloc = &relocs[i];
3873 if(reloc->r_pcrel && !reloc->r_extern)
3876 if(reloc->r_length == 2)
3878 unsigned long word = 0;
3879 #ifdef powerpc_HOST_ARCH
3880 unsigned long jumpIsland = 0;
3881 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
3882 // to avoid warning and to catch
3886 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3887 checkProddableBlock(oc,wordPtr);
3889 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3893 #ifdef powerpc_HOST_ARCH
3894 else if(reloc->r_type == PPC_RELOC_LO16)
3896 word = ((unsigned short*) wordPtr)[1];
3897 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3899 else if(reloc->r_type == PPC_RELOC_HI16)
3901 word = ((unsigned short*) wordPtr)[1] << 16;
3902 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3904 else if(reloc->r_type == PPC_RELOC_HA16)
3906 word = ((unsigned short*) wordPtr)[1] << 16;
3907 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3909 else if(reloc->r_type == PPC_RELOC_BR24)
3912 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
3916 if(!reloc->r_extern)
3919 sections[reloc->r_symbolnum-1].offset
3920 - sections[reloc->r_symbolnum-1].addr
3927 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3928 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3929 void *symbolAddress = lookupSymbol(nm);
3932 errorBelch("\nunknown symbol `%s'", nm);
3938 #ifdef powerpc_HOST_ARCH
3939 // In the .o file, this should be a relative jump to NULL
3940 // and we'll change it to a relative jump to the symbol
3941 ASSERT(-word == reloc->r_address);
3942 jumpIsland = makeJumpIsland(oc,reloc->r_symbolnum,(unsigned long) symbolAddress);
3945 offsetToJumpIsland = word + jumpIsland
3946 - (((long)image) + sect->offset - sect->addr);
3949 word += (unsigned long) symbolAddress
3950 - (((long)image) + sect->offset - sect->addr);
3954 word += (unsigned long) symbolAddress;
3958 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3963 #ifdef powerpc_HOST_ARCH
3964 else if(reloc->r_type == PPC_RELOC_LO16)
3966 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3969 else if(reloc->r_type == PPC_RELOC_HI16)
3971 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3974 else if(reloc->r_type == PPC_RELOC_HA16)
3976 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3977 + ((word & (1<<15)) ? 1 : 0);
3980 else if(reloc->r_type == PPC_RELOC_BR24)
3982 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3984 // The branch offset is too large.
3985 // Therefore, we try to use a jump island.
3988 barf("unconditional relative branch out of range: "
3989 "no jump island available");
3992 word = offsetToJumpIsland;
3993 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3994 barf("unconditional relative branch out of range: "
3995 "jump island out of range");
3997 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
4002 barf("\nunknown relocation %d",reloc->r_type);
4009 static int ocGetNames_MachO(ObjectCode* oc)
4011 char *image = (char*) oc->image;
4012 struct mach_header *header = (struct mach_header*) image;
4013 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
4014 unsigned i,curSymbol = 0;
4015 struct segment_command *segLC = NULL;
4016 struct section *sections;
4017 struct symtab_command *symLC = NULL;
4018 struct nlist *nlist;
4019 unsigned long commonSize = 0;
4020 char *commonStorage = NULL;
4021 unsigned long commonCounter;
4023 for(i=0;i<header->ncmds;i++)
4025 if(lc->cmd == LC_SEGMENT)
4026 segLC = (struct segment_command*) lc;
4027 else if(lc->cmd == LC_SYMTAB)
4028 symLC = (struct symtab_command*) lc;
4029 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4032 sections = (struct section*) (segLC+1);
4033 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4036 for(i=0;i<segLC->nsects;i++)
4038 if(sections[i].size == 0)
4041 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
4043 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
4044 "ocGetNames_MachO(common symbols)");
4045 sections[i].offset = zeroFillArea - image;
4048 if(!strcmp(sections[i].sectname,"__text"))
4049 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
4050 (void*) (image + sections[i].offset),
4051 (void*) (image + sections[i].offset + sections[i].size));
4052 else if(!strcmp(sections[i].sectname,"__const"))
4053 addSection(oc, SECTIONKIND_RWDATA,
4054 (void*) (image + sections[i].offset),
4055 (void*) (image + sections[i].offset + sections[i].size));
4056 else if(!strcmp(sections[i].sectname,"__data"))
4057 addSection(oc, SECTIONKIND_RWDATA,
4058 (void*) (image + sections[i].offset),
4059 (void*) (image + sections[i].offset + sections[i].size));
4060 else if(!strcmp(sections[i].sectname,"__bss")
4061 || !strcmp(sections[i].sectname,"__common"))
4062 addSection(oc, SECTIONKIND_RWDATA,
4063 (void*) (image + sections[i].offset),
4064 (void*) (image + sections[i].offset + sections[i].size));
4066 addProddableBlock(oc, (void*) (image + sections[i].offset),
4070 // count external symbols defined here
4074 for(i=0;i<symLC->nsyms;i++)
4076 if(nlist[i].n_type & N_STAB)
4078 else if(nlist[i].n_type & N_EXT)
4080 if((nlist[i].n_type & N_TYPE) == N_UNDF
4081 && (nlist[i].n_value != 0))
4083 commonSize += nlist[i].n_value;
4086 else if((nlist[i].n_type & N_TYPE) == N_SECT)
4091 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
4092 "ocGetNames_MachO(oc->symbols)");
4096 for(i=0;i<symLC->nsyms;i++)
4098 if(nlist[i].n_type & N_STAB)
4100 else if((nlist[i].n_type & N_TYPE) == N_SECT)
4102 if(nlist[i].n_type & N_EXT)
4104 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4105 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4107 + sections[nlist[i].n_sect-1].offset
4108 - sections[nlist[i].n_sect-1].addr
4109 + nlist[i].n_value);
4110 oc->symbols[curSymbol++] = nm;
4114 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4115 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm,
4117 + sections[nlist[i].n_sect-1].offset
4118 - sections[nlist[i].n_sect-1].addr
4119 + nlist[i].n_value);
4125 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
4126 commonCounter = (unsigned long)commonStorage;
4129 for(i=0;i<symLC->nsyms;i++)
4131 if((nlist[i].n_type & N_TYPE) == N_UNDF
4132 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
4134 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4135 unsigned long sz = nlist[i].n_value;
4137 nlist[i].n_value = commonCounter;
4139 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4140 (void*)commonCounter);
4141 oc->symbols[curSymbol++] = nm;
4143 commonCounter += sz;
4150 static int ocResolve_MachO(ObjectCode* oc)
4152 char *image = (char*) oc->image;
4153 struct mach_header *header = (struct mach_header*) image;
4154 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
4156 struct segment_command *segLC = NULL;
4157 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
4158 struct symtab_command *symLC = NULL;
4159 struct dysymtab_command *dsymLC = NULL;
4160 struct nlist *nlist;
4162 for(i=0;i<header->ncmds;i++)
4164 if(lc->cmd == LC_SEGMENT)
4165 segLC = (struct segment_command*) lc;
4166 else if(lc->cmd == LC_SYMTAB)
4167 symLC = (struct symtab_command*) lc;
4168 else if(lc->cmd == LC_DYSYMTAB)
4169 dsymLC = (struct dysymtab_command*) lc;
4170 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4173 sections = (struct section*) (segLC+1);
4174 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4177 for(i=0;i<segLC->nsects;i++)
4179 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
4180 la_ptrs = §ions[i];
4181 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
4182 nl_ptrs = §ions[i];
4183 else if(!strcmp(sections[i].sectname,"__la_sym_ptr2"))
4184 la_ptrs = §ions[i];
4185 else if(!strcmp(sections[i].sectname,"__la_sym_ptr3"))
4186 la_ptrs = §ions[i];
4191 unsigned long *indirectSyms
4192 = (unsigned long*) (image + dsymLC->indirectsymoff);
4195 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
4198 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
4202 for(i=0;i<segLC->nsects;i++)
4204 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
4208 /* Free the local symbol table; we won't need it again. */
4209 freeHashTable(oc->lochash, NULL);
4212 #if defined (powerpc_HOST_ARCH)
4213 ocFlushInstructionCache( oc );
4219 #ifdef powerpc_HOST_ARCH
4221 * The Mach-O object format uses leading underscores. But not everywhere.
4222 * There is a small number of runtime support functions defined in
4223 * libcc_dynamic.a whose name does not have a leading underscore.
4224 * As a consequence, we can't get their address from C code.
4225 * We have to use inline assembler just to take the address of a function.
4229 static void machoInitSymbolsWithoutUnderscore()
4231 extern void* symbolsWithoutUnderscore[];
4232 void **p = symbolsWithoutUnderscore;
4233 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
4237 __asm__ volatile(".long " # x);
4239 RTS_MACHO_NOUNDERLINE_SYMBOLS
4241 __asm__ volatile(".text");
4245 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
4247 RTS_MACHO_NOUNDERLINE_SYMBOLS
4254 * Figure out by how much to shift the entire Mach-O file in memory
4255 * when loading so that its single segment ends up 16-byte-aligned
4257 static int machoGetMisalignment( FILE * f )
4259 struct mach_header header;
4262 fread(&header, sizeof(header), 1, f);
4265 if(header.magic != MH_MAGIC)
4268 misalignment = (header.sizeofcmds + sizeof(header))
4271 return misalignment ? (16 - misalignment) : 0;