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