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) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 27 */ 28 29 /* Get the sparc version of the relocation engine */ 30 #define DO_RELOC_LIBLD_SPARC 31 32 #include <string.h> 33 #include <stdio.h> 34 #include <sys/elf_SPARC.h> 35 #include <debug.h> 36 #include <reloc.h> 37 #include <sparc/machdep_sparc.h> 38 #include "msg.h" 39 #include "_libld.h" 40 #include "machsym.sparc.h" 41 42 /* 43 * Local Variable Definitions 44 */ 45 static Sword neggotoffset = 0; /* off. of GOT table from GOT symbol */ 46 static Sword smlgotcnt = M_GOT_XNumber; /* no. of small GOT symbols */ 47 static Sword mixgotcnt = 0; /* # syms with both large/small GOT */ 48 49 /* 50 * Search the GOT index list for a GOT entry with a matching reference and the 51 * proper addend. 52 */ 53 static Gotndx * 54 ld_find_got_ndx(Alist *alp, Gotref gref, Ofl_desc *ofl, Rel_desc *rdesc) 55 { 56 Aliste idx; 57 Gotndx *gnp; 58 59 assert(rdesc != 0); 60 61 if ((gref == GOT_REF_TLSLD) && ofl->ofl_tlsldgotndx) 62 return (ofl->ofl_tlsldgotndx); 63 64 for (ALIST_TRAVERSE(alp, idx, gnp)) { 65 if ((rdesc->rel_raddend == gnp->gn_addend) && 66 (gref == gnp->gn_gotref)) 67 return (gnp); 68 } 69 return (NULL); 70 } 71 72 static Xword 73 ld_calc_got_offset(Rel_desc * rdesc, Ofl_desc * ofl) 74 { 75 Os_desc *osp = ofl->ofl_osgot; 76 Sym_desc *sdp = rdesc->rel_sym; 77 Xword gotndx; 78 Gotref gref; 79 Gotndx *gnp; 80 81 if (rdesc->rel_flags & FLG_REL_DTLS) 82 gref = GOT_REF_TLSGD; 83 else if (rdesc->rel_flags & FLG_REL_MTLS) 84 gref = GOT_REF_TLSLD; 85 else if (rdesc->rel_flags & FLG_REL_STLS) 86 gref = GOT_REF_TLSIE; 87 else 88 gref = GOT_REF_GENERIC; 89 90 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, rdesc); 91 assert(gnp); 92 93 gotndx = (Xword)gnp->gn_gotndx; 94 95 if ((rdesc->rel_flags & FLG_REL_DTLS) && 96 (rdesc->rel_rtype == M_R_DTPOFF)) 97 gotndx++; 98 99 return ((Xword)((osp->os_shdr->sh_addr) + (gotndx * M_GOT_ENTSIZE) + 100 (-neggotoffset * M_GOT_ENTSIZE))); 101 } 102 103 static Word 104 ld_init_rel(Rel_desc *reld, Word *typedata, void *reloc) 105 { 106 Rela *rela = (Rela *)reloc; 107 108 /* LINTED */ 109 reld->rel_rtype = (Word)ELF_R_TYPE(rela->r_info, M_MACH); 110 reld->rel_roffset = rela->r_offset; 111 reld->rel_raddend = rela->r_addend; 112 *typedata = (Word)ELF_R_TYPE_DATA(rela->r_info); 113 114 reld->rel_flags |= FLG_REL_RELA; 115 116 return ((Word)ELF_R_SYM(rela->r_info)); 117 } 118 119 static void 120 ld_mach_eflags(Ehdr *ehdr, Ofl_desc *ofl) 121 { 122 Word eflags = ofl->ofl_dehdr->e_flags; 123 Word memopt1, memopt2; 124 static int firstpass; 125 126 /* 127 * If a *PLUS relocatable is included, the output object is type *PLUS. 128 */ 129 if ((ehdr->e_machine == EM_SPARC32PLUS) && 130 (ehdr->e_flags & EF_SPARC_32PLUS)) 131 ofl->ofl_dehdr->e_machine = EM_SPARC32PLUS; 132 133 /* 134 * On the first pass, we don't yet have a memory model to compare 135 * against, therefore the initial file becomes our baseline. Subsequent 136 * passes will do the comparison described below. 137 */ 138 if (firstpass == 0) { 139 ofl->ofl_dehdr->e_flags |= ehdr->e_flags; 140 firstpass++; 141 return; 142 } 143 144 /* 145 * Determine which memory model to mark the binary with. The options 146 * are (most restrictive to least): 147 * 148 * EF_SPARCV9_TSO 0x0 Total Store Order 149 * EF_SPARCV9_PSO 0x1 Partial Store Order 150 * EF_SPARCV9_RMO 0x2 Relaxed Memory Order 151 * 152 * Mark the binary with the most restrictive option encountered from a 153 * relocatable object included in the link. 154 */ 155 eflags |= (ehdr->e_flags & ~EF_SPARCV9_MM); 156 memopt1 = eflags & EF_SPARCV9_MM; 157 memopt2 = ehdr->e_flags & EF_SPARCV9_MM; 158 eflags &= ~EF_SPARCV9_MM; 159 160 if ((memopt1 == EF_SPARCV9_TSO) || (memopt2 == EF_SPARCV9_TSO)) 161 /* EMPTY */ 162 ; 163 else if ((memopt1 == EF_SPARCV9_PSO) || (memopt2 == EF_SPARCV9_PSO)) 164 eflags |= EF_SPARCV9_PSO; 165 else 166 eflags |= EF_SPARCV9_RMO; 167 168 ofl->ofl_dehdr->e_flags = eflags; 169 } 170 171 static void 172 ld_mach_make_dynamic(Ofl_desc *ofl, size_t *cnt) 173 { 174 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) { 175 /* 176 * Create this entry if we are going to create a PLT table. 177 */ 178 if (ofl->ofl_pltcnt) 179 (*cnt)++; /* DT_PLTGOT */ 180 } 181 } 182 183 static void 184 ld_mach_update_odynamic(Ofl_desc *ofl, Dyn **dyn) 185 { 186 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && ofl->ofl_pltcnt) { 187 (*dyn)->d_tag = DT_PLTGOT; 188 if (ofl->ofl_osplt) 189 (*dyn)->d_un.d_ptr = ofl->ofl_osplt->os_shdr->sh_addr; 190 else 191 (*dyn)->d_un.d_ptr = 0; 192 (*dyn)++; 193 } 194 } 195 196 #if defined(_ELF64) 197 198 static Xword 199 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl) 200 { 201 Xword value, pltndx, farpltndx; 202 203 pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1; 204 205 if ((pltndx) < M64_PLT_NEARPLTS) { 206 value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) + 207 (pltndx * M_PLT_ENTSIZE); 208 return (value); 209 } 210 211 farpltndx = pltndx - M64_PLT_NEARPLTS; 212 213 /* 214 * pltoffset of a far plt is calculated by: 215 * 216 * <size of near plt table> + 217 * <size of preceding far plt blocks> + 218 * <blockndx * sizeof (far plt entsize)> 219 */ 220 value = 221 /* size of near plt table */ 222 (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) + 223 /* size of preceding far plt blocks */ 224 ((farpltndx / M64_PLT_FBLKCNTS) * 225 ((M64_PLT_FENTSIZE + sizeof (Addr)) * 226 M64_PLT_FBLKCNTS)) + 227 /* pltblockendx * fentsize */ 228 ((farpltndx % M64_PLT_FBLKCNTS) * M64_PLT_FENTSIZE); 229 230 value += (Xword)(ofl->ofl_osplt->os_shdr->sh_addr); 231 return (value); 232 } 233 234 /* 235 * Instructions required for Far PLT's 236 */ 237 static uchar_t farplt_instrs[24] = { 238 0x8a, 0x10, 0x00, 0x0f, /* mov %o7, %g5 */ 239 0x40, 0x00, 0x00, 0x02, /* call . + 0x8 */ 240 0x01, 0x00, 0x00, 0x00, /* nop */ 241 0xc2, 0x5b, 0xe0, 0x00, /* ldx [%o7 + 0], %g1 */ 242 0x83, 0xc3, 0xc0, 0x01, /* jmpl %o7 + %g1, %g1 */ 243 0x9e, 0x10, 0x00, 0x05 /* mov %g5, %o7 */ 244 }; 245 246 /* 247 * Far PLT'S: 248 * 249 * Far PLT's are established in blocks of '160' at a time. These 250 * PLT's consist of 6 instructions (24 bytes) and 1 pointer (8 bytes). 251 * The instructions are collected together in blocks of 160 entries 252 * followed by 160 pointers. The last group of entries and pointers 253 * may contain less then 160 items. No padding is required. 254 * 255 * .PLT32768: 256 * mov %o7, %g5 257 * call . + 8 258 * nop 259 * ldx [%o7 + .PLTP32768 - (.PLT32768 + 4)], %g1 260 * jmpl %o7 + %g1, %g1 261 * mov %g5, %o7 262 * ................................ 263 * .PLT32927: 264 * mov %o7, %g5 265 * call . + 8 266 * nop 267 * ldx [%o7 + .PLTP32927 - (.PLT32927 + 4)], %g1 268 * jmpl %o7 + %g1, %g1 269 * mov %g5, %o7 270 * .PLTP32768: 271 * .xword .PLT0-(.PLT32768+4) 272 * ................................ 273 * .PLTP32927: 274 * .xword .PLT0-(.PLT32927+4) 275 * 276 */ 277 static void 278 plt_far_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend) 279 { 280 uint_t blockndx; /* # of far PLT blocks */ 281 uint_t farblkcnt; /* Index to far PLT block */ 282 Xword farpltndx; /* index of Far Plt */ 283 Xword farpltblkndx; /* index of PLT in BLOCK */ 284 uint32_t *pltent; /* ptr to plt instr. sequence */ 285 uint64_t *pltentptr; /* ptr to plt addr ptr */ 286 Sxword pltblockoff; /* offset to Far plt block */ 287 Sxword pltoff; /* offset to PLT instr. sequence */ 288 Sxword pltptroff; /* offset to PLT addr ptr */ 289 uchar_t *pltbuf; /* ptr to PLT's in file */ 290 291 292 farblkcnt = ((ofl->ofl_pltcnt - 1 + 293 M_PLT_XNumber - M64_PLT_NEARPLTS) / M64_PLT_FBLKCNTS); 294 295 /* 296 * Determine the 'Far' PLT index. 297 */ 298 farpltndx = pltndx - 1 + M_PLT_XNumber - M64_PLT_NEARPLTS; 299 farpltblkndx = farpltndx % M64_PLT_FBLKCNTS; 300 301 /* 302 * Determine what FPLT block this plt falls into. 303 */ 304 blockndx = (uint_t)(farpltndx / M64_PLT_FBLKCNTS); 305 306 /* 307 * Calculate the starting offset of the Far PLT block 308 * that this PLT is a member of. 309 */ 310 pltblockoff = (M64_PLT_NEARPLTS * M_PLT_ENTSIZE) + 311 (blockndx * M64_PLT_FBLOCKSZ); 312 313 pltoff = pltblockoff + 314 (farpltblkndx * M64_PLT_FENTSIZE); 315 316 pltptroff = pltblockoff; 317 318 319 if (farblkcnt > blockndx) { 320 /* 321 * If this is a full block - the 'pltptroffs' start 322 * after 160 fplts. 323 */ 324 pltptroff += (M64_PLT_FBLKCNTS * M64_PLT_FENTSIZE) + 325 (farpltblkndx * M64_PLT_PSIZE); 326 } else { 327 Xword lastblkpltndx; 328 /* 329 * If this is the last block - the the pltptr's start 330 * after the last FPLT instruction sequence. 331 */ 332 lastblkpltndx = (ofl->ofl_pltcnt - 1 + M_PLT_XNumber - 333 M64_PLT_NEARPLTS) % M64_PLT_FBLKCNTS; 334 pltptroff += ((lastblkpltndx + 1) * M64_PLT_FENTSIZE) + 335 (farpltblkndx * M64_PLT_PSIZE); 336 } 337 pltbuf = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf; 338 339 /* 340 * For far-plts, the Raddend and Roffset fields are defined 341 * to be: 342 * 343 * roffset: address of .PLTP# 344 * raddend: -(.PLT#+4) 345 */ 346 *roffset = pltptroff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr); 347 *raddend = -(pltoff + 4 + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr)); 348 349 /* LINTED */ 350 pltent = (uint32_t *)(pltbuf + pltoff); 351 /* LINTED */ 352 pltentptr = (uint64_t *)(pltbuf + pltptroff); 353 (void) memcpy(pltent, farplt_instrs, sizeof (farplt_instrs)); 354 355 /* 356 * update 357 * ldx [%o7 + 0], %g1 358 * to 359 * ldx [%o7 + .PLTP# - (.PLT# + 4)], %g1 360 */ 361 /* LINTED */ 362 pltent[3] |= (uint32_t)(pltptroff - (pltoff + 4)); 363 364 /* 365 * Store: 366 * .PLTP# 367 * .xword .PLT0 - .PLT# + 4 368 */ 369 *pltentptr = -(pltoff + 4); 370 } 371 372 /* 373 * Build a single V9 P.L.T. entry - code is: 374 * 375 * For Target Addresses +/- 4GB of the entry 376 * ----------------------------------------- 377 * sethi (. - .PLT0), %g1 378 * ba,a %xcc, .PLT1 379 * nop 380 * nop 381 * nop 382 * nop 383 * nop 384 * nop 385 * 386 * For Target Addresses +/- 2GB of the entry 387 * ----------------------------------------- 388 * 389 * .PLT0 is the address of the first entry in the P.L.T. 390 * This one is filled in by the run-time link editor. We just 391 * have to leave space for it. 392 */ 393 static void 394 plt_entry(Ofl_desc *ofl, Xword pltndx, Xword *roffset, Sxword *raddend) 395 { 396 uchar_t *pltent; /* PLT entry being created. */ 397 Sxword pltoff; /* Offset of this entry from PLT top */ 398 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 399 400 /* 401 * The second part of the V9 ABI (sec. 5.2.4) 402 * applies to plt entries greater than 0x8000 (32,768). 403 * This is handled in 'plt_far_entry()' 404 */ 405 if ((pltndx - 1 + M_PLT_XNumber) >= M64_PLT_NEARPLTS) { 406 plt_far_entry(ofl, pltndx, roffset, raddend); 407 return; 408 } 409 410 pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE; 411 pltent = (uchar_t *)ofl->ofl_osplt->os_outdata->d_buf + pltoff; 412 413 *roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr); 414 *raddend = 0; 415 416 /* 417 * PLT[0]: sethi %hi(. - .L0), %g1 418 */ 419 /* LINTED */ 420 *(Word *)pltent = M_SETHIG1 | pltoff; 421 if (bswap) 422 /* LINTED */ 423 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 424 425 /* 426 * PLT[1]: ba,a %xcc, .PLT1 (.PLT1 accessed as a 427 * PC-relative index of longwords). 428 */ 429 pltent += M_PLT_INSSIZE; 430 pltoff += M_PLT_INSSIZE; 431 pltoff = -pltoff; 432 /* LINTED */ 433 *(Word *)pltent = M_BA_A_XCC | 434 (((pltoff + M_PLT_ENTSIZE) >> 2) & S_MASK(19)); 435 if (bswap) 436 /* LINTED */ 437 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 438 439 /* 440 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI). 441 */ 442 pltent += M_PLT_INSSIZE; 443 /* LINTED */ 444 *(Word *)pltent = M_NOP; 445 if (bswap) 446 /* LINTED */ 447 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 448 449 /* 450 * PLT[3]: sethi 0, %g0 (NOP for PLT padding). 451 */ 452 pltent += M_PLT_INSSIZE; 453 /* LINTED */ 454 *(Word *)pltent = M_NOP; 455 if (bswap) 456 /* LINTED */ 457 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 458 459 /* 460 * PLT[4]: sethi 0, %g0 (NOP for PLT padding). 461 */ 462 pltent += M_PLT_INSSIZE; 463 /* LINTED */ 464 *(Word *)pltent = M_NOP; 465 if (bswap) 466 /* LINTED */ 467 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 468 469 /* 470 * PLT[5]: sethi 0, %g0 (NOP for PLT padding). 471 */ 472 pltent += M_PLT_INSSIZE; 473 /* LINTED */ 474 *(Word *)pltent = M_NOP; 475 if (bswap) 476 /* LINTED */ 477 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 478 479 /* 480 * PLT[6]: sethi 0, %g0 (NOP for PLT padding). 481 */ 482 pltent += M_PLT_INSSIZE; 483 /* LINTED */ 484 *(Word *)pltent = M_NOP; 485 if (bswap) 486 /* LINTED */ 487 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 488 489 /* 490 * PLT[7]: sethi 0, %g0 (NOP for PLT padding). 491 */ 492 pltent += M_PLT_INSSIZE; 493 /* LINTED */ 494 *(Word *)pltent = M_NOP; 495 if (bswap) 496 /* LINTED */ 497 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 498 } 499 500 501 #else /* Elf 32 */ 502 503 static Xword 504 ld_calc_plt_addr(Sym_desc *sdp, Ofl_desc *ofl) 505 { 506 Xword value, pltndx; 507 508 pltndx = sdp->sd_aux->sa_PLTndx + M_PLT_XNumber - 1; 509 value = (Xword)(ofl->ofl_osplt->os_shdr->sh_addr) + 510 (pltndx * M_PLT_ENTSIZE); 511 return (value); 512 } 513 514 515 /* 516 * Build a single P.L.T. entry - code is: 517 * 518 * sethi (. - .L0), %g1 519 * ba,a .L0 520 * sethi 0, %g0 (nop) 521 * 522 * .L0 is the address of the first entry in the P.L.T. 523 * This one is filled in by the run-time link editor. We just 524 * have to leave space for it. 525 */ 526 static void 527 plt_entry(Ofl_desc * ofl, Xword pltndx, Xword *roffset, Sxword *raddend) 528 { 529 Byte *pltent; /* PLT entry being created. */ 530 Sxword pltoff; /* Offset of this entry from PLT top */ 531 int bswap = (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0; 532 533 pltoff = M_PLT_RESERVSZ + (pltndx - 1) * M_PLT_ENTSIZE; 534 pltent = (Byte *)ofl->ofl_osplt->os_outdata->d_buf + pltoff; 535 536 *roffset = pltoff + (Xword)(ofl->ofl_osplt->os_shdr->sh_addr); 537 *raddend = 0; 538 539 /* 540 * PLT[0]: sethi %hi(. - .L0), %g1 541 */ 542 /* LINTED */ 543 *(Word *)pltent = M_SETHIG1 | pltoff; 544 if (bswap) 545 /* LINTED */ 546 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 547 548 /* 549 * PLT[1]: ba,a .L0 (.L0 accessed as a PC-relative index of longwords) 550 */ 551 pltent += M_PLT_INSSIZE; 552 pltoff += M_PLT_INSSIZE; 553 pltoff = -pltoff; 554 /* LINTED */ 555 *(Word *)pltent = M_BA_A | ((pltoff >> 2) & S_MASK(22)); 556 if (bswap) 557 /* LINTED */ 558 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 559 560 /* 561 * PLT[2]: sethi 0, %g0 (NOP for delay slot of eventual CTI). 562 */ 563 pltent += M_PLT_INSSIZE; 564 /* LINTED */ 565 *(Word *)pltent = M_SETHIG0; 566 if (bswap) 567 /* LINTED */ 568 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 569 570 /* 571 * PLT[3]: sethi 0, %g0 (NOP for PLT padding). 572 */ 573 pltent += M_PLT_INSSIZE; 574 /* LINTED */ 575 *(Word *)pltent = M_SETHIG0; 576 if (bswap) 577 /* LINTED */ 578 *(Word *)pltent = ld_bswap_Word(*(Word *)pltent); 579 } 580 581 #endif /* _ELF64 */ 582 583 static uintptr_t 584 ld_perform_outreloc(Rel_desc *orsp, Ofl_desc *ofl) 585 { 586 Os_desc *relosp, *osp = NULL; 587 Xword ndx, roffset, value; 588 Sxword raddend; 589 const Rel_entry *rep; 590 Rela rea; 591 char *relbits; 592 Sym_desc *sdp, *psym = NULL; 593 int sectmoved = 0; 594 Word dtflags1 = ofl->ofl_dtflags_1; 595 ofl_flag_t flags = ofl->ofl_flags; 596 597 raddend = orsp->rel_raddend; 598 sdp = orsp->rel_sym; 599 600 /* 601 * Special case, a regsiter symbol associated with symbol 602 * index 0 is initialized (i.e. relocated) to a constant 603 * in the r_addend field rather than to a symbol value. 604 */ 605 if ((orsp->rel_rtype == M_R_REGISTER) && !sdp) { 606 relosp = ofl->ofl_osrel; 607 relbits = (char *)relosp->os_outdata->d_buf; 608 609 rea.r_info = ELF_R_INFO(0, 610 ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp), 611 orsp->rel_rtype)); 612 rea.r_offset = orsp->rel_roffset; 613 rea.r_addend = raddend; 614 DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, 615 relosp->os_name, ld_reloc_sym_name(orsp))); 616 617 assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size); 618 (void) memcpy((relbits + relosp->os_szoutrels), 619 (char *)&rea, sizeof (Rela)); 620 relosp->os_szoutrels += (Xword)sizeof (Rela); 621 622 return (1); 623 } 624 625 /* 626 * If the section this relocation is against has been discarded 627 * (-zignore), then also discard (skip) the relocation itself. 628 */ 629 if (orsp->rel_isdesc && ((orsp->rel_flags & 630 (FLG_REL_GOT | FLG_REL_BSS | FLG_REL_PLT | FLG_REL_NOINFO)) == 0) && 631 (orsp->rel_isdesc->is_flags & FLG_IS_DISCARD)) { 632 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, orsp)); 633 return (1); 634 } 635 636 /* 637 * If this is a relocation against a move table, or expanded move 638 * table, adjust the relocation entries. 639 */ 640 if (RELAUX_GET_MOVE(orsp)) 641 ld_adj_movereloc(ofl, orsp); 642 643 /* 644 * If this is a relocation against a section then we need to adjust the 645 * raddend field to compensate for the new position of the input section 646 * within the new output section. 647 */ 648 if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) { 649 if (ofl->ofl_parsyms && 650 (sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 651 (psym = ld_am_I_partial(orsp, orsp->rel_raddend))) { 652 /* 653 * If the symbol is moved, adjust the value 654 */ 655 DBG_CALL(Dbg_move_outsctadj(ofl->ofl_lml, psym)); 656 sectmoved = 1; 657 if (ofl->ofl_flags & FLG_OF_RELOBJ) 658 raddend = psym->sd_sym->st_value; 659 else 660 raddend = psym->sd_sym->st_value - 661 psym->sd_isc->is_osdesc->os_shdr->sh_addr; 662 /* LINTED */ 663 raddend += (Off)_elf_getxoff(psym->sd_isc->is_indata); 664 if (psym->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 665 raddend += 666 psym->sd_isc->is_osdesc->os_shdr->sh_addr; 667 } else { 668 /* LINTED */ 669 raddend += (Off)_elf_getxoff(sdp->sd_isc->is_indata); 670 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 671 raddend += 672 sdp->sd_isc->is_osdesc->os_shdr->sh_addr; 673 } 674 } 675 676 value = sdp->sd_sym->st_value; 677 678 if (orsp->rel_flags & FLG_REL_GOT) { 679 osp = ofl->ofl_osgot; 680 roffset = ld_calc_got_offset(orsp, ofl); 681 682 } else if (orsp->rel_flags & FLG_REL_PLT) { 683 osp = ofl->ofl_osplt; 684 plt_entry(ofl, sdp->sd_aux->sa_PLTndx, &roffset, &raddend); 685 } else if (orsp->rel_flags & FLG_REL_BSS) { 686 /* 687 * This must be a R_SPARC_COPY. For these set the roffset to 688 * point to the new symbols location. 689 */ 690 osp = ofl->ofl_isbss->is_osdesc; 691 roffset = (Xword)value; 692 693 /* 694 * The raddend doesn't mean anything in an R_SPARC_COPY 695 * relocation. Null it out because it can confuse people. 696 */ 697 raddend = 0; 698 } else if (orsp->rel_flags & FLG_REL_REG) { 699 /* 700 * The offsets of relocations against register symbols 701 * identifiy the register directly - so the offset 702 * does not need to be adjusted. 703 */ 704 roffset = orsp->rel_roffset; 705 } else { 706 osp = RELAUX_GET_OSDESC(orsp); 707 708 /* 709 * Calculate virtual offset of reference point; equals offset 710 * into section + vaddr of section for loadable sections, or 711 * offset plus section displacement for nonloadable sections. 712 */ 713 roffset = orsp->rel_roffset + 714 (Off)_elf_getxoff(orsp->rel_isdesc->is_indata); 715 if (!(ofl->ofl_flags & FLG_OF_RELOBJ)) 716 roffset += orsp->rel_isdesc->is_osdesc-> 717 os_shdr->sh_addr; 718 } 719 720 if ((osp == 0) || ((relosp = osp->os_relosdesc) == 0)) 721 relosp = ofl->ofl_osrel; 722 723 /* 724 * Verify that the output relocations offset meets the 725 * alignment requirements of the relocation being processed. 726 */ 727 rep = &reloc_table[orsp->rel_rtype]; 728 if (((flags & FLG_OF_RELOBJ) || !(dtflags1 & DF_1_NORELOC)) && 729 !(rep->re_flags & FLG_RE_UNALIGN)) { 730 if (((rep->re_fsize == 2) && (roffset & 0x1)) || 731 ((rep->re_fsize == 4) && (roffset & 0x3)) || 732 ((rep->re_fsize == 8) && (roffset & 0x7))) { 733 Conv_inv_buf_t inv_buf; 734 735 eprintf(ofl->ofl_lml, ERR_FATAL, 736 MSG_INTL(MSG_REL_NONALIGN), 737 conv_reloc_SPARC_type(orsp->rel_rtype, 0, &inv_buf), 738 orsp->rel_isdesc->is_file->ifl_name, 739 ld_reloc_sym_name(orsp), EC_XWORD(roffset)); 740 return (S_ERROR); 741 } 742 } 743 744 /* 745 * Assign the symbols index for the output relocation. If the 746 * relocation refers to a SECTION symbol then it's index is based upon 747 * the output sections symbols index. Otherwise the index can be 748 * derived from the symbols index itself. 749 */ 750 if (orsp->rel_rtype == R_SPARC_RELATIVE) 751 ndx = STN_UNDEF; 752 else if ((orsp->rel_flags & FLG_REL_SCNNDX) || 753 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 754 if (sectmoved == 0) { 755 /* 756 * Check for a null input section. This can 757 * occur if this relocation references a symbol 758 * generated by sym_add_sym(). 759 */ 760 if (sdp->sd_isc && sdp->sd_isc->is_osdesc) 761 ndx = sdp->sd_isc->is_osdesc->os_identndx; 762 else 763 ndx = sdp->sd_shndx; 764 } else 765 ndx = ofl->ofl_parexpnndx; 766 } else 767 ndx = sdp->sd_symndx; 768 769 /* 770 * Add the symbols 'value' to the addend field. 771 */ 772 if (orsp->rel_flags & FLG_REL_ADVAL) 773 raddend += value; 774 775 /* 776 * The addend field for R_SPARC_TLS_DTPMOD32 and R_SPARC_TLS_DTPMOD64 777 * mean nothing. The addend is propagated in the corresponding 778 * R_SPARC_TLS_DTPOFF* relocations. 779 */ 780 if (orsp->rel_rtype == M_R_DTPMOD) 781 raddend = 0; 782 783 relbits = (char *)relosp->os_outdata->d_buf; 784 785 rea.r_info = ELF_R_INFO(ndx, 786 ELF_R_TYPE_INFO(RELAUX_GET_TYPEDATA(orsp), orsp->rel_rtype)); 787 rea.r_offset = roffset; 788 rea.r_addend = raddend; 789 DBG_CALL(Dbg_reloc_out(ofl, ELF_DBG_LD, SHT_RELA, &rea, relosp->os_name, 790 ld_reloc_sym_name(orsp))); 791 792 /* 793 * Assert we haven't walked off the end of our relocation table. 794 */ 795 assert(relosp->os_szoutrels <= relosp->os_shdr->sh_size); 796 797 (void) memcpy((relbits + relosp->os_szoutrels), 798 (char *)&rea, sizeof (Rela)); 799 relosp->os_szoutrels += (Xword)sizeof (Rela); 800 801 /* 802 * Determine if this relocation is against a non-writable, allocatable 803 * section. If so we may need to provide a text relocation diagnostic. 804 */ 805 ld_reloc_remain_entry(orsp, osp, ofl); 806 return (1); 807 } 808 809 810 /* 811 * Sparc Instructions for TLS processing 812 */ 813 #if defined(_ELF64) 814 #define TLS_GD_IE_LD 0xd0580000 /* ldx [%g0 + %g0], %o0 */ 815 #else 816 #define TLS_GD_IE_LD 0xd0000000 /* ld [%g0 + %g0], %o0 */ 817 #endif 818 #define TLS_GD_IE_ADD 0x9001c008 /* add %g7, %o0, %o0 */ 819 820 #define TLS_GD_LE_XOR 0x80182000 /* xor %g0, 0, %g0 */ 821 #define TLS_IE_LE_OR 0x80100000 /* or %g0, %o0, %o1 */ 822 /* synthetic: mov %g0, %g0 */ 823 824 #define TLS_LD_LE_CLRO0 0x90100000 /* clr %o0 */ 825 826 #define FM3_REG_MSK_RD (0x1f << 25) /* Formate (3) rd register mask */ 827 /* bits 25->29 */ 828 #define FM3_REG_MSK_RS1 (0x1f << 14) /* Formate (3) rs1 register mask */ 829 /* bits 14->18 */ 830 #define FM3_REG_MSK_RS2 0x1f /* Formate (3) rs2 register mask */ 831 /* bits 0->4 */ 832 833 #define REG_G7 7 /* %g7 register */ 834 835 static Fixupret 836 tls_fixups(Ofl_desc *ofl, Rel_desc *arsp) 837 { 838 Sym_desc *sdp = arsp->rel_sym; 839 Word rtype = arsp->rel_rtype; 840 Word *offset, w; 841 int bswap = OFL_SWAP_RELOC_DATA(ofl, arsp); 842 843 844 offset = (Word *)((uintptr_t)arsp->rel_roffset + 845 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata) + 846 (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf); 847 848 if (sdp->sd_ref == REF_DYN_NEED) { 849 /* 850 * IE reference model 851 */ 852 switch (rtype) { 853 case R_SPARC_TLS_GD_HI22: 854 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 855 R_SPARC_TLS_IE_HI22, arsp, 856 ld_reloc_sym_name)); 857 arsp->rel_rtype = R_SPARC_TLS_IE_HI22; 858 return (FIX_RELOC); 859 860 case R_SPARC_TLS_GD_LO10: 861 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 862 R_SPARC_TLS_IE_LO10, arsp, 863 ld_reloc_sym_name)); 864 arsp->rel_rtype = R_SPARC_TLS_IE_LO10; 865 return (FIX_RELOC); 866 867 case R_SPARC_TLS_GD_ADD: 868 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 869 R_SPARC_NONE, arsp, ld_reloc_sym_name)); 870 w = bswap ? ld_bswap_Word(*offset) : *offset; 871 w = (TLS_GD_IE_LD | 872 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RS2))); 873 *offset = bswap ? ld_bswap_Word(w) : w; 874 return (FIX_DONE); 875 876 case R_SPARC_TLS_GD_CALL: 877 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 878 R_SPARC_NONE, arsp, ld_reloc_sym_name)); 879 *offset = TLS_GD_IE_ADD; 880 if (bswap) 881 *offset = ld_bswap_Word(*offset); 882 return (FIX_DONE); 883 } 884 return (FIX_RELOC); 885 } 886 887 /* 888 * LE reference model 889 */ 890 switch (rtype) { 891 case R_SPARC_TLS_IE_HI22: 892 case R_SPARC_TLS_GD_HI22: 893 case R_SPARC_TLS_LDO_HIX22: 894 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 895 R_SPARC_TLS_LE_HIX22, arsp, ld_reloc_sym_name)); 896 arsp->rel_rtype = R_SPARC_TLS_LE_HIX22; 897 return (FIX_RELOC); 898 899 case R_SPARC_TLS_LDO_LOX10: 900 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 901 R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name)); 902 arsp->rel_rtype = R_SPARC_TLS_LE_LOX10; 903 return (FIX_RELOC); 904 905 case R_SPARC_TLS_IE_LO10: 906 case R_SPARC_TLS_GD_LO10: 907 /* 908 * Current instruction is: 909 * 910 * or r1, %lo(x), r2 911 * or 912 * add r1, %lo(x), r2 913 * 914 * Need to udpate this to: 915 * 916 * xor r1, %lox(x), r2 917 */ 918 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 919 R_SPARC_TLS_LE_LOX10, arsp, ld_reloc_sym_name)); 920 w = bswap ? ld_bswap_Word(*offset) : *offset; 921 w = TLS_GD_LE_XOR | 922 (w & (FM3_REG_MSK_RS1 | FM3_REG_MSK_RD)); 923 *offset = bswap ? ld_bswap_Word(w) : w; 924 arsp->rel_rtype = R_SPARC_TLS_LE_LOX10; 925 return (FIX_RELOC); 926 927 case R_SPARC_TLS_IE_LD: 928 case R_SPARC_TLS_IE_LDX: 929 /* 930 * Current instruction: 931 * ld{x} [r1 + r2], r3 932 * 933 * Need to update this to: 934 * 935 * mov r2, r3 (or %g0, r2, r3) 936 */ 937 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 938 R_SPARC_NONE, arsp, ld_reloc_sym_name)); 939 w = bswap ? ld_bswap_Word(*offset) : *offset; 940 w = (w & (FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | TLS_IE_LE_OR; 941 *offset = bswap ? ld_bswap_Word(w) : w; 942 return (FIX_DONE); 943 944 case R_SPARC_TLS_LDO_ADD: 945 case R_SPARC_TLS_GD_ADD: 946 /* 947 * Current instruction is: 948 * 949 * add gptr_reg, r2, r3 950 * 951 * Need to updated this to: 952 * 953 * add %g7, r2, r3 954 */ 955 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 956 R_SPARC_NONE, arsp, ld_reloc_sym_name)); 957 w = bswap ? ld_bswap_Word(*offset) : *offset; 958 w = w & (~FM3_REG_MSK_RS1); 959 w = w | (REG_G7 << 14); 960 *offset = bswap ? ld_bswap_Word(w) : w; 961 return (FIX_DONE); 962 963 case R_SPARC_TLS_LDM_CALL: 964 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 965 R_SPARC_NONE, arsp, ld_reloc_sym_name)); 966 *offset = TLS_LD_LE_CLRO0; 967 if (bswap) 968 *offset = ld_bswap_Word(*offset); 969 return (FIX_DONE); 970 971 case R_SPARC_TLS_LDM_HI22: 972 case R_SPARC_TLS_LDM_LO10: 973 case R_SPARC_TLS_LDM_ADD: 974 case R_SPARC_TLS_IE_ADD: 975 case R_SPARC_TLS_GD_CALL: 976 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 977 R_SPARC_NONE, arsp, ld_reloc_sym_name)); 978 *offset = M_NOP; 979 if (bswap) 980 *offset = ld_bswap_Word(*offset); 981 return (FIX_DONE); 982 } 983 return (FIX_RELOC); 984 } 985 986 #define GOTOP_ADDINST 0x80000000 /* add %g0, %g0, %g0 */ 987 988 static Fixupret 989 gotop_fixups(Ofl_desc *ofl, Rel_desc *arsp) 990 { 991 Word rtype = arsp->rel_rtype; 992 Word *offset, w; 993 const char *ifl_name; 994 Conv_inv_buf_t inv_buf; 995 int bswap; 996 997 switch (rtype) { 998 case R_SPARC_GOTDATA_OP_HIX22: 999 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 1000 R_SPARC_GOTDATA_HIX22, arsp, ld_reloc_sym_name)); 1001 arsp->rel_rtype = R_SPARC_GOTDATA_HIX22; 1002 return (FIX_RELOC); 1003 1004 case R_SPARC_GOTDATA_OP_LOX10: 1005 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 1006 R_SPARC_GOTDATA_LOX10, arsp, ld_reloc_sym_name)); 1007 arsp->rel_rtype = R_SPARC_GOTDATA_LOX10; 1008 return (FIX_RELOC); 1009 1010 case R_SPARC_GOTDATA_OP: 1011 /* 1012 * Current instruction: 1013 * ld{x} [r1 + r2], r3 1014 * 1015 * Need to update this to: 1016 * 1017 * add r1, r2, r3 1018 */ 1019 DBG_CALL(Dbg_reloc_transition(ofl->ofl_lml, M_MACH, 1020 R_SPARC_NONE, arsp, ld_reloc_sym_name)); 1021 offset = (Word *)(uintptr_t)(arsp->rel_roffset + 1022 _elf_getxoff(arsp->rel_isdesc->is_indata) + 1023 (uintptr_t)RELAUX_GET_OSDESC(arsp)->os_outdata->d_buf); 1024 bswap = OFL_SWAP_RELOC_DATA(ofl, arsp); 1025 w = bswap ? ld_bswap_Word(*offset) : *offset; 1026 w = (w & (FM3_REG_MSK_RS1 | 1027 FM3_REG_MSK_RS2 | FM3_REG_MSK_RD)) | GOTOP_ADDINST; 1028 *offset = bswap ? ld_bswap_Word(w) : w; 1029 return (FIX_DONE); 1030 } 1031 /* 1032 * We should not get here 1033 */ 1034 if (arsp->rel_isdesc->is_file) 1035 ifl_name = arsp->rel_isdesc->is_file->ifl_name; 1036 else 1037 ifl_name = MSG_INTL(MSG_STR_NULL); 1038 1039 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTFIX), 1040 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf), 1041 ifl_name, ld_reloc_sym_name(arsp)); 1042 1043 assert(0); 1044 return (FIX_ERROR); 1045 } 1046 1047 static uintptr_t 1048 ld_do_activerelocs(Ofl_desc *ofl) 1049 { 1050 Rel_desc *arsp; 1051 Rel_cachebuf *rcbp; 1052 Aliste idx; 1053 uintptr_t return_code = 1; 1054 ofl_flag_t flags = ofl->ofl_flags; 1055 1056 if (aplist_nitems(ofl->ofl_actrels.rc_list) != 0) 1057 DBG_CALL(Dbg_reloc_doact_title(ofl->ofl_lml)); 1058 1059 /* 1060 * Process active relocations. 1061 */ 1062 REL_CACHE_TRAVERSE(&ofl->ofl_actrels, idx, rcbp, arsp) { 1063 uchar_t *addr; 1064 Xword value; 1065 Sym_desc *sdp; 1066 const char *ifl_name; 1067 Xword refaddr; 1068 Os_desc *osp; 1069 1070 /* 1071 * If the section this relocation is against has been discarded 1072 * (-zignore), then discard (skip) the relocation itself. 1073 */ 1074 if ((arsp->rel_isdesc->is_flags & FLG_IS_DISCARD) && 1075 ((arsp->rel_flags & (FLG_REL_GOT | FLG_REL_BSS | 1076 FLG_REL_PLT | FLG_REL_NOINFO)) == 0)) { 1077 DBG_CALL(Dbg_reloc_discard(ofl->ofl_lml, M_MACH, arsp)); 1078 continue; 1079 } 1080 1081 /* 1082 * Perform any required TLS fixups. 1083 */ 1084 if (arsp->rel_flags & FLG_REL_TLSFIX) { 1085 Fixupret ret; 1086 1087 if ((ret = tls_fixups(ofl, arsp)) == FIX_ERROR) 1088 return (S_ERROR); 1089 if (ret == FIX_DONE) 1090 continue; 1091 } 1092 1093 /* 1094 * Perform any required GOTOP fixups. 1095 */ 1096 if (arsp->rel_flags & FLG_REL_GOTFIX) { 1097 Fixupret ret; 1098 1099 if ((ret = gotop_fixups(ofl, arsp)) == FIX_ERROR) 1100 return (S_ERROR); 1101 if (ret == FIX_DONE) 1102 continue; 1103 } 1104 1105 /* 1106 * If this is a relocation against the move table, or 1107 * expanded move table, adjust the relocation entries. 1108 */ 1109 if (RELAUX_GET_MOVE(arsp)) 1110 ld_adj_movereloc(ofl, arsp); 1111 1112 sdp = arsp->rel_sym; 1113 refaddr = arsp->rel_roffset + 1114 (Off)_elf_getxoff(arsp->rel_isdesc->is_indata); 1115 1116 if ((arsp->rel_flags & FLG_REL_CLVAL) || 1117 (arsp->rel_flags & FLG_REL_GOTCL)) 1118 value = 0; 1119 else if (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) { 1120 Sym_desc *sym; 1121 1122 /* 1123 * The value for a symbol pointing to a SECTION 1124 * is based off of that sections position. 1125 */ 1126 if ((sdp->sd_isc->is_flags & FLG_IS_RELUPD) && 1127 (sym = ld_am_I_partial(arsp, arsp->rel_raddend))) { 1128 /* 1129 * The symbol was moved, so adjust the value 1130 * relative to the new section. 1131 */ 1132 value = _elf_getxoff(sym->sd_isc->is_indata); 1133 if (sym->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 1134 value += sym->sd_isc-> 1135 is_osdesc->os_shdr->sh_addr; 1136 1137 /* 1138 * The original raddend covers the displacement 1139 * from the section start to the desired 1140 * address. The value computed above gets us 1141 * from the section start to the start of the 1142 * symbol range. Adjust the old raddend to 1143 * remove the offset from section start to 1144 * symbol start, leaving the displacement 1145 * within the range of the symbol. 1146 */ 1147 arsp->rel_raddend -= sym->sd_osym->st_value; 1148 } else { 1149 value = _elf_getxoff(sdp->sd_isc->is_indata); 1150 if (sdp->sd_isc->is_shdr->sh_flags & SHF_ALLOC) 1151 value += sdp->sd_isc-> 1152 is_osdesc->os_shdr->sh_addr; 1153 } 1154 1155 if (sdp->sd_isc->is_shdr->sh_flags & SHF_TLS) 1156 value -= ofl->ofl_tlsphdr->p_vaddr; 1157 1158 } else if (IS_SIZE(arsp->rel_rtype)) { 1159 /* 1160 * Size relocations require the symbols size. 1161 */ 1162 value = sdp->sd_sym->st_size; 1163 1164 } else if ((sdp->sd_flags & FLG_SY_CAP) && 1165 sdp->sd_aux && sdp->sd_aux->sa_PLTndx) { 1166 /* 1167 * If relocation is against a capabilities symbol, we 1168 * need to jump to an associated PLT, so that at runtime 1169 * ld.so.1 is involved to determine the best binding 1170 * choice. Otherwise, the value is the symbols value. 1171 */ 1172 value = ld_calc_plt_addr(sdp, ofl); 1173 1174 } else 1175 value = sdp->sd_sym->st_value; 1176 1177 /* 1178 * Relocation against the GLOBAL_OFFSET_TABLE. 1179 */ 1180 if ((arsp->rel_flags & FLG_REL_GOT) && 1181 !ld_reloc_set_aux_osdesc(ofl, arsp, ofl->ofl_osgot)) 1182 return (S_ERROR); 1183 osp = RELAUX_GET_OSDESC(arsp); 1184 1185 /* 1186 * If loadable and not producing a relocatable object add the 1187 * sections virtual address to the reference address. 1188 */ 1189 if ((arsp->rel_flags & FLG_REL_LOAD) && 1190 ((flags & FLG_OF_RELOBJ) == 0)) 1191 refaddr += 1192 arsp->rel_isdesc->is_osdesc->os_shdr->sh_addr; 1193 1194 /* 1195 * If this entry has a PLT assigned to it, its value is actually 1196 * the address of the PLT (and not the address of the function). 1197 */ 1198 if (IS_PLT(arsp->rel_rtype)) { 1199 if (sdp->sd_aux && sdp->sd_aux->sa_PLTndx) 1200 value = ld_calc_plt_addr(sdp, ofl); 1201 } 1202 1203 /* 1204 * Add relocations addend to value. Add extra 1205 * relocation addend if needed. 1206 */ 1207 value += arsp->rel_raddend; 1208 if (IS_EXTOFFSET(arsp->rel_rtype)) 1209 value += RELAUX_GET_TYPEDATA(arsp); 1210 1211 /* 1212 * Determine whether the value needs further adjustment. Filter 1213 * through the attributes of the relocation to determine what 1214 * adjustment is required. Note, many of the following cases 1215 * are only applicable when a .got is present. As a .got is 1216 * not generated when a relocatable object is being built, 1217 * any adjustments that require a .got need to be skipped. 1218 */ 1219 if ((arsp->rel_flags & FLG_REL_GOT) && 1220 ((flags & FLG_OF_RELOBJ) == 0)) { 1221 Xword R1addr; 1222 uintptr_t R2addr; 1223 Sword gotndx; 1224 Gotndx *gnp; 1225 Gotref gref; 1226 1227 /* 1228 * Clear the GOT table entry, on SPARC we clear 1229 * the entry and the 'value' if needed is stored 1230 * in an output relocations addend. 1231 * 1232 * Calculate offset into GOT at which to apply 1233 * the relocation. 1234 */ 1235 if (arsp->rel_flags & FLG_REL_DTLS) 1236 gref = GOT_REF_TLSGD; 1237 else if (arsp->rel_flags & FLG_REL_MTLS) 1238 gref = GOT_REF_TLSLD; 1239 else if (arsp->rel_flags & FLG_REL_STLS) 1240 gref = GOT_REF_TLSIE; 1241 else 1242 gref = GOT_REF_GENERIC; 1243 1244 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp); 1245 assert(gnp); 1246 1247 if (arsp->rel_rtype == M_R_DTPOFF) 1248 gotndx = gnp->gn_gotndx + 1; 1249 else 1250 gotndx = gnp->gn_gotndx; 1251 1252 /* LINTED */ 1253 R1addr = (Xword)((-neggotoffset * M_GOT_ENTSIZE) + 1254 (gotndx * M_GOT_ENTSIZE)); 1255 1256 /* 1257 * Add the GOTs data's offset. 1258 */ 1259 R2addr = R1addr + (uintptr_t)osp->os_outdata->d_buf; 1260 1261 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, 1262 ELF_DBG_LD_ACT, M_MACH, SHT_RELA, 1263 arsp, R1addr, value, ld_reloc_sym_name)); 1264 1265 /* 1266 * And do it. 1267 */ 1268 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 1269 *(Xword *)R2addr = ld_bswap_Xword(value); 1270 else 1271 *(Xword *)R2addr = value; 1272 continue; 1273 1274 } else if (IS_GOT_BASED(arsp->rel_rtype) && 1275 ((flags & FLG_OF_RELOBJ) == 0)) { 1276 value -= (ofl->ofl_osgot->os_shdr->sh_addr + 1277 (-neggotoffset * M_GOT_ENTSIZE)); 1278 1279 } else if (IS_PC_RELATIVE(arsp->rel_rtype)) { 1280 value -= refaddr; 1281 1282 } else if (IS_TLS_INS(arsp->rel_rtype) && 1283 IS_GOT_RELATIVE(arsp->rel_rtype) && 1284 ((flags & FLG_OF_RELOBJ) == 0)) { 1285 Gotndx *gnp; 1286 Gotref gref; 1287 1288 if (arsp->rel_flags & FLG_REL_STLS) 1289 gref = GOT_REF_TLSIE; 1290 else if (arsp->rel_flags & FLG_REL_DTLS) 1291 gref = GOT_REF_TLSGD; 1292 else if (arsp->rel_flags & FLG_REL_MTLS) 1293 gref = GOT_REF_TLSLD; 1294 1295 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, gref, ofl, arsp); 1296 assert(gnp); 1297 1298 value = gnp->gn_gotndx * M_GOT_ENTSIZE; 1299 1300 } else if (IS_GOT_RELATIVE(arsp->rel_rtype) && 1301 ((flags & FLG_OF_RELOBJ) == 0)) { 1302 Gotndx *gnp; 1303 1304 gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1305 GOT_REF_GENERIC, ofl, arsp); 1306 assert(gnp); 1307 1308 value = gnp->gn_gotndx * M_GOT_ENTSIZE; 1309 1310 } else if ((arsp->rel_flags & FLG_REL_STLS) && 1311 ((flags & FLG_OF_RELOBJ) == 0)) { 1312 Xword tlsstatsize; 1313 1314 /* 1315 * This is the LE TLS reference model. Static offset is 1316 * hard-coded, and negated so that it can be added to 1317 * the thread pointer (%g7) 1318 */ 1319 tlsstatsize = 1320 S_ROUND(ofl->ofl_tlsphdr->p_memsz, M_TLSSTATALIGN); 1321 value = -(tlsstatsize - value); 1322 } 1323 1324 if (arsp->rel_isdesc->is_file) 1325 ifl_name = arsp->rel_isdesc->is_file->ifl_name; 1326 else 1327 ifl_name = MSG_INTL(MSG_STR_NULL); 1328 1329 /* 1330 * Make sure we have data to relocate. Compiler and assembler 1331 * developers have been known to generate relocations against 1332 * invalid sections (normally .bss), so for their benefit give 1333 * them sufficient information to help analyze the problem. 1334 * End users should never see this. 1335 */ 1336 if (arsp->rel_isdesc->is_indata->d_buf == 0) { 1337 Conv_inv_buf_t inv_buf; 1338 1339 eprintf(ofl->ofl_lml, ERR_FATAL, 1340 MSG_INTL(MSG_REL_EMPTYSEC), 1341 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf), 1342 ifl_name, ld_reloc_sym_name(arsp), 1343 EC_WORD(arsp->rel_isdesc->is_scnndx), 1344 arsp->rel_isdesc->is_name); 1345 return (S_ERROR); 1346 } 1347 1348 /* 1349 * Get the address of the data item we need to modify. 1350 */ 1351 addr = (uchar_t *)((uintptr_t)arsp->rel_roffset + 1352 (uintptr_t)_elf_getxoff(arsp->rel_isdesc->is_indata)); 1353 1354 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT, 1355 M_MACH, SHT_RELA, arsp, EC_NATPTR(addr), value, 1356 ld_reloc_sym_name)); 1357 addr += (uintptr_t)osp->os_outdata->d_buf; 1358 1359 if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 1360 ofl->ofl_size) || (arsp->rel_roffset > 1361 osp->os_shdr->sh_size)) { 1362 Conv_inv_buf_t inv_buf; 1363 int class; 1364 1365 if (((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 1366 ofl->ofl_size) 1367 class = ERR_FATAL; 1368 else 1369 class = ERR_WARNING; 1370 1371 eprintf(ofl->ofl_lml, class, 1372 MSG_INTL(MSG_REL_INVALOFFSET), 1373 conv_reloc_SPARC_type(arsp->rel_rtype, 0, &inv_buf), 1374 ifl_name, EC_WORD(arsp->rel_isdesc->is_scnndx), 1375 arsp->rel_isdesc->is_name, ld_reloc_sym_name(arsp), 1376 EC_ADDR((uintptr_t)addr - 1377 (uintptr_t)ofl->ofl_nehdr)); 1378 1379 if (class == ERR_FATAL) { 1380 return_code = S_ERROR; 1381 continue; 1382 } 1383 } 1384 1385 /* 1386 * If '-z noreloc' is specified - skip the do_reloc stage. 1387 */ 1388 if (OFL_DO_RELOC(ofl)) { 1389 if (do_reloc_ld(arsp, addr, &value, ld_reloc_sym_name, 1390 ifl_name, OFL_SWAP_RELOC_DATA(ofl, arsp), 1391 ofl->ofl_lml) == 0) 1392 return_code = S_ERROR; 1393 } 1394 } 1395 return (return_code); 1396 } 1397 1398 static uintptr_t 1399 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 1400 { 1401 Rel_desc *orsp; 1402 Sym_desc *sdp = rsp->rel_sym; 1403 Conv_inv_buf_t inv_buf; 1404 1405 /* 1406 * Static executables *do not* want any relocations against them. 1407 * Since our engine still creates relocations against a WEAK UNDEFINED 1408 * symbol in a static executable, it's best to disable them here 1409 * instead of through out the relocation code. 1410 */ 1411 if (OFL_IS_STATIC_EXEC(ofl)) 1412 return (1); 1413 1414 /* 1415 * Certain relocations do not make sense in a 64bit shared object, 1416 * if building a shared object do a sanity check on the output 1417 * relocations being created. 1418 */ 1419 if (ofl->ofl_flags & FLG_OF_SHAROBJ) { 1420 Word rtype = rsp->rel_rtype; 1421 /* 1422 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations 1423 * are not relative they make no sense to create in a shared 1424 * object - so emit the proper error message if that occurs. 1425 */ 1426 if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) { 1427 eprintf(ofl->ofl_lml, ERR_FATAL, 1428 MSG_INTL(MSG_REL_UNRELREL), 1429 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf), 1430 rsp->rel_isdesc->is_file->ifl_name, 1431 ld_reloc_sym_name(rsp)); 1432 return (S_ERROR); 1433 } 1434 #if defined(_ELF64) 1435 /* 1436 * Each of the following relocations requires that the 1437 * object being built be loaded in either the upper 32 or 1438 * 44 bit range of memory. Since shared libraries traditionally 1439 * are loaded in the lower range of memory - this isn't going 1440 * to work. 1441 */ 1442 if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) || 1443 (rtype == R_SPARC_L44)) { 1444 eprintf(ofl->ofl_lml, ERR_FATAL, 1445 MSG_INTL(MSG_REL_SHOBJABS44), 1446 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf), 1447 rsp->rel_isdesc->is_file->ifl_name, 1448 ld_reloc_sym_name(rsp)); 1449 return (S_ERROR); 1450 } 1451 #endif 1452 } 1453 1454 /* 1455 * If we are adding a output relocation against a section 1456 * symbol (non-RELATIVE) then mark that section. These sections 1457 * will be added to the .dynsym symbol table. 1458 */ 1459 if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 1460 ((flags & FLG_REL_SCNNDX) || 1461 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 1462 1463 /* 1464 * If this is a COMMON symbol - no output section 1465 * exists yet - (it's created as part of sym_validate()). 1466 * So - we mark here that when it's created it should 1467 * be tagged with the FLG_OS_OUTREL flag. 1468 */ 1469 if ((sdp->sd_flags & FLG_SY_SPECSEC) && 1470 (sdp->sd_sym->st_shndx == SHN_COMMON)) { 1471 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 1472 ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 1473 else 1474 ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 1475 } else { 1476 Os_desc *osp; 1477 Is_desc *isp = sdp->sd_isc; 1478 1479 if (isp && ((osp = isp->is_osdesc) != NULL) && 1480 ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 1481 ofl->ofl_dynshdrcnt++; 1482 osp->os_flags |= FLG_OS_OUTREL; 1483 } 1484 } 1485 } 1486 1487 /* Enter it into the output relocation cache */ 1488 if ((orsp = ld_reloc_enter(ofl, &ofl->ofl_outrels, rsp, flags)) == NULL) 1489 return (S_ERROR); 1490 1491 if (flags & FLG_REL_GOT) 1492 ofl->ofl_relocgotsz += (Xword)sizeof (Rela); 1493 else if (flags & FLG_REL_PLT) 1494 ofl->ofl_relocpltsz += (Xword)sizeof (Rela); 1495 else if (flags & FLG_REL_BSS) 1496 ofl->ofl_relocbsssz += (Xword)sizeof (Rela); 1497 else if (flags & FLG_REL_NOINFO) 1498 ofl->ofl_relocrelsz += (Xword)sizeof (Rela); 1499 else 1500 RELAUX_GET_OSDESC(orsp)->os_szoutrels += (Xword)sizeof (Rela); 1501 1502 if (orsp->rel_rtype == M_R_RELATIVE) 1503 ofl->ofl_relocrelcnt++; 1504 1505 #if defined(_ELF64) 1506 /* 1507 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given 1508 * a plt padding entry, unless we're building a relocatable object 1509 * (ld -r) or -b is in effect. 1510 */ 1511 if ((orsp->rel_rtype == R_SPARC_WDISP30) && 1512 ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) && 1513 ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) { 1514 ofl->ofl_pltpad++; 1515 orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD; 1516 } 1517 #endif 1518 /* 1519 * We don't perform sorting on PLT relocations because 1520 * they have already been assigned a PLT index and if we 1521 * were to sort them we would have to re-assign the plt indexes. 1522 */ 1523 if (!(flags & FLG_REL_PLT)) 1524 ofl->ofl_reloccnt++; 1525 1526 /* 1527 * Insure a GLOBAL_OFFSET_TABLE is generated if required. 1528 */ 1529 if (IS_GOT_REQUIRED(orsp->rel_rtype)) 1530 ofl->ofl_flags |= FLG_OF_BLDGOT; 1531 1532 /* 1533 * Identify and possibly warn of a displacement relocation. 1534 */ 1535 if (orsp->rel_flags & FLG_REL_DISP) { 1536 ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 1537 1538 if (ofl->ofl_flags & FLG_OF_VERBOSE) 1539 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 1540 } 1541 DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA, 1542 M_MACH, orsp)); 1543 return (1); 1544 } 1545 1546 /* 1547 * Process relocation against a register symbol. Note, of -z muldefs is in 1548 * effect there may have been multiple register definitions, which would have 1549 * been processed as non-fatal, with the first definition winning. But, we 1550 * will also process multiple relocations for these multiple definitions. In 1551 * this case we must only preserve the relocation for the definition that was 1552 * kept. The sad part is that register relocations don't typically specify 1553 * the register symbol with which they are associated, so we might have to 1554 * search the input files global symbols to determine if this relocation is 1555 * appropriate. 1556 */ 1557 static uintptr_t 1558 ld_reloc_register(Rel_desc *rsp, Is_desc *isp, Ofl_desc *ofl) 1559 { 1560 if (ofl->ofl_flags & FLG_OF_MULDEFS) { 1561 Ifl_desc *ifl = isp->is_file; 1562 Sym_desc *sdp = rsp->rel_sym; 1563 1564 if (sdp == 0) { 1565 Xword offset = rsp->rel_roffset; 1566 Word ndx; 1567 1568 for (ndx = ifl->ifl_locscnt; 1569 ndx < ifl->ifl_symscnt; ndx++) { 1570 if (((sdp = ifl->ifl_oldndx[ndx]) != 0) && 1571 (sdp->sd_flags & FLG_SY_REGSYM) && 1572 (sdp->sd_sym->st_value == offset)) 1573 break; 1574 } 1575 } 1576 if (sdp && (sdp->sd_file != ifl)) 1577 return (1); 1578 } 1579 return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl)); 1580 } 1581 1582 /* 1583 * process relocation for a LOCAL symbol 1584 */ 1585 static uintptr_t 1586 ld_reloc_local(Rel_desc *rsp, Ofl_desc *ofl) 1587 { 1588 ofl_flag_t flags = ofl->ofl_flags; 1589 Sym_desc *sdp = rsp->rel_sym; 1590 Word shndx = sdp->sd_sym->st_shndx; 1591 1592 /* 1593 * if ((shared object) and (not pc relative relocation) and 1594 * (not against ABS symbol)) 1595 * then 1596 * if (rtype != R_SPARC_32) 1597 * then 1598 * build relocation against section 1599 * else 1600 * build R_SPARC_RELATIVE 1601 * fi 1602 * fi 1603 */ 1604 if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 1605 !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 1606 !(IS_GOT_BASED(rsp->rel_rtype)) && 1607 !(rsp->rel_isdesc != NULL && 1608 (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 1609 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 1610 (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 1611 Word ortype = rsp->rel_rtype; 1612 1613 if ((rsp->rel_rtype != R_SPARC_32) && 1614 (rsp->rel_rtype != R_SPARC_PLT32) && 1615 (rsp->rel_rtype != R_SPARC_64)) 1616 return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL), 1617 rsp, ofl)); 1618 1619 rsp->rel_rtype = R_SPARC_RELATIVE; 1620 if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR) 1621 return (S_ERROR); 1622 rsp->rel_rtype = ortype; 1623 return (1); 1624 } 1625 1626 /* 1627 * If the relocation is against a 'non-allocatable' section 1628 * and we can not resolve it now - then give a warning 1629 * message. 1630 * 1631 * We can not resolve the symbol if either: 1632 * a) it's undefined 1633 * b) it's defined in a shared library and a 1634 * COPY relocation hasn't moved it to the executable 1635 * 1636 * Note: because we process all of the relocations against the 1637 * text segment before any others - we know whether 1638 * or not a copy relocation will be generated before 1639 * we get here (see reloc_init()->reloc_segments()). 1640 */ 1641 if (!(rsp->rel_flags & FLG_REL_LOAD) && 1642 ((shndx == SHN_UNDEF) || 1643 ((sdp->sd_ref == REF_DYN_NEED) && 1644 ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 1645 Conv_inv_buf_t inv_buf; 1646 Os_desc *osp = RELAUX_GET_OSDESC(rsp); 1647 1648 /* 1649 * If the relocation is against a SHT_SUNW_ANNOTATE 1650 * section - then silently ignore that the relocation 1651 * can not be resolved. 1652 */ 1653 if (osp && (osp->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 1654 return (0); 1655 (void) eprintf(ofl->ofl_lml, ERR_WARNING, 1656 MSG_INTL(MSG_REL_EXTERNSYM), 1657 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf), 1658 rsp->rel_isdesc->is_file->ifl_name, 1659 ld_reloc_sym_name(rsp), osp->os_name); 1660 return (1); 1661 } 1662 1663 /* 1664 * Perform relocation. 1665 */ 1666 return (ld_add_actrel(NULL, rsp, ofl)); 1667 } 1668 1669 /* 1670 * Establish a relocation transition. Note, at this point of input relocation 1671 * processing, we have no idea of the relocation value that will be used in 1672 * the eventual relocation calculation. This value is only known after the 1673 * initial image has been constructed. Therefore, there is a small chance 1674 * that a value can exceed the capabilities of the transitioned relocation. 1675 * One example might be the offset from the GOT to a symbol. 1676 * 1677 * The only instance of this failure discovered so far has been via the use of 1678 * ABS symbols to represent an external memory location. This situation is 1679 * rare, since ABS symbols aren't typically generated by the compilers. 1680 * Therefore, our solution is to excluded ABS symbols from the transition 1681 * relocation possibilities. As an additional safeguard, if an inappropriate 1682 * value is passed to the final relocation engine, a verification ("V") 1683 * relocation should trigger a fatal error condition. 1684 */ 1685 static uintptr_t 1686 ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl) 1687 { 1688 Word rtype = rsp->rel_rtype; 1689 1690 if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) { 1691 /* 1692 * When binding to a external symbol, no fixups are required 1693 * and the GOTDATA_OP relocation can be ignored. 1694 */ 1695 if (rtype == R_SPARC_GOTDATA_OP) 1696 return (1); 1697 return (ld_reloc_GOT_relative(local, rsp, ofl)); 1698 } 1699 1700 /* 1701 * When binding to a local symbol the relocations can be transitioned: 1702 * 1703 * R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22 1704 * R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10 1705 * R_*_GOTDATA_OP -> instruction fixup 1706 */ 1707 return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl)); 1708 } 1709 1710 static uintptr_t 1711 ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl) 1712 { 1713 Word rtype = rsp->rel_rtype; 1714 Sym_desc *sdp = rsp->rel_sym; 1715 ofl_flag_t flags = ofl->ofl_flags; 1716 Gotndx *gnp; 1717 1718 /* 1719 * If we're building an executable - use either the IE or LE access 1720 * model. If we're building a shared object process any IE model. 1721 */ 1722 if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 1723 /* 1724 * Set the DF_STATIC_TLS flag. 1725 */ 1726 ofl->ofl_dtflags |= DF_STATIC_TLS; 1727 1728 if (!local || ((flags & FLG_OF_EXEC) == 0)) { 1729 /* 1730 * When processing static TLS - these relocations 1731 * can be ignored. 1732 */ 1733 if ((rtype == R_SPARC_TLS_IE_LD) || 1734 (rtype == R_SPARC_TLS_IE_LDX) || 1735 (rtype == R_SPARC_TLS_IE_ADD)) 1736 return (1); 1737 1738 /* 1739 * Assign a GOT entry for IE static TLS references. 1740 */ 1741 if (((rtype == R_SPARC_TLS_GD_HI22) || 1742 (rtype == R_SPARC_TLS_GD_LO10) || 1743 (rtype == R_SPARC_TLS_IE_HI22) || 1744 (rtype == R_SPARC_TLS_IE_LO10)) && 1745 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1746 GOT_REF_TLSIE, ofl, rsp)) == NULL)) { 1747 1748 if (ld_assign_got_TLS(local, rsp, ofl, sdp, 1749 gnp, GOT_REF_TLSIE, FLG_REL_STLS, 1750 rtype, M_R_TPOFF, NULL) == S_ERROR) 1751 return (S_ERROR); 1752 } 1753 1754 /* 1755 * IE access model. 1756 */ 1757 if (IS_TLS_IE(rtype)) 1758 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1759 1760 /* 1761 * Fixups are required for other executable models. 1762 */ 1763 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1764 rsp, ofl)); 1765 } 1766 1767 /* 1768 * LE access model. 1769 */ 1770 if (IS_TLS_LE(rtype)) 1771 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1772 1773 /* 1774 * When processing static TLS - these relocations can be 1775 * ignored. 1776 */ 1777 if (rtype == R_SPARC_TLS_IE_ADD) 1778 return (1); 1779 1780 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1781 rsp, ofl)); 1782 } 1783 1784 /* 1785 * Building a shared object. 1786 * 1787 * For dynamic TLS references, ADD relocations are ignored. 1788 */ 1789 if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) || 1790 (rtype == R_SPARC_TLS_LDO_ADD)) 1791 return (1); 1792 1793 /* 1794 * Assign a GOT entry for a dynamic TLS reference. 1795 */ 1796 if (((rtype == R_SPARC_TLS_LDM_HI22) || 1797 (rtype == R_SPARC_TLS_LDM_LO10)) && 1798 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSLD, 1799 ofl, rsp)) == NULL)) { 1800 1801 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 1802 FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR) 1803 return (S_ERROR); 1804 1805 } else if (((rtype == R_SPARC_TLS_GD_HI22) || 1806 (rtype == R_SPARC_TLS_GD_LO10)) && 1807 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD, 1808 ofl, rsp)) == NULL)) { 1809 1810 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 1811 FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR) 1812 return (S_ERROR); 1813 } 1814 1815 /* 1816 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually 1817 * cause a call to __tls_get_addr(). Convert this relocation to that 1818 * symbol now, and prepare for the PLT magic. 1819 */ 1820 if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) { 1821 Sym_desc *tlsgetsym; 1822 1823 if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U), 1824 ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR) 1825 return (S_ERROR); 1826 1827 rsp->rel_sym = tlsgetsym; 1828 rsp->rel_rtype = R_SPARC_WPLT30; 1829 1830 if (ld_reloc_plt(rsp, ofl) == S_ERROR) 1831 return (S_ERROR); 1832 1833 rsp->rel_sym = sdp; 1834 rsp->rel_rtype = rtype; 1835 return (1); 1836 } 1837 1838 if (IS_TLS_LD(rtype)) 1839 return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 1840 1841 return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 1842 } 1843 1844 /* 1845 * ld_allocate_got: if a GOT is to be made, after the section is built this 1846 * function is called to allocate all the GOT slots. The allocation is 1847 * deferred until after all GOTs have been counted and sorted according 1848 * to their size, for only then will we know how to allocate them on 1849 * a processor like SPARC which has different models for addressing the 1850 * GOT. SPARC has two: small and large, small uses a signed 13-bit offset 1851 * into the GOT, whereas large uses an unsigned 32-bit offset. 1852 */ 1853 static Sword small_index; /* starting index for small GOT entries */ 1854 static Sword mixed_index; /* starting index for mixed GOT entries */ 1855 static Sword large_index; /* starting index for large GOT entries */ 1856 1857 static uintptr_t 1858 ld_assign_got(Ofl_desc *ofl, Sym_desc *sdp) 1859 { 1860 Aliste idx; 1861 Gotndx *gnp; 1862 1863 for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx, gnp)) { 1864 uint_t gotents; 1865 Gotref gref = gnp->gn_gotref; 1866 1867 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 1868 gotents = 2; 1869 else 1870 gotents = 1; 1871 1872 switch (gnp->gn_gotndx) { 1873 case M_GOT_SMALL: 1874 gnp->gn_gotndx = small_index; 1875 small_index += gotents; 1876 if (small_index == 0) 1877 small_index = M_GOT_XNumber; 1878 break; 1879 case M_GOT_MIXED: 1880 gnp->gn_gotndx = mixed_index; 1881 mixed_index += gotents; 1882 break; 1883 case M_GOT_LARGE: 1884 gnp->gn_gotndx = large_index; 1885 large_index += gotents; 1886 break; 1887 default: 1888 eprintf(ofl->ofl_lml, ERR_FATAL, 1889 MSG_INTL(MSG_REL_ASSIGNGOT), 1890 EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name)); 1891 return (S_ERROR); 1892 } 1893 } 1894 return (1); 1895 } 1896 1897 static uintptr_t 1898 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl, 1899 Rel_desc *rsp, Sym_desc *sdp) 1900 { 1901 Xword raddend; 1902 Gotndx gn, *gnp; 1903 Aliste idx; 1904 uint_t gotents; 1905 1906 /* Some TLS requires two relocations with two GOT entries */ 1907 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 1908 gotents = 2; 1909 else 1910 gotents = 1; 1911 1912 raddend = rsp->rel_raddend; 1913 if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) { 1914 1915 /* 1916 * If an entry for this addend already exists, determine if it 1917 * has mixed mode GOT access (both PIC and pic). 1918 * 1919 * In order to be accessible by both large and small pic, 1920 * a mixed mode GOT must be located in the positive index 1921 * range above _GLOBAL_OFFSET_TABLE_, and in the range 1922 * reachable small pic. This is necessary because the large 1923 * PIC mode cannot use a negative offset. This implies that 1924 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber) 1925 * such entries. 1926 */ 1927 switch (pgnp->gn_gotndx) { 1928 case M_GOT_SMALL: 1929 /* 1930 * This one was previously identified as a small 1931 * GOT. If this access is large, then convert 1932 * it to mixed. 1933 */ 1934 if (rsp->rel_rtype != R_SPARC_GOT13) { 1935 pgnp->gn_gotndx = M_GOT_MIXED; 1936 mixgotcnt += gotents; 1937 } 1938 break; 1939 1940 case M_GOT_LARGE: 1941 /* 1942 * This one was previously identified as a large 1943 * GOT. If this access is small, convert it to mixed. 1944 */ 1945 if (rsp->rel_rtype == R_SPARC_GOT13) { 1946 smlgotcnt += gotents; 1947 mixgotcnt += gotents; 1948 pgnp->gn_gotndx = M_GOT_MIXED; 1949 sdp->sd_flags |= FLG_SY_SMGOT; 1950 } 1951 break; 1952 } 1953 return (1); 1954 } 1955 1956 gn.gn_addend = raddend; 1957 gn.gn_gotref = gref; 1958 1959 if (rsp->rel_rtype == R_SPARC_GOT13) { 1960 gn.gn_gotndx = M_GOT_SMALL; 1961 smlgotcnt += gotents; 1962 sdp->sd_flags |= FLG_SY_SMGOT; 1963 } else 1964 gn.gn_gotndx = M_GOT_LARGE; 1965 1966 ofl->ofl_gotcnt += gotents; 1967 1968 if (gref == GOT_REF_TLSLD) { 1969 if (ofl->ofl_tlsldgotndx == NULL) { 1970 if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL) 1971 return (S_ERROR); 1972 (void) memcpy(gnp, &gn, sizeof (Gotndx)); 1973 ofl->ofl_tlsldgotndx = gnp; 1974 } 1975 return (1); 1976 } 1977 1978 idx = 0; 1979 for (ALIST_TRAVERSE(*alpp, idx, gnp)) { 1980 if (gnp->gn_addend > raddend) 1981 break; 1982 } 1983 1984 /* 1985 * GOT indexes are maintained on an Alist, where there is typically 1986 * only one index. The usage of this list is to scan the list to find 1987 * an index, and then apply that index immediately to a relocation. 1988 * Thus there are no external references to these GOT index structures 1989 * that can be compromised by the Alist being reallocated. 1990 */ 1991 if (alist_insert(alpp, &gn, sizeof (Gotndx), 1992 AL_CNT_SDP_GOT, idx) == NULL) 1993 return (S_ERROR); 1994 1995 return (1); 1996 } 1997 1998 static void 1999 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 2000 { 2001 sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 2002 } 2003 2004 2005 static uintptr_t 2006 ld_allocate_got(Ofl_desc * ofl) 2007 { 2008 const Sword first_large_ndx = M_GOT_MAXSMALL / 2; 2009 Sym_desc *sdp; 2010 Addr addr; 2011 2012 /* 2013 * Sanity check -- is this going to fit at all? There are two 2014 * limits to be concerned about: 2015 * 1) There is a limit on the number of small pic GOT indices, 2016 * given by M_GOT_MAXSMALL. 2017 * 2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber) 2018 * small GOT indices, there will be items at negative 2019 * offsets from _GLOBAL_OFFSET_TABLE_. Items that are 2020 * accessed via large (PIC) code cannot reach these 2021 * negative slots, so mixed mode items must be in the 2022 * non-negative range. This implies a limit of 2023 * (M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices. 2024 */ 2025 if (smlgotcnt > M_GOT_MAXSMALL) { 2026 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT), 2027 EC_WORD(smlgotcnt), M_GOT_MAXSMALL); 2028 return (S_ERROR); 2029 } 2030 if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) { 2031 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT), 2032 EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber); 2033 return (S_ERROR); 2034 } 2035 2036 /* 2037 * Set starting offset to be either 0, or a negative index into 2038 * the GOT based on the number of small symbols we've got. 2039 */ 2040 neggotoffset = ((smlgotcnt >= first_large_ndx) ? 2041 (first_large_ndx - smlgotcnt) : 0); 2042 2043 /* 2044 * Initialize the got offsets used by assign_got() to 2045 * locate GOT items: 2046 * small - Starting index of items referenced only 2047 * by small offsets (-Kpic). 2048 * mixed - Starting index of items referenced 2049 * by both large (-KPIC) and small (-Kpic). 2050 * large - Indexes referenced only by large (-KPIC) 2051 * 2052 * Small items can have negative indexes (i.e. lie below 2053 * _GLOBAL_OFFSET_TABLE_). Mixed and large items must have 2054 * non-negative offsets. 2055 */ 2056 small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset; 2057 large_index = neggotoffset + smlgotcnt; 2058 mixed_index = large_index - mixgotcnt; 2059 2060 /* 2061 * Assign bias to GOT symbols. 2062 */ 2063 addr = -neggotoffset * M_GOT_ENTSIZE; 2064 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH, 2065 NULL, ofl)) != NULL) 2066 sdp->sd_sym->st_value = addr; 2067 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 2068 NULL, ofl)) != NULL) 2069 sdp->sd_sym->st_value = addr; 2070 2071 if (ofl->ofl_tlsldgotndx) { 2072 ofl->ofl_tlsldgotndx->gn_gotndx = large_index; 2073 large_index += 2; 2074 } 2075 return (1); 2076 } 2077 2078 /* 2079 * Initializes .got[0] with the _DYNAMIC symbol value. 2080 */ 2081 static uintptr_t 2082 ld_fillin_gotplt(Ofl_desc *ofl) 2083 { 2084 if (ofl->ofl_osgot) { 2085 Sym_desc *sdp; 2086 2087 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_DYNAMIC_U), 2088 SYM_NOHASH, NULL, ofl)) != NULL) { 2089 uchar_t *genptr; 2090 2091 genptr = ((uchar_t *)ofl->ofl_osgot->os_outdata->d_buf + 2092 (-neggotoffset * M_GOT_ENTSIZE) + 2093 (M_GOT_XDYNAMIC * M_GOT_ENTSIZE)); 2094 /* LINTED */ 2095 *((Xword *)genptr) = sdp->sd_sym->st_value; 2096 if (ofl->ofl_flags1 & FLG_OF1_ENCDIFF) 2097 /* LINTED */ 2098 *((Xword *)genptr) = 2099 /* LINTED */ 2100 ld_bswap_Xword(*((Xword *)genptr)); 2101 } 2102 } 2103 return (1); 2104 } 2105 2106 2107 2108 /* 2109 * Template for generating "void (*)(void)" function 2110 */ 2111 static const uchar_t nullfunc_tmpl[] = { 2112 /* 0x00 */ 0x81, 0xc3, 0xe0, 0x08, /* retl */ 2113 /* 0x04 */ 0x01, 0x00, 0x00, 0x00 /* nop */ 2114 }; 2115 2116 2117 2118 /* 2119 * Return the ld_targ definition for this target. 2120 */ 2121 const Target * 2122 ld_targ_init_sparc(void) 2123 { 2124 static const Target _ld_targ = { 2125 { /* Target_mach */ 2126 M_MACH, /* m_mach */ 2127 M_MACHPLUS, /* m_machplus */ 2128 M_FLAGSPLUS, /* m_flagsplus */ 2129 M_CLASS, /* m_class */ 2130 M_DATA, /* m_data */ 2131 2132 M_SEGM_ALIGN, /* m_segm_align */ 2133 M_SEGM_ORIGIN, /* m_segm_origin */ 2134 M_SEGM_AORIGIN, /* m_segm_aorigin */ 2135 M_DATASEG_PERM, /* m_dataseg_perm */ 2136 M_STACK_PERM, /* m_stack_perm */ 2137 M_WORD_ALIGN, /* m_word_align */ 2138 /* m_def_interp */ 2139 #if defined(_ELF64) 2140 MSG_ORIG(MSG_PTH_RTLD_SPARCV9), 2141 #else 2142 MSG_ORIG(MSG_PTH_RTLD), 2143 #endif 2144 2145 /* Relocation type codes */ 2146 M_R_ARRAYADDR, /* m_r_arrayaddr */ 2147 M_R_COPY, /* m_r_copy */ 2148 M_R_GLOB_DAT, /* m_r_glob_dat */ 2149 M_R_JMP_SLOT, /* m_r_jmp_slot */ 2150 M_R_NUM, /* m_r_num */ 2151 M_R_NONE, /* m_r_none */ 2152 M_R_RELATIVE, /* m_r_relative */ 2153 M_R_REGISTER, /* m_r_register */ 2154 2155 /* Relocation related constants */ 2156 M_REL_DT_COUNT, /* m_rel_dt_count */ 2157 M_REL_DT_ENT, /* m_rel_dt_ent */ 2158 M_REL_DT_SIZE, /* m_rel_dt_size */ 2159 M_REL_DT_TYPE, /* m_rel_dt_type */ 2160 M_REL_SHT_TYPE, /* m_rel_sht_type */ 2161 2162 /* GOT related constants */ 2163 M_GOT_ENTSIZE, /* m_got_entsize */ 2164 M_GOT_XNumber, /* m_got_xnumber */ 2165 2166 /* PLT related constants */ 2167 M_PLT_ALIGN, /* m_plt_align */ 2168 M_PLT_ENTSIZE, /* m_plt_entsize */ 2169 M_PLT_RESERVSZ, /* m_plt_reservsz */ 2170 M_PLT_SHF_FLAGS, /* m_plt_shf_flags */ 2171 2172 /* Section type of .eh_frame/.eh_frame_hdr sections */ 2173 SHT_PROGBITS, /* m_sht_unwind */ 2174 2175 M_DT_REGISTER, /* m_dt_register */ 2176 }, 2177 { /* Target_machid */ 2178 M_ID_ARRAY, /* id_array */ 2179 M_ID_BSS, /* id_bss */ 2180 M_ID_CAP, /* id_cap */ 2181 M_ID_CAPINFO, /* id_capinfo */ 2182 M_ID_CAPCHAIN, /* id_capchain */ 2183 M_ID_DATA, /* id_data */ 2184 M_ID_DYNAMIC, /* id_dynamic */ 2185 M_ID_DYNSORT, /* id_dynsort */ 2186 M_ID_DYNSTR, /* id_dynstr */ 2187 M_ID_DYNSYM, /* id_dynsym */ 2188 M_ID_DYNSYM_NDX, /* id_dynsym_ndx */ 2189 M_ID_GOT, /* id_got */ 2190 M_ID_GOTDATA, /* id_gotdata */ 2191 M_ID_HASH, /* id_hash */ 2192 M_ID_INTERP, /* id_interp */ 2193 M_ID_UNKNOWN, /* id_lbss (unused) */ 2194 M_ID_LDYNSYM, /* id_ldynsym */ 2195 M_ID_NOTE, /* id_note */ 2196 M_ID_NULL, /* id_null */ 2197 M_ID_PLT, /* id_plt */ 2198 M_ID_REL, /* id_rel */ 2199 M_ID_STRTAB, /* id_strtab */ 2200 M_ID_SYMINFO, /* id_syminfo */ 2201 M_ID_SYMTAB, /* id_symtab */ 2202 M_ID_SYMTAB_NDX, /* id_symtab_ndx */ 2203 M_ID_TEXT, /* id_text */ 2204 M_ID_TLS, /* id_tls */ 2205 M_ID_TLSBSS, /* id_tlsbss */ 2206 M_ID_UNKNOWN, /* id_unknown */ 2207 M_ID_UNWIND, /* id_unwind */ 2208 M_ID_UNWINDHDR, /* id_unwindhdr */ 2209 M_ID_USER, /* id_user */ 2210 M_ID_VERSION, /* id_version */ 2211 }, 2212 { /* Target_nullfunc */ 2213 nullfunc_tmpl, /* nf_template */ 2214 sizeof (nullfunc_tmpl), /* nf_size */ 2215 }, 2216 { /* Target_fillfunc */ 2217 /* 2218 * On sparc, special filling of executable sections 2219 * is undesirable, and the default 0 fill supplied 2220 * by libelf is preferred: 2221 * 2222 * - 0 fill is interpreted as UNIMP instructions, 2223 * which cause an illegal_instruction_trap. These 2224 * serve as a sentinel against poorly written 2225 * code. The sparc architecture manual discusses 2226 * this as providing a measure of runtime safety. 2227 * 2228 * - The one place where a hole should conceivably 2229 * be filled with NOP instructions is in the 2230 * .init/.fini sections. However, the sparc 2231 * assembler sizes the sections it generates 2232 * to a multiple of the section alignment, and as 2233 * such, takes the filling task out of our hands. 2234 * Furthermore, the sparc assembler uses 0-fill 2235 * for this, forcing the authors of sparc 2236 * assembler for .init/.fini sections to be aware 2237 * of this case and explicitly supply NOP fill. 2238 * Hence, there is no role for the link-editor. 2239 */ 2240 NULL /* ff_execfill */ 2241 }, 2242 { /* Target_machrel */ 2243 reloc_table, 2244 2245 ld_init_rel, /* mr_init_rel */ 2246 ld_mach_eflags, /* mr_mach_eflags */ 2247 ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 2248 ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 2249 ld_calc_plt_addr, /* mr_calc_plt_addr */ 2250 ld_perform_outreloc, /* mr_perform_outreloc */ 2251 ld_do_activerelocs, /* mr_do_activerelocs */ 2252 ld_add_outrel, /* mr_add_outrel */ 2253 ld_reloc_register, /* mr_reloc_register */ 2254 ld_reloc_local, /* mr_reloc_local */ 2255 ld_reloc_GOTOP, /* mr_reloc_GOTOP */ 2256 ld_reloc_TLS, /* mr_reloc_TLS */ 2257 ld_assign_got, /* mr_assign_got */ 2258 ld_find_got_ndx, /* mr_find_got_ndx */ 2259 ld_calc_got_offset, /* mr_calc_got_offset */ 2260 ld_assign_got_ndx, /* mr_assign_got_ndx */ 2261 ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 2262 ld_allocate_got, /* mr_allocate_got */ 2263 ld_fillin_gotplt, /* mr_fillin_gotplt */ 2264 }, 2265 { /* Target_machsym */ 2266 ld_reg_check_sparc, /* ms_reg_check */ 2267 ld_mach_sym_typecheck_sparc, /* ms_mach_sym_typecheck */ 2268 ld_is_regsym_sparc, /* ms_is_regsym */ 2269 ld_reg_find_sparc, /* ms_reg_find */ 2270 ld_reg_enter_sparc /* ms_reg_enter */ 2271 } 2272 }; 2273 2274 return (&_ld_targ); 2275 } 2276