1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.56 2001/08/13 14:34:40 simonmar 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) \
162 #define RTS_SYMBOLS \
164 Sym(stg_gc_enter_1) \
171 Sym(stg_gc_unpt_r1) \
172 Sym(stg_gc_unbx_r1) \
177 SymX(stg_update_PAP) \
178 SymX(stg_ap_1_upd_info) \
179 SymX(stg_ap_2_upd_info) \
180 SymX(stg_ap_3_upd_info) \
181 SymX(stg_ap_4_upd_info) \
182 SymX(stg_ap_5_upd_info) \
183 SymX(stg_ap_6_upd_info) \
184 SymX(stg_ap_7_upd_info) \
185 SymX(stg_ap_8_upd_info) \
186 SymX(stg_sel_0_upd_info) \
187 SymX(stg_sel_1_upd_info) \
188 SymX(stg_sel_2_upd_info) \
189 SymX(stg_sel_3_upd_info) \
190 SymX(stg_sel_4_upd_info) \
191 SymX(stg_sel_5_upd_info) \
192 SymX(stg_sel_6_upd_info) \
193 SymX(stg_sel_7_upd_info) \
194 SymX(stg_sel_8_upd_info) \
195 SymX(stg_sel_9_upd_info) \
196 SymX(stg_sel_10_upd_info) \
197 SymX(stg_sel_11_upd_info) \
198 SymX(stg_sel_12_upd_info) \
199 SymX(stg_sel_13_upd_info) \
200 SymX(stg_sel_14_upd_info) \
201 SymX(stg_sel_15_upd_info) \
202 SymX(stg_upd_frame_info) \
203 SymX(stg_seq_frame_info) \
204 SymX(stg_CAF_BLACKHOLE_info) \
205 SymX(stg_IND_STATIC_info) \
206 SymX(stg_EMPTY_MVAR_info) \
207 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
208 SymX(stg_WEAK_info) \
209 SymX(stg_CHARLIKE_closure) \
210 SymX(stg_INTLIKE_closure) \
212 SymX(newBCOzh_fast) \
213 SymX(mkApUpd0zh_fast) \
214 SymX(putMVarzh_fast) \
215 SymX(newMVarzh_fast) \
216 SymX(takeMVarzh_fast) \
217 SymX(tryTakeMVarzh_fast) \
218 SymX(tryPutMVarzh_fast) \
224 SymX(killThreadzh_fast) \
225 SymX(waitReadzh_fast) \
226 SymX(waitWritezh_fast) \
227 SymX(suspendThread) \
229 SymX(stackOverflow) \
230 SymX(int2Integerzh_fast) \
231 SymX(word2Integerzh_fast) \
233 SymX(__encodeDouble) \
234 SymX(decodeDoublezh_fast) \
235 SymX(decodeFloatzh_fast) \
236 SymX(gcdIntegerzh_fast) \
237 SymX(newArrayzh_fast) \
238 SymX(unsafeThawArrayzh_fast) \
239 SymX(newByteArrayzh_fast) \
240 SymX(newPinnedByteArrayzh_fast) \
241 SymX(newMutVarzh_fast) \
242 SymX(quotRemIntegerzh_fast) \
243 SymX(quotIntegerzh_fast) \
244 SymX(remIntegerzh_fast) \
245 SymX(divExactIntegerzh_fast) \
246 SymX(divModIntegerzh_fast) \
247 SymX(timesIntegerzh_fast) \
248 SymX(minusIntegerzh_fast) \
249 SymX(plusIntegerzh_fast) \
250 SymX(andIntegerzh_fast) \
251 SymX(orIntegerzh_fast) \
252 SymX(xorIntegerzh_fast) \
253 SymX(complementIntegerzh_fast) \
255 SymX(blockAsyncExceptionszh_fast) \
256 SymX(unblockAsyncExceptionszh_fast) \
258 SymX(isDoubleInfinite) \
259 SymX(isDoubleDenormalized) \
260 SymX(isDoubleNegativeZero) \
261 SymX(__encodeFloat) \
263 SymX(isFloatInfinite) \
264 SymX(isFloatDenormalized) \
265 SymX(isFloatNegativeZero) \
266 SymX(__int_encodeFloat) \
267 SymX(__int_encodeDouble) \
268 SymX(__gmpz_cmp_si) \
269 SymX(__gmpz_cmp_ui) \
272 SymX(__gmpz_get_si) \
273 SymX(__gmpz_get_ui) \
276 SymX(resetNonBlockingFd) \
279 SymX(stable_ptr_table) \
280 SymX(shutdownHaskellAndExit) \
281 Sym(stg_enterStackTop) \
282 Sym(stg_yield_to_interpreter) \
286 Sym(__init_PrelGHC) \
287 SymX(freeHaskellFunctionPtr) \
290 SymX(NoRunnableThreadsHook) \
291 SymX(StackOverflowHook) \
292 SymX(OutOfHeapHook) \
293 SymX(MallocFailHook) \
294 SymX(PatErrorHdrHook) \
297 SymX(PostTraceHook) \
298 SymX(createAdjustor) \
313 SymX(rts_mkStablePtr) \
322 SymX(rts_getWord32) \
325 SymX(rts_getDouble) \
326 SymX(rts_getStablePtr) \
332 SymX(rts_evalLazyIO) \
333 SymX(rts_checkSchedStatus)
335 #ifndef SUPPORT_LONG_LONGS
336 #define RTS_LONG_LONG_SYMS /* nothing */
338 #define RTS_LONG_LONG_SYMS \
339 SymX(int64ToIntegerzh_fast) \
340 SymX(word64ToIntegerzh_fast)
341 #endif /* SUPPORT_LONG_LONGS */
343 /* entirely bogus claims about types of these symbols */
344 #define Sym(vvv) extern void (vvv);
345 #define SymX(vvv) /**/
348 RTS_POSIX_ONLY_SYMBOLS
349 RTS_MINGW_ONLY_SYMBOLS
353 #ifdef LEADING_UNDERSCORE
354 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
356 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
359 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
361 #define SymX(vvv) Sym(vvv)
363 static RtsSymbolVal rtsSyms[] = {
366 RTS_POSIX_ONLY_SYMBOLS
367 RTS_MINGW_ONLY_SYMBOLS
368 { 0, 0 } /* sentinel */
371 /* -----------------------------------------------------------------------------
372 * initialize the object linker
374 #if defined(OBJFORMAT_ELF)
375 static void *dl_prog_handle;
383 symhash = allocStrHashTable();
385 /* populate the symbol table with stuff from the RTS */
386 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
387 insertStrHashTable(symhash, sym->lbl, sym->addr);
389 # if defined(OBJFORMAT_ELF)
390 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
394 /* -----------------------------------------------------------------------------
395 * Add a DLL from which symbols may be found. In the ELF case, just
396 * do RTLD_GLOBAL-style add, so no further messing around needs to
397 * happen in order that symbols in the loaded .so are findable --
398 * lookupSymbol() will subsequently see them by dlsym on the program's
399 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
401 * In the PEi386 case, open the DLLs and put handles to them in a
402 * linked list. When looking for a symbol, try all handles in the
406 #if defined(OBJFORMAT_PEi386)
407 /* A record for storing handles into DLLs. */
412 struct _OpenedDLL* next;
417 /* A list thereof. */
418 static OpenedDLL* opened_dlls = NULL;
424 addDLL ( char* path, char* dll_name )
426 # if defined(OBJFORMAT_ELF)
431 if (path == NULL || strlen(path) == 0) {
432 buf = stgMallocBytes(strlen(dll_name) + 10, "addDll");
433 sprintf(buf, "lib%s.so", dll_name);
435 buf = stgMallocBytes(strlen(path) + 1 + strlen(dll_name) + 10, "addDll");
436 sprintf(buf, "%s/lib%s.so", path, dll_name);
438 hdl = dlopen(buf, RTLD_NOW | RTLD_GLOBAL );
441 /* dlopen failed; return a ptr to the error msg. */
443 if (errmsg == NULL) errmsg = "addDLL: unknown error";
450 # elif defined(OBJFORMAT_PEi386)
452 /* Add this DLL to the list of DLLs in which to search for symbols.
453 The path argument is ignored. */
458 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
460 /* See if we've already got it, and ignore if so. */
461 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
462 if (0 == strcmp(o_dll->name, dll_name))
466 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
467 sprintf(buf, "%s.DLL", dll_name);
468 instance = LoadLibrary(buf);
470 if (instance == NULL) {
471 /* LoadLibrary failed; return a ptr to the error msg. */
472 return "addDLL: unknown error";
475 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
476 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
477 strcpy(o_dll->name, dll_name);
478 o_dll->instance = instance;
479 o_dll->next = opened_dlls;
484 barf("addDLL: not implemented on this platform");
488 /* -----------------------------------------------------------------------------
489 * lookup a symbol in the hash table
492 lookupSymbol( char *lbl )
495 ASSERT(symhash != NULL);
496 val = lookupStrHashTable(symhash, lbl);
499 # if defined(OBJFORMAT_ELF)
500 return dlsym(dl_prog_handle, lbl);
501 # elif defined(OBJFORMAT_PEi386)
504 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
505 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
507 /* HACK: if the name has an initial underscore, try stripping
508 it off & look that up first. I've yet to verify whether there's
509 a Rule that governs whether an initial '_' *should always* be
510 stripped off when mapping from import lib name to the DLL name.
512 sym = GetProcAddress(o_dll->instance, (lbl+1));
513 if (sym != NULL) return sym;
515 sym = GetProcAddress(o_dll->instance, lbl);
516 if (sym != NULL) return sym;
530 lookupLocalSymbol( ObjectCode* oc, char *lbl )
533 val = lookupStrHashTable(oc->lochash, lbl);
543 /* -----------------------------------------------------------------------------
544 * Load an obj (populate the global symbol table, but don't resolve yet)
546 * Returns: 1 if ok, 0 on error.
549 loadObj( char *path )
556 /* fprintf(stderr, "loadObj %s\n", path ); */
558 /* assert that we haven't already loaded this object */
561 for (o = objects; o; o = o->next)
562 ASSERT(strcmp(o->fileName, path));
566 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
568 # if defined(OBJFORMAT_ELF)
569 oc->formatName = "ELF";
570 # elif defined(OBJFORMAT_PEi386)
571 oc->formatName = "PEi386";
574 barf("loadObj: not implemented on this platform");
578 if (r == -1) { return 0; }
580 /* sigh, strdup() isn't a POSIX function, so do it the long way */
581 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
582 strcpy(oc->fileName, path);
584 oc->fileSize = st.st_size;
585 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
588 oc->lochash = allocStrHashTable();
590 /* chain it onto the list of objects */
594 /* load the image into memory */
595 f = fopen(path, "rb");
597 barf("loadObj: can't read `%s'", path);
599 n = fread ( oc->image, 1, oc->fileSize, f );
600 if (n != oc->fileSize) {
602 barf("loadObj: error whilst reading `%s'", path);
605 /* verify the in-memory image */
606 # if defined(OBJFORMAT_ELF)
607 r = ocVerifyImage_ELF ( oc );
608 # elif defined(OBJFORMAT_PEi386)
609 r = ocVerifyImage_PEi386 ( oc );
611 barf("loadObj: no verify method");
613 if (!r) { return r; }
615 /* build the symbol list for this image */
616 # if defined(OBJFORMAT_ELF)
617 r = ocGetNames_ELF ( oc );
618 # elif defined(OBJFORMAT_PEi386)
619 r = ocGetNames_PEi386 ( oc );
621 barf("loadObj: no getNames method");
623 if (!r) { return r; }
625 /* loaded, but not resolved yet */
626 oc->status = OBJECT_LOADED;
631 /* -----------------------------------------------------------------------------
632 * resolve all the currently unlinked objects in memory
634 * Returns: 1 if ok, 0 on error.
642 for (oc = objects; oc; oc = oc->next) {
643 if (oc->status != OBJECT_RESOLVED) {
644 # if defined(OBJFORMAT_ELF)
645 r = ocResolve_ELF ( oc );
646 # elif defined(OBJFORMAT_PEi386)
647 r = ocResolve_PEi386 ( oc );
649 barf("resolveObjs: not implemented on this platform");
651 if (!r) { return r; }
652 oc->status = OBJECT_RESOLVED;
658 /* -----------------------------------------------------------------------------
659 * delete an object from the pool
662 unloadObj( char *path )
664 ObjectCode *oc, *prev;
666 ASSERT(symhash != NULL);
667 ASSERT(objects != NULL);
670 for (oc = objects; oc; prev = oc, oc = oc->next) {
671 if (!strcmp(oc->fileName,path)) {
673 /* Remove all the mappings for the symbols within this
678 for (i = 0; i < oc->n_symbols; i++) {
679 if (oc->symbols[i] != NULL) {
680 removeStrHashTable(symhash, oc->symbols[i], NULL);
688 prev->next = oc->next;
691 /* We're going to leave this in place, in case there are
692 any pointers from the heap into it: */
693 /* free(oc->image); */
697 /* The local hash table should have been freed at the end
698 of the ocResolve_ call on it. */
699 ASSERT(oc->lochash == NULL);
705 belch("unloadObj: can't find `%s' to unload", path);
709 /* --------------------------------------------------------------------------
710 * PEi386 specifics (Win32 targets)
711 * ------------------------------------------------------------------------*/
713 /* The information for this linker comes from
714 Microsoft Portable Executable
715 and Common Object File Format Specification
716 revision 5.1 January 1998
717 which SimonM says comes from the MS Developer Network CDs.
721 #if defined(OBJFORMAT_PEi386)
725 typedef unsigned char UChar;
726 typedef unsigned short UInt16;
727 typedef unsigned int UInt32;
734 UInt16 NumberOfSections;
735 UInt32 TimeDateStamp;
736 UInt32 PointerToSymbolTable;
737 UInt32 NumberOfSymbols;
738 UInt16 SizeOfOptionalHeader;
739 UInt16 Characteristics;
743 #define sizeof_COFF_header 20
750 UInt32 VirtualAddress;
751 UInt32 SizeOfRawData;
752 UInt32 PointerToRawData;
753 UInt32 PointerToRelocations;
754 UInt32 PointerToLinenumbers;
755 UInt16 NumberOfRelocations;
756 UInt16 NumberOfLineNumbers;
757 UInt32 Characteristics;
761 #define sizeof_COFF_section 40
768 UInt16 SectionNumber;
771 UChar NumberOfAuxSymbols;
775 #define sizeof_COFF_symbol 18
780 UInt32 VirtualAddress;
781 UInt32 SymbolTableIndex;
786 #define sizeof_COFF_reloc 10
789 /* From PE spec doc, section 3.3.2 */
790 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
791 windows.h -- for the same purpose, but I want to know what I'm
793 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
794 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
795 #define MYIMAGE_FILE_DLL 0x2000
796 #define MYIMAGE_FILE_SYSTEM 0x1000
797 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
798 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
799 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
801 /* From PE spec doc, section 5.4.2 and 5.4.4 */
802 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
803 #define MYIMAGE_SYM_CLASS_STATIC 3
804 #define MYIMAGE_SYM_UNDEFINED 0
806 /* From PE spec doc, section 4.1 */
807 #define MYIMAGE_SCN_CNT_CODE 0x00000020
808 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
810 /* From PE spec doc, section 5.2.1 */
811 #define MYIMAGE_REL_I386_DIR32 0x0006
812 #define MYIMAGE_REL_I386_REL32 0x0014
815 /* We use myindex to calculate array addresses, rather than
816 simply doing the normal subscript thing. That's because
817 some of the above structs have sizes which are not
818 a whole number of words. GCC rounds their sizes up to a
819 whole number of words, which means that the address calcs
820 arising from using normal C indexing or pointer arithmetic
821 are just plain wrong. Sigh.
824 myindex ( int scale, void* base, int index )
827 ((UChar*)base) + scale * index;
832 printName ( UChar* name, UChar* strtab )
834 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
835 UInt32 strtab_offset = * (UInt32*)(name+4);
836 fprintf ( stderr, "%s", strtab + strtab_offset );
839 for (i = 0; i < 8; i++) {
840 if (name[i] == 0) break;
841 fprintf ( stderr, "%c", name[i] );
848 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
850 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
851 UInt32 strtab_offset = * (UInt32*)(name+4);
852 strncpy ( dst, strtab+strtab_offset, dstSize );
858 if (name[i] == 0) break;
868 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
871 /* If the string is longer than 8 bytes, look in the
872 string table for it -- this will be correctly zero terminated.
874 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
875 UInt32 strtab_offset = * (UInt32*)(name+4);
876 return ((UChar*)strtab) + strtab_offset;
878 /* Otherwise, if shorter than 8 bytes, return the original,
879 which by defn is correctly terminated.
881 if (name[7]==0) return name;
882 /* The annoying case: 8 bytes. Copy into a temporary
883 (which is never freed ...)
885 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
887 strncpy(newstr,name,8);
893 /* Just compares the short names (first 8 chars) */
894 static COFF_section *
895 findPEi386SectionCalled ( ObjectCode* oc, char* name )
899 = (COFF_header*)(oc->image);
902 ((UChar*)(oc->image))
903 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
905 for (i = 0; i < hdr->NumberOfSections; i++) {
908 COFF_section* section_i
910 myindex ( sizeof_COFF_section, sectab, i );
911 n1 = (UChar*) &(section_i->Name);
913 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
914 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
915 n1[6]==n2[6] && n1[7]==n2[7])
924 zapTrailingAtSign ( UChar* sym )
926 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
928 if (sym[0] == 0) return;
930 while (sym[i] != 0) i++;
933 while (j > 0 && my_isdigit(sym[j])) j--;
934 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
940 ocVerifyImage_PEi386 ( ObjectCode* oc )
944 COFF_section* sectab;
947 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
948 hdr = (COFF_header*)(oc->image);
949 sectab = (COFF_section*) (
950 ((UChar*)(oc->image))
951 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
953 symtab = (COFF_symbol*) (
954 ((UChar*)(oc->image))
955 + hdr->PointerToSymbolTable
957 strtab = ((UChar*)symtab)
958 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
960 if (hdr->Machine != 0x14c) {
961 belch("Not x86 PEi386");
964 if (hdr->SizeOfOptionalHeader != 0) {
965 belch("PEi386 with nonempty optional header");
968 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
969 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
970 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
971 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
972 belch("Not a PEi386 object file");
975 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
976 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
977 belch("Invalid PEi386 word size or endiannness: %d",
978 (int)(hdr->Characteristics));
981 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
982 if (* (UInt32*)strtab > 510000) {
983 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
987 /* No further verification after this point; only debug printing. */
989 IF_DEBUG(linker, i=1);
990 if (i == 0) return 1;
993 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
995 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
997 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
999 fprintf ( stderr, "\n" );
1001 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1003 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1005 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1007 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1009 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1011 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1013 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1015 /* Print the section table. */
1016 fprintf ( stderr, "\n" );
1017 for (i = 0; i < hdr->NumberOfSections; i++) {
1019 COFF_section* sectab_i
1021 myindex ( sizeof_COFF_section, sectab, i );
1028 printName ( sectab_i->Name, strtab );
1037 sectab_i->VirtualSize,
1038 sectab_i->VirtualAddress,
1039 sectab_i->SizeOfRawData,
1040 sectab_i->PointerToRawData,
1041 sectab_i->NumberOfRelocations,
1042 sectab_i->PointerToRelocations
1044 reltab = (COFF_reloc*) (
1045 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1048 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1050 COFF_reloc* rel = (COFF_reloc*)
1051 myindex ( sizeof_COFF_reloc, reltab, j );
1053 " type 0x%-4x vaddr 0x%-8x name `",
1055 rel->VirtualAddress );
1056 sym = (COFF_symbol*)
1057 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1058 printName ( sym->Name, strtab -10 );
1059 fprintf ( stderr, "'\n" );
1061 fprintf ( stderr, "\n" );
1064 fprintf ( stderr, "\n" );
1065 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1066 fprintf ( stderr, "---START of string table---\n");
1067 for (i = 4; i < *(Int32*)strtab; i++) {
1069 fprintf ( stderr, "\n"); else
1070 fprintf( stderr, "%c", strtab[i] );
1072 fprintf ( stderr, "--- END of string table---\n");
1074 fprintf ( stderr, "\n" );
1077 COFF_symbol* symtab_i;
1078 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1079 symtab_i = (COFF_symbol*)
1080 myindex ( sizeof_COFF_symbol, symtab, i );
1086 printName ( symtab_i->Name, strtab );
1095 (Int32)(symtab_i->SectionNumber) - 1,
1096 (UInt32)symtab_i->Type,
1097 (UInt32)symtab_i->StorageClass,
1098 (UInt32)symtab_i->NumberOfAuxSymbols
1100 i += symtab_i->NumberOfAuxSymbols;
1104 fprintf ( stderr, "\n" );
1110 ocGetNames_PEi386 ( ObjectCode* oc )
1113 COFF_section* sectab;
1114 COFF_symbol* symtab;
1121 hdr = (COFF_header*)(oc->image);
1122 sectab = (COFF_section*) (
1123 ((UChar*)(oc->image))
1124 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1126 symtab = (COFF_symbol*) (
1127 ((UChar*)(oc->image))
1128 + hdr->PointerToSymbolTable
1130 strtab = ((UChar*)(oc->image))
1131 + hdr->PointerToSymbolTable
1132 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1134 /* Copy exported symbols into the ObjectCode. */
1136 oc->n_symbols = hdr->NumberOfSymbols;
1137 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1138 "ocGetNames_PEi386(oc->symbols)");
1139 /* Call me paranoid; I don't care. */
1140 for (i = 0; i < oc->n_symbols; i++)
1141 oc->symbols[i] = NULL;
1145 COFF_symbol* symtab_i;
1146 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1147 symtab_i = (COFF_symbol*)
1148 myindex ( sizeof_COFF_symbol, symtab, i );
1150 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL &&
1151 symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1153 /* This symbol is global and defined, viz, exported */
1154 COFF_section* sectabent;
1156 /* cstring_from_COFF_symbol_name always succeeds. */
1157 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1159 /* for MYIMAGE_SYMCLASS_EXTERNAL
1160 && !MYIMAGE_SYM_UNDEFINED,
1161 the address of the symbol is:
1162 address of relevant section + offset in section
1164 sectabent = (COFF_section*)
1165 myindex ( sizeof_COFF_section,
1167 symtab_i->SectionNumber-1 );
1168 addr = ((UChar*)(oc->image))
1169 + (sectabent->PointerToRawData
1171 /* fprintf(stderr,"addSymbol %p `%s'\n", addr,sname); */
1172 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1173 ASSERT(i >= 0 && i < oc->n_symbols);
1174 oc->symbols[i] = sname;
1175 insertStrHashTable(symhash, sname, addr);
1177 i += symtab_i->NumberOfAuxSymbols;
1181 /* Copy section information into the ObjectCode. */
1183 oc->n_sections = hdr->NumberOfSections;
1184 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1185 "ocGetNamesPEi386" );
1187 for (i = 0; i < oc->n_sections; i++) {
1192 = SECTIONKIND_OTHER;
1193 COFF_section* sectab_i
1195 myindex ( sizeof_COFF_section, sectab, i );
1196 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1199 /* I'm sure this is the Right Way to do it. However, the
1200 alternative of testing the sectab_i->Name field seems to
1201 work ok with Cygwin.
1203 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1204 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1205 kind = SECTIONKIND_CODE_OR_RODATA;
1208 if (0==strcmp(".text",sectab_i->Name) ||
1209 0==strcmp(".rodata",sectab_i->Name))
1210 kind = SECTIONKIND_CODE_OR_RODATA;
1211 if (0==strcmp(".data",sectab_i->Name) ||
1212 0==strcmp(".bss",sectab_i->Name))
1213 kind = SECTIONKIND_RWDATA;
1215 start = ((UChar*)(oc->image))
1216 + sectab_i->PointerToRawData;
1218 + sectab_i->SizeOfRawData - 1;
1220 if (kind == SECTIONKIND_OTHER) {
1221 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1225 oc->sections[i].start = start;
1226 oc->sections[i].end = end;
1227 oc->sections[i].kind = kind;
1235 ocResolve_PEi386 ( ObjectCode* oc )
1238 COFF_section* sectab;
1239 COFF_symbol* symtab;
1248 /* ToDo: should be variable-sized? But is at least safe in the
1249 sense of buffer-overrun-proof. */
1251 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1253 hdr = (COFF_header*)(oc->image);
1254 sectab = (COFF_section*) (
1255 ((UChar*)(oc->image))
1256 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1258 symtab = (COFF_symbol*) (
1259 ((UChar*)(oc->image))
1260 + hdr->PointerToSymbolTable
1262 strtab = ((UChar*)(oc->image))
1263 + hdr->PointerToSymbolTable
1264 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1266 for (i = 0; i < hdr->NumberOfSections; i++) {
1267 COFF_section* sectab_i
1269 myindex ( sizeof_COFF_section, sectab, i );
1272 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1274 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1276 COFF_reloc* reltab_j
1278 myindex ( sizeof_COFF_reloc, reltab, j );
1280 /* the location to patch */
1282 ((UChar*)(oc->image))
1283 + (sectab_i->PointerToRawData
1284 + reltab_j->VirtualAddress
1285 - sectab_i->VirtualAddress )
1287 /* the existing contents of pP */
1289 /* the symbol to connect to */
1290 sym = (COFF_symbol*)
1291 myindex ( sizeof_COFF_symbol,
1292 symtab, reltab_j->SymbolTableIndex );
1295 "reloc sec %2d num %3d: type 0x%-4x "
1296 "vaddr 0x%-8x name `",
1298 (UInt32)reltab_j->Type,
1299 reltab_j->VirtualAddress );
1300 printName ( sym->Name, strtab );
1301 fprintf ( stderr, "'\n" ));
1303 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1304 COFF_section* section_sym
1305 = findPEi386SectionCalled ( oc, sym->Name );
1307 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1310 S = ((UInt32)(oc->image))
1311 + (section_sym->PointerToRawData
1314 copyName ( sym->Name, strtab, symbol, 1000-1 );
1315 zapTrailingAtSign ( symbol );
1316 (void*)S = lookupLocalSymbol( oc, symbol );
1317 if ((void*)S == NULL)
1318 (void*)S = lookupSymbol( symbol );
1320 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1325 switch (reltab_j->Type) {
1326 case MYIMAGE_REL_I386_DIR32:
1329 case MYIMAGE_REL_I386_REL32:
1330 /* Tricky. We have to insert a displacement at
1331 pP which, when added to the PC for the _next_
1332 insn, gives the address of the target (S).
1333 Problem is to know the address of the next insn
1334 when we only know pP. We assume that this
1335 literal field is always the last in the insn,
1336 so that the address of the next insn is pP+4
1337 -- hence the constant 4.
1338 Also I don't know if A should be added, but so
1339 far it has always been zero.
1342 *pP = S - ((UInt32)pP) - 4;
1345 belch("%s: unhandled PEi386 relocation type %d",
1346 oc->fileName, reltab_j->Type);
1353 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1357 #endif /* defined(OBJFORMAT_PEi386) */
1360 /* --------------------------------------------------------------------------
1362 * ------------------------------------------------------------------------*/
1364 #if defined(OBJFORMAT_ELF)
1369 #if defined(sparc_TARGET_ARCH)
1370 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1371 #elif defined(i386_TARGET_ARCH)
1372 # define ELF_TARGET_386 /* Used inside <elf.h> */
1374 /* There is a similar case for IA64 in the Solaris2 headers if this
1375 * ever becomes relevant.
1381 findElfSection ( void* objImage, Elf32_Word sh_type )
1384 char* ehdrC = (char*)objImage;
1385 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1386 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1388 for (i = 0; i < ehdr->e_shnum; i++) {
1389 if (shdr[i].sh_type == sh_type &&
1390 i != ehdr->e_shstrndx) {
1391 ptr = ehdrC + shdr[i].sh_offset;
1400 ocVerifyImage_ELF ( ObjectCode* oc )
1404 int i, j, nent, nstrtab, nsymtabs;
1408 char* ehdrC = (char*)(oc->image);
1409 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1411 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1412 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1413 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1414 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1415 belch("%s: not an ELF header", oc->fileName);
1418 IF_DEBUG(linker,belch( "Is an ELF header" ));
1420 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1421 belch("%s: not 32 bit ELF", oc->fileName);
1425 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1427 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1428 IF_DEBUG(linker,belch( "Is little-endian" ));
1430 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1431 IF_DEBUG(linker,belch( "Is big-endian" ));
1433 belch("%s: unknown endiannness", oc->fileName);
1437 if (ehdr->e_type != ET_REL) {
1438 belch("%s: not a relocatable object (.o) file", oc->fileName);
1441 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1443 IF_DEBUG(linker,belch( "Architecture is " ));
1444 switch (ehdr->e_machine) {
1445 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1446 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1447 default: IF_DEBUG(linker,belch( "unknown" ));
1448 belch("%s: unknown architecture", oc->fileName);
1452 IF_DEBUG(linker,belch(
1453 "\nSection header table: start %d, n_entries %d, ent_size %d",
1454 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1456 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1458 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1460 if (ehdr->e_shstrndx == SHN_UNDEF) {
1461 belch("%s: no section header string table", oc->fileName);
1464 IF_DEBUG(linker,belch( "Section header string table is section %d",
1466 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1469 for (i = 0; i < ehdr->e_shnum; i++) {
1470 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1471 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1472 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1473 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1474 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1475 ehdrC + shdr[i].sh_offset,
1476 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1478 if (shdr[i].sh_type == SHT_REL) {
1479 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1480 } else if (shdr[i].sh_type == SHT_RELA) {
1481 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1483 IF_DEBUG(linker,fprintf(stderr," "));
1486 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1490 IF_DEBUG(linker,belch( "\nString tables" ));
1493 for (i = 0; i < ehdr->e_shnum; i++) {
1494 if (shdr[i].sh_type == SHT_STRTAB &&
1495 i != ehdr->e_shstrndx) {
1496 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1497 strtab = ehdrC + shdr[i].sh_offset;
1502 belch("%s: no string tables, or too many", oc->fileName);
1507 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1508 for (i = 0; i < ehdr->e_shnum; i++) {
1509 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1510 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1512 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1513 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1514 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1516 shdr[i].sh_size % sizeof(Elf32_Sym)
1518 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1519 belch("%s: non-integral number of symbol table entries", oc->fileName);
1522 for (j = 0; j < nent; j++) {
1523 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1524 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1525 (int)stab[j].st_shndx,
1526 (int)stab[j].st_size,
1527 (char*)stab[j].st_value ));
1529 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1530 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1531 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1532 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1533 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1534 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1535 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1536 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1538 IF_DEBUG(linker,fprintf(stderr, " " ));
1540 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1541 switch (ELF32_ST_BIND(stab[j].st_info)) {
1542 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1543 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1544 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1545 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1547 IF_DEBUG(linker,fprintf(stderr, " " ));
1549 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1553 if (nsymtabs == 0) {
1554 belch("%s: didn't find any symbol tables", oc->fileName);
1563 ocGetNames_ELF ( ObjectCode* oc )
1568 char* ehdrC = (char*)(oc->image);
1569 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1570 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1571 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1572 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1574 ASSERT(symhash != NULL);
1577 belch("%s: no strtab", oc->fileName);
1582 oc->n_sections = ehdr->e_shnum;
1583 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1584 "ocGetNames_ELF(oc->sections)" );
1586 for (i = 0; i < oc->n_sections; i++) {
1588 /* make a section entry for relevant sections */
1589 SectionKind kind = SECTIONKIND_OTHER;
1590 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1591 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1592 kind = SECTIONKIND_RWDATA;
1593 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1594 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1595 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1596 kind = SECTIONKIND_CODE_OR_RODATA;
1598 /* fill in the section info */
1599 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1600 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1601 oc->sections[i].kind = kind;
1603 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1605 /* copy stuff into this module's object symbol table */
1606 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1607 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1609 oc->n_symbols = nent;
1610 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1611 "ocGetNames_ELF(oc->symbols)");
1613 for (j = 0; j < nent; j++) {
1614 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1615 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1617 /* and not an undefined symbol */
1618 && stab[j].st_shndx != SHN_UNDEF
1619 /* and not in a "special section" */
1620 && stab[j].st_shndx < SHN_LORESERVE
1622 /* and it's a not a section or string table or anything silly */
1623 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1624 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1625 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1628 char* nm = strtab + stab[j].st_name;
1630 + shdr[ stab[j].st_shndx ].sh_offset
1634 oc->symbols[j] = nm;
1635 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1636 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1637 ad, oc->fileName, nm ));
1638 insertStrHashTable(oc->lochash, nm, ad);
1640 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1641 ad, oc->fileName, nm ));
1642 insertStrHashTable(symhash, nm, ad);
1646 IF_DEBUG(linker,belch( "skipping `%s'",
1647 strtab + stab[j].st_name ));
1650 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1651 (int)ELF32_ST_BIND(stab[j].st_info),
1652 (int)ELF32_ST_TYPE(stab[j].st_info),
1653 (int)stab[j].st_shndx,
1654 strtab + stab[j].st_name
1657 oc->symbols[j] = NULL;
1666 /* Do ELF relocations which lack an explicit addend. All x86-linux
1667 relocations appear to be of this form. */
1669 do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1670 Elf32_Shdr* shdr, int shnum,
1671 Elf32_Sym* stab, char* strtab )
1676 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1677 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1678 int target_shndx = shdr[shnum].sh_info;
1679 int symtab_shndx = shdr[shnum].sh_link;
1680 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1681 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1682 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1683 target_shndx, symtab_shndx ));
1684 for (j = 0; j < nent; j++) {
1685 Elf32_Addr offset = rtab[j].r_offset;
1686 Elf32_Word info = rtab[j].r_info;
1688 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1689 Elf32_Word* pP = (Elf32_Word*)P;
1693 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1694 j, (void*)offset, (void*)info ));
1696 IF_DEBUG(linker,belch( " ZERO" ));
1699 /* First see if it is a nameless local symbol. */
1700 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1701 symbol = "(noname)";
1703 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1704 + stab[ELF32_R_SYM(info)].st_value);
1706 /* No? Should be in a symbol table then; first try the
1708 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1709 (void*)S = lookupLocalSymbol( oc, symbol );
1710 if ((void*)S == NULL)
1711 (void*)S = lookupSymbol( symbol );
1714 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1717 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1719 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1720 (void*)P, (void*)S, (void*)A ));
1721 switch (ELF32_R_TYPE(info)) {
1722 # ifdef i386_TARGET_ARCH
1723 case R_386_32: *pP = S + A; break;
1724 case R_386_PC32: *pP = S + A - P; break;
1727 belch("%s: unhandled ELF relocation(Rel) type %d\n",
1728 oc->fileName, ELF32_R_TYPE(info));
1737 /* Do ELF relocations for which explicit addends are supplied.
1738 sparc-solaris relocations appear to be of this form. */
1740 do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1741 Elf32_Shdr* shdr, int shnum,
1742 Elf32_Sym* stab, char* strtab )
1747 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1748 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1749 int target_shndx = shdr[shnum].sh_info;
1750 int symtab_shndx = shdr[shnum].sh_link;
1751 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1752 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1753 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1754 target_shndx, symtab_shndx ));
1755 for (j = 0; j < nent; j++) {
1756 Elf32_Addr offset = rtab[j].r_offset;
1757 Elf32_Word info = rtab[j].r_info;
1758 Elf32_Sword addend = rtab[j].r_addend;
1760 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1761 Elf32_Addr A = addend;
1763 # if defined(sparc_TARGET_ARCH)
1764 /* This #ifdef only serves to avoid unused-var warnings. */
1765 Elf32_Word* pP = (Elf32_Word*)P;
1769 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1770 j, (void*)offset, (void*)info,
1773 IF_DEBUG(linker,belch( " ZERO" ));
1776 /* First see if it is a nameless local symbol. */
1777 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1778 symbol = "(noname)";
1780 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1781 + stab[ELF32_R_SYM(info)].st_value);
1783 /* No? Should be in a symbol table then; first try the
1785 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1786 (void*)S = lookupLocalSymbol( oc, symbol );
1787 if ((void*)S == NULL)
1788 (void*)S = lookupSymbol( symbol );
1791 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1795 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1798 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1800 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1801 (void*)P, (void*)S, (void*)A ));
1802 switch (ELF32_R_TYPE(info)) {
1803 # if defined(sparc_TARGET_ARCH)
1804 case R_SPARC_WDISP30:
1805 w1 = *pP & 0xC0000000;
1806 w2 = (Elf32_Word)((S + A - P) >> 2);
1807 ASSERT((w2 & 0xC0000000) == 0);
1812 w1 = *pP & 0xFFC00000;
1813 w2 = (Elf32_Word)((S + A) >> 10);
1814 ASSERT((w2 & 0xFFC00000) == 0);
1820 w2 = (Elf32_Word)((S + A) & 0x3FF);
1821 ASSERT((w2 & ~0x3FF) == 0);
1826 w2 = (Elf32_Word)(S + A);
1831 belch("%s: unhandled ELF relocation(RelA) type %d\n",
1832 oc->fileName, ELF32_R_TYPE(info));
1842 ocResolve_ELF ( ObjectCode* oc )
1846 Elf32_Sym* stab = NULL;
1847 char* ehdrC = (char*)(oc->image);
1848 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1849 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1851 /* first find "the" symbol table */
1852 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1854 /* also go find the string table */
1855 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1857 if (stab == NULL || strtab == NULL) {
1858 belch("%s: can't find string or symbol table", oc->fileName);
1862 /* Process the relocation sections. */
1863 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
1864 if (shdr[shnum].sh_type == SHT_REL ) {
1865 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
1866 shnum, stab, strtab );
1870 if (shdr[shnum].sh_type == SHT_RELA) {
1871 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
1872 shnum, stab, strtab );
1877 /* Free the local symbol table; we won't need it again. */
1878 freeHashTable(oc->lochash, NULL);