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