1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.48 2001/06/29 14:47: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 \
100 SymX(getprotobynumber) \
101 SymX(getprotobyname) \
102 SymX(gethostbyname) \
103 SymX(gethostbyaddr) \
138 Sym(_imp___timezone) \
148 SymX(GetCurrentProcess) \
149 SymX(GetProcessTimes) \
151 SymX(GetExitCodeProcess) \
152 SymX(WaitForSingleObject) \
153 SymX(CreateProcessA) \
158 #define RTS_SYMBOLS \
160 Sym(stg_gc_enter_1) \
167 Sym(stg_gc_unpt_r1) \
168 Sym(stg_gc_unbx_r1) \
173 SymX(stg_update_PAP) \
174 SymX(stg_ap_1_upd_info) \
175 SymX(stg_ap_2_upd_info) \
176 SymX(stg_ap_3_upd_info) \
177 SymX(stg_ap_4_upd_info) \
178 SymX(stg_ap_5_upd_info) \
179 SymX(stg_ap_6_upd_info) \
180 SymX(stg_ap_7_upd_info) \
181 SymX(stg_ap_8_upd_info) \
182 SymX(stg_sel_0_upd_info) \
183 SymX(stg_sel_1_upd_info) \
184 SymX(stg_sel_2_upd_info) \
185 SymX(stg_sel_3_upd_info) \
186 SymX(stg_sel_4_upd_info) \
187 SymX(stg_sel_5_upd_info) \
188 SymX(stg_sel_6_upd_info) \
189 SymX(stg_sel_7_upd_info) \
190 SymX(stg_sel_8_upd_info) \
191 SymX(stg_sel_9_upd_info) \
192 SymX(stg_sel_10_upd_info) \
193 SymX(stg_sel_11_upd_info) \
194 SymX(stg_sel_12_upd_info) \
195 SymX(stg_sel_13_upd_info) \
196 SymX(stg_sel_14_upd_info) \
197 SymX(stg_sel_15_upd_info) \
198 SymX(stg_upd_frame_info) \
199 SymX(stg_seq_frame_info) \
200 SymX(stg_CAF_BLACKHOLE_info) \
201 SymX(stg_IND_STATIC_info) \
202 SymX(stg_EMPTY_MVAR_info) \
203 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
204 SymX(stg_WEAK_info) \
205 SymX(stg_CHARLIKE_closure) \
206 SymX(stg_INTLIKE_closure) \
208 SymX(newBCOzh_fast) \
209 SymX(mkApUpd0zh_fast) \
210 SymX(putMVarzh_fast) \
211 SymX(newMVarzh_fast) \
212 SymX(takeMVarzh_fast) \
213 SymX(tryTakeMVarzh_fast) \
214 SymX(tryPutMVarzh_fast) \
220 SymX(killThreadzh_fast) \
221 SymX(waitReadzh_fast) \
222 SymX(waitWritezh_fast) \
223 SymX(suspendThread) \
225 SymX(stackOverflow) \
226 SymX(int2Integerzh_fast) \
227 SymX(word2Integerzh_fast) \
229 SymX(__encodeDouble) \
230 SymX(decodeDoublezh_fast) \
231 SymX(decodeFloatzh_fast) \
232 SymX(gcdIntegerzh_fast) \
233 SymX(newArrayzh_fast) \
234 SymX(unsafeThawArrayzh_fast) \
235 SymX(newByteArrayzh_fast) \
236 SymX(newMutVarzh_fast) \
237 SymX(quotRemIntegerzh_fast) \
238 SymX(quotIntegerzh_fast) \
239 SymX(remIntegerzh_fast) \
240 SymX(divExactIntegerzh_fast) \
241 SymX(divModIntegerzh_fast) \
242 SymX(timesIntegerzh_fast) \
243 SymX(minusIntegerzh_fast) \
244 SymX(plusIntegerzh_fast) \
245 SymX(andIntegerzh_fast) \
246 SymX(orIntegerzh_fast) \
247 SymX(xorIntegerzh_fast) \
248 SymX(complementIntegerzh_fast) \
250 SymX(blockAsyncExceptionszh_fast) \
251 SymX(unblockAsyncExceptionszh_fast) \
253 SymX(isDoubleInfinite) \
254 SymX(isDoubleDenormalized) \
255 SymX(isDoubleNegativeZero) \
256 SymX(__encodeFloat) \
258 SymX(isFloatInfinite) \
259 SymX(isFloatDenormalized) \
260 SymX(isFloatNegativeZero) \
261 SymX(__int_encodeFloat) \
262 SymX(__int_encodeDouble) \
263 SymX(__gmpz_cmp_si) \
264 SymX(__gmpz_cmp_ui) \
267 SymX(__gmpz_get_si) \
268 SymX(__gmpz_get_ui) \
271 SymX(resetNonBlockingFd) \
274 SymX(stable_ptr_table) \
275 SymX(shutdownHaskellAndExit) \
276 Sym(stg_enterStackTop) \
277 Sym(stg_yield_to_interpreter) \
281 Sym(__init_PrelGHC) \
282 SymX(freeHaskellFunctionPtr) \
285 SymX(NoRunnableThreadsHook) \
286 SymX(StackOverflowHook) \
287 SymX(OutOfHeapHook) \
288 SymX(MallocFailHook) \
289 SymX(PatErrorHdrHook) \
292 SymX(PostTraceHook) \
293 SymX(createAdjustor) \
308 SymX(rts_mkStablePtr) \
317 SymX(rts_getWord32) \
320 SymX(rts_getDouble) \
321 SymX(rts_getStablePtr) \
327 SymX(rts_evalLazyIO) \
328 SymX(rts_checkSchedStatus)
330 #ifndef SUPPORT_LONG_LONGS
331 #define RTS_LONG_LONG_SYMS /* nothing */
333 #define RTS_LONG_LONG_SYMS \
346 SymX(stg_remWord64) \
347 SymX(stg_quotWord64) \
349 SymX(stg_quotInt64) \
350 SymX(stg_negateInt64) \
351 SymX(stg_plusInt64) \
352 SymX(stg_minusInt64) \
353 SymX(stg_timesInt64) \
359 SymX(stg_shiftRL64) \
360 SymX(stg_iShiftL64) \
361 SymX(stg_iShiftRL64) \
362 SymX(stg_iShiftRA64) \
363 SymX(stg_intToInt64) \
364 SymX(stg_int64ToInt) \
365 SymX(stg_int64ToWord64) \
366 SymX(stg_wordToWord64) \
367 SymX(stg_word64ToWord) \
368 SymX(stg_word64ToInt64) \
369 SymX(int64ToIntegerzh_fast) \
370 SymX(word64ToIntegerzh_fast)
371 #endif /* SUPPORT_LONG_LONGS */
373 /* entirely bogus claims about types of these symbols */
374 #define Sym(vvv) extern void (vvv);
375 #define SymX(vvv) /**/
378 RTS_POSIX_ONLY_SYMBOLS
379 RTS_MINGW_ONLY_SYMBOLS
383 #ifdef LEADING_UNDERSCORE
384 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
386 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
389 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
391 #define SymX(vvv) Sym(vvv)
393 static RtsSymbolVal rtsSyms[] = {
396 RTS_POSIX_ONLY_SYMBOLS
397 RTS_MINGW_ONLY_SYMBOLS
398 { 0, 0 } /* sentinel */
401 /* -----------------------------------------------------------------------------
402 * initialize the object linker
404 #if defined(OBJFORMAT_ELF)
405 static void *dl_prog_handle;
413 symhash = allocStrHashTable();
415 /* populate the symbol table with stuff from the RTS */
416 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
417 insertStrHashTable(symhash, sym->lbl, sym->addr);
419 # if defined(OBJFORMAT_ELF)
420 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
424 /* -----------------------------------------------------------------------------
425 * Add a DLL from which symbols may be found. In the ELF case, just
426 * do RTLD_GLOBAL-style add, so no further messing around needs to
427 * happen in order that symbols in the loaded .so are findable --
428 * lookupSymbol() will subsequently see them by dlsym on the program's
429 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
431 * In the PEi386 case, open the DLLs and put handles to them in a
432 * linked list. When looking for a symbol, try all handles in the
436 #if defined(OBJFORMAT_PEi386)
437 /* A record for storing handles into DLLs. */
442 struct _OpenedDLL* next;
447 /* A list thereof. */
448 static OpenedDLL* opened_dlls = NULL;
454 addDLL ( char* path, char* dll_name )
456 # if defined(OBJFORMAT_ELF)
461 if (path == NULL || strlen(path) == 0) {
462 buf = stgMallocBytes(strlen(dll_name) + 10, "addDll");
463 sprintf(buf, "lib%s.so", dll_name);
465 buf = stgMallocBytes(strlen(path) + 1 + strlen(dll_name) + 10, "addDll");
466 sprintf(buf, "%s/lib%s.so", path, dll_name);
468 hdl = dlopen(buf, RTLD_NOW | RTLD_GLOBAL );
471 /* dlopen failed; return a ptr to the error msg. */
473 if (errmsg == NULL) errmsg = "addDLL: unknown error";
480 # elif defined(OBJFORMAT_PEi386)
482 /* Add this DLL to the list of DLLs in which to search for symbols.
483 The path argument is ignored. */
488 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
490 /* See if we've already got it, and ignore if so. */
491 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
492 if (0 == strcmp(o_dll->name, dll_name))
496 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
497 sprintf(buf, "%s.DLL", dll_name);
498 instance = LoadLibrary(buf);
500 if (instance == NULL) {
501 /* LoadLibrary failed; return a ptr to the error msg. */
502 return "addDLL: unknown error";
505 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
506 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
507 strcpy(o_dll->name, dll_name);
508 o_dll->instance = instance;
509 o_dll->next = opened_dlls;
514 barf("addDLL: not implemented on this platform");
518 /* -----------------------------------------------------------------------------
519 * lookup a symbol in the hash table
522 lookupSymbol( char *lbl )
525 ASSERT(symhash != NULL);
526 val = lookupStrHashTable(symhash, lbl);
529 # if defined(OBJFORMAT_ELF)
530 return dlsym(dl_prog_handle, lbl);
531 # elif defined(OBJFORMAT_PEi386)
534 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
535 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
536 sym = GetProcAddress(o_dll->instance, lbl);
537 if (sym != NULL) return sym;
548 lookupLocalSymbol( ObjectCode* oc, char *lbl )
551 val = lookupStrHashTable(oc->lochash, lbl);
561 /* -----------------------------------------------------------------------------
562 * Load an obj (populate the global symbol table, but don't resolve yet)
564 * Returns: 1 if ok, 0 on error.
567 loadObj( char *path )
574 /* fprintf(stderr, "loadObj %s\n", path ); */
576 /* assert that we haven't already loaded this object */
579 for (o = objects; o; o = o->next)
580 ASSERT(strcmp(o->fileName, path));
584 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
586 # if defined(OBJFORMAT_ELF)
587 oc->formatName = "ELF";
588 # elif defined(OBJFORMAT_PEi386)
589 oc->formatName = "PEi386";
592 barf("loadObj: not implemented on this platform");
596 if (r == -1) { return 0; }
598 /* sigh, strdup() isn't a POSIX function, so do it the long way */
599 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
600 strcpy(oc->fileName, path);
602 oc->fileSize = st.st_size;
603 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
606 oc->lochash = allocStrHashTable();
608 /* chain it onto the list of objects */
612 /* load the image into memory */
613 f = fopen(path, "rb");
615 barf("loadObj: can't read `%s'", path);
617 n = fread ( oc->image, 1, oc->fileSize, f );
618 if (n != oc->fileSize) {
620 barf("loadObj: error whilst reading `%s'", path);
623 /* verify the in-memory image */
624 # if defined(OBJFORMAT_ELF)
625 r = ocVerifyImage_ELF ( oc );
626 # elif defined(OBJFORMAT_PEi386)
627 r = ocVerifyImage_PEi386 ( oc );
629 barf("loadObj: no verify method");
631 if (!r) { return r; }
633 /* build the symbol list for this image */
634 # if defined(OBJFORMAT_ELF)
635 r = ocGetNames_ELF ( oc );
636 # elif defined(OBJFORMAT_PEi386)
637 r = ocGetNames_PEi386 ( oc );
639 barf("loadObj: no getNames method");
641 if (!r) { return r; }
643 /* loaded, but not resolved yet */
644 oc->status = OBJECT_LOADED;
649 /* -----------------------------------------------------------------------------
650 * resolve all the currently unlinked objects in memory
652 * Returns: 1 if ok, 0 on error.
660 for (oc = objects; oc; oc = oc->next) {
661 if (oc->status != OBJECT_RESOLVED) {
662 # if defined(OBJFORMAT_ELF)
663 r = ocResolve_ELF ( oc );
664 # elif defined(OBJFORMAT_PEi386)
665 r = ocResolve_PEi386 ( oc );
667 barf("resolveObjs: not implemented on this platform");
669 if (!r) { return r; }
670 oc->status = OBJECT_RESOLVED;
676 /* -----------------------------------------------------------------------------
677 * delete an object from the pool
680 unloadObj( char *path )
682 ObjectCode *oc, *prev;
684 ASSERT(symhash != NULL);
685 ASSERT(objects != NULL);
688 for (oc = objects; oc; prev = oc, oc = oc->next) {
689 if (!strcmp(oc->fileName,path)) {
691 /* Remove all the mappings for the symbols within this
696 for (i = 0; i < oc->n_symbols; i++) {
697 if (oc->symbols[i] != NULL) {
698 removeStrHashTable(symhash, oc->symbols[i], NULL);
706 prev->next = oc->next;
709 /* We're going to leave this in place, in case there are
710 any pointers from the heap into it: */
711 /* free(oc->image); */
715 /* The local hash table should have been freed at the end
716 of the ocResolve_ call on it. */
717 ASSERT(oc->lochash == NULL);
723 belch("unloadObj: can't find `%s' to unload", path);
727 /* --------------------------------------------------------------------------
728 * PEi386 specifics (Win32 targets)
729 * ------------------------------------------------------------------------*/
731 /* The information for this linker comes from
732 Microsoft Portable Executable
733 and Common Object File Format Specification
734 revision 5.1 January 1998
735 which SimonM says comes from the MS Developer Network CDs.
739 #if defined(OBJFORMAT_PEi386)
743 typedef unsigned char UChar;
744 typedef unsigned short UInt16;
745 typedef unsigned int UInt32;
752 UInt16 NumberOfSections;
753 UInt32 TimeDateStamp;
754 UInt32 PointerToSymbolTable;
755 UInt32 NumberOfSymbols;
756 UInt16 SizeOfOptionalHeader;
757 UInt16 Characteristics;
761 #define sizeof_COFF_header 20
768 UInt32 VirtualAddress;
769 UInt32 SizeOfRawData;
770 UInt32 PointerToRawData;
771 UInt32 PointerToRelocations;
772 UInt32 PointerToLinenumbers;
773 UInt16 NumberOfRelocations;
774 UInt16 NumberOfLineNumbers;
775 UInt32 Characteristics;
779 #define sizeof_COFF_section 40
786 UInt16 SectionNumber;
789 UChar NumberOfAuxSymbols;
793 #define sizeof_COFF_symbol 18
798 UInt32 VirtualAddress;
799 UInt32 SymbolTableIndex;
804 #define sizeof_COFF_reloc 10
807 /* From PE spec doc, section 3.3.2 */
808 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
809 windows.h -- for the same purpose, but I want to know what I'm
811 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
812 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
813 #define MYIMAGE_FILE_DLL 0x2000
814 #define MYIMAGE_FILE_SYSTEM 0x1000
815 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
816 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
817 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
819 /* From PE spec doc, section 5.4.2 and 5.4.4 */
820 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
821 #define MYIMAGE_SYM_CLASS_STATIC 3
822 #define MYIMAGE_SYM_UNDEFINED 0
824 /* From PE spec doc, section 4.1 */
825 #define MYIMAGE_SCN_CNT_CODE 0x00000020
826 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
828 /* From PE spec doc, section 5.2.1 */
829 #define MYIMAGE_REL_I386_DIR32 0x0006
830 #define MYIMAGE_REL_I386_REL32 0x0014
833 /* We use myindex to calculate array addresses, rather than
834 simply doing the normal subscript thing. That's because
835 some of the above structs have sizes which are not
836 a whole number of words. GCC rounds their sizes up to a
837 whole number of words, which means that the address calcs
838 arising from using normal C indexing or pointer arithmetic
839 are just plain wrong. Sigh.
842 myindex ( int scale, void* base, int index )
845 ((UChar*)base) + scale * index;
850 printName ( UChar* name, UChar* strtab )
852 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
853 UInt32 strtab_offset = * (UInt32*)(name+4);
854 fprintf ( stderr, "%s", strtab + strtab_offset );
857 for (i = 0; i < 8; i++) {
858 if (name[i] == 0) break;
859 fprintf ( stderr, "%c", name[i] );
866 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
868 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
869 UInt32 strtab_offset = * (UInt32*)(name+4);
870 strncpy ( dst, strtab+strtab_offset, dstSize );
876 if (name[i] == 0) break;
886 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
889 /* If the string is longer than 8 bytes, look in the
890 string table for it -- this will be correctly zero terminated.
892 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
893 UInt32 strtab_offset = * (UInt32*)(name+4);
894 return ((UChar*)strtab) + strtab_offset;
896 /* Otherwise, if shorter than 8 bytes, return the original,
897 which by defn is correctly terminated.
899 if (name[7]==0) return name;
900 /* The annoying case: 8 bytes. Copy into a temporary
901 (which is never freed ...)
903 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
905 strncpy(newstr,name,8);
911 /* Just compares the short names (first 8 chars) */
912 static COFF_section *
913 findPEi386SectionCalled ( ObjectCode* oc, char* name )
917 = (COFF_header*)(oc->image);
920 ((UChar*)(oc->image))
921 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
923 for (i = 0; i < hdr->NumberOfSections; i++) {
926 COFF_section* section_i
928 myindex ( sizeof_COFF_section, sectab, i );
929 n1 = (UChar*) &(section_i->Name);
931 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
932 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
933 n1[6]==n2[6] && n1[7]==n2[7])
942 zapTrailingAtSign ( UChar* sym )
944 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
946 if (sym[0] == 0) return;
948 while (sym[i] != 0) i++;
951 while (j > 0 && my_isdigit(sym[j])) j--;
952 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
958 ocVerifyImage_PEi386 ( ObjectCode* oc )
962 COFF_section* sectab;
965 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
966 hdr = (COFF_header*)(oc->image);
967 sectab = (COFF_section*) (
968 ((UChar*)(oc->image))
969 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
971 symtab = (COFF_symbol*) (
972 ((UChar*)(oc->image))
973 + hdr->PointerToSymbolTable
975 strtab = ((UChar*)symtab)
976 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
978 if (hdr->Machine != 0x14c) {
979 belch("Not x86 PEi386");
982 if (hdr->SizeOfOptionalHeader != 0) {
983 belch("PEi386 with nonempty optional header");
986 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
987 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
988 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
989 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
990 belch("Not a PEi386 object file");
993 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
994 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
995 belch("Invalid PEi386 word size or endiannness: %d",
996 (int)(hdr->Characteristics));
999 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1000 if (* (UInt32*)strtab > 510000) {
1001 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1005 /* No further verification after this point; only debug printing. */
1007 IF_DEBUG(linker, i=1);
1008 if (i == 0) return 1;
1011 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1013 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1015 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1017 fprintf ( stderr, "\n" );
1019 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1021 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1023 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1025 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1027 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1029 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1031 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1033 /* Print the section table. */
1034 fprintf ( stderr, "\n" );
1035 for (i = 0; i < hdr->NumberOfSections; i++) {
1037 COFF_section* sectab_i
1039 myindex ( sizeof_COFF_section, sectab, i );
1046 printName ( sectab_i->Name, strtab );
1055 sectab_i->VirtualSize,
1056 sectab_i->VirtualAddress,
1057 sectab_i->SizeOfRawData,
1058 sectab_i->PointerToRawData,
1059 sectab_i->NumberOfRelocations,
1060 sectab_i->PointerToRelocations
1062 reltab = (COFF_reloc*) (
1063 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1066 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1068 COFF_reloc* rel = (COFF_reloc*)
1069 myindex ( sizeof_COFF_reloc, reltab, j );
1071 " type 0x%-4x vaddr 0x%-8x name `",
1073 rel->VirtualAddress );
1074 sym = (COFF_symbol*)
1075 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1076 printName ( sym->Name, strtab -10 );
1077 fprintf ( stderr, "'\n" );
1079 fprintf ( stderr, "\n" );
1082 fprintf ( stderr, "\n" );
1083 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1084 fprintf ( stderr, "---START of string table---\n");
1085 for (i = 4; i < *(Int32*)strtab; i++) {
1087 fprintf ( stderr, "\n"); else
1088 fprintf( stderr, "%c", strtab[i] );
1090 fprintf ( stderr, "--- END of string table---\n");
1092 fprintf ( stderr, "\n" );
1095 COFF_symbol* symtab_i;
1096 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1097 symtab_i = (COFF_symbol*)
1098 myindex ( sizeof_COFF_symbol, symtab, i );
1104 printName ( symtab_i->Name, strtab );
1113 (Int32)(symtab_i->SectionNumber) - 1,
1114 (UInt32)symtab_i->Type,
1115 (UInt32)symtab_i->StorageClass,
1116 (UInt32)symtab_i->NumberOfAuxSymbols
1118 i += symtab_i->NumberOfAuxSymbols;
1122 fprintf ( stderr, "\n" );
1128 ocGetNames_PEi386 ( ObjectCode* oc )
1131 COFF_section* sectab;
1132 COFF_symbol* symtab;
1139 hdr = (COFF_header*)(oc->image);
1140 sectab = (COFF_section*) (
1141 ((UChar*)(oc->image))
1142 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1144 symtab = (COFF_symbol*) (
1145 ((UChar*)(oc->image))
1146 + hdr->PointerToSymbolTable
1148 strtab = ((UChar*)(oc->image))
1149 + hdr->PointerToSymbolTable
1150 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1152 /* Copy exported symbols into the ObjectCode. */
1154 oc->n_symbols = hdr->NumberOfSymbols;
1155 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1156 "ocGetNames_PEi386(oc->symbols)");
1157 /* Call me paranoid; I don't care. */
1158 for (i = 0; i < oc->n_symbols; i++)
1159 oc->symbols[i] = NULL;
1163 COFF_symbol* symtab_i;
1164 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1165 symtab_i = (COFF_symbol*)
1166 myindex ( sizeof_COFF_symbol, symtab, i );
1168 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL &&
1169 symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1171 /* This symbol is global and defined, viz, exported */
1172 COFF_section* sectabent;
1174 /* cstring_from_COFF_symbol_name always succeeds. */
1175 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1177 /* for MYIMAGE_SYMCLASS_EXTERNAL
1178 && !MYIMAGE_SYM_UNDEFINED,
1179 the address of the symbol is:
1180 address of relevant section + offset in section
1182 sectabent = (COFF_section*)
1183 myindex ( sizeof_COFF_section,
1185 symtab_i->SectionNumber-1 );
1186 addr = ((UChar*)(oc->image))
1187 + (sectabent->PointerToRawData
1189 /* fprintf(stderr,"addSymbol %p `%s'\n", addr,sname); */
1190 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1191 ASSERT(i >= 0 && i < oc->n_symbols);
1192 oc->symbols[i] = sname;
1193 insertStrHashTable(symhash, sname, addr);
1195 i += symtab_i->NumberOfAuxSymbols;
1199 /* Copy section information into the ObjectCode. */
1201 oc->n_sections = hdr->NumberOfSections;
1202 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1203 "ocGetNamesPEi386" );
1205 for (i = 0; i < oc->n_sections; i++) {
1210 = SECTIONKIND_OTHER;
1211 COFF_section* sectab_i
1213 myindex ( sizeof_COFF_section, sectab, i );
1214 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1217 /* I'm sure this is the Right Way to do it. However, the
1218 alternative of testing the sectab_i->Name field seems to
1219 work ok with Cygwin.
1221 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1222 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1223 kind = SECTIONKIND_CODE_OR_RODATA;
1226 if (0==strcmp(".text",sectab_i->Name))
1227 kind = SECTIONKIND_CODE_OR_RODATA;
1228 if (0==strcmp(".data",sectab_i->Name) ||
1229 0==strcmp(".bss",sectab_i->Name))
1230 kind = SECTIONKIND_RWDATA;
1232 start = ((UChar*)(oc->image))
1233 + sectab_i->PointerToRawData;
1235 + sectab_i->SizeOfRawData - 1;
1237 if (kind == SECTIONKIND_OTHER) {
1238 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1242 oc->sections[i].start = start;
1243 oc->sections[i].end = end;
1244 oc->sections[i].kind = kind;
1252 ocResolve_PEi386 ( ObjectCode* oc )
1255 COFF_section* sectab;
1256 COFF_symbol* symtab;
1265 /* ToDo: should be variable-sized? But is at least safe in the
1266 sense of buffer-overrun-proof. */
1268 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1270 hdr = (COFF_header*)(oc->image);
1271 sectab = (COFF_section*) (
1272 ((UChar*)(oc->image))
1273 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1275 symtab = (COFF_symbol*) (
1276 ((UChar*)(oc->image))
1277 + hdr->PointerToSymbolTable
1279 strtab = ((UChar*)(oc->image))
1280 + hdr->PointerToSymbolTable
1281 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1283 for (i = 0; i < hdr->NumberOfSections; i++) {
1284 COFF_section* sectab_i
1286 myindex ( sizeof_COFF_section, sectab, i );
1289 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1291 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1293 COFF_reloc* reltab_j
1295 myindex ( sizeof_COFF_reloc, reltab, j );
1297 /* the location to patch */
1299 ((UChar*)(oc->image))
1300 + (sectab_i->PointerToRawData
1301 + reltab_j->VirtualAddress
1302 - sectab_i->VirtualAddress )
1304 /* the existing contents of pP */
1306 /* the symbol to connect to */
1307 sym = (COFF_symbol*)
1308 myindex ( sizeof_COFF_symbol,
1309 symtab, reltab_j->SymbolTableIndex );
1312 "reloc sec %2d num %3d: type 0x%-4x "
1313 "vaddr 0x%-8x name `",
1315 (UInt32)reltab_j->Type,
1316 reltab_j->VirtualAddress );
1317 printName ( sym->Name, strtab );
1318 fprintf ( stderr, "'\n" ));
1320 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1321 COFF_section* section_sym
1322 = findPEi386SectionCalled ( oc, sym->Name );
1324 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
1325 barf("Can't find abovementioned PEi386 section");
1328 S = ((UInt32)(oc->image))
1329 + (section_sym->PointerToRawData
1332 copyName ( sym->Name, strtab, symbol, 1000-1 );
1333 zapTrailingAtSign ( symbol );
1334 (void*)S = lookupLocalSymbol( oc, symbol );
1335 if ((void*)S == NULL)
1336 (void*)S = lookupSymbol( symbol );
1338 belch("ocResolve_PEi386: %s: unknown symbol `%s'",
1339 oc->fileName, symbol);
1344 switch (reltab_j->Type) {
1345 case MYIMAGE_REL_I386_DIR32:
1348 case MYIMAGE_REL_I386_REL32:
1349 /* Tricky. We have to insert a displacement at
1350 pP which, when added to the PC for the _next_
1351 insn, gives the address of the target (S).
1352 Problem is to know the address of the next insn
1353 when we only know pP. We assume that this
1354 literal field is always the last in the insn,
1355 so that the address of the next insn is pP+4
1356 -- hence the constant 4.
1357 Also I don't know if A should be added, but so
1358 far it has always been zero.
1361 *pP = S - ((UInt32)pP) - 4;
1365 "unhandled PEi386 relocation type %d\n",
1367 barf("unhandled PEi386 relocation type");
1374 /* fprintf(stderr, "completed %s\n", oc->fileName); */
1378 #endif /* defined(OBJFORMAT_PEi386) */
1381 /* --------------------------------------------------------------------------
1383 * ------------------------------------------------------------------------*/
1385 #if defined(OBJFORMAT_ELF)
1390 #if defined(sparc_TARGET_ARCH)
1391 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1397 findElfSection ( void* objImage, Elf32_Word sh_type )
1400 char* ehdrC = (char*)objImage;
1401 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1402 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1404 for (i = 0; i < ehdr->e_shnum; i++) {
1405 if (shdr[i].sh_type == sh_type &&
1406 i != ehdr->e_shstrndx) {
1407 ptr = ehdrC + shdr[i].sh_offset;
1416 ocVerifyImage_ELF ( ObjectCode* oc )
1420 int i, j, nent, nstrtab, nsymtabs;
1424 char* ehdrC = (char*)(oc->image);
1425 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1427 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1428 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1429 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1430 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1431 belch("ocVerifyImage_ELF: not an ELF header");
1434 IF_DEBUG(linker,belch( "Is an ELF header" ));
1436 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1437 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1441 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1443 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1444 IF_DEBUG(linker,belch( "Is little-endian" ));
1446 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1447 IF_DEBUG(linker,belch( "Is big-endian" ));
1449 belch("ocVerifyImage_ELF: unknown endiannness");
1453 if (ehdr->e_type != ET_REL) {
1454 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1457 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1459 IF_DEBUG(linker,belch( "Architecture is " ));
1460 switch (ehdr->e_machine) {
1461 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1462 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1463 default: IF_DEBUG(linker,belch( "unknown" ));
1464 belch("ocVerifyImage_ELF: unknown architecture");
1468 IF_DEBUG(linker,belch(
1469 "\nSection header table: start %d, n_entries %d, ent_size %d",
1470 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1472 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1474 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1476 if (ehdr->e_shstrndx == SHN_UNDEF) {
1477 belch("ocVerifyImage_ELF: no section header string table");
1480 IF_DEBUG(linker,belch( "Section header string table is section %d",
1482 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1485 for (i = 0; i < ehdr->e_shnum; i++) {
1486 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1487 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1488 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1489 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1490 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1491 ehdrC + shdr[i].sh_offset,
1492 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1494 if (shdr[i].sh_type == SHT_REL) {
1495 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1496 } else if (shdr[i].sh_type == SHT_RELA) {
1497 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1499 IF_DEBUG(linker,fprintf(stderr," "));
1502 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1506 IF_DEBUG(linker,belch( "\nString tables" ));
1509 for (i = 0; i < ehdr->e_shnum; i++) {
1510 if (shdr[i].sh_type == SHT_STRTAB &&
1511 i != ehdr->e_shstrndx) {
1512 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1513 strtab = ehdrC + shdr[i].sh_offset;
1518 belch("ocVerifyImage_ELF: no string tables, or too many");
1523 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1524 for (i = 0; i < ehdr->e_shnum; i++) {
1525 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1526 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1528 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1529 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1530 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1532 shdr[i].sh_size % sizeof(Elf32_Sym)
1534 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1535 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1538 for (j = 0; j < nent; j++) {
1539 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1540 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1541 (int)stab[j].st_shndx,
1542 (int)stab[j].st_size,
1543 (char*)stab[j].st_value ));
1545 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1546 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1547 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1548 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1549 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1550 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1551 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1552 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1554 IF_DEBUG(linker,fprintf(stderr, " " ));
1556 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1557 switch (ELF32_ST_BIND(stab[j].st_info)) {
1558 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1559 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1560 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1561 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1563 IF_DEBUG(linker,fprintf(stderr, " " ));
1565 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1569 if (nsymtabs == 0) {
1570 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1579 ocGetNames_ELF ( ObjectCode* oc )
1584 char* ehdrC = (char*)(oc->image);
1585 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1586 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1587 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1588 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1590 ASSERT(symhash != NULL);
1593 belch("ocGetNames_ELF: no strtab");
1598 oc->n_sections = ehdr->e_shnum;
1599 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1600 "ocGetNames_ELF(oc->sections)" );
1602 for (i = 0; i < oc->n_sections; i++) {
1604 /* make a section entry for relevant sections */
1605 SectionKind kind = SECTIONKIND_OTHER;
1606 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1607 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1608 kind = SECTIONKIND_RWDATA;
1609 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1610 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1611 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1612 kind = SECTIONKIND_CODE_OR_RODATA;
1614 /* fill in the section info */
1615 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1616 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1617 oc->sections[i].kind = kind;
1619 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1621 /* copy stuff into this module's object symbol table */
1622 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1623 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1625 oc->n_symbols = nent;
1626 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1627 "ocGetNames_ELF(oc->symbols)");
1629 for (j = 0; j < nent; j++) {
1630 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1631 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1633 /* and not an undefined symbol */
1634 && stab[j].st_shndx != SHN_UNDEF
1635 /* and not in a "special section" */
1636 && stab[j].st_shndx < SHN_LORESERVE
1638 /* and it's a not a section or string table or anything silly */
1639 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1640 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1641 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1644 char* nm = strtab + stab[j].st_name;
1646 + shdr[ stab[j].st_shndx ].sh_offset
1650 oc->symbols[j] = nm;
1651 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1652 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1653 ad, oc->fileName, nm ));
1654 insertStrHashTable(oc->lochash, nm, ad);
1656 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1657 ad, oc->fileName, nm ));
1658 insertStrHashTable(symhash, nm, ad);
1662 IF_DEBUG(linker,belch( "skipping `%s'",
1663 strtab + stab[j].st_name ));
1666 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1667 (int)ELF32_ST_BIND(stab[j].st_info),
1668 (int)ELF32_ST_TYPE(stab[j].st_info),
1669 (int)stab[j].st_shndx,
1670 strtab + stab[j].st_name
1673 oc->symbols[j] = NULL;
1682 /* Do ELF relocations which lack an explicit addend. All x86-linux
1683 relocations appear to be of this form. */
1684 static int do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1685 Elf32_Shdr* shdr, int shnum,
1686 Elf32_Sym* stab, char* strtab )
1691 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1692 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1693 int target_shndx = shdr[shnum].sh_info;
1694 int symtab_shndx = shdr[shnum].sh_link;
1695 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1696 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1697 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1698 target_shndx, symtab_shndx ));
1699 for (j = 0; j < nent; j++) {
1700 Elf32_Addr offset = rtab[j].r_offset;
1701 Elf32_Word info = rtab[j].r_info;
1703 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1704 Elf32_Word* pP = (Elf32_Word*)P;
1708 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1709 j, (void*)offset, (void*)info ));
1711 IF_DEBUG(linker,belch( " ZERO" ));
1714 /* First see if it is a nameless local symbol. */
1715 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1716 symbol = "(noname)";
1718 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1719 + stab[ELF32_R_SYM(info)].st_value);
1721 /* No? Should be in a symbol table then; first try the
1723 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1724 (void*)S = lookupLocalSymbol( oc, symbol );
1725 if ((void*)S == NULL)
1726 (void*)S = lookupSymbol( symbol );
1729 barf("do_Elf32_Rel_relocations: %s: unknown symbol `%s'",
1730 oc->fileName, symbol);
1732 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1734 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1735 (void*)P, (void*)S, (void*)A ));
1736 switch (ELF32_R_TYPE(info)) {
1737 # ifdef i386_TARGET_ARCH
1738 case R_386_32: *pP = S + A; break;
1739 case R_386_PC32: *pP = S + A - P; break;
1742 fprintf(stderr, "unhandled ELF relocation(Rel) type %d\n",
1743 ELF32_R_TYPE(info));
1744 barf("do_Elf32_Rel_relocations: unhandled ELF relocation type");
1753 /* Do ELF relocations for which explicit addends are supplied.
1754 sparc-solaris relocations appear to be of this form. */
1755 static int do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1756 Elf32_Shdr* shdr, int shnum,
1757 Elf32_Sym* stab, char* strtab )
1762 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1763 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1764 int target_shndx = shdr[shnum].sh_info;
1765 int symtab_shndx = shdr[shnum].sh_link;
1766 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1767 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1768 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1769 target_shndx, symtab_shndx ));
1770 for (j = 0; j < nent; j++) {
1771 Elf32_Addr offset = rtab[j].r_offset;
1772 Elf32_Word info = rtab[j].r_info;
1773 Elf32_Sword addend = rtab[j].r_addend;
1775 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1776 Elf32_Addr A = addend;
1778 # if defined(sparc_TARGET_ARCH)
1779 /* This #ifdef only serves to avoid unused-var warnings. */
1780 Elf32_Word* pP = (Elf32_Word*)P;
1784 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1785 j, (void*)offset, (void*)info,
1788 IF_DEBUG(linker,belch( " ZERO" ));
1791 /* First see if it is a nameless local symbol. */
1792 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1793 symbol = "(noname)";
1795 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1796 + stab[ELF32_R_SYM(info)].st_value);
1798 /* No? Should be in a symbol table then; first try the
1800 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1801 (void*)S = lookupLocalSymbol( oc, symbol );
1802 if ((void*)S == NULL)
1803 (void*)S = lookupSymbol( symbol );
1806 barf("do_Elf32_Rela_relocations: %s: unknown symbol `%s'",
1807 oc->fileName, symbol);
1810 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1813 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1815 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1816 (void*)P, (void*)S, (void*)A ));
1817 switch (ELF32_R_TYPE(info)) {
1818 # if defined(sparc_TARGET_ARCH)
1819 case R_SPARC_WDISP30:
1820 w1 = *pP & 0xC0000000;
1821 w2 = (Elf32_Word)((S + A - P) >> 2);
1822 ASSERT((w2 & 0xC0000000) == 0);
1827 w1 = *pP & 0xFFC00000;
1828 w2 = (Elf32_Word)((S + A) >> 10);
1829 ASSERT((w2 & 0xFFC00000) == 0);
1835 w2 = (Elf32_Word)((S + A) & 0x3FF);
1836 ASSERT((w2 & ~0x3FF) == 0);
1841 w2 = (Elf32_Word)(S + A);
1846 fprintf(stderr, "unhandled ELF relocation(RelA) type %d\n",
1847 ELF32_R_TYPE(info));
1848 barf("do_Elf32_Rela_relocations: unhandled ELF relocation type");
1858 ocResolve_ELF ( ObjectCode* oc )
1862 Elf32_Sym* stab = NULL;
1863 char* ehdrC = (char*)(oc->image);
1864 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1865 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1867 /* first find "the" symbol table */
1868 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1870 /* also go find the string table */
1871 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1873 if (stab == NULL || strtab == NULL) {
1874 belch("ocResolve_ELF: can't find string or symbol table");
1878 /* Process the relocation sections. */
1879 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
1880 if (shdr[shnum].sh_type == SHT_REL ) {
1881 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
1882 shnum, stab, strtab );
1886 if (shdr[shnum].sh_type == SHT_RELA) {
1887 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
1888 shnum, stab, strtab );
1893 /* Free the local symbol table; we won't need it again. */
1894 freeHashTable(oc->lochash, NULL);