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