1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.114 2003/02/10 23:35:03 wolfgang Exp $
4 * (c) The GHC Team, 2000, 2001
8 * ---------------------------------------------------------------------------*/
11 #include "PosixSource.h"
18 #include "LinkerInternals.h"
20 #include "StoragePriv.h"
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
30 #ifdef HAVE_SYS_STAT_H
34 #if defined(HAVE_FRAMEWORK_HASKELLSUPPORT)
35 #include <HaskellSupport/dlfcn.h>
36 #elif defined(HAVE_DLFCN_H)
40 #if defined(cygwin32_TARGET_OS)
45 #ifdef HAVE_SYS_TIME_H
49 #include <sys/fcntl.h>
50 #include <sys/termios.h>
51 #include <sys/utime.h>
52 #include <sys/utsname.h>
56 #if defined(ia64_TARGET_ARCH)
62 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
63 # define OBJFORMAT_ELF
64 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
65 # define OBJFORMAT_PEi386
68 #elif defined(darwin_TARGET_OS)
69 # include <mach-o/ppc/reloc.h>
70 # define OBJFORMAT_MACHO
71 # include <mach-o/loader.h>
72 # include <mach-o/nlist.h>
73 # include <mach-o/reloc.h>
76 /* Hash table mapping symbol names to Symbol */
77 static /*Str*/HashTable *symhash;
79 /* List of currently loaded objects */
80 ObjectCode *objects = NULL; /* initially empty */
82 #if defined(OBJFORMAT_ELF)
83 static int ocVerifyImage_ELF ( ObjectCode* oc );
84 static int ocGetNames_ELF ( ObjectCode* oc );
85 static int ocResolve_ELF ( ObjectCode* oc );
86 #elif defined(OBJFORMAT_PEi386)
87 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
88 static int ocGetNames_PEi386 ( ObjectCode* oc );
89 static int ocResolve_PEi386 ( ObjectCode* oc );
90 #elif defined(OBJFORMAT_MACHO)
91 static int ocVerifyImage_MachO ( ObjectCode* oc );
92 static int ocGetNames_MachO ( ObjectCode* oc );
93 static int ocResolve_MachO ( ObjectCode* oc );
95 static void machoInitSymbolsWithoutUnderscore();
98 /* -----------------------------------------------------------------------------
99 * Built-in symbols from the RTS
102 typedef struct _RtsSymbolVal {
109 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
111 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
112 SymX(makeStableNamezh_fast) \
113 SymX(finalizzeWeakzh_fast)
115 /* These are not available in GUM!!! -- HWL */
116 #define Maybe_ForeignObj
117 #define Maybe_Stable_Names
120 #if !defined (mingw32_TARGET_OS)
121 #define RTS_POSIX_ONLY_SYMBOLS \
122 SymX(stg_sig_install) \
126 #if defined (cygwin32_TARGET_OS)
127 #define RTS_MINGW_ONLY_SYMBOLS /**/
128 /* Don't have the ability to read import libs / archives, so
129 * we have to stupidly list a lot of what libcygwin.a
132 #define RTS_CYGWIN_ONLY_SYMBOLS \
214 #elif !defined(mingw32_TARGET_OS)
215 #define RTS_MINGW_ONLY_SYMBOLS /**/
216 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
217 #else /* defined(mingw32_TARGET_OS) */
218 #define RTS_POSIX_ONLY_SYMBOLS /**/
219 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
221 /* These are statically linked from the mingw libraries into the ghc
222 executable, so we have to employ this hack. */
223 #define RTS_MINGW_ONLY_SYMBOLS \
235 SymX(getservbyname) \
236 SymX(getservbyport) \
237 SymX(getprotobynumber) \
238 SymX(getprotobyname) \
239 SymX(gethostbyname) \
240 SymX(gethostbyaddr) \
275 Sym(_imp___timezone) \
291 # define MAIN_CAP_SYM SymX(MainCapability)
293 # define MAIN_CAP_SYM
296 #define RTS_SYMBOLS \
300 SymX(stg_enter_info) \
301 SymX(stg_enter_ret) \
302 SymX(stg_gc_void_info) \
303 SymX(__stg_gc_enter_1) \
304 SymX(stg_gc_noregs) \
305 SymX(stg_gc_unpt_r1_info) \
306 SymX(stg_gc_unpt_r1) \
307 SymX(stg_gc_unbx_r1_info) \
308 SymX(stg_gc_unbx_r1) \
309 SymX(stg_gc_f1_info) \
311 SymX(stg_gc_d1_info) \
313 SymX(stg_gc_l1_info) \
316 SymX(stg_gc_fun_info) \
317 SymX(stg_gc_fun_ret) \
319 SymX(stg_gc_gen_info) \
320 SymX(stg_gc_gen_hp) \
322 SymX(stg_gen_yield) \
323 SymX(stg_yield_noregs) \
324 SymX(stg_yield_to_interpreter) \
325 SymX(stg_gen_block) \
326 SymX(stg_block_noregs) \
328 SymX(stg_block_takemvar) \
329 SymX(stg_block_putmvar) \
330 SymX(stg_seq_frame_info) \
333 SymX(MallocFailHook) \
335 SymX(OutOfHeapHook) \
336 SymX(PatErrorHdrHook) \
337 SymX(PostTraceHook) \
339 SymX(StackOverflowHook) \
340 SymX(__encodeDouble) \
341 SymX(__encodeFloat) \
344 SymX(__gmpz_cmp_si) \
345 SymX(__gmpz_cmp_ui) \
346 SymX(__gmpz_get_si) \
347 SymX(__gmpz_get_ui) \
348 SymX(__int_encodeDouble) \
349 SymX(__int_encodeFloat) \
350 SymX(andIntegerzh_fast) \
351 SymX(blockAsyncExceptionszh_fast) \
354 SymX(complementIntegerzh_fast) \
355 SymX(cmpIntegerzh_fast) \
356 SymX(cmpIntegerIntzh_fast) \
357 SymX(createAdjustor) \
358 SymX(decodeDoublezh_fast) \
359 SymX(decodeFloatzh_fast) \
362 SymX(deRefWeakzh_fast) \
363 SymX(deRefStablePtrzh_fast) \
364 SymX(divExactIntegerzh_fast) \
365 SymX(divModIntegerzh_fast) \
367 SymX(forkProcesszh_fast) \
368 SymX(freeHaskellFunctionPtr) \
369 SymX(freeStablePtr) \
370 SymX(gcdIntegerzh_fast) \
371 SymX(gcdIntegerIntzh_fast) \
372 SymX(gcdIntzh_fast) \
375 SymX(int2Integerzh_fast) \
376 SymX(integer2Intzh_fast) \
377 SymX(integer2Wordzh_fast) \
378 SymX(isDoubleDenormalized) \
379 SymX(isDoubleInfinite) \
381 SymX(isDoubleNegativeZero) \
382 SymX(isEmptyMVarzh_fast) \
383 SymX(isFloatDenormalized) \
384 SymX(isFloatInfinite) \
386 SymX(isFloatNegativeZero) \
387 SymX(killThreadzh_fast) \
388 SymX(makeStablePtrzh_fast) \
389 SymX(minusIntegerzh_fast) \
390 SymX(mkApUpd0zh_fast) \
391 SymX(myThreadIdzh_fast) \
392 SymX(labelThreadzh_fast) \
393 SymX(newArrayzh_fast) \
394 SymX(newBCOzh_fast) \
395 SymX(newByteArrayzh_fast) \
396 SymX_redirect(newCAF, newDynCAF) \
397 SymX(newMVarzh_fast) \
398 SymX(newMutVarzh_fast) \
399 SymX(atomicModifyMutVarzh_fast) \
400 SymX(newPinnedByteArrayzh_fast) \
401 SymX(orIntegerzh_fast) \
403 SymX(plusIntegerzh_fast) \
406 SymX(putMVarzh_fast) \
407 SymX(quotIntegerzh_fast) \
408 SymX(quotRemIntegerzh_fast) \
410 SymX(remIntegerzh_fast) \
411 SymX(resetNonBlockingFd) \
414 SymX(rts_checkSchedStatus) \
417 SymX(rts_evalLazyIO) \
421 SymX(rts_getDouble) \
426 SymX(rts_getFunPtr) \
427 SymX(rts_getStablePtr) \
428 SymX(rts_getThreadId) \
430 SymX(rts_getWord32) \
443 SymX(rts_mkStablePtr) \
453 SymX(startupHaskell) \
454 SymX(shutdownHaskell) \
455 SymX(shutdownHaskellAndExit) \
456 SymX(stable_ptr_table) \
457 SymX(stackOverflow) \
458 SymX(stg_CAF_BLACKHOLE_info) \
459 SymX(stg_CHARLIKE_closure) \
460 SymX(stg_EMPTY_MVAR_info) \
461 SymX(stg_IND_STATIC_info) \
462 SymX(stg_INTLIKE_closure) \
463 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
464 SymX(stg_WEAK_info) \
465 SymX(stg_ap_v_info) \
466 SymX(stg_ap_f_info) \
467 SymX(stg_ap_d_info) \
468 SymX(stg_ap_l_info) \
469 SymX(stg_ap_n_info) \
470 SymX(stg_ap_p_info) \
471 SymX(stg_ap_pv_info) \
472 SymX(stg_ap_pp_info) \
473 SymX(stg_ap_ppv_info) \
474 SymX(stg_ap_ppp_info) \
475 SymX(stg_ap_pppp_info) \
476 SymX(stg_ap_ppppp_info) \
477 SymX(stg_ap_pppppp_info) \
478 SymX(stg_ap_ppppppp_info) \
486 SymX(stg_ap_pv_ret) \
487 SymX(stg_ap_pp_ret) \
488 SymX(stg_ap_ppv_ret) \
489 SymX(stg_ap_ppp_ret) \
490 SymX(stg_ap_pppp_ret) \
491 SymX(stg_ap_ppppp_ret) \
492 SymX(stg_ap_pppppp_ret) \
493 SymX(stg_ap_ppppppp_ret) \
494 SymX(stg_ap_1_upd_info) \
495 SymX(stg_ap_2_upd_info) \
496 SymX(stg_ap_3_upd_info) \
497 SymX(stg_ap_4_upd_info) \
498 SymX(stg_ap_5_upd_info) \
499 SymX(stg_ap_6_upd_info) \
500 SymX(stg_ap_7_upd_info) \
501 SymX(stg_ap_8_upd_info) \
503 SymX(stg_sel_0_upd_info) \
504 SymX(stg_sel_10_upd_info) \
505 SymX(stg_sel_11_upd_info) \
506 SymX(stg_sel_12_upd_info) \
507 SymX(stg_sel_13_upd_info) \
508 SymX(stg_sel_14_upd_info) \
509 SymX(stg_sel_15_upd_info) \
510 SymX(stg_sel_1_upd_info) \
511 SymX(stg_sel_2_upd_info) \
512 SymX(stg_sel_3_upd_info) \
513 SymX(stg_sel_4_upd_info) \
514 SymX(stg_sel_5_upd_info) \
515 SymX(stg_sel_6_upd_info) \
516 SymX(stg_sel_7_upd_info) \
517 SymX(stg_sel_8_upd_info) \
518 SymX(stg_sel_9_upd_info) \
519 SymX(stg_upd_frame_info) \
520 SymX(suspendThread) \
521 SymX(takeMVarzh_fast) \
522 SymX(timesIntegerzh_fast) \
523 SymX(tryPutMVarzh_fast) \
524 SymX(tryTakeMVarzh_fast) \
525 SymX(unblockAsyncExceptionszh_fast) \
526 SymX(unsafeThawArrayzh_fast) \
527 SymX(waitReadzh_fast) \
528 SymX(waitWritezh_fast) \
529 SymX(word2Integerzh_fast) \
530 SymX(xorIntegerzh_fast) \
533 #ifdef SUPPORT_LONG_LONGS
534 #define RTS_LONG_LONG_SYMS \
535 SymX(int64ToIntegerzh_fast) \
536 SymX(word64ToIntegerzh_fast)
538 #define RTS_LONG_LONG_SYMS /* nothing */
541 #ifdef ia64_TARGET_ARCH
542 /* force these symbols to be present */
543 #define RTS_EXTRA_SYMBOLS \
545 #elif defined(powerpc_TARGET_ARCH)
546 #define RTS_EXTRA_SYMBOLS \
556 // Symbols that don't have a leading underscore
557 // on Mac OS X. They have to receive special treatment,
558 // see machoInitSymbolsWithoutUnderscore()
559 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
563 #define RTS_EXTRA_SYMBOLS /* nothing */
566 /* entirely bogus claims about types of these symbols */
567 #define Sym(vvv) extern void (vvv);
568 #define SymX(vvv) /**/
569 #define SymX_redirect(vvv,xxx) /**/
573 RTS_POSIX_ONLY_SYMBOLS
574 RTS_MINGW_ONLY_SYMBOLS
575 RTS_CYGWIN_ONLY_SYMBOLS
580 #ifdef LEADING_UNDERSCORE
581 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
583 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
586 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
588 #define SymX(vvv) Sym(vvv)
590 // SymX_redirect allows us to redirect references to one symbol to
591 // another symbol. See newCAF/newDynCAF for an example.
592 #define SymX_redirect(vvv,xxx) \
593 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
596 static RtsSymbolVal rtsSyms[] = {
600 RTS_POSIX_ONLY_SYMBOLS
601 RTS_MINGW_ONLY_SYMBOLS
602 RTS_CYGWIN_ONLY_SYMBOLS
603 { 0, 0 } /* sentinel */
606 /* -----------------------------------------------------------------------------
607 * Insert symbols into hash tables, checking for duplicates.
609 static void ghciInsertStrHashTable ( char* obj_name,
615 if (lookupHashTable(table, (StgWord)key) == NULL)
617 insertStrHashTable(table, (StgWord)key, data);
622 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
624 "whilst processing object file\n"
626 "This could be caused by:\n"
627 " * Loading two different object files which export the same symbol\n"
628 " * Specifying the same object file twice on the GHCi command line\n"
629 " * An incorrect `package.conf' entry, causing some object to be\n"
631 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
640 /* -----------------------------------------------------------------------------
641 * initialize the object linker
645 static int linker_init_done = 0 ;
647 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
648 static void *dl_prog_handle;
656 /* Make initLinker idempotent, so we can call it
657 before evey relevant operation; that means we
658 don't need to initialise the linker separately */
659 if (linker_init_done == 1) { return; } else {
660 linker_init_done = 1;
663 symhash = allocStrHashTable();
665 /* populate the symbol table with stuff from the RTS */
666 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
667 ghciInsertStrHashTable("(GHCi built-in symbols)",
668 symhash, sym->lbl, sym->addr);
670 # if defined(OBJFORMAT_MACHO)
671 machoInitSymbolsWithoutUnderscore();
674 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
675 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
679 /* -----------------------------------------------------------------------------
680 * Loading DLL or .so dynamic libraries
681 * -----------------------------------------------------------------------------
683 * Add a DLL from which symbols may be found. In the ELF case, just
684 * do RTLD_GLOBAL-style add, so no further messing around needs to
685 * happen in order that symbols in the loaded .so are findable --
686 * lookupSymbol() will subsequently see them by dlsym on the program's
687 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
689 * In the PEi386 case, open the DLLs and put handles to them in a
690 * linked list. When looking for a symbol, try all handles in the
691 * list. This means that we need to load even DLLs that are guaranteed
692 * to be in the ghc.exe image already, just so we can get a handle
693 * to give to loadSymbol, so that we can find the symbols. For such
694 * libraries, the LoadLibrary call should be a no-op except for returning
699 #if defined(OBJFORMAT_PEi386)
700 /* A record for storing handles into DLLs. */
705 struct _OpenedDLL* next;
710 /* A list thereof. */
711 static OpenedDLL* opened_dlls = NULL;
715 addDLL( char *dll_name )
717 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
718 /* ------------------- ELF DLL loader ------------------- */
724 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
726 /* dlopen failed; return a ptr to the error msg. */
728 if (errmsg == NULL) errmsg = "addDLL: unknown error";
735 # elif defined(OBJFORMAT_PEi386)
736 /* ------------------- Win32 DLL loader ------------------- */
744 /* fprintf(stderr, "\naddDLL; dll_name = `%s'\n", dll_name); */
746 /* See if we've already got it, and ignore if so. */
747 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
748 if (0 == strcmp(o_dll->name, dll_name))
752 /* The file name has no suffix (yet) so that we can try
753 both foo.dll and foo.drv
755 The documentation for LoadLibrary says:
756 If no file name extension is specified in the lpFileName
757 parameter, the default library extension .dll is
758 appended. However, the file name string can include a trailing
759 point character (.) to indicate that the module name has no
762 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
763 sprintf(buf, "%s.DLL", dll_name);
764 instance = LoadLibrary(buf);
765 if (instance == NULL) {
766 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
767 instance = LoadLibrary(buf);
768 if (instance == NULL) {
771 /* LoadLibrary failed; return a ptr to the error msg. */
772 return "addDLL: unknown error";
777 /* Add this DLL to the list of DLLs in which to search for symbols. */
778 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
779 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
780 strcpy(o_dll->name, dll_name);
781 o_dll->instance = instance;
782 o_dll->next = opened_dlls;
787 barf("addDLL: not implemented on this platform");
791 /* -----------------------------------------------------------------------------
792 * lookup a symbol in the hash table
795 lookupSymbol( char *lbl )
799 ASSERT(symhash != NULL);
800 val = lookupStrHashTable(symhash, lbl);
803 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
804 return dlsym(dl_prog_handle, lbl);
805 # elif defined(OBJFORMAT_PEi386)
808 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
809 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
811 /* HACK: if the name has an initial underscore, try stripping
812 it off & look that up first. I've yet to verify whether there's
813 a Rule that governs whether an initial '_' *should always* be
814 stripped off when mapping from import lib name to the DLL name.
816 sym = GetProcAddress(o_dll->instance, (lbl+1));
818 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
822 sym = GetProcAddress(o_dll->instance, lbl);
824 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
839 __attribute((unused))
841 lookupLocalSymbol( ObjectCode* oc, char *lbl )
845 val = lookupStrHashTable(oc->lochash, lbl);
855 /* -----------------------------------------------------------------------------
856 * Debugging aid: look in GHCi's object symbol tables for symbols
857 * within DELTA bytes of the specified address, and show their names.
860 void ghci_enquire ( char* addr );
862 void ghci_enquire ( char* addr )
867 const int DELTA = 64;
872 for (oc = objects; oc; oc = oc->next) {
873 for (i = 0; i < oc->n_symbols; i++) {
874 sym = oc->symbols[i];
875 if (sym == NULL) continue;
876 // fprintf(stderr, "enquire %p %p\n", sym, oc->lochash);
878 if (oc->lochash != NULL) {
879 a = lookupStrHashTable(oc->lochash, sym);
882 a = lookupStrHashTable(symhash, sym);
885 // fprintf(stderr, "ghci_enquire: can't find %s\n", sym);
887 else if (addr-DELTA <= a && a <= addr+DELTA) {
888 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
895 #ifdef ia64_TARGET_ARCH
896 static unsigned int PLTSize(void);
899 /* -----------------------------------------------------------------------------
900 * Load an obj (populate the global symbol table, but don't resolve yet)
902 * Returns: 1 if ok, 0 on error.
905 loadObj( char *path )
919 /* fprintf(stderr, "loadObj %s\n", path ); */
921 /* Check that we haven't already loaded this object. Don't give up
922 at this stage; ocGetNames_* will barf later. */
926 for (o = objects; o; o = o->next) {
927 if (0 == strcmp(o->fileName, path))
933 "GHCi runtime linker: warning: looks like you're trying to load the\n"
934 "same object file twice:\n"
936 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
942 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
944 # if defined(OBJFORMAT_ELF)
945 oc->formatName = "ELF";
946 # elif defined(OBJFORMAT_PEi386)
947 oc->formatName = "PEi386";
948 # elif defined(OBJFORMAT_MACHO)
949 oc->formatName = "Mach-O";
952 barf("loadObj: not implemented on this platform");
956 if (r == -1) { return 0; }
958 /* sigh, strdup() isn't a POSIX function, so do it the long way */
959 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
960 strcpy(oc->fileName, path);
962 oc->fileSize = st.st_size;
965 oc->lochash = allocStrHashTable();
966 oc->proddables = NULL;
968 /* chain it onto the list of objects */
973 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
975 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
977 fd = open(path, O_RDONLY);
979 barf("loadObj: can't open `%s'", path);
981 pagesize = getpagesize();
983 #ifdef ia64_TARGET_ARCH
984 /* The PLT needs to be right before the object */
985 n = ROUND_UP(PLTSize(), pagesize);
986 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
987 if (oc->plt == MAP_FAILED)
988 barf("loadObj: can't allocate PLT");
991 map_addr = oc->plt + n;
994 n = ROUND_UP(oc->fileSize, pagesize);
995 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
996 if (oc->image == MAP_FAILED)
997 barf("loadObj: can't map `%s'", path);
1001 #else /* !USE_MMAP */
1003 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
1005 /* load the image into memory */
1006 f = fopen(path, "rb");
1008 barf("loadObj: can't read `%s'", path);
1010 n = fread ( oc->image, 1, oc->fileSize, f );
1011 if (n != oc->fileSize)
1012 barf("loadObj: error whilst reading `%s'", path);
1016 #endif /* USE_MMAP */
1018 /* verify the in-memory image */
1019 # if defined(OBJFORMAT_ELF)
1020 r = ocVerifyImage_ELF ( oc );
1021 # elif defined(OBJFORMAT_PEi386)
1022 r = ocVerifyImage_PEi386 ( oc );
1023 # elif defined(OBJFORMAT_MACHO)
1024 r = ocVerifyImage_MachO ( oc );
1026 barf("loadObj: no verify method");
1028 if (!r) { return r; }
1030 /* build the symbol list for this image */
1031 # if defined(OBJFORMAT_ELF)
1032 r = ocGetNames_ELF ( oc );
1033 # elif defined(OBJFORMAT_PEi386)
1034 r = ocGetNames_PEi386 ( oc );
1035 # elif defined(OBJFORMAT_MACHO)
1036 r = ocGetNames_MachO ( oc );
1038 barf("loadObj: no getNames method");
1040 if (!r) { return r; }
1042 /* loaded, but not resolved yet */
1043 oc->status = OBJECT_LOADED;
1048 /* -----------------------------------------------------------------------------
1049 * resolve all the currently unlinked objects in memory
1051 * Returns: 1 if ok, 0 on error.
1061 for (oc = objects; oc; oc = oc->next) {
1062 if (oc->status != OBJECT_RESOLVED) {
1063 # if defined(OBJFORMAT_ELF)
1064 r = ocResolve_ELF ( oc );
1065 # elif defined(OBJFORMAT_PEi386)
1066 r = ocResolve_PEi386 ( oc );
1067 # elif defined(OBJFORMAT_MACHO)
1068 r = ocResolve_MachO ( oc );
1070 barf("resolveObjs: not implemented on this platform");
1072 if (!r) { return r; }
1073 oc->status = OBJECT_RESOLVED;
1079 /* -----------------------------------------------------------------------------
1080 * delete an object from the pool
1083 unloadObj( char *path )
1085 ObjectCode *oc, *prev;
1087 ASSERT(symhash != NULL);
1088 ASSERT(objects != NULL);
1093 for (oc = objects; oc; prev = oc, oc = oc->next) {
1094 if (!strcmp(oc->fileName,path)) {
1096 /* Remove all the mappings for the symbols within this
1101 for (i = 0; i < oc->n_symbols; i++) {
1102 if (oc->symbols[i] != NULL) {
1103 removeStrHashTable(symhash, oc->symbols[i], NULL);
1111 prev->next = oc->next;
1114 /* We're going to leave this in place, in case there are
1115 any pointers from the heap into it: */
1116 /* free(oc->image); */
1120 /* The local hash table should have been freed at the end
1121 of the ocResolve_ call on it. */
1122 ASSERT(oc->lochash == NULL);
1128 belch("unloadObj: can't find `%s' to unload", path);
1132 /* -----------------------------------------------------------------------------
1133 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1134 * which may be prodded during relocation, and abort if we try and write
1135 * outside any of these.
1137 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1140 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1141 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1145 pb->next = oc->proddables;
1146 oc->proddables = pb;
1149 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1152 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1153 char* s = (char*)(pb->start);
1154 char* e = s + pb->size - 1;
1155 char* a = (char*)addr;
1156 /* Assumes that the biggest fixup involves a 4-byte write. This
1157 probably needs to be changed to 8 (ie, +7) on 64-bit
1159 if (a >= s && (a+3) <= e) return;
1161 barf("checkProddableBlock: invalid fixup in runtime linker");
1164 /* -----------------------------------------------------------------------------
1165 * Section management.
1167 static void addSection ( ObjectCode* oc, SectionKind kind,
1168 void* start, void* end )
1170 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1174 s->next = oc->sections;
1177 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1178 start, ((char*)end)-1, end - start + 1, kind );
1184 /* --------------------------------------------------------------------------
1185 * PEi386 specifics (Win32 targets)
1186 * ------------------------------------------------------------------------*/
1188 /* The information for this linker comes from
1189 Microsoft Portable Executable
1190 and Common Object File Format Specification
1191 revision 5.1 January 1998
1192 which SimonM says comes from the MS Developer Network CDs.
1194 It can be found there (on older CDs), but can also be found
1197 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1199 (this is Rev 6.0 from February 1999).
1201 Things move, so if that fails, try searching for it via
1203 http://www.google.com/search?q=PE+COFF+specification
1205 The ultimate reference for the PE format is the Winnt.h
1206 header file that comes with the Platform SDKs; as always,
1207 implementations will drift wrt their documentation.
1209 A good background article on the PE format is Matt Pietrek's
1210 March 1994 article in Microsoft System Journal (MSJ)
1211 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1212 Win32 Portable Executable File Format." The info in there
1213 has recently been updated in a two part article in
1214 MSDN magazine, issues Feb and March 2002,
1215 "Inside Windows: An In-Depth Look into the Win32 Portable
1216 Executable File Format"
1218 John Levine's book "Linkers and Loaders" contains useful
1223 #if defined(OBJFORMAT_PEi386)
1227 typedef unsigned char UChar;
1228 typedef unsigned short UInt16;
1229 typedef unsigned int UInt32;
1236 UInt16 NumberOfSections;
1237 UInt32 TimeDateStamp;
1238 UInt32 PointerToSymbolTable;
1239 UInt32 NumberOfSymbols;
1240 UInt16 SizeOfOptionalHeader;
1241 UInt16 Characteristics;
1245 #define sizeof_COFF_header 20
1252 UInt32 VirtualAddress;
1253 UInt32 SizeOfRawData;
1254 UInt32 PointerToRawData;
1255 UInt32 PointerToRelocations;
1256 UInt32 PointerToLinenumbers;
1257 UInt16 NumberOfRelocations;
1258 UInt16 NumberOfLineNumbers;
1259 UInt32 Characteristics;
1263 #define sizeof_COFF_section 40
1270 UInt16 SectionNumber;
1273 UChar NumberOfAuxSymbols;
1277 #define sizeof_COFF_symbol 18
1282 UInt32 VirtualAddress;
1283 UInt32 SymbolTableIndex;
1288 #define sizeof_COFF_reloc 10
1291 /* From PE spec doc, section 3.3.2 */
1292 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1293 windows.h -- for the same purpose, but I want to know what I'm
1295 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1296 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1297 #define MYIMAGE_FILE_DLL 0x2000
1298 #define MYIMAGE_FILE_SYSTEM 0x1000
1299 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1300 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1301 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1303 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1304 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1305 #define MYIMAGE_SYM_CLASS_STATIC 3
1306 #define MYIMAGE_SYM_UNDEFINED 0
1308 /* From PE spec doc, section 4.1 */
1309 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1310 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1311 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1313 /* From PE spec doc, section 5.2.1 */
1314 #define MYIMAGE_REL_I386_DIR32 0x0006
1315 #define MYIMAGE_REL_I386_REL32 0x0014
1318 /* We use myindex to calculate array addresses, rather than
1319 simply doing the normal subscript thing. That's because
1320 some of the above structs have sizes which are not
1321 a whole number of words. GCC rounds their sizes up to a
1322 whole number of words, which means that the address calcs
1323 arising from using normal C indexing or pointer arithmetic
1324 are just plain wrong. Sigh.
1327 myindex ( int scale, void* base, int index )
1330 ((UChar*)base) + scale * index;
1335 printName ( UChar* name, UChar* strtab )
1337 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1338 UInt32 strtab_offset = * (UInt32*)(name+4);
1339 fprintf ( stderr, "%s", strtab + strtab_offset );
1342 for (i = 0; i < 8; i++) {
1343 if (name[i] == 0) break;
1344 fprintf ( stderr, "%c", name[i] );
1351 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1353 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1354 UInt32 strtab_offset = * (UInt32*)(name+4);
1355 strncpy ( dst, strtab+strtab_offset, dstSize );
1361 if (name[i] == 0) break;
1371 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1374 /* If the string is longer than 8 bytes, look in the
1375 string table for it -- this will be correctly zero terminated.
1377 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1378 UInt32 strtab_offset = * (UInt32*)(name+4);
1379 return ((UChar*)strtab) + strtab_offset;
1381 /* Otherwise, if shorter than 8 bytes, return the original,
1382 which by defn is correctly terminated.
1384 if (name[7]==0) return name;
1385 /* The annoying case: 8 bytes. Copy into a temporary
1386 (which is never freed ...)
1388 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1390 strncpy(newstr,name,8);
1396 /* Just compares the short names (first 8 chars) */
1397 static COFF_section *
1398 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1402 = (COFF_header*)(oc->image);
1403 COFF_section* sectab
1405 ((UChar*)(oc->image))
1406 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1408 for (i = 0; i < hdr->NumberOfSections; i++) {
1411 COFF_section* section_i
1413 myindex ( sizeof_COFF_section, sectab, i );
1414 n1 = (UChar*) &(section_i->Name);
1416 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1417 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1418 n1[6]==n2[6] && n1[7]==n2[7])
1427 zapTrailingAtSign ( UChar* sym )
1429 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1431 if (sym[0] == 0) return;
1433 while (sym[i] != 0) i++;
1436 while (j > 0 && my_isdigit(sym[j])) j--;
1437 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1443 ocVerifyImage_PEi386 ( ObjectCode* oc )
1448 COFF_section* sectab;
1449 COFF_symbol* symtab;
1451 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1452 hdr = (COFF_header*)(oc->image);
1453 sectab = (COFF_section*) (
1454 ((UChar*)(oc->image))
1455 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1457 symtab = (COFF_symbol*) (
1458 ((UChar*)(oc->image))
1459 + hdr->PointerToSymbolTable
1461 strtab = ((UChar*)symtab)
1462 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1464 if (hdr->Machine != 0x14c) {
1465 belch("Not x86 PEi386");
1468 if (hdr->SizeOfOptionalHeader != 0) {
1469 belch("PEi386 with nonempty optional header");
1472 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1473 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1474 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1475 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1476 belch("Not a PEi386 object file");
1479 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1480 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1481 belch("Invalid PEi386 word size or endiannness: %d",
1482 (int)(hdr->Characteristics));
1485 /* If the string table size is way crazy, this might indicate that
1486 there are more than 64k relocations, despite claims to the
1487 contrary. Hence this test. */
1488 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1490 if ( (*(UInt32*)strtab) > 600000 ) {
1491 /* Note that 600k has no special significance other than being
1492 big enough to handle the almost-2MB-sized lumps that
1493 constitute HSwin32*.o. */
1494 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1499 /* No further verification after this point; only debug printing. */
1501 IF_DEBUG(linker, i=1);
1502 if (i == 0) return 1;
1505 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1507 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1509 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1511 fprintf ( stderr, "\n" );
1513 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1515 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1517 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1519 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1521 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1523 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1525 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1527 /* Print the section table. */
1528 fprintf ( stderr, "\n" );
1529 for (i = 0; i < hdr->NumberOfSections; i++) {
1531 COFF_section* sectab_i
1533 myindex ( sizeof_COFF_section, sectab, i );
1540 printName ( sectab_i->Name, strtab );
1550 sectab_i->VirtualSize,
1551 sectab_i->VirtualAddress,
1552 sectab_i->SizeOfRawData,
1553 sectab_i->PointerToRawData,
1554 sectab_i->NumberOfRelocations,
1555 sectab_i->PointerToRelocations,
1556 sectab_i->PointerToRawData
1558 reltab = (COFF_reloc*) (
1559 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1562 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1563 /* If the relocation field (a short) has overflowed, the
1564 * real count can be found in the first reloc entry.
1566 * See Section 4.1 (last para) of the PE spec (rev6.0).
1568 COFF_reloc* rel = (COFF_reloc*)
1569 myindex ( sizeof_COFF_reloc, reltab, 0 );
1570 noRelocs = rel->VirtualAddress;
1573 noRelocs = sectab_i->NumberOfRelocations;
1577 for (; j < noRelocs; j++) {
1579 COFF_reloc* rel = (COFF_reloc*)
1580 myindex ( sizeof_COFF_reloc, reltab, j );
1582 " type 0x%-4x vaddr 0x%-8x name `",
1584 rel->VirtualAddress );
1585 sym = (COFF_symbol*)
1586 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1587 /* Hmm..mysterious looking offset - what's it for? SOF */
1588 printName ( sym->Name, strtab -10 );
1589 fprintf ( stderr, "'\n" );
1592 fprintf ( stderr, "\n" );
1594 fprintf ( stderr, "\n" );
1595 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1596 fprintf ( stderr, "---START of string table---\n");
1597 for (i = 4; i < *(Int32*)strtab; i++) {
1599 fprintf ( stderr, "\n"); else
1600 fprintf( stderr, "%c", strtab[i] );
1602 fprintf ( stderr, "--- END of string table---\n");
1604 fprintf ( stderr, "\n" );
1607 COFF_symbol* symtab_i;
1608 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1609 symtab_i = (COFF_symbol*)
1610 myindex ( sizeof_COFF_symbol, symtab, i );
1616 printName ( symtab_i->Name, strtab );
1625 (Int32)(symtab_i->SectionNumber),
1626 (UInt32)symtab_i->Type,
1627 (UInt32)symtab_i->StorageClass,
1628 (UInt32)symtab_i->NumberOfAuxSymbols
1630 i += symtab_i->NumberOfAuxSymbols;
1634 fprintf ( stderr, "\n" );
1640 ocGetNames_PEi386 ( ObjectCode* oc )
1643 COFF_section* sectab;
1644 COFF_symbol* symtab;
1651 hdr = (COFF_header*)(oc->image);
1652 sectab = (COFF_section*) (
1653 ((UChar*)(oc->image))
1654 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1656 symtab = (COFF_symbol*) (
1657 ((UChar*)(oc->image))
1658 + hdr->PointerToSymbolTable
1660 strtab = ((UChar*)(oc->image))
1661 + hdr->PointerToSymbolTable
1662 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1664 /* Allocate space for any (local, anonymous) .bss sections. */
1666 for (i = 0; i < hdr->NumberOfSections; i++) {
1668 COFF_section* sectab_i
1670 myindex ( sizeof_COFF_section, sectab, i );
1671 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1672 if (sectab_i->VirtualSize == 0) continue;
1673 /* This is a non-empty .bss section. Allocate zeroed space for
1674 it, and set its PointerToRawData field such that oc->image +
1675 PointerToRawData == addr_of_zeroed_space. */
1676 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1677 "ocGetNames_PEi386(anonymous bss)");
1678 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1679 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1680 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1683 /* Copy section information into the ObjectCode. */
1685 for (i = 0; i < hdr->NumberOfSections; i++) {
1691 = SECTIONKIND_OTHER;
1692 COFF_section* sectab_i
1694 myindex ( sizeof_COFF_section, sectab, i );
1695 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1698 /* I'm sure this is the Right Way to do it. However, the
1699 alternative of testing the sectab_i->Name field seems to
1700 work ok with Cygwin.
1702 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1703 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1704 kind = SECTIONKIND_CODE_OR_RODATA;
1707 if (0==strcmp(".text",sectab_i->Name) ||
1708 0==strcmp(".rodata",sectab_i->Name))
1709 kind = SECTIONKIND_CODE_OR_RODATA;
1710 if (0==strcmp(".data",sectab_i->Name) ||
1711 0==strcmp(".bss",sectab_i->Name))
1712 kind = SECTIONKIND_RWDATA;
1714 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1715 sz = sectab_i->SizeOfRawData;
1716 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1718 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1719 end = start + sz - 1;
1721 if (kind == SECTIONKIND_OTHER
1722 /* Ignore sections called which contain stabs debugging
1724 && 0 != strcmp(".stab", sectab_i->Name)
1725 && 0 != strcmp(".stabstr", sectab_i->Name)
1727 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1731 if (kind != SECTIONKIND_OTHER && end >= start) {
1732 addSection(oc, kind, start, end);
1733 addProddableBlock(oc, start, end - start + 1);
1737 /* Copy exported symbols into the ObjectCode. */
1739 oc->n_symbols = hdr->NumberOfSymbols;
1740 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1741 "ocGetNames_PEi386(oc->symbols)");
1742 /* Call me paranoid; I don't care. */
1743 for (i = 0; i < oc->n_symbols; i++)
1744 oc->symbols[i] = NULL;
1748 COFF_symbol* symtab_i;
1749 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1750 symtab_i = (COFF_symbol*)
1751 myindex ( sizeof_COFF_symbol, symtab, i );
1755 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1756 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1757 /* This symbol is global and defined, viz, exported */
1758 /* for MYIMAGE_SYMCLASS_EXTERNAL
1759 && !MYIMAGE_SYM_UNDEFINED,
1760 the address of the symbol is:
1761 address of relevant section + offset in section
1763 COFF_section* sectabent
1764 = (COFF_section*) myindex ( sizeof_COFF_section,
1766 symtab_i->SectionNumber-1 );
1767 addr = ((UChar*)(oc->image))
1768 + (sectabent->PointerToRawData
1772 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1773 && symtab_i->Value > 0) {
1774 /* This symbol isn't in any section at all, ie, global bss.
1775 Allocate zeroed space for it. */
1776 addr = stgCallocBytes(1, symtab_i->Value,
1777 "ocGetNames_PEi386(non-anonymous bss)");
1778 addSection(oc, SECTIONKIND_RWDATA, addr,
1779 ((UChar*)addr) + symtab_i->Value - 1);
1780 addProddableBlock(oc, addr, symtab_i->Value);
1781 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1784 if (addr != NULL ) {
1785 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1786 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1787 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1788 ASSERT(i >= 0 && i < oc->n_symbols);
1789 /* cstring_from_COFF_symbol_name always succeeds. */
1790 oc->symbols[i] = sname;
1791 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1795 "IGNORING symbol %d\n"
1799 printName ( symtab_i->Name, strtab );
1808 (Int32)(symtab_i->SectionNumber),
1809 (UInt32)symtab_i->Type,
1810 (UInt32)symtab_i->StorageClass,
1811 (UInt32)symtab_i->NumberOfAuxSymbols
1816 i += symtab_i->NumberOfAuxSymbols;
1825 ocResolve_PEi386 ( ObjectCode* oc )
1828 COFF_section* sectab;
1829 COFF_symbol* symtab;
1839 /* ToDo: should be variable-sized? But is at least safe in the
1840 sense of buffer-overrun-proof. */
1842 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1844 hdr = (COFF_header*)(oc->image);
1845 sectab = (COFF_section*) (
1846 ((UChar*)(oc->image))
1847 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1849 symtab = (COFF_symbol*) (
1850 ((UChar*)(oc->image))
1851 + hdr->PointerToSymbolTable
1853 strtab = ((UChar*)(oc->image))
1854 + hdr->PointerToSymbolTable
1855 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1857 for (i = 0; i < hdr->NumberOfSections; i++) {
1858 COFF_section* sectab_i
1860 myindex ( sizeof_COFF_section, sectab, i );
1863 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1866 /* Ignore sections called which contain stabs debugging
1868 if (0 == strcmp(".stab", sectab_i->Name)
1869 || 0 == strcmp(".stabstr", sectab_i->Name))
1872 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1873 /* If the relocation field (a short) has overflowed, the
1874 * real count can be found in the first reloc entry.
1876 * See Section 4.1 (last para) of the PE spec (rev6.0).
1878 COFF_reloc* rel = (COFF_reloc*)
1879 myindex ( sizeof_COFF_reloc, reltab, 0 );
1880 noRelocs = rel->VirtualAddress;
1881 fprintf(stderr, "Overflown relocs: %u\n", noRelocs);
1884 noRelocs = sectab_i->NumberOfRelocations;
1889 for (; j < noRelocs; j++) {
1891 COFF_reloc* reltab_j
1893 myindex ( sizeof_COFF_reloc, reltab, j );
1895 /* the location to patch */
1897 ((UChar*)(oc->image))
1898 + (sectab_i->PointerToRawData
1899 + reltab_j->VirtualAddress
1900 - sectab_i->VirtualAddress )
1902 /* the existing contents of pP */
1904 /* the symbol to connect to */
1905 sym = (COFF_symbol*)
1906 myindex ( sizeof_COFF_symbol,
1907 symtab, reltab_j->SymbolTableIndex );
1910 "reloc sec %2d num %3d: type 0x%-4x "
1911 "vaddr 0x%-8x name `",
1913 (UInt32)reltab_j->Type,
1914 reltab_j->VirtualAddress );
1915 printName ( sym->Name, strtab );
1916 fprintf ( stderr, "'\n" ));
1918 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1919 COFF_section* section_sym
1920 = findPEi386SectionCalled ( oc, sym->Name );
1922 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1925 S = ((UInt32)(oc->image))
1926 + (section_sym->PointerToRawData
1929 copyName ( sym->Name, strtab, symbol, 1000-1 );
1930 (void*)S = lookupLocalSymbol( oc, symbol );
1931 if ((void*)S != NULL) goto foundit;
1932 (void*)S = lookupSymbol( symbol );
1933 if ((void*)S != NULL) goto foundit;
1934 zapTrailingAtSign ( symbol );
1935 (void*)S = lookupLocalSymbol( oc, symbol );
1936 if ((void*)S != NULL) goto foundit;
1937 (void*)S = lookupSymbol( symbol );
1938 if ((void*)S != NULL) goto foundit;
1939 /* Newline first because the interactive linker has printed "linking..." */
1940 belch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
1944 checkProddableBlock(oc, pP);
1945 switch (reltab_j->Type) {
1946 case MYIMAGE_REL_I386_DIR32:
1949 case MYIMAGE_REL_I386_REL32:
1950 /* Tricky. We have to insert a displacement at
1951 pP which, when added to the PC for the _next_
1952 insn, gives the address of the target (S).
1953 Problem is to know the address of the next insn
1954 when we only know pP. We assume that this
1955 literal field is always the last in the insn,
1956 so that the address of the next insn is pP+4
1957 -- hence the constant 4.
1958 Also I don't know if A should be added, but so
1959 far it has always been zero.
1962 *pP = S - ((UInt32)pP) - 4;
1965 belch("%s: unhandled PEi386 relocation type %d",
1966 oc->fileName, reltab_j->Type);
1973 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1977 #endif /* defined(OBJFORMAT_PEi386) */
1980 /* --------------------------------------------------------------------------
1982 * ------------------------------------------------------------------------*/
1984 #if defined(OBJFORMAT_ELF)
1989 #if defined(sparc_TARGET_ARCH)
1990 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1991 #elif defined(i386_TARGET_ARCH)
1992 # define ELF_TARGET_386 /* Used inside <elf.h> */
1993 #elif defined (ia64_TARGET_ARCH)
1994 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
1996 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
1997 # define ELF_NEED_GOT /* needs Global Offset Table */
1998 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2004 * Define a set of types which can be used for both ELF32 and ELF64
2008 #define ELFCLASS ELFCLASS64
2009 #define Elf_Addr Elf64_Addr
2010 #define Elf_Word Elf64_Word
2011 #define Elf_Sword Elf64_Sword
2012 #define Elf_Ehdr Elf64_Ehdr
2013 #define Elf_Phdr Elf64_Phdr
2014 #define Elf_Shdr Elf64_Shdr
2015 #define Elf_Sym Elf64_Sym
2016 #define Elf_Rel Elf64_Rel
2017 #define Elf_Rela Elf64_Rela
2018 #define ELF_ST_TYPE ELF64_ST_TYPE
2019 #define ELF_ST_BIND ELF64_ST_BIND
2020 #define ELF_R_TYPE ELF64_R_TYPE
2021 #define ELF_R_SYM ELF64_R_SYM
2023 #define ELFCLASS ELFCLASS32
2024 #define Elf_Addr Elf32_Addr
2025 #define Elf_Word Elf32_Word
2026 #define Elf_Sword Elf32_Sword
2027 #define Elf_Ehdr Elf32_Ehdr
2028 #define Elf_Phdr Elf32_Phdr
2029 #define Elf_Shdr Elf32_Shdr
2030 #define Elf_Sym Elf32_Sym
2031 #define Elf_Rel Elf32_Rel
2032 #define Elf_Rela Elf32_Rela
2033 #define ELF_ST_TYPE ELF32_ST_TYPE
2034 #define ELF_ST_BIND ELF32_ST_BIND
2035 #define ELF_R_TYPE ELF32_R_TYPE
2036 #define ELF_R_SYM ELF32_R_SYM
2041 * Functions to allocate entries in dynamic sections. Currently we simply
2042 * preallocate a large number, and we don't check if a entry for the given
2043 * target already exists (a linear search is too slow). Ideally these
2044 * entries would be associated with symbols.
2047 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2048 #define GOT_SIZE 0x20000
2049 #define FUNCTION_TABLE_SIZE 0x10000
2050 #define PLT_SIZE 0x08000
2053 static Elf_Addr got[GOT_SIZE];
2054 static unsigned int gotIndex;
2055 static Elf_Addr gp_val = (Elf_Addr)got;
2058 allocateGOTEntry(Elf_Addr target)
2062 if (gotIndex >= GOT_SIZE)
2063 barf("Global offset table overflow");
2065 entry = &got[gotIndex++];
2067 return (Elf_Addr)entry;
2071 #ifdef ELF_FUNCTION_DESC
2077 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2078 static unsigned int functionTableIndex;
2081 allocateFunctionDesc(Elf_Addr target)
2083 FunctionDesc *entry;
2085 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2086 barf("Function table overflow");
2088 entry = &functionTable[functionTableIndex++];
2090 entry->gp = (Elf_Addr)gp_val;
2091 return (Elf_Addr)entry;
2095 copyFunctionDesc(Elf_Addr target)
2097 FunctionDesc *olddesc = (FunctionDesc *)target;
2098 FunctionDesc *newdesc;
2100 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2101 newdesc->gp = olddesc->gp;
2102 return (Elf_Addr)newdesc;
2107 #ifdef ia64_TARGET_ARCH
2108 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2109 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2111 static unsigned char plt_code[] =
2113 /* taken from binutils bfd/elfxx-ia64.c */
2114 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2115 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2116 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2117 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2118 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2119 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2122 /* If we can't get to the function descriptor via gp, take a local copy of it */
2123 #define PLT_RELOC(code, target) { \
2124 Elf64_Sxword rel_value = target - gp_val; \
2125 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2126 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2128 ia64_reloc_gprel22((Elf_Addr)code, target); \
2133 unsigned char code[sizeof(plt_code)];
2137 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2139 PLTEntry *plt = (PLTEntry *)oc->plt;
2142 if (oc->pltIndex >= PLT_SIZE)
2143 barf("Procedure table overflow");
2145 entry = &plt[oc->pltIndex++];
2146 memcpy(entry->code, plt_code, sizeof(entry->code));
2147 PLT_RELOC(entry->code, target);
2148 return (Elf_Addr)entry;
2154 return (PLT_SIZE * sizeof(PLTEntry));
2160 * Generic ELF functions
2164 findElfSection ( void* objImage, Elf_Word sh_type )
2166 char* ehdrC = (char*)objImage;
2167 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2168 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2169 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2173 for (i = 0; i < ehdr->e_shnum; i++) {
2174 if (shdr[i].sh_type == sh_type
2175 /* Ignore the section header's string table. */
2176 && i != ehdr->e_shstrndx
2177 /* Ignore string tables named .stabstr, as they contain
2179 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2181 ptr = ehdrC + shdr[i].sh_offset;
2188 #if defined(ia64_TARGET_ARCH)
2190 findElfSegment ( void* objImage, Elf_Addr vaddr )
2192 char* ehdrC = (char*)objImage;
2193 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2194 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2195 Elf_Addr segaddr = 0;
2198 for (i = 0; i < ehdr->e_phnum; i++) {
2199 segaddr = phdr[i].p_vaddr;
2200 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2208 ocVerifyImage_ELF ( ObjectCode* oc )
2212 int i, j, nent, nstrtab, nsymtabs;
2216 char* ehdrC = (char*)(oc->image);
2217 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2219 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2220 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2221 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2222 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2223 belch("%s: not an ELF object", oc->fileName);
2227 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2228 belch("%s: unsupported ELF format", oc->fileName);
2232 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2233 IF_DEBUG(linker,belch( "Is little-endian" ));
2235 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2236 IF_DEBUG(linker,belch( "Is big-endian" ));
2238 belch("%s: unknown endiannness", oc->fileName);
2242 if (ehdr->e_type != ET_REL) {
2243 belch("%s: not a relocatable object (.o) file", oc->fileName);
2246 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2248 IF_DEBUG(linker,belch( "Architecture is " ));
2249 switch (ehdr->e_machine) {
2250 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2251 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2253 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2255 default: IF_DEBUG(linker,belch( "unknown" ));
2256 belch("%s: unknown architecture", oc->fileName);
2260 IF_DEBUG(linker,belch(
2261 "\nSection header table: start %d, n_entries %d, ent_size %d",
2262 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2264 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2266 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2268 if (ehdr->e_shstrndx == SHN_UNDEF) {
2269 belch("%s: no section header string table", oc->fileName);
2272 IF_DEBUG(linker,belch( "Section header string table is section %d",
2274 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2277 for (i = 0; i < ehdr->e_shnum; i++) {
2278 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2279 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2280 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2281 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2282 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2283 ehdrC + shdr[i].sh_offset,
2284 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2286 if (shdr[i].sh_type == SHT_REL) {
2287 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2288 } else if (shdr[i].sh_type == SHT_RELA) {
2289 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2291 IF_DEBUG(linker,fprintf(stderr," "));
2294 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2298 IF_DEBUG(linker,belch( "\nString tables" ));
2301 for (i = 0; i < ehdr->e_shnum; i++) {
2302 if (shdr[i].sh_type == SHT_STRTAB
2303 /* Ignore the section header's string table. */
2304 && i != ehdr->e_shstrndx
2305 /* Ignore string tables named .stabstr, as they contain
2307 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2309 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2310 strtab = ehdrC + shdr[i].sh_offset;
2315 belch("%s: no string tables, or too many", oc->fileName);
2320 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2321 for (i = 0; i < ehdr->e_shnum; i++) {
2322 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2323 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2325 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2326 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2327 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2329 shdr[i].sh_size % sizeof(Elf_Sym)
2331 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2332 belch("%s: non-integral number of symbol table entries", oc->fileName);
2335 for (j = 0; j < nent; j++) {
2336 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2337 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2338 (int)stab[j].st_shndx,
2339 (int)stab[j].st_size,
2340 (char*)stab[j].st_value ));
2342 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2343 switch (ELF_ST_TYPE(stab[j].st_info)) {
2344 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2345 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2346 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2347 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2348 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2349 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2351 IF_DEBUG(linker,fprintf(stderr, " " ));
2353 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2354 switch (ELF_ST_BIND(stab[j].st_info)) {
2355 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2356 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2357 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2358 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2360 IF_DEBUG(linker,fprintf(stderr, " " ));
2362 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2366 if (nsymtabs == 0) {
2367 belch("%s: didn't find any symbol tables", oc->fileName);
2376 ocGetNames_ELF ( ObjectCode* oc )
2381 char* ehdrC = (char*)(oc->image);
2382 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2383 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2384 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2386 ASSERT(symhash != NULL);
2389 belch("%s: no strtab", oc->fileName);
2394 for (i = 0; i < ehdr->e_shnum; i++) {
2395 /* Figure out what kind of section it is. Logic derived from
2396 Figure 1.14 ("Special Sections") of the ELF document
2397 ("Portable Formats Specification, Version 1.1"). */
2398 Elf_Shdr hdr = shdr[i];
2399 SectionKind kind = SECTIONKIND_OTHER;
2402 if (hdr.sh_type == SHT_PROGBITS
2403 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2404 /* .text-style section */
2405 kind = SECTIONKIND_CODE_OR_RODATA;
2408 if (hdr.sh_type == SHT_PROGBITS
2409 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2410 /* .data-style section */
2411 kind = SECTIONKIND_RWDATA;
2414 if (hdr.sh_type == SHT_PROGBITS
2415 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2416 /* .rodata-style section */
2417 kind = SECTIONKIND_CODE_OR_RODATA;
2420 if (hdr.sh_type == SHT_NOBITS
2421 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2422 /* .bss-style section */
2423 kind = SECTIONKIND_RWDATA;
2427 if (is_bss && shdr[i].sh_size > 0) {
2428 /* This is a non-empty .bss section. Allocate zeroed space for
2429 it, and set its .sh_offset field such that
2430 ehdrC + .sh_offset == addr_of_zeroed_space. */
2431 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2432 "ocGetNames_ELF(BSS)");
2433 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2435 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2436 zspace, shdr[i].sh_size);
2440 /* fill in the section info */
2441 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2442 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2443 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2444 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2447 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2449 /* copy stuff into this module's object symbol table */
2450 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2451 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2453 oc->n_symbols = nent;
2454 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2455 "ocGetNames_ELF(oc->symbols)");
2457 for (j = 0; j < nent; j++) {
2459 char isLocal = FALSE; /* avoids uninit-var warning */
2461 char* nm = strtab + stab[j].st_name;
2462 int secno = stab[j].st_shndx;
2464 /* Figure out if we want to add it; if so, set ad to its
2465 address. Otherwise leave ad == NULL. */
2467 if (secno == SHN_COMMON) {
2469 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2471 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2472 stab[j].st_size, nm);
2474 /* Pointless to do addProddableBlock() for this area,
2475 since the linker should never poke around in it. */
2478 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2479 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2481 /* and not an undefined symbol */
2482 && stab[j].st_shndx != SHN_UNDEF
2483 /* and not in a "special section" */
2484 && stab[j].st_shndx < SHN_LORESERVE
2486 /* and it's a not a section or string table or anything silly */
2487 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2488 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2489 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2492 /* Section 0 is the undefined section, hence > and not >=. */
2493 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2495 if (shdr[secno].sh_type == SHT_NOBITS) {
2496 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2497 stab[j].st_size, stab[j].st_value, nm);
2500 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2501 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2504 #ifdef ELF_FUNCTION_DESC
2505 /* dlsym() and the initialisation table both give us function
2506 * descriptors, so to be consistent we store function descriptors
2507 * in the symbol table */
2508 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2509 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2511 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2512 ad, oc->fileName, nm ));
2517 /* And the decision is ... */
2521 oc->symbols[j] = nm;
2524 /* Ignore entirely. */
2526 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2530 IF_DEBUG(linker,belch( "skipping `%s'",
2531 strtab + stab[j].st_name ));
2534 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2535 (int)ELF_ST_BIND(stab[j].st_info),
2536 (int)ELF_ST_TYPE(stab[j].st_info),
2537 (int)stab[j].st_shndx,
2538 strtab + stab[j].st_name
2541 oc->symbols[j] = NULL;
2550 /* Do ELF relocations which lack an explicit addend. All x86-linux
2551 relocations appear to be of this form. */
2553 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2554 Elf_Shdr* shdr, int shnum,
2555 Elf_Sym* stab, char* strtab )
2560 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2561 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2562 int target_shndx = shdr[shnum].sh_info;
2563 int symtab_shndx = shdr[shnum].sh_link;
2565 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2566 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2567 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2568 target_shndx, symtab_shndx ));
2570 for (j = 0; j < nent; j++) {
2571 Elf_Addr offset = rtab[j].r_offset;
2572 Elf_Addr info = rtab[j].r_info;
2574 Elf_Addr P = ((Elf_Addr)targ) + offset;
2575 Elf_Word* pP = (Elf_Word*)P;
2580 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2581 j, (void*)offset, (void*)info ));
2583 IF_DEBUG(linker,belch( " ZERO" ));
2586 Elf_Sym sym = stab[ELF_R_SYM(info)];
2587 /* First see if it is a local symbol. */
2588 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2589 /* Yes, so we can get the address directly from the ELF symbol
2591 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2593 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2594 + stab[ELF_R_SYM(info)].st_value);
2597 /* No, so look up the name in our global table. */
2598 symbol = strtab + sym.st_name;
2599 (void*)S = lookupSymbol( symbol );
2602 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2605 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2608 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2609 (void*)P, (void*)S, (void*)A ));
2610 checkProddableBlock ( oc, pP );
2614 switch (ELF_R_TYPE(info)) {
2615 # ifdef i386_TARGET_ARCH
2616 case R_386_32: *pP = value; break;
2617 case R_386_PC32: *pP = value - P; break;
2620 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2621 oc->fileName, ELF_R_TYPE(info));
2629 /* Do ELF relocations for which explicit addends are supplied.
2630 sparc-solaris relocations appear to be of this form. */
2632 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2633 Elf_Shdr* shdr, int shnum,
2634 Elf_Sym* stab, char* strtab )
2639 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2640 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2641 int target_shndx = shdr[shnum].sh_info;
2642 int symtab_shndx = shdr[shnum].sh_link;
2644 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2645 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2646 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2647 target_shndx, symtab_shndx ));
2649 for (j = 0; j < nent; j++) {
2650 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
2651 /* This #ifdef only serves to avoid unused-var warnings. */
2652 Elf_Addr offset = rtab[j].r_offset;
2653 Elf_Addr P = targ + offset;
2655 Elf_Addr info = rtab[j].r_info;
2656 Elf_Addr A = rtab[j].r_addend;
2659 # if defined(sparc_TARGET_ARCH)
2660 Elf_Word* pP = (Elf_Word*)P;
2662 # elif defined(ia64_TARGET_ARCH)
2663 Elf64_Xword *pP = (Elf64_Xword *)P;
2667 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2668 j, (void*)offset, (void*)info,
2671 IF_DEBUG(linker,belch( " ZERO" ));
2674 Elf_Sym sym = stab[ELF_R_SYM(info)];
2675 /* First see if it is a local symbol. */
2676 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2677 /* Yes, so we can get the address directly from the ELF symbol
2679 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2681 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2682 + stab[ELF_R_SYM(info)].st_value);
2683 #ifdef ELF_FUNCTION_DESC
2684 /* Make a function descriptor for this function */
2685 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2686 S = allocateFunctionDesc(S + A);
2691 /* No, so look up the name in our global table. */
2692 symbol = strtab + sym.st_name;
2693 (void*)S = lookupSymbol( symbol );
2695 #ifdef ELF_FUNCTION_DESC
2696 /* If a function, already a function descriptor - we would
2697 have to copy it to add an offset. */
2698 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC)
2703 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2706 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2709 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2710 (void*)P, (void*)S, (void*)A ));
2711 /* checkProddableBlock ( oc, (void*)P ); */
2715 switch (ELF_R_TYPE(info)) {
2716 # if defined(sparc_TARGET_ARCH)
2717 case R_SPARC_WDISP30:
2718 w1 = *pP & 0xC0000000;
2719 w2 = (Elf_Word)((value - P) >> 2);
2720 ASSERT((w2 & 0xC0000000) == 0);
2725 w1 = *pP & 0xFFC00000;
2726 w2 = (Elf_Word)(value >> 10);
2727 ASSERT((w2 & 0xFFC00000) == 0);
2733 w2 = (Elf_Word)(value & 0x3FF);
2734 ASSERT((w2 & ~0x3FF) == 0);
2738 /* According to the Sun documentation:
2740 This relocation type resembles R_SPARC_32, except it refers to an
2741 unaligned word. That is, the word to be relocated must be treated
2742 as four separate bytes with arbitrary alignment, not as a word
2743 aligned according to the architecture requirements.
2745 (JRS: which means that freeloading on the R_SPARC_32 case
2746 is probably wrong, but hey ...)
2750 w2 = (Elf_Word)value;
2753 # elif defined(ia64_TARGET_ARCH)
2754 case R_IA64_DIR64LSB:
2755 case R_IA64_FPTR64LSB:
2758 case R_IA64_SEGREL64LSB:
2759 addr = findElfSegment(ehdrC, value);
2762 case R_IA64_GPREL22:
2763 ia64_reloc_gprel22(P, value);
2765 case R_IA64_LTOFF22:
2766 case R_IA64_LTOFF_FPTR22:
2767 addr = allocateGOTEntry(value);
2768 ia64_reloc_gprel22(P, addr);
2770 case R_IA64_PCREL21B:
2771 ia64_reloc_pcrel21(P, S, oc);
2775 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2776 oc->fileName, ELF_R_TYPE(info));
2785 ocResolve_ELF ( ObjectCode* oc )
2789 Elf_Sym* stab = NULL;
2790 char* ehdrC = (char*)(oc->image);
2791 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2792 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2793 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2795 /* first find "the" symbol table */
2796 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2798 /* also go find the string table */
2799 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2801 if (stab == NULL || strtab == NULL) {
2802 belch("%s: can't find string or symbol table", oc->fileName);
2806 /* Process the relocation sections. */
2807 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2809 /* Skip sections called ".rel.stab". These appear to contain
2810 relocation entries that, when done, make the stabs debugging
2811 info point at the right places. We ain't interested in all
2813 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2816 if (shdr[shnum].sh_type == SHT_REL ) {
2817 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2818 shnum, stab, strtab );
2822 if (shdr[shnum].sh_type == SHT_RELA) {
2823 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2824 shnum, stab, strtab );
2829 /* Free the local symbol table; we won't need it again. */
2830 freeHashTable(oc->lochash, NULL);
2838 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2839 * at the front. The following utility functions pack and unpack instructions, and
2840 * take care of the most common relocations.
2843 #ifdef ia64_TARGET_ARCH
2846 ia64_extract_instruction(Elf64_Xword *target)
2849 int slot = (Elf_Addr)target & 3;
2850 (Elf_Addr)target &= ~3;
2858 return ((w1 >> 5) & 0x1ffffffffff);
2860 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2864 barf("ia64_extract_instruction: invalid slot %p", target);
2869 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2871 int slot = (Elf_Addr)target & 3;
2872 (Elf_Addr)target &= ~3;
2877 *target |= value << 5;
2880 *target |= value << 46;
2881 *(target+1) |= value >> 18;
2884 *(target+1) |= value << 23;
2890 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2892 Elf64_Xword instruction;
2893 Elf64_Sxword rel_value;
2895 rel_value = value - gp_val;
2896 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2897 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2899 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2900 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2901 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2902 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2903 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2904 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2908 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2910 Elf64_Xword instruction;
2911 Elf64_Sxword rel_value;
2914 entry = allocatePLTEntry(value, oc);
2916 rel_value = (entry >> 4) - (target >> 4);
2917 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2918 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
2920 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2921 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
2922 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2923 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2930 /* --------------------------------------------------------------------------
2932 * ------------------------------------------------------------------------*/
2934 #if defined(OBJFORMAT_MACHO)
2937 Initial support for MachO linking on Darwin/MacOS X on PowerPC chips
2938 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
2940 I hereby formally apologize for the hackish nature of this code.
2941 Things that need to be done:
2942 *) get common symbols and .bss sections to work properly.
2943 Haskell modules seem to work, but C modules can cause problems
2944 *) implement ocVerifyImage_MachO
2945 *) add more sanity checks. The current code just has to segfault if there's a
2949 static int ocVerifyImage_MachO(ObjectCode* oc)
2951 // FIXME: do some verifying here
2955 static int resolveImports(
2958 struct symtab_command *symLC,
2959 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
2960 unsigned long *indirectSyms,
2961 struct nlist *nlist)
2965 for(i=0;i*4<sect->size;i++)
2967 // according to otool, reserved1 contains the first index into the indirect symbol table
2968 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
2969 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
2972 if((symbol->n_type & N_TYPE) == N_UNDF
2973 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
2974 addr = (void*) (symbol->n_value);
2975 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
2978 addr = lookupSymbol(nm);
2981 belch("\n%s: unknown symbol `%s'", oc->fileName, nm);
2985 ((void**)(image + sect->offset))[i] = addr;
2991 static int relocateSection(char *image,
2992 struct symtab_command *symLC, struct nlist *nlist,
2993 struct section* sections, struct section *sect)
2995 struct relocation_info *relocs;
2998 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3000 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3004 relocs = (struct relocation_info*) (image + sect->reloff);
3008 if(relocs[i].r_address & R_SCATTERED)
3010 struct scattered_relocation_info *scat =
3011 (struct scattered_relocation_info*) &relocs[i];
3015 if(scat->r_length == 2 && scat->r_type == GENERIC_RELOC_VANILLA)
3017 unsigned long* word = (unsigned long*) (image + sect->offset + scat->r_address);
3019 *word = scat->r_value + sect->offset + ((long) image);
3023 continue; // FIXME: I hope it's OK to ignore all the others.
3027 struct relocation_info *reloc = &relocs[i];
3028 if(reloc->r_pcrel && !reloc->r_extern)
3031 if(reloc->r_length == 2)
3033 unsigned long word = 0;
3035 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3037 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3041 else if(reloc->r_type == PPC_RELOC_LO16)
3043 word = ((unsigned short*) wordPtr)[1];
3044 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3046 else if(reloc->r_type == PPC_RELOC_HI16)
3048 word = ((unsigned short*) wordPtr)[1] << 16;
3049 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3051 else if(reloc->r_type == PPC_RELOC_HA16)
3053 word = ((unsigned short*) wordPtr)[1] << 16;
3054 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3056 else if(reloc->r_type == PPC_RELOC_BR24)
3059 word = (word & 0x03FFFFFC) | (word & 0x02000000) ? 0xFC000000 : 0;
3063 if(!reloc->r_extern)
3066 sections[reloc->r_symbolnum-1].offset
3067 - sections[reloc->r_symbolnum-1].addr
3074 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3075 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3076 word = (unsigned long) (lookupSymbol(nm));
3079 belch("\nunknown symbol `%s'", nm);
3084 word -= ((long)image) + sect->offset + reloc->r_address;
3087 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3092 else if(reloc->r_type == PPC_RELOC_LO16)
3094 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3097 else if(reloc->r_type == PPC_RELOC_HI16)
3099 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3102 else if(reloc->r_type == PPC_RELOC_HA16)
3104 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3105 + ((word & (1<<15)) ? 1 : 0);
3108 else if(reloc->r_type == PPC_RELOC_BR24)
3110 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3114 barf("\nunknown relocation %d",reloc->r_type);
3121 static int ocGetNames_MachO(ObjectCode* oc)
3123 char *image = (char*) oc->image;
3124 struct mach_header *header = (struct mach_header*) image;
3125 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3126 unsigned i,curSymbol;
3127 struct segment_command *segLC = NULL;
3128 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3129 struct symtab_command *symLC = NULL;
3130 struct dysymtab_command *dsymLC = NULL;
3131 struct nlist *nlist;
3132 unsigned long commonSize = 0;
3133 char *commonStorage = NULL;
3134 unsigned long commonCounter;
3136 for(i=0;i<header->ncmds;i++)
3138 if(lc->cmd == LC_SEGMENT)
3139 segLC = (struct segment_command*) lc;
3140 else if(lc->cmd == LC_SYMTAB)
3141 symLC = (struct symtab_command*) lc;
3142 else if(lc->cmd == LC_DYSYMTAB)
3143 dsymLC = (struct dysymtab_command*) lc;
3144 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3147 sections = (struct section*) (segLC+1);
3148 nlist = (struct nlist*) (image + symLC->symoff);
3150 for(i=0;i<segLC->nsects;i++)
3152 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3153 la_ptrs = §ions[i];
3154 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3155 nl_ptrs = §ions[i];
3157 // for now, only add __text and __const to the sections table
3158 else if(!strcmp(sections[i].sectname,"__text"))
3159 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3160 (void*) (image + sections[i].offset),
3161 (void*) (image + sections[i].offset + sections[i].size));
3162 else if(!strcmp(sections[i].sectname,"__const"))
3163 addSection(oc, SECTIONKIND_RWDATA,
3164 (void*) (image + sections[i].offset),
3165 (void*) (image + sections[i].offset + sections[i].size));
3166 else if(!strcmp(sections[i].sectname,"__data"))
3167 addSection(oc, SECTIONKIND_RWDATA,
3168 (void*) (image + sections[i].offset),
3169 (void*) (image + sections[i].offset + sections[i].size));
3172 // count external symbols defined here
3174 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3176 if((nlist[i].n_type & N_TYPE) == N_SECT)
3179 for(i=0;i<symLC->nsyms;i++)
3181 if((nlist[i].n_type & N_TYPE) == N_UNDF
3182 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3184 commonSize += nlist[i].n_value;
3188 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3189 "ocGetNames_MachO(oc->symbols)");
3191 // insert symbols into hash table
3192 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3194 if((nlist[i].n_type & N_TYPE) == N_SECT)
3196 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3197 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3198 sections[nlist[i].n_sect-1].offset
3199 - sections[nlist[i].n_sect-1].addr
3200 + nlist[i].n_value);
3201 oc->symbols[curSymbol++] = nm;
3205 // insert local symbols into lochash
3206 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3208 if((nlist[i].n_type & N_TYPE) == N_SECT)
3210 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3211 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3212 sections[nlist[i].n_sect-1].offset
3213 - sections[nlist[i].n_sect-1].addr
3214 + nlist[i].n_value);
3219 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3220 commonCounter = (unsigned long)commonStorage;
3221 for(i=0;i<symLC->nsyms;i++)
3223 if((nlist[i].n_type & N_TYPE) == N_UNDF
3224 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3226 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3227 unsigned long sz = nlist[i].n_value;
3229 nlist[i].n_value = commonCounter;
3231 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3232 oc->symbols[curSymbol++] = nm;
3234 commonCounter += sz;
3240 static int ocResolve_MachO(ObjectCode* oc)
3242 char *image = (char*) oc->image;
3243 struct mach_header *header = (struct mach_header*) image;
3244 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3246 struct segment_command *segLC = NULL;
3247 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3248 struct symtab_command *symLC = NULL;
3249 struct dysymtab_command *dsymLC = NULL;
3250 struct nlist *nlist;
3251 unsigned long *indirectSyms;
3253 for(i=0;i<header->ncmds;i++)
3255 if(lc->cmd == LC_SEGMENT)
3256 segLC = (struct segment_command*) lc;
3257 else if(lc->cmd == LC_SYMTAB)
3258 symLC = (struct symtab_command*) lc;
3259 else if(lc->cmd == LC_DYSYMTAB)
3260 dsymLC = (struct dysymtab_command*) lc;
3261 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3264 sections = (struct section*) (segLC+1);
3265 nlist = (struct nlist*) (image + symLC->symoff);
3267 for(i=0;i<segLC->nsects;i++)
3269 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3270 la_ptrs = §ions[i];
3271 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3272 nl_ptrs = §ions[i];
3275 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3278 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
3281 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
3284 for(i=0;i<segLC->nsects;i++)
3286 if(!relocateSection(image,symLC,nlist,sections,§ions[i]))
3290 /* Free the local symbol table; we won't need it again. */
3291 freeHashTable(oc->lochash, NULL);
3297 * The Mach-O object format uses leading underscores. But not everywhere.
3298 * There is a small number of runtime support functions defined in
3299 * libcc_dynamic.a whose name does not have a leading underscore.
3300 * As a consequence, we can't get their address from C code.
3301 * We have to use inline assembler just to take the address of a function.
3305 static void machoInitSymbolsWithoutUnderscore()
3311 __asm__ ("lis %0,hi16(" #x ")\n\tori %0,%0,lo16(" #x ")" : "=r" (p)); \
3312 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, p);
3314 RTS_MACHO_NOUNDERLINE_SYMBOLS