Typos in comments
[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 endiannness", 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 case EM_PPC64: IF_DEBUG(linker,debugBelch( "powerpc64" ));
403 errorBelch("%s: RTS linker not implemented on PowerPC 64-bit",
404 oc->fileName);
405 return 0;
406 #ifdef EM_X86_64
407 case EM_X86_64: IF_DEBUG(linker,debugBelch( "x86_64" )); break;
408 #elif defined(EM_AMD64)
409 case EM_AMD64: IF_DEBUG(linker,debugBelch( "amd64" )); break;
410 #endif
411 default: IF_DEBUG(linker,debugBelch( "unknown" ));
412 errorBelch("%s: unknown architecture (e_machine == %d)"
413 , oc->fileName, ehdr->e_machine);
414 return 0;
415 }
416
417 shnum = elf_shnum(ehdr);
418 IF_DEBUG(linker,debugBelch(
419 "\nSection header table: start %ld, n_entries %d, ent_size %d\n",
420 (long)ehdr->e_shoff, shnum, ehdr->e_shentsize ));
421
422 ASSERT(ehdr->e_shentsize == sizeof(Elf_Shdr));
423
424 shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
425
426 shstrndx = elf_shstrndx(ehdr);
427 if (shstrndx == SHN_UNDEF) {
428 errorBelch("%s: no section header string table", oc->fileName);
429 return 0;
430 } else {
431 IF_DEBUG(linker,debugBelch( "Section header string table is section %d\n",
432 shstrndx));
433 sh_strtab = ehdrC + shdr[shstrndx].sh_offset;
434 }
435
436 for (i = 0; i < shnum; i++) {
437 IF_DEBUG(linker,debugBelch("%2d: ", i ));
438 IF_DEBUG(linker,debugBelch("type=%2d ", (int)shdr[i].sh_type ));
439 IF_DEBUG(linker,debugBelch("size=%4d ", (int)shdr[i].sh_size ));
440 IF_DEBUG(linker,debugBelch("offs=%4d ", (int)shdr[i].sh_offset ));
441 IF_DEBUG(linker,debugBelch(" (%p .. %p) ",
442 ehdrC + shdr[i].sh_offset,
443 ehdrC + shdr[i].sh_offset + shdr[i].sh_size - 1));
444
445 #define SECTION_INDEX_VALID(ndx) (ndx > SHN_UNDEF && ndx < shnum)
446
447 switch (shdr[i].sh_type) {
448
449 case SHT_REL:
450 case SHT_RELA:
451 IF_DEBUG(linker,debugBelch( shdr[i].sh_type == SHT_REL ? "Rel " : "RelA "));
452
453 if (!SECTION_INDEX_VALID(shdr[i].sh_link)) {
454 if (shdr[i].sh_link == SHN_UNDEF)
455 errorBelch("\n%s: relocation section #%d has no symbol table\n"
456 "This object file has probably been fully striped. "
457 "Such files cannot be linked.\n",
458 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
459 else
460 errorBelch("\n%s: relocation section #%d has an invalid link field (%d)\n",
461 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
462 i, shdr[i].sh_link);
463 return 0;
464 }
465 if (shdr[shdr[i].sh_link].sh_type != SHT_SYMTAB) {
466 errorBelch("\n%s: relocation section #%d does not link to a symbol table\n",
467 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
468 return 0;
469 }
470 if (!SECTION_INDEX_VALID(shdr[i].sh_info)) {
471 errorBelch("\n%s: relocation section #%d has an invalid info field (%d)\n",
472 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
473 i, shdr[i].sh_info);
474 return 0;
475 }
476
477 break;
478 case SHT_SYMTAB:
479 IF_DEBUG(linker,debugBelch("Sym "));
480
481 if (!SECTION_INDEX_VALID(shdr[i].sh_link)) {
482 errorBelch("\n%s: symbol table section #%d has an invalid link field (%d)\n",
483 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName,
484 i, shdr[i].sh_link);
485 return 0;
486 }
487 if (shdr[shdr[i].sh_link].sh_type != SHT_STRTAB) {
488 errorBelch("\n%s: symbol table section #%d does not link to a string table\n",
489 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName, i);
490
491 return 0;
492 }
493 break;
494 case SHT_STRTAB: IF_DEBUG(linker,debugBelch("Str ")); break;
495 default: IF_DEBUG(linker,debugBelch(" ")); break;
496 }
497 if (sh_strtab) {
498 IF_DEBUG(linker,debugBelch("sname=%s\n", sh_strtab + shdr[i].sh_name ));
499 }
500 }
501
502 IF_DEBUG(linker,debugBelch( "\nString tables\n" ));
503 nstrtab = 0;
504 for (i = 0; i < shnum; i++) {
505 if (shdr[i].sh_type == SHT_STRTAB
506 /* Ignore the section header's string table. */
507 && i != shstrndx
508 /* Ignore string tables named .stabstr, as they contain
509 debugging info. */
510 && 0 != memcmp(".stabstr", sh_strtab + shdr[i].sh_name, 8)
511 ) {
512 IF_DEBUG(linker,debugBelch(" section %d is a normal string table\n", i ));
513 nstrtab++;
514 }
515 }
516 if (nstrtab == 0) {
517 IF_DEBUG(linker,debugBelch(" no normal string tables (potentially, but not necessarily a problem)\n"));
518 }
519 #if defined(SHN_XINDEX)
520 Elf_Word* shndxTable = get_shndx_table(ehdr);
521 #endif
522 nsymtabs = 0;
523 IF_DEBUG(linker,debugBelch( "Symbol tables\n" ));
524 for (i = 0; i < shnum; i++) {
525 if (shdr[i].sh_type != SHT_SYMTAB) continue;
526 IF_DEBUG(linker,debugBelch( "section %d is a symbol table\n", i ));
527 nsymtabs++;
528 stab = (Elf_Sym*) (ehdrC + shdr[i].sh_offset);
529 nent = shdr[i].sh_size / sizeof(Elf_Sym);
530 IF_DEBUG(linker,debugBelch( " number of entries is apparently %d (%ld rem)\n",
531 nent,
532 (long)shdr[i].sh_size % sizeof(Elf_Sym)
533 ));
534 if (0 != shdr[i].sh_size % sizeof(Elf_Sym)) {
535 errorBelch("%s: non-integral number of symbol table entries", oc->fileName);
536 return 0;
537 }
538 for (j = 0; j < nent; j++) {
539 Elf_Word secno = stab[j].st_shndx;
540 #if defined(SHN_XINDEX)
541 /* See Note [Many ELF Sections] */
542 if (secno == SHN_XINDEX) {
543 ASSERT(shndxTable);
544 secno = shndxTable[j];
545 }
546 #endif
547 IF_DEBUG(linker,debugBelch(" %2d ", j ));
548 IF_DEBUG(linker,debugBelch(" sec=%-5d size=%-3d val=%5p ",
549 (int)secno,
550 (int)stab[j].st_size,
551 (char*)stab[j].st_value ));
552
553 IF_DEBUG(linker,debugBelch("type=" ));
554 switch (ELF_ST_TYPE(stab[j].st_info)) {
555 case STT_NOTYPE: IF_DEBUG(linker,debugBelch("notype " )); break;
556 case STT_OBJECT: IF_DEBUG(linker,debugBelch("object " )); break;
557 case STT_FUNC : IF_DEBUG(linker,debugBelch("func " )); break;
558 case STT_SECTION: IF_DEBUG(linker,debugBelch("section" )); break;
559 case STT_FILE: IF_DEBUG(linker,debugBelch("file " )); break;
560 default: IF_DEBUG(linker,debugBelch("? " )); break;
561 }
562 IF_DEBUG(linker,debugBelch(" " ));
563
564 IF_DEBUG(linker,debugBelch("bind=" ));
565 switch (ELF_ST_BIND(stab[j].st_info)) {
566 case STB_LOCAL : IF_DEBUG(linker,debugBelch("local " )); break;
567 case STB_GLOBAL: IF_DEBUG(linker,debugBelch("global" )); break;
568 case STB_WEAK : IF_DEBUG(linker,debugBelch("weak " )); break;
569 default: IF_DEBUG(linker,debugBelch("? " )); break;
570 }
571 IF_DEBUG(linker,debugBelch(" " ));
572
573 IF_DEBUG(linker,debugBelch("other=%2x ", stab[j].st_other ));
574 IF_DEBUG(linker,debugBelch("name=%s [%x]\n",
575 ehdrC + shdr[shdr[i].sh_link].sh_offset
576 + stab[j].st_name, stab[j].st_name ));
577 }
578 }
579
580 if (nsymtabs == 0) {
581 // Not having a symbol table is not in principle a problem.
582 // When an object file has no symbols then the 'strip' program
583 // typically will remove the symbol table entirely.
584 IF_DEBUG(linker,debugBelch(" no symbol tables (potentially, but not necessarily a problem)\n"));
585 }
586
587 return 1;
588 }
589
590 /* Figure out what kind of section it is. Logic derived from
591 Figure 1.14 ("Special Sections") of the ELF document
592 ("Portable Formats Specification, Version 1.1"). */
593 static int getSectionKind_ELF( Elf_Shdr *hdr, int *is_bss )
594 {
595 *is_bss = false;
596
597 if (hdr->sh_type == SHT_PROGBITS
598 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_EXECINSTR)) {
599 /* .text-style section */
600 return SECTIONKIND_CODE_OR_RODATA;
601 }
602
603 if (hdr->sh_type == SHT_PROGBITS
604 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
605 /* .data-style section */
606 return SECTIONKIND_RWDATA;
607 }
608
609 if (hdr->sh_type == SHT_PROGBITS
610 && (hdr->sh_flags & SHF_ALLOC) && !(hdr->sh_flags & SHF_WRITE)) {
611 /* .rodata-style section */
612 return SECTIONKIND_CODE_OR_RODATA;
613 }
614 #ifndef openbsd_HOST_OS
615 if (hdr->sh_type == SHT_INIT_ARRAY
616 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
617 /* .init_array section */
618 return SECTIONKIND_INIT_ARRAY;
619 }
620 #endif /* not OpenBSD */
621 if (hdr->sh_type == SHT_NOBITS
622 && (hdr->sh_flags & SHF_ALLOC) && (hdr->sh_flags & SHF_WRITE)) {
623 /* .bss-style section */
624 *is_bss = true;
625 return SECTIONKIND_RWDATA;
626 }
627
628 return SECTIONKIND_OTHER;
629 }
630
631 static void *
632 mapObjectFileSection (int fd, Elf_Word offset, Elf_Word size,
633 void **mapped_start, StgWord *mapped_size,
634 StgWord *mapped_offset)
635 {
636 void *p;
637 size_t pageOffset, pageSize;
638
639 pageOffset = roundDownToPage(offset);
640 pageSize = roundUpToPage(offset-pageOffset+size);
641 p = mmapForLinker(pageSize, 0, fd, pageOffset);
642 if (p == NULL) return NULL;
643 *mapped_size = pageSize;
644 *mapped_offset = pageOffset;
645 *mapped_start = p;
646 return (void*)((StgWord)p + offset - pageOffset);
647 }
648
649 int
650 ocGetNames_ELF ( ObjectCode* oc )
651 {
652 Elf_Word i;
653 int j, nent, result, fd = -1;
654 Elf_Sym* stab;
655
656 char* ehdrC = (char*)(oc->image);
657 Elf_Ehdr* ehdr = (Elf_Ehdr*)ehdrC;
658 char* strtab;
659 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
660 Section * sections;
661 #if defined(SHN_XINDEX)
662 Elf_Word* shndxTable = get_shndx_table(ehdr);
663 #endif
664 const Elf_Word shnum = elf_shnum(ehdr);
665
666 ASSERT(symhash != NULL);
667
668 sections = (Section*)stgCallocBytes(sizeof(Section), shnum,
669 "ocGetNames_ELF(sections)");
670 oc->sections = sections;
671 oc->n_sections = shnum;
672
673
674 if (oc->imageMapped) {
675 #if defined(openbsd_HOST_OS)
676 fd = open(oc->fileName, O_RDONLY, S_IRUSR);
677 #else
678 fd = open(oc->fileName, O_RDONLY);
679 #endif
680 if (fd == -1) {
681 errorBelch("loadObj: can't open %" PATH_FMT, oc->fileName);
682 return 0;
683 }
684 }
685
686 for (i = 0; i < shnum; i++) {
687 int is_bss = false;
688 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
689 SectionAlloc alloc = SECTION_NOMEM;
690 void *start = NULL, *mapped_start = NULL;
691 StgWord mapped_size = 0, mapped_offset = 0;
692 StgWord size = shdr[i].sh_size;
693 StgWord offset = shdr[i].sh_offset;
694
695 if (is_bss && size > 0) {
696 /* This is a non-empty .bss section. Allocate zeroed space for
697 it, and set its .sh_offset field such that
698 ehdrC + .sh_offset == addr_of_zeroed_space. */
699 alloc = SECTION_MALLOC;
700 start = stgCallocBytes(1, size, "ocGetNames_ELF(BSS)");
701 mapped_start = start;
702 /*
703 debugBelch("BSS section at 0x%x, size %d\n",
704 zspace, shdr[i].sh_size);
705 */
706 }
707
708 else if (kind != SECTIONKIND_OTHER && size > 0) {
709 if (USE_CONTIGUOUS_MMAP) {
710 // already mapped.
711 start = oc->image + offset;
712 alloc = SECTION_NOMEM;
713 }
714 // use the m32 allocator if either the image is not mapped
715 // (i.e. we cannot map the secions separately), or if the section
716 // size is small.
717 else if (!oc->imageMapped || size < getPageSize() / 3) {
718 start = m32_alloc(size, 8);
719 if (start == NULL) goto fail;
720 memcpy(start, oc->image + offset, size);
721 alloc = SECTION_M32;
722 } else {
723 start = mapObjectFileSection(fd, offset, size,
724 &mapped_start, &mapped_size,
725 &mapped_offset);
726 if (start == NULL) goto fail;
727 alloc = SECTION_MMAP;
728 }
729 addProddableBlock(oc, start, size);
730 }
731
732 addSection(&sections[i], kind, alloc, start, size,
733 mapped_offset, mapped_start, mapped_size);
734
735 if (shdr[i].sh_type != SHT_SYMTAB) continue;
736
737 /* copy stuff into this module's object symbol table */
738 stab = (Elf_Sym*) (ehdrC + offset);
739 strtab = ehdrC + shdr[shdr[i].sh_link].sh_offset;
740 nent = shdr[i].sh_size / sizeof(Elf_Sym);
741
742 oc->n_symbols = nent;
743 oc->symbols = stgCallocBytes(oc->n_symbols, sizeof(SymbolName*),
744 "ocGetNames_ELF(oc->symbols)");
745 // Note calloc: if we fail partway through initializing symbols, we need
746 // to undo the additions to the symbol table so far. We know which ones
747 // have been added by whether the entry is NULL or not.
748
749 //TODO: we ignore local symbols anyway right? So we can use the
750 // shdr[i].sh_info to get the index of the first non-local symbol
751 // ie we should use j = shdr[i].sh_info
752 for (j = 0; j < nent; j++) {
753
754 char isLocal = false; /* avoids uninit-var warning */
755 HsBool isWeak = HS_BOOL_FALSE;
756 SymbolAddr* ad = NULL;
757 SymbolName* nm = strtab + stab[j].st_name;
758 unsigned short shndx = stab[j].st_shndx;
759 Elf_Word secno;
760
761 /* See Note [Many ELF Sections] */
762 /* Note that future checks for special SHN_* numbers should check the
763 * shndx variable, not the section number in secno. Sections with the
764 * real number in the SHN_LORESERVE..HIRESERVE range will have shndx
765 * SHN_XINDEX and a secno with one of the reserved values. */
766 secno = shndx;
767 #if defined(SHN_XINDEX)
768 if (shndx == SHN_XINDEX) {
769 ASSERT(shndxTable);
770 secno = shndxTable[j];
771 }
772 #endif
773 /* Figure out if we want to add it; if so, set ad to its
774 address. Otherwise leave ad == NULL. */
775
776 if (shndx == SHN_COMMON) {
777 isLocal = false;
778 ad = stgCallocBytes(1, stab[j].st_size, "ocGetNames_ELF(COMMON)");
779 /*
780 debugBelch("COMMON symbol, size %d name %s\n",
781 stab[j].st_size, nm);
782 */
783 /* Pointless to do addProddableBlock() for this area,
784 since the linker should never poke around in it. */
785 }
786 else
787 if ( ( ELF_ST_BIND(stab[j].st_info)==STB_GLOBAL
788 || ELF_ST_BIND(stab[j].st_info)==STB_LOCAL
789 || ELF_ST_BIND(stab[j].st_info)==STB_WEAK
790 )
791 /* and not an undefined symbol */
792 && shndx != SHN_UNDEF
793 /* and not in a "special section" */
794 && (shndx < SHN_LORESERVE
795 #if defined(SHN_XINDEX)
796 || shndx == SHN_XINDEX
797 #endif
798 )
799 &&
800 /* and it's a not a section or string table or anything silly */
801 ( ELF_ST_TYPE(stab[j].st_info)==STT_FUNC ||
802 ELF_ST_TYPE(stab[j].st_info)==STT_OBJECT ||
803 ELF_ST_TYPE(stab[j].st_info)==STT_NOTYPE
804 )
805 ) {
806 /* Section 0 is the undefined section, hence > and not >=. */
807 ASSERT(secno > 0 && secno < shnum);
808 /*
809 if (shdr[secno].sh_type == SHT_NOBITS) {
810 debugBelch(" BSS symbol, size %d off %d name %s\n",
811 stab[j].st_size, stab[j].st_value, nm);
812 }
813 */
814 ad = (SymbolAddr*)((intptr_t)sections[secno].start +
815 (intptr_t)stab[j].st_value);
816 if (ELF_ST_BIND(stab[j].st_info)==STB_LOCAL) {
817 isLocal = true;
818 isWeak = false;
819 } else { /* STB_GLOBAL or STB_WEAK */
820 #ifdef ELF_FUNCTION_DESC
821 /* dlsym() and the initialisation table both give us function
822 * descriptors, so to be consistent we store function descriptors
823 * in the symbol table */
824 if (ELF_ST_TYPE(stab[j].st_info) == STT_FUNC)
825 ad = (SymbolAddr*)allocateFunctionDesc((Elf_Addr)ad);
826 #endif
827 IF_DEBUG(linker,debugBelch( "addOTabName(GLOB): %10p %s %s\n",
828 ad, oc->fileName, nm ));
829 isLocal = false;
830 isWeak = (ELF_ST_BIND(stab[j].st_info)==STB_WEAK);
831 }
832 }
833
834 /* And the decision is ... */
835
836 oc->symbols[j] = nm;
837
838 if (ad != NULL) {
839 ASSERT(nm != NULL);
840 /* Acquire! */
841 if (isLocal) {
842 /* Ignore entirely. */
843 oc->symbols[j] = NULL;
844 } else {
845
846 if (isWeak == HS_BOOL_TRUE) {
847 setWeakSymbol(oc, nm);
848 }
849
850 if (! ghciInsertSymbolTable(oc->fileName, symhash,
851 nm, ad, isWeak, oc)) {
852 goto fail;
853 }
854 }
855 } else {
856 /* Skip. */
857 IF_DEBUG(linker,debugBelch( "skipping `%s'\n",
858 nm ));
859
860 /* We're skipping the symbol, but if we ever load this
861 object file we'll want to skip it then too. */
862 oc->symbols[j] = NULL;
863
864 /*
865 debugBelch(
866 "skipping bind = %d, type = %d, secno = %d `%s'\n",
867 (int)ELF_ST_BIND(stab[j].st_info),
868 (int)ELF_ST_TYPE(stab[j].st_info),
869 (int)secno,
870 nm
871 );
872 */
873 }
874
875 }
876 }
877
878 result = 1;
879 goto end;
880
881 fail:
882 result = 0;
883 goto end;
884
885 end:
886 if (fd >= 0) close(fd);
887 return result;
888 }
889
890 #ifdef arm_HOST_ARCH
891 // TODO: These likely belong in a library somewhere
892
893 // Signed extend a number to a 32-bit int.
894 static inline StgInt32 sign_extend32(uint32_t bits, StgWord32 x) {
895 return ((StgInt32) (x << (32 - bits))) >> (32 - bits);
896 }
897
898 // Does the given signed integer fit into the given bit width?
899 static inline StgBool is_int(uint32_t bits, StgInt32 x) {
900 return bits > 32 || (-(1 << (bits-1)) <= x
901 && x < (1 << (bits-1)));
902 }
903 #endif
904
905 /* Do ELF relocations which lack an explicit addend. All x86-linux
906 and arm-linux relocations appear to be of this form. */
907 static int
908 do_Elf_Rel_relocations ( ObjectCode* oc, char* ehdrC,
909 Elf_Shdr* shdr, int shnum )
910 {
911 int j;
912 SymbolName* symbol;
913 Elf_Word* targ;
914 Elf_Rel* rtab = (Elf_Rel*) (ehdrC + shdr[shnum].sh_offset);
915 Elf_Sym* stab;
916 char* strtab;
917 int nent = shdr[shnum].sh_size / sizeof(Elf_Rel);
918 int target_shndx = shdr[shnum].sh_info;
919 int symtab_shndx = shdr[shnum].sh_link;
920 int strtab_shndx = shdr[symtab_shndx].sh_link;
921 #if defined(SHN_XINDEX)
922 Elf_Word* shndx_table = get_shndx_table((Elf_Ehdr*)ehdrC);
923 #endif
924
925 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
926 strtab= (char*) (ehdrC + shdr[ strtab_shndx ].sh_offset);
927 targ = (Elf_Word*)oc->sections[target_shndx].start;
928 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d and strtab %d\n",
929 target_shndx, symtab_shndx, strtab_shndx ));
930
931 /* Skip sections that we're not interested in. */
932 if (oc->sections[target_shndx].kind == SECTIONKIND_OTHER) {
933 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
934 return 1;
935 }
936
937 for (j = 0; j < nent; j++) {
938 Elf_Addr offset = rtab[j].r_offset;
939 Elf_Addr info = rtab[j].r_info;
940
941 Elf_Addr P = ((Elf_Addr)targ) + offset;
942 Elf_Word* pP = (Elf_Word*)P;
943 #if defined(i386_HOST_ARCH) || defined(DEBUG)
944 Elf_Addr A = *pP;
945 #endif
946 Elf_Addr S;
947 void* S_tmp;
948 #ifdef i386_HOST_ARCH
949 Elf_Addr value;
950 #endif
951 #ifdef arm_HOST_ARCH
952 int is_target_thm=0, T=0;
953 #endif
954
955 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p): ",
956 j, (void*)offset, (void*)info ));
957 if (!info) {
958 IF_DEBUG(linker,debugBelch( " ZERO" ));
959 S = 0;
960 } else {
961 Elf_Sym sym = stab[ELF_R_SYM(info)];
962 /* First see if it is a local symbol. */
963 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
964 /* Yes, so we can get the address directly from the ELF symbol
965 table. */
966 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
967 /* See Note [Many ELF Sections] */
968 Elf_Word secno = sym.st_shndx;
969 #if defined(SHN_XINDEX)
970 if (secno == SHN_XINDEX) {
971 ASSERT(shndx_table);
972 secno = shndx_table[ELF_R_SYM(info)];
973 }
974 #endif
975 S = (Elf_Addr)oc->sections[ secno ].start +
976 stab[ELF_R_SYM(info)].st_value;
977 } else {
978 symbol = strtab + sym.st_name;
979 S_tmp = lookupSymbol_( symbol );
980 S = (Elf_Addr)S_tmp;
981 }
982 if (!S) {
983 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
984 return 0;
985 }
986 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
987
988 #ifdef arm_HOST_ARCH
989 // Thumb instructions have bit 0 of symbol's st_value set
990 is_target_thm = S & 0x1;
991
992 T = sym.st_info & STT_FUNC && is_target_thm;
993
994 // Make sure we clear bit 0. Strictly speaking we should have done
995 // this to st_value above but I believe alignment requirements should
996 // ensure that no instructions start on an odd address
997 S &= ~1;
998 #endif
999 }
1000
1001 int reloc_type = ELF_R_TYPE(info);
1002 IF_DEBUG(linker,debugBelch( "Reloc: P = %p S = %p A = %p type=%d\n",
1003 (void*)P, (void*)S, (void*)A, reloc_type ));
1004 checkProddableBlock ( oc, pP, sizeof(Elf_Word) );
1005
1006 #ifdef i386_HOST_ARCH
1007 value = S + A;
1008 #endif
1009
1010 switch (reloc_type) {
1011 # ifdef i386_HOST_ARCH
1012 case R_386_32: *pP = value; break;
1013 case R_386_PC32: *pP = value - P; break;
1014 # endif
1015
1016 # ifdef arm_HOST_ARCH
1017 case R_ARM_ABS32:
1018 case R_ARM_TARGET1: // Specified by Linux ARM ABI to be equivalent to ABS32
1019 *(Elf32_Word *)P += S;
1020 *(Elf32_Word *)P |= T;
1021 break;
1022
1023 case R_ARM_REL32:
1024 *(Elf32_Word *)P += S;
1025 *(Elf32_Word *)P |= T;
1026 *(Elf32_Word *)P -= P;
1027 break;
1028
1029 case R_ARM_CALL:
1030 case R_ARM_JUMP24:
1031 {
1032 // N.B. LLVM's LLD linker's relocation implement is a fantastic
1033 // resource
1034 StgWord32 *word = (StgWord32 *)P;
1035 StgInt32 imm = (*word & ((1<<24)-1)) << 2;
1036
1037 const StgBool is_blx = (*word & 0xf0000000) == 0xf0000000;
1038 const StgWord32 hBit = is_blx ? ((*word >> 24) & 1) : 0;
1039 imm |= hBit << 1;
1040
1041 // Sign extend to 32 bits
1042 // I would have thought this would be 24 bits but LLD uses 26 here.
1043 // Hmm.
1044 imm = sign_extend32(26, imm);
1045
1046 StgWord32 result = ((S + imm) | T) - P;
1047
1048 const StgBool overflow = !is_int(26, (StgInt32) result);
1049
1050 // Handle overflow and Thumb interworking
1051 const StgBool needs_veneer = (is_target_thm && ELF_R_TYPE(info) == R_ARM_JUMP24) || overflow;
1052 if (needs_veneer) {
1053 // Generate veneer
1054 // The +8 below is to undo the PC-bias compensation done by the object producer
1055 SymbolExtra *extra = makeArmSymbolExtra(oc, ELF_R_SYM(info), S+imm+8, 0, is_target_thm);
1056 // The -8 below is to compensate for PC bias
1057 result = (StgWord32) ((StgInt32) extra->jumpIsland - P - 8);
1058 result &= ~1; // Clear thumb indicator bit
1059 if (!is_int(26, (StgInt32) result)) {
1060 errorBelch("Unable to fixup overflow'd R_ARM_CALL: jump island=%p, reloc=%p\n",
1061 (void*) extra->jumpIsland, (void*) P);
1062 return 0;
1063 }
1064 }
1065
1066 // Update the branch target
1067 const StgWord32 imm24 = (result & 0x03fffffc) >> 2;
1068 *word = (*word & ~0x00ffffff)
1069 | (imm24 & 0x00ffffff);
1070
1071 // Change the relocated branch into a BLX if necessary
1072 const StgBool switch_mode = is_target_thm && (reloc_type == R_ARM_CALL);
1073 if (!needs_veneer && switch_mode) {
1074 const StgWord32 hBit = (result & 0x2) >> 1;
1075 // Change instruction to BLX
1076 *word = (*word & ~0xFF000000) | ((0xfa | hBit) << 24);
1077 IF_DEBUG(linker, debugBelch("Changed BL to BLX at %p\n", word));
1078 }
1079 break;
1080 }
1081
1082 case R_ARM_MOVT_ABS:
1083 case R_ARM_MOVW_ABS_NC:
1084 {
1085 StgWord32 *word = (StgWord32 *)P;
1086 StgWord32 imm12 = *word & 0xfff;
1087 StgWord32 imm4 = (*word >> 16) & 0xf;
1088 StgInt32 offset = imm4 << 12 | imm12;
1089 StgWord32 result = (S + offset) | T;
1090
1091 if (reloc_type == R_ARM_MOVT_ABS)
1092 result = (result & 0xffff0000) >> 16;
1093
1094 StgWord32 result12 = result & 0xfff;
1095 StgWord32 result4 = (result >> 12) & 0xf;
1096 *word = (*word & ~0xf0fff) | (result4 << 16) | result12;
1097 break;
1098 }
1099
1100 case R_ARM_THM_CALL:
1101 case R_ARM_THM_JUMP24:
1102 {
1103 StgWord16 *upper = (StgWord16 *)P;
1104 StgWord16 *lower = (StgWord16 *)(P + 2);
1105
1106 int overflow;
1107 int to_thm = (*lower >> 12) & 1;
1108 int sign = (*upper >> 10) & 1;
1109 int j1, j2, i1, i2;
1110
1111 // Decode immediate value
1112 j1 = (*lower >> 13) & 1; i1 = ~(j1 ^ sign) & 1;
1113 j2 = (*lower >> 11) & 1; i2 = ~(j2 ^ sign) & 1;
1114 StgInt32 imm = (sign << 24)
1115 | (i1 << 23)
1116 | (i2 << 22)
1117 | ((*upper & 0x03ff) << 12)
1118 | ((*lower & 0x07ff) << 1);
1119
1120 // Sign extend 25 to 32 bits
1121 if (imm & 0x01000000)
1122 imm -= 0x02000000;
1123
1124 offset = ((imm + S) | T) - P;
1125 overflow = offset <= (StgWord32)0xff000000 || offset >= (StgWord32)0x01000000;
1126
1127 if ((!is_target_thm && ELF_R_TYPE(info) == R_ARM_THM_JUMP24) || overflow) {
1128 // Generate veneer
1129 SymbolExtra *extra = makeArmSymbolExtra(oc, ELF_R_SYM(info), S+imm+4, 1, is_target_thm);
1130 offset = (StgWord32) &extra->jumpIsland - P - 4;
1131 sign = offset >> 31;
1132 to_thm = 1;
1133 } else if (!is_target_thm && ELF_R_TYPE(info) == R_ARM_THM_CALL) {
1134 offset &= ~0x3;
1135 to_thm = 0;
1136 }
1137
1138 // Reencode instruction
1139 i1 = ~(offset >> 23) & 1; j1 = sign ^ i1;
1140 i2 = ~(offset >> 22) & 1; j2 = sign ^ i2;
1141 *upper = ( (*upper & 0xf800)
1142 | (sign << 10)
1143 | ((offset >> 12) & 0x03ff) );
1144 *lower = ( (*lower & 0xd000)
1145 | (j1 << 13)
1146 | (to_thm << 12)
1147 | (j2 << 11)
1148 | ((offset >> 1) & 0x07ff) );
1149 break;
1150 }
1151
1152 case R_ARM_THM_MOVT_ABS:
1153 case R_ARM_THM_MOVW_ABS_NC:
1154 {
1155 StgWord16 *upper = (StgWord16 *)P;
1156 StgWord16 *lower = (StgWord16 *)(P + 2);
1157 StgInt32 offset = ((*upper & 0x000f) << 12)
1158 | ((*upper & 0x0400) << 1)
1159 | ((*lower & 0x7000) >> 4)
1160 | (*lower & 0x00ff);
1161
1162 offset = (offset ^ 0x8000) - 0x8000; // Sign extend
1163 offset += S;
1164 if (ELF_R_TYPE(info) == R_ARM_THM_MOVW_ABS_NC)
1165 offset |= T;
1166 else if (ELF_R_TYPE(info) == R_ARM_THM_MOVT_ABS)
1167 offset >>= 16;
1168
1169 *upper = ( (*upper & 0xfbf0)
1170 | ((offset & 0xf000) >> 12)
1171 | ((offset & 0x0800) >> 1) );
1172 *lower = ( (*lower & 0x8f00)
1173 | ((offset & 0x0700) << 4)
1174 | (offset & 0x00ff) );
1175 break;
1176 }
1177
1178 case R_ARM_THM_JUMP8:
1179 {
1180 StgWord16 *word = (StgWord16 *)P;
1181 StgWord offset = *word & 0x01fe;
1182 offset += S - P;
1183 if (!is_target_thm) {
1184 errorBelch("%s: Thumb to ARM transition with JUMP8 relocation not supported\n",
1185 oc->fileName);
1186 return 0;
1187 }
1188
1189 *word = (*word & ~0x01fe)
1190 | (offset & 0x01fe);
1191 break;
1192 }
1193
1194 case R_ARM_THM_JUMP11:
1195 {
1196 StgWord16 *word = (StgWord16 *)P;
1197 StgWord offset = *word & 0x0ffe;
1198 offset += S - P;
1199 if (!is_target_thm) {
1200 errorBelch("%s: Thumb to ARM transition with JUMP11 relocation not supported\n",
1201 oc->fileName);
1202 return 0;
1203 }
1204
1205 *word = (*word & ~0x0ffe)
1206 | (offset & 0x0ffe);
1207 break;
1208 }
1209
1210 # endif // arm_HOST_ARCH
1211
1212 default:
1213 errorBelch("%s: unhandled ELF relocation(Rel) type %" FMT_Word "\n",
1214 oc->fileName, (W_)ELF_R_TYPE(info));
1215 return 0;
1216 }
1217
1218 }
1219 return 1;
1220 }
1221
1222 /* Do ELF relocations for which explicit addends are supplied.
1223 sparc-solaris relocations appear to be of this form. */
1224 static int
1225 do_Elf_Rela_relocations ( ObjectCode* oc, char* ehdrC,
1226 Elf_Shdr* shdr, int shnum )
1227 {
1228 int j;
1229 SymbolName* symbol = NULL;
1230 Elf_Rela* rtab = (Elf_Rela*) (ehdrC + shdr[shnum].sh_offset);
1231 Elf_Sym* stab;
1232 char* strtab;
1233 int nent = shdr[shnum].sh_size / sizeof(Elf_Rela);
1234 int symtab_shndx = shdr[shnum].sh_link;
1235 int strtab_shndx = shdr[symtab_shndx].sh_link;
1236 int target_shndx = shdr[shnum].sh_info;
1237 #if defined(SHN_XINDEX)
1238 Elf_Word* shndx_table = get_shndx_table((Elf_Ehdr*)ehdrC);
1239 #endif
1240 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1241 /* This #ifdef only serves to avoid unused-var warnings. */
1242 Elf_Addr targ = (Elf_Addr) oc->sections[target_shndx].start;
1243 #endif
1244
1245 stab = (Elf_Sym*) (ehdrC + shdr[ symtab_shndx ].sh_offset);
1246 strtab= (char*) (ehdrC + shdr[ strtab_shndx ].sh_offset);
1247
1248 IF_DEBUG(linker,debugBelch( "relocations for section %d using symtab %d\n",
1249 target_shndx, symtab_shndx ));
1250
1251 /* Skip sections that we're not interested in. */
1252 if (oc->sections[target_shndx].kind == SECTIONKIND_OTHER) {
1253 IF_DEBUG(linker,debugBelch( "skipping (target section not loaded)"));
1254 return 1;
1255 }
1256
1257 for (j = 0; j < nent; j++) {
1258 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1259 /* This #ifdef only serves to avoid unused-var warnings. */
1260 Elf_Addr offset = rtab[j].r_offset;
1261 Elf_Addr P = targ + offset;
1262 Elf_Addr A = rtab[j].r_addend;
1263 #endif
1264 #if defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1265 Elf_Addr value;
1266 #endif
1267 Elf_Addr info = rtab[j].r_info;
1268 Elf_Addr S;
1269 void* S_tmp;
1270 # if defined(sparc_HOST_ARCH)
1271 Elf_Word* pP = (Elf_Word*)P;
1272 Elf_Word w1, w2;
1273 # elif defined(powerpc_HOST_ARCH)
1274 Elf_Sword delta;
1275 # endif
1276
1277 IF_DEBUG(linker,debugBelch( "Rel entry %3d is raw(%6p %6p %6p) ",
1278 j, (void*)offset, (void*)info,
1279 (void*)A ));
1280 if (!info) {
1281 IF_DEBUG(linker,debugBelch( " ZERO" ));
1282 S = 0;
1283 } else {
1284 Elf_Sym sym = stab[ELF_R_SYM(info)];
1285 /* First see if it is a local symbol. */
1286 if (ELF_ST_BIND(sym.st_info) == STB_LOCAL) {
1287 /* Yes, so we can get the address directly from the ELF symbol
1288 table. */
1289 symbol = sym.st_name==0 ? "(noname)" : strtab+sym.st_name;
1290 /* See Note [Many ELF Sections] */
1291 Elf_Word secno = sym.st_shndx;
1292 #if defined(SHN_XINDEX)
1293 if (secno == SHN_XINDEX) {
1294 secno = shndx_table[ELF_R_SYM(info)];
1295 }
1296 #endif
1297 S = (Elf_Addr)oc->sections[secno].start
1298 + stab[ELF_R_SYM(info)].st_value;
1299 #ifdef ELF_FUNCTION_DESC
1300 /* Make a function descriptor for this function */
1301 if (S && ELF_ST_TYPE(sym.st_info) == STT_FUNC) {
1302 S = allocateFunctionDesc(S + A);
1303 A = 0;
1304 }
1305 #endif
1306 } else {
1307 /* No, so look up the name in our global table. */
1308 symbol = strtab + sym.st_name;
1309 S_tmp = lookupSymbol_( symbol );
1310 S = (Elf_Addr)S_tmp;
1311
1312 #ifdef ELF_FUNCTION_DESC
1313 /* If a function, already a function descriptor - we would
1314 have to copy it to add an offset. */
1315 if (S && (ELF_ST_TYPE(sym.st_info) == STT_FUNC) && (A != 0))
1316 errorBelch("%s: function %s with addend %p", oc->fileName, symbol, (void *)A);
1317 #endif
1318 }
1319 if (!S) {
1320 errorBelch("%s: unknown symbol `%s'", oc->fileName, symbol);
1321 return 0;
1322 }
1323 IF_DEBUG(linker,debugBelch( "`%s' resolves to %p\n", symbol, (void*)S ));
1324 }
1325
1326 #if defined(DEBUG) || defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) \
1327 || defined(x86_64_HOST_ARCH)
1328 IF_DEBUG(linker,debugBelch("Reloc: P = %p S = %p A = %p\n",
1329 (void*)P, (void*)S, (void*)A ));
1330 checkProddableBlock(oc, (void*)P, sizeof(Elf_Word));
1331 #endif
1332
1333 #if defined(sparc_HOST_ARCH) || defined(powerpc_HOST_ARCH) || defined(x86_64_HOST_ARCH)
1334 value = S + A;
1335 #endif
1336
1337 switch (ELF_R_TYPE(info)) {
1338 # if defined(sparc_HOST_ARCH)
1339 case R_SPARC_WDISP30:
1340 w1 = *pP & 0xC0000000;
1341 w2 = (Elf_Word)((value - P) >> 2);
1342 ASSERT((w2 & 0xC0000000) == 0);
1343 w1 |= w2;
1344 *pP = w1;
1345 break;
1346 case R_SPARC_HI22:
1347 w1 = *pP & 0xFFC00000;
1348 w2 = (Elf_Word)(value >> 10);
1349 ASSERT((w2 & 0xFFC00000) == 0);
1350 w1 |= w2;
1351 *pP = w1;
1352 break;
1353 case R_SPARC_LO10:
1354 w1 = *pP & ~0x3FF;
1355 w2 = (Elf_Word)(value & 0x3FF);
1356 ASSERT((w2 & ~0x3FF) == 0);
1357 w1 |= w2;
1358 *pP = w1;
1359 break;
1360
1361 /* According to the Sun documentation:
1362 R_SPARC_UA32
1363 This relocation type resembles R_SPARC_32, except it refers to an
1364 unaligned word. That is, the word to be relocated must be treated
1365 as four separate bytes with arbitrary alignment, not as a word
1366 aligned according to the architecture requirements.
1367 */
1368 case R_SPARC_UA32:
1369 w2 = (Elf_Word)value;
1370
1371 // SPARC doesn't do misaligned writes of 32 bit words,
1372 // so we have to do this one byte-at-a-time.
1373 char *pPc = (char*)pP;
1374 pPc[0] = (char) ((Elf_Word)(w2 & 0xff000000) >> 24);
1375 pPc[1] = (char) ((Elf_Word)(w2 & 0x00ff0000) >> 16);
1376 pPc[2] = (char) ((Elf_Word)(w2 & 0x0000ff00) >> 8);
1377 pPc[3] = (char) ((Elf_Word)(w2 & 0x000000ff));
1378 break;
1379
1380 case R_SPARC_32:
1381 w2 = (Elf_Word)value;
1382 *pP = w2;
1383 break;
1384 # elif defined(powerpc_HOST_ARCH)
1385 case R_PPC_ADDR16_LO:
1386 *(Elf32_Half*) P = value;
1387 break;
1388
1389 case R_PPC_ADDR16_HI:
1390 *(Elf32_Half*) P = value >> 16;
1391 break;
1392
1393 case R_PPC_ADDR16_HA:
1394 *(Elf32_Half*) P = (value + 0x8000) >> 16;
1395 break;
1396
1397 case R_PPC_ADDR32:
1398 *(Elf32_Word *) P = value;
1399 break;
1400
1401 case R_PPC_REL32:
1402 *(Elf32_Word *) P = value - P;
1403 break;
1404
1405 case R_PPC_PLTREL24:
1406 value -= 0x8000; /* See Note [.LCTOC1 in PPC PIC code] */
1407 /* fallthrough */
1408 case R_PPC_REL24:
1409 delta = value - P;
1410
1411 if( delta << 6 >> 6 != delta )
1412 {
1413 value = (Elf_Addr) (&makeSymbolExtra( oc, ELF_R_SYM(info), value )
1414 ->jumpIsland);
1415 delta = value - P;
1416
1417 if( value == 0 || delta << 6 >> 6 != delta )
1418 {
1419 barf( "Unable to make SymbolExtra for #%d",
1420 ELF_R_SYM(info) );
1421 return 0;
1422 }
1423 }
1424
1425 *(Elf_Word *) P = (*(Elf_Word *) P & 0xfc000003)
1426 | (delta & 0x3fffffc);
1427 break;
1428
1429 case R_PPC_REL16_LO:
1430 *(Elf32_Half*) P = value - P;
1431 break;
1432
1433 case R_PPC_REL16_HI:
1434 *(Elf32_Half*) P = (value - P) >> 16;
1435 break;
1436
1437 case R_PPC_REL16_HA:
1438 *(Elf32_Half*) P = (value + 0x8000 - P) >> 16;
1439 break;
1440 # endif
1441
1442 #if x86_64_HOST_ARCH
1443 case R_X86_64_64:
1444 *(Elf64_Xword *)P = value;
1445 break;
1446
1447 case R_X86_64_PC32:
1448 {
1449 #if defined(ALWAYS_PIC)
1450 barf("R_X86_64_PC32 relocation, but ALWAYS_PIC.");
1451 #else
1452 StgInt64 off = value - P;
1453 if (off >= 0x7fffffffL || off < -0x80000000L) {
1454 if (X86_64_ELF_NONPIC_HACK) {
1455 StgInt64 pltAddress =
1456 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1457 -> jumpIsland;
1458 off = pltAddress + A - P;
1459 } else {
1460 errorBelch("R_X86_64_PC32 relocation out of range: %s = %"
1461 PRId64 "d\nRecompile %s with -fPIC.",
1462 symbol, off, oc->fileName );
1463 return 0;
1464 }
1465 }
1466 *(Elf64_Word *)P = (Elf64_Word)off;
1467 #endif
1468 break;
1469 }
1470
1471 case R_X86_64_PC64:
1472 {
1473 StgInt64 off = value - P;
1474 *(Elf64_Word *)P = (Elf64_Word)off;
1475 break;
1476 }
1477
1478 case R_X86_64_32:
1479 #if defined(ALWAYS_PIC)
1480 barf("R_X86_64_32 relocation, but ALWAYS_PIC.");
1481 #else
1482 if (value >= 0x7fffffffL) {
1483 if (X86_64_ELF_NONPIC_HACK) {
1484 StgInt64 pltAddress =
1485 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1486 -> jumpIsland;
1487 value = pltAddress + A;
1488 } else {
1489 errorBelch("R_X86_64_32 relocation out of range: %s = %"
1490 PRId64 "d\nRecompile %s with -fPIC.",
1491 symbol, value, oc->fileName );
1492 return 0;
1493 }
1494 }
1495 *(Elf64_Word *)P = (Elf64_Word)value;
1496 #endif
1497 break;
1498
1499 case R_X86_64_32S:
1500 #if defined(ALWAYS_PIC)
1501 barf("R_X86_64_32S relocation, but ALWAYS_PIC.");
1502 #else
1503 if ((StgInt64)value > 0x7fffffffL || (StgInt64)value < -0x80000000L) {
1504 if (X86_64_ELF_NONPIC_HACK) {
1505 StgInt64 pltAddress =
1506 (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1507 -> jumpIsland;
1508 value = pltAddress + A;
1509 } else {
1510 errorBelch("R_X86_64_32S relocation out of range: %s = %"
1511 PRId64 "d\nRecompile %s with -fPIC.",
1512 symbol, value, oc->fileName );
1513 return 0;
1514 }
1515 }
1516 *(Elf64_Sword *)P = (Elf64_Sword)value;
1517 #endif
1518 break;
1519 /* These two relocations were introduced in glibc 2.23 and binutils 2.26.
1520 But in order to use them the system which compiles the bindist for GHC needs
1521 to have glibc >= 2.23. So only use them if they're defined. */
1522 #if defined(R_X86_64_REX_GOTPCRELX) && defined(R_X86_64_GOTPCRELX)
1523 case R_X86_64_REX_GOTPCRELX:
1524 case R_X86_64_GOTPCRELX:
1525 #endif
1526 case R_X86_64_GOTPCREL:
1527 {
1528 StgInt64 gotAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)->addr;
1529 StgInt64 off = gotAddress + A - P;
1530 *(Elf64_Word *)P = (Elf64_Word)off;
1531 break;
1532 }
1533 #if defined(dragonfly_HOST_OS)
1534 case R_X86_64_GOTTPOFF:
1535 {
1536 #if defined(ALWAYS_PIC)
1537 barf("R_X86_64_GOTTPOFF relocation, but ALWAYS_PIC.");
1538 #else
1539 /* determine the offset of S to the current thread's tls
1540 area
1541 XXX: Move this to the beginning of function */
1542 struct tls_info ti;
1543 get_tls_area(0, &ti, sizeof(ti));
1544 /* make entry in GOT that contains said offset */
1545 StgInt64 gotEntry = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info),
1546 (S - (Elf64_Addr)(ti.base)))->addr;
1547 *(Elf64_Word *)P = gotEntry + A - P;
1548 #endif
1549 break;
1550 }
1551 #endif
1552
1553
1554
1555 case R_X86_64_PLT32:
1556 {
1557 #if defined(ALWAYS_PIC)
1558 barf("R_X86_64_PLT32 relocation, but ALWAYS_PIC.");
1559 #else
1560 StgInt64 off = value - P;
1561 if (off >= 0x7fffffffL || off < -0x80000000L) {
1562 StgInt64 pltAddress = (StgInt64) &makeSymbolExtra(oc, ELF_R_SYM(info), S)
1563 -> jumpIsland;
1564 off = pltAddress + A - P;
1565 }
1566 *(Elf64_Word *)P = (Elf64_Word)off;
1567 #endif
1568 break;
1569 }
1570 #endif
1571
1572 default:
1573 errorBelch("%s: unhandled ELF relocation(RelA) type %" FMT_Word "\n",
1574 oc->fileName, (W_)ELF_R_TYPE(info));
1575 return 0;
1576 }
1577
1578 }
1579 return 1;
1580 }
1581
1582 int
1583 ocResolve_ELF ( ObjectCode* oc )
1584 {
1585 int ok;
1586 Elf_Word i;
1587 char* ehdrC = (char*)(oc->image);
1588 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
1589 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
1590 const Elf_Word shnum = elf_shnum(ehdr);
1591
1592 /* Process the relocation sections. */
1593 for (i = 0; i < shnum; i++) {
1594 if (shdr[i].sh_type == SHT_REL) {
1595 ok = do_Elf_Rel_relocations ( oc, ehdrC, shdr, i );
1596 if (!ok) return ok;
1597 }
1598 else
1599 if (shdr[i].sh_type == SHT_RELA) {
1600 ok = do_Elf_Rela_relocations ( oc, ehdrC, shdr, i );
1601 if (!ok) return ok;
1602 }
1603 }
1604
1605 #if defined(powerpc_HOST_ARCH) || defined(arm_HOST_ARCH)
1606 ocFlushInstructionCache( oc );
1607 #endif
1608
1609 return 1;
1610 }
1611
1612 int ocRunInit_ELF( ObjectCode *oc )
1613 {
1614 Elf_Word i;
1615 char* ehdrC = (char*)(oc->image);
1616 Elf_Ehdr* ehdr = (Elf_Ehdr*) ehdrC;
1617 Elf_Shdr* shdr = (Elf_Shdr*) (ehdrC + ehdr->e_shoff);
1618 char* sh_strtab = ehdrC + shdr[elf_shstrndx(ehdr)].sh_offset;
1619 int argc, envc;
1620 char **argv, **envv;
1621
1622 getProgArgv(&argc, &argv);
1623 getProgEnvv(&envc, &envv);
1624
1625 // XXX Apparently in some archs .init may be something
1626 // special! See DL_DT_INIT_ADDRESS macro in glibc
1627 // as well as ELF_FUNCTION_PTR_IS_SPECIAL. We've not handled
1628 // it here, please file a bug report if it affects you.
1629 for (i = 0; i < elf_shnum(ehdr); i++) {
1630 init_t *init_start, *init_end, *init;
1631 int is_bss = false;
1632 SectionKind kind = getSectionKind_ELF(&shdr[i], &is_bss);
1633 if (kind == SECTIONKIND_CODE_OR_RODATA
1634 && 0 == memcmp(".init", sh_strtab + shdr[i].sh_name, 5)) {
1635 init_t init_f = (init_t)(oc->sections[i].start);
1636 init_f(argc, argv, envv);
1637 }
1638
1639 if (kind == SECTIONKIND_INIT_ARRAY) {
1640 char *init_startC = oc->sections[i].start;
1641 init_start = (init_t*)init_startC;
1642 init_end = (init_t*)(init_startC + shdr[i].sh_size);
1643 for (init = init_start; init < init_end; init++) {
1644 (*init)(argc, argv, envv);
1645 }
1646 }
1647
1648 // XXX could be more strict and assert that it's
1649 // SECTIONKIND_RWDATA; but allowing RODATA seems harmless enough.
1650 if ((kind == SECTIONKIND_RWDATA || kind == SECTIONKIND_CODE_OR_RODATA)
1651 && 0 == memcmp(".ctors", sh_strtab + shdr[i].sh_name, 6)) {
1652 char *init_startC = oc->sections[i].start;
1653 init_start = (init_t*)init_startC;
1654 init_end = (init_t*)(init_startC + shdr[i].sh_size);
1655 // ctors run in reverse
1656 for (init = init_end - 1; init >= init_start; init--) {
1657 (*init)(argc, argv, envv);
1658 }
1659 }
1660 }
1661
1662 freeProgEnvv(envc, envv);
1663 return 1;
1664 }
1665
1666 /*
1667 * PowerPC & X86_64 ELF specifics
1668 */
1669
1670 #if NEED_SYMBOL_EXTRAS
1671
1672 int ocAllocateSymbolExtras_ELF( ObjectCode *oc )
1673 {
1674 Elf_Ehdr *ehdr;
1675 Elf_Shdr* shdr;
1676 Elf_Word i, shnum;
1677
1678 ehdr = (Elf_Ehdr *) oc->image;
1679 shdr = (Elf_Shdr *) ( ((char *)oc->image) + ehdr->e_shoff );
1680
1681 shnum = elf_shnum(ehdr);
1682
1683 for( i = 0; i < shnum; i++ )
1684 if( shdr[i].sh_type == SHT_SYMTAB )
1685 break;
1686
1687 if( i == shnum )
1688 {
1689 // Not having a symbol table is not in principle a problem.
1690 // When an object file has no symbols then the 'strip' program
1691 // typically will remove the symbol table entirely.
1692 IF_DEBUG(linker, debugBelch( "The ELF file %s contains no symtab\n",
1693 oc->archiveMemberName ? oc->archiveMemberName : oc->fileName ));
1694 return 1;
1695 }
1696
1697 if( shdr[i].sh_entsize != sizeof( Elf_Sym ) )
1698 {
1699 errorBelch( "The entry size (%d) of the symtab isn't %d\n",
1700 (int) shdr[i].sh_entsize, (int) sizeof( Elf_Sym ) );
1701
1702 return 0;
1703 }
1704
1705 return ocAllocateSymbolExtras( oc, shdr[i].sh_size / sizeof( Elf_Sym ), 0 );
1706 }
1707
1708 #endif /* NEED_SYMBOL_EXTRAS */
1709
1710 #endif /* elf */