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