1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.3 2000/12/12 12:19:57 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 /* A bucket in the symbol hash-table. Primarily, maps symbol names to
25 * absolute addresses. All symbols from a given module are linked
26 * together, so they can be freed at the same time. There's also a
27 * bucket link field for the hash table.
29 typedef struct _SymbolVal {
34 typedef enum { OBJECT_LOADED, OBJECT_RESOLVED } OStatus;
36 /* Indication of section kinds for loaded objects. Needed by
37 the GC for deciding whether or not a pointer on the stack
40 typedef enum { SECTIONKIND_CODE_OR_RODATA,
43 SECTIONKIND_NOINFOAVAIL }
46 typedef struct { void* start; void* end; SectionKind kind; }
49 /* Top-level structure for an object module. One of these is allocated
50 * for each object file in use.
52 typedef struct _ObjectCode {
56 char* formatName; /* eg "ELF32", "DLL", "COFF", etc. */
61 /* ptr to malloc'd lump of memory holding the obj file */
64 /* The section-kind entries for this object module. Dynamically expands. */
68 /* Allow a chain of these things */
69 struct _ObjectCode * next;
73 /* Hash table mapping symbol names to Symbol */
74 /*Str*/HashTable *symhash;
76 /* List of currently loaded objects */
79 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
80 static int ocVerifyImage_ELF ( ObjectCode* oc );
81 static int ocGetNames_ELF ( ObjectCode* oc );
82 static int ocResolve_ELF ( ObjectCode* oc );
83 #elif defined(cygwin32_TARGET_OS)
84 static int ocVerifyImage_PEi386 ( ObjectCode* oc );
85 static int ocGetNames_PEi386 ( ObjectCode* oc );
86 static int ocResolve_PEi386 ( ObjectCode* oc );
89 /* -----------------------------------------------------------------------------
90 * Built-in symbols from the RTS
102 Sym(stg_gc_unbx_r1) \
107 SymX(stg_update_PAP) \
108 SymX(stg_ap_2_upd_info) \
109 SymX(stg_ap_3_upd_info) \
110 SymX(stg_ap_4_upd_info) \
111 SymX(stg_ap_5_upd_info) \
112 SymX(stg_ap_6_upd_info) \
113 SymX(stg_ap_7_upd_info) \
114 SymX(stg_ap_8_upd_info) \
115 SymX(stg_sel_0_upd_info) \
116 SymX(stg_sel_1_upd_info) \
117 SymX(stg_sel_2_upd_info) \
118 SymX(stg_sel_3_upd_info) \
119 SymX(stg_sel_4_upd_info) \
120 SymX(stg_sel_5_upd_info) \
121 SymX(stg_sel_6_upd_info) \
122 SymX(stg_sel_7_upd_info) \
123 SymX(stg_sel_8_upd_info) \
124 SymX(stg_sel_9_upd_info) \
125 SymX(stg_sel_10_upd_info) \
126 SymX(stg_sel_11_upd_info) \
127 SymX(stg_sel_12_upd_info) \
128 SymX(stg_upd_frame_info) \
129 SymX(stg_seq_frame_info) \
130 SymX(stg_CAF_BLACKHOLE_info) \
131 SymX(stg_IND_STATIC_info) \
132 SymX(stg_EMPTY_MVAR_info) \
133 SymX(stg_MUT_ARR_PTRS_FROZEN_info) \
134 SymX(stg_CHARLIKE_closure) \
135 SymX(stg_INTLIKE_closure) \
136 SymX(stg_CAF_UNENTERED_entry) \
138 SymX(putMVarzh_fast) \
139 SymX(newMVarzh_fast) \
140 SymX(takeMVarzh_fast) \
141 SymX(tryTakeMVarzh_fast) \
146 SymX(killThreadzh_fast) \
147 SymX(waitReadzh_fast) \
148 SymX(waitWritezh_fast) \
149 SymX(suspendThread) \
151 SymX(stackOverflow) \
152 SymX(int2Integerzh_fast) \
154 SymX(mkForeignObjzh_fast) \
155 SymX(__encodeDouble) \
156 SymX(decodeDoublezh_fast) \
158 SymX(isDoubleInfinite) \
159 SymX(isDoubleDenormalized) \
160 SymX(isDoubleNegativeZero) \
161 SymX(__encodeFloat) \
162 SymX(decodeFloatzh_fast) \
164 SymX(isFloatInfinite) \
165 SymX(isFloatDenormalized) \
166 SymX(isFloatNegativeZero) \
167 SymX(__int_encodeFloat) \
168 SymX(__int_encodeDouble) \
169 SymX(__gmpz_cmp_si) \
172 SymX(gcdIntegerzh_fast) \
173 SymX(newArrayzh_fast) \
174 SymX(unsafeThawArrayzh_fast) \
175 SymX(newByteArrayzh_fast) \
176 SymX(newMutVarzh_fast) \
177 SymX(quotRemIntegerzh_fast) \
178 SymX(quotIntegerzh_fast) \
179 SymX(remIntegerzh_fast) \
180 SymX(divExactIntegerzh_fast) \
181 SymX(divModIntegerzh_fast) \
182 SymX(timesIntegerzh_fast) \
183 SymX(minusIntegerzh_fast) \
184 SymX(plusIntegerzh_fast) \
185 SymX(mkWeakzh_fast) \
188 SymX(resetNonBlockingFd) \
190 SymX(stable_ptr_table) \
191 SymX(shutdownHaskellAndExit) \
192 Sym(stg_enterStackTop) \
193 Sym(stg_yield_to_Hugs) \
196 SymX(blockAsyncExceptionszh_fast) \
197 SymX(unblockAsyncExceptionszh_fast) \
200 /* entirely bogus claims about types of these symbols */
201 #define Sym(vvv) extern void (vvv);
202 #define SymX(vvv) /**/
207 #ifdef LEADING_UNDERSCORE
208 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
210 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
213 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
215 #define SymX(vvv) Sym(vvv)
217 static SymbolVal rtsSyms[] = {
219 { 0, 0 } /* sentinel */
222 /* -----------------------------------------------------------------------------
223 * initialize the object linker
225 static void *dl_prog_handle;
232 symhash = allocStrHashTable();
234 /* populate the symbol table with stuff from the RTS */
235 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
236 insertStrHashTable(symhash, sym->lbl, sym);
239 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
242 /* -----------------------------------------------------------------------------
243 * lookup a symbol in the hash table
246 lookupSymbol( char *lbl )
249 val = lookupStrHashTable(symhash, lbl);
252 return dlsym(dl_prog_handle, lbl);
258 /* -----------------------------------------------------------------------------
259 * Load an obj (populate the global symbol table, but don't resolve yet)
261 * Returns: 1 if ok, 0 on error.
264 loadObj( char *path )
272 /* assert that we haven't already loaded this object */
275 for (o = objects; o; o = o->next)
276 ASSERT(strcmp(o->fileName, path));
280 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
282 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
283 oc->formatName = "ELF";
284 # elif defined(cygwin32_TARGET_OS)
285 oc->formatName = "PEi386";
288 barf("loadObj: not implemented on this platform");
292 if (r == -1) { return 0; }
295 oc->fileSize = st.st_size;
296 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
300 /* chain it onto the list of objects */
304 /* load the image into memory */
305 f = fopen(path, "rb");
307 barf("loadObj: can't read `%s'", path);
309 n = fread ( oc->image, 1, oc->fileSize, f );
310 if (n != oc->fileSize) {
312 barf("loadObj: error whilst reading `%s'", path);
315 /* verify the in-memory image */
316 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
317 r = ocVerifyImage_ELF ( oc );
318 # elif defined(cygwin32_TARGET_OS)
319 r = ocVerifyImage_PEi386 ( oc );
321 barf("loadObj: no verify method");
323 if (!r) { return r; }
325 /* build the symbol list for this image */
326 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
327 r = ocGetNames_ELF ( oc );
328 # elif defined(cygwin32_TARGET_OS)
329 r = ocGetNames_PEi386 ( oc );
331 barf("loadObj: no getNames method");
333 if (!r) { return r; }
335 /* loaded, but not resolved yet */
336 oc->status = OBJECT_LOADED;
341 /* -----------------------------------------------------------------------------
342 * resolve all the currently unlinked objects in memory
344 * Returns: 1 if ok, 0 on error.
352 for (oc = objects; oc; oc = oc->next) {
353 if (oc->status != OBJECT_RESOLVED) {
354 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
355 r = ocResolve_ELF ( oc );
356 # elif defined(cygwin32_TARGET_OS)
357 r = ocResolve_PEi386 ( oc );
359 barf("link: not implemented on this platform");
361 if (!r) { return r; }
362 oc->status = OBJECT_RESOLVED;
368 /* -----------------------------------------------------------------------------
369 * delete an object from the pool
372 unloadObj( char *path )
376 for (oc = objects; oc; oc = oc->next) {
377 if (!strcmp(oc->fileName,path)) {
379 /* Remove all the mappings for the symbols within this
384 for (s = oc->symbols; s < oc->symbols + oc->n_symbols; s++) {
385 removeStrHashTable(symhash, s->lbl, NULL);
389 /* We're going to leave this in place, in case there are
390 any pointers from the heap into it: */
391 /* free(oc->image); */
399 belch("unloadObj: can't find `%s' to unload", path);
403 /* --------------------------------------------------------------------------
404 * PEi386 specifics (cygwin32)
405 * ------------------------------------------------------------------------*/
407 /* The information for this linker comes from
408 Microsoft Portable Executable
409 and Common Object File Format Specification
410 revision 5.1 January 1998
411 which SimonM says comes from the MS Developer Network CDs.
415 #if defined(cygwin32_TARGET_OS)
419 typedef unsigned char UChar;
420 typedef unsigned short UInt16;
421 typedef unsigned int UInt32;
428 UInt16 NumberOfSections;
429 UInt32 TimeDateStamp;
430 UInt32 PointerToSymbolTable;
431 UInt32 NumberOfSymbols;
432 UInt16 SizeOfOptionalHeader;
433 UInt16 Characteristics;
437 #define sizeof_COFF_header 20
444 UInt32 VirtualAddress;
445 UInt32 SizeOfRawData;
446 UInt32 PointerToRawData;
447 UInt32 PointerToRelocations;
448 UInt32 PointerToLinenumbers;
449 UInt16 NumberOfRelocations;
450 UInt16 NumberOfLineNumbers;
451 UInt32 Characteristics;
455 #define sizeof_COFF_section 40
462 UInt16 SectionNumber;
465 UChar NumberOfAuxSymbols;
469 #define sizeof_COFF_symbol 18
474 UInt32 VirtualAddress;
475 UInt32 SymbolTableIndex;
480 #define sizeof_COFF_reloc 10
483 /* From PE spec doc, section 3.3.2 */
484 #define IMAGE_FILE_RELOCS_STRIPPED 0x0001
485 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
486 #define IMAGE_FILE_DLL 0x2000
487 #define IMAGE_FILE_SYSTEM 0x1000
488 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
489 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
490 #define IMAGE_FILE_32BIT_MACHINE 0x0100
492 /* From PE spec doc, section 5.4.2 and 5.4.4 */
493 #define IMAGE_SYM_CLASS_EXTERNAL 2
494 #define IMAGE_SYM_CLASS_STATIC 3
495 #define IMAGE_SYM_UNDEFINED 0
497 /* From PE spec doc, section 4.1 */
498 #define IMAGE_SCN_CNT_CODE 0x00000020
499 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
501 /* From PE spec doc, section 5.2.1 */
502 #define IMAGE_REL_I386_DIR32 0x0006
503 #define IMAGE_REL_I386_REL32 0x0014
506 /* We use myindex to calculate array addresses, rather than
507 simply doing the normal subscript thing. That's because
508 some of the above structs have sizes which are not
509 a whole number of words. GCC rounds their sizes up to a
510 whole number of words, which means that the address calcs
511 arising from using normal C indexing or pointer arithmetic
512 are just plain wrong. Sigh.
515 myindex ( int scale, int index, void* base )
518 ((UChar*)base) + scale * index;
523 printName ( UChar* name, UChar* strtab )
525 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
526 UInt32 strtab_offset = * (UInt32*)(name+4);
527 fprintf ( stderr, "%s", strtab + strtab_offset );
530 for (i = 0; i < 8; i++) {
531 if (name[i] == 0) break;
532 fprintf ( stderr, "%c", name[i] );
539 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
541 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
542 UInt32 strtab_offset = * (UInt32*)(name+4);
543 strncpy ( dst, strtab+strtab_offset, dstSize );
549 if (name[i] == 0) break;
559 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
562 /* If the string is longer than 8 bytes, look in the
563 string table for it -- this will be correctly zero terminated.
565 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
566 UInt32 strtab_offset = * (UInt32*)(name+4);
567 return ((UChar*)strtab) + strtab_offset;
569 /* Otherwise, if shorter than 8 bytes, return the original,
570 which by defn is correctly terminated.
572 if (name[7]==0) return name;
573 /* The annoying case: 8 bytes. Copy into a temporary
574 (which is never freed ...)
578 strncpy(newstr,name,8);
585 /* Just compares the short names (first 8 chars) */
586 static COFF_section *
587 findPEi386SectionCalled ( ObjectCode* oc, char* name )
591 = (COFF_header*)(oc->image);
594 ((UChar*)(oc->image))
595 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
597 for (i = 0; i < hdr->NumberOfSections; i++) {
600 COFF_section* section_i
602 myindex ( sizeof_COFF_section, i, sectab );
603 n1 = (UChar*) &(section_i->Name);
605 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
606 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
607 n1[6]==n2[6] && n1[7]==n2[7])
616 zapTrailingAtSign ( UChar* sym )
619 if (sym[0] == 0) return;
621 while (sym[i] != 0) i++;
624 while (j > 0 && isdigit(sym[j])) j--;
625 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
630 ocVerifyImage_PEi386 ( ObjectCode* oc )
634 COFF_section* sectab;
638 hdr = (COFF_header*)(oc->image);
639 sectab = (COFF_section*) (
640 ((UChar*)(oc->image))
641 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
643 symtab = (COFF_symbol*) (
644 ((UChar*)(oc->image))
645 + hdr->PointerToSymbolTable
647 strtab = ((UChar*)(oc->image))
648 + hdr->PointerToSymbolTable
649 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
651 if (hdr->Machine != 0x14c) {
652 oc->errMsg("Not x86 PEi386");
655 if (hdr->SizeOfOptionalHeader != 0) {
656 oc->errMsg("PEi386 with nonempty optional header");
659 if ( /* (hdr->Characteristics & IMAGE_FILE_RELOCS_STRIPPED) || */
660 (hdr->Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ||
661 (hdr->Characteristics & IMAGE_FILE_DLL) ||
662 (hdr->Characteristics & IMAGE_FILE_SYSTEM) ) {
663 oc->errMsg("Not a PEi386 object file");
666 if ( (hdr->Characteristics & IMAGE_FILE_BYTES_REVERSED_HI) ||
667 !(hdr->Characteristics & IMAGE_FILE_32BIT_MACHINE) ) {
668 oc->errMsg("Invalid PEi386 word size or endiannness");
672 if (!verb) return TRUE;
673 /* No further verification after this point; only debug printing. */
676 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
678 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
680 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
682 fprintf ( stderr, "\n" );
684 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
686 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
688 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
690 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
692 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
694 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
696 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
698 fprintf ( stderr, "\n" );
699 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
700 fprintf ( stderr, "---START of string table---\n");
701 for (i = 4; i < *(UInt32*)strtab; i++) {
703 fprintf ( stderr, "\n"); else
704 fprintf( stderr, "%c", strtab[i] );
706 fprintf ( stderr, "--- END of string table---\n");
708 fprintf ( stderr, "\n" );
709 for (i = 0; i < hdr->NumberOfSections; i++) {
711 COFF_section* sectab_i
713 myindex ( sizeof_COFF_section, i, sectab );
720 printName ( sectab_i->Name, strtab );
729 sectab_i->VirtualSize,
730 sectab_i->VirtualAddress,
731 sectab_i->SizeOfRawData,
732 sectab_i->PointerToRawData,
733 sectab_i->NumberOfRelocations,
734 sectab_i->PointerToRelocations
736 reltab = (COFF_reloc*) (
737 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
739 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
741 COFF_reloc* rel = (COFF_reloc*)
742 myindex ( sizeof_COFF_reloc, j, reltab );
744 " type 0x%-4x vaddr 0x%-8x name `",
746 rel->VirtualAddress );
748 myindex ( sizeof_COFF_symbol, rel->SymbolTableIndex, symtab );
749 printName ( sym->Name, strtab );
750 fprintf ( stderr, "'\n" );
752 fprintf ( stderr, "\n" );
756 fprintf ( stderr, "\n" );
759 COFF_symbol* symtab_i;
760 if (i >= hdr->NumberOfSymbols) break;
761 symtab_i = (COFF_symbol*)
762 myindex ( sizeof_COFF_symbol, i, symtab );
768 printName ( symtab_i->Name, strtab );
777 (Int32)(symtab_i->SectionNumber) - 1,
778 (UInt32)symtab_i->Type,
779 (UInt32)symtab_i->StorageClass,
780 (UInt32)symtab_i->NumberOfAuxSymbols
782 i += symtab_i->NumberOfAuxSymbols;
786 fprintf ( stderr, "\n" );
793 ocGetNames_PEi386 ( ObjectCode* oc )
796 COFF_section* sectab;
804 hdr = (COFF_header*)(oc->image);
805 sectab = (COFF_section*) (
806 ((UChar*)(oc->image))
807 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
809 symtab = (COFF_symbol*) (
810 ((UChar*)(oc->image))
811 + hdr->PointerToSymbolTable
813 strtab = ((UChar*)(oc->image))
814 + hdr->PointerToSymbolTable
815 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
817 /* Copy exported symbols into the ObjectCode. */
820 COFF_symbol* symtab_i;
821 if (i >= hdr->NumberOfSymbols) break;
822 symtab_i = (COFF_symbol*)
823 myindex ( sizeof_COFF_symbol, i, symtab );
825 if (symtab_i->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
826 symtab_i->SectionNumber != IMAGE_SYM_UNDEFINED) {
828 /* This symbol is global and defined, viz, exported */
829 COFF_section* sectabent;
831 sname = cstring_from_COFF_symbol_name (
832 symtab_i->Name, strtab
835 oc->errMsg("Out of memory when copying PEi386 symbol");
839 /* for IMAGE_SYMCLASS_EXTERNAL
840 && !IMAGE_SYM_UNDEFINED,
841 the address of the symbol is:
842 address of relevant section + offset in section
844 sectabent = (COFF_section*)
845 myindex ( sizeof_COFF_section,
846 symtab_i->SectionNumber-1,
848 addr = ((UChar*)(oc->image))
849 + (sectabent->PointerToRawData
851 /* fprintf ( stderr, "addSymbol %p `%s'\n", addr,sname); */
852 if (!addSymbol(oc,sname,addr)) return FALSE;
854 i += symtab_i->NumberOfAuxSymbols;
858 oc->sections = stgMallocBytes( NumberOfSections * sizeof(Section),
859 "ocGetNamesPEi386" );
861 /* Copy section information into the ObjectCode. */
862 for (i = 0; i < hdr->NumberOfSections; i++) {
868 COFF_section* sectab_i
870 myindex ( sizeof_COFF_section, i, sectab );
871 /* fprintf ( stderr, "section name = %s\n", sectab_i->Name ); */
874 /* I'm sure this is the Right Way to do it. However, the
875 alternative of testing the sectab_i->Name field seems to
878 if (sectab_i->Characteristics & IMAGE_SCN_CNT_CODE ||
879 sectab_i->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
880 kind = SECTIONKIND_CODE_OR_RODATA;
883 if (0==strcmp(".text",sectab_i->Name))
884 kind = SECTIONKIND_CODE_OR_RODATA;
885 if (0==strcmp(".data",sectab_i->Name) ||
886 0==strcmp(".bss",sectab_i->Name))
887 kind = SECTIONKIND_RWDATA;
889 start = ((UChar*)(oc->image))
890 + sectab_i->PointerToRawData;
892 + sectab_i->SizeOfRawData - 1;
894 if (kind != SECTIONKIND_OTHER) {
895 addSection ( oc, start, end, kind );
897 fprintf ( stderr, "unknown section name = `%s'\n",
899 oc->errMsg("Unknown PEi386 section name");
909 ocResolve_PEi386 ( ObjectCode* oc, int verb )
912 COFF_section* sectab;
921 char symbol[1000]; // ToDo
923 hdr = (COFF_header*)(oc->image);
924 sectab = (COFF_section*) (
925 ((UChar*)(oc->image))
926 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
928 symtab = (COFF_symbol*) (
929 ((UChar*)(oc->image))
930 + hdr->PointerToSymbolTable
932 strtab = ((UChar*)(oc->image))
933 + hdr->PointerToSymbolTable
934 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
936 for (i = 0; i < hdr->NumberOfSections; i++) {
937 COFF_section* sectab_i
939 myindex ( sizeof_COFF_section, i, sectab );
942 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
944 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
948 myindex ( sizeof_COFF_reloc, j, reltab );
950 /* the location to patch */
952 ((UChar*)(oc->image))
953 + (sectab_i->PointerToRawData
954 + reltab_j->VirtualAddress)
956 /* the existing contents of pP */
958 /* the symbol to connect to */
960 myindex ( sizeof_COFF_symbol,
961 reltab_j->SymbolTableIndex, symtab );
964 "reloc sec %2d num %3d: type 0x%-4x "
965 "vaddr 0x%-8x name `",
967 (UInt32)reltab_j->Type,
968 reltab_j->VirtualAddress );
969 printName ( sym->Name, strtab );
970 fprintf ( stderr, "'\n" );
973 if (sym->StorageClass == IMAGE_SYM_CLASS_STATIC) {
974 COFF_section* section_sym
975 = findPEi386SectionCalled ( oc, sym->Name );
977 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
978 oc->errMsg("Can't find abovementioned PEi386 section");
981 S = ((UInt32)(oc->image))
982 + (section_sym->PointerToRawData
985 copyName ( sym->Name, strtab, symbol, 1000 );
986 zapTrailingAtSign ( symbol );
987 S = (UInt32) ocLookupSym ( oc, symbol );
989 S = (UInt32)(oc->clientLookup ( symbol ));
991 belch("%s: unresolvable reference to `%s'", oc->fileName, symbol);
996 switch (reltab_j->Type) {
997 case IMAGE_REL_I386_DIR32:
1000 case IMAGE_REL_I386_REL32:
1001 /* Tricky. We have to insert a displacement at
1002 pP which, when added to the PC for the _next_
1003 insn, gives the address of the target (S).
1004 Problem is to know the address of the next insn
1005 when we only know pP. We assume that this
1006 literal field is always the last in the insn,
1007 so that the address of the next insn is pP+4
1008 -- hence the constant 4.
1009 Also I don't know if A should be added, but so
1010 far it has always been zero.
1013 *pP = S - ((UInt32)pP) - 4;
1017 "unhandled PEi386 relocation type %d\n",
1019 oc->errMsg("unhandled PEi386 relocation type");
1029 #endif /* defined(cygwin32_TARGET_OS) */
1032 /* --------------------------------------------------------------------------
1033 * ELF specifics (Linux, Solaris)
1034 * ------------------------------------------------------------------------*/
1036 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
1044 findElfSection ( void* objImage, Elf32_Word sh_type )
1047 char* ehdrC = (char*)objImage;
1048 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1049 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1051 for (i = 0; i < ehdr->e_shnum; i++) {
1052 if (shdr[i].sh_type == sh_type &&
1053 i != ehdr->e_shstrndx) {
1054 ptr = ehdrC + shdr[i].sh_offset;
1063 ocVerifyImage_ELF ( ObjectCode* oc )
1067 int i, j, nent, nstrtab, nsymtabs;
1071 char* ehdrC = (char*)(oc->image);
1072 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1074 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1075 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1076 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1077 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1078 belch("ocVerifyImage_ELF: not an ELF header");
1081 IF_DEBUG(linker,belch( "Is an ELF header" ));
1083 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1084 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1088 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1090 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1091 IF_DEBUG(linker,belch( "Is little-endian" ));
1093 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1094 IF_DEBUG(linker,belch( "Is big-endian" ));
1096 belch("ocVerifyImage_ELF: unknown endiannness");
1100 if (ehdr->e_type != ET_REL) {
1101 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1104 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1106 IF_DEBUG(linker,belch( "Architecture is " ));
1107 switch (ehdr->e_machine) {
1108 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1109 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1110 default: IF_DEBUG(linker,belch( "unknown" ));
1111 belch("ocVerifyImage_ELF: unknown architecture");
1115 IF_DEBUG(linker,belch(
1116 "\nSection header table: start %d, n_entries %d, ent_size %d",
1117 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1119 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1121 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1123 if (ehdr->e_shstrndx == SHN_UNDEF) {
1124 belch("ocVerifyImage_ELF: no section header string table");
1127 IF_DEBUG(linker,belch( "Section header string table is section %d",
1129 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1132 for (i = 0; i < ehdr->e_shnum; i++) {
1133 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1134 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", shdr[i].sh_type ));
1135 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", shdr[i].sh_size ));
1136 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", shdr[i].sh_offset ));
1137 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1138 ehdrC + shdr[i].sh_offset,
1139 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1141 if (shdr[i].sh_type == SHT_REL) {
1142 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1143 } else if (shdr[i].sh_type == SHT_RELA) {
1144 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1146 IF_DEBUG(linker,fprintf(stderr," "));
1149 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1153 IF_DEBUG(linker,belch( "\nString tables" ));
1156 for (i = 0; i < ehdr->e_shnum; i++) {
1157 if (shdr[i].sh_type == SHT_STRTAB &&
1158 i != ehdr->e_shstrndx) {
1159 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1160 strtab = ehdrC + shdr[i].sh_offset;
1165 belch("ocVerifyImage_ELF: no string tables, or too many");
1170 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1171 for (i = 0; i < ehdr->e_shnum; i++) {
1172 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1173 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1175 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1176 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1177 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1179 shdr[i].sh_size % sizeof(Elf32_Sym)
1181 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1182 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1185 for (j = 0; j < nent; j++) {
1186 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1187 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1188 (int)stab[j].st_shndx,
1189 (int)stab[j].st_size,
1190 (char*)stab[j].st_value ));
1192 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1193 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1194 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1195 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1196 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1197 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1198 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1199 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1201 IF_DEBUG(linker,fprintf(stderr, " " ));
1203 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1204 switch (ELF32_ST_BIND(stab[j].st_info)) {
1205 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1206 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1207 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1208 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1210 IF_DEBUG(linker,fprintf(stderr, " " ));
1212 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1216 if (nsymtabs == 0) {
1217 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1226 ocGetNames_ELF ( ObjectCode* oc )
1231 char* ehdrC = (char*)(oc->image);
1232 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1233 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1234 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1235 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1238 belch("ocGetNames_ELF: no strtab");
1243 oc->sections = stgMallocBytes( ehdr->e_shnum * sizeof(Section),
1245 oc->n_sections = ehdr->e_shnum;
1247 for (i = 0; i < ehdr->e_shnum; i++) {
1249 /* make a section entry for relevant sections */
1250 SectionKind kind = SECTIONKIND_OTHER;
1251 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1252 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1253 kind = SECTIONKIND_RWDATA;
1254 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1255 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1256 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1257 kind = SECTIONKIND_CODE_OR_RODATA;
1259 /* fill in the section info */
1260 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1261 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1262 oc->sections[i].kind = kind;
1264 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1266 /* copy stuff into this module's object symbol table */
1267 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1268 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1269 oc->symbols = malloc(nent * sizeof(SymbolVal));
1270 for (j = 0; j < nent; j++) {
1271 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL /* ||
1272 ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL */
1274 /* and not an undefined symbol */
1275 && stab[j].st_shndx != SHN_UNDEF
1277 /* and it's a not a section or string table or anything silly */
1278 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1279 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1280 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE )
1282 char* nm = strtab + stab[j].st_name;
1284 + shdr[ stab[j].st_shndx ].sh_offset
1288 IF_DEBUG(linker,belch( "addOTabName: %10p %s %s",
1289 ad, oc->fileName, nm ));
1290 oc->symbols[j].lbl = nm;
1291 oc->symbols[j].addr = ad;
1292 insertStrHashTable(symhash, nm, &(oc->symbols[j]));
1295 IF_DEBUG(linker,belch( "skipping `%s'", strtab +
1297 oc->symbols[j].lbl = NULL;
1298 oc->symbols[j].addr = NULL;
1308 ocResolve_ELF ( ObjectCode* oc )
1310 char *strtab, *symbol;
1312 Elf32_Sym* stab = NULL;
1313 char* ehdrC = (char*)(oc->image);
1314 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1315 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1318 /* first find "the" symbol table */
1319 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1321 /* also go find the string table */
1322 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1324 if (stab == NULL || strtab == NULL) {
1325 belch("ocResolve_ELF: can't find string or symbol table");
1329 for (i = 0; i < ehdr->e_shnum; i++) {
1330 if (shdr[i].sh_type == SHT_REL ) {
1331 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[i].sh_offset);
1332 int nent = shdr[i].sh_size / sizeof(Elf32_Rel);
1333 int target_shndx = shdr[i].sh_info;
1334 int symtab_shndx = shdr[i].sh_link;
1335 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1336 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1337 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1338 target_shndx, symtab_shndx ));
1339 for (j = 0; j < nent; j++) {
1340 Elf32_Addr offset = rtab[j].r_offset;
1341 Elf32_Word info = rtab[j].r_info;
1343 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1344 Elf32_Word* pP = (Elf32_Word*)P;
1348 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p) ",
1349 j, (void*)offset, (void*)info ));
1351 IF_DEBUG(linker,belch( " ZERO" ));
1354 /* First see if it is a nameless local symbol. */
1355 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1356 symbol = "(noname)";
1357 S = (Elf32_Addr)(ehdrC
1358 + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1359 + stab[ELF32_R_SYM(info)].st_value
1362 /* No? Should be in the symbol table then. */
1363 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1364 (void *)S = lookupSymbol( symbol );
1367 barf("ocResolve_ELF: %s: unknown symbol `%s'",
1368 oc->fileName, symbol);
1370 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1372 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n", (void*)P, (void*)S, (void*)A ));
1373 switch (ELF32_R_TYPE(info)) {
1374 case R_386_32: *pP = S + A; break;
1375 case R_386_PC32: *pP = S + A - P; break;
1376 default: fprintf(stderr,
1377 "unhandled ELF relocation type %d",
1378 ELF32_R_TYPE(info));
1379 belch("ocResolve_ELF: unhandled ELF relocation type");
1386 if (shdr[i].sh_type == SHT_RELA) {
1387 belch("ocResolve_ELF: RelA style reloc table -- not yet done");
1396 #endif /* defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) */
1398 /* -----------------------------------------------------------------------------
1399 * Look up an address to discover whether it is in text or data space.
1401 * Used by the garbage collector when walking the stack.
1402 * -------------------------------------------------------------------------- */
1405 lookupSection ( void* addr )
1410 for ( oc = objects; oc; oc = oc->next ) {
1411 for (i = 0; i < oc->n_sections; i++) {
1412 if (oc->sections[i].start <= addr
1413 && addr <= oc->sections[i].end)
1414 return oc->sections[i].kind;
1417 return SECTIONKIND_OTHER;
1421 is_dynamically_loaded_code_or_rodata_ptr ( char* p )
1423 SectionKind sk = lookupSection(p);
1424 assert (sk != SECTIONKIND_NOINFOAVAIL);
1425 return (sk == SECTIONKIND_CODE_OR_RODATA);
1430 is_dynamically_loaded_rwdata_ptr ( char* p )
1432 SectionKind sk = lookupSection(p);
1433 assert (sk != SECTIONKIND_NOINFOAVAIL);
1434 return (sk == SECTIONKIND_RWDATA);
1439 is_not_dynamically_loaded_ptr ( char* p )
1441 SectionKind sk = lookupSection(p);
1442 assert (sk != SECTIONKIND_NOINFOAVAIL);
1443 return (sk == SECTIONKIND_OTHER);