1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.23 2001/02/12 12:22:01 simonmar Exp $
4 * (c) The GHC Team, 2000
8 * ---------------------------------------------------------------------------*/
15 #include "LinkerInternals.h"
17 #include "StoragePriv.h"
19 #ifdef HAVE_SYS_TYPES_H
20 #include <sys/types.h>
23 #ifdef HAVE_SYS_STAT_H
31 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) || defined(freebsd_TARGET_OS)
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);
298 /* -----------------------------------------------------------------------------
299 * Load an obj (populate the global symbol table, but don't resolve yet)
301 * Returns: 1 if ok, 0 on error.
304 loadObj( char *path )
312 /* assert that we haven't already loaded this object */
315 for (o = objects; o; o = o->next)
316 ASSERT(strcmp(o->fileName, path));
320 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
322 # if defined(OBJFORMAT_ELF)
323 oc->formatName = "ELF";
324 # elif defined(OBJFORMAT_PEi386)
325 oc->formatName = "PEi386";
328 barf("loadObj: not implemented on this platform");
332 if (r == -1) { return 0; }
334 /* sigh, stdup() isn't a POSIX function, so do it the long way */
335 oc->fileName = stgMallocBytes( strlen(path)+1, "loadObj" );
336 strcpy(oc->fileName, path);
338 oc->fileSize = st.st_size;
339 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
343 /* chain it onto the list of objects */
347 /* load the image into memory */
348 f = fopen(path, "rb");
350 barf("loadObj: can't read `%s'", path);
352 n = fread ( oc->image, 1, oc->fileSize, f );
353 if (n != oc->fileSize) {
355 barf("loadObj: error whilst reading `%s'", path);
358 /* verify the in-memory image */
359 # if defined(OBJFORMAT_ELF)
360 r = ocVerifyImage_ELF ( oc );
361 # elif defined(OBJFORMAT_PEi386)
362 r = ocVerifyImage_PEi386 ( oc );
364 barf("loadObj: no verify method");
366 if (!r) { return r; }
368 /* build the symbol list for this image */
369 # if defined(OBJFORMAT_ELF)
370 r = ocGetNames_ELF ( oc );
371 # elif defined(OBJFORMAT_PEi386)
372 r = ocGetNames_PEi386 ( oc );
374 barf("loadObj: no getNames method");
376 if (!r) { return r; }
378 /* loaded, but not resolved yet */
379 oc->status = OBJECT_LOADED;
384 /* -----------------------------------------------------------------------------
385 * resolve all the currently unlinked objects in memory
387 * Returns: 1 if ok, 0 on error.
395 for (oc = objects; oc; oc = oc->next) {
396 if (oc->status != OBJECT_RESOLVED) {
397 # if defined(OBJFORMAT_ELF)
398 r = ocResolve_ELF ( oc );
399 # elif defined(OBJFORMAT_PEi386)
400 r = ocResolve_PEi386 ( oc );
402 barf("link: not implemented on this platform");
404 if (!r) { return r; }
405 oc->status = OBJECT_RESOLVED;
411 /* -----------------------------------------------------------------------------
412 * delete an object from the pool
415 unloadObj( char *path )
417 ObjectCode *oc, *prev;
419 ASSERT(symhash != NULL);
420 ASSERT(objects != NULL);
423 for (oc = objects; oc; prev = oc, oc = oc->next) {
424 if (!strcmp(oc->fileName,path)) {
426 /* Remove all the mappings for the symbols within this
431 for (s = oc->symbols; s < oc->symbols + oc->n_symbols; s++) {
432 if (s->lbl != NULL) {
433 removeStrHashTable(symhash, s->lbl, NULL);
441 prev->next = oc->next;
444 /* We're going to leave this in place, in case there are
445 any pointers from the heap into it: */
446 /* free(oc->image); */
455 belch("unloadObj: can't find `%s' to unload", path);
459 /* --------------------------------------------------------------------------
460 * PEi386 specifics (Win32 targets)
461 * ------------------------------------------------------------------------*/
463 /* The information for this linker comes from
464 Microsoft Portable Executable
465 and Common Object File Format Specification
466 revision 5.1 January 1998
467 which SimonM says comes from the MS Developer Network CDs.
471 #if defined(OBJFORMAT_PEi386)
475 typedef unsigned char UChar;
476 typedef unsigned short UInt16;
477 typedef unsigned int UInt32;
484 UInt16 NumberOfSections;
485 UInt32 TimeDateStamp;
486 UInt32 PointerToSymbolTable;
487 UInt32 NumberOfSymbols;
488 UInt16 SizeOfOptionalHeader;
489 UInt16 Characteristics;
493 #define sizeof_COFF_header 20
500 UInt32 VirtualAddress;
501 UInt32 SizeOfRawData;
502 UInt32 PointerToRawData;
503 UInt32 PointerToRelocations;
504 UInt32 PointerToLinenumbers;
505 UInt16 NumberOfRelocations;
506 UInt16 NumberOfLineNumbers;
507 UInt32 Characteristics;
511 #define sizeof_COFF_section 40
518 UInt16 SectionNumber;
521 UChar NumberOfAuxSymbols;
525 #define sizeof_COFF_symbol 18
530 UInt32 VirtualAddress;
531 UInt32 SymbolTableIndex;
536 #define sizeof_COFF_reloc 10
539 /* From PE spec doc, section 3.3.2 */
540 #define IMAGE_FILE_RELOCS_STRIPPED 0x0001
541 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
542 #define IMAGE_FILE_DLL 0x2000
543 #define IMAGE_FILE_SYSTEM 0x1000
544 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
545 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
546 #define IMAGE_FILE_32BIT_MACHINE 0x0100
548 /* From PE spec doc, section 5.4.2 and 5.4.4 */
549 #define IMAGE_SYM_CLASS_EXTERNAL 2
550 #define IMAGE_SYM_CLASS_STATIC 3
551 #define IMAGE_SYM_UNDEFINED 0
553 /* From PE spec doc, section 4.1 */
554 #define IMAGE_SCN_CNT_CODE 0x00000020
555 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
557 /* From PE spec doc, section 5.2.1 */
558 #define IMAGE_REL_I386_DIR32 0x0006
559 #define IMAGE_REL_I386_REL32 0x0014
562 /* We use myindex to calculate array addresses, rather than
563 simply doing the normal subscript thing. That's because
564 some of the above structs have sizes which are not
565 a whole number of words. GCC rounds their sizes up to a
566 whole number of words, which means that the address calcs
567 arising from using normal C indexing or pointer arithmetic
568 are just plain wrong. Sigh.
571 myindex ( int scale, int index, void* base )
574 ((UChar*)base) + scale * index;
579 printName ( UChar* name, UChar* strtab )
581 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
582 UInt32 strtab_offset = * (UInt32*)(name+4);
583 fprintf ( stderr, "%s", strtab + strtab_offset );
586 for (i = 0; i < 8; i++) {
587 if (name[i] == 0) break;
588 fprintf ( stderr, "%c", name[i] );
595 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
597 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
598 UInt32 strtab_offset = * (UInt32*)(name+4);
599 strncpy ( dst, strtab+strtab_offset, dstSize );
605 if (name[i] == 0) break;
615 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
618 /* If the string is longer than 8 bytes, look in the
619 string table for it -- this will be correctly zero terminated.
621 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
622 UInt32 strtab_offset = * (UInt32*)(name+4);
623 return ((UChar*)strtab) + strtab_offset;
625 /* Otherwise, if shorter than 8 bytes, return the original,
626 which by defn is correctly terminated.
628 if (name[7]==0) return name;
629 /* The annoying case: 8 bytes. Copy into a temporary
630 (which is never freed ...)
634 strncpy(newstr,name,8);
641 /* Just compares the short names (first 8 chars) */
642 static COFF_section *
643 findPEi386SectionCalled ( ObjectCode* oc, char* name )
647 = (COFF_header*)(oc->image);
650 ((UChar*)(oc->image))
651 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
653 for (i = 0; i < hdr->NumberOfSections; i++) {
656 COFF_section* section_i
658 myindex ( sizeof_COFF_section, i, sectab );
659 n1 = (UChar*) &(section_i->Name);
661 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
662 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
663 n1[6]==n2[6] && n1[7]==n2[7])
672 zapTrailingAtSign ( UChar* sym )
675 if (sym[0] == 0) return;
677 while (sym[i] != 0) i++;
680 while (j > 0 && isdigit(sym[j])) j--;
681 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
686 ocVerifyImage_PEi386 ( ObjectCode* oc )
690 COFF_section* sectab;
694 hdr = (COFF_header*)(oc->image);
695 sectab = (COFF_section*) (
696 ((UChar*)(oc->image))
697 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
699 symtab = (COFF_symbol*) (
700 ((UChar*)(oc->image))
701 + hdr->PointerToSymbolTable
703 strtab = ((UChar*)(oc->image))
704 + hdr->PointerToSymbolTable
705 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
707 if (hdr->Machine != 0x14c) {
708 oc->errMsg("Not x86 PEi386");
711 if (hdr->SizeOfOptionalHeader != 0) {
712 oc->errMsg("PEi386 with nonempty optional header");
715 if ( /* (hdr->Characteristics & IMAGE_FILE_RELOCS_STRIPPED) || */
716 (hdr->Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ||
717 (hdr->Characteristics & IMAGE_FILE_DLL) ||
718 (hdr->Characteristics & IMAGE_FILE_SYSTEM) ) {
719 oc->errMsg("Not a PEi386 object file");
722 if ( (hdr->Characteristics & IMAGE_FILE_BYTES_REVERSED_HI) ||
723 !(hdr->Characteristics & IMAGE_FILE_32BIT_MACHINE) ) {
724 oc->errMsg("Invalid PEi386 word size or endiannness");
728 if (!verb) return TRUE;
729 /* No further verification after this point; only debug printing. */
732 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
734 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
736 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
738 fprintf ( stderr, "\n" );
740 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
742 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
744 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
746 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
748 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
750 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
752 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
754 fprintf ( stderr, "\n" );
755 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
756 fprintf ( stderr, "---START of string table---\n");
757 for (i = 4; i < *(UInt32*)strtab; i++) {
759 fprintf ( stderr, "\n"); else
760 fprintf( stderr, "%c", strtab[i] );
762 fprintf ( stderr, "--- END of string table---\n");
764 fprintf ( stderr, "\n" );
765 for (i = 0; i < hdr->NumberOfSections; i++) {
767 COFF_section* sectab_i
769 myindex ( sizeof_COFF_section, i, sectab );
776 printName ( sectab_i->Name, strtab );
785 sectab_i->VirtualSize,
786 sectab_i->VirtualAddress,
787 sectab_i->SizeOfRawData,
788 sectab_i->PointerToRawData,
789 sectab_i->NumberOfRelocations,
790 sectab_i->PointerToRelocations
792 reltab = (COFF_reloc*) (
793 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
795 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
797 COFF_reloc* rel = (COFF_reloc*)
798 myindex ( sizeof_COFF_reloc, j, reltab );
800 " type 0x%-4x vaddr 0x%-8x name `",
802 rel->VirtualAddress );
804 myindex ( sizeof_COFF_symbol, rel->SymbolTableIndex, symtab );
805 printName ( sym->Name, strtab );
806 fprintf ( stderr, "'\n" );
808 fprintf ( stderr, "\n" );
812 fprintf ( stderr, "\n" );
815 COFF_symbol* symtab_i;
816 if (i >= hdr->NumberOfSymbols) break;
817 symtab_i = (COFF_symbol*)
818 myindex ( sizeof_COFF_symbol, i, symtab );
824 printName ( symtab_i->Name, strtab );
833 (Int32)(symtab_i->SectionNumber) - 1,
834 (UInt32)symtab_i->Type,
835 (UInt32)symtab_i->StorageClass,
836 (UInt32)symtab_i->NumberOfAuxSymbols
838 i += symtab_i->NumberOfAuxSymbols;
842 fprintf ( stderr, "\n" );
849 ocGetNames_PEi386 ( ObjectCode* oc )
852 COFF_section* sectab;
860 hdr = (COFF_header*)(oc->image);
861 sectab = (COFF_section*) (
862 ((UChar*)(oc->image))
863 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
865 symtab = (COFF_symbol*) (
866 ((UChar*)(oc->image))
867 + hdr->PointerToSymbolTable
869 strtab = ((UChar*)(oc->image))
870 + hdr->PointerToSymbolTable
871 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
873 /* Copy exported symbols into the ObjectCode. */
876 COFF_symbol* symtab_i;
877 if (i >= hdr->NumberOfSymbols) break;
878 symtab_i = (COFF_symbol*)
879 myindex ( sizeof_COFF_symbol, i, symtab );
881 if (symtab_i->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
882 symtab_i->SectionNumber != IMAGE_SYM_UNDEFINED) {
884 /* This symbol is global and defined, viz, exported */
885 COFF_section* sectabent;
887 sname = cstring_from_COFF_symbol_name (
888 symtab_i->Name, strtab
891 oc->errMsg("Out of memory when copying PEi386 symbol");
895 /* for IMAGE_SYMCLASS_EXTERNAL
896 && !IMAGE_SYM_UNDEFINED,
897 the address of the symbol is:
898 address of relevant section + offset in section
900 sectabent = (COFF_section*)
901 myindex ( sizeof_COFF_section,
902 symtab_i->SectionNumber-1,
904 addr = ((UChar*)(oc->image))
905 + (sectabent->PointerToRawData
907 /* fprintf ( stderr, "addSymbol %p `%s'\n", addr,sname); */
908 if (!addSymbol(oc,sname,addr)) return FALSE;
910 i += symtab_i->NumberOfAuxSymbols;
914 oc->sections = stgMallocBytes( NumberOfSections * sizeof(Section),
915 "ocGetNamesPEi386" );
917 /* Copy section information into the ObjectCode. */
918 for (i = 0; i < hdr->NumberOfSections; i++) {
924 COFF_section* sectab_i
926 myindex ( sizeof_COFF_section, i, sectab );
927 /* fprintf ( stderr, "section name = %s\n", sectab_i->Name ); */
930 /* I'm sure this is the Right Way to do it. However, the
931 alternative of testing the sectab_i->Name field seems to
934 if (sectab_i->Characteristics & IMAGE_SCN_CNT_CODE ||
935 sectab_i->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
936 kind = SECTIONKIND_CODE_OR_RODATA;
939 if (0==strcmp(".text",sectab_i->Name))
940 kind = SECTIONKIND_CODE_OR_RODATA;
941 if (0==strcmp(".data",sectab_i->Name) ||
942 0==strcmp(".bss",sectab_i->Name))
943 kind = SECTIONKIND_RWDATA;
945 start = ((UChar*)(oc->image))
946 + sectab_i->PointerToRawData;
948 + sectab_i->SizeOfRawData - 1;
950 if (kind != SECTIONKIND_OTHER) {
951 addSection ( oc, start, end, kind );
953 fprintf ( stderr, "unknown section name = `%s'\n",
955 oc->errMsg("Unknown PEi386 section name");
965 ocResolve_PEi386 ( ObjectCode* oc, int verb )
968 COFF_section* sectab;
977 char symbol[1000]; // ToDo
979 hdr = (COFF_header*)(oc->image);
980 sectab = (COFF_section*) (
981 ((UChar*)(oc->image))
982 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
984 symtab = (COFF_symbol*) (
985 ((UChar*)(oc->image))
986 + hdr->PointerToSymbolTable
988 strtab = ((UChar*)(oc->image))
989 + hdr->PointerToSymbolTable
990 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
992 for (i = 0; i < hdr->NumberOfSections; i++) {
993 COFF_section* sectab_i
995 myindex ( sizeof_COFF_section, i, sectab );
998 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1000 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1002 COFF_reloc* reltab_j
1004 myindex ( sizeof_COFF_reloc, j, reltab );
1006 /* the location to patch */
1008 ((UChar*)(oc->image))
1009 + (sectab_i->PointerToRawData
1010 + reltab_j->VirtualAddress)
1012 /* the existing contents of pP */
1014 /* the symbol to connect to */
1015 sym = (COFF_symbol*)
1016 myindex ( sizeof_COFF_symbol,
1017 reltab_j->SymbolTableIndex, symtab );
1020 "reloc sec %2d num %3d: type 0x%-4x "
1021 "vaddr 0x%-8x name `",
1023 (UInt32)reltab_j->Type,
1024 reltab_j->VirtualAddress );
1025 printName ( sym->Name, strtab );
1026 fprintf ( stderr, "'\n" );
1029 if (sym->StorageClass == IMAGE_SYM_CLASS_STATIC) {
1030 COFF_section* section_sym
1031 = findPEi386SectionCalled ( oc, sym->Name );
1033 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
1034 oc->errMsg("Can't find abovementioned PEi386 section");
1037 S = ((UInt32)(oc->image))
1038 + (section_sym->PointerToRawData
1041 copyName ( sym->Name, strtab, symbol, 1000 );
1042 zapTrailingAtSign ( symbol );
1043 S = (UInt32) ocLookupSym ( oc, symbol );
1045 S = (UInt32)(oc->clientLookup ( symbol ));
1047 belch("%s: unresolvable reference to `%s'", oc->fileName, symbol);
1052 switch (reltab_j->Type) {
1053 case IMAGE_REL_I386_DIR32:
1056 case IMAGE_REL_I386_REL32:
1057 /* Tricky. We have to insert a displacement at
1058 pP which, when added to the PC for the _next_
1059 insn, gives the address of the target (S).
1060 Problem is to know the address of the next insn
1061 when we only know pP. We assume that this
1062 literal field is always the last in the insn,
1063 so that the address of the next insn is pP+4
1064 -- hence the constant 4.
1065 Also I don't know if A should be added, but so
1066 far it has always been zero.
1069 *pP = S - ((UInt32)pP) - 4;
1073 "unhandled PEi386 relocation type %d\n",
1075 oc->errMsg("unhandled PEi386 relocation type");
1085 #endif /* defined(OBJFORMAT_PEi386) */
1088 /* --------------------------------------------------------------------------
1090 * ------------------------------------------------------------------------*/
1092 #if defined(OBJFORMAT_ELF)
1097 #if defined(sparc_TARGET_ARCH)
1098 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
1104 findElfSection ( void* objImage, Elf32_Word sh_type )
1107 char* ehdrC = (char*)objImage;
1108 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1109 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1111 for (i = 0; i < ehdr->e_shnum; i++) {
1112 if (shdr[i].sh_type == sh_type &&
1113 i != ehdr->e_shstrndx) {
1114 ptr = ehdrC + shdr[i].sh_offset;
1123 ocVerifyImage_ELF ( ObjectCode* oc )
1127 int i, j, nent, nstrtab, nsymtabs;
1131 char* ehdrC = (char*)(oc->image);
1132 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1134 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1135 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1136 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1137 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1138 belch("ocVerifyImage_ELF: not an ELF header");
1141 IF_DEBUG(linker,belch( "Is an ELF header" ));
1143 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1144 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1148 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1150 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1151 IF_DEBUG(linker,belch( "Is little-endian" ));
1153 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1154 IF_DEBUG(linker,belch( "Is big-endian" ));
1156 belch("ocVerifyImage_ELF: unknown endiannness");
1160 if (ehdr->e_type != ET_REL) {
1161 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1164 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1166 IF_DEBUG(linker,belch( "Architecture is " ));
1167 switch (ehdr->e_machine) {
1168 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1169 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1170 default: IF_DEBUG(linker,belch( "unknown" ));
1171 belch("ocVerifyImage_ELF: unknown architecture");
1175 IF_DEBUG(linker,belch(
1176 "\nSection header table: start %d, n_entries %d, ent_size %d",
1177 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1179 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1181 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1183 if (ehdr->e_shstrndx == SHN_UNDEF) {
1184 belch("ocVerifyImage_ELF: no section header string table");
1187 IF_DEBUG(linker,belch( "Section header string table is section %d",
1189 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1192 for (i = 0; i < ehdr->e_shnum; i++) {
1193 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1194 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", (int)shdr[i].sh_type ));
1195 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", (int)shdr[i].sh_size ));
1196 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", (int)shdr[i].sh_offset ));
1197 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1198 ehdrC + shdr[i].sh_offset,
1199 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1201 if (shdr[i].sh_type == SHT_REL) {
1202 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1203 } else if (shdr[i].sh_type == SHT_RELA) {
1204 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1206 IF_DEBUG(linker,fprintf(stderr," "));
1209 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1213 IF_DEBUG(linker,belch( "\nString tables" ));
1216 for (i = 0; i < ehdr->e_shnum; i++) {
1217 if (shdr[i].sh_type == SHT_STRTAB &&
1218 i != ehdr->e_shstrndx) {
1219 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1220 strtab = ehdrC + shdr[i].sh_offset;
1225 belch("ocVerifyImage_ELF: no string tables, or too many");
1230 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1231 for (i = 0; i < ehdr->e_shnum; i++) {
1232 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1233 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1235 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1236 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1237 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1239 shdr[i].sh_size % sizeof(Elf32_Sym)
1241 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1242 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1245 for (j = 0; j < nent; j++) {
1246 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1247 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1248 (int)stab[j].st_shndx,
1249 (int)stab[j].st_size,
1250 (char*)stab[j].st_value ));
1252 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1253 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1254 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1255 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1256 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1257 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1258 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1259 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1261 IF_DEBUG(linker,fprintf(stderr, " " ));
1263 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1264 switch (ELF32_ST_BIND(stab[j].st_info)) {
1265 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1266 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1267 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1268 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1270 IF_DEBUG(linker,fprintf(stderr, " " ));
1272 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1276 if (nsymtabs == 0) {
1277 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1286 ocGetNames_ELF ( ObjectCode* oc )
1291 char* ehdrC = (char*)(oc->image);
1292 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1293 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1294 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1295 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1297 ASSERT(symhash != NULL);
1300 belch("ocGetNames_ELF: no strtab");
1305 oc->sections = stgMallocBytes( ehdr->e_shnum * sizeof(Section),
1307 oc->n_sections = ehdr->e_shnum;
1309 for (i = 0; i < ehdr->e_shnum; i++) {
1311 /* make a section entry for relevant sections */
1312 SectionKind kind = SECTIONKIND_OTHER;
1313 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1314 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1315 kind = SECTIONKIND_RWDATA;
1316 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1317 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1318 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1319 kind = SECTIONKIND_CODE_OR_RODATA;
1321 /* fill in the section info */
1322 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1323 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1324 oc->sections[i].kind = kind;
1326 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1328 /* copy stuff into this module's object symbol table */
1329 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1330 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1331 oc->symbols = malloc(nent * sizeof(SymbolVal));
1332 oc->n_symbols = nent;
1333 for (j = 0; j < nent; j++) {
1334 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL
1335 /* || ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL */
1337 /* and not an undefined symbol */
1338 && stab[j].st_shndx != SHN_UNDEF
1340 /* and it's a not a section or string table or anything silly */
1341 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1342 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1343 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE )
1345 char* nm = strtab + stab[j].st_name;
1347 + shdr[ stab[j].st_shndx ].sh_offset
1351 IF_DEBUG(linker,belch( "addOTabName: %10p %s %s",
1352 ad, oc->fileName, nm ));
1353 oc->symbols[j].lbl = nm;
1354 oc->symbols[j].addr = ad;
1355 insertStrHashTable(symhash, nm, &(oc->symbols[j]));
1358 IF_DEBUG(linker,belch( "skipping `%s'",
1359 strtab + stab[j].st_name ));
1362 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1363 (int)ELF32_ST_BIND(stab[j].st_info),
1364 (int)ELF32_ST_TYPE(stab[j].st_info),
1365 (int)stab[j].st_shndx,
1366 strtab + stab[j].st_name
1369 oc->symbols[j].lbl = NULL;
1370 oc->symbols[j].addr = NULL;
1379 /* Do ELF relocations which lack an explicit addend. All x86-linux
1380 relocations appear to be of this form. */
1381 static int do_Elf32_Rel_relocations ( ObjectCode* oc, char* ehdrC,
1382 Elf32_Shdr* shdr, int shnum,
1383 Elf32_Sym* stab, char* strtab )
1388 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[shnum].sh_offset);
1389 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rel);
1390 int target_shndx = shdr[shnum].sh_info;
1391 int symtab_shndx = shdr[shnum].sh_link;
1392 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1393 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1394 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1395 target_shndx, symtab_shndx ));
1396 for (j = 0; j < nent; j++) {
1397 Elf32_Addr offset = rtab[j].r_offset;
1398 Elf32_Word info = rtab[j].r_info;
1400 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1401 Elf32_Word* pP = (Elf32_Word*)P;
1405 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p)",
1406 j, (void*)offset, (void*)info ));
1408 IF_DEBUG(linker,belch( " ZERO" ));
1411 /* First see if it is a nameless local symbol. */
1412 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1413 symbol = "(noname)";
1415 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1416 + stab[ELF32_R_SYM(info)].st_value);
1418 /* No? Should be in the symbol table then. */
1419 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1420 (void *)S = lookupSymbol( symbol );
1423 barf("do_Elf32_Rel_relocations: %s: unknown symbol `%s'",
1424 oc->fileName, symbol);
1426 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1428 IF_DEBUG(linker,belch( "Reloc: P = %p S = %p A = %p",
1429 (void*)P, (void*)S, (void*)A ));
1430 switch (ELF32_R_TYPE(info)) {
1431 #ifdef i386_TARGET_ARCH
1432 case R_386_32: *pP = S + A; break;
1433 case R_386_PC32: *pP = S + A - P; break;
1436 barf("do_Elf32_Rel_relocations: unhandled ELF relocation(Rel) type %d\n", ELF32_R_TYPE(info));
1445 /* Do ELF relocations for which explicit addends are supplied.
1446 sparc-solaris relocations appear to be of this form. */
1447 static int do_Elf32_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1448 Elf32_Shdr* shdr, int shnum,
1449 Elf32_Sym* stab, char* strtab )
1454 Elf32_Rela* rtab = (Elf32_Rela*) (ehdrC + shdr[shnum].sh_offset);
1455 int nent = shdr[shnum].sh_size / sizeof(Elf32_Rela);
1456 int target_shndx = shdr[shnum].sh_info;
1457 int symtab_shndx = shdr[shnum].sh_link;
1458 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1459 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1460 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1461 target_shndx, symtab_shndx ));
1462 for (j = 0; j < nent; j++) {
1463 Elf32_Addr offset = rtab[j].r_offset;
1464 Elf32_Word info = rtab[j].r_info;
1465 Elf32_Sword addend = rtab[j].r_addend;
1467 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1468 Elf32_Addr A = addend;
1470 # if defined(sparc_TARGET_ARCH)
1471 /* This #ifdef only serves to avoid unused-var warnings. */
1472 Elf32_Word* pP = (Elf32_Word*)P;
1476 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
1477 j, (void*)offset, (void*)info,
1480 IF_DEBUG(linker,belch( " ZERO" ));
1483 /* First see if it is a nameless local symbol. */
1484 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1485 symbol = "(noname)";
1487 (ehdrC + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1488 + stab[ELF32_R_SYM(info)].st_value);
1490 /* No? Should be in the symbol table then. */
1491 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1492 (void *)S = lookupSymbol( symbol );
1495 barf("ocResolve_ELF: %s: unknown symbol `%s'",
1496 oc->fileName, symbol);
1499 fprintf ( stderr, "S %p A %p S+A %p S+A-P %p\n",S,A,S+A,S+A-P);
1502 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1504 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n",
1505 (void*)P, (void*)S, (void*)A ));
1506 switch (ELF32_R_TYPE(info)) {
1507 # if defined(sparc_TARGET_ARCH)
1508 case R_SPARC_WDISP30:
1509 w1 = *pP & 0xC0000000;
1510 w2 = (Elf32_Word)((S + A - P) >> 2);
1511 ASSERT((w2 & 0xC0000000) == 0);
1516 w1 = *pP & 0xFFC00000;
1517 w2 = (Elf32_Word)((S + A) >> 10);
1518 ASSERT((w2 & 0xFFC00000) == 0);
1524 w2 = (Elf32_Word)((S + A) & 0x3FF);
1525 ASSERT((w2 & ~0x3FF) == 0);
1530 w2 = (Elf32_Word)(S + A);
1533 case R_SPARC_NONE: belch("R_SPARC_NONE");
1538 fprintf(stderr, "unhandled ELF relocation(RelA) type %d\n",
1539 ELF32_R_TYPE(info));
1540 barf("do_Elf32_Rela_relocations: unhandled ELF relocation type");
1550 ocResolve_ELF ( ObjectCode* oc )
1554 Elf32_Sym* stab = NULL;
1555 char* ehdrC = (char*)(oc->image);
1556 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1557 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1559 /* first find "the" symbol table */
1560 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1562 /* also go find the string table */
1563 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1565 if (stab == NULL || strtab == NULL) {
1566 belch("ocResolve_ELF: can't find string or symbol table");
1570 /* Process the relocation sections. */
1571 for (shnum = 0; shnum < ehdr->e_shnum; shnum++) {
1572 if (shdr[shnum].sh_type == SHT_REL ) {
1573 ok = do_Elf32_Rel_relocations ( oc, ehdrC, shdr,
1574 shnum, stab, strtab );
1578 if (shdr[shnum].sh_type == SHT_RELA) {
1579 ok = do_Elf32_Rela_relocations ( oc, ehdrC, shdr,
1580 shnum, stab, strtab );