1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.69 2001/10/19 09:45:26 sewardj Exp $
4 * (c) The GHC Team, 2000, 2001
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
16 #include "LinkerInternals.h"
18 #include "StoragePriv.h"
21 #ifdef HAVE_SYS_TYPES_H
22 #include <sys/types.h>
25 #ifdef HAVE_SYS_STAT_H
33 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
34 # define OBJFORMAT_ELF
35 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
36 # define OBJFORMAT_PEi386
40 /* Hash table mapping symbol names to Symbol */
41 /*Str*/HashTable *symhash;
43 #if defined(OBJFORMAT_ELF)
44 static int ocVerifyImage_ELF ( ObjectCode* oc );
45 static int ocGetNames_ELF ( ObjectCode* oc );
46 static int ocResolve_ELF ( ObjectCode* oc );
47 #elif defined(OBJFORMAT_PEi386)
48 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
49 static int ocGetNames_PEi386 ( ObjectCode* oc );
50 static int ocResolve_PEi386 ( ObjectCode* oc );
53 /* -----------------------------------------------------------------------------
54 * Built-in symbols from the RTS
57 typedef struct _RtsSymbolVal {
64 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
66 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
67 SymX(makeStableNamezh_fast) \
68 SymX(finalizzeWeakzh_fast)
70 /* These are not available in GUM!!! -- HWL */
71 #define Maybe_ForeignObj
72 #define Maybe_Stable_Names
75 #if !defined (mingw32_TARGET_OS)
77 #define RTS_POSIX_ONLY_SYMBOLS \
78 SymX(stg_sig_install) \
80 #define RTS_MINGW_ONLY_SYMBOLS /**/
84 #define RTS_POSIX_ONLY_SYMBOLS
86 /* These are statically linked from the mingw libraries into the ghc
87 executable, so we have to employ this hack. */
88 #define RTS_MINGW_ONLY_SYMBOLS \
100 SymX(getservbyname) \
101 SymX(getservbyport) \
102 SymX(getprotobynumber) \
103 SymX(getprotobyname) \
104 SymX(gethostbyname) \
105 SymX(gethostbyaddr) \
140 Sym(_imp___timezone) \
151 SymX(GetCurrentProcess) \
152 SymX(GetProcessTimes) \
154 SymX(GetExitCodeProcess) \
155 SymX(WaitForSingleObject) \
156 SymX(CreateProcessA) \
165 #define RTS_SYMBOLS \
169 Sym(__stginit_PrelGHC) \
173 Sym(stg_enterStackTop) \
175 Sym(stg_gc_enter_1) \
179 Sym(stg_gc_unbx_r1) \
180 Sym(stg_gc_unpt_r1) \
184 Sym(stg_yield_to_interpreter) \
187 SymX(MallocFailHook) \
188 SymX(NoRunnableThreadsHook) \
190 SymX(OutOfHeapHook) \
191 SymX(PatErrorHdrHook) \
192 SymX(PostTraceHook) \
194 SymX(StackOverflowHook) \
195 SymX(__encodeDouble) \
196 SymX(__encodeFloat) \
199 SymX(__gmpz_cmp_si) \
200 SymX(__gmpz_cmp_ui) \
201 SymX(__gmpz_get_si) \
202 SymX(__gmpz_get_ui) \
203 SymX(__int_encodeDouble) \
204 SymX(__int_encodeFloat) \
205 SymX(andIntegerzh_fast) \
206 SymX(blockAsyncExceptionszh_fast) \
209 SymX(complementIntegerzh_fast) \
210 SymX(createAdjustor) \
211 SymX(decodeDoublezh_fast) \
212 SymX(decodeFloatzh_fast) \
215 SymX(divExactIntegerzh_fast) \
216 SymX(divModIntegerzh_fast) \
218 SymX(freeHaskellFunctionPtr) \
219 SymX(gcdIntegerzh_fast) \
222 SymX(int2Integerzh_fast) \
223 SymX(isDoubleDenormalized) \
224 SymX(isDoubleInfinite) \
226 SymX(isDoubleNegativeZero) \
227 SymX(isFloatDenormalized) \
228 SymX(isFloatInfinite) \
230 SymX(isFloatNegativeZero) \
231 SymX(killThreadzh_fast) \
232 SymX(minusIntegerzh_fast) \
233 SymX(mkApUpd0zh_fast) \
234 SymX(newArrayzh_fast) \
235 SymX(newBCOzh_fast) \
236 SymX(newByteArrayzh_fast) \
238 SymX(newMVarzh_fast) \
239 SymX(newMutVarzh_fast) \
240 SymX(newPinnedByteArrayzh_fast) \
241 SymX(orIntegerzh_fast) \
243 SymX(plusIntegerzh_fast) \
246 SymX(putMVarzh_fast) \
247 SymX(quotIntegerzh_fast) \
248 SymX(quotRemIntegerzh_fast) \
250 SymX(remIntegerzh_fast) \
251 SymX(resetNonBlockingFd) \
254 SymX(rts_checkSchedStatus) \
257 SymX(rts_evalLazyIO) \
262 SymX(rts_getDouble) \
267 SymX(rts_getStablePtr) \
269 SymX(rts_getWord32) \
281 SymX(rts_mkStablePtr) \
290 SymX(shutdownHaskellAndExit) \
291 SymX(stable_ptr_table) \
292 SymX(stackOverflow) \
293 SymX(stg_CAF_BLACKHOLE_info) \
294 SymX(stg_CHARLIKE_closure) \
295 SymX(stg_EMPTY_MVAR_info) \
296 SymX(stg_IND_STATIC_info) \
297 SymX(stg_INTLIKE_closure) \
298 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
299 SymX(stg_WEAK_info) \
300 SymX(stg_ap_1_upd_info) \
301 SymX(stg_ap_2_upd_info) \
302 SymX(stg_ap_3_upd_info) \
303 SymX(stg_ap_4_upd_info) \
304 SymX(stg_ap_5_upd_info) \
305 SymX(stg_ap_6_upd_info) \
306 SymX(stg_ap_7_upd_info) \
307 SymX(stg_ap_8_upd_info) \
309 SymX(stg_sel_0_upd_info) \
310 SymX(stg_sel_10_upd_info) \
311 SymX(stg_sel_11_upd_info) \
312 SymX(stg_sel_12_upd_info) \
313 SymX(stg_sel_13_upd_info) \
314 SymX(stg_sel_14_upd_info) \
315 SymX(stg_sel_15_upd_info) \
316 SymX(stg_sel_1_upd_info) \
317 SymX(stg_sel_2_upd_info) \
318 SymX(stg_sel_3_upd_info) \
319 SymX(stg_sel_4_upd_info) \
320 SymX(stg_sel_5_upd_info) \
321 SymX(stg_sel_6_upd_info) \
322 SymX(stg_sel_7_upd_info) \
323 SymX(stg_sel_8_upd_info) \
324 SymX(stg_sel_9_upd_info) \
325 SymX(stg_seq_frame_info) \
326 SymX(stg_upd_frame_info) \
327 SymX(stg_update_PAP) \
328 SymX(suspendThread) \
329 SymX(takeMVarzh_fast) \
330 SymX(timesIntegerzh_fast) \
331 SymX(tryPutMVarzh_fast) \
332 SymX(tryTakeMVarzh_fast) \
333 SymX(unblockAsyncExceptionszh_fast) \
334 SymX(unsafeThawArrayzh_fast) \
335 SymX(waitReadzh_fast) \
336 SymX(waitWritezh_fast) \
337 SymX(word2Integerzh_fast) \
338 SymX(xorIntegerzh_fast) \
341 #ifndef SUPPORT_LONG_LONGS
342 #define RTS_LONG_LONG_SYMS /* nothing */
344 #define RTS_LONG_LONG_SYMS \
345 SymX(int64ToIntegerzh_fast) \
346 SymX(word64ToIntegerzh_fast)
347 #endif /* SUPPORT_LONG_LONGS */
349 /* entirely bogus claims about types of these symbols */
350 #define Sym(vvv) extern void (vvv);
351 #define SymX(vvv) /**/
354 RTS_POSIX_ONLY_SYMBOLS
355 RTS_MINGW_ONLY_SYMBOLS
359 #ifdef LEADING_UNDERSCORE
360 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
362 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
365 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
367 #define SymX(vvv) Sym(vvv)
369 static RtsSymbolVal rtsSyms[] = {
372 RTS_POSIX_ONLY_SYMBOLS
373 RTS_MINGW_ONLY_SYMBOLS
374 { 0, 0 } /* sentinel */
377 /* -----------------------------------------------------------------------------
378 * initialize the object linker
380 #if defined(OBJFORMAT_ELF)
381 static void *dl_prog_handle;
389 symhash = allocStrHashTable();
391 /* populate the symbol table with stuff from the RTS */
392 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
393 insertStrHashTable(symhash, sym->lbl, sym->addr);
395 # if defined(OBJFORMAT_ELF)
396 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
400 /* -----------------------------------------------------------------------------
401 * Add a DLL from which symbols may be found. In the ELF case, just
402 * do RTLD_GLOBAL-style add, so no further messing around needs to
403 * happen in order that symbols in the loaded .so are findable --
404 * lookupSymbol() will subsequently see them by dlsym on the program's
405 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
407 * In the PEi386 case, open the DLLs and put handles to them in a
408 * linked list. When looking for a symbol, try all handles in the
412 #if defined(OBJFORMAT_PEi386)
413 /* A record for storing handles into DLLs. */
418 struct _OpenedDLL* next;
423 /* A list thereof. */
424 static OpenedDLL* opened_dlls = NULL;
430 addDLL ( __attribute((unused)) char* path, char* dll_name )
432 # if defined(OBJFORMAT_ELF)
437 if (path == NULL || strlen(path) == 0) {
438 buf = stgMallocBytes(strlen(dll_name) + 10, "addDll");
439 sprintf(buf, "lib%s.so", dll_name);
441 buf = stgMallocBytes(strlen(path) + 1 + strlen(dll_name) + 10, "addDll");
442 sprintf(buf, "%s/lib%s.so", path, dll_name);
444 hdl = dlopen(buf, RTLD_NOW | RTLD_GLOBAL );
447 /* dlopen failed; return a ptr to the error msg. */
449 if (errmsg == NULL) errmsg = "addDLL: unknown error";
456 # elif defined(OBJFORMAT_PEi386)
458 /* Add this DLL to the list of DLLs in which to search for symbols.
459 The path argument is ignored. */
464 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
466 /* See if we've already got it, and ignore if so. */
467 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
468 if (0 == strcmp(o_dll->name, dll_name))
472 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
473 sprintf(buf, "%s.DLL", dll_name);
474 instance = LoadLibrary(buf);
476 if (instance == NULL) {
477 /* LoadLibrary failed; return a ptr to the error msg. */
478 return "addDLL: unknown error";
481 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
482 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
483 strcpy(o_dll->name, dll_name);
484 o_dll->instance = instance;
485 o_dll->next = opened_dlls;
490 barf("addDLL: not implemented on this platform");
494 /* -----------------------------------------------------------------------------
495 * lookup a symbol in the hash table
498 lookupSymbol( char *lbl )
501 ASSERT(symhash != NULL);
502 val = lookupStrHashTable(symhash, lbl);
505 # if defined(OBJFORMAT_ELF)
506 return dlsym(dl_prog_handle, lbl);
507 # elif defined(OBJFORMAT_PEi386)
510 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
511 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
513 /* HACK: if the name has an initial underscore, try stripping
514 it off & look that up first. I've yet to verify whether there's
515 a Rule that governs whether an initial '_' *should always* be
516 stripped off when mapping from import lib name to the DLL name.
518 sym = GetProcAddress(o_dll->instance, (lbl+1));
519 if (sym != NULL) return sym;
521 sym = GetProcAddress(o_dll->instance, lbl);
522 if (sym != NULL) return sym;
536 lookupLocalSymbol( ObjectCode* oc, char *lbl )
539 val = lookupStrHashTable(oc->lochash, lbl);
549 /* -----------------------------------------------------------------------------
550 * Load an obj (populate the global symbol table, but don't resolve yet)
552 * Returns: 1 if ok, 0 on error.
555 loadObj( char *path )
562 /* fprintf(stderr, "loadObj %s\n", path ); */
564 /* assert that we haven't already loaded this object */
567 for (o = objects; o; o = o->next)
568 ASSERT(strcmp(o->fileName, path));
572 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
574 # if defined(OBJFORMAT_ELF)
575 oc->formatName = "ELF";
576 # elif defined(OBJFORMAT_PEi386)
577 oc->formatName = "PEi386";
580 barf("loadObj: not implemented on this platform");
584 if (r == -1) { return 0; }
586 /* sigh, strdup() isn't a POSIX function, so do it the long way */
587 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
588 strcpy(oc->fileName, path);
590 oc->fileSize = st.st_size;
591 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
594 oc->lochash = allocStrHashTable();
595 oc->proddables = NULL;
597 /* chain it onto the list of objects */
601 /* load the image into memory */
602 f = fopen(path, "rb");
604 barf("loadObj: can't read `%s'", path);
606 n = fread ( oc->image, 1, oc->fileSize, f );
607 if (n != oc->fileSize) {
609 barf("loadObj: error whilst reading `%s'", path);
612 /* verify the in-memory image */
613 # if defined(OBJFORMAT_ELF)
614 r = ocVerifyImage_ELF ( oc );
615 # elif defined(OBJFORMAT_PEi386)
616 r = ocVerifyImage_PEi386 ( oc );
618 barf("loadObj: no verify method");
620 if (!r) { return r; }
622 /* build the symbol list for this image */
623 # if defined(OBJFORMAT_ELF)
624 r = ocGetNames_ELF ( oc );
625 # elif defined(OBJFORMAT_PEi386)
626 r = ocGetNames_PEi386 ( oc );
628 barf("loadObj: no getNames method");
630 if (!r) { return r; }
632 /* loaded, but not resolved yet */
633 oc->status = OBJECT_LOADED;
638 /* -----------------------------------------------------------------------------
639 * resolve all the currently unlinked objects in memory
641 * Returns: 1 if ok, 0 on error.
649 for (oc = objects; oc; oc = oc->next) {
650 if (oc->status != OBJECT_RESOLVED) {
651 # if defined(OBJFORMAT_ELF)
652 r = ocResolve_ELF ( oc );
653 # elif defined(OBJFORMAT_PEi386)
654 r = ocResolve_PEi386 ( oc );
656 barf("resolveObjs: not implemented on this platform");
658 if (!r) { return r; }
659 oc->status = OBJECT_RESOLVED;
665 /* -----------------------------------------------------------------------------
666 * delete an object from the pool
669 unloadObj( char *path )
671 ObjectCode *oc, *prev;
673 ASSERT(symhash != NULL);
674 ASSERT(objects != NULL);
677 for (oc = objects; oc; prev = oc, oc = oc->next) {
678 if (!strcmp(oc->fileName,path)) {
680 /* Remove all the mappings for the symbols within this
685 for (i = 0; i < oc->n_symbols; i++) {
686 if (oc->symbols[i] != NULL) {
687 removeStrHashTable(symhash, oc->symbols[i], NULL);
695 prev->next = oc->next;
698 /* We're going to leave this in place, in case there are
699 any pointers from the heap into it: */
700 /* free(oc->image); */
704 /* The local hash table should have been freed at the end
705 of the ocResolve_ call on it. */
706 ASSERT(oc->lochash == NULL);
712 belch("unloadObj: can't find `%s' to unload", path);
716 /* -----------------------------------------------------------------------------
717 * Sanity checking. For each ObjectCode, maintain a list of address ranges
718 * which may be prodded during relocation, and abort if we try and write
719 * outside any of these.
721 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
724 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
725 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
729 pb->next = oc->proddables;
733 static void checkProddableBlock ( ObjectCode* oc, void* addr )
736 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
737 char* s = (char*)(pb->start);
738 char* e = s + pb->size - 1;
739 char* a = (char*)addr;
740 /* Assumes that the biggest fixup involves a 4-byte write. This
741 probably needs to be changed to 8 (ie, +7) on 64-bit
743 if (a >= s && (a+3) <= e) return;
745 barf("checkProddableBlock: invalid fixup in runtime linker");
748 /* -----------------------------------------------------------------------------
749 * Section management.
751 static void addSection ( ObjectCode* oc, SectionKind kind,
752 void* start, void* end )
754 Section* s = stgMallocBytes(sizeof(Section), "addSection");
758 s->next = oc->sections;
764 /* --------------------------------------------------------------------------
765 * PEi386 specifics (Win32 targets)
766 * ------------------------------------------------------------------------*/
768 /* The information for this linker comes from
769 Microsoft Portable Executable
770 and Common Object File Format Specification
771 revision 5.1 January 1998
772 which SimonM says comes from the MS Developer Network CDs.
776 #if defined(OBJFORMAT_PEi386)
780 typedef unsigned char UChar;
781 typedef unsigned short UInt16;
782 typedef unsigned int UInt32;
789 UInt16 NumberOfSections;
790 UInt32 TimeDateStamp;
791 UInt32 PointerToSymbolTable;
792 UInt32 NumberOfSymbols;
793 UInt16 SizeOfOptionalHeader;
794 UInt16 Characteristics;
798 #define sizeof_COFF_header 20
805 UInt32 VirtualAddress;
806 UInt32 SizeOfRawData;
807 UInt32 PointerToRawData;
808 UInt32 PointerToRelocations;
809 UInt32 PointerToLinenumbers;
810 UInt16 NumberOfRelocations;
811 UInt16 NumberOfLineNumbers;
812 UInt32 Characteristics;
816 #define sizeof_COFF_section 40
823 UInt16 SectionNumber;
826 UChar NumberOfAuxSymbols;
830 #define sizeof_COFF_symbol 18
835 UInt32 VirtualAddress;
836 UInt32 SymbolTableIndex;
841 #define sizeof_COFF_reloc 10
844 /* From PE spec doc, section 3.3.2 */
845 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
846 windows.h -- for the same purpose, but I want to know what I'm
848 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
849 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
850 #define MYIMAGE_FILE_DLL 0x2000
851 #define MYIMAGE_FILE_SYSTEM 0x1000
852 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
853 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
854 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
856 /* From PE spec doc, section 5.4.2 and 5.4.4 */
857 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
858 #define MYIMAGE_SYM_CLASS_STATIC 3
859 #define MYIMAGE_SYM_UNDEFINED 0
861 /* From PE spec doc, section 4.1 */
862 #define MYIMAGE_SCN_CNT_CODE 0x00000020
863 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
865 /* From PE spec doc, section 5.2.1 */
866 #define MYIMAGE_REL_I386_DIR32 0x0006
867 #define MYIMAGE_REL_I386_REL32 0x0014
870 /* We use myindex to calculate array addresses, rather than
871 simply doing the normal subscript thing. That's because
872 some of the above structs have sizes which are not
873 a whole number of words. GCC rounds their sizes up to a
874 whole number of words, which means that the address calcs
875 arising from using normal C indexing or pointer arithmetic
876 are just plain wrong. Sigh.
879 myindex ( int scale, void* base, int index )
882 ((UChar*)base) + scale * index;
887 printName ( UChar* name, UChar* strtab )
889 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
890 UInt32 strtab_offset = * (UInt32*)(name+4);
891 fprintf ( stderr, "%s", strtab + strtab_offset );
894 for (i = 0; i < 8; i++) {
895 if (name[i] == 0) break;
896 fprintf ( stderr, "%c", name[i] );
903 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
905 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
906 UInt32 strtab_offset = * (UInt32*)(name+4);
907 strncpy ( dst, strtab+strtab_offset, dstSize );
913 if (name[i] == 0) break;
923 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
926 /* If the string is longer than 8 bytes, look in the
927 string table for it -- this will be correctly zero terminated.
929 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
930 UInt32 strtab_offset = * (UInt32*)(name+4);
931 return ((UChar*)strtab) + strtab_offset;
933 /* Otherwise, if shorter than 8 bytes, return the original,
934 which by defn is correctly terminated.
936 if (name[7]==0) return name;
937 /* The annoying case: 8 bytes. Copy into a temporary
938 (which is never freed ...)
940 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
942 strncpy(newstr,name,8);
948 /* Just compares the short names (first 8 chars) */
949 static COFF_section *
950 findPEi386SectionCalled ( ObjectCode* oc, char* name )
954 = (COFF_header*)(oc->image);
957 ((UChar*)(oc->image))
958 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
960 for (i = 0; i < hdr->NumberOfSections; i++) {
963 COFF_section* section_i
965 myindex ( sizeof_COFF_section, sectab, i );
966 n1 = (UChar*) &(section_i->Name);
968 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
969 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
970 n1[6]==n2[6] && n1[7]==n2[7])
979 zapTrailingAtSign ( UChar* sym )
981 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
983 if (sym[0] == 0) return;
985 while (sym[i] != 0) i++;
988 while (j > 0 && my_isdigit(sym[j])) j--;
989 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
995 ocVerifyImage_PEi386 ( ObjectCode* oc )
999 COFF_section* sectab;
1000 COFF_symbol* symtab;
1002 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
1003 hdr = (COFF_header*)(oc->image);
1004 sectab = (COFF_section*) (
1005 ((UChar*)(oc->image))
1006 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1008 symtab = (COFF_symbol*) (
1009 ((UChar*)(oc->image))
1010 + hdr->PointerToSymbolTable
1012 strtab = ((UChar*)symtab)
1013 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1015 if (hdr->Machine != 0x14c) {
1016 belch("Not x86 PEi386");
1019 if (hdr->SizeOfOptionalHeader != 0) {
1020 belch("PEi386 with nonempty optional header");
1023 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1024 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1025 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1026 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1027 belch("Not a PEi386 object file");
1030 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1031 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1032 belch("Invalid PEi386 word size or endiannness: %d",
1033 (int)(hdr->Characteristics));
1036 /* If the string table size is way crazy, this might indicate that
1037 there are more than 64k relocations, despite claims to the
1038 contrary. Hence this test. */
1039 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1040 if (* (UInt32*)strtab > 600000) {
1041 /* Note that 600k has no special significance other than being
1042 big enough to handle the almost-2MB-sized lumps that
1043 constitute HSwin32*.o. */
1044 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1048 /* No further verification after this point; only debug printing. */
1050 IF_DEBUG(linker, i=1);
1051 if (i == 0) return 1;
1054 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1056 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1058 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1060 fprintf ( stderr, "\n" );
1062 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1064 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1066 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1068 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1070 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1072 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1074 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1076 /* Print the section table. */
1077 fprintf ( stderr, "\n" );
1078 for (i = 0; i < hdr->NumberOfSections; i++) {
1080 COFF_section* sectab_i
1082 myindex ( sizeof_COFF_section, sectab, i );
1089 printName ( sectab_i->Name, strtab );
1099 sectab_i->VirtualSize,
1100 sectab_i->VirtualAddress,
1101 sectab_i->SizeOfRawData,
1102 sectab_i->PointerToRawData,
1103 sectab_i->NumberOfRelocations,
1104 sectab_i->PointerToRelocations,
1105 sectab_i->PointerToRawData
1107 reltab = (COFF_reloc*) (
1108 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1111 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1113 COFF_reloc* rel = (COFF_reloc*)
1114 myindex ( sizeof_COFF_reloc, reltab, j );
1116 " type 0x%-4x vaddr 0x%-8x name `",
1118 rel->VirtualAddress );
1119 sym = (COFF_symbol*)
1120 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1121 printName ( sym->Name, strtab -10 );
1122 fprintf ( stderr, "'\n" );
1125 fprintf ( stderr, "\n" );
1127 fprintf ( stderr, "\n" );
1128 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1129 fprintf ( stderr, "---START of string table---\n");
1130 for (i = 4; i < *(Int32*)strtab; i++) {
1132 fprintf ( stderr, "\n"); else
1133 fprintf( stderr, "%c", strtab[i] );
1135 fprintf ( stderr, "--- END of string table---\n");
1137 fprintf ( stderr, "\n" );
1140 COFF_symbol* symtab_i;
1141 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1142 symtab_i = (COFF_symbol*)
1143 myindex ( sizeof_COFF_symbol, symtab, i );
1149 printName ( symtab_i->Name, strtab );
1158 (Int32)(symtab_i->SectionNumber),
1159 (UInt32)symtab_i->Type,
1160 (UInt32)symtab_i->StorageClass,
1161 (UInt32)symtab_i->NumberOfAuxSymbols
1163 i += symtab_i->NumberOfAuxSymbols;
1167 fprintf ( stderr, "\n" );
1173 ocGetNames_PEi386 ( ObjectCode* oc )
1176 COFF_section* sectab;
1177 COFF_symbol* symtab;
1184 hdr = (COFF_header*)(oc->image);
1185 sectab = (COFF_section*) (
1186 ((UChar*)(oc->image))
1187 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1189 symtab = (COFF_symbol*) (
1190 ((UChar*)(oc->image))
1191 + hdr->PointerToSymbolTable
1193 strtab = ((UChar*)(oc->image))
1194 + hdr->PointerToSymbolTable
1195 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1197 /* Allocate space for any (local, anonymous) .bss sections. */
1199 for (i = 0; i < hdr->NumberOfSections; i++) {
1201 COFF_section* sectab_i
1203 myindex ( sizeof_COFF_section, sectab, i );
1204 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1205 if (sectab_i->VirtualSize == 0) continue;
1206 /* This is a non-empty .bss section. Allocate zeroed space for
1207 it, and set its PointerToRawData field such that oc->image +
1208 PointerToRawData == addr_of_zeroed_space. */
1209 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1210 "ocGetNames_PEi386(anonymous bss)");
1211 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1212 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1213 /* fprintf(stderr, "BSS anon section at 0x%x\n", zspace); */
1216 /* Copy section information into the ObjectCode. */
1218 for (i = 0; i < hdr->NumberOfSections; i++) {
1224 = SECTIONKIND_OTHER;
1225 COFF_section* sectab_i
1227 myindex ( sizeof_COFF_section, sectab, i );
1228 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1231 /* I'm sure this is the Right Way to do it. However, the
1232 alternative of testing the sectab_i->Name field seems to
1233 work ok with Cygwin.
1235 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1236 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1237 kind = SECTIONKIND_CODE_OR_RODATA;
1240 if (0==strcmp(".text",sectab_i->Name) ||
1241 0==strcmp(".rodata",sectab_i->Name))
1242 kind = SECTIONKIND_CODE_OR_RODATA;
1243 if (0==strcmp(".data",sectab_i->Name) ||
1244 0==strcmp(".bss",sectab_i->Name))
1245 kind = SECTIONKIND_RWDATA;
1247 ASSERT(sectab_i->SizeOfRawData == 0 || sectab_i->VirtualSize == 0);
1248 sz = sectab_i->SizeOfRawData;
1249 if (sz < sectab_i->VirtualSize) sz = sectab_i->VirtualSize;
1251 start = ((UChar*)(oc->image)) + sectab_i->PointerToRawData;
1252 end = start + sz - 1;
1254 if (kind == SECTIONKIND_OTHER
1255 /* Ignore sections called which contain stabs debugging
1257 && 0 != strcmp(".stab", sectab_i->Name)
1258 && 0 != strcmp(".stabstr", sectab_i->Name)
1260 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1264 if (kind != SECTIONKIND_OTHER && end >= start) {
1265 addSection(oc, kind, start, end);
1266 addProddableBlock(oc, start, end - start + 1);
1270 /* Copy exported symbols into the ObjectCode. */
1272 oc->n_symbols = hdr->NumberOfSymbols;
1273 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1274 "ocGetNames_PEi386(oc->symbols)");
1275 /* Call me paranoid; I don't care. */
1276 for (i = 0; i < oc->n_symbols; i++)
1277 oc->symbols[i] = NULL;
1281 COFF_symbol* symtab_i;
1282 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1283 symtab_i = (COFF_symbol*)
1284 myindex ( sizeof_COFF_symbol, symtab, i );
1288 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1289 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1290 /* This symbol is global and defined, viz, exported */
1291 /* for MYIMAGE_SYMCLASS_EXTERNAL
1292 && !MYIMAGE_SYM_UNDEFINED,
1293 the address of the symbol is:
1294 address of relevant section + offset in section
1296 COFF_section* sectabent
1297 = (COFF_section*) myindex ( sizeof_COFF_section,
1299 symtab_i->SectionNumber-1 );
1300 addr = ((UChar*)(oc->image))
1301 + (sectabent->PointerToRawData
1305 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1306 && symtab_i->Value > 0) {
1307 /* This symbol isn't in any section at all, ie, global bss.
1308 Allocate zeroed space for it. */
1309 addr = stgCallocBytes(1, symtab_i->Value,
1310 "ocGetNames_PEi386(non-anonymous bss)");
1311 addSection(oc, SECTIONKIND_RWDATA, addr,
1312 ((UChar*)addr) + symtab_i->Value - 1);
1313 addProddableBlock(oc, addr, symtab_i->Value);
1314 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1318 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1319 /* fprintf(stderr,"addSymbol %p `%s'\n", addr,sname); */
1320 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1321 ASSERT(i >= 0 && i < oc->n_symbols);
1322 /* cstring_from_COFF_symbol_name always succeeds. */
1323 oc->symbols[i] = sname;
1324 insertStrHashTable(symhash, sname, addr);
1328 "IGNORING symbol %d\n"
1332 printName ( symtab_i->Name, strtab );
1341 (Int32)(symtab_i->SectionNumber),
1342 (UInt32)symtab_i->Type,
1343 (UInt32)symtab_i->StorageClass,
1344 (UInt32)symtab_i->NumberOfAuxSymbols
1349 i += symtab_i->NumberOfAuxSymbols;
1358 ocResolve_PEi386 ( ObjectCode* oc )
1361 COFF_section* sectab;
1362 COFF_symbol* symtab;
1371 /* ToDo: should be variable-sized? But is at least safe in the
1372 sense of buffer-overrun-proof. */
1374 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1376 hdr = (COFF_header*)(oc->image);
1377 sectab = (COFF_section*) (
1378 ((UChar*)(oc->image))
1379 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1381 symtab = (COFF_symbol*) (
1382 ((UChar*)(oc->image))
1383 + hdr->PointerToSymbolTable
1385 strtab = ((UChar*)(oc->image))
1386 + hdr->PointerToSymbolTable
1387 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1389 for (i = 0; i < hdr->NumberOfSections; i++) {
1390 COFF_section* sectab_i
1392 myindex ( sizeof_COFF_section, sectab, i );
1395 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1398 /* Ignore sections called which contain stabs debugging
1400 if (0 == strcmp(".stab", sectab_i->Name)
1401 || 0 == strcmp(".stabstr", sectab_i->Name))
1404 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1406 COFF_reloc* reltab_j
1408 myindex ( sizeof_COFF_reloc, reltab, j );
1410 /* the location to patch */
1412 ((UChar*)(oc->image))
1413 + (sectab_i->PointerToRawData
1414 + reltab_j->VirtualAddress
1415 - sectab_i->VirtualAddress )
1417 /* the existing contents of pP */
1419 /* the symbol to connect to */
1420 sym = (COFF_symbol*)
1421 myindex ( sizeof_COFF_symbol,
1422 symtab, reltab_j->SymbolTableIndex );
1425 "reloc sec %2d num %3d: type 0x%-4x "
1426 "vaddr 0x%-8x name `",
1428 (UInt32)reltab_j->Type,
1429 reltab_j->VirtualAddress );
1430 printName ( sym->Name, strtab );
1431 fprintf ( stderr, "'\n" ));
1433 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1434 COFF_section* section_sym
1435 = findPEi386SectionCalled ( oc, sym->Name );
1437 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1440 S = ((UInt32)(oc->image))
1441 + (section_sym->PointerToRawData
1444 copyName ( sym->Name, strtab, symbol, 1000-1 );
1445 (void*)S = lookupLocalSymbol( oc, symbol );
1446 if ((void*)S != NULL) goto foundit;
1447 (void*)S = lookupSymbol( symbol );
1448 if ((void*)S != NULL) goto foundit;
1449 zapTrailingAtSign ( symbol );
1450 (void*)S = lookupLocalSymbol( oc, symbol );
1451 if ((void*)S != NULL) goto foundit;
1452 (void*)S = lookupSymbol( symbol );
1453 if ((void*)S != NULL) goto foundit;
1454 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1458 checkProddableBlock(oc, pP);
1459 switch (reltab_j->Type) {
1460 case MYIMAGE_REL_I386_DIR32:
1463 case MYIMAGE_REL_I386_REL32:
1464 /* Tricky. We have to insert a displacement at
1465 pP which, when added to the PC for the _next_
1466 insn, gives the address of the target (S).
1467 Problem is to know the address of the next insn
1468 when we only know pP. We assume that this
1469 literal field is always the last in the insn,
1470 so that the address of the next insn is pP+4
1471 -- hence the constant 4.
1472 Also I don't know if A should be added, but so
1473 far it has always been zero.
1476 *pP = S - ((UInt32)pP) - 4;
1479 belch("%s: unhandled PEi386 relocation type %d",
1480 oc->fileName, reltab_j->Type);
1487 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1491 #endif /* defined(OBJFORMAT_PEi386) */
1494 /* --------------------------------------------------------------------------
1496 * ------------------------------------------------------------------------*/
1498 #if defined(OBJFORMAT_ELF)
1503 #if defined(sparc_TARGET_ARCH)
1504 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1505 #elif defined(i386_TARGET_ARCH)
1506 # define ELF_TARGET_386 /* Used inside <elf.h> */
1508 /* There is a similar case for IA64 in the Solaris2 headers if this
1509 * ever becomes relevant.
1515 findElfSection ( void* objImage, Elf32_Word sh_type )
1518 char* ehdrC = (char*)objImage;
1519 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1520 Elf32_Shdr* shdr = (Elf32_Shdr*)(ehdrC + ehdr->e_shoff);
1521 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1523 for (i = 0; i < ehdr->e_shnum; i++) {
1524 if (shdr[i].sh_type == sh_type
1525 /* Ignore the section header's string table. */
1526 && i != ehdr->e_shstrndx
1527 /* Ignore string tables named .stabstr, as they contain
1529 && 0 != strcmp(".stabstr", sh_strtab + shdr[i].sh_name)
1531 ptr = ehdrC + shdr[i].sh_offset;
1540 ocVerifyImage_ELF ( ObjectCode* oc )
1544 int i, j, nent, nstrtab, nsymtabs;
1548 char* ehdrC = (char*)(oc->image);
1549 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1551 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1552 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1553 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1554 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1555 belch("%s: not an ELF header", oc->fileName);
1558 IF_DEBUG(linker,belch( "Is an ELF header" ));
1560 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1561 belch("%s: not 32 bit ELF", oc->fileName);
1565 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1567 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1568 IF_DEBUG(linker,belch( "Is little-endian" ));
1570 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1571 IF_DEBUG(linker,belch( "Is big-endian" ));
1573 belch("%s: unknown endiannness", oc->fileName);
1577 if (ehdr->e_type != ET_REL) {
1578 belch("%s: not a relocatable object (.o) file", oc->fileName);
1581 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1583 IF_DEBUG(linker,belch( "Architecture is " ));
1584 switch (ehdr->e_machine) {
1585 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1586 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1587 default: IF_DEBUG(linker,belch( "unknown" ));
1588 belch("%s: unknown architecture", oc->fileName);
1592 IF_DEBUG(linker,belch(
1593 "\nSection header table: start %d, n_entries %d, ent_size %d",
1594 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1596 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1598 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1600 if (ehdr->e_shstrndx == SHN_UNDEF) {
1601 belch("%s: no section header string table", oc->fileName);
1604 IF_DEBUG(linker,belch( "Section header string table is section %d",
1606 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1609 for (i = 0; i < ehdr->e_shnum; i++) {
1610 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1611 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1612 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1613 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1614 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1615 ehdrC + shdr[i].sh_offset,
1616 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1618 if (shdr[i].sh_type == SHT_REL) {
1619 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1620 } else if (shdr[i].sh_type == SHT_RELA) {
1621 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1623 IF_DEBUG(linker,fprintf(stderr," "));
1626 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1630 IF_DEBUG(linker,belch( "\nString tables" ));
1633 for (i = 0; i < ehdr->e_shnum; i++) {
1634 if (shdr[i].sh_type == SHT_STRTAB
1635 /* Ignore the section header's string table. */
1636 && i != ehdr->e_shstrndx
1637 /* Ignore string tables named .stabstr, as they contain
1639 && 0 != strcmp(".stabstr", sh_strtab + shdr[i].sh_name)
1641 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1642 strtab = ehdrC + shdr[i].sh_offset;
1647 belch("%s: no string tables, or too many", oc->fileName);
1652 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1653 for (i = 0; i < ehdr->e_shnum; i++) {
1654 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1655 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1657 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1658 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1659 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1661 shdr[i].sh_size % sizeof(Elf32_Sym)
1663 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1664 belch("%s: non-integral number of symbol table entries", oc->fileName);
1667 for (j = 0; j < nent; j++) {
1668 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1669 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1670 (int)stab[j].st_shndx,
1671 (int)stab[j].st_size,
1672 (char*)stab[j].st_value ));
1674 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1675 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1676 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1677 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1678 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1679 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1680 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1681 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1683 IF_DEBUG(linker,fprintf(stderr, " " ));
1685 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1686 switch (ELF32_ST_BIND(stab[j].st_info)) {
1687 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1688 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1689 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1690 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1692 IF_DEBUG(linker,fprintf(stderr, " " ));
1694 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1698 if (nsymtabs == 0) {
1699 belch("%s: didn't find any symbol tables", oc->fileName);
1708 ocGetNames_ELF ( ObjectCode* oc )
1713 char* ehdrC = (char*)(oc->image);
1714 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1715 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1716 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1717 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1719 ASSERT(symhash != NULL);
1722 belch("%s: no strtab", oc->fileName);
1727 for (i = 0; i < ehdr->e_shnum; i++) {
1729 /* make a section entry for relevant sections */
1730 SectionKind kind = SECTIONKIND_OTHER;
1731 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1732 !strcmp(".data1",sh_strtab+shdr[i].sh_name) ||
1733 !strcmp(".bss",sh_strtab+shdr[i].sh_name))
1734 kind = SECTIONKIND_RWDATA;
1735 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1736 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1737 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1738 kind = SECTIONKIND_CODE_OR_RODATA;
1740 if (!strcmp(".bss",sh_strtab+shdr[i].sh_name) && shdr[i].sh_size > 0) {
1741 /* This is a non-empty .bss section. Allocate zeroed space for
1742 it, and set its .sh_offset field such that
1743 ehdrC + .sh_offset == addr_of_zeroed_space. */
1744 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
1745 "ocGetNames_ELF(BSS)");
1746 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
1748 fprintf(stderr, "BSS section at 0x%x, size %d\n",
1749 zspace, shdr[i].sh_size);
1753 /* fill in the section info */
1754 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
1755 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
1756 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0)
1757 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
1759 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1761 /* copy stuff into this module's object symbol table */
1762 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1763 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1765 oc->n_symbols = nent;
1766 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1767 "ocGetNames_ELF(oc->symbols)");
1769 for (j = 0; j < nent; j++) {
1771 char isLocal = FALSE; /* avoids uninit-var warning */
1773 char* nm = strtab + stab[j].st_name;
1774 int secno = stab[j].st_shndx;
1776 /* Figure out if we want to add it; if so, set ad to its
1777 address. Otherwise leave ad == NULL. */
1779 if (secno == SHN_COMMON) {
1781 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
1783 fprintf(stderr, "COMMON symbol, size %d name %s\n",
1784 stab[j].st_size, nm);
1786 /* Pointless to do addProddableBlock() for this area,
1787 since the linker should never poke around in it. */
1790 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1791 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1793 /* and not an undefined symbol */
1794 && stab[j].st_shndx != SHN_UNDEF
1795 /* and not in a "special section" */
1796 && stab[j].st_shndx < SHN_LORESERVE
1798 /* and it's a not a section or string table or anything silly */
1799 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1800 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1801 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1804 /* Section 0 is the undefined section, hence > and not >=. */
1805 ASSERT(secno > 0 && secno < ehdr->e_shnum);
1807 if (shdr[secno].sh_type == SHT_NOBITS) {
1808 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
1809 stab[j].st_size, stab[j].st_value, nm);
1812 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
1813 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1814 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1815 ad, oc->fileName, nm ));
1818 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1819 ad, oc->fileName, nm ));
1824 /* And the decision is ... */
1828 oc->symbols[j] = nm;
1831 insertStrHashTable(oc->lochash, nm, ad);
1833 insertStrHashTable(symhash, nm, ad);
1837 IF_DEBUG(linker,belch( "skipping `%s'",
1838 strtab + stab[j].st_name ));
1841 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1842 (int)ELF32_ST_BIND(stab[j].st_info),
1843 (int)ELF32_ST_TYPE(stab[j].st_info),
1844 (int)stab[j].st_shndx,
1845 strtab + stab[j].st_name
1848 oc->symbols[j] = NULL;
1858 /* Do ELF relocations which lack an explicit addend. All x86-linux
1859 relocations appear to be of this form. */
1861 do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1862 Elf32_Shdr* shdr, int shnum,
1863 Elf32_Sym* stab, char* strtab )
1868 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1869 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1870 int target_shndx = shdr[shnum].sh_info;
1871 int symtab_shndx = shdr[shnum].sh_link;
1872 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1873 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1874 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1875 target_shndx, symtab_shndx ));
1876 for (j = 0; j < nent; j++) {
1877 Elf32_Addr offset = rtab[j].r_offset;
1878 Elf32_Word info = rtab[j].r_info;
1880 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1881 Elf32_Word* pP = (Elf32_Word*)P;
1885 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1886 j, (void*)offset, (void*)info ));
1888 IF_DEBUG(linker,belch( " ZERO" ));
1891 /* First see if it is a nameless local symbol. */
1892 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1893 symbol = "(noname)";
1895 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1896 + stab[ELF32_R_SYM(info)].st_value);
1898 /* No? Should be in a symbol table then; first try the
1900 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1901 (void*)S = lookupLocalSymbol( oc, symbol );
1902 if ((void*)S == NULL)
1903 (void*)S = lookupSymbol( symbol );
1906 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1909 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1911 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1912 (void*)P, (void*)S, (void*)A ));
1913 checkProddableBlock ( oc, pP );
1914 switch (ELF32_R_TYPE(info)) {
1915 # ifdef i386_TARGET_ARCH
1916 case R_386_32: *pP = S + A; break;
1917 case R_386_PC32: *pP = S + A - P; break;
1920 belch("%s: unhandled ELF relocation(Rel) type %d\n",
1921 oc->fileName, ELF32_R_TYPE(info));
1930 /* Do ELF relocations for which explicit addends are supplied.
1931 sparc-solaris relocations appear to be of this form. */
1933 do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1934 Elf32_Shdr* shdr, int shnum,
1935 Elf32_Sym* stab, char* strtab )
1940 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1941 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1942 int target_shndx = shdr[shnum].sh_info;
1943 int symtab_shndx = shdr[shnum].sh_link;
1944 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1945 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1946 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1947 target_shndx, symtab_shndx ));
1948 for (j = 0; j < nent; j++) {
1949 Elf32_Addr offset = rtab[j].r_offset;
1950 Elf32_Word info = rtab[j].r_info;
1951 Elf32_Sword addend = rtab[j].r_addend;
1952 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1953 Elf32_Addr A = addend;
1955 # if defined(sparc_TARGET_ARCH)
1956 /* This #ifdef only serves to avoid unused-var warnings. */
1957 Elf32_Word* pP = (Elf32_Word*)P;
1961 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1962 j, (void*)offset, (void*)info,
1965 IF_DEBUG(linker,belch( " ZERO" ));
1968 /* First see if it is a nameless local symbol. */
1969 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1970 symbol = "(noname)";
1972 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1973 + stab[ELF32_R_SYM(info)].st_value);
1975 /* No? Should be in a symbol table then; first try the
1977 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1978 (void*)S = lookupLocalSymbol( oc, symbol );
1979 if ((void*)S == NULL)
1980 (void*)S = lookupSymbol( symbol );
1983 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1987 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1990 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1992 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1993 (void*)P, (void*)S, (void*)A ));
1994 checkProddableBlock ( oc, (void*)P );
1995 switch (ELF32_R_TYPE(info)) {
1996 # if defined(sparc_TARGET_ARCH)
1997 case R_SPARC_WDISP30:
1998 w1 = *pP & 0xC0000000;
1999 w2 = (Elf32_Word)((S + A - P) >> 2);
2000 ASSERT((w2 & 0xC0000000) == 0);
2005 w1 = *pP & 0xFFC00000;
2006 w2 = (Elf32_Word)((S + A) >> 10);
2007 ASSERT((w2 & 0xFFC00000) == 0);
2013 w2 = (Elf32_Word)((S + A) & 0x3FF);
2014 ASSERT((w2 & ~0x3FF) == 0);
2018 /* According to the Sun documentation:
2020 This relocation type resembles R_SPARC_32, except it refers to an
2021 unaligned word. That is, the word to be relocated must be treated
2022 as four separate bytes with arbitrary alignment, not as a word
2023 aligned according to the architecture requirements.
2025 (JRS: which means that freeloading on the R_SPARC_32 case
2026 is probably wrong, but hey ...)
2030 w2 = (Elf32_Word)(S + A);
2035 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2036 oc->fileName, ELF32_R_TYPE(info));
2046 ocResolve_ELF ( ObjectCode* oc )
2050 Elf32_Sym* stab = NULL;
2051 char* ehdrC = (char*)(oc->image);
2052 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
2053 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
2054 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
2056 /* first find "the" symbol table */
2057 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2059 /* also go find the string table */
2060 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2062 if (stab == NULL || strtab == NULL) {
2063 belch("%s: can't find string or symbol table", oc->fileName);
2067 /* Process the relocation sections. */
2068 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2070 /* Skip sections called ".rel.stab". These appear to contain
2071 relocation entries that, when done, make the stabs debugging
2072 info point at the right places. We ain't interested in all
2074 if (0 == strcmp(".rel.stab", sh_strtab + shdr[shnum].sh_name))
2077 if (shdr[shnum].sh_type == SHT_REL ) {
2078 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
2079 shnum, stab, strtab );
2083 if (shdr[shnum].sh_type == SHT_RELA) {
2084 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
2085 shnum, stab, strtab );
2091 /* Free the local symbol table; we won't need it again. */
2092 freeHashTable(oc->lochash, NULL);