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