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