1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.145 2004/02/15 13:29:44 krasimir 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) \
345 SymX(MallocFailHook) \
347 SymX(OutOfHeapHook) \
348 SymX(StackOverflowHook) \
349 SymX(__encodeDouble) \
350 SymX(__encodeFloat) \
353 SymX(__gmpz_cmp_si) \
354 SymX(__gmpz_cmp_ui) \
355 SymX(__gmpz_get_si) \
356 SymX(__gmpz_get_ui) \
357 SymX(__int_encodeDouble) \
358 SymX(__int_encodeFloat) \
359 SymX(andIntegerzh_fast) \
360 SymX(blockAsyncExceptionszh_fast) \
363 SymX(complementIntegerzh_fast) \
364 SymX(cmpIntegerzh_fast) \
365 SymX(cmpIntegerIntzh_fast) \
366 SymX(createAdjustor) \
367 SymX(decodeDoublezh_fast) \
368 SymX(decodeFloatzh_fast) \
371 SymX(deRefWeakzh_fast) \
372 SymX(deRefStablePtrzh_fast) \
373 SymX(divExactIntegerzh_fast) \
374 SymX(divModIntegerzh_fast) \
377 SymX(forkOS_createThread) \
378 SymX(freeHaskellFunctionPtr) \
379 SymX(freeStablePtr) \
380 SymX(gcdIntegerzh_fast) \
381 SymX(gcdIntegerIntzh_fast) \
382 SymX(gcdIntzh_fast) \
386 SymX(int2Integerzh_fast) \
387 SymX(integer2Intzh_fast) \
388 SymX(integer2Wordzh_fast) \
389 SymX(isCurrentThreadBoundzh_fast) \
390 SymX(isDoubleDenormalized) \
391 SymX(isDoubleInfinite) \
393 SymX(isDoubleNegativeZero) \
394 SymX(isEmptyMVarzh_fast) \
395 SymX(isFloatDenormalized) \
396 SymX(isFloatInfinite) \
398 SymX(isFloatNegativeZero) \
399 SymX(killThreadzh_fast) \
400 SymX(makeStablePtrzh_fast) \
401 SymX(minusIntegerzh_fast) \
402 SymX(mkApUpd0zh_fast) \
403 SymX(myThreadIdzh_fast) \
404 SymX(labelThreadzh_fast) \
405 SymX(newArrayzh_fast) \
406 SymX(newBCOzh_fast) \
407 SymX(newByteArrayzh_fast) \
408 SymX_redirect(newCAF, newDynCAF) \
409 SymX(newMVarzh_fast) \
410 SymX(newMutVarzh_fast) \
411 SymX(atomicModifyMutVarzh_fast) \
412 SymX(newPinnedByteArrayzh_fast) \
413 SymX(orIntegerzh_fast) \
415 SymX(performMajorGC) \
416 SymX(plusIntegerzh_fast) \
419 SymX(putMVarzh_fast) \
420 SymX(quotIntegerzh_fast) \
421 SymX(quotRemIntegerzh_fast) \
423 SymX(raiseIOzh_fast) \
424 SymX(remIntegerzh_fast) \
425 SymX(resetNonBlockingFd) \
428 SymX(rts_checkSchedStatus) \
431 SymX(rts_evalLazyIO) \
432 SymX(rts_evalStableIO) \
436 SymX(rts_getDouble) \
441 SymX(rts_getFunPtr) \
442 SymX(rts_getStablePtr) \
443 SymX(rts_getThreadId) \
445 SymX(rts_getWord32) \
458 SymX(rts_mkStablePtr) \
466 SymX(rtsSupportsBoundThreads) \
468 SymX(__hscore_get_saved_termios) \
469 SymX(__hscore_set_saved_termios) \
471 SymX(startupHaskell) \
472 SymX(shutdownHaskell) \
473 SymX(shutdownHaskellAndExit) \
474 SymX(stable_ptr_table) \
475 SymX(stackOverflow) \
476 SymX(stg_CAF_BLACKHOLE_info) \
477 SymX(stg_BLACKHOLE_BQ_info) \
478 SymX(awakenBlockedQueue) \
479 SymX(stg_CHARLIKE_closure) \
480 SymX(stg_EMPTY_MVAR_info) \
481 SymX(stg_IND_STATIC_info) \
482 SymX(stg_INTLIKE_closure) \
483 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
484 SymX(stg_WEAK_info) \
485 SymX(stg_ap_v_info) \
486 SymX(stg_ap_f_info) \
487 SymX(stg_ap_d_info) \
488 SymX(stg_ap_l_info) \
489 SymX(stg_ap_n_info) \
490 SymX(stg_ap_p_info) \
491 SymX(stg_ap_pv_info) \
492 SymX(stg_ap_pp_info) \
493 SymX(stg_ap_ppv_info) \
494 SymX(stg_ap_ppp_info) \
495 SymX(stg_ap_pppp_info) \
496 SymX(stg_ap_ppppp_info) \
497 SymX(stg_ap_pppppp_info) \
498 SymX(stg_ap_ppppppp_info) \
506 SymX(stg_ap_pv_ret) \
507 SymX(stg_ap_pp_ret) \
508 SymX(stg_ap_ppv_ret) \
509 SymX(stg_ap_ppp_ret) \
510 SymX(stg_ap_pppp_ret) \
511 SymX(stg_ap_ppppp_ret) \
512 SymX(stg_ap_pppppp_ret) \
513 SymX(stg_ap_ppppppp_ret) \
514 SymX(stg_ap_1_upd_info) \
515 SymX(stg_ap_2_upd_info) \
516 SymX(stg_ap_3_upd_info) \
517 SymX(stg_ap_4_upd_info) \
518 SymX(stg_ap_5_upd_info) \
519 SymX(stg_ap_6_upd_info) \
520 SymX(stg_ap_7_upd_info) \
521 SymX(stg_ap_8_upd_info) \
523 SymX(stg_sel_0_upd_info) \
524 SymX(stg_sel_10_upd_info) \
525 SymX(stg_sel_11_upd_info) \
526 SymX(stg_sel_12_upd_info) \
527 SymX(stg_sel_13_upd_info) \
528 SymX(stg_sel_14_upd_info) \
529 SymX(stg_sel_15_upd_info) \
530 SymX(stg_sel_1_upd_info) \
531 SymX(stg_sel_2_upd_info) \
532 SymX(stg_sel_3_upd_info) \
533 SymX(stg_sel_4_upd_info) \
534 SymX(stg_sel_5_upd_info) \
535 SymX(stg_sel_6_upd_info) \
536 SymX(stg_sel_7_upd_info) \
537 SymX(stg_sel_8_upd_info) \
538 SymX(stg_sel_9_upd_info) \
539 SymX(stg_upd_frame_info) \
540 SymX(suspendThread) \
541 SymX(takeMVarzh_fast) \
542 SymX(timesIntegerzh_fast) \
543 SymX(tryPutMVarzh_fast) \
544 SymX(tryTakeMVarzh_fast) \
545 SymX(unblockAsyncExceptionszh_fast) \
546 SymX(unsafeThawArrayzh_fast) \
547 SymX(waitReadzh_fast) \
548 SymX(waitWritezh_fast) \
549 SymX(word2Integerzh_fast) \
550 SymX(xorIntegerzh_fast) \
553 #ifdef SUPPORT_LONG_LONGS
554 #define RTS_LONG_LONG_SYMS \
555 SymX(int64ToIntegerzh_fast) \
556 SymX(word64ToIntegerzh_fast)
558 #define RTS_LONG_LONG_SYMS /* nothing */
561 // 64-bit support functions in libgcc.a
562 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
563 #define RTS_LIBGCC_SYMBOLS \
572 #elif defined(ia64_TARGET_ARCH)
573 #define RTS_LIBGCC_SYMBOLS \
581 #define RTS_LIBGCC_SYMBOLS
584 #ifdef darwin_TARGET_OS
585 // Symbols that don't have a leading underscore
586 // on Mac OS X. They have to receive special treatment,
587 // see machoInitSymbolsWithoutUnderscore()
588 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
593 /* entirely bogus claims about types of these symbols */
594 #define Sym(vvv) extern void vvv(void);
595 #define SymX(vvv) /**/
596 #define SymX_redirect(vvv,xxx) /**/
599 RTS_POSIX_ONLY_SYMBOLS
600 RTS_MINGW_ONLY_SYMBOLS
601 RTS_CYGWIN_ONLY_SYMBOLS
607 #ifdef LEADING_UNDERSCORE
608 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
610 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
613 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
615 #define SymX(vvv) Sym(vvv)
617 // SymX_redirect allows us to redirect references to one symbol to
618 // another symbol. See newCAF/newDynCAF for an example.
619 #define SymX_redirect(vvv,xxx) \
620 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
623 static RtsSymbolVal rtsSyms[] = {
626 RTS_POSIX_ONLY_SYMBOLS
627 RTS_MINGW_ONLY_SYMBOLS
628 RTS_CYGWIN_ONLY_SYMBOLS
630 { 0, 0 } /* sentinel */
633 /* -----------------------------------------------------------------------------
634 * Insert symbols into hash tables, checking for duplicates.
636 static void ghciInsertStrHashTable ( char* obj_name,
642 if (lookupHashTable(table, (StgWord)key) == NULL)
644 insertStrHashTable(table, (StgWord)key, data);
649 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
651 "whilst processing object file\n"
653 "This could be caused by:\n"
654 " * Loading two different object files which export the same symbol\n"
655 " * Specifying the same object file twice on the GHCi command line\n"
656 " * An incorrect `package.conf' entry, causing some object to be\n"
658 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
667 /* -----------------------------------------------------------------------------
668 * initialize the object linker
672 static int linker_init_done = 0 ;
674 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
675 static void *dl_prog_handle;
683 /* Make initLinker idempotent, so we can call it
684 before evey relevant operation; that means we
685 don't need to initialise the linker separately */
686 if (linker_init_done == 1) { return; } else {
687 linker_init_done = 1;
690 symhash = allocStrHashTable();
692 /* populate the symbol table with stuff from the RTS */
693 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
694 ghciInsertStrHashTable("(GHCi built-in symbols)",
695 symhash, sym->lbl, sym->addr);
697 # if defined(OBJFORMAT_MACHO)
698 machoInitSymbolsWithoutUnderscore();
701 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
702 # if defined(RTLD_DEFAULT)
703 dl_prog_handle = RTLD_DEFAULT;
705 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
706 # endif // RTLD_DEFAULT
710 /* -----------------------------------------------------------------------------
711 * Loading DLL or .so dynamic libraries
712 * -----------------------------------------------------------------------------
714 * Add a DLL from which symbols may be found. In the ELF case, just
715 * do RTLD_GLOBAL-style add, so no further messing around needs to
716 * happen in order that symbols in the loaded .so are findable --
717 * lookupSymbol() will subsequently see them by dlsym on the program's
718 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
720 * In the PEi386 case, open the DLLs and put handles to them in a
721 * linked list. When looking for a symbol, try all handles in the
722 * list. This means that we need to load even DLLs that are guaranteed
723 * to be in the ghc.exe image already, just so we can get a handle
724 * to give to loadSymbol, so that we can find the symbols. For such
725 * libraries, the LoadLibrary call should be a no-op except for returning
730 #if defined(OBJFORMAT_PEi386)
731 /* A record for storing handles into DLLs. */
736 struct _OpenedDLL* next;
741 /* A list thereof. */
742 static OpenedDLL* opened_dlls = NULL;
746 addDLL( char *dll_name )
748 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
749 /* ------------------- ELF DLL loader ------------------- */
755 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
758 /* dlopen failed; return a ptr to the error msg. */
760 if (errmsg == NULL) errmsg = "addDLL: unknown error";
767 # elif defined(OBJFORMAT_PEi386)
768 /* ------------------- Win32 DLL loader ------------------- */
776 /* fprintf(stderr, "\naddDLL; dll_name = `%s'\n", dll_name); */
778 /* See if we've already got it, and ignore if so. */
779 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
780 if (0 == strcmp(o_dll->name, dll_name))
784 /* The file name has no suffix (yet) so that we can try
785 both foo.dll and foo.drv
787 The documentation for LoadLibrary says:
788 If no file name extension is specified in the lpFileName
789 parameter, the default library extension .dll is
790 appended. However, the file name string can include a trailing
791 point character (.) to indicate that the module name has no
794 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
795 sprintf(buf, "%s.DLL", dll_name);
796 instance = LoadLibrary(buf);
797 if (instance == NULL) {
798 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
799 instance = LoadLibrary(buf);
800 if (instance == NULL) {
803 /* LoadLibrary failed; return a ptr to the error msg. */
804 return "addDLL: unknown error";
809 /* Add this DLL to the list of DLLs in which to search for symbols. */
810 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
811 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
812 strcpy(o_dll->name, dll_name);
813 o_dll->instance = instance;
814 o_dll->next = opened_dlls;
819 barf("addDLL: not implemented on this platform");
823 /* -----------------------------------------------------------------------------
824 * lookup a symbol in the hash table
827 lookupSymbol( char *lbl )
831 ASSERT(symhash != NULL);
832 val = lookupStrHashTable(symhash, lbl);
835 # if defined(OBJFORMAT_ELF)
836 return dlsym(dl_prog_handle, lbl);
837 # elif defined(OBJFORMAT_MACHO)
838 if(NSIsSymbolNameDefined(lbl)) {
839 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
840 return NSAddressOfSymbol(symbol);
844 # elif defined(OBJFORMAT_PEi386)
847 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
848 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
850 /* HACK: if the name has an initial underscore, try stripping
851 it off & look that up first. I've yet to verify whether there's
852 a Rule that governs whether an initial '_' *should always* be
853 stripped off when mapping from import lib name to the DLL name.
855 sym = GetProcAddress(o_dll->instance, (lbl+1));
857 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
861 sym = GetProcAddress(o_dll->instance, lbl);
863 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
878 __attribute((unused))
880 lookupLocalSymbol( ObjectCode* oc, char *lbl )
884 val = lookupStrHashTable(oc->lochash, lbl);
894 /* -----------------------------------------------------------------------------
895 * Debugging aid: look in GHCi's object symbol tables for symbols
896 * within DELTA bytes of the specified address, and show their names.
899 void ghci_enquire ( char* addr );
901 void ghci_enquire ( char* addr )
906 const int DELTA = 64;
911 for (oc = objects; oc; oc = oc->next) {
912 for (i = 0; i < oc->n_symbols; i++) {
913 sym = oc->symbols[i];
914 if (sym == NULL) continue;
915 // fprintf(stderr, "enquire %p %p\n", sym, oc->lochash);
917 if (oc->lochash != NULL) {
918 a = lookupStrHashTable(oc->lochash, sym);
921 a = lookupStrHashTable(symhash, sym);
924 // fprintf(stderr, "ghci_enquire: can't find %s\n", sym);
926 else if (addr-DELTA <= a && a <= addr+DELTA) {
927 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
934 #ifdef ia64_TARGET_ARCH
935 static unsigned int PLTSize(void);
938 /* -----------------------------------------------------------------------------
939 * Load an obj (populate the global symbol table, but don't resolve yet)
941 * Returns: 1 if ok, 0 on error.
944 loadObj( char *path )
958 /* fprintf(stderr, "loadObj %s\n", path ); */
960 /* Check that we haven't already loaded this object. Don't give up
961 at this stage; ocGetNames_* will barf later. */
965 for (o = objects; o; o = o->next) {
966 if (0 == strcmp(o->fileName, path))
972 "GHCi runtime linker: warning: looks like you're trying to load the\n"
973 "same object file twice:\n"
975 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
981 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
983 # if defined(OBJFORMAT_ELF)
984 oc->formatName = "ELF";
985 # elif defined(OBJFORMAT_PEi386)
986 oc->formatName = "PEi386";
987 # elif defined(OBJFORMAT_MACHO)
988 oc->formatName = "Mach-O";
991 barf("loadObj: not implemented on this platform");
995 if (r == -1) { return 0; }
997 /* sigh, strdup() isn't a POSIX function, so do it the long way */
998 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
999 strcpy(oc->fileName, path);
1001 oc->fileSize = st.st_size;
1003 oc->sections = NULL;
1004 oc->lochash = allocStrHashTable();
1005 oc->proddables = NULL;
1007 /* chain it onto the list of objects */
1012 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1014 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1016 fd = open(path, O_RDONLY);
1018 barf("loadObj: can't open `%s'", path);
1020 pagesize = getpagesize();
1022 #ifdef ia64_TARGET_ARCH
1023 /* The PLT needs to be right before the object */
1024 n = ROUND_UP(PLTSize(), pagesize);
1025 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1026 if (oc->plt == MAP_FAILED)
1027 barf("loadObj: can't allocate PLT");
1030 map_addr = oc->plt + n;
1033 n = ROUND_UP(oc->fileSize, pagesize);
1034 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1035 if (oc->image == MAP_FAILED)
1036 barf("loadObj: can't map `%s'", path);
1040 #else /* !USE_MMAP */
1042 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
1044 /* load the image into memory */
1045 f = fopen(path, "rb");
1047 barf("loadObj: can't read `%s'", path);
1049 n = fread ( oc->image, 1, oc->fileSize, f );
1050 if (n != oc->fileSize)
1051 barf("loadObj: error whilst reading `%s'", path);
1055 #endif /* USE_MMAP */
1057 # if defined(OBJFORMAT_MACHO)
1058 r = ocAllocateJumpIslands_MachO ( oc );
1059 if (!r) { return r; }
1062 /* verify the in-memory image */
1063 # if defined(OBJFORMAT_ELF)
1064 r = ocVerifyImage_ELF ( oc );
1065 # elif defined(OBJFORMAT_PEi386)
1066 r = ocVerifyImage_PEi386 ( oc );
1067 # elif defined(OBJFORMAT_MACHO)
1068 r = ocVerifyImage_MachO ( oc );
1070 barf("loadObj: no verify method");
1072 if (!r) { return r; }
1074 /* build the symbol list for this image */
1075 # if defined(OBJFORMAT_ELF)
1076 r = ocGetNames_ELF ( oc );
1077 # elif defined(OBJFORMAT_PEi386)
1078 r = ocGetNames_PEi386 ( oc );
1079 # elif defined(OBJFORMAT_MACHO)
1080 r = ocGetNames_MachO ( oc );
1082 barf("loadObj: no getNames method");
1084 if (!r) { return r; }
1086 /* loaded, but not resolved yet */
1087 oc->status = OBJECT_LOADED;
1092 /* -----------------------------------------------------------------------------
1093 * resolve all the currently unlinked objects in memory
1095 * Returns: 1 if ok, 0 on error.
1105 for (oc = objects; oc; oc = oc->next) {
1106 if (oc->status != OBJECT_RESOLVED) {
1107 # if defined(OBJFORMAT_ELF)
1108 r = ocResolve_ELF ( oc );
1109 # elif defined(OBJFORMAT_PEi386)
1110 r = ocResolve_PEi386 ( oc );
1111 # elif defined(OBJFORMAT_MACHO)
1112 r = ocResolve_MachO ( oc );
1114 barf("resolveObjs: not implemented on this platform");
1116 if (!r) { return r; }
1117 oc->status = OBJECT_RESOLVED;
1123 /* -----------------------------------------------------------------------------
1124 * delete an object from the pool
1127 unloadObj( char *path )
1129 ObjectCode *oc, *prev;
1131 ASSERT(symhash != NULL);
1132 ASSERT(objects != NULL);
1137 for (oc = objects; oc; prev = oc, oc = oc->next) {
1138 if (!strcmp(oc->fileName,path)) {
1140 /* Remove all the mappings for the symbols within this
1145 for (i = 0; i < oc->n_symbols; i++) {
1146 if (oc->symbols[i] != NULL) {
1147 removeStrHashTable(symhash, oc->symbols[i], NULL);
1155 prev->next = oc->next;
1158 /* We're going to leave this in place, in case there are
1159 any pointers from the heap into it: */
1160 /* stgFree(oc->image); */
1161 stgFree(oc->fileName);
1162 stgFree(oc->symbols);
1163 stgFree(oc->sections);
1164 /* The local hash table should have been freed at the end
1165 of the ocResolve_ call on it. */
1166 ASSERT(oc->lochash == NULL);
1172 belch("unloadObj: can't find `%s' to unload", path);
1176 /* -----------------------------------------------------------------------------
1177 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1178 * which may be prodded during relocation, and abort if we try and write
1179 * outside any of these.
1181 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1184 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1185 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1189 pb->next = oc->proddables;
1190 oc->proddables = pb;
1193 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1196 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1197 char* s = (char*)(pb->start);
1198 char* e = s + pb->size - 1;
1199 char* a = (char*)addr;
1200 /* Assumes that the biggest fixup involves a 4-byte write. This
1201 probably needs to be changed to 8 (ie, +7) on 64-bit
1203 if (a >= s && (a+3) <= e) return;
1205 barf("checkProddableBlock: invalid fixup in runtime linker");
1208 /* -----------------------------------------------------------------------------
1209 * Section management.
1211 static void addSection ( ObjectCode* oc, SectionKind kind,
1212 void* start, void* end )
1214 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1218 s->next = oc->sections;
1221 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1222 start, ((char*)end)-1, end - start + 1, kind );
1228 /* --------------------------------------------------------------------------
1229 * PEi386 specifics (Win32 targets)
1230 * ------------------------------------------------------------------------*/
1232 /* The information for this linker comes from
1233 Microsoft Portable Executable
1234 and Common Object File Format Specification
1235 revision 5.1 January 1998
1236 which SimonM says comes from the MS Developer Network CDs.
1238 It can be found there (on older CDs), but can also be found
1241 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1243 (this is Rev 6.0 from February 1999).
1245 Things move, so if that fails, try searching for it via
1247 http://www.google.com/search?q=PE+COFF+specification
1249 The ultimate reference for the PE format is the Winnt.h
1250 header file that comes with the Platform SDKs; as always,
1251 implementations will drift wrt their documentation.
1253 A good background article on the PE format is Matt Pietrek's
1254 March 1994 article in Microsoft System Journal (MSJ)
1255 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1256 Win32 Portable Executable File Format." The info in there
1257 has recently been updated in a two part article in
1258 MSDN magazine, issues Feb and March 2002,
1259 "Inside Windows: An In-Depth Look into the Win32 Portable
1260 Executable File Format"
1262 John Levine's book "Linkers and Loaders" contains useful
1267 #if defined(OBJFORMAT_PEi386)
1271 typedef unsigned char UChar;
1272 typedef unsigned short UInt16;
1273 typedef unsigned int UInt32;
1280 UInt16 NumberOfSections;
1281 UInt32 TimeDateStamp;
1282 UInt32 PointerToSymbolTable;
1283 UInt32 NumberOfSymbols;
1284 UInt16 SizeOfOptionalHeader;
1285 UInt16 Characteristics;
1289 #define sizeof_COFF_header 20
1296 UInt32 VirtualAddress;
1297 UInt32 SizeOfRawData;
1298 UInt32 PointerToRawData;
1299 UInt32 PointerToRelocations;
1300 UInt32 PointerToLinenumbers;
1301 UInt16 NumberOfRelocations;
1302 UInt16 NumberOfLineNumbers;
1303 UInt32 Characteristics;
1307 #define sizeof_COFF_section 40
1314 UInt16 SectionNumber;
1317 UChar NumberOfAuxSymbols;
1321 #define sizeof_COFF_symbol 18
1326 UInt32 VirtualAddress;
1327 UInt32 SymbolTableIndex;
1332 #define sizeof_COFF_reloc 10
1335 /* From PE spec doc, section 3.3.2 */
1336 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1337 windows.h -- for the same purpose, but I want to know what I'm
1339 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1340 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1341 #define MYIMAGE_FILE_DLL 0x2000
1342 #define MYIMAGE_FILE_SYSTEM 0x1000
1343 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1344 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1345 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1347 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1348 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1349 #define MYIMAGE_SYM_CLASS_STATIC 3
1350 #define MYIMAGE_SYM_UNDEFINED 0
1352 /* From PE spec doc, section 4.1 */
1353 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1354 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1355 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1357 /* From PE spec doc, section 5.2.1 */
1358 #define MYIMAGE_REL_I386_DIR32 0x0006
1359 #define MYIMAGE_REL_I386_REL32 0x0014
1362 /* We use myindex to calculate array addresses, rather than
1363 simply doing the normal subscript thing. That's because
1364 some of the above structs have sizes which are not
1365 a whole number of words. GCC rounds their sizes up to a
1366 whole number of words, which means that the address calcs
1367 arising from using normal C indexing or pointer arithmetic
1368 are just plain wrong. Sigh.
1371 myindex ( int scale, void* base, int index )
1374 ((UChar*)base) + scale * index;
1379 printName ( UChar* name, UChar* strtab )
1381 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1382 UInt32 strtab_offset = * (UInt32*)(name+4);
1383 fprintf ( stderr, "%s", strtab + strtab_offset );
1386 for (i = 0; i < 8; i++) {
1387 if (name[i] == 0) break;
1388 fprintf ( stderr, "%c", name[i] );
1395 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1397 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1398 UInt32 strtab_offset = * (UInt32*)(name+4);
1399 strncpy ( dst, strtab+strtab_offset, dstSize );
1405 if (name[i] == 0) break;
1415 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1418 /* If the string is longer than 8 bytes, look in the
1419 string table for it -- this will be correctly zero terminated.
1421 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1422 UInt32 strtab_offset = * (UInt32*)(name+4);
1423 return ((UChar*)strtab) + strtab_offset;
1425 /* Otherwise, if shorter than 8 bytes, return the original,
1426 which by defn is correctly terminated.
1428 if (name[7]==0) return name;
1429 /* The annoying case: 8 bytes. Copy into a temporary
1430 (which is never freed ...)
1432 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1434 strncpy(newstr,name,8);
1440 /* Just compares the short names (first 8 chars) */
1441 static COFF_section *
1442 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1446 = (COFF_header*)(oc->image);
1447 COFF_section* sectab
1449 ((UChar*)(oc->image))
1450 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1452 for (i = 0; i < hdr->NumberOfSections; i++) {
1455 COFF_section* section_i
1457 myindex ( sizeof_COFF_section, sectab, i );
1458 n1 = (UChar*) &(section_i->Name);
1460 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1461 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1462 n1[6]==n2[6] && n1[7]==n2[7])
1471 zapTrailingAtSign ( UChar* sym )
1473 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1475 if (sym[0] == 0) return;
1477 while (sym[i] != 0) i++;
1480 while (j > 0 && my_isdigit(sym[j])) j--;
1481 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1487 ocVerifyImage_PEi386 ( ObjectCode* oc )
1492 COFF_section* sectab;
1493 COFF_symbol* symtab;
1495 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1496 hdr = (COFF_header*)(oc->image);
1497 sectab = (COFF_section*) (
1498 ((UChar*)(oc->image))
1499 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1501 symtab = (COFF_symbol*) (
1502 ((UChar*)(oc->image))
1503 + hdr->PointerToSymbolTable
1505 strtab = ((UChar*)symtab)
1506 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1508 if (hdr->Machine != 0x14c) {
1509 belch("Not x86 PEi386");
1512 if (hdr->SizeOfOptionalHeader != 0) {
1513 belch("PEi386 with nonempty optional header");
1516 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1517 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1518 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1519 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1520 belch("Not a PEi386 object file");
1523 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1524 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1525 belch("Invalid PEi386 word size or endiannness: %d",
1526 (int)(hdr->Characteristics));
1529 /* If the string table size is way crazy, this might indicate that
1530 there are more than 64k relocations, despite claims to the
1531 contrary. Hence this test. */
1532 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1534 if ( (*(UInt32*)strtab) > 600000 ) {
1535 /* Note that 600k has no special significance other than being
1536 big enough to handle the almost-2MB-sized lumps that
1537 constitute HSwin32*.o. */
1538 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1543 /* No further verification after this point; only debug printing. */
1545 IF_DEBUG(linker, i=1);
1546 if (i == 0) return 1;
1549 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1551 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1553 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1555 fprintf ( stderr, "\n" );
1557 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1559 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1561 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1563 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1565 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1567 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1569 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1571 /* Print the section table. */
1572 fprintf ( stderr, "\n" );
1573 for (i = 0; i < hdr->NumberOfSections; i++) {
1575 COFF_section* sectab_i
1577 myindex ( sizeof_COFF_section, sectab, i );
1584 printName ( sectab_i->Name, strtab );
1594 sectab_i->VirtualSize,
1595 sectab_i->VirtualAddress,
1596 sectab_i->SizeOfRawData,
1597 sectab_i->PointerToRawData,
1598 sectab_i->NumberOfRelocations,
1599 sectab_i->PointerToRelocations,
1600 sectab_i->PointerToRawData
1602 reltab = (COFF_reloc*) (
1603 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1606 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1607 /* If the relocation field (a short) has overflowed, the
1608 * real count can be found in the first reloc entry.
1610 * See Section 4.1 (last para) of the PE spec (rev6.0).
1612 COFF_reloc* rel = (COFF_reloc*)
1613 myindex ( sizeof_COFF_reloc, reltab, 0 );
1614 noRelocs = rel->VirtualAddress;
1617 noRelocs = sectab_i->NumberOfRelocations;
1621 for (; j < noRelocs; j++) {
1623 COFF_reloc* rel = (COFF_reloc*)
1624 myindex ( sizeof_COFF_reloc, reltab, j );
1626 " type 0x%-4x vaddr 0x%-8x name `",
1628 rel->VirtualAddress );
1629 sym = (COFF_symbol*)
1630 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1631 /* Hmm..mysterious looking offset - what's it for? SOF */
1632 printName ( sym->Name, strtab -10 );
1633 fprintf ( stderr, "'\n" );
1636 fprintf ( stderr, "\n" );
1638 fprintf ( stderr, "\n" );
1639 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1640 fprintf ( stderr, "---START of string table---\n");
1641 for (i = 4; i < *(Int32*)strtab; i++) {
1643 fprintf ( stderr, "\n"); else
1644 fprintf( stderr, "%c", strtab[i] );
1646 fprintf ( stderr, "--- END of string table---\n");
1648 fprintf ( stderr, "\n" );
1651 COFF_symbol* symtab_i;
1652 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1653 symtab_i = (COFF_symbol*)
1654 myindex ( sizeof_COFF_symbol, symtab, i );
1660 printName ( symtab_i->Name, strtab );
1669 (Int32)(symtab_i->SectionNumber),
1670 (UInt32)symtab_i->Type,
1671 (UInt32)symtab_i->StorageClass,
1672 (UInt32)symtab_i->NumberOfAuxSymbols
1674 i += symtab_i->NumberOfAuxSymbols;
1678 fprintf ( stderr, "\n" );
1684 ocGetNames_PEi386 ( ObjectCode* oc )
1687 COFF_section* sectab;
1688 COFF_symbol* symtab;
1695 hdr = (COFF_header*)(oc->image);
1696 sectab = (COFF_section*) (
1697 ((UChar*)(oc->image))
1698 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1700 symtab = (COFF_symbol*) (
1701 ((UChar*)(oc->image))
1702 + hdr->PointerToSymbolTable
1704 strtab = ((UChar*)(oc->image))
1705 + hdr->PointerToSymbolTable
1706 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1708 /* Allocate space for any (local, anonymous) .bss sections. */
1710 for (i = 0; i < hdr->NumberOfSections; i++) {
1712 COFF_section* sectab_i
1714 myindex ( sizeof_COFF_section, sectab, i );
1715 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1716 if (sectab_i->VirtualSize == 0) continue;
1717 /* This is a non-empty .bss section. Allocate zeroed space for
1718 it, and set its PointerToRawData field such that oc->image +
1719 PointerToRawData == addr_of_zeroed_space. */
1720 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1721 "ocGetNames_PEi386(anonymous bss)");
1722 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1723 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1724 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1727 /* Copy section information into the ObjectCode. */
1729 for (i = 0; i < hdr->NumberOfSections; i++) {
1735 = SECTIONKIND_OTHER;
1736 COFF_section* sectab_i
1738 myindex ( sizeof_COFF_section, sectab, i );
1739 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1742 /* I'm sure this is the Right Way to do it. However, the
1743 alternative of testing the sectab_i->Name field seems to
1744 work ok with Cygwin.
1746 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1747 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1748 kind = SECTIONKIND_CODE_OR_RODATA;
1751 if (0==strcmp(".text",sectab_i->Name) ||
1752 0==strcmp(".rodata",sectab_i->Name))
1753 kind = SECTIONKIND_CODE_OR_RODATA;
1754 if (0==strcmp(".data",sectab_i->Name) ||
1755 0==strcmp(".bss",sectab_i->Name))
1756 kind = SECTIONKIND_RWDATA;
1758 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1759 sz = sectab_i->SizeOfRawData;
1760 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1762 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1763 end = start + sz - 1;
1765 if (kind == SECTIONKIND_OTHER
1766 /* Ignore sections called which contain stabs debugging
1768 && 0 != strcmp(".stab", sectab_i->Name)
1769 && 0 != strcmp(".stabstr", sectab_i->Name)
1771 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1775 if (kind != SECTIONKIND_OTHER && end >= start) {
1776 addSection(oc, kind, start, end);
1777 addProddableBlock(oc, start, end - start + 1);
1781 /* Copy exported symbols into the ObjectCode. */
1783 oc->n_symbols = hdr->NumberOfSymbols;
1784 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1785 "ocGetNames_PEi386(oc->symbols)");
1786 /* Call me paranoid; I don't care. */
1787 for (i = 0; i < oc->n_symbols; i++)
1788 oc->symbols[i] = NULL;
1792 COFF_symbol* symtab_i;
1793 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1794 symtab_i = (COFF_symbol*)
1795 myindex ( sizeof_COFF_symbol, symtab, i );
1799 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1800 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1801 /* This symbol is global and defined, viz, exported */
1802 /* for MYIMAGE_SYMCLASS_EXTERNAL
1803 && !MYIMAGE_SYM_UNDEFINED,
1804 the address of the symbol is:
1805 address of relevant section + offset in section
1807 COFF_section* sectabent
1808 = (COFF_section*) myindex ( sizeof_COFF_section,
1810 symtab_i->SectionNumber-1 );
1811 addr = ((UChar*)(oc->image))
1812 + (sectabent->PointerToRawData
1816 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1817 && symtab_i->Value > 0) {
1818 /* This symbol isn't in any section at all, ie, global bss.
1819 Allocate zeroed space for it. */
1820 addr = stgCallocBytes(1, symtab_i->Value,
1821 "ocGetNames_PEi386(non-anonymous bss)");
1822 addSection(oc, SECTIONKIND_RWDATA, addr,
1823 ((UChar*)addr) + symtab_i->Value - 1);
1824 addProddableBlock(oc, addr, symtab_i->Value);
1825 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1828 if (addr != NULL ) {
1829 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1830 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1831 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1832 ASSERT(i >= 0 && i < oc->n_symbols);
1833 /* cstring_from_COFF_symbol_name always succeeds. */
1834 oc->symbols[i] = sname;
1835 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1839 "IGNORING symbol %d\n"
1843 printName ( symtab_i->Name, strtab );
1852 (Int32)(symtab_i->SectionNumber),
1853 (UInt32)symtab_i->Type,
1854 (UInt32)symtab_i->StorageClass,
1855 (UInt32)symtab_i->NumberOfAuxSymbols
1860 i += symtab_i->NumberOfAuxSymbols;
1869 ocResolve_PEi386 ( ObjectCode* oc )
1872 COFF_section* sectab;
1873 COFF_symbol* symtab;
1883 /* ToDo: should be variable-sized? But is at least safe in the
1884 sense of buffer-overrun-proof. */
1886 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1888 hdr = (COFF_header*)(oc->image);
1889 sectab = (COFF_section*) (
1890 ((UChar*)(oc->image))
1891 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1893 symtab = (COFF_symbol*) (
1894 ((UChar*)(oc->image))
1895 + hdr->PointerToSymbolTable
1897 strtab = ((UChar*)(oc->image))
1898 + hdr->PointerToSymbolTable
1899 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1901 for (i = 0; i < hdr->NumberOfSections; i++) {
1902 COFF_section* sectab_i
1904 myindex ( sizeof_COFF_section, sectab, i );
1907 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1910 /* Ignore sections called which contain stabs debugging
1912 if (0 == strcmp(".stab", sectab_i->Name)
1913 || 0 == strcmp(".stabstr", sectab_i->Name))
1916 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1917 /* If the relocation field (a short) has overflowed, the
1918 * real count can be found in the first reloc entry.
1920 * See Section 4.1 (last para) of the PE spec (rev6.0).
1922 * Nov2003 update: the GNU linker still doesn't correctly
1923 * handle the generation of relocatable object files with
1924 * overflown relocations. Hence the output to warn of potential
1927 COFF_reloc* rel = (COFF_reloc*)
1928 myindex ( sizeof_COFF_reloc, reltab, 0 );
1929 noRelocs = rel->VirtualAddress;
1930 fprintf(stderr, "WARNING: Overflown relocation field (# relocs found: %u)\n", noRelocs); fflush(stderr);
1933 noRelocs = sectab_i->NumberOfRelocations;
1938 for (; j < noRelocs; j++) {
1940 COFF_reloc* reltab_j
1942 myindex ( sizeof_COFF_reloc, reltab, j );
1944 /* the location to patch */
1946 ((UChar*)(oc->image))
1947 + (sectab_i->PointerToRawData
1948 + reltab_j->VirtualAddress
1949 - sectab_i->VirtualAddress )
1951 /* the existing contents of pP */
1953 /* the symbol to connect to */
1954 sym = (COFF_symbol*)
1955 myindex ( sizeof_COFF_symbol,
1956 symtab, reltab_j->SymbolTableIndex );
1959 "reloc sec %2d num %3d: type 0x%-4x "
1960 "vaddr 0x%-8x name `",
1962 (UInt32)reltab_j->Type,
1963 reltab_j->VirtualAddress );
1964 printName ( sym->Name, strtab );
1965 fprintf ( stderr, "'\n" ));
1967 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1968 COFF_section* section_sym
1969 = findPEi386SectionCalled ( oc, sym->Name );
1971 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1974 S = ((UInt32)(oc->image))
1975 + (section_sym->PointerToRawData
1978 copyName ( sym->Name, strtab, symbol, 1000-1 );
1979 (void*)S = lookupLocalSymbol( oc, symbol );
1980 if ((void*)S != NULL) goto foundit;
1981 (void*)S = lookupSymbol( symbol );
1982 if ((void*)S != NULL) goto foundit;
1983 zapTrailingAtSign ( symbol );
1984 (void*)S = lookupLocalSymbol( oc, symbol );
1985 if ((void*)S != NULL) goto foundit;
1986 (void*)S = lookupSymbol( symbol );
1987 if ((void*)S != NULL) goto foundit;
1988 /* Newline first because the interactive linker has printed "linking..." */
1989 belch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
1993 checkProddableBlock(oc, pP);
1994 switch (reltab_j->Type) {
1995 case MYIMAGE_REL_I386_DIR32:
1998 case MYIMAGE_REL_I386_REL32:
1999 /* Tricky. We have to insert a displacement at
2000 pP which, when added to the PC for the _next_
2001 insn, gives the address of the target (S).
2002 Problem is to know the address of the next insn
2003 when we only know pP. We assume that this
2004 literal field is always the last in the insn,
2005 so that the address of the next insn is pP+4
2006 -- hence the constant 4.
2007 Also I don't know if A should be added, but so
2008 far it has always been zero.
2011 *pP = S - ((UInt32)pP) - 4;
2014 belch("%s: unhandled PEi386 relocation type %d",
2015 oc->fileName, reltab_j->Type);
2022 IF_DEBUG(linker, belch("completed %s", oc->fileName));
2026 #endif /* defined(OBJFORMAT_PEi386) */
2029 /* --------------------------------------------------------------------------
2031 * ------------------------------------------------------------------------*/
2033 #if defined(OBJFORMAT_ELF)
2038 #if defined(sparc_TARGET_ARCH)
2039 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2040 #elif defined(i386_TARGET_ARCH)
2041 # define ELF_TARGET_386 /* Used inside <elf.h> */
2042 #elif defined(x86_64_TARGET_ARCH)
2043 # define ELF_TARGET_X64_64
2045 #elif defined (ia64_TARGET_ARCH)
2046 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2048 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2049 # define ELF_NEED_GOT /* needs Global Offset Table */
2050 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2053 #if !defined(openbsd_TARGET_OS)
2056 /* openbsd elf has things in different places, with diff names */
2057 #include <elf_abi.h>
2058 #include <machine/reloc.h>
2059 #define R_386_32 RELOC_32
2060 #define R_386_PC32 RELOC_PC32
2064 * Define a set of types which can be used for both ELF32 and ELF64
2068 #define ELFCLASS ELFCLASS64
2069 #define Elf_Addr Elf64_Addr
2070 #define Elf_Word Elf64_Word
2071 #define Elf_Sword Elf64_Sword
2072 #define Elf_Ehdr Elf64_Ehdr
2073 #define Elf_Phdr Elf64_Phdr
2074 #define Elf_Shdr Elf64_Shdr
2075 #define Elf_Sym Elf64_Sym
2076 #define Elf_Rel Elf64_Rel
2077 #define Elf_Rela Elf64_Rela
2078 #define ELF_ST_TYPE ELF64_ST_TYPE
2079 #define ELF_ST_BIND ELF64_ST_BIND
2080 #define ELF_R_TYPE ELF64_R_TYPE
2081 #define ELF_R_SYM ELF64_R_SYM
2083 #define ELFCLASS ELFCLASS32
2084 #define Elf_Addr Elf32_Addr
2085 #define Elf_Word Elf32_Word
2086 #define Elf_Sword Elf32_Sword
2087 #define Elf_Ehdr Elf32_Ehdr
2088 #define Elf_Phdr Elf32_Phdr
2089 #define Elf_Shdr Elf32_Shdr
2090 #define Elf_Sym Elf32_Sym
2091 #define Elf_Rel Elf32_Rel
2092 #define Elf_Rela Elf32_Rela
2094 #define ELF_ST_TYPE ELF32_ST_TYPE
2097 #define ELF_ST_BIND ELF32_ST_BIND
2100 #define ELF_R_TYPE ELF32_R_TYPE
2103 #define ELF_R_SYM ELF32_R_SYM
2109 * Functions to allocate entries in dynamic sections. Currently we simply
2110 * preallocate a large number, and we don't check if a entry for the given
2111 * target already exists (a linear search is too slow). Ideally these
2112 * entries would be associated with symbols.
2115 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2116 #define GOT_SIZE 0x20000
2117 #define FUNCTION_TABLE_SIZE 0x10000
2118 #define PLT_SIZE 0x08000
2121 static Elf_Addr got[GOT_SIZE];
2122 static unsigned int gotIndex;
2123 static Elf_Addr gp_val = (Elf_Addr)got;
2126 allocateGOTEntry(Elf_Addr target)
2130 if (gotIndex >= GOT_SIZE)
2131 barf("Global offset table overflow");
2133 entry = &got[gotIndex++];
2135 return (Elf_Addr)entry;
2139 #ifdef ELF_FUNCTION_DESC
2145 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2146 static unsigned int functionTableIndex;
2149 allocateFunctionDesc(Elf_Addr target)
2151 FunctionDesc *entry;
2153 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2154 barf("Function table overflow");
2156 entry = &functionTable[functionTableIndex++];
2158 entry->gp = (Elf_Addr)gp_val;
2159 return (Elf_Addr)entry;
2163 copyFunctionDesc(Elf_Addr target)
2165 FunctionDesc *olddesc = (FunctionDesc *)target;
2166 FunctionDesc *newdesc;
2168 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2169 newdesc->gp = olddesc->gp;
2170 return (Elf_Addr)newdesc;
2175 #ifdef ia64_TARGET_ARCH
2176 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2177 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2179 static unsigned char plt_code[] =
2181 /* taken from binutils bfd/elfxx-ia64.c */
2182 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2183 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2184 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2185 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2186 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2187 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2190 /* If we can't get to the function descriptor via gp, take a local copy of it */
2191 #define PLT_RELOC(code, target) { \
2192 Elf64_Sxword rel_value = target - gp_val; \
2193 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2194 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2196 ia64_reloc_gprel22((Elf_Addr)code, target); \
2201 unsigned char code[sizeof(plt_code)];
2205 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2207 PLTEntry *plt = (PLTEntry *)oc->plt;
2210 if (oc->pltIndex >= PLT_SIZE)
2211 barf("Procedure table overflow");
2213 entry = &plt[oc->pltIndex++];
2214 memcpy(entry->code, plt_code, sizeof(entry->code));
2215 PLT_RELOC(entry->code, target);
2216 return (Elf_Addr)entry;
2222 return (PLT_SIZE * sizeof(PLTEntry));
2228 * Generic ELF functions
2232 findElfSection ( void* objImage, Elf_Word sh_type )
2234 char* ehdrC = (char*)objImage;
2235 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2236 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2237 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2241 for (i = 0; i < ehdr->e_shnum; i++) {
2242 if (shdr[i].sh_type == sh_type
2243 /* Ignore the section header's string table. */
2244 && i != ehdr->e_shstrndx
2245 /* Ignore string tables named .stabstr, as they contain
2247 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2249 ptr = ehdrC + shdr[i].sh_offset;
2256 #if defined(ia64_TARGET_ARCH)
2258 findElfSegment ( void* objImage, Elf_Addr vaddr )
2260 char* ehdrC = (char*)objImage;
2261 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2262 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2263 Elf_Addr segaddr = 0;
2266 for (i = 0; i < ehdr->e_phnum; i++) {
2267 segaddr = phdr[i].p_vaddr;
2268 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2276 ocVerifyImage_ELF ( ObjectCode* oc )
2280 int i, j, nent, nstrtab, nsymtabs;
2284 char* ehdrC = (char*)(oc->image);
2285 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2287 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2288 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2289 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2290 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2291 belch("%s: not an ELF object", oc->fileName);
2295 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2296 belch("%s: unsupported ELF format", oc->fileName);
2300 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2301 IF_DEBUG(linker,belch( "Is little-endian" ));
2303 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2304 IF_DEBUG(linker,belch( "Is big-endian" ));
2306 belch("%s: unknown endiannness", oc->fileName);
2310 if (ehdr->e_type != ET_REL) {
2311 belch("%s: not a relocatable object (.o) file", oc->fileName);
2314 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2316 IF_DEBUG(linker,belch( "Architecture is " ));
2317 switch (ehdr->e_machine) {
2318 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2319 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2321 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2323 default: IF_DEBUG(linker,belch( "unknown" ));
2324 belch("%s: unknown architecture", oc->fileName);
2328 IF_DEBUG(linker,belch(
2329 "\nSection header table: start %d, n_entries %d, ent_size %d",
2330 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2332 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2334 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2336 if (ehdr->e_shstrndx == SHN_UNDEF) {
2337 belch("%s: no section header string table", oc->fileName);
2340 IF_DEBUG(linker,belch( "Section header string table is section %d",
2342 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2345 for (i = 0; i < ehdr->e_shnum; i++) {
2346 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2347 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2348 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2349 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2350 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2351 ehdrC + shdr[i].sh_offset,
2352 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2354 if (shdr[i].sh_type == SHT_REL) {
2355 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2356 } else if (shdr[i].sh_type == SHT_RELA) {
2357 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2359 IF_DEBUG(linker,fprintf(stderr," "));
2362 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2366 IF_DEBUG(linker,belch( "\nString tables" ));
2369 for (i = 0; i < ehdr->e_shnum; i++) {
2370 if (shdr[i].sh_type == SHT_STRTAB
2371 /* Ignore the section header's string table. */
2372 && i != ehdr->e_shstrndx
2373 /* Ignore string tables named .stabstr, as they contain
2375 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2377 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2378 strtab = ehdrC + shdr[i].sh_offset;
2383 belch("%s: no string tables, or too many", oc->fileName);
2388 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2389 for (i = 0; i < ehdr->e_shnum; i++) {
2390 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2391 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2393 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2394 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2395 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2397 shdr[i].sh_size % sizeof(Elf_Sym)
2399 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2400 belch("%s: non-integral number of symbol table entries", oc->fileName);
2403 for (j = 0; j < nent; j++) {
2404 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2405 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2406 (int)stab[j].st_shndx,
2407 (int)stab[j].st_size,
2408 (char*)stab[j].st_value ));
2410 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2411 switch (ELF_ST_TYPE(stab[j].st_info)) {
2412 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2413 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2414 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2415 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2416 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2417 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2419 IF_DEBUG(linker,fprintf(stderr, " " ));
2421 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2422 switch (ELF_ST_BIND(stab[j].st_info)) {
2423 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2424 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2425 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2426 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2428 IF_DEBUG(linker,fprintf(stderr, " " ));
2430 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2434 if (nsymtabs == 0) {
2435 belch("%s: didn't find any symbol tables", oc->fileName);
2444 ocGetNames_ELF ( ObjectCode* oc )
2449 char* ehdrC = (char*)(oc->image);
2450 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2451 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2452 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2454 ASSERT(symhash != NULL);
2457 belch("%s: no strtab", oc->fileName);
2462 for (i = 0; i < ehdr->e_shnum; i++) {
2463 /* Figure out what kind of section it is. Logic derived from
2464 Figure 1.14 ("Special Sections") of the ELF document
2465 ("Portable Formats Specification, Version 1.1"). */
2466 Elf_Shdr hdr = shdr[i];
2467 SectionKind kind = SECTIONKIND_OTHER;
2470 if (hdr.sh_type == SHT_PROGBITS
2471 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2472 /* .text-style section */
2473 kind = SECTIONKIND_CODE_OR_RODATA;
2476 if (hdr.sh_type == SHT_PROGBITS
2477 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2478 /* .data-style section */
2479 kind = SECTIONKIND_RWDATA;
2482 if (hdr.sh_type == SHT_PROGBITS
2483 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2484 /* .rodata-style section */
2485 kind = SECTIONKIND_CODE_OR_RODATA;
2488 if (hdr.sh_type == SHT_NOBITS
2489 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2490 /* .bss-style section */
2491 kind = SECTIONKIND_RWDATA;
2495 if (is_bss && shdr[i].sh_size > 0) {
2496 /* This is a non-empty .bss section. Allocate zeroed space for
2497 it, and set its .sh_offset field such that
2498 ehdrC + .sh_offset == addr_of_zeroed_space. */
2499 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2500 "ocGetNames_ELF(BSS)");
2501 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2503 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2504 zspace, shdr[i].sh_size);
2508 /* fill in the section info */
2509 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2510 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2511 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2512 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2515 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2517 /* copy stuff into this module's object symbol table */
2518 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2519 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2521 oc->n_symbols = nent;
2522 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2523 "ocGetNames_ELF(oc->symbols)");
2525 for (j = 0; j < nent; j++) {
2527 char isLocal = FALSE; /* avoids uninit-var warning */
2529 char* nm = strtab + stab[j].st_name;
2530 int secno = stab[j].st_shndx;
2532 /* Figure out if we want to add it; if so, set ad to its
2533 address. Otherwise leave ad == NULL. */
2535 if (secno == SHN_COMMON) {
2537 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2539 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2540 stab[j].st_size, nm);
2542 /* Pointless to do addProddableBlock() for this area,
2543 since the linker should never poke around in it. */
2546 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2547 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2549 /* and not an undefined symbol */
2550 && stab[j].st_shndx != SHN_UNDEF
2551 /* and not in a "special section" */
2552 && stab[j].st_shndx < SHN_LORESERVE
2554 /* and it's a not a section or string table or anything silly */
2555 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2556 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2557 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2560 /* Section 0 is the undefined section, hence > and not >=. */
2561 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2563 if (shdr[secno].sh_type == SHT_NOBITS) {
2564 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2565 stab[j].st_size, stab[j].st_value, nm);
2568 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2569 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2572 #ifdef ELF_FUNCTION_DESC
2573 /* dlsym() and the initialisation table both give us function
2574 * descriptors, so to be consistent we store function descriptors
2575 * in the symbol table */
2576 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2577 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2579 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2580 ad, oc->fileName, nm ));
2585 /* And the decision is ... */
2589 oc->symbols[j] = nm;
2592 /* Ignore entirely. */
2594 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2598 IF_DEBUG(linker,belch( "skipping `%s'",
2599 strtab + stab[j].st_name ));
2602 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2603 (int)ELF_ST_BIND(stab[j].st_info),
2604 (int)ELF_ST_TYPE(stab[j].st_info),
2605 (int)stab[j].st_shndx,
2606 strtab + stab[j].st_name
2609 oc->symbols[j] = NULL;
2618 /* Do ELF relocations which lack an explicit addend. All x86-linux
2619 relocations appear to be of this form. */
2621 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2622 Elf_Shdr* shdr, int shnum,
2623 Elf_Sym* stab, char* strtab )
2628 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2629 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2630 int target_shndx = shdr[shnum].sh_info;
2631 int symtab_shndx = shdr[shnum].sh_link;
2633 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2634 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2635 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2636 target_shndx, symtab_shndx ));
2638 for (j = 0; j < nent; j++) {
2639 Elf_Addr offset = rtab[j].r_offset;
2640 Elf_Addr info = rtab[j].r_info;
2642 Elf_Addr P = ((Elf_Addr)targ) + offset;
2643 Elf_Word* pP = (Elf_Word*)P;
2648 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2649 j, (void*)offset, (void*)info ));
2651 IF_DEBUG(linker,belch( " ZERO" ));
2654 Elf_Sym sym = stab[ELF_R_SYM(info)];
2655 /* First see if it is a local symbol. */
2656 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2657 /* Yes, so we can get the address directly from the ELF symbol
2659 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2661 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2662 + stab[ELF_R_SYM(info)].st_value);
2665 /* No, so look up the name in our global table. */
2666 symbol = strtab + sym.st_name;
2667 (void*)S = lookupSymbol( symbol );
2670 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2673 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2676 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2677 (void*)P, (void*)S, (void*)A ));
2678 checkProddableBlock ( oc, pP );
2682 switch (ELF_R_TYPE(info)) {
2683 # ifdef i386_TARGET_ARCH
2684 case R_386_32: *pP = value; break;
2685 case R_386_PC32: *pP = value - P; break;
2688 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2689 oc->fileName, ELF_R_TYPE(info));
2697 /* Do ELF relocations for which explicit addends are supplied.
2698 sparc-solaris relocations appear to be of this form. */
2700 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2701 Elf_Shdr* shdr, int shnum,
2702 Elf_Sym* stab, char* strtab )
2707 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2708 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2709 int target_shndx = shdr[shnum].sh_info;
2710 int symtab_shndx = shdr[shnum].sh_link;
2712 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2713 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2714 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2715 target_shndx, symtab_shndx ));
2717 for (j = 0; j < nent; j++) {
2718 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
2719 /* This #ifdef only serves to avoid unused-var warnings. */
2720 Elf_Addr offset = rtab[j].r_offset;
2721 Elf_Addr P = targ + offset;
2723 Elf_Addr info = rtab[j].r_info;
2724 Elf_Addr A = rtab[j].r_addend;
2727 # if defined(sparc_TARGET_ARCH)
2728 Elf_Word* pP = (Elf_Word*)P;
2730 # elif defined(ia64_TARGET_ARCH)
2731 Elf64_Xword *pP = (Elf64_Xword *)P;
2735 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2736 j, (void*)offset, (void*)info,
2739 IF_DEBUG(linker,belch( " ZERO" ));
2742 Elf_Sym sym = stab[ELF_R_SYM(info)];
2743 /* First see if it is a local symbol. */
2744 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2745 /* Yes, so we can get the address directly from the ELF symbol
2747 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2749 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2750 + stab[ELF_R_SYM(info)].st_value);
2751 #ifdef ELF_FUNCTION_DESC
2752 /* Make a function descriptor for this function */
2753 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2754 S = allocateFunctionDesc(S + A);
2759 /* No, so look up the name in our global table. */
2760 symbol = strtab + sym.st_name;
2761 (void*)S = lookupSymbol( symbol );
2763 #ifdef ELF_FUNCTION_DESC
2764 /* If a function, already a function descriptor - we would
2765 have to copy it to add an offset. */
2766 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
2767 belch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
2771 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2774 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2777 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2778 (void*)P, (void*)S, (void*)A ));
2779 /* checkProddableBlock ( oc, (void*)P ); */
2783 switch (ELF_R_TYPE(info)) {
2784 # if defined(sparc_TARGET_ARCH)
2785 case R_SPARC_WDISP30:
2786 w1 = *pP & 0xC0000000;
2787 w2 = (Elf_Word)((value - P) >> 2);
2788 ASSERT((w2 & 0xC0000000) == 0);
2793 w1 = *pP & 0xFFC00000;
2794 w2 = (Elf_Word)(value >> 10);
2795 ASSERT((w2 & 0xFFC00000) == 0);
2801 w2 = (Elf_Word)(value & 0x3FF);
2802 ASSERT((w2 & ~0x3FF) == 0);
2806 /* According to the Sun documentation:
2808 This relocation type resembles R_SPARC_32, except it refers to an
2809 unaligned word. That is, the word to be relocated must be treated
2810 as four separate bytes with arbitrary alignment, not as a word
2811 aligned according to the architecture requirements.
2813 (JRS: which means that freeloading on the R_SPARC_32 case
2814 is probably wrong, but hey ...)
2818 w2 = (Elf_Word)value;
2821 # elif defined(ia64_TARGET_ARCH)
2822 case R_IA64_DIR64LSB:
2823 case R_IA64_FPTR64LSB:
2826 case R_IA64_PCREL64LSB:
2829 case R_IA64_SEGREL64LSB:
2830 addr = findElfSegment(ehdrC, value);
2833 case R_IA64_GPREL22:
2834 ia64_reloc_gprel22(P, value);
2836 case R_IA64_LTOFF22:
2837 case R_IA64_LTOFF22X:
2838 case R_IA64_LTOFF_FPTR22:
2839 addr = allocateGOTEntry(value);
2840 ia64_reloc_gprel22(P, addr);
2842 case R_IA64_PCREL21B:
2843 ia64_reloc_pcrel21(P, S, oc);
2846 /* This goes with R_IA64_LTOFF22X and points to the load to
2847 * convert into a move. We don't implement relaxation. */
2851 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2852 oc->fileName, ELF_R_TYPE(info));
2861 ocResolve_ELF ( ObjectCode* oc )
2865 Elf_Sym* stab = NULL;
2866 char* ehdrC = (char*)(oc->image);
2867 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2868 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2869 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2871 /* first find "the" symbol table */
2872 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2874 /* also go find the string table */
2875 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2877 if (stab == NULL || strtab == NULL) {
2878 belch("%s: can't find string or symbol table", oc->fileName);
2882 /* Process the relocation sections. */
2883 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2885 /* Skip sections called ".rel.stab". These appear to contain
2886 relocation entries that, when done, make the stabs debugging
2887 info point at the right places. We ain't interested in all
2889 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2892 if (shdr[shnum].sh_type == SHT_REL ) {
2893 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2894 shnum, stab, strtab );
2898 if (shdr[shnum].sh_type == SHT_RELA) {
2899 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2900 shnum, stab, strtab );
2905 /* Free the local symbol table; we won't need it again. */
2906 freeHashTable(oc->lochash, NULL);
2914 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2915 * at the front. The following utility functions pack and unpack instructions, and
2916 * take care of the most common relocations.
2919 #ifdef ia64_TARGET_ARCH
2922 ia64_extract_instruction(Elf64_Xword *target)
2925 int slot = (Elf_Addr)target & 3;
2926 (Elf_Addr)target &= ~3;
2934 return ((w1 >> 5) & 0x1ffffffffff);
2936 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2940 barf("ia64_extract_instruction: invalid slot %p", target);
2945 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2947 int slot = (Elf_Addr)target & 3;
2948 (Elf_Addr)target &= ~3;
2953 *target |= value << 5;
2956 *target |= value << 46;
2957 *(target+1) |= value >> 18;
2960 *(target+1) |= value << 23;
2966 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2968 Elf64_Xword instruction;
2969 Elf64_Sxword rel_value;
2971 rel_value = value - gp_val;
2972 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2973 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2975 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2976 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2977 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2978 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2979 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2980 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2984 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2986 Elf64_Xword instruction;
2987 Elf64_Sxword rel_value;
2990 entry = allocatePLTEntry(value, oc);
2992 rel_value = (entry >> 4) - (target >> 4);
2993 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2994 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
2996 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2997 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
2998 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2999 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
3006 /* --------------------------------------------------------------------------
3008 * ------------------------------------------------------------------------*/
3010 #if defined(OBJFORMAT_MACHO)
3013 Support for MachO linking on Darwin/MacOS X on PowerPC chips
3014 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3016 I hereby formally apologize for the hackish nature of this code.
3017 Things that need to be done:
3018 *) implement ocVerifyImage_MachO
3019 *) add still more sanity checks.
3024 ocAllocateJumpIslands_MachO
3026 Allocate additional space at the end of the object file image to make room
3029 PowerPC relative branch instructions have a 24 bit displacement field.
3030 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
3031 If a particular imported symbol is outside this range, we have to redirect
3032 the jump to a short piece of new code that just loads the 32bit absolute
3033 address and jumps there.
3034 This function just allocates space for one 16 byte jump island for every
3035 undefined symbol in the object file. The code for the islands is filled in by
3036 makeJumpIsland below.
3039 static const int islandSize = 16;
3041 static int ocAllocateJumpIslands_MachO(ObjectCode* oc)
3043 char *image = (char*) oc->image;
3044 struct mach_header *header = (struct mach_header*) image;
3045 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3048 for(i=0;i<header->ncmds;i++)
3050 if(lc->cmd == LC_DYSYMTAB)
3052 struct dysymtab_command *dsymLC = (struct dysymtab_command*) lc;
3053 unsigned long nundefsym = dsymLC->nundefsym;
3054 oc->island_start_symbol = dsymLC->iundefsym;
3055 oc->n_islands = nundefsym;
3060 #error ocAllocateJumpIslands_MachO doesnt want USE_MMAP to be defined
3062 oc->image = stgReallocBytes(
3063 image, oc->fileSize + islandSize * nundefsym,
3064 "ocAllocateJumpIslands_MachO");
3066 oc->jump_islands = oc->image + oc->fileSize;
3067 memset(oc->jump_islands, 0, islandSize * nundefsym);
3070 break; // there can be only one LC_DSYMTAB
3072 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3077 static int ocVerifyImage_MachO(ObjectCode* oc)
3079 // FIXME: do some verifying here
3083 static int resolveImports(
3086 struct symtab_command *symLC,
3087 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3088 unsigned long *indirectSyms,
3089 struct nlist *nlist)
3093 for(i=0;i*4<sect->size;i++)
3095 // according to otool, reserved1 contains the first index into the indirect symbol table
3096 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3097 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3100 if((symbol->n_type & N_TYPE) == N_UNDF
3101 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3102 addr = (void*) (symbol->n_value);
3103 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3106 addr = lookupSymbol(nm);
3109 belch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3113 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3114 ((void**)(image + sect->offset))[i] = addr;
3120 static void* makeJumpIsland(
3122 unsigned long symbolNumber,
3125 if(symbolNumber < oc->island_start_symbol ||
3126 symbolNumber - oc->island_start_symbol > oc->n_islands)
3128 symbolNumber -= oc->island_start_symbol;
3130 void *island = (void*) ((char*)oc->jump_islands + islandSize * symbolNumber);
3131 unsigned long *p = (unsigned long*) island;
3133 // lis r12, hi16(target)
3134 *p++ = 0x3d800000 | ( ((unsigned long) target) >> 16 );
3135 // ori r12, r12, lo16(target)
3136 *p++ = 0x618c0000 | ( ((unsigned long) target) & 0xFFFF );
3142 return (void*) island;
3145 static char* relocateAddress(
3148 struct section* sections,
3149 unsigned long address)
3152 for(i = 0; i < nSections; i++)
3154 if(sections[i].addr <= address
3155 && address < sections[i].addr + sections[i].size)
3157 return oc->image + sections[i].offset + address - sections[i].addr;
3160 barf("Invalid Mach-O file:"
3161 "Address out of bounds while relocating object file");
3165 static int relocateSection(
3168 struct symtab_command *symLC, struct nlist *nlist,
3169 int nSections, struct section* sections, struct section *sect)
3171 struct relocation_info *relocs;
3174 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3176 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3180 relocs = (struct relocation_info*) (image + sect->reloff);
3184 if(relocs[i].r_address & R_SCATTERED)
3186 struct scattered_relocation_info *scat =
3187 (struct scattered_relocation_info*) &relocs[i];
3191 if(scat->r_length == 2)
3193 unsigned long word = 0;
3194 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + scat->r_address);
3195 checkProddableBlock(oc,wordPtr);
3197 // Step 1: Figure out what the relocated value should be
3198 if(scat->r_type == GENERIC_RELOC_VANILLA)
3200 word = scat->r_value + sect->offset + ((long) image);
3202 else if(scat->r_type == PPC_RELOC_SECTDIFF
3203 || scat->r_type == PPC_RELOC_LO16_SECTDIFF
3204 || scat->r_type == PPC_RELOC_HI16_SECTDIFF
3205 || scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3207 struct scattered_relocation_info *pair =
3208 (struct scattered_relocation_info*) &relocs[i+1];
3210 if(!pair->r_scattered || pair->r_type != PPC_RELOC_PAIR)
3211 barf("Invalid Mach-O file: "
3212 "PPC_RELOC_*_SECTDIFF not followed by PPC_RELOC_PAIR");
3214 word = (unsigned long)
3215 (relocateAddress(oc, nSections, sections, scat->r_value)
3216 - relocateAddress(oc, nSections, sections, pair->r_value));
3220 continue; // ignore the others
3222 if(scat->r_type == GENERIC_RELOC_VANILLA
3223 || scat->r_type == PPC_RELOC_SECTDIFF)
3227 else if(scat->r_type == PPC_RELOC_LO16_SECTDIFF)
3229 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3231 else if(scat->r_type == PPC_RELOC_HI16_SECTDIFF)
3233 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3235 else if(scat->r_type == PPC_RELOC_HA16_SECTDIFF)
3237 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3238 + ((word & (1<<15)) ? 1 : 0);
3243 continue; // FIXME: I hope it's OK to ignore all the others.
3247 struct relocation_info *reloc = &relocs[i];
3248 if(reloc->r_pcrel && !reloc->r_extern)
3251 if(reloc->r_length == 2)
3253 unsigned long word = 0;
3254 unsigned long jumpIsland = 0;
3255 long offsetToJumpIsland;
3257 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3258 checkProddableBlock(oc,wordPtr);
3260 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3264 else if(reloc->r_type == PPC_RELOC_LO16)
3266 word = ((unsigned short*) wordPtr)[1];
3267 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3269 else if(reloc->r_type == PPC_RELOC_HI16)
3271 word = ((unsigned short*) wordPtr)[1] << 16;
3272 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3274 else if(reloc->r_type == PPC_RELOC_HA16)
3276 word = ((unsigned short*) wordPtr)[1] << 16;
3277 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3279 else if(reloc->r_type == PPC_RELOC_BR24)
3282 word = (word & 0x03FFFFFC) | (word & 0x02000000) ? 0xFC000000 : 0;
3286 if(!reloc->r_extern)
3289 sections[reloc->r_symbolnum-1].offset
3290 - sections[reloc->r_symbolnum-1].addr
3297 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3298 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3299 word = (unsigned long) (lookupSymbol(nm));
3302 belch("\nunknown symbol `%s'", nm);
3308 jumpIsland = (long) makeJumpIsland(oc,reloc->r_symbolnum,(void*)word);
3309 word -= ((long)image) + sect->offset + reloc->r_address;
3312 offsetToJumpIsland = jumpIsland
3313 - (((long)image) + sect->offset + reloc->r_address);
3318 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3323 else if(reloc->r_type == PPC_RELOC_LO16)
3325 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3328 else if(reloc->r_type == PPC_RELOC_HI16)
3330 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3333 else if(reloc->r_type == PPC_RELOC_HA16)
3335 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3336 + ((word & (1<<15)) ? 1 : 0);
3339 else if(reloc->r_type == PPC_RELOC_BR24)
3341 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3343 // The branch offset is too large.
3344 // Therefore, we try to use a jump island.
3346 barf("unconditional relative branch out of range: "
3347 "no jump island available");
3349 word = offsetToJumpIsland;
3350 if((long)word > (long)0x01FFFFFF || (long)word < (long)0xFFE00000)
3351 barf("unconditional relative branch out of range: "
3352 "jump island out of range");
3354 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3358 barf("\nunknown relocation %d",reloc->r_type);
3365 static int ocGetNames_MachO(ObjectCode* oc)
3367 char *image = (char*) oc->image;
3368 struct mach_header *header = (struct mach_header*) image;
3369 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3370 unsigned i,curSymbol;
3371 struct segment_command *segLC = NULL;
3372 struct section *sections;
3373 struct symtab_command *symLC = NULL;
3374 struct dysymtab_command *dsymLC = NULL;
3375 struct nlist *nlist;
3376 unsigned long commonSize = 0;
3377 char *commonStorage = NULL;
3378 unsigned long commonCounter;
3380 for(i=0;i<header->ncmds;i++)
3382 if(lc->cmd == LC_SEGMENT)
3383 segLC = (struct segment_command*) lc;
3384 else if(lc->cmd == LC_SYMTAB)
3385 symLC = (struct symtab_command*) lc;
3386 else if(lc->cmd == LC_DYSYMTAB)
3387 dsymLC = (struct dysymtab_command*) lc;
3388 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3391 sections = (struct section*) (segLC+1);
3392 nlist = (struct nlist*) (image + symLC->symoff);
3394 for(i=0;i<segLC->nsects;i++)
3396 if(sections[i].size == 0)
3399 if((sections[i].flags & SECTION_TYPE) == S_ZEROFILL)
3401 char * zeroFillArea = stgCallocBytes(1,sections[i].size,
3402 "ocGetNames_MachO(common symbols)");
3403 sections[i].offset = zeroFillArea - image;
3406 if(!strcmp(sections[i].sectname,"__text"))
3407 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3408 (void*) (image + sections[i].offset),
3409 (void*) (image + sections[i].offset + sections[i].size));
3410 else if(!strcmp(sections[i].sectname,"__const"))
3411 addSection(oc, SECTIONKIND_RWDATA,
3412 (void*) (image + sections[i].offset),
3413 (void*) (image + sections[i].offset + sections[i].size));
3414 else if(!strcmp(sections[i].sectname,"__data"))
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,"__bss")
3419 || !strcmp(sections[i].sectname,"__common"))
3420 addSection(oc, SECTIONKIND_RWDATA,
3421 (void*) (image + sections[i].offset),
3422 (void*) (image + sections[i].offset + sections[i].size));
3424 addProddableBlock(oc, (void*) (image + sections[i].offset),
3428 // count external symbols defined here
3430 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3432 if((nlist[i].n_type & N_TYPE) == N_SECT)
3435 for(i=0;i<symLC->nsyms;i++)
3437 if((nlist[i].n_type & N_TYPE) == N_UNDF
3438 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3440 commonSize += nlist[i].n_value;
3444 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3445 "ocGetNames_MachO(oc->symbols)");
3447 // insert symbols into hash table
3448 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3450 if((nlist[i].n_type & N_TYPE) == N_SECT)
3452 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3453 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3454 sections[nlist[i].n_sect-1].offset
3455 - sections[nlist[i].n_sect-1].addr
3456 + nlist[i].n_value);
3457 oc->symbols[curSymbol++] = nm;
3461 // insert local symbols into lochash
3462 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3464 if((nlist[i].n_type & N_TYPE) == N_SECT)
3466 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3467 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3468 sections[nlist[i].n_sect-1].offset
3469 - sections[nlist[i].n_sect-1].addr
3470 + nlist[i].n_value);
3475 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3476 commonCounter = (unsigned long)commonStorage;
3477 for(i=0;i<symLC->nsyms;i++)
3479 if((nlist[i].n_type & N_TYPE) == N_UNDF
3480 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3482 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3483 unsigned long sz = nlist[i].n_value;
3485 nlist[i].n_value = commonCounter;
3487 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3488 oc->symbols[curSymbol++] = nm;
3490 commonCounter += sz;
3496 static int ocResolve_MachO(ObjectCode* oc)
3498 char *image = (char*) oc->image;
3499 struct mach_header *header = (struct mach_header*) image;
3500 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3502 struct segment_command *segLC = NULL;
3503 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3504 struct symtab_command *symLC = NULL;
3505 struct dysymtab_command *dsymLC = NULL;
3506 struct nlist *nlist;
3507 unsigned long *indirectSyms;
3509 for(i=0;i<header->ncmds;i++)
3511 if(lc->cmd == LC_SEGMENT)
3512 segLC = (struct segment_command*) lc;
3513 else if(lc->cmd == LC_SYMTAB)
3514 symLC = (struct symtab_command*) lc;
3515 else if(lc->cmd == LC_DYSYMTAB)
3516 dsymLC = (struct dysymtab_command*) lc;
3517 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3520 sections = (struct section*) (segLC+1);
3521 nlist = (struct nlist*) (image + symLC->symoff);
3523 for(i=0;i<segLC->nsects;i++)
3525 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3526 la_ptrs = §ions[i];
3527 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3528 nl_ptrs = §ions[i];
3531 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3534 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
3537 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
3540 for(i=0;i<segLC->nsects;i++)
3542 if(!relocateSection(oc,image,symLC,nlist,segLC->nsects,sections,§ions[i]))
3546 /* Free the local symbol table; we won't need it again. */
3547 freeHashTable(oc->lochash, NULL);
3551 Flush the data & instruction caches.
3552 Because the PPC has split data/instruction caches, we have to
3553 do that whenever we modify code at runtime.
3556 int n = (oc->fileSize + islandSize * oc->n_islands) / 4;
3557 unsigned long *p = (unsigned long*)oc->image;
3560 __asm__ volatile ("dcbf 0,%0\n\tsync\n\ticbi 0,%0"
3564 __asm__ volatile ("sync\n\tisync");
3570 * The Mach-O object format uses leading underscores. But not everywhere.
3571 * There is a small number of runtime support functions defined in
3572 * libcc_dynamic.a whose name does not have a leading underscore.
3573 * As a consequence, we can't get their address from C code.
3574 * We have to use inline assembler just to take the address of a function.
3578 static void machoInitSymbolsWithoutUnderscore()
3584 __asm__ ("lis %0,hi16(" #x ")\n\tori %0,%0,lo16(" #x ")" : "=r" (p)); \
3585 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, p);
3587 RTS_MACHO_NOUNDERLINE_SYMBOLS