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