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