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