1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.46 2001/06/27 13:56:01 sewardj Exp $
4 * (c) The GHC Team, 2000
8 * ---------------------------------------------------------------------------*/
15 #include "LinkerInternals.h"
17 #include "StoragePriv.h"
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
23 #ifdef HAVE_SYS_STAT_H
31 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
32 # define OBJFORMAT_ELF
33 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
34 # define OBJFORMAT_PEi386
38 /* Hash table mapping symbol names to Symbol */
39 /*Str*/HashTable *symhash;
41 #if defined(OBJFORMAT_ELF)
42 static int ocVerifyImage_ELF ( ObjectCode* oc );
43 static int ocGetNames_ELF ( ObjectCode* oc );
44 static int ocResolve_ELF ( ObjectCode* oc );
45 #elif defined(OBJFORMAT_PEi386)
46 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
47 static int ocGetNames_PEi386 ( ObjectCode* oc );
48 static int ocResolve_PEi386 ( ObjectCode* oc );
51 /* -----------------------------------------------------------------------------
52 * Built-in symbols from the RTS
55 typedef struct _RtsSymbolVal {
62 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
64 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
65 SymX(makeStableNamezh_fast) \
66 SymX(finalizzeWeakzh_fast)
68 /* These are not available in GUM!!! -- HWL */
69 #define Maybe_ForeignObj
70 #define Maybe_Stable_Names
73 #if !defined (mingw32_TARGET_OS)
75 #define RTS_POSIX_ONLY_SYMBOLS \
76 SymX(stg_sig_install) \
78 #define RTS_MINGW_ONLY_SYMBOLS /**/
82 #define RTS_POSIX_ONLY_SYMBOLS
84 /* These are statically linked from the mingw libraries into the ghc
85 executable, so we have to employ this hack. */
86 #define RTS_MINGW_ONLY_SYMBOLS \
105 Sym(_imp___timezone) \
115 SymX(GetCurrentProcess) \
116 SymX(GetProcessTimes) \
118 SymX(GetExitCodeProcess) \
119 SymX(WaitForSingleObject) \
120 SymX(CreateProcessA) \
125 #define RTS_SYMBOLS \
127 Sym(stg_gc_enter_1) \
134 Sym(stg_gc_unpt_r1) \
135 Sym(stg_gc_unbx_r1) \
140 SymX(stg_update_PAP) \
141 SymX(stg_ap_1_upd_info) \
142 SymX(stg_ap_2_upd_info) \
143 SymX(stg_ap_3_upd_info) \
144 SymX(stg_ap_4_upd_info) \
145 SymX(stg_ap_5_upd_info) \
146 SymX(stg_ap_6_upd_info) \
147 SymX(stg_ap_7_upd_info) \
148 SymX(stg_ap_8_upd_info) \
149 SymX(stg_sel_0_upd_info) \
150 SymX(stg_sel_1_upd_info) \
151 SymX(stg_sel_2_upd_info) \
152 SymX(stg_sel_3_upd_info) \
153 SymX(stg_sel_4_upd_info) \
154 SymX(stg_sel_5_upd_info) \
155 SymX(stg_sel_6_upd_info) \
156 SymX(stg_sel_7_upd_info) \
157 SymX(stg_sel_8_upd_info) \
158 SymX(stg_sel_9_upd_info) \
159 SymX(stg_sel_10_upd_info) \
160 SymX(stg_sel_11_upd_info) \
161 SymX(stg_sel_12_upd_info) \
162 SymX(stg_sel_13_upd_info) \
163 SymX(stg_sel_14_upd_info) \
164 SymX(stg_sel_15_upd_info) \
165 SymX(stg_upd_frame_info) \
166 SymX(stg_seq_frame_info) \
167 SymX(stg_CAF_BLACKHOLE_info) \
168 SymX(stg_IND_STATIC_info) \
169 SymX(stg_EMPTY_MVAR_info) \
170 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
171 SymX(stg_WEAK_info) \
172 SymX(stg_CHARLIKE_closure) \
173 SymX(stg_INTLIKE_closure) \
175 SymX(newBCOzh_fast) \
176 SymX(mkApUpd0zh_fast) \
177 SymX(putMVarzh_fast) \
178 SymX(newMVarzh_fast) \
179 SymX(takeMVarzh_fast) \
180 SymX(tryTakeMVarzh_fast) \
181 SymX(tryPutMVarzh_fast) \
187 SymX(killThreadzh_fast) \
188 SymX(waitReadzh_fast) \
189 SymX(waitWritezh_fast) \
190 SymX(suspendThread) \
192 SymX(stackOverflow) \
193 SymX(int2Integerzh_fast) \
194 SymX(word2Integerzh_fast) \
196 SymX(__encodeDouble) \
197 SymX(decodeDoublezh_fast) \
198 SymX(decodeFloatzh_fast) \
199 SymX(gcdIntegerzh_fast) \
200 SymX(newArrayzh_fast) \
201 SymX(unsafeThawArrayzh_fast) \
202 SymX(newByteArrayzh_fast) \
203 SymX(newMutVarzh_fast) \
204 SymX(quotRemIntegerzh_fast) \
205 SymX(quotIntegerzh_fast) \
206 SymX(remIntegerzh_fast) \
207 SymX(divExactIntegerzh_fast) \
208 SymX(divModIntegerzh_fast) \
209 SymX(timesIntegerzh_fast) \
210 SymX(minusIntegerzh_fast) \
211 SymX(plusIntegerzh_fast) \
212 SymX(andIntegerzh_fast) \
213 SymX(orIntegerzh_fast) \
214 SymX(xorIntegerzh_fast) \
215 SymX(complementIntegerzh_fast) \
217 SymX(blockAsyncExceptionszh_fast) \
218 SymX(unblockAsyncExceptionszh_fast) \
220 SymX(isDoubleInfinite) \
221 SymX(isDoubleDenormalized) \
222 SymX(isDoubleNegativeZero) \
223 SymX(__encodeFloat) \
225 SymX(isFloatInfinite) \
226 SymX(isFloatDenormalized) \
227 SymX(isFloatNegativeZero) \
228 SymX(__int_encodeFloat) \
229 SymX(__int_encodeDouble) \
230 SymX(__gmpz_cmp_si) \
231 SymX(__gmpz_cmp_ui) \
234 SymX(__gmpz_get_si) \
235 SymX(__gmpz_get_ui) \
238 SymX(resetNonBlockingFd) \
241 SymX(stable_ptr_table) \
242 SymX(shutdownHaskellAndExit) \
243 Sym(stg_enterStackTop) \
244 Sym(stg_yield_to_interpreter) \
248 Sym(__init_PrelGHC) \
249 SymX(freeHaskellFunctionPtr) \
252 SymX(NoRunnableThreadsHook) \
253 SymX(StackOverflowHook) \
254 SymX(OutOfHeapHook) \
255 SymX(MallocFailHook) \
256 SymX(PatErrorHdrHook) \
259 SymX(PostTraceHook) \
260 SymX(createAdjustor) \
275 SymX(rts_mkStablePtr) \
284 SymX(rts_getWord32) \
287 SymX(rts_getDouble) \
288 SymX(rts_getStablePtr) \
294 SymX(rts_evalLazyIO) \
295 SymX(rts_checkSchedStatus)
297 #ifndef SUPPORT_LONG_LONGS
298 #define RTS_LONG_LONG_SYMS /* nothing */
300 #define RTS_LONG_LONG_SYMS \
313 SymX(stg_remWord64) \
314 SymX(stg_quotWord64) \
316 SymX(stg_quotInt64) \
317 SymX(stg_negateInt64) \
318 SymX(stg_plusInt64) \
319 SymX(stg_minusInt64) \
320 SymX(stg_timesInt64) \
326 SymX(stg_shiftRL64) \
327 SymX(stg_iShiftL64) \
328 SymX(stg_iShiftRL64) \
329 SymX(stg_iShiftRA64) \
330 SymX(stg_intToInt64) \
331 SymX(stg_int64ToInt) \
332 SymX(stg_int64ToWord64) \
333 SymX(stg_wordToWord64) \
334 SymX(stg_word64ToWord) \
335 SymX(stg_word64ToInt64) \
336 SymX(int64ToIntegerzh_fast) \
337 SymX(word64ToIntegerzh_fast)
338 #endif /* SUPPORT_LONG_LONGS */
340 /* entirely bogus claims about types of these symbols */
341 #define Sym(vvv) extern void (vvv);
342 #define SymX(vvv) /**/
345 RTS_POSIX_ONLY_SYMBOLS
346 RTS_MINGW_ONLY_SYMBOLS
350 #ifdef LEADING_UNDERSCORE
351 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
353 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
356 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
358 #define SymX(vvv) Sym(vvv)
360 static RtsSymbolVal rtsSyms[] = {
363 RTS_POSIX_ONLY_SYMBOLS
364 RTS_MINGW_ONLY_SYMBOLS
365 { 0, 0 } /* sentinel */
368 /* -----------------------------------------------------------------------------
369 * initialize the object linker
371 #if defined(OBJFORMAT_ELF)
372 static void *dl_prog_handle;
380 symhash = allocStrHashTable();
382 /* populate the symbol table with stuff from the RTS */
383 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
384 insertStrHashTable(symhash, sym->lbl, sym->addr);
386 # if defined(OBJFORMAT_ELF)
387 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
391 /* -----------------------------------------------------------------------------
392 * Add a DLL from which symbols may be found. In the ELF case, just
393 * do RTLD_GLOBAL-style add, so no further messing around needs to
394 * happen in order that symbols in the loaded .so are findable --
395 * lookupSymbol() will subsequently see them by dlsym on the program's
396 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
398 * In the PEi386 case, open the DLLs and put handles to them in a
399 * linked list. When looking for a symbol, try all handles in the
403 #if defined(OBJFORMAT_PEi386)
404 /* A record for storing handles into DLLs. */
409 struct _OpenedDLL* next;
414 /* A list thereof. */
415 static OpenedDLL* opened_dlls = NULL;
421 addDLL ( char* path, char* dll_name )
423 # if defined(OBJFORMAT_ELF)
428 if (path == NULL || strlen(path) == 0) {
429 buf = stgMallocBytes(strlen(dll_name) + 10, "addDll");
430 sprintf(buf, "lib%s.so", dll_name);
432 buf = stgMallocBytes(strlen(path) + 1 + strlen(dll_name) + 10, "addDll");
433 sprintf(buf, "%s/lib%s.so", path, dll_name);
435 hdl = dlopen(buf, RTLD_NOW | RTLD_GLOBAL );
438 /* dlopen failed; return a ptr to the error msg. */
440 if (errmsg == NULL) errmsg = "addDLL: unknown error";
447 # elif defined(OBJFORMAT_PEi386)
449 /* Add this DLL to the list of DLLs in which to search for symbols.
450 The path argument is ignored. */
455 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
457 /* See if we've already got it, and ignore if so. */
458 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
459 if (0 == strcmp(o_dll->name, dll_name))
463 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
464 sprintf(buf, "%s.DLL", dll_name);
465 instance = LoadLibrary(buf);
467 if (instance == NULL) {
468 /* LoadLibrary failed; return a ptr to the error msg. */
469 return "addDLL: unknown error";
472 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
473 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
474 strcpy(o_dll->name, dll_name);
475 o_dll->instance = instance;
476 o_dll->next = opened_dlls;
481 barf("addDLL: not implemented on this platform");
485 /* -----------------------------------------------------------------------------
486 * lookup a symbol in the hash table
489 lookupSymbol( char *lbl )
492 ASSERT(symhash != NULL);
493 val = lookupStrHashTable(symhash, lbl);
496 # if defined(OBJFORMAT_ELF)
497 return dlsym(dl_prog_handle, lbl);
498 # elif defined(OBJFORMAT_PEi386)
501 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
502 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
503 sym = GetProcAddress(o_dll->instance, lbl);
504 if (sym != NULL) return sym;
515 lookupLocalSymbol( ObjectCode* oc, char *lbl )
518 val = lookupStrHashTable(oc->lochash, lbl);
528 /* -----------------------------------------------------------------------------
529 * Load an obj (populate the global symbol table, but don't resolve yet)
531 * Returns: 1 if ok, 0 on error.
534 loadObj( char *path )
541 /* fprintf(stderr, "loadObj %s\n", path ); */
543 /* assert that we haven't already loaded this object */
546 for (o = objects; o; o = o->next)
547 ASSERT(strcmp(o->fileName, path));
551 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
553 # if defined(OBJFORMAT_ELF)
554 oc->formatName = "ELF";
555 # elif defined(OBJFORMAT_PEi386)
556 oc->formatName = "PEi386";
559 barf("loadObj: not implemented on this platform");
563 if (r == -1) { return 0; }
565 /* sigh, strdup() isn't a POSIX function, so do it the long way */
566 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
567 strcpy(oc->fileName, path);
569 oc->fileSize = st.st_size;
570 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
573 oc->lochash = allocStrHashTable();
575 /* chain it onto the list of objects */
579 /* load the image into memory */
580 f = fopen(path, "rb");
582 barf("loadObj: can't read `%s'", path);
584 n = fread ( oc->image, 1, oc->fileSize, f );
585 if (n != oc->fileSize) {
587 barf("loadObj: error whilst reading `%s'", path);
590 /* verify the in-memory image */
591 # if defined(OBJFORMAT_ELF)
592 r = ocVerifyImage_ELF ( oc );
593 # elif defined(OBJFORMAT_PEi386)
594 r = ocVerifyImage_PEi386 ( oc );
596 barf("loadObj: no verify method");
598 if (!r) { return r; }
600 /* build the symbol list for this image */
601 # if defined(OBJFORMAT_ELF)
602 r = ocGetNames_ELF ( oc );
603 # elif defined(OBJFORMAT_PEi386)
604 r = ocGetNames_PEi386 ( oc );
606 barf("loadObj: no getNames method");
608 if (!r) { return r; }
610 /* loaded, but not resolved yet */
611 oc->status = OBJECT_LOADED;
616 /* -----------------------------------------------------------------------------
617 * resolve all the currently unlinked objects in memory
619 * Returns: 1 if ok, 0 on error.
627 for (oc = objects; oc; oc = oc->next) {
628 if (oc->status != OBJECT_RESOLVED) {
629 # if defined(OBJFORMAT_ELF)
630 r = ocResolve_ELF ( oc );
631 # elif defined(OBJFORMAT_PEi386)
632 r = ocResolve_PEi386 ( oc );
634 barf("resolveObjs: not implemented on this platform");
636 if (!r) { return r; }
637 oc->status = OBJECT_RESOLVED;
643 /* -----------------------------------------------------------------------------
644 * delete an object from the pool
647 unloadObj( char *path )
649 ObjectCode *oc, *prev;
651 ASSERT(symhash != NULL);
652 ASSERT(objects != NULL);
655 for (oc = objects; oc; prev = oc, oc = oc->next) {
656 if (!strcmp(oc->fileName,path)) {
658 /* Remove all the mappings for the symbols within this
663 for (i = 0; i < oc->n_symbols; i++) {
664 if (oc->symbols[i] != NULL) {
665 removeStrHashTable(symhash, oc->symbols[i], NULL);
673 prev->next = oc->next;
676 /* We're going to leave this in place, in case there are
677 any pointers from the heap into it: */
678 /* free(oc->image); */
682 /* The local hash table should have been freed at the end
683 of the ocResolve_ call on it. */
684 ASSERT(oc->lochash == NULL);
690 belch("unloadObj: can't find `%s' to unload", path);
694 /* --------------------------------------------------------------------------
695 * PEi386 specifics (Win32 targets)
696 * ------------------------------------------------------------------------*/
698 /* The information for this linker comes from
699 Microsoft Portable Executable
700 and Common Object File Format Specification
701 revision 5.1 January 1998
702 which SimonM says comes from the MS Developer Network CDs.
706 #if defined(OBJFORMAT_PEi386)
710 typedef unsigned char UChar;
711 typedef unsigned short UInt16;
712 typedef unsigned int UInt32;
719 UInt16 NumberOfSections;
720 UInt32 TimeDateStamp;
721 UInt32 PointerToSymbolTable;
722 UInt32 NumberOfSymbols;
723 UInt16 SizeOfOptionalHeader;
724 UInt16 Characteristics;
728 #define sizeof_COFF_header 20
735 UInt32 VirtualAddress;
736 UInt32 SizeOfRawData;
737 UInt32 PointerToRawData;
738 UInt32 PointerToRelocations;
739 UInt32 PointerToLinenumbers;
740 UInt16 NumberOfRelocations;
741 UInt16 NumberOfLineNumbers;
742 UInt32 Characteristics;
746 #define sizeof_COFF_section 40
753 UInt16 SectionNumber;
756 UChar NumberOfAuxSymbols;
760 #define sizeof_COFF_symbol 18
765 UInt32 VirtualAddress;
766 UInt32 SymbolTableIndex;
771 #define sizeof_COFF_reloc 10
774 /* From PE spec doc, section 3.3.2 */
775 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
776 windows.h -- for the same purpose, but I want to know what I'm
778 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
779 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
780 #define MYIMAGE_FILE_DLL 0x2000
781 #define MYIMAGE_FILE_SYSTEM 0x1000
782 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
783 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
784 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
786 /* From PE spec doc, section 5.4.2 and 5.4.4 */
787 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
788 #define MYIMAGE_SYM_CLASS_STATIC 3
789 #define MYIMAGE_SYM_UNDEFINED 0
791 /* From PE spec doc, section 4.1 */
792 #define MYIMAGE_SCN_CNT_CODE 0x00000020
793 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
795 /* From PE spec doc, section 5.2.1 */
796 #define MYIMAGE_REL_I386_DIR32 0x0006
797 #define MYIMAGE_REL_I386_REL32 0x0014
800 /* We use myindex to calculate array addresses, rather than
801 simply doing the normal subscript thing. That's because
802 some of the above structs have sizes which are not
803 a whole number of words. GCC rounds their sizes up to a
804 whole number of words, which means that the address calcs
805 arising from using normal C indexing or pointer arithmetic
806 are just plain wrong. Sigh.
809 myindex ( int scale, void* base, int index )
812 ((UChar*)base) + scale * index;
817 printName ( UChar* name, UChar* strtab )
819 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
820 UInt32 strtab_offset = * (UInt32*)(name+4);
821 fprintf ( stderr, "%s", strtab + strtab_offset );
824 for (i = 0; i < 8; i++) {
825 if (name[i] == 0) break;
826 fprintf ( stderr, "%c", name[i] );
833 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
835 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
836 UInt32 strtab_offset = * (UInt32*)(name+4);
837 strncpy ( dst, strtab+strtab_offset, dstSize );
843 if (name[i] == 0) break;
853 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
856 /* If the string is longer than 8 bytes, look in the
857 string table for it -- this will be correctly zero terminated.
859 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
860 UInt32 strtab_offset = * (UInt32*)(name+4);
861 return ((UChar*)strtab) + strtab_offset;
863 /* Otherwise, if shorter than 8 bytes, return the original,
864 which by defn is correctly terminated.
866 if (name[7]==0) return name;
867 /* The annoying case: 8 bytes. Copy into a temporary
868 (which is never freed ...)
870 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
872 strncpy(newstr,name,8);
878 /* Just compares the short names (first 8 chars) */
879 static COFF_section *
880 findPEi386SectionCalled ( ObjectCode* oc, char* name )
884 = (COFF_header*)(oc->image);
887 ((UChar*)(oc->image))
888 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
890 for (i = 0; i < hdr->NumberOfSections; i++) {
893 COFF_section* section_i
895 myindex ( sizeof_COFF_section, sectab, i );
896 n1 = (UChar*) &(section_i->Name);
898 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
899 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
900 n1[6]==n2[6] && n1[7]==n2[7])
909 zapTrailingAtSign ( UChar* sym )
911 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
913 if (sym[0] == 0) return;
915 while (sym[i] != 0) i++;
918 while (j > 0 && my_isdigit(sym[j])) j--;
919 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
925 ocVerifyImage_PEi386 ( ObjectCode* oc )
929 COFF_section* sectab;
932 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
933 hdr = (COFF_header*)(oc->image);
934 sectab = (COFF_section*) (
935 ((UChar*)(oc->image))
936 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
938 symtab = (COFF_symbol*) (
939 ((UChar*)(oc->image))
940 + hdr->PointerToSymbolTable
942 strtab = ((UChar*)symtab)
943 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
945 if (hdr->Machine != 0x14c) {
946 belch("Not x86 PEi386");
949 if (hdr->SizeOfOptionalHeader != 0) {
950 belch("PEi386 with nonempty optional header");
953 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
954 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
955 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
956 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
957 belch("Not a PEi386 object file");
960 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
961 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
962 belch("Invalid PEi386 word size or endiannness: %d",
963 (int)(hdr->Characteristics));
966 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
967 if (* (UInt32*)strtab > 510000) {
968 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
972 /* No further verification after this point; only debug printing. */
974 IF_DEBUG(linker, i=1);
975 if (i == 0) return 1;
978 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
980 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
982 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
984 fprintf ( stderr, "\n" );
986 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
988 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
990 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
992 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
994 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
996 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
998 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1000 /* Print the section table. */
1001 fprintf ( stderr, "\n" );
1002 for (i = 0; i < hdr->NumberOfSections; i++) {
1004 COFF_section* sectab_i
1006 myindex ( sizeof_COFF_section, sectab, i );
1013 printName ( sectab_i->Name, strtab );
1022 sectab_i->VirtualSize,
1023 sectab_i->VirtualAddress,
1024 sectab_i->SizeOfRawData,
1025 sectab_i->PointerToRawData,
1026 sectab_i->NumberOfRelocations,
1027 sectab_i->PointerToRelocations
1029 reltab = (COFF_reloc*) (
1030 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1033 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1035 COFF_reloc* rel = (COFF_reloc*)
1036 myindex ( sizeof_COFF_reloc, reltab, j );
1038 " type 0x%-4x vaddr 0x%-8x name `",
1040 rel->VirtualAddress );
1041 sym = (COFF_symbol*)
1042 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1043 printName ( sym->Name, strtab -10 );
1044 fprintf ( stderr, "'\n" );
1046 fprintf ( stderr, "\n" );
1049 fprintf ( stderr, "\n" );
1050 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1051 fprintf ( stderr, "---START of string table---\n");
1052 for (i = 4; i < *(Int32*)strtab; i++) {
1054 fprintf ( stderr, "\n"); else
1055 fprintf( stderr, "%c", strtab[i] );
1057 fprintf ( stderr, "--- END of string table---\n");
1059 fprintf ( stderr, "\n" );
1062 COFF_symbol* symtab_i;
1063 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1064 symtab_i = (COFF_symbol*)
1065 myindex ( sizeof_COFF_symbol, symtab, i );
1071 printName ( symtab_i->Name, strtab );
1080 (Int32)(symtab_i->SectionNumber) - 1,
1081 (UInt32)symtab_i->Type,
1082 (UInt32)symtab_i->StorageClass,
1083 (UInt32)symtab_i->NumberOfAuxSymbols
1085 i += symtab_i->NumberOfAuxSymbols;
1089 fprintf ( stderr, "\n" );
1095 ocGetNames_PEi386 ( ObjectCode* oc )
1098 COFF_section* sectab;
1099 COFF_symbol* symtab;
1106 hdr = (COFF_header*)(oc->image);
1107 sectab = (COFF_section*) (
1108 ((UChar*)(oc->image))
1109 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1111 symtab = (COFF_symbol*) (
1112 ((UChar*)(oc->image))
1113 + hdr->PointerToSymbolTable
1115 strtab = ((UChar*)(oc->image))
1116 + hdr->PointerToSymbolTable
1117 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1119 /* Copy exported symbols into the ObjectCode. */
1121 oc->n_symbols = hdr->NumberOfSymbols;
1122 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1123 "ocGetNames_PEi386(oc->symbols)");
1124 /* Call me paranoid; I don't care. */
1125 for (i = 0; i < oc->n_symbols; i++)
1126 oc->symbols[i] = NULL;
1130 COFF_symbol* symtab_i;
1131 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1132 symtab_i = (COFF_symbol*)
1133 myindex ( sizeof_COFF_symbol, symtab, i );
1135 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL &&
1136 symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1138 /* This symbol is global and defined, viz, exported */
1139 COFF_section* sectabent;
1141 /* cstring_from_COFF_symbol_name always succeeds. */
1142 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1144 /* for MYIMAGE_SYMCLASS_EXTERNAL
1145 && !MYIMAGE_SYM_UNDEFINED,
1146 the address of the symbol is:
1147 address of relevant section + offset in section
1149 sectabent = (COFF_section*)
1150 myindex ( sizeof_COFF_section,
1152 symtab_i->SectionNumber-1 );
1153 addr = ((UChar*)(oc->image))
1154 + (sectabent->PointerToRawData
1156 /* fprintf(stderr,"addSymbol %p `%s'\n", addr,sname); */
1157 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1158 ASSERT(i >= 0 && i < oc->n_symbols);
1159 oc->symbols[i] = sname;
1160 insertStrHashTable(symhash, sname, addr);
1162 i += symtab_i->NumberOfAuxSymbols;
1166 /* Copy section information into the ObjectCode. */
1168 oc->n_sections = hdr->NumberOfSections;
1169 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1170 "ocGetNamesPEi386" );
1172 for (i = 0; i < oc->n_sections; i++) {
1177 = SECTIONKIND_OTHER;
1178 COFF_section* sectab_i
1180 myindex ( sizeof_COFF_section, sectab, i );
1181 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1184 /* I'm sure this is the Right Way to do it. However, the
1185 alternative of testing the sectab_i->Name field seems to
1186 work ok with Cygwin.
1188 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1189 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1190 kind = SECTIONKIND_CODE_OR_RODATA;
1193 if (0==strcmp(".text",sectab_i->Name))
1194 kind = SECTIONKIND_CODE_OR_RODATA;
1195 if (0==strcmp(".data",sectab_i->Name) ||
1196 0==strcmp(".bss",sectab_i->Name))
1197 kind = SECTIONKIND_RWDATA;
1199 start = ((UChar*)(oc->image))
1200 + sectab_i->PointerToRawData;
1202 + sectab_i->SizeOfRawData - 1;
1204 if (kind == SECTIONKIND_OTHER) {
1205 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1209 oc->sections[i].start = start;
1210 oc->sections[i].end = end;
1211 oc->sections[i].kind = kind;
1219 ocResolve_PEi386 ( ObjectCode* oc )
1222 COFF_section* sectab;
1223 COFF_symbol* symtab;
1232 /* ToDo: should be variable-sized? But is at least safe in the
1233 sense of buffer-overrun-proof. */
1235 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1237 hdr = (COFF_header*)(oc->image);
1238 sectab = (COFF_section*) (
1239 ((UChar*)(oc->image))
1240 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1242 symtab = (COFF_symbol*) (
1243 ((UChar*)(oc->image))
1244 + hdr->PointerToSymbolTable
1246 strtab = ((UChar*)(oc->image))
1247 + hdr->PointerToSymbolTable
1248 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1250 for (i = 0; i < hdr->NumberOfSections; i++) {
1251 COFF_section* sectab_i
1253 myindex ( sizeof_COFF_section, sectab, i );
1256 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1258 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1260 COFF_reloc* reltab_j
1262 myindex ( sizeof_COFF_reloc, reltab, j );
1264 /* the location to patch */
1266 ((UChar*)(oc->image))
1267 + (sectab_i->PointerToRawData
1268 + reltab_j->VirtualAddress
1269 - sectab_i->VirtualAddress )
1271 /* the existing contents of pP */
1273 /* the symbol to connect to */
1274 sym = (COFF_symbol*)
1275 myindex ( sizeof_COFF_symbol,
1276 symtab, reltab_j->SymbolTableIndex );
1279 "reloc sec %2d num %3d: type 0x%-4x "
1280 "vaddr 0x%-8x name `",
1282 (UInt32)reltab_j->Type,
1283 reltab_j->VirtualAddress );
1284 printName ( sym->Name, strtab );
1285 fprintf ( stderr, "'\n" ));
1287 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1288 COFF_section* section_sym
1289 = findPEi386SectionCalled ( oc, sym->Name );
1291 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
1292 barf("Can't find abovementioned PEi386 section");
1295 S = ((UInt32)(oc->image))
1296 + (section_sym->PointerToRawData
1299 copyName ( sym->Name, strtab, symbol, 1000-1 );
1300 zapTrailingAtSign ( symbol );
1301 (void*)S = lookupLocalSymbol( oc, symbol );
1302 if ((void*)S == NULL)
1303 (void*)S = lookupSymbol( symbol );
1305 belch("ocResolve_PEi386: %s: unknown symbol `%s'",
1306 oc->fileName, symbol);
1311 switch (reltab_j->Type) {
1312 case MYIMAGE_REL_I386_DIR32:
1315 case MYIMAGE_REL_I386_REL32:
1316 /* Tricky. We have to insert a displacement at
1317 pP which, when added to the PC for the _next_
1318 insn, gives the address of the target (S).
1319 Problem is to know the address of the next insn
1320 when we only know pP. We assume that this
1321 literal field is always the last in the insn,
1322 so that the address of the next insn is pP+4
1323 -- hence the constant 4.
1324 Also I don't know if A should be added, but so
1325 far it has always been zero.
1328 *pP = S - ((UInt32)pP) - 4;
1332 "unhandled PEi386 relocation type %d\n",
1334 barf("unhandled PEi386 relocation type");
1341 /* fprintf(stderr, "completed %s\n", oc->fileName); */
1345 #endif /* defined(OBJFORMAT_PEi386) */
1348 /* --------------------------------------------------------------------------
1350 * ------------------------------------------------------------------------*/
1352 #if defined(OBJFORMAT_ELF)
1357 #if defined(sparc_TARGET_ARCH)
1358 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1364 findElfSection ( void* objImage, Elf32_Word sh_type )
1367 char* ehdrC = (char*)objImage;
1368 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1369 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1371 for (i = 0; i < ehdr->e_shnum; i++) {
1372 if (shdr[i].sh_type == sh_type &&
1373 i != ehdr->e_shstrndx) {
1374 ptr = ehdrC + shdr[i].sh_offset;
1383 ocVerifyImage_ELF ( ObjectCode* oc )
1387 int i, j, nent, nstrtab, nsymtabs;
1391 char* ehdrC = (char*)(oc->image);
1392 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1394 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1395 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1396 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1397 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1398 belch("ocVerifyImage_ELF: not an ELF header");
1401 IF_DEBUG(linker,belch( "Is an ELF header" ));
1403 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1404 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1408 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1410 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1411 IF_DEBUG(linker,belch( "Is little-endian" ));
1413 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1414 IF_DEBUG(linker,belch( "Is big-endian" ));
1416 belch("ocVerifyImage_ELF: unknown endiannness");
1420 if (ehdr->e_type != ET_REL) {
1421 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1424 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1426 IF_DEBUG(linker,belch( "Architecture is " ));
1427 switch (ehdr->e_machine) {
1428 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1429 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1430 default: IF_DEBUG(linker,belch( "unknown" ));
1431 belch("ocVerifyImage_ELF: unknown architecture");
1435 IF_DEBUG(linker,belch(
1436 "\nSection header table: start %d, n_entries %d, ent_size %d",
1437 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1439 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1441 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1443 if (ehdr->e_shstrndx == SHN_UNDEF) {
1444 belch("ocVerifyImage_ELF: no section header string table");
1447 IF_DEBUG(linker,belch( "Section header string table is section %d",
1449 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1452 for (i = 0; i < ehdr->e_shnum; i++) {
1453 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1454 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1455 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1456 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1457 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1458 ehdrC + shdr[i].sh_offset,
1459 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1461 if (shdr[i].sh_type == SHT_REL) {
1462 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1463 } else if (shdr[i].sh_type == SHT_RELA) {
1464 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1466 IF_DEBUG(linker,fprintf(stderr," "));
1469 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1473 IF_DEBUG(linker,belch( "\nString tables" ));
1476 for (i = 0; i < ehdr->e_shnum; i++) {
1477 if (shdr[i].sh_type == SHT_STRTAB &&
1478 i != ehdr->e_shstrndx) {
1479 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1480 strtab = ehdrC + shdr[i].sh_offset;
1485 belch("ocVerifyImage_ELF: no string tables, or too many");
1490 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1491 for (i = 0; i < ehdr->e_shnum; i++) {
1492 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1493 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1495 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1496 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1497 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1499 shdr[i].sh_size % sizeof(Elf32_Sym)
1501 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1502 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1505 for (j = 0; j < nent; j++) {
1506 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1507 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1508 (int)stab[j].st_shndx,
1509 (int)stab[j].st_size,
1510 (char*)stab[j].st_value ));
1512 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1513 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1514 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1515 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1516 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1517 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1518 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1519 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1521 IF_DEBUG(linker,fprintf(stderr, " " ));
1523 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1524 switch (ELF32_ST_BIND(stab[j].st_info)) {
1525 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1526 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1527 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1528 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1530 IF_DEBUG(linker,fprintf(stderr, " " ));
1532 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1536 if (nsymtabs == 0) {
1537 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1546 ocGetNames_ELF ( ObjectCode* oc )
1551 char* ehdrC = (char*)(oc->image);
1552 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1553 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1554 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1555 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1557 ASSERT(symhash != NULL);
1560 belch("ocGetNames_ELF: no strtab");
1565 oc->n_sections = ehdr->e_shnum;
1566 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1567 "ocGetNames_ELF(oc->sections)" );
1569 for (i = 0; i < oc->n_sections; i++) {
1571 /* make a section entry for relevant sections */
1572 SectionKind kind = SECTIONKIND_OTHER;
1573 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1574 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1575 kind = SECTIONKIND_RWDATA;
1576 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1577 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1578 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1579 kind = SECTIONKIND_CODE_OR_RODATA;
1581 /* fill in the section info */
1582 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1583 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1584 oc->sections[i].kind = kind;
1586 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1588 /* copy stuff into this module's object symbol table */
1589 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1590 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1592 oc->n_symbols = nent;
1593 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1594 "ocGetNames_ELF(oc->symbols)");
1596 for (j = 0; j < nent; j++) {
1597 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1598 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1600 /* and not an undefined symbol */
1601 && stab[j].st_shndx != SHN_UNDEF
1602 /* and not in a "special section" */
1603 && stab[j].st_shndx < SHN_LORESERVE
1605 /* and it's a not a section or string table or anything silly */
1606 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1607 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1608 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1611 char* nm = strtab + stab[j].st_name;
1613 + shdr[ stab[j].st_shndx ].sh_offset
1617 oc->symbols[j] = nm;
1618 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1619 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1620 ad, oc->fileName, nm ));
1621 insertStrHashTable(oc->lochash, nm, ad);
1623 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1624 ad, oc->fileName, nm ));
1625 insertStrHashTable(symhash, nm, ad);
1629 IF_DEBUG(linker,belch( "skipping `%s'",
1630 strtab + stab[j].st_name ));
1633 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1634 (int)ELF32_ST_BIND(stab[j].st_info),
1635 (int)ELF32_ST_TYPE(stab[j].st_info),
1636 (int)stab[j].st_shndx,
1637 strtab + stab[j].st_name
1640 oc->symbols[j] = NULL;
1649 /* Do ELF relocations which lack an explicit addend. All x86-linux
1650 relocations appear to be of this form. */
1651 static int do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1652 Elf32_Shdr* shdr, int shnum,
1653 Elf32_Sym* stab, char* strtab )
1658 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1659 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1660 int target_shndx = shdr[shnum].sh_info;
1661 int symtab_shndx = shdr[shnum].sh_link;
1662 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1663 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1664 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1665 target_shndx, symtab_shndx ));
1666 for (j = 0; j < nent; j++) {
1667 Elf32_Addr offset = rtab[j].r_offset;
1668 Elf32_Word info = rtab[j].r_info;
1670 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1671 Elf32_Word* pP = (Elf32_Word*)P;
1675 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1676 j, (void*)offset, (void*)info ));
1678 IF_DEBUG(linker,belch( " ZERO" ));
1681 /* First see if it is a nameless local symbol. */
1682 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1683 symbol = "(noname)";
1685 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1686 + stab[ELF32_R_SYM(info)].st_value);
1688 /* No? Should be in a symbol table then; first try the
1690 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1691 (void*)S = lookupLocalSymbol( oc, symbol );
1692 if ((void*)S == NULL)
1693 (void*)S = lookupSymbol( symbol );
1696 barf("do_Elf32_Rel_relocations: %s: unknown symbol `%s'",
1697 oc->fileName, symbol);
1699 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1701 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1702 (void*)P, (void*)S, (void*)A ));
1703 switch (ELF32_R_TYPE(info)) {
1704 # ifdef i386_TARGET_ARCH
1705 case R_386_32: *pP = S + A; break;
1706 case R_386_PC32: *pP = S + A - P; break;
1709 fprintf(stderr, "unhandled ELF relocation(Rel) type %d\n",
1710 ELF32_R_TYPE(info));
1711 barf("do_Elf32_Rel_relocations: unhandled ELF relocation type");
1720 /* Do ELF relocations for which explicit addends are supplied.
1721 sparc-solaris relocations appear to be of this form. */
1722 static int do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1723 Elf32_Shdr* shdr, int shnum,
1724 Elf32_Sym* stab, char* strtab )
1729 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1730 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1731 int target_shndx = shdr[shnum].sh_info;
1732 int symtab_shndx = shdr[shnum].sh_link;
1733 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1734 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1735 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1736 target_shndx, symtab_shndx ));
1737 for (j = 0; j < nent; j++) {
1738 Elf32_Addr offset = rtab[j].r_offset;
1739 Elf32_Word info = rtab[j].r_info;
1740 Elf32_Sword addend = rtab[j].r_addend;
1742 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1743 Elf32_Addr A = addend;
1745 # if defined(sparc_TARGET_ARCH)
1746 /* This #ifdef only serves to avoid unused-var warnings. */
1747 Elf32_Word* pP = (Elf32_Word*)P;
1751 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1752 j, (void*)offset, (void*)info,
1755 IF_DEBUG(linker,belch( " ZERO" ));
1758 /* First see if it is a nameless local symbol. */
1759 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1760 symbol = "(noname)";
1762 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1763 + stab[ELF32_R_SYM(info)].st_value);
1765 /* No? Should be in a symbol table then; first try the
1767 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1768 (void*)S = lookupLocalSymbol( oc, symbol );
1769 if ((void*)S == NULL)
1770 (void*)S = lookupSymbol( symbol );
1773 barf("do_Elf32_Rela_relocations: %s: unknown symbol `%s'",
1774 oc->fileName, symbol);
1777 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1780 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1782 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1783 (void*)P, (void*)S, (void*)A ));
1784 switch (ELF32_R_TYPE(info)) {
1785 # if defined(sparc_TARGET_ARCH)
1786 case R_SPARC_WDISP30:
1787 w1 = *pP & 0xC0000000;
1788 w2 = (Elf32_Word)((S + A - P) >> 2);
1789 ASSERT((w2 & 0xC0000000) == 0);
1794 w1 = *pP & 0xFFC00000;
1795 w2 = (Elf32_Word)((S + A) >> 10);
1796 ASSERT((w2 & 0xFFC00000) == 0);
1802 w2 = (Elf32_Word)((S + A) & 0x3FF);
1803 ASSERT((w2 & ~0x3FF) == 0);
1808 w2 = (Elf32_Word)(S + A);
1813 fprintf(stderr, "unhandled ELF relocation(RelA) type %d\n",
1814 ELF32_R_TYPE(info));
1815 barf("do_Elf32_Rela_relocations: unhandled ELF relocation type");
1825 ocResolve_ELF ( ObjectCode* oc )
1829 Elf32_Sym* stab = NULL;
1830 char* ehdrC = (char*)(oc->image);
1831 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1832 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1834 /* first find "the" symbol table */
1835 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1837 /* also go find the string table */
1838 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1840 if (stab == NULL || strtab == NULL) {
1841 belch("ocResolve_ELF: can't find string or symbol table");
1845 /* Process the relocation sections. */
1846 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
1847 if (shdr[shnum].sh_type == SHT_REL ) {
1848 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
1849 shnum, stab, strtab );
1853 if (shdr[shnum].sh_type == SHT_RELA) {
1854 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
1855 shnum, stab, strtab );
1860 /* Free the local symbol table; we won't need it again. */
1861 freeHashTable(oc->lochash, NULL);