1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright (c) 1988 AT&T 24 * All Rights Reserved 25 * 26 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 27 * Use is subject to license terms. 28 */ 29 #pragma ident "%Z%%M% %I% %E% SMI" 30 31 /* 32 * Processing of relocatable objects and shared objects. 33 */ 34 #include <stdio.h> 35 #include <string.h> 36 #include <fcntl.h> 37 #include <unistd.h> 38 #include <link.h> 39 #include <limits.h> 40 #include <sys/stat.h> 41 #include <sys/systeminfo.h> 42 #include <debug.h> 43 #include <msg.h> 44 #include <_libld.h> 45 46 /* 47 * Decide if we can link against this input file. 48 */ 49 static int 50 ifl_verify(Ehdr * ehdr, Ofl_desc * ofl, Rej_desc * rej) 51 { 52 /* 53 * Check the validity of the elf header information for compatibility 54 * with this machine and our own internal elf library. 55 */ 56 if ((ehdr->e_machine != M_MACH) && 57 ((ehdr->e_machine != M_MACHPLUS) && 58 ((ehdr->e_flags & M_FLAGSPLUS) == 0))) { 59 rej->rej_type = SGS_REJ_MACH; 60 rej->rej_info = (uint_t)ehdr->e_machine; 61 return (0); 62 } 63 if (ehdr->e_ident[EI_DATA] != M_DATA) { 64 rej->rej_type = SGS_REJ_DATA; 65 rej->rej_info = (uint_t)ehdr->e_ident[EI_DATA]; 66 return (0); 67 } 68 if (ehdr->e_version > ofl->ofl_dehdr->e_version) { 69 rej->rej_type = SGS_REJ_VERSION; 70 rej->rej_info = (uint_t)ehdr->e_version; 71 return (0); 72 } 73 return (1); 74 } 75 76 /* 77 * Check sanity of file header and allocate an infile descriptor 78 * for the file being processed. 79 */ 80 static Ifl_desc * 81 ifl_setup(const char *name, Ehdr *ehdr, Elf *elf, Word flags, Ofl_desc *ofl, 82 Rej_desc *rej) 83 { 84 Ifl_desc *ifl; 85 List *list; 86 Rej_desc _rej = { 0 }; 87 88 if (ifl_verify(ehdr, ofl, &_rej) == 0) { 89 _rej.rej_name = name; 90 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 91 if (rej->rej_type == 0) { 92 *rej = _rej; 93 rej->rej_name = strdup(_rej.rej_name); 94 } 95 return (0); 96 } 97 98 if ((ifl = libld_calloc(1, sizeof (Ifl_desc))) == 0) 99 return ((Ifl_desc *)S_ERROR); 100 ifl->ifl_name = name; 101 ifl->ifl_ehdr = ehdr; 102 ifl->ifl_elf = elf; 103 ifl->ifl_flags = flags; 104 105 /* 106 * Is this file using 'extended Section Indexes'. If so, use the 107 * e_shnum & e_shstrndx which can be found at: 108 * 109 * e_shnum == Shdr[0].sh_size 110 * e_shstrndx == Shdr[0].sh_link 111 */ 112 if ((ehdr->e_shnum == 0) && (ehdr->e_shoff != 0)) { 113 Elf_Scn *scn; 114 Shdr *shdr0; 115 116 if ((scn = elf_getscn(elf, 0)) == NULL) { 117 eprintf(ofl->ofl_lml, ERR_ELF, 118 MSG_INTL(MSG_ELF_GETSCN), name); 119 ofl->ofl_flags |= FLG_OF_FATAL; 120 return ((Ifl_desc *)S_ERROR); 121 } 122 if ((shdr0 = elf_getshdr(scn)) == NULL) { 123 eprintf(ofl->ofl_lml, ERR_ELF, 124 MSG_INTL(MSG_ELF_GETSHDR), name); 125 ofl->ofl_flags |= FLG_OF_FATAL; 126 return ((Ifl_desc *)S_ERROR); 127 } 128 ifl->ifl_shnum = (Word)shdr0->sh_size; 129 if (ehdr->e_shstrndx == SHN_XINDEX) 130 ifl->ifl_shstrndx = shdr0->sh_link; 131 else 132 ifl->ifl_shstrndx = ehdr->e_shstrndx; 133 } else { 134 ifl->ifl_shnum = ehdr->e_shnum; 135 ifl->ifl_shstrndx = ehdr->e_shstrndx; 136 } 137 138 if ((ifl->ifl_isdesc = libld_calloc(ifl->ifl_shnum, 139 sizeof (Is_desc *))) == 0) 140 return ((Ifl_desc *)S_ERROR); 141 142 /* 143 * Record this new input file on the shared object or relocatable 144 * object input file list. 145 */ 146 if (ifl->ifl_ehdr->e_type == ET_DYN) { 147 list = &ofl->ofl_sos; 148 } else { 149 list = &ofl->ofl_objs; 150 } 151 152 if (list_appendc(list, ifl) == 0) 153 return ((Ifl_desc *)S_ERROR); 154 return (ifl); 155 } 156 157 /* 158 * Process a generic section. The appropriate section information is added 159 * to the files input descriptor list. 160 */ 161 static uintptr_t 162 process_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 163 Word ndx, int ident, Ofl_desc *ofl) 164 { 165 Is_desc *isp; 166 167 /* 168 * Create a new input section descriptor. If this is a NOBITS 169 * section elf_getdata() will still create a data buffer (the buffer 170 * will be null and the size will reflect the actual memory size). 171 */ 172 if ((isp = libld_calloc(sizeof (Is_desc), 1)) == 0) 173 return (S_ERROR); 174 isp->is_shdr = shdr; 175 isp->is_file = ifl; 176 isp->is_name = name; 177 isp->is_scnndx = ndx; 178 isp->is_flags = FLG_IS_EXTERNAL; 179 /* LINTED */ 180 isp->is_key = (Half)ident; 181 if ((isp->is_indata = elf_getdata(scn, NULL)) == NULL) { 182 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETDATA), 183 ifl->ifl_name); 184 ofl->ofl_flags |= FLG_OF_FATAL; 185 return (0); 186 } 187 188 if ((shdr->sh_flags & SHF_EXCLUDE) && 189 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) { 190 isp->is_flags |= FLG_IS_DISCARD; 191 } 192 193 /* 194 * Add the new input section to the files input section list and 195 * to the output section list (some sections like .strtab and 196 * .shstrtab are not added to the output section list). 197 * 198 * If the section has the SHF_ORDERED flag on, do the ld_place_section() 199 * after all input sections from this file are read in. 200 */ 201 ifl->ifl_isdesc[ndx] = isp; 202 if (ident && (shdr->sh_flags & ALL_SHF_ORDER) == 0) 203 return ((uintptr_t)ld_place_section(ofl, isp, ident, 0)); 204 205 if (ident && (shdr->sh_flags & ALL_SHF_ORDER)) { 206 isp->is_flags |= FLG_IS_ORDERED; 207 isp->is_ident = ident; 208 209 if ((ndx != 0) && (ndx == shdr->sh_link) && 210 (shdr->sh_flags & SHF_ORDERED)) { 211 return ((uintptr_t)ld_place_section(ofl, isp, 212 ident, 0)); 213 } 214 } 215 return (1); 216 } 217 218 /* 219 * Determine the software capabilities of the object being built from the 220 * capabilities of the input relocatable objects. One software capability 221 * is presently recognized, and represented with the following (sys/elf.h): 222 * 223 * SF1_SUNW_FPKNWN use/non-use of frame pointer is known, and 224 * SF1_SUNW_FPUSED the frame pointer is in use. 225 * 226 * The resolution of the present fame pointer state, and the capabilities 227 * provided by a new input relocatable object are: 228 * 229 * new input relocatable object 230 * 231 * present | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | <unknown> 232 * state | SF1_SUNW_FPUSED | | 233 * --------------------------------------------------------------------------- 234 * SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN 235 * SF1_SUNW_FPUSED | SF1_SUNW_FPUSED | | SF1_SUNW_FPUSED 236 * --------------------------------------------------------------------------- 237 * SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN 238 * | | | 239 * --------------------------------------------------------------------------- 240 * <unknown> | SF1_SUNW_FPKNWN | SF1_SUNW_FPKNWN | <unknown> 241 * | SF1_SUNW_FPUSED | | 242 */ 243 static void 244 sf1_cap(Ofl_desc *ofl, Xword val, Ifl_desc *ifl, const char *name) 245 { 246 Xword badval; 247 248 /* 249 * If a mapfile has established definitions to override any input 250 * capabilities, ignore any new input capabilities. 251 */ 252 if (ofl->ofl_flags1 & FLG_OF1_OVSFCAP) { 253 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_SF_1, 254 val, M_MACH); 255 return; 256 } 257 258 /* 259 * If this object doesn't specify any capabilities, ignore it, and 260 * leave the state as is. 261 */ 262 if (val == 0) 263 return; 264 265 /* 266 * Make sure we only accept known software capabilities. Note, that 267 * an F1_SUNW_FPUSED by itself is viewed as bad practice. 268 */ 269 if ((badval = (val & ~SF1_SUNW_MASK)) != 0) { 270 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1), 271 ifl->ifl_name, name, EC_XWORD(badval)); 272 val &= SF1_SUNW_MASK; 273 } 274 if (val == SF1_SUNW_FPUSED) { 275 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_BADSF1), 276 ifl->ifl_name, name, EC_XWORD(val)); 277 return; 278 } 279 280 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_SF_1, 281 ofl->ofl_sfcap_1, M_MACH); 282 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_SF_1, 283 val, M_MACH); 284 285 /* 286 * Determine the resolution of the present frame pointer and the 287 * new input relocatable objects frame pointer. 288 */ 289 if ((ofl->ofl_sfcap_1 & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) == 290 (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) { 291 /* 292 * If the new relocatable object isn't using a frame pointer, 293 * reduce the present state to unused. 294 */ 295 if ((val & (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) != 296 (SF1_SUNW_FPKNWN | SF1_SUNW_FPUSED)) 297 ofl->ofl_sfcap_1 &= ~SF1_SUNW_FPUSED; 298 299 } else if ((ofl->ofl_sfcap_1 & SF1_SUNW_FPKNWN) == 0) { 300 /* 301 * If the present state is unknown, take the new relocatable 302 * object frame pointer usage. 303 */ 304 ofl->ofl_sfcap_1 = val; 305 } 306 307 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_SF_1, 308 ofl->ofl_sfcap_1, M_MACH); 309 } 310 311 /* 312 * Determine the hardware capabilities of the object being built from the 313 * capabilities of the input relocatable objects. There's really little to 314 * do here, other than to offer diagnostics, hardware capabilities are simply 315 * additive. 316 */ 317 static void 318 hw1_cap(Ofl_desc *ofl, Xword val) 319 { 320 /* 321 * If a mapfile has established definitions to override any input 322 * capabilities, ignore any new input capabilities. 323 */ 324 if (ofl->ofl_flags1 & FLG_OF1_OVHWCAP) { 325 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_IGNORE, CA_SUNW_HW_1, 326 val, M_MACH); 327 return; 328 } 329 330 /* 331 * If this object doesn't specify any capabilities, ignore it, and 332 * leave the state as is. 333 */ 334 if (val == 0) 335 return; 336 337 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_OLD, CA_SUNW_HW_1, 338 ofl->ofl_hwcap_1, M_MACH); 339 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_NEW, CA_SUNW_HW_1, val, M_MACH); 340 341 ofl->ofl_hwcap_1 |= val; 342 343 Dbg_cap_sec_entry(ofl->ofl_lml, DBG_CAP_RESOLVED, CA_SUNW_HW_1, 344 ofl->ofl_hwcap_1, M_MACH); 345 } 346 347 /* 348 * Process a hardware/software capabilities section. Traverse the section 349 * updating the global capabilities variables as necessary. 350 */ 351 static void 352 process_cap(Ifl_desc *ifl, Is_desc *cisp, Ofl_desc *ofl) 353 { 354 Cap *cdata; 355 Word ndx, cnum; 356 357 DBG_CALL(Dbg_cap_sec_title(ofl)); 358 359 /* 360 * The capabilities are supposed to be terminated with a CA_SUNW_NULL 361 * entry. However, the compilers have been known to not follow this 362 * convention. Use the section information to determine the number 363 * of capabilities, and skip any CA_SUNW_NULL entries. 364 */ 365 cdata = (Cap *)cisp->is_indata->d_buf; 366 cnum = (Word)(cisp->is_shdr->sh_size / cisp->is_shdr->sh_entsize); 367 368 for (ndx = 0; ndx < cnum; cdata++, ndx++) { 369 switch (cdata->c_tag) { 370 case CA_SUNW_HW_1: 371 hw1_cap(ofl, cdata->c_un.c_val); 372 break; 373 case CA_SUNW_SF_1: 374 sf1_cap(ofl, cdata->c_un.c_val, ifl, 375 cisp->is_name); 376 break; 377 case CA_SUNW_NULL: 378 break; 379 default: 380 eprintf(ofl->ofl_lml, ERR_WARNING, 381 MSG_INTL(MSG_FIL_UNKCAP), 382 ifl->ifl_name, cisp->is_name, cdata->c_tag); 383 } 384 } 385 } 386 387 /* 388 * Simply process the section so that we have pointers to the data for use 389 * in later routines, however don't add the section to the output section 390 * list as we will be creating our own replacement sections later (ie. 391 * symtab and relocation). 392 */ 393 static uintptr_t 394 /* ARGSUSED5 */ 395 process_input(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 396 Word ndx, int ident, Ofl_desc *ofl) 397 { 398 return (process_section(name, ifl, shdr, scn, ndx, M_ID_NULL, ofl)); 399 } 400 401 /* 402 * Keep a running count of relocation entries from input relocatable objects for 403 * sizing relocation buckets later. If we're building an executable, save any 404 * relocations from shared objects to determine if any copy relocation symbol 405 * has a displacement relocation against it. 406 */ 407 static uintptr_t 408 /* ARGSUSED5 */ 409 process_reloc(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 410 Word ndx, int ident, Ofl_desc *ofl) 411 { 412 if (process_section(name, ifl, 413 shdr, scn, ndx, M_ID_NULL, ofl) == S_ERROR) 414 return (S_ERROR); 415 416 if (ifl->ifl_ehdr->e_type == ET_REL) { 417 if (shdr->sh_entsize && (shdr->sh_entsize <= shdr->sh_size)) 418 /* LINTED */ 419 ofl->ofl_relocincnt += 420 (Word)(shdr->sh_size / shdr->sh_entsize); 421 } else if (ofl->ofl_flags & FLG_OF_EXEC) { 422 if (list_appendc(&ifl->ifl_relsect, ifl->ifl_isdesc[ndx]) == 0) 423 return (S_ERROR); 424 } 425 return (1); 426 } 427 428 429 /* 430 * Process a string table section. A valid section contains an initial and 431 * final null byte. 432 */ 433 static uintptr_t 434 process_strtab(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 435 Word ndx, int ident, Ofl_desc *ofl) 436 { 437 char *data; 438 size_t size; 439 Is_desc *isp; 440 uintptr_t error; 441 442 /* 443 * Never include .stab.excl sections in any output file. 444 * If the -s flag has been specified strip any .stab sections. 445 */ 446 if (((ofl->ofl_flags & FLG_OF_STRIP) && ident && 447 (strncmp(name, MSG_ORIG(MSG_SCN_STAB), MSG_SCN_STAB_SIZE) == 0)) || 448 (strcmp(name, MSG_ORIG(MSG_SCN_STABEXCL)) == 0) && ident) 449 return (1); 450 451 /* 452 * If we got here to process a .shstrtab or .dynstr table, `ident' will 453 * be null. Otherwise make sure we don't have a .strtab section as this 454 * should not be added to the output section list either. 455 */ 456 if ((ident != M_ID_NULL) && 457 (strcmp(name, MSG_ORIG(MSG_SCN_STRTAB)) == 0)) 458 ident = M_ID_NULL; 459 460 error = process_section(name, ifl, shdr, scn, ndx, ident, ofl); 461 if ((error == 0) || (error == S_ERROR)) 462 return (error); 463 464 /* 465 * String tables should start and end with a NULL byte. Note, it has 466 * been known for the assembler to create empty string tables, so check 467 * the size before attempting to verify the data itself. 468 */ 469 isp = ifl->ifl_isdesc[ndx]; 470 size = isp->is_indata->d_size; 471 if (size) { 472 data = isp->is_indata->d_buf; 473 if (data[0] != '\0' || data[size - 1] != '\0') 474 eprintf(ofl->ofl_lml, ERR_WARNING, 475 MSG_INTL(MSG_FIL_MALSTR), ifl->ifl_name, name); 476 } else 477 isp->is_indata->d_buf = (void *)MSG_ORIG(MSG_STR_EMPTY); 478 479 ifl->ifl_flags |= FLG_IF_HSTRTAB; 480 return (1); 481 } 482 483 /* 484 * Invalid sections produce a warning and are skipped. 485 */ 486 static uintptr_t 487 /* ARGSUSED3 */ 488 invalid_section(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 489 Word ndx, int ident, Ofl_desc *ofl) 490 { 491 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_INVALSEC), 492 ifl->ifl_name, name, conv_sec_type(ifl->ifl_ehdr->e_machine, 493 shdr->sh_type, 0)); 494 return (1); 495 } 496 497 /* 498 * Process a progbits section. 499 */ 500 static uintptr_t 501 process_progbits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 502 Word ndx, int ident, Ofl_desc *ofl) 503 { 504 int stab_index = 0; 505 506 /* 507 * Never include .stab.excl sections in any output file. 508 * If the -s flag has been specified strip any .stab sections. 509 */ 510 if (ident && (strncmp(name, MSG_ORIG(MSG_SCN_STAB), 511 MSG_SCN_STAB_SIZE) == 0)) { 512 if ((ofl->ofl_flags & FLG_OF_STRIP) || 513 (strcmp((name + MSG_SCN_STAB_SIZE), 514 MSG_ORIG(MSG_SCN_EXCL)) == 0)) 515 return (1); 516 517 if (strcmp((name + MSG_SCN_STAB_SIZE), 518 MSG_ORIG(MSG_SCN_INDEX)) == 0) 519 stab_index = 1; 520 } 521 522 if ((ofl->ofl_flags & FLG_OF_STRIP) && ident) { 523 if ((strncmp(name, MSG_ORIG(MSG_SCN_DEBUG), 524 MSG_SCN_DEBUG_SIZE) == 0) || 525 (strcmp(name, MSG_ORIG(MSG_SCN_LINE)) == 0)) 526 return (1); 527 } 528 529 /* 530 * Update the ident to reflect the type of section we've got. 531 * 532 * If there is any .plt or .got section to generate we'll be creating 533 * our own version, so don't allow any input sections of these types to 534 * be added to the output section list (why a relocatable object would 535 * have a .plt or .got is a mystery, but stranger things have occurred). 536 */ 537 if (ident) { 538 if (shdr->sh_flags & SHF_TLS) 539 ident = M_ID_TLS; 540 else if ((shdr->sh_flags & ~ALL_SHF_IGNORE) == 541 (SHF_ALLOC | SHF_EXECINSTR)) 542 ident = M_ID_TEXT; 543 else if (shdr->sh_flags & SHF_ALLOC) { 544 if ((strcmp(name, MSG_ORIG(MSG_SCN_PLT)) == 0) || 545 (strcmp(name, MSG_ORIG(MSG_SCN_GOT)) == 0)) 546 ident = M_ID_NULL; 547 else if (stab_index) { 548 /* 549 * This is a work-around for x86 compilers that 550 * have set SHF_ALLOC for the .stab.index 551 * section. 552 * 553 * Because of this, make sure that the 554 * .stab.index does not end up as the last 555 * section in the text segment. Older linkers 556 * can produce segmentation violations when they 557 * strip (ld -s) against a shared object whose 558 * last section in the text segment is a .stab. 559 */ 560 ident = M_ID_INTERP; 561 } else 562 ident = M_ID_DATA; 563 } else 564 ident = M_ID_NOTE; 565 } 566 return (process_section(name, ifl, shdr, scn, ndx, ident, ofl)); 567 } 568 569 /* 570 * Handles the SHT_SUNW_{DEBUG,DEBUGSTR) sections. 571 */ 572 static uintptr_t 573 process_debug(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 574 Word ndx, int ident, Ofl_desc *ofl) 575 { 576 /* 577 * Debug information is discarded when the 'ld -s' flag is invoked. 578 */ 579 if (ofl->ofl_flags & FLG_OF_STRIP) { 580 return (1); 581 } 582 return (process_progbits(name, ifl, shdr, scn, ndx, ident, ofl)); 583 } 584 585 /* 586 * Process a nobits section. 587 */ 588 static uintptr_t 589 process_nobits(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 590 Word ndx, int ident, Ofl_desc *ofl) 591 { 592 if (ident) { 593 if (shdr->sh_flags & SHF_TLS) 594 ident = M_ID_TLSBSS; 595 #if defined(__x86) && defined(_ELF64) 596 else if (shdr->sh_flags & SHF_AMD64_LARGE) 597 ident = M_ID_LBSS; 598 #endif 599 else 600 ident = M_ID_BSS; 601 } 602 return (process_section(name, ifl, shdr, scn, ndx, ident, ofl)); 603 } 604 605 /* 606 * Process a SHT_*_ARRAY section. 607 */ 608 static uintptr_t 609 process_array(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 610 Word ndx, int ident, Ofl_desc *ofl) 611 { 612 Os_desc *osp; 613 Is_desc *isp; 614 615 if (ident) 616 ident = M_ID_ARRAY; 617 618 if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 619 return (S_ERROR); 620 621 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 622 ((osp = isp->is_osdesc) == 0)) 623 return (0); 624 625 if ((shdr->sh_type == SHT_FINI_ARRAY) && 626 (ofl->ofl_osfiniarray == 0)) 627 ofl->ofl_osfiniarray = osp; 628 else if ((shdr->sh_type == SHT_INIT_ARRAY) && 629 (ofl->ofl_osinitarray == 0)) 630 ofl->ofl_osinitarray = osp; 631 else if ((shdr->sh_type == SHT_PREINIT_ARRAY) && 632 (ofl->ofl_ospreinitarray == 0)) 633 ofl->ofl_ospreinitarray = osp; 634 635 return (1); 636 } 637 638 /* 639 * Process a SHT_SYMTAB_SHNDX section. 640 */ 641 static uintptr_t 642 process_sym_shndx(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 643 Word ndx, int ident, Ofl_desc *ofl) 644 { 645 if (process_input(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 646 return (S_ERROR); 647 648 /* 649 * Have we already seen the related SYMTAB - if so verify it now. 650 */ 651 if (shdr->sh_link < ndx) { 652 Is_desc *isp = ifl->ifl_isdesc[shdr->sh_link]; 653 654 if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) && 655 (isp->is_shdr->sh_type != SHT_DYNSYM))) { 656 eprintf(ofl->ofl_lml, ERR_FATAL, 657 MSG_INTL(MSG_FIL_INVSHLINK), ifl->ifl_name, name, 658 EC_XWORD(shdr->sh_link)); 659 return (S_ERROR); 660 } 661 isp->is_symshndx = ifl->ifl_isdesc[ndx]; 662 } 663 return (1); 664 } 665 666 /* 667 * Final processing for SHT_SYMTAB_SHNDX section. 668 */ 669 static uintptr_t 670 /* ARGSUSED2 */ 671 sym_shndx_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 672 { 673 if (isc->is_shdr->sh_link > isc->is_scnndx) { 674 Is_desc *isp = ifl->ifl_isdesc[isc->is_shdr->sh_link]; 675 676 if ((isp == 0) || ((isp->is_shdr->sh_type != SHT_SYMTAB) && 677 (isp->is_shdr->sh_type != SHT_DYNSYM))) { 678 eprintf(ofl->ofl_lml, ERR_FATAL, 679 MSG_INTL(MSG_FIL_INVSHLINK), isc->is_file->ifl_name, 680 isc->is_name, EC_XWORD(isc->is_shdr->sh_link)); 681 return (S_ERROR); 682 } 683 isp->is_symshndx = isc; 684 } 685 return (1); 686 } 687 688 /* 689 * Process .dynamic section from a relocatable object. 690 * 691 * Note: That the .dynamic section is only considered interesting when 692 * dlopen()ing a relocatable object (thus FLG_OF1_RELDYN can only get 693 * set when libld is called from ld.so.1). 694 */ 695 /*ARGSUSED*/ 696 static uintptr_t 697 process_rel_dynamic(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 698 Word ndx, int ident, Ofl_desc *ofl) 699 { 700 Dyn *dyn; 701 Elf_Scn *strscn; 702 Elf_Data *dp; 703 char *str; 704 705 /* 706 * Process .dynamic sections from relocatable objects ? 707 */ 708 if ((ofl->ofl_flags1 & FLG_OF1_RELDYN) == 0) 709 return (1); 710 711 /* 712 * Find the string section associated with the .dynamic section. 713 */ 714 if ((strscn = elf_getscn(ifl->ifl_elf, shdr->sh_link)) == NULL) { 715 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN), 716 ifl->ifl_name); 717 ofl->ofl_flags |= FLG_OF_FATAL; 718 return (0); 719 } 720 dp = elf_getdata(strscn, NULL); 721 str = (char *)dp->d_buf; 722 723 /* 724 * And get the .dynamic data 725 */ 726 dp = elf_getdata(scn, NULL); 727 728 for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) { 729 Ifl_desc *difl; 730 731 switch (dyn->d_tag) { 732 case DT_NEEDED: 733 case DT_USED: 734 if (((difl = 735 libld_calloc(1, sizeof (Ifl_desc))) == 0) || 736 (list_appendc(&ofl->ofl_sos, difl) == 0)) 737 return (S_ERROR); 738 739 difl->ifl_name = MSG_ORIG(MSG_STR_DYNAMIC); 740 difl->ifl_soname = str + (size_t)dyn->d_un.d_val; 741 difl->ifl_flags = FLG_IF_NEEDSTR; 742 break; 743 case DT_RPATH: 744 case DT_RUNPATH: 745 if ((ofl->ofl_rpath = add_string(ofl->ofl_rpath, 746 (str + (size_t)dyn->d_un.d_val))) == 747 (const char *)S_ERROR) 748 return (S_ERROR); 749 break; 750 case DT_VERSYM: 751 /* 752 * The Solaris ld does not put DT_VERSYM in the 753 * dynamic section. If the object has DT_VERSYM, 754 * then it must have been produced by the GNU ld, 755 * and is using the GNU style of versioning. 756 */ 757 ifl->ifl_flags |= FLG_IF_GNUVER; 758 break; 759 } 760 } 761 return (1); 762 } 763 764 /* 765 * Expand implicit references. Dependencies can be specified in terms of the 766 * $ORIGIN, $PLATFORM, $OSREL and $OSNAME tokens, either from their needed name, 767 * or via a runpath. In addition runpaths may also specify the $ISALIST token. 768 * 769 * Probably the most common reference to explicit dependencies (via -L) will be 770 * sufficient to find any associated implicit dependencies, but just in case we 771 * expand any occurrence of these known tokens here. 772 * 773 * Note, if any errors occur we simply return the original name. 774 * 775 * This code is remarkably similar to expand() in rtld/common/paths.c. 776 */ 777 static char *platform = 0; 778 static size_t platform_sz = 0; 779 static Isa_desc *isa = 0; 780 static Uts_desc *uts = 0; 781 782 static char * 783 expand(const char *parent, const char *name, char **next) 784 { 785 char _name[PATH_MAX], *nptr, *_next; 786 const char *optr; 787 size_t nrem = PATH_MAX - 1; 788 int expanded = 0, _expanded, isaflag = 0; 789 790 optr = name; 791 nptr = _name; 792 793 while (*optr) { 794 if (nrem == 0) 795 return ((char *)name); 796 797 if (*optr != '$') { 798 *nptr++ = *optr++, nrem--; 799 continue; 800 } 801 802 _expanded = 0; 803 804 if (strncmp(optr, MSG_ORIG(MSG_STR_ORIGIN), 805 MSG_STR_ORIGIN_SIZE) == 0) { 806 char *eptr; 807 808 /* 809 * For $ORIGIN, expansion is really just a concatenation 810 * of the parents directory name. For example, an 811 * explicit dependency foo/bar/lib1.so with a dependency 812 * on $ORIGIN/lib2.so would be expanded to 813 * foo/bar/lib2.so. 814 */ 815 if ((eptr = strrchr(parent, '/')) == 0) { 816 *nptr++ = '.'; 817 nrem--; 818 } else { 819 size_t len = eptr - parent; 820 821 if (len >= nrem) 822 return ((char *)name); 823 824 (void) strncpy(nptr, parent, len); 825 nptr = nptr + len; 826 nrem -= len; 827 } 828 optr += MSG_STR_ORIGIN_SIZE; 829 expanded = _expanded = 1; 830 831 } else if (strncmp(optr, MSG_ORIG(MSG_STR_PLATFORM), 832 MSG_STR_PLATFORM_SIZE) == 0) { 833 /* 834 * Establish the platform from sysconf - like uname -i. 835 */ 836 if ((platform == 0) && (platform_sz == 0)) { 837 char info[SYS_NMLN]; 838 long size; 839 840 size = sysinfo(SI_PLATFORM, info, SYS_NMLN); 841 if ((size != -1) && 842 (platform = libld_malloc((size_t)size))) { 843 (void) strcpy(platform, info); 844 platform_sz = (size_t)size - 1; 845 } else 846 platform_sz = 1; 847 } 848 if (platform != 0) { 849 if (platform_sz >= nrem) 850 return ((char *)name); 851 852 (void) strncpy(nptr, platform, platform_sz); 853 nptr = nptr + platform_sz; 854 nrem -= platform_sz; 855 856 optr += MSG_STR_PLATFORM_SIZE; 857 expanded = _expanded = 1; 858 } 859 860 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSNAME), 861 MSG_STR_OSNAME_SIZE) == 0) { 862 /* 863 * Establish the os name - like uname -s. 864 */ 865 if (uts == 0) 866 uts = conv_uts(); 867 868 if (uts && uts->uts_osnamesz) { 869 if (uts->uts_osnamesz >= nrem) 870 return ((char *)name); 871 872 (void) strncpy(nptr, uts->uts_osname, 873 uts->uts_osnamesz); 874 nptr = nptr + uts->uts_osnamesz; 875 nrem -= uts->uts_osnamesz; 876 877 optr += MSG_STR_OSNAME_SIZE; 878 expanded = _expanded = 1; 879 } 880 881 } else if (strncmp(optr, MSG_ORIG(MSG_STR_OSREL), 882 MSG_STR_OSREL_SIZE) == 0) { 883 /* 884 * Establish the os release - like uname -r. 885 */ 886 if (uts == 0) 887 uts = conv_uts(); 888 889 if (uts && uts->uts_osrelsz) { 890 if (uts->uts_osrelsz >= nrem) 891 return ((char *)name); 892 893 (void) strncpy(nptr, uts->uts_osrel, 894 uts->uts_osrelsz); 895 nptr = nptr + uts->uts_osrelsz; 896 nrem -= uts->uts_osrelsz; 897 898 optr += MSG_STR_OSREL_SIZE; 899 expanded = _expanded = 1; 900 } 901 902 } else if ((strncmp(optr, MSG_ORIG(MSG_STR_ISALIST), 903 MSG_STR_ISALIST_SIZE) == 0) && next && (isaflag++ == 0)) { 904 /* 905 * Establish instruction sets from sysconf. Note that 906 * this is only meaningful from runpaths. 907 */ 908 if (isa == 0) 909 isa = conv_isalist(); 910 911 if (isa && isa->isa_listsz && 912 (nrem > isa->isa_opt->isa_namesz)) { 913 size_t mlen, tlen, hlen = optr - name; 914 size_t no; 915 char *lptr; 916 Isa_opt *opt = isa->isa_opt; 917 918 (void) strncpy(nptr, opt->isa_name, 919 opt->isa_namesz); 920 nptr = nptr + opt->isa_namesz; 921 nrem -= opt->isa_namesz; 922 923 optr += MSG_STR_ISALIST_SIZE; 924 expanded = _expanded = 1; 925 926 tlen = strlen(optr); 927 928 /* 929 * As ISALIST expands to a number of elements, 930 * establish a new list to return to the caller. 931 * This will contain the present path being 932 * processed redefined for each isalist option, 933 * plus the original remaining list entries. 934 */ 935 mlen = ((hlen + tlen) * (isa->isa_optno - 1)) + 936 isa->isa_listsz - opt->isa_namesz; 937 if (*next) 938 mlen += strlen(*next); 939 if ((_next = lptr = libld_malloc(mlen)) == 0) 940 return (0); 941 942 for (no = 1, opt++; no < isa->isa_optno; 943 no++, opt++) { 944 (void) strncpy(lptr, name, hlen); 945 lptr = lptr + hlen; 946 (void) strncpy(lptr, opt->isa_name, 947 opt->isa_namesz); 948 lptr = lptr + opt->isa_namesz; 949 (void) strncpy(lptr, optr, tlen); 950 lptr = lptr + tlen; 951 *lptr++ = ':'; 952 } 953 if (*next) 954 (void) strcpy(lptr, *next); 955 else 956 *--lptr = '\0'; 957 } 958 } 959 960 /* 961 * If no expansion occurred skip the $ and continue. 962 */ 963 if (_expanded == 0) 964 *nptr++ = *optr++, nrem--; 965 } 966 967 /* 968 * If any ISALIST processing has occurred not only do we return the 969 * expanded node we're presently working on, but we must also update the 970 * remaining list so that it is effectively prepended with this node 971 * expanded to all remaining isalist options. Note that we can only 972 * handle one ISALIST per node. For more than one ISALIST to be 973 * processed we'd need a better algorithm than above to replace the 974 * newly generated list. Whether we want to encourage the number of 975 * pathname permutations this would provide is another question. So, for 976 * now if more than one ISALIST is encountered we return the original 977 * node untouched. 978 */ 979 if (isaflag) { 980 if (isaflag == 1) 981 *next = _next; 982 else 983 return ((char *)name); 984 } 985 986 *nptr = '\0'; 987 988 if (expanded) { 989 if ((nptr = libld_malloc(strlen(_name) + 1)) == 0) 990 return ((char *)name); 991 (void) strcpy(nptr, _name); 992 return (nptr); 993 } 994 return ((char *)name); 995 } 996 997 /* 998 * The Solaris ld does not put DT_VERSYM in the dynamic section, but the 999 * GNU ld does, and it is used by the runtime linker to implement their 1000 * versioning scheme. Use this fact to determine if the sharable object 1001 * was produced by the GNU ld rather than the Solaris one, and to set 1002 * FLG_IF_GNUVER if so. This needs to be done before the symbols are 1003 * processed, since the answer determines whether we interpret the 1004 * symbols versions according to Solaris or GNU rules. 1005 */ 1006 /*ARGSUSED*/ 1007 static uintptr_t 1008 process_dynamic_isgnu(const char *name, Ifl_desc *ifl, Shdr *shdr, 1009 Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl) 1010 { 1011 Dyn *dyn; 1012 Elf_Data *dp; 1013 1014 if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 1015 return (S_ERROR); 1016 1017 /* Get the .dynamic data */ 1018 dp = elf_getdata(scn, NULL); 1019 1020 for (dyn = (Dyn *)dp->d_buf; dyn->d_tag != DT_NULL; dyn++) { 1021 if (dyn->d_tag == DT_VERSYM) { 1022 ifl->ifl_flags |= FLG_IF_GNUVER; 1023 break; 1024 } 1025 } 1026 return (1); 1027 } 1028 1029 /* 1030 * Process a dynamic section. If we are processing an explicit shared object 1031 * then we need to determine if it has a recorded SONAME, if so, this name will 1032 * be recorded in the output file being generated as the NEEDED entry rather 1033 * than the shared objects filename itself. 1034 * If the mode of the link-edit indicates that no undefined symbols should 1035 * remain, then we also need to build up a list of any additional shared object 1036 * dependencies this object may have. In this case save any NEEDED entries 1037 * together with any associated run-path specifications. This information is 1038 * recorded on the `ofl_soneed' list and will be analyzed after all explicit 1039 * file processing has been completed (refer finish_libs()). 1040 */ 1041 static uintptr_t 1042 process_dynamic(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 1043 { 1044 Dyn *data, *dyn; 1045 char *str, *rpath = NULL; 1046 const char *soname, *needed; 1047 Sdf_desc *sdf; 1048 Listnode *lnp; 1049 1050 data = (Dyn *)isc->is_indata->d_buf; 1051 str = (char *)ifl->ifl_isdesc[isc->is_shdr->sh_link]->is_indata->d_buf; 1052 1053 /* 1054 * First loop through the dynamic section looking for a run path. 1055 */ 1056 if (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC)) { 1057 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) { 1058 if ((dyn->d_tag != DT_RPATH) && 1059 (dyn->d_tag != DT_RUNPATH)) 1060 continue; 1061 if ((rpath = str + (size_t)dyn->d_un.d_val) == NULL) 1062 continue; 1063 break; 1064 } 1065 } 1066 1067 /* 1068 * Now look for any needed dependencies (which may use the rpath) 1069 * or a new SONAME. 1070 */ 1071 for (dyn = data; dyn->d_tag != DT_NULL; dyn++) { 1072 if (dyn->d_tag == DT_SONAME) { 1073 if ((soname = str + (size_t)dyn->d_un.d_val) == NULL) 1074 continue; 1075 1076 /* 1077 * Update the input file structure with this new name. 1078 */ 1079 ifl->ifl_soname = soname; 1080 1081 } else if ((dyn->d_tag == DT_NEEDED) || 1082 (dyn->d_tag == DT_USED)) { 1083 if (!(ofl->ofl_flags & 1084 (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC))) 1085 continue; 1086 if ((needed = str + (size_t)dyn->d_un.d_val) == NULL) 1087 continue; 1088 1089 /* 1090 * Determine if this needed entry is already recorded on 1091 * the shared object needed list, if not create a new 1092 * definition for later processing (see finish_libs()). 1093 */ 1094 needed = expand(ifl->ifl_name, needed, (char **)0); 1095 1096 if ((sdf = sdf_find(needed, &ofl->ofl_soneed)) == 0) { 1097 if ((sdf = sdf_add(needed, 1098 &ofl->ofl_soneed)) == (Sdf_desc *)S_ERROR) 1099 return (S_ERROR); 1100 sdf->sdf_rfile = ifl->ifl_name; 1101 } 1102 1103 /* 1104 * Record the runpath (Note that we take the first 1105 * runpath which is exactly what ld.so.1 would do during 1106 * its dependency processing). 1107 */ 1108 if (rpath && (sdf->sdf_rpath == 0)) 1109 sdf->sdf_rpath = rpath; 1110 1111 } else if (dyn->d_tag == DT_FLAGS_1) { 1112 if (dyn->d_un.d_val & (DF_1_INITFIRST | DF_1_INTERPOSE)) 1113 ifl->ifl_flags &= ~FLG_IF_LAZYLD; 1114 if (dyn->d_un.d_val & DF_1_DISPRELPND) 1115 ifl->ifl_flags |= FLG_IF_DISPPEND; 1116 if (dyn->d_un.d_val & DF_1_DISPRELDNE) 1117 ifl->ifl_flags |= FLG_IF_DISPDONE; 1118 if (dyn->d_un.d_val & DF_1_NODIRECT) 1119 ifl->ifl_flags |= FLG_IF_NODIRECT; 1120 1121 } else if ((dyn->d_tag == DT_AUDIT) && 1122 (ifl->ifl_flags & FLG_IF_NEEDED)) { 1123 /* 1124 * Record audit string as DT_DEPAUDIT. 1125 */ 1126 if ((ofl->ofl_depaudit = add_string(ofl->ofl_depaudit, 1127 (str + (size_t)dyn->d_un.d_val))) == 1128 (const char *)S_ERROR) 1129 return (S_ERROR); 1130 1131 } else if (dyn->d_tag == DT_SUNW_RTLDINF) { 1132 /* 1133 * If a library has the SUNW_RTLDINF .dynamic entry 1134 * then we must not permit lazyloading of this library. 1135 * This is because critical startup information (TLS 1136 * routines) are provided as part of these interfaces 1137 * and we must have them as part of process startup. 1138 */ 1139 ifl->ifl_flags &= ~FLG_IF_LAZYLD; 1140 } 1141 } 1142 1143 /* 1144 * Perform some SONAME sanity checks. 1145 */ 1146 if (ifl->ifl_flags & FLG_IF_NEEDED) { 1147 Ifl_desc * sifl; 1148 1149 /* 1150 * Determine if anyone else will cause the same SONAME to be 1151 * used (this is either caused by two different files having the 1152 * same SONAME, or by one file SONAME actually matching another 1153 * file basename (if no SONAME is specified within a shared 1154 * library its basename will be used)). Probably rare, but some 1155 * idiot will do it. 1156 */ 1157 for (LIST_TRAVERSE(&ofl->ofl_sos, lnp, sifl)) { 1158 if ((strcmp(ifl->ifl_soname, sifl->ifl_soname) == 0) && 1159 (ifl != sifl)) { 1160 const char *hint, *iflb, *siflb; 1161 1162 /* 1163 * Determine the basename of each file. Perhaps 1164 * there are multiple copies of the same file 1165 * being brought in using different -L search 1166 * paths, and if so give an extra hint in the 1167 * error message. 1168 */ 1169 iflb = strrchr(ifl->ifl_name, '/'); 1170 if (iflb == NULL) 1171 iflb = ifl->ifl_name; 1172 else 1173 iflb++; 1174 1175 siflb = strrchr(sifl->ifl_name, '/'); 1176 if (siflb == NULL) 1177 siflb = sifl->ifl_name; 1178 else 1179 siflb++; 1180 1181 if (strcmp(iflb, siflb) == 0) 1182 hint = MSG_INTL(MSG_REC_CNFLTHINT); 1183 else 1184 hint = MSG_ORIG(MSG_STR_EMPTY); 1185 1186 eprintf(ofl->ofl_lml, ERR_FATAL, 1187 MSG_INTL(MSG_REC_OBJCNFLT), sifl->ifl_name, 1188 ifl->ifl_name, sifl->ifl_soname, hint); 1189 ofl->ofl_flags |= FLG_OF_FATAL; 1190 return (0); 1191 } 1192 } 1193 1194 /* 1195 * If the SONAME is the same as the name the user wishes to 1196 * record when building a dynamic library (refer -h option), 1197 * we also have a name clash. 1198 */ 1199 if (ofl->ofl_soname && 1200 (strcmp(ofl->ofl_soname, ifl->ifl_soname) == 0)) { 1201 eprintf(ofl->ofl_lml, ERR_FATAL, 1202 MSG_INTL(MSG_REC_OPTCNFLT), ifl->ifl_name, 1203 ifl->ifl_soname); 1204 ofl->ofl_flags |= FLG_OF_FATAL; 1205 return (0); 1206 } 1207 } 1208 return (1); 1209 } 1210 1211 /* 1212 * Process a relocation entry. At this point all input sections from this 1213 * input file have been assigned an input section descriptor which is saved 1214 * in the `ifl_isdesc' array. 1215 */ 1216 static uintptr_t 1217 rel_process(Is_desc *isc, Ifl_desc *ifl, Ofl_desc *ofl) 1218 { 1219 Word rndx; 1220 Is_desc *risc; 1221 Os_desc *osp; 1222 Shdr *shdr = isc->is_shdr; 1223 1224 /* 1225 * Make sure this is a valid relocation we can handle. 1226 */ 1227 if (shdr->sh_type != M_REL_SHT_TYPE) { 1228 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVALSEC), 1229 ifl->ifl_name, isc->is_name, 1230 conv_sec_type(ifl->ifl_ehdr->e_machine, shdr->sh_type, 0)); 1231 ofl->ofl_flags |= FLG_OF_FATAL; 1232 return (0); 1233 } 1234 1235 /* 1236 * From the relocation section header information determine which 1237 * section needs the actual relocation. Determine which output section 1238 * this input section has been assigned to and add to its relocation 1239 * list. Note that the relocation section may be null if it is not 1240 * required (ie. .debug, .stabs, etc). 1241 */ 1242 rndx = shdr->sh_info; 1243 if (rndx >= ifl->ifl_shnum) { 1244 /* 1245 * Broken input file. 1246 */ 1247 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_INVSHINFO), 1248 ifl->ifl_name, isc->is_name, EC_XWORD(rndx)); 1249 ofl->ofl_flags |= FLG_OF_FATAL; 1250 return (0); 1251 } 1252 if (rndx == 0) { 1253 if (list_appendc(&ofl->ofl_extrarels, isc) == 0) 1254 return (S_ERROR); 1255 } else if ((risc = ifl->ifl_isdesc[rndx]) != 0) { 1256 /* 1257 * Discard relocations if they are against a section 1258 * which has been discarded. 1259 */ 1260 if (risc->is_flags & FLG_IS_DISCARD) 1261 return (1); 1262 if ((osp = risc->is_osdesc) == 0) { 1263 if (risc->is_shdr->sh_type == SHT_SUNW_move) { 1264 /* 1265 * This section is processed later 1266 * in sunwmove_preprocess() and 1267 * reloc_init(). 1268 */ 1269 if (list_appendc(&ofl->ofl_mvrelisdescs, 1270 isc) == 0) 1271 return (S_ERROR); 1272 return (1); 1273 } 1274 eprintf(ofl->ofl_lml, ERR_FATAL, 1275 MSG_INTL(MSG_FIL_INVRELOC1), ifl->ifl_name, 1276 isc->is_name, risc->is_name); 1277 return (0); 1278 } 1279 if (list_appendc(&osp->os_relisdescs, isc) == 0) 1280 return (S_ERROR); 1281 } 1282 return (1); 1283 } 1284 1285 /* 1286 * SHF_EXCLUDE flags is set for this section. 1287 */ 1288 static uintptr_t 1289 process_exclude(const char *name, Ifl_desc *ifl, Shdr *shdr, Elf_Scn *scn, 1290 Word ndx, Ofl_desc *ofl) 1291 { 1292 /* 1293 * Sections SHT_SYMTAB and SHT_DYNDYM, even if SHF_EXCLUDE is on, might 1294 * be needed for ld processing. These sections need to be in the 1295 * internal table. Later it will be determined whether they can be 1296 * eliminated or not. 1297 */ 1298 if (shdr->sh_type == SHT_SYMTAB || shdr->sh_type == SHT_DYNSYM) 1299 return (0); 1300 1301 /* 1302 * Other checks 1303 */ 1304 if (shdr->sh_flags & SHF_ALLOC) { 1305 /* 1306 * A conflict, issue an warning message, and ignore the section. 1307 */ 1308 eprintf(ofl->ofl_lml, ERR_WARNING, MSG_INTL(MSG_FIL_EXCLUDE), 1309 ifl->ifl_name, name); 1310 return (0); 1311 } 1312 1313 /* 1314 * This sections is not going to the output file. 1315 */ 1316 return (process_section(name, ifl, shdr, scn, ndx, 0, ofl)); 1317 } 1318 1319 #if defined(__x86) && defined(_ELF64) 1320 1321 static uintptr_t 1322 process_amd64_unwind(const char *name, Ifl_desc *ifl, Shdr *shdr, 1323 Elf_Scn *scn, Word ndx, int ident, Ofl_desc *ofl) 1324 { 1325 Os_desc *osp, *eosp; 1326 Is_desc *isp; 1327 Listnode *lnp; 1328 1329 if (process_section(name, ifl, shdr, scn, ndx, ident, ofl) == S_ERROR) 1330 return (S_ERROR); 1331 1332 /* 1333 * When producing a relocatable object - just collect the sections. 1334 */ 1335 if (ofl->ofl_flags & FLG_OF_RELOBJ) 1336 return (1); 1337 1338 /* 1339 * If producing a executable or shared library, keep track of all the 1340 * output UNWIND sections to allow the creation of the appropriate 1341 * frame_hdr information. 1342 * 1343 * If the section hasn't been placed in the output file, then there's 1344 * nothing for us to do. 1345 */ 1346 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 1347 ((osp = isp->is_osdesc) == 0)) 1348 return (1); 1349 1350 /* 1351 * Check to see if this output section is already on the list, and if 1352 * not, add it. 1353 */ 1354 for (LIST_TRAVERSE(&ofl->ofl_unwind, lnp, eosp)) 1355 if (osp == eosp) 1356 return (1); 1357 1358 if (list_appendc(&ofl->ofl_unwind, osp) == 0) 1359 return (S_ERROR); 1360 1361 return (1); 1362 } 1363 1364 #endif 1365 1366 /* 1367 * Section processing state table. `Initial' describes the required initial 1368 * procedure to be called (if any), `Final' describes the final processing 1369 * procedure (ie. things that can only be done when all required sections 1370 * have been collected). 1371 */ 1372 static uintptr_t (*Initial[SHT_NUM][2])() = { 1373 1374 /* ET_REL ET_DYN */ 1375 1376 /* SHT_NULL */ invalid_section, invalid_section, 1377 /* SHT_PROGBITS */ process_progbits, process_progbits, 1378 /* SHT_SYMTAB */ process_input, process_input, 1379 /* SHT_STRTAB */ process_strtab, process_strtab, 1380 /* SHT_RELA */ process_reloc, process_reloc, 1381 /* SHT_HASH */ invalid_section, NULL, 1382 /* SHT_DYNAMIC */ process_rel_dynamic, process_dynamic_isgnu, 1383 /* SHT_NOTE */ process_section, NULL, 1384 /* SHT_NOBITS */ process_nobits, process_nobits, 1385 /* SHT_REL */ process_reloc, process_reloc, 1386 /* SHT_SHLIB */ process_section, invalid_section, 1387 /* SHT_DYNSYM */ invalid_section, process_input, 1388 /* SHT_UNKNOWN12 */ process_progbits, process_progbits, 1389 /* SHT_UNKNOWN13 */ process_progbits, process_progbits, 1390 /* SHT_INIT_ARRAY */ process_array, NULL, 1391 /* SHT_FINI_ARRAY */ process_array, NULL, 1392 /* SHT_PREINIT_ARRAY */ process_array, NULL, 1393 /* SHT_GROUP */ process_section, invalid_section, 1394 /* SHT_SYMTAB_SHNDX */ process_sym_shndx, NULL 1395 }; 1396 1397 static uintptr_t (*Final[SHT_NUM][2])() = { 1398 1399 /* SHT_NULL */ NULL, NULL, 1400 /* SHT_PROGBITS */ NULL, NULL, 1401 /* SHT_SYMTAB */ ld_sym_process, ld_sym_process, 1402 /* SHT_STRTAB */ NULL, NULL, 1403 /* SHT_RELA */ rel_process, NULL, 1404 /* SHT_HASH */ NULL, NULL, 1405 /* SHT_DYNAMIC */ NULL, process_dynamic, 1406 /* SHT_NOTE */ NULL, NULL, 1407 /* SHT_NOBITS */ NULL, NULL, 1408 /* SHT_REL */ rel_process, NULL, 1409 /* SHT_SHLIB */ NULL, NULL, 1410 /* SHT_DYNSYM */ NULL, ld_sym_process, 1411 /* SHT_UNKNOWN12 */ NULL, NULL, 1412 /* SHT_UNKNOWN13 */ NULL, NULL, 1413 /* SHT_INIT_ARRAY */ NULL, NULL, 1414 /* SHT_FINI_ARRAY */ NULL, NULL, 1415 /* SHT_PREINIT_ARRAY */ NULL, NULL, 1416 /* SHT_GROUP */ NULL, NULL, 1417 /* SHT_SYMTAB_SHNDX */ sym_shndx_process, NULL 1418 }; 1419 1420 #define MAXNDXSIZE 10 1421 1422 /* 1423 * Process an elf file. Each section is compared against the section state 1424 * table to determine whether it should be processed (saved), ignored, or 1425 * is invalid for the type of input file being processed. 1426 */ 1427 static uintptr_t 1428 process_elf(Ifl_desc *ifl, Elf *elf, Ofl_desc *ofl) 1429 { 1430 Elf_Scn *scn; 1431 Shdr *shdr; 1432 Word ndx, sndx; 1433 char *str, *name, _name[MAXNDXSIZE]; 1434 Word row, column; 1435 int ident; 1436 uintptr_t error; 1437 Is_desc *vdfisp, *vndisp, *vsyisp, *sifisp, *capisp; 1438 Sdf_desc *sdf; 1439 Word ordered_shndx = 0; /* index to first ordered section */ 1440 Word ordered_cnt = 0; 1441 1442 /* 1443 * First process the .shstrtab section so that later sections can 1444 * reference their name. 1445 */ 1446 ld_sup_file(ofl, ifl->ifl_name, elf_kind(elf), ifl->ifl_flags, elf); 1447 1448 sndx = ifl->ifl_shstrndx; 1449 if ((scn = elf_getscn(elf, (size_t)sndx)) == NULL) { 1450 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSCN), 1451 ifl->ifl_name); 1452 ofl->ofl_flags |= FLG_OF_FATAL; 1453 return (0); 1454 } 1455 if ((shdr = elf_getshdr(scn)) == NULL) { 1456 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_GETSHDR), 1457 ifl->ifl_name); 1458 ofl->ofl_flags |= FLG_OF_FATAL; 1459 return (0); 1460 } 1461 if ((name = elf_strptr(elf, (size_t)sndx, (size_t)shdr->sh_name)) == 1462 NULL) { 1463 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR), 1464 ifl->ifl_name); 1465 ofl->ofl_flags |= FLG_OF_FATAL; 1466 return (0); 1467 } 1468 1469 if (ld_sup_input_section(ofl, ifl, name, &shdr, sndx, scn, 1470 elf) == S_ERROR) 1471 return (S_ERROR); 1472 1473 /* 1474 * Reset the name since the shdr->sh_name could have been changed as 1475 * part of ld_sup_input_section(). If there is no name, fabricate one 1476 * using the section index. 1477 */ 1478 if (shdr->sh_name == 0) { 1479 (void) snprintf(_name, MAXNDXSIZE, MSG_ORIG(MSG_FMT_INDEX), 1480 EC_XWORD(sndx)); 1481 if ((name = libld_malloc(strlen(_name) + 1)) == 0) 1482 return (S_ERROR); 1483 (void) strcpy(name, _name); 1484 1485 } else if ((name = elf_strptr(elf, (size_t)sndx, 1486 (size_t)shdr->sh_name)) == NULL) { 1487 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_STRPTR), 1488 ifl->ifl_name); 1489 ofl->ofl_flags |= FLG_OF_FATAL; 1490 return (0); 1491 } 1492 1493 error = process_strtab(name, ifl, shdr, scn, sndx, FALSE, ofl); 1494 if ((error == 0) || (error == S_ERROR)) 1495 return (error); 1496 str = ifl->ifl_isdesc[sndx]->is_indata->d_buf; 1497 1498 /* 1499 * Determine the state table column from the input file type. Note, 1500 * shared library sections are not added to the output section list. 1501 */ 1502 if (ifl->ifl_ehdr->e_type == ET_DYN) { 1503 column = 1; 1504 ofl->ofl_soscnt++; 1505 ident = M_ID_NULL; 1506 } else { 1507 column = 0; 1508 ofl->ofl_objscnt++; 1509 ident = M_ID_UNKNOWN; 1510 } 1511 1512 DBG_CALL(Dbg_file_generic(ofl->ofl_lml, ifl)); 1513 ndx = 0; 1514 vdfisp = vndisp = vsyisp = sifisp = capisp = 0; 1515 scn = NULL; 1516 while (scn = elf_nextscn(elf, scn)) { 1517 ndx++; 1518 /* 1519 * As we've already processed the .shstrtab don't do it again. 1520 */ 1521 if (ndx == sndx) 1522 continue; 1523 1524 if ((shdr = elf_getshdr(scn)) == NULL) { 1525 eprintf(ofl->ofl_lml, ERR_ELF, 1526 MSG_INTL(MSG_ELF_GETSHDR), ifl->ifl_name); 1527 ofl->ofl_flags |= FLG_OF_FATAL; 1528 return (0); 1529 } 1530 name = str + (size_t)(shdr->sh_name); 1531 1532 if (ld_sup_input_section(ofl, ifl, name, &shdr, ndx, scn, 1533 elf) == S_ERROR) 1534 return (S_ERROR); 1535 1536 /* 1537 * Reset the name since the shdr->sh_name could have been 1538 * changed as part of ld_sup_input_section(). If there is no 1539 * name, fabricate one using the section index. 1540 */ 1541 if (shdr->sh_name == 0) { 1542 (void) snprintf(_name, MAXNDXSIZE, 1543 MSG_ORIG(MSG_FMT_INDEX), EC_XWORD(ndx)); 1544 if ((name = libld_malloc(strlen(_name) + 1)) == 0) 1545 return (S_ERROR); 1546 (void) strcpy(name, _name); 1547 } else 1548 name = str + (size_t)(shdr->sh_name); 1549 1550 row = shdr->sh_type; 1551 1552 /* 1553 * If the section has the SHF_EXCLUDE flag on, and we're not 1554 * generating a relocatable object, exclude the section. 1555 */ 1556 if (((shdr->sh_flags & SHF_EXCLUDE) != 0) && 1557 ((ofl->ofl_flags & FLG_OF_RELOBJ) == 0)) { 1558 if ((error = process_exclude(name, ifl, shdr, scn, 1559 ndx, ofl)) == S_ERROR) 1560 return (S_ERROR); 1561 if (error == 1) 1562 continue; 1563 } 1564 1565 /* 1566 * If this is a standard section type process it via the 1567 * appropriate action routine. 1568 */ 1569 if (row < SHT_NUM) { 1570 if (Initial[row][column] != NULL) { 1571 if (Initial[row][column](name, ifl, shdr, scn, 1572 ndx, ident, ofl) == S_ERROR) 1573 return (S_ERROR); 1574 } 1575 } else { 1576 /* 1577 * If this section is below SHT_LOSUNW then we don't 1578 * really know what to do with it, issue a warning 1579 * message but do the basic section processing anyway. 1580 */ 1581 if (row < (Word)SHT_LOSUNW) 1582 eprintf(ofl->ofl_lml, ERR_WARNING, 1583 MSG_INTL(MSG_FIL_INVALSEC), ifl->ifl_name, 1584 name, 1585 conv_sec_type(ifl->ifl_ehdr->e_machine, 1586 shdr->sh_type, 0)); 1587 1588 /* 1589 * Handle sections greater than SHT_LOSUNW. 1590 */ 1591 switch (row) { 1592 case SHT_SUNW_dof: 1593 if (process_section(name, ifl, shdr, scn, 1594 ndx, ident, ofl) == S_ERROR) 1595 return (S_ERROR); 1596 break; 1597 case SHT_SUNW_cap: 1598 if (process_section(name, ifl, shdr, scn, 1599 ndx, M_ID_NULL, ofl) == S_ERROR) 1600 return (S_ERROR); 1601 capisp = ifl->ifl_isdesc[ndx]; 1602 break; 1603 case SHT_SUNW_DEBUGSTR: 1604 case SHT_SUNW_DEBUG: 1605 if (process_debug(name, ifl, shdr, scn, 1606 ndx, ident, ofl) == S_ERROR) 1607 return (S_ERROR); 1608 break; 1609 case SHT_SUNW_move: 1610 if (process_section(name, ifl, shdr, scn, 1611 ndx, M_ID_NULL, ofl) == S_ERROR) 1612 return (S_ERROR); 1613 break; 1614 case SHT_SUNW_syminfo: 1615 if (process_section(name, ifl, shdr, scn, 1616 ndx, M_ID_NULL, ofl) == S_ERROR) 1617 return (S_ERROR); 1618 sifisp = ifl->ifl_isdesc[ndx]; 1619 break; 1620 case SHT_SUNW_ANNOTATE: 1621 case SHT_SUNW_COMDAT: 1622 if (process_progbits(name, ifl, shdr, scn, 1623 ndx, ident, ofl) == S_ERROR) 1624 return (S_ERROR); 1625 break; 1626 case SHT_SUNW_verdef: 1627 if (process_section(name, ifl, shdr, scn, 1628 ndx, M_ID_NULL, ofl) == S_ERROR) 1629 return (S_ERROR); 1630 vdfisp = ifl->ifl_isdesc[ndx]; 1631 break; 1632 case SHT_SUNW_verneed: 1633 if (process_section(name, ifl, shdr, scn, 1634 ndx, M_ID_NULL, ofl) == S_ERROR) 1635 return (S_ERROR); 1636 vndisp = ifl->ifl_isdesc[ndx]; 1637 break; 1638 case SHT_SUNW_versym: 1639 if (process_section(name, ifl, shdr, scn, 1640 ndx, M_ID_NULL, ofl) == S_ERROR) 1641 return (S_ERROR); 1642 vsyisp = ifl->ifl_isdesc[ndx]; 1643 break; 1644 #if defined(__sparc) 1645 case SHT_SPARC_GOTDATA: 1646 if (process_section(name, ifl, shdr, scn, 1647 ndx, M_ID_GOTDATA, ofl) == S_ERROR) 1648 return (S_ERROR); 1649 break; 1650 #endif 1651 #if defined(__x86) && defined(_ELF64) 1652 case SHT_AMD64_UNWIND: 1653 if (column == 0) { 1654 /* 1655 * column == ET_REL 1656 */ 1657 if (process_amd64_unwind(name, ifl, 1658 shdr, scn, ndx, M_ID_UNWIND, 1659 ofl) == S_ERROR) 1660 return (S_ERROR); 1661 } 1662 break; 1663 #endif 1664 default: 1665 if (ident != M_ID_NULL) 1666 ident = M_ID_USER; 1667 if (process_section(name, ifl, shdr, scn, 1668 ndx, ident, ofl) == S_ERROR) 1669 return (S_ERROR); 1670 break; 1671 } 1672 } 1673 1674 /* 1675 * If we have any sections that require ORDERED processing, 1676 * remember the index of the first ordered section. This let's 1677 * us know if we need an ORDERED place_section pass, and if so, 1678 * where to start. 1679 */ 1680 if (ifl->ifl_isdesc[ndx] && 1681 (ifl->ifl_isdesc[ndx]->is_shdr->sh_flags & ALL_SHF_ORDER)) { 1682 ordered_cnt++; 1683 if (ordered_shndx == 0) 1684 ordered_shndx = ndx; 1685 } 1686 } 1687 1688 /* 1689 * Now that all of sections have been placed, scan through any sections 1690 * which have special ordering requirements and place them now. 1691 */ 1692 if (ordered_shndx) { 1693 Word cnt; 1694 1695 for (ndx = ordered_shndx, cnt = 0; 1696 (ndx < ifl->ifl_shnum) && (cnt < ordered_cnt); ndx++) { 1697 Is_desc *isp; 1698 /* LINTED */ 1699 Os_desc *osp; 1700 1701 if (((isp = ifl->ifl_isdesc[ndx]) == 0) || 1702 ((isp->is_shdr->sh_flags & ALL_SHF_ORDER) == 0)) 1703 continue; 1704 1705 /* 1706 * If this is an ordered section, process it. 1707 */ 1708 cnt++; 1709 if ((osp = (Os_desc *)ld_process_ordered(ifl, ofl, ndx, 1710 ifl->ifl_shnum)) == (Os_desc *)S_ERROR) 1711 return (S_ERROR); 1712 1713 #if defined(__x86) && defined(_ELF64) 1714 /* 1715 * If this section is 'ordered' then it was not 1716 * caught in the previous 'place_section' operation. 1717 * 1718 * So - now that we have a OSP section for 1719 * the unwind info - record it. 1720 */ 1721 if (osp && 1722 (osp->os_shdr->sh_type == SHT_AMD64_UNWIND) && 1723 (append_amd64_unwind(osp, ofl) == S_ERROR)) 1724 return (S_ERROR); 1725 #endif 1726 } 1727 } 1728 1729 /* 1730 * If this is an explicit shared object determine if the user has 1731 * specified a control definition. This descriptor may specify which 1732 * version definitions can be used from this object (it may also update 1733 * the dependency to USED and supply an alternative SONAME). 1734 */ 1735 sdf = 0; 1736 if (column && (ifl->ifl_flags & FLG_IF_NEEDED)) { 1737 const char *base; 1738 1739 /* 1740 * Use the basename of the input file (typically this is the 1741 * compilation environment name, ie. libfoo.so). 1742 */ 1743 if ((base = strrchr(ifl->ifl_name, '/')) == NULL) 1744 base = ifl->ifl_name; 1745 else 1746 base++; 1747 1748 if ((sdf = sdf_find(base, &ofl->ofl_socntl)) != 0) { 1749 sdf->sdf_file = ifl; 1750 ifl->ifl_sdfdesc = sdf; 1751 } 1752 } 1753 1754 /* 1755 * Process any hardware/software capabilities sections. Only propagate 1756 * capabilities for input relocatable objects. If the object doesn't 1757 * contain any capabilities, any capability state that has already been 1758 * gathered will prevail. 1759 */ 1760 if (capisp && (ifl->ifl_ehdr->e_type == ET_REL)) 1761 process_cap(ifl, capisp, ofl); 1762 1763 /* 1764 * Process any version dependencies. These will establish shared object 1765 * `needed' entries in the same manner as will be generated from the 1766 * .dynamic's NEEDED entries. 1767 */ 1768 if (vndisp && (ofl->ofl_flags & (FLG_OF_NOUNDEF | FLG_OF_SYMBOLIC))) 1769 if (ld_vers_need_process(vndisp, ifl, ofl) == S_ERROR) 1770 return (S_ERROR); 1771 1772 /* 1773 * Before processing any symbol resolution or relocations process any 1774 * version sections. 1775 */ 1776 if (vsyisp) 1777 (void) ld_vers_sym_process(ofl->ofl_lml, vsyisp, ifl); 1778 1779 if (ifl->ifl_versym && 1780 (vdfisp || (sdf && (sdf->sdf_flags & FLG_SDF_SELECT)))) 1781 if (ld_vers_def_process(vdfisp, ifl, ofl) == S_ERROR) 1782 return (S_ERROR); 1783 1784 /* 1785 * Having collected the appropriate sections carry out any additional 1786 * processing if necessary. 1787 */ 1788 for (ndx = 0; ndx < ifl->ifl_shnum; ndx++) { 1789 Is_desc * isp; 1790 1791 if ((isp = ifl->ifl_isdesc[ndx]) == 0) 1792 continue; 1793 row = isp->is_shdr->sh_type; 1794 1795 if ((isp->is_flags & FLG_IS_DISCARD) == 0) 1796 ld_sup_section(ofl, isp->is_name, isp->is_shdr, ndx, 1797 isp->is_indata, elf); 1798 1799 /* 1800 * If this is a ST_SUNW_move section from a 1801 * a relocatable file, keep the input section. 1802 */ 1803 if ((row == SHT_SUNW_move) && (column == 0)) { 1804 if (list_appendc(&(ofl->ofl_ismove), isp) == 0) 1805 return (S_ERROR); 1806 } 1807 1808 /* 1809 * If this is a standard section type process it via the 1810 * appropriate action routine. 1811 */ 1812 if (row < SHT_NUM) { 1813 if (Final[row][column] != NULL) 1814 if (Final[row][column](isp, ifl, ofl) == 1815 S_ERROR) 1816 return (S_ERROR); 1817 } 1818 } 1819 1820 /* 1821 * After processing any symbol resolution, and if this dependency 1822 * indicates it contains symbols that can't be directly bound to, 1823 * set the symbols appropriately. 1824 */ 1825 if (sifisp && ((ifl->ifl_flags & (FLG_IF_NEEDED | FLG_IF_NODIRECT)) == 1826 (FLG_IF_NEEDED | FLG_IF_NODIRECT))) 1827 (void) ld_sym_nodirect(sifisp, ifl, ofl); 1828 1829 return (1); 1830 } 1831 1832 /* 1833 * Process the current input file. There are basically three types of files 1834 * that come through here: 1835 * 1836 * o files explicitly defined on the command line (ie. foo.o or bar.so), 1837 * in this case only the `name' field is valid. 1838 * 1839 * o libraries determined from the -l command line option (ie. -lbar), 1840 * in this case the `soname' field contains the basename of the located 1841 * file. 1842 * 1843 * Any shared object specified via the above two conventions must be recorded 1844 * as a needed dependency. 1845 * 1846 * o libraries specified as dependencies of those libraries already obtained 1847 * via the command line (ie. bar.so has a DT_NEEDED entry of fred.so.1), 1848 * in this case the `soname' field contains either a full pathname (if the 1849 * needed entry contained a `/'), or the basename of the located file. 1850 * These libraries are processed to verify symbol binding but are not 1851 * recorded as dependencies of the output file being generated. 1852 */ 1853 Ifl_desc * 1854 ld_process_ifl(const char *name, const char *soname, int fd, Elf *elf, 1855 Word flags, Ofl_desc * ofl, Rej_desc * rej) 1856 { 1857 Ifl_desc *ifl; 1858 Ehdr *ehdr; 1859 Listnode *lnp; 1860 uintptr_t error = 0; 1861 struct stat status; 1862 Ar_desc *adp; 1863 Rej_desc _rej; 1864 1865 /* 1866 * If this file was not extracted from an archive obtain its device 1867 * information. This will be used to determine if the file has already 1868 * been processed (rather than simply comparing filenames, the device 1869 * information provides a quicker comparison and detects linked files). 1870 */ 1871 if (!(flags & FLG_IF_EXTRACT)) 1872 (void) fstat(fd, &status); 1873 else { 1874 status.st_dev = 0; 1875 status.st_ino = 0; 1876 } 1877 1878 switch (elf_kind(elf)) { 1879 case ELF_K_AR: 1880 /* 1881 * Determine if we've already come across this archive file. 1882 */ 1883 if (!(flags & FLG_IF_EXTRACT)) { 1884 for (LIST_TRAVERSE(&ofl->ofl_ars, lnp, adp)) { 1885 if ((adp->ad_stdev != status.st_dev) || 1886 (adp->ad_stino != status.st_ino)) 1887 continue; 1888 1889 /* 1890 * We've seen this file before so reuse the 1891 * original archive descriptor and discard the 1892 * new elf descriptor. 1893 */ 1894 DBG_CALL(Dbg_file_reuse(ofl->ofl_lml, name, 1895 adp->ad_name)); 1896 (void) elf_end(elf); 1897 return ((Ifl_desc *)ld_process_archive(name, fd, 1898 adp, ofl)); 1899 } 1900 } 1901 1902 /* 1903 * As we haven't processed this file before establish a new 1904 * archive descriptor. 1905 */ 1906 adp = ld_ar_setup(name, elf, ofl); 1907 if ((adp == 0) || (adp == (Ar_desc *)S_ERROR)) 1908 return ((Ifl_desc *)adp); 1909 adp->ad_stdev = status.st_dev; 1910 adp->ad_stino = status.st_ino; 1911 1912 ld_sup_file(ofl, name, ELF_K_AR, flags, elf); 1913 1914 return ((Ifl_desc *)ld_process_archive(name, fd, adp, ofl)); 1915 1916 case ELF_K_ELF: 1917 /* 1918 * Obtain the elf header so that we can determine what type of 1919 * elf ELF_K_ELF file this is. 1920 */ 1921 if ((ehdr = elf_getehdr(elf)) == NULL) { 1922 int _class = gelf_getclass(elf); 1923 1924 /* 1925 * Failure could occur for a number of reasons at this 1926 * point. Typically the files class is incorrect (ie. 1927 * user is building 64-bit but managed to pint at 32-bit 1928 * libraries). However any number of elf errors can 1929 * also occur, such as from a truncated or corrupt file. 1930 * Here we try and get the best error message possible. 1931 */ 1932 if (M_CLASS != _class) { 1933 _rej.rej_type = SGS_REJ_CLASS; 1934 _rej.rej_info = (uint_t)_class; 1935 } else { 1936 _rej.rej_type = SGS_REJ_STR; 1937 _rej.rej_str = elf_errmsg(-1); 1938 } 1939 _rej.rej_name = name; 1940 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 1941 if (rej->rej_type == 0) { 1942 *rej = _rej; 1943 rej->rej_name = strdup(_rej.rej_name); 1944 } 1945 return (0); 1946 } 1947 1948 /* 1949 * Determine if we've already come across this file. 1950 */ 1951 if (!(flags & FLG_IF_EXTRACT)) { 1952 List * lst; 1953 1954 if (ehdr->e_type == ET_REL) 1955 lst = &ofl->ofl_objs; 1956 else 1957 lst = &ofl->ofl_sos; 1958 1959 /* 1960 * Traverse the appropriate file list and determine if 1961 * a dev/inode match is found. 1962 */ 1963 for (LIST_TRAVERSE(lst, lnp, ifl)) { 1964 /* 1965 * Ifl_desc generated via -Nneed, therefore no 1966 * actual file behind it. 1967 */ 1968 if (ifl->ifl_flags & FLG_IF_NEEDSTR) 1969 continue; 1970 1971 if ((ifl->ifl_stino != status.st_ino) || 1972 (ifl->ifl_stdev != status.st_dev)) 1973 continue; 1974 1975 /* 1976 * Disregard (skip) this image. 1977 */ 1978 DBG_CALL(Dbg_file_skip(ofl->ofl_lml, 1979 ifl->ifl_name, name)); 1980 (void) elf_end(elf); 1981 1982 /* 1983 * If the file was explicitly defined on the 1984 * command line (this is always the case for 1985 * relocatable objects, and is true for shared 1986 * objects when they weren't specified via -l or 1987 * were dragged in as an implicit dependency), 1988 * then warn the user. 1989 */ 1990 if ((flags & FLG_IF_CMDLINE) || 1991 (ifl->ifl_flags & FLG_IF_CMDLINE)) { 1992 const char *errmsg; 1993 1994 /* 1995 * Determine whether this is the same 1996 * file name as originally encountered 1997 * so as to provide the most 1998 * descriptive diagnostic. 1999 */ 2000 if (strcmp(name, ifl->ifl_name) == 0) 2001 errmsg = 2002 MSG_INTL(MSG_FIL_MULINC_1); 2003 else 2004 errmsg = 2005 MSG_INTL(MSG_FIL_MULINC_2); 2006 2007 eprintf(ofl->ofl_lml, ERR_WARNING, 2008 errmsg, name, ifl->ifl_name); 2009 } 2010 return (ifl); 2011 } 2012 } 2013 2014 /* 2015 * At this point, we know we need the file. Establish an input 2016 * file descriptor and continue processing. 2017 */ 2018 ifl = ifl_setup(name, ehdr, elf, flags, ofl, rej); 2019 if ((ifl == 0) || (ifl == (Ifl_desc *)S_ERROR)) 2020 return (ifl); 2021 ifl->ifl_stdev = status.st_dev; 2022 ifl->ifl_stino = status.st_ino; 2023 2024 /* 2025 * If -zignore is in effect, mark this file as a potential 2026 * candidate (the files use isn't actually determined until 2027 * symbol resolution and relocation processing are completed). 2028 */ 2029 if (ofl->ofl_flags1 & FLG_OF1_IGNORE) 2030 ifl->ifl_flags |= FLG_IF_IGNORE; 2031 2032 switch (ehdr->e_type) { 2033 case ET_REL: 2034 ld_mach_eflags(ehdr, ofl); 2035 error = process_elf(ifl, elf, ofl); 2036 break; 2037 case ET_DYN: 2038 if ((ofl->ofl_flags & FLG_OF_STATIC) || 2039 !(ofl->ofl_flags & FLG_OF_DYNLIBS)) { 2040 eprintf(ofl->ofl_lml, ERR_FATAL, 2041 MSG_INTL(MSG_FIL_SOINSTAT), name); 2042 ofl->ofl_flags |= FLG_OF_FATAL; 2043 return (0); 2044 } 2045 2046 /* 2047 * Record any additional shared object information. 2048 * If no soname is specified (eg. this file was 2049 * derived from a explicit filename declaration on the 2050 * command line, ie. bar.so) use the pathname. 2051 * This entry may be overridden if the files dynamic 2052 * section specifies an DT_SONAME value. 2053 */ 2054 if (soname == NULL) 2055 ifl->ifl_soname = ifl->ifl_name; 2056 else 2057 ifl->ifl_soname = soname; 2058 2059 /* 2060 * If direct bindings, lazy loading, or group 2061 * permissions need to be established, mark this object. 2062 */ 2063 if (ofl->ofl_flags1 & FLG_OF1_ZDIRECT) 2064 ifl->ifl_flags |= FLG_IF_DIRECT; 2065 if (ofl->ofl_flags1 & FLG_OF1_LAZYLD) 2066 ifl->ifl_flags |= FLG_IF_LAZYLD; 2067 if (ofl->ofl_flags1 & FLG_OF1_GRPPRM) 2068 ifl->ifl_flags |= FLG_IF_GRPPRM; 2069 error = process_elf(ifl, elf, ofl); 2070 2071 /* 2072 * At this point we know if this file will be 2073 * lazyloaded, or whether bindings to it must be direct. 2074 * In either case, a syminfo section is required. 2075 */ 2076 if (ifl->ifl_flags & (FLG_IF_LAZYLD | FLG_IF_DIRECT)) 2077 ofl->ofl_flags |= FLG_OF_SYMINFO; 2078 2079 break; 2080 default: 2081 (void) elf_errno(); 2082 _rej.rej_type = SGS_REJ_UNKFILE; 2083 _rej.rej_name = name; 2084 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 2085 if (rej->rej_type == 0) { 2086 *rej = _rej; 2087 rej->rej_name = strdup(_rej.rej_name); 2088 } 2089 return (0); 2090 } 2091 break; 2092 default: 2093 (void) elf_errno(); 2094 _rej.rej_type = SGS_REJ_UNKFILE; 2095 _rej.rej_name = name; 2096 DBG_CALL(Dbg_file_rejected(ofl->ofl_lml, &_rej)); 2097 if (rej->rej_type == 0) { 2098 *rej = _rej; 2099 rej->rej_name = strdup(_rej.rej_name); 2100 } 2101 return (0); 2102 } 2103 if ((error == 0) || (error == S_ERROR)) 2104 return ((Ifl_desc *)error); 2105 else 2106 return (ifl); 2107 } 2108 2109 /* 2110 * Having successfully opened a file, set up the necessary elf structures to 2111 * process it further. This small section of processing is slightly different 2112 * from the elf initialization required to process a relocatable object from an 2113 * archive (see libs.c: ld_process_archive()). 2114 */ 2115 Ifl_desc * 2116 ld_process_open(const char *opath, const char *ofile, int *fd, Ofl_desc *ofl, 2117 Word flags, Rej_desc *rej) 2118 { 2119 Elf *elf; 2120 const char *npath = opath; 2121 const char *nfile = ofile; 2122 2123 if ((elf = elf_begin(*fd, ELF_C_READ, NULL)) == NULL) { 2124 eprintf(ofl->ofl_lml, ERR_ELF, MSG_INTL(MSG_ELF_BEGIN), npath); 2125 ofl->ofl_flags |= FLG_OF_FATAL; 2126 return (0); 2127 } 2128 2129 /* 2130 * Determine whether the support library wishes to process this open. 2131 * The support library may return: 2132 * . a different ELF descriptor (in which case they should have 2133 * closed the original) 2134 * . a different file descriptor (in which case they should have 2135 * closed the original) 2136 * . a different path and file name (presumably associated with 2137 * a different file descriptor) 2138 * 2139 * A file descriptor of -1, or and ELF descriptor of zero indicates 2140 * the file should be ignored. 2141 */ 2142 ld_sup_open(ofl, &npath, &nfile, fd, flags, &elf, NULL, 0, 2143 elf_kind(elf)); 2144 2145 if ((*fd == -1) || (elf == NULL)) 2146 return (0); 2147 2148 return (ld_process_ifl(npath, nfile, *fd, elf, flags, ofl, rej)); 2149 } 2150 2151 /* 2152 * Process a required library (i.e. the dependency of a shared object). 2153 * Combine the directory and filename, check the resultant path size, and try 2154 * opening the pathname. 2155 */ 2156 static Ifl_desc * 2157 process_req_lib(Sdf_desc *sdf, const char *dir, const char *file, 2158 Ofl_desc * ofl, Rej_desc * rej) 2159 { 2160 size_t dlen, plen; 2161 int fd; 2162 char path[PATH_MAX]; 2163 const char *_dir = dir; 2164 2165 /* 2166 * Determine the sizes of the directory and filename to insure we don't 2167 * exceed our buffer. 2168 */ 2169 if ((dlen = strlen(dir)) == 0) { 2170 _dir = MSG_ORIG(MSG_STR_DOT); 2171 dlen = 1; 2172 } 2173 dlen++; 2174 plen = dlen + strlen(file) + 1; 2175 if (plen > PATH_MAX) { 2176 eprintf(ofl->ofl_lml, ERR_FATAL, MSG_INTL(MSG_FIL_PTHTOLONG), 2177 _dir, file); 2178 ofl->ofl_flags |= FLG_OF_FATAL; 2179 return (0); 2180 } 2181 2182 /* 2183 * Build the entire pathname and try and open the file. 2184 */ 2185 (void) strcpy(path, _dir); 2186 (void) strcat(path, MSG_ORIG(MSG_STR_SLASH)); 2187 (void) strcat(path, file); 2188 DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name, 2189 sdf->sdf_rfile, path)); 2190 2191 if ((fd = open(path, O_RDONLY)) == -1) 2192 return (0); 2193 else { 2194 Ifl_desc *ifl; 2195 char *_path; 2196 2197 if ((_path = libld_malloc(strlen(path) + 1)) == 0) 2198 return ((Ifl_desc *)S_ERROR); 2199 (void) strcpy(_path, path); 2200 ifl = ld_process_open(_path, &_path[dlen], &fd, ofl, 0, rej); 2201 if (fd != -1) 2202 (void) close(fd); 2203 return (ifl); 2204 } 2205 } 2206 2207 /* 2208 * Finish any library processing. Walk the list of so's that have been listed 2209 * as "included" by shared objects we have previously processed. Examine them, 2210 * without adding them as explicit dependents of this program, in order to 2211 * complete our symbol definition process. The search path rules are: 2212 * 2213 * o use any user supplied paths, i.e. LD_LIBRARY_PATH and -L, then 2214 * 2215 * o use any RPATH defined within the parent shared object, then 2216 * 2217 * o use the default directories, i.e. LIBPATH or -YP. 2218 */ 2219 uintptr_t 2220 ld_finish_libs(Ofl_desc *ofl) 2221 { 2222 Listnode *lnp1; 2223 Sdf_desc *sdf; 2224 Rej_desc rej = { 0 }; 2225 2226 /* 2227 * Make sure we are back in dynamic mode. 2228 */ 2229 ofl->ofl_flags |= FLG_OF_DYNLIBS; 2230 2231 for (LIST_TRAVERSE(&ofl->ofl_soneed, lnp1, sdf)) { 2232 Listnode *lnp2; 2233 char *path, *slash = NULL; 2234 int fd; 2235 Ifl_desc *ifl; 2236 char *file = (char *)sdf->sdf_name; 2237 2238 /* 2239 * See if this file has already been processed. At the time 2240 * this implicit dependency was determined there may still have 2241 * been more explicit dependencies to process. Note, if we ever 2242 * do parse the command line three times we would be able to 2243 * do all this checking when processing the dynamic section. 2244 */ 2245 if (sdf->sdf_file) 2246 continue; 2247 2248 for (LIST_TRAVERSE(&ofl->ofl_sos, lnp2, ifl)) { 2249 if (!(ifl->ifl_flags & FLG_IF_NEEDSTR) && 2250 (strcmp(file, ifl->ifl_soname) == 0)) { 2251 sdf->sdf_file = ifl; 2252 break; 2253 } 2254 } 2255 if (sdf->sdf_file) 2256 continue; 2257 2258 /* 2259 * If the current path name element embeds a "/", then it's to 2260 * be taken "as is", with no searching involved. Process all 2261 * "/" occurrences, so that we can deduce the base file name. 2262 */ 2263 for (path = file; *path; path++) { 2264 if (*path == '/') 2265 slash = path; 2266 } 2267 if (slash) { 2268 DBG_CALL(Dbg_libs_req(ofl->ofl_lml, sdf->sdf_name, 2269 sdf->sdf_rfile, file)); 2270 if ((fd = open(file, O_RDONLY)) == -1) { 2271 eprintf(ofl->ofl_lml, ERR_WARNING, 2272 MSG_INTL(MSG_FIL_NOTFOUND), file, 2273 sdf->sdf_rfile); 2274 } else { 2275 Rej_desc _rej = { 0 }; 2276 2277 ifl = ld_process_open(file, ++slash, &fd, ofl, 2278 0, &_rej); 2279 if (fd != -1) 2280 (void) close(fd); 2281 2282 if (ifl == (Ifl_desc *)S_ERROR) { 2283 return (S_ERROR); 2284 } 2285 if (_rej.rej_type) { 2286 eprintf(ofl->ofl_lml, ERR_WARNING, 2287 MSG_INTL(reject[_rej.rej_type]), 2288 _rej.rej_name ? rej.rej_name : 2289 MSG_INTL(MSG_STR_UNKNOWN), 2290 conv_reject_desc(&_rej)); 2291 } else 2292 sdf->sdf_file = ifl; 2293 } 2294 continue; 2295 } 2296 2297 /* 2298 * Now search for this file in any user defined directories. 2299 */ 2300 for (LIST_TRAVERSE(&ofl->ofl_ulibdirs, lnp2, path)) { 2301 Rej_desc _rej = { 0 }; 2302 2303 ifl = process_req_lib(sdf, path, file, ofl, &_rej); 2304 if (ifl == (Ifl_desc *)S_ERROR) { 2305 return (S_ERROR); 2306 } 2307 if (_rej.rej_type) { 2308 if (rej.rej_type == 0) { 2309 rej = _rej; 2310 rej.rej_name = strdup(_rej.rej_name); 2311 } 2312 } 2313 if (ifl) { 2314 sdf->sdf_file = ifl; 2315 break; 2316 } 2317 } 2318 if (sdf->sdf_file) 2319 continue; 2320 2321 /* 2322 * Next use the local rules defined within the parent shared 2323 * object. 2324 */ 2325 if (sdf->sdf_rpath != NULL) { 2326 char *rpath, *next; 2327 2328 rpath = libld_malloc(strlen(sdf->sdf_rpath) + 1); 2329 if (rpath == 0) 2330 return (S_ERROR); 2331 (void) strcpy(rpath, sdf->sdf_rpath); 2332 DBG_CALL(Dbg_libs_path(ofl->ofl_lml, rpath, 2333 LA_SER_RUNPATH, sdf->sdf_rfile)); 2334 if ((path = strtok_r(rpath, 2335 MSG_ORIG(MSG_STR_COLON), &next)) != NULL) { 2336 do { 2337 Rej_desc _rej = { 0 }; 2338 2339 path = expand(sdf->sdf_rfile, path, 2340 &next); 2341 2342 ifl = process_req_lib(sdf, path, 2343 file, ofl, &_rej); 2344 if (ifl == (Ifl_desc *)S_ERROR) { 2345 return (S_ERROR); 2346 } 2347 if ((_rej.rej_type) && 2348 (rej.rej_type == 0)) { 2349 rej = _rej; 2350 rej.rej_name = 2351 strdup(_rej.rej_name); 2352 } 2353 if (ifl) { 2354 sdf->sdf_file = ifl; 2355 break; 2356 } 2357 } while ((path = strtok_r(NULL, 2358 MSG_ORIG(MSG_STR_COLON), &next)) != NULL); 2359 } 2360 } 2361 if (sdf->sdf_file) 2362 continue; 2363 2364 /* 2365 * Finally try the default library search directories. 2366 */ 2367 for (LIST_TRAVERSE(&ofl->ofl_dlibdirs, lnp2, path)) { 2368 Rej_desc _rej = { 0 }; 2369 2370 ifl = process_req_lib(sdf, path, file, ofl, &rej); 2371 if (ifl == (Ifl_desc *)S_ERROR) { 2372 return (S_ERROR); 2373 } 2374 if (_rej.rej_type) { 2375 if (rej.rej_type == 0) { 2376 rej = _rej; 2377 rej.rej_name = strdup(_rej.rej_name); 2378 } 2379 } 2380 if (ifl) { 2381 sdf->sdf_file = ifl; 2382 break; 2383 } 2384 } 2385 if (sdf->sdf_file) 2386 continue; 2387 2388 /* 2389 * If we've got this far we haven't found the shared object. 2390 * If an object was found, but was rejected for some reason, 2391 * print a diagnostic to that effect, otherwise generate a 2392 * generic "not found" diagnostic. 2393 */ 2394 if (rej.rej_type) { 2395 eprintf(ofl->ofl_lml, ERR_WARNING, 2396 MSG_INTL(reject[rej.rej_type]), 2397 rej.rej_name ? rej.rej_name : 2398 MSG_INTL(MSG_STR_UNKNOWN), conv_reject_desc(&rej)); 2399 } else { 2400 eprintf(ofl->ofl_lml, ERR_WARNING, 2401 MSG_INTL(MSG_FIL_NOTFOUND), file, sdf->sdf_rfile); 2402 } 2403 } 2404 2405 /* 2406 * Finally, now that all objects have been input, make sure any version 2407 * requirements have been met. 2408 */ 2409 return (ld_vers_verify(ofl)); 2410 } 2411