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 2009 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 determine 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 if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 695 /* LINTED */ 696 (sym = ld_am_I_partial(arsp, 697 arsp->rel_raddend))) { 698 /* 699 * The symbol was moved, so adjust 700 * the value relative to the new 701 * section. 702 */ 703 value = sym->sd_sym->st_value; 704 moved = 1; 705 706 /* 707 * The original raddend covers the 708 * displacement from the section start 709 * to the desired address. The value 710 * computed above gets us from the 711 * section start to the start of the 712 * symbol range. Adjust the old raddend 713 * to remove the offset from section 714 * start to symbol start, leaving the 715 * displacement within the range of 716 * the symbol. 717 */ 718 arsp->rel_raddend -= 719 sym->sd_osym->st_value; 720 } else { 721 value = _elf_getxoff( 722 sdp->sd_isc->is_indata); 723 if (sdp->sd_isc->is_shdr->sh_flags & 724 SHF_ALLOC) 725 value += 726 sdp->sd_isc->is_osdesc-> 727 os_shdr->sh_addr; 728 } 729 if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS) 730 value -= ofl->ofl_tlsphdr->p_vaddr; 731 732 } else if (IS_SIZE(arsp->rel_rtype)) { 733 /* 734 * Size relocations require the symbols size. 735 */ 736 value = sdp->sd_sym->st_size; 737 } else { 738 /* 739 * Else the value is the symbols value. 740 */ 741 value = sdp->sd_sym->st_value; 742 } 743 744 /* 745 * Relocation against the GLOBAL_OFFSET_TABLE. 746 */ 747 if (arsp->rel_flags & FLG_REL_GOT) 748 arsp->rel_osdesc = ofl->ofl_osgot; 749 750 /* 751 * If loadable and not producing a relocatable object 752 * add the sections virtual address to the reference 753 * address. 754 */ 755 if ((arsp->rel_flags & FLG_REL_LOAD) && 756 ((flags & FLG_OF_RELOBJ) == 0)) 757 refaddr += arsp->rel_isdesc->is_osdesc-> 758 os_shdr->sh_addr; 759 760 /* 761 * If this entry has a PLT assigned to it, it's 762 * value is actually the address of the PLT (and 763 * not the address of the function). 764 */ 765 if (IS_PLT(arsp->rel_rtype)) { 766 if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 767 value = ld_calc_plt_addr(sdp, ofl); 768 } 769 770 /* 771 * Add relocations addend to value. Add extra 772 * relocation addend if needed. 773 * 774 * Note: for GOT relative relocations on amd64 775 * we discard the addend. It was relevant 776 * to the reference - not to the data item 777 * being referenced (ie: that -4 thing). 778 */ 779 if ((arsp->rel_flags & FLG_REL_GOT) == 0) 780 value += arsp->rel_raddend; 781 782 /* 783 * Determine whether the value needs further adjustment. 784 * Filter through the attributes of the relocation to 785 * determine what adjustment is required. Note, many 786 * of the following cases are only applicable when a 787 * .got is present. As a .got is not generated when a 788 * relocatable object is being built, any adjustments 789 * that require a .got need to be skipped. 790 */ 791 if ((arsp->rel_flags & FLG_REL_GOT) && 792 ((flags & FLG_OF_RELOBJ) == 0)) { 793 Xword R1addr; 794 uintptr_t R2addr; 795 Word gotndx; 796 Gotndx *gnp; 797 798 /* 799 * Perform relocation against GOT table. Since 800 * this doesn't fit exactly into a relocation 801 * we place the appropriate byte in the GOT 802 * directly 803 * 804 * Calculate offset into GOT at which to apply 805 * the relocation. 806 */ 807 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, 808 ofl, arsp); 809 assert(gnp); 810 811 if (arsp->rel_rtype == R_AMD64_DTPOFF64) 812 gotndx = gnp->gn_gotndx + 1; 813 else 814 gotndx = gnp->gn_gotndx; 815 816 R1addr = (Xword)(gotndx * M_GOT_ENTSIZE); 817 818 /* 819 * Add the GOTs data's offset. 820 */ 821 R2addr = R1addr + (uintptr_t) 822 arsp->rel_osdesc->os_outdata->d_buf; 823 824 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 825 ELF_DBG_LD, M_MACH, SHT_RELA, 826 arsp->rel_rtype, R1addr, value, 827 arsp->rel_sname, arsp->rel_osdesc)); 828 829 /* 830 * And do it. 831 */ 832 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 833 *(Xword *)R2addr = 834 ld_bswap_Xword(value); 835 else 836 *(Xword *)R2addr = value; 837 continue; 838 839 } else if (IS_GOT_BASED(arsp->rel_rtype) && 840 ((flags & FLG_OF_RELOBJ) == 0)) { 841 value -= ofl->ofl_osgot->os_shdr->sh_addr; 842 843 } else if (IS_GOTPCREL(arsp->rel_rtype) && 844 ((flags & FLG_OF_RELOBJ) == 0)) { 845 Gotndx *gnp; 846 847 /* 848 * Calculation: 849 * G + GOT + A - P 850 */ 851 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 852 gref, ofl, arsp); 853 assert(gnp); 854 value = (Xword)(ofl->ofl_osgot->os_shdr-> 855 sh_addr) + ((Xword)gnp->gn_gotndx * 856 M_GOT_ENTSIZE) + arsp->rel_raddend - 857 refaddr; 858 859 } else if (IS_GOT_PC(arsp->rel_rtype) && 860 ((flags & FLG_OF_RELOBJ) == 0)) { 861 value = (Xword)(ofl->ofl_osgot->os_shdr-> 862 sh_addr) - refaddr + arsp->rel_raddend; 863 864 } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) && 865 (((flags & FLG_OF_RELOBJ) == 0) || 866 (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) { 867 value -= refaddr; 868 869 } else if (IS_TLS_INS(arsp->rel_rtype) && 870 IS_GOT_RELATIVE(arsp->rel_rtype) && 871 ((flags & FLG_OF_RELOBJ) == 0)) { 872 Gotndx *gnp; 873 874 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, 875 ofl, arsp); 876 assert(gnp); 877 value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 878 879 } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 880 ((flags & FLG_OF_RELOBJ) == 0)) { 881 Gotndx *gnp; 882 883 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 884 gref, ofl, arsp); 885 assert(gnp); 886 value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 887 888 } else if ((arsp->rel_flags & FLG_REL_STLS) && 889 ((flags & FLG_OF_RELOBJ) == 0)) { 890 Xword tlsstatsize; 891 892 /* 893 * This is the LE TLS reference model. Static 894 * offset is hard-coded. 895 */ 896 tlsstatsize = 897 S_ROUND(ofl->ofl_tlsphdr->p_memsz, 898 M_TLSSTATALIGN); 899 value = tlsstatsize - value; 900 901 /* 902 * Since this code is fixed up, it assumes a 903 * negative offset that can be added to the 904 * thread pointer. 905 */ 906 if (arsp->rel_rtype == R_AMD64_TPOFF32) 907 value = -value; 908 } 909 910 if (arsp->rel_isdesc->is_file) 911 ifl_name = arsp->rel_isdesc->is_file->ifl_name; 912 else 913 ifl_name = MSG_INTL(MSG_STR_NULL); 914 915 /* 916 * Make sure we have data to relocate. Compiler and 917 * assembler developers have been known to generate 918 * relocations against invalid sections (normally .bss), 919 * so for their benefit give them sufficient information 920 * to help analyze the problem. End users should never 921 * see this. 922 */ 923 if (arsp->rel_isdesc->is_indata->d_buf == 0) { 924 Conv_inv_buf_t inv_buf; 925 926 eprintf(ofl->ofl_lml, ERR_FATAL, 927 MSG_INTL(MSG_REL_EMPTYSEC), 928 conv_reloc_amd64_type(arsp->rel_rtype, 929 0, &inv_buf), ifl_name, 930 demangle(arsp->rel_sname), 931 arsp->rel_isdesc->is_name); 932 return (S_ERROR); 933 } 934 935 /* 936 * Get the address of the data item we need to modify. 937 */ 938 addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 939 (uintptr_t)_elf_getxoff(arsp->rel_isdesc-> 940 is_indata)); 941 942 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD, 943 M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr), 944 value, arsp->rel_sname, arsp->rel_osdesc)); 945 addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 946 947 if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 948 ofl->ofl_size) || (arsp->rel_roffset > 949 arsp->rel_osdesc->os_shdr->sh_size)) { 950 int class; 951 Conv_inv_buf_t inv_buf; 952 953 if (((uintptr_t)addr - 954 (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 955 class = ERR_FATAL; 956 else 957 class = ERR_WARNING; 958 959 eprintf(ofl->ofl_lml, class, 960 MSG_INTL(MSG_REL_INVALOFFSET), 961 conv_reloc_amd64_type(arsp->rel_rtype, 962 0, &inv_buf), ifl_name, 963 arsp->rel_isdesc->is_name, 964 demangle(arsp->rel_sname), 965 EC_ADDR((uintptr_t)addr - 966 (uintptr_t)ofl->ofl_nehdr)); 967 968 if (class == ERR_FATAL) { 969 return_code = S_ERROR; 970 continue; 971 } 972 } 973 974 /* 975 * The relocation is additive. Ignore the previous 976 * symbol value if this local partial symbol is 977 * expanded. 978 */ 979 if (moved) 980 value -= *addr; 981 982 /* 983 * If '-z noreloc' is specified - skip the do_reloc_ld 984 * stage. 985 */ 986 if (OFL_DO_RELOC(ofl)) { 987 /* 988 * If this is a PROGBITS section and the 989 * running linker has a different byte order 990 * than the target host, tell do_reloc_ld() 991 * to swap bytes. 992 */ 993 if (do_reloc_ld((uchar_t)arsp->rel_rtype, 994 addr, &value, arsp->rel_sname, ifl_name, 995 OFL_SWAP_RELOC_DATA(ofl, arsp), 996 ofl->ofl_lml) == 0) 997 return_code = S_ERROR; 998 } 999 } 1000 } 1001 return (return_code); 1002 } 1003 1004 static uintptr_t 1005 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 1006 { 1007 Rel_desc *orsp; 1008 Rel_cache *rcp; 1009 Sym_desc *sdp = rsp->rel_sym; 1010 1011 /* 1012 * Static executables *do not* want any relocations against them. 1013 * Since our engine still creates relocations against a WEAK UNDEFINED 1014 * symbol in a static executable, it's best to disable them here 1015 * instead of through out the relocation code. 1016 */ 1017 if ((ofl->ofl_flags & (FLG_OF_STATIC | FLG_OF_EXEC)) == 1018 (FLG_OF_STATIC | FLG_OF_EXEC)) 1019 return (1); 1020 1021 /* 1022 * If no relocation cache structures are available allocate 1023 * a new one and link it into the cache list. 1024 */ 1025 if ((ofl->ofl_outrels.tail == 0) || 1026 ((rcp = (Rel_cache *)ofl->ofl_outrels.tail->data) == 0) || 1027 ((orsp = rcp->rc_free) == rcp->rc_end)) { 1028 static size_t nextsize = 0; 1029 size_t size; 1030 1031 /* 1032 * Output relocation numbers can vary considerably between 1033 * building executables or shared objects (pic vs. non-pic), 1034 * etc. But, they typically aren't very large, so for these 1035 * objects use a standard bucket size. For building relocatable 1036 * objects, typically there will be an output relocation for 1037 * every input relocation. 1038 */ 1039 if (nextsize == 0) { 1040 if (ofl->ofl_flags & FLG_OF_RELOBJ) { 1041 if ((size = ofl->ofl_relocincnt) == 0) 1042 size = REL_LOIDESCNO; 1043 if (size > REL_HOIDESCNO) 1044 nextsize = REL_HOIDESCNO; 1045 else 1046 nextsize = REL_LOIDESCNO; 1047 } else 1048 nextsize = size = REL_HOIDESCNO; 1049 } else 1050 size = nextsize; 1051 1052 size = size * sizeof (Rel_desc); 1053 1054 if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) || 1055 (list_appendc(&ofl->ofl_outrels, rcp) == 0)) 1056 return (S_ERROR); 1057 1058 /* LINTED */ 1059 rcp->rc_free = orsp = (Rel_desc *)(rcp + 1); 1060 /* LINTED */ 1061 rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size); 1062 } 1063 1064 /* 1065 * If we are adding a output relocation against a section 1066 * symbol (non-RELATIVE) then mark that section. These sections 1067 * will be added to the .dynsym symbol table. 1068 */ 1069 if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 1070 ((flags & FLG_REL_SCNNDX) || 1071 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 1072 1073 /* 1074 * If this is a COMMON symbol - no output section 1075 * exists yet - (it's created as part of sym_validate()). 1076 * So - we mark here that when it's created it should 1077 * be tagged with the FLG_OS_OUTREL flag. 1078 */ 1079 if ((sdp->sd_flags & FLG_SY_SPECSEC) && 1080 (sdp->sd_sym->st_shndx == SHN_COMMON)) { 1081 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 1082 ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 1083 else 1084 ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 1085 } else { 1086 Os_desc *osp = sdp->sd_isc->is_osdesc; 1087 1088 if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 1089 ofl->ofl_dynshdrcnt++; 1090 osp->os_flags |= FLG_OS_OUTREL; 1091 } 1092 } 1093 } 1094 1095 *orsp = *rsp; 1096 orsp->rel_flags |= flags; 1097 1098 rcp->rc_free++; 1099 ofl->ofl_outrelscnt++; 1100 1101 if (flags & FLG_REL_GOT) 1102 ofl->ofl_relocgotsz += (Xword)sizeof (Rela); 1103 else if (flags & FLG_REL_PLT) 1104 ofl->ofl_relocpltsz += (Xword)sizeof (Rela); 1105 else if (flags & FLG_REL_BSS) 1106 ofl->ofl_relocbsssz += (Xword)sizeof (Rela); 1107 else if (flags & FLG_REL_NOINFO) 1108 ofl->ofl_relocrelsz += (Xword)sizeof (Rela); 1109 else 1110 orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela); 1111 1112 if (orsp->rel_rtype == M_R_RELATIVE) 1113 ofl->ofl_relocrelcnt++; 1114 1115 /* 1116 * We don't perform sorting on PLT relocations because 1117 * they have already been assigned a PLT index and if we 1118 * were to sort them we would have to re-assign the plt indexes. 1119 */ 1120 if (!(flags & FLG_REL_PLT)) 1121 ofl->ofl_reloccnt++; 1122 1123 /* 1124 * Insure a GLOBAL_OFFSET_TABLE is generated if required. 1125 */ 1126 if (IS_GOT_REQUIRED(orsp->rel_rtype)) 1127 ofl->ofl_flags |= FLG_OF_BLDGOT; 1128 1129 /* 1130 * Identify and possibly warn of a displacement relocation. 1131 */ 1132 if (orsp->rel_flags & FLG_REL_DISP) { 1133 ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 1134 1135 if (ofl->ofl_flags & FLG_OF_VERBOSE) 1136 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 1137 } 1138 DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA, 1139 M_MACH, orsp)); 1140 return (1); 1141 } 1142 1143 /* 1144 * process relocation for a LOCAL symbol 1145 */ 1146 static uintptr_t 1147 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl) 1148 { 1149 ofl_flag_t flags = ofl->ofl_flags; 1150 Sym_desc *sdp = rsp->rel_sym; 1151 Word shndx = sdp->sd_sym->st_shndx; 1152 Word ortype = rsp->rel_rtype; 1153 1154 /* 1155 * if ((shared object) and (not pc relative relocation) and 1156 * (not against ABS symbol)) 1157 * then 1158 * build R_AMD64_RELATIVE 1159 * fi 1160 */ 1161 if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 1162 !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 1163 !(IS_GOT_BASED(rsp->rel_rtype)) && 1164 !(rsp->rel_isdesc != NULL && 1165 (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 1166 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 1167 (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 1168 1169 /* 1170 * R_AMD64_RELATIVE updates a 64bit address, if this 1171 * relocation isn't a 64bit binding then we can not 1172 * simplify it to a RELATIVE relocation. 1173 */ 1174 if (reloc_table[ortype].re_fsize != sizeof (Addr)) { 1175 return (ld_add_outrel(0, rsp, ofl)); 1176 } 1177 1178 rsp->rel_rtype = R_AMD64_RELATIVE; 1179 if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR) 1180 return (S_ERROR); 1181 rsp->rel_rtype = ortype; 1182 return (1); 1183 } 1184 1185 /* 1186 * If the relocation is against a 'non-allocatable' section 1187 * and we can not resolve it now - then give a warning 1188 * message. 1189 * 1190 * We can not resolve the symbol if either: 1191 * a) it's undefined 1192 * b) it's defined in a shared library and a 1193 * COPY relocation hasn't moved it to the executable 1194 * 1195 * Note: because we process all of the relocations against the 1196 * text segment before any others - we know whether 1197 * or not a copy relocation will be generated before 1198 * we get here (see reloc_init()->reloc_segments()). 1199 */ 1200 if (!(rsp->rel_flags & FLG_REL_LOAD) && 1201 ((shndx == SHN_UNDEF) || 1202 ((sdp->sd_ref == REF_DYN_NEED) && 1203 ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 1204 Conv_inv_buf_t inv_buf; 1205 1206 /* 1207 * If the relocation is against a SHT_SUNW_ANNOTATE 1208 * section - then silently ignore that the relocation 1209 * can not be resolved. 1210 */ 1211 if (rsp->rel_osdesc && 1212 (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 1213 return (0); 1214 (void) eprintf(ofl->ofl_lml, ERR_WARNING, 1215 MSG_INTL(MSG_REL_EXTERNSYM), 1216 conv_reloc_amd64_type(rsp->rel_rtype, 0, &inv_buf), 1217 rsp->rel_isdesc->is_file->ifl_name, 1218 demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 1219 return (1); 1220 } 1221 1222 /* 1223 * Perform relocation. 1224 */ 1225 return (ld_add_actrel(NULL, rsp, ofl)); 1226 } 1227 1228 1229 static uintptr_t 1230 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 1231 { 1232 Word rtype = rsp->rel_rtype; 1233 Sym_desc *sdp = rsp->rel_sym; 1234 ofl_flag_t flags = ofl->ofl_flags; 1235 Gotndx *gnp; 1236 1237 /* 1238 * If we're building an executable - use either the IE or LE access 1239 * model. If we're building a shared object process any IE model. 1240 */ 1241 if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 1242 /* 1243 * Set the DF_STATIC_TLS flag. 1244 */ 1245 ofl->ofl_dtflags |= DF_STATIC_TLS; 1246 1247 if (!local || ((flags & FLG_OF_EXEC) == 0)) { 1248 /* 1249 * Assign a GOT entry for static TLS references. 1250 */ 1251 if ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 1252 GOT_REF_TLSIE, ofl, rsp)) == 0) { 1253 1254 if (ld_assign_got_TLS(local, rsp, ofl, sdp, 1255 gnp, GOT_REF_TLSIE, FLG_REL_STLS, 1256 rtype, R_AMD64_TPOFF64, 0) == S_ERROR) 1257 return (S_ERROR); 1258 } 1259 1260 /* 1261 * IE access model. 1262 */ 1263 if (IS_TLS_IE(rtype)) 1264 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1265 1266 /* 1267 * Fixups are required for other executable models. 1268 */ 1269 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1270 rsp, ofl)); 1271 } 1272 1273 /* 1274 * LE access model. 1275 */ 1276 if (IS_TLS_LE(rtype)) 1277 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1278 1279 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1280 rsp, ofl)); 1281 } 1282 1283 /* 1284 * Building a shared object. 1285 * 1286 * Assign a GOT entry for a dynamic TLS reference. 1287 */ 1288 if (IS_TLS_LD(rtype) && ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), 1289 GOT_REF_TLSLD, ofl, rsp)) == 0)) { 1290 1291 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 1292 FLG_REL_MTLS, rtype, R_AMD64_DTPMOD64, 0) == S_ERROR) 1293 return (S_ERROR); 1294 1295 } else if (IS_TLS_GD(rtype) && 1296 ((gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), GOT_REF_TLSGD, 1297 ofl, rsp)) == 0)) { 1298 1299 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 1300 FLG_REL_DTLS, rtype, R_AMD64_DTPMOD64, 1301 R_AMD64_DTPOFF64) == S_ERROR) 1302 return (S_ERROR); 1303 } 1304 1305 if (IS_TLS_LD(rtype)) 1306 return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 1307 1308 return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 1309 } 1310 1311 /* ARGSUSED3 */ 1312 static Gotndx * 1313 ld_find_gotndx(List * lst, Gotref gref, Ofl_desc * ofl, Rel_desc * rdesc) 1314 { 1315 Listnode * lnp; 1316 Gotndx * gnp; 1317 1318 assert(rdesc != 0); 1319 1320 if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx) 1321 return (ofl->ofl_tlsldgotndx); 1322 1323 for (LIST_TRAVERSE(lst, lnp, gnp)) { 1324 if ((rdesc->rel_raddend == gnp->gn_addend) && 1325 (gnp->gn_gotref == gref)) { 1326 return (gnp); 1327 } 1328 } 1329 return ((Gotndx *)0); 1330 } 1331 1332 static Xword 1333 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl) 1334 { 1335 Os_desc *osp = ofl->ofl_osgot; 1336 Sym_desc *sdp = rdesc->rel_sym; 1337 Xword gotndx; 1338 Gotref gref; 1339 Gotndx *gnp; 1340 1341 if (rdesc->rel_flags & FLG_REL_DTLS) 1342 gref = GOT_REF_TLSGD; 1343 else if (rdesc->rel_flags & FLG_REL_MTLS) 1344 gref = GOT_REF_TLSLD; 1345 else if (rdesc->rel_flags & FLG_REL_STLS) 1346 gref = GOT_REF_TLSIE; 1347 else 1348 gref = GOT_REF_GENERIC; 1349 1350 gnp = ld_find_gotndx(&(sdp->sd_GOTndxs), gref, ofl, rdesc); 1351 assert(gnp); 1352 1353 gotndx = (Xword)gnp->gn_gotndx; 1354 1355 if ((rdesc->rel_flags & FLG_REL_DTLS) && 1356 (rdesc->rel_rtype == R_AMD64_DTPOFF64)) 1357 gotndx++; 1358 1359 return ((Xword)(osp->os_shdr->sh_addr + (gotndx * M_GOT_ENTSIZE))); 1360 } 1361 1362 1363 /* ARGSUSED5 */ 1364 static uintptr_t 1365 ld_assign_got_ndx(List * lst, Gotndx * pgnp, Gotref gref, Ofl_desc * ofl, 1366 Rel_desc * rsp, Sym_desc * sdp) 1367 { 1368 Xword raddend; 1369 Gotndx *gnp, *_gnp; 1370 Listnode *lnp, *plnp; 1371 uint_t gotents; 1372 1373 raddend = rsp->rel_raddend; 1374 if (pgnp && (pgnp->gn_addend == raddend) && 1375 (pgnp->gn_gotref == gref)) 1376 return (1); 1377 1378 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 1379 gotents = 2; 1380 else 1381 gotents = 1; 1382 1383 plnp = 0; 1384 for (LIST_TRAVERSE(lst, lnp, _gnp)) { 1385 if (_gnp->gn_addend > raddend) 1386 break; 1387 plnp = lnp; 1388 } 1389 1390 /* 1391 * Allocate a new entry. 1392 */ 1393 if ((gnp = libld_calloc(sizeof (Gotndx), 1)) == 0) 1394 return (S_ERROR); 1395 gnp->gn_addend = raddend; 1396 gnp->gn_gotndx = ofl->ofl_gotcnt; 1397 gnp->gn_gotref = gref; 1398 1399 ofl->ofl_gotcnt += gotents; 1400 1401 if (gref == GOT_REF_TLSLD) { 1402 ofl->ofl_tlsldgotndx = gnp; 1403 return (1); 1404 } 1405 1406 if (plnp == 0) { 1407 /* 1408 * Insert at head of list 1409 */ 1410 if (list_prependc(lst, (void *)gnp) == 0) 1411 return (S_ERROR); 1412 } else if (_gnp->gn_addend > raddend) { 1413 /* 1414 * Insert in middle of lest 1415 */ 1416 if (list_insertc(lst, (void *)gnp, plnp) == 0) 1417 return (S_ERROR); 1418 } else { 1419 /* 1420 * Append to tail of list 1421 */ 1422 if (list_appendc(lst, (void *)gnp) == 0) 1423 return (S_ERROR); 1424 } 1425 return (1); 1426 } 1427 1428 static void 1429 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 1430 { 1431 sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 1432 sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++; 1433 ofl->ofl_flags |= FLG_OF_BLDGOT; 1434 } 1435 1436 static uchar_t plt0_template[M_PLT_ENTSIZE] = { 1437 /* 0x00 PUSHQ GOT+8(%rip) */ 0xff, 0x35, 0x00, 0x00, 0x00, 0x00, 1438 /* 0x06 JMP *GOT+16(%rip) */ 0xff, 0x25, 0x00, 0x00, 0x00, 0x00, 1439 /* 0x0c NOP */ 0x90, 1440 /* 0x0d NOP */ 0x90, 1441 /* 0x0e NOP */ 0x90, 1442 /* 0x0f NOP */ 0x90 1443 }; 1444 1445 /* 1446 * Initializes .got[0] with the _DYNAMIC symbol value. 1447 */ 1448 static uintptr_t 1449 ld_fillin_gotplt(Ofl_desc *ofl) 1450 { 1451 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1452 1453 if (ofl->ofl_osgot) { 1454 Sym_desc *sdp; 1455 1456 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 1457 SYM_NOHASH, 0, ofl)) != NULL) { 1458 uchar_t *genptr; 1459 1460 genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 1461 (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 1462 /* LINTED */ 1463 *(Xword *)genptr = sdp->sd_sym->st_value; 1464 if (bswap) 1465 /* LINTED */ 1466 *(Xword *)genptr = 1467 /* LINTED */ 1468 ld_bswap_Xword(*(Xword *)genptr); 1469 } 1470 } 1471 1472 /* 1473 * Fill in the reserved slot in the procedure linkage table the first 1474 * entry is: 1475 * 0x00 PUSHQ GOT+8(%rip) # GOT[1] 1476 * 0x06 JMP *GOT+16(%rip) # GOT[2] 1477 * 0x0c NOP 1478 * 0x0d NOP 1479 * 0x0e NOP 1480 * 0x0f NOP 1481 */ 1482 if ((ofl->ofl_flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) { 1483 uchar_t *pltent; 1484 Xword val1; 1485 1486 pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 1487 bcopy(plt0_template, pltent, sizeof (plt0_template)); 1488 1489 /* 1490 * If '-z noreloc' is specified - skip the do_reloc_ld 1491 * stage. 1492 */ 1493 if (!OFL_DO_RELOC(ofl)) 1494 return (1); 1495 1496 /* 1497 * filin: 1498 * PUSHQ GOT + 8(%rip) 1499 * 1500 * Note: 0x06 below represents the offset to the 1501 * next instruction - which is what %rip will 1502 * be pointing at. 1503 */ 1504 val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 1505 (M_GOT_XLINKMAP * M_GOT_ENTSIZE) - 1506 ofl->ofl_osplt->os_shdr->sh_addr - 0x06; 1507 1508 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x02], 1509 &val1, MSG_ORIG(MSG_SYM_PLTENT), 1510 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 1511 eprintf(ofl->ofl_lml, ERR_FATAL, 1512 MSG_INTL(MSG_PLT_PLT0FAIL)); 1513 return (S_ERROR); 1514 } 1515 1516 /* 1517 * filin: 1518 * JMP *GOT+16(%rip) 1519 */ 1520 val1 = (ofl->ofl_osgot->os_shdr->sh_addr) + 1521 (M_GOT_XRTLD * M_GOT_ENTSIZE) - 1522 ofl->ofl_osplt->os_shdr->sh_addr - 0x0c; 1523 1524 if (do_reloc_ld(R_AMD64_GOTPCREL, &pltent[0x08], 1525 &val1, MSG_ORIG(MSG_SYM_PLTENT), 1526 MSG_ORIG(MSG_SPECFIL_PLTENT), bswap, ofl->ofl_lml) == 0) { 1527 eprintf(ofl->ofl_lml, ERR_FATAL, 1528 MSG_INTL(MSG_PLT_PLT0FAIL)); 1529 return (S_ERROR); 1530 } 1531 } 1532 1533 return (1); 1534 } 1535 1536 1537 1538 /* 1539 * Template for generating "void (*)(void)" function 1540 */ 1541 static const uchar_t nullfunc_tmpl[] = { /* amd64 */ 1542 /* 0x00 */ 0x55, /* pushq %rbp */ 1543 /* 0x01 */ 0x48, 0x8b, 0xec, /* movq %rsp,%rbp */ 1544 /* 0x04 */ 0x48, 0x8b, 0xe5, /* movq %rbp,%rsp */ 1545 /* 0x07 */ 0x5d, /* popq %rbp */ 1546 /* 0x08 */ 0xc3 /* ret */ 1547 }; 1548 1549 1550 /* 1551 * Return the ld_targ definition for this target. 1552 */ 1553 const Target * 1554 ld_targ_init_x86(void) 1555 { 1556 static const Target _ld_targ = { 1557 { /* Target_mach */ 1558 M_MACH, /* m_mach */ 1559 M_MACHPLUS, /* m_machplus */ 1560 M_FLAGSPLUS, /* m_flagsplus */ 1561 M_CLASS, /* m_class */ 1562 M_DATA, /* m_data */ 1563 1564 M_SEGM_ALIGN, /* m_segm_align */ 1565 M_SEGM_ORIGIN, /* m_segm_origin */ 1566 M_SEGM_AORIGIN, /* m_segm_aorigin */ 1567 M_DATASEG_PERM, /* m_dataseg_perm */ 1568 M_WORD_ALIGN, /* m_word_align */ 1569 MSG_ORIG(MSG_PTH_RTLD_AMD64), /* m_def_interp */ 1570 1571 /* Relocation type codes */ 1572 M_R_ARRAYADDR, /* m_r_arrayaddr */ 1573 M_R_COPY, /* m_r_copy */ 1574 M_R_GLOB_DAT, /* m_r_glob_dat */ 1575 M_R_JMP_SLOT, /* m_r_jmp_slot */ 1576 M_R_NUM, /* m_r_num */ 1577 M_R_NONE, /* m_r_none */ 1578 M_R_RELATIVE, /* m_r_relative */ 1579 M_R_REGISTER, /* m_r_register */ 1580 1581 /* Relocation related constants */ 1582 M_REL_DT_COUNT, /* m_rel_dt_count */ 1583 M_REL_DT_ENT, /* m_rel_dt_ent */ 1584 M_REL_DT_SIZE, /* m_rel_dt_size */ 1585 M_REL_DT_TYPE, /* m_rel_dt_type */ 1586 M_REL_SHT_TYPE, /* m_rel_sht_type */ 1587 1588 /* GOT related constants */ 1589 M_GOT_ENTSIZE, /* m_got_entsize */ 1590 M_GOT_XNumber, /* m_got_xnumber */ 1591 1592 /* PLT related constants */ 1593 M_PLT_ALIGN, /* m_plt_align */ 1594 M_PLT_ENTSIZE, /* m_plt_entsize */ 1595 M_PLT_RESERVSZ, /* m_plt_reservsz */ 1596 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */ 1597 1598 M_DT_REGISTER, /* m_dt_register */ 1599 }, 1600 { /* Target_machid */ 1601 M_ID_ARRAY, /* id_array */ 1602 M_ID_BSS, /* id_bss */ 1603 M_ID_CAP, /* id_cap */ 1604 M_ID_DATA, /* id_data */ 1605 M_ID_DYNAMIC, /* id_dynamic */ 1606 M_ID_DYNSORT, /* id_dynsort */ 1607 M_ID_DYNSTR, /* id_dynstr */ 1608 M_ID_DYNSYM, /* id_dynsym */ 1609 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */ 1610 M_ID_GOT, /* id_got */ 1611 M_ID_UNKNOWN, /* id_gotdata (unused) */ 1612 M_ID_HASH, /* id_hash */ 1613 M_ID_INTERP, /* id_interp */ 1614 M_ID_LBSS, /* id_lbss */ 1615 M_ID_LDYNSYM, /* id_ldynsym */ 1616 M_ID_NOTE, /* id_note */ 1617 M_ID_NULL, /* id_null */ 1618 M_ID_PLT, /* id_plt */ 1619 M_ID_REL, /* id_rel */ 1620 M_ID_STRTAB, /* id_strtab */ 1621 M_ID_SYMINFO, /* id_syminfo */ 1622 M_ID_SYMTAB, /* id_symtab */ 1623 M_ID_SYMTAB_NDX, /* id_symtab_ndx */ 1624 M_ID_TEXT, /* id_text */ 1625 M_ID_TLS, /* id_tls */ 1626 M_ID_TLSBSS, /* id_tlsbss */ 1627 M_ID_UNKNOWN, /* id_unknown */ 1628 M_ID_UNWIND, /* id_unwind */ 1629 M_ID_USER, /* id_user */ 1630 M_ID_VERSION, /* id_version */ 1631 }, 1632 { /* Target_nullfunc */ 1633 nullfunc_tmpl, /* nf_template */ 1634 sizeof (nullfunc_tmpl), /* nf_size */ 1635 }, 1636 { /* Target_machrel */ 1637 reloc_table, 1638 1639 ld_init_rel, /* mr_init_rel */ 1640 ld_mach_eflags, /* mr_mach_eflags */ 1641 ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 1642 ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 1643 ld_calc_plt_addr, /* mr_calc_plt_addr */ 1644 ld_perform_outreloc, /* mr_perform_outreloc */ 1645 ld_do_activerelocs, /* mr_do_activerelocs */ 1646 ld_add_outrel, /* mr_add_outrel */ 1647 NULL, /* mr_reloc_register */ 1648 ld_reloc_local, /* mr_reloc_local */ 1649 NULL, /* mr_reloc_GOTOP */ 1650 ld_reloc_TLS, /* mr_reloc_TLS */ 1651 NULL, /* mr_assign_got */ 1652 ld_find_gotndx, /* mr_find_gotndx */ 1653 ld_calc_got_offset, /* mr_calc_got_offset */ 1654 ld_assign_got_ndx, /* mr_assign_got_ndx */ 1655 ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 1656 NULL, /* mr_allocate_got */ 1657 ld_fillin_gotplt, /* mr_fillin_gotplt */ 1658 }, 1659 { /* Target_machsym */ 1660 NULL, /* ms_reg_check */ 1661 NULL, /* ms_mach_sym_typecheck */ 1662 NULL, /* ms_is_regsym */ 1663 NULL, /* ms_reg_find */ 1664 NULL /* ms_reg_enter */ 1665 }, 1666 { /* Target_unwind */ 1667 make_amd64_unwindhdr, /* uw_make_unwindhdr */ 1668 populate_amd64_unwindhdr, /* uw_populate_unwindhdr */ 1669 append_amd64_unwind, /* uw_append_unwind */ 1670 } 1671 }; 1672 1673 return (&_ld_targ); 1674 } 1675