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