1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.39 2001/04/24 15:49:19 qrczak 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
37 /* Hash table mapping symbol names to Symbol */
38 /*Str*/HashTable *symhash;
40 #if defined(OBJFORMAT_ELF)
41 static int ocVerifyImage_ELF ( ObjectCode* oc );
42 static int ocGetNames_ELF ( ObjectCode* oc );
43 static int ocResolve_ELF ( ObjectCode* oc );
44 #elif defined(OBJFORMAT_PEi386)
45 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
46 static int ocGetNames_PEi386 ( ObjectCode* oc );
47 static int ocResolve_PEi386 ( ObjectCode* oc );
50 /* -----------------------------------------------------------------------------
51 * Built-in symbols from the RTS
54 typedef struct _RtsSymbolVal {
61 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
63 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
64 SymX(makeStableNamezh_fast) \
65 SymX(finalizzeWeakzh_fast)
67 /* These are not available in GUM!!! -- HWL */
68 #define Maybe_ForeignObj
69 #define Maybe_Stable_Names
72 #if !defined (mingw32_TARGET_OS)
73 #define RTS_POSIX_ONLY_SYMBOLS \
74 SymX(stg_sig_install) \
77 #define RTS_POSIX_ONLY_SYMBOLS
94 SymX(stg_update_PAP) \
95 SymX(stg_ap_1_upd_info) \
96 SymX(stg_ap_2_upd_info) \
97 SymX(stg_ap_3_upd_info) \
98 SymX(stg_ap_4_upd_info) \
99 SymX(stg_ap_5_upd_info) \
100 SymX(stg_ap_6_upd_info) \
101 SymX(stg_ap_7_upd_info) \
102 SymX(stg_ap_8_upd_info) \
103 SymX(stg_sel_0_upd_info) \
104 SymX(stg_sel_1_upd_info) \
105 SymX(stg_sel_2_upd_info) \
106 SymX(stg_sel_3_upd_info) \
107 SymX(stg_sel_4_upd_info) \
108 SymX(stg_sel_5_upd_info) \
109 SymX(stg_sel_6_upd_info) \
110 SymX(stg_sel_7_upd_info) \
111 SymX(stg_sel_8_upd_info) \
112 SymX(stg_sel_9_upd_info) \
113 SymX(stg_sel_10_upd_info) \
114 SymX(stg_sel_11_upd_info) \
115 SymX(stg_sel_12_upd_info) \
116 SymX(stg_sel_13_upd_info) \
117 SymX(stg_sel_14_upd_info) \
118 SymX(stg_sel_15_upd_info) \
119 SymX(stg_upd_frame_info) \
120 SymX(stg_seq_frame_info) \
121 SymX(stg_CAF_BLACKHOLE_info) \
122 SymX(stg_IND_STATIC_info) \
123 SymX(stg_EMPTY_MVAR_info) \
124 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
125 SymX(stg_WEAK_info) \
126 SymX(stg_CHARLIKE_closure) \
127 SymX(stg_INTLIKE_closure) \
129 SymX(newBCOzh_fast) \
130 SymX(mkApUpd0zh_fast) \
131 SymX(putMVarzh_fast) \
132 SymX(newMVarzh_fast) \
133 SymX(takeMVarzh_fast) \
134 SymX(tryTakeMVarzh_fast) \
135 SymX(tryPutMVarzh_fast) \
141 SymX(killThreadzh_fast) \
142 SymX(waitReadzh_fast) \
143 SymX(waitWritezh_fast) \
144 SymX(suspendThread) \
146 SymX(stackOverflow) \
147 SymX(int2Integerzh_fast) \
148 SymX(word2Integerzh_fast) \
150 SymX(__encodeDouble) \
151 SymX(decodeDoublezh_fast) \
152 SymX(decodeFloatzh_fast) \
153 SymX(gcdIntegerzh_fast) \
154 SymX(newArrayzh_fast) \
155 SymX(unsafeThawArrayzh_fast) \
156 SymX(newByteArrayzh_fast) \
157 SymX(newMutVarzh_fast) \
158 SymX(quotRemIntegerzh_fast) \
159 SymX(quotIntegerzh_fast) \
160 SymX(remIntegerzh_fast) \
161 SymX(divExactIntegerzh_fast) \
162 SymX(divModIntegerzh_fast) \
163 SymX(timesIntegerzh_fast) \
164 SymX(minusIntegerzh_fast) \
165 SymX(plusIntegerzh_fast) \
166 SymX(andIntegerzh_fast) \
167 SymX(orIntegerzh_fast) \
168 SymX(xorIntegerzh_fast) \
169 SymX(complementIntegerzh_fast) \
171 SymX(blockAsyncExceptionszh_fast) \
172 SymX(unblockAsyncExceptionszh_fast) \
174 SymX(isDoubleInfinite) \
175 SymX(isDoubleDenormalized) \
176 SymX(isDoubleNegativeZero) \
177 SymX(__encodeFloat) \
179 SymX(isFloatInfinite) \
180 SymX(isFloatDenormalized) \
181 SymX(isFloatNegativeZero) \
182 SymX(__int_encodeFloat) \
183 SymX(__int_encodeDouble) \
184 SymX(__gmpz_cmp_si) \
185 SymX(__gmpz_cmp_ui) \
188 SymX(__gmpz_get_si) \
189 SymX(__gmpz_get_ui) \
192 SymX(resetNonBlockingFd) \
195 SymX(stable_ptr_table) \
196 SymX(shutdownHaskellAndExit) \
197 Sym(stg_enterStackTop) \
198 Sym(stg_yield_to_interpreter) \
202 Sym(__init_PrelGHC) \
203 SymX(freeHaskellFunctionPtr) \
206 SymX(NoRunnableThreadsHook) \
207 SymX(StackOverflowHook) \
208 SymX(OutOfHeapHook) \
209 SymX(MallocFailHook) \
210 SymX(PatErrorHdrHook) \
213 SymX(PostTraceHook) \
214 SymX(createAdjustor) \
229 SymX(rts_mkStablePtr) \
238 SymX(rts_getWord32) \
241 SymX(rts_getDouble) \
242 SymX(rts_getStablePtr) \
248 SymX(rts_evalLazyIO) \
249 SymX(rts_checkSchedStatus)
251 #ifndef SUPPORT_LONG_LONGS
252 #define RTS_LONG_LONG_SYMS /* nothing */
254 #define RTS_LONG_LONG_SYMS \
267 SymX(stg_remWord64) \
268 SymX(stg_quotWord64) \
270 SymX(stg_quotInt64) \
271 SymX(stg_negateInt64) \
272 SymX(stg_plusInt64) \
273 SymX(stg_minusInt64) \
274 SymX(stg_timesInt64) \
280 SymX(stg_shiftRL64) \
281 SymX(stg_iShiftL64) \
282 SymX(stg_iShiftRL64) \
283 SymX(stg_iShiftRA64) \
284 SymX(stg_intToInt64) \
285 SymX(stg_int64ToInt) \
286 SymX(stg_int64ToWord64) \
287 SymX(stg_wordToWord64) \
288 SymX(stg_word64ToWord) \
289 SymX(stg_word64ToInt64) \
290 SymX(int64ToIntegerzh_fast) \
291 SymX(word64ToIntegerzh_fast)
292 #endif /* SUPPORT_LONG_LONGS */
294 /* entirely bogus claims about types of these symbols */
295 #define Sym(vvv) extern void (vvv);
296 #define SymX(vvv) /**/
298 RTS_POSIX_ONLY_SYMBOLS
302 #ifdef LEADING_UNDERSCORE
303 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
305 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
308 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
310 #define SymX(vvv) Sym(vvv)
312 static RtsSymbolVal rtsSyms[] = {
315 RTS_POSIX_ONLY_SYMBOLS
316 { 0, 0 } /* sentinel */
319 /* -----------------------------------------------------------------------------
320 * initialize the object linker
322 #if defined(OBJFORMAT_ELF)
323 static void *dl_prog_handle;
331 symhash = allocStrHashTable();
333 /* populate the symbol table with stuff from the RTS */
334 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
335 insertStrHashTable(symhash, sym->lbl, sym->addr);
337 # if defined(OBJFORMAT_ELF)
338 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
342 /* -----------------------------------------------------------------------------
343 * Add a DLL from which symbols may be found. In the ELF case, just
344 * do RTLD_GLOBAL-style add, so no further messing around needs to
345 * happen in order that symbols in the loaded .so are findable --
346 * lookupSymbol() will subsequently see them by dlsym on the program's
347 * dl-handle. Returns 0 if fail, 1 if success.
350 addDLL ( char* dll_name )
352 # if defined(OBJFORMAT_ELF)
357 buf = stgMallocBytes(strlen(dll_name) + 10, "addDll");
358 sprintf(buf, "lib%s.so", dll_name);
359 hdl = dlopen(buf, RTLD_NOW | RTLD_GLOBAL );
362 /* dlopen failed; return a ptr to the error msg. */
364 if (errmsg == NULL) errmsg = "addDLL: unknown error";
369 ASSERT(0); /*NOTREACHED*/
370 # elif defined(OBJFORMAT_PEi386)
371 barf("addDLL: not implemented on PEi386 yet");
374 barf("addDLL: not implemented on this platform");
378 /* -----------------------------------------------------------------------------
379 * lookup a symbol in the hash table
382 lookupSymbol( char *lbl )
385 ASSERT(symhash != NULL);
386 val = lookupStrHashTable(symhash, lbl);
389 # if defined(OBJFORMAT_ELF)
390 return dlsym(dl_prog_handle, lbl);
391 # elif defined(OBJFORMAT_PEi386)
402 lookupLocalSymbol( ObjectCode* oc, char *lbl )
405 val = lookupStrHashTable(oc->lochash, lbl);
415 /* -----------------------------------------------------------------------------
416 * Load an obj (populate the global symbol table, but don't resolve yet)
418 * Returns: 1 if ok, 0 on error.
421 loadObj( char *path )
429 /* assert that we haven't already loaded this object */
432 for (o = objects; o; o = o->next)
433 ASSERT(strcmp(o->fileName, path));
437 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
439 # if defined(OBJFORMAT_ELF)
440 oc->formatName = "ELF";
441 # elif defined(OBJFORMAT_PEi386)
442 oc->formatName = "PEi386";
445 barf("loadObj: not implemented on this platform");
449 if (r == -1) { return 0; }
451 /* sigh, strdup() isn't a POSIX function, so do it the long way */
452 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
453 strcpy(oc->fileName, path);
455 oc->fileSize = st.st_size;
456 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
459 oc->lochash = allocStrHashTable();
461 /* chain it onto the list of objects */
465 /* load the image into memory */
466 f = fopen(path, "rb");
468 barf("loadObj: can't read `%s'", path);
470 n = fread ( oc->image, 1, oc->fileSize, f );
471 if (n != oc->fileSize) {
473 barf("loadObj: error whilst reading `%s'", path);
476 /* verify the in-memory image */
477 # if defined(OBJFORMAT_ELF)
478 r = ocVerifyImage_ELF ( oc );
479 # elif defined(OBJFORMAT_PEi386)
480 r = ocVerifyImage_PEi386 ( oc );
482 barf("loadObj: no verify method");
484 if (!r) { return r; }
486 /* build the symbol list for this image */
487 # if defined(OBJFORMAT_ELF)
488 r = ocGetNames_ELF ( oc );
489 # elif defined(OBJFORMAT_PEi386)
490 r = ocGetNames_PEi386 ( oc );
492 barf("loadObj: no getNames method");
494 if (!r) { return r; }
496 /* loaded, but not resolved yet */
497 oc->status = OBJECT_LOADED;
502 /* -----------------------------------------------------------------------------
503 * resolve all the currently unlinked objects in memory
505 * Returns: 1 if ok, 0 on error.
513 for (oc = objects; oc; oc = oc->next) {
514 if (oc->status != OBJECT_RESOLVED) {
515 # if defined(OBJFORMAT_ELF)
516 r = ocResolve_ELF ( oc );
517 # elif defined(OBJFORMAT_PEi386)
518 r = ocResolve_PEi386 ( oc );
520 barf("resolveObjs: not implemented on this platform");
522 if (!r) { return r; }
523 oc->status = OBJECT_RESOLVED;
529 /* -----------------------------------------------------------------------------
530 * delete an object from the pool
533 unloadObj( char *path )
535 ObjectCode *oc, *prev;
537 ASSERT(symhash != NULL);
538 ASSERT(objects != NULL);
541 for (oc = objects; oc; prev = oc, oc = oc->next) {
542 if (!strcmp(oc->fileName,path)) {
544 /* Remove all the mappings for the symbols within this
549 for (i = 0; i < oc->n_symbols; i++) {
550 if (oc->symbols[i] != NULL) {
551 removeStrHashTable(symhash, oc->symbols[i], NULL);
559 prev->next = oc->next;
562 /* We're going to leave this in place, in case there are
563 any pointers from the heap into it: */
564 /* free(oc->image); */
568 /* The local hash table should have been freed at the end
569 of the ocResolve_ call on it. */
570 ASSERT(oc->lochash == NULL);
576 belch("unloadObj: can't find `%s' to unload", path);
580 /* --------------------------------------------------------------------------
581 * PEi386 specifics (Win32 targets)
582 * ------------------------------------------------------------------------*/
584 /* The information for this linker comes from
585 Microsoft Portable Executable
586 and Common Object File Format Specification
587 revision 5.1 January 1998
588 which SimonM says comes from the MS Developer Network CDs.
592 #if defined(OBJFORMAT_PEi386)
596 typedef unsigned char UChar;
597 typedef unsigned short UInt16;
598 typedef unsigned int UInt32;
605 UInt16 NumberOfSections;
606 UInt32 TimeDateStamp;
607 UInt32 PointerToSymbolTable;
608 UInt32 NumberOfSymbols;
609 UInt16 SizeOfOptionalHeader;
610 UInt16 Characteristics;
614 #define sizeof_COFF_header 20
621 UInt32 VirtualAddress;
622 UInt32 SizeOfRawData;
623 UInt32 PointerToRawData;
624 UInt32 PointerToRelocations;
625 UInt32 PointerToLinenumbers;
626 UInt16 NumberOfRelocations;
627 UInt16 NumberOfLineNumbers;
628 UInt32 Characteristics;
632 #define sizeof_COFF_section 40
639 UInt16 SectionNumber;
642 UChar NumberOfAuxSymbols;
646 #define sizeof_COFF_symbol 18
651 UInt32 VirtualAddress;
652 UInt32 SymbolTableIndex;
657 #define sizeof_COFF_reloc 10
660 /* From PE spec doc, section 3.3.2 */
661 #define IMAGE_FILE_RELOCS_STRIPPED 0x0001
662 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
663 #define IMAGE_FILE_DLL 0x2000
664 #define IMAGE_FILE_SYSTEM 0x1000
665 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
666 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
667 #define IMAGE_FILE_32BIT_MACHINE 0x0100
669 /* From PE spec doc, section 5.4.2 and 5.4.4 */
670 #define IMAGE_SYM_CLASS_EXTERNAL 2
671 #define IMAGE_SYM_CLASS_STATIC 3
672 #define IMAGE_SYM_UNDEFINED 0
674 /* From PE spec doc, section 4.1 */
675 #define IMAGE_SCN_CNT_CODE 0x00000020
676 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
678 /* From PE spec doc, section 5.2.1 */
679 #define IMAGE_REL_I386_DIR32 0x0006
680 #define IMAGE_REL_I386_REL32 0x0014
683 /* We use myindex to calculate array addresses, rather than
684 simply doing the normal subscript thing. That's because
685 some of the above structs have sizes which are not
686 a whole number of words. GCC rounds their sizes up to a
687 whole number of words, which means that the address calcs
688 arising from using normal C indexing or pointer arithmetic
689 are just plain wrong. Sigh.
692 myindex ( int scale, void* base, int index )
695 ((UChar*)base) + scale * index;
700 printName ( UChar* name, UChar* strtab )
702 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
703 UInt32 strtab_offset = * (UInt32*)(name+4);
704 fprintf ( stderr, "%s", strtab + strtab_offset );
707 for (i = 0; i < 8; i++) {
708 if (name[i] == 0) break;
709 fprintf ( stderr, "%c", name[i] );
716 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
718 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
719 UInt32 strtab_offset = * (UInt32*)(name+4);
720 strncpy ( dst, strtab+strtab_offset, dstSize );
726 if (name[i] == 0) break;
736 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
739 /* If the string is longer than 8 bytes, look in the
740 string table for it -- this will be correctly zero terminated.
742 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
743 UInt32 strtab_offset = * (UInt32*)(name+4);
744 return ((UChar*)strtab) + strtab_offset;
746 /* Otherwise, if shorter than 8 bytes, return the original,
747 which by defn is correctly terminated.
749 if (name[7]==0) return name;
750 /* The annoying case: 8 bytes. Copy into a temporary
751 (which is never freed ...)
753 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
755 strncpy(newstr,name,8);
761 /* Just compares the short names (first 8 chars) */
762 static COFF_section *
763 findPEi386SectionCalled ( ObjectCode* oc, char* name )
767 = (COFF_header*)(oc->image);
770 ((UChar*)(oc->image))
771 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
773 for (i = 0; i < hdr->NumberOfSections; i++) {
776 COFF_section* section_i
778 myindex ( sizeof_COFF_section, sectab, i );
779 n1 = (UChar*) &(section_i->Name);
781 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
782 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
783 n1[6]==n2[6] && n1[7]==n2[7])
792 zapTrailingAtSign ( UChar* sym )
794 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
796 if (sym[0] == 0) return;
798 while (sym[i] != 0) i++;
801 while (j > 0 && my_isdigit(sym[j])) j--;
802 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
808 ocVerifyImage_PEi386 ( ObjectCode* oc )
812 COFF_section* sectab;
816 hdr = (COFF_header*)(oc->image);
817 sectab = (COFF_section*) (
818 ((UChar*)(oc->image))
819 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
821 symtab = (COFF_symbol*) (
822 ((UChar*)(oc->image))
823 + hdr->PointerToSymbolTable
825 strtab = ((UChar*)(oc->image))
826 + hdr->PointerToSymbolTable
827 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
829 if (hdr->Machine != 0x14c) {
830 belch("Not x86 PEi386");
833 if (hdr->SizeOfOptionalHeader != 0) {
834 belch("PEi386 with nonempty optional header");
837 if ( /* (hdr->Characteristics & IMAGE_FILE_RELOCS_STRIPPED) || */
838 (hdr->Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ||
839 (hdr->Characteristics & IMAGE_FILE_DLL) ||
840 (hdr->Characteristics & IMAGE_FILE_SYSTEM) ) {
841 belch("Not a PEi386 object file");
844 if ( (hdr->Characteristics & IMAGE_FILE_BYTES_REVERSED_HI) ||
845 !(hdr->Characteristics & IMAGE_FILE_32BIT_MACHINE) ) {
846 belch("Invalid PEi386 word size or endiannness");
850 /* No further verification after this point; only debug printing. */
852 IF_DEBUG(linker, i=1);
853 if (i == 0) return 1;
856 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
858 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
860 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
862 fprintf ( stderr, "\n" );
864 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
866 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
868 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
870 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
872 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
874 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
876 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
878 fprintf ( stderr, "\n" );
879 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
880 fprintf ( stderr, "---START of string table---\n");
881 for (i = 4; i < *(Int32*)strtab; i++) {
883 fprintf ( stderr, "\n"); else
884 fprintf( stderr, "%c", strtab[i] );
886 fprintf ( stderr, "--- END of string table---\n");
888 fprintf ( stderr, "\n" );
889 for (i = 0; i < hdr->NumberOfSections; i++) {
891 COFF_section* sectab_i
893 myindex ( sizeof_COFF_section, sectab, i );
900 printName ( sectab_i->Name, strtab );
909 sectab_i->VirtualSize,
910 sectab_i->VirtualAddress,
911 sectab_i->SizeOfRawData,
912 sectab_i->PointerToRawData,
913 sectab_i->NumberOfRelocations,
914 sectab_i->PointerToRelocations
916 reltab = (COFF_reloc*) (
917 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
919 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
921 COFF_reloc* rel = (COFF_reloc*)
922 myindex ( sizeof_COFF_reloc, reltab, j );
924 " type 0x%-4x vaddr 0x%-8x name `",
926 rel->VirtualAddress );
928 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
929 printName ( sym->Name, strtab );
930 fprintf ( stderr, "'\n" );
932 fprintf ( stderr, "\n" );
936 fprintf ( stderr, "\n" );
939 COFF_symbol* symtab_i;
940 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
941 symtab_i = (COFF_symbol*)
942 myindex ( sizeof_COFF_symbol, symtab, i );
948 printName ( symtab_i->Name, strtab );
957 (Int32)(symtab_i->SectionNumber) - 1,
958 (UInt32)symtab_i->Type,
959 (UInt32)symtab_i->StorageClass,
960 (UInt32)symtab_i->NumberOfAuxSymbols
962 i += symtab_i->NumberOfAuxSymbols;
966 fprintf ( stderr, "\n" );
973 ocGetNames_PEi386 ( ObjectCode* oc )
976 COFF_section* sectab;
984 hdr = (COFF_header*)(oc->image);
985 sectab = (COFF_section*) (
986 ((UChar*)(oc->image))
987 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
989 symtab = (COFF_symbol*) (
990 ((UChar*)(oc->image))
991 + hdr->PointerToSymbolTable
993 strtab = ((UChar*)(oc->image))
994 + hdr->PointerToSymbolTable
995 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
997 /* Copy exported symbols into the ObjectCode. */
999 oc->n_symbols = hdr->NumberOfSymbols;
1000 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1001 "ocGetNames_PEi386(oc->symbols)");
1002 /* Call me paranoid; I don't care. */
1003 for (i = 0; i < oc->n_symbols; i++)
1004 oc->symbols[i] = NULL;
1008 COFF_symbol* symtab_i;
1009 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1010 symtab_i = (COFF_symbol*)
1011 myindex ( sizeof_COFF_symbol, symtab, i );
1013 if (symtab_i->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
1014 symtab_i->SectionNumber != IMAGE_SYM_UNDEFINED) {
1016 /* This symbol is global and defined, viz, exported */
1017 COFF_section* sectabent;
1019 /* cstring_from_COFF_symbol_name always succeeds. */
1020 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1022 /* for IMAGE_SYMCLASS_EXTERNAL
1023 && !IMAGE_SYM_UNDEFINED,
1024 the address of the symbol is:
1025 address of relevant section + offset in section
1027 sectabent = (COFF_section*)
1028 myindex ( sizeof_COFF_section,
1030 symtab_i->SectionNumber-1 );
1031 addr = ((UChar*)(oc->image))
1032 + (sectabent->PointerToRawData
1034 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1035 ASSERT(i >= 0 && i < oc->n_symbols);
1036 oc->symbols[i] = sname;
1037 insertStrHashTable(symhash, sname, addr);
1039 i += symtab_i->NumberOfAuxSymbols;
1043 /* Copy section information into the ObjectCode. */
1045 oc->n_sections = hdr->NumberOfSections;
1046 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1047 "ocGetNamesPEi386" );
1049 for (i = 0; i < oc->n_sections; i++) {
1054 = SECTIONKIND_OTHER;
1055 COFF_section* sectab_i
1057 myindex ( sizeof_COFF_section, sectab, i );
1058 IF_DEBUG(linker, belchf("section name = %s\n", sectab_i->Name ));
1061 /* I'm sure this is the Right Way to do it. However, the
1062 alternative of testing the sectab_i->Name field seems to
1063 work ok with Cygwin.
1065 if (sectab_i->Characteristics & IMAGE_SCN_CNT_CODE ||
1066 sectab_i->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1067 kind = SECTIONKIND_CODE_OR_RODATA;
1070 if (0==strcmp(".text",sectab_i->Name))
1071 kind = SECTIONKIND_CODE_OR_RODATA;
1072 if (0==strcmp(".data",sectab_i->Name) ||
1073 0==strcmp(".bss",sectab_i->Name))
1074 kind = SECTIONKIND_RWDATA;
1076 start = ((UChar*)(oc->image))
1077 + sectab_i->PointerToRawData;
1079 + sectab_i->SizeOfRawData - 1;
1081 if (kind == SECTIONKIND_OTHER) {
1082 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1086 oc->sections[i].start = start;
1087 oc->sections[i].end = end;
1088 oc->sections[i].kind = kind;
1096 ocResolve_PEi386 ( ObjectCode* oc )
1099 COFF_section* sectab;
1100 COFF_symbol* symtab;
1109 /* ToDo: should be variable-sized? But is at least safe in the
1110 sense of buffer-overrun-proof. */
1113 hdr = (COFF_header*)(oc->image);
1114 sectab = (COFF_section*) (
1115 ((UChar*)(oc->image))
1116 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1118 symtab = (COFF_symbol*) (
1119 ((UChar*)(oc->image))
1120 + hdr->PointerToSymbolTable
1122 strtab = ((UChar*)(oc->image))
1123 + hdr->PointerToSymbolTable
1124 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1126 for (i = 0; i < hdr->NumberOfSections; i++) {
1127 COFF_section* sectab_i
1129 myindex ( sizeof_COFF_section, sectab, i );
1132 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1134 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1136 COFF_reloc* reltab_j
1138 myindex ( sizeof_COFF_reloc, reltab, j );
1140 /* the location to patch */
1142 ((UChar*)(oc->image))
1143 + (sectab_i->PointerToRawData
1144 + reltab_j->VirtualAddress)
1146 /* the existing contents of pP */
1148 /* the symbol to connect to */
1149 sym = (COFF_symbol*)
1150 myindex ( sizeof_COFF_symbol,
1151 symtab, reltab_j->SymbolTableIndex );
1154 "reloc sec %2d num %3d: type 0x%-4x "
1155 "vaddr 0x%-8x name `",
1157 (UInt32)reltab_j->Type,
1158 reltab_j->VirtualAddress );
1159 printName ( sym->Name, strtab );
1160 fprintf ( stderr, "'\n" ));
1162 if (sym->StorageClass == IMAGE_SYM_CLASS_STATIC) {
1163 COFF_section* section_sym
1164 = findPEi386SectionCalled ( oc, sym->Name );
1166 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
1167 barf("Can't find abovementioned PEi386 section");
1170 S = ((UInt32)(oc->image))
1171 + (section_sym->PointerToRawData
1174 copyName ( sym->Name, strtab, symbol, 1000 );
1175 zapTrailingAtSign ( symbol );
1176 (void*)S = lookupLocalSymbol( oc, symbol );
1177 if ((void*)S == NULL)
1178 (void*)S = lookupSymbol( symbol );
1180 belch("ocResolve_PEi386: %s: unknown symbol `%s'",
1181 oc->fileName, symbol);
1186 switch (reltab_j->Type) {
1187 case IMAGE_REL_I386_DIR32:
1190 case IMAGE_REL_I386_REL32:
1191 /* Tricky. We have to insert a displacement at
1192 pP which, when added to the PC for the _next_
1193 insn, gives the address of the target (S).
1194 Problem is to know the address of the next insn
1195 when we only know pP. We assume that this
1196 literal field is always the last in the insn,
1197 so that the address of the next insn is pP+4
1198 -- hence the constant 4.
1199 Also I don't know if A should be added, but so
1200 far it has always been zero.
1203 *pP = S - ((UInt32)pP) - 4;
1207 "unhandled PEi386 relocation type %d\n",
1209 barf("unhandled PEi386 relocation type");
1219 #endif /* defined(OBJFORMAT_PEi386) */
1222 /* --------------------------------------------------------------------------
1224 * ------------------------------------------------------------------------*/
1226 #if defined(OBJFORMAT_ELF)
1231 #if defined(sparc_TARGET_ARCH)
1232 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1238 findElfSection ( void* objImage, Elf32_Word sh_type )
1241 char* ehdrC = (char*)objImage;
1242 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1243 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1245 for (i = 0; i < ehdr->e_shnum; i++) {
1246 if (shdr[i].sh_type == sh_type &&
1247 i != ehdr->e_shstrndx) {
1248 ptr = ehdrC + shdr[i].sh_offset;
1257 ocVerifyImage_ELF ( ObjectCode* oc )
1261 int i, j, nent, nstrtab, nsymtabs;
1265 char* ehdrC = (char*)(oc->image);
1266 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1268 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1269 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1270 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1271 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1272 belch("ocVerifyImage_ELF: not an ELF header");
1275 IF_DEBUG(linker,belch( "Is an ELF header" ));
1277 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1278 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1282 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1284 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1285 IF_DEBUG(linker,belch( "Is little-endian" ));
1287 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1288 IF_DEBUG(linker,belch( "Is big-endian" ));
1290 belch("ocVerifyImage_ELF: unknown endiannness");
1294 if (ehdr->e_type != ET_REL) {
1295 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1298 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1300 IF_DEBUG(linker,belch( "Architecture is " ));
1301 switch (ehdr->e_machine) {
1302 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1303 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1304 default: IF_DEBUG(linker,belch( "unknown" ));
1305 belch("ocVerifyImage_ELF: unknown architecture");
1309 IF_DEBUG(linker,belch(
1310 "\nSection header table: start %d, n_entries %d, ent_size %d",
1311 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1313 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1315 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1317 if (ehdr->e_shstrndx == SHN_UNDEF) {
1318 belch("ocVerifyImage_ELF: no section header string table");
1321 IF_DEBUG(linker,belch( "Section header string table is section %d",
1323 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1326 for (i = 0; i < ehdr->e_shnum; i++) {
1327 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1328 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1329 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1330 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1331 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1332 ehdrC + shdr[i].sh_offset,
1333 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1335 if (shdr[i].sh_type == SHT_REL) {
1336 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1337 } else if (shdr[i].sh_type == SHT_RELA) {
1338 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1340 IF_DEBUG(linker,fprintf(stderr," "));
1343 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1347 IF_DEBUG(linker,belch( "\nString tables" ));
1350 for (i = 0; i < ehdr->e_shnum; i++) {
1351 if (shdr[i].sh_type == SHT_STRTAB &&
1352 i != ehdr->e_shstrndx) {
1353 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1354 strtab = ehdrC + shdr[i].sh_offset;
1359 belch("ocVerifyImage_ELF: no string tables, or too many");
1364 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1365 for (i = 0; i < ehdr->e_shnum; i++) {
1366 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1367 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1369 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1370 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1371 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1373 shdr[i].sh_size % sizeof(Elf32_Sym)
1375 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1376 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1379 for (j = 0; j < nent; j++) {
1380 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1381 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1382 (int)stab[j].st_shndx,
1383 (int)stab[j].st_size,
1384 (char*)stab[j].st_value ));
1386 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1387 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1388 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1389 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1390 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1391 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1392 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1393 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1395 IF_DEBUG(linker,fprintf(stderr, " " ));
1397 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1398 switch (ELF32_ST_BIND(stab[j].st_info)) {
1399 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1400 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1401 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1402 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1404 IF_DEBUG(linker,fprintf(stderr, " " ));
1406 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1410 if (nsymtabs == 0) {
1411 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1420 ocGetNames_ELF ( ObjectCode* oc )
1425 char* ehdrC = (char*)(oc->image);
1426 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1427 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1428 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1429 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1431 ASSERT(symhash != NULL);
1434 belch("ocGetNames_ELF: no strtab");
1439 oc->n_sections = ehdr->e_shnum;
1440 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1441 "ocGetNames_ELF(oc->sections)" );
1443 for (i = 0; i < oc->n_sections; i++) {
1445 /* make a section entry for relevant sections */
1446 SectionKind kind = SECTIONKIND_OTHER;
1447 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1448 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1449 kind = SECTIONKIND_RWDATA;
1450 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1451 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1452 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1453 kind = SECTIONKIND_CODE_OR_RODATA;
1455 /* fill in the section info */
1456 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1457 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1458 oc->sections[i].kind = kind;
1460 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1462 /* copy stuff into this module's object symbol table */
1463 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1464 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1466 oc->n_symbols = nent;
1467 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1468 "ocGetNames_ELF(oc->symbols)");
1470 for (j = 0; j < nent; j++) {
1471 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1472 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1474 /* and not an undefined symbol */
1475 && stab[j].st_shndx != SHN_UNDEF
1476 /* and not in a "special section" */
1477 && stab[j].st_shndx < SHN_LORESERVE
1479 /* and it's a not a section or string table or anything silly */
1480 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1481 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1482 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1485 char* nm = strtab + stab[j].st_name;
1487 + shdr[ stab[j].st_shndx ].sh_offset
1491 oc->symbols[j] = nm;
1492 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1493 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1494 ad, oc->fileName, nm ));
1495 insertStrHashTable(oc->lochash, nm, ad);
1497 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1498 ad, oc->fileName, nm ));
1499 insertStrHashTable(symhash, nm, ad);
1503 IF_DEBUG(linker,belch( "skipping `%s'",
1504 strtab + stab[j].st_name ));
1507 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1508 (int)ELF32_ST_BIND(stab[j].st_info),
1509 (int)ELF32_ST_TYPE(stab[j].st_info),
1510 (int)stab[j].st_shndx,
1511 strtab + stab[j].st_name
1514 oc->symbols[j] = NULL;
1523 /* Do ELF relocations which lack an explicit addend. All x86-linux
1524 relocations appear to be of this form. */
1525 static int do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1526 Elf32_Shdr* shdr, int shnum,
1527 Elf32_Sym* stab, char* strtab )
1532 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1533 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1534 int target_shndx = shdr[shnum].sh_info;
1535 int symtab_shndx = shdr[shnum].sh_link;
1536 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1537 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1538 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1539 target_shndx, symtab_shndx ));
1540 for (j = 0; j < nent; j++) {
1541 Elf32_Addr offset = rtab[j].r_offset;
1542 Elf32_Word info = rtab[j].r_info;
1544 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1545 Elf32_Word* pP = (Elf32_Word*)P;
1549 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1550 j, (void*)offset, (void*)info ));
1552 IF_DEBUG(linker,belch( " ZERO" ));
1555 /* First see if it is a nameless local symbol. */
1556 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1557 symbol = "(noname)";
1559 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1560 + stab[ELF32_R_SYM(info)].st_value);
1562 /* No? Should be in a symbol table then; first try the
1564 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1565 (void*)S = lookupLocalSymbol( oc, symbol );
1566 if ((void*)S == NULL)
1567 (void*)S = lookupSymbol( symbol );
1570 barf("do_Elf32_Rel_relocations: %s: unknown symbol `%s'",
1571 oc->fileName, symbol);
1573 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1575 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1576 (void*)P, (void*)S, (void*)A ));
1577 switch (ELF32_R_TYPE(info)) {
1578 # ifdef i386_TARGET_ARCH
1579 case R_386_32: *pP = S + A; break;
1580 case R_386_PC32: *pP = S + A - P; break;
1583 fprintf(stderr, "unhandled ELF relocation(Rel) type %d\n",
1584 ELF32_R_TYPE(info));
1585 barf("do_Elf32_Rel_relocations: unhandled ELF relocation type");
1594 /* Do ELF relocations for which explicit addends are supplied.
1595 sparc-solaris relocations appear to be of this form. */
1596 static int do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1597 Elf32_Shdr* shdr, int shnum,
1598 Elf32_Sym* stab, char* strtab )
1603 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1604 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1605 int target_shndx = shdr[shnum].sh_info;
1606 int symtab_shndx = shdr[shnum].sh_link;
1607 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1608 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1609 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1610 target_shndx, symtab_shndx ));
1611 for (j = 0; j < nent; j++) {
1612 Elf32_Addr offset = rtab[j].r_offset;
1613 Elf32_Word info = rtab[j].r_info;
1614 Elf32_Sword addend = rtab[j].r_addend;
1616 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1617 Elf32_Addr A = addend;
1619 # if defined(sparc_TARGET_ARCH)
1620 /* This #ifdef only serves to avoid unused-var warnings. */
1621 Elf32_Word* pP = (Elf32_Word*)P;
1625 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1626 j, (void*)offset, (void*)info,
1629 IF_DEBUG(linker,belch( " ZERO" ));
1632 /* First see if it is a nameless local symbol. */
1633 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1634 symbol = "(noname)";
1636 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1637 + stab[ELF32_R_SYM(info)].st_value);
1639 /* No? Should be in a symbol table then; first try the
1641 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1642 (void*)S = lookupLocalSymbol( oc, symbol );
1643 if ((void*)S == NULL)
1644 (void*)S = lookupSymbol( symbol );
1647 barf("do_Elf32_Rela_relocations: %s: unknown symbol `%s'",
1648 oc->fileName, symbol);
1651 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1654 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1656 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1657 (void*)P, (void*)S, (void*)A ));
1658 switch (ELF32_R_TYPE(info)) {
1659 # if defined(sparc_TARGET_ARCH)
1660 case R_SPARC_WDISP30:
1661 w1 = *pP & 0xC0000000;
1662 w2 = (Elf32_Word)((S + A - P) >> 2);
1663 ASSERT((w2 & 0xC0000000) == 0);
1668 w1 = *pP & 0xFFC00000;
1669 w2 = (Elf32_Word)((S + A) >> 10);
1670 ASSERT((w2 & 0xFFC00000) == 0);
1676 w2 = (Elf32_Word)((S + A) & 0x3FF);
1677 ASSERT((w2 & ~0x3FF) == 0);
1682 w2 = (Elf32_Word)(S + A);
1687 fprintf(stderr, "unhandled ELF relocation(RelA) type %d\n",
1688 ELF32_R_TYPE(info));
1689 barf("do_Elf32_Rela_relocations: unhandled ELF relocation type");
1699 ocResolve_ELF ( ObjectCode* oc )
1703 Elf32_Sym* stab = NULL;
1704 char* ehdrC = (char*)(oc->image);
1705 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1706 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1708 /* first find "the" symbol table */
1709 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1711 /* also go find the string table */
1712 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1714 if (stab == NULL || strtab == NULL) {
1715 belch("ocResolve_ELF: can't find string or symbol table");
1719 /* Process the relocation sections. */
1720 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
1721 if (shdr[shnum].sh_type == SHT_REL ) {
1722 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
1723 shnum, stab, strtab );
1727 if (shdr[shnum].sh_type == SHT_RELA) {
1728 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
1729 shnum, stab, strtab );
1734 /* Free the local symbol table; we won't need it again. */
1735 freeHashTable(oc->lochash, NULL);