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