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