1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.99 2002/07/17 08:26:44 simonmar Exp $
4 * (c) The GHC Team, 2000, 2001
8 * ---------------------------------------------------------------------------*/
11 #include "PosixSource.h"
18 #include "LinkerInternals.h"
20 #include "StoragePriv.h"
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
30 #ifdef HAVE_SYS_STAT_H
38 #if defined(cygwin32_TARGET_OS)
43 #ifdef HAVE_SYS_TIME_H
47 #include <sys/fcntl.h>
48 #include <sys/termios.h>
49 #include <sys/utime.h>
50 #include <sys/utsname.h>
54 #if defined(ia64_TARGET_ARCH)
60 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
61 # define OBJFORMAT_ELF
62 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
63 # define OBJFORMAT_PEi386
65 #elif defined(darwin_TARGET_OS)
66 # define OBJFORMAT_MACHO
67 # include <mach-o/loader.h>
68 # include <mach-o/nlist.h>
69 # include <mach-o/reloc.h>
72 /* Hash table mapping symbol names to Symbol */
73 /*Str*/HashTable *symhash;
75 #if defined(OBJFORMAT_ELF)
76 static int ocVerifyImage_ELF ( ObjectCode* oc );
77 static int ocGetNames_ELF ( ObjectCode* oc );
78 static int ocResolve_ELF ( ObjectCode* oc );
79 #elif defined(OBJFORMAT_PEi386)
80 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
81 static int ocGetNames_PEi386 ( ObjectCode* oc );
82 static int ocResolve_PEi386 ( ObjectCode* oc );
83 #elif defined(OBJFORMAT_MACHO)
84 static int ocVerifyImage_MachO ( ObjectCode* oc );
85 static int ocGetNames_MachO ( ObjectCode* oc );
86 static int ocResolve_MachO ( ObjectCode* oc );
89 /* -----------------------------------------------------------------------------
90 * Built-in symbols from the RTS
93 typedef struct _RtsSymbolVal {
100 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
102 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
103 SymX(makeStableNamezh_fast) \
104 SymX(finalizzeWeakzh_fast)
106 /* These are not available in GUM!!! -- HWL */
107 #define Maybe_ForeignObj
108 #define Maybe_Stable_Names
111 #if !defined (mingw32_TARGET_OS)
112 #define RTS_POSIX_ONLY_SYMBOLS \
113 SymX(stg_sig_install) \
117 #if defined (cygwin32_TARGET_OS)
118 #define RTS_MINGW_ONLY_SYMBOLS /**/
119 /* Don't have the ability to read import libs / archives, so
120 * we have to stupidly list a lot of what libcygwin.a
123 #define RTS_CYGWIN_ONLY_SYMBOLS \
205 #elif !defined(mingw32_TARGET_OS)
206 #define RTS_MINGW_ONLY_SYMBOLS /**/
207 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
208 #else /* defined(mingw32_TARGET_OS) */
209 #define RTS_POSIX_ONLY_SYMBOLS /**/
210 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
212 /* These are statically linked from the mingw libraries into the ghc
213 executable, so we have to employ this hack. */
214 #define RTS_MINGW_ONLY_SYMBOLS \
226 SymX(getservbyname) \
227 SymX(getservbyport) \
228 SymX(getprotobynumber) \
229 SymX(getprotobyname) \
230 SymX(gethostbyname) \
231 SymX(gethostbyaddr) \
266 Sym(_imp___timezone) \
282 # define MAIN_CAP_SYM SymX(MainCapability)
284 # define MAIN_CAP_SYM
287 #define RTS_SYMBOLS \
301 Sym(stg_enterStackTop) \
304 SymX(__stg_gc_enter_1) \
305 SymX(stg_gc_enter_2) \
306 SymX(stg_gc_enter_3) \
307 SymX(stg_gc_enter_4) \
308 SymX(stg_gc_enter_5) \
309 SymX(stg_gc_enter_6) \
310 SymX(stg_gc_enter_7) \
311 SymX(stg_gc_enter_8) \
313 SymX(stg_gc_noregs) \
315 SymX(stg_gc_unbx_r1) \
316 SymX(stg_gc_unpt_r1) \
317 SymX(stg_gc_ut_0_1) \
318 SymX(stg_gc_ut_1_0) \
320 SymX(stg_yield_to_interpreter) \
323 SymX(MallocFailHook) \
324 SymX(NoRunnableThreadsHook) \
326 SymX(OutOfHeapHook) \
327 SymX(PatErrorHdrHook) \
328 SymX(PostTraceHook) \
330 SymX(StackOverflowHook) \
331 SymX(__encodeDouble) \
332 SymX(__encodeFloat) \
335 SymX(__gmpz_cmp_si) \
336 SymX(__gmpz_cmp_ui) \
337 SymX(__gmpz_get_si) \
338 SymX(__gmpz_get_ui) \
339 SymX(__int_encodeDouble) \
340 SymX(__int_encodeFloat) \
341 SymX(andIntegerzh_fast) \
342 SymX(blockAsyncExceptionszh_fast) \
345 SymX(complementIntegerzh_fast) \
346 SymX(cmpIntegerzh_fast) \
347 SymX(cmpIntegerIntzh_fast) \
348 SymX(createAdjustor) \
349 SymX(decodeDoublezh_fast) \
350 SymX(decodeFloatzh_fast) \
353 SymX(deRefWeakzh_fast) \
354 SymX(deRefStablePtrzh_fast) \
355 SymX(divExactIntegerzh_fast) \
356 SymX(divModIntegerzh_fast) \
358 SymX(forkProcesszh_fast) \
359 SymX(freeHaskellFunctionPtr) \
360 SymX(freeStablePtr) \
361 SymX(gcdIntegerzh_fast) \
362 SymX(gcdIntegerIntzh_fast) \
363 SymX(gcdIntzh_fast) \
366 SymX(int2Integerzh_fast) \
367 SymX(integer2Intzh_fast) \
368 SymX(integer2Wordzh_fast) \
369 SymX(isDoubleDenormalized) \
370 SymX(isDoubleInfinite) \
372 SymX(isDoubleNegativeZero) \
373 SymX(isEmptyMVarzh_fast) \
374 SymX(isFloatDenormalized) \
375 SymX(isFloatInfinite) \
377 SymX(isFloatNegativeZero) \
378 SymX(killThreadzh_fast) \
379 SymX(makeStablePtrzh_fast) \
380 SymX(minusIntegerzh_fast) \
381 SymX(mkApUpd0zh_fast) \
382 SymX(myThreadIdzh_fast) \
383 SymX(labelThreadzh_fast) \
384 SymX(newArrayzh_fast) \
385 SymX(newBCOzh_fast) \
386 SymX(newByteArrayzh_fast) \
388 SymX(newMVarzh_fast) \
389 SymX(newMutVarzh_fast) \
390 SymX(newPinnedByteArrayzh_fast) \
391 SymX(orIntegerzh_fast) \
393 SymX(plusIntegerzh_fast) \
396 SymX(putMVarzh_fast) \
397 SymX(quotIntegerzh_fast) \
398 SymX(quotRemIntegerzh_fast) \
400 SymX(remIntegerzh_fast) \
401 SymX(resetNonBlockingFd) \
404 SymX(rts_checkSchedStatus) \
407 SymX(rts_evalLazyIO) \
412 SymX(rts_getDouble) \
417 SymX(rts_getStablePtr) \
418 SymX(rts_getThreadId) \
420 SymX(rts_getWord32) \
432 SymX(rts_mkStablePtr) \
441 SymX(shutdownHaskellAndExit) \
442 SymX(stable_ptr_table) \
443 SymX(stackOverflow) \
444 SymX(stg_CAF_BLACKHOLE_info) \
445 SymX(stg_CHARLIKE_closure) \
446 SymX(stg_EMPTY_MVAR_info) \
447 SymX(stg_IND_STATIC_info) \
448 SymX(stg_INTLIKE_closure) \
449 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
450 SymX(stg_WEAK_info) \
451 SymX(stg_ap_1_upd_info) \
452 SymX(stg_ap_2_upd_info) \
453 SymX(stg_ap_3_upd_info) \
454 SymX(stg_ap_4_upd_info) \
455 SymX(stg_ap_5_upd_info) \
456 SymX(stg_ap_6_upd_info) \
457 SymX(stg_ap_7_upd_info) \
458 SymX(stg_ap_8_upd_info) \
460 SymX(stg_sel_0_upd_info) \
461 SymX(stg_sel_10_upd_info) \
462 SymX(stg_sel_11_upd_info) \
463 SymX(stg_sel_12_upd_info) \
464 SymX(stg_sel_13_upd_info) \
465 SymX(stg_sel_14_upd_info) \
466 SymX(stg_sel_15_upd_info) \
467 SymX(stg_sel_1_upd_info) \
468 SymX(stg_sel_2_upd_info) \
469 SymX(stg_sel_3_upd_info) \
470 SymX(stg_sel_4_upd_info) \
471 SymX(stg_sel_5_upd_info) \
472 SymX(stg_sel_6_upd_info) \
473 SymX(stg_sel_7_upd_info) \
474 SymX(stg_sel_8_upd_info) \
475 SymX(stg_sel_9_upd_info) \
476 SymX(stg_seq_frame_info) \
477 SymX(stg_upd_frame_info) \
478 SymX(__stg_update_PAP) \
479 SymX(suspendThread) \
480 SymX(takeMVarzh_fast) \
481 SymX(timesIntegerzh_fast) \
482 SymX(tryPutMVarzh_fast) \
483 SymX(tryTakeMVarzh_fast) \
484 SymX(unblockAsyncExceptionszh_fast) \
485 SymX(unsafeThawArrayzh_fast) \
486 SymX(waitReadzh_fast) \
487 SymX(waitWritezh_fast) \
488 SymX(word2Integerzh_fast) \
489 SymX(xorIntegerzh_fast) \
492 #ifdef SUPPORT_LONG_LONGS
493 #define RTS_LONG_LONG_SYMS \
494 SymX(int64ToIntegerzh_fast) \
495 SymX(word64ToIntegerzh_fast)
497 #define RTS_LONG_LONG_SYMS /* nothing */
500 #ifdef ia64_TARGET_ARCH
501 /* force these symbols to be present */
502 #define RTS_EXTRA_SYMBOLS \
504 #elif defined(powerpc_TARGET_ARCH)
505 #define RTS_EXTRA_SYMBOLS \
515 #define RTS_EXTRA_SYMBOLS /* nothing */
518 /* entirely bogus claims about types of these symbols */
519 #define Sym(vvv) extern void (vvv);
520 #define SymX(vvv) /**/
524 RTS_POSIX_ONLY_SYMBOLS
525 RTS_MINGW_ONLY_SYMBOLS
526 RTS_CYGWIN_ONLY_SYMBOLS
530 #ifdef LEADING_UNDERSCORE
531 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
533 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
536 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
538 #define SymX(vvv) Sym(vvv)
540 static RtsSymbolVal rtsSyms[] = {
544 RTS_POSIX_ONLY_SYMBOLS
545 RTS_MINGW_ONLY_SYMBOLS
546 RTS_CYGWIN_ONLY_SYMBOLS
547 { 0, 0 } /* sentinel */
550 /* -----------------------------------------------------------------------------
551 * Insert symbols into hash tables, checking for duplicates.
553 static void ghciInsertStrHashTable ( char* obj_name,
559 if (lookupHashTable(table, (StgWord)key) == NULL)
561 insertStrHashTable(table, (StgWord)key, data);
566 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
568 "whilst processing object file\n"
570 "This could be caused by:\n"
571 " * Loading two different object files which export the same symbol\n"
572 " * Specifying the same object file twice on the GHCi command line\n"
573 " * An incorrect `package.conf' entry, causing some object to be\n"
575 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
584 /* -----------------------------------------------------------------------------
585 * initialize the object linker
587 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
588 static void *dl_prog_handle;
596 symhash = allocStrHashTable();
598 /* populate the symbol table with stuff from the RTS */
599 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
600 ghciInsertStrHashTable("(GHCi built-in symbols)",
601 symhash, sym->lbl, sym->addr);
603 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
604 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
608 /* -----------------------------------------------------------------------------
609 * Add a DLL from which symbols may be found. In the ELF case, just
610 * do RTLD_GLOBAL-style add, so no further messing around needs to
611 * happen in order that symbols in the loaded .so are findable --
612 * lookupSymbol() will subsequently see them by dlsym on the program's
613 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
615 * In the PEi386 case, open the DLLs and put handles to them in a
616 * linked list. When looking for a symbol, try all handles in the
620 #if defined(OBJFORMAT_PEi386)
621 /* A record for storing handles into DLLs. */
626 struct _OpenedDLL* next;
631 /* A list thereof. */
632 static OpenedDLL* opened_dlls = NULL;
638 addDLL( char *dll_name )
640 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
644 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
646 /* dlopen failed; return a ptr to the error msg. */
648 if (errmsg == NULL) errmsg = "addDLL: unknown error";
655 # elif defined(OBJFORMAT_PEi386)
657 /* Add this DLL to the list of DLLs in which to search for symbols.
658 The path argument is ignored. */
663 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
665 /* See if we've already got it, and ignore if so. */
666 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
667 if (0 == strcmp(o_dll->name, dll_name))
671 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
672 sprintf(buf, "%s.DLL", dll_name);
673 instance = LoadLibrary(buf);
674 if (instance == NULL) {
675 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
676 instance = LoadLibrary(buf);
677 if (instance == NULL) {
680 /* LoadLibrary failed; return a ptr to the error msg. */
681 return "addDLL: unknown error";
686 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
687 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
688 strcpy(o_dll->name, dll_name);
689 o_dll->instance = instance;
690 o_dll->next = opened_dlls;
695 barf("addDLL: not implemented on this platform");
699 /* -----------------------------------------------------------------------------
700 * lookup a symbol in the hash table
703 lookupSymbol( char *lbl )
706 ASSERT(symhash != NULL);
707 val = lookupStrHashTable(symhash, lbl);
710 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
711 return dlsym(dl_prog_handle, lbl);
712 # elif defined(OBJFORMAT_PEi386)
715 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
716 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
718 /* HACK: if the name has an initial underscore, try stripping
719 it off & look that up first. I've yet to verify whether there's
720 a Rule that governs whether an initial '_' *should always* be
721 stripped off when mapping from import lib name to the DLL name.
723 sym = GetProcAddress(o_dll->instance, (lbl+1));
725 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
729 sym = GetProcAddress(o_dll->instance, lbl);
731 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
746 __attribute((unused))
748 lookupLocalSymbol( ObjectCode* oc, char *lbl )
751 val = lookupStrHashTable(oc->lochash, lbl);
761 /* -----------------------------------------------------------------------------
762 * Debugging aid: look in GHCi's object symbol tables for symbols
763 * within DELTA bytes of the specified address, and show their names.
766 void ghci_enquire ( char* addr );
768 void ghci_enquire ( char* addr )
773 const int DELTA = 64;
775 for (oc = objects; oc; oc = oc->next) {
776 for (i = 0; i < oc->n_symbols; i++) {
777 sym = oc->symbols[i];
778 if (sym == NULL) continue;
779 /* fprintf(stderr, "enquire %p %p\n", sym, oc->lochash); */
781 if (oc->lochash != NULL)
782 a = lookupStrHashTable(oc->lochash, sym);
784 a = lookupStrHashTable(symhash, sym);
786 /* fprintf(stderr, "ghci_enquire: can't find %s\n", sym); */
788 else if (addr-DELTA <= a && a <= addr+DELTA) {
789 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
796 #ifdef ia64_TARGET_ARCH
797 static unsigned int PLTSize(void);
800 /* -----------------------------------------------------------------------------
801 * Load an obj (populate the global symbol table, but don't resolve yet)
803 * Returns: 1 if ok, 0 on error.
806 loadObj( char *path )
818 /* fprintf(stderr, "loadObj %s\n", path ); */
820 /* Check that we haven't already loaded this object. Don't give up
821 at this stage; ocGetNames_* will barf later. */
825 for (o = objects; o; o = o->next) {
826 if (0 == strcmp(o->fileName, path))
832 "GHCi runtime linker: warning: looks like you're trying to load the\n"
833 "same object file twice:\n"
835 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
841 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
843 # if defined(OBJFORMAT_ELF)
844 oc->formatName = "ELF";
845 # elif defined(OBJFORMAT_PEi386)
846 oc->formatName = "PEi386";
847 # elif defined(OBJFORMAT_MACHO)
848 oc->formatName = "Mach-O";
851 barf("loadObj: not implemented on this platform");
855 if (r == -1) { return 0; }
857 /* sigh, strdup() isn't a POSIX function, so do it the long way */
858 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
859 strcpy(oc->fileName, path);
861 oc->fileSize = st.st_size;
864 oc->lochash = allocStrHashTable();
865 oc->proddables = NULL;
867 /* chain it onto the list of objects */
872 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
874 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
876 fd = open(path, O_RDONLY);
878 barf("loadObj: can't open `%s'", path);
880 pagesize = getpagesize();
882 #ifdef ia64_TARGET_ARCH
883 /* The PLT needs to be right before the object */
884 n = ROUND_UP(PLTSize(), pagesize);
885 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
886 if (oc->plt == MAP_FAILED)
887 barf("loadObj: can't allocate PLT");
890 map_addr = oc->plt + n;
893 n = ROUND_UP(oc->fileSize, pagesize);
894 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
895 if (oc->image == MAP_FAILED)
896 barf("loadObj: can't map `%s'", path);
900 #else /* !USE_MMAP */
902 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
904 /* load the image into memory */
905 f = fopen(path, "rb");
907 barf("loadObj: can't read `%s'", path);
909 n = fread ( oc->image, 1, oc->fileSize, f );
910 if (n != oc->fileSize)
911 barf("loadObj: error whilst reading `%s'", path);
915 #endif /* USE_MMAP */
917 /* verify the in-memory image */
918 # if defined(OBJFORMAT_ELF)
919 r = ocVerifyImage_ELF ( oc );
920 # elif defined(OBJFORMAT_PEi386)
921 r = ocVerifyImage_PEi386 ( oc );
922 # elif defined(OBJFORMAT_MACHO)
923 r = ocVerifyImage_MachO ( oc );
925 barf("loadObj: no verify method");
927 if (!r) { return r; }
929 /* build the symbol list for this image */
930 # if defined(OBJFORMAT_ELF)
931 r = ocGetNames_ELF ( oc );
932 # elif defined(OBJFORMAT_PEi386)
933 r = ocGetNames_PEi386 ( oc );
934 # elif defined(OBJFORMAT_MACHO)
935 r = ocGetNames_MachO ( oc );
937 barf("loadObj: no getNames method");
939 if (!r) { return r; }
941 /* loaded, but not resolved yet */
942 oc->status = OBJECT_LOADED;
947 /* -----------------------------------------------------------------------------
948 * resolve all the currently unlinked objects in memory
950 * Returns: 1 if ok, 0 on error.
958 for (oc = objects; oc; oc = oc->next) {
959 if (oc->status != OBJECT_RESOLVED) {
960 # if defined(OBJFORMAT_ELF)
961 r = ocResolve_ELF ( oc );
962 # elif defined(OBJFORMAT_PEi386)
963 r = ocResolve_PEi386 ( oc );
964 # elif defined(OBJFORMAT_MACHO)
965 r = ocResolve_MachO ( oc );
967 barf("resolveObjs: not implemented on this platform");
969 if (!r) { return r; }
970 oc->status = OBJECT_RESOLVED;
976 /* -----------------------------------------------------------------------------
977 * delete an object from the pool
980 unloadObj( char *path )
982 ObjectCode *oc, *prev;
984 ASSERT(symhash != NULL);
985 ASSERT(objects != NULL);
988 for (oc = objects; oc; prev = oc, oc = oc->next) {
989 if (!strcmp(oc->fileName,path)) {
991 /* Remove all the mappings for the symbols within this
996 for (i = 0; i < oc->n_symbols; i++) {
997 if (oc->symbols[i] != NULL) {
998 removeStrHashTable(symhash, oc->symbols[i], NULL);
1006 prev->next = oc->next;
1009 /* We're going to leave this in place, in case there are
1010 any pointers from the heap into it: */
1011 /* free(oc->image); */
1015 /* The local hash table should have been freed at the end
1016 of the ocResolve_ call on it. */
1017 ASSERT(oc->lochash == NULL);
1023 belch("unloadObj: can't find `%s' to unload", path);
1027 /* -----------------------------------------------------------------------------
1028 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1029 * which may be prodded during relocation, and abort if we try and write
1030 * outside any of these.
1032 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1035 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1036 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1040 pb->next = oc->proddables;
1041 oc->proddables = pb;
1044 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1047 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1048 char* s = (char*)(pb->start);
1049 char* e = s + pb->size - 1;
1050 char* a = (char*)addr;
1051 /* Assumes that the biggest fixup involves a 4-byte write. This
1052 probably needs to be changed to 8 (ie, +7) on 64-bit
1054 if (a >= s && (a+3) <= e) return;
1056 barf("checkProddableBlock: invalid fixup in runtime linker");
1059 /* -----------------------------------------------------------------------------
1060 * Section management.
1062 static void addSection ( ObjectCode* oc, SectionKind kind,
1063 void* start, void* end )
1065 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1069 s->next = oc->sections;
1072 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1073 start, ((char*)end)-1, end - start + 1, kind );
1079 /* --------------------------------------------------------------------------
1080 * PEi386 specifics (Win32 targets)
1081 * ------------------------------------------------------------------------*/
1083 /* The information for this linker comes from
1084 Microsoft Portable Executable
1085 and Common Object File Format Specification
1086 revision 5.1 January 1998
1087 which SimonM says comes from the MS Developer Network CDs.
1089 It can be found there (on older CDs), but can also be found
1092 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1094 (this is Rev 6.0 from February 1999).
1096 Things move, so if that fails, try searching for it via
1098 http://www.google.com/search?q=PE+COFF+specification
1100 The ultimate reference for the PE format is the Winnt.h
1101 header file that comes with the Platform SDKs; as always,
1102 implementations will drift wrt their documentation.
1104 A good background article on the PE format is Matt Pietrek's
1105 March 1994 article in Microsoft System Journal (MSJ)
1106 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1107 Win32 Portable Executable File Format." The info in there
1108 has recently been updated in a two part article in
1109 MSDN magazine, issues Feb and March 2002,
1110 "Inside Windows: An In-Depth Look into the Win32 Portable
1111 Executable File Format"
1113 John Levine's book "Linkers and Loaders" contains useful
1118 #if defined(OBJFORMAT_PEi386)
1122 typedef unsigned char UChar;
1123 typedef unsigned short UInt16;
1124 typedef unsigned int UInt32;
1131 UInt16 NumberOfSections;
1132 UInt32 TimeDateStamp;
1133 UInt32 PointerToSymbolTable;
1134 UInt32 NumberOfSymbols;
1135 UInt16 SizeOfOptionalHeader;
1136 UInt16 Characteristics;
1140 #define sizeof_COFF_header 20
1147 UInt32 VirtualAddress;
1148 UInt32 SizeOfRawData;
1149 UInt32 PointerToRawData;
1150 UInt32 PointerToRelocations;
1151 UInt32 PointerToLinenumbers;
1152 UInt16 NumberOfRelocations;
1153 UInt16 NumberOfLineNumbers;
1154 UInt32 Characteristics;
1158 #define sizeof_COFF_section 40
1165 UInt16 SectionNumber;
1168 UChar NumberOfAuxSymbols;
1172 #define sizeof_COFF_symbol 18
1177 UInt32 VirtualAddress;
1178 UInt32 SymbolTableIndex;
1183 #define sizeof_COFF_reloc 10
1186 /* From PE spec doc, section 3.3.2 */
1187 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1188 windows.h -- for the same purpose, but I want to know what I'm
1190 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1191 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1192 #define MYIMAGE_FILE_DLL 0x2000
1193 #define MYIMAGE_FILE_SYSTEM 0x1000
1194 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1195 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1196 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1198 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1199 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1200 #define MYIMAGE_SYM_CLASS_STATIC 3
1201 #define MYIMAGE_SYM_UNDEFINED 0
1203 /* From PE spec doc, section 4.1 */
1204 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1205 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1206 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1208 /* From PE spec doc, section 5.2.1 */
1209 #define MYIMAGE_REL_I386_DIR32 0x0006
1210 #define MYIMAGE_REL_I386_REL32 0x0014
1213 /* We use myindex to calculate array addresses, rather than
1214 simply doing the normal subscript thing. That's because
1215 some of the above structs have sizes which are not
1216 a whole number of words. GCC rounds their sizes up to a
1217 whole number of words, which means that the address calcs
1218 arising from using normal C indexing or pointer arithmetic
1219 are just plain wrong. Sigh.
1222 myindex ( int scale, void* base, int index )
1225 ((UChar*)base) + scale * index;
1230 printName ( UChar* name, UChar* strtab )
1232 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1233 UInt32 strtab_offset = * (UInt32*)(name+4);
1234 fprintf ( stderr, "%s", strtab + strtab_offset );
1237 for (i = 0; i < 8; i++) {
1238 if (name[i] == 0) break;
1239 fprintf ( stderr, "%c", name[i] );
1246 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1248 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1249 UInt32 strtab_offset = * (UInt32*)(name+4);
1250 strncpy ( dst, strtab+strtab_offset, dstSize );
1256 if (name[i] == 0) break;
1266 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1269 /* If the string is longer than 8 bytes, look in the
1270 string table for it -- this will be correctly zero terminated.
1272 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1273 UInt32 strtab_offset = * (UInt32*)(name+4);
1274 return ((UChar*)strtab) + strtab_offset;
1276 /* Otherwise, if shorter than 8 bytes, return the original,
1277 which by defn is correctly terminated.
1279 if (name[7]==0) return name;
1280 /* The annoying case: 8 bytes. Copy into a temporary
1281 (which is never freed ...)
1283 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1285 strncpy(newstr,name,8);
1291 /* Just compares the short names (first 8 chars) */
1292 static COFF_section *
1293 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1297 = (COFF_header*)(oc->image);
1298 COFF_section* sectab
1300 ((UChar*)(oc->image))
1301 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1303 for (i = 0; i < hdr->NumberOfSections; i++) {
1306 COFF_section* section_i
1308 myindex ( sizeof_COFF_section, sectab, i );
1309 n1 = (UChar*) &(section_i->Name);
1311 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1312 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1313 n1[6]==n2[6] && n1[7]==n2[7])
1322 zapTrailingAtSign ( UChar* sym )
1324 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1326 if (sym[0] == 0) return;
1328 while (sym[i] != 0) i++;
1331 while (j > 0 && my_isdigit(sym[j])) j--;
1332 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1338 ocVerifyImage_PEi386 ( ObjectCode* oc )
1343 COFF_section* sectab;
1344 COFF_symbol* symtab;
1346 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1347 hdr = (COFF_header*)(oc->image);
1348 sectab = (COFF_section*) (
1349 ((UChar*)(oc->image))
1350 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1352 symtab = (COFF_symbol*) (
1353 ((UChar*)(oc->image))
1354 + hdr->PointerToSymbolTable
1356 strtab = ((UChar*)symtab)
1357 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1359 if (hdr->Machine != 0x14c) {
1360 belch("Not x86 PEi386");
1363 if (hdr->SizeOfOptionalHeader != 0) {
1364 belch("PEi386 with nonempty optional header");
1367 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1368 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1369 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1370 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1371 belch("Not a PEi386 object file");
1374 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1375 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1376 belch("Invalid PEi386 word size or endiannness: %d",
1377 (int)(hdr->Characteristics));
1380 /* If the string table size is way crazy, this might indicate that
1381 there are more than 64k relocations, despite claims to the
1382 contrary. Hence this test. */
1383 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1385 if ( (*(UInt32*)strtab) > 600000 ) {
1386 /* Note that 600k has no special significance other than being
1387 big enough to handle the almost-2MB-sized lumps that
1388 constitute HSwin32*.o. */
1389 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1394 /* No further verification after this point; only debug printing. */
1396 IF_DEBUG(linker, i=1);
1397 if (i == 0) return 1;
1400 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1402 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1404 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1406 fprintf ( stderr, "\n" );
1408 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1410 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1412 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1414 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1416 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1418 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1420 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1422 /* Print the section table. */
1423 fprintf ( stderr, "\n" );
1424 for (i = 0; i < hdr->NumberOfSections; i++) {
1426 COFF_section* sectab_i
1428 myindex ( sizeof_COFF_section, sectab, i );
1435 printName ( sectab_i->Name, strtab );
1445 sectab_i->VirtualSize,
1446 sectab_i->VirtualAddress,
1447 sectab_i->SizeOfRawData,
1448 sectab_i->PointerToRawData,
1449 sectab_i->NumberOfRelocations,
1450 sectab_i->PointerToRelocations,
1451 sectab_i->PointerToRawData
1453 reltab = (COFF_reloc*) (
1454 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1457 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1458 /* If the relocation field (a short) has overflowed, the
1459 * real count can be found in the first reloc entry.
1461 * See Section 4.1 (last para) of the PE spec (rev6.0).
1463 COFF_reloc* rel = (COFF_reloc*)
1464 myindex ( sizeof_COFF_reloc, reltab, 0 );
1465 noRelocs = rel->VirtualAddress;
1468 noRelocs = sectab_i->NumberOfRelocations;
1472 for (; j < noRelocs; j++) {
1474 COFF_reloc* rel = (COFF_reloc*)
1475 myindex ( sizeof_COFF_reloc, reltab, j );
1477 " type 0x%-4x vaddr 0x%-8x name `",
1479 rel->VirtualAddress );
1480 sym = (COFF_symbol*)
1481 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1482 /* Hmm..mysterious looking offset - what's it for? SOF */
1483 printName ( sym->Name, strtab -10 );
1484 fprintf ( stderr, "'\n" );
1487 fprintf ( stderr, "\n" );
1489 fprintf ( stderr, "\n" );
1490 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1491 fprintf ( stderr, "---START of string table---\n");
1492 for (i = 4; i < *(Int32*)strtab; i++) {
1494 fprintf ( stderr, "\n"); else
1495 fprintf( stderr, "%c", strtab[i] );
1497 fprintf ( stderr, "--- END of string table---\n");
1499 fprintf ( stderr, "\n" );
1502 COFF_symbol* symtab_i;
1503 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1504 symtab_i = (COFF_symbol*)
1505 myindex ( sizeof_COFF_symbol, symtab, i );
1511 printName ( symtab_i->Name, strtab );
1520 (Int32)(symtab_i->SectionNumber),
1521 (UInt32)symtab_i->Type,
1522 (UInt32)symtab_i->StorageClass,
1523 (UInt32)symtab_i->NumberOfAuxSymbols
1525 i += symtab_i->NumberOfAuxSymbols;
1529 fprintf ( stderr, "\n" );
1535 ocGetNames_PEi386 ( ObjectCode* oc )
1538 COFF_section* sectab;
1539 COFF_symbol* symtab;
1546 hdr = (COFF_header*)(oc->image);
1547 sectab = (COFF_section*) (
1548 ((UChar*)(oc->image))
1549 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1551 symtab = (COFF_symbol*) (
1552 ((UChar*)(oc->image))
1553 + hdr->PointerToSymbolTable
1555 strtab = ((UChar*)(oc->image))
1556 + hdr->PointerToSymbolTable
1557 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1559 /* Allocate space for any (local, anonymous) .bss sections. */
1561 for (i = 0; i < hdr->NumberOfSections; i++) {
1563 COFF_section* sectab_i
1565 myindex ( sizeof_COFF_section, sectab, i );
1566 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1567 if (sectab_i->VirtualSize == 0) continue;
1568 /* This is a non-empty .bss section. Allocate zeroed space for
1569 it, and set its PointerToRawData field such that oc->image +
1570 PointerToRawData == addr_of_zeroed_space. */
1571 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1572 "ocGetNames_PEi386(anonymous bss)");
1573 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1574 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1575 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1578 /* Copy section information into the ObjectCode. */
1580 for (i = 0; i < hdr->NumberOfSections; i++) {
1586 = SECTIONKIND_OTHER;
1587 COFF_section* sectab_i
1589 myindex ( sizeof_COFF_section, sectab, i );
1590 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1593 /* I'm sure this is the Right Way to do it. However, the
1594 alternative of testing the sectab_i->Name field seems to
1595 work ok with Cygwin.
1597 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1598 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1599 kind = SECTIONKIND_CODE_OR_RODATA;
1602 if (0==strcmp(".text",sectab_i->Name) ||
1603 0==strcmp(".rodata",sectab_i->Name))
1604 kind = SECTIONKIND_CODE_OR_RODATA;
1605 if (0==strcmp(".data",sectab_i->Name) ||
1606 0==strcmp(".bss",sectab_i->Name))
1607 kind = SECTIONKIND_RWDATA;
1609 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1610 sz = sectab_i->SizeOfRawData;
1611 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1613 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1614 end = start + sz - 1;
1616 if (kind == SECTIONKIND_OTHER
1617 /* Ignore sections called which contain stabs debugging
1619 && 0 != strcmp(".stab", sectab_i->Name)
1620 && 0 != strcmp(".stabstr", sectab_i->Name)
1622 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1626 if (kind != SECTIONKIND_OTHER && end >= start) {
1627 addSection(oc, kind, start, end);
1628 addProddableBlock(oc, start, end - start + 1);
1632 /* Copy exported symbols into the ObjectCode. */
1634 oc->n_symbols = hdr->NumberOfSymbols;
1635 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1636 "ocGetNames_PEi386(oc->symbols)");
1637 /* Call me paranoid; I don't care. */
1638 for (i = 0; i < oc->n_symbols; i++)
1639 oc->symbols[i] = NULL;
1643 COFF_symbol* symtab_i;
1644 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1645 symtab_i = (COFF_symbol*)
1646 myindex ( sizeof_COFF_symbol, symtab, i );
1650 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1651 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1652 /* This symbol is global and defined, viz, exported */
1653 /* for MYIMAGE_SYMCLASS_EXTERNAL
1654 && !MYIMAGE_SYM_UNDEFINED,
1655 the address of the symbol is:
1656 address of relevant section + offset in section
1658 COFF_section* sectabent
1659 = (COFF_section*) myindex ( sizeof_COFF_section,
1661 symtab_i->SectionNumber-1 );
1662 addr = ((UChar*)(oc->image))
1663 + (sectabent->PointerToRawData
1667 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1668 && symtab_i->Value > 0) {
1669 /* This symbol isn't in any section at all, ie, global bss.
1670 Allocate zeroed space for it. */
1671 addr = stgCallocBytes(1, symtab_i->Value,
1672 "ocGetNames_PEi386(non-anonymous bss)");
1673 addSection(oc, SECTIONKIND_RWDATA, addr,
1674 ((UChar*)addr) + symtab_i->Value - 1);
1675 addProddableBlock(oc, addr, symtab_i->Value);
1676 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1679 if (addr != NULL ) {
1680 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1681 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1682 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1683 ASSERT(i >= 0 && i < oc->n_symbols);
1684 /* cstring_from_COFF_symbol_name always succeeds. */
1685 oc->symbols[i] = sname;
1686 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1690 "IGNORING symbol %d\n"
1694 printName ( symtab_i->Name, strtab );
1703 (Int32)(symtab_i->SectionNumber),
1704 (UInt32)symtab_i->Type,
1705 (UInt32)symtab_i->StorageClass,
1706 (UInt32)symtab_i->NumberOfAuxSymbols
1711 i += symtab_i->NumberOfAuxSymbols;
1720 ocResolve_PEi386 ( ObjectCode* oc )
1723 COFF_section* sectab;
1724 COFF_symbol* symtab;
1734 /* ToDo: should be variable-sized? But is at least safe in the
1735 sense of buffer-overrun-proof. */
1737 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1739 hdr = (COFF_header*)(oc->image);
1740 sectab = (COFF_section*) (
1741 ((UChar*)(oc->image))
1742 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1744 symtab = (COFF_symbol*) (
1745 ((UChar*)(oc->image))
1746 + hdr->PointerToSymbolTable
1748 strtab = ((UChar*)(oc->image))
1749 + hdr->PointerToSymbolTable
1750 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1752 for (i = 0; i < hdr->NumberOfSections; i++) {
1753 COFF_section* sectab_i
1755 myindex ( sizeof_COFF_section, sectab, i );
1758 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1761 /* Ignore sections called which contain stabs debugging
1763 if (0 == strcmp(".stab", sectab_i->Name)
1764 || 0 == strcmp(".stabstr", sectab_i->Name))
1767 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1768 /* If the relocation field (a short) has overflowed, the
1769 * real count can be found in the first reloc entry.
1771 * See Section 4.1 (last para) of the PE spec (rev6.0).
1773 COFF_reloc* rel = (COFF_reloc*)
1774 myindex ( sizeof_COFF_reloc, reltab, 0 );
1775 noRelocs = rel->VirtualAddress;
1776 fprintf(stderr, "Overflown relocs: %u\n", noRelocs);
1779 noRelocs = sectab_i->NumberOfRelocations;
1784 for (; j < noRelocs; j++) {
1786 COFF_reloc* reltab_j
1788 myindex ( sizeof_COFF_reloc, reltab, j );
1790 /* the location to patch */
1792 ((UChar*)(oc->image))
1793 + (sectab_i->PointerToRawData
1794 + reltab_j->VirtualAddress
1795 - sectab_i->VirtualAddress )
1797 /* the existing contents of pP */
1799 /* the symbol to connect to */
1800 sym = (COFF_symbol*)
1801 myindex ( sizeof_COFF_symbol,
1802 symtab, reltab_j->SymbolTableIndex );
1805 "reloc sec %2d num %3d: type 0x%-4x "
1806 "vaddr 0x%-8x name `",
1808 (UInt32)reltab_j->Type,
1809 reltab_j->VirtualAddress );
1810 printName ( sym->Name, strtab );
1811 fprintf ( stderr, "'\n" ));
1813 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1814 COFF_section* section_sym
1815 = findPEi386SectionCalled ( oc, sym->Name );
1817 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1820 S = ((UInt32)(oc->image))
1821 + (section_sym->PointerToRawData
1824 copyName ( sym->Name, strtab, symbol, 1000-1 );
1825 (void*)S = lookupLocalSymbol( oc, symbol );
1826 if ((void*)S != NULL) goto foundit;
1827 (void*)S = lookupSymbol( symbol );
1828 if ((void*)S != NULL) goto foundit;
1829 zapTrailingAtSign ( symbol );
1830 (void*)S = lookupLocalSymbol( oc, symbol );
1831 if ((void*)S != NULL) goto foundit;
1832 (void*)S = lookupSymbol( symbol );
1833 if ((void*)S != NULL) goto foundit;
1834 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1838 checkProddableBlock(oc, pP);
1839 switch (reltab_j->Type) {
1840 case MYIMAGE_REL_I386_DIR32:
1843 case MYIMAGE_REL_I386_REL32:
1844 /* Tricky. We have to insert a displacement at
1845 pP which, when added to the PC for the _next_
1846 insn, gives the address of the target (S).
1847 Problem is to know the address of the next insn
1848 when we only know pP. We assume that this
1849 literal field is always the last in the insn,
1850 so that the address of the next insn is pP+4
1851 -- hence the constant 4.
1852 Also I don't know if A should be added, but so
1853 far it has always been zero.
1856 *pP = S - ((UInt32)pP) - 4;
1859 belch("%s: unhandled PEi386 relocation type %d",
1860 oc->fileName, reltab_j->Type);
1867 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1871 #endif /* defined(OBJFORMAT_PEi386) */
1874 /* --------------------------------------------------------------------------
1876 * ------------------------------------------------------------------------*/
1878 #if defined(OBJFORMAT_ELF)
1883 #if defined(sparc_TARGET_ARCH)
1884 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1885 #elif defined(i386_TARGET_ARCH)
1886 # define ELF_TARGET_386 /* Used inside <elf.h> */
1887 #elif defined (ia64_TARGET_ARCH)
1888 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
1890 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
1891 # define ELF_NEED_GOT /* needs Global Offset Table */
1892 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
1898 * Define a set of types which can be used for both ELF32 and ELF64
1902 #define ELFCLASS ELFCLASS64
1903 #define Elf_Addr Elf64_Addr
1904 #define Elf_Word Elf64_Word
1905 #define Elf_Sword Elf64_Sword
1906 #define Elf_Ehdr Elf64_Ehdr
1907 #define Elf_Phdr Elf64_Phdr
1908 #define Elf_Shdr Elf64_Shdr
1909 #define Elf_Sym Elf64_Sym
1910 #define Elf_Rel Elf64_Rel
1911 #define Elf_Rela Elf64_Rela
1912 #define ELF_ST_TYPE ELF64_ST_TYPE
1913 #define ELF_ST_BIND ELF64_ST_BIND
1914 #define ELF_R_TYPE ELF64_R_TYPE
1915 #define ELF_R_SYM ELF64_R_SYM
1917 #define ELFCLASS ELFCLASS32
1918 #define Elf_Addr Elf32_Addr
1919 #define Elf_Word Elf32_Word
1920 #define Elf_Sword Elf32_Sword
1921 #define Elf_Ehdr Elf32_Ehdr
1922 #define Elf_Phdr Elf32_Phdr
1923 #define Elf_Shdr Elf32_Shdr
1924 #define Elf_Sym Elf32_Sym
1925 #define Elf_Rel Elf32_Rel
1926 #define Elf_Rela Elf32_Rela
1927 #define ELF_ST_TYPE ELF32_ST_TYPE
1928 #define ELF_ST_BIND ELF32_ST_BIND
1929 #define ELF_R_TYPE ELF32_R_TYPE
1930 #define ELF_R_SYM ELF32_R_SYM
1935 * Functions to allocate entries in dynamic sections. Currently we simply
1936 * preallocate a large number, and we don't check if a entry for the given
1937 * target already exists (a linear search is too slow). Ideally these
1938 * entries would be associated with symbols.
1941 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
1942 #define GOT_SIZE 0x20000
1943 #define FUNCTION_TABLE_SIZE 0x10000
1944 #define PLT_SIZE 0x08000
1947 static Elf_Addr got[GOT_SIZE];
1948 static unsigned int gotIndex;
1949 static Elf_Addr gp_val = (Elf_Addr)got;
1952 allocateGOTEntry(Elf_Addr target)
1956 if (gotIndex >= GOT_SIZE)
1957 barf("Global offset table overflow");
1959 entry = &got[gotIndex++];
1961 return (Elf_Addr)entry;
1965 #ifdef ELF_FUNCTION_DESC
1971 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
1972 static unsigned int functionTableIndex;
1975 allocateFunctionDesc(Elf_Addr target)
1977 FunctionDesc *entry;
1979 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
1980 barf("Function table overflow");
1982 entry = &functionTable[functionTableIndex++];
1984 entry->gp = (Elf_Addr)gp_val;
1985 return (Elf_Addr)entry;
1989 copyFunctionDesc(Elf_Addr target)
1991 FunctionDesc *olddesc = (FunctionDesc *)target;
1992 FunctionDesc *newdesc;
1994 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
1995 newdesc->gp = olddesc->gp;
1996 return (Elf_Addr)newdesc;
2001 #ifdef ia64_TARGET_ARCH
2002 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2003 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2005 static unsigned char plt_code[] =
2007 /* taken from binutils bfd/elfxx-ia64.c */
2008 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2009 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2010 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2011 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2012 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2013 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2016 /* If we can't get to the function descriptor via gp, take a local copy of it */
2017 #define PLT_RELOC(code, target) { \
2018 Elf64_Sxword rel_value = target - gp_val; \
2019 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2020 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2022 ia64_reloc_gprel22((Elf_Addr)code, target); \
2027 unsigned char code[sizeof(plt_code)];
2031 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2033 PLTEntry *plt = (PLTEntry *)oc->plt;
2036 if (oc->pltIndex >= PLT_SIZE)
2037 barf("Procedure table overflow");
2039 entry = &plt[oc->pltIndex++];
2040 memcpy(entry->code, plt_code, sizeof(entry->code));
2041 PLT_RELOC(entry->code, target);
2042 return (Elf_Addr)entry;
2048 return (PLT_SIZE * sizeof(PLTEntry));
2054 * Generic ELF functions
2058 findElfSection ( void* objImage, Elf_Word sh_type )
2060 char* ehdrC = (char*)objImage;
2061 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2062 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2063 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2067 for (i = 0; i < ehdr->e_shnum; i++) {
2068 if (shdr[i].sh_type == sh_type
2069 /* Ignore the section header's string table. */
2070 && i != ehdr->e_shstrndx
2071 /* Ignore string tables named .stabstr, as they contain
2073 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2075 ptr = ehdrC + shdr[i].sh_offset;
2082 #if defined(ia64_TARGET_ARCH)
2084 findElfSegment ( void* objImage, Elf_Addr vaddr )
2086 char* ehdrC = (char*)objImage;
2087 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2088 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2089 Elf_Addr segaddr = 0;
2092 for (i = 0; i < ehdr->e_phnum; i++) {
2093 segaddr = phdr[i].p_vaddr;
2094 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2102 ocVerifyImage_ELF ( ObjectCode* oc )
2106 int i, j, nent, nstrtab, nsymtabs;
2110 char* ehdrC = (char*)(oc->image);
2111 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2113 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2114 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2115 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2116 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2117 belch("%s: not an ELF object", oc->fileName);
2121 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2122 belch("%s: unsupported ELF format", oc->fileName);
2126 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2127 IF_DEBUG(linker,belch( "Is little-endian" ));
2129 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2130 IF_DEBUG(linker,belch( "Is big-endian" ));
2132 belch("%s: unknown endiannness", oc->fileName);
2136 if (ehdr->e_type != ET_REL) {
2137 belch("%s: not a relocatable object (.o) file", oc->fileName);
2140 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2142 IF_DEBUG(linker,belch( "Architecture is " ));
2143 switch (ehdr->e_machine) {
2144 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2145 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2147 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2149 default: IF_DEBUG(linker,belch( "unknown" ));
2150 belch("%s: unknown architecture", oc->fileName);
2154 IF_DEBUG(linker,belch(
2155 "\nSection header table: start %d, n_entries %d, ent_size %d",
2156 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2158 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2160 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2162 if (ehdr->e_shstrndx == SHN_UNDEF) {
2163 belch("%s: no section header string table", oc->fileName);
2166 IF_DEBUG(linker,belch( "Section header string table is section %d",
2168 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2171 for (i = 0; i < ehdr->e_shnum; i++) {
2172 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2173 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2174 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2175 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2176 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2177 ehdrC + shdr[i].sh_offset,
2178 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2180 if (shdr[i].sh_type == SHT_REL) {
2181 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2182 } else if (shdr[i].sh_type == SHT_RELA) {
2183 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2185 IF_DEBUG(linker,fprintf(stderr," "));
2188 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2192 IF_DEBUG(linker,belch( "\nString tables" ));
2195 for (i = 0; i < ehdr->e_shnum; i++) {
2196 if (shdr[i].sh_type == SHT_STRTAB
2197 /* Ignore the section header's string table. */
2198 && i != ehdr->e_shstrndx
2199 /* Ignore string tables named .stabstr, as they contain
2201 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2203 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2204 strtab = ehdrC + shdr[i].sh_offset;
2209 belch("%s: no string tables, or too many", oc->fileName);
2214 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2215 for (i = 0; i < ehdr->e_shnum; i++) {
2216 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2217 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2219 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2220 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2221 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2223 shdr[i].sh_size % sizeof(Elf_Sym)
2225 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2226 belch("%s: non-integral number of symbol table entries", oc->fileName);
2229 for (j = 0; j < nent; j++) {
2230 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2231 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2232 (int)stab[j].st_shndx,
2233 (int)stab[j].st_size,
2234 (char*)stab[j].st_value ));
2236 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2237 switch (ELF_ST_TYPE(stab[j].st_info)) {
2238 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2239 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2240 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2241 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2242 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2243 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2245 IF_DEBUG(linker,fprintf(stderr, " " ));
2247 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2248 switch (ELF_ST_BIND(stab[j].st_info)) {
2249 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2250 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2251 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2252 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2254 IF_DEBUG(linker,fprintf(stderr, " " ));
2256 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2260 if (nsymtabs == 0) {
2261 belch("%s: didn't find any symbol tables", oc->fileName);
2270 ocGetNames_ELF ( ObjectCode* oc )
2275 char* ehdrC = (char*)(oc->image);
2276 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2277 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2278 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2280 ASSERT(symhash != NULL);
2283 belch("%s: no strtab", oc->fileName);
2288 for (i = 0; i < ehdr->e_shnum; i++) {
2289 /* Figure out what kind of section it is. Logic derived from
2290 Figure 1.14 ("Special Sections") of the ELF document
2291 ("Portable Formats Specification, Version 1.1"). */
2292 Elf_Shdr hdr = shdr[i];
2293 SectionKind kind = SECTIONKIND_OTHER;
2296 if (hdr.sh_type == SHT_PROGBITS
2297 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2298 /* .text-style section */
2299 kind = SECTIONKIND_CODE_OR_RODATA;
2302 if (hdr.sh_type == SHT_PROGBITS
2303 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2304 /* .data-style section */
2305 kind = SECTIONKIND_RWDATA;
2308 if (hdr.sh_type == SHT_PROGBITS
2309 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2310 /* .rodata-style section */
2311 kind = SECTIONKIND_CODE_OR_RODATA;
2314 if (hdr.sh_type == SHT_NOBITS
2315 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2316 /* .bss-style section */
2317 kind = SECTIONKIND_RWDATA;
2321 if (is_bss && shdr[i].sh_size > 0) {
2322 /* This is a non-empty .bss section. Allocate zeroed space for
2323 it, and set its .sh_offset field such that
2324 ehdrC + .sh_offset == addr_of_zeroed_space. */
2325 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2326 "ocGetNames_ELF(BSS)");
2327 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2329 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2330 zspace, shdr[i].sh_size);
2334 /* fill in the section info */
2335 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2336 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2337 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2338 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2341 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2343 /* copy stuff into this module's object symbol table */
2344 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2345 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2347 oc->n_symbols = nent;
2348 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2349 "ocGetNames_ELF(oc->symbols)");
2351 for (j = 0; j < nent; j++) {
2353 char isLocal = FALSE; /* avoids uninit-var warning */
2355 char* nm = strtab + stab[j].st_name;
2356 int secno = stab[j].st_shndx;
2358 /* Figure out if we want to add it; if so, set ad to its
2359 address. Otherwise leave ad == NULL. */
2361 if (secno == SHN_COMMON) {
2363 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2365 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2366 stab[j].st_size, nm);
2368 /* Pointless to do addProddableBlock() for this area,
2369 since the linker should never poke around in it. */
2372 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2373 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2375 /* and not an undefined symbol */
2376 && stab[j].st_shndx != SHN_UNDEF
2377 /* and not in a "special section" */
2378 && stab[j].st_shndx < SHN_LORESERVE
2380 /* and it's a not a section or string table or anything silly */
2381 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2382 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2383 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2386 /* Section 0 is the undefined section, hence > and not >=. */
2387 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2389 if (shdr[secno].sh_type == SHT_NOBITS) {
2390 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2391 stab[j].st_size, stab[j].st_value, nm);
2394 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2395 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2398 #ifdef ELF_FUNCTION_DESC
2399 /* dlsym() and the initialisation table both give us function
2400 * descriptors, so to be consistent we store function descriptors
2401 * in the symbol table */
2402 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2403 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2405 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2406 ad, oc->fileName, nm ));
2411 /* And the decision is ... */
2415 oc->symbols[j] = nm;
2418 /* Ignore entirely. */
2420 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2424 IF_DEBUG(linker,belch( "skipping `%s'",
2425 strtab + stab[j].st_name ));
2428 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2429 (int)ELF_ST_BIND(stab[j].st_info),
2430 (int)ELF_ST_TYPE(stab[j].st_info),
2431 (int)stab[j].st_shndx,
2432 strtab + stab[j].st_name
2435 oc->symbols[j] = NULL;
2444 /* Do ELF relocations which lack an explicit addend. All x86-linux
2445 relocations appear to be of this form. */
2447 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2448 Elf_Shdr* shdr, int shnum,
2449 Elf_Sym* stab, char* strtab )
2454 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2455 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2456 int target_shndx = shdr[shnum].sh_info;
2457 int symtab_shndx = shdr[shnum].sh_link;
2459 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2460 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2461 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2462 target_shndx, symtab_shndx ));
2464 for (j = 0; j < nent; j++) {
2465 Elf_Addr offset = rtab[j].r_offset;
2466 Elf_Addr info = rtab[j].r_info;
2468 Elf_Addr P = ((Elf_Addr)targ) + offset;
2469 Elf_Word* pP = (Elf_Word*)P;
2474 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2475 j, (void*)offset, (void*)info ));
2477 IF_DEBUG(linker,belch( " ZERO" ));
2480 Elf_Sym sym = stab[ELF_R_SYM(info)];
2481 /* First see if it is a local symbol. */
2482 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2483 /* Yes, so we can get the address directly from the ELF symbol
2485 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2487 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2488 + stab[ELF_R_SYM(info)].st_value);
2491 /* No, so look up the name in our global table. */
2492 symbol = strtab + sym.st_name;
2493 (void*)S = lookupSymbol( symbol );
2496 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2499 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2502 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2503 (void*)P, (void*)S, (void*)A ));
2504 checkProddableBlock ( oc, pP );
2508 switch (ELF_R_TYPE(info)) {
2509 # ifdef i386_TARGET_ARCH
2510 case R_386_32: *pP = value; break;
2511 case R_386_PC32: *pP = value - P; break;
2514 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2515 oc->fileName, ELF_R_TYPE(info));
2523 /* Do ELF relocations for which explicit addends are supplied.
2524 sparc-solaris relocations appear to be of this form. */
2526 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2527 Elf_Shdr* shdr, int shnum,
2528 Elf_Sym* stab, char* strtab )
2533 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2534 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2535 int target_shndx = shdr[shnum].sh_info;
2536 int symtab_shndx = shdr[shnum].sh_link;
2538 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2539 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2540 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2541 target_shndx, symtab_shndx ));
2543 for (j = 0; j < nent; j++) {
2544 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
2545 /* This #ifdef only serves to avoid unused-var warnings. */
2546 Elf_Addr offset = rtab[j].r_offset;
2547 Elf_Addr P = targ + offset;
2549 Elf_Addr info = rtab[j].r_info;
2550 Elf_Addr A = rtab[j].r_addend;
2553 # if defined(sparc_TARGET_ARCH)
2554 Elf_Word* pP = (Elf_Word*)P;
2556 # elif defined(ia64_TARGET_ARCH)
2557 Elf64_Xword *pP = (Elf64_Xword *)P;
2561 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2562 j, (void*)offset, (void*)info,
2565 IF_DEBUG(linker,belch( " ZERO" ));
2568 Elf_Sym sym = stab[ELF_R_SYM(info)];
2569 /* First see if it is a local symbol. */
2570 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2571 /* Yes, so we can get the address directly from the ELF symbol
2573 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2575 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2576 + stab[ELF_R_SYM(info)].st_value);
2577 #ifdef ELF_FUNCTION_DESC
2578 /* Make a function descriptor for this function */
2579 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2580 S = allocateFunctionDesc(S + A);
2585 /* No, so look up the name in our global table. */
2586 symbol = strtab + sym.st_name;
2587 (void*)S = lookupSymbol( symbol );
2589 #ifdef ELF_FUNCTION_DESC
2590 /* If a function, already a function descriptor - we would
2591 have to copy it to add an offset. */
2592 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC)
2597 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2600 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2603 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2604 (void*)P, (void*)S, (void*)A ));
2605 /* checkProddableBlock ( oc, (void*)P ); */
2609 switch (ELF_R_TYPE(info)) {
2610 # if defined(sparc_TARGET_ARCH)
2611 case R_SPARC_WDISP30:
2612 w1 = *pP & 0xC0000000;
2613 w2 = (Elf_Word)((value - P) >> 2);
2614 ASSERT((w2 & 0xC0000000) == 0);
2619 w1 = *pP & 0xFFC00000;
2620 w2 = (Elf_Word)(value >> 10);
2621 ASSERT((w2 & 0xFFC00000) == 0);
2627 w2 = (Elf_Word)(value & 0x3FF);
2628 ASSERT((w2 & ~0x3FF) == 0);
2632 /* According to the Sun documentation:
2634 This relocation type resembles R_SPARC_32, except it refers to an
2635 unaligned word. That is, the word to be relocated must be treated
2636 as four separate bytes with arbitrary alignment, not as a word
2637 aligned according to the architecture requirements.
2639 (JRS: which means that freeloading on the R_SPARC_32 case
2640 is probably wrong, but hey ...)
2644 w2 = (Elf_Word)value;
2647 # elif defined(ia64_TARGET_ARCH)
2648 case R_IA64_DIR64LSB:
2649 case R_IA64_FPTR64LSB:
2652 case R_IA64_SEGREL64LSB:
2653 addr = findElfSegment(ehdrC, value);
2656 case R_IA64_GPREL22:
2657 ia64_reloc_gprel22(P, value);
2659 case R_IA64_LTOFF22:
2660 case R_IA64_LTOFF_FPTR22:
2661 addr = allocateGOTEntry(value);
2662 ia64_reloc_gprel22(P, addr);
2664 case R_IA64_PCREL21B:
2665 ia64_reloc_pcrel21(P, S, oc);
2669 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2670 oc->fileName, ELF_R_TYPE(info));
2679 ocResolve_ELF ( ObjectCode* oc )
2683 Elf_Sym* stab = NULL;
2684 char* ehdrC = (char*)(oc->image);
2685 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2686 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2687 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2689 /* first find "the" symbol table */
2690 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2692 /* also go find the string table */
2693 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2695 if (stab == NULL || strtab == NULL) {
2696 belch("%s: can't find string or symbol table", oc->fileName);
2700 /* Process the relocation sections. */
2701 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2703 /* Skip sections called ".rel.stab". These appear to contain
2704 relocation entries that, when done, make the stabs debugging
2705 info point at the right places. We ain't interested in all
2707 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2710 if (shdr[shnum].sh_type == SHT_REL ) {
2711 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2712 shnum, stab, strtab );
2716 if (shdr[shnum].sh_type == SHT_RELA) {
2717 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2718 shnum, stab, strtab );
2723 /* Free the local symbol table; we won't need it again. */
2724 freeHashTable(oc->lochash, NULL);
2732 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2733 * at the front. The following utility functions pack and unpack instructions, and
2734 * take care of the most common relocations.
2737 #ifdef ia64_TARGET_ARCH
2740 ia64_extract_instruction(Elf64_Xword *target)
2743 int slot = (Elf_Addr)target & 3;
2744 (Elf_Addr)target &= ~3;
2752 return ((w1 >> 5) & 0x1ffffffffff);
2754 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2758 barf("ia64_extract_instruction: invalid slot %p", target);
2763 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2765 int slot = (Elf_Addr)target & 3;
2766 (Elf_Addr)target &= ~3;
2771 *target |= value << 5;
2774 *target |= value << 46;
2775 *(target+1) |= value >> 18;
2778 *(target+1) |= value << 23;
2784 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2786 Elf64_Xword instruction;
2787 Elf64_Sxword rel_value;
2789 rel_value = value - gp_val;
2790 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2791 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2793 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2794 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2795 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2796 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2797 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2798 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2802 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2804 Elf64_Xword instruction;
2805 Elf64_Sxword rel_value;
2808 entry = allocatePLTEntry(value, oc);
2810 rel_value = (entry >> 4) - (target >> 4);
2811 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2812 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
2814 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2815 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
2816 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2817 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2824 /* --------------------------------------------------------------------------
2826 * ------------------------------------------------------------------------*/
2828 #if defined(OBJFORMAT_MACHO)
2831 Initial support for MachO linking on Darwin/MacOS X on PowerPC chips
2832 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
2834 I hereby formally apologize for the hackish nature of this code.
2835 Things that need to be done:
2836 *) get common symbols and .bss sections to work properly.
2837 Haskell modules seem to work, but C modules can cause problems
2838 *) implement ocVerifyImage_MachO
2839 *) add more sanity checks. The current code just has to segfault if there's a
2843 static int ocVerifyImage_MachO(ObjectCode* oc)
2845 // FIXME: do some verifying here
2849 static void resolveImports(
2852 struct symtab_command *symLC,
2853 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
2854 unsigned long *indirectSyms,
2855 struct nlist *nlist)
2859 for(i=0;i*4<sect->size;i++)
2861 // according to otool, reserved1 contains the first index into the indirect symbol table
2862 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
2863 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
2866 if((symbol->n_type & N_TYPE) == N_UNDF
2867 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
2868 addr = (void*) (symbol->n_value);
2869 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
2872 addr = lookupSymbol(nm);
2875 fprintf(stderr, "not found: %s\n", nm);
2879 ((void**)(image + sect->offset))[i] = addr;
2883 static void relocateSection(char *image,
2884 struct symtab_command *symLC, struct nlist *nlist,
2885 struct section* sections, struct section *sect)
2887 struct relocation_info *relocs;
2890 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
2892 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
2896 relocs = (struct relocation_info*) (image + sect->reloff);
2900 if(relocs[i].r_address & R_SCATTERED)
2902 struct scattered_relocation_info *scat =
2903 (struct scattered_relocation_info*) &relocs[i];
2907 if(scat->r_length == 2 && scat->r_type == GENERIC_RELOC_VANILLA)
2909 unsigned long* word = (unsigned long*) (image + sect->offset + scat->r_address);
2911 *word = scat->r_value + sect->offset + ((long) image);
2915 continue; // FIXME: I hope it's OK to ignore all the others.
2919 struct relocation_info *reloc = &relocs[i];
2920 if(reloc->r_pcrel && !reloc->r_extern)
2924 && reloc->r_length == 2
2925 && reloc->r_type == GENERIC_RELOC_VANILLA)
2927 unsigned long* word = (unsigned long*) (image + sect->offset + reloc->r_address);
2929 if(!reloc->r_extern)
2932 sections[reloc->r_symbolnum-1].offset
2933 - sections[reloc->r_symbolnum-1].addr
2940 struct nlist *symbol = &nlist[reloc->r_symbolnum];
2941 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
2942 *word = (unsigned long) (lookupSymbol(nm));
2947 fprintf(stderr, "unknown reloc\n");
2954 static int ocGetNames_MachO(ObjectCode* oc)
2956 char *image = (char*) oc->image;
2957 struct mach_header *header = (struct mach_header*) image;
2958 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
2959 unsigned i,curSymbol;
2960 struct segment_command *segLC = NULL;
2961 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
2962 struct symtab_command *symLC = NULL;
2963 struct dysymtab_command *dsymLC = NULL;
2964 struct nlist *nlist;
2965 unsigned long commonSize = 0;
2966 char *commonStorage = NULL;
2967 unsigned long commonCounter;
2969 for(i=0;i<header->ncmds;i++)
2971 if(lc->cmd == LC_SEGMENT)
2972 segLC = (struct segment_command*) lc;
2973 else if(lc->cmd == LC_SYMTAB)
2974 symLC = (struct symtab_command*) lc;
2975 else if(lc->cmd == LC_DYSYMTAB)
2976 dsymLC = (struct dysymtab_command*) lc;
2977 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
2980 sections = (struct section*) (segLC+1);
2981 nlist = (struct nlist*) (image + symLC->symoff);
2983 for(i=0;i<segLC->nsects;i++)
2985 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
2986 la_ptrs = §ions[i];
2987 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
2988 nl_ptrs = §ions[i];
2990 // for now, only add __text and __const to the sections table
2991 else if(!strcmp(sections[i].sectname,"__text"))
2992 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
2993 (void*) (image + sections[i].offset),
2994 (void*) (image + sections[i].offset + sections[i].size));
2995 else if(!strcmp(sections[i].sectname,"__const"))
2996 addSection(oc, SECTIONKIND_RWDATA,
2997 (void*) (image + sections[i].offset),
2998 (void*) (image + sections[i].offset + sections[i].size));
2999 else if(!strcmp(sections[i].sectname,"__data"))
3000 addSection(oc, SECTIONKIND_RWDATA,
3001 (void*) (image + sections[i].offset),
3002 (void*) (image + sections[i].offset + sections[i].size));
3005 // count external symbols defined here
3007 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3009 if((nlist[i].n_type & N_TYPE) == N_SECT)
3012 for(i=0;i<symLC->nsyms;i++)
3014 if((nlist[i].n_type & N_TYPE) == N_UNDF
3015 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3017 commonSize += nlist[i].n_value;
3021 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3022 "ocGetNames_MachO(oc->symbols)");
3024 // insert symbols into hash table
3025 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3027 if((nlist[i].n_type & N_TYPE) == N_SECT)
3029 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3030 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3031 sections[nlist[i].n_sect-1].offset
3032 - sections[nlist[i].n_sect-1].addr
3033 + nlist[i].n_value);
3034 oc->symbols[curSymbol++] = nm;
3038 // insert local symbols into lochash
3039 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3041 if((nlist[i].n_type & N_TYPE) == N_SECT)
3043 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3044 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3045 sections[nlist[i].n_sect-1].offset
3046 - sections[nlist[i].n_sect-1].addr
3047 + nlist[i].n_value);
3052 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3053 commonCounter = (unsigned long)commonStorage;
3054 for(i=0;i<symLC->nsyms;i++)
3056 if((nlist[i].n_type & N_TYPE) == N_UNDF
3057 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3059 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3060 unsigned long sz = nlist[i].n_value;
3062 nlist[i].n_value = commonCounter;
3064 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3065 oc->symbols[curSymbol++] = nm;
3067 commonCounter += sz;
3073 static int ocResolve_MachO(ObjectCode* oc)
3075 char *image = (char*) oc->image;
3076 struct mach_header *header = (struct mach_header*) image;
3077 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3079 struct segment_command *segLC = NULL;
3080 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3081 struct symtab_command *symLC = NULL;
3082 struct dysymtab_command *dsymLC = NULL;
3083 struct nlist *nlist;
3084 unsigned long *indirectSyms;
3086 for(i=0;i<header->ncmds;i++)
3088 if(lc->cmd == LC_SEGMENT)
3089 segLC = (struct segment_command*) lc;
3090 else if(lc->cmd == LC_SYMTAB)
3091 symLC = (struct symtab_command*) lc;
3092 else if(lc->cmd == LC_DYSYMTAB)
3093 dsymLC = (struct dysymtab_command*) lc;
3094 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3097 sections = (struct section*) (segLC+1);
3098 nlist = (struct nlist*) (image + symLC->symoff);
3100 for(i=0;i<segLC->nsects;i++)
3102 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3103 la_ptrs = §ions[i];
3104 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3105 nl_ptrs = §ions[i];
3108 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3111 resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist);
3113 resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist);
3115 for(i=0;i<segLC->nsects;i++)
3117 relocateSection(image,symLC,nlist,sections,§ions[i]);
3120 /* Free the local symbol table; we won't need it again. */
3121 freeHashTable(oc->lochash, NULL);