1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.90 2002/06/04 19:21:28 sof 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(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
52 # define OBJFORMAT_ELF
53 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
54 # define OBJFORMAT_PEi386
58 /* Hash table mapping symbol names to Symbol */
59 /*Str*/HashTable *symhash;
61 #if defined(OBJFORMAT_ELF)
62 static int ocVerifyImage_ELF ( ObjectCode* oc );
63 static int ocGetNames_ELF ( ObjectCode* oc );
64 static int ocResolve_ELF ( ObjectCode* oc );
65 #elif defined(OBJFORMAT_PEi386)
66 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
67 static int ocGetNames_PEi386 ( ObjectCode* oc );
68 static int ocResolve_PEi386 ( ObjectCode* oc );
71 /* -----------------------------------------------------------------------------
72 * Built-in symbols from the RTS
75 typedef struct _RtsSymbolVal {
82 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
84 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
85 SymX(makeStableNamezh_fast) \
86 SymX(finalizzeWeakzh_fast)
88 /* These are not available in GUM!!! -- HWL */
89 #define Maybe_ForeignObj
90 #define Maybe_Stable_Names
93 #if !defined (mingw32_TARGET_OS)
94 #define RTS_POSIX_ONLY_SYMBOLS \
95 SymX(stg_sig_install) \
99 #if defined (cygwin32_TARGET_OS)
100 #define RTS_MINGW_ONLY_SYMBOLS /**/
101 /* Don't have the ability to read import libs / archives, so
102 * we have to stupidly list a lot of what libcygwin.a
105 #define RTS_CYGWIN_ONLY_SYMBOLS \
187 #elif !defined(mingw32_TARGET_OS)
188 #define RTS_MINGW_ONLY_SYMBOLS /**/
189 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
190 #else /* defined(mingw32_TARGET_OS) */
191 #define RTS_POSIX_ONLY_SYMBOLS /**/
192 #define RTS_CYGWIN_ONLY_SYMBOLS /**/
194 /* These are statically linked from the mingw libraries into the ghc
195 executable, so we have to employ this hack. */
196 #define RTS_MINGW_ONLY_SYMBOLS \
209 SymX(getservbyname) \
210 SymX(getservbyport) \
211 SymX(getprotobynumber) \
212 SymX(getprotobyname) \
213 SymX(gethostbyname) \
214 SymX(gethostbyaddr) \
249 Sym(_imp___timezone) \
265 # define MAIN_CAP_SYM SymX(MainCapability)
267 # define MAIN_CAP_SYM
270 #define RTS_SYMBOLS \
274 Sym(__stginit_GHCziPrim) \
278 Sym(stg_enterStackTop) \
281 SymX(__stg_gc_enter_1) \
283 SymX(stg_gc_noregs) \
285 SymX(stg_gc_unbx_r1) \
286 SymX(stg_gc_unpt_r1) \
287 SymX(stg_gc_ut_0_1) \
288 SymX(stg_gc_ut_1_0) \
290 SymX(stg_yield_to_interpreter) \
293 SymX(MallocFailHook) \
294 SymX(NoRunnableThreadsHook) \
296 SymX(OutOfHeapHook) \
297 SymX(PatErrorHdrHook) \
298 SymX(PostTraceHook) \
300 SymX(StackOverflowHook) \
301 SymX(__encodeDouble) \
302 SymX(__encodeFloat) \
305 SymX(__gmpz_cmp_si) \
306 SymX(__gmpz_cmp_ui) \
307 SymX(__gmpz_get_si) \
308 SymX(__gmpz_get_ui) \
309 SymX(__int_encodeDouble) \
310 SymX(__int_encodeFloat) \
311 SymX(andIntegerzh_fast) \
312 SymX(blockAsyncExceptionszh_fast) \
315 SymX(complementIntegerzh_fast) \
316 SymX(cmpIntegerzh_fast) \
317 SymX(cmpIntegerIntzh_fast) \
318 SymX(createAdjustor) \
319 SymX(decodeDoublezh_fast) \
320 SymX(decodeFloatzh_fast) \
323 SymX(deRefWeakzh_fast) \
324 SymX(deRefStablePtrzh_fast) \
325 SymX(divExactIntegerzh_fast) \
326 SymX(divModIntegerzh_fast) \
328 SymX(forkProcesszh_fast) \
329 SymX(freeHaskellFunctionPtr) \
330 SymX(freeStablePtr) \
331 SymX(gcdIntegerzh_fast) \
332 SymX(gcdIntegerIntzh_fast) \
333 SymX(gcdIntzh_fast) \
336 SymX(int2Integerzh_fast) \
337 SymX(integer2Intzh_fast) \
338 SymX(integer2Wordzh_fast) \
339 SymX(isDoubleDenormalized) \
340 SymX(isDoubleInfinite) \
342 SymX(isDoubleNegativeZero) \
343 SymX(isEmptyMVarzh_fast) \
344 SymX(isFloatDenormalized) \
345 SymX(isFloatInfinite) \
347 SymX(isFloatNegativeZero) \
348 SymX(killThreadzh_fast) \
349 SymX(makeStablePtrzh_fast) \
350 SymX(minusIntegerzh_fast) \
351 SymX(mkApUpd0zh_fast) \
352 SymX(myThreadIdzh_fast) \
353 SymX(labelThreadzh_fast) \
354 SymX(newArrayzh_fast) \
355 SymX(newBCOzh_fast) \
356 SymX(newByteArrayzh_fast) \
358 SymX(newMVarzh_fast) \
359 SymX(newMutVarzh_fast) \
360 SymX(newPinnedByteArrayzh_fast) \
361 SymX(orIntegerzh_fast) \
363 SymX(plusIntegerzh_fast) \
366 SymX(putMVarzh_fast) \
367 SymX(quotIntegerzh_fast) \
368 SymX(quotRemIntegerzh_fast) \
370 SymX(remIntegerzh_fast) \
371 SymX(resetNonBlockingFd) \
374 SymX(rts_checkSchedStatus) \
377 SymX(rts_evalLazyIO) \
382 SymX(rts_getDouble) \
387 SymX(rts_getStablePtr) \
388 SymX(rts_getThreadId) \
390 SymX(rts_getWord32) \
402 SymX(rts_mkStablePtr) \
411 SymX(shutdownHaskellAndExit) \
412 SymX(stable_ptr_table) \
413 SymX(stackOverflow) \
414 SymX(stg_CAF_BLACKHOLE_info) \
415 SymX(stg_CHARLIKE_closure) \
416 SymX(stg_EMPTY_MVAR_info) \
417 SymX(stg_IND_STATIC_info) \
418 SymX(stg_INTLIKE_closure) \
419 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
420 SymX(stg_WEAK_info) \
421 SymX(stg_ap_1_upd_info) \
422 SymX(stg_ap_2_upd_info) \
423 SymX(stg_ap_3_upd_info) \
424 SymX(stg_ap_4_upd_info) \
425 SymX(stg_ap_5_upd_info) \
426 SymX(stg_ap_6_upd_info) \
427 SymX(stg_ap_7_upd_info) \
428 SymX(stg_ap_8_upd_info) \
430 SymX(stg_sel_0_upd_info) \
431 SymX(stg_sel_10_upd_info) \
432 SymX(stg_sel_11_upd_info) \
433 SymX(stg_sel_12_upd_info) \
434 SymX(stg_sel_13_upd_info) \
435 SymX(stg_sel_14_upd_info) \
436 SymX(stg_sel_15_upd_info) \
437 SymX(stg_sel_1_upd_info) \
438 SymX(stg_sel_2_upd_info) \
439 SymX(stg_sel_3_upd_info) \
440 SymX(stg_sel_4_upd_info) \
441 SymX(stg_sel_5_upd_info) \
442 SymX(stg_sel_6_upd_info) \
443 SymX(stg_sel_7_upd_info) \
444 SymX(stg_sel_8_upd_info) \
445 SymX(stg_sel_9_upd_info) \
446 SymX(stg_seq_frame_info) \
447 SymX(stg_upd_frame_info) \
448 SymX(__stg_update_PAP) \
449 SymX(suspendThread) \
450 SymX(takeMVarzh_fast) \
451 SymX(timesIntegerzh_fast) \
452 SymX(tryPutMVarzh_fast) \
453 SymX(tryTakeMVarzh_fast) \
454 SymX(unblockAsyncExceptionszh_fast) \
455 SymX(unsafeThawArrayzh_fast) \
456 SymX(waitReadzh_fast) \
457 SymX(waitWritezh_fast) \
458 SymX(word2Integerzh_fast) \
459 SymX(xorIntegerzh_fast) \
462 #ifndef SUPPORT_LONG_LONGS
463 #define RTS_LONG_LONG_SYMS /* nothing */
465 #define RTS_LONG_LONG_SYMS \
466 SymX(int64ToIntegerzh_fast) \
467 SymX(word64ToIntegerzh_fast)
468 #endif /* SUPPORT_LONG_LONGS */
470 /* entirely bogus claims about types of these symbols */
471 #define Sym(vvv) extern void (vvv);
472 #define SymX(vvv) /**/
475 RTS_POSIX_ONLY_SYMBOLS
476 RTS_MINGW_ONLY_SYMBOLS
477 RTS_CYGWIN_ONLY_SYMBOLS
481 #ifdef LEADING_UNDERSCORE
482 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
484 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
487 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
489 #define SymX(vvv) Sym(vvv)
491 static RtsSymbolVal rtsSyms[] = {
494 RTS_POSIX_ONLY_SYMBOLS
495 RTS_MINGW_ONLY_SYMBOLS
496 RTS_CYGWIN_ONLY_SYMBOLS
497 { 0, 0 } /* sentinel */
500 /* -----------------------------------------------------------------------------
501 * Insert symbols into hash tables, checking for duplicates.
503 static void ghciInsertStrHashTable ( char* obj_name,
509 if (lookupHashTable(table, (StgWord)key) == NULL)
511 insertStrHashTable(table, (StgWord)key, data);
516 "GHCi runtime linker: fatal error: I found a duplicate definition for symbol\n"
518 "whilst processing object file\n"
520 "This could be caused by:\n"
521 " * Loading two different object files which export the same symbol\n"
522 " * Specifying the same object file twice on the GHCi command line\n"
523 " * An incorrect `package.conf' entry, causing some object to be\n"
525 "GHCi cannot safely continue in this situation. Exiting now. Sorry.\n"
534 /* -----------------------------------------------------------------------------
535 * initialize the object linker
537 #if defined(OBJFORMAT_ELF)
538 static void *dl_prog_handle;
546 symhash = allocStrHashTable();
548 /* populate the symbol table with stuff from the RTS */
549 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
550 ghciInsertStrHashTable("(GHCi built-in symbols)",
551 symhash, sym->lbl, sym->addr);
553 # if defined(OBJFORMAT_ELF)
554 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
558 /* -----------------------------------------------------------------------------
559 * Add a DLL from which symbols may be found. In the ELF case, just
560 * do RTLD_GLOBAL-style add, so no further messing around needs to
561 * happen in order that symbols in the loaded .so are findable --
562 * lookupSymbol() will subsequently see them by dlsym on the program's
563 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
565 * In the PEi386 case, open the DLLs and put handles to them in a
566 * linked list. When looking for a symbol, try all handles in the
570 #if defined(OBJFORMAT_PEi386)
571 /* A record for storing handles into DLLs. */
576 struct _OpenedDLL* next;
581 /* A list thereof. */
582 static OpenedDLL* opened_dlls = NULL;
588 addDLL( char *dll_name )
590 # if defined(OBJFORMAT_ELF)
594 hdl= dlopen(dll_name, RTLD_NOW | RTLD_GLOBAL);
596 /* dlopen failed; return a ptr to the error msg. */
598 if (errmsg == NULL) errmsg = "addDLL: unknown error";
605 # elif defined(OBJFORMAT_PEi386)
607 /* Add this DLL to the list of DLLs in which to search for symbols.
608 The path argument is ignored. */
613 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
615 /* See if we've already got it, and ignore if so. */
616 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
617 if (0 == strcmp(o_dll->name, dll_name))
621 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
622 sprintf(buf, "%s.DLL", dll_name);
623 instance = LoadLibrary(buf);
624 if (instance == NULL) {
625 sprintf(buf, "%s.DRV", dll_name); // KAA: allow loading of drivers (like winspool.drv)
626 instance = LoadLibrary(buf);
627 if (instance == NULL) {
630 /* LoadLibrary failed; return a ptr to the error msg. */
631 return "addDLL: unknown error";
636 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
637 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
638 strcpy(o_dll->name, dll_name);
639 o_dll->instance = instance;
640 o_dll->next = opened_dlls;
645 barf("addDLL: not implemented on this platform");
649 /* -----------------------------------------------------------------------------
650 * lookup a symbol in the hash table
653 lookupSymbol( char *lbl )
656 ASSERT(symhash != NULL);
657 val = lookupStrHashTable(symhash, lbl);
660 # if defined(OBJFORMAT_ELF)
661 return dlsym(dl_prog_handle, lbl);
662 # elif defined(OBJFORMAT_PEi386)
665 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
666 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
668 /* HACK: if the name has an initial underscore, try stripping
669 it off & look that up first. I've yet to verify whether there's
670 a Rule that governs whether an initial '_' *should always* be
671 stripped off when mapping from import lib name to the DLL name.
673 sym = GetProcAddress(o_dll->instance, (lbl+1));
675 /*fprintf(stderr, "found %s in %s\n", lbl+1,o_dll->name); fflush(stderr);*/
679 sym = GetProcAddress(o_dll->instance, lbl);
681 /*fprintf(stderr, "found %s in %s\n", lbl,o_dll->name); fflush(stderr);*/
696 __attribute((unused))
698 lookupLocalSymbol( ObjectCode* oc, char *lbl )
701 val = lookupStrHashTable(oc->lochash, lbl);
711 /* -----------------------------------------------------------------------------
712 * Debugging aid: look in GHCi's object symbol tables for symbols
713 * within DELTA bytes of the specified address, and show their names.
716 void ghci_enquire ( char* addr );
718 void ghci_enquire ( char* addr )
723 const int DELTA = 64;
725 for (oc = objects; oc; oc = oc->next) {
726 for (i = 0; i < oc->n_symbols; i++) {
727 sym = oc->symbols[i];
728 if (sym == NULL) continue;
729 /* fprintf(stderr, "enquire %p %p\n", sym, oc->lochash); */
731 if (oc->lochash != NULL)
732 a = lookupStrHashTable(oc->lochash, sym);
734 a = lookupStrHashTable(symhash, sym);
736 /* fprintf(stderr, "ghci_enquire: can't find %s\n", sym); */
738 else if (addr-DELTA <= a && a <= addr+DELTA) {
739 fprintf(stderr, "%p + %3d == `%s'\n", addr, a - addr, sym);
747 /* -----------------------------------------------------------------------------
748 * Load an obj (populate the global symbol table, but don't resolve yet)
750 * Returns: 1 if ok, 0 on error.
753 loadObj( char *path )
760 /* fprintf(stderr, "loadObj %s\n", path ); */
762 /* Check that we haven't already loaded this object. Don't give up
763 at this stage; ocGetNames_* will barf later. */
767 for (o = objects; o; o = o->next) {
768 if (0 == strcmp(o->fileName, path))
774 "GHCi runtime linker: warning: looks like you're trying to load the\n"
775 "same object file twice:\n"
777 "GHCi will continue, but a duplicate-symbol error may shortly follow.\n"
783 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
785 # if defined(OBJFORMAT_ELF)
786 oc->formatName = "ELF";
787 # elif defined(OBJFORMAT_PEi386)
788 oc->formatName = "PEi386";
791 barf("loadObj: not implemented on this platform");
795 if (r == -1) { return 0; }
797 /* sigh, strdup() isn't a POSIX function, so do it the long way */
798 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
799 strcpy(oc->fileName, path);
801 oc->fileSize = st.st_size;
802 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
805 oc->lochash = allocStrHashTable();
806 oc->proddables = NULL;
808 /* chain it onto the list of objects */
812 /* load the image into memory */
813 f = fopen(path, "rb");
815 barf("loadObj: can't read `%s'", path);
817 n = fread ( oc->image, 1, oc->fileSize, f );
818 if (n != oc->fileSize) {
820 barf("loadObj: error whilst reading `%s'", path);
823 /* verify the in-memory image */
824 # if defined(OBJFORMAT_ELF)
825 r = ocVerifyImage_ELF ( oc );
826 # elif defined(OBJFORMAT_PEi386)
827 r = ocVerifyImage_PEi386 ( oc );
829 barf("loadObj: no verify method");
831 if (!r) { return r; }
833 /* build the symbol list for this image */
834 # if defined(OBJFORMAT_ELF)
835 r = ocGetNames_ELF ( oc );
836 # elif defined(OBJFORMAT_PEi386)
837 r = ocGetNames_PEi386 ( oc );
839 barf("loadObj: no getNames method");
841 if (!r) { return r; }
843 /* loaded, but not resolved yet */
844 oc->status = OBJECT_LOADED;
849 /* -----------------------------------------------------------------------------
850 * resolve all the currently unlinked objects in memory
852 * Returns: 1 if ok, 0 on error.
860 for (oc = objects; oc; oc = oc->next) {
861 if (oc->status != OBJECT_RESOLVED) {
862 # if defined(OBJFORMAT_ELF)
863 r = ocResolve_ELF ( oc );
864 # elif defined(OBJFORMAT_PEi386)
865 r = ocResolve_PEi386 ( oc );
867 barf("resolveObjs: not implemented on this platform");
869 if (!r) { return r; }
870 oc->status = OBJECT_RESOLVED;
876 /* -----------------------------------------------------------------------------
877 * delete an object from the pool
880 unloadObj( char *path )
882 ObjectCode *oc, *prev;
884 ASSERT(symhash != NULL);
885 ASSERT(objects != NULL);
888 for (oc = objects; oc; prev = oc, oc = oc->next) {
889 if (!strcmp(oc->fileName,path)) {
891 /* Remove all the mappings for the symbols within this
896 for (i = 0; i < oc->n_symbols; i++) {
897 if (oc->symbols[i] != NULL) {
898 removeStrHashTable(symhash, oc->symbols[i], NULL);
906 prev->next = oc->next;
909 /* We're going to leave this in place, in case there are
910 any pointers from the heap into it: */
911 /* free(oc->image); */
915 /* The local hash table should have been freed at the end
916 of the ocResolve_ call on it. */
917 ASSERT(oc->lochash == NULL);
923 belch("unloadObj: can't find `%s' to unload", path);
927 /* -----------------------------------------------------------------------------
928 * Sanity checking. For each ObjectCode, maintain a list of address ranges
929 * which may be prodded during relocation, and abort if we try and write
930 * outside any of these.
932 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
935 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
936 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
940 pb->next = oc->proddables;
944 static void checkProddableBlock ( ObjectCode* oc, void* addr )
947 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
948 char* s = (char*)(pb->start);
949 char* e = s + pb->size - 1;
950 char* a = (char*)addr;
951 /* Assumes that the biggest fixup involves a 4-byte write. This
952 probably needs to be changed to 8 (ie, +7) on 64-bit
954 if (a >= s && (a+3) <= e) return;
956 barf("checkProddableBlock: invalid fixup in runtime linker");
959 /* -----------------------------------------------------------------------------
960 * Section management.
962 static void addSection ( ObjectCode* oc, SectionKind kind,
963 void* start, void* end )
965 Section* s = stgMallocBytes(sizeof(Section), "addSection");
969 s->next = oc->sections;
972 fprintf(stderr, "addSection: %p-%p (size %d), kind %d\n",
973 start, ((char*)end)-1, end - start + 1, kind );
979 /* --------------------------------------------------------------------------
980 * PEi386 specifics (Win32 targets)
981 * ------------------------------------------------------------------------*/
983 /* The information for this linker comes from
984 Microsoft Portable Executable
985 and Common Object File Format Specification
986 revision 5.1 January 1998
987 which SimonM says comes from the MS Developer Network CDs.
989 It can be found there (on older CDs), but can also be found
992 http://www.microsoft.com/hwdev/hardware/PECOFF.asp
994 (this is Rev 6.0 from February 1999).
996 Things move, so if that fails, try searching for it via
998 http://www.google.com/search?q=PE+COFF+specification
1000 The ultimate reference for the PE format is the Winnt.h
1001 header file that comes with the Platform SDKs; as always,
1002 implementations will drift wrt their documentation.
1004 A good background article on the PE format is Matt Pietrek's
1005 March 1994 article in Microsoft System Journal (MSJ)
1006 (Vol.9, No. 3): "Peering Inside the PE: A Tour of the
1007 Win32 Portable Executable File Format." The info in there
1008 has recently been updated in a two part article in
1009 MSDN magazine, issues Feb and March 2002,
1010 "Inside Windows: An In-Depth Look into the Win32 Portable
1011 Executable File Format"
1013 John Levine's book "Linkers and Loaders" contains useful
1018 #if defined(OBJFORMAT_PEi386)
1022 typedef unsigned char UChar;
1023 typedef unsigned short UInt16;
1024 typedef unsigned int UInt32;
1031 UInt16 NumberOfSections;
1032 UInt32 TimeDateStamp;
1033 UInt32 PointerToSymbolTable;
1034 UInt32 NumberOfSymbols;
1035 UInt16 SizeOfOptionalHeader;
1036 UInt16 Characteristics;
1040 #define sizeof_COFF_header 20
1047 UInt32 VirtualAddress;
1048 UInt32 SizeOfRawData;
1049 UInt32 PointerToRawData;
1050 UInt32 PointerToRelocations;
1051 UInt32 PointerToLinenumbers;
1052 UInt16 NumberOfRelocations;
1053 UInt16 NumberOfLineNumbers;
1054 UInt32 Characteristics;
1058 #define sizeof_COFF_section 40
1065 UInt16 SectionNumber;
1068 UChar NumberOfAuxSymbols;
1072 #define sizeof_COFF_symbol 18
1077 UInt32 VirtualAddress;
1078 UInt32 SymbolTableIndex;
1083 #define sizeof_COFF_reloc 10
1086 /* From PE spec doc, section 3.3.2 */
1087 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
1088 windows.h -- for the same purpose, but I want to know what I'm
1090 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
1091 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
1092 #define MYIMAGE_FILE_DLL 0x2000
1093 #define MYIMAGE_FILE_SYSTEM 0x1000
1094 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
1095 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
1096 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
1098 /* From PE spec doc, section 5.4.2 and 5.4.4 */
1099 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
1100 #define MYIMAGE_SYM_CLASS_STATIC 3
1101 #define MYIMAGE_SYM_UNDEFINED 0
1103 /* From PE spec doc, section 4.1 */
1104 #define MYIMAGE_SCN_CNT_CODE 0x00000020
1105 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
1106 #define MYIMAGE_SCN_LNK_NRELOC_OVFL 0x01000000
1108 /* From PE spec doc, section 5.2.1 */
1109 #define MYIMAGE_REL_I386_DIR32 0x0006
1110 #define MYIMAGE_REL_I386_REL32 0x0014
1113 /* We use myindex to calculate array addresses, rather than
1114 simply doing the normal subscript thing. That's because
1115 some of the above structs have sizes which are not
1116 a whole number of words. GCC rounds their sizes up to a
1117 whole number of words, which means that the address calcs
1118 arising from using normal C indexing or pointer arithmetic
1119 are just plain wrong. Sigh.
1122 myindex ( int scale, void* base, int index )
1125 ((UChar*)base) + scale * index;
1130 printName ( UChar* name, UChar* strtab )
1132 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1133 UInt32 strtab_offset = * (UInt32*)(name+4);
1134 fprintf ( stderr, "%s", strtab + strtab_offset );
1137 for (i = 0; i < 8; i++) {
1138 if (name[i] == 0) break;
1139 fprintf ( stderr, "%c", name[i] );
1146 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
1148 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1149 UInt32 strtab_offset = * (UInt32*)(name+4);
1150 strncpy ( dst, strtab+strtab_offset, dstSize );
1156 if (name[i] == 0) break;
1166 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
1169 /* If the string is longer than 8 bytes, look in the
1170 string table for it -- this will be correctly zero terminated.
1172 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
1173 UInt32 strtab_offset = * (UInt32*)(name+4);
1174 return ((UChar*)strtab) + strtab_offset;
1176 /* Otherwise, if shorter than 8 bytes, return the original,
1177 which by defn is correctly terminated.
1179 if (name[7]==0) return name;
1180 /* The annoying case: 8 bytes. Copy into a temporary
1181 (which is never freed ...)
1183 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
1185 strncpy(newstr,name,8);
1191 /* Just compares the short names (first 8 chars) */
1192 static COFF_section *
1193 findPEi386SectionCalled ( ObjectCode* oc, char* name )
1197 = (COFF_header*)(oc->image);
1198 COFF_section* sectab
1200 ((UChar*)(oc->image))
1201 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1203 for (i = 0; i < hdr->NumberOfSections; i++) {
1206 COFF_section* section_i
1208 myindex ( sizeof_COFF_section, sectab, i );
1209 n1 = (UChar*) &(section_i->Name);
1211 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
1212 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
1213 n1[6]==n2[6] && n1[7]==n2[7])
1222 zapTrailingAtSign ( UChar* sym )
1224 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
1226 if (sym[0] == 0) return;
1228 while (sym[i] != 0) i++;
1231 while (j > 0 && my_isdigit(sym[j])) j--;
1232 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
1238 ocVerifyImage_PEi386 ( ObjectCode* oc )
1243 COFF_section* sectab;
1244 COFF_symbol* symtab;
1246 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1247 hdr = (COFF_header*)(oc->image);
1248 sectab = (COFF_section*) (
1249 ((UChar*)(oc->image))
1250 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1252 symtab = (COFF_symbol*) (
1253 ((UChar*)(oc->image))
1254 + hdr->PointerToSymbolTable
1256 strtab = ((UChar*)symtab)
1257 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1259 if (hdr->Machine != 0x14c) {
1260 belch("Not x86 PEi386");
1263 if (hdr->SizeOfOptionalHeader != 0) {
1264 belch("PEi386 with nonempty optional header");
1267 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1268 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1269 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1270 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1271 belch("Not a PEi386 object file");
1274 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1275 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1276 belch("Invalid PEi386 word size or endiannness: %d",
1277 (int)(hdr->Characteristics));
1280 /* If the string table size is way crazy, this might indicate that
1281 there are more than 64k relocations, despite claims to the
1282 contrary. Hence this test. */
1283 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1285 if ( (*(UInt32*)strtab) > 600000 ) {
1286 /* Note that 600k has no special significance other than being
1287 big enough to handle the almost-2MB-sized lumps that
1288 constitute HSwin32*.o. */
1289 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1294 /* No further verification after this point; only debug printing. */
1296 IF_DEBUG(linker, i=1);
1297 if (i == 0) return 1;
1300 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1302 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1304 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1306 fprintf ( stderr, "\n" );
1308 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1310 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1312 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1314 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1316 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1318 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1320 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1322 /* Print the section table. */
1323 fprintf ( stderr, "\n" );
1324 for (i = 0; i < hdr->NumberOfSections; i++) {
1326 COFF_section* sectab_i
1328 myindex ( sizeof_COFF_section, sectab, i );
1335 printName ( sectab_i->Name, strtab );
1345 sectab_i->VirtualSize,
1346 sectab_i->VirtualAddress,
1347 sectab_i->SizeOfRawData,
1348 sectab_i->PointerToRawData,
1349 sectab_i->NumberOfRelocations,
1350 sectab_i->PointerToRelocations,
1351 sectab_i->PointerToRawData
1353 reltab = (COFF_reloc*) (
1354 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1357 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1358 /* If the relocation field (a short) has overflowed, the
1359 * real count can be found in the first reloc entry.
1361 * See Section 4.1 (last para) of the PE spec (rev6.0).
1363 COFF_reloc* rel = (COFF_reloc*)
1364 myindex ( sizeof_COFF_reloc, reltab, 0 );
1365 noRelocs = rel->VirtualAddress;
1368 noRelocs = sectab_i->NumberOfRelocations;
1372 for (; j < noRelocs; j++) {
1374 COFF_reloc* rel = (COFF_reloc*)
1375 myindex ( sizeof_COFF_reloc, reltab, j );
1377 " type 0x%-4x vaddr 0x%-8x name `",
1379 rel->VirtualAddress );
1380 sym = (COFF_symbol*)
1381 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1382 /* Hmm..mysterious looking offset - what's it for? SOF */
1383 printName ( sym->Name, strtab -10 );
1384 fprintf ( stderr, "'\n" );
1387 fprintf ( stderr, "\n" );
1389 fprintf ( stderr, "\n" );
1390 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1391 fprintf ( stderr, "---START of string table---\n");
1392 for (i = 4; i < *(Int32*)strtab; i++) {
1394 fprintf ( stderr, "\n"); else
1395 fprintf( stderr, "%c", strtab[i] );
1397 fprintf ( stderr, "--- END of string table---\n");
1399 fprintf ( stderr, "\n" );
1402 COFF_symbol* symtab_i;
1403 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1404 symtab_i = (COFF_symbol*)
1405 myindex ( sizeof_COFF_symbol, symtab, i );
1411 printName ( symtab_i->Name, strtab );
1420 (Int32)(symtab_i->SectionNumber),
1421 (UInt32)symtab_i->Type,
1422 (UInt32)symtab_i->StorageClass,
1423 (UInt32)symtab_i->NumberOfAuxSymbols
1425 i += symtab_i->NumberOfAuxSymbols;
1429 fprintf ( stderr, "\n" );
1435 ocGetNames_PEi386 ( ObjectCode* oc )
1438 COFF_section* sectab;
1439 COFF_symbol* symtab;
1446 hdr = (COFF_header*)(oc->image);
1447 sectab = (COFF_section*) (
1448 ((UChar*)(oc->image))
1449 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1451 symtab = (COFF_symbol*) (
1452 ((UChar*)(oc->image))
1453 + hdr->PointerToSymbolTable
1455 strtab = ((UChar*)(oc->image))
1456 + hdr->PointerToSymbolTable
1457 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1459 /* Allocate space for any (local, anonymous) .bss sections. */
1461 for (i = 0; i < hdr->NumberOfSections; i++) {
1463 COFF_section* sectab_i
1465 myindex ( sizeof_COFF_section, sectab, i );
1466 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1467 if (sectab_i->VirtualSize == 0) continue;
1468 /* This is a non-empty .bss section. Allocate zeroed space for
1469 it, and set its PointerToRawData field such that oc->image +
1470 PointerToRawData == addr_of_zeroed_space. */
1471 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1472 "ocGetNames_PEi386(anonymous bss)");
1473 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1474 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1475 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1478 /* Copy section information into the ObjectCode. */
1480 for (i = 0; i < hdr->NumberOfSections; i++) {
1486 = SECTIONKIND_OTHER;
1487 COFF_section* sectab_i
1489 myindex ( sizeof_COFF_section, sectab, i );
1490 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1493 /* I'm sure this is the Right Way to do it. However, the
1494 alternative of testing the sectab_i->Name field seems to
1495 work ok with Cygwin.
1497 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1498 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1499 kind = SECTIONKIND_CODE_OR_RODATA;
1502 if (0==strcmp(".text",sectab_i->Name) ||
1503 0==strcmp(".rodata",sectab_i->Name))
1504 kind = SECTIONKIND_CODE_OR_RODATA;
1505 if (0==strcmp(".data",sectab_i->Name) ||
1506 0==strcmp(".bss",sectab_i->Name))
1507 kind = SECTIONKIND_RWDATA;
1509 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1510 sz = sectab_i->SizeOfRawData;
1511 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1513 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1514 end = start + sz - 1;
1516 if (kind == SECTIONKIND_OTHER
1517 /* Ignore sections called which contain stabs debugging
1519 && 0 != strcmp(".stab", sectab_i->Name)
1520 && 0 != strcmp(".stabstr", sectab_i->Name)
1522 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1526 if (kind != SECTIONKIND_OTHER && end >= start) {
1527 addSection(oc, kind, start, end);
1528 addProddableBlock(oc, start, end - start + 1);
1532 /* Copy exported symbols into the ObjectCode. */
1534 oc->n_symbols = hdr->NumberOfSymbols;
1535 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1536 "ocGetNames_PEi386(oc->symbols)");
1537 /* Call me paranoid; I don't care. */
1538 for (i = 0; i < oc->n_symbols; i++)
1539 oc->symbols[i] = NULL;
1543 COFF_symbol* symtab_i;
1544 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1545 symtab_i = (COFF_symbol*)
1546 myindex ( sizeof_COFF_symbol, symtab, i );
1550 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1551 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1552 /* This symbol is global and defined, viz, exported */
1553 /* for MYIMAGE_SYMCLASS_EXTERNAL
1554 && !MYIMAGE_SYM_UNDEFINED,
1555 the address of the symbol is:
1556 address of relevant section + offset in section
1558 COFF_section* sectabent
1559 = (COFF_section*) myindex ( sizeof_COFF_section,
1561 symtab_i->SectionNumber-1 );
1562 addr = ((UChar*)(oc->image))
1563 + (sectabent->PointerToRawData
1567 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1568 && symtab_i->Value > 0) {
1569 /* This symbol isn't in any section at all, ie, global bss.
1570 Allocate zeroed space for it. */
1571 addr = stgCallocBytes(1, symtab_i->Value,
1572 "ocGetNames_PEi386(non-anonymous bss)");
1573 addSection(oc, SECTIONKIND_RWDATA, addr,
1574 ((UChar*)addr) + symtab_i->Value - 1);
1575 addProddableBlock(oc, addr, symtab_i->Value);
1576 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1579 if (addr != NULL ) {
1580 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1581 /* fprintf(stderr,"addSymbol %p `%s \n", addr,sname); */
1582 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1583 ASSERT(i >= 0 && i < oc->n_symbols);
1584 /* cstring_from_COFF_symbol_name always succeeds. */
1585 oc->symbols[i] = sname;
1586 ghciInsertStrHashTable(oc->fileName, symhash, sname, addr);
1590 "IGNORING symbol %d\n"
1594 printName ( symtab_i->Name, strtab );
1603 (Int32)(symtab_i->SectionNumber),
1604 (UInt32)symtab_i->Type,
1605 (UInt32)symtab_i->StorageClass,
1606 (UInt32)symtab_i->NumberOfAuxSymbols
1611 i += symtab_i->NumberOfAuxSymbols;
1620 ocResolve_PEi386 ( ObjectCode* oc )
1623 COFF_section* sectab;
1624 COFF_symbol* symtab;
1634 /* ToDo: should be variable-sized? But is at least safe in the
1635 sense of buffer-overrun-proof. */
1637 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1639 hdr = (COFF_header*)(oc->image);
1640 sectab = (COFF_section*) (
1641 ((UChar*)(oc->image))
1642 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1644 symtab = (COFF_symbol*) (
1645 ((UChar*)(oc->image))
1646 + hdr->PointerToSymbolTable
1648 strtab = ((UChar*)(oc->image))
1649 + hdr->PointerToSymbolTable
1650 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1652 for (i = 0; i < hdr->NumberOfSections; i++) {
1653 COFF_section* sectab_i
1655 myindex ( sizeof_COFF_section, sectab, i );
1658 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1661 /* Ignore sections called which contain stabs debugging
1663 if (0 == strcmp(".stab", sectab_i->Name)
1664 || 0 == strcmp(".stabstr", sectab_i->Name))
1667 if ( sectab_i->Characteristics & MYIMAGE_SCN_LNK_NRELOC_OVFL ) {
1668 /* If the relocation field (a short) has overflowed, the
1669 * real count can be found in the first reloc entry.
1671 * See Section 4.1 (last para) of the PE spec (rev6.0).
1673 COFF_reloc* rel = (COFF_reloc*)
1674 myindex ( sizeof_COFF_reloc, reltab, 0 );
1675 noRelocs = rel->VirtualAddress;
1676 fprintf(stderr, "Overflown relocs: %u\n", noRelocs);
1679 noRelocs = sectab_i->NumberOfRelocations;
1684 for (; j < noRelocs; j++) {
1686 COFF_reloc* reltab_j
1688 myindex ( sizeof_COFF_reloc, reltab, j );
1690 /* the location to patch */
1692 ((UChar*)(oc->image))
1693 + (sectab_i->PointerToRawData
1694 + reltab_j->VirtualAddress
1695 - sectab_i->VirtualAddress )
1697 /* the existing contents of pP */
1699 /* the symbol to connect to */
1700 sym = (COFF_symbol*)
1701 myindex ( sizeof_COFF_symbol,
1702 symtab, reltab_j->SymbolTableIndex );
1705 "reloc sec %2d num %3d: type 0x%-4x "
1706 "vaddr 0x%-8x name `",
1708 (UInt32)reltab_j->Type,
1709 reltab_j->VirtualAddress );
1710 printName ( sym->Name, strtab );
1711 fprintf ( stderr, "'\n" ));
1713 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1714 COFF_section* section_sym
1715 = findPEi386SectionCalled ( oc, sym->Name );
1717 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1720 S = ((UInt32)(oc->image))
1721 + (section_sym->PointerToRawData
1724 copyName ( sym->Name, strtab, symbol, 1000-1 );
1725 (void*)S = lookupLocalSymbol( oc, symbol );
1726 if ((void*)S != NULL) goto foundit;
1727 (void*)S = lookupSymbol( symbol );
1728 if ((void*)S != NULL) goto foundit;
1729 zapTrailingAtSign ( symbol );
1730 (void*)S = lookupLocalSymbol( oc, symbol );
1731 if ((void*)S != NULL) goto foundit;
1732 (void*)S = lookupSymbol( symbol );
1733 if ((void*)S != NULL) goto foundit;
1734 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1738 checkProddableBlock(oc, pP);
1739 switch (reltab_j->Type) {
1740 case MYIMAGE_REL_I386_DIR32:
1743 case MYIMAGE_REL_I386_REL32:
1744 /* Tricky. We have to insert a displacement at
1745 pP which, when added to the PC for the _next_
1746 insn, gives the address of the target (S).
1747 Problem is to know the address of the next insn
1748 when we only know pP. We assume that this
1749 literal field is always the last in the insn,
1750 so that the address of the next insn is pP+4
1751 -- hence the constant 4.
1752 Also I don't know if A should be added, but so
1753 far it has always been zero.
1756 *pP = S - ((UInt32)pP) - 4;
1759 belch("%s: unhandled PEi386 relocation type %d",
1760 oc->fileName, reltab_j->Type);
1767 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1771 #endif /* defined(OBJFORMAT_PEi386) */
1774 /* --------------------------------------------------------------------------
1776 * ------------------------------------------------------------------------*/
1778 #if defined(OBJFORMAT_ELF)
1783 #if defined(sparc_TARGET_ARCH)
1784 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1785 #elif defined(i386_TARGET_ARCH)
1786 # define ELF_TARGET_386 /* Used inside <elf.h> */
1788 /* There is a similar case for IA64 in the Solaris2 headers if this
1789 * ever becomes relevant.
1796 findElfSection ( void* objImage, Elf32_Word sh_type )
1799 char* ehdrC = (char*)objImage;
1800 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1801 Elf32_Shdr* shdr = (Elf32_Shdr*)(ehdrC + ehdr->e_shoff);
1802 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1804 for (i = 0; i < ehdr->e_shnum; i++) {
1805 if (shdr[i].sh_type == sh_type
1806 /* Ignore the section header's string table. */
1807 && i != ehdr->e_shstrndx
1808 /* Ignore string tables named .stabstr, as they contain
1810 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
1812 ptr = ehdrC + shdr[i].sh_offset;
1821 ocVerifyImage_ELF ( ObjectCode* oc )
1825 int i, j, nent, nstrtab, nsymtabs;
1829 char* ehdrC = (char*)(oc->image);
1830 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1832 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1833 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1834 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1835 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1836 belch("%s: not an ELF header", oc->fileName);
1839 IF_DEBUG(linker,belch( "Is an ELF header" ));
1841 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1842 belch("%s: not 32 bit ELF", oc->fileName);
1846 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1848 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1849 IF_DEBUG(linker,belch( "Is little-endian" ));
1851 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1852 IF_DEBUG(linker,belch( "Is big-endian" ));
1854 belch("%s: unknown endiannness", oc->fileName);
1858 if (ehdr->e_type != ET_REL) {
1859 belch("%s: not a relocatable object (.o) file", oc->fileName);
1862 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1864 IF_DEBUG(linker,belch( "Architecture is " ));
1865 switch (ehdr->e_machine) {
1866 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1867 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1868 default: IF_DEBUG(linker,belch( "unknown" ));
1869 belch("%s: unknown architecture", oc->fileName);
1873 IF_DEBUG(linker,belch(
1874 "\nSection header table: start %d, n_entries %d, ent_size %d",
1875 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1877 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1879 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1881 if (ehdr->e_shstrndx == SHN_UNDEF) {
1882 belch("%s: no section header string table", oc->fileName);
1885 IF_DEBUG(linker,belch( "Section header string table is section %d",
1887 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1890 for (i = 0; i < ehdr->e_shnum; i++) {
1891 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1892 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1893 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1894 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1895 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1896 ehdrC + shdr[i].sh_offset,
1897 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1899 if (shdr[i].sh_type == SHT_REL) {
1900 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1901 } else if (shdr[i].sh_type == SHT_RELA) {
1902 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1904 IF_DEBUG(linker,fprintf(stderr," "));
1907 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1911 IF_DEBUG(linker,belch( "\nString tables" ));
1914 for (i = 0; i < ehdr->e_shnum; i++) {
1915 if (shdr[i].sh_type == SHT_STRTAB
1916 /* Ignore the section header's string table. */
1917 && i != ehdr->e_shstrndx
1918 /* Ignore string tables named .stabstr, as they contain
1920 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
1922 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1923 strtab = ehdrC + shdr[i].sh_offset;
1928 belch("%s: no string tables, or too many", oc->fileName);
1933 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1934 for (i = 0; i < ehdr->e_shnum; i++) {
1935 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1936 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1938 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1939 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1940 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1942 shdr[i].sh_size % sizeof(Elf32_Sym)
1944 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1945 belch("%s: non-integral number of symbol table entries", oc->fileName);
1948 for (j = 0; j < nent; j++) {
1949 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1950 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1951 (int)stab[j].st_shndx,
1952 (int)stab[j].st_size,
1953 (char*)stab[j].st_value ));
1955 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1956 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1957 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1958 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1959 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1960 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1961 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1962 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1964 IF_DEBUG(linker,fprintf(stderr, " " ));
1966 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1967 switch (ELF32_ST_BIND(stab[j].st_info)) {
1968 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1969 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1970 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1971 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1973 IF_DEBUG(linker,fprintf(stderr, " " ));
1975 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1979 if (nsymtabs == 0) {
1980 belch("%s: didn't find any symbol tables", oc->fileName);
1989 ocGetNames_ELF ( ObjectCode* oc )
1994 char* ehdrC = (char*)(oc->image);
1995 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1996 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1997 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1999 ASSERT(symhash != NULL);
2002 belch("%s: no strtab", oc->fileName);
2007 for (i = 0; i < ehdr->e_shnum; i++) {
2008 /* Figure out what kind of section it is. Logic derived from
2009 Figure 1.14 ("Special Sections") of the ELF document
2010 ("Portable Formats Specification, Version 1.1"). */
2011 Elf32_Shdr hdr = shdr[i];
2012 SectionKind kind = SECTIONKIND_OTHER;
2015 if (hdr.sh_type == SHT_PROGBITS
2016 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_EXECINSTR)) {
2017 /* .text-style section */
2018 kind = SECTIONKIND_CODE_OR_RODATA;
2021 if (hdr.sh_type == SHT_PROGBITS
2022 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2023 /* .data-style section */
2024 kind = SECTIONKIND_RWDATA;
2027 if (hdr.sh_type == SHT_PROGBITS
2028 && (hdr.sh_flags & SHF_ALLOC) && !(hdr.sh_flags & SHF_WRITE)) {
2029 /* .rodata-style section */
2030 kind = SECTIONKIND_CODE_OR_RODATA;
2033 if (hdr.sh_type == SHT_NOBITS
2034 && (hdr.sh_flags & SHF_ALLOC) && (hdr.sh_flags & SHF_WRITE)) {
2035 /* .bss-style section */
2036 kind = SECTIONKIND_RWDATA;
2040 if (is_bss && shdr[i].sh_size > 0) {
2041 /* This is a non-empty .bss section. Allocate zeroed space for
2042 it, and set its .sh_offset field such that
2043 ehdrC + .sh_offset == addr_of_zeroed_space. */
2044 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
2045 "ocGetNames_ELF(BSS)");
2046 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
2048 fprintf(stderr, "BSS section at 0x%x, size %d\n",
2049 zspace, shdr[i].sh_size);
2053 /* fill in the section info */
2054 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0) {
2055 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
2056 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
2057 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
2060 if (shdr[i].sh_type != SHT_SYMTAB) continue;
2062 /* copy stuff into this module's object symbol table */
2063 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
2064 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
2066 oc->n_symbols = nent;
2067 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
2068 "ocGetNames_ELF(oc->symbols)");
2070 for (j = 0; j < nent; j++) {
2072 char isLocal = FALSE; /* avoids uninit-var warning */
2074 char* nm = strtab + stab[j].st_name;
2075 int secno = stab[j].st_shndx;
2077 /* Figure out if we want to add it; if so, set ad to its
2078 address. Otherwise leave ad == NULL. */
2080 if (secno == SHN_COMMON) {
2082 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
2084 fprintf(stderr, "COMMON symbol, size %d name %s\n",
2085 stab[j].st_size, nm);
2087 /* Pointless to do addProddableBlock() for this area,
2088 since the linker should never poke around in it. */
2091 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
2092 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
2094 /* and not an undefined symbol */
2095 && stab[j].st_shndx != SHN_UNDEF
2096 /* and not in a "special section" */
2097 && stab[j].st_shndx < SHN_LORESERVE
2099 /* and it's a not a section or string table or anything silly */
2100 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
2101 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
2102 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
2105 /* Section 0 is the undefined section, hence > and not >=. */
2106 ASSERT(secno > 0 && secno < ehdr->e_shnum);
2108 if (shdr[secno].sh_type == SHT_NOBITS) {
2109 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
2110 stab[j].st_size, stab[j].st_value, nm);
2113 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
2114 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
2117 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
2118 ad, oc->fileName, nm ));
2123 /* And the decision is ... */
2127 oc->symbols[j] = nm;
2130 /* Ignore entirely. */
2132 ghciInsertStrHashTable(oc->fileName, symhash, nm, ad);
2136 IF_DEBUG(linker,belch( "skipping `%s'",
2137 strtab + stab[j].st_name ));
2140 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
2141 (int)ELF32_ST_BIND(stab[j].st_info),
2142 (int)ELF32_ST_TYPE(stab[j].st_info),
2143 (int)stab[j].st_shndx,
2144 strtab + stab[j].st_name
2147 oc->symbols[j] = NULL;
2157 /* Do ELF relocations which lack an explicit addend. All x86-linux
2158 relocations appear to be of this form. */
2160 do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
2161 Elf32_Shdr* shdr, int shnum,
2162 Elf32_Sym* stab, char* strtab )
2167 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
2168 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
2169 int target_shndx = shdr[shnum].sh_info;
2170 int symtab_shndx = shdr[shnum].sh_link;
2171 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2172 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2173 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2174 target_shndx, symtab_shndx ));
2175 for (j = 0; j < nent; j++) {
2176 Elf32_Addr offset = rtab[j].r_offset;
2177 Elf32_Word info = rtab[j].r_info;
2179 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
2180 Elf32_Word* pP = (Elf32_Word*)P;
2184 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
2185 j, (void*)offset, (void*)info ));
2187 IF_DEBUG(linker,belch( " ZERO" ));
2190 Elf32_Sym sym = stab[ELF32_R_SYM(info)];
2191 /* First see if it is a local symbol. */
2192 if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
2193 /* Yes, so we can get the address directly from the ELF symbol
2195 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2197 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2198 + stab[ELF32_R_SYM(info)].st_value);
2201 /* No, so look up the name in our global table. */
2202 symbol = strtab + sym.st_name;
2203 (void*)S = lookupSymbol( symbol );
2206 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2209 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2211 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
2212 (void*)P, (void*)S, (void*)A ));
2213 checkProddableBlock ( oc, pP );
2214 switch (ELF32_R_TYPE(info)) {
2215 # ifdef i386_TARGET_ARCH
2216 case R_386_32: *pP = S + A; break;
2217 case R_386_PC32: *pP = S + A - P; break;
2220 belch("%s: unhandled ELF relocation(Rel) type %d\n",
2221 oc->fileName, ELF32_R_TYPE(info));
2230 /* Do ELF relocations for which explicit addends are supplied.
2231 sparc-solaris relocations appear to be of this form. */
2233 do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
2234 Elf32_Shdr* shdr, int shnum,
2235 Elf32_Sym* stab, char* strtab )
2240 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
2241 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
2242 int target_shndx = shdr[shnum].sh_info;
2243 int symtab_shndx = shdr[shnum].sh_link;
2244 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
2245 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
2246 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
2247 target_shndx, symtab_shndx ));
2248 for (j = 0; j < nent; j++) {
2249 Elf32_Addr offset = rtab[j].r_offset;
2250 Elf32_Word info = rtab[j].r_info;
2251 # if defined(sparc_TARGET_ARCH) || defined(DEBUG)
2252 Elf32_Sword addend = rtab[j].r_addend;
2253 Elf32_Addr A = addend;
2255 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
2257 # if defined(sparc_TARGET_ARCH)
2258 /* This #ifdef only serves to avoid unused-var warnings. */
2259 Elf32_Word* pP = (Elf32_Word*)P;
2263 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
2264 j, (void*)offset, (void*)info,
2267 IF_DEBUG(linker,belch( " ZERO" ));
2270 Elf32_Sym sym = stab[ELF32_R_SYM(info)];
2271 /* First see if it is a local symbol. */
2272 if (ELF32_ST_BIND(sym.st_info) == STB_LOCAL) {
2273 /* Yes, so we can get the address directly from the ELF symbol
2275 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
2277 (ehdrC + shdr[ sym.st_shndx ].sh_offset
2278 + stab[ELF32_R_SYM(info)].st_value);
2281 /* No, so look up the name in our global table. */
2282 symbol = strtab + sym.st_name;
2283 (void*)S = lookupSymbol( symbol );
2286 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
2290 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
2293 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
2295 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
2296 (void*)P, (void*)S, (void*)A ));
2297 checkProddableBlock ( oc, (void*)P );
2298 switch (ELF32_R_TYPE(info)) {
2299 # if defined(sparc_TARGET_ARCH)
2300 case R_SPARC_WDISP30:
2301 w1 = *pP & 0xC0000000;
2302 w2 = (Elf32_Word)((S + A - P) >> 2);
2303 ASSERT((w2 & 0xC0000000) == 0);
2308 w1 = *pP & 0xFFC00000;
2309 w2 = (Elf32_Word)((S + A) >> 10);
2310 ASSERT((w2 & 0xFFC00000) == 0);
2316 w2 = (Elf32_Word)((S + A) & 0x3FF);
2317 ASSERT((w2 & ~0x3FF) == 0);
2321 /* According to the Sun documentation:
2323 This relocation type resembles R_SPARC_32, except it refers to an
2324 unaligned word. That is, the word to be relocated must be treated
2325 as four separate bytes with arbitrary alignment, not as a word
2326 aligned according to the architecture requirements.
2328 (JRS: which means that freeloading on the R_SPARC_32 case
2329 is probably wrong, but hey ...)
2333 w2 = (Elf32_Word)(S + A);
2338 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2339 oc->fileName, ELF32_R_TYPE(info));
2349 ocResolve_ELF ( ObjectCode* oc )
2353 Elf32_Sym* stab = NULL;
2354 char* ehdrC = (char*)(oc->image);
2355 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
2356 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
2357 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2359 /* first find "the" symbol table */
2360 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2362 /* also go find the string table */
2363 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2365 if (stab == NULL || strtab == NULL) {
2366 belch("%s: can't find string or symbol table", oc->fileName);
2370 /* Process the relocation sections. */
2371 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2373 /* Skip sections called ".rel.stab". These appear to contain
2374 relocation entries that, when done, make the stabs debugging
2375 info point at the right places. We ain't interested in all
2377 if (0 == memcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name, 9))
2380 if (shdr[shnum].sh_type == SHT_REL ) {
2381 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
2382 shnum, stab, strtab );
2386 if (shdr[shnum].sh_type == SHT_RELA) {
2387 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
2388 shnum, stab, strtab );
2394 /* Free the local symbol table; we won't need it again. */
2395 freeHashTable(oc->lochash, NULL);