options.cc 41 KB

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