1 /* Copyright (C) 2001-2010, 2012 Red Hat, Inc. 2 This file is part of elfutils. 3 Written by Ulrich Drepper <drepper (at) redhat.com>, 2001. 4 5 This file is free software; you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation; either version 3 of the License, or 8 (at your option) any later version. 9 10 elfutils is distributed in the hope that it will be useful, but 11 WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ 17 18 #ifdef HAVE_CONFIG_H 19 # include <config.h> 20 #endif 21 22 #include <argp.h> 23 #include <assert.h> 24 #include <error.h> 25 #include <fcntl.h> 26 #include <libelf.h> 27 #include <libintl.h> 28 #include <locale.h> 29 #include <stdio.h> 30 #include <stdio_ext.h> 31 #include <stdlib.h> 32 #include <string.h> 33 #include <unistd.h> 34 35 #include <system.h> 36 #include "ld.h" 37 #include "list.h" 38 39 40 /* Name and version of program. */ 41 static void print_version (FILE *stream, struct argp_state *state); 42 ARGP_PROGRAM_VERSION_HOOK_DEF = print_version; 43 44 /* Bug report address. */ 45 ARGP_PROGRAM_BUG_ADDRESS_DEF = PACKAGE_BUGREPORT; 46 47 48 /* Values for the various options. */ 49 enum 50 { 51 ARGP_whole_archive = 300, 52 ARGP_no_whole_archive, 53 ARGP_static, 54 ARGP_dynamic, 55 ARGP_pagesize, 56 ARGP_rpath_link, 57 ARGP_runpath, 58 ARGP_runpath_link, 59 ARGP_version_script, 60 ARGP_gc_sections, 61 ARGP_no_gc_sections, 62 ARGP_no_undefined, 63 ARGP_conserve, 64 ARGP_as_needed, 65 ARGP_no_as_needed, 66 ARGP_eh_frame_hdr, 67 ARGP_hash_style, 68 ARGP_build_id, 69 #if YYDEBUG 70 ARGP_yydebug, 71 #endif 72 }; 73 74 75 /* Definitions of arguments for argp functions. */ 76 static const struct argp_option options[] = 77 { 78 { NULL, 0, NULL, 0, N_("Input File Control:"), 0 }, 79 { "whole-archive", ARGP_whole_archive, NULL, 0, 80 N_("Include whole archives in the output from now on."), 0 }, 81 { "no-whole-archive", ARGP_no_whole_archive, NULL, 0, 82 N_("Stop including the whole archives in the output."), 0 }, 83 { NULL, 'l', N_("FILE"), OPTION_HIDDEN, NULL, 0 }, 84 { "start-group", '(', NULL, 0, N_("Start a group."), 0 }, 85 { "end-group", ')', NULL, 0, N_("End a group."), 0 }, 86 { NULL, 'L', N_("PATH"), 0, 87 N_("Add PATH to list of directories files are searched in."), 0 }, 88 { "as-needed", ARGP_as_needed, NULL, 0, 89 N_("Only set DT_NEEDED for following dynamic libs if actually used"), 0 }, 90 { "no-as-needed", ARGP_no_as_needed, NULL, 0, 91 N_("Always set DT_NEEDED for following dynamic libs"), 0 }, 92 { "rpath-link", ARGP_rpath_link, "PATH", OPTION_HIDDEN, NULL, 0 }, 93 { NULL, 'i', NULL, 0, N_("Ignore LD_LIBRARY_PATH environment variable."), 94 0 }, 95 96 { NULL, 0, NULL, 0, N_("Output File Control:"), 0 }, 97 { "output", 'o', N_("FILE"), 0, N_("Place output in FILE."), 0 }, 98 { NULL, 'z', "KEYWORD", OPTION_HIDDEN, NULL, 0 }, 99 { "-z nodefaultlib", '\0', NULL, OPTION_DOC, 100 N_("Object is marked to not use default search path at runtime."), 0 }, 101 { "-z allextract", '\0', NULL, OPTION_DOC, 102 N_("Same as --whole-archive."), 0 }, 103 { "-z defaultextract", '\0', NULL, OPTION_DOC, N_("\ 104 Default rules of extracting from archive; weak references are not enough."), 105 0 }, 106 { "-z weakextract", '\0', NULL, OPTION_DOC, 107 N_("Weak references cause extraction from archive."), 0 }, 108 { "-z muldefs", '\0', NULL, OPTION_DOC, 109 N_("Allow multiple definitions; first is used."), 0 }, 110 { "-z defs | nodefs", '\0', NULL, OPTION_DOC, 111 N_("Disallow/allow undefined symbols in DSOs."), 0 }, 112 { "no-undefined", ARGP_no_undefined, NULL, OPTION_HIDDEN, NULL, 0 }, 113 { "-z origin", '\0', NULL, OPTION_DOC, 114 N_("Object requires immediate handling of $ORIGIN."), 0 }, 115 { "-z now", '\0', NULL, OPTION_DOC, 116 N_("Relocation will not be processed lazily."), 0 }, 117 { "-z nodelete", '\0', NULL, OPTION_DOC, 118 N_("Object cannot be unloaded at runtime."), 0 }, 119 { "-z initfirst", '\0', NULL, OPTION_DOC, 120 N_("Mark object to be initialized first."), 0 }, 121 { "-z lazyload | nolazyload", '\0', NULL, OPTION_DOC, 122 N_("Enable/disable lazy-loading flag for following dependencies."), 0 }, 123 { "-z nodlopen", '\0', NULL, OPTION_DOC, 124 N_("Mark object as not loadable with 'dlopen'."), 0 }, 125 { "-z ignore | record", '\0', NULL, OPTION_DOC, 126 N_("Ignore/record dependencies on unused DSOs."), 0 }, 127 { "-z systemlibrary", '\0', NULL, OPTION_DOC, 128 N_("Generated DSO will be a system library."), 0 }, 129 { "entry", 'e', N_("ADDRESS"), 0, N_("Set entry point address."), 0 }, 130 { "static", ARGP_static, NULL, OPTION_HIDDEN, NULL, 0 }, 131 { "-B static", ARGP_static, NULL, OPTION_DOC, 132 N_("Do not link against shared libraries."), 0 }, 133 { "dynamic", ARGP_dynamic, NULL, OPTION_HIDDEN, NULL, 0 }, 134 { "-B dynamic", ARGP_dynamic, NULL, OPTION_DOC, 135 N_("Prefer linking against shared libraries."), 0 }, 136 { "export-dynamic", 'E', NULL, 0, N_("Export all dynamic symbols."), 0 }, 137 { "strip-all", 's', NULL, 0, N_("Strip all symbols."), 0 }, 138 { "strip-debug", 'S', NULL, 0, N_("Strip debugging symbols."), 0 }, 139 { "pagesize", ARGP_pagesize, "SIZE", 0, 140 N_("Assume pagesize for the target system to be SIZE."), 0 }, 141 { "rpath", 'R', "PATH", OPTION_HIDDEN, NULL, 0 }, 142 { "runpath", ARGP_runpath, "PATH", 0, N_("Set runtime DSO search path."), 143 0 }, 144 { "runpath-link", ARGP_runpath_link, "PATH", 0, 145 N_("Set link time DSO search path."), 0 }, 146 { "shared", 'G', NULL, 0, N_("Generate dynamic shared object."), 0 }, 147 { NULL, 'r', NULL, 0L, N_("Generate relocatable object."), 0 }, 148 { NULL, 'B', "KEYWORD", OPTION_HIDDEN, "", 0 }, 149 { "-B local", 'B', NULL, OPTION_DOC, 150 N_("Causes symbol not assigned to a version be reduced to local."), 0 }, 151 { "gc-sections", ARGP_gc_sections, NULL, 0, N_("Remove unused sections."), 152 0 }, 153 { "no-gc-sections", ARGP_no_gc_sections, NULL, 0, 154 N_("Don't remove unused sections."), 0 }, 155 { "soname", 'h', "NAME", 0, N_("Set soname of shared object."), 0 }, 156 { "dynamic-linker", 'I', "NAME", 0, N_("Set the dynamic linker name."), 0 }, 157 { NULL, 'Q', "YN", OPTION_HIDDEN, NULL, 0 }, 158 { "-Q y | n", 'Q', NULL, OPTION_DOC, 159 N_("Add/suppress addition indentifying link-editor to .comment section."), 160 0 }, 161 { "eh-frame-hdr", ARGP_eh_frame_hdr, NULL, 0, 162 N_("Create .eh_frame_hdr section"), 0 }, 163 { "hash-style", ARGP_hash_style, "STYLE", 0, 164 N_("Set hash style to sysv, gnu or both."), 0 }, 165 { "build-id", ARGP_build_id, "STYLE", OPTION_ARG_OPTIONAL, 166 N_("Generate build ID note (md5, sha1 (default), uuid)."), 0 }, 167 168 { NULL, 0, NULL, 0, N_("Linker Operation Control:"), 0 }, 169 { "verbose", 'v', NULL, 0, N_("Verbose messages."), 0 }, 170 { "trace", 't', NULL, 0, N_("Trace file opens."), 0 }, 171 { "conserve-memory", ARGP_conserve, NULL, 0, 172 N_("Trade speed for less memory usage"), 0 }, 173 { NULL, 'O', N_("LEVEL"), OPTION_ARG_OPTIONAL, 174 N_("Set optimization level to LEVEL."), 0 }, 175 { NULL, 'c', N_("FILE"), 0, N_("Use linker script in FILE."), 0 }, 176 #if YYDEBUG 177 { "yydebug", ARGP_yydebug, NULL, 0, 178 N_("Select to get parser debug information"), 0 }, 179 #endif 180 { "version-script", ARGP_version_script, "FILE", 0, 181 N_("Read version information from FILE."), 0 }, 182 { "emulation", 'm', "NAME", 0, N_("Set emulation to NAME."), 0 }, 183 184 { NULL, 0, NULL, 0, NULL, 0 } 185 }; 186 187 /* Short description of program. */ 188 static const char doc[] = N_("Combine object and archive files."); 189 190 /* Strings for arguments in help texts. */ 191 static const char args_doc[] = N_("[FILE]..."); 192 193 /* Prototype for option handler. */ 194 static void replace_args (int argc, char *argv[]); 195 static error_t parse_opt_1st (int key, char *arg, struct argp_state *state); 196 static error_t parse_opt_2nd (int key, char *arg, struct argp_state *state); 197 198 /* Data structure to communicate with argp functions. */ 199 static struct argp argp_1st = 200 { 201 options, parse_opt_1st, args_doc, doc, NULL, NULL, NULL 202 }; 203 static struct argp argp_2nd = 204 { 205 options, parse_opt_2nd, args_doc, doc, NULL, NULL, NULL 206 }; 207 208 209 /* Linker state. This contains all global information. */ 210 struct ld_state ld_state; 211 212 /* List of the input files. */ 213 static struct file_list 214 { 215 const char *name; 216 struct file_list *next; 217 } *input_file_list; 218 219 /* If nonzero be verbose. */ 220 int verbose; 221 222 /* If nonzero, trade speed for less memory/address space usage. */ 223 int conserve_memory; 224 225 /* The emulation name to use. */ 226 static const char *emulation; 227 228 /* Keep track of the nesting level. Even though we don't handle nested 229 groups we still keep track to improve the error messages. */ 230 static int group_level; 231 232 /* The last file we processed. */ 233 static struct usedfiles *last_file; 234 235 /* The default linker script. */ 236 /* XXX We'll do this a bit different in the real solution. */ 237 static const char *linker_script = SRCDIR "/elf32-i386.script"; 238 239 /* Nonzero if an error occurred while loading the input files. */ 240 static int error_loading; 241 242 243 /* Intermediate storage for the LD_LIBRARY_PATH information from the 244 environment. */ 245 static char *ld_library_path1; 246 247 /* Flag used to communicate with the scanner. */ 248 int ld_scan_version_script; 249 250 /* Name of the input file. */ 251 const char *ldin_fname; 252 253 /* Define by parser if required. */ 254 extern int lddebug; 255 256 257 /* Prototypes for local functions. */ 258 static void parse_z_option (const char *arg); 259 static void parse_z_option_2 (const char *arg); 260 static void parse_B_option (const char *arg); 261 static void parse_B_option_2 (const char *arg); 262 static void determine_output_format (void); 263 static void load_needed (void); 264 static void collect_sections (void); 265 static void add_rxxpath (struct pathelement **pathp, const char *str); 266 static void gen_rxxpath_data (void); 267 static void read_version_script (const char *fname); 268 static void create_lscript_symbols (void); 269 static void create_special_section_symbol (struct symbol **symp, 270 const char *name); 271 272 273 int 274 main (int argc, char *argv[]) 275 { 276 int remaining; 277 int err; 278 279 /* Sanity check. We always want to use the LFS functionality. */ 280 if (sizeof (off_t) != 8) 281 abort (); 282 283 /* We use no threads here which can interfere with handling a stream. */ 284 __fsetlocking (stdin, FSETLOCKING_BYCALLER); 285 __fsetlocking (stdout, FSETLOCKING_BYCALLER); 286 __fsetlocking (stderr, FSETLOCKING_BYCALLER); 287 288 /* Set locale. */ 289 setlocale (LC_ALL, ""); 290 291 /* Make sure the message catalog can be found. */ 292 bindtextdomain (PACKAGE_TARNAME, LOCALEDIR); 293 294 /* Initialize the message catalog. */ 295 textdomain (PACKAGE_TARNAME); 296 297 /* Before we start tell the ELF library which version we are using. */ 298 elf_version (EV_CURRENT); 299 300 /* The user can use the LD_LIBRARY_PATH environment variable to add 301 additional lookup directories. */ 302 ld_library_path1 = getenv ("LD_LIBRARY_PATH"); 303 304 /* Initialize the memory handling. */ 305 #define obstack_chunk_alloc xmalloc 306 #define obstack_chunk_free free 307 obstack_init (&ld_state.smem); 308 309 /* Recognize old-style parameters for compatibility. */ 310 replace_args (argc, argv); 311 312 /* One quick pass over the parameters which allows us to scan for options 313 with global effect which influence the rest of the processing. */ 314 argp_parse (&argp_1st, argc, argv, ARGP_IN_ORDER, &remaining, NULL); 315 316 /* We need at least one input file. */ 317 if (input_file_list == NULL) 318 { 319 error (0, 0, gettext ("At least one input file needed")); 320 argp_help (&argp_1st, stderr, ARGP_HELP_SEE, "ld"); 321 exit (EXIT_FAILURE); 322 } 323 324 /* Determine which ELF backend to use. */ 325 determine_output_format (); 326 327 /* If no hash style was specific default to the oldand slow SysV 328 method. */ 329 if (unlikely (ld_state.hash_style == hash_style_none)) 330 ld_state.hash_style = hash_style_sysv; 331 332 /* Prepare state. */ 333 err = ld_prepare_state (emulation); 334 if (err != 0) 335 error (EXIT_FAILURE, 0, gettext ("error while preparing linking")); 336 337 /* XXX Read the linker script now. Since we later will have the linker 338 script built in we don't go into trouble to make sure we handle GROUP 339 statements in the script. This simply must not happen. */ 340 ldin = fopen (linker_script, "r"); 341 if (ldin == NULL) 342 error (EXIT_FAILURE, errno, gettext ("cannot open linker script '%s'"), 343 linker_script); 344 /* No need for locking. */ 345 __fsetlocking (ldin, FSETLOCKING_BYCALLER); 346 347 ld_state.srcfiles = NULL; 348 ldlineno = 1; 349 ld_scan_version_script = 0; 350 ldin_fname = linker_script; 351 if (ldparse () != 0) 352 /* Something went wrong during parsing. */ 353 exit (EXIT_FAILURE); 354 fclose (ldin); 355 356 /* We now might have a list of directories to look for libraries in 357 named by the linker script. Put them in a different list so that 358 they are searched after all paths given by the user on the 359 command line. */ 360 ld_state.default_paths = ld_state.paths; 361 ld_state.paths = ld_state.tailpaths = NULL; 362 363 /* Get runpath/rpath information in usable form. */ 364 gen_rxxpath_data (); 365 366 /* Parse and process arguments for real. */ 367 argp_parse (&argp_2nd, argc, argv, ARGP_IN_ORDER, &remaining, NULL); 368 /* All options should have been processed by the argp parser. */ 369 assert (remaining == argc); 370 371 /* Process the last file. */ 372 while (last_file != NULL) 373 /* Try to open the file. */ 374 error_loading |= FILE_PROCESS (-1, last_file, &ld_state, &last_file); 375 376 /* Stop if there has been a problem while reading the input files. */ 377 if (error_loading) 378 exit (error_loading); 379 380 /* See whether all opened -( were closed. */ 381 if (group_level > 0) 382 { 383 error (0, 0, gettext ("-( without matching -)")); 384 argp_help (&argp_1st, stderr, ARGP_HELP_SEE, "ld"); 385 exit (EXIT_FAILURE); 386 } 387 388 /* When we create a relocatable file we don't have to look for the 389 DT_NEEDED DSOs and we also don't test for undefined symbols. */ 390 if (ld_state.file_type != relocatable_file_type) 391 { 392 /* At this point we have loaded all the direct dependencies. What 393 remains to be done is find the indirect dependencies. These are 394 DSOs which are referenced by the DT_NEEDED entries in the DSOs 395 which are direct dependencies. We have to transitively find and 396 load all these dependencies. */ 397 load_needed (); 398 399 /* At this point all object files and DSOs are read. If there 400 are still undefined symbols left they might have to be 401 synthesized from the linker script. */ 402 create_lscript_symbols (); 403 404 /* Now that we have loaded all the object files we can determine 405 whether we have any non-weak unresolved references left. If 406 there are any we stop. If the user used the '-z nodefs' option 407 and we are creating a DSO don't perform the tests. */ 408 if (FLAG_UNRESOLVED (&ld_state) != 0) 409 exit (1); 410 } 411 412 /* Collect information about the relocations which will be carried 413 forward into the output. We have to do this here and now since 414 we need to know which sections have to be created. */ 415 if (ld_state.file_type != relocatable_file_type) 416 { 417 void *p ; 418 struct scnhead *h; 419 420 p = NULL; 421 while ((h = ld_section_tab_iterate (&ld_state.section_tab, &p)) != NULL) 422 if (h->type == SHT_REL || h->type == SHT_RELA) 423 { 424 struct scninfo *runp = h->last; 425 do 426 { 427 /* If we are processing the relocations determine how 428 many will be in the output file. Also determine 429 how many GOT entries are needed. */ 430 COUNT_RELOCATIONS (&ld_state, runp); 431 432 ld_state.relsize_total += runp->relsize; 433 } 434 while ((runp = runp->next) != h->last); 435 } 436 } 437 438 /* Not part of the gABI, but part of every psABI: the symbols for the 439 GOT section. Add the symbol if necessary. */ 440 if (ld_state.need_got) 441 create_special_section_symbol (&ld_state.got_symbol, 442 "_GLOBAL_OFFSET_TABLE_"); 443 /* Similarly for the _DYNAMIC symbol which points to the dynamic 444 section. */ 445 if (dynamically_linked_p ()) 446 create_special_section_symbol (&ld_state.dyn_symbol, "_DYNAMIC"); 447 448 /* We are ready to start working on the output file. Not all 449 information has been gather or created yet. This will be done as 450 we go. Open the file now. */ 451 if (OPEN_OUTFILE (&ld_state, EM_NONE, ELFCLASSNONE, ELFDATANONE) != 0) 452 exit (1); 453 454 /* Create the sections which are generated by the linker and are not 455 present in the input file. The output file must already have 456 been opened since we need the ELF descriptor to deduce type 457 sizes. */ 458 GENERATE_SECTIONS (&ld_state); 459 460 /* At this point we have read all the files and know all the 461 sections which have to be linked into the application. We do now 462 create an array listing all the sections. We will than pass this 463 array to a system specific function which can reorder it at will. 464 The functions can also merge sections if this is what is 465 wanted. */ 466 collect_sections (); 467 468 /* Create the output sections now. This may requires sorting them 469 first. */ 470 CREATE_SECTIONS (&ld_state); 471 472 /* Create the output file data. Appropriate code for the selected 473 output file type is called. */ 474 if (CREATE_OUTFILE (&ld_state) != 0) 475 exit (1); 476 477 /* Finalize the output file, write the data out. */ 478 err |= FINALIZE (&ld_state); 479 480 /* Return with an non-zero exit status also if any error message has 481 been printed. */ 482 return err | (error_message_count != 0); 483 } 484 485 486 static void 487 replace_args (int argc, char *argv[]) 488 { 489 static const struct 490 { 491 const char *from; 492 const char *to; 493 } args[] = 494 { 495 { "-export-dynamic", "--export-dynamic" }, 496 { "-dynamic-linker", "--dynamic-linker" }, 497 { "-static", "--static" }, 498 }; 499 const size_t nargs = sizeof (args) / sizeof (args[0]); 500 501 for (int i = 1; i < argc; ++i) 502 if (argv[i][0] == '-' && islower (argv[i][1]) && argv[i][2] != '\0') 503 for (size_t j = 0; j < nargs; ++j) 504 if (strcmp (argv[i], args[j].from) == 0) 505 { 506 argv[i] = (char *) args[j].to; 507 break; 508 } 509 } 510 511 512 static int 513 valid_hexarg (const char *arg) 514 { 515 if (strncasecmp (arg, "0x", 2) != 0) 516 return 0; 517 518 arg += 2; 519 do 520 { 521 if (isxdigit (arg[0]) && isxdigit (arg[1])) 522 { 523 arg += 2; 524 if (arg[0] == '-' || arg[0] == ':') 525 ++arg; 526 } 527 else 528 return 0; 529 } 530 while (*arg != '\0'); 531 532 return 1; 533 } 534 535 536 /* Quick scan of the parameter list for options with global effect. */ 537 static error_t 538 parse_opt_1st (int key, char *arg, 539 struct argp_state *state __attribute__ ((unused))) 540 { 541 switch (key) 542 { 543 case 'B': 544 parse_B_option (arg); 545 break; 546 547 case 'c': 548 linker_script = arg; 549 break; 550 551 case 'E': 552 ld_state.export_all_dynamic = true; 553 break; 554 555 case 'G': 556 if (ld_state.file_type != no_file_type) 557 error (EXIT_FAILURE, 0, 558 gettext ("only one option of -G and -r is allowed")); 559 ld_state.file_type = dso_file_type; 560 561 /* If we generate a DSO we have to export all symbols. */ 562 ld_state.export_all_dynamic = true; 563 break; 564 565 case 'h': 566 ld_state.soname = arg; 567 break; 568 569 case 'i': 570 /* Discard the LD_LIBRARY_PATH value we found. */ 571 ld_library_path1 = NULL; 572 break; 573 574 case 'I': 575 ld_state.interp = arg; 576 break; 577 578 case 'm': 579 if (emulation != NULL) 580 error (EXIT_FAILURE, 0, gettext ("more than one '-m' parameter")); 581 emulation = arg; 582 break; 583 584 case 'Q': 585 if (arg[1] == '\0' && (arg[0] == 'y' || arg[0] == 'Y')) 586 ld_state.add_ld_comment = true; 587 else if (arg[1] == '\0' && (arg[0] == 'n' || arg[0] == 'N')) 588 ld_state.add_ld_comment = true; 589 else 590 error (EXIT_FAILURE, 0, gettext ("unknown option `-%c %s'"), 'Q', arg); 591 break; 592 593 case 'r': 594 if (ld_state.file_type != no_file_type) 595 error (EXIT_FAILURE, 0, 596 gettext ("only one option of -G and -r is allowed")); 597 ld_state.file_type = relocatable_file_type; 598 break; 599 600 case 'S': 601 ld_state.strip = strip_debug; 602 break; 603 604 case 't': 605 ld_state.trace_files = true; 606 break; 607 608 case 'v': 609 verbose = 1; 610 break; 611 612 case 'z': 613 /* The SysV linker used 'z' to pass various flags to the linker. 614 We follow this. See 'parse_z_option' for the options we 615 recognize. */ 616 parse_z_option (arg); 617 break; 618 619 case ARGP_pagesize: 620 { 621 char *endp; 622 ld_state.pagesize = strtoul (arg, &endp, 0); 623 if (*endp != '\0') 624 { 625 if (endp[1] == '\0' && tolower (*endp) == 'k') 626 ld_state.pagesize *= 1024; 627 else if (endp[1] == '\0' && tolower (*endp) == 'm') 628 ld_state.pagesize *= 1024 * 1024; 629 else 630 { 631 error (0, 0, 632 gettext ("invalid page size value '%s': ignored"), 633 arg); 634 ld_state.pagesize = 0; 635 } 636 } 637 } 638 break; 639 640 case 'R': 641 add_rxxpath (&ld_state.rpath, arg); 642 break; 643 644 case ARGP_rpath_link: 645 add_rxxpath (&ld_state.rpath_link, arg); 646 break; 647 648 case ARGP_runpath: 649 add_rxxpath (&ld_state.runpath, arg); 650 break; 651 652 case ARGP_runpath_link: 653 add_rxxpath (&ld_state.runpath_link, arg); 654 break; 655 656 case ARGP_gc_sections: 657 case ARGP_no_gc_sections: 658 ld_state.gc_sections = key == ARGP_gc_sections; 659 break; 660 661 case ARGP_eh_frame_hdr: 662 ld_state.eh_frame_hdr = true; 663 break; 664 665 case ARGP_hash_style: 666 if (strcmp (arg, "gnu") == 0) 667 ld_state.hash_style = hash_style_gnu; 668 else if (strcmp (arg, "both") == 0) 669 ld_state.hash_style = hash_style_gnu | hash_style_sysv; 670 else if (strcmp (arg, "sysv") == 0) 671 ld_state.hash_style = hash_style_sysv; 672 else 673 error (EXIT_FAILURE, 0, gettext ("invalid hash style '%s'"), arg); 674 break; 675 676 case ARGP_build_id: 677 if (arg == NULL) 678 ld_state.build_id = "sha1"; 679 else if (strcmp (arg, "uuid") != 0 680 && strcmp (arg, "md5") != 0 681 && strcmp (arg, "sha1") != 0 682 && !valid_hexarg (arg)) 683 error (EXIT_FAILURE, 0, gettext ("invalid build-ID style '%s'"), arg); 684 else 685 ld_state.build_id = arg; 686 break; 687 688 case 's': 689 if (arg == NULL) 690 { 691 if (ld_state.strip == strip_all) 692 ld_state.strip = strip_everything; 693 else 694 ld_state.strip = strip_all; 695 break; 696 } 697 /* FALLTHROUGH */ 698 699 case 'e': 700 case 'o': 701 case 'O': 702 case ARGP_whole_archive: 703 case ARGP_no_whole_archive: 704 case ARGP_as_needed: 705 case ARGP_no_as_needed: 706 case 'L': 707 case '(': 708 case ')': 709 case 'l': 710 case ARGP_static: 711 case ARGP_dynamic: 712 case ARGP_version_script: 713 /* We'll handle these in the second pass. */ 714 break; 715 716 case ARGP_KEY_ARG: 717 { 718 struct file_list *newp; 719 720 newp = (struct file_list *) xmalloc (sizeof (struct file_list)); 721 newp->name = arg; 722 #ifndef NDEBUG 723 newp->next = NULL; 724 #endif 725 CSNGL_LIST_ADD_REAR (input_file_list, newp); 726 } 727 break; 728 729 #if YYDEBUG 730 case ARGP_yydebug: 731 lddebug = 1; 732 break; 733 #endif 734 735 case ARGP_no_undefined: 736 ld_state.nodefs = false; 737 break; 738 739 case ARGP_conserve: 740 conserve_memory = 1; 741 break; 742 743 default: 744 return ARGP_ERR_UNKNOWN; 745 } 746 return 0; 747 } 748 749 750 /* Handle program arguments for real. */ 751 static error_t 752 parse_opt_2nd (int key, char *arg, 753 struct argp_state *state __attribute__ ((unused))) 754 { 755 static bool group_start_requested; 756 static bool group_end_requested; 757 758 switch (key) 759 { 760 case 'B': 761 parse_B_option_2 (arg); 762 break; 763 764 case 'e': 765 ld_state.entry = arg; 766 break; 767 768 case 'o': 769 if (ld_state.outfname != NULL) 770 { 771 error (0, 0, gettext ("More than one output file name given.")); 772 see_help: 773 argp_help (&argp_2nd, stderr, ARGP_HELP_SEE, "ld"); 774 exit (EXIT_FAILURE); 775 } 776 ld_state.outfname = arg; 777 break; 778 779 case 'O': 780 if (arg == NULL) 781 ld_state.optlevel = 1; 782 else 783 { 784 char *endp; 785 unsigned long int level = strtoul (arg, &endp, 10); 786 if (*endp != '\0') 787 { 788 error (0, 0, gettext ("Invalid optimization level `%s'"), arg); 789 goto see_help; 790 } 791 ld_state.optlevel = level; 792 } 793 break; 794 795 case ARGP_whole_archive: 796 ld_state.extract_rule = allextract; 797 break; 798 case ARGP_no_whole_archive: 799 ld_state.extract_rule = defaultextract; 800 break; 801 802 case ARGP_as_needed: 803 ld_state.as_needed = true; 804 break; 805 case ARGP_no_as_needed: 806 ld_state.as_needed = false; 807 break; 808 809 case ARGP_static: 810 case ARGP_dynamic: 811 /* Enable/disable use for DSOs. */ 812 ld_state.statically = key == ARGP_static; 813 break; 814 815 case 'z': 816 /* The SysV linker used 'z' to pass various flags to the linker. 817 We follow this. See 'parse_z_option' for the options we 818 recognize. */ 819 parse_z_option_2 (arg); 820 break; 821 822 case ARGP_version_script: 823 read_version_script (arg); 824 break; 825 826 case 'L': 827 /* Add a new search directory. */ 828 ld_new_searchdir (arg); 829 break; 830 831 case '(': 832 /* Start a link group. We have to be able to determine the object 833 file which is named next. Do this by remembering a pointer to 834 the pointer which will point to the next object. */ 835 if (verbose && (group_start_requested || !group_end_requested)) 836 error (0, 0, gettext ("nested -( -) groups are not allowed")); 837 838 /* Increment the nesting level. */ 839 ++group_level; 840 841 /* Record group start. */ 842 group_start_requested = true; 843 group_end_requested = false; 844 break; 845 846 case ')': 847 /* End a link group. If there is no group open this is clearly 848 a bug. If there is a group open insert a back reference 849 pointer in the record for the last object of the group. If 850 there is no new object or just one don't do anything. */ 851 if (!group_end_requested) 852 { 853 if (group_level == 0) 854 { 855 error (0, 0, gettext ("-) without matching -(")); 856 goto see_help; 857 } 858 } 859 else 860 last_file->group_end = true; 861 862 if (group_level > 0) 863 --group_level; 864 break; 865 866 case 'l': 867 case ARGP_KEY_ARG: 868 { 869 while (last_file != NULL) 870 /* Try to open the file. */ 871 error_loading |= FILE_PROCESS (-1, last_file, &ld_state, &last_file); 872 873 last_file = ld_new_inputfile (arg, 874 key == 'l' 875 ? archive_file_type 876 : relocatable_file_type); 877 if (group_start_requested) 878 { 879 last_file->group_start = true; 880 881 group_start_requested = false; 882 group_end_requested = true; 883 } 884 } 885 break; 886 887 default: 888 /* We can catch all other options here. They either have 889 already been handled or, if the parameter was not correct, 890 the error has been reported. */ 891 break; 892 } 893 return 0; 894 } 895 896 897 /* Load all the DSOs named as dependencies in other DSOs we already 898 loaded. */ 899 static void 900 load_needed (void) 901 { 902 struct usedfiles *first; 903 struct usedfiles *runp; 904 905 /* XXX There is one problem here: do we allow references from 906 regular object files to be satisfied by these implicit 907 dependencies? The old linker allows this and several libraries 908 depend on this. Solaris' linker does not allow this; it provides 909 the user with a comprehensive error message explaining the 910 situation. 911 912 XXX IMO the old ld behavior is correct since this is also how the 913 dynamic linker will work. It will look for unresolved references 914 in all loaded DSOs. 915 916 XXX Should we add an option to get Solaris compatibility? */ 917 if (ld_state.needed == NULL) 918 return; 919 920 runp = first = ld_state.needed->next; 921 do 922 { 923 struct usedfiles *ignore; 924 struct usedfiles *next = runp->next; 925 int err; 926 927 err = FILE_PROCESS (-1, runp, &ld_state, &ignore); 928 if (err != 0) 929 /* Something went wrong. */ 930 exit (err); 931 932 runp = next; 933 } 934 while (runp != first); 935 } 936 937 938 /* Print the version information. */ 939 static void 940 print_version (FILE *stream, struct argp_state *state __attribute__ ((unused))) 941 { 942 fprintf (stream, "ld (%s) %s\n", PACKAGE_NAME, PACKAGE_VERSION); 943 fprintf (stream, gettext ("\ 944 Copyright (C) %s Red Hat, Inc.\n\ 945 This is free software; see the source for copying conditions. There is NO\n\ 946 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ 947 "), "2012"); 948 fprintf (stream, gettext ("Written by %s.\n"), "Ulrich Drepper"); 949 } 950 951 952 /* There are a lot of -z options, parse them here. Some of them have 953 to be parsed in the first pass, others must be handled in the 954 second pass. */ 955 static void 956 parse_z_option (const char *arg) 957 { 958 if (strcmp (arg, "nodefaultlib") == 0 959 /* This is only meaningful if we create a DSO. */ 960 && ld_state.file_type == dso_file_type) 961 ld_state.dt_flags_1 |= DF_1_NODEFLIB; 962 else if (strcmp (arg, "muldefs") == 0) 963 ld_state.muldefs = true; 964 else if (strcmp (arg, "nodefs") == 0) 965 ld_state.nodefs = true; 966 else if (strcmp (arg, "defs") == 0) 967 ld_state.nodefs = false; 968 else if (strcmp (arg, "now") == 0) 969 /* We could also set the DF_1_NOW flag in DT_FLAGS_1 but this isn't 970 necessary. */ 971 ld_state.dt_flags |= DF_BIND_NOW; 972 else if (strcmp (arg, "origin") == 0) 973 /* We could also set the DF_1_ORIGIN flag in DT_FLAGS_1 but this isn't 974 necessary. */ 975 ld_state.dt_flags |= DF_ORIGIN; 976 else if (strcmp (arg, "nodelete") == 0 977 /* This is only meaningful if we create a DSO. */ 978 && ld_state.file_type == dso_file_type) 979 ld_state.dt_flags_1 |= DF_1_NODELETE; 980 else if (strcmp (arg, "initfirst") == 0) 981 ld_state.dt_flags_1 |= DF_1_INITFIRST; 982 else if (strcmp (arg, "nodlopen") == 0 983 /* This is only meaningful if we create a DSO. */ 984 && ld_state.file_type == dso_file_type) 985 ld_state.dt_flags_1 |= DF_1_NOOPEN; 986 else if (strcmp (arg, "systemlibrary") == 0) 987 ld_state.is_system_library = true; 988 else if (strcmp (arg, "execstack") == 0) 989 ld_state.execstack = execstack_true; 990 else if (strcmp (arg, "noexecstack") == 0) 991 ld_state.execstack = execstack_false_force; 992 else if (strcmp (arg, "allextract") != 0 993 && strcmp (arg, "defaultextract") != 0 994 && strcmp (arg, "weakextract") != 0 995 && strcmp (arg, "lazyload") != 0 996 && strcmp (arg, "nolazyload") != 0 997 && strcmp (arg, "ignore") != 0 998 && strcmp (arg, "record") != 0) 999 error (0, 0, gettext ("unknown option `-%c %s'"), 'z', arg); 1000 } 1001 1002 1003 static void 1004 parse_z_option_2 (const char *arg) 1005 { 1006 if (strcmp (arg, "allextract") == 0) 1007 ld_state.extract_rule = allextract; 1008 else if (strcmp (arg, "defaultextract") == 0) 1009 ld_state.extract_rule = defaultextract; 1010 else if (strcmp (arg, "weakextract") == 0) 1011 ld_state.extract_rule = weakextract; 1012 else if (strcmp (arg, "lazyload") == 0) 1013 ld_state.lazyload = true; 1014 else if (strcmp (arg, "nolazyload") == 0) 1015 ld_state.lazyload = false; 1016 else if (strcmp (arg, "ignore") == 0) 1017 ld_state.as_needed = true; 1018 else if (strcmp (arg, "record") == 0) 1019 ld_state.as_needed = false; 1020 } 1021 1022 1023 /* There are a lot of -B options, parse them here. */ 1024 static void 1025 parse_B_option (const char *arg) 1026 { 1027 if (strcmp (arg, "local") == 0) 1028 ld_state.default_bind_local = true; 1029 else if (strcmp (arg, "symbolic") != 0 1030 && strcmp (arg, "static") != 0 1031 && strcmp (arg, "dynamic") != 0) 1032 error (0, 0, gettext ("unknown option '-%c %s'"), 'B', arg); 1033 } 1034 1035 1036 /* The same functionality, but called in the second pass over the 1037 parameters. */ 1038 static void 1039 parse_B_option_2 (const char *arg) 1040 { 1041 if (strcmp (arg, "static") == 0) 1042 ld_state.statically = true; 1043 else if (strcmp (arg, "dynamic") == 0) 1044 ld_state.statically = false; 1045 else if (strcmp (arg, "symbolic") == 0 1046 /* This is only meaningful if we create a DSO. */ 1047 && ld_state.file_type == dso_file_type) 1048 ld_state.dt_flags |= DF_SYMBOLIC; 1049 } 1050 1051 1052 static inline int 1053 try (int fd, Elf *elf) 1054 { 1055 int result = 0; 1056 1057 if (elf == NULL) 1058 return 0; 1059 1060 if (elf_kind (elf) == ELF_K_ELF) 1061 { 1062 /* We have an ELF file. We now can find out 1063 what the output format should be. */ 1064 XElf_Ehdr_vardef(ehdr); 1065 1066 /* Get the ELF header of the object. */ 1067 xelf_getehdr (elf, ehdr); 1068 if (ehdr != NULL) 1069 ld_state.ebl = 1070 ebl_openbackend_machine (ehdr->e_machine); 1071 1072 result = 1; 1073 } 1074 else if (elf_kind (elf) == ELF_K_AR) 1075 { 1076 /* Try the archive members. This could 1077 potentially lead to wrong results if the 1078 archive contains files for more than one 1079 architecture. But this is the user's 1080 problem. */ 1081 Elf *subelf; 1082 Elf_Cmd cmd = ELF_C_READ_MMAP; 1083 1084 while ((subelf = elf_begin (fd, cmd, elf)) != NULL) 1085 { 1086 cmd = elf_next (subelf); 1087 1088 if (try (fd, subelf) != 0) 1089 break; 1090 } 1091 } 1092 1093 elf_end (elf); 1094 1095 return result; 1096 } 1097 1098 1099 static void 1100 determine_output_format (void) 1101 { 1102 /* First change the 'input_file_list' variable in a simple 1103 single-linked list. */ 1104 struct file_list *last = input_file_list; 1105 input_file_list = input_file_list->next; 1106 last->next = NULL; 1107 1108 /* Determine the target configuration which we are supposed to use. 1109 The user can use the '-m' option to select one. If this is 1110 missing we are trying to load one file and determine the 1111 architecture from that. */ 1112 if (emulation != NULL) 1113 { 1114 ld_state.ebl = ebl_openbackend_emulation (emulation); 1115 1116 assert (ld_state.ebl != NULL); 1117 } 1118 else 1119 { 1120 /* Find an ELF input file and let it determine the ELf backend. */ 1121 struct file_list *runp = input_file_list; 1122 1123 while (runp != NULL) 1124 { 1125 int fd = open (runp->name, O_RDONLY); 1126 if (fd != -1) 1127 { 1128 if (try (fd, elf_begin (fd, ELF_C_READ_MMAP, NULL)) != 0) 1129 /* Found a file. */ 1130 break; 1131 } 1132 1133 runp = runp->next; 1134 } 1135 1136 if (ld_state.ebl == NULL) 1137 { 1138 error (0, 0, gettext ("\ 1139 could not find input file to determine output file format")); 1140 error (EXIT_FAILURE, 0, gettext ("\ 1141 try again with an appropriate '-m' parameter")); 1142 } 1143 } 1144 1145 /* We don't need the list of input files anymore. The second run over 1146 the parameters will handle them. */ 1147 while (input_file_list != NULL) 1148 { 1149 struct file_list *oldp = input_file_list; 1150 input_file_list = input_file_list->next; 1151 free (oldp); 1152 } 1153 1154 /* We also know now what kind of file we are supposed to create. If 1155 the user hasn't selected anythign we create and executable. */ 1156 if (ld_state.file_type == no_file_type) 1157 ld_state.file_type = executable_file_type; 1158 } 1159 1160 /* Add DIR to the list of directories searched for object files and 1161 libraries. */ 1162 void 1163 ld_new_searchdir (const char *dir) 1164 { 1165 struct pathelement *newpath; 1166 1167 newpath = (struct pathelement *) 1168 obstack_calloc (&ld_state.smem, sizeof (struct pathelement)); 1169 1170 newpath->pname = dir; 1171 1172 /* Enqueue the file. */ 1173 if (ld_state.tailpaths == NULL) 1174 ld_state.paths = ld_state.tailpaths = newpath->next = newpath; 1175 else 1176 { 1177 ld_state.tailpaths->next = newpath; 1178 ld_state.tailpaths = newpath; 1179 newpath->next = ld_state.paths; 1180 } 1181 } 1182 1183 1184 struct usedfiles * 1185 ld_new_inputfile (const char *fname, enum file_type type) 1186 { 1187 struct usedfiles *newfile = (struct usedfiles *) 1188 obstack_calloc (&ld_state.smem, sizeof (struct usedfiles)); 1189 1190 newfile->soname = newfile->fname = newfile->rfname = fname; 1191 newfile->file_type = type; 1192 newfile->extract_rule = ld_state.extract_rule; 1193 newfile->as_needed = ld_state.as_needed; 1194 newfile->lazyload = ld_state.lazyload; 1195 newfile->status = not_opened; 1196 1197 return newfile; 1198 } 1199 1200 1201 /* Create an array listing all the sections. We will than pass this 1202 array to a system specific function which can reorder it at will. 1203 The functions can also merge sections if this is what is 1204 wanted. */ 1205 static void 1206 collect_sections (void) 1207 { 1208 void *p ; 1209 struct scnhead *h; 1210 size_t cnt; 1211 1212 /* We have that many sections. At least for now. */ 1213 ld_state.nallsections = ld_state.section_tab.filled; 1214 1215 /* Allocate the array. We allocate one more entry than computed so 1216 far since we might need a new section for the copy relocations. */ 1217 ld_state.allsections = 1218 (struct scnhead **) obstack_alloc (&ld_state.smem, 1219 (ld_state.nallsections + 1) 1220 * sizeof (struct scnhead *)); 1221 1222 /* Fill the array. We rely here on the hash table iterator to 1223 return the entries in the order they were added. */ 1224 cnt = 0; 1225 p = NULL; 1226 while ((h = ld_section_tab_iterate (&ld_state.section_tab, &p)) != NULL) 1227 { 1228 struct scninfo *runp; 1229 bool used = false; 1230 1231 if (h->kind == scn_normal) 1232 { 1233 runp = h->last; 1234 do 1235 { 1236 if (h->type == SHT_REL || h->type == SHT_RELA) 1237 { 1238 if (runp->used) 1239 /* This is a relocation section. If the section 1240 it is relocating is used in the result so must 1241 the relocation section. */ 1242 runp->used 1243 = runp->fileinfo->scninfo[SCNINFO_SHDR (runp->shdr).sh_info].used; 1244 } 1245 1246 /* Accumulate the result. */ 1247 used |= runp->used; 1248 1249 /* Next input section. */ 1250 runp = runp->next; 1251 } 1252 while (runp != h->last); 1253 1254 h->used = used; 1255 } 1256 1257 ld_state.allsections[cnt++] = h; 1258 } 1259 ld_state.nusedsections = cnt; 1260 1261 assert (cnt == ld_state.nallsections); 1262 } 1263 1264 1265 /* Add given path to the end of list. */ 1266 static void 1267 add_rxxpath (struct pathelement **pathp, const char *str) 1268 { 1269 struct pathelement *newp; 1270 1271 /* The path elements can in theory be freed after we read all the 1272 files. But the amount of memory we are talking about is small 1273 and the cost of free() calls is not neglectable. */ 1274 newp = (struct pathelement *) obstack_alloc (&ld_state.smem, sizeof (*newp)); 1275 newp->pname = str; 1276 newp->exist = 0; 1277 #ifndef NDEBUG 1278 newp->next = NULL; 1279 #endif 1280 1281 CSNGL_LIST_ADD_REAR (*pathp, newp); 1282 } 1283 1284 1285 /* Convert lists of possibly colon-separated directory lists into lists 1286 where each entry is for a single directory. */ 1287 static void 1288 normalize_dirlist (struct pathelement **pathp) 1289 { 1290 struct pathelement *firstp = *pathp; 1291 1292 do 1293 { 1294 const char *pname = (*pathp)->pname; 1295 const char *colonp = strchrnul (pname, ':'); 1296 1297 if (colonp != NULL) 1298 { 1299 struct pathelement *lastp = *pathp; 1300 struct pathelement *newp; 1301 1302 while (1) 1303 { 1304 if (colonp == pname) 1305 lastp->pname = "."; 1306 else 1307 lastp->pname = obstack_strndup (&ld_state.smem, pname, 1308 colonp - pname); 1309 1310 if (*colonp == '\0') 1311 break; 1312 pname = colonp + 1; 1313 1314 newp = (struct pathelement *) obstack_alloc (&ld_state.smem, 1315 sizeof (*newp)); 1316 newp->next = lastp->next; 1317 newp->exist = 0; 1318 lastp = lastp->next = newp; 1319 1320 colonp = strchrnul (pname, ':'); 1321 } 1322 1323 pathp = &lastp->next; 1324 } 1325 else 1326 pathp = &(*pathp)->next; 1327 } 1328 while (*pathp != firstp); 1329 } 1330 1331 1332 /* Called after all parameters are parsed to bring the runpath/rpath 1333 information into a usable form. */ 1334 static void 1335 gen_rxxpath_data (void) 1336 { 1337 char *ld_library_path2; 1338 1339 /* Convert the information in true single-linked lists for easy use. 1340 At this point we also discard the rpath information if runpath 1341 information is provided. rpath is deprecated and should not be 1342 used (or ever be invented for that matter). */ 1343 if (ld_state.rpath != NULL) 1344 { 1345 struct pathelement *endp = ld_state.rpath; 1346 ld_state.rpath = ld_state.rpath->next; 1347 endp->next = NULL; 1348 } 1349 if (ld_state.rpath_link != NULL) 1350 { 1351 struct pathelement *endp = ld_state.rpath_link; 1352 ld_state.rpath_link = ld_state.rpath_link->next; 1353 endp->next = NULL; 1354 } 1355 1356 if (ld_state.runpath != NULL) 1357 { 1358 struct pathelement *endp = ld_state.runpath; 1359 ld_state.runpath = ld_state.runpath->next; 1360 endp->next = NULL; 1361 1362 /* If rpath information is also available discard it. 1363 XXX Should there be a possibility to avoid this? */ 1364 while (ld_state.rpath != NULL) 1365 { 1366 struct pathelement *old = ld_state.rpath; 1367 ld_state.rpath = ld_state.rpath->next; 1368 free (old); 1369 } 1370 } 1371 if (ld_state.runpath_link != NULL) 1372 { 1373 struct pathelement *endp = ld_state.runpath_link; 1374 ld_state.runpath_link = ld_state.runpath_link->next; 1375 endp->next = NULL; 1376 1377 /* If rpath information is also available discard it. 1378 XXX Should there be a possibility to avoid this? */ 1379 while (ld_state.rpath_link != NULL) 1380 { 1381 struct pathelement *old = ld_state.rpath_link; 1382 ld_state.rpath_link = ld_state.rpath_link->next; 1383 free (old); 1384 } 1385 1386 /* The information in the strings in the list can actually be 1387 directory lists themselves, with entries separated by colons. 1388 Convert the list now to a list with one list entry for each 1389 directory. */ 1390 normalize_dirlist (&ld_state.runpath_link); 1391 } 1392 else if (ld_state.rpath_link != NULL) 1393 /* Same as for the runpath_link above. */ 1394 normalize_dirlist (&ld_state.rpath_link); 1395 1396 1397 /* As a related task, handle the LD_LIBRARY_PATH value here. First 1398 we have to possibly split the value found (if it contains a 1399 semicolon). Then we have to split the value in list of 1400 directories, i.e., split at the colons. */ 1401 if (ld_library_path1 != NULL) 1402 { 1403 ld_library_path2 = strchr (ld_library_path1, ';'); 1404 if (ld_library_path2 == NULL) 1405 { 1406 /* If no semicolon is present the directories are looked at 1407 after the -L parameters (-> ld_library_path2). */ 1408 ld_library_path2 = ld_library_path1; 1409 ld_library_path1 = NULL; 1410 } 1411 else 1412 { 1413 /* NUL terminate the first part. */ 1414 *ld_library_path2++ = '\0'; 1415 1416 /* Convert the string value in a list. */ 1417 add_rxxpath (&ld_state.ld_library_path1, ld_library_path1); 1418 normalize_dirlist (&ld_state.ld_library_path1); 1419 } 1420 1421 add_rxxpath (&ld_state.ld_library_path2, ld_library_path2); 1422 normalize_dirlist (&ld_state.ld_library_path2); 1423 } 1424 } 1425 1426 1427 static void 1428 read_version_script (const char *fname) 1429 { 1430 /* Open the file. The name is supposed to be the complete (relative 1431 or absolute) path. No search along a path will be performed. */ 1432 ldin = fopen (fname, "r"); 1433 if (ldin == NULL) 1434 error (EXIT_FAILURE, errno, gettext ("cannot read version script '%s'"), 1435 fname); 1436 /* No need for locking. */ 1437 __fsetlocking (ldin, FSETLOCKING_BYCALLER); 1438 1439 /* Tell the parser that this is a version script. */ 1440 ld_scan_version_script = 1; 1441 1442 ldlineno = 1; 1443 ldin_fname = fname; 1444 if (ldparse () != 0) 1445 /* Something went wrong during parsing. */ 1446 exit (EXIT_FAILURE); 1447 1448 fclose (ldin); 1449 } 1450 1451 1452 static void 1453 create_lscript_symbols (void) 1454 { 1455 /* Walk through the data from the linker script and generate all the 1456 symbols which are required to be present and those marked 1457 with PROVIDE if there is a undefined reference. */ 1458 if (ld_state.output_segments == NULL) 1459 return; 1460 1461 struct output_segment *segment = ld_state.output_segments->next; 1462 do 1463 { 1464 struct output_rule *orule; 1465 1466 for (orule = segment->output_rules; orule != NULL; orule = orule->next) 1467 if (orule->tag == output_assignment 1468 /* The assignments to "." (i.e., the PC) have to be 1469 ignored here. */ 1470 && strcmp (orule->val.assignment->variable, ".") != 0) 1471 { 1472 struct symbol *s = ld_state.unresolved; 1473 1474 /* Check whether the symbol is needed. */ 1475 if (likely (s != NULL)) 1476 { 1477 struct symbol *first = s; 1478 const char *providename = orule->val.assignment->variable; 1479 1480 /* Determine whether the provided symbol is still 1481 undefined. */ 1482 // XXX TODO Loop inside a loop. Gag! Must rewrite. */ 1483 do 1484 if (strcmp (s->name, providename) == 0) 1485 { 1486 /* Not defined but referenced. */ 1487 if (unlikely (!s->defined)) 1488 { 1489 /* Put on the list of symbols. First remove it from 1490 whatever list it currently is on. */ 1491 CDBL_LIST_DEL (ld_state.unresolved, s); 1492 --ld_state.nunresolved; 1493 goto use_it; 1494 } 1495 1496 if (unlikely (!orule->val.assignment->provide_flag)) 1497 { 1498 /* The symbol is already defined and now again 1499 in the linker script. This is an error. */ 1500 error (0, 0, gettext ("\ 1501 duplicate definition of '%s' in linker script"), 1502 providename); 1503 goto next_rule; 1504 } 1505 } 1506 while ((s = s->next) != first); 1507 } 1508 1509 /* If the symbol only has to be provided if it is needed, 1510 ignore it here since it is not undefined. */ 1511 if (orule->val.assignment->provide_flag) 1512 continue; 1513 1514 /* Allocate memory for this new symbol. */ 1515 s = (struct symbol *) 1516 obstack_calloc (&ld_state.smem, sizeof (struct symbol)); 1517 1518 /* Initialize it. */ 1519 s->name = orule->val.assignment->variable; 1520 1521 /* Insert it into the symbol hash table. */ 1522 unsigned long int hval = elf_hash (s->name); 1523 if (unlikely (ld_symbol_tab_insert (&ld_state.symbol_tab, 1524 hval, s) != 0)) 1525 { 1526 /* This means the symbol is defined somewhere else. 1527 Maybe it comes from a DSO or so. Get the 1528 definition. */ 1529 free (s); 1530 struct symbol *old = ld_symbol_tab_find (&ld_state.symbol_tab, 1531 hval, s); 1532 assert (old != NULL); 1533 free (s); 1534 1535 /* If this is a definition from the application itself 1536 this means a duplicate definition. */ 1537 if (! old->in_dso) 1538 { 1539 error (0, 0, gettext ("\ 1540 duplicate definition of '%s' in linker script"), 1541 s->name); 1542 goto next_rule; 1543 } 1544 1545 /* We use the definition from the linker script. */ 1546 s = old; 1547 } 1548 1549 use_it: 1550 /* The symbol is (now) defined. */ 1551 s->defined = 1; 1552 s->type = STT_NOTYPE; 1553 1554 /* Add a reference to the symbol record. We will come 1555 across it when creating the output file. */ 1556 orule->val.assignment->sym = s; 1557 1558 SNGL_LIST_PUSH (ld_state.lscript_syms, s); 1559 ++ld_state.nlscript_syms; 1560 1561 next_rule: 1562 ; 1563 } 1564 1565 segment = segment->next; 1566 } 1567 while (segment != ld_state.output_segments->next); 1568 } 1569 1570 1571 /* Create creation of spection section symbols representing sections in the 1572 output file. This is done for symbols like _GLOBAL_OFFSET_TABLE_ and 1573 _DYNAMIC. */ 1574 static void 1575 create_special_section_symbol (struct symbol **symp, const char *name) 1576 { 1577 if (*symp == NULL) 1578 { 1579 /* No symbol defined found yet. Create one. */ 1580 struct symbol *newsym = (struct symbol *) 1581 obstack_calloc (&ld_state.smem, sizeof (*newsym)); 1582 1583 newsym->name = name; 1584 // XXX Should we mark the symbol hidden? They are hardly useful 1585 // used outside the current object. 1586 1587 /* Add to the symbol table. */ 1588 if (unlikely (ld_symbol_tab_insert (&ld_state.symbol_tab, 1589 elf_hash (name), newsym) != 0)) 1590 abort (); 1591 1592 *symp = newsym; 1593 } 1594 else if ((*symp)->defined) 1595 /* Cannot happen. We do use this symbol from any input file. */ 1596 abort (); 1597 1598 (*symp)->defined = 1; 1599 (*symp)->local = 1; 1600 (*symp)->hidden = 1; 1601 (*symp)->type = STT_OBJECT; 1602 1603 ++ld_state.nsymtab; 1604 } 1605 1606 1607 #include "debugpred.h" 1608