options.cc 46 KB

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