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) \
392 SymX(blockAsyncExceptionszh_fast) \
394 SymX(closure_flags) \
396 SymX(cmpIntegerzh_fast) \
397 SymX(cmpIntegerIntzh_fast) \
398 SymX(complementIntegerzh_fast) \
399 SymX(createAdjustor) \
400 SymX(decodeDoublezh_fast) \
401 SymX(decodeFloatzh_fast) \
404 SymX(deRefWeakzh_fast) \
405 SymX(deRefStablePtrzh_fast) \
406 SymX(divExactIntegerzh_fast) \
407 SymX(divModIntegerzh_fast) \
410 SymX(forkOS_createThread) \
411 SymX(freeHaskellFunctionPtr) \
412 SymX(freeStablePtr) \
413 SymX(gcdIntegerzh_fast) \
414 SymX(gcdIntegerIntzh_fast) \
415 SymX(gcdIntzh_fast) \
420 SymX(int2Integerzh_fast) \
421 SymX(integer2Intzh_fast) \
422 SymX(integer2Wordzh_fast) \
423 SymX(isCurrentThreadBoundzh_fast) \
424 SymX(isDoubleDenormalized) \
425 SymX(isDoubleInfinite) \
427 SymX(isDoubleNegativeZero) \
428 SymX(isEmptyMVarzh_fast) \
429 SymX(isFloatDenormalized) \
430 SymX(isFloatInfinite) \
432 SymX(isFloatNegativeZero) \
433 SymX(killThreadzh_fast) \
436 SymX(makeStablePtrzh_fast) \
437 SymX(minusIntegerzh_fast) \
438 SymX(mkApUpd0zh_fast) \
439 SymX(myThreadIdzh_fast) \
440 SymX(labelThreadzh_fast) \
441 SymX(newArrayzh_fast) \
442 SymX(newBCOzh_fast) \
443 SymX(newByteArrayzh_fast) \
444 SymX_redirect(newCAF, newDynCAF) \
445 SymX(newMVarzh_fast) \
446 SymX(newMutVarzh_fast) \
447 SymX(atomicModifyMutVarzh_fast) \
448 SymX(newPinnedByteArrayzh_fast) \
449 SymX(orIntegerzh_fast) \
451 SymX(performMajorGC) \
452 SymX(plusIntegerzh_fast) \
455 SymX(putMVarzh_fast) \
456 SymX(quotIntegerzh_fast) \
457 SymX(quotRemIntegerzh_fast) \
459 SymX(raiseIOzh_fast) \
460 SymX(remIntegerzh_fast) \
461 SymX(resetNonBlockingFd) \
465 SymX(rts_checkSchedStatus) \
468 SymX(rts_evalLazyIO) \
469 SymX(rts_evalStableIO) \
473 SymX(rts_getDouble) \
478 SymX(rts_getFunPtr) \
479 SymX(rts_getStablePtr) \
480 SymX(rts_getThreadId) \
482 SymX(rts_getWord32) \
495 SymX(rts_mkStablePtr) \
503 SymX(rtsSupportsBoundThreads) \
505 SymX(__hscore_get_saved_termios) \
506 SymX(__hscore_set_saved_termios) \
508 SymX(startupHaskell) \
509 SymX(shutdownHaskell) \
510 SymX(shutdownHaskellAndExit) \
511 SymX(stable_ptr_table) \
512 SymX(stackOverflow) \
513 SymX(stg_CAF_BLACKHOLE_info) \
514 SymX(stg_BLACKHOLE_BQ_info) \
515 SymX(awakenBlockedQueue) \
516 SymX(stg_CHARLIKE_closure) \
517 SymX(stg_EMPTY_MVAR_info) \
518 SymX(stg_IND_STATIC_info) \
519 SymX(stg_INTLIKE_closure) \
520 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
521 SymX(stg_WEAK_info) \
522 SymX(stg_ap_0_info) \
523 SymX(stg_ap_v_info) \
524 SymX(stg_ap_f_info) \
525 SymX(stg_ap_d_info) \
526 SymX(stg_ap_l_info) \
527 SymX(stg_ap_n_info) \
528 SymX(stg_ap_p_info) \
529 SymX(stg_ap_pv_info) \
530 SymX(stg_ap_pp_info) \
531 SymX(stg_ap_ppv_info) \
532 SymX(stg_ap_ppp_info) \
533 SymX(stg_ap_pppv_info) \
534 SymX(stg_ap_pppp_info) \
535 SymX(stg_ap_ppppp_info) \
536 SymX(stg_ap_pppppp_info) \
537 SymX(stg_ap_1_upd_info) \
538 SymX(stg_ap_2_upd_info) \
539 SymX(stg_ap_3_upd_info) \
540 SymX(stg_ap_4_upd_info) \
541 SymX(stg_ap_5_upd_info) \
542 SymX(stg_ap_6_upd_info) \
543 SymX(stg_ap_7_upd_info) \
545 SymX(stg_sel_0_upd_info) \
546 SymX(stg_sel_10_upd_info) \
547 SymX(stg_sel_11_upd_info) \
548 SymX(stg_sel_12_upd_info) \
549 SymX(stg_sel_13_upd_info) \
550 SymX(stg_sel_14_upd_info) \
551 SymX(stg_sel_15_upd_info) \
552 SymX(stg_sel_1_upd_info) \
553 SymX(stg_sel_2_upd_info) \
554 SymX(stg_sel_3_upd_info) \
555 SymX(stg_sel_4_upd_info) \
556 SymX(stg_sel_5_upd_info) \
557 SymX(stg_sel_6_upd_info) \
558 SymX(stg_sel_7_upd_info) \
559 SymX(stg_sel_8_upd_info) \
560 SymX(stg_sel_9_upd_info) \
561 SymX(stg_upd_frame_info) \
562 SymX(suspendThread) \
563 SymX(takeMVarzh_fast) \
564 SymX(timesIntegerzh_fast) \
565 SymX(tryPutMVarzh_fast) \
566 SymX(tryTakeMVarzh_fast) \
567 SymX(unblockAsyncExceptionszh_fast) \
569 SymX(unsafeThawArrayzh_fast) \
570 SymX(waitReadzh_fast) \
571 SymX(waitWritezh_fast) \
572 SymX(word2Integerzh_fast) \
573 SymX(xorIntegerzh_fast) \
576 #ifdef SUPPORT_LONG_LONGS
577 #define RTS_LONG_LONG_SYMS \
578 SymX(int64ToIntegerzh_fast) \
579 SymX(word64ToIntegerzh_fast)
581 #define RTS_LONG_LONG_SYMS /* nothing */
584 // 64-bit support functions in libgcc.a
585 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
586 #define RTS_LIBGCC_SYMBOLS \
595 #elif defined(ia64_TARGET_ARCH)
596 #define RTS_LIBGCC_SYMBOLS \
604 #define RTS_LIBGCC_SYMBOLS
607 #ifdef darwin_TARGET_OS
608 // Symbols that don't have a leading underscore
609 // on Mac OS X. They have to receive special treatment,
610 // see machoInitSymbolsWithoutUnderscore()
611 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
616 /* entirely bogus claims about types of these symbols */
617 #define Sym(vvv) extern void vvv(void);
618 #define SymX(vvv) /**/
619 #define SymX_redirect(vvv,xxx) /**/
623 RTS_POSIX_ONLY_SYMBOLS
624 RTS_MINGW_ONLY_SYMBOLS
625 RTS_CYGWIN_ONLY_SYMBOLS
631 #ifdef LEADING_UNDERSCORE
632 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
634 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
637 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
639 #define SymX(vvv) Sym(vvv)
641 // SymX_redirect allows us to redirect references to one symbol to
642 // another symbol. See newCAF/newDynCAF for an example.
643 #define SymX_redirect(vvv,xxx) \
644 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
647 static RtsSymbolVal rtsSyms[] = {
651 RTS_POSIX_ONLY_SYMBOLS
652 RTS_MINGW_ONLY_SYMBOLS
653 RTS_CYGWIN_ONLY_SYMBOLS
655 { 0, 0 } /* sentinel */
658 /* -----------------------------------------------------------------------------
659 * Insert symbols into hash tables, checking for duplicates.
661 static void ghciInsertStrHashTable ( char* obj_name,
667 if (lookupHashTable(table, (StgWord)key) == NULL)
669 insertStrHashTable(table, (StgWord)key, data);
674 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
676 "whilst processing object file\n"
678 "This could be caused by:\n"
679 " * Loading two different object files which export the same symbol\n"
680 " * Specifying the same object file twice on the GHCi command line\n"
681 " * An incorrect `package.conf' entry, causing some object to be\n"
683 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
692 /* -----------------------------------------------------------------------------
693 * initialize the object linker
697 static int linker_init_done = 0 ;
699 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
700 static void *dl_prog_handle;
703 /* dlopen(NULL,..) doesn't work so we grab libc explicitly */
704 #if defined(openbsd_TARGET_OS)
705 static void *dl_libc_handle;
713 /* Make initLinker idempotent, so we can call it
714 before evey relevant operation; that means we
715 don't need to initialise the linker separately */
716 if (linker_init_done == 1) { return; } else {
717 linker_init_done = 1;
720 symhash = allocStrHashTable();
722 /* populate the symbol table with stuff from the RTS */
723 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
724 ghciInsertStrHashTable("(GHCi built-in symbols)",
725 symhash, sym->lbl, sym->addr);
727 # if defined(OBJFORMAT_MACHO)
728 machoInitSymbolsWithoutUnderscore();
731 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
732 # if defined(RTLD_DEFAULT)
733 dl_prog_handle = RTLD_DEFAULT;
735 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
736 # if defined(openbsd_TARGET_OS)
737 dl_libc_handle = dlopen("libc.so", RTLD_LAZY);
739 # endif // RTLD_DEFAULT
743 /* -----------------------------------------------------------------------------
744 * Loading DLL or .so dynamic libraries
745 * -----------------------------------------------------------------------------
747 * Add a DLL from which symbols may be found. In the ELF case, just
748 * do RTLD_GLOBAL-style add, so no further messing around needs to
749 * happen in order that symbols in the loaded .so are findable --
750 * lookupSymbol() will subsequently see them by dlsym on the program's
751 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
753 * In the PEi386 case, open the DLLs and put handles to them in a
754 * linked list. When looking for a symbol, try all handles in the
755 * list. This means that we need to load even DLLs that are guaranteed
756 * to be in the ghc.exe image already, just so we can get a handle
757 * to give to loadSymbol, so that we can find the symbols. For such
758 * libraries, the LoadLibrary call should be a no-op except for returning
763 #if defined(OBJFORMAT_PEi386)
764 /* A record for storing handles into DLLs. */
769 struct _OpenedDLL* next;
774 /* A list thereof. */
775 static OpenedDLL* opened_dlls = NULL;
779 addDLL( char *dll_name )
781 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
782 /* ------------------- ELF DLL loader ------------------- */
788 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
791 /* dlopen failed; return a ptr to the error msg. */
793 if (errmsg == NULL) errmsg = "addDLL: unknown error";
800 # elif defined(OBJFORMAT_PEi386)
801 /* ------------------- Win32 DLL loader ------------------- */
809 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
811 /* See if we've already got it, and ignore if so. */
812 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
813 if (0 == strcmp(o_dll->name, dll_name))
817 /* The file name has no suffix (yet) so that we can try
818 both foo.dll and foo.drv
820 The documentation for LoadLibrary says:
821 If no file name extension is specified in the lpFileName
822 parameter, the default library extension .dll is
823 appended. However, the file name string can include a trailing
824 point character (.) to indicate that the module name has no
827 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
828 sprintf(buf, "%s.DLL", dll_name);
829 instance = LoadLibrary(buf);
830 if (instance == NULL) {
831 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
832 instance = LoadLibrary(buf);
833 if (instance == NULL) {
836 /* LoadLibrary failed; return a ptr to the error msg. */
837 return "addDLL: unknown error";
842 /* Add this DLL to the list of DLLs in which to search for symbols. */
843 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
844 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
845 strcpy(o_dll->name, dll_name);
846 o_dll->instance = instance;
847 o_dll->next = opened_dlls;
852 barf("addDLL: not implemented on this platform");
856 /* -----------------------------------------------------------------------------
857 * lookup a symbol in the hash table
860 lookupSymbol( char *lbl )
864 ASSERT(symhash != NULL);
865 val = lookupStrHashTable(symhash, lbl);
868 # if defined(OBJFORMAT_ELF)
869 # if defined(openbsd_TARGET_OS)
870 val = dlsym(dl_prog_handle, lbl);
871 return (val != NULL) ? val : dlsym(dl_libc_handle,lbl);
872 # else /* not openbsd */
873 return dlsym(dl_prog_handle, lbl);
875 # elif defined(OBJFORMAT_MACHO)
876 if(NSIsSymbolNameDefined(lbl)) {
877 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
878 return NSAddressOfSymbol(symbol);
882 # elif defined(OBJFORMAT_PEi386)
885 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
886 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
888 /* HACK: if the name has an initial underscore, try stripping
889 it off & look that up first. I've yet to verify whether there's
890 a Rule that governs whether an initial '_' *should always* be
891 stripped off when mapping from import lib name to the DLL name.
893 sym = GetProcAddress(o_dll->instance, (lbl+1));
895 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
899 sym = GetProcAddress(o_dll->instance, lbl);
901 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
916 __attribute((unused))
918 lookupLocalSymbol( ObjectCode* oc, char *lbl )
922 val = lookupStrHashTable(oc->lochash, lbl);
932 /* -----------------------------------------------------------------------------
933 * Debugging aid: look in GHCi's object symbol tables for symbols
934 * within DELTA bytes of the specified address, and show their names.
937 void ghci_enquire ( char* addr );
939 void ghci_enquire ( char* addr )
944 const int DELTA = 64;
949 for (oc = objects; oc; oc = oc->next) {
950 for (i = 0; i < oc->n_symbols; i++) {
951 sym = oc->symbols[i];
952 if (sym == NULL) continue;
953 // debugBelch("enquire %p %p\n", sym, oc->lochash);
955 if (oc->lochash != NULL) {
956 a = lookupStrHashTable(oc->lochash, sym);
959 a = lookupStrHashTable(symhash, sym);
962 // debugBelch("ghci_enquire: can't find %s\n", sym);
964 else if (addr-DELTA <= a && a <= addr+DELTA) {
965 debugBelch("%p + %3d == `%s'\n", addr, a - addr, sym);
972 #ifdef ia64_TARGET_ARCH
973 static unsigned int PLTSize(void);
976 /* -----------------------------------------------------------------------------
977 * Load an obj (populate the global symbol table, but don't resolve yet)
979 * Returns: 1 if ok, 0 on error.
982 loadObj( char *path )
989 void *map_addr = NULL;
996 /* debugBelch("loadObj %s\n", path ); */
998 /* Check that we haven't already loaded this object.
999 Ignore requests to load multiple times */
1003 for (o = objects; o; o = o->next) {
1004 if (0 == strcmp(o->fileName, path)) {
1006 break; /* don't need to search further */
1010 IF_DEBUG(linker, debugBelch(
1011 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1012 "same object file twice:\n"
1014 "GHCi will ignore this, but be warned.\n"
1016 return 1; /* success */
1020 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
1022 # if defined(OBJFORMAT_ELF)
1023 oc->formatName = "ELF";
1024 # elif defined(OBJFORMAT_PEi386)
1025 oc->formatName = "PEi386";
1026 # elif defined(OBJFORMAT_MACHO)
1027 oc->formatName = "Mach-O";
1030 barf("loadObj: not implemented on this platform");
1033 r = stat(path, &st);
1034 if (r == -1) { return 0; }
1036 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1037 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1038 strcpy(oc->fileName, path);
1040 oc->fileSize = st.st_size;
1042 oc->sections = NULL;
1043 oc->lochash = allocStrHashTable();
1044 oc->proddables = NULL;
1046 /* chain it onto the list of objects */
1051 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1053 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1055 #if defined(openbsd_TARGET_OS)
1056 fd = open(path, O_RDONLY, S_IRUSR);
1058 fd = open(path, O_RDONLY);
1061 barf("loadObj: can't open `%s'", path);
1063 pagesize = getpagesize();
1065 #ifdef ia64_TARGET_ARCH
1066 /* The PLT needs to be right before the object */
1067 n = ROUND_UP(PLTSize(), pagesize);
1068 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1069 if (oc->plt == MAP_FAILED)
1070 barf("loadObj: can't allocate PLT");
1073 map_addr = oc->plt + n;
1076 n = ROUND_UP(oc->fileSize, pagesize);
1077 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1078 if (oc->image == MAP_FAILED)
1079 barf("loadObj: can't map `%s'", path);
1083 #else /* !USE_MMAP */
1085 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
1087 /* load the image into memory */
1088 f = fopen(path, "rb");
1090 barf("loadObj: can't read `%s'", path);
1092 n = fread ( oc->image, 1, oc->fileSize, f );
1093 if (n != oc->fileSize)
1094 barf("loadObj: error whilst reading `%s'", path);
1098 #endif /* USE_MMAP */
1100 # if defined(OBJFORMAT_MACHO)
1101 r = ocAllocateJumpIslands_MachO ( oc );
1102 if (!r) { return r; }
1103 # elif defined(OBJFORMAT_ELF) && defined(powerpc_TARGET_ARCH)
1104 r = ocAllocateJumpIslands_ELF ( oc );
1105 if (!r) { return r; }
1108 /* verify the in-memory image */
1109 # if defined(OBJFORMAT_ELF)
1110 r = ocVerifyImage_ELF ( oc );
1111 # elif defined(OBJFORMAT_PEi386)
1112 r = ocVerifyImage_PEi386 ( oc );
1113 # elif defined(OBJFORMAT_MACHO)
1114 r = ocVerifyImage_MachO ( oc );
1116 barf("loadObj: no verify method");
1118 if (!r) { return r; }
1120 /* build the symbol list for this image */
1121 # if defined(OBJFORMAT_ELF)
1122 r = ocGetNames_ELF ( oc );
1123 # elif defined(OBJFORMAT_PEi386)
1124 r = ocGetNames_PEi386 ( oc );
1125 # elif defined(OBJFORMAT_MACHO)
1126 r = ocGetNames_MachO ( oc );
1128 barf("loadObj: no getNames method");
1130 if (!r) { return r; }
1132 /* loaded, but not resolved yet */
1133 oc->status = OBJECT_LOADED;
1138 /* -----------------------------------------------------------------------------
1139 * resolve all the currently unlinked objects in memory
1141 * Returns: 1 if ok, 0 on error.
1151 for (oc = objects; oc; oc = oc->next) {
1152 if (oc->status != OBJECT_RESOLVED) {
1153 # if defined(OBJFORMAT_ELF)
1154 r = ocResolve_ELF ( oc );
1155 # elif defined(OBJFORMAT_PEi386)
1156 r = ocResolve_PEi386 ( oc );
1157 # elif defined(OBJFORMAT_MACHO)
1158 r = ocResolve_MachO ( oc );
1160 barf("resolveObjs: not implemented on this platform");
1162 if (!r) { return r; }
1163 oc->status = OBJECT_RESOLVED;
1169 /* -----------------------------------------------------------------------------
1170 * delete an object from the pool
1173 unloadObj( char *path )
1175 ObjectCode *oc, *prev;
1177 ASSERT(symhash != NULL);
1178 ASSERT(objects != NULL);
1183 for (oc = objects; oc; prev = oc, oc = oc->next) {
1184 if (!strcmp(oc->fileName,path)) {
1186 /* Remove all the mappings for the symbols within this
1191 for (i = 0; i < oc->n_symbols; i++) {
1192 if (oc->symbols[i] != NULL) {
1193 removeStrHashTable(symhash, oc->symbols[i], NULL);
1201 prev->next = oc->next;
1204 /* We're going to leave this in place, in case there are
1205 any pointers from the heap into it: */
1206 /* stgFree(oc->image); */
1207 stgFree(oc->fileName);
1208 stgFree(oc->symbols);
1209 stgFree(oc->sections);
1210 /* The local hash table should have been freed at the end
1211 of the ocResolve_ call on it. */
1212 ASSERT(oc->lochash == NULL);
1218 errorBelch("unloadObj: can't find `%s' to unload", path);
1222 /* -----------------------------------------------------------------------------
1223 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1224 * which may be prodded during relocation, and abort if we try and write
1225 * outside any of these.
1227 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1230 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1231 /* debugBelch("aPB %p %p %d\n", oc, start, size); */
1235 pb->next = oc->proddables;
1236 oc->proddables = pb;
1239 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1242 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1243 char* s = (char*)(pb->start);
1244 char* e = s + pb->size - 1;
1245 char* a = (char*)addr;
1246 /* Assumes that the biggest fixup involves a 4-byte write. This
1247 probably needs to be changed to 8 (ie, +7) on 64-bit
1249 if (a >= s && (a+3) <= e) return;
1251 barf("checkProddableBlock: invalid fixup in runtime linker");
1254 /* -----------------------------------------------------------------------------
1255 * Section management.
1257 static void addSection ( ObjectCode* oc, SectionKind kind,
1258 void* start, void* end )
1260 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1264 s->next = oc->sections;
1267 debugBelch("addSection: %p-%p (size %d), kind %d\n",
1268 start, ((char*)end)-1, end - start + 1, kind );
1273 /* --------------------------------------------------------------------------
1274 * PowerPC specifics (jump islands)
1275 * ------------------------------------------------------------------------*/
1277 #if defined(powerpc_TARGET_ARCH)
1280 ocAllocateJumpIslands
1282 Allocate additional space at the end of the object file image to make room
1285 PowerPC relative branch instructions have a 24 bit displacement field.
1286 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
1287 If a particular imported symbol is outside this range, we have to redirect
1288 the jump to a short piece of new code that just loads the 32bit absolute
1289 address and jumps there.
1290 This function just allocates space for one 16 byte ppcJumpIsland for every
1291 undefined symbol in the object file. The code for the islands is filled in by
1292 makeJumpIsland below.
1295 static int ocAllocateJumpIslands( ObjectCode* oc, int count, int first )
1302 #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined
1304 // round up to the nearest 4
1305 aligned = (oc->fileSize + 3) & ~3;
1307 oc->image = stgReallocBytes( oc->image,
1308 aligned + sizeof( ppcJumpIsland ) * count,
1309 "ocAllocateJumpIslands" );
1310 oc->jump_islands = (ppcJumpIsland *) (((char *) oc->image) + aligned);
1311 memset( oc->jump_islands, 0, sizeof( ppcJumpIsland ) * count );
1314 oc->jump_islands = NULL;
1316 oc->island_start_symbol = first;
1317 oc->n_islands = count;
1322 static unsigned long makeJumpIsland( ObjectCode* oc,
1323 unsigned long symbolNumber,
1324 unsigned long target )
1326 ppcJumpIsland *island;
1328 if( symbolNumber < oc->island_start_symbol ||
1329 symbolNumber - oc->island_start_symbol > oc->n_islands)
1332 island = &oc->jump_islands[symbolNumber - oc->island_start_symbol];
1334 // lis r12, hi16(target)
1335 island->lis_r12 = 0x3d80;
1336 island->hi_addr = target >> 16;
1338 // ori r12, r12, lo16(target)
1339 island->ori_r12_r12 = 0x618c;
1340 island->lo_addr = target & 0xffff;
1343 island->mtctr_r12 = 0x7d8903a6;
1346 island->bctr = 0x4e800420;
1348 return (unsigned long) island;
1352 ocFlushInstructionCache
1354 Flush the data & instruction caches.
1355 Because the PPC has split data/instruction caches, we have to
1356 do that whenever we modify code at runtime.
1359 static void ocFlushInstructionCache( ObjectCode *oc )
1361 int n = (oc->fileSize + sizeof( ppcJumpIsland ) * oc->n_islands + 3) / 4;
1362 unsigned long *p = (unsigned long *) oc->image;
1366 __asm__ volatile ( "dcbf 0,%0\n\t"
1374 __asm__ volatile ( "sync\n\t"
1380 /* --------------------------------------------------------------------------
1381 * PEi386 specifics (Win32 targets)
1382 * ------------------------------------------------------------------------*/
1384 /* The information for this linker comes from
1385 Microsoft Portable Executable
1386 and Common Object File Format Specification
1387 revision 5.1 January 1998
1388 which SimonM says comes from the MS Developer Network CDs.
1390 It can be found there (on older CDs), but can also be found
1393 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1395 (this is Rev 6.0 from February 1999).
1397 Things move, so if that fails, try searching for it via
1399 http://www.google.com/search?q=PE+COFF+specification
1401 The ultimate reference for the PE format is the Winnt.h
1402 header file that comes with the Platform SDKs; as always,
1403 implementations will drift wrt their documentation.
1405 A good background article on the PE format is Matt Pietrek's
1406 March 1994 article in Microsoft System Journal (MSJ)
1407 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1408 Win32 Portable Executable File Format." The info in there
1409 has recently been updated in a two part article in
1410 MSDN magazine, issues Feb and March 2002,
1411 "Inside Windows: An In-Depth Look into the Win32 Portable
1412 Executable File Format"
1414 John Levine's book "Linkers and Loaders" contains useful
1419 #if defined(OBJFORMAT_PEi386)
1423 typedef unsigned char UChar;
1424 typedef unsigned short UInt16;
1425 typedef unsigned int UInt32;
1432 UInt16 NumberOfSections;
1433 UInt32 TimeDateStamp;
1434 UInt32 PointerToSymbolTable;
1435 UInt32 NumberOfSymbols;
1436 UInt16 SizeOfOptionalHeader;
1437 UInt16 Characteristics;
1441 #define sizeof_COFF_header 20
1448 UInt32 VirtualAddress;
1449 UInt32 SizeOfRawData;
1450 UInt32 PointerToRawData;
1451 UInt32 PointerToRelocations;
1452 UInt32 PointerToLinenumbers;
1453 UInt16 NumberOfRelocations;
1454 UInt16 NumberOfLineNumbers;
1455 UInt32 Characteristics;
1459 #define sizeof_COFF_section 40
1466 UInt16 SectionNumber;
1469 UChar NumberOfAuxSymbols;
1473 #define sizeof_COFF_symbol 18
1478 UInt32 VirtualAddress;
1479 UInt32 SymbolTableIndex;
1484 #define sizeof_COFF_reloc 10
1487 /* From PE spec doc, section 3.3.2 */
1488 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1489 windows.h -- for the same purpose, but I want to know what I'm
1491 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1492 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1493 #define MYIMAGE_FILE_DLL 0x2000
1494 #define MYIMAGE_FILE_SYSTEM 0x1000
1495 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1496 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1497 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1499 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1500 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1501 #define MYIMAGE_SYM_CLASS_STATIC 3
1502 #define MYIMAGE_SYM_UNDEFINED 0
1504 /* From PE spec doc, section 4.1 */
1505 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1506 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1507 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1509 /* From PE spec doc, section 5.2.1 */
1510 #define MYIMAGE_REL_I386_DIR32 0x0006
1511 #define MYIMAGE_REL_I386_REL32 0x0014
1514 /* We use myindex to calculate array addresses, rather than
1515 simply doing the normal subscript thing. That's because
1516 some of the above structs have sizes which are not
1517 a whole number of words. GCC rounds their sizes up to a
1518 whole number of words, which means that the address calcs
1519 arising from using normal C indexing or pointer arithmetic
1520 are just plain wrong. Sigh.
1523 myindex ( int scale, void* base, int index )
1526 ((UChar*)base) + scale * index;
1531 printName ( UChar* name, UChar* strtab )
1533 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1534 UInt32 strtab_offset = * (UInt32*)(name+4);
1535 debugBelch("%s", strtab + strtab_offset );
1538 for (i = 0; i < 8; i++) {
1539 if (name[i] == 0) break;
1540 debugBelch("%c", name[i] );
1547 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1549 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1550 UInt32 strtab_offset = * (UInt32*)(name+4);
1551 strncpy ( dst, strtab+strtab_offset, dstSize );
1557 if (name[i] == 0) break;
1567 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1570 /* If the string is longer than 8 bytes, look in the
1571 string table for it -- this will be correctly zero terminated.
1573 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1574 UInt32 strtab_offset = * (UInt32*)(name+4);
1575 return ((UChar*)strtab) + strtab_offset;
1577 /* Otherwise, if shorter than 8 bytes, return the original,
1578 which by defn is correctly terminated.
1580 if (name[7]==0) return name;
1581 /* The annoying case: 8 bytes. Copy into a temporary
1582 (which is never freed ...)
1584 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1586 strncpy(newstr,name,8);
1592 /* Just compares the short names (first 8 chars) */
1593 static COFF_section *
1594 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1598 = (COFF_header*)(oc->image);
1599 COFF_section* sectab
1601 ((UChar*)(oc->image))
1602 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1604 for (i = 0; i < hdr->NumberOfSections; i++) {
1607 COFF_section* section_i
1609 myindex ( sizeof_COFF_section, sectab, i );
1610 n1 = (UChar*) &(section_i->Name);
1612 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1613 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1614 n1[6]==n2[6] && n1[7]==n2[7])
1623 zapTrailingAtSign ( UChar* sym )
1625 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1627 if (sym[0] == 0) return;
1629 while (sym[i] != 0) i++;
1632 while (j > 0 && my_isdigit(sym[j])) j--;
1633 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1639 ocVerifyImage_PEi386 ( ObjectCode* oc )
1644 COFF_section* sectab;
1645 COFF_symbol* symtab;
1647 /* debugBelch("\nLOADING %s\n", oc->fileName); */
1648 hdr = (COFF_header*)(oc->image);
1649 sectab = (COFF_section*) (
1650 ((UChar*)(oc->image))
1651 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1653 symtab = (COFF_symbol*) (
1654 ((UChar*)(oc->image))
1655 + hdr->PointerToSymbolTable
1657 strtab = ((UChar*)symtab)
1658 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1660 if (hdr->Machine != 0x14c) {
1661 errorBelch("Not x86 PEi386");
1664 if (hdr->SizeOfOptionalHeader != 0) {
1665 errorBelch("PEi386 with nonempty optional header");
1668 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1669 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1670 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1671 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1672 errorBelch("Not a PEi386 object file");
1675 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1676 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1677 errorBelch("Invalid PEi386 word size or endiannness: %d",
1678 (int)(hdr->Characteristics));
1681 /* If the string table size is way crazy, this might indicate that
1682 there are more than 64k relocations, despite claims to the
1683 contrary. Hence this test. */
1684 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
1686 if ( (*(UInt32*)strtab) > 600000 ) {
1687 /* Note that 600k has no special significance other than being
1688 big enough to handle the almost-2MB-sized lumps that
1689 constitute HSwin32*.o. */
1690 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
1695 /* No further verification after this point; only debug printing. */
1697 IF_DEBUG(linker, i=1);
1698 if (i == 0) return 1;
1700 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1701 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1702 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1705 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1706 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1707 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1708 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1709 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1710 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1711 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1713 /* Print the section table. */
1715 for (i = 0; i < hdr->NumberOfSections; i++) {
1717 COFF_section* sectab_i
1719 myindex ( sizeof_COFF_section, sectab, i );
1726 printName ( sectab_i->Name, strtab );
1736 sectab_i->VirtualSize,
1737 sectab_i->VirtualAddress,
1738 sectab_i->SizeOfRawData,
1739 sectab_i->PointerToRawData,
1740 sectab_i->NumberOfRelocations,
1741 sectab_i->PointerToRelocations,
1742 sectab_i->PointerToRawData
1744 reltab = (COFF_reloc*) (
1745 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1748 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1749 /* If the relocation field (a short) has overflowed, the
1750 * real count can be found in the first reloc entry.
1752 * See Section 4.1 (last para) of the PE spec (rev6.0).
1754 COFF_reloc* rel = (COFF_reloc*)
1755 myindex ( sizeof_COFF_reloc, reltab, 0 );
1756 noRelocs = rel->VirtualAddress;
1759 noRelocs = sectab_i->NumberOfRelocations;
1763 for (; j < noRelocs; j++) {
1765 COFF_reloc* rel = (COFF_reloc*)
1766 myindex ( sizeof_COFF_reloc, reltab, j );
1768 " type 0x%-4x vaddr 0x%-8x name `",
1770 rel->VirtualAddress );
1771 sym = (COFF_symbol*)
1772 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1773 /* Hmm..mysterious looking offset - what's it for? SOF */
1774 printName ( sym->Name, strtab -10 );
1781 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
1782 debugBelch("---START of string table---\n");
1783 for (i = 4; i < *(Int32*)strtab; i++) {
1785 debugBelch("\n"); else
1786 debugBelch("%c", strtab[i] );
1788 debugBelch("--- END of string table---\n");
1793 COFF_symbol* symtab_i;
1794 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1795 symtab_i = (COFF_symbol*)
1796 myindex ( sizeof_COFF_symbol, symtab, i );
1802 printName ( symtab_i->Name, strtab );
1811 (Int32)(symtab_i->SectionNumber),
1812 (UInt32)symtab_i->Type,
1813 (UInt32)symtab_i->StorageClass,
1814 (UInt32)symtab_i->NumberOfAuxSymbols
1816 i += symtab_i->NumberOfAuxSymbols;
1826 ocGetNames_PEi386 ( ObjectCode* oc )
1829 COFF_section* sectab;
1830 COFF_symbol* symtab;
1837 hdr = (COFF_header*)(oc->image);
1838 sectab = (COFF_section*) (
1839 ((UChar*)(oc->image))
1840 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1842 symtab = (COFF_symbol*) (
1843 ((UChar*)(oc->image))
1844 + hdr->PointerToSymbolTable
1846 strtab = ((UChar*)(oc->image))
1847 + hdr->PointerToSymbolTable
1848 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1850 /* Allocate space for any (local, anonymous) .bss sections. */
1852 for (i = 0; i < hdr->NumberOfSections; i++) {
1854 COFF_section* sectab_i
1856 myindex ( sizeof_COFF_section, sectab, i );
1857 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1858 if (sectab_i->VirtualSize == 0) continue;
1859 /* This is a non-empty .bss section. Allocate zeroed space for
1860 it, and set its PointerToRawData field such that oc->image +
1861 PointerToRawData == addr_of_zeroed_space. */
1862 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1863 "ocGetNames_PEi386(anonymous bss)");
1864 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1865 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1866 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
1869 /* Copy section information into the ObjectCode. */
1871 for (i = 0; i < hdr->NumberOfSections; i++) {
1877 = SECTIONKIND_OTHER;
1878 COFF_section* sectab_i
1880 myindex ( sizeof_COFF_section, sectab, i );
1881 IF_DEBUG(linker, debugBelch("section name = %s\n", sectab_i->Name ));
1884 /* I'm sure this is the Right Way to do it. However, the
1885 alternative of testing the sectab_i->Name field seems to
1886 work ok with Cygwin.
1888 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1889 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1890 kind = SECTIONKIND_CODE_OR_RODATA;
1893 if (0==strcmp(".text",sectab_i->Name) ||
1894 0==strcmp(".rodata",sectab_i->Name))
1895 kind = SECTIONKIND_CODE_OR_RODATA;
1896 if (0==strcmp(".data",sectab_i->Name) ||
1897 0==strcmp(".bss",sectab_i->Name))
1898 kind = SECTIONKIND_RWDATA;
1900 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1901 sz = sectab_i->SizeOfRawData;
1902 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1904 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1905 end = start + sz - 1;
1907 if (kind == SECTIONKIND_OTHER
1908 /* Ignore sections called which contain stabs debugging
1910 && 0 != strcmp(".stab", sectab_i->Name)
1911 && 0 != strcmp(".stabstr", sectab_i->Name)
1913 errorBelch("Unknown PEi386 section name `%s'", sectab_i->Name);
1917 if (kind != SECTIONKIND_OTHER && end >= start) {
1918 addSection(oc, kind, start, end);
1919 addProddableBlock(oc, start, end - start + 1);
1923 /* Copy exported symbols into the ObjectCode. */
1925 oc->n_symbols = hdr->NumberOfSymbols;
1926 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1927 "ocGetNames_PEi386(oc->symbols)");
1928 /* Call me paranoid; I don't care. */
1929 for (i = 0; i < oc->n_symbols; i++)
1930 oc->symbols[i] = NULL;
1934 COFF_symbol* symtab_i;
1935 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1936 symtab_i = (COFF_symbol*)
1937 myindex ( sizeof_COFF_symbol, symtab, i );
1941 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1942 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1943 /* This symbol is global and defined, viz, exported */
1944 /* for MYIMAGE_SYMCLASS_EXTERNAL
1945 && !MYIMAGE_SYM_UNDEFINED,
1946 the address of the symbol is:
1947 address of relevant section + offset in section
1949 COFF_section* sectabent
1950 = (COFF_section*) myindex ( sizeof_COFF_section,
1952 symtab_i->SectionNumber-1 );
1953 addr = ((UChar*)(oc->image))
1954 + (sectabent->PointerToRawData
1958 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1959 && symtab_i->Value > 0) {
1960 /* This symbol isn't in any section at all, ie, global bss.
1961 Allocate zeroed space for it. */
1962 addr = stgCallocBytes(1, symtab_i->Value,
1963 "ocGetNames_PEi386(non-anonymous bss)");
1964 addSection(oc, SECTIONKIND_RWDATA, addr,
1965 ((UChar*)addr) + symtab_i->Value - 1);
1966 addProddableBlock(oc, addr, symtab_i->Value);
1967 /* debugBelch("BSS section at 0x%x\n", addr); */
1970 if (addr != NULL ) {
1971 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1972 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
1973 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
1974 ASSERT(i >= 0 && i < oc->n_symbols);
1975 /* cstring_from_COFF_symbol_name always succeeds. */
1976 oc->symbols[i] = sname;
1977 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1981 "IGNORING symbol %d\n"
1985 printName ( symtab_i->Name, strtab );
1994 (Int32)(symtab_i->SectionNumber),
1995 (UInt32)symtab_i->Type,
1996 (UInt32)symtab_i->StorageClass,
1997 (UInt32)symtab_i->NumberOfAuxSymbols
2002 i += symtab_i->NumberOfAuxSymbols;
2011 ocResolve_PEi386 ( ObjectCode* oc )
2014 COFF_section* sectab;
2015 COFF_symbol* symtab;
2025 /* ToDo: should be variable-sized? But is at least safe in the
2026 sense of buffer-overrun-proof. */
2028 /* debugBelch("resolving for %s\n", oc->fileName); */
2030 hdr = (COFF_header*)(oc->image);
2031 sectab = (COFF_section*) (
2032 ((UChar*)(oc->image))
2033 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2035 symtab = (COFF_symbol*) (
2036 ((UChar*)(oc->image))
2037 + hdr->PointerToSymbolTable
2039 strtab = ((UChar*)(oc->image))
2040 + hdr->PointerToSymbolTable
2041 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2043 for (i = 0; i < hdr->NumberOfSections; i++) {
2044 COFF_section* sectab_i
2046 myindex ( sizeof_COFF_section, sectab, i );
2049 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2052 /* Ignore sections called which contain stabs debugging
2054 if (0 == strcmp(".stab", sectab_i->Name)
2055 || 0 == strcmp(".stabstr", sectab_i->Name))
2058 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2059 /* If the relocation field (a short) has overflowed, the
2060 * real count can be found in the first reloc entry.
2062 * See Section 4.1 (last para) of the PE spec (rev6.0).
2064 * Nov2003 update: the GNU linker still doesn't correctly
2065 * handle the generation of relocatable object files with
2066 * overflown relocations. Hence the output to warn of potential
2069 COFF_reloc* rel = (COFF_reloc*)
2070 myindex ( sizeof_COFF_reloc, reltab, 0 );
2071 noRelocs = rel->VirtualAddress;
2072 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
2076 noRelocs = sectab_i->NumberOfRelocations;
2081 for (; j < noRelocs; j++) {
2083 COFF_reloc* reltab_j
2085 myindex ( sizeof_COFF_reloc, reltab, j );
2087 /* the location to patch */
2089 ((UChar*)(oc->image))
2090 + (sectab_i->PointerToRawData
2091 + reltab_j->VirtualAddress
2092 - sectab_i->VirtualAddress )
2094 /* the existing contents of pP */
2096 /* the symbol to connect to */
2097 sym = (COFF_symbol*)
2098 myindex ( sizeof_COFF_symbol,
2099 symtab, reltab_j->SymbolTableIndex );
2102 "reloc sec %2d num %3d: type 0x%-4x "
2103 "vaddr 0x%-8x name `",
2105 (UInt32)reltab_j->Type,
2106 reltab_j->VirtualAddress );
2107 printName ( sym->Name, strtab );
2108 debugBelch("'\n" ));
2110 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
2111 COFF_section* section_sym
2112 = findPEi386SectionCalled ( oc, sym->Name );
2114 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
2117 S = ((UInt32)(oc->image))
2118 + (section_sym->PointerToRawData
2121 copyName ( sym->Name, strtab, symbol, 1000-1 );
2122 (void*)S = lookupLocalSymbol( oc, symbol );
2123 if ((void*)S != NULL) goto foundit;
2124 (void*)S = lookupSymbol( symbol );
2125 if ((void*)S != NULL) goto foundit;
2126 zapTrailingAtSign ( symbol );
2127 (void*)S = lookupLocalSymbol( oc, symbol );
2128 if ((void*)S != NULL) goto foundit;
2129 (void*)S = lookupSymbol( symbol );
2130 if ((void*)S != NULL) goto foundit;
2131 /* Newline first because the interactive linker has printed "linking..." */
2132 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
2136 checkProddableBlock(oc, pP);
2137 switch (reltab_j->Type) {
2138 case MYIMAGE_REL_I386_DIR32:
2141 case MYIMAGE_REL_I386_REL32:
2142 /* Tricky. We have to insert a displacement at
2143 pP which, when added to the PC for the _next_
2144 insn, gives the address of the target (S).
2145 Problem is to know the address of the next insn
2146 when we only know pP. We assume that this
2147 literal field is always the last in the insn,
2148 so that the address of the next insn is pP+4
2149 -- hence the constant 4.
2150 Also I don't know if A should be added, but so
2151 far it has always been zero.
2154 *pP = S - ((UInt32)pP) - 4;
2157 debugBelch("%s: unhandled PEi386 relocation type %d",
2158 oc->fileName, reltab_j->Type);
2165 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
2169 #endif /* defined(OBJFORMAT_PEi386) */
2172 /* --------------------------------------------------------------------------
2174 * ------------------------------------------------------------------------*/
2176 #if defined(OBJFORMAT_ELF)
2181 #if defined(sparc_TARGET_ARCH)
2182 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2183 #elif defined(i386_TARGET_ARCH)
2184 # define ELF_TARGET_386 /* Used inside <elf.h> */
2185 #elif defined(x86_64_TARGET_ARCH)
2186 # define ELF_TARGET_X64_64
2188 #elif defined (ia64_TARGET_ARCH)
2189 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2191 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2192 # define ELF_NEED_GOT /* needs Global Offset Table */
2193 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2196 #if !defined(openbsd_TARGET_OS)
2199 /* openbsd elf has things in different places, with diff names */
2200 #include <elf_abi.h>
2201 #include <machine/reloc.h>
2202 #define R_386_32 RELOC_32
2203 #define R_386_PC32 RELOC_PC32
2207 * Define a set of types which can be used for both ELF32 and ELF64
2211 #define ELFCLASS ELFCLASS64
2212 #define Elf_Addr Elf64_Addr
2213 #define Elf_Word Elf64_Word
2214 #define Elf_Sword Elf64_Sword
2215 #define Elf_Ehdr Elf64_Ehdr
2216 #define Elf_Phdr Elf64_Phdr
2217 #define Elf_Shdr Elf64_Shdr
2218 #define Elf_Sym Elf64_Sym
2219 #define Elf_Rel Elf64_Rel
2220 #define Elf_Rela Elf64_Rela
2221 #define ELF_ST_TYPE ELF64_ST_TYPE
2222 #define ELF_ST_BIND ELF64_ST_BIND
2223 #define ELF_R_TYPE ELF64_R_TYPE
2224 #define ELF_R_SYM ELF64_R_SYM
2226 #define ELFCLASS ELFCLASS32
2227 #define Elf_Addr Elf32_Addr
2228 #define Elf_Word Elf32_Word
2229 #define Elf_Sword Elf32_Sword
2230 #define Elf_Ehdr Elf32_Ehdr
2231 #define Elf_Phdr Elf32_Phdr
2232 #define Elf_Shdr Elf32_Shdr
2233 #define Elf_Sym Elf32_Sym
2234 #define Elf_Rel Elf32_Rel
2235 #define Elf_Rela Elf32_Rela
2237 #define ELF_ST_TYPE ELF32_ST_TYPE
2240 #define ELF_ST_BIND ELF32_ST_BIND
2243 #define ELF_R_TYPE ELF32_R_TYPE
2246 #define ELF_R_SYM ELF32_R_SYM
2252 * Functions to allocate entries in dynamic sections. Currently we simply
2253 * preallocate a large number, and we don't check if a entry for the given
2254 * target already exists (a linear search is too slow). Ideally these
2255 * entries would be associated with symbols.
2258 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2259 #define GOT_SIZE 0x20000
2260 #define FUNCTION_TABLE_SIZE 0x10000
2261 #define PLT_SIZE 0x08000
2264 static Elf_Addr got[GOT_SIZE];
2265 static unsigned int gotIndex;
2266 static Elf_Addr gp_val = (Elf_Addr)got;
2269 allocateGOTEntry(Elf_Addr target)
2273 if (gotIndex >= GOT_SIZE)
2274 barf("Global offset table overflow");
2276 entry = &got[gotIndex++];
2278 return (Elf_Addr)entry;
2282 #ifdef ELF_FUNCTION_DESC
2288 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2289 static unsigned int functionTableIndex;
2292 allocateFunctionDesc(Elf_Addr target)
2294 FunctionDesc *entry;
2296 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2297 barf("Function table overflow");
2299 entry = &functionTable[functionTableIndex++];
2301 entry->gp = (Elf_Addr)gp_val;
2302 return (Elf_Addr)entry;
2306 copyFunctionDesc(Elf_Addr target)
2308 FunctionDesc *olddesc = (FunctionDesc *)target;
2309 FunctionDesc *newdesc;
2311 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2312 newdesc->gp = olddesc->gp;
2313 return (Elf_Addr)newdesc;
2318 #ifdef ia64_TARGET_ARCH
2319 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2320 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2322 static unsigned char plt_code[] =
2324 /* taken from binutils bfd/elfxx-ia64.c */
2325 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2326 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2327 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2328 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2329 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2330 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2333 /* If we can't get to the function descriptor via gp, take a local copy of it */
2334 #define PLT_RELOC(code, target) { \
2335 Elf64_Sxword rel_value = target - gp_val; \
2336 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2337 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2339 ia64_reloc_gprel22((Elf_Addr)code, target); \
2344 unsigned char code[sizeof(plt_code)];
2348 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2350 PLTEntry *plt = (PLTEntry *)oc->plt;
2353 if (oc->pltIndex >= PLT_SIZE)
2354 barf("Procedure table overflow");
2356 entry = &plt[oc->pltIndex++];
2357 memcpy(entry->code, plt_code, sizeof(entry->code));
2358 PLT_RELOC(entry->code, target);
2359 return (Elf_Addr)entry;
2365 return (PLT_SIZE * sizeof(PLTEntry));
2371 * Generic ELF functions
2375 findElfSection ( void* objImage, Elf_Word sh_type )
2377 char* ehdrC = (char*)objImage;
2378 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2379 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2380 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2384 for (i = 0; i < ehdr->e_shnum; i++) {
2385 if (shdr[i].sh_type == sh_type
2386 /* Ignore the section header's string table. */
2387 && i != ehdr->e_shstrndx
2388 /* Ignore string tables named .stabstr, as they contain
2390 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2392 ptr = ehdrC + shdr[i].sh_offset;
2399 #if defined(ia64_TARGET_ARCH)
2401 findElfSegment ( void* objImage, Elf_Addr vaddr )
2403 char* ehdrC = (char*)objImage;
2404 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2405 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2406 Elf_Addr segaddr = 0;
2409 for (i = 0; i < ehdr->e_phnum; i++) {
2410 segaddr = phdr[i].p_vaddr;
2411 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2419 ocVerifyImage_ELF ( ObjectCode* oc )
2423 int i, j, nent, nstrtab, nsymtabs;
2427 char* ehdrC = (char*)(oc->image);
2428 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2430 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2431 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2432 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2433 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2434 errorBelch("%s: not an ELF object", oc->fileName);
2438 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2439 errorBelch("%s: unsupported ELF format", oc->fileName);
2443 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2444 IF_DEBUG(linker,debugBelch( "Is little-endian" ));
2446 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2447 IF_DEBUG(linker,debugBelch( "Is big-endian" ));
2449 errorBelch("%s: unknown endiannness", oc->fileName);
2453 if (ehdr->e_type != ET_REL) {
2454 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
2457 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file" ));
2459 IF_DEBUG(linker,debugBelch( "Architecture is " ));
2460 switch (ehdr->e_machine) {
2461 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
2462 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
2464 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
2466 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
2467 default: IF_DEBUG(linker,debugBelch( "unknown" ));
2468 errorBelch("%s: unknown architecture", oc->fileName);
2472 IF_DEBUG(linker,debugBelch(
2473 "\nSection header table: start %d, n_entries %d, ent_size %d",
2474 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2476 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2478 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2480 if (ehdr->e_shstrndx == SHN_UNDEF) {
2481 errorBelch("%s: no section header string table", oc->fileName);
2484 IF_DEBUG(linker,debugBelch( "Section header string table is section %d",
2486 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2489 for (i = 0; i < ehdr->e_shnum; i++) {
2490 IF_DEBUG(linker,debugBelch("%2d: ", i ));
2491 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
2492 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
2493 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
2494 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
2495 ehdrC + shdr[i].sh_offset,
2496 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2498 if (shdr[i].sh_type == SHT_REL) {
2499 IF_DEBUG(linker,debugBelch("Rel " ));
2500 } else if (shdr[i].sh_type == SHT_RELA) {
2501 IF_DEBUG(linker,debugBelch("RelA " ));
2503 IF_DEBUG(linker,debugBelch(" "));
2506 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
2510 IF_DEBUG(linker,debugBelch( "\nString tables" ));
2513 for (i = 0; i < ehdr->e_shnum; i++) {
2514 if (shdr[i].sh_type == SHT_STRTAB
2515 /* Ignore the section header's string table. */
2516 && i != ehdr->e_shstrndx
2517 /* Ignore string tables named .stabstr, as they contain
2519 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2521 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
2522 strtab = ehdrC + shdr[i].sh_offset;
2527 errorBelch("%s: no string tables, or too many", oc->fileName);
2532 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
2533 for (i = 0; i < ehdr->e_shnum; i++) {
2534 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2535 IF_DEBUG(linker,debugBelch( "section %d is a symbol table", i ));
2537 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2538 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2539 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%d rem)",
2541 shdr[i].sh_size % sizeof(Elf_Sym)
2543 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2544 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
2547 for (j = 0; j < nent; j++) {
2548 IF_DEBUG(linker,debugBelch(" %2d ", j ));
2549 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
2550 (int)stab[j].st_shndx,
2551 (int)stab[j].st_size,
2552 (char*)stab[j].st_value ));
2554 IF_DEBUG(linker,debugBelch("type=" ));
2555 switch (ELF_ST_TYPE(stab[j].st_info)) {
2556 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
2557 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
2558 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
2559 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
2560 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
2561 default: IF_DEBUG(linker,debugBelch("? " )); break;
2563 IF_DEBUG(linker,debugBelch(" " ));
2565 IF_DEBUG(linker,debugBelch("bind=" ));
2566 switch (ELF_ST_BIND(stab[j].st_info)) {
2567 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
2568 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
2569 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
2570 default: IF_DEBUG(linker,debugBelch("? " )); break;
2572 IF_DEBUG(linker,debugBelch(" " ));
2574 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
2578 if (nsymtabs == 0) {
2579 errorBelch("%s: didn't find any symbol tables", oc->fileName);
2588 ocGetNames_ELF ( ObjectCode* oc )
2593 char* ehdrC = (char*)(oc->image);
2594 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2595 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2596 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2598 ASSERT(symhash != NULL);
2601 errorBelch("%s: no strtab", oc->fileName);
2606 for (i = 0; i < ehdr->e_shnum; i++) {
2607 /* Figure out what kind of section it is. Logic derived from
2608 Figure 1.14 ("Special Sections") of the ELF document
2609 ("Portable Formats Specification, Version 1.1"). */
2610 Elf_Shdr hdr = shdr[i];
2611 SectionKind kind = SECTIONKIND_OTHER;
2614 if (hdr.sh_type == SHT_PROGBITS
2615 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2616 /* .text-style section */
2617 kind = SECTIONKIND_CODE_OR_RODATA;
2620 if (hdr.sh_type == SHT_PROGBITS
2621 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2622 /* .data-style section */
2623 kind = SECTIONKIND_RWDATA;
2626 if (hdr.sh_type == SHT_PROGBITS
2627 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2628 /* .rodata-style section */
2629 kind = SECTIONKIND_CODE_OR_RODATA;
2632 if (hdr.sh_type == SHT_NOBITS
2633 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2634 /* .bss-style section */
2635 kind = SECTIONKIND_RWDATA;
2639 if (is_bss && shdr[i].sh_size > 0) {
2640 /* This is a non-empty .bss section. Allocate zeroed space for
2641 it, and set its .sh_offset field such that
2642 ehdrC + .sh_offset == addr_of_zeroed_space. */
2643 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2644 "ocGetNames_ELF(BSS)");
2645 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2647 debugBelch("BSS section at 0x%x, size %d\n",
2648 zspace, shdr[i].sh_size);
2652 /* fill in the section info */
2653 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2654 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2655 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2656 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2659 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2661 /* copy stuff into this module's object symbol table */
2662 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2663 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2665 oc->n_symbols = nent;
2666 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2667 "ocGetNames_ELF(oc->symbols)");
2669 for (j = 0; j < nent; j++) {
2671 char isLocal = FALSE; /* avoids uninit-var warning */
2673 char* nm = strtab + stab[j].st_name;
2674 int secno = stab[j].st_shndx;
2676 /* Figure out if we want to add it; if so, set ad to its
2677 address. Otherwise leave ad == NULL. */
2679 if (secno == SHN_COMMON) {
2681 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2683 debugBelch("COMMON symbol, size %d name %s\n",
2684 stab[j].st_size, nm);
2686 /* Pointless to do addProddableBlock() for this area,
2687 since the linker should never poke around in it. */
2690 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2691 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2693 /* and not an undefined symbol */
2694 && stab[j].st_shndx != SHN_UNDEF
2695 /* and not in a "special section" */
2696 && stab[j].st_shndx < SHN_LORESERVE
2698 /* and it's a not a section or string table or anything silly */
2699 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2700 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2701 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2704 /* Section 0 is the undefined section, hence > and not >=. */
2705 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2707 if (shdr[secno].sh_type == SHT_NOBITS) {
2708 debugBelch(" BSS symbol, size %d off %d name %s\n",
2709 stab[j].st_size, stab[j].st_value, nm);
2712 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2713 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2716 #ifdef ELF_FUNCTION_DESC
2717 /* dlsym() and the initialisation table both give us function
2718 * descriptors, so to be consistent we store function descriptors
2719 * in the symbol table */
2720 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2721 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2723 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s",
2724 ad, oc->fileName, nm ));
2729 /* And the decision is ... */
2733 oc->symbols[j] = nm;
2736 /* Ignore entirely. */
2738 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2742 IF_DEBUG(linker,debugBelch( "skipping `%s'",
2743 strtab + stab[j].st_name ));
2746 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2747 (int)ELF_ST_BIND(stab[j].st_info),
2748 (int)ELF_ST_TYPE(stab[j].st_info),
2749 (int)stab[j].st_shndx,
2750 strtab + stab[j].st_name
2753 oc->symbols[j] = NULL;
2762 /* Do ELF relocations which lack an explicit addend. All x86-linux
2763 relocations appear to be of this form. */
2765 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2766 Elf_Shdr* shdr, int shnum,
2767 Elf_Sym* stab, char* strtab )
2772 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2773 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2774 int target_shndx = shdr[shnum].sh_info;
2775 int symtab_shndx = shdr[shnum].sh_link;
2777 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2778 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2779 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d",
2780 target_shndx, symtab_shndx ));
2782 for (j = 0; j < nent; j++) {
2783 Elf_Addr offset = rtab[j].r_offset;
2784 Elf_Addr info = rtab[j].r_info;
2786 Elf_Addr P = ((Elf_Addr)targ) + offset;
2787 Elf_Word* pP = (Elf_Word*)P;
2793 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
2794 j, (void*)offset, (void*)info ));
2796 IF_DEBUG(linker,debugBelch( " ZERO" ));
2799 Elf_Sym sym = stab[ELF_R_SYM(info)];
2800 /* First see if it is a local symbol. */
2801 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2802 /* Yes, so we can get the address directly from the ELF symbol
2804 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2806 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2807 + stab[ELF_R_SYM(info)].st_value);
2810 /* No, so look up the name in our global table. */
2811 symbol = strtab + sym.st_name;
2812 S_tmp = lookupSymbol( symbol );
2813 S = (Elf_Addr)S_tmp;
2816 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
2819 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
2822 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p",
2823 (void*)P, (void*)S, (void*)A ));
2824 checkProddableBlock ( oc, pP );
2828 switch (ELF_R_TYPE(info)) {
2829 # ifdef i386_TARGET_ARCH
2830 case R_386_32: *pP = value; break;
2831 case R_386_PC32: *pP = value - P; break;
2834 errorBelch("%s: unhandled ELF relocation(Rel) type %d\n",
2835 oc->fileName, ELF_R_TYPE(info));
2843 /* Do ELF relocations for which explicit addends are supplied.
2844 sparc-solaris relocations appear to be of this form. */
2846 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2847 Elf_Shdr* shdr, int shnum,
2848 Elf_Sym* stab, char* strtab )
2853 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2854 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2855 int target_shndx = shdr[shnum].sh_info;
2856 int symtab_shndx = shdr[shnum].sh_link;
2858 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2859 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2860 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d",
2861 target_shndx, symtab_shndx ));
2863 for (j = 0; j < nent; j++) {
2864 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH) || defined(powerpc_TARGET_ARCH)
2865 /* This #ifdef only serves to avoid unused-var warnings. */
2866 Elf_Addr offset = rtab[j].r_offset;
2867 Elf_Addr P = targ + offset;
2869 Elf_Addr info = rtab[j].r_info;
2870 Elf_Addr A = rtab[j].r_addend;
2874 # if defined(sparc_TARGET_ARCH)
2875 Elf_Word* pP = (Elf_Word*)P;
2877 # elif defined(ia64_TARGET_ARCH)
2878 Elf64_Xword *pP = (Elf64_Xword *)P;
2880 # elif defined(powerpc_TARGET_ARCH)
2884 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
2885 j, (void*)offset, (void*)info,
2888 IF_DEBUG(linker,debugBelch( " ZERO" ));
2891 Elf_Sym sym = stab[ELF_R_SYM(info)];
2892 /* First see if it is a local symbol. */
2893 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2894 /* Yes, so we can get the address directly from the ELF symbol
2896 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2898 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2899 + stab[ELF_R_SYM(info)].st_value);
2900 #ifdef ELF_FUNCTION_DESC
2901 /* Make a function descriptor for this function */
2902 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2903 S = allocateFunctionDesc(S + A);
2908 /* No, so look up the name in our global table. */
2909 symbol = strtab + sym.st_name;
2910 S_tmp = lookupSymbol( symbol );
2911 S = (Elf_Addr)S_tmp;
2913 #ifdef ELF_FUNCTION_DESC
2914 /* If a function, already a function descriptor - we would
2915 have to copy it to add an offset. */
2916 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
2917 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
2921 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
2924 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
2927 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
2928 (void*)P, (void*)S, (void*)A ));
2929 /* checkProddableBlock ( oc, (void*)P ); */
2933 switch (ELF_R_TYPE(info)) {
2934 # if defined(sparc_TARGET_ARCH)
2935 case R_SPARC_WDISP30:
2936 w1 = *pP & 0xC0000000;
2937 w2 = (Elf_Word)((value - P) >> 2);
2938 ASSERT((w2 & 0xC0000000) == 0);
2943 w1 = *pP & 0xFFC00000;
2944 w2 = (Elf_Word)(value >> 10);
2945 ASSERT((w2 & 0xFFC00000) == 0);
2951 w2 = (Elf_Word)(value & 0x3FF);
2952 ASSERT((w2 & ~0x3FF) == 0);
2956 /* According to the Sun documentation:
2958 This relocation type resembles R_SPARC_32, except it refers to an
2959 unaligned word. That is, the word to be relocated must be treated
2960 as four separate bytes with arbitrary alignment, not as a word
2961 aligned according to the architecture requirements.
2963 (JRS: which means that freeloading on the R_SPARC_32 case
2964 is probably wrong, but hey ...)
2968 w2 = (Elf_Word)value;
2971 # elif defined(ia64_TARGET_ARCH)
2972 case R_IA64_DIR64LSB:
2973 case R_IA64_FPTR64LSB:
2976 case R_IA64_PCREL64LSB:
2979 case R_IA64_SEGREL64LSB:
2980 addr = findElfSegment(ehdrC, value);
2983 case R_IA64_GPREL22:
2984 ia64_reloc_gprel22(P, value);
2986 case R_IA64_LTOFF22:
2987 case R_IA64_LTOFF22X:
2988 case R_IA64_LTOFF_FPTR22:
2989 addr = allocateGOTEntry(value);
2990 ia64_reloc_gprel22(P, addr);
2992 case R_IA64_PCREL21B:
2993 ia64_reloc_pcrel21(P, S, oc);
2996 /* This goes with R_IA64_LTOFF22X and points to the load to
2997 * convert into a move. We don't implement relaxation. */
2999 # elif defined(powerpc_TARGET_ARCH)
3000 case R_PPC_ADDR16_LO:
3001 *(Elf32_Half*) P = value;
3004 case R_PPC_ADDR16_HI:
3005 *(Elf32_Half*) P = value >> 16;
3008 case R_PPC_ADDR16_HA:
3009 *(Elf32_Half*) P = (value + 0x8000) >> 16;
3013 *(Elf32_Word *) P = value;
3017 *(Elf32_Word *) P = value - P;
3023 if( delta << 6 >> 6 != delta )
3025 value = makeJumpIsland( oc, ELF_R_SYM(info), value );
3028 if( value == 0 || delta << 6 >> 6 != delta )
3030 barf( "Unable to make ppcJumpIsland for #%d",
3036 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
3037 | (delta & 0x3fffffc);
3041 errorBelch("%s: unhandled ELF relocation(RelA) type %d\n",
3042 oc->fileName, ELF_R_TYPE(info));
3051 ocResolve_ELF ( ObjectCode* oc )
3055 Elf_Sym* stab = NULL;
3056 char* ehdrC = (char*)(oc->image);
3057 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
3058 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3059 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3061 /* first find "the" symbol table */
3062 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
3064 /* also go find the string table */
3065 strtab = findElfSection ( ehdrC, SHT_STRTAB );
3067 if (stab == NULL || strtab == NULL) {
3068 errorBelch("%s: can't find string or symbol table", oc->fileName);
3072 /* Process the relocation sections. */
3073 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
3075 /* Skip sections called ".rel.stab". These appear to contain
3076 relocation entries that, when done, make the stabs debugging
3077 info point at the right places. We ain't interested in all
3079 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
3082 if (shdr[shnum].sh_type == SHT_REL ) {
3083 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
3084 shnum, stab, strtab );
3088 if (shdr[shnum].sh_type == SHT_RELA) {
3089 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
3090 shnum, stab, strtab );
3095 /* Free the local symbol table; we won't need it again. */
3096 freeHashTable(oc->lochash, NULL);
3099 #if defined(powerpc_TARGET_ARCH)
3100 ocFlushInstructionCache( oc );
3108 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
3109 * at the front. The following utility functions pack and unpack instructions, and
3110 * take care of the most common relocations.
3113 #ifdef ia64_TARGET_ARCH
3116 ia64_extract_instruction(Elf64_Xword *target)
3119 int slot = (Elf_Addr)target & 3;
3120 (Elf_Addr)target &= ~3;
3128 return ((w1 >> 5) & 0x1ffffffffff);
3130 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
3134 barf("ia64_extract_instruction: invalid slot %p", target);
3139 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
3141 int slot = (Elf_Addr)target & 3;
3142 (Elf_Addr)target &= ~3;
3147 *target |= value << 5;
3150 *target |= value << 46;
3151 *(target+1) |= value >> 18;
3154 *(target+1) |= value << 23;
3160 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
3162 Elf64_Xword instruction;
3163 Elf64_Sxword rel_value;
3165 rel_value = value - gp_val;
3166 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
3167 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
3169 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3170 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
3171 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
3172 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
3173 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3174 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3178 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
3180 Elf64_Xword instruction;
3181 Elf64_Sxword rel_value;
3184 entry = allocatePLTEntry(value, oc);
3186 rel_value = (entry >> 4) - (target >> 4);
3187 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
3188 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
3190 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3191 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
3192 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3193 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3199 * PowerPC ELF specifics
3202 #ifdef powerpc_TARGET_ARCH
3204 static int ocAllocateJumpIslands_ELF( ObjectCode *oc )
3210 ehdr = (Elf_Ehdr *) oc->image;
3211 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
3213 for( i = 0; i < ehdr->e_shnum; i++ )
3214 if( shdr[i].sh_type == SHT_SYMTAB )
3217 if( i == ehdr->e_shnum )
3219 errorBelch( "This ELF file contains no symtab" );
3223 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
3225 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
3226 shdr[i].sh_entsize, sizeof( Elf_Sym ) );
3231 return ocAllocateJumpIslands( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
3234 #endif /* powerpc */
3238 /* --------------------------------------------------------------------------
3240 * ------------------------------------------------------------------------*/
3242 #if defined(OBJFORMAT_MACHO)
3245 Support for MachO linking on Darwin/MacOS X on PowerPC chips
3246 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3248 I hereby formally apologize for the hackish nature of this code.
3249 Things that need to be done:
3250 *) implement ocVerifyImage_MachO
3251 *) add still more sanity checks.
3254 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3256 struct mach_header *header = (struct mach_header *) oc->image;
3257 struct load_command *lc = (struct load_command *) (header + 1);
3260 for( i = 0; i < header->ncmds; i++ )
3262 if( lc->cmd == LC_DYSYMTAB )
3264 struct dysymtab_command *dsymLC = (struct dysymtab_command *) lc;
3266 if( !ocAllocateJumpIslands( oc, dsymLC->nundefsym,
3267 dsymLC->iundefsym ) )
3270 break; // there can be only one LC_DSYMTAB
3272 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3277 static int ocVerifyImage_MachO(ObjectCode* oc)
3279 // FIXME: do some verifying here
3283 static int resolveImports(
3286 struct symtab_command *symLC,
3287 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3288 unsigned long *indirectSyms,
3289 struct nlist *nlist)
3293 for(i=0;i*4<sect->size;i++)
3295 // according to otool, reserved1 contains the first index into the indirect symbol table
3296 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3297 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3300 if((symbol->n_type & N_TYPE) == N_UNDF
3301 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3302 addr = (void*) (symbol->n_value);
3303 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3306 addr = lookupSymbol(nm);
3309 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3313 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3314 ((void**)(image + sect->offset))[i] = addr;
3320 static char* relocateAddress(
3323 struct section* sections,
3324 unsigned long address)
3327 for(i = 0; i < nSections; i++)
3329 if(sections[i].addr <= address
3330 && address < sections[i].addr + sections[i].size)
3332 return oc->image + sections[i].offset + address - sections[i].addr;
3335 barf("Invalid Mach-O file:"
3336 "Address out of bounds while relocating object file");
3340 static int relocateSection(
3343 struct symtab_command *symLC, struct nlist *nlist,
3344 int nSections, struct section* sections, struct section *sect)
3346 struct relocation_info *relocs;
3349 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3351 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3355 relocs = (struct relocation_info*) (image + sect->reloff);
3359 if(relocs[i].r_address & R_SCATTERED)
3361 struct scattered_relocation_info *scat =
3362 (struct scattered_relocation_info*) &relocs[i];
3366 if(scat->r_length == 2)
3368 unsigned long word = 0;
3369 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3370 checkProddableBlock(oc,wordPtr);
3372 // Step 1: Figure out what the relocated value should be
3373 if(scat->r_type == GENERIC_RELOC_VANILLA)
3375 word = scat->r_value + sect->offset + ((long) image);
3377 else if(scat->r_type == PPC_RELOC_SECTDIFF
3378 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3379 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3380 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3382 struct scattered_relocation_info *pair =
3383 (struct scattered_relocation_info*) &relocs[i+1];
3385 if(!pair->r_scattered || pair->r_type != PPC_RELOC_PAIR)
3386 barf("Invalid Mach-O file: "
3387 "PPC_RELOC_*_SECTDIFF not followed by PPC_RELOC_PAIR");
3389 word = (unsigned long)
3390 (relocateAddress(oc, nSections, sections, scat->r_value)
3391 - relocateAddress(oc, nSections, sections, pair->r_value));
3394 else if(scat->r_type == PPC_RELOC_HI16
3395 || scat->r_type == PPC_RELOC_LO16
3396 || scat->r_type == PPC_RELOC_HA16
3397 || scat->r_type == PPC_RELOC_LO14)
3398 { // these are generated by label+offset things
3399 struct relocation_info *pair = &relocs[i+1];
3400 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
3401 barf("Invalid Mach-O file: "
3402 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
3404 if(scat->r_type == PPC_RELOC_LO16)
3406 word = ((unsigned short*) wordPtr)[1];
3407 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3409 else if(scat->r_type == PPC_RELOC_LO14)
3411 barf("Unsupported Relocation: PPC_RELOC_LO14");
3412 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
3413 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3415 else if(scat->r_type == PPC_RELOC_HI16)
3417 word = ((unsigned short*) wordPtr)[1] << 16;
3418 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3420 else if(scat->r_type == PPC_RELOC_HA16)
3422 word = ((unsigned short*) wordPtr)[1] << 16;
3423 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3427 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
3433 continue; // ignore the others
3435 if(scat->r_type == GENERIC_RELOC_VANILLA
3436 || scat->r_type == PPC_RELOC_SECTDIFF)
3440 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
3442 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3444 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
3446 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3448 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
3450 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3451 + ((word & (1<<15)) ? 1 : 0);
3456 continue; // FIXME: I hope it's OK to ignore all the others.
3460 struct relocation_info *reloc = &relocs[i];
3461 if(reloc->r_pcrel && !reloc->r_extern)
3464 if(reloc->r_length == 2)
3466 unsigned long word = 0;
3467 unsigned long jumpIsland = 0;
3468 long offsetToJumpIsland;
3470 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3471 checkProddableBlock(oc,wordPtr);
3473 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3477 else if(reloc->r_type == PPC_RELOC_LO16)
3479 word = ((unsigned short*) wordPtr)[1];
3480 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3482 else if(reloc->r_type == PPC_RELOC_HI16)
3484 word = ((unsigned short*) wordPtr)[1] << 16;
3485 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3487 else if(reloc->r_type == PPC_RELOC_HA16)
3489 word = ((unsigned short*) wordPtr)[1] << 16;
3490 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3492 else if(reloc->r_type == PPC_RELOC_BR24)
3495 word = (word & 0x03FFFFFC) | (word & 0x02000000) ? 0xFC000000 : 0;
3499 if(!reloc->r_extern)
3502 sections[reloc->r_symbolnum-1].offset
3503 - sections[reloc->r_symbolnum-1].addr
3510 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3511 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3512 unsigned long symbolAddress = (unsigned long) (lookupSymbol(nm));
3515 errorBelch("\nunknown symbol `%s'", nm);
3522 word = symbolAddress;
3523 jumpIsland = makeJumpIsland(oc,reloc->r_symbolnum,word);
3524 word -= ((long)image) + sect->offset + reloc->r_address;
3527 offsetToJumpIsland = jumpIsland
3528 - (((long)image) + sect->offset + reloc->r_address);
3533 word += symbolAddress;
3537 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3542 else if(reloc->r_type == PPC_RELOC_LO16)
3544 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3547 else if(reloc->r_type == PPC_RELOC_HI16)
3549 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3552 else if(reloc->r_type == PPC_RELOC_HA16)
3554 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3555 + ((word & (1<<15)) ? 1 : 0);
3558 else if(reloc->r_type == PPC_RELOC_BR24)
3560 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3562 // The branch offset is too large.
3563 // Therefore, we try to use a jump island.
3565 barf("unconditional relative branch out of range: "
3566 "no jump island available");
3568 word = offsetToJumpIsland;
3569 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3570 barf("unconditional relative branch out of range: "
3571 "jump island out of range");
3573 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3577 barf("\nunknown relocation %d",reloc->r_type);
3584 static int ocGetNames_MachO(ObjectCode* oc)
3586 char *image = (char*) oc->image;
3587 struct mach_header *header = (struct mach_header*) image;
3588 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3589 unsigned i,curSymbol;
3590 struct segment_command *segLC = NULL;
3591 struct section *sections;
3592 struct symtab_command *symLC = NULL;
3593 struct dysymtab_command *dsymLC = NULL;
3594 struct nlist *nlist;
3595 unsigned long commonSize = 0;
3596 char *commonStorage = NULL;
3597 unsigned long commonCounter;
3599 for(i=0;i<header->ncmds;i++)
3601 if(lc->cmd == LC_SEGMENT)
3602 segLC = (struct segment_command*) lc;
3603 else if(lc->cmd == LC_SYMTAB)
3604 symLC = (struct symtab_command*) lc;
3605 else if(lc->cmd == LC_DYSYMTAB)
3606 dsymLC = (struct dysymtab_command*) lc;
3607 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3610 sections = (struct section*) (segLC+1);
3611 nlist = (struct nlist*) (image + symLC->symoff);
3613 for(i=0;i<segLC->nsects;i++)
3615 if(sections[i].size == 0)
3618 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
3620 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
3621 "ocGetNames_MachO(common symbols)");
3622 sections[i].offset = zeroFillArea - image;
3625 if(!strcmp(sections[i].sectname,"__text"))
3626 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3627 (void*) (image + sections[i].offset),
3628 (void*) (image + sections[i].offset + sections[i].size));
3629 else if(!strcmp(sections[i].sectname,"__const"))
3630 addSection(oc, SECTIONKIND_RWDATA,
3631 (void*) (image + sections[i].offset),
3632 (void*) (image + sections[i].offset + sections[i].size));
3633 else if(!strcmp(sections[i].sectname,"__data"))
3634 addSection(oc, SECTIONKIND_RWDATA,
3635 (void*) (image + sections[i].offset),
3636 (void*) (image + sections[i].offset + sections[i].size));
3637 else if(!strcmp(sections[i].sectname,"__bss")
3638 || !strcmp(sections[i].sectname,"__common"))
3639 addSection(oc, SECTIONKIND_RWDATA,
3640 (void*) (image + sections[i].offset),
3641 (void*) (image + sections[i].offset + sections[i].size));
3643 addProddableBlock(oc, (void*) (image + sections[i].offset),
3647 // count external symbols defined here
3649 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3651 if((nlist[i].n_type & N_TYPE) == N_SECT)
3654 for(i=0;i<symLC->nsyms;i++)
3656 if((nlist[i].n_type & N_TYPE) == N_UNDF
3657 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3659 commonSize += nlist[i].n_value;
3663 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3664 "ocGetNames_MachO(oc->symbols)");
3666 // insert symbols into hash table
3667 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3669 if((nlist[i].n_type & N_TYPE) == N_SECT)
3671 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3672 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3673 sections[nlist[i].n_sect-1].offset
3674 - sections[nlist[i].n_sect-1].addr
3675 + nlist[i].n_value);
3676 oc->symbols[curSymbol++] = nm;
3680 // insert local symbols into lochash
3681 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3683 if((nlist[i].n_type & N_TYPE) == N_SECT)
3685 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3686 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3687 sections[nlist[i].n_sect-1].offset
3688 - sections[nlist[i].n_sect-1].addr
3689 + nlist[i].n_value);
3694 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3695 commonCounter = (unsigned long)commonStorage;
3696 for(i=0;i<symLC->nsyms;i++)
3698 if((nlist[i].n_type & N_TYPE) == N_UNDF
3699 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3701 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3702 unsigned long sz = nlist[i].n_value;
3704 nlist[i].n_value = commonCounter;
3706 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3707 oc->symbols[curSymbol++] = nm;
3709 commonCounter += sz;
3715 static int ocResolve_MachO(ObjectCode* oc)
3717 char *image = (char*) oc->image;
3718 struct mach_header *header = (struct mach_header*) image;
3719 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3721 struct segment_command *segLC = NULL;
3722 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3723 struct symtab_command *symLC = NULL;
3724 struct dysymtab_command *dsymLC = NULL;
3725 struct nlist *nlist;
3726 unsigned long *indirectSyms;
3728 for(i=0;i<header->ncmds;i++)
3730 if(lc->cmd == LC_SEGMENT)
3731 segLC = (struct segment_command*) lc;
3732 else if(lc->cmd == LC_SYMTAB)
3733 symLC = (struct symtab_command*) lc;
3734 else if(lc->cmd == LC_DYSYMTAB)
3735 dsymLC = (struct dysymtab_command*) lc;
3736 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3739 sections = (struct section*) (segLC+1);
3740 nlist = (struct nlist*) (image + symLC->symoff);
3742 for(i=0;i<segLC->nsects;i++)
3744 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3745 la_ptrs = §ions[i];
3746 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3747 nl_ptrs = §ions[i];
3750 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3753 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
3756 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
3759 for(i=0;i<segLC->nsects;i++)
3761 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
3765 /* Free the local symbol table; we won't need it again. */
3766 freeHashTable(oc->lochash, NULL);
3769 #if defined (powerpc_TARGET_ARCH)
3770 ocFlushInstructionCache( oc );
3777 * The Mach-O object format uses leading underscores. But not everywhere.
3778 * There is a small number of runtime support functions defined in
3779 * libcc_dynamic.a whose name does not have a leading underscore.
3780 * As a consequence, we can't get their address from C code.
3781 * We have to use inline assembler just to take the address of a function.
3785 static void machoInitSymbolsWithoutUnderscore()
3791 __asm__ ("lis %0,hi16(" #x ")\n\tori %0,%0,lo16(" #x ")" : "=r" (p)); \
3792 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, p);
3794 RTS_MACHO_NOUNDERLINE_SYMBOLS