1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.144 2004/01/05 17:32:35 simonmar Exp $
4 * (c) The GHC Team, 2000-2003
8 * ---------------------------------------------------------------------------*/
11 #include "PosixSource.h"
14 // Linux needs _GNU_SOURCE to get RTLD_DEFAULT from <dlfcn.h>.
24 #include "LinkerInternals.h"
26 #include "StoragePriv.h"
29 #ifdef HAVE_SYS_TYPES_H
30 #include <sys/types.h>
36 #ifdef HAVE_SYS_STAT_H
40 #if defined(HAVE_FRAMEWORK_HASKELLSUPPORT)
41 #include <HaskellSupport/dlfcn.h>
42 #elif defined(HAVE_DLFCN_H)
46 #if defined(cygwin32_TARGET_OS)
51 #ifdef HAVE_SYS_TIME_H
55 #include <sys/fcntl.h>
56 #include <sys/termios.h>
57 #include <sys/utime.h>
58 #include <sys/utsname.h>
62 #if defined(ia64_TARGET_ARCH)
68 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS) || defined(netbsd_TARGET_OS) || defined(openbsd_TARGET_OS)
69 # define OBJFORMAT_ELF
70 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
71 # define OBJFORMAT_PEi386
74 #elif defined(darwin_TARGET_OS)
75 # include <mach-o/ppc/reloc.h>
76 # define OBJFORMAT_MACHO
77 # include <mach-o/loader.h>
78 # include <mach-o/nlist.h>
79 # include <mach-o/reloc.h>
80 # include <mach-o/dyld.h>
83 /* Hash table mapping symbol names to Symbol */
84 static /*Str*/HashTable *symhash;
86 /* List of currently loaded objects */
87 ObjectCode *objects = NULL; /* initially empty */
89 #if defined(OBJFORMAT_ELF)
90 static int ocVerifyImage_ELF ( ObjectCode* oc );
91 static int ocGetNames_ELF ( ObjectCode* oc );
92 static int ocResolve_ELF ( ObjectCode* oc );
93 #elif defined(OBJFORMAT_PEi386)
94 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
95 static int ocGetNames_PEi386 ( ObjectCode* oc );
96 static int ocResolve_PEi386 ( ObjectCode* oc );
97 #elif defined(OBJFORMAT_MACHO)
98 static int ocAllocateJumpIslands_MachO ( ObjectCode* oc );
99 static int ocVerifyImage_MachO ( ObjectCode* oc );
100 static int ocGetNames_MachO ( ObjectCode* oc );
101 static int ocResolve_MachO ( ObjectCode* oc );
103 static void machoInitSymbolsWithoutUnderscore( void );
106 /* -----------------------------------------------------------------------------
107 * Built-in symbols from the RTS
110 typedef struct _RtsSymbolVal {
117 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
119 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
120 SymX(makeStableNamezh_fast) \
121 SymX(finalizzeWeakzh_fast)
123 /* These are not available in GUM!!! -- HWL */
124 #define Maybe_ForeignObj
125 #define Maybe_Stable_Names
128 #if !defined (mingw32_TARGET_OS)
129 #define RTS_POSIX_ONLY_SYMBOLS \
130 SymX(stg_sig_install) \
134 #if defined (cygwin32_TARGET_OS)
135 #define RTS_MINGW_ONLY_SYMBOLS /**/
136 /* Don't have the ability to read import libs / archives, so
137 * we have to stupidly list a lot of what libcygwin.a
140 #define RTS_CYGWIN_ONLY_SYMBOLS \
218 #elif !defined(mingw32_TARGET_OS)
219 #define RTS_MINGW_ONLY_SYMBOLS /**/
220 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
221 #else /* defined(mingw32_TARGET_OS) */
222 #define RTS_POSIX_ONLY_SYMBOLS /**/
223 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
225 /* Extra syms gen'ed by mingw-2's gcc-3.2: */
227 #define RTS_MINGW_EXTRA_SYMS \
228 Sym(_imp____mb_cur_max) \
231 #define RTS_MINGW_EXTRA_SYMS
234 /* These are statically linked from the mingw libraries into the ghc
235 executable, so we have to employ this hack. */
236 #define RTS_MINGW_ONLY_SYMBOLS \
237 SymX(asyncReadzh_fast) \
238 SymX(asyncWritezh_fast) \
239 SymX(asyncDoProczh_fast) \
251 SymX(getservbyname) \
252 SymX(getservbyport) \
253 SymX(getprotobynumber) \
254 SymX(getprotobyname) \
255 SymX(gethostbyname) \
256 SymX(gethostbyaddr) \
291 Sym(_imp___timezone) \
299 RTS_MINGW_EXTRA_SYMS \
304 # define MAIN_CAP_SYM SymX(MainCapability)
306 # define MAIN_CAP_SYM
309 #define RTS_SYMBOLS \
313 SymX(stg_enter_info) \
314 SymX(stg_enter_ret) \
315 SymX(stg_gc_void_info) \
316 SymX(__stg_gc_enter_1) \
317 SymX(stg_gc_noregs) \
318 SymX(stg_gc_unpt_r1_info) \
319 SymX(stg_gc_unpt_r1) \
320 SymX(stg_gc_unbx_r1_info) \
321 SymX(stg_gc_unbx_r1) \
322 SymX(stg_gc_f1_info) \
324 SymX(stg_gc_d1_info) \
326 SymX(stg_gc_l1_info) \
329 SymX(stg_gc_fun_info) \
330 SymX(stg_gc_fun_ret) \
332 SymX(stg_gc_gen_info) \
333 SymX(stg_gc_gen_hp) \
335 SymX(stg_gen_yield) \
336 SymX(stg_yield_noregs) \
337 SymX(stg_yield_to_interpreter) \
338 SymX(stg_gen_block) \
339 SymX(stg_block_noregs) \
341 SymX(stg_block_takemvar) \
342 SymX(stg_block_putmvar) \
343 SymX(stg_seq_frame_info) \
346 SymX(MallocFailHook) \
348 SymX(OutOfHeapHook) \
349 SymX(PatErrorHdrHook) \
350 SymX(PostTraceHook) \
352 SymX(StackOverflowHook) \
353 SymX(__encodeDouble) \
354 SymX(__encodeFloat) \
357 SymX(__gmpz_cmp_si) \
358 SymX(__gmpz_cmp_ui) \
359 SymX(__gmpz_get_si) \
360 SymX(__gmpz_get_ui) \
361 SymX(__int_encodeDouble) \
362 SymX(__int_encodeFloat) \
363 SymX(andIntegerzh_fast) \
364 SymX(blockAsyncExceptionszh_fast) \
367 SymX(complementIntegerzh_fast) \
368 SymX(cmpIntegerzh_fast) \
369 SymX(cmpIntegerIntzh_fast) \
370 SymX(createAdjustor) \
371 SymX(decodeDoublezh_fast) \
372 SymX(decodeFloatzh_fast) \
375 SymX(deRefWeakzh_fast) \
376 SymX(deRefStablePtrzh_fast) \
377 SymX(divExactIntegerzh_fast) \
378 SymX(divModIntegerzh_fast) \
381 SymX(forkOS_createThread) \
382 SymX(freeHaskellFunctionPtr) \
383 SymX(freeStablePtr) \
384 SymX(gcdIntegerzh_fast) \
385 SymX(gcdIntegerIntzh_fast) \
386 SymX(gcdIntzh_fast) \
390 SymX(int2Integerzh_fast) \
391 SymX(integer2Intzh_fast) \
392 SymX(integer2Wordzh_fast) \
393 SymX(isCurrentThreadBoundzh_fast) \
394 SymX(isDoubleDenormalized) \
395 SymX(isDoubleInfinite) \
397 SymX(isDoubleNegativeZero) \
398 SymX(isEmptyMVarzh_fast) \
399 SymX(isFloatDenormalized) \
400 SymX(isFloatInfinite) \
402 SymX(isFloatNegativeZero) \
403 SymX(killThreadzh_fast) \
404 SymX(makeStablePtrzh_fast) \
405 SymX(minusIntegerzh_fast) \
406 SymX(mkApUpd0zh_fast) \
407 SymX(myThreadIdzh_fast) \
408 SymX(labelThreadzh_fast) \
409 SymX(newArrayzh_fast) \
410 SymX(newBCOzh_fast) \
411 SymX(newByteArrayzh_fast) \
412 SymX_redirect(newCAF, newDynCAF) \
413 SymX(newMVarzh_fast) \
414 SymX(newMutVarzh_fast) \
415 SymX(atomicModifyMutVarzh_fast) \
416 SymX(newPinnedByteArrayzh_fast) \
417 SymX(orIntegerzh_fast) \
419 SymX(performMajorGC) \
420 SymX(plusIntegerzh_fast) \
423 SymX(putMVarzh_fast) \
424 SymX(quotIntegerzh_fast) \
425 SymX(quotRemIntegerzh_fast) \
427 SymX(raiseIOzh_fast) \
428 SymX(remIntegerzh_fast) \
429 SymX(resetNonBlockingFd) \
432 SymX(rts_checkSchedStatus) \
435 SymX(rts_evalLazyIO) \
436 SymX(rts_evalStableIO) \
440 SymX(rts_getDouble) \
445 SymX(rts_getFunPtr) \
446 SymX(rts_getStablePtr) \
447 SymX(rts_getThreadId) \
449 SymX(rts_getWord32) \
462 SymX(rts_mkStablePtr) \
470 SymX(rtsSupportsBoundThreads) \
472 SymX(__hscore_get_saved_termios) \
473 SymX(__hscore_set_saved_termios) \
475 SymX(startupHaskell) \
476 SymX(shutdownHaskell) \
477 SymX(shutdownHaskellAndExit) \
478 SymX(stable_ptr_table) \
479 SymX(stackOverflow) \
480 SymX(stg_CAF_BLACKHOLE_info) \
481 SymX(stg_BLACKHOLE_BQ_info) \
482 SymX(awakenBlockedQueue) \
483 SymX(stg_CHARLIKE_closure) \
484 SymX(stg_EMPTY_MVAR_info) \
485 SymX(stg_IND_STATIC_info) \
486 SymX(stg_INTLIKE_closure) \
487 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
488 SymX(stg_WEAK_info) \
489 SymX(stg_ap_v_info) \
490 SymX(stg_ap_f_info) \
491 SymX(stg_ap_d_info) \
492 SymX(stg_ap_l_info) \
493 SymX(stg_ap_n_info) \
494 SymX(stg_ap_p_info) \
495 SymX(stg_ap_pv_info) \
496 SymX(stg_ap_pp_info) \
497 SymX(stg_ap_ppv_info) \
498 SymX(stg_ap_ppp_info) \
499 SymX(stg_ap_pppp_info) \
500 SymX(stg_ap_ppppp_info) \
501 SymX(stg_ap_pppppp_info) \
502 SymX(stg_ap_ppppppp_info) \
510 SymX(stg_ap_pv_ret) \
511 SymX(stg_ap_pp_ret) \
512 SymX(stg_ap_ppv_ret) \
513 SymX(stg_ap_ppp_ret) \
514 SymX(stg_ap_pppp_ret) \
515 SymX(stg_ap_ppppp_ret) \
516 SymX(stg_ap_pppppp_ret) \
517 SymX(stg_ap_ppppppp_ret) \
518 SymX(stg_ap_1_upd_info) \
519 SymX(stg_ap_2_upd_info) \
520 SymX(stg_ap_3_upd_info) \
521 SymX(stg_ap_4_upd_info) \
522 SymX(stg_ap_5_upd_info) \
523 SymX(stg_ap_6_upd_info) \
524 SymX(stg_ap_7_upd_info) \
525 SymX(stg_ap_8_upd_info) \
527 SymX(stg_sel_0_upd_info) \
528 SymX(stg_sel_10_upd_info) \
529 SymX(stg_sel_11_upd_info) \
530 SymX(stg_sel_12_upd_info) \
531 SymX(stg_sel_13_upd_info) \
532 SymX(stg_sel_14_upd_info) \
533 SymX(stg_sel_15_upd_info) \
534 SymX(stg_sel_1_upd_info) \
535 SymX(stg_sel_2_upd_info) \
536 SymX(stg_sel_3_upd_info) \
537 SymX(stg_sel_4_upd_info) \
538 SymX(stg_sel_5_upd_info) \
539 SymX(stg_sel_6_upd_info) \
540 SymX(stg_sel_7_upd_info) \
541 SymX(stg_sel_8_upd_info) \
542 SymX(stg_sel_9_upd_info) \
543 SymX(stg_upd_frame_info) \
544 SymX(suspendThread) \
545 SymX(takeMVarzh_fast) \
546 SymX(timesIntegerzh_fast) \
547 SymX(tryPutMVarzh_fast) \
548 SymX(tryTakeMVarzh_fast) \
549 SymX(unblockAsyncExceptionszh_fast) \
550 SymX(unsafeThawArrayzh_fast) \
551 SymX(waitReadzh_fast) \
552 SymX(waitWritezh_fast) \
553 SymX(word2Integerzh_fast) \
554 SymX(xorIntegerzh_fast) \
557 #ifdef SUPPORT_LONG_LONGS
558 #define RTS_LONG_LONG_SYMS \
559 SymX(int64ToIntegerzh_fast) \
560 SymX(word64ToIntegerzh_fast)
562 #define RTS_LONG_LONG_SYMS /* nothing */
565 // 64-bit support functions in libgcc.a
566 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
567 #define RTS_LIBGCC_SYMBOLS \
576 #elif defined(ia64_TARGET_ARCH)
577 #define RTS_LIBGCC_SYMBOLS \
585 #define RTS_LIBGCC_SYMBOLS
588 #ifdef darwin_TARGET_OS
589 // Symbols that don't have a leading underscore
590 // on Mac OS X. They have to receive special treatment,
591 // see machoInitSymbolsWithoutUnderscore()
592 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
597 /* entirely bogus claims about types of these symbols */
598 #define Sym(vvv) extern void vvv(void);
599 #define SymX(vvv) /**/
600 #define SymX_redirect(vvv,xxx) /**/
603 RTS_POSIX_ONLY_SYMBOLS
604 RTS_MINGW_ONLY_SYMBOLS
605 RTS_CYGWIN_ONLY_SYMBOLS
611 #ifdef LEADING_UNDERSCORE
612 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
614 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
617 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
619 #define SymX(vvv) Sym(vvv)
621 // SymX_redirect allows us to redirect references to one symbol to
622 // another symbol. See newCAF/newDynCAF for an example.
623 #define SymX_redirect(vvv,xxx) \
624 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
627 static RtsSymbolVal rtsSyms[] = {
630 RTS_POSIX_ONLY_SYMBOLS
631 RTS_MINGW_ONLY_SYMBOLS
632 RTS_CYGWIN_ONLY_SYMBOLS
634 { 0, 0 } /* sentinel */
637 /* -----------------------------------------------------------------------------
638 * Insert symbols into hash tables, checking for duplicates.
640 static void ghciInsertStrHashTable ( char* obj_name,
646 if (lookupHashTable(table, (StgWord)key) == NULL)
648 insertStrHashTable(table, (StgWord)key, data);
653 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
655 "whilst processing object file\n"
657 "This could be caused by:\n"
658 " * Loading two different object files which export the same symbol\n"
659 " * Specifying the same object file twice on the GHCi command line\n"
660 " * An incorrect `package.conf' entry, causing some object to be\n"
662 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
671 /* -----------------------------------------------------------------------------
672 * initialize the object linker
676 static int linker_init_done = 0 ;
678 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
679 static void *dl_prog_handle;
687 /* Make initLinker idempotent, so we can call it
688 before evey relevant operation; that means we
689 don't need to initialise the linker separately */
690 if (linker_init_done == 1) { return; } else {
691 linker_init_done = 1;
694 symhash = allocStrHashTable();
696 /* populate the symbol table with stuff from the RTS */
697 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
698 ghciInsertStrHashTable("(GHCi built-in symbols)",
699 symhash, sym->lbl, sym->addr);
701 # if defined(OBJFORMAT_MACHO)
702 machoInitSymbolsWithoutUnderscore();
705 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
706 # if defined(RTLD_DEFAULT)
707 dl_prog_handle = RTLD_DEFAULT;
709 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
710 # endif // RTLD_DEFAULT
714 /* -----------------------------------------------------------------------------
715 * Loading DLL or .so dynamic libraries
716 * -----------------------------------------------------------------------------
718 * Add a DLL from which symbols may be found. In the ELF case, just
719 * do RTLD_GLOBAL-style add, so no further messing around needs to
720 * happen in order that symbols in the loaded .so are findable --
721 * lookupSymbol() will subsequently see them by dlsym on the program's
722 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
724 * In the PEi386 case, open the DLLs and put handles to them in a
725 * linked list. When looking for a symbol, try all handles in the
726 * list. This means that we need to load even DLLs that are guaranteed
727 * to be in the ghc.exe image already, just so we can get a handle
728 * to give to loadSymbol, so that we can find the symbols. For such
729 * libraries, the LoadLibrary call should be a no-op except for returning
734 #if defined(OBJFORMAT_PEi386)
735 /* A record for storing handles into DLLs. */
740 struct _OpenedDLL* next;
745 /* A list thereof. */
746 static OpenedDLL* opened_dlls = NULL;
750 addDLL( char *dll_name )
752 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
753 /* ------------------- ELF DLL loader ------------------- */
759 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
762 /* dlopen failed; return a ptr to the error msg. */
764 if (errmsg == NULL) errmsg = "addDLL: unknown error";
771 # elif defined(OBJFORMAT_PEi386)
772 /* ------------------- Win32 DLL loader ------------------- */
780 /* fprintf(stderr, "\naddDLL; dll_name = `%s'\n", dll_name); */
782 /* See if we've already got it, and ignore if so. */
783 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
784 if (0 == strcmp(o_dll->name, dll_name))
788 /* The file name has no suffix (yet) so that we can try
789 both foo.dll and foo.drv
791 The documentation for LoadLibrary says:
792 If no file name extension is specified in the lpFileName
793 parameter, the default library extension .dll is
794 appended. However, the file name string can include a trailing
795 point character (.) to indicate that the module name has no
798 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
799 sprintf(buf, "%s.DLL", dll_name);
800 instance = LoadLibrary(buf);
801 if (instance == NULL) {
802 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
803 instance = LoadLibrary(buf);
804 if (instance == NULL) {
807 /* LoadLibrary failed; return a ptr to the error msg. */
808 return "addDLL: unknown error";
813 /* Add this DLL to the list of DLLs in which to search for symbols. */
814 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
815 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
816 strcpy(o_dll->name, dll_name);
817 o_dll->instance = instance;
818 o_dll->next = opened_dlls;
823 barf("addDLL: not implemented on this platform");
827 /* -----------------------------------------------------------------------------
828 * lookup a symbol in the hash table
831 lookupSymbol( char *lbl )
835 ASSERT(symhash != NULL);
836 val = lookupStrHashTable(symhash, lbl);
839 # if defined(OBJFORMAT_ELF)
840 return dlsym(dl_prog_handle, lbl);
841 # elif defined(OBJFORMAT_MACHO)
842 if(NSIsSymbolNameDefined(lbl)) {
843 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
844 return NSAddressOfSymbol(symbol);
848 # elif defined(OBJFORMAT_PEi386)
851 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
852 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
854 /* HACK: if the name has an initial underscore, try stripping
855 it off & look that up first. I've yet to verify whether there's
856 a Rule that governs whether an initial '_' *should always* be
857 stripped off when mapping from import lib name to the DLL name.
859 sym = GetProcAddress(o_dll->instance, (lbl+1));
861 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
865 sym = GetProcAddress(o_dll->instance, lbl);
867 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
882 __attribute((unused))
884 lookupLocalSymbol( ObjectCode* oc, char *lbl )
888 val = lookupStrHashTable(oc->lochash, lbl);
898 /* -----------------------------------------------------------------------------
899 * Debugging aid: look in GHCi's object symbol tables for symbols
900 * within DELTA bytes of the specified address, and show their names.
903 void ghci_enquire ( char* addr );
905 void ghci_enquire ( char* addr )
910 const int DELTA = 64;
915 for (oc = objects; oc; oc = oc->next) {
916 for (i = 0; i < oc->n_symbols; i++) {
917 sym = oc->symbols[i];
918 if (sym == NULL) continue;
919 // fprintf(stderr, "enquire %p %p\n", sym, oc->lochash);
921 if (oc->lochash != NULL) {
922 a = lookupStrHashTable(oc->lochash, sym);
925 a = lookupStrHashTable(symhash, sym);
928 // fprintf(stderr, "ghci_enquire: can't find %s\n", sym);
930 else if (addr-DELTA <= a && a <= addr+DELTA) {
931 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
938 #ifdef ia64_TARGET_ARCH
939 static unsigned int PLTSize(void);
942 /* -----------------------------------------------------------------------------
943 * Load an obj (populate the global symbol table, but don't resolve yet)
945 * Returns: 1 if ok, 0 on error.
948 loadObj( char *path )
962 /* fprintf(stderr, "loadObj %s\n", path ); */
964 /* Check that we haven't already loaded this object. Don't give up
965 at this stage; ocGetNames_* will barf later. */
969 for (o = objects; o; o = o->next) {
970 if (0 == strcmp(o->fileName, path))
976 "GHCi runtime linker: warning: looks like you're trying to load the\n"
977 "same object file twice:\n"
979 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
985 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
987 # if defined(OBJFORMAT_ELF)
988 oc->formatName = "ELF";
989 # elif defined(OBJFORMAT_PEi386)
990 oc->formatName = "PEi386";
991 # elif defined(OBJFORMAT_MACHO)
992 oc->formatName = "Mach-O";
995 barf("loadObj: not implemented on this platform");
999 if (r == -1) { return 0; }
1001 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1002 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
1003 strcpy(oc->fileName, path);
1005 oc->fileSize = st.st_size;
1007 oc->sections = NULL;
1008 oc->lochash = allocStrHashTable();
1009 oc->proddables = NULL;
1011 /* chain it onto the list of objects */
1016 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1018 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1020 fd = open(path, O_RDONLY);
1022 barf("loadObj: can't open `%s'", path);
1024 pagesize = getpagesize();
1026 #ifdef ia64_TARGET_ARCH
1027 /* The PLT needs to be right before the object */
1028 n = ROUND_UP(PLTSize(), pagesize);
1029 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1030 if (oc->plt == MAP_FAILED)
1031 barf("loadObj: can't allocate PLT");
1034 map_addr = oc->plt + n;
1037 n = ROUND_UP(oc->fileSize, pagesize);
1038 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1039 if (oc->image == MAP_FAILED)
1040 barf("loadObj: can't map `%s'", path);
1044 #else /* !USE_MMAP */
1046 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
1048 /* load the image into memory */
1049 f = fopen(path, "rb");
1051 barf("loadObj: can't read `%s'", path);
1053 n = fread ( oc->image, 1, oc->fileSize, f );
1054 if (n != oc->fileSize)
1055 barf("loadObj: error whilst reading `%s'", path);
1059 #endif /* USE_MMAP */
1061 # if defined(OBJFORMAT_MACHO)
1062 r = ocAllocateJumpIslands_MachO ( oc );
1063 if (!r) { return r; }
1066 /* verify the in-memory image */
1067 # if defined(OBJFORMAT_ELF)
1068 r = ocVerifyImage_ELF ( oc );
1069 # elif defined(OBJFORMAT_PEi386)
1070 r = ocVerifyImage_PEi386 ( oc );
1071 # elif defined(OBJFORMAT_MACHO)
1072 r = ocVerifyImage_MachO ( oc );
1074 barf("loadObj: no verify method");
1076 if (!r) { return r; }
1078 /* build the symbol list for this image */
1079 # if defined(OBJFORMAT_ELF)
1080 r = ocGetNames_ELF ( oc );
1081 # elif defined(OBJFORMAT_PEi386)
1082 r = ocGetNames_PEi386 ( oc );
1083 # elif defined(OBJFORMAT_MACHO)
1084 r = ocGetNames_MachO ( oc );
1086 barf("loadObj: no getNames method");
1088 if (!r) { return r; }
1090 /* loaded, but not resolved yet */
1091 oc->status = OBJECT_LOADED;
1096 /* -----------------------------------------------------------------------------
1097 * resolve all the currently unlinked objects in memory
1099 * Returns: 1 if ok, 0 on error.
1109 for (oc = objects; oc; oc = oc->next) {
1110 if (oc->status != OBJECT_RESOLVED) {
1111 # if defined(OBJFORMAT_ELF)
1112 r = ocResolve_ELF ( oc );
1113 # elif defined(OBJFORMAT_PEi386)
1114 r = ocResolve_PEi386 ( oc );
1115 # elif defined(OBJFORMAT_MACHO)
1116 r = ocResolve_MachO ( oc );
1118 barf("resolveObjs: not implemented on this platform");
1120 if (!r) { return r; }
1121 oc->status = OBJECT_RESOLVED;
1127 /* -----------------------------------------------------------------------------
1128 * delete an object from the pool
1131 unloadObj( char *path )
1133 ObjectCode *oc, *prev;
1135 ASSERT(symhash != NULL);
1136 ASSERT(objects != NULL);
1141 for (oc = objects; oc; prev = oc, oc = oc->next) {
1142 if (!strcmp(oc->fileName,path)) {
1144 /* Remove all the mappings for the symbols within this
1149 for (i = 0; i < oc->n_symbols; i++) {
1150 if (oc->symbols[i] != NULL) {
1151 removeStrHashTable(symhash, oc->symbols[i], NULL);
1159 prev->next = oc->next;
1162 /* We're going to leave this in place, in case there are
1163 any pointers from the heap into it: */
1164 /* stgFree(oc->image); */
1165 stgFree(oc->fileName);
1166 stgFree(oc->symbols);
1167 stgFree(oc->sections);
1168 /* The local hash table should have been freed at the end
1169 of the ocResolve_ call on it. */
1170 ASSERT(oc->lochash == NULL);
1176 belch("unloadObj: can't find `%s' to unload", path);
1180 /* -----------------------------------------------------------------------------
1181 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1182 * which may be prodded during relocation, and abort if we try and write
1183 * outside any of these.
1185 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1188 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1189 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1193 pb->next = oc->proddables;
1194 oc->proddables = pb;
1197 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1200 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1201 char* s = (char*)(pb->start);
1202 char* e = s + pb->size - 1;
1203 char* a = (char*)addr;
1204 /* Assumes that the biggest fixup involves a 4-byte write. This
1205 probably needs to be changed to 8 (ie, +7) on 64-bit
1207 if (a >= s && (a+3) <= e) return;
1209 barf("checkProddableBlock: invalid fixup in runtime linker");
1212 /* -----------------------------------------------------------------------------
1213 * Section management.
1215 static void addSection ( ObjectCode* oc, SectionKind kind,
1216 void* start, void* end )
1218 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1222 s->next = oc->sections;
1225 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1226 start, ((char*)end)-1, end - start + 1, kind );
1232 /* --------------------------------------------------------------------------
1233 * PEi386 specifics (Win32 targets)
1234 * ------------------------------------------------------------------------*/
1236 /* The information for this linker comes from
1237 Microsoft Portable Executable
1238 and Common Object File Format Specification
1239 revision 5.1 January 1998
1240 which SimonM says comes from the MS Developer Network CDs.
1242 It can be found there (on older CDs), but can also be found
1245 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1247 (this is Rev 6.0 from February 1999).
1249 Things move, so if that fails, try searching for it via
1251 http://www.google.com/search?q=PE+COFF+specification
1253 The ultimate reference for the PE format is the Winnt.h
1254 header file that comes with the Platform SDKs; as always,
1255 implementations will drift wrt their documentation.
1257 A good background article on the PE format is Matt Pietrek's
1258 March 1994 article in Microsoft System Journal (MSJ)
1259 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1260 Win32 Portable Executable File Format." The info in there
1261 has recently been updated in a two part article in
1262 MSDN magazine, issues Feb and March 2002,
1263 "Inside Windows: An In-Depth Look into the Win32 Portable
1264 Executable File Format"
1266 John Levine's book "Linkers and Loaders" contains useful
1271 #if defined(OBJFORMAT_PEi386)
1275 typedef unsigned char UChar;
1276 typedef unsigned short UInt16;
1277 typedef unsigned int UInt32;
1284 UInt16 NumberOfSections;
1285 UInt32 TimeDateStamp;
1286 UInt32 PointerToSymbolTable;
1287 UInt32 NumberOfSymbols;
1288 UInt16 SizeOfOptionalHeader;
1289 UInt16 Characteristics;
1293 #define sizeof_COFF_header 20
1300 UInt32 VirtualAddress;
1301 UInt32 SizeOfRawData;
1302 UInt32 PointerToRawData;
1303 UInt32 PointerToRelocations;
1304 UInt32 PointerToLinenumbers;
1305 UInt16 NumberOfRelocations;
1306 UInt16 NumberOfLineNumbers;
1307 UInt32 Characteristics;
1311 #define sizeof_COFF_section 40
1318 UInt16 SectionNumber;
1321 UChar NumberOfAuxSymbols;
1325 #define sizeof_COFF_symbol 18
1330 UInt32 VirtualAddress;
1331 UInt32 SymbolTableIndex;
1336 #define sizeof_COFF_reloc 10
1339 /* From PE spec doc, section 3.3.2 */
1340 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1341 windows.h -- for the same purpose, but I want to know what I'm
1343 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1344 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1345 #define MYIMAGE_FILE_DLL 0x2000
1346 #define MYIMAGE_FILE_SYSTEM 0x1000
1347 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1348 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1349 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1351 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1352 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1353 #define MYIMAGE_SYM_CLASS_STATIC 3
1354 #define MYIMAGE_SYM_UNDEFINED 0
1356 /* From PE spec doc, section 4.1 */
1357 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1358 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1359 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1361 /* From PE spec doc, section 5.2.1 */
1362 #define MYIMAGE_REL_I386_DIR32 0x0006
1363 #define MYIMAGE_REL_I386_REL32 0x0014
1366 /* We use myindex to calculate array addresses, rather than
1367 simply doing the normal subscript thing. That's because
1368 some of the above structs have sizes which are not
1369 a whole number of words. GCC rounds their sizes up to a
1370 whole number of words, which means that the address calcs
1371 arising from using normal C indexing or pointer arithmetic
1372 are just plain wrong. Sigh.
1375 myindex ( int scale, void* base, int index )
1378 ((UChar*)base) + scale * index;
1383 printName ( UChar* name, UChar* strtab )
1385 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1386 UInt32 strtab_offset = * (UInt32*)(name+4);
1387 fprintf ( stderr, "%s", strtab + strtab_offset );
1390 for (i = 0; i < 8; i++) {
1391 if (name[i] == 0) break;
1392 fprintf ( stderr, "%c", name[i] );
1399 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1401 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1402 UInt32 strtab_offset = * (UInt32*)(name+4);
1403 strncpy ( dst, strtab+strtab_offset, dstSize );
1409 if (name[i] == 0) break;
1419 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1422 /* If the string is longer than 8 bytes, look in the
1423 string table for it -- this will be correctly zero terminated.
1425 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1426 UInt32 strtab_offset = * (UInt32*)(name+4);
1427 return ((UChar*)strtab) + strtab_offset;
1429 /* Otherwise, if shorter than 8 bytes, return the original,
1430 which by defn is correctly terminated.
1432 if (name[7]==0) return name;
1433 /* The annoying case: 8 bytes. Copy into a temporary
1434 (which is never freed ...)
1436 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1438 strncpy(newstr,name,8);
1444 /* Just compares the short names (first 8 chars) */
1445 static COFF_section *
1446 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1450 = (COFF_header*)(oc->image);
1451 COFF_section* sectab
1453 ((UChar*)(oc->image))
1454 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1456 for (i = 0; i < hdr->NumberOfSections; i++) {
1459 COFF_section* section_i
1461 myindex ( sizeof_COFF_section, sectab, i );
1462 n1 = (UChar*) &(section_i->Name);
1464 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1465 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1466 n1[6]==n2[6] && n1[7]==n2[7])
1475 zapTrailingAtSign ( UChar* sym )
1477 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1479 if (sym[0] == 0) return;
1481 while (sym[i] != 0) i++;
1484 while (j > 0 && my_isdigit(sym[j])) j--;
1485 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1491 ocVerifyImage_PEi386 ( ObjectCode* oc )
1496 COFF_section* sectab;
1497 COFF_symbol* symtab;
1499 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1500 hdr = (COFF_header*)(oc->image);
1501 sectab = (COFF_section*) (
1502 ((UChar*)(oc->image))
1503 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1505 symtab = (COFF_symbol*) (
1506 ((UChar*)(oc->image))
1507 + hdr->PointerToSymbolTable
1509 strtab = ((UChar*)symtab)
1510 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1512 if (hdr->Machine != 0x14c) {
1513 belch("Not x86 PEi386");
1516 if (hdr->SizeOfOptionalHeader != 0) {
1517 belch("PEi386 with nonempty optional header");
1520 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1521 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1522 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1523 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1524 belch("Not a PEi386 object file");
1527 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1528 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1529 belch("Invalid PEi386 word size or endiannness: %d",
1530 (int)(hdr->Characteristics));
1533 /* If the string table size is way crazy, this might indicate that
1534 there are more than 64k relocations, despite claims to the
1535 contrary. Hence this test. */
1536 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1538 if ( (*(UInt32*)strtab) > 600000 ) {
1539 /* Note that 600k has no special significance other than being
1540 big enough to handle the almost-2MB-sized lumps that
1541 constitute HSwin32*.o. */
1542 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1547 /* No further verification after this point; only debug printing. */
1549 IF_DEBUG(linker, i=1);
1550 if (i == 0) return 1;
1553 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1555 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1557 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1559 fprintf ( stderr, "\n" );
1561 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1563 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1565 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1567 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1569 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1571 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1573 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1575 /* Print the section table. */
1576 fprintf ( stderr, "\n" );
1577 for (i = 0; i < hdr->NumberOfSections; i++) {
1579 COFF_section* sectab_i
1581 myindex ( sizeof_COFF_section, sectab, i );
1588 printName ( sectab_i->Name, strtab );
1598 sectab_i->VirtualSize,
1599 sectab_i->VirtualAddress,
1600 sectab_i->SizeOfRawData,
1601 sectab_i->PointerToRawData,
1602 sectab_i->NumberOfRelocations,
1603 sectab_i->PointerToRelocations,
1604 sectab_i->PointerToRawData
1606 reltab = (COFF_reloc*) (
1607 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1610 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1611 /* If the relocation field (a short) has overflowed, the
1612 * real count can be found in the first reloc entry.
1614 * See Section 4.1 (last para) of the PE spec (rev6.0).
1616 COFF_reloc* rel = (COFF_reloc*)
1617 myindex ( sizeof_COFF_reloc, reltab, 0 );
1618 noRelocs = rel->VirtualAddress;
1621 noRelocs = sectab_i->NumberOfRelocations;
1625 for (; j < noRelocs; j++) {
1627 COFF_reloc* rel = (COFF_reloc*)
1628 myindex ( sizeof_COFF_reloc, reltab, j );
1630 " type 0x%-4x vaddr 0x%-8x name `",
1632 rel->VirtualAddress );
1633 sym = (COFF_symbol*)
1634 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1635 /* Hmm..mysterious looking offset - what's it for? SOF */
1636 printName ( sym->Name, strtab -10 );
1637 fprintf ( stderr, "'\n" );
1640 fprintf ( stderr, "\n" );
1642 fprintf ( stderr, "\n" );
1643 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1644 fprintf ( stderr, "---START of string table---\n");
1645 for (i = 4; i < *(Int32*)strtab; i++) {
1647 fprintf ( stderr, "\n"); else
1648 fprintf( stderr, "%c", strtab[i] );
1650 fprintf ( stderr, "--- END of string table---\n");
1652 fprintf ( stderr, "\n" );
1655 COFF_symbol* symtab_i;
1656 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1657 symtab_i = (COFF_symbol*)
1658 myindex ( sizeof_COFF_symbol, symtab, i );
1664 printName ( symtab_i->Name, strtab );
1673 (Int32)(symtab_i->SectionNumber),
1674 (UInt32)symtab_i->Type,
1675 (UInt32)symtab_i->StorageClass,
1676 (UInt32)symtab_i->NumberOfAuxSymbols
1678 i += symtab_i->NumberOfAuxSymbols;
1682 fprintf ( stderr, "\n" );
1688 ocGetNames_PEi386 ( ObjectCode* oc )
1691 COFF_section* sectab;
1692 COFF_symbol* symtab;
1699 hdr = (COFF_header*)(oc->image);
1700 sectab = (COFF_section*) (
1701 ((UChar*)(oc->image))
1702 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1704 symtab = (COFF_symbol*) (
1705 ((UChar*)(oc->image))
1706 + hdr->PointerToSymbolTable
1708 strtab = ((UChar*)(oc->image))
1709 + hdr->PointerToSymbolTable
1710 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1712 /* Allocate space for any (local, anonymous) .bss sections. */
1714 for (i = 0; i < hdr->NumberOfSections; i++) {
1716 COFF_section* sectab_i
1718 myindex ( sizeof_COFF_section, sectab, i );
1719 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1720 if (sectab_i->VirtualSize == 0) continue;
1721 /* This is a non-empty .bss section. Allocate zeroed space for
1722 it, and set its PointerToRawData field such that oc->image +
1723 PointerToRawData == addr_of_zeroed_space. */
1724 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1725 "ocGetNames_PEi386(anonymous bss)");
1726 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1727 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1728 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1731 /* Copy section information into the ObjectCode. */
1733 for (i = 0; i < hdr->NumberOfSections; i++) {
1739 = SECTIONKIND_OTHER;
1740 COFF_section* sectab_i
1742 myindex ( sizeof_COFF_section, sectab, i );
1743 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1746 /* I'm sure this is the Right Way to do it. However, the
1747 alternative of testing the sectab_i->Name field seems to
1748 work ok with Cygwin.
1750 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1751 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1752 kind = SECTIONKIND_CODE_OR_RODATA;
1755 if (0==strcmp(".text",sectab_i->Name) ||
1756 0==strcmp(".rodata",sectab_i->Name))
1757 kind = SECTIONKIND_CODE_OR_RODATA;
1758 if (0==strcmp(".data",sectab_i->Name) ||
1759 0==strcmp(".bss",sectab_i->Name))
1760 kind = SECTIONKIND_RWDATA;
1762 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1763 sz = sectab_i->SizeOfRawData;
1764 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1766 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1767 end = start + sz - 1;
1769 if (kind == SECTIONKIND_OTHER
1770 /* Ignore sections called which contain stabs debugging
1772 && 0 != strcmp(".stab", sectab_i->Name)
1773 && 0 != strcmp(".stabstr", sectab_i->Name)
1775 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1779 if (kind != SECTIONKIND_OTHER && end >= start) {
1780 addSection(oc, kind, start, end);
1781 addProddableBlock(oc, start, end - start + 1);
1785 /* Copy exported symbols into the ObjectCode. */
1787 oc->n_symbols = hdr->NumberOfSymbols;
1788 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1789 "ocGetNames_PEi386(oc->symbols)");
1790 /* Call me paranoid; I don't care. */
1791 for (i = 0; i < oc->n_symbols; i++)
1792 oc->symbols[i] = NULL;
1796 COFF_symbol* symtab_i;
1797 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1798 symtab_i = (COFF_symbol*)
1799 myindex ( sizeof_COFF_symbol, symtab, i );
1803 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1804 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1805 /* This symbol is global and defined, viz, exported */
1806 /* for MYIMAGE_SYMCLASS_EXTERNAL
1807 && !MYIMAGE_SYM_UNDEFINED,
1808 the address of the symbol is:
1809 address of relevant section + offset in section
1811 COFF_section* sectabent
1812 = (COFF_section*) myindex ( sizeof_COFF_section,
1814 symtab_i->SectionNumber-1 );
1815 addr = ((UChar*)(oc->image))
1816 + (sectabent->PointerToRawData
1820 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1821 && symtab_i->Value > 0) {
1822 /* This symbol isn't in any section at all, ie, global bss.
1823 Allocate zeroed space for it. */
1824 addr = stgCallocBytes(1, symtab_i->Value,
1825 "ocGetNames_PEi386(non-anonymous bss)");
1826 addSection(oc, SECTIONKIND_RWDATA, addr,
1827 ((UChar*)addr) + symtab_i->Value - 1);
1828 addProddableBlock(oc, addr, symtab_i->Value);
1829 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1832 if (addr != NULL ) {
1833 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1834 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1835 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1836 ASSERT(i >= 0 && i < oc->n_symbols);
1837 /* cstring_from_COFF_symbol_name always succeeds. */
1838 oc->symbols[i] = sname;
1839 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1843 "IGNORING symbol %d\n"
1847 printName ( symtab_i->Name, strtab );
1856 (Int32)(symtab_i->SectionNumber),
1857 (UInt32)symtab_i->Type,
1858 (UInt32)symtab_i->StorageClass,
1859 (UInt32)symtab_i->NumberOfAuxSymbols
1864 i += symtab_i->NumberOfAuxSymbols;
1873 ocResolve_PEi386 ( ObjectCode* oc )
1876 COFF_section* sectab;
1877 COFF_symbol* symtab;
1887 /* ToDo: should be variable-sized? But is at least safe in the
1888 sense of buffer-overrun-proof. */
1890 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1892 hdr = (COFF_header*)(oc->image);
1893 sectab = (COFF_section*) (
1894 ((UChar*)(oc->image))
1895 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1897 symtab = (COFF_symbol*) (
1898 ((UChar*)(oc->image))
1899 + hdr->PointerToSymbolTable
1901 strtab = ((UChar*)(oc->image))
1902 + hdr->PointerToSymbolTable
1903 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1905 for (i = 0; i < hdr->NumberOfSections; i++) {
1906 COFF_section* sectab_i
1908 myindex ( sizeof_COFF_section, sectab, i );
1911 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1914 /* Ignore sections called which contain stabs debugging
1916 if (0 == strcmp(".stab", sectab_i->Name)
1917 || 0 == strcmp(".stabstr", sectab_i->Name))
1920 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1921 /* If the relocation field (a short) has overflowed, the
1922 * real count can be found in the first reloc entry.
1924 * See Section 4.1 (last para) of the PE spec (rev6.0).
1926 * Nov2003 update: the GNU linker still doesn't correctly
1927 * handle the generation of relocatable object files with
1928 * overflown relocations. Hence the output to warn of potential
1931 COFF_reloc* rel = (COFF_reloc*)
1932 myindex ( sizeof_COFF_reloc, reltab, 0 );
1933 noRelocs = rel->VirtualAddress;
1934 fprintf(stderr, "WARNING: Overflown relocation field (# relocs found: %u)\n", noRelocs); fflush(stderr);
1937 noRelocs = sectab_i->NumberOfRelocations;
1942 for (; j < noRelocs; j++) {
1944 COFF_reloc* reltab_j
1946 myindex ( sizeof_COFF_reloc, reltab, j );
1948 /* the location to patch */
1950 ((UChar*)(oc->image))
1951 + (sectab_i->PointerToRawData
1952 + reltab_j->VirtualAddress
1953 - sectab_i->VirtualAddress )
1955 /* the existing contents of pP */
1957 /* the symbol to connect to */
1958 sym = (COFF_symbol*)
1959 myindex ( sizeof_COFF_symbol,
1960 symtab, reltab_j->SymbolTableIndex );
1963 "reloc sec %2d num %3d: type 0x%-4x "
1964 "vaddr 0x%-8x name `",
1966 (UInt32)reltab_j->Type,
1967 reltab_j->VirtualAddress );
1968 printName ( sym->Name, strtab );
1969 fprintf ( stderr, "'\n" ));
1971 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1972 COFF_section* section_sym
1973 = findPEi386SectionCalled ( oc, sym->Name );
1975 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1978 S = ((UInt32)(oc->image))
1979 + (section_sym->PointerToRawData
1982 copyName ( sym->Name, strtab, symbol, 1000-1 );
1983 (void*)S = lookupLocalSymbol( oc, symbol );
1984 if ((void*)S != NULL) goto foundit;
1985 (void*)S = lookupSymbol( symbol );
1986 if ((void*)S != NULL) goto foundit;
1987 zapTrailingAtSign ( symbol );
1988 (void*)S = lookupLocalSymbol( oc, symbol );
1989 if ((void*)S != NULL) goto foundit;
1990 (void*)S = lookupSymbol( symbol );
1991 if ((void*)S != NULL) goto foundit;
1992 /* Newline first because the interactive linker has printed "linking..." */
1993 belch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
1997 checkProddableBlock(oc, pP);
1998 switch (reltab_j->Type) {
1999 case MYIMAGE_REL_I386_DIR32:
2002 case MYIMAGE_REL_I386_REL32:
2003 /* Tricky. We have to insert a displacement at
2004 pP which, when added to the PC for the _next_
2005 insn, gives the address of the target (S).
2006 Problem is to know the address of the next insn
2007 when we only know pP. We assume that this
2008 literal field is always the last in the insn,
2009 so that the address of the next insn is pP+4
2010 -- hence the constant 4.
2011 Also I don't know if A should be added, but so
2012 far it has always been zero.
2015 *pP = S - ((UInt32)pP) - 4;
2018 belch("%s: unhandled PEi386 relocation type %d",
2019 oc->fileName, reltab_j->Type);
2026 IF_DEBUG(linker, belch("completed %s", oc->fileName));
2030 #endif /* defined(OBJFORMAT_PEi386) */
2033 /* --------------------------------------------------------------------------
2035 * ------------------------------------------------------------------------*/
2037 #if defined(OBJFORMAT_ELF)
2042 #if defined(sparc_TARGET_ARCH)
2043 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2044 #elif defined(i386_TARGET_ARCH)
2045 # define ELF_TARGET_386 /* Used inside <elf.h> */
2046 #elif defined(x86_64_TARGET_ARCH)
2047 # define ELF_TARGET_X64_64
2049 #elif defined (ia64_TARGET_ARCH)
2050 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2052 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2053 # define ELF_NEED_GOT /* needs Global Offset Table */
2054 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2057 #if !defined(openbsd_TARGET_OS)
2060 /* openbsd elf has things in different places, with diff names */
2061 #include <elf_abi.h>
2062 #include <machine/reloc.h>
2063 #define R_386_32 RELOC_32
2064 #define R_386_PC32 RELOC_PC32
2068 * Define a set of types which can be used for both ELF32 and ELF64
2072 #define ELFCLASS ELFCLASS64
2073 #define Elf_Addr Elf64_Addr
2074 #define Elf_Word Elf64_Word
2075 #define Elf_Sword Elf64_Sword
2076 #define Elf_Ehdr Elf64_Ehdr
2077 #define Elf_Phdr Elf64_Phdr
2078 #define Elf_Shdr Elf64_Shdr
2079 #define Elf_Sym Elf64_Sym
2080 #define Elf_Rel Elf64_Rel
2081 #define Elf_Rela Elf64_Rela
2082 #define ELF_ST_TYPE ELF64_ST_TYPE
2083 #define ELF_ST_BIND ELF64_ST_BIND
2084 #define ELF_R_TYPE ELF64_R_TYPE
2085 #define ELF_R_SYM ELF64_R_SYM
2087 #define ELFCLASS ELFCLASS32
2088 #define Elf_Addr Elf32_Addr
2089 #define Elf_Word Elf32_Word
2090 #define Elf_Sword Elf32_Sword
2091 #define Elf_Ehdr Elf32_Ehdr
2092 #define Elf_Phdr Elf32_Phdr
2093 #define Elf_Shdr Elf32_Shdr
2094 #define Elf_Sym Elf32_Sym
2095 #define Elf_Rel Elf32_Rel
2096 #define Elf_Rela Elf32_Rela
2098 #define ELF_ST_TYPE ELF32_ST_TYPE
2101 #define ELF_ST_BIND ELF32_ST_BIND
2104 #define ELF_R_TYPE ELF32_R_TYPE
2107 #define ELF_R_SYM ELF32_R_SYM
2113 * Functions to allocate entries in dynamic sections. Currently we simply
2114 * preallocate a large number, and we don't check if a entry for the given
2115 * target already exists (a linear search is too slow). Ideally these
2116 * entries would be associated with symbols.
2119 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2120 #define GOT_SIZE 0x20000
2121 #define FUNCTION_TABLE_SIZE 0x10000
2122 #define PLT_SIZE 0x08000
2125 static Elf_Addr got[GOT_SIZE];
2126 static unsigned int gotIndex;
2127 static Elf_Addr gp_val = (Elf_Addr)got;
2130 allocateGOTEntry(Elf_Addr target)
2134 if (gotIndex >= GOT_SIZE)
2135 barf("Global offset table overflow");
2137 entry = &got[gotIndex++];
2139 return (Elf_Addr)entry;
2143 #ifdef ELF_FUNCTION_DESC
2149 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2150 static unsigned int functionTableIndex;
2153 allocateFunctionDesc(Elf_Addr target)
2155 FunctionDesc *entry;
2157 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2158 barf("Function table overflow");
2160 entry = &functionTable[functionTableIndex++];
2162 entry->gp = (Elf_Addr)gp_val;
2163 return (Elf_Addr)entry;
2167 copyFunctionDesc(Elf_Addr target)
2169 FunctionDesc *olddesc = (FunctionDesc *)target;
2170 FunctionDesc *newdesc;
2172 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2173 newdesc->gp = olddesc->gp;
2174 return (Elf_Addr)newdesc;
2179 #ifdef ia64_TARGET_ARCH
2180 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2181 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2183 static unsigned char plt_code[] =
2185 /* taken from binutils bfd/elfxx-ia64.c */
2186 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2187 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2188 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2189 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2190 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2191 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2194 /* If we can't get to the function descriptor via gp, take a local copy of it */
2195 #define PLT_RELOC(code, target) { \
2196 Elf64_Sxword rel_value = target - gp_val; \
2197 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2198 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2200 ia64_reloc_gprel22((Elf_Addr)code, target); \
2205 unsigned char code[sizeof(plt_code)];
2209 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2211 PLTEntry *plt = (PLTEntry *)oc->plt;
2214 if (oc->pltIndex >= PLT_SIZE)
2215 barf("Procedure table overflow");
2217 entry = &plt[oc->pltIndex++];
2218 memcpy(entry->code, plt_code, sizeof(entry->code));
2219 PLT_RELOC(entry->code, target);
2220 return (Elf_Addr)entry;
2226 return (PLT_SIZE * sizeof(PLTEntry));
2232 * Generic ELF functions
2236 findElfSection ( void* objImage, Elf_Word sh_type )
2238 char* ehdrC = (char*)objImage;
2239 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2240 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2241 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2245 for (i = 0; i < ehdr->e_shnum; i++) {
2246 if (shdr[i].sh_type == sh_type
2247 /* Ignore the section header's string table. */
2248 && i != ehdr->e_shstrndx
2249 /* Ignore string tables named .stabstr, as they contain
2251 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2253 ptr = ehdrC + shdr[i].sh_offset;
2260 #if defined(ia64_TARGET_ARCH)
2262 findElfSegment ( void* objImage, Elf_Addr vaddr )
2264 char* ehdrC = (char*)objImage;
2265 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2266 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2267 Elf_Addr segaddr = 0;
2270 for (i = 0; i < ehdr->e_phnum; i++) {
2271 segaddr = phdr[i].p_vaddr;
2272 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2280 ocVerifyImage_ELF ( ObjectCode* oc )
2284 int i, j, nent, nstrtab, nsymtabs;
2288 char* ehdrC = (char*)(oc->image);
2289 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2291 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2292 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2293 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2294 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2295 belch("%s: not an ELF object", oc->fileName);
2299 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2300 belch("%s: unsupported ELF format", oc->fileName);
2304 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2305 IF_DEBUG(linker,belch( "Is little-endian" ));
2307 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2308 IF_DEBUG(linker,belch( "Is big-endian" ));
2310 belch("%s: unknown endiannness", oc->fileName);
2314 if (ehdr->e_type != ET_REL) {
2315 belch("%s: not a relocatable object (.o) file", oc->fileName);
2318 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2320 IF_DEBUG(linker,belch( "Architecture is " ));
2321 switch (ehdr->e_machine) {
2322 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2323 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2325 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2327 default: IF_DEBUG(linker,belch( "unknown" ));
2328 belch("%s: unknown architecture", oc->fileName);
2332 IF_DEBUG(linker,belch(
2333 "\nSection header table: start %d, n_entries %d, ent_size %d",
2334 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2336 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2338 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2340 if (ehdr->e_shstrndx == SHN_UNDEF) {
2341 belch("%s: no section header string table", oc->fileName);
2344 IF_DEBUG(linker,belch( "Section header string table is section %d",
2346 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2349 for (i = 0; i < ehdr->e_shnum; i++) {
2350 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2351 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2352 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2353 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2354 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2355 ehdrC + shdr[i].sh_offset,
2356 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2358 if (shdr[i].sh_type == SHT_REL) {
2359 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2360 } else if (shdr[i].sh_type == SHT_RELA) {
2361 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2363 IF_DEBUG(linker,fprintf(stderr," "));
2366 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2370 IF_DEBUG(linker,belch( "\nString tables" ));
2373 for (i = 0; i < ehdr->e_shnum; i++) {
2374 if (shdr[i].sh_type == SHT_STRTAB
2375 /* Ignore the section header's string table. */
2376 && i != ehdr->e_shstrndx
2377 /* Ignore string tables named .stabstr, as they contain
2379 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2381 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2382 strtab = ehdrC + shdr[i].sh_offset;
2387 belch("%s: no string tables, or too many", oc->fileName);
2392 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2393 for (i = 0; i < ehdr->e_shnum; i++) {
2394 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2395 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2397 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2398 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2399 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2401 shdr[i].sh_size % sizeof(Elf_Sym)
2403 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2404 belch("%s: non-integral number of symbol table entries", oc->fileName);
2407 for (j = 0; j < nent; j++) {
2408 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2409 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2410 (int)stab[j].st_shndx,
2411 (int)stab[j].st_size,
2412 (char*)stab[j].st_value ));
2414 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2415 switch (ELF_ST_TYPE(stab[j].st_info)) {
2416 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2417 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2418 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2419 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2420 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2421 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2423 IF_DEBUG(linker,fprintf(stderr, " " ));
2425 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2426 switch (ELF_ST_BIND(stab[j].st_info)) {
2427 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2428 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2429 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2430 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2432 IF_DEBUG(linker,fprintf(stderr, " " ));
2434 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2438 if (nsymtabs == 0) {
2439 belch("%s: didn't find any symbol tables", oc->fileName);
2448 ocGetNames_ELF ( ObjectCode* oc )
2453 char* ehdrC = (char*)(oc->image);
2454 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2455 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2456 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2458 ASSERT(symhash != NULL);
2461 belch("%s: no strtab", oc->fileName);
2466 for (i = 0; i < ehdr->e_shnum; i++) {
2467 /* Figure out what kind of section it is. Logic derived from
2468 Figure 1.14 ("Special Sections") of the ELF document
2469 ("Portable Formats Specification, Version 1.1"). */
2470 Elf_Shdr hdr = shdr[i];
2471 SectionKind kind = SECTIONKIND_OTHER;
2474 if (hdr.sh_type == SHT_PROGBITS
2475 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2476 /* .text-style section */
2477 kind = SECTIONKIND_CODE_OR_RODATA;
2480 if (hdr.sh_type == SHT_PROGBITS
2481 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2482 /* .data-style section */
2483 kind = SECTIONKIND_RWDATA;
2486 if (hdr.sh_type == SHT_PROGBITS
2487 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2488 /* .rodata-style section */
2489 kind = SECTIONKIND_CODE_OR_RODATA;
2492 if (hdr.sh_type == SHT_NOBITS
2493 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2494 /* .bss-style section */
2495 kind = SECTIONKIND_RWDATA;
2499 if (is_bss && shdr[i].sh_size > 0) {
2500 /* This is a non-empty .bss section. Allocate zeroed space for
2501 it, and set its .sh_offset field such that
2502 ehdrC + .sh_offset == addr_of_zeroed_space. */
2503 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2504 "ocGetNames_ELF(BSS)");
2505 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2507 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2508 zspace, shdr[i].sh_size);
2512 /* fill in the section info */
2513 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2514 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2515 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2516 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2519 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2521 /* copy stuff into this module's object symbol table */
2522 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2523 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2525 oc->n_symbols = nent;
2526 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2527 "ocGetNames_ELF(oc->symbols)");
2529 for (j = 0; j < nent; j++) {
2531 char isLocal = FALSE; /* avoids uninit-var warning */
2533 char* nm = strtab + stab[j].st_name;
2534 int secno = stab[j].st_shndx;
2536 /* Figure out if we want to add it; if so, set ad to its
2537 address. Otherwise leave ad == NULL. */
2539 if (secno == SHN_COMMON) {
2541 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2543 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2544 stab[j].st_size, nm);
2546 /* Pointless to do addProddableBlock() for this area,
2547 since the linker should never poke around in it. */
2550 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2551 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2553 /* and not an undefined symbol */
2554 && stab[j].st_shndx != SHN_UNDEF
2555 /* and not in a "special section" */
2556 && stab[j].st_shndx < SHN_LORESERVE
2558 /* and it's a not a section or string table or anything silly */
2559 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2560 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2561 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2564 /* Section 0 is the undefined section, hence > and not >=. */
2565 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2567 if (shdr[secno].sh_type == SHT_NOBITS) {
2568 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2569 stab[j].st_size, stab[j].st_value, nm);
2572 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2573 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2576 #ifdef ELF_FUNCTION_DESC
2577 /* dlsym() and the initialisation table both give us function
2578 * descriptors, so to be consistent we store function descriptors
2579 * in the symbol table */
2580 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2581 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2583 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2584 ad, oc->fileName, nm ));
2589 /* And the decision is ... */
2593 oc->symbols[j] = nm;
2596 /* Ignore entirely. */
2598 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2602 IF_DEBUG(linker,belch( "skipping `%s'",
2603 strtab + stab[j].st_name ));
2606 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2607 (int)ELF_ST_BIND(stab[j].st_info),
2608 (int)ELF_ST_TYPE(stab[j].st_info),
2609 (int)stab[j].st_shndx,
2610 strtab + stab[j].st_name
2613 oc->symbols[j] = NULL;
2622 /* Do ELF relocations which lack an explicit addend. All x86-linux
2623 relocations appear to be of this form. */
2625 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2626 Elf_Shdr* shdr, int shnum,
2627 Elf_Sym* stab, char* strtab )
2632 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2633 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2634 int target_shndx = shdr[shnum].sh_info;
2635 int symtab_shndx = shdr[shnum].sh_link;
2637 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2638 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2639 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2640 target_shndx, symtab_shndx ));
2642 for (j = 0; j < nent; j++) {
2643 Elf_Addr offset = rtab[j].r_offset;
2644 Elf_Addr info = rtab[j].r_info;
2646 Elf_Addr P = ((Elf_Addr)targ) + offset;
2647 Elf_Word* pP = (Elf_Word*)P;
2652 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2653 j, (void*)offset, (void*)info ));
2655 IF_DEBUG(linker,belch( " ZERO" ));
2658 Elf_Sym sym = stab[ELF_R_SYM(info)];
2659 /* First see if it is a local symbol. */
2660 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2661 /* Yes, so we can get the address directly from the ELF symbol
2663 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2665 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2666 + stab[ELF_R_SYM(info)].st_value);
2669 /* No, so look up the name in our global table. */
2670 symbol = strtab + sym.st_name;
2671 (void*)S = lookupSymbol( symbol );
2674 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2677 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2680 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2681 (void*)P, (void*)S, (void*)A ));
2682 checkProddableBlock ( oc, pP );
2686 switch (ELF_R_TYPE(info)) {
2687 # ifdef i386_TARGET_ARCH
2688 case R_386_32: *pP = value; break;
2689 case R_386_PC32: *pP = value - P; break;
2692 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2693 oc->fileName, ELF_R_TYPE(info));
2701 /* Do ELF relocations for which explicit addends are supplied.
2702 sparc-solaris relocations appear to be of this form. */
2704 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2705 Elf_Shdr* shdr, int shnum,
2706 Elf_Sym* stab, char* strtab )
2711 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2712 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2713 int target_shndx = shdr[shnum].sh_info;
2714 int symtab_shndx = shdr[shnum].sh_link;
2716 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2717 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2718 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2719 target_shndx, symtab_shndx ));
2721 for (j = 0; j < nent; j++) {
2722 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
2723 /* This #ifdef only serves to avoid unused-var warnings. */
2724 Elf_Addr offset = rtab[j].r_offset;
2725 Elf_Addr P = targ + offset;
2727 Elf_Addr info = rtab[j].r_info;
2728 Elf_Addr A = rtab[j].r_addend;
2731 # if defined(sparc_TARGET_ARCH)
2732 Elf_Word* pP = (Elf_Word*)P;
2734 # elif defined(ia64_TARGET_ARCH)
2735 Elf64_Xword *pP = (Elf64_Xword *)P;
2739 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2740 j, (void*)offset, (void*)info,
2743 IF_DEBUG(linker,belch( " ZERO" ));
2746 Elf_Sym sym = stab[ELF_R_SYM(info)];
2747 /* First see if it is a local symbol. */
2748 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2749 /* Yes, so we can get the address directly from the ELF symbol
2751 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2753 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2754 + stab[ELF_R_SYM(info)].st_value);
2755 #ifdef ELF_FUNCTION_DESC
2756 /* Make a function descriptor for this function */
2757 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2758 S = allocateFunctionDesc(S + A);
2763 /* No, so look up the name in our global table. */
2764 symbol = strtab + sym.st_name;
2765 (void*)S = lookupSymbol( symbol );
2767 #ifdef ELF_FUNCTION_DESC
2768 /* If a function, already a function descriptor - we would
2769 have to copy it to add an offset. */
2770 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
2771 belch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
2775 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2778 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2781 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2782 (void*)P, (void*)S, (void*)A ));
2783 /* checkProddableBlock ( oc, (void*)P ); */
2787 switch (ELF_R_TYPE(info)) {
2788 # if defined(sparc_TARGET_ARCH)
2789 case R_SPARC_WDISP30:
2790 w1 = *pP & 0xC0000000;
2791 w2 = (Elf_Word)((value - P) >> 2);
2792 ASSERT((w2 & 0xC0000000) == 0);
2797 w1 = *pP & 0xFFC00000;
2798 w2 = (Elf_Word)(value >> 10);
2799 ASSERT((w2 & 0xFFC00000) == 0);
2805 w2 = (Elf_Word)(value & 0x3FF);
2806 ASSERT((w2 & ~0x3FF) == 0);
2810 /* According to the Sun documentation:
2812 This relocation type resembles R_SPARC_32, except it refers to an
2813 unaligned word. That is, the word to be relocated must be treated
2814 as four separate bytes with arbitrary alignment, not as a word
2815 aligned according to the architecture requirements.
2817 (JRS: which means that freeloading on the R_SPARC_32 case
2818 is probably wrong, but hey ...)
2822 w2 = (Elf_Word)value;
2825 # elif defined(ia64_TARGET_ARCH)
2826 case R_IA64_DIR64LSB:
2827 case R_IA64_FPTR64LSB:
2830 case R_IA64_PCREL64LSB:
2833 case R_IA64_SEGREL64LSB:
2834 addr = findElfSegment(ehdrC, value);
2837 case R_IA64_GPREL22:
2838 ia64_reloc_gprel22(P, value);
2840 case R_IA64_LTOFF22:
2841 case R_IA64_LTOFF22X:
2842 case R_IA64_LTOFF_FPTR22:
2843 addr = allocateGOTEntry(value);
2844 ia64_reloc_gprel22(P, addr);
2846 case R_IA64_PCREL21B:
2847 ia64_reloc_pcrel21(P, S, oc);
2850 /* This goes with R_IA64_LTOFF22X and points to the load to
2851 * convert into a move. We don't implement relaxation. */
2855 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2856 oc->fileName, ELF_R_TYPE(info));
2865 ocResolve_ELF ( ObjectCode* oc )
2869 Elf_Sym* stab = NULL;
2870 char* ehdrC = (char*)(oc->image);
2871 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2872 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2873 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2875 /* first find "the" symbol table */
2876 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2878 /* also go find the string table */
2879 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2881 if (stab == NULL || strtab == NULL) {
2882 belch("%s: can't find string or symbol table", oc->fileName);
2886 /* Process the relocation sections. */
2887 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2889 /* Skip sections called ".rel.stab". These appear to contain
2890 relocation entries that, when done, make the stabs debugging
2891 info point at the right places. We ain't interested in all
2893 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2896 if (shdr[shnum].sh_type == SHT_REL ) {
2897 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2898 shnum, stab, strtab );
2902 if (shdr[shnum].sh_type == SHT_RELA) {
2903 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2904 shnum, stab, strtab );
2909 /* Free the local symbol table; we won't need it again. */
2910 freeHashTable(oc->lochash, NULL);
2918 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2919 * at the front. The following utility functions pack and unpack instructions, and
2920 * take care of the most common relocations.
2923 #ifdef ia64_TARGET_ARCH
2926 ia64_extract_instruction(Elf64_Xword *target)
2929 int slot = (Elf_Addr)target & 3;
2930 (Elf_Addr)target &= ~3;
2938 return ((w1 >> 5) & 0x1ffffffffff);
2940 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2944 barf("ia64_extract_instruction: invalid slot %p", target);
2949 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2951 int slot = (Elf_Addr)target & 3;
2952 (Elf_Addr)target &= ~3;
2957 *target |= value << 5;
2960 *target |= value << 46;
2961 *(target+1) |= value >> 18;
2964 *(target+1) |= value << 23;
2970 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2972 Elf64_Xword instruction;
2973 Elf64_Sxword rel_value;
2975 rel_value = value - gp_val;
2976 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2977 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2979 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2980 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2981 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2982 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2983 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2984 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2988 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2990 Elf64_Xword instruction;
2991 Elf64_Sxword rel_value;
2994 entry = allocatePLTEntry(value, oc);
2996 rel_value = (entry >> 4) - (target >> 4);
2997 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2998 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
3000 instruction = ia64_extract_instruction((Elf64_Xword *)target);
3001 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
3002 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
3003 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3010 /* --------------------------------------------------------------------------
3012 * ------------------------------------------------------------------------*/
3014 #if defined(OBJFORMAT_MACHO)
3017 Support for MachO linking on Darwin/MacOS X on PowerPC chips
3018 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3020 I hereby formally apologize for the hackish nature of this code.
3021 Things that need to be done:
3022 *) implement ocVerifyImage_MachO
3023 *) add still more sanity checks.
3028 ocAllocateJumpIslands_MachO
3030 Allocate additional space at the end of the object file image to make room
3033 PowerPC relative branch instructions have a 24 bit displacement field.
3034 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
3035 If a particular imported symbol is outside this range, we have to redirect
3036 the jump to a short piece of new code that just loads the 32bit absolute
3037 address and jumps there.
3038 This function just allocates space for one 16 byte jump island for every
3039 undefined symbol in the object file. The code for the islands is filled in by
3040 makeJumpIsland below.
3043 static const int islandSize = 16;
3045 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3047 char *image = (char*) oc->image;
3048 struct mach_header *header = (struct mach_header*) image;
3049 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3052 for(i=0;i<header->ncmds;i++)
3054 if(lc->cmd == LC_DYSYMTAB)
3056 struct dysymtab_command *dsymLC = (struct dysymtab_command*) lc;
3057 unsigned long nundefsym = dsymLC->nundefsym;
3058 oc->island_start_symbol = dsymLC->iundefsym;
3059 oc->n_islands = nundefsym;
3064 #error ocAllocateJumpIslands_MachO doesnt want USE_MMAP to be defined
3066 oc->image = stgReallocBytes(
3067 image, oc->fileSize + islandSize * nundefsym,
3068 "ocAllocateJumpIslands_MachO");
3070 oc->jump_islands = oc->image + oc->fileSize;
3071 memset(oc->jump_islands, 0, islandSize * nundefsym);
3074 break; // there can be only one LC_DSYMTAB
3076 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3081 static int ocVerifyImage_MachO(ObjectCode* oc)
3083 // FIXME: do some verifying here
3087 static int resolveImports(
3090 struct symtab_command *symLC,
3091 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3092 unsigned long *indirectSyms,
3093 struct nlist *nlist)
3097 for(i=0;i*4<sect->size;i++)
3099 // according to otool, reserved1 contains the first index into the indirect symbol table
3100 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3101 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3104 if((symbol->n_type & N_TYPE) == N_UNDF
3105 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3106 addr = (void*) (symbol->n_value);
3107 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3110 addr = lookupSymbol(nm);
3113 belch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3117 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3118 ((void**)(image + sect->offset))[i] = addr;
3124 static void* makeJumpIsland(
3126 unsigned long symbolNumber,
3129 if(symbolNumber < oc->island_start_symbol ||
3130 symbolNumber - oc->island_start_symbol > oc->n_islands)
3132 symbolNumber -= oc->island_start_symbol;
3134 void *island = (void*) ((char*)oc->jump_islands + islandSize * symbolNumber);
3135 unsigned long *p = (unsigned long*) island;
3137 // lis r12, hi16(target)
3138 *p++ = 0x3d800000 | ( ((unsigned long) target) >> 16 );
3139 // ori r12, r12, lo16(target)
3140 *p++ = 0x618c0000 | ( ((unsigned long) target) & 0xFFFF );
3146 return (void*) island;
3149 static char* relocateAddress(
3152 struct section* sections,
3153 unsigned long address)
3156 for(i = 0; i < nSections; i++)
3158 if(sections[i].addr <= address
3159 && address < sections[i].addr + sections[i].size)
3161 return oc->image + sections[i].offset + address - sections[i].addr;
3164 barf("Invalid Mach-O file:"
3165 "Address out of bounds while relocating object file");
3169 static int relocateSection(
3172 struct symtab_command *symLC, struct nlist *nlist,
3173 int nSections, struct section* sections, struct section *sect)
3175 struct relocation_info *relocs;
3178 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3180 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3184 relocs = (struct relocation_info*) (image + sect->reloff);
3188 if(relocs[i].r_address & R_SCATTERED)
3190 struct scattered_relocation_info *scat =
3191 (struct scattered_relocation_info*) &relocs[i];
3195 if(scat->r_length == 2)
3197 unsigned long word = 0;
3198 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3199 checkProddableBlock(oc,wordPtr);
3201 // Step 1: Figure out what the relocated value should be
3202 if(scat->r_type == GENERIC_RELOC_VANILLA)
3204 word = scat->r_value + sect->offset + ((long) image);
3206 else if(scat->r_type == PPC_RELOC_SECTDIFF
3207 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3208 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3209 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3211 struct scattered_relocation_info *pair =
3212 (struct scattered_relocation_info*) &relocs[i+1];
3214 if(!pair->r_scattered || pair->r_type != PPC_RELOC_PAIR)
3215 barf("Invalid Mach-O file: "
3216 "PPC_RELOC_*_SECTDIFF not followed by PPC_RELOC_PAIR");
3218 word = (unsigned long)
3219 (relocateAddress(oc, nSections, sections, scat->r_value)
3220 - relocateAddress(oc, nSections, sections, pair->r_value));
3224 continue; // ignore the others
3226 if(scat->r_type == GENERIC_RELOC_VANILLA
3227 || scat->r_type == PPC_RELOC_SECTDIFF)
3231 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF)
3233 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3235 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF)
3237 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3239 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3241 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3242 + ((word & (1<<15)) ? 1 : 0);
3247 continue; // FIXME: I hope it's OK to ignore all the others.
3251 struct relocation_info *reloc = &relocs[i];
3252 if(reloc->r_pcrel && !reloc->r_extern)
3255 if(reloc->r_length == 2)
3257 unsigned long word = 0;
3258 unsigned long jumpIsland = 0;
3259 long offsetToJumpIsland;
3261 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3262 checkProddableBlock(oc,wordPtr);
3264 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3268 else if(reloc->r_type == PPC_RELOC_LO16)
3270 word = ((unsigned short*) wordPtr)[1];
3271 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3273 else if(reloc->r_type == PPC_RELOC_HI16)
3275 word = ((unsigned short*) wordPtr)[1] << 16;
3276 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3278 else if(reloc->r_type == PPC_RELOC_HA16)
3280 word = ((unsigned short*) wordPtr)[1] << 16;
3281 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3283 else if(reloc->r_type == PPC_RELOC_BR24)
3286 word = (word & 0x03FFFFFC) | (word & 0x02000000) ? 0xFC000000 : 0;
3290 if(!reloc->r_extern)
3293 sections[reloc->r_symbolnum-1].offset
3294 - sections[reloc->r_symbolnum-1].addr
3301 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3302 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3303 word = (unsigned long) (lookupSymbol(nm));
3306 belch("\nunknown symbol `%s'", nm);
3312 jumpIsland = (long) makeJumpIsland(oc,reloc->r_symbolnum,(void*)word);
3313 word -= ((long)image) + sect->offset + reloc->r_address;
3316 offsetToJumpIsland = jumpIsland
3317 - (((long)image) + sect->offset + reloc->r_address);
3322 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3327 else if(reloc->r_type == PPC_RELOC_LO16)
3329 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3332 else if(reloc->r_type == PPC_RELOC_HI16)
3334 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3337 else if(reloc->r_type == PPC_RELOC_HA16)
3339 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3340 + ((word & (1<<15)) ? 1 : 0);
3343 else if(reloc->r_type == PPC_RELOC_BR24)
3345 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3347 // The branch offset is too large.
3348 // Therefore, we try to use a jump island.
3350 barf("unconditional relative branch out of range: "
3351 "no jump island available");
3353 word = offsetToJumpIsland;
3354 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3355 barf("unconditional relative branch out of range: "
3356 "jump island out of range");
3358 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3362 barf("\nunknown relocation %d",reloc->r_type);
3369 static int ocGetNames_MachO(ObjectCode* oc)
3371 char *image = (char*) oc->image;
3372 struct mach_header *header = (struct mach_header*) image;
3373 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3374 unsigned i,curSymbol;
3375 struct segment_command *segLC = NULL;
3376 struct section *sections;
3377 struct symtab_command *symLC = NULL;
3378 struct dysymtab_command *dsymLC = NULL;
3379 struct nlist *nlist;
3380 unsigned long commonSize = 0;
3381 char *commonStorage = NULL;
3382 unsigned long commonCounter;
3384 for(i=0;i<header->ncmds;i++)
3386 if(lc->cmd == LC_SEGMENT)
3387 segLC = (struct segment_command*) lc;
3388 else if(lc->cmd == LC_SYMTAB)
3389 symLC = (struct symtab_command*) lc;
3390 else if(lc->cmd == LC_DYSYMTAB)
3391 dsymLC = (struct dysymtab_command*) lc;
3392 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3395 sections = (struct section*) (segLC+1);
3396 nlist = (struct nlist*) (image + symLC->symoff);
3398 for(i=0;i<segLC->nsects;i++)
3400 if(sections[i].size == 0)
3403 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
3405 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
3406 "ocGetNames_MachO(common symbols)");
3407 sections[i].offset = zeroFillArea - image;
3410 if(!strcmp(sections[i].sectname,"__text"))
3411 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3412 (void*) (image + sections[i].offset),
3413 (void*) (image + sections[i].offset + sections[i].size));
3414 else if(!strcmp(sections[i].sectname,"__const"))
3415 addSection(oc, SECTIONKIND_RWDATA,
3416 (void*) (image + sections[i].offset),
3417 (void*) (image + sections[i].offset + sections[i].size));
3418 else if(!strcmp(sections[i].sectname,"__data"))
3419 addSection(oc, SECTIONKIND_RWDATA,
3420 (void*) (image + sections[i].offset),
3421 (void*) (image + sections[i].offset + sections[i].size));
3422 else if(!strcmp(sections[i].sectname,"__bss")
3423 || !strcmp(sections[i].sectname,"__common"))
3424 addSection(oc, SECTIONKIND_RWDATA,
3425 (void*) (image + sections[i].offset),
3426 (void*) (image + sections[i].offset + sections[i].size));
3428 addProddableBlock(oc, (void*) (image + sections[i].offset),
3432 // count external symbols defined here
3434 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3436 if((nlist[i].n_type & N_TYPE) == N_SECT)
3439 for(i=0;i<symLC->nsyms;i++)
3441 if((nlist[i].n_type & N_TYPE) == N_UNDF
3442 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3444 commonSize += nlist[i].n_value;
3448 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3449 "ocGetNames_MachO(oc->symbols)");
3451 // insert symbols into hash table
3452 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3454 if((nlist[i].n_type & N_TYPE) == N_SECT)
3456 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3457 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3458 sections[nlist[i].n_sect-1].offset
3459 - sections[nlist[i].n_sect-1].addr
3460 + nlist[i].n_value);
3461 oc->symbols[curSymbol++] = nm;
3465 // insert local symbols into lochash
3466 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3468 if((nlist[i].n_type & N_TYPE) == N_SECT)
3470 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3471 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3472 sections[nlist[i].n_sect-1].offset
3473 - sections[nlist[i].n_sect-1].addr
3474 + nlist[i].n_value);
3479 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3480 commonCounter = (unsigned long)commonStorage;
3481 for(i=0;i<symLC->nsyms;i++)
3483 if((nlist[i].n_type & N_TYPE) == N_UNDF
3484 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3486 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3487 unsigned long sz = nlist[i].n_value;
3489 nlist[i].n_value = commonCounter;
3491 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3492 oc->symbols[curSymbol++] = nm;
3494 commonCounter += sz;
3500 static int ocResolve_MachO(ObjectCode* oc)
3502 char *image = (char*) oc->image;
3503 struct mach_header *header = (struct mach_header*) image;
3504 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3506 struct segment_command *segLC = NULL;
3507 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3508 struct symtab_command *symLC = NULL;
3509 struct dysymtab_command *dsymLC = NULL;
3510 struct nlist *nlist;
3511 unsigned long *indirectSyms;
3513 for(i=0;i<header->ncmds;i++)
3515 if(lc->cmd == LC_SEGMENT)
3516 segLC = (struct segment_command*) lc;
3517 else if(lc->cmd == LC_SYMTAB)
3518 symLC = (struct symtab_command*) lc;
3519 else if(lc->cmd == LC_DYSYMTAB)
3520 dsymLC = (struct dysymtab_command*) lc;
3521 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3524 sections = (struct section*) (segLC+1);
3525 nlist = (struct nlist*) (image + symLC->symoff);
3527 for(i=0;i<segLC->nsects;i++)
3529 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3530 la_ptrs = §ions[i];
3531 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3532 nl_ptrs = §ions[i];
3535 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3538 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
3541 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
3544 for(i=0;i<segLC->nsects;i++)
3546 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
3550 /* Free the local symbol table; we won't need it again. */
3551 freeHashTable(oc->lochash, NULL);
3555 Flush the data & instruction caches.
3556 Because the PPC has split data/instruction caches, we have to
3557 do that whenever we modify code at runtime.
3560 int n = (oc->fileSize + islandSize * oc->n_islands) / 4;
3561 unsigned long *p = (unsigned long*)oc->image;
3564 __asm__ volatile ("dcbf 0,%0\n\tsync\n\ticbi 0,%0"
3568 __asm__ volatile ("sync\n\tisync");
3574 * The Mach-O object format uses leading underscores. But not everywhere.
3575 * There is a small number of runtime support functions defined in
3576 * libcc_dynamic.a whose name does not have a leading underscore.
3577 * As a consequence, we can't get their address from C code.
3578 * We have to use inline assembler just to take the address of a function.
3582 static void machoInitSymbolsWithoutUnderscore()
3588 __asm__ ("lis %0,hi16(" #x ")\n\tori %0,%0,lo16(" #x ")" : "=r" (p)); \
3589 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, p);
3591 RTS_MACHO_NOUNDERLINE_SYMBOLS