1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.68 2001/10/01 13:10:53 simonmar 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 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1260 addSection(oc, kind, start, end);
1261 addProddableBlock(oc, start, end - start + 1);
1265 /* Copy exported symbols into the ObjectCode. */
1267 oc->n_symbols = hdr->NumberOfSymbols;
1268 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1269 "ocGetNames_PEi386(oc->symbols)");
1270 /* Call me paranoid; I don't care. */
1271 for (i = 0; i < oc->n_symbols; i++)
1272 oc->symbols[i] = NULL;
1276 COFF_symbol* symtab_i;
1277 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1278 symtab_i = (COFF_symbol*)
1279 myindex ( sizeof_COFF_symbol, symtab, i );
1283 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1284 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1285 /* This symbol is global and defined, viz, exported */
1286 /* for MYIMAGE_SYMCLASS_EXTERNAL
1287 && !MYIMAGE_SYM_UNDEFINED,
1288 the address of the symbol is:
1289 address of relevant section + offset in section
1291 COFF_section* sectabent
1292 = (COFF_section*) myindex ( sizeof_COFF_section,
1294 symtab_i->SectionNumber-1 );
1295 addr = ((UChar*)(oc->image))
1296 + (sectabent->PointerToRawData
1300 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1301 && symtab_i->Value > 0) {
1302 /* This symbol isn't in any section at all, ie, global bss.
1303 Allocate zeroed space for it. */
1304 addr = stgCallocBytes(1, symtab_i->Value,
1305 "ocGetNames_PEi386(non-anonymous bss)");
1306 addSection(oc, SECTIONKIND_RWDATA, addr,
1307 ((UChar*)addr) + symtab_i->Value - 1);
1308 addProddableBlock(oc, addr, symtab_i->Value);
1309 /* fprintf(stderr, "BSS section at 0x%x\n", addr); */
1313 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1314 /* fprintf(stderr,"addSymbol %p `%s'\n", addr,sname); */
1315 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1316 ASSERT(i >= 0 && i < oc->n_symbols);
1317 /* cstring_from_COFF_symbol_name always succeeds. */
1318 oc->symbols[i] = sname;
1319 insertStrHashTable(symhash, sname, addr);
1323 "IGNORING symbol %d\n"
1327 printName ( symtab_i->Name, strtab );
1336 (Int32)(symtab_i->SectionNumber),
1337 (UInt32)symtab_i->Type,
1338 (UInt32)symtab_i->StorageClass,
1339 (UInt32)symtab_i->NumberOfAuxSymbols
1344 i += symtab_i->NumberOfAuxSymbols;
1353 ocResolve_PEi386 ( ObjectCode* oc )
1356 COFF_section* sectab;
1357 COFF_symbol* symtab;
1366 /* ToDo: should be variable-sized? But is at least safe in the
1367 sense of buffer-overrun-proof. */
1369 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1371 hdr = (COFF_header*)(oc->image);
1372 sectab = (COFF_section*) (
1373 ((UChar*)(oc->image))
1374 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1376 symtab = (COFF_symbol*) (
1377 ((UChar*)(oc->image))
1378 + hdr->PointerToSymbolTable
1380 strtab = ((UChar*)(oc->image))
1381 + hdr->PointerToSymbolTable
1382 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1384 for (i = 0; i < hdr->NumberOfSections; i++) {
1385 COFF_section* sectab_i
1387 myindex ( sizeof_COFF_section, sectab, i );
1390 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1392 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1394 COFF_reloc* reltab_j
1396 myindex ( sizeof_COFF_reloc, reltab, j );
1398 /* the location to patch */
1400 ((UChar*)(oc->image))
1401 + (sectab_i->PointerToRawData
1402 + reltab_j->VirtualAddress
1403 - sectab_i->VirtualAddress )
1405 /* the existing contents of pP */
1407 /* the symbol to connect to */
1408 sym = (COFF_symbol*)
1409 myindex ( sizeof_COFF_symbol,
1410 symtab, reltab_j->SymbolTableIndex );
1413 "reloc sec %2d num %3d: type 0x%-4x "
1414 "vaddr 0x%-8x name `",
1416 (UInt32)reltab_j->Type,
1417 reltab_j->VirtualAddress );
1418 printName ( sym->Name, strtab );
1419 fprintf ( stderr, "'\n" ));
1421 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1422 COFF_section* section_sym
1423 = findPEi386SectionCalled ( oc, sym->Name );
1425 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1428 S = ((UInt32)(oc->image))
1429 + (section_sym->PointerToRawData
1432 copyName ( sym->Name, strtab, symbol, 1000-1 );
1433 (void*)S = lookupLocalSymbol( oc, symbol );
1434 if ((void*)S != NULL) goto foundit;
1435 (void*)S = lookupSymbol( symbol );
1436 if ((void*)S != NULL) goto foundit;
1437 zapTrailingAtSign ( symbol );
1438 (void*)S = lookupLocalSymbol( oc, symbol );
1439 if ((void*)S != NULL) goto foundit;
1440 (void*)S = lookupSymbol( symbol );
1441 if ((void*)S != NULL) goto foundit;
1442 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1446 checkProddableBlock(oc, pP);
1447 switch (reltab_j->Type) {
1448 case MYIMAGE_REL_I386_DIR32:
1451 case MYIMAGE_REL_I386_REL32:
1452 /* Tricky. We have to insert a displacement at
1453 pP which, when added to the PC for the _next_
1454 insn, gives the address of the target (S).
1455 Problem is to know the address of the next insn
1456 when we only know pP. We assume that this
1457 literal field is always the last in the insn,
1458 so that the address of the next insn is pP+4
1459 -- hence the constant 4.
1460 Also I don't know if A should be added, but so
1461 far it has always been zero.
1464 *pP = S - ((UInt32)pP) - 4;
1467 belch("%s: unhandled PEi386 relocation type %d",
1468 oc->fileName, reltab_j->Type);
1475 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1479 #endif /* defined(OBJFORMAT_PEi386) */
1482 /* --------------------------------------------------------------------------
1484 * ------------------------------------------------------------------------*/
1486 #if defined(OBJFORMAT_ELF)
1491 #if defined(sparc_TARGET_ARCH)
1492 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1493 #elif defined(i386_TARGET_ARCH)
1494 # define ELF_TARGET_386 /* Used inside <elf.h> */
1496 /* There is a similar case for IA64 in the Solaris2 headers if this
1497 * ever becomes relevant.
1503 findElfSection ( void* objImage, Elf32_Word sh_type )
1506 char* ehdrC = (char*)objImage;
1507 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1508 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1510 for (i = 0; i < ehdr->e_shnum; i++) {
1511 if (shdr[i].sh_type == sh_type &&
1512 i != ehdr->e_shstrndx) {
1513 ptr = ehdrC + shdr[i].sh_offset;
1522 ocVerifyImage_ELF ( ObjectCode* oc )
1526 int i, j, nent, nstrtab, nsymtabs;
1530 char* ehdrC = (char*)(oc->image);
1531 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1533 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1534 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1535 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1536 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1537 belch("%s: not an ELF header", oc->fileName);
1540 IF_DEBUG(linker,belch( "Is an ELF header" ));
1542 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1543 belch("%s: not 32 bit ELF", oc->fileName);
1547 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1549 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1550 IF_DEBUG(linker,belch( "Is little-endian" ));
1552 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1553 IF_DEBUG(linker,belch( "Is big-endian" ));
1555 belch("%s: unknown endiannness", oc->fileName);
1559 if (ehdr->e_type != ET_REL) {
1560 belch("%s: not a relocatable object (.o) file", oc->fileName);
1563 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1565 IF_DEBUG(linker,belch( "Architecture is " ));
1566 switch (ehdr->e_machine) {
1567 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1568 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1569 default: IF_DEBUG(linker,belch( "unknown" ));
1570 belch("%s: unknown architecture", oc->fileName);
1574 IF_DEBUG(linker,belch(
1575 "\nSection header table: start %d, n_entries %d, ent_size %d",
1576 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1578 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1580 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1582 if (ehdr->e_shstrndx == SHN_UNDEF) {
1583 belch("%s: no section header string table", oc->fileName);
1586 IF_DEBUG(linker,belch( "Section header string table is section %d",
1588 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1591 for (i = 0; i < ehdr->e_shnum; i++) {
1592 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1593 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1594 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1595 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1596 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1597 ehdrC + shdr[i].sh_offset,
1598 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1600 if (shdr[i].sh_type == SHT_REL) {
1601 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1602 } else if (shdr[i].sh_type == SHT_RELA) {
1603 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1605 IF_DEBUG(linker,fprintf(stderr," "));
1608 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1612 IF_DEBUG(linker,belch( "\nString tables" ));
1615 for (i = 0; i < ehdr->e_shnum; i++) {
1616 if (shdr[i].sh_type == SHT_STRTAB &&
1617 i != ehdr->e_shstrndx) {
1618 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1619 strtab = ehdrC + shdr[i].sh_offset;
1624 belch("%s: no string tables, or too many", oc->fileName);
1629 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1630 for (i = 0; i < ehdr->e_shnum; i++) {
1631 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1632 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1634 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1635 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1636 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1638 shdr[i].sh_size % sizeof(Elf32_Sym)
1640 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1641 belch("%s: non-integral number of symbol table entries", oc->fileName);
1644 for (j = 0; j < nent; j++) {
1645 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1646 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1647 (int)stab[j].st_shndx,
1648 (int)stab[j].st_size,
1649 (char*)stab[j].st_value ));
1651 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1652 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1653 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1654 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1655 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1656 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1657 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1658 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1660 IF_DEBUG(linker,fprintf(stderr, " " ));
1662 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1663 switch (ELF32_ST_BIND(stab[j].st_info)) {
1664 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1665 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1666 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1667 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1669 IF_DEBUG(linker,fprintf(stderr, " " ));
1671 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1675 if (nsymtabs == 0) {
1676 belch("%s: didn't find any symbol tables", oc->fileName);
1685 ocGetNames_ELF ( ObjectCode* oc )
1690 char* ehdrC = (char*)(oc->image);
1691 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1692 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1693 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1694 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1696 ASSERT(symhash != NULL);
1699 belch("%s: no strtab", oc->fileName);
1704 for (i = 0; i < ehdr->e_shnum; i++) {
1706 /* make a section entry for relevant sections */
1707 SectionKind kind = SECTIONKIND_OTHER;
1708 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1709 !strcmp(".data1",sh_strtab+shdr[i].sh_name) ||
1710 !strcmp(".bss",sh_strtab+shdr[i].sh_name))
1711 kind = SECTIONKIND_RWDATA;
1712 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1713 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1714 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1715 kind = SECTIONKIND_CODE_OR_RODATA;
1717 if (!strcmp(".bss",sh_strtab+shdr[i].sh_name) && shdr[i].sh_size > 0) {
1718 /* This is a non-empty .bss section. Allocate zeroed space for
1719 it, and set its .sh_offset field such that
1720 ehdrC + .sh_offset == addr_of_zeroed_space. */
1721 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
1722 "ocGetNames_ELF(BSS)");
1723 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
1725 fprintf(stderr, "BSS section at 0x%x, size %d\n",
1726 zspace, shdr[i].sh_size);
1730 /* fill in the section info */
1731 addSection(oc, kind, ehdrC + shdr[i].sh_offset,
1732 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
1733 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0)
1734 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
1736 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1738 /* copy stuff into this module's object symbol table */
1739 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1740 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1742 oc->n_symbols = nent;
1743 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1744 "ocGetNames_ELF(oc->symbols)");
1746 for (j = 0; j < nent; j++) {
1748 char isLocal = FALSE; /* avoids uninit-var warning */
1750 char* nm = strtab + stab[j].st_name;
1751 int secno = stab[j].st_shndx;
1753 /* Figure out if we want to add it; if so, set ad to its
1754 address. Otherwise leave ad == NULL. */
1756 if (secno == SHN_COMMON) {
1758 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
1760 fprintf(stderr, "COMMON symbol, size %d name %s\n",
1761 stab[j].st_size, nm);
1763 /* Pointless to do addProddableBlock() for this area,
1764 since the linker should never poke around in it. */
1767 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1768 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1770 /* and not an undefined symbol */
1771 && stab[j].st_shndx != SHN_UNDEF
1772 /* and not in a "special section" */
1773 && stab[j].st_shndx < SHN_LORESERVE
1775 /* and it's a not a section or string table or anything silly */
1776 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1777 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1778 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1781 /* Section 0 is the undefined section, hence > and not >=. */
1782 ASSERT(secno > 0 && secno < ehdr->e_shnum);
1784 if (shdr[secno].sh_type == SHT_NOBITS) {
1785 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
1786 stab[j].st_size, stab[j].st_value, nm);
1789 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
1790 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1791 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1792 ad, oc->fileName, nm ));
1795 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1796 ad, oc->fileName, nm ));
1801 /* And the decision is ... */
1805 oc->symbols[j] = nm;
1808 insertStrHashTable(oc->lochash, nm, ad);
1810 insertStrHashTable(symhash, nm, ad);
1814 IF_DEBUG(linker,belch( "skipping `%s'",
1815 strtab + stab[j].st_name ));
1818 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1819 (int)ELF32_ST_BIND(stab[j].st_info),
1820 (int)ELF32_ST_TYPE(stab[j].st_info),
1821 (int)stab[j].st_shndx,
1822 strtab + stab[j].st_name
1825 oc->symbols[j] = NULL;
1835 /* Do ELF relocations which lack an explicit addend. All x86-linux
1836 relocations appear to be of this form. */
1838 do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1839 Elf32_Shdr* shdr, int shnum,
1840 Elf32_Sym* stab, char* strtab )
1845 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1846 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1847 int target_shndx = shdr[shnum].sh_info;
1848 int symtab_shndx = shdr[shnum].sh_link;
1849 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1850 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1851 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1852 target_shndx, symtab_shndx ));
1853 for (j = 0; j < nent; j++) {
1854 Elf32_Addr offset = rtab[j].r_offset;
1855 Elf32_Word info = rtab[j].r_info;
1857 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1858 Elf32_Word* pP = (Elf32_Word*)P;
1862 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1863 j, (void*)offset, (void*)info ));
1865 IF_DEBUG(linker,belch( " ZERO" ));
1868 /* First see if it is a nameless local symbol. */
1869 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1870 symbol = "(noname)";
1872 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1873 + stab[ELF32_R_SYM(info)].st_value);
1875 /* No? Should be in a symbol table then; first try the
1877 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1878 (void*)S = lookupLocalSymbol( oc, symbol );
1879 if ((void*)S == NULL)
1880 (void*)S = lookupSymbol( symbol );
1883 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1886 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1888 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1889 (void*)P, (void*)S, (void*)A ));
1890 checkProddableBlock ( oc, pP );
1891 switch (ELF32_R_TYPE(info)) {
1892 # ifdef i386_TARGET_ARCH
1893 case R_386_32: *pP = S + A; break;
1894 case R_386_PC32: *pP = S + A - P; break;
1897 belch("%s: unhandled ELF relocation(Rel) type %d\n",
1898 oc->fileName, ELF32_R_TYPE(info));
1907 /* Do ELF relocations for which explicit addends are supplied.
1908 sparc-solaris relocations appear to be of this form. */
1910 do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1911 Elf32_Shdr* shdr, int shnum,
1912 Elf32_Sym* stab, char* strtab )
1917 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1918 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1919 int target_shndx = shdr[shnum].sh_info;
1920 int symtab_shndx = shdr[shnum].sh_link;
1921 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1922 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1923 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1924 target_shndx, symtab_shndx ));
1925 for (j = 0; j < nent; j++) {
1926 Elf32_Addr offset = rtab[j].r_offset;
1927 Elf32_Word info = rtab[j].r_info;
1928 Elf32_Sword addend = rtab[j].r_addend;
1929 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1930 Elf32_Addr A = addend;
1932 # if defined(sparc_TARGET_ARCH)
1933 /* This #ifdef only serves to avoid unused-var warnings. */
1934 Elf32_Word* pP = (Elf32_Word*)P;
1938 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1939 j, (void*)offset, (void*)info,
1942 IF_DEBUG(linker,belch( " ZERO" ));
1945 /* First see if it is a nameless local symbol. */
1946 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1947 symbol = "(noname)";
1949 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1950 + stab[ELF32_R_SYM(info)].st_value);
1952 /* No? Should be in a symbol table then; first try the
1954 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1955 (void*)S = lookupLocalSymbol( oc, symbol );
1956 if ((void*)S == NULL)
1957 (void*)S = lookupSymbol( symbol );
1960 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1964 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1967 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1969 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1970 (void*)P, (void*)S, (void*)A ));
1971 checkProddableBlock ( oc, (void*)P );
1972 switch (ELF32_R_TYPE(info)) {
1973 # if defined(sparc_TARGET_ARCH)
1974 case R_SPARC_WDISP30:
1975 w1 = *pP & 0xC0000000;
1976 w2 = (Elf32_Word)((S + A - P) >> 2);
1977 ASSERT((w2 & 0xC0000000) == 0);
1982 w1 = *pP & 0xFFC00000;
1983 w2 = (Elf32_Word)((S + A) >> 10);
1984 ASSERT((w2 & 0xFFC00000) == 0);
1990 w2 = (Elf32_Word)((S + A) & 0x3FF);
1991 ASSERT((w2 & ~0x3FF) == 0);
1995 /* According to the Sun documentation:
1997 This relocation type resembles R_SPARC_32, except it refers to an
1998 unaligned word. That is, the word to be relocated must be treated
1999 as four separate bytes with arbitrary alignment, not as a word
2000 aligned according to the architecture requirements.
2002 (JRS: which means that freeloading on the R_SPARC_32 case
2003 is probably wrong, but hey ...)
2007 w2 = (Elf32_Word)(S + A);
2012 belch("%s: unhandled ELF relocation(RelA) type %d\n",
2013 oc->fileName, ELF32_R_TYPE(info));
2023 ocResolve_ELF ( ObjectCode* oc )
2027 Elf32_Sym* stab = NULL;
2028 char* ehdrC = (char*)(oc->image);
2029 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
2030 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
2032 /* first find "the" symbol table */
2033 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
2035 /* also go find the string table */
2036 strtab = findElfSection ( ehdrC, SHT_STRTAB );
2038 if (stab == NULL || strtab == NULL) {
2039 belch("%s: can't find string or symbol table", oc->fileName);
2043 /* Process the relocation sections. */
2044 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2045 if (shdr[shnum].sh_type == SHT_REL ) {
2046 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
2047 shnum, stab, strtab );
2051 if (shdr[shnum].sh_type == SHT_RELA) {
2052 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
2053 shnum, stab, strtab );
2058 /* Free the local symbol table; we won't need it again. */
2059 freeHashTable(oc->lochash, NULL);