1 /* -----------------------------------------------------------------------------
2 * $Id: Linker.c,v 1.2 2000/11/13 14:40:37 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(newDoubleArrayzh_fast) \
176 SymX(newFloatArrayzh_fast) \
177 SymX(newAddrArrayzh_fast) \
178 SymX(newWordArrayzh_fast) \
179 SymX(newIntArrayzh_fast) \
180 SymX(newCharArrayzh_fast) \
181 SymX(newMutVarzh_fast) \
182 SymX(quotRemIntegerzh_fast) \
183 SymX(quotIntegerzh_fast) \
184 SymX(remIntegerzh_fast) \
185 SymX(divExactIntegerzh_fast) \
186 SymX(divModIntegerzh_fast) \
187 SymX(timesIntegerzh_fast) \
188 SymX(minusIntegerzh_fast) \
189 SymX(plusIntegerzh_fast) \
190 SymX(mkWeakzh_fast) \
193 SymX(resetNonBlockingFd) \
195 SymX(stable_ptr_table) \
196 SymX(shutdownHaskellAndExit) \
197 Sym(stg_enterStackTop) \
198 Sym(stg_yield_to_Hugs) \
201 SymX(blockAsyncExceptionszh_fast) \
202 SymX(unblockAsyncExceptionszh_fast) \
205 /* entirely bogus claims about types of these symbols */
206 #define Sym(vvv) extern void (vvv);
207 #define SymX(vvv) /**/
212 #ifdef LEADING_UNDERSCORE
213 #define MAYBE_LEADING_UNDERSCORE_STR(s) ("_" s)
215 #define MAYBE_LEADING_UNDERSCORE_STR(s) (s)
218 #define Sym(vvv) { MAYBE_LEADING_UNDERSCORE_STR(#vvv), \
220 #define SymX(vvv) Sym(vvv)
222 static SymbolVal rtsSyms[] = {
224 { 0, 0 } /* sentinel */
227 /* -----------------------------------------------------------------------------
228 * initialize the object linker
230 static void *dl_prog_handle;
237 symhash = allocStrHashTable();
239 /* populate the symbol table with stuff from the RTS */
240 for (sym = rtsSyms; sym->lbl != NULL; sym++) {
241 insertStrHashTable(symhash, sym->lbl, sym);
244 dl_prog_handle = dlopen(NULL, RTLD_LAZY);
247 /* -----------------------------------------------------------------------------
248 * lookup a symbol in the hash table
251 lookupSymbol( char *lbl )
254 val = lookupStrHashTable(symhash, lbl);
257 return dlsym(dl_prog_handle, lbl);
263 /* -----------------------------------------------------------------------------
264 * Load an obj (populate the global symbol table, but don't resolve yet)
266 * Returns: 1 if ok, 0 on error.
269 loadObj( char *path )
277 /* assert that we haven't already loaded this object */
280 for (o = objects; o; o = o->next)
281 ASSERT(strcmp(o->fileName, path));
285 oc = stgMallocBytes(sizeof(ObjectCode), "loadObj(oc)");
287 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
288 oc->formatName = "ELF";
289 # elif defined(cygwin32_TARGET_OS)
290 oc->formatName = "PEi386";
293 barf("loadObj: not implemented on this platform");
297 if (r == -1) { return 0; }
300 oc->fileSize = st.st_size;
301 oc->image = stgMallocBytes( st.st_size, "loadObj(image)" );
305 /* chain it onto the list of objects */
309 /* load the image into memory */
310 f = fopen(path, "rb");
312 barf("loadObj: can't read `%s'", path);
314 n = fread ( oc->image, 1, oc->fileSize, f );
315 if (n != oc->fileSize) {
317 barf("loadObj: error whilst reading `%s'", path);
320 /* verify the in-memory image */
321 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
322 r = ocVerifyImage_ELF ( oc );
323 # elif defined(cygwin32_TARGET_OS)
324 r = ocVerifyImage_PEi386 ( oc );
326 barf("loadObj: no verify method");
328 if (!r) { return r; }
330 /* build the symbol list for this image */
331 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
332 r = ocGetNames_ELF ( oc );
333 # elif defined(cygwin32_TARGET_OS)
334 r = ocGetNames_PEi386 ( oc );
336 barf("loadObj: no getNames method");
338 if (!r) { return r; }
340 /* loaded, but not resolved yet */
341 oc->status = OBJECT_LOADED;
346 /* -----------------------------------------------------------------------------
347 * resolve all the currently unlinked objects in memory
349 * Returns: 1 if ok, 0 on error.
357 for (oc = objects; oc; oc = oc->next) {
358 if (oc->status != OBJECT_RESOLVED) {
359 # if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
360 r = ocResolve_ELF ( oc );
361 # elif defined(cygwin32_TARGET_OS)
362 r = ocResolve_PEi386 ( oc );
364 barf("link: not implemented on this platform");
366 if (!r) { return r; }
367 oc->status = OBJECT_RESOLVED;
373 /* -----------------------------------------------------------------------------
374 * delete an object from the pool
377 unloadObj( char *path )
381 for (oc = objects; oc; oc = oc->next) {
382 if (!strcmp(oc->fileName,path)) {
384 /* Remove all the mappings for the symbols within this
389 for (s = oc->symbols; s < oc->symbols + oc->n_symbols; s++) {
390 removeStrHashTable(symhash, s->lbl, NULL);
394 /* We're going to leave this in place, in case there are
395 any pointers from the heap into it: */
396 /* free(oc->image); */
404 belch("unloadObj: can't find `%s' to unload", path);
408 /* --------------------------------------------------------------------------
409 * PEi386 specifics (cygwin32)
410 * ------------------------------------------------------------------------*/
412 /* The information for this linker comes from
413 Microsoft Portable Executable
414 and Common Object File Format Specification
415 revision 5.1 January 1998
416 which SimonM says comes from the MS Developer Network CDs.
420 #if defined(cygwin32_TARGET_OS)
424 typedef unsigned char UChar;
425 typedef unsigned short UInt16;
426 typedef unsigned int UInt32;
433 UInt16 NumberOfSections;
434 UInt32 TimeDateStamp;
435 UInt32 PointerToSymbolTable;
436 UInt32 NumberOfSymbols;
437 UInt16 SizeOfOptionalHeader;
438 UInt16 Characteristics;
442 #define sizeof_COFF_header 20
449 UInt32 VirtualAddress;
450 UInt32 SizeOfRawData;
451 UInt32 PointerToRawData;
452 UInt32 PointerToRelocations;
453 UInt32 PointerToLinenumbers;
454 UInt16 NumberOfRelocations;
455 UInt16 NumberOfLineNumbers;
456 UInt32 Characteristics;
460 #define sizeof_COFF_section 40
467 UInt16 SectionNumber;
470 UChar NumberOfAuxSymbols;
474 #define sizeof_COFF_symbol 18
479 UInt32 VirtualAddress;
480 UInt32 SymbolTableIndex;
485 #define sizeof_COFF_reloc 10
488 /* From PE spec doc, section 3.3.2 */
489 #define IMAGE_FILE_RELOCS_STRIPPED 0x0001
490 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
491 #define IMAGE_FILE_DLL 0x2000
492 #define IMAGE_FILE_SYSTEM 0x1000
493 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
494 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
495 #define IMAGE_FILE_32BIT_MACHINE 0x0100
497 /* From PE spec doc, section 5.4.2 and 5.4.4 */
498 #define IMAGE_SYM_CLASS_EXTERNAL 2
499 #define IMAGE_SYM_CLASS_STATIC 3
500 #define IMAGE_SYM_UNDEFINED 0
502 /* From PE spec doc, section 4.1 */
503 #define IMAGE_SCN_CNT_CODE 0x00000020
504 #define IMAGE_SCN_CNT_INITIALIZED_DATA 0x00000040
506 /* From PE spec doc, section 5.2.1 */
507 #define IMAGE_REL_I386_DIR32 0x0006
508 #define IMAGE_REL_I386_REL32 0x0014
511 /* We use myindex to calculate array addresses, rather than
512 simply doing the normal subscript thing. That's because
513 some of the above structs have sizes which are not
514 a whole number of words. GCC rounds their sizes up to a
515 whole number of words, which means that the address calcs
516 arising from using normal C indexing or pointer arithmetic
517 are just plain wrong. Sigh.
520 myindex ( int scale, int index, void* base )
523 ((UChar*)base) + scale * index;
528 printName ( UChar* name, UChar* strtab )
530 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
531 UInt32 strtab_offset = * (UInt32*)(name+4);
532 fprintf ( stderr, "%s", strtab + strtab_offset );
535 for (i = 0; i < 8; i++) {
536 if (name[i] == 0) break;
537 fprintf ( stderr, "%c", name[i] );
544 copyName ( UChar* name, UChar* strtab, UChar* dst, int dstSize )
546 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
547 UInt32 strtab_offset = * (UInt32*)(name+4);
548 strncpy ( dst, strtab+strtab_offset, dstSize );
554 if (name[i] == 0) break;
564 cstring_from_COFF_symbol_name ( UChar* name, UChar* strtab )
567 /* If the string is longer than 8 bytes, look in the
568 string table for it -- this will be correctly zero terminated.
570 if (name[0]==0 && name[1]==0 && name[2]==0 && name[3]==0) {
571 UInt32 strtab_offset = * (UInt32*)(name+4);
572 return ((UChar*)strtab) + strtab_offset;
574 /* Otherwise, if shorter than 8 bytes, return the original,
575 which by defn is correctly terminated.
577 if (name[7]==0) return name;
578 /* The annoying case: 8 bytes. Copy into a temporary
579 (which is never freed ...)
583 strncpy(newstr,name,8);
590 /* Just compares the short names (first 8 chars) */
591 static COFF_section *
592 findPEi386SectionCalled ( ObjectCode* oc, char* name )
596 = (COFF_header*)(oc->image);
599 ((UChar*)(oc->image))
600 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
602 for (i = 0; i < hdr->NumberOfSections; i++) {
605 COFF_section* section_i
607 myindex ( sizeof_COFF_section, i, sectab );
608 n1 = (UChar*) &(section_i->Name);
610 if (n1[0]==n2[0] && n1[1]==n2[1] && n1[2]==n2[2] &&
611 n1[3]==n2[3] && n1[4]==n2[4] && n1[5]==n2[5] &&
612 n1[6]==n2[6] && n1[7]==n2[7])
621 zapTrailingAtSign ( UChar* sym )
624 if (sym[0] == 0) return;
626 while (sym[i] != 0) i++;
629 while (j > 0 && isdigit(sym[j])) j--;
630 if (j > 0 && sym[j] == '@' && j != i) sym[j] = 0;
635 ocVerifyImage_PEi386 ( ObjectCode* oc )
639 COFF_section* sectab;
643 hdr = (COFF_header*)(oc->image);
644 sectab = (COFF_section*) (
645 ((UChar*)(oc->image))
646 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
648 symtab = (COFF_symbol*) (
649 ((UChar*)(oc->image))
650 + hdr->PointerToSymbolTable
652 strtab = ((UChar*)(oc->image))
653 + hdr->PointerToSymbolTable
654 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
656 if (hdr->Machine != 0x14c) {
657 oc->errMsg("Not x86 PEi386");
660 if (hdr->SizeOfOptionalHeader != 0) {
661 oc->errMsg("PEi386 with nonempty optional header");
664 if ( /* (hdr->Characteristics & IMAGE_FILE_RELOCS_STRIPPED) || */
665 (hdr->Characteristics & IMAGE_FILE_EXECUTABLE_IMAGE) ||
666 (hdr->Characteristics & IMAGE_FILE_DLL) ||
667 (hdr->Characteristics & IMAGE_FILE_SYSTEM) ) {
668 oc->errMsg("Not a PEi386 object file");
671 if ( (hdr->Characteristics & IMAGE_FILE_BYTES_REVERSED_HI) ||
672 !(hdr->Characteristics & IMAGE_FILE_32BIT_MACHINE) ) {
673 oc->errMsg("Invalid PEi386 word size or endiannness");
677 if (!verb) return TRUE;
678 /* No further verification after this point; only debug printing. */
681 "sectab offset = %d\n", ((UChar*)sectab) - ((UChar*)hdr) );
683 "symtab offset = %d\n", ((UChar*)symtab) - ((UChar*)hdr) );
685 "strtab offset = %d\n", ((UChar*)strtab) - ((UChar*)hdr) );
687 fprintf ( stderr, "\n" );
689 "Machine: 0x%x\n", (UInt32)(hdr->Machine) );
691 "# sections: %d\n", (UInt32)(hdr->NumberOfSections) );
693 "time/date: 0x%x\n", (UInt32)(hdr->TimeDateStamp) );
695 "symtab offset: %d\n", (UInt32)(hdr->PointerToSymbolTable) );
697 "# symbols: %d\n", (UInt32)(hdr->NumberOfSymbols) );
699 "sz of opt hdr: %d\n", (UInt32)(hdr->SizeOfOptionalHeader) );
701 "characteristics: 0x%x\n", (UInt32)(hdr->Characteristics) );
703 fprintf ( stderr, "\n" );
704 fprintf ( stderr, "string table has size 0x%x\n", * (UInt32*)strtab );
705 fprintf ( stderr, "---START of string table---\n");
706 for (i = 4; i < *(UInt32*)strtab; i++) {
708 fprintf ( stderr, "\n"); else
709 fprintf( stderr, "%c", strtab[i] );
711 fprintf ( stderr, "--- END of string table---\n");
713 fprintf ( stderr, "\n" );
714 for (i = 0; i < hdr->NumberOfSections; i++) {
716 COFF_section* sectab_i
718 myindex ( sizeof_COFF_section, i, sectab );
725 printName ( sectab_i->Name, strtab );
734 sectab_i->VirtualSize,
735 sectab_i->VirtualAddress,
736 sectab_i->SizeOfRawData,
737 sectab_i->PointerToRawData,
738 sectab_i->NumberOfRelocations,
739 sectab_i->PointerToRelocations
741 reltab = (COFF_reloc*) (
742 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
744 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
746 COFF_reloc* rel = (COFF_reloc*)
747 myindex ( sizeof_COFF_reloc, j, reltab );
749 " type 0x%-4x vaddr 0x%-8x name `",
751 rel->VirtualAddress );
753 myindex ( sizeof_COFF_symbol, rel->SymbolTableIndex, symtab );
754 printName ( sym->Name, strtab );
755 fprintf ( stderr, "'\n" );
757 fprintf ( stderr, "\n" );
761 fprintf ( stderr, "\n" );
764 COFF_symbol* symtab_i;
765 if (i >= hdr->NumberOfSymbols) break;
766 symtab_i = (COFF_symbol*)
767 myindex ( sizeof_COFF_symbol, i, symtab );
773 printName ( symtab_i->Name, strtab );
782 (Int32)(symtab_i->SectionNumber) - 1,
783 (UInt32)symtab_i->Type,
784 (UInt32)symtab_i->StorageClass,
785 (UInt32)symtab_i->NumberOfAuxSymbols
787 i += symtab_i->NumberOfAuxSymbols;
791 fprintf ( stderr, "\n" );
798 ocGetNames_PEi386 ( ObjectCode* oc )
801 COFF_section* sectab;
809 hdr = (COFF_header*)(oc->image);
810 sectab = (COFF_section*) (
811 ((UChar*)(oc->image))
812 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
814 symtab = (COFF_symbol*) (
815 ((UChar*)(oc->image))
816 + hdr->PointerToSymbolTable
818 strtab = ((UChar*)(oc->image))
819 + hdr->PointerToSymbolTable
820 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
822 /* Copy exported symbols into the ObjectCode. */
825 COFF_symbol* symtab_i;
826 if (i >= hdr->NumberOfSymbols) break;
827 symtab_i = (COFF_symbol*)
828 myindex ( sizeof_COFF_symbol, i, symtab );
830 if (symtab_i->StorageClass == IMAGE_SYM_CLASS_EXTERNAL &&
831 symtab_i->SectionNumber != IMAGE_SYM_UNDEFINED) {
833 /* This symbol is global and defined, viz, exported */
834 COFF_section* sectabent;
836 sname = cstring_from_COFF_symbol_name (
837 symtab_i->Name, strtab
840 oc->errMsg("Out of memory when copying PEi386 symbol");
844 /* for IMAGE_SYMCLASS_EXTERNAL
845 && !IMAGE_SYM_UNDEFINED,
846 the address of the symbol is:
847 address of relevant section + offset in section
849 sectabent = (COFF_section*)
850 myindex ( sizeof_COFF_section,
851 symtab_i->SectionNumber-1,
853 addr = ((UChar*)(oc->image))
854 + (sectabent->PointerToRawData
856 /* fprintf ( stderr, "addSymbol %p `%s'\n", addr,sname); */
857 if (!addSymbol(oc,sname,addr)) return FALSE;
859 i += symtab_i->NumberOfAuxSymbols;
863 oc->sections = stgMallocBytes( NumberOfSections * sizeof(Section),
864 "ocGetNamesPEi386" );
866 /* Copy section information into the ObjectCode. */
867 for (i = 0; i < hdr->NumberOfSections; i++) {
873 COFF_section* sectab_i
875 myindex ( sizeof_COFF_section, i, sectab );
876 /* fprintf ( stderr, "section name = %s\n", sectab_i->Name ); */
879 /* I'm sure this is the Right Way to do it. However, the
880 alternative of testing the sectab_i->Name field seems to
883 if (sectab_i->Characteristics & IMAGE_SCN_CNT_CODE ||
884 sectab_i->Characteristics & IMAGE_SCN_CNT_INITIALIZED_DATA)
885 kind = SECTIONKIND_CODE_OR_RODATA;
888 if (0==strcmp(".text",sectab_i->Name))
889 kind = SECTIONKIND_CODE_OR_RODATA;
890 if (0==strcmp(".data",sectab_i->Name) ||
891 0==strcmp(".bss",sectab_i->Name))
892 kind = SECTIONKIND_RWDATA;
894 start = ((UChar*)(oc->image))
895 + sectab_i->PointerToRawData;
897 + sectab_i->SizeOfRawData - 1;
899 if (kind != SECTIONKIND_OTHER) {
900 addSection ( oc, start, end, kind );
902 fprintf ( stderr, "unknown section name = `%s'\n",
904 oc->errMsg("Unknown PEi386 section name");
914 ocResolve_PEi386 ( ObjectCode* oc, int verb )
917 COFF_section* sectab;
926 char symbol[1000]; // ToDo
928 hdr = (COFF_header*)(oc->image);
929 sectab = (COFF_section*) (
930 ((UChar*)(oc->image))
931 + sizeof_COFF_header + hdr->SizeOfOptionalHeader
933 symtab = (COFF_symbol*) (
934 ((UChar*)(oc->image))
935 + hdr->PointerToSymbolTable
937 strtab = ((UChar*)(oc->image))
938 + hdr->PointerToSymbolTable
939 + hdr->NumberOfSymbols * sizeof_COFF_symbol;
941 for (i = 0; i < hdr->NumberOfSections; i++) {
942 COFF_section* sectab_i
944 myindex ( sizeof_COFF_section, i, sectab );
947 ((UChar*)(oc->image)) + sectab_i->PointerToRelocations
949 for (j = 0; j < sectab_i->NumberOfRelocations; j++) {
953 myindex ( sizeof_COFF_reloc, j, reltab );
955 /* the location to patch */
957 ((UChar*)(oc->image))
958 + (sectab_i->PointerToRawData
959 + reltab_j->VirtualAddress)
961 /* the existing contents of pP */
963 /* the symbol to connect to */
965 myindex ( sizeof_COFF_symbol,
966 reltab_j->SymbolTableIndex, symtab );
969 "reloc sec %2d num %3d: type 0x%-4x "
970 "vaddr 0x%-8x name `",
972 (UInt32)reltab_j->Type,
973 reltab_j->VirtualAddress );
974 printName ( sym->Name, strtab );
975 fprintf ( stderr, "'\n" );
978 if (sym->StorageClass == IMAGE_SYM_CLASS_STATIC) {
979 COFF_section* section_sym
980 = findPEi386SectionCalled ( oc, sym->Name );
982 fprintf ( stderr, "bad section = `%s'\n", sym->Name );
983 oc->errMsg("Can't find abovementioned PEi386 section");
986 S = ((UInt32)(oc->image))
987 + (section_sym->PointerToRawData
990 copyName ( sym->Name, strtab, symbol, 1000 );
991 zapTrailingAtSign ( symbol );
992 S = (UInt32) ocLookupSym ( oc, symbol );
994 S = (UInt32)(oc->clientLookup ( symbol ));
996 belch("%s: unresolvable reference to `%s'", oc->fileName, symbol);
1001 switch (reltab_j->Type) {
1002 case IMAGE_REL_I386_DIR32:
1005 case IMAGE_REL_I386_REL32:
1006 /* Tricky. We have to insert a displacement at
1007 pP which, when added to the PC for the _next_
1008 insn, gives the address of the target (S).
1009 Problem is to know the address of the next insn
1010 when we only know pP. We assume that this
1011 literal field is always the last in the insn,
1012 so that the address of the next insn is pP+4
1013 -- hence the constant 4.
1014 Also I don't know if A should be added, but so
1015 far it has always been zero.
1018 *pP = S - ((UInt32)pP) - 4;
1022 "unhandled PEi386 relocation type %d\n",
1024 oc->errMsg("unhandled PEi386 relocation type");
1034 #endif /* defined(cygwin32_TARGET_OS) */
1037 /* --------------------------------------------------------------------------
1038 * ELF specifics (Linux, Solaris)
1039 * ------------------------------------------------------------------------*/
1041 #if defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS)
1049 findElfSection ( void* objImage, Elf32_Word sh_type )
1052 char* ehdrC = (char*)objImage;
1053 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1054 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1056 for (i = 0; i < ehdr->e_shnum; i++) {
1057 if (shdr[i].sh_type == sh_type &&
1058 i != ehdr->e_shstrndx) {
1059 ptr = ehdrC + shdr[i].sh_offset;
1068 ocVerifyImage_ELF ( ObjectCode* oc )
1072 int i, j, nent, nstrtab, nsymtabs;
1076 char* ehdrC = (char*)(oc->image);
1077 Elf32_Ehdr* ehdr = ( Elf32_Ehdr*)ehdrC;
1079 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
1080 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
1081 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
1082 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
1083 belch("ocVerifyImage_ELF: not an ELF header");
1086 IF_DEBUG(linker,belch( "Is an ELF header" ));
1088 if (ehdr->e_ident[EI_CLASS] != ELFCLASS32) {
1089 belch("ocVerifyImage_ELF: not 32 bit ELF" );
1093 IF_DEBUG(linker,belch( "Is 32 bit ELF" ));
1095 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
1096 IF_DEBUG(linker,belch( "Is little-endian" ));
1098 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
1099 IF_DEBUG(linker,belch( "Is big-endian" ));
1101 belch("ocVerifyImage_ELF: unknown endiannness");
1105 if (ehdr->e_type != ET_REL) {
1106 belch("ocVerifyImage_ELF: not a relocatable object (.o) file");
1109 IF_DEBUG(linker, belch( "Is a relocatable object (.o) file" ));
1111 IF_DEBUG(linker,belch( "Architecture is " ));
1112 switch (ehdr->e_machine) {
1113 case EM_386: IF_DEBUG(linker,belch( "x86" )); break;
1114 case EM_SPARC: IF_DEBUG(linker,belch( "sparc" )); break;
1115 default: IF_DEBUG(linker,belch( "unknown" ));
1116 belch("ocVerifyImage_ELF: unknown architecture");
1120 IF_DEBUG(linker,belch(
1121 "\nSection header table: start %d, n_entries %d, ent_size %d",
1122 ehdr->e_shoff, ehdr->e_shnum, ehdr->e_shentsize ));
1124 ASSERT (ehdr->e_shentsize == sizeof(Elf32_Shdr));
1126 shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1128 if (ehdr->e_shstrndx == SHN_UNDEF) {
1129 belch("ocVerifyImage_ELF: no section header string table");
1132 IF_DEBUG(linker,belch( "Section header string table is section %d",
1134 sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1137 for (i = 0; i < ehdr->e_shnum; i++) {
1138 IF_DEBUG(linker,fprintf(stderr, "%2d: ", i ));
1139 IF_DEBUG(linker,fprintf(stderr, "type=%2d ", shdr[i].sh_type ));
1140 IF_DEBUG(linker,fprintf(stderr, "size=%4d ", shdr[i].sh_size ));
1141 IF_DEBUG(linker,fprintf(stderr, "offs=%4d ", shdr[i].sh_offset ));
1142 IF_DEBUG(linker,fprintf(stderr, " (%p .. %p) ",
1143 ehdrC + shdr[i].sh_offset,
1144 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
1146 if (shdr[i].sh_type == SHT_REL) {
1147 IF_DEBUG(linker,fprintf(stderr, "Rel " ));
1148 } else if (shdr[i].sh_type == SHT_RELA) {
1149 IF_DEBUG(linker,fprintf(stderr, "RelA " ));
1151 IF_DEBUG(linker,fprintf(stderr," "));
1154 IF_DEBUG(linker,fprintf(stderr, "sname=%s\n", sh_strtab + shdr[i].sh_name ));
1158 IF_DEBUG(linker,belch( "\nString tables" ));
1161 for (i = 0; i < ehdr->e_shnum; i++) {
1162 if (shdr[i].sh_type == SHT_STRTAB &&
1163 i != ehdr->e_shstrndx) {
1164 IF_DEBUG(linker,belch(" section %d is a normal string table", i ));
1165 strtab = ehdrC + shdr[i].sh_offset;
1170 belch("ocVerifyImage_ELF: no string tables, or too many");
1175 IF_DEBUG(linker,belch( "\nSymbol tables" ));
1176 for (i = 0; i < ehdr->e_shnum; i++) {
1177 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1178 IF_DEBUG(linker,belch( "section %d is a symbol table", i ));
1180 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1181 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1182 IF_DEBUG(linker,belch( " number of entries is apparently %d (%d rem)",
1184 shdr[i].sh_size % sizeof(Elf32_Sym)
1186 if (0 != shdr[i].sh_size % sizeof(Elf32_Sym)) {
1187 belch("ocVerifyImage_ELF: non-integral number of symbol table entries");
1190 for (j = 0; j < nent; j++) {
1191 IF_DEBUG(linker,fprintf(stderr, " %2d ", j ));
1192 IF_DEBUG(linker,fprintf(stderr, " sec=%-5d size=%-3d val=%5p ",
1193 (int)stab[j].st_shndx,
1194 (int)stab[j].st_size,
1195 (char*)stab[j].st_value ));
1197 IF_DEBUG(linker,fprintf(stderr, "type=" ));
1198 switch (ELF32_ST_TYPE(stab[j].st_info)) {
1199 case STT_NOTYPE: IF_DEBUG(linker,fprintf(stderr, "notype " )); break;
1200 case STT_OBJECT: IF_DEBUG(linker,fprintf(stderr, "object " )); break;
1201 case STT_FUNC : IF_DEBUG(linker,fprintf(stderr, "func " )); break;
1202 case STT_SECTION: IF_DEBUG(linker,fprintf(stderr, "section" )); break;
1203 case STT_FILE: IF_DEBUG(linker,fprintf(stderr, "file " )); break;
1204 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1206 IF_DEBUG(linker,fprintf(stderr, " " ));
1208 IF_DEBUG(linker,fprintf(stderr, "bind=" ));
1209 switch (ELF32_ST_BIND(stab[j].st_info)) {
1210 case STB_LOCAL : IF_DEBUG(linker,fprintf(stderr, "local " )); break;
1211 case STB_GLOBAL: IF_DEBUG(linker,fprintf(stderr, "global" )); break;
1212 case STB_WEAK : IF_DEBUG(linker,fprintf(stderr, "weak " )); break;
1213 default: IF_DEBUG(linker,fprintf(stderr, "? " )); break;
1215 IF_DEBUG(linker,fprintf(stderr, " " ));
1217 IF_DEBUG(linker,fprintf(stderr, "name=%s\n", strtab + stab[j].st_name ));
1221 if (nsymtabs == 0) {
1222 belch("ocVerifyImage_ELF: didn't find any symbol tables");
1231 ocGetNames_ELF ( ObjectCode* oc )
1236 char* ehdrC = (char*)(oc->image);
1237 Elf32_Ehdr* ehdr = (Elf32_Ehdr*)ehdrC;
1238 char* strtab = findElfSection ( ehdrC, SHT_STRTAB );
1239 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1240 char* sh_strtab = ehdrC + shdr[ehdr->e_shstrndx].sh_offset;
1243 belch("ocGetNames_ELF: no strtab");
1248 oc->sections = stgMallocBytes( ehdr->e_shnum * sizeof(Section),
1250 oc->n_sections = ehdr->e_shnum;
1252 for (i = 0; i < ehdr->e_shnum; i++) {
1254 /* make a section entry for relevant sections */
1255 SectionKind kind = SECTIONKIND_OTHER;
1256 if (!strcmp(".data",sh_strtab+shdr[i].sh_name) ||
1257 !strcmp(".data1",sh_strtab+shdr[i].sh_name))
1258 kind = SECTIONKIND_RWDATA;
1259 if (!strcmp(".text",sh_strtab+shdr[i].sh_name) ||
1260 !strcmp(".rodata",sh_strtab+shdr[i].sh_name) ||
1261 !strcmp(".rodata1",sh_strtab+shdr[i].sh_name))
1262 kind = SECTIONKIND_CODE_OR_RODATA;
1264 /* fill in the section info */
1265 oc->sections[i].start = ehdrC + shdr[i].sh_offset;
1266 oc->sections[i].end = ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1;
1267 oc->sections[i].kind = kind;
1269 if (shdr[i].sh_type != SHT_SYMTAB) continue;
1271 /* copy stuff into this module's object symbol table */
1272 stab = (Elf32_Sym*) (ehdrC + shdr[i].sh_offset);
1273 nent = shdr[i].sh_size / sizeof(Elf32_Sym);
1274 oc->symbols = malloc(nent * sizeof(SymbolVal));
1275 for (j = 0; j < nent; j++) {
1276 if ( ( ELF32_ST_BIND(stab[j].st_info)==STB_GLOBAL /* ||
1277 ELF32_ST_BIND(stab[j].st_info)==STB_LOCAL */
1279 /* and not an undefined symbol */
1280 && stab[j].st_shndx != SHN_UNDEF
1282 /* and it's a not a section or string table or anything silly */
1283 ( ELF32_ST_TYPE(stab[j].st_info)==STT_FUNC ||
1284 ELF32_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
1285 ELF32_ST_TYPE(stab[j].st_info)==STT_NOTYPE )
1287 char* nm = strtab + stab[j].st_name;
1289 + shdr[ stab[j].st_shndx ].sh_offset
1293 IF_DEBUG(linker,belch( "addOTabName: %10p %s %s",
1294 ad, oc->fileName, nm ));
1295 oc->symbols[j].lbl = nm;
1296 oc->symbols[j].addr = ad;
1297 insertStrHashTable(symhash, nm, &(oc->symbols[j]));
1300 IF_DEBUG(linker,belch( "skipping `%s'", strtab +
1302 oc->symbols[j].lbl = NULL;
1303 oc->symbols[j].addr = NULL;
1313 ocResolve_ELF ( ObjectCode* oc )
1315 char *strtab, *symbol;
1317 Elf32_Sym* stab = NULL;
1318 char* ehdrC = (char*)(oc->image);
1319 Elf32_Ehdr* ehdr = (Elf32_Ehdr*) ehdrC;
1320 Elf32_Shdr* shdr = (Elf32_Shdr*) (ehdrC + ehdr->e_shoff);
1323 /* first find "the" symbol table */
1324 stab = (Elf32_Sym*) findElfSection ( ehdrC, SHT_SYMTAB );
1326 /* also go find the string table */
1327 strtab = findElfSection ( ehdrC, SHT_STRTAB );
1329 if (stab == NULL || strtab == NULL) {
1330 belch("ocResolve_ELF: can't find string or symbol table");
1334 for (i = 0; i < ehdr->e_shnum; i++) {
1335 if (shdr[i].sh_type == SHT_REL ) {
1336 Elf32_Rel* rtab = (Elf32_Rel*) (ehdrC + shdr[i].sh_offset);
1337 int nent = shdr[i].sh_size / sizeof(Elf32_Rel);
1338 int target_shndx = shdr[i].sh_info;
1339 int symtab_shndx = shdr[i].sh_link;
1340 stab = (Elf32_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1341 targ = (Elf32_Word*)(ehdrC + shdr[ target_shndx ].sh_offset);
1342 IF_DEBUG(linker,belch( "relocations for section %d using symtab %d",
1343 target_shndx, symtab_shndx ));
1344 for (j = 0; j < nent; j++) {
1345 Elf32_Addr offset = rtab[j].r_offset;
1346 Elf32_Word info = rtab[j].r_info;
1348 Elf32_Addr P = ((Elf32_Addr)targ) + offset;
1349 Elf32_Word* pP = (Elf32_Word*)P;
1353 IF_DEBUG(linker,belch( "Rel entry %3d is raw(%6p %6p) ",
1354 j, (void*)offset, (void*)info ));
1356 IF_DEBUG(linker,belch( " ZERO" ));
1359 /* First see if it is a nameless local symbol. */
1360 if (stab[ ELF32_R_SYM(info)].st_name == 0) {
1361 symbol = "(noname)";
1362 S = (Elf32_Addr)(ehdrC
1363 + shdr[stab[ELF32_R_SYM(info)].st_shndx ].sh_offset
1364 + stab[ELF32_R_SYM(info)].st_value
1367 /* No? Should be in the symbol table then. */
1368 symbol = strtab+stab[ ELF32_R_SYM(info)].st_name;
1369 (void *)S = lookupSymbol( symbol );
1372 barf("ocResolve_ELF: %s: unknown symbol `%s'",
1373 oc->fileName, symbol);
1375 IF_DEBUG(linker,belch( "`%s' resolves to %p", symbol, (void*)S ));
1377 IF_DEBUG(linker,fprintf ( stderr, "Reloc: P = %p S = %p A = %p\n", (void*)P, (void*)S, (void*)A ));
1378 switch (ELF32_R_TYPE(info)) {
1379 case R_386_32: *pP = S + A; break;
1380 case R_386_PC32: *pP = S + A - P; break;
1381 default: fprintf(stderr,
1382 "unhandled ELF relocation type %d",
1383 ELF32_R_TYPE(info));
1384 belch("ocResolve_ELF: unhandled ELF relocation type");
1391 if (shdr[i].sh_type == SHT_RELA) {
1392 belch("ocResolve_ELF: RelA style reloc table -- not yet done");
1401 #endif /* defined(linux_TARGET_OS) || defined(solaris2_TARGET_OS) */
1403 /* -----------------------------------------------------------------------------
1404 * Look up an address to discover whether it is in text or data space.
1406 * Used by the garbage collector when walking the stack.
1407 * -------------------------------------------------------------------------- */
1410 lookupSection ( void* addr )
1415 for ( oc = objects; oc; oc = oc->next ) {
1416 for (i = 0; i < oc->n_sections; i++) {
1417 if (oc->sections[i].start <= addr
1418 && addr <= oc->sections[i].end)
1419 return oc->sections[i].kind;
1422 return SECTIONKIND_OTHER;
1426 is_dynamically_loaded_code_or_rodata_ptr ( char* p )
1428 SectionKind sk = lookupSection(p);
1429 assert (sk != SECTIONKIND_NOINFOAVAIL);
1430 return (sk == SECTIONKIND_CODE_OR_RODATA);
1435 is_dynamically_loaded_rwdata_ptr ( char* p )
1437 SectionKind sk = lookupSection(p);
1438 assert (sk != SECTIONKIND_NOINFOAVAIL);
1439 return (sk == SECTIONKIND_RWDATA);
1444 is_not_dynamically_loaded_ptr ( char* p )
1446 SectionKind sk = lookupSection(p);
1447 assert (sk != SECTIONKIND_NOINFOAVAIL);
1448 return (sk == SECTIONKIND_OTHER);