1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.91 2002/06/09 13:37:41 matthewc Exp $
4 * (c) The GHC Team, 2000, 2001
8 * ---------------------------------------------------------------------------*/
11 #include "PosixSource.h"
18 #include "LinkerInternals.h"
20 #include "StoragePriv.h"
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
27 #ifdef HAVE_SYS_STAT_H
35 #if defined(cygwin32_TARGET_OS)
40 #ifdef HAVE_SYS_TIME_H
44 #include <sys/fcntl.h>
45 #include <sys/termios.h>
46 #include <sys/utime.h>
47 #include <sys/utsname.h>
51 #if defined(ia64_TARGET_ARCH)
57 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
58 # define OBJFORMAT_ELF
59 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
60 # define OBJFORMAT_PEi386
64 /* Hash table mapping symbol names to Symbol */
65 /*Str*/HashTable *symhash;
67 #if defined(OBJFORMAT_ELF)
68 static int ocVerifyImage_ELF ( ObjectCode* oc );
69 static int ocGetNames_ELF ( ObjectCode* oc );
70 static int ocResolve_ELF ( ObjectCode* oc );
71 #elif defined(OBJFORMAT_PEi386)
72 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
73 static int ocGetNames_PEi386 ( ObjectCode* oc );
74 static int ocResolve_PEi386 ( ObjectCode* oc );
77 /* -----------------------------------------------------------------------------
78 * Built-in symbols from the RTS
81 typedef struct _RtsSymbolVal {
88 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
90 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
91 SymX(makeStableNamezh_fast) \
92 SymX(finalizzeWeakzh_fast)
94 /* These are not available in GUM!!! -- HWL */
95 #define Maybe_ForeignObj
96 #define Maybe_Stable_Names
99 #if !defined (mingw32_TARGET_OS)
100 #define RTS_POSIX_ONLY_SYMBOLS \
101 SymX(stg_sig_install) \
105 #if defined (cygwin32_TARGET_OS)
106 #define RTS_MINGW_ONLY_SYMBOLS /**/
107 /* Don't have the ability to read import libs / archives, so
108 * we have to stupidly list a lot of what libcygwin.a
111 #define RTS_CYGWIN_ONLY_SYMBOLS \
193 #elif !defined(mingw32_TARGET_OS)
194 #define RTS_MINGW_ONLY_SYMBOLS /**/
195 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
196 #else /* defined(mingw32_TARGET_OS) */
197 #define RTS_POSIX_ONLY_SYMBOLS /**/
198 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
200 /* These are statically linked from the mingw libraries into the ghc
201 executable, so we have to employ this hack. */
202 #define RTS_MINGW_ONLY_SYMBOLS \
215 SymX(getservbyname) \
216 SymX(getservbyport) \
217 SymX(getprotobynumber) \
218 SymX(getprotobyname) \
219 SymX(gethostbyname) \
220 SymX(gethostbyaddr) \
255 Sym(_imp___timezone) \
271 # define MAIN_CAP_SYM SymX(MainCapability)
273 # define MAIN_CAP_SYM
276 #define RTS_SYMBOLS \
280 Sym(__stginit_GHCziPrim) \
291 Sym(stg_enterStackTop) \
294 SymX(__stg_gc_enter_1) \
295 SymX(stg_gc_enter_2) \
296 SymX(stg_gc_enter_3) \
297 SymX(stg_gc_enter_4) \
298 SymX(stg_gc_enter_5) \
299 SymX(stg_gc_enter_6) \
300 SymX(stg_gc_enter_7) \
301 SymX(stg_gc_enter_8) \
303 SymX(stg_gc_noregs) \
305 SymX(stg_gc_unbx_r1) \
306 SymX(stg_gc_unpt_r1) \
307 SymX(stg_gc_ut_0_1) \
308 SymX(stg_gc_ut_1_0) \
310 SymX(stg_yield_to_interpreter) \
313 SymX(MallocFailHook) \
314 SymX(NoRunnableThreadsHook) \
316 SymX(OutOfHeapHook) \
317 SymX(PatErrorHdrHook) \
318 SymX(PostTraceHook) \
320 SymX(StackOverflowHook) \
321 SymX(__encodeDouble) \
322 SymX(__encodeFloat) \
325 SymX(__gmpz_cmp_si) \
326 SymX(__gmpz_cmp_ui) \
327 SymX(__gmpz_get_si) \
328 SymX(__gmpz_get_ui) \
329 SymX(__int_encodeDouble) \
330 SymX(__int_encodeFloat) \
331 SymX(andIntegerzh_fast) \
332 SymX(blockAsyncExceptionszh_fast) \
335 SymX(complementIntegerzh_fast) \
336 SymX(cmpIntegerzh_fast) \
337 SymX(cmpIntegerIntzh_fast) \
338 SymX(createAdjustor) \
339 SymX(decodeDoublezh_fast) \
340 SymX(decodeFloatzh_fast) \
343 SymX(deRefWeakzh_fast) \
344 SymX(deRefStablePtrzh_fast) \
345 SymX(divExactIntegerzh_fast) \
346 SymX(divModIntegerzh_fast) \
348 SymX(forkProcesszh_fast) \
349 SymX(freeHaskellFunctionPtr) \
350 SymX(freeStablePtr) \
351 SymX(gcdIntegerzh_fast) \
352 SymX(gcdIntegerIntzh_fast) \
353 SymX(gcdIntzh_fast) \
356 SymX(int2Integerzh_fast) \
357 SymX(integer2Intzh_fast) \
358 SymX(integer2Wordzh_fast) \
359 SymX(isDoubleDenormalized) \
360 SymX(isDoubleInfinite) \
362 SymX(isDoubleNegativeZero) \
363 SymX(isEmptyMVarzh_fast) \
364 SymX(isFloatDenormalized) \
365 SymX(isFloatInfinite) \
367 SymX(isFloatNegativeZero) \
368 SymX(killThreadzh_fast) \
369 SymX(makeStablePtrzh_fast) \
370 SymX(minusIntegerzh_fast) \
371 SymX(mkApUpd0zh_fast) \
372 SymX(myThreadIdzh_fast) \
373 SymX(labelThreadzh_fast) \
374 SymX(newArrayzh_fast) \
375 SymX(newBCOzh_fast) \
376 SymX(newByteArrayzh_fast) \
378 SymX(newMVarzh_fast) \
379 SymX(newMutVarzh_fast) \
380 SymX(newPinnedByteArrayzh_fast) \
381 SymX(orIntegerzh_fast) \
383 SymX(plusIntegerzh_fast) \
386 SymX(putMVarzh_fast) \
387 SymX(quotIntegerzh_fast) \
388 SymX(quotRemIntegerzh_fast) \
390 SymX(remIntegerzh_fast) \
391 SymX(resetNonBlockingFd) \
394 SymX(rts_checkSchedStatus) \
397 SymX(rts_evalLazyIO) \
402 SymX(rts_getDouble) \
407 SymX(rts_getStablePtr) \
408 SymX(rts_getThreadId) \
410 SymX(rts_getWord32) \
422 SymX(rts_mkStablePtr) \
431 SymX(shutdownHaskellAndExit) \
432 SymX(stable_ptr_table) \
433 SymX(stackOverflow) \
434 SymX(stg_CAF_BLACKHOLE_info) \
435 SymX(stg_CHARLIKE_closure) \
436 SymX(stg_EMPTY_MVAR_info) \
437 SymX(stg_IND_STATIC_info) \
438 SymX(stg_INTLIKE_closure) \
439 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
440 SymX(stg_WEAK_info) \
441 SymX(stg_ap_1_upd_info) \
442 SymX(stg_ap_2_upd_info) \
443 SymX(stg_ap_3_upd_info) \
444 SymX(stg_ap_4_upd_info) \
445 SymX(stg_ap_5_upd_info) \
446 SymX(stg_ap_6_upd_info) \
447 SymX(stg_ap_7_upd_info) \
448 SymX(stg_ap_8_upd_info) \
450 SymX(stg_sel_0_upd_info) \
451 SymX(stg_sel_10_upd_info) \
452 SymX(stg_sel_11_upd_info) \
453 SymX(stg_sel_12_upd_info) \
454 SymX(stg_sel_13_upd_info) \
455 SymX(stg_sel_14_upd_info) \
456 SymX(stg_sel_15_upd_info) \
457 SymX(stg_sel_1_upd_info) \
458 SymX(stg_sel_2_upd_info) \
459 SymX(stg_sel_3_upd_info) \
460 SymX(stg_sel_4_upd_info) \
461 SymX(stg_sel_5_upd_info) \
462 SymX(stg_sel_6_upd_info) \
463 SymX(stg_sel_7_upd_info) \
464 SymX(stg_sel_8_upd_info) \
465 SymX(stg_sel_9_upd_info) \
466 SymX(stg_seq_frame_info) \
467 SymX(stg_upd_frame_info) \
468 SymX(__stg_update_PAP) \
469 SymX(suspendThread) \
470 SymX(takeMVarzh_fast) \
471 SymX(timesIntegerzh_fast) \
472 SymX(tryPutMVarzh_fast) \
473 SymX(tryTakeMVarzh_fast) \
474 SymX(unblockAsyncExceptionszh_fast) \
475 SymX(unsafeThawArrayzh_fast) \
476 SymX(waitReadzh_fast) \
477 SymX(waitWritezh_fast) \
478 SymX(word2Integerzh_fast) \
479 SymX(xorIntegerzh_fast) \
482 #ifdef SUPPORT_LONG_LONGS
483 #define RTS_LONG_LONG_SYMS \
484 SymX(int64ToIntegerzh_fast) \
485 SymX(word64ToIntegerzh_fast)
487 #define RTS_LONG_LONG_SYMS /* nothing */
490 #ifdef ia64_TARGET_ARCH
491 /* force these symbols to be present */
492 #define RTS_EXTRA_SYMBOLS \
495 #define RTS_EXTRA_SYMBOLS /* nothing */
498 /* entirely bogus claims about types of these symbols */
499 #define Sym(vvv) extern void (vvv);
500 #define SymX(vvv) /**/
504 RTS_POSIX_ONLY_SYMBOLS
505 RTS_MINGW_ONLY_SYMBOLS
506 RTS_CYGWIN_ONLY_SYMBOLS
510 #ifdef LEADING_UNDERSCORE
511 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
513 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
516 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
518 #define SymX(vvv) Sym(vvv)
520 static RtsSymbolVal rtsSyms[] = {
524 RTS_POSIX_ONLY_SYMBOLS
525 RTS_MINGW_ONLY_SYMBOLS
526 RTS_CYGWIN_ONLY_SYMBOLS
527 { 0, 0 } /* sentinel */
530 /* -----------------------------------------------------------------------------
531 * Insert symbols into hash tables, checking for duplicates.
533 static void ghciInsertStrHashTable ( char* obj_name,
539 if (lookupHashTable(table, (StgWord)key) == NULL)
541 insertStrHashTable(table, (StgWord)key, data);
546 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
548 "whilst processing object file\n"
550 "This could be caused by:\n"
551 " * Loading two different object files which export the same symbol\n"
552 " * Specifying the same object file twice on the GHCi command line\n"
553 " * An incorrect `package.conf' entry, causing some object to be\n"
555 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
564 /* -----------------------------------------------------------------------------
565 * initialize the object linker
567 #if defined(OBJFORMAT_ELF)
568 static void *dl_prog_handle;
576 symhash = allocStrHashTable();
578 /* populate the symbol table with stuff from the RTS */
579 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
580 ghciInsertStrHashTable("(GHCi built-in symbols)",
581 symhash, sym->lbl, sym->addr);
583 # if defined(OBJFORMAT_ELF)
584 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
588 /* -----------------------------------------------------------------------------
589 * Add a DLL from which symbols may be found. In the ELF case, just
590 * do RTLD_GLOBAL-style add, so no further messing around needs to
591 * happen in order that symbols in the loaded .so are findable --
592 * lookupSymbol() will subsequently see them by dlsym on the program's
593 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
595 * In the PEi386 case, open the DLLs and put handles to them in a
596 * linked list. When looking for a symbol, try all handles in the
600 #if defined(OBJFORMAT_PEi386)
601 /* A record for storing handles into DLLs. */
606 struct _OpenedDLL* next;
611 /* A list thereof. */
612 static OpenedDLL* opened_dlls = NULL;
618 addDLL( char *dll_name )
620 # if defined(OBJFORMAT_ELF)
624 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
626 /* dlopen failed; return a ptr to the error msg. */
628 if (errmsg == NULL) errmsg = "addDLL: unknown error";
635 # elif defined(OBJFORMAT_PEi386)
637 /* Add this DLL to the list of DLLs in which to search for symbols.
638 The path argument is ignored. */
643 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
645 /* See if we've already got it, and ignore if so. */
646 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
647 if (0 == strcmp(o_dll->name, dll_name))
651 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
652 sprintf(buf, "%s.DLL", dll_name);
653 instance = LoadLibrary(buf);
654 if (instance == NULL) {
655 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
656 instance = LoadLibrary(buf);
657 if (instance == NULL) {
660 /* LoadLibrary failed; return a ptr to the error msg. */
661 return "addDLL: unknown error";
666 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
667 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
668 strcpy(o_dll->name, dll_name);
669 o_dll->instance = instance;
670 o_dll->next = opened_dlls;
675 barf("addDLL: not implemented on this platform");
679 /* -----------------------------------------------------------------------------
680 * lookup a symbol in the hash table
683 lookupSymbol( char *lbl )
686 ASSERT(symhash != NULL);
687 val = lookupStrHashTable(symhash, lbl);
690 # if defined(OBJFORMAT_ELF)
691 return dlsym(dl_prog_handle, lbl);
692 # elif defined(OBJFORMAT_PEi386)
695 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
696 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
698 /* HACK: if the name has an initial underscore, try stripping
699 it off & look that up first. I've yet to verify whether there's
700 a Rule that governs whether an initial '_' *should always* be
701 stripped off when mapping from import lib name to the DLL name.
703 sym = GetProcAddress(o_dll->instance, (lbl+1));
705 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
709 sym = GetProcAddress(o_dll->instance, lbl);
711 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
726 __attribute((unused))
728 lookupLocalSymbol( ObjectCode* oc, char *lbl )
731 val = lookupStrHashTable(oc->lochash, lbl);
741 /* -----------------------------------------------------------------------------
742 * Debugging aid: look in GHCi's object symbol tables for symbols
743 * within DELTA bytes of the specified address, and show their names.
746 void ghci_enquire ( char* addr );
748 void ghci_enquire ( char* addr )
753 const int DELTA = 64;
755 for (oc = objects; oc; oc = oc->next) {
756 for (i = 0; i < oc->n_symbols; i++) {
757 sym = oc->symbols[i];
758 if (sym == NULL) continue;
759 /* fprintf(stderr, "enquire %p %p\n", sym, oc->lochash); */
761 if (oc->lochash != NULL)
762 a = lookupStrHashTable(oc->lochash, sym);
764 a = lookupStrHashTable(symhash, sym);
766 /* fprintf(stderr, "ghci_enquire: can't find %s\n", sym); */
768 else if (addr-DELTA <= a && a <= addr+DELTA) {
769 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
776 #ifdef ia64_TARGET_ARCH
777 static unsigned int PLTSize(void);
780 /* -----------------------------------------------------------------------------
781 * Load an obj (populate the global symbol table, but don't resolve yet)
783 * Returns: 1 if ok, 0 on error.
786 loadObj( char *path )
798 /* fprintf(stderr, "loadObj %s\n", path ); */
800 /* Check that we haven't already loaded this object. Don't give up
801 at this stage; ocGetNames_* will barf later. */
805 for (o = objects; o; o = o->next) {
806 if (0 == strcmp(o->fileName, path))
812 "GHCi runtime linker: warning: looks like you're trying to load the\n"
813 "same object file twice:\n"
815 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
821 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
823 # if defined(OBJFORMAT_ELF)
824 oc->formatName = "ELF";
825 # elif defined(OBJFORMAT_PEi386)
826 oc->formatName = "PEi386";
829 barf("loadObj: not implemented on this platform");
833 if (r == -1) { return 0; }
835 /* sigh, strdup() isn't a POSIX function, so do it the long way */
836 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
837 strcpy(oc->fileName, path);
839 oc->fileSize = st.st_size;
842 oc->lochash = allocStrHashTable();
843 oc->proddables = NULL;
845 /* chain it onto the list of objects */
849 fd = open(path, O_RDONLY);
851 barf("loadObj: can't open `%s'", path);
854 #define ROUND_UP(x,size) ((x + size - 1) & ~(size - 1))
856 /* On many architectures malloc'd memory isn't executable, so we need to use mmap. */
858 fd = open(path, O_RDONLY);
860 barf("loadObj: can't open `%s'", path);
862 pagesize = getpagesize();
864 #ifdef ia64_TARGET_ARCH
865 /* The PLT needs to be right before the object */
866 n = ROUND_UP(PLTSize(), pagesize);
867 oc->plt = mmap(NULL, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
868 if (oc->plt == MAP_FAILED)
869 barf("loadObj: can't allocate PLT");
872 map_addr = oc->plt + n;
875 n = ROUND_UP(oc->fileSize, pagesize);
876 oc->image = mmap(map_addr, n, PROT_EXEC|PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
877 if (oc->image == MAP_FAILED)
878 barf("loadObj: can't map `%s'", path);
882 #else /* !USE_MMAP */
884 oc->image = stgMallocBytes(oc->fileSize, "loadObj(image)");
886 /* load the image into memory */
887 f = fopen(path, "rb");
889 barf("loadObj: can't read `%s'", path);
891 n = fread ( oc->image, 1, oc->fileSize, f );
892 if (n != oc->fileSize)
893 barf("loadObj: error whilst reading `%s'", path);
897 #endif /* USE_MMAP */
899 /* verify the in-memory image */
900 # if defined(OBJFORMAT_ELF)
901 r = ocVerifyImage_ELF ( oc );
902 # elif defined(OBJFORMAT_PEi386)
903 r = ocVerifyImage_PEi386 ( oc );
905 barf("loadObj: no verify method");
907 if (!r) { return r; }
909 /* build the symbol list for this image */
910 # if defined(OBJFORMAT_ELF)
911 r = ocGetNames_ELF ( oc );
912 # elif defined(OBJFORMAT_PEi386)
913 r = ocGetNames_PEi386 ( oc );
915 barf("loadObj: no getNames method");
917 if (!r) { return r; }
919 /* loaded, but not resolved yet */
920 oc->status = OBJECT_LOADED;
925 /* -----------------------------------------------------------------------------
926 * resolve all the currently unlinked objects in memory
928 * Returns: 1 if ok, 0 on error.
936 for (oc = objects; oc; oc = oc->next) {
937 if (oc->status != OBJECT_RESOLVED) {
938 # if defined(OBJFORMAT_ELF)
939 r = ocResolve_ELF ( oc );
940 # elif defined(OBJFORMAT_PEi386)
941 r = ocResolve_PEi386 ( oc );
943 barf("resolveObjs: not implemented on this platform");
945 if (!r) { return r; }
946 oc->status = OBJECT_RESOLVED;
952 /* -----------------------------------------------------------------------------
953 * delete an object from the pool
956 unloadObj( char *path )
958 ObjectCode *oc, *prev;
960 ASSERT(symhash != NULL);
961 ASSERT(objects != NULL);
964 for (oc = objects; oc; prev = oc, oc = oc->next) {
965 if (!strcmp(oc->fileName,path)) {
967 /* Remove all the mappings for the symbols within this
972 for (i = 0; i < oc->n_symbols; i++) {
973 if (oc->symbols[i] != NULL) {
974 removeStrHashTable(symhash, oc->symbols[i], NULL);
982 prev->next = oc->next;
985 /* We're going to leave this in place, in case there are
986 any pointers from the heap into it: */
987 /* free(oc->image); */
991 /* The local hash table should have been freed at the end
992 of the ocResolve_ call on it. */
993 ASSERT(oc->lochash == NULL);
999 belch("unloadObj: can't find `%s' to unload", path);
1003 /* -----------------------------------------------------------------------------
1004 * Sanity checking. For each ObjectCode, maintain a list of address ranges
1005 * which may be prodded during relocation, and abort if we try and write
1006 * outside any of these.
1008 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
1011 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
1012 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
1016 pb->next = oc->proddables;
1017 oc->proddables = pb;
1020 static void checkProddableBlock ( ObjectCode* oc, void* addr )
1023 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
1024 char* s = (char*)(pb->start);
1025 char* e = s + pb->size - 1;
1026 char* a = (char*)addr;
1027 /* Assumes that the biggest fixup involves a 4-byte write. This
1028 probably needs to be changed to 8 (ie, +7) on 64-bit
1030 if (a >= s && (a+3) <= e) return;
1032 barf("checkProddableBlock: invalid fixup in runtime linker");
1035 /* -----------------------------------------------------------------------------
1036 * Section management.
1038 static void addSection ( ObjectCode* oc, SectionKind kind,
1039 void* start, void* end )
1041 Section* s = stgMallocBytes(sizeof(Section), "addSection");
1045 s->next = oc->sections;
1048 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
1049 start, ((char*)end)-1, end - start + 1, kind );
1055 /* --------------------------------------------------------------------------
1056 * PEi386 specifics (Win32 targets)
1057 * ------------------------------------------------------------------------*/
1059 /* The information for this linker comes from
1060 Microsoft Portable Executable
1061 and Common Object File Format Specification
1062 revision 5.1 January 1998
1063 which SimonM says comes from the MS Developer Network CDs.
1065 It can be found there (on older CDs), but can also be found
1068 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
1070 (this is Rev 6.0 from February 1999).
1072 Things move, so if that fails, try searching for it via
1074 http://www.google.com/search?q=PE+COFF+specification
1076 The ultimate reference for the PE format is the Winnt.h
1077 header file that comes with the Platform SDKs; as always,
1078 implementations will drift wrt their documentation.
1080 A good background article on the PE format is Matt Pietrek's
1081 March 1994 article in Microsoft System Journal (MSJ)
1082 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1083 Win32 Portable Executable File Format." The info in there
1084 has recently been updated in a two part article in
1085 MSDN magazine, issues Feb and March 2002,
1086 "Inside Windows: An In-Depth Look into the Win32 Portable
1087 Executable File Format"
1089 John Levine's book "Linkers and Loaders" contains useful
1094 #if defined(OBJFORMAT_PEi386)
1098 typedef unsigned char UChar;
1099 typedef unsigned short UInt16;
1100 typedef unsigned int UInt32;
1107 UInt16 NumberOfSections;
1108 UInt32 TimeDateStamp;
1109 UInt32 PointerToSymbolTable;
1110 UInt32 NumberOfSymbols;
1111 UInt16 SizeOfOptionalHeader;
1112 UInt16 Characteristics;
1116 #define sizeof_COFF_header 20
1123 UInt32 VirtualAddress;
1124 UInt32 SizeOfRawData;
1125 UInt32 PointerToRawData;
1126 UInt32 PointerToRelocations;
1127 UInt32 PointerToLinenumbers;
1128 UInt16 NumberOfRelocations;
1129 UInt16 NumberOfLineNumbers;
1130 UInt32 Characteristics;
1134 #define sizeof_COFF_section 40
1141 UInt16 SectionNumber;
1144 UChar NumberOfAuxSymbols;
1148 #define sizeof_COFF_symbol 18
1153 UInt32 VirtualAddress;
1154 UInt32 SymbolTableIndex;
1159 #define sizeof_COFF_reloc 10
1162 /* From PE spec doc, section 3.3.2 */
1163 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1164 windows.h -- for the same purpose, but I want to know what I'm
1166 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1167 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1168 #define MYIMAGE_FILE_DLL 0x2000
1169 #define MYIMAGE_FILE_SYSTEM 0x1000
1170 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1171 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1172 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1174 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1175 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1176 #define MYIMAGE_SYM_CLASS_STATIC 3
1177 #define MYIMAGE_SYM_UNDEFINED 0
1179 /* From PE spec doc, section 4.1 */
1180 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1181 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1182 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1184 /* From PE spec doc, section 5.2.1 */
1185 #define MYIMAGE_REL_I386_DIR32 0x0006
1186 #define MYIMAGE_REL_I386_REL32 0x0014
1189 /* We use myindex to calculate array addresses, rather than
1190 simply doing the normal subscript thing. That's because
1191 some of the above structs have sizes which are not
1192 a whole number of words. GCC rounds their sizes up to a
1193 whole number of words, which means that the address calcs
1194 arising from using normal C indexing or pointer arithmetic
1195 are just plain wrong. Sigh.
1198 myindex ( int scale, void* base, int index )
1201 ((UChar*)base) + scale * index;
1206 printName ( UChar* name, UChar* strtab )
1208 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1209 UInt32 strtab_offset = * (UInt32*)(name+4);
1210 fprintf ( stderr, "%s", strtab + strtab_offset );
1213 for (i = 0; i < 8; i++) {
1214 if (name[i] == 0) break;
1215 fprintf ( stderr, "%c", name[i] );
1222 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1224 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1225 UInt32 strtab_offset = * (UInt32*)(name+4);
1226 strncpy ( dst, strtab+strtab_offset, dstSize );
1232 if (name[i] == 0) break;
1242 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1245 /* If the string is longer than 8 bytes, look in the
1246 string table for it -- this will be correctly zero terminated.
1248 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1249 UInt32 strtab_offset = * (UInt32*)(name+4);
1250 return ((UChar*)strtab) + strtab_offset;
1252 /* Otherwise, if shorter than 8 bytes, return the original,
1253 which by defn is correctly terminated.
1255 if (name[7]==0) return name;
1256 /* The annoying case: 8 bytes. Copy into a temporary
1257 (which is never freed ...)
1259 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1261 strncpy(newstr,name,8);
1267 /* Just compares the short names (first 8 chars) */
1268 static COFF_section *
1269 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1273 = (COFF_header*)(oc->image);
1274 COFF_section* sectab
1276 ((UChar*)(oc->image))
1277 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1279 for (i = 0; i < hdr->NumberOfSections; i++) {
1282 COFF_section* section_i
1284 myindex ( sizeof_COFF_section, sectab, i );
1285 n1 = (UChar*) &(section_i->Name);
1287 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1288 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1289 n1[6]==n2[6] && n1[7]==n2[7])
1298 zapTrailingAtSign ( UChar* sym )
1300 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1302 if (sym[0] == 0) return;
1304 while (sym[i] != 0) i++;
1307 while (j > 0 && my_isdigit(sym[j])) j--;
1308 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1314 ocVerifyImage_PEi386 ( ObjectCode* oc )
1319 COFF_section* sectab;
1320 COFF_symbol* symtab;
1322 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1323 hdr = (COFF_header*)(oc->image);
1324 sectab = (COFF_section*) (
1325 ((UChar*)(oc->image))
1326 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1328 symtab = (COFF_symbol*) (
1329 ((UChar*)(oc->image))
1330 + hdr->PointerToSymbolTable
1332 strtab = ((UChar*)symtab)
1333 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1335 if (hdr->Machine != 0x14c) {
1336 belch("Not x86 PEi386");
1339 if (hdr->SizeOfOptionalHeader != 0) {
1340 belch("PEi386 with nonempty optional header");
1343 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1344 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1345 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1346 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1347 belch("Not a PEi386 object file");
1350 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1351 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1352 belch("Invalid PEi386 word size or endiannness: %d",
1353 (int)(hdr->Characteristics));
1356 /* If the string table size is way crazy, this might indicate that
1357 there are more than 64k relocations, despite claims to the
1358 contrary. Hence this test. */
1359 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1361 if ( (*(UInt32*)strtab) > 600000 ) {
1362 /* Note that 600k has no special significance other than being
1363 big enough to handle the almost-2MB-sized lumps that
1364 constitute HSwin32*.o. */
1365 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1370 /* No further verification after this point; only debug printing. */
1372 IF_DEBUG(linker, i=1);
1373 if (i == 0) return 1;
1376 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1378 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1380 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1382 fprintf ( stderr, "\n" );
1384 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1386 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1388 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1390 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1392 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1394 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1396 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1398 /* Print the section table. */
1399 fprintf ( stderr, "\n" );
1400 for (i = 0; i < hdr->NumberOfSections; i++) {
1402 COFF_section* sectab_i
1404 myindex ( sizeof_COFF_section, sectab, i );
1411 printName ( sectab_i->Name, strtab );
1421 sectab_i->VirtualSize,
1422 sectab_i->VirtualAddress,
1423 sectab_i->SizeOfRawData,
1424 sectab_i->PointerToRawData,
1425 sectab_i->NumberOfRelocations,
1426 sectab_i->PointerToRelocations,
1427 sectab_i->PointerToRawData
1429 reltab = (COFF_reloc*) (
1430 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1433 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1434 /* If the relocation field (a short) has overflowed, the
1435 * real count can be found in the first reloc entry.
1437 * See Section 4.1 (last para) of the PE spec (rev6.0).
1439 COFF_reloc* rel = (COFF_reloc*)
1440 myindex ( sizeof_COFF_reloc, reltab, 0 );
1441 noRelocs = rel->VirtualAddress;
1444 noRelocs = sectab_i->NumberOfRelocations;
1448 for (; j < noRelocs; j++) {
1450 COFF_reloc* rel = (COFF_reloc*)
1451 myindex ( sizeof_COFF_reloc, reltab, j );
1453 " type 0x%-4x vaddr 0x%-8x name `",
1455 rel->VirtualAddress );
1456 sym = (COFF_symbol*)
1457 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1458 /* Hmm..mysterious looking offset - what's it for? SOF */
1459 printName ( sym->Name, strtab -10 );
1460 fprintf ( stderr, "'\n" );
1463 fprintf ( stderr, "\n" );
1465 fprintf ( stderr, "\n" );
1466 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1467 fprintf ( stderr, "---START of string table---\n");
1468 for (i = 4; i < *(Int32*)strtab; i++) {
1470 fprintf ( stderr, "\n"); else
1471 fprintf( stderr, "%c", strtab[i] );
1473 fprintf ( stderr, "--- END of string table---\n");
1475 fprintf ( stderr, "\n" );
1478 COFF_symbol* symtab_i;
1479 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1480 symtab_i = (COFF_symbol*)
1481 myindex ( sizeof_COFF_symbol, symtab, i );
1487 printName ( symtab_i->Name, strtab );
1496 (Int32)(symtab_i->SectionNumber),
1497 (UInt32)symtab_i->Type,
1498 (UInt32)symtab_i->StorageClass,
1499 (UInt32)symtab_i->NumberOfAuxSymbols
1501 i += symtab_i->NumberOfAuxSymbols;
1505 fprintf ( stderr, "\n" );
1511 ocGetNames_PEi386 ( ObjectCode* oc )
1514 COFF_section* sectab;
1515 COFF_symbol* symtab;
1522 hdr = (COFF_header*)(oc->image);
1523 sectab = (COFF_section*) (
1524 ((UChar*)(oc->image))
1525 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1527 symtab = (COFF_symbol*) (
1528 ((UChar*)(oc->image))
1529 + hdr->PointerToSymbolTable
1531 strtab = ((UChar*)(oc->image))
1532 + hdr->PointerToSymbolTable
1533 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1535 /* Allocate space for any (local, anonymous) .bss sections. */
1537 for (i = 0; i < hdr->NumberOfSections; i++) {
1539 COFF_section* sectab_i
1541 myindex ( sizeof_COFF_section, sectab, i );
1542 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1543 if (sectab_i->VirtualSize == 0) continue;
1544 /* This is a non-empty .bss section. Allocate zeroed space for
1545 it, and set its PointerToRawData field such that oc->image +
1546 PointerToRawData == addr_of_zeroed_space. */
1547 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1548 "ocGetNames_PEi386(anonymous bss)");
1549 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1550 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1551 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1554 /* Copy section information into the ObjectCode. */
1556 for (i = 0; i < hdr->NumberOfSections; i++) {
1562 = SECTIONKIND_OTHER;
1563 COFF_section* sectab_i
1565 myindex ( sizeof_COFF_section, sectab, i );
1566 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1569 /* I'm sure this is the Right Way to do it. However, the
1570 alternative of testing the sectab_i->Name field seems to
1571 work ok with Cygwin.
1573 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1574 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1575 kind = SECTIONKIND_CODE_OR_RODATA;
1578 if (0==strcmp(".text",sectab_i->Name) ||
1579 0==strcmp(".rodata",sectab_i->Name))
1580 kind = SECTIONKIND_CODE_OR_RODATA;
1581 if (0==strcmp(".data",sectab_i->Name) ||
1582 0==strcmp(".bss",sectab_i->Name))
1583 kind = SECTIONKIND_RWDATA;
1585 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1586 sz = sectab_i->SizeOfRawData;
1587 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1589 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1590 end = start + sz - 1;
1592 if (kind == SECTIONKIND_OTHER
1593 /* Ignore sections called which contain stabs debugging
1595 && 0 != strcmp(".stab", sectab_i->Name)
1596 && 0 != strcmp(".stabstr", sectab_i->Name)
1598 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1602 if (kind != SECTIONKIND_OTHER && end >= start) {
1603 addSection(oc, kind, start, end);
1604 addProddableBlock(oc, start, end - start + 1);
1608 /* Copy exported symbols into the ObjectCode. */
1610 oc->n_symbols = hdr->NumberOfSymbols;
1611 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1612 "ocGetNames_PEi386(oc->symbols)");
1613 /* Call me paranoid; I don't care. */
1614 for (i = 0; i < oc->n_symbols; i++)
1615 oc->symbols[i] = NULL;
1619 COFF_symbol* symtab_i;
1620 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1621 symtab_i = (COFF_symbol*)
1622 myindex ( sizeof_COFF_symbol, symtab, i );
1626 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1627 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1628 /* This symbol is global and defined, viz, exported */
1629 /* for MYIMAGE_SYMCLASS_EXTERNAL
1630 && !MYIMAGE_SYM_UNDEFINED,
1631 the address of the symbol is:
1632 address of relevant section + offset in section
1634 COFF_section* sectabent
1635 = (COFF_section*) myindex ( sizeof_COFF_section,
1637 symtab_i->SectionNumber-1 );
1638 addr = ((UChar*)(oc->image))
1639 + (sectabent->PointerToRawData
1643 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1644 && symtab_i->Value > 0) {
1645 /* This symbol isn't in any section at all, ie, global bss.
1646 Allocate zeroed space for it. */
1647 addr = stgCallocBytes(1, symtab_i->Value,
1648 "ocGetNames_PEi386(non-anonymous bss)");
1649 addSection(oc, SECTIONKIND_RWDATA, addr,
1650 ((UChar*)addr) + symtab_i->Value - 1);
1651 addProddableBlock(oc, addr, symtab_i->Value);
1652 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1655 if (addr != NULL ) {
1656 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1657 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1658 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1659 ASSERT(i >= 0 && i < oc->n_symbols);
1660 /* cstring_from_COFF_symbol_name always succeeds. */
1661 oc->symbols[i] = sname;
1662 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1666 "IGNORING symbol %d\n"
1670 printName ( symtab_i->Name, strtab );
1679 (Int32)(symtab_i->SectionNumber),
1680 (UInt32)symtab_i->Type,
1681 (UInt32)symtab_i->StorageClass,
1682 (UInt32)symtab_i->NumberOfAuxSymbols
1687 i += symtab_i->NumberOfAuxSymbols;
1696 ocResolve_PEi386 ( ObjectCode* oc )
1699 COFF_section* sectab;
1700 COFF_symbol* symtab;
1710 /* ToDo: should be variable-sized? But is at least safe in the
1711 sense of buffer-overrun-proof. */
1713 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1715 hdr = (COFF_header*)(oc->image);
1716 sectab = (COFF_section*) (
1717 ((UChar*)(oc->image))
1718 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1720 symtab = (COFF_symbol*) (
1721 ((UChar*)(oc->image))
1722 + hdr->PointerToSymbolTable
1724 strtab = ((UChar*)(oc->image))
1725 + hdr->PointerToSymbolTable
1726 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1728 for (i = 0; i < hdr->NumberOfSections; i++) {
1729 COFF_section* sectab_i
1731 myindex ( sizeof_COFF_section, sectab, i );
1734 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1737 /* Ignore sections called which contain stabs debugging
1739 if (0 == strcmp(".stab", sectab_i->Name)
1740 || 0 == strcmp(".stabstr", sectab_i->Name))
1743 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1744 /* If the relocation field (a short) has overflowed, the
1745 * real count can be found in the first reloc entry.
1747 * See Section 4.1 (last para) of the PE spec (rev6.0).
1749 COFF_reloc* rel = (COFF_reloc*)
1750 myindex ( sizeof_COFF_reloc, reltab, 0 );
1751 noRelocs = rel->VirtualAddress;
1752 fprintf(stderr, "Overflown relocs: %u\n", noRelocs);
1755 noRelocs = sectab_i->NumberOfRelocations;
1760 for (; j < noRelocs; j++) {
1762 COFF_reloc* reltab_j
1764 myindex ( sizeof_COFF_reloc, reltab, j );
1766 /* the location to patch */
1768 ((UChar*)(oc->image))
1769 + (sectab_i->PointerToRawData
1770 + reltab_j->VirtualAddress
1771 - sectab_i->VirtualAddress )
1773 /* the existing contents of pP */
1775 /* the symbol to connect to */
1776 sym = (COFF_symbol*)
1777 myindex ( sizeof_COFF_symbol,
1778 symtab, reltab_j->SymbolTableIndex );
1781 "reloc sec %2d num %3d: type 0x%-4x "
1782 "vaddr 0x%-8x name `",
1784 (UInt32)reltab_j->Type,
1785 reltab_j->VirtualAddress );
1786 printName ( sym->Name, strtab );
1787 fprintf ( stderr, "'\n" ));
1789 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1790 COFF_section* section_sym
1791 = findPEi386SectionCalled ( oc, sym->Name );
1793 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1796 S = ((UInt32)(oc->image))
1797 + (section_sym->PointerToRawData
1800 copyName ( sym->Name, strtab, symbol, 1000-1 );
1801 (void*)S = lookupLocalSymbol( oc, symbol );
1802 if ((void*)S != NULL) goto foundit;
1803 (void*)S = lookupSymbol( symbol );
1804 if ((void*)S != NULL) goto foundit;
1805 zapTrailingAtSign ( symbol );
1806 (void*)S = lookupLocalSymbol( oc, symbol );
1807 if ((void*)S != NULL) goto foundit;
1808 (void*)S = lookupSymbol( symbol );
1809 if ((void*)S != NULL) goto foundit;
1810 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1814 checkProddableBlock(oc, pP);
1815 switch (reltab_j->Type) {
1816 case MYIMAGE_REL_I386_DIR32:
1819 case MYIMAGE_REL_I386_REL32:
1820 /* Tricky. We have to insert a displacement at
1821 pP which, when added to the PC for the _next_
1822 insn, gives the address of the target (S).
1823 Problem is to know the address of the next insn
1824 when we only know pP. We assume that this
1825 literal field is always the last in the insn,
1826 so that the address of the next insn is pP+4
1827 -- hence the constant 4.
1828 Also I don't know if A should be added, but so
1829 far it has always been zero.
1832 *pP = S - ((UInt32)pP) - 4;
1835 belch("%s: unhandled PEi386 relocation type %d",
1836 oc->fileName, reltab_j->Type);
1843 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1847 #endif /* defined(OBJFORMAT_PEi386) */
1850 /* --------------------------------------------------------------------------
1852 * ------------------------------------------------------------------------*/
1854 #if defined(OBJFORMAT_ELF)
1859 #if defined(sparc_TARGET_ARCH)
1860 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1861 #elif defined(i386_TARGET_ARCH)
1862 # define ELF_TARGET_386 /* Used inside <elf.h> */
1863 #elif defined (ia64_TARGET_ARCH)
1864 # define ELF_TARGET_IA64 /* Used inside <elf.h> */
1866 # define ELF_FUNCTION_DESC /* calling convention uses function descriptors */
1867 # define ELF_NEED_GOT /* needs Global Offset Table */
1868 # define ELF_NEED_PLT /* needs Procedure Linkage Tables */
1874 * Define a set of types which can be used for both ELF32 and ELF64
1878 #define ELFCLASS ELFCLASS64
1879 #define Elf_Addr Elf64_Addr
1880 #define Elf_Word Elf64_Word
1881 #define Elf_Sword Elf64_Sword
1882 #define Elf_Ehdr Elf64_Ehdr
1883 #define Elf_Phdr Elf64_Phdr
1884 #define Elf_Shdr Elf64_Shdr
1885 #define Elf_Sym Elf64_Sym
1886 #define Elf_Rel Elf64_Rel
1887 #define Elf_Rela Elf64_Rela
1888 #define ELF_ST_TYPE ELF64_ST_TYPE
1889 #define ELF_ST_BIND ELF64_ST_BIND
1890 #define ELF_R_TYPE ELF64_R_TYPE
1891 #define ELF_R_SYM ELF64_R_SYM
1893 #define ELFCLASS ELFCLASS32
1894 #define Elf_Addr Elf32_Addr
1895 #define Elf_Word Elf32_Word
1896 #define Elf_Sword Elf32_Sword
1897 #define Elf_Ehdr Elf32_Ehdr
1898 #define Elf_Phdr Elf32_Phdr
1899 #define Elf_Shdr Elf32_Shdr
1900 #define Elf_Sym Elf32_Sym
1901 #define Elf_Rel Elf32_Rel
1902 #define Elf_Rela Elf32_Rela
1903 #define ELF_ST_TYPE ELF32_ST_TYPE
1904 #define ELF_ST_BIND ELF32_ST_BIND
1905 #define ELF_R_TYPE ELF32_R_TYPE
1906 #define ELF_R_SYM ELF32_R_SYM
1911 * Functions to allocate entries in dynamic sections. Currently we simply
1912 * preallocate a large number, and we don't check if a entry for the given
1913 * target already exists (a linear search is too slow). Ideally these
1914 * entries would be associated with symbols.
1917 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
1918 #define GOT_SIZE 0x20000
1919 #define FUNCTION_TABLE_SIZE 0x10000
1920 #define PLT_SIZE 0x08000
1923 static Elf_Addr got[GOT_SIZE];
1924 static unsigned int gotIndex;
1925 static Elf_Addr gp_val = (Elf_Addr)got;
1928 allocateGOTEntry(Elf_Addr target)
1932 if (gotIndex >= GOT_SIZE)
1933 barf("Global offset table overflow");
1935 entry = &got[gotIndex++];
1937 return (Elf_Addr)entry;
1941 #ifdef ELF_FUNCTION_DESC
1947 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
1948 static unsigned int functionTableIndex;
1951 allocateFunctionDesc(Elf_Addr target)
1953 FunctionDesc *entry;
1955 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
1956 barf("Function table overflow");
1958 entry = &functionTable[functionTableIndex++];
1960 entry->gp = (Elf_Addr)gp_val;
1961 return (Elf_Addr)entry;
1965 copyFunctionDesc(Elf_Addr target)
1967 FunctionDesc *olddesc = (FunctionDesc *)target;
1968 FunctionDesc *newdesc;
1970 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
1971 newdesc->gp = olddesc->gp;
1972 return (Elf_Addr)newdesc;
1977 #ifdef ia64_TARGET_ARCH
1978 static void ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value);
1979 static void ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc);
1981 static unsigned char plt_code[] =
1983 /* taken from binutils bfd/elfxx-ia64.c */
1984 0x0b, 0x78, 0x00, 0x02, 0x00, 0x24, /* [MMI] addl r15=0,r1;; */
1985 0x00, 0x41, 0x3c, 0x30, 0x28, 0xc0, /* ld8 r16=[r15],8 */
1986 0x01, 0x08, 0x00, 0x84, /* mov r14=r1;; */
1987 0x11, 0x08, 0x00, 0x1e, 0x18, 0x10, /* [MIB] ld8 r1=[r15] */
1988 0x60, 0x80, 0x04, 0x80, 0x03, 0x00, /* mov b6=r16 */
1989 0x60, 0x00, 0x80, 0x00 /* br.few b6;; */
1992 /* If we can't get to the function descriptor via gp, take a local copy of it */
1993 #define PLT_RELOC(code, target) { \
1994 Elf64_Sxword rel_value = target - gp_val; \
1995 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff)) \
1996 ia64_reloc_gprel22((Elf_Addr)code, copyFunctionDesc(target)); \
1998 ia64_reloc_gprel22((Elf_Addr)code, target); \
2003 unsigned char code[sizeof(plt_code)];
2007 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
2009 PLTEntry *plt = (PLTEntry *)oc->plt;
2012 if (oc->pltIndex >= PLT_SIZE)
2013 barf("Procedure table overflow");
2015 entry = &plt[oc->pltIndex++];
2016 memcpy(entry->code, plt_code, sizeof(entry->code));
2017 PLT_RELOC(entry->code, target);
2018 return (Elf_Addr)entry;
2024 return (PLT_SIZE * sizeof(PLTEntry));
2030 * Generic ELF functions
2034 findElfSection ( void* objImage, Elf_Word sh_type )
2036 char* ehdrC = (char*)objImage;
2037 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2038 Elf_Shdr* shdr = (Elf_Shdr*)(ehdrC + ehdr->e_shoff);
2039 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2043 for (i = 0; i < ehdr->e_shnum; i++) {
2044 if (shdr[i].sh_type == sh_type
2045 /* Ignore the section header's string table. */
2046 && i != ehdr->e_shstrndx
2047 /* Ignore string tables named .stabstr, as they contain
2049 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2051 ptr = ehdrC + shdr[i].sh_offset;
2059 findElfSegment ( void* objImage, Elf_Addr vaddr )
2061 char* ehdrC = (char*)objImage;
2062 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2063 Elf_Phdr* phdr = (Elf_Phdr*)(ehdrC + ehdr->e_phoff);
2064 Elf_Addr segaddr = 0;
2067 for (i = 0; i < ehdr->e_phnum; i++) {
2068 segaddr = phdr[i].p_vaddr;
2069 if ((vaddr >= segaddr) && (vaddr < segaddr + phdr[i].p_memsz))
2076 ocVerifyImage_ELF ( ObjectCode* oc )
2080 int i, j, nent, nstrtab, nsymtabs;
2084 char* ehdrC = (char*)(oc->image);
2085 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2087 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
2088 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
2089 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
2090 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
2091 belch("%s: not an ELF object", oc->fileName);
2095 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
2096 belch("%s: unsupported ELF format", oc->fileName);
2100 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
2101 IF_DEBUG(linker,belch( "Is little-endian" ));
2103 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
2104 IF_DEBUG(linker,belch( "Is big-endian" ));
2106 belch("%s: unknown endiannness", oc->fileName);
2110 if (ehdr->e_type != ET_REL) {
2111 belch("%s: not a relocatable object (.o) file", oc->fileName);
2114 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
2116 IF_DEBUG(linker,belch( "Architecture is " ));
2117 switch (ehdr->e_machine) {
2118 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
2119 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
2120 case EM_IA_64: IF_DEBUG(linker,belch( "ia64" )); break;
2121 default: IF_DEBUG(linker,belch( "unknown" ));
2122 belch("%s: unknown architecture", oc->fileName);
2126 IF_DEBUG(linker,belch(
2127 "\nSection header table: start %d, n_entries %d, ent_size %d",
2128 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
2130 ASSERT (ehdr->e_shentsize == sizeof(Elf_Shdr));
2132 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2134 if (ehdr->e_shstrndx == SHN_UNDEF) {
2135 belch("%s: no section header string table", oc->fileName);
2138 IF_DEBUG(linker,belch( "Section header string table is section %d",
2140 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2143 for (i = 0; i < ehdr->e_shnum; i++) {
2144 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
2145 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
2146 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
2147 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
2148 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
2149 ehdrC + shdr[i].sh_offset,
2150 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
2152 if (shdr[i].sh_type == SHT_REL) {
2153 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
2154 } else if (shdr[i].sh_type == SHT_RELA) {
2155 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
2157 IF_DEBUG(linker,fprintf(stderr," "));
2160 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
2164 IF_DEBUG(linker,belch( "\nString tables" ));
2167 for (i = 0; i < ehdr->e_shnum; i++) {
2168 if (shdr[i].sh_type == SHT_STRTAB
2169 /* Ignore the section header's string table. */
2170 && i != ehdr->e_shstrndx
2171 /* Ignore string tables named .stabstr, as they contain
2173 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
2175 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
2176 strtab = ehdrC + shdr[i].sh_offset;
2181 belch("%s: no string tables, or too many", oc->fileName);
2186 IF_DEBUG(linker,belch( "\nSymbol tables" ));
2187 for (i = 0; i < ehdr->e_shnum; i++) {
2188 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2189 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
2191 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2192 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2193 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
2195 shdr[i].sh_size % sizeof(Elf_Sym)
2197 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
2198 belch("%s: non-integral number of symbol table entries", oc->fileName);
2201 for (j = 0; j < nent; j++) {
2202 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
2203 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
2204 (int)stab[j].st_shndx,
2205 (int)stab[j].st_size,
2206 (char*)stab[j].st_value ));
2208 IF_DEBUG(linker,fprintf(stderr, "type=" ));
2209 switch (ELF_ST_TYPE(stab[j].st_info)) {
2210 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
2211 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
2212 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
2213 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
2214 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
2215 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2217 IF_DEBUG(linker,fprintf(stderr, " " ));
2219 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
2220 switch (ELF_ST_BIND(stab[j].st_info)) {
2221 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
2222 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
2223 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
2224 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
2226 IF_DEBUG(linker,fprintf(stderr, " " ));
2228 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
2232 if (nsymtabs == 0) {
2233 belch("%s: didn't find any symbol tables", oc->fileName);
2242 ocGetNames_ELF ( ObjectCode* oc )
2247 char* ehdrC = (char*)(oc->image);
2248 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
2249 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
2250 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2252 ASSERT(symhash != NULL);
2255 belch("%s: no strtab", oc->fileName);
2260 for (i = 0; i < ehdr->e_shnum; i++) {
2261 /* Figure out what kind of section it is. Logic derived from
2262 Figure 1.14 ("Special Sections") of the ELF document
2263 ("Portable Formats Specification, Version 1.1"). */
2264 Elf_Shdr hdr = shdr[i];
2265 SectionKind kind = SECTIONKIND_OTHER;
2268 if (hdr.sh_type == SHT_PROGBITS
2269 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2270 /* .text-style section */
2271 kind = SECTIONKIND_CODE_OR_RODATA;
2274 if (hdr.sh_type == SHT_PROGBITS
2275 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2276 /* .data-style section */
2277 kind = SECTIONKIND_RWDATA;
2280 if (hdr.sh_type == SHT_PROGBITS
2281 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2282 /* .rodata-style section */
2283 kind = SECTIONKIND_CODE_OR_RODATA;
2286 if (hdr.sh_type == SHT_NOBITS
2287 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2288 /* .bss-style section */
2289 kind = SECTIONKIND_RWDATA;
2293 if (is_bss && shdr[i].sh_size > 0) {
2294 /* This is a non-empty .bss section. Allocate zeroed space for
2295 it, and set its .sh_offset field such that
2296 ehdrC + .sh_offset == addr_of_zeroed_space. */
2297 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2298 "ocGetNames_ELF(BSS)");
2299 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2301 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2302 zspace, shdr[i].sh_size);
2306 /* fill in the section info */
2307 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2308 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2309 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2310 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2313 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2315 /* copy stuff into this module's object symbol table */
2316 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
2317 nent = shdr[i].sh_size / sizeof(Elf_Sym);
2319 oc->n_symbols = nent;
2320 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2321 "ocGetNames_ELF(oc->symbols)");
2323 for (j = 0; j < nent; j++) {
2325 char isLocal = FALSE; /* avoids uninit-var warning */
2327 char* nm = strtab + stab[j].st_name;
2328 int secno = stab[j].st_shndx;
2330 /* Figure out if we want to add it; if so, set ad to its
2331 address. Otherwise leave ad == NULL. */
2333 if (secno == SHN_COMMON) {
2335 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2337 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2338 stab[j].st_size, nm);
2340 /* Pointless to do addProddableBlock() for this area,
2341 since the linker should never poke around in it. */
2344 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
2345 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
2347 /* and not an undefined symbol */
2348 && stab[j].st_shndx != SHN_UNDEF
2349 /* and not in a "special section" */
2350 && stab[j].st_shndx < SHN_LORESERVE
2352 /* and it's a not a section or string table or anything silly */
2353 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2354 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2355 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2358 /* Section 0 is the undefined section, hence > and not >=. */
2359 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2361 if (shdr[secno].sh_type == SHT_NOBITS) {
2362 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2363 stab[j].st_size, stab[j].st_value, nm);
2366 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2367 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2370 #ifdef ELF_FUNCTION_DESC
2371 /* dlsym() and the initialisation table both give us function
2372 * descriptors, so to be consistent we store function descriptors
2373 * in the symbol table */
2374 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
2375 ad = (char *)allocateFunctionDesc((Elf_Addr)ad);
2377 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2378 ad, oc->fileName, nm ));
2383 /* And the decision is ... */
2387 oc->symbols[j] = nm;
2390 /* Ignore entirely. */
2392 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2396 IF_DEBUG(linker,belch( "skipping `%s'",
2397 strtab + stab[j].st_name ));
2400 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2401 (int)ELF_ST_BIND(stab[j].st_info),
2402 (int)ELF_ST_TYPE(stab[j].st_info),
2403 (int)stab[j].st_shndx,
2404 strtab + stab[j].st_name
2407 oc->symbols[j] = NULL;
2416 /* Do ELF relocations which lack an explicit addend. All x86-linux
2417 relocations appear to be of this form. */
2419 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2420 Elf_Shdr* shdr, int shnum,
2421 Elf_Sym* stab, char* strtab )
2426 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
2427 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
2428 int target_shndx = shdr[shnum].sh_info;
2429 int symtab_shndx = shdr[shnum].sh_link;
2431 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2432 targ = (Elf_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2433 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2434 target_shndx, symtab_shndx ));
2436 for (j = 0; j < nent; j++) {
2437 Elf_Addr offset = rtab[j].r_offset;
2438 Elf_Word info = rtab[j].r_info;
2440 Elf_Addr P = ((Elf_Addr)targ) + offset;
2441 Elf_Word* pP = (Elf_Word*)P;
2446 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2447 j, (void*)offset, (void*)info ));
2449 IF_DEBUG(linker,belch( " ZERO" ));
2452 Elf_Sym sym = stab[ELF_R_SYM(info)];
2453 /* First see if it is a local symbol. */
2454 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2455 /* Yes, so we can get the address directly from the ELF symbol
2457 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2459 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2460 + stab[ELF_R_SYM(info)].st_value);
2463 /* No, so look up the name in our global table. */
2464 symbol = strtab + sym.st_name;
2465 (void*)S = lookupSymbol( symbol );
2468 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2471 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2474 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2475 (void*)P, (void*)S, (void*)A ));
2476 checkProddableBlock ( oc, pP );
2480 switch (ELF_R_TYPE(info)) {
2481 # ifdef i386_TARGET_ARCH
2482 case R_386_32: *pP = value; break;
2483 case R_386_PC32: *pP = value - P; break;
2486 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2487 oc->fileName, ELF_R_TYPE(info));
2495 /* Do ELF relocations for which explicit addends are supplied.
2496 sparc-solaris relocations appear to be of this form. */
2498 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2499 Elf_Shdr* shdr, int shnum,
2500 Elf_Sym* stab, char* strtab )
2505 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
2506 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
2507 int target_shndx = shdr[shnum].sh_info;
2508 int symtab_shndx = shdr[shnum].sh_link;
2510 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2511 targ = (Elf_Addr*)(ehdrC + shdr[ target_shndx ].sh_offset);
2512 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2513 target_shndx, symtab_shndx ));
2515 for (j = 0; j < nent; j++) {
2516 Elf_Addr offset = rtab[j].r_offset;
2517 Elf_Addr info = rtab[j].r_info;
2518 Elf_Addr A = rtab[j].r_addend;
2519 Elf_Addr P = (Elf_Addr)targ + offset;
2522 # if defined(sparc_TARGET_ARCH)
2523 /* This #ifdef only serves to avoid unused-var warnings. */
2524 Elf_Word* pP = (Elf_Word*)P;
2526 # elif defined(ia64_TARGET_ARCH)
2527 Elf64_Xword *pP = (Elf64_Xword *)P;
2531 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2532 j, (void*)offset, (void*)info,
2535 IF_DEBUG(linker,belch( " ZERO" ));
2538 Elf_Sym sym = stab[ELF_R_SYM(info)];
2539 /* First see if it is a local symbol. */
2540 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
2541 /* Yes, so we can get the address directly from the ELF symbol
2543 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2545 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2546 + stab[ELF_R_SYM(info)].st_value);
2547 #ifdef ELF_FUNCTION_DESC
2548 /* Make a function descriptor for this function */
2549 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
2550 S = allocateFunctionDesc(S + A);
2555 /* No, so look up the name in our global table. */
2556 symbol = strtab + sym.st_name;
2557 (void*)S = lookupSymbol( symbol );
2559 #ifdef ELF_FUNCTION_DESC
2560 /* If a function, already a function descriptor - we would
2561 have to copy it to add an offset. */
2562 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC)
2567 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2570 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2573 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2574 (void*)P, (void*)S, (void*)A ));
2575 /* checkProddableBlock ( oc, (void*)P ); */
2579 switch (ELF_R_TYPE(info)) {
2580 # if defined(sparc_TARGET_ARCH)
2581 case R_SPARC_WDISP30:
2582 w1 = *pP & 0xC0000000;
2583 w2 = (Elf_Word)((value - P) >> 2);
2584 ASSERT((w2 & 0xC0000000) == 0);
2589 w1 = *pP & 0xFFC00000;
2590 w2 = (Elf_Word)(value >> 10);
2591 ASSERT((w2 & 0xFFC00000) == 0);
2597 w2 = (Elf_Word)(value & 0x3FF);
2598 ASSERT((w2 & ~0x3FF) == 0);
2602 /* According to the Sun documentation:
2604 This relocation type resembles R_SPARC_32, except it refers to an
2605 unaligned word. That is, the word to be relocated must be treated
2606 as four separate bytes with arbitrary alignment, not as a word
2607 aligned according to the architecture requirements.
2609 (JRS: which means that freeloading on the R_SPARC_32 case
2610 is probably wrong, but hey ...)
2614 w2 = (Elf_Word)value;
2617 # elif defined(ia64_TARGET_ARCH)
2618 case R_IA64_DIR64LSB:
2619 case R_IA64_FPTR64LSB:
2622 case R_IA64_SEGREL64LSB:
2623 addr = findElfSegment(ehdrC, value);
2626 case R_IA64_GPREL22:
2627 ia64_reloc_gprel22(P, value);
2629 case R_IA64_LTOFF22:
2630 case R_IA64_LTOFF_FPTR22:
2631 addr = allocateGOTEntry(value);
2632 ia64_reloc_gprel22(P, addr);
2634 case R_IA64_PCREL21B:
2635 ia64_reloc_pcrel21(P, S, oc);
2639 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2640 oc->fileName, ELF_R_TYPE(info));
2649 ocResolve_ELF ( ObjectCode* oc )
2653 Elf_Sym* stab = NULL;
2654 char* ehdrC = (char*)(oc->image);
2655 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
2656 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
2657 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2659 /* first find "the" symbol table */
2660 stab = (Elf_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2662 /* also go find the string table */
2663 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2665 if (stab == NULL || strtab == NULL) {
2666 belch("%s: can't find string or symbol table", oc->fileName);
2670 /* Process the relocation sections. */
2671 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2673 /* Skip sections called ".rel.stab". These appear to contain
2674 relocation entries that, when done, make the stabs debugging
2675 info point at the right places. We ain't interested in all
2677 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2680 if (shdr[shnum].sh_type == SHT_REL ) {
2681 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr,
2682 shnum, stab, strtab );
2686 if (shdr[shnum].sh_type == SHT_RELA) {
2687 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr,
2688 shnum, stab, strtab );
2693 /* Free the local symbol table; we won't need it again. */
2694 freeHashTable(oc->lochash, NULL);
2703 * Instructions are 41 bits long, packed into 128 bit bundles with a 5-bit template
2704 * at the front. The following utility functions pack and unpack instructions, and
2705 * take care of the most common relocations.
2708 #ifdef ia64_TARGET_ARCH
2711 ia64_extract_instruction(Elf64_Xword *target)
2714 int slot = (Elf_Addr)target & 3;
2715 (Elf_Addr)target &= ~3;
2723 return ((w1 >> 5) & 0x1ffffffffff);
2725 return (w1 >> 46) | ((w2 & 0x7fffff) << 18);
2729 barf("ia64_extract_instruction: invalid slot %p", target);
2734 ia64_deposit_instruction(Elf64_Xword *target, Elf64_Xword value)
2736 int slot = (Elf_Addr)target & 3;
2737 (Elf_Addr)target &= ~3;
2742 *target |= value << 5;
2745 *target |= value << 46;
2746 *(target+1) |= value >> 18;
2749 *(target+1) |= value << 23;
2755 ia64_reloc_gprel22(Elf_Addr target, Elf_Addr value)
2757 Elf64_Xword instruction;
2758 Elf64_Sxword rel_value;
2760 rel_value = value - gp_val;
2761 if ((rel_value > 0x1fffff) || (rel_value < -0x1fffff))
2762 barf("GP-relative data out of range (address = 0x%lx, gp = 0x%lx)", value, gp_val);
2764 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2765 instruction |= (((rel_value >> 0) & 0x07f) << 13) /* imm7b */
2766 | (((rel_value >> 7) & 0x1ff) << 27) /* imm9d */
2767 | (((rel_value >> 16) & 0x01f) << 22) /* imm5c */
2768 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2769 ia64_deposit_instruction((Elf64_Xword *)target, instruction);
2773 ia64_reloc_pcrel21(Elf_Addr target, Elf_Addr value, ObjectCode *oc)
2775 Elf64_Xword instruction;
2776 Elf64_Sxword rel_value;
2779 entry = allocatePLTEntry(value, oc);
2781 rel_value = (entry >> 4) - (target >> 4);
2782 if ((rel_value > 0xfffff) || (rel_value < -0xfffff))
2783 barf("PLT entry too far away (entry = 0x%lx, target = 0x%lx)", entry, target);
2785 instruction = ia64_extract_instruction((Elf64_Xword *)target);
2786 instruction |= ((rel_value & 0xfffff) << 13) /* imm20b */
2787 | ((Elf64_Xword)(rel_value < 0) << 36); /* s */
2788 ia64_deposit_instruction((Elf64_Xword *)target, instruction);