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