X-Git-Url: http://git.megacz.com/?a=blobdiff_plain;f=rts%2FLinker.c;h=26d671e6e9820c4cb0d6a97bd5ef8760c0ece728;hb=b9110541efb85f9489b1f9a0c95445419e61d86d;hp=243eae1b76f8a176ceedf00cca1fee332aa3167a;hpb=8bac478832e0cf9fa7ad1cfc81c08b0b9f13938e;p=ghc-hetmet.git diff --git a/rts/Linker.c b/rts/Linker.c index 243eae1..26d671e 100644 --- a/rts/Linker.c +++ b/rts/Linker.c @@ -27,6 +27,7 @@ #include "Schedule.h" #include "Sparks.h" #include "RtsTypeable.h" +#include "Timer.h" #ifdef HAVE_SYS_TYPES_H #include @@ -59,12 +60,12 @@ #include #endif -#if defined(ia64_HOST_ARCH) || defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS) +#if defined(ia64_HOST_ARCH) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS) #define USE_MMAP #include #include -#if defined(openbsd_HOST_OS) || defined(linux_HOST_OS) || defined(freebsd_HOST_OS) +#if defined(linux_HOST_OS) || defined(freebsd_HOST_OS) || defined(netbsd_HOST_OS) || defined(openbsd_HOST_OS) #ifdef HAVE_UNISTD_H #include #endif @@ -83,7 +84,9 @@ # include # include # include +#if !defined(HAVE_DLFCN_H) # include +#endif #if defined(powerpc_HOST_ARCH) # include #endif @@ -105,7 +108,7 @@ ObjectCode *objects = NULL; /* initially empty */ static int ocVerifyImage_ELF ( ObjectCode* oc ); static int ocGetNames_ELF ( ObjectCode* oc ); static int ocResolve_ELF ( ObjectCode* oc ); -#if defined(powerpc_HOST_ARCH) +#if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH) static int ocAllocateSymbolExtras_ELF ( ObjectCode* oc ); #endif #elif defined(OBJFORMAT_PEi386) @@ -126,9 +129,30 @@ static void machoInitSymbolsWithoutUnderscore( void ); #endif #endif -#if defined(x86_64_HOST_ARCH) && defined(OBJFORMAT_ELF) -static void*x86_64_high_symbol( char *lbl, void *addr ); -#endif +/* on x86_64 we have a problem with relocating symbol references in + * code that was compiled without -fPIC. By default, the small memory + * model is used, which assumes that symbol references can fit in a + * 32-bit slot. The system dynamic linker makes this work for + * references to shared libraries by either (a) allocating a jump + * table slot for code references, or (b) moving the symbol at load + * time (and copying its contents, if necessary) for data references. + * + * We unfortunately can't tell whether symbol references are to code + * or data. So for now we assume they are code (the vast majority + * are), and allocate jump-table slots. Unfortunately this will + * SILENTLY generate crashing code for data references. This hack is + * enabled by X86_64_ELF_NONPIC_HACK. + * + * One workaround is to use shared Haskell libraries. This is + * coming. Another workaround is to keep the static libraries but + * compile them with -fPIC, because that will generate PIC references + * to data which can be relocated. The PIC code is still too green to + * do this systematically, though. + * + * See bug #781 + * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html + */ +#define X86_64_ELF_NONPIC_HACK 1 /* ----------------------------------------------------------------------------- * Built-in symbols from the RTS @@ -151,6 +175,9 @@ typedef struct _RtsSymbolVal { #if !defined (mingw32_HOST_OS) #define RTS_POSIX_ONLY_SYMBOLS \ + SymX(shutdownHaskellAndSignal) \ + Sym(lockFile) \ + Sym(unlockFile) \ SymX(signal_handlers) \ SymX(stg_sig_install) \ Sym(nocldstop) @@ -416,6 +443,22 @@ typedef struct _RtsSymbolVal { SymX(console_handler) #endif +#define RTS_LIBFFI_SYMBOLS \ + Sym(ffi_prep_cif) \ + Sym(ffi_call) \ + Sym(ffi_type_void) \ + Sym(ffi_type_float) \ + Sym(ffi_type_double) \ + Sym(ffi_type_sint64) \ + Sym(ffi_type_uint64) \ + Sym(ffi_type_sint32) \ + Sym(ffi_type_uint32) \ + Sym(ffi_type_sint16) \ + Sym(ffi_type_uint16) \ + Sym(ffi_type_sint8) \ + Sym(ffi_type_uint8) \ + Sym(ffi_type_pointer) + #ifdef TABLES_NEXT_TO_CODE #define RTS_RET_SYMBOLS /* nothing */ #else @@ -438,9 +481,26 @@ typedef struct _RtsSymbolVal { SymX(stg_ap_pppppp_ret) #endif +/* On Windows, we link libgmp.a statically into libHSrts.dll */ +#ifdef mingw32_HOST_OS +#define GMP_SYMS \ + SymX(__gmpz_cmp) \ + SymX(__gmpz_cmp_si) \ + SymX(__gmpz_cmp_ui) \ + SymX(__gmpz_get_si) \ + SymX(__gmpz_get_ui) +#else +#define GMP_SYMS \ + SymExtern(__gmpz_cmp) \ + SymExtern(__gmpz_cmp_si) \ + SymExtern(__gmpz_cmp_ui) \ + SymExtern(__gmpz_get_si) \ + SymExtern(__gmpz_get_ui) +#endif + #define RTS_SYMBOLS \ Maybe_Stable_Names \ - Sym(StgReturn) \ + SymX(StgReturn) \ SymX(stg_enter_info) \ SymX(stg_gc_void_info) \ SymX(__stg_gc_enter_1) \ @@ -477,19 +537,18 @@ typedef struct _RtsSymbolVal { SymX(__encodeDouble) \ SymX(__encodeFloat) \ SymX(addDLL) \ - SymX(__gmpn_gcd_1) \ - SymX(__gmpz_cmp) \ - SymX(__gmpz_cmp_si) \ - SymX(__gmpz_cmp_ui) \ - SymX(__gmpz_get_si) \ - SymX(__gmpz_get_ui) \ + GMP_SYMS \ SymX(__int_encodeDouble) \ + SymX(__word_encodeDouble) \ + SymX(__2Int_encodeDouble) \ SymX(__int_encodeFloat) \ + SymX(__word_encodeFloat) \ SymX(andIntegerzh_fast) \ SymX(atomicallyzh_fast) \ SymX(barf) \ SymX(debugBelch) \ SymX(errorBelch) \ + SymX(asyncExceptionsBlockedzh_fast) \ SymX(blockAsyncExceptionszh_fast) \ SymX(catchzh_fast) \ SymX(catchRetryzh_fast) \ @@ -503,6 +562,8 @@ typedef struct _RtsSymbolVal { SymX(createAdjustor) \ SymX(decodeDoublezh_fast) \ SymX(decodeFloatzh_fast) \ + SymX(decodeDoublezu2Intzh_fast) \ + SymX(decodeFloatzuIntzh_fast) \ SymX(defaultsHook) \ SymX(delayzh_fast) \ SymX(deRefWeakzh_fast) \ @@ -532,6 +593,7 @@ typedef struct _RtsSymbolVal { SymX(hs_perform_gc) \ SymX(hs_free_stable_ptr) \ SymX(hs_free_fun_ptr) \ + SymX(hs_hpc_rootModule) \ SymX(initLinker) \ SymX(unpackClosurezh_fast) \ SymX(getApStackValzh_fast) \ @@ -598,32 +660,38 @@ typedef struct _RtsSymbolVal { SymX(rts_getDouble) \ SymX(rts_getFloat) \ SymX(rts_getInt) \ + SymX(rts_getInt8) \ + SymX(rts_getInt16) \ SymX(rts_getInt32) \ + SymX(rts_getInt64) \ SymX(rts_getPtr) \ SymX(rts_getFunPtr) \ SymX(rts_getStablePtr) \ SymX(rts_getThreadId) \ SymX(rts_getWord) \ + SymX(rts_getWord8) \ + SymX(rts_getWord16) \ SymX(rts_getWord32) \ + SymX(rts_getWord64) \ SymX(rts_lock) \ SymX(rts_mkBool) \ SymX(rts_mkChar) \ SymX(rts_mkDouble) \ SymX(rts_mkFloat) \ SymX(rts_mkInt) \ + SymX(rts_mkInt8) \ SymX(rts_mkInt16) \ SymX(rts_mkInt32) \ SymX(rts_mkInt64) \ - SymX(rts_mkInt8) \ SymX(rts_mkPtr) \ SymX(rts_mkFunPtr) \ SymX(rts_mkStablePtr) \ SymX(rts_mkString) \ SymX(rts_mkWord) \ + SymX(rts_mkWord8) \ SymX(rts_mkWord16) \ SymX(rts_mkWord32) \ SymX(rts_mkWord64) \ - SymX(rts_mkWord8) \ SymX(rts_unlock) \ SymX(rtsSupportsBoundThreads) \ SymX(__hscore_get_saved_termios) \ @@ -636,8 +704,10 @@ typedef struct _RtsSymbolVal { SymX(stackOverflow) \ SymX(stg_CAF_BLACKHOLE_info) \ SymX(awakenBlockedQueue) \ + SymX(startTimer) \ SymX(stg_CHARLIKE_closure) \ - SymX(stg_EMPTY_MVAR_info) \ + SymX(stg_MVAR_CLEAN_info) \ + SymX(stg_MVAR_DIRTY_info) \ SymX(stg_IND_STATIC_info) \ SymX(stg_INTLIKE_closure) \ SymX(stg_MUT_ARR_PTRS_DIRTY_info) \ @@ -700,6 +770,7 @@ typedef struct _RtsSymbolVal { SymX(stg_upd_frame_info) \ SymX(suspendThread) \ SymX(takeMVarzh_fast) \ + SymX(threadStatuszh_fast) \ SymX(timesIntegerzh_fast) \ SymX(tryPutMVarzh_fast) \ SymX(tryTakeMVarzh_fast) \ @@ -712,15 +783,17 @@ typedef struct _RtsSymbolVal { SymX(writeTVarzh_fast) \ SymX(xorIntegerzh_fast) \ SymX(yieldzh_fast) \ - SymX(stg_interp_constr_entry) \ + Sym(stg_interp_constr_entry) \ SymX(allocateExec) \ SymX(freeExec) \ SymX(getAllocations) \ SymX(revertCAFs) \ SymX(RtsFlags) \ - SymX(rts_breakpoint_io_action) \ - SymX(rts_stop_next_breakpoint) \ - SymX(rts_stop_on_exception) \ + Sym(rts_breakpoint_io_action) \ + Sym(rts_stop_next_breakpoint) \ + Sym(rts_stop_on_exception) \ + SymX(stopTimer) \ + SymX(n_capabilities) \ RTS_USER_SIGNALS_SYMBOLS #ifdef SUPPORT_LONG_LONGS @@ -766,6 +839,11 @@ typedef struct _RtsSymbolVal { /* entirely bogus claims about types of these symbols */ #define Sym(vvv) extern void vvv(void); +#if defined(__PIC__) && defined(mingw32_TARGET_OS) +#define SymExtern(vvv) extern void _imp__ ## vvv (void); +#else +#define SymExtern(vvv) SymX(vvv) +#endif #define SymX(vvv) /**/ #define SymX_redirect(vvv,xxx) /**/ RTS_SYMBOLS @@ -776,9 +854,11 @@ RTS_MINGW_ONLY_SYMBOLS RTS_CYGWIN_ONLY_SYMBOLS RTS_DARWIN_ONLY_SYMBOLS RTS_LIBGCC_SYMBOLS +RTS_LIBFFI_SYMBOLS #undef Sym #undef SymX #undef SymX_redirect +#undef SymExtern #ifdef LEADING_UNDERSCORE #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s) @@ -789,6 +869,8 @@ RTS_LIBGCC_SYMBOLS #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \ (void*)(&(vvv)) }, #define SymX(vvv) Sym(vvv) +#define SymExtern(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \ + (void*)DLL_IMPORT_DATA_REF(vvv) }, // SymX_redirect allows us to redirect references to one symbol to // another symbol. See newCAF/newDynCAF for an example. @@ -805,6 +887,7 @@ static RtsSymbolVal rtsSyms[] = { RTS_CYGWIN_ONLY_SYMBOLS RTS_DARWIN_ONLY_SYMBOLS RTS_LIBGCC_SYMBOLS + RTS_LIBFFI_SYMBOLS #if defined(darwin_HOST_OS) && defined(i386_HOST_ARCH) // dyld stub code contains references to this, // but it should never be called because we treat @@ -928,17 +1011,19 @@ typedef static OpenedDLL* opened_dlls = NULL; #endif -char * +const char * addDLL( char *dll_name ) { # if defined(OBJFORMAT_ELF) || defined(OBJFORMAT_MACHO) /* ------------------- ELF DLL loader ------------------- */ void *hdl; - char *errmsg; + const char *errmsg; initLinker(); - hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL); + // omitted: RTLD_NOW + // see http://www.haskell.org/pipermail/cvs-ghc/2007-September/038570.html + hdl= dlopen(dll_name, RTLD_LAZY | RTLD_GLOBAL); if (hdl == NULL) { /* dlopen failed; return a ptr to the error msg. */ @@ -981,14 +1066,19 @@ addDLL( char *dll_name ) sprintf(buf, "%s.DLL", dll_name); instance = LoadLibrary(buf); if (instance == NULL) { - sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv) - instance = LoadLibrary(buf); - if (instance == NULL) { - stgFree(buf); - - /* LoadLibrary failed; return a ptr to the error msg. */ - return "addDLL: unknown error"; - } + if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error; + // KAA: allow loading of drivers (like winspool.drv) + sprintf(buf, "%s.DRV", dll_name); + instance = LoadLibrary(buf); + if (instance == NULL) { + if (GetLastError() != ERROR_MOD_NOT_FOUND) goto error; + // #1883: allow loading of unix-style libfoo.dll DLLs + sprintf(buf, "lib%s.DLL", dll_name); + instance = LoadLibrary(buf); + if (instance == NULL) { + goto error; + } + } } stgFree(buf); @@ -1004,6 +1094,13 @@ addDLL( char *dll_name ) # else barf("addDLL: not implemented on this platform"); # endif + +error: + stgFree(buf); + sysErrorBelch(dll_name); + + /* LoadLibrary failed; return a ptr to the error msg. */ + return "addDLL: could not load DLL"; } /* ----------------------------------------------------------------------------- @@ -1039,26 +1136,27 @@ lookupSymbol( char *lbl ) if (val == NULL) { # if defined(OBJFORMAT_ELF) -# if defined(x86_64_HOST_ARCH) - val = dlsym(dl_prog_handle, lbl); - if (val >= (void *)0x80000000) { - void *new_val; - new_val = x86_64_high_symbol(lbl, val); - IF_DEBUG(linker,debugBelch("lookupSymbol: relocating out of range symbol: %s = %p, now %p\n", lbl, val, new_val)); - return new_val; - } else { - return val; - } -# else return dlsym(dl_prog_handle, lbl); -# endif # elif defined(OBJFORMAT_MACHO) +# if HAVE_DLFCN_H + /* On OS X 10.3 and later, we use dlsym instead of the old legacy + interface. + + HACK: On OS X, global symbols are prefixed with an underscore. + However, dlsym wants us to omit the leading underscore from the + symbol name. For now, we simply strip it off here (and ONLY + here). + */ + ASSERT(lbl[0] == '_'); + return dlsym(dl_prog_handle, lbl+1); +# else if(NSIsSymbolNameDefined(lbl)) { NSSymbol symbol = NSLookupAndBindSymbol(lbl); return NSAddressOfSymbol(symbol); } else { return NULL; } +# endif /* HAVE_DLFCN_H */ # elif defined(OBJFORMAT_PEi386) OpenedDLL* o_dll; void* sym; @@ -1092,23 +1190,6 @@ lookupSymbol( char *lbl ) } } -static -__attribute((unused)) -void * -lookupLocalSymbol( ObjectCode* oc, char *lbl ) -{ - void *val; - initLinker() ; - val = lookupStrHashTable(oc->lochash, lbl); - - if (val == NULL) { - return NULL; - } else { - return val; - } -} - - /* ----------------------------------------------------------------------------- * Debugging aid: look in GHCi's object symbol tables for symbols * within DELTA bytes of the specified address, and show their names. @@ -1130,11 +1211,7 @@ void ghci_enquire ( char* addr ) for (i = 0; i < oc->n_symbols; i++) { sym = oc->symbols[i]; if (sym == NULL) continue; - // debugBelch("enquire %p %p\n", sym, oc->lochash); a = NULL; - if (oc->lochash != NULL) { - a = lookupStrHashTable(oc->lochash, sym); - } if (a == NULL) { a = lookupStrHashTable(symhash, sym); } @@ -1219,7 +1296,6 @@ loadObj( char *path ) oc->fileSize = st.st_size; oc->symbols = NULL; oc->sections = NULL; - oc->lochash = allocStrHashTable(); oc->proddables = NULL; /* chain it onto the list of objects */ @@ -1257,13 +1333,20 @@ loadObj( char *path ) /* Link objects into the lower 2Gb on x86_64. GHC assumes the * small memory model on this architecture (see gcc docs, * -mcmodel=small). + * + * MAP_32BIT not available on OpenBSD/amd64 */ -#ifdef x86_64_HOST_ARCH +#if defined(x86_64_HOST_ARCH) && defined(MAP_32BIT) #define EXTRA_MAP_FLAGS MAP_32BIT #else #define EXTRA_MAP_FLAGS 0 #endif + /* MAP_ANONYMOUS is MAP_ANON on some systems, e.g. OpenBSD */ +#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON) +#define MAP_ANONYMOUS MAP_ANON +#endif + oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|EXTRA_MAP_FLAGS, fd, 0); if (oc->image == MAP_FAILED) @@ -1310,7 +1393,7 @@ loadObj( char *path ) # if defined(OBJFORMAT_MACHO) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)) r = ocAllocateSymbolExtras_MachO ( oc ); if (!r) { return r; } -# elif defined(OBJFORMAT_ELF) && defined(powerpc_HOST_ARCH) +# elif defined(OBJFORMAT_ELF) && (defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)) r = ocAllocateSymbolExtras_ELF ( oc ); if (!r) { return r; } #endif @@ -1421,9 +1504,6 @@ unloadObj( char *path ) stgFree(oc->fileName); stgFree(oc->symbols); stgFree(oc->sections); - /* The local hash table should have been freed at the end - of the ocResolve_ call on it. */ - ASSERT(oc->lochash == NULL); stgFree(oc); return 1; } @@ -1492,8 +1572,7 @@ static void addSection ( ObjectCode* oc, SectionKind kind, * them right next to the object code itself. */ -#if defined(powerpc_HOST_ARCH) || (defined(x86_64_HOST_ARCH) \ - && defined(darwin_TARGET_OS)) +#if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH) /* ocAllocateSymbolExtras @@ -1520,10 +1599,12 @@ static int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first ) int pagesize, n, m; #endif int aligned; +#ifndef USE_MMAP int misalignment = 0; -#if darwin_HOST_OS +#ifdef darwin_HOST_OS misalignment = oc->misalignment; #endif +#endif if( count > 0 ) { @@ -2221,6 +2302,8 @@ ocGetNames_PEi386 ( ObjectCode* oc ) && 0 != strcmp(".ctors", sectab_i->Name) /* ignore section generated from .ident */ && 0!= strcmp("/4", sectab_i->Name) + /* ignore unknown section that appeared in gcc 3.4.5(?) */ + && 0!= strcmp(".reloc", sectab_i->Name) ) { errorBelch("Unknown PEi386 section name `%s' (while processing: %s)", sectab_i->Name, oc->fileName); return 0; @@ -2438,13 +2521,9 @@ ocResolve_PEi386 ( ObjectCode* oc ) + sym->Value); } else { copyName ( sym->Name, strtab, symbol, 1000-1 ); - S = (UInt32) lookupLocalSymbol( oc, symbol ); - if ((void*)S != NULL) goto foundit; S = (UInt32) lookupSymbol( symbol ); if ((void*)S != NULL) goto foundit; zapTrailingAtSign ( symbol ); - S = (UInt32) lookupLocalSymbol( oc, symbol ); - if ((void*)S != NULL) goto foundit; S = (UInt32) lookupSymbol( symbol ); if ((void*)S != NULL) goto foundit; /* Newline first because the interactive linker has printed "linking..." */ @@ -2526,15 +2605,20 @@ ocResolve_PEi386 ( ObjectCode* oc ) #endif #if !defined(openbsd_HOST_OS) -#include +# include #else /* openbsd elf has things in different places, with diff names */ -#include -#include -#define R_386_32 RELOC_32 -#define R_386_PC32 RELOC_PC32 +# include +# include +# define R_386_32 RELOC_32 +# define R_386_PC32 RELOC_PC32 #endif +/* If elf.h doesn't define it */ +# ifndef R_X86_64_PC64 +# define R_X86_64_PC64 24 +# endif + /* * Define a set of types which can be used for both ELF32 and ELF64 */ @@ -2699,64 +2783,6 @@ PLTSize(void) #endif -#if x86_64_HOST_ARCH -// On x86_64, 32-bit relocations are often used, which requires that -// we can resolve a symbol to a 32-bit offset. However, shared -// libraries are placed outside the 2Gb area, which leaves us with a -// problem when we need to give a 32-bit offset to a symbol in a -// shared library. -// -// For a function symbol, we can allocate a bounce sequence inside the -// 2Gb area and resolve the symbol to this. The bounce sequence is -// simply a long jump instruction to the real location of the symbol. -// -// For data references, we're screwed. -// -typedef struct { - unsigned char jmp[8]; /* 6 byte instruction: jmpq *0x00000002(%rip) */ - void *addr; -} x86_64_bounce; - -#define X86_64_BB_SIZE 1024 - -static x86_64_bounce *x86_64_bounce_buffer = NULL; -static nat x86_64_bb_next_off; - -static void* -x86_64_high_symbol( char *lbl, void *addr ) -{ - x86_64_bounce *bounce; - - if ( x86_64_bounce_buffer == NULL || - x86_64_bb_next_off >= X86_64_BB_SIZE ) { - x86_64_bounce_buffer = - mmap(NULL, X86_64_BB_SIZE * sizeof(x86_64_bounce), - PROT_EXEC|PROT_READ|PROT_WRITE, - MAP_PRIVATE|MAP_32BIT|MAP_ANONYMOUS, -1, 0); - if (x86_64_bounce_buffer == MAP_FAILED) { - barf("x86_64_high_symbol: mmap failed"); - } - x86_64_bb_next_off = 0; - } - bounce = &x86_64_bounce_buffer[x86_64_bb_next_off]; - bounce->jmp[0] = 0xff; - bounce->jmp[1] = 0x25; - bounce->jmp[2] = 0x02; - bounce->jmp[3] = 0x00; - bounce->jmp[4] = 0x00; - bounce->jmp[5] = 0x00; - bounce->addr = addr; - x86_64_bb_next_off++; - - IF_DEBUG(linker, debugBelch("x86_64: allocated bounce entry for %s->%p at %p\n", - lbl, addr, bounce)); - - insertStrHashTable(symhash, lbl, bounce); - return bounce; -} -#endif - - /* * Generic ELF functions */ @@ -2859,9 +2885,12 @@ ocVerifyImage_ELF ( ObjectCode* oc ) case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break; #ifdef EM_X86_64 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break; +#elif defined(EM_AMD64) + case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break; #endif default: IF_DEBUG(linker,debugBelch( "unknown" )); - errorBelch("%s: unknown architecture", oc->fileName); + errorBelch("%s: unknown architecture (e_machine == %d)" + , oc->fileName, ehdr->e_machine); return 0; } @@ -3122,7 +3151,7 @@ ocGetNames_ELF ( ObjectCode* oc ) if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC) ad = (char *)allocateFunctionDesc((Elf_Addr)ad); #endif - IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s", + IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s\n", ad, oc->fileName, nm )); isLocal = FALSE; } @@ -3469,28 +3498,73 @@ do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC, { StgInt64 off = value - P; if (off >= 0x7fffffffL || off < -0x80000000L) { - barf("R_X86_64_PC32 relocation out of range: %s = %p", - symbol, off); - } +#if X86_64_ELF_NONPIC_HACK + StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S) + -> jumpIsland; + off = pltAddress + A - P; +#else + barf("R_X86_64_PC32 relocation out of range: %s = %p\nRecompile %s with -fPIC.", + symbol, off, oc->fileName ); +#endif + } + *(Elf64_Word *)P = (Elf64_Word)off; + break; + } + + case R_X86_64_PC64: + { + StgInt64 off = value - P; *(Elf64_Word *)P = (Elf64_Word)off; break; } case R_X86_64_32: if (value >= 0x7fffffffL) { - barf("R_X86_64_32 relocation out of range: %s = %p\n", - symbol, value); - } +#if X86_64_ELF_NONPIC_HACK + StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S) + -> jumpIsland; + value = pltAddress + A; +#else + barf("R_X86_64_32 relocation out of range: %s = %p\nRecompile %s with -fPIC.", + symbol, value, oc->fileName ); +#endif + } *(Elf64_Word *)P = (Elf64_Word)value; break; case R_X86_64_32S: if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) { - barf("R_X86_64_32S relocation out of range: %s = %p\n", - symbol, value); +#if X86_64_ELF_NONPIC_HACK + StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S) + -> jumpIsland; + value = pltAddress + A; +#else + barf("R_X86_64_32S relocation out of range: %s = %p\nRecompile %s with -fPIC.", + symbol, value, oc->fileName ); +#endif } *(Elf64_Sword *)P = (Elf64_Sword)value; break; + + case R_X86_64_GOTPCREL: + { + StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr; + StgInt64 off = gotAddress + A - P; + *(Elf64_Word *)P = (Elf64_Word)off; + break; + } + + case R_X86_64_PLT32: + { + StgInt64 off = value - P; + if (off >= 0x7fffffffL || off < -0x80000000L) { + StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S) + -> jumpIsland; + off = pltAddress + A - P; + } + *(Elf64_Word *)P = (Elf64_Word)off; + break; + } #endif default: @@ -3539,10 +3613,6 @@ ocResolve_ELF ( ObjectCode* oc ) } } - /* Free the local symbol table; we won't need it again. */ - freeHashTable(oc->lochash, NULL); - oc->lochash = NULL; - #if defined(powerpc_HOST_ARCH) ocFlushInstructionCache( oc ); #endif @@ -3643,10 +3713,10 @@ ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc) #endif /* ia64 */ /* - * PowerPC ELF specifics + * PowerPC & X86_64 ELF specifics */ -#ifdef powerpc_HOST_ARCH +#if defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH) static int ocAllocateSymbolExtras_ELF( ObjectCode *oc ) { @@ -3670,7 +3740,7 @@ static int ocAllocateSymbolExtras_ELF( ObjectCode *oc ) if( shdr[i].sh_entsize != sizeof( Elf_Sym ) ) { errorBelch( "The entry size (%d) of the symtab isn't %d\n", - shdr[i].sh_entsize, sizeof( Elf_Sym ) ); + (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) ); return 0; } @@ -3821,8 +3891,6 @@ static int resolveImports( if((symbol->n_type & N_TYPE) == N_UNDF && (symbol->n_type & N_EXT) && (symbol->n_value != 0)) addr = (void*) (symbol->n_value); - else if((addr = lookupLocalSymbol(oc,nm)) != NULL) - ; else addr = lookupSymbol(nm); if(!addr) @@ -4195,7 +4263,7 @@ static int relocateSection( #ifdef powerpc_HOST_ARCH // In the .o file, this should be a relative jump to NULL // and we'll change it to a relative jump to the symbol - ASSERT(-word == reloc->r_address); + ASSERT(word + reloc->r_address == 0); jumpIsland = (unsigned long) &makeSymbolExtra(oc, reloc->r_symbolnum, @@ -4469,10 +4537,6 @@ static int ocResolve_MachO(ObjectCode* oc) return 0; } - /* Free the local symbol table; we won't need it again. */ - freeHashTable(oc->lochash, NULL); - oc->lochash = NULL; - #if defined (powerpc_HOST_ARCH) ocFlushInstructionCache( oc ); #endif