Various patches to support android cross compilation
[ghc.git] / rts / linker / Elf.c
1 #include "Rts.h"
2
3 #if defined(linux_HOST_OS) || defined(solaris2_HOST_OS) \
4 || defined(linux_android_HOST_OS) \
5 || defined(freebsd_HOST_OS) || defined(kfreebsdgnu_HOST_OS) \
6 || defined(dragonfly_HOST_OS) || defined(netbsd_HOST_OS) \
7 || defined(openbsd_HOST_OS) || defined(gnu_HOST_OS)
8
9 #include "RtsUtils.h"
10 #include "RtsSymbolInfo.h"
11 #include "linker/Elf.h"
12 #include "linker/CacheFlush.h"
13 #include "linker/M32Alloc.h"
14 #include "linker/SymbolExtras.h"
15 #include "sm/OSMem.h"
16 #include "GetEnv.h"
17
18 #include <stdlib.h>
19 #include <string.h>
20 #ifdef HAVE_SYS_STAT_H
21 #include <sys/stat.h>
22 #endif
23 #ifdef HAVE_SYS_TYPES_H
24 #include <sys/types.h>
25 #endif
26 #ifdef HAVE_FCNTL_H
27 #include <fcntl.h>
28 #endif
29
30 /* on x86_64 we have a problem with relocating symbol references in
31 * code that was compiled without -fPIC. By default, the small memory
32 * model is used, which assumes that symbol references can fit in a
33 * 32-bit slot. The system dynamic linker makes this work for
34 * references to shared libraries by either (a) allocating a jump
35 * table slot for code references, or (b) moving the symbol at load
36 * time (and copying its contents, if necessary) for data references.
37 *
38 * We unfortunately can't tell whether symbol references are to code
39 * or data. So for now we assume they are code (the vast majority
40 * are), and allocate jump-table slots. Unfortunately this will
41 * SILENTLY generate crashing code for data references. This hack is
42 * enabled by X86_64_ELF_NONPIC_HACK.
43 *
44 * One workaround is to use shared Haskell libraries. This is
45 * coming. Another workaround is to keep the static libraries but
46 * compile them with -fPIC, because that will generate PIC references
47 * to data which can be relocated. The PIC code is still too green to
48 * do this systematically, though.
49 *
50 * See bug #781
51 * See thread http://www.haskell.org/pipermail/cvs-ghc/2007-September/038458.html
52 *
53 * Naming Scheme for Symbol Macros
54 *
55 * SymI_*: symbol is internal to the RTS. It resides in an object
56 * file/library that is statically.
57 * SymE_*: symbol is external to the RTS library. It might be linked
58 * dynamically.
59 *
60 * Sym*_HasProto : the symbol prototype is imported in an include file
61 * or defined explicitly
62 * Sym*_NeedsProto: the symbol is undefined and we add a dummy
63 * default proto extern void sym(void);
64 */
65 #define X86_64_ELF_NONPIC_HACK 1
66
67 #if defined(sparc_HOST_ARCH)
68 # define ELF_TARGET_SPARC /* Used inside <elf.h> */
69 #elif defined(i386_HOST_ARCH)
70 # define ELF_TARGET_386 /* Used inside <elf.h> */
71 #elif defined(x86_64_HOST_ARCH)
72 # define ELF_TARGET_X64_64
73 # define ELF_64BIT
74 # define ELF_TARGET_AMD64 /* Used inside <elf.h> on Solaris 11 */
75 #elif defined(powerpc64_HOST_ARCH) || defined(powerpc64le_HOST_ARCH)
76 # define ELF_64BIT
77 #elif defined(ia64_HOST_ARCH)
78 # define ELF_64BIT
79 #elif defined(aarch64_HOST_ARCH)
80 # define ELF_64BIT
81 #endif
82
83 #if !defined(openbsd_HOST_OS)
84 # include <elf.h>
85 #else
86 /* openbsd elf has things in different places, with diff names */
87 # include <elf_abi.h>
88 # include <machine/reloc.h>
89 # define R_386_32 RELOC_32
90 # define R_386_PC32 RELOC_PC32
91 #endif
92
93 /* If elf.h doesn't define it */
94 # ifndef R_X86_64_PC64
95 # define R_X86_64_PC64 24
96 # endif
97
98 /*
99 * Workaround for libc implementations (e.g. eglibc) with incomplete
100 * relocation lists
101 */
102 #ifndef R_ARM_THM_CALL
103 # define R_ARM_THM_CALL 10
104 #endif
105 #ifndef R_ARM_CALL
106 # define R_ARM_CALL 28
107 #endif
108 #ifndef R_ARM_JUMP24
109 # define R_ARM_JUMP24 29
110 #endif
111 #ifndef R_ARM_THM_JUMP24
112 # define R_ARM_THM_JUMP24 30
113 #endif
114 #ifndef R_ARM_TARGET1
115 # define R_ARM_TARGET1 38
116 #endif
117 #ifndef R_ARM_MOVW_ABS_NC
118 # define R_ARM_MOVW_ABS_NC 43
119 #endif
120 #ifndef R_ARM_MOVT_ABS
121 # define R_ARM_MOVT_ABS 44
122 #endif
123 #ifndef R_ARM_THM_MOVW_ABS_NC
124 # define R_ARM_THM_MOVW_ABS_NC 47
125 #endif
126 #ifndef R_ARM_THM_MOVT_ABS
127 # define R_ARM_THM_MOVT_ABS 48
128 #endif
129 #ifndef R_ARM_THM_JUMP11
130 # define R_ARM_THM_JUMP11 102
131 #endif
132 #ifndef R_ARM_THM_JUMP8
133 # define R_ARM_THM_JUMP8 103
134 #endif
135
136 /*
137 * Define a set of types which can be used for both ELF32 and ELF64
138 */
139
140 #ifdef ELF_64BIT
141 #define ELFCLASS ELFCLASS64
142 #define Elf_Addr Elf64_Addr
143 #define Elf_Word Elf64_Word
144 #define Elf_Sword Elf64_Sword
145 #define Elf_Half Elf64_Half
146 #define Elf_Ehdr Elf64_Ehdr
147 #define Elf_Phdr Elf64_Phdr
148 #define Elf_Shdr Elf64_Shdr
149 #define Elf_Sym Elf64_Sym
150 #define Elf_Rel Elf64_Rel
151 #define Elf_Rela Elf64_Rela
152 #ifndef ELF_ST_TYPE
153 #define ELF_ST_TYPE ELF64_ST_TYPE
154 #endif
155 #ifndef ELF_ST_BIND
156 #define ELF_ST_BIND ELF64_ST_BIND
157 #endif
158 #ifndef ELF_R_TYPE
159 #define ELF_R_TYPE ELF64_R_TYPE
160 #endif
161 #ifndef ELF_R_SYM
162 #define ELF_R_SYM ELF64_R_SYM
163 #endif
164 #else
165 #define ELFCLASS ELFCLASS32
166 #define Elf_Addr Elf32_Addr
167 #define Elf_Word Elf32_Word
168 #define Elf_Sword Elf32_Sword
169 #define Elf_Half Elf32_Half
170 #define Elf_Ehdr Elf32_Ehdr
171 #define Elf_Phdr Elf32_Phdr
172 #define Elf_Shdr Elf32_Shdr
173 #define Elf_Sym Elf32_Sym
174 #define Elf_Rel Elf32_Rel
175 #define Elf_Rela Elf32_Rela
176 #ifndef ELF_ST_TYPE
177 #define ELF_ST_TYPE ELF32_ST_TYPE
178 #endif
179 #ifndef ELF_ST_BIND
180 #define ELF_ST_BIND ELF32_ST_BIND
181 #endif
182 #ifndef ELF_R_TYPE
183 #define ELF_R_TYPE ELF32_R_TYPE
184 #endif
185 #ifndef ELF_R_SYM
186 #define ELF_R_SYM ELF32_R_SYM
187 #endif
188 #endif
189
190
191 /*
192 * Functions to allocate entries in dynamic sections. Currently we simply
193 * preallocate a large number, and we don't check if a entry for the given
194 * target already exists (a linear search is too slow). Ideally these
195 * entries would be associated with symbols.
196 */
197
198 /* These sizes sufficient to load HSbase + HShaskell98 + a few modules */
199 #define GOT_SIZE 0x20000
200 #define FUNCTION_TABLE_SIZE 0x10000
201 #define PLT_SIZE 0x08000
202
203 #ifdef ELF_NEED_GOT
204 static Elf_Addr got[GOT_SIZE];
205 static unsigned int gotIndex;
206 static Elf_Addr gp_val = (Elf_Addr)got;
207
208 static Elf_Addr
209 allocateGOTEntry(Elf_Addr target)
210 {
211 Elf_Addr *entry;
212
213 if (gotIndex >= GOT_SIZE)
214 barf("Global offset table overflow");
215
216 entry = &got[gotIndex++];
217 *entry = target;
218 return (Elf_Addr)entry;
219 }
220 #endif
221
222 #ifdef ELF_FUNCTION_DESC
223 typedef struct {
224 Elf_Addr ip;
225 Elf_Addr gp;
226 } FunctionDesc;
227
228 static FunctionDesc functionTable[FUNCTION_TABLE_SIZE];
229 static unsigned int functionTableIndex;
230
231 static Elf_Addr
232 allocateFunctionDesc(Elf_Addr target)
233 {
234 FunctionDesc *entry;
235
236 if (functionTableIndex >= FUNCTION_TABLE_SIZE)
237 barf("Function table overflow");
238
239 entry = &functionTable[functionTableIndex++];
240 entry->ip = target;
241 entry->gp = (Elf_Addr)gp_val;
242 return (Elf_Addr)entry;
243 }
244
245 static Elf_Addr
246 copyFunctionDesc(Elf_Addr target)
247 {
248 FunctionDesc *olddesc = (FunctionDesc *)target;
249 FunctionDesc *newdesc;
250
251 newdesc = (FunctionDesc *)allocateFunctionDesc(olddesc->ip);
252 newdesc->gp = olddesc->gp;
253 return (Elf_Addr)newdesc;
254 }
255 #endif
256
257 #ifdef ELF_NEED_PLT
258
259 typedef struct {
260 unsigned char code[sizeof(plt_code)];
261 } PLTEntry;
262
263 static Elf_Addr
264 allocatePLTEntry(Elf_Addr target, ObjectCode *oc)
265 {
266 PLTEntry *plt = (PLTEntry *)oc->plt;
267 PLTEntry *entry;
268
269 if (oc->pltIndex >= PLT_SIZE)
270 barf("Procedure table overflow");
271
272 entry = &plt[oc->pltIndex++];
273 memcpy(entry->code, plt_code, sizeof(entry->code));
274 PLT_RELOC(entry->code, target);
275 return (Elf_Addr)entry;
276 }
277
278 static unsigned int
279 PLTSize(void)
280 {
281 return (PLT_SIZE * sizeof(PLTEntry));
282 }
283 #endif
284
285 /*
286
287 Note [Many ELF Sections]
288
289 The normal section number fields in ELF are limited to 16 bits, which runs
290 out of bits when you try to cram in more sections than that.
291
292 To solve this, the fields e_shnum and e_shstrndx in the ELF header have an
293 escape value (different for each case), and the actual section number is
294 stashed into unused fields in the first section header.
295
296 For symbols, there seems to have been no place in the actual symbol table
297 for the extra bits, so the indexes have been moved into an auxiliary
298 section instead.
299 For symbols in sections beyond 0xff00, the symbol's st_shndx will be an
300 escape value (SHN_XINDEX), and the actual 32-bit section number for symbol N
301 is stored at index N in the SHT_SYMTAB_SHNDX table.
302
303 These extensions seem to be undocumented in version 4.1 of the ABI and only
304 appear in the drafts for the "next" version:
305 https://refspecs.linuxfoundation.org/elf/gabi4+/contents.html
306
307 */
308
309 static Elf_Word elf_shnum(Elf_Ehdr* ehdr)
310 {
311 Elf_Shdr* shdr = (Elf_Shdr*) ((char*)ehdr + ehdr->e_shoff);
312 Elf_Half shnum = ehdr->e_shnum;
313 return shnum != SHN_UNDEF ? shnum : shdr[0].sh_size;
314 }
315
316 static Elf_Word elf_shstrndx(Elf_Ehdr* ehdr)
317 {
318 Elf_Shdr* shdr = (Elf_Shdr*) ((char*)ehdr + ehdr->e_shoff);
319 Elf_Half shstrndx = ehdr->e_shstrndx;
320 #if defined(SHN_XINDEX)
321 return shstrndx != SHN_XINDEX ? shstrndx : shdr[0].sh_link;
322 #else
323 // some OSes do not support SHN_XINDEX yet, let's revert to
324 // old way
325 return shstrndx;
326 #endif
327 }
328
329 #if defined(SHN_XINDEX)
330 static Elf_Word*
331 get_shndx_table(Elf_Ehdr* ehdr)
332 {
333 Elf_Word i;
334 char* ehdrC = (char*)ehdr;
335 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
336 const Elf_Word shnum = elf_shnum(ehdr);
337
338 for (i = 0; i < shnum; i++) {
339 if (shdr[i].sh_type == SHT_SYMTAB_SHNDX) {
340 return (Elf32_Word*)(ehdrC + shdr[i].sh_offset);
341 }
342 }
343 return NULL;
344 }
345 #endif
346
347 /*
348 * Generic ELF functions
349 */
350
351 int
352 ocVerifyImage_ELF ( ObjectCode* oc )
353 {
354 Elf_Shdr* shdr;
355 Elf_Sym* stab;
356 int j, nent, nstrtab, nsymtabs;
357 Elf_Word i, shnum, shstrndx;
358 char* sh_strtab;
359
360 char* ehdrC = (char*)(oc->image);
361 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
362
363 if (ehdr->e_ident[EI_MAG0] != ELFMAG0 ||
364 ehdr->e_ident[EI_MAG1] != ELFMAG1 ||
365 ehdr->e_ident[EI_MAG2] != ELFMAG2 ||
366 ehdr->e_ident[EI_MAG3] != ELFMAG3) {
367 errorBelch("%s: not an ELF object", oc->fileName);
368 return 0;
369 }
370
371 if (ehdr->e_ident[EI_CLASS] != ELFCLASS) {
372 errorBelch("%s: unsupported ELF format", oc->fileName);
373 return 0;
374 }
375
376 if (ehdr->e_ident[EI_DATA] == ELFDATA2LSB) {
377 IF_DEBUG(linker,debugBelch( "Is little-endian\n" ));
378 } else
379 if (ehdr->e_ident[EI_DATA] == ELFDATA2MSB) {
380 IF_DEBUG(linker,debugBelch( "Is big-endian\n" ));
381 } else {
382 errorBelch("%s: unknown endianness", oc->fileName);
383 return 0;
384 }
385
386 if (ehdr->e_type != ET_REL) {
387 errorBelch("%s: not a relocatable object (.o) file", oc->fileName);
388 return 0;
389 }
390 IF_DEBUG(linker, debugBelch( "Is a relocatable object (.o) file\n" ));
391
392 IF_DEBUG(linker,debugBelch( "Architecture is " ));
393 switch (ehdr->e_machine) {
394 #ifdef EM_ARM
395 case EM_ARM: IF_DEBUG(linker,debugBelch( "arm" )); break;
396 #endif
397 case EM_386: IF_DEBUG(linker,debugBelch( "x86" )); break;
398 #ifdef EM_SPARC32PLUS
399 case EM_SPARC32PLUS:
400 #endif
401 case EM_SPARC: IF_DEBUG(linker,debugBelch( "sparc" )); break;
402 #ifdef EM_IA_64
403 case EM_IA_64: IF_DEBUG(linker,debugBelch( "ia64" )); break;
404 #endif
405 case EM_PPC: IF_DEBUG(linker,debugBelch( "powerpc32" )); break;
406 #ifdef EM_PPC64
407 case EM_PPC64: IF_DEBUG(linker,debugBelch( "powerpc64" ));
408 errorBelch("%s: RTS linker not implemented on PowerPC 64-bit",
409 oc->fileName);
410 return 0;
411 #endif
412 #ifdef EM_X86_64
413 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
414 #elif defined(EM_AMD64)
415 case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break;
416 #endif
417 default: IF_DEBUG(linker,debugBelch( "unknown" ));
418 errorBelch("%s: unknown architecture (e_machine == %d)"
419 , oc->fileName, ehdr->e_machine);
420 return 0;
421 }
422
423 shnum = elf_shnum(ehdr);
424 IF_DEBUG(linker,debugBelch(
425 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
426 (long)ehdr->e_shoff, shnum, ehdr->e_shentsize ));
427
428 ASSERT(ehdr->e_shentsize == sizeof(Elf_Shdr));
429
430 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
431
432 shstrndx = elf_shstrndx(ehdr);
433 if (shstrndx == SHN_UNDEF) {
434 errorBelch("%s: no section header string table", oc->fileName);
435 return 0;
436 } else {
437 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
438 shstrndx));
439 sh_strtab = ehdrC + shdr[shstrndx].sh_offset;
440 }
441
442 for (i = 0; i < shnum; i++) {
443 IF_DEBUG(linker,debugBelch("%2d: ", i ));
444 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
445 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
446 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
447 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
448 ehdrC + shdr[i].sh_offset,
449 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
450
451 #define SECTION_INDEX_VALID(ndx) (ndx > SHN_UNDEF && ndx < shnum)
452
453 switch (shdr[i].sh_type) {
454
455 case SHT_REL:
456 case SHT_RELA:
457 IF_DEBUG(linker,debugBelch( shdr[i].sh_type == SHT_REL ? "Rel " : "RelA "));
458
459 if (!SECTION_INDEX_VALID(shdr[i].sh_link)) {
460 if (shdr[i].sh_link == SHN_UNDEF)
461 errorBelch("\n%s: relocation section #%d has no symbol table\n"
462 "This object file has probably been fully striped. "
463 "Such files cannot be linked.\n",
464 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
465 else
466 errorBelch("\n%s: relocation section #%d has an invalid link field (%d)\n",
467 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
468 i, shdr[i].sh_link);
469 return 0;
470 }
471 if (shdr[shdr[i].sh_link].sh_type != SHT_SYMTAB) {
472 errorBelch("\n%s: relocation section #%d does not link to a symbol table\n",
473 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
474 return 0;
475 }
476 if (!SECTION_INDEX_VALID(shdr[i].sh_info)) {
477 errorBelch("\n%s: relocation section #%d has an invalid info field (%d)\n",
478 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
479 i, shdr[i].sh_info);
480 return 0;
481 }
482
483 break;
484 case SHT_SYMTAB:
485 IF_DEBUG(linker,debugBelch("Sym "));
486
487 if (!SECTION_INDEX_VALID(shdr[i].sh_link)) {
488 errorBelch("\n%s: symbol table section #%d has an invalid link field (%d)\n",
489 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
490 i, shdr[i].sh_link);
491 return 0;
492 }
493 if (shdr[shdr[i].sh_link].sh_type != SHT_STRTAB) {
494 errorBelch("\n%s: symbol table section #%d does not link to a string table\n",
495 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
496
497 return 0;
498 }
499 break;
500 case SHT_STRTAB: IF_DEBUG(linker,debugBelch("Str ")); break;
501 default: IF_DEBUG(linker,debugBelch(" ")); break;
502 }
503 if (sh_strtab) {
504 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
505 }
506 }
507
508 IF_DEBUG(linker,debugBelch( "\nString tables\n" ));
509 nstrtab = 0;
510 for (i = 0; i < shnum; i++) {
511 if (shdr[i].sh_type == SHT_STRTAB
512 /* Ignore the section header's string table. */
513 && i != shstrndx
514 /* Ignore string tables named .stabstr, as they contain
515 debugging info. */
516 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
517 ) {
518 IF_DEBUG(linker,debugBelch(" section %d is a normal string table\n", i ));
519 nstrtab++;
520 }
521 }
522 if (nstrtab == 0) {
523 IF_DEBUG(linker,debugBelch(" no normal string tables (potentially, but not necessarily a problem)\n"));
524 }
525 #if defined(SHN_XINDEX)
526 Elf_Word* shndxTable = get_shndx_table(ehdr);
527 #endif
528 nsymtabs = 0;
529 IF_DEBUG(linker,debugBelch( "Symbol tables\n" ));
530 for (i = 0; i < shnum; i++) {
531 if (shdr[i].sh_type != SHT_SYMTAB) continue;
532 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
533 nsymtabs++;
534 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
535 nent = shdr[i].sh_size / sizeof(Elf_Sym);
536 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
537 nent,
538 (long)shdr[i].sh_size % sizeof(Elf_Sym)
539 ));
540 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
541 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
542 return 0;
543 }
544 for (j = 0; j < nent; j++) {
545 Elf_Word secno = stab[j].st_shndx;
546 #if defined(SHN_XINDEX)
547 /* See Note [Many ELF Sections] */
548 if (secno == SHN_XINDEX) {
549 ASSERT(shndxTable);
550 secno = shndxTable[j];
551 }
552 #endif
553 IF_DEBUG(linker,debugBelch(" %2d ", j ));
554 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
555 (int)secno,
556 (int)stab[j].st_size,
557 (char*)stab[j].st_value ));
558
559 IF_DEBUG(linker,debugBelch("type=" ));
560 switch (ELF_ST_TYPE(stab[j].st_info)) {
561 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
562 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
563 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
564 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
565 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
566 default: IF_DEBUG(linker,debugBelch("? " )); break;
567 }
568 IF_DEBUG(linker,debugBelch(" " ));
569
570 IF_DEBUG(linker,debugBelch("bind=" ));
571 switch (ELF_ST_BIND(stab[j].st_info)) {
572 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
573 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
574 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
575 default: IF_DEBUG(linker,debugBelch("? " )); break;
576 }
577 IF_DEBUG(linker,debugBelch(" " ));
578
579 IF_DEBUG(linker,debugBelch("other=%2x ", stab[j].st_other ));
580 IF_DEBUG(linker,debugBelch("name=%s [%x]\n",
581 ehdrC + shdr[shdr[i].sh_link].sh_offset
582 + stab[j].st_name, stab[j].st_name ));
583 }
584 }
585
586 if (nsymtabs == 0) {
587 // Not having a symbol table is not in principle a problem.
588 // When an object file has no symbols then the 'strip' program
589 // typically will remove the symbol table entirely.
590 IF_DEBUG(linker,debugBelch(" no symbol tables (potentially, but not necessarily a problem)\n"));
591 }
592
593 return 1;
594 }
595
596 /* Figure out what kind of section it is. Logic derived from
597 Figure 1.14 ("Special Sections") of the ELF document
598 ("Portable Formats Specification, Version 1.1"). */
599 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
600 {
601 *is_bss = false;
602
603 if (hdr->sh_type == SHT_PROGBITS
604 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
605 /* .text-style section */
606 return SECTIONKIND_CODE_OR_RODATA;
607 }
608
609 if (hdr->sh_type == SHT_PROGBITS
610 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
611 /* .data-style section */
612 return SECTIONKIND_RWDATA;
613 }
614
615 if (hdr->sh_type == SHT_PROGBITS
616 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
617 /* .rodata-style section */
618 return SECTIONKIND_CODE_OR_RODATA;
619 }
620 #ifdef SHT_INIT_ARRAY
621 if (hdr->sh_type == SHT_INIT_ARRAY
622 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
623 /* .init_array section */
624 return SECTIONKIND_INIT_ARRAY;
625 }
626 #endif /* not SHT_INIT_ARRAY */
627 if (hdr->sh_type == SHT_NOBITS
628 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
629 /* .bss-style section */
630 *is_bss = true;
631 return SECTIONKIND_RWDATA;
632 }
633
634 return SECTIONKIND_OTHER;
635 }
636
637 static void *
638 mapObjectFileSection (int fd, Elf_Word offset, Elf_Word size,
639 void **mapped_start, StgWord *mapped_size,
640 StgWord *mapped_offset)
641 {
642 void *p;
643 size_t pageOffset, pageSize;
644
645 pageOffset = roundDownToPage(offset);
646 pageSize = roundUpToPage(offset-pageOffset+size);
647 p = mmapForLinker(pageSize, 0, fd, pageOffset);
648 if (p == NULL) return NULL;
649 *mapped_size = pageSize;
650 *mapped_offset = pageOffset;
651 *mapped_start = p;
652 return (void*)((StgWord)p + offset - pageOffset);
653 }
654
655 int
656 ocGetNames_ELF ( ObjectCode* oc )
657 {
658 Elf_Word i;
659 int j, nent, result, fd = -1;
660 Elf_Sym* stab;
661
662 char* ehdrC = (char*)(oc->image);
663 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
664 char* strtab;
665 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
666 Section * sections;
667 #if defined(SHN_XINDEX)
668 Elf_Word* shndxTable = get_shndx_table(ehdr);
669 #endif
670 const Elf_Word shnum = elf_shnum(ehdr);
671
672 ASSERT(symhash != NULL);
673
674 sections = (Section*)stgCallocBytes(sizeof(Section), shnum,
675 "ocGetNames_ELF(sections)");
676 oc->sections = sections;
677 oc->n_sections = shnum;
678
679
680 if (oc->imageMapped) {
681 #if defined(openbsd_HOST_OS)
682 fd = open(oc->fileName, O_RDONLY, S_IRUSR);
683 #else
684 fd = open(oc->fileName, O_RDONLY);
685 #endif
686 if (fd == -1) {
687 errorBelch("loadObj: can't open %" PATH_FMT, oc->fileName);
688 return 0;
689 }
690 }
691
692 for (i = 0; i < shnum; i++) {
693 int is_bss = false;
694 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
695 SectionAlloc alloc = SECTION_NOMEM;
696 void *start = NULL, *mapped_start = NULL;
697 StgWord mapped_size = 0, mapped_offset = 0;
698 StgWord size = shdr[i].sh_size;
699 StgWord offset = shdr[i].sh_offset;
700
701 if (is_bss && size > 0) {
702 /* This is a non-empty .bss section. Allocate zeroed space for
703 it, and set its .sh_offset field such that
704 ehdrC + .sh_offset == addr_of_zeroed_space. */
705 alloc = SECTION_MALLOC;
706 start = stgCallocBytes(1, size, "ocGetNames_ELF(BSS)");
707 mapped_start = start;
708 /*
709 debugBelch("BSS section at 0x%x, size %d\n",
710 zspace, shdr[i].sh_size);
711 */
712 }
713
714 else if (kind != SECTIONKIND_OTHER && size > 0) {
715 if (USE_CONTIGUOUS_MMAP) {
716 // already mapped.
717 start = oc->image + offset;
718 alloc = SECTION_NOMEM;
719 }
720 // use the m32 allocator if either the image is not mapped
721 // (i.e. we cannot map the secions separately), or if the section
722 // size is small.
723 else if (!oc->imageMapped || size < getPageSize() / 3) {
724 start = m32_alloc(size, 8);
725 if (start == NULL) goto fail;
726 memcpy(start, oc->image + offset, size);
727 alloc = SECTION_M32;
728 } else {
729 start = mapObjectFileSection(fd, offset, size,
730 &mapped_start, &mapped_size,
731 &mapped_offset);
732 if (start == NULL) goto fail;
733 alloc = SECTION_MMAP;
734 }
735 addProddableBlock(oc, start, size);
736 }
737
738 addSection(&sections[i], kind, alloc, start, size,
739 mapped_offset, mapped_start, mapped_size);
740
741 if (shdr[i].sh_type != SHT_SYMTAB) continue;
742
743 /* copy stuff into this module's object symbol table */
744 stab = (Elf_Sym*) (ehdrC + offset);
745 strtab = ehdrC + shdr[shdr[i].sh_link].sh_offset;
746 nent = shdr[i].sh_size / sizeof(Elf_Sym);
747
748 oc->n_symbols = nent;
749 oc->symbols = stgCallocBytes(oc->n_symbols, sizeof(SymbolName*),
750 "ocGetNames_ELF(oc->symbols)");
751 // Note calloc: if we fail partway through initializing symbols, we need
752 // to undo the additions to the symbol table so far. We know which ones
753 // have been added by whether the entry is NULL or not.
754
755 //TODO: we ignore local symbols anyway right? So we can use the
756 // shdr[i].sh_info to get the index of the first non-local symbol
757 // ie we should use j = shdr[i].sh_info
758 for (j = 0; j < nent; j++) {
759
760 char isLocal = false; /* avoids uninit-var warning */
761 HsBool isWeak = HS_BOOL_FALSE;
762 SymbolAddr* ad = NULL;
763 SymbolName* nm = strtab + stab[j].st_name;
764 unsigned short shndx = stab[j].st_shndx;
765 Elf_Word secno;
766
767 /* See Note [Many ELF Sections] */
768 /* Note that future checks for special SHN_* numbers should check the
769 * shndx variable, not the section number in secno. Sections with the
770 * real number in the SHN_LORESERVE..HIRESERVE range will have shndx
771 * SHN_XINDEX and a secno with one of the reserved values. */
772 secno = shndx;
773 #if defined(SHN_XINDEX)
774 if (shndx == SHN_XINDEX) {
775 ASSERT(shndxTable);
776 secno = shndxTable[j];
777 }
778 #endif
779 /* Figure out if we want to add it; if so, set ad to its
780 address. Otherwise leave ad == NULL. */
781
782 if (shndx == SHN_COMMON) {
783 isLocal = false;
784 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
785 /*
786 debugBelch("COMMON symbol, size %d name %s\n",
787 stab[j].st_size, nm);
788 */
789 /* Pointless to do addProddableBlock() for this area,
790 since the linker should never poke around in it. */
791 }
792 else
793 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
794 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
795 || ELF_ST_BIND(stab[j].st_info)==STB_WEAK
796 )
797 /* and not an undefined symbol */
798 && shndx != SHN_UNDEF
799 /* and not in a "special section" */
800 && (shndx < SHN_LORESERVE
801 #if defined(SHN_XINDEX)
802 || shndx == SHN_XINDEX
803 #endif
804 )
805 &&
806 /* and it's a not a section or string table or anything silly */
807 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
808 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
809 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
810 )
811 ) {
812 /* Section 0 is the undefined section, hence > and not >=. */
813 ASSERT(secno > 0 && secno < shnum);
814 /*
815 if (shdr[secno].sh_type == SHT_NOBITS) {
816 debugBelch(" BSS symbol, size %d off %d name %s\n",
817 stab[j].st_size, stab[j].st_value, nm);
818 }
819 */
820 ad = (SymbolAddr*)((intptr_t)sections[secno].start +
821 (intptr_t)stab[j].st_value);
822 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
823 isLocal = true;
824 isWeak = false;
825 } else { /* STB_GLOBAL or STB_WEAK */
826 #ifdef ELF_FUNCTION_DESC
827 /* dlsym() and the initialisation table both give us function
828 * descriptors, so to be consistent we store function descriptors
829 * in the symbol table */
830 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
831 ad = (SymbolAddr*)allocateFunctionDesc((Elf_Addr)ad);
832 #endif
833 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s\n",
834 ad, oc->fileName, nm ));
835 isLocal = false;
836 isWeak = (ELF_ST_BIND(stab[j].st_info)==STB_WEAK);
837 }
838 }
839
840 /* And the decision is ... */
841
842 oc->symbols[j] = nm;
843
844 if (ad != NULL) {
845 ASSERT(nm != NULL);
846 /* Acquire! */
847 if (isLocal) {
848 /* Ignore entirely. */
849 oc->symbols[j] = NULL;
850 } else {
851
852 if (isWeak == HS_BOOL_TRUE) {
853 setWeakSymbol(oc, nm);
854 }
855
856 if (! ghciInsertSymbolTable(oc->fileName, symhash,
857 nm, ad, isWeak, oc)) {
858 goto fail;
859 }
860 }
861 } else {
862 /* Skip. */
863 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
864 nm ));
865
866 /* We're skipping the symbol, but if we ever load this
867 object file we'll want to skip it then too. */
868 oc->symbols[j] = NULL;
869
870 /*
871 debugBelch(
872 "skipping bind = %d, type = %d, secno = %d `%s'\n",
873 (int)ELF_ST_BIND(stab[j].st_info),
874 (int)ELF_ST_TYPE(stab[j].st_info),
875 (int)secno,
876 nm
877 );
878 */
879 }
880
881 }
882 }
883
884 result = 1;
885 goto end;
886
887 fail:
888 result = 0;
889 goto end;
890
891 end:
892 if (fd >= 0) close(fd);
893 return result;
894 }
895
896 #ifdef arm_HOST_ARCH
897 // TODO: These likely belong in a library somewhere
898
899 // Signed extend a number to a 32-bit int.
900 static inline StgInt32 sign_extend32(uint32_t bits, StgWord32 x) {
901 return ((StgInt32) (x << (32 - bits))) >> (32 - bits);
902 }
903
904 // Does the given signed integer fit into the given bit width?
905 static inline StgBool is_int(uint32_t bits, StgInt32 x) {
906 return bits > 32 || (-(1 << (bits-1)) <= x
907 && x < (1 << (bits-1)));
908 }
909 #endif
910
911 /* Do ELF relocations which lack an explicit addend. All x86-linux
912 and arm-linux relocations appear to be of this form. */
913 static int
914 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
915 Elf_Shdr* shdr, int shnum )
916 {
917 int j;
918 SymbolName* symbol;
919 Elf_Word* targ;
920 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
921 Elf_Sym* stab;
922 char* strtab;
923 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
924 int target_shndx = shdr[shnum].sh_info;
925 int symtab_shndx = shdr[shnum].sh_link;
926 int strtab_shndx = shdr[symtab_shndx].sh_link;
927 #if defined(SHN_XINDEX)
928 Elf_Word* shndx_table = get_shndx_table((Elf_Ehdr*)ehdrC);
929 #endif
930
931 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
932 strtab= (char*) (ehdrC + shdr[ strtab_shndx ].sh_offset);
933 targ = (Elf_Word*)oc->sections[target_shndx].start;
934 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d and strtab %d\n",
935 target_shndx, symtab_shndx, strtab_shndx ));
936
937 /* Skip sections that we're not interested in. */
938 if (oc->sections[target_shndx].kind == SECTIONKIND_OTHER) {
939 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
940 return 1;
941 }
942
943 for (j = 0; j < nent; j++) {
944 Elf_Addr offset = rtab[j].r_offset;
945 Elf_Addr info = rtab[j].r_info;
946
947 Elf_Addr P = ((Elf_Addr)targ) + offset;
948 Elf_Word* pP = (Elf_Word*)P;
949 #if defined(i386_HOST_ARCH) || defined(DEBUG)
950 Elf_Addr A = *pP;
951 #endif
952 Elf_Addr S;
953 void* S_tmp;
954 #ifdef i386_HOST_ARCH
955 Elf_Addr value;
956 #endif
957 #ifdef arm_HOST_ARCH
958 int is_target_thm=0, T=0;
959 #endif
960
961 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p): ",
962 j, (void*)offset, (void*)info ));
963 if (!info) {
964 IF_DEBUG(linker,debugBelch( " ZERO" ));
965 S = 0;
966 } else {
967 Elf_Sym sym = stab[ELF_R_SYM(info)];
968 /* First see if it is a local symbol. */
969 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
970 /* Yes, so we can get the address directly from the ELF symbol
971 table. */
972 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
973 /* See Note [Many ELF Sections] */
974 Elf_Word secno = sym.st_shndx;
975 #if defined(SHN_XINDEX)
976 if (secno == SHN_XINDEX) {
977 ASSERT(shndx_table);
978 secno = shndx_table[ELF_R_SYM(info)];
979 }
980 #endif
981 S = (Elf_Addr)oc->sections[ secno ].start +
982 stab[ELF_R_SYM(info)].st_value;
983 } else {
984 symbol = strtab + sym.st_name;
985 S_tmp = lookupSymbol_( symbol );
986 S = (Elf_Addr)S_tmp;
987 }
988 if (!S) {
989 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
990 return 0;
991 }
992 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
993
994 #ifdef arm_HOST_ARCH
995 // Thumb instructions have bit 0 of symbol's st_value set
996 is_target_thm = S & 0x1;
997
998 T = sym.st_info & STT_FUNC && is_target_thm;
999
1000 // Make sure we clear bit 0. Strictly speaking we should have done
1001 // this to st_value above but I believe alignment requirements should
1002 // ensure that no instructions start on an odd address
1003 S &= ~1;
1004 #endif
1005 }
1006
1007 int reloc_type = ELF_R_TYPE(info);
1008 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p type=%d\n",
1009 (void*)P, (void*)S, (void*)A, reloc_type ));
1010 checkProddableBlock ( oc, pP, sizeof(Elf_Word) );
1011
1012 #ifdef i386_HOST_ARCH
1013 value = S + A;
1014 #endif
1015
1016 switch (reloc_type) {
1017 # ifdef i386_HOST_ARCH
1018 case R_386_32: *pP = value; break;
1019 case R_386_PC32: *pP = value - P; break;
1020 # endif
1021
1022 # ifdef arm_HOST_ARCH
1023 case R_ARM_ABS32:
1024 case R_ARM_TARGET1: // Specified by Linux ARM ABI to be equivalent to ABS32
1025 *(Elf32_Word *)P += S;
1026 *(Elf32_Word *)P |= T;
1027 break;
1028
1029 case R_ARM_REL32:
1030 *(Elf32_Word *)P += S;
1031 *(Elf32_Word *)P |= T;
1032 *(Elf32_Word *)P -= P;
1033 break;
1034
1035 case R_ARM_CALL:
1036 case R_ARM_JUMP24:
1037 {
1038 // N.B. LLVM's LLD linker's relocation implement is a fantastic
1039 // resource
1040 StgWord32 *word = (StgWord32 *)P;
1041 StgInt32 imm = (*word & ((1<<24)-1)) << 2;
1042
1043 const StgBool is_blx = (*word & 0xf0000000) == 0xf0000000;
1044 const StgWord32 hBit = is_blx ? ((*word >> 24) & 1) : 0;
1045 imm |= hBit << 1;
1046
1047 // Sign extend to 32 bits
1048 // I would have thought this would be 24 bits but LLD uses 26 here.
1049 // Hmm.
1050 imm = sign_extend32(26, imm);
1051
1052 StgWord32 result = ((S + imm) | T) - P;
1053
1054 const StgBool overflow = !is_int(26, (StgInt32) result);
1055
1056 // Handle overflow and Thumb interworking
1057 const StgBool needs_veneer = (is_target_thm && ELF_R_TYPE(info) == R_ARM_JUMP24) || overflow;
1058 if (needs_veneer) {
1059 // Generate veneer
1060 // The +8 below is to undo the PC-bias compensation done by the object producer
1061 SymbolExtra *extra = makeArmSymbolExtra(oc, ELF_R_SYM(info), S+imm+8, 0, is_target_thm);
1062 // The -8 below is to compensate for PC bias
1063 result = (StgWord32) ((StgInt32) extra->jumpIsland - P - 8);
1064 result &= ~1; // Clear thumb indicator bit
1065 if (!is_int(26, (StgInt32) result)) {
1066 errorBelch("Unable to fixup overflow'd R_ARM_CALL: jump island=%p, reloc=%p\n",
1067 (void*) extra->jumpIsland, (void*) P);
1068 return 0;
1069 }
1070 }
1071
1072 // Update the branch target
1073 const StgWord32 imm24 = (result & 0x03fffffc) >> 2;
1074 *word = (*word & ~0x00ffffff)
1075 | (imm24 & 0x00ffffff);
1076
1077 // Change the relocated branch into a BLX if necessary
1078 const StgBool switch_mode = is_target_thm && (reloc_type == R_ARM_CALL);
1079 if (!needs_veneer && switch_mode) {
1080 const StgWord32 hBit = (result & 0x2) >> 1;
1081 // Change instruction to BLX
1082 *word = (*word & ~0xFF000000) | ((0xfa | hBit) << 24);
1083 IF_DEBUG(linker, debugBelch("Changed BL to BLX at %p\n", word));
1084 }
1085 break;
1086 }
1087
1088 case R_ARM_MOVT_ABS:
1089 case R_ARM_MOVW_ABS_NC:
1090 {
1091 StgWord32 *word = (StgWord32 *)P;
1092 StgWord32 imm12 = *word & 0xfff;
1093 StgWord32 imm4 = (*word >> 16) & 0xf;
1094 StgInt32 offset = imm4 << 12 | imm12;
1095 StgWord32 result = (S + offset) | T;
1096
1097 if (reloc_type == R_ARM_MOVT_ABS)
1098 result = (result & 0xffff0000) >> 16;
1099
1100 StgWord32 result12 = result & 0xfff;
1101 StgWord32 result4 = (result >> 12) & 0xf;
1102 *word = (*word & ~0xf0fff) | (result4 << 16) | result12;
1103 break;
1104 }
1105
1106 case R_ARM_THM_CALL:
1107 case R_ARM_THM_JUMP24:
1108 {
1109 StgWord16 *upper = (StgWord16 *)P;
1110 StgWord16 *lower = (StgWord16 *)(P + 2);
1111
1112 int overflow;
1113 int to_thm = (*lower >> 12) & 1;
1114 int sign = (*upper >> 10) & 1;
1115 int j1, j2, i1, i2;
1116
1117 // Decode immediate value
1118 j1 = (*lower >> 13) & 1; i1 = ~(j1 ^ sign) & 1;
1119 j2 = (*lower >> 11) & 1; i2 = ~(j2 ^ sign) & 1;
1120 StgInt32 imm = (sign << 24)
1121 | (i1 << 23)
1122 | (i2 << 22)
1123 | ((*upper & 0x03ff) << 12)
1124 | ((*lower & 0x07ff) << 1);
1125
1126 // Sign extend 25 to 32 bits
1127 if (imm & 0x01000000)
1128 imm -= 0x02000000;
1129
1130 offset = ((imm + S) | T) - P;
1131 overflow = offset <= (StgWord32)0xff000000 || offset >= (StgWord32)0x01000000;
1132
1133 if ((!is_target_thm && ELF_R_TYPE(info) == R_ARM_THM_JUMP24) || overflow) {
1134 // Generate veneer
1135 SymbolExtra *extra = makeArmSymbolExtra(oc, ELF_R_SYM(info), S+imm+4, 1, is_target_thm);
1136 offset = (StgWord32) &extra->jumpIsland - P - 4;
1137 sign = offset >> 31;
1138 to_thm = 1;
1139 } else if (!is_target_thm && ELF_R_TYPE(info) == R_ARM_THM_CALL) {
1140 offset &= ~0x3;
1141 to_thm = 0;
1142 }
1143
1144 // Reencode instruction
1145 i1 = ~(offset >> 23) & 1; j1 = sign ^ i1;
1146 i2 = ~(offset >> 22) & 1; j2 = sign ^ i2;
1147 *upper = ( (*upper & 0xf800)
1148 | (sign << 10)
1149 | ((offset >> 12) & 0x03ff) );
1150 *lower = ( (*lower & 0xd000)
1151 | (j1 << 13)
1152 | (to_thm << 12)
1153 | (j2 << 11)
1154 | ((offset >> 1) & 0x07ff) );
1155 break;
1156 }
1157
1158 case R_ARM_THM_MOVT_ABS:
1159 case R_ARM_THM_MOVW_ABS_NC:
1160 {
1161 StgWord16 *upper = (StgWord16 *)P;
1162 StgWord16 *lower = (StgWord16 *)(P + 2);
1163 StgInt32 offset = ((*upper & 0x000f) << 12)
1164 | ((*upper & 0x0400) << 1)
1165 | ((*lower & 0x7000) >> 4)
1166 | (*lower & 0x00ff);
1167
1168 offset = (offset ^ 0x8000) - 0x8000; // Sign extend
1169 offset += S;
1170 if (ELF_R_TYPE(info) == R_ARM_THM_MOVW_ABS_NC)
1171 offset |= T;
1172 else if (ELF_R_TYPE(info) == R_ARM_THM_MOVT_ABS)
1173 offset >>= 16;
1174
1175 *upper = ( (*upper & 0xfbf0)
1176 | ((offset & 0xf000) >> 12)
1177 | ((offset & 0x0800) >> 1) );
1178 *lower = ( (*lower & 0x8f00)
1179 | ((offset & 0x0700) << 4)
1180 | (offset & 0x00ff) );
1181 break;
1182 }
1183
1184 case R_ARM_THM_JUMP8:
1185 {
1186 StgWord16 *word = (StgWord16 *)P;
1187 StgWord offset = *word & 0x01fe;
1188 offset += S - P;
1189 if (!is_target_thm) {
1190 errorBelch("%s: Thumb to ARM transition with JUMP8 relocation not supported\n",
1191 oc->fileName);
1192 return 0;
1193 }
1194
1195 *word = (*word & ~0x01fe)
1196 | (offset & 0x01fe);
1197 break;
1198 }
1199
1200 case R_ARM_THM_JUMP11:
1201 {
1202 StgWord16 *word = (StgWord16 *)P;
1203 StgWord offset = *word & 0x0ffe;
1204 offset += S - P;
1205 if (!is_target_thm) {
1206 errorBelch("%s: Thumb to ARM transition with JUMP11 relocation not supported\n",
1207 oc->fileName);
1208 return 0;
1209 }
1210
1211 *word = (*word & ~0x0ffe)
1212 | (offset & 0x0ffe);
1213 break;
1214 }
1215
1216 # endif // arm_HOST_ARCH
1217
1218 default:
1219 errorBelch("%s: unhandled ELF relocation(Rel) type %" FMT_Word "\n",
1220 oc->fileName, (W_)ELF_R_TYPE(info));
1221 return 0;
1222 }
1223
1224 }
1225 return 1;
1226 }
1227
1228 /* Do ELF relocations for which explicit addends are supplied.
1229 sparc-solaris relocations appear to be of this form. */
1230 static int
1231 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1232 Elf_Shdr* shdr, int shnum )
1233 {
1234 int j;
1235 SymbolName* symbol = NULL;
1236 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
1237 Elf_Sym* stab;
1238 char* strtab;
1239 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
1240 int symtab_shndx = shdr[shnum].sh_link;
1241 int strtab_shndx = shdr[symtab_shndx].sh_link;
1242 int target_shndx = shdr[shnum].sh_info;
1243 #if defined(SHN_XINDEX)
1244 Elf_Word* shndx_table = get_shndx_table((Elf_Ehdr*)ehdrC);
1245 #endif
1246 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1247 /* This #ifdef only serves to avoid unused-var warnings. */
1248 Elf_Addr targ = (Elf_Addr) oc->sections[target_shndx].start;
1249 #endif
1250
1251 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1252 strtab= (char*) (ehdrC + shdr[ strtab_shndx ].sh_offset);
1253
1254 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
1255 target_shndx, symtab_shndx ));
1256
1257 /* Skip sections that we're not interested in. */
1258 if (oc->sections[target_shndx].kind == SECTIONKIND_OTHER) {
1259 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
1260 return 1;
1261 }
1262
1263 for (j = 0; j < nent; j++) {
1264 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1265 /* This #ifdef only serves to avoid unused-var warnings. */
1266 Elf_Addr offset = rtab[j].r_offset;
1267 Elf_Addr P = targ + offset;
1268 Elf_Addr A = rtab[j].r_addend;
1269 #endif
1270 #if defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1271 Elf_Addr value;
1272 #endif
1273 Elf_Addr info = rtab[j].r_info;
1274 Elf_Addr S;
1275 void* S_tmp;
1276 # if defined(sparc_HOST_ARCH)
1277 Elf_Word* pP = (Elf_Word*)P;
1278 Elf_Word w1, w2;
1279 # elif defined(powerpc_HOST_ARCH)
1280 Elf_Sword delta;
1281 # endif
1282
1283 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
1284 j, (void*)offset, (void*)info,
1285 (void*)A ));
1286 if (!info) {
1287 IF_DEBUG(linker,debugBelch( " ZERO" ));
1288 S = 0;
1289 } else {
1290 Elf_Sym sym = stab[ELF_R_SYM(info)];
1291 /* First see if it is a local symbol. */
1292 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
1293 /* Yes, so we can get the address directly from the ELF symbol
1294 table. */
1295 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
1296 /* See Note [Many ELF Sections] */
1297 Elf_Word secno = sym.st_shndx;
1298 #if defined(SHN_XINDEX)
1299 if (secno == SHN_XINDEX) {
1300 secno = shndx_table[ELF_R_SYM(info)];
1301 }
1302 #endif
1303 S = (Elf_Addr)oc->sections[secno].start
1304 + stab[ELF_R_SYM(info)].st_value;
1305 #ifdef ELF_FUNCTION_DESC
1306 /* Make a function descriptor for this function */
1307 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
1308 S = allocateFunctionDesc(S + A);
1309 A = 0;
1310 }
1311 #endif
1312 } else {
1313 /* No, so look up the name in our global table. */
1314 symbol = strtab + sym.st_name;
1315 S_tmp = lookupSymbol_( symbol );
1316 S = (Elf_Addr)S_tmp;
1317
1318 #ifdef ELF_FUNCTION_DESC
1319 /* If a function, already a function descriptor - we would
1320 have to copy it to add an offset. */
1321 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
1322 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
1323 #endif
1324 }
1325 if (!S) {
1326 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
1327 return 0;
1328 }
1329 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
1330 }
1331
1332 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) \
1333 || defined(x86_64_HOST_ARCH)
1334 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
1335 (void*)P, (void*)S, (void*)A ));
1336 checkProddableBlock(oc, (void*)P, sizeof(Elf_Word));
1337 #endif
1338
1339 #if defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1340 value = S + A;
1341 #endif
1342
1343 switch (ELF_R_TYPE(info)) {
1344 # if defined(sparc_HOST_ARCH)
1345 case R_SPARC_WDISP30:
1346 w1 = *pP & 0xC0000000;
1347 w2 = (Elf_Word)((value - P) >> 2);
1348 ASSERT((w2 & 0xC0000000) == 0);
1349 w1 |= w2;
1350 *pP = w1;
1351 break;
1352 case R_SPARC_HI22:
1353 w1 = *pP & 0xFFC00000;
1354 w2 = (Elf_Word)(value >> 10);
1355 ASSERT((w2 & 0xFFC00000) == 0);
1356 w1 |= w2;
1357 *pP = w1;
1358 break;
1359 case R_SPARC_LO10:
1360 w1 = *pP & ~0x3FF;
1361 w2 = (Elf_Word)(value & 0x3FF);
1362 ASSERT((w2 & ~0x3FF) == 0);
1363 w1 |= w2;
1364 *pP = w1;
1365 break;
1366
1367 /* According to the Sun documentation:
1368 R_SPARC_UA32
1369 This relocation type resembles R_SPARC_32, except it refers to an
1370 unaligned word. That is, the word to be relocated must be treated
1371 as four separate bytes with arbitrary alignment, not as a word
1372 aligned according to the architecture requirements.
1373 */
1374 case R_SPARC_UA32:
1375 w2 = (Elf_Word)value;
1376
1377 // SPARC doesn't do misaligned writes of 32 bit words,
1378 // so we have to do this one byte-at-a-time.
1379 char *pPc = (char*)pP;
1380 pPc[0] = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
1381 pPc[1] = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
1382 pPc[2] = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
1383 pPc[3] = (char) ((Elf_Word)(w2 & 0x000000ff));
1384 break;
1385
1386 case R_SPARC_32:
1387 w2 = (Elf_Word)value;
1388 *pP = w2;
1389 break;
1390 # elif defined(powerpc_HOST_ARCH)
1391 case R_PPC_ADDR16_LO:
1392 *(Elf32_Half*) P = value;
1393 break;
1394
1395 case R_PPC_ADDR16_HI:
1396 *(Elf32_Half*) P = value >> 16;
1397 break;
1398
1399 case R_PPC_ADDR16_HA:
1400 *(Elf32_Half*) P = (value + 0x8000) >> 16;
1401 break;
1402
1403 case R_PPC_ADDR32:
1404 *(Elf32_Word *) P = value;
1405 break;
1406
1407 case R_PPC_REL32:
1408 *(Elf32_Word *) P = value - P;
1409 break;
1410
1411 case R_PPC_PLTREL24:
1412 value -= 0x8000; /* See Note [.LCTOC1 in PPC PIC code] */
1413 /* fallthrough */
1414 case R_PPC_REL24:
1415 delta = value - P;
1416
1417 if( delta << 6 >> 6 != delta )
1418 {
1419 value = (Elf_Addr) (&makeSymbolExtra( oc, ELF_R_SYM(info), value )
1420 ->jumpIsland);
1421 delta = value - P;
1422
1423 if( value == 0 || delta << 6 >> 6 != delta )
1424 {
1425 barf( "Unable to make SymbolExtra for #%d",
1426 ELF_R_SYM(info) );
1427 return 0;
1428 }
1429 }
1430
1431 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
1432 | (delta & 0x3fffffc);
1433 break;
1434
1435 case R_PPC_REL16_LO:
1436 *(Elf32_Half*) P = value - P;
1437 break;
1438
1439 case R_PPC_REL16_HI:
1440 *(Elf32_Half*) P = (value - P) >> 16;
1441 break;
1442
1443 case R_PPC_REL16_HA:
1444 *(Elf32_Half*) P = (value + 0x8000 - P) >> 16;
1445 break;
1446 # endif
1447
1448 #if x86_64_HOST_ARCH
1449 case R_X86_64_64:
1450 *(Elf64_Xword *)P = value;
1451 break;
1452
1453 case R_X86_64_PC32:
1454 {
1455 #if defined(ALWAYS_PIC)
1456 barf("R_X86_64_PC32 relocation, but ALWAYS_PIC.");
1457 #else
1458 StgInt64 off = value - P;
1459 if (off >= 0x7fffffffL || off < -0x80000000L) {
1460 if (X86_64_ELF_NONPIC_HACK) {
1461 StgInt64 pltAddress =
1462 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1463 -> jumpIsland;
1464 off = pltAddress + A - P;
1465 } else {
1466 errorBelch("R_X86_64_PC32 relocation out of range: %s = %"
1467 PRId64 "d\nRecompile %s with -fPIC.",
1468 symbol, off, oc->fileName );
1469 return 0;
1470 }
1471 }
1472 *(Elf64_Word *)P = (Elf64_Word)off;
1473 #endif
1474 break;
1475 }
1476
1477 case R_X86_64_PC64:
1478 {
1479 StgInt64 off = value - P;
1480 *(Elf64_Word *)P = (Elf64_Word)off;
1481 break;
1482 }
1483
1484 case R_X86_64_32:
1485 #if defined(ALWAYS_PIC)
1486 barf("R_X86_64_32 relocation, but ALWAYS_PIC.");
1487 #else
1488 if (value >= 0x7fffffffL) {
1489 if (X86_64_ELF_NONPIC_HACK) {
1490 StgInt64 pltAddress =
1491 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1492 -> jumpIsland;
1493 value = pltAddress + A;
1494 } else {
1495 errorBelch("R_X86_64_32 relocation out of range: %s = %"
1496 PRId64 "d\nRecompile %s with -fPIC.",
1497 symbol, value, oc->fileName );
1498 return 0;
1499 }
1500 }
1501 *(Elf64_Word *)P = (Elf64_Word)value;
1502 #endif
1503 break;
1504
1505 case R_X86_64_32S:
1506 #if defined(ALWAYS_PIC)
1507 barf("R_X86_64_32S relocation, but ALWAYS_PIC.");
1508 #else
1509 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
1510 if (X86_64_ELF_NONPIC_HACK) {
1511 StgInt64 pltAddress =
1512 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1513 -> jumpIsland;
1514 value = pltAddress + A;
1515 } else {
1516 errorBelch("R_X86_64_32S relocation out of range: %s = %"
1517 PRId64 "d\nRecompile %s with -fPIC.",
1518 symbol, value, oc->fileName );
1519 return 0;
1520 }
1521 }
1522 *(Elf64_Sword *)P = (Elf64_Sword)value;
1523 #endif
1524 break;
1525 /* These two relocations were introduced in glibc 2.23 and binutils 2.26.
1526 But in order to use them the system which compiles the bindist for GHC needs
1527 to have glibc >= 2.23. So only use them if they're defined. */
1528 #if defined(R_X86_64_REX_GOTPCRELX) && defined(R_X86_64_GOTPCRELX)
1529 case R_X86_64_REX_GOTPCRELX:
1530 case R_X86_64_GOTPCRELX:
1531 #endif
1532 case R_X86_64_GOTPCREL:
1533 {
1534 StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
1535 StgInt64 off = gotAddress + A - P;
1536 *(Elf64_Word *)P = (Elf64_Word)off;
1537 break;
1538 }
1539 #if defined(dragonfly_HOST_OS)
1540 case R_X86_64_GOTTPOFF:
1541 {
1542 #if defined(ALWAYS_PIC)
1543 barf("R_X86_64_GOTTPOFF relocation, but ALWAYS_PIC.");
1544 #else
1545 /* determine the offset of S to the current thread's tls
1546 area
1547 XXX: Move this to the beginning of function */
1548 struct tls_info ti;
1549 get_tls_area(0, &ti, sizeof(ti));
1550 /* make entry in GOT that contains said offset */
1551 StgInt64 gotEntry = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info),
1552 (S - (Elf64_Addr)(ti.base)))->addr;
1553 *(Elf64_Word *)P = gotEntry + A - P;
1554 #endif
1555 break;
1556 }
1557 #endif
1558
1559
1560
1561 case R_X86_64_PLT32:
1562 {
1563 #if defined(ALWAYS_PIC)
1564 barf("R_X86_64_PLT32 relocation, but ALWAYS_PIC.");
1565 #else
1566 StgInt64 off = value - P;
1567 if (off >= 0x7fffffffL || off < -0x80000000L) {
1568 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1569 -> jumpIsland;
1570 off = pltAddress + A - P;
1571 }
1572 *(Elf64_Word *)P = (Elf64_Word)off;
1573 #endif
1574 break;
1575 }
1576 #endif
1577
1578 default:
1579 errorBelch("%s: unhandled ELF relocation(RelA) type %" FMT_Word "\n",
1580 oc->fileName, (W_)ELF_R_TYPE(info));
1581 return 0;
1582 }
1583
1584 }
1585 return 1;
1586 }
1587
1588 int
1589 ocResolve_ELF ( ObjectCode* oc )
1590 {
1591 int ok;
1592 Elf_Word i;
1593 char* ehdrC = (char*)(oc->image);
1594 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
1595 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
1596 const Elf_Word shnum = elf_shnum(ehdr);
1597
1598 /* Process the relocation sections. */
1599 for (i = 0; i < shnum; i++) {
1600 if (shdr[i].sh_type == SHT_REL) {
1601 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr, i );
1602 if (!ok) return ok;
1603 }
1604 else
1605 if (shdr[i].sh_type == SHT_RELA) {
1606 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr, i );
1607 if (!ok) return ok;
1608 }
1609 }
1610
1611 #if defined(powerpc_HOST_ARCH) || defined(arm_HOST_ARCH)
1612 ocFlushInstructionCache( oc );
1613 #endif
1614
1615 return 1;
1616 }
1617
1618 int ocRunInit_ELF( ObjectCode *oc )
1619 {
1620 Elf_Word i;
1621 char* ehdrC = (char*)(oc->image);
1622 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
1623 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
1624 char* sh_strtab = ehdrC + shdr[elf_shstrndx(ehdr)].sh_offset;
1625 int argc, envc;
1626 char **argv, **envv;
1627
1628 getProgArgv(&argc, &argv);
1629 getProgEnvv(&envc, &envv);
1630
1631 // XXX Apparently in some archs .init may be something
1632 // special! See DL_DT_INIT_ADDRESS macro in glibc
1633 // as well as ELF_FUNCTION_PTR_IS_SPECIAL. We've not handled
1634 // it here, please file a bug report if it affects you.
1635 for (i = 0; i < elf_shnum(ehdr); i++) {
1636 init_t *init_start, *init_end, *init;
1637 int is_bss = false;
1638 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
1639 if (kind == SECTIONKIND_CODE_OR_RODATA
1640 && 0 == memcmp(".init", sh_strtab + shdr[i].sh_name, 5)) {
1641 init_t init_f = (init_t)(oc->sections[i].start);
1642 init_f(argc, argv, envv);
1643 }
1644
1645 if (kind == SECTIONKIND_INIT_ARRAY) {
1646 char *init_startC = oc->sections[i].start;
1647 init_start = (init_t*)init_startC;
1648 init_end = (init_t*)(init_startC + shdr[i].sh_size);
1649 for (init = init_start; init < init_end; init++) {
1650 (*init)(argc, argv, envv);
1651 }
1652 }
1653
1654 // XXX could be more strict and assert that it's
1655 // SECTIONKIND_RWDATA; but allowing RODATA seems harmless enough.
1656 if ((kind == SECTIONKIND_RWDATA || kind == SECTIONKIND_CODE_OR_RODATA)
1657 && 0 == memcmp(".ctors", sh_strtab + shdr[i].sh_name, 6)) {
1658 char *init_startC = oc->sections[i].start;
1659 init_start = (init_t*)init_startC;
1660 init_end = (init_t*)(init_startC + shdr[i].sh_size);
1661 // ctors run in reverse
1662 for (init = init_end - 1; init >= init_start; init--) {
1663 (*init)(argc, argv, envv);
1664 }
1665 }
1666 }
1667
1668 freeProgEnvv(envc, envv);
1669 return 1;
1670 }
1671
1672 /*
1673 * PowerPC & X86_64 ELF specifics
1674 */
1675
1676 #if NEED_SYMBOL_EXTRAS
1677
1678 int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
1679 {
1680 Elf_Ehdr *ehdr;
1681 Elf_Shdr* shdr;
1682 Elf_Word i, shnum;
1683
1684 ehdr = (Elf_Ehdr *) oc->image;
1685 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
1686
1687 shnum = elf_shnum(ehdr);
1688
1689 for( i = 0; i < shnum; i++ )
1690 if( shdr[i].sh_type == SHT_SYMTAB )
1691 break;
1692
1693 if( i == shnum )
1694 {
1695 // Not having a symbol table is not in principle a problem.
1696 // When an object file has no symbols then the 'strip' program
1697 // typically will remove the symbol table entirely.
1698 IF_DEBUG(linker, debugBelch( "The ELF file %s contains no symtab\n",
1699 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName ));
1700 return 1;
1701 }
1702
1703 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
1704 {
1705 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
1706 (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
1707
1708 return 0;
1709 }
1710
1711 return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
1712 }
1713
1714 #endif /* NEED_SYMBOL_EXTRAS */
1715
1716 #endif /* elf */