4c40b108776fc1b8680e914f0ae4f573abee7cd4
[ghc.git] / rts / linker / SymbolExtras.c
1 /* --------------------------------------------------------------------------
2 * Symbol Extras.
3 * This is about allocating a small chunk of memory for every symbol in the
4 * object file. We make sure that the SymboLExtras are always "in range" of
5 * limited-range PC-relative instructions on various platforms by allocating
6 * them right next to the object code itself.
7 *
8 * This implementation is shared by the MachO and ELF implementations. Windows'
9 * PEi386 has its own implementation of symbol extras.
10 */
11
12 #include "LinkerInternals.h"
13
14 #if defined(NEED_SYMBOL_EXTRAS)
15 #if !defined(x86_64_HOST_ARCH) || !defined(mingw32_HOST_OS)
16
17 #include "RtsUtils.h"
18 #include "sm/OSMem.h"
19 #include "linker/SymbolExtras.h"
20 #include "linker/M32Alloc.h"
21
22 #if defined(OBJFORMAT_ELF)
23 # include "linker/Elf.h"
24 #elif defined(OBJFORMAT_MACHO)
25 # include "linker/MachO.h"
26 #endif
27
28 #include <string.h>
29 #if RTS_LINKER_USE_MMAP
30 #include <sys/mman.h>
31 #endif /* RTS_LINKER_USE_MMAP */
32
33 /*
34 ocAllocateSymbolExtras
35
36 Allocate additional space at the end of the object file image to make room
37 for jump islands (powerpc, x86_64, arm) and GOT entries (x86_64).
38
39 PowerPC relative branch instructions have a 24 bit displacement field.
40 As PPC code is always 4-byte-aligned, this yields a +-32MB range.
41 If a particular imported symbol is outside this range, we have to redirect
42 the jump to a short piece of new code that just loads the 32bit absolute
43 address and jumps there.
44 On x86_64, PC-relative jumps and PC-relative accesses to the GOT are limited
45 to 32 bits (+-2GB).
46
47 This function just allocates space for one SymbolExtra for every
48 undefined symbol in the object file. The code for the jump islands is
49 filled in by makeSymbolExtra below.
50 */
51
52 int ocAllocateSymbolExtras( ObjectCode* oc, int count, int first )
53 {
54 size_t n;
55 void* oldImage = oc->image;
56
57 if (count > 0) {
58 if (!RTS_LINKER_USE_MMAP) {
59
60 // round up to the nearest 4
61 int aligned = (oc->fileSize + 3) & ~3;
62 int misalignment = oc->misalignment;
63
64 oc->image -= misalignment;
65 oc->image = stgReallocBytes( oc->image,
66 misalignment +
67 aligned + sizeof (SymbolExtra) * count,
68 "ocAllocateSymbolExtras" );
69 oc->image += misalignment;
70
71 oc->symbol_extras = (SymbolExtra *) (oc->image + aligned);
72 } else if (USE_CONTIGUOUS_MMAP || RtsFlags.MiscFlags.linkerAlwaysPic) {
73 n = roundUpToPage(oc->fileSize);
74
75 /* Keep image and symbol_extras contiguous */
76
77 size_t allocated_size = n + (sizeof(SymbolExtra) * count);
78 void *new = mmapForLinker(allocated_size, MAP_ANONYMOUS, -1, 0);
79 if (new) {
80 memcpy(new, oc->image, oc->fileSize);
81 if (oc->imageMapped) {
82 munmap(oc->image, n);
83 }
84 oc->image = new;
85 oc->imageMapped = true;
86 oc->fileSize = n + (sizeof(SymbolExtra) * count);
87 oc->symbol_extras = (SymbolExtra *) (oc->image + n);
88 if (mprotect(new, allocated_size,
89 PROT_READ | PROT_WRITE | PROT_EXEC) != 0) {
90 sysErrorBelch("unable to protect memory");
91 }
92 }
93 else {
94 oc->symbol_extras = NULL;
95 return 0;
96 }
97 } else {
98 oc->symbol_extras = m32_alloc(sizeof(SymbolExtra) * count, 8);
99 if (oc->symbol_extras == NULL) return 0;
100 }
101 }
102
103 if (oc->symbol_extras != NULL) {
104 memset( oc->symbol_extras, 0, sizeof (SymbolExtra) * count );
105 }
106
107 // ObjectCodeFormatInfo contains computed addresses based on offset to
108 // image, if the address of image changes, we need to invalidate
109 // the ObjectCodeFormatInfo and recompute it.
110 if (oc->image != oldImage) {
111 #if defined(OBJFORMAT_MACHO)
112 ocInit_MachO( oc );
113 #endif
114 #if defined(OBJFORMAT_ELF)
115 ocInit_ELF( oc );
116 #endif
117 }
118
119 oc->first_symbol_extra = first;
120 oc->n_symbol_extras = count;
121
122 return 1;
123 }
124
125
126 #if !defined(arm_HOST_ARCH)
127 SymbolExtra* makeSymbolExtra( ObjectCode const* oc,
128 unsigned long symbolNumber,
129 unsigned long target )
130 {
131 SymbolExtra *extra;
132
133 ASSERT( symbolNumber >= oc->first_symbol_extra
134 && symbolNumber - oc->first_symbol_extra < oc->n_symbol_extras);
135
136 extra = &oc->symbol_extras[symbolNumber - oc->first_symbol_extra];
137
138 #if defined(powerpc_HOST_ARCH)
139 // lis r12, hi16(target)
140 extra->jumpIsland.lis_r12 = 0x3d80;
141 extra->jumpIsland.hi_addr = target >> 16;
142
143 // ori r12, r12, lo16(target)
144 extra->jumpIsland.ori_r12_r12 = 0x618c;
145 extra->jumpIsland.lo_addr = target & 0xffff;
146
147 // mtctr r12
148 extra->jumpIsland.mtctr_r12 = 0x7d8903a6;
149
150 // bctr
151 extra->jumpIsland.bctr = 0x4e800420;
152 #endif /* powerpc_HOST_ARCH */
153 #if defined(x86_64_HOST_ARCH)
154 // jmp *-14(%rip)
155 static uint8_t jmp[] = { 0xFF, 0x25, 0xF2, 0xFF, 0xFF, 0xFF };
156 extra->addr = target;
157 memcpy(extra->jumpIsland, jmp, 6);
158 #endif /* x86_64_HOST_ARCH */
159
160 return extra;
161 }
162 #endif /* !arm_HOST_ARCH */
163 #endif /* !x86_64_HOST_ARCH) || !mingw32_HOST_OS */
164 #endif // NEED_SYMBOL_EXTRAS