1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.95 2002/06/12 22:29:43 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>
27 #ifdef HAVE_SYS_STAT_H
35 #if defined(cygwin32_TARGET_OS)
40 #ifdef HAVE_SYS_TIME_H
44 #include <sys/fcntl.h>
45 #include <sys/termios.h>
46 #include <sys/utime.h>
47 #include <sys/utsname.h>
51 #if defined(ia64_TARGET_ARCH)
57 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
58 # define OBJFORMAT_ELF
59 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
60 # define OBJFORMAT_PEi386
62 #elif defined(darwin_TARGET_OS)
63 # define OBJFORMAT_MACHO
64 # include <mach-o/loader.h>
65 # include <mach-o/nlist.h>
66 # include <mach-o/reloc.h>
69 /* Hash table mapping symbol names to Symbol */
70 /*Str*/HashTable *symhash;
72 #if defined(OBJFORMAT_ELF)
73 static int ocVerifyImage_ELF ( ObjectCode* oc );
74 static int ocGetNames_ELF ( ObjectCode* oc );
75 static int ocResolve_ELF ( ObjectCode* oc );
76 #elif defined(OBJFORMAT_PEi386)
77 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
78 static int ocGetNames_PEi386 ( ObjectCode* oc );
79 static int ocResolve_PEi386 ( ObjectCode* oc );
80 #elif defined(OBJFORMAT_MACHO)
81 static int ocVerifyImage_MachO ( ObjectCode* oc );
82 static int ocGetNames_MachO ( ObjectCode* oc );
83 static int ocResolve_MachO ( ObjectCode* oc );
86 /* -----------------------------------------------------------------------------
87 * Built-in symbols from the RTS
90 typedef struct _RtsSymbolVal {
97 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
99 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
100 SymX(makeStableNamezh_fast) \
101 SymX(finalizzeWeakzh_fast)
103 /* These are not available in GUM!!! -- HWL */
104 #define Maybe_ForeignObj
105 #define Maybe_Stable_Names
108 #if !defined (mingw32_TARGET_OS)
109 #define RTS_POSIX_ONLY_SYMBOLS \
110 SymX(stg_sig_install) \
114 #if defined (cygwin32_TARGET_OS)
115 #define RTS_MINGW_ONLY_SYMBOLS /**/
116 /* Don't have the ability to read import libs / archives, so
117 * we have to stupidly list a lot of what libcygwin.a
120 #define RTS_CYGWIN_ONLY_SYMBOLS \
202 #elif !defined(mingw32_TARGET_OS)
203 #define RTS_MINGW_ONLY_SYMBOLS /**/
204 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
205 #else /* defined(mingw32_TARGET_OS) */
206 #define RTS_POSIX_ONLY_SYMBOLS /**/
207 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
209 /* These are statically linked from the mingw libraries into the ghc
210 executable, so we have to employ this hack. */
211 #define RTS_MINGW_ONLY_SYMBOLS \
224 SymX(getservbyname) \
225 SymX(getservbyport) \
226 SymX(getprotobynumber) \
227 SymX(getprotobyname) \
228 SymX(gethostbyname) \
229 SymX(gethostbyaddr) \
264 Sym(_imp___timezone) \
279 #ifdef darwin_TARGET_OS
280 #define RTS_DARWIN_ONLY_SYMBOLS \
288 SymX(stg_gc_enter_2) \
289 SymX(stg_gc_enter_3) \
290 SymX(stg_gc_enter_4) \
291 SymX(stg_gc_enter_5) \
292 SymX(stg_gc_enter_6) \
293 SymX(stg_gc_enter_7) \
294 SymX(stg_gc_enter_8) \
304 #define RTS_DARWIN_ONLY_SYMBOLS
308 # define MAIN_CAP_SYM SymX(MainCapability)
310 # define MAIN_CAP_SYM
313 #define RTS_SYMBOLS \
317 Sym(__stginit_GHCziPrim) \
328 Sym(stg_enterStackTop) \
331 SymX(__stg_gc_enter_1) \
332 SymX(stg_gc_enter_2) \
333 SymX(stg_gc_enter_3) \
334 SymX(stg_gc_enter_4) \
335 SymX(stg_gc_enter_5) \
336 SymX(stg_gc_enter_6) \
337 SymX(stg_gc_enter_7) \
338 SymX(stg_gc_enter_8) \
340 SymX(stg_gc_noregs) \
342 SymX(stg_gc_unbx_r1) \
343 SymX(stg_gc_unpt_r1) \
344 SymX(stg_gc_ut_0_1) \
345 SymX(stg_gc_ut_1_0) \
347 SymX(stg_yield_to_interpreter) \
350 SymX(MallocFailHook) \
351 SymX(NoRunnableThreadsHook) \
353 SymX(OutOfHeapHook) \
354 SymX(PatErrorHdrHook) \
355 SymX(PostTraceHook) \
357 SymX(StackOverflowHook) \
358 SymX(__encodeDouble) \
359 SymX(__encodeFloat) \
362 SymX(__gmpz_cmp_si) \
363 SymX(__gmpz_cmp_ui) \
364 SymX(__gmpz_get_si) \
365 SymX(__gmpz_get_ui) \
366 SymX(__int_encodeDouble) \
367 SymX(__int_encodeFloat) \
368 SymX(andIntegerzh_fast) \
369 SymX(blockAsyncExceptionszh_fast) \
372 SymX(complementIntegerzh_fast) \
373 SymX(cmpIntegerzh_fast) \
374 SymX(cmpIntegerIntzh_fast) \
375 SymX(createAdjustor) \
376 SymX(decodeDoublezh_fast) \
377 SymX(decodeFloatzh_fast) \
380 SymX(deRefWeakzh_fast) \
381 SymX(deRefStablePtrzh_fast) \
382 SymX(divExactIntegerzh_fast) \
383 SymX(divModIntegerzh_fast) \
385 SymX(forkProcesszh_fast) \
386 SymX(freeHaskellFunctionPtr) \
387 SymX(freeStablePtr) \
388 SymX(gcdIntegerzh_fast) \
389 SymX(gcdIntegerIntzh_fast) \
390 SymX(gcdIntzh_fast) \
393 SymX(int2Integerzh_fast) \
394 SymX(integer2Intzh_fast) \
395 SymX(integer2Wordzh_fast) \
396 SymX(isDoubleDenormalized) \
397 SymX(isDoubleInfinite) \
399 SymX(isDoubleNegativeZero) \
400 SymX(isEmptyMVarzh_fast) \
401 SymX(isFloatDenormalized) \
402 SymX(isFloatInfinite) \
404 SymX(isFloatNegativeZero) \
405 SymX(killThreadzh_fast) \
406 SymX(makeStablePtrzh_fast) \
407 SymX(minusIntegerzh_fast) \
408 SymX(mkApUpd0zh_fast) \
409 SymX(myThreadIdzh_fast) \
410 SymX(labelThreadzh_fast) \
411 SymX(newArrayzh_fast) \
412 SymX(newBCOzh_fast) \
413 SymX(newByteArrayzh_fast) \
415 SymX(newMVarzh_fast) \
416 SymX(newMutVarzh_fast) \
417 SymX(newPinnedByteArrayzh_fast) \
418 SymX(orIntegerzh_fast) \
420 SymX(plusIntegerzh_fast) \
423 SymX(putMVarzh_fast) \
424 SymX(quotIntegerzh_fast) \
425 SymX(quotRemIntegerzh_fast) \
427 SymX(remIntegerzh_fast) \
428 SymX(resetNonBlockingFd) \
431 SymX(rts_checkSchedStatus) \
434 SymX(rts_evalLazyIO) \
439 SymX(rts_getDouble) \
444 SymX(rts_getStablePtr) \
445 SymX(rts_getThreadId) \
447 SymX(rts_getWord32) \
459 SymX(rts_mkStablePtr) \
468 SymX(shutdownHaskellAndExit) \
469 SymX(stable_ptr_table) \
470 SymX(stackOverflow) \
471 SymX(stg_CAF_BLACKHOLE_info) \
472 SymX(stg_CHARLIKE_closure) \
473 SymX(stg_EMPTY_MVAR_info) \
474 SymX(stg_IND_STATIC_info) \
475 SymX(stg_INTLIKE_closure) \
476 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
477 SymX(stg_WEAK_info) \
478 SymX(stg_ap_1_upd_info) \
479 SymX(stg_ap_2_upd_info) \
480 SymX(stg_ap_3_upd_info) \
481 SymX(stg_ap_4_upd_info) \
482 SymX(stg_ap_5_upd_info) \
483 SymX(stg_ap_6_upd_info) \
484 SymX(stg_ap_7_upd_info) \
485 SymX(stg_ap_8_upd_info) \
487 SymX(stg_sel_0_upd_info) \
488 SymX(stg_sel_10_upd_info) \
489 SymX(stg_sel_11_upd_info) \
490 SymX(stg_sel_12_upd_info) \
491 SymX(stg_sel_13_upd_info) \
492 SymX(stg_sel_14_upd_info) \
493 SymX(stg_sel_15_upd_info) \
494 SymX(stg_sel_1_upd_info) \
495 SymX(stg_sel_2_upd_info) \
496 SymX(stg_sel_3_upd_info) \
497 SymX(stg_sel_4_upd_info) \
498 SymX(stg_sel_5_upd_info) \
499 SymX(stg_sel_6_upd_info) \
500 SymX(stg_sel_7_upd_info) \
501 SymX(stg_sel_8_upd_info) \
502 SymX(stg_sel_9_upd_info) \
503 SymX(stg_seq_frame_info) \
504 SymX(stg_upd_frame_info) \
505 SymX(__stg_update_PAP) \
506 SymX(suspendThread) \
507 SymX(takeMVarzh_fast) \
508 SymX(timesIntegerzh_fast) \
509 SymX(tryPutMVarzh_fast) \
510 SymX(tryTakeMVarzh_fast) \
511 SymX(unblockAsyncExceptionszh_fast) \
512 SymX(unsafeThawArrayzh_fast) \
513 SymX(waitReadzh_fast) \
514 SymX(waitWritezh_fast) \
515 SymX(word2Integerzh_fast) \
516 SymX(xorIntegerzh_fast) \
519 #ifdef SUPPORT_LONG_LONGS
520 #define RTS_LONG_LONG_SYMS \
521 SymX(int64ToIntegerzh_fast) \
522 SymX(word64ToIntegerzh_fast)
524 #define RTS_LONG_LONG_SYMS /* nothing */
527 #ifdef ia64_TARGET_ARCH
528 /* force these symbols to be present */
529 #define RTS_EXTRA_SYMBOLS \
532 #define RTS_EXTRA_SYMBOLS /* nothing */
535 /* entirely bogus claims about types of these symbols */
536 #define Sym(vvv) extern void (vvv);
537 #define SymX(vvv) /**/
541 RTS_POSIX_ONLY_SYMBOLS
542 RTS_MINGW_ONLY_SYMBOLS
543 RTS_CYGWIN_ONLY_SYMBOLS
544 RTS_DARWIN_ONLY_SYMBOLS
548 #ifdef LEADING_UNDERSCORE
549 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
551 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
554 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
556 #define SymX(vvv) Sym(vvv)
558 static RtsSymbolVal rtsSyms[] = {
562 RTS_POSIX_ONLY_SYMBOLS
563 RTS_MINGW_ONLY_SYMBOLS
564 RTS_CYGWIN_ONLY_SYMBOLS
565 RTS_DARWIN_ONLY_SYMBOLS
566 { 0, 0 } /* sentinel */
569 /* -----------------------------------------------------------------------------
570 * Insert symbols into hash tables, checking for duplicates.
572 static void ghciInsertStrHashTable ( char* obj_name,
578 if (lookupHashTable(table, (StgWord)key) == NULL)
580 insertStrHashTable(table, (StgWord)key, data);
585 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
587 "whilst processing object file\n"
589 "This could be caused by:\n"
590 " * Loading two different object files which export the same symbol\n"
591 " * Specifying the same object file twice on the GHCi command line\n"
592 " * An incorrect `package.conf' entry, causing some object to be\n"
594 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
603 /* -----------------------------------------------------------------------------
604 * initialize the object linker
606 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
607 static void *dl_prog_handle;
615 symhash = allocStrHashTable();
617 /* populate the symbol table with stuff from the RTS */
618 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
619 ghciInsertStrHashTable("(GHCi built-in symbols)",
620 symhash, sym->lbl, sym->addr);
622 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
623 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
627 /* -----------------------------------------------------------------------------
628 * Add a DLL from which symbols may be found. In the ELF case, just
629 * do RTLD_GLOBAL-style add, so no further messing around needs to
630 * happen in order that symbols in the loaded .so are findable --
631 * lookupSymbol() will subsequently see them by dlsym on the program's
632 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
634 * In the PEi386 case, open the DLLs and put handles to them in a
635 * linked list. When looking for a symbol, try all handles in the
639 #if defined(OBJFORMAT_PEi386)
640 /* A record for storing handles into DLLs. */
645 struct _OpenedDLL* next;
650 /* A list thereof. */
651 static OpenedDLL* opened_dlls = NULL;
657 addDLL( char *dll_name )
659 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
663 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
665 /* dlopen failed; return a ptr to the error msg. */
667 if (errmsg == NULL) errmsg = "addDLL: unknown error";
674 # elif defined(OBJFORMAT_PEi386)
676 /* Add this DLL to the list of DLLs in which to search for symbols.
677 The path argument is ignored. */
682 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
684 /* See if we've already got it, and ignore if so. */
685 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
686 if (0 == strcmp(o_dll->name, dll_name))
690 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
691 sprintf(buf, "%s.DLL", dll_name);
692 instance = LoadLibrary(buf);
693 if (instance == NULL) {
694 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
695 instance = LoadLibrary(buf);
696 if (instance == NULL) {
699 /* LoadLibrary failed; return a ptr to the error msg. */
700 return "addDLL: unknown error";
705 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
706 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
707 strcpy(o_dll->name, dll_name);
708 o_dll->instance = instance;
709 o_dll->next = opened_dlls;
714 barf("addDLL: not implemented on this platform");
718 /* -----------------------------------------------------------------------------
719 * lookup a symbol in the hash table
722 lookupSymbol( char *lbl )
725 ASSERT(symhash != NULL);
726 val = lookupStrHashTable(symhash, lbl);
729 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
730 return dlsym(dl_prog_handle, lbl);
731 # elif defined(OBJFORMAT_PEi386)
734 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
735 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
737 /* HACK: if the name has an initial underscore, try stripping
738 it off & look that up first. I've yet to verify whether there's
739 a Rule that governs whether an initial '_' *should always* be
740 stripped off when mapping from import lib name to the DLL name.
742 sym = GetProcAddress(o_dll->instance, (lbl+1));
744 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
748 sym = GetProcAddress(o_dll->instance, lbl);
750 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
765 __attribute((unused))
767 lookupLocalSymbol( ObjectCode* oc, char *lbl )
770 val = lookupStrHashTable(oc->lochash, lbl);
780 /* -----------------------------------------------------------------------------
781 * Debugging aid: look in GHCi's object symbol tables for symbols
782 * within DELTA bytes of the specified address, and show their names.
785 void ghci_enquire ( char* addr );
787 void ghci_enquire ( char* addr )
792 const int DELTA = 64;
794 for (oc = objects; oc; oc = oc->next) {
795 for (i = 0; i < oc->n_symbols; i++) {
796 sym = oc->symbols[i];
797 if (sym == NULL) continue;
798 /* fprintf(stderr, "enquire %p %p\n", sym, oc->lochash); */
800 if (oc->lochash != NULL)
801 a = lookupStrHashTable(oc->lochash, sym);
803 a = lookupStrHashTable(symhash, sym);
805 /* fprintf(stderr, "ghci_enquire: can't find %s\n", sym); */
807 else if (addr-DELTA <= a && a <= addr+DELTA) {
808 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
815 #ifdef ia64_TARGET_ARCH
816 static unsigned int PLTSize(void);
819 /* -----------------------------------------------------------------------------
820 * Load an obj (populate the global symbol table, but don't resolve yet)
822 * Returns: 1 if ok, 0 on error.
825 loadObj( char *path )
837 /* fprintf(stderr, "loadObj %s\n", path ); */
839 /* Check that we haven't already loaded this object. Don't give up
840 at this stage; ocGetNames_* will barf later. */
844 for (o = objects; o; o = o->next) {
845 if (0 == strcmp(o->fileName, path))
851 "GHCi runtime linker: warning: looks like you're trying to load the\n"
852 "same object file twice:\n"
854 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
860 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
862 # if defined(OBJFORMAT_ELF)
863 oc->formatName = "ELF";
864 # elif defined(OBJFORMAT_PEi386)
865 oc->formatName = "PEi386";
866 # elif defined(OBJFORMAT_MACHO)
867 oc->formatName = "Mach-O";
870 barf("loadObj: not implemented on this platform");
874 if (r == -1) { return 0; }
876 /* sigh, strdup() isn't a POSIX function, so do it the long way */
877 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
878 strcpy(oc->fileName, path);
880 oc->fileSize = st.st_size;
883 oc->lochash = allocStrHashTable();
884 oc->proddables = NULL;
886 /* chain it onto the list of objects */
891 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
893 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
895 fd = open(path, O_RDONLY);
897 barf("loadObj: can't open `%s'", path);
899 pagesize = getpagesize();
901 #ifdef ia64_TARGET_ARCH
902 /* The PLT needs to be right before the object */
903 n = ROUND_UP(PLTSize(), pagesize);
904 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
905 if (oc->plt == MAP_FAILED)
906 barf("loadObj: can't allocate PLT");
909 map_addr = oc->plt + n;
912 n = ROUND_UP(oc->fileSize, pagesize);
913 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
914 if (oc->image == MAP_FAILED)
915 barf("loadObj: can't map `%s'", path);
919 #else /* !USE_MMAP */
921 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
923 /* load the image into memory */
924 f = fopen(path, "rb");
926 barf("loadObj: can't read `%s'", path);
928 n = fread ( oc->image, 1, oc->fileSize, f );
929 if (n != oc->fileSize)
930 barf("loadObj: error whilst reading `%s'", path);
934 #endif /* USE_MMAP */
936 /* verify the in-memory image */
937 # if defined(OBJFORMAT_ELF)
938 r = ocVerifyImage_ELF ( oc );
939 # elif defined(OBJFORMAT_PEi386)
940 r = ocVerifyImage_PEi386 ( oc );
941 # elif defined(OBJFORMAT_MACHO)
942 r = ocVerifyImage_MachO ( oc );
944 barf("loadObj: no verify method");
946 if (!r) { return r; }
948 /* build the symbol list for this image */
949 # if defined(OBJFORMAT_ELF)
950 r = ocGetNames_ELF ( oc );
951 # elif defined(OBJFORMAT_PEi386)
952 r = ocGetNames_PEi386 ( oc );
953 # elif defined(OBJFORMAT_MACHO)
954 r = ocGetNames_MachO ( oc );
956 barf("loadObj: no getNames method");
958 if (!r) { return r; }
960 /* loaded, but not resolved yet */
961 oc->status = OBJECT_LOADED;
966 /* -----------------------------------------------------------------------------
967 * resolve all the currently unlinked objects in memory
969 * Returns: 1 if ok, 0 on error.
977 for (oc = objects; oc; oc = oc->next) {
978 if (oc->status != OBJECT_RESOLVED) {
979 # if defined(OBJFORMAT_ELF)
980 r = ocResolve_ELF ( oc );
981 # elif defined(OBJFORMAT_PEi386)
982 r = ocResolve_PEi386 ( oc );
983 # elif defined(OBJFORMAT_MACHO)
984 r = ocResolve_MachO ( oc );
986 barf("resolveObjs: not implemented on this platform");
988 if (!r) { return r; }
989 oc->status = OBJECT_RESOLVED;
995 /* -----------------------------------------------------------------------------
996 * delete an object from the pool
999 unloadObj( char *path )
1001 ObjectCode *oc, *prev;
1003 ASSERT(symhash != NULL);
1004 ASSERT(objects != NULL);
1007 for (oc = objects; oc; prev = oc, oc = oc->next) {
1008 if (!strcmp(oc->fileName,path)) {
1010 /* Remove all the mappings for the symbols within this
1015 for (i = 0; i < oc->n_symbols; i++) {
1016 if (oc->symbols[i] != NULL) {
1017 removeStrHashTable(symhash, oc->symbols[i], NULL);
1025 prev->next = oc->next;
1028 /* We're going to leave this in place, in case there are
1029 any pointers from the heap into it: */
1030 /* free(oc->image); */
1034 /* The local hash table should have been freed at the end
1035 of the ocResolve_ call on it. */
1036 ASSERT(oc->lochash == NULL);
1042 belch("unloadObj: can't find `%s' to unload", path);
1046 /* -----------------------------------------------------------------------------
1047 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1048 * which may be prodded during relocation, and abort if we try and write
1049 * outside any of these.
1051 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1054 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1055 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1059 pb->next = oc->proddables;
1060 oc->proddables = pb;
1063 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1066 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1067 char* s = (char*)(pb->start);
1068 char* e = s + pb->size - 1;
1069 char* a = (char*)addr;
1070 /* Assumes that the biggest fixup involves a 4-byte write. This
1071 probably needs to be changed to 8 (ie, +7) on 64-bit
1073 if (a >= s && (a+3) <= e) return;
1075 barf("checkProddableBlock: invalid fixup in runtime linker");
1078 /* -----------------------------------------------------------------------------
1079 * Section management.
1081 static void addSection ( ObjectCode* oc, SectionKind kind,
1082 void* start, void* end )
1084 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1088 s->next = oc->sections;
1091 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1092 start, ((char*)end)-1, end - start + 1, kind );
1098 /* --------------------------------------------------------------------------
1099 * PEi386 specifics (Win32 targets)
1100 * ------------------------------------------------------------------------*/
1102 /* The information for this linker comes from
1103 Microsoft Portable Executable
1104 and Common Object File Format Specification
1105 revision 5.1 January 1998
1106 which SimonM says comes from the MS Developer Network CDs.
1108 It can be found there (on older CDs), but can also be found
1111 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1113 (this is Rev 6.0 from February 1999).
1115 Things move, so if that fails, try searching for it via
1117 http://www.google.com/search?q=PE+COFF+specification
1119 The ultimate reference for the PE format is the Winnt.h
1120 header file that comes with the Platform SDKs; as always,
1121 implementations will drift wrt their documentation.
1123 A good background article on the PE format is Matt Pietrek's
1124 March 1994 article in Microsoft System Journal (MSJ)
1125 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1126 Win32 Portable Executable File Format." The info in there
1127 has recently been updated in a two part article in
1128 MSDN magazine, issues Feb and March 2002,
1129 "Inside Windows: An In-Depth Look into the Win32 Portable
1130 Executable File Format"
1132 John Levine's book "Linkers and Loaders" contains useful
1137 #if defined(OBJFORMAT_PEi386)
1141 typedef unsigned char UChar;
1142 typedef unsigned short UInt16;
1143 typedef unsigned int UInt32;
1150 UInt16 NumberOfSections;
1151 UInt32 TimeDateStamp;
1152 UInt32 PointerToSymbolTable;
1153 UInt32 NumberOfSymbols;
1154 UInt16 SizeOfOptionalHeader;
1155 UInt16 Characteristics;
1159 #define sizeof_COFF_header 20
1166 UInt32 VirtualAddress;
1167 UInt32 SizeOfRawData;
1168 UInt32 PointerToRawData;
1169 UInt32 PointerToRelocations;
1170 UInt32 PointerToLinenumbers;
1171 UInt16 NumberOfRelocations;
1172 UInt16 NumberOfLineNumbers;
1173 UInt32 Characteristics;
1177 #define sizeof_COFF_section 40
1184 UInt16 SectionNumber;
1187 UChar NumberOfAuxSymbols;
1191 #define sizeof_COFF_symbol 18
1196 UInt32 VirtualAddress;
1197 UInt32 SymbolTableIndex;
1202 #define sizeof_COFF_reloc 10
1205 /* From PE spec doc, section 3.3.2 */
1206 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1207 windows.h -- for the same purpose, but I want to know what I'm
1209 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1210 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1211 #define MYIMAGE_FILE_DLL 0x2000
1212 #define MYIMAGE_FILE_SYSTEM 0x1000
1213 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1214 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1215 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1217 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1218 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1219 #define MYIMAGE_SYM_CLASS_STATIC 3
1220 #define MYIMAGE_SYM_UNDEFINED 0
1222 /* From PE spec doc, section 4.1 */
1223 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1224 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1225 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1227 /* From PE spec doc, section 5.2.1 */
1228 #define MYIMAGE_REL_I386_DIR32 0x0006
1229 #define MYIMAGE_REL_I386_REL32 0x0014
1232 /* We use myindex to calculate array addresses, rather than
1233 simply doing the normal subscript thing. That's because
1234 some of the above structs have sizes which are not
1235 a whole number of words. GCC rounds their sizes up to a
1236 whole number of words, which means that the address calcs
1237 arising from using normal C indexing or pointer arithmetic
1238 are just plain wrong. Sigh.
1241 myindex ( int scale, void* base, int index )
1244 ((UChar*)base) + scale * index;
1249 printName ( UChar* name, UChar* strtab )
1251 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1252 UInt32 strtab_offset = * (UInt32*)(name+4);
1253 fprintf ( stderr, "%s", strtab + strtab_offset );
1256 for (i = 0; i < 8; i++) {
1257 if (name[i] == 0) break;
1258 fprintf ( stderr, "%c", name[i] );
1265 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1267 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1268 UInt32 strtab_offset = * (UInt32*)(name+4);
1269 strncpy ( dst, strtab+strtab_offset, dstSize );
1275 if (name[i] == 0) break;
1285 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1288 /* If the string is longer than 8 bytes, look in the
1289 string table for it -- this will be correctly zero terminated.
1291 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1292 UInt32 strtab_offset = * (UInt32*)(name+4);
1293 return ((UChar*)strtab) + strtab_offset;
1295 /* Otherwise, if shorter than 8 bytes, return the original,
1296 which by defn is correctly terminated.
1298 if (name[7]==0) return name;
1299 /* The annoying case: 8 bytes. Copy into a temporary
1300 (which is never freed ...)
1302 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1304 strncpy(newstr,name,8);
1310 /* Just compares the short names (first 8 chars) */
1311 static COFF_section *
1312 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1316 = (COFF_header*)(oc->image);
1317 COFF_section* sectab
1319 ((UChar*)(oc->image))
1320 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1322 for (i = 0; i < hdr->NumberOfSections; i++) {
1325 COFF_section* section_i
1327 myindex ( sizeof_COFF_section, sectab, i );
1328 n1 = (UChar*) &(section_i->Name);
1330 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1331 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1332 n1[6]==n2[6] && n1[7]==n2[7])
1341 zapTrailingAtSign ( UChar* sym )
1343 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1345 if (sym[0] == 0) return;
1347 while (sym[i] != 0) i++;
1350 while (j > 0 && my_isdigit(sym[j])) j--;
1351 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1357 ocVerifyImage_PEi386 ( ObjectCode* oc )
1362 COFF_section* sectab;
1363 COFF_symbol* symtab;
1365 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1366 hdr = (COFF_header*)(oc->image);
1367 sectab = (COFF_section*) (
1368 ((UChar*)(oc->image))
1369 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1371 symtab = (COFF_symbol*) (
1372 ((UChar*)(oc->image))
1373 + hdr->PointerToSymbolTable
1375 strtab = ((UChar*)symtab)
1376 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1378 if (hdr->Machine != 0x14c) {
1379 belch("Not x86 PEi386");
1382 if (hdr->SizeOfOptionalHeader != 0) {
1383 belch("PEi386 with nonempty optional header");
1386 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1387 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1388 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1389 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1390 belch("Not a PEi386 object file");
1393 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1394 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1395 belch("Invalid PEi386 word size or endiannness: %d",
1396 (int)(hdr->Characteristics));
1399 /* If the string table size is way crazy, this might indicate that
1400 there are more than 64k relocations, despite claims to the
1401 contrary. Hence this test. */
1402 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1404 if ( (*(UInt32*)strtab) > 600000 ) {
1405 /* Note that 600k has no special significance other than being
1406 big enough to handle the almost-2MB-sized lumps that
1407 constitute HSwin32*.o. */
1408 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1413 /* No further verification after this point; only debug printing. */
1415 IF_DEBUG(linker, i=1);
1416 if (i == 0) return 1;
1419 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1421 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1423 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1425 fprintf ( stderr, "\n" );
1427 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1429 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1431 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1433 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1435 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1437 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1439 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1441 /* Print the section table. */
1442 fprintf ( stderr, "\n" );
1443 for (i = 0; i < hdr->NumberOfSections; i++) {
1445 COFF_section* sectab_i
1447 myindex ( sizeof_COFF_section, sectab, i );
1454 printName ( sectab_i->Name, strtab );
1464 sectab_i->VirtualSize,
1465 sectab_i->VirtualAddress,
1466 sectab_i->SizeOfRawData,
1467 sectab_i->PointerToRawData,
1468 sectab_i->NumberOfRelocations,
1469 sectab_i->PointerToRelocations,
1470 sectab_i->PointerToRawData
1472 reltab = (COFF_reloc*) (
1473 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1476 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1477 /* If the relocation field (a short) has overflowed, the
1478 * real count can be found in the first reloc entry.
1480 * See Section 4.1 (last para) of the PE spec (rev6.0).
1482 COFF_reloc* rel = (COFF_reloc*)
1483 myindex ( sizeof_COFF_reloc, reltab, 0 );
1484 noRelocs = rel->VirtualAddress;
1487 noRelocs = sectab_i->NumberOfRelocations;
1491 for (; j < noRelocs; j++) {
1493 COFF_reloc* rel = (COFF_reloc*)
1494 myindex ( sizeof_COFF_reloc, reltab, j );
1496 " type 0x%-4x vaddr 0x%-8x name `",
1498 rel->VirtualAddress );
1499 sym = (COFF_symbol*)
1500 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1501 /* Hmm..mysterious looking offset - what's it for? SOF */
1502 printName ( sym->Name, strtab -10 );
1503 fprintf ( stderr, "'\n" );
1506 fprintf ( stderr, "\n" );
1508 fprintf ( stderr, "\n" );
1509 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1510 fprintf ( stderr, "---START of string table---\n");
1511 for (i = 4; i < *(Int32*)strtab; i++) {
1513 fprintf ( stderr, "\n"); else
1514 fprintf( stderr, "%c", strtab[i] );
1516 fprintf ( stderr, "--- END of string table---\n");
1518 fprintf ( stderr, "\n" );
1521 COFF_symbol* symtab_i;
1522 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1523 symtab_i = (COFF_symbol*)
1524 myindex ( sizeof_COFF_symbol, symtab, i );
1530 printName ( symtab_i->Name, strtab );
1539 (Int32)(symtab_i->SectionNumber),
1540 (UInt32)symtab_i->Type,
1541 (UInt32)symtab_i->StorageClass,
1542 (UInt32)symtab_i->NumberOfAuxSymbols
1544 i += symtab_i->NumberOfAuxSymbols;
1548 fprintf ( stderr, "\n" );
1554 ocGetNames_PEi386 ( ObjectCode* oc )
1557 COFF_section* sectab;
1558 COFF_symbol* symtab;
1565 hdr = (COFF_header*)(oc->image);
1566 sectab = (COFF_section*) (
1567 ((UChar*)(oc->image))
1568 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1570 symtab = (COFF_symbol*) (
1571 ((UChar*)(oc->image))
1572 + hdr->PointerToSymbolTable
1574 strtab = ((UChar*)(oc->image))
1575 + hdr->PointerToSymbolTable
1576 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1578 /* Allocate space for any (local, anonymous) .bss sections. */
1580 for (i = 0; i < hdr->NumberOfSections; i++) {
1582 COFF_section* sectab_i
1584 myindex ( sizeof_COFF_section, sectab, i );
1585 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1586 if (sectab_i->VirtualSize == 0) continue;
1587 /* This is a non-empty .bss section. Allocate zeroed space for
1588 it, and set its PointerToRawData field such that oc->image +
1589 PointerToRawData == addr_of_zeroed_space. */
1590 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1591 "ocGetNames_PEi386(anonymous bss)");
1592 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1593 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1594 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1597 /* Copy section information into the ObjectCode. */
1599 for (i = 0; i < hdr->NumberOfSections; i++) {
1605 = SECTIONKIND_OTHER;
1606 COFF_section* sectab_i
1608 myindex ( sizeof_COFF_section, sectab, i );
1609 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1612 /* I'm sure this is the Right Way to do it. However, the
1613 alternative of testing the sectab_i->Name field seems to
1614 work ok with Cygwin.
1616 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1617 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1618 kind = SECTIONKIND_CODE_OR_RODATA;
1621 if (0==strcmp(".text",sectab_i->Name) ||
1622 0==strcmp(".rodata",sectab_i->Name))
1623 kind = SECTIONKIND_CODE_OR_RODATA;
1624 if (0==strcmp(".data",sectab_i->Name) ||
1625 0==strcmp(".bss",sectab_i->Name))
1626 kind = SECTIONKIND_RWDATA;
1628 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1629 sz = sectab_i->SizeOfRawData;
1630 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1632 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1633 end = start + sz - 1;
1635 if (kind == SECTIONKIND_OTHER
1636 /* Ignore sections called which contain stabs debugging
1638 && 0 != strcmp(".stab", sectab_i->Name)
1639 && 0 != strcmp(".stabstr", sectab_i->Name)
1641 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1645 if (kind != SECTIONKIND_OTHER && end >= start) {
1646 addSection(oc, kind, start, end);
1647 addProddableBlock(oc, start, end - start + 1);
1651 /* Copy exported symbols into the ObjectCode. */
1653 oc->n_symbols = hdr->NumberOfSymbols;
1654 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1655 "ocGetNames_PEi386(oc->symbols)");
1656 /* Call me paranoid; I don't care. */
1657 for (i = 0; i < oc->n_symbols; i++)
1658 oc->symbols[i] = NULL;
1662 COFF_symbol* symtab_i;
1663 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1664 symtab_i = (COFF_symbol*)
1665 myindex ( sizeof_COFF_symbol, symtab, i );
1669 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1670 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1671 /* This symbol is global and defined, viz, exported */
1672 /* for MYIMAGE_SYMCLASS_EXTERNAL
1673 && !MYIMAGE_SYM_UNDEFINED,
1674 the address of the symbol is:
1675 address of relevant section + offset in section
1677 COFF_section* sectabent
1678 = (COFF_section*) myindex ( sizeof_COFF_section,
1680 symtab_i->SectionNumber-1 );
1681 addr = ((UChar*)(oc->image))
1682 + (sectabent->PointerToRawData
1686 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1687 && symtab_i->Value > 0) {
1688 /* This symbol isn't in any section at all, ie, global bss.
1689 Allocate zeroed space for it. */
1690 addr = stgCallocBytes(1, symtab_i->Value,
1691 "ocGetNames_PEi386(non-anonymous bss)");
1692 addSection(oc, SECTIONKIND_RWDATA, addr,
1693 ((UChar*)addr) + symtab_i->Value - 1);
1694 addProddableBlock(oc, addr, symtab_i->Value);
1695 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1698 if (addr != NULL ) {
1699 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1700 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1701 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1702 ASSERT(i >= 0 && i < oc->n_symbols);
1703 /* cstring_from_COFF_symbol_name always succeeds. */
1704 oc->symbols[i] = sname;
1705 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1709 "IGNORING symbol %d\n"
1713 printName ( symtab_i->Name, strtab );
1722 (Int32)(symtab_i->SectionNumber),
1723 (UInt32)symtab_i->Type,
1724 (UInt32)symtab_i->StorageClass,
1725 (UInt32)symtab_i->NumberOfAuxSymbols
1730 i += symtab_i->NumberOfAuxSymbols;
1739 ocResolve_PEi386 ( ObjectCode* oc )
1742 COFF_section* sectab;
1743 COFF_symbol* symtab;
1753 /* ToDo: should be variable-sized? But is at least safe in the
1754 sense of buffer-overrun-proof. */
1756 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1758 hdr = (COFF_header*)(oc->image);
1759 sectab = (COFF_section*) (
1760 ((UChar*)(oc->image))
1761 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1763 symtab = (COFF_symbol*) (
1764 ((UChar*)(oc->image))
1765 + hdr->PointerToSymbolTable
1767 strtab = ((UChar*)(oc->image))
1768 + hdr->PointerToSymbolTable
1769 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1771 for (i = 0; i < hdr->NumberOfSections; i++) {
1772 COFF_section* sectab_i
1774 myindex ( sizeof_COFF_section, sectab, i );
1777 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1780 /* Ignore sections called which contain stabs debugging
1782 if (0 == strcmp(".stab", sectab_i->Name)
1783 || 0 == strcmp(".stabstr", sectab_i->Name))
1786 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1787 /* If the relocation field (a short) has overflowed, the
1788 * real count can be found in the first reloc entry.
1790 * See Section 4.1 (last para) of the PE spec (rev6.0).
1792 COFF_reloc* rel = (COFF_reloc*)
1793 myindex ( sizeof_COFF_reloc, reltab, 0 );
1794 noRelocs = rel->VirtualAddress;
1795 fprintf(stderr, "Overflown relocs: %u\n", noRelocs);
1798 noRelocs = sectab_i->NumberOfRelocations;
1803 for (; j < noRelocs; j++) {
1805 COFF_reloc* reltab_j
1807 myindex ( sizeof_COFF_reloc, reltab, j );
1809 /* the location to patch */
1811 ((UChar*)(oc->image))
1812 + (sectab_i->PointerToRawData
1813 + reltab_j->VirtualAddress
1814 - sectab_i->VirtualAddress )
1816 /* the existing contents of pP */
1818 /* the symbol to connect to */
1819 sym = (COFF_symbol*)
1820 myindex ( sizeof_COFF_symbol,
1821 symtab, reltab_j->SymbolTableIndex );
1824 "reloc sec %2d num %3d: type 0x%-4x "
1825 "vaddr 0x%-8x name `",
1827 (UInt32)reltab_j->Type,
1828 reltab_j->VirtualAddress );
1829 printName ( sym->Name, strtab );
1830 fprintf ( stderr, "'\n" ));
1832 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1833 COFF_section* section_sym
1834 = findPEi386SectionCalled ( oc, sym->Name );
1836 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1839 S = ((UInt32)(oc->image))
1840 + (section_sym->PointerToRawData
1843 copyName ( sym->Name, strtab, symbol, 1000-1 );
1844 (void*)S = lookupLocalSymbol( oc, symbol );
1845 if ((void*)S != NULL) goto foundit;
1846 (void*)S = lookupSymbol( symbol );
1847 if ((void*)S != NULL) goto foundit;
1848 zapTrailingAtSign ( symbol );
1849 (void*)S = lookupLocalSymbol( oc, symbol );
1850 if ((void*)S != NULL) goto foundit;
1851 (void*)S = lookupSymbol( symbol );
1852 if ((void*)S != NULL) goto foundit;
1853 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1857 checkProddableBlock(oc, pP);
1858 switch (reltab_j->Type) {
1859 case MYIMAGE_REL_I386_DIR32:
1862 case MYIMAGE_REL_I386_REL32:
1863 /* Tricky. We have to insert a displacement at
1864 pP which, when added to the PC for the _next_
1865 insn, gives the address of the target (S).
1866 Problem is to know the address of the next insn
1867 when we only know pP. We assume that this
1868 literal field is always the last in the insn,
1869 so that the address of the next insn is pP+4
1870 -- hence the constant 4.
1871 Also I don't know if A should be added, but so
1872 far it has always been zero.
1875 *pP = S - ((UInt32)pP) - 4;
1878 belch("%s: unhandled PEi386 relocation type %d",
1879 oc->fileName, reltab_j->Type);
1886 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1890 #endif /* defined(OBJFORMAT_PEi386) */
1893 /* --------------------------------------------------------------------------
1895 * ------------------------------------------------------------------------*/
1897 #if defined(OBJFORMAT_ELF)
1902 #if defined(sparc_TARGET_ARCH)
1903 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1904 #elif defined(i386_TARGET_ARCH)
1905 # define ELF_TARGET_386 /* Used inside <elf.h> */
1906 #elif defined (ia64_TARGET_ARCH)
1907 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
1909 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
1910 # define ELF_NEED_GOT /* needs Global Offset Table */
1911 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
1917 * Define a set of types which can be used for both ELF32 and ELF64
1921 #define ELFCLASS ELFCLASS64
1922 #define Elf_Addr Elf64_Addr
1923 #define Elf_Word Elf64_Word
1924 #define Elf_Sword Elf64_Sword
1925 #define Elf_Ehdr Elf64_Ehdr
1926 #define Elf_Phdr Elf64_Phdr
1927 #define Elf_Shdr Elf64_Shdr
1928 #define Elf_Sym Elf64_Sym
1929 #define Elf_Rel Elf64_Rel
1930 #define Elf_Rela Elf64_Rela
1931 #define ELF_ST_TYPE ELF64_ST_TYPE
1932 #define ELF_ST_BIND ELF64_ST_BIND
1933 #define ELF_R_TYPE ELF64_R_TYPE
1934 #define ELF_R_SYM ELF64_R_SYM
1936 #define ELFCLASS ELFCLASS32
1937 #define Elf_Addr Elf32_Addr
1938 #define Elf_Word Elf32_Word
1939 #define Elf_Sword Elf32_Sword
1940 #define Elf_Ehdr Elf32_Ehdr
1941 #define Elf_Phdr Elf32_Phdr
1942 #define Elf_Shdr Elf32_Shdr
1943 #define Elf_Sym Elf32_Sym
1944 #define Elf_Rel Elf32_Rel
1945 #define Elf_Rela Elf32_Rela
1946 #define ELF_ST_TYPE ELF32_ST_TYPE
1947 #define ELF_ST_BIND ELF32_ST_BIND
1948 #define ELF_R_TYPE ELF32_R_TYPE
1949 #define ELF_R_SYM ELF32_R_SYM
1954 * Functions to allocate entries in dynamic sections. Currently we simply
1955 * preallocate a large number, and we don't check if a entry for the given
1956 * target already exists (a linear search is too slow). Ideally these
1957 * entries would be associated with symbols.
1960 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
1961 #define GOT_SIZE 0x20000
1962 #define FUNCTION_TABLE_SIZE 0x10000
1963 #define PLT_SIZE 0x08000
1966 static Elf_Addr got[GOT_SIZE];
1967 static unsigned int gotIndex;
1968 static Elf_Addr gp_val = (Elf_Addr)got;
1971 allocateGOTEntry(Elf_Addr target)
1975 if (gotIndex >= GOT_SIZE)
1976 barf("Global offset table overflow");
1978 entry = &got[gotIndex++];
1980 return (Elf_Addr)entry;
1984 #ifdef ELF_FUNCTION_DESC
1990 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
1991 static unsigned int functionTableIndex;
1994 allocateFunctionDesc(Elf_Addr target)
1996 FunctionDesc *entry;
1998 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
1999 barf("Function table overflow");
2001 entry = &functionTable[functionTableIndex++];
2003 entry->gp = (Elf_Addr)gp_val;
2004 return (Elf_Addr)entry;
2008 copyFunctionDesc(Elf_Addr target)
2010 FunctionDesc *olddesc = (FunctionDesc *)target;
2011 FunctionDesc *newdesc;
2013 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2014 newdesc->gp = olddesc->gp;
2015 return (Elf_Addr)newdesc;
2020 #ifdef ia64_TARGET_ARCH
2021 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2022 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2024 static unsigned char plt_code[] =
2026 /* taken from binutils bfd/elfxx-ia64.c */
2027 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2028 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2029 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2030 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2031 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2032 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2035 /* If we can't get to the function descriptor via gp, take a local copy of it */
2036 #define PLT_RELOC(code, target) { \
2037 Elf64_Sxword rel_value = target - gp_val; \
2038 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2039 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2041 ia64_reloc_gprel22((Elf_Addr)code, target); \
2046 unsigned char code[sizeof(plt_code)];
2050 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2052 PLTEntry *plt = (PLTEntry *)oc->plt;
2055 if (oc->pltIndex >= PLT_SIZE)
2056 barf("Procedure table overflow");
2058 entry = &plt[oc->pltIndex++];
2059 memcpy(entry->code, plt_code, sizeof(entry->code));
2060 PLT_RELOC(entry->code, target);
2061 return (Elf_Addr)entry;
2067 return (PLT_SIZE * sizeof(PLTEntry));
2073 * Generic ELF functions
2077 findElfSection ( void* objImage, Elf_Word sh_type )
2079 char* ehdrC = (char*)objImage;
2080 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2081 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2082 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2086 for (i = 0; i < ehdr->e_shnum; i++) {
2087 if (shdr[i].sh_type == sh_type
2088 /* Ignore the section header's string table. */
2089 && i != ehdr->e_shstrndx
2090 /* Ignore string tables named .stabstr, as they contain
2092 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2094 ptr = ehdrC + shdr[i].sh_offset;
2101 #if defined(ia64_TARGET_ARCH)
2103 findElfSegment ( void* objImage, Elf_Addr vaddr )
2105 char* ehdrC = (char*)objImage;
2106 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2107 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2108 Elf_Addr segaddr = 0;
2111 for (i = 0; i < ehdr->e_phnum; i++) {
2112 segaddr = phdr[i].p_vaddr;
2113 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2121 ocVerifyImage_ELF ( ObjectCode* oc )
2125 int i, j, nent, nstrtab, nsymtabs;
2129 char* ehdrC = (char*)(oc->image);
2130 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2132 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2133 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2134 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2135 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2136 belch("%s: not an ELF object", oc->fileName);
2140 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2141 belch("%s: unsupported ELF format", oc->fileName);
2145 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2146 IF_DEBUG(linker,belch( "Is little-endian" ));
2148 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2149 IF_DEBUG(linker,belch( "Is big-endian" ));
2151 belch("%s: unknown endiannness", oc->fileName);
2155 if (ehdr->e_type != ET_REL) {
2156 belch("%s: not a relocatable object (.o) file", oc->fileName);
2159 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2161 IF_DEBUG(linker,belch( "Architecture is " ));
2162 switch (ehdr->e_machine) {
2163 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2164 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2166 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2168 default: IF_DEBUG(linker,belch( "unknown" ));
2169 belch("%s: unknown architecture", oc->fileName);
2173 IF_DEBUG(linker,belch(
2174 "\nSection header table: start %d, n_entries %d, ent_size %d",
2175 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2177 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2179 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2181 if (ehdr->e_shstrndx == SHN_UNDEF) {
2182 belch("%s: no section header string table", oc->fileName);
2185 IF_DEBUG(linker,belch( "Section header string table is section %d",
2187 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2190 for (i = 0; i < ehdr->e_shnum; i++) {
2191 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2192 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2193 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2194 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2195 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2196 ehdrC + shdr[i].sh_offset,
2197 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2199 if (shdr[i].sh_type == SHT_REL) {
2200 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2201 } else if (shdr[i].sh_type == SHT_RELA) {
2202 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2204 IF_DEBUG(linker,fprintf(stderr," "));
2207 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2211 IF_DEBUG(linker,belch( "\nString tables" ));
2214 for (i = 0; i < ehdr->e_shnum; i++) {
2215 if (shdr[i].sh_type == SHT_STRTAB
2216 /* Ignore the section header's string table. */
2217 && i != ehdr->e_shstrndx
2218 /* Ignore string tables named .stabstr, as they contain
2220 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2222 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2223 strtab = ehdrC + shdr[i].sh_offset;
2228 belch("%s: no string tables, or too many", oc->fileName);
2233 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2234 for (i = 0; i < ehdr->e_shnum; i++) {
2235 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2236 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2238 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2239 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2240 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2242 shdr[i].sh_size % sizeof(Elf_Sym)
2244 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2245 belch("%s: non-integral number of symbol table entries", oc->fileName);
2248 for (j = 0; j < nent; j++) {
2249 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2250 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2251 (int)stab[j].st_shndx,
2252 (int)stab[j].st_size,
2253 (char*)stab[j].st_value ));
2255 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2256 switch (ELF_ST_TYPE(stab[j].st_info)) {
2257 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2258 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2259 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2260 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2261 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2262 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2264 IF_DEBUG(linker,fprintf(stderr, " " ));
2266 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2267 switch (ELF_ST_BIND(stab[j].st_info)) {
2268 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2269 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2270 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2271 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2273 IF_DEBUG(linker,fprintf(stderr, " " ));
2275 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2279 if (nsymtabs == 0) {
2280 belch("%s: didn't find any symbol tables", oc->fileName);
2289 ocGetNames_ELF ( ObjectCode* oc )
2294 char* ehdrC = (char*)(oc->image);
2295 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2296 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2297 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2299 ASSERT(symhash != NULL);
2302 belch("%s: no strtab", oc->fileName);
2307 for (i = 0; i < ehdr->e_shnum; i++) {
2308 /* Figure out what kind of section it is. Logic derived from
2309 Figure 1.14 ("Special Sections") of the ELF document
2310 ("Portable Formats Specification, Version 1.1"). */
2311 Elf_Shdr hdr = shdr[i];
2312 SectionKind kind = SECTIONKIND_OTHER;
2315 if (hdr.sh_type == SHT_PROGBITS
2316 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2317 /* .text-style section */
2318 kind = SECTIONKIND_CODE_OR_RODATA;
2321 if (hdr.sh_type == SHT_PROGBITS
2322 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2323 /* .data-style section */
2324 kind = SECTIONKIND_RWDATA;
2327 if (hdr.sh_type == SHT_PROGBITS
2328 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2329 /* .rodata-style section */
2330 kind = SECTIONKIND_CODE_OR_RODATA;
2333 if (hdr.sh_type == SHT_NOBITS
2334 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2335 /* .bss-style section */
2336 kind = SECTIONKIND_RWDATA;
2340 if (is_bss && shdr[i].sh_size > 0) {
2341 /* This is a non-empty .bss section. Allocate zeroed space for
2342 it, and set its .sh_offset field such that
2343 ehdrC + .sh_offset == addr_of_zeroed_space. */
2344 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2345 "ocGetNames_ELF(BSS)");
2346 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2348 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2349 zspace, shdr[i].sh_size);
2353 /* fill in the section info */
2354 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2355 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2356 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2357 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2360 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2362 /* copy stuff into this module's object symbol table */
2363 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2364 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2366 oc->n_symbols = nent;
2367 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2368 "ocGetNames_ELF(oc->symbols)");
2370 for (j = 0; j < nent; j++) {
2372 char isLocal = FALSE; /* avoids uninit-var warning */
2374 char* nm = strtab + stab[j].st_name;
2375 int secno = stab[j].st_shndx;
2377 /* Figure out if we want to add it; if so, set ad to its
2378 address. Otherwise leave ad == NULL. */
2380 if (secno == SHN_COMMON) {
2382 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2384 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2385 stab[j].st_size, nm);
2387 /* Pointless to do addProddableBlock() for this area,
2388 since the linker should never poke around in it. */
2391 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2392 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2394 /* and not an undefined symbol */
2395 && stab[j].st_shndx != SHN_UNDEF
2396 /* and not in a "special section" */
2397 && stab[j].st_shndx < SHN_LORESERVE
2399 /* and it's a not a section or string table or anything silly */
2400 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2401 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2402 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2405 /* Section 0 is the undefined section, hence > and not >=. */
2406 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2408 if (shdr[secno].sh_type == SHT_NOBITS) {
2409 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2410 stab[j].st_size, stab[j].st_value, nm);
2413 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2414 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2417 #ifdef ELF_FUNCTION_DESC
2418 /* dlsym() and the initialisation table both give us function
2419 * descriptors, so to be consistent we store function descriptors
2420 * in the symbol table */
2421 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2422 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2424 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2425 ad, oc->fileName, nm ));
2430 /* And the decision is ... */
2434 oc->symbols[j] = nm;
2437 /* Ignore entirely. */
2439 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2443 IF_DEBUG(linker,belch( "skipping `%s'",
2444 strtab + stab[j].st_name ));
2447 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2448 (int)ELF_ST_BIND(stab[j].st_info),
2449 (int)ELF_ST_TYPE(stab[j].st_info),
2450 (int)stab[j].st_shndx,
2451 strtab + stab[j].st_name
2454 oc->symbols[j] = NULL;
2463 /* Do ELF relocations which lack an explicit addend. All x86-linux
2464 relocations appear to be of this form. */
2466 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2467 Elf_Shdr* shdr, int shnum,
2468 Elf_Sym* stab, char* strtab )
2473 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2474 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2475 int target_shndx = shdr[shnum].sh_info;
2476 int symtab_shndx = shdr[shnum].sh_link;
2478 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2479 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2480 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2481 target_shndx, symtab_shndx ));
2483 for (j = 0; j < nent; j++) {
2484 Elf_Addr offset = rtab[j].r_offset;
2485 Elf_Addr info = rtab[j].r_info;
2487 Elf_Addr P = ((Elf_Addr)targ) + offset;
2488 Elf_Word* pP = (Elf_Word*)P;
2493 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2494 j, (void*)offset, (void*)info ));
2496 IF_DEBUG(linker,belch( " ZERO" ));
2499 Elf_Sym sym = stab[ELF_R_SYM(info)];
2500 /* First see if it is a local symbol. */
2501 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2502 /* Yes, so we can get the address directly from the ELF symbol
2504 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2506 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2507 + stab[ELF_R_SYM(info)].st_value);
2510 /* No, so look up the name in our global table. */
2511 symbol = strtab + sym.st_name;
2512 (void*)S = lookupSymbol( symbol );
2515 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2518 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2521 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2522 (void*)P, (void*)S, (void*)A ));
2523 checkProddableBlock ( oc, pP );
2527 switch (ELF_R_TYPE(info)) {
2528 # ifdef i386_TARGET_ARCH
2529 case R_386_32: *pP = value; break;
2530 case R_386_PC32: *pP = value - P; break;
2533 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2534 oc->fileName, ELF_R_TYPE(info));
2542 /* Do ELF relocations for which explicit addends are supplied.
2543 sparc-solaris relocations appear to be of this form. */
2545 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2546 Elf_Shdr* shdr, int shnum,
2547 Elf_Sym* stab, char* strtab )
2552 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2553 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2554 int target_shndx = shdr[shnum].sh_info;
2555 int symtab_shndx = shdr[shnum].sh_link;
2557 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2558 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2559 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2560 target_shndx, symtab_shndx ));
2562 for (j = 0; j < nent; j++) {
2563 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
2564 /* This #ifdef only serves to avoid unused-var warnings. */
2565 Elf_Addr offset = rtab[j].r_offset;
2566 Elf_Addr P = targ + offset;
2568 Elf_Addr info = rtab[j].r_info;
2569 Elf_Addr A = rtab[j].r_addend;
2572 # if defined(sparc_TARGET_ARCH)
2573 Elf_Word* pP = (Elf_Word*)P;
2575 # elif defined(ia64_TARGET_ARCH)
2576 Elf64_Xword *pP = (Elf64_Xword *)P;
2580 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2581 j, (void*)offset, (void*)info,
2584 IF_DEBUG(linker,belch( " ZERO" ));
2587 Elf_Sym sym = stab[ELF_R_SYM(info)];
2588 /* First see if it is a local symbol. */
2589 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2590 /* Yes, so we can get the address directly from the ELF symbol
2592 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2594 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2595 + stab[ELF_R_SYM(info)].st_value);
2596 #ifdef ELF_FUNCTION_DESC
2597 /* Make a function descriptor for this function */
2598 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2599 S = allocateFunctionDesc(S + A);
2604 /* No, so look up the name in our global table. */
2605 symbol = strtab + sym.st_name;
2606 (void*)S = lookupSymbol( symbol );
2608 #ifdef ELF_FUNCTION_DESC
2609 /* If a function, already a function descriptor - we would
2610 have to copy it to add an offset. */
2611 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC)
2616 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2619 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2622 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2623 (void*)P, (void*)S, (void*)A ));
2624 /* checkProddableBlock ( oc, (void*)P ); */
2628 switch (ELF_R_TYPE(info)) {
2629 # if defined(sparc_TARGET_ARCH)
2630 case R_SPARC_WDISP30:
2631 w1 = *pP & 0xC0000000;
2632 w2 = (Elf_Word)((value - P) >> 2);
2633 ASSERT((w2 & 0xC0000000) == 0);
2638 w1 = *pP & 0xFFC00000;
2639 w2 = (Elf_Word)(value >> 10);
2640 ASSERT((w2 & 0xFFC00000) == 0);
2646 w2 = (Elf_Word)(value & 0x3FF);
2647 ASSERT((w2 & ~0x3FF) == 0);
2651 /* According to the Sun documentation:
2653 This relocation type resembles R_SPARC_32, except it refers to an
2654 unaligned word. That is, the word to be relocated must be treated
2655 as four separate bytes with arbitrary alignment, not as a word
2656 aligned according to the architecture requirements.
2658 (JRS: which means that freeloading on the R_SPARC_32 case
2659 is probably wrong, but hey ...)
2663 w2 = (Elf_Word)value;
2666 # elif defined(ia64_TARGET_ARCH)
2667 case R_IA64_DIR64LSB:
2668 case R_IA64_FPTR64LSB:
2671 case R_IA64_SEGREL64LSB:
2672 addr = findElfSegment(ehdrC, value);
2675 case R_IA64_GPREL22:
2676 ia64_reloc_gprel22(P, value);
2678 case R_IA64_LTOFF22:
2679 case R_IA64_LTOFF_FPTR22:
2680 addr = allocateGOTEntry(value);
2681 ia64_reloc_gprel22(P, addr);
2683 case R_IA64_PCREL21B:
2684 ia64_reloc_pcrel21(P, S, oc);
2688 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2689 oc->fileName, ELF_R_TYPE(info));
2698 ocResolve_ELF ( ObjectCode* oc )
2702 Elf_Sym* stab = NULL;
2703 char* ehdrC = (char*)(oc->image);
2704 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2705 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2706 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2708 /* first find "the" symbol table */
2709 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2711 /* also go find the string table */
2712 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2714 if (stab == NULL || strtab == NULL) {
2715 belch("%s: can't find string or symbol table", oc->fileName);
2719 /* Process the relocation sections. */
2720 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2722 /* Skip sections called ".rel.stab". These appear to contain
2723 relocation entries that, when done, make the stabs debugging
2724 info point at the right places. We ain't interested in all
2726 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2729 if (shdr[shnum].sh_type == SHT_REL ) {
2730 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2731 shnum, stab, strtab );
2735 if (shdr[shnum].sh_type == SHT_RELA) {
2736 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2737 shnum, stab, strtab );
2742 /* Free the local symbol table; we won't need it again. */
2743 freeHashTable(oc->lochash, NULL);
2751 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2752 * at the front. The following utility functions pack and unpack instructions, and
2753 * take care of the most common relocations.
2756 #ifdef ia64_TARGET_ARCH
2759 ia64_extract_instruction(Elf64_Xword *target)
2762 int slot = (Elf_Addr)target & 3;
2763 (Elf_Addr)target &= ~3;
2771 return ((w1 >> 5) & 0x1ffffffffff);
2773 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2777 barf("ia64_extract_instruction: invalid slot %p", target);
2782 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2784 int slot = (Elf_Addr)target & 3;
2785 (Elf_Addr)target &= ~3;
2790 *target |= value << 5;
2793 *target |= value << 46;
2794 *(target+1) |= value >> 18;
2797 *(target+1) |= value << 23;
2803 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2805 Elf64_Xword instruction;
2806 Elf64_Sxword rel_value;
2808 rel_value = value - gp_val;
2809 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2810 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2812 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2813 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2814 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2815 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2816 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2817 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2821 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2823 Elf64_Xword instruction;
2824 Elf64_Sxword rel_value;
2827 entry = allocatePLTEntry(value, oc);
2829 rel_value = (entry >> 4) - (target >> 4);
2830 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2831 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
2833 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2834 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
2835 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2836 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2843 /* --------------------------------------------------------------------------
2845 * ------------------------------------------------------------------------*/
2847 #if defined(OBJFORMAT_MACHO)
2850 Initial support for MachO linking on Darwin/MacOS X on PowerPC chips
2851 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
2853 I hereby formally apologize for the hackish nature of this code.
2854 Things that need to be done:
2855 *) get common symbols and .bss sections to work properly.
2856 Haskell modules seem to work, but C modules can cause problems
2857 *) implement ocVerifyImage_MachO
2858 *) add more sanity checks. The current code just has to segfault if there's a
2862 static int ocVerifyImage_MachO(ObjectCode* oc)
2864 // FIXME: do some verifying here
2868 static void resolveImports(
2870 struct symtab_command *symLC,
2871 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
2872 unsigned long *indirectSyms,
2873 struct nlist *nlist)
2877 for(i=0;i*4<sect->size;i++)
2879 // according to otool, reserved1 contains the first index into the indirect symbol table
2880 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
2881 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
2884 if((symbol->n_type & N_TYPE) == N_UNDF
2885 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
2886 addr = (void*) (symbol->n_value);
2888 addr = lookupSymbol(nm);
2891 fprintf(stderr, "not found: %s\n", nm);
2895 ((void**)(image + sect->offset))[i] = addr;
2899 static void relocateSection(char *image,
2900 struct symtab_command *symLC, struct nlist *nlist,
2901 struct section* sections, struct section *sect)
2903 struct relocation_info *relocs;
2906 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
2908 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
2912 relocs = (struct relocation_info*) (image + sect->reloff);
2916 if(relocs[i].r_address & R_SCATTERED)
2918 struct scattered_relocation_info *scat =
2919 (struct scattered_relocation_info*) &relocs[i];
2923 if(scat->r_length == 2 && scat->r_type == GENERIC_RELOC_VANILLA)
2925 unsigned long* word = (unsigned long*) (image + sect->offset + scat->r_address);
2927 *word = scat->r_value + sect->offset + ((long) image);
2931 continue; // FIXME: I hope it's OK to ignore all the others.
2935 struct relocation_info *reloc = &relocs[i];
2936 if(reloc->r_pcrel && !reloc->r_extern)
2940 && reloc->r_length == 2
2941 && reloc->r_type == GENERIC_RELOC_VANILLA)
2943 unsigned long* word = (unsigned long*) (image + sect->offset + reloc->r_address);
2945 if(!reloc->r_extern)
2948 sections[reloc->r_symbolnum-1].offset
2949 - sections[reloc->r_symbolnum-1].addr
2956 struct nlist *symbol = &nlist[reloc->r_symbolnum];
2957 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
2958 *word = lookupSymbol(nm);
2963 fprintf(stderr, "unknown reloc\n");
2970 static int ocGetNames_MachO(ObjectCode* oc)
2972 char *image = (char*) oc->image;
2973 struct mach_header *header = (struct mach_header*) image;
2974 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
2976 struct segment_command *segLC = NULL;
2977 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
2978 struct symtab_command *symLC = NULL;
2979 struct dysymtab_command *dsymLC = NULL;
2980 struct nlist *nlist;
2981 unsigned long commonSize = 0;
2982 char *commonStorage = NULL;
2983 unsigned long commonCounter;
2985 for(i=0;i<header->ncmds;i++)
2987 if(lc->cmd == LC_SEGMENT)
2988 segLC = (struct segment_command*) lc;
2989 else if(lc->cmd == LC_SYMTAB)
2990 symLC = (struct symtab_command*) lc;
2991 else if(lc->cmd == LC_DYSYMTAB)
2992 dsymLC = (struct dysymtab_command*) lc;
2993 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
2996 sections = (struct section*) (segLC+1);
2997 nlist = (struct nlist*) (image + symLC->symoff);
2999 for(i=0;i<segLC->nsects;i++)
3001 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3002 la_ptrs = §ions[i];
3003 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3004 nl_ptrs = §ions[i];
3006 // for now, only add __text and __const to the sections table
3007 else if(!strcmp(sections[i].sectname,"__text"))
3008 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3009 (void*) (image + sections[i].offset),
3010 (void*) (image + sections[i].offset + sections[i].size));
3011 else if(!strcmp(sections[i].sectname,"__const"))
3012 addSection(oc, SECTIONKIND_RWDATA,
3013 (void*) (image + sections[i].offset),
3014 (void*) (image + sections[i].offset + sections[i].size));
3015 else if(!strcmp(sections[i].sectname,"__data"))
3016 addSection(oc, SECTIONKIND_RWDATA,
3017 (void*) (image + sections[i].offset),
3018 (void*) (image + sections[i].offset + sections[i].size));
3021 // count external symbols defined here
3023 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3025 if((nlist[i].n_type & N_TYPE) == N_SECT)
3028 for(i=0;i<symLC->nsyms;i++)
3030 if((nlist[i].n_type & N_TYPE) == N_UNDF
3031 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3033 commonSize += nlist[i].n_value;
3037 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3038 "ocGetNames_MachO(oc->symbols)");
3040 // insert symbols into hash table
3041 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3043 if((nlist[i].n_type & N_TYPE) == N_SECT)
3045 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3046 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3047 sections[nlist[i].n_sect-1].offset
3048 - sections[nlist[i].n_sect-1].addr
3049 + nlist[i].n_value);
3050 oc->symbols[curSymbol++] = nm;
3054 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3055 commonCounter = (unsigned long)commonStorage;
3056 for(i=0;i<symLC->nsyms;i++)
3058 if((nlist[i].n_type & N_TYPE) == N_UNDF
3059 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3061 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3062 unsigned long sz = nlist[i].n_value;
3064 nlist[i].n_value = commonCounter;
3066 ghciInsertStrHashTable(oc->fileName, symhash, nm, nlist[i].n_value);
3067 oc->symbols[curSymbol++] = nm;
3069 commonCounter += sz;
3075 static int ocResolve_MachO(ObjectCode* oc)
3077 char *image = (char*) oc->image;
3078 struct mach_header *header = (struct mach_header*) image;
3079 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3081 struct segment_command *segLC = NULL;
3082 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3083 struct symtab_command *symLC = NULL;
3084 struct dysymtab_command *dsymLC = NULL;
3085 struct nlist *nlist;
3086 unsigned long *indirectSyms;
3088 for(i=0;i<header->ncmds;i++)
3090 if(lc->cmd == LC_SEGMENT)
3091 segLC = (struct segment_command*) lc;
3092 else if(lc->cmd == LC_SYMTAB)
3093 symLC = (struct symtab_command*) lc;
3094 else if(lc->cmd == LC_DYSYMTAB)
3095 dsymLC = (struct dysymtab_command*) lc;
3096 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3099 sections = (struct section*) (segLC+1);
3100 nlist = (struct nlist*) (image + symLC->symoff);
3102 for(i=0;i<segLC->nsects;i++)
3104 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3105 la_ptrs = §ions[i];
3106 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3107 nl_ptrs = §ions[i];
3110 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3113 resolveImports(image,symLC,la_ptrs,indirectSyms,nlist);
3115 resolveImports(image,symLC,nl_ptrs,indirectSyms,nlist);
3117 for(i=0;i<segLC->nsects;i++)
3119 relocateSection(image,symLC,nlist,sections,§ions[i]);
3122 /* Free the local symbol table; we won't need it again. */
3123 freeHashTable(oc->lochash, NULL);