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 2009 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* 28 * Copyright (c) 1988 AT&T 29 * All Rights Reserved 30 */ 31 32 /* 33 * SPARC machine dependent and ELF file class dependent functions. 34 * Contains routines for performing function binding and symbol relocations. 35 */ 36 37 #include <stdio.h> 38 #include <sys/elf.h> 39 #include <sys/elf_SPARC.h> 40 #include <sys/mman.h> 41 #include <dlfcn.h> 42 #include <synch.h> 43 #include <string.h> 44 #include <debug.h> 45 #include <reloc.h> 46 #include <conv.h> 47 #include "_rtld.h" 48 #include "_audit.h" 49 #include "_elf.h" 50 #include "_inline.h" 51 #include "msg.h" 52 53 extern void iflush_range(caddr_t, size_t); 54 extern void plt_full_range(uintptr_t, uintptr_t); 55 56 int 57 elf_mach_flags_check(Rej_desc *rej, Ehdr *ehdr) 58 { 59 /* 60 * Check machine type and flags. 61 */ 62 if (ehdr->e_machine != EM_SPARC) { 63 if (ehdr->e_machine != EM_SPARC32PLUS) { 64 rej->rej_type = SGS_REJ_MACH; 65 rej->rej_info = (uint_t)ehdr->e_machine; 66 return (0); 67 } 68 if ((ehdr->e_flags & EF_SPARC_32PLUS) == 0) { 69 rej->rej_type = SGS_REJ_MISFLAG; 70 rej->rej_info = (uint_t)ehdr->e_flags; 71 return (0); 72 } 73 if ((ehdr->e_flags & ~at_flags) & EF_SPARC_32PLUS_MASK) { 74 rej->rej_type = SGS_REJ_BADFLAG; 75 rej->rej_info = (uint_t)ehdr->e_flags; 76 return (0); 77 } 78 } else if ((ehdr->e_flags & ~EF_SPARCV9_MM) != 0) { 79 rej->rej_type = SGS_REJ_BADFLAG; 80 rej->rej_info = (uint_t)ehdr->e_flags; 81 return (0); 82 } 83 return (1); 84 } 85 86 void 87 ldso_plt_init(Rt_map *lmp) 88 { 89 /* 90 * There is no need to analyze ld.so because we don't map in any of 91 * its dependencies. However we may map these dependencies in later 92 * (as if ld.so had dlopened them), so initialize the plt and the 93 * permission information. 94 */ 95 if (PLTGOT(lmp)) 96 elf_plt_init((PLTGOT(lmp)), (caddr_t)lmp); 97 } 98 99 /* 100 * elf_plt_write() will test to see how far away our destination 101 * address lies. If it is close enough that a branch can 102 * be used instead of a jmpl - we will fill the plt in with 103 * single branch. The branches are much quicker then 104 * a jmpl instruction - see bug#4356879 for further 105 * details. 106 * 107 * NOTE: we pass in both a 'pltaddr' and a 'vpltaddr' since 108 * librtld/dldump update PLT's who's physical 109 * address is not the same as the 'virtual' runtime 110 * address. 111 */ 112 Pltbindtype 113 /* ARGSUSED4 */ 114 elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval, 115 Xword pltndx) 116 { 117 Rela *rel = (Rela *)rptr; 118 uintptr_t vpltaddr, pltaddr; 119 long disp; 120 121 pltaddr = addr + rel->r_offset; 122 vpltaddr = vaddr + rel->r_offset; 123 disp = symval - vpltaddr - 4; 124 125 /* 126 * Test if the destination address is close enough to use 127 * a ba,a... instruction to reach it. 128 */ 129 if (S_INRANGE(disp, 23) && !(rtld_flags & RT_FL_NOBAPLT)) { 130 uint_t *pltent, bainstr; 131 Pltbindtype rc; 132 133 pltent = (uint_t *)pltaddr; 134 135 /* 136 * The 137 * 138 * ba,a,pt %icc, <dest> 139 * 140 * is the most efficient of the PLT's. If we 141 * are within +-20 bits *and* running on a 142 * v8plus architecture - use that branch. 143 */ 144 if ((at_flags & EF_SPARC_32PLUS) && 145 S_INRANGE(disp, 20)) { 146 bainstr = M_BA_A_PT; /* ba,a,pt %icc,<dest> */ 147 bainstr |= (S_MASK(19) & (disp >> 2)); 148 rc = PLT_T_21D; 149 DBG_CALL(pltcnt21d++); 150 } else { 151 /* 152 * Otherwise - we fall back to the good old 153 * 154 * ba,a <dest> 155 * 156 * Which still beats a jmpl instruction. 157 */ 158 bainstr = M_BA_A; /* ba,a <dest> */ 159 bainstr |= (S_MASK(22) & (disp >> 2)); 160 rc = PLT_T_24D; 161 DBG_CALL(pltcnt24d++); 162 } 163 164 pltent[2] = M_NOP; /* nop instr */ 165 pltent[1] = bainstr; 166 167 iflush_range((char *)(&pltent[1]), 4); 168 pltent[0] = M_NOP; /* nop instr */ 169 iflush_range((char *)(&pltent[0]), 4); 170 return (rc); 171 } 172 173 /* 174 * The PLT destination is not in reach of 175 * a branch instruction - so we fall back 176 * to a 'jmpl' sequence. 177 */ 178 plt_full_range(pltaddr, symval); 179 DBG_CALL(pltcntfull++); 180 return (PLT_T_FULL); 181 } 182 183 /* 184 * Local storage space created on the stack created for this glue 185 * code includes space for: 186 * 0x4 pointer to dyn_data 187 * 0x4 size prev stack frame 188 */ 189 static const uchar_t dyn_plt_template[] = { 190 /* 0x00 */ 0x80, 0x90, 0x00, 0x1e, /* tst %fp */ 191 /* 0x04 */ 0x02, 0x80, 0x00, 0x04, /* be 0x14 */ 192 /* 0x08 */ 0x82, 0x27, 0x80, 0x0e, /* sub %sp, %fp, %g1 */ 193 /* 0x0c */ 0x10, 0x80, 0x00, 0x03, /* ba 0x20 */ 194 /* 0x10 */ 0x01, 0x00, 0x00, 0x00, /* nop */ 195 /* 0x14 */ 0x82, 0x10, 0x20, 0x60, /* mov 0x60, %g1 */ 196 /* 0x18 */ 0x9d, 0xe3, 0xbf, 0x98, /* save %sp, -0x68, %sp */ 197 /* 0x1c */ 0xc2, 0x27, 0xbf, 0xf8, /* st %g1, [%fp + -0x8] */ 198 /* 0x20 */ 0x03, 0x00, 0x00, 0x00, /* sethi %hi(val), %g1 */ 199 /* 0x24 */ 0x82, 0x10, 0x60, 0x00, /* or %g1, %lo(val), %g1 */ 200 /* 0x28 */ 0x40, 0x00, 0x00, 0x00, /* call <rel_addr> */ 201 /* 0x2c */ 0xc2, 0x27, 0xbf, 0xfc /* st %g1, [%fp + -0x4] */ 202 }; 203 204 int dyn_plt_ent_size = sizeof (dyn_plt_template) + 205 sizeof (uintptr_t) + /* reflmp */ 206 sizeof (uintptr_t) + /* deflmp */ 207 sizeof (ulong_t) + /* symndx */ 208 sizeof (ulong_t) + /* sb_flags */ 209 sizeof (Sym); /* symdef */ 210 211 /* 212 * the dynamic plt entry is: 213 * 214 * tst %fp 215 * be 1f 216 * nop 217 * sub %sp, %fp, %g1 218 * ba 2f 219 * nop 220 * 1: 221 * mov SA(MINFRAME), %g1 ! if %fp is null this is the 222 * ! 'minimum stack'. %fp is null 223 * ! on the initial stack frame 224 * 2: 225 * save %sp, -(SA(MINFRAME) + 2 * CLONGSIZE), %sp 226 * st %g1, [%fp + -0x8] ! store prev_stack size in [%fp - 8] 227 * sethi %hi(dyn_data), %g1 228 * or %g1, %lo(dyn_data), %g1 229 * call elf_plt_trace 230 * st %g1, [%fp + -0x4] ! store dyn_data ptr in [%fp - 4] 231 * dyn data: 232 * uintptr_t reflmp 233 * uintptr_t deflmp 234 * ulong_t symndx 235 * ulong_t sb_flags 236 * Sym symdef 237 */ 238 static caddr_t 239 elf_plt_trace_write(caddr_t addr, Rela *rptr, Rt_map *rlmp, Rt_map *dlmp, 240 Sym *sym, ulong_t symndx, ulong_t pltndx, caddr_t to, ulong_t sb_flags, 241 int *fail) 242 { 243 extern ulong_t elf_plt_trace(); 244 uchar_t *dyn_plt; 245 uintptr_t *dyndata; 246 247 /* 248 * If both pltenter & pltexit have been disabled there 249 * there is no reason to even create the glue code. 250 */ 251 if ((sb_flags & (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)) == 252 (LA_SYMB_NOPLTENTER | LA_SYMB_NOPLTEXIT)) { 253 (void) elf_plt_write((uintptr_t)addr, (uintptr_t)addr, 254 rptr, (uintptr_t)to, pltndx); 255 return (to); 256 } 257 258 /* 259 * We only need to add the glue code if there is an auditing 260 * library that is interested in this binding. 261 */ 262 dyn_plt = (uchar_t *)((uintptr_t)AUDINFO(rlmp)->ai_dynplts + 263 (pltndx * dyn_plt_ent_size)); 264 265 /* 266 * Have we initialized this dynamic plt entry yet? If we haven't do it 267 * now. Otherwise this function has been called before, but from a 268 * different plt (ie. from another shared object). In that case 269 * we just set the plt to point to the new dyn_plt. 270 */ 271 if (*dyn_plt == 0) { 272 Sym *symp; 273 Xword symvalue; 274 Lm_list *lml = LIST(rlmp); 275 276 (void) memcpy((void *)dyn_plt, dyn_plt_template, 277 sizeof (dyn_plt_template)); 278 dyndata = (uintptr_t *)((uintptr_t)dyn_plt + 279 sizeof (dyn_plt_template)); 280 281 /* 282 * relocating: 283 * sethi %hi(dyndata), %g1 284 */ 285 symvalue = (Xword)dyndata; 286 if (do_reloc_rtld(R_SPARC_HI22, (dyn_plt + 0x20), 287 &symvalue, MSG_ORIG(MSG_SYM_LADYNDATA), 288 MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) { 289 *fail = 1; 290 return (0); 291 } 292 293 /* 294 * relocating: 295 * or %g1, %lo(dyndata), %g1 296 */ 297 symvalue = (Xword)dyndata; 298 if (do_reloc_rtld(R_SPARC_LO10, (dyn_plt + 0x24), 299 &symvalue, MSG_ORIG(MSG_SYM_LADYNDATA), 300 MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) { 301 *fail = 1; 302 return (0); 303 } 304 305 /* 306 * relocating: 307 * call elf_plt_trace 308 */ 309 symvalue = (Xword)((uintptr_t)&elf_plt_trace - 310 (uintptr_t)(dyn_plt + 0x28)); 311 if (do_reloc_rtld(R_SPARC_WDISP30, (dyn_plt + 0x28), 312 &symvalue, MSG_ORIG(MSG_SYM_ELFPLTTRACE), 313 MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) { 314 *fail = 1; 315 return (0); 316 } 317 318 *dyndata++ = (uintptr_t)rlmp; 319 *dyndata++ = (uintptr_t)dlmp; 320 *(ulong_t *)dyndata++ = symndx; 321 *(ulong_t *)dyndata++ = sb_flags; 322 symp = (Sym *)dyndata; 323 *symp = *sym; 324 symp->st_name += (Word)STRTAB(dlmp); 325 symp->st_value = (Addr)to; 326 327 iflush_range((void *)dyn_plt, sizeof (dyn_plt_template)); 328 } 329 330 (void) elf_plt_write((uintptr_t)addr, (uintptr_t)addr, rptr, 331 (uintptr_t)dyn_plt, 0); 332 return ((caddr_t)dyn_plt); 333 } 334 335 /* 336 * Function binding routine - invoked on the first call to a function through 337 * the procedure linkage table; 338 * passes first through an assembly language interface. 339 * 340 * Takes the address of the PLT entry where the call originated, 341 * the offset into the relocation table of the associated 342 * relocation entry and the address of the link map (rt_private_map struct) 343 * for the entry. 344 * 345 * Returns the address of the function referenced after re-writing the PLT 346 * entry to invoke the function directly. 347 * 348 * On error, causes process to terminate with a signal. 349 */ 350 ulong_t 351 elf_bndr(Rt_map *lmp, ulong_t pltoff, caddr_t from) 352 { 353 Rt_map *nlmp, *llmp; 354 ulong_t addr, vaddr, reloff, symval, rsymndx; 355 char *name; 356 Rela *rptr; 357 Sym *rsym, *nsym; 358 Xword pltndx; 359 uint_t binfo, sb_flags = 0, dbg_class; 360 Slookup sl; 361 Pltbindtype pbtype; 362 int entry, lmflags; 363 Lm_list *lml; 364 365 /* 366 * For compatibility with libthread (TI_VERSION 1) we track the entry 367 * value. A zero value indicates we have recursed into ld.so.1 to 368 * further process a locking request. Under this recursion we disable 369 * tsort and cleanup activities. 370 */ 371 entry = enter(0); 372 373 lml = LIST(lmp); 374 if ((lmflags = lml->lm_flags) & LML_FLG_RTLDLM) { 375 dbg_class = dbg_desc->d_class; 376 dbg_desc->d_class = 0; 377 } 378 379 /* 380 * Must calculate true plt relocation address from reloc. 381 * Take offset, subtract number of reserved PLT entries, and divide 382 * by PLT entry size, which should give the index of the plt 383 * entry (and relocation entry since they have been defined to be 384 * in the same order). Then we must multiply by the size of 385 * a relocation entry, which will give us the offset of the 386 * plt relocation entry from the start of them given by JMPREL(lm). 387 */ 388 addr = pltoff - M_PLT_RESERVSZ; 389 pltndx = addr / M_PLT_ENTSIZE; 390 391 /* 392 * Perform some basic sanity checks. If we didn't get a load map 393 * or the plt offset is invalid then its possible someone has walked 394 * over the plt entries or jumped to plt[0] out of the blue. 395 */ 396 if (!lmp || ((addr % M_PLT_ENTSIZE) != 0)) { 397 Conv_inv_buf_t inv_buf; 398 399 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_PLTREF), 400 conv_reloc_SPARC_type(R_SPARC_JMP_SLOT, 0, &inv_buf), 401 EC_NATPTR(lmp), EC_XWORD(pltoff), EC_NATPTR(from)); 402 rtldexit(lml, 1); 403 } 404 reloff = pltndx * sizeof (Rela); 405 406 /* 407 * Use relocation entry to get symbol table entry and symbol name. 408 */ 409 addr = (ulong_t)JMPREL(lmp); 410 rptr = (Rela *)(addr + reloff); 411 rsymndx = ELF_R_SYM(rptr->r_info); 412 rsym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp))); 413 name = (char *)(STRTAB(lmp) + rsym->st_name); 414 415 /* 416 * Determine the last link-map of this list, this'll be the starting 417 * point for any tsort() processing. 418 */ 419 llmp = lml->lm_tail; 420 421 /* 422 * Find definition for symbol. Initialize the symbol lookup data 423 * structure. 424 */ 425 SLOOKUP_INIT(sl, name, lmp, lml->lm_head, ld_entry_cnt, 0, 426 rsymndx, rsym, 0, LKUP_DEFT); 427 428 if ((nsym = lookup_sym(&sl, &nlmp, &binfo, NULL)) == 0) { 429 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp), 430 demangle(name)); 431 rtldexit(lml, 1); 432 } 433 434 symval = nsym->st_value; 435 if (!(FLAGS(nlmp) & FLG_RT_FIXED) && 436 (nsym->st_shndx != SHN_ABS)) 437 symval += ADDR(nlmp); 438 if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) { 439 /* 440 * Record that this new link map is now bound to the caller. 441 */ 442 if (bind_one(lmp, nlmp, BND_REFER) == 0) 443 rtldexit(lml, 1); 444 } 445 446 if ((lml->lm_tflags | AFLAGS(lmp)) & LML_TFLG_AUD_SYMBIND) { 447 ulong_t symndx = (((uintptr_t)nsym - 448 (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp)); 449 450 symval = audit_symbind(lmp, nlmp, nsym, symndx, symval, 451 &sb_flags); 452 } 453 454 if (FLAGS(lmp) & FLG_RT_FIXED) 455 vaddr = 0; 456 else 457 vaddr = ADDR(lmp); 458 459 pbtype = PLT_T_NONE; 460 if (!(rtld_flags & RT_FL_NOBIND)) { 461 if (((lml->lm_tflags | AFLAGS(lmp)) & 462 (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) && 463 AUDINFO(lmp)->ai_dynplts) { 464 int fail = 0; 465 ulong_t symndx = (((uintptr_t)nsym - 466 (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp)); 467 468 symval = (ulong_t)elf_plt_trace_write((caddr_t)vaddr, 469 rptr, lmp, nlmp, nsym, symndx, pltndx, 470 (caddr_t)symval, sb_flags, &fail); 471 if (fail) 472 rtldexit(lml, 1); 473 } else { 474 /* 475 * Write standard PLT entry to jump directly 476 * to newly bound function. 477 */ 478 pbtype = elf_plt_write((uintptr_t)vaddr, 479 (uintptr_t)vaddr, rptr, symval, pltndx); 480 } 481 } 482 483 /* 484 * Print binding information and rebuild PLT entry. 485 */ 486 DBG_CALL(Dbg_bind_global(lmp, (Addr)from, (Off)(from - ADDR(lmp)), 487 pltndx, pbtype, nlmp, (Addr)symval, nsym->st_value, name, binfo)); 488 489 /* 490 * Complete any processing for newly loaded objects. Note we don't 491 * know exactly where any new objects are loaded (we know the object 492 * that supplied the symbol, but others may have been loaded lazily as 493 * we searched for the symbol), so sorting starts from the last 494 * link-map know on entry to this routine. 495 */ 496 if (entry) 497 load_completion(llmp); 498 499 /* 500 * Some operations like dldump() or dlopen()'ing a relocatable object 501 * result in objects being loaded on rtld's link-map, make sure these 502 * objects are initialized also. 503 */ 504 if ((LIST(nlmp)->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init) 505 load_completion(nlmp); 506 507 /* 508 * Make sure the object to which we've bound has had it's .init fired. 509 * Cleanup before return to user code. 510 */ 511 if (entry) { 512 is_dep_init(nlmp, lmp); 513 leave(lml, 0); 514 } 515 516 if (lmflags & LML_FLG_RTLDLM) 517 dbg_desc->d_class = dbg_class; 518 519 return (symval); 520 } 521 522 /* 523 * Read and process the relocations for one link object, we assume all 524 * relocation sections for loadable segments are stored contiguously in 525 * the file. 526 */ 527 int 528 elf_reloc(Rt_map *lmp, uint_t plt, int *in_nfavl, APlist **textrel) 529 { 530 ulong_t relbgn, relend, relsiz, basebgn, pltbgn, pltend; 531 ulong_t dsymndx, pltndx, roffset, rsymndx, psymndx = 0; 532 uchar_t rtype; 533 long reladd, value, pvalue, relacount = RELACOUNT(lmp); 534 Sym *symref, *psymref, *symdef, *psymdef; 535 char *name, *pname; 536 Rt_map *_lmp, *plmp; 537 int ret = 1, noplt = 0; 538 Rela *rel; 539 Pltbindtype pbtype; 540 uint_t binfo, pbinfo; 541 APlist *bound = NULL; 542 543 /* 544 * If an object has any DT_REGISTER entries associated with 545 * it, they are processed now. 546 */ 547 if ((plt == 0) && (FLAGS(lmp) & FLG_RT_REGSYMS)) { 548 if (elf_regsyms(lmp) == 0) 549 return (0); 550 } 551 552 /* 553 * Although only necessary for lazy binding, initialize the first 554 * procedure linkage table entry to go to elf_rtbndr(). dbx(1) seems 555 * to find this useful. 556 */ 557 if ((plt == 0) && PLTGOT(lmp)) { 558 mmapobj_result_t *mpp; 559 560 /* 561 * Make sure the segment is writable. 562 */ 563 if ((((mpp = 564 find_segment((caddr_t)PLTGOT(lmp), lmp)) != NULL) && 565 ((mpp->mr_prot & PROT_WRITE) == 0)) && 566 ((set_prot(lmp, mpp, 1) == 0) || 567 (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL))) 568 return (0); 569 570 elf_plt_init(PLTGOT(lmp), (caddr_t)lmp); 571 } 572 573 /* 574 * Initialize the plt start and end addresses. 575 */ 576 if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0) 577 pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp)); 578 579 /* 580 * If we've been called upon to promote an RTLD_LAZY object to an 581 * RTLD_NOW then we're only interested in scaning the .plt table. 582 */ 583 if (plt) { 584 relbgn = pltbgn; 585 relend = pltend; 586 } else { 587 /* 588 * The relocation sections appear to the run-time linker as a 589 * single table. Determine the address of the beginning and end 590 * of this table. There are two different interpretations of 591 * the ABI at this point: 592 * 593 * o The REL table and its associated RELSZ indicate the 594 * concatenation of *all* relocation sections (this is the 595 * model our link-editor constructs). 596 * 597 * o The REL table and its associated RELSZ indicate the 598 * concatenation of all *but* the .plt relocations. These 599 * relocations are specified individually by the JMPREL and 600 * PLTRELSZ entries. 601 * 602 * Determine from our knowledege of the relocation range and 603 * .plt range, the range of the total relocation table. Note 604 * that one other ABI assumption seems to be that the .plt 605 * relocations always follow any other relocations, the 606 * following range checking drops that assumption. 607 */ 608 relbgn = (ulong_t)(REL(lmp)); 609 relend = relbgn + (ulong_t)(RELSZ(lmp)); 610 if (pltbgn) { 611 if (!relbgn || (relbgn > pltbgn)) 612 relbgn = pltbgn; 613 if (!relbgn || (relend < pltend)) 614 relend = pltend; 615 } 616 } 617 if (!relbgn || (relbgn == relend)) { 618 DBG_CALL(Dbg_reloc_run(lmp, 0, plt, DBG_REL_NONE)); 619 return (1); 620 } 621 622 relsiz = (ulong_t)(RELENT(lmp)); 623 basebgn = ADDR(lmp); 624 625 DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, plt, DBG_REL_START)); 626 627 /* 628 * If we're processing in lazy mode there is no need to scan the 629 * .rela.plt table. 630 */ 631 if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0)) 632 noplt = 1; 633 634 /* 635 * Loop through relocations. 636 */ 637 while (relbgn < relend) { 638 mmapobj_result_t *mpp; 639 uint_t sb_flags = 0; 640 Addr vaddr; 641 642 rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info, M_MACH); 643 644 /* 645 * If this is a RELATIVE relocation in a shared object (the 646 * common case), and if we are not debugging, then jump into one 647 * of the tighter relocation loops. 648 */ 649 if ((rtype == R_SPARC_RELATIVE) && 650 ((FLAGS(lmp) & FLG_RT_FIXED) == 0) && (DBG_ENABLED == 0)) { 651 if (relacount) { 652 relbgn = elf_reloc_relative_count(relbgn, 653 relacount, relsiz, basebgn, lmp, textrel); 654 relacount = 0; 655 } else { 656 relbgn = elf_reloc_relative(relbgn, relend, 657 relsiz, basebgn, lmp, textrel); 658 } 659 if (relbgn >= relend) 660 break; 661 rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info, M_MACH); 662 } 663 664 roffset = ((Rela *)relbgn)->r_offset; 665 666 reladd = (long)(((Rela *)relbgn)->r_addend); 667 rsymndx = ELF_R_SYM(((Rela *)relbgn)->r_info); 668 669 rel = (Rela *)relbgn; 670 relbgn += relsiz; 671 672 /* 673 * Optimizations. 674 */ 675 if (rtype == R_SPARC_NONE) 676 continue; 677 if (noplt && ((ulong_t)rel >= pltbgn) && 678 ((ulong_t)rel < pltend)) { 679 relbgn = pltend; 680 continue; 681 } 682 683 if (rtype != R_SPARC_REGISTER) { 684 /* 685 * If this is a shared object, add the base address 686 * to offset. 687 */ 688 if (!(FLAGS(lmp) & FLG_RT_FIXED)) 689 roffset += basebgn; 690 691 /* 692 * If this relocation is not against part of the image 693 * mapped into memory we skip it. 694 */ 695 if ((mpp = find_segment((caddr_t)roffset, 696 lmp)) == NULL) { 697 elf_reloc_bad(lmp, (void *)rel, rtype, roffset, 698 rsymndx); 699 continue; 700 } 701 } 702 703 /* 704 * If we're promoting .plts, try and determine if this one has 705 * already been written. An uninitialized .plts' second 706 * instruction is a branch. Note, elf_plt_write() optimizes 707 * .plt relocations, and it's possible that a relocated entry 708 * is a branch. If this is the case, we can't tell the 709 * difference between an uninitialized .plt and a relocated, 710 * .plt that uses a branch. In this case, we'll simply redo 711 * the relocation calculation, which is a bit sad. 712 */ 713 if (plt) { 714 ulong_t *_roffset = (ulong_t *)roffset; 715 716 _roffset++; 717 if ((*_roffset & (~(S_MASK(22)))) != M_BA_A) 718 continue; 719 } 720 721 binfo = 0; 722 pltndx = (ulong_t)-1; 723 pbtype = PLT_T_NONE; 724 725 /* 726 * If a symbol index is specified then get the symbol table 727 * entry, locate the symbol definition, and determine its 728 * address. 729 */ 730 if (rsymndx) { 731 /* 732 * Get the local symbol table entry. 733 */ 734 symref = (Sym *)((ulong_t)SYMTAB(lmp) + 735 (rsymndx * SYMENT(lmp))); 736 737 /* 738 * If this is a local symbol, just use the base address. 739 * (we should have no local relocations in the 740 * executable). 741 */ 742 if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) { 743 value = basebgn; 744 name = (char *)0; 745 746 /* 747 * Special case TLS relocations. 748 */ 749 if (rtype == R_SPARC_TLS_DTPMOD32) { 750 /* 751 * Use the TLS modid. 752 */ 753 value = TLSMODID(lmp); 754 755 } else if (rtype == R_SPARC_TLS_TPOFF32) { 756 if ((value = elf_static_tls(lmp, symref, 757 rel, rtype, 0, roffset, 0)) == 0) { 758 ret = 0; 759 break; 760 } 761 } 762 } else { 763 /* 764 * If the symbol index is equal to the previous 765 * symbol index relocation we processed then 766 * reuse the previous values. (Note that there 767 * have been cases where a relocation exists 768 * against a copy relocation symbol, our ld(1) 769 * should optimize this away, but make sure we 770 * don't use the same symbol information should 771 * this case exist). 772 */ 773 if ((rsymndx == psymndx) && 774 (rtype != R_SPARC_COPY)) { 775 /* LINTED */ 776 if (psymdef == 0) { 777 DBG_CALL(Dbg_bind_weak(lmp, 778 (Addr)roffset, (Addr) 779 (roffset - basebgn), name)); 780 continue; 781 } 782 /* LINTED */ 783 value = pvalue; 784 /* LINTED */ 785 name = pname; 786 symdef = psymdef; 787 /* LINTED */ 788 symref = psymref; 789 /* LINTED */ 790 _lmp = plmp; 791 /* LINTED */ 792 binfo = pbinfo; 793 794 if ((LIST(_lmp)->lm_tflags | 795 AFLAGS(_lmp)) & 796 LML_TFLG_AUD_SYMBIND) { 797 value = audit_symbind(lmp, _lmp, 798 /* LINTED */ 799 symdef, dsymndx, value, 800 &sb_flags); 801 } 802 } else { 803 Slookup sl; 804 805 /* 806 * Lookup the symbol definition. 807 * Initialize the symbol lookup data 808 * structure. 809 */ 810 name = (char *)(STRTAB(lmp) + 811 symref->st_name); 812 813 SLOOKUP_INIT(sl, name, lmp, 0, 814 ld_entry_cnt, 0, rsymndx, symref, 815 rtype, LKUP_STDRELOC); 816 817 symdef = lookup_sym(&sl, &_lmp, 818 &binfo, in_nfavl); 819 820 /* 821 * If the symbol is not found and the 822 * reference was not to a weak symbol, 823 * report an error. Weak references 824 * may be unresolved. 825 */ 826 /* BEGIN CSTYLED */ 827 if (symdef == 0) { 828 if (sl.sl_bind != STB_WEAK) { 829 if (elf_reloc_error(lmp, name, 830 rel, binfo)) 831 continue; 832 833 ret = 0; 834 break; 835 836 } else { 837 psymndx = rsymndx; 838 psymdef = 0; 839 840 DBG_CALL(Dbg_bind_weak(lmp, 841 (Addr)roffset, (Addr) 842 (roffset - basebgn), name)); 843 continue; 844 } 845 } 846 /* END CSTYLED */ 847 848 /* 849 * If symbol was found in an object 850 * other than the referencing object 851 * then record the binding. 852 */ 853 if ((lmp != _lmp) && ((FLAGS1(_lmp) & 854 FL1_RT_NOINIFIN) == 0)) { 855 if (aplist_test(&bound, _lmp, 856 AL_CNT_RELBIND) == 0) { 857 ret = 0; 858 break; 859 } 860 } 861 862 /* 863 * Calculate the location of definition; 864 * symbol value plus base address of 865 * containing shared object. 866 */ 867 if (IS_SIZE(rtype)) 868 value = symdef->st_size; 869 else 870 value = symdef->st_value; 871 872 if (!(FLAGS(_lmp) & FLG_RT_FIXED) && 873 !(IS_SIZE(rtype)) && 874 (symdef->st_shndx != SHN_ABS) && 875 (ELF_ST_TYPE(symdef->st_info) != 876 STT_TLS)) 877 value += ADDR(_lmp); 878 879 /* 880 * Retain this symbol index and the 881 * value in case it can be used for the 882 * subsequent relocations. 883 */ 884 if (rtype != R_SPARC_COPY) { 885 psymndx = rsymndx; 886 pvalue = value; 887 pname = name; 888 psymdef = symdef; 889 psymref = symref; 890 plmp = _lmp; 891 pbinfo = binfo; 892 } 893 if ((LIST(_lmp)->lm_tflags | 894 AFLAGS(_lmp)) & 895 LML_TFLG_AUD_SYMBIND) { 896 dsymndx = (((uintptr_t)symdef - 897 (uintptr_t)SYMTAB(_lmp)) / 898 SYMENT(_lmp)); 899 value = audit_symbind(lmp, _lmp, 900 symdef, dsymndx, value, 901 &sb_flags); 902 } 903 } 904 905 /* 906 * If relocation is PC-relative, subtract 907 * offset address. 908 */ 909 if (IS_PC_RELATIVE(rtype)) 910 value -= roffset; 911 912 /* 913 * Special case TLS relocations. 914 */ 915 if (rtype == R_SPARC_TLS_DTPMOD32) { 916 /* 917 * Relocation value is the TLS modid. 918 */ 919 value = TLSMODID(_lmp); 920 921 } else if (rtype == R_SPARC_TLS_TPOFF32) { 922 if ((value = elf_static_tls(_lmp, 923 symdef, rel, rtype, name, roffset, 924 value)) == 0) { 925 ret = 0; 926 break; 927 } 928 } 929 } 930 } else { 931 /* 932 * Special cases. 933 */ 934 if (rtype == R_SPARC_REGISTER) { 935 /* 936 * A register symbol associated with symbol 937 * index 0 is initialized (i.e. relocated) to 938 * a constant in the r_addend field rather than 939 * to a symbol value. 940 */ 941 value = 0; 942 943 } else if (rtype == R_SPARC_TLS_DTPMOD32) { 944 /* 945 * TLS relocation value is the TLS modid. 946 */ 947 value = TLSMODID(lmp); 948 } else 949 value = basebgn; 950 name = (char *)0; 951 } 952 953 DBG_CALL(Dbg_reloc_in(LIST(lmp), ELF_DBG_RTLD, M_MACH, 954 M_REL_SHT_TYPE, rel, NULL, 0, name)); 955 956 /* 957 * Make sure the segment is writable. 958 */ 959 if ((rtype != R_SPARC_REGISTER) && 960 ((mpp->mr_prot & PROT_WRITE) == 0) && 961 ((set_prot(lmp, mpp, 1) == 0) || 962 (aplist_append(textrel, mpp, AL_CNT_TEXTREL) == NULL))) { 963 ret = 0; 964 break; 965 } 966 967 /* 968 * Call relocation routine to perform required relocation. 969 */ 970 switch (rtype) { 971 case R_SPARC_REGISTER: 972 /* 973 * The v9 ABI 4.2.4 says that system objects may, 974 * but are not required to, use register symbols 975 * to inidcate how they use global registers. Thus 976 * at least %g6, %g7 must be allowed in addition 977 * to %g2 and %g3. 978 */ 979 value += reladd; 980 if (roffset == STO_SPARC_REGISTER_G1) { 981 set_sparc_g1(value); 982 } else if (roffset == STO_SPARC_REGISTER_G2) { 983 set_sparc_g2(value); 984 } else if (roffset == STO_SPARC_REGISTER_G3) { 985 set_sparc_g3(value); 986 } else if (roffset == STO_SPARC_REGISTER_G4) { 987 set_sparc_g4(value); 988 } else if (roffset == STO_SPARC_REGISTER_G5) { 989 set_sparc_g5(value); 990 } else if (roffset == STO_SPARC_REGISTER_G6) { 991 set_sparc_g6(value); 992 } else if (roffset == STO_SPARC_REGISTER_G7) { 993 set_sparc_g7(value); 994 } else { 995 eprintf(LIST(lmp), ERR_FATAL, 996 MSG_INTL(MSG_REL_BADREG), NAME(lmp), 997 EC_ADDR(roffset)); 998 ret = 0; 999 break; 1000 } 1001 1002 DBG_CALL(Dbg_reloc_apply_reg(LIST(lmp), ELF_DBG_RTLD, 1003 M_MACH, (Xword)roffset, (Xword)value)); 1004 break; 1005 case R_SPARC_COPY: 1006 if (elf_copy_reloc(name, symref, lmp, (void *)roffset, 1007 symdef, _lmp, (const void *)value) == 0) 1008 ret = 0; 1009 break; 1010 case R_SPARC_JMP_SLOT: 1011 pltndx = ((ulong_t)rel - 1012 (uintptr_t)JMPREL(lmp)) / relsiz; 1013 1014 if (FLAGS(lmp) & FLG_RT_FIXED) 1015 vaddr = 0; 1016 else 1017 vaddr = ADDR(lmp); 1018 1019 if (((LIST(lmp)->lm_tflags | AFLAGS(lmp)) & 1020 (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) && 1021 AUDINFO(lmp)->ai_dynplts) { 1022 int fail = 0; 1023 ulong_t symndx = (((uintptr_t)symdef - 1024 (uintptr_t)SYMTAB(_lmp)) / SYMENT(_lmp)); 1025 1026 (void) elf_plt_trace_write((caddr_t)vaddr, 1027 (Rela *)rel, lmp, _lmp, symdef, symndx, 1028 pltndx, (caddr_t)value, sb_flags, &fail); 1029 if (fail) 1030 ret = 0; 1031 } else { 1032 /* 1033 * Write standard PLT entry to jump directly 1034 * to newly bound function. 1035 */ 1036 DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), 1037 ELF_DBG_RTLD, (Xword)roffset, 1038 (Xword)value)); 1039 pbtype = elf_plt_write((uintptr_t)vaddr, 1040 (uintptr_t)vaddr, (void *)rel, value, 1041 pltndx); 1042 } 1043 break; 1044 default: 1045 value += reladd; 1046 1047 /* 1048 * Write the relocation out. If this relocation is a 1049 * common basic write, skip the doreloc() engine. 1050 */ 1051 if ((rtype == R_SPARC_GLOB_DAT) || 1052 (rtype == R_SPARC_32)) { 1053 if (roffset & 0x3) { 1054 Conv_inv_buf_t inv_buf; 1055 1056 eprintf(LIST(lmp), ERR_FATAL, 1057 MSG_INTL(MSG_REL_NONALIGN), 1058 conv_reloc_SPARC_type(rtype, 1059 0, &inv_buf), 1060 NAME(lmp), demangle(name), 1061 EC_OFF(roffset)); 1062 ret = 0; 1063 } else 1064 *(uint_t *)roffset += value; 1065 } else { 1066 if (do_reloc_rtld(rtype, (uchar_t *)roffset, 1067 (Xword *)&value, name, 1068 NAME(lmp), LIST(lmp)) == 0) 1069 ret = 0; 1070 } 1071 1072 /* 1073 * The value now contains the 'bit-shifted' value that 1074 * was or'ed into memory (this was set by 1075 * do_reloc_rtld()). 1076 */ 1077 DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), ELF_DBG_RTLD, 1078 (Xword)roffset, (Xword)value)); 1079 1080 /* 1081 * If this relocation is against a text segment, make 1082 * sure that the instruction cache is flushed. 1083 */ 1084 if (textrel) 1085 iflush_range((caddr_t)roffset, 0x4); 1086 } 1087 1088 if ((ret == 0) && 1089 ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0)) 1090 break; 1091 1092 if (binfo) { 1093 DBG_CALL(Dbg_bind_global(lmp, (Addr)roffset, 1094 (Off)(roffset - basebgn), pltndx, pbtype, 1095 _lmp, (Addr)value, symdef->st_value, name, binfo)); 1096 } 1097 } 1098 1099 return (relocate_finish(lmp, bound, ret)); 1100 } 1101 1102 /* 1103 * Provide a machine specific interface to the conversion routine. By calling 1104 * the machine specific version, rather than the generic version, we insure that 1105 * the data tables/strings for all known machine versions aren't dragged into 1106 * ld.so.1. 1107 */ 1108 const char * 1109 _conv_reloc_type(uint_t rel) 1110 { 1111 static Conv_inv_buf_t inv_buf; 1112 1113 return (conv_reloc_SPARC_type(rel, 0, &inv_buf)); 1114 } 1115