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