1 /* -----------------------------------------------------------------------------
3 * (c) The GHC Team, 2000-2004
7 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
13 /* Linux needs _GNU_SOURCE to get RTLD_DEFAULT from <dlfcn.h> and
14 MREMAP_MAYMOVE from <sys/mman.h>.
25 #include "LinkerInternals.h"
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
37 #ifdef HAVE_SYS_STAT_H
41 #if defined(HAVE_DLFCN_H)
45 #if defined(cygwin32_HOST_OS)
50 #ifdef HAVE_SYS_TIME_H
54 #include <sys/fcntl.h>
55 #include <sys/termios.h>
56 #include <sys/utime.h>
57 #include <sys/utsname.h>
61 #if defined(ia64_HOST_ARCH) || defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS)
66 #if defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS)
74 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) || defined(freebsd_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS)
75 # define OBJFORMAT_ELF
76 #elif defined(cygwin32_HOST_OS) || defined (mingw32_HOST_OS)
77 # define OBJFORMAT_PEi386
80 #elif defined(darwin_HOST_OS)
81 # define OBJFORMAT_MACHO
82 # include <mach-o/loader.h>
83 # include <mach-o/nlist.h>
84 # include <mach-o/reloc.h>
85 # include <mach-o/dyld.h>
86 #if defined(powerpc_HOST_ARCH)
87 # include <mach-o/ppc/reloc.h>
91 /* Hash table mapping symbol names to Symbol */
92 static /*Str*/HashTable *symhash;
94 /* List of currently loaded objects */
95 ObjectCode *objects = NULL; /* initially empty */
97 #if defined(OBJFORMAT_ELF)
98 static int ocVerifyImage_ELF ( ObjectCode* oc );
99 static int ocGetNames_ELF ( ObjectCode* oc );
100 static int ocResolve_ELF ( ObjectCode* oc );
101 #if defined(powerpc_HOST_ARCH)
102 static int ocAllocateJumpIslands_ELF ( ObjectCode* oc );
104 #elif defined(OBJFORMAT_PEi386)
105 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
106 static int ocGetNames_PEi386 ( ObjectCode* oc );
107 static int ocResolve_PEi386 ( ObjectCode* oc );
108 #elif defined(OBJFORMAT_MACHO)
109 static int ocVerifyImage_MachO ( ObjectCode* oc );
110 static int ocGetNames_MachO ( ObjectCode* oc );
111 static int ocResolve_MachO ( ObjectCode* oc );
113 static int machoGetMisalignment( FILE * );
114 #ifdef powerpc_HOST_ARCH
115 static int ocAllocateJumpIslands_MachO ( ObjectCode* oc );
116 static void machoInitSymbolsWithoutUnderscore( void );
120 #if defined(x86_64_HOST_ARCH)
121 static void*x86_64_high_symbol( char *lbl, void *addr );
124 /* -----------------------------------------------------------------------------
125 * Built-in symbols from the RTS
128 typedef struct _RtsSymbolVal {
135 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
136 SymX(makeStableNamezh_fast) \
137 SymX(finalizzeWeakzh_fast)
139 /* These are not available in GUM!!! -- HWL */
140 #define Maybe_Stable_Names
143 #if !defined (mingw32_HOST_OS)
144 #define RTS_POSIX_ONLY_SYMBOLS \
145 SymX(signal_handlers) \
146 SymX(stg_sig_install) \
150 #if defined (cygwin32_HOST_OS)
151 #define RTS_MINGW_ONLY_SYMBOLS /**/
152 /* Don't have the ability to read import libs / archives, so
153 * we have to stupidly list a lot of what libcygwin.a
156 #define RTS_CYGWIN_ONLY_SYMBOLS \
234 #elif !defined(mingw32_HOST_OS)
235 #define RTS_MINGW_ONLY_SYMBOLS /**/
236 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
237 #else /* defined(mingw32_HOST_OS) */
238 #define RTS_POSIX_ONLY_SYMBOLS /**/
239 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
241 /* Extra syms gen'ed by mingw-2's gcc-3.2: */
243 #define RTS_MINGW_EXTRA_SYMS \
244 Sym(_imp____mb_cur_max) \
247 #define RTS_MINGW_EXTRA_SYMS
250 /* These are statically linked from the mingw libraries into the ghc
251 executable, so we have to employ this hack. */
252 #define RTS_MINGW_ONLY_SYMBOLS \
253 SymX(asyncReadzh_fast) \
254 SymX(asyncWritezh_fast) \
255 SymX(asyncDoProczh_fast) \
267 SymX(getservbyname) \
268 SymX(getservbyport) \
269 SymX(getprotobynumber) \
270 SymX(getprotobyname) \
271 SymX(gethostbyname) \
272 SymX(gethostbyaddr) \
306 SymX(rts_InstallConsoleEvent) \
307 SymX(rts_ConsoleHandlerDone) \
309 Sym(_imp___timezone) \
318 RTS_MINGW_EXTRA_SYMS \
322 #if defined(darwin_TARGET_OS) && HAVE_PRINTF_LDBLSTUB
323 #define RTS_DARWIN_ONLY_SYMBOLS \
324 Sym(asprintf$LDBLStub) \
328 Sym(fprintf$LDBLStub) \
329 Sym(fscanf$LDBLStub) \
330 Sym(fwprintf$LDBLStub) \
331 Sym(fwscanf$LDBLStub) \
332 Sym(printf$LDBLStub) \
333 Sym(scanf$LDBLStub) \
334 Sym(snprintf$LDBLStub) \
335 Sym(sprintf$LDBLStub) \
336 Sym(sscanf$LDBLStub) \
337 Sym(strtold$LDBLStub) \
338 Sym(swprintf$LDBLStub) \
339 Sym(swscanf$LDBLStub) \
340 Sym(syslog$LDBLStub) \
341 Sym(vasprintf$LDBLStub) \
343 Sym(verrc$LDBLStub) \
344 Sym(verrx$LDBLStub) \
345 Sym(vfprintf$LDBLStub) \
346 Sym(vfscanf$LDBLStub) \
347 Sym(vfwprintf$LDBLStub) \
348 Sym(vfwscanf$LDBLStub) \
349 Sym(vprintf$LDBLStub) \
350 Sym(vscanf$LDBLStub) \
351 Sym(vsnprintf$LDBLStub) \
352 Sym(vsprintf$LDBLStub) \
353 Sym(vsscanf$LDBLStub) \
354 Sym(vswprintf$LDBLStub) \
355 Sym(vswscanf$LDBLStub) \
356 Sym(vsyslog$LDBLStub) \
357 Sym(vwarn$LDBLStub) \
358 Sym(vwarnc$LDBLStub) \
359 Sym(vwarnx$LDBLStub) \
360 Sym(vwprintf$LDBLStub) \
361 Sym(vwscanf$LDBLStub) \
363 Sym(warnc$LDBLStub) \
364 Sym(warnx$LDBLStub) \
365 Sym(wcstold$LDBLStub) \
366 Sym(wprintf$LDBLStub) \
369 #define RTS_DARWIN_ONLY_SYMBOLS
373 # define MAIN_CAP_SYM SymX(MainCapability)
375 # define MAIN_CAP_SYM
378 #if !defined(mingw32_HOST_OS)
379 #define RTS_USER_SIGNALS_SYMBOLS \
380 SymX(setIOManagerPipe)
382 #define RTS_USER_SIGNALS_SYMBOLS /* nothing */
385 #ifdef TABLES_NEXT_TO_CODE
386 #define RTS_RET_SYMBOLS /* nothing */
388 #define RTS_RET_SYMBOLS \
389 SymX(stg_enter_ret) \
390 SymX(stg_gc_fun_ret) \
398 SymX(stg_ap_pv_ret) \
399 SymX(stg_ap_pp_ret) \
400 SymX(stg_ap_ppv_ret) \
401 SymX(stg_ap_ppp_ret) \
402 SymX(stg_ap_pppv_ret) \
403 SymX(stg_ap_pppp_ret) \
404 SymX(stg_ap_ppppp_ret) \
405 SymX(stg_ap_pppppp_ret)
408 #define RTS_SYMBOLS \
411 SymX(stg_enter_info) \
412 SymX(stg_gc_void_info) \
413 SymX(__stg_gc_enter_1) \
414 SymX(stg_gc_noregs) \
415 SymX(stg_gc_unpt_r1_info) \
416 SymX(stg_gc_unpt_r1) \
417 SymX(stg_gc_unbx_r1_info) \
418 SymX(stg_gc_unbx_r1) \
419 SymX(stg_gc_f1_info) \
421 SymX(stg_gc_d1_info) \
423 SymX(stg_gc_l1_info) \
426 SymX(stg_gc_fun_info) \
428 SymX(stg_gc_gen_info) \
429 SymX(stg_gc_gen_hp) \
431 SymX(stg_gen_yield) \
432 SymX(stg_yield_noregs) \
433 SymX(stg_yield_to_interpreter) \
434 SymX(stg_gen_block) \
435 SymX(stg_block_noregs) \
437 SymX(stg_block_takemvar) \
438 SymX(stg_block_putmvar) \
439 SymX(stg_seq_frame_info) \
441 SymX(MallocFailHook) \
443 SymX(OutOfHeapHook) \
444 SymX(StackOverflowHook) \
445 SymX(__encodeDouble) \
446 SymX(__encodeFloat) \
450 SymX(__gmpz_cmp_si) \
451 SymX(__gmpz_cmp_ui) \
452 SymX(__gmpz_get_si) \
453 SymX(__gmpz_get_ui) \
454 SymX(__int_encodeDouble) \
455 SymX(__int_encodeFloat) \
456 SymX(andIntegerzh_fast) \
457 SymX(atomicallyzh_fast) \
461 SymX(blockAsyncExceptionszh_fast) \
463 SymX(catchRetryzh_fast) \
464 SymX(catchSTMzh_fast) \
465 SymX(closure_flags) \
467 SymX(cmpIntegerzh_fast) \
468 SymX(cmpIntegerIntzh_fast) \
469 SymX(complementIntegerzh_fast) \
470 SymX(createAdjustor) \
471 SymX(decodeDoublezh_fast) \
472 SymX(decodeFloatzh_fast) \
475 SymX(deRefWeakzh_fast) \
476 SymX(deRefStablePtrzh_fast) \
477 SymX(dirty_MUT_VAR) \
478 SymX(divExactIntegerzh_fast) \
479 SymX(divModIntegerzh_fast) \
482 SymX(forkOS_createThread) \
483 SymX(freeHaskellFunctionPtr) \
484 SymX(freeStablePtr) \
485 SymX(gcdIntegerzh_fast) \
486 SymX(gcdIntegerIntzh_fast) \
487 SymX(gcdIntzh_fast) \
496 SymX(hs_perform_gc) \
497 SymX(hs_free_stable_ptr) \
498 SymX(hs_free_fun_ptr) \
500 SymX(int2Integerzh_fast) \
501 SymX(integer2Intzh_fast) \
502 SymX(integer2Wordzh_fast) \
503 SymX(isCurrentThreadBoundzh_fast) \
504 SymX(isDoubleDenormalized) \
505 SymX(isDoubleInfinite) \
507 SymX(isDoubleNegativeZero) \
508 SymX(isEmptyMVarzh_fast) \
509 SymX(isFloatDenormalized) \
510 SymX(isFloatInfinite) \
512 SymX(isFloatNegativeZero) \
513 SymX(killThreadzh_fast) \
516 SymX(makeStablePtrzh_fast) \
517 SymX(minusIntegerzh_fast) \
518 SymX(mkApUpd0zh_fast) \
519 SymX(myThreadIdzh_fast) \
520 SymX(labelThreadzh_fast) \
521 SymX(newArrayzh_fast) \
522 SymX(newBCOzh_fast) \
523 SymX(newByteArrayzh_fast) \
524 SymX_redirect(newCAF, newDynCAF) \
525 SymX(newMVarzh_fast) \
526 SymX(newMutVarzh_fast) \
527 SymX(newTVarzh_fast) \
528 SymX(atomicModifyMutVarzh_fast) \
529 SymX(newPinnedByteArrayzh_fast) \
530 SymX(orIntegerzh_fast) \
532 SymX(performMajorGC) \
533 SymX(plusIntegerzh_fast) \
536 SymX(putMVarzh_fast) \
537 SymX(quotIntegerzh_fast) \
538 SymX(quotRemIntegerzh_fast) \
540 SymX(raiseIOzh_fast) \
541 SymX(readTVarzh_fast) \
542 SymX(remIntegerzh_fast) \
543 SymX(resetNonBlockingFd) \
548 SymX(rts_checkSchedStatus) \
551 SymX(rts_evalLazyIO) \
552 SymX(rts_evalStableIO) \
556 SymX(rts_getDouble) \
561 SymX(rts_getFunPtr) \
562 SymX(rts_getStablePtr) \
563 SymX(rts_getThreadId) \
565 SymX(rts_getWord32) \
578 SymX(rts_mkStablePtr) \
586 SymX(rtsSupportsBoundThreads) \
587 SymX(__hscore_get_saved_termios) \
588 SymX(__hscore_set_saved_termios) \
590 SymX(startupHaskell) \
591 SymX(shutdownHaskell) \
592 SymX(shutdownHaskellAndExit) \
593 SymX(stable_ptr_table) \
594 SymX(stackOverflow) \
595 SymX(stg_CAF_BLACKHOLE_info) \
596 SymX(awakenBlockedQueue) \
597 SymX(stg_CHARLIKE_closure) \
598 SymX(stg_EMPTY_MVAR_info) \
599 SymX(stg_IND_STATIC_info) \
600 SymX(stg_INTLIKE_closure) \
601 SymX(stg_MUT_ARR_PTRS_DIRTY_info) \
602 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
603 SymX(stg_MUT_ARR_PTRS_FROZEN0_info) \
604 SymX(stg_WEAK_info) \
605 SymX(stg_ap_0_info) \
606 SymX(stg_ap_v_info) \
607 SymX(stg_ap_f_info) \
608 SymX(stg_ap_d_info) \
609 SymX(stg_ap_l_info) \
610 SymX(stg_ap_n_info) \
611 SymX(stg_ap_p_info) \
612 SymX(stg_ap_pv_info) \
613 SymX(stg_ap_pp_info) \
614 SymX(stg_ap_ppv_info) \
615 SymX(stg_ap_ppp_info) \
616 SymX(stg_ap_pppv_info) \
617 SymX(stg_ap_pppp_info) \
618 SymX(stg_ap_ppppp_info) \
619 SymX(stg_ap_pppppp_info) \
620 SymX(stg_ap_1_upd_info) \
621 SymX(stg_ap_2_upd_info) \
622 SymX(stg_ap_3_upd_info) \
623 SymX(stg_ap_4_upd_info) \
624 SymX(stg_ap_5_upd_info) \
625 SymX(stg_ap_6_upd_info) \
626 SymX(stg_ap_7_upd_info) \
628 SymX(stg_sel_0_upd_info) \
629 SymX(stg_sel_10_upd_info) \
630 SymX(stg_sel_11_upd_info) \
631 SymX(stg_sel_12_upd_info) \
632 SymX(stg_sel_13_upd_info) \
633 SymX(stg_sel_14_upd_info) \
634 SymX(stg_sel_15_upd_info) \
635 SymX(stg_sel_1_upd_info) \
636 SymX(stg_sel_2_upd_info) \
637 SymX(stg_sel_3_upd_info) \
638 SymX(stg_sel_4_upd_info) \
639 SymX(stg_sel_5_upd_info) \
640 SymX(stg_sel_6_upd_info) \
641 SymX(stg_sel_7_upd_info) \
642 SymX(stg_sel_8_upd_info) \
643 SymX(stg_sel_9_upd_info) \
644 SymX(stg_upd_frame_info) \
645 SymX(suspendThread) \
646 SymX(takeMVarzh_fast) \
647 SymX(timesIntegerzh_fast) \
648 SymX(tryPutMVarzh_fast) \
649 SymX(tryTakeMVarzh_fast) \
650 SymX(unblockAsyncExceptionszh_fast) \
652 SymX(unsafeThawArrayzh_fast) \
653 SymX(waitReadzh_fast) \
654 SymX(waitWritezh_fast) \
655 SymX(word2Integerzh_fast) \
656 SymX(writeTVarzh_fast) \
657 SymX(xorIntegerzh_fast) \
659 SymX(stg_interp_constr_entry) \
660 SymX(stg_interp_constr1_entry) \
661 SymX(stg_interp_constr2_entry) \
662 SymX(stg_interp_constr3_entry) \
663 SymX(stg_interp_constr4_entry) \
664 SymX(stg_interp_constr5_entry) \
665 SymX(stg_interp_constr6_entry) \
666 SymX(stg_interp_constr7_entry) \
667 SymX(stg_interp_constr8_entry) \
668 SymX(stgMallocBytesRWX) \
669 SymX(getAllocations) \
672 RTS_USER_SIGNALS_SYMBOLS
674 #ifdef SUPPORT_LONG_LONGS
675 #define RTS_LONG_LONG_SYMS \
676 SymX(int64ToIntegerzh_fast) \
677 SymX(word64ToIntegerzh_fast)
679 #define RTS_LONG_LONG_SYMS /* nothing */
682 // 64-bit support functions in libgcc.a
683 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
684 #define RTS_LIBGCC_SYMBOLS \
694 #elif defined(ia64_HOST_ARCH)
695 #define RTS_LIBGCC_SYMBOLS \
703 #define RTS_LIBGCC_SYMBOLS
706 #if defined(darwin_HOST_OS) && defined(powerpc_HOST_ARCH)
707 // Symbols that don't have a leading underscore
708 // on Mac OS X. They have to receive special treatment,
709 // see machoInitSymbolsWithoutUnderscore()
710 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
715 /* entirely bogus claims about types of these symbols */
716 #define Sym(vvv) extern void vvv(void);
717 #define SymX(vvv) /**/
718 #define SymX_redirect(vvv,xxx) /**/
722 RTS_POSIX_ONLY_SYMBOLS
723 RTS_MINGW_ONLY_SYMBOLS
724 RTS_CYGWIN_ONLY_SYMBOLS
725 RTS_DARWIN_ONLY_SYMBOLS
731 #ifdef LEADING_UNDERSCORE
732 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
734 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
737 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
739 #define SymX(vvv) Sym(vvv)
741 // SymX_redirect allows us to redirect references to one symbol to
742 // another symbol. See newCAF/newDynCAF for an example.
743 #define SymX_redirect(vvv,xxx) \
744 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
747 static RtsSymbolVal rtsSyms[] = {
751 RTS_POSIX_ONLY_SYMBOLS
752 RTS_MINGW_ONLY_SYMBOLS
753 RTS_CYGWIN_ONLY_SYMBOLS
755 #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH)
756 // dyld stub code contains references to this,
757 // but it should never be called because we treat
758 // lazy pointers as nonlazy.
759 { "dyld_stub_binding_helper", (void*)0xDEADBEEF },
761 { 0, 0 } /* sentinel */
764 /* -----------------------------------------------------------------------------
765 * Insert symbols into hash tables, checking for duplicates.
767 static void ghciInsertStrHashTable ( char* obj_name,
773 if (lookupHashTable(table, (StgWord)key) == NULL)
775 insertStrHashTable(table, (StgWord)key, data);
780 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
782 "whilst processing object file\n"
784 "This could be caused by:\n"
785 " * Loading two different object files which export the same symbol\n"
786 " * Specifying the same object file twice on the GHCi command line\n"
787 " * An incorrect `package.conf' entry, causing some object to be\n"
789 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
798 /* -----------------------------------------------------------------------------
799 * initialize the object linker
803 static int linker_init_done = 0 ;
805 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
806 static void *dl_prog_handle;
809 /* dlopen(NULL,..) doesn't work so we grab libc explicitly */
810 #if defined(openbsd_HOST_OS)
811 static void *dl_libc_handle;
819 /* Make initLinker idempotent, so we can call it
820 before evey relevant operation; that means we
821 don't need to initialise the linker separately */
822 if (linker_init_done == 1) { return; } else {
823 linker_init_done = 1;
826 symhash = allocStrHashTable();
828 /* populate the symbol table with stuff from the RTS */
829 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
830 ghciInsertStrHashTable("(GHCi built-in symbols)",
831 symhash, sym->lbl, sym->addr);
833 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
834 machoInitSymbolsWithoutUnderscore();
837 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
838 # if defined(RTLD_DEFAULT)
839 dl_prog_handle = RTLD_DEFAULT;
841 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
842 # if defined(openbsd_HOST_OS)
843 dl_libc_handle = dlopen("libc.so", RTLD_LAZY);
845 # endif /* RTLD_DEFAULT */
849 /* -----------------------------------------------------------------------------
850 * Loading DLL or .so dynamic libraries
851 * -----------------------------------------------------------------------------
853 * Add a DLL from which symbols may be found. In the ELF case, just
854 * do RTLD_GLOBAL-style add, so no further messing around needs to
855 * happen in order that symbols in the loaded .so are findable --
856 * lookupSymbol() will subsequently see them by dlsym on the program's
857 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
859 * In the PEi386 case, open the DLLs and put handles to them in a
860 * linked list. When looking for a symbol, try all handles in the
861 * list. This means that we need to load even DLLs that are guaranteed
862 * to be in the ghc.exe image already, just so we can get a handle
863 * to give to loadSymbol, so that we can find the symbols. For such
864 * libraries, the LoadLibrary call should be a no-op except for returning
869 #if defined(OBJFORMAT_PEi386)
870 /* A record for storing handles into DLLs. */
875 struct _OpenedDLL* next;
880 /* A list thereof. */
881 static OpenedDLL* opened_dlls = NULL;
885 addDLL( char *dll_name )
887 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
888 /* ------------------- ELF DLL loader ------------------- */
894 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
897 /* dlopen failed; return a ptr to the error msg. */
899 if (errmsg == NULL) errmsg = "addDLL: unknown error";
906 # elif defined(OBJFORMAT_PEi386)
907 /* ------------------- Win32 DLL loader ------------------- */
915 /* debugBelch("\naddDLL; dll_name = `%s'\n", dll_name); */
917 /* See if we've already got it, and ignore if so. */
918 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
919 if (0 == strcmp(o_dll->name, dll_name))
923 /* The file name has no suffix (yet) so that we can try
924 both foo.dll and foo.drv
926 The documentation for LoadLibrary says:
927 If no file name extension is specified in the lpFileName
928 parameter, the default library extension .dll is
929 appended. However, the file name string can include a trailing
930 point character (.) to indicate that the module name has no
933 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
934 sprintf(buf, "%s.DLL", dll_name);
935 instance = LoadLibrary(buf);
936 if (instance == NULL) {
937 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
938 instance = LoadLibrary(buf);
939 if (instance == NULL) {
942 /* LoadLibrary failed; return a ptr to the error msg. */
943 return "addDLL: unknown error";
948 /* Add this DLL to the list of DLLs in which to search for symbols. */
949 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
950 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
951 strcpy(o_dll->name, dll_name);
952 o_dll->instance = instance;
953 o_dll->next = opened_dlls;
958 barf("addDLL: not implemented on this platform");
962 /* -----------------------------------------------------------------------------
963 * lookup a symbol in the hash table
966 lookupSymbol( char *lbl )
970 ASSERT(symhash != NULL);
971 val = lookupStrHashTable(symhash, lbl);
974 # if defined(OBJFORMAT_ELF)
975 # if defined(openbsd_HOST_OS)
976 val = dlsym(dl_prog_handle, lbl);
977 return (val != NULL) ? val : dlsym(dl_libc_handle,lbl);
978 # elif defined(x86_64_HOST_ARCH)
979 val = dlsym(dl_prog_handle, lbl);
980 if (val >= (void *)0x80000000) {
982 new_val = x86_64_high_symbol(lbl, val);
983 IF_DEBUG(linker,debugBelch("lookupSymbol: relocating out of range symbol: %s = %p, now %p\n", lbl, val, new_val));
988 # else /* not openbsd */
989 return dlsym(dl_prog_handle, lbl);
991 # elif defined(OBJFORMAT_MACHO)
992 if(NSIsSymbolNameDefined(lbl)) {
993 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
994 return NSAddressOfSymbol(symbol);
998 # elif defined(OBJFORMAT_PEi386)
1001 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
1002 /* debugBelch("look in %s for %s\n", o_dll->name, lbl); */
1003 if (lbl[0] == '_') {
1004 /* HACK: if the name has an initial underscore, try stripping
1005 it off & look that up first. I've yet to verify whether there's
1006 a Rule that governs whether an initial '_' *should always* be
1007 stripped off when mapping from import lib name to the DLL name.
1009 sym = GetProcAddress(o_dll->instance, (lbl+1));
1011 /*debugBelch("found %s in %s\n", lbl+1,o_dll->name);*/
1015 sym = GetProcAddress(o_dll->instance, lbl);
1017 /*debugBelch("found %s in %s\n", lbl,o_dll->name);*/
1032 __attribute((unused))
1034 lookupLocalSymbol( ObjectCode* oc, char *lbl )
1038 val = lookupStrHashTable(oc->lochash, lbl);
1048 /* -----------------------------------------------------------------------------
1049 * Debugging aid: look in GHCi's object symbol tables for symbols
1050 * within DELTA bytes of the specified address, and show their names.
1053 void ghci_enquire ( char* addr );
1055 void ghci_enquire ( char* addr )
1060 const int DELTA = 64;
1065 for (oc = objects; oc; oc = oc->next) {
1066 for (i = 0; i < oc->n_symbols; i++) {
1067 sym = oc->symbols[i];
1068 if (sym == NULL) continue;
1069 // debugBelch("enquire %p %p\n", sym, oc->lochash);
1071 if (oc->lochash != NULL) {
1072 a = lookupStrHashTable(oc->lochash, sym);
1075 a = lookupStrHashTable(symhash, sym);
1078 // debugBelch("ghci_enquire: can't find %s\n", sym);
1080 else if (addr-DELTA <= a && a <= addr+DELTA) {
1081 debugBelch("%p + %3d == `%s'\n", addr, (int)(a - addr), sym);
1088 #ifdef ia64_HOST_ARCH
1089 static unsigned int PLTSize(void);
1092 /* -----------------------------------------------------------------------------
1093 * Load an obj (populate the global symbol table, but don't resolve yet)
1095 * Returns: 1 if ok, 0 on error.
1098 loadObj( char *path )
1105 void *map_addr = NULL;
1112 /* debugBelch("loadObj %s\n", path ); */
1114 /* Check that we haven't already loaded this object.
1115 Ignore requests to load multiple times */
1119 for (o = objects; o; o = o->next) {
1120 if (0 == strcmp(o->fileName, path)) {
1122 break; /* don't need to search further */
1126 IF_DEBUG(linker, debugBelch(
1127 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1128 "same object file twice:\n"
1130 "GHCi will ignore this, but be warned.\n"
1132 return 1; /* success */
1136 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
1138 # if defined(OBJFORMAT_ELF)
1139 oc->formatName = "ELF";
1140 # elif defined(OBJFORMAT_PEi386)
1141 oc->formatName = "PEi386";
1142 # elif defined(OBJFORMAT_MACHO)
1143 oc->formatName = "Mach-O";
1146 barf("loadObj: not implemented on this platform");
1149 r = stat(path, &st);
1150 if (r == -1) { return 0; }
1152 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1153 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1154 strcpy(oc->fileName, path);
1156 oc->fileSize = st.st_size;
1158 oc->sections = NULL;
1159 oc->lochash = allocStrHashTable();
1160 oc->proddables = NULL;
1162 /* chain it onto the list of objects */
1167 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1169 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1171 #if defined(openbsd_HOST_OS)
1172 fd = open(path, O_RDONLY, S_IRUSR);
1174 fd = open(path, O_RDONLY);
1177 barf("loadObj: can't open `%s'", path);
1179 pagesize = getpagesize();
1181 #ifdef ia64_HOST_ARCH
1182 /* The PLT needs to be right before the object */
1183 n = ROUND_UP(PLTSize(), pagesize);
1184 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1185 if (oc->plt == MAP_FAILED)
1186 barf("loadObj: can't allocate PLT");
1189 map_addr = oc->plt + n;
1192 n = ROUND_UP(oc->fileSize, pagesize);
1194 /* Link objects into the lower 2Gb on x86_64. GHC assumes the
1195 * small memory model on this architecture (see gcc docs,
1198 #ifdef x86_64_HOST_ARCH
1199 #define EXTRA_MAP_FLAGS MAP_32BIT
1201 #define EXTRA_MAP_FLAGS 0
1204 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE,
1205 MAP_PRIVATE|EXTRA_MAP_FLAGS, fd, 0);
1206 if (oc->image == MAP_FAILED)
1207 barf("loadObj: can't map `%s'", path);
1211 #else /* !USE_MMAP */
1213 /* load the image into memory */
1214 f = fopen(path, "rb");
1216 barf("loadObj: can't read `%s'", path);
1218 #ifdef darwin_HOST_OS
1219 // In a Mach-O .o file, all sections can and will be misaligned
1220 // if the total size of the headers is not a multiple of the
1221 // desired alignment. This is fine for .o files that only serve
1222 // as input for the static linker, but it's not fine for us,
1223 // as SSE (used by gcc for floating point) and Altivec require
1224 // 16-byte alignment.
1225 // We calculate the correct alignment from the header before
1226 // reading the file, and then we misalign oc->image on purpose so
1227 // that the actual sections end up aligned again.
1228 misalignment = machoGetMisalignment(f);
1229 oc->misalignment = misalignment;
1234 oc->image = stgMallocBytes(oc->fileSize + misalignment, "loadObj(image)");
1235 oc->image += misalignment;
1237 n = fread ( oc->image, 1, oc->fileSize, f );
1238 if (n != oc->fileSize)
1239 barf("loadObj: error whilst reading `%s'", path);
1243 #endif /* USE_MMAP */
1245 # if defined(OBJFORMAT_MACHO) && defined(powerpc_HOST_ARCH)
1246 r = ocAllocateJumpIslands_MachO ( oc );
1247 if (!r) { return r; }
1248 # elif defined(OBJFORMAT_ELF) && defined(powerpc_HOST_ARCH)
1249 r = ocAllocateJumpIslands_ELF ( oc );
1250 if (!r) { return r; }
1253 /* verify the in-memory image */
1254 # if defined(OBJFORMAT_ELF)
1255 r = ocVerifyImage_ELF ( oc );
1256 # elif defined(OBJFORMAT_PEi386)
1257 r = ocVerifyImage_PEi386 ( oc );
1258 # elif defined(OBJFORMAT_MACHO)
1259 r = ocVerifyImage_MachO ( oc );
1261 barf("loadObj: no verify method");
1263 if (!r) { return r; }
1265 /* build the symbol list for this image */
1266 # if defined(OBJFORMAT_ELF)
1267 r = ocGetNames_ELF ( oc );
1268 # elif defined(OBJFORMAT_PEi386)
1269 r = ocGetNames_PEi386 ( oc );
1270 # elif defined(OBJFORMAT_MACHO)
1271 r = ocGetNames_MachO ( oc );
1273 barf("loadObj: no getNames method");
1275 if (!r) { return r; }
1277 /* loaded, but not resolved yet */
1278 oc->status = OBJECT_LOADED;
1283 /* -----------------------------------------------------------------------------
1284 * resolve all the currently unlinked objects in memory
1286 * Returns: 1 if ok, 0 on error.
1296 for (oc = objects; oc; oc = oc->next) {
1297 if (oc->status != OBJECT_RESOLVED) {
1298 # if defined(OBJFORMAT_ELF)
1299 r = ocResolve_ELF ( oc );
1300 # elif defined(OBJFORMAT_PEi386)
1301 r = ocResolve_PEi386 ( oc );
1302 # elif defined(OBJFORMAT_MACHO)
1303 r = ocResolve_MachO ( oc );
1305 barf("resolveObjs: not implemented on this platform");
1307 if (!r) { return r; }
1308 oc->status = OBJECT_RESOLVED;
1314 /* -----------------------------------------------------------------------------
1315 * delete an object from the pool
1318 unloadObj( char *path )
1320 ObjectCode *oc, *prev;
1322 ASSERT(symhash != NULL);
1323 ASSERT(objects != NULL);
1328 for (oc = objects; oc; prev = oc, oc = oc->next) {
1329 if (!strcmp(oc->fileName,path)) {
1331 /* Remove all the mappings for the symbols within this
1336 for (i = 0; i < oc->n_symbols; i++) {
1337 if (oc->symbols[i] != NULL) {
1338 removeStrHashTable(symhash, oc->symbols[i], NULL);
1346 prev->next = oc->next;
1349 /* We're going to leave this in place, in case there are
1350 any pointers from the heap into it: */
1351 /* stgFree(oc->image); */
1352 stgFree(oc->fileName);
1353 stgFree(oc->symbols);
1354 stgFree(oc->sections);
1355 /* The local hash table should have been freed at the end
1356 of the ocResolve_ call on it. */
1357 ASSERT(oc->lochash == NULL);
1363 errorBelch("unloadObj: can't find `%s' to unload", path);
1367 /* -----------------------------------------------------------------------------
1368 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1369 * which may be prodded during relocation, and abort if we try and write
1370 * outside any of these.
1372 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1375 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1376 /* debugBelch("aPB %p %p %d\n", oc, start, size); */
1380 pb->next = oc->proddables;
1381 oc->proddables = pb;
1384 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1387 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1388 char* s = (char*)(pb->start);
1389 char* e = s + pb->size - 1;
1390 char* a = (char*)addr;
1391 /* Assumes that the biggest fixup involves a 4-byte write. This
1392 probably needs to be changed to 8 (ie, +7) on 64-bit
1394 if (a >= s && (a+3) <= e) return;
1396 barf("checkProddableBlock: invalid fixup in runtime linker");
1399 /* -----------------------------------------------------------------------------
1400 * Section management.
1402 static void addSection ( ObjectCode* oc, SectionKind kind,
1403 void* start, void* end )
1405 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1409 s->next = oc->sections;
1412 debugBelch("addSection: %p-%p (size %d), kind %d\n",
1413 start, ((char*)end)-1, end - start + 1, kind );
1418 /* --------------------------------------------------------------------------
1419 * PowerPC specifics (jump islands)
1420 * ------------------------------------------------------------------------*/
1422 #if defined(powerpc_HOST_ARCH)
1425 ocAllocateJumpIslands
1427 Allocate additional space at the end of the object file image to make room
1430 PowerPC relative branch instructions have a 24 bit displacement field.
1431 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
1432 If a particular imported symbol is outside this range, we have to redirect
1433 the jump to a short piece of new code that just loads the 32bit absolute
1434 address and jumps there.
1435 This function just allocates space for one 16 byte ppcJumpIsland for every
1436 undefined symbol in the object file. The code for the islands is filled in by
1437 makeJumpIsland below.
1440 static int ocAllocateJumpIslands( ObjectCode* oc, int count, int first )
1446 int misalignment = 0;
1448 misalignment = oc->misalignment;
1453 // round up to the nearest 4
1454 aligned = (oc->fileSize + 3) & ~3;
1457 #ifndef linux_HOST_OS /* mremap is a linux extension */
1458 #error ocAllocateJumpIslands doesnt want USE_MMAP to be defined
1461 pagesize = getpagesize();
1462 n = ROUND_UP( oc->fileSize, pagesize );
1463 m = ROUND_UP( aligned + sizeof (ppcJumpIsland) * count, pagesize );
1465 /* If we have a half-page-size file and map one page of it then
1466 * the part of the page after the size of the file remains accessible.
1467 * If, however, we map in 2 pages, the 2nd page is not accessible
1468 * and will give a "Bus Error" on access. To get around this, we check
1469 * if we need any extra pages for the jump islands and map them in
1470 * anonymously. We must check that we actually require extra pages
1471 * otherwise the attempt to mmap 0 pages of anonymous memory will
1477 /* The effect of this mremap() call is only the ensure that we have
1478 * a sufficient number of virtually contiguous pages. As returned from
1479 * mremap, the pages past the end of the file are not backed. We give
1480 * them a backing by using MAP_FIXED to map in anonymous pages.
1482 oc->image = mremap( oc->image, n, m, MREMAP_MAYMOVE );
1484 if( oc->image == MAP_FAILED )
1486 errorBelch( "Unable to mremap for Jump Islands\n" );
1490 if( mmap( oc->image + n, m - n, PROT_READ | PROT_WRITE | PROT_EXEC,
1491 MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0 ) == MAP_FAILED )
1493 errorBelch( "Unable to mmap( MAP_FIXED ) for Jump Islands\n" );
1499 oc->image -= misalignment;
1500 oc->image = stgReallocBytes( oc->image,
1502 aligned + sizeof (ppcJumpIsland) * count,
1503 "ocAllocateJumpIslands" );
1504 oc->image += misalignment;
1505 #endif /* USE_MMAP */
1507 oc->jump_islands = (ppcJumpIsland *) (oc->image + aligned);
1508 memset( oc->jump_islands, 0, sizeof (ppcJumpIsland) * count );
1511 oc->jump_islands = NULL;
1513 oc->island_start_symbol = first;
1514 oc->n_islands = count;
1519 static unsigned long makeJumpIsland( ObjectCode* oc,
1520 unsigned long symbolNumber,
1521 unsigned long target )
1523 ppcJumpIsland *island;
1525 if( symbolNumber < oc->island_start_symbol ||
1526 symbolNumber - oc->island_start_symbol > oc->n_islands)
1529 island = &oc->jump_islands[symbolNumber - oc->island_start_symbol];
1531 // lis r12, hi16(target)
1532 island->lis_r12 = 0x3d80;
1533 island->hi_addr = target >> 16;
1535 // ori r12, r12, lo16(target)
1536 island->ori_r12_r12 = 0x618c;
1537 island->lo_addr = target & 0xffff;
1540 island->mtctr_r12 = 0x7d8903a6;
1543 island->bctr = 0x4e800420;
1545 return (unsigned long) island;
1549 ocFlushInstructionCache
1551 Flush the data & instruction caches.
1552 Because the PPC has split data/instruction caches, we have to
1553 do that whenever we modify code at runtime.
1556 static void ocFlushInstructionCache( ObjectCode *oc )
1558 int n = (oc->fileSize + sizeof( ppcJumpIsland ) * oc->n_islands + 3) / 4;
1559 unsigned long *p = (unsigned long *) oc->image;
1563 __asm__ volatile ( "dcbf 0,%0\n\t"
1571 __asm__ volatile ( "sync\n\t"
1577 /* --------------------------------------------------------------------------
1578 * PEi386 specifics (Win32 targets)
1579 * ------------------------------------------------------------------------*/
1581 /* The information for this linker comes from
1582 Microsoft Portable Executable
1583 and Common Object File Format Specification
1584 revision 5.1 January 1998
1585 which SimonM says comes from the MS Developer Network CDs.
1587 It can be found there (on older CDs), but can also be found
1590 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1592 (this is Rev 6.0 from February 1999).
1594 Things move, so if that fails, try searching for it via
1596 http://www.google.com/search?q=PE+COFF+specification
1598 The ultimate reference for the PE format is the Winnt.h
1599 header file that comes with the Platform SDKs; as always,
1600 implementations will drift wrt their documentation.
1602 A good background article on the PE format is Matt Pietrek's
1603 March 1994 article in Microsoft System Journal (MSJ)
1604 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1605 Win32 Portable Executable File Format." The info in there
1606 has recently been updated in a two part article in
1607 MSDN magazine, issues Feb and March 2002,
1608 "Inside Windows: An In-Depth Look into the Win32 Portable
1609 Executable File Format"
1611 John Levine's book "Linkers and Loaders" contains useful
1616 #if defined(OBJFORMAT_PEi386)
1620 typedef unsigned char UChar;
1621 typedef unsigned short UInt16;
1622 typedef unsigned int UInt32;
1629 UInt16 NumberOfSections;
1630 UInt32 TimeDateStamp;
1631 UInt32 PointerToSymbolTable;
1632 UInt32 NumberOfSymbols;
1633 UInt16 SizeOfOptionalHeader;
1634 UInt16 Characteristics;
1638 #define sizeof_COFF_header 20
1645 UInt32 VirtualAddress;
1646 UInt32 SizeOfRawData;
1647 UInt32 PointerToRawData;
1648 UInt32 PointerToRelocations;
1649 UInt32 PointerToLinenumbers;
1650 UInt16 NumberOfRelocations;
1651 UInt16 NumberOfLineNumbers;
1652 UInt32 Characteristics;
1656 #define sizeof_COFF_section 40
1663 UInt16 SectionNumber;
1666 UChar NumberOfAuxSymbols;
1670 #define sizeof_COFF_symbol 18
1675 UInt32 VirtualAddress;
1676 UInt32 SymbolTableIndex;
1681 #define sizeof_COFF_reloc 10
1684 /* From PE spec doc, section 3.3.2 */
1685 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1686 windows.h -- for the same purpose, but I want to know what I'm
1688 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1689 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1690 #define MYIMAGE_FILE_DLL 0x2000
1691 #define MYIMAGE_FILE_SYSTEM 0x1000
1692 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1693 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1694 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1696 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1697 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1698 #define MYIMAGE_SYM_CLASS_STATIC 3
1699 #define MYIMAGE_SYM_UNDEFINED 0
1701 /* From PE spec doc, section 4.1 */
1702 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1703 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1704 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1706 /* From PE spec doc, section 5.2.1 */
1707 #define MYIMAGE_REL_I386_DIR32 0x0006
1708 #define MYIMAGE_REL_I386_REL32 0x0014
1711 /* We use myindex to calculate array addresses, rather than
1712 simply doing the normal subscript thing. That's because
1713 some of the above structs have sizes which are not
1714 a whole number of words. GCC rounds their sizes up to a
1715 whole number of words, which means that the address calcs
1716 arising from using normal C indexing or pointer arithmetic
1717 are just plain wrong. Sigh.
1720 myindex ( int scale, void* base, int index )
1723 ((UChar*)base) + scale * index;
1728 printName ( UChar* name, UChar* strtab )
1730 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1731 UInt32 strtab_offset = * (UInt32*)(name+4);
1732 debugBelch("%s", strtab + strtab_offset );
1735 for (i = 0; i < 8; i++) {
1736 if (name[i] == 0) break;
1737 debugBelch("%c", name[i] );
1744 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1746 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1747 UInt32 strtab_offset = * (UInt32*)(name+4);
1748 strncpy ( dst, strtab+strtab_offset, dstSize );
1754 if (name[i] == 0) break;
1764 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1767 /* If the string is longer than 8 bytes, look in the
1768 string table for it -- this will be correctly zero terminated.
1770 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1771 UInt32 strtab_offset = * (UInt32*)(name+4);
1772 return ((UChar*)strtab) + strtab_offset;
1774 /* Otherwise, if shorter than 8 bytes, return the original,
1775 which by defn is correctly terminated.
1777 if (name[7]==0) return name;
1778 /* The annoying case: 8 bytes. Copy into a temporary
1779 (which is never freed ...)
1781 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1783 strncpy(newstr,name,8);
1789 /* Just compares the short names (first 8 chars) */
1790 static COFF_section *
1791 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1795 = (COFF_header*)(oc->image);
1796 COFF_section* sectab
1798 ((UChar*)(oc->image))
1799 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1801 for (i = 0; i < hdr->NumberOfSections; i++) {
1804 COFF_section* section_i
1806 myindex ( sizeof_COFF_section, sectab, i );
1807 n1 = (UChar*) &(section_i->Name);
1809 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1810 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1811 n1[6]==n2[6] && n1[7]==n2[7])
1820 zapTrailingAtSign ( UChar* sym )
1822 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1824 if (sym[0] == 0) return;
1826 while (sym[i] != 0) i++;
1829 while (j > 0 && my_isdigit(sym[j])) j--;
1830 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1836 ocVerifyImage_PEi386 ( ObjectCode* oc )
1841 COFF_section* sectab;
1842 COFF_symbol* symtab;
1844 /* debugBelch("\nLOADING %s\n", oc->fileName); */
1845 hdr = (COFF_header*)(oc->image);
1846 sectab = (COFF_section*) (
1847 ((UChar*)(oc->image))
1848 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1850 symtab = (COFF_symbol*) (
1851 ((UChar*)(oc->image))
1852 + hdr->PointerToSymbolTable
1854 strtab = ((UChar*)symtab)
1855 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1857 if (hdr->Machine != 0x14c) {
1858 errorBelch("%s: Not x86 PEi386", oc->fileName);
1861 if (hdr->SizeOfOptionalHeader != 0) {
1862 errorBelch("%s: PEi386 with nonempty optional header", oc->fileName);
1865 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1866 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1867 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1868 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1869 errorBelch("%s: Not a PEi386 object file", oc->fileName);
1872 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1873 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1874 errorBelch("%s: Invalid PEi386 word size or endiannness: %d",
1876 (int)(hdr->Characteristics));
1879 /* If the string table size is way crazy, this might indicate that
1880 there are more than 64k relocations, despite claims to the
1881 contrary. Hence this test. */
1882 /* debugBelch("strtab size %d\n", * (UInt32*)strtab); */
1884 if ( (*(UInt32*)strtab) > 600000 ) {
1885 /* Note that 600k has no special significance other than being
1886 big enough to handle the almost-2MB-sized lumps that
1887 constitute HSwin32*.o. */
1888 debugBelch("PEi386 object has suspiciously large string table; > 64k relocs?");
1893 /* No further verification after this point; only debug printing. */
1895 IF_DEBUG(linker, i=1);
1896 if (i == 0) return 1;
1898 debugBelch( "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1899 debugBelch( "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1900 debugBelch( "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1903 debugBelch( "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1904 debugBelch( "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1905 debugBelch( "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1906 debugBelch( "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1907 debugBelch( "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1908 debugBelch( "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1909 debugBelch( "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1911 /* Print the section table. */
1913 for (i = 0; i < hdr->NumberOfSections; i++) {
1915 COFF_section* sectab_i
1917 myindex ( sizeof_COFF_section, sectab, i );
1924 printName ( sectab_i->Name, strtab );
1934 sectab_i->VirtualSize,
1935 sectab_i->VirtualAddress,
1936 sectab_i->SizeOfRawData,
1937 sectab_i->PointerToRawData,
1938 sectab_i->NumberOfRelocations,
1939 sectab_i->PointerToRelocations,
1940 sectab_i->PointerToRawData
1942 reltab = (COFF_reloc*) (
1943 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1946 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1947 /* If the relocation field (a short) has overflowed, the
1948 * real count can be found in the first reloc entry.
1950 * See Section 4.1 (last para) of the PE spec (rev6.0).
1952 COFF_reloc* rel = (COFF_reloc*)
1953 myindex ( sizeof_COFF_reloc, reltab, 0 );
1954 noRelocs = rel->VirtualAddress;
1957 noRelocs = sectab_i->NumberOfRelocations;
1961 for (; j < noRelocs; j++) {
1963 COFF_reloc* rel = (COFF_reloc*)
1964 myindex ( sizeof_COFF_reloc, reltab, j );
1966 " type 0x%-4x vaddr 0x%-8x name `",
1968 rel->VirtualAddress );
1969 sym = (COFF_symbol*)
1970 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1971 /* Hmm..mysterious looking offset - what's it for? SOF */
1972 printName ( sym->Name, strtab -10 );
1979 debugBelch("string table has size 0x%x\n", * (UInt32*)strtab );
1980 debugBelch("---START of string table---\n");
1981 for (i = 4; i < *(Int32*)strtab; i++) {
1983 debugBelch("\n"); else
1984 debugBelch("%c", strtab[i] );
1986 debugBelch("--- END of string table---\n");
1991 COFF_symbol* symtab_i;
1992 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1993 symtab_i = (COFF_symbol*)
1994 myindex ( sizeof_COFF_symbol, symtab, i );
2000 printName ( symtab_i->Name, strtab );
2009 (Int32)(symtab_i->SectionNumber),
2010 (UInt32)symtab_i->Type,
2011 (UInt32)symtab_i->StorageClass,
2012 (UInt32)symtab_i->NumberOfAuxSymbols
2014 i += symtab_i->NumberOfAuxSymbols;
2024 ocGetNames_PEi386 ( ObjectCode* oc )
2027 COFF_section* sectab;
2028 COFF_symbol* symtab;
2035 hdr = (COFF_header*)(oc->image);
2036 sectab = (COFF_section*) (
2037 ((UChar*)(oc->image))
2038 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2040 symtab = (COFF_symbol*) (
2041 ((UChar*)(oc->image))
2042 + hdr->PointerToSymbolTable
2044 strtab = ((UChar*)(oc->image))
2045 + hdr->PointerToSymbolTable
2046 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2048 /* Allocate space for any (local, anonymous) .bss sections. */
2050 for (i = 0; i < hdr->NumberOfSections; i++) {
2053 COFF_section* sectab_i
2055 myindex ( sizeof_COFF_section, sectab, i );
2056 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
2057 /* sof 10/05: the PE spec text isn't too clear regarding what
2058 * the SizeOfRawData field is supposed to hold for object
2059 * file sections containing just uninitialized data -- for executables,
2060 * it is supposed to be zero; unclear what it's supposed to be
2061 * for object files. However, VirtualSize is guaranteed to be
2062 * zero for object files, which definitely suggests that SizeOfRawData
2063 * will be non-zero (where else would the size of this .bss section be
2064 * stored?) Looking at the COFF_section info for incoming object files,
2065 * this certainly appears to be the case.
2067 * => I suspect we've been incorrectly handling .bss sections in (relocatable)
2068 * object files up until now. This turned out to bite us with ghc-6.4.1's use
2069 * of gcc-3.4.x, which has started to emit initially-zeroed-out local 'static'
2070 * variable decls into to the .bss section. (The specific function in Q which
2071 * triggered this is libraries/base/cbits/dirUtils.c:__hscore_getFolderPath())
2073 if (sectab_i->VirtualSize == 0 && sectab_i->SizeOfRawData == 0) continue;
2074 /* This is a non-empty .bss section. Allocate zeroed space for
2075 it, and set its PointerToRawData field such that oc->image +
2076 PointerToRawData == addr_of_zeroed_space. */
2077 bss_sz = sectab_i->VirtualSize;
2078 if ( bss_sz < sectab_i->SizeOfRawData) { bss_sz = sectab_i->SizeOfRawData; }
2079 zspace = stgCallocBytes(1, bss_sz, "ocGetNames_PEi386(anonymous bss)");
2080 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
2081 addProddableBlock(oc, zspace, bss_sz);
2082 /* debugBelch("BSS anon section at 0x%x\n", zspace); */
2085 /* Copy section information into the ObjectCode. */
2087 for (i = 0; i < hdr->NumberOfSections; i++) {
2093 = SECTIONKIND_OTHER;
2094 COFF_section* sectab_i
2096 myindex ( sizeof_COFF_section, sectab, i );
2097 IF_DEBUG(linker, debugBelch("section name = %s\n", sectab_i->Name ));
2100 /* I'm sure this is the Right Way to do it. However, the
2101 alternative of testing the sectab_i->Name field seems to
2102 work ok with Cygwin.
2104 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
2105 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
2106 kind = SECTIONKIND_CODE_OR_RODATA;
2109 if (0==strcmp(".text",sectab_i->Name) ||
2110 0==strcmp(".rdata",sectab_i->Name)||
2111 0==strcmp(".rodata",sectab_i->Name))
2112 kind = SECTIONKIND_CODE_OR_RODATA;
2113 if (0==strcmp(".data",sectab_i->Name) ||
2114 0==strcmp(".bss",sectab_i->Name))
2115 kind = SECTIONKIND_RWDATA;
2117 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
2118 sz = sectab_i->SizeOfRawData;
2119 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
2121 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
2122 end = start + sz - 1;
2124 if (kind == SECTIONKIND_OTHER
2125 /* Ignore sections called which contain stabs debugging
2127 && 0 != strcmp(".stab", sectab_i->Name)
2128 && 0 != strcmp(".stabstr", sectab_i->Name)
2129 /* ignore constructor section for now */
2130 && 0 != strcmp(".ctors", sectab_i->Name)
2132 errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", sectab_i->Name, oc->fileName);
2136 if (kind != SECTIONKIND_OTHER && end >= start) {
2137 addSection(oc, kind, start, end);
2138 addProddableBlock(oc, start, end - start + 1);
2142 /* Copy exported symbols into the ObjectCode. */
2144 oc->n_symbols = hdr->NumberOfSymbols;
2145 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2146 "ocGetNames_PEi386(oc->symbols)");
2147 /* Call me paranoid; I don't care. */
2148 for (i = 0; i < oc->n_symbols; i++)
2149 oc->symbols[i] = NULL;
2153 COFF_symbol* symtab_i;
2154 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
2155 symtab_i = (COFF_symbol*)
2156 myindex ( sizeof_COFF_symbol, symtab, i );
2160 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
2161 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
2162 /* This symbol is global and defined, viz, exported */
2163 /* for MYIMAGE_SYMCLASS_EXTERNAL
2164 && !MYIMAGE_SYM_UNDEFINED,
2165 the address of the symbol is:
2166 address of relevant section + offset in section
2168 COFF_section* sectabent
2169 = (COFF_section*) myindex ( sizeof_COFF_section,
2171 symtab_i->SectionNumber-1 );
2172 addr = ((UChar*)(oc->image))
2173 + (sectabent->PointerToRawData
2177 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
2178 && symtab_i->Value > 0) {
2179 /* This symbol isn't in any section at all, ie, global bss.
2180 Allocate zeroed space for it. */
2181 addr = stgCallocBytes(1, symtab_i->Value,
2182 "ocGetNames_PEi386(non-anonymous bss)");
2183 addSection(oc, SECTIONKIND_RWDATA, addr,
2184 ((UChar*)addr) + symtab_i->Value - 1);
2185 addProddableBlock(oc, addr, symtab_i->Value);
2186 /* debugBelch("BSS section at 0x%x\n", addr); */
2189 if (addr != NULL ) {
2190 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
2191 /* debugBelch("addSymbol %p `%s \n", addr,sname); */
2192 IF_DEBUG(linker, debugBelch("addSymbol %p `%s'\n", addr,sname);)
2193 ASSERT(i >= 0 && i < oc->n_symbols);
2194 /* cstring_from_COFF_symbol_name always succeeds. */
2195 oc->symbols[i] = sname;
2196 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
2200 "IGNORING symbol %d\n"
2204 printName ( symtab_i->Name, strtab );
2213 (Int32)(symtab_i->SectionNumber),
2214 (UInt32)symtab_i->Type,
2215 (UInt32)symtab_i->StorageClass,
2216 (UInt32)symtab_i->NumberOfAuxSymbols
2221 i += symtab_i->NumberOfAuxSymbols;
2230 ocResolve_PEi386 ( ObjectCode* oc )
2233 COFF_section* sectab;
2234 COFF_symbol* symtab;
2244 /* ToDo: should be variable-sized? But is at least safe in the
2245 sense of buffer-overrun-proof. */
2247 /* debugBelch("resolving for %s\n", oc->fileName); */
2249 hdr = (COFF_header*)(oc->image);
2250 sectab = (COFF_section*) (
2251 ((UChar*)(oc->image))
2252 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
2254 symtab = (COFF_symbol*) (
2255 ((UChar*)(oc->image))
2256 + hdr->PointerToSymbolTable
2258 strtab = ((UChar*)(oc->image))
2259 + hdr->PointerToSymbolTable
2260 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
2262 for (i = 0; i < hdr->NumberOfSections; i++) {
2263 COFF_section* sectab_i
2265 myindex ( sizeof_COFF_section, sectab, i );
2268 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
2271 /* Ignore sections called which contain stabs debugging
2273 if (0 == strcmp(".stab", sectab_i->Name)
2274 || 0 == strcmp(".stabstr", sectab_i->Name)
2275 || 0 == strcmp(".ctors", sectab_i->Name))
2278 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
2279 /* If the relocation field (a short) has overflowed, the
2280 * real count can be found in the first reloc entry.
2282 * See Section 4.1 (last para) of the PE spec (rev6.0).
2284 * Nov2003 update: the GNU linker still doesn't correctly
2285 * handle the generation of relocatable object files with
2286 * overflown relocations. Hence the output to warn of potential
2289 COFF_reloc* rel = (COFF_reloc*)
2290 myindex ( sizeof_COFF_reloc, reltab, 0 );
2291 noRelocs = rel->VirtualAddress;
2293 /* 10/05: we now assume (and check for) a GNU ld that is capable
2294 * of handling object files with (>2^16) of relocs.
2297 debugBelch("WARNING: Overflown relocation field (# relocs found: %u)\n",
2302 noRelocs = sectab_i->NumberOfRelocations;
2307 for (; j < noRelocs; j++) {
2309 COFF_reloc* reltab_j
2311 myindex ( sizeof_COFF_reloc, reltab, j );
2313 /* the location to patch */
2315 ((UChar*)(oc->image))
2316 + (sectab_i->PointerToRawData
2317 + reltab_j->VirtualAddress
2318 - sectab_i->VirtualAddress )
2320 /* the existing contents of pP */
2322 /* the symbol to connect to */
2323 sym = (COFF_symbol*)
2324 myindex ( sizeof_COFF_symbol,
2325 symtab, reltab_j->SymbolTableIndex );
2328 "reloc sec %2d num %3d: type 0x%-4x "
2329 "vaddr 0x%-8x name `",
2331 (UInt32)reltab_j->Type,
2332 reltab_j->VirtualAddress );
2333 printName ( sym->Name, strtab );
2334 debugBelch("'\n" ));
2336 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
2337 COFF_section* section_sym
2338 = findPEi386SectionCalled ( oc, sym->Name );
2340 errorBelch("%s: can't find section `%s'", oc->fileName, sym->Name);
2343 S = ((UInt32)(oc->image))
2344 + (section_sym->PointerToRawData
2347 copyName ( sym->Name, strtab, symbol, 1000-1 );
2348 (void*)S = lookupLocalSymbol( oc, symbol );
2349 if ((void*)S != NULL) goto foundit;
2350 (void*)S = lookupSymbol( symbol );
2351 if ((void*)S != NULL) goto foundit;
2352 zapTrailingAtSign ( symbol );
2353 (void*)S = lookupLocalSymbol( oc, symbol );
2354 if ((void*)S != NULL) goto foundit;
2355 (void*)S = lookupSymbol( symbol );
2356 if ((void*)S != NULL) goto foundit;
2357 /* Newline first because the interactive linker has printed "linking..." */
2358 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
2362 checkProddableBlock(oc, pP);
2363 switch (reltab_j->Type) {
2364 case MYIMAGE_REL_I386_DIR32:
2367 case MYIMAGE_REL_I386_REL32:
2368 /* Tricky. We have to insert a displacement at
2369 pP which, when added to the PC for the _next_
2370 insn, gives the address of the target (S).
2371 Problem is to know the address of the next insn
2372 when we only know pP. We assume that this
2373 literal field is always the last in the insn,
2374 so that the address of the next insn is pP+4
2375 -- hence the constant 4.
2376 Also I don't know if A should be added, but so
2377 far it has always been zero.
2379 SOF 05/2005: 'A' (old contents of *pP) have been observed
2380 to contain values other than zero (the 'wx' object file
2381 that came with wxhaskell-0.9.4; dunno how it was compiled..).
2382 So, add displacement to old value instead of asserting
2383 A to be zero. Fixes wxhaskell-related crashes, and no other
2384 ill effects have been observed.
2386 Update: the reason why we're seeing these more elaborate
2387 relocations is due to a switch in how the NCG compiles SRTs
2388 and offsets to them from info tables. SRTs live in .(ro)data,
2389 while info tables live in .text, causing GAS to emit REL32/DISP32
2390 relocations with non-zero values. Adding the displacement is
2391 the right thing to do.
2393 *pP = S - ((UInt32)pP) - 4 + A;
2396 debugBelch("%s: unhandled PEi386 relocation type %d",
2397 oc->fileName, reltab_j->Type);
2404 IF_DEBUG(linker, debugBelch("completed %s", oc->fileName));
2408 #endif /* defined(OBJFORMAT_PEi386) */
2411 /* --------------------------------------------------------------------------
2413 * ------------------------------------------------------------------------*/
2415 #if defined(OBJFORMAT_ELF)
2420 #if defined(sparc_HOST_ARCH)
2421 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2422 #elif defined(i386_HOST_ARCH)
2423 # define ELF_TARGET_386 /* Used inside <elf.h> */
2424 #elif defined(x86_64_HOST_ARCH)
2425 # define ELF_TARGET_X64_64
2427 #elif defined (ia64_HOST_ARCH)
2428 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2430 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2431 # define ELF_NEED_GOT /* needs Global Offset Table */
2432 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2435 #if !defined(openbsd_HOST_OS)
2438 /* openbsd elf has things in different places, with diff names */
2439 #include <elf_abi.h>
2440 #include <machine/reloc.h>
2441 #define R_386_32 RELOC_32
2442 #define R_386_PC32 RELOC_PC32
2446 * Define a set of types which can be used for both ELF32 and ELF64
2450 #define ELFCLASS ELFCLASS64
2451 #define Elf_Addr Elf64_Addr
2452 #define Elf_Word Elf64_Word
2453 #define Elf_Sword Elf64_Sword
2454 #define Elf_Ehdr Elf64_Ehdr
2455 #define Elf_Phdr Elf64_Phdr
2456 #define Elf_Shdr Elf64_Shdr
2457 #define Elf_Sym Elf64_Sym
2458 #define Elf_Rel Elf64_Rel
2459 #define Elf_Rela Elf64_Rela
2460 #define ELF_ST_TYPE ELF64_ST_TYPE
2461 #define ELF_ST_BIND ELF64_ST_BIND
2462 #define ELF_R_TYPE ELF64_R_TYPE
2463 #define ELF_R_SYM ELF64_R_SYM
2465 #define ELFCLASS ELFCLASS32
2466 #define Elf_Addr Elf32_Addr
2467 #define Elf_Word Elf32_Word
2468 #define Elf_Sword Elf32_Sword
2469 #define Elf_Ehdr Elf32_Ehdr
2470 #define Elf_Phdr Elf32_Phdr
2471 #define Elf_Shdr Elf32_Shdr
2472 #define Elf_Sym Elf32_Sym
2473 #define Elf_Rel Elf32_Rel
2474 #define Elf_Rela Elf32_Rela
2476 #define ELF_ST_TYPE ELF32_ST_TYPE
2479 #define ELF_ST_BIND ELF32_ST_BIND
2482 #define ELF_R_TYPE ELF32_R_TYPE
2485 #define ELF_R_SYM ELF32_R_SYM
2491 * Functions to allocate entries in dynamic sections. Currently we simply
2492 * preallocate a large number, and we don't check if a entry for the given
2493 * target already exists (a linear search is too slow). Ideally these
2494 * entries would be associated with symbols.
2497 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2498 #define GOT_SIZE 0x20000
2499 #define FUNCTION_TABLE_SIZE 0x10000
2500 #define PLT_SIZE 0x08000
2503 static Elf_Addr got[GOT_SIZE];
2504 static unsigned int gotIndex;
2505 static Elf_Addr gp_val = (Elf_Addr)got;
2508 allocateGOTEntry(Elf_Addr target)
2512 if (gotIndex >= GOT_SIZE)
2513 barf("Global offset table overflow");
2515 entry = &got[gotIndex++];
2517 return (Elf_Addr)entry;
2521 #ifdef ELF_FUNCTION_DESC
2527 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2528 static unsigned int functionTableIndex;
2531 allocateFunctionDesc(Elf_Addr target)
2533 FunctionDesc *entry;
2535 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2536 barf("Function table overflow");
2538 entry = &functionTable[functionTableIndex++];
2540 entry->gp = (Elf_Addr)gp_val;
2541 return (Elf_Addr)entry;
2545 copyFunctionDesc(Elf_Addr target)
2547 FunctionDesc *olddesc = (FunctionDesc *)target;
2548 FunctionDesc *newdesc;
2550 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2551 newdesc->gp = olddesc->gp;
2552 return (Elf_Addr)newdesc;
2557 #ifdef ia64_HOST_ARCH
2558 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2559 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2561 static unsigned char plt_code[] =
2563 /* taken from binutils bfd/elfxx-ia64.c */
2564 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2565 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2566 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2567 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2568 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2569 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2572 /* If we can't get to the function descriptor via gp, take a local copy of it */
2573 #define PLT_RELOC(code, target) { \
2574 Elf64_Sxword rel_value = target - gp_val; \
2575 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2576 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2578 ia64_reloc_gprel22((Elf_Addr)code, target); \
2583 unsigned char code[sizeof(plt_code)];
2587 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2589 PLTEntry *plt = (PLTEntry *)oc->plt;
2592 if (oc->pltIndex >= PLT_SIZE)
2593 barf("Procedure table overflow");
2595 entry = &plt[oc->pltIndex++];
2596 memcpy(entry->code, plt_code, sizeof(entry->code));
2597 PLT_RELOC(entry->code, target);
2598 return (Elf_Addr)entry;
2604 return (PLT_SIZE * sizeof(PLTEntry));
2609 #if x86_64_HOST_ARCH
2610 // On x86_64, 32-bit relocations are often used, which requires that
2611 // we can resolve a symbol to a 32-bit offset. However, shared
2612 // libraries are placed outside the 2Gb area, which leaves us with a
2613 // problem when we need to give a 32-bit offset to a symbol in a
2616 // For a function symbol, we can allocate a bounce sequence inside the
2617 // 2Gb area and resolve the symbol to this. The bounce sequence is
2618 // simply a long jump instruction to the real location of the symbol.
2620 // For data references, we're screwed.
2623 unsigned char jmp[8]; /* 6 byte instruction: jmpq *0x00000002(%rip) */
2627 #define X86_64_BB_SIZE 1024
2629 static x86_64_bounce *x86_64_bounce_buffer = NULL;
2630 static nat x86_64_bb_next_off;
2633 x86_64_high_symbol( char *lbl, void *addr )
2635 x86_64_bounce *bounce;
2637 if ( x86_64_bounce_buffer == NULL ||
2638 x86_64_bb_next_off >= X86_64_BB_SIZE ) {
2639 x86_64_bounce_buffer =
2640 mmap(NULL, X86_64_BB_SIZE * sizeof(x86_64_bounce),
2641 PROT_EXEC|PROT_READ|PROT_WRITE,
2642 MAP_PRIVATE|MAP_32BIT|MAP_ANONYMOUS, -1, 0);
2643 if (x86_64_bounce_buffer == MAP_FAILED) {
2644 barf("x86_64_high_symbol: mmap failed");
2646 x86_64_bb_next_off = 0;
2648 bounce = &x86_64_bounce_buffer[x86_64_bb_next_off];
2649 bounce->jmp[0] = 0xff;
2650 bounce->jmp[1] = 0x25;
2651 bounce->jmp[2] = 0x02;
2652 bounce->jmp[3] = 0x00;
2653 bounce->jmp[4] = 0x00;
2654 bounce->jmp[5] = 0x00;
2655 bounce->addr = addr;
2656 x86_64_bb_next_off++;
2658 IF_DEBUG(linker, debugBelch("x86_64: allocated bounce entry for %s->%p at %p\n",
2659 lbl, addr, bounce));
2661 insertStrHashTable(symhash, lbl, bounce);
2668 * Generic ELF functions
2672 findElfSection ( void* objImage, Elf_Word sh_type )
2674 char* ehdrC = (char*)objImage;
2675 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2676 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2677 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2681 for (i = 0; i < ehdr->e_shnum; i++) {
2682 if (shdr[i].sh_type == sh_type
2683 /* Ignore the section header's string table. */
2684 && i != ehdr->e_shstrndx
2685 /* Ignore string tables named .stabstr, as they contain
2687 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2689 ptr = ehdrC + shdr[i].sh_offset;
2696 #if defined(ia64_HOST_ARCH)
2698 findElfSegment ( void* objImage, Elf_Addr vaddr )
2700 char* ehdrC = (char*)objImage;
2701 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2702 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2703 Elf_Addr segaddr = 0;
2706 for (i = 0; i < ehdr->e_phnum; i++) {
2707 segaddr = phdr[i].p_vaddr;
2708 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2716 ocVerifyImage_ELF ( ObjectCode* oc )
2720 int i, j, nent, nstrtab, nsymtabs;
2724 char* ehdrC = (char*)(oc->image);
2725 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2727 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2728 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2729 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2730 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2731 errorBelch("%s: not an ELF object", oc->fileName);
2735 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2736 errorBelch("%s: unsupported ELF format", oc->fileName);
2740 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2741 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
2743 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2744 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
2746 errorBelch("%s: unknown endiannness", oc->fileName);
2750 if (ehdr->e_type != ET_REL) {
2751 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
2754 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
2756 IF_DEBUG(linker,debugBelch( "Architecture is " ));
2757 switch (ehdr->e_machine) {
2758 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
2759 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
2761 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
2763 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
2765 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
2767 default: IF_DEBUG(linker,debugBelch( "unknown" ));
2768 errorBelch("%s: unknown architecture", oc->fileName);
2772 IF_DEBUG(linker,debugBelch(
2773 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
2774 (long)ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2776 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2778 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2780 if (ehdr->e_shstrndx == SHN_UNDEF) {
2781 errorBelch("%s: no section header string table", oc->fileName);
2784 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
2786 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2789 for (i = 0; i < ehdr->e_shnum; i++) {
2790 IF_DEBUG(linker,debugBelch("%2d: ", i ));
2791 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
2792 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
2793 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
2794 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
2795 ehdrC + shdr[i].sh_offset,
2796 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2798 if (shdr[i].sh_type == SHT_REL) {
2799 IF_DEBUG(linker,debugBelch("Rel " ));
2800 } else if (shdr[i].sh_type == SHT_RELA) {
2801 IF_DEBUG(linker,debugBelch("RelA " ));
2803 IF_DEBUG(linker,debugBelch(" "));
2806 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
2810 IF_DEBUG(linker,debugBelch( "\nString tables" ));
2813 for (i = 0; i < ehdr->e_shnum; i++) {
2814 if (shdr[i].sh_type == SHT_STRTAB
2815 /* Ignore the section header's string table. */
2816 && i != ehdr->e_shstrndx
2817 /* Ignore string tables named .stabstr, as they contain
2819 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2821 IF_DEBUG(linker,debugBelch(" section %d is a normal string table", i ));
2822 strtab = ehdrC + shdr[i].sh_offset;
2827 errorBelch("%s: no string tables, or too many", oc->fileName);
2832 IF_DEBUG(linker,debugBelch( "\nSymbol tables" ));
2833 for (i = 0; i < ehdr->e_shnum; i++) {
2834 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2835 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
2837 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2838 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2839 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
2841 (long)shdr[i].sh_size % sizeof(Elf_Sym)
2843 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2844 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
2847 for (j = 0; j < nent; j++) {
2848 IF_DEBUG(linker,debugBelch(" %2d ", j ));
2849 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
2850 (int)stab[j].st_shndx,
2851 (int)stab[j].st_size,
2852 (char*)stab[j].st_value ));
2854 IF_DEBUG(linker,debugBelch("type=" ));
2855 switch (ELF_ST_TYPE(stab[j].st_info)) {
2856 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
2857 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
2858 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
2859 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
2860 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
2861 default: IF_DEBUG(linker,debugBelch("? " )); break;
2863 IF_DEBUG(linker,debugBelch(" " ));
2865 IF_DEBUG(linker,debugBelch("bind=" ));
2866 switch (ELF_ST_BIND(stab[j].st_info)) {
2867 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
2868 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
2869 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
2870 default: IF_DEBUG(linker,debugBelch("? " )); break;
2872 IF_DEBUG(linker,debugBelch(" " ));
2874 IF_DEBUG(linker,debugBelch("name=%s\n", strtab + stab[j].st_name ));
2878 if (nsymtabs == 0) {
2879 errorBelch("%s: didn't find any symbol tables", oc->fileName);
2886 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
2890 if (hdr->sh_type == SHT_PROGBITS
2891 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
2892 /* .text-style section */
2893 return SECTIONKIND_CODE_OR_RODATA;
2896 if (hdr->sh_type == SHT_PROGBITS
2897 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
2898 /* .data-style section */
2899 return SECTIONKIND_RWDATA;
2902 if (hdr->sh_type == SHT_PROGBITS
2903 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
2904 /* .rodata-style section */
2905 return SECTIONKIND_CODE_OR_RODATA;
2908 if (hdr->sh_type == SHT_NOBITS
2909 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
2910 /* .bss-style section */
2912 return SECTIONKIND_RWDATA;
2915 return SECTIONKIND_OTHER;
2920 ocGetNames_ELF ( ObjectCode* oc )
2925 char* ehdrC = (char*)(oc->image);
2926 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2927 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2928 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2930 ASSERT(symhash != NULL);
2933 errorBelch("%s: no strtab", oc->fileName);
2938 for (i = 0; i < ehdr->e_shnum; i++) {
2939 /* Figure out what kind of section it is. Logic derived from
2940 Figure 1.14 ("Special Sections") of the ELF document
2941 ("Portable Formats Specification, Version 1.1"). */
2943 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
2945 if (is_bss && shdr[i].sh_size > 0) {
2946 /* This is a non-empty .bss section. Allocate zeroed space for
2947 it, and set its .sh_offset field such that
2948 ehdrC + .sh_offset == addr_of_zeroed_space. */
2949 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2950 "ocGetNames_ELF(BSS)");
2951 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2953 debugBelch("BSS section at 0x%x, size %d\n",
2954 zspace, shdr[i].sh_size);
2958 /* fill in the section info */
2959 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2960 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2961 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2962 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2965 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2967 /* copy stuff into this module's object symbol table */
2968 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2969 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2971 oc->n_symbols = nent;
2972 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2973 "ocGetNames_ELF(oc->symbols)");
2975 for (j = 0; j < nent; j++) {
2977 char isLocal = FALSE; /* avoids uninit-var warning */
2979 char* nm = strtab + stab[j].st_name;
2980 int secno = stab[j].st_shndx;
2982 /* Figure out if we want to add it; if so, set ad to its
2983 address. Otherwise leave ad == NULL. */
2985 if (secno == SHN_COMMON) {
2987 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2989 debugBelch("COMMON symbol, size %d name %s\n",
2990 stab[j].st_size, nm);
2992 /* Pointless to do addProddableBlock() for this area,
2993 since the linker should never poke around in it. */
2996 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2997 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2999 /* and not an undefined symbol */
3000 && stab[j].st_shndx != SHN_UNDEF
3001 /* and not in a "special section" */
3002 && stab[j].st_shndx < SHN_LORESERVE
3004 /* and it's a not a section or string table or anything silly */
3005 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
3006 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
3007 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
3010 /* Section 0 is the undefined section, hence > and not >=. */
3011 ASSERT(secno > 0 && secno < ehdr->e_shnum);
3013 if (shdr[secno].sh_type == SHT_NOBITS) {
3014 debugBelch(" BSS symbol, size %d off %d name %s\n",
3015 stab[j].st_size, stab[j].st_value, nm);
3018 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
3019 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
3022 #ifdef ELF_FUNCTION_DESC
3023 /* dlsym() and the initialisation table both give us function
3024 * descriptors, so to be consistent we store function descriptors
3025 * in the symbol table */
3026 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
3027 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
3029 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s",
3030 ad, oc->fileName, nm ));
3035 /* And the decision is ... */
3039 oc->symbols[j] = nm;
3042 /* Ignore entirely. */
3044 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
3048 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
3049 strtab + stab[j].st_name ));
3052 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
3053 (int)ELF_ST_BIND(stab[j].st_info),
3054 (int)ELF_ST_TYPE(stab[j].st_info),
3055 (int)stab[j].st_shndx,
3056 strtab + stab[j].st_name
3059 oc->symbols[j] = NULL;
3068 /* Do ELF relocations which lack an explicit addend. All x86-linux
3069 relocations appear to be of this form. */
3071 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
3072 Elf_Shdr* shdr, int shnum,
3073 Elf_Sym* stab, char* strtab )
3078 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
3079 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
3080 int target_shndx = shdr[shnum].sh_info;
3081 int symtab_shndx = shdr[shnum].sh_link;
3083 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3084 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
3085 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3086 target_shndx, symtab_shndx ));
3088 /* Skip sections that we're not interested in. */
3091 SectionKind kind = getSectionKind_ELF(&shdr[target_shndx], &is_bss);
3092 if (kind == SECTIONKIND_OTHER) {
3093 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
3098 for (j = 0; j < nent; j++) {
3099 Elf_Addr offset = rtab[j].r_offset;
3100 Elf_Addr info = rtab[j].r_info;
3102 Elf_Addr P = ((Elf_Addr)targ) + offset;
3103 Elf_Word* pP = (Elf_Word*)P;
3109 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p)",
3110 j, (void*)offset, (void*)info ));
3112 IF_DEBUG(linker,debugBelch( " ZERO" ));
3115 Elf_Sym sym = stab[ELF_R_SYM(info)];
3116 /* First see if it is a local symbol. */
3117 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3118 /* Yes, so we can get the address directly from the ELF symbol
3120 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3122 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3123 + stab[ELF_R_SYM(info)].st_value);
3126 /* No, so look up the name in our global table. */
3127 symbol = strtab + sym.st_name;
3128 S_tmp = lookupSymbol( symbol );
3129 S = (Elf_Addr)S_tmp;
3132 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3135 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
3138 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p\n",
3139 (void*)P, (void*)S, (void*)A ));
3140 checkProddableBlock ( oc, pP );
3144 switch (ELF_R_TYPE(info)) {
3145 # ifdef i386_HOST_ARCH
3146 case R_386_32: *pP = value; break;
3147 case R_386_PC32: *pP = value - P; break;
3150 errorBelch("%s: unhandled ELF relocation(Rel) type %lu\n",
3151 oc->fileName, (lnat)ELF_R_TYPE(info));
3159 /* Do ELF relocations for which explicit addends are supplied.
3160 sparc-solaris relocations appear to be of this form. */
3162 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
3163 Elf_Shdr* shdr, int shnum,
3164 Elf_Sym* stab, char* strtab )
3167 char *symbol = NULL;
3169 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
3170 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
3171 int target_shndx = shdr[shnum].sh_info;
3172 int symtab_shndx = shdr[shnum].sh_link;
3174 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
3175 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
3176 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
3177 target_shndx, symtab_shndx ));
3179 for (j = 0; j < nent; j++) {
3180 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(ia64_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
3181 /* This #ifdef only serves to avoid unused-var warnings. */
3182 Elf_Addr offset = rtab[j].r_offset;
3183 Elf_Addr P = targ + offset;
3185 Elf_Addr info = rtab[j].r_info;
3186 Elf_Addr A = rtab[j].r_addend;
3190 # if defined(sparc_HOST_ARCH)
3191 Elf_Word* pP = (Elf_Word*)P;
3193 # elif defined(ia64_HOST_ARCH)
3194 Elf64_Xword *pP = (Elf64_Xword *)P;
3196 # elif defined(powerpc_HOST_ARCH)
3200 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
3201 j, (void*)offset, (void*)info,
3204 IF_DEBUG(linker,debugBelch( " ZERO" ));
3207 Elf_Sym sym = stab[ELF_R_SYM(info)];
3208 /* First see if it is a local symbol. */
3209 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
3210 /* Yes, so we can get the address directly from the ELF symbol
3212 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
3214 (ehdrC + shdr[ sym.st_shndx ].sh_offset
3215 + stab[ELF_R_SYM(info)].st_value);
3216 #ifdef ELF_FUNCTION_DESC
3217 /* Make a function descriptor for this function */
3218 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
3219 S = allocateFunctionDesc(S + A);
3224 /* No, so look up the name in our global table. */
3225 symbol = strtab + sym.st_name;
3226 S_tmp = lookupSymbol( symbol );
3227 S = (Elf_Addr)S_tmp;
3229 #ifdef ELF_FUNCTION_DESC
3230 /* If a function, already a function descriptor - we would
3231 have to copy it to add an offset. */
3232 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
3233 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
3237 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
3240 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p", symbol, (void*)S ));
3243 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
3244 (void*)P, (void*)S, (void*)A ));
3245 /* checkProddableBlock ( oc, (void*)P ); */
3249 switch (ELF_R_TYPE(info)) {
3250 # if defined(sparc_HOST_ARCH)
3251 case R_SPARC_WDISP30:
3252 w1 = *pP & 0xC0000000;
3253 w2 = (Elf_Word)((value - P) >> 2);
3254 ASSERT((w2 & 0xC0000000) == 0);
3259 w1 = *pP & 0xFFC00000;
3260 w2 = (Elf_Word)(value >> 10);
3261 ASSERT((w2 & 0xFFC00000) == 0);
3267 w2 = (Elf_Word)(value & 0x3FF);
3268 ASSERT((w2 & ~0x3FF) == 0);
3272 /* According to the Sun documentation:
3274 This relocation type resembles R_SPARC_32, except it refers to an
3275 unaligned word. That is, the word to be relocated must be treated
3276 as four separate bytes with arbitrary alignment, not as a word
3277 aligned according to the architecture requirements.
3279 (JRS: which means that freeloading on the R_SPARC_32 case
3280 is probably wrong, but hey ...)
3284 w2 = (Elf_Word)value;
3287 # elif defined(ia64_HOST_ARCH)
3288 case R_IA64_DIR64LSB:
3289 case R_IA64_FPTR64LSB:
3292 case R_IA64_PCREL64LSB:
3295 case R_IA64_SEGREL64LSB:
3296 addr = findElfSegment(ehdrC, value);
3299 case R_IA64_GPREL22:
3300 ia64_reloc_gprel22(P, value);
3302 case R_IA64_LTOFF22:
3303 case R_IA64_LTOFF22X:
3304 case R_IA64_LTOFF_FPTR22:
3305 addr = allocateGOTEntry(value);
3306 ia64_reloc_gprel22(P, addr);
3308 case R_IA64_PCREL21B:
3309 ia64_reloc_pcrel21(P, S, oc);
3312 /* This goes with R_IA64_LTOFF22X and points to the load to
3313 * convert into a move. We don't implement relaxation. */
3315 # elif defined(powerpc_HOST_ARCH)
3316 case R_PPC_ADDR16_LO:
3317 *(Elf32_Half*) P = value;
3320 case R_PPC_ADDR16_HI:
3321 *(Elf32_Half*) P = value >> 16;
3324 case R_PPC_ADDR16_HA:
3325 *(Elf32_Half*) P = (value + 0x8000) >> 16;
3329 *(Elf32_Word *) P = value;
3333 *(Elf32_Word *) P = value - P;
3339 if( delta << 6 >> 6 != delta )
3341 value = makeJumpIsland( oc, ELF_R_SYM(info), value );
3344 if( value == 0 || delta << 6 >> 6 != delta )
3346 barf( "Unable to make ppcJumpIsland for #%d",
3352 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
3353 | (delta & 0x3fffffc);
3357 #if x86_64_HOST_ARCH
3359 *(Elf64_Xword *)P = value;
3364 StgInt64 off = value - P;
3365 if (off >= 0x7fffffffL || off < -0x80000000L) {
3366 barf("R_X86_64_PC32 relocation out of range: %s = %p",
3369 *(Elf64_Word *)P = (Elf64_Word)off;
3374 if (value >= 0x7fffffffL) {
3375 barf("R_X86_64_32 relocation out of range: %s = %p\n",
3378 *(Elf64_Word *)P = (Elf64_Word)value;
3382 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
3383 barf("R_X86_64_32S relocation out of range: %s = %p\n",
3386 *(Elf64_Sword *)P = (Elf64_Sword)value;
3391 errorBelch("%s: unhandled ELF relocation(RelA) type %lu\n",
3392 oc->fileName, (lnat)ELF_R_TYPE(info));
3401 ocResolve_ELF ( ObjectCode* oc )
3405 Elf_Sym* stab = NULL;
3406 char* ehdrC = (char*)(oc->image);
3407 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
3408 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
3410 /* first find "the" symbol table */
3411 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
3413 /* also go find the string table */
3414 strtab = findElfSection ( ehdrC, SHT_STRTAB );
3416 if (stab == NULL || strtab == NULL) {
3417 errorBelch("%s: can't find string or symbol table", oc->fileName);
3421 /* Process the relocation sections. */
3422 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
3423 if (shdr[shnum].sh_type == SHT_REL) {
3424 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
3425 shnum, stab, strtab );
3429 if (shdr[shnum].sh_type == SHT_RELA) {
3430 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
3431 shnum, stab, strtab );
3436 /* Free the local symbol table; we won't need it again. */
3437 freeHashTable(oc->lochash, NULL);
3440 #if defined(powerpc_HOST_ARCH)
3441 ocFlushInstructionCache( oc );
3449 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
3450 * at the front. The following utility functions pack and unpack instructions, and
3451 * take care of the most common relocations.
3454 #ifdef ia64_HOST_ARCH
3457 ia64_extract_instruction(Elf64_Xword *target)
3460 int slot = (Elf_Addr)target & 3;
3461 target = (Elf_Addr)target & ~3;
3469 return ((w1 >> 5) & 0x1ffffffffff);
3471 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
3475 barf("ia64_extract_instruction: invalid slot %p", target);
3480 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
3482 int slot = (Elf_Addr)target & 3;
3483 target = (Elf_Addr)target & ~3;
3488 *target |= value << 5;
3491 *target |= value << 46;
3492 *(target+1) |= value >> 18;
3495 *(target+1) |= value << 23;
3501 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
3503 Elf64_Xword instruction;
3504 Elf64_Sxword rel_value;
3506 rel_value = value - gp_val;
3507 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
3508 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
3510 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3511 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
3512 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
3513 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
3514 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3515 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3519 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
3521 Elf64_Xword instruction;
3522 Elf64_Sxword rel_value;
3525 entry = allocatePLTEntry(value, oc);
3527 rel_value = (entry >> 4) - (target >> 4);
3528 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
3529 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
3531 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3532 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
3533 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3534 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3540 * PowerPC ELF specifics
3543 #ifdef powerpc_HOST_ARCH
3545 static int ocAllocateJumpIslands_ELF( ObjectCode *oc )
3551 ehdr = (Elf_Ehdr *) oc->image;
3552 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
3554 for( i = 0; i < ehdr->e_shnum; i++ )
3555 if( shdr[i].sh_type == SHT_SYMTAB )
3558 if( i == ehdr->e_shnum )
3560 errorBelch( "This ELF file contains no symtab" );
3564 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
3566 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
3567 shdr[i].sh_entsize, sizeof( Elf_Sym ) );
3572 return ocAllocateJumpIslands( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
3575 #endif /* powerpc */
3579 /* --------------------------------------------------------------------------
3581 * ------------------------------------------------------------------------*/
3583 #if defined(OBJFORMAT_MACHO)
3586 Support for MachO linking on Darwin/MacOS X
3587 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3589 I hereby formally apologize for the hackish nature of this code.
3590 Things that need to be done:
3591 *) implement ocVerifyImage_MachO
3592 *) add still more sanity checks.
3595 #ifdef powerpc_HOST_ARCH
3596 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3598 struct mach_header *header = (struct mach_header *) oc->image;
3599 struct load_command *lc = (struct load_command *) (header + 1);
3602 for( i = 0; i < header->ncmds; i++ )
3604 if( lc->cmd == LC_SYMTAB )
3606 // Find out the first and last undefined external
3607 // symbol, so we don't have to allocate too many
3609 struct symtab_command *symLC = (struct symtab_command *) lc;
3610 unsigned min = symLC->nsyms, max = 0;
3611 struct nlist *nlist =
3612 symLC ? (struct nlist*) ((char*) oc->image + symLC->symoff)
3614 for(i=0;i<symLC->nsyms;i++)
3616 if(nlist[i].n_type & N_STAB)
3618 else if(nlist[i].n_type & N_EXT)
3620 if((nlist[i].n_type & N_TYPE) == N_UNDF
3621 && (nlist[i].n_value == 0))
3631 return ocAllocateJumpIslands(oc, max - min + 1, min);
3636 lc = (struct load_command *) ( ((char *)lc) + lc->cmdsize );
3638 return ocAllocateJumpIslands(oc,0,0);
3642 static int ocVerifyImage_MachO(ObjectCode* oc STG_UNUSED)
3644 // FIXME: do some verifying here
3648 static int resolveImports(
3651 struct symtab_command *symLC,
3652 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3653 unsigned long *indirectSyms,
3654 struct nlist *nlist)
3658 for(i=0;i*4<sect->size;i++)
3660 // according to otool, reserved1 contains the first index into the indirect symbol table
3661 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3662 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3665 if((symbol->n_type & N_TYPE) == N_UNDF
3666 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3667 addr = (void*) (symbol->n_value);
3668 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3671 addr = lookupSymbol(nm);
3674 errorBelch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3678 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3679 ((void**)(image + sect->offset))[i] = addr;
3685 static unsigned long relocateAddress(
3688 struct section* sections,
3689 unsigned long address)
3692 for(i = 0; i < nSections; i++)
3694 if(sections[i].addr <= address
3695 && address < sections[i].addr + sections[i].size)
3697 return (unsigned long)oc->image
3698 + sections[i].offset + address - sections[i].addr;
3701 barf("Invalid Mach-O file:"
3702 "Address out of bounds while relocating object file");
3706 static int relocateSection(
3709 struct symtab_command *symLC, struct nlist *nlist,
3710 int nSections, struct section* sections, struct section *sect)
3712 struct relocation_info *relocs;
3715 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3717 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3719 else if(!strcmp(sect->sectname,"__la_sym_ptr2"))
3721 else if(!strcmp(sect->sectname,"__la_sym_ptr3"))
3725 relocs = (struct relocation_info*) (image + sect->reloff);
3729 if(relocs[i].r_address & R_SCATTERED)
3731 struct scattered_relocation_info *scat =
3732 (struct scattered_relocation_info*) &relocs[i];
3736 if(scat->r_length == 2)
3738 unsigned long word = 0;
3739 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3740 checkProddableBlock(oc,wordPtr);
3742 // Note on relocation types:
3743 // i386 uses the GENERIC_RELOC_* types,
3744 // while ppc uses special PPC_RELOC_* types.
3745 // *_RELOC_VANILLA and *_RELOC_PAIR have the same value
3746 // in both cases, all others are different.
3747 // Therefore, we use GENERIC_RELOC_VANILLA
3748 // and GENERIC_RELOC_PAIR instead of the PPC variants,
3749 // and use #ifdefs for the other types.
3751 // Step 1: Figure out what the relocated value should be
3752 if(scat->r_type == GENERIC_RELOC_VANILLA)
3754 word = *wordPtr + (unsigned long) relocateAddress(
3761 #ifdef powerpc_HOST_ARCH
3762 else if(scat->r_type == PPC_RELOC_SECTDIFF
3763 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3764 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3765 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3767 else if(scat->r_type == GENERIC_RELOC_SECTDIFF)
3770 struct scattered_relocation_info *pair =
3771 (struct scattered_relocation_info*) &relocs[i+1];
3773 if(!pair->r_scattered || pair->r_type != GENERIC_RELOC_PAIR)
3774 barf("Invalid Mach-O file: "
3775 "RELOC_*_SECTDIFF not followed by RELOC_PAIR");
3777 word = (unsigned long)
3778 (relocateAddress(oc, nSections, sections, scat->r_value)
3779 - relocateAddress(oc, nSections, sections, pair->r_value));
3782 #ifdef powerpc_HOST_ARCH
3783 else if(scat->r_type == PPC_RELOC_HI16
3784 || scat->r_type == PPC_RELOC_LO16
3785 || scat->r_type == PPC_RELOC_HA16
3786 || scat->r_type == PPC_RELOC_LO14)
3787 { // these are generated by label+offset things
3788 struct relocation_info *pair = &relocs[i+1];
3789 if((pair->r_address & R_SCATTERED) || pair->r_type != PPC_RELOC_PAIR)
3790 barf("Invalid Mach-O file: "
3791 "PPC_RELOC_* not followed by PPC_RELOC_PAIR");
3793 if(scat->r_type == PPC_RELOC_LO16)
3795 word = ((unsigned short*) wordPtr)[1];
3796 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3798 else if(scat->r_type == PPC_RELOC_LO14)
3800 barf("Unsupported Relocation: PPC_RELOC_LO14");
3801 word = ((unsigned short*) wordPtr)[1] & 0xFFFC;
3802 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3804 else if(scat->r_type == PPC_RELOC_HI16)
3806 word = ((unsigned short*) wordPtr)[1] << 16;
3807 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3809 else if(scat->r_type == PPC_RELOC_HA16)
3811 word = ((unsigned short*) wordPtr)[1] << 16;
3812 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3816 word += (unsigned long) relocateAddress(oc, nSections, sections, scat->r_value)
3823 continue; // ignore the others
3825 #ifdef powerpc_HOST_ARCH
3826 if(scat->r_type == GENERIC_RELOC_VANILLA
3827 || scat->r_type == PPC_RELOC_SECTDIFF)
3829 if(scat->r_type == GENERIC_RELOC_VANILLA
3830 || scat->r_type == GENERIC_RELOC_SECTDIFF)
3835 #ifdef powerpc_HOST_ARCH
3836 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF || scat->r_type == PPC_RELOC_LO16)
3838 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3840 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF || scat->r_type == PPC_RELOC_HI16)
3842 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3844 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF || scat->r_type == PPC_RELOC_HA16)
3846 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3847 + ((word & (1<<15)) ? 1 : 0);
3853 continue; // FIXME: I hope it's OK to ignore all the others.
3857 struct relocation_info *reloc = &relocs[i];
3858 if(reloc->r_pcrel && !reloc->r_extern)
3861 if(reloc->r_length == 2)
3863 unsigned long word = 0;
3864 #ifdef powerpc_HOST_ARCH
3865 unsigned long jumpIsland = 0;
3866 long offsetToJumpIsland = 0xBADBAD42; // initialise to bad value
3867 // to avoid warning and to catch
3871 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3872 checkProddableBlock(oc,wordPtr);
3874 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3878 #ifdef powerpc_HOST_ARCH
3879 else if(reloc->r_type == PPC_RELOC_LO16)
3881 word = ((unsigned short*) wordPtr)[1];
3882 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3884 else if(reloc->r_type == PPC_RELOC_HI16)
3886 word = ((unsigned short*) wordPtr)[1] << 16;
3887 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3889 else if(reloc->r_type == PPC_RELOC_HA16)
3891 word = ((unsigned short*) wordPtr)[1] << 16;
3892 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3894 else if(reloc->r_type == PPC_RELOC_BR24)
3897 word = (word & 0x03FFFFFC) | ((word & 0x02000000) ? 0xFC000000 : 0);
3901 if(!reloc->r_extern)
3904 sections[reloc->r_symbolnum-1].offset
3905 - sections[reloc->r_symbolnum-1].addr
3912 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3913 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3914 void *symbolAddress = lookupSymbol(nm);
3917 errorBelch("\nunknown symbol `%s'", nm);
3923 #ifdef powerpc_HOST_ARCH
3924 // In the .o file, this should be a relative jump to NULL
3925 // and we'll change it to a relative jump to the symbol
3926 ASSERT(-word == reloc->r_address);
3927 jumpIsland = makeJumpIsland(oc,reloc->r_symbolnum,(unsigned long) symbolAddress);
3930 offsetToJumpIsland = word + jumpIsland
3931 - (((long)image) + sect->offset - sect->addr);
3934 word += (unsigned long) symbolAddress
3935 - (((long)image) + sect->offset - sect->addr);
3939 word += (unsigned long) symbolAddress;
3943 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3948 #ifdef powerpc_HOST_ARCH
3949 else if(reloc->r_type == PPC_RELOC_LO16)
3951 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3954 else if(reloc->r_type == PPC_RELOC_HI16)
3956 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3959 else if(reloc->r_type == PPC_RELOC_HA16)
3961 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3962 + ((word & (1<<15)) ? 1 : 0);
3965 else if(reloc->r_type == PPC_RELOC_BR24)
3967 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3969 // The branch offset is too large.
3970 // Therefore, we try to use a jump island.
3973 barf("unconditional relative branch out of range: "
3974 "no jump island available");
3977 word = offsetToJumpIsland;
3978 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3979 barf("unconditional relative branch out of range: "
3980 "jump island out of range");
3982 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3987 barf("\nunknown relocation %d",reloc->r_type);
3994 static int ocGetNames_MachO(ObjectCode* oc)
3996 char *image = (char*) oc->image;
3997 struct mach_header *header = (struct mach_header*) image;
3998 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3999 unsigned i,curSymbol = 0;
4000 struct segment_command *segLC = NULL;
4001 struct section *sections;
4002 struct symtab_command *symLC = NULL;
4003 struct nlist *nlist;
4004 unsigned long commonSize = 0;
4005 char *commonStorage = NULL;
4006 unsigned long commonCounter;
4008 for(i=0;i<header->ncmds;i++)
4010 if(lc->cmd == LC_SEGMENT)
4011 segLC = (struct segment_command*) lc;
4012 else if(lc->cmd == LC_SYMTAB)
4013 symLC = (struct symtab_command*) lc;
4014 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4017 sections = (struct section*) (segLC+1);
4018 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4021 for(i=0;i<segLC->nsects;i++)
4023 if(sections[i].size == 0)
4026 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
4028 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
4029 "ocGetNames_MachO(common symbols)");
4030 sections[i].offset = zeroFillArea - image;
4033 if(!strcmp(sections[i].sectname,"__text"))
4034 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
4035 (void*) (image + sections[i].offset),
4036 (void*) (image + sections[i].offset + sections[i].size));
4037 else if(!strcmp(sections[i].sectname,"__const"))
4038 addSection(oc, SECTIONKIND_RWDATA,
4039 (void*) (image + sections[i].offset),
4040 (void*) (image + sections[i].offset + sections[i].size));
4041 else if(!strcmp(sections[i].sectname,"__data"))
4042 addSection(oc, SECTIONKIND_RWDATA,
4043 (void*) (image + sections[i].offset),
4044 (void*) (image + sections[i].offset + sections[i].size));
4045 else if(!strcmp(sections[i].sectname,"__bss")
4046 || !strcmp(sections[i].sectname,"__common"))
4047 addSection(oc, SECTIONKIND_RWDATA,
4048 (void*) (image + sections[i].offset),
4049 (void*) (image + sections[i].offset + sections[i].size));
4051 addProddableBlock(oc, (void*) (image + sections[i].offset),
4055 // count external symbols defined here
4059 for(i=0;i<symLC->nsyms;i++)
4061 if(nlist[i].n_type & N_STAB)
4063 else if(nlist[i].n_type & N_EXT)
4065 if((nlist[i].n_type & N_TYPE) == N_UNDF
4066 && (nlist[i].n_value != 0))
4068 commonSize += nlist[i].n_value;
4071 else if((nlist[i].n_type & N_TYPE) == N_SECT)
4076 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
4077 "ocGetNames_MachO(oc->symbols)");
4081 for(i=0;i<symLC->nsyms;i++)
4083 if(nlist[i].n_type & N_STAB)
4085 else if((nlist[i].n_type & N_TYPE) == N_SECT)
4087 if(nlist[i].n_type & N_EXT)
4089 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4090 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4092 + sections[nlist[i].n_sect-1].offset
4093 - sections[nlist[i].n_sect-1].addr
4094 + nlist[i].n_value);
4095 oc->symbols[curSymbol++] = nm;
4099 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4100 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm,
4102 + sections[nlist[i].n_sect-1].offset
4103 - sections[nlist[i].n_sect-1].addr
4104 + nlist[i].n_value);
4110 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
4111 commonCounter = (unsigned long)commonStorage;
4114 for(i=0;i<symLC->nsyms;i++)
4116 if((nlist[i].n_type & N_TYPE) == N_UNDF
4117 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
4119 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
4120 unsigned long sz = nlist[i].n_value;
4122 nlist[i].n_value = commonCounter;
4124 ghciInsertStrHashTable(oc->fileName, symhash, nm,
4125 (void*)commonCounter);
4126 oc->symbols[curSymbol++] = nm;
4128 commonCounter += sz;
4135 static int ocResolve_MachO(ObjectCode* oc)
4137 char *image = (char*) oc->image;
4138 struct mach_header *header = (struct mach_header*) image;
4139 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
4141 struct segment_command *segLC = NULL;
4142 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
4143 struct symtab_command *symLC = NULL;
4144 struct dysymtab_command *dsymLC = NULL;
4145 struct nlist *nlist;
4147 for(i=0;i<header->ncmds;i++)
4149 if(lc->cmd == LC_SEGMENT)
4150 segLC = (struct segment_command*) lc;
4151 else if(lc->cmd == LC_SYMTAB)
4152 symLC = (struct symtab_command*) lc;
4153 else if(lc->cmd == LC_DYSYMTAB)
4154 dsymLC = (struct dysymtab_command*) lc;
4155 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
4158 sections = (struct section*) (segLC+1);
4159 nlist = symLC ? (struct nlist*) (image + symLC->symoff)
4162 for(i=0;i<segLC->nsects;i++)
4164 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
4165 la_ptrs = §ions[i];
4166 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
4167 nl_ptrs = §ions[i];
4168 else if(!strcmp(sections[i].sectname,"__la_sym_ptr2"))
4169 la_ptrs = §ions[i];
4170 else if(!strcmp(sections[i].sectname,"__la_sym_ptr3"))
4171 la_ptrs = §ions[i];
4176 unsigned long *indirectSyms
4177 = (unsigned long*) (image + dsymLC->indirectsymoff);
4180 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
4183 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
4187 for(i=0;i<segLC->nsects;i++)
4189 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
4193 /* Free the local symbol table; we won't need it again. */
4194 freeHashTable(oc->lochash, NULL);
4197 #if defined (powerpc_HOST_ARCH)
4198 ocFlushInstructionCache( oc );
4204 #ifdef powerpc_HOST_ARCH
4206 * The Mach-O object format uses leading underscores. But not everywhere.
4207 * There is a small number of runtime support functions defined in
4208 * libcc_dynamic.a whose name does not have a leading underscore.
4209 * As a consequence, we can't get their address from C code.
4210 * We have to use inline assembler just to take the address of a function.
4214 static void machoInitSymbolsWithoutUnderscore()
4216 extern void* symbolsWithoutUnderscore[];
4217 void **p = symbolsWithoutUnderscore;
4218 __asm__ volatile(".globl _symbolsWithoutUnderscore\n.data\n_symbolsWithoutUnderscore:");
4222 __asm__ volatile(".long " # x);
4224 RTS_MACHO_NOUNDERLINE_SYMBOLS
4226 __asm__ volatile(".text");
4230 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, *p++);
4232 RTS_MACHO_NOUNDERLINE_SYMBOLS
4239 * Figure out by how much to shift the entire Mach-O file in memory
4240 * when loading so that its single segment ends up 16-byte-aligned
4242 static int machoGetMisalignment( FILE * f )
4244 struct mach_header header;
4247 fread(&header, sizeof(header), 1, f);
4250 if(header.magic != MH_MAGIC)
4253 misalignment = (header.sizeofcmds + sizeof(header))
4256 return misalignment ? (16 - misalignment) : 0;