[331] | 1 | #ifdef TARGET_DEFS_ONLY
|
---|
| 2 |
|
---|
| 3 | #define EM_TCC_TARGET EM_X86_64
|
---|
| 4 |
|
---|
| 5 | /* relocation type for 32 bit data relocation */
|
---|
| 6 | #define R_DATA_32 R_X86_64_32S
|
---|
| 7 | #define R_DATA_PTR R_X86_64_64
|
---|
| 8 | #define R_JMP_SLOT R_X86_64_JUMP_SLOT
|
---|
| 9 | #define R_GLOB_DAT R_X86_64_GLOB_DAT
|
---|
| 10 | #define R_COPY R_X86_64_COPY
|
---|
| 11 | #define R_RELATIVE R_X86_64_RELATIVE
|
---|
| 12 |
|
---|
| 13 | #define R_NUM R_X86_64_NUM
|
---|
| 14 |
|
---|
| 15 | #define ELF_START_ADDR 0x400000
|
---|
| 16 | #define ELF_PAGE_SIZE 0x200000
|
---|
| 17 |
|
---|
| 18 | #define PCRELATIVE_DLLPLT 1
|
---|
| 19 | #define RELOCATE_DLLPLT 1
|
---|
| 20 |
|
---|
| 21 | #else /* !TARGET_DEFS_ONLY */
|
---|
| 22 |
|
---|
| 23 | #include "tcc.h"
|
---|
| 24 |
|
---|
| 25 | /* Returns 1 for a code relocation, 0 for a data relocation. For unknown
|
---|
| 26 | relocations, returns -1. */
|
---|
| 27 | int code_reloc (int reloc_type)
|
---|
| 28 | {
|
---|
| 29 | switch (reloc_type) {
|
---|
| 30 | case R_X86_64_32:
|
---|
| 31 | case R_X86_64_32S:
|
---|
| 32 | case R_X86_64_64:
|
---|
| 33 | case R_X86_64_GOTPC32:
|
---|
| 34 | case R_X86_64_GOTPC64:
|
---|
| 35 | case R_X86_64_GOTPCREL:
|
---|
| 36 | case R_X86_64_GOTPCRELX:
|
---|
| 37 | case R_X86_64_REX_GOTPCRELX:
|
---|
| 38 | case R_X86_64_GOTTPOFF:
|
---|
| 39 | case R_X86_64_GOT32:
|
---|
| 40 | case R_X86_64_GOT64:
|
---|
| 41 | case R_X86_64_GLOB_DAT:
|
---|
| 42 | case R_X86_64_COPY:
|
---|
| 43 | case R_X86_64_RELATIVE:
|
---|
| 44 | case R_X86_64_GOTOFF64:
|
---|
| 45 | return 0;
|
---|
| 46 |
|
---|
| 47 | case R_X86_64_PC32:
|
---|
| 48 | case R_X86_64_PC64:
|
---|
| 49 | case R_X86_64_PLT32:
|
---|
| 50 | case R_X86_64_PLTOFF64:
|
---|
| 51 | case R_X86_64_JUMP_SLOT:
|
---|
| 52 | return 1;
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | tcc_error ("Unknown relocation type: %d", reloc_type);
|
---|
| 56 | return -1;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | /* Returns an enumerator to describe whether and when the relocation needs a
|
---|
| 60 | GOT and/or PLT entry to be created. See tcc.h for a description of the
|
---|
| 61 | different values. */
|
---|
| 62 | int gotplt_entry_type (int reloc_type)
|
---|
| 63 | {
|
---|
| 64 | switch (reloc_type) {
|
---|
| 65 | case R_X86_64_GLOB_DAT:
|
---|
| 66 | case R_X86_64_JUMP_SLOT:
|
---|
| 67 | case R_X86_64_COPY:
|
---|
| 68 | case R_X86_64_RELATIVE:
|
---|
| 69 | return NO_GOTPLT_ENTRY;
|
---|
| 70 |
|
---|
| 71 | /* The following relocs wouldn't normally need GOT or PLT
|
---|
| 72 | slots, but we need them for simplicity in the link
|
---|
| 73 | editor part. See our caller for comments. */
|
---|
| 74 | case R_X86_64_32:
|
---|
| 75 | case R_X86_64_32S:
|
---|
| 76 | case R_X86_64_64:
|
---|
| 77 | case R_X86_64_PC32:
|
---|
| 78 | case R_X86_64_PC64:
|
---|
| 79 | return AUTO_GOTPLT_ENTRY;
|
---|
| 80 |
|
---|
| 81 | case R_X86_64_GOTTPOFF:
|
---|
| 82 | return BUILD_GOT_ONLY;
|
---|
| 83 |
|
---|
| 84 | case R_X86_64_GOT32:
|
---|
| 85 | case R_X86_64_GOT64:
|
---|
| 86 | case R_X86_64_GOTPC32:
|
---|
| 87 | case R_X86_64_GOTPC64:
|
---|
| 88 | case R_X86_64_GOTOFF64:
|
---|
| 89 | case R_X86_64_GOTPCREL:
|
---|
| 90 | case R_X86_64_GOTPCRELX:
|
---|
| 91 | case R_X86_64_REX_GOTPCRELX:
|
---|
| 92 | case R_X86_64_PLT32:
|
---|
| 93 | case R_X86_64_PLTOFF64:
|
---|
| 94 | return ALWAYS_GOTPLT_ENTRY;
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | tcc_error ("Unknown relocation type: %d", reloc_type);
|
---|
| 98 | return -1;
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | ST_FUNC unsigned create_plt_entry(TCCState *s1, unsigned got_offset, struct sym_attr *attr)
|
---|
| 102 | {
|
---|
| 103 | Section *plt = s1->plt;
|
---|
| 104 | uint8_t *p;
|
---|
| 105 | int modrm;
|
---|
| 106 | unsigned plt_offset, relofs;
|
---|
| 107 |
|
---|
| 108 | modrm = 0x25;
|
---|
| 109 |
|
---|
| 110 | /* empty PLT: create PLT0 entry that pushes the library identifier
|
---|
| 111 | (GOT + PTR_SIZE) and jumps to ld.so resolution routine
|
---|
| 112 | (GOT + 2 * PTR_SIZE) */
|
---|
| 113 | if (plt->data_offset == 0) {
|
---|
| 114 | p = section_ptr_add(plt, 16);
|
---|
| 115 | p[0] = 0xff; /* pushl got + PTR_SIZE */
|
---|
| 116 | p[1] = modrm + 0x10;
|
---|
| 117 | write32le(p + 2, PTR_SIZE);
|
---|
| 118 | p[6] = 0xff; /* jmp *(got + PTR_SIZE * 2) */
|
---|
| 119 | p[7] = modrm;
|
---|
| 120 | write32le(p + 8, PTR_SIZE * 2);
|
---|
| 121 | }
|
---|
| 122 | plt_offset = plt->data_offset;
|
---|
| 123 |
|
---|
| 124 | /* The PLT slot refers to the relocation entry it needs via offset.
|
---|
| 125 | The reloc entry is created below, so its offset is the current
|
---|
| 126 | data_offset */
|
---|
| 127 | relofs = s1->got->reloc ? s1->got->reloc->data_offset : 0;
|
---|
| 128 |
|
---|
| 129 | /* Jump to GOT entry where ld.so initially put the address of ip + 4 */
|
---|
| 130 | p = section_ptr_add(plt, 16);
|
---|
| 131 | p[0] = 0xff; /* jmp *(got + x) */
|
---|
| 132 | p[1] = modrm;
|
---|
| 133 | write32le(p + 2, got_offset);
|
---|
| 134 | p[6] = 0x68; /* push $xxx */
|
---|
| 135 | /* On x86-64, the relocation is referred to by _index_ */
|
---|
| 136 | write32le(p + 7, relofs / sizeof (ElfW_Rel));
|
---|
| 137 | p[11] = 0xe9; /* jmp plt_start */
|
---|
| 138 | write32le(p + 12, -(plt->data_offset));
|
---|
| 139 | return plt_offset;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | /* relocate the PLT: compute addresses and offsets in the PLT now that final
|
---|
| 143 | address for PLT and GOT are known (see fill_program_header) */
|
---|
| 144 | ST_FUNC void relocate_plt(TCCState *s1)
|
---|
| 145 | {
|
---|
| 146 | uint8_t *p, *p_end;
|
---|
| 147 |
|
---|
| 148 | if (!s1->plt)
|
---|
| 149 | return;
|
---|
| 150 |
|
---|
| 151 | p = s1->plt->data;
|
---|
| 152 | p_end = p + s1->plt->data_offset;
|
---|
| 153 |
|
---|
| 154 | if (p < p_end) {
|
---|
| 155 | int x = s1->got->sh_addr - s1->plt->sh_addr - 6;
|
---|
| 156 | add32le(p + 2, x);
|
---|
| 157 | add32le(p + 8, x - 6);
|
---|
| 158 | p += 16;
|
---|
| 159 | while (p < p_end) {
|
---|
| 160 | add32le(p + 2, x + s1->plt->data - p);
|
---|
| 161 | p += 16;
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | static ElfW_Rel *qrel; /* ptr to next reloc entry reused */
|
---|
| 167 |
|
---|
| 168 | void relocate_init(Section *sr)
|
---|
| 169 | {
|
---|
| 170 | qrel = (ElfW_Rel *) sr->data;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | void relocate(TCCState *s1, ElfW_Rel *rel, int type, unsigned char *ptr, addr_t addr, addr_t val)
|
---|
| 174 | {
|
---|
| 175 | int sym_index, esym_index;
|
---|
| 176 |
|
---|
| 177 | sym_index = ELFW(R_SYM)(rel->r_info);
|
---|
| 178 |
|
---|
| 179 | switch (type) {
|
---|
| 180 | case R_X86_64_64:
|
---|
| 181 | if (s1->output_type == TCC_OUTPUT_DLL) {
|
---|
| 182 | esym_index = s1->sym_attrs[sym_index].dyn_index;
|
---|
| 183 | qrel->r_offset = rel->r_offset;
|
---|
| 184 | if (esym_index) {
|
---|
| 185 | qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_64);
|
---|
| 186 | qrel->r_addend = rel->r_addend;
|
---|
| 187 | qrel++;
|
---|
| 188 | break;
|
---|
| 189 | } else {
|
---|
| 190 | qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
|
---|
| 191 | qrel->r_addend = read64le(ptr) + val;
|
---|
| 192 | qrel++;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | add64le(ptr, val);
|
---|
| 196 | break;
|
---|
| 197 | case R_X86_64_32:
|
---|
| 198 | case R_X86_64_32S:
|
---|
| 199 | if (s1->output_type == TCC_OUTPUT_DLL) {
|
---|
| 200 | /* XXX: this logic may depend on TCC's codegen
|
---|
| 201 | now TCC uses R_X86_64_32 even for a 64bit pointer */
|
---|
| 202 | qrel->r_info = ELFW(R_INFO)(0, R_X86_64_RELATIVE);
|
---|
| 203 | /* Use sign extension! */
|
---|
| 204 | qrel->r_addend = (int)read32le(ptr) + val;
|
---|
| 205 | qrel++;
|
---|
| 206 | }
|
---|
| 207 | add32le(ptr, val);
|
---|
| 208 | break;
|
---|
| 209 |
|
---|
| 210 | case R_X86_64_PC32:
|
---|
| 211 | if (s1->output_type == TCC_OUTPUT_DLL) {
|
---|
| 212 | /* DLL relocation */
|
---|
| 213 | esym_index = s1->sym_attrs[sym_index].dyn_index;
|
---|
| 214 | if (esym_index) {
|
---|
| 215 | qrel->r_offset = rel->r_offset;
|
---|
| 216 | qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC32);
|
---|
| 217 | /* Use sign extension! */
|
---|
| 218 | qrel->r_addend = (int)read32le(ptr) + rel->r_addend;
|
---|
| 219 | qrel++;
|
---|
| 220 | break;
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | goto plt32pc32;
|
---|
| 224 |
|
---|
| 225 | case R_X86_64_PLT32:
|
---|
| 226 | /* fallthrough: val already holds the PLT slot address */
|
---|
| 227 |
|
---|
| 228 | plt32pc32:
|
---|
| 229 | {
|
---|
| 230 | long long diff;
|
---|
| 231 | diff = (long long)val - addr;
|
---|
| 232 | if (diff < -2147483648LL || diff > 2147483647LL) {
|
---|
| 233 | tcc_error("internal error: relocation failed");
|
---|
| 234 | }
|
---|
| 235 | add32le(ptr, diff);
|
---|
| 236 | }
|
---|
| 237 | break;
|
---|
| 238 |
|
---|
| 239 | case R_X86_64_PLTOFF64:
|
---|
| 240 | add64le(ptr, val - s1->got->sh_addr + rel->r_addend);
|
---|
| 241 | break;
|
---|
| 242 |
|
---|
| 243 | case R_X86_64_PC64:
|
---|
| 244 | if (s1->output_type == TCC_OUTPUT_DLL) {
|
---|
| 245 | /* DLL relocation */
|
---|
| 246 | esym_index = s1->sym_attrs[sym_index].dyn_index;
|
---|
| 247 | if (esym_index) {
|
---|
| 248 | qrel->r_offset = rel->r_offset;
|
---|
| 249 | qrel->r_info = ELFW(R_INFO)(esym_index, R_X86_64_PC64);
|
---|
| 250 | qrel->r_addend = read64le(ptr) + rel->r_addend;
|
---|
| 251 | qrel++;
|
---|
| 252 | break;
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 | add64le(ptr, val - addr);
|
---|
| 256 | break;
|
---|
| 257 |
|
---|
| 258 | case R_X86_64_GLOB_DAT:
|
---|
| 259 | case R_X86_64_JUMP_SLOT:
|
---|
| 260 | /* They don't need addend */
|
---|
| 261 | write64le(ptr, val - rel->r_addend);
|
---|
| 262 | break;
|
---|
| 263 | case R_X86_64_GOTPCREL:
|
---|
| 264 | case R_X86_64_GOTPCRELX:
|
---|
| 265 | case R_X86_64_REX_GOTPCRELX:
|
---|
| 266 | add32le(ptr, s1->got->sh_addr - addr +
|
---|
| 267 | s1->sym_attrs[sym_index].got_offset - 4);
|
---|
| 268 | break;
|
---|
| 269 | case R_X86_64_GOTPC32:
|
---|
| 270 | add32le(ptr, s1->got->sh_addr - addr + rel->r_addend);
|
---|
| 271 | break;
|
---|
| 272 | case R_X86_64_GOTPC64:
|
---|
| 273 | add64le(ptr, s1->got->sh_addr - addr + rel->r_addend);
|
---|
| 274 | break;
|
---|
| 275 | case R_X86_64_GOTTPOFF:
|
---|
| 276 | add32le(ptr, val - s1->got->sh_addr);
|
---|
| 277 | break;
|
---|
| 278 | case R_X86_64_GOT32:
|
---|
| 279 | /* we load the got offset */
|
---|
| 280 | add32le(ptr, s1->sym_attrs[sym_index].got_offset);
|
---|
| 281 | break;
|
---|
| 282 | case R_X86_64_GOT64:
|
---|
| 283 | /* we load the got offset */
|
---|
| 284 | add64le(ptr, s1->sym_attrs[sym_index].got_offset);
|
---|
| 285 | break;
|
---|
| 286 | case R_X86_64_GOTOFF64:
|
---|
| 287 | add64le(ptr, val - s1->got->sh_addr);
|
---|
| 288 | break;
|
---|
| 289 | case R_X86_64_RELATIVE:
|
---|
| 290 | #ifdef TCC_TARGET_PE
|
---|
| 291 | add32le(ptr, val - s1->pe_imagebase);
|
---|
| 292 | #endif
|
---|
| 293 | /* do nothing */
|
---|
| 294 | break;
|
---|
| 295 | }
|
---|
| 296 | }
|
---|
| 297 |
|
---|
| 298 | #endif /* !TARGET_DEFS_ONLY */
|
---|