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_ACT, 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 DBG_CALL(Dbg_reloc_doact(ofl->ofl_lml, ELF_DBG_LD_ACT, 1372 M_MACH, SHT_RELA, arsp->rel_rtype, EC_NATPTR(addr), 1373 value, arsp->rel_sname, arsp->rel_osdesc)); 1374 addr += (uintptr_t)arsp->rel_osdesc->os_outdata->d_buf; 1375 1376 if ((((uintptr_t)addr - (uintptr_t)ofl->ofl_nehdr) > 1377 ofl->ofl_size) || (arsp->rel_roffset > 1378 arsp->rel_osdesc->os_shdr->sh_size)) { 1379 Conv_inv_buf_t inv_buf; 1380 int class; 1381 1382 if (((uintptr_t)addr - 1383 (uintptr_t)ofl->ofl_nehdr) > ofl->ofl_size) 1384 class = ERR_FATAL; 1385 else 1386 class = ERR_WARNING; 1387 1388 eprintf(ofl->ofl_lml, class, 1389 MSG_INTL(MSG_REL_INVALOFFSET), 1390 conv_reloc_SPARC_type(arsp->rel_rtype, 1391 0, &inv_buf), ifl_name, 1392 EC_WORD(arsp->rel_isdesc->is_scnndx), 1393 arsp->rel_isdesc->is_name, 1394 demangle(arsp->rel_sname), 1395 EC_ADDR((uintptr_t)addr - 1396 (uintptr_t)ofl->ofl_nehdr)); 1397 1398 if (class == ERR_FATAL) { 1399 return_code = S_ERROR; 1400 continue; 1401 } 1402 } 1403 1404 /* 1405 * If '-z noreloc' is specified - skip the do_reloc 1406 * stage. 1407 */ 1408 if (OFL_DO_RELOC(ofl)) { 1409 if (do_reloc_ld((uchar_t)arsp->rel_rtype, addr, 1410 &value, arsp->rel_sname, ifl_name, 1411 OFL_SWAP_RELOC_DATA(ofl, arsp), 1412 ofl->ofl_lml) == 0) 1413 return_code = S_ERROR; 1414 } 1415 } 1416 } 1417 return (return_code); 1418 } 1419 1420 static uintptr_t 1421 ld_add_outrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 1422 { 1423 Rel_desc *orsp; 1424 Rel_cache *rcp; 1425 Sym_desc *sdp = rsp->rel_sym; 1426 static size_t nextsize = 0; 1427 Conv_inv_buf_t inv_buf; 1428 1429 /* 1430 * Static executables *do not* want any relocations against them. 1431 * Since our engine still creates relocations against a WEAK UNDEFINED 1432 * symbol in a static executable, it's best to disable them here 1433 * instead of through out the relocation code. 1434 */ 1435 if (OFL_IS_STATIC_EXEC(ofl)) 1436 return (1); 1437 1438 /* 1439 * Certain relocations do not make sense in a 64bit shared object, 1440 * if building a shared object do a sanity check on the output 1441 * relocations being created. 1442 */ 1443 if (ofl->ofl_flags & FLG_OF_SHAROBJ) { 1444 Word rtype = rsp->rel_rtype; 1445 /* 1446 * Because the R_SPARC_HIPLT22 & R_SPARC_LOPLT10 relocations 1447 * are not relative they make no sense to create in a shared 1448 * object - so emit the proper error message if that occurs. 1449 */ 1450 if ((rtype == R_SPARC_HIPLT22) || (rtype == R_SPARC_LOPLT10)) { 1451 eprintf(ofl->ofl_lml, ERR_FATAL, 1452 MSG_INTL(MSG_REL_UNRELREL), 1453 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf), 1454 rsp->rel_isdesc->is_file->ifl_name, 1455 demangle(rsp->rel_sname)); 1456 return (S_ERROR); 1457 } 1458 #if defined(_ELF64) 1459 /* 1460 * Each of the following relocations requires that the 1461 * object being built be loaded in either the upper 32 or 1462 * 44 bit range of memory. Since shared libraries traditionally 1463 * are loaded in the lower range of memory - this isn't going 1464 * to work. 1465 */ 1466 if ((rtype == R_SPARC_H44) || (rtype == R_SPARC_M44) || 1467 (rtype == R_SPARC_L44)) { 1468 eprintf(ofl->ofl_lml, ERR_FATAL, 1469 MSG_INTL(MSG_REL_SHOBJABS44), 1470 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf), 1471 rsp->rel_isdesc->is_file->ifl_name, 1472 demangle(rsp->rel_sname)); 1473 return (S_ERROR); 1474 } 1475 #endif 1476 } 1477 1478 /* 1479 * Obtain the new available relocation cache entry. 1480 */ 1481 if ((rcp = ld_add_rel_cache(ofl, &ofl->ofl_outrels, &nextsize, 1482 REL_LOIDESCNO, REL_HOIDESCNO)) == (Rel_cache *)S_ERROR) 1483 return (S_ERROR); 1484 1485 orsp = rcp->rc_free; 1486 1487 /* 1488 * If we are adding a output relocation against a section 1489 * symbol (non-RELATIVE) then mark that section. These sections 1490 * will be added to the .dynsym symbol table. 1491 */ 1492 if (sdp && (rsp->rel_rtype != M_R_RELATIVE) && 1493 ((flags & FLG_REL_SCNNDX) || 1494 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION))) { 1495 1496 /* 1497 * If this is a COMMON symbol - no output section 1498 * exists yet - (it's created as part of sym_validate()). 1499 * So - we mark here that when it's created it should 1500 * be tagged with the FLG_OS_OUTREL flag. 1501 */ 1502 if ((sdp->sd_flags & FLG_SY_SPECSEC) && 1503 (sdp->sd_sym->st_shndx == SHN_COMMON)) { 1504 if (ELF_ST_TYPE(sdp->sd_sym->st_info) != STT_TLS) 1505 ofl->ofl_flags1 |= FLG_OF1_BSSOREL; 1506 else 1507 ofl->ofl_flags1 |= FLG_OF1_TLSOREL; 1508 } else { 1509 Os_desc *osp = sdp->sd_isc->is_osdesc; 1510 1511 if (osp && ((osp->os_flags & FLG_OS_OUTREL) == 0)) { 1512 ofl->ofl_dynshdrcnt++; 1513 osp->os_flags |= FLG_OS_OUTREL; 1514 } 1515 } 1516 } 1517 1518 *orsp = *rsp; 1519 orsp->rel_flags |= flags; 1520 1521 rcp->rc_free++; 1522 ofl->ofl_outrelscnt++; 1523 1524 if (flags & FLG_REL_GOT) 1525 ofl->ofl_relocgotsz += (Xword)sizeof (Rela); 1526 else if (flags & FLG_REL_PLT) 1527 ofl->ofl_relocpltsz += (Xword)sizeof (Rela); 1528 else if (flags & FLG_REL_BSS) 1529 ofl->ofl_relocbsssz += (Xword)sizeof (Rela); 1530 else if (flags & FLG_REL_NOINFO) 1531 ofl->ofl_relocrelsz += (Xword)sizeof (Rela); 1532 else 1533 orsp->rel_osdesc->os_szoutrels += (Xword)sizeof (Rela); 1534 1535 if (orsp->rel_rtype == M_R_RELATIVE) 1536 ofl->ofl_relocrelcnt++; 1537 1538 #if defined(_ELF64) 1539 /* 1540 * When building a 64-bit object any R_SPARC_WDISP30 relocation is given 1541 * a plt padding entry, unless we're building a relocatable object 1542 * (ld -r) or -b is in effect. 1543 */ 1544 if ((orsp->rel_rtype == R_SPARC_WDISP30) && 1545 ((ofl->ofl_flags & (FLG_OF_BFLAG | FLG_OF_RELOBJ)) == 0) && 1546 ((orsp->rel_sym->sd_flags & FLG_SY_PLTPAD) == 0)) { 1547 ofl->ofl_pltpad++; 1548 orsp->rel_sym->sd_flags |= FLG_SY_PLTPAD; 1549 } 1550 #endif 1551 /* 1552 * We don't perform sorting on PLT relocations because 1553 * they have already been assigned a PLT index and if we 1554 * were to sort them we would have to re-assign the plt indexes. 1555 */ 1556 if (!(flags & FLG_REL_PLT)) 1557 ofl->ofl_reloccnt++; 1558 1559 /* 1560 * Insure a GLOBAL_OFFSET_TABLE is generated if required. 1561 */ 1562 if (IS_GOT_REQUIRED(orsp->rel_rtype)) 1563 ofl->ofl_flags |= FLG_OF_BLDGOT; 1564 1565 /* 1566 * Identify and possibly warn of a displacement relocation. 1567 */ 1568 if (orsp->rel_flags & FLG_REL_DISP) { 1569 ofl->ofl_dtflags_1 |= DF_1_DISPRELPND; 1570 1571 if (ofl->ofl_flags & FLG_OF_VERBOSE) 1572 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL4), orsp, ofl); 1573 } 1574 DBG_CALL(Dbg_reloc_ors_entry(ofl->ofl_lml, ELF_DBG_LD, SHT_RELA, 1575 M_MACH, orsp)); 1576 return (1); 1577 } 1578 1579 /* 1580 * Process relocation against a register symbol. Note, of -z muldefs is in 1581 * effect there may have been multiple register definitions, which would have 1582 * been processed as non-fatal, with the first definition winning. But, we 1583 * will also process multiple relocations for these multiple definitions. In 1584 * this case we must only preserve the relocation for the definition that was 1585 * kept. The sad part is that register relocations don't typically specify 1586 * the register symbol with which they are associated, so we might have to 1587 * search the input files global symbols to determine if this relocation is 1588 * appropriate. 1589 */ 1590 static uintptr_t 1591 ld_reloc_register(Rel_desc *rsp, Is_desc *isp, Ofl_desc *ofl) 1592 { 1593 if (ofl->ofl_flags & FLG_OF_MULDEFS) { 1594 Ifl_desc *ifl = isp->is_file; 1595 Sym_desc *sdp = rsp->rel_sym; 1596 1597 if (sdp == 0) { 1598 Xword offset = rsp->rel_roffset; 1599 Word ndx; 1600 1601 for (ndx = ifl->ifl_locscnt; 1602 ndx < ifl->ifl_symscnt; ndx++) { 1603 if (((sdp = ifl->ifl_oldndx[ndx]) != 0) && 1604 (sdp->sd_flags & FLG_SY_REGSYM) && 1605 (sdp->sd_sym->st_value == offset)) 1606 break; 1607 } 1608 } 1609 if (sdp && (sdp->sd_file != ifl)) 1610 return (1); 1611 } 1612 return (ld_add_outrel((rsp->rel_flags | FLG_REL_REG), rsp, ofl)); 1613 } 1614 1615 /* 1616 * process relocation for a LOCAL symbol 1617 */ 1618 static uintptr_t 1619 ld_reloc_local(Rel_desc *rsp, Ofl_desc *ofl) 1620 { 1621 ofl_flag_t flags = ofl->ofl_flags; 1622 Sym_desc *sdp = rsp->rel_sym; 1623 Word shndx = sdp->sd_sym->st_shndx; 1624 1625 /* 1626 * if ((shared object) and (not pc relative relocation) and 1627 * (not against ABS symbol)) 1628 * then 1629 * if (rtype != R_SPARC_32) 1630 * then 1631 * build relocation against section 1632 * else 1633 * build R_SPARC_RELATIVE 1634 * fi 1635 * fi 1636 */ 1637 if ((flags & FLG_OF_SHAROBJ) && (rsp->rel_flags & FLG_REL_LOAD) && 1638 !(IS_PC_RELATIVE(rsp->rel_rtype)) && !(IS_SIZE(rsp->rel_rtype)) && 1639 !(IS_GOT_BASED(rsp->rel_rtype)) && 1640 !(rsp->rel_isdesc != NULL && 1641 (rsp->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof)) && 1642 (((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 1643 (shndx != SHN_ABS) || (sdp->sd_aux && sdp->sd_aux->sa_symspec))) { 1644 Word ortype = rsp->rel_rtype; 1645 1646 if ((rsp->rel_rtype != R_SPARC_32) && 1647 (rsp->rel_rtype != R_SPARC_PLT32) && 1648 (rsp->rel_rtype != R_SPARC_64)) 1649 return (ld_add_outrel((FLG_REL_SCNNDX | FLG_REL_ADVAL), 1650 rsp, ofl)); 1651 1652 rsp->rel_rtype = R_SPARC_RELATIVE; 1653 if (ld_add_outrel(FLG_REL_ADVAL, rsp, ofl) == S_ERROR) 1654 return (S_ERROR); 1655 rsp->rel_rtype = ortype; 1656 return (1); 1657 } 1658 1659 /* 1660 * If the relocation is against a 'non-allocatable' section 1661 * and we can not resolve it now - then give a warning 1662 * message. 1663 * 1664 * We can not resolve the symbol if either: 1665 * a) it's undefined 1666 * b) it's defined in a shared library and a 1667 * COPY relocation hasn't moved it to the executable 1668 * 1669 * Note: because we process all of the relocations against the 1670 * text segment before any others - we know whether 1671 * or not a copy relocation will be generated before 1672 * we get here (see reloc_init()->reloc_segments()). 1673 */ 1674 if (!(rsp->rel_flags & FLG_REL_LOAD) && 1675 ((shndx == SHN_UNDEF) || 1676 ((sdp->sd_ref == REF_DYN_NEED) && 1677 ((sdp->sd_flags & FLG_SY_MVTOCOMM) == 0)))) { 1678 Conv_inv_buf_t inv_buf; 1679 1680 /* 1681 * If the relocation is against a SHT_SUNW_ANNOTATE 1682 * section - then silently ignore that the relocation 1683 * can not be resolved. 1684 */ 1685 if (rsp->rel_osdesc && 1686 (rsp->rel_osdesc->os_shdr->sh_type == SHT_SUNW_ANNOTATE)) 1687 return (0); 1688 (void) eprintf(ofl->ofl_lml, ERR_WARNING, 1689 MSG_INTL(MSG_REL_EXTERNSYM), 1690 conv_reloc_SPARC_type(rsp->rel_rtype, 0, &inv_buf), 1691 rsp->rel_isdesc->is_file->ifl_name, 1692 demangle(rsp->rel_sname), rsp->rel_osdesc->os_name); 1693 return (1); 1694 } 1695 1696 /* 1697 * Perform relocation. 1698 */ 1699 return (ld_add_actrel(NULL, rsp, ofl)); 1700 } 1701 1702 /* 1703 * Establish a relocation transition. Note, at this point of input relocation 1704 * processing, we have no idea of the relocation value that will be used in 1705 * the eventual relocation calculation. This value is only known after the 1706 * initial image has been constructed. Therefore, there is a small chance 1707 * that a value can exceed the capabilities of the transitioned relocation. 1708 * One example might be the offset from the GOT to a symbol. 1709 * 1710 * The only instance of this failure discovered so far has been via the use of 1711 * ABS symbols to represent an external memory location. This situation is 1712 * rare, since ABS symbols aren't typically generated by the compilers. 1713 * Therefore, our solution is to excluded ABS symbols from the transition 1714 * relocation possibilities. As an additional safeguard, if an inappropriate 1715 * value is passed to the final relocation engine, a verification ("V") 1716 * relocation should trigger a fatal error condition. 1717 */ 1718 static uintptr_t 1719 ld_reloc_GOTOP(Boolean local, Rel_desc *rsp, Ofl_desc *ofl) 1720 { 1721 Word rtype = rsp->rel_rtype; 1722 1723 if (!local || (rsp->rel_sym->sd_sym->st_shndx == SHN_ABS)) { 1724 /* 1725 * When binding to a external symbol, no fixups are required 1726 * and the GOTDATA_OP relocation can be ignored. 1727 */ 1728 if (rtype == R_SPARC_GOTDATA_OP) 1729 return (1); 1730 return (ld_reloc_GOT_relative(local, rsp, ofl)); 1731 } 1732 1733 /* 1734 * When binding to a local symbol the relocations can be transitioned: 1735 * 1736 * R_*_GOTDATA_OP_HIX22 -> R_*_GOTDATA_HIX22 1737 * R_*_GOTDATA_OP_LOX10 -> R_*_GOTDATA_LOX10 1738 * R_*_GOTDATA_OP -> instruction fixup 1739 */ 1740 return (ld_add_actrel(FLG_REL_GOTFIX, rsp, ofl)); 1741 } 1742 1743 static uintptr_t 1744 ld_reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl) 1745 { 1746 Word rtype = rsp->rel_rtype; 1747 Sym_desc *sdp = rsp->rel_sym; 1748 ofl_flag_t flags = ofl->ofl_flags; 1749 Gotndx *gnp; 1750 1751 /* 1752 * If we're building an executable - use either the IE or LE access 1753 * model. If we're building a shared object process any IE model. 1754 */ 1755 if ((flags & FLG_OF_EXEC) || (IS_TLS_IE(rtype))) { 1756 /* 1757 * Set the DF_STATIC_TLS flag. 1758 */ 1759 ofl->ofl_dtflags |= DF_STATIC_TLS; 1760 1761 if (!local || ((flags & FLG_OF_EXEC) == 0)) { 1762 /* 1763 * When processing static TLS - these relocations 1764 * can be ignored. 1765 */ 1766 if ((rtype == R_SPARC_TLS_IE_LD) || 1767 (rtype == R_SPARC_TLS_IE_LDX) || 1768 (rtype == R_SPARC_TLS_IE_ADD)) 1769 return (1); 1770 1771 /* 1772 * Assign a GOT entry for IE static TLS references. 1773 */ 1774 if (((rtype == R_SPARC_TLS_GD_HI22) || 1775 (rtype == R_SPARC_TLS_GD_LO10) || 1776 (rtype == R_SPARC_TLS_IE_HI22) || 1777 (rtype == R_SPARC_TLS_IE_LO10)) && 1778 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, 1779 GOT_REF_TLSIE, ofl, rsp)) == NULL)) { 1780 1781 if (ld_assign_got_TLS(local, rsp, ofl, sdp, 1782 gnp, GOT_REF_TLSIE, FLG_REL_STLS, 1783 rtype, M_R_TPOFF, NULL) == S_ERROR) 1784 return (S_ERROR); 1785 } 1786 1787 /* 1788 * IE access model. 1789 */ 1790 if (IS_TLS_IE(rtype)) 1791 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1792 1793 /* 1794 * Fixups are required for other executable models. 1795 */ 1796 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1797 rsp, ofl)); 1798 } 1799 1800 /* 1801 * LE access model. 1802 */ 1803 if (IS_TLS_LE(rtype)) 1804 return (ld_add_actrel(FLG_REL_STLS, rsp, ofl)); 1805 1806 /* 1807 * When processing static TLS - these relocations can be 1808 * ignored. 1809 */ 1810 if (rtype == R_SPARC_TLS_IE_ADD) 1811 return (1); 1812 1813 return (ld_add_actrel((FLG_REL_TLSFIX | FLG_REL_STLS), 1814 rsp, ofl)); 1815 } 1816 1817 /* 1818 * Building a shared object. 1819 * 1820 * For dynamic TLS references, ADD relocations are ignored. 1821 */ 1822 if ((rtype == R_SPARC_TLS_GD_ADD) || (rtype == R_SPARC_TLS_LDM_ADD) || 1823 (rtype == R_SPARC_TLS_LDO_ADD)) 1824 return (1); 1825 1826 /* 1827 * Assign a GOT entry for a dynamic TLS reference. 1828 */ 1829 if (((rtype == R_SPARC_TLS_LDM_HI22) || 1830 (rtype == R_SPARC_TLS_LDM_LO10)) && 1831 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSLD, 1832 ofl, rsp)) == NULL)) { 1833 1834 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSLD, 1835 FLG_REL_MTLS, rtype, M_R_DTPMOD, 0) == S_ERROR) 1836 return (S_ERROR); 1837 1838 } else if (((rtype == R_SPARC_TLS_GD_HI22) || 1839 (rtype == R_SPARC_TLS_GD_LO10)) && 1840 ((gnp = ld_find_got_ndx(sdp->sd_GOTndxs, GOT_REF_TLSGD, 1841 ofl, rsp)) == NULL)) { 1842 1843 if (ld_assign_got_TLS(local, rsp, ofl, sdp, gnp, GOT_REF_TLSGD, 1844 FLG_REL_DTLS, rtype, M_R_DTPMOD, M_R_DTPOFF) == S_ERROR) 1845 return (S_ERROR); 1846 } 1847 1848 /* 1849 * For GD/LD TLS reference - TLS_{GD,LD}_CALL, this will eventually 1850 * cause a call to __tls_get_addr(). Convert this relocation to that 1851 * symbol now, and prepare for the PLT magic. 1852 */ 1853 if ((rtype == R_SPARC_TLS_GD_CALL) || (rtype == R_SPARC_TLS_LDM_CALL)) { 1854 Sym_desc *tlsgetsym; 1855 1856 if ((tlsgetsym = ld_sym_add_u(MSG_ORIG(MSG_SYM_TLSGETADDR_U), 1857 ofl, MSG_STR_TLSREL)) == (Sym_desc *)S_ERROR) 1858 return (S_ERROR); 1859 1860 rsp->rel_sym = tlsgetsym; 1861 rsp->rel_sname = tlsgetsym->sd_name; 1862 rsp->rel_rtype = R_SPARC_WPLT30; 1863 1864 if (ld_reloc_plt(rsp, ofl) == S_ERROR) 1865 return (S_ERROR); 1866 1867 rsp->rel_sym = sdp; 1868 rsp->rel_sname = sdp->sd_name; 1869 rsp->rel_rtype = rtype; 1870 return (1); 1871 } 1872 1873 if (IS_TLS_LD(rtype)) 1874 return (ld_add_actrel(FLG_REL_MTLS, rsp, ofl)); 1875 1876 return (ld_add_actrel(FLG_REL_DTLS, rsp, ofl)); 1877 } 1878 1879 /* 1880 * ld_allocate_got: if a GOT is to be made, after the section is built this 1881 * function is called to allocate all the GOT slots. The allocation is 1882 * deferred until after all GOTs have been counted and sorted according 1883 * to their size, for only then will we know how to allocate them on 1884 * a processor like SPARC which has different models for addressing the 1885 * GOT. SPARC has two: small and large, small uses a signed 13-bit offset 1886 * into the GOT, whereas large uses an unsigned 32-bit offset. 1887 */ 1888 static Sword small_index; /* starting index for small GOT entries */ 1889 static Sword mixed_index; /* starting index for mixed GOT entries */ 1890 static Sword large_index; /* starting index for large GOT entries */ 1891 1892 static uintptr_t 1893 ld_assign_got(Ofl_desc *ofl, Sym_desc *sdp) 1894 { 1895 Aliste idx; 1896 Gotndx *gnp; 1897 1898 for (ALIST_TRAVERSE(sdp->sd_GOTndxs, idx, gnp)) { 1899 uint_t gotents; 1900 Gotref gref = gnp->gn_gotref; 1901 1902 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 1903 gotents = 2; 1904 else 1905 gotents = 1; 1906 1907 switch (gnp->gn_gotndx) { 1908 case M_GOT_SMALL: 1909 gnp->gn_gotndx = small_index; 1910 small_index += gotents; 1911 if (small_index == 0) 1912 small_index = M_GOT_XNumber; 1913 break; 1914 case M_GOT_MIXED: 1915 gnp->gn_gotndx = mixed_index; 1916 mixed_index += gotents; 1917 break; 1918 case M_GOT_LARGE: 1919 gnp->gn_gotndx = large_index; 1920 large_index += gotents; 1921 break; 1922 default: 1923 eprintf(ofl->ofl_lml, ERR_FATAL, 1924 MSG_INTL(MSG_REL_ASSIGNGOT), 1925 EC_XWORD(gnp->gn_gotndx), demangle(sdp->sd_name)); 1926 return (S_ERROR); 1927 } 1928 } 1929 return (1); 1930 } 1931 1932 static uintptr_t 1933 ld_assign_got_ndx(Alist **alpp, Gotndx *pgnp, Gotref gref, Ofl_desc *ofl, 1934 Rel_desc *rsp, Sym_desc *sdp) 1935 { 1936 Xword raddend; 1937 Gotndx gn, *gnp; 1938 Aliste idx; 1939 uint_t gotents; 1940 1941 /* Some TLS requires two relocations with two GOT entries */ 1942 if ((gref == GOT_REF_TLSGD) || (gref == GOT_REF_TLSLD)) 1943 gotents = 2; 1944 else 1945 gotents = 1; 1946 1947 raddend = rsp->rel_raddend; 1948 if (pgnp && (pgnp->gn_addend == raddend) && (pgnp->gn_gotref == gref)) { 1949 1950 /* 1951 * If an entry for this addend already exists, determine if it 1952 * has mixed mode GOT access (both PIC and pic). 1953 * 1954 * In order to be accessible by both large and small pic, 1955 * a mixed mode GOT must be located in the positive index 1956 * range above _GLOBAL_OFFSET_TABLE_, and in the range 1957 * reachable small pic. This is necessary because the large 1958 * PIC mode cannot use a negative offset. This implies that 1959 * there can be no more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber) 1960 * such entries. 1961 */ 1962 switch (pgnp->gn_gotndx) { 1963 case M_GOT_SMALL: 1964 /* 1965 * This one was previously identified as a small 1966 * GOT. If this access is large, then convert 1967 * it to mixed. 1968 */ 1969 if (rsp->rel_rtype != R_SPARC_GOT13) { 1970 pgnp->gn_gotndx = M_GOT_MIXED; 1971 mixgotcnt += gotents; 1972 } 1973 break; 1974 1975 case M_GOT_LARGE: 1976 /* 1977 * This one was previously identified as a large 1978 * GOT. If this access is small, convert it to mixed. 1979 */ 1980 if (rsp->rel_rtype == R_SPARC_GOT13) { 1981 smlgotcnt += gotents; 1982 mixgotcnt += gotents; 1983 pgnp->gn_gotndx = M_GOT_MIXED; 1984 sdp->sd_flags |= FLG_SY_SMGOT; 1985 } 1986 break; 1987 } 1988 return (1); 1989 } 1990 1991 gn.gn_addend = raddend; 1992 gn.gn_gotref = gref; 1993 1994 if (rsp->rel_rtype == R_SPARC_GOT13) { 1995 gn.gn_gotndx = M_GOT_SMALL; 1996 smlgotcnt += gotents; 1997 sdp->sd_flags |= FLG_SY_SMGOT; 1998 } else 1999 gn.gn_gotndx = M_GOT_LARGE; 2000 2001 ofl->ofl_gotcnt += gotents; 2002 2003 if (gref == GOT_REF_TLSLD) { 2004 if (ofl->ofl_tlsldgotndx == NULL) { 2005 if ((gnp = libld_malloc(sizeof (Gotndx))) == NULL) 2006 return (S_ERROR); 2007 (void) memcpy(gnp, &gn, sizeof (Gotndx)); 2008 ofl->ofl_tlsldgotndx = gnp; 2009 } 2010 return (1); 2011 } 2012 2013 idx = 0; 2014 for (ALIST_TRAVERSE(*alpp, idx, gnp)) { 2015 if (gnp->gn_addend > raddend) 2016 break; 2017 } 2018 2019 /* 2020 * GOT indexes are maintained on an Alist, where there is typically 2021 * only one index. The usage of this list is to scan the list to find 2022 * an index, and then apply that index immediately to a relocation. 2023 * Thus there are no external references to these GOT index structures 2024 * that can be compromised by the Alist being reallocated. 2025 */ 2026 if (alist_insert(alpp, &gn, sizeof (Gotndx), 2027 AL_CNT_SDP_GOT, idx) == NULL) 2028 return (S_ERROR); 2029 2030 return (1); 2031 } 2032 2033 static void 2034 ld_assign_plt_ndx(Sym_desc * sdp, Ofl_desc *ofl) 2035 { 2036 sdp->sd_aux->sa_PLTndx = 1 + ofl->ofl_pltcnt++; 2037 } 2038 2039 2040 static uintptr_t 2041 ld_allocate_got(Ofl_desc * ofl) 2042 { 2043 const Sword first_large_ndx = M_GOT_MAXSMALL / 2; 2044 Sym_desc *sdp; 2045 Addr addr; 2046 2047 /* 2048 * Sanity check -- is this going to fit at all? There are two 2049 * limits to be concerned about: 2050 * 1) There is a limit on the number of small pic GOT indices, 2051 * given by M_GOT_MAXSMALL. 2052 * 2) If there are more than (M_GOT_MAXSMALL/2 - M_GOT_XNumber) 2053 * small GOT indices, there will be items at negative 2054 * offsets from _GLOBAL_OFFSET_TABLE_. Items that are 2055 * accessed via large (PIC) code cannot reach these 2056 * negative slots, so mixed mode items must be in the 2057 * non-negative range. This implies a limit of 2058 * (M_GOT_MAXSMALL/2 - M_GOT_XNumber) mixed mode indices. 2059 */ 2060 if (smlgotcnt > M_GOT_MAXSMALL) { 2061 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_SMALLGOT), 2062 EC_WORD(smlgotcnt), M_GOT_MAXSMALL); 2063 return (S_ERROR); 2064 } 2065 if (mixgotcnt > (first_large_ndx - M_GOT_XNumber)) { 2066 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_MIXEDGOT), 2067 EC_WORD(mixgotcnt), first_large_ndx - M_GOT_XNumber); 2068 return (S_ERROR); 2069 } 2070 2071 /* 2072 * Set starting offset to be either 0, or a negative index into 2073 * the GOT based on the number of small symbols we've got. 2074 */ 2075 neggotoffset = ((smlgotcnt >= first_large_ndx) ? 2076 (first_large_ndx - smlgotcnt) : 0); 2077 2078 /* 2079 * Initialize the got offsets used by assign_got() to 2080 * locate GOT items: 2081 * small - Starting index of items referenced only 2082 * by small offsets (-Kpic). 2083 * mixed - Starting index of items referenced 2084 * by both large (-KPIC) and small (-Kpic). 2085 * large - Indexes referenced only by large (-KPIC) 2086 * 2087 * Small items can have negative indexes (i.e. lie below 2088 * _GLOBAL_OFFSET_TABLE_). Mixed and large items must have 2089 * non-negative offsets. 2090 */ 2091 small_index = (neggotoffset == 0) ? M_GOT_XNumber : neggotoffset; 2092 large_index = neggotoffset + smlgotcnt; 2093 mixed_index = large_index - mixgotcnt; 2094 2095 /* 2096 * Assign bias to GOT symbols. 2097 */ 2098 addr = -neggotoffset * M_GOT_ENTSIZE; 2099 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), SYM_NOHASH, 2100 NULL, ofl)) != NULL) 2101 sdp->sd_sym->st_value = addr; 2102 if ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), SYM_NOHASH, 2103 NULL, ofl)) != NULL) 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, NULL, 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_fillfunc */ 2249 /* 2250 * On sparc, special filling of executable sections 2251 * is undesirable, and the default 0 fill supplied 2252 * by libelf is preferred: 2253 * 2254 * - 0 fill is interpreted as UNIMP instructions, 2255 * which cause an illegal_instruction_trap. These 2256 * serve as a sentinel against poorly written 2257 * code. The sparc architecture manual discusses 2258 * this as providing a measure of runtime safety. 2259 * 2260 * - The one place where a hole should conceivably 2261 * be filled with NOP instructions is in the 2262 * .init/.fini sections. However, the sparc 2263 * assembler sizes the sections it generates 2264 * to a multiple of the section alignment, and as 2265 * such, takes the filling task out of our hands. 2266 * Furthermore, the sparc assembler uses 0-fill 2267 * for this, forcing the authors of sparc 2268 * assembler for .init/.fini sections to be aware 2269 * of this case and explicitly supply NOP fill. 2270 * Hence, there is no role for the link-editor. 2271 */ 2272 NULL /* ff_execfill */ 2273 }, 2274 { /* Target_machrel */ 2275 reloc_table, 2276 2277 ld_init_rel, /* mr_init_rel */ 2278 ld_mach_eflags, /* mr_mach_eflags */ 2279 ld_mach_make_dynamic, /* mr_mach_make_dynamic */ 2280 ld_mach_update_odynamic, /* mr_mach_update_odynamic */ 2281 ld_calc_plt_addr, /* mr_calc_plt_addr */ 2282 ld_perform_outreloc, /* mr_perform_outreloc */ 2283 ld_do_activerelocs, /* mr_do_activerelocs */ 2284 ld_add_outrel, /* mr_add_outrel */ 2285 ld_reloc_register, /* mr_reloc_register */ 2286 ld_reloc_local, /* mr_reloc_local */ 2287 ld_reloc_GOTOP, /* mr_reloc_GOTOP */ 2288 ld_reloc_TLS, /* mr_reloc_TLS */ 2289 ld_assign_got, /* mr_assign_got */ 2290 ld_find_got_ndx, /* mr_find_got_ndx */ 2291 ld_calc_got_offset, /* mr_calc_got_offset */ 2292 ld_assign_got_ndx, /* mr_assign_got_ndx */ 2293 ld_assign_plt_ndx, /* mr_assign_plt_ndx */ 2294 ld_allocate_got, /* mr_allocate_got */ 2295 ld_fillin_gotplt, /* mr_fillin_gotplt */ 2296 }, 2297 { /* Target_machsym */ 2298 ld_reg_check_sparc, /* ms_reg_check */ 2299 ld_mach_sym_typecheck_sparc, /* ms_mach_sym_typecheck */ 2300 ld_is_regsym_sparc, /* ms_is_regsym */ 2301 ld_reg_find_sparc, /* ms_reg_find */ 2302 ld_reg_enter_sparc /* ms_reg_enter */ 2303 } 2304 }; 2305 2306 return (&_ld_targ); 2307 } 2308