14 #define IF_DEBUG(x,y) /* */
15 static int debug_linker
= 0;
17 #define i386_TARGET_ARCH
18 // #define arm_TARGET_ARCH
20 #if !defined(i386_TARGET_ARCH) && !defined(arm_TARGET_ARCH)
21 # error "Must #define i386_TARGET_ARCH or arm_TARGET_ARCH"
25 ///////////////////////////////////////////////////////////////////
26 ///////////////////////////////////////////////////////////////////
27 ///////////////////////////////////////////////////////////////////
34 typedef enum { OBJECT_LOADED
, OBJECT_RESOLVED
} OStatus
;
37 #define N_FIXUP_PAGES 1
40 /* Indication of section kinds for loaded objects. Needed by
41 the GC for deciding whether or not a pointer on the stack
45 enum { SECTIONKIND_CODE_OR_RODATA
,
48 SECTIONKIND_NOINFOAVAIL
}
56 struct _Section
* next
;
61 struct _ProddableBlock
{
64 struct _ProddableBlock
* next
;
68 /* Top-level structure for an object module. One of these is allocated
69 * for each object file in use.
71 typedef struct _ObjectCode
{
75 char* formatName
; /* eg "ELF32", "DLL", "COFF", etc. */
77 /* An array containing ptrs to all the symbol names copied from
78 this object into the global symbol hash table. This is so that
79 we know which parts of the latter mapping to nuke when this
80 object is removed from the system. */
84 /* ptr to malloc'd lump of memory holding the obj file */
87 /* Fixup area for long-distance jumps. */
92 /* The section-kind entries for this object module. Linked
96 /* A private hash table for local symbols. */
97 /* HashTable* */ void* lochash
;
99 /* Allow a chain of these things */
100 struct _ObjectCode
* next
;
102 /* SANITY CHECK ONLY: a list of the only memory regions which may
103 safely be prodded during relocation. Any attempt to prod
104 outside one of these is an error in the linker. */
105 ProddableBlock
* proddables
;
110 * Define a set of types which can be used for both ELF32 and ELF64
114 #define ELFCLASS ELFCLASS64
115 #define Elf_Addr Elf64_Addr
116 #define Elf_Word Elf64_Word
117 #define Elf_Sword Elf64_Sword
118 #define Elf_Ehdr Elf64_Ehdr
119 #define Elf_Phdr Elf64_Phdr
120 #define Elf_Shdr Elf64_Shdr
121 #define Elf_Sym Elf64_Sym
122 #define Elf_Rel Elf64_Rel
123 #define Elf_Rela Elf64_Rela
124 #define ELF_ST_TYPE ELF64_ST_TYPE
125 #define ELF_ST_BIND ELF64_ST_BIND
126 #define ELF_R_TYPE ELF64_R_TYPE
127 #define ELF_R_SYM ELF64_R_SYM
129 #define ELFCLASS ELFCLASS32
130 #define Elf_Addr Elf32_Addr
131 #define Elf_Word Elf32_Word
132 #define Elf_Sword Elf32_Sword
133 #define Elf_Ehdr Elf32_Ehdr
134 #define Elf_Phdr Elf32_Phdr
135 #define Elf_Shdr Elf32_Shdr
136 #define Elf_Sym Elf32_Sym
137 #define Elf_Rel Elf32_Rel
138 #define Elf_Rela Elf32_Rela
140 #define ELF_ST_TYPE ELF32_ST_TYPE
143 #define ELF_ST_BIND ELF32_ST_BIND
146 #define ELF_R_TYPE ELF32_R_TYPE
149 #define ELF_R_SYM ELF32_R_SYM
156 ///////////////////////////////////////////////////////////////////
157 ///////////////////////////////////////////////////////////////////
158 ///////////////////////////////////////////////////////////////////
162 /* -----------------------------------------------------------------------
163 * Sanity checking. For each ObjectCode, maintain a list of address ranges
164 * which may be prodded during relocation, and abort if we try and write
165 * outside any of these.
167 static void addProddableBlock ( ObjectCode
* oc
, void* start
, int size
)
170 = malloc(sizeof(ProddableBlock
));
172 fprintf(stderr
, "aPB oc=%p %p %d (%p .. %p)\n", oc
, start
, size
,
173 start
, ((char*)start
)+size
-1 );
177 pb
->next
= oc
->proddables
;
181 static void checkProddableBlock ( ObjectCode
* oc
, void* addr
)
184 for (pb
= oc
->proddables
; pb
!= NULL
; pb
= pb
->next
) {
185 char* s
= (char*)(pb
->start
);
186 char* e
= s
+ pb
->size
- 1;
187 char* a
= (char*)addr
;
188 /* Assumes that the biggest fixup involves a 4-byte write. This
189 probably needs to be changed to 8 (ie, +7) on 64-bit
191 if (a
>= s
&& (a
+3) <= e
) return;
194 "checkProddableBlock: invalid fixup %p in runtime linker\n",
201 ///////////////////////////////////////////////////////////////////
202 ///////////////////////////////////////////////////////////////////
203 ///////////////////////////////////////////////////////////////////
205 // String->Addr mappings
208 struct { char* mp_name
; void* mp_addr
; }
219 static StringMap
* new_StringMap ( void )
221 StringMap
* sm
= malloc(sizeof(StringMap
));
224 sm
->maplets
= malloc(10 * sizeof(Maplet
));
228 static void delete_StringMap ( StringMap
* sm
)
230 assert(sm
->maplets
!= NULL
);
236 static void ensure_StringMap ( StringMap
* sm
)
240 assert(sm
->maplets
!= NULL
);
241 if (sm
->sm_used
< sm
->sm_size
)
244 mp2
= malloc(sm
->sm_size
* sizeof(Maplet
));
245 for (i
= 0; i
< sm
->sm_used
; i
++)
246 mp2
[i
] = sm
->maplets
[i
];
251 static void* search_StringMap ( StringMap
* sm
, char* name
)
254 for (i
= 0; i
< sm
->sm_used
; i
++)
255 if (0 == strcmp(name
, sm
->maplets
[i
].mp_name
))
256 return sm
->maplets
[i
].mp_addr
;
260 static void addto_StringMap ( StringMap
* sm
, char* name
, void* addr
)
262 ensure_StringMap(sm
);
263 sm
->maplets
[sm
->sm_used
].mp_name
= name
;
264 sm
->maplets
[sm
->sm_used
].mp_addr
= addr
;
268 static void paranoid_addto_StringMap ( StringMap
* sm
, char* name
, void* addr
)
270 if (search_StringMap(sm
,name
) != NULL
) {
271 fprintf(stderr
, "paranoid_addto_StringMap(%s,%p)\n", name
, addr
);
274 addto_StringMap(sm
,name
,addr
);
278 ///////////////////////////////////////////////////////////////////
279 ///////////////////////////////////////////////////////////////////
280 ///////////////////////////////////////////////////////////////////
282 // Top-level linker control.
284 StringMap
* global_symbol_table
= NULL
;
285 ObjectCode
* global_object_list
= NULL
;
287 static void initLinker ( void )
289 if (global_symbol_table
!= NULL
)
291 global_symbol_table
= new_StringMap();
296 ///////////////////////////////////////////////////////////////////
297 ///////////////////////////////////////////////////////////////////
298 ///////////////////////////////////////////////////////////////////
302 /* -----------------------------------------------------------------
303 * lookup a symbol in the global symbol table
306 void * lookupSymbol( char *lbl
)
310 assert(global_symbol_table
!= NULL
);
311 val
= search_StringMap(global_symbol_table
, lbl
);
316 ///////////////////////////////////////////////////////////////////
317 ///////////////////////////////////////////////////////////////////
318 ///////////////////////////////////////////////////////////////////
323 * Generic ELF functions
327 findElfSection ( void* objImage
, Elf_Word sh_type
)
329 char* ehdrC
= (char*)objImage
;
330 Elf_Ehdr
* ehdr
= (Elf_Ehdr
*)ehdrC
;
331 Elf_Shdr
* shdr
= (Elf_Shdr
*)(ehdrC
+ ehdr
->e_shoff
);
332 char* sh_strtab
= ehdrC
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
336 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
337 if (shdr
[i
].sh_type
== sh_type
338 /* Ignore the section header's string table. */
339 && i
!= ehdr
->e_shstrndx
340 /* Ignore string tables named .stabstr, as they contain
342 && 0 != memcmp(".stabstr", sh_strtab
+ shdr
[i
].sh_name
, 8)
344 ptr
= ehdrC
+ shdr
[i
].sh_offset
;
351 #ifdef arm_TARGET_ARCH
353 char* alloc_fixup_bytes ( ObjectCode
* oc
, int nbytes
)
356 assert(nbytes
% 4 == 0);
358 res
= &(oc
->fixup
[oc
->fixup_used
]);
359 oc
->fixup_used
+= nbytes
;
360 if (oc
->fixup_used
>= oc
->fixup_size
) {
361 fprintf(stderr
, "fixup area too small for %s\n", oc
->fileName
);
369 ///////////////////////////////////////////////////////////////////
370 ///////////////////////////////////////////////////////////////////
371 ///////////////////////////////////////////////////////////////////
376 void* lookup_magic_hacks ( char* sym
)
378 if (0==strcmp(sym
, "printf")) return (void*)(&printf
);
382 #ifdef arm_TARGET_ARCH
384 void arm_notify_new_code ( char* start
, int length
)
386 __asm
__volatile ("mov r1, %0\n\t"
391 : "ir" (start
), "ir" (length
), "ir" (0) );
396 void gen_armle_goto ( char* fixup
, char* dstP
)
398 Elf_Word w
= (Elf_Word
)dstP
;
401 3 0000 04F01FE5 ldr pc, value
402 4 0004 44332211 value: .word 0x11223344
404 fprintf(stderr
,"at %p generating jump to %p\n", fixup
, dstP
);
405 fixup
[0] = 0x04; fixup
[1] = 0xF0; fixup
[2] = 0x1F; fixup
[3] = 0xE5;
406 fixup
[4] = w
& 0xFF; w
>>= 8;
407 fixup
[5] = w
& 0xFF; w
>>= 8;
408 fixup
[6] = w
& 0xFF; w
>>= 8;
409 fixup
[7] = w
& 0xFF; w
>>= 8;
410 arm_notify_new_code(fixup
, 8);
412 #endif /* arm_TARGET_ARCH */
416 /* Do ELF relocations which lack an explicit addend. All x86-linux
417 relocations appear to be of this form. */
419 do_Elf_Rel_relocations ( ObjectCode
* oc
, char* ehdrC
,
420 Elf_Shdr
* shdr
, int shnum
,
421 Elf_Sym
* stab
, char* strtab
)
426 Elf_Rel
* rtab
= (Elf_Rel
*) (ehdrC
+ shdr
[shnum
].sh_offset
);
427 int nent
= shdr
[shnum
].sh_size
/ sizeof(Elf_Rel
);
428 int target_shndx
= shdr
[shnum
].sh_info
;
429 int symtab_shndx
= shdr
[shnum
].sh_link
;
431 stab
= (Elf_Sym
*) (ehdrC
+ shdr
[ symtab_shndx
].sh_offset
);
432 targ
= (Elf_Word
*)(ehdrC
+ shdr
[ target_shndx
].sh_offset
);
433 IF_DEBUG(linker
,belch( "relocations for section %d using symtab %d",
434 target_shndx
, symtab_shndx
));
436 for (j
= 0; j
< nent
; j
++) {
437 Elf_Addr offset
= rtab
[j
].r_offset
;
438 Elf_Addr info
= rtab
[j
].r_info
;
440 Elf_Addr P
= ((Elf_Addr
)targ
) + offset
;
441 Elf_Word
* pP
= (Elf_Word
*)P
;
446 IF_DEBUG(linker
,belch( "Rel entry %3d is raw(%6p %6p)",
447 j
, (void*)offset
, (void*)info
));
449 IF_DEBUG(linker
,belch( " ZERO" ));
452 Elf_Sym sym
= stab
[ELF_R_SYM(info
)];
453 /* First see if it is a local symbol. */
454 if (ELF_ST_BIND(sym
.st_info
) == STB_LOCAL
) {
455 /* Yes, so we can get the address directly from the ELF symbol
457 symbol
= sym
.st_name
==0 ? "(noname)" : strtab
+sym
.st_name
;
459 (ehdrC
+ shdr
[ sym
.st_shndx
].sh_offset
460 + stab
[ELF_R_SYM(info
)].st_value
);
463 /* No, so look up the name in our global table. */
464 symbol
= strtab
+ sym
.st_name
;
465 S
= (Elf_Addr
)lookupSymbol( symbol
);
468 S
= (Elf_Addr
)lookup_magic_hacks(symbol
);
471 fprintf(stderr
,"%s: unknown symbol `%s'\n",
472 oc
->fileName
, symbol
);
476 fprintf(stderr
, "\n`%s' resolves to %p\n", symbol
, (void*)S
);
480 fprintf(stderr
, "Reloc: P = %p S = %p A = %p\n",
481 (void*)P
, (void*)S
, (void*)A
);
482 checkProddableBlock ( oc
, pP
);
486 switch (ELF_R_TYPE(info
)) {
487 # ifdef i386_TARGET_ARCH
488 case R_386_32
: *pP
= value
; break;
489 case R_386_PC32
: *pP
= value
- P
; break;
491 # ifdef arm_TARGET_ARCH
493 Elf_Word w
, delta
, deltaTop8
;
494 /* Generate a jump sequence into the fixup area
495 and branch to that instead. */
496 char* fixup
= alloc_fixup_bytes(oc
, 8);
497 /* First of all, figure out where we're really trying to
499 // compensate for pc+8 bias
500 Elf_Word real_dst
= (A
& 0x00FFFFFF) + 2;
501 // sign-extend 24-to-32 of real_dst
502 if (real_dst
& 0x00800000)
503 real_dst
|= 0xFF000000;
505 real_dst
&= 0x00FFFFFF;
510 gen_armle_goto(fixup
, (char*)real_dst
);
512 /* Delta is in bytes .. */
513 delta
= (((Elf_Word
)fixup
) - ((Elf_Word
)pP
) - 8);
514 deltaTop8
= (delta
>> 24) & 0xFF;
515 if (deltaTop8
!= 0 && deltaTop8
!= 0xFF) {
516 fprintf(stderr
,"R_ARM_PC24: out of range delta 0x%x for %s\n",
523 w
|= (0x00FFFFFF & delta
);
533 "%s: unhandled ELF relocation(Rel) type %d\n\n",
534 oc
->fileName
, ELF_R_TYPE(info
));
542 /* Do ELF relocations for which explicit addends are supplied.
543 sparc-solaris relocations appear to be of this form. */
545 do_Elf_Rela_relocations ( ObjectCode
* oc
, char* ehdrC
,
546 Elf_Shdr
* shdr
, int shnum
,
547 Elf_Sym
* stab
, char* strtab
)
552 Elf_Rela
* rtab
= (Elf_Rela
*) (ehdrC
+ shdr
[shnum
].sh_offset
);
553 int nent
= shdr
[shnum
].sh_size
/ sizeof(Elf_Rela
);
554 int target_shndx
= shdr
[shnum
].sh_info
;
555 int symtab_shndx
= shdr
[shnum
].sh_link
;
557 stab
= (Elf_Sym
*) (ehdrC
+ shdr
[ symtab_shndx
].sh_offset
);
558 targ
= (Elf_Addr
) (ehdrC
+ shdr
[ target_shndx
].sh_offset
);
559 IF_DEBUG(linker
,belch( "relocations for section %d using symtab %d",
560 target_shndx
, symtab_shndx
));
562 for (j
= 0; j
< nent
; j
++) {
563 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) || defined(ia64_TARGET_ARCH)
564 /* This #ifdef only serves to avoid unused-var warnings. */
565 Elf_Addr offset
= rtab
[j
].r_offset
;
566 Elf_Addr P
= targ
+ offset
;
568 Elf_Addr info
= rtab
[j
].r_info
;
569 Elf_Addr A
= rtab
[j
].r_addend
;
572 # if defined(sparc_TARGET_ARCH)
573 Elf_Word
* pP
= (Elf_Word
*)P
;
575 # elif defined(ia64_TARGET_ARCH)
576 Elf64_Xword
*pP
= (Elf64_Xword
*)P
;
580 IF_DEBUG(linker
,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
581 j
, (void*)offset
, (void*)info
,
584 IF_DEBUG(linker
,belch( " ZERO" ));
587 Elf_Sym sym
= stab
[ELF_R_SYM(info
)];
588 /* First see if it is a local symbol. */
589 if (ELF_ST_BIND(sym
.st_info
) == STB_LOCAL
) {
590 /* Yes, so we can get the address directly from the ELF symbol
592 symbol
= sym
.st_name
==0 ? "(noname)" : strtab
+sym
.st_name
;
594 (ehdrC
+ shdr
[ sym
.st_shndx
].sh_offset
595 + stab
[ELF_R_SYM(info
)].st_value
);
596 #ifdef ELF_FUNCTION_DESC
597 /* Make a function descriptor for this function */
598 if (S
&& ELF_ST_TYPE(sym
.st_info
) == STT_FUNC
) {
599 S
= allocateFunctionDesc(S
+ A
);
604 /* No, so look up the name in our global table. */
605 symbol
= strtab
+ sym
.st_name
;
606 S
= (Elf_Addr
)lookupSymbol( symbol
);
608 #ifdef ELF_FUNCTION_DESC
609 /* If a function, already a function descriptor - we would
610 have to copy it to add an offset. */
611 if (S
&& (ELF_ST_TYPE(sym
.st_info
) == STT_FUNC
) && (A
!= 0))
612 belch("%s: function %s with addend %p", oc
->fileName
, symbol
, (void *)A
);
616 fprintf(stderr
,"%s: unknown symbol `%s'\n", oc
->fileName
, symbol
);
619 IF_DEBUG(linker
,belch( "`%s' resolves to %p\n", symbol
, (void*)S
));
622 IF_DEBUG(linker
,fprintf ( stderr
, "Reloc: P = %p S = %p A = %p\n",
623 (void*)P
, (void*)S
, (void*)A
));
624 /* checkProddableBlock ( oc, (void*)P ); */
628 switch (ELF_R_TYPE(info
)) {
629 # if defined(sparc_TARGET_ARCH)
630 case R_SPARC_WDISP30
:
631 w1
= *pP
& 0xC0000000;
632 w2
= (Elf_Word
)((value
- P
) >> 2);
633 ASSERT((w2
& 0xC0000000) == 0);
638 w1
= *pP
& 0xFFC00000;
639 w2
= (Elf_Word
)(value
>> 10);
640 ASSERT((w2
& 0xFFC00000) == 0);
646 w2
= (Elf_Word
)(value
& 0x3FF);
647 ASSERT((w2
& ~0x3FF) == 0);
651 /* According to the Sun documentation:
653 This relocation type resembles R_SPARC_32, except it refers to an
654 unaligned word. That is, the word to be relocated must be treated
655 as four separate bytes with arbitrary alignment, not as a word
656 aligned according to the architecture requirements.
658 (JRS: which means that freeloading on the R_SPARC_32 case
659 is probably wrong, but hey ...)
663 w2
= (Elf_Word
)value
;
666 # elif defined(ia64_TARGET_ARCH)
667 case R_IA64_DIR64LSB
:
668 case R_IA64_FPTR64LSB
:
671 case R_IA64_PCREL64LSB
:
674 case R_IA64_SEGREL64LSB
:
675 addr
= findElfSegment(ehdrC
, value
);
679 ia64_reloc_gprel22(P
, value
);
682 case R_IA64_LTOFF22X
:
683 case R_IA64_LTOFF_FPTR22
:
684 addr
= allocateGOTEntry(value
);
685 ia64_reloc_gprel22(P
, addr
);
687 case R_IA64_PCREL21B
:
688 ia64_reloc_pcrel21(P
, S
, oc
);
691 /* This goes with R_IA64_LTOFF22X and points to the load to
692 * convert into a move. We don't implement relaxation. */
697 "%s: unhandled ELF relocation(RelA) type %d\n",
698 oc
->fileName
, ELF_R_TYPE(info
));
708 ocResolve_ELF ( ObjectCode
* oc
)
712 Elf_Sym
* stab
= NULL
;
713 char* ehdrC
= (char*)(oc
->image
);
714 Elf_Ehdr
* ehdr
= (Elf_Ehdr
*) ehdrC
;
715 Elf_Shdr
* shdr
= (Elf_Shdr
*) (ehdrC
+ ehdr
->e_shoff
);
716 char* sh_strtab
= ehdrC
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
718 /* first find "the" symbol table */
719 stab
= (Elf_Sym
*) findElfSection ( ehdrC
, SHT_SYMTAB
);
721 /* also go find the string table */
722 strtab
= findElfSection ( ehdrC
, SHT_STRTAB
);
724 if (stab
== NULL
|| strtab
== NULL
) {
725 fprintf(stderr
,"%s: can't find string or symbol table\n", oc
->fileName
);
729 /* Process the relocation sections. */
730 for (shnum
= 0; shnum
< ehdr
->e_shnum
; shnum
++) {
732 /* Skip sections called ".rel.stab". These appear to contain
733 relocation entries that, when done, make the stabs debugging
734 info point at the right places. We ain't interested in all
736 if (0 == memcmp(".rel.stab", sh_strtab
+ shdr
[shnum
].sh_name
, 9))
739 if (shdr
[shnum
].sh_type
== SHT_REL
) {
740 ok
= do_Elf_Rel_relocations ( oc
, ehdrC
, shdr
,
741 shnum
, stab
, strtab
);
745 if (shdr
[shnum
].sh_type
== SHT_RELA
) {
746 ok
= do_Elf_Rela_relocations ( oc
, ehdrC
, shdr
,
747 shnum
, stab
, strtab
);
752 /* Free the local symbol table; we won't need it again. */
753 delete_StringMap(oc
->lochash
);
760 ///////////////////////////////////////////////////////////////////
761 ///////////////////////////////////////////////////////////////////
762 ///////////////////////////////////////////////////////////////////
767 ocVerifyImage_ELF ( ObjectCode
* oc
)
771 int i
, j
, nent
, nstrtab
, nsymtabs
;
775 char* ehdrC
= (char*)(oc
->image
);
776 Elf_Ehdr
* ehdr
= (Elf_Ehdr
*)ehdrC
;
778 if (ehdr
->e_ident
[EI_MAG0
] != ELFMAG0
||
779 ehdr
->e_ident
[EI_MAG1
] != ELFMAG1
||
780 ehdr
->e_ident
[EI_MAG2
] != ELFMAG2
||
781 ehdr
->e_ident
[EI_MAG3
] != ELFMAG3
) {
782 fprintf(stderr
,"%s: not an ELF object\n", oc
->fileName
);
786 if (ehdr
->e_ident
[EI_CLASS
] != ELFCLASS
) {
787 fprintf(stderr
,"%s: unsupported ELF format\n", oc
->fileName
);
791 if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2LSB
) {
793 fprintf(stderr
, "Is little-endian\n" );
795 if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2MSB
) {
797 fprintf(stderr
, "Is big-endian\n" );
799 fprintf(stderr
,"%s: unknown endiannness\n", oc
->fileName
);
803 if (ehdr
->e_type
!= ET_REL
) {
804 fprintf(stderr
,"%s: not a relocatable object (.o) file\n", oc
->fileName
);
808 fprintf(stderr
, "Is a relocatable object (.o) file\n" );
811 fprintf(stderr
, "Architecture is " );
812 switch (ehdr
->e_machine
) {
813 case EM_386
: if (debug_linker
) fprintf(stderr
, "x86\n" ); break;
814 case EM_SPARC
: if (debug_linker
) fprintf(stderr
, "sparc\n" ); break;
815 case EM_ARM
: if (debug_linker
) fprintf(stderr
, "arm\n" ); break;
817 case EM_IA_64
: if (debug_linker
) fprintf(stderr
, "ia64\n" ); break;
819 default: if (debug_linker
) fprintf(stderr
, "unknown\n" );
820 fprintf(stderr
,"%s: unknown architecture\n", oc
->fileName
);
824 if (debug_linker
>1) fprintf(stderr
,
825 "\nSection header table: start %d, n_entries %d, ent_size %d\n",
826 ehdr
->e_shoff
, ehdr
->e_shnum
, ehdr
->e_shentsize
);
828 assert (ehdr
->e_shentsize
== sizeof(Elf_Shdr
));
830 shdr
= (Elf_Shdr
*) (ehdrC
+ ehdr
->e_shoff
);
832 if (ehdr
->e_shstrndx
== SHN_UNDEF
) {
833 fprintf(stderr
,"%s: no section header string table\n", oc
->fileName
);
837 fprintf(stderr
, "Section header string table is section %d\n",
839 sh_strtab
= ehdrC
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
842 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
843 if (debug_linker
>1) fprintf(stderr
, "%2d: ", i
);
844 if (debug_linker
>1) fprintf(stderr
, "type=%2d ", (int)shdr
[i
].sh_type
);
845 if (debug_linker
>1) fprintf(stderr
, "size=%4d ", (int)shdr
[i
].sh_size
);
846 if (debug_linker
>1) fprintf(stderr
, "offs=%4d ", (int)shdr
[i
].sh_offset
);
847 if (debug_linker
>1) fprintf(stderr
, " (%p .. %p) ",
848 ehdrC
+ shdr
[i
].sh_offset
,
849 ehdrC
+ shdr
[i
].sh_offset
+ shdr
[i
].sh_size
- 1);
851 if (shdr
[i
].sh_type
== SHT_REL
) {
852 if (debug_linker
>1) fprintf(stderr
, "Rel " );
853 } else if (shdr
[i
].sh_type
== SHT_RELA
) {
854 if (debug_linker
>1) fprintf(stderr
, "RelA " );
856 if (debug_linker
>1) fprintf(stderr
," ");
859 if (debug_linker
>1) fprintf(stderr
, "sname=%s\n",
860 sh_strtab
+ shdr
[i
].sh_name
);
864 if (debug_linker
>1) fprintf(stderr
, "\nString tables\n" );
867 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
868 if (shdr
[i
].sh_type
== SHT_STRTAB
869 /* Ignore the section header's string table. */
870 && i
!= ehdr
->e_shstrndx
871 /* Ignore string tables named .stabstr, as they contain
873 && 0 != memcmp(".stabstr", sh_strtab
+ shdr
[i
].sh_name
, 8)
876 fprintf(stderr
," section %d is a normal string table\n", i
);
877 strtab
= ehdrC
+ shdr
[i
].sh_offset
;
882 fprintf(stderr
,"%s: no string tables, or too many\n", oc
->fileName
);
887 if (debug_linker
>1) fprintf(stderr
, "\nSymbol tables\n" );
888 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
889 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) continue;
890 if (debug_linker
>1) fprintf(stderr
, "section %d is a symbol table\n", i
);
892 stab
= (Elf_Sym
*) (ehdrC
+ shdr
[i
].sh_offset
);
893 nent
= shdr
[i
].sh_size
/ sizeof(Elf_Sym
);
894 if (debug_linker
>1) fprintf(stderr
,
895 " number of entries is apparently %d (%d rem)\n",
897 shdr
[i
].sh_size
% sizeof(Elf_Sym
)
899 if (0 != shdr
[i
].sh_size
% sizeof(Elf_Sym
)) {
900 fprintf(stderr
,"%s: non-integral number of symbol table entries\n",
904 for (j
= 0; j
< nent
; j
++) {
905 if (debug_linker
>1) fprintf(stderr
, " %2d ", j
);
906 if (debug_linker
>1) fprintf(stderr
, " sec=%-5d size=%-3d val=%5p ",
907 (int)stab
[j
].st_shndx
,
908 (int)stab
[j
].st_size
,
909 (char*)stab
[j
].st_value
);
911 if (debug_linker
>1) fprintf(stderr
, "type=" );
912 switch (ELF_ST_TYPE(stab
[j
].st_info
)) {
913 case STT_NOTYPE
: if (debug_linker
>1) fprintf(stderr
, "notype " ); break;
914 case STT_OBJECT
: if (debug_linker
>1) fprintf(stderr
, "object " ); break;
915 case STT_FUNC
: if (debug_linker
>1) fprintf(stderr
, "func " ); break;
916 case STT_SECTION
: if (debug_linker
>1) fprintf(stderr
, "section" ); break;
917 case STT_FILE
: if (debug_linker
>1) fprintf(stderr
, "file " ); break;
918 default: if (debug_linker
>1) fprintf(stderr
, "? " ); break;
920 if (debug_linker
>1) fprintf(stderr
, " " );
922 if (debug_linker
>1) fprintf(stderr
, "bind=" );
923 switch (ELF_ST_BIND(stab
[j
].st_info
)) {
924 case STB_LOCAL
: if (debug_linker
>1) fprintf(stderr
, "local " ); break;
925 case STB_GLOBAL
: if (debug_linker
>1) fprintf(stderr
, "global" ); break;
926 case STB_WEAK
: if (debug_linker
>1) fprintf(stderr
, "weak " ); break;
927 default: if (debug_linker
>1) fprintf(stderr
, "? " ); break;
929 if (debug_linker
>1) fprintf(stderr
, " " );
931 if (debug_linker
>1) fprintf(stderr
, "name=%s\n", strtab
+ stab
[j
].st_name
);
936 fprintf(stderr
,"%s: didn't find any symbol tables\n", oc
->fileName
);
944 ///////////////////////////////////////////////////////////////////
945 ///////////////////////////////////////////////////////////////////
946 ///////////////////////////////////////////////////////////////////
951 ocGetNames_ELF ( ObjectCode
* oc
)
956 char* ehdrC
= (char*)(oc
->image
);
957 Elf_Ehdr
* ehdr
= (Elf_Ehdr
*)ehdrC
;
958 char* strtab
= findElfSection ( ehdrC
, SHT_STRTAB
);
959 Elf_Shdr
* shdr
= (Elf_Shdr
*) (ehdrC
+ ehdr
->e_shoff
);
961 char* sh_strtab
= ehdrC
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
964 assert(global_symbol_table
!= NULL
);
967 fprintf(stderr
,"%s: no strtab\n", oc
->fileName
);
972 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
973 /* Figure out what kind of section it is. Logic derived from
974 Figure 1.14 ("Special Sections") of the ELF document
975 ("Portable Formats Specification, Version 1.1"). */
976 Elf_Shdr hdr
= shdr
[i
];
977 SectionKind kind
= SECTIONKIND_OTHER
;
980 if (hdr
.sh_type
== SHT_PROGBITS
981 && (hdr
.sh_flags
& SHF_ALLOC
) && (hdr
.sh_flags
& SHF_EXECINSTR
)) {
982 /* .text-style section */
983 kind
= SECTIONKIND_CODE_OR_RODATA
;
986 if (hdr
.sh_type
== SHT_PROGBITS
987 && (hdr
.sh_flags
& SHF_ALLOC
) && (hdr
.sh_flags
& SHF_WRITE
)) {
988 /* .data-style section */
989 kind
= SECTIONKIND_RWDATA
;
992 if (hdr
.sh_type
== SHT_PROGBITS
993 && (hdr
.sh_flags
& SHF_ALLOC
) && !(hdr
.sh_flags
& SHF_WRITE
)) {
994 /* .rodata-style section */
995 kind
= SECTIONKIND_CODE_OR_RODATA
;
998 if (hdr
.sh_type
== SHT_NOBITS
999 && (hdr
.sh_flags
& SHF_ALLOC
) && (hdr
.sh_flags
& SHF_WRITE
)) {
1000 /* .bss-style section */
1001 kind
= SECTIONKIND_RWDATA
;
1005 if (is_bss
&& shdr
[i
].sh_size
> 0) {
1006 /* This is a non-empty .bss section. Allocate zeroed space for
1007 it, and set its .sh_offset field such that
1008 ehdrC + .sh_offset == addr_of_zeroed_space. */
1009 char* zspace
= calloc(1, shdr
[i
].sh_size
);
1010 shdr
[i
].sh_offset
= ((char*)zspace
) - ((char*)ehdrC
);
1012 fprintf(stderr, "BSS section at 0x%x, size %d\n",
1013 zspace, shdr[i].sh_size);
1017 /* When loading objects compiled with -g, it seems there are
1018 relocations in various debug-info sections. So we'd better
1019 tell addProddableBlock to allow those bits to be prodded. */
1020 //fprintf(stderr, "ZZZZZZZZZZ %s\n", sh_strtab + hdr.sh_name);
1021 sec_name
= sh_strtab
+ shdr
[i
].sh_name
;
1022 if (kind
== SECTIONKIND_OTHER
1023 && (0 == strcmp(".debug_info", sec_name
)
1024 || 0 == strcmp(".debug_line", sec_name
)
1025 || 0 == strcmp(".debug_pubnames", sec_name
)
1026 || 0 == strcmp(".debug_aranges", sec_name
)
1027 || 0 == strcmp(".debug_frame", sec_name
))) {
1028 kind
= SECTIONKIND_CODE_OR_RODATA
;
1031 /* fill in the section info */
1032 if (kind
!= SECTIONKIND_OTHER
&& shdr
[i
].sh_size
> 0) {
1033 addProddableBlock(oc
, ehdrC
+ shdr
[i
].sh_offset
, shdr
[i
].sh_size
);
1034 //addSection(oc, kind, ehdrC + shdr[i].sh_offset,
1035 // ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
1038 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) continue;
1040 /* copy stuff into this module's object symbol table */
1041 stab
= (Elf_Sym
*) (ehdrC
+ shdr
[i
].sh_offset
);
1042 nent
= shdr
[i
].sh_size
/ sizeof(Elf_Sym
);
1044 oc
->n_symbols
= nent
;
1045 oc
->symbols
= malloc(oc
->n_symbols
* sizeof(char*));
1047 for (j
= 0; j
< nent
; j
++) {
1049 char isLocal
= FALSE
; /* avoids uninit-var warning */
1051 char* nm
= strtab
+ stab
[j
].st_name
;
1052 int secno
= stab
[j
].st_shndx
;
1054 /* Figure out if we want to add it; if so, set ad to its
1055 address. Otherwise leave ad == NULL. */
1057 if (secno
== SHN_COMMON
) {
1059 ad
= calloc(1, stab
[j
].st_size
);
1061 fprintf(stderr, "COMMON symbol, size %d name %s\n",
1062 stab[j].st_size, nm);
1064 /* Pointless to do addProddableBlock() for this area,
1065 since the linker should never poke around in it. */
1068 if ( ( ELF_ST_BIND(stab
[j
].st_info
)==STB_GLOBAL
1069 || ELF_ST_BIND(stab
[j
].st_info
)==STB_LOCAL
1071 /* and not an undefined symbol */
1072 && stab
[j
].st_shndx
!= SHN_UNDEF
1073 /* and not in a "special section" */
1074 && stab
[j
].st_shndx
< SHN_LORESERVE
1076 /* and it's a not a section or string table or anything silly */
1077 ( ELF_ST_TYPE(stab
[j
].st_info
)==STT_FUNC
||
1078 ELF_ST_TYPE(stab
[j
].st_info
)==STT_OBJECT
||
1079 ELF_ST_TYPE(stab
[j
].st_info
)==STT_NOTYPE
1082 /* Section 0 is the undefined section, hence > and not >=. */
1083 assert(secno
> 0 && secno
< ehdr
->e_shnum
);
1085 if (shdr[secno].sh_type == SHT_NOBITS) {
1086 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
1087 stab[j].st_size, stab[j].st_value, nm);
1090 ad
= ehdrC
+ shdr
[ secno
].sh_offset
+ stab
[j
].st_value
;
1091 if (ELF_ST_BIND(stab
[j
].st_info
)==STB_LOCAL
) {
1094 #ifdef ELF_FUNCTION_DESC
1095 /* dlsym() and the initialisation table both give us function
1096 * descriptors, so to be consistent we store function descriptors
1097 * in the symbol table */
1098 if (ELF_ST_TYPE(stab
[j
].st_info
) == STT_FUNC
)
1099 ad
= (char *)allocateFunctionDesc((Elf_Addr
)ad
);
1102 fprintf(stderr
, "addOTabName(GLOB): %10p %s %s\n",
1103 ad
, oc
->fileName
, nm
);
1108 /* And the decision is ... */
1112 oc
->symbols
[j
] = nm
;
1115 /* Ignore entirely. */
1117 //ghciInsertStrHashTable(oc->fileName, global_symbol_table, nm, ad);
1118 paranoid_addto_StringMap(global_symbol_table
, nm
, ad
);
1122 if (debug_linker
>1) fprintf(stderr
, "skipping `%s'\n",
1123 strtab
+ stab
[j
].st_name
);
1126 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1127 (int)ELF_ST_BIND(stab[j].st_info),
1128 (int)ELF_ST_TYPE(stab[j].st_info),
1129 (int)stab[j].st_shndx,
1130 strtab + stab[j].st_name
1133 oc
->symbols
[j
] = NULL
;
1143 ///////////////////////////////////////////////////////////////////
1144 ///////////////////////////////////////////////////////////////////
1145 ///////////////////////////////////////////////////////////////////
1147 // TOP-LEVEL CONTROL OF THE LINKER
1150 /* ---------------------------------------------------------------------
1151 * Load an obj (populate the global symbol table, but don't resolve yet)
1153 * Returns: 1 if ok, 0 on error.
1156 int loadObj( char *path
)
1166 fprintf(stderr
, "==== loadObj %s ====\n", path
);
1168 /* Check that we haven't already loaded this object. */
1172 for (o
= global_object_list
; o
; o
= o
->next
) {
1173 if (0 == strcmp(o
->fileName
, path
))
1179 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1180 "same object file twice:\n"
1187 oc
= malloc(sizeof(ObjectCode
));
1189 oc
->formatName
= "ELF";
1191 r
= stat(path
, &st
);
1192 if (r
== -1) { return 0; }
1194 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1195 oc
->fileName
= malloc( strlen(path
)+1 );
1196 strcpy(oc
->fileName
, path
);
1198 oc
->fileSize
= st
.st_size
;
1200 oc
->sections
= NULL
;
1201 oc
->lochash
= new_StringMap();
1202 oc
->proddables
= NULL
;
1207 /* chain it onto the list of objects */
1208 oc
->next
= global_object_list
;
1209 global_object_list
= oc
;
1211 fd
= open(path
, O_RDONLY
);
1213 fprintf(stderr
,"loadObj: can't open `%s'\n", path
);
1217 /* Allocate a 1-page area just prior to the image, so we can put
1218 fixup code fragments there. Used for doing R_ARM_PC24
1219 relocations for jump distances > 64M. */
1221 pagesize
= getpagesize();
1222 p
= memalign(pagesize
, N_FIXUP_PAGES
* pagesize
1226 fprintf(stderr
,"loadObj: failed to allocate space for `%s'\n", path
);
1231 oc
->fixup_size
= N_FIXUP_PAGES
* pagesize
;
1233 oc
->image
= &(p
[ oc
->fixup_size
]);
1235 r
= read(fd
, oc
->image
, oc
->fileSize
);
1236 if (r
!= oc
->fileSize
) {
1237 fprintf(stderr
,"loadObj: failed to read `%s'\n", path
);
1241 fprintf(stderr
, "loaded %s at %p (fixup = %p)\n",
1242 oc
->fileName
, oc
->image
, oc
->fixup
);
1246 /* verify the in-memory image */
1247 r
= ocVerifyImage_ELF ( oc
);
1248 if (!r
) { return r
; }
1250 /* build the symbol list for this image */
1251 r
= ocGetNames_ELF ( oc
);
1252 if (!r
) { return r
; }
1254 /* loaded, but not resolved yet */
1255 oc
->status
= OBJECT_LOADED
;
1262 /* ---------------------------------------------------------------------------
1263 * resolve all the currently unlinked objects in memory
1265 * Returns: 1 if ok, 0 on error.
1268 int resolveObjs( void )
1275 for (oc
= global_object_list
; oc
; oc
= oc
->next
) {
1276 if (oc
->status
!= OBJECT_RESOLVED
) {
1277 r
= ocResolve_ELF ( oc
);
1278 if (!r
) { return r
; }
1279 oc
->status
= OBJECT_RESOLVED
;
1286 /* ---------------------------------------------------------------------------
1290 /* Load and link a bunch of .o's, and return the address of
1291 'main'. Or NULL if something borks.
1293 void* linker_top_level_LINK ( int n_object_names
, char** object_names
)
1299 for (i
= 0; i
< n_object_names
; i
++) {
1300 //fprintf(stderr, "linkloop %d %s\n", i, object_names[i] );
1301 r
= loadObj( object_names
[i
] );
1302 if (r
!= 1) return NULL
;
1305 if (r
!= 1) return NULL
;
1306 mainp
= search_StringMap ( global_symbol_table
, "main" );
1307 if (mainp
== NULL
) return NULL
;
1308 printf("Linker: success!\n");
1314 int main ( int argc
, char** argv
)
1317 linker_top_level_LINK( argc
- 1 , &argv
[1]);
1318 /* find and run "main" */
1320 mainp
= search_StringMap ( global_symbol_table
, "main" );
1321 if (mainp
== NULL
) {
1322 fprintf(stderr
, "no binding for main\n");
1326 printf("\nSTARTING PROGRAM\n");
1327 ( (int(*)(int,char**)) mainp
) (argc
,argv
);
1328 printf("FINISHED\n");
1334 ////////////////////////////////////////////////////////////////////////////
1335 ////////////////////////////////////////////////////////////////////////////
1336 ////////////////////////////////////////////////////////////////////////////
1337 ////////////////////////////////////////////////////////////////////////////
1338 ////////////////////////////////////////////////////////////////////////////
1339 ////////////////////////////////////////////////////////////////////////////
1341 // VIRTUAL MACHINE ...
1343 /* --------------------------------------------------------- */
1344 /* SIMULATED STATE */
1345 /* --------------------------------------------------------- */
1347 typedef unsigned int Word
;
1349 /* Stack for the simulation */
1352 /* Stop when we get a jump to here. */
1357 /* r0 .. r15, flags */
1358 Word regs_arm
[16+1];
1364 //---------------------------------------------
1366 /* Calling convention: enter the translation with r0 pointing at
1367 regs_arm. Translation may trash r1 .. r12 inclusive. Translation
1368 should update all regs in regs_arm, and put the next pc value
1369 in regs_arm[REG_PC]. */
1372 void run_translation ( char* trans
, char* baseblock
)
1374 /* r0 holds trans */
1376 ("stmfd sp!, {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13}\n\t"
1380 "ldmea sp!, {r0,r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12,r13}\n\t"
1382 : "ir" (trans
), "ir" (baseblock
) );
1388 /* Called by Haskell to initialise the simulated machine. The
1389 supplied address is the entry point of some procedure to call. */
1392 void initialise_machine ( char* first_pc
)
1394 static char start
[12];
1395 Word w
= (Word
)first_pc
;
1397 n_transtab_used
= 0;
1399 sim_stack
= malloc(10000 * sizeof(Word
));
1400 regs_arm
[REG_SP
] = (Word
)(&sim_stack
[9999]);
1402 regs_arm
[REG_PC
] = (Word
)first_pc
;
1404 /* Generate this. Note, we'll be returning directly to the
1405 data, so the JIT must stop at this point! */
1407 3 0000 00C09FE5 ldr ip, value
1408 4 0004 FEFFFFEB bl ip
1410 6 0008 44332211 .word 0x11223344
1412 start
[0] = 0x00; start
[1] = 0xC0; start
[2] = 0x9F; start
[3] = 0xE5;
1413 start
[4] = 0xFE; start
[5] = 0xFF; start
[6] = 0xFF; start
[7] = 0xEB;
1414 start
[8] = w
& 0xFF; w
>>= 8;
1415 start
[9] = w
& 0xFF; w
>>= 8;
1416 start
[10] = w
& 0xFF; w
>>= 8;
1417 start
[11] = w
& 0xFF; w
>>= 8;
1419 stop_at
= &start
[8];
1420 arm_notify_new_code(stop_at
, 12);