1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.61 2001/08/31 11:44:12 sewardj Exp $
4 * (c) The GHC Team, 2000, 2001
8 * ---------------------------------------------------------------------------*/
10 #include "PosixSource.h"
16 #include "LinkerInternals.h"
18 #include "StoragePriv.h"
20 #ifdef HAVE_SYS_TYPES_H
21 #include <sys/types.h>
24 #ifdef HAVE_SYS_STAT_H
32 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
33 # define OBJFORMAT_ELF
34 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
35 # define OBJFORMAT_PEi386
39 /* Hash table mapping symbol names to Symbol */
40 /*Str*/HashTable *symhash;
42 #if defined(OBJFORMAT_ELF)
43 static int ocVerifyImage_ELF ( ObjectCode* oc );
44 static int ocGetNames_ELF ( ObjectCode* oc );
45 static int ocResolve_ELF ( ObjectCode* oc );
46 #elif defined(OBJFORMAT_PEi386)
47 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
48 static int ocGetNames_PEi386 ( ObjectCode* oc );
49 static int ocResolve_PEi386 ( ObjectCode* oc );
52 /* -----------------------------------------------------------------------------
53 * Built-in symbols from the RTS
56 typedef struct _RtsSymbolVal {
63 #define Maybe_ForeignObj SymX(mkForeignObjzh_fast)
65 #define Maybe_Stable_Names SymX(mkWeakzh_fast) \
66 SymX(makeStableNamezh_fast) \
67 SymX(finalizzeWeakzh_fast)
69 /* These are not available in GUM!!! -- HWL */
70 #define Maybe_ForeignObj
71 #define Maybe_Stable_Names
74 #if !defined (mingw32_TARGET_OS)
76 #define RTS_POSIX_ONLY_SYMBOLS \
77 SymX(stg_sig_install) \
79 #define RTS_MINGW_ONLY_SYMBOLS /**/
83 #define RTS_POSIX_ONLY_SYMBOLS
85 /* These are statically linked from the mingw libraries into the ghc
86 executable, so we have to employ this hack. */
87 #define RTS_MINGW_ONLY_SYMBOLS \
100 SymX(getservbyport) \
101 SymX(getprotobynumber) \
102 SymX(getprotobyname) \
103 SymX(gethostbyname) \
104 SymX(gethostbyaddr) \
139 Sym(_imp___timezone) \
149 SymX(GetCurrentProcess) \
150 SymX(GetProcessTimes) \
152 SymX(GetExitCodeProcess) \
153 SymX(WaitForSingleObject) \
154 SymX(CreateProcessA) \
163 #define RTS_SYMBOLS \
165 Sym(stg_gc_enter_1) \
172 Sym(stg_gc_unpt_r1) \
173 Sym(stg_gc_unbx_r1) \
178 SymX(stg_update_PAP) \
179 SymX(stg_ap_1_upd_info) \
180 SymX(stg_ap_2_upd_info) \
181 SymX(stg_ap_3_upd_info) \
182 SymX(stg_ap_4_upd_info) \
183 SymX(stg_ap_5_upd_info) \
184 SymX(stg_ap_6_upd_info) \
185 SymX(stg_ap_7_upd_info) \
186 SymX(stg_ap_8_upd_info) \
187 SymX(stg_sel_0_upd_info) \
188 SymX(stg_sel_1_upd_info) \
189 SymX(stg_sel_2_upd_info) \
190 SymX(stg_sel_3_upd_info) \
191 SymX(stg_sel_4_upd_info) \
192 SymX(stg_sel_5_upd_info) \
193 SymX(stg_sel_6_upd_info) \
194 SymX(stg_sel_7_upd_info) \
195 SymX(stg_sel_8_upd_info) \
196 SymX(stg_sel_9_upd_info) \
197 SymX(stg_sel_10_upd_info) \
198 SymX(stg_sel_11_upd_info) \
199 SymX(stg_sel_12_upd_info) \
200 SymX(stg_sel_13_upd_info) \
201 SymX(stg_sel_14_upd_info) \
202 SymX(stg_sel_15_upd_info) \
203 SymX(stg_upd_frame_info) \
204 SymX(stg_seq_frame_info) \
205 SymX(stg_CAF_BLACKHOLE_info) \
206 SymX(stg_IND_STATIC_info) \
207 SymX(stg_EMPTY_MVAR_info) \
208 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
209 SymX(stg_WEAK_info) \
210 SymX(stg_CHARLIKE_closure) \
211 SymX(stg_INTLIKE_closure) \
213 SymX(newBCOzh_fast) \
214 SymX(mkApUpd0zh_fast) \
215 SymX(putMVarzh_fast) \
216 SymX(newMVarzh_fast) \
217 SymX(takeMVarzh_fast) \
218 SymX(tryTakeMVarzh_fast) \
219 SymX(tryPutMVarzh_fast) \
225 SymX(killThreadzh_fast) \
226 SymX(waitReadzh_fast) \
227 SymX(waitWritezh_fast) \
228 SymX(suspendThread) \
230 SymX(stackOverflow) \
231 SymX(int2Integerzh_fast) \
232 SymX(word2Integerzh_fast) \
234 SymX(__encodeDouble) \
235 SymX(decodeDoublezh_fast) \
236 SymX(decodeFloatzh_fast) \
237 SymX(gcdIntegerzh_fast) \
238 SymX(newArrayzh_fast) \
239 SymX(unsafeThawArrayzh_fast) \
240 SymX(newByteArrayzh_fast) \
241 SymX(newPinnedByteArrayzh_fast) \
242 SymX(newMutVarzh_fast) \
243 SymX(quotRemIntegerzh_fast) \
244 SymX(quotIntegerzh_fast) \
245 SymX(remIntegerzh_fast) \
246 SymX(divExactIntegerzh_fast) \
247 SymX(divModIntegerzh_fast) \
248 SymX(timesIntegerzh_fast) \
249 SymX(minusIntegerzh_fast) \
250 SymX(plusIntegerzh_fast) \
251 SymX(andIntegerzh_fast) \
252 SymX(orIntegerzh_fast) \
253 SymX(xorIntegerzh_fast) \
254 SymX(complementIntegerzh_fast) \
256 SymX(blockAsyncExceptionszh_fast) \
257 SymX(unblockAsyncExceptionszh_fast) \
259 SymX(isDoubleInfinite) \
260 SymX(isDoubleDenormalized) \
261 SymX(isDoubleNegativeZero) \
262 SymX(__encodeFloat) \
264 SymX(isFloatInfinite) \
265 SymX(isFloatDenormalized) \
266 SymX(isFloatNegativeZero) \
267 SymX(__int_encodeFloat) \
268 SymX(__int_encodeDouble) \
269 SymX(__gmpz_cmp_si) \
270 SymX(__gmpz_cmp_ui) \
273 SymX(__gmpz_get_si) \
274 SymX(__gmpz_get_ui) \
279 SymX(resetNonBlockingFd) \
282 SymX(stable_ptr_table) \
283 SymX(shutdownHaskellAndExit) \
284 Sym(stg_enterStackTop) \
285 Sym(stg_yield_to_interpreter) \
289 Sym(__init_PrelGHC) \
290 SymX(freeHaskellFunctionPtr) \
293 SymX(NoRunnableThreadsHook) \
294 SymX(StackOverflowHook) \
295 SymX(OutOfHeapHook) \
296 SymX(MallocFailHook) \
297 SymX(PatErrorHdrHook) \
300 SymX(PostTraceHook) \
301 SymX(createAdjustor) \
316 SymX(rts_mkStablePtr) \
325 SymX(rts_getWord32) \
328 SymX(rts_getDouble) \
329 SymX(rts_getStablePtr) \
335 SymX(rts_evalLazyIO) \
336 SymX(rts_checkSchedStatus)
338 #ifndef SUPPORT_LONG_LONGS
339 #define RTS_LONG_LONG_SYMS /* nothing */
341 #define RTS_LONG_LONG_SYMS \
342 SymX(int64ToIntegerzh_fast) \
343 SymX(word64ToIntegerzh_fast)
344 #endif /* SUPPORT_LONG_LONGS */
346 /* entirely bogus claims about types of these symbols */
347 #define Sym(vvv) extern void (vvv);
348 #define SymX(vvv) /**/
351 RTS_POSIX_ONLY_SYMBOLS
352 RTS_MINGW_ONLY_SYMBOLS
356 #ifdef LEADING_UNDERSCORE
357 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
359 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
362 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
364 #define SymX(vvv) Sym(vvv)
366 static RtsSymbolVal rtsSyms[] = {
369 RTS_POSIX_ONLY_SYMBOLS
370 RTS_MINGW_ONLY_SYMBOLS
371 { 0, 0 } /* sentinel */
374 /* -----------------------------------------------------------------------------
375 * initialize the object linker
377 #if defined(OBJFORMAT_ELF)
378 static void *dl_prog_handle;
386 symhash = allocStrHashTable();
388 /* populate the symbol table with stuff from the RTS */
389 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
390 insertStrHashTable(symhash, sym->lbl, sym->addr);
392 # if defined(OBJFORMAT_ELF)
393 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
397 /* -----------------------------------------------------------------------------
398 * Add a DLL from which symbols may be found. In the ELF case, just
399 * do RTLD_GLOBAL-style add, so no further messing around needs to
400 * happen in order that symbols in the loaded .so are findable --
401 * lookupSymbol() will subsequently see them by dlsym on the program's
402 * dl-handle. Returns NULL if success, otherwise ptr to an err msg.
404 * In the PEi386 case, open the DLLs and put handles to them in a
405 * linked list. When looking for a symbol, try all handles in the
409 #if defined(OBJFORMAT_PEi386)
410 /* A record for storing handles into DLLs. */
415 struct _OpenedDLL* next;
420 /* A list thereof. */
421 static OpenedDLL* opened_dlls = NULL;
427 addDLL ( __attribute((unused)) char* path, char* dll_name )
429 # if defined(OBJFORMAT_ELF)
434 if (path == NULL || strlen(path) == 0) {
435 buf = stgMallocBytes(strlen(dll_name) + 10, "addDll");
436 sprintf(buf, "lib%s.so", dll_name);
438 buf = stgMallocBytes(strlen(path) + 1 + strlen(dll_name) + 10, "addDll");
439 sprintf(buf, "%s/lib%s.so", path, dll_name);
441 hdl = dlopen(buf, RTLD_NOW | RTLD_GLOBAL );
444 /* dlopen failed; return a ptr to the error msg. */
446 if (errmsg == NULL) errmsg = "addDLL: unknown error";
453 # elif defined(OBJFORMAT_PEi386)
455 /* Add this DLL to the list of DLLs in which to search for symbols.
456 The path argument is ignored. */
461 /* fprintf(stderr, "\naddDLL; path=`%s', dll_name = `%s'\n", path, dll_name); */
463 /* See if we've already got it, and ignore if so. */
464 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
465 if (0 == strcmp(o_dll->name, dll_name))
469 buf = stgMallocBytes(strlen(dll_name) + 10, "addDLL");
470 sprintf(buf, "%s.DLL", dll_name);
471 instance = LoadLibrary(buf);
473 if (instance == NULL) {
474 /* LoadLibrary failed; return a ptr to the error msg. */
475 return "addDLL: unknown error";
478 o_dll = stgMallocBytes( sizeof(OpenedDLL), "addDLL" );
479 o_dll->name = stgMallocBytes(1+strlen(dll_name), "addDLL");
480 strcpy(o_dll->name, dll_name);
481 o_dll->instance = instance;
482 o_dll->next = opened_dlls;
487 barf("addDLL: not implemented on this platform");
491 /* -----------------------------------------------------------------------------
492 * lookup a symbol in the hash table
495 lookupSymbol( char *lbl )
498 ASSERT(symhash != NULL);
499 val = lookupStrHashTable(symhash, lbl);
502 # if defined(OBJFORMAT_ELF)
503 return dlsym(dl_prog_handle, lbl);
504 # elif defined(OBJFORMAT_PEi386)
507 for (o_dll = opened_dlls; o_dll != NULL; o_dll = o_dll->next) {
508 /* fprintf(stderr, "look in %s for %s\n", o_dll->name, lbl); */
510 /* HACK: if the name has an initial underscore, try stripping
511 it off & look that up first. I've yet to verify whether there's
512 a Rule that governs whether an initial '_' *should always* be
513 stripped off when mapping from import lib name to the DLL name.
515 sym = GetProcAddress(o_dll->instance, (lbl+1));
516 if (sym != NULL) return sym;
518 sym = GetProcAddress(o_dll->instance, lbl);
519 if (sym != NULL) return sym;
533 lookupLocalSymbol( ObjectCode* oc, char *lbl )
536 val = lookupStrHashTable(oc->lochash, lbl);
546 /* -----------------------------------------------------------------------------
547 * Load an obj (populate the global symbol table, but don't resolve yet)
549 * Returns: 1 if ok, 0 on error.
552 loadObj( char *path )
559 /* fprintf(stderr, "loadObj %s\n", path ); */
561 /* assert that we haven't already loaded this object */
564 for (o = objects; o; o = o->next)
565 ASSERT(strcmp(o->fileName, path));
569 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
571 # if defined(OBJFORMAT_ELF)
572 oc->formatName = "ELF";
573 # elif defined(OBJFORMAT_PEi386)
574 oc->formatName = "PEi386";
577 barf("loadObj: not implemented on this platform");
581 if (r == -1) { return 0; }
583 /* sigh, strdup() isn't a POSIX function, so do it the long way */
584 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
585 strcpy(oc->fileName, path);
587 oc->fileSize = st.st_size;
588 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
591 oc->lochash = allocStrHashTable();
592 oc->proddables = NULL;
594 /* chain it onto the list of objects */
598 /* load the image into memory */
599 f = fopen(path, "rb");
601 barf("loadObj: can't read `%s'", path);
603 n = fread ( oc->image, 1, oc->fileSize, f );
604 if (n != oc->fileSize) {
606 barf("loadObj: error whilst reading `%s'", path);
609 /* verify the in-memory image */
610 # if defined(OBJFORMAT_ELF)
611 r = ocVerifyImage_ELF ( oc );
612 # elif defined(OBJFORMAT_PEi386)
613 r = ocVerifyImage_PEi386 ( oc );
615 barf("loadObj: no verify method");
617 if (!r) { return r; }
619 /* build the symbol list for this image */
620 # if defined(OBJFORMAT_ELF)
621 r = ocGetNames_ELF ( oc );
622 # elif defined(OBJFORMAT_PEi386)
623 r = ocGetNames_PEi386 ( oc );
625 barf("loadObj: no getNames method");
627 if (!r) { return r; }
629 /* loaded, but not resolved yet */
630 oc->status = OBJECT_LOADED;
635 /* -----------------------------------------------------------------------------
636 * resolve all the currently unlinked objects in memory
638 * Returns: 1 if ok, 0 on error.
646 for (oc = objects; oc; oc = oc->next) {
647 if (oc->status != OBJECT_RESOLVED) {
648 # if defined(OBJFORMAT_ELF)
649 r = ocResolve_ELF ( oc );
650 # elif defined(OBJFORMAT_PEi386)
651 r = ocResolve_PEi386 ( oc );
653 barf("resolveObjs: not implemented on this platform");
655 if (!r) { return r; }
656 oc->status = OBJECT_RESOLVED;
662 /* -----------------------------------------------------------------------------
663 * delete an object from the pool
666 unloadObj( char *path )
668 ObjectCode *oc, *prev;
670 ASSERT(symhash != NULL);
671 ASSERT(objects != NULL);
674 for (oc = objects; oc; prev = oc, oc = oc->next) {
675 if (!strcmp(oc->fileName,path)) {
677 /* Remove all the mappings for the symbols within this
682 for (i = 0; i < oc->n_symbols; i++) {
683 if (oc->symbols[i] != NULL) {
684 removeStrHashTable(symhash, oc->symbols[i], NULL);
692 prev->next = oc->next;
695 /* We're going to leave this in place, in case there are
696 any pointers from the heap into it: */
697 /* free(oc->image); */
701 /* The local hash table should have been freed at the end
702 of the ocResolve_ call on it. */
703 ASSERT(oc->lochash == NULL);
709 belch("unloadObj: can't find `%s' to unload", path);
713 /* -----------------------------------------------------------------------------
714 * Sanity checking. For each ObjectCode, maintain a list of address ranges
715 * which may be prodded during relocation, and abort if we try and write
716 * outside any of these.
718 static void addProddableBlock ( ObjectCode* oc, void* start, int size )
721 = stgMallocBytes(sizeof(ProddableBlock), "addProddableBlock");
722 /* fprintf(stderr, "aPB %p %p %d\n", oc, start, size); */
726 pb->next = oc->proddables;
730 static void checkProddableBlock ( ObjectCode* oc, void* addr )
733 for (pb = oc->proddables; pb != NULL; pb = pb->next) {
734 char* s = (char*)(pb->start);
735 char* e = s + pb->size - 1;
736 char* a = (char*)addr;
737 /* Assumes that the biggest fixup involves a 4-byte write. This
738 probably needs to be changed to 8 (ie, +7) on 64-bit
740 if (a >= s && (a+3) <= e) return;
742 barf("checkProddableBlock: invalid fixup in runtime linker");
746 /* --------------------------------------------------------------------------
747 * PEi386 specifics (Win32 targets)
748 * ------------------------------------------------------------------------*/
750 /* The information for this linker comes from
751 Microsoft Portable Executable
752 and Common Object File Format Specification
753 revision 5.1 January 1998
754 which SimonM says comes from the MS Developer Network CDs.
758 #if defined(OBJFORMAT_PEi386)
762 typedef unsigned char UChar;
763 typedef unsigned short UInt16;
764 typedef unsigned int UInt32;
771 UInt16 NumberOfSections;
772 UInt32 TimeDateStamp;
773 UInt32 PointerToSymbolTable;
774 UInt32 NumberOfSymbols;
775 UInt16 SizeOfOptionalHeader;
776 UInt16 Characteristics;
780 #define sizeof_COFF_header 20
787 UInt32 VirtualAddress;
788 UInt32 SizeOfRawData;
789 UInt32 PointerToRawData;
790 UInt32 PointerToRelocations;
791 UInt32 PointerToLinenumbers;
792 UInt16 NumberOfRelocations;
793 UInt16 NumberOfLineNumbers;
794 UInt32 Characteristics;
798 #define sizeof_COFF_section 40
805 UInt16 SectionNumber;
808 UChar NumberOfAuxSymbols;
812 #define sizeof_COFF_symbol 18
817 UInt32 VirtualAddress;
818 UInt32 SymbolTableIndex;
823 #define sizeof_COFF_reloc 10
826 /* From PE spec doc, section 3.3.2 */
827 /* Note use of MYIMAGE_* since IMAGE_* are already defined in
828 windows.h -- for the same purpose, but I want to know what I'm
830 #define MYIMAGE_FILE_RELOCS_STRIPPED 0x0001
831 #define MYIMAGE_FILE_EXECUTABLE_IMAGE 0x0002
832 #define MYIMAGE_FILE_DLL 0x2000
833 #define MYIMAGE_FILE_SYSTEM 0x1000
834 #define MYIMAGE_FILE_BYTES_REVERSED_HI 0x8000
835 #define MYIMAGE_FILE_BYTES_REVERSED_LO 0x0080
836 #define MYIMAGE_FILE_32BIT_MACHINE 0x0100
838 /* From PE spec doc, section 5.4.2 and 5.4.4 */
839 #define MYIMAGE_SYM_CLASS_EXTERNAL 2
840 #define MYIMAGE_SYM_CLASS_STATIC 3
841 #define MYIMAGE_SYM_UNDEFINED 0
843 /* From PE spec doc, section 4.1 */
844 #define MYIMAGE_SCN_CNT_CODE 0x00000020
845 #define MYIMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
847 /* From PE spec doc, section 5.2.1 */
848 #define MYIMAGE_REL_I386_DIR32 0x0006
849 #define MYIMAGE_REL_I386_REL32 0x0014
852 /* We use myindex to calculate array addresses, rather than
853 simply doing the normal subscript thing. That's because
854 some of the above structs have sizes which are not
855 a whole number of words. GCC rounds their sizes up to a
856 whole number of words, which means that the address calcs
857 arising from using normal C indexing or pointer arithmetic
858 are just plain wrong. Sigh.
861 myindex ( int scale, void* base, int index )
864 ((UChar*)base) + scale * index;
869 printName ( UChar* name, UChar* strtab )
871 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
872 UInt32 strtab_offset = * (UInt32*)(name+4);
873 fprintf ( stderr, "%s", strtab + strtab_offset );
876 for (i = 0; i < 8; i++) {
877 if (name[i] == 0) break;
878 fprintf ( stderr, "%c", name[i] );
885 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
887 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
888 UInt32 strtab_offset = * (UInt32*)(name+4);
889 strncpy ( dst, strtab+strtab_offset, dstSize );
895 if (name[i] == 0) break;
905 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
908 /* If the string is longer than 8 bytes, look in the
909 string table for it -- this will be correctly zero terminated.
911 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
912 UInt32 strtab_offset = * (UInt32*)(name+4);
913 return ((UChar*)strtab) + strtab_offset;
915 /* Otherwise, if shorter than 8 bytes, return the original,
916 which by defn is correctly terminated.
918 if (name[7]==0) return name;
919 /* The annoying case: 8 bytes. Copy into a temporary
920 (which is never freed ...)
922 newstr = stgMallocBytes(9, "cstring_from_COFF_symbol_name");
924 strncpy(newstr,name,8);
930 /* Just compares the short names (first 8 chars) */
931 static COFF_section *
932 findPEi386SectionCalled ( ObjectCode* oc, char* name )
936 = (COFF_header*)(oc->image);
939 ((UChar*)(oc->image))
940 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
942 for (i = 0; i < hdr->NumberOfSections; i++) {
945 COFF_section* section_i
947 myindex ( sizeof_COFF_section, sectab, i );
948 n1 = (UChar*) &(section_i->Name);
950 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
951 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
952 n1[6]==n2[6] && n1[7]==n2[7])
961 zapTrailingAtSign ( UChar* sym )
963 # define my_isdigit(c) ((c) >= '0' && (c) <= '9')
965 if (sym[0] == 0) return;
967 while (sym[i] != 0) i++;
970 while (j > 0 && my_isdigit(sym[j])) j--;
971 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
977 ocVerifyImage_PEi386 ( ObjectCode* oc )
981 COFF_section* sectab;
984 /* fprintf(stderr, "\nLOADING %s\n", oc->fileName); */
985 hdr = (COFF_header*)(oc->image);
986 sectab = (COFF_section*) (
987 ((UChar*)(oc->image))
988 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
990 symtab = (COFF_symbol*) (
991 ((UChar*)(oc->image))
992 + hdr->PointerToSymbolTable
994 strtab = ((UChar*)symtab)
995 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
997 if (hdr->Machine != 0x14c) {
998 belch("Not x86 PEi386");
1001 if (hdr->SizeOfOptionalHeader != 0) {
1002 belch("PEi386 with nonempty optional header");
1005 if ( /* (hdr->Characteristics & MYIMAGE_FILE_RELOCS_STRIPPED) || */
1006 (hdr->Characteristics & MYIMAGE_FILE_EXECUTABLE_IMAGE) ||
1007 (hdr->Characteristics & MYIMAGE_FILE_DLL) ||
1008 (hdr->Characteristics & MYIMAGE_FILE_SYSTEM) ) {
1009 belch("Not a PEi386 object file");
1012 if ( (hdr->Characteristics & MYIMAGE_FILE_BYTES_REVERSED_HI)
1013 /* || !(hdr->Characteristics & MYIMAGE_FILE_32BIT_MACHINE) */ ) {
1014 belch("Invalid PEi386 word size or endiannness: %d",
1015 (int)(hdr->Characteristics));
1018 /* fprintf(stderr, "strtab size %d\n", * (UInt32*)strtab); */
1019 if (* (UInt32*)strtab > 510000) {
1020 belch("PEi386 object has suspiciously large string table; > 64k relocs?");
1024 /* No further verification after this point; only debug printing. */
1026 IF_DEBUG(linker, i=1);
1027 if (i == 0) return 1;
1030 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
1032 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
1034 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
1036 fprintf ( stderr, "\n" );
1038 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
1040 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
1042 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
1044 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
1046 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
1048 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
1050 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
1052 /* Print the section table. */
1053 fprintf ( stderr, "\n" );
1054 for (i = 0; i < hdr->NumberOfSections; i++) {
1056 COFF_section* sectab_i
1058 myindex ( sizeof_COFF_section, sectab, i );
1065 printName ( sectab_i->Name, strtab );
1075 sectab_i->VirtualSize,
1076 sectab_i->VirtualAddress,
1077 sectab_i->SizeOfRawData,
1078 sectab_i->PointerToRawData,
1079 sectab_i->NumberOfRelocations,
1080 sectab_i->PointerToRelocations,
1081 sectab_i->PointerToRawData
1083 reltab = (COFF_reloc*) (
1084 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1087 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1089 COFF_reloc* rel = (COFF_reloc*)
1090 myindex ( sizeof_COFF_reloc, reltab, j );
1092 " type 0x%-4x vaddr 0x%-8x name `",
1094 rel->VirtualAddress );
1095 sym = (COFF_symbol*)
1096 myindex ( sizeof_COFF_symbol, symtab, rel->SymbolTableIndex );
1097 printName ( sym->Name, strtab -10 );
1098 fprintf ( stderr, "'\n" );
1101 fprintf ( stderr, "\n" );
1103 fprintf ( stderr, "\n" );
1104 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
1105 fprintf ( stderr, "---START of string table---\n");
1106 for (i = 4; i < *(Int32*)strtab; i++) {
1108 fprintf ( stderr, "\n"); else
1109 fprintf( stderr, "%c", strtab[i] );
1111 fprintf ( stderr, "--- END of string table---\n");
1113 fprintf ( stderr, "\n" );
1116 COFF_symbol* symtab_i;
1117 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1118 symtab_i = (COFF_symbol*)
1119 myindex ( sizeof_COFF_symbol, symtab, i );
1125 printName ( symtab_i->Name, strtab );
1134 (Int32)(symtab_i->SectionNumber),
1135 (UInt32)symtab_i->Type,
1136 (UInt32)symtab_i->StorageClass,
1137 (UInt32)symtab_i->NumberOfAuxSymbols
1139 i += symtab_i->NumberOfAuxSymbols;
1143 fprintf ( stderr, "\n" );
1149 ocGetNames_PEi386 ( ObjectCode* oc )
1152 COFF_section* sectab;
1153 COFF_symbol* symtab;
1160 hdr = (COFF_header*)(oc->image);
1161 sectab = (COFF_section*) (
1162 ((UChar*)(oc->image))
1163 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1165 symtab = (COFF_symbol*) (
1166 ((UChar*)(oc->image))
1167 + hdr->PointerToSymbolTable
1169 strtab = ((UChar*)(oc->image))
1170 + hdr->PointerToSymbolTable
1171 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1173 /* Allocate space for any (local, anonymous) .bss sections. */
1175 for (i = 0; i < hdr->NumberOfSections; i++) {
1177 COFF_section* sectab_i
1179 myindex ( sizeof_COFF_section, sectab, i );
1180 if (0 != strcmp(sectab_i->Name, ".bss")) continue;
1181 if (sectab_i->VirtualSize == 0) continue;
1182 /* This is a non-empty .bss section. Allocate zeroed space for
1183 it, and set its PointerToRawData field such that oc->image +
1184 PointerToRawData == addr_of_zeroed_space. */
1185 zspace = stgCallocBytes(1, sectab_i->VirtualSize,
1186 "ocGetNames_PEi386(anonymous bss)");
1187 sectab_i->PointerToRawData = ((UChar*)zspace) - ((UChar*)(oc->image));
1188 addProddableBlock(oc, zspace, sectab_i->VirtualSize);
1189 /* fprintf(stderr, "BSS section at 0x%x\n", zspace); */
1192 /* Copy exported symbols into the ObjectCode. */
1194 oc->n_symbols = hdr->NumberOfSymbols;
1195 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1196 "ocGetNames_PEi386(oc->symbols)");
1197 /* Call me paranoid; I don't care. */
1198 for (i = 0; i < oc->n_symbols; i++)
1199 oc->symbols[i] = NULL;
1203 COFF_symbol* symtab_i;
1204 if (i >= (Int32)(hdr->NumberOfSymbols)) break;
1205 symtab_i = (COFF_symbol*)
1206 myindex ( sizeof_COFF_symbol, symtab, i );
1210 if (symtab_i->StorageClass == MYIMAGE_SYM_CLASS_EXTERNAL
1211 && symtab_i->SectionNumber != MYIMAGE_SYM_UNDEFINED) {
1212 /* This symbol is global and defined, viz, exported */
1213 /* for MYIMAGE_SYMCLASS_EXTERNAL
1214 && !MYIMAGE_SYM_UNDEFINED,
1215 the address of the symbol is:
1216 address of relevant section + offset in section
1218 COFF_section* sectabent
1219 = (COFF_section*) myindex ( sizeof_COFF_section,
1221 symtab_i->SectionNumber-1 );
1222 addr = ((UChar*)(oc->image))
1223 + (sectabent->PointerToRawData
1227 if (symtab_i->SectionNumber == MYIMAGE_SYM_UNDEFINED
1228 && symtab_i->Value > 0) {
1229 /* This symbol isn't in any section at all, ie, global bss.
1230 Allocate zeroed space for it. */
1231 addr = stgCallocBytes(1, symtab_i->Value,
1232 "ocGetNames_PEi386(non-anonymous bss)");
1233 addProddableBlock(oc, addr, symtab_i->Value);
1237 sname = cstring_from_COFF_symbol_name ( symtab_i->Name, strtab );
1238 /* fprintf(stderr,"addSymbol %p `%s'\n", addr,sname); */
1239 IF_DEBUG(linker, belch("addSymbol %p `%s'\n", addr,sname);)
1240 ASSERT(i >= 0 && i < oc->n_symbols);
1241 /* cstring_from_COFF_symbol_name always succeeds. */
1242 oc->symbols[i] = sname;
1243 insertStrHashTable(symhash, sname, addr);
1247 "IGNORING symbol %d\n"
1251 printName ( symtab_i->Name, strtab );
1260 (Int32)(symtab_i->SectionNumber),
1261 (UInt32)symtab_i->Type,
1262 (UInt32)symtab_i->StorageClass,
1263 (UInt32)symtab_i->NumberOfAuxSymbols
1268 i += symtab_i->NumberOfAuxSymbols;
1272 /* Copy section information into the ObjectCode. */
1274 oc->n_sections = hdr->NumberOfSections;
1275 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1276 "ocGetNamesPEi386" );
1278 for (i = 0; i < oc->n_sections; i++) {
1283 = SECTIONKIND_OTHER;
1284 COFF_section* sectab_i
1286 myindex ( sizeof_COFF_section, sectab, i );
1287 IF_DEBUG(linker, belch("section name = %s\n", sectab_i->Name ));
1290 /* I'm sure this is the Right Way to do it. However, the
1291 alternative of testing the sectab_i->Name field seems to
1292 work ok with Cygwin.
1294 if (sectab_i->Characteristics & MYIMAGE_SCN_CNT_CODE ||
1295 sectab_i->Characteristics & MYIMAGE_SCN_CNT_INITIALIZED_DATA)
1296 kind = SECTIONKIND_CODE_OR_RODATA;
1299 if (0==strcmp(".text",sectab_i->Name) ||
1300 0==strcmp(".rodata",sectab_i->Name))
1301 kind = SECTIONKIND_CODE_OR_RODATA;
1302 if (0==strcmp(".data",sectab_i->Name) ||
1303 0==strcmp(".bss",sectab_i->Name))
1304 kind = SECTIONKIND_RWDATA;
1306 start = ((UChar*)(oc->image))
1307 + sectab_i->PointerToRawData;
1309 + sectab_i->SizeOfRawData - 1;
1311 if (kind == SECTIONKIND_OTHER) {
1312 belch("Unknown PEi386 section name `%s'", sectab_i->Name);
1317 oc->sections[i].start = start;
1318 oc->sections[i].end = end;
1319 oc->sections[i].kind = kind;
1320 addProddableBlock(oc, start, end - start + 1);
1329 ocResolve_PEi386 ( ObjectCode* oc )
1332 COFF_section* sectab;
1333 COFF_symbol* symtab;
1342 /* ToDo: should be variable-sized? But is at least safe in the
1343 sense of buffer-overrun-proof. */
1345 /* fprintf(stderr, "resolving for %s\n", oc->fileName); */
1347 hdr = (COFF_header*)(oc->image);
1348 sectab = (COFF_section*) (
1349 ((UChar*)(oc->image))
1350 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1352 symtab = (COFF_symbol*) (
1353 ((UChar*)(oc->image))
1354 + hdr->PointerToSymbolTable
1356 strtab = ((UChar*)(oc->image))
1357 + hdr->PointerToSymbolTable
1358 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1360 for (i = 0; i < hdr->NumberOfSections; i++) {
1361 COFF_section* sectab_i
1363 myindex ( sizeof_COFF_section, sectab, i );
1366 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1368 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1370 COFF_reloc* reltab_j
1372 myindex ( sizeof_COFF_reloc, reltab, j );
1374 /* the location to patch */
1376 ((UChar*)(oc->image))
1377 + (sectab_i->PointerToRawData
1378 + reltab_j->VirtualAddress
1379 - sectab_i->VirtualAddress )
1381 /* the existing contents of pP */
1383 /* the symbol to connect to */
1384 sym = (COFF_symbol*)
1385 myindex ( sizeof_COFF_symbol,
1386 symtab, reltab_j->SymbolTableIndex );
1389 "reloc sec %2d num %3d: type 0x%-4x "
1390 "vaddr 0x%-8x name `",
1392 (UInt32)reltab_j->Type,
1393 reltab_j->VirtualAddress );
1394 printName ( sym->Name, strtab );
1395 fprintf ( stderr, "'\n" ));
1397 if (sym->StorageClass == MYIMAGE_SYM_CLASS_STATIC) {
1398 COFF_section* section_sym
1399 = findPEi386SectionCalled ( oc, sym->Name );
1401 belch("%s: can't find section `%s'", oc->fileName, sym->Name);
1404 S = ((UInt32)(oc->image))
1405 + (section_sym->PointerToRawData
1408 copyName ( sym->Name, strtab, symbol, 1000-1 );
1409 zapTrailingAtSign ( symbol );
1410 (void*)S = lookupLocalSymbol( oc, symbol );
1411 if ((void*)S == NULL)
1412 (void*)S = lookupSymbol( symbol );
1414 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1418 checkProddableBlock(oc, pP);
1419 switch (reltab_j->Type) {
1420 case MYIMAGE_REL_I386_DIR32:
1423 case MYIMAGE_REL_I386_REL32:
1424 /* Tricky. We have to insert a displacement at
1425 pP which, when added to the PC for the _next_
1426 insn, gives the address of the target (S).
1427 Problem is to know the address of the next insn
1428 when we only know pP. We assume that this
1429 literal field is always the last in the insn,
1430 so that the address of the next insn is pP+4
1431 -- hence the constant 4.
1432 Also I don't know if A should be added, but so
1433 far it has always been zero.
1436 *pP = S - ((UInt32)pP) - 4;
1439 belch("%s: unhandled PEi386 relocation type %d",
1440 oc->fileName, reltab_j->Type);
1447 IF_DEBUG(linker, belch("completed %s", oc->fileName));
1451 #endif /* defined(OBJFORMAT_PEi386) */
1454 /* --------------------------------------------------------------------------
1456 * ------------------------------------------------------------------------*/
1458 #if defined(OBJFORMAT_ELF)
1463 #if defined(sparc_TARGET_ARCH)
1464 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1465 #elif defined(i386_TARGET_ARCH)
1466 # define ELF_TARGET_386 /* Used inside <elf.h> */
1468 /* There is a similar case for IA64 in the Solaris2 headers if this
1469 * ever becomes relevant.
1475 findElfSection ( void* objImage, Elf32_Word sh_type )
1478 char* ehdrC = (char*)objImage;
1479 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1480 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1482 for (i = 0; i < ehdr->e_shnum; i++) {
1483 if (shdr[i].sh_type == sh_type &&
1484 i != ehdr->e_shstrndx) {
1485 ptr = ehdrC + shdr[i].sh_offset;
1494 ocVerifyImage_ELF ( ObjectCode* oc )
1498 int i, j, nent, nstrtab, nsymtabs;
1502 char* ehdrC = (char*)(oc->image);
1503 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1505 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1506 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1507 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1508 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1509 belch("%s: not an ELF header", oc->fileName);
1512 IF_DEBUG(linker,belch( "Is an ELF header" ));
1514 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1515 belch("%s: not 32 bit ELF", oc->fileName);
1519 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1521 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1522 IF_DEBUG(linker,belch( "Is little-endian" ));
1524 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1525 IF_DEBUG(linker,belch( "Is big-endian" ));
1527 belch("%s: unknown endiannness", oc->fileName);
1531 if (ehdr->e_type != ET_REL) {
1532 belch("%s: not a relocatable object (.o) file", oc->fileName);
1535 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1537 IF_DEBUG(linker,belch( "Architecture is " ));
1538 switch (ehdr->e_machine) {
1539 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1540 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1541 default: IF_DEBUG(linker,belch( "unknown" ));
1542 belch("%s: unknown architecture", oc->fileName);
1546 IF_DEBUG(linker,belch(
1547 "\nSection header table: start %d, n_entries %d, ent_size %d",
1548 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1550 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1552 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1554 if (ehdr->e_shstrndx == SHN_UNDEF) {
1555 belch("%s: no section header string table", oc->fileName);
1558 IF_DEBUG(linker,belch( "Section header string table is section %d",
1560 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1563 for (i = 0; i < ehdr->e_shnum; i++) {
1564 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1565 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1566 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1567 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1568 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1569 ehdrC + shdr[i].sh_offset,
1570 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1572 if (shdr[i].sh_type == SHT_REL) {
1573 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1574 } else if (shdr[i].sh_type == SHT_RELA) {
1575 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1577 IF_DEBUG(linker,fprintf(stderr," "));
1580 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1584 IF_DEBUG(linker,belch( "\nString tables" ));
1587 for (i = 0; i < ehdr->e_shnum; i++) {
1588 if (shdr[i].sh_type == SHT_STRTAB &&
1589 i != ehdr->e_shstrndx) {
1590 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1591 strtab = ehdrC + shdr[i].sh_offset;
1596 belch("%s: no string tables, or too many", oc->fileName);
1601 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1602 for (i = 0; i < ehdr->e_shnum; i++) {
1603 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1604 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1606 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1607 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1608 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1610 shdr[i].sh_size % sizeof(Elf32_Sym)
1612 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1613 belch("%s: non-integral number of symbol table entries", oc->fileName);
1616 for (j = 0; j < nent; j++) {
1617 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1618 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1619 (int)stab[j].st_shndx,
1620 (int)stab[j].st_size,
1621 (char*)stab[j].st_value ));
1623 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1624 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1625 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1626 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1627 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1628 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1629 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1630 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1632 IF_DEBUG(linker,fprintf(stderr, " " ));
1634 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1635 switch (ELF32_ST_BIND(stab[j].st_info)) {
1636 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1637 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1638 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1639 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1641 IF_DEBUG(linker,fprintf(stderr, " " ));
1643 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1647 if (nsymtabs == 0) {
1648 belch("%s: didn't find any symbol tables", oc->fileName);
1657 ocGetNames_ELF ( ObjectCode* oc )
1662 char* ehdrC = (char*)(oc->image);
1663 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1664 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1665 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1666 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1668 ASSERT(symhash != NULL);
1671 belch("%s: no strtab", oc->fileName);
1676 oc->n_sections = ehdr->e_shnum;
1677 oc->sections = stgMallocBytes( oc->n_sections * sizeof(Section),
1678 "ocGetNames_ELF(oc->sections)" );
1680 for (i = 0; i < oc->n_sections; i++) {
1682 /* make a section entry for relevant sections */
1683 SectionKind kind = SECTIONKIND_OTHER;
1684 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1685 !strcmp(".data1",sh_strtab+shdr[i].sh_name) ||
1686 !strcmp(".bss",sh_strtab+shdr[i].sh_name))
1687 kind = SECTIONKIND_RWDATA;
1688 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1689 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1690 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1691 kind = SECTIONKIND_CODE_OR_RODATA;
1693 if (!strcmp(".bss",sh_strtab+shdr[i].sh_name) && shdr[i].sh_size > 0) {
1694 /* This is a non-empty .bss section. Allocate zeroed space for
1695 it, and set its .sh_offset field such that
1696 ehdrC + .sh_offset == addr_of_zeroed_space. */
1697 char* zspace = stgCallocBytes(1, shdr[i].sh_size,
1698 "ocGetNames_ELF(anonymous bss)");
1699 shdr[i].sh_offset = ((char*)zspace) - ((char*)ehdrC);
1700 /* We don't prod BSS sections, hence the following isn't
1702 addProddableBlock(oc, zspace, shdr[i].sh_size);
1705 fprintf(stderr, "BSS section at 0x%x, size %d\n",
1706 zspace, shdr[i].sh_size);
1710 /* fill in the section info */
1711 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1712 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1713 oc->sections[i].kind = kind;
1714 if (kind != SECTIONKIND_OTHER && shdr[i].sh_size > 0)
1715 addProddableBlock(oc, ehdrC + shdr[i].sh_offset, shdr[i].sh_size);
1717 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1719 /* copy stuff into this module's object symbol table */
1720 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1721 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1723 oc->n_symbols = nent;
1724 oc->symbols = stgMallocBytes(oc->n_symbols * sizeof(char*),
1725 "ocGetNames_ELF(oc->symbols)");
1727 for (j = 0; j < nent; j++) {
1728 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1729 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1731 /* and not an undefined symbol */
1732 && stab[j].st_shndx != SHN_UNDEF
1733 /* and not in a "special section" */
1734 && stab[j].st_shndx < SHN_LORESERVE
1736 /* and it's a not a section or string table or anything silly */
1737 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1738 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1739 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1744 int secno = stab[j].st_shndx;
1745 /* Section 0 is the undefined section, hence > and not >=. */
1746 ASSERT(secno > 0 && secno < ehdr->e_shnum);
1747 nm = strtab + stab[j].st_name;
1749 if (shdr[secno].sh_type == SHT_NOBITS) {
1750 fprintf(stderr, "bss symbol, size %d off %d name %s\n",
1751 stab[j].st_size, stab[j].st_value, nm);
1754 ad = ehdrC + shdr[ secno ].sh_offset + stab[j].st_value;
1757 oc->symbols[j] = nm;
1758 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1759 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1760 ad, oc->fileName, nm ));
1761 insertStrHashTable(oc->lochash, nm, ad);
1763 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1764 ad, oc->fileName, nm ));
1765 insertStrHashTable(symhash, nm, ad);
1769 IF_DEBUG(linker,belch( "skipping `%s'",
1770 strtab + stab[j].st_name ));
1773 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1774 (int)ELF32_ST_BIND(stab[j].st_info),
1775 (int)ELF32_ST_TYPE(stab[j].st_info),
1776 (int)stab[j].st_shndx,
1777 strtab + stab[j].st_name
1780 oc->symbols[j] = NULL;
1789 /* Do ELF relocations which lack an explicit addend. All x86-linux
1790 relocations appear to be of this form. */
1792 do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1793 Elf32_Shdr* shdr, int shnum,
1794 Elf32_Sym* stab, char* strtab )
1799 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1800 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1801 int target_shndx = shdr[shnum].sh_info;
1802 int symtab_shndx = shdr[shnum].sh_link;
1803 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1804 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1805 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1806 target_shndx, symtab_shndx ));
1807 for (j = 0; j < nent; j++) {
1808 Elf32_Addr offset = rtab[j].r_offset;
1809 Elf32_Word info = rtab[j].r_info;
1811 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1812 Elf32_Word* pP = (Elf32_Word*)P;
1816 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1817 j, (void*)offset, (void*)info ));
1819 IF_DEBUG(linker,belch( " ZERO" ));
1822 /* First see if it is a nameless local symbol. */
1823 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1824 symbol = "(noname)";
1826 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1827 + stab[ELF32_R_SYM(info)].st_value);
1829 /* No? Should be in a symbol table then; first try the
1831 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1832 (void*)S = lookupLocalSymbol( oc, symbol );
1833 if ((void*)S == NULL)
1834 (void*)S = lookupSymbol( symbol );
1837 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1840 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1842 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1843 (void*)P, (void*)S, (void*)A ));
1844 checkProddableBlock ( oc, pP );
1845 switch (ELF32_R_TYPE(info)) {
1846 # ifdef i386_TARGET_ARCH
1847 case R_386_32: *pP = S + A; break;
1848 case R_386_PC32: *pP = S + A - P; break;
1851 belch("%s: unhandled ELF relocation(Rel) type %d\n",
1852 oc->fileName, ELF32_R_TYPE(info));
1861 /* Do ELF relocations for which explicit addends are supplied.
1862 sparc-solaris relocations appear to be of this form. */
1864 do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1865 Elf32_Shdr* shdr, int shnum,
1866 Elf32_Sym* stab, char* strtab )
1871 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1872 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1873 int target_shndx = shdr[shnum].sh_info;
1874 int symtab_shndx = shdr[shnum].sh_link;
1875 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1876 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1877 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1878 target_shndx, symtab_shndx ));
1879 for (j = 0; j < nent; j++) {
1880 Elf32_Addr offset = rtab[j].r_offset;
1881 Elf32_Word info = rtab[j].r_info;
1882 Elf32_Sword addend = rtab[j].r_addend;
1884 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1885 Elf32_Addr A = addend;
1887 # if defined(sparc_TARGET_ARCH)
1888 /* This #ifdef only serves to avoid unused-var warnings. */
1889 Elf32_Word* pP = (Elf32_Word*)P;
1893 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1894 j, (void*)offset, (void*)info,
1897 IF_DEBUG(linker,belch( " ZERO" ));
1900 /* First see if it is a nameless local symbol. */
1901 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1902 symbol = "(noname)";
1904 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1905 + stab[ELF32_R_SYM(info)].st_value);
1907 /* No? Should be in a symbol table then; first try the
1909 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1910 (void*)S = lookupLocalSymbol( oc, symbol );
1911 if ((void*)S == NULL)
1912 (void*)S = lookupSymbol( symbol );
1915 belch("%s: unknown symbol `%s'", oc->fileName, symbol);
1919 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1922 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1924 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1925 (void*)P, (void*)S, (void*)A ));
1926 checkProddableBlock ( oc, pP );
1927 switch (ELF32_R_TYPE(info)) {
1928 # if defined(sparc_TARGET_ARCH)
1929 case R_SPARC_WDISP30:
1930 w1 = *pP & 0xC0000000;
1931 w2 = (Elf32_Word)((S + A - P) >> 2);
1932 ASSERT((w2 & 0xC0000000) == 0);
1937 w1 = *pP & 0xFFC00000;
1938 w2 = (Elf32_Word)((S + A) >> 10);
1939 ASSERT((w2 & 0xFFC00000) == 0);
1945 w2 = (Elf32_Word)((S + A) & 0x3FF);
1946 ASSERT((w2 & ~0x3FF) == 0);
1950 /* According to the Sun documentation:
1952 This relocation type resembles R_SPARC_32, except it refers to an
1953 unaligned word. That is, the word to be relocated must be treated
1954 as four separate bytes with arbitrary alignment, not as a word
1955 aligned according to the architecture requirements.
1957 (JRS: which means that freeloading on the R_SPARC_32 case
1958 is probably wrong, but hey ...)
1962 w2 = (Elf32_Word)(S + A);
1967 belch("%s: unhandled ELF relocation(RelA) type %d\n",
1968 oc->fileName, ELF32_R_TYPE(info));
1978 ocResolve_ELF ( ObjectCode* oc )
1982 Elf32_Sym* stab = NULL;
1983 char* ehdrC = (char*)(oc->image);
1984 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1985 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1987 /* first find "the" symbol table */
1988 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1990 /* also go find the string table */
1991 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1993 if (stab == NULL || strtab == NULL) {
1994 belch("%s: can't find string or symbol table", oc->fileName);
1998 /* Process the relocation sections. */
1999 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
2000 if (shdr[shnum].sh_type == SHT_REL ) {
2001 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
2002 shnum, stab, strtab );
2006 if (shdr[shnum].sh_type == SHT_RELA) {
2007 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
2008 shnum, stab, strtab );
2013 /* Free the local symbol table; we won't need it again. */
2014 freeHashTable(oc->lochash, NULL);