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 (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 27 * 28 * Copyright 2020 Joyent, Inc. 29 */ 30 31 /* 32 * Processing of relocatable objects and shared objects. 33 */ 34 35 /* 36 * ld -- link/editor main program 37 */ 38 #include <sys/types.h> 39 #include <sys/time.h> 40 #include <sys/mman.h> 41 #include <string.h> 42 #include <stdio.h> 43 #include <locale.h> 44 #include <stdarg.h> 45 #include <debug.h> 46 #include "msg.h" 47 #include "_libld.h" 48 49 /* 50 * All target specific code is referenced via this global variable, which 51 * is initialized in ld_main(). This allows the linker to function as 52 * a cross linker, by vectoring to the target-specific code for the 53 * current target machine. 54 */ 55 Target ld_targ; 56 57 /* 58 * A default library search path is used if one was not supplied on the command 59 * line. Note: these strings can not use MSG_ORIG() since they are modified as 60 * part of the path processing. 61 */ 62 #if defined(_ELF64) 63 static char def_Plibpath[] = "/lib/64:/usr/lib/64"; 64 #else 65 static char def_Plibpath[] = "/usr/ccs/lib:/lib:/usr/lib"; 66 #endif 67 68 /* 69 * A default elf header provides for simplifying diagnostic processing. 70 */ 71 static Ehdr def_ehdr = { { ELFMAG0, ELFMAG1, ELFMAG2, ELFMAG3, 72 ELFCLASSNONE, ELFDATANONE }, 0, EM_NONE, 73 EV_CURRENT }; 74 75 /* 76 * ld-centric wrapper on top of veprintf(): 77 * - Accepts output descriptor rather than linkmap list 78 * - Sets the FLG_OF_FATAL/FLG_OF_WARN flags as necessary 79 */ 80 void 81 ld_eprintf(Ofl_desc *ofl, Error error, const char *format, ...) 82 { 83 va_list args; 84 85 /* Set flag indicating type of error being issued */ 86 switch (error) { 87 case ERR_NONE: 88 case ERR_WARNING_NF: 89 break; 90 case ERR_WARNING: 91 ofl->ofl_flags |= FLG_OF_WARN; 92 break; 93 case ERR_GUIDANCE: 94 if ((ofl->ofl_guideflags & FLG_OFG_ENABLE) == 0) 95 return; 96 ofl->ofl_guideflags |= FLG_OFG_ISSUED; 97 ofl->ofl_flags |= FLG_OF_WARN; 98 break; 99 default: 100 ofl->ofl_flags |= FLG_OF_FATAL; 101 } 102 103 /* Issue the error */ 104 va_start(args, format); 105 veprintf(ofl->ofl_lml, error, format, args); 106 va_end(args); 107 } 108 109 /* 110 * Establish the global state necessary to link the desired machine 111 * target, as reflected by the ld_targ global variable. 112 */ 113 int 114 ld_init_target(Lm_list *lml, Half mach) 115 { 116 switch (mach) { 117 case EM_386: 118 case EM_AMD64: 119 ld_targ = *ld_targ_init_x86(); 120 break; 121 122 case EM_SPARC: 123 case EM_SPARC32PLUS: 124 case EM_SPARCV9: 125 ld_targ = *ld_targ_init_sparc(); 126 break; 127 128 default: 129 { 130 Conv_inv_buf_t inv_buf; 131 132 eprintf(lml, ERR_FATAL, MSG_INTL(MSG_TARG_UNSUPPORTED), 133 conv_ehdr_mach(mach, 0, &inv_buf)); 134 return (1); 135 } 136 } 137 138 return (0); 139 } 140 141 142 /* 143 * The main program 144 */ 145 int 146 ld_main(int argc, char **argv, Half mach) 147 { 148 char *sgs_support; /* SGS_SUPPORT environment string */ 149 Half etype; 150 Ofl_desc *ofl; 151 ofl_flag_t save_flg_of_warn; 152 153 /* 154 * Establish a base time. Total time diagnostics are relative to 155 * entering the link-editor here. 156 */ 157 (void) gettimeofday(&DBG_TOTALTIME, NULL); 158 DBG_DELTATIME = DBG_TOTALTIME; 159 160 /* Output file descriptor */ 161 if ((ofl = libld_calloc(1, sizeof (Ofl_desc))) == 0) 162 return (1); 163 164 /* Initialize target state */ 165 if (ld_init_target(NULL, mach) != 0) 166 return (1); 167 168 /* 169 * Set up the default output ELF header to satisfy diagnostic 170 * requirements, and initialize the machine and class details. 171 */ 172 ofl->ofl_dehdr = &def_ehdr; 173 def_ehdr.e_ident[EI_CLASS] = ld_targ.t_m.m_class; 174 def_ehdr.e_ident[EI_DATA] = ld_targ.t_m.m_data; 175 def_ehdr.e_machine = ld_targ.t_m.m_mach; 176 177 /* 178 * Build up linker version string 179 */ 180 if ((ofl->ofl_sgsid = (char *)libld_calloc(MSG_SGS_ID_SIZE + 181 strlen(link_ver_string) + 1, 1)) == NULL) 182 return (1); 183 (void) strcpy(ofl->ofl_sgsid, MSG_ORIG(MSG_SGS_ID)); 184 (void) strcat(ofl->ofl_sgsid, link_ver_string); 185 186 /* 187 * Argument pass one. Get all the input flags (skip any files) and 188 * check for consistency. Return from ld_process_flags() marks the 189 * end of mapfile processing. The entrance criteria and segment 190 * descriptors are complete and in their final form. 191 */ 192 if (ld_process_flags(ofl, argc, argv) == S_ERROR) { 193 /* If any ERR_GUIDANCE messages were issued, add a summary */ 194 if (ofl->ofl_guideflags & FLG_OFG_ISSUED) 195 ld_eprintf(ofl, ERR_GUIDANCE, 196 MSG_INTL(MSG_GUIDE_SUMMARY)); 197 return (1); 198 } 199 if (ofl->ofl_flags & FLG_OF_FATAL) { 200 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_FLAGS)); 201 /* If any ERR_GUIDANCE messages were issued, add a summary */ 202 if (ofl->ofl_guideflags & FLG_OFG_ISSUED) 203 ld_eprintf(ofl, ERR_GUIDANCE, 204 MSG_INTL(MSG_GUIDE_SUMMARY)); 205 return (1); 206 } 207 208 /* 209 * At this point a call such as ld -V is considered complete. 210 */ 211 if (ofl->ofl_flags1 & FLG_OF1_DONE) 212 return (0); 213 214 /* Initialize signal handler */ 215 ld_init_sighandler(ofl); 216 217 /* 218 * Determine whether any support libraries should be loaded, 219 * (either through the SGS_SUPPORT environment variable and/or 220 * through the -S option). 221 */ 222 #if defined(_LP64) 223 if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_64))) == NULL) 224 #else 225 if ((sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT_32))) == NULL) 226 #endif 227 sgs_support = getenv(MSG_ORIG(MSG_SGS_SUPPORT)); 228 229 if (sgs_support && sgs_support[0]) { 230 const char *sep = MSG_ORIG(MSG_STR_COLON); 231 char *lib; 232 char *lasts; 233 234 DBG_CALL(Dbg_support_req(ofl->ofl_lml, sgs_support, 235 DBG_SUP_ENVIRON)); 236 if ((lib = strtok_r(sgs_support, sep, &lasts)) != NULL) { 237 do { 238 if (ld_sup_loadso(ofl, lib) == S_ERROR) 239 return (ld_exit(ofl)); 240 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 241 242 } while ((lib = strtok_r(NULL, sep, &lasts)) != NULL); 243 } 244 } 245 if (lib_support) { 246 Aliste idx; 247 char *lib; 248 249 for (APLIST_TRAVERSE(lib_support, idx, lib)) { 250 DBG_CALL(Dbg_support_req(ofl->ofl_lml, lib, 251 DBG_SUP_CMDLINE)); 252 if (ld_sup_loadso(ofl, lib) == S_ERROR) 253 return (ld_exit(ofl)); 254 DBG_CALL(Dbg_util_nl(ofl->ofl_lml, DBG_NL_STD)); 255 } 256 } 257 258 DBG_CALL(Dbg_ent_print(ofl->ofl_lml, 259 ofl->ofl_dehdr->e_ident[EI_OSABI], ofl->ofl_dehdr->e_machine, 260 ofl->ofl_ents)); 261 DBG_CALL(Dbg_seg_list(ofl->ofl_lml, 262 ofl->ofl_dehdr->e_ident[EI_OSABI], ofl->ofl_dehdr->e_machine, 263 ofl->ofl_segs)); 264 265 /* 266 * The objscnt and soscnt variables were used to estimate the expected 267 * input files, and size the symbol hash buckets accordingly. Reset 268 * these values now, so as to gain an accurate count from pass two, for 269 * later statistics diagnostics. 270 */ 271 ofl->ofl_objscnt = ofl->ofl_soscnt = 0; 272 273 /* 274 * Determine whether we can create the file before going any further. 275 */ 276 if (ld_open_outfile(ofl) == S_ERROR) 277 return (ld_exit(ofl)); 278 279 /* 280 * If the user didn't supply a library path supply a default. And, if 281 * no run-path has been specified (-R), see if the environment variable 282 * is in use (historic). 283 */ 284 if (Plibpath == NULL) 285 Plibpath = def_Plibpath; 286 287 if (ofl->ofl_rpath == NULL) { 288 char *rpath; 289 290 if (((rpath = getenv(MSG_ORIG(MSG_LD_RUN_PATH))) != NULL) && 291 rpath[0]) 292 ofl->ofl_rpath = rpath; 293 } 294 295 /* 296 * Argument pass two. Input all libraries and objects. 297 */ 298 if (ld_lib_setup(ofl) == S_ERROR) 299 return (ld_exit(ofl)); 300 301 /* 302 * Call ld_start() with the etype of our output file and the 303 * output file name. 304 */ 305 if (ofl->ofl_flags & FLG_OF_SHAROBJ) 306 etype = ET_DYN; 307 else if (ofl->ofl_flags & FLG_OF_RELOBJ) 308 etype = ET_REL; 309 else 310 etype = ET_EXEC; 311 312 ld_sup_start(ofl, etype, argv[0]); 313 314 /* 315 * Process all input files. 316 */ 317 if (ld_process_files(ofl, argc, argv) == S_ERROR) 318 return (ld_exit(ofl)); 319 if (ofl->ofl_flags & FLG_OF_FATAL) { 320 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_FILES), 321 ofl->ofl_name); 322 return (ld_exit(ofl)); 323 } 324 325 ld_sup_input_done(ofl); 326 327 /* 328 * Now that all input section processing is complete, validate and 329 * process any SHT_SUNW_move sections. 330 */ 331 if (ofl->ofl_ismove && (ld_process_move(ofl) == S_ERROR)) 332 return (ld_exit(ofl)); 333 334 /* 335 * Before validating all symbols count the number of relocation entries. 336 * If copy relocations exist, COMMON symbols must be generated which are 337 * assigned to the executables .bss. During sym_validate() the actual 338 * size and alignment of the .bss is calculated. Doing things in this 339 * order reduces the number of symbol table traversals required (however 340 * it does take a little longer for the user to be told of any undefined 341 * symbol errors). 342 */ 343 if (ld_reloc_init(ofl) == S_ERROR) 344 return (ld_exit(ofl)); 345 346 /* 347 * We need to know if FLG_OF_WARN is currently set, in case 348 * we need to honor a -z fatal-warnings request. However, we also 349 * need to know if a warning due to symbol validation results from 350 * the upcoming call to ld_sym_validate() in order to issue the 351 * appropriate message for it. So we save the current value, 352 * and clear the main flag. 353 */ 354 save_flg_of_warn = ofl->ofl_flags & FLG_OF_WARN; 355 ofl->ofl_flags &= ~FLG_OF_WARN; 356 357 if (ld_sym_validate(ofl) == S_ERROR) 358 return (ld_exit(ofl)); 359 360 /* 361 * Now that all symbol processing is complete see if any undefined 362 * references still remain. If we observed undefined symbols the 363 * FLG_OF_FATAL bit will be set: If creating a static executable, or a 364 * dynamic executable or shared object with the -zdefs flag set, this 365 * condition is fatal. If creating a shared object with the -Bsymbolic 366 * flag set, this condition is simply a warning. 367 */ 368 if (ofl->ofl_flags & FLG_OF_FATAL) 369 ld_eprintf(ofl, ERR_FATAL, MSG_INTL(MSG_ARG_SYM_FATAL), 370 ofl->ofl_name); 371 else if (ofl->ofl_flags & FLG_OF_WARN) 372 ld_eprintf(ofl, ERR_WARNING, MSG_INTL(MSG_ARG_SYM_WARN)); 373 374 /* 375 * Guidance: Use -z defs|nodefs when building shared objects. 376 * 377 * ld_sym_validate() will mask this guidance message out unless we are 378 * intended to send it here, so all we need to do is use OFL_GUIDANCE() 379 * to decide whether to issue it or not. 380 */ 381 if (OFL_GUIDANCE(ofl, FLG_OFG_NO_DEFS)) 382 ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_DEFS)); 383 384 /* 385 * Symbol processing was the final step before we start producing the 386 * output object. At this time, if we've seen warnings and the 387 * -z fatal-warnings option is specified, promote them to fatal, which 388 * will cause us to exit without creating an object. 389 * 390 * We didn't do this as the warnings were reported in order to 391 * maximize the number of problems a given link-editor invocation 392 * can diagnose. This is safe, since warnings are by definition events 393 * one can choose to ignore. 394 */ 395 if (((ofl->ofl_flags | save_flg_of_warn) & 396 (FLG_OF_WARN | FLG_OF_FATWARN)) == 397 (FLG_OF_WARN | FLG_OF_FATWARN)) 398 ofl->ofl_flags |= FLG_OF_FATAL; 399 400 /* 401 * If fatal errors occurred in symbol processing, or due to warnings 402 * promoted by -z fatal-warnings, this is the end of the line. 403 */ 404 if (ofl->ofl_flags & FLG_OF_FATAL) 405 return (ld_exit(ofl)); 406 407 /* 408 * Generate any necessary sections. 409 */ 410 if (ld_make_sections(ofl) == S_ERROR) 411 return (ld_exit(ofl)); 412 413 /* 414 * Now that all sections have been added to the output file, determine 415 * whether any mapfile section ordering was specified, and verify that 416 * all mapfile ordering directives have been matched. Issue a warning 417 * for any directives that have not been matched. 418 * Also, if SHF_ORDERED sections exist, set up sort key values. 419 */ 420 if (ofl->ofl_flags & (FLG_OF_OS_ORDER | FLG_OF_KEY)) 421 ld_sec_validate(ofl); 422 423 /* 424 * Having collected all the input data create the initial output file 425 * image, assign virtual addresses to the image, and generate a load 426 * map if the user requested one. 427 */ 428 if (ld_create_outfile(ofl) == S_ERROR) 429 return (ld_exit(ofl)); 430 431 if (ld_update_outfile(ofl) == S_ERROR) 432 return (ld_exit(ofl)); 433 if (ofl->ofl_flags & FLG_OF_GENMAP) 434 ld_map_out(ofl); 435 436 /* 437 * Build relocation sections and perform any relocation updates. 438 */ 439 if (ld_reloc_process(ofl) == S_ERROR) 440 return (ld_exit(ofl)); 441 442 /* 443 * Fill in contents for unwind header (.eh_frame_hdr) 444 */ 445 if (ld_unwind_populate_hdr(ofl) == S_ERROR) 446 return (ld_exit(ofl)); 447 448 /* 449 * Finally create the files elf checksum. 450 */ 451 if (ofl->ofl_checksum) 452 *ofl->ofl_checksum = (Xword)elf_checksum(ofl->ofl_elf); 453 454 /* 455 * If this is a cross link to a target with a different byte 456 * order than the linker, swap the data to the target byte order. 457 */ 458 if (((ofl->ofl_flags1 & FLG_OF1_ENCDIFF) != 0) && 459 (_elf_swap_wrimage(ofl->ofl_elf) != 0)) { 460 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_SWAP_WRIMAGE), 461 ofl->ofl_name); 462 return (ld_exit(ofl)); 463 } 464 465 /* 466 * We're done, so make sure the updates are flushed to the output file. 467 */ 468 if ((ofl->ofl_size = elf_update(ofl->ofl_welf, ELF_C_WRITE)) == 0) { 469 ld_eprintf(ofl, ERR_ELF, MSG_INTL(MSG_ELF_UPDATE), 470 ofl->ofl_name); 471 return (ld_exit(ofl)); 472 } 473 474 ld_sup_atexit(ofl, 0); 475 476 DBG_CALL(Dbg_statistics_ld(ofl)); 477 DBG_CALL(Dbg_basic_finish(ofl->ofl_lml)); 478 479 /* 480 * Wrap up debug output file if one is open 481 */ 482 dbg_cleanup(); 483 484 /* If any ERR_GUIDANCE messages were issued, add a summary */ 485 if (ofl->ofl_guideflags & FLG_OFG_ISSUED) { 486 ld_eprintf(ofl, ERR_GUIDANCE, MSG_INTL(MSG_GUIDE_SUMMARY)); 487 ofl->ofl_guideflags &= ~FLG_OFG_ISSUED; 488 } 489 490 /* 491 * One final check for any new warnings we found that should fail the 492 * link edit. 493 */ 494 if ((ofl->ofl_flags & (FLG_OF_WARN | FLG_OF_FATWARN)) == 495 (FLG_OF_WARN | FLG_OF_FATWARN)) 496 return (ld_exit(ofl)); 497 498 /* 499 * For performance reasons we don't actually free up the memory we've 500 * allocated, it will be freed when we exit. 501 * 502 * But the below line can be uncommented if/when we want to measure how 503 * our memory consumption and freeing are doing. We should be able to 504 * free all the memory that has been allocated as part of the link-edit 505 * process. 506 */ 507 /* ld_ofl_cleanup(ofl); */ 508 return (0); 509 } 510 511 /* 512 * Cleanup an Ifl_desc. 513 */ 514 static void 515 ifl_list_cleanup(APlist *apl) 516 { 517 Aliste idx; 518 Ifl_desc *ifl; 519 520 for (APLIST_TRAVERSE(apl, idx, ifl)) { 521 if (ifl->ifl_elf) 522 (void) elf_end(ifl->ifl_elf); 523 } 524 } 525 526 /* 527 * Cleanup all memory that has been dynamically allocated during libld 528 * processing and elf_end() all Elf descriptors that are still open. 529 */ 530 void 531 ld_ofl_cleanup(Ofl_desc *ofl) 532 { 533 Ld_heap *chp, *php; 534 Ar_desc *adp; 535 Aliste idx; 536 537 ifl_list_cleanup(ofl->ofl_objs); 538 ofl->ofl_objs = NULL; 539 ifl_list_cleanup(ofl->ofl_sos); 540 ofl->ofl_sos = NULL; 541 542 for (APLIST_TRAVERSE(ofl->ofl_ars, idx, adp)) { 543 Ar_aux *aup; 544 Elf_Arsym *arsym; 545 546 for (arsym = adp->ad_start, aup = adp->ad_aux; 547 arsym->as_name; ++arsym, ++aup) { 548 if ((aup->au_mem) && (aup->au_mem != FLG_ARMEM_PROC)) { 549 (void) elf_end(aup->au_mem->am_elf); 550 551 /* 552 * Null out all entries to this member so 553 * that we don't attempt to elf_end() it again. 554 */ 555 ld_ar_member(adp, arsym, aup, 0); 556 } 557 } 558 (void) elf_end(adp->ad_elf); 559 } 560 ofl->ofl_ars = NULL; 561 562 (void) elf_end(ofl->ofl_elf); 563 (void) elf_end(ofl->ofl_welf); 564 565 for (chp = ld_heap, php = NULL; chp; php = chp, chp = chp->lh_next) { 566 if (php) 567 (void) munmap((void *)php, 568 (size_t)php->lh_end - (size_t)php); 569 } 570 if (php) 571 (void) munmap((void *)php, (size_t)php->lh_end - (size_t)php); 572 573 ld_heap = NULL; 574 }