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>.
23 #include "LinkerInternals.h"
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
35 #ifdef HAVE_SYS_STAT_H
39 #if defined(HAVE_FRAMEWORK_HASKELLSUPPORT)
40 #include <HaskellSupport/dlfcn.h>
41 #elif defined(HAVE_DLFCN_H)
45 #if defined(cygwin32_TARGET_OS)
50 #ifdef HAVE_SYS_TIME_H
54 #include <sys/fcntl.h>
55 #include <sys/termios.h>
56 #include <sys/utime.h>
57 #include <sys/utsname.h>
61 #if defined(ia64_TARGET_ARCH) || defined(openbsd_TARGET_OS)
66 #if defined(openbsd_TARGET_OS)
74 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS) || defined(netbsd_TARGET_OS) || defined(openbsd_TARGET_OS)
75 # define OBJFORMAT_ELF
76 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
77 # define OBJFORMAT_PEi386
80 #elif defined(darwin_TARGET_OS)
81 # include <mach-o/ppc/reloc.h>
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>
89 /* Hash table mapping symbol names to Symbol */
90 static /*Str*/HashTable *symhash;
92 /* List of currently loaded objects */
93 ObjectCode *objects = NULL; /* initially empty */
95 #if defined(OBJFORMAT_ELF)
96 static int ocVerifyImage_ELF ( ObjectCode* oc );
97 static int ocGetNames_ELF ( ObjectCode* oc );
98 static int ocResolve_ELF ( ObjectCode* oc );
99 #if defined(powerpc_TARGET_ARCH)
100 static int ocAllocateJumpIslands_ELF ( ObjectCode* oc );
102 #elif defined(OBJFORMAT_PEi386)
103 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
104 static int ocGetNames_PEi386 ( ObjectCode* oc );
105 static int ocResolve_PEi386 ( ObjectCode* oc );
106 #elif defined(OBJFORMAT_MACHO)
107 static int ocAllocateJumpIslands_MachO ( ObjectCode* oc );
108 static int ocVerifyImage_MachO ( ObjectCode* oc );
109 static int ocGetNames_MachO ( ObjectCode* oc );
110 static int ocResolve_MachO ( ObjectCode* oc );
112 static void machoInitSymbolsWithoutUnderscore( void );
115 /* -----------------------------------------------------------------------------
116 * Built-in symbols from the RTS
119 typedef struct _RtsSymbolVal {
126 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
128 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
129 SymX(makeStableNamezh_fast) \
130 SymX(finalizzeWeakzh_fast)
132 /* These are not available in GUM!!! -- HWL */
133 #define Maybe_ForeignObj
134 #define Maybe_Stable_Names
137 #if !defined (mingw32_TARGET_OS)
138 #define RTS_POSIX_ONLY_SYMBOLS \
139 SymX(stg_sig_install) \
143 #if defined (cygwin32_TARGET_OS)
144 #define RTS_MINGW_ONLY_SYMBOLS /**/
145 /* Don't have the ability to read import libs / archives, so
146 * we have to stupidly list a lot of what libcygwin.a
149 #define RTS_CYGWIN_ONLY_SYMBOLS \
227 #elif !defined(mingw32_TARGET_OS)
228 #define RTS_MINGW_ONLY_SYMBOLS /**/
229 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
230 #else /* defined(mingw32_TARGET_OS) */
231 #define RTS_POSIX_ONLY_SYMBOLS /**/
232 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
234 /* Extra syms gen'ed by mingw-2's gcc-3.2: */
236 #define RTS_MINGW_EXTRA_SYMS \
237 Sym(_imp____mb_cur_max) \
240 #define RTS_MINGW_EXTRA_SYMS
243 /* These are statically linked from the mingw libraries into the ghc
244 executable, so we have to employ this hack. */
245 #define RTS_MINGW_ONLY_SYMBOLS \
246 SymX(asyncReadzh_fast) \
247 SymX(asyncWritezh_fast) \
248 SymX(asyncDoProczh_fast) \
260 SymX(getservbyname) \
261 SymX(getservbyport) \
262 SymX(getprotobynumber) \
263 SymX(getprotobyname) \
264 SymX(gethostbyname) \
265 SymX(gethostbyaddr) \
300 Sym(_imp___timezone) \
308 RTS_MINGW_EXTRA_SYMS \
313 # define MAIN_CAP_SYM SymX(MainCapability)
315 # define MAIN_CAP_SYM
318 #ifdef TABLES_NEXT_TO_CODE
319 #define RTS_RET_SYMBOLS /* nothing */
321 #define RTS_RET_SYMBOLS \
322 SymX(stg_enter_ret) \
323 SymX(stg_gc_fun_ret) \
331 SymX(stg_ap_pv_ret) \
332 SymX(stg_ap_pp_ret) \
333 SymX(stg_ap_ppv_ret) \
334 SymX(stg_ap_ppp_ret) \
335 SymX(stg_ap_pppv_ret) \
336 SymX(stg_ap_pppp_ret) \
337 SymX(stg_ap_ppppp_ret) \
338 SymX(stg_ap_pppppp_ret)
341 #define RTS_SYMBOLS \
345 SymX(stg_enter_info) \
346 SymX(stg_gc_void_info) \
347 SymX(__stg_gc_enter_1) \
348 SymX(stg_gc_noregs) \
349 SymX(stg_gc_unpt_r1_info) \
350 SymX(stg_gc_unpt_r1) \
351 SymX(stg_gc_unbx_r1_info) \
352 SymX(stg_gc_unbx_r1) \
353 SymX(stg_gc_f1_info) \
355 SymX(stg_gc_d1_info) \
357 SymX(stg_gc_l1_info) \
360 SymX(stg_gc_fun_info) \
362 SymX(stg_gc_gen_info) \
363 SymX(stg_gc_gen_hp) \
365 SymX(stg_gen_yield) \
366 SymX(stg_yield_noregs) \
367 SymX(stg_yield_to_interpreter) \
368 SymX(stg_gen_block) \
369 SymX(stg_block_noregs) \
371 SymX(stg_block_takemvar) \
372 SymX(stg_block_putmvar) \
373 SymX(stg_seq_frame_info) \
375 SymX(MallocFailHook) \
377 SymX(OutOfHeapHook) \
378 SymX(StackOverflowHook) \
379 SymX(__encodeDouble) \
380 SymX(__encodeFloat) \
384 SymX(__gmpz_cmp_si) \
385 SymX(__gmpz_cmp_ui) \
386 SymX(__gmpz_get_si) \
387 SymX(__gmpz_get_ui) \
388 SymX(__int_encodeDouble) \
389 SymX(__int_encodeFloat) \
390 SymX(andIntegerzh_fast) \
391 SymX(atomicallyzh_fast) \
393 SymX(blockAsyncExceptionszh_fast) \
395 SymX(catchRetryzh_fast) \
396 SymX(catchSTMzh_fast) \
397 SymX(closure_flags) \
399 SymX(cmpIntegerzh_fast) \
400 SymX(cmpIntegerIntzh_fast) \
401 SymX(complementIntegerzh_fast) \
402 SymX(createAdjustor) \
403 SymX(decodeDoublezh_fast) \
404 SymX(decodeFloatzh_fast) \
407 SymX(deRefWeakzh_fast) \
408 SymX(deRefStablePtrzh_fast) \
409 SymX(divExactIntegerzh_fast) \
410 SymX(divModIntegerzh_fast) \
413 SymX(forkOS_createThread) \
414 SymX(freeHaskellFunctionPtr) \
415 SymX(freeStablePtr) \
416 SymX(gcdIntegerzh_fast) \
417 SymX(gcdIntegerIntzh_fast) \
418 SymX(gcdIntzh_fast) \
424 SymX(int2Integerzh_fast) \
425 SymX(integer2Intzh_fast) \
426 SymX(integer2Wordzh_fast) \
427 SymX(isCurrentThreadBoundzh_fast) \
428 SymX(isDoubleDenormalized) \
429 SymX(isDoubleInfinite) \
431 SymX(isDoubleNegativeZero) \
432 SymX(isEmptyMVarzh_fast) \
433 SymX(isFloatDenormalized) \
434 SymX(isFloatInfinite) \
436 SymX(isFloatNegativeZero) \
437 SymX(killThreadzh_fast) \
440 SymX(makeStablePtrzh_fast) \
441 SymX(minusIntegerzh_fast) \
442 SymX(mkApUpd0zh_fast) \
443 SymX(myThreadIdzh_fast) \
444 SymX(labelThreadzh_fast) \
445 SymX(newArrayzh_fast) \
446 SymX(newBCOzh_fast) \
447 SymX(newByteArrayzh_fast) \
448 SymX_redirect(newCAF, newDynCAF) \
449 SymX(newMVarzh_fast) \
450 SymX(newMutVarzh_fast) \
451 SymX(newTVarzh_fast) \
452 SymX(atomicModifyMutVarzh_fast) \
453 SymX(newPinnedByteArrayzh_fast) \
454 SymX(orIntegerzh_fast) \
456 SymX(performMajorGC) \
457 SymX(plusIntegerzh_fast) \
460 SymX(putMVarzh_fast) \
461 SymX(quotIntegerzh_fast) \
462 SymX(quotRemIntegerzh_fast) \
464 SymX(raiseIOzh_fast) \
465 SymX(readTVarzh_fast) \
466 SymX(remIntegerzh_fast) \
467 SymX(resetNonBlockingFd) \
472 SymX(rts_checkSchedStatus) \
475 SymX(rts_evalLazyIO) \
476 SymX(rts_evalStableIO) \
480 SymX(rts_getDouble) \
485 SymX(rts_getFunPtr) \
486 SymX(rts_getStablePtr) \
487 SymX(rts_getThreadId) \
489 SymX(rts_getWord32) \
502 SymX(rts_mkStablePtr) \
510 SymX(rtsSupportsBoundThreads) \
512 SymX(__hscore_get_saved_termios) \
513 SymX(__hscore_set_saved_termios) \
515 SymX(startupHaskell) \
516 SymX(shutdownHaskell) \
517 SymX(shutdownHaskellAndExit) \
518 SymX(stable_ptr_table) \
519 SymX(stackOverflow) \
520 SymX(stg_CAF_BLACKHOLE_info) \
521 SymX(stg_BLACKHOLE_BQ_info) \
522 SymX(awakenBlockedQueue) \
523 SymX(stg_CHARLIKE_closure) \
524 SymX(stg_EMPTY_MVAR_info) \
525 SymX(stg_IND_STATIC_info) \
526 SymX(stg_INTLIKE_closure) \
527 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
528 SymX(stg_WEAK_info) \
529 SymX(stg_ap_0_info) \
530 SymX(stg_ap_v_info) \
531 SymX(stg_ap_f_info) \
532 SymX(stg_ap_d_info) \
533 SymX(stg_ap_l_info) \
534 SymX(stg_ap_n_info) \
535 SymX(stg_ap_p_info) \
536 SymX(stg_ap_pv_info) \
537 SymX(stg_ap_pp_info) \
538 SymX(stg_ap_ppv_info) \
539 SymX(stg_ap_ppp_info) \
540 SymX(stg_ap_pppv_info) \
541 SymX(stg_ap_pppp_info) \
542 SymX(stg_ap_ppppp_info) \
543 SymX(stg_ap_pppppp_info) \
544 SymX(stg_ap_1_upd_info) \
545 SymX(stg_ap_2_upd_info) \
546 SymX(stg_ap_3_upd_info) \
547 SymX(stg_ap_4_upd_info) \
548 SymX(stg_ap_5_upd_info) \
549 SymX(stg_ap_6_upd_info) \
550 SymX(stg_ap_7_upd_info) \
552 SymX(stg_sel_0_upd_info) \
553 SymX(stg_sel_10_upd_info) \
554 SymX(stg_sel_11_upd_info) \
555 SymX(stg_sel_12_upd_info) \
556 SymX(stg_sel_13_upd_info) \
557 SymX(stg_sel_14_upd_info) \
558 SymX(stg_sel_15_upd_info) \
559 SymX(stg_sel_1_upd_info) \
560 SymX(stg_sel_2_upd_info) \
561 SymX(stg_sel_3_upd_info) \
562 SymX(stg_sel_4_upd_info) \
563 SymX(stg_sel_5_upd_info) \
564 SymX(stg_sel_6_upd_info) \
565 SymX(stg_sel_7_upd_info) \
566 SymX(stg_sel_8_upd_info) \
567 SymX(stg_sel_9_upd_info) \
568 SymX(stg_upd_frame_info) \
569 SymX(suspendThread) \
570 SymX(takeMVarzh_fast) \
571 SymX(timesIntegerzh_fast) \
572 SymX(tryPutMVarzh_fast) \
573 SymX(tryTakeMVarzh_fast) \
574 SymX(unblockAsyncExceptionszh_fast) \
576 SymX(unsafeThawArrayzh_fast) \
577 SymX(waitReadzh_fast) \
578 SymX(waitWritezh_fast) \
579 SymX(word2Integerzh_fast) \
580 SymX(writeTVarzh_fast) \
581 SymX(xorIntegerzh_fast) \
584 #ifdef SUPPORT_LONG_LONGS
585 #define RTS_LONG_LONG_SYMS \
586 SymX(int64ToIntegerzh_fast) \
587 SymX(word64ToIntegerzh_fast)
589 #define RTS_LONG_LONG_SYMS /* nothing */
592 // 64-bit support functions in libgcc.a
593 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
594 #define RTS_LIBGCC_SYMBOLS \
603 #elif defined(ia64_TARGET_ARCH)
604 #define RTS_LIBGCC_SYMBOLS \
612 #define RTS_LIBGCC_SYMBOLS
615 #ifdef darwin_TARGET_OS
616 // Symbols that don't have a leading underscore
617 // on Mac OS X. They have to receive special treatment,
618 // see machoInitSymbolsWithoutUnderscore()
619 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
624 /* entirely bogus claims about types of these symbols */
625 #define Sym(vvv) extern void vvv(void);
626 #define SymX(vvv) /**/
627 #define SymX_redirect(vvv,xxx) /**/
631 RTS_POSIX_ONLY_SYMBOLS
632 RTS_MINGW_ONLY_SYMBOLS
633 RTS_CYGWIN_ONLY_SYMBOLS
639 #ifdef LEADING_UNDERSCORE
640 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
642 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
645 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
647 #define SymX(vvv) Sym(vvv)
649 // SymX_redirect allows us to redirect references to one symbol to
650 // another symbol. See newCAF/newDynCAF for an example.
651 #define SymX_redirect(vvv,xxx) \
652 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
655 static RtsSymbolVal rtsSyms[] = {
659 RTS_POSIX_ONLY_SYMBOLS
660 RTS_MINGW_ONLY_SYMBOLS
661 RTS_CYGWIN_ONLY_SYMBOLS
663 { 0, 0 } /* sentinel */
666 /* -----------------------------------------------------------------------------
667 * Insert symbols into hash tables, checking for duplicates.
669 static void ghciInsertStrHashTable ( char* obj_name,
675 if (lookupHashTable(table, (StgWord)key) == NULL)
677 insertStrHashTable(table, (StgWord)key, data);
682 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
684 "whilst processing object file\n"
686 "This could be caused by:\n"
687 " * Loading two different object files which export the same symbol\n"
688 " * Specifying the same object file twice on the GHCi command line\n"
689 " * An incorrect `package.conf' entry, causing some object to be\n"
691 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
700 /* -----------------------------------------------------------------------------
701 * initialize the object linker
705 static int linker_init_done = 0 ;
707 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
708 static void *dl_prog_handle;
711 /* dlopen(NULL,..) doesn't work so we grab libc explicitly */
712 #if defined(openbsd_TARGET_OS)
713 static void *dl_libc_handle;
721 /* Make initLinker idempotent, so we can call it
722 before evey relevant operation; that means we
723 don't need to initialise the linker separately */
724 if (linker_init_done == 1) { return; } else {
725 linker_init_done = 1;
728 symhash = allocStrHashTable();
730 /* populate the symbol table with stuff from the RTS */
731 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
732 ghciInsertStrHashTable("(GHCi built-in symbols)",
733 symhash, sym->lbl, sym->addr);
735 # if defined(OBJFORMAT_MACHO)
736 machoInitSymbolsWithoutUnderscore();
739 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
740 # if defined(RTLD_DEFAULT)
741 dl_prog_handle = RTLD_DEFAULT;
743 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
744 # if defined(openbsd_TARGET_OS)
745 dl_libc_handle = dlopen("libc.so", RTLD_LAZY);
747 # endif // RTLD_DEFAULT
751 /* -----------------------------------------------------------------------------
752 * Loading DLL or .so dynamic libraries
753 * -----------------------------------------------------------------------------
755 * Add a DLL from which symbols may be found. In the ELF case, just
756 * do RTLD_GLOBAL-style add, so no further messing around needs to
757 * happen in order that symbols in the loaded .so are findable --
758 * lookupSymbol() will subsequently see them by dlsym on the program's
759 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
761 * In the PEi386 case, open the DLLs and put handles to them in a
762 * linked list. When looking for a symbol, try all handles in the
763 * list. This means that we need to load even DLLs that are guaranteed
764 * to be in the ghc.exe image already, just so we can get a handle
765 * to give to loadSymbol, so that we can find the symbols. For such
766 * libraries, the LoadLibrary call should be a no-op except for returning
771 #if defined(OBJFORMAT_PEi386)
772 /* A record for storing handles into DLLs. */
777 struct _OpenedDLL* next;
782 /* A list thereof. */
783 static OpenedDLL* opened_dlls = NULL;
787 addDLL( char *dll_name )
789 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
790 /* ------------------- ELF DLL loader ------------------- */
796 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
799 /* dlopen failed; return a ptr to the error msg. */
801 if (errmsg == NULL) errmsg = "addDLL: unknown error";
808 # elif defined(OBJFORMAT_PEi386)
809 /* ------------------- Win32 DLL loader ------------------- */
817 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
819 /* See if we've already got it, and ignore if so. */
820 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
821 if (0 == strcmp(o_dll->name, dll_name))
825 /* The file name has no suffix (yet) so that we can try
826 both foo.dll and foo.drv
828 The documentation for LoadLibrary says:
829 If no file name extension is specified in the lpFileName
830 parameter, the default library extension .dll is
831 appended. However, the file name string can include a trailing
832 point character (.) to indicate that the module name has no
835 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
836 sprintf(buf, "%s.DLL", dll_name);
837 instance = LoadLibrary(buf);
838 if (instance == NULL) {
839 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
840 instance = LoadLibrary(buf);
841 if (instance == NULL) {
844 /* LoadLibrary failed; return a ptr to the error msg. */
845 return "addDLL: unknown error";
850 /* Add this DLL to the list of DLLs in which to search for symbols. */
851 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
852 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
853 strcpy(o_dll->name, dll_name);
854 o_dll->instance = instance;
855 o_dll->next = opened_dlls;
860 barf("addDLL: not implemented on this platform");
864 /* -----------------------------------------------------------------------------
865 * lookup a symbol in the hash table
868 lookupSymbol( char *lbl )
872 ASSERT(symhash != NULL);
873 val = lookupStrHashTable(symhash, lbl);
876 # if defined(OBJFORMAT_ELF)
877 # if defined(openbsd_TARGET_OS)
878 val = dlsym(dl_prog_handle, lbl);
879 return (val != NULL) ? val : dlsym(dl_libc_handle,lbl);
880 # else /* not openbsd */
881 return dlsym(dl_prog_handle, lbl);
883 # elif defined(OBJFORMAT_MACHO)
884 if(NSIsSymbolNameDefined(lbl)) {
885 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
886 return NSAddressOfSymbol(symbol);
890 # elif defined(OBJFORMAT_PEi386)
893 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
894 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
896 /* HACK: if the name has an initial underscore, try stripping
897 it off & look that up first. I've yet to verify whether there's
898 a Rule that governs whether an initial '_' *should always* be
899 stripped off when mapping from import lib name to the DLL name.
901 sym = GetProcAddress(o_dll->instance, (lbl+1));
903 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
907 sym = GetProcAddress(o_dll->instance, lbl);
909 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
924 __attribute((unused))
926 lookupLocalSymbol( ObjectCode* oc, char *lbl )
930 val = lookupStrHashTable(oc->lochash, lbl);
940 /* -----------------------------------------------------------------------------
941 * Debugging aid: look in GHCi's object symbol tables for symbols
942 * within DELTA bytes of the specified address, and show their names.
945 void ghci_enquire ( char* addr );
947 void ghci_enquire ( char* addr )
952 const int DELTA = 64;
957 for (oc = objects; oc; oc = oc->next) {
958 for (i = 0; i < oc->n_symbols; i++) {
959 sym = oc->symbols[i];
960 if (sym == NULL) continue;
961 // debugBelch("enquire %p %p\n", sym, oc->lochash);
963 if (oc->lochash != NULL) {
964 a = lookupStrHashTable(oc->lochash, sym);
967 a = lookupStrHashTable(symhash, sym);
970 // debugBelch("ghci_enquire: can't find %s\n", sym);
972 else if (addr-DELTA <= a && a <= addr+DELTA) {
973 debugBelch("%p + %3d == `%s'\n", addr, a - addr, sym);
980 #ifdef ia64_TARGET_ARCH
981 static unsigned int PLTSize(void);
984 /* -----------------------------------------------------------------------------
985 * Load an obj (populate the global symbol table, but don't resolve yet)
987 * Returns: 1 if ok, 0 on error.
990 loadObj( char *path )
997 void *map_addr = NULL;
1004 /* debugBelch("loadObj %s\n", path ); */
1006 /* Check that we haven't already loaded this object.
1007 Ignore requests to load multiple times */
1011 for (o = objects; o; o = o->next) {
1012 if (0 == strcmp(o->fileName, path)) {
1014 break; /* don't need to search further */
1018 IF_DEBUG(linker, debugBelch(
1019 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1020 "same object file twice:\n"
1022 "GHCi will ignore this, but be warned.\n"
1024 return 1; /* success */
1028 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
1030 # if defined(OBJFORMAT_ELF)
1031 oc->formatName = "ELF";
1032 # elif defined(OBJFORMAT_PEi386)
1033 oc->formatName = "PEi386";
1034 # elif defined(OBJFORMAT_MACHO)
1035 oc->formatName = "Mach-O";
1038 barf("loadObj: not implemented on this platform");
1041 r = stat(path, &st);
1042 if (r == -1) { return 0; }
1044 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1045 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1046 strcpy(oc->fileName, path);
1048 oc->fileSize = st.st_size;
1050 oc->sections = NULL;
1051 oc->lochash = allocStrHashTable();
1052 oc->proddables = NULL;
1054 /* chain it onto the list of objects */
1059 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1061 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1063 #if defined(openbsd_TARGET_OS)
1064 fd = open(path, O_RDONLY, S_IRUSR);
1066 fd = open(path, O_RDONLY);
1069 barf("loadObj: can't open `%s'", path);
1071 pagesize = getpagesize();
1073 #ifdef ia64_TARGET_ARCH
1074 /* The PLT needs to be right before the object */
1075 n = ROUND_UP(PLTSize(), pagesize);
1076 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1077 if (oc->plt == MAP_FAILED)
1078 barf("loadObj: can't allocate PLT");
1081 map_addr = oc->plt + n;
1084 n = ROUND_UP(oc->fileSize, pagesize);
1085 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1086 if (oc->image == MAP_FAILED)
1087 barf("loadObj: can't map `%s'", path);
1091 #else /* !USE_MMAP */
1093 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
1095 /* load the image into memory */
1096 f = fopen(path, "rb");
1098 barf("loadObj: can't read `%s'", path);
1100 n = fread ( oc->image, 1, oc->fileSize, f );
1101 if (n != oc->fileSize)
1102 barf("loadObj: error whilst reading `%s'", path);
1106 #endif /* USE_MMAP */
1108 # if defined(OBJFORMAT_MACHO)
1109 r = ocAllocateJumpIslands_MachO ( oc );
1110 if (!r) { return r; }
1111 # elif defined(OBJFORMAT_ELF) && defined(powerpc_TARGET_ARCH)
1112 r = ocAllocateJumpIslands_ELF ( oc );
1113 if (!r) { return r; }
1116 /* verify the in-memory image */
1117 # if defined(OBJFORMAT_ELF)
1118 r = ocVerifyImage_ELF ( oc );
1119 # elif defined(OBJFORMAT_PEi386)
1120 r = ocVerifyImage_PEi386 ( oc );
1121 # elif defined(OBJFORMAT_MACHO)
1122 r = ocVerifyImage_MachO ( oc );
1124 barf("loadObj: no verify method");
1126 if (!r) { return r; }
1128 /* build the symbol list for this image */
1129 # if defined(OBJFORMAT_ELF)
1130 r = ocGetNames_ELF ( oc );
1131 # elif defined(OBJFORMAT_PEi386)
1132 r = ocGetNames_PEi386 ( oc );
1133 # elif defined(OBJFORMAT_MACHO)
1134 r = ocGetNames_MachO ( oc );
1136 barf("loadObj: no getNames method");
1138 if (!r) { return r; }
1140 /* loaded, but not resolved yet */
1141 oc->status = OBJECT_LOADED;
1146 /* -----------------------------------------------------------------------------
1147 * resolve all the currently unlinked objects in memory
1149 * Returns: 1 if ok, 0 on error.
1159 for (oc = objects; oc; oc = oc->next) {
1160 if (oc->status != OBJECT_RESOLVED) {
1161 # if defined(OBJFORMAT_ELF)
1162 r = ocResolve_ELF ( oc );
1163 # elif defined(OBJFORMAT_PEi386)
1164 r = ocResolve_PEi386 ( oc );
1165 # elif defined(OBJFORMAT_MACHO)
1166 r = ocResolve_MachO ( oc );
1168 barf("resolveObjs: not implemented on this platform");
1170 if (!r) { return r; }
1171 oc->status = OBJECT_RESOLVED;
1177 /* -----------------------------------------------------------------------------
1178 * delete an object from the pool
1181 unloadObj( char *path )
1183 ObjectCode *oc, *prev;
1185 ASSERT(symhash != NULL);
1186 ASSERT(objects != NULL);
1191 for (oc = objects; oc; prev = oc, oc = oc->next) {
1192 if (!strcmp(oc->fileName,path)) {
1194 /* Remove all the mappings for the symbols within this
1199 for (i = 0; i < oc->n_symbols; i++) {
1200 if (oc->symbols[i] != NULL) {
1201 removeStrHashTable(symhash, oc->symbols[i], NULL);
1209 prev->next = oc->next;
1212 /* We're going to leave this in place, in case there are
1213 any pointers from the heap into it: */
1214 /* stgFree(oc->image); */
1215 stgFree(oc->fileName);
1216 stgFree(oc->symbols);
1217 stgFree(oc->sections);
1218 /* The local hash table should have been freed at the end
1219 of the ocResolve_ call on it. */
1220 ASSERT(oc->lochash == NULL);
1226 errorBelch("unloadObj: can't find `%s' to unload", path);
1230 /* -----------------------------------------------------------------------------
1231 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1232 * which may be prodded during relocation, and abort if we try and write
1233 * outside any of these.
1235 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1238 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1239 /* debugBelch("aPB %p %p %d\n", oc, start, size); */
1243 pb->next = oc->proddables;
1244 oc->proddables = pb;
1247 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1250 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1251 char* s = (char*)(pb->start);
1252 char* e = s + pb->size - 1;
1253 char* a = (char*)addr;
1254 /* Assumes that the biggest fixup involves a 4-byte write. This
1255 probably needs to be changed to 8 (ie, +7) on 64-bit
1257 if (a >= s && (a+3) <= e) return;
1259 barf("checkProddableBlock: invalid fixup in runtime linker");
1262 /* -----------------------------------------------------------------------------
1263 * Section management.
1265 static void addSection ( ObjectCode* oc, SectionKind kind,
1266 void* start, void* end )
1268 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1272 s->next = oc->sections;
1275 debugBelch("addSection: %p-%p (size %d), kind %d\n",
1276 start, ((char*)end)-1, end - start + 1, kind );
1281 /* --------------------------------------------------------------------------
1282 * PowerPC specifics (jump islands)
1283 * ------------------------------------------------------------------------*/
1285 #if defined(powerpc_TARGET_ARCH)
1288 ocAllocateJumpIslands
1290 Allocate additional space at the end of the object file image to make room
1293 PowerPC relative branch instructions have a 24 bit displacement field.
1294 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
1295 If a particular imported symbol is outside this range, we have to redirect
1296 the jump to a short piece of new code that just loads the 32bit absolute
1297 address and jumps there.
1298 This function just allocates space for one 16 byte ppcJumpIsland for every
1299 undefined symbol in the object file. The code for the islands is filled in by
1300 makeJumpIsland below.
1303 static int ocAllocateJumpIslands( ObjectCode* oc, int count, int first )
1310 #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined
1312 // round up to the nearest 4
1313 aligned = (oc->fileSize + 3) & ~3;
1315 oc->image = stgReallocBytes( oc->image,
1316 aligned + sizeof( ppcJumpIsland ) * count,
1317 "ocAllocateJumpIslands" );
1318 oc->jump_islands = (ppcJumpIsland *) (((char *) oc->image) + aligned);
1319 memset( oc->jump_islands, 0, sizeof( ppcJumpIsland ) * count );
1322 oc->jump_islands = NULL;
1324 oc->island_start_symbol = first;
1325 oc->n_islands = count;
1330 static unsigned long makeJumpIsland( ObjectCode* oc,
1331 unsigned long symbolNumber,
1332 unsigned long target )
1334 ppcJumpIsland *island;
1336 if( symbolNumber < oc->island_start_symbol ||
1337 symbolNumber - oc->island_start_symbol > oc->n_islands)
1340 island = &oc->jump_islands[symbolNumber - oc->island_start_symbol];
1342 // lis r12, hi16(target)
1343 island->lis_r12 = 0x3d80;
1344 island->hi_addr = target >> 16;
1346 // ori r12, r12, lo16(target)
1347 island->ori_r12_r12 = 0x618c;
1348 island->lo_addr = target & 0xffff;
1351 island->mtctr_r12 = 0x7d8903a6;
1354 island->bctr = 0x4e800420;
1356 return (unsigned long) island;
1360 ocFlushInstructionCache
1362 Flush the data & instruction caches.
1363 Because the PPC has split data/instruction caches, we have to
1364 do that whenever we modify code at runtime.
1367 static void ocFlushInstructionCache( ObjectCode *oc )
1369 int n = (oc->fileSize + sizeof( ppcJumpIsland ) * oc->n_islands + 3) / 4;
1370 unsigned long *p = (unsigned long *) oc->image;
1374 __asm__ volatile ( "dcbf 0,%0\n\t"
1382 __asm__ volatile ( "sync\n\t"
1388 /* --------------------------------------------------------------------------
1389 * PEi386 specifics (Win32 targets)
1390 * ------------------------------------------------------------------------*/
1392 /* The information for this linker comes from
1393 Microsoft Portable Executable
1394 and Common Object File Format Specification
1395 revision 5.1 January 1998
1396 which SimonM says comes from the MS Developer Network CDs.
1398 It can be found there (on older CDs), but can also be found
1401 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1403 (this is Rev 6.0 from February 1999).
1405 Things move, so if that fails, try searching for it via
1407 http://www.google.com/search?q=PE+COFF+specification
1409 The ultimate reference for the PE format is the Winnt.h
1410 header file that comes with the Platform SDKs; as always,
1411 implementations will drift wrt their documentation.
1413 A good background article on the PE format is Matt Pietrek's
1414 March 1994 article in Microsoft System Journal (MSJ)
1415 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1416 Win32 Portable Executable File Format." The info in there
1417 has recently been updated in a two part article in
1418 MSDN magazine, issues Feb and March 2002,
1419 "Inside Windows: An In-Depth Look into the Win32 Portable
1420 Executable File Format"
1422 John Levine's book "Linkers and Loaders" contains useful
1427 #if defined(OBJFORMAT_PEi386)
1431 typedef unsigned char UChar;
1432 typedef unsigned short UInt16;
1433 typedef unsigned int UInt32;
1440 UInt16 NumberOfSections;
1441 UInt32 TimeDateStamp;
1442 UInt32 PointerToSymbolTable;
1443 UInt32 NumberOfSymbols;
1444 UInt16 SizeOfOptionalHeader;
1445 UInt16 Characteristics;
1449 #define sizeof_COFF_header 20
1456 UInt32 VirtualAddress;
1457 UInt32 SizeOfRawData;
1458 UInt32 PointerToRawData;
1459 UInt32 PointerToRelocations;
1460 UInt32 PointerToLinenumbers;
1461 UInt16 NumberOfRelocations;
1462 UInt16 NumberOfLineNumbers;
1463 UInt32 Characteristics;
1467 #define sizeof_COFF_section 40
1474 UInt16 SectionNumber;
1477 UChar NumberOfAuxSymbols;
1481 #define sizeof_COFF_symbol 18
1486 UInt32 VirtualAddress;
1487 UInt32 SymbolTableIndex;
1492 #define sizeof_COFF_reloc 10
1495 /* From PE spec doc, section 3.3.2 */
1496 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1497 windows.h -- for the same purpose, but I want to know what I'm
1499 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1500 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1501 #define MYIMAGE_FILE_DLL 0x2000
1502 #define MYIMAGE_FILE_SYSTEM 0x1000
1503 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1504 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1505 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1507 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1508 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1509 #define MYIMAGE_SYM_CLASS_STATIC 3
1510 #define MYIMAGE_SYM_UNDEFINED 0
1512 /* From PE spec doc, section 4.1 */
1513 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1514 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1515 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1517 /* From PE spec doc, section 5.2.1 */
1518 #define MYIMAGE_REL_I386_DIR32 0x0006
1519 #define MYIMAGE_REL_I386_REL32 0x0014
1522 /* We use myindex to calculate array addresses, rather than
1523 simply doing the normal subscript thing. That's because
1524 some of the above structs have sizes which are not
1525 a whole number of words. GCC rounds their sizes up to a
1526 whole number of words, which means that the address calcs
1527 arising from using normal C indexing or pointer arithmetic
1528 are just plain wrong. Sigh.
1531 myindex ( int scale, void* base, int index )
1534 ((UChar*)base) + scale * index;
1539 printName ( UChar* name, UChar* strtab )
1541 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1542 UInt32 strtab_offset = * (UInt32*)(name+4);
1543 debugBelch("%s", strtab + strtab_offset );
1546 for (i = 0; i < 8; i++) {
1547 if (name[i] == 0) break;
1548 debugBelch("%c", name[i] );
1555 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1557 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1558 UInt32 strtab_offset = * (UInt32*)(name+4);
1559 strncpy ( dst, strtab+strtab_offset, dstSize );
1565 if (name[i] == 0) break;
1575 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1578 /* If the string is longer than 8 bytes, look in the
1579 string table for it -- this will be correctly zero terminated.
1581 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1582 UInt32 strtab_offset = * (UInt32*)(name+4);
1583 return ((UChar*)strtab) + strtab_offset;
1585 /* Otherwise, if shorter than 8 bytes, return the original,
1586 which by defn is correctly terminated.
1588 if (name[7]==0) return name;
1589 /* The annoying case: 8 bytes. Copy into a temporary
1590 (which is never freed ...)
1592 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1594 strncpy(newstr,name,8);
1600 /* Just compares the short names (first 8 chars) */
1601 static COFF_section *
1602 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1606 = (COFF_header*)(oc->image);
1607 COFF_section* sectab
1609 ((UChar*)(oc->image))
1610 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1612 for (i = 0; i < hdr->NumberOfSections; i++) {
1615 COFF_section* section_i
1617 myindex ( sizeof_COFF_section, sectab, i );
1618 n1 = (UChar*) &(section_i->Name);
1620 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1621 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1622 n1[6]==n2[6] && n1[7]==n2[7])
1631 zapTrailingAtSign ( UChar* sym )
1633 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1635 if (sym[0] == 0) return;
1637 while (sym[i] != 0) i++;
1640 while (j > 0 && my_isdigit(sym[j])) j--;
1641 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1647 ocVerifyImage_PEi386 ( ObjectCode* oc )
1652 COFF_section* sectab;
1653 COFF_symbol* symtab;
1655 /* debugBelch("\nLOADING %s\n", oc->fileName); */
1656 hdr = (COFF_header*)(oc->image);
1657 sectab = (COFF_section*) (
1658 ((UChar*)(oc->image))
1659 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1661 symtab = (COFF_symbol*) (
1662 ((UChar*)(oc->image))
1663 + hdr->PointerToSymbolTable
1665 strtab = ((UChar*)symtab)
1666 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1668 if (hdr->Machine != 0x14c) {
1669 errorBelch("Not x86 PEi386");
1672 if (hdr->SizeOfOptionalHeader != 0) {
1673 errorBelch("PEi386 with nonempty optional header");
1676 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1677 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1678 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1679 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1680 errorBelch("Not a PEi386 object file");
1683 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1684 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1685 errorBelch("Invalid PEi386 word size or endiannness: %d",
1686 (int)(hdr->Characteristics));
1689 /* If the string table size is way crazy, this might indicate that
1690 there are more than 64k relocations, despite claims to the
1691 contrary. Hence this test. */
1692 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
1694 if ( (*(UInt32*)strtab) > 600000 ) {
1695 /* Note that 600k has no special significance other than being
1696 big enough to handle the almost-2MB-sized lumps that
1697 constitute HSwin32*.o. */
1698 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
1703 /* No further verification after this point; only debug printing. */
1705 IF_DEBUG(linker, i=1);
1706 if (i == 0) return 1;
1708 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1709 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1710 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1713 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1714 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1715 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1716 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1717 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1718 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1719 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1721 /* Print the section table. */
1723 for (i = 0; i < hdr->NumberOfSections; i++) {
1725 COFF_section* sectab_i
1727 myindex ( sizeof_COFF_section, sectab, i );
1734 printName ( sectab_i->Name, strtab );
1744 sectab_i->VirtualSize,
1745 sectab_i->VirtualAddress,
1746 sectab_i->SizeOfRawData,
1747 sectab_i->PointerToRawData,
1748 sectab_i->NumberOfRelocations,
1749 sectab_i->PointerToRelocations,
1750 sectab_i->PointerToRawData
1752 reltab = (COFF_reloc*) (
1753 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1756 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1757 /* If the relocation field (a short) has overflowed, the
1758 * real count can be found in the first reloc entry.
1760 * See Section 4.1 (last para) of the PE spec (rev6.0).
1762 COFF_reloc* rel = (COFF_reloc*)
1763 myindex ( sizeof_COFF_reloc, reltab, 0 );
1764 noRelocs = rel->VirtualAddress;
1767 noRelocs = sectab_i->NumberOfRelocations;
1771 for (; j < noRelocs; j++) {
1773 COFF_reloc* rel = (COFF_reloc*)
1774 myindex ( sizeof_COFF_reloc, reltab, j );
1776 " type 0x%-4x vaddr 0x%-8x name `",
1778 rel->VirtualAddress );
1779 sym = (COFF_symbol*)
1780 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1781 /* Hmm..mysterious looking offset - what's it for? SOF */
1782 printName ( sym->Name, strtab -10 );
1789 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
1790 debugBelch("---START of string table---\n");
1791 for (i = 4; i < *(Int32*)strtab; i++) {
1793 debugBelch("\n"); else
1794 debugBelch("%c", strtab[i] );
1796 debugBelch("--- END of string table---\n");
1801 COFF_symbol* symtab_i;
1802 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1803 symtab_i = (COFF_symbol*)
1804 myindex ( sizeof_COFF_symbol, symtab, i );
1810 printName ( symtab_i->Name, strtab );
1819 (Int32)(symtab_i->SectionNumber),
1820 (UInt32)symtab_i->Type,
1821 (UInt32)symtab_i->StorageClass,
1822 (UInt32)symtab_i->NumberOfAuxSymbols
1824 i += symtab_i->NumberOfAuxSymbols;
1834 ocGetNames_PEi386 ( ObjectCode* oc )
1837 COFF_section* sectab;
1838 COFF_symbol* symtab;
1845 hdr = (COFF_header*)(oc->image);
1846 sectab = (COFF_section*) (
1847 ((UChar*)(oc->image))
1848 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1850 symtab = (COFF_symbol*) (
1851 ((UChar*)(oc->image))
1852 + hdr->PointerToSymbolTable
1854 strtab = ((UChar*)(oc->image))
1855 + hdr->PointerToSymbolTable
1856 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1858 /* Allocate space for any (local, anonymous) .bss sections. */
1860 for (i = 0; i < hdr->NumberOfSections; i++) {
1862 COFF_section* sectab_i
1864 myindex ( sizeof_COFF_section, sectab, i );
1865 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1866 if (sectab_i->VirtualSize == 0) continue;
1867 /* This is a non-empty .bss section. Allocate zeroed space for
1868 it, and set its PointerToRawData field such that oc->image +
1869 PointerToRawData == addr_of_zeroed_space. */
1870 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1871 "ocGetNames_PEi386(anonymous bss)");
1872 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1873 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1874 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
1877 /* Copy section information into the ObjectCode. */
1879 for (i = 0; i < hdr->NumberOfSections; i++) {
1885 = SECTIONKIND_OTHER;
1886 COFF_section* sectab_i
1888 myindex ( sizeof_COFF_section, sectab, i );
1889 IF_DEBUG(linker, debugBelch("section name = %s\n", sectab_i->Name ));
1892 /* I'm sure this is the Right Way to do it. However, the
1893 alternative of testing the sectab_i->Name field seems to
1894 work ok with Cygwin.
1896 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1897 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1898 kind = SECTIONKIND_CODE_OR_RODATA;
1901 if (0==strcmp(".text",sectab_i->Name) ||
1902 0==strcmp(".rodata",sectab_i->Name))
1903 kind = SECTIONKIND_CODE_OR_RODATA;
1904 if (0==strcmp(".data",sectab_i->Name) ||
1905 0==strcmp(".bss",sectab_i->Name))
1906 kind = SECTIONKIND_RWDATA;
1908 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1909 sz = sectab_i->SizeOfRawData;
1910 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1912 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1913 end = start + sz - 1;
1915 if (kind == SECTIONKIND_OTHER
1916 /* Ignore sections called which contain stabs debugging
1918 && 0 != strcmp(".stab", sectab_i->Name)
1919 && 0 != strcmp(".stabstr", sectab_i->Name)
1921 errorBelch("Unknown PEi386 section name `%s'", sectab_i->Name);
1925 if (kind != SECTIONKIND_OTHER && end >= start) {
1926 addSection(oc, kind, start, end);
1927 addProddableBlock(oc, start, end - start + 1);
1931 /* Copy exported symbols into the ObjectCode. */
1933 oc->n_symbols = hdr->NumberOfSymbols;
1934 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1935 "ocGetNames_PEi386(oc->symbols)");
1936 /* Call me paranoid; I don't care. */
1937 for (i = 0; i < oc->n_symbols; i++)
1938 oc->symbols[i] = NULL;
1942 COFF_symbol* symtab_i;
1943 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1944 symtab_i = (COFF_symbol*)
1945 myindex ( sizeof_COFF_symbol, symtab, i );
1949 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1950 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1951 /* This symbol is global and defined, viz, exported */
1952 /* for MYIMAGE_SYMCLASS_EXTERNAL
1953 && !MYIMAGE_SYM_UNDEFINED,
1954 the address of the symbol is:
1955 address of relevant section + offset in section
1957 COFF_section* sectabent
1958 = (COFF_section*) myindex ( sizeof_COFF_section,
1960 symtab_i->SectionNumber-1 );
1961 addr = ((UChar*)(oc->image))
1962 + (sectabent->PointerToRawData
1966 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1967 && symtab_i->Value > 0) {
1968 /* This symbol isn't in any section at all, ie, global bss.
1969 Allocate zeroed space for it. */
1970 addr = stgCallocBytes(1, symtab_i->Value,
1971 "ocGetNames_PEi386(non-anonymous bss)");
1972 addSection(oc, SECTIONKIND_RWDATA, addr,
1973 ((UChar*)addr) + symtab_i->Value - 1);
1974 addProddableBlock(oc, addr, symtab_i->Value);
1975 /* debugBelch("BSS section at 0x%x\n", addr); */
1978 if (addr != NULL ) {
1979 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1980 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
1981 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
1982 ASSERT(i >= 0 && i < oc->n_symbols);
1983 /* cstring_from_COFF_symbol_name always succeeds. */
1984 oc->symbols[i] = sname;
1985 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1989 "IGNORING symbol %d\n"
1993 printName ( symtab_i->Name, strtab );
2002 (Int32)(symtab_i->SectionNumber),
2003 (UInt32)symtab_i->Type,
2004 (UInt32)symtab_i->StorageClass,
2005 (UInt32)symtab_i->NumberOfAuxSymbols
2010 i += symtab_i->NumberOfAuxSymbols;
2019 ocResolve_PEi386 ( ObjectCode* oc )
2022 COFF_section* sectab;
2023 COFF_symbol* symtab;
2033 /* ToDo: should be variable-sized? But is at least safe in the
2034 sense of buffer-overrun-proof. */
2036 /* debugBelch("resolving for %s\n", oc->fileName); */
2038 hdr = (COFF_header*)(oc->image);
2039 sectab = (COFF_section*) (
2040 ((UChar*)(oc->image))
2041 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2043 symtab = (COFF_symbol*) (
2044 ((UChar*)(oc->image))
2045 + hdr->PointerToSymbolTable
2047 strtab = ((UChar*)(oc->image))
2048 + hdr->PointerToSymbolTable
2049 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2051 for (i = 0; i < hdr->NumberOfSections; i++) {
2052 COFF_section* sectab_i
2054 myindex ( sizeof_COFF_section, sectab, i );
2057 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2060 /* Ignore sections called which contain stabs debugging
2062 if (0 == strcmp(".stab", sectab_i->Name)
2063 || 0 == strcmp(".stabstr", sectab_i->Name))
2066 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2067 /* If the relocation field (a short) has overflowed, the
2068 * real count can be found in the first reloc entry.
2070 * See Section 4.1 (last para) of the PE spec (rev6.0).
2072 * Nov2003 update: the GNU linker still doesn't correctly
2073 * handle the generation of relocatable object files with
2074 * overflown relocations. Hence the output to warn of potential
2077 COFF_reloc* rel = (COFF_reloc*)
2078 myindex ( sizeof_COFF_reloc, reltab, 0 );
2079 noRelocs = rel->VirtualAddress;
2080 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
2084 noRelocs = sectab_i->NumberOfRelocations;
2089 for (; j < noRelocs; j++) {
2091 COFF_reloc* reltab_j
2093 myindex ( sizeof_COFF_reloc, reltab, j );
2095 /* the location to patch */
2097 ((UChar*)(oc->image))
2098 + (sectab_i->PointerToRawData
2099 + reltab_j->VirtualAddress
2100 - sectab_i->VirtualAddress )
2102 /* the existing contents of pP */
2104 /* the symbol to connect to */
2105 sym = (COFF_symbol*)
2106 myindex ( sizeof_COFF_symbol,
2107 symtab, reltab_j->SymbolTableIndex );
2110 "reloc sec %2d num %3d: type 0x%-4x "
2111 "vaddr 0x%-8x name `",
2113 (UInt32)reltab_j->Type,
2114 reltab_j->VirtualAddress );
2115 printName ( sym->Name, strtab );
2116 debugBelch("'\n" ));
2118 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
2119 COFF_section* section_sym
2120 = findPEi386SectionCalled ( oc, sym->Name );
2122 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
2125 S = ((UInt32)(oc->image))
2126 + (section_sym->PointerToRawData
2129 copyName ( sym->Name, strtab, symbol, 1000-1 );
2130 (void*)S = lookupLocalSymbol( oc, symbol );
2131 if ((void*)S != NULL) goto foundit;
2132 (void*)S = lookupSymbol( symbol );
2133 if ((void*)S != NULL) goto foundit;
2134 zapTrailingAtSign ( symbol );
2135 (void*)S = lookupLocalSymbol( oc, symbol );
2136 if ((void*)S != NULL) goto foundit;
2137 (void*)S = lookupSymbol( symbol );
2138 if ((void*)S != NULL) goto foundit;
2139 /* Newline first because the interactive linker has printed "linking..." */
2140 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
2144 checkProddableBlock(oc, pP);
2145 switch (reltab_j->Type) {
2146 case MYIMAGE_REL_I386_DIR32:
2149 case MYIMAGE_REL_I386_REL32:
2150 /* Tricky. We have to insert a displacement at
2151 pP which, when added to the PC for the _next_
2152 insn, gives the address of the target (S).
2153 Problem is to know the address of the next insn
2154 when we only know pP. We assume that this
2155 literal field is always the last in the insn,
2156 so that the address of the next insn is pP+4
2157 -- hence the constant 4.
2158 Also I don't know if A should be added, but so
2159 far it has always been zero.
2162 *pP = S - ((UInt32)pP) - 4;
2165 debugBelch("%s: unhandled PEi386 relocation type %d",
2166 oc->fileName, reltab_j->Type);
2173 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
2177 #endif /* defined(OBJFORMAT_PEi386) */
2180 /* --------------------------------------------------------------------------
2182 * ------------------------------------------------------------------------*/
2184 #if defined(OBJFORMAT_ELF)
2189 #if defined(sparc_TARGET_ARCH)
2190 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2191 #elif defined(i386_TARGET_ARCH)
2192 # define ELF_TARGET_386 /* Used inside <elf.h> */
2193 #elif defined(x86_64_TARGET_ARCH)
2194 # define ELF_TARGET_X64_64
2196 #elif defined (ia64_TARGET_ARCH)
2197 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2199 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2200 # define ELF_NEED_GOT /* needs Global Offset Table */
2201 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2204 #if !defined(openbsd_TARGET_OS)
2207 /* openbsd elf has things in different places, with diff names */
2208 #include <elf_abi.h>
2209 #include <machine/reloc.h>
2210 #define R_386_32 RELOC_32
2211 #define R_386_PC32 RELOC_PC32
2215 * Define a set of types which can be used for both ELF32 and ELF64
2219 #define ELFCLASS ELFCLASS64
2220 #define Elf_Addr Elf64_Addr
2221 #define Elf_Word Elf64_Word
2222 #define Elf_Sword Elf64_Sword
2223 #define Elf_Ehdr Elf64_Ehdr
2224 #define Elf_Phdr Elf64_Phdr
2225 #define Elf_Shdr Elf64_Shdr
2226 #define Elf_Sym Elf64_Sym
2227 #define Elf_Rel Elf64_Rel
2228 #define Elf_Rela Elf64_Rela
2229 #define ELF_ST_TYPE ELF64_ST_TYPE
2230 #define ELF_ST_BIND ELF64_ST_BIND
2231 #define ELF_R_TYPE ELF64_R_TYPE
2232 #define ELF_R_SYM ELF64_R_SYM
2234 #define ELFCLASS ELFCLASS32
2235 #define Elf_Addr Elf32_Addr
2236 #define Elf_Word Elf32_Word
2237 #define Elf_Sword Elf32_Sword
2238 #define Elf_Ehdr Elf32_Ehdr
2239 #define Elf_Phdr Elf32_Phdr
2240 #define Elf_Shdr Elf32_Shdr
2241 #define Elf_Sym Elf32_Sym
2242 #define Elf_Rel Elf32_Rel
2243 #define Elf_Rela Elf32_Rela
2245 #define ELF_ST_TYPE ELF32_ST_TYPE
2248 #define ELF_ST_BIND ELF32_ST_BIND
2251 #define ELF_R_TYPE ELF32_R_TYPE
2254 #define ELF_R_SYM ELF32_R_SYM
2260 * Functions to allocate entries in dynamic sections. Currently we simply
2261 * preallocate a large number, and we don't check if a entry for the given
2262 * target already exists (a linear search is too slow). Ideally these
2263 * entries would be associated with symbols.
2266 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2267 #define GOT_SIZE 0x20000
2268 #define FUNCTION_TABLE_SIZE 0x10000
2269 #define PLT_SIZE 0x08000
2272 static Elf_Addr got[GOT_SIZE];
2273 static unsigned int gotIndex;
2274 static Elf_Addr gp_val = (Elf_Addr)got;
2277 allocateGOTEntry(Elf_Addr target)
2281 if (gotIndex >= GOT_SIZE)
2282 barf("Global offset table overflow");
2284 entry = &got[gotIndex++];
2286 return (Elf_Addr)entry;
2290 #ifdef ELF_FUNCTION_DESC
2296 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2297 static unsigned int functionTableIndex;
2300 allocateFunctionDesc(Elf_Addr target)
2302 FunctionDesc *entry;
2304 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2305 barf("Function table overflow");
2307 entry = &functionTable[functionTableIndex++];
2309 entry->gp = (Elf_Addr)gp_val;
2310 return (Elf_Addr)entry;
2314 copyFunctionDesc(Elf_Addr target)
2316 FunctionDesc *olddesc = (FunctionDesc *)target;
2317 FunctionDesc *newdesc;
2319 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2320 newdesc->gp = olddesc->gp;
2321 return (Elf_Addr)newdesc;
2326 #ifdef ia64_TARGET_ARCH
2327 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2328 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2330 static unsigned char plt_code[] =
2332 /* taken from binutils bfd/elfxx-ia64.c */
2333 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2334 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2335 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2336 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2337 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2338 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2341 /* If we can't get to the function descriptor via gp, take a local copy of it */
2342 #define PLT_RELOC(code, target) { \
2343 Elf64_Sxword rel_value = target - gp_val; \
2344 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2345 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2347 ia64_reloc_gprel22((Elf_Addr)code, target); \
2352 unsigned char code[sizeof(plt_code)];
2356 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2358 PLTEntry *plt = (PLTEntry *)oc->plt;
2361 if (oc->pltIndex >= PLT_SIZE)
2362 barf("Procedure table overflow");
2364 entry = &plt[oc->pltIndex++];
2365 memcpy(entry->code, plt_code, sizeof(entry->code));
2366 PLT_RELOC(entry->code, target);
2367 return (Elf_Addr)entry;
2373 return (PLT_SIZE * sizeof(PLTEntry));
2379 * Generic ELF functions
2383 findElfSection ( void* objImage, Elf_Word sh_type )
2385 char* ehdrC = (char*)objImage;
2386 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2387 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2388 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2392 for (i = 0; i < ehdr->e_shnum; i++) {
2393 if (shdr[i].sh_type == sh_type
2394 /* Ignore the section header's string table. */
2395 && i != ehdr->e_shstrndx
2396 /* Ignore string tables named .stabstr, as they contain
2398 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2400 ptr = ehdrC + shdr[i].sh_offset;
2407 #if defined(ia64_TARGET_ARCH)
2409 findElfSegment ( void* objImage, Elf_Addr vaddr )
2411 char* ehdrC = (char*)objImage;
2412 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2413 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2414 Elf_Addr segaddr = 0;
2417 for (i = 0; i < ehdr->e_phnum; i++) {
2418 segaddr = phdr[i].p_vaddr;
2419 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2427 ocVerifyImage_ELF ( ObjectCode* oc )
2431 int i, j, nent, nstrtab, nsymtabs;
2435 char* ehdrC = (char*)(oc->image);
2436 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2438 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2439 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2440 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2441 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2442 errorBelch("%s: not an ELF object", oc->fileName);
2446 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2447 errorBelch("%s: unsupported ELF format", oc->fileName);
2451 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2452 IF_DEBUG(linker,debugBelch( "Is little-endian" ));
2454 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2455 IF_DEBUG(linker,debugBelch( "Is big-endian" ));
2457 errorBelch("%s: unknown endiannness", oc->fileName);
2461 if (ehdr->e_type != ET_REL) {
2462 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
2465 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file" ));
2467 IF_DEBUG(linker,debugBelch( "Architecture is " ));
2468 switch (ehdr->e_machine) {
2469 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
2470 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
2472 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
2474 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
2475 default: IF_DEBUG(linker,debugBelch( "unknown" ));
2476 errorBelch("%s: unknown architecture", oc->fileName);
2480 IF_DEBUG(linker,debugBelch(
2481 "\nSection header table: start %d, n_entries %d, ent_size %d",
2482 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2484 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2486 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2488 if (ehdr->e_shstrndx == SHN_UNDEF) {
2489 errorBelch("%s: no section header string table", oc->fileName);
2492 IF_DEBUG(linker,debugBelch( "Section header string table is section %d",
2494 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2497 for (i = 0; i < ehdr->e_shnum; i++) {
2498 IF_DEBUG(linker,debugBelch("%2d: ", i ));
2499 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
2500 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
2501 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
2502 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
2503 ehdrC + shdr[i].sh_offset,
2504 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2506 if (shdr[i].sh_type == SHT_REL) {
2507 IF_DEBUG(linker,debugBelch("Rel " ));
2508 } else if (shdr[i].sh_type == SHT_RELA) {
2509 IF_DEBUG(linker,debugBelch("RelA " ));
2511 IF_DEBUG(linker,debugBelch(" "));
2514 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
2518 IF_DEBUG(linker,debugBelch( "\nString tables" ));
2521 for (i = 0; i < ehdr->e_shnum; i++) {
2522 if (shdr[i].sh_type == SHT_STRTAB
2523 /* Ignore the section header's string table. */
2524 && i != ehdr->e_shstrndx
2525 /* Ignore string tables named .stabstr, as they contain
2527 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2529 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
2530 strtab = ehdrC + shdr[i].sh_offset;
2535 errorBelch("%s: no string tables, or too many", oc->fileName);
2540 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
2541 for (i = 0; i < ehdr->e_shnum; i++) {
2542 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2543 IF_DEBUG(linker,debugBelch( "section %d is a symbol table", i ));
2545 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2546 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2547 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%d rem)",
2549 shdr[i].sh_size % sizeof(Elf_Sym)
2551 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2552 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
2555 for (j = 0; j < nent; j++) {
2556 IF_DEBUG(linker,debugBelch(" %2d ", j ));
2557 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
2558 (int)stab[j].st_shndx,
2559 (int)stab[j].st_size,
2560 (char*)stab[j].st_value ));
2562 IF_DEBUG(linker,debugBelch("type=" ));
2563 switch (ELF_ST_TYPE(stab[j].st_info)) {
2564 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
2565 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
2566 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
2567 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
2568 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
2569 default: IF_DEBUG(linker,debugBelch("? " )); break;
2571 IF_DEBUG(linker,debugBelch(" " ));
2573 IF_DEBUG(linker,debugBelch("bind=" ));
2574 switch (ELF_ST_BIND(stab[j].st_info)) {
2575 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
2576 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
2577 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
2578 default: IF_DEBUG(linker,debugBelch("? " )); break;
2580 IF_DEBUG(linker,debugBelch(" " ));
2582 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
2586 if (nsymtabs == 0) {
2587 errorBelch("%s: didn't find any symbol tables", oc->fileName);
2596 ocGetNames_ELF ( ObjectCode* oc )
2601 char* ehdrC = (char*)(oc->image);
2602 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2603 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2604 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2606 ASSERT(symhash != NULL);
2609 errorBelch("%s: no strtab", oc->fileName);
2614 for (i = 0; i < ehdr->e_shnum; i++) {
2615 /* Figure out what kind of section it is. Logic derived from
2616 Figure 1.14 ("Special Sections") of the ELF document
2617 ("Portable Formats Specification, Version 1.1"). */
2618 Elf_Shdr hdr = shdr[i];
2619 SectionKind kind = SECTIONKIND_OTHER;
2622 if (hdr.sh_type == SHT_PROGBITS
2623 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2624 /* .text-style section */
2625 kind = SECTIONKIND_CODE_OR_RODATA;
2628 if (hdr.sh_type == SHT_PROGBITS
2629 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2630 /* .data-style section */
2631 kind = SECTIONKIND_RWDATA;
2634 if (hdr.sh_type == SHT_PROGBITS
2635 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2636 /* .rodata-style section */
2637 kind = SECTIONKIND_CODE_OR_RODATA;
2640 if (hdr.sh_type == SHT_NOBITS
2641 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2642 /* .bss-style section */
2643 kind = SECTIONKIND_RWDATA;
2647 if (is_bss && shdr[i].sh_size > 0) {
2648 /* This is a non-empty .bss section. Allocate zeroed space for
2649 it, and set its .sh_offset field such that
2650 ehdrC + .sh_offset == addr_of_zeroed_space. */
2651 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2652 "ocGetNames_ELF(BSS)");
2653 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2655 debugBelch("BSS section at 0x%x, size %d\n",
2656 zspace, shdr[i].sh_size);
2660 /* fill in the section info */
2661 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2662 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2663 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2664 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2667 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2669 /* copy stuff into this module's object symbol table */
2670 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2671 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2673 oc->n_symbols = nent;
2674 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2675 "ocGetNames_ELF(oc->symbols)");
2677 for (j = 0; j < nent; j++) {
2679 char isLocal = FALSE; /* avoids uninit-var warning */
2681 char* nm = strtab + stab[j].st_name;
2682 int secno = stab[j].st_shndx;
2684 /* Figure out if we want to add it; if so, set ad to its
2685 address. Otherwise leave ad == NULL. */
2687 if (secno == SHN_COMMON) {
2689 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2691 debugBelch("COMMON symbol, size %d name %s\n",
2692 stab[j].st_size, nm);
2694 /* Pointless to do addProddableBlock() for this area,
2695 since the linker should never poke around in it. */
2698 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2699 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2701 /* and not an undefined symbol */
2702 && stab[j].st_shndx != SHN_UNDEF
2703 /* and not in a "special section" */
2704 && stab[j].st_shndx < SHN_LORESERVE
2706 /* and it's a not a section or string table or anything silly */
2707 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2708 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2709 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2712 /* Section 0 is the undefined section, hence > and not >=. */
2713 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2715 if (shdr[secno].sh_type == SHT_NOBITS) {
2716 debugBelch(" BSS symbol, size %d off %d name %s\n",
2717 stab[j].st_size, stab[j].st_value, nm);
2720 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2721 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2724 #ifdef ELF_FUNCTION_DESC
2725 /* dlsym() and the initialisation table both give us function
2726 * descriptors, so to be consistent we store function descriptors
2727 * in the symbol table */
2728 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2729 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2731 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s",
2732 ad, oc->fileName, nm ));
2737 /* And the decision is ... */
2741 oc->symbols[j] = nm;
2744 /* Ignore entirely. */
2746 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2750 IF_DEBUG(linker,debugBelch( "skipping `%s'",
2751 strtab + stab[j].st_name ));
2754 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2755 (int)ELF_ST_BIND(stab[j].st_info),
2756 (int)ELF_ST_TYPE(stab[j].st_info),
2757 (int)stab[j].st_shndx,
2758 strtab + stab[j].st_name
2761 oc->symbols[j] = NULL;
2770 /* Do ELF relocations which lack an explicit addend. All x86-linux
2771 relocations appear to be of this form. */
2773 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2774 Elf_Shdr* shdr, int shnum,
2775 Elf_Sym* stab, char* strtab )
2780 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2781 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2782 int target_shndx = shdr[shnum].sh_info;
2783 int symtab_shndx = shdr[shnum].sh_link;
2785 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2786 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2787 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d",
2788 target_shndx, symtab_shndx ));
2790 for (j = 0; j < nent; j++) {
2791 Elf_Addr offset = rtab[j].r_offset;
2792 Elf_Addr info = rtab[j].r_info;
2794 Elf_Addr P = ((Elf_Addr)targ) + offset;
2795 Elf_Word* pP = (Elf_Word*)P;
2801 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
2802 j, (void*)offset, (void*)info ));
2804 IF_DEBUG(linker,debugBelch( " ZERO" ));
2807 Elf_Sym sym = stab[ELF_R_SYM(info)];
2808 /* First see if it is a local symbol. */
2809 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2810 /* Yes, so we can get the address directly from the ELF symbol
2812 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2814 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2815 + stab[ELF_R_SYM(info)].st_value);
2818 /* No, so look up the name in our global table. */
2819 symbol = strtab + sym.st_name;
2820 S_tmp = lookupSymbol( symbol );
2821 S = (Elf_Addr)S_tmp;
2824 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
2827 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
2830 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p",
2831 (void*)P, (void*)S, (void*)A ));
2832 checkProddableBlock ( oc, pP );
2836 switch (ELF_R_TYPE(info)) {
2837 # ifdef i386_TARGET_ARCH
2838 case R_386_32: *pP = value; break;
2839 case R_386_PC32: *pP = value - P; break;
2842 errorBelch("%s: unhandled ELF relocation(Rel) type %d\n",
2843 oc->fileName, ELF_R_TYPE(info));
2851 /* Do ELF relocations for which explicit addends are supplied.
2852 sparc-solaris relocations appear to be of this form. */
2854 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2855 Elf_Shdr* shdr, int shnum,
2856 Elf_Sym* stab, char* strtab )
2861 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2862 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2863 int target_shndx = shdr[shnum].sh_info;
2864 int symtab_shndx = shdr[shnum].sh_link;
2866 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2867 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2868 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d",
2869 target_shndx, symtab_shndx ));
2871 for (j = 0; j < nent; j++) {
2872 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH) || defined(powerpc_TARGET_ARCH)
2873 /* This #ifdef only serves to avoid unused-var warnings. */
2874 Elf_Addr offset = rtab[j].r_offset;
2875 Elf_Addr P = targ + offset;
2877 Elf_Addr info = rtab[j].r_info;
2878 Elf_Addr A = rtab[j].r_addend;
2882 # if defined(sparc_TARGET_ARCH)
2883 Elf_Word* pP = (Elf_Word*)P;
2885 # elif defined(ia64_TARGET_ARCH)
2886 Elf64_Xword *pP = (Elf64_Xword *)P;
2888 # elif defined(powerpc_TARGET_ARCH)
2892 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
2893 j, (void*)offset, (void*)info,
2896 IF_DEBUG(linker,debugBelch( " ZERO" ));
2899 Elf_Sym sym = stab[ELF_R_SYM(info)];
2900 /* First see if it is a local symbol. */
2901 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2902 /* Yes, so we can get the address directly from the ELF symbol
2904 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2906 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2907 + stab[ELF_R_SYM(info)].st_value);
2908 #ifdef ELF_FUNCTION_DESC
2909 /* Make a function descriptor for this function */
2910 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2911 S = allocateFunctionDesc(S + A);
2916 /* No, so look up the name in our global table. */
2917 symbol = strtab + sym.st_name;
2918 S_tmp = lookupSymbol( symbol );
2919 S = (Elf_Addr)S_tmp;
2921 #ifdef ELF_FUNCTION_DESC
2922 /* If a function, already a function descriptor - we would
2923 have to copy it to add an offset. */
2924 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
2925 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
2929 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
2932 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
2935 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
2936 (void*)P, (void*)S, (void*)A ));
2937 /* checkProddableBlock ( oc, (void*)P ); */
2941 switch (ELF_R_TYPE(info)) {
2942 # if defined(sparc_TARGET_ARCH)
2943 case R_SPARC_WDISP30:
2944 w1 = *pP & 0xC0000000;
2945 w2 = (Elf_Word)((value - P) >> 2);
2946 ASSERT((w2 & 0xC0000000) == 0);
2951 w1 = *pP & 0xFFC00000;
2952 w2 = (Elf_Word)(value >> 10);
2953 ASSERT((w2 & 0xFFC00000) == 0);
2959 w2 = (Elf_Word)(value & 0x3FF);
2960 ASSERT((w2 & ~0x3FF) == 0);
2964 /* According to the Sun documentation:
2966 This relocation type resembles R_SPARC_32, except it refers to an
2967 unaligned word. That is, the word to be relocated must be treated
2968 as four separate bytes with arbitrary alignment, not as a word
2969 aligned according to the architecture requirements.
2971 (JRS: which means that freeloading on the R_SPARC_32 case
2972 is probably wrong, but hey ...)
2976 w2 = (Elf_Word)value;
2979 # elif defined(ia64_TARGET_ARCH)
2980 case R_IA64_DIR64LSB:
2981 case R_IA64_FPTR64LSB:
2984 case R_IA64_PCREL64LSB:
2987 case R_IA64_SEGREL64LSB:
2988 addr = findElfSegment(ehdrC, value);
2991 case R_IA64_GPREL22:
2992 ia64_reloc_gprel22(P, value);
2994 case R_IA64_LTOFF22:
2995 case R_IA64_LTOFF22X:
2996 case R_IA64_LTOFF_FPTR22:
2997 addr = allocateGOTEntry(value);
2998 ia64_reloc_gprel22(P, addr);
3000 case R_IA64_PCREL21B:
3001 ia64_reloc_pcrel21(P, S, oc);
3004 /* This goes with R_IA64_LTOFF22X and points to the load to
3005 * convert into a move. We don't implement relaxation. */
3007 # elif defined(powerpc_TARGET_ARCH)
3008 case R_PPC_ADDR16_LO:
3009 *(Elf32_Half*) P = value;
3012 case R_PPC_ADDR16_HI:
3013 *(Elf32_Half*) P = value >> 16;
3016 case R_PPC_ADDR16_HA:
3017 *(Elf32_Half*) P = (value + 0x8000) >> 16;
3021 *(Elf32_Word *) P = value;
3025 *(Elf32_Word *) P = value - P;
3031 if( delta << 6 >> 6 != delta )
3033 value = makeJumpIsland( oc, ELF_R_SYM(info), value );
3036 if( value == 0 || delta << 6 >> 6 != delta )
3038 barf( "Unable to make ppcJumpIsland for #%d",
3044 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
3045 | (delta & 0x3fffffc);
3049 errorBelch("%s: unhandled ELF relocation(RelA) type %d\n",
3050 oc->fileName, ELF_R_TYPE(info));
3059 ocResolve_ELF ( ObjectCode* oc )
3063 Elf_Sym* stab = NULL;
3064 char* ehdrC = (char*)(oc->image);
3065 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
3066 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3067 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3069 /* first find "the" symbol table */
3070 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
3072 /* also go find the string table */
3073 strtab = findElfSection ( ehdrC, SHT_STRTAB );
3075 if (stab == NULL || strtab == NULL) {
3076 errorBelch("%s: can't find string or symbol table", oc->fileName);
3080 /* Process the relocation sections. */
3081 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
3083 /* Skip sections called ".rel.stab". These appear to contain
3084 relocation entries that, when done, make the stabs debugging
3085 info point at the right places. We ain't interested in all
3087 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
3090 if (shdr[shnum].sh_type == SHT_REL ) {
3091 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
3092 shnum, stab, strtab );
3096 if (shdr[shnum].sh_type == SHT_RELA) {
3097 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
3098 shnum, stab, strtab );
3103 /* Free the local symbol table; we won't need it again. */
3104 freeHashTable(oc->lochash, NULL);
3107 #if defined(powerpc_TARGET_ARCH)
3108 ocFlushInstructionCache( oc );
3116 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
3117 * at the front. The following utility functions pack and unpack instructions, and
3118 * take care of the most common relocations.
3121 #ifdef ia64_TARGET_ARCH
3124 ia64_extract_instruction(Elf64_Xword *target)
3127 int slot = (Elf_Addr)target & 3;
3128 (Elf_Addr)target &= ~3;
3136 return ((w1 >> 5) & 0x1ffffffffff);
3138 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
3142 barf("ia64_extract_instruction: invalid slot %p", target);
3147 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
3149 int slot = (Elf_Addr)target & 3;
3150 (Elf_Addr)target &= ~3;
3155 *target |= value << 5;
3158 *target |= value << 46;
3159 *(target+1) |= value >> 18;
3162 *(target+1) |= value << 23;
3168 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
3170 Elf64_Xword instruction;
3171 Elf64_Sxword rel_value;
3173 rel_value = value - gp_val;
3174 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
3175 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
3177 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3178 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
3179 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
3180 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
3181 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3182 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3186 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
3188 Elf64_Xword instruction;
3189 Elf64_Sxword rel_value;
3192 entry = allocatePLTEntry(value, oc);
3194 rel_value = (entry >> 4) - (target >> 4);
3195 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
3196 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
3198 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3199 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
3200 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3201 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3207 * PowerPC ELF specifics
3210 #ifdef powerpc_TARGET_ARCH
3212 static int ocAllocateJumpIslands_ELF( ObjectCode *oc )
3218 ehdr = (Elf_Ehdr *) oc->image;
3219 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
3221 for( i = 0; i < ehdr->e_shnum; i++ )
3222 if( shdr[i].sh_type == SHT_SYMTAB )
3225 if( i == ehdr->e_shnum )
3227 errorBelch( "This ELF file contains no symtab" );
3231 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
3233 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
3234 shdr[i].sh_entsize, sizeof( Elf_Sym ) );
3239 return ocAllocateJumpIslands( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
3242 #endif /* powerpc */
3246 /* --------------------------------------------------------------------------
3248 * ------------------------------------------------------------------------*/
3250 #if defined(OBJFORMAT_MACHO)
3253 Support for MachO linking on Darwin/MacOS X on PowerPC chips
3254 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3256 I hereby formally apologize for the hackish nature of this code.
3257 Things that need to be done:
3258 *) implement ocVerifyImage_MachO
3259 *) add still more sanity checks.
3262 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3264 struct mach_header *header = (struct mach_header *) oc->image;
3265 struct load_command *lc = (struct load_command *) (header + 1);
3268 for( i = 0; i < header->ncmds; i++ )
3270 if( lc->cmd == LC_DYSYMTAB )
3272 struct dysymtab_command *dsymLC = (struct dysymtab_command *) lc;
3274 if( !ocAllocateJumpIslands( oc, dsymLC->nundefsym,
3275 dsymLC->iundefsym ) )
3278 break; // there can be only one LC_DSYMTAB
3280 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3285 static int ocVerifyImage_MachO(ObjectCode* oc)
3287 // FIXME: do some verifying here
3291 static int resolveImports(
3294 struct symtab_command *symLC,
3295 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3296 unsigned long *indirectSyms,
3297 struct nlist *nlist)
3301 for(i=0;i*4<sect->size;i++)
3303 // according to otool, reserved1 contains the first index into the indirect symbol table
3304 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3305 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3308 if((symbol->n_type & N_TYPE) == N_UNDF
3309 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3310 addr = (void*) (symbol->n_value);
3311 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3314 addr = lookupSymbol(nm);
3317 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3321 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3322 ((void**)(image + sect->offset))[i] = addr;
3328 static char* relocateAddress(
3331 struct section* sections,
3332 unsigned long address)
3335 for(i = 0; i < nSections; i++)
3337 if(sections[i].addr <= address
3338 && address < sections[i].addr + sections[i].size)
3340 return oc->image + sections[i].offset + address - sections[i].addr;
3343 barf("Invalid Mach-O file:"
3344 "Address out of bounds while relocating object file");
3348 static int relocateSection(
3351 struct symtab_command *symLC, struct nlist *nlist,
3352 int nSections, struct section* sections, struct section *sect)
3354 struct relocation_info *relocs;
3357 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3359 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3363 relocs = (struct relocation_info*) (image + sect->reloff);
3367 if(relocs[i].r_address & R_SCATTERED)
3369 struct scattered_relocation_info *scat =
3370 (struct scattered_relocation_info*) &relocs[i];
3374 if(scat->r_length == 2)
3376 unsigned long word = 0;
3377 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3378 checkProddableBlock(oc,wordPtr);
3380 // Step 1: Figure out what the relocated value should be
3381 if(scat->r_type == GENERIC_RELOC_VANILLA)
3383 word = *wordPtr + (unsigned long) relocateAddress(
3390 else if(scat->r_type == PPC_RELOC_SECTDIFF
3391 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3392 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3393 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3395 struct scattered_relocation_info *pair =
3396 (struct scattered_relocation_info*) &relocs[i+1];
3398 if(!pair->r_scattered || pair->r_type != PPC_RELOC_PAIR)
3399 barf("Invalid Mach-O file: "
3400 "PPC_RELOC_*_SECTDIFF not followed by PPC_RELOC_PAIR");
3402 word = (unsigned long)
3403 (relocateAddress(oc, nSections, sections, scat->r_value)
3404 - relocateAddress(oc, nSections, sections, pair->r_value));
3407 else if(scat->r_type == PPC_RELOC_HI16
3408 || scat->r_type == PPC_RELOC_LO16
3409 || scat->r_type == PPC_RELOC_HA16
3410 || scat->r_type == PPC_RELOC_LO14)
3411 { // these are generated by label+offset things
3412 struct relocation_info *pair = &relocs[i+1];
3413 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
3414 barf("Invalid Mach-O file: "
3415 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
3417 if(scat->r_type == PPC_RELOC_LO16)
3419 word = ((unsigned short*) wordPtr)[1];
3420 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3422 else if(scat->r_type == PPC_RELOC_LO14)
3424 barf("Unsupported Relocation: PPC_RELOC_LO14");
3425 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
3426 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3428 else if(scat->r_type == PPC_RELOC_HI16)
3430 word = ((unsigned short*) wordPtr)[1] << 16;
3431 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3433 else if(scat->r_type == PPC_RELOC_HA16)
3435 word = ((unsigned short*) wordPtr)[1] << 16;
3436 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3440 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
3446 continue; // ignore the others
3448 if(scat->r_type == GENERIC_RELOC_VANILLA
3449 || scat->r_type == PPC_RELOC_SECTDIFF)
3453 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
3455 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3457 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
3459 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3461 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
3463 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3464 + ((word & (1<<15)) ? 1 : 0);
3469 continue; // FIXME: I hope it's OK to ignore all the others.
3473 struct relocation_info *reloc = &relocs[i];
3474 if(reloc->r_pcrel && !reloc->r_extern)
3477 if(reloc->r_length == 2)
3479 unsigned long word = 0;
3480 unsigned long jumpIsland = 0;
3481 long offsetToJumpIsland;
3483 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3484 checkProddableBlock(oc,wordPtr);
3486 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3490 else if(reloc->r_type == PPC_RELOC_LO16)
3492 word = ((unsigned short*) wordPtr)[1];
3493 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3495 else if(reloc->r_type == PPC_RELOC_HI16)
3497 word = ((unsigned short*) wordPtr)[1] << 16;
3498 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3500 else if(reloc->r_type == PPC_RELOC_HA16)
3502 word = ((unsigned short*) wordPtr)[1] << 16;
3503 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3505 else if(reloc->r_type == PPC_RELOC_BR24)
3508 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
3512 if(!reloc->r_extern)
3515 sections[reloc->r_symbolnum-1].offset
3516 - sections[reloc->r_symbolnum-1].addr
3523 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3524 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3525 unsigned long symbolAddress = (unsigned long) (lookupSymbol(nm));
3528 errorBelch("\nunknown symbol `%s'", nm);
3534 // In the .o file, this should be a relative jump to NULL
3535 // and we'll change it to a jump to a relative jump to the symbol
3536 ASSERT(-word == reloc->r_address);
3537 word = symbolAddress;
3538 jumpIsland = makeJumpIsland(oc,reloc->r_symbolnum,word);
3539 word -= ((long)image) + sect->offset + reloc->r_address;
3542 offsetToJumpIsland = jumpIsland
3543 - (((long)image) + sect->offset + reloc->r_address);
3548 word += symbolAddress;
3552 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3557 else if(reloc->r_type == PPC_RELOC_LO16)
3559 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3562 else if(reloc->r_type == PPC_RELOC_HI16)
3564 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3567 else if(reloc->r_type == PPC_RELOC_HA16)
3569 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3570 + ((word & (1<<15)) ? 1 : 0);
3573 else if(reloc->r_type == PPC_RELOC_BR24)
3575 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3577 // The branch offset is too large.
3578 // Therefore, we try to use a jump island.
3580 barf("unconditional relative branch out of range: "
3581 "no jump island available");
3583 word = offsetToJumpIsland;
3584 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3585 barf("unconditional relative branch out of range: "
3586 "jump island out of range");
3588 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3592 barf("\nunknown relocation %d",reloc->r_type);
3599 static int ocGetNames_MachO(ObjectCode* oc)
3601 char *image = (char*) oc->image;
3602 struct mach_header *header = (struct mach_header*) image;
3603 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3604 unsigned i,curSymbol;
3605 struct segment_command *segLC = NULL;
3606 struct section *sections;
3607 struct symtab_command *symLC = NULL;
3608 struct dysymtab_command *dsymLC = NULL;
3609 struct nlist *nlist;
3610 unsigned long commonSize = 0;
3611 char *commonStorage = NULL;
3612 unsigned long commonCounter;
3614 for(i=0;i<header->ncmds;i++)
3616 if(lc->cmd == LC_SEGMENT)
3617 segLC = (struct segment_command*) lc;
3618 else if(lc->cmd == LC_SYMTAB)
3619 symLC = (struct symtab_command*) lc;
3620 else if(lc->cmd == LC_DYSYMTAB)
3621 dsymLC = (struct dysymtab_command*) lc;
3622 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3625 sections = (struct section*) (segLC+1);
3626 nlist = (struct nlist*) (image + symLC->symoff);
3628 for(i=0;i<segLC->nsects;i++)
3630 if(sections[i].size == 0)
3633 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
3635 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
3636 "ocGetNames_MachO(common symbols)");
3637 sections[i].offset = zeroFillArea - image;
3640 if(!strcmp(sections[i].sectname,"__text"))
3641 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3642 (void*) (image + sections[i].offset),
3643 (void*) (image + sections[i].offset + sections[i].size));
3644 else if(!strcmp(sections[i].sectname,"__const"))
3645 addSection(oc, SECTIONKIND_RWDATA,
3646 (void*) (image + sections[i].offset),
3647 (void*) (image + sections[i].offset + sections[i].size));
3648 else if(!strcmp(sections[i].sectname,"__data"))
3649 addSection(oc, SECTIONKIND_RWDATA,
3650 (void*) (image + sections[i].offset),
3651 (void*) (image + sections[i].offset + sections[i].size));
3652 else if(!strcmp(sections[i].sectname,"__bss")
3653 || !strcmp(sections[i].sectname,"__common"))
3654 addSection(oc, SECTIONKIND_RWDATA,
3655 (void*) (image + sections[i].offset),
3656 (void*) (image + sections[i].offset + sections[i].size));
3658 addProddableBlock(oc, (void*) (image + sections[i].offset),
3662 // count external symbols defined here
3666 for(i = dsymLC->iextdefsym;
3667 i < dsymLC->iextdefsym + dsymLC->nextdefsym;
3670 if((nlist[i].n_type & N_TYPE) == N_SECT)
3676 for(i=0;i<symLC->nsyms;i++)
3678 if((nlist[i].n_type & N_TYPE) == N_UNDF
3679 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3681 commonSize += nlist[i].n_value;
3686 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3687 "ocGetNames_MachO(oc->symbols)");
3691 // insert symbols into hash table
3692 for(i = dsymLC->iextdefsym, curSymbol = 0;
3693 i < dsymLC->iextdefsym + dsymLC->nextdefsym;
3696 if((nlist[i].n_type & N_TYPE) == N_SECT)
3698 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3699 ghciInsertStrHashTable(oc->fileName, symhash, nm,
3701 + sections[nlist[i].n_sect-1].offset
3702 - sections[nlist[i].n_sect-1].addr
3703 + nlist[i].n_value);
3704 oc->symbols[curSymbol++] = nm;
3708 // insert local symbols into lochash
3709 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3711 if((nlist[i].n_type & N_TYPE) == N_SECT)
3713 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3714 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm,
3716 + sections[nlist[i].n_sect-1].offset
3717 - sections[nlist[i].n_sect-1].addr
3718 + nlist[i].n_value);
3723 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3724 commonCounter = (unsigned long)commonStorage;
3727 for(i=0;i<symLC->nsyms;i++)
3729 if((nlist[i].n_type & N_TYPE) == N_UNDF
3730 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3732 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3733 unsigned long sz = nlist[i].n_value;
3735 nlist[i].n_value = commonCounter;
3737 ghciInsertStrHashTable(oc->fileName, symhash, nm,
3738 (void*)commonCounter);
3739 oc->symbols[curSymbol++] = nm;
3741 commonCounter += sz;
3748 static int ocResolve_MachO(ObjectCode* oc)
3750 char *image = (char*) oc->image;
3751 struct mach_header *header = (struct mach_header*) image;
3752 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3754 struct segment_command *segLC = NULL;
3755 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3756 struct symtab_command *symLC = NULL;
3757 struct dysymtab_command *dsymLC = NULL;
3758 struct nlist *nlist;
3760 for(i=0;i<header->ncmds;i++)
3762 if(lc->cmd == LC_SEGMENT)
3763 segLC = (struct segment_command*) lc;
3764 else if(lc->cmd == LC_SYMTAB)
3765 symLC = (struct symtab_command*) lc;
3766 else if(lc->cmd == LC_DYSYMTAB)
3767 dsymLC = (struct dysymtab_command*) lc;
3768 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3771 sections = (struct section*) (segLC+1);
3772 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
3775 for(i=0;i<segLC->nsects;i++)
3777 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3778 la_ptrs = §ions[i];
3779 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3780 nl_ptrs = §ions[i];
3785 unsigned long *indirectSyms
3786 = (unsigned long*) (image + dsymLC->indirectsymoff);
3789 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
3792 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
3796 for(i=0;i<segLC->nsects;i++)
3798 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
3802 /* Free the local symbol table; we won't need it again. */
3803 freeHashTable(oc->lochash, NULL);
3806 #if defined (powerpc_TARGET_ARCH)
3807 ocFlushInstructionCache( oc );
3814 * The Mach-O object format uses leading underscores. But not everywhere.
3815 * There is a small number of runtime support functions defined in
3816 * libcc_dynamic.a whose name does not have a leading underscore.
3817 * As a consequence, we can't get their address from C code.
3818 * We have to use inline assembler just to take the address of a function.
3822 static void machoInitSymbolsWithoutUnderscore()
3824 extern void* symbolsWithoutUnderscore[];
3825 void **p = symbolsWithoutUnderscore;
3826 __asm__ volatile(".data\n_symbolsWithoutUnderscore:");
3830 __asm__ volatile(".long " # x);
3832 RTS_MACHO_NOUNDERLINE_SYMBOLS
3834 __asm__ volatile(".text");
3838 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
3840 RTS_MACHO_NOUNDERLINE_SYMBOLS