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