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