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 2008 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* 32 * set-up for relocations 33 */ 34 35 #define ELF_TARGET_AMD64 36 #define ELF_TARGET_SPARC 37 38 #include <string.h> 39 #include <stdio.h> 40 #include <alloca.h> 41 #include <debug.h> 42 #include "msg.h" 43 #include "_libld.h" 44 45 /* 46 * Set up the relocation table flag test macros so that they use the 47 * relocation table for the current target machine. 48 */ 49 #define IS_PLT(X) RELTAB_IS_PLT(X, ld_targ.t_mr.mr_reloc_table) 50 #define IS_GOT_RELATIVE(X) \ 51 RELTAB_IS_GOT_RELATIVE(X, ld_targ.t_mr.mr_reloc_table) 52 #define IS_GOT_PC(X) RELTAB_IS_GOT_PC(X, ld_targ.t_mr.mr_reloc_table) 53 #define IS_GOTPCREL(X) RELTAB_IS_GOTPCREL(X, ld_targ.t_mr.mr_reloc_table) 54 #define IS_GOT_BASED(X) RELTAB_IS_GOT_BASED(X, ld_targ.t_mr.mr_reloc_table) 55 #define IS_GOT_OPINS(X) RELTAB_IS_GOT_OPINS(X, ld_targ.t_mr.mr_reloc_table) 56 #define IS_GOT_REQUIRED(X) \ 57 RELTAB_IS_GOT_REQUIRED(X, ld_targ.t_mr.mr_reloc_table) 58 #define IS_PC_RELATIVE(X) RELTAB_IS_PC_RELATIVE(X, ld_targ.t_mr.mr_reloc_table) 59 #define IS_ADD_RELATIVE(X) \ 60 RELTAB_IS_ADD_RELATIVE(X, ld_targ.t_mr.mr_reloc_table) 61 #define IS_REGISTER(X) RELTAB_IS_REGISTER(X, ld_targ.t_mr.mr_reloc_table) 62 #define IS_NOTSUP(X) RELTAB_IS_NOTSUP(X, ld_targ.t_mr.mr_reloc_table) 63 #define IS_SEG_RELATIVE(X) \ 64 RELTAB_IS_SEG_RELATIVE(X, ld_targ.t_mr.mr_reloc_table) 65 #define IS_EXTOFFSET(X) RELTAB_IS_EXTOFFSET(X, ld_targ.t_mr.mr_reloc_table) 66 #define IS_SEC_RELATIVE(X) \ 67 RELTAB_IS_SEC_RELATIVE(X, ld_targ.t_mr.mr_reloc_table) 68 #define IS_TLS_INS(X) RELTAB_IS_TLS_INS(X, ld_targ.t_mr.mr_reloc_table) 69 #define IS_TLS_GD(X) RELTAB_IS_TLS_GD(X, ld_targ.t_mr.mr_reloc_table) 70 #define IS_TLS_LD(X) RELTAB_IS_TLS_LD(X, ld_targ.t_mr.mr_reloc_table) 71 #define IS_TLS_IE(X) RELTAB_IS_TLS_IE(X, ld_targ.t_mr.mr_reloc_table) 72 #define IS_TLS_LE(X) RELTAB_IS_TLS_LE(X, ld_targ.t_mr.mr_reloc_table) 73 #define IS_LOCALBND(X) RELTAB_IS_LOCALBND(X, ld_targ.t_mr.mr_reloc_table) 74 #define IS_SIZE(X) RELTAB_IS_SIZE(X, ld_targ.t_mr.mr_reloc_table) 75 76 /* 77 * Structure to hold copy relocation items. 78 */ 79 typedef struct copy_rel { 80 Sym_desc * copyrel_symd; /* symbol descriptor to be copied */ 81 Addr copyrel_stval; /* Original symbol value */ 82 } Copy_rel; 83 84 /* 85 * For each copy relocation symbol, determine if the symbol is: 86 * 1) to be *disp* relocated at runtime 87 * 2) a reference symbol for *disp* relocation 88 * 3) possibly *disp* relocated at ld time. 89 * 90 * The first and the second are serious errors. 91 */ 92 static void 93 is_disp_copied(Ofl_desc *ofl, Copy_rel *cpy) 94 { 95 Ifl_desc *ifl = cpy->copyrel_symd->sd_file; 96 Sym_desc *sdp = cpy->copyrel_symd; 97 Is_desc *irel; 98 Addr symaddr = cpy->copyrel_stval; 99 Listnode *lnp1; 100 Conv_inv_buf_t inv_buf; 101 102 /* 103 * This symbol may not be *disp* relocated at run time, but could 104 * already have been *disp* relocated when the shared object was 105 * created. Warn the user. 106 */ 107 if ((ifl->ifl_flags & FLG_IF_DISPDONE) && 108 (ofl->ofl_flags & FLG_OF_VERBOSE)) 109 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_DISPREL2), 110 conv_reloc_type(ifl->ifl_ehdr->e_machine, 111 ld_targ.t_m.m_r_copy, 0, &inv_buf), 112 ifl->ifl_name, demangle(sdp->sd_name)); 113 114 if ((ifl->ifl_flags & FLG_IF_DISPPEND) == 0) 115 return; 116 117 /* 118 * Traverse the input relocation sections. 119 */ 120 for (LIST_TRAVERSE(&ifl->ifl_relsect, lnp1, irel)) { 121 Sym_desc *rsdp; 122 Is_desc *trel; 123 Rel *rend, *reloc; 124 Xword rsize, entsize; 125 126 trel = ifl->ifl_isdesc[irel->is_shdr->sh_info]; 127 rsize = irel->is_shdr->sh_size; 128 entsize = irel->is_shdr->sh_entsize; 129 reloc = (Rel *)irel->is_indata->d_buf; 130 131 /* 132 * Decide entry size 133 */ 134 if ((entsize == 0) || (entsize > rsize)) { 135 if (irel->is_shdr->sh_type == SHT_RELA) 136 entsize = sizeof (Rela); 137 else 138 entsize = sizeof (Rel); 139 } 140 141 /* 142 * Traverse the relocation entries. 143 */ 144 for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize); 145 reloc < rend; 146 reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) { 147 const char *str; 148 Word rstndx; 149 150 if (IS_PC_RELATIVE(ELF_R_TYPE(reloc->r_info, 151 ld_targ.t_m.m_mach)) == 0) 152 continue; 153 154 /* 155 * First, check if this symbol is reference symbol 156 * for this relocation entry. 157 */ 158 rstndx = (Word) ELF_R_SYM(reloc->r_info); 159 rsdp = ifl->ifl_oldndx[rstndx]; 160 if (rsdp == sdp) { 161 if ((str = demangle(rsdp->sd_name)) != 162 rsdp->sd_name) { 163 char *_str = alloca(strlen(str) + 1); 164 (void) strcpy(_str, str); 165 str = (const char *)_str; 166 } 167 eprintf(ofl->ofl_lml, 168 ERR_WARNING, MSG_INTL(MSG_REL_DISPREL1), 169 conv_reloc_type(ifl->ifl_ehdr->e_machine, 170 (uint_t)ELF_R_TYPE(reloc->r_info, 171 ld_targ.t_m.m_mach), 172 0, &inv_buf), ifl->ifl_name, str, 173 MSG_INTL(MSG_STR_UNKNOWN), 174 EC_XWORD(reloc->r_offset), 175 demangle(sdp->sd_name)); 176 } 177 178 /* 179 * Then check if this relocation entry is relocating 180 * this symbol. 181 */ 182 if ((sdp->sd_isc != trel) || 183 (reloc->r_offset < symaddr) || 184 (reloc->r_offset >= 185 (symaddr + sdp->sd_sym->st_size))) 186 continue; 187 188 /* 189 * This symbol is truely *disp* relocated, so should 190 * really be fixed by user. 191 */ 192 if ((str = demangle(sdp->sd_name)) != sdp->sd_name) { 193 char *_str = alloca(strlen(str) + 1); 194 (void) strcpy(_str, str); 195 str = (const char *)_str; 196 } 197 eprintf(ofl->ofl_lml, ERR_WARNING, 198 MSG_INTL(MSG_REL_DISPREL1), 199 conv_reloc_type(ifl->ifl_ehdr->e_machine, 200 (uint_t)ELF_R_TYPE(reloc->r_info, 201 ld_targ.t_m.m_mach), 0, &inv_buf), 202 ifl->ifl_name, demangle(rsdp->sd_name), str, 203 EC_XWORD(reloc->r_offset), str); 204 } 205 } 206 } 207 208 /* 209 * The number of symbols provided by some objects can be very large. Use a 210 * binary search to match the associated value to a symbol table entry. 211 */ 212 static int 213 disp_bsearch(const void *key, const void *array) 214 { 215 Addr kvalue, avalue; 216 Ssv_desc *ssvp = (Ssv_desc *)array; 217 218 kvalue = *((Addr *)key); 219 avalue = ssvp->ssv_value; 220 221 if (avalue > kvalue) 222 return (-1); 223 if ((avalue < kvalue) && 224 ((avalue + ssvp->ssv_sdp->sd_sym->st_size) <= kvalue)) 225 return (1); 226 return (0); 227 } 228 229 /* 230 * Given a sorted list of symbols, look for a symbol in which the relocation 231 * offset falls between the [sym.st_value - sym.st_value + sym.st_size]. Since 232 * the symbol list is maintained in sorted order, we can bail once the 233 * relocation offset becomes less than the symbol values. The symbol is 234 * returned for use in error diagnostics. 235 */ 236 static Sym_desc * 237 disp_scansyms(Ifl_desc * ifl, Rel_desc *rld, Boolean rlocal, int inspect, 238 Ofl_desc *ofl) 239 { 240 Sym_desc *tsdp, *rsdp; 241 Sym *rsym, *tsym; 242 Ssv_desc *ssvp; 243 uchar_t rtype, ttype; 244 Addr value; 245 246 /* 247 * Sorted symbol values have been uniquified by adding their associated 248 * section offset. Uniquify the relocation offset by adding its 249 * associated section offset, and search for the symbol. 250 */ 251 value = rld->rel_roffset; 252 if (rld->rel_isdesc->is_shdr) 253 value += rld->rel_isdesc->is_shdr->sh_offset; 254 255 if ((ssvp = bsearch((void *)&value, (void *)ifl->ifl_sortsyms, 256 ifl->ifl_sortcnt, sizeof (Ssv_desc), &disp_bsearch)) != 0) 257 tsdp = ssvp->ssv_sdp; 258 else 259 tsdp = 0; 260 261 if (inspect) 262 return (tsdp); 263 264 /* 265 * Determine the relocation reference symbol and its type. 266 */ 267 rsdp = rld->rel_sym; 268 rsym = rsdp->sd_sym; 269 rtype = ELF_ST_TYPE(rsym->st_info); 270 271 /* 272 * If there is no target symbol to match the relocation offset, then the 273 * offset is effectively local data. If the relocation symbol is global 274 * data we have a potential for this displacement relocation to be 275 * invalidated should the global symbol be copied. 276 */ 277 if (tsdp == 0) { 278 if ((rlocal == TRUE) || 279 ((rtype != STT_OBJECT) && (rtype != STT_SECTION))) 280 return (tsdp); 281 } else { 282 /* 283 * If both symbols are local, no copy relocations can occur to 284 * either symbol. Note, this test is very similar to the test 285 * used in ld_sym_adjust_vis(). 286 */ 287 if ((rlocal == TRUE) && 288 ((tsdp->sd_flags1 & FLG_SY1_HIDDEN) || 289 (ELF_ST_BIND(tsdp->sd_sym->st_info) != STB_GLOBAL) || 290 ((ofl->ofl_flags & (FLG_OF_AUTOLCL | FLG_OF_AUTOELM)) && 291 ((tsdp->sd_flags1 & MSK_SY1_NOAUTO) == 0)))) 292 return (tsdp); 293 294 /* 295 * Determine the relocation target symbols type. 296 */ 297 tsym = tsdp->sd_sym; 298 ttype = ELF_ST_TYPE(tsym->st_info); 299 300 /* 301 * If the reference symbol is local, and the target isn't a 302 * data element, then no copy relocations can occur to either 303 * symbol. Note, this catches pc-relative relocations against 304 * the _GLOBAL_OFFSET_TABLE_, which is effectively treated as 305 * a local symbol. 306 */ 307 if ((rlocal == TRUE) && (ttype != STT_OBJECT) && 308 (ttype != STT_SECTION)) 309 return (tsdp); 310 311 /* 312 * Finally, one of the symbols must reference a data element. 313 */ 314 if ((rtype != STT_OBJECT) && (rtype != STT_SECTION) && 315 (ttype != STT_OBJECT) && (ttype != STT_SECTION)) 316 return (tsdp); 317 } 318 319 /* 320 * We have two global symbols, at least one of which is a data item. 321 * The last case where a displacement relocation can be ignored, is 322 * if the reference symbol is included in the target symbol. 323 */ 324 value = rsym->st_value; 325 if ((rld->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) 326 value += rld->rel_raddend; 327 328 if ((rld->rel_roffset >= value) && 329 (rld->rel_roffset < (value + rsym->st_size))) 330 return (tsdp); 331 332 /* 333 * We have a displacement relocation that could be compromised by a 334 * copy relocation of one of the associated data items. 335 */ 336 rld->rel_flags |= FLG_REL_DISP; 337 return (tsdp); 338 } 339 340 void 341 ld_disp_errmsg(const char *msg, Rel_desc *rsp, Ofl_desc *ofl) 342 { 343 Sym_desc *sdp; 344 const char *str; 345 Ifl_desc *ifl = rsp->rel_isdesc->is_file; 346 Conv_inv_buf_t inv_buf; 347 348 if ((sdp = disp_scansyms(ifl, rsp, 0, 1, ofl)) != 0) 349 str = demangle(sdp->sd_name); 350 else 351 str = MSG_INTL(MSG_STR_UNKNOWN); 352 353 eprintf(ofl->ofl_lml, ERR_WARNING, msg, 354 conv_reloc_type(ifl->ifl_ehdr->e_machine, rsp->rel_rtype, 355 0, &inv_buf), ifl->ifl_name, rsp->rel_sname, str, 356 EC_OFF(rsp->rel_roffset)); 357 } 358 359 /* 360 * qsort(3C) comparison routine used for the disp_sortsyms(). 361 */ 362 static int 363 disp_qsort(const void * s1, const void * s2) 364 { 365 Ssv_desc *ssvp1 = ((Ssv_desc *)s1); 366 Ssv_desc *ssvp2 = ((Ssv_desc *)s2); 367 Addr val1 = ssvp1->ssv_value; 368 Addr val2 = ssvp2->ssv_value; 369 370 if (val1 > val2) 371 return (1); 372 if (val1 < val2) 373 return (-1); 374 return (0); 375 } 376 377 /* 378 * Determine whether a displacement relocation is between a local and global 379 * symbol pair. One symbol is used to perform the relocation, and the other 380 * is the destination offset of the relocation. 381 */ 382 static uintptr_t 383 disp_inspect(Ofl_desc *ofl, Rel_desc *rld, Boolean rlocal) 384 { 385 Is_desc *isp = rld->rel_isdesc; 386 Ifl_desc *ifl = rld->rel_isdesc->is_file; 387 388 /* 389 * If the input files symbols haven't been sorted yet, do so. 390 */ 391 if (ifl->ifl_sortsyms == 0) { 392 Word ondx, nndx; 393 394 if ((ifl->ifl_sortsyms = libld_malloc((ifl->ifl_symscnt + 1) * 395 sizeof (Ssv_desc))) == 0) 396 return (S_ERROR); 397 398 for (ondx = 0, nndx = 0; ondx < ifl->ifl_symscnt; ondx++) { 399 Sym_desc *sdp; 400 Addr value; 401 402 /* 403 * As symbol resolution has already occurred, various 404 * symbols from this object may have been satisfied 405 * from other objects. Only select symbols from this 406 * object. For the displacement test, we only really 407 * need to observe data definitions, however, later as 408 * part of providing warning disgnostics, relating the 409 * relocation offset to a symbol is desirable. Thus, 410 * collect all symbols that define a memory area. 411 */ 412 if (((sdp = ifl->ifl_oldndx[ondx]) == 0) || 413 (sdp->sd_sym->st_shndx == SHN_UNDEF) || 414 (sdp->sd_sym->st_shndx >= SHN_LORESERVE) || 415 (sdp->sd_ref != REF_REL_NEED) || 416 (sdp->sd_file != ifl) || 417 (sdp->sd_sym->st_size == 0)) 418 continue; 419 420 /* 421 * As a further optimization for later checking, mark 422 * this section if this a global data definition. 423 */ 424 if (sdp->sd_isc && (ondx >= ifl->ifl_locscnt)) 425 sdp->sd_isc->is_flags |= FLG_IS_GDATADEF; 426 427 /* 428 * Capture the symbol. Within relocatable objects, a 429 * symbols value is its offset within its associated 430 * section. Add the section offset to this value to 431 * uniquify the symbol. 432 */ 433 value = sdp->sd_sym->st_value; 434 if (sdp->sd_isc && sdp->sd_isc->is_shdr) 435 value += sdp->sd_isc->is_shdr->sh_offset; 436 437 ifl->ifl_sortsyms[nndx].ssv_value = value; 438 ifl->ifl_sortsyms[nndx].ssv_sdp = sdp; 439 nndx++; 440 } 441 442 /* 443 * Sort the list based on the symbols value (address). 444 */ 445 if ((ifl->ifl_sortcnt = nndx) != 0) 446 qsort(ifl->ifl_sortsyms, nndx, sizeof (Ssv_desc), 447 &disp_qsort); 448 } 449 450 /* 451 * If the reference symbol is local, and the section being relocated 452 * contains no global definitions, neither can be the target of a copy 453 * relocation. 454 */ 455 if ((rlocal == FALSE) && ((isp->is_flags & FLG_IS_GDATADEF) == 0)) 456 return (1); 457 458 /* 459 * Otherwise determine whether this relocation symbol and its offset 460 * could be candidates for a copy relocation. 461 */ 462 if (ifl->ifl_sortcnt) 463 (void) disp_scansyms(ifl, rld, rlocal, 0, ofl); 464 return (1); 465 } 466 467 /* 468 * Add an active relocation record. 469 */ 470 uintptr_t 471 ld_add_actrel(Word flags, Rel_desc *rsp, Ofl_desc *ofl) 472 { 473 Rel_desc *arsp; 474 Rel_cache *rcp; 475 476 /* 477 * If no relocation cache structures are available, allocate a new 478 * one and link it into the bucket list. 479 */ 480 if ((ofl->ofl_actrels.tail == 0) || 481 ((rcp = (Rel_cache *)ofl->ofl_actrels.tail->data) == 0) || 482 ((arsp = rcp->rc_free) == rcp->rc_end)) { 483 static size_t nextsize = 0; 484 size_t size; 485 486 /* 487 * Typically, when generating an executable or shared object 488 * there will be an active relocation for every input 489 * relocation. 490 */ 491 if (nextsize == 0) { 492 if ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) { 493 if ((size = ofl->ofl_relocincnt) == 0) 494 size = REL_LAIDESCNO; 495 if (size > REL_HAIDESCNO) 496 nextsize = REL_HAIDESCNO; 497 else 498 nextsize = REL_LAIDESCNO; 499 } else 500 nextsize = size = REL_HAIDESCNO; 501 } else 502 size = nextsize; 503 504 size = size * sizeof (Rel_desc); 505 506 if (((rcp = libld_malloc(sizeof (Rel_cache) + size)) == 0) || 507 (list_appendc(&ofl->ofl_actrels, rcp) == 0)) 508 return (S_ERROR); 509 510 /* LINTED */ 511 rcp->rc_free = arsp = (Rel_desc *)(rcp + 1); 512 /* LINTED */ 513 rcp->rc_end = (Rel_desc *)((char *)rcp->rc_free + size); 514 } 515 516 *arsp = *rsp; 517 arsp->rel_flags |= flags; 518 519 rcp->rc_free++; 520 ofl->ofl_actrelscnt++; 521 522 /* 523 * Any GOT relocation reference requires the creation of a .got table. 524 * Most references to a .got require a .got entry, which is accounted 525 * for with the ofl_gotcnt counter. However, some references are 526 * relative to the .got table, but require no .got entry. This test 527 * insures a .got is created regardless of the type of reference. 528 */ 529 if (IS_GOT_REQUIRED(arsp->rel_rtype)) 530 ofl->ofl_flags |= FLG_OF_BLDGOT; 531 532 /* 533 * If this is a displacement relocation generate a warning. 534 */ 535 if (arsp->rel_flags & FLG_REL_DISP) { 536 ofl->ofl_dtflags_1 |= DF_1_DISPRELDNE; 537 538 if (ofl->ofl_flags & FLG_OF_VERBOSE) 539 ld_disp_errmsg(MSG_INTL(MSG_REL_DISPREL3), arsp, ofl); 540 } 541 542 DBG_CALL(Dbg_reloc_ars_entry(ofl->ofl_lml, ELF_DBG_LD, 543 arsp->rel_isdesc->is_shdr->sh_type, ld_targ.t_m.m_mach, arsp)); 544 return (1); 545 } 546 547 /* 548 * In the platform specific machrel.XXX.c files, we sometimes write 549 * a value directly into the got/plt. These function can be used when 550 * the running linker has the opposite byte order of the object being 551 * produced. 552 */ 553 Word 554 ld_bswap_Word(Word v) 555 { 556 return (BSWAP_WORD(v)); 557 } 558 559 560 Xword 561 ld_bswap_Xword(Xword v) 562 { 563 return (BSWAP_XWORD(v)); 564 } 565 566 567 uintptr_t 568 ld_reloc_GOT_relative(Boolean local, Rel_desc *rsp, Ofl_desc *ofl) 569 { 570 Sym_desc *sdp; 571 ofl_flag_t flags = ofl->ofl_flags; 572 Gotndx *gnp; 573 574 sdp = rsp->rel_sym; 575 576 /* 577 * If this is the first time we've seen this symbol in a GOT 578 * relocation we need to assign it a GOT token. Once we've got 579 * all of the GOT's assigned we can assign the actual indexes. 580 */ 581 if ((gnp = (*ld_targ.t_mr.mr_find_gotndx)(&(sdp->sd_GOTndxs), 582 GOT_REF_GENERIC, ofl, rsp)) == 0) { 583 Word rtype = rsp->rel_rtype; 584 585 if ((*ld_targ.t_mr.mr_assign_got_ndx)(&(sdp->sd_GOTndxs), 0, 586 GOT_REF_GENERIC, ofl, rsp, sdp) == S_ERROR) 587 return (S_ERROR); 588 589 /* 590 * Now we initialize the GOT table entry. 591 * 592 * Pseudo code to describe the the decisions below: 593 * 594 * If (local) 595 * then 596 * enter symbol value in GOT table entry 597 * if (Shared Object) 598 * then 599 * create Relative relocation against symbol 600 * fi 601 * else 602 * clear GOT table entry 603 * create a GLOB_DAT relocation against symbol 604 * fi 605 */ 606 if (local == TRUE) { 607 if (flags & FLG_OF_SHAROBJ) { 608 if (ld_add_actrel((FLG_REL_GOT | FLG_REL_GOTCL), 609 rsp, ofl) == S_ERROR) 610 return (S_ERROR); 611 612 /* 613 * Add a RELATIVE relocation if this is 614 * anything but a ABS symbol. 615 */ 616 if ((((sdp->sd_flags & FLG_SY_SPECSEC) == 0) || 617 (sdp->sd_sym->st_shndx != SHN_ABS)) || 618 (sdp->sd_aux && sdp->sd_aux->sa_symspec)) { 619 rsp->rel_rtype = 620 ld_targ.t_m.m_r_relative; 621 if ((*ld_targ.t_mr.mr_add_outrel) 622 ((FLG_REL_GOT | FLG_REL_ADVAL), rsp, 623 ofl) == S_ERROR) 624 return (S_ERROR); 625 rsp->rel_rtype = rtype; 626 } 627 } else { 628 if (ld_add_actrel(FLG_REL_GOT, rsp, 629 ofl) == S_ERROR) 630 return (S_ERROR); 631 } 632 } else { 633 rsp->rel_rtype = ld_targ.t_m.m_r_glob_dat; 634 if ((*ld_targ.t_mr.mr_add_outrel)(FLG_REL_GOT, 635 rsp, ofl) == S_ERROR) 636 return (S_ERROR); 637 rsp->rel_rtype = rtype; 638 } 639 } else { 640 if ((*ld_targ.t_mr.mr_assign_got_ndx)(&(sdp->sd_GOTndxs), gnp, 641 GOT_REF_GENERIC, ofl, rsp, sdp) == S_ERROR) 642 return (S_ERROR); 643 } 644 645 /* 646 * Perform relocation to GOT table entry. 647 */ 648 return (ld_add_actrel(NULL, rsp, ofl)); 649 } 650 651 652 /* 653 * Perform relocations for PLT's 654 */ 655 uintptr_t 656 ld_reloc_plt(Rel_desc *rsp, Ofl_desc *ofl) 657 { 658 Sym_desc *sdp = rsp->rel_sym; 659 660 switch (ld_targ.t_m.m_mach) { 661 case EM_AMD64: 662 /* 663 * AMD64 TLS code sequences do not use a unique TLS 664 * relocation to reference the __tls_get_addr() function call. 665 */ 666 if ((ofl->ofl_flags & FLG_OF_EXEC) && 667 (strcmp(sdp->sd_name, MSG_ORIG(MSG_SYM_TLSGETADDR_U)) == 668 0)) 669 return (ld_add_actrel(FLG_REL_TLSFIX, rsp, ofl)); 670 break; 671 672 case EM_386: 673 /* 674 * GNUC IA32 TLS code sequences do not use a unique TLS 675 * relocation to reference the ___tls_get_addr() function call. 676 */ 677 if ((ofl->ofl_flags & FLG_OF_EXEC) && 678 (strcmp(sdp->sd_name, MSG_ORIG(MSG_SYM_TLSGETADDR_UU)) == 679 0)) 680 return (ld_add_actrel(FLG_REL_TLSFIX, rsp, ofl)); 681 break; 682 } 683 684 /* 685 * if (not PLT yet assigned) 686 * then 687 * assign PLT index to symbol 688 * build output JMP_SLOT relocation 689 * fi 690 */ 691 if (sdp->sd_aux->sa_PLTndx == 0) { 692 Word ortype = rsp->rel_rtype; 693 694 (*ld_targ.t_mr.mr_assign_plt_ndx)(sdp, ofl); 695 696 /* 697 * If this symbol is binding to a LAZYLOADED object then 698 * set the LAZYLD symbol flag. 699 */ 700 if ((sdp->sd_aux->sa_bindto && 701 (sdp->sd_aux->sa_bindto->ifl_flags & FLG_IF_LAZYLD)) || 702 (sdp->sd_file && 703 (sdp->sd_file->ifl_flags & FLG_IF_LAZYLD))) 704 sdp->sd_flags |= FLG_SY_LAZYLD; 705 706 rsp->rel_rtype = ld_targ.t_m.m_r_jmp_slot; 707 if ((*ld_targ.t_mr.mr_add_outrel)(FLG_REL_PLT, rsp, ofl) == 708 S_ERROR) 709 return (S_ERROR); 710 rsp->rel_rtype = ortype; 711 } 712 713 /* 714 * Perform relocation to PLT table entry. 715 */ 716 if ((ofl->ofl_flags & FLG_OF_SHAROBJ) && 717 IS_ADD_RELATIVE(rsp->rel_rtype)) { 718 Word ortype = rsp->rel_rtype; 719 720 rsp->rel_rtype = ld_targ.t_m.m_r_relative; 721 if ((*ld_targ.t_mr.mr_add_outrel)(FLG_REL_ADVAL, rsp, ofl) == 722 S_ERROR) 723 return (S_ERROR); 724 rsp->rel_rtype = ortype; 725 return (1); 726 } else 727 return (ld_add_actrel(NULL, rsp, ofl)); 728 } 729 730 /* 731 * process GLOBAL undefined and ref_dyn_need symbols. 732 */ 733 static uintptr_t 734 reloc_exec(Rel_desc *rsp, Ofl_desc *ofl) 735 { 736 Sym_desc *_sdp, *sdp = rsp->rel_sym; 737 Sym_aux *sap = sdp->sd_aux; 738 Sym *sym = sdp->sd_sym; 739 Addr stval; 740 741 /* 742 * Reference is to a function so simply create a plt entry for it. 743 */ 744 if (ELF_ST_TYPE(sym->st_info) == STT_FUNC) 745 return (ld_reloc_plt(rsp, ofl)); 746 747 /* 748 * Catch absolutes - these may cause a text relocation. 749 */ 750 if ((sdp->sd_flags & FLG_SY_SPECSEC) && (sym->st_shndx == SHN_ABS)) { 751 if ((ofl->ofl_flags1 & FLG_OF1_ABSEXEC) == 0) 752 return ((*ld_targ.t_mr.mr_add_outrel)(NULL, rsp, ofl)); 753 754 /* 755 * If -zabsexec is set then promote the ABSOLUTE symbol to 756 * current the current object and perform the relocation now. 757 */ 758 sdp->sd_ref = REF_REL_NEED; 759 return (ld_add_actrel(NULL, rsp, ofl)); 760 } 761 762 /* 763 * If the relocation is against a writable section simply compute the 764 * necessary output relocation. As an optimization, if the symbol has 765 * already been transformed into a copy relocation then we can perform 766 * the relocation directly (copy relocations should only be generated 767 * for references from the text segment and these relocations are 768 * normally carried out before we get to the data segment relocations). 769 */ 770 if ((ELF_ST_TYPE(sym->st_info) == STT_OBJECT) && 771 (rsp->rel_osdesc->os_shdr->sh_flags & SHF_WRITE)) { 772 if (sdp->sd_flags & FLG_SY_MVTOCOMM) 773 return (ld_add_actrel(NULL, rsp, ofl)); 774 else 775 return ((*ld_targ.t_mr.mr_add_outrel)(NULL, rsp, ofl)); 776 } 777 778 /* 779 * If the reference isn't to an object (normally because a .type 780 * directive hasn't defined in some assembler source), then simply apply 781 * a generic relocation (this has a tendency to result in text 782 * relocations). 783 */ 784 if (ELF_ST_TYPE(sym->st_info) != STT_OBJECT) { 785 Conv_inv_buf_t inv_buf; 786 787 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_UNEXPSYM), 788 conv_sym_info_type(sdp->sd_file->ifl_ehdr->e_machine, 789 ELF_ST_TYPE(sym->st_info), 0, &inv_buf), 790 rsp->rel_isdesc->is_file->ifl_name, 791 demangle(rsp->rel_sname), sdp->sd_file->ifl_name); 792 return ((*ld_targ.t_mr.mr_add_outrel)(NULL, rsp, ofl)); 793 } 794 795 /* 796 * Prepare for generating a copy relocation. 797 * 798 * If this symbol is one of an alias pair, we need to insure both 799 * symbols become part of the output (the strong symbol will be used to 800 * maintain the symbols state). And, if we did raise the precedence of 801 * a symbol we need to check and see if this is a weak symbol. If it is 802 * we want to use it's strong counter part. 803 * 804 * The results of this logic should be: 805 * rel_usym: assigned to strong 806 * rel_sym: assigned to symbol to perform 807 * copy_reloc against (weak or strong). 808 */ 809 if (sap->sa_linkndx) { 810 _sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx]; 811 812 if (_sdp->sd_ref < sdp->sd_ref) { 813 _sdp->sd_ref = sdp->sd_ref; 814 _sdp->sd_flags |= FLG_SY_REFRSD; 815 816 /* 817 * As we're going to replicate a symbol from a shared 818 * object, retain its correct binding status. 819 */ 820 if (ELF_ST_BIND(_sdp->sd_sym->st_info) == STB_GLOBAL) 821 _sdp->sd_flags |= FLG_SY_GLOBREF; 822 823 } else if (_sdp->sd_ref > sdp->sd_ref) { 824 sdp->sd_ref = _sdp->sd_ref; 825 sdp->sd_flags |= FLG_SY_REFRSD; 826 827 /* 828 * As we're going to replicate a symbol from a shared 829 * object, retain its correct binding status. 830 */ 831 if (ELF_ST_BIND(sym->st_info) == STB_GLOBAL) 832 sdp->sd_flags |= FLG_SY_GLOBREF; 833 } 834 835 /* 836 * If this is a weak symbol then we want to move the strong 837 * symbol into local .bss. If there is a copy_reloc to be 838 * performed, that should still occur against the WEAK symbol. 839 */ 840 if ((ELF_ST_BIND(sdp->sd_sym->st_info) == STB_WEAK) || 841 (sdp->sd_flags & FLG_SY_WEAKDEF)) 842 rsp->rel_usym = _sdp; 843 } else 844 _sdp = 0; 845 846 /* 847 * If the reference is to an object then allocate space for the object 848 * within the executables .bss. Relocations will now be performed from 849 * this new location. If the original shared objects data is 850 * initialized, then generate a copy relocation that will copy the data 851 * to the executables .bss at runtime. 852 */ 853 if (!(rsp->rel_usym->sd_flags & FLG_SY_MVTOCOMM)) { 854 Word rtype = rsp->rel_rtype; 855 Copy_rel *cpy_rel; 856 857 /* 858 * Indicate that the symbol(s) against which we're relocating 859 * have been moved to the executables common. Also, insure that 860 * the symbol(s) remain marked as global, as the shared object 861 * from which they are copied must be able to relocate to the 862 * new common location within the executable. 863 * 864 * Note that even though a new symbol has been generated in the 865 * output files' .bss, the symbol must remain REF_DYN_NEED and 866 * not be promoted to REF_REL_NEED. sym_validate() still needs 867 * to carry out a number of checks against the symbols binding 868 * that are triggered by the REF_DYN_NEED state. 869 */ 870 sdp->sd_flags |= FLG_SY_MVTOCOMM; 871 sdp->sd_flags1 |= (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF); 872 sdp->sd_flags1 &= ~MSK_SY1_LOCAL; 873 sdp->sd_sym->st_other &= ~MSK_SYM_VISIBILITY; 874 if (_sdp) { 875 _sdp->sd_flags |= FLG_SY_MVTOCOMM; 876 _sdp->sd_flags1 |= (FLG_SY1_DEFAULT | FLG_SY1_EXPDEF); 877 _sdp->sd_flags1 &= ~MSK_SY1_LOCAL; 878 _sdp->sd_sym->st_other &= ~MSK_SYM_VISIBILITY; 879 880 /* 881 * Make sure the symbol has a reference in case of any 882 * error diagnostics against it (perhaps this belongs 883 * to a version that isn't allowable for this build). 884 * The resulting diagnostic (see sym_undef_entry()) 885 * might seem a little bogus, as the symbol hasn't 886 * really been referenced by this file, but has been 887 * promoted as a consequence of its alias reference. 888 */ 889 if (!(_sdp->sd_aux->sa_rfile)) 890 _sdp->sd_aux->sa_rfile = sdp->sd_aux->sa_rfile; 891 } 892 893 /* 894 * Assign the symbol to the bss and insure sufficient alignment 895 * (we don't know the real alignment so we have to make the 896 * worst case guess). 897 */ 898 _sdp = rsp->rel_usym; 899 stval = _sdp->sd_sym->st_value; 900 if (ld_sym_copy(_sdp) == S_ERROR) 901 return (S_ERROR); 902 _sdp->sd_shndx = _sdp->sd_sym->st_shndx = SHN_COMMON; 903 _sdp->sd_flags |= FLG_SY_SPECSEC; 904 _sdp->sd_sym->st_value = 905 (_sdp->sd_sym->st_size < (ld_targ.t_m.m_word_align * 2)) ? 906 ld_targ.t_m.m_word_align : ld_targ.t_m.m_word_align * 2; 907 908 /* 909 * Whether or not the symbol references initialized 910 * data we generate a copy relocation - this differs 911 * from the past where we would not create the COPY_RELOC 912 * if we were binding against .bss. This is done 913 * for *two* reasons. 914 * 915 * o If the symbol in the shared object changes to 916 * a initialized data - we need the COPY to pick it 917 * up. 918 * o without the COPY RELOC we can't tell that the 919 * symbol from the COPY'd object has been moved 920 * and all bindings to it should bind here. 921 */ 922 923 /* 924 * Keep this symbol in the copy relocation list 925 * to check the validity later. 926 */ 927 if ((cpy_rel = libld_malloc(sizeof (Copy_rel))) == 0) 928 return (S_ERROR); 929 cpy_rel->copyrel_symd = _sdp; 930 cpy_rel->copyrel_stval = stval; 931 if (list_appendc(&ofl->ofl_copyrels, cpy_rel) == 0) 932 return (S_ERROR); 933 934 rsp->rel_rtype = ld_targ.t_m.m_r_copy; 935 if ((*ld_targ.t_mr.mr_add_outrel)(FLG_REL_BSS, rsp, ofl) == 936 S_ERROR) 937 return (S_ERROR); 938 rsp->rel_rtype = rtype; 939 940 /* 941 * If this symbol is a protected symbol, warn it. 942 */ 943 if (_sdp->sd_flags & FLG_SY_PROT) { 944 Conv_inv_buf_t inv_buf; 945 946 eprintf(ofl->ofl_lml, ERR_WARNING, 947 MSG_INTL(MSG_REL_COPY), 948 conv_reloc_type(_sdp->sd_file->ifl_ehdr->e_machine, 949 ld_targ.t_m.m_r_copy, 0, &inv_buf), 950 _sdp->sd_file->ifl_name, _sdp->sd_name); 951 } 952 DBG_CALL(Dbg_syms_reloc(ofl, sdp)); 953 } 954 return (ld_add_actrel(NULL, rsp, ofl)); 955 } 956 957 /* 958 * All relocations should have been handled by the other routines. This 959 * routine is here as a catch all, if we do enter it we've goofed - but 960 * we'll try and to the best we can. 961 */ 962 static uintptr_t 963 reloc_generic(Rel_desc *rsp, Ofl_desc *ofl) 964 { 965 Ifl_desc *ifl = rsp->rel_isdesc->is_file; 966 Conv_inv_buf_t inv_buf; 967 968 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_UNEXPREL), 969 conv_reloc_type(ifl->ifl_ehdr->e_machine, rsp->rel_rtype, 970 0, &inv_buf), ifl->ifl_name, demangle(rsp->rel_sname)); 971 972 /* 973 * If building a shared object then put the relocation off 974 * until runtime. 975 */ 976 if (ofl->ofl_flags & FLG_OF_SHAROBJ) 977 return ((*ld_targ.t_mr.mr_add_outrel)(NULL, rsp, ofl)); 978 979 /* 980 * Otherwise process relocation now. 981 */ 982 return (ld_add_actrel(NULL, rsp, ofl)); 983 } 984 985 /* 986 * Process relocations when building a relocatable object. Typically, there 987 * aren't many relocations that can be caught at this point, most are simply 988 * passed through to the output relocatable object. 989 */ 990 static uintptr_t 991 reloc_relobj(Boolean local, Rel_desc *rsp, Ofl_desc *ofl) 992 { 993 Word rtype = rsp->rel_rtype; 994 Sym_desc *sdp = rsp->rel_sym; 995 Is_desc *isp = rsp->rel_isdesc; 996 Word oflags = NULL; 997 998 /* 999 * Determine if we can do any relocations at this point. We can if: 1000 * 1001 * this is local_symbol and a non-GOT relocation, and 1002 * the relocation is pc-relative, and 1003 * the relocation is against a symbol in same section 1004 */ 1005 if (local && !IS_GOT_RELATIVE(rtype) && 1006 !IS_GOT_BASED(rtype) && !IS_GOT_PC(rtype) && 1007 IS_PC_RELATIVE(rtype) && 1008 ((sdp->sd_isc) && (sdp->sd_isc->is_osdesc == isp->is_osdesc))) 1009 return (ld_add_actrel(NULL, rsp, ofl)); 1010 1011 /* 1012 * If -zredlocsym is in effect, translate all local symbol relocations 1013 * to be against section symbols, since section symbols are the only 1014 * local symbols which will be added to the .symtab. 1015 */ 1016 if (local && (((ofl->ofl_flags & FLG_OF_REDLSYM) && 1017 (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL)) || 1018 ((sdp->sd_flags1 & FLG_SY1_ELIM) && 1019 (ofl->ofl_flags & FLG_OF_PROCRED)))) { 1020 /* 1021 * But if this is PIC code, don't allow it for now. 1022 */ 1023 if (IS_GOT_RELATIVE(rsp->rel_rtype)) { 1024 Ifl_desc *ifl = rsp->rel_isdesc->is_file; 1025 Conv_inv_buf_t inv_buf; 1026 1027 eprintf(ofl->ofl_lml, ERR_FATAL, 1028 MSG_INTL(MSG_REL_PICREDLOC), 1029 demangle(rsp->rel_sname), ifl->ifl_name, 1030 conv_reloc_type(ifl->ifl_ehdr->e_machine, 1031 rsp->rel_rtype, 0, &inv_buf)); 1032 return (S_ERROR); 1033 } 1034 1035 /* 1036 * Indicate that this relocation should be processed the same 1037 * as a section symbol. For RELA, indicate that the addend 1038 * also needs to be applied to this relocation. 1039 */ 1040 if ((rsp->rel_flags & FLG_REL_RELA) == FLG_REL_RELA) 1041 oflags = FLG_REL_SCNNDX | FLG_REL_ADVAL; 1042 else 1043 oflags = FLG_REL_SCNNDX; 1044 } 1045 1046 if ((rsp->rel_flags & FLG_REL_RELA) == 0) { 1047 /* 1048 * Intel (Rel) relocations do not contain an addend. Any 1049 * addend is contained within the file at the location 1050 * identified by the relocation offset. Therefore, if we're 1051 * processing a section symbol, or a -zredlocsym relocation 1052 * (that basically transforms a local symbol reference into 1053 * a section reference), perform an active relocation to 1054 * propagate any addend. 1055 */ 1056 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) || 1057 (oflags == FLG_REL_SCNNDX)) 1058 if (ld_add_actrel(NULL, rsp, ofl) == S_ERROR) 1059 return (S_ERROR); 1060 } 1061 return ((*ld_targ.t_mr.mr_add_outrel)(oflags, rsp, ofl)); 1062 } 1063 1064 /* 1065 * Perform any generic TLS validations before passing control to machine 1066 * specific routines. At this point we know we are dealing with an executable 1067 * or shared object - relocatable objects have already been processed. 1068 */ 1069 static uintptr_t 1070 reloc_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl) 1071 { 1072 Word rtype = rsp->rel_rtype; 1073 ofl_flag_t flags = ofl->ofl_flags; 1074 Ifl_desc *ifl = rsp->rel_isdesc->is_file; 1075 Half mach = ifl->ifl_ehdr->e_machine; 1076 Sym_desc *sdp = rsp->rel_sym; 1077 unsigned char type; 1078 Conv_inv_buf_t inv_buf1, inv_buf2; 1079 1080 /* 1081 * All TLS relocations are illegal in a static executable. 1082 */ 1083 if ((flags & (FLG_OF_STATIC | FLG_OF_EXEC)) == 1084 (FLG_OF_STATIC | FLG_OF_EXEC)) { 1085 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSSTAT), 1086 conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name, 1087 demangle(rsp->rel_sname)); 1088 return (S_ERROR); 1089 } 1090 1091 /* 1092 * Any TLS relocation must be against a STT_TLS symbol, all others 1093 * are illegal. 1094 */ 1095 if ((type = ELF_ST_TYPE(sdp->sd_sym->st_info)) != STT_TLS) { 1096 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSBADSYM), 1097 conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name, 1098 demangle(rsp->rel_sname), 1099 conv_sym_info_type(mach, type, 0, &inv_buf2)); 1100 return (S_ERROR); 1101 } 1102 1103 /* 1104 * A dynamic executable can not use the LD or LE reference models to 1105 * reference an external symbol. A shared object can not use the LD 1106 * reference model to reference an external symbol. 1107 */ 1108 if (!local && (IS_TLS_LD(rtype) || 1109 ((flags & FLG_OF_EXEC) && IS_TLS_LE(rtype)))) { 1110 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_TLSBND), 1111 conv_reloc_type(mach, rtype, 0, &inv_buf1), ifl->ifl_name, 1112 demangle(rsp->rel_sname), sdp->sd_file->ifl_name); 1113 return (S_ERROR); 1114 } 1115 1116 /* 1117 * The TLS LE model is only allowed for dynamic executables. The TLS IE 1118 * model is allowed for shared objects, but this model has restrictions. 1119 * This model can only be used freely in dependencies that are loaded 1120 * immediately as part of process initialization. However, during the 1121 * initial runtime handshake with libc that establishes the thread 1122 * pointer, a small backup TLS reservation is created. This area can 1123 * be used by objects that are loaded after threads are initialized. 1124 * However, this area is limited in size and may have already been 1125 * used. This area is intended for specialized applications, and does 1126 * not provide the degree of flexibility dynamic TLS can offer. Under 1127 * -z verbose indicate this restriction to the user. 1128 */ 1129 if ((flags & FLG_OF_EXEC) == 0) { 1130 if (IS_TLS_LE(rtype)) { 1131 eprintf(ofl->ofl_lml, ERR_FATAL, 1132 MSG_INTL(MSG_REL_TLSLE), 1133 conv_reloc_type(mach, rtype, 0, &inv_buf1), 1134 ifl->ifl_name, demangle(rsp->rel_sname)); 1135 return (S_ERROR); 1136 1137 } else if ((IS_TLS_IE(rtype)) && 1138 (flags & FLG_OF_VERBOSE)) { 1139 eprintf(ofl->ofl_lml, ERR_WARNING, 1140 MSG_INTL(MSG_REL_TLSIE), 1141 conv_reloc_type(mach, rtype, 0, &inv_buf1), 1142 ifl->ifl_name, demangle(rsp->rel_sname)); 1143 } 1144 } 1145 1146 return ((*ld_targ.t_mr.mr_reloc_TLS)(local, rsp, ofl)); 1147 } 1148 1149 uintptr_t 1150 ld_process_sym_reloc(Ofl_desc *ofl, Rel_desc *reld, Rel *reloc, Is_desc *isp, 1151 const char *isname) 1152 { 1153 Word rtype = reld->rel_rtype; 1154 ofl_flag_t flags = ofl->ofl_flags; 1155 Sym_desc *sdp = reld->rel_sym; 1156 Sym_aux *sap; 1157 Boolean local; 1158 Conv_inv_buf_t inv_buf; 1159 1160 DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD, ld_targ.t_m.m_mach, 1161 ld_targ.t_m.m_rel_sht_type, (void *)reloc, isname, 1162 reld->rel_sname)); 1163 1164 /* 1165 * Indicate this symbol is being used for relocation and therefore must 1166 * have its output address updated accordingly (refer to update_osym()). 1167 */ 1168 sdp->sd_flags |= FLG_SY_UPREQD; 1169 1170 /* 1171 * Indicate the section this symbol is defined in has been referenced, 1172 * therefor it *is not* a candidate for elimination. 1173 */ 1174 if (sdp->sd_isc) { 1175 sdp->sd_isc->is_flags |= FLG_IS_SECTREF; 1176 sdp->sd_isc->is_file->ifl_flags |= FLG_IF_FILEREF; 1177 } 1178 1179 reld->rel_usym = sdp; 1180 1181 /* 1182 * Determine if this symbol is actually an alias to another symbol. If 1183 * so, and the alias is not REF_DYN_SEEN, set rel_usym to point to the 1184 * weak symbols strong counter-part. The one exception is if the 1185 * FLG_SY_MVTOCOMM flag is set on the weak symbol. If this is the case, 1186 * the strong is only here because of its promotion, and the weak symbol 1187 * should still be used for the relocation reference (see reloc_exec()). 1188 */ 1189 sap = sdp->sd_aux; 1190 if (sap && sap->sa_linkndx && 1191 ((ELF_ST_BIND(sdp->sd_sym->st_info) == STB_WEAK) || 1192 (sdp->sd_flags & FLG_SY_WEAKDEF)) && 1193 (!(sdp->sd_flags & FLG_SY_MVTOCOMM))) { 1194 Sym_desc * _sdp; 1195 1196 _sdp = sdp->sd_file->ifl_oldndx[sap->sa_linkndx]; 1197 if (_sdp->sd_ref != REF_DYN_SEEN) 1198 reld->rel_usym = _sdp; 1199 } 1200 1201 /* 1202 * Determine whether this symbol should be bound locally or not. 1203 * Symbols are bound locally if one of the following is true: 1204 * 1205 * o the symbol is of type STB_LOCAL. 1206 * 1207 * o the output image is not a relocatable object and the relocation 1208 * is relative to the .got. 1209 * 1210 * o the section being relocated is of type SHT_SUNW_dof. These 1211 * sections must be bound to the functions in the containing 1212 * object and can not be interposed upon. 1213 * 1214 * o the symbol has been reduced (scoped to a local or symbolic) and 1215 * reductions are being processed. 1216 * 1217 * o the -Bsymbolic flag is in use when building a shared object, 1218 * and the symbol hasn't explicitly been defined as nodirect. 1219 * 1220 * o an executable (fixed address) is being created, and the symbol 1221 * is defined in the executable. 1222 * 1223 * o the relocation is against a segment which will not be loaded 1224 * into memory. In this case, the relocation must be resolved 1225 * now, as ld.so.1 can not process relocations against unmapped 1226 * segments. 1227 */ 1228 local = FALSE; 1229 if (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL) { 1230 local = TRUE; 1231 } else if (!(reld->rel_flags & FLG_REL_LOAD)) { 1232 local = TRUE; 1233 } else if (sdp->sd_sym->st_shndx != SHN_UNDEF) { 1234 if (reld->rel_isdesc && 1235 reld->rel_isdesc->is_shdr->sh_type == SHT_SUNW_dof) { 1236 local = TRUE; 1237 } else if (!(flags & FLG_OF_RELOBJ) && 1238 (IS_LOCALBND(rtype) || 1239 IS_SEG_RELATIVE(rtype))) { 1240 local = TRUE; 1241 } else if (sdp->sd_ref == REF_REL_NEED) { 1242 /* 1243 * Global symbols may have been individually reduced in 1244 * scope. If the whole object is to be self contained, 1245 * such as when generating an executable or a symbolic 1246 * shared object, make sure all relocation symbol 1247 * references (sections too) are treated locally. Note, 1248 * explicit no-direct symbols should not be bound to 1249 * locally. 1250 */ 1251 if ((sdp->sd_flags1 & 1252 (FLG_SY1_HIDDEN | FLG_SY1_PROTECT))) 1253 local = TRUE; 1254 else if ((flags & FLG_OF_EXEC) || 1255 ((flags & FLG_OF_SYMBOLIC) && 1256 ((sdp->sd_flags1 & FLG_SY1_NDIR) == 0))) 1257 local = TRUE; 1258 } 1259 } 1260 1261 /* 1262 * If this is a PC_RELATIVE relocation, the relocation could be 1263 * compromised if the relocated address is later used as a copy 1264 * relocated symbol (PSARC 1999/636, bugid 4187211). Scan the input 1265 * files symbol table to cross reference this relocation offset. 1266 */ 1267 if ((ofl->ofl_flags & FLG_OF_SHAROBJ) && 1268 IS_PC_RELATIVE(rtype) && 1269 (IS_GOT_PC(rtype) == 0) && 1270 (IS_PLT(rtype) == 0)) { 1271 if (disp_inspect(ofl, reld, local) == S_ERROR) 1272 return (S_ERROR); 1273 } 1274 1275 /* 1276 * GOT based relocations must bind to the object being built - since 1277 * they are relevant to the current GOT. If not building a relocatable 1278 * object - give a appropriate error message. 1279 */ 1280 if (!local && !(flags & FLG_OF_RELOBJ) && 1281 IS_GOT_BASED(rtype)) { 1282 Ifl_desc *ifl = reld->rel_isdesc->is_file; 1283 1284 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_BADGOTBASED), 1285 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype, 1286 0, &inv_buf), ifl->ifl_name, demangle(sdp->sd_name)); 1287 return (S_ERROR); 1288 } 1289 1290 /* 1291 * TLS symbols can only have TLS relocations. 1292 */ 1293 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_TLS) && 1294 (IS_TLS_INS(rtype) == 0)) { 1295 /* 1296 * The above test is relaxed if the target section is 1297 * non-allocable. 1298 */ 1299 if (reld->rel_osdesc->os_shdr->sh_flags & SHF_ALLOC) { 1300 Ifl_desc *ifl = reld->rel_isdesc->is_file; 1301 1302 eprintf(ofl->ofl_lml, ERR_FATAL, 1303 MSG_INTL(MSG_REL_BADTLS), 1304 conv_reloc_type(ifl->ifl_ehdr->e_machine, 1305 rtype, 0, &inv_buf), ifl->ifl_name, 1306 demangle(sdp->sd_name)); 1307 return (S_ERROR); 1308 } 1309 } 1310 1311 /* 1312 * Select the relocation to perform. 1313 */ 1314 if (IS_REGISTER(rtype)) { 1315 if (ld_targ.t_mr.mr_reloc_register == NULL) { 1316 eprintf(ofl->ofl_lml, ERR_FATAL, 1317 MSG_INTL(MSG_REL_NOREG)); 1318 return (S_ERROR); 1319 } 1320 return ((*ld_targ.t_mr.mr_reloc_register)(reld, isp, ofl)); 1321 } 1322 1323 if (flags & FLG_OF_RELOBJ) 1324 return (reloc_relobj(local, reld, ofl)); 1325 1326 if (IS_TLS_INS(rtype)) 1327 return (reloc_TLS(local, reld, ofl)); 1328 1329 if (IS_GOT_OPINS(rtype)) { 1330 if (ld_targ.t_mr.mr_reloc_GOTOP == NULL) { 1331 assert(0); 1332 return (S_ERROR); 1333 } 1334 return ((*ld_targ.t_mr.mr_reloc_GOTOP)(local, reld, ofl)); 1335 } 1336 1337 if (IS_GOT_RELATIVE(rtype)) 1338 return (ld_reloc_GOT_relative(local, reld, ofl)); 1339 1340 if (local) 1341 return ((*ld_targ.t_mr.mr_reloc_local)(reld, ofl)); 1342 1343 if ((IS_PLT(rtype)) && ((flags & FLG_OF_BFLAG) == 0)) 1344 return (ld_reloc_plt(reld, ofl)); 1345 1346 if ((sdp->sd_ref == REF_REL_NEED) || 1347 (flags & FLG_OF_BFLAG) || (flags & FLG_OF_SHAROBJ) || 1348 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_NOTYPE)) 1349 return ((*ld_targ.t_mr.mr_add_outrel)(NULL, reld, ofl)); 1350 1351 if (sdp->sd_ref == REF_DYN_NEED) 1352 return (reloc_exec(reld, ofl)); 1353 1354 /* 1355 * IS_NOT_REL(rtype) 1356 */ 1357 return (reloc_generic(reld, ofl)); 1358 } 1359 1360 /* 1361 * Given a relocation that references a local symbol from a discarded 1362 * COMDAT (or SHF_GROUP) section, replace the symbol with the 1363 * corresponding symbol from the section that was kept. 1364 * 1365 * entry: 1366 * reld - Relocation 1367 * orig_sdp - Symbol to be replaced. Must be a local symbol (STB_LOCAL). 1368 * 1369 * exit: 1370 * Returns address of replacement symbol descriptor if one was 1371 * found, and NULL otherwise. 1372 * 1373 * note: 1374 * [Note that I'm using the word "COMDAT" here loosely, to refer 1375 * to actual COMDAT sections as well as to groups tied together 1376 * with an SHF_GROUP section. SHF_GROUP is simply a more advanced 1377 * version of the same idea: That multiple versions of the same thing 1378 * can come in, but only one of them is used for the output.] 1379 * 1380 * In principle, this sort of sloppy relocation remapping is 1381 * a questionable practice. All self-referential sections should 1382 * be in a common SHF_GROUP so that they are all kept or removed 1383 * together. The problem is that there is no way to ensure that the 1384 * two sections are similar enough that the replacement section will 1385 * really supply the correct information. However, we see a couple of 1386 * situations where it is useful to do this: (1) Older Sun C compilers 1387 * generated DWARF sections that would refer to one of the COMDAT 1388 * sections, and (2) gcc, when its COMDAT feature is enabled. 1389 * It turns out that the GNU ld does these sloppy remappings. 1390 * 1391 * The GNU ld takes an approach that hard wires special section 1392 * names and treats them specially. We avoid that practice and 1393 * try to get the necessary work done relying only on the ELF 1394 * attributes of the sections and symbols involved. This means 1395 * that our heuristic is somewhat different than theirs, but the 1396 * end result is close enough to solve the same problem. 1397 * 1398 * It is our hope that gcc will eventually phase out the need 1399 * for sloppy relocations, and this won't be needed. In the 1400 * meantime, the option allows us to interoperate. 1401 * 1402 * Here is how we do it: The symbol points at the input section, 1403 * and the input section points at the output section to which it 1404 * is assigned. The output section contains a list of all of the 1405 * input sections that have been mapped to it, including the 1406 * corresponding COMDAT section that was kept. The kept COMDAT 1407 * section contains a reference to its input file, where we can find 1408 * the array of all the symbols in that file. From the input file, 1409 * we then locate the corresponding symbol that references the kept 1410 * COMDAT section. 1411 * 1412 */ 1413 static Sym_desc * 1414 sloppy_comdat_reloc(Ofl_desc *ofl, Rel_desc *reld, Sym_desc *sdp) 1415 { 1416 Listnode *lnp; 1417 Is_desc *rep_isp; 1418 const char *is_name; 1419 Sym *sym = sdp->sd_sym; 1420 Is_desc *isp = sdp->sd_isc; 1421 Conv_inv_buf_t inv_buf; 1422 1423 /* 1424 * ld_place_section() can alter the section name if it contains 1425 * a '%' character. We need to use the original name in this 1426 * case. 1427 */ 1428 is_name = isp->is_basename ? isp->is_basename : isp->is_name; 1429 1430 /* 1431 * 1) The caller is required to ensure that the input symbol is 1432 * local. We don't check for that here. 1433 * 2) If the discarded section has not been assigned to an 1434 * output section, we won't be able to continue. 1435 * 3) This operation only applies to SHT_SUNW_COMDAT sections 1436 * or sections contained within a COMDAT group (SHF_GROUP). 1437 */ 1438 if ((isp->is_osdesc == NULL) || 1439 ((isp->is_shdr->sh_type != SHT_SUNW_COMDAT) && 1440 ((isp->is_shdr->sh_flags & SHF_GROUP) == 0))) 1441 return (NULL); 1442 1443 /* 1444 * Examine each input section assigned to this output section. 1445 * The replacement section must: 1446 * - Have the same name as the original 1447 * - Not have been discarded 1448 * - Have the same size 1449 * - Have the same section type (and note that this type may 1450 * be SHT_SUNW_COMDAT). 1451 * - Have the same SHF_GROUP flag setting (either on or off) 1452 * - Must be a COMDAT section of one form or the other. 1453 */ 1454 /* BEGIN CSTYLED */ 1455 for (LIST_TRAVERSE(&isp->is_osdesc->os_isdescs, lnp, rep_isp)) { 1456 const char *rep_is_name = rep_isp->is_basename ? 1457 rep_isp->is_basename : rep_isp->is_name; 1458 1459 if (!(rep_isp->is_flags & FLG_IS_DISCARD) && 1460 (isp->is_indata->d_size == rep_isp->is_indata->d_size) && 1461 (isp->is_shdr->sh_type == rep_isp->is_shdr->sh_type) && 1462 ((isp->is_shdr->sh_flags & SHF_GROUP) == 1463 (rep_isp->is_shdr->sh_flags & SHF_GROUP)) && 1464 (strcmp(rep_is_name, is_name) == 0)) { 1465 /* 1466 * We found the kept COMDAT section. Now, look at all of the 1467 * symbols from the input file that contains it to find the 1468 * symbol that corresponds to the one we started with: 1469 * - Hasn't been discarded 1470 * - Has section index of kept section 1471 * - If one symbol has a name, the other must have 1472 * the same name. The st_name field of a symbol 1473 * is 0 if there is no name, and is a string 1474 * table offset otherwise. The string table 1475 * offsets may well not agree --- it is the 1476 * actual string that matters. 1477 * - Type and binding attributes match (st_info) 1478 * - Values match (st_value) 1479 * - Sizes match (st_size) 1480 * - Visibility matches (st_other) 1481 */ 1482 Word scnndx = rep_isp->is_scnndx; 1483 Sym_desc **oldndx = rep_isp->is_file->ifl_oldndx; 1484 Word symscnt = rep_isp->is_file->ifl_symscnt; 1485 Sym_desc *rep_sdp; 1486 Sym *rep_sym; 1487 1488 while (symscnt--) { 1489 rep_sdp = *oldndx++; 1490 if (rep_sdp && !(rep_sdp->sd_flags & FLG_SY_ISDISC) && 1491 ((rep_sym = rep_sdp->sd_sym)->st_shndx == scnndx) && 1492 ((sym->st_name == 0) == (rep_sym->st_name == 0)) && 1493 !((sym->st_name != 0) && 1494 (strcmp(sdp->sd_name, rep_sdp->sd_name) != 0)) && 1495 (sym->st_info == rep_sym->st_info) && 1496 (sym->st_value == rep_sym->st_value) && 1497 (sym->st_size == rep_sym->st_size) && 1498 (sym->st_other == rep_sym->st_other)) { 1499 1500 if (ofl->ofl_flags & FLG_OF_VERBOSE) { 1501 Ifl_desc *ifl = sdp->sd_file; 1502 1503 if (sym->st_name != 0) { 1504 eprintf(ofl->ofl_lml, ERR_WARNING, 1505 MSG_INTL(MSG_REL_SLOPCDATNAM), 1506 conv_reloc_type(ifl->ifl_ehdr->e_machine, 1507 reld->rel_rtype, 0, &inv_buf), 1508 ifl->ifl_name, reld->rel_isdesc->is_name, 1509 rep_sdp->sd_name, is_name, 1510 rep_sdp->sd_file->ifl_name); 1511 } else { 1512 eprintf(ofl->ofl_lml, ERR_WARNING, 1513 MSG_INTL(MSG_REL_SLOPCDATNONAM), 1514 conv_reloc_type( 1515 ifl->ifl_ehdr->e_machine, 1516 reld->rel_rtype, 0, &inv_buf), 1517 ifl->ifl_name, reld->rel_isdesc->is_name, 1518 is_name, rep_sdp->sd_file->ifl_name); 1519 } 1520 } 1521 DBG_CALL(Dbg_reloc_sloppycomdat(ofl->ofl_lml, 1522 is_name, rep_sdp)); 1523 return (rep_sdp); 1524 } 1525 } 1526 } 1527 } 1528 /* END CSTYLED */ 1529 1530 /* If didn't return above, we didn't find it */ 1531 return (NULL); 1532 } 1533 1534 1535 /* 1536 * Generate a name for a relocation descriptor that has an STT_SECTION 1537 * symbol associated with it. If it is a regular input section, it will 1538 * look like: 1539 * 1540 * "XXX (section)" 1541 * 1542 * If it is a generated section created to receive the strings from 1543 * input SHF_MERGE|SHF_STRINGS sections, then it will look like: 1544 * 1545 * "XXX (merged string section)" 1546 * 1547 * STT_SECTION relocations to the same section tend to come in clusters, 1548 * so we use a static variable to retain the last string we generate. If 1549 * another one comes along for the same section before some other section 1550 * intervenes, we will reuse the string. 1551 * 1552 * entry: 1553 * sdp - STT_SECTION symbol for which a relocation descriptor name 1554 * should be generated. 1555 * sd_isc - NULL, or input section that should be used instead of 1556 * the input section already assocated with the symbol 1557 * (sdp->sd_isc). This value is set to a non-NULL value when 1558 * a transition from the old input section to a new one is 1559 * being made, but the symbol has not yet been updated. 1560 */ 1561 const const char * 1562 ld_section_reld_name(Sym_desc *sdp, Is_desc *sd_isc) 1563 { 1564 static Is_desc *last_sd_isc = NULL; 1565 static char *namestr; 1566 1567 const char *fmt; 1568 size_t len; 1569 1570 /* 1571 * If caller didn't supply a replacement input section, 1572 * use the one referenced by the symbol. 1573 */ 1574 if (sd_isc == NULL) 1575 sd_isc = sdp->sd_isc; 1576 1577 if ((ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) && 1578 (sd_isc != NULL) && (sd_isc->is_name != NULL)) { 1579 if (last_sd_isc != sd_isc) { 1580 fmt = (sd_isc->is_flags & FLG_IS_GNSTRMRG) ? 1581 MSG_INTL(MSG_STR_SECTION_MSTR) : 1582 MSG_INTL(MSG_STR_SECTION); 1583 len = strlen(fmt) + 1584 strlen(sd_isc->is_name) + 1; 1585 1586 if ((namestr = libld_malloc(len)) == 0) 1587 return (NULL); 1588 (void) snprintf(namestr, len, fmt, 1589 sd_isc->is_name); 1590 last_sd_isc = sd_isc; /* Remember for next time */ 1591 } 1592 return (namestr); 1593 } 1594 1595 return (NULL); 1596 } 1597 1598 1599 /* 1600 * Generate relocation descriptor and dispatch 1601 */ 1602 static uintptr_t 1603 process_reld(Ofl_desc *ofl, Is_desc *isp, Rel_desc *reld, Word rsndx, 1604 Rel *reloc) 1605 { 1606 Ifl_desc *ifl = isp->is_file; 1607 Word rtype = reld->rel_rtype; 1608 Sym_desc *sdp; 1609 Conv_inv_buf_t inv_buf; 1610 1611 /* 1612 * Make sure the relocation is in the valid range. 1613 */ 1614 if (rtype >= ld_targ.t_m.m_r_num) { 1615 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_INVALRELT), 1616 ifl->ifl_name, isp->is_name, rtype); 1617 return (S_ERROR); 1618 } 1619 1620 ofl->ofl_entrelscnt++; 1621 1622 /* 1623 * Special case: a register symbol associated with symbol index 0 is 1624 * initialized (i.e., relocated) to a constant from the r_addend field 1625 * rather than from a symbol value. 1626 */ 1627 if (IS_REGISTER(rtype) && (rsndx == 0)) { 1628 reld->rel_sym = 0; 1629 reld->rel_sname = MSG_ORIG(MSG_STR_EMPTY); 1630 1631 DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD, 1632 ld_targ.t_m.m_mach, isp->is_shdr->sh_type, 1633 (void *)reloc, isp->is_name, reld->rel_sname)); 1634 if (ld_targ.t_mr.mr_reloc_register == NULL) { 1635 eprintf(ofl->ofl_lml, ERR_FATAL, 1636 MSG_INTL(MSG_REL_NOREG)); 1637 return (S_ERROR); 1638 } 1639 return ((*ld_targ.t_mr.mr_reloc_register)(reld, isp, ofl)); 1640 } 1641 1642 /* 1643 * Come up with a descriptive name for the symbol: 1644 * - If it is a named symbol, use the name as is 1645 * - If it is an STT_SECTION symbol, generate a descriptive 1646 * string that incorporates the section name. 1647 * - Otherwise, supply an "unknown" string. 1648 * Note that bogus relocations can result in a null symbol descriptor 1649 * (sdp), the error condition should be caught below after determining 1650 * whether a valid symbol name exists. 1651 */ 1652 sdp = ifl->ifl_oldndx[rsndx]; 1653 if ((sdp != NULL) && sdp->sd_name && *sdp->sd_name) { 1654 reld->rel_sname = sdp->sd_name; 1655 } else if ((sdp != NULL) && 1656 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION) && 1657 (sdp->sd_isc != NULL) && (sdp->sd_isc->is_name != NULL)) { 1658 if ((reld->rel_sname = ld_section_reld_name(sdp, NULL)) == NULL) 1659 return (S_ERROR); 1660 } else { 1661 static char *strunknown; 1662 1663 if (strunknown == 0) 1664 strunknown = (char *)MSG_INTL(MSG_STR_UNKNOWN); 1665 reld->rel_sname = strunknown; 1666 } 1667 1668 /* 1669 * If for some reason we have a null relocation record issue a 1670 * warning and continue (the compiler folks can get into this 1671 * state some time). Normal users should never see this error. 1672 */ 1673 if (rtype == ld_targ.t_m.m_r_none) { 1674 DBG_CALL(Dbg_reloc_in(ofl->ofl_lml, ELF_DBG_LD, 1675 ld_targ.t_m.m_mach, ld_targ.t_m.m_rel_sht_type, 1676 (void *)reloc, isp->is_name, reld->rel_sname)); 1677 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_REL_NULL), 1678 ifl->ifl_name, isp->is_name); 1679 return (1); 1680 } 1681 1682 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && 1683 IS_NOTSUP(rtype)) { 1684 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOTSUP), 1685 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype, 1686 0, &inv_buf), ifl->ifl_name, isp->is_name); 1687 return (S_ERROR); 1688 } 1689 1690 /* 1691 * If we are here, we know that the relocation requires reference 1692 * symbol. If no symbol is assigned, this is a fatal error. 1693 */ 1694 if (sdp == NULL) { 1695 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_NOSYMBOL), 1696 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype, 1697 0, &inv_buf), isp->is_name, ifl->ifl_name, 1698 EC_XWORD(reloc->r_offset)); 1699 return (S_ERROR); 1700 } 1701 1702 if (sdp->sd_flags1 & FLG_SY1_IGNORE) 1703 return (1); 1704 1705 /* 1706 * If this symbol is part of a DISCARDED section attempt to find another 1707 * definition. 1708 */ 1709 if (sdp->sd_flags & FLG_SY_ISDISC) { 1710 Sym_desc *nsdp = NULL; 1711 1712 if (ELF_ST_BIND(sdp->sd_sym->st_info) == STB_LOCAL) { 1713 /* 1714 * If "-z relaxreloc", then check to see if 1715 * this is a reference to a discarded COMDAT 1716 * section that can be replaced with the 1717 * one that was kept. 1718 */ 1719 if (ofl->ofl_flags1 & FLG_OF1_RLXREL) 1720 nsdp = sloppy_comdat_reloc(ofl, reld, sdp); 1721 } else if (reld->rel_sname == sdp->sd_name) { 1722 nsdp = ld_sym_find(sdp->sd_name, SYM_NOHASH, 0, ofl); 1723 } 1724 if (nsdp == 0) { 1725 eprintf(ofl->ofl_lml, ERR_FATAL, 1726 MSG_INTL(MSG_REL_SYMDISC), 1727 ifl->ifl_name, isp->is_name, 1728 demangle(sdp->sd_name), sdp->sd_isc->is_name); 1729 return (S_ERROR); 1730 } 1731 ifl->ifl_oldndx[rsndx] = sdp = nsdp; 1732 } 1733 1734 /* 1735 * If this is a global symbol, determine whether its visibility needs 1736 * adjusting. 1737 */ 1738 if (sdp->sd_aux && ((sdp->sd_flags & FLG_SY_VISIBLE) == 0)) 1739 ld_sym_adjust_vis(sdp, ofl); 1740 1741 /* 1742 * Ignore any relocation against a section that will not be in the 1743 * output file (has been stripped). 1744 */ 1745 if ((sdp->sd_isc == 0) && 1746 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) 1747 return (1); 1748 1749 /* 1750 * If the input section exists, but the section has not been associated 1751 * to an output section, then this is a little suspicious. 1752 */ 1753 if (sdp->sd_isc && (sdp->sd_isc->is_osdesc == 0) && 1754 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 1755 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_RELINVSEC), 1756 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype, 1757 0, &inv_buf), ifl->ifl_name, isp->is_name, 1758 sdp->sd_isc->is_name); 1759 return (1); 1760 } 1761 1762 /* 1763 * If the symbol for this relocation is invalid (which should have 1764 * generated a message during symbol processing), or the relocation 1765 * record's symbol reference is in any other way invalid, then it's 1766 * about time we gave up. 1767 */ 1768 if ((sdp->sd_flags & FLG_SY_INVALID) || (rsndx == 0) || 1769 (rsndx >= ifl->ifl_symscnt)) { 1770 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_UNKNWSYM), 1771 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype, 1772 0, &inv_buf), ifl->ifl_name, isp->is_name, 1773 demangle(reld->rel_sname), EC_XWORD(reloc->r_offset), 1774 EC_WORD(rsndx)); 1775 return (S_ERROR); 1776 } 1777 1778 /* 1779 * Size relocations against section symbols are presently unsupported. 1780 * There is a question as to whether the input section size, or output 1781 * section size would be used. Until an explicit requirement is 1782 * established for either case, we'll punt. 1783 */ 1784 if (IS_SIZE(rtype) && 1785 (ELF_ST_TYPE(sdp->sd_sym->st_info) == STT_SECTION)) { 1786 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_UNSUPSIZE), 1787 conv_reloc_type(ifl->ifl_ehdr->e_machine, rtype, 1788 0, &inv_buf), ifl->ifl_name, isp->is_name); 1789 return (S_ERROR); 1790 } 1791 1792 reld->rel_sym = sdp; 1793 return (ld_process_sym_reloc(ofl, reld, reloc, isp, isp->is_name)); 1794 } 1795 1796 static uintptr_t 1797 reloc_section(Ofl_desc *ofl, Is_desc *isect, Is_desc *rsect, Os_desc *osect) 1798 { 1799 Rel *rend; /* end of relocation section data */ 1800 Rel *reloc; /* current relocation entry */ 1801 Xword rsize; /* size of relocation section data */ 1802 Xword entsize; /* size of relocation entry */ 1803 Rel_desc reld; /* relocation descriptor */ 1804 Shdr * shdr; 1805 Word flags = 0; 1806 uintptr_t ret = 1; 1807 1808 shdr = rsect->is_shdr; 1809 rsize = shdr->sh_size; 1810 reloc = (Rel *)rsect->is_indata->d_buf; 1811 1812 /* 1813 * Decide entry size. 1814 */ 1815 if (((entsize = shdr->sh_entsize) == 0) || (entsize > rsize)) { 1816 if (shdr->sh_type == SHT_RELA) 1817 entsize = sizeof (Rela); 1818 else 1819 entsize = sizeof (Rel); 1820 } 1821 1822 /* 1823 * Build up the basic information in for the Rel_desc structure. 1824 */ 1825 reld.rel_osdesc = osect; 1826 reld.rel_isdesc = isect; 1827 reld.rel_move = 0; 1828 1829 if ((ofl->ofl_flags & FLG_OF_RELOBJ) || 1830 (osect && (osect->os_sgdesc->sg_phdr.p_type == PT_LOAD))) 1831 flags |= FLG_REL_LOAD; 1832 1833 if (shdr->sh_info == 0) 1834 flags |= FLG_REL_NOINFO; 1835 1836 DBG_CALL(Dbg_reloc_proc(ofl->ofl_lml, osect, isect, rsect)); 1837 1838 for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize); 1839 reloc < rend; 1840 reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) { 1841 Word rsndx; 1842 1843 /* 1844 * Initialize the relocation record information and process 1845 * the individual relocation. Reinitialize the flags to 1846 * insure we don't carry any state over from the previous 1847 * relocation records processing. 1848 */ 1849 reld.rel_flags = flags; 1850 rsndx = (*ld_targ.t_mr.mr_init_rel)(&reld, (void *)reloc); 1851 1852 if (process_reld(ofl, rsect, &reld, rsndx, reloc) == S_ERROR) 1853 ret = S_ERROR; 1854 } 1855 return (ret); 1856 } 1857 1858 static uintptr_t 1859 reloc_segments(int wr_flag, Ofl_desc *ofl) 1860 { 1861 Listnode *lnp1; 1862 Sg_desc *sgp; 1863 Is_desc *isp; 1864 1865 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) { 1866 Os_desc *osp; 1867 Aliste idx; 1868 1869 if ((sgp->sg_phdr.p_flags & PF_W) != wr_flag) 1870 continue; 1871 1872 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp)) { 1873 Is_desc *risp; 1874 Listnode *lnp3; 1875 1876 osp->os_szoutrels = 0; 1877 for (LIST_TRAVERSE(&(osp->os_relisdescs), lnp3, risp)) { 1878 Word indx; 1879 1880 /* 1881 * Determine the input section that this 1882 * relocation information refers to. 1883 */ 1884 indx = risp->is_shdr->sh_info; 1885 isp = risp->is_file->ifl_isdesc[indx]; 1886 1887 /* 1888 * Do not process relocations against sections 1889 * which are being discarded (COMDAT) 1890 */ 1891 if (isp->is_flags & FLG_IS_DISCARD) 1892 continue; 1893 1894 if (reloc_section(ofl, isp, risp, osp) == 1895 S_ERROR) 1896 return (S_ERROR); 1897 } 1898 1899 /* 1900 * Check for relocations against non-writable 1901 * allocatable sections. 1902 */ 1903 if ((osp->os_szoutrels) && 1904 (sgp->sg_phdr.p_type == PT_LOAD) && 1905 ((sgp->sg_phdr.p_flags & PF_W) == 0)) { 1906 ofl->ofl_flags |= FLG_OF_TEXTREL; 1907 ofl->ofl_dtflags |= DF_TEXTREL; 1908 } 1909 } 1910 } 1911 1912 return (1); 1913 } 1914 1915 /* 1916 * Move Section related function 1917 * Get move entry 1918 */ 1919 static Move * 1920 get_move_entry(Is_desc *rsect, Xword roffset) 1921 { 1922 Ifl_desc *ifile = rsect->is_file; 1923 Shdr *rshdr = rsect->is_shdr; 1924 Is_desc *misp; 1925 Shdr *mshdr; 1926 Xword midx; 1927 Move *ret; 1928 1929 /* 1930 * Set info for the target move section 1931 */ 1932 misp = ifile->ifl_isdesc[rshdr->sh_info]; 1933 mshdr = (ifile->ifl_isdesc[rshdr->sh_info])->is_shdr; 1934 1935 if (mshdr->sh_entsize == 0) 1936 return ((Move *)0); 1937 midx = roffset / mshdr->sh_entsize; 1938 1939 ret = (Move *)misp->is_indata->d_buf; 1940 ret += midx; 1941 1942 /* 1943 * If this is an illgal entry, retun NULL. 1944 */ 1945 if ((midx * mshdr->sh_entsize) >= mshdr->sh_size) 1946 return ((Move *)0); 1947 return (ret); 1948 } 1949 1950 /* 1951 * Relocation against Move Table. 1952 */ 1953 static uintptr_t 1954 process_movereloc(Ofl_desc *ofl, Is_desc *rsect) 1955 { 1956 Ifl_desc *file = rsect->is_file; 1957 Rel *rend, *reloc; 1958 Xword rsize, entsize; 1959 static Rel_desc reld_zero; 1960 Rel_desc reld; 1961 1962 rsize = rsect->is_shdr->sh_size; 1963 reloc = (Rel *)rsect->is_indata->d_buf; 1964 1965 reld = reld_zero; 1966 1967 /* 1968 * Decide entry size 1969 */ 1970 entsize = rsect->is_shdr->sh_entsize; 1971 if ((entsize == 0) || 1972 (entsize > rsect->is_shdr->sh_size)) { 1973 if (rsect->is_shdr->sh_type == SHT_RELA) 1974 entsize = sizeof (Rela); 1975 else 1976 entsize = sizeof (Rel); 1977 } 1978 1979 /* 1980 * Go through the relocation entries. 1981 */ 1982 for (rend = (Rel *)((uintptr_t)reloc + (uintptr_t)rsize); 1983 reloc < rend; 1984 reloc = (Rel *)((uintptr_t)reloc + (uintptr_t)entsize)) { 1985 Sym_desc * psdp; 1986 Move * mp; 1987 Word rsndx; 1988 1989 /* 1990 * Initialize the relocation record information. 1991 */ 1992 reld.rel_flags = FLG_REL_LOAD; 1993 rsndx = (*ld_targ.t_mr.mr_init_rel)(&reld, (void *)reloc); 1994 1995 if (((mp = get_move_entry(rsect, reloc->r_offset)) == 0) || 1996 ((reld.rel_move = libld_malloc(sizeof (Mv_desc))) == 0)) 1997 return (S_ERROR); 1998 1999 psdp = file->ifl_oldndx[ELF_M_SYM(mp->m_info)]; 2000 reld.rel_move->mvd_move = mp; 2001 reld.rel_move->mvd_sym = psdp; 2002 2003 if (psdp->sd_flags & FLG_SY_PAREXPN) { 2004 int _num, num; 2005 2006 reld.rel_osdesc = ofl->ofl_issunwdata1->is_osdesc; 2007 reld.rel_isdesc = ofl->ofl_issunwdata1; 2008 reld.rel_roffset = mp->m_poffset; 2009 2010 for (num = mp->m_repeat, _num = 0; _num < num; _num++) { 2011 reld.rel_roffset += 2012 /* LINTED */ 2013 (_num * ELF_M_SIZE(mp->m_info)); 2014 2015 /* 2016 * Generate Reld 2017 */ 2018 if (process_reld(ofl, 2019 rsect, &reld, rsndx, reloc) == S_ERROR) 2020 return (S_ERROR); 2021 } 2022 } else { 2023 /* 2024 * Generate Reld 2025 */ 2026 reld.rel_flags |= FLG_REL_MOVETAB; 2027 reld.rel_osdesc = ofl->ofl_osmove; 2028 reld.rel_isdesc = 2029 ofl->ofl_osmove->os_isdescs.head->data; 2030 2031 if (process_reld(ofl, 2032 rsect, &reld, rsndx, reloc) == S_ERROR) 2033 return (S_ERROR); 2034 } 2035 } 2036 return (1); 2037 } 2038 2039 /* 2040 * This function is similar to reloc_init(). 2041 * 2042 * This function is called when the SHT_SUNW_move table is expanded 2043 * and there were relocation against the SHT_SUNW_move section. 2044 */ 2045 static uintptr_t 2046 reloc_movesections(Ofl_desc *ofl) 2047 { 2048 Listnode *lnp1; 2049 Is_desc *risp; 2050 uintptr_t ret = 1; 2051 2052 /* 2053 * Generate/Expand relocation entries 2054 */ 2055 for (LIST_TRAVERSE(&ofl->ofl_mvrelisdescs, lnp1, risp)) { 2056 if (process_movereloc(ofl, risp) == S_ERROR) 2057 ret = S_ERROR; 2058 } 2059 2060 return (ret); 2061 } 2062 2063 /* 2064 * Count the number of output relocation entries, global offset table entries, 2065 * and procedure linkage table entries. This function searches the segment and 2066 * outsect lists and passes each input reloc section to process_reloc(). 2067 * It allocates space for any output relocations needed. And builds up 2068 * the relocation structures for later processing. 2069 */ 2070 uintptr_t 2071 ld_reloc_init(Ofl_desc *ofl) 2072 { 2073 Listnode *lnp; 2074 Is_desc *isp; 2075 Sym_desc *sdp; 2076 2077 /* 2078 * At this point we have finished processing all input symbols. Make 2079 * sure we add any absolute (internal) symbols before continuing with 2080 * any relocation processing. 2081 */ 2082 if (ld_sym_spec(ofl) == S_ERROR) 2083 return (S_ERROR); 2084 2085 ofl->ofl_gotcnt = ld_targ.t_m.m_got_xnumber; 2086 2087 /* 2088 * First process all of the relocations against NON-writable 2089 * segments followed by relocations against the writeable segments. 2090 * 2091 * This separation is so that when the writable segments are processed 2092 * we know whether or not a COPYRELOC will be produced for any symbols. 2093 * If relocations aren't processed in this order, a COPYRELOC and a 2094 * regular relocation can be produced against the same symbol. The 2095 * regular relocation would be redundant. 2096 */ 2097 if (reloc_segments(0, ofl) == S_ERROR) 2098 return (S_ERROR); 2099 2100 if (reloc_segments(PF_W, ofl) == S_ERROR) 2101 return (S_ERROR); 2102 2103 /* 2104 * Process any extra relocations. These are relocation sections that 2105 * have a NULL sh_info. 2106 */ 2107 for (LIST_TRAVERSE(&ofl->ofl_extrarels, lnp, isp)) { 2108 if (reloc_section(ofl, NULL, isp, NULL) == S_ERROR) 2109 return (S_ERROR); 2110 } 2111 2112 /* 2113 * If there were relocation against move table, 2114 * process the relocation sections. 2115 */ 2116 if (reloc_movesections(ofl) == S_ERROR) 2117 return (S_ERROR); 2118 2119 /* 2120 * Now all the relocations are pre-processed, 2121 * check the validity of copy relocations. 2122 */ 2123 if (ofl->ofl_copyrels.head != 0) { 2124 Copy_rel *cpyrel; 2125 2126 for (LIST_TRAVERSE(&ofl->ofl_copyrels, lnp, cpyrel)) { 2127 sdp = cpyrel->copyrel_symd; 2128 2129 /* 2130 * If there were no displacement relocation 2131 * in this file, don't worry about it. 2132 */ 2133 if (sdp->sd_file->ifl_flags & 2134 (FLG_IF_DISPPEND | FLG_IF_DISPDONE)) 2135 is_disp_copied(ofl, cpyrel); 2136 } 2137 } 2138 2139 /* 2140 * GOT sections are created for dynamic executables and shared objects 2141 * if the FLG_OF_BLDGOT is set, or explicit reference has been made to 2142 * a GOT symbol. 2143 */ 2144 if (((ofl->ofl_flags & FLG_OF_RELOBJ) == 0) && 2145 ((ofl->ofl_flags & FLG_OF_BLDGOT) || 2146 ((((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL), 2147 SYM_NOHASH, 0, ofl)) != 0) || 2148 ((sdp = ld_sym_find(MSG_ORIG(MSG_SYM_GOFTBL_U), 2149 SYM_NOHASH, 0, ofl)) != 0)) && (sdp->sd_ref != REF_DYN_SEEN)))) { 2150 if (ld_make_got(ofl) == S_ERROR) 2151 return (S_ERROR); 2152 2153 /* Allocate the GOT if required by target */ 2154 if ((ld_targ.t_mr.mr_allocate_got != NULL) && 2155 ((*ld_targ.t_mr.mr_allocate_got)(ofl) == S_ERROR)) 2156 return (S_ERROR); 2157 } 2158 2159 return (1); 2160 } 2161 2162 /* 2163 * Simple comparison routine to be used by qsort() for 2164 * the sorting of the output relocation list. 2165 * 2166 * The reloc_compare() routine results in a relocation 2167 * table which is located on: 2168 * 2169 * file referenced (NEEDED NDX) 2170 * referenced symbol 2171 * relocation offset 2172 * 2173 * This provides the most efficient traversal of the relocation 2174 * table at run-time. 2175 */ 2176 static int 2177 reloc_compare(Reloc_list *i, Reloc_list *j) 2178 { 2179 2180 /* 2181 * first - sort on neededndx 2182 */ 2183 if (i->rl_key1 > j->rl_key1) 2184 return (1); 2185 if (i->rl_key1 < j->rl_key1) 2186 return (-1); 2187 2188 /* 2189 * Then sort on symbol 2190 */ 2191 if ((uintptr_t)i->rl_key2 > (uintptr_t)j->rl_key2) 2192 return (1); 2193 if ((uintptr_t)i->rl_key2 < (uintptr_t)j->rl_key2) 2194 return (-1); 2195 2196 /* 2197 * i->key2 == j->key2 2198 * 2199 * At this point we fall back to key2 (offsets) to 2200 * sort the output relocations. Ideally this will 2201 * make for the most efficient processing of these 2202 * relocations at run-time. 2203 */ 2204 if (i->rl_key3 > j->rl_key3) 2205 return (1); 2206 if (i->rl_key3 < j->rl_key3) 2207 return (-1); 2208 return (0); 2209 } 2210 2211 static uintptr_t 2212 do_sorted_outrelocs(Ofl_desc *ofl) 2213 { 2214 Rel_desc *orsp; 2215 Rel_cache *rcp; 2216 Listnode *lnp; 2217 Reloc_list *sorted_list; 2218 Word index = 0; 2219 int debug = 0; 2220 uintptr_t error = 1; 2221 2222 if ((sorted_list = libld_malloc((size_t)(sizeof (Reloc_list) * 2223 ofl->ofl_reloccnt))) == NULL) 2224 return (S_ERROR); 2225 2226 /* 2227 * All but the PLT output relocations are sorted in the output file 2228 * based upon their sym_desc. By doing this multiple relocations 2229 * against the same symbol are grouped together, thus when the object 2230 * is later relocated by ld.so.1 it will take advantage of the symbol 2231 * cache that ld.so.1 has. This can significantly reduce the runtime 2232 * relocation cost of a dynamic object. 2233 * 2234 * PLT relocations are not sorted because the order of the PLT 2235 * relocations is used by ld.so.1 to determine what symbol a PLT 2236 * relocation is against. 2237 */ 2238 for (LIST_TRAVERSE(&ofl->ofl_outrels, lnp, rcp)) { 2239 /*LINTED*/ 2240 for (orsp = (Rel_desc *)(rcp + 1); 2241 orsp < rcp->rc_free; orsp++) { 2242 if (debug == 0) { 2243 DBG_CALL(Dbg_reloc_dooutrel(ofl->ofl_lml, 2244 ld_targ.t_m.m_rel_sht_type)); 2245 debug = 1; 2246 } 2247 2248 /* 2249 * If it's a PLT relocation we output it now in the 2250 * order that it was originally processed. 2251 */ 2252 if (orsp->rel_flags & FLG_REL_PLT) { 2253 if ((*ld_targ.t_mr.mr_perform_outreloc)(orsp, 2254 ofl) == S_ERROR) 2255 error = S_ERROR; 2256 continue; 2257 } 2258 2259 if ((orsp->rel_rtype == ld_targ.t_m.m_r_relative) || 2260 (orsp->rel_rtype == ld_targ.t_m.m_r_register)) { 2261 sorted_list[index].rl_key1 = 0; 2262 sorted_list[index].rl_key2 = 2263 /* LINTED */ 2264 (Sym_desc *)(uintptr_t)orsp->rel_rtype; 2265 } else { 2266 sorted_list[index].rl_key1 = 2267 orsp->rel_sym->sd_file->ifl_neededndx; 2268 sorted_list[index].rl_key2 = 2269 orsp->rel_sym; 2270 } 2271 2272 if (orsp->rel_flags & FLG_REL_GOT) 2273 sorted_list[index].rl_key3 = 2274 (*ld_targ.t_mr.mr_calc_got_offset)(orsp, 2275 ofl); 2276 else { 2277 if (orsp->rel_rtype == ld_targ.t_m.m_r_register) 2278 sorted_list[index].rl_key3 = 0; 2279 else { 2280 sorted_list[index].rl_key3 = 2281 orsp->rel_roffset + 2282 (Xword)_elf_getxoff(orsp-> 2283 rel_isdesc->is_indata) + 2284 orsp->rel_isdesc->is_osdesc-> 2285 os_shdr->sh_addr; 2286 } 2287 } 2288 2289 sorted_list[index++].rl_rsp = orsp; 2290 } 2291 } 2292 2293 qsort(sorted_list, (size_t)ofl->ofl_reloccnt, sizeof (Reloc_list), 2294 (int (*)(const void *, const void *))reloc_compare); 2295 2296 /* 2297 * All output relocations have now been sorted, go through 2298 * and process each relocation. 2299 */ 2300 for (index = 0; index < ofl->ofl_reloccnt; index++) { 2301 if ((*ld_targ.t_mr.mr_perform_outreloc) 2302 (sorted_list[index].rl_rsp, ofl) == S_ERROR) 2303 error = S_ERROR; 2304 } 2305 2306 return (error); 2307 } 2308 2309 /* 2310 * Process relocations. Finds every input relocation section for each output 2311 * section and invokes reloc_section() to relocate that section. 2312 */ 2313 uintptr_t 2314 ld_reloc_process(Ofl_desc *ofl) 2315 { 2316 Listnode *lnp1; 2317 Sg_desc *sgp; 2318 Os_desc *osp; 2319 Word ndx = 0; 2320 ofl_flag_t flags = ofl->ofl_flags; 2321 Shdr *shdr; 2322 2323 /* 2324 * Determine the index of the symbol table that will be referenced by 2325 * the relocation entries. 2326 */ 2327 if ((flags & (FLG_OF_DYNAMIC|FLG_OF_RELOBJ)) == FLG_OF_DYNAMIC) 2328 /* LINTED */ 2329 ndx = (Word)elf_ndxscn(ofl->ofl_osdynsym->os_scn); 2330 else if (!(flags & FLG_OF_STRIP) || (flags & FLG_OF_RELOBJ)) 2331 /* LINTED */ 2332 ndx = (Word)elf_ndxscn(ofl->ofl_ossymtab->os_scn); 2333 2334 /* 2335 * Re-initialize counters. These are used to provide relocation 2336 * offsets within the output buffers. 2337 */ 2338 ofl->ofl_relocpltsz = 0; 2339 ofl->ofl_relocgotsz = 0; 2340 ofl->ofl_relocbsssz = 0; 2341 2342 /* 2343 * Now that the output file is created and symbol update has occurred, 2344 * process the relocations collected in process_reloc(). 2345 */ 2346 if (do_sorted_outrelocs(ofl) == S_ERROR) 2347 return (S_ERROR); 2348 2349 if ((*ld_targ.t_mr.mr_do_activerelocs)(ofl) == S_ERROR) 2350 return (S_ERROR); 2351 2352 if ((flags & FLG_OF_COMREL) == 0) { 2353 /* 2354 * Process the relocation sections: 2355 * 2356 * o for each relocation section generated for the output 2357 * image update its shdr information to reflect the 2358 * symbol table it needs (sh_link) and the section to 2359 * which the relocation must be applied (sh_info). 2360 */ 2361 for (LIST_TRAVERSE(&ofl->ofl_segs, lnp1, sgp)) { 2362 Os_desc *osp; 2363 Aliste idx; 2364 2365 for (APLIST_TRAVERSE(sgp->sg_osdescs, idx, osp)) { 2366 if (osp->os_relosdesc == 0) 2367 continue; 2368 2369 shdr = osp->os_relosdesc->os_shdr; 2370 shdr->sh_link = ndx; 2371 /* LINTED */ 2372 shdr->sh_info = (Word)elf_ndxscn(osp->os_scn); 2373 } 2374 } 2375 2376 /* 2377 * Since the .rel[a] section is not tied to any specific 2378 * section, we'd not have found it above. 2379 */ 2380 if ((osp = ofl->ofl_osrel) != NULL) { 2381 shdr = osp->os_shdr; 2382 shdr->sh_link = ndx; 2383 shdr->sh_info = 0; 2384 } 2385 } else { 2386 /* 2387 * We only have two relocation sections here, (PLT's, 2388 * coalesced) so just hit them directly instead of stepping 2389 * over the output sections. 2390 */ 2391 if ((osp = ofl->ofl_osrelhead) != NULL) { 2392 shdr = osp->os_shdr; 2393 shdr->sh_link = ndx; 2394 shdr->sh_info = 0; 2395 } 2396 if (((osp = ofl->ofl_osplt) != NULL) && osp->os_relosdesc) { 2397 shdr = osp->os_relosdesc->os_shdr; 2398 shdr->sh_link = ndx; 2399 /* LINTED */ 2400 shdr->sh_info = (Word)elf_ndxscn(osp->os_scn); 2401 } 2402 } 2403 2404 /* 2405 * If the -z text option was given, and we have output relocations 2406 * against a non-writable, allocatable section, issue a diagnostic and 2407 * return (the actual entries that caused this error would have been 2408 * output during the relocating section phase). 2409 */ 2410 if ((flags & (FLG_OF_PURETXT | FLG_OF_TEXTREL)) == 2411 (FLG_OF_PURETXT | FLG_OF_TEXTREL)) { 2412 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_REL_REMAIN_3)); 2413 return (S_ERROR); 2414 } 2415 2416 /* 2417 * Finally, initialize the first got entry with the address of the 2418 * .dynamic section (_DYNAMIC). 2419 */ 2420 if (flags & FLG_OF_DYNAMIC) { 2421 if ((*ld_targ.t_mr.mr_fillin_gotplt)(ofl) == S_ERROR) 2422 return (S_ERROR); 2423 } 2424 2425 /* 2426 * Now that any GOT information has been written, display the debugging 2427 * information if required. 2428 */ 2429 if ((osp = ofl->ofl_osgot) != NULL) 2430 DBG_CALL(Dbg_got_display(ofl, osp->os_shdr->sh_addr, 1, 2431 ld_targ.t_m.m_got_xnumber, ld_targ.t_m.m_got_entsize)); 2432 2433 return (1); 2434 } 2435 2436 /* 2437 * If the -z text option was given, and we have output relocations against a 2438 * non-writable, allocatable section, issue a diagnostic. Print offending 2439 * symbols in tabular form similar to the way undefined symbols are presented. 2440 * Called from reloc_count(). The actual fatal error condition is triggered on 2441 * in reloc_process() above. 2442 * 2443 * Note. For historic reasons -ztext is not a default option (however all OS 2444 * shared object builds use this option). It can be argued that this option 2445 * should also be default when generating an a.out (see 1163979). However, if 2446 * an a.out contains text relocations it is either because the user is creating 2447 * something pretty weird (they've used the -b or -znodefs options), or because 2448 * the library against which they're building wasn't constructed correctly (ie. 2449 * a function has a NOTYPE type, in which case the a.out won't generate an 2450 * associated plt). In the latter case the builder of the a.out can't do 2451 * anything to fix the error - thus we've chosen not to give the user an error, 2452 * or warning, for this case. 2453 */ 2454 static void 2455 reloc_remain_title(Ofl_desc *ofl, int warning) 2456 { 2457 const char *str1; 2458 2459 if (warning) 2460 str1 = MSG_INTL(MSG_REL_RMN_ITM_13); 2461 else 2462 str1 = MSG_INTL(MSG_REL_RMN_ITM_11); 2463 2464 eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_REL_REMAIN_FMT_1), str1, 2465 MSG_INTL(MSG_REL_RMN_ITM_31), MSG_INTL(MSG_REL_RMN_ITM_12), 2466 MSG_INTL(MSG_REL_RMN_ITM_2), MSG_INTL(MSG_REL_RMN_ITM_32)); 2467 } 2468 2469 void 2470 ld_reloc_remain_entry(Rel_desc *orsp, Os_desc *osp, Ofl_desc *ofl) 2471 { 2472 static Boolean reloc_title = TRUE; 2473 2474 /* 2475 * -ztextoff 2476 */ 2477 if (ofl->ofl_flags1 & FLG_OF1_TEXTOFF) 2478 return; 2479 2480 /* 2481 * Only give relocation errors against loadable read-only segments. 2482 */ 2483 if ((orsp->rel_rtype == ld_targ.t_m.m_r_register) || (!osp) || 2484 (osp->os_sgdesc->sg_phdr.p_type != PT_LOAD) || 2485 (osp->os_sgdesc->sg_phdr.p_flags & PF_W)) 2486 return; 2487 2488 /* 2489 * If we are in -ztextwarn mode, it's a silent error if a relocation is 2490 * due to a 'WEAK REFERENCE'. This is because if the symbol is not 2491 * provided at run-time we will not perform a text-relocation. 2492 */ 2493 if (((ofl->ofl_flags & FLG_OF_PURETXT) == 0) && 2494 (ELF_ST_BIND(orsp->rel_sym->sd_sym->st_info) == STB_WEAK) && 2495 (orsp->rel_sym->sd_sym->st_shndx == SHN_UNDEF)) 2496 return; 2497 2498 if (reloc_title) { 2499 /* 2500 * If building with '-ztext' then emit a fatal error. If 2501 * building a executable then only emit a 'warning'. 2502 */ 2503 if (ofl->ofl_flags & FLG_OF_PURETXT) 2504 reloc_remain_title(ofl, 0); 2505 else 2506 reloc_remain_title(ofl, 1); 2507 reloc_title = FALSE; 2508 } 2509 2510 eprintf(ofl->ofl_lml, ERR_NONE, MSG_INTL(MSG_REL_REMAIN_2), 2511 demangle(orsp->rel_sname), EC_OFF(orsp->rel_roffset), 2512 orsp->rel_isdesc->is_file->ifl_name); 2513 } 2514 2515 /* 2516 * Generic encapsulation for generating a TLS got index. 2517 */ 2518 uintptr_t 2519 ld_assign_got_TLS(Boolean local, Rel_desc *rsp, Ofl_desc *ofl, Sym_desc *sdp, 2520 Gotndx *gnp, Gotref gref, Word rflag, Word ortype, Word rtype1, Word rtype2) 2521 { 2522 Word rflags; 2523 2524 if ((*ld_targ.t_mr.mr_assign_got_ndx)(&(sdp->sd_GOTndxs), gnp, 2525 gref, ofl, rsp, sdp) == S_ERROR) 2526 return (S_ERROR); 2527 2528 rflags = FLG_REL_GOT | rflag; 2529 if (local) 2530 rflags |= FLG_REL_SCNNDX; 2531 rsp->rel_rtype = rtype1; 2532 2533 if ((*ld_targ.t_mr.mr_add_outrel)(rflags, rsp, ofl) == S_ERROR) 2534 return (S_ERROR); 2535 2536 if (local && (gref == GOT_REF_TLSIE)) { 2537 /* 2538 * If this is a local LE TLS symbol, then the symbol won't be 2539 * available at runtime. The value of the local symbol will 2540 * be placed in the associated got entry, and the got 2541 * relocation is reassigned to a section symbol. 2542 */ 2543 if (ld_add_actrel(rflags, rsp, ofl) == S_ERROR) 2544 return (S_ERROR); 2545 } 2546 2547 if (rtype2) { 2548 rflags = FLG_REL_GOT | rflag; 2549 rsp->rel_rtype = rtype2; 2550 2551 if (local) { 2552 if (ld_add_actrel(rflags, rsp, ofl) == S_ERROR) 2553 return (S_ERROR); 2554 } else { 2555 if ((*ld_targ.t_mr.mr_add_outrel)(rflags, rsp, ofl) == 2556 S_ERROR) 2557 return (S_ERROR); 2558 } 2559 } 2560 2561 rsp->rel_rtype = ortype; 2562 2563 return (1); 2564 } 2565 2566 /* 2567 * Move Section related function 2568 */ 2569 static uintptr_t 2570 newroffset_for_move(Sym_desc *symd, 2571 Move *mventry, Xword offset1, Xword *offset2) 2572 { 2573 Psym_info *psym = symd->sd_psyminfo; 2574 Mv_itm *itm; 2575 Listnode *lnp1; 2576 int found = 0; 2577 2578 /* 2579 * Search for matching move entry 2580 */ 2581 found = 0; 2582 for (LIST_TRAVERSE(&psym->psym_mvs, lnp1, itm)) { 2583 if (itm->mv_ientry == mventry) { 2584 found = 1; 2585 break; 2586 } 2587 } 2588 if (found == 0) { 2589 /* 2590 * This should never happen. 2591 */ 2592 return (S_ERROR); 2593 } 2594 2595 /* 2596 * Update r_offset 2597 */ 2598 *offset2 = (Xword)((itm->mv_oidx - 1)*sizeof (Move) + 2599 offset1 % sizeof (Move)); 2600 return (1); 2601 } 2602 2603 void 2604 ld_adj_movereloc(Ofl_desc *ofl, Rel_desc *arsp) 2605 { 2606 Move *move = arsp->rel_move->mvd_move; 2607 Sym_desc *psdp = arsp->rel_move->mvd_sym; 2608 Xword newoffset; 2609 2610 if (arsp->rel_flags & FLG_REL_MOVETAB) { 2611 /* 2612 * We are relocating the move table itself. 2613 */ 2614 (void) newroffset_for_move(psdp, move, arsp->rel_roffset, 2615 &newoffset); 2616 DBG_CALL(Dbg_move_adjmovereloc(ofl->ofl_lml, arsp->rel_roffset, 2617 newoffset, psdp->sd_name)); 2618 arsp->rel_roffset = newoffset; 2619 } else { 2620 /* 2621 * We are expanding the partial symbol. So we are generating 2622 * the relocation entry relocating the expanded partial symbol. 2623 */ 2624 arsp->rel_roffset += psdp->sd_sym->st_value - 2625 ofl->ofl_issunwdata1->is_osdesc->os_shdr->sh_addr; 2626 DBG_CALL(Dbg_move_adjexpandreloc(ofl->ofl_lml, 2627 arsp->rel_roffset, psdp->sd_name)); 2628 } 2629 } 2630 2631 /* 2632 * Partially Initialized Symbol Handling routines 2633 * For sparc architecture, the second argument is reld->rel_raddend. 2634 * For i386 acrchitecure, the second argument is the value stored 2635 * at the relocation target address. 2636 */ 2637 Sym_desc * 2638 ld_am_I_partial(Rel_desc *reld, Xword val) 2639 { 2640 Ifl_desc * ifile = reld->rel_sym->sd_isc->is_file; 2641 int nlocs = ifile->ifl_locscnt, i; 2642 2643 for (i = 1; i < nlocs; i++) { 2644 Sym * osym; 2645 Sym_desc * symd = ifile->ifl_oldndx[i]; 2646 2647 if ((osym = symd->sd_osym) == 0) 2648 continue; 2649 if ((symd->sd_flags & FLG_SY_PAREXPN) == 0) 2650 continue; 2651 if ((osym->st_value <= val) && 2652 (osym->st_value + osym->st_size > val)) 2653 return (symd); 2654 } 2655 return ((Sym_desc *) 0); 2656 } 2657 2658 2659 2660 /* 2661 * Return True (1) if the code processing the given relocation 2662 * needs to perform byte swapping when accessing the section data. 2663 */ 2664 int 2665 ld_swap_reloc_data(Ofl_desc *ofl, Rel_desc *rsp) 2666 { 2667 /* 2668 * In a cross-link situation where the linker host and target 2669 * have opposite byte orders, it can be necessary to swap bytes 2670 * when doing relocation processing. This is indicated by the 2671 * presence of the FLG_OF1_ENCDIFF flag bit. However, swapping 2672 * is only needed for the section types that libelf doesn't 2673 * automatically xlate. 2674 */ 2675 if ((ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0) { 2676 switch (rsp->rel_osdesc->os_shdr->sh_type) { 2677 case SHT_PROGBITS: 2678 return (1); 2679 2680 case SHT_SPARC_GOTDATA: 2681 if (ld_targ.t_m.m_mach == 2682 LD_TARG_BYCLASS(EM_SPARC, EM_SPARCV9)) 2683 return (1); 2684 break; 2685 2686 case SHT_AMD64_UNWIND: 2687 if (ld_targ.t_m.m_mach == EM_AMD64) 2688 return (1); 2689 break; 2690 } 2691 } 2692 2693 /* 2694 * If FLG_OF1_ENCDIFF isn't set, or the section isn't 2695 * progbits (or similar), then no swapping is needed. 2696 */ 2697 return (0); 2698 } 2699 2700 2701 2702 /* 2703 * Obtain the current value at the given relocation target. 2704 * 2705 * entry: 2706 * ofl - Output file descriptor 2707 * rsp - Relocation record 2708 * data - Pointer to relocation target 2709 * value - Address of variable to recieve value 2710 * 2711 * exit: 2712 * The value of the data at the relocation target has 2713 * been stored in value. 2714 */ 2715 int 2716 ld_reloc_targval_get(Ofl_desc *ofl, Rel_desc *rsp, uchar_t *data, Xword *value) 2717 { 2718 const Rel_entry *rep; 2719 2720 rep = &ld_targ.t_mr.mr_reloc_table[rsp->rel_rtype]; 2721 2722 switch (rep->re_fsize) { 2723 case 1: 2724 /* LINTED */ 2725 *value = (Xword) *((uchar_t *)data); 2726 break; 2727 case 2: 2728 { 2729 Half v; 2730 uchar_t *v_bytes = (uchar_t *)&v; 2731 2732 if (OFL_SWAP_RELOC_DATA(ofl, rsp)) { 2733 UL_ASSIGN_BSWAP_HALF(v_bytes, data); 2734 } else { 2735 UL_ASSIGN_HALF(v_bytes, data); 2736 } 2737 *value = (Xword) v; 2738 } 2739 break; 2740 case 4: 2741 { 2742 Word v; 2743 uchar_t *v_bytes = (uchar_t *)&v; 2744 2745 if (OFL_SWAP_RELOC_DATA(ofl, rsp)) { 2746 UL_ASSIGN_BSWAP_WORD(v_bytes, data); 2747 } else { 2748 UL_ASSIGN_WORD(v_bytes, data); 2749 } 2750 *value = (Xword) v; 2751 } 2752 break; 2753 default: 2754 { 2755 Conv_inv_buf_t inv_buf; 2756 eprintf(ofl->ofl_lml, ERR_FATAL, 2757 MSG_INTL(MSG_REL_UNSUPSZ), 2758 conv_reloc_type(ld_targ.t_m.m_mach, rsp->rel_rtype, 2759 0, &inv_buf), rsp->rel_isdesc->is_file->ifl_name, 2760 (rsp->rel_sname ? demangle(rsp->rel_sname) : 2761 MSG_INTL(MSG_STR_UNKNOWN)), (int)rep->re_fsize); 2762 } 2763 return (0); 2764 } 2765 return (1); 2766 } 2767 2768 2769 /* 2770 * Set the value at the given relocation target. 2771 * 2772 * entry: 2773 * ofl - Output file descriptor 2774 * rsp - Relocation record 2775 * data - Pointer to relocation target 2776 * value - Address of variable to recieve value 2777 * 2778 * exit: 2779 * The value of the data at the relocation target has 2780 * been stored in value. 2781 */ 2782 int 2783 ld_reloc_targval_set(Ofl_desc *ofl, Rel_desc *rsp, uchar_t *data, Xword value) 2784 { 2785 const Rel_entry *rep; 2786 2787 rep = &ld_targ.t_mr.mr_reloc_table[rsp->rel_rtype]; 2788 2789 switch (rep->re_fsize) { 2790 case 1: 2791 /* LINTED */ 2792 *((uchar_t *)data) = (uchar_t)value; 2793 break; 2794 case 2: 2795 { 2796 Half v = (Half)value; 2797 uchar_t *v_bytes = (uchar_t *)&v; 2798 2799 if (OFL_SWAP_RELOC_DATA(ofl, rsp)) { 2800 UL_ASSIGN_BSWAP_HALF(data, v_bytes); 2801 } else { 2802 UL_ASSIGN_HALF(data, v_bytes); 2803 } 2804 } 2805 break; 2806 case 4: 2807 { 2808 Word v = (Word)value; 2809 uchar_t *v_bytes = (uchar_t *)&v; 2810 2811 if (OFL_SWAP_RELOC_DATA(ofl, rsp)) { 2812 UL_ASSIGN_BSWAP_WORD(data, v_bytes); 2813 } else { 2814 UL_ASSIGN_WORD(data, v_bytes); 2815 } 2816 } 2817 break; 2818 default: 2819 { 2820 Conv_inv_buf_t inv_buf; 2821 eprintf(ofl->ofl_lml, ERR_FATAL, 2822 MSG_INTL(MSG_REL_UNSUPSZ), 2823 conv_reloc_type(ld_targ.t_m.m_mach, rsp->rel_rtype, 2824 0, &inv_buf), rsp->rel_isdesc->is_file->ifl_name, 2825 (rsp->rel_sname ? demangle(rsp->rel_sname) : 2826 MSG_INTL(MSG_STR_UNKNOWN)), (int)rep->re_fsize); 2827 } 2828 return (0); 2829 } 2830 return (1); 2831 } 2832 2833 2834 /* 2835 * Because of the combinations of 32-bit lib providing 64-bit support, and 2836 * visa-versa, the use of krtld's dorelocs can result in differing message 2837 * requirements that make msg.c/msg.h creation and chkmsg "interesting". 2838 * Thus the actual message files contain a couple of entries to satisfy 2839 * each architectures build. Here we add dummy calls to quieten chkmsg. 2840 * 2841 * chkmsg: MSG_INTL(MSG_REL_NOFIT) 2842 * chkmsg: MSG_INTL(MSG_REL_NONALIGN) 2843 */ 2844