1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.133 2003/09/26 09:17:28 simonmar Exp $
4 * (c) The GHC Team, 2000-2003
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) || defined(netbsd_TARGET_OS) || defined(openbsd_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>
74 # include <mach-o/dyld.h>
77 /* Hash table mapping symbol names to Symbol */
78 static /*Str*/HashTable *symhash;
80 /* List of currently loaded objects */
81 ObjectCode *objects = NULL; /* initially empty */
83 #if defined(OBJFORMAT_ELF)
84 static int ocVerifyImage_ELF ( ObjectCode* oc );
85 static int ocGetNames_ELF ( ObjectCode* oc );
86 static int ocResolve_ELF ( ObjectCode* oc );
87 #elif defined(OBJFORMAT_PEi386)
88 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
89 static int ocGetNames_PEi386 ( ObjectCode* oc );
90 static int ocResolve_PEi386 ( ObjectCode* oc );
91 #elif defined(OBJFORMAT_MACHO)
92 static int ocVerifyImage_MachO ( ObjectCode* oc );
93 static int ocGetNames_MachO ( ObjectCode* oc );
94 static int ocResolve_MachO ( ObjectCode* oc );
96 static void machoInitSymbolsWithoutUnderscore( void );
99 /* -----------------------------------------------------------------------------
100 * Built-in symbols from the RTS
103 typedef struct _RtsSymbolVal {
110 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
112 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
113 SymX(makeStableNamezh_fast) \
114 SymX(finalizzeWeakzh_fast)
116 /* These are not available in GUM!!! -- HWL */
117 #define Maybe_ForeignObj
118 #define Maybe_Stable_Names
121 #if !defined (mingw32_TARGET_OS)
122 #define RTS_POSIX_ONLY_SYMBOLS \
123 SymX(stg_sig_install) \
127 #if defined (cygwin32_TARGET_OS)
128 #define RTS_MINGW_ONLY_SYMBOLS /**/
129 /* Don't have the ability to read import libs / archives, so
130 * we have to stupidly list a lot of what libcygwin.a
133 #define RTS_CYGWIN_ONLY_SYMBOLS \
211 #elif !defined(mingw32_TARGET_OS)
212 #define RTS_MINGW_ONLY_SYMBOLS /**/
213 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
214 #else /* defined(mingw32_TARGET_OS) */
215 #define RTS_POSIX_ONLY_SYMBOLS /**/
216 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
218 /* Extra syms gen'ed by mingw-2's gcc-3.2: */
220 #define RTS_MINGW_EXTRA_SYMS \
221 Sym(_imp____mb_cur_max) \
224 #define RTS_MINGW_EXTRA_SYMS
227 /* These are statically linked from the mingw libraries into the ghc
228 executable, so we have to employ this hack. */
229 #define RTS_MINGW_ONLY_SYMBOLS \
230 SymX(asyncReadzh_fast) \
231 SymX(asyncWritezh_fast) \
243 SymX(getservbyname) \
244 SymX(getservbyport) \
245 SymX(getprotobynumber) \
246 SymX(getprotobyname) \
247 SymX(gethostbyname) \
248 SymX(gethostbyaddr) \
283 Sym(_imp___timezone) \
291 RTS_MINGW_EXTRA_SYMS \
296 # define MAIN_CAP_SYM SymX(MainCapability)
298 # define MAIN_CAP_SYM
301 #define RTS_SYMBOLS \
305 SymX(stg_enter_info) \
306 SymX(stg_enter_ret) \
307 SymX(stg_gc_void_info) \
308 SymX(__stg_gc_enter_1) \
309 SymX(stg_gc_noregs) \
310 SymX(stg_gc_unpt_r1_info) \
311 SymX(stg_gc_unpt_r1) \
312 SymX(stg_gc_unbx_r1_info) \
313 SymX(stg_gc_unbx_r1) \
314 SymX(stg_gc_f1_info) \
316 SymX(stg_gc_d1_info) \
318 SymX(stg_gc_l1_info) \
321 SymX(stg_gc_fun_info) \
322 SymX(stg_gc_fun_ret) \
324 SymX(stg_gc_gen_info) \
325 SymX(stg_gc_gen_hp) \
327 SymX(stg_gen_yield) \
328 SymX(stg_yield_noregs) \
329 SymX(stg_yield_to_interpreter) \
330 SymX(stg_gen_block) \
331 SymX(stg_block_noregs) \
333 SymX(stg_block_takemvar) \
334 SymX(stg_block_putmvar) \
335 SymX(stg_seq_frame_info) \
338 SymX(MallocFailHook) \
340 SymX(OutOfHeapHook) \
341 SymX(PatErrorHdrHook) \
342 SymX(PostTraceHook) \
344 SymX(StackOverflowHook) \
345 SymX(__encodeDouble) \
346 SymX(__encodeFloat) \
349 SymX(__gmpz_cmp_si) \
350 SymX(__gmpz_cmp_ui) \
351 SymX(__gmpz_get_si) \
352 SymX(__gmpz_get_ui) \
353 SymX(__int_encodeDouble) \
354 SymX(__int_encodeFloat) \
355 SymX(andIntegerzh_fast) \
356 SymX(blockAsyncExceptionszh_fast) \
359 SymX(complementIntegerzh_fast) \
360 SymX(cmpIntegerzh_fast) \
361 SymX(cmpIntegerIntzh_fast) \
362 SymX(createAdjustor) \
363 SymX(decodeDoublezh_fast) \
364 SymX(decodeFloatzh_fast) \
367 SymX(deRefWeakzh_fast) \
368 SymX(deRefStablePtrzh_fast) \
369 SymX(divExactIntegerzh_fast) \
370 SymX(divModIntegerzh_fast) \
372 SymX(forkProcesszh_fast) \
373 SymX(forkOS_createThread) \
374 SymX(freeHaskellFunctionPtr) \
375 SymX(freeStablePtr) \
376 SymX(gcdIntegerzh_fast) \
377 SymX(gcdIntegerIntzh_fast) \
378 SymX(gcdIntzh_fast) \
382 SymX(int2Integerzh_fast) \
383 SymX(integer2Intzh_fast) \
384 SymX(integer2Wordzh_fast) \
385 SymX(isCurrentThreadBoundzh_fast) \
386 SymX(isDoubleDenormalized) \
387 SymX(isDoubleInfinite) \
389 SymX(isDoubleNegativeZero) \
390 SymX(isEmptyMVarzh_fast) \
391 SymX(isFloatDenormalized) \
392 SymX(isFloatInfinite) \
394 SymX(isFloatNegativeZero) \
395 SymX(killThreadzh_fast) \
396 SymX(makeStablePtrzh_fast) \
397 SymX(minusIntegerzh_fast) \
398 SymX(mkApUpd0zh_fast) \
399 SymX(myThreadIdzh_fast) \
400 SymX(labelThreadzh_fast) \
401 SymX(newArrayzh_fast) \
402 SymX(newBCOzh_fast) \
403 SymX(newByteArrayzh_fast) \
404 SymX_redirect(newCAF, newDynCAF) \
405 SymX(newMVarzh_fast) \
406 SymX(newMutVarzh_fast) \
407 SymX(atomicModifyMutVarzh_fast) \
408 SymX(newPinnedByteArrayzh_fast) \
409 SymX(orIntegerzh_fast) \
411 SymX(plusIntegerzh_fast) \
414 SymX(putMVarzh_fast) \
415 SymX(quotIntegerzh_fast) \
416 SymX(quotRemIntegerzh_fast) \
418 SymX(raiseIOzh_fast) \
419 SymX(remIntegerzh_fast) \
420 SymX(resetNonBlockingFd) \
423 SymX(rts_checkSchedStatus) \
426 SymX(rts_evalLazyIO) \
427 SymX(rts_evalStableIO) \
431 SymX(rts_getDouble) \
436 SymX(rts_getFunPtr) \
437 SymX(rts_getStablePtr) \
438 SymX(rts_getThreadId) \
440 SymX(rts_getWord32) \
453 SymX(rts_mkStablePtr) \
461 SymX(rtsSupportsBoundThreads) \
463 Sym(__hscore_get_saved_termios) \
464 Sym(__hscore_set_saved_termios) \
466 SymX(startupHaskell) \
467 SymX(shutdownHaskell) \
468 SymX(shutdownHaskellAndExit) \
469 SymX(stable_ptr_table) \
470 SymX(stackOverflow) \
471 SymX(stg_CAF_BLACKHOLE_info) \
472 SymX(stg_BLACKHOLE_BQ_info) \
473 SymX(awakenBlockedQueue) \
474 SymX(stg_CHARLIKE_closure) \
475 SymX(stg_EMPTY_MVAR_info) \
476 SymX(stg_IND_STATIC_info) \
477 SymX(stg_INTLIKE_closure) \
478 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
479 SymX(stg_WEAK_info) \
480 SymX(stg_ap_v_info) \
481 SymX(stg_ap_f_info) \
482 SymX(stg_ap_d_info) \
483 SymX(stg_ap_l_info) \
484 SymX(stg_ap_n_info) \
485 SymX(stg_ap_p_info) \
486 SymX(stg_ap_pv_info) \
487 SymX(stg_ap_pp_info) \
488 SymX(stg_ap_ppv_info) \
489 SymX(stg_ap_ppp_info) \
490 SymX(stg_ap_pppp_info) \
491 SymX(stg_ap_ppppp_info) \
492 SymX(stg_ap_pppppp_info) \
493 SymX(stg_ap_ppppppp_info) \
501 SymX(stg_ap_pv_ret) \
502 SymX(stg_ap_pp_ret) \
503 SymX(stg_ap_ppv_ret) \
504 SymX(stg_ap_ppp_ret) \
505 SymX(stg_ap_pppp_ret) \
506 SymX(stg_ap_ppppp_ret) \
507 SymX(stg_ap_pppppp_ret) \
508 SymX(stg_ap_ppppppp_ret) \
509 SymX(stg_ap_1_upd_info) \
510 SymX(stg_ap_2_upd_info) \
511 SymX(stg_ap_3_upd_info) \
512 SymX(stg_ap_4_upd_info) \
513 SymX(stg_ap_5_upd_info) \
514 SymX(stg_ap_6_upd_info) \
515 SymX(stg_ap_7_upd_info) \
516 SymX(stg_ap_8_upd_info) \
518 SymX(stg_sel_0_upd_info) \
519 SymX(stg_sel_10_upd_info) \
520 SymX(stg_sel_11_upd_info) \
521 SymX(stg_sel_12_upd_info) \
522 SymX(stg_sel_13_upd_info) \
523 SymX(stg_sel_14_upd_info) \
524 SymX(stg_sel_15_upd_info) \
525 SymX(stg_sel_1_upd_info) \
526 SymX(stg_sel_2_upd_info) \
527 SymX(stg_sel_3_upd_info) \
528 SymX(stg_sel_4_upd_info) \
529 SymX(stg_sel_5_upd_info) \
530 SymX(stg_sel_6_upd_info) \
531 SymX(stg_sel_7_upd_info) \
532 SymX(stg_sel_8_upd_info) \
533 SymX(stg_sel_9_upd_info) \
534 SymX(stg_upd_frame_info) \
535 SymX(suspendThread) \
536 SymX(takeMVarzh_fast) \
537 SymX(timesIntegerzh_fast) \
538 SymX(tryPutMVarzh_fast) \
539 SymX(tryTakeMVarzh_fast) \
540 SymX(unblockAsyncExceptionszh_fast) \
541 SymX(unsafeThawArrayzh_fast) \
542 SymX(waitReadzh_fast) \
543 SymX(waitWritezh_fast) \
544 SymX(word2Integerzh_fast) \
545 SymX(xorIntegerzh_fast) \
548 #ifdef SUPPORT_LONG_LONGS
549 #define RTS_LONG_LONG_SYMS \
550 SymX(int64ToIntegerzh_fast) \
551 SymX(word64ToIntegerzh_fast)
553 #define RTS_LONG_LONG_SYMS /* nothing */
556 // 64-bit support functions in libgcc.a
557 #if defined(__GNUC__) && SIZEOF_VOID_P <= 4
558 #define RTS_LIBGCC_SYMBOLS \
567 #elif defined(ia64_TARGET_ARCH)
568 #define RTS_LIBGCC_SYMBOLS \
576 #define RTS_LIBGCC_SYMBOLS
579 #ifdef darwin_TARGET_OS
580 // Symbols that don't have a leading underscore
581 // on Mac OS X. They have to receive special treatment,
582 // see machoInitSymbolsWithoutUnderscore()
583 #define RTS_MACHO_NOUNDERLINE_SYMBOLS \
588 /* entirely bogus claims about types of these symbols */
589 #define Sym(vvv) extern void vvv(void);
590 #define SymX(vvv) /**/
591 #define SymX_redirect(vvv,xxx) /**/
594 RTS_POSIX_ONLY_SYMBOLS
595 RTS_MINGW_ONLY_SYMBOLS
596 RTS_CYGWIN_ONLY_SYMBOLS
602 #ifdef LEADING_UNDERSCORE
603 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
605 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
608 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
610 #define SymX(vvv) Sym(vvv)
612 // SymX_redirect allows us to redirect references to one symbol to
613 // another symbol. See newCAF/newDynCAF for an example.
614 #define SymX_redirect(vvv,xxx) \
615 { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
618 static RtsSymbolVal rtsSyms[] = {
621 RTS_POSIX_ONLY_SYMBOLS
622 RTS_MINGW_ONLY_SYMBOLS
623 RTS_CYGWIN_ONLY_SYMBOLS
625 { 0, 0 } /* sentinel */
628 /* -----------------------------------------------------------------------------
629 * Insert symbols into hash tables, checking for duplicates.
631 static void ghciInsertStrHashTable ( char* obj_name,
637 if (lookupHashTable(table, (StgWord)key) == NULL)
639 insertStrHashTable(table, (StgWord)key, data);
644 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
646 "whilst processing object file\n"
648 "This could be caused by:\n"
649 " * Loading two different object files which export the same symbol\n"
650 " * Specifying the same object file twice on the GHCi command line\n"
651 " * An incorrect `package.conf' entry, causing some object to be\n"
653 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
662 /* -----------------------------------------------------------------------------
663 * initialize the object linker
667 static int linker_init_done = 0 ;
669 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
670 static void *dl_prog_handle;
678 /* Make initLinker idempotent, so we can call it
679 before evey relevant operation; that means we
680 don't need to initialise the linker separately */
681 if (linker_init_done == 1) { return; } else {
682 linker_init_done = 1;
685 symhash = allocStrHashTable();
687 /* populate the symbol table with stuff from the RTS */
688 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
689 ghciInsertStrHashTable("(GHCi built-in symbols)",
690 symhash, sym->lbl, sym->addr);
692 # if defined(OBJFORMAT_MACHO)
693 machoInitSymbolsWithoutUnderscore();
696 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
697 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
701 /* -----------------------------------------------------------------------------
702 * Loading DLL or .so dynamic libraries
703 * -----------------------------------------------------------------------------
705 * Add a DLL from which symbols may be found. In the ELF case, just
706 * do RTLD_GLOBAL-style add, so no further messing around needs to
707 * happen in order that symbols in the loaded .so are findable --
708 * lookupSymbol() will subsequently see them by dlsym on the program's
709 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
711 * In the PEi386 case, open the DLLs and put handles to them in a
712 * linked list. When looking for a symbol, try all handles in the
713 * list. This means that we need to load even DLLs that are guaranteed
714 * to be in the ghc.exe image already, just so we can get a handle
715 * to give to loadSymbol, so that we can find the symbols. For such
716 * libraries, the LoadLibrary call should be a no-op except for returning
721 #if defined(OBJFORMAT_PEi386)
722 /* A record for storing handles into DLLs. */
727 struct _OpenedDLL* next;
732 /* A list thereof. */
733 static OpenedDLL* opened_dlls = NULL;
737 addDLL( char *dll_name )
739 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
740 /* ------------------- ELF DLL loader ------------------- */
746 #if !defined(openbsd_TARGET_OS)
747 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
749 hdl= dlopen(dll_name, RTLD_LAZY);
752 /* dlopen failed; return a ptr to the error msg. */
754 if (errmsg == NULL) errmsg = "addDLL: unknown error";
761 # elif defined(OBJFORMAT_PEi386)
762 /* ------------------- Win32 DLL loader ------------------- */
770 /* fprintf(stderr, "\naddDLL; dll_name = `%s'\n", dll_name); */
772 /* See if we've already got it, and ignore if so. */
773 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
774 if (0 == strcmp(o_dll->name, dll_name))
778 /* The file name has no suffix (yet) so that we can try
779 both foo.dll and foo.drv
781 The documentation for LoadLibrary says:
782 If no file name extension is specified in the lpFileName
783 parameter, the default library extension .dll is
784 appended. However, the file name string can include a trailing
785 point character (.) to indicate that the module name has no
788 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
789 sprintf(buf, "%s.DLL", dll_name);
790 instance = LoadLibrary(buf);
791 if (instance == NULL) {
792 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
793 instance = LoadLibrary(buf);
794 if (instance == NULL) {
797 /* LoadLibrary failed; return a ptr to the error msg. */
798 return "addDLL: unknown error";
803 /* Add this DLL to the list of DLLs in which to search for symbols. */
804 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
805 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
806 strcpy(o_dll->name, dll_name);
807 o_dll->instance = instance;
808 o_dll->next = opened_dlls;
813 barf("addDLL: not implemented on this platform");
817 /* -----------------------------------------------------------------------------
818 * lookup a symbol in the hash table
821 lookupSymbol( char *lbl )
825 ASSERT(symhash != NULL);
826 val = lookupStrHashTable(symhash, lbl);
829 # if defined(OBJFORMAT_ELF)
830 return dlsym(dl_prog_handle, lbl);
831 # elif defined(OBJFORMAT_MACHO)
832 if(NSIsSymbolNameDefined(lbl)) {
833 NSSymbol symbol = NSLookupAndBindSymbol(lbl);
834 return NSAddressOfSymbol(symbol);
838 # elif defined(OBJFORMAT_PEi386)
841 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
842 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
844 /* HACK: if the name has an initial underscore, try stripping
845 it off & look that up first. I've yet to verify whether there's
846 a Rule that governs whether an initial '_' *should always* be
847 stripped off when mapping from import lib name to the DLL name.
849 sym = GetProcAddress(o_dll->instance, (lbl+1));
851 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
855 sym = GetProcAddress(o_dll->instance, lbl);
857 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
872 __attribute((unused))
874 lookupLocalSymbol( ObjectCode* oc, char *lbl )
878 val = lookupStrHashTable(oc->lochash, lbl);
888 /* -----------------------------------------------------------------------------
889 * Debugging aid: look in GHCi's object symbol tables for symbols
890 * within DELTA bytes of the specified address, and show their names.
893 void ghci_enquire ( char* addr );
895 void ghci_enquire ( char* addr )
900 const int DELTA = 64;
905 for (oc = objects; oc; oc = oc->next) {
906 for (i = 0; i < oc->n_symbols; i++) {
907 sym = oc->symbols[i];
908 if (sym == NULL) continue;
909 // fprintf(stderr, "enquire %p %p\n", sym, oc->lochash);
911 if (oc->lochash != NULL) {
912 a = lookupStrHashTable(oc->lochash, sym);
915 a = lookupStrHashTable(symhash, sym);
918 // fprintf(stderr, "ghci_enquire: can't find %s\n", sym);
920 else if (addr-DELTA <= a && a <= addr+DELTA) {
921 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
928 #ifdef ia64_TARGET_ARCH
929 static unsigned int PLTSize(void);
932 /* -----------------------------------------------------------------------------
933 * Load an obj (populate the global symbol table, but don't resolve yet)
935 * Returns: 1 if ok, 0 on error.
938 loadObj( char *path )
952 /* fprintf(stderr, "loadObj %s\n", path ); */
954 /* Check that we haven't already loaded this object. Don't give up
955 at this stage; ocGetNames_* will barf later. */
959 for (o = objects; o; o = o->next) {
960 if (0 == strcmp(o->fileName, path))
966 "GHCi runtime linker: warning: looks like you're trying to load the\n"
967 "same object file twice:\n"
969 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
975 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
977 # if defined(OBJFORMAT_ELF)
978 oc->formatName = "ELF";
979 # elif defined(OBJFORMAT_PEi386)
980 oc->formatName = "PEi386";
981 # elif defined(OBJFORMAT_MACHO)
982 oc->formatName = "Mach-O";
985 barf("loadObj: not implemented on this platform");
989 if (r == -1) { return 0; }
991 /* sigh, strdup() isn't a POSIX function, so do it the long way */
992 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
993 strcpy(oc->fileName, path);
995 oc->fileSize = st.st_size;
998 oc->lochash = allocStrHashTable();
999 oc->proddables = NULL;
1001 /* chain it onto the list of objects */
1006 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
1008 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
1010 fd = open(path, O_RDONLY);
1012 barf("loadObj: can't open `%s'", path);
1014 pagesize = getpagesize();
1016 #ifdef ia64_TARGET_ARCH
1017 /* The PLT needs to be right before the object */
1018 n = ROUND_UP(PLTSize(), pagesize);
1019 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
1020 if (oc->plt == MAP_FAILED)
1021 barf("loadObj: can't allocate PLT");
1024 map_addr = oc->plt + n;
1027 n = ROUND_UP(oc->fileSize, pagesize);
1028 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
1029 if (oc->image == MAP_FAILED)
1030 barf("loadObj: can't map `%s'", path);
1034 #else /* !USE_MMAP */
1036 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
1038 /* load the image into memory */
1039 f = fopen(path, "rb");
1041 barf("loadObj: can't read `%s'", path);
1043 n = fread ( oc->image, 1, oc->fileSize, f );
1044 if (n != oc->fileSize)
1045 barf("loadObj: error whilst reading `%s'", path);
1049 #endif /* USE_MMAP */
1051 /* verify the in-memory image */
1052 # if defined(OBJFORMAT_ELF)
1053 r = ocVerifyImage_ELF ( oc );
1054 # elif defined(OBJFORMAT_PEi386)
1055 r = ocVerifyImage_PEi386 ( oc );
1056 # elif defined(OBJFORMAT_MACHO)
1057 r = ocVerifyImage_MachO ( oc );
1059 barf("loadObj: no verify method");
1061 if (!r) { return r; }
1063 /* build the symbol list for this image */
1064 # if defined(OBJFORMAT_ELF)
1065 r = ocGetNames_ELF ( oc );
1066 # elif defined(OBJFORMAT_PEi386)
1067 r = ocGetNames_PEi386 ( oc );
1068 # elif defined(OBJFORMAT_MACHO)
1069 r = ocGetNames_MachO ( oc );
1071 barf("loadObj: no getNames method");
1073 if (!r) { return r; }
1075 /* loaded, but not resolved yet */
1076 oc->status = OBJECT_LOADED;
1081 /* -----------------------------------------------------------------------------
1082 * resolve all the currently unlinked objects in memory
1084 * Returns: 1 if ok, 0 on error.
1094 for (oc = objects; oc; oc = oc->next) {
1095 if (oc->status != OBJECT_RESOLVED) {
1096 # if defined(OBJFORMAT_ELF)
1097 r = ocResolve_ELF ( oc );
1098 # elif defined(OBJFORMAT_PEi386)
1099 r = ocResolve_PEi386 ( oc );
1100 # elif defined(OBJFORMAT_MACHO)
1101 r = ocResolve_MachO ( oc );
1103 barf("resolveObjs: not implemented on this platform");
1105 if (!r) { return r; }
1106 oc->status = OBJECT_RESOLVED;
1112 /* -----------------------------------------------------------------------------
1113 * delete an object from the pool
1116 unloadObj( char *path )
1118 ObjectCode *oc, *prev;
1120 ASSERT(symhash != NULL);
1121 ASSERT(objects != NULL);
1126 for (oc = objects; oc; prev = oc, oc = oc->next) {
1127 if (!strcmp(oc->fileName,path)) {
1129 /* Remove all the mappings for the symbols within this
1134 for (i = 0; i < oc->n_symbols; i++) {
1135 if (oc->symbols[i] != NULL) {
1136 removeStrHashTable(symhash, oc->symbols[i], NULL);
1144 prev->next = oc->next;
1147 /* We're going to leave this in place, in case there are
1148 any pointers from the heap into it: */
1149 /* stgFree(oc->image); */
1150 stgFree(oc->fileName);
1151 stgFree(oc->symbols);
1152 stgFree(oc->sections);
1153 /* The local hash table should have been freed at the end
1154 of the ocResolve_ call on it. */
1155 ASSERT(oc->lochash == NULL);
1161 belch("unloadObj: can't find `%s' to unload", path);
1165 /* -----------------------------------------------------------------------------
1166 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1167 * which may be prodded during relocation, and abort if we try and write
1168 * outside any of these.
1170 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1173 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1174 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1178 pb->next = oc->proddables;
1179 oc->proddables = pb;
1182 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1185 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1186 char* s = (char*)(pb->start);
1187 char* e = s + pb->size - 1;
1188 char* a = (char*)addr;
1189 /* Assumes that the biggest fixup involves a 4-byte write. This
1190 probably needs to be changed to 8 (ie, +7) on 64-bit
1192 if (a >= s && (a+3) <= e) return;
1194 barf("checkProddableBlock: invalid fixup in runtime linker");
1197 /* -----------------------------------------------------------------------------
1198 * Section management.
1200 static void addSection ( ObjectCode* oc, SectionKind kind,
1201 void* start, void* end )
1203 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1207 s->next = oc->sections;
1210 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1211 start, ((char*)end)-1, end - start + 1, kind );
1217 /* --------------------------------------------------------------------------
1218 * PEi386 specifics (Win32 targets)
1219 * ------------------------------------------------------------------------*/
1221 /* The information for this linker comes from
1222 Microsoft Portable Executable
1223 and Common Object File Format Specification
1224 revision 5.1 January 1998
1225 which SimonM says comes from the MS Developer Network CDs.
1227 It can be found there (on older CDs), but can also be found
1230 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1232 (this is Rev 6.0 from February 1999).
1234 Things move, so if that fails, try searching for it via
1236 http://www.google.com/search?q=PE+COFF+specification
1238 The ultimate reference for the PE format is the Winnt.h
1239 header file that comes with the Platform SDKs; as always,
1240 implementations will drift wrt their documentation.
1242 A good background article on the PE format is Matt Pietrek's
1243 March 1994 article in Microsoft System Journal (MSJ)
1244 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1245 Win32 Portable Executable File Format." The info in there
1246 has recently been updated in a two part article in
1247 MSDN magazine, issues Feb and March 2002,
1248 "Inside Windows: An In-Depth Look into the Win32 Portable
1249 Executable File Format"
1251 John Levine's book "Linkers and Loaders" contains useful
1256 #if defined(OBJFORMAT_PEi386)
1260 typedef unsigned char UChar;
1261 typedef unsigned short UInt16;
1262 typedef unsigned int UInt32;
1269 UInt16 NumberOfSections;
1270 UInt32 TimeDateStamp;
1271 UInt32 PointerToSymbolTable;
1272 UInt32 NumberOfSymbols;
1273 UInt16 SizeOfOptionalHeader;
1274 UInt16 Characteristics;
1278 #define sizeof_COFF_header 20
1285 UInt32 VirtualAddress;
1286 UInt32 SizeOfRawData;
1287 UInt32 PointerToRawData;
1288 UInt32 PointerToRelocations;
1289 UInt32 PointerToLinenumbers;
1290 UInt16 NumberOfRelocations;
1291 UInt16 NumberOfLineNumbers;
1292 UInt32 Characteristics;
1296 #define sizeof_COFF_section 40
1303 UInt16 SectionNumber;
1306 UChar NumberOfAuxSymbols;
1310 #define sizeof_COFF_symbol 18
1315 UInt32 VirtualAddress;
1316 UInt32 SymbolTableIndex;
1321 #define sizeof_COFF_reloc 10
1324 /* From PE spec doc, section 3.3.2 */
1325 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1326 windows.h -- for the same purpose, but I want to know what I'm
1328 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1329 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1330 #define MYIMAGE_FILE_DLL 0x2000
1331 #define MYIMAGE_FILE_SYSTEM 0x1000
1332 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1333 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1334 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1336 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1337 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1338 #define MYIMAGE_SYM_CLASS_STATIC 3
1339 #define MYIMAGE_SYM_UNDEFINED 0
1341 /* From PE spec doc, section 4.1 */
1342 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1343 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1344 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1346 /* From PE spec doc, section 5.2.1 */
1347 #define MYIMAGE_REL_I386_DIR32 0x0006
1348 #define MYIMAGE_REL_I386_REL32 0x0014
1351 /* We use myindex to calculate array addresses, rather than
1352 simply doing the normal subscript thing. That's because
1353 some of the above structs have sizes which are not
1354 a whole number of words. GCC rounds their sizes up to a
1355 whole number of words, which means that the address calcs
1356 arising from using normal C indexing or pointer arithmetic
1357 are just plain wrong. Sigh.
1360 myindex ( int scale, void* base, int index )
1363 ((UChar*)base) + scale * index;
1368 printName ( UChar* name, UChar* strtab )
1370 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1371 UInt32 strtab_offset = * (UInt32*)(name+4);
1372 fprintf ( stderr, "%s", strtab + strtab_offset );
1375 for (i = 0; i < 8; i++) {
1376 if (name[i] == 0) break;
1377 fprintf ( stderr, "%c", name[i] );
1384 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1386 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1387 UInt32 strtab_offset = * (UInt32*)(name+4);
1388 strncpy ( dst, strtab+strtab_offset, dstSize );
1394 if (name[i] == 0) break;
1404 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1407 /* If the string is longer than 8 bytes, look in the
1408 string table for it -- this will be correctly zero terminated.
1410 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1411 UInt32 strtab_offset = * (UInt32*)(name+4);
1412 return ((UChar*)strtab) + strtab_offset;
1414 /* Otherwise, if shorter than 8 bytes, return the original,
1415 which by defn is correctly terminated.
1417 if (name[7]==0) return name;
1418 /* The annoying case: 8 bytes. Copy into a temporary
1419 (which is never freed ...)
1421 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1423 strncpy(newstr,name,8);
1429 /* Just compares the short names (first 8 chars) */
1430 static COFF_section *
1431 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1435 = (COFF_header*)(oc->image);
1436 COFF_section* sectab
1438 ((UChar*)(oc->image))
1439 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1441 for (i = 0; i < hdr->NumberOfSections; i++) {
1444 COFF_section* section_i
1446 myindex ( sizeof_COFF_section, sectab, i );
1447 n1 = (UChar*) &(section_i->Name);
1449 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1450 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1451 n1[6]==n2[6] && n1[7]==n2[7])
1460 zapTrailingAtSign ( UChar* sym )
1462 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1464 if (sym[0] == 0) return;
1466 while (sym[i] != 0) i++;
1469 while (j > 0 && my_isdigit(sym[j])) j--;
1470 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1476 ocVerifyImage_PEi386 ( ObjectCode* oc )
1481 COFF_section* sectab;
1482 COFF_symbol* symtab;
1484 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1485 hdr = (COFF_header*)(oc->image);
1486 sectab = (COFF_section*) (
1487 ((UChar*)(oc->image))
1488 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1490 symtab = (COFF_symbol*) (
1491 ((UChar*)(oc->image))
1492 + hdr->PointerToSymbolTable
1494 strtab = ((UChar*)symtab)
1495 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1497 if (hdr->Machine != 0x14c) {
1498 belch("Not x86 PEi386");
1501 if (hdr->SizeOfOptionalHeader != 0) {
1502 belch("PEi386 with nonempty optional header");
1505 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1506 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1507 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1508 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1509 belch("Not a PEi386 object file");
1512 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1513 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1514 belch("Invalid PEi386 word size or endiannness: %d",
1515 (int)(hdr->Characteristics));
1518 /* If the string table size is way crazy, this might indicate that
1519 there are more than 64k relocations, despite claims to the
1520 contrary. Hence this test. */
1521 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1523 if ( (*(UInt32*)strtab) > 600000 ) {
1524 /* Note that 600k has no special significance other than being
1525 big enough to handle the almost-2MB-sized lumps that
1526 constitute HSwin32*.o. */
1527 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1532 /* No further verification after this point; only debug printing. */
1534 IF_DEBUG(linker, i=1);
1535 if (i == 0) return 1;
1538 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1540 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1542 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1544 fprintf ( stderr, "\n" );
1546 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1548 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1550 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1552 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1554 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1556 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1558 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1560 /* Print the section table. */
1561 fprintf ( stderr, "\n" );
1562 for (i = 0; i < hdr->NumberOfSections; i++) {
1564 COFF_section* sectab_i
1566 myindex ( sizeof_COFF_section, sectab, i );
1573 printName ( sectab_i->Name, strtab );
1583 sectab_i->VirtualSize,
1584 sectab_i->VirtualAddress,
1585 sectab_i->SizeOfRawData,
1586 sectab_i->PointerToRawData,
1587 sectab_i->NumberOfRelocations,
1588 sectab_i->PointerToRelocations,
1589 sectab_i->PointerToRawData
1591 reltab = (COFF_reloc*) (
1592 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1595 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1596 /* If the relocation field (a short) has overflowed, the
1597 * real count can be found in the first reloc entry.
1599 * See Section 4.1 (last para) of the PE spec (rev6.0).
1601 COFF_reloc* rel = (COFF_reloc*)
1602 myindex ( sizeof_COFF_reloc, reltab, 0 );
1603 noRelocs = rel->VirtualAddress;
1606 noRelocs = sectab_i->NumberOfRelocations;
1610 for (; j < noRelocs; j++) {
1612 COFF_reloc* rel = (COFF_reloc*)
1613 myindex ( sizeof_COFF_reloc, reltab, j );
1615 " type 0x%-4x vaddr 0x%-8x name `",
1617 rel->VirtualAddress );
1618 sym = (COFF_symbol*)
1619 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1620 /* Hmm..mysterious looking offset - what's it for? SOF */
1621 printName ( sym->Name, strtab -10 );
1622 fprintf ( stderr, "'\n" );
1625 fprintf ( stderr, "\n" );
1627 fprintf ( stderr, "\n" );
1628 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1629 fprintf ( stderr, "---START of string table---\n");
1630 for (i = 4; i < *(Int32*)strtab; i++) {
1632 fprintf ( stderr, "\n"); else
1633 fprintf( stderr, "%c", strtab[i] );
1635 fprintf ( stderr, "--- END of string table---\n");
1637 fprintf ( stderr, "\n" );
1640 COFF_symbol* symtab_i;
1641 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1642 symtab_i = (COFF_symbol*)
1643 myindex ( sizeof_COFF_symbol, symtab, i );
1649 printName ( symtab_i->Name, strtab );
1658 (Int32)(symtab_i->SectionNumber),
1659 (UInt32)symtab_i->Type,
1660 (UInt32)symtab_i->StorageClass,
1661 (UInt32)symtab_i->NumberOfAuxSymbols
1663 i += symtab_i->NumberOfAuxSymbols;
1667 fprintf ( stderr, "\n" );
1673 ocGetNames_PEi386 ( ObjectCode* oc )
1676 COFF_section* sectab;
1677 COFF_symbol* symtab;
1684 hdr = (COFF_header*)(oc->image);
1685 sectab = (COFF_section*) (
1686 ((UChar*)(oc->image))
1687 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1689 symtab = (COFF_symbol*) (
1690 ((UChar*)(oc->image))
1691 + hdr->PointerToSymbolTable
1693 strtab = ((UChar*)(oc->image))
1694 + hdr->PointerToSymbolTable
1695 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1697 /* Allocate space for any (local, anonymous) .bss sections. */
1699 for (i = 0; i < hdr->NumberOfSections; i++) {
1701 COFF_section* sectab_i
1703 myindex ( sizeof_COFF_section, sectab, i );
1704 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1705 if (sectab_i->VirtualSize == 0) continue;
1706 /* This is a non-empty .bss section. Allocate zeroed space for
1707 it, and set its PointerToRawData field such that oc->image +
1708 PointerToRawData == addr_of_zeroed_space. */
1709 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1710 "ocGetNames_PEi386(anonymous bss)");
1711 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1712 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1713 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1716 /* Copy section information into the ObjectCode. */
1718 for (i = 0; i < hdr->NumberOfSections; i++) {
1724 = SECTIONKIND_OTHER;
1725 COFF_section* sectab_i
1727 myindex ( sizeof_COFF_section, sectab, i );
1728 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1731 /* I'm sure this is the Right Way to do it. However, the
1732 alternative of testing the sectab_i->Name field seems to
1733 work ok with Cygwin.
1735 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1736 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1737 kind = SECTIONKIND_CODE_OR_RODATA;
1740 if (0==strcmp(".text",sectab_i->Name) ||
1741 0==strcmp(".rodata",sectab_i->Name))
1742 kind = SECTIONKIND_CODE_OR_RODATA;
1743 if (0==strcmp(".data",sectab_i->Name) ||
1744 0==strcmp(".bss",sectab_i->Name))
1745 kind = SECTIONKIND_RWDATA;
1747 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1748 sz = sectab_i->SizeOfRawData;
1749 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1751 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1752 end = start + sz - 1;
1754 if (kind == SECTIONKIND_OTHER
1755 /* Ignore sections called which contain stabs debugging
1757 && 0 != strcmp(".stab", sectab_i->Name)
1758 && 0 != strcmp(".stabstr", sectab_i->Name)
1760 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1764 if (kind != SECTIONKIND_OTHER && end >= start) {
1765 addSection(oc, kind, start, end);
1766 addProddableBlock(oc, start, end - start + 1);
1770 /* Copy exported symbols into the ObjectCode. */
1772 oc->n_symbols = hdr->NumberOfSymbols;
1773 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1774 "ocGetNames_PEi386(oc->symbols)");
1775 /* Call me paranoid; I don't care. */
1776 for (i = 0; i < oc->n_symbols; i++)
1777 oc->symbols[i] = NULL;
1781 COFF_symbol* symtab_i;
1782 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1783 symtab_i = (COFF_symbol*)
1784 myindex ( sizeof_COFF_symbol, symtab, i );
1788 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1789 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1790 /* This symbol is global and defined, viz, exported */
1791 /* for MYIMAGE_SYMCLASS_EXTERNAL
1792 && !MYIMAGE_SYM_UNDEFINED,
1793 the address of the symbol is:
1794 address of relevant section + offset in section
1796 COFF_section* sectabent
1797 = (COFF_section*) myindex ( sizeof_COFF_section,
1799 symtab_i->SectionNumber-1 );
1800 addr = ((UChar*)(oc->image))
1801 + (sectabent->PointerToRawData
1805 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1806 && symtab_i->Value > 0) {
1807 /* This symbol isn't in any section at all, ie, global bss.
1808 Allocate zeroed space for it. */
1809 addr = stgCallocBytes(1, symtab_i->Value,
1810 "ocGetNames_PEi386(non-anonymous bss)");
1811 addSection(oc, SECTIONKIND_RWDATA, addr,
1812 ((UChar*)addr) + symtab_i->Value - 1);
1813 addProddableBlock(oc, addr, symtab_i->Value);
1814 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1817 if (addr != NULL ) {
1818 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1819 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1820 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1821 ASSERT(i >= 0 && i < oc->n_symbols);
1822 /* cstring_from_COFF_symbol_name always succeeds. */
1823 oc->symbols[i] = sname;
1824 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1828 "IGNORING symbol %d\n"
1832 printName ( symtab_i->Name, strtab );
1841 (Int32)(symtab_i->SectionNumber),
1842 (UInt32)symtab_i->Type,
1843 (UInt32)symtab_i->StorageClass,
1844 (UInt32)symtab_i->NumberOfAuxSymbols
1849 i += symtab_i->NumberOfAuxSymbols;
1858 ocResolve_PEi386 ( ObjectCode* oc )
1861 COFF_section* sectab;
1862 COFF_symbol* symtab;
1872 /* ToDo: should be variable-sized? But is at least safe in the
1873 sense of buffer-overrun-proof. */
1875 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1877 hdr = (COFF_header*)(oc->image);
1878 sectab = (COFF_section*) (
1879 ((UChar*)(oc->image))
1880 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1882 symtab = (COFF_symbol*) (
1883 ((UChar*)(oc->image))
1884 + hdr->PointerToSymbolTable
1886 strtab = ((UChar*)(oc->image))
1887 + hdr->PointerToSymbolTable
1888 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1890 for (i = 0; i < hdr->NumberOfSections; i++) {
1891 COFF_section* sectab_i
1893 myindex ( sizeof_COFF_section, sectab, i );
1896 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1899 /* Ignore sections called which contain stabs debugging
1901 if (0 == strcmp(".stab", sectab_i->Name)
1902 || 0 == strcmp(".stabstr", sectab_i->Name))
1905 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1906 /* If the relocation field (a short) has overflowed, the
1907 * real count can be found in the first reloc entry.
1909 * See Section 4.1 (last para) of the PE spec (rev6.0).
1911 COFF_reloc* rel = (COFF_reloc*)
1912 myindex ( sizeof_COFF_reloc, reltab, 0 );
1913 noRelocs = rel->VirtualAddress;
1914 fprintf(stderr, "Overflown relocs: %u\n", noRelocs);
1917 noRelocs = sectab_i->NumberOfRelocations;
1922 for (; j < noRelocs; j++) {
1924 COFF_reloc* reltab_j
1926 myindex ( sizeof_COFF_reloc, reltab, j );
1928 /* the location to patch */
1930 ((UChar*)(oc->image))
1931 + (sectab_i->PointerToRawData
1932 + reltab_j->VirtualAddress
1933 - sectab_i->VirtualAddress )
1935 /* the existing contents of pP */
1937 /* the symbol to connect to */
1938 sym = (COFF_symbol*)
1939 myindex ( sizeof_COFF_symbol,
1940 symtab, reltab_j->SymbolTableIndex );
1943 "reloc sec %2d num %3d: type 0x%-4x "
1944 "vaddr 0x%-8x name `",
1946 (UInt32)reltab_j->Type,
1947 reltab_j->VirtualAddress );
1948 printName ( sym->Name, strtab );
1949 fprintf ( stderr, "'\n" ));
1951 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1952 COFF_section* section_sym
1953 = findPEi386SectionCalled ( oc, sym->Name );
1955 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1958 S = ((UInt32)(oc->image))
1959 + (section_sym->PointerToRawData
1962 copyName ( sym->Name, strtab, symbol, 1000-1 );
1963 (void*)S = lookupLocalSymbol( oc, symbol );
1964 if ((void*)S != NULL) goto foundit;
1965 (void*)S = lookupSymbol( symbol );
1966 if ((void*)S != NULL) goto foundit;
1967 zapTrailingAtSign ( symbol );
1968 (void*)S = lookupLocalSymbol( oc, symbol );
1969 if ((void*)S != NULL) goto foundit;
1970 (void*)S = lookupSymbol( symbol );
1971 if ((void*)S != NULL) goto foundit;
1972 /* Newline first because the interactive linker has printed "linking..." */
1973 belch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
1977 checkProddableBlock(oc, pP);
1978 switch (reltab_j->Type) {
1979 case MYIMAGE_REL_I386_DIR32:
1982 case MYIMAGE_REL_I386_REL32:
1983 /* Tricky. We have to insert a displacement at
1984 pP which, when added to the PC for the _next_
1985 insn, gives the address of the target (S).
1986 Problem is to know the address of the next insn
1987 when we only know pP. We assume that this
1988 literal field is always the last in the insn,
1989 so that the address of the next insn is pP+4
1990 -- hence the constant 4.
1991 Also I don't know if A should be added, but so
1992 far it has always been zero.
1995 *pP = S - ((UInt32)pP) - 4;
1998 belch("%s: unhandled PEi386 relocation type %d",
1999 oc->fileName, reltab_j->Type);
2006 IF_DEBUG(linker, belch("completed %s", oc->fileName));
2010 #endif /* defined(OBJFORMAT_PEi386) */
2013 /* --------------------------------------------------------------------------
2015 * ------------------------------------------------------------------------*/
2017 #if defined(OBJFORMAT_ELF)
2022 #if defined(sparc_TARGET_ARCH)
2023 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
2024 #elif defined(i386_TARGET_ARCH)
2025 # define ELF_TARGET_386 /* Used inside <elf.h> */
2026 #elif defined(x86_64_TARGET_ARCH)
2027 # define ELF_TARGET_X64_64
2029 #elif defined (ia64_TARGET_ARCH)
2030 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
2032 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
2033 # define ELF_NEED_GOT /* needs Global Offset Table */
2034 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
2037 #if !defined(openbsd_TARGET_OS)
2040 /* openbsd elf has things in different places, with diff names */
2041 #include <elf_abi.h>
2042 #include <machine/reloc.h>
2043 #define R_386_32 RELOC_32
2044 #define R_386_PC32 RELOC_PC32
2048 * Define a set of types which can be used for both ELF32 and ELF64
2052 #define ELFCLASS ELFCLASS64
2053 #define Elf_Addr Elf64_Addr
2054 #define Elf_Word Elf64_Word
2055 #define Elf_Sword Elf64_Sword
2056 #define Elf_Ehdr Elf64_Ehdr
2057 #define Elf_Phdr Elf64_Phdr
2058 #define Elf_Shdr Elf64_Shdr
2059 #define Elf_Sym Elf64_Sym
2060 #define Elf_Rel Elf64_Rel
2061 #define Elf_Rela Elf64_Rela
2062 #define ELF_ST_TYPE ELF64_ST_TYPE
2063 #define ELF_ST_BIND ELF64_ST_BIND
2064 #define ELF_R_TYPE ELF64_R_TYPE
2065 #define ELF_R_SYM ELF64_R_SYM
2067 #define ELFCLASS ELFCLASS32
2068 #define Elf_Addr Elf32_Addr
2069 #define Elf_Word Elf32_Word
2070 #define Elf_Sword Elf32_Sword
2071 #define Elf_Ehdr Elf32_Ehdr
2072 #define Elf_Phdr Elf32_Phdr
2073 #define Elf_Shdr Elf32_Shdr
2074 #define Elf_Sym Elf32_Sym
2075 #define Elf_Rel Elf32_Rel
2076 #define Elf_Rela Elf32_Rela
2078 #define ELF_ST_TYPE ELF32_ST_TYPE
2081 #define ELF_ST_BIND ELF32_ST_BIND
2084 #define ELF_R_TYPE ELF32_R_TYPE
2087 #define ELF_R_SYM ELF32_R_SYM
2093 * Functions to allocate entries in dynamic sections. Currently we simply
2094 * preallocate a large number, and we don't check if a entry for the given
2095 * target already exists (a linear search is too slow). Ideally these
2096 * entries would be associated with symbols.
2099 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
2100 #define GOT_SIZE 0x20000
2101 #define FUNCTION_TABLE_SIZE 0x10000
2102 #define PLT_SIZE 0x08000
2105 static Elf_Addr got[GOT_SIZE];
2106 static unsigned int gotIndex;
2107 static Elf_Addr gp_val = (Elf_Addr)got;
2110 allocateGOTEntry(Elf_Addr target)
2114 if (gotIndex >= GOT_SIZE)
2115 barf("Global offset table overflow");
2117 entry = &got[gotIndex++];
2119 return (Elf_Addr)entry;
2123 #ifdef ELF_FUNCTION_DESC
2129 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2130 static unsigned int functionTableIndex;
2133 allocateFunctionDesc(Elf_Addr target)
2135 FunctionDesc *entry;
2137 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2138 barf("Function table overflow");
2140 entry = &functionTable[functionTableIndex++];
2142 entry->gp = (Elf_Addr)gp_val;
2143 return (Elf_Addr)entry;
2147 copyFunctionDesc(Elf_Addr target)
2149 FunctionDesc *olddesc = (FunctionDesc *)target;
2150 FunctionDesc *newdesc;
2152 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2153 newdesc->gp = olddesc->gp;
2154 return (Elf_Addr)newdesc;
2159 #ifdef ia64_TARGET_ARCH
2160 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2161 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2163 static unsigned char plt_code[] =
2165 /* taken from binutils bfd/elfxx-ia64.c */
2166 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2167 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2168 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2169 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2170 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2171 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2174 /* If we can't get to the function descriptor via gp, take a local copy of it */
2175 #define PLT_RELOC(code, target) { \
2176 Elf64_Sxword rel_value = target - gp_val; \
2177 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2178 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2180 ia64_reloc_gprel22((Elf_Addr)code, target); \
2185 unsigned char code[sizeof(plt_code)];
2189 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2191 PLTEntry *plt = (PLTEntry *)oc->plt;
2194 if (oc->pltIndex >= PLT_SIZE)
2195 barf("Procedure table overflow");
2197 entry = &plt[oc->pltIndex++];
2198 memcpy(entry->code, plt_code, sizeof(entry->code));
2199 PLT_RELOC(entry->code, target);
2200 return (Elf_Addr)entry;
2206 return (PLT_SIZE * sizeof(PLTEntry));
2212 * Generic ELF functions
2216 findElfSection ( void* objImage, Elf_Word sh_type )
2218 char* ehdrC = (char*)objImage;
2219 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2220 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2221 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2225 for (i = 0; i < ehdr->e_shnum; i++) {
2226 if (shdr[i].sh_type == sh_type
2227 /* Ignore the section header's string table. */
2228 && i != ehdr->e_shstrndx
2229 /* Ignore string tables named .stabstr, as they contain
2231 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2233 ptr = ehdrC + shdr[i].sh_offset;
2240 #if defined(ia64_TARGET_ARCH)
2242 findElfSegment ( void* objImage, Elf_Addr vaddr )
2244 char* ehdrC = (char*)objImage;
2245 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2246 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2247 Elf_Addr segaddr = 0;
2250 for (i = 0; i < ehdr->e_phnum; i++) {
2251 segaddr = phdr[i].p_vaddr;
2252 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2260 ocVerifyImage_ELF ( ObjectCode* oc )
2264 int i, j, nent, nstrtab, nsymtabs;
2268 char* ehdrC = (char*)(oc->image);
2269 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2271 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2272 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2273 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2274 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2275 belch("%s: not an ELF object", oc->fileName);
2279 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2280 belch("%s: unsupported ELF format", oc->fileName);
2284 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2285 IF_DEBUG(linker,belch( "Is little-endian" ));
2287 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2288 IF_DEBUG(linker,belch( "Is big-endian" ));
2290 belch("%s: unknown endiannness", oc->fileName);
2294 if (ehdr->e_type != ET_REL) {
2295 belch("%s: not a relocatable object (.o) file", oc->fileName);
2298 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2300 IF_DEBUG(linker,belch( "Architecture is " ));
2301 switch (ehdr->e_machine) {
2302 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2303 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2305 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2307 default: IF_DEBUG(linker,belch( "unknown" ));
2308 belch("%s: unknown architecture", oc->fileName);
2312 IF_DEBUG(linker,belch(
2313 "\nSection header table: start %d, n_entries %d, ent_size %d",
2314 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2316 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2318 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2320 if (ehdr->e_shstrndx == SHN_UNDEF) {
2321 belch("%s: no section header string table", oc->fileName);
2324 IF_DEBUG(linker,belch( "Section header string table is section %d",
2326 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2329 for (i = 0; i < ehdr->e_shnum; i++) {
2330 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2331 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2332 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2333 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2334 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2335 ehdrC + shdr[i].sh_offset,
2336 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2338 if (shdr[i].sh_type == SHT_REL) {
2339 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2340 } else if (shdr[i].sh_type == SHT_RELA) {
2341 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2343 IF_DEBUG(linker,fprintf(stderr," "));
2346 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2350 IF_DEBUG(linker,belch( "\nString tables" ));
2353 for (i = 0; i < ehdr->e_shnum; i++) {
2354 if (shdr[i].sh_type == SHT_STRTAB
2355 /* Ignore the section header's string table. */
2356 && i != ehdr->e_shstrndx
2357 /* Ignore string tables named .stabstr, as they contain
2359 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2361 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2362 strtab = ehdrC + shdr[i].sh_offset;
2367 belch("%s: no string tables, or too many", oc->fileName);
2372 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2373 for (i = 0; i < ehdr->e_shnum; i++) {
2374 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2375 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2377 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2378 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2379 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2381 shdr[i].sh_size % sizeof(Elf_Sym)
2383 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2384 belch("%s: non-integral number of symbol table entries", oc->fileName);
2387 for (j = 0; j < nent; j++) {
2388 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2389 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2390 (int)stab[j].st_shndx,
2391 (int)stab[j].st_size,
2392 (char*)stab[j].st_value ));
2394 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2395 switch (ELF_ST_TYPE(stab[j].st_info)) {
2396 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2397 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2398 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2399 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2400 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2401 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2403 IF_DEBUG(linker,fprintf(stderr, " " ));
2405 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2406 switch (ELF_ST_BIND(stab[j].st_info)) {
2407 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2408 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2409 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2410 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2412 IF_DEBUG(linker,fprintf(stderr, " " ));
2414 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2418 if (nsymtabs == 0) {
2419 belch("%s: didn't find any symbol tables", oc->fileName);
2428 ocGetNames_ELF ( ObjectCode* oc )
2433 char* ehdrC = (char*)(oc->image);
2434 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2435 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2436 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2438 ASSERT(symhash != NULL);
2441 belch("%s: no strtab", oc->fileName);
2446 for (i = 0; i < ehdr->e_shnum; i++) {
2447 /* Figure out what kind of section it is. Logic derived from
2448 Figure 1.14 ("Special Sections") of the ELF document
2449 ("Portable Formats Specification, Version 1.1"). */
2450 Elf_Shdr hdr = shdr[i];
2451 SectionKind kind = SECTIONKIND_OTHER;
2454 if (hdr.sh_type == SHT_PROGBITS
2455 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2456 /* .text-style section */
2457 kind = SECTIONKIND_CODE_OR_RODATA;
2460 if (hdr.sh_type == SHT_PROGBITS
2461 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2462 /* .data-style section */
2463 kind = SECTIONKIND_RWDATA;
2466 if (hdr.sh_type == SHT_PROGBITS
2467 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2468 /* .rodata-style section */
2469 kind = SECTIONKIND_CODE_OR_RODATA;
2472 if (hdr.sh_type == SHT_NOBITS
2473 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2474 /* .bss-style section */
2475 kind = SECTIONKIND_RWDATA;
2479 if (is_bss && shdr[i].sh_size > 0) {
2480 /* This is a non-empty .bss section. Allocate zeroed space for
2481 it, and set its .sh_offset field such that
2482 ehdrC + .sh_offset == addr_of_zeroed_space. */
2483 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2484 "ocGetNames_ELF(BSS)");
2485 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2487 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2488 zspace, shdr[i].sh_size);
2492 /* fill in the section info */
2493 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2494 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2495 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2496 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2499 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2501 /* copy stuff into this module's object symbol table */
2502 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2503 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2505 oc->n_symbols = nent;
2506 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2507 "ocGetNames_ELF(oc->symbols)");
2509 for (j = 0; j < nent; j++) {
2511 char isLocal = FALSE; /* avoids uninit-var warning */
2513 char* nm = strtab + stab[j].st_name;
2514 int secno = stab[j].st_shndx;
2516 /* Figure out if we want to add it; if so, set ad to its
2517 address. Otherwise leave ad == NULL. */
2519 if (secno == SHN_COMMON) {
2521 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2523 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2524 stab[j].st_size, nm);
2526 /* Pointless to do addProddableBlock() for this area,
2527 since the linker should never poke around in it. */
2530 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2531 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2533 /* and not an undefined symbol */
2534 && stab[j].st_shndx != SHN_UNDEF
2535 /* and not in a "special section" */
2536 && stab[j].st_shndx < SHN_LORESERVE
2538 /* and it's a not a section or string table or anything silly */
2539 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2540 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2541 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2544 /* Section 0 is the undefined section, hence > and not >=. */
2545 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2547 if (shdr[secno].sh_type == SHT_NOBITS) {
2548 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2549 stab[j].st_size, stab[j].st_value, nm);
2552 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2553 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2556 #ifdef ELF_FUNCTION_DESC
2557 /* dlsym() and the initialisation table both give us function
2558 * descriptors, so to be consistent we store function descriptors
2559 * in the symbol table */
2560 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2561 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2563 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2564 ad, oc->fileName, nm ));
2569 /* And the decision is ... */
2573 oc->symbols[j] = nm;
2576 /* Ignore entirely. */
2578 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2582 IF_DEBUG(linker,belch( "skipping `%s'",
2583 strtab + stab[j].st_name ));
2586 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2587 (int)ELF_ST_BIND(stab[j].st_info),
2588 (int)ELF_ST_TYPE(stab[j].st_info),
2589 (int)stab[j].st_shndx,
2590 strtab + stab[j].st_name
2593 oc->symbols[j] = NULL;
2602 /* Do ELF relocations which lack an explicit addend. All x86-linux
2603 relocations appear to be of this form. */
2605 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2606 Elf_Shdr* shdr, int shnum,
2607 Elf_Sym* stab, char* strtab )
2612 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2613 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2614 int target_shndx = shdr[shnum].sh_info;
2615 int symtab_shndx = shdr[shnum].sh_link;
2617 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2618 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2619 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2620 target_shndx, symtab_shndx ));
2622 for (j = 0; j < nent; j++) {
2623 Elf_Addr offset = rtab[j].r_offset;
2624 Elf_Addr info = rtab[j].r_info;
2626 Elf_Addr P = ((Elf_Addr)targ) + offset;
2627 Elf_Word* pP = (Elf_Word*)P;
2632 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2633 j, (void*)offset, (void*)info ));
2635 IF_DEBUG(linker,belch( " ZERO" ));
2638 Elf_Sym sym = stab[ELF_R_SYM(info)];
2639 /* First see if it is a local symbol. */
2640 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2641 /* Yes, so we can get the address directly from the ELF symbol
2643 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2645 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2646 + stab[ELF_R_SYM(info)].st_value);
2649 /* No, so look up the name in our global table. */
2650 symbol = strtab + sym.st_name;
2651 (void*)S = lookupSymbol( symbol );
2654 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2657 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2660 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2661 (void*)P, (void*)S, (void*)A ));
2662 checkProddableBlock ( oc, pP );
2666 switch (ELF_R_TYPE(info)) {
2667 # ifdef i386_TARGET_ARCH
2668 case R_386_32: *pP = value; break;
2669 case R_386_PC32: *pP = value - P; break;
2672 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2673 oc->fileName, ELF_R_TYPE(info));
2681 /* Do ELF relocations for which explicit addends are supplied.
2682 sparc-solaris relocations appear to be of this form. */
2684 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2685 Elf_Shdr* shdr, int shnum,
2686 Elf_Sym* stab, char* strtab )
2691 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2692 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2693 int target_shndx = shdr[shnum].sh_info;
2694 int symtab_shndx = shdr[shnum].sh_link;
2696 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2697 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2698 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2699 target_shndx, symtab_shndx ));
2701 for (j = 0; j < nent; j++) {
2702 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
2703 /* This #ifdef only serves to avoid unused-var warnings. */
2704 Elf_Addr offset = rtab[j].r_offset;
2705 Elf_Addr P = targ + offset;
2707 Elf_Addr info = rtab[j].r_info;
2708 Elf_Addr A = rtab[j].r_addend;
2711 # if defined(sparc_TARGET_ARCH)
2712 Elf_Word* pP = (Elf_Word*)P;
2714 # elif defined(ia64_TARGET_ARCH)
2715 Elf64_Xword *pP = (Elf64_Xword *)P;
2719 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2720 j, (void*)offset, (void*)info,
2723 IF_DEBUG(linker,belch( " ZERO" ));
2726 Elf_Sym sym = stab[ELF_R_SYM(info)];
2727 /* First see if it is a local symbol. */
2728 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2729 /* Yes, so we can get the address directly from the ELF symbol
2731 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2733 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2734 + stab[ELF_R_SYM(info)].st_value);
2735 #ifdef ELF_FUNCTION_DESC
2736 /* Make a function descriptor for this function */
2737 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2738 S = allocateFunctionDesc(S + A);
2743 /* No, so look up the name in our global table. */
2744 symbol = strtab + sym.st_name;
2745 (void*)S = lookupSymbol( symbol );
2747 #ifdef ELF_FUNCTION_DESC
2748 /* If a function, already a function descriptor - we would
2749 have to copy it to add an offset. */
2750 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
2751 belch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
2755 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2758 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2761 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2762 (void*)P, (void*)S, (void*)A ));
2763 /* checkProddableBlock ( oc, (void*)P ); */
2767 switch (ELF_R_TYPE(info)) {
2768 # if defined(sparc_TARGET_ARCH)
2769 case R_SPARC_WDISP30:
2770 w1 = *pP & 0xC0000000;
2771 w2 = (Elf_Word)((value - P) >> 2);
2772 ASSERT((w2 & 0xC0000000) == 0);
2777 w1 = *pP & 0xFFC00000;
2778 w2 = (Elf_Word)(value >> 10);
2779 ASSERT((w2 & 0xFFC00000) == 0);
2785 w2 = (Elf_Word)(value & 0x3FF);
2786 ASSERT((w2 & ~0x3FF) == 0);
2790 /* According to the Sun documentation:
2792 This relocation type resembles R_SPARC_32, except it refers to an
2793 unaligned word. That is, the word to be relocated must be treated
2794 as four separate bytes with arbitrary alignment, not as a word
2795 aligned according to the architecture requirements.
2797 (JRS: which means that freeloading on the R_SPARC_32 case
2798 is probably wrong, but hey ...)
2802 w2 = (Elf_Word)value;
2805 # elif defined(ia64_TARGET_ARCH)
2806 case R_IA64_DIR64LSB:
2807 case R_IA64_FPTR64LSB:
2810 case R_IA64_PCREL64LSB:
2813 case R_IA64_SEGREL64LSB:
2814 addr = findElfSegment(ehdrC, value);
2817 case R_IA64_GPREL22:
2818 ia64_reloc_gprel22(P, value);
2820 case R_IA64_LTOFF22:
2821 case R_IA64_LTOFF22X:
2822 case R_IA64_LTOFF_FPTR22:
2823 addr = allocateGOTEntry(value);
2824 ia64_reloc_gprel22(P, addr);
2826 case R_IA64_PCREL21B:
2827 ia64_reloc_pcrel21(P, S, oc);
2830 /* This goes with R_IA64_LTOFF22X and points to the load to
2831 * convert into a move. We don't implement relaxation. */
2835 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2836 oc->fileName, ELF_R_TYPE(info));
2845 ocResolve_ELF ( ObjectCode* oc )
2849 Elf_Sym* stab = NULL;
2850 char* ehdrC = (char*)(oc->image);
2851 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2852 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2853 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2855 /* first find "the" symbol table */
2856 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2858 /* also go find the string table */
2859 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2861 if (stab == NULL || strtab == NULL) {
2862 belch("%s: can't find string or symbol table", oc->fileName);
2866 /* Process the relocation sections. */
2867 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2869 /* Skip sections called ".rel.stab". These appear to contain
2870 relocation entries that, when done, make the stabs debugging
2871 info point at the right places. We ain't interested in all
2873 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2876 if (shdr[shnum].sh_type == SHT_REL ) {
2877 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2878 shnum, stab, strtab );
2882 if (shdr[shnum].sh_type == SHT_RELA) {
2883 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2884 shnum, stab, strtab );
2889 /* Free the local symbol table; we won't need it again. */
2890 freeHashTable(oc->lochash, NULL);
2898 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2899 * at the front. The following utility functions pack and unpack instructions, and
2900 * take care of the most common relocations.
2903 #ifdef ia64_TARGET_ARCH
2906 ia64_extract_instruction(Elf64_Xword *target)
2909 int slot = (Elf_Addr)target & 3;
2910 (Elf_Addr)target &= ~3;
2918 return ((w1 >> 5) & 0x1ffffffffff);
2920 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2924 barf("ia64_extract_instruction: invalid slot %p", target);
2929 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2931 int slot = (Elf_Addr)target & 3;
2932 (Elf_Addr)target &= ~3;
2937 *target |= value << 5;
2940 *target |= value << 46;
2941 *(target+1) |= value >> 18;
2944 *(target+1) |= value << 23;
2950 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2952 Elf64_Xword instruction;
2953 Elf64_Sxword rel_value;
2955 rel_value = value - gp_val;
2956 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2957 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2959 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2960 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2961 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2962 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2963 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2964 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2968 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2970 Elf64_Xword instruction;
2971 Elf64_Sxword rel_value;
2974 entry = allocatePLTEntry(value, oc);
2976 rel_value = (entry >> 4) - (target >> 4);
2977 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2978 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
2980 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2981 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
2982 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2983 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2990 /* --------------------------------------------------------------------------
2992 * ------------------------------------------------------------------------*/
2994 #if defined(OBJFORMAT_MACHO)
2997 Initial support for MachO linking on Darwin/MacOS X on PowerPC chips
2998 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
3000 I hereby formally apologize for the hackish nature of this code.
3001 Things that need to be done:
3002 *) get common symbols and .bss sections to work properly.
3003 Haskell modules seem to work, but C modules can cause problems
3004 *) implement ocVerifyImage_MachO
3005 *) add more sanity checks. The current code just has to segfault if there's a
3009 static int ocVerifyImage_MachO(ObjectCode* oc)
3011 // FIXME: do some verifying here
3015 static int resolveImports(
3018 struct symtab_command *symLC,
3019 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
3020 unsigned long *indirectSyms,
3021 struct nlist *nlist)
3025 for(i=0;i*4<sect->size;i++)
3027 // according to otool, reserved1 contains the first index into the indirect symbol table
3028 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
3029 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3032 if((symbol->n_type & N_TYPE) == N_UNDF
3033 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
3034 addr = (void*) (symbol->n_value);
3035 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
3038 addr = lookupSymbol(nm);
3041 belch("\n%s: unknown symbol `%s'", oc->fileName, nm);
3045 checkProddableBlock(oc,((void**)(image + sect->offset)) + i);
3046 ((void**)(image + sect->offset))[i] = addr;
3052 static int relocateSection(
3055 struct symtab_command *symLC, struct nlist *nlist,
3056 struct section* sections, struct section *sect)
3058 struct relocation_info *relocs;
3061 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
3063 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
3067 relocs = (struct relocation_info*) (image + sect->reloff);
3071 if(relocs[i].r_address & R_SCATTERED)
3073 struct scattered_relocation_info *scat =
3074 (struct scattered_relocation_info*) &relocs[i];
3078 if(scat->r_length == 2 && scat->r_type == GENERIC_RELOC_VANILLA)
3080 unsigned long* word = (unsigned long*) (image + sect->offset + scat->r_address);
3082 checkProddableBlock(oc,word);
3083 *word = scat->r_value + sect->offset + ((long) image);
3087 continue; // FIXME: I hope it's OK to ignore all the others.
3091 struct relocation_info *reloc = &relocs[i];
3092 if(reloc->r_pcrel && !reloc->r_extern)
3095 if(reloc->r_length == 2)
3097 unsigned long word = 0;
3099 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
3100 checkProddableBlock(oc,wordPtr);
3102 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3106 else if(reloc->r_type == PPC_RELOC_LO16)
3108 word = ((unsigned short*) wordPtr)[1];
3109 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
3111 else if(reloc->r_type == PPC_RELOC_HI16)
3113 word = ((unsigned short*) wordPtr)[1] << 16;
3114 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
3116 else if(reloc->r_type == PPC_RELOC_HA16)
3118 word = ((unsigned short*) wordPtr)[1] << 16;
3119 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
3121 else if(reloc->r_type == PPC_RELOC_BR24)
3124 word = (word & 0x03FFFFFC) | (word & 0x02000000) ? 0xFC000000 : 0;
3128 if(!reloc->r_extern)
3131 sections[reloc->r_symbolnum-1].offset
3132 - sections[reloc->r_symbolnum-1].addr
3139 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3140 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3141 word = (unsigned long) (lookupSymbol(nm));
3144 belch("\nunknown symbol `%s'", nm);
3149 word -= ((long)image) + sect->offset + reloc->r_address;
3152 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3157 else if(reloc->r_type == PPC_RELOC_LO16)
3159 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3162 else if(reloc->r_type == PPC_RELOC_HI16)
3164 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3167 else if(reloc->r_type == PPC_RELOC_HA16)
3169 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3170 + ((word & (1<<15)) ? 1 : 0);
3173 else if(reloc->r_type == PPC_RELOC_BR24)
3175 *wordPtr = (*wordPtr & 0xFC000003) | (word & 0x03FFFFFC);
3179 barf("\nunknown relocation %d",reloc->r_type);
3186 static int ocGetNames_MachO(ObjectCode* oc)
3188 char *image = (char*) oc->image;
3189 struct mach_header *header = (struct mach_header*) image;
3190 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3191 unsigned i,curSymbol;
3192 struct segment_command *segLC = NULL;
3193 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3194 struct symtab_command *symLC = NULL;
3195 struct dysymtab_command *dsymLC = NULL;
3196 struct nlist *nlist;
3197 unsigned long commonSize = 0;
3198 char *commonStorage = NULL;
3199 unsigned long commonCounter;
3201 for(i=0;i<header->ncmds;i++)
3203 if(lc->cmd == LC_SEGMENT)
3204 segLC = (struct segment_command*) lc;
3205 else if(lc->cmd == LC_SYMTAB)
3206 symLC = (struct symtab_command*) lc;
3207 else if(lc->cmd == LC_DYSYMTAB)
3208 dsymLC = (struct dysymtab_command*) lc;
3209 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3212 sections = (struct section*) (segLC+1);
3213 nlist = (struct nlist*) (image + symLC->symoff);
3215 for(i=0;i<segLC->nsects;i++)
3217 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3218 la_ptrs = §ions[i];
3219 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3220 nl_ptrs = §ions[i];
3222 // for now, only add __text and __const to the sections table
3223 else if(!strcmp(sections[i].sectname,"__text"))
3224 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3225 (void*) (image + sections[i].offset),
3226 (void*) (image + sections[i].offset + sections[i].size));
3227 else if(!strcmp(sections[i].sectname,"__const"))
3228 addSection(oc, SECTIONKIND_RWDATA,
3229 (void*) (image + sections[i].offset),
3230 (void*) (image + sections[i].offset + sections[i].size));
3231 else if(!strcmp(sections[i].sectname,"__data"))
3232 addSection(oc, SECTIONKIND_RWDATA,
3233 (void*) (image + sections[i].offset),
3234 (void*) (image + sections[i].offset + sections[i].size));
3236 if(sections[i].size > 0) // size 0 segments do exist
3237 addProddableBlock(oc, (void*) (image + sections[i].offset),
3241 // count external symbols defined here
3243 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3245 if((nlist[i].n_type & N_TYPE) == N_SECT)
3248 for(i=0;i<symLC->nsyms;i++)
3250 if((nlist[i].n_type & N_TYPE) == N_UNDF
3251 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3253 commonSize += nlist[i].n_value;
3257 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3258 "ocGetNames_MachO(oc->symbols)");
3260 // insert symbols into hash table
3261 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3263 if((nlist[i].n_type & N_TYPE) == N_SECT)
3265 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3266 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3267 sections[nlist[i].n_sect-1].offset
3268 - sections[nlist[i].n_sect-1].addr
3269 + nlist[i].n_value);
3270 oc->symbols[curSymbol++] = nm;
3274 // insert local symbols into lochash
3275 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3277 if((nlist[i].n_type & N_TYPE) == N_SECT)
3279 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3280 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3281 sections[nlist[i].n_sect-1].offset
3282 - sections[nlist[i].n_sect-1].addr
3283 + nlist[i].n_value);
3288 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3289 commonCounter = (unsigned long)commonStorage;
3290 for(i=0;i<symLC->nsyms;i++)
3292 if((nlist[i].n_type & N_TYPE) == N_UNDF
3293 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3295 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3296 unsigned long sz = nlist[i].n_value;
3298 nlist[i].n_value = commonCounter;
3300 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3301 oc->symbols[curSymbol++] = nm;
3303 commonCounter += sz;
3309 static int ocResolve_MachO(ObjectCode* oc)
3311 char *image = (char*) oc->image;
3312 struct mach_header *header = (struct mach_header*) image;
3313 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3315 struct segment_command *segLC = NULL;
3316 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3317 struct symtab_command *symLC = NULL;
3318 struct dysymtab_command *dsymLC = NULL;
3319 struct nlist *nlist;
3320 unsigned long *indirectSyms;
3322 for(i=0;i<header->ncmds;i++)
3324 if(lc->cmd == LC_SEGMENT)
3325 segLC = (struct segment_command*) lc;
3326 else if(lc->cmd == LC_SYMTAB)
3327 symLC = (struct symtab_command*) lc;
3328 else if(lc->cmd == LC_DYSYMTAB)
3329 dsymLC = (struct dysymtab_command*) lc;
3330 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3333 sections = (struct section*) (segLC+1);
3334 nlist = (struct nlist*) (image + symLC->symoff);
3336 for(i=0;i<segLC->nsects;i++)
3338 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3339 la_ptrs = §ions[i];
3340 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3341 nl_ptrs = §ions[i];
3344 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3347 if(!resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist))
3350 if(!resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist))
3353 for(i=0;i<segLC->nsects;i++)
3355 if(!relocateSection(oc,image,symLC,nlist,sections,§ions[i]))
3359 /* Free the local symbol table; we won't need it again. */
3360 freeHashTable(oc->lochash, NULL);
3366 * The Mach-O object format uses leading underscores. But not everywhere.
3367 * There is a small number of runtime support functions defined in
3368 * libcc_dynamic.a whose name does not have a leading underscore.
3369 * As a consequence, we can't get their address from C code.
3370 * We have to use inline assembler just to take the address of a function.
3374 static void machoInitSymbolsWithoutUnderscore()
3380 __asm__ ("lis %0,hi16(" #x ")\n\tori %0,%0,lo16(" #x ")" : "=r" (p)); \
3381 ghciInsertStrHashTable("(GHCi built-in symbols)", symhash, #x, p);
3383 RTS_MACHO_NOUNDERLINE_SYMBOLS