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 2009 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 } else { 885 /* 886 * Else the value is the symbols value. 887 */ 888 value = sdp->sd_sym->st_value; 889 } 890 891 /* 892 * Relocation against the GLOBAL_OFFSET_TABLE. 893 */ 894 if (arsp->rel_flags & FLG_REL_GOT) 895 arsp->rel_osdesc = ofl->ofl_osgot; 896 897 /* 898 * If loadable and not producing a relocatable object 899 * add the sections virtual address to the reference 900 * address. 901 */ 902 if ((arsp->rel_flags & FLG_REL_LOAD) && 903 ((flags & FLG_OF_RELOBJ) == 0)) 904 refaddr += arsp->rel_isdesc->is_osdesc-> 905 os_shdr->sh_addr; 906 907 /* 908 * If this entry has a PLT assigned to it, it's 909 * value is actually the address of the PLT (and 910 * not the address of the function). 911 */ 912 if (IS_PLT(arsp->rel_rtype)) { 913 if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 914 value = ld_calc_plt_addr(sdp, ofl); 915 } 916 917 /* 918 * Determine whether the value needs further adjustment. 919 * Filter through the attributes of the relocation to 920 * determine what adjustment is required. Note, many 921 * of the following cases are only applicable when a 922 * .got is present. As a .got is not generated when a 923 * relocatable object is being built, any adjustments 924 * that require a .got need to be skipped. 925 */ 926 if ((arsp->rel_flags & FLG_REL_GOT) && 927 ((flags & FLG_OF_RELOBJ) == 0)) { 928 Xword R1addr; 929 uintptr_t R2addr; 930 Word gotndx; 931 Gotndx *gnp; 932 933 /* 934 * Perform relocation against GOT table. Since 935 * this doesn't fit exactly into a relocation 936 * we place the appropriate byte in the GOT 937 * directly 938 * 939 * Calculate offset into GOT at which to apply 940 * the relocation. 941 */ 942 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, 943 ofl, NULL); 944 assert(gnp); 945 946 if (arsp->rel_rtype == R_386_TLS_DTPOFF32) 947 gotndx = gnp->gn_gotndx + 1; 948 else 949 gotndx = gnp->gn_gotndx; 950 951 R1addr = (Xword)(gotndx * M_GOT_ENTSIZE); 952 953 /* 954 * Add the GOTs data's offset. 955 */ 956 R2addr = R1addr + (uintptr_t) 957 arsp->rel_osdesc->os_outdata->d_buf; 958 959 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 960 ELF_DBG_LD_ACT, M_MACH, SHT_REL, 961 arsp->rel_rtype, R1addr, value, 962 arsp->rel_sname, arsp->rel_osdesc)); 963 964 /* 965 * And do it. 966 */ 967 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 968 *(Xword *)R2addr = 969 ld_bswap_Xword(value); 970 else 971 *(Xword *)R2addr = value; 972 continue; 973 974 } else if (IS_GOT_BASED(arsp->rel_rtype) && 975 ((flags & FLG_OF_RELOBJ) == 0)) { 976 value -= ofl->ofl_osgot->os_shdr->sh_addr; 977 978 } else if (IS_GOT_PC(arsp->rel_rtype) && 979 ((flags & FLG_OF_RELOBJ) == 0)) { 980 value = 981 (Xword)(ofl->ofl_osgot->os_shdr->sh_addr) - 982 refaddr; 983 984 } else if ((IS_PC_RELATIVE(arsp->rel_rtype)) && 985 (((flags & FLG_OF_RELOBJ) == 0) || 986 (arsp->rel_osdesc == sdp->sd_isc->is_osdesc))) { 987 value -= refaddr; 988 989 } else if (IS_TLS_INS(arsp->rel_rtype) && 990 IS_GOT_RELATIVE(arsp->rel_rtype) && 991 ((flags & FLG_OF_RELOBJ) == 0)) { 992 Gotndx *gnp; 993 994 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, 995 ofl, NULL); 996 assert(gnp); 997 value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 998 if (arsp->rel_rtype == R_386_TLS_IE) { 999 value += 1000 ofl->ofl_osgot->os_shdr->sh_addr; 1001 } 1002 1003 } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 1004 ((flags & FLG_OF_RELOBJ) == 0)) { 1005 Gotndx *gnp; 1006 1007 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1008 GOT_REF_GENERIC, ofl, NULL); 1009 assert(gnp); 1010 value = (Xword)gnp->gn_gotndx * M_GOT_ENTSIZE; 1011 1012 } else if ((arsp->rel_flags & FLG_REL_STLS) && 1013 ((flags & FLG_OF_RELOBJ) == 0)) { 1014 Xword tlsstatsize; 1015 1016 /* 1017 * This is the LE TLS reference model. Static 1018 * offset is hard-coded. 1019 */ 1020 tlsstatsize = 1021 S_ROUND(ofl->ofl_tlsphdr->p_memsz, 1022 M_TLSSTATALIGN); 1023 value = tlsstatsize - value; 1024 1025 /* 1026 * Since this code is fixed up, it assumes a 1027 * negative offset that can be added to the 1028 * thread pointer. 1029 */ 1030 if ((arsp->rel_rtype == R_386_TLS_LDO_32) || 1031 (arsp->rel_rtype == R_386_TLS_LE)) 1032 value = -value; 1033 } 1034 1035 if (arsp->rel_isdesc->is_file) 1036 ifl_name = arsp->rel_isdesc->is_file->ifl_name; 1037 else 1038 ifl_name = MSG_INTL(MSG_STR_NULL); 1039 1040 /* 1041 * Make sure we have data to relocate. Compiler and 1042 * assembler developers have been known to generate 1043 * relocations against invalid sections (normally .bss), 1044 * so for their benefit give them sufficient information 1045 * to help analyze the problem. End users should never 1046 * see this. 1047 */ 1048 if (arsp->rel_isdesc->is_indata->d_buf == 0) { 1049 Conv_inv_buf_t inv_buf; 1050 1051 eprintf(ofl->ofl_lml, ERR_FATAL, 1052 MSG_INTL(MSG_REL_EMPTYSEC), 1053 conv_reloc_386_type(arsp->rel_rtype, 1054 0, &inv_buf), 1055 ifl_name, demangle(arsp->rel_sname), 1056 EC_WORD(arsp->rel_isdesc->is_scnndx), 1057 arsp->rel_isdesc->is_name); 1058 return (S_ERROR); 1059 } 1060 1061 /* 1062 * Get the address of the data item we need to modify. 1063 */ 1064 addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 1065 (uintptr_t)_elf_getxoff(arsp->rel_isdesc-> 1066 is_indata)); 1067 1068 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT, 1069 M_MACH, SHT_REL, arsp->rel_rtype, EC_NATPTR(addr), 1070 value, arsp->rel_sname, arsp->rel_osdesc)); 1071 addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 1072 1073 if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 1074 ofl->ofl_size) || (arsp->rel_roffset > 1075 arsp->rel_osdesc->os_shdr->sh_size)) { 1076 Conv_inv_buf_t inv_buf; 1077 int class; 1078 1079 if (((uintptr_t)addr - 1080 (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 1081 class = ERR_FATAL; 1082 else 1083 class = ERR_WARNING; 1084 1085 eprintf(ofl->ofl_lml, class, 1086 MSG_INTL(MSG_REL_INVALOFFSET), 1087 conv_reloc_386_type(arsp->rel_rtype, 1088 0, &inv_buf), ifl_name, 1089 EC_WORD(arsp->rel_isdesc->is_scnndx), 1090 arsp->rel_isdesc->is_name, 1091 demangle(arsp->rel_sname), 1092 EC_ADDR((uintptr_t)addr - 1093 (uintptr_t)ofl->ofl_nehdr)); 1094 1095 if (class == ERR_FATAL) { 1096 return_code = S_ERROR; 1097 continue; 1098 } 1099 } 1100 1101 /* 1102 * The relocation is additive. Ignore the previous 1103 * symbol value if this local partial symbol is 1104 * expanded. 1105 */ 1106 if (moved) 1107 value -= *addr; 1108 1109 /* 1110 * If we have a replacement value for the relocation 1111 * target, put it in place now. 1112 */ 1113 if (arsp->rel_flags & FLG_REL_NADDEND) { 1114 Xword addend = arsp->rel_raddend; 1115 1116 if (ld_reloc_targval_set(ofl, arsp, 1117 addr, addend) == 0) 1118 return (S_ERROR); 1119 } 1120 1121 /* 1122 * If '-z noreloc' is specified - skip the do_reloc_ld 1123 * stage. 1124 */ 1125 if (OFL_DO_RELOC(ofl)) { 1126 if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr, 1127 &value, arsp->rel_sname, ifl_name, 1128 OFL_SWAP_RELOC_DATA(ofl, arsp), 1129 ofl->ofl_lml) == 0) 1130 return_code = S_ERROR; 1131 } 1132 } 1133 } 1134 return (return_code); 1135 } 1136 1137 /* 1138 * Add an output relocation record. 1139 */ 1140 static uintptr_t 1141 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 1142 { 1143 Rel_desc *orsp; 1144 Rel_cache *rcp; 1145 Sym_desc *sdp = rsp->rel_sym; 1146 static size_t nextsize = 0; 1147 1148 /* 1149 * Static executables *do not* want any relocations against them. 1150 * Since our engine still creates relocations against a WEAK UNDEFINED 1151 * symbol in a static executable, it's best to disable them here 1152 * instead of through out the relocation code. 1153 */ 1154 if (OFL_IS_STATIC_EXEC(ofl)) 1155 return (1); 1156 1157 /* 1158 * Obtain the new available relocation cache entry. 1159 */ 1160 if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize, 1161 REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR) 1162 return (S_ERROR); 1163 1164 orsp = rcp->rc_free; 1165 1166 /* 1167 * If we are adding a output relocation against a section 1168 * symbol (non-RELATIVE) then mark that section. These sections 1169 * will be added to the .dynsym symbol table. 1170 */ 1171 if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 1172 ((flags & FLG_REL_SCNNDX) || 1173 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 1174 1175 /* 1176 * If this is a COMMON symbol - no output section 1177 * exists yet - (it's created as part of sym_validate()). 1178 * So - we mark here that when it's created it should 1179 * be tagged with the FLG_OS_OUTREL flag. 1180 */ 1181 if ((sdp->sd_flags & FLG_SY_SPECSEC) && 1182 (sdp->sd_sym->st_shndx == SHN_COMMON)) { 1183 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 1184 ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 1185 else 1186 ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 1187 } else { 1188 Os_desc *osp = sdp->sd_isc->is_osdesc; 1189 1190 if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 1191 ofl->ofl_dynshdrcnt++; 1192 osp->os_flags |= FLG_OS_OUTREL; 1193 } 1194 } 1195 } 1196 1197 *orsp = *rsp; 1198 orsp->rel_flags |= flags; 1199 1200 rcp->rc_free++; 1201 ofl->ofl_outrelscnt++; 1202 1203 if (flags & FLG_REL_GOT) 1204 ofl->ofl_relocgotsz += (Xword)sizeof (Rel); 1205 else if (flags & FLG_REL_PLT) 1206 ofl->ofl_relocpltsz += (Xword)sizeof (Rel); 1207 else if (flags & FLG_REL_BSS) 1208 ofl->ofl_relocbsssz += (Xword)sizeof (Rel); 1209 else if (flags & FLG_REL_NOINFO) 1210 ofl->ofl_relocrelsz += (Xword)sizeof (Rel); 1211 else 1212 orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rel); 1213 1214 if (orsp->rel_rtype == M_R_RELATIVE) 1215 ofl->ofl_relocrelcnt++; 1216 1217 /* 1218 * We don't perform sorting on PLT relocations because 1219 * they have already been assigned a PLT index and if we 1220 * were to sort them we would have to re-assign the plt indexes. 1221 */ 1222 if (!(flags & FLG_REL_PLT)) 1223 ofl->ofl_reloccnt++; 1224 1225 /* 1226 * Insure a GLOBAL_OFFSET_TABLE is generated if required. 1227 */ 1228 if (IS_GOT_REQUIRED(orsp->rel_rtype)) 1229 ofl->ofl_flags |= FLG_OF_BLDGOT; 1230 1231 /* 1232 * Identify and possibly warn of a displacement relocation. 1233 */ 1234 if (orsp->rel_flags & FLG_REL_DISP) { 1235 ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 1236 1237 if (ofl->ofl_flags & FLG_OF_VERBOSE) 1238 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 1239 } 1240 DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_REL, 1241 M_MACH, orsp)); 1242 return (1); 1243 } 1244 1245 /* 1246 * process relocation for a LOCAL symbol 1247 */ 1248 static uintptr_t 1249 ld_reloc_local(Rel_desc * rsp, Ofl_desc * ofl) 1250 { 1251 ofl_flag_t flags = ofl->ofl_flags; 1252 Sym_desc *sdp = rsp->rel_sym; 1253 Word shndx = sdp->sd_sym->st_shndx; 1254 1255 /* 1256 * if ((shared object) and (not pc relative relocation) and 1257 * (not against ABS symbol)) 1258 * then 1259 * build R_386_RELATIVE 1260 * fi 1261 */ 1262 if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 1263 !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 1264 !(IS_GOT_BASED(rsp->rel_rtype)) && 1265 !(rsp->rel_isdesc != NULL && 1266 (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 1267 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 1268 (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 1269 Word ortype = rsp->rel_rtype; 1270 1271 rsp->rel_rtype = R_386_RELATIVE; 1272 if (ld_add_outrel(NULL, rsp, ofl) == S_ERROR) 1273 return (S_ERROR); 1274 rsp->rel_rtype = ortype; 1275 } 1276 1277 /* 1278 * If the relocation is against a 'non-allocatable' section 1279 * and we can not resolve it now - then give a warning 1280 * message. 1281 * 1282 * We can not resolve the symbol if either: 1283 * a) it's undefined 1284 * b) it's defined in a shared library and a 1285 * COPY relocation hasn't moved it to the executable 1286 * 1287 * Note: because we process all of the relocations against the 1288 * text segment before any others - we know whether 1289 * or not a copy relocation will be generated before 1290 * we get here (see reloc_init()->reloc_segments()). 1291 */ 1292 if (!(rsp->rel_flags & FLG_REL_LOAD) && 1293 ((shndx == SHN_UNDEF) || 1294 ((sdp->sd_ref == REF_DYN_NEED) && 1295 ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 1296 Conv_inv_buf_t inv_buf; 1297 1298 /* 1299 * If the relocation is against a SHT_SUNW_ANNOTATE 1300 * section - then silently ignore that the relocation 1301 * can not be resolved. 1302 */ 1303 if (rsp->rel_osdesc && 1304 (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 1305 return (0); 1306 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_EXTERNSYM), 1307 conv_reloc_386_type(rsp->rel_rtype, 0, &inv_buf), 1308 rsp->rel_isdesc->is_file->ifl_name, 1309 demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 1310 return (1); 1311 } 1312 1313 /* 1314 * Perform relocation. 1315 */ 1316 return (ld_add_actrel(NULL, rsp, ofl)); 1317 } 1318 1319 static uintptr_t 1320 ld_reloc_TLS(Boolean local, Rel_desc * rsp, Ofl_desc * ofl) 1321 { 1322 Word rtype = rsp->rel_rtype; 1323 Sym_desc *sdp = rsp->rel_sym; 1324 ofl_flag_t flags = ofl->ofl_flags; 1325 Gotndx *gnp; 1326 1327 /* 1328 * If we're building an executable - use either the IE or LE access 1329 * model. If we're building a shared object process any IE model. 1330 */ 1331 if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 1332 /* 1333 * Set the DF_STATIC_TLS flag. 1334 */ 1335 ofl->ofl_dtflags |= DF_STATIC_TLS; 1336 1337 if (!local || ((flags & FLG_OF_EXEC) == 0)) { 1338 /* 1339 * Assign a GOT entry for static TLS references. 1340 */ 1341 if ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1342 GOT_REF_TLSIE, ofl, NULL)) == NULL) { 1343 1344 if (ld_assign_got_TLS(local, rsp, ofl, sdp, 1345 gnp, GOT_REF_TLSIE, FLG_REL_STLS, 1346 rtype, R_386_TLS_TPOFF, NULL) == S_ERROR) 1347 return (S_ERROR); 1348 } 1349 1350 /* 1351 * IE access model. 1352 */ 1353 if (IS_TLS_IE(rtype)) { 1354 if (ld_add_actrel(FLG_REL_STLS, 1355 rsp, ofl) == S_ERROR) 1356 return (S_ERROR); 1357 1358 /* 1359 * A non-pic shared object needs to adjust the 1360 * active relocation (indntpoff). 1361 */ 1362 if (((flags & FLG_OF_EXEC) == 0) && 1363 (rtype == R_386_TLS_IE)) { 1364 rsp->rel_rtype = R_386_RELATIVE; 1365 return (ld_add_outrel(NULL, rsp, ofl)); 1366 } 1367 return (1); 1368 } 1369 1370 /* 1371 * Fixups are required for other executable models. 1372 */ 1373 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1374 rsp, ofl)); 1375 } 1376 1377 /* 1378 * LE access model. 1379 */ 1380 if (IS_TLS_LE(rtype) || (rtype == R_386_TLS_LDO_32)) 1381 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1382 1383 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1384 rsp, ofl)); 1385 } 1386 1387 /* 1388 * Building a shared object. 1389 * 1390 * Assign a GOT entry for a dynamic TLS reference. 1391 */ 1392 if (IS_TLS_LD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1393 GOT_REF_TLSLD, ofl, NULL)) == NULL)) { 1394 1395 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 1396 FLG_REL_MTLS, rtype, R_386_TLS_DTPMOD32, NULL) == S_ERROR) 1397 return (S_ERROR); 1398 1399 } else if (IS_TLS_GD(rtype) && ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1400 GOT_REF_TLSGD, ofl, NULL)) == NULL)) { 1401 1402 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 1403 FLG_REL_DTLS, rtype, R_386_TLS_DTPMOD32, 1404 R_386_TLS_DTPOFF32) == S_ERROR) 1405 return (S_ERROR); 1406 } 1407 1408 /* 1409 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually 1410 * cause a call to __tls_get_addr(). Convert this relocation to that 1411 * symbol now, and prepare for the PLT magic. 1412 */ 1413 if ((rtype == R_386_TLS_GD_PLT) || (rtype == R_386_TLS_LDM_PLT)) { 1414 Sym_desc *tlsgetsym; 1415 1416 if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_UU), 1417 ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR) 1418 return (S_ERROR); 1419 1420 rsp->rel_sym = tlsgetsym; 1421 rsp->rel_sname = tlsgetsym->sd_name; 1422 rsp->rel_rtype = R_386_PLT32; 1423 1424 if (ld_reloc_plt(rsp, ofl) == S_ERROR) 1425 return (S_ERROR); 1426 1427 rsp->rel_sym = sdp; 1428 rsp->rel_sname = sdp->sd_name; 1429 rsp->rel_rtype = rtype; 1430 return (1); 1431 } 1432 1433 if (IS_TLS_LD(rtype)) 1434 return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 1435 1436 return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 1437 } 1438 1439 /* ARGSUSED4 */ 1440 static uintptr_t 1441 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl, 1442 Rel_desc *rsp, Sym_desc *sdp) 1443 { 1444 Gotndx gn, *gnp; 1445 uint_t gotents; 1446 1447 if (pgnp) 1448 return (1); 1449 1450 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 1451 gotents = 2; 1452 else 1453 gotents = 1; 1454 1455 gn.gn_addend = 0; 1456 gn.gn_gotndx = ofl->ofl_gotcnt; 1457 gn.gn_gotref = gref; 1458 1459 ofl->ofl_gotcnt += gotents; 1460 1461 if (gref == GOT_REF_TLSLD) { 1462 if (ofl->ofl_tlsldgotndx == NULL) { 1463 if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL) 1464 return (S_ERROR); 1465 (void) memcpy(gnp, &gn, sizeof (Gotndx)); 1466 ofl->ofl_tlsldgotndx = gnp; 1467 } 1468 return (1); 1469 } 1470 1471 /* 1472 * GOT indexes are maintained on an Alist, where there is typically 1473 * only one index. The usage of this list is to scan the list to find 1474 * an index, and then apply that index immediately to a relocation. 1475 * Thus there are no external references to these GOT index structures 1476 * that can be compromised by the Alist being reallocated. 1477 */ 1478 if (alist_append(alpp, &gn, sizeof (Gotndx), AL_CNT_SDP_GOT) == NULL) 1479 return (S_ERROR); 1480 1481 return (1); 1482 } 1483 1484 static void 1485 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 1486 { 1487 sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 1488 sdp->sd_aux->sa_PLTGOTndx = ofl->ofl_gotcnt++; 1489 ofl->ofl_flags |= FLG_OF_BLDGOT; 1490 } 1491 1492 /* 1493 * Initializes .got[0] with the _DYNAMIC symbol value. 1494 */ 1495 static uintptr_t 1496 ld_fillin_gotplt(Ofl_desc *ofl) 1497 { 1498 ofl_flag_t flags = ofl->ofl_flags; 1499 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 1500 1501 if (ofl->ofl_osgot) { 1502 Sym_desc *sdp; 1503 1504 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 1505 SYM_NOHASH, NULL, ofl)) != NULL) { 1506 uchar_t *genptr; 1507 1508 genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 1509 (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 1510 /* LINTED */ 1511 *(Word *)genptr = (Word)sdp->sd_sym->st_value; 1512 if (bswap) 1513 /* LINTED */ 1514 *(Word *)genptr = 1515 /* LINTED */ 1516 ld_bswap_Word(*(Word *)genptr); 1517 } 1518 } 1519 1520 /* 1521 * Fill in the reserved slot in the procedure linkage table the first 1522 * entry is: 1523 * if (building a.out) { 1524 * PUSHL got[1] # the address of the link map entry 1525 * JMP * got[2] # the address of rtbinder 1526 * } else { 1527 * PUSHL got[1]@GOT(%ebx) # the address of the link map entry 1528 * JMP * got[2]@GOT(%ebx) # the address of rtbinder 1529 * } 1530 */ 1531 if ((flags & FLG_OF_DYNAMIC) && ofl->ofl_osplt) { 1532 uchar_t *pltent; 1533 1534 pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 1535 if (!(flags & FLG_OF_SHAROBJ)) { 1536 pltent[0] = M_SPECIAL_INST; 1537 pltent[1] = M_PUSHL_DISP; 1538 pltent += 2; 1539 /* LINTED */ 1540 *(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr-> 1541 sh_addr + M_GOT_XLINKMAP * M_GOT_ENTSIZE); 1542 if (bswap) 1543 /* LINTED */ 1544 *(Word *)pltent = 1545 /* LINTED */ 1546 ld_bswap_Word(*(Word *)pltent); 1547 pltent += 4; 1548 pltent[0] = M_SPECIAL_INST; 1549 pltent[1] = M_JMP_DISP_IND; 1550 pltent += 2; 1551 /* LINTED */ 1552 *(Word *)pltent = (Word)(ofl->ofl_osgot->os_shdr-> 1553 sh_addr + M_GOT_XRTLD * M_GOT_ENTSIZE); 1554 if (bswap) 1555 /* LINTED */ 1556 *(Word *)pltent = 1557 /* LINTED */ 1558 ld_bswap_Word(*(Word *)pltent); 1559 } else { 1560 pltent[0] = M_SPECIAL_INST; 1561 pltent[1] = M_PUSHL_REG_DISP; 1562 pltent += 2; 1563 /* LINTED */ 1564 *(Word *)pltent = (Word)(M_GOT_XLINKMAP * 1565 M_GOT_ENTSIZE); 1566 if (bswap) 1567 /* LINTED */ 1568 *(Word *)pltent = 1569 /* LINTED */ 1570 ld_bswap_Word(*(Word *)pltent); 1571 pltent += 4; 1572 pltent[0] = M_SPECIAL_INST; 1573 pltent[1] = M_JMP_REG_DISP_IND; 1574 pltent += 2; 1575 /* LINTED */ 1576 *(Word *)pltent = (Word)(M_GOT_XRTLD * 1577 M_GOT_ENTSIZE); 1578 if (bswap) 1579 /* LINTED */ 1580 *(Word *)pltent = 1581 /* LINTED */ 1582 ld_bswap_Word(*(Word *)pltent); 1583 } 1584 } 1585 return (1); 1586 } 1587 1588 1589 1590 /* 1591 * Template for generating "void (*)(void)" function 1592 */ 1593 static const uchar_t nullfunc_tmpl[] = { /* IA32 */ 1594 /* 0x00 */ 0xc3 /* ret */ 1595 }; 1596 1597 1598 1599 /* 1600 * Function used to provide fill padding in SHF_EXECINSTR sections 1601 * 1602 * entry: 1603 * 1604 * base - base address of section being filled 1605 * offset - starting offset for fill within memory referenced by base 1606 * cnt - # bytes to be filled 1607 * 1608 * exit: 1609 * The fill has been completed. 1610 */ 1611 static void 1612 execfill(void *base, off_t off, size_t cnt) 1613 { 1614 /* 1615 * 0x90 is an X86 NOP instruction in both 32 and 64-bit worlds. 1616 * There are no alignment constraints. 1617 */ 1618 (void) memset(off + (char *)base, 0x90, cnt); 1619 } 1620 1621 1622 /* 1623 * Return the ld_targ definition for this target. 1624 */ 1625 const Target * 1626 ld_targ_init_x86(void) 1627 { 1628 static const Target _ld_targ = { 1629 { /* Target_mach */ 1630 M_MACH, /* m_mach */ 1631 M_MACHPLUS, /* m_machplus */ 1632 M_FLAGSPLUS, /* m_flagsplus */ 1633 M_CLASS, /* m_class */ 1634 M_DATA, /* m_data */ 1635 1636 M_SEGM_ALIGN, /* m_segm_align */ 1637 M_SEGM_ORIGIN, /* m_segm_origin */ 1638 M_SEGM_AORIGIN, /* m_segm_aorigin */ 1639 M_DATASEG_PERM, /* m_dataseg_perm */ 1640 M_WORD_ALIGN, /* m_word_align */ 1641 MSG_ORIG(MSG_PTH_RTLD), /* m_def_interp */ 1642 1643 /* Relocation type codes */ 1644 M_R_ARRAYADDR, /* m_r_arrayaddr */ 1645 M_R_COPY, /* m_r_copy */ 1646 M_R_GLOB_DAT, /* m_r_glob_dat */ 1647 M_R_JMP_SLOT, /* m_r_jmp_slot */ 1648 M_R_NUM, /* m_r_num */ 1649 M_R_NONE, /* m_r_none */ 1650 M_R_RELATIVE, /* m_r_relative */ 1651 M_R_REGISTER, /* m_r_register */ 1652 1653 /* Relocation related constants */ 1654 M_REL_DT_COUNT, /* m_rel_dt_count */ 1655 M_REL_DT_ENT, /* m_rel_dt_ent */ 1656 M_REL_DT_SIZE, /* m_rel_dt_size */ 1657 M_REL_DT_TYPE, /* m_rel_dt_type */ 1658 M_REL_SHT_TYPE, /* m_rel_sht_type */ 1659 1660 /* GOT related constants */ 1661 M_GOT_ENTSIZE, /* m_got_entsize */ 1662 M_GOT_XNumber, /* m_got_xnumber */ 1663 1664 /* PLT related constants */ 1665 M_PLT_ALIGN, /* m_plt_align */ 1666 M_PLT_ENTSIZE, /* m_plt_entsize */ 1667 M_PLT_RESERVSZ, /* m_plt_reservsz */ 1668 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */ 1669 1670 /* Section type of .eh_frame/.eh_frame_hdr sections */ 1671 SHT_PROGBITS, /* m_sht_unwind */ 1672 1673 M_DT_REGISTER, /* m_dt_register */ 1674 }, 1675 { /* Target_machid */ 1676 M_ID_ARRAY, /* id_array */ 1677 M_ID_BSS, /* id_bss */ 1678 M_ID_CAP, /* id_cap */ 1679 M_ID_DATA, /* id_data */ 1680 M_ID_DYNAMIC, /* id_dynamic */ 1681 M_ID_DYNSORT, /* id_dynsort */ 1682 M_ID_DYNSTR, /* id_dynstr */ 1683 M_ID_DYNSYM, /* id_dynsym */ 1684 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */ 1685 M_ID_GOT, /* id_got */ 1686 M_ID_UNKNOWN, /* id_gotdata (unused) */ 1687 M_ID_HASH, /* id_hash */ 1688 M_ID_INTERP, /* id_interp */ 1689 M_ID_LBSS, /* id_lbss */ 1690 M_ID_LDYNSYM, /* id_ldynsym */ 1691 M_ID_NOTE, /* id_note */ 1692 M_ID_NULL, /* id_null */ 1693 M_ID_PLT, /* id_plt */ 1694 M_ID_REL, /* id_rel */ 1695 M_ID_STRTAB, /* id_strtab */ 1696 M_ID_SYMINFO, /* id_syminfo */ 1697 M_ID_SYMTAB, /* id_symtab */ 1698 M_ID_SYMTAB_NDX, /* id_symtab_ndx */ 1699 M_ID_TEXT, /* id_text */ 1700 M_ID_TLS, /* id_tls */ 1701 M_ID_TLSBSS, /* id_tlsbss */ 1702 M_ID_UNKNOWN, /* id_unknown */ 1703 M_ID_UNWIND, /* id_unwind */ 1704 M_ID_UNWINDHDR, /* id_unwindhdr */ 1705 M_ID_USER, /* id_user */ 1706 M_ID_VERSION, /* id_version */ 1707 }, 1708 { /* Target_nullfunc */ 1709 nullfunc_tmpl, /* nf_template */ 1710 sizeof (nullfunc_tmpl), /* nf_size */ 1711 }, 1712 { /* Target_fillfunc */ 1713 execfill /* ff_execfill */ 1714 }, 1715 { /* Target_machrel */ 1716 reloc_table, 1717 1718 ld_init_rel, /* mr_init_rel */ 1719 ld_mach_eflags, /* mr_mach_eflags */ 1720 ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 1721 ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 1722 ld_calc_plt_addr, /* mr_calc_plt_addr */ 1723 ld_perform_outreloc, /* mr_perform_outreloc */ 1724 ld_do_activerelocs, /* mr_do_activerelocs */ 1725 ld_add_outrel, /* mr_add_outrel */ 1726 NULL, /* mr_reloc_register */ 1727 ld_reloc_local, /* mr_reloc_local */ 1728 NULL, /* mr_reloc_GOTOP */ 1729 ld_reloc_TLS, /* mr_reloc_TLS */ 1730 NULL, /* mr_assign_got */ 1731 ld_find_got_ndx, /* mr_find_got_ndx */ 1732 ld_calc_got_offset, /* mr_calc_got_offset */ 1733 ld_assign_got_ndx, /* mr_assign_got_ndx */ 1734 ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 1735 NULL, /* mr_allocate_got */ 1736 ld_fillin_gotplt, /* mr_fillin_gotplt */ 1737 }, 1738 { /* Target_machsym */ 1739 NULL, /* ms_reg_check */ 1740 NULL, /* ms_mach_sym_typecheck */ 1741 NULL, /* ms_is_regsym */ 1742 NULL, /* ms_reg_find */ 1743 NULL /* ms_reg_enter */ 1744 } 1745 }; 1746 1747 return (&_ld_targ); 1748 } 1749