1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Get the x86 version of the relocation engine */ 28 #define DO_RELOC_LIBLD_X86 29 30 #include <string.h> 31 #include <stdio.h> 32 #include <strings.h> 33 #include <sys/elf_amd64.h> 34 #include <debug.h> 35 #include <reloc.h> 36 #include <i386/machdep_x86.h> 37 #include "msg.h" 38 #include "_libld.h" 39 #include "unwind.amd.h" 40 41 42 /* Forward declarations */ 43 static Gotndx *ld_find_gotndx(List *, Gotref, Ofl_desc *, Rel_desc *); 44 static Xword ld_calc_got_offset(Rel_desc *, Ofl_desc *); 45 46 47 static Word 48 ld_init_rel(Rel_desc *reld, void *reloc) 49 { 50 Rela * rel = (Rela *)reloc; 51 52 /* LINTED */ 53 reld->rel_rtype = (Word)ELF_R_TYPE(rel->r_info, M_MACH); 54 reld->rel_roffset = rel->r_offset; 55 reld->rel_raddend = rel->r_addend; 56 reld->rel_typedata = 0; 57 58 reld->rel_flags |= FLG_REL_RELA; 59 60 return ((Word)ELF_R_SYM(rel->r_info)); 61 } 62 63 static void 64 ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl) 65 { 66 ofl->ofl_dehdr->e_flags |= ehdr->e_flags; 67 } 68 69 static void 70 ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt) 71 { 72 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) { 73 /* 74 * Create this entry if we are going to create a PLT table. 75 */ 76 if (ofl->ofl_pltcnt) 77 (*cnt)++; /* DT_PLTGOT */ 78 } 79 } 80 81 static void 82 ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn) 83 { 84 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) { 85 (*dyn)->d_tag = DT_PLTGOT; 86 if (ofl->ofl_osgot) 87 (*dyn)->d_un.d_ptr = ofl->ofl_osgot->os_shdr->sh_addr; 88 else 89 (*dyn)->d_un.d_ptr = 0; 90 (*dyn)++; 91 } 92 } 93 94 static Xword 95 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl) 96 { 97 Xword value; 98 99 value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) + 100 M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * M_PLT_ENTSIZE); 101 return (value); 102 } 103 104 /* 105 * Build a single plt entry - code is: 106 * JMP *name1@GOTPCREL(%rip) 107 * PUSHL $index 108 * JMP .PLT0 109 */ 110 static uchar_t pltn_entry[M_PLT_ENTSIZE] = { 111 /* 0x00 jmpq *name1@GOTPCREL(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 112 /* 0x06 pushq $index */ 0x68, 0x00, 0x00, 0x00, 0x00, 113 /* 0x0b jmpq .plt0(%rip) */ 0xe9, 0x00, 0x00, 0x00, 0x00 114 /* 0x10 */ 115 }; 116 117 static uintptr_t 118 plt_entry(Ofl_desc * ofl, Sym_desc * sdp) 119 { 120 uchar_t *plt0, *pltent, *gotent; 121 Sword plt_off; 122 Word got_off; 123 Xword val1; 124 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 125 126 got_off = sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 127 plt_off = M_PLT_RESERVSZ + ((sdp->sd_aux->sa_PLTndx - 1) * 128 M_PLT_ENTSIZE); 129 plt0 = (uchar_t *)(ofl->ofl_osplt->os_outdata->d_buf); 130 pltent = plt0 + plt_off; 131 gotent = (uchar_t *)(ofl->ofl_osgot->os_outdata->d_buf) + got_off; 132 133 bcopy(pltn_entry, pltent, sizeof (pltn_entry)); 134 /* 135 * Fill in the got entry with the address of the next instruction. 136 */ 137 /* LINTED */ 138 *(Word *)gotent = ofl->ofl_osplt->os_shdr->sh_addr + plt_off + 139 M_PLT_INSSIZE; 140 if (bswap) 141 /* LINTED */ 142 *(Word *)gotent = ld_bswap_Word(*(Word *)gotent); 143 144 /* 145 * If '-z noreloc' is specified - skip the do_reloc_ld 146 * stage. 147 */ 148 if (!OFL_DO_RELOC(ofl)) 149 return (1); 150 151 /* 152 * patchup: 153 * jmpq *name1@gotpcrel(%rip) 154 * 155 * NOTE: 0x06 represents next instruction. 156 */ 157 val1 = (ofl->ofl_osgot->os_shdr->sh_addr + got_off) - 158 (ofl->ofl_osplt->os_shdr->sh_addr + plt_off) - 0x06; 159 160 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 161 &val1, MSG_ORIG(MSG_SYM_PLTENT), 162 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 163 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 164 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 165 return (S_ERROR); 166 } 167 168 /* 169 * patchup: 170 * pushq $pltndx 171 */ 172 val1 = (Xword)(sdp->sd_aux->sa_PLTndx - 1); 173 174 if (do_reloc_ld(R_AMD64_32, &pltent[0x07], 175 &val1, MSG_ORIG(MSG_SYM_PLTENT), 176 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 177 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 178 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 179 return (S_ERROR); 180 } 181 182 /* 183 * patchup: 184 * jmpq .plt0(%rip) 185 * NOTE: 0x10 represents next instruction. The rather complex 186 * series of casts is necessary to sign extend an offset into 187 * a 64-bit value while satisfying various compiler error 188 * checks. Handle with care. 189 */ 190 val1 = (Xword)((intptr_t)((uintptr_t)plt0 - 191 (uintptr_t)(&pltent[0x10]))); 192 193 if (do_reloc_ld(R_AMD64_PC32, &pltent[0x0c], 194 &val1, MSG_ORIG(MSG_SYM_PLTENT), 195 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 196 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_PLT_PLTNFAIL), 197 sdp->sd_aux->sa_PLTndx, demangle(sdp->sd_name)); 198 return (S_ERROR); 199 } 200 201 return (1); 202 } 203 204 static uintptr_t 205 ld_perform_outreloc(Rel_desc * orsp, Ofl_desc * ofl) 206 { 207 Os_desc * relosp, * osp = 0; 208 Word ndx; 209 Xword roffset, value; 210 Sxword raddend; 211 Rela rea; 212 char *relbits; 213 Sym_desc * sdp, * psym = (Sym_desc *)0; 214 int sectmoved = 0; 215 216 raddend = orsp->rel_raddend; 217 sdp = orsp->rel_sym; 218 219 /* 220 * If the section this relocation is against has been discarded 221 * (-zignore), then also discard (skip) the relocation itself. 222 */ 223 if (orsp->rel_isdesc && ((orsp->rel_flags & 224 (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) && 225 (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) { 226 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp)); 227 return (1); 228 } 229 230 /* 231 * If this is a relocation against a move table, or expanded move 232 * table, adjust the relocation entries. 233 */ 234 if (orsp->rel_move) 235 ld_adj_movereloc(ofl, orsp); 236 237 /* 238 * If this is a relocation against a section then we need to adjust the 239 * raddend field to compensate for the new position of the input section 240 * within the new output section. 241 */ 242 if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) { 243 if (ofl->ofl_parsym.head && 244 (sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 245 /* LINTED */ 246 (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) { 247 DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym)); 248 sectmoved = 1; 249 if (ofl->ofl_flags & FLG_OF_RELOBJ) 250 raddend = psym->sd_sym->st_value; 251 else 252 raddend = psym->sd_sym->st_value - 253 psym->sd_isc->is_osdesc->os_shdr->sh_addr; 254 /* LINTED */ 255 raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata); 256 if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 257 raddend += 258 psym->sd_isc->is_osdesc->os_shdr->sh_addr; 259 } else { 260 /* LINTED */ 261 raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata); 262 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 263 raddend += 264 sdp->sd_isc->is_osdesc->os_shdr->sh_addr; 265 } 266 } 267 268 value = sdp->sd_sym->st_value; 269 270 if (orsp->rel_flags & FLG_REL_GOT) { 271 /* 272 * Note: for GOT relative relocations on amd64 273 * we discard the addend. It was relevant 274 * to the reference - not to the data item 275 * being referenced (ie: that -4 thing). 276 */ 277 raddend = 0; 278 osp = ofl->ofl_osgot; 279 roffset = ld_calc_got_offset(orsp, ofl); 280 281 } else if (orsp->rel_flags & FLG_REL_PLT) { 282 /* 283 * Note that relocations for PLT's actually 284 * cause a relocation againt the GOT. 285 */ 286 osp = ofl->ofl_osplt; 287 roffset = (ofl->ofl_osgot->os_shdr->sh_addr) + 288 sdp->sd_aux->sa_PLTGOTndx * M_GOT_ENTSIZE; 289 raddend = 0; 290 if (plt_entry(ofl, sdp) == S_ERROR) 291 return (S_ERROR); 292 293 } else if (orsp->rel_flags & FLG_REL_BSS) { 294 /* 295 * This must be a R_AMD64_COPY. For these set the roffset to 296 * point to the new symbols location. 297 */ 298 osp = ofl->ofl_isbss->is_osdesc; 299 roffset = value; 300 301 /* 302 * The raddend doesn't mean anything in a R_SPARC_COPY 303 * relocation. Null it out because it can confuse people. 304 */ 305 raddend = 0; 306 } else { 307 osp = orsp->rel_osdesc; 308 309 /* 310 * Calculate virtual offset of reference point; equals offset 311 * into section + vaddr of section for loadable sections, or 312 * offset plus section displacement for nonloadable sections. 313 */ 314 roffset = orsp->rel_roffset + 315 (Off)_elf_getxoff(orsp->rel_isdesc->is_indata); 316 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 317 roffset += orsp->rel_isdesc->is_osdesc-> 318 os_shdr->sh_addr; 319 } 320 321 if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0)) 322 relosp = ofl->ofl_osrel; 323 324 /* 325 * Assign the symbols index for the output relocation. If the 326 * relocation refers to a SECTION symbol then it's index is based upon 327 * the output sections symbols index. Otherwise the index can be 328 * derived from the symbols index itself. 329 */ 330 if (orsp->rel_rtype == R_AMD64_RELATIVE) 331 ndx = STN_UNDEF; 332 else if ((orsp->rel_flags & FLG_REL_SCNNDX) || 333 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 334 if (sectmoved == 0) { 335 /* 336 * Check for a null input section. This can 337 * occur if this relocation references a symbol 338 * generated by sym_add_sym(). 339 */ 340 if ((sdp->sd_isc != 0) && 341 (sdp->sd_isc->is_osdesc != 0)) 342 ndx = sdp->sd_isc->is_osdesc->os_scnsymndx; 343 else 344 ndx = sdp->sd_shndx; 345 } else 346 ndx = ofl->ofl_parexpnndx; 347 } else 348 ndx = sdp->sd_symndx; 349 350 /* 351 * Add the symbols 'value' to the addend field. 352 */ 353 if (orsp->rel_flags & FLG_REL_ADVAL) 354 raddend += value; 355 356 /* 357 * The addend field for R_AMD64_DTPMOD64 means nothing. The addend 358 * is propagated in the corresponding R_AMD64_DTPOFF64 relocation. 359 */ 360 if (orsp->rel_rtype == R_AMD64_DTPMOD64) 361 raddend = 0; 362 363 relbits = (char *)relosp->os_outdata->d_buf; 364 365 rea.r_info = ELF_R_INFO(ndx, orsp->rel_rtype); 366 rea.r_offset = roffset; 367 rea.r_addend = raddend; 368 DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name, 369 orsp->rel_sname)); 370 371 /* 372 * Assert we haven't walked off the end of our relocation table. 373 */ 374 assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size); 375 376 (void) memcpy((relbits + relosp->os_szoutrels), 377 (char *)&rea, sizeof (Rela)); 378 relosp->os_szoutrels += (Xword)sizeof (Rela); 379 380 /* 381 * Determine if this relocation is against a non-writable, allocatable 382 * section. If so we may need to provide a text relocation diagnostic. 383 * Note that relocations against the .plt (R_AMD64_JUMP_SLOT) actually 384 * result in modifications to the .got. 385 */ 386 if (orsp->rel_rtype == R_AMD64_JUMP_SLOT) 387 osp = ofl->ofl_osgot; 388 389 ld_reloc_remain_entry(orsp, osp, ofl); 390 return (1); 391 } 392 393 /* 394 * amd64 Instructions for TLS processing 395 */ 396 static uchar_t tlsinstr_gd_ie[] = { 397 /* 398 * 0x00 movq %fs:0, %rax 399 */ 400 0x64, 0x48, 0x8b, 0x04, 0x25, 401 0x00, 0x00, 0x00, 0x00, 402 /* 403 * 0x09 addq x@gottpoff(%rip), %rax 404 */ 405 0x48, 0x03, 0x05, 0x00, 0x00, 406 0x00, 0x00 407 }; 408 409 static uchar_t tlsinstr_gd_le[] = { 410 /* 411 * 0x00 movq %fs:0, %rax 412 */ 413 0x64, 0x48, 0x8b, 0x04, 0x25, 414 0x00, 0x00, 0x00, 0x00, 415 /* 416 * 0x09 leaq x@gottpoff(%rip), %rax 417 */ 418 0x48, 0x8d, 0x80, 0x00, 0x00, 419 0x00, 0x00 420 }; 421 422 static uchar_t tlsinstr_ld_le[] = { 423 /* 424 * .byte 0x66 425 */ 426 0x66, 427 /* 428 * .byte 0x66 429 */ 430 0x66, 431 /* 432 * .byte 0x66 433 */ 434 0x66, 435 /* 436 * movq %fs:0, %rax 437 */ 438 0x64, 0x48, 0x8b, 0x04, 0x25, 439 0x00, 0x00, 0x00, 0x00 440 }; 441 442 443 static Fixupret 444 tls_fixups(Ofl_desc *ofl, Rel_desc *arsp) 445 { 446 Sym_desc *sdp = arsp->rel_sym; 447 Word rtype = arsp->rel_rtype; 448 uchar_t *offset; 449 450 offset = (uchar_t *)((uintptr_t)arsp->rel_roffset + 451 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) + 452 (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf); 453 454 if (sdp->sd_ref == REF_DYN_NEED) { 455 /* 456 * IE reference model 457 */ 458 switch (rtype) { 459 case R_AMD64_TLSGD: 460 /* 461 * GD -> IE 462 * 463 * Transition: 464 * 0x00 .byte 0x66 465 * 0x01 leaq x@tlsgd(%rip), %rdi 466 * 0x08 .word 0x6666 467 * 0x0a rex64 468 * 0x0b call __tls_get_addr@plt 469 * 0x10 470 * To: 471 * 0x00 movq %fs:0, %rax 472 * 0x09 addq x@gottpoff(%rip), %rax 473 * 0x10 474 */ 475 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 476 R_AMD64_GOTTPOFF, arsp)); 477 arsp->rel_rtype = R_AMD64_GOTTPOFF; 478 arsp->rel_roffset += 8; 479 arsp->rel_raddend = (Sxword)-4; 480 481 /* 482 * Adjust 'offset' to beginning of instruction 483 * sequence. 484 */ 485 offset -= 4; 486 (void) memcpy(offset, tlsinstr_gd_ie, 487 sizeof (tlsinstr_gd_ie)); 488 return (FIX_RELOC); 489 490 case R_AMD64_PLT32: 491 /* 492 * Fixup done via the TLS_GD relocation. 493 */ 494 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 495 R_AMD64_NONE, arsp)); 496 return (FIX_DONE); 497 } 498 } 499 500 /* 501 * LE reference model 502 */ 503 switch (rtype) { 504 case R_AMD64_TLSGD: 505 /* 506 * GD -> LE 507 * 508 * Transition: 509 * 0x00 .byte 0x66 510 * 0x01 leaq x@tlsgd(%rip), %rdi 511 * 0x08 .word 0x6666 512 * 0x0a rex64 513 * 0x0b call __tls_get_addr@plt 514 * 0x10 515 * To: 516 * 0x00 movq %fs:0, %rax 517 * 0x09 leaq x@tpoff(%rax), %rax 518 * 0x10 519 */ 520 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 521 R_AMD64_TPOFF32, arsp)); 522 arsp->rel_rtype = R_AMD64_TPOFF32; 523 arsp->rel_roffset += 8; 524 arsp->rel_raddend = 0; 525 526 /* 527 * Adjust 'offset' to beginning of instruction sequence. 528 */ 529 offset -= 4; 530 (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 531 return (FIX_RELOC); 532 533 case R_AMD64_GOTTPOFF: 534 /* 535 * IE -> LE 536 * 537 * Transition: 538 * 0x00 movq %fs:0, %rax 539 * 0x09 addq x@gottopoff(%rip), %rax 540 * 0x10 541 * To: 542 * 0x00 movq %fs:0, %rax 543 * 0x09 leaq x@tpoff(%rax), %rax 544 * 0x10 545 */ 546 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 547 R_AMD64_TPOFF32, arsp)); 548 arsp->rel_rtype = R_AMD64_TPOFF32; 549 arsp->rel_raddend = 0; 550 551 /* 552 * Adjust 'offset' to beginning of instruction sequence. 553 */ 554 offset -= 12; 555 556 /* 557 * Same code sequence used in the GD -> LE transition. 558 */ 559 (void) memcpy(offset, tlsinstr_gd_le, sizeof (tlsinstr_gd_le)); 560 return (FIX_RELOC); 561 562 case R_AMD64_TLSLD: 563 /* 564 * LD -> LE 565 * 566 * Transition 567 * 0x00 leaq x1@tlsgd(%rip), %rdi 568 * 0x07 call __tls_get_addr@plt 569 * 0x0c 570 * To: 571 * 0x00 .byte 0x66 572 * 0x01 .byte 0x66 573 * 0x02 .byte 0x66 574 * 0x03 movq %fs:0, %rax 575 */ 576 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 577 R_AMD64_NONE, arsp)); 578 offset -= 3; 579 (void) memcpy(offset, tlsinstr_ld_le, sizeof (tlsinstr_ld_le)); 580 return (FIX_DONE); 581 582 case R_AMD64_DTPOFF32: 583 /* 584 * LD->LE 585 * 586 * Transition: 587 * 0x00 leaq x1@dtpoff(%rax), %rcx 588 * To: 589 * 0x00 leaq x1@tpoff(%rax), %rcx 590 */ 591 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 592 R_AMD64_TPOFF32, arsp)); 593 arsp->rel_rtype = R_AMD64_TPOFF32; 594 arsp->rel_raddend = 0; 595 return (FIX_RELOC); 596 } 597 598 return (FIX_RELOC); 599 } 600 601 static uintptr_t 602 ld_do_activerelocs(Ofl_desc *ofl) 603 { 604 Rel_desc *arsp; 605 Rel_cache *rcp; 606 Listnode *lnp; 607 uintptr_t return_code = 1; 608 ofl_flag_t flags = ofl->ofl_flags; 609 610 if (ofl->ofl_actrels.head) 611 DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml)); 612 613 /* 614 * Process active relocations. 615 */ 616 for (LIST_TRAVERSE(&ofl->ofl_actrels, lnp, rcp)) { 617 /* LINTED */ 618 for (arsp = (Rel_desc *)(rcp + 1); 619 arsp < rcp->rc_free; arsp++) { 620 uchar_t *addr; 621 Xword value; 622 Sym_desc *sdp; 623 const char *ifl_name; 624 Xword refaddr; 625 int moved = 0; 626 Gotref gref; 627 628 /* 629 * If the section this relocation is against has been 630 * discarded (-zignore), then discard (skip) the 631 * relocation itself. 632 */ 633 if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) && 634 ((arsp->rel_flags & 635 (FLG_REL_GOT | FLG_REL_BSS | 636 FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) { 637 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, 638 M_MACH, arsp)); 639 continue; 640 } 641 642 /* 643 * We deteremine what the 'got reference' 644 * model (if required) is at this point. This 645 * needs to be done before tls_fixup() since 646 * it may 'transition' our instructions. 647 * 648 * The got table entries have already been assigned, 649 * and we bind to those initial entries. 650 */ 651 if (arsp->rel_flags & FLG_REL_DTLS) 652 gref = GOT_REF_TLSGD; 653 else if (arsp->rel_flags & FLG_REL_MTLS) 654 gref = GOT_REF_TLSLD; 655 else if (arsp->rel_flags & FLG_REL_STLS) 656 gref = GOT_REF_TLSIE; 657 else 658 gref = GOT_REF_GENERIC; 659 660 /* 661 * Perform any required TLS fixups. 662 */ 663 if (arsp->rel_flags & FLG_REL_TLSFIX) { 664 Fixupret ret; 665 666 if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR) 667 return (S_ERROR); 668 if (ret == FIX_DONE) 669 continue; 670 } 671 672 /* 673 * If this is a relocation against a move table, or 674 * expanded move table, adjust the relocation entries. 675 */ 676 if (arsp->rel_move) 677 ld_adj_movereloc(ofl, arsp); 678 679 sdp = arsp->rel_sym; 680 refaddr = arsp->rel_roffset + 681 (Off)_elf_getxoff(arsp->rel_isdesc->is_indata); 682 683 if ((arsp->rel_flags & FLG_REL_CLVAL) || 684 (arsp->rel_flags & FLG_REL_GOTCL)) 685 value = 0; 686 else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == 687 STT_SECTION) { 688 Sym_desc *sym; 689 690 /* 691 * The value for a symbol pointing to a SECTION 692 * is based off of that sections position. 693 * 694 * The second argument of the ld_am_I_partial() 695 * is the value stored at the target address 696 * relocation is going to be applied. 697 */ 698 if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 699 /* LINTED */ 700 (sym = ld_am_I_partial(arsp, *(Xword *) 701 ((uchar_t *) 702 arsp->rel_isdesc->is_indata->d_buf + 703 arsp->rel_roffset)))) { 704 /* 705 * If the symbol is moved, 706 * adjust the value 707 */ 708 value = sym->sd_sym->st_value; 709 moved = 1; 710 } else { 711 value = _elf_getxoff( 712 sdp->sd_isc->is_indata); 713 if (sdp->sd_isc->is_shdr->sh_flags & 714 SHF_ALLOC) 715 value += 716 sdp->sd_isc->is_osdesc-> 717 os_shdr->sh_addr; 718 } 719 if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS) 720 value -= ofl->ofl_tlsphdr->p_vaddr; 721 722 } else if (IS_SIZE(arsp->rel_rtype)) { 723 /* 724 * Size relocations require the symbols size. 725 */ 726 value = sdp->sd_sym->st_size; 727 } else { 728 /* 729 * Else the value is the symbols value. 730 */ 731 value = sdp->sd_sym->st_value; 732 } 733 734 /* 735 * Relocation against the GLOBAL_OFFSET_TABLE. 736 */ 737 if (arsp->rel_flags & FLG_REL_GOT) 738 arsp->rel_osdesc = ofl->ofl_osgot; 739 740 /* 741 * If loadable and not producing a relocatable object 742 * add the sections virtual address to the reference 743 * address. 744 */ 745 if ((arsp->rel_flags & FLG_REL_LOAD) && 746 ((flags & FLG_OF_RELOBJ) == 0)) 747 refaddr += arsp->rel_isdesc->is_osdesc-> 748 os_shdr->sh_addr; 749 750 /* 751 * If this entry has a PLT assigned to it, it's 752 * value is actually the address of the PLT (and 753 * not the address of the function). 754 */ 755 if (IS_PLT(arsp->rel_rtype)) { 756 if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 757 value = ld_calc_plt_addr(sdp, ofl); 758 } 759 760 /* 761 * Add relocations addend to value. Add extra 762 * relocation addend if needed. 763 * 764 * Note: for GOT relative relocations on amd64 765 * we discard the addend. It was relevant 766 * to the reference - not to the data item 767 * being referenced (ie: that -4 thing). 768 */ 769 if ((arsp->rel_flags & FLG_REL_GOT) == 0) 770 value += arsp->rel_raddend; 771 772 /* 773 * Determine whether the value needs further adjustment. 774 * Filter through the attributes of the relocation to 775 * determine what adjustment is required. Note, many 776 * of the following cases are only applicable when a 777 * .got is present. As a .got is not generated when a 778 * relocatable object is being built, any adjustments 779 * that require a .got need to be skipped. 780 */ 781 if ((arsp->rel_flags & FLG_REL_GOT) && 782 ((flags & FLG_OF_RELOBJ) == 0)) { 783 Xword R1addr; 784 uintptr_t R2addr; 785 Word gotndx; 786 Gotndx *gnp; 787 788 /* 789 * Perform relocation against GOT table. Since 790 * this doesn't fit exactly into a relocation 791 * we place the appropriate byte in the GOT 792 * directly 793 * 794 * Calculate offset into GOT at which to apply 795 * the relocation. 796 */ 797 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, 798 ofl, arsp); 799 assert(gnp); 800 801 if (arsp->rel_rtype == R_AMD64_DTPOFF64) 802 gotndx = gnp->gn_gotndx + 1; 803 else 804 gotndx = gnp->gn_gotndx; 805 806 R1addr = (Xword)(gotndx * M_GOT_ENTSIZE); 807 808 /* 809 * Add the GOTs data's offset. 810 */ 811 R2addr = R1addr + (uintptr_t) 812 arsp->rel_osdesc->os_outdata->d_buf; 813 814 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 815 ELF_DBG_LD, M_MACH, SHT_RELA, 816 arsp->rel_rtype, R1addr, value, 817 arsp->rel_sname, arsp->rel_osdesc)); 818 819 /* 820 * And do it. 821 */ 822 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 823 *(Xword *)R2addr = 824 ld_bswap_Xword(value); 825 else 826 *(Xword *)R2addr = value; 827 continue; 828 829 } else if (IS_GOT_BASED(arsp->rel_rtype) && 830 ((flags & FLG_OF_RELOBJ) == 0)) { 831 value -= ofl->ofl_osgot->os_shdr->sh_addr; 832 833 } else if (IS_GOTPCREL(arsp->rel_rtype) && 834 ((flags & FLG_OF_RELOBJ) == 0)) { 835 Gotndx *gnp; 836 837 /* 838 * Calculation: 839 * G + GOT + A - P 840 */ 841 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 842 gref, ofl, arsp); 843 assert(gnp); 844 value = (Xword)(ofl->ofl_osgot->os_shdr-> 845 sh_addr) + ((Xword)gnp->gn_gotndx * 846 M_GOT_ENTSIZE) + arsp->rel_raddend - 847 refaddr; 848 849 } else if (IS_GOT_PC(arsp->rel_rtype) && 850 ((flags & FLG_OF_RELOBJ) == 0)) { 851 value = (Xword)(ofl->ofl_osgot->os_shdr-> 852 sh_addr) - refaddr + arsp->rel_raddend; 853 854 } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) && 855 (((flags & FLG_OF_RELOBJ) == 0) || 856 (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) { 857 value -= refaddr; 858 859 } else if (IS_TLS_INS(arsp->rel_rtype) && 860 IS_GOT_RELATIVE(arsp->rel_rtype) && 861 ((flags & FLG_OF_RELOBJ) == 0)) { 862 Gotndx *gnp; 863 864 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, 865 ofl, arsp); 866 assert(gnp); 867 value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 868 869 } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 870 ((flags & FLG_OF_RELOBJ) == 0)) { 871 Gotndx *gnp; 872 873 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 874 gref, ofl, arsp); 875 assert(gnp); 876 value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 877 878 } else if ((arsp->rel_flags & FLG_REL_STLS) && 879 ((flags & FLG_OF_RELOBJ) == 0)) { 880 Xword tlsstatsize; 881 882 /* 883 * This is the LE TLS reference model. Static 884 * offset is hard-coded. 885 */ 886 tlsstatsize = 887 S_ROUND(ofl->ofl_tlsphdr->p_memsz, 888 M_TLSSTATALIGN); 889 value = tlsstatsize - value; 890 891 /* 892 * Since this code is fixed up, it assumes a 893 * negative offset that can be added to the 894 * thread pointer. 895 */ 896 if (arsp->rel_rtype == R_AMD64_TPOFF32) 897 value = -value; 898 } 899 900 if (arsp->rel_isdesc->is_file) 901 ifl_name = arsp->rel_isdesc->is_file->ifl_name; 902 else 903 ifl_name = MSG_INTL(MSG_STR_NULL); 904 905 /* 906 * Make sure we have data to relocate. Compiler and 907 * assembler developers have been known to generate 908 * relocations against invalid sections (normally .bss), 909 * so for their benefit give them sufficient information 910 * to help analyze the problem. End users should never 911 * see this. 912 */ 913 if (arsp->rel_isdesc->is_indata->d_buf == 0) { 914 Conv_inv_buf_t inv_buf; 915 916 eprintf(ofl->ofl_lml, ERR_FATAL, 917 MSG_INTL(MSG_REL_EMPTYSEC), 918 conv_reloc_amd64_type(arsp->rel_rtype, 919 0, &inv_buf), ifl_name, 920 demangle(arsp->rel_sname), 921 arsp->rel_isdesc->is_name); 922 return (S_ERROR); 923 } 924 925 /* 926 * Get the address of the data item we need to modify. 927 */ 928 addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 929 (uintptr_t)_elf_getxoff(arsp->rel_isdesc-> 930 is_indata)); 931 932 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD, 933 M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr), 934 value, arsp->rel_sname, arsp->rel_osdesc)); 935 addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 936 937 if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 938 ofl->ofl_size) || (arsp->rel_roffset > 939 arsp->rel_osdesc->os_shdr->sh_size)) { 940 int class; 941 Conv_inv_buf_t inv_buf; 942 943 if (((uintptr_t)addr - 944 (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 945 class = ERR_FATAL; 946 else 947 class = ERR_WARNING; 948 949 eprintf(ofl->ofl_lml, class, 950 MSG_INTL(MSG_REL_INVALOFFSET), 951 conv_reloc_amd64_type(arsp->rel_rtype, 952 0, &inv_buf), ifl_name, 953 arsp->rel_isdesc->is_name, 954 demangle(arsp->rel_sname), 955 EC_ADDR((uintptr_t)addr - 956 (uintptr_t)ofl->ofl_nehdr)); 957 958 if (class == ERR_FATAL) { 959 return_code = S_ERROR; 960 continue; 961 } 962 } 963 964 /* 965 * The relocation is additive. Ignore the previous 966 * symbol value if this local partial symbol is 967 * expanded. 968 */ 969 if (moved) 970 value -= *addr; 971 972 /* 973 * If '-z noreloc' is specified - skip the do_reloc_ld 974 * stage. 975 */ 976 if (OFL_DO_RELOC(ofl)) { 977 /* 978 * If this is a PROGBITS section and the 979 * running linker has a different byte order 980 * than the target host, tell do_reloc_ld() 981 * to swap bytes. 982 */ 983 if (do_reloc_ld((uchar_t)arsp->rel_rtype, 984 addr, &value, arsp->rel_sname, ifl_name, 985 OFL_SWAP_RELOC_DATA(ofl, arsp), 986 ofl->ofl_lml) == 0) 987 return_code = S_ERROR; 988 } 989 } 990 } 991 return (return_code); 992 } 993 994 static uintptr_t 995 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 996 { 997 Rel_desc *orsp; 998 Rel_cache *rcp; 999 Sym_desc *sdp = rsp->rel_sym; 1000 1001 /* 1002 * Static executables *do not* want any relocations against them. 1003 * Since our engine still creates relocations against a WEAK UNDEFINED 1004 * symbol in a static executable, it's best to disable them here 1005 * instead of through out the relocation code. 1006 */ 1007 if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) == 1008 (FLG_OF_STATIC | FLG_OF_EXEC)) 1009 return (1); 1010 1011 /* 1012 * If no relocation cache structures are available allocate 1013 * a new one and link it into the cache list. 1014 */ 1015 if ((ofl->ofl_outrels.tail == 0) || 1016 ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) || 1017 ((orsp = rcp->rc_free) == rcp->rc_end)) { 1018 static size_t nextsize = 0; 1019 size_t size; 1020 1021 /* 1022 * Output relocation numbers can vary considerably between 1023 * building executables or shared objects (pic vs. non-pic), 1024 * etc. But, they typically aren't very large, so for these 1025 * objects use a standard bucket size. For building relocatable 1026 * objects, typically there will be an output relocation for 1027 * every input relocation. 1028 */ 1029 if (nextsize == 0) { 1030 if (ofl->ofl_flags & FLG_OF_RELOBJ) { 1031 if ((size = ofl->ofl_relocincnt) == 0) 1032 size = REL_LOIDESCNO; 1033 if (size > REL_HOIDESCNO) 1034 nextsize = REL_HOIDESCNO; 1035 else 1036 nextsize = REL_LOIDESCNO; 1037 } else 1038 nextsize = size = REL_HOIDESCNO; 1039 } else 1040 size = nextsize; 1041 1042 size = size * sizeof (Rel_desc); 1043 1044 if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) || 1045 (list_appendc(&ofl->ofl_outrels, rcp) == 0)) 1046 return (S_ERROR); 1047 1048 /* LINTED */ 1049 rcp->rc_free = orsp = (Rel_desc *)(rcp + 1); 1050 /* LINTED */ 1051 rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size); 1052 } 1053 1054 /* 1055 * If we are adding a output relocation against a section 1056 * symbol (non-RELATIVE) then mark that section. These sections 1057 * will be added to the .dynsym symbol table. 1058 */ 1059 if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 1060 ((flags & FLG_REL_SCNNDX) || 1061 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 1062 1063 /* 1064 * If this is a COMMON symbol - no output section 1065 * exists yet - (it's created as part of sym_validate()). 1066 * So - we mark here that when it's created it should 1067 * be tagged with the FLG_OS_OUTREL flag. 1068 */ 1069 if ((sdp->sd_flags & FLG_SY_SPECSEC) && 1070 (sdp->sd_sym->st_shndx == SHN_COMMON)) { 1071 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 1072 ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 1073 else 1074 ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 1075 } else { 1076 Os_desc *osp = sdp->sd_isc->is_osdesc; 1077 1078 if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 1079 ofl->ofl_dynshdrcnt++; 1080 osp->os_flags |= FLG_OS_OUTREL; 1081 } 1082 } 1083 } 1084 1085 *orsp = *rsp; 1086 orsp->rel_flags |= flags; 1087 1088 rcp->rc_free++; 1089 ofl->ofl_outrelscnt++; 1090 1091 if (flags & FLG_REL_GOT) 1092 ofl->ofl_relocgotsz += (Xword)sizeof (Rela); 1093 else if (flags & FLG_REL_PLT) 1094 ofl->ofl_relocpltsz += (Xword)sizeof (Rela); 1095 else if (flags & FLG_REL_BSS) 1096 ofl->ofl_relocbsssz += (Xword)sizeof (Rela); 1097 else if (flags & FLG_REL_NOINFO) 1098 ofl->ofl_relocrelsz += (Xword)sizeof (Rela); 1099 else 1100 orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela); 1101 1102 if (orsp->rel_rtype == M_R_RELATIVE) 1103 ofl->ofl_relocrelcnt++; 1104 1105 /* 1106 * We don't perform sorting on PLT relocations because 1107 * they have already been assigned a PLT index and if we 1108 * were to sort them we would have to re-assign the plt indexes. 1109 */ 1110 if (!(flags & FLG_REL_PLT)) 1111 ofl->ofl_reloccnt++; 1112 1113 /* 1114 * Insure a GLOBAL_OFFSET_TABLE is generated if required. 1115 */ 1116 if (IS_GOT_REQUIRED(orsp->rel_rtype)) 1117 ofl->ofl_flags |= FLG_OF_BLDGOT; 1118 1119 /* 1120 * Identify and possibly warn of a displacement relocation. 1121 */ 1122 if (orsp->rel_flags & FLG_REL_DISP) { 1123 ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 1124 1125 if (ofl->ofl_flags & FLG_OF_VERBOSE) 1126 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 1127 } 1128 DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA, 1129 M_MACH, orsp)); 1130 return (1); 1131 } 1132 1133 /* 1134 * process relocation for a LOCAL symbol 1135 */ 1136 static uintptr_t 1137 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl) 1138 { 1139 ofl_flag_t flags = ofl->ofl_flags; 1140 Sym_desc *sdp = rsp->rel_sym; 1141 Word shndx = sdp->sd_sym->st_shndx; 1142 Word ortype = rsp->rel_rtype; 1143 1144 /* 1145 * if ((shared object) and (not pc relative relocation) and 1146 * (not against ABS symbol)) 1147 * then 1148 * build R_AMD64_RELATIVE 1149 * fi 1150 */ 1151 if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 1152 !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 1153 !(IS_GOT_BASED(rsp->rel_rtype)) && 1154 !(rsp->rel_isdesc != NULL && 1155 (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 1156 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 1157 (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 1158 1159 /* 1160 * R_AMD64_RELATIVE updates a 64bit address, if this 1161 * relocation isn't a 64bit binding then we can not 1162 * simplify it to a RELATIVE relocation. 1163 */ 1164 if (reloc_table[ortype].re_fsize != sizeof (Addr)) { 1165 return (ld_add_outrel(0, rsp, ofl)); 1166 } 1167 1168 rsp->rel_rtype = R_AMD64_RELATIVE; 1169 if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR) 1170 return (S_ERROR); 1171 rsp->rel_rtype = ortype; 1172 return (1); 1173 } 1174 1175 /* 1176 * If the relocation is against a 'non-allocatable' section 1177 * and we can not resolve it now - then give a warning 1178 * message. 1179 * 1180 * We can not resolve the symbol if either: 1181 * a) it's undefined 1182 * b) it's defined in a shared library and a 1183 * COPY relocation hasn't moved it to the executable 1184 * 1185 * Note: because we process all of the relocations against the 1186 * text segment before any others - we know whether 1187 * or not a copy relocation will be generated before 1188 * we get here (see reloc_init()->reloc_segments()). 1189 */ 1190 if (!(rsp->rel_flags & FLG_REL_LOAD) && 1191 ((shndx == SHN_UNDEF) || 1192 ((sdp->sd_ref == REF_DYN_NEED) && 1193 ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 1194 Conv_inv_buf_t inv_buf; 1195 1196 /* 1197 * If the relocation is against a SHT_SUNW_ANNOTATE 1198 * section - then silently ignore that the relocation 1199 * can not be resolved. 1200 */ 1201 if (rsp->rel_osdesc && 1202 (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 1203 return (0); 1204 (void) eprintf(ofl->ofl_lml, ERR_WARNING, 1205 MSG_INTL(MSG_REL_EXTERNSYM), 1206 conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf), 1207 rsp->rel_isdesc->is_file->ifl_name, 1208 demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 1209 return (1); 1210 } 1211 1212 /* 1213 * Perform relocation. 1214 */ 1215 return (ld_add_actrel(NULL, rsp, ofl)); 1216 } 1217 1218 1219 static uintptr_t 1220 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 1221 { 1222 Word rtype = rsp->rel_rtype; 1223 Sym_desc *sdp = rsp->rel_sym; 1224 ofl_flag_t flags = ofl->ofl_flags; 1225 Gotndx *gnp; 1226 1227 /* 1228 * If we're building an executable - use either the IE or LE access 1229 * model. If we're building a shared object process any IE model. 1230 */ 1231 if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 1232 /* 1233 * Set the DF_STATIC_TLS flag. 1234 */ 1235 ofl->ofl_dtflags |= DF_STATIC_TLS; 1236 1237 if (!local || ((flags & FLG_OF_EXEC) == 0)) { 1238 /* 1239 * Assign a GOT entry for static TLS references. 1240 */ 1241 if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 1242 GOT_REF_TLSIE, ofl, rsp)) == 0) { 1243 1244 if (ld_assign_got_TLS(local, rsp, ofl, sdp, 1245 gnp, GOT_REF_TLSIE, FLG_REL_STLS, 1246 rtype, R_AMD64_TPOFF64, 0) == S_ERROR) 1247 return (S_ERROR); 1248 } 1249 1250 /* 1251 * IE access model. 1252 */ 1253 if (IS_TLS_IE(rtype)) 1254 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1255 1256 /* 1257 * Fixups are required for other executable models. 1258 */ 1259 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1260 rsp, ofl)); 1261 } 1262 1263 /* 1264 * LE access model. 1265 */ 1266 if (IS_TLS_LE(rtype)) 1267 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1268 1269 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1270 rsp, ofl)); 1271 } 1272 1273 /* 1274 * Building a shared object. 1275 * 1276 * Assign a GOT entry for a dynamic TLS reference. 1277 */ 1278 if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 1279 GOT_REF_TLSLD, ofl, rsp)) == 0)) { 1280 1281 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 1282 FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, 0) == S_ERROR) 1283 return (S_ERROR); 1284 1285 } else if (IS_TLS_GD(rtype) && 1286 ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD, 1287 ofl, rsp)) == 0)) { 1288 1289 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 1290 FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64, 1291 R_AMD64_DTPOFF64) == S_ERROR) 1292 return (S_ERROR); 1293 } 1294 1295 if (IS_TLS_LD(rtype)) 1296 return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 1297 1298 return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 1299 } 1300 1301 /* ARGSUSED3 */ 1302 static Gotndx * 1303 ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc) 1304 { 1305 Listnode * lnp; 1306 Gotndx * gnp; 1307 1308 assert(rdesc != 0); 1309 1310 if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx) 1311 return (ofl->ofl_tlsldgotndx); 1312 1313 for (LIST_TRAVERSE(lst, lnp, gnp)) { 1314 if ((rdesc->rel_raddend == gnp->gn_addend) && 1315 (gnp->gn_gotref == gref)) { 1316 return (gnp); 1317 } 1318 } 1319 return ((Gotndx *)0); 1320 } 1321 1322 static Xword 1323 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl) 1324 { 1325 Os_desc *osp = ofl->ofl_osgot; 1326 Sym_desc *sdp = rdesc->rel_sym; 1327 Xword gotndx; 1328 Gotref gref; 1329 Gotndx *gnp; 1330 1331 if (rdesc->rel_flags & FLG_REL_DTLS) 1332 gref = GOT_REF_TLSGD; 1333 else if (rdesc->rel_flags & FLG_REL_MTLS) 1334 gref = GOT_REF_TLSLD; 1335 else if (rdesc->rel_flags & FLG_REL_STLS) 1336 gref = GOT_REF_TLSIE; 1337 else 1338 gref = GOT_REF_GENERIC; 1339 1340 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc); 1341 assert(gnp); 1342 1343 gotndx = (Xword)gnp->gn_gotndx; 1344 1345 if ((rdesc->rel_flags & FLG_REL_DTLS) && 1346 (rdesc->rel_rtype == R_AMD64_DTPOFF64)) 1347 gotndx++; 1348 1349 return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE))); 1350 } 1351 1352 1353 /* ARGSUSED5 */ 1354 static uintptr_t 1355 ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl, 1356 Rel_desc * rsp, Sym_desc * sdp) 1357 { 1358 Xword raddend; 1359 Gotndx *gnp, *_gnp; 1360 Listnode *lnp, *plnp; 1361 uint_t gotents; 1362 1363 raddend = rsp->rel_raddend; 1364 if (pgnp && (pgnp->gn_addend == raddend) && 1365 (pgnp->gn_gotref == gref)) 1366 return (1); 1367 1368 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 1369 gotents = 2; 1370 else 1371 gotents = 1; 1372 1373 plnp = 0; 1374 for (LIST_TRAVERSE(lst, lnp, _gnp)) { 1375 if (_gnp->gn_addend > raddend) 1376 break; 1377 plnp = lnp; 1378 } 1379 1380 /* 1381 * Allocate a new entry. 1382 */ 1383 if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0) 1384 return (S_ERROR); 1385 gnp->gn_addend = raddend; 1386 gnp->gn_gotndx = ofl->ofl_gotcnt; 1387 gnp->gn_gotref = gref; 1388 1389 ofl->ofl_gotcnt += gotents; 1390 1391 if (gref == GOT_REF_TLSLD) { 1392 ofl->ofl_tlsldgotndx = gnp; 1393 return (1); 1394 } 1395 1396 if (plnp == 0) { 1397 /* 1398 * Insert at head of list 1399 */ 1400 if (list_prependc(lst, (void *)gnp) == 0) 1401 return (S_ERROR); 1402 } else if (_gnp->gn_addend > raddend) { 1403 /* 1404 * Insert in middle of lest 1405 */ 1406 if (list_insertc(lst, (void *)gnp, plnp) == 0) 1407 return (S_ERROR); 1408 } else { 1409 /* 1410 * Append to tail of list 1411 */ 1412 if (list_appendc(lst, (void *)gnp) == 0) 1413 return (S_ERROR); 1414 } 1415 return (1); 1416 } 1417 1418 static void 1419 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 1420 { 1421 sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 1422 sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++; 1423 ofl->ofl_flags |= FLG_OF_BLDGOT; 1424 } 1425 1426 static uchar_t plt0_template[M_PLT_ENTSIZE] = { 1427 /* 0x00 PUSHQ GOT+8(%rip) */ 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, 1428 /* 0x06 JMP *GOT+16(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 1429 /* 0x0c NOP */ 0x90, 1430 /* 0x0d NOP */ 0x90, 1431 /* 0x0e NOP */ 0x90, 1432 /* 0x0f NOP */ 0x90 1433 }; 1434 1435 /* 1436 * Initializes .got[0] with the _DYNAMIC symbol value. 1437 */ 1438 static uintptr_t 1439 ld_fillin_gotplt(Ofl_desc *ofl) 1440 { 1441 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1442 1443 if (ofl->ofl_osgot) { 1444 Sym_desc *sdp; 1445 1446 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 1447 SYM_NOHASH, 0, ofl)) != NULL) { 1448 uchar_t *genptr; 1449 1450 genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 1451 (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 1452 /* LINTED */ 1453 *(Xword *)genptr = sdp->sd_sym->st_value; 1454 if (bswap) 1455 /* LINTED */ 1456 *(Xword *)genptr = 1457 /* LINTED */ 1458 ld_bswap_Xword(*(Xword *)genptr); 1459 } 1460 } 1461 1462 /* 1463 * Fill in the reserved slot in the procedure linkage table the first 1464 * entry is: 1465 * 0x00 PUSHQ GOT+8(%rip) # GOT[1] 1466 * 0x06 JMP *GOT+16(%rip) # GOT[2] 1467 * 0x0c NOP 1468 * 0x0d NOP 1469 * 0x0e NOP 1470 * 0x0f NOP 1471 */ 1472 if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) { 1473 uchar_t *pltent; 1474 Xword val1; 1475 1476 pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 1477 bcopy(plt0_template, pltent, sizeof (plt0_template)); 1478 1479 /* 1480 * If '-z noreloc' is specified - skip the do_reloc_ld 1481 * stage. 1482 */ 1483 if (!OFL_DO_RELOC(ofl)) 1484 return (1); 1485 1486 /* 1487 * filin: 1488 * PUSHQ GOT + 8(%rip) 1489 * 1490 * Note: 0x06 below represents the offset to the 1491 * next instruction - which is what %rip will 1492 * be pointing at. 1493 */ 1494 val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 1495 (M_GOT_XLINKMAP * M_GOT_ENTSIZE) - 1496 ofl->ofl_osplt->os_shdr->sh_addr - 0x06; 1497 1498 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 1499 &val1, MSG_ORIG(MSG_SYM_PLTENT), 1500 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 1501 eprintf(ofl->ofl_lml, ERR_FATAL, 1502 MSG_INTL(MSG_PLT_PLT0FAIL)); 1503 return (S_ERROR); 1504 } 1505 1506 /* 1507 * filin: 1508 * JMP *GOT+16(%rip) 1509 */ 1510 val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 1511 (M_GOT_XRTLD * M_GOT_ENTSIZE) - 1512 ofl->ofl_osplt->os_shdr->sh_addr - 0x0c; 1513 1514 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x08], 1515 &val1, MSG_ORIG(MSG_SYM_PLTENT), 1516 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 1517 eprintf(ofl->ofl_lml, ERR_FATAL, 1518 MSG_INTL(MSG_PLT_PLT0FAIL)); 1519 return (S_ERROR); 1520 } 1521 } 1522 1523 return (1); 1524 } 1525 1526 1527 1528 /* 1529 * Template for generating "void (*)(void)" function 1530 */ 1531 static const uchar_t nullfunc_tmpl[] = { /* amd64 */ 1532 /* 0x00 */ 0x55, /* pushq %rbp */ 1533 /* 0x01 */ 0x48, 0x8b, 0xec, /* movq %rsp,%rbp */ 1534 /* 0x04 */ 0x48, 0x8b, 0xe5, /* movq %rbp,%rsp */ 1535 /* 0x07 */ 0x5d, /* popq %rbp */ 1536 /* 0x08 */ 0xc3 /* ret */ 1537 }; 1538 1539 1540 /* 1541 * Return the ld_targ definition for this target. 1542 */ 1543 const Target * 1544 ld_targ_init_x86(void) 1545 { 1546 static const Target _ld_targ = { 1547 { /* Target_mach */ 1548 M_MACH, /* m_mach */ 1549 M_MACHPLUS, /* m_machplus */ 1550 M_FLAGSPLUS, /* m_flagsplus */ 1551 M_CLASS, /* m_class */ 1552 M_DATA, /* m_data */ 1553 1554 M_SEGM_ALIGN, /* m_segm_align */ 1555 M_SEGM_ORIGIN, /* m_segm_origin */ 1556 M_DATASEG_PERM, /* m_dataseg_perm */ 1557 M_WORD_ALIGN, /* m_word_align */ 1558 MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */ 1559 1560 /* Relocation type codes */ 1561 M_R_ARRAYADDR, /* m_r_arrayaddr */ 1562 M_R_COPY, /* m_r_copy */ 1563 M_R_GLOB_DAT, /* m_r_glob_dat */ 1564 M_R_JMP_SLOT, /* m_r_jmp_slot */ 1565 M_R_NUM, /* m_r_num */ 1566 M_R_NONE, /* m_r_none */ 1567 M_R_RELATIVE, /* m_r_relative */ 1568 M_R_REGISTER, /* m_r_register */ 1569 1570 /* Relocation related constants */ 1571 M_REL_DT_COUNT, /* m_rel_dt_count */ 1572 M_REL_DT_ENT, /* m_rel_dt_ent */ 1573 M_REL_DT_SIZE, /* m_rel_dt_size */ 1574 M_REL_DT_TYPE, /* m_rel_dt_type */ 1575 M_REL_SHT_TYPE, /* m_rel_sht_type */ 1576 1577 /* GOT related constants */ 1578 M_GOT_ENTSIZE, /* m_got_entsize */ 1579 M_GOT_XNumber, /* m_got_xnumber */ 1580 1581 /* PLT related constants */ 1582 M_PLT_ALIGN, /* m_plt_align */ 1583 M_PLT_ENTSIZE, /* m_plt_entsize */ 1584 M_PLT_RESERVSZ, /* m_plt_reservsz */ 1585 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */ 1586 1587 M_DT_REGISTER, /* m_dt_register */ 1588 }, 1589 { /* Target_machid */ 1590 M_ID_ARRAY, /* id_array */ 1591 M_ID_BSS, /* id_bss */ 1592 M_ID_CAP, /* id_cap */ 1593 M_ID_DATA, /* id_data */ 1594 M_ID_DYNAMIC, /* id_dynamic */ 1595 M_ID_DYNSORT, /* id_dynsort */ 1596 M_ID_DYNSTR, /* id_dynstr */ 1597 M_ID_DYNSYM, /* id_dynsym */ 1598 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */ 1599 M_ID_GOT, /* id_got */ 1600 M_ID_UNKNOWN, /* id_gotdata (unused) */ 1601 M_ID_HASH, /* id_hash */ 1602 M_ID_INTERP, /* id_interp */ 1603 M_ID_LBSS, /* id_lbss */ 1604 M_ID_LDYNSYM, /* id_ldynsym */ 1605 M_ID_NOTE, /* id_note */ 1606 M_ID_NULL, /* id_null */ 1607 M_ID_PLT, /* id_plt */ 1608 M_ID_REL, /* id_rel */ 1609 M_ID_STRTAB, /* id_strtab */ 1610 M_ID_SYMINFO, /* id_syminfo */ 1611 M_ID_SYMTAB, /* id_symtab */ 1612 M_ID_SYMTAB_NDX, /* id_symtab_ndx */ 1613 M_ID_TEXT, /* id_text */ 1614 M_ID_TLS, /* id_tls */ 1615 M_ID_TLSBSS, /* id_tlsbss */ 1616 M_ID_UNKNOWN, /* id_unknown */ 1617 M_ID_UNWIND, /* id_unwind */ 1618 M_ID_USER, /* id_user */ 1619 M_ID_VERSION, /* id_version */ 1620 }, 1621 { /* Target_nullfunc */ 1622 nullfunc_tmpl, /* nf_template */ 1623 sizeof (nullfunc_tmpl), /* nf_size */ 1624 }, 1625 { /* Target_machrel */ 1626 reloc_table, 1627 1628 ld_init_rel, /* mr_init_rel */ 1629 ld_mach_eflags, /* mr_mach_eflags */ 1630 ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 1631 ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 1632 ld_calc_plt_addr, /* mr_calc_plt_addr */ 1633 ld_perform_outreloc, /* mr_perform_outreloc */ 1634 ld_do_activerelocs, /* mr_do_activerelocs */ 1635 ld_add_outrel, /* mr_add_outrel */ 1636 NULL, /* mr_reloc_register */ 1637 ld_reloc_local, /* mr_reloc_local */ 1638 NULL, /* mr_reloc_GOTOP */ 1639 ld_reloc_TLS, /* mr_reloc_TLS */ 1640 NULL, /* mr_assign_got */ 1641 ld_find_gotndx, /* mr_find_gotndx */ 1642 ld_calc_got_offset, /* mr_calc_got_offset */ 1643 ld_assign_got_ndx, /* mr_assign_got_ndx */ 1644 ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 1645 NULL, /* mr_allocate_got */ 1646 ld_fillin_gotplt, /* mr_fillin_gotplt */ 1647 }, 1648 { /* Target_machsym */ 1649 NULL, /* ms_reg_check */ 1650 NULL, /* ms_mach_sym_typecheck */ 1651 NULL, /* ms_is_regsym */ 1652 NULL, /* ms_reg_find */ 1653 NULL /* ms_reg_enter */ 1654 }, 1655 { /* Target_unwind */ 1656 make_amd64_unwindhdr, /* uw_make_unwindhdr */ 1657 populate_amd64_unwindhdr, /* uw_populate_unwindhdr */ 1658 append_amd64_unwind, /* uw_append_unwind */ 1659 } 1660 }; 1661 1662 return (&_ld_targ); 1663 } 1664