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