fe87aed0b0f5bb88aeecdec7618adbc2674fcdd0
[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 int 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
685 if (is_bss && size > 0) {
686 /* This is a non-empty .bss section. Allocate zeroed space for
687 it, and set its .sh_offset field such that
688 ehdrC + .sh_offset == addr_of_zeroed_space. */
689 #if defined(NEED_GOT)
690 /* always use mmap if we use GOT slots. Otherwise the malloced
691 * address might be out of range for sections that are mmaped.
692 */
693 alloc = SECTION_MMAP;
694 start = mmap(NULL, size,
695 PROT_READ | PROT_WRITE | PROT_EXEC,
696 MAP_ANON | MAP_PRIVATE,
697 -1, 0);
698 mapped_start = start;
699 mapped_offset = 0;
700 mapped_size = roundUpToPage(size);
701 #else
702 alloc = SECTION_MALLOC;
703 start = stgCallocBytes(1, size, "ocGetNames_ELF(BSS)");
704 mapped_start = start;
705 #endif
706 /*
707 debugBelch("BSS section at 0x%x, size %d\n",
708 zspace, shdr[i].sh_size);
709 */
710 addSection(&sections[i], kind, alloc, start, size,
711 mapped_offset, mapped_start, mapped_size);
712
713 oc->sections[i].info->nstubs = 0;
714 oc->sections[i].info->stub_offset = NULL;
715 oc->sections[i].info->stub_size = 0;
716 oc->sections[i].info->stubs = NULL;
717 } else if (kind != SECTIONKIND_OTHER && size > 0) {
718
719 #if defined(NEED_PLT)
720 /* To support stubs next to sections, we will use the following
721 * layout:
722 *
723 * .--------------.
724 * | Section data |
725 * |--------------|
726 * | Stub space |
727 * '--------------'
728 *
729 * This ensures that the plt stubs are in range for the section data,
730 * Unless the section data exceeds the size for relative jump, in
731 * which case I wouldn't know how to solve this, without starting to
732 * break up the section itself.
733 */
734
735 unsigned nstubs = numberOfStubsForSection(oc, i);
736 unsigned stub_space = STUB_SIZE * nstubs;
737
738 void * mem = mmap(NULL, size+stub_space,
739 PROT_READ | PROT_WRITE | PROT_EXEC,
740 MAP_ANON | MAP_PRIVATE,
741 -1, 0);
742
743 if( mem == MAP_FAILED ) {
744 barf("failed to mmap allocated memory to load section %d. "
745 "errno = %d", i, errno);
746 }
747
748 /* copy only the image part over; we don't want to copy data
749 * into the stub part.
750 */
751 memcpy( mem, oc->image + offset, size );
752
753 alloc = SECTION_MMAP;
754
755 mapped_offset = 0;
756 mapped_size = roundUpToPage(size+stub_space);
757 start = mem;
758 mapped_start = mem;
759 #else
760 if (USE_CONTIGUOUS_MMAP || RtsFlags.MiscFlags.linkerAlwaysPic) {
761 // already mapped.
762 start = oc->image + offset;
763 alloc = SECTION_NOMEM;
764 }
765 // use the m32 allocator if either the image is not mapped
766 // (i.e. we cannot map the secions separately), or if the section
767 // size is small.
768 else if (!oc->imageMapped || size < getPageSize() / 3) {
769 start = m32_alloc(size, 8);
770 if (start == NULL) goto fail;
771 memcpy(start, oc->image + offset, size);
772 alloc = SECTION_M32;
773 } else {
774 start = mapObjectFileSection(fd, offset, size,
775 &mapped_start, &mapped_size,
776 &mapped_offset);
777 if (start == NULL) goto fail;
778 alloc = SECTION_MMAP;
779 }
780 #endif
781 addSection(&sections[i], kind, alloc, start, size,
782 mapped_offset, mapped_start, mapped_size);
783
784 #if defined(NEED_PLT)
785 oc->sections[i].info->nstubs = 0;
786 oc->sections[i].info->stub_offset = (uint8_t*)mem + size;
787 oc->sections[i].info->stub_size = stub_space;
788 oc->sections[i].info->stubs = NULL;
789 #else
790 oc->sections[i].info->nstubs = 0;
791 oc->sections[i].info->stub_offset = NULL;
792 oc->sections[i].info->stub_size = 0;
793 oc->sections[i].info->stubs = NULL;
794 #endif
795
796 addProddableBlock(oc, start, size);
797 } else {
798 addSection(&oc->sections[i], kind, alloc, oc->image+offset, size,
799 0, 0, 0);
800 oc->sections[i].info->nstubs = 0;
801 oc->sections[i].info->stub_offset = NULL;
802 oc->sections[i].info->stub_size = 0;
803 oc->sections[i].info->stubs = NULL;
804 }
805 oc->sections[i].info->name = oc->info->sectionHeaderStrtab
806 + shdr[i].sh_name;
807 oc->sections[i].info->sectionHeader = &shdr[i];
808
809
810
811
812 if (shdr[i].sh_type != SHT_SYMTAB) continue;
813
814 /* copy stuff into this module's object symbol table */
815
816 oc->n_symbols = 0;
817 for(ElfSymbolTable *symTab = oc->info->symbolTables;
818 symTab != NULL; symTab = symTab->next) {
819 oc->n_symbols += symTab->n_symbols;
820 }
821
822 oc->symbols = stgCallocBytes(oc->n_symbols, sizeof(SymbolName*),
823 "ocGetNames_ELF(oc->symbols)");
824 // Note calloc: if we fail partway through initializing symbols, we need
825 // to undo the additions to the symbol table so far. We know which ones
826 // have been added by whether the entry is NULL or not.
827
828 unsigned curSymbol = 0;
829
830 //TODO: we ignore local symbols anyway right? So we can use the
831 // shdr[i].sh_info to get the index of the first non-local symbol
832 // ie we should use j = shdr[i].sh_info
833 for(ElfSymbolTable *symTab = oc->info->symbolTables;
834 symTab != NULL; symTab = symTab->next) {
835 for (size_t j = 0; j < symTab->n_symbols; j++) {
836
837 char isLocal = false; /* avoids uninit-var warning */
838 HsBool isWeak = HS_BOOL_FALSE;
839 SymbolName *nm = symTab->symbols[j].name;
840 unsigned short shndx = symTab->symbols[j].elf_sym->st_shndx;
841
842 ElfSymbol *symbol = &symTab->symbols[j];
843
844 Elf_Word secno;
845
846
847 /* See Note [Many ELF Sections] */
848 /* Note that future checks for special SHN_* numbers should check
849 * the shndx variable, not the section number in secno. Sections
850 * with the real number in the SHN_LORESERVE..HIRESERVE range
851 * will have shndx SHN_XINDEX and a secno with one of the
852 * reserved values. */
853 secno = shndx;
854 #if defined(SHN_XINDEX)
855 if (shndx == SHN_XINDEX) {
856 ASSERT(shndxTable);
857 secno = shndxTable[j];
858 }
859 #endif
860 /* Figure out if we want to add it; if so, set ad to its
861 address. Otherwise leave ad == NULL. */
862
863 if (shndx == SHN_COMMON) {
864 isLocal = false;
865 symbol->addr = stgCallocBytes(1, symbol->elf_sym->st_size,
866 "ocGetNames_ELF(COMMON)");
867 /*
868 debugBelch("COMMON symbol, size %d name %s\n",
869 stab[j].st_size, nm);
870 */
871 /* Pointless to do addProddableBlock() for this area,
872 since the linker should never poke around in it. */
873 } else if ((ELF_ST_BIND(symbol->elf_sym->st_info) == STB_GLOBAL
874 || ELF_ST_BIND(symbol->elf_sym->st_info) == STB_LOCAL
875 || ELF_ST_BIND(symbol->elf_sym->st_info) == STB_WEAK
876 )
877 /* and not an undefined symbol */
878 && shndx != SHN_UNDEF
879 /* and not in a "special section" */
880 && (shndx < SHN_LORESERVE
881 #if defined(SHN_XINDEX)
882 || shndx == SHN_XINDEX
883 #endif
884 )
885 &&
886 /* and it's a not a section or string table or
887 * anything silly */
888 (ELF_ST_TYPE(symbol->elf_sym->st_info) == STT_FUNC
889 || ELF_ST_TYPE(symbol->elf_sym->st_info) == STT_OBJECT
890 || ELF_ST_TYPE(symbol->elf_sym->st_info) == STT_NOTYPE
891 )
892 ) {
893 /* Section 0 is the undefined section, hence > and not >=. */
894 ASSERT(secno > 0 && secno < shnum);
895 /*
896 if (shdr[secno].sh_type == SHT_NOBITS) {
897 debugBelch(" BSS symbol, size %d off %d name %s\n",
898 stab[j].st_size, stab[j].st_value, nm);
899 }
900 */
901 symbol->addr = (SymbolAddr*)(
902 (intptr_t) oc->sections[secno].start +
903 (intptr_t) symbol->elf_sym->st_value);
904
905 if (ELF_ST_BIND(symbol->elf_sym->st_info) == STB_LOCAL) {
906 isLocal = true;
907 isWeak = false;
908 } else { /* STB_GLOBAL or STB_WEAK */
909 IF_DEBUG(linker,
910 debugBelch("addOTabName(GLOB): %10p %s %s\n",
911 symbol->addr, oc->fileName, nm));
912 isLocal = false;
913 isWeak = ELF_ST_BIND(symbol->elf_sym->st_info)
914 == STB_WEAK;
915 }
916 }
917
918 /* And the decision is ... */
919
920 if (symbol->addr != NULL) {
921 ASSERT(nm != NULL);
922 /* Acquire! */
923 if (!isLocal) {
924
925 if (isWeak == HS_BOOL_TRUE) {
926 setWeakSymbol(oc, nm);
927 }
928 if (!ghciInsertSymbolTable(oc->fileName, symhash,
929 nm, symbol->addr, isWeak, oc)
930 ) {
931 goto fail;
932 }
933 oc->symbols[curSymbol++] = nm;
934 }
935 } else {
936 /* Skip. */
937 IF_DEBUG(linker,
938 debugBelch("skipping `%s'\n",
939 nm)
940 );
941
942 /*
943 debugBelch(
944 "skipping bind = %d, type = %d, secno = %d `%s'\n",
945 (int)ELF_ST_BIND(stab[j].st_info),
946 (int)ELF_ST_TYPE(stab[j].st_info),
947 (int)secno,
948 nm
949 );
950 */
951 }
952 }
953 }
954 }
955
956 #if defined(NEED_GOT)
957 if(makeGot( oc ))
958 errorBelch("Failed to create GOT for %s",
959 oc->archiveMemberName
960 ? oc->archiveMemberName
961 : oc->fileName);
962 #endif
963 result = 1;
964 goto end;
965
966 fail:
967 result = 0;
968 goto end;
969
970 end:
971 if (fd >= 0) close(fd);
972 return result;
973 }
974
975 // the aarch64 linker uses relocacteObjectCodeAarch64,
976 // see elf_reloc_aarch64.{h,c}
977 #if !defined(aarch64_HOST_ARCH)
978
979 /* Do ELF relocations which lack an explicit addend. All x86-linux
980 and arm-linux relocations appear to be of this form. */
981 static int
982 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
983 Elf_Shdr* shdr, int shnum )
984 {
985 int j;
986
987 Elf_Word* targ;
988 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
989
990 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
991 int target_shndx = shdr[shnum].sh_info;
992 int symtab_shndx = shdr[shnum].sh_link;
993
994 ElfSymbolTable *stab = NULL;
995 for(ElfSymbolTable * st = oc->info->symbolTables;
996 st != NULL; st = st->next) {
997 if((int)st->index == symtab_shndx) {
998 stab = st;
999 break;
1000 }
1001 }
1002 ASSERT(stab != NULL);
1003
1004 targ = (Elf_Word*)oc->sections[target_shndx].start;
1005 IF_DEBUG(linker,debugBelch(
1006 "relocations for section %d using symtab %d\n",
1007 target_shndx, symtab_shndx));
1008
1009 /* Skip sections that we're not interested in. */
1010 if (oc->sections[target_shndx].kind == SECTIONKIND_OTHER) {
1011 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
1012 return 1;
1013 }
1014
1015 for (j = 0; j < nent; j++) {
1016 Elf_Addr offset = rtab[j].r_offset;
1017 Elf_Addr info = rtab[j].r_info;
1018
1019 Elf_Addr P = ((Elf_Addr)targ) + offset;
1020 Elf_Word* pP = (Elf_Word*)P;
1021 #if defined(i386_HOST_ARCH) || defined(DEBUG)
1022 Elf_Addr A = *pP;
1023 #endif
1024 Elf_Addr S;
1025 void* S_tmp;
1026 #if defined(i386_HOST_ARCH)
1027 Elf_Addr value;
1028 #endif
1029 #if defined(arm_HOST_ARCH)
1030 int is_target_thm=0, T=0;
1031 #endif
1032
1033 ElfSymbol * symbol = NULL;
1034
1035 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p): ",
1036 j, (void*)offset, (void*)info ));
1037 if (!info) {
1038 IF_DEBUG(linker,debugBelch( " ZERO" ));
1039 S = 0;
1040 } else {
1041 symbol = &stab->symbols[ELF_R_SYM(info)];
1042 /* First see if it is a local symbol. */
1043 if (ELF_ST_BIND(symbol->elf_sym->st_info) == STB_LOCAL) {
1044 S = (Elf_Addr)symbol->addr;
1045 } else {
1046 S_tmp = lookupSymbol_( symbol->name );
1047 S = (Elf_Addr)S_tmp;
1048 }
1049 if (!S) {
1050 errorBelch("%s: unknown symbol `%s'",
1051 oc->fileName, symbol->name);
1052 return 0;
1053 }
1054 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol->name,
1055 (void*)S ));
1056
1057 #if defined(arm_HOST_ARCH)
1058 /*
1059 * 4.5.3 Symbol Values
1060 *
1061 * In addition to the normal rules for symbol values the following
1062 * rules shall also apply to symbols of type STT_FUNC:
1063 * - If the symbol addresses an ARM instruction, its value is the
1064 * address of the instruction (in a relocatable object, the
1065 * offset of the instruction from the start of the section
1066 * containing it).
1067 * - If the symbol addresses a Thumb instruction, its value is the
1068 * address of the instruction with bit zero set (in a relocatable
1069 * object, the section offset with bit zero set).
1070 * - For the purposes of relocation the value used shall be the
1071 * address of the instruction (st_value & ~1).
1072 *
1073 * Note: This allows a linker to distinguish ARM and Thumb code
1074 * symbols without having to refer to the map. An ARM symbol
1075 * will always have an even value, while a Thumb symbol will
1076 * always have an odd value. However, a linker should strip
1077 * the discriminating bit from the value before using it for
1078 * relocation.
1079 *
1080 * (source: ELF for the ARM Architecture
1081 * ARM IHI 0044F, current through ABI release 2.10
1082 * 24th November 2015)
1083 */
1084 if(ELF_ST_TYPE(symbol->elf_sym->st_info) == STT_FUNC) {
1085 is_target_thm = S & 0x1;
1086 T = is_target_thm;
1087 S &= ~1;
1088 }
1089 #endif
1090 }
1091
1092 int reloc_type = ELF_R_TYPE(info);
1093 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p type=%d\n",
1094 (void*)P, (void*)S, (void*)A, reloc_type ));
1095 checkProddableBlock ( oc, pP, sizeof(Elf_Word) );
1096
1097 #if defined(i386_HOST_ARCH)
1098 value = S + A;
1099 #endif
1100
1101 switch (reloc_type) {
1102 # ifdef i386_HOST_ARCH
1103 case COMPAT_R_386_NONE: break;
1104 case COMPAT_R_386_32: *pP = value; break;
1105 case COMPAT_R_386_PC32: *pP = value - P; break;
1106 # endif
1107
1108 # ifdef arm_HOST_ARCH
1109 case COMPAT_R_ARM_ABS32:
1110 // Specified by Linux ARM ABI to be equivalent to ABS32
1111 case COMPAT_R_ARM_TARGET1:
1112 *(Elf32_Word *)P += S;
1113 *(Elf32_Word *)P |= T;
1114 break;
1115
1116 case COMPAT_R_ARM_REL32:
1117 *(Elf32_Word *)P += S;
1118 *(Elf32_Word *)P |= T;
1119 *(Elf32_Word *)P -= P;
1120 break;
1121
1122 case COMPAT_R_ARM_CALL:
1123 case COMPAT_R_ARM_JUMP24:
1124 {
1125 // N.B. LLVM's LLD linker's relocation implementation is a fantastic
1126 // resource
1127 StgWord32 *word = (StgWord32 *)P;
1128 StgInt32 imm = (*word & ((1<<24)-1)) << 2;
1129
1130 const StgBool is_blx = (*word & 0xf0000000) == 0xf0000000;
1131 const StgWord32 hBit = is_blx ? ((*word >> 24) & 1) : 0;
1132 imm |= hBit << 1;
1133
1134 // Sign extend to 32 bits
1135 // I would have thought this would be 24 bits but LLD uses 26 here.
1136 // Hmm.
1137 int32_t A = signExtend32(26, imm);
1138
1139 S = S + A; A = 0;
1140
1141 StgWord32 result = ((S + A) | T) - P;
1142
1143 const StgBool overflow = !isInt(26, (StgInt32) result);
1144 // Handle overflow and Thumb interworking
1145 const StgBool needs_veneer =
1146 (is_target_thm && ELF_R_TYPE(info) == COMPAT_R_ARM_JUMP24)
1147 || overflow;
1148
1149 if(needs_veneer) { /* overflow or thum interworking */
1150 // Note [PC bias]
1151 // From the ELF for the ARM Architecture documentation:
1152 // > 4.6.1.1 Addends and PC-bias compensation
1153 // > A binary file may use REL or RELA relocations or a mixture
1154 // > of the two (but multiple relocations for the same address
1155 // > must use only one type).
1156 // > If the relocation is pc-relative then compensation for the
1157 // > PC bias (the PC value is 8 bytes ahead of the executing
1158 // > instruction in ARM state and 4 bytes in Thumb state) must
1159 // > be encoded in the relocation by the object producer.
1160 int32_t bias = 8;
1161
1162 S += bias;
1163 /* try to locate an existing stub for this target */
1164 if(findStub(&oc->sections[target_shndx], (void**)&S, 0)) {
1165 /* didn't find any. Need to create one */
1166 if(makeStub(&oc->sections[target_shndx], (void**)&S, 0)) {
1167 errorBelch("Unable to create veneer for ARM_CALL\n");
1168 return 0;
1169 }
1170 }
1171 S -= bias;
1172
1173 result = ((S + A) | T) - P;
1174 result &= ~1; // Clear thumb indicator bit
1175
1176 ASSERT(isInt(26, result)); /* X in range */
1177 }
1178
1179 // Update the branch target
1180 const StgWord32 imm24 = (result & 0x03fffffc) >> 2;
1181 *word = (*word & ~0x00ffffff)
1182 | (imm24 & 0x00ffffff);
1183
1184 // Change the relocated branch into a BLX if necessary
1185 const StgBool switch_mode =
1186 is_target_thm && (reloc_type == COMPAT_R_ARM_CALL);
1187 if (!needs_veneer && switch_mode) {
1188 const StgWord32 hBit = (result & 0x2) >> 1;
1189 // Change instruction to BLX
1190 *word = (*word & ~0xFF000000) | ((0xfa | hBit) << 24);
1191 IF_DEBUG(linker, debugBelch("Changed BL to BLX at %p\n", word));
1192 }
1193 break;
1194 }
1195
1196 case COMPAT_R_ARM_MOVT_ABS:
1197 case COMPAT_R_ARM_MOVW_ABS_NC:
1198 {
1199 StgWord32 *word = (StgWord32 *)P;
1200 StgWord32 imm12 = *word & 0xfff;
1201 StgWord32 imm4 = (*word >> 16) & 0xf;
1202 StgInt32 offset = imm4 << 12 | imm12;
1203 StgWord32 result = (S + offset) | T;
1204
1205 if (reloc_type == COMPAT_R_ARM_MOVT_ABS)
1206 result = (result & 0xffff0000) >> 16;
1207
1208 StgWord32 result12 = result & 0xfff;
1209 StgWord32 result4 = (result >> 12) & 0xf;
1210 *word = (*word & ~0xf0fff) | (result4 << 16) | result12;
1211 break;
1212 }
1213
1214 case COMPAT_R_ARM_THM_CALL:
1215 case COMPAT_R_ARM_THM_JUMP24:
1216 {
1217 StgWord16 *upper = (StgWord16 *)P;
1218 StgWord16 *lower = (StgWord16 *)(P + 2);
1219
1220 int overflow;
1221 int to_thm = (*lower >> 12) & 1;
1222 int sign = (*upper >> 10) & 1;
1223 int j1, j2, i1, i2;
1224
1225 // Decode immediate value
1226 j1 = (*lower >> 13) & 1; i1 = ~(j1 ^ sign) & 1;
1227 j2 = (*lower >> 11) & 1; i2 = ~(j2 ^ sign) & 1;
1228
1229 StgInt32 A = (sign << 24)
1230 | (i1 << 23)
1231 | (i2 << 22)
1232 | ((*upper & 0x03ff) << 12)
1233 | ((*lower & 0x07ff) << 1);
1234
1235 // Sign extend 25 to 32 bits
1236 if (A & 0x01000000)
1237 A -= 0x02000000;
1238
1239 S = S + A; A = 0;
1240
1241 offset = ((S + A) | T) - P;
1242 overflow = offset <= (StgWord32)0xff000000
1243 || offset >= (StgWord32)0x01000000;
1244
1245 if ((!is_target_thm && ELF_R_TYPE(info) == COMPAT_R_ARM_THM_JUMP24)
1246 || overflow) {
1247 // Generate veneer
1248
1249 // see [PC bias] above.
1250 int32_t bias = 4;
1251 S += bias;
1252 // set the Thumb indicator to S, the final address should
1253 // carry the correct thumb indicator.
1254 S |= T;
1255 /* try to locate an existing stub for this target */
1256 if(findStub(&oc->sections[target_shndx], (void**)&S, 1)) {
1257 /* didn't find any. Need to create one */
1258 if(makeStub(&oc->sections[target_shndx], (void**)&S, 1)) {
1259 errorBelch("Unable to create veneer for ARM_THM_CALL\n");
1260 return 0;
1261 }
1262 }
1263 S -= bias;
1264
1265 offset = ((S + A) | T) - P;
1266
1267 sign = offset >> 31;
1268 to_thm = 1;
1269 } else if (!is_target_thm
1270 && ELF_R_TYPE(info) == COMPAT_R_ARM_THM_CALL) {
1271 offset &= ~0x3;
1272 to_thm = 0;
1273 }
1274
1275 // Reencode instruction
1276 i1 = ~(offset >> 23) & 1; j1 = sign ^ i1;
1277 i2 = ~(offset >> 22) & 1; j2 = sign ^ i2;
1278 *upper = ( (*upper & 0xf800)
1279 | (sign << 10)
1280 | ((offset >> 12) & 0x03ff) );
1281 *lower = ( (*lower & 0xd000)
1282 | (j1 << 13)
1283 | (to_thm << 12)
1284 | (j2 << 11)
1285 | ((offset >> 1) & 0x07ff) );
1286 break;
1287 }
1288
1289 case COMPAT_R_ARM_THM_MOVT_ABS:
1290 case COMPAT_R_ARM_THM_MOVW_ABS_NC:
1291 {
1292 StgWord16 *upper = (StgWord16 *)P;
1293 StgWord16 *lower = (StgWord16 *)(P + 2);
1294 StgInt32 offset = ((*upper & 0x000f) << 12)
1295 | ((*upper & 0x0400) << 1)
1296 | ((*lower & 0x7000) >> 4)
1297 | (*lower & 0x00ff);
1298
1299 offset = (offset ^ 0x8000) - 0x8000; // Sign extend
1300 offset += S;
1301 if (ELF_R_TYPE(info) == COMPAT_R_ARM_THM_MOVW_ABS_NC)
1302 offset |= T;
1303 else if (ELF_R_TYPE(info) == COMPAT_R_ARM_THM_MOVT_ABS)
1304 offset >>= 16;
1305
1306 *upper = ( (*upper & 0xfbf0)
1307 | ((offset & 0xf000) >> 12)
1308 | ((offset & 0x0800) >> 1) );
1309 *lower = ( (*lower & 0x8f00)
1310 | ((offset & 0x0700) << 4)
1311 | (offset & 0x00ff) );
1312 break;
1313 }
1314
1315 case COMPAT_R_ARM_THM_JUMP8:
1316 {
1317 StgWord16 *word = (StgWord16 *)P;
1318 StgWord offset = *word & 0x01fe;
1319 offset += S - P;
1320 if (!is_target_thm) {
1321 errorBelch("%s: Thumb to ARM transition with JUMP8 relocation "
1322 "not supported\n",
1323 oc->fileName);
1324 return 0;
1325 }
1326
1327 *word = (*word & ~0x01fe)
1328 | (offset & 0x01fe);
1329 break;
1330 }
1331
1332 case COMPAT_R_ARM_THM_JUMP11:
1333 {
1334 StgWord16 *word = (StgWord16 *)P;
1335 StgWord offset = *word & 0x0ffe;
1336 offset += S - P;
1337 if (!is_target_thm) {
1338 errorBelch("%s: Thumb to ARM transition with JUMP11 relocation "
1339 "not supported\n",
1340 oc->fileName);
1341 return 0;
1342 }
1343
1344 *word = (*word & ~0x0ffe)
1345 | (offset & 0x0ffe);
1346 break;
1347 }
1348 case COMPAT_R_ARM_GOT_PREL: {
1349 int32_t A = *pP;
1350 void* GOT_S = symbol->got_addr;
1351 ASSERT(GOT_S);
1352 *(uint32_t *)P = (uint32_t) GOT_S + A - P;
1353 break;
1354 }
1355 # endif // arm_HOST_ARCH
1356
1357 default:
1358 errorBelch("%s: unhandled ELF relocation(Rel) type %" FMT_Word "\n",
1359 oc->fileName, (W_)ELF_R_TYPE(info));
1360 return 0;
1361 }
1362
1363 }
1364 return 1;
1365 }
1366
1367 /* Do ELF relocations for which explicit addends are supplied.
1368 sparc-solaris relocations appear to be of this form. */
1369 static int
1370 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1371 Elf_Shdr* shdr, int shnum )
1372 {
1373 int j;
1374 SymbolName* symbol = NULL;
1375 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
1376 Elf_Sym* stab;
1377 char* strtab;
1378 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
1379 int symtab_shndx = shdr[shnum].sh_link;
1380 int strtab_shndx = shdr[symtab_shndx].sh_link;
1381 int target_shndx = shdr[shnum].sh_info;
1382 #if defined(SHN_XINDEX)
1383 Elf_Word* shndx_table = get_shndx_table((Elf_Ehdr*)ehdrC);
1384 #endif
1385 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) \
1386 || defined(x86_64_HOST_ARCH)
1387 /* This #if def only serves to avoid unused-var warnings. */
1388 Elf_Addr targ = (Elf_Addr) oc->sections[target_shndx].start;
1389 #endif
1390
1391 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1392 strtab= (char*) (ehdrC + shdr[ strtab_shndx ].sh_offset);
1393
1394 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
1395 target_shndx, symtab_shndx ));
1396
1397 /* Skip sections that we're not interested in. */
1398 if (oc->sections[target_shndx].kind == SECTIONKIND_OTHER) {
1399 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
1400 return 1;
1401 }
1402
1403 for (j = 0; j < nent; j++) {
1404 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) \
1405 || defined(x86_64_HOST_ARCH)
1406 /* This #if def only serves to avoid unused-var warnings. */
1407 Elf_Addr offset = rtab[j].r_offset;
1408 Elf_Addr P = targ + offset;
1409 Elf_Addr A = rtab[j].r_addend;
1410 #endif
1411 #if defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) \
1412 || defined(x86_64_HOST_ARCH)
1413 Elf_Addr value;
1414 #endif
1415 Elf_Addr info = rtab[j].r_info;
1416 Elf_Addr S;
1417 void* S_tmp;
1418 # if defined(sparc_HOST_ARCH)
1419 Elf_Word* pP = (Elf_Word*)P;
1420 Elf_Word w1, w2;
1421 # elif defined(powerpc_HOST_ARCH)
1422 Elf_Sword delta;
1423 # endif
1424
1425 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
1426 j, (void*)offset, (void*)info,
1427 (void*)A ));
1428 if (!info) {
1429 IF_DEBUG(linker,debugBelch( " ZERO" ));
1430 S = 0;
1431 } else {
1432 Elf_Sym sym = stab[ELF_R_SYM(info)];
1433 /* First see if it is a local symbol. */
1434 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
1435 /* Yes, so we can get the address directly from the ELF symbol
1436 table. */
1437 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
1438 /* See Note [Many ELF Sections] */
1439 Elf_Word secno = sym.st_shndx;
1440 #if defined(SHN_XINDEX)
1441 if (secno == SHN_XINDEX) {
1442 secno = shndx_table[ELF_R_SYM(info)];
1443 }
1444 #endif
1445 S = (Elf_Addr)oc->sections[secno].start
1446 + stab[ELF_R_SYM(info)].st_value;
1447 } else {
1448 /* No, so look up the name in our global table. */
1449 symbol = strtab + sym.st_name;
1450 S_tmp = lookupSymbol_( symbol );
1451 S = (Elf_Addr)S_tmp;
1452 }
1453 if (!S) {
1454 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
1455 return 0;
1456 }
1457 IF_DEBUG(linker,debugBelch("`%s' resolves to %p\n", symbol, (void*)S));
1458 }
1459
1460 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) \
1461 || defined(x86_64_HOST_ARCH)
1462 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
1463 (void*)P, (void*)S, (void*)A ));
1464 checkProddableBlock(oc, (void*)P, sizeof(Elf_Word));
1465 #endif
1466
1467 #if defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) \
1468 || defined(x86_64_HOST_ARCH)
1469 value = S + A;
1470 #endif
1471
1472 switch (ELF_R_TYPE(info)) {
1473 # if defined(sparc_HOST_ARCH)
1474 case R_SPARC_WDISP30:
1475 w1 = *pP & 0xC0000000;
1476 w2 = (Elf_Word)((value - P) >> 2);
1477 ASSERT((w2 & 0xC0000000) == 0);
1478 w1 |= w2;
1479 *pP = w1;
1480 break;
1481 case R_SPARC_HI22:
1482 w1 = *pP & 0xFFC00000;
1483 w2 = (Elf_Word)(value >> 10);
1484 ASSERT((w2 & 0xFFC00000) == 0);
1485 w1 |= w2;
1486 *pP = w1;
1487 break;
1488 case R_SPARC_LO10:
1489 w1 = *pP & ~0x3FF;
1490 w2 = (Elf_Word)(value & 0x3FF);
1491 ASSERT((w2 & ~0x3FF) == 0);
1492 w1 |= w2;
1493 *pP = w1;
1494 break;
1495
1496 /* According to the Sun documentation:
1497 R_SPARC_UA32
1498 This relocation type resembles R_SPARC_32, except it refers to an
1499 unaligned word. That is, the word to be relocated must be treated
1500 as four separate bytes with arbitrary alignment, not as a word
1501 aligned according to the architecture requirements.
1502 */
1503 case R_SPARC_UA32:
1504 w2 = (Elf_Word)value;
1505
1506 // SPARC doesn't do misaligned writes of 32 bit words,
1507 // so we have to do this one byte-at-a-time.
1508 char *pPc = (char*)pP;
1509 pPc[0] = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
1510 pPc[1] = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
1511 pPc[2] = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
1512 pPc[3] = (char) ((Elf_Word)(w2 & 0x000000ff));
1513 break;
1514
1515 case R_SPARC_32:
1516 w2 = (Elf_Word)value;
1517 *pP = w2;
1518 break;
1519 # elif defined(powerpc_HOST_ARCH)
1520 case R_PPC_ADDR16_LO:
1521 *(Elf32_Half*) P = value;
1522 break;
1523
1524 case R_PPC_ADDR16_HI:
1525 *(Elf32_Half*) P = value >> 16;
1526 break;
1527
1528 case R_PPC_ADDR16_HA:
1529 *(Elf32_Half*) P = (value + 0x8000) >> 16;
1530 break;
1531
1532 case R_PPC_ADDR32:
1533 *(Elf32_Word *) P = value;
1534 break;
1535
1536 case R_PPC_REL32:
1537 *(Elf32_Word *) P = value - P;
1538 break;
1539
1540 case R_PPC_PLTREL24:
1541 value -= 0x8000; /* See Note [.LCTOC1 in PPC PIC code] */
1542 /* fallthrough */
1543 case R_PPC_REL24:
1544 delta = value - P;
1545
1546 if( delta << 6 >> 6 != delta )
1547 {
1548 value = (Elf_Addr)(&makeSymbolExtra( oc, ELF_R_SYM(info), value )
1549 ->jumpIsland);
1550 delta = value - P;
1551
1552 if( value == 0 || delta << 6 >> 6 != delta )
1553 {
1554 barf( "Unable to make SymbolExtra for #%d",
1555 ELF_R_SYM(info) );
1556 return 0;
1557 }
1558 }
1559
1560 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
1561 | (delta & 0x3fffffc);
1562 break;
1563
1564 case R_PPC_REL16_LO:
1565 *(Elf32_Half*) P = value - P;
1566 break;
1567
1568 case R_PPC_REL16_HI:
1569 *(Elf32_Half*) P = (value - P) >> 16;
1570 break;
1571
1572 case R_PPC_REL16_HA:
1573 *(Elf32_Half*) P = (value + 0x8000 - P) >> 16;
1574 break;
1575 # endif
1576
1577 #if defined(x86_64_HOST_ARCH)
1578 case COMPAT_R_X86_64_NONE:
1579 break;
1580
1581 case COMPAT_R_X86_64_64:
1582 {
1583 Elf64_Xword payload = value;
1584 memcpy((void*)P, &payload, sizeof(payload));
1585 break;
1586 }
1587
1588 case COMPAT_R_X86_64_PC32:
1589 {
1590 StgInt64 off = value - P;
1591 if (off != (Elf64_Sword)off && X86_64_ELF_NONPIC_HACK) {
1592 StgInt64 pltAddress =
1593 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1594 -> jumpIsland;
1595 off = pltAddress + A - P;
1596 }
1597 if (off != (Elf64_Sword)off) {
1598 errorBelch(
1599 "R_X86_64_PC32 relocation out of range: %s = %" PRIx64
1600 "\nRecompile %s with -fPIC -fexternal-dynamic-refs.",
1601 symbol, off, oc->fileName);
1602 return 0;
1603 }
1604 Elf64_Sword payload = off;
1605 memcpy((void*)P, &payload, sizeof(payload));
1606 break;
1607 }
1608
1609 case COMPAT_R_X86_64_PC64:
1610 {
1611 Elf64_Sxword payload = value - P;
1612 memcpy((void*)P, &payload, sizeof(payload));
1613 break;
1614 }
1615
1616 case COMPAT_R_X86_64_32:
1617 {
1618 if (value != (Elf64_Word)value && X86_64_ELF_NONPIC_HACK) {
1619 StgInt64 pltAddress =
1620 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1621 -> jumpIsland;
1622 value = pltAddress + A;
1623 }
1624 if (value != (Elf64_Word)value) {
1625 errorBelch(
1626 "R_X86_64_32 relocation out of range: %s = %" PRIx64
1627 "\nRecompile %s with -fPIC -fexternal-dynamic-refs.",
1628 symbol, value, oc->fileName);
1629 return 0;
1630 }
1631 Elf64_Word payload = value;
1632 memcpy((void*)P, &payload, sizeof(payload));
1633 break;
1634 }
1635
1636 case COMPAT_R_X86_64_32S:
1637 {
1638 if ((StgInt64)value != (Elf64_Sword)value && X86_64_ELF_NONPIC_HACK) {
1639 StgInt64 pltAddress =
1640 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1641 -> jumpIsland;
1642 value = pltAddress + A;
1643 }
1644 if ((StgInt64)value != (Elf64_Sword)value) {
1645 errorBelch(
1646 "R_X86_64_32S relocation out of range: %s = %" PRIx64
1647 "\nRecompile %s with -fPIC -fexternal-dynamic-refs.",
1648 symbol, value, oc->fileName);
1649 return 0;
1650 }
1651 Elf64_Sword payload = value;
1652 memcpy((void*)P, &payload, sizeof(payload));
1653 break;
1654 }
1655 case COMPAT_R_X86_64_REX_GOTPCRELX:
1656 case COMPAT_R_X86_64_GOTPCRELX:
1657 case COMPAT_R_X86_64_GOTPCREL:
1658 {
1659 StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
1660 StgInt64 off = gotAddress + A - P;
1661 if (off != (Elf64_Sword)off) {
1662 barf(
1663 "COMPAT_R_X86_64_GOTPCREL relocation out of range: "
1664 "%s = %" PRIx64 " in %s.",
1665 symbol, off, oc->fileName);
1666 }
1667 Elf64_Sword payload = off;
1668 memcpy((void*)P, &payload, sizeof(payload));
1669 break;
1670 }
1671 #if defined(dragonfly_HOST_OS)
1672 case COMPAT_R_X86_64_GOTTPOFF:
1673 {
1674 /* determine the offset of S to the current thread's tls
1675 area
1676 XXX: Move this to the beginning of function */
1677 struct tls_info ti;
1678 get_tls_area(0, &ti, sizeof(ti));
1679 /* make entry in GOT that contains said offset */
1680 StgInt64 gotEntry = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info),
1681 (S - (Elf64_Addr)(ti.base)))->addr;
1682 StgInt64 off = gotEntry + A - P;
1683 if (off != (Elf64_Sword)off) {
1684 barf(
1685 "COMPAT_R_X86_64_GOTTPOFF relocation out of range: "
1686 "%s = %" PRIx64 " in %s.",
1687 symbol, off, oc->fileName);
1688 }
1689 Elf64_SWord payload = off;
1690 memcpy((void*)P, &payload, sizeof(payload));
1691 break;
1692 }
1693 #endif
1694
1695 case COMPAT_R_X86_64_PLT32:
1696 {
1697 StgInt64 off = value - P;
1698 if (off != (Elf64_Sword)off) {
1699 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1700 -> jumpIsland;
1701 off = pltAddress + A - P;
1702 }
1703 if (off != (Elf64_Sword)off) {
1704 barf(
1705 "R_X86_64_PLT32 relocation out of range: "
1706 "%s = %" PRIx64 " in %s.",
1707 symbol, off, oc->fileName);
1708 }
1709 Elf64_Sword payload = off;
1710 memcpy((void*)P, &payload, sizeof(payload));
1711 break;
1712 }
1713 #endif
1714
1715 default:
1716 barf("%s: unhandled ELF relocation(RelA) type %" FMT_Word "\n",
1717 oc->fileName, (W_)ELF_R_TYPE(info));
1718 return 0;
1719 }
1720
1721 }
1722 return 1;
1723 }
1724 #endif /* !aarch64_HOST_ARCH */
1725
1726
1727 int
1728 ocResolve_ELF ( ObjectCode* oc )
1729 {
1730 char* ehdrC = (char*)(oc->image);
1731 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
1732 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
1733 const Elf_Word shnum = elf_shnum(ehdr);
1734
1735 #if defined(SHN_XINDEX)
1736 Elf_Word* shndxTable = get_shndx_table(ehdr);
1737 #endif
1738
1739 /* resolve section symbols
1740 * these are special symbols that point to sections, and have no name.
1741 * Usually there should be one symbol for each text and data section.
1742 *
1743 * We need to resolve (assign addresses) to them, to be able to use them
1744 * during relocation.
1745 */
1746 for(ElfSymbolTable *symTab = oc->info->symbolTables;
1747 symTab != NULL; symTab = symTab->next) {
1748 for (size_t i = 0; i < symTab->n_symbols; i++) {
1749 ElfSymbol *symbol = &symTab->symbols[i];
1750 if(STT_SECTION == ELF_ST_TYPE(symbol->elf_sym->st_info)) {
1751 /* NOTE: We assume that oc->sections corresponds to the
1752 * sections in the object file. This is currently true,
1753 * and will stay true, unless we start to compress
1754 * oc->sections by not having an entry for sections we
1755 * are not interested in.
1756 */
1757
1758
1759 /* See Note [Many ELF Sections] */
1760 /* Note that future checks for special SHN_* numbers should
1761 * check the shndx variable, not the section number in secno.
1762 * Sections with the real number in the SHN_LORESERVE..HIRESERVE
1763 * range will have shndx SHN_XINDEX and a secno with one of the
1764 * reserved values.
1765 */
1766 Elf_Word secno = symbol->elf_sym->st_shndx;
1767 #if defined(SHN_XINDEX)
1768 if (secno == SHN_XINDEX) {
1769 ASSERT(shndxTable);
1770 secno = shndxTable[i];
1771 }
1772 #endif
1773 ASSERT(symbol->elf_sym->st_name == 0);
1774 ASSERT(symbol->elf_sym->st_value == 0);
1775 symbol->addr = oc->sections[ secno ].start;
1776 }
1777 }
1778 }
1779
1780 #if defined(NEED_GOT)
1781 if(fillGot( oc ))
1782 return 0;
1783 /* silence warnings */
1784 (void) shnum;
1785 (void) shdr;
1786 #endif /* NEED_GOT */
1787
1788 #if defined(aarch64_HOST_ARCH)
1789 /* use new relocation design */
1790 if(relocateObjectCode( oc ))
1791 return 0;
1792 #else
1793 /* Process the relocation sections. */
1794 for (Elf_Word i = 0; i < shnum; i++) {
1795 if (shdr[i].sh_type == SHT_REL) {
1796 bool ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr, i );
1797 if (!ok)
1798 return ok;
1799 }
1800 else
1801 if (shdr[i].sh_type == SHT_RELA) {
1802 bool ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr, i );
1803 if (!ok)
1804 return ok;
1805 }
1806 }
1807 #endif
1808
1809 #if defined(powerpc_HOST_ARCH)
1810 ocFlushInstructionCache( oc );
1811 #endif
1812
1813 return 1;
1814 }
1815
1816 int ocRunInit_ELF( ObjectCode *oc )
1817 {
1818 Elf_Word i;
1819 char* ehdrC = (char*)(oc->image);
1820 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
1821 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
1822 char* sh_strtab = ehdrC + shdr[elf_shstrndx(ehdr)].sh_offset;
1823 int argc, envc;
1824 char **argv, **envv;
1825
1826 getProgArgv(&argc, &argv);
1827 getProgEnvv(&envc, &envv);
1828
1829 // XXX Apparently in some archs .init may be something
1830 // special! See DL_DT_INIT_ADDRESS macro in glibc
1831 // as well as ELF_FUNCTION_PTR_IS_SPECIAL. We've not handled
1832 // it here, please file a bug report if it affects you.
1833 for (i = 0; i < elf_shnum(ehdr); i++) {
1834 init_t *init_start, *init_end, *init;
1835 int is_bss = false;
1836 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
1837 if (kind == SECTIONKIND_CODE_OR_RODATA
1838 && 0 == memcmp(".init", sh_strtab + shdr[i].sh_name, 5)) {
1839 init_t init_f = (init_t)(oc->sections[i].start);
1840 init_f(argc, argv, envv);
1841 }
1842
1843 if (kind == SECTIONKIND_INIT_ARRAY) {
1844 char *init_startC = oc->sections[i].start;
1845 init_start = (init_t*)init_startC;
1846 init_end = (init_t*)(init_startC + shdr[i].sh_size);
1847 for (init = init_start; init < init_end; init++) {
1848 (*init)(argc, argv, envv);
1849 }
1850 }
1851
1852 // XXX could be more strict and assert that it's
1853 // SECTIONKIND_RWDATA; but allowing RODATA seems harmless enough.
1854 if ((kind == SECTIONKIND_RWDATA || kind == SECTIONKIND_CODE_OR_RODATA)
1855 && 0 == memcmp(".ctors", sh_strtab + shdr[i].sh_name, 6)) {
1856 char *init_startC = oc->sections[i].start;
1857 init_start = (init_t*)init_startC;
1858 init_end = (init_t*)(init_startC + shdr[i].sh_size);
1859 // ctors run in reverse
1860 for (init = init_end - 1; init >= init_start; init--) {
1861 (*init)(argc, argv, envv);
1862 }
1863 }
1864 }
1865
1866 freeProgEnvv(envc, envv);
1867 return 1;
1868 }
1869
1870 /*
1871 * PowerPC & X86_64 ELF specifics
1872 */
1873
1874 #if defined(NEED_SYMBOL_EXTRAS)
1875
1876 int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
1877 {
1878 Elf_Ehdr *ehdr;
1879 Elf_Shdr* shdr;
1880 Elf_Word i, shnum;
1881
1882 ehdr = (Elf_Ehdr *) oc->image;
1883 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
1884
1885 shnum = elf_shnum(ehdr);
1886
1887 for( i = 0; i < shnum; i++ )
1888 if( shdr[i].sh_type == SHT_SYMTAB )
1889 break;
1890
1891 if( i == shnum )
1892 {
1893 // Not having a symbol table is not in principle a problem.
1894 // When an object file has no symbols then the 'strip' program
1895 // typically will remove the symbol table entirely.
1896 IF_DEBUG(linker, debugBelch( "The ELF file %s contains no symtab\n",
1897 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName ));
1898 return 1;
1899 }
1900
1901 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
1902 {
1903 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
1904 (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
1905
1906 return 0;
1907 }
1908
1909 return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
1910 }
1911
1912 #endif /* NEED_SYMBOL_EXTRAS */
1913
1914 #endif /* elf */