1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.9 2001/01/18 10:31:38 simonmar Exp $
4 * (c) The GHC Team, 2000
8 * ---------------------------------------------------------------------------*/
17 /* These two are POSIX headers */
18 #include <sys/types.h>
21 /* ToDo: configure this */
24 #ifdef GHCI /* endif is right at end of file */
26 /* A bucket in the symbol hash-table. Primarily, maps symbol names to
27 * absolute addresses. All symbols from a given module are linked
28 * together, so they can be freed at the same time. There's also a
29 * bucket link field for the hash table.
31 typedef struct _SymbolVal {
36 typedef enum { OBJECT_LOADED, OBJECT_RESOLVED } OStatus;
38 /* Indication of section kinds for loaded objects. Needed by
39 the GC for deciding whether or not a pointer on the stack
42 typedef enum { SECTIONKIND_CODE_OR_RODATA,
45 SECTIONKIND_NOINFOAVAIL }
48 typedef struct { void* start; void* end; SectionKind kind; }
51 /* Top-level structure for an object module. One of these is allocated
52 * for each object file in use.
54 typedef struct _ObjectCode {
58 char* formatName; /* eg "ELF32", "DLL", "COFF", etc. */
63 /* ptr to malloc'd lump of memory holding the obj file */
66 /* The section-kind entries for this object module. Dynamically expands. */
70 /* Allow a chain of these things */
71 struct _ObjectCode * next;
75 /* Hash table mapping symbol names to Symbol */
76 /*Str*/HashTable *symhash;
78 /* List of currently loaded objects */
81 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
82 static int ocVerifyImage_ELF ( ObjectCode* oc );
83 static int ocGetNames_ELF ( ObjectCode* oc );
84 static int ocResolve_ELF ( ObjectCode* oc );
85 #elif defined(cygwin32_TARGET_OS)
86 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
87 static int ocGetNames_PEi386 ( ObjectCode* oc );
88 static int ocResolve_PEi386 ( ObjectCode* oc );
91 /* -----------------------------------------------------------------------------
92 * Built-in symbols from the RTS
104 Sym(stg_gc_unbx_r1) \
109 SymX(stg_update_PAP) \
110 SymX(stg_ap_2_upd_info) \
111 SymX(stg_ap_3_upd_info) \
112 SymX(stg_ap_4_upd_info) \
113 SymX(stg_ap_5_upd_info) \
114 SymX(stg_ap_6_upd_info) \
115 SymX(stg_ap_7_upd_info) \
116 SymX(stg_ap_8_upd_info) \
117 SymX(stg_sel_0_upd_info) \
118 SymX(stg_sel_1_upd_info) \
119 SymX(stg_sel_2_upd_info) \
120 SymX(stg_sel_3_upd_info) \
121 SymX(stg_sel_4_upd_info) \
122 SymX(stg_sel_5_upd_info) \
123 SymX(stg_sel_6_upd_info) \
124 SymX(stg_sel_7_upd_info) \
125 SymX(stg_sel_8_upd_info) \
126 SymX(stg_sel_9_upd_info) \
127 SymX(stg_sel_10_upd_info) \
128 SymX(stg_sel_11_upd_info) \
129 SymX(stg_sel_12_upd_info) \
130 SymX(stg_upd_frame_info) \
131 SymX(stg_seq_frame_info) \
132 SymX(stg_CAF_BLACKHOLE_info) \
133 SymX(stg_IND_STATIC_info) \
134 SymX(stg_EMPTY_MVAR_info) \
135 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
136 SymX(stg_WEAK_info) \
137 SymX(stg_CHARLIKE_closure) \
138 SymX(stg_INTLIKE_closure) \
139 SymX(stg_CAF_UNENTERED_entry) \
141 SymX(newBCOzh_fast) \
142 SymX(mkApUpd0zh_fast) \
143 SymX(putMVarzh_fast) \
144 SymX(newMVarzh_fast) \
145 SymX(takeMVarzh_fast) \
146 SymX(tryTakeMVarzh_fast) \
152 SymX(killThreadzh_fast) \
153 SymX(waitReadzh_fast) \
154 SymX(waitWritezh_fast) \
155 SymX(suspendThread) \
157 SymX(stackOverflow) \
158 SymX(int2Integerzh_fast) \
159 SymX(word2Integerzh_fast) \
160 SymX(mkForeignObjzh_fast) \
161 SymX(__encodeDouble) \
162 SymX(decodeDoublezh_fast) \
163 SymX(decodeFloatzh_fast) \
164 SymX(gcdIntegerzh_fast) \
165 SymX(newArrayzh_fast) \
166 SymX(unsafeThawArrayzh_fast) \
167 SymX(newByteArrayzh_fast) \
168 SymX(newMutVarzh_fast) \
169 SymX(quotRemIntegerzh_fast) \
170 SymX(quotIntegerzh_fast) \
171 SymX(remIntegerzh_fast) \
172 SymX(divExactIntegerzh_fast) \
173 SymX(divModIntegerzh_fast) \
174 SymX(timesIntegerzh_fast) \
175 SymX(minusIntegerzh_fast) \
176 SymX(plusIntegerzh_fast) \
177 SymX(andIntegerzh_fast) \
178 SymX(orIntegerzh_fast) \
179 SymX(xorIntegerzh_fast) \
180 SymX(complementIntegerzh_fast) \
181 SymX(mkWeakzh_fast) \
182 SymX(makeStableNamezh_fast) \
183 SymX(finalizzeWeakzh_fast) \
184 SymX(blockAsyncExceptionszh_fast) \
185 SymX(unblockAsyncExceptionszh_fast) \
187 SymX(isDoubleInfinite) \
188 SymX(isDoubleDenormalized) \
189 SymX(isDoubleNegativeZero) \
190 SymX(__encodeFloat) \
192 SymX(isFloatInfinite) \
193 SymX(isFloatDenormalized) \
194 SymX(isFloatNegativeZero) \
195 SymX(__int_encodeFloat) \
196 SymX(__int_encodeDouble) \
197 SymX(__gmpz_cmp_si) \
202 SymX(resetNonBlockingFd) \
204 SymX(stable_ptr_table) \
205 SymX(shutdownHaskellAndExit) \
206 Sym(stg_enterStackTop) \
207 Sym(stg_yield_to_interpreter) \
211 Sym(__init_PrelGHC) \
212 SymX(freeHaskellFunctionPtr) \
215 SymX(NoRunnableThreadsHook) \
216 SymX(StackOverflowHook) \
217 SymX(OutOfHeapHook) \
218 SymX(MallocFailHook) \
219 SymX(PatErrorHdrHook) \
224 #ifndef SUPPORT_LONG_LONGS
225 #define RTS_LONG_LONG_SYMS /* nothing */
227 #define RTS_LONG_LONG_SYMS \
240 SymX(stg_remWord64) \
241 SymX(stg_quotWord64) \
243 SymX(stg_quotInt64) \
244 SymX(stg_negateInt64) \
245 SymX(stg_plusInt64) \
246 SymX(stg_minusInt64) \
247 SymX(stg_timesInt64) \
253 SymX(stg_shiftRL64) \
254 SymX(stg_iShiftL64) \
255 SymX(stg_iShiftRL64) \
256 SymX(stg_iShiftRA64) \
257 SymX(stg_intToInt64) \
258 SymX(stg_int64ToInt) \
259 SymX(stg_int64ToWord64) \
260 SymX(stg_wordToWord64) \
261 SymX(stg_word64ToWord) \
262 SymX(stg_word64ToInt64) \
263 SymX(int64ToIntegerzh_fast) \
264 SymX(word64ToIntegerzh_fast)
265 #endif /* SUPPORT_LONG_LONGS */
267 /* entirely bogus claims about types of these symbols */
268 #define Sym(vvv) extern void (vvv);
269 #define SymX(vvv) /**/
274 #ifdef LEADING_UNDERSCORE
275 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
277 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
280 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
282 #define SymX(vvv) Sym(vvv)
284 static SymbolVal rtsSyms[] = {
287 { 0, 0 } /* sentinel */
290 /* -----------------------------------------------------------------------------
291 * initialize the object linker
293 static void *dl_prog_handle;
300 symhash = allocStrHashTable();
302 /* populate the symbol table with stuff from the RTS */
303 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
304 insertStrHashTable(symhash, sym->lbl, sym);
307 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
310 /* -----------------------------------------------------------------------------
311 * lookup a symbol in the hash table
314 lookupSymbol( char *lbl )
317 val = lookupStrHashTable(symhash, lbl);
320 return dlsym(dl_prog_handle, lbl);
326 /* -----------------------------------------------------------------------------
327 * Load an obj (populate the global symbol table, but don't resolve yet)
329 * Returns: 1 if ok, 0 on error.
332 loadObj( char *path )
340 /* assert that we haven't already loaded this object */
343 for (o = objects; o; o = o->next)
344 ASSERT(strcmp(o->fileName, path));
348 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
350 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
351 oc->formatName = "ELF";
352 # elif defined(cygwin32_TARGET_OS)
353 oc->formatName = "PEi386";
356 barf("loadObj: not implemented on this platform");
360 if (r == -1) { return 0; }
363 oc->fileSize = st.st_size;
364 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
368 /* chain it onto the list of objects */
372 /* load the image into memory */
373 f = fopen(path, "rb");
375 barf("loadObj: can't read `%s'", path);
377 n = fread ( oc->image, 1, oc->fileSize, f );
378 if (n != oc->fileSize) {
380 barf("loadObj: error whilst reading `%s'", path);
383 /* verify the in-memory image */
384 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
385 r = ocVerifyImage_ELF ( oc );
386 # elif defined(cygwin32_TARGET_OS)
387 r = ocVerifyImage_PEi386 ( oc );
389 barf("loadObj: no verify method");
391 if (!r) { return r; }
393 /* build the symbol list for this image */
394 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
395 r = ocGetNames_ELF ( oc );
396 # elif defined(cygwin32_TARGET_OS)
397 r = ocGetNames_PEi386 ( oc );
399 barf("loadObj: no getNames method");
401 if (!r) { return r; }
403 /* loaded, but not resolved yet */
404 oc->status = OBJECT_LOADED;
409 /* -----------------------------------------------------------------------------
410 * resolve all the currently unlinked objects in memory
412 * Returns: 1 if ok, 0 on error.
420 for (oc = objects; oc; oc = oc->next) {
421 if (oc->status != OBJECT_RESOLVED) {
422 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
423 r = ocResolve_ELF ( oc );
424 # elif defined(cygwin32_TARGET_OS)
425 r = ocResolve_PEi386 ( oc );
427 barf("link: not implemented on this platform");
429 if (!r) { return r; }
430 oc->status = OBJECT_RESOLVED;
436 /* -----------------------------------------------------------------------------
437 * delete an object from the pool
440 unloadObj( char *path )
444 for (oc = objects; oc; oc = oc->next) {
445 if (!strcmp(oc->fileName,path)) {
447 /* Remove all the mappings for the symbols within this
452 for (s = oc->symbols; s < oc->symbols + oc->n_symbols; s++) {
453 removeStrHashTable(symhash, s->lbl, NULL);
457 /* We're going to leave this in place, in case there are
458 any pointers from the heap into it: */
459 /* free(oc->image); */
467 belch("unloadObj: can't find `%s' to unload", path);
471 /* --------------------------------------------------------------------------
472 * PEi386 specifics (cygwin32)
473 * ------------------------------------------------------------------------*/
475 /* The information for this linker comes from
476 Microsoft Portable Executable
477 and Common Object File Format Specification
478 revision 5.1 January 1998
479 which SimonM says comes from the MS Developer Network CDs.
483 #if defined(cygwin32_TARGET_OS)
487 typedef unsigned char UChar;
488 typedef unsigned short UInt16;
489 typedef unsigned int UInt32;
496 UInt16 NumberOfSections;
497 UInt32 TimeDateStamp;
498 UInt32 PointerToSymbolTable;
499 UInt32 NumberOfSymbols;
500 UInt16 SizeOfOptionalHeader;
501 UInt16 Characteristics;
505 #define sizeof_COFF_header 20
512 UInt32 VirtualAddress;
513 UInt32 SizeOfRawData;
514 UInt32 PointerToRawData;
515 UInt32 PointerToRelocations;
516 UInt32 PointerToLinenumbers;
517 UInt16 NumberOfRelocations;
518 UInt16 NumberOfLineNumbers;
519 UInt32 Characteristics;
523 #define sizeof_COFF_section 40
530 UInt16 SectionNumber;
533 UChar NumberOfAuxSymbols;
537 #define sizeof_COFF_symbol 18
542 UInt32 VirtualAddress;
543 UInt32 SymbolTableIndex;
548 #define sizeof_COFF_reloc 10
551 /* From PE spec doc, section 3.3.2 */
552 #define IMAGE_FILE_RELOCS_STRIPPED 0x0001
553 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
554 #define IMAGE_FILE_DLL 0x2000
555 #define IMAGE_FILE_SYSTEM 0x1000
556 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
557 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
558 #define IMAGE_FILE_32BIT_MACHINE 0x0100
560 /* From PE spec doc, section 5.4.2 and 5.4.4 */
561 #define IMAGE_SYM_CLASS_EXTERNAL 2
562 #define IMAGE_SYM_CLASS_STATIC 3
563 #define IMAGE_SYM_UNDEFINED 0
565 /* From PE spec doc, section 4.1 */
566 #define IMAGE_SCN_CNT_CODE 0x00000020
567 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
569 /* From PE spec doc, section 5.2.1 */
570 #define IMAGE_REL_I386_DIR32 0x0006
571 #define IMAGE_REL_I386_REL32 0x0014
574 /* We use myindex to calculate array addresses, rather than
575 simply doing the normal subscript thing. That's because
576 some of the above structs have sizes which are not
577 a whole number of words. GCC rounds their sizes up to a
578 whole number of words, which means that the address calcs
579 arising from using normal C indexing or pointer arithmetic
580 are just plain wrong. Sigh.
583 myindex ( int scale, int index, void* base )
586 ((UChar*)base) + scale * index;
591 printName ( UChar* name, UChar* strtab )
593 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
594 UInt32 strtab_offset = * (UInt32*)(name+4);
595 fprintf ( stderr, "%s", strtab + strtab_offset );
598 for (i = 0; i < 8; i++) {
599 if (name[i] == 0) break;
600 fprintf ( stderr, "%c", name[i] );
607 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
609 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
610 UInt32 strtab_offset = * (UInt32*)(name+4);
611 strncpy ( dst, strtab+strtab_offset, dstSize );
617 if (name[i] == 0) break;
627 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
630 /* If the string is longer than 8 bytes, look in the
631 string table for it -- this will be correctly zero terminated.
633 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
634 UInt32 strtab_offset = * (UInt32*)(name+4);
635 return ((UChar*)strtab) + strtab_offset;
637 /* Otherwise, if shorter than 8 bytes, return the original,
638 which by defn is correctly terminated.
640 if (name[7]==0) return name;
641 /* The annoying case: 8 bytes. Copy into a temporary
642 (which is never freed ...)
646 strncpy(newstr,name,8);
653 /* Just compares the short names (first 8 chars) */
654 static COFF_section *
655 findPEi386SectionCalled ( ObjectCode* oc, char* name )
659 = (COFF_header*)(oc->image);
662 ((UChar*)(oc->image))
663 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
665 for (i = 0; i < hdr->NumberOfSections; i++) {
668 COFF_section* section_i
670 myindex ( sizeof_COFF_section, i, sectab );
671 n1 = (UChar*) &(section_i->Name);
673 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
674 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
675 n1[6]==n2[6] && n1[7]==n2[7])
684 zapTrailingAtSign ( UChar* sym )
687 if (sym[0] == 0) return;
689 while (sym[i] != 0) i++;
692 while (j > 0 && isdigit(sym[j])) j--;
693 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
698 ocVerifyImage_PEi386 ( ObjectCode* oc )
702 COFF_section* sectab;
706 hdr = (COFF_header*)(oc->image);
707 sectab = (COFF_section*) (
708 ((UChar*)(oc->image))
709 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
711 symtab = (COFF_symbol*) (
712 ((UChar*)(oc->image))
713 + hdr->PointerToSymbolTable
715 strtab = ((UChar*)(oc->image))
716 + hdr->PointerToSymbolTable
717 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
719 if (hdr->Machine != 0x14c) {
720 oc->errMsg("Not x86 PEi386");
723 if (hdr->SizeOfOptionalHeader != 0) {
724 oc->errMsg("PEi386 with nonempty optional header");
727 if ( /* (hdr->Characteristics & IMAGE_FILE_RELOCS_STRIPPED) || */
728 (hdr->Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ||
729 (hdr->Characteristics & IMAGE_FILE_DLL) ||
730 (hdr->Characteristics & IMAGE_FILE_SYSTEM) ) {
731 oc->errMsg("Not a PEi386 object file");
734 if ( (hdr->Characteristics & IMAGE_FILE_BYTES_REVERSED_HI) ||
735 !(hdr->Characteristics & IMAGE_FILE_32BIT_MACHINE) ) {
736 oc->errMsg("Invalid PEi386 word size or endiannness");
740 if (!verb) return TRUE;
741 /* No further verification after this point; only debug printing. */
744 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
746 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
748 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
750 fprintf ( stderr, "\n" );
752 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
754 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
756 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
758 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
760 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
762 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
764 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
766 fprintf ( stderr, "\n" );
767 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
768 fprintf ( stderr, "---START of string table---\n");
769 for (i = 4; i < *(UInt32*)strtab; i++) {
771 fprintf ( stderr, "\n"); else
772 fprintf( stderr, "%c", strtab[i] );
774 fprintf ( stderr, "--- END of string table---\n");
776 fprintf ( stderr, "\n" );
777 for (i = 0; i < hdr->NumberOfSections; i++) {
779 COFF_section* sectab_i
781 myindex ( sizeof_COFF_section, i, sectab );
788 printName ( sectab_i->Name, strtab );
797 sectab_i->VirtualSize,
798 sectab_i->VirtualAddress,
799 sectab_i->SizeOfRawData,
800 sectab_i->PointerToRawData,
801 sectab_i->NumberOfRelocations,
802 sectab_i->PointerToRelocations
804 reltab = (COFF_reloc*) (
805 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
807 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
809 COFF_reloc* rel = (COFF_reloc*)
810 myindex ( sizeof_COFF_reloc, j, reltab );
812 " type 0x%-4x vaddr 0x%-8x name `",
814 rel->VirtualAddress );
816 myindex ( sizeof_COFF_symbol, rel->SymbolTableIndex, symtab );
817 printName ( sym->Name, strtab );
818 fprintf ( stderr, "'\n" );
820 fprintf ( stderr, "\n" );
824 fprintf ( stderr, "\n" );
827 COFF_symbol* symtab_i;
828 if (i >= hdr->NumberOfSymbols) break;
829 symtab_i = (COFF_symbol*)
830 myindex ( sizeof_COFF_symbol, i, symtab );
836 printName ( symtab_i->Name, strtab );
845 (Int32)(symtab_i->SectionNumber) - 1,
846 (UInt32)symtab_i->Type,
847 (UInt32)symtab_i->StorageClass,
848 (UInt32)symtab_i->NumberOfAuxSymbols
850 i += symtab_i->NumberOfAuxSymbols;
854 fprintf ( stderr, "\n" );
861 ocGetNames_PEi386 ( ObjectCode* oc )
864 COFF_section* sectab;
872 hdr = (COFF_header*)(oc->image);
873 sectab = (COFF_section*) (
874 ((UChar*)(oc->image))
875 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
877 symtab = (COFF_symbol*) (
878 ((UChar*)(oc->image))
879 + hdr->PointerToSymbolTable
881 strtab = ((UChar*)(oc->image))
882 + hdr->PointerToSymbolTable
883 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
885 /* Copy exported symbols into the ObjectCode. */
888 COFF_symbol* symtab_i;
889 if (i >= hdr->NumberOfSymbols) break;
890 symtab_i = (COFF_symbol*)
891 myindex ( sizeof_COFF_symbol, i, symtab );
893 if (symtab_i->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
894 symtab_i->SectionNumber != IMAGE_SYM_UNDEFINED) {
896 /* This symbol is global and defined, viz, exported */
897 COFF_section* sectabent;
899 sname = cstring_from_COFF_symbol_name (
900 symtab_i->Name, strtab
903 oc->errMsg("Out of memory when copying PEi386 symbol");
907 /* for IMAGE_SYMCLASS_EXTERNAL
908 && !IMAGE_SYM_UNDEFINED,
909 the address of the symbol is:
910 address of relevant section + offset in section
912 sectabent = (COFF_section*)
913 myindex ( sizeof_COFF_section,
914 symtab_i->SectionNumber-1,
916 addr = ((UChar*)(oc->image))
917 + (sectabent->PointerToRawData
919 /* fprintf ( stderr, "addSymbol %p `%s'\n", addr,sname); */
920 if (!addSymbol(oc,sname,addr)) return FALSE;
922 i += symtab_i->NumberOfAuxSymbols;
926 oc->sections = stgMallocBytes( NumberOfSections * sizeof(Section),
927 "ocGetNamesPEi386" );
929 /* Copy section information into the ObjectCode. */
930 for (i = 0; i < hdr->NumberOfSections; i++) {
936 COFF_section* sectab_i
938 myindex ( sizeof_COFF_section, i, sectab );
939 /* fprintf ( stderr, "section name = %s\n", sectab_i->Name ); */
942 /* I'm sure this is the Right Way to do it. However, the
943 alternative of testing the sectab_i->Name field seems to
946 if (sectab_i->Characteristics & IMAGE_SCN_CNT_CODE ||
947 sectab_i->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
948 kind = SECTIONKIND_CODE_OR_RODATA;
951 if (0==strcmp(".text",sectab_i->Name))
952 kind = SECTIONKIND_CODE_OR_RODATA;
953 if (0==strcmp(".data",sectab_i->Name) ||
954 0==strcmp(".bss",sectab_i->Name))
955 kind = SECTIONKIND_RWDATA;
957 start = ((UChar*)(oc->image))
958 + sectab_i->PointerToRawData;
960 + sectab_i->SizeOfRawData - 1;
962 if (kind != SECTIONKIND_OTHER) {
963 addSection ( oc, start, end, kind );
965 fprintf ( stderr, "unknown section name = `%s'\n",
967 oc->errMsg("Unknown PEi386 section name");
977 ocResolve_PEi386 ( ObjectCode* oc, int verb )
980 COFF_section* sectab;
989 char symbol[1000]; // ToDo
991 hdr = (COFF_header*)(oc->image);
992 sectab = (COFF_section*) (
993 ((UChar*)(oc->image))
994 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
996 symtab = (COFF_symbol*) (
997 ((UChar*)(oc->image))
998 + hdr->PointerToSymbolTable
1000 strtab = ((UChar*)(oc->image))
1001 + hdr->PointerToSymbolTable
1002 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
1004 for (i = 0; i < hdr->NumberOfSections; i++) {
1005 COFF_section* sectab_i
1007 myindex ( sizeof_COFF_section, i, sectab );
1010 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
1012 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
1014 COFF_reloc* reltab_j
1016 myindex ( sizeof_COFF_reloc, j, reltab );
1018 /* the location to patch */
1020 ((UChar*)(oc->image))
1021 + (sectab_i->PointerToRawData
1022 + reltab_j->VirtualAddress)
1024 /* the existing contents of pP */
1026 /* the symbol to connect to */
1027 sym = (COFF_symbol*)
1028 myindex ( sizeof_COFF_symbol,
1029 reltab_j->SymbolTableIndex, symtab );
1032 "reloc sec %2d num %3d: type 0x%-4x "
1033 "vaddr 0x%-8x name `",
1035 (UInt32)reltab_j->Type,
1036 reltab_j->VirtualAddress );
1037 printName ( sym->Name, strtab );
1038 fprintf ( stderr, "'\n" );
1041 if (sym->StorageClass == IMAGE_SYM_CLASS_STATIC) {
1042 COFF_section* section_sym
1043 = findPEi386SectionCalled ( oc, sym->Name );
1045 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
1046 oc->errMsg("Can't find abovementioned PEi386 section");
1049 S = ((UInt32)(oc->image))
1050 + (section_sym->PointerToRawData
1053 copyName ( sym->Name, strtab, symbol, 1000 );
1054 zapTrailingAtSign ( symbol );
1055 S = (UInt32) ocLookupSym ( oc, symbol );
1057 S = (UInt32)(oc->clientLookup ( symbol ));
1059 belch("%s: unresolvable reference to `%s'", oc->fileName, symbol);
1064 switch (reltab_j->Type) {
1065 case IMAGE_REL_I386_DIR32:
1068 case IMAGE_REL_I386_REL32:
1069 /* Tricky. We have to insert a displacement at
1070 pP which, when added to the PC for the _next_
1071 insn, gives the address of the target (S).
1072 Problem is to know the address of the next insn
1073 when we only know pP. We assume that this
1074 literal field is always the last in the insn,
1075 so that the address of the next insn is pP+4
1076 -- hence the constant 4.
1077 Also I don't know if A should be added, but so
1078 far it has always been zero.
1081 *pP = S - ((UInt32)pP) - 4;
1085 "unhandled PEi386 relocation type %d\n",
1087 oc->errMsg("unhandled PEi386 relocation type");
1097 #endif /* defined(cygwin32_TARGET_OS) */
1100 /* --------------------------------------------------------------------------
1101 * ELF specifics (Linux, Solaris)
1102 * ------------------------------------------------------------------------*/
1104 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
1112 findElfSection ( void* objImage, Elf32_Word sh_type )
1115 char* ehdrC = (char*)objImage;
1116 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1117 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1119 for (i = 0; i < ehdr->e_shnum; i++) {
1120 if (shdr[i].sh_type == sh_type &&
1121 i != ehdr->e_shstrndx) {
1122 ptr = ehdrC + shdr[i].sh_offset;
1131 ocVerifyImage_ELF ( ObjectCode* oc )
1135 int i, j, nent, nstrtab, nsymtabs;
1139 char* ehdrC = (char*)(oc->image);
1140 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1142 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1143 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1144 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1145 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1146 belch("ocVerifyImage_ELF: not an ELF header");
1149 IF_DEBUG(linker,belch( "Is an ELF header" ));
1151 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1152 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1156 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1158 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1159 IF_DEBUG(linker,belch( "Is little-endian" ));
1161 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1162 IF_DEBUG(linker,belch( "Is big-endian" ));
1164 belch("ocVerifyImage_ELF: unknown endiannness");
1168 if (ehdr->e_type != ET_REL) {
1169 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1172 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1174 IF_DEBUG(linker,belch( "Architecture is " ));
1175 switch (ehdr->e_machine) {
1176 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1177 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1178 default: IF_DEBUG(linker,belch( "unknown" ));
1179 belch("ocVerifyImage_ELF: unknown architecture");
1183 IF_DEBUG(linker,belch(
1184 "\nSection header table: start %d, n_entries %d, ent_size %d",
1185 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1187 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1189 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1191 if (ehdr->e_shstrndx == SHN_UNDEF) {
1192 belch("ocVerifyImage_ELF: no section header string table");
1195 IF_DEBUG(linker,belch( "Section header string table is section %d",
1197 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1200 for (i = 0; i < ehdr->e_shnum; i++) {
1201 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1202 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", shdr[i].sh_type ));
1203 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", shdr[i].sh_size ));
1204 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", shdr[i].sh_offset ));
1205 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1206 ehdrC + shdr[i].sh_offset,
1207 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1209 if (shdr[i].sh_type == SHT_REL) {
1210 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1211 } else if (shdr[i].sh_type == SHT_RELA) {
1212 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1214 IF_DEBUG(linker,fprintf(stderr," "));
1217 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1221 IF_DEBUG(linker,belch( "\nString tables" ));
1224 for (i = 0; i < ehdr->e_shnum; i++) {
1225 if (shdr[i].sh_type == SHT_STRTAB &&
1226 i != ehdr->e_shstrndx) {
1227 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1228 strtab = ehdrC + shdr[i].sh_offset;
1233 belch("ocVerifyImage_ELF: no string tables, or too many");
1238 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1239 for (i = 0; i < ehdr->e_shnum; i++) {
1240 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1241 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1243 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1244 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1245 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1247 shdr[i].sh_size % sizeof(Elf32_Sym)
1249 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1250 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1253 for (j = 0; j < nent; j++) {
1254 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1255 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1256 (int)stab[j].st_shndx,
1257 (int)stab[j].st_size,
1258 (char*)stab[j].st_value ));
1260 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1261 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1262 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1263 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1264 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1265 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1266 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1267 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1269 IF_DEBUG(linker,fprintf(stderr, " " ));
1271 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1272 switch (ELF32_ST_BIND(stab[j].st_info)) {
1273 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1274 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1275 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1276 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1278 IF_DEBUG(linker,fprintf(stderr, " " ));
1280 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1284 if (nsymtabs == 0) {
1285 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1294 ocGetNames_ELF ( ObjectCode* oc )
1299 char* ehdrC = (char*)(oc->image);
1300 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1301 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1302 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1303 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1306 belch("ocGetNames_ELF: no strtab");
1311 oc->sections = stgMallocBytes( ehdr->e_shnum * sizeof(Section),
1313 oc->n_sections = ehdr->e_shnum;
1315 for (i = 0; i < ehdr->e_shnum; i++) {
1317 /* make a section entry for relevant sections */
1318 SectionKind kind = SECTIONKIND_OTHER;
1319 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1320 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1321 kind = SECTIONKIND_RWDATA;
1322 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1323 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1324 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1325 kind = SECTIONKIND_CODE_OR_RODATA;
1327 /* fill in the section info */
1328 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1329 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1330 oc->sections[i].kind = kind;
1332 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1334 /* copy stuff into this module's object symbol table */
1335 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1336 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1337 oc->symbols = malloc(nent * sizeof(SymbolVal));
1338 for (j = 0; j < nent; j++) {
1339 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL /* ||
1340 ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL */
1342 /* and not an undefined symbol */
1343 && stab[j].st_shndx != SHN_UNDEF
1345 /* and it's a not a section or string table or anything silly */
1346 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1347 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1348 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE )
1350 char* nm = strtab + stab[j].st_name;
1352 + shdr[ stab[j].st_shndx ].sh_offset
1356 IF_DEBUG(linker,belch( "addOTabName: %10p %s %s",
1357 ad, oc->fileName, nm ));
1358 oc->symbols[j].lbl = nm;
1359 oc->symbols[j].addr = ad;
1360 insertStrHashTable(symhash, nm, &(oc->symbols[j]));
1363 IF_DEBUG(linker,belch( "skipping `%s'", strtab +
1365 oc->symbols[j].lbl = NULL;
1366 oc->symbols[j].addr = NULL;
1376 ocResolve_ELF ( ObjectCode* oc )
1378 char *strtab, *symbol;
1380 Elf32_Sym* stab = NULL;
1381 char* ehdrC = (char*)(oc->image);
1382 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1383 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1386 /* first find "the" symbol table */
1387 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1389 /* also go find the string table */
1390 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1392 if (stab == NULL || strtab == NULL) {
1393 belch("ocResolve_ELF: can't find string or symbol table");
1397 for (i = 0; i < ehdr->e_shnum; i++) {
1398 if (shdr[i].sh_type == SHT_REL ) {
1399 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[i].sh_offset);
1400 int nent = shdr[i].sh_size / sizeof(Elf32_Rel);
1401 int target_shndx = shdr[i].sh_info;
1402 int symtab_shndx = shdr[i].sh_link;
1403 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1404 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1405 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1406 target_shndx, symtab_shndx ));
1407 for (j = 0; j < nent; j++) {
1408 Elf32_Addr offset = rtab[j].r_offset;
1409 Elf32_Word info = rtab[j].r_info;
1411 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1412 Elf32_Word* pP = (Elf32_Word*)P;
1416 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p) ",
1417 j, (void*)offset, (void*)info ));
1419 IF_DEBUG(linker,belch( " ZERO" ));
1422 /* First see if it is a nameless local symbol. */
1423 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1424 symbol = "(noname)";
1425 S = (Elf32_Addr)(ehdrC
1426 + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1427 + stab[ELF32_R_SYM(info)].st_value
1430 /* No? Should be in the symbol table then. */
1431 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1432 (void *)S = lookupSymbol( symbol );
1435 barf("ocResolve_ELF: %s: unknown symbol `%s'",
1436 oc->fileName, symbol);
1438 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1440 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n", (void*)P, (void*)S, (void*)A ));
1441 switch (ELF32_R_TYPE(info)) {
1442 case R_386_32: *pP = S + A; break;
1443 case R_386_PC32: *pP = S + A - P; break;
1444 default: fprintf(stderr,
1445 "unhandled ELF relocation type %d",
1446 ELF32_R_TYPE(info));
1447 belch("ocResolve_ELF: unhandled ELF relocation type");
1454 if (shdr[i].sh_type == SHT_RELA) {
1455 belch("ocResolve_ELF: RelA style reloc table -- not yet done");
1464 #endif /* defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) */
1466 /* -----------------------------------------------------------------------------
1467 * Look up an address to discover whether it is in text or data space.
1469 * Used by the garbage collector when walking the stack.
1470 * -------------------------------------------------------------------------- */
1472 static __inline__ SectionKind
1473 lookupSection ( void* addr )
1478 for ( oc = objects; oc; oc = oc->next ) {
1479 for (i = 0; i < oc->n_sections; i++) {
1480 if (oc->sections[i].start <= addr
1481 && addr <= oc->sections[i].end)
1482 return oc->sections[i].kind;
1485 return SECTIONKIND_OTHER;
1489 is_dynamically_loaded_code_or_rodata_ptr ( char* p )
1491 SectionKind sk = lookupSection(p);
1492 ASSERT (sk != SECTIONKIND_NOINFOAVAIL);
1493 return (sk == SECTIONKIND_CODE_OR_RODATA);
1498 is_dynamically_loaded_rwdata_ptr ( char* p )
1500 SectionKind sk = lookupSection(p);
1501 ASSERT (sk != SECTIONKIND_NOINFOAVAIL);
1502 return (sk == SECTIONKIND_RWDATA);
1507 is_not_dynamically_loaded_ptr ( char* p )
1509 SectionKind sk = lookupSection(p);
1510 ASSERT (sk != SECTIONKIND_NOINFOAVAIL);
1511 return (sk == SECTIONKIND_OTHER);