1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.38 2001/04/24 11:20:50 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
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) /**/
301 #ifdef LEADING_UNDERSCORE
302 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
304 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
307 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
309 #define SymX(vvv) Sym(vvv)
311 static RtsSymbolVal rtsSyms[] = {
314 RTS_POSIX_ONLY_SYMBOLS
315 { 0, 0 } /* sentinel */
318 /* -----------------------------------------------------------------------------
319 * initialize the object linker
321 #if defined(OBJFORMAT_ELF)
322 static void *dl_prog_handle;
330 symhash = allocStrHashTable();
332 /* populate the symbol table with stuff from the RTS */
333 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
334 insertStrHashTable(symhash, sym->lbl, sym->addr);
336 # if defined(OBJFORMAT_ELF)
337 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
341 /* -----------------------------------------------------------------------------
342 * Add a DLL from which symbols may be found. In the ELF case, just
343 * do RTLD_GLOBAL-style add, so no further messing around needs to
344 * happen in order that symbols in the loaded .so are findable --
345 * lookupSymbol() will subsequently see them by dlsym on the program's
346 * dl-handle. Returns 0 if fail, 1 if success.
349 addDLL ( char* dll_name )
351 # if defined(OBJFORMAT_ELF)
356 buf = stgMallocBytes(strlen(dll_name) + 10, "addDll");
357 sprintf(buf, "lib%s.so", dll_name);
358 hdl = dlopen(buf, RTLD_NOW | RTLD_GLOBAL );
361 /* dlopen failed; return a ptr to the error msg. */
363 if (errmsg == NULL) errmsg = "addDLL: unknown error";
368 ASSERT(0); /*NOTREACHED*/
369 # elif defined(OBJFORMAT_PEi386)
370 barf("addDLL: not implemented on PEi386 yet");
373 barf("addDLL: not implemented on this platform");
377 /* -----------------------------------------------------------------------------
378 * lookup a symbol in the hash table
381 lookupSymbol( char *lbl )
384 ASSERT(symhash != NULL);
385 val = lookupStrHashTable(symhash, lbl);
388 # if defined(OBJFORMAT_ELF)
389 return dlsym(dl_prog_handle, lbl);
390 # elif defined(OBJFORMAT_PEi386)
401 lookupLocalSymbol( ObjectCode* oc, char *lbl )
404 val = lookupStrHashTable(oc->lochash, lbl);
414 /* -----------------------------------------------------------------------------
415 * Load an obj (populate the global symbol table, but don't resolve yet)
417 * Returns: 1 if ok, 0 on error.
420 loadObj( char *path )
428 /* assert that we haven't already loaded this object */
431 for (o = objects; o; o = o->next)
432 ASSERT(strcmp(o->fileName, path));
436 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
438 # if defined(OBJFORMAT_ELF)
439 oc->formatName = "ELF";
440 # elif defined(OBJFORMAT_PEi386)
441 oc->formatName = "PEi386";
444 barf("loadObj: not implemented on this platform");
448 if (r == -1) { return 0; }
450 /* sigh, strdup() isn't a POSIX function, so do it the long way */
451 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
452 strcpy(oc->fileName, path);
454 oc->fileSize = st.st_size;
455 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
458 oc->lochash = allocStrHashTable();
460 /* chain it onto the list of objects */
464 /* load the image into memory */
465 f = fopen(path, "rb");
467 barf("loadObj: can't read `%s'", path);
469 n = fread ( oc->image, 1, oc->fileSize, f );
470 if (n != oc->fileSize) {
472 barf("loadObj: error whilst reading `%s'", path);
475 /* verify the in-memory image */
476 # if defined(OBJFORMAT_ELF)
477 r = ocVerifyImage_ELF ( oc );
478 # elif defined(OBJFORMAT_PEi386)
479 r = ocVerifyImage_PEi386 ( oc );
481 barf("loadObj: no verify method");
483 if (!r) { return r; }
485 /* build the symbol list for this image */
486 # if defined(OBJFORMAT_ELF)
487 r = ocGetNames_ELF ( oc );
488 # elif defined(OBJFORMAT_PEi386)
489 r = ocGetNames_PEi386 ( oc );
491 barf("loadObj: no getNames method");
493 if (!r) { return r; }
495 /* loaded, but not resolved yet */
496 oc->status = OBJECT_LOADED;
501 /* -----------------------------------------------------------------------------
502 * resolve all the currently unlinked objects in memory
504 * Returns: 1 if ok, 0 on error.
512 for (oc = objects; oc; oc = oc->next) {
513 if (oc->status != OBJECT_RESOLVED) {
514 # if defined(OBJFORMAT_ELF)
515 r = ocResolve_ELF ( oc );
516 # elif defined(OBJFORMAT_PEi386)
517 r = ocResolve_PEi386 ( oc );
519 barf("resolveObjs: not implemented on this platform");
521 if (!r) { return r; }
522 oc->status = OBJECT_RESOLVED;
528 /* -----------------------------------------------------------------------------
529 * delete an object from the pool
532 unloadObj( char *path )
534 ObjectCode *oc, *prev;
536 ASSERT(symhash != NULL);
537 ASSERT(objects != NULL);
540 for (oc = objects; oc; prev = oc, oc = oc->next) {
541 if (!strcmp(oc->fileName,path)) {
543 /* Remove all the mappings for the symbols within this
548 for (i = 0; i < oc->n_symbols; i++) {
549 if (oc->symbols[i] != NULL) {
550 removeStrHashTable(symhash, oc->symbols[i], NULL);
558 prev->next = oc->next;
561 /* We're going to leave this in place, in case there are
562 any pointers from the heap into it: */
563 /* free(oc->image); */
567 /* The local hash table should have been freed at the end
568 of the ocResolve_ call on it. */
569 ASSERT(oc->lochash == NULL);
575 belch("unloadObj: can't find `%s' to unload", path);
579 /* --------------------------------------------------------------------------
580 * PEi386 specifics (Win32 targets)
581 * ------------------------------------------------------------------------*/
583 /* The information for this linker comes from
584 Microsoft Portable Executable
585 and Common Object File Format Specification
586 revision 5.1 January 1998
587 which SimonM says comes from the MS Developer Network CDs.
591 #if defined(OBJFORMAT_PEi386)
595 typedef unsigned char UChar;
596 typedef unsigned short UInt16;
597 typedef unsigned int UInt32;
604 UInt16 NumberOfSections;
605 UInt32 TimeDateStamp;
606 UInt32 PointerToSymbolTable;
607 UInt32 NumberOfSymbols;
608 UInt16 SizeOfOptionalHeader;
609 UInt16 Characteristics;
613 #define sizeof_COFF_header 20
620 UInt32 VirtualAddress;
621 UInt32 SizeOfRawData;
622 UInt32 PointerToRawData;
623 UInt32 PointerToRelocations;
624 UInt32 PointerToLinenumbers;
625 UInt16 NumberOfRelocations;
626 UInt16 NumberOfLineNumbers;
627 UInt32 Characteristics;
631 #define sizeof_COFF_section 40
638 UInt16 SectionNumber;
641 UChar NumberOfAuxSymbols;
645 #define sizeof_COFF_symbol 18
650 UInt32 VirtualAddress;
651 UInt32 SymbolTableIndex;
656 #define sizeof_COFF_reloc 10
659 /* From PE spec doc, section 3.3.2 */
660 #define IMAGE_FILE_RELOCS_STRIPPED 0x0001
661 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
662 #define IMAGE_FILE_DLL 0x2000
663 #define IMAGE_FILE_SYSTEM 0x1000
664 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
665 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
666 #define IMAGE_FILE_32BIT_MACHINE 0x0100
668 /* From PE spec doc, section 5.4.2 and 5.4.4 */
669 #define IMAGE_SYM_CLASS_EXTERNAL 2
670 #define IMAGE_SYM_CLASS_STATIC 3
671 #define IMAGE_SYM_UNDEFINED 0
673 /* From PE spec doc, section 4.1 */
674 #define IMAGE_SCN_CNT_CODE 0x00000020
675 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
677 /* From PE spec doc, section 5.2.1 */
678 #define IMAGE_REL_I386_DIR32 0x0006
679 #define IMAGE_REL_I386_REL32 0x0014
682 /* We use myindex to calculate array addresses, rather than
683 simply doing the normal subscript thing. That's because
684 some of the above structs have sizes which are not
685 a whole number of words. GCC rounds their sizes up to a
686 whole number of words, which means that the address calcs
687 arising from using normal C indexing or pointer arithmetic
688 are just plain wrong. Sigh.
691 myindex ( int scale, void* base, int index )
694 ((UChar*)base) + scale * index;
699 printName ( UChar* name, UChar* strtab )
701 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
702 UInt32 strtab_offset = * (UInt32*)(name+4);
703 fprintf ( stderr, "%s", strtab + strtab_offset );
706 for (i = 0; i < 8; i++) {
707 if (name[i] == 0) break;
708 fprintf ( stderr, "%c", name[i] );
715 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
717 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
718 UInt32 strtab_offset = * (UInt32*)(name+4);
719 strncpy ( dst, strtab+strtab_offset, dstSize );
725 if (name[i] == 0) break;
735 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
738 /* If the string is longer than 8 bytes, look in the
739 string table for it -- this will be correctly zero terminated.
741 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
742 UInt32 strtab_offset = * (UInt32*)(name+4);
743 return ((UChar*)strtab) + strtab_offset;
745 /* Otherwise, if shorter than 8 bytes, return the original,
746 which by defn is correctly terminated.
748 if (name[7]==0) return name;
749 /* The annoying case: 8 bytes. Copy into a temporary
750 (which is never freed ...)
752 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
754 strncpy(newstr,name,8);
760 /* Just compares the short names (first 8 chars) */
761 static COFF_section *
762 findPEi386SectionCalled ( ObjectCode* oc, char* name )
766 = (COFF_header*)(oc->image);
769 ((UChar*)(oc->image))
770 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
772 for (i = 0; i < hdr->NumberOfSections; i++) {
775 COFF_section* section_i
777 myindex ( sizeof_COFF_section, sectab, i );
778 n1 = (UChar*) &(section_i->Name);
780 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
781 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
782 n1[6]==n2[6] && n1[7]==n2[7])
791 zapTrailingAtSign ( UChar* sym )
793 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
795 if (sym[0] == 0) return;
797 while (sym[i] != 0) i++;
800 while (j > 0 && my_isdigit(sym[j])) j--;
801 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
807 ocVerifyImage_PEi386 ( ObjectCode* oc )
811 COFF_section* sectab;
815 hdr = (COFF_header*)(oc->image);
816 sectab = (COFF_section*) (
817 ((UChar*)(oc->image))
818 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
820 symtab = (COFF_symbol*) (
821 ((UChar*)(oc->image))
822 + hdr->PointerToSymbolTable
824 strtab = ((UChar*)(oc->image))
825 + hdr->PointerToSymbolTable
826 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
828 if (hdr->Machine != 0x14c) {
829 belch("Not x86 PEi386");
832 if (hdr->SizeOfOptionalHeader != 0) {
833 belch("PEi386 with nonempty optional header");
836 if ( /* (hdr->Characteristics & IMAGE_FILE_RELOCS_STRIPPED) || */
837 (hdr->Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ||
838 (hdr->Characteristics & IMAGE_FILE_DLL) ||
839 (hdr->Characteristics & IMAGE_FILE_SYSTEM) ) {
840 belch("Not a PEi386 object file");
843 if ( (hdr->Characteristics & IMAGE_FILE_BYTES_REVERSED_HI) ||
844 !(hdr->Characteristics & IMAGE_FILE_32BIT_MACHINE) ) {
845 belch("Invalid PEi386 word size or endiannness");
849 /* No further verification after this point; only debug printing. */
851 IF_DEBUG(linker, i=1);
852 if (i == 0) return 1;
855 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
857 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
859 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
861 fprintf ( stderr, "\n" );
863 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
865 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
867 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
869 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
871 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
873 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
875 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
877 fprintf ( stderr, "\n" );
878 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
879 fprintf ( stderr, "---START of string table---\n");
880 for (i = 4; i < *(Int32*)strtab; i++) {
882 fprintf ( stderr, "\n"); else
883 fprintf( stderr, "%c", strtab[i] );
885 fprintf ( stderr, "--- END of string table---\n");
887 fprintf ( stderr, "\n" );
888 for (i = 0; i < hdr->NumberOfSections; i++) {
890 COFF_section* sectab_i
892 myindex ( sizeof_COFF_section, sectab, i );
899 printName ( sectab_i->Name, strtab );
908 sectab_i->VirtualSize,
909 sectab_i->VirtualAddress,
910 sectab_i->SizeOfRawData,
911 sectab_i->PointerToRawData,
912 sectab_i->NumberOfRelocations,
913 sectab_i->PointerToRelocations
915 reltab = (COFF_reloc*) (
916 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
918 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
920 COFF_reloc* rel = (COFF_reloc*)
921 myindex ( sizeof_COFF_reloc, reltab, j );
923 " type 0x%-4x vaddr 0x%-8x name `",
925 rel->VirtualAddress );
927 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
928 printName ( sym->Name, strtab );
929 fprintf ( stderr, "'\n" );
931 fprintf ( stderr, "\n" );
935 fprintf ( stderr, "\n" );
938 COFF_symbol* symtab_i;
939 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
940 symtab_i = (COFF_symbol*)
941 myindex ( sizeof_COFF_symbol, symtab, i );
947 printName ( symtab_i->Name, strtab );
956 (Int32)(symtab_i->SectionNumber) - 1,
957 (UInt32)symtab_i->Type,
958 (UInt32)symtab_i->StorageClass,
959 (UInt32)symtab_i->NumberOfAuxSymbols
961 i += symtab_i->NumberOfAuxSymbols;
965 fprintf ( stderr, "\n" );
972 ocGetNames_PEi386 ( ObjectCode* oc )
975 COFF_section* sectab;
983 hdr = (COFF_header*)(oc->image);
984 sectab = (COFF_section*) (
985 ((UChar*)(oc->image))
986 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
988 symtab = (COFF_symbol*) (
989 ((UChar*)(oc->image))
990 + hdr->PointerToSymbolTable
992 strtab = ((UChar*)(oc->image))
993 + hdr->PointerToSymbolTable
994 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
996 /* Copy exported symbols into the ObjectCode. */
998 oc->n_symbols = hdr->NumberOfSymbols;
999 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1000 "ocGetNames_PEi386(oc->symbols)");
1001 /* Call me paranoid; I don't care. */
1002 for (i = 0; i < oc->n_symbols; i++)
1003 oc->symbols[i] = NULL;
1007 COFF_symbol* symtab_i;
1008 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1009 symtab_i = (COFF_symbol*)
1010 myindex ( sizeof_COFF_symbol, symtab, i );
1012 if (symtab_i->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
1013 symtab_i->SectionNumber != IMAGE_SYM_UNDEFINED) {
1015 /* This symbol is global and defined, viz, exported */
1016 COFF_section* sectabent;
1018 /* cstring_from_COFF_symbol_name always succeeds. */
1019 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1021 /* for IMAGE_SYMCLASS_EXTERNAL
1022 && !IMAGE_SYM_UNDEFINED,
1023 the address of the symbol is:
1024 address of relevant section + offset in section
1026 sectabent = (COFF_section*)
1027 myindex ( sizeof_COFF_section,
1029 symtab_i->SectionNumber-1 );
1030 addr = ((UChar*)(oc->image))
1031 + (sectabent->PointerToRawData
1033 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1034 ASSERT(i >= 0 && i < oc->n_symbols);
1035 oc->symbols[i] = sname;
1036 insertStrHashTable(symhash, sname, addr);
1038 i += symtab_i->NumberOfAuxSymbols;
1042 /* Copy section information into the ObjectCode. */
1044 oc->n_sections = hdr->NumberOfSections;
1045 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1046 "ocGetNamesPEi386" );
1048 for (i = 0; i < oc->n_sections; i++) {
1053 = SECTIONKIND_OTHER;
1054 COFF_section* sectab_i
1056 myindex ( sizeof_COFF_section, sectab, i );
1057 IF_DEBUG(linker, belchf("section name = %s\n", sectab_i->Name ));
1060 /* I'm sure this is the Right Way to do it. However, the
1061 alternative of testing the sectab_i->Name field seems to
1062 work ok with Cygwin.
1064 if (sectab_i->Characteristics & IMAGE_SCN_CNT_CODE ||
1065 sectab_i->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
1066 kind = SECTIONKIND_CODE_OR_RODATA;
1069 if (0==strcmp(".text",sectab_i->Name))
1070 kind = SECTIONKIND_CODE_OR_RODATA;
1071 if (0==strcmp(".data",sectab_i->Name) ||
1072 0==strcmp(".bss",sectab_i->Name))
1073 kind = SECTIONKIND_RWDATA;
1075 start = ((UChar*)(oc->image))
1076 + sectab_i->PointerToRawData;
1078 + sectab_i->SizeOfRawData - 1;
1080 if (kind == SECTIONKIND_OTHER) {
1081 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1085 oc->sections[i].start = start;
1086 oc->sections[i].end = end;
1087 oc->sections[i].kind = kind;
1095 ocResolve_PEi386 ( ObjectCode* oc )
1098 COFF_section* sectab;
1099 COFF_symbol* symtab;
1108 /* ToDo: should be variable-sized? But is at least safe in the
1109 sense of buffer-overrun-proof. */
1112 hdr = (COFF_header*)(oc->image);
1113 sectab = (COFF_section*) (
1114 ((UChar*)(oc->image))
1115 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1117 symtab = (COFF_symbol*) (
1118 ((UChar*)(oc->image))
1119 + hdr->PointerToSymbolTable
1121 strtab = ((UChar*)(oc->image))
1122 + hdr->PointerToSymbolTable
1123 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1125 for (i = 0; i < hdr->NumberOfSections; i++) {
1126 COFF_section* sectab_i
1128 myindex ( sizeof_COFF_section, sectab, i );
1131 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1133 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1135 COFF_reloc* reltab_j
1137 myindex ( sizeof_COFF_reloc, reltab, j );
1139 /* the location to patch */
1141 ((UChar*)(oc->image))
1142 + (sectab_i->PointerToRawData
1143 + reltab_j->VirtualAddress)
1145 /* the existing contents of pP */
1147 /* the symbol to connect to */
1148 sym = (COFF_symbol*)
1149 myindex ( sizeof_COFF_symbol,
1150 symtab, reltab_j->SymbolTableIndex );
1153 "reloc sec %2d num %3d: type 0x%-4x "
1154 "vaddr 0x%-8x name `",
1156 (UInt32)reltab_j->Type,
1157 reltab_j->VirtualAddress );
1158 printName ( sym->Name, strtab );
1159 fprintf ( stderr, "'\n" ));
1161 if (sym->StorageClass == IMAGE_SYM_CLASS_STATIC) {
1162 COFF_section* section_sym
1163 = findPEi386SectionCalled ( oc, sym->Name );
1165 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
1166 barf("Can't find abovementioned PEi386 section");
1169 S = ((UInt32)(oc->image))
1170 + (section_sym->PointerToRawData
1173 copyName ( sym->Name, strtab, symbol, 1000 );
1174 zapTrailingAtSign ( symbol );
1175 (void*)S = lookupLocalSymbol( oc, symbol );
1176 if ((void*)S == NULL)
1177 (void*)S = lookupSymbol( symbol );
1179 belch("ocResolve_PEi386: %s: unknown symbol `%s'",
1180 oc->fileName, symbol);
1185 switch (reltab_j->Type) {
1186 case IMAGE_REL_I386_DIR32:
1189 case IMAGE_REL_I386_REL32:
1190 /* Tricky. We have to insert a displacement at
1191 pP which, when added to the PC for the _next_
1192 insn, gives the address of the target (S).
1193 Problem is to know the address of the next insn
1194 when we only know pP. We assume that this
1195 literal field is always the last in the insn,
1196 so that the address of the next insn is pP+4
1197 -- hence the constant 4.
1198 Also I don't know if A should be added, but so
1199 far it has always been zero.
1202 *pP = S - ((UInt32)pP) - 4;
1206 "unhandled PEi386 relocation type %d\n",
1208 barf("unhandled PEi386 relocation type");
1218 #endif /* defined(OBJFORMAT_PEi386) */
1221 /* --------------------------------------------------------------------------
1223 * ------------------------------------------------------------------------*/
1225 #if defined(OBJFORMAT_ELF)
1230 #if defined(sparc_TARGET_ARCH)
1231 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1237 findElfSection ( void* objImage, Elf32_Word sh_type )
1240 char* ehdrC = (char*)objImage;
1241 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1242 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1244 for (i = 0; i < ehdr->e_shnum; i++) {
1245 if (shdr[i].sh_type == sh_type &&
1246 i != ehdr->e_shstrndx) {
1247 ptr = ehdrC + shdr[i].sh_offset;
1256 ocVerifyImage_ELF ( ObjectCode* oc )
1260 int i, j, nent, nstrtab, nsymtabs;
1264 char* ehdrC = (char*)(oc->image);
1265 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1267 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1268 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1269 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1270 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1271 belch("ocVerifyImage_ELF: not an ELF header");
1274 IF_DEBUG(linker,belch( "Is an ELF header" ));
1276 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1277 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1281 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1283 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1284 IF_DEBUG(linker,belch( "Is little-endian" ));
1286 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1287 IF_DEBUG(linker,belch( "Is big-endian" ));
1289 belch("ocVerifyImage_ELF: unknown endiannness");
1293 if (ehdr->e_type != ET_REL) {
1294 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1297 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1299 IF_DEBUG(linker,belch( "Architecture is " ));
1300 switch (ehdr->e_machine) {
1301 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1302 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1303 default: IF_DEBUG(linker,belch( "unknown" ));
1304 belch("ocVerifyImage_ELF: unknown architecture");
1308 IF_DEBUG(linker,belch(
1309 "\nSection header table: start %d, n_entries %d, ent_size %d",
1310 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1312 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1314 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1316 if (ehdr->e_shstrndx == SHN_UNDEF) {
1317 belch("ocVerifyImage_ELF: no section header string table");
1320 IF_DEBUG(linker,belch( "Section header string table is section %d",
1322 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1325 for (i = 0; i < ehdr->e_shnum; i++) {
1326 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1327 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1328 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1329 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1330 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1331 ehdrC + shdr[i].sh_offset,
1332 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1334 if (shdr[i].sh_type == SHT_REL) {
1335 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1336 } else if (shdr[i].sh_type == SHT_RELA) {
1337 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1339 IF_DEBUG(linker,fprintf(stderr," "));
1342 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1346 IF_DEBUG(linker,belch( "\nString tables" ));
1349 for (i = 0; i < ehdr->e_shnum; i++) {
1350 if (shdr[i].sh_type == SHT_STRTAB &&
1351 i != ehdr->e_shstrndx) {
1352 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1353 strtab = ehdrC + shdr[i].sh_offset;
1358 belch("ocVerifyImage_ELF: no string tables, or too many");
1363 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1364 for (i = 0; i < ehdr->e_shnum; i++) {
1365 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1366 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1368 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1369 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1370 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1372 shdr[i].sh_size % sizeof(Elf32_Sym)
1374 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1375 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1378 for (j = 0; j < nent; j++) {
1379 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1380 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1381 (int)stab[j].st_shndx,
1382 (int)stab[j].st_size,
1383 (char*)stab[j].st_value ));
1385 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1386 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1387 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1388 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1389 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1390 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1391 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1392 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1394 IF_DEBUG(linker,fprintf(stderr, " " ));
1396 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1397 switch (ELF32_ST_BIND(stab[j].st_info)) {
1398 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1399 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1400 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1401 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1403 IF_DEBUG(linker,fprintf(stderr, " " ));
1405 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1409 if (nsymtabs == 0) {
1410 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1419 ocGetNames_ELF ( ObjectCode* oc )
1424 char* ehdrC = (char*)(oc->image);
1425 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1426 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1427 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1428 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1430 ASSERT(symhash != NULL);
1433 belch("ocGetNames_ELF: no strtab");
1438 oc->n_sections = ehdr->e_shnum;
1439 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1440 "ocGetNames_ELF(oc->sections)" );
1442 for (i = 0; i < oc->n_sections; i++) {
1444 /* make a section entry for relevant sections */
1445 SectionKind kind = SECTIONKIND_OTHER;
1446 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1447 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1448 kind = SECTIONKIND_RWDATA;
1449 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1450 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1451 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1452 kind = SECTIONKIND_CODE_OR_RODATA;
1454 /* fill in the section info */
1455 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1456 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1457 oc->sections[i].kind = kind;
1459 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1461 /* copy stuff into this module's object symbol table */
1462 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1463 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1465 oc->n_symbols = nent;
1466 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1467 "ocGetNames_ELF(oc->symbols)");
1469 for (j = 0; j < nent; j++) {
1470 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1471 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1473 /* and not an undefined symbol */
1474 && stab[j].st_shndx != SHN_UNDEF
1475 /* and not in a "special section" */
1476 && stab[j].st_shndx < SHN_LORESERVE
1478 /* and it's a not a section or string table or anything silly */
1479 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1480 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1481 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1484 char* nm = strtab + stab[j].st_name;
1486 + shdr[ stab[j].st_shndx ].sh_offset
1490 oc->symbols[j] = nm;
1491 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1492 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1493 ad, oc->fileName, nm ));
1494 insertStrHashTable(oc->lochash, nm, ad);
1496 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1497 ad, oc->fileName, nm ));
1498 insertStrHashTable(symhash, nm, ad);
1502 IF_DEBUG(linker,belch( "skipping `%s'",
1503 strtab + stab[j].st_name ));
1506 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1507 (int)ELF32_ST_BIND(stab[j].st_info),
1508 (int)ELF32_ST_TYPE(stab[j].st_info),
1509 (int)stab[j].st_shndx,
1510 strtab + stab[j].st_name
1513 oc->symbols[j] = NULL;
1522 /* Do ELF relocations which lack an explicit addend. All x86-linux
1523 relocations appear to be of this form. */
1524 static int do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1525 Elf32_Shdr* shdr, int shnum,
1526 Elf32_Sym* stab, char* strtab )
1531 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1532 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1533 int target_shndx = shdr[shnum].sh_info;
1534 int symtab_shndx = shdr[shnum].sh_link;
1535 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1536 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1537 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1538 target_shndx, symtab_shndx ));
1539 for (j = 0; j < nent; j++) {
1540 Elf32_Addr offset = rtab[j].r_offset;
1541 Elf32_Word info = rtab[j].r_info;
1543 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1544 Elf32_Word* pP = (Elf32_Word*)P;
1548 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1549 j, (void*)offset, (void*)info ));
1551 IF_DEBUG(linker,belch( " ZERO" ));
1554 /* First see if it is a nameless local symbol. */
1555 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1556 symbol = "(noname)";
1558 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1559 + stab[ELF32_R_SYM(info)].st_value);
1561 /* No? Should be in a symbol table then; first try the
1563 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1564 (void*)S = lookupLocalSymbol( oc, symbol );
1565 if ((void*)S == NULL)
1566 (void*)S = lookupSymbol( symbol );
1569 barf("do_Elf32_Rel_relocations: %s: unknown symbol `%s'",
1570 oc->fileName, symbol);
1572 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1574 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1575 (void*)P, (void*)S, (void*)A ));
1576 switch (ELF32_R_TYPE(info)) {
1577 # ifdef i386_TARGET_ARCH
1578 case R_386_32: *pP = S + A; break;
1579 case R_386_PC32: *pP = S + A - P; break;
1582 fprintf(stderr, "unhandled ELF relocation(Rel) type %d\n",
1583 ELF32_R_TYPE(info));
1584 barf("do_Elf32_Rel_relocations: unhandled ELF relocation type");
1593 /* Do ELF relocations for which explicit addends are supplied.
1594 sparc-solaris relocations appear to be of this form. */
1595 static int do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1596 Elf32_Shdr* shdr, int shnum,
1597 Elf32_Sym* stab, char* strtab )
1602 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1603 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1604 int target_shndx = shdr[shnum].sh_info;
1605 int symtab_shndx = shdr[shnum].sh_link;
1606 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1607 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1608 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1609 target_shndx, symtab_shndx ));
1610 for (j = 0; j < nent; j++) {
1611 Elf32_Addr offset = rtab[j].r_offset;
1612 Elf32_Word info = rtab[j].r_info;
1613 Elf32_Sword addend = rtab[j].r_addend;
1615 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1616 Elf32_Addr A = addend;
1618 # if defined(sparc_TARGET_ARCH)
1619 /* This #ifdef only serves to avoid unused-var warnings. */
1620 Elf32_Word* pP = (Elf32_Word*)P;
1624 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1625 j, (void*)offset, (void*)info,
1628 IF_DEBUG(linker,belch( " ZERO" ));
1631 /* First see if it is a nameless local symbol. */
1632 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1633 symbol = "(noname)";
1635 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1636 + stab[ELF32_R_SYM(info)].st_value);
1638 /* No? Should be in a symbol table then; first try the
1640 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1641 (void*)S = lookupLocalSymbol( oc, symbol );
1642 if ((void*)S == NULL)
1643 (void*)S = lookupSymbol( symbol );
1646 barf("do_Elf32_Rela_relocations: %s: unknown symbol `%s'",
1647 oc->fileName, symbol);
1650 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1653 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1655 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1656 (void*)P, (void*)S, (void*)A ));
1657 switch (ELF32_R_TYPE(info)) {
1658 # if defined(sparc_TARGET_ARCH)
1659 case R_SPARC_WDISP30:
1660 w1 = *pP & 0xC0000000;
1661 w2 = (Elf32_Word)((S + A - P) >> 2);
1662 ASSERT((w2 & 0xC0000000) == 0);
1667 w1 = *pP & 0xFFC00000;
1668 w2 = (Elf32_Word)((S + A) >> 10);
1669 ASSERT((w2 & 0xFFC00000) == 0);
1675 w2 = (Elf32_Word)((S + A) & 0x3FF);
1676 ASSERT((w2 & ~0x3FF) == 0);
1681 w2 = (Elf32_Word)(S + A);
1686 fprintf(stderr, "unhandled ELF relocation(RelA) type %d\n",
1687 ELF32_R_TYPE(info));
1688 barf("do_Elf32_Rela_relocations: unhandled ELF relocation type");
1698 ocResolve_ELF ( ObjectCode* oc )
1702 Elf32_Sym* stab = NULL;
1703 char* ehdrC = (char*)(oc->image);
1704 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1705 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1707 /* first find "the" symbol table */
1708 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1710 /* also go find the string table */
1711 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1713 if (stab == NULL || strtab == NULL) {
1714 belch("ocResolve_ELF: can't find string or symbol table");
1718 /* Process the relocation sections. */
1719 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
1720 if (shdr[shnum].sh_type == SHT_REL ) {
1721 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
1722 shnum, stab, strtab );
1726 if (shdr[shnum].sh_type == SHT_RELA) {
1727 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
1728 shnum, stab, strtab );
1733 /* Free the local symbol table; we won't need it again. */
1734 freeHashTable(oc->lochash, NULL);