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) \
421 SymX(int2Integerzh_fast) \
422 SymX(integer2Intzh_fast) \
423 SymX(integer2Wordzh_fast) \
424 SymX(isCurrentThreadBoundzh_fast) \
425 SymX(isDoubleDenormalized) \
426 SymX(isDoubleInfinite) \
428 SymX(isDoubleNegativeZero) \
429 SymX(isEmptyMVarzh_fast) \
430 SymX(isFloatDenormalized) \
431 SymX(isFloatInfinite) \
433 SymX(isFloatNegativeZero) \
434 SymX(killThreadzh_fast) \
437 SymX(makeStablePtrzh_fast) \
438 SymX(minusIntegerzh_fast) \
439 SymX(mkApUpd0zh_fast) \
440 SymX(myThreadIdzh_fast) \
441 SymX(labelThreadzh_fast) \
442 SymX(newArrayzh_fast) \
443 SymX(newBCOzh_fast) \
444 SymX(newByteArrayzh_fast) \
445 SymX_redirect(newCAF, newDynCAF) \
446 SymX(newMVarzh_fast) \
447 SymX(newMutVarzh_fast) \
448 SymX(atomicModifyMutVarzh_fast) \
449 SymX(newPinnedByteArrayzh_fast) \
450 SymX(orIntegerzh_fast) \
452 SymX(performMajorGC) \
453 SymX(plusIntegerzh_fast) \
456 SymX(putMVarzh_fast) \
457 SymX(quotIntegerzh_fast) \
458 SymX(quotRemIntegerzh_fast) \
460 SymX(raiseIOzh_fast) \
461 SymX(remIntegerzh_fast) \
462 SymX(resetNonBlockingFd) \
466 SymX(rts_checkSchedStatus) \
469 SymX(rts_evalLazyIO) \
470 SymX(rts_evalStableIO) \
474 SymX(rts_getDouble) \
479 SymX(rts_getFunPtr) \
480 SymX(rts_getStablePtr) \
481 SymX(rts_getThreadId) \
483 SymX(rts_getWord32) \
496 SymX(rts_mkStablePtr) \
504 SymX(rtsSupportsBoundThreads) \
506 SymX(__hscore_get_saved_termios) \
507 SymX(__hscore_set_saved_termios) \
509 SymX(startupHaskell) \
510 SymX(shutdownHaskell) \
511 SymX(shutdownHaskellAndExit) \
512 SymX(stable_ptr_table) \
513 SymX(stackOverflow) \
514 SymX(stg_CAF_BLACKHOLE_info) \
515 SymX(stg_BLACKHOLE_BQ_info) \
516 SymX(awakenBlockedQueue) \
517 SymX(stg_CHARLIKE_closure) \
518 SymX(stg_EMPTY_MVAR_info) \
519 SymX(stg_IND_STATIC_info) \
520 SymX(stg_INTLIKE_closure) \
521 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
522 SymX(stg_WEAK_info) \
523 SymX(stg_ap_0_info) \
524 SymX(stg_ap_v_info) \
525 SymX(stg_ap_f_info) \
526 SymX(stg_ap_d_info) \
527 SymX(stg_ap_l_info) \
528 SymX(stg_ap_n_info) \
529 SymX(stg_ap_p_info) \
530 SymX(stg_ap_pv_info) \
531 SymX(stg_ap_pp_info) \
532 SymX(stg_ap_ppv_info) \
533 SymX(stg_ap_ppp_info) \
534 SymX(stg_ap_pppv_info) \
535 SymX(stg_ap_pppp_info) \
536 SymX(stg_ap_ppppp_info) \
537 SymX(stg_ap_pppppp_info) \
538 SymX(stg_ap_1_upd_info) \
539 SymX(stg_ap_2_upd_info) \
540 SymX(stg_ap_3_upd_info) \
541 SymX(stg_ap_4_upd_info) \
542 SymX(stg_ap_5_upd_info) \
543 SymX(stg_ap_6_upd_info) \
544 SymX(stg_ap_7_upd_info) \
546 SymX(stg_sel_0_upd_info) \
547 SymX(stg_sel_10_upd_info) \
548 SymX(stg_sel_11_upd_info) \
549 SymX(stg_sel_12_upd_info) \
550 SymX(stg_sel_13_upd_info) \
551 SymX(stg_sel_14_upd_info) \
552 SymX(stg_sel_15_upd_info) \
553 SymX(stg_sel_1_upd_info) \
554 SymX(stg_sel_2_upd_info) \
555 SymX(stg_sel_3_upd_info) \
556 SymX(stg_sel_4_upd_info) \
557 SymX(stg_sel_5_upd_info) \
558 SymX(stg_sel_6_upd_info) \
559 SymX(stg_sel_7_upd_info) \
560 SymX(stg_sel_8_upd_info) \
561 SymX(stg_sel_9_upd_info) \
562 SymX(stg_upd_frame_info) \
563 SymX(suspendThread) \
564 SymX(takeMVarzh_fast) \
565 SymX(timesIntegerzh_fast) \
566 SymX(tryPutMVarzh_fast) \
567 SymX(tryTakeMVarzh_fast) \
568 SymX(unblockAsyncExceptionszh_fast) \
570 SymX(unsafeThawArrayzh_fast) \
571 SymX(waitReadzh_fast) \
572 SymX(waitWritezh_fast) \
573 SymX(word2Integerzh_fast) \
574 SymX(xorIntegerzh_fast) \
577 #ifdef SUPPORT_LONG_LONGS
578 #define RTS_LONG_LONG_SYMS \
579 SymX(int64ToIntegerzh_fast) \
580 SymX(word64ToIntegerzh_fast)
582 #define RTS_LONG_LONG_SYMS /* nothing */
585 // 64-bit support functions in libgcc.a
586 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
587 #define RTS_LIBGCC_SYMBOLS \
596 #elif defined(ia64_TARGET_ARCH)
597 #define RTS_LIBGCC_SYMBOLS \
605 #define RTS_LIBGCC_SYMBOLS
608 #ifdef darwin_TARGET_OS
609 // Symbols that don't have a leading underscore
610 // on Mac OS X. They have to receive special treatment,
611 // see machoInitSymbolsWithoutUnderscore()
612 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
617 /* entirely bogus claims about types of these symbols */
618 #define Sym(vvv) extern void vvv(void);
619 #define SymX(vvv) /**/
620 #define SymX_redirect(vvv,xxx) /**/
624 RTS_POSIX_ONLY_SYMBOLS
625 RTS_MINGW_ONLY_SYMBOLS
626 RTS_CYGWIN_ONLY_SYMBOLS
632 #ifdef LEADING_UNDERSCORE
633 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
635 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
638 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
640 #define SymX(vvv) Sym(vvv)
642 // SymX_redirect allows us to redirect references to one symbol to
643 // another symbol. See newCAF/newDynCAF for an example.
644 #define SymX_redirect(vvv,xxx) \
645 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
648 static RtsSymbolVal rtsSyms[] = {
652 RTS_POSIX_ONLY_SYMBOLS
653 RTS_MINGW_ONLY_SYMBOLS
654 RTS_CYGWIN_ONLY_SYMBOLS
656 { 0, 0 } /* sentinel */
659 /* -----------------------------------------------------------------------------
660 * Insert symbols into hash tables, checking for duplicates.
662 static void ghciInsertStrHashTable ( char* obj_name,
668 if (lookupHashTable(table, (StgWord)key) == NULL)
670 insertStrHashTable(table, (StgWord)key, data);
675 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
677 "whilst processing object file\n"
679 "This could be caused by:\n"
680 " * Loading two different object files which export the same symbol\n"
681 " * Specifying the same object file twice on the GHCi command line\n"
682 " * An incorrect `package.conf' entry, causing some object to be\n"
684 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
693 /* -----------------------------------------------------------------------------
694 * initialize the object linker
698 static int linker_init_done = 0 ;
700 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
701 static void *dl_prog_handle;
704 /* dlopen(NULL,..) doesn't work so we grab libc explicitly */
705 #if defined(openbsd_TARGET_OS)
706 static void *dl_libc_handle;
714 /* Make initLinker idempotent, so we can call it
715 before evey relevant operation; that means we
716 don't need to initialise the linker separately */
717 if (linker_init_done == 1) { return; } else {
718 linker_init_done = 1;
721 symhash = allocStrHashTable();
723 /* populate the symbol table with stuff from the RTS */
724 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
725 ghciInsertStrHashTable("(GHCi built-in symbols)",
726 symhash, sym->lbl, sym->addr);
728 # if defined(OBJFORMAT_MACHO)
729 machoInitSymbolsWithoutUnderscore();
732 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
733 # if defined(RTLD_DEFAULT)
734 dl_prog_handle = RTLD_DEFAULT;
736 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
737 # if defined(openbsd_TARGET_OS)
738 dl_libc_handle = dlopen("libc.so", RTLD_LAZY);
740 # endif // RTLD_DEFAULT
744 /* -----------------------------------------------------------------------------
745 * Loading DLL or .so dynamic libraries
746 * -----------------------------------------------------------------------------
748 * Add a DLL from which symbols may be found. In the ELF case, just
749 * do RTLD_GLOBAL-style add, so no further messing around needs to
750 * happen in order that symbols in the loaded .so are findable --
751 * lookupSymbol() will subsequently see them by dlsym on the program's
752 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
754 * In the PEi386 case, open the DLLs and put handles to them in a
755 * linked list. When looking for a symbol, try all handles in the
756 * list. This means that we need to load even DLLs that are guaranteed
757 * to be in the ghc.exe image already, just so we can get a handle
758 * to give to loadSymbol, so that we can find the symbols. For such
759 * libraries, the LoadLibrary call should be a no-op except for returning
764 #if defined(OBJFORMAT_PEi386)
765 /* A record for storing handles into DLLs. */
770 struct _OpenedDLL* next;
775 /* A list thereof. */
776 static OpenedDLL* opened_dlls = NULL;
780 addDLL( char *dll_name )
782 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
783 /* ------------------- ELF DLL loader ------------------- */
789 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
792 /* dlopen failed; return a ptr to the error msg. */
794 if (errmsg == NULL) errmsg = "addDLL: unknown error";
801 # elif defined(OBJFORMAT_PEi386)
802 /* ------------------- Win32 DLL loader ------------------- */
810 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
812 /* See if we've already got it, and ignore if so. */
813 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
814 if (0 == strcmp(o_dll->name, dll_name))
818 /* The file name has no suffix (yet) so that we can try
819 both foo.dll and foo.drv
821 The documentation for LoadLibrary says:
822 If no file name extension is specified in the lpFileName
823 parameter, the default library extension .dll is
824 appended. However, the file name string can include a trailing
825 point character (.) to indicate that the module name has no
828 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
829 sprintf(buf, "%s.DLL", dll_name);
830 instance = LoadLibrary(buf);
831 if (instance == NULL) {
832 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
833 instance = LoadLibrary(buf);
834 if (instance == NULL) {
837 /* LoadLibrary failed; return a ptr to the error msg. */
838 return "addDLL: unknown error";
843 /* Add this DLL to the list of DLLs in which to search for symbols. */
844 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
845 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
846 strcpy(o_dll->name, dll_name);
847 o_dll->instance = instance;
848 o_dll->next = opened_dlls;
853 barf("addDLL: not implemented on this platform");
857 /* -----------------------------------------------------------------------------
858 * lookup a symbol in the hash table
861 lookupSymbol( char *lbl )
865 ASSERT(symhash != NULL);
866 val = lookupStrHashTable(symhash, lbl);
869 # if defined(OBJFORMAT_ELF)
870 # if defined(openbsd_TARGET_OS)
871 val = dlsym(dl_prog_handle, lbl);
872 return (val != NULL) ? val : dlsym(dl_libc_handle,lbl);
873 # else /* not openbsd */
874 return dlsym(dl_prog_handle, lbl);
876 # elif defined(OBJFORMAT_MACHO)
877 if(NSIsSymbolNameDefined(lbl)) {
878 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
879 return NSAddressOfSymbol(symbol);
883 # elif defined(OBJFORMAT_PEi386)
886 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
887 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
889 /* HACK: if the name has an initial underscore, try stripping
890 it off & look that up first. I've yet to verify whether there's
891 a Rule that governs whether an initial '_' *should always* be
892 stripped off when mapping from import lib name to the DLL name.
894 sym = GetProcAddress(o_dll->instance, (lbl+1));
896 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
900 sym = GetProcAddress(o_dll->instance, lbl);
902 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
917 __attribute((unused))
919 lookupLocalSymbol( ObjectCode* oc, char *lbl )
923 val = lookupStrHashTable(oc->lochash, lbl);
933 /* -----------------------------------------------------------------------------
934 * Debugging aid: look in GHCi's object symbol tables for symbols
935 * within DELTA bytes of the specified address, and show their names.
938 void ghci_enquire ( char* addr );
940 void ghci_enquire ( char* addr )
945 const int DELTA = 64;
950 for (oc = objects; oc; oc = oc->next) {
951 for (i = 0; i < oc->n_symbols; i++) {
952 sym = oc->symbols[i];
953 if (sym == NULL) continue;
954 // debugBelch("enquire %p %p\n", sym, oc->lochash);
956 if (oc->lochash != NULL) {
957 a = lookupStrHashTable(oc->lochash, sym);
960 a = lookupStrHashTable(symhash, sym);
963 // debugBelch("ghci_enquire: can't find %s\n", sym);
965 else if (addr-DELTA <= a && a <= addr+DELTA) {
966 debugBelch("%p + %3d == `%s'\n", addr, a - addr, sym);
973 #ifdef ia64_TARGET_ARCH
974 static unsigned int PLTSize(void);
977 /* -----------------------------------------------------------------------------
978 * Load an obj (populate the global symbol table, but don't resolve yet)
980 * Returns: 1 if ok, 0 on error.
983 loadObj( char *path )
990 void *map_addr = NULL;
997 /* debugBelch("loadObj %s\n", path ); */
999 /* Check that we haven't already loaded this object.
1000 Ignore requests to load multiple times */
1004 for (o = objects; o; o = o->next) {
1005 if (0 == strcmp(o->fileName, path)) {
1007 break; /* don't need to search further */
1011 IF_DEBUG(linker, debugBelch(
1012 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1013 "same object file twice:\n"
1015 "GHCi will ignore this, but be warned.\n"
1017 return 1; /* success */
1021 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
1023 # if defined(OBJFORMAT_ELF)
1024 oc->formatName = "ELF";
1025 # elif defined(OBJFORMAT_PEi386)
1026 oc->formatName = "PEi386";
1027 # elif defined(OBJFORMAT_MACHO)
1028 oc->formatName = "Mach-O";
1031 barf("loadObj: not implemented on this platform");
1034 r = stat(path, &st);
1035 if (r == -1) { return 0; }
1037 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1038 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1039 strcpy(oc->fileName, path);
1041 oc->fileSize = st.st_size;
1043 oc->sections = NULL;
1044 oc->lochash = allocStrHashTable();
1045 oc->proddables = NULL;
1047 /* chain it onto the list of objects */
1052 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1054 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1056 #if defined(openbsd_TARGET_OS)
1057 fd = open(path, O_RDONLY, S_IRUSR);
1059 fd = open(path, O_RDONLY);
1062 barf("loadObj: can't open `%s'", path);
1064 pagesize = getpagesize();
1066 #ifdef ia64_TARGET_ARCH
1067 /* The PLT needs to be right before the object */
1068 n = ROUND_UP(PLTSize(), pagesize);
1069 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1070 if (oc->plt == MAP_FAILED)
1071 barf("loadObj: can't allocate PLT");
1074 map_addr = oc->plt + n;
1077 n = ROUND_UP(oc->fileSize, pagesize);
1078 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1079 if (oc->image == MAP_FAILED)
1080 barf("loadObj: can't map `%s'", path);
1084 #else /* !USE_MMAP */
1086 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
1088 /* load the image into memory */
1089 f = fopen(path, "rb");
1091 barf("loadObj: can't read `%s'", path);
1093 n = fread ( oc->image, 1, oc->fileSize, f );
1094 if (n != oc->fileSize)
1095 barf("loadObj: error whilst reading `%s'", path);
1099 #endif /* USE_MMAP */
1101 # if defined(OBJFORMAT_MACHO)
1102 r = ocAllocateJumpIslands_MachO ( oc );
1103 if (!r) { return r; }
1104 # elif defined(OBJFORMAT_ELF) && defined(powerpc_TARGET_ARCH)
1105 r = ocAllocateJumpIslands_ELF ( oc );
1106 if (!r) { return r; }
1109 /* verify the in-memory image */
1110 # if defined(OBJFORMAT_ELF)
1111 r = ocVerifyImage_ELF ( oc );
1112 # elif defined(OBJFORMAT_PEi386)
1113 r = ocVerifyImage_PEi386 ( oc );
1114 # elif defined(OBJFORMAT_MACHO)
1115 r = ocVerifyImage_MachO ( oc );
1117 barf("loadObj: no verify method");
1119 if (!r) { return r; }
1121 /* build the symbol list for this image */
1122 # if defined(OBJFORMAT_ELF)
1123 r = ocGetNames_ELF ( oc );
1124 # elif defined(OBJFORMAT_PEi386)
1125 r = ocGetNames_PEi386 ( oc );
1126 # elif defined(OBJFORMAT_MACHO)
1127 r = ocGetNames_MachO ( oc );
1129 barf("loadObj: no getNames method");
1131 if (!r) { return r; }
1133 /* loaded, but not resolved yet */
1134 oc->status = OBJECT_LOADED;
1139 /* -----------------------------------------------------------------------------
1140 * resolve all the currently unlinked objects in memory
1142 * Returns: 1 if ok, 0 on error.
1152 for (oc = objects; oc; oc = oc->next) {
1153 if (oc->status != OBJECT_RESOLVED) {
1154 # if defined(OBJFORMAT_ELF)
1155 r = ocResolve_ELF ( oc );
1156 # elif defined(OBJFORMAT_PEi386)
1157 r = ocResolve_PEi386 ( oc );
1158 # elif defined(OBJFORMAT_MACHO)
1159 r = ocResolve_MachO ( oc );
1161 barf("resolveObjs: not implemented on this platform");
1163 if (!r) { return r; }
1164 oc->status = OBJECT_RESOLVED;
1170 /* -----------------------------------------------------------------------------
1171 * delete an object from the pool
1174 unloadObj( char *path )
1176 ObjectCode *oc, *prev;
1178 ASSERT(symhash != NULL);
1179 ASSERT(objects != NULL);
1184 for (oc = objects; oc; prev = oc, oc = oc->next) {
1185 if (!strcmp(oc->fileName,path)) {
1187 /* Remove all the mappings for the symbols within this
1192 for (i = 0; i < oc->n_symbols; i++) {
1193 if (oc->symbols[i] != NULL) {
1194 removeStrHashTable(symhash, oc->symbols[i], NULL);
1202 prev->next = oc->next;
1205 /* We're going to leave this in place, in case there are
1206 any pointers from the heap into it: */
1207 /* stgFree(oc->image); */
1208 stgFree(oc->fileName);
1209 stgFree(oc->symbols);
1210 stgFree(oc->sections);
1211 /* The local hash table should have been freed at the end
1212 of the ocResolve_ call on it. */
1213 ASSERT(oc->lochash == NULL);
1219 errorBelch("unloadObj: can't find `%s' to unload", path);
1223 /* -----------------------------------------------------------------------------
1224 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1225 * which may be prodded during relocation, and abort if we try and write
1226 * outside any of these.
1228 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1231 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1232 /* debugBelch("aPB %p %p %d\n", oc, start, size); */
1236 pb->next = oc->proddables;
1237 oc->proddables = pb;
1240 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1243 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1244 char* s = (char*)(pb->start);
1245 char* e = s + pb->size - 1;
1246 char* a = (char*)addr;
1247 /* Assumes that the biggest fixup involves a 4-byte write. This
1248 probably needs to be changed to 8 (ie, +7) on 64-bit
1250 if (a >= s && (a+3) <= e) return;
1252 barf("checkProddableBlock: invalid fixup in runtime linker");
1255 /* -----------------------------------------------------------------------------
1256 * Section management.
1258 static void addSection ( ObjectCode* oc, SectionKind kind,
1259 void* start, void* end )
1261 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1265 s->next = oc->sections;
1268 debugBelch("addSection: %p-%p (size %d), kind %d\n",
1269 start, ((char*)end)-1, end - start + 1, kind );
1274 /* --------------------------------------------------------------------------
1275 * PowerPC specifics (jump islands)
1276 * ------------------------------------------------------------------------*/
1278 #if defined(powerpc_TARGET_ARCH)
1281 ocAllocateJumpIslands
1283 Allocate additional space at the end of the object file image to make room
1286 PowerPC relative branch instructions have a 24 bit displacement field.
1287 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
1288 If a particular imported symbol is outside this range, we have to redirect
1289 the jump to a short piece of new code that just loads the 32bit absolute
1290 address and jumps there.
1291 This function just allocates space for one 16 byte ppcJumpIsland for every
1292 undefined symbol in the object file. The code for the islands is filled in by
1293 makeJumpIsland below.
1296 static int ocAllocateJumpIslands( ObjectCode* oc, int count, int first )
1303 #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined
1305 // round up to the nearest 4
1306 aligned = (oc->fileSize + 3) & ~3;
1308 oc->image = stgReallocBytes( oc->image,
1309 aligned + sizeof( ppcJumpIsland ) * count,
1310 "ocAllocateJumpIslands" );
1311 oc->jump_islands = (ppcJumpIsland *) (((char *) oc->image) + aligned);
1312 memset( oc->jump_islands, 0, sizeof( ppcJumpIsland ) * count );
1315 oc->jump_islands = NULL;
1317 oc->island_start_symbol = first;
1318 oc->n_islands = count;
1323 static unsigned long makeJumpIsland( ObjectCode* oc,
1324 unsigned long symbolNumber,
1325 unsigned long target )
1327 ppcJumpIsland *island;
1329 if( symbolNumber < oc->island_start_symbol ||
1330 symbolNumber - oc->island_start_symbol > oc->n_islands)
1333 island = &oc->jump_islands[symbolNumber - oc->island_start_symbol];
1335 // lis r12, hi16(target)
1336 island->lis_r12 = 0x3d80;
1337 island->hi_addr = target >> 16;
1339 // ori r12, r12, lo16(target)
1340 island->ori_r12_r12 = 0x618c;
1341 island->lo_addr = target & 0xffff;
1344 island->mtctr_r12 = 0x7d8903a6;
1347 island->bctr = 0x4e800420;
1349 return (unsigned long) island;
1353 ocFlushInstructionCache
1355 Flush the data & instruction caches.
1356 Because the PPC has split data/instruction caches, we have to
1357 do that whenever we modify code at runtime.
1360 static void ocFlushInstructionCache( ObjectCode *oc )
1362 int n = (oc->fileSize + sizeof( ppcJumpIsland ) * oc->n_islands + 3) / 4;
1363 unsigned long *p = (unsigned long *) oc->image;
1367 __asm__ volatile ( "dcbf 0,%0\n\t"
1375 __asm__ volatile ( "sync\n\t"
1381 /* --------------------------------------------------------------------------
1382 * PEi386 specifics (Win32 targets)
1383 * ------------------------------------------------------------------------*/
1385 /* The information for this linker comes from
1386 Microsoft Portable Executable
1387 and Common Object File Format Specification
1388 revision 5.1 January 1998
1389 which SimonM says comes from the MS Developer Network CDs.
1391 It can be found there (on older CDs), but can also be found
1394 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1396 (this is Rev 6.0 from February 1999).
1398 Things move, so if that fails, try searching for it via
1400 http://www.google.com/search?q=PE+COFF+specification
1402 The ultimate reference for the PE format is the Winnt.h
1403 header file that comes with the Platform SDKs; as always,
1404 implementations will drift wrt their documentation.
1406 A good background article on the PE format is Matt Pietrek's
1407 March 1994 article in Microsoft System Journal (MSJ)
1408 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1409 Win32 Portable Executable File Format." The info in there
1410 has recently been updated in a two part article in
1411 MSDN magazine, issues Feb and March 2002,
1412 "Inside Windows: An In-Depth Look into the Win32 Portable
1413 Executable File Format"
1415 John Levine's book "Linkers and Loaders" contains useful
1420 #if defined(OBJFORMAT_PEi386)
1424 typedef unsigned char UChar;
1425 typedef unsigned short UInt16;
1426 typedef unsigned int UInt32;
1433 UInt16 NumberOfSections;
1434 UInt32 TimeDateStamp;
1435 UInt32 PointerToSymbolTable;
1436 UInt32 NumberOfSymbols;
1437 UInt16 SizeOfOptionalHeader;
1438 UInt16 Characteristics;
1442 #define sizeof_COFF_header 20
1449 UInt32 VirtualAddress;
1450 UInt32 SizeOfRawData;
1451 UInt32 PointerToRawData;
1452 UInt32 PointerToRelocations;
1453 UInt32 PointerToLinenumbers;
1454 UInt16 NumberOfRelocations;
1455 UInt16 NumberOfLineNumbers;
1456 UInt32 Characteristics;
1460 #define sizeof_COFF_section 40
1467 UInt16 SectionNumber;
1470 UChar NumberOfAuxSymbols;
1474 #define sizeof_COFF_symbol 18
1479 UInt32 VirtualAddress;
1480 UInt32 SymbolTableIndex;
1485 #define sizeof_COFF_reloc 10
1488 /* From PE spec doc, section 3.3.2 */
1489 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1490 windows.h -- for the same purpose, but I want to know what I'm
1492 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1493 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1494 #define MYIMAGE_FILE_DLL 0x2000
1495 #define MYIMAGE_FILE_SYSTEM 0x1000
1496 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1497 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1498 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1500 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1501 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1502 #define MYIMAGE_SYM_CLASS_STATIC 3
1503 #define MYIMAGE_SYM_UNDEFINED 0
1505 /* From PE spec doc, section 4.1 */
1506 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1507 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1508 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1510 /* From PE spec doc, section 5.2.1 */
1511 #define MYIMAGE_REL_I386_DIR32 0x0006
1512 #define MYIMAGE_REL_I386_REL32 0x0014
1515 /* We use myindex to calculate array addresses, rather than
1516 simply doing the normal subscript thing. That's because
1517 some of the above structs have sizes which are not
1518 a whole number of words. GCC rounds their sizes up to a
1519 whole number of words, which means that the address calcs
1520 arising from using normal C indexing or pointer arithmetic
1521 are just plain wrong. Sigh.
1524 myindex ( int scale, void* base, int index )
1527 ((UChar*)base) + scale * index;
1532 printName ( UChar* name, UChar* strtab )
1534 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1535 UInt32 strtab_offset = * (UInt32*)(name+4);
1536 debugBelch("%s", strtab + strtab_offset );
1539 for (i = 0; i < 8; i++) {
1540 if (name[i] == 0) break;
1541 debugBelch("%c", name[i] );
1548 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1550 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1551 UInt32 strtab_offset = * (UInt32*)(name+4);
1552 strncpy ( dst, strtab+strtab_offset, dstSize );
1558 if (name[i] == 0) break;
1568 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1571 /* If the string is longer than 8 bytes, look in the
1572 string table for it -- this will be correctly zero terminated.
1574 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1575 UInt32 strtab_offset = * (UInt32*)(name+4);
1576 return ((UChar*)strtab) + strtab_offset;
1578 /* Otherwise, if shorter than 8 bytes, return the original,
1579 which by defn is correctly terminated.
1581 if (name[7]==0) return name;
1582 /* The annoying case: 8 bytes. Copy into a temporary
1583 (which is never freed ...)
1585 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1587 strncpy(newstr,name,8);
1593 /* Just compares the short names (first 8 chars) */
1594 static COFF_section *
1595 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1599 = (COFF_header*)(oc->image);
1600 COFF_section* sectab
1602 ((UChar*)(oc->image))
1603 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1605 for (i = 0; i < hdr->NumberOfSections; i++) {
1608 COFF_section* section_i
1610 myindex ( sizeof_COFF_section, sectab, i );
1611 n1 = (UChar*) &(section_i->Name);
1613 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1614 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1615 n1[6]==n2[6] && n1[7]==n2[7])
1624 zapTrailingAtSign ( UChar* sym )
1626 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1628 if (sym[0] == 0) return;
1630 while (sym[i] != 0) i++;
1633 while (j > 0 && my_isdigit(sym[j])) j--;
1634 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1640 ocVerifyImage_PEi386 ( ObjectCode* oc )
1645 COFF_section* sectab;
1646 COFF_symbol* symtab;
1648 /* debugBelch("\nLOADING %s\n", oc->fileName); */
1649 hdr = (COFF_header*)(oc->image);
1650 sectab = (COFF_section*) (
1651 ((UChar*)(oc->image))
1652 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1654 symtab = (COFF_symbol*) (
1655 ((UChar*)(oc->image))
1656 + hdr->PointerToSymbolTable
1658 strtab = ((UChar*)symtab)
1659 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1661 if (hdr->Machine != 0x14c) {
1662 errorBelch("Not x86 PEi386");
1665 if (hdr->SizeOfOptionalHeader != 0) {
1666 errorBelch("PEi386 with nonempty optional header");
1669 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1670 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1671 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1672 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1673 errorBelch("Not a PEi386 object file");
1676 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1677 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1678 errorBelch("Invalid PEi386 word size or endiannness: %d",
1679 (int)(hdr->Characteristics));
1682 /* If the string table size is way crazy, this might indicate that
1683 there are more than 64k relocations, despite claims to the
1684 contrary. Hence this test. */
1685 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
1687 if ( (*(UInt32*)strtab) > 600000 ) {
1688 /* Note that 600k has no special significance other than being
1689 big enough to handle the almost-2MB-sized lumps that
1690 constitute HSwin32*.o. */
1691 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
1696 /* No further verification after this point; only debug printing. */
1698 IF_DEBUG(linker, i=1);
1699 if (i == 0) return 1;
1701 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1702 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1703 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1706 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1707 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1708 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1709 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1710 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1711 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1712 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1714 /* Print the section table. */
1716 for (i = 0; i < hdr->NumberOfSections; i++) {
1718 COFF_section* sectab_i
1720 myindex ( sizeof_COFF_section, sectab, i );
1727 printName ( sectab_i->Name, strtab );
1737 sectab_i->VirtualSize,
1738 sectab_i->VirtualAddress,
1739 sectab_i->SizeOfRawData,
1740 sectab_i->PointerToRawData,
1741 sectab_i->NumberOfRelocations,
1742 sectab_i->PointerToRelocations,
1743 sectab_i->PointerToRawData
1745 reltab = (COFF_reloc*) (
1746 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1749 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1750 /* If the relocation field (a short) has overflowed, the
1751 * real count can be found in the first reloc entry.
1753 * See Section 4.1 (last para) of the PE spec (rev6.0).
1755 COFF_reloc* rel = (COFF_reloc*)
1756 myindex ( sizeof_COFF_reloc, reltab, 0 );
1757 noRelocs = rel->VirtualAddress;
1760 noRelocs = sectab_i->NumberOfRelocations;
1764 for (; j < noRelocs; j++) {
1766 COFF_reloc* rel = (COFF_reloc*)
1767 myindex ( sizeof_COFF_reloc, reltab, j );
1769 " type 0x%-4x vaddr 0x%-8x name `",
1771 rel->VirtualAddress );
1772 sym = (COFF_symbol*)
1773 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1774 /* Hmm..mysterious looking offset - what's it for? SOF */
1775 printName ( sym->Name, strtab -10 );
1782 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
1783 debugBelch("---START of string table---\n");
1784 for (i = 4; i < *(Int32*)strtab; i++) {
1786 debugBelch("\n"); else
1787 debugBelch("%c", strtab[i] );
1789 debugBelch("--- END of string table---\n");
1794 COFF_symbol* symtab_i;
1795 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1796 symtab_i = (COFF_symbol*)
1797 myindex ( sizeof_COFF_symbol, symtab, i );
1803 printName ( symtab_i->Name, strtab );
1812 (Int32)(symtab_i->SectionNumber),
1813 (UInt32)symtab_i->Type,
1814 (UInt32)symtab_i->StorageClass,
1815 (UInt32)symtab_i->NumberOfAuxSymbols
1817 i += symtab_i->NumberOfAuxSymbols;
1827 ocGetNames_PEi386 ( ObjectCode* oc )
1830 COFF_section* sectab;
1831 COFF_symbol* symtab;
1838 hdr = (COFF_header*)(oc->image);
1839 sectab = (COFF_section*) (
1840 ((UChar*)(oc->image))
1841 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1843 symtab = (COFF_symbol*) (
1844 ((UChar*)(oc->image))
1845 + hdr->PointerToSymbolTable
1847 strtab = ((UChar*)(oc->image))
1848 + hdr->PointerToSymbolTable
1849 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1851 /* Allocate space for any (local, anonymous) .bss sections. */
1853 for (i = 0; i < hdr->NumberOfSections; i++) {
1855 COFF_section* sectab_i
1857 myindex ( sizeof_COFF_section, sectab, i );
1858 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1859 if (sectab_i->VirtualSize == 0) continue;
1860 /* This is a non-empty .bss section. Allocate zeroed space for
1861 it, and set its PointerToRawData field such that oc->image +
1862 PointerToRawData == addr_of_zeroed_space. */
1863 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1864 "ocGetNames_PEi386(anonymous bss)");
1865 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1866 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1867 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
1870 /* Copy section information into the ObjectCode. */
1872 for (i = 0; i < hdr->NumberOfSections; i++) {
1878 = SECTIONKIND_OTHER;
1879 COFF_section* sectab_i
1881 myindex ( sizeof_COFF_section, sectab, i );
1882 IF_DEBUG(linker, debugBelch("section name = %s\n", sectab_i->Name ));
1885 /* I'm sure this is the Right Way to do it. However, the
1886 alternative of testing the sectab_i->Name field seems to
1887 work ok with Cygwin.
1889 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1890 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1891 kind = SECTIONKIND_CODE_OR_RODATA;
1894 if (0==strcmp(".text",sectab_i->Name) ||
1895 0==strcmp(".rodata",sectab_i->Name))
1896 kind = SECTIONKIND_CODE_OR_RODATA;
1897 if (0==strcmp(".data",sectab_i->Name) ||
1898 0==strcmp(".bss",sectab_i->Name))
1899 kind = SECTIONKIND_RWDATA;
1901 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1902 sz = sectab_i->SizeOfRawData;
1903 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1905 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1906 end = start + sz - 1;
1908 if (kind == SECTIONKIND_OTHER
1909 /* Ignore sections called which contain stabs debugging
1911 && 0 != strcmp(".stab", sectab_i->Name)
1912 && 0 != strcmp(".stabstr", sectab_i->Name)
1914 errorBelch("Unknown PEi386 section name `%s'", sectab_i->Name);
1918 if (kind != SECTIONKIND_OTHER && end >= start) {
1919 addSection(oc, kind, start, end);
1920 addProddableBlock(oc, start, end - start + 1);
1924 /* Copy exported symbols into the ObjectCode. */
1926 oc->n_symbols = hdr->NumberOfSymbols;
1927 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1928 "ocGetNames_PEi386(oc->symbols)");
1929 /* Call me paranoid; I don't care. */
1930 for (i = 0; i < oc->n_symbols; i++)
1931 oc->symbols[i] = NULL;
1935 COFF_symbol* symtab_i;
1936 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1937 symtab_i = (COFF_symbol*)
1938 myindex ( sizeof_COFF_symbol, symtab, i );
1942 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1943 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1944 /* This symbol is global and defined, viz, exported */
1945 /* for MYIMAGE_SYMCLASS_EXTERNAL
1946 && !MYIMAGE_SYM_UNDEFINED,
1947 the address of the symbol is:
1948 address of relevant section + offset in section
1950 COFF_section* sectabent
1951 = (COFF_section*) myindex ( sizeof_COFF_section,
1953 symtab_i->SectionNumber-1 );
1954 addr = ((UChar*)(oc->image))
1955 + (sectabent->PointerToRawData
1959 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1960 && symtab_i->Value > 0) {
1961 /* This symbol isn't in any section at all, ie, global bss.
1962 Allocate zeroed space for it. */
1963 addr = stgCallocBytes(1, symtab_i->Value,
1964 "ocGetNames_PEi386(non-anonymous bss)");
1965 addSection(oc, SECTIONKIND_RWDATA, addr,
1966 ((UChar*)addr) + symtab_i->Value - 1);
1967 addProddableBlock(oc, addr, symtab_i->Value);
1968 /* debugBelch("BSS section at 0x%x\n", addr); */
1971 if (addr != NULL ) {
1972 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1973 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
1974 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
1975 ASSERT(i >= 0 && i < oc->n_symbols);
1976 /* cstring_from_COFF_symbol_name always succeeds. */
1977 oc->symbols[i] = sname;
1978 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1982 "IGNORING symbol %d\n"
1986 printName ( symtab_i->Name, strtab );
1995 (Int32)(symtab_i->SectionNumber),
1996 (UInt32)symtab_i->Type,
1997 (UInt32)symtab_i->StorageClass,
1998 (UInt32)symtab_i->NumberOfAuxSymbols
2003 i += symtab_i->NumberOfAuxSymbols;
2012 ocResolve_PEi386 ( ObjectCode* oc )
2015 COFF_section* sectab;
2016 COFF_symbol* symtab;
2026 /* ToDo: should be variable-sized? But is at least safe in the
2027 sense of buffer-overrun-proof. */
2029 /* debugBelch("resolving for %s\n", oc->fileName); */
2031 hdr = (COFF_header*)(oc->image);
2032 sectab = (COFF_section*) (
2033 ((UChar*)(oc->image))
2034 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2036 symtab = (COFF_symbol*) (
2037 ((UChar*)(oc->image))
2038 + hdr->PointerToSymbolTable
2040 strtab = ((UChar*)(oc->image))
2041 + hdr->PointerToSymbolTable
2042 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2044 for (i = 0; i < hdr->NumberOfSections; i++) {
2045 COFF_section* sectab_i
2047 myindex ( sizeof_COFF_section, sectab, i );
2050 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2053 /* Ignore sections called which contain stabs debugging
2055 if (0 == strcmp(".stab", sectab_i->Name)
2056 || 0 == strcmp(".stabstr", sectab_i->Name))
2059 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2060 /* If the relocation field (a short) has overflowed, the
2061 * real count can be found in the first reloc entry.
2063 * See Section 4.1 (last para) of the PE spec (rev6.0).
2065 * Nov2003 update: the GNU linker still doesn't correctly
2066 * handle the generation of relocatable object files with
2067 * overflown relocations. Hence the output to warn of potential
2070 COFF_reloc* rel = (COFF_reloc*)
2071 myindex ( sizeof_COFF_reloc, reltab, 0 );
2072 noRelocs = rel->VirtualAddress;
2073 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
2077 noRelocs = sectab_i->NumberOfRelocations;
2082 for (; j < noRelocs; j++) {
2084 COFF_reloc* reltab_j
2086 myindex ( sizeof_COFF_reloc, reltab, j );
2088 /* the location to patch */
2090 ((UChar*)(oc->image))
2091 + (sectab_i->PointerToRawData
2092 + reltab_j->VirtualAddress
2093 - sectab_i->VirtualAddress )
2095 /* the existing contents of pP */
2097 /* the symbol to connect to */
2098 sym = (COFF_symbol*)
2099 myindex ( sizeof_COFF_symbol,
2100 symtab, reltab_j->SymbolTableIndex );
2103 "reloc sec %2d num %3d: type 0x%-4x "
2104 "vaddr 0x%-8x name `",
2106 (UInt32)reltab_j->Type,
2107 reltab_j->VirtualAddress );
2108 printName ( sym->Name, strtab );
2109 debugBelch("'\n" ));
2111 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
2112 COFF_section* section_sym
2113 = findPEi386SectionCalled ( oc, sym->Name );
2115 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
2118 S = ((UInt32)(oc->image))
2119 + (section_sym->PointerToRawData
2122 copyName ( sym->Name, strtab, symbol, 1000-1 );
2123 (void*)S = lookupLocalSymbol( oc, symbol );
2124 if ((void*)S != NULL) goto foundit;
2125 (void*)S = lookupSymbol( symbol );
2126 if ((void*)S != NULL) goto foundit;
2127 zapTrailingAtSign ( symbol );
2128 (void*)S = lookupLocalSymbol( oc, symbol );
2129 if ((void*)S != NULL) goto foundit;
2130 (void*)S = lookupSymbol( symbol );
2131 if ((void*)S != NULL) goto foundit;
2132 /* Newline first because the interactive linker has printed "linking..." */
2133 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
2137 checkProddableBlock(oc, pP);
2138 switch (reltab_j->Type) {
2139 case MYIMAGE_REL_I386_DIR32:
2142 case MYIMAGE_REL_I386_REL32:
2143 /* Tricky. We have to insert a displacement at
2144 pP which, when added to the PC for the _next_
2145 insn, gives the address of the target (S).
2146 Problem is to know the address of the next insn
2147 when we only know pP. We assume that this
2148 literal field is always the last in the insn,
2149 so that the address of the next insn is pP+4
2150 -- hence the constant 4.
2151 Also I don't know if A should be added, but so
2152 far it has always been zero.
2155 *pP = S - ((UInt32)pP) - 4;
2158 debugBelch("%s: unhandled PEi386 relocation type %d",
2159 oc->fileName, reltab_j->Type);
2166 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
2170 #endif /* defined(OBJFORMAT_PEi386) */
2173 /* --------------------------------------------------------------------------
2175 * ------------------------------------------------------------------------*/
2177 #if defined(OBJFORMAT_ELF)
2182 #if defined(sparc_TARGET_ARCH)
2183 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2184 #elif defined(i386_TARGET_ARCH)
2185 # define ELF_TARGET_386 /* Used inside <elf.h> */
2186 #elif defined(x86_64_TARGET_ARCH)
2187 # define ELF_TARGET_X64_64
2189 #elif defined (ia64_TARGET_ARCH)
2190 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2192 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2193 # define ELF_NEED_GOT /* needs Global Offset Table */
2194 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2197 #if !defined(openbsd_TARGET_OS)
2200 /* openbsd elf has things in different places, with diff names */
2201 #include <elf_abi.h>
2202 #include <machine/reloc.h>
2203 #define R_386_32 RELOC_32
2204 #define R_386_PC32 RELOC_PC32
2208 * Define a set of types which can be used for both ELF32 and ELF64
2212 #define ELFCLASS ELFCLASS64
2213 #define Elf_Addr Elf64_Addr
2214 #define Elf_Word Elf64_Word
2215 #define Elf_Sword Elf64_Sword
2216 #define Elf_Ehdr Elf64_Ehdr
2217 #define Elf_Phdr Elf64_Phdr
2218 #define Elf_Shdr Elf64_Shdr
2219 #define Elf_Sym Elf64_Sym
2220 #define Elf_Rel Elf64_Rel
2221 #define Elf_Rela Elf64_Rela
2222 #define ELF_ST_TYPE ELF64_ST_TYPE
2223 #define ELF_ST_BIND ELF64_ST_BIND
2224 #define ELF_R_TYPE ELF64_R_TYPE
2225 #define ELF_R_SYM ELF64_R_SYM
2227 #define ELFCLASS ELFCLASS32
2228 #define Elf_Addr Elf32_Addr
2229 #define Elf_Word Elf32_Word
2230 #define Elf_Sword Elf32_Sword
2231 #define Elf_Ehdr Elf32_Ehdr
2232 #define Elf_Phdr Elf32_Phdr
2233 #define Elf_Shdr Elf32_Shdr
2234 #define Elf_Sym Elf32_Sym
2235 #define Elf_Rel Elf32_Rel
2236 #define Elf_Rela Elf32_Rela
2238 #define ELF_ST_TYPE ELF32_ST_TYPE
2241 #define ELF_ST_BIND ELF32_ST_BIND
2244 #define ELF_R_TYPE ELF32_R_TYPE
2247 #define ELF_R_SYM ELF32_R_SYM
2253 * Functions to allocate entries in dynamic sections. Currently we simply
2254 * preallocate a large number, and we don't check if a entry for the given
2255 * target already exists (a linear search is too slow). Ideally these
2256 * entries would be associated with symbols.
2259 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2260 #define GOT_SIZE 0x20000
2261 #define FUNCTION_TABLE_SIZE 0x10000
2262 #define PLT_SIZE 0x08000
2265 static Elf_Addr got[GOT_SIZE];
2266 static unsigned int gotIndex;
2267 static Elf_Addr gp_val = (Elf_Addr)got;
2270 allocateGOTEntry(Elf_Addr target)
2274 if (gotIndex >= GOT_SIZE)
2275 barf("Global offset table overflow");
2277 entry = &got[gotIndex++];
2279 return (Elf_Addr)entry;
2283 #ifdef ELF_FUNCTION_DESC
2289 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2290 static unsigned int functionTableIndex;
2293 allocateFunctionDesc(Elf_Addr target)
2295 FunctionDesc *entry;
2297 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2298 barf("Function table overflow");
2300 entry = &functionTable[functionTableIndex++];
2302 entry->gp = (Elf_Addr)gp_val;
2303 return (Elf_Addr)entry;
2307 copyFunctionDesc(Elf_Addr target)
2309 FunctionDesc *olddesc = (FunctionDesc *)target;
2310 FunctionDesc *newdesc;
2312 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2313 newdesc->gp = olddesc->gp;
2314 return (Elf_Addr)newdesc;
2319 #ifdef ia64_TARGET_ARCH
2320 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2321 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2323 static unsigned char plt_code[] =
2325 /* taken from binutils bfd/elfxx-ia64.c */
2326 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2327 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2328 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2329 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2330 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2331 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2334 /* If we can't get to the function descriptor via gp, take a local copy of it */
2335 #define PLT_RELOC(code, target) { \
2336 Elf64_Sxword rel_value = target - gp_val; \
2337 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2338 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2340 ia64_reloc_gprel22((Elf_Addr)code, target); \
2345 unsigned char code[sizeof(plt_code)];
2349 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2351 PLTEntry *plt = (PLTEntry *)oc->plt;
2354 if (oc->pltIndex >= PLT_SIZE)
2355 barf("Procedure table overflow");
2357 entry = &plt[oc->pltIndex++];
2358 memcpy(entry->code, plt_code, sizeof(entry->code));
2359 PLT_RELOC(entry->code, target);
2360 return (Elf_Addr)entry;
2366 return (PLT_SIZE * sizeof(PLTEntry));
2372 * Generic ELF functions
2376 findElfSection ( void* objImage, Elf_Word sh_type )
2378 char* ehdrC = (char*)objImage;
2379 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2380 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2381 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2385 for (i = 0; i < ehdr->e_shnum; i++) {
2386 if (shdr[i].sh_type == sh_type
2387 /* Ignore the section header's string table. */
2388 && i != ehdr->e_shstrndx
2389 /* Ignore string tables named .stabstr, as they contain
2391 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2393 ptr = ehdrC + shdr[i].sh_offset;
2400 #if defined(ia64_TARGET_ARCH)
2402 findElfSegment ( void* objImage, Elf_Addr vaddr )
2404 char* ehdrC = (char*)objImage;
2405 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2406 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2407 Elf_Addr segaddr = 0;
2410 for (i = 0; i < ehdr->e_phnum; i++) {
2411 segaddr = phdr[i].p_vaddr;
2412 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2420 ocVerifyImage_ELF ( ObjectCode* oc )
2424 int i, j, nent, nstrtab, nsymtabs;
2428 char* ehdrC = (char*)(oc->image);
2429 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2431 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2432 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2433 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2434 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2435 errorBelch("%s: not an ELF object", oc->fileName);
2439 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2440 errorBelch("%s: unsupported ELF format", oc->fileName);
2444 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2445 IF_DEBUG(linker,debugBelch( "Is little-endian" ));
2447 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2448 IF_DEBUG(linker,debugBelch( "Is big-endian" ));
2450 errorBelch("%s: unknown endiannness", oc->fileName);
2454 if (ehdr->e_type != ET_REL) {
2455 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
2458 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file" ));
2460 IF_DEBUG(linker,debugBelch( "Architecture is " ));
2461 switch (ehdr->e_machine) {
2462 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
2463 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
2465 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
2467 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
2468 default: IF_DEBUG(linker,debugBelch( "unknown" ));
2469 errorBelch("%s: unknown architecture", oc->fileName);
2473 IF_DEBUG(linker,debugBelch(
2474 "\nSection header table: start %d, n_entries %d, ent_size %d",
2475 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2477 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2479 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2481 if (ehdr->e_shstrndx == SHN_UNDEF) {
2482 errorBelch("%s: no section header string table", oc->fileName);
2485 IF_DEBUG(linker,debugBelch( "Section header string table is section %d",
2487 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2490 for (i = 0; i < ehdr->e_shnum; i++) {
2491 IF_DEBUG(linker,debugBelch("%2d: ", i ));
2492 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
2493 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
2494 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
2495 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
2496 ehdrC + shdr[i].sh_offset,
2497 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2499 if (shdr[i].sh_type == SHT_REL) {
2500 IF_DEBUG(linker,debugBelch("Rel " ));
2501 } else if (shdr[i].sh_type == SHT_RELA) {
2502 IF_DEBUG(linker,debugBelch("RelA " ));
2504 IF_DEBUG(linker,debugBelch(" "));
2507 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
2511 IF_DEBUG(linker,debugBelch( "\nString tables" ));
2514 for (i = 0; i < ehdr->e_shnum; i++) {
2515 if (shdr[i].sh_type == SHT_STRTAB
2516 /* Ignore the section header's string table. */
2517 && i != ehdr->e_shstrndx
2518 /* Ignore string tables named .stabstr, as they contain
2520 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2522 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
2523 strtab = ehdrC + shdr[i].sh_offset;
2528 errorBelch("%s: no string tables, or too many", oc->fileName);
2533 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
2534 for (i = 0; i < ehdr->e_shnum; i++) {
2535 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2536 IF_DEBUG(linker,debugBelch( "section %d is a symbol table", i ));
2538 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2539 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2540 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%d rem)",
2542 shdr[i].sh_size % sizeof(Elf_Sym)
2544 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2545 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
2548 for (j = 0; j < nent; j++) {
2549 IF_DEBUG(linker,debugBelch(" %2d ", j ));
2550 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
2551 (int)stab[j].st_shndx,
2552 (int)stab[j].st_size,
2553 (char*)stab[j].st_value ));
2555 IF_DEBUG(linker,debugBelch("type=" ));
2556 switch (ELF_ST_TYPE(stab[j].st_info)) {
2557 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
2558 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
2559 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
2560 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
2561 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
2562 default: IF_DEBUG(linker,debugBelch("? " )); break;
2564 IF_DEBUG(linker,debugBelch(" " ));
2566 IF_DEBUG(linker,debugBelch("bind=" ));
2567 switch (ELF_ST_BIND(stab[j].st_info)) {
2568 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
2569 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
2570 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
2571 default: IF_DEBUG(linker,debugBelch("? " )); break;
2573 IF_DEBUG(linker,debugBelch(" " ));
2575 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
2579 if (nsymtabs == 0) {
2580 errorBelch("%s: didn't find any symbol tables", oc->fileName);
2589 ocGetNames_ELF ( ObjectCode* oc )
2594 char* ehdrC = (char*)(oc->image);
2595 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2596 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2597 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2599 ASSERT(symhash != NULL);
2602 errorBelch("%s: no strtab", oc->fileName);
2607 for (i = 0; i < ehdr->e_shnum; i++) {
2608 /* Figure out what kind of section it is. Logic derived from
2609 Figure 1.14 ("Special Sections") of the ELF document
2610 ("Portable Formats Specification, Version 1.1"). */
2611 Elf_Shdr hdr = shdr[i];
2612 SectionKind kind = SECTIONKIND_OTHER;
2615 if (hdr.sh_type == SHT_PROGBITS
2616 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2617 /* .text-style section */
2618 kind = SECTIONKIND_CODE_OR_RODATA;
2621 if (hdr.sh_type == SHT_PROGBITS
2622 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2623 /* .data-style section */
2624 kind = SECTIONKIND_RWDATA;
2627 if (hdr.sh_type == SHT_PROGBITS
2628 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2629 /* .rodata-style section */
2630 kind = SECTIONKIND_CODE_OR_RODATA;
2633 if (hdr.sh_type == SHT_NOBITS
2634 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2635 /* .bss-style section */
2636 kind = SECTIONKIND_RWDATA;
2640 if (is_bss && shdr[i].sh_size > 0) {
2641 /* This is a non-empty .bss section. Allocate zeroed space for
2642 it, and set its .sh_offset field such that
2643 ehdrC + .sh_offset == addr_of_zeroed_space. */
2644 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2645 "ocGetNames_ELF(BSS)");
2646 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2648 debugBelch("BSS section at 0x%x, size %d\n",
2649 zspace, shdr[i].sh_size);
2653 /* fill in the section info */
2654 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2655 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2656 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2657 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2660 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2662 /* copy stuff into this module's object symbol table */
2663 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2664 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2666 oc->n_symbols = nent;
2667 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2668 "ocGetNames_ELF(oc->symbols)");
2670 for (j = 0; j < nent; j++) {
2672 char isLocal = FALSE; /* avoids uninit-var warning */
2674 char* nm = strtab + stab[j].st_name;
2675 int secno = stab[j].st_shndx;
2677 /* Figure out if we want to add it; if so, set ad to its
2678 address. Otherwise leave ad == NULL. */
2680 if (secno == SHN_COMMON) {
2682 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2684 debugBelch("COMMON symbol, size %d name %s\n",
2685 stab[j].st_size, nm);
2687 /* Pointless to do addProddableBlock() for this area,
2688 since the linker should never poke around in it. */
2691 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2692 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2694 /* and not an undefined symbol */
2695 && stab[j].st_shndx != SHN_UNDEF
2696 /* and not in a "special section" */
2697 && stab[j].st_shndx < SHN_LORESERVE
2699 /* and it's a not a section or string table or anything silly */
2700 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2701 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2702 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2705 /* Section 0 is the undefined section, hence > and not >=. */
2706 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2708 if (shdr[secno].sh_type == SHT_NOBITS) {
2709 debugBelch(" BSS symbol, size %d off %d name %s\n",
2710 stab[j].st_size, stab[j].st_value, nm);
2713 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2714 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2717 #ifdef ELF_FUNCTION_DESC
2718 /* dlsym() and the initialisation table both give us function
2719 * descriptors, so to be consistent we store function descriptors
2720 * in the symbol table */
2721 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2722 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2724 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s",
2725 ad, oc->fileName, nm ));
2730 /* And the decision is ... */
2734 oc->symbols[j] = nm;
2737 /* Ignore entirely. */
2739 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2743 IF_DEBUG(linker,debugBelch( "skipping `%s'",
2744 strtab + stab[j].st_name ));
2747 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2748 (int)ELF_ST_BIND(stab[j].st_info),
2749 (int)ELF_ST_TYPE(stab[j].st_info),
2750 (int)stab[j].st_shndx,
2751 strtab + stab[j].st_name
2754 oc->symbols[j] = NULL;
2763 /* Do ELF relocations which lack an explicit addend. All x86-linux
2764 relocations appear to be of this form. */
2766 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2767 Elf_Shdr* shdr, int shnum,
2768 Elf_Sym* stab, char* strtab )
2773 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2774 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2775 int target_shndx = shdr[shnum].sh_info;
2776 int symtab_shndx = shdr[shnum].sh_link;
2778 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2779 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2780 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d",
2781 target_shndx, symtab_shndx ));
2783 for (j = 0; j < nent; j++) {
2784 Elf_Addr offset = rtab[j].r_offset;
2785 Elf_Addr info = rtab[j].r_info;
2787 Elf_Addr P = ((Elf_Addr)targ) + offset;
2788 Elf_Word* pP = (Elf_Word*)P;
2794 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
2795 j, (void*)offset, (void*)info ));
2797 IF_DEBUG(linker,debugBelch( " ZERO" ));
2800 Elf_Sym sym = stab[ELF_R_SYM(info)];
2801 /* First see if it is a local symbol. */
2802 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2803 /* Yes, so we can get the address directly from the ELF symbol
2805 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2807 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2808 + stab[ELF_R_SYM(info)].st_value);
2811 /* No, so look up the name in our global table. */
2812 symbol = strtab + sym.st_name;
2813 S_tmp = lookupSymbol( symbol );
2814 S = (Elf_Addr)S_tmp;
2817 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
2820 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
2823 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p",
2824 (void*)P, (void*)S, (void*)A ));
2825 checkProddableBlock ( oc, pP );
2829 switch (ELF_R_TYPE(info)) {
2830 # ifdef i386_TARGET_ARCH
2831 case R_386_32: *pP = value; break;
2832 case R_386_PC32: *pP = value - P; break;
2835 errorBelch("%s: unhandled ELF relocation(Rel) type %d\n",
2836 oc->fileName, ELF_R_TYPE(info));
2844 /* Do ELF relocations for which explicit addends are supplied.
2845 sparc-solaris relocations appear to be of this form. */
2847 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2848 Elf_Shdr* shdr, int shnum,
2849 Elf_Sym* stab, char* strtab )
2854 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2855 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2856 int target_shndx = shdr[shnum].sh_info;
2857 int symtab_shndx = shdr[shnum].sh_link;
2859 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2860 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2861 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d",
2862 target_shndx, symtab_shndx ));
2864 for (j = 0; j < nent; j++) {
2865 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH) || defined(powerpc_TARGET_ARCH)
2866 /* This #ifdef only serves to avoid unused-var warnings. */
2867 Elf_Addr offset = rtab[j].r_offset;
2868 Elf_Addr P = targ + offset;
2870 Elf_Addr info = rtab[j].r_info;
2871 Elf_Addr A = rtab[j].r_addend;
2875 # if defined(sparc_TARGET_ARCH)
2876 Elf_Word* pP = (Elf_Word*)P;
2878 # elif defined(ia64_TARGET_ARCH)
2879 Elf64_Xword *pP = (Elf64_Xword *)P;
2881 # elif defined(powerpc_TARGET_ARCH)
2885 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
2886 j, (void*)offset, (void*)info,
2889 IF_DEBUG(linker,debugBelch( " ZERO" ));
2892 Elf_Sym sym = stab[ELF_R_SYM(info)];
2893 /* First see if it is a local symbol. */
2894 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2895 /* Yes, so we can get the address directly from the ELF symbol
2897 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2899 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2900 + stab[ELF_R_SYM(info)].st_value);
2901 #ifdef ELF_FUNCTION_DESC
2902 /* Make a function descriptor for this function */
2903 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2904 S = allocateFunctionDesc(S + A);
2909 /* No, so look up the name in our global table. */
2910 symbol = strtab + sym.st_name;
2911 S_tmp = lookupSymbol( symbol );
2912 S = (Elf_Addr)S_tmp;
2914 #ifdef ELF_FUNCTION_DESC
2915 /* If a function, already a function descriptor - we would
2916 have to copy it to add an offset. */
2917 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
2918 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
2922 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
2925 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
2928 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
2929 (void*)P, (void*)S, (void*)A ));
2930 /* checkProddableBlock ( oc, (void*)P ); */
2934 switch (ELF_R_TYPE(info)) {
2935 # if defined(sparc_TARGET_ARCH)
2936 case R_SPARC_WDISP30:
2937 w1 = *pP & 0xC0000000;
2938 w2 = (Elf_Word)((value - P) >> 2);
2939 ASSERT((w2 & 0xC0000000) == 0);
2944 w1 = *pP & 0xFFC00000;
2945 w2 = (Elf_Word)(value >> 10);
2946 ASSERT((w2 & 0xFFC00000) == 0);
2952 w2 = (Elf_Word)(value & 0x3FF);
2953 ASSERT((w2 & ~0x3FF) == 0);
2957 /* According to the Sun documentation:
2959 This relocation type resembles R_SPARC_32, except it refers to an
2960 unaligned word. That is, the word to be relocated must be treated
2961 as four separate bytes with arbitrary alignment, not as a word
2962 aligned according to the architecture requirements.
2964 (JRS: which means that freeloading on the R_SPARC_32 case
2965 is probably wrong, but hey ...)
2969 w2 = (Elf_Word)value;
2972 # elif defined(ia64_TARGET_ARCH)
2973 case R_IA64_DIR64LSB:
2974 case R_IA64_FPTR64LSB:
2977 case R_IA64_PCREL64LSB:
2980 case R_IA64_SEGREL64LSB:
2981 addr = findElfSegment(ehdrC, value);
2984 case R_IA64_GPREL22:
2985 ia64_reloc_gprel22(P, value);
2987 case R_IA64_LTOFF22:
2988 case R_IA64_LTOFF22X:
2989 case R_IA64_LTOFF_FPTR22:
2990 addr = allocateGOTEntry(value);
2991 ia64_reloc_gprel22(P, addr);
2993 case R_IA64_PCREL21B:
2994 ia64_reloc_pcrel21(P, S, oc);
2997 /* This goes with R_IA64_LTOFF22X and points to the load to
2998 * convert into a move. We don't implement relaxation. */
3000 # elif defined(powerpc_TARGET_ARCH)
3001 case R_PPC_ADDR16_LO:
3002 *(Elf32_Half*) P = value;
3005 case R_PPC_ADDR16_HI:
3006 *(Elf32_Half*) P = value >> 16;
3009 case R_PPC_ADDR16_HA:
3010 *(Elf32_Half*) P = (value + 0x8000) >> 16;
3014 *(Elf32_Word *) P = value;
3018 *(Elf32_Word *) P = value - P;
3024 if( delta << 6 >> 6 != delta )
3026 value = makeJumpIsland( oc, ELF_R_SYM(info), value );
3029 if( value == 0 || delta << 6 >> 6 != delta )
3031 barf( "Unable to make ppcJumpIsland for #%d",
3037 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
3038 | (delta & 0x3fffffc);
3042 errorBelch("%s: unhandled ELF relocation(RelA) type %d\n",
3043 oc->fileName, ELF_R_TYPE(info));
3052 ocResolve_ELF ( ObjectCode* oc )
3056 Elf_Sym* stab = NULL;
3057 char* ehdrC = (char*)(oc->image);
3058 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
3059 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3060 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
3062 /* first find "the" symbol table */
3063 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
3065 /* also go find the string table */
3066 strtab = findElfSection ( ehdrC, SHT_STRTAB );
3068 if (stab == NULL || strtab == NULL) {
3069 errorBelch("%s: can't find string or symbol table", oc->fileName);
3073 /* Process the relocation sections. */
3074 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
3076 /* Skip sections called ".rel.stab". These appear to contain
3077 relocation entries that, when done, make the stabs debugging
3078 info point at the right places. We ain't interested in all
3080 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
3083 if (shdr[shnum].sh_type == SHT_REL ) {
3084 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
3085 shnum, stab, strtab );
3089 if (shdr[shnum].sh_type == SHT_RELA) {
3090 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
3091 shnum, stab, strtab );
3096 /* Free the local symbol table; we won't need it again. */
3097 freeHashTable(oc->lochash, NULL);
3100 #if defined(powerpc_TARGET_ARCH)
3101 ocFlushInstructionCache( oc );
3109 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
3110 * at the front. The following utility functions pack and unpack instructions, and
3111 * take care of the most common relocations.
3114 #ifdef ia64_TARGET_ARCH
3117 ia64_extract_instruction(Elf64_Xword *target)
3120 int slot = (Elf_Addr)target & 3;
3121 (Elf_Addr)target &= ~3;
3129 return ((w1 >> 5) & 0x1ffffffffff);
3131 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
3135 barf("ia64_extract_instruction: invalid slot %p", target);
3140 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
3142 int slot = (Elf_Addr)target & 3;
3143 (Elf_Addr)target &= ~3;
3148 *target |= value << 5;
3151 *target |= value << 46;
3152 *(target+1) |= value >> 18;
3155 *(target+1) |= value << 23;
3161 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
3163 Elf64_Xword instruction;
3164 Elf64_Sxword rel_value;
3166 rel_value = value - gp_val;
3167 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
3168 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
3170 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3171 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
3172 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
3173 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
3174 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3175 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3179 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
3181 Elf64_Xword instruction;
3182 Elf64_Sxword rel_value;
3185 entry = allocatePLTEntry(value, oc);
3187 rel_value = (entry >> 4) - (target >> 4);
3188 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
3189 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
3191 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3192 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
3193 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3194 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3200 * PowerPC ELF specifics
3203 #ifdef powerpc_TARGET_ARCH
3205 static int ocAllocateJumpIslands_ELF( ObjectCode *oc )
3211 ehdr = (Elf_Ehdr *) oc->image;
3212 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
3214 for( i = 0; i < ehdr->e_shnum; i++ )
3215 if( shdr[i].sh_type == SHT_SYMTAB )
3218 if( i == ehdr->e_shnum )
3220 errorBelch( "This ELF file contains no symtab" );
3224 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
3226 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
3227 shdr[i].sh_entsize, sizeof( Elf_Sym ) );
3232 return ocAllocateJumpIslands( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
3235 #endif /* powerpc */
3239 /* --------------------------------------------------------------------------
3241 * ------------------------------------------------------------------------*/
3243 #if defined(OBJFORMAT_MACHO)
3246 Support for MachO linking on Darwin/MacOS X on PowerPC chips
3247 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3249 I hereby formally apologize for the hackish nature of this code.
3250 Things that need to be done:
3251 *) implement ocVerifyImage_MachO
3252 *) add still more sanity checks.
3255 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3257 struct mach_header *header = (struct mach_header *) oc->image;
3258 struct load_command *lc = (struct load_command *) (header + 1);
3261 for( i = 0; i < header->ncmds; i++ )
3263 if( lc->cmd == LC_DYSYMTAB )
3265 struct dysymtab_command *dsymLC = (struct dysymtab_command *) lc;
3267 if( !ocAllocateJumpIslands( oc, dsymLC->nundefsym,
3268 dsymLC->iundefsym ) )
3271 break; // there can be only one LC_DSYMTAB
3273 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3278 static int ocVerifyImage_MachO(ObjectCode* oc)
3280 // FIXME: do some verifying here
3284 static int resolveImports(
3287 struct symtab_command *symLC,
3288 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3289 unsigned long *indirectSyms,
3290 struct nlist *nlist)
3294 for(i=0;i*4<sect->size;i++)
3296 // according to otool, reserved1 contains the first index into the indirect symbol table
3297 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3298 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3301 if((symbol->n_type & N_TYPE) == N_UNDF
3302 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3303 addr = (void*) (symbol->n_value);
3304 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3307 addr = lookupSymbol(nm);
3310 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3314 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3315 ((void**)(image + sect->offset))[i] = addr;
3321 static char* relocateAddress(
3324 struct section* sections,
3325 unsigned long address)
3328 for(i = 0; i < nSections; i++)
3330 if(sections[i].addr <= address
3331 && address < sections[i].addr + sections[i].size)
3333 return oc->image + sections[i].offset + address - sections[i].addr;
3336 barf("Invalid Mach-O file:"
3337 "Address out of bounds while relocating object file");
3341 static int relocateSection(
3344 struct symtab_command *symLC, struct nlist *nlist,
3345 int nSections, struct section* sections, struct section *sect)
3347 struct relocation_info *relocs;
3350 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3352 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3356 relocs = (struct relocation_info*) (image + sect->reloff);
3360 if(relocs[i].r_address & R_SCATTERED)
3362 struct scattered_relocation_info *scat =
3363 (struct scattered_relocation_info*) &relocs[i];
3367 if(scat->r_length == 2)
3369 unsigned long word = 0;
3370 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3371 checkProddableBlock(oc,wordPtr);
3373 // Step 1: Figure out what the relocated value should be
3374 if(scat->r_type == GENERIC_RELOC_VANILLA)
3376 word = *wordPtr + (unsigned long) relocateAddress(
3383 else if(scat->r_type == PPC_RELOC_SECTDIFF
3384 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3385 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3386 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3388 struct scattered_relocation_info *pair =
3389 (struct scattered_relocation_info*) &relocs[i+1];
3391 if(!pair->r_scattered || pair->r_type != PPC_RELOC_PAIR)
3392 barf("Invalid Mach-O file: "
3393 "PPC_RELOC_*_SECTDIFF not followed by PPC_RELOC_PAIR");
3395 word = (unsigned long)
3396 (relocateAddress(oc, nSections, sections, scat->r_value)
3397 - relocateAddress(oc, nSections, sections, pair->r_value));
3400 else if(scat->r_type == PPC_RELOC_HI16
3401 || scat->r_type == PPC_RELOC_LO16
3402 || scat->r_type == PPC_RELOC_HA16
3403 || scat->r_type == PPC_RELOC_LO14)
3404 { // these are generated by label+offset things
3405 struct relocation_info *pair = &relocs[i+1];
3406 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
3407 barf("Invalid Mach-O file: "
3408 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
3410 if(scat->r_type == PPC_RELOC_LO16)
3412 word = ((unsigned short*) wordPtr)[1];
3413 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3415 else if(scat->r_type == PPC_RELOC_LO14)
3417 barf("Unsupported Relocation: PPC_RELOC_LO14");
3418 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
3419 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3421 else if(scat->r_type == PPC_RELOC_HI16)
3423 word = ((unsigned short*) wordPtr)[1] << 16;
3424 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3426 else if(scat->r_type == PPC_RELOC_HA16)
3428 word = ((unsigned short*) wordPtr)[1] << 16;
3429 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3433 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
3439 continue; // ignore the others
3441 if(scat->r_type == GENERIC_RELOC_VANILLA
3442 || scat->r_type == PPC_RELOC_SECTDIFF)
3446 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
3448 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3450 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
3452 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3454 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
3456 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3457 + ((word & (1<<15)) ? 1 : 0);
3462 continue; // FIXME: I hope it's OK to ignore all the others.
3466 struct relocation_info *reloc = &relocs[i];
3467 if(reloc->r_pcrel && !reloc->r_extern)
3470 if(reloc->r_length == 2)
3472 unsigned long word = 0;
3473 unsigned long jumpIsland = 0;
3474 long offsetToJumpIsland;
3476 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3477 checkProddableBlock(oc,wordPtr);
3479 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3483 else if(reloc->r_type == PPC_RELOC_LO16)
3485 word = ((unsigned short*) wordPtr)[1];
3486 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3488 else if(reloc->r_type == PPC_RELOC_HI16)
3490 word = ((unsigned short*) wordPtr)[1] << 16;
3491 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3493 else if(reloc->r_type == PPC_RELOC_HA16)
3495 word = ((unsigned short*) wordPtr)[1] << 16;
3496 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3498 else if(reloc->r_type == PPC_RELOC_BR24)
3501 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
3505 if(!reloc->r_extern)
3508 sections[reloc->r_symbolnum-1].offset
3509 - sections[reloc->r_symbolnum-1].addr
3516 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3517 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3518 unsigned long symbolAddress = (unsigned long) (lookupSymbol(nm));
3521 errorBelch("\nunknown symbol `%s'", nm);
3527 // In the .o file, this should be a relative jump to NULL
3528 // and we'll change it to a jump to a relative jump to the symbol
3529 ASSERT(-word == reloc->r_address);
3530 word = symbolAddress;
3531 jumpIsland = makeJumpIsland(oc,reloc->r_symbolnum,word);
3532 word -= ((long)image) + sect->offset + reloc->r_address;
3535 offsetToJumpIsland = jumpIsland
3536 - (((long)image) + sect->offset + reloc->r_address);
3541 word += symbolAddress;
3545 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3550 else if(reloc->r_type == PPC_RELOC_LO16)
3552 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3555 else if(reloc->r_type == PPC_RELOC_HI16)
3557 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3560 else if(reloc->r_type == PPC_RELOC_HA16)
3562 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3563 + ((word & (1<<15)) ? 1 : 0);
3566 else if(reloc->r_type == PPC_RELOC_BR24)
3568 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3570 // The branch offset is too large.
3571 // Therefore, we try to use a jump island.
3573 barf("unconditional relative branch out of range: "
3574 "no jump island available");
3576 word = offsetToJumpIsland;
3577 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3578 barf("unconditional relative branch out of range: "
3579 "jump island out of range");
3581 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3585 barf("\nunknown relocation %d",reloc->r_type);
3592 static int ocGetNames_MachO(ObjectCode* oc)
3594 char *image = (char*) oc->image;
3595 struct mach_header *header = (struct mach_header*) image;
3596 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3597 unsigned i,curSymbol;
3598 struct segment_command *segLC = NULL;
3599 struct section *sections;
3600 struct symtab_command *symLC = NULL;
3601 struct dysymtab_command *dsymLC = NULL;
3602 struct nlist *nlist;
3603 unsigned long commonSize = 0;
3604 char *commonStorage = NULL;
3605 unsigned long commonCounter;
3607 for(i=0;i<header->ncmds;i++)
3609 if(lc->cmd == LC_SEGMENT)
3610 segLC = (struct segment_command*) lc;
3611 else if(lc->cmd == LC_SYMTAB)
3612 symLC = (struct symtab_command*) lc;
3613 else if(lc->cmd == LC_DYSYMTAB)
3614 dsymLC = (struct dysymtab_command*) lc;
3615 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3618 sections = (struct section*) (segLC+1);
3619 nlist = (struct nlist*) (image + symLC->symoff);
3621 for(i=0;i<segLC->nsects;i++)
3623 if(sections[i].size == 0)
3626 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
3628 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
3629 "ocGetNames_MachO(common symbols)");
3630 sections[i].offset = zeroFillArea - image;
3633 if(!strcmp(sections[i].sectname,"__text"))
3634 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3635 (void*) (image + sections[i].offset),
3636 (void*) (image + sections[i].offset + sections[i].size));
3637 else if(!strcmp(sections[i].sectname,"__const"))
3638 addSection(oc, SECTIONKIND_RWDATA,
3639 (void*) (image + sections[i].offset),
3640 (void*) (image + sections[i].offset + sections[i].size));
3641 else if(!strcmp(sections[i].sectname,"__data"))
3642 addSection(oc, SECTIONKIND_RWDATA,
3643 (void*) (image + sections[i].offset),
3644 (void*) (image + sections[i].offset + sections[i].size));
3645 else if(!strcmp(sections[i].sectname,"__bss")
3646 || !strcmp(sections[i].sectname,"__common"))
3647 addSection(oc, SECTIONKIND_RWDATA,
3648 (void*) (image + sections[i].offset),
3649 (void*) (image + sections[i].offset + sections[i].size));
3651 addProddableBlock(oc, (void*) (image + sections[i].offset),
3655 // count external symbols defined here
3657 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3659 if((nlist[i].n_type & N_TYPE) == N_SECT)
3662 for(i=0;i<symLC->nsyms;i++)
3664 if((nlist[i].n_type & N_TYPE) == N_UNDF
3665 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3667 commonSize += nlist[i].n_value;
3671 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3672 "ocGetNames_MachO(oc->symbols)");
3674 // insert symbols into hash table
3675 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3677 if((nlist[i].n_type & N_TYPE) == N_SECT)
3679 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3680 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3681 sections[nlist[i].n_sect-1].offset
3682 - sections[nlist[i].n_sect-1].addr
3683 + nlist[i].n_value);
3684 oc->symbols[curSymbol++] = nm;
3688 // insert local symbols into lochash
3689 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3691 if((nlist[i].n_type & N_TYPE) == N_SECT)
3693 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3694 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3695 sections[nlist[i].n_sect-1].offset
3696 - sections[nlist[i].n_sect-1].addr
3697 + nlist[i].n_value);
3702 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3703 commonCounter = (unsigned long)commonStorage;
3704 for(i=0;i<symLC->nsyms;i++)
3706 if((nlist[i].n_type & N_TYPE) == N_UNDF
3707 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3709 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3710 unsigned long sz = nlist[i].n_value;
3712 nlist[i].n_value = commonCounter;
3714 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3715 oc->symbols[curSymbol++] = nm;
3717 commonCounter += sz;
3723 static int ocResolve_MachO(ObjectCode* oc)
3725 char *image = (char*) oc->image;
3726 struct mach_header *header = (struct mach_header*) image;
3727 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3729 struct segment_command *segLC = NULL;
3730 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3731 struct symtab_command *symLC = NULL;
3732 struct dysymtab_command *dsymLC = NULL;
3733 struct nlist *nlist;
3734 unsigned long *indirectSyms;
3736 for(i=0;i<header->ncmds;i++)
3738 if(lc->cmd == LC_SEGMENT)
3739 segLC = (struct segment_command*) lc;
3740 else if(lc->cmd == LC_SYMTAB)
3741 symLC = (struct symtab_command*) lc;
3742 else if(lc->cmd == LC_DYSYMTAB)
3743 dsymLC = (struct dysymtab_command*) lc;
3744 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3747 sections = (struct section*) (segLC+1);
3748 nlist = (struct nlist*) (image + symLC->symoff);
3750 for(i=0;i<segLC->nsects;i++)
3752 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3753 la_ptrs = §ions[i];
3754 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3755 nl_ptrs = §ions[i];
3758 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3761 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
3764 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
3767 for(i=0;i<segLC->nsects;i++)
3769 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
3773 /* Free the local symbol table; we won't need it again. */
3774 freeHashTable(oc->lochash, NULL);
3777 #if defined (powerpc_TARGET_ARCH)
3778 ocFlushInstructionCache( oc );
3785 * The Mach-O object format uses leading underscores. But not everywhere.
3786 * There is a small number of runtime support functions defined in
3787 * libcc_dynamic.a whose name does not have a leading underscore.
3788 * As a consequence, we can't get their address from C code.
3789 * We have to use inline assembler just to take the address of a function.
3793 static void machoInitSymbolsWithoutUnderscore()
3799 __asm__ ("lis %0,hi16(" #x ")\n\tori %0,%0,lo16(" #x ")" : "=r" (p)); \
3800 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, p);
3802 RTS_MACHO_NOUNDERLINE_SYMBOLS