1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.47 2001/06/28 14:26:58 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 \
99 SymX(getprotobynumber) \
100 SymX(getprotobyname) \
101 SymX(gethostbyname) \
102 SymX(gethostbyaddr) \
137 Sym(_imp___timezone) \
147 SymX(GetCurrentProcess) \
148 SymX(GetProcessTimes) \
150 SymX(GetExitCodeProcess) \
151 SymX(WaitForSingleObject) \
152 SymX(CreateProcessA) \
157 #define RTS_SYMBOLS \
159 Sym(stg_gc_enter_1) \
166 Sym(stg_gc_unpt_r1) \
167 Sym(stg_gc_unbx_r1) \
172 SymX(stg_update_PAP) \
173 SymX(stg_ap_1_upd_info) \
174 SymX(stg_ap_2_upd_info) \
175 SymX(stg_ap_3_upd_info) \
176 SymX(stg_ap_4_upd_info) \
177 SymX(stg_ap_5_upd_info) \
178 SymX(stg_ap_6_upd_info) \
179 SymX(stg_ap_7_upd_info) \
180 SymX(stg_ap_8_upd_info) \
181 SymX(stg_sel_0_upd_info) \
182 SymX(stg_sel_1_upd_info) \
183 SymX(stg_sel_2_upd_info) \
184 SymX(stg_sel_3_upd_info) \
185 SymX(stg_sel_4_upd_info) \
186 SymX(stg_sel_5_upd_info) \
187 SymX(stg_sel_6_upd_info) \
188 SymX(stg_sel_7_upd_info) \
189 SymX(stg_sel_8_upd_info) \
190 SymX(stg_sel_9_upd_info) \
191 SymX(stg_sel_10_upd_info) \
192 SymX(stg_sel_11_upd_info) \
193 SymX(stg_sel_12_upd_info) \
194 SymX(stg_sel_13_upd_info) \
195 SymX(stg_sel_14_upd_info) \
196 SymX(stg_sel_15_upd_info) \
197 SymX(stg_upd_frame_info) \
198 SymX(stg_seq_frame_info) \
199 SymX(stg_CAF_BLACKHOLE_info) \
200 SymX(stg_IND_STATIC_info) \
201 SymX(stg_EMPTY_MVAR_info) \
202 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
203 SymX(stg_WEAK_info) \
204 SymX(stg_CHARLIKE_closure) \
205 SymX(stg_INTLIKE_closure) \
207 SymX(newBCOzh_fast) \
208 SymX(mkApUpd0zh_fast) \
209 SymX(putMVarzh_fast) \
210 SymX(newMVarzh_fast) \
211 SymX(takeMVarzh_fast) \
212 SymX(tryTakeMVarzh_fast) \
213 SymX(tryPutMVarzh_fast) \
219 SymX(killThreadzh_fast) \
220 SymX(waitReadzh_fast) \
221 SymX(waitWritezh_fast) \
222 SymX(suspendThread) \
224 SymX(stackOverflow) \
225 SymX(int2Integerzh_fast) \
226 SymX(word2Integerzh_fast) \
228 SymX(__encodeDouble) \
229 SymX(decodeDoublezh_fast) \
230 SymX(decodeFloatzh_fast) \
231 SymX(gcdIntegerzh_fast) \
232 SymX(newArrayzh_fast) \
233 SymX(unsafeThawArrayzh_fast) \
234 SymX(newByteArrayzh_fast) \
235 SymX(newMutVarzh_fast) \
236 SymX(quotRemIntegerzh_fast) \
237 SymX(quotIntegerzh_fast) \
238 SymX(remIntegerzh_fast) \
239 SymX(divExactIntegerzh_fast) \
240 SymX(divModIntegerzh_fast) \
241 SymX(timesIntegerzh_fast) \
242 SymX(minusIntegerzh_fast) \
243 SymX(plusIntegerzh_fast) \
244 SymX(andIntegerzh_fast) \
245 SymX(orIntegerzh_fast) \
246 SymX(xorIntegerzh_fast) \
247 SymX(complementIntegerzh_fast) \
249 SymX(blockAsyncExceptionszh_fast) \
250 SymX(unblockAsyncExceptionszh_fast) \
252 SymX(isDoubleInfinite) \
253 SymX(isDoubleDenormalized) \
254 SymX(isDoubleNegativeZero) \
255 SymX(__encodeFloat) \
257 SymX(isFloatInfinite) \
258 SymX(isFloatDenormalized) \
259 SymX(isFloatNegativeZero) \
260 SymX(__int_encodeFloat) \
261 SymX(__int_encodeDouble) \
262 SymX(__gmpz_cmp_si) \
263 SymX(__gmpz_cmp_ui) \
266 SymX(__gmpz_get_si) \
267 SymX(__gmpz_get_ui) \
270 SymX(resetNonBlockingFd) \
273 SymX(stable_ptr_table) \
274 SymX(shutdownHaskellAndExit) \
275 Sym(stg_enterStackTop) \
276 Sym(stg_yield_to_interpreter) \
280 Sym(__init_PrelGHC) \
281 SymX(freeHaskellFunctionPtr) \
284 SymX(NoRunnableThreadsHook) \
285 SymX(StackOverflowHook) \
286 SymX(OutOfHeapHook) \
287 SymX(MallocFailHook) \
288 SymX(PatErrorHdrHook) \
291 SymX(PostTraceHook) \
292 SymX(createAdjustor) \
307 SymX(rts_mkStablePtr) \
316 SymX(rts_getWord32) \
319 SymX(rts_getDouble) \
320 SymX(rts_getStablePtr) \
326 SymX(rts_evalLazyIO) \
327 SymX(rts_checkSchedStatus)
329 #ifndef SUPPORT_LONG_LONGS
330 #define RTS_LONG_LONG_SYMS /* nothing */
332 #define RTS_LONG_LONG_SYMS \
345 SymX(stg_remWord64) \
346 SymX(stg_quotWord64) \
348 SymX(stg_quotInt64) \
349 SymX(stg_negateInt64) \
350 SymX(stg_plusInt64) \
351 SymX(stg_minusInt64) \
352 SymX(stg_timesInt64) \
358 SymX(stg_shiftRL64) \
359 SymX(stg_iShiftL64) \
360 SymX(stg_iShiftRL64) \
361 SymX(stg_iShiftRA64) \
362 SymX(stg_intToInt64) \
363 SymX(stg_int64ToInt) \
364 SymX(stg_int64ToWord64) \
365 SymX(stg_wordToWord64) \
366 SymX(stg_word64ToWord) \
367 SymX(stg_word64ToInt64) \
368 SymX(int64ToIntegerzh_fast) \
369 SymX(word64ToIntegerzh_fast)
370 #endif /* SUPPORT_LONG_LONGS */
372 /* entirely bogus claims about types of these symbols */
373 #define Sym(vvv) extern void (vvv);
374 #define SymX(vvv) /**/
377 RTS_POSIX_ONLY_SYMBOLS
378 RTS_MINGW_ONLY_SYMBOLS
382 #ifdef LEADING_UNDERSCORE
383 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
385 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
388 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
390 #define SymX(vvv) Sym(vvv)
392 static RtsSymbolVal rtsSyms[] = {
395 RTS_POSIX_ONLY_SYMBOLS
396 RTS_MINGW_ONLY_SYMBOLS
397 { 0, 0 } /* sentinel */
400 /* -----------------------------------------------------------------------------
401 * initialize the object linker
403 #if defined(OBJFORMAT_ELF)
404 static void *dl_prog_handle;
412 symhash = allocStrHashTable();
414 /* populate the symbol table with stuff from the RTS */
415 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
416 insertStrHashTable(symhash, sym->lbl, sym->addr);
418 # if defined(OBJFORMAT_ELF)
419 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
423 /* -----------------------------------------------------------------------------
424 * Add a DLL from which symbols may be found. In the ELF case, just
425 * do RTLD_GLOBAL-style add, so no further messing around needs to
426 * happen in order that symbols in the loaded .so are findable --
427 * lookupSymbol() will subsequently see them by dlsym on the program's
428 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
430 * In the PEi386 case, open the DLLs and put handles to them in a
431 * linked list. When looking for a symbol, try all handles in the
435 #if defined(OBJFORMAT_PEi386)
436 /* A record for storing handles into DLLs. */
441 struct _OpenedDLL* next;
446 /* A list thereof. */
447 static OpenedDLL* opened_dlls = NULL;
453 addDLL ( char* path, char* dll_name )
455 # if defined(OBJFORMAT_ELF)
460 if (path == NULL || strlen(path) == 0) {
461 buf = stgMallocBytes(strlen(dll_name) + 10, "addDll");
462 sprintf(buf, "lib%s.so", dll_name);
464 buf = stgMallocBytes(strlen(path) + 1 + strlen(dll_name) + 10, "addDll");
465 sprintf(buf, "%s/lib%s.so", path, dll_name);
467 hdl = dlopen(buf, RTLD_NOW | RTLD_GLOBAL );
470 /* dlopen failed; return a ptr to the error msg. */
472 if (errmsg == NULL) errmsg = "addDLL: unknown error";
479 # elif defined(OBJFORMAT_PEi386)
481 /* Add this DLL to the list of DLLs in which to search for symbols.
482 The path argument is ignored. */
487 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
489 /* See if we've already got it, and ignore if so. */
490 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
491 if (0 == strcmp(o_dll->name, dll_name))
495 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
496 sprintf(buf, "%s.DLL", dll_name);
497 instance = LoadLibrary(buf);
499 if (instance == NULL) {
500 /* LoadLibrary failed; return a ptr to the error msg. */
501 return "addDLL: unknown error";
504 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
505 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
506 strcpy(o_dll->name, dll_name);
507 o_dll->instance = instance;
508 o_dll->next = opened_dlls;
513 barf("addDLL: not implemented on this platform");
517 /* -----------------------------------------------------------------------------
518 * lookup a symbol in the hash table
521 lookupSymbol( char *lbl )
524 ASSERT(symhash != NULL);
525 val = lookupStrHashTable(symhash, lbl);
528 # if defined(OBJFORMAT_ELF)
529 return dlsym(dl_prog_handle, lbl);
530 # elif defined(OBJFORMAT_PEi386)
533 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
534 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
535 sym = GetProcAddress(o_dll->instance, lbl);
536 if (sym != NULL) return sym;
547 lookupLocalSymbol( ObjectCode* oc, char *lbl )
550 val = lookupStrHashTable(oc->lochash, lbl);
560 /* -----------------------------------------------------------------------------
561 * Load an obj (populate the global symbol table, but don't resolve yet)
563 * Returns: 1 if ok, 0 on error.
566 loadObj( char *path )
573 /* fprintf(stderr, "loadObj %s\n", path ); */
575 /* assert that we haven't already loaded this object */
578 for (o = objects; o; o = o->next)
579 ASSERT(strcmp(o->fileName, path));
583 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
585 # if defined(OBJFORMAT_ELF)
586 oc->formatName = "ELF";
587 # elif defined(OBJFORMAT_PEi386)
588 oc->formatName = "PEi386";
591 barf("loadObj: not implemented on this platform");
595 if (r == -1) { return 0; }
597 /* sigh, strdup() isn't a POSIX function, so do it the long way */
598 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
599 strcpy(oc->fileName, path);
601 oc->fileSize = st.st_size;
602 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
605 oc->lochash = allocStrHashTable();
607 /* chain it onto the list of objects */
611 /* load the image into memory */
612 f = fopen(path, "rb");
614 barf("loadObj: can't read `%s'", path);
616 n = fread ( oc->image, 1, oc->fileSize, f );
617 if (n != oc->fileSize) {
619 barf("loadObj: error whilst reading `%s'", path);
622 /* verify the in-memory image */
623 # if defined(OBJFORMAT_ELF)
624 r = ocVerifyImage_ELF ( oc );
625 # elif defined(OBJFORMAT_PEi386)
626 r = ocVerifyImage_PEi386 ( oc );
628 barf("loadObj: no verify method");
630 if (!r) { return r; }
632 /* build the symbol list for this image */
633 # if defined(OBJFORMAT_ELF)
634 r = ocGetNames_ELF ( oc );
635 # elif defined(OBJFORMAT_PEi386)
636 r = ocGetNames_PEi386 ( oc );
638 barf("loadObj: no getNames method");
640 if (!r) { return r; }
642 /* loaded, but not resolved yet */
643 oc->status = OBJECT_LOADED;
648 /* -----------------------------------------------------------------------------
649 * resolve all the currently unlinked objects in memory
651 * Returns: 1 if ok, 0 on error.
659 for (oc = objects; oc; oc = oc->next) {
660 if (oc->status != OBJECT_RESOLVED) {
661 # if defined(OBJFORMAT_ELF)
662 r = ocResolve_ELF ( oc );
663 # elif defined(OBJFORMAT_PEi386)
664 r = ocResolve_PEi386 ( oc );
666 barf("resolveObjs: not implemented on this platform");
668 if (!r) { return r; }
669 oc->status = OBJECT_RESOLVED;
675 /* -----------------------------------------------------------------------------
676 * delete an object from the pool
679 unloadObj( char *path )
681 ObjectCode *oc, *prev;
683 ASSERT(symhash != NULL);
684 ASSERT(objects != NULL);
687 for (oc = objects; oc; prev = oc, oc = oc->next) {
688 if (!strcmp(oc->fileName,path)) {
690 /* Remove all the mappings for the symbols within this
695 for (i = 0; i < oc->n_symbols; i++) {
696 if (oc->symbols[i] != NULL) {
697 removeStrHashTable(symhash, oc->symbols[i], NULL);
705 prev->next = oc->next;
708 /* We're going to leave this in place, in case there are
709 any pointers from the heap into it: */
710 /* free(oc->image); */
714 /* The local hash table should have been freed at the end
715 of the ocResolve_ call on it. */
716 ASSERT(oc->lochash == NULL);
722 belch("unloadObj: can't find `%s' to unload", path);
726 /* --------------------------------------------------------------------------
727 * PEi386 specifics (Win32 targets)
728 * ------------------------------------------------------------------------*/
730 /* The information for this linker comes from
731 Microsoft Portable Executable
732 and Common Object File Format Specification
733 revision 5.1 January 1998
734 which SimonM says comes from the MS Developer Network CDs.
738 #if defined(OBJFORMAT_PEi386)
742 typedef unsigned char UChar;
743 typedef unsigned short UInt16;
744 typedef unsigned int UInt32;
751 UInt16 NumberOfSections;
752 UInt32 TimeDateStamp;
753 UInt32 PointerToSymbolTable;
754 UInt32 NumberOfSymbols;
755 UInt16 SizeOfOptionalHeader;
756 UInt16 Characteristics;
760 #define sizeof_COFF_header 20
767 UInt32 VirtualAddress;
768 UInt32 SizeOfRawData;
769 UInt32 PointerToRawData;
770 UInt32 PointerToRelocations;
771 UInt32 PointerToLinenumbers;
772 UInt16 NumberOfRelocations;
773 UInt16 NumberOfLineNumbers;
774 UInt32 Characteristics;
778 #define sizeof_COFF_section 40
785 UInt16 SectionNumber;
788 UChar NumberOfAuxSymbols;
792 #define sizeof_COFF_symbol 18
797 UInt32 VirtualAddress;
798 UInt32 SymbolTableIndex;
803 #define sizeof_COFF_reloc 10
806 /* From PE spec doc, section 3.3.2 */
807 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
808 windows.h -- for the same purpose, but I want to know what I'm
810 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
811 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
812 #define MYIMAGE_FILE_DLL 0x2000
813 #define MYIMAGE_FILE_SYSTEM 0x1000
814 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
815 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
816 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
818 /* From PE spec doc, section 5.4.2 and 5.4.4 */
819 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
820 #define MYIMAGE_SYM_CLASS_STATIC 3
821 #define MYIMAGE_SYM_UNDEFINED 0
823 /* From PE spec doc, section 4.1 */
824 #define MYIMAGE_SCN_CNT_CODE 0x00000020
825 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
827 /* From PE spec doc, section 5.2.1 */
828 #define MYIMAGE_REL_I386_DIR32 0x0006
829 #define MYIMAGE_REL_I386_REL32 0x0014
832 /* We use myindex to calculate array addresses, rather than
833 simply doing the normal subscript thing. That's because
834 some of the above structs have sizes which are not
835 a whole number of words. GCC rounds their sizes up to a
836 whole number of words, which means that the address calcs
837 arising from using normal C indexing or pointer arithmetic
838 are just plain wrong. Sigh.
841 myindex ( int scale, void* base, int index )
844 ((UChar*)base) + scale * index;
849 printName ( UChar* name, UChar* strtab )
851 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
852 UInt32 strtab_offset = * (UInt32*)(name+4);
853 fprintf ( stderr, "%s", strtab + strtab_offset );
856 for (i = 0; i < 8; i++) {
857 if (name[i] == 0) break;
858 fprintf ( stderr, "%c", name[i] );
865 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
867 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
868 UInt32 strtab_offset = * (UInt32*)(name+4);
869 strncpy ( dst, strtab+strtab_offset, dstSize );
875 if (name[i] == 0) break;
885 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
888 /* If the string is longer than 8 bytes, look in the
889 string table for it -- this will be correctly zero terminated.
891 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
892 UInt32 strtab_offset = * (UInt32*)(name+4);
893 return ((UChar*)strtab) + strtab_offset;
895 /* Otherwise, if shorter than 8 bytes, return the original,
896 which by defn is correctly terminated.
898 if (name[7]==0) return name;
899 /* The annoying case: 8 bytes. Copy into a temporary
900 (which is never freed ...)
902 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
904 strncpy(newstr,name,8);
910 /* Just compares the short names (first 8 chars) */
911 static COFF_section *
912 findPEi386SectionCalled ( ObjectCode* oc, char* name )
916 = (COFF_header*)(oc->image);
919 ((UChar*)(oc->image))
920 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
922 for (i = 0; i < hdr->NumberOfSections; i++) {
925 COFF_section* section_i
927 myindex ( sizeof_COFF_section, sectab, i );
928 n1 = (UChar*) &(section_i->Name);
930 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
931 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
932 n1[6]==n2[6] && n1[7]==n2[7])
941 zapTrailingAtSign ( UChar* sym )
943 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
945 if (sym[0] == 0) return;
947 while (sym[i] != 0) i++;
950 while (j > 0 && my_isdigit(sym[j])) j--;
951 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
957 ocVerifyImage_PEi386 ( ObjectCode* oc )
961 COFF_section* sectab;
964 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
965 hdr = (COFF_header*)(oc->image);
966 sectab = (COFF_section*) (
967 ((UChar*)(oc->image))
968 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
970 symtab = (COFF_symbol*) (
971 ((UChar*)(oc->image))
972 + hdr->PointerToSymbolTable
974 strtab = ((UChar*)symtab)
975 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
977 if (hdr->Machine != 0x14c) {
978 belch("Not x86 PEi386");
981 if (hdr->SizeOfOptionalHeader != 0) {
982 belch("PEi386 with nonempty optional header");
985 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
986 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
987 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
988 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
989 belch("Not a PEi386 object file");
992 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
993 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
994 belch("Invalid PEi386 word size or endiannness: %d",
995 (int)(hdr->Characteristics));
998 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
999 if (* (UInt32*)strtab > 510000) {
1000 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1004 /* No further verification after this point; only debug printing. */
1006 IF_DEBUG(linker, i=1);
1007 if (i == 0) return 1;
1010 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1012 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1014 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1016 fprintf ( stderr, "\n" );
1018 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1020 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1022 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1024 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1026 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1028 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1030 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1032 /* Print the section table. */
1033 fprintf ( stderr, "\n" );
1034 for (i = 0; i < hdr->NumberOfSections; i++) {
1036 COFF_section* sectab_i
1038 myindex ( sizeof_COFF_section, sectab, i );
1045 printName ( sectab_i->Name, strtab );
1054 sectab_i->VirtualSize,
1055 sectab_i->VirtualAddress,
1056 sectab_i->SizeOfRawData,
1057 sectab_i->PointerToRawData,
1058 sectab_i->NumberOfRelocations,
1059 sectab_i->PointerToRelocations
1061 reltab = (COFF_reloc*) (
1062 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1065 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1067 COFF_reloc* rel = (COFF_reloc*)
1068 myindex ( sizeof_COFF_reloc, reltab, j );
1070 " type 0x%-4x vaddr 0x%-8x name `",
1072 rel->VirtualAddress );
1073 sym = (COFF_symbol*)
1074 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1075 printName ( sym->Name, strtab -10 );
1076 fprintf ( stderr, "'\n" );
1078 fprintf ( stderr, "\n" );
1081 fprintf ( stderr, "\n" );
1082 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1083 fprintf ( stderr, "---START of string table---\n");
1084 for (i = 4; i < *(Int32*)strtab; i++) {
1086 fprintf ( stderr, "\n"); else
1087 fprintf( stderr, "%c", strtab[i] );
1089 fprintf ( stderr, "--- END of string table---\n");
1091 fprintf ( stderr, "\n" );
1094 COFF_symbol* symtab_i;
1095 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1096 symtab_i = (COFF_symbol*)
1097 myindex ( sizeof_COFF_symbol, symtab, i );
1103 printName ( symtab_i->Name, strtab );
1112 (Int32)(symtab_i->SectionNumber) - 1,
1113 (UInt32)symtab_i->Type,
1114 (UInt32)symtab_i->StorageClass,
1115 (UInt32)symtab_i->NumberOfAuxSymbols
1117 i += symtab_i->NumberOfAuxSymbols;
1121 fprintf ( stderr, "\n" );
1127 ocGetNames_PEi386 ( ObjectCode* oc )
1130 COFF_section* sectab;
1131 COFF_symbol* symtab;
1138 hdr = (COFF_header*)(oc->image);
1139 sectab = (COFF_section*) (
1140 ((UChar*)(oc->image))
1141 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1143 symtab = (COFF_symbol*) (
1144 ((UChar*)(oc->image))
1145 + hdr->PointerToSymbolTable
1147 strtab = ((UChar*)(oc->image))
1148 + hdr->PointerToSymbolTable
1149 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1151 /* Copy exported symbols into the ObjectCode. */
1153 oc->n_symbols = hdr->NumberOfSymbols;
1154 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1155 "ocGetNames_PEi386(oc->symbols)");
1156 /* Call me paranoid; I don't care. */
1157 for (i = 0; i < oc->n_symbols; i++)
1158 oc->symbols[i] = NULL;
1162 COFF_symbol* symtab_i;
1163 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1164 symtab_i = (COFF_symbol*)
1165 myindex ( sizeof_COFF_symbol, symtab, i );
1167 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL &&
1168 symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1170 /* This symbol is global and defined, viz, exported */
1171 COFF_section* sectabent;
1173 /* cstring_from_COFF_symbol_name always succeeds. */
1174 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1176 /* for MYIMAGE_SYMCLASS_EXTERNAL
1177 && !MYIMAGE_SYM_UNDEFINED,
1178 the address of the symbol is:
1179 address of relevant section + offset in section
1181 sectabent = (COFF_section*)
1182 myindex ( sizeof_COFF_section,
1184 symtab_i->SectionNumber-1 );
1185 addr = ((UChar*)(oc->image))
1186 + (sectabent->PointerToRawData
1188 /* fprintf(stderr,"addSymbol %p `%s'\n", addr,sname); */
1189 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1190 ASSERT(i >= 0 && i < oc->n_symbols);
1191 oc->symbols[i] = sname;
1192 insertStrHashTable(symhash, sname, addr);
1194 i += symtab_i->NumberOfAuxSymbols;
1198 /* Copy section information into the ObjectCode. */
1200 oc->n_sections = hdr->NumberOfSections;
1201 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1202 "ocGetNamesPEi386" );
1204 for (i = 0; i < oc->n_sections; i++) {
1209 = SECTIONKIND_OTHER;
1210 COFF_section* sectab_i
1212 myindex ( sizeof_COFF_section, sectab, i );
1213 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1216 /* I'm sure this is the Right Way to do it. However, the
1217 alternative of testing the sectab_i->Name field seems to
1218 work ok with Cygwin.
1220 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1221 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1222 kind = SECTIONKIND_CODE_OR_RODATA;
1225 if (0==strcmp(".text",sectab_i->Name))
1226 kind = SECTIONKIND_CODE_OR_RODATA;
1227 if (0==strcmp(".data",sectab_i->Name) ||
1228 0==strcmp(".bss",sectab_i->Name))
1229 kind = SECTIONKIND_RWDATA;
1231 start = ((UChar*)(oc->image))
1232 + sectab_i->PointerToRawData;
1234 + sectab_i->SizeOfRawData - 1;
1236 if (kind == SECTIONKIND_OTHER) {
1237 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1241 oc->sections[i].start = start;
1242 oc->sections[i].end = end;
1243 oc->sections[i].kind = kind;
1251 ocResolve_PEi386 ( ObjectCode* oc )
1254 COFF_section* sectab;
1255 COFF_symbol* symtab;
1264 /* ToDo: should be variable-sized? But is at least safe in the
1265 sense of buffer-overrun-proof. */
1267 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1269 hdr = (COFF_header*)(oc->image);
1270 sectab = (COFF_section*) (
1271 ((UChar*)(oc->image))
1272 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1274 symtab = (COFF_symbol*) (
1275 ((UChar*)(oc->image))
1276 + hdr->PointerToSymbolTable
1278 strtab = ((UChar*)(oc->image))
1279 + hdr->PointerToSymbolTable
1280 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1282 for (i = 0; i < hdr->NumberOfSections; i++) {
1283 COFF_section* sectab_i
1285 myindex ( sizeof_COFF_section, sectab, i );
1288 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1290 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1292 COFF_reloc* reltab_j
1294 myindex ( sizeof_COFF_reloc, reltab, j );
1296 /* the location to patch */
1298 ((UChar*)(oc->image))
1299 + (sectab_i->PointerToRawData
1300 + reltab_j->VirtualAddress
1301 - sectab_i->VirtualAddress )
1303 /* the existing contents of pP */
1305 /* the symbol to connect to */
1306 sym = (COFF_symbol*)
1307 myindex ( sizeof_COFF_symbol,
1308 symtab, reltab_j->SymbolTableIndex );
1311 "reloc sec %2d num %3d: type 0x%-4x "
1312 "vaddr 0x%-8x name `",
1314 (UInt32)reltab_j->Type,
1315 reltab_j->VirtualAddress );
1316 printName ( sym->Name, strtab );
1317 fprintf ( stderr, "'\n" ));
1319 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1320 COFF_section* section_sym
1321 = findPEi386SectionCalled ( oc, sym->Name );
1323 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
1324 barf("Can't find abovementioned PEi386 section");
1327 S = ((UInt32)(oc->image))
1328 + (section_sym->PointerToRawData
1331 copyName ( sym->Name, strtab, symbol, 1000-1 );
1332 zapTrailingAtSign ( symbol );
1333 (void*)S = lookupLocalSymbol( oc, symbol );
1334 if ((void*)S == NULL)
1335 (void*)S = lookupSymbol( symbol );
1337 belch("ocResolve_PEi386: %s: unknown symbol `%s'",
1338 oc->fileName, symbol);
1343 switch (reltab_j->Type) {
1344 case MYIMAGE_REL_I386_DIR32:
1347 case MYIMAGE_REL_I386_REL32:
1348 /* Tricky. We have to insert a displacement at
1349 pP which, when added to the PC for the _next_
1350 insn, gives the address of the target (S).
1351 Problem is to know the address of the next insn
1352 when we only know pP. We assume that this
1353 literal field is always the last in the insn,
1354 so that the address of the next insn is pP+4
1355 -- hence the constant 4.
1356 Also I don't know if A should be added, but so
1357 far it has always been zero.
1360 *pP = S - ((UInt32)pP) - 4;
1364 "unhandled PEi386 relocation type %d\n",
1366 barf("unhandled PEi386 relocation type");
1373 /* fprintf(stderr, "completed %s\n", oc->fileName); */
1377 #endif /* defined(OBJFORMAT_PEi386) */
1380 /* --------------------------------------------------------------------------
1382 * ------------------------------------------------------------------------*/
1384 #if defined(OBJFORMAT_ELF)
1389 #if defined(sparc_TARGET_ARCH)
1390 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1396 findElfSection ( void* objImage, Elf32_Word sh_type )
1399 char* ehdrC = (char*)objImage;
1400 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1401 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1403 for (i = 0; i < ehdr->e_shnum; i++) {
1404 if (shdr[i].sh_type == sh_type &&
1405 i != ehdr->e_shstrndx) {
1406 ptr = ehdrC + shdr[i].sh_offset;
1415 ocVerifyImage_ELF ( ObjectCode* oc )
1419 int i, j, nent, nstrtab, nsymtabs;
1423 char* ehdrC = (char*)(oc->image);
1424 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1426 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1427 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1428 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1429 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1430 belch("ocVerifyImage_ELF: not an ELF header");
1433 IF_DEBUG(linker,belch( "Is an ELF header" ));
1435 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1436 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1440 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1442 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1443 IF_DEBUG(linker,belch( "Is little-endian" ));
1445 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1446 IF_DEBUG(linker,belch( "Is big-endian" ));
1448 belch("ocVerifyImage_ELF: unknown endiannness");
1452 if (ehdr->e_type != ET_REL) {
1453 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1456 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1458 IF_DEBUG(linker,belch( "Architecture is " ));
1459 switch (ehdr->e_machine) {
1460 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1461 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1462 default: IF_DEBUG(linker,belch( "unknown" ));
1463 belch("ocVerifyImage_ELF: unknown architecture");
1467 IF_DEBUG(linker,belch(
1468 "\nSection header table: start %d, n_entries %d, ent_size %d",
1469 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1471 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1473 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1475 if (ehdr->e_shstrndx == SHN_UNDEF) {
1476 belch("ocVerifyImage_ELF: no section header string table");
1479 IF_DEBUG(linker,belch( "Section header string table is section %d",
1481 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1484 for (i = 0; i < ehdr->e_shnum; i++) {
1485 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1486 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1487 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1488 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1489 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1490 ehdrC + shdr[i].sh_offset,
1491 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1493 if (shdr[i].sh_type == SHT_REL) {
1494 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1495 } else if (shdr[i].sh_type == SHT_RELA) {
1496 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1498 IF_DEBUG(linker,fprintf(stderr," "));
1501 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1505 IF_DEBUG(linker,belch( "\nString tables" ));
1508 for (i = 0; i < ehdr->e_shnum; i++) {
1509 if (shdr[i].sh_type == SHT_STRTAB &&
1510 i != ehdr->e_shstrndx) {
1511 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1512 strtab = ehdrC + shdr[i].sh_offset;
1517 belch("ocVerifyImage_ELF: no string tables, or too many");
1522 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1523 for (i = 0; i < ehdr->e_shnum; i++) {
1524 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1525 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1527 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1528 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1529 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1531 shdr[i].sh_size % sizeof(Elf32_Sym)
1533 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1534 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1537 for (j = 0; j < nent; j++) {
1538 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1539 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1540 (int)stab[j].st_shndx,
1541 (int)stab[j].st_size,
1542 (char*)stab[j].st_value ));
1544 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1545 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1546 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1547 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1548 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1549 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1550 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1551 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1553 IF_DEBUG(linker,fprintf(stderr, " " ));
1555 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1556 switch (ELF32_ST_BIND(stab[j].st_info)) {
1557 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1558 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1559 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1560 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1562 IF_DEBUG(linker,fprintf(stderr, " " ));
1564 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1568 if (nsymtabs == 0) {
1569 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1578 ocGetNames_ELF ( ObjectCode* oc )
1583 char* ehdrC = (char*)(oc->image);
1584 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1585 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1586 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1587 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1589 ASSERT(symhash != NULL);
1592 belch("ocGetNames_ELF: no strtab");
1597 oc->n_sections = ehdr->e_shnum;
1598 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1599 "ocGetNames_ELF(oc->sections)" );
1601 for (i = 0; i < oc->n_sections; i++) {
1603 /* make a section entry for relevant sections */
1604 SectionKind kind = SECTIONKIND_OTHER;
1605 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1606 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1607 kind = SECTIONKIND_RWDATA;
1608 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1609 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1610 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1611 kind = SECTIONKIND_CODE_OR_RODATA;
1613 /* fill in the section info */
1614 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1615 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1616 oc->sections[i].kind = kind;
1618 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1620 /* copy stuff into this module's object symbol table */
1621 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1622 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1624 oc->n_symbols = nent;
1625 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1626 "ocGetNames_ELF(oc->symbols)");
1628 for (j = 0; j < nent; j++) {
1629 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1630 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1632 /* and not an undefined symbol */
1633 && stab[j].st_shndx != SHN_UNDEF
1634 /* and not in a "special section" */
1635 && stab[j].st_shndx < SHN_LORESERVE
1637 /* and it's a not a section or string table or anything silly */
1638 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1639 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1640 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1643 char* nm = strtab + stab[j].st_name;
1645 + shdr[ stab[j].st_shndx ].sh_offset
1649 oc->symbols[j] = nm;
1650 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1651 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1652 ad, oc->fileName, nm ));
1653 insertStrHashTable(oc->lochash, nm, ad);
1655 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1656 ad, oc->fileName, nm ));
1657 insertStrHashTable(symhash, nm, ad);
1661 IF_DEBUG(linker,belch( "skipping `%s'",
1662 strtab + stab[j].st_name ));
1665 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1666 (int)ELF32_ST_BIND(stab[j].st_info),
1667 (int)ELF32_ST_TYPE(stab[j].st_info),
1668 (int)stab[j].st_shndx,
1669 strtab + stab[j].st_name
1672 oc->symbols[j] = NULL;
1681 /* Do ELF relocations which lack an explicit addend. All x86-linux
1682 relocations appear to be of this form. */
1683 static int do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1684 Elf32_Shdr* shdr, int shnum,
1685 Elf32_Sym* stab, char* strtab )
1690 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1691 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1692 int target_shndx = shdr[shnum].sh_info;
1693 int symtab_shndx = shdr[shnum].sh_link;
1694 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1695 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1696 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1697 target_shndx, symtab_shndx ));
1698 for (j = 0; j < nent; j++) {
1699 Elf32_Addr offset = rtab[j].r_offset;
1700 Elf32_Word info = rtab[j].r_info;
1702 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1703 Elf32_Word* pP = (Elf32_Word*)P;
1707 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1708 j, (void*)offset, (void*)info ));
1710 IF_DEBUG(linker,belch( " ZERO" ));
1713 /* First see if it is a nameless local symbol. */
1714 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1715 symbol = "(noname)";
1717 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1718 + stab[ELF32_R_SYM(info)].st_value);
1720 /* No? Should be in a symbol table then; first try the
1722 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1723 (void*)S = lookupLocalSymbol( oc, symbol );
1724 if ((void*)S == NULL)
1725 (void*)S = lookupSymbol( symbol );
1728 barf("do_Elf32_Rel_relocations: %s: unknown symbol `%s'",
1729 oc->fileName, symbol);
1731 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1733 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1734 (void*)P, (void*)S, (void*)A ));
1735 switch (ELF32_R_TYPE(info)) {
1736 # ifdef i386_TARGET_ARCH
1737 case R_386_32: *pP = S + A; break;
1738 case R_386_PC32: *pP = S + A - P; break;
1741 fprintf(stderr, "unhandled ELF relocation(Rel) type %d\n",
1742 ELF32_R_TYPE(info));
1743 barf("do_Elf32_Rel_relocations: unhandled ELF relocation type");
1752 /* Do ELF relocations for which explicit addends are supplied.
1753 sparc-solaris relocations appear to be of this form. */
1754 static int do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1755 Elf32_Shdr* shdr, int shnum,
1756 Elf32_Sym* stab, char* strtab )
1761 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1762 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1763 int target_shndx = shdr[shnum].sh_info;
1764 int symtab_shndx = shdr[shnum].sh_link;
1765 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1766 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1767 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1768 target_shndx, symtab_shndx ));
1769 for (j = 0; j < nent; j++) {
1770 Elf32_Addr offset = rtab[j].r_offset;
1771 Elf32_Word info = rtab[j].r_info;
1772 Elf32_Sword addend = rtab[j].r_addend;
1774 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1775 Elf32_Addr A = addend;
1777 # if defined(sparc_TARGET_ARCH)
1778 /* This #ifdef only serves to avoid unused-var warnings. */
1779 Elf32_Word* pP = (Elf32_Word*)P;
1783 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1784 j, (void*)offset, (void*)info,
1787 IF_DEBUG(linker,belch( " ZERO" ));
1790 /* First see if it is a nameless local symbol. */
1791 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1792 symbol = "(noname)";
1794 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1795 + stab[ELF32_R_SYM(info)].st_value);
1797 /* No? Should be in a symbol table then; first try the
1799 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1800 (void*)S = lookupLocalSymbol( oc, symbol );
1801 if ((void*)S == NULL)
1802 (void*)S = lookupSymbol( symbol );
1805 barf("do_Elf32_Rela_relocations: %s: unknown symbol `%s'",
1806 oc->fileName, symbol);
1809 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1812 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1814 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1815 (void*)P, (void*)S, (void*)A ));
1816 switch (ELF32_R_TYPE(info)) {
1817 # if defined(sparc_TARGET_ARCH)
1818 case R_SPARC_WDISP30:
1819 w1 = *pP & 0xC0000000;
1820 w2 = (Elf32_Word)((S + A - P) >> 2);
1821 ASSERT((w2 & 0xC0000000) == 0);
1826 w1 = *pP & 0xFFC00000;
1827 w2 = (Elf32_Word)((S + A) >> 10);
1828 ASSERT((w2 & 0xFFC00000) == 0);
1834 w2 = (Elf32_Word)((S + A) & 0x3FF);
1835 ASSERT((w2 & ~0x3FF) == 0);
1840 w2 = (Elf32_Word)(S + A);
1845 fprintf(stderr, "unhandled ELF relocation(RelA) type %d\n",
1846 ELF32_R_TYPE(info));
1847 barf("do_Elf32_Rela_relocations: unhandled ELF relocation type");
1857 ocResolve_ELF ( ObjectCode* oc )
1861 Elf32_Sym* stab = NULL;
1862 char* ehdrC = (char*)(oc->image);
1863 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1864 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1866 /* first find "the" symbol table */
1867 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1869 /* also go find the string table */
1870 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1872 if (stab == NULL || strtab == NULL) {
1873 belch("ocResolve_ELF: can't find string or symbol table");
1877 /* Process the relocation sections. */
1878 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
1879 if (shdr[shnum].sh_type == SHT_REL ) {
1880 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
1881 shnum, stab, strtab );
1885 if (shdr[shnum].sh_type == SHT_RELA) {
1886 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
1887 shnum, stab, strtab );
1892 /* Free the local symbol table; we won't need it again. */
1893 freeHashTable(oc->lochash, NULL);