1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.26 2001/02/12 16:40:34 sewardj Exp $
4 * (c) The GHC Team, 2000
8 * ---------------------------------------------------------------------------*/
15 #include "LinkerInternals.h"
17 #include "StoragePriv.h"
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
23 #ifdef HAVE_SYS_STAT_H
31 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
33 #elif defined(cygwin32_TARGET_OS) || defined (mingw32_TARGET_OS)
34 #define OBJFORMAT_PEi386
37 /* Hash table mapping symbol names to Symbol */
38 /*Str*/HashTable *symhash;
40 #if defined(OBJFORMAT_ELF)
41 static int ocVerifyImage_ELF ( ObjectCode* oc );
42 static int ocGetNames_ELF ( ObjectCode* oc );
43 static int ocResolve_ELF ( ObjectCode* oc );
44 #elif defined(OBJFORMAT_PEi386)
45 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
46 static int ocGetNames_PEi386 ( ObjectCode* oc );
47 static int ocResolve_PEi386 ( ObjectCode* oc );
50 /* -----------------------------------------------------------------------------
51 * Built-in symbols from the RTS
68 SymX(stg_update_PAP) \
69 SymX(stg_ap_2_upd_info) \
70 SymX(stg_ap_3_upd_info) \
71 SymX(stg_ap_4_upd_info) \
72 SymX(stg_ap_5_upd_info) \
73 SymX(stg_ap_6_upd_info) \
74 SymX(stg_ap_7_upd_info) \
75 SymX(stg_ap_8_upd_info) \
76 SymX(stg_sel_0_upd_info) \
77 SymX(stg_sel_1_upd_info) \
78 SymX(stg_sel_2_upd_info) \
79 SymX(stg_sel_3_upd_info) \
80 SymX(stg_sel_4_upd_info) \
81 SymX(stg_sel_5_upd_info) \
82 SymX(stg_sel_6_upd_info) \
83 SymX(stg_sel_7_upd_info) \
84 SymX(stg_sel_8_upd_info) \
85 SymX(stg_sel_9_upd_info) \
86 SymX(stg_sel_10_upd_info) \
87 SymX(stg_sel_11_upd_info) \
88 SymX(stg_sel_12_upd_info) \
89 SymX(stg_sel_13_upd_info) \
90 SymX(stg_sel_14_upd_info) \
91 SymX(stg_sel_15_upd_info) \
92 SymX(stg_upd_frame_info) \
93 SymX(stg_seq_frame_info) \
94 SymX(stg_CAF_BLACKHOLE_info) \
95 SymX(stg_IND_STATIC_info) \
96 SymX(stg_EMPTY_MVAR_info) \
97 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
99 SymX(stg_CHARLIKE_closure) \
100 SymX(stg_INTLIKE_closure) \
102 SymX(newBCOzh_fast) \
103 SymX(mkApUpd0zh_fast) \
104 SymX(putMVarzh_fast) \
105 SymX(newMVarzh_fast) \
106 SymX(takeMVarzh_fast) \
107 SymX(tryTakeMVarzh_fast) \
113 SymX(killThreadzh_fast) \
114 SymX(waitReadzh_fast) \
115 SymX(waitWritezh_fast) \
116 SymX(suspendThread) \
118 SymX(stackOverflow) \
119 SymX(int2Integerzh_fast) \
120 SymX(word2Integerzh_fast) \
121 SymX(mkForeignObjzh_fast) \
122 SymX(__encodeDouble) \
123 SymX(decodeDoublezh_fast) \
124 SymX(decodeFloatzh_fast) \
125 SymX(gcdIntegerzh_fast) \
126 SymX(newArrayzh_fast) \
127 SymX(unsafeThawArrayzh_fast) \
128 SymX(newByteArrayzh_fast) \
129 SymX(newMutVarzh_fast) \
130 SymX(quotRemIntegerzh_fast) \
131 SymX(quotIntegerzh_fast) \
132 SymX(remIntegerzh_fast) \
133 SymX(divExactIntegerzh_fast) \
134 SymX(divModIntegerzh_fast) \
135 SymX(timesIntegerzh_fast) \
136 SymX(minusIntegerzh_fast) \
137 SymX(plusIntegerzh_fast) \
138 SymX(andIntegerzh_fast) \
139 SymX(orIntegerzh_fast) \
140 SymX(xorIntegerzh_fast) \
141 SymX(complementIntegerzh_fast) \
142 SymX(mkWeakzh_fast) \
143 SymX(makeStableNamezh_fast) \
144 SymX(finalizzeWeakzh_fast) \
145 SymX(blockAsyncExceptionszh_fast) \
146 SymX(unblockAsyncExceptionszh_fast) \
148 SymX(isDoubleInfinite) \
149 SymX(isDoubleDenormalized) \
150 SymX(isDoubleNegativeZero) \
151 SymX(__encodeFloat) \
153 SymX(isFloatInfinite) \
154 SymX(isFloatDenormalized) \
155 SymX(isFloatNegativeZero) \
156 SymX(__int_encodeFloat) \
157 SymX(__int_encodeDouble) \
158 SymX(__gmpz_cmp_si) \
159 SymX(__gmpz_cmp_ui) \
164 SymX(resetNonBlockingFd) \
166 SymX(stable_ptr_table) \
167 SymX(shutdownHaskellAndExit) \
168 Sym(stg_enterStackTop) \
169 Sym(stg_yield_to_interpreter) \
173 Sym(__init_PrelGHC) \
174 SymX(freeHaskellFunctionPtr) \
177 SymX(NoRunnableThreadsHook) \
178 SymX(StackOverflowHook) \
179 SymX(OutOfHeapHook) \
180 SymX(MallocFailHook) \
181 SymX(PatErrorHdrHook) \
184 SymX(PostTraceHook) \
185 SymX(stg_sig_install) \
187 SymX(createAdjustor) \
189 SymX(rts_mkStablePtr) \
192 SymX(rts_checkSchedStatus) \
195 #ifndef SUPPORT_LONG_LONGS
196 #define RTS_LONG_LONG_SYMS /* nothing */
198 #define RTS_LONG_LONG_SYMS \
211 SymX(stg_remWord64) \
212 SymX(stg_quotWord64) \
214 SymX(stg_quotInt64) \
215 SymX(stg_negateInt64) \
216 SymX(stg_plusInt64) \
217 SymX(stg_minusInt64) \
218 SymX(stg_timesInt64) \
224 SymX(stg_shiftRL64) \
225 SymX(stg_iShiftL64) \
226 SymX(stg_iShiftRL64) \
227 SymX(stg_iShiftRA64) \
228 SymX(stg_intToInt64) \
229 SymX(stg_int64ToInt) \
230 SymX(stg_int64ToWord64) \
231 SymX(stg_wordToWord64) \
232 SymX(stg_word64ToWord) \
233 SymX(stg_word64ToInt64) \
234 SymX(int64ToIntegerzh_fast) \
235 SymX(word64ToIntegerzh_fast)
236 #endif /* SUPPORT_LONG_LONGS */
238 /* entirely bogus claims about types of these symbols */
239 #define Sym(vvv) extern void (vvv);
240 #define SymX(vvv) /**/
245 #ifdef LEADING_UNDERSCORE
246 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
248 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
251 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
253 #define SymX(vvv) Sym(vvv)
255 static SymbolVal rtsSyms[] = {
258 { 0, 0 } /* sentinel */
261 /* -----------------------------------------------------------------------------
262 * initialize the object linker
264 static void *dl_prog_handle;
271 symhash = allocStrHashTable();
273 /* populate the symbol table with stuff from the RTS */
274 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
275 insertStrHashTable(symhash, sym->lbl, sym);
278 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
281 /* -----------------------------------------------------------------------------
282 * lookup a symbol in the hash table
285 lookupSymbol( char *lbl )
288 ASSERT(symhash != NULL);
289 val = lookupStrHashTable(symhash, lbl);
292 return dlsym(dl_prog_handle, lbl);
300 lookupLocalSymbol( ObjectCode* oc, char *lbl )
303 val = lookupStrHashTable(oc->lochash, lbl);
313 /* -----------------------------------------------------------------------------
314 * Load an obj (populate the global symbol table, but don't resolve yet)
316 * Returns: 1 if ok, 0 on error.
319 loadObj( char *path )
327 /* assert that we haven't already loaded this object */
330 for (o = objects; o; o = o->next)
331 ASSERT(strcmp(o->fileName, path));
335 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
337 # if defined(OBJFORMAT_ELF)
338 oc->formatName = "ELF";
339 # elif defined(OBJFORMAT_PEi386)
340 oc->formatName = "PEi386";
343 barf("loadObj: not implemented on this platform");
347 if (r == -1) { return 0; }
349 /* sigh, strdup() isn't a POSIX function, so do it the long way */
350 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
351 strcpy(oc->fileName, path);
353 oc->fileSize = st.st_size;
354 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
357 oc->lochash = allocStrHashTable();
359 /* chain it onto the list of objects */
363 /* load the image into memory */
364 f = fopen(path, "rb");
366 barf("loadObj: can't read `%s'", path);
368 n = fread ( oc->image, 1, oc->fileSize, f );
369 if (n != oc->fileSize) {
371 barf("loadObj: error whilst reading `%s'", path);
374 /* verify the in-memory image */
375 # if defined(OBJFORMAT_ELF)
376 r = ocVerifyImage_ELF ( oc );
377 # elif defined(OBJFORMAT_PEi386)
378 r = ocVerifyImage_PEi386 ( oc );
380 barf("loadObj: no verify method");
382 if (!r) { return r; }
384 /* build the symbol list for this image */
385 # if defined(OBJFORMAT_ELF)
386 r = ocGetNames_ELF ( oc );
387 # elif defined(OBJFORMAT_PEi386)
388 r = ocGetNames_PEi386 ( oc );
390 barf("loadObj: no getNames method");
392 if (!r) { return r; }
394 /* loaded, but not resolved yet */
395 oc->status = OBJECT_LOADED;
400 /* -----------------------------------------------------------------------------
401 * resolve all the currently unlinked objects in memory
403 * Returns: 1 if ok, 0 on error.
411 for (oc = objects; oc; oc = oc->next) {
412 if (oc->status != OBJECT_RESOLVED) {
413 # if defined(OBJFORMAT_ELF)
414 r = ocResolve_ELF ( oc );
415 # elif defined(OBJFORMAT_PEi386)
416 r = ocResolve_PEi386 ( oc );
418 barf("link: not implemented on this platform");
420 if (!r) { return r; }
421 oc->status = OBJECT_RESOLVED;
427 /* -----------------------------------------------------------------------------
428 * delete an object from the pool
431 unloadObj( char *path )
433 ObjectCode *oc, *prev;
435 ASSERT(symhash != NULL);
436 ASSERT(objects != NULL);
439 for (oc = objects; oc; prev = oc, oc = oc->next) {
440 if (!strcmp(oc->fileName,path)) {
442 /* Remove all the mappings for the symbols within this
447 for (s = oc->symbols; s < oc->symbols + oc->n_symbols; s++) {
448 if (s->lbl != NULL) {
449 removeStrHashTable(symhash, s->lbl, NULL);
457 prev->next = oc->next;
460 /* We're going to leave this in place, in case there are
461 any pointers from the heap into it: */
462 /* free(oc->image); */
466 /* The local hash table should have been freed at the end
467 of the ocResolve_ call on it. */
468 ASSERT(oc->lochash == NULL);
474 belch("unloadObj: can't find `%s' to unload", path);
478 /* --------------------------------------------------------------------------
479 * PEi386 specifics (Win32 targets)
480 * ------------------------------------------------------------------------*/
482 /* The information for this linker comes from
483 Microsoft Portable Executable
484 and Common Object File Format Specification
485 revision 5.1 January 1998
486 which SimonM says comes from the MS Developer Network CDs.
490 #if defined(OBJFORMAT_PEi386)
494 typedef unsigned char UChar;
495 typedef unsigned short UInt16;
496 typedef unsigned int UInt32;
503 UInt16 NumberOfSections;
504 UInt32 TimeDateStamp;
505 UInt32 PointerToSymbolTable;
506 UInt32 NumberOfSymbols;
507 UInt16 SizeOfOptionalHeader;
508 UInt16 Characteristics;
512 #define sizeof_COFF_header 20
519 UInt32 VirtualAddress;
520 UInt32 SizeOfRawData;
521 UInt32 PointerToRawData;
522 UInt32 PointerToRelocations;
523 UInt32 PointerToLinenumbers;
524 UInt16 NumberOfRelocations;
525 UInt16 NumberOfLineNumbers;
526 UInt32 Characteristics;
530 #define sizeof_COFF_section 40
537 UInt16 SectionNumber;
540 UChar NumberOfAuxSymbols;
544 #define sizeof_COFF_symbol 18
549 UInt32 VirtualAddress;
550 UInt32 SymbolTableIndex;
555 #define sizeof_COFF_reloc 10
558 /* From PE spec doc, section 3.3.2 */
559 #define IMAGE_FILE_RELOCS_STRIPPED 0x0001
560 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
561 #define IMAGE_FILE_DLL 0x2000
562 #define IMAGE_FILE_SYSTEM 0x1000
563 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
564 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
565 #define IMAGE_FILE_32BIT_MACHINE 0x0100
567 /* From PE spec doc, section 5.4.2 and 5.4.4 */
568 #define IMAGE_SYM_CLASS_EXTERNAL 2
569 #define IMAGE_SYM_CLASS_STATIC 3
570 #define IMAGE_SYM_UNDEFINED 0
572 /* From PE spec doc, section 4.1 */
573 #define IMAGE_SCN_CNT_CODE 0x00000020
574 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
576 /* From PE spec doc, section 5.2.1 */
577 #define IMAGE_REL_I386_DIR32 0x0006
578 #define IMAGE_REL_I386_REL32 0x0014
581 /* We use myindex to calculate array addresses, rather than
582 simply doing the normal subscript thing. That's because
583 some of the above structs have sizes which are not
584 a whole number of words. GCC rounds their sizes up to a
585 whole number of words, which means that the address calcs
586 arising from using normal C indexing or pointer arithmetic
587 are just plain wrong. Sigh.
590 myindex ( int scale, int index, void* base )
593 ((UChar*)base) + scale * index;
598 printName ( UChar* name, UChar* strtab )
600 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
601 UInt32 strtab_offset = * (UInt32*)(name+4);
602 fprintf ( stderr, "%s", strtab + strtab_offset );
605 for (i = 0; i < 8; i++) {
606 if (name[i] == 0) break;
607 fprintf ( stderr, "%c", name[i] );
614 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
616 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
617 UInt32 strtab_offset = * (UInt32*)(name+4);
618 strncpy ( dst, strtab+strtab_offset, dstSize );
624 if (name[i] == 0) break;
634 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
637 /* If the string is longer than 8 bytes, look in the
638 string table for it -- this will be correctly zero terminated.
640 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
641 UInt32 strtab_offset = * (UInt32*)(name+4);
642 return ((UChar*)strtab) + strtab_offset;
644 /* Otherwise, if shorter than 8 bytes, return the original,
645 which by defn is correctly terminated.
647 if (name[7]==0) return name;
648 /* The annoying case: 8 bytes. Copy into a temporary
649 (which is never freed ...)
653 strncpy(newstr,name,8);
660 /* Just compares the short names (first 8 chars) */
661 static COFF_section *
662 findPEi386SectionCalled ( ObjectCode* oc, char* name )
666 = (COFF_header*)(oc->image);
669 ((UChar*)(oc->image))
670 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
672 for (i = 0; i < hdr->NumberOfSections; i++) {
675 COFF_section* section_i
677 myindex ( sizeof_COFF_section, i, sectab );
678 n1 = (UChar*) &(section_i->Name);
680 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
681 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
682 n1[6]==n2[6] && n1[7]==n2[7])
691 zapTrailingAtSign ( UChar* sym )
694 if (sym[0] == 0) return;
696 while (sym[i] != 0) i++;
699 while (j > 0 && isdigit(sym[j])) j--;
700 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
705 ocVerifyImage_PEi386 ( ObjectCode* oc )
709 COFF_section* sectab;
713 hdr = (COFF_header*)(oc->image);
714 sectab = (COFF_section*) (
715 ((UChar*)(oc->image))
716 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
718 symtab = (COFF_symbol*) (
719 ((UChar*)(oc->image))
720 + hdr->PointerToSymbolTable
722 strtab = ((UChar*)(oc->image))
723 + hdr->PointerToSymbolTable
724 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
726 if (hdr->Machine != 0x14c) {
727 oc->errMsg("Not x86 PEi386");
730 if (hdr->SizeOfOptionalHeader != 0) {
731 oc->errMsg("PEi386 with nonempty optional header");
734 if ( /* (hdr->Characteristics & IMAGE_FILE_RELOCS_STRIPPED) || */
735 (hdr->Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ||
736 (hdr->Characteristics & IMAGE_FILE_DLL) ||
737 (hdr->Characteristics & IMAGE_FILE_SYSTEM) ) {
738 oc->errMsg("Not a PEi386 object file");
741 if ( (hdr->Characteristics & IMAGE_FILE_BYTES_REVERSED_HI) ||
742 !(hdr->Characteristics & IMAGE_FILE_32BIT_MACHINE) ) {
743 oc->errMsg("Invalid PEi386 word size or endiannness");
747 if (!verb) return TRUE;
748 /* No further verification after this point; only debug printing. */
751 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
753 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
755 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
757 fprintf ( stderr, "\n" );
759 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
761 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
763 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
765 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
767 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
769 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
771 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
773 fprintf ( stderr, "\n" );
774 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
775 fprintf ( stderr, "---START of string table---\n");
776 for (i = 4; i < *(UInt32*)strtab; i++) {
778 fprintf ( stderr, "\n"); else
779 fprintf( stderr, "%c", strtab[i] );
781 fprintf ( stderr, "--- END of string table---\n");
783 fprintf ( stderr, "\n" );
784 for (i = 0; i < hdr->NumberOfSections; i++) {
786 COFF_section* sectab_i
788 myindex ( sizeof_COFF_section, i, sectab );
795 printName ( sectab_i->Name, strtab );
804 sectab_i->VirtualSize,
805 sectab_i->VirtualAddress,
806 sectab_i->SizeOfRawData,
807 sectab_i->PointerToRawData,
808 sectab_i->NumberOfRelocations,
809 sectab_i->PointerToRelocations
811 reltab = (COFF_reloc*) (
812 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
814 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
816 COFF_reloc* rel = (COFF_reloc*)
817 myindex ( sizeof_COFF_reloc, j, reltab );
819 " type 0x%-4x vaddr 0x%-8x name `",
821 rel->VirtualAddress );
823 myindex ( sizeof_COFF_symbol, rel->SymbolTableIndex, symtab );
824 printName ( sym->Name, strtab );
825 fprintf ( stderr, "'\n" );
827 fprintf ( stderr, "\n" );
831 fprintf ( stderr, "\n" );
834 COFF_symbol* symtab_i;
835 if (i >= hdr->NumberOfSymbols) break;
836 symtab_i = (COFF_symbol*)
837 myindex ( sizeof_COFF_symbol, i, symtab );
843 printName ( symtab_i->Name, strtab );
852 (Int32)(symtab_i->SectionNumber) - 1,
853 (UInt32)symtab_i->Type,
854 (UInt32)symtab_i->StorageClass,
855 (UInt32)symtab_i->NumberOfAuxSymbols
857 i += symtab_i->NumberOfAuxSymbols;
861 fprintf ( stderr, "\n" );
868 ocGetNames_PEi386 ( ObjectCode* oc )
871 COFF_section* sectab;
879 hdr = (COFF_header*)(oc->image);
880 sectab = (COFF_section*) (
881 ((UChar*)(oc->image))
882 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
884 symtab = (COFF_symbol*) (
885 ((UChar*)(oc->image))
886 + hdr->PointerToSymbolTable
888 strtab = ((UChar*)(oc->image))
889 + hdr->PointerToSymbolTable
890 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
892 /* Copy exported symbols into the ObjectCode. */
895 COFF_symbol* symtab_i;
896 if (i >= hdr->NumberOfSymbols) break;
897 symtab_i = (COFF_symbol*)
898 myindex ( sizeof_COFF_symbol, i, symtab );
900 if (symtab_i->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
901 symtab_i->SectionNumber != IMAGE_SYM_UNDEFINED) {
903 /* This symbol is global and defined, viz, exported */
904 COFF_section* sectabent;
906 sname = cstring_from_COFF_symbol_name (
907 symtab_i->Name, strtab
910 oc->errMsg("Out of memory when copying PEi386 symbol");
914 /* for IMAGE_SYMCLASS_EXTERNAL
915 && !IMAGE_SYM_UNDEFINED,
916 the address of the symbol is:
917 address of relevant section + offset in section
919 sectabent = (COFF_section*)
920 myindex ( sizeof_COFF_section,
921 symtab_i->SectionNumber-1,
923 addr = ((UChar*)(oc->image))
924 + (sectabent->PointerToRawData
926 /* fprintf ( stderr, "addSymbol %p `%s'\n", addr,sname); */
927 if (!addSymbol(oc,sname,addr)) return FALSE;
929 i += symtab_i->NumberOfAuxSymbols;
933 oc->sections = stgMallocBytes( NumberOfSections * sizeof(Section),
934 "ocGetNamesPEi386" );
936 /* Copy section information into the ObjectCode. */
937 for (i = 0; i < hdr->NumberOfSections; i++) {
943 COFF_section* sectab_i
945 myindex ( sizeof_COFF_section, i, sectab );
946 /* fprintf ( stderr, "section name = %s\n", sectab_i->Name ); */
949 /* I'm sure this is the Right Way to do it. However, the
950 alternative of testing the sectab_i->Name field seems to
953 if (sectab_i->Characteristics & IMAGE_SCN_CNT_CODE ||
954 sectab_i->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
955 kind = SECTIONKIND_CODE_OR_RODATA;
958 if (0==strcmp(".text",sectab_i->Name))
959 kind = SECTIONKIND_CODE_OR_RODATA;
960 if (0==strcmp(".data",sectab_i->Name) ||
961 0==strcmp(".bss",sectab_i->Name))
962 kind = SECTIONKIND_RWDATA;
964 start = ((UChar*)(oc->image))
965 + sectab_i->PointerToRawData;
967 + sectab_i->SizeOfRawData - 1;
969 if (kind != SECTIONKIND_OTHER) {
970 addSection ( oc, start, end, kind );
972 fprintf ( stderr, "unknown section name = `%s'\n",
974 oc->errMsg("Unknown PEi386 section name");
984 ocResolve_PEi386 ( ObjectCode* oc, int verb )
987 COFF_section* sectab;
996 char symbol[1000]; // ToDo
998 hdr = (COFF_header*)(oc->image);
999 sectab = (COFF_section*) (
1000 ((UChar*)(oc->image))
1001 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
1003 symtab = (COFF_symbol*) (
1004 ((UChar*)(oc->image))
1005 + hdr->PointerToSymbolTable
1007 strtab = ((UChar*)(oc->image))
1008 + hdr->PointerToSymbolTable
1009 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1011 for (i = 0; i < hdr->NumberOfSections; i++) {
1012 COFF_section* sectab_i
1014 myindex ( sizeof_COFF_section, i, sectab );
1017 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1019 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1021 COFF_reloc* reltab_j
1023 myindex ( sizeof_COFF_reloc, j, reltab );
1025 /* the location to patch */
1027 ((UChar*)(oc->image))
1028 + (sectab_i->PointerToRawData
1029 + reltab_j->VirtualAddress)
1031 /* the existing contents of pP */
1033 /* the symbol to connect to */
1034 sym = (COFF_symbol*)
1035 myindex ( sizeof_COFF_symbol,
1036 reltab_j->SymbolTableIndex, symtab );
1039 "reloc sec %2d num %3d: type 0x%-4x "
1040 "vaddr 0x%-8x name `",
1042 (UInt32)reltab_j->Type,
1043 reltab_j->VirtualAddress );
1044 printName ( sym->Name, strtab );
1045 fprintf ( stderr, "'\n" );
1048 if (sym->StorageClass == IMAGE_SYM_CLASS_STATIC) {
1049 COFF_section* section_sym
1050 = findPEi386SectionCalled ( oc, sym->Name );
1052 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
1053 oc->errMsg("Can't find abovementioned PEi386 section");
1056 S = ((UInt32)(oc->image))
1057 + (section_sym->PointerToRawData
1060 copyName ( sym->Name, strtab, symbol, 1000 );
1061 zapTrailingAtSign ( symbol );
1062 S = (UInt32) ocLookupSym ( oc, symbol );
1064 S = (UInt32)(oc->clientLookup ( symbol ));
1066 belch("%s: unresolvable reference to `%s'", oc->fileName, symbol);
1071 switch (reltab_j->Type) {
1072 case IMAGE_REL_I386_DIR32:
1075 case IMAGE_REL_I386_REL32:
1076 /* Tricky. We have to insert a displacement at
1077 pP which, when added to the PC for the _next_
1078 insn, gives the address of the target (S).
1079 Problem is to know the address of the next insn
1080 when we only know pP. We assume that this
1081 literal field is always the last in the insn,
1082 so that the address of the next insn is pP+4
1083 -- hence the constant 4.
1084 Also I don't know if A should be added, but so
1085 far it has always been zero.
1088 *pP = S - ((UInt32)pP) - 4;
1092 "unhandled PEi386 relocation type %d\n",
1094 oc->errMsg("unhandled PEi386 relocation type");
1104 #endif /* defined(OBJFORMAT_PEi386) */
1107 /* --------------------------------------------------------------------------
1109 * ------------------------------------------------------------------------*/
1111 #if defined(OBJFORMAT_ELF)
1116 #if defined(sparc_TARGET_ARCH)
1117 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1123 findElfSection ( void* objImage, Elf32_Word sh_type )
1126 char* ehdrC = (char*)objImage;
1127 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1128 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1130 for (i = 0; i < ehdr->e_shnum; i++) {
1131 if (shdr[i].sh_type == sh_type &&
1132 i != ehdr->e_shstrndx) {
1133 ptr = ehdrC + shdr[i].sh_offset;
1142 ocVerifyImage_ELF ( ObjectCode* oc )
1146 int i, j, nent, nstrtab, nsymtabs;
1150 char* ehdrC = (char*)(oc->image);
1151 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1153 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1154 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1155 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1156 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1157 belch("ocVerifyImage_ELF: not an ELF header");
1160 IF_DEBUG(linker,belch( "Is an ELF header" ));
1162 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1163 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1167 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1169 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1170 IF_DEBUG(linker,belch( "Is little-endian" ));
1172 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1173 IF_DEBUG(linker,belch( "Is big-endian" ));
1175 belch("ocVerifyImage_ELF: unknown endiannness");
1179 if (ehdr->e_type != ET_REL) {
1180 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1183 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1185 IF_DEBUG(linker,belch( "Architecture is " ));
1186 switch (ehdr->e_machine) {
1187 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1188 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1189 default: IF_DEBUG(linker,belch( "unknown" ));
1190 belch("ocVerifyImage_ELF: unknown architecture");
1194 IF_DEBUG(linker,belch(
1195 "\nSection header table: start %d, n_entries %d, ent_size %d",
1196 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1198 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1200 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1202 if (ehdr->e_shstrndx == SHN_UNDEF) {
1203 belch("ocVerifyImage_ELF: no section header string table");
1206 IF_DEBUG(linker,belch( "Section header string table is section %d",
1208 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1211 for (i = 0; i < ehdr->e_shnum; i++) {
1212 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1213 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1214 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1215 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1216 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1217 ehdrC + shdr[i].sh_offset,
1218 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1220 if (shdr[i].sh_type == SHT_REL) {
1221 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1222 } else if (shdr[i].sh_type == SHT_RELA) {
1223 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1225 IF_DEBUG(linker,fprintf(stderr," "));
1228 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1232 IF_DEBUG(linker,belch( "\nString tables" ));
1235 for (i = 0; i < ehdr->e_shnum; i++) {
1236 if (shdr[i].sh_type == SHT_STRTAB &&
1237 i != ehdr->e_shstrndx) {
1238 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1239 strtab = ehdrC + shdr[i].sh_offset;
1244 belch("ocVerifyImage_ELF: no string tables, or too many");
1249 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1250 for (i = 0; i < ehdr->e_shnum; i++) {
1251 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1252 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1254 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1255 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1256 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1258 shdr[i].sh_size % sizeof(Elf32_Sym)
1260 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1261 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1264 for (j = 0; j < nent; j++) {
1265 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1266 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1267 (int)stab[j].st_shndx,
1268 (int)stab[j].st_size,
1269 (char*)stab[j].st_value ));
1271 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1272 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1273 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1274 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1275 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1276 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1277 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1278 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1280 IF_DEBUG(linker,fprintf(stderr, " " ));
1282 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1283 switch (ELF32_ST_BIND(stab[j].st_info)) {
1284 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1285 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1286 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1287 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1289 IF_DEBUG(linker,fprintf(stderr, " " ));
1291 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1295 if (nsymtabs == 0) {
1296 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1305 ocGetNames_ELF ( ObjectCode* oc )
1310 char* ehdrC = (char*)(oc->image);
1311 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1312 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1313 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1314 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1316 ASSERT(symhash != NULL);
1319 belch("ocGetNames_ELF: no strtab");
1324 oc->sections = stgMallocBytes( ehdr->e_shnum * sizeof(Section),
1326 oc->n_sections = ehdr->e_shnum;
1328 for (i = 0; i < ehdr->e_shnum; i++) {
1330 /* make a section entry for relevant sections */
1331 SectionKind kind = SECTIONKIND_OTHER;
1332 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1333 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1334 kind = SECTIONKIND_RWDATA;
1335 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1336 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1337 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1338 kind = SECTIONKIND_CODE_OR_RODATA;
1340 /* fill in the section info */
1341 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1342 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1343 oc->sections[i].kind = kind;
1345 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1347 /* copy stuff into this module's object symbol table */
1348 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1349 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1350 oc->symbols = malloc(nent * sizeof(SymbolVal));
1351 oc->n_symbols = nent;
1352 for (j = 0; j < nent; j++) {
1353 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1354 || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL
1356 /* and not an undefined symbol */
1357 && stab[j].st_shndx != SHN_UNDEF
1358 /* and not in a "special section" */
1359 && stab[j].st_shndx < SHN_LORESERVE
1361 /* and it's a not a section or string table or anything silly */
1362 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1363 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1364 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE
1367 char* nm = strtab + stab[j].st_name;
1369 + shdr[ stab[j].st_shndx ].sh_offset
1373 oc->symbols[j].lbl = nm;
1374 oc->symbols[j].addr = ad;
1375 if (ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL) {
1376 IF_DEBUG(linker,belch( "addOTabName(LOCL): %10p %s %s",
1377 ad, oc->fileName, nm ));
1378 insertStrHashTable(oc->lochash, nm, &(oc->symbols[j]));
1380 IF_DEBUG(linker,belch( "addOTabName(GLOB): %10p %s %s",
1381 ad, oc->fileName, nm ));
1382 insertStrHashTable(symhash, nm, &(oc->symbols[j]));
1386 IF_DEBUG(linker,belch( "skipping `%s'",
1387 strtab + stab[j].st_name ));
1390 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1391 (int)ELF32_ST_BIND(stab[j].st_info),
1392 (int)ELF32_ST_TYPE(stab[j].st_info),
1393 (int)stab[j].st_shndx,
1394 strtab + stab[j].st_name
1397 oc->symbols[j].lbl = NULL;
1398 oc->symbols[j].addr = NULL;
1407 /* Do ELF relocations which lack an explicit addend. All x86-linux
1408 relocations appear to be of this form. */
1409 static int do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1410 Elf32_Shdr* shdr, int shnum,
1411 Elf32_Sym* stab, char* strtab )
1416 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1417 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1418 int target_shndx = shdr[shnum].sh_info;
1419 int symtab_shndx = shdr[shnum].sh_link;
1420 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1421 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1422 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1423 target_shndx, symtab_shndx ));
1424 for (j = 0; j < nent; j++) {
1425 Elf32_Addr offset = rtab[j].r_offset;
1426 Elf32_Word info = rtab[j].r_info;
1428 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1429 Elf32_Word* pP = (Elf32_Word*)P;
1433 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1434 j, (void*)offset, (void*)info ));
1436 IF_DEBUG(linker,belch( " ZERO" ));
1439 /* First see if it is a nameless local symbol. */
1440 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1441 symbol = "(noname)";
1443 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1444 + stab[ELF32_R_SYM(info)].st_value);
1446 /* No? Should be in a symbol table then; first try the
1448 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1449 (void*)S = lookupLocalSymbol( oc, symbol );
1450 if ((void*)S == NULL)
1451 (void*)S = lookupSymbol( symbol );
1454 barf("do_Elf32_Rel_relocations: %s: unknown symbol `%s'",
1455 oc->fileName, symbol);
1457 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1459 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1460 (void*)P, (void*)S, (void*)A ));
1461 switch (ELF32_R_TYPE(info)) {
1462 #ifdef i386_TARGET_ARCH
1463 case R_386_32: *pP = S + A; break;
1464 case R_386_PC32: *pP = S + A - P; break;
1467 barf("do_Elf32_Rel_relocations: unhandled ELF relocation(Rel) type %d\n", ELF32_R_TYPE(info));
1476 /* Do ELF relocations for which explicit addends are supplied.
1477 sparc-solaris relocations appear to be of this form. */
1478 static int do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1479 Elf32_Shdr* shdr, int shnum,
1480 Elf32_Sym* stab, char* strtab )
1485 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1486 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1487 int target_shndx = shdr[shnum].sh_info;
1488 int symtab_shndx = shdr[shnum].sh_link;
1489 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1490 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1491 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1492 target_shndx, symtab_shndx ));
1493 for (j = 0; j < nent; j++) {
1494 Elf32_Addr offset = rtab[j].r_offset;
1495 Elf32_Word info = rtab[j].r_info;
1496 Elf32_Sword addend = rtab[j].r_addend;
1498 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1499 Elf32_Addr A = addend;
1501 # if defined(sparc_TARGET_ARCH)
1502 /* This #ifdef only serves to avoid unused-var warnings. */
1503 Elf32_Word* pP = (Elf32_Word*)P;
1507 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1508 j, (void*)offset, (void*)info,
1511 IF_DEBUG(linker,belch( " ZERO" ));
1514 /* First see if it is a nameless local symbol. */
1515 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1516 symbol = "(noname)";
1518 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1519 + stab[ELF32_R_SYM(info)].st_value);
1521 /* No? Should be in a symbol table then; first try the
1523 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1524 (void*)S = lookupLocalSymbol( oc, symbol );
1525 if ((void*)S == NULL)
1526 (void*)S = lookupSymbol( symbol );
1529 barf("ocResolve_ELF: %s: unknown symbol `%s'",
1530 oc->fileName, symbol);
1533 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1536 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1538 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1539 (void*)P, (void*)S, (void*)A ));
1540 switch (ELF32_R_TYPE(info)) {
1541 # if defined(sparc_TARGET_ARCH)
1542 case R_SPARC_WDISP30:
1543 w1 = *pP & 0xC0000000;
1544 w2 = (Elf32_Word)((S + A - P) >> 2);
1545 ASSERT((w2 & 0xC0000000) == 0);
1550 w1 = *pP & 0xFFC00000;
1551 w2 = (Elf32_Word)((S + A) >> 10);
1552 ASSERT((w2 & 0xFFC00000) == 0);
1558 w2 = (Elf32_Word)((S + A) & 0x3FF);
1559 ASSERT((w2 & ~0x3FF) == 0);
1564 w2 = (Elf32_Word)(S + A);
1569 fprintf(stderr, "unhandled ELF relocation(RelA) type %d\n",
1570 ELF32_R_TYPE(info));
1571 barf("do_Elf32_Rela_relocations: unhandled ELF relocation type");
1581 ocResolve_ELF ( ObjectCode* oc )
1585 Elf32_Sym* stab = NULL;
1586 char* ehdrC = (char*)(oc->image);
1587 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1588 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1590 /* first find "the" symbol table */
1591 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1593 /* also go find the string table */
1594 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1596 if (stab == NULL || strtab == NULL) {
1597 belch("ocResolve_ELF: can't find string or symbol table");
1601 /* Process the relocation sections. */
1602 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
1603 if (shdr[shnum].sh_type == SHT_REL ) {
1604 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
1605 shnum, stab, strtab );
1609 if (shdr[shnum].sh_type == SHT_RELA) {
1610 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
1611 shnum, stab, strtab );
1616 /* Free the local symbol table; we won't need it again. */
1617 freeHashTable(oc->lochash, NULL);