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 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 #pragma ident "%Z%%M% %I% %E% SMI" 27 28 /* 29 * amd64 machine dependent and ELF file class dependent functions. 30 * Contains routines for performing function binding and symbol relocations. 31 */ 32 #include "_synonyms.h" 33 34 #include <stdio.h> 35 #include <sys/elf.h> 36 #include <sys/elf_amd64.h> 37 #include <sys/mman.h> 38 #include <dlfcn.h> 39 #include <synch.h> 40 #include <string.h> 41 #include <debug.h> 42 #include <reloc.h> 43 #include <conv.h> 44 #include "_rtld.h" 45 #include "_audit.h" 46 #include "_elf.h" 47 #include "msg.h" 48 49 50 extern void elf_rtbndr(Rt_map *, ulong_t, caddr_t); 51 52 int 53 elf_mach_flags_check(Rej_desc *rej, Ehdr *ehdr) 54 { 55 /* 56 * Check machine type and flags. 57 */ 58 if (ehdr->e_flags != 0) { 59 rej->rej_type = SGS_REJ_BADFLAG; 60 rej->rej_info = (uint_t)ehdr->e_flags; 61 return (0); 62 } 63 return (1); 64 } 65 66 void 67 ldso_plt_init(Rt_map * lmp) 68 { 69 /* 70 * There is no need to analyze ld.so because we don't map in any of 71 * its dependencies. However we may map these dependencies in later 72 * (as if ld.so had dlopened them), so initialize the plt and the 73 * permission information. 74 */ 75 if (PLTGOT(lmp)) 76 elf_plt_init((void *)(PLTGOT(lmp)), (caddr_t)lmp); 77 } 78 79 static const uchar_t dyn_plt_template[] = { 80 /* 0x00 */ 0x55, /* pushq %rbp */ 81 /* 0x01 */ 0x48, 0x89, 0xe5, /* movq %rsp, %rbp */ 82 /* 0x04 */ 0x48, 0x83, 0xec, 0x10, /* subq $0x10, %rsp */ 83 /* 0x08 */ 0x4c, 0x8d, 0x1d, 0x00, /* leaq trace_fields(%rip), %r11 */ 84 0x00, 0x00, 0x00, 85 /* 0x0f */ 0x4c, 0x89, 0x5d, 0xf8, /* movq %r11, -0x8(%rbp) */ 86 /* 0x13 */ 0x49, 0xbb, 0x00, 0x00, /* movq $elf_plt_trace, %r11 */ 87 0x00, 0x00, 0x00, 88 0x00, 0x00, 0x00, 89 /* 0x1d */ 0x41, 0xff, 0xe3 /* jmp *%r11 */ 90 /* 0x20 */ 91 }; 92 93 /* 94 * And the virutal outstanding relocations against the 95 * above block are: 96 * 97 * reloc offset Addend symbol 98 * R_AMD64_PC32 0x0b -4 trace_fields 99 * R_AMD64_64 0x15 0 elf_plt_trace 100 */ 101 102 #define TRCREL1OFF 0x0b 103 #define TRCREL2OFF 0x15 104 105 int dyn_plt_ent_size = sizeof (dyn_plt_template); 106 107 /* 108 * the dynamic plt entry is: 109 * 110 * pushq %rbp 111 * movq %rsp, %rbp 112 * subq $0x10, %rsp 113 * leaq trace_fields(%rip), %r11 114 * movq %r11, -0x8(%rbp) 115 * movq $elf_plt_trace, %r11 116 * jmp *%r11 117 * dyn_data: 118 * .align 8 119 * uintptr_t reflmp 120 * uintptr_t deflmp 121 * uint_t symndx 122 * uint_t sb_flags 123 * Sym symdef 124 */ 125 static caddr_t 126 elf_plt_trace_write(ulong_t roffset, Rt_map *rlmp, Rt_map *dlmp, Sym *sym, 127 uint_t symndx, uint_t pltndx, caddr_t to, uint_t sb_flags, int *fail) 128 { 129 extern int elf_plt_trace(); 130 ulong_t got_entry; 131 uchar_t *dyn_plt; 132 uintptr_t *dyndata; 133 134 135 /* 136 * We only need to add the glue code if there is an auditing 137 * library that is interested in this binding. 138 */ 139 dyn_plt = (uchar_t *)((uintptr_t)AUDINFO(rlmp)->ai_dynplts + 140 (pltndx * dyn_plt_ent_size)); 141 142 /* 143 * Have we initialized this dynamic plt entry yet? If we haven't do it 144 * now. Otherwise this function has been called before, but from a 145 * different plt (ie. from another shared object). In that case 146 * we just set the plt to point to the new dyn_plt. 147 */ 148 if (*dyn_plt == 0) { 149 Sym * symp; 150 Xword symvalue; 151 Lm_list *lml = LIST(rlmp); 152 153 (void) memcpy((void *)dyn_plt, dyn_plt_template, 154 sizeof (dyn_plt_template)); 155 dyndata = (uintptr_t *)((uintptr_t)dyn_plt + 156 ROUND(sizeof (dyn_plt_template), M_WORD_ALIGN)); 157 158 /* 159 * relocate: 160 * leaq trace_fields(%rip), %r11 161 * R_AMD64_PC32 0x0b -4 trace_fields 162 */ 163 symvalue = (Xword)((uintptr_t)dyndata - 164 (uintptr_t)(&dyn_plt[TRCREL1OFF]) - 4); 165 if (do_reloc(R_AMD64_PC32, &dyn_plt[TRCREL1OFF], 166 &symvalue, MSG_ORIG(MSG_SYM_LADYNDATA), 167 MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) { 168 *fail = 1; 169 return (0); 170 } 171 172 /* 173 * relocating: 174 * movq $elf_plt_trace, %r11 175 * R_AMD64_64 0x15 0 elf_plt_trace 176 */ 177 symvalue = (Xword)elf_plt_trace; 178 if (do_reloc(R_AMD64_64, &dyn_plt[TRCREL2OFF], 179 &symvalue, MSG_ORIG(MSG_SYM_ELFPLTTRACE), 180 MSG_ORIG(MSG_SPECFIL_DYNPLT), lml) == 0) { 181 *fail = 1; 182 return (0); 183 } 184 185 *dyndata++ = (uintptr_t)rlmp; 186 *dyndata++ = (uintptr_t)dlmp; 187 *dyndata = (uintptr_t)(((uint64_t)sb_flags << 32) | symndx); 188 dyndata++; 189 symp = (Sym *)dyndata; 190 *symp = *sym; 191 symp->st_value = (Addr)to; 192 } 193 194 got_entry = (ulong_t)roffset; 195 *(ulong_t *)got_entry = (ulong_t)dyn_plt; 196 return ((caddr_t)dyn_plt); 197 } 198 199 200 /* 201 * Function binding routine - invoked on the first call to a function through 202 * the procedure linkage table; 203 * passes first through an assembly language interface. 204 * 205 * Takes the offset into the relocation table of the associated 206 * relocation entry and the address of the link map (rt_private_map struct) 207 * for the entry. 208 * 209 * Returns the address of the function referenced after re-writing the PLT 210 * entry to invoke the function directly. 211 * 212 * On error, causes process to terminate with a signal. 213 */ 214 ulong_t 215 elf_bndr(Rt_map *lmp, ulong_t pltndx, caddr_t from) 216 { 217 Rt_map *nlmp, * llmp; 218 ulong_t addr, reloff, symval, rsymndx; 219 char *name; 220 Rela *rptr; 221 Sym *sym, *nsym; 222 uint_t binfo, sb_flags = 0, dbg_class; 223 Slookup sl; 224 int entry, lmflags; 225 Lm_list *lml; 226 227 /* 228 * For compatibility with libthread (TI_VERSION 1) we track the entry 229 * value. A zero value indicates we have recursed into ld.so.1 to 230 * further process a locking request. Under this recursion we disable 231 * tsort and cleanup activities. 232 */ 233 entry = enter(); 234 235 lml = LIST(lmp); 236 if ((lmflags = lml->lm_flags) & LML_FLG_RTLDLM) { 237 dbg_class = dbg_desc->d_class; 238 dbg_desc->d_class = 0; 239 } 240 241 /* 242 * Perform some basic sanity checks. If we didn't get a load map or 243 * the relocation offset is invalid then its possible someone has walked 244 * over the .got entries or jumped to plt0 out of the blue. 245 */ 246 if ((!lmp) && (pltndx <= 247 (ulong_t)PLTRELSZ(lmp) / (ulong_t)RELENT(lmp))) { 248 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_PLTREF), 249 conv_reloc_amd64_type(R_AMD64_JUMP_SLOT, 0), 250 EC_NATPTR(lmp), EC_XWORD(pltndx), EC_NATPTR(from)); 251 rtldexit(lml, 1); 252 } 253 reloff = pltndx * (ulong_t)RELENT(lmp); 254 255 /* 256 * Use relocation entry to get symbol table entry and symbol name. 257 */ 258 addr = (ulong_t)JMPREL(lmp); 259 rptr = (Rela *)(addr + reloff); 260 rsymndx = ELF_R_SYM(rptr->r_info); 261 sym = (Sym *)((ulong_t)SYMTAB(lmp) + (rsymndx * SYMENT(lmp))); 262 name = (char *)(STRTAB(lmp) + sym->st_name); 263 264 /* 265 * Determine the last link-map of this list, this'll be the starting 266 * point for any tsort() processing. 267 */ 268 llmp = lml->lm_tail; 269 270 /* 271 * Find definition for symbol. 272 */ 273 sl.sl_name = name; 274 sl.sl_cmap = lmp; 275 sl.sl_imap = lml->lm_head; 276 sl.sl_hash = 0; 277 sl.sl_rsymndx = rsymndx; 278 sl.sl_flags = LKUP_DEFT; 279 280 if ((nsym = lookup_sym(&sl, &nlmp, &binfo)) == 0) { 281 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYM), NAME(lmp), 282 demangle(name)); 283 rtldexit(lml, 1); 284 } 285 286 symval = nsym->st_value; 287 if (!(FLAGS(nlmp) & FLG_RT_FIXED) && 288 (nsym->st_shndx != SHN_ABS)) 289 symval += ADDR(nlmp); 290 if ((lmp != nlmp) && ((FLAGS1(nlmp) & FL1_RT_NOINIFIN) == 0)) { 291 /* 292 * Record that this new link map is now bound to the caller. 293 */ 294 if (bind_one(lmp, nlmp, BND_REFER) == 0) 295 rtldexit(lml, 1); 296 } 297 298 if ((lml->lm_tflags | FLAGS1(lmp)) & LML_TFLG_AUD_SYMBIND) { 299 uint_t symndx = (((uintptr_t)nsym - 300 (uintptr_t)SYMTAB(nlmp)) / SYMENT(nlmp)); 301 symval = audit_symbind(lmp, nlmp, nsym, symndx, symval, 302 &sb_flags); 303 } 304 305 if (!(rtld_flags & RT_FL_NOBIND)) { 306 addr = rptr->r_offset; 307 if (!(FLAGS(lmp) & FLG_RT_FIXED)) 308 addr += ADDR(lmp); 309 if (((lml->lm_tflags | FLAGS1(lmp)) & 310 (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) && 311 AUDINFO(lmp)->ai_dynplts) { 312 int fail = 0; 313 uint_t pltndx = reloff / sizeof (Rela); 314 uint_t symndx = (((uintptr_t)nsym - 315 (uintptr_t)SYMTAB(nlmp)) / 316 SYMENT(nlmp)); 317 318 symval = (ulong_t)elf_plt_trace_write(addr, lmp, nlmp, 319 nsym, symndx, pltndx, (caddr_t)symval, sb_flags, 320 &fail); 321 if (fail) 322 rtldexit(lml, 1); 323 } else { 324 /* 325 * Write standard PLT entry to jump directly 326 * to newly bound function. 327 */ 328 *(ulong_t *)addr = symval; 329 } 330 } 331 332 /* 333 * Print binding information and rebuild PLT entry. 334 */ 335 DBG_CALL(Dbg_bind_global(lmp, (Addr)from, (Off)(from - ADDR(lmp)), 336 (Xword)(reloff / sizeof (Rela)), PLT_T_FULL, nlmp, 337 (Addr)symval, nsym->st_value, name, binfo)); 338 339 /* 340 * Complete any processing for newly loaded objects. Note we don't 341 * know exactly where any new objects are loaded (we know the object 342 * that supplied the symbol, but others may have been loaded lazily as 343 * we searched for the symbol), so sorting starts from the last 344 * link-map know on entry to this routine. 345 */ 346 if (entry) 347 load_completion(llmp, lmp); 348 349 /* 350 * Some operations like dldump() or dlopen()'ing a relocatable object 351 * result in objects being loaded on rtld's link-map, make sure these 352 * objects are initialized also. 353 */ 354 if ((lml->lm_flags & LML_FLG_RTLDLM) && LIST(nlmp)->lm_init) 355 load_completion(nlmp, 0); 356 357 /* 358 * If the object we've bound to is in the process of being initialized 359 * by another thread, determine whether we should block. 360 */ 361 is_dep_ready(nlmp, lmp, DBG_WAIT_SYMBOL); 362 363 /* 364 * Make sure the object to which we've bound has had it's .init fired. 365 * Cleanup before return to user code. 366 */ 367 if (entry) { 368 is_dep_init(nlmp, lmp); 369 leave(lml); 370 } 371 372 if (lmflags & LML_FLG_RTLDLM) 373 dbg_desc->d_class = dbg_class; 374 375 return (symval); 376 } 377 378 379 /* 380 * When the relocation loop realizes that it's dealing with relative 381 * relocations in a shared object, it breaks into this tighter loop 382 * as an optimization. 383 */ 384 ulong_t 385 elf_reloc_relative(ulong_t relbgn, ulong_t relend, ulong_t relsiz, 386 ulong_t basebgn, ulong_t etext, ulong_t emap) 387 { 388 ulong_t roffset = ((Rela *)relbgn)->r_offset; 389 char rtype; 390 391 do { 392 roffset += basebgn; 393 394 /* 395 * If this relocation is against an address not mapped in, 396 * then break out of the relative relocation loop, falling 397 * back on the main relocation loop. 398 */ 399 if (roffset < etext || roffset > emap) 400 break; 401 402 /* 403 * Perform the actual relocation. 404 */ 405 *((ulong_t *)roffset) = basebgn + 406 ((Rela *)relbgn)->r_addend; 407 408 relbgn += relsiz; 409 410 if (relbgn >= relend) 411 break; 412 413 rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info); 414 roffset = ((Rela *)relbgn)->r_offset; 415 416 } while (rtype == R_AMD64_RELATIVE); 417 418 return (relbgn); 419 } 420 421 /* 422 * This is the tightest loop for RELATIVE relocations for those 423 * objects built with the DT_RELACOUNT .dynamic entry. 424 */ 425 ulong_t 426 elf_reloc_relacount(ulong_t relbgn, ulong_t relacount, ulong_t relsiz, 427 ulong_t basebgn) 428 { 429 ulong_t roffset = ((Rela *) relbgn)->r_offset; 430 431 for (; relacount; relacount--) { 432 roffset += basebgn; 433 434 /* 435 * Perform the actual relocation. 436 */ 437 *((ulong_t *)roffset) = basebgn + 438 ((Rela *)relbgn)->r_addend; 439 440 relbgn += relsiz; 441 442 roffset = ((Rela *)relbgn)->r_offset; 443 444 } 445 446 return (relbgn); 447 } 448 449 /* 450 * Read and process the relocations for one link object, we assume all 451 * relocation sections for loadable segments are stored contiguously in 452 * the file. 453 */ 454 int 455 elf_reloc(Rt_map *lmp, uint_t plt) 456 { 457 ulong_t relbgn, relend, relsiz, basebgn; 458 ulong_t pltbgn, pltend, _pltbgn, _pltend; 459 ulong_t roffset, rsymndx, psymndx = 0, etext = ETEXT(lmp); 460 ulong_t emap, dsymndx; 461 uchar_t rtype; 462 long reladd, value, pvalue; 463 Sym *symref, *psymref, *symdef, *psymdef; 464 char *name, *pname; 465 Rt_map *_lmp, *plmp; 466 int textrel = 0, ret = 1, noplt = 0; 467 int relacount = RELACOUNT(lmp), plthint = 0; 468 Rela *rel; 469 uint_t binfo, pbinfo; 470 Alist *bound = 0; 471 472 /* 473 * Although only necessary for lazy binding, initialize the first 474 * global offset entry to go to elf_rtbndr(). dbx(1) seems 475 * to find this useful. 476 */ 477 if ((plt == 0) && PLTGOT(lmp)) { 478 if ((ulong_t)PLTGOT(lmp) < etext) { 479 if (elf_set_prot(lmp, PROT_WRITE) == 0) 480 return (0); 481 textrel = 1; 482 } 483 elf_plt_init((void *)PLTGOT(lmp), (caddr_t)lmp); 484 } 485 486 /* 487 * Initialize the plt start and end addresses. 488 */ 489 if ((pltbgn = (ulong_t)JMPREL(lmp)) != 0) 490 pltend = pltbgn + (ulong_t)(PLTRELSZ(lmp)); 491 492 493 relsiz = (ulong_t)(RELENT(lmp)); 494 basebgn = ADDR(lmp); 495 emap = ADDR(lmp) + MSIZE(lmp); 496 497 if (PLTRELSZ(lmp)) 498 plthint = PLTRELSZ(lmp) / relsiz; 499 500 /* 501 * If we've been called upon to promote an RTLD_LAZY object to an 502 * RTLD_NOW then we're only interested in scaning the .plt table. 503 * An uninitialized .plt is the case where the associated got entry 504 * points back to the plt itself. Determine the range of the real .plt 505 * entries using the _PROCEDURE_LINKAGE_TABLE_ symbol. 506 */ 507 if (plt) { 508 Slookup sl; 509 510 relbgn = pltbgn; 511 relend = pltend; 512 if (!relbgn || (relbgn == relend)) 513 return (1); 514 515 sl.sl_name = MSG_ORIG(MSG_SYM_PLT); 516 sl.sl_cmap = lmp; 517 sl.sl_imap = lmp; 518 sl.sl_hash = elf_hash(MSG_ORIG(MSG_SYM_PLT)); 519 sl.sl_rsymndx = 0; 520 sl.sl_flags = LKUP_DEFT; 521 522 if ((symdef = elf_find_sym(&sl, &_lmp, &binfo)) == 0) 523 return (1); 524 525 _pltbgn = symdef->st_value; 526 if (!(FLAGS(lmp) & FLG_RT_FIXED) && 527 (symdef->st_shndx != SHN_ABS)) 528 _pltbgn += basebgn; 529 _pltend = _pltbgn + (((PLTRELSZ(lmp) / relsiz)) * 530 M_PLT_ENTSIZE) + M_PLT_RESERVSZ; 531 532 } else { 533 /* 534 * The relocation sections appear to the run-time linker as a 535 * single table. Determine the address of the beginning and end 536 * of this table. There are two different interpretations of 537 * the ABI at this point: 538 * 539 * o The REL table and its associated RELSZ indicate the 540 * concatenation of *all* relocation sections (this is the 541 * model our link-editor constructs). 542 * 543 * o The REL table and its associated RELSZ indicate the 544 * concatenation of all *but* the .plt relocations. These 545 * relocations are specified individually by the JMPREL and 546 * PLTRELSZ entries. 547 * 548 * Determine from our knowledege of the relocation range and 549 * .plt range, the range of the total relocation table. Note 550 * that one other ABI assumption seems to be that the .plt 551 * relocations always follow any other relocations, the 552 * following range checking drops that assumption. 553 */ 554 relbgn = (ulong_t)(REL(lmp)); 555 relend = relbgn + (ulong_t)(RELSZ(lmp)); 556 if (pltbgn) { 557 if (!relbgn || (relbgn > pltbgn)) 558 relbgn = pltbgn; 559 if (!relbgn || (relend < pltend)) 560 relend = pltend; 561 } 562 } 563 if (!relbgn || (relbgn == relend)) { 564 DBG_CALL(Dbg_reloc_run(lmp, 0, plt, DBG_REL_NONE)); 565 return (1); 566 } 567 DBG_CALL(Dbg_reloc_run(lmp, M_REL_SHT_TYPE, plt, DBG_REL_START)); 568 569 /* 570 * If we're processing a dynamic executable in lazy mode there is no 571 * need to scan the .rel.plt table, however if we're processing a shared 572 * object in lazy mode the .got addresses associated to each .plt must 573 * be relocated to reflect the location of the shared object. 574 */ 575 if (pltbgn && ((MODE(lmp) & RTLD_NOW) == 0) && 576 (FLAGS(lmp) & FLG_RT_FIXED)) 577 noplt = 1; 578 579 /* 580 * Loop through relocations. 581 */ 582 while (relbgn < relend) { 583 uint_t sb_flags = 0; 584 585 rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info); 586 587 /* 588 * If this is a RELATIVE relocation in a shared object (the 589 * common case), and if we are not debugging, then jump into a 590 * tighter relocation loop (elf_reloc_relative). Only make the 591 * jump if we've been given a hint on the number of relocations. 592 */ 593 if ((rtype == R_AMD64_RELATIVE) && 594 ((FLAGS(lmp) & FLG_RT_FIXED) == 0) && (DBG_ENABLED == 0)) { 595 /* 596 * It's possible that the relative relocation block 597 * has relocations against the text segment as well 598 * as the data segment. Since our optimized relocation 599 * engine does not check which segment the relocation 600 * is against - just mprotect it now if it's been 601 * marked as containing TEXTREL's. 602 */ 603 if ((textrel == 0) && (FLAGS1(lmp) & FL1_RT_TEXTREL)) { 604 if (elf_set_prot(lmp, PROT_WRITE) == 0) { 605 ret = 0; 606 break; 607 } 608 textrel = 1; 609 } 610 if (relacount) { 611 relbgn = elf_reloc_relacount(relbgn, relacount, 612 relsiz, basebgn); 613 relacount = 0; 614 } else { 615 relbgn = elf_reloc_relative(relbgn, relend, 616 relsiz, basebgn, etext, emap); 617 } 618 619 if (relbgn >= relend) 620 break; 621 rtype = ELF_R_TYPE(((Rela *)relbgn)->r_info); 622 } 623 624 roffset = ((Rela *)relbgn)->r_offset; 625 626 /* 627 * If this is a shared object, add the base address to offset. 628 */ 629 if (!(FLAGS(lmp) & FLG_RT_FIXED)) { 630 631 632 /* 633 * If we're processing lazy bindings, we have to step 634 * through the plt entries and add the base address 635 * to the corresponding got entry. 636 */ 637 if (plthint && (plt == 0) && 638 (rtype == R_AMD64_JUMP_SLOT) && 639 ((MODE(lmp) & RTLD_NOW) == 0)) { 640 /* 641 * The PLT relocations (for lazy bindings) 642 * are additive to what's already in the GOT. 643 * This differs to what happens in 644 * elf_reloc_relacount() and that's why we 645 * just do it inline here. 646 */ 647 for (roffset = ((Rela *)relbgn)->r_offset; 648 plthint; plthint--) { 649 roffset += basebgn; 650 651 /* 652 * Perform the actual relocation. 653 */ 654 *((ulong_t *)roffset) += basebgn; 655 656 relbgn += relsiz; 657 roffset = ((Rela *)relbgn)->r_offset; 658 659 } 660 continue; 661 } 662 roffset += basebgn; 663 } 664 665 reladd = (long)(((Rela *)relbgn)->r_addend); 666 rsymndx = ELF_R_SYM(((Rela *)relbgn)->r_info); 667 rel = (Rela *)relbgn; 668 relbgn += relsiz; 669 670 /* 671 * Optimizations. 672 */ 673 if (rtype == R_AMD64_NONE) 674 continue; 675 if (noplt && ((ulong_t)rel >= pltbgn) && 676 ((ulong_t)rel < pltend)) { 677 relbgn = pltend; 678 continue; 679 } 680 681 /* 682 * If this relocation is not against part of the image 683 * mapped into memory we skip it. 684 */ 685 if ((roffset < ADDR(lmp)) || (roffset > (ADDR(lmp) + 686 MSIZE(lmp)))) { 687 elf_reloc_bad(lmp, (void *)rel, rtype, roffset, 688 rsymndx); 689 continue; 690 } 691 692 /* 693 * If we're promoting plts determine if this one has already 694 * been written. 695 */ 696 if (plt) { 697 if ((*(ulong_t *)roffset < _pltbgn) || 698 (*(ulong_t *)roffset > _pltend)) 699 continue; 700 } 701 702 binfo = 0; 703 /* 704 * If a symbol index is specified then get the symbol table 705 * entry, locate the symbol definition, and determine its 706 * address. 707 */ 708 if (rsymndx) { 709 /* 710 * Get the local symbol table entry. 711 */ 712 symref = (Sym *)((ulong_t)SYMTAB(lmp) + 713 (rsymndx * SYMENT(lmp))); 714 715 /* 716 * If this is a local symbol, just use the base address. 717 * (we should have no local relocations in the 718 * executable). 719 */ 720 if (ELF_ST_BIND(symref->st_info) == STB_LOCAL) { 721 value = basebgn; 722 name = (char *)0; 723 724 /* 725 * Special case TLS relocations. 726 */ 727 if (rtype == R_AMD64_DTPMOD64) { 728 /* 729 * Use the TLS modid. 730 */ 731 value = TLSMODID(lmp); 732 733 } else if ((rtype == R_AMD64_TPOFF64) || 734 (rtype == R_AMD64_TPOFF32)) { 735 if ((value = elf_static_tls(lmp, symref, 736 rel, rtype, 0, roffset, 0)) == 0) { 737 ret = 0; 738 break; 739 } 740 } 741 } else { 742 /* 743 * If the symbol index is equal to the previous 744 * symbol index relocation we processed then 745 * reuse the previous values. (Note that there 746 * have been cases where a relocation exists 747 * against a copy relocation symbol, our ld(1) 748 * should optimize this away, but make sure we 749 * don't use the same symbol information should 750 * this case exist). 751 */ 752 if ((rsymndx == psymndx) && 753 (rtype != R_AMD64_COPY)) { 754 /* LINTED */ 755 if (psymdef == 0) { 756 DBG_CALL(Dbg_bind_weak(lmp, 757 (Addr)roffset, (Addr) 758 (roffset - basebgn), name)); 759 continue; 760 } 761 /* LINTED */ 762 value = pvalue; 763 /* LINTED */ 764 name = pname; 765 /* LINTED */ 766 symdef = psymdef; 767 /* LINTED */ 768 symref = psymref; 769 /* LINTED */ 770 _lmp = plmp; 771 /* LINTED */ 772 binfo = pbinfo; 773 774 if ((LIST(_lmp)->lm_tflags | 775 FLAGS1(_lmp)) & 776 LML_TFLG_AUD_SYMBIND) { 777 value = audit_symbind(lmp, _lmp, 778 /* LINTED */ 779 symdef, dsymndx, value, 780 &sb_flags); 781 } 782 } else { 783 Slookup sl; 784 uchar_t bind; 785 786 /* 787 * Lookup the symbol definition. 788 */ 789 name = (char *)(STRTAB(lmp) + 790 symref->st_name); 791 792 sl.sl_name = name; 793 sl.sl_cmap = lmp; 794 sl.sl_imap = 0; 795 sl.sl_hash = 0; 796 sl.sl_rsymndx = rsymndx; 797 798 if (rtype == R_AMD64_COPY) 799 sl.sl_flags = LKUP_COPY; 800 else 801 sl.sl_flags = LKUP_DEFT; 802 803 sl.sl_flags |= LKUP_ALLCNTLIST; 804 805 if (rtype != R_AMD64_JUMP_SLOT) 806 sl.sl_flags |= LKUP_SPEC; 807 808 bind = ELF_ST_BIND(symref->st_info); 809 if (bind == STB_WEAK) 810 sl.sl_flags |= LKUP_WEAK; 811 812 symdef = lookup_sym(&sl, &_lmp, &binfo); 813 814 /* 815 * If the symbol is not found and the 816 * reference was not to a weak symbol, 817 * report an error. Weak references 818 * may be unresolved. 819 * chkmsg: MSG_INTL(MSG_LDD_SYM_NFOUND) 820 */ 821 if (symdef == 0) { 822 Lm_list *lml = LIST(lmp); 823 824 if (bind != STB_WEAK) { 825 if (lml->lm_flags & 826 LML_FLG_IGNRELERR) { 827 continue; 828 } else if (lml->lm_flags & 829 LML_FLG_TRC_WARN) { 830 (void) printf(MSG_INTL( 831 MSG_LDD_SYM_NFOUND), 832 demangle(name), 833 NAME(lmp)); 834 continue; 835 } else { 836 DBG_CALL(Dbg_reloc_in(lml, 837 ELF_DBG_RTLD, M_MACH, 838 M_REL_SHT_TYPE, rel, 839 NULL, name)); 840 eprintf(lml, ERR_FATAL, 841 MSG_INTL(MSG_REL_NOSYM), 842 NAME(lmp), 843 demangle(name)); 844 ret = 0; 845 break; 846 } 847 } else { 848 psymndx = rsymndx; 849 psymdef = 0; 850 851 DBG_CALL(Dbg_bind_weak(lmp, 852 (Addr)roffset, (Addr) 853 (roffset - basebgn), name)); 854 continue; 855 } 856 } 857 858 /* 859 * If symbol was found in an object 860 * other than the referencing object 861 * then record the binding. 862 */ 863 if ((lmp != _lmp) && ((FLAGS1(_lmp) & 864 FL1_RT_NOINIFIN) == 0)) { 865 if (alist_test(&bound, _lmp, 866 sizeof (Rt_map *), 867 AL_CNT_RELBIND) == 0) { 868 ret = 0; 869 break; 870 } 871 } 872 873 /* 874 * Calculate the location of definition; 875 * symbol value plus base address of 876 * containing shared object. 877 */ 878 if (IS_SIZE(rtype)) 879 value = symdef->st_size; 880 else 881 value = symdef->st_value; 882 883 if (!(FLAGS(_lmp) & FLG_RT_FIXED) && 884 !(IS_SIZE(rtype)) && 885 (symdef->st_shndx != SHN_ABS) && 886 (ELF_ST_TYPE(symdef->st_info) != 887 STT_TLS)) 888 value += ADDR(_lmp); 889 890 /* 891 * Retain this symbol index and the 892 * value in case it can be used for the 893 * subsequent relocations. 894 */ 895 if (rtype != R_AMD64_COPY) { 896 psymndx = rsymndx; 897 pvalue = value; 898 pname = name; 899 psymdef = symdef; 900 psymref = symref; 901 plmp = _lmp; 902 pbinfo = binfo; 903 } 904 if ((LIST(_lmp)->lm_tflags | 905 FLAGS1(_lmp)) & 906 LML_TFLG_AUD_SYMBIND) { 907 dsymndx = (((uintptr_t)symdef - 908 (uintptr_t)SYMTAB(_lmp)) / 909 SYMENT(_lmp)); 910 value = audit_symbind(lmp, _lmp, 911 symdef, dsymndx, value, 912 &sb_flags); 913 } 914 } 915 916 /* 917 * If relocation is PC-relative, subtract 918 * offset address. 919 */ 920 if (IS_PC_RELATIVE(rtype)) 921 value -= roffset; 922 923 /* 924 * Special case TLS relocations. 925 */ 926 if (rtype == R_AMD64_DTPMOD64) { 927 /* 928 * Relocation value is the TLS modid. 929 */ 930 value = TLSMODID(_lmp); 931 932 } else if ((rtype == R_AMD64_TPOFF64) || 933 (rtype == R_AMD64_TPOFF32)) { 934 if ((value = elf_static_tls(_lmp, 935 symdef, rel, rtype, name, roffset, 936 value)) == 0) { 937 ret = 0; 938 break; 939 } 940 } 941 } 942 } else { 943 /* 944 * Special cases. 945 */ 946 if (rtype == R_AMD64_DTPMOD64) { 947 /* 948 * TLS relocation value is the TLS modid. 949 */ 950 value = TLSMODID(lmp); 951 } else 952 value = basebgn; 953 name = (char *)0; 954 } 955 956 DBG_CALL(Dbg_reloc_in(LIST(lmp), ELF_DBG_RTLD, M_MACH, 957 M_REL_SHT_TYPE, rel, NULL, name)); 958 959 /* 960 * If this object has relocations in the text segment, turn 961 * off the write protect. 962 */ 963 if ((roffset < etext) && (textrel == 0)) { 964 if (elf_set_prot(lmp, PROT_WRITE) == 0) { 965 ret = 0; 966 break; 967 } 968 textrel = 1; 969 } 970 971 /* 972 * Call relocation routine to perform required relocation. 973 */ 974 switch (rtype) { 975 case R_AMD64_COPY: 976 if (elf_copy_reloc(name, symref, lmp, (void *)roffset, 977 symdef, _lmp, (const void *)value) == 0) 978 ret = 0; 979 break; 980 case R_AMD64_JUMP_SLOT: 981 if (((LIST(lmp)->lm_tflags | FLAGS1(lmp)) & 982 (LML_TFLG_AUD_PLTENTER | LML_TFLG_AUD_PLTEXIT)) && 983 AUDINFO(lmp)->ai_dynplts) { 984 int fail = 0; 985 int pltndx = (((ulong_t)rel - 986 (uintptr_t)JMPREL(lmp)) / relsiz); 987 int symndx = (((uintptr_t)symdef - 988 (uintptr_t)SYMTAB(_lmp)) / 989 SYMENT(_lmp)); 990 991 (void) elf_plt_trace_write(roffset, lmp, _lmp, 992 symdef, symndx, pltndx, (caddr_t)value, 993 sb_flags, &fail); 994 if (fail) 995 ret = 0; 996 } else { 997 /* 998 * Write standard PLT entry to jump directly 999 * to newly bound function. 1000 */ 1001 DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), 1002 ELF_DBG_RTLD, (Xword)roffset, 1003 (Xword)value)); 1004 *(ulong_t *)roffset = value; 1005 } 1006 break; 1007 default: 1008 value += reladd; 1009 /* 1010 * Write the relocation out. 1011 */ 1012 if (do_reloc(rtype, (uchar_t *)roffset, 1013 (Xword *)&value, name, NAME(lmp), LIST(lmp)) == 0) 1014 ret = 0; 1015 1016 DBG_CALL(Dbg_reloc_apply_val(LIST(lmp), ELF_DBG_RTLD, 1017 (Xword)roffset, (Xword)value)); 1018 } 1019 1020 if ((ret == 0) && 1021 ((LIST(lmp)->lm_flags & LML_FLG_TRC_WARN) == 0)) 1022 break; 1023 1024 if (binfo) { 1025 DBG_CALL(Dbg_bind_global(lmp, (Addr)roffset, 1026 (Off)(roffset - basebgn), (Xword)(-1), PLT_T_FULL, 1027 _lmp, (Addr)value, symdef->st_value, name, binfo)); 1028 } 1029 } 1030 1031 return (relocate_finish(lmp, bound, textrel, ret)); 1032 } 1033 1034 /* 1035 * Initialize the first few got entries so that function calls go to 1036 * elf_rtbndr: 1037 * 1038 * GOT[GOT_XLINKMAP] = the address of the link map 1039 * GOT[GOT_XRTLD] = the address of rtbinder 1040 */ 1041 void 1042 elf_plt_init(void *got, caddr_t l) 1043 { 1044 uint64_t *_got; 1045 /* LINTED */ 1046 Rt_map *lmp = (Rt_map *)l; 1047 1048 _got = (uint64_t *)got + M_GOT_XLINKMAP; 1049 *_got = (uint64_t)lmp; 1050 _got = (uint64_t *)got + M_GOT_XRTLD; 1051 *_got = (uint64_t)elf_rtbndr; 1052 } 1053 1054 /* 1055 * Plt writing interface to allow debugging initialization to be generic. 1056 */ 1057 Pltbindtype 1058 /* ARGSUSED1 */ 1059 elf_plt_write(uintptr_t addr, uintptr_t vaddr, void *rptr, uintptr_t symval, 1060 Xword pltndx) 1061 { 1062 Rela *rel = (Rela*)rptr; 1063 uintptr_t pltaddr; 1064 1065 pltaddr = addr + rel->r_offset; 1066 *(ulong_t *)pltaddr = (ulong_t)symval + rel->r_addend; 1067 DBG_CALL(pltcntfull++); 1068 return (PLT_T_FULL); 1069 } 1070 1071 /* 1072 * Provide a machine specific interface to the conversion routine. By calling 1073 * the machine specific version, rather than the generic version, we insure that 1074 * the data tables/strings for all known machine versions aren't dragged into 1075 * ld.so.1. 1076 */ 1077 const char * 1078 _conv_reloc_type(uint_t rel) 1079 { 1080 return (conv_reloc_amd64_type(rel, 0)); 1081 } 1082