1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.103 2002/09/13 15:02:50 simonpj 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
66 #elif defined(darwin_TARGET_OS)
67 # define OBJFORMAT_MACHO
68 # include <mach-o/loader.h>
69 # include <mach-o/nlist.h>
70 # include <mach-o/reloc.h>
73 /* Hash table mapping symbol names to Symbol */
74 static /*Str*/HashTable *symhash;
76 #if defined(OBJFORMAT_ELF)
77 static int ocVerifyImage_ELF ( ObjectCode* oc );
78 static int ocGetNames_ELF ( ObjectCode* oc );
79 static int ocResolve_ELF ( ObjectCode* oc );
80 #elif defined(OBJFORMAT_PEi386)
81 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
82 static int ocGetNames_PEi386 ( ObjectCode* oc );
83 static int ocResolve_PEi386 ( ObjectCode* oc );
84 #elif defined(OBJFORMAT_MACHO)
85 static int ocVerifyImage_MachO ( ObjectCode* oc );
86 static int ocGetNames_MachO ( ObjectCode* oc );
87 static int ocResolve_MachO ( ObjectCode* oc );
90 /* -----------------------------------------------------------------------------
91 * Built-in symbols from the RTS
94 typedef struct _RtsSymbolVal {
101 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
103 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
104 SymX(makeStableNamezh_fast) \
105 SymX(finalizzeWeakzh_fast)
107 /* These are not available in GUM!!! -- HWL */
108 #define Maybe_ForeignObj
109 #define Maybe_Stable_Names
112 #if !defined (mingw32_TARGET_OS)
113 #define RTS_POSIX_ONLY_SYMBOLS \
114 SymX(stg_sig_install) \
118 #if defined (cygwin32_TARGET_OS)
119 #define RTS_MINGW_ONLY_SYMBOLS /**/
120 /* Don't have the ability to read import libs / archives, so
121 * we have to stupidly list a lot of what libcygwin.a
124 #define RTS_CYGWIN_ONLY_SYMBOLS \
206 #elif !defined(mingw32_TARGET_OS)
207 #define RTS_MINGW_ONLY_SYMBOLS /**/
208 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
209 #else /* defined(mingw32_TARGET_OS) */
210 #define RTS_POSIX_ONLY_SYMBOLS /**/
211 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
213 /* These are statically linked from the mingw libraries into the ghc
214 executable, so we have to employ this hack. */
215 #define RTS_MINGW_ONLY_SYMBOLS \
227 SymX(getservbyname) \
228 SymX(getservbyport) \
229 SymX(getprotobynumber) \
230 SymX(getprotobyname) \
231 SymX(gethostbyname) \
232 SymX(gethostbyaddr) \
267 Sym(_imp___timezone) \
283 # define MAIN_CAP_SYM SymX(MainCapability)
285 # define MAIN_CAP_SYM
288 #define RTS_SYMBOLS \
302 Sym(stg_enterStackTop) \
305 SymX(__stg_gc_enter_1) \
306 SymX(stg_gc_enter_2) \
307 SymX(stg_gc_enter_3) \
308 SymX(stg_gc_enter_4) \
309 SymX(stg_gc_enter_5) \
310 SymX(stg_gc_enter_6) \
311 SymX(stg_gc_enter_7) \
312 SymX(stg_gc_enter_8) \
314 SymX(stg_gc_noregs) \
316 SymX(stg_gc_unbx_r1) \
317 SymX(stg_gc_unpt_r1) \
318 SymX(stg_gc_ut_0_1) \
319 SymX(stg_gc_ut_1_0) \
321 SymX(stg_yield_to_interpreter) \
324 SymX(MallocFailHook) \
325 SymX(NoRunnableThreadsHook) \
327 SymX(OutOfHeapHook) \
328 SymX(PatErrorHdrHook) \
329 SymX(PostTraceHook) \
331 SymX(StackOverflowHook) \
332 SymX(__encodeDouble) \
333 SymX(__encodeFloat) \
336 SymX(__gmpz_cmp_si) \
337 SymX(__gmpz_cmp_ui) \
338 SymX(__gmpz_get_si) \
339 SymX(__gmpz_get_ui) \
340 SymX(__int_encodeDouble) \
341 SymX(__int_encodeFloat) \
342 SymX(andIntegerzh_fast) \
343 SymX(blockAsyncExceptionszh_fast) \
346 SymX(complementIntegerzh_fast) \
347 SymX(cmpIntegerzh_fast) \
348 SymX(cmpIntegerIntzh_fast) \
349 SymX(createAdjustor) \
350 SymX(decodeDoublezh_fast) \
351 SymX(decodeFloatzh_fast) \
354 SymX(deRefWeakzh_fast) \
355 SymX(deRefStablePtrzh_fast) \
356 SymX(divExactIntegerzh_fast) \
357 SymX(divModIntegerzh_fast) \
359 SymX(forkProcesszh_fast) \
360 SymX(freeHaskellFunctionPtr) \
361 SymX(freeStablePtr) \
362 SymX(gcdIntegerzh_fast) \
363 SymX(gcdIntegerIntzh_fast) \
364 SymX(gcdIntzh_fast) \
367 SymX(int2Integerzh_fast) \
368 SymX(integer2Intzh_fast) \
369 SymX(integer2Wordzh_fast) \
370 SymX(isDoubleDenormalized) \
371 SymX(isDoubleInfinite) \
373 SymX(isDoubleNegativeZero) \
374 SymX(isEmptyMVarzh_fast) \
375 SymX(isFloatDenormalized) \
376 SymX(isFloatInfinite) \
378 SymX(isFloatNegativeZero) \
379 SymX(killThreadzh_fast) \
380 SymX(makeStablePtrzh_fast) \
381 SymX(minusIntegerzh_fast) \
382 SymX(mkApUpd0zh_fast) \
383 SymX(myThreadIdzh_fast) \
384 SymX(labelThreadzh_fast) \
385 SymX(newArrayzh_fast) \
386 SymX(newBCOzh_fast) \
387 SymX(newByteArrayzh_fast) \
389 SymX(newMVarzh_fast) \
390 SymX(newMutVarzh_fast) \
391 SymX(newPinnedByteArrayzh_fast) \
392 SymX(orIntegerzh_fast) \
394 SymX(plusIntegerzh_fast) \
397 SymX(putMVarzh_fast) \
398 SymX(quotIntegerzh_fast) \
399 SymX(quotRemIntegerzh_fast) \
401 SymX(remIntegerzh_fast) \
402 SymX(resetNonBlockingFd) \
405 SymX(rts_checkSchedStatus) \
408 SymX(rts_evalLazyIO) \
412 SymX(rts_getDouble) \
417 SymX(rts_getStablePtr) \
418 SymX(rts_getThreadId) \
420 SymX(rts_getWord32) \
431 SymX(rts_mkStablePtr) \
440 SymX(shutdownHaskellAndExit) \
441 SymX(stable_ptr_table) \
442 SymX(stackOverflow) \
443 SymX(stg_CAF_BLACKHOLE_info) \
444 SymX(stg_CHARLIKE_closure) \
445 SymX(stg_EMPTY_MVAR_info) \
446 SymX(stg_IND_STATIC_info) \
447 SymX(stg_INTLIKE_closure) \
448 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
449 SymX(stg_WEAK_info) \
450 SymX(stg_ap_1_upd_info) \
451 SymX(stg_ap_2_upd_info) \
452 SymX(stg_ap_3_upd_info) \
453 SymX(stg_ap_4_upd_info) \
454 SymX(stg_ap_5_upd_info) \
455 SymX(stg_ap_6_upd_info) \
456 SymX(stg_ap_7_upd_info) \
457 SymX(stg_ap_8_upd_info) \
459 SymX(stg_sel_0_upd_info) \
460 SymX(stg_sel_10_upd_info) \
461 SymX(stg_sel_11_upd_info) \
462 SymX(stg_sel_12_upd_info) \
463 SymX(stg_sel_13_upd_info) \
464 SymX(stg_sel_14_upd_info) \
465 SymX(stg_sel_15_upd_info) \
466 SymX(stg_sel_1_upd_info) \
467 SymX(stg_sel_2_upd_info) \
468 SymX(stg_sel_3_upd_info) \
469 SymX(stg_sel_4_upd_info) \
470 SymX(stg_sel_5_upd_info) \
471 SymX(stg_sel_6_upd_info) \
472 SymX(stg_sel_7_upd_info) \
473 SymX(stg_sel_8_upd_info) \
474 SymX(stg_sel_9_upd_info) \
475 SymX(stg_seq_frame_info) \
476 SymX(stg_upd_frame_info) \
477 SymX(__stg_update_PAP) \
478 SymX(suspendThread) \
479 SymX(takeMVarzh_fast) \
480 SymX(timesIntegerzh_fast) \
481 SymX(tryPutMVarzh_fast) \
482 SymX(tryTakeMVarzh_fast) \
483 SymX(unblockAsyncExceptionszh_fast) \
484 SymX(unsafeThawArrayzh_fast) \
485 SymX(waitReadzh_fast) \
486 SymX(waitWritezh_fast) \
487 SymX(word2Integerzh_fast) \
488 SymX(xorIntegerzh_fast) \
491 #ifdef SUPPORT_LONG_LONGS
492 #define RTS_LONG_LONG_SYMS \
493 SymX(int64ToIntegerzh_fast) \
494 SymX(word64ToIntegerzh_fast)
496 #define RTS_LONG_LONG_SYMS /* nothing */
499 #ifdef ia64_TARGET_ARCH
500 /* force these symbols to be present */
501 #define RTS_EXTRA_SYMBOLS \
503 #elif defined(powerpc_TARGET_ARCH)
504 #define RTS_EXTRA_SYMBOLS \
514 #define RTS_EXTRA_SYMBOLS /* nothing */
517 /* entirely bogus claims about types of these symbols */
518 #define Sym(vvv) extern void (vvv);
519 #define SymX(vvv) /**/
523 RTS_POSIX_ONLY_SYMBOLS
524 RTS_MINGW_ONLY_SYMBOLS
525 RTS_CYGWIN_ONLY_SYMBOLS
529 #ifdef LEADING_UNDERSCORE
530 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
532 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
535 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
537 #define SymX(vvv) Sym(vvv)
539 static RtsSymbolVal rtsSyms[] = {
543 RTS_POSIX_ONLY_SYMBOLS
544 RTS_MINGW_ONLY_SYMBOLS
545 RTS_CYGWIN_ONLY_SYMBOLS
546 { 0, 0 } /* sentinel */
549 /* -----------------------------------------------------------------------------
550 * Insert symbols into hash tables, checking for duplicates.
552 static void ghciInsertStrHashTable ( char* obj_name,
558 if (lookupHashTable(table, (StgWord)key) == NULL)
560 insertStrHashTable(table, (StgWord)key, data);
565 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
567 "whilst processing object file\n"
569 "This could be caused by:\n"
570 " * Loading two different object files which export the same symbol\n"
571 " * Specifying the same object file twice on the GHCi command line\n"
572 " * An incorrect `package.conf' entry, causing some object to be\n"
574 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
583 /* -----------------------------------------------------------------------------
584 * initialize the object linker
588 static int linker_init_done = 0 ;
590 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
591 static void *dl_prog_handle;
599 /* Make initLinker idempotent, so we can call it
600 before evey relevant operation; that means we
601 don't need to initialise the linker separately */
602 if (linker_init_done == 1) { return; } else {
603 linker_init_done = 1;
606 symhash = allocStrHashTable();
608 /* populate the symbol table with stuff from the RTS */
609 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
610 ghciInsertStrHashTable("(GHCi built-in symbols)",
611 symhash, sym->lbl, sym->addr);
613 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
614 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
618 /* -----------------------------------------------------------------------------
619 * Loading DLL or .so dynamic libraries
620 * -----------------------------------------------------------------------------
622 * Add a DLL from which symbols may be found. In the ELF case, just
623 * do RTLD_GLOBAL-style add, so no further messing around needs to
624 * happen in order that symbols in the loaded .so are findable --
625 * lookupSymbol() will subsequently see them by dlsym on the program's
626 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
628 * In the PEi386 case, open the DLLs and put handles to them in a
629 * linked list. When looking for a symbol, try all handles in the
630 * list. This means that we need to load even DLLs that are guaranteed
631 * to be in the ghc.exe image already, just so we can get a handle
632 * to give to loadSymbol, so that we can find the symbols. For such
633 * libraries, the LoadLibrary call should be a no-op except for returning
638 #if defined(OBJFORMAT_PEi386)
639 /* A record for storing handles into DLLs. */
644 struct _OpenedDLL* next;
649 /* A list thereof. */
650 static OpenedDLL* opened_dlls = NULL;
654 addDLL( char *dll_name )
656 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
657 /* ------------------- ELF DLL loader ------------------- */
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)
675 /* ------------------- Win32 DLL loader ------------------- */
683 /* fprintf(stderr, "\naddDLL; dll_name = `%s'\n", dll_name); */
685 /* See if we've already got it, and ignore if so. */
686 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
687 if (0 == strcmp(o_dll->name, dll_name))
691 /* The file name has no suffix (yet) so that we can try
692 both foo.dll and foo.drv
694 The documentation for LoadLibrary says:
695 If no file name extension is specified in the lpFileName
696 parameter, the default library extension .dll is
697 appended. However, the file name string can include a trailing
698 point character (.) to indicate that the module name has no
701 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
702 sprintf(buf, "%s.DLL", dll_name);
703 instance = LoadLibrary(buf);
704 if (instance == NULL) {
705 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
706 instance = LoadLibrary(buf);
707 if (instance == NULL) {
710 /* LoadLibrary failed; return a ptr to the error msg. */
711 return "addDLL: unknown error";
716 /* Add this DLL to the list of DLLs in which to search for symbols. */
717 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
718 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
719 strcpy(o_dll->name, dll_name);
720 o_dll->instance = instance;
721 o_dll->next = opened_dlls;
726 barf("addDLL: not implemented on this platform");
730 /* -----------------------------------------------------------------------------
731 * lookup a symbol in the hash table
734 lookupSymbol( char *lbl )
738 ASSERT(symhash != NULL);
739 val = lookupStrHashTable(symhash, lbl);
742 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
743 return dlsym(dl_prog_handle, lbl);
744 # elif defined(OBJFORMAT_PEi386)
747 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
748 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
750 /* HACK: if the name has an initial underscore, try stripping
751 it off & look that up first. I've yet to verify whether there's
752 a Rule that governs whether an initial '_' *should always* be
753 stripped off when mapping from import lib name to the DLL name.
755 sym = GetProcAddress(o_dll->instance, (lbl+1));
757 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
761 sym = GetProcAddress(o_dll->instance, lbl);
763 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
778 __attribute((unused))
780 lookupLocalSymbol( ObjectCode* oc, char *lbl )
784 val = lookupStrHashTable(oc->lochash, lbl);
794 /* -----------------------------------------------------------------------------
795 * Debugging aid: look in GHCi's object symbol tables for symbols
796 * within DELTA bytes of the specified address, and show their names.
799 void ghci_enquire ( char* addr );
801 void ghci_enquire ( char* addr )
806 const int DELTA = 64;
811 for (oc = objects; oc; oc = oc->next) {
812 for (i = 0; i < oc->n_symbols; i++) {
813 sym = oc->symbols[i];
814 if (sym == NULL) continue;
815 /* fprintf(stderr, "enquire %p %p\n", sym, oc->lochash); */
817 if (oc->lochash != NULL)
818 a = lookupStrHashTable(oc->lochash, sym);
820 a = lookupStrHashTable(symhash, sym);
822 /* fprintf(stderr, "ghci_enquire: can't find %s\n", sym); */
824 else if (addr-DELTA <= a && a <= addr+DELTA) {
825 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
832 #ifdef ia64_TARGET_ARCH
833 static unsigned int PLTSize(void);
836 /* -----------------------------------------------------------------------------
837 * Load an obj (populate the global symbol table, but don't resolve yet)
839 * Returns: 1 if ok, 0 on error.
842 loadObj( char *path )
856 /* fprintf(stderr, "loadObj %s\n", path ); */
858 /* Check that we haven't already loaded this object. Don't give up
859 at this stage; ocGetNames_* will barf later. */
863 for (o = objects; o; o = o->next) {
864 if (0 == strcmp(o->fileName, path))
870 "GHCi runtime linker: warning: looks like you're trying to load the\n"
871 "same object file twice:\n"
873 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
879 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
881 # if defined(OBJFORMAT_ELF)
882 oc->formatName = "ELF";
883 # elif defined(OBJFORMAT_PEi386)
884 oc->formatName = "PEi386";
885 # elif defined(OBJFORMAT_MACHO)
886 oc->formatName = "Mach-O";
889 barf("loadObj: not implemented on this platform");
893 if (r == -1) { return 0; }
895 /* sigh, strdup() isn't a POSIX function, so do it the long way */
896 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
897 strcpy(oc->fileName, path);
899 oc->fileSize = st.st_size;
902 oc->lochash = allocStrHashTable();
903 oc->proddables = NULL;
905 /* chain it onto the list of objects */
910 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
912 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
914 fd = open(path, O_RDONLY);
916 barf("loadObj: can't open `%s'", path);
918 pagesize = getpagesize();
920 #ifdef ia64_TARGET_ARCH
921 /* The PLT needs to be right before the object */
922 n = ROUND_UP(PLTSize(), pagesize);
923 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
924 if (oc->plt == MAP_FAILED)
925 barf("loadObj: can't allocate PLT");
928 map_addr = oc->plt + n;
931 n = ROUND_UP(oc->fileSize, pagesize);
932 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
933 if (oc->image == MAP_FAILED)
934 barf("loadObj: can't map `%s'", path);
938 #else /* !USE_MMAP */
940 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
942 /* load the image into memory */
943 f = fopen(path, "rb");
945 barf("loadObj: can't read `%s'", path);
947 n = fread ( oc->image, 1, oc->fileSize, f );
948 if (n != oc->fileSize)
949 barf("loadObj: error whilst reading `%s'", path);
953 #endif /* USE_MMAP */
955 /* verify the in-memory image */
956 # if defined(OBJFORMAT_ELF)
957 r = ocVerifyImage_ELF ( oc );
958 # elif defined(OBJFORMAT_PEi386)
959 r = ocVerifyImage_PEi386 ( oc );
960 # elif defined(OBJFORMAT_MACHO)
961 r = ocVerifyImage_MachO ( oc );
963 barf("loadObj: no verify method");
965 if (!r) { return r; }
967 /* build the symbol list for this image */
968 # if defined(OBJFORMAT_ELF)
969 r = ocGetNames_ELF ( oc );
970 # elif defined(OBJFORMAT_PEi386)
971 r = ocGetNames_PEi386 ( oc );
972 # elif defined(OBJFORMAT_MACHO)
973 r = ocGetNames_MachO ( oc );
975 barf("loadObj: no getNames method");
977 if (!r) { return r; }
979 /* loaded, but not resolved yet */
980 oc->status = OBJECT_LOADED;
985 /* -----------------------------------------------------------------------------
986 * resolve all the currently unlinked objects in memory
988 * Returns: 1 if ok, 0 on error.
998 for (oc = objects; oc; oc = oc->next) {
999 if (oc->status != OBJECT_RESOLVED) {
1000 # if defined(OBJFORMAT_ELF)
1001 r = ocResolve_ELF ( oc );
1002 # elif defined(OBJFORMAT_PEi386)
1003 r = ocResolve_PEi386 ( oc );
1004 # elif defined(OBJFORMAT_MACHO)
1005 r = ocResolve_MachO ( oc );
1007 barf("resolveObjs: not implemented on this platform");
1009 if (!r) { return r; }
1010 oc->status = OBJECT_RESOLVED;
1016 /* -----------------------------------------------------------------------------
1017 * delete an object from the pool
1020 unloadObj( char *path )
1022 ObjectCode *oc, *prev;
1024 ASSERT(symhash != NULL);
1025 ASSERT(objects != NULL);
1030 for (oc = objects; oc; prev = oc, oc = oc->next) {
1031 if (!strcmp(oc->fileName,path)) {
1033 /* Remove all the mappings for the symbols within this
1038 for (i = 0; i < oc->n_symbols; i++) {
1039 if (oc->symbols[i] != NULL) {
1040 removeStrHashTable(symhash, oc->symbols[i], NULL);
1048 prev->next = oc->next;
1051 /* We're going to leave this in place, in case there are
1052 any pointers from the heap into it: */
1053 /* free(oc->image); */
1057 /* The local hash table should have been freed at the end
1058 of the ocResolve_ call on it. */
1059 ASSERT(oc->lochash == NULL);
1065 belch("unloadObj: can't find `%s' to unload", path);
1069 /* -----------------------------------------------------------------------------
1070 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1071 * which may be prodded during relocation, and abort if we try and write
1072 * outside any of these.
1074 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1077 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1078 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1082 pb->next = oc->proddables;
1083 oc->proddables = pb;
1086 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1089 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1090 char* s = (char*)(pb->start);
1091 char* e = s + pb->size - 1;
1092 char* a = (char*)addr;
1093 /* Assumes that the biggest fixup involves a 4-byte write. This
1094 probably needs to be changed to 8 (ie, +7) on 64-bit
1096 if (a >= s && (a+3) <= e) return;
1098 barf("checkProddableBlock: invalid fixup in runtime linker");
1101 /* -----------------------------------------------------------------------------
1102 * Section management.
1104 static void addSection ( ObjectCode* oc, SectionKind kind,
1105 void* start, void* end )
1107 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1111 s->next = oc->sections;
1114 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1115 start, ((char*)end)-1, end - start + 1, kind );
1121 /* --------------------------------------------------------------------------
1122 * PEi386 specifics (Win32 targets)
1123 * ------------------------------------------------------------------------*/
1125 /* The information for this linker comes from
1126 Microsoft Portable Executable
1127 and Common Object File Format Specification
1128 revision 5.1 January 1998
1129 which SimonM says comes from the MS Developer Network CDs.
1131 It can be found there (on older CDs), but can also be found
1134 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1136 (this is Rev 6.0 from February 1999).
1138 Things move, so if that fails, try searching for it via
1140 http://www.google.com/search?q=PE+COFF+specification
1142 The ultimate reference for the PE format is the Winnt.h
1143 header file that comes with the Platform SDKs; as always,
1144 implementations will drift wrt their documentation.
1146 A good background article on the PE format is Matt Pietrek's
1147 March 1994 article in Microsoft System Journal (MSJ)
1148 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1149 Win32 Portable Executable File Format." The info in there
1150 has recently been updated in a two part article in
1151 MSDN magazine, issues Feb and March 2002,
1152 "Inside Windows: An In-Depth Look into the Win32 Portable
1153 Executable File Format"
1155 John Levine's book "Linkers and Loaders" contains useful
1160 #if defined(OBJFORMAT_PEi386)
1164 typedef unsigned char UChar;
1165 typedef unsigned short UInt16;
1166 typedef unsigned int UInt32;
1173 UInt16 NumberOfSections;
1174 UInt32 TimeDateStamp;
1175 UInt32 PointerToSymbolTable;
1176 UInt32 NumberOfSymbols;
1177 UInt16 SizeOfOptionalHeader;
1178 UInt16 Characteristics;
1182 #define sizeof_COFF_header 20
1189 UInt32 VirtualAddress;
1190 UInt32 SizeOfRawData;
1191 UInt32 PointerToRawData;
1192 UInt32 PointerToRelocations;
1193 UInt32 PointerToLinenumbers;
1194 UInt16 NumberOfRelocations;
1195 UInt16 NumberOfLineNumbers;
1196 UInt32 Characteristics;
1200 #define sizeof_COFF_section 40
1207 UInt16 SectionNumber;
1210 UChar NumberOfAuxSymbols;
1214 #define sizeof_COFF_symbol 18
1219 UInt32 VirtualAddress;
1220 UInt32 SymbolTableIndex;
1225 #define sizeof_COFF_reloc 10
1228 /* From PE spec doc, section 3.3.2 */
1229 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1230 windows.h -- for the same purpose, but I want to know what I'm
1232 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1233 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1234 #define MYIMAGE_FILE_DLL 0x2000
1235 #define MYIMAGE_FILE_SYSTEM 0x1000
1236 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1237 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1238 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1240 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1241 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1242 #define MYIMAGE_SYM_CLASS_STATIC 3
1243 #define MYIMAGE_SYM_UNDEFINED 0
1245 /* From PE spec doc, section 4.1 */
1246 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1247 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1248 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1250 /* From PE spec doc, section 5.2.1 */
1251 #define MYIMAGE_REL_I386_DIR32 0x0006
1252 #define MYIMAGE_REL_I386_REL32 0x0014
1255 /* We use myindex to calculate array addresses, rather than
1256 simply doing the normal subscript thing. That's because
1257 some of the above structs have sizes which are not
1258 a whole number of words. GCC rounds their sizes up to a
1259 whole number of words, which means that the address calcs
1260 arising from using normal C indexing or pointer arithmetic
1261 are just plain wrong. Sigh.
1264 myindex ( int scale, void* base, int index )
1267 ((UChar*)base) + scale * index;
1272 printName ( UChar* name, UChar* strtab )
1274 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1275 UInt32 strtab_offset = * (UInt32*)(name+4);
1276 fprintf ( stderr, "%s", strtab + strtab_offset );
1279 for (i = 0; i < 8; i++) {
1280 if (name[i] == 0) break;
1281 fprintf ( stderr, "%c", name[i] );
1288 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1290 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1291 UInt32 strtab_offset = * (UInt32*)(name+4);
1292 strncpy ( dst, strtab+strtab_offset, dstSize );
1298 if (name[i] == 0) break;
1308 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1311 /* If the string is longer than 8 bytes, look in the
1312 string table for it -- this will be correctly zero terminated.
1314 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1315 UInt32 strtab_offset = * (UInt32*)(name+4);
1316 return ((UChar*)strtab) + strtab_offset;
1318 /* Otherwise, if shorter than 8 bytes, return the original,
1319 which by defn is correctly terminated.
1321 if (name[7]==0) return name;
1322 /* The annoying case: 8 bytes. Copy into a temporary
1323 (which is never freed ...)
1325 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1327 strncpy(newstr,name,8);
1333 /* Just compares the short names (first 8 chars) */
1334 static COFF_section *
1335 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1339 = (COFF_header*)(oc->image);
1340 COFF_section* sectab
1342 ((UChar*)(oc->image))
1343 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1345 for (i = 0; i < hdr->NumberOfSections; i++) {
1348 COFF_section* section_i
1350 myindex ( sizeof_COFF_section, sectab, i );
1351 n1 = (UChar*) &(section_i->Name);
1353 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1354 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1355 n1[6]==n2[6] && n1[7]==n2[7])
1364 zapTrailingAtSign ( UChar* sym )
1366 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1368 if (sym[0] == 0) return;
1370 while (sym[i] != 0) i++;
1373 while (j > 0 && my_isdigit(sym[j])) j--;
1374 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1380 ocVerifyImage_PEi386 ( ObjectCode* oc )
1385 COFF_section* sectab;
1386 COFF_symbol* symtab;
1388 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1389 hdr = (COFF_header*)(oc->image);
1390 sectab = (COFF_section*) (
1391 ((UChar*)(oc->image))
1392 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1394 symtab = (COFF_symbol*) (
1395 ((UChar*)(oc->image))
1396 + hdr->PointerToSymbolTable
1398 strtab = ((UChar*)symtab)
1399 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1401 if (hdr->Machine != 0x14c) {
1402 belch("Not x86 PEi386");
1405 if (hdr->SizeOfOptionalHeader != 0) {
1406 belch("PEi386 with nonempty optional header");
1409 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1410 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1411 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1412 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1413 belch("Not a PEi386 object file");
1416 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1417 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1418 belch("Invalid PEi386 word size or endiannness: %d",
1419 (int)(hdr->Characteristics));
1422 /* If the string table size is way crazy, this might indicate that
1423 there are more than 64k relocations, despite claims to the
1424 contrary. Hence this test. */
1425 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1427 if ( (*(UInt32*)strtab) > 600000 ) {
1428 /* Note that 600k has no special significance other than being
1429 big enough to handle the almost-2MB-sized lumps that
1430 constitute HSwin32*.o. */
1431 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1436 /* No further verification after this point; only debug printing. */
1438 IF_DEBUG(linker, i=1);
1439 if (i == 0) return 1;
1442 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1444 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1446 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1448 fprintf ( stderr, "\n" );
1450 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1452 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1454 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1456 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1458 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1460 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1462 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1464 /* Print the section table. */
1465 fprintf ( stderr, "\n" );
1466 for (i = 0; i < hdr->NumberOfSections; i++) {
1468 COFF_section* sectab_i
1470 myindex ( sizeof_COFF_section, sectab, i );
1477 printName ( sectab_i->Name, strtab );
1487 sectab_i->VirtualSize,
1488 sectab_i->VirtualAddress,
1489 sectab_i->SizeOfRawData,
1490 sectab_i->PointerToRawData,
1491 sectab_i->NumberOfRelocations,
1492 sectab_i->PointerToRelocations,
1493 sectab_i->PointerToRawData
1495 reltab = (COFF_reloc*) (
1496 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1499 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1500 /* If the relocation field (a short) has overflowed, the
1501 * real count can be found in the first reloc entry.
1503 * See Section 4.1 (last para) of the PE spec (rev6.0).
1505 COFF_reloc* rel = (COFF_reloc*)
1506 myindex ( sizeof_COFF_reloc, reltab, 0 );
1507 noRelocs = rel->VirtualAddress;
1510 noRelocs = sectab_i->NumberOfRelocations;
1514 for (; j < noRelocs; j++) {
1516 COFF_reloc* rel = (COFF_reloc*)
1517 myindex ( sizeof_COFF_reloc, reltab, j );
1519 " type 0x%-4x vaddr 0x%-8x name `",
1521 rel->VirtualAddress );
1522 sym = (COFF_symbol*)
1523 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1524 /* Hmm..mysterious looking offset - what's it for? SOF */
1525 printName ( sym->Name, strtab -10 );
1526 fprintf ( stderr, "'\n" );
1529 fprintf ( stderr, "\n" );
1531 fprintf ( stderr, "\n" );
1532 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1533 fprintf ( stderr, "---START of string table---\n");
1534 for (i = 4; i < *(Int32*)strtab; i++) {
1536 fprintf ( stderr, "\n"); else
1537 fprintf( stderr, "%c", strtab[i] );
1539 fprintf ( stderr, "--- END of string table---\n");
1541 fprintf ( stderr, "\n" );
1544 COFF_symbol* symtab_i;
1545 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1546 symtab_i = (COFF_symbol*)
1547 myindex ( sizeof_COFF_symbol, symtab, i );
1553 printName ( symtab_i->Name, strtab );
1562 (Int32)(symtab_i->SectionNumber),
1563 (UInt32)symtab_i->Type,
1564 (UInt32)symtab_i->StorageClass,
1565 (UInt32)symtab_i->NumberOfAuxSymbols
1567 i += symtab_i->NumberOfAuxSymbols;
1571 fprintf ( stderr, "\n" );
1577 ocGetNames_PEi386 ( ObjectCode* oc )
1580 COFF_section* sectab;
1581 COFF_symbol* symtab;
1588 hdr = (COFF_header*)(oc->image);
1589 sectab = (COFF_section*) (
1590 ((UChar*)(oc->image))
1591 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1593 symtab = (COFF_symbol*) (
1594 ((UChar*)(oc->image))
1595 + hdr->PointerToSymbolTable
1597 strtab = ((UChar*)(oc->image))
1598 + hdr->PointerToSymbolTable
1599 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1601 /* Allocate space for any (local, anonymous) .bss sections. */
1603 for (i = 0; i < hdr->NumberOfSections; i++) {
1605 COFF_section* sectab_i
1607 myindex ( sizeof_COFF_section, sectab, i );
1608 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1609 if (sectab_i->VirtualSize == 0) continue;
1610 /* This is a non-empty .bss section. Allocate zeroed space for
1611 it, and set its PointerToRawData field such that oc->image +
1612 PointerToRawData == addr_of_zeroed_space. */
1613 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1614 "ocGetNames_PEi386(anonymous bss)");
1615 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1616 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1617 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1620 /* Copy section information into the ObjectCode. */
1622 for (i = 0; i < hdr->NumberOfSections; i++) {
1628 = SECTIONKIND_OTHER;
1629 COFF_section* sectab_i
1631 myindex ( sizeof_COFF_section, sectab, i );
1632 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1635 /* I'm sure this is the Right Way to do it. However, the
1636 alternative of testing the sectab_i->Name field seems to
1637 work ok with Cygwin.
1639 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1640 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1641 kind = SECTIONKIND_CODE_OR_RODATA;
1644 if (0==strcmp(".text",sectab_i->Name) ||
1645 0==strcmp(".rodata",sectab_i->Name))
1646 kind = SECTIONKIND_CODE_OR_RODATA;
1647 if (0==strcmp(".data",sectab_i->Name) ||
1648 0==strcmp(".bss",sectab_i->Name))
1649 kind = SECTIONKIND_RWDATA;
1651 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1652 sz = sectab_i->SizeOfRawData;
1653 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1655 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1656 end = start + sz - 1;
1658 if (kind == SECTIONKIND_OTHER
1659 /* Ignore sections called which contain stabs debugging
1661 && 0 != strcmp(".stab", sectab_i->Name)
1662 && 0 != strcmp(".stabstr", sectab_i->Name)
1664 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1668 if (kind != SECTIONKIND_OTHER && end >= start) {
1669 addSection(oc, kind, start, end);
1670 addProddableBlock(oc, start, end - start + 1);
1674 /* Copy exported symbols into the ObjectCode. */
1676 oc->n_symbols = hdr->NumberOfSymbols;
1677 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1678 "ocGetNames_PEi386(oc->symbols)");
1679 /* Call me paranoid; I don't care. */
1680 for (i = 0; i < oc->n_symbols; i++)
1681 oc->symbols[i] = NULL;
1685 COFF_symbol* symtab_i;
1686 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1687 symtab_i = (COFF_symbol*)
1688 myindex ( sizeof_COFF_symbol, symtab, i );
1692 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1693 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1694 /* This symbol is global and defined, viz, exported */
1695 /* for MYIMAGE_SYMCLASS_EXTERNAL
1696 && !MYIMAGE_SYM_UNDEFINED,
1697 the address of the symbol is:
1698 address of relevant section + offset in section
1700 COFF_section* sectabent
1701 = (COFF_section*) myindex ( sizeof_COFF_section,
1703 symtab_i->SectionNumber-1 );
1704 addr = ((UChar*)(oc->image))
1705 + (sectabent->PointerToRawData
1709 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1710 && symtab_i->Value > 0) {
1711 /* This symbol isn't in any section at all, ie, global bss.
1712 Allocate zeroed space for it. */
1713 addr = stgCallocBytes(1, symtab_i->Value,
1714 "ocGetNames_PEi386(non-anonymous bss)");
1715 addSection(oc, SECTIONKIND_RWDATA, addr,
1716 ((UChar*)addr) + symtab_i->Value - 1);
1717 addProddableBlock(oc, addr, symtab_i->Value);
1718 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1721 if (addr != NULL ) {
1722 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1723 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1724 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1725 ASSERT(i >= 0 && i < oc->n_symbols);
1726 /* cstring_from_COFF_symbol_name always succeeds. */
1727 oc->symbols[i] = sname;
1728 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1732 "IGNORING symbol %d\n"
1736 printName ( symtab_i->Name, strtab );
1745 (Int32)(symtab_i->SectionNumber),
1746 (UInt32)symtab_i->Type,
1747 (UInt32)symtab_i->StorageClass,
1748 (UInt32)symtab_i->NumberOfAuxSymbols
1753 i += symtab_i->NumberOfAuxSymbols;
1762 ocResolve_PEi386 ( ObjectCode* oc )
1765 COFF_section* sectab;
1766 COFF_symbol* symtab;
1776 /* ToDo: should be variable-sized? But is at least safe in the
1777 sense of buffer-overrun-proof. */
1779 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1781 hdr = (COFF_header*)(oc->image);
1782 sectab = (COFF_section*) (
1783 ((UChar*)(oc->image))
1784 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1786 symtab = (COFF_symbol*) (
1787 ((UChar*)(oc->image))
1788 + hdr->PointerToSymbolTable
1790 strtab = ((UChar*)(oc->image))
1791 + hdr->PointerToSymbolTable
1792 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1794 for (i = 0; i < hdr->NumberOfSections; i++) {
1795 COFF_section* sectab_i
1797 myindex ( sizeof_COFF_section, sectab, i );
1800 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1803 /* Ignore sections called which contain stabs debugging
1805 if (0 == strcmp(".stab", sectab_i->Name)
1806 || 0 == strcmp(".stabstr", sectab_i->Name))
1809 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1810 /* If the relocation field (a short) has overflowed, the
1811 * real count can be found in the first reloc entry.
1813 * See Section 4.1 (last para) of the PE spec (rev6.0).
1815 COFF_reloc* rel = (COFF_reloc*)
1816 myindex ( sizeof_COFF_reloc, reltab, 0 );
1817 noRelocs = rel->VirtualAddress;
1818 fprintf(stderr, "Overflown relocs: %u\n", noRelocs);
1821 noRelocs = sectab_i->NumberOfRelocations;
1826 for (; j < noRelocs; j++) {
1828 COFF_reloc* reltab_j
1830 myindex ( sizeof_COFF_reloc, reltab, j );
1832 /* the location to patch */
1834 ((UChar*)(oc->image))
1835 + (sectab_i->PointerToRawData
1836 + reltab_j->VirtualAddress
1837 - sectab_i->VirtualAddress )
1839 /* the existing contents of pP */
1841 /* the symbol to connect to */
1842 sym = (COFF_symbol*)
1843 myindex ( sizeof_COFF_symbol,
1844 symtab, reltab_j->SymbolTableIndex );
1847 "reloc sec %2d num %3d: type 0x%-4x "
1848 "vaddr 0x%-8x name `",
1850 (UInt32)reltab_j->Type,
1851 reltab_j->VirtualAddress );
1852 printName ( sym->Name, strtab );
1853 fprintf ( stderr, "'\n" ));
1855 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1856 COFF_section* section_sym
1857 = findPEi386SectionCalled ( oc, sym->Name );
1859 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1862 S = ((UInt32)(oc->image))
1863 + (section_sym->PointerToRawData
1866 copyName ( sym->Name, strtab, symbol, 1000-1 );
1867 (void*)S = lookupLocalSymbol( oc, symbol );
1868 if ((void*)S != NULL) goto foundit;
1869 (void*)S = lookupSymbol( symbol );
1870 if ((void*)S != NULL) goto foundit;
1871 zapTrailingAtSign ( symbol );
1872 (void*)S = lookupLocalSymbol( oc, symbol );
1873 if ((void*)S != NULL) goto foundit;
1874 (void*)S = lookupSymbol( symbol );
1875 if ((void*)S != NULL) goto foundit;
1876 /* Newline first because the interactive linker has printed "linking..." */
1877 belch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
1881 checkProddableBlock(oc, pP);
1882 switch (reltab_j->Type) {
1883 case MYIMAGE_REL_I386_DIR32:
1886 case MYIMAGE_REL_I386_REL32:
1887 /* Tricky. We have to insert a displacement at
1888 pP which, when added to the PC for the _next_
1889 insn, gives the address of the target (S).
1890 Problem is to know the address of the next insn
1891 when we only know pP. We assume that this
1892 literal field is always the last in the insn,
1893 so that the address of the next insn is pP+4
1894 -- hence the constant 4.
1895 Also I don't know if A should be added, but so
1896 far it has always been zero.
1899 *pP = S - ((UInt32)pP) - 4;
1902 belch("%s: unhandled PEi386 relocation type %d",
1903 oc->fileName, reltab_j->Type);
1910 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1914 #endif /* defined(OBJFORMAT_PEi386) */
1917 /* --------------------------------------------------------------------------
1919 * ------------------------------------------------------------------------*/
1921 #if defined(OBJFORMAT_ELF)
1926 #if defined(sparc_TARGET_ARCH)
1927 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1928 #elif defined(i386_TARGET_ARCH)
1929 # define ELF_TARGET_386 /* Used inside <elf.h> */
1930 #elif defined (ia64_TARGET_ARCH)
1931 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
1933 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
1934 # define ELF_NEED_GOT /* needs Global Offset Table */
1935 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
1941 * Define a set of types which can be used for both ELF32 and ELF64
1945 #define ELFCLASS ELFCLASS64
1946 #define Elf_Addr Elf64_Addr
1947 #define Elf_Word Elf64_Word
1948 #define Elf_Sword Elf64_Sword
1949 #define Elf_Ehdr Elf64_Ehdr
1950 #define Elf_Phdr Elf64_Phdr
1951 #define Elf_Shdr Elf64_Shdr
1952 #define Elf_Sym Elf64_Sym
1953 #define Elf_Rel Elf64_Rel
1954 #define Elf_Rela Elf64_Rela
1955 #define ELF_ST_TYPE ELF64_ST_TYPE
1956 #define ELF_ST_BIND ELF64_ST_BIND
1957 #define ELF_R_TYPE ELF64_R_TYPE
1958 #define ELF_R_SYM ELF64_R_SYM
1960 #define ELFCLASS ELFCLASS32
1961 #define Elf_Addr Elf32_Addr
1962 #define Elf_Word Elf32_Word
1963 #define Elf_Sword Elf32_Sword
1964 #define Elf_Ehdr Elf32_Ehdr
1965 #define Elf_Phdr Elf32_Phdr
1966 #define Elf_Shdr Elf32_Shdr
1967 #define Elf_Sym Elf32_Sym
1968 #define Elf_Rel Elf32_Rel
1969 #define Elf_Rela Elf32_Rela
1970 #define ELF_ST_TYPE ELF32_ST_TYPE
1971 #define ELF_ST_BIND ELF32_ST_BIND
1972 #define ELF_R_TYPE ELF32_R_TYPE
1973 #define ELF_R_SYM ELF32_R_SYM
1978 * Functions to allocate entries in dynamic sections. Currently we simply
1979 * preallocate a large number, and we don't check if a entry for the given
1980 * target already exists (a linear search is too slow). Ideally these
1981 * entries would be associated with symbols.
1984 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
1985 #define GOT_SIZE 0x20000
1986 #define FUNCTION_TABLE_SIZE 0x10000
1987 #define PLT_SIZE 0x08000
1990 static Elf_Addr got[GOT_SIZE];
1991 static unsigned int gotIndex;
1992 static Elf_Addr gp_val = (Elf_Addr)got;
1995 allocateGOTEntry(Elf_Addr target)
1999 if (gotIndex >= GOT_SIZE)
2000 barf("Global offset table overflow");
2002 entry = &got[gotIndex++];
2004 return (Elf_Addr)entry;
2008 #ifdef ELF_FUNCTION_DESC
2014 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2015 static unsigned int functionTableIndex;
2018 allocateFunctionDesc(Elf_Addr target)
2020 FunctionDesc *entry;
2022 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2023 barf("Function table overflow");
2025 entry = &functionTable[functionTableIndex++];
2027 entry->gp = (Elf_Addr)gp_val;
2028 return (Elf_Addr)entry;
2032 copyFunctionDesc(Elf_Addr target)
2034 FunctionDesc *olddesc = (FunctionDesc *)target;
2035 FunctionDesc *newdesc;
2037 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2038 newdesc->gp = olddesc->gp;
2039 return (Elf_Addr)newdesc;
2044 #ifdef ia64_TARGET_ARCH
2045 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2046 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2048 static unsigned char plt_code[] =
2050 /* taken from binutils bfd/elfxx-ia64.c */
2051 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2052 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2053 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2054 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2055 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2056 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2059 /* If we can't get to the function descriptor via gp, take a local copy of it */
2060 #define PLT_RELOC(code, target) { \
2061 Elf64_Sxword rel_value = target - gp_val; \
2062 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2063 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2065 ia64_reloc_gprel22((Elf_Addr)code, target); \
2070 unsigned char code[sizeof(plt_code)];
2074 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2076 PLTEntry *plt = (PLTEntry *)oc->plt;
2079 if (oc->pltIndex >= PLT_SIZE)
2080 barf("Procedure table overflow");
2082 entry = &plt[oc->pltIndex++];
2083 memcpy(entry->code, plt_code, sizeof(entry->code));
2084 PLT_RELOC(entry->code, target);
2085 return (Elf_Addr)entry;
2091 return (PLT_SIZE * sizeof(PLTEntry));
2097 * Generic ELF functions
2101 findElfSection ( void* objImage, Elf_Word sh_type )
2103 char* ehdrC = (char*)objImage;
2104 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2105 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2106 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2110 for (i = 0; i < ehdr->e_shnum; i++) {
2111 if (shdr[i].sh_type == sh_type
2112 /* Ignore the section header's string table. */
2113 && i != ehdr->e_shstrndx
2114 /* Ignore string tables named .stabstr, as they contain
2116 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2118 ptr = ehdrC + shdr[i].sh_offset;
2125 #if defined(ia64_TARGET_ARCH)
2127 findElfSegment ( void* objImage, Elf_Addr vaddr )
2129 char* ehdrC = (char*)objImage;
2130 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2131 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2132 Elf_Addr segaddr = 0;
2135 for (i = 0; i < ehdr->e_phnum; i++) {
2136 segaddr = phdr[i].p_vaddr;
2137 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2145 ocVerifyImage_ELF ( ObjectCode* oc )
2149 int i, j, nent, nstrtab, nsymtabs;
2153 char* ehdrC = (char*)(oc->image);
2154 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2156 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2157 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2158 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2159 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2160 belch("%s: not an ELF object", oc->fileName);
2164 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2165 belch("%s: unsupported ELF format", oc->fileName);
2169 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2170 IF_DEBUG(linker,belch( "Is little-endian" ));
2172 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2173 IF_DEBUG(linker,belch( "Is big-endian" ));
2175 belch("%s: unknown endiannness", oc->fileName);
2179 if (ehdr->e_type != ET_REL) {
2180 belch("%s: not a relocatable object (.o) file", oc->fileName);
2183 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2185 IF_DEBUG(linker,belch( "Architecture is " ));
2186 switch (ehdr->e_machine) {
2187 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2188 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2190 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2192 default: IF_DEBUG(linker,belch( "unknown" ));
2193 belch("%s: unknown architecture", oc->fileName);
2197 IF_DEBUG(linker,belch(
2198 "\nSection header table: start %d, n_entries %d, ent_size %d",
2199 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2201 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2203 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2205 if (ehdr->e_shstrndx == SHN_UNDEF) {
2206 belch("%s: no section header string table", oc->fileName);
2209 IF_DEBUG(linker,belch( "Section header string table is section %d",
2211 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2214 for (i = 0; i < ehdr->e_shnum; i++) {
2215 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2216 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2217 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2218 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2219 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2220 ehdrC + shdr[i].sh_offset,
2221 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2223 if (shdr[i].sh_type == SHT_REL) {
2224 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2225 } else if (shdr[i].sh_type == SHT_RELA) {
2226 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2228 IF_DEBUG(linker,fprintf(stderr," "));
2231 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2235 IF_DEBUG(linker,belch( "\nString tables" ));
2238 for (i = 0; i < ehdr->e_shnum; i++) {
2239 if (shdr[i].sh_type == SHT_STRTAB
2240 /* Ignore the section header's string table. */
2241 && i != ehdr->e_shstrndx
2242 /* Ignore string tables named .stabstr, as they contain
2244 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2246 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2247 strtab = ehdrC + shdr[i].sh_offset;
2252 belch("%s: no string tables, or too many", oc->fileName);
2257 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2258 for (i = 0; i < ehdr->e_shnum; i++) {
2259 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2260 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2262 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2263 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2264 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2266 shdr[i].sh_size % sizeof(Elf_Sym)
2268 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2269 belch("%s: non-integral number of symbol table entries", oc->fileName);
2272 for (j = 0; j < nent; j++) {
2273 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2274 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2275 (int)stab[j].st_shndx,
2276 (int)stab[j].st_size,
2277 (char*)stab[j].st_value ));
2279 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2280 switch (ELF_ST_TYPE(stab[j].st_info)) {
2281 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2282 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2283 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2284 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2285 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2286 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2288 IF_DEBUG(linker,fprintf(stderr, " " ));
2290 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2291 switch (ELF_ST_BIND(stab[j].st_info)) {
2292 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2293 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2294 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2295 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2297 IF_DEBUG(linker,fprintf(stderr, " " ));
2299 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2303 if (nsymtabs == 0) {
2304 belch("%s: didn't find any symbol tables", oc->fileName);
2313 ocGetNames_ELF ( ObjectCode* oc )
2318 char* ehdrC = (char*)(oc->image);
2319 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2320 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2321 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2323 ASSERT(symhash != NULL);
2326 belch("%s: no strtab", oc->fileName);
2331 for (i = 0; i < ehdr->e_shnum; i++) {
2332 /* Figure out what kind of section it is. Logic derived from
2333 Figure 1.14 ("Special Sections") of the ELF document
2334 ("Portable Formats Specification, Version 1.1"). */
2335 Elf_Shdr hdr = shdr[i];
2336 SectionKind kind = SECTIONKIND_OTHER;
2339 if (hdr.sh_type == SHT_PROGBITS
2340 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2341 /* .text-style section */
2342 kind = SECTIONKIND_CODE_OR_RODATA;
2345 if (hdr.sh_type == SHT_PROGBITS
2346 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2347 /* .data-style section */
2348 kind = SECTIONKIND_RWDATA;
2351 if (hdr.sh_type == SHT_PROGBITS
2352 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2353 /* .rodata-style section */
2354 kind = SECTIONKIND_CODE_OR_RODATA;
2357 if (hdr.sh_type == SHT_NOBITS
2358 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2359 /* .bss-style section */
2360 kind = SECTIONKIND_RWDATA;
2364 if (is_bss && shdr[i].sh_size > 0) {
2365 /* This is a non-empty .bss section. Allocate zeroed space for
2366 it, and set its .sh_offset field such that
2367 ehdrC + .sh_offset == addr_of_zeroed_space. */
2368 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2369 "ocGetNames_ELF(BSS)");
2370 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2372 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2373 zspace, shdr[i].sh_size);
2377 /* fill in the section info */
2378 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2379 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2380 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2381 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2384 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2386 /* copy stuff into this module's object symbol table */
2387 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2388 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2390 oc->n_symbols = nent;
2391 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2392 "ocGetNames_ELF(oc->symbols)");
2394 for (j = 0; j < nent; j++) {
2396 char isLocal = FALSE; /* avoids uninit-var warning */
2398 char* nm = strtab + stab[j].st_name;
2399 int secno = stab[j].st_shndx;
2401 /* Figure out if we want to add it; if so, set ad to its
2402 address. Otherwise leave ad == NULL. */
2404 if (secno == SHN_COMMON) {
2406 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2408 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2409 stab[j].st_size, nm);
2411 /* Pointless to do addProddableBlock() for this area,
2412 since the linker should never poke around in it. */
2415 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2416 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2418 /* and not an undefined symbol */
2419 && stab[j].st_shndx != SHN_UNDEF
2420 /* and not in a "special section" */
2421 && stab[j].st_shndx < SHN_LORESERVE
2423 /* and it's a not a section or string table or anything silly */
2424 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2425 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2426 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2429 /* Section 0 is the undefined section, hence > and not >=. */
2430 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2432 if (shdr[secno].sh_type == SHT_NOBITS) {
2433 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2434 stab[j].st_size, stab[j].st_value, nm);
2437 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2438 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2441 #ifdef ELF_FUNCTION_DESC
2442 /* dlsym() and the initialisation table both give us function
2443 * descriptors, so to be consistent we store function descriptors
2444 * in the symbol table */
2445 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2446 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2448 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2449 ad, oc->fileName, nm ));
2454 /* And the decision is ... */
2458 oc->symbols[j] = nm;
2461 /* Ignore entirely. */
2463 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2467 IF_DEBUG(linker,belch( "skipping `%s'",
2468 strtab + stab[j].st_name ));
2471 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2472 (int)ELF_ST_BIND(stab[j].st_info),
2473 (int)ELF_ST_TYPE(stab[j].st_info),
2474 (int)stab[j].st_shndx,
2475 strtab + stab[j].st_name
2478 oc->symbols[j] = NULL;
2487 /* Do ELF relocations which lack an explicit addend. All x86-linux
2488 relocations appear to be of this form. */
2490 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2491 Elf_Shdr* shdr, int shnum,
2492 Elf_Sym* stab, char* strtab )
2497 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2498 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2499 int target_shndx = shdr[shnum].sh_info;
2500 int symtab_shndx = shdr[shnum].sh_link;
2502 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2503 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2504 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2505 target_shndx, symtab_shndx ));
2507 for (j = 0; j < nent; j++) {
2508 Elf_Addr offset = rtab[j].r_offset;
2509 Elf_Addr info = rtab[j].r_info;
2511 Elf_Addr P = ((Elf_Addr)targ) + offset;
2512 Elf_Word* pP = (Elf_Word*)P;
2517 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2518 j, (void*)offset, (void*)info ));
2520 IF_DEBUG(linker,belch( " ZERO" ));
2523 Elf_Sym sym = stab[ELF_R_SYM(info)];
2524 /* First see if it is a local symbol. */
2525 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2526 /* Yes, so we can get the address directly from the ELF symbol
2528 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2530 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2531 + stab[ELF_R_SYM(info)].st_value);
2534 /* No, so look up the name in our global table. */
2535 symbol = strtab + sym.st_name;
2536 (void*)S = lookupSymbol( symbol );
2539 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2542 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2545 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2546 (void*)P, (void*)S, (void*)A ));
2547 checkProddableBlock ( oc, pP );
2551 switch (ELF_R_TYPE(info)) {
2552 # ifdef i386_TARGET_ARCH
2553 case R_386_32: *pP = value; break;
2554 case R_386_PC32: *pP = value - P; break;
2557 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2558 oc->fileName, ELF_R_TYPE(info));
2566 /* Do ELF relocations for which explicit addends are supplied.
2567 sparc-solaris relocations appear to be of this form. */
2569 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2570 Elf_Shdr* shdr, int shnum,
2571 Elf_Sym* stab, char* strtab )
2576 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2577 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2578 int target_shndx = shdr[shnum].sh_info;
2579 int symtab_shndx = shdr[shnum].sh_link;
2581 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2582 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2583 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2584 target_shndx, symtab_shndx ));
2586 for (j = 0; j < nent; j++) {
2587 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
2588 /* This #ifdef only serves to avoid unused-var warnings. */
2589 Elf_Addr offset = rtab[j].r_offset;
2590 Elf_Addr P = targ + offset;
2592 Elf_Addr info = rtab[j].r_info;
2593 Elf_Addr A = rtab[j].r_addend;
2596 # if defined(sparc_TARGET_ARCH)
2597 Elf_Word* pP = (Elf_Word*)P;
2599 # elif defined(ia64_TARGET_ARCH)
2600 Elf64_Xword *pP = (Elf64_Xword *)P;
2604 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2605 j, (void*)offset, (void*)info,
2608 IF_DEBUG(linker,belch( " ZERO" ));
2611 Elf_Sym sym = stab[ELF_R_SYM(info)];
2612 /* First see if it is a local symbol. */
2613 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2614 /* Yes, so we can get the address directly from the ELF symbol
2616 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2618 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2619 + stab[ELF_R_SYM(info)].st_value);
2620 #ifdef ELF_FUNCTION_DESC
2621 /* Make a function descriptor for this function */
2622 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2623 S = allocateFunctionDesc(S + A);
2628 /* No, so look up the name in our global table. */
2629 symbol = strtab + sym.st_name;
2630 (void*)S = lookupSymbol( symbol );
2632 #ifdef ELF_FUNCTION_DESC
2633 /* If a function, already a function descriptor - we would
2634 have to copy it to add an offset. */
2635 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC)
2640 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2643 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2646 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2647 (void*)P, (void*)S, (void*)A ));
2648 /* checkProddableBlock ( oc, (void*)P ); */
2652 switch (ELF_R_TYPE(info)) {
2653 # if defined(sparc_TARGET_ARCH)
2654 case R_SPARC_WDISP30:
2655 w1 = *pP & 0xC0000000;
2656 w2 = (Elf_Word)((value - P) >> 2);
2657 ASSERT((w2 & 0xC0000000) == 0);
2662 w1 = *pP & 0xFFC00000;
2663 w2 = (Elf_Word)(value >> 10);
2664 ASSERT((w2 & 0xFFC00000) == 0);
2670 w2 = (Elf_Word)(value & 0x3FF);
2671 ASSERT((w2 & ~0x3FF) == 0);
2675 /* According to the Sun documentation:
2677 This relocation type resembles R_SPARC_32, except it refers to an
2678 unaligned word. That is, the word to be relocated must be treated
2679 as four separate bytes with arbitrary alignment, not as a word
2680 aligned according to the architecture requirements.
2682 (JRS: which means that freeloading on the R_SPARC_32 case
2683 is probably wrong, but hey ...)
2687 w2 = (Elf_Word)value;
2690 # elif defined(ia64_TARGET_ARCH)
2691 case R_IA64_DIR64LSB:
2692 case R_IA64_FPTR64LSB:
2695 case R_IA64_SEGREL64LSB:
2696 addr = findElfSegment(ehdrC, value);
2699 case R_IA64_GPREL22:
2700 ia64_reloc_gprel22(P, value);
2702 case R_IA64_LTOFF22:
2703 case R_IA64_LTOFF_FPTR22:
2704 addr = allocateGOTEntry(value);
2705 ia64_reloc_gprel22(P, addr);
2707 case R_IA64_PCREL21B:
2708 ia64_reloc_pcrel21(P, S, oc);
2712 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2713 oc->fileName, ELF_R_TYPE(info));
2722 ocResolve_ELF ( ObjectCode* oc )
2726 Elf_Sym* stab = NULL;
2727 char* ehdrC = (char*)(oc->image);
2728 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2729 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2730 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2732 /* first find "the" symbol table */
2733 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2735 /* also go find the string table */
2736 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2738 if (stab == NULL || strtab == NULL) {
2739 belch("%s: can't find string or symbol table", oc->fileName);
2743 /* Process the relocation sections. */
2744 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2746 /* Skip sections called ".rel.stab". These appear to contain
2747 relocation entries that, when done, make the stabs debugging
2748 info point at the right places. We ain't interested in all
2750 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2753 if (shdr[shnum].sh_type == SHT_REL ) {
2754 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2755 shnum, stab, strtab );
2759 if (shdr[shnum].sh_type == SHT_RELA) {
2760 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2761 shnum, stab, strtab );
2766 /* Free the local symbol table; we won't need it again. */
2767 freeHashTable(oc->lochash, NULL);
2775 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2776 * at the front. The following utility functions pack and unpack instructions, and
2777 * take care of the most common relocations.
2780 #ifdef ia64_TARGET_ARCH
2783 ia64_extract_instruction(Elf64_Xword *target)
2786 int slot = (Elf_Addr)target & 3;
2787 (Elf_Addr)target &= ~3;
2795 return ((w1 >> 5) & 0x1ffffffffff);
2797 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2801 barf("ia64_extract_instruction: invalid slot %p", target);
2806 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2808 int slot = (Elf_Addr)target & 3;
2809 (Elf_Addr)target &= ~3;
2814 *target |= value << 5;
2817 *target |= value << 46;
2818 *(target+1) |= value >> 18;
2821 *(target+1) |= value << 23;
2827 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2829 Elf64_Xword instruction;
2830 Elf64_Sxword rel_value;
2832 rel_value = value - gp_val;
2833 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2834 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2836 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2837 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2838 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2839 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2840 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2841 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2845 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2847 Elf64_Xword instruction;
2848 Elf64_Sxword rel_value;
2851 entry = allocatePLTEntry(value, oc);
2853 rel_value = (entry >> 4) - (target >> 4);
2854 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2855 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
2857 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2858 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
2859 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2860 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2867 /* --------------------------------------------------------------------------
2869 * ------------------------------------------------------------------------*/
2871 #if defined(OBJFORMAT_MACHO)
2874 Initial support for MachO linking on Darwin/MacOS X on PowerPC chips
2875 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
2877 I hereby formally apologize for the hackish nature of this code.
2878 Things that need to be done:
2879 *) get common symbols and .bss sections to work properly.
2880 Haskell modules seem to work, but C modules can cause problems
2881 *) implement ocVerifyImage_MachO
2882 *) add more sanity checks. The current code just has to segfault if there's a
2886 static int ocVerifyImage_MachO(ObjectCode* oc)
2888 // FIXME: do some verifying here
2892 static void resolveImports(
2895 struct symtab_command *symLC,
2896 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
2897 unsigned long *indirectSyms,
2898 struct nlist *nlist)
2902 for(i=0;i*4<sect->size;i++)
2904 // according to otool, reserved1 contains the first index into the indirect symbol table
2905 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
2906 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
2909 if((symbol->n_type & N_TYPE) == N_UNDF
2910 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
2911 addr = (void*) (symbol->n_value);
2912 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
2915 addr = lookupSymbol(nm);
2918 fprintf(stderr, "not found: %s\n", nm);
2922 ((void**)(image + sect->offset))[i] = addr;
2926 static void relocateSection(char *image,
2927 struct symtab_command *symLC, struct nlist *nlist,
2928 struct section* sections, struct section *sect)
2930 struct relocation_info *relocs;
2933 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
2935 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
2939 relocs = (struct relocation_info*) (image + sect->reloff);
2943 if(relocs[i].r_address & R_SCATTERED)
2945 struct scattered_relocation_info *scat =
2946 (struct scattered_relocation_info*) &relocs[i];
2950 if(scat->r_length == 2 && scat->r_type == GENERIC_RELOC_VANILLA)
2952 unsigned long* word = (unsigned long*) (image + sect->offset + scat->r_address);
2954 *word = scat->r_value + sect->offset + ((long) image);
2958 continue; // FIXME: I hope it's OK to ignore all the others.
2962 struct relocation_info *reloc = &relocs[i];
2963 if(reloc->r_pcrel && !reloc->r_extern)
2967 && reloc->r_length == 2
2968 && reloc->r_type == GENERIC_RELOC_VANILLA)
2970 unsigned long* word = (unsigned long*) (image + sect->offset + reloc->r_address);
2972 if(!reloc->r_extern)
2975 sections[reloc->r_symbolnum-1].offset
2976 - sections[reloc->r_symbolnum-1].addr
2983 struct nlist *symbol = &nlist[reloc->r_symbolnum];
2984 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
2985 *word = (unsigned long) (lookupSymbol(nm));
2990 fprintf(stderr, "unknown reloc\n");
2997 static int ocGetNames_MachO(ObjectCode* oc)
2999 char *image = (char*) oc->image;
3000 struct mach_header *header = (struct mach_header*) image;
3001 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3002 unsigned i,curSymbol;
3003 struct segment_command *segLC = NULL;
3004 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3005 struct symtab_command *symLC = NULL;
3006 struct dysymtab_command *dsymLC = NULL;
3007 struct nlist *nlist;
3008 unsigned long commonSize = 0;
3009 char *commonStorage = NULL;
3010 unsigned long commonCounter;
3012 for(i=0;i<header->ncmds;i++)
3014 if(lc->cmd == LC_SEGMENT)
3015 segLC = (struct segment_command*) lc;
3016 else if(lc->cmd == LC_SYMTAB)
3017 symLC = (struct symtab_command*) lc;
3018 else if(lc->cmd == LC_DYSYMTAB)
3019 dsymLC = (struct dysymtab_command*) lc;
3020 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3023 sections = (struct section*) (segLC+1);
3024 nlist = (struct nlist*) (image + symLC->symoff);
3026 for(i=0;i<segLC->nsects;i++)
3028 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3029 la_ptrs = §ions[i];
3030 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3031 nl_ptrs = §ions[i];
3033 // for now, only add __text and __const to the sections table
3034 else if(!strcmp(sections[i].sectname,"__text"))
3035 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3036 (void*) (image + sections[i].offset),
3037 (void*) (image + sections[i].offset + sections[i].size));
3038 else if(!strcmp(sections[i].sectname,"__const"))
3039 addSection(oc, SECTIONKIND_RWDATA,
3040 (void*) (image + sections[i].offset),
3041 (void*) (image + sections[i].offset + sections[i].size));
3042 else if(!strcmp(sections[i].sectname,"__data"))
3043 addSection(oc, SECTIONKIND_RWDATA,
3044 (void*) (image + sections[i].offset),
3045 (void*) (image + sections[i].offset + sections[i].size));
3048 // count external symbols defined here
3050 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3052 if((nlist[i].n_type & N_TYPE) == N_SECT)
3055 for(i=0;i<symLC->nsyms;i++)
3057 if((nlist[i].n_type & N_TYPE) == N_UNDF
3058 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3060 commonSize += nlist[i].n_value;
3064 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3065 "ocGetNames_MachO(oc->symbols)");
3067 // insert symbols into hash table
3068 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3070 if((nlist[i].n_type & N_TYPE) == N_SECT)
3072 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3073 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3074 sections[nlist[i].n_sect-1].offset
3075 - sections[nlist[i].n_sect-1].addr
3076 + nlist[i].n_value);
3077 oc->symbols[curSymbol++] = nm;
3081 // insert local symbols into lochash
3082 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3084 if((nlist[i].n_type & N_TYPE) == N_SECT)
3086 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3087 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3088 sections[nlist[i].n_sect-1].offset
3089 - sections[nlist[i].n_sect-1].addr
3090 + nlist[i].n_value);
3095 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3096 commonCounter = (unsigned long)commonStorage;
3097 for(i=0;i<symLC->nsyms;i++)
3099 if((nlist[i].n_type & N_TYPE) == N_UNDF
3100 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3102 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3103 unsigned long sz = nlist[i].n_value;
3105 nlist[i].n_value = commonCounter;
3107 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3108 oc->symbols[curSymbol++] = nm;
3110 commonCounter += sz;
3116 static int ocResolve_MachO(ObjectCode* oc)
3118 char *image = (char*) oc->image;
3119 struct mach_header *header = (struct mach_header*) image;
3120 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3122 struct segment_command *segLC = NULL;
3123 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3124 struct symtab_command *symLC = NULL;
3125 struct dysymtab_command *dsymLC = NULL;
3126 struct nlist *nlist;
3127 unsigned long *indirectSyms;
3129 for(i=0;i<header->ncmds;i++)
3131 if(lc->cmd == LC_SEGMENT)
3132 segLC = (struct segment_command*) lc;
3133 else if(lc->cmd == LC_SYMTAB)
3134 symLC = (struct symtab_command*) lc;
3135 else if(lc->cmd == LC_DYSYMTAB)
3136 dsymLC = (struct dysymtab_command*) lc;
3137 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3140 sections = (struct section*) (segLC+1);
3141 nlist = (struct nlist*) (image + symLC->symoff);
3143 for(i=0;i<segLC->nsects;i++)
3145 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3146 la_ptrs = §ions[i];
3147 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3148 nl_ptrs = §ions[i];
3151 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3154 resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist);
3156 resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist);
3158 for(i=0;i<segLC->nsects;i++)
3160 relocateSection(image,symLC,nlist,sections,§ions[i]);
3163 /* Free the local symbol table; we won't need it again. */
3164 freeHashTable(oc->lochash, NULL);