1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.104 2002/10/02 09:36:00 wolfgang Exp $
4 * (c) The GHC Team, 2000, 2001
8 * ---------------------------------------------------------------------------*/
11 #include "PosixSource.h"
18 #include "LinkerInternals.h"
20 #include "StoragePriv.h"
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
30 #ifdef HAVE_SYS_STAT_H
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 # include <mach-o/ppc/reloc.h>
68 # define OBJFORMAT_MACHO
69 # include <mach-o/loader.h>
70 # include <mach-o/nlist.h>
71 # include <mach-o/reloc.h>
74 /* Hash table mapping symbol names to Symbol */
75 static /*Str*/HashTable *symhash;
77 #if defined(OBJFORMAT_ELF)
78 static int ocVerifyImage_ELF ( ObjectCode* oc );
79 static int ocGetNames_ELF ( ObjectCode* oc );
80 static int ocResolve_ELF ( ObjectCode* oc );
81 #elif defined(OBJFORMAT_PEi386)
82 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
83 static int ocGetNames_PEi386 ( ObjectCode* oc );
84 static int ocResolve_PEi386 ( ObjectCode* oc );
85 #elif defined(OBJFORMAT_MACHO)
86 static int ocVerifyImage_MachO ( ObjectCode* oc );
87 static int ocGetNames_MachO ( ObjectCode* oc );
88 static int ocResolve_MachO ( ObjectCode* oc );
91 /* -----------------------------------------------------------------------------
92 * Built-in symbols from the RTS
95 typedef struct _RtsSymbolVal {
102 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
104 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
105 SymX(makeStableNamezh_fast) \
106 SymX(finalizzeWeakzh_fast)
108 /* These are not available in GUM!!! -- HWL */
109 #define Maybe_ForeignObj
110 #define Maybe_Stable_Names
113 #if !defined (mingw32_TARGET_OS)
114 #define RTS_POSIX_ONLY_SYMBOLS \
115 SymX(stg_sig_install) \
119 #if defined (cygwin32_TARGET_OS)
120 #define RTS_MINGW_ONLY_SYMBOLS /**/
121 /* Don't have the ability to read import libs / archives, so
122 * we have to stupidly list a lot of what libcygwin.a
125 #define RTS_CYGWIN_ONLY_SYMBOLS \
207 #elif !defined(mingw32_TARGET_OS)
208 #define RTS_MINGW_ONLY_SYMBOLS /**/
209 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
210 #else /* defined(mingw32_TARGET_OS) */
211 #define RTS_POSIX_ONLY_SYMBOLS /**/
212 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
214 /* These are statically linked from the mingw libraries into the ghc
215 executable, so we have to employ this hack. */
216 #define RTS_MINGW_ONLY_SYMBOLS \
228 SymX(getservbyname) \
229 SymX(getservbyport) \
230 SymX(getprotobynumber) \
231 SymX(getprotobyname) \
232 SymX(gethostbyname) \
233 SymX(gethostbyaddr) \
268 Sym(_imp___timezone) \
284 # define MAIN_CAP_SYM SymX(MainCapability)
286 # define MAIN_CAP_SYM
289 #define RTS_SYMBOLS \
303 Sym(stg_enterStackTop) \
306 SymX(__stg_gc_enter_1) \
307 SymX(stg_gc_enter_2) \
308 SymX(stg_gc_enter_3) \
309 SymX(stg_gc_enter_4) \
310 SymX(stg_gc_enter_5) \
311 SymX(stg_gc_enter_6) \
312 SymX(stg_gc_enter_7) \
313 SymX(stg_gc_enter_8) \
315 SymX(stg_gc_noregs) \
317 SymX(stg_gc_unbx_r1) \
318 SymX(stg_gc_unpt_r1) \
319 SymX(stg_gc_ut_0_1) \
320 SymX(stg_gc_ut_1_0) \
322 SymX(stg_yield_to_interpreter) \
325 SymX(MallocFailHook) \
326 SymX(NoRunnableThreadsHook) \
328 SymX(OutOfHeapHook) \
329 SymX(PatErrorHdrHook) \
330 SymX(PostTraceHook) \
332 SymX(StackOverflowHook) \
333 SymX(__encodeDouble) \
334 SymX(__encodeFloat) \
337 SymX(__gmpz_cmp_si) \
338 SymX(__gmpz_cmp_ui) \
339 SymX(__gmpz_get_si) \
340 SymX(__gmpz_get_ui) \
341 SymX(__int_encodeDouble) \
342 SymX(__int_encodeFloat) \
343 SymX(andIntegerzh_fast) \
344 SymX(blockAsyncExceptionszh_fast) \
347 SymX(complementIntegerzh_fast) \
348 SymX(cmpIntegerzh_fast) \
349 SymX(cmpIntegerIntzh_fast) \
350 SymX(createAdjustor) \
351 SymX(decodeDoublezh_fast) \
352 SymX(decodeFloatzh_fast) \
355 SymX(deRefWeakzh_fast) \
356 SymX(deRefStablePtrzh_fast) \
357 SymX(divExactIntegerzh_fast) \
358 SymX(divModIntegerzh_fast) \
360 SymX(forkProcesszh_fast) \
361 SymX(freeHaskellFunctionPtr) \
362 SymX(freeStablePtr) \
363 SymX(gcdIntegerzh_fast) \
364 SymX(gcdIntegerIntzh_fast) \
365 SymX(gcdIntzh_fast) \
368 SymX(int2Integerzh_fast) \
369 SymX(integer2Intzh_fast) \
370 SymX(integer2Wordzh_fast) \
371 SymX(isDoubleDenormalized) \
372 SymX(isDoubleInfinite) \
374 SymX(isDoubleNegativeZero) \
375 SymX(isEmptyMVarzh_fast) \
376 SymX(isFloatDenormalized) \
377 SymX(isFloatInfinite) \
379 SymX(isFloatNegativeZero) \
380 SymX(killThreadzh_fast) \
381 SymX(makeStablePtrzh_fast) \
382 SymX(minusIntegerzh_fast) \
383 SymX(mkApUpd0zh_fast) \
384 SymX(myThreadIdzh_fast) \
385 SymX(labelThreadzh_fast) \
386 SymX(newArrayzh_fast) \
387 SymX(newBCOzh_fast) \
388 SymX(newByteArrayzh_fast) \
390 SymX(newMVarzh_fast) \
391 SymX(newMutVarzh_fast) \
392 SymX(newPinnedByteArrayzh_fast) \
393 SymX(orIntegerzh_fast) \
395 SymX(plusIntegerzh_fast) \
398 SymX(putMVarzh_fast) \
399 SymX(quotIntegerzh_fast) \
400 SymX(quotRemIntegerzh_fast) \
402 SymX(remIntegerzh_fast) \
403 SymX(resetNonBlockingFd) \
406 SymX(rts_checkSchedStatus) \
409 SymX(rts_evalLazyIO) \
413 SymX(rts_getDouble) \
418 SymX(rts_getStablePtr) \
419 SymX(rts_getThreadId) \
421 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
589 static int linker_init_done = 0 ;
591 #if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
592 static void *dl_prog_handle;
600 /* Make initLinker idempotent, so we can call it
601 before evey relevant operation; that means we
602 don't need to initialise the linker separately */
603 if (linker_init_done == 1) { return; } else {
604 linker_init_done = 1;
607 symhash = allocStrHashTable();
609 /* populate the symbol table with stuff from the RTS */
610 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
611 ghciInsertStrHashTable("(GHCi built-in symbols)",
612 symhash, sym->lbl, sym->addr);
614 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
615 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
619 /* -----------------------------------------------------------------------------
620 * Loading DLL or .so dynamic libraries
621 * -----------------------------------------------------------------------------
623 * Add a DLL from which symbols may be found. In the ELF case, just
624 * do RTLD_GLOBAL-style add, so no further messing around needs to
625 * happen in order that symbols in the loaded .so are findable --
626 * lookupSymbol() will subsequently see them by dlsym on the program's
627 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
629 * In the PEi386 case, open the DLLs and put handles to them in a
630 * linked list. When looking for a symbol, try all handles in the
631 * list. This means that we need to load even DLLs that are guaranteed
632 * to be in the ghc.exe image already, just so we can get a handle
633 * to give to loadSymbol, so that we can find the symbols. For such
634 * libraries, the LoadLibrary call should be a no-op except for returning
639 #if defined(OBJFORMAT_PEi386)
640 /* A record for storing handles into DLLs. */
645 struct _OpenedDLL* next;
650 /* A list thereof. */
651 static OpenedDLL* opened_dlls = NULL;
655 addDLL( char *dll_name )
657 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
658 /* ------------------- ELF DLL loader ------------------- */
664 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
666 /* dlopen failed; return a ptr to the error msg. */
668 if (errmsg == NULL) errmsg = "addDLL: unknown error";
675 # elif defined(OBJFORMAT_PEi386)
676 /* ------------------- Win32 DLL loader ------------------- */
684 /* fprintf(stderr, "\naddDLL; dll_name = `%s'\n", dll_name); */
686 /* See if we've already got it, and ignore if so. */
687 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
688 if (0 == strcmp(o_dll->name, dll_name))
692 /* The file name has no suffix (yet) so that we can try
693 both foo.dll and foo.drv
695 The documentation for LoadLibrary says:
696 If no file name extension is specified in the lpFileName
697 parameter, the default library extension .dll is
698 appended. However, the file name string can include a trailing
699 point character (.) to indicate that the module name has no
702 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
703 sprintf(buf, "%s.DLL", dll_name);
704 instance = LoadLibrary(buf);
705 if (instance == NULL) {
706 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
707 instance = LoadLibrary(buf);
708 if (instance == NULL) {
711 /* LoadLibrary failed; return a ptr to the error msg. */
712 return "addDLL: unknown error";
717 /* Add this DLL to the list of DLLs in which to search for symbols. */
718 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
719 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
720 strcpy(o_dll->name, dll_name);
721 o_dll->instance = instance;
722 o_dll->next = opened_dlls;
727 barf("addDLL: not implemented on this platform");
731 /* -----------------------------------------------------------------------------
732 * lookup a symbol in the hash table
735 lookupSymbol( char *lbl )
739 ASSERT(symhash != NULL);
740 val = lookupStrHashTable(symhash, lbl);
743 # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO)
744 return dlsym(dl_prog_handle, lbl);
745 # elif defined(OBJFORMAT_PEi386)
748 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
749 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
751 /* HACK: if the name has an initial underscore, try stripping
752 it off & look that up first. I've yet to verify whether there's
753 a Rule that governs whether an initial '_' *should always* be
754 stripped off when mapping from import lib name to the DLL name.
756 sym = GetProcAddress(o_dll->instance, (lbl+1));
758 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
762 sym = GetProcAddress(o_dll->instance, lbl);
764 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
779 __attribute((unused))
781 lookupLocalSymbol( ObjectCode* oc, char *lbl )
785 val = lookupStrHashTable(oc->lochash, lbl);
795 /* -----------------------------------------------------------------------------
796 * Debugging aid: look in GHCi's object symbol tables for symbols
797 * within DELTA bytes of the specified address, and show their names.
800 void ghci_enquire ( char* addr );
802 void ghci_enquire ( char* addr )
807 const int DELTA = 64;
812 for (oc = objects; oc; oc = oc->next) {
813 for (i = 0; i < oc->n_symbols; i++) {
814 sym = oc->symbols[i];
815 if (sym == NULL) continue;
816 /* fprintf(stderr, "enquire %p %p\n", sym, oc->lochash); */
818 if (oc->lochash != NULL)
819 a = lookupStrHashTable(oc->lochash, sym);
821 a = lookupStrHashTable(symhash, sym);
823 /* fprintf(stderr, "ghci_enquire: can't find %s\n", sym); */
825 else if (addr-DELTA <= a && a <= addr+DELTA) {
826 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
833 #ifdef ia64_TARGET_ARCH
834 static unsigned int PLTSize(void);
837 /* -----------------------------------------------------------------------------
838 * Load an obj (populate the global symbol table, but don't resolve yet)
840 * Returns: 1 if ok, 0 on error.
843 loadObj( char *path )
857 /* fprintf(stderr, "loadObj %s\n", path ); */
859 /* Check that we haven't already loaded this object. Don't give up
860 at this stage; ocGetNames_* will barf later. */
864 for (o = objects; o; o = o->next) {
865 if (0 == strcmp(o->fileName, path))
871 "GHCi runtime linker: warning: looks like you're trying to load the\n"
872 "same object file twice:\n"
874 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
880 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
882 # if defined(OBJFORMAT_ELF)
883 oc->formatName = "ELF";
884 # elif defined(OBJFORMAT_PEi386)
885 oc->formatName = "PEi386";
886 # elif defined(OBJFORMAT_MACHO)
887 oc->formatName = "Mach-O";
890 barf("loadObj: not implemented on this platform");
894 if (r == -1) { return 0; }
896 /* sigh, strdup() isn't a POSIX function, so do it the long way */
897 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
898 strcpy(oc->fileName, path);
900 oc->fileSize = st.st_size;
903 oc->lochash = allocStrHashTable();
904 oc->proddables = NULL;
906 /* chain it onto the list of objects */
911 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
913 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
915 fd = open(path, O_RDONLY);
917 barf("loadObj: can't open `%s'", path);
919 pagesize = getpagesize();
921 #ifdef ia64_TARGET_ARCH
922 /* The PLT needs to be right before the object */
923 n = ROUND_UP(PLTSize(), pagesize);
924 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
925 if (oc->plt == MAP_FAILED)
926 barf("loadObj: can't allocate PLT");
929 map_addr = oc->plt + n;
932 n = ROUND_UP(oc->fileSize, pagesize);
933 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
934 if (oc->image == MAP_FAILED)
935 barf("loadObj: can't map `%s'", path);
939 #else /* !USE_MMAP */
941 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
943 /* load the image into memory */
944 f = fopen(path, "rb");
946 barf("loadObj: can't read `%s'", path);
948 n = fread ( oc->image, 1, oc->fileSize, f );
949 if (n != oc->fileSize)
950 barf("loadObj: error whilst reading `%s'", path);
954 #endif /* USE_MMAP */
956 /* verify the in-memory image */
957 # if defined(OBJFORMAT_ELF)
958 r = ocVerifyImage_ELF ( oc );
959 # elif defined(OBJFORMAT_PEi386)
960 r = ocVerifyImage_PEi386 ( oc );
961 # elif defined(OBJFORMAT_MACHO)
962 r = ocVerifyImage_MachO ( oc );
964 barf("loadObj: no verify method");
966 if (!r) { return r; }
968 /* build the symbol list for this image */
969 # if defined(OBJFORMAT_ELF)
970 r = ocGetNames_ELF ( oc );
971 # elif defined(OBJFORMAT_PEi386)
972 r = ocGetNames_PEi386 ( oc );
973 # elif defined(OBJFORMAT_MACHO)
974 r = ocGetNames_MachO ( oc );
976 barf("loadObj: no getNames method");
978 if (!r) { return r; }
980 /* loaded, but not resolved yet */
981 oc->status = OBJECT_LOADED;
986 /* -----------------------------------------------------------------------------
987 * resolve all the currently unlinked objects in memory
989 * Returns: 1 if ok, 0 on error.
999 for (oc = objects; oc; oc = oc->next) {
1000 if (oc->status != OBJECT_RESOLVED) {
1001 # if defined(OBJFORMAT_ELF)
1002 r = ocResolve_ELF ( oc );
1003 # elif defined(OBJFORMAT_PEi386)
1004 r = ocResolve_PEi386 ( oc );
1005 # elif defined(OBJFORMAT_MACHO)
1006 r = ocResolve_MachO ( oc );
1008 barf("resolveObjs: not implemented on this platform");
1010 if (!r) { return r; }
1011 oc->status = OBJECT_RESOLVED;
1017 /* -----------------------------------------------------------------------------
1018 * delete an object from the pool
1021 unloadObj( char *path )
1023 ObjectCode *oc, *prev;
1025 ASSERT(symhash != NULL);
1026 ASSERT(objects != NULL);
1031 for (oc = objects; oc; prev = oc, oc = oc->next) {
1032 if (!strcmp(oc->fileName,path)) {
1034 /* Remove all the mappings for the symbols within this
1039 for (i = 0; i < oc->n_symbols; i++) {
1040 if (oc->symbols[i] != NULL) {
1041 removeStrHashTable(symhash, oc->symbols[i], NULL);
1049 prev->next = oc->next;
1052 /* We're going to leave this in place, in case there are
1053 any pointers from the heap into it: */
1054 /* free(oc->image); */
1058 /* The local hash table should have been freed at the end
1059 of the ocResolve_ call on it. */
1060 ASSERT(oc->lochash == NULL);
1066 belch("unloadObj: can't find `%s' to unload", path);
1070 /* -----------------------------------------------------------------------------
1071 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1072 * which may be prodded during relocation, and abort if we try and write
1073 * outside any of these.
1075 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1078 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1079 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1083 pb->next = oc->proddables;
1084 oc->proddables = pb;
1087 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1090 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1091 char* s = (char*)(pb->start);
1092 char* e = s + pb->size - 1;
1093 char* a = (char*)addr;
1094 /* Assumes that the biggest fixup involves a 4-byte write. This
1095 probably needs to be changed to 8 (ie, +7) on 64-bit
1097 if (a >= s && (a+3) <= e) return;
1099 barf("checkProddableBlock: invalid fixup in runtime linker");
1102 /* -----------------------------------------------------------------------------
1103 * Section management.
1105 static void addSection ( ObjectCode* oc, SectionKind kind,
1106 void* start, void* end )
1108 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1112 s->next = oc->sections;
1115 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1116 start, ((char*)end)-1, end - start + 1, kind );
1122 /* --------------------------------------------------------------------------
1123 * PEi386 specifics (Win32 targets)
1124 * ------------------------------------------------------------------------*/
1126 /* The information for this linker comes from
1127 Microsoft Portable Executable
1128 and Common Object File Format Specification
1129 revision 5.1 January 1998
1130 which SimonM says comes from the MS Developer Network CDs.
1132 It can be found there (on older CDs), but can also be found
1135 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1137 (this is Rev 6.0 from February 1999).
1139 Things move, so if that fails, try searching for it via
1141 http://www.google.com/search?q=PE+COFF+specification
1143 The ultimate reference for the PE format is the Winnt.h
1144 header file that comes with the Platform SDKs; as always,
1145 implementations will drift wrt their documentation.
1147 A good background article on the PE format is Matt Pietrek's
1148 March 1994 article in Microsoft System Journal (MSJ)
1149 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1150 Win32 Portable Executable File Format." The info in there
1151 has recently been updated in a two part article in
1152 MSDN magazine, issues Feb and March 2002,
1153 "Inside Windows: An In-Depth Look into the Win32 Portable
1154 Executable File Format"
1156 John Levine's book "Linkers and Loaders" contains useful
1161 #if defined(OBJFORMAT_PEi386)
1165 typedef unsigned char UChar;
1166 typedef unsigned short UInt16;
1167 typedef unsigned int UInt32;
1174 UInt16 NumberOfSections;
1175 UInt32 TimeDateStamp;
1176 UInt32 PointerToSymbolTable;
1177 UInt32 NumberOfSymbols;
1178 UInt16 SizeOfOptionalHeader;
1179 UInt16 Characteristics;
1183 #define sizeof_COFF_header 20
1190 UInt32 VirtualAddress;
1191 UInt32 SizeOfRawData;
1192 UInt32 PointerToRawData;
1193 UInt32 PointerToRelocations;
1194 UInt32 PointerToLinenumbers;
1195 UInt16 NumberOfRelocations;
1196 UInt16 NumberOfLineNumbers;
1197 UInt32 Characteristics;
1201 #define sizeof_COFF_section 40
1208 UInt16 SectionNumber;
1211 UChar NumberOfAuxSymbols;
1215 #define sizeof_COFF_symbol 18
1220 UInt32 VirtualAddress;
1221 UInt32 SymbolTableIndex;
1226 #define sizeof_COFF_reloc 10
1229 /* From PE spec doc, section 3.3.2 */
1230 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1231 windows.h -- for the same purpose, but I want to know what I'm
1233 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1234 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1235 #define MYIMAGE_FILE_DLL 0x2000
1236 #define MYIMAGE_FILE_SYSTEM 0x1000
1237 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1238 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1239 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1241 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1242 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1243 #define MYIMAGE_SYM_CLASS_STATIC 3
1244 #define MYIMAGE_SYM_UNDEFINED 0
1246 /* From PE spec doc, section 4.1 */
1247 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1248 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1249 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1251 /* From PE spec doc, section 5.2.1 */
1252 #define MYIMAGE_REL_I386_DIR32 0x0006
1253 #define MYIMAGE_REL_I386_REL32 0x0014
1256 /* We use myindex to calculate array addresses, rather than
1257 simply doing the normal subscript thing. That's because
1258 some of the above structs have sizes which are not
1259 a whole number of words. GCC rounds their sizes up to a
1260 whole number of words, which means that the address calcs
1261 arising from using normal C indexing or pointer arithmetic
1262 are just plain wrong. Sigh.
1265 myindex ( int scale, void* base, int index )
1268 ((UChar*)base) + scale * index;
1273 printName ( UChar* name, UChar* strtab )
1275 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1276 UInt32 strtab_offset = * (UInt32*)(name+4);
1277 fprintf ( stderr, "%s", strtab + strtab_offset );
1280 for (i = 0; i < 8; i++) {
1281 if (name[i] == 0) break;
1282 fprintf ( stderr, "%c", name[i] );
1289 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1291 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1292 UInt32 strtab_offset = * (UInt32*)(name+4);
1293 strncpy ( dst, strtab+strtab_offset, dstSize );
1299 if (name[i] == 0) break;
1309 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1312 /* If the string is longer than 8 bytes, look in the
1313 string table for it -- this will be correctly zero terminated.
1315 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1316 UInt32 strtab_offset = * (UInt32*)(name+4);
1317 return ((UChar*)strtab) + strtab_offset;
1319 /* Otherwise, if shorter than 8 bytes, return the original,
1320 which by defn is correctly terminated.
1322 if (name[7]==0) return name;
1323 /* The annoying case: 8 bytes. Copy into a temporary
1324 (which is never freed ...)
1326 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1328 strncpy(newstr,name,8);
1334 /* Just compares the short names (first 8 chars) */
1335 static COFF_section *
1336 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1340 = (COFF_header*)(oc->image);
1341 COFF_section* sectab
1343 ((UChar*)(oc->image))
1344 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1346 for (i = 0; i < hdr->NumberOfSections; i++) {
1349 COFF_section* section_i
1351 myindex ( sizeof_COFF_section, sectab, i );
1352 n1 = (UChar*) &(section_i->Name);
1354 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1355 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1356 n1[6]==n2[6] && n1[7]==n2[7])
1365 zapTrailingAtSign ( UChar* sym )
1367 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1369 if (sym[0] == 0) return;
1371 while (sym[i] != 0) i++;
1374 while (j > 0 && my_isdigit(sym[j])) j--;
1375 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1381 ocVerifyImage_PEi386 ( ObjectCode* oc )
1386 COFF_section* sectab;
1387 COFF_symbol* symtab;
1389 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1390 hdr = (COFF_header*)(oc->image);
1391 sectab = (COFF_section*) (
1392 ((UChar*)(oc->image))
1393 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1395 symtab = (COFF_symbol*) (
1396 ((UChar*)(oc->image))
1397 + hdr->PointerToSymbolTable
1399 strtab = ((UChar*)symtab)
1400 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1402 if (hdr->Machine != 0x14c) {
1403 belch("Not x86 PEi386");
1406 if (hdr->SizeOfOptionalHeader != 0) {
1407 belch("PEi386 with nonempty optional header");
1410 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1411 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1412 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1413 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1414 belch("Not a PEi386 object file");
1417 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1418 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1419 belch("Invalid PEi386 word size or endiannness: %d",
1420 (int)(hdr->Characteristics));
1423 /* If the string table size is way crazy, this might indicate that
1424 there are more than 64k relocations, despite claims to the
1425 contrary. Hence this test. */
1426 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1428 if ( (*(UInt32*)strtab) > 600000 ) {
1429 /* Note that 600k has no special significance other than being
1430 big enough to handle the almost-2MB-sized lumps that
1431 constitute HSwin32*.o. */
1432 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1437 /* No further verification after this point; only debug printing. */
1439 IF_DEBUG(linker, i=1);
1440 if (i == 0) return 1;
1443 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1445 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1447 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1449 fprintf ( stderr, "\n" );
1451 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1453 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1455 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1457 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1459 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1461 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1463 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1465 /* Print the section table. */
1466 fprintf ( stderr, "\n" );
1467 for (i = 0; i < hdr->NumberOfSections; i++) {
1469 COFF_section* sectab_i
1471 myindex ( sizeof_COFF_section, sectab, i );
1478 printName ( sectab_i->Name, strtab );
1488 sectab_i->VirtualSize,
1489 sectab_i->VirtualAddress,
1490 sectab_i->SizeOfRawData,
1491 sectab_i->PointerToRawData,
1492 sectab_i->NumberOfRelocations,
1493 sectab_i->PointerToRelocations,
1494 sectab_i->PointerToRawData
1496 reltab = (COFF_reloc*) (
1497 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1500 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1501 /* If the relocation field (a short) has overflowed, the
1502 * real count can be found in the first reloc entry.
1504 * See Section 4.1 (last para) of the PE spec (rev6.0).
1506 COFF_reloc* rel = (COFF_reloc*)
1507 myindex ( sizeof_COFF_reloc, reltab, 0 );
1508 noRelocs = rel->VirtualAddress;
1511 noRelocs = sectab_i->NumberOfRelocations;
1515 for (; j < noRelocs; j++) {
1517 COFF_reloc* rel = (COFF_reloc*)
1518 myindex ( sizeof_COFF_reloc, reltab, j );
1520 " type 0x%-4x vaddr 0x%-8x name `",
1522 rel->VirtualAddress );
1523 sym = (COFF_symbol*)
1524 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1525 /* Hmm..mysterious looking offset - what's it for? SOF */
1526 printName ( sym->Name, strtab -10 );
1527 fprintf ( stderr, "'\n" );
1530 fprintf ( stderr, "\n" );
1532 fprintf ( stderr, "\n" );
1533 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1534 fprintf ( stderr, "---START of string table---\n");
1535 for (i = 4; i < *(Int32*)strtab; i++) {
1537 fprintf ( stderr, "\n"); else
1538 fprintf( stderr, "%c", strtab[i] );
1540 fprintf ( stderr, "--- END of string table---\n");
1542 fprintf ( stderr, "\n" );
1545 COFF_symbol* symtab_i;
1546 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1547 symtab_i = (COFF_symbol*)
1548 myindex ( sizeof_COFF_symbol, symtab, i );
1554 printName ( symtab_i->Name, strtab );
1563 (Int32)(symtab_i->SectionNumber),
1564 (UInt32)symtab_i->Type,
1565 (UInt32)symtab_i->StorageClass,
1566 (UInt32)symtab_i->NumberOfAuxSymbols
1568 i += symtab_i->NumberOfAuxSymbols;
1572 fprintf ( stderr, "\n" );
1578 ocGetNames_PEi386 ( ObjectCode* oc )
1581 COFF_section* sectab;
1582 COFF_symbol* symtab;
1589 hdr = (COFF_header*)(oc->image);
1590 sectab = (COFF_section*) (
1591 ((UChar*)(oc->image))
1592 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1594 symtab = (COFF_symbol*) (
1595 ((UChar*)(oc->image))
1596 + hdr->PointerToSymbolTable
1598 strtab = ((UChar*)(oc->image))
1599 + hdr->PointerToSymbolTable
1600 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1602 /* Allocate space for any (local, anonymous) .bss sections. */
1604 for (i = 0; i < hdr->NumberOfSections; i++) {
1606 COFF_section* sectab_i
1608 myindex ( sizeof_COFF_section, sectab, i );
1609 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1610 if (sectab_i->VirtualSize == 0) continue;
1611 /* This is a non-empty .bss section. Allocate zeroed space for
1612 it, and set its PointerToRawData field such that oc->image +
1613 PointerToRawData == addr_of_zeroed_space. */
1614 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1615 "ocGetNames_PEi386(anonymous bss)");
1616 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1617 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1618 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1621 /* Copy section information into the ObjectCode. */
1623 for (i = 0; i < hdr->NumberOfSections; i++) {
1629 = SECTIONKIND_OTHER;
1630 COFF_section* sectab_i
1632 myindex ( sizeof_COFF_section, sectab, i );
1633 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1636 /* I'm sure this is the Right Way to do it. However, the
1637 alternative of testing the sectab_i->Name field seems to
1638 work ok with Cygwin.
1640 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1641 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1642 kind = SECTIONKIND_CODE_OR_RODATA;
1645 if (0==strcmp(".text",sectab_i->Name) ||
1646 0==strcmp(".rodata",sectab_i->Name))
1647 kind = SECTIONKIND_CODE_OR_RODATA;
1648 if (0==strcmp(".data",sectab_i->Name) ||
1649 0==strcmp(".bss",sectab_i->Name))
1650 kind = SECTIONKIND_RWDATA;
1652 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1653 sz = sectab_i->SizeOfRawData;
1654 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1656 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1657 end = start + sz - 1;
1659 if (kind == SECTIONKIND_OTHER
1660 /* Ignore sections called which contain stabs debugging
1662 && 0 != strcmp(".stab", sectab_i->Name)
1663 && 0 != strcmp(".stabstr", sectab_i->Name)
1665 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1669 if (kind != SECTIONKIND_OTHER && end >= start) {
1670 addSection(oc, kind, start, end);
1671 addProddableBlock(oc, start, end - start + 1);
1675 /* Copy exported symbols into the ObjectCode. */
1677 oc->n_symbols = hdr->NumberOfSymbols;
1678 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1679 "ocGetNames_PEi386(oc->symbols)");
1680 /* Call me paranoid; I don't care. */
1681 for (i = 0; i < oc->n_symbols; i++)
1682 oc->symbols[i] = NULL;
1686 COFF_symbol* symtab_i;
1687 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1688 symtab_i = (COFF_symbol*)
1689 myindex ( sizeof_COFF_symbol, symtab, i );
1693 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1694 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1695 /* This symbol is global and defined, viz, exported */
1696 /* for MYIMAGE_SYMCLASS_EXTERNAL
1697 && !MYIMAGE_SYM_UNDEFINED,
1698 the address of the symbol is:
1699 address of relevant section + offset in section
1701 COFF_section* sectabent
1702 = (COFF_section*) myindex ( sizeof_COFF_section,
1704 symtab_i->SectionNumber-1 );
1705 addr = ((UChar*)(oc->image))
1706 + (sectabent->PointerToRawData
1710 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1711 && symtab_i->Value > 0) {
1712 /* This symbol isn't in any section at all, ie, global bss.
1713 Allocate zeroed space for it. */
1714 addr = stgCallocBytes(1, symtab_i->Value,
1715 "ocGetNames_PEi386(non-anonymous bss)");
1716 addSection(oc, SECTIONKIND_RWDATA, addr,
1717 ((UChar*)addr) + symtab_i->Value - 1);
1718 addProddableBlock(oc, addr, symtab_i->Value);
1719 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1722 if (addr != NULL ) {
1723 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1724 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1725 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1726 ASSERT(i >= 0 && i < oc->n_symbols);
1727 /* cstring_from_COFF_symbol_name always succeeds. */
1728 oc->symbols[i] = sname;
1729 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1733 "IGNORING symbol %d\n"
1737 printName ( symtab_i->Name, strtab );
1746 (Int32)(symtab_i->SectionNumber),
1747 (UInt32)symtab_i->Type,
1748 (UInt32)symtab_i->StorageClass,
1749 (UInt32)symtab_i->NumberOfAuxSymbols
1754 i += symtab_i->NumberOfAuxSymbols;
1763 ocResolve_PEi386 ( ObjectCode* oc )
1766 COFF_section* sectab;
1767 COFF_symbol* symtab;
1777 /* ToDo: should be variable-sized? But is at least safe in the
1778 sense of buffer-overrun-proof. */
1780 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1782 hdr = (COFF_header*)(oc->image);
1783 sectab = (COFF_section*) (
1784 ((UChar*)(oc->image))
1785 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1787 symtab = (COFF_symbol*) (
1788 ((UChar*)(oc->image))
1789 + hdr->PointerToSymbolTable
1791 strtab = ((UChar*)(oc->image))
1792 + hdr->PointerToSymbolTable
1793 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1795 for (i = 0; i < hdr->NumberOfSections; i++) {
1796 COFF_section* sectab_i
1798 myindex ( sizeof_COFF_section, sectab, i );
1801 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1804 /* Ignore sections called which contain stabs debugging
1806 if (0 == strcmp(".stab", sectab_i->Name)
1807 || 0 == strcmp(".stabstr", sectab_i->Name))
1810 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1811 /* If the relocation field (a short) has overflowed, the
1812 * real count can be found in the first reloc entry.
1814 * See Section 4.1 (last para) of the PE spec (rev6.0).
1816 COFF_reloc* rel = (COFF_reloc*)
1817 myindex ( sizeof_COFF_reloc, reltab, 0 );
1818 noRelocs = rel->VirtualAddress;
1819 fprintf(stderr, "Overflown relocs: %u\n", noRelocs);
1822 noRelocs = sectab_i->NumberOfRelocations;
1827 for (; j < noRelocs; j++) {
1829 COFF_reloc* reltab_j
1831 myindex ( sizeof_COFF_reloc, reltab, j );
1833 /* the location to patch */
1835 ((UChar*)(oc->image))
1836 + (sectab_i->PointerToRawData
1837 + reltab_j->VirtualAddress
1838 - sectab_i->VirtualAddress )
1840 /* the existing contents of pP */
1842 /* the symbol to connect to */
1843 sym = (COFF_symbol*)
1844 myindex ( sizeof_COFF_symbol,
1845 symtab, reltab_j->SymbolTableIndex );
1848 "reloc sec %2d num %3d: type 0x%-4x "
1849 "vaddr 0x%-8x name `",
1851 (UInt32)reltab_j->Type,
1852 reltab_j->VirtualAddress );
1853 printName ( sym->Name, strtab );
1854 fprintf ( stderr, "'\n" ));
1856 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1857 COFF_section* section_sym
1858 = findPEi386SectionCalled ( oc, sym->Name );
1860 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1863 S = ((UInt32)(oc->image))
1864 + (section_sym->PointerToRawData
1867 copyName ( sym->Name, strtab, symbol, 1000-1 );
1868 (void*)S = lookupLocalSymbol( oc, symbol );
1869 if ((void*)S != NULL) goto foundit;
1870 (void*)S = lookupSymbol( symbol );
1871 if ((void*)S != NULL) goto foundit;
1872 zapTrailingAtSign ( symbol );
1873 (void*)S = lookupLocalSymbol( oc, symbol );
1874 if ((void*)S != NULL) goto foundit;
1875 (void*)S = lookupSymbol( symbol );
1876 if ((void*)S != NULL) goto foundit;
1877 /* Newline first because the interactive linker has printed "linking..." */
1878 belch("\n%s: unknown symbol `%s'", oc->fileName, symbol);
1882 checkProddableBlock(oc, pP);
1883 switch (reltab_j->Type) {
1884 case MYIMAGE_REL_I386_DIR32:
1887 case MYIMAGE_REL_I386_REL32:
1888 /* Tricky. We have to insert a displacement at
1889 pP which, when added to the PC for the _next_
1890 insn, gives the address of the target (S).
1891 Problem is to know the address of the next insn
1892 when we only know pP. We assume that this
1893 literal field is always the last in the insn,
1894 so that the address of the next insn is pP+4
1895 -- hence the constant 4.
1896 Also I don't know if A should be added, but so
1897 far it has always been zero.
1900 *pP = S - ((UInt32)pP) - 4;
1903 belch("%s: unhandled PEi386 relocation type %d",
1904 oc->fileName, reltab_j->Type);
1911 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1915 #endif /* defined(OBJFORMAT_PEi386) */
1918 /* --------------------------------------------------------------------------
1920 * ------------------------------------------------------------------------*/
1922 #if defined(OBJFORMAT_ELF)
1927 #if defined(sparc_TARGET_ARCH)
1928 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1929 #elif defined(i386_TARGET_ARCH)
1930 # define ELF_TARGET_386 /* Used inside <elf.h> */
1931 #elif defined (ia64_TARGET_ARCH)
1932 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
1934 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
1935 # define ELF_NEED_GOT /* needs Global Offset Table */
1936 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
1942 * Define a set of types which can be used for both ELF32 and ELF64
1946 #define ELFCLASS ELFCLASS64
1947 #define Elf_Addr Elf64_Addr
1948 #define Elf_Word Elf64_Word
1949 #define Elf_Sword Elf64_Sword
1950 #define Elf_Ehdr Elf64_Ehdr
1951 #define Elf_Phdr Elf64_Phdr
1952 #define Elf_Shdr Elf64_Shdr
1953 #define Elf_Sym Elf64_Sym
1954 #define Elf_Rel Elf64_Rel
1955 #define Elf_Rela Elf64_Rela
1956 #define ELF_ST_TYPE ELF64_ST_TYPE
1957 #define ELF_ST_BIND ELF64_ST_BIND
1958 #define ELF_R_TYPE ELF64_R_TYPE
1959 #define ELF_R_SYM ELF64_R_SYM
1961 #define ELFCLASS ELFCLASS32
1962 #define Elf_Addr Elf32_Addr
1963 #define Elf_Word Elf32_Word
1964 #define Elf_Sword Elf32_Sword
1965 #define Elf_Ehdr Elf32_Ehdr
1966 #define Elf_Phdr Elf32_Phdr
1967 #define Elf_Shdr Elf32_Shdr
1968 #define Elf_Sym Elf32_Sym
1969 #define Elf_Rel Elf32_Rel
1970 #define Elf_Rela Elf32_Rela
1971 #define ELF_ST_TYPE ELF32_ST_TYPE
1972 #define ELF_ST_BIND ELF32_ST_BIND
1973 #define ELF_R_TYPE ELF32_R_TYPE
1974 #define ELF_R_SYM ELF32_R_SYM
1979 * Functions to allocate entries in dynamic sections. Currently we simply
1980 * preallocate a large number, and we don't check if a entry for the given
1981 * target already exists (a linear search is too slow). Ideally these
1982 * entries would be associated with symbols.
1985 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
1986 #define GOT_SIZE 0x20000
1987 #define FUNCTION_TABLE_SIZE 0x10000
1988 #define PLT_SIZE 0x08000
1991 static Elf_Addr got[GOT_SIZE];
1992 static unsigned int gotIndex;
1993 static Elf_Addr gp_val = (Elf_Addr)got;
1996 allocateGOTEntry(Elf_Addr target)
2000 if (gotIndex >= GOT_SIZE)
2001 barf("Global offset table overflow");
2003 entry = &got[gotIndex++];
2005 return (Elf_Addr)entry;
2009 #ifdef ELF_FUNCTION_DESC
2015 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
2016 static unsigned int functionTableIndex;
2019 allocateFunctionDesc(Elf_Addr target)
2021 FunctionDesc *entry;
2023 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
2024 barf("Function table overflow");
2026 entry = &functionTable[functionTableIndex++];
2028 entry->gp = (Elf_Addr)gp_val;
2029 return (Elf_Addr)entry;
2033 copyFunctionDesc(Elf_Addr target)
2035 FunctionDesc *olddesc = (FunctionDesc *)target;
2036 FunctionDesc *newdesc;
2038 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
2039 newdesc->gp = olddesc->gp;
2040 return (Elf_Addr)newdesc;
2045 #ifdef ia64_TARGET_ARCH
2046 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
2047 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
2049 static unsigned char plt_code[] =
2051 /* taken from binutils bfd/elfxx-ia64.c */
2052 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
2053 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
2054 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
2055 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
2056 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
2057 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
2060 /* If we can't get to the function descriptor via gp, take a local copy of it */
2061 #define PLT_RELOC(code, target) { \
2062 Elf64_Sxword rel_value = target - gp_val; \
2063 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
2064 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
2066 ia64_reloc_gprel22((Elf_Addr)code, target); \
2071 unsigned char code[sizeof(plt_code)];
2075 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2077 PLTEntry *plt = (PLTEntry *)oc->plt;
2080 if (oc->pltIndex >= PLT_SIZE)
2081 barf("Procedure table overflow");
2083 entry = &plt[oc->pltIndex++];
2084 memcpy(entry->code, plt_code, sizeof(entry->code));
2085 PLT_RELOC(entry->code, target);
2086 return (Elf_Addr)entry;
2092 return (PLT_SIZE * sizeof(PLTEntry));
2098 * Generic ELF functions
2102 findElfSection ( void* objImage, Elf_Word sh_type )
2104 char* ehdrC = (char*)objImage;
2105 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2106 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2107 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2111 for (i = 0; i < ehdr->e_shnum; i++) {
2112 if (shdr[i].sh_type == sh_type
2113 /* Ignore the section header's string table. */
2114 && i != ehdr->e_shstrndx
2115 /* Ignore string tables named .stabstr, as they contain
2117 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2119 ptr = ehdrC + shdr[i].sh_offset;
2126 #if defined(ia64_TARGET_ARCH)
2128 findElfSegment ( void* objImage, Elf_Addr vaddr )
2130 char* ehdrC = (char*)objImage;
2131 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2132 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2133 Elf_Addr segaddr = 0;
2136 for (i = 0; i < ehdr->e_phnum; i++) {
2137 segaddr = phdr[i].p_vaddr;
2138 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2146 ocVerifyImage_ELF ( ObjectCode* oc )
2150 int i, j, nent, nstrtab, nsymtabs;
2154 char* ehdrC = (char*)(oc->image);
2155 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2157 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2158 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2159 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2160 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2161 belch("%s: not an ELF object", oc->fileName);
2165 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2166 belch("%s: unsupported ELF format", oc->fileName);
2170 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2171 IF_DEBUG(linker,belch( "Is little-endian" ));
2173 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2174 IF_DEBUG(linker,belch( "Is big-endian" ));
2176 belch("%s: unknown endiannness", oc->fileName);
2180 if (ehdr->e_type != ET_REL) {
2181 belch("%s: not a relocatable object (.o) file", oc->fileName);
2184 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2186 IF_DEBUG(linker,belch( "Architecture is " ));
2187 switch (ehdr->e_machine) {
2188 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2189 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2191 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2193 default: IF_DEBUG(linker,belch( "unknown" ));
2194 belch("%s: unknown architecture", oc->fileName);
2198 IF_DEBUG(linker,belch(
2199 "\nSection header table: start %d, n_entries %d, ent_size %d",
2200 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2202 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2204 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2206 if (ehdr->e_shstrndx == SHN_UNDEF) {
2207 belch("%s: no section header string table", oc->fileName);
2210 IF_DEBUG(linker,belch( "Section header string table is section %d",
2212 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2215 for (i = 0; i < ehdr->e_shnum; i++) {
2216 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2217 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2218 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2219 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2220 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2221 ehdrC + shdr[i].sh_offset,
2222 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2224 if (shdr[i].sh_type == SHT_REL) {
2225 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2226 } else if (shdr[i].sh_type == SHT_RELA) {
2227 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2229 IF_DEBUG(linker,fprintf(stderr," "));
2232 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2236 IF_DEBUG(linker,belch( "\nString tables" ));
2239 for (i = 0; i < ehdr->e_shnum; i++) {
2240 if (shdr[i].sh_type == SHT_STRTAB
2241 /* Ignore the section header's string table. */
2242 && i != ehdr->e_shstrndx
2243 /* Ignore string tables named .stabstr, as they contain
2245 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2247 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2248 strtab = ehdrC + shdr[i].sh_offset;
2253 belch("%s: no string tables, or too many", oc->fileName);
2258 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2259 for (i = 0; i < ehdr->e_shnum; i++) {
2260 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2261 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2263 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2264 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2265 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2267 shdr[i].sh_size % sizeof(Elf_Sym)
2269 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2270 belch("%s: non-integral number of symbol table entries", oc->fileName);
2273 for (j = 0; j < nent; j++) {
2274 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2275 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2276 (int)stab[j].st_shndx,
2277 (int)stab[j].st_size,
2278 (char*)stab[j].st_value ));
2280 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2281 switch (ELF_ST_TYPE(stab[j].st_info)) {
2282 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2283 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2284 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2285 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2286 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2287 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2289 IF_DEBUG(linker,fprintf(stderr, " " ));
2291 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2292 switch (ELF_ST_BIND(stab[j].st_info)) {
2293 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2294 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2295 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2296 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2298 IF_DEBUG(linker,fprintf(stderr, " " ));
2300 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2304 if (nsymtabs == 0) {
2305 belch("%s: didn't find any symbol tables", oc->fileName);
2314 ocGetNames_ELF ( ObjectCode* oc )
2319 char* ehdrC = (char*)(oc->image);
2320 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2321 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2322 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2324 ASSERT(symhash != NULL);
2327 belch("%s: no strtab", oc->fileName);
2332 for (i = 0; i < ehdr->e_shnum; i++) {
2333 /* Figure out what kind of section it is. Logic derived from
2334 Figure 1.14 ("Special Sections") of the ELF document
2335 ("Portable Formats Specification, Version 1.1"). */
2336 Elf_Shdr hdr = shdr[i];
2337 SectionKind kind = SECTIONKIND_OTHER;
2340 if (hdr.sh_type == SHT_PROGBITS
2341 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2342 /* .text-style section */
2343 kind = SECTIONKIND_CODE_OR_RODATA;
2346 if (hdr.sh_type == SHT_PROGBITS
2347 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2348 /* .data-style section */
2349 kind = SECTIONKIND_RWDATA;
2352 if (hdr.sh_type == SHT_PROGBITS
2353 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2354 /* .rodata-style section */
2355 kind = SECTIONKIND_CODE_OR_RODATA;
2358 if (hdr.sh_type == SHT_NOBITS
2359 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2360 /* .bss-style section */
2361 kind = SECTIONKIND_RWDATA;
2365 if (is_bss && shdr[i].sh_size > 0) {
2366 /* This is a non-empty .bss section. Allocate zeroed space for
2367 it, and set its .sh_offset field such that
2368 ehdrC + .sh_offset == addr_of_zeroed_space. */
2369 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2370 "ocGetNames_ELF(BSS)");
2371 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2373 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2374 zspace, shdr[i].sh_size);
2378 /* fill in the section info */
2379 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2380 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2381 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2382 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2385 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2387 /* copy stuff into this module's object symbol table */
2388 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2389 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2391 oc->n_symbols = nent;
2392 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2393 "ocGetNames_ELF(oc->symbols)");
2395 for (j = 0; j < nent; j++) {
2397 char isLocal = FALSE; /* avoids uninit-var warning */
2399 char* nm = strtab + stab[j].st_name;
2400 int secno = stab[j].st_shndx;
2402 /* Figure out if we want to add it; if so, set ad to its
2403 address. Otherwise leave ad == NULL. */
2405 if (secno == SHN_COMMON) {
2407 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2409 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2410 stab[j].st_size, nm);
2412 /* Pointless to do addProddableBlock() for this area,
2413 since the linker should never poke around in it. */
2416 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2417 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2419 /* and not an undefined symbol */
2420 && stab[j].st_shndx != SHN_UNDEF
2421 /* and not in a "special section" */
2422 && stab[j].st_shndx < SHN_LORESERVE
2424 /* and it's a not a section or string table or anything silly */
2425 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2426 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2427 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2430 /* Section 0 is the undefined section, hence > and not >=. */
2431 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2433 if (shdr[secno].sh_type == SHT_NOBITS) {
2434 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2435 stab[j].st_size, stab[j].st_value, nm);
2438 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2439 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2442 #ifdef ELF_FUNCTION_DESC
2443 /* dlsym() and the initialisation table both give us function
2444 * descriptors, so to be consistent we store function descriptors
2445 * in the symbol table */
2446 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2447 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2449 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2450 ad, oc->fileName, nm ));
2455 /* And the decision is ... */
2459 oc->symbols[j] = nm;
2462 /* Ignore entirely. */
2464 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2468 IF_DEBUG(linker,belch( "skipping `%s'",
2469 strtab + stab[j].st_name ));
2472 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2473 (int)ELF_ST_BIND(stab[j].st_info),
2474 (int)ELF_ST_TYPE(stab[j].st_info),
2475 (int)stab[j].st_shndx,
2476 strtab + stab[j].st_name
2479 oc->symbols[j] = NULL;
2488 /* Do ELF relocations which lack an explicit addend. All x86-linux
2489 relocations appear to be of this form. */
2491 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2492 Elf_Shdr* shdr, int shnum,
2493 Elf_Sym* stab, char* strtab )
2498 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2499 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2500 int target_shndx = shdr[shnum].sh_info;
2501 int symtab_shndx = shdr[shnum].sh_link;
2503 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2504 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2505 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2506 target_shndx, symtab_shndx ));
2508 for (j = 0; j < nent; j++) {
2509 Elf_Addr offset = rtab[j].r_offset;
2510 Elf_Addr info = rtab[j].r_info;
2512 Elf_Addr P = ((Elf_Addr)targ) + offset;
2513 Elf_Word* pP = (Elf_Word*)P;
2518 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2519 j, (void*)offset, (void*)info ));
2521 IF_DEBUG(linker,belch( " ZERO" ));
2524 Elf_Sym sym = stab[ELF_R_SYM(info)];
2525 /* First see if it is a local symbol. */
2526 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2527 /* Yes, so we can get the address directly from the ELF symbol
2529 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2531 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2532 + stab[ELF_R_SYM(info)].st_value);
2535 /* No, so look up the name in our global table. */
2536 symbol = strtab + sym.st_name;
2537 (void*)S = lookupSymbol( symbol );
2540 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2543 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2546 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2547 (void*)P, (void*)S, (void*)A ));
2548 checkProddableBlock ( oc, pP );
2552 switch (ELF_R_TYPE(info)) {
2553 # ifdef i386_TARGET_ARCH
2554 case R_386_32: *pP = value; break;
2555 case R_386_PC32: *pP = value - P; break;
2558 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2559 oc->fileName, ELF_R_TYPE(info));
2567 /* Do ELF relocations for which explicit addends are supplied.
2568 sparc-solaris relocations appear to be of this form. */
2570 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2571 Elf_Shdr* shdr, int shnum,
2572 Elf_Sym* stab, char* strtab )
2577 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2578 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2579 int target_shndx = shdr[shnum].sh_info;
2580 int symtab_shndx = shdr[shnum].sh_link;
2582 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2583 targ = (Elf_Addr) (ehdrC + shdr[ target_shndx ].sh_offset);
2584 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2585 target_shndx, symtab_shndx ));
2587 for (j = 0; j < nent; j++) {
2588 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
2589 /* This #ifdef only serves to avoid unused-var warnings. */
2590 Elf_Addr offset = rtab[j].r_offset;
2591 Elf_Addr P = targ + offset;
2593 Elf_Addr info = rtab[j].r_info;
2594 Elf_Addr A = rtab[j].r_addend;
2597 # if defined(sparc_TARGET_ARCH)
2598 Elf_Word* pP = (Elf_Word*)P;
2600 # elif defined(ia64_TARGET_ARCH)
2601 Elf64_Xword *pP = (Elf64_Xword *)P;
2605 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2606 j, (void*)offset, (void*)info,
2609 IF_DEBUG(linker,belch( " ZERO" ));
2612 Elf_Sym sym = stab[ELF_R_SYM(info)];
2613 /* First see if it is a local symbol. */
2614 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2615 /* Yes, so we can get the address directly from the ELF symbol
2617 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2619 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2620 + stab[ELF_R_SYM(info)].st_value);
2621 #ifdef ELF_FUNCTION_DESC
2622 /* Make a function descriptor for this function */
2623 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2624 S = allocateFunctionDesc(S + A);
2629 /* No, so look up the name in our global table. */
2630 symbol = strtab + sym.st_name;
2631 (void*)S = lookupSymbol( symbol );
2633 #ifdef ELF_FUNCTION_DESC
2634 /* If a function, already a function descriptor - we would
2635 have to copy it to add an offset. */
2636 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC)
2641 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2644 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2647 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2648 (void*)P, (void*)S, (void*)A ));
2649 /* checkProddableBlock ( oc, (void*)P ); */
2653 switch (ELF_R_TYPE(info)) {
2654 # if defined(sparc_TARGET_ARCH)
2655 case R_SPARC_WDISP30:
2656 w1 = *pP & 0xC0000000;
2657 w2 = (Elf_Word)((value - P) >> 2);
2658 ASSERT((w2 & 0xC0000000) == 0);
2663 w1 = *pP & 0xFFC00000;
2664 w2 = (Elf_Word)(value >> 10);
2665 ASSERT((w2 & 0xFFC00000) == 0);
2671 w2 = (Elf_Word)(value & 0x3FF);
2672 ASSERT((w2 & ~0x3FF) == 0);
2676 /* According to the Sun documentation:
2678 This relocation type resembles R_SPARC_32, except it refers to an
2679 unaligned word. That is, the word to be relocated must be treated
2680 as four separate bytes with arbitrary alignment, not as a word
2681 aligned according to the architecture requirements.
2683 (JRS: which means that freeloading on the R_SPARC_32 case
2684 is probably wrong, but hey ...)
2688 w2 = (Elf_Word)value;
2691 # elif defined(ia64_TARGET_ARCH)
2692 case R_IA64_DIR64LSB:
2693 case R_IA64_FPTR64LSB:
2696 case R_IA64_SEGREL64LSB:
2697 addr = findElfSegment(ehdrC, value);
2700 case R_IA64_GPREL22:
2701 ia64_reloc_gprel22(P, value);
2703 case R_IA64_LTOFF22:
2704 case R_IA64_LTOFF_FPTR22:
2705 addr = allocateGOTEntry(value);
2706 ia64_reloc_gprel22(P, addr);
2708 case R_IA64_PCREL21B:
2709 ia64_reloc_pcrel21(P, S, oc);
2713 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2714 oc->fileName, ELF_R_TYPE(info));
2723 ocResolve_ELF ( ObjectCode* oc )
2727 Elf_Sym* stab = NULL;
2728 char* ehdrC = (char*)(oc->image);
2729 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2730 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2731 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2733 /* first find "the" symbol table */
2734 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2736 /* also go find the string table */
2737 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2739 if (stab == NULL || strtab == NULL) {
2740 belch("%s: can't find string or symbol table", oc->fileName);
2744 /* Process the relocation sections. */
2745 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2747 /* Skip sections called ".rel.stab". These appear to contain
2748 relocation entries that, when done, make the stabs debugging
2749 info point at the right places. We ain't interested in all
2751 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2754 if (shdr[shnum].sh_type == SHT_REL ) {
2755 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2756 shnum, stab, strtab );
2760 if (shdr[shnum].sh_type == SHT_RELA) {
2761 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2762 shnum, stab, strtab );
2767 /* Free the local symbol table; we won't need it again. */
2768 freeHashTable(oc->lochash, NULL);
2776 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2777 * at the front. The following utility functions pack and unpack instructions, and
2778 * take care of the most common relocations.
2781 #ifdef ia64_TARGET_ARCH
2784 ia64_extract_instruction(Elf64_Xword *target)
2787 int slot = (Elf_Addr)target & 3;
2788 (Elf_Addr)target &= ~3;
2796 return ((w1 >> 5) & 0x1ffffffffff);
2798 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2802 barf("ia64_extract_instruction: invalid slot %p", target);
2807 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2809 int slot = (Elf_Addr)target & 3;
2810 (Elf_Addr)target &= ~3;
2815 *target |= value << 5;
2818 *target |= value << 46;
2819 *(target+1) |= value >> 18;
2822 *(target+1) |= value << 23;
2828 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2830 Elf64_Xword instruction;
2831 Elf64_Sxword rel_value;
2833 rel_value = value - gp_val;
2834 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2835 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2837 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2838 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2839 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2840 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2841 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2842 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2846 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2848 Elf64_Xword instruction;
2849 Elf64_Sxword rel_value;
2852 entry = allocatePLTEntry(value, oc);
2854 rel_value = (entry >> 4) - (target >> 4);
2855 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2856 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
2858 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2859 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
2860 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2861 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2868 /* --------------------------------------------------------------------------
2870 * ------------------------------------------------------------------------*/
2872 #if defined(OBJFORMAT_MACHO)
2875 Initial support for MachO linking on Darwin/MacOS X on PowerPC chips
2876 by Wolfgang Thaller (wolfgang.thaller@gmx.net)
2878 I hereby formally apologize for the hackish nature of this code.
2879 Things that need to be done:
2880 *) get common symbols and .bss sections to work properly.
2881 Haskell modules seem to work, but C modules can cause problems
2882 *) implement ocVerifyImage_MachO
2883 *) add more sanity checks. The current code just has to segfault if there's a
2887 static int ocVerifyImage_MachO(ObjectCode* oc)
2889 // FIXME: do some verifying here
2893 static void resolveImports(
2896 struct symtab_command *symLC,
2897 struct section *sect, // ptr to lazy or non-lazy symbol pointer section
2898 unsigned long *indirectSyms,
2899 struct nlist *nlist)
2903 for(i=0;i*4<sect->size;i++)
2905 // according to otool, reserved1 contains the first index into the indirect symbol table
2906 struct nlist *symbol = &nlist[indirectSyms[sect->reserved1+i]];
2907 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
2910 if((symbol->n_type & N_TYPE) == N_UNDF
2911 && (symbol->n_type & N_EXT) && (symbol->n_value != 0))
2912 addr = (void*) (symbol->n_value);
2913 else if((addr = lookupLocalSymbol(oc,nm)) != NULL)
2916 addr = lookupSymbol(nm);
2919 fprintf(stderr, "not found: %s\n", nm);
2923 ((void**)(image + sect->offset))[i] = addr;
2927 static void relocateSection(char *image,
2928 struct symtab_command *symLC, struct nlist *nlist,
2929 struct section* sections, struct section *sect)
2931 struct relocation_info *relocs;
2934 if(!strcmp(sect->sectname,"__la_symbol_ptr"))
2936 else if(!strcmp(sect->sectname,"__nl_symbol_ptr"))
2940 relocs = (struct relocation_info*) (image + sect->reloff);
2944 if(relocs[i].r_address & R_SCATTERED)
2946 struct scattered_relocation_info *scat =
2947 (struct scattered_relocation_info*) &relocs[i];
2951 if(scat->r_length == 2 && scat->r_type == GENERIC_RELOC_VANILLA)
2953 unsigned long* word = (unsigned long*) (image + sect->offset + scat->r_address);
2955 *word = scat->r_value + sect->offset + ((long) image);
2959 continue; // FIXME: I hope it's OK to ignore all the others.
2963 struct relocation_info *reloc = &relocs[i];
2964 if(reloc->r_pcrel && !reloc->r_extern)
2967 if(!reloc->r_pcrel && reloc->r_length == 2)
2971 unsigned long* wordPtr = (unsigned long*) (image + sect->offset + reloc->r_address);
2973 if(reloc->r_type == GENERIC_RELOC_VANILLA)
2977 else if(reloc->r_type == PPC_RELOC_LO16)
2979 word = ((unsigned short*) wordPtr)[1];
2980 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF) << 16;
2982 else if(reloc->r_type == PPC_RELOC_HI16)
2984 word = ((unsigned short*) wordPtr)[1] << 16;
2985 word |= ((unsigned long) relocs[i+1].r_address & 0xFFFF);
2987 else if(reloc->r_type == PPC_RELOC_HA16)
2989 word = ((unsigned short*) wordPtr)[1] << 16;
2990 word += ((short)relocs[i+1].r_address & (short)0xFFFF);
2993 if(!reloc->r_extern)
2996 sections[reloc->r_symbolnum-1].offset
2997 - sections[reloc->r_symbolnum-1].addr
3004 struct nlist *symbol = &nlist[reloc->r_symbolnum];
3005 char *nm = image + symLC->stroff + symbol->n_un.n_strx;
3006 word = (unsigned long) (lookupSymbol(nm));
3010 if(reloc->r_type == GENERIC_RELOC_VANILLA)
3015 else if(reloc->r_type == PPC_RELOC_LO16)
3017 ((unsigned short*) wordPtr)[1] = word & 0xFFFF;
3020 else if(reloc->r_type == PPC_RELOC_HI16)
3022 ((unsigned short*) wordPtr)[1] = (word >> 16) & 0xFFFF;
3025 else if(reloc->r_type == PPC_RELOC_HA16)
3027 ((unsigned short*) wordPtr)[1] = ((word >> 16) & 0xFFFF)
3028 + ((word & (1<<15)) ? 1 : 0);
3033 fprintf(stderr, "unknown reloc\n");
3040 static int ocGetNames_MachO(ObjectCode* oc)
3042 char *image = (char*) oc->image;
3043 struct mach_header *header = (struct mach_header*) image;
3044 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3045 unsigned i,curSymbol;
3046 struct segment_command *segLC = NULL;
3047 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3048 struct symtab_command *symLC = NULL;
3049 struct dysymtab_command *dsymLC = NULL;
3050 struct nlist *nlist;
3051 unsigned long commonSize = 0;
3052 char *commonStorage = NULL;
3053 unsigned long commonCounter;
3055 for(i=0;i<header->ncmds;i++)
3057 if(lc->cmd == LC_SEGMENT)
3058 segLC = (struct segment_command*) lc;
3059 else if(lc->cmd == LC_SYMTAB)
3060 symLC = (struct symtab_command*) lc;
3061 else if(lc->cmd == LC_DYSYMTAB)
3062 dsymLC = (struct dysymtab_command*) lc;
3063 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3066 sections = (struct section*) (segLC+1);
3067 nlist = (struct nlist*) (image + symLC->symoff);
3069 for(i=0;i<segLC->nsects;i++)
3071 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3072 la_ptrs = §ions[i];
3073 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3074 nl_ptrs = §ions[i];
3076 // for now, only add __text and __const to the sections table
3077 else if(!strcmp(sections[i].sectname,"__text"))
3078 addSection(oc, SECTIONKIND_CODE_OR_RODATA,
3079 (void*) (image + sections[i].offset),
3080 (void*) (image + sections[i].offset + sections[i].size));
3081 else if(!strcmp(sections[i].sectname,"__const"))
3082 addSection(oc, SECTIONKIND_RWDATA,
3083 (void*) (image + sections[i].offset),
3084 (void*) (image + sections[i].offset + sections[i].size));
3085 else if(!strcmp(sections[i].sectname,"__data"))
3086 addSection(oc, SECTIONKIND_RWDATA,
3087 (void*) (image + sections[i].offset),
3088 (void*) (image + sections[i].offset + sections[i].size));
3091 // count external symbols defined here
3093 for(i=dsymLC->iextdefsym;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3095 if((nlist[i].n_type & N_TYPE) == N_SECT)
3098 for(i=0;i<symLC->nsyms;i++)
3100 if((nlist[i].n_type & N_TYPE) == N_UNDF
3101 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3103 commonSize += nlist[i].n_value;
3107 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
3108 "ocGetNames_MachO(oc->symbols)");
3110 // insert symbols into hash table
3111 for(i=dsymLC->iextdefsym,curSymbol=0;i<dsymLC->iextdefsym+dsymLC->nextdefsym;i++)
3113 if((nlist[i].n_type & N_TYPE) == N_SECT)
3115 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3116 ghciInsertStrHashTable(oc->fileName, symhash, nm, image +
3117 sections[nlist[i].n_sect-1].offset
3118 - sections[nlist[i].n_sect-1].addr
3119 + nlist[i].n_value);
3120 oc->symbols[curSymbol++] = nm;
3124 // insert local symbols into lochash
3125 for(i=dsymLC->ilocalsym;i<dsymLC->ilocalsym+dsymLC->nlocalsym;i++)
3127 if((nlist[i].n_type & N_TYPE) == N_SECT)
3129 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3130 ghciInsertStrHashTable(oc->fileName, oc->lochash, nm, image +
3131 sections[nlist[i].n_sect-1].offset
3132 - sections[nlist[i].n_sect-1].addr
3133 + nlist[i].n_value);
3138 commonStorage = stgCallocBytes(1,commonSize,"ocGetNames_MachO(common symbols)");
3139 commonCounter = (unsigned long)commonStorage;
3140 for(i=0;i<symLC->nsyms;i++)
3142 if((nlist[i].n_type & N_TYPE) == N_UNDF
3143 && (nlist[i].n_type & N_EXT) && (nlist[i].n_value != 0))
3145 char *nm = image + symLC->stroff + nlist[i].n_un.n_strx;
3146 unsigned long sz = nlist[i].n_value;
3148 nlist[i].n_value = commonCounter;
3150 ghciInsertStrHashTable(oc->fileName, symhash, nm, (void*)commonCounter);
3151 oc->symbols[curSymbol++] = nm;
3153 commonCounter += sz;
3159 static int ocResolve_MachO(ObjectCode* oc)
3161 char *image = (char*) oc->image;
3162 struct mach_header *header = (struct mach_header*) image;
3163 struct load_command *lc = (struct load_command*) (image + sizeof(struct mach_header));
3165 struct segment_command *segLC = NULL;
3166 struct section *sections, *la_ptrs = NULL, *nl_ptrs = NULL;
3167 struct symtab_command *symLC = NULL;
3168 struct dysymtab_command *dsymLC = NULL;
3169 struct nlist *nlist;
3170 unsigned long *indirectSyms;
3172 for(i=0;i<header->ncmds;i++)
3174 if(lc->cmd == LC_SEGMENT)
3175 segLC = (struct segment_command*) lc;
3176 else if(lc->cmd == LC_SYMTAB)
3177 symLC = (struct symtab_command*) lc;
3178 else if(lc->cmd == LC_DYSYMTAB)
3179 dsymLC = (struct dysymtab_command*) lc;
3180 lc = (struct load_command *) ( ((char*)lc) + lc->cmdsize );
3183 sections = (struct section*) (segLC+1);
3184 nlist = (struct nlist*) (image + symLC->symoff);
3186 for(i=0;i<segLC->nsects;i++)
3188 if(!strcmp(sections[i].sectname,"__la_symbol_ptr"))
3189 la_ptrs = §ions[i];
3190 else if(!strcmp(sections[i].sectname,"__nl_symbol_ptr"))
3191 nl_ptrs = §ions[i];
3194 indirectSyms = (unsigned long*) (image + dsymLC->indirectsymoff);
3197 resolveImports(oc,image,symLC,la_ptrs,indirectSyms,nlist);
3199 resolveImports(oc,image,symLC,nl_ptrs,indirectSyms,nlist);
3201 for(i=0;i<segLC->nsects;i++)
3203 relocateSection(image,symLC,nlist,sections,§ions[i]);
3206 /* Free the local symbol table; we won't need it again. */
3207 freeHashTable(oc->lochash, NULL);