1*56bb7041Schristos // options.c -- handle command line options for gold
2*56bb7041Schristos 
3*56bb7041Schristos // Copyright (C) 2006-2020 Free Software Foundation, Inc.
4*56bb7041Schristos // Written by Ian Lance Taylor <iant@google.com>.
5*56bb7041Schristos 
6*56bb7041Schristos // This file is part of gold.
7*56bb7041Schristos 
8*56bb7041Schristos // This program is free software; you can redistribute it and/or modify
9*56bb7041Schristos // it under the terms of the GNU General Public License as published by
10*56bb7041Schristos // the Free Software Foundation; either version 3 of the License, or
11*56bb7041Schristos // (at your option) any later version.
12*56bb7041Schristos 
13*56bb7041Schristos // This program is distributed in the hope that it will be useful,
14*56bb7041Schristos // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*56bb7041Schristos // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*56bb7041Schristos // GNU General Public License for more details.
17*56bb7041Schristos 
18*56bb7041Schristos // You should have received a copy of the GNU General Public License
19*56bb7041Schristos // along with this program; if not, write to the Free Software
20*56bb7041Schristos // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*56bb7041Schristos // MA 02110-1301, USA.
22*56bb7041Schristos 
23*56bb7041Schristos #include "gold.h"
24*56bb7041Schristos 
25*56bb7041Schristos #include <cerrno>
26*56bb7041Schristos #include <cstdlib>
27*56bb7041Schristos #include <cstring>
28*56bb7041Schristos #include <fstream>
29*56bb7041Schristos #include <vector>
30*56bb7041Schristos #include <iostream>
31*56bb7041Schristos #include <sys/stat.h>
32*56bb7041Schristos #include "filenames.h"
33*56bb7041Schristos #include "libiberty.h"
34*56bb7041Schristos #include "demangle.h"
35*56bb7041Schristos #include "../bfd/bfdver.h"
36*56bb7041Schristos 
37*56bb7041Schristos #include "debug.h"
38*56bb7041Schristos #include "script.h"
39*56bb7041Schristos #include "target-select.h"
40*56bb7041Schristos #include "options.h"
41*56bb7041Schristos #include "plugin.h"
42*56bb7041Schristos 
43*56bb7041Schristos namespace gold
44*56bb7041Schristos {
45*56bb7041Schristos 
46*56bb7041Schristos General_options
47*56bb7041Schristos Position_dependent_options::default_options_;
48*56bb7041Schristos 
49*56bb7041Schristos namespace options
50*56bb7041Schristos {
51*56bb7041Schristos 
52*56bb7041Schristos // This flag is TRUE if we should register the command-line options as they
53*56bb7041Schristos // are constructed.  It is set after construction of the options within
54*56bb7041Schristos // class Position_dependent_options.
55*56bb7041Schristos static bool ready_to_register = false;
56*56bb7041Schristos 
57*56bb7041Schristos // This global variable is set up as General_options is constructed.
58*56bb7041Schristos static std::vector<const One_option*> registered_options;
59*56bb7041Schristos 
60*56bb7041Schristos // These are set up at the same time -- the variables that accept one
61*56bb7041Schristos // dash, two, or require -z.  A single variable may be in more than
62*56bb7041Schristos // one of these data structures.
63*56bb7041Schristos typedef Unordered_map<std::string, One_option*> Option_map;
64*56bb7041Schristos static Option_map* long_options = NULL;
65*56bb7041Schristos static One_option* short_options[128];
66*56bb7041Schristos 
67*56bb7041Schristos void
register_option()68*56bb7041Schristos One_option::register_option()
69*56bb7041Schristos {
70*56bb7041Schristos   if (!ready_to_register)
71*56bb7041Schristos     return;
72*56bb7041Schristos 
73*56bb7041Schristos   registered_options.push_back(this);
74*56bb7041Schristos 
75*56bb7041Schristos   // We can't make long_options a static Option_map because we can't
76*56bb7041Schristos   // guarantee that will be initialized before register_option() is
77*56bb7041Schristos   // first called.
78*56bb7041Schristos   if (long_options == NULL)
79*56bb7041Schristos     long_options = new Option_map;
80*56bb7041Schristos 
81*56bb7041Schristos   // TWO_DASHES means that two dashes are preferred, but one is ok too.
82*56bb7041Schristos   if (!this->longname.empty())
83*56bb7041Schristos     (*long_options)[this->longname] = this;
84*56bb7041Schristos 
85*56bb7041Schristos   const int shortname_as_int = static_cast<int>(this->shortname);
86*56bb7041Schristos   gold_assert(shortname_as_int >= 0 && shortname_as_int < 128);
87*56bb7041Schristos   if (this->shortname != '\0')
88*56bb7041Schristos     {
89*56bb7041Schristos       gold_assert(short_options[shortname_as_int] == NULL);
90*56bb7041Schristos       short_options[shortname_as_int] = this;
91*56bb7041Schristos     }
92*56bb7041Schristos }
93*56bb7041Schristos 
94*56bb7041Schristos void
print() const95*56bb7041Schristos One_option::print() const
96*56bb7041Schristos {
97*56bb7041Schristos   bool comma = false;
98*56bb7041Schristos   printf("  ");
99*56bb7041Schristos   int len = 2;
100*56bb7041Schristos   if (this->shortname != '\0')
101*56bb7041Schristos     {
102*56bb7041Schristos       len += printf("-%c", this->shortname);
103*56bb7041Schristos       if (this->helparg)
104*56bb7041Schristos 	{
105*56bb7041Schristos 	  // -z takes long-names only.
106*56bb7041Schristos 	  gold_assert(this->dashes != DASH_Z);
107*56bb7041Schristos 	  len += printf(" %s", gettext(this->helparg));
108*56bb7041Schristos 	}
109*56bb7041Schristos       comma = true;
110*56bb7041Schristos     }
111*56bb7041Schristos   if (!this->longname.empty()
112*56bb7041Schristos       && !(this->longname[0] == this->shortname
113*56bb7041Schristos 	   && this->longname[1] == '\0'))
114*56bb7041Schristos     {
115*56bb7041Schristos       if (comma)
116*56bb7041Schristos 	len += printf(", ");
117*56bb7041Schristos       switch (this->dashes)
118*56bb7041Schristos 	{
119*56bb7041Schristos 	case options::ONE_DASH: case options::EXACTLY_ONE_DASH:
120*56bb7041Schristos 	  len += printf("-");
121*56bb7041Schristos 	  break;
122*56bb7041Schristos 	case options::TWO_DASHES: case options::EXACTLY_TWO_DASHES:
123*56bb7041Schristos 	  len += printf("--");
124*56bb7041Schristos 	  break;
125*56bb7041Schristos 	case options::DASH_Z:
126*56bb7041Schristos 	  len += printf("-z ");
127*56bb7041Schristos 	  break;
128*56bb7041Schristos 	default:
129*56bb7041Schristos 	  gold_unreachable();
130*56bb7041Schristos 	}
131*56bb7041Schristos       len += printf("%s", this->longname.c_str());
132*56bb7041Schristos       if (this->helparg)
133*56bb7041Schristos 	{
134*56bb7041Schristos 	  // For most options, we print "--frob FOO".  But for -z
135*56bb7041Schristos 	  // we print "-z frob=FOO".
136*56bb7041Schristos 	  len += printf("%c%s", this->dashes == options::DASH_Z ? '=' : ' ',
137*56bb7041Schristos 			gettext(this->helparg));
138*56bb7041Schristos 	}
139*56bb7041Schristos     }
140*56bb7041Schristos 
141*56bb7041Schristos   if (len >= 30)
142*56bb7041Schristos     {
143*56bb7041Schristos       printf("\n");
144*56bb7041Schristos       len = 0;
145*56bb7041Schristos     }
146*56bb7041Schristos   for (; len < 30; ++len)
147*56bb7041Schristos     std::putchar(' ');
148*56bb7041Schristos 
149*56bb7041Schristos   printf("%s", gettext(this->helpstring));
150*56bb7041Schristos   if (this->is_default)
151*56bb7041Schristos     printf(" (%s)", _("default"));
152*56bb7041Schristos   printf("\n");
153*56bb7041Schristos }
154*56bb7041Schristos 
155*56bb7041Schristos void
help()156*56bb7041Schristos help()
157*56bb7041Schristos {
158*56bb7041Schristos   printf(_("Usage: %s [options] file...\nOptions:\n"), gold::program_name);
159*56bb7041Schristos 
160*56bb7041Schristos   std::vector<const One_option*>::const_iterator it;
161*56bb7041Schristos   for (it = registered_options.begin(); it != registered_options.end(); ++it)
162*56bb7041Schristos     (*it)->print();
163*56bb7041Schristos 
164*56bb7041Schristos   // config.guess and libtool.m4 look in ld --help output for the
165*56bb7041Schristos   // string "supported targets".
166*56bb7041Schristos   printf(_("%s: supported targets:"), gold::program_name);
167*56bb7041Schristos   std::vector<const char*> supported_names;
168*56bb7041Schristos   gold::supported_target_names(&supported_names);
169*56bb7041Schristos   for (std::vector<const char*>::const_iterator p = supported_names.begin();
170*56bb7041Schristos        p != supported_names.end();
171*56bb7041Schristos        ++p)
172*56bb7041Schristos     printf(" %s", *p);
173*56bb7041Schristos   printf("\n");
174*56bb7041Schristos 
175*56bb7041Schristos   printf(_("%s: supported emulations:"), gold::program_name);
176*56bb7041Schristos   supported_names.clear();
177*56bb7041Schristos   gold::supported_emulation_names(&supported_names);
178*56bb7041Schristos   for (std::vector<const char*>::const_iterator p = supported_names.begin();
179*56bb7041Schristos        p != supported_names.end();
180*56bb7041Schristos        ++p)
181*56bb7041Schristos     printf(" %s", *p);
182*56bb7041Schristos   printf("\n");
183*56bb7041Schristos 
184*56bb7041Schristos   // REPORT_BUGS_TO is defined in bfd/bfdver.h.
185*56bb7041Schristos   const char* report = REPORT_BUGS_TO;
186*56bb7041Schristos   if (*report != '\0')
187*56bb7041Schristos     printf(_("Report bugs to %s\n"), report);
188*56bb7041Schristos }
189*56bb7041Schristos 
190*56bb7041Schristos // For bool, arg will be NULL (boolean options take no argument);
191*56bb7041Schristos // we always just set to true.
192*56bb7041Schristos void
parse_bool(const char *,const char *,bool * retval)193*56bb7041Schristos parse_bool(const char*, const char*, bool* retval)
194*56bb7041Schristos {
195*56bb7041Schristos   *retval = true;
196*56bb7041Schristos }
197*56bb7041Schristos 
198*56bb7041Schristos void
parse_uint(const char * option_name,const char * arg,int * retval)199*56bb7041Schristos parse_uint(const char* option_name, const char* arg, int* retval)
200*56bb7041Schristos {
201*56bb7041Schristos   char* endptr;
202*56bb7041Schristos   *retval = strtol(arg, &endptr, 0);
203*56bb7041Schristos   if (*endptr != '\0' || *retval < 0)
204*56bb7041Schristos     gold_fatal(_("%s: invalid option value (expected an integer): %s"),
205*56bb7041Schristos 	       option_name, arg);
206*56bb7041Schristos }
207*56bb7041Schristos 
208*56bb7041Schristos void
parse_int(const char * option_name,const char * arg,int * retval)209*56bb7041Schristos parse_int(const char* option_name, const char* arg, int* retval)
210*56bb7041Schristos {
211*56bb7041Schristos   char* endptr;
212*56bb7041Schristos   *retval = strtol(arg, &endptr, 0);
213*56bb7041Schristos   if (*endptr != '\0')
214*56bb7041Schristos     gold_fatal(_("%s: invalid option value (expected an integer): %s"),
215*56bb7041Schristos 	       option_name, arg);
216*56bb7041Schristos }
217*56bb7041Schristos 
218*56bb7041Schristos void
parse_uint64(const char * option_name,const char * arg,uint64_t * retval)219*56bb7041Schristos parse_uint64(const char* option_name, const char* arg, uint64_t* retval)
220*56bb7041Schristos {
221*56bb7041Schristos   char* endptr;
222*56bb7041Schristos   *retval = strtoull(arg, &endptr, 0);
223*56bb7041Schristos   if (*endptr != '\0')
224*56bb7041Schristos     gold_fatal(_("%s: invalid option value (expected an integer): %s"),
225*56bb7041Schristos 	       option_name, arg);
226*56bb7041Schristos }
227*56bb7041Schristos 
228*56bb7041Schristos void
parse_double(const char * option_name,const char * arg,double * retval)229*56bb7041Schristos parse_double(const char* option_name, const char* arg, double* retval)
230*56bb7041Schristos {
231*56bb7041Schristos   char* endptr;
232*56bb7041Schristos   *retval = strtod(arg, &endptr);
233*56bb7041Schristos   if (*endptr != '\0')
234*56bb7041Schristos     gold_fatal(_("%s: invalid option value "
235*56bb7041Schristos 		 "(expected a floating point number): %s"),
236*56bb7041Schristos 	       option_name, arg);
237*56bb7041Schristos }
238*56bb7041Schristos 
239*56bb7041Schristos void
parse_percent(const char * option_name,const char * arg,double * retval)240*56bb7041Schristos parse_percent(const char* option_name, const char* arg, double* retval)
241*56bb7041Schristos {
242*56bb7041Schristos   char* endptr;
243*56bb7041Schristos   *retval = strtod(arg, &endptr) / 100.0;
244*56bb7041Schristos   if (*endptr != '\0')
245*56bb7041Schristos     gold_fatal(_("%s: invalid option value "
246*56bb7041Schristos 		 "(expected a floating point number): %s"),
247*56bb7041Schristos 	       option_name, arg);
248*56bb7041Schristos }
249*56bb7041Schristos 
250*56bb7041Schristos void
parse_string(const char * option_name,const char * arg,const char ** retval)251*56bb7041Schristos parse_string(const char* option_name, const char* arg, const char** retval)
252*56bb7041Schristos {
253*56bb7041Schristos   if (*arg == '\0')
254*56bb7041Schristos     gold_fatal(_("%s: must take a non-empty argument"), option_name);
255*56bb7041Schristos   *retval = arg;
256*56bb7041Schristos }
257*56bb7041Schristos 
258*56bb7041Schristos void
parse_optional_string(const char *,const char * arg,const char ** retval)259*56bb7041Schristos parse_optional_string(const char*, const char* arg, const char** retval)
260*56bb7041Schristos {
261*56bb7041Schristos   *retval = arg;
262*56bb7041Schristos }
263*56bb7041Schristos 
264*56bb7041Schristos void
parse_dirlist(const char *,const char * arg,Dir_list * retval)265*56bb7041Schristos parse_dirlist(const char*, const char* arg, Dir_list* retval)
266*56bb7041Schristos {
267*56bb7041Schristos   retval->push_back(Search_directory(arg, false));
268*56bb7041Schristos }
269*56bb7041Schristos 
270*56bb7041Schristos void
parse_set(const char *,const char * arg,String_set * retval)271*56bb7041Schristos parse_set(const char*, const char* arg, String_set* retval)
272*56bb7041Schristos {
273*56bb7041Schristos   retval->insert(std::string(arg));
274*56bb7041Schristos }
275*56bb7041Schristos 
276*56bb7041Schristos void
parse_choices(const char * option_name,const char * arg,const char ** retval,const char * choices[],int num_choices)277*56bb7041Schristos parse_choices(const char* option_name, const char* arg, const char** retval,
278*56bb7041Schristos 	      const char* choices[], int num_choices)
279*56bb7041Schristos {
280*56bb7041Schristos   for (int i = 0; i < num_choices; i++)
281*56bb7041Schristos     if (strcmp(choices[i], arg) == 0)
282*56bb7041Schristos       {
283*56bb7041Schristos 	*retval = arg;
284*56bb7041Schristos 	return;
285*56bb7041Schristos       }
286*56bb7041Schristos 
287*56bb7041Schristos   // If we get here, the user did not enter a valid choice, so we die.
288*56bb7041Schristos   std::string choices_list;
289*56bb7041Schristos   for (int i = 0; i < num_choices; i++)
290*56bb7041Schristos     {
291*56bb7041Schristos       choices_list += choices[i];
292*56bb7041Schristos       if (i != num_choices - 1)
293*56bb7041Schristos 	choices_list += ", ";
294*56bb7041Schristos     }
295*56bb7041Schristos   gold_fatal(_("%s: must take one of the following arguments: %s"),
296*56bb7041Schristos 	     option_name, choices_list.c_str());
297*56bb7041Schristos }
298*56bb7041Schristos 
299*56bb7041Schristos } // End namespace options.
300*56bb7041Schristos 
301*56bb7041Schristos // Define the handler for "special" options (set via DEFINE_special).
302*56bb7041Schristos 
303*56bb7041Schristos void
parse_help(const char *,const char *,Command_line *)304*56bb7041Schristos General_options::parse_help(const char*, const char*, Command_line*)
305*56bb7041Schristos {
306*56bb7041Schristos   options::help();
307*56bb7041Schristos   ::exit(EXIT_SUCCESS);
308*56bb7041Schristos }
309*56bb7041Schristos 
310*56bb7041Schristos void
parse_version(const char * opt,const char *,Command_line *)311*56bb7041Schristos General_options::parse_version(const char* opt, const char*, Command_line*)
312*56bb7041Schristos {
313*56bb7041Schristos   bool print_short = (opt[0] == '-' && opt[1] == 'v');
314*56bb7041Schristos   gold::print_version(print_short);
315*56bb7041Schristos   this->printed_version_ = true;
316*56bb7041Schristos   if (!print_short)
317*56bb7041Schristos     ::exit(EXIT_SUCCESS);
318*56bb7041Schristos }
319*56bb7041Schristos 
320*56bb7041Schristos void
parse_V(const char *,const char *,Command_line *)321*56bb7041Schristos General_options::parse_V(const char*, const char*, Command_line*)
322*56bb7041Schristos {
323*56bb7041Schristos   gold::print_version(true);
324*56bb7041Schristos   this->printed_version_ = true;
325*56bb7041Schristos 
326*56bb7041Schristos   printf(_("  Supported targets:\n"));
327*56bb7041Schristos   std::vector<const char*> supported_names;
328*56bb7041Schristos   gold::supported_target_names(&supported_names);
329*56bb7041Schristos   for (std::vector<const char*>::const_iterator p = supported_names.begin();
330*56bb7041Schristos        p != supported_names.end();
331*56bb7041Schristos        ++p)
332*56bb7041Schristos     printf("   %s\n", *p);
333*56bb7041Schristos 
334*56bb7041Schristos   printf(_("  Supported emulations:\n"));
335*56bb7041Schristos   supported_names.clear();
336*56bb7041Schristos   gold::supported_emulation_names(&supported_names);
337*56bb7041Schristos   for (std::vector<const char*>::const_iterator p = supported_names.begin();
338*56bb7041Schristos        p != supported_names.end();
339*56bb7041Schristos        ++p)
340*56bb7041Schristos     printf("   %s\n", *p);
341*56bb7041Schristos }
342*56bb7041Schristos 
343*56bb7041Schristos void
parse_defsym(const char *,const char * arg,Command_line * cmdline)344*56bb7041Schristos General_options::parse_defsym(const char*, const char* arg,
345*56bb7041Schristos 			      Command_line* cmdline)
346*56bb7041Schristos {
347*56bb7041Schristos   cmdline->script_options().define_symbol(arg);
348*56bb7041Schristos }
349*56bb7041Schristos 
350*56bb7041Schristos void
parse_discard_all(const char *,const char *,Command_line *)351*56bb7041Schristos General_options::parse_discard_all(const char*, const char*,
352*56bb7041Schristos 				   Command_line*)
353*56bb7041Schristos {
354*56bb7041Schristos   this->discard_locals_ = DISCARD_ALL;
355*56bb7041Schristos }
356*56bb7041Schristos 
357*56bb7041Schristos void
parse_discard_locals(const char *,const char *,Command_line *)358*56bb7041Schristos General_options::parse_discard_locals(const char*, const char*,
359*56bb7041Schristos 				      Command_line*)
360*56bb7041Schristos {
361*56bb7041Schristos   this->discard_locals_ = DISCARD_LOCALS;
362*56bb7041Schristos }
363*56bb7041Schristos 
364*56bb7041Schristos void
parse_discard_none(const char *,const char *,Command_line *)365*56bb7041Schristos General_options::parse_discard_none(const char*, const char*,
366*56bb7041Schristos 				    Command_line*)
367*56bb7041Schristos {
368*56bb7041Schristos   this->discard_locals_ = DISCARD_NONE;
369*56bb7041Schristos }
370*56bb7041Schristos 
371*56bb7041Schristos void
parse_incremental(const char *,const char *,Command_line *)372*56bb7041Schristos General_options::parse_incremental(const char*, const char*,
373*56bb7041Schristos 				   Command_line*)
374*56bb7041Schristos {
375*56bb7041Schristos   this->incremental_mode_ = INCREMENTAL_AUTO;
376*56bb7041Schristos }
377*56bb7041Schristos 
378*56bb7041Schristos void
parse_no_incremental(const char *,const char *,Command_line *)379*56bb7041Schristos General_options::parse_no_incremental(const char*, const char*,
380*56bb7041Schristos 				      Command_line*)
381*56bb7041Schristos {
382*56bb7041Schristos   this->incremental_mode_ = INCREMENTAL_OFF;
383*56bb7041Schristos }
384*56bb7041Schristos 
385*56bb7041Schristos void
parse_incremental_full(const char *,const char *,Command_line *)386*56bb7041Schristos General_options::parse_incremental_full(const char*, const char*,
387*56bb7041Schristos 					Command_line*)
388*56bb7041Schristos {
389*56bb7041Schristos   this->incremental_mode_ = INCREMENTAL_FULL;
390*56bb7041Schristos }
391*56bb7041Schristos 
392*56bb7041Schristos void
parse_incremental_update(const char *,const char *,Command_line *)393*56bb7041Schristos General_options::parse_incremental_update(const char*, const char*,
394*56bb7041Schristos 					  Command_line*)
395*56bb7041Schristos {
396*56bb7041Schristos   this->incremental_mode_ = INCREMENTAL_UPDATE;
397*56bb7041Schristos }
398*56bb7041Schristos 
399*56bb7041Schristos void
parse_incremental_changed(const char *,const char *,Command_line *)400*56bb7041Schristos General_options::parse_incremental_changed(const char*, const char*,
401*56bb7041Schristos 					   Command_line*)
402*56bb7041Schristos {
403*56bb7041Schristos   this->implicit_incremental_ = true;
404*56bb7041Schristos   this->incremental_disposition_ = INCREMENTAL_CHANGED;
405*56bb7041Schristos }
406*56bb7041Schristos 
407*56bb7041Schristos void
parse_incremental_unchanged(const char *,const char *,Command_line *)408*56bb7041Schristos General_options::parse_incremental_unchanged(const char*, const char*,
409*56bb7041Schristos 					     Command_line*)
410*56bb7041Schristos {
411*56bb7041Schristos   this->implicit_incremental_ = true;
412*56bb7041Schristos   this->incremental_disposition_ = INCREMENTAL_UNCHANGED;
413*56bb7041Schristos }
414*56bb7041Schristos 
415*56bb7041Schristos void
parse_incremental_unknown(const char *,const char *,Command_line *)416*56bb7041Schristos General_options::parse_incremental_unknown(const char*, const char*,
417*56bb7041Schristos 					   Command_line*)
418*56bb7041Schristos {
419*56bb7041Schristos   this->implicit_incremental_ = true;
420*56bb7041Schristos   this->incremental_disposition_ = INCREMENTAL_CHECK;
421*56bb7041Schristos }
422*56bb7041Schristos 
423*56bb7041Schristos void
parse_incremental_startup_unchanged(const char *,const char *,Command_line *)424*56bb7041Schristos General_options::parse_incremental_startup_unchanged(const char*, const char*,
425*56bb7041Schristos 						     Command_line*)
426*56bb7041Schristos {
427*56bb7041Schristos   this->implicit_incremental_ = true;
428*56bb7041Schristos   this->incremental_startup_disposition_ = INCREMENTAL_UNCHANGED;
429*56bb7041Schristos }
430*56bb7041Schristos 
431*56bb7041Schristos void
parse_library(const char *,const char * arg,Command_line * cmdline)432*56bb7041Schristos General_options::parse_library(const char*, const char* arg,
433*56bb7041Schristos 			       Command_line* cmdline)
434*56bb7041Schristos {
435*56bb7041Schristos   Input_file_argument::Input_file_type type;
436*56bb7041Schristos   const char* name;
437*56bb7041Schristos   if (arg[0] == ':')
438*56bb7041Schristos     {
439*56bb7041Schristos       type = Input_file_argument::INPUT_FILE_TYPE_SEARCHED_FILE;
440*56bb7041Schristos       name = arg + 1;
441*56bb7041Schristos     }
442*56bb7041Schristos   else
443*56bb7041Schristos     {
444*56bb7041Schristos       type = Input_file_argument::INPUT_FILE_TYPE_LIBRARY;
445*56bb7041Schristos       name = arg;
446*56bb7041Schristos     }
447*56bb7041Schristos   Input_file_argument file(name, type, "", false, *this);
448*56bb7041Schristos   cmdline->inputs().add_file(file);
449*56bb7041Schristos }
450*56bb7041Schristos 
451*56bb7041Schristos void
parse_plugin(const char *,const char * arg,Command_line *)452*56bb7041Schristos General_options::parse_plugin(const char*, const char* arg,
453*56bb7041Schristos 			      Command_line*)
454*56bb7041Schristos {
455*56bb7041Schristos   this->add_plugin(arg);
456*56bb7041Schristos }
457*56bb7041Schristos 
458*56bb7041Schristos // Parse --plugin-opt.
459*56bb7041Schristos 
460*56bb7041Schristos void
parse_plugin_opt(const char *,const char * arg,Command_line *)461*56bb7041Schristos General_options::parse_plugin_opt(const char*, const char* arg,
462*56bb7041Schristos 				  Command_line*)
463*56bb7041Schristos {
464*56bb7041Schristos   this->add_plugin_option(arg);
465*56bb7041Schristos }
466*56bb7041Schristos 
467*56bb7041Schristos void
parse_no_power10_stubs(const char *,const char *,Command_line *)468*56bb7041Schristos General_options::parse_no_power10_stubs(const char*, const char*,
469*56bb7041Schristos 					Command_line*)
470*56bb7041Schristos {
471*56bb7041Schristos   this->set_power10_stubs("no");
472*56bb7041Schristos   this->set_user_set_power10_stubs();
473*56bb7041Schristos }
474*56bb7041Schristos 
475*56bb7041Schristos void
parse_R(const char * option,const char * arg,Command_line * cmdline)476*56bb7041Schristos General_options::parse_R(const char* option, const char* arg,
477*56bb7041Schristos 			 Command_line* cmdline)
478*56bb7041Schristos {
479*56bb7041Schristos   struct stat s;
480*56bb7041Schristos   if (::stat(arg, &s) != 0 || S_ISDIR(s.st_mode))
481*56bb7041Schristos     this->add_to_rpath(arg);
482*56bb7041Schristos   else
483*56bb7041Schristos     this->parse_just_symbols(option, arg, cmdline);
484*56bb7041Schristos }
485*56bb7041Schristos 
486*56bb7041Schristos void
parse_just_symbols(const char *,const char * arg,Command_line * cmdline)487*56bb7041Schristos General_options::parse_just_symbols(const char*, const char* arg,
488*56bb7041Schristos 				    Command_line* cmdline)
489*56bb7041Schristos {
490*56bb7041Schristos   Input_file_argument file(arg, Input_file_argument::INPUT_FILE_TYPE_FILE,
491*56bb7041Schristos 			   "", true, *this);
492*56bb7041Schristos   cmdline->inputs().add_file(file);
493*56bb7041Schristos }
494*56bb7041Schristos 
495*56bb7041Schristos // Handle --section-start.
496*56bb7041Schristos 
497*56bb7041Schristos void
parse_section_start(const char *,const char * arg,Command_line *)498*56bb7041Schristos General_options::parse_section_start(const char*, const char* arg,
499*56bb7041Schristos 				     Command_line*)
500*56bb7041Schristos {
501*56bb7041Schristos   const char* eq = strchr(arg, '=');
502*56bb7041Schristos   if (eq == NULL)
503*56bb7041Schristos     {
504*56bb7041Schristos       gold_error(_("invalid argument to --section-start; "
505*56bb7041Schristos 		   "must be SECTION=ADDRESS"));
506*56bb7041Schristos       return;
507*56bb7041Schristos     }
508*56bb7041Schristos 
509*56bb7041Schristos   std::string section_name(arg, eq - arg);
510*56bb7041Schristos 
511*56bb7041Schristos   ++eq;
512*56bb7041Schristos   const char* val_start = eq;
513*56bb7041Schristos   if (eq[0] == '0' && (eq[1] == 'x' || eq[1] == 'X'))
514*56bb7041Schristos     eq += 2;
515*56bb7041Schristos   if (*eq == '\0')
516*56bb7041Schristos     {
517*56bb7041Schristos       gold_error(_("--section-start address missing"));
518*56bb7041Schristos       return;
519*56bb7041Schristos     }
520*56bb7041Schristos   uint64_t addr = 0;
521*56bb7041Schristos   hex_init();
522*56bb7041Schristos   for (; *eq != '\0'; ++eq)
523*56bb7041Schristos     {
524*56bb7041Schristos       if (!hex_p(*eq))
525*56bb7041Schristos 	{
526*56bb7041Schristos 	  gold_error(_("--section-start argument %s is not a valid hex number"),
527*56bb7041Schristos 		     val_start);
528*56bb7041Schristos 	  return;
529*56bb7041Schristos 	}
530*56bb7041Schristos       addr <<= 4;
531*56bb7041Schristos       addr += hex_value(*eq);
532*56bb7041Schristos     }
533*56bb7041Schristos 
534*56bb7041Schristos   this->section_starts_[section_name] = addr;
535*56bb7041Schristos }
536*56bb7041Schristos 
537*56bb7041Schristos // Look up a --section-start value.
538*56bb7041Schristos 
539*56bb7041Schristos bool
section_start(const char * secname,uint64_t * paddr) const540*56bb7041Schristos General_options::section_start(const char* secname, uint64_t* paddr) const
541*56bb7041Schristos {
542*56bb7041Schristos   if (this->section_starts_.empty())
543*56bb7041Schristos     return false;
544*56bb7041Schristos   std::map<std::string, uint64_t>::const_iterator p =
545*56bb7041Schristos     this->section_starts_.find(secname);
546*56bb7041Schristos   if (p == this->section_starts_.end())
547*56bb7041Schristos     return false;
548*56bb7041Schristos   *paddr = p->second;
549*56bb7041Schristos   return true;
550*56bb7041Schristos }
551*56bb7041Schristos 
552*56bb7041Schristos void
parse_static(const char *,const char *,Command_line *)553*56bb7041Schristos General_options::parse_static(const char*, const char*, Command_line*)
554*56bb7041Schristos {
555*56bb7041Schristos   this->set_static(true);
556*56bb7041Schristos }
557*56bb7041Schristos 
558*56bb7041Schristos void
parse_script(const char *,const char * arg,Command_line * cmdline)559*56bb7041Schristos General_options::parse_script(const char*, const char* arg,
560*56bb7041Schristos 			      Command_line* cmdline)
561*56bb7041Schristos {
562*56bb7041Schristos   if (!read_commandline_script(arg, cmdline))
563*56bb7041Schristos     gold::gold_fatal(_("unable to parse script file %s"), arg);
564*56bb7041Schristos }
565*56bb7041Schristos 
566*56bb7041Schristos void
parse_version_script(const char *,const char * arg,Command_line * cmdline)567*56bb7041Schristos General_options::parse_version_script(const char*, const char* arg,
568*56bb7041Schristos 				      Command_line* cmdline)
569*56bb7041Schristos {
570*56bb7041Schristos   if (!read_version_script(arg, cmdline))
571*56bb7041Schristos     gold::gold_fatal(_("unable to parse version script file %s"), arg);
572*56bb7041Schristos }
573*56bb7041Schristos 
574*56bb7041Schristos void
parse_dynamic_list(const char *,const char * arg,Command_line * cmdline)575*56bb7041Schristos General_options::parse_dynamic_list(const char*, const char* arg,
576*56bb7041Schristos 				    Command_line* cmdline)
577*56bb7041Schristos {
578*56bb7041Schristos   if (!read_dynamic_list(arg, cmdline, &this->dynamic_list_))
579*56bb7041Schristos     gold::gold_fatal(_("unable to parse dynamic-list script file %s"), arg);
580*56bb7041Schristos   this->have_dynamic_list_ = true;
581*56bb7041Schristos }
582*56bb7041Schristos 
583*56bb7041Schristos void
parse_start_group(const char *,const char *,Command_line * cmdline)584*56bb7041Schristos General_options::parse_start_group(const char*, const char*,
585*56bb7041Schristos 				   Command_line* cmdline)
586*56bb7041Schristos {
587*56bb7041Schristos   cmdline->inputs().start_group();
588*56bb7041Schristos }
589*56bb7041Schristos 
590*56bb7041Schristos void
parse_end_group(const char *,const char *,Command_line * cmdline)591*56bb7041Schristos General_options::parse_end_group(const char*, const char*,
592*56bb7041Schristos 				 Command_line* cmdline)
593*56bb7041Schristos {
594*56bb7041Schristos   cmdline->inputs().end_group();
595*56bb7041Schristos }
596*56bb7041Schristos 
597*56bb7041Schristos void
parse_start_lib(const char *,const char *,Command_line * cmdline)598*56bb7041Schristos General_options::parse_start_lib(const char*, const char*,
599*56bb7041Schristos 				 Command_line* cmdline)
600*56bb7041Schristos {
601*56bb7041Schristos   cmdline->inputs().start_lib(cmdline->position_dependent_options());
602*56bb7041Schristos }
603*56bb7041Schristos 
604*56bb7041Schristos void
parse_end_lib(const char *,const char *,Command_line * cmdline)605*56bb7041Schristos General_options::parse_end_lib(const char*, const char*,
606*56bb7041Schristos 			       Command_line* cmdline)
607*56bb7041Schristos {
608*56bb7041Schristos   cmdline->inputs().end_lib();
609*56bb7041Schristos }
610*56bb7041Schristos 
611*56bb7041Schristos // The function add_excluded_libs() in ld/ldlang.c of GNU ld breaks up a list
612*56bb7041Schristos // of names separated by commas or colons and puts them in a linked list.
613*56bb7041Schristos // We implement the same parsing of names here but store names in an unordered
614*56bb7041Schristos // map to speed up searching of names.
615*56bb7041Schristos 
616*56bb7041Schristos void
parse_exclude_libs(const char *,const char * arg,Command_line *)617*56bb7041Schristos General_options::parse_exclude_libs(const char*, const char* arg,
618*56bb7041Schristos 				    Command_line*)
619*56bb7041Schristos {
620*56bb7041Schristos   const char* p = arg;
621*56bb7041Schristos 
622*56bb7041Schristos   while (*p != '\0')
623*56bb7041Schristos     {
624*56bb7041Schristos       size_t length = strcspn(p, ",:");
625*56bb7041Schristos       this->excluded_libs_.insert(std::string(p, length));
626*56bb7041Schristos       p += (p[length] ? length + 1 : length);
627*56bb7041Schristos     }
628*56bb7041Schristos }
629*56bb7041Schristos 
630*56bb7041Schristos // The checking logic is based on the function check_excluded_libs() in
631*56bb7041Schristos // ld/ldlang.c of GNU ld but our implementation is different because we use
632*56bb7041Schristos // an unordered map instead of a linked list, which is what GNU ld uses.  GNU
633*56bb7041Schristos // ld searches sequentially in the excluded libs list.  For a given archive,
634*56bb7041Schristos // a match is found if the archive's name matches exactly one of the list
635*56bb7041Schristos // entry or if the archive's name is of the form FOO.a and FOO matches exactly
636*56bb7041Schristos // one of the list entry.  An entry "ALL" in the list is considered as a
637*56bb7041Schristos // wild-card and matches any given name.
638*56bb7041Schristos 
639*56bb7041Schristos bool
check_excluded_libs(const std::string & name) const640*56bb7041Schristos General_options::check_excluded_libs(const std::string &name) const
641*56bb7041Schristos {
642*56bb7041Schristos   Unordered_set<std::string>::const_iterator p;
643*56bb7041Schristos 
644*56bb7041Schristos   // Exit early for the most common case.
645*56bb7041Schristos   if (excluded_libs_.empty())
646*56bb7041Schristos     return false;
647*56bb7041Schristos 
648*56bb7041Schristos   // If we see "ALL", all archives are excluded from automatic export.
649*56bb7041Schristos   p = excluded_libs_.find(std::string("ALL"));
650*56bb7041Schristos   if (p != excluded_libs_.end())
651*56bb7041Schristos     return true;
652*56bb7041Schristos 
653*56bb7041Schristos   // First strip off any directories in name.
654*56bb7041Schristos   const char* basename = lbasename(name.c_str());
655*56bb7041Schristos 
656*56bb7041Schristos   // Try finding an exact match.
657*56bb7041Schristos   p = excluded_libs_.find(std::string(basename));
658*56bb7041Schristos   if (p != excluded_libs_.end())
659*56bb7041Schristos     return true;
660*56bb7041Schristos 
661*56bb7041Schristos   // Try matching NAME without ".a" at the end.
662*56bb7041Schristos   size_t length = strlen(basename);
663*56bb7041Schristos   if ((length >= 2)
664*56bb7041Schristos       && (basename[length - 2] == '.')
665*56bb7041Schristos       && (basename[length - 1] == 'a'))
666*56bb7041Schristos     {
667*56bb7041Schristos       p = excluded_libs_.find(std::string(basename, length - 2));
668*56bb7041Schristos       if (p != excluded_libs_.end())
669*56bb7041Schristos 	return true;
670*56bb7041Schristos     }
671*56bb7041Schristos 
672*56bb7041Schristos   return false;
673*56bb7041Schristos }
674*56bb7041Schristos 
675*56bb7041Schristos // Recognize input and output target names.  The GNU linker accepts
676*56bb7041Schristos // these with --format and --oformat.  This code is intended to be
677*56bb7041Schristos // minimally compatible.  In practice for an ELF target this would be
678*56bb7041Schristos // the same target as the input files; that name always start with
679*56bb7041Schristos // "elf".  Non-ELF targets would be "srec", "symbolsrec", "tekhex",
680*56bb7041Schristos // "binary", "ihex".
681*56bb7041Schristos 
682*56bb7041Schristos General_options::Object_format
string_to_object_format(const char * arg)683*56bb7041Schristos General_options::string_to_object_format(const char* arg)
684*56bb7041Schristos {
685*56bb7041Schristos   if (strncmp(arg, "elf", 3) == 0 || strcmp(arg, "default") == 0)
686*56bb7041Schristos     return gold::General_options::OBJECT_FORMAT_ELF;
687*56bb7041Schristos   else if (strcmp(arg, "binary") == 0)
688*56bb7041Schristos     return gold::General_options::OBJECT_FORMAT_BINARY;
689*56bb7041Schristos   else
690*56bb7041Schristos     {
691*56bb7041Schristos       gold::gold_error(_("format '%s' not supported; treating as elf "
692*56bb7041Schristos 			 "(supported formats: elf, binary)"),
693*56bb7041Schristos 		       arg);
694*56bb7041Schristos       return gold::General_options::OBJECT_FORMAT_ELF;
695*56bb7041Schristos     }
696*56bb7041Schristos }
697*56bb7041Schristos 
698*56bb7041Schristos const char*
object_format_to_string(General_options::Object_format fmt)699*56bb7041Schristos General_options::object_format_to_string(General_options::Object_format fmt)
700*56bb7041Schristos {
701*56bb7041Schristos   switch (fmt)
702*56bb7041Schristos     {
703*56bb7041Schristos     case General_options::OBJECT_FORMAT_ELF:
704*56bb7041Schristos       return "elf";
705*56bb7041Schristos     case General_options::OBJECT_FORMAT_BINARY:
706*56bb7041Schristos       return "binary";
707*56bb7041Schristos     default:
708*56bb7041Schristos       gold_unreachable();
709*56bb7041Schristos     }
710*56bb7041Schristos }
711*56bb7041Schristos 
712*56bb7041Schristos void
parse_fix_v4bx(const char *,const char *,Command_line *)713*56bb7041Schristos General_options::parse_fix_v4bx(const char*, const char*,
714*56bb7041Schristos 				Command_line*)
715*56bb7041Schristos {
716*56bb7041Schristos   this->fix_v4bx_ = FIX_V4BX_REPLACE;
717*56bb7041Schristos }
718*56bb7041Schristos 
719*56bb7041Schristos void
parse_fix_v4bx_interworking(const char *,const char *,Command_line *)720*56bb7041Schristos General_options::parse_fix_v4bx_interworking(const char*, const char*,
721*56bb7041Schristos 					     Command_line*)
722*56bb7041Schristos {
723*56bb7041Schristos   this->fix_v4bx_ = FIX_V4BX_INTERWORKING;
724*56bb7041Schristos }
725*56bb7041Schristos 
726*56bb7041Schristos void
parse_EB(const char *,const char *,Command_line *)727*56bb7041Schristos General_options::parse_EB(const char*, const char*, Command_line*)
728*56bb7041Schristos {
729*56bb7041Schristos   this->endianness_ = ENDIANNESS_BIG;
730*56bb7041Schristos }
731*56bb7041Schristos 
732*56bb7041Schristos void
parse_EL(const char *,const char *,Command_line *)733*56bb7041Schristos General_options::parse_EL(const char*, const char*, Command_line*)
734*56bb7041Schristos {
735*56bb7041Schristos   this->endianness_ = ENDIANNESS_LITTLE;
736*56bb7041Schristos }
737*56bb7041Schristos 
738*56bb7041Schristos void
copy_from_posdep_options(const Position_dependent_options & posdep)739*56bb7041Schristos General_options::copy_from_posdep_options(
740*56bb7041Schristos     const Position_dependent_options& posdep)
741*56bb7041Schristos {
742*56bb7041Schristos   this->set_as_needed(posdep.as_needed());
743*56bb7041Schristos   this->set_Bdynamic(posdep.Bdynamic());
744*56bb7041Schristos   this->set_format(
745*56bb7041Schristos       General_options::object_format_to_string(posdep.format_enum()));
746*56bb7041Schristos   this->set_whole_archive(posdep.whole_archive());
747*56bb7041Schristos   this->set_incremental_disposition(posdep.incremental_disposition());
748*56bb7041Schristos }
749*56bb7041Schristos 
750*56bb7041Schristos void
parse_push_state(const char *,const char *,Command_line *)751*56bb7041Schristos General_options::parse_push_state(const char*, const char*, Command_line*)
752*56bb7041Schristos {
753*56bb7041Schristos   Position_dependent_options* posdep = new Position_dependent_options(*this);
754*56bb7041Schristos   this->options_stack_.push_back(posdep);
755*56bb7041Schristos }
756*56bb7041Schristos 
757*56bb7041Schristos void
parse_pop_state(const char *,const char *,Command_line *)758*56bb7041Schristos General_options::parse_pop_state(const char*, const char*, Command_line*)
759*56bb7041Schristos {
760*56bb7041Schristos   if (this->options_stack_.empty())
761*56bb7041Schristos     {
762*56bb7041Schristos       gold::gold_error(_("unbalanced --push-state/--pop-state"));
763*56bb7041Schristos       return;
764*56bb7041Schristos     }
765*56bb7041Schristos   Position_dependent_options* posdep = this->options_stack_.back();
766*56bb7041Schristos   this->options_stack_.pop_back();
767*56bb7041Schristos   this->copy_from_posdep_options(*posdep);
768*56bb7041Schristos   delete posdep;
769*56bb7041Schristos }
770*56bb7041Schristos 
771*56bb7041Schristos } // End namespace gold.
772*56bb7041Schristos 
773*56bb7041Schristos namespace
774*56bb7041Schristos {
775*56bb7041Schristos 
776*56bb7041Schristos void
usage()777*56bb7041Schristos usage()
778*56bb7041Schristos {
779*56bb7041Schristos   fprintf(stderr,
780*56bb7041Schristos 	  _("%s: use the --help option for usage information\n"),
781*56bb7041Schristos 	  gold::program_name);
782*56bb7041Schristos   ::exit(EXIT_FAILURE);
783*56bb7041Schristos }
784*56bb7041Schristos 
785*56bb7041Schristos void
usage(const char * msg,const char * opt)786*56bb7041Schristos usage(const char* msg, const char* opt)
787*56bb7041Schristos {
788*56bb7041Schristos   fprintf(stderr,
789*56bb7041Schristos 	  _("%s: %s: %s\n"),
790*56bb7041Schristos 	  gold::program_name, opt, msg);
791*56bb7041Schristos   usage();
792*56bb7041Schristos }
793*56bb7041Schristos 
794*56bb7041Schristos // If the default sysroot is relocatable, try relocating it based on
795*56bb7041Schristos // the prefix FROM.
796*56bb7041Schristos 
797*56bb7041Schristos static char*
get_relative_sysroot(const char * from)798*56bb7041Schristos get_relative_sysroot(const char* from)
799*56bb7041Schristos {
800*56bb7041Schristos   char* path = make_relative_prefix(gold::program_name, from,
801*56bb7041Schristos 				    TARGET_SYSTEM_ROOT);
802*56bb7041Schristos   if (path != NULL)
803*56bb7041Schristos     {
804*56bb7041Schristos       struct stat s;
805*56bb7041Schristos       if (::stat(path, &s) == 0 && S_ISDIR(s.st_mode))
806*56bb7041Schristos 	return path;
807*56bb7041Schristos       free(path);
808*56bb7041Schristos     }
809*56bb7041Schristos 
810*56bb7041Schristos   return NULL;
811*56bb7041Schristos }
812*56bb7041Schristos 
813*56bb7041Schristos // Return the default sysroot.  This is set by the --with-sysroot
814*56bb7041Schristos // option to configure.  Note we do not free the return value of
815*56bb7041Schristos // get_relative_sysroot, which is a small memory leak, but is
816*56bb7041Schristos // necessary since we store this pointer directly in General_options.
817*56bb7041Schristos 
818*56bb7041Schristos static const char*
get_default_sysroot()819*56bb7041Schristos get_default_sysroot()
820*56bb7041Schristos {
821*56bb7041Schristos   const char* sysroot = TARGET_SYSTEM_ROOT;
822*56bb7041Schristos   if (*sysroot == '\0')
823*56bb7041Schristos     return NULL;
824*56bb7041Schristos 
825*56bb7041Schristos   if (TARGET_SYSTEM_ROOT_RELOCATABLE)
826*56bb7041Schristos     {
827*56bb7041Schristos       char* path = get_relative_sysroot(BINDIR);
828*56bb7041Schristos       if (path == NULL)
829*56bb7041Schristos 	path = get_relative_sysroot(TOOLBINDIR);
830*56bb7041Schristos       if (path != NULL)
831*56bb7041Schristos 	return path;
832*56bb7041Schristos     }
833*56bb7041Schristos 
834*56bb7041Schristos   return sysroot;
835*56bb7041Schristos }
836*56bb7041Schristos 
837*56bb7041Schristos // Parse a long option.  Such options have the form
838*56bb7041Schristos // <-|--><option>[=arg].  If "=arg" is not present but the option
839*56bb7041Schristos // takes an argument, the next word is taken to the be the argument.
840*56bb7041Schristos // If equals_only is set, then only the <option>=<arg> form is
841*56bb7041Schristos // accepted, not the <option><space><arg> form.  Returns a One_option
842*56bb7041Schristos // struct or NULL if argv[i] cannot be parsed as a long option.  In
843*56bb7041Schristos // the not-NULL case, *arg is set to the option's argument (NULL if
844*56bb7041Schristos // the option takes no argument), and *i is advanced past this option.
845*56bb7041Schristos // NOTE: it is safe for argv and arg to point to the same place.
846*56bb7041Schristos gold::options::One_option*
parse_long_option(int argc,const char ** argv,bool equals_only,const char ** arg,int * i)847*56bb7041Schristos parse_long_option(int argc, const char** argv, bool equals_only,
848*56bb7041Schristos 		  const char** arg, int* i)
849*56bb7041Schristos {
850*56bb7041Schristos   const char* const this_argv = argv[*i];
851*56bb7041Schristos 
852*56bb7041Schristos   const char* equals = strchr(this_argv, '=');
853*56bb7041Schristos   const char* option_start = this_argv + strspn(this_argv, "-");
854*56bb7041Schristos   std::string option(option_start,
855*56bb7041Schristos 		     equals ? equals - option_start : strlen(option_start));
856*56bb7041Schristos 
857*56bb7041Schristos   gold::options::Option_map::iterator it
858*56bb7041Schristos       = gold::options::long_options->find(option);
859*56bb7041Schristos   if (it == gold::options::long_options->end())
860*56bb7041Schristos     return NULL;
861*56bb7041Schristos 
862*56bb7041Schristos   gold::options::One_option* retval = it->second;
863*56bb7041Schristos 
864*56bb7041Schristos   // If the dash-count doesn't match, we fail.
865*56bb7041Schristos   if (this_argv[0] != '-')  // no dashes at all: had better be "-z <longopt>"
866*56bb7041Schristos     {
867*56bb7041Schristos       if (retval->dashes != gold::options::DASH_Z)
868*56bb7041Schristos 	return NULL;
869*56bb7041Schristos     }
870*56bb7041Schristos   else if (this_argv[1] != '-')   // one dash
871*56bb7041Schristos     {
872*56bb7041Schristos       if (retval->dashes != gold::options::ONE_DASH
873*56bb7041Schristos 	  && retval->dashes != gold::options::EXACTLY_ONE_DASH
874*56bb7041Schristos 	  && retval->dashes != gold::options::TWO_DASHES)
875*56bb7041Schristos 	return NULL;
876*56bb7041Schristos     }
877*56bb7041Schristos   else                            // two dashes (or more!)
878*56bb7041Schristos     {
879*56bb7041Schristos       if (retval->dashes != gold::options::TWO_DASHES
880*56bb7041Schristos 	  && retval->dashes != gold::options::EXACTLY_TWO_DASHES
881*56bb7041Schristos 	  && retval->dashes != gold::options::ONE_DASH)
882*56bb7041Schristos 	return NULL;
883*56bb7041Schristos     }
884*56bb7041Schristos 
885*56bb7041Schristos   // Now that we know the option is good (or else bad in a way that
886*56bb7041Schristos   // will cause us to die), increment i to point past this argv.
887*56bb7041Schristos   ++(*i);
888*56bb7041Schristos 
889*56bb7041Schristos   // Figure out the option's argument, if any.
890*56bb7041Schristos   if (!retval->takes_argument())
891*56bb7041Schristos     {
892*56bb7041Schristos       if (equals)
893*56bb7041Schristos 	usage(_("unexpected argument"), this_argv);
894*56bb7041Schristos       else
895*56bb7041Schristos 	*arg = NULL;
896*56bb7041Schristos     }
897*56bb7041Schristos   else
898*56bb7041Schristos     {
899*56bb7041Schristos       if (equals)
900*56bb7041Schristos 	*arg = equals + 1;
901*56bb7041Schristos       else if (retval->takes_optional_argument())
902*56bb7041Schristos 	*arg = retval->default_value;
903*56bb7041Schristos       else if (*i < argc && !equals_only)
904*56bb7041Schristos 	*arg = argv[(*i)++];
905*56bb7041Schristos       else
906*56bb7041Schristos 	usage(_("missing argument"), this_argv);
907*56bb7041Schristos     }
908*56bb7041Schristos 
909*56bb7041Schristos   return retval;
910*56bb7041Schristos }
911*56bb7041Schristos 
912*56bb7041Schristos // Parse a short option.  Such options have the form -<option>[arg].
913*56bb7041Schristos // If "arg" is not present but the option takes an argument, the next
914*56bb7041Schristos // word is taken to the be the argument.  If the option does not take
915*56bb7041Schristos // an argument, it may be followed by another short option.  Returns a
916*56bb7041Schristos // One_option struct or NULL if argv[i] cannot be parsed as a short
917*56bb7041Schristos // option.  In the not-NULL case, *arg is set to the option's argument
918*56bb7041Schristos // (NULL if the option takes no argument), and *i is advanced past
919*56bb7041Schristos // this option.  This function keeps *i the same if we parsed a short
920*56bb7041Schristos // option that does not take an argument, that looks to be followed by
921*56bb7041Schristos // another short option in the same word.
922*56bb7041Schristos gold::options::One_option*
parse_short_option(int argc,const char ** argv,int pos_in_argv_i,const char ** arg,int * i)923*56bb7041Schristos parse_short_option(int argc, const char** argv, int pos_in_argv_i,
924*56bb7041Schristos 		   const char** arg, int* i)
925*56bb7041Schristos {
926*56bb7041Schristos   const char* const this_argv = argv[*i];
927*56bb7041Schristos 
928*56bb7041Schristos   if (this_argv[0] != '-')
929*56bb7041Schristos     return NULL;
930*56bb7041Schristos 
931*56bb7041Schristos   // We handle -z as a special case.
932*56bb7041Schristos   static gold::options::One_option dash_z("", gold::options::DASH_Z,
933*56bb7041Schristos 					  'z', "", NULL, "Z-OPTION", false,
934*56bb7041Schristos 					  NULL, false);
935*56bb7041Schristos   gold::options::One_option* retval = NULL;
936*56bb7041Schristos   if (this_argv[pos_in_argv_i] == 'z')
937*56bb7041Schristos     retval = &dash_z;
938*56bb7041Schristos   else
939*56bb7041Schristos     {
940*56bb7041Schristos       const int char_as_int = static_cast<int>(this_argv[pos_in_argv_i]);
941*56bb7041Schristos       if (char_as_int > 0 && char_as_int < 128)
942*56bb7041Schristos 	retval = gold::options::short_options[char_as_int];
943*56bb7041Schristos     }
944*56bb7041Schristos 
945*56bb7041Schristos   if (retval == NULL)
946*56bb7041Schristos     return NULL;
947*56bb7041Schristos 
948*56bb7041Schristos   // Figure out the option's argument, if any.
949*56bb7041Schristos   if (!retval->takes_argument())
950*56bb7041Schristos     {
951*56bb7041Schristos       *arg = NULL;
952*56bb7041Schristos       // We only advance past this argument if it's the only one in argv.
953*56bb7041Schristos       if (this_argv[pos_in_argv_i + 1] == '\0')
954*56bb7041Schristos 	++(*i);
955*56bb7041Schristos     }
956*56bb7041Schristos   else
957*56bb7041Schristos     {
958*56bb7041Schristos       // If we take an argument, we'll eat up this entire argv entry.
959*56bb7041Schristos       ++(*i);
960*56bb7041Schristos       if (this_argv[pos_in_argv_i + 1] != '\0')
961*56bb7041Schristos 	*arg = this_argv + pos_in_argv_i + 1;
962*56bb7041Schristos       else if (retval->takes_optional_argument())
963*56bb7041Schristos 	*arg = retval->default_value;
964*56bb7041Schristos       else if (*i < argc)
965*56bb7041Schristos 	*arg = argv[(*i)++];
966*56bb7041Schristos       else
967*56bb7041Schristos 	usage(_("missing argument"), this_argv);
968*56bb7041Schristos     }
969*56bb7041Schristos 
970*56bb7041Schristos   // If we're a -z option, we need to parse our argument as a
971*56bb7041Schristos   // long-option, e.g. "-z stacksize=8192".
972*56bb7041Schristos   if (retval == &dash_z)
973*56bb7041Schristos     {
974*56bb7041Schristos       int dummy_i = 0;
975*56bb7041Schristos       const char* dash_z_arg = *arg;
976*56bb7041Schristos       retval = parse_long_option(1, arg, true, arg, &dummy_i);
977*56bb7041Schristos       if (retval == NULL)
978*56bb7041Schristos 	usage(_("unknown -z option"), dash_z_arg);
979*56bb7041Schristos     }
980*56bb7041Schristos 
981*56bb7041Schristos   return retval;
982*56bb7041Schristos }
983*56bb7041Schristos 
984*56bb7041Schristos } // End anonymous namespace.
985*56bb7041Schristos 
986*56bb7041Schristos namespace gold
987*56bb7041Schristos {
988*56bb7041Schristos 
General_options()989*56bb7041Schristos General_options::General_options()
990*56bb7041Schristos   : printed_version_(false),
991*56bb7041Schristos     execstack_status_(EXECSTACK_FROM_INPUT),
992*56bb7041Schristos     icf_status_(ICF_NONE),
993*56bb7041Schristos     static_(false),
994*56bb7041Schristos     do_demangle_(false),
995*56bb7041Schristos     plugins_(NULL),
996*56bb7041Schristos     dynamic_list_(),
997*56bb7041Schristos     have_dynamic_list_(false),
998*56bb7041Schristos     incremental_mode_(INCREMENTAL_OFF),
999*56bb7041Schristos     incremental_disposition_(INCREMENTAL_STARTUP),
1000*56bb7041Schristos     incremental_startup_disposition_(INCREMENTAL_CHECK),
1001*56bb7041Schristos     implicit_incremental_(false),
1002*56bb7041Schristos     excluded_libs_(),
1003*56bb7041Schristos     symbols_to_retain_(),
1004*56bb7041Schristos     section_starts_(),
1005*56bb7041Schristos     fix_v4bx_(FIX_V4BX_NONE),
1006*56bb7041Schristos     endianness_(ENDIANNESS_NOT_SET),
1007*56bb7041Schristos     discard_locals_(DISCARD_SEC_MERGE),
1008*56bb7041Schristos     orphan_handling_enum_(ORPHAN_PLACE),
1009*56bb7041Schristos     start_stop_visibility_enum_(elfcpp::STV_PROTECTED)
1010*56bb7041Schristos {
1011*56bb7041Schristos   // Turn off option registration once construction is complete.
1012*56bb7041Schristos   gold::options::ready_to_register = false;
1013*56bb7041Schristos }
1014*56bb7041Schristos 
1015*56bb7041Schristos General_options::Object_format
format_enum() const1016*56bb7041Schristos General_options::format_enum() const
1017*56bb7041Schristos {
1018*56bb7041Schristos   return General_options::string_to_object_format(this->format());
1019*56bb7041Schristos }
1020*56bb7041Schristos 
1021*56bb7041Schristos General_options::Object_format
oformat_enum() const1022*56bb7041Schristos General_options::oformat_enum() const
1023*56bb7041Schristos {
1024*56bb7041Schristos   return General_options::string_to_object_format(this->oformat());
1025*56bb7041Schristos }
1026*56bb7041Schristos 
1027*56bb7041Schristos // Add the sysroot, if any, to the search paths.
1028*56bb7041Schristos 
1029*56bb7041Schristos void
add_sysroot()1030*56bb7041Schristos General_options::add_sysroot()
1031*56bb7041Schristos {
1032*56bb7041Schristos   if (this->sysroot() == NULL || this->sysroot()[0] == '\0')
1033*56bb7041Schristos     {
1034*56bb7041Schristos       this->set_sysroot(get_default_sysroot());
1035*56bb7041Schristos       if (this->sysroot() == NULL || this->sysroot()[0] == '\0')
1036*56bb7041Schristos 	return;
1037*56bb7041Schristos     }
1038*56bb7041Schristos 
1039*56bb7041Schristos   char* canonical_sysroot = lrealpath(this->sysroot());
1040*56bb7041Schristos 
1041*56bb7041Schristos   for (Dir_list::iterator p = this->library_path_.value.begin();
1042*56bb7041Schristos        p != this->library_path_.value.end();
1043*56bb7041Schristos        ++p)
1044*56bb7041Schristos     p->add_sysroot(this->sysroot(), canonical_sysroot);
1045*56bb7041Schristos 
1046*56bb7041Schristos   free(canonical_sysroot);
1047*56bb7041Schristos }
1048*56bb7041Schristos 
1049*56bb7041Schristos // Return whether FILENAME is in a system directory.
1050*56bb7041Schristos 
1051*56bb7041Schristos bool
is_in_system_directory(const std::string & filename) const1052*56bb7041Schristos General_options::is_in_system_directory(const std::string& filename) const
1053*56bb7041Schristos {
1054*56bb7041Schristos   for (Dir_list::const_iterator p = this->library_path_.value.begin();
1055*56bb7041Schristos        p != this->library_path_.value.end();
1056*56bb7041Schristos        ++p)
1057*56bb7041Schristos     {
1058*56bb7041Schristos       // We use a straight string comparison rather than calling
1059*56bb7041Schristos       // FILENAME_CMP because we are only interested in the cases
1060*56bb7041Schristos       // where we found the file in a system directory, which means
1061*56bb7041Schristos       // that we used the directory name as a prefix for a -L search.
1062*56bb7041Schristos       if (p->is_system_directory()
1063*56bb7041Schristos 	  && filename.compare(0, p->name().size(), p->name()) == 0)
1064*56bb7041Schristos 	return true;
1065*56bb7041Schristos     }
1066*56bb7041Schristos   return false;
1067*56bb7041Schristos }
1068*56bb7041Schristos 
1069*56bb7041Schristos // Add a plugin to the list of plugins.
1070*56bb7041Schristos 
1071*56bb7041Schristos void
add_plugin(const char * filename)1072*56bb7041Schristos General_options::add_plugin(const char* filename)
1073*56bb7041Schristos {
1074*56bb7041Schristos   if (this->plugins_ == NULL)
1075*56bb7041Schristos     this->plugins_ = new Plugin_manager(*this);
1076*56bb7041Schristos   this->plugins_->add_plugin(filename);
1077*56bb7041Schristos }
1078*56bb7041Schristos 
1079*56bb7041Schristos // Add a plugin option to a plugin.
1080*56bb7041Schristos 
1081*56bb7041Schristos void
add_plugin_option(const char * arg)1082*56bb7041Schristos General_options::add_plugin_option(const char* arg)
1083*56bb7041Schristos {
1084*56bb7041Schristos   if (this->plugins_ == NULL)
1085*56bb7041Schristos     gold_fatal("--plugin-opt requires --plugin.");
1086*56bb7041Schristos   this->plugins_->add_plugin_option(arg);
1087*56bb7041Schristos }
1088*56bb7041Schristos 
1089*56bb7041Schristos // Set up variables and other state that isn't set up automatically by
1090*56bb7041Schristos // the parse routine, and ensure options don't contradict each other
1091*56bb7041Schristos // and are otherwise kosher.
1092*56bb7041Schristos 
1093*56bb7041Schristos void
finalize()1094*56bb7041Schristos General_options::finalize()
1095*56bb7041Schristos {
1096*56bb7041Schristos   // Normalize the strip modifiers.  They have a total order:
1097*56bb7041Schristos   // strip_all > strip_debug > strip_non_line > strip_debug_gdb.
1098*56bb7041Schristos   // If one is true, set all beneath it to true as well.
1099*56bb7041Schristos   if (this->strip_all())
1100*56bb7041Schristos     this->set_strip_debug(true);
1101*56bb7041Schristos   if (this->strip_debug())
1102*56bb7041Schristos     this->set_strip_debug_non_line(true);
1103*56bb7041Schristos   if (this->strip_debug_non_line())
1104*56bb7041Schristos     this->set_strip_debug_gdb(true);
1105*56bb7041Schristos 
1106*56bb7041Schristos   if (this->Bshareable())
1107*56bb7041Schristos     this->set_shared(true);
1108*56bb7041Schristos 
1109*56bb7041Schristos   // If the user specifies both -s and -r, convert the -s to -S.
1110*56bb7041Schristos   // -r requires us to keep externally visible symbols!
1111*56bb7041Schristos   if (this->strip_all() && this->relocatable())
1112*56bb7041Schristos     {
1113*56bb7041Schristos       this->set_strip_all(false);
1114*56bb7041Schristos       gold_assert(this->strip_debug());
1115*56bb7041Schristos     }
1116*56bb7041Schristos 
1117*56bb7041Schristos   // For us, -dc and -dp are synonyms for --define-common.
1118*56bb7041Schristos   if (this->dc())
1119*56bb7041Schristos     this->set_define_common(true);
1120*56bb7041Schristos   if (this->dp())
1121*56bb7041Schristos     this->set_define_common(true);
1122*56bb7041Schristos 
1123*56bb7041Schristos   // We also set --define-common if we're not relocatable, as long as
1124*56bb7041Schristos   // the user didn't explicitly ask for something different.
1125*56bb7041Schristos   if (!this->user_set_define_common())
1126*56bb7041Schristos     this->set_define_common(!this->relocatable());
1127*56bb7041Schristos 
1128*56bb7041Schristos   // execstack_status_ is a three-state variable; update it based on
1129*56bb7041Schristos   // -z [no]execstack.
1130*56bb7041Schristos   if (this->execstack())
1131*56bb7041Schristos     this->set_execstack_status(EXECSTACK_YES);
1132*56bb7041Schristos   else if (this->noexecstack())
1133*56bb7041Schristos     this->set_execstack_status(EXECSTACK_NO);
1134*56bb7041Schristos 
1135*56bb7041Schristos   // icf_status_ is a three-state variable; update it based on the
1136*56bb7041Schristos   // value of this->icf().
1137*56bb7041Schristos   if (strcmp(this->icf(), "none") == 0)
1138*56bb7041Schristos     this->set_icf_status(ICF_NONE);
1139*56bb7041Schristos   else if (strcmp(this->icf(), "safe") == 0)
1140*56bb7041Schristos     this->set_icf_status(ICF_SAFE);
1141*56bb7041Schristos   else
1142*56bb7041Schristos     this->set_icf_status(ICF_ALL);
1143*56bb7041Schristos 
1144*56bb7041Schristos   // Handle the optional argument for --demangle.
1145*56bb7041Schristos   if (this->user_set_demangle())
1146*56bb7041Schristos     {
1147*56bb7041Schristos       this->set_do_demangle(true);
1148*56bb7041Schristos       const char* style = this->demangle();
1149*56bb7041Schristos       if (*style != '\0')
1150*56bb7041Schristos 	{
1151*56bb7041Schristos 	  enum demangling_styles style_code;
1152*56bb7041Schristos 
1153*56bb7041Schristos 	  style_code = cplus_demangle_name_to_style(style);
1154*56bb7041Schristos 	  if (style_code == unknown_demangling)
1155*56bb7041Schristos 	    gold_fatal("unknown demangling style '%s'", style);
1156*56bb7041Schristos 	  cplus_demangle_set_style(style_code);
1157*56bb7041Schristos 	}
1158*56bb7041Schristos     }
1159*56bb7041Schristos   else if (this->user_set_no_demangle())
1160*56bb7041Schristos     this->set_do_demangle(false);
1161*56bb7041Schristos   else
1162*56bb7041Schristos     {
1163*56bb7041Schristos       // Testing COLLECT_NO_DEMANGLE makes our default demangling
1164*56bb7041Schristos       // behaviour identical to that of gcc's linker wrapper.
1165*56bb7041Schristos       this->set_do_demangle(getenv("COLLECT_NO_DEMANGLE") == NULL);
1166*56bb7041Schristos     }
1167*56bb7041Schristos 
1168*56bb7041Schristos   // Parse the --orphan-handling argument.
1169*56bb7041Schristos   if (this->user_set_orphan_handling())
1170*56bb7041Schristos     {
1171*56bb7041Schristos       if (strcmp(this->orphan_handling(), "place") == 0)
1172*56bb7041Schristos         this->set_orphan_handling_enum(ORPHAN_PLACE);
1173*56bb7041Schristos       else if (strcmp(this->orphan_handling(), "discard") == 0)
1174*56bb7041Schristos         this->set_orphan_handling_enum(ORPHAN_DISCARD);
1175*56bb7041Schristos       else if (strcmp(this->orphan_handling(), "warn") == 0)
1176*56bb7041Schristos         this->set_orphan_handling_enum(ORPHAN_WARN);
1177*56bb7041Schristos       else if (strcmp(this->orphan_handling(), "error") == 0)
1178*56bb7041Schristos         this->set_orphan_handling_enum(ORPHAN_ERROR);
1179*56bb7041Schristos     }
1180*56bb7041Schristos 
1181*56bb7041Schristos   // Parse the -z start-stop-visibility argument.
1182*56bb7041Schristos   if (this->user_set_start_stop_visibility())
1183*56bb7041Schristos     {
1184*56bb7041Schristos       if (strcmp(this->start_stop_visibility(), "default") == 0)
1185*56bb7041Schristos         this->set_start_stop_visibility_enum(elfcpp::STV_DEFAULT);
1186*56bb7041Schristos       else if (strcmp(this->start_stop_visibility(), "internal") == 0)
1187*56bb7041Schristos         this->set_start_stop_visibility_enum(elfcpp::STV_INTERNAL);
1188*56bb7041Schristos       else if (strcmp(this->start_stop_visibility(), "hidden") == 0)
1189*56bb7041Schristos         this->set_start_stop_visibility_enum(elfcpp::STV_HIDDEN);
1190*56bb7041Schristos       else if (strcmp(this->start_stop_visibility(), "protected") == 0)
1191*56bb7041Schristos         this->set_start_stop_visibility_enum(elfcpp::STV_PROTECTED);
1192*56bb7041Schristos     }
1193*56bb7041Schristos 
1194*56bb7041Schristos   // Parse the --power10-stubs argument.
1195*56bb7041Schristos   if (!this->user_set_power10_stubs())
1196*56bb7041Schristos     {
1197*56bb7041Schristos       // --power10-stubs without an arg is equivalent to --power10-stubs=yes
1198*56bb7041Schristos       // but not specifying --power10-stubs at all should be equivalent to
1199*56bb7041Schristos       // --power10-stubs=auto.  This doesn't fit into the notion of
1200*56bb7041Schristos       // "default_value", used both as a static initializer and to provide
1201*56bb7041Schristos       // a missing optional arg.  Fix it here.
1202*56bb7041Schristos       this->set_power10_stubs("auto");
1203*56bb7041Schristos       this->set_power10_stubs_enum(POWER10_STUBS_AUTO);
1204*56bb7041Schristos     }
1205*56bb7041Schristos   else
1206*56bb7041Schristos     {
1207*56bb7041Schristos       if (strcmp(this->power10_stubs(), "auto") == 0)
1208*56bb7041Schristos 	this->set_power10_stubs_enum(POWER10_STUBS_AUTO);
1209*56bb7041Schristos       else if (strcmp(this->power10_stubs(), "no") == 0)
1210*56bb7041Schristos 	this->set_power10_stubs_enum(POWER10_STUBS_NO);
1211*56bb7041Schristos       else if (strcmp(this->power10_stubs(), "yes") == 0)
1212*56bb7041Schristos 	this->set_power10_stubs_enum(POWER10_STUBS_YES);
1213*56bb7041Schristos     }
1214*56bb7041Schristos 
1215*56bb7041Schristos   // -M is equivalent to "-Map -".
1216*56bb7041Schristos   if (this->print_map() && !this->user_set_Map())
1217*56bb7041Schristos     {
1218*56bb7041Schristos       this->set_Map("-");
1219*56bb7041Schristos       this->set_user_set_Map();
1220*56bb7041Schristos     }
1221*56bb7041Schristos 
1222*56bb7041Schristos   // Using -n or -N implies -static.
1223*56bb7041Schristos   if (this->nmagic() || this->omagic())
1224*56bb7041Schristos     this->set_static(true);
1225*56bb7041Schristos 
1226*56bb7041Schristos   // If --thread_count is specified, it applies to
1227*56bb7041Schristos   // --thread-count-{initial,middle,final}, though it doesn't override
1228*56bb7041Schristos   // them.
1229*56bb7041Schristos   if (this->thread_count() > 0 && this->thread_count_initial() == 0)
1230*56bb7041Schristos     this->set_thread_count_initial(this->thread_count());
1231*56bb7041Schristos   if (this->thread_count() > 0 && this->thread_count_middle() == 0)
1232*56bb7041Schristos     this->set_thread_count_middle(this->thread_count());
1233*56bb7041Schristos   if (this->thread_count() > 0 && this->thread_count_final() == 0)
1234*56bb7041Schristos     this->set_thread_count_final(this->thread_count());
1235*56bb7041Schristos 
1236*56bb7041Schristos   // Let's warn if you set the thread-count but we're going to ignore it.
1237*56bb7041Schristos #ifndef ENABLE_THREADS
1238*56bb7041Schristos   if (this->threads())
1239*56bb7041Schristos     {
1240*56bb7041Schristos       gold_warning(_("ignoring --threads: "
1241*56bb7041Schristos 		     "%s was compiled without thread support"),
1242*56bb7041Schristos 		   program_name);
1243*56bb7041Schristos       this->set_threads(false);
1244*56bb7041Schristos     }
1245*56bb7041Schristos   if (this->thread_count() > 0 || this->thread_count_initial() > 0
1246*56bb7041Schristos       || this->thread_count_middle() > 0 || this->thread_count_final() > 0)
1247*56bb7041Schristos     gold_warning(_("ignoring --thread-count: "
1248*56bb7041Schristos 		   "%s was compiled without thread support"),
1249*56bb7041Schristos 		 program_name);
1250*56bb7041Schristos #endif
1251*56bb7041Schristos 
1252*56bb7041Schristos #ifndef ENABLE_PLUGINS
1253*56bb7041Schristos   if (this->has_plugins())
1254*56bb7041Schristos     gold_fatal(_("cannot use --plugin: "
1255*56bb7041Schristos 		 "%s was compiled without plugin support"),
1256*56bb7041Schristos 	       program_name);
1257*56bb7041Schristos #endif
1258*56bb7041Schristos 
1259*56bb7041Schristos   std::string libpath;
1260*56bb7041Schristos   if (this->user_set_Y())
1261*56bb7041Schristos     {
1262*56bb7041Schristos       libpath = this->Y();
1263*56bb7041Schristos       if (libpath.compare(0, 2, "P,") == 0)
1264*56bb7041Schristos 	libpath.erase(0, 2);
1265*56bb7041Schristos     }
1266*56bb7041Schristos   else if (!this->nostdlib())
1267*56bb7041Schristos     {
1268*56bb7041Schristos #ifndef NATIVE_LINKER
1269*56bb7041Schristos #define NATIVE_LINKER 0
1270*56bb7041Schristos #endif
1271*56bb7041Schristos       const char* p = LIB_PATH;
1272*56bb7041Schristos       if (strcmp(p, "::DEFAULT::") != 0)
1273*56bb7041Schristos 	libpath = p;
1274*56bb7041Schristos       else if (NATIVE_LINKER
1275*56bb7041Schristos 	       || this->user_set_sysroot()
1276*56bb7041Schristos 	       || *TARGET_SYSTEM_ROOT != '\0')
1277*56bb7041Schristos 	{
1278*56bb7041Schristos 	  this->add_to_library_path_with_sysroot("/lib");
1279*56bb7041Schristos 	  this->add_to_library_path_with_sysroot("/usr/lib");
1280*56bb7041Schristos 	}
1281*56bb7041Schristos       else
1282*56bb7041Schristos 	this->add_to_library_path_with_sysroot(TOOLLIBDIR);
1283*56bb7041Schristos     }
1284*56bb7041Schristos 
1285*56bb7041Schristos   if (!libpath.empty())
1286*56bb7041Schristos     {
1287*56bb7041Schristos       size_t pos = 0;
1288*56bb7041Schristos       size_t next_pos;
1289*56bb7041Schristos       do
1290*56bb7041Schristos 	{
1291*56bb7041Schristos 	  next_pos = libpath.find(':', pos);
1292*56bb7041Schristos 	  size_t len = (next_pos == std::string::npos
1293*56bb7041Schristos 			? next_pos
1294*56bb7041Schristos 			: next_pos - pos);
1295*56bb7041Schristos 	  if (len != 0)
1296*56bb7041Schristos 	    this->add_to_library_path_with_sysroot(libpath.substr(pos, len));
1297*56bb7041Schristos 	  pos = next_pos + 1;
1298*56bb7041Schristos 	}
1299*56bb7041Schristos       while (next_pos != std::string::npos);
1300*56bb7041Schristos     }
1301*56bb7041Schristos 
1302*56bb7041Schristos   // Parse the contents of -retain-symbols-file into a set.
1303*56bb7041Schristos   if (this->retain_symbols_file())
1304*56bb7041Schristos     {
1305*56bb7041Schristos       std::ifstream in;
1306*56bb7041Schristos       in.open(this->retain_symbols_file());
1307*56bb7041Schristos       if (!in)
1308*56bb7041Schristos 	gold_fatal(_("unable to open -retain-symbols-file file %s: %s"),
1309*56bb7041Schristos 		   this->retain_symbols_file(), strerror(errno));
1310*56bb7041Schristos       std::string line;
1311*56bb7041Schristos       std::getline(in, line);   // this chops off the trailing \n, if any
1312*56bb7041Schristos       while (in)
1313*56bb7041Schristos 	{
1314*56bb7041Schristos 	  if (!line.empty() && line[line.length() - 1] == '\r')   // Windows
1315*56bb7041Schristos 	    line.resize(line.length() - 1);
1316*56bb7041Schristos 	  this->symbols_to_retain_.insert(line);
1317*56bb7041Schristos 	  std::getline(in, line);
1318*56bb7041Schristos 	}
1319*56bb7041Schristos     }
1320*56bb7041Schristos 
1321*56bb7041Schristos   // -Bgroup implies --unresolved-symbols=report-all.
1322*56bb7041Schristos   if (this->Bgroup() && !this->user_set_unresolved_symbols())
1323*56bb7041Schristos     this->set_unresolved_symbols("report-all");
1324*56bb7041Schristos 
1325*56bb7041Schristos   // -shared implies --allow-shlib-undefined.  Currently
1326*56bb7041Schristos   // ---allow-shlib-undefined controls warnings issued based on the
1327*56bb7041Schristos   // -symbol table.  --unresolved-symbols controls warnings issued
1328*56bb7041Schristos   // -based on relocations.
1329*56bb7041Schristos   if (this->shared() && !this->user_set_allow_shlib_undefined())
1330*56bb7041Schristos     this->set_allow_shlib_undefined(true);
1331*56bb7041Schristos 
1332*56bb7041Schristos   // Normalize library_path() by adding the sysroot to all directories
1333*56bb7041Schristos   // in the path, as appropriate.
1334*56bb7041Schristos   this->add_sysroot();
1335*56bb7041Schristos 
1336*56bb7041Schristos   // Now that we've normalized the options, check for contradictory ones.
1337*56bb7041Schristos   if (this->shared() && this->is_static())
1338*56bb7041Schristos     gold_fatal(_("-shared and -static are incompatible"));
1339*56bb7041Schristos   if (this->shared() && this->pie())
1340*56bb7041Schristos     gold_fatal(_("-shared and -pie are incompatible"));
1341*56bb7041Schristos   if (this->pie() && this->is_static())
1342*56bb7041Schristos     gold_fatal(_("-pie and -static are incompatible"));
1343*56bb7041Schristos 
1344*56bb7041Schristos   if (this->shared() && this->relocatable())
1345*56bb7041Schristos     gold_fatal(_("-shared and -r are incompatible"));
1346*56bb7041Schristos   if (this->pie() && this->relocatable())
1347*56bb7041Schristos     gold_fatal(_("-pie and -r are incompatible"));
1348*56bb7041Schristos 
1349*56bb7041Schristos   if (!this->shared())
1350*56bb7041Schristos     {
1351*56bb7041Schristos       if (this->filter() != NULL)
1352*56bb7041Schristos 	gold_fatal(_("-F/--filter may not used without -shared"));
1353*56bb7041Schristos       if (this->any_auxiliary())
1354*56bb7041Schristos 	gold_fatal(_("-f/--auxiliary may not be used without -shared"));
1355*56bb7041Schristos     }
1356*56bb7041Schristos 
1357*56bb7041Schristos   // TODO: implement support for -retain-symbols-file with -r, if needed.
1358*56bb7041Schristos   if (this->relocatable() && this->retain_symbols_file())
1359*56bb7041Schristos     gold_fatal(_("-retain-symbols-file does not yet work with -r"));
1360*56bb7041Schristos 
1361*56bb7041Schristos   if (this->oformat_enum() != General_options::OBJECT_FORMAT_ELF
1362*56bb7041Schristos       && (this->shared()
1363*56bb7041Schristos 	  || this->pie()
1364*56bb7041Schristos 	  || this->relocatable()))
1365*56bb7041Schristos     gold_fatal(_("binary output format not compatible "
1366*56bb7041Schristos 		 "with -shared or -pie or -r"));
1367*56bb7041Schristos 
1368*56bb7041Schristos   if (this->user_set_hash_bucket_empty_fraction()
1369*56bb7041Schristos       && (this->hash_bucket_empty_fraction() < 0.0
1370*56bb7041Schristos 	  || this->hash_bucket_empty_fraction() >= 1.0))
1371*56bb7041Schristos     gold_fatal(_("--hash-bucket-empty-fraction value %g out of range "
1372*56bb7041Schristos 		 "[0.0, 1.0)"),
1373*56bb7041Schristos 	       this->hash_bucket_empty_fraction());
1374*56bb7041Schristos 
1375*56bb7041Schristos   if (this->implicit_incremental_ && this->incremental_mode_ == INCREMENTAL_OFF)
1376*56bb7041Schristos     gold_fatal(_("Options --incremental-changed, --incremental-unchanged, "
1377*56bb7041Schristos 		 "--incremental-unknown require the use of --incremental"));
1378*56bb7041Schristos 
1379*56bb7041Schristos   // Check for options that are not compatible with incremental linking.
1380*56bb7041Schristos   // Where an option can be disabled without seriously changing the semantics
1381*56bb7041Schristos   // of the link, we turn the option off; otherwise, we issue a fatal error.
1382*56bb7041Schristos 
1383*56bb7041Schristos   if (this->incremental_mode_ != INCREMENTAL_OFF)
1384*56bb7041Schristos     {
1385*56bb7041Schristos       if (this->relocatable())
1386*56bb7041Schristos 	gold_fatal(_("incremental linking is not compatible with -r"));
1387*56bb7041Schristos       if (this->emit_relocs())
1388*56bb7041Schristos 	gold_fatal(_("incremental linking is not compatible with "
1389*56bb7041Schristos 		     "--emit-relocs"));
1390*56bb7041Schristos       if (this->has_plugins())
1391*56bb7041Schristos 	gold_fatal(_("incremental linking is not compatible with --plugin"));
1392*56bb7041Schristos       if (this->relro())
1393*56bb7041Schristos 	gold_fatal(_("incremental linking is not compatible with -z relro"));
1394*56bb7041Schristos       if (this->pie())
1395*56bb7041Schristos 	gold_fatal(_("incremental linking is not compatible with -pie"));
1396*56bb7041Schristos       if (this->gc_sections())
1397*56bb7041Schristos 	{
1398*56bb7041Schristos 	  gold_warning(_("ignoring --gc-sections for an incremental link"));
1399*56bb7041Schristos 	  this->set_gc_sections(false);
1400*56bb7041Schristos 	}
1401*56bb7041Schristos       if (this->icf_enabled())
1402*56bb7041Schristos 	{
1403*56bb7041Schristos 	  gold_warning(_("ignoring --icf for an incremental link"));
1404*56bb7041Schristos 	  this->set_icf_status(ICF_NONE);
1405*56bb7041Schristos 	}
1406*56bb7041Schristos       if (strcmp(this->compress_debug_sections(), "none") != 0)
1407*56bb7041Schristos 	{
1408*56bb7041Schristos 	  gold_warning(_("ignoring --compress-debug-sections for an "
1409*56bb7041Schristos 			 "incremental link"));
1410*56bb7041Schristos 	  this->set_compress_debug_sections("none");
1411*56bb7041Schristos 	}
1412*56bb7041Schristos     }
1413*56bb7041Schristos 
1414*56bb7041Schristos   // --rosegment-gap implies --rosegment.
1415*56bb7041Schristos   if (this->user_set_rosegment_gap())
1416*56bb7041Schristos     this->set_rosegment(true);
1417*56bb7041Schristos 
1418*56bb7041Schristos   // FIXME: we can/should be doing a lot more sanity checking here.
1419*56bb7041Schristos }
1420*56bb7041Schristos 
1421*56bb7041Schristos // Search_directory methods.
1422*56bb7041Schristos 
1423*56bb7041Schristos // This is called if we have a sysroot.  Apply the sysroot if
1424*56bb7041Schristos // appropriate.  Record whether the directory is in the sysroot.
1425*56bb7041Schristos 
1426*56bb7041Schristos void
add_sysroot(const char * sysroot,const char * canonical_sysroot)1427*56bb7041Schristos Search_directory::add_sysroot(const char* sysroot,
1428*56bb7041Schristos 			      const char* canonical_sysroot)
1429*56bb7041Schristos {
1430*56bb7041Schristos   gold_assert(*sysroot != '\0');
1431*56bb7041Schristos   if (this->put_in_sysroot_)
1432*56bb7041Schristos     {
1433*56bb7041Schristos       if (!IS_DIR_SEPARATOR(this->name_[0])
1434*56bb7041Schristos 	  && !IS_DIR_SEPARATOR(sysroot[strlen(sysroot) - 1]))
1435*56bb7041Schristos 	this->name_ = '/' + this->name_;
1436*56bb7041Schristos       this->name_ = sysroot + this->name_;
1437*56bb7041Schristos       this->is_in_sysroot_ = true;
1438*56bb7041Schristos     }
1439*56bb7041Schristos   else
1440*56bb7041Schristos     {
1441*56bb7041Schristos       // Check whether this entry is in the sysroot.  To do this
1442*56bb7041Schristos       // correctly, we need to use canonical names.  Otherwise we will
1443*56bb7041Schristos       // get confused by the ../../.. paths that gcc tends to use.
1444*56bb7041Schristos       char* canonical_name = lrealpath(this->name_.c_str());
1445*56bb7041Schristos       int canonical_name_len = strlen(canonical_name);
1446*56bb7041Schristos       int canonical_sysroot_len = strlen(canonical_sysroot);
1447*56bb7041Schristos       if (canonical_name_len > canonical_sysroot_len
1448*56bb7041Schristos 	  && IS_DIR_SEPARATOR(canonical_name[canonical_sysroot_len]))
1449*56bb7041Schristos 	{
1450*56bb7041Schristos 	  canonical_name[canonical_sysroot_len] = '\0';
1451*56bb7041Schristos 	  if (FILENAME_CMP(canonical_name, canonical_sysroot) == 0)
1452*56bb7041Schristos 	    this->is_in_sysroot_ = true;
1453*56bb7041Schristos 	}
1454*56bb7041Schristos       free(canonical_name);
1455*56bb7041Schristos     }
1456*56bb7041Schristos }
1457*56bb7041Schristos 
1458*56bb7041Schristos // Input_arguments methods.
1459*56bb7041Schristos 
1460*56bb7041Schristos // Add a file to the list.
1461*56bb7041Schristos 
1462*56bb7041Schristos Input_argument&
add_file(Input_file_argument & file)1463*56bb7041Schristos Input_arguments::add_file(Input_file_argument& file)
1464*56bb7041Schristos {
1465*56bb7041Schristos   file.set_arg_serial(++this->file_count_);
1466*56bb7041Schristos   if (this->in_group_)
1467*56bb7041Schristos     {
1468*56bb7041Schristos       gold_assert(!this->input_argument_list_.empty());
1469*56bb7041Schristos       gold_assert(this->input_argument_list_.back().is_group());
1470*56bb7041Schristos       return this->input_argument_list_.back().group()->add_file(file);
1471*56bb7041Schristos     }
1472*56bb7041Schristos   if (this->in_lib_)
1473*56bb7041Schristos     {
1474*56bb7041Schristos       gold_assert(!this->input_argument_list_.empty());
1475*56bb7041Schristos       gold_assert(this->input_argument_list_.back().is_lib());
1476*56bb7041Schristos       return this->input_argument_list_.back().lib()->add_file(file);
1477*56bb7041Schristos     }
1478*56bb7041Schristos   this->input_argument_list_.push_back(Input_argument(file));
1479*56bb7041Schristos   return this->input_argument_list_.back();
1480*56bb7041Schristos }
1481*56bb7041Schristos 
1482*56bb7041Schristos // Start a group.
1483*56bb7041Schristos 
1484*56bb7041Schristos void
start_group()1485*56bb7041Schristos Input_arguments::start_group()
1486*56bb7041Schristos {
1487*56bb7041Schristos   if (this->in_group_)
1488*56bb7041Schristos     gold_fatal(_("May not nest groups"));
1489*56bb7041Schristos   if (this->in_lib_)
1490*56bb7041Schristos     gold_fatal(_("may not nest groups in libraries"));
1491*56bb7041Schristos   Input_file_group* group = new Input_file_group();
1492*56bb7041Schristos   this->input_argument_list_.push_back(Input_argument(group));
1493*56bb7041Schristos   this->in_group_ = true;
1494*56bb7041Schristos }
1495*56bb7041Schristos 
1496*56bb7041Schristos // End a group.
1497*56bb7041Schristos 
1498*56bb7041Schristos void
end_group()1499*56bb7041Schristos Input_arguments::end_group()
1500*56bb7041Schristos {
1501*56bb7041Schristos   if (!this->in_group_)
1502*56bb7041Schristos     gold_fatal(_("Group end without group start"));
1503*56bb7041Schristos   this->in_group_ = false;
1504*56bb7041Schristos }
1505*56bb7041Schristos 
1506*56bb7041Schristos // Start a lib.
1507*56bb7041Schristos 
1508*56bb7041Schristos void
start_lib(const Position_dependent_options & options)1509*56bb7041Schristos Input_arguments::start_lib(const Position_dependent_options& options)
1510*56bb7041Schristos {
1511*56bb7041Schristos   if (this->in_lib_)
1512*56bb7041Schristos     gold_fatal(_("may not nest libraries"));
1513*56bb7041Schristos   if (this->in_group_)
1514*56bb7041Schristos     gold_fatal(_("may not nest libraries in groups"));
1515*56bb7041Schristos   Input_file_lib* lib = new Input_file_lib(options);
1516*56bb7041Schristos   this->input_argument_list_.push_back(Input_argument(lib));
1517*56bb7041Schristos   this->in_lib_ = true;
1518*56bb7041Schristos }
1519*56bb7041Schristos 
1520*56bb7041Schristos // End a lib.
1521*56bb7041Schristos 
1522*56bb7041Schristos void
end_lib()1523*56bb7041Schristos Input_arguments::end_lib()
1524*56bb7041Schristos {
1525*56bb7041Schristos   if (!this->in_lib_)
1526*56bb7041Schristos     gold_fatal(_("lib end without lib start"));
1527*56bb7041Schristos   this->in_lib_ = false;
1528*56bb7041Schristos }
1529*56bb7041Schristos 
1530*56bb7041Schristos // Command_line options.
1531*56bb7041Schristos 
Command_line()1532*56bb7041Schristos Command_line::Command_line()
1533*56bb7041Schristos {
1534*56bb7041Schristos }
1535*56bb7041Schristos 
1536*56bb7041Schristos // Pre_options is the hook that sets the ready_to_register flag.
1537*56bb7041Schristos 
Pre_options()1538*56bb7041Schristos Command_line::Pre_options::Pre_options()
1539*56bb7041Schristos {
1540*56bb7041Schristos   gold::options::ready_to_register = true;
1541*56bb7041Schristos }
1542*56bb7041Schristos 
1543*56bb7041Schristos // Process the command line options.  For process_one_option, i is the
1544*56bb7041Schristos // index of argv to process next, and must be an option (that is,
1545*56bb7041Schristos // start with a dash).  The return value is the index of the next
1546*56bb7041Schristos // option to process (i+1 or i+2, or argc to indicate processing is
1547*56bb7041Schristos // done).  no_more_options is set to true if (and when) "--" is seen
1548*56bb7041Schristos // as an option.
1549*56bb7041Schristos 
1550*56bb7041Schristos int
process_one_option(int argc,const char ** argv,int i,bool * no_more_options)1551*56bb7041Schristos Command_line::process_one_option(int argc, const char** argv, int i,
1552*56bb7041Schristos 				 bool* no_more_options)
1553*56bb7041Schristos {
1554*56bb7041Schristos   gold_assert(argv[i][0] == '-' && !(*no_more_options));
1555*56bb7041Schristos 
1556*56bb7041Schristos   // If we are reading "--", then just set no_more_options and return.
1557*56bb7041Schristos   if (argv[i][1] == '-' && argv[i][2] == '\0')
1558*56bb7041Schristos     {
1559*56bb7041Schristos       *no_more_options = true;
1560*56bb7041Schristos       return i + 1;
1561*56bb7041Schristos     }
1562*56bb7041Schristos 
1563*56bb7041Schristos   int new_i = i;
1564*56bb7041Schristos   options::One_option* option = NULL;
1565*56bb7041Schristos   const char* arg = NULL;
1566*56bb7041Schristos 
1567*56bb7041Schristos   // First, try to process argv as a long option.
1568*56bb7041Schristos   option = parse_long_option(argc, argv, false, &arg, &new_i);
1569*56bb7041Schristos   if (option)
1570*56bb7041Schristos     {
1571*56bb7041Schristos       option->reader->parse_to_value(argv[i], arg, this, &this->options_);
1572*56bb7041Schristos       return new_i;
1573*56bb7041Schristos     }
1574*56bb7041Schristos 
1575*56bb7041Schristos   // Now, try to process argv as a short option.  Since several short
1576*56bb7041Schristos   // options can be combined in one argv, we may have to parse a lot
1577*56bb7041Schristos   // until we're done reading this argv.
1578*56bb7041Schristos   int pos_in_argv_i = 1;
1579*56bb7041Schristos   while (new_i == i)
1580*56bb7041Schristos     {
1581*56bb7041Schristos       option = parse_short_option(argc, argv, pos_in_argv_i, &arg, &new_i);
1582*56bb7041Schristos       if (!option)
1583*56bb7041Schristos 	break;
1584*56bb7041Schristos       option->reader->parse_to_value(argv[i], arg, this, &this->options_);
1585*56bb7041Schristos       ++pos_in_argv_i;
1586*56bb7041Schristos     }
1587*56bb7041Schristos   if (option)
1588*56bb7041Schristos     return new_i;
1589*56bb7041Schristos 
1590*56bb7041Schristos   // I guess it's neither a long option nor a short option.
1591*56bb7041Schristos   usage(_("unknown option"), argv[i]);
1592*56bb7041Schristos   return argc;
1593*56bb7041Schristos }
1594*56bb7041Schristos 
1595*56bb7041Schristos 
1596*56bb7041Schristos void
process(int argc,const char ** argv)1597*56bb7041Schristos Command_line::process(int argc, const char** argv)
1598*56bb7041Schristos {
1599*56bb7041Schristos   bool no_more_options = false;
1600*56bb7041Schristos   int i = 0;
1601*56bb7041Schristos   while (i < argc)
1602*56bb7041Schristos     {
1603*56bb7041Schristos       this->position_options_.copy_from_options(this->options());
1604*56bb7041Schristos       if (no_more_options || argv[i][0] != '-')
1605*56bb7041Schristos 	{
1606*56bb7041Schristos 	  Input_file_argument file(argv[i],
1607*56bb7041Schristos 				   Input_file_argument::INPUT_FILE_TYPE_FILE,
1608*56bb7041Schristos 				   "", false, this->position_options_);
1609*56bb7041Schristos 	  this->inputs_.add_file(file);
1610*56bb7041Schristos 	  ++i;
1611*56bb7041Schristos 	}
1612*56bb7041Schristos       else
1613*56bb7041Schristos 	i = process_one_option(argc, argv, i, &no_more_options);
1614*56bb7041Schristos     }
1615*56bb7041Schristos 
1616*56bb7041Schristos   if (this->inputs_.in_group())
1617*56bb7041Schristos     {
1618*56bb7041Schristos       fprintf(stderr, _("%s: missing group end\n"), program_name);
1619*56bb7041Schristos       usage();
1620*56bb7041Schristos     }
1621*56bb7041Schristos 
1622*56bb7041Schristos   if (this->inputs_.in_lib())
1623*56bb7041Schristos     {
1624*56bb7041Schristos       fprintf(stderr, _("%s: missing lib end\n"), program_name);
1625*56bb7041Schristos       usage();
1626*56bb7041Schristos     }
1627*56bb7041Schristos 
1628*56bb7041Schristos   // Normalize the options and ensure they don't contradict each other.
1629*56bb7041Schristos   this->options_.finalize();
1630*56bb7041Schristos }
1631*56bb7041Schristos 
1632*56bb7041Schristos // Finalize the version script options and return them.
1633*56bb7041Schristos 
1634*56bb7041Schristos const Version_script_info&
version_script()1635*56bb7041Schristos Command_line::version_script()
1636*56bb7041Schristos {
1637*56bb7041Schristos   this->options_.finalize_dynamic_list();
1638*56bb7041Schristos   Version_script_info* vsi = this->script_options_.version_script_info();
1639*56bb7041Schristos   vsi->finalize();
1640*56bb7041Schristos   return *vsi;
1641*56bb7041Schristos }
1642*56bb7041Schristos 
1643*56bb7041Schristos } // End namespace gold.
1644