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