3 Linker no longer used - apart from mymalloc().
4 Instead, simply compile and link switchback.c with test_xxx.c, e.g.:
5 ./> (cd .. && make EXTRA_CFLAGS="-m64" libvex_ppc64_linux.a) && gcc -m64 -Wall -O -g -o switchback switchback.c linker.c ../libvex_ppc64_linux.a test_bzip2.c
12 #include <sys/types.h>
22 #include "../pub/libvex_basictypes.h"
25 #define IF_DEBUG(x,y) /* */
26 static int debug_linker
= 0;
30 #if defined(__x86_64__)
31 # define x86_64_TARGET_ARCH
32 #elif defined(__i386__)
33 # define i386_TARGET_ARCH
34 #elif defined (__powerpc__)
35 # define ppc32_TARGET_ARCH
36 #elif defined(__aarch64__)
37 # define arm64_TARGET_ARCH
39 # error "Unknown arch"
44 #define CALLOC_MAX 10000000
45 static HChar calloc_area
[CALLOC_MAX
];
46 static UInt calloc_used
= 0;
47 static void* calloc_below2G ( Int n
, Int m
)
51 while ((calloc_used
% 16) > 0) calloc_used
++;
52 assert(calloc_used
+ n
*m
< CALLOC_MAX
);
53 p
= &calloc_area
[calloc_used
];
54 for (i
= 0; i
< n
*m
; i
++)
55 calloc_area
[calloc_used
+i
] = 0;
61 #define MYMALLOC_MAX 50*1000*1000
62 static HChar mymalloc_area
[MYMALLOC_MAX
];
63 static UInt mymalloc_used
= 0;
64 void* mymalloc ( Int n
)
67 #if defined(__powerpc64__) || defined(__aarch64__)
68 while ((ULong
)(mymalloc_area
+mymalloc_used
) & 0xFFF)
70 while ((UInt
)(mymalloc_area
+mymalloc_used
) & 0xFFF)
73 assert(mymalloc_used
+n
< MYMALLOC_MAX
);
74 p
= (void*)(&mymalloc_area
[mymalloc_used
]);
76 // printf("mymalloc(%d) = %p\n", n, p);
80 void myfree ( void* p
)
91 ///////////////////////////////////////////////////////////////////
92 ///////////////////////////////////////////////////////////////////
93 ///////////////////////////////////////////////////////////////////
100 typedef enum { OBJECT_LOADED
, OBJECT_RESOLVED
} OStatus
;
103 #define N_FIXUP_PAGES 1
106 /* Indication of section kinds for loaded objects. Needed by
107 the GC for deciding whether or not a pointer on the stack
111 enum { SECTIONKIND_CODE_OR_RODATA
,
114 SECTIONKIND_NOINFOAVAIL
}
122 struct _Section
* next
;
127 struct _ProddableBlock
{
130 struct _ProddableBlock
* next
;
134 /* Top-level structure for an object module. One of these is allocated
135 * for each object file in use.
137 typedef struct _ObjectCode
{
141 char* formatName
; /* eg "ELF32", "DLL", "COFF", etc. */
143 /* An array containing ptrs to all the symbol names copied from
144 this object into the global symbol hash table. This is so that
145 we know which parts of the latter mapping to nuke when this
146 object is removed from the system. */
150 /* ptr to malloc'd lump of memory holding the obj file */
153 /* Fixup area for long-distance jumps. */
158 /* The section-kind entries for this object module. Linked
162 /* A private hash table for local symbols. */
163 /* HashTable* */ void* lochash
;
165 /* Allow a chain of these things */
166 struct _ObjectCode
* next
;
168 /* SANITY CHECK ONLY: a list of the only memory regions which may
169 safely be prodded during relocation. Any attempt to prod
170 outside one of these is an error in the linker. */
171 ProddableBlock
* proddables
;
176 * Define a set of types which can be used for both ELF32 and ELF64
179 #if VEX_HOST_WORDSIZE == 8
180 #define ELFCLASS ELFCLASS64
181 #define Elf_Addr Elf64_Addr
182 #define Elf_Word Elf64_Word
183 #define Elf_Sword Elf64_Sword
184 #define Elf_Ehdr Elf64_Ehdr
185 #define Elf_Phdr Elf64_Phdr
186 #define Elf_Shdr Elf64_Shdr
187 #define Elf_Sym Elf64_Sym
188 #define Elf_Rel Elf64_Rel
189 #define Elf_Rela Elf64_Rela
190 #define ELF_ST_TYPE ELF64_ST_TYPE
191 #define ELF_ST_BIND ELF64_ST_BIND
192 #define ELF_R_TYPE ELF64_R_TYPE
193 #define ELF_R_SYM ELF64_R_SYM
195 #define ELFCLASS ELFCLASS32
196 #define Elf_Addr Elf32_Addr
197 #define Elf_Word Elf32_Word
198 #define Elf_Sword Elf32_Sword
199 #define Elf_Ehdr Elf32_Ehdr
200 #define Elf_Phdr Elf32_Phdr
201 #define Elf_Shdr Elf32_Shdr
202 #define Elf_Sym Elf32_Sym
203 #define Elf_Rel Elf32_Rel
204 #define Elf_Rela Elf32_Rela
206 #define ELF_ST_TYPE ELF32_ST_TYPE
209 #define ELF_ST_BIND ELF32_ST_BIND
212 #define ELF_R_TYPE ELF32_R_TYPE
215 #define ELF_R_SYM ELF32_R_SYM
222 ///////////////////////////////////////////////////////////////////
223 ///////////////////////////////////////////////////////////////////
224 ///////////////////////////////////////////////////////////////////
228 /* -----------------------------------------------------------------------
229 * Sanity checking. For each ObjectCode, maintain a list of address ranges
230 * which may be prodded during relocation, and abort if we try and write
231 * outside any of these.
233 static void addProddableBlock ( ObjectCode
* oc
, void* start
, int size
)
236 = mymalloc(sizeof(ProddableBlock
));
238 fprintf(stderr
, "aPB oc=%p %p %d (%p .. %p)\n", oc
, start
, size
,
239 start
, ((char*)start
)+size
-1 );
243 pb
->next
= oc
->proddables
;
247 static void checkProddableBlock ( ObjectCode
* oc
, void* addr
)
250 for (pb
= oc
->proddables
; pb
!= NULL
; pb
= pb
->next
) {
251 char* s
= (char*)(pb
->start
);
252 char* e
= s
+ pb
->size
- 1;
253 char* a
= (char*)addr
;
254 /* Assumes that the biggest fixup involves a 4-byte write. This
255 probably needs to be changed to 8 (ie, +7) on 64-bit
257 if (a
>= s
&& (a
+3) <= e
) return;
260 "checkProddableBlock: invalid fixup %p in runtime linker\n",
267 ///////////////////////////////////////////////////////////////////
268 ///////////////////////////////////////////////////////////////////
269 ///////////////////////////////////////////////////////////////////
271 // String->Addr mappings
274 struct { char* mp_name
; void* mp_addr
; }
285 static StringMap
* new_StringMap ( void )
287 StringMap
* sm
= mymalloc(sizeof(StringMap
));
290 sm
->maplets
= mymalloc(10 * sizeof(Maplet
));
294 static void delete_StringMap ( StringMap
* sm
)
296 assert(sm
->maplets
!= NULL
);
302 static void ensure_StringMap ( StringMap
* sm
)
306 assert(sm
->maplets
!= NULL
);
307 if (sm
->sm_used
< sm
->sm_size
)
310 mp2
= mymalloc(sm
->sm_size
* sizeof(Maplet
));
311 for (i
= 0; i
< sm
->sm_used
; i
++)
312 mp2
[i
] = sm
->maplets
[i
];
317 static void* search_StringMap ( StringMap
* sm
, char* name
)
320 for (i
= 0; i
< sm
->sm_used
; i
++)
321 if (0 == strcmp(name
, sm
->maplets
[i
].mp_name
))
322 return sm
->maplets
[i
].mp_addr
;
326 static void addto_StringMap ( StringMap
* sm
, char* name
, void* addr
)
328 ensure_StringMap(sm
);
329 sm
->maplets
[sm
->sm_used
].mp_name
= name
;
330 sm
->maplets
[sm
->sm_used
].mp_addr
= addr
;
334 static void paranoid_addto_StringMap ( StringMap
* sm
, char* name
, void* addr
)
337 fprintf(stderr
, "paranoid_addto_StringMap(%s,%p)\n", name
, addr
);
338 if (search_StringMap(sm
,name
) != NULL
) {
339 fprintf(stderr
, "duplicate: paranoid_addto_StringMap(%s,%p)\n", name
, addr
);
342 addto_StringMap(sm
,name
,addr
);
346 ///////////////////////////////////////////////////////////////////
347 ///////////////////////////////////////////////////////////////////
348 ///////////////////////////////////////////////////////////////////
350 // Top-level linker control.
352 StringMap
* global_symbol_table
= NULL
;
353 ObjectCode
* global_object_list
= NULL
;
355 static void initLinker ( void )
357 if (global_symbol_table
!= NULL
)
359 global_symbol_table
= new_StringMap();
364 ///////////////////////////////////////////////////////////////////
365 ///////////////////////////////////////////////////////////////////
366 ///////////////////////////////////////////////////////////////////
370 /* -----------------------------------------------------------------
371 * lookup a symbol in the global symbol table
374 void * lookupSymbol( char *lbl
)
378 assert(global_symbol_table
!= NULL
);
379 val
= search_StringMap(global_symbol_table
, lbl
);
384 ///////////////////////////////////////////////////////////////////
385 ///////////////////////////////////////////////////////////////////
386 ///////////////////////////////////////////////////////////////////
391 * Generic ELF functions
395 findElfSection ( void* objImage
, Elf_Word sh_type
)
397 char* ehdrC
= (char*)objImage
;
398 Elf_Ehdr
* ehdr
= (Elf_Ehdr
*)ehdrC
;
399 Elf_Shdr
* shdr
= (Elf_Shdr
*)(ehdrC
+ ehdr
->e_shoff
);
400 char* sh_strtab
= ehdrC
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
404 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
405 if (shdr
[i
].sh_type
== sh_type
406 /* Ignore the section header's string table. */
407 && i
!= ehdr
->e_shstrndx
408 /* Ignore string tables named .stabstr, as they contain
410 && 0 != memcmp(".stabstr", sh_strtab
+ shdr
[i
].sh_name
, 8)
412 ptr
= ehdrC
+ shdr
[i
].sh_offset
;
419 #ifdef arm_TARGET_ARCH
421 char* alloc_fixup_bytes ( ObjectCode
* oc
, int nbytes
)
424 assert(nbytes
% 4 == 0);
426 res
= &(oc
->fixup
[oc
->fixup_used
]);
427 oc
->fixup_used
+= nbytes
;
428 if (oc
->fixup_used
>= oc
->fixup_size
) {
429 fprintf(stderr
, "fixup area too small for %s\n", oc
->fileName
);
437 ///////////////////////////////////////////////////////////////////
438 ///////////////////////////////////////////////////////////////////
439 ///////////////////////////////////////////////////////////////////
444 void* lookup_magic_hacks ( char* sym
)
446 if (0==strcmp(sym
, "printf")) return (void*)(&printf
);
450 #ifdef arm_TARGET_ARCH
452 void arm_notify_new_code ( char* start
, int length
)
454 __asm
__volatile ("mov r1, %0\n\t"
459 : "ir" (start
), "ir" (length
), "ir" (0) );
464 void gen_armle_goto ( char* fixup
, char* dstP
)
466 Elf_Word w
= (Elf_Word
)dstP
;
469 3 0000 04F01FE5 ldr pc, value
470 4 0004 44332211 value: .word 0x11223344
472 fprintf(stderr
,"at %p generating jump to %p\n", fixup
, dstP
);
473 fixup
[0] = 0x04; fixup
[1] = 0xF0; fixup
[2] = 0x1F; fixup
[3] = 0xE5;
474 fixup
[4] = w
& 0xFF; w
>>= 8;
475 fixup
[5] = w
& 0xFF; w
>>= 8;
476 fixup
[6] = w
& 0xFF; w
>>= 8;
477 fixup
[7] = w
& 0xFF; w
>>= 8;
478 arm_notify_new_code(fixup
, 8);
480 #endif /* arm_TARGET_ARCH */
483 #ifdef ppc32_TARGET_ARCH
484 static void invalidate_icache(void *ptr
, int nbytes
)
486 unsigned long startaddr
= (unsigned long) ptr
;
487 unsigned long endaddr
= startaddr
+ nbytes
;
489 unsigned long cls
= 16; //VG_(cache_line_size);
491 startaddr
&= ~(cls
- 1);
492 for (addr
= startaddr
; addr
< endaddr
; addr
+= cls
)
493 asm volatile("dcbst 0,%0" : : "r" (addr
));
494 asm volatile("sync");
495 for (addr
= startaddr
; addr
< endaddr
; addr
+= cls
)
496 asm volatile("icbi 0,%0" : : "r" (addr
));
497 asm volatile("sync; isync");
500 static UInt
compute_ppc_HA ( UInt x
) {
501 return 0xFFFF & ( (x
>> 16) + ((x
& 0x8000) ? 1 : 0) );
503 static UInt
compute_ppc_LO ( UInt x
) {
506 static UInt
compute_ppc_HI ( UInt x
) {
507 return 0xFFFF & (x
>> 16);
509 #endif /* ppc32_TARGET_ARCH */
512 /* Do ELF relocations which lack an explicit addend. All x86-linux
513 relocations appear to be of this form. */
515 do_Elf_Rel_relocations ( ObjectCode
* oc
, char* ehdrC
,
516 Elf_Shdr
* shdr
, int shnum
,
517 Elf_Sym
* stab
, char* strtab
)
522 Elf_Rel
* rtab
= (Elf_Rel
*) (ehdrC
+ shdr
[shnum
].sh_offset
);
523 int nent
= shdr
[shnum
].sh_size
/ sizeof(Elf_Rel
);
524 int target_shndx
= shdr
[shnum
].sh_info
;
525 int symtab_shndx
= shdr
[shnum
].sh_link
;
527 stab
= (Elf_Sym
*) (ehdrC
+ shdr
[ symtab_shndx
].sh_offset
);
528 targ
= (Elf_Word
*)(ehdrC
+ shdr
[ target_shndx
].sh_offset
);
529 IF_DEBUG(linker
,belch( "relocations for section %d using symtab %d",
530 target_shndx
, symtab_shndx
));
532 for (j
= 0; j
< nent
; j
++) {
533 Elf_Addr offset
= rtab
[j
].r_offset
;
534 Elf_Addr info
= rtab
[j
].r_info
;
536 Elf_Addr P
= ((Elf_Addr
)targ
) + offset
;
537 Elf_Word
* pP
= (Elf_Word
*)P
;
542 IF_DEBUG(linker
,belch( "Rel entry %3d is raw(%6p %6p)",
543 j
, (void*)offset
, (void*)info
));
545 IF_DEBUG(linker
,belch( " ZERO" ));
548 Elf_Sym sym
= stab
[ELF_R_SYM(info
)];
549 /* First see if it is a local symbol. */
550 if (ELF_ST_BIND(sym
.st_info
) == STB_LOCAL
) {
551 /* Yes, so we can get the address directly from the ELF symbol
553 symbol
= sym
.st_name
==0 ? "(noname)" : strtab
+sym
.st_name
;
555 (ehdrC
+ shdr
[ sym
.st_shndx
].sh_offset
556 + stab
[ELF_R_SYM(info
)].st_value
);
559 /* No, so look up the name in our global table. */
560 symbol
= strtab
+ sym
.st_name
;
561 S
= (Elf_Addr
)lookupSymbol( symbol
);
564 S
= (Elf_Addr
)lookup_magic_hacks(symbol
);
567 fprintf(stderr
,"%s: unknown symbol `%s'\n",
568 oc
->fileName
, symbol
);
572 fprintf(stderr
, "\n`%s' resolves to %p\n", symbol
, (void*)S
);
576 fprintf(stderr
, "Reloc: P = %p S = %p A = %p\n",
577 (void*)P
, (void*)S
, (void*)A
);
578 checkProddableBlock ( oc
, pP
);
582 switch (ELF_R_TYPE(info
)) {
583 # ifdef i386_TARGET_ARCH
584 case R_386_32
: *pP
= value
; break;
585 case R_386_PC32
: *pP
= value
- P
; break;
587 # ifdef arm_TARGET_ARCH
589 Elf_Word w
, delta
, deltaTop8
;
590 /* Generate a jump sequence into the fixup area
591 and branch to that instead. */
592 char* fixup
= alloc_fixup_bytes(oc
, 8);
593 /* First of all, figure out where we're really trying to
595 // compensate for pc+8 bias
596 Elf_Word real_dst
= (A
& 0x00FFFFFF) + 2;
597 // sign-extend 24-to-32 of real_dst
598 if (real_dst
& 0x00800000)
599 real_dst
|= 0xFF000000;
601 real_dst
&= 0x00FFFFFF;
606 gen_armle_goto(fixup
, (char*)real_dst
);
608 /* Delta is in bytes .. */
609 delta
= (((Elf_Word
)fixup
) - ((Elf_Word
)pP
) - 8);
610 deltaTop8
= (delta
>> 24) & 0xFF;
611 if (deltaTop8
!= 0 && deltaTop8
!= 0xFF) {
612 fprintf(stderr
,"R_ARM_PC24: out of range delta 0x%x for %s\n",
619 w
|= (0x00FFFFFF & delta
);
629 "%s: unhandled ELF relocation(Rel) type %d\n\n",
630 oc
->fileName
, (Int
)ELF_R_TYPE(info
));
638 /* Do ELF relocations for which explicit addends are supplied.
639 sparc-solaris relocations appear to be of this form. */
641 do_Elf_Rela_relocations ( ObjectCode
* oc
, char* ehdrC
,
642 Elf_Shdr
* shdr
, int shnum
,
643 Elf_Sym
* stab
, char* strtab
)
648 Elf_Rela
* rtab
= (Elf_Rela
*) (ehdrC
+ shdr
[shnum
].sh_offset
);
649 int nent
= shdr
[shnum
].sh_size
/ sizeof(Elf_Rela
);
650 int target_shndx
= shdr
[shnum
].sh_info
;
651 int symtab_shndx
= shdr
[shnum
].sh_link
;
653 stab
= (Elf_Sym
*) (ehdrC
+ shdr
[ symtab_shndx
].sh_offset
);
654 targ
= (Elf_Addr
) (ehdrC
+ shdr
[ target_shndx
].sh_offset
);
655 IF_DEBUG(linker
,belch( "relocations for section %d using symtab %d",
656 target_shndx
, symtab_shndx
));
658 for (j
= 0; j
< nent
; j
++) {
659 #if defined(DEBUG) || defined(sparc_TARGET_ARCH) \
660 || defined(ia64_TARGET_ARCH) \
661 || defined(x86_64_TARGET_ARCH) \
662 || defined(ppc32_TARGET_ARCH)
663 /* This #ifdef only serves to avoid unused-var warnings. */
664 Elf_Addr offset
= rtab
[j
].r_offset
;
665 Elf_Addr P
= targ
+ offset
;
667 Elf_Addr info
= rtab
[j
].r_info
;
668 Elf_Addr A
= rtab
[j
].r_addend
;
671 # if defined(sparc_TARGET_ARCH)
672 Elf_Word
* pP
= (Elf_Word
*)P
;
675 # if defined(ia64_TARGET_ARCH)
676 Elf64_Xword
*pP
= (Elf64_Xword
*)P
;
679 # if defined(x86_64_TARGET_ARCH)
680 ULong
* pP
= (ULong
*)P
;
682 # if defined(ppc32_TARGET_ARCH)
684 Elf_Word
* pP
= (Elf_Word
*)P
;
687 IF_DEBUG(linker
,belch( "Rel entry %3d is raw(%6p %6p %6p) ",
688 j
, (void*)offset
, (void*)info
,
691 IF_DEBUG(linker
,belch( " ZERO" ));
694 Elf_Sym sym
= stab
[ELF_R_SYM(info
)];
695 /* First see if it is a local symbol. */
696 if (ELF_ST_BIND(sym
.st_info
) == STB_LOCAL
) {
697 /* Yes, so we can get the address directly from the ELF symbol
699 symbol
= sym
.st_name
==0 ? "(noname)" : strtab
+sym
.st_name
;
701 (ehdrC
+ shdr
[ sym
.st_shndx
].sh_offset
702 + stab
[ELF_R_SYM(info
)].st_value
);
703 #ifdef ELF_FUNCTION_DESC
704 /* Make a function descriptor for this function */
705 if (S
&& ELF_ST_TYPE(sym
.st_info
) == STT_FUNC
) {
706 S
= allocateFunctionDesc(S
+ A
);
711 /* No, so look up the name in our global table. */
712 symbol
= strtab
+ sym
.st_name
;
713 S
= (Elf_Addr
)lookupSymbol( symbol
);
715 #ifdef ELF_FUNCTION_DESC
716 /* If a function, already a function descriptor - we would
717 have to copy it to add an offset. */
718 if (S
&& (ELF_ST_TYPE(sym
.st_info
) == STT_FUNC
) && (A
!= 0))
719 belch("%s: function %s with addend %p", oc
->fileName
, symbol
, (void *)A
);
723 fprintf(stderr
,"%s: unknown symbol `%s'\n", oc
->fileName
, symbol
);
727 fprintf(stderr
, "`%s' resolves to %p\n", symbol
, (void*)S
);
731 fprintf ( stderr
, "Reloc: offset = %p P = %p S = %p A = %p\n",
732 (void*)offset
, (void*)P
, (void*)S
, (void*)A
);
735 /* checkProddableBlock ( oc, (void*)P ); */
739 switch (ELF_R_TYPE(info
)) {
740 # if defined(sparc_TARGET_ARCH)
741 case R_SPARC_WDISP30
:
742 w1
= *pP
& 0xC0000000;
743 w2
= (Elf_Word
)((value
- P
) >> 2);
744 ASSERT((w2
& 0xC0000000) == 0);
749 w1
= *pP
& 0xFFC00000;
750 w2
= (Elf_Word
)(value
>> 10);
751 ASSERT((w2
& 0xFFC00000) == 0);
757 w2
= (Elf_Word
)(value
& 0x3FF);
758 ASSERT((w2
& ~0x3FF) == 0);
762 /* According to the Sun documentation:
764 This relocation type resembles R_SPARC_32, except it refers to an
765 unaligned word. That is, the word to be relocated must be treated
766 as four separate bytes with arbitrary alignment, not as a word
767 aligned according to the architecture requirements.
769 (JRS: which means that freeloading on the R_SPARC_32 case
770 is probably wrong, but hey ...)
774 w2
= (Elf_Word
)value
;
778 # if defined(ia64_TARGET_ARCH)
779 case R_IA64_DIR64LSB
:
780 case R_IA64_FPTR64LSB
:
783 case R_IA64_PCREL64LSB
:
786 case R_IA64_SEGREL64LSB
:
787 addr
= findElfSegment(ehdrC
, value
);
791 ia64_reloc_gprel22(P
, value
);
794 case R_IA64_LTOFF22X
:
795 case R_IA64_LTOFF_FPTR22
:
796 addr
= allocateGOTEntry(value
);
797 ia64_reloc_gprel22(P
, addr
);
799 case R_IA64_PCREL21B
:
800 ia64_reloc_pcrel21(P
, S
, oc
);
803 /* This goes with R_IA64_LTOFF22X and points to the load to
804 convert into a move. We don't implement relaxation. */
807 # if defined(x86_64_TARGET_ARCH)
808 case R_X86_64_64
: /* 1 *//* Direct 64 bit */
809 *((ULong
*)pP
) = (ULong
)(S
+ A
);
811 case R_X86_64_PC32
: /* 2 *//* PC relative 32 bit signed */
812 *((UInt
*)pP
) = (UInt
)(S
+ A
- P
);
814 case R_X86_64_32
: /* 10 *//* Direct 32 bit zero extended */
815 *((UInt
*)pP
) = (UInt
)(S
+ A
);
817 case R_X86_64_32S
: /* 11 *//* Direct 32 bit sign extended */
818 *((UInt
*)pP
) = (UInt
)(S
+ A
);
821 # if defined(ppc32_TARGET_ARCH)
822 case R_PPC_ADDR32
: /* 1 *//* 32bit absolute address */
824 invalidate_icache(pP
,4);
826 case R_PPC_ADDR16_LO
: /* 4 *//* lower 16bit of absolute address */
827 *((UInt
*)pP
) &= 0x0000FFFF;
828 *((UInt
*)pP
) |= 0xFFFF0000 & (compute_ppc_LO(S
+A
) << 16);
829 invalidate_icache(pP
,4);
831 case R_PPC_ADDR16_HA
: /* 6 *//* adjusted high 16bit */
832 *((UInt
*)pP
) &= 0x0000FFFF;
833 *((UInt
*)pP
) |= 0xFFFF0000 & (compute_ppc_HA(S
+A
) << 16);
834 invalidate_icache(pP
,4);
836 case R_PPC_REL24
: /* 10 *//* PC relative 26 bit */
839 /* the top 9 bits of sI must be the same (all 0s or
840 all 1s) for this to be valid; else we have to fail. */
841 sI2
= sI
>> 23; /* 23 == 32 - 9 */
842 if (sI2
!= 0 && sI2
!= 0xFFFFFFFF) {
843 fprintf(stderr
, "%s: R_PPC_REL24 relocation failed\n", oc
->fileName
);
846 *((UInt
*)pP
) &= ~(0x00FFFFFF << 2);
847 *((UInt
*)pP
) |= (0xFFFFFF & sI
) << 2;
848 invalidate_icache(pP
,4);
850 case R_PPC_REL32
: /* 26 */
851 *((UInt
*)pP
) = S
+A
-P
;
852 invalidate_icache(pP
,4);
857 "%s: unhandled ELF relocation(RelA) type %d\n",
858 oc
->fileName
, (Int
)ELF_R_TYPE(info
));
868 ocResolve_ELF ( ObjectCode
* oc
)
872 Elf_Sym
* stab
= NULL
;
873 char* ehdrC
= (char*)(oc
->image
);
874 Elf_Ehdr
* ehdr
= (Elf_Ehdr
*) ehdrC
;
875 Elf_Shdr
* shdr
= (Elf_Shdr
*) (ehdrC
+ ehdr
->e_shoff
);
876 char* sh_strtab
= ehdrC
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
878 /* first find "the" symbol table */
879 stab
= (Elf_Sym
*) findElfSection ( ehdrC
, SHT_SYMTAB
);
881 /* also go find the string table */
882 strtab
= findElfSection ( ehdrC
, SHT_STRTAB
);
884 if (stab
== NULL
|| strtab
== NULL
) {
885 fprintf(stderr
,"%s: can't find string or symbol table\n", oc
->fileName
);
889 /* Process the relocation sections. */
890 for (shnum
= 0; shnum
< ehdr
->e_shnum
; shnum
++) {
892 /* Skip sections called ".rel.stab". These appear to contain
893 relocation entries that, when done, make the stabs debugging
894 info point at the right places. We ain't interested in all
896 if (0 == memcmp(".rel.stab", sh_strtab
+ shdr
[shnum
].sh_name
, 9))
899 if (shdr
[shnum
].sh_type
== SHT_REL
) {
900 ok
= do_Elf_Rel_relocations ( oc
, ehdrC
, shdr
,
901 shnum
, stab
, strtab
);
905 if (shdr
[shnum
].sh_type
== SHT_RELA
) {
906 ok
= do_Elf_Rela_relocations ( oc
, ehdrC
, shdr
,
907 shnum
, stab
, strtab
);
912 /* Free the local symbol table; we won't need it again. */
913 delete_StringMap(oc
->lochash
);
920 ///////////////////////////////////////////////////////////////////
921 ///////////////////////////////////////////////////////////////////
922 ///////////////////////////////////////////////////////////////////
927 ocVerifyImage_ELF ( ObjectCode
* oc
)
931 int i
, j
, nent
, nstrtab
, nsymtabs
;
935 char* ehdrC
= (char*)(oc
->image
);
936 Elf_Ehdr
* ehdr
= (Elf_Ehdr
*)ehdrC
;
938 if (ehdr
->e_ident
[EI_MAG0
] != ELFMAG0
||
939 ehdr
->e_ident
[EI_MAG1
] != ELFMAG1
||
940 ehdr
->e_ident
[EI_MAG2
] != ELFMAG2
||
941 ehdr
->e_ident
[EI_MAG3
] != ELFMAG3
) {
942 fprintf(stderr
,"%s: not an ELF object\n", oc
->fileName
);
946 if (ehdr
->e_ident
[EI_CLASS
] != ELFCLASS
) {
947 fprintf(stderr
,"%s: unsupported ELF format\n", oc
->fileName
);
951 if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2LSB
) {
953 fprintf(stderr
, "Is little-endian\n" );
955 if (ehdr
->e_ident
[EI_DATA
] == ELFDATA2MSB
) {
957 fprintf(stderr
, "Is big-endian\n" );
959 fprintf(stderr
,"%s: unknown endiannness\n", oc
->fileName
);
963 if (ehdr
->e_type
!= ET_REL
) {
964 fprintf(stderr
,"%s: not a relocatable object (.o) file\n", oc
->fileName
);
968 fprintf(stderr
, "Is a relocatable object (.o) file\n" );
971 fprintf(stderr
, "Architecture is " );
972 switch (ehdr
->e_machine
) {
973 case EM_386
: if (debug_linker
) fprintf(stderr
, "x86\n" ); break;
974 case EM_SPARC
: if (debug_linker
) fprintf(stderr
, "sparc\n" ); break;
975 case EM_ARM
: if (debug_linker
) fprintf(stderr
, "arm\n" ); break;
977 case EM_IA_64
: if (debug_linker
) fprintf(stderr
, "ia64\n" ); break;
979 case EM_X86_64
: if (debug_linker
) fprintf(stderr
, "x86_64\n" ); break;
980 case EM_PPC
: if (debug_linker
) fprintf(stderr
, "ppc\n" ); break;
981 default: if (debug_linker
) fprintf(stderr
, "unknown\n" );
982 fprintf(stderr
,"%s: unknown architecture\n", oc
->fileName
);
986 if (debug_linker
>1) fprintf(stderr
,
987 "\nSection header table: start %lld, n_entries %d, ent_size %d\n",
989 ehdr
->e_shnum
, ehdr
->e_shentsize
);
991 assert (ehdr
->e_shentsize
== sizeof(Elf_Shdr
));
993 shdr
= (Elf_Shdr
*) (ehdrC
+ ehdr
->e_shoff
);
995 if (ehdr
->e_shstrndx
== SHN_UNDEF
) {
996 fprintf(stderr
,"%s: no section header string table\n", oc
->fileName
);
1000 fprintf(stderr
, "Section header string table is section %d\n",
1002 sh_strtab
= ehdrC
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
1005 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
1006 if (debug_linker
>1) fprintf(stderr
, "%2d: ", i
);
1007 if (debug_linker
>1) fprintf(stderr
, "type=%2d ", (int)shdr
[i
].sh_type
);
1008 if (debug_linker
>1) fprintf(stderr
, "size=%4d ", (int)shdr
[i
].sh_size
);
1009 if (debug_linker
>1) fprintf(stderr
, "offs=%4d ", (int)shdr
[i
].sh_offset
);
1010 if (debug_linker
>1) fprintf(stderr
, " (%p .. %p) ",
1011 ehdrC
+ shdr
[i
].sh_offset
,
1012 ehdrC
+ shdr
[i
].sh_offset
+ shdr
[i
].sh_size
- 1);
1014 if (shdr
[i
].sh_type
== SHT_REL
) {
1015 if (debug_linker
>1) fprintf(stderr
, "Rel " );
1016 } else if (shdr
[i
].sh_type
== SHT_RELA
) {
1017 if (debug_linker
>1) fprintf(stderr
, "RelA " );
1019 if (debug_linker
>1) fprintf(stderr
," ");
1022 if (debug_linker
>1) fprintf(stderr
, "sname=%s\n",
1023 sh_strtab
+ shdr
[i
].sh_name
);
1027 if (debug_linker
>1) fprintf(stderr
, "\nString tables\n" );
1030 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
1031 if (shdr
[i
].sh_type
== SHT_STRTAB
1032 /* Ignore the section header's string table. */
1033 && i
!= ehdr
->e_shstrndx
1034 /* Ignore string tables named .stabstr, as they contain
1036 && 0 != memcmp(".stabstr", sh_strtab
+ shdr
[i
].sh_name
, 8)
1039 fprintf(stderr
," section %d is a normal string table\n", i
);
1040 strtab
= ehdrC
+ shdr
[i
].sh_offset
;
1045 fprintf(stderr
,"%s: no string tables, or too many\n", oc
->fileName
);
1050 if (debug_linker
>1) fprintf(stderr
, "\nSymbol tables\n" );
1051 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
1052 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) continue;
1053 if (debug_linker
>1) fprintf(stderr
, "section %d is a symbol table\n", i
);
1055 stab
= (Elf_Sym
*) (ehdrC
+ shdr
[i
].sh_offset
);
1056 nent
= shdr
[i
].sh_size
/ sizeof(Elf_Sym
);
1057 if (debug_linker
>1) fprintf(stderr
,
1058 " number of entries is apparently %d (%lld rem)\n",
1060 (Long
)(shdr
[i
].sh_size
% sizeof(Elf_Sym
))
1062 if (0 != shdr
[i
].sh_size
% sizeof(Elf_Sym
)) {
1063 fprintf(stderr
,"%s: non-integral number of symbol table entries\n",
1067 for (j
= 0; j
< nent
; j
++) {
1068 if (debug_linker
>1) fprintf(stderr
, " %2d ", j
);
1069 if (debug_linker
>1) fprintf(stderr
, " sec=%-5d size=%-3d val=%5p ",
1070 (int)stab
[j
].st_shndx
,
1071 (int)stab
[j
].st_size
,
1072 (char*)stab
[j
].st_value
);
1074 if (debug_linker
>1) fprintf(stderr
, "type=" );
1075 switch (ELF_ST_TYPE(stab
[j
].st_info
)) {
1076 case STT_NOTYPE
: if (debug_linker
>1) fprintf(stderr
, "notype " ); break;
1077 case STT_OBJECT
: if (debug_linker
>1) fprintf(stderr
, "object " ); break;
1078 case STT_FUNC
: if (debug_linker
>1) fprintf(stderr
, "func " ); break;
1079 case STT_SECTION
: if (debug_linker
>1) fprintf(stderr
, "section" ); break;
1080 case STT_FILE
: if (debug_linker
>1) fprintf(stderr
, "file " ); break;
1081 default: if (debug_linker
>1) fprintf(stderr
, "? " ); break;
1083 if (debug_linker
>1) fprintf(stderr
, " " );
1085 if (debug_linker
>1) fprintf(stderr
, "bind=" );
1086 switch (ELF_ST_BIND(stab
[j
].st_info
)) {
1087 case STB_LOCAL
: if (debug_linker
>1) fprintf(stderr
, "local " ); break;
1088 case STB_GLOBAL
: if (debug_linker
>1) fprintf(stderr
, "global" ); break;
1089 case STB_WEAK
: if (debug_linker
>1) fprintf(stderr
, "weak " ); break;
1090 default: if (debug_linker
>1) fprintf(stderr
, "? " ); break;
1092 if (debug_linker
>1) fprintf(stderr
, " " );
1094 if (debug_linker
>1) fprintf(stderr
, "name=%s\n", strtab
+ stab
[j
].st_name
);
1098 if (nsymtabs
== 0) {
1099 fprintf(stderr
,"%s: didn't find any symbol tables\n", oc
->fileName
);
1107 ///////////////////////////////////////////////////////////////////
1108 ///////////////////////////////////////////////////////////////////
1109 ///////////////////////////////////////////////////////////////////
1114 ocGetNames_ELF ( ObjectCode
* oc
)
1119 char* ehdrC
= (char*)(oc
->image
);
1120 Elf_Ehdr
* ehdr
= (Elf_Ehdr
*)ehdrC
;
1121 char* strtab
= findElfSection ( ehdrC
, SHT_STRTAB
);
1122 Elf_Shdr
* shdr
= (Elf_Shdr
*) (ehdrC
+ ehdr
->e_shoff
);
1124 char* sh_strtab
= ehdrC
+ shdr
[ehdr
->e_shstrndx
].sh_offset
;
1127 assert(global_symbol_table
!= NULL
);
1130 fprintf(stderr
,"%s: no strtab\n", oc
->fileName
);
1135 for (i
= 0; i
< ehdr
->e_shnum
; i
++) {
1136 /* Figure out what kind of section it is. Logic derived from
1137 Figure 1.14 ("Special Sections") of the ELF document
1138 ("Portable Formats Specification, Version 1.1"). */
1139 Elf_Shdr hdr
= shdr
[i
];
1140 SectionKind kind
= SECTIONKIND_OTHER
;
1143 if (hdr
.sh_type
== SHT_PROGBITS
1144 && (hdr
.sh_flags
& SHF_ALLOC
) && (hdr
.sh_flags
& SHF_EXECINSTR
)) {
1145 /* .text-style section */
1146 kind
= SECTIONKIND_CODE_OR_RODATA
;
1149 if (hdr
.sh_type
== SHT_PROGBITS
1150 && (hdr
.sh_flags
& SHF_ALLOC
) && (hdr
.sh_flags
& SHF_WRITE
)) {
1151 /* .data-style section */
1152 kind
= SECTIONKIND_RWDATA
;
1155 if (hdr
.sh_type
== SHT_PROGBITS
1156 && (hdr
.sh_flags
& SHF_ALLOC
) && !(hdr
.sh_flags
& SHF_WRITE
)) {
1157 /* .rodata-style section */
1158 kind
= SECTIONKIND_CODE_OR_RODATA
;
1161 if (hdr
.sh_type
== SHT_NOBITS
1162 && (hdr
.sh_flags
& SHF_ALLOC
) && (hdr
.sh_flags
& SHF_WRITE
)) {
1163 /* .bss-style section */
1164 kind
= SECTIONKIND_RWDATA
;
1168 if (is_bss
&& shdr
[i
].sh_size
> 0) {
1169 /* This is a non-empty .bss section. Allocate zeroed space for
1170 it, and set its .sh_offset field such that
1171 ehdrC + .sh_offset == addr_of_zeroed_space. */
1172 char* zspace
= calloc(1, shdr
[i
].sh_size
);
1173 shdr
[i
].sh_offset
= ((char*)zspace
) - ((char*)ehdrC
);
1175 fprintf(stderr
, "BSS section at %p, size %lld\n",
1176 zspace
, (Long
)shdr
[i
].sh_size
);
1179 /* When loading objects compiled with -g, it seems there are
1180 relocations in various debug-info sections. So we'd better
1181 tell addProddableBlock to allow those bits to be prodded. */
1182 //fprintf(stderr, "ZZZZZZZZZZ %s\n", sh_strtab + hdr.sh_name);
1183 sec_name
= sh_strtab
+ shdr
[i
].sh_name
;
1184 if (kind
== SECTIONKIND_OTHER
1185 && (0 == strcmp(".debug_info", sec_name
)
1186 || 0 == strcmp(".debug_line", sec_name
)
1187 || 0 == strcmp(".debug_pubnames", sec_name
)
1188 || 0 == strcmp(".debug_aranges", sec_name
)
1189 || 0 == strcmp(".debug_frame", sec_name
))) {
1190 kind
= SECTIONKIND_CODE_OR_RODATA
;
1193 /* fill in the section info */
1194 if (kind
!= SECTIONKIND_OTHER
&& shdr
[i
].sh_size
> 0) {
1195 addProddableBlock(oc
, ehdrC
+ shdr
[i
].sh_offset
, shdr
[i
].sh_size
);
1196 //addSection(oc, kind, ehdrC + shdr[i].sh_offset,
1197 // ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1);
1200 if (shdr
[i
].sh_type
!= SHT_SYMTAB
) continue;
1202 /* copy stuff into this module's object symbol table */
1203 stab
= (Elf_Sym
*) (ehdrC
+ shdr
[i
].sh_offset
);
1204 nent
= shdr
[i
].sh_size
/ sizeof(Elf_Sym
);
1206 oc
->n_symbols
= nent
;
1207 oc
->symbols
= mymalloc(oc
->n_symbols
* sizeof(char*));
1209 for (j
= 0; j
< nent
; j
++) {
1211 char isLocal
= FALSE
; /* avoids uninit-var warning */
1213 char* nm
= strtab
+ stab
[j
].st_name
;
1214 int secno
= stab
[j
].st_shndx
;
1216 /* Figure out if we want to add it; if so, set ad to its
1217 address. Otherwise leave ad == NULL. */
1219 if (secno
== SHN_COMMON
) {
1221 # if defined(__x86_64__)
1222 ad
= calloc_below2G(1, stab
[j
].st_size
);
1224 ad
= calloc(1, stab
[j
].st_size
);
1226 // assert( (Addr)ad < 0xF0000000ULL );
1229 fprintf(stderr
, "COMMON symbol, size %lld name %s allocd %p\n",
1230 (Long
)stab
[j
].st_size
, nm
, ad
);
1231 /* Pointless to do addProddableBlock() for this area,
1232 since the linker should never poke around in it. */
1235 if ( ( ELF_ST_BIND(stab
[j
].st_info
)==STB_GLOBAL
1236 || ELF_ST_BIND(stab
[j
].st_info
)==STB_LOCAL
1238 /* and not an undefined symbol */
1239 && stab
[j
].st_shndx
!= SHN_UNDEF
1240 /* and not in a "special section" */
1241 && stab
[j
].st_shndx
< SHN_LORESERVE
1243 /* and it's a not a section or string table or anything silly */
1244 ( ELF_ST_TYPE(stab
[j
].st_info
)==STT_FUNC
||
1245 ELF_ST_TYPE(stab
[j
].st_info
)==STT_OBJECT
||
1246 ELF_ST_TYPE(stab
[j
].st_info
)==STT_NOTYPE
1249 /* Section 0 is the undefined section, hence > and not >=. */
1250 assert(secno
> 0 && secno
< ehdr
->e_shnum
);
1252 if (shdr[secno].sh_type == SHT_NOBITS) {
1253 fprintf(stderr, " BSS symbol, size %d off %d name %s\n",
1254 stab[j].st_size, stab[j].st_value, nm);
1257 ad
= ehdrC
+ shdr
[ secno
].sh_offset
+ stab
[j
].st_value
;
1258 if (ELF_ST_BIND(stab
[j
].st_info
)==STB_LOCAL
) {
1261 #ifdef ELF_FUNCTION_DESC
1262 /* dlsym() and the initialisation table both give us function
1263 * descriptors, so to be consistent we store function descriptors
1264 * in the symbol table */
1265 if (ELF_ST_TYPE(stab
[j
].st_info
) == STT_FUNC
)
1266 ad
= (char *)allocateFunctionDesc((Elf_Addr
)ad
);
1268 if (0|| debug_linker
)
1269 fprintf(stderr
, "addOTabName(GLOB): %10p %s %s\n",
1270 ad
, oc
->fileName
, nm
);
1275 /* And the decision is ... */
1279 oc
->symbols
[j
] = nm
;
1282 /* Ignore entirely. */
1284 //ghciInsertStrHashTable(oc->fileName, global_symbol_table, nm, ad);
1285 paranoid_addto_StringMap(global_symbol_table
, nm
, ad
);
1289 if (debug_linker
>1) fprintf(stderr
, "skipping `%s'\n",
1290 strtab
+ stab
[j
].st_name
);
1293 "skipping bind = %d, type = %d, shndx = %d `%s'\n",
1294 (int)ELF_ST_BIND(stab[j].st_info),
1295 (int)ELF_ST_TYPE(stab[j].st_info),
1296 (int)stab[j].st_shndx,
1297 strtab + stab[j].st_name
1300 oc
->symbols
[j
] = NULL
;
1310 ///////////////////////////////////////////////////////////////////
1311 ///////////////////////////////////////////////////////////////////
1312 ///////////////////////////////////////////////////////////////////
1314 // TOP-LEVEL CONTROL OF THE LINKER
1317 /* ---------------------------------------------------------------------
1318 * Load an obj (populate the global symbol table, but don't resolve yet)
1320 * Returns: 1 if ok, 0 on error.
1323 int loadObj( char *path
)
1333 fprintf(stderr
, "==== loadObj %s ====\n", path
);
1335 /* Check that we haven't already loaded this object. */
1339 for (o
= global_object_list
; o
; o
= o
->next
) {
1340 if (0 == strcmp(o
->fileName
, path
))
1346 "GHCi runtime linker: warning: looks like you're trying to load the\n"
1347 "same object file twice:\n"
1354 oc
= mymalloc(sizeof(ObjectCode
));
1356 oc
->formatName
= "ELF";
1358 r
= stat(path
, &st
);
1359 if (r
== -1) { return 0; }
1361 /* sigh, strdup() isn't a POSIX function, so do it the long way */
1362 oc
->fileName
= mymalloc( strlen(path
)+1 );
1363 strcpy(oc
->fileName
, path
);
1365 oc
->fileSize
= st
.st_size
;
1367 oc
->sections
= NULL
;
1368 oc
->lochash
= new_StringMap();
1369 oc
->proddables
= NULL
;
1374 /* chain it onto the list of objects */
1375 oc
->next
= global_object_list
;
1376 global_object_list
= oc
;
1378 fd
= open(path
, O_RDONLY
);
1380 fprintf(stderr
,"loadObj: can't open `%s'\n", path
);
1384 /* Allocate a 1-page area just prior to the image, so we can put
1385 fixup code fragments there. Used for doing R_ARM_PC24
1386 relocations for jump distances > 64M. */
1388 pagesize
= getpagesize();
1389 // p = memalign(pagesize, N_FIXUP_PAGES * pagesize
1391 p
= mymalloc(N_FIXUP_PAGES
* pagesize
+ oc
->fileSize
);
1392 if (0) fprintf(stderr
,"XXXX p = %p\n", p
);
1394 fprintf(stderr
,"loadObj: failed to allocate space for `%s'\n", path
);
1399 oc
->fixup_size
= N_FIXUP_PAGES
* pagesize
;
1401 oc
->image
= &(p
[ oc
->fixup_size
]);
1403 r
= read(fd
, oc
->image
, oc
->fileSize
);
1404 if (r
!= oc
->fileSize
) {
1405 fprintf(stderr
,"loadObj: failed to read `%s'\n", path
);
1409 fprintf(stderr
, "loaded %s at %p (fixup = %p)\n",
1410 oc
->fileName
, oc
->image
, oc
->fixup
);
1414 /* verify the in-memory image */
1415 r
= ocVerifyImage_ELF ( oc
);
1416 if (!r
) { return r
; }
1418 /* build the symbol list for this image */
1419 r
= ocGetNames_ELF ( oc
);
1420 if (!r
) { return r
; }
1422 /* loaded, but not resolved yet */
1423 oc
->status
= OBJECT_LOADED
;
1425 #ifdef ppc32_TARGET_ARCH
1426 invalidate_icache(oc
->image
, oc
->fileSize
);
1434 /* ---------------------------------------------------------------------------
1435 * resolve all the currently unlinked objects in memory
1437 * Returns: 1 if ok, 0 on error.
1440 int resolveObjs( void )
1447 for (oc
= global_object_list
; oc
; oc
= oc
->next
) {
1448 if (oc
->status
!= OBJECT_RESOLVED
) {
1449 r
= ocResolve_ELF ( oc
);
1450 if (!r
) { return r
; }
1451 oc
->status
= OBJECT_RESOLVED
;
1458 /* ---------------------------------------------------------------------------
1462 /* Load and link a bunch of .o's, and return the address of
1463 'entry'. Or NULL if something borks.
1465 void* linker_top_level_LINK ( int n_object_names
, char** object_names
)
1471 for (i
= 0; i
< n_object_names
; i
++) {
1472 //fprintf(stderr, "linkloop %d %s\n", i, object_names[i] );
1473 r
= loadObj( object_names
[i
] );
1474 if (r
!= 1) return NULL
;
1477 if (r
!= 1) return NULL
;
1478 mainp
= search_StringMap ( global_symbol_table
, "entry" );
1479 if (mainp
== NULL
) return NULL
;
1480 printf("switchback: Linker: success!\n");