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