options.h 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166
  1. // options.h -- handle command line options for gold -*- C++ -*-
  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. // General_options (from Command_line::options())
  18. // All the options (a.k.a. command-line flags)
  19. // Input_argument (from Command_line::inputs())
  20. // The list of input files, including -l options.
  21. // Command_line
  22. // Everything we get from the command line -- the General_options
  23. // plus the Input_arguments.
  24. //
  25. // There are also some smaller classes, such as
  26. // Position_dependent_options which hold a subset of General_options
  27. // that change as options are parsed (as opposed to the usual behavior
  28. // of the last instance of that option specified on the commandline wins).
  29. #ifndef GOLD_OPTIONS_H
  30. #define GOLD_OPTIONS_H
  31. #include <cstdlib>
  32. #include <cstring>
  33. #include <list>
  34. #include <map>
  35. #include <string>
  36. #include <vector>
  37. #include "elfcpp.h"
  38. #include "script.h"
  39. namespace gold
  40. {
  41. class Command_line;
  42. class General_options;
  43. class Search_directory;
  44. class Input_file_group;
  45. class Input_file_lib;
  46. class Position_dependent_options;
  47. class Target;
  48. class Plugin_manager;
  49. class Script_info;
  50. // Incremental build action for a specific file, as selected by the user.
  51. enum Incremental_disposition
  52. {
  53. // Startup files that appear before the first disposition option.
  54. // These will default to INCREMENTAL_CHECK unless the
  55. // --incremental-startup-unchanged option is given.
  56. // (For files added implicitly by gcc before any user options.)
  57. INCREMENTAL_STARTUP,
  58. // Determine the status from the timestamp (default).
  59. INCREMENTAL_CHECK,
  60. // Assume the file changed from the previous build.
  61. INCREMENTAL_CHANGED,
  62. // Assume the file didn't change from the previous build.
  63. INCREMENTAL_UNCHANGED
  64. };
  65. // The nested namespace is to contain all the global variables and
  66. // structs that need to be defined in the .h file, but do not need to
  67. // be used outside this class.
  68. namespace options
  69. {
  70. typedef std::vector<Search_directory> Dir_list;
  71. typedef Unordered_set<std::string> String_set;
  72. // These routines convert from a string option to various types.
  73. // Each gives a fatal error if it cannot parse the argument.
  74. extern void
  75. parse_bool(const char* option_name, const char* arg, bool* retval);
  76. extern void
  77. parse_int(const char* option_name, const char* arg, int* retval);
  78. extern void
  79. parse_uint(const char* option_name, const char* arg, int* retval);
  80. extern void
  81. parse_uint64(const char* option_name, const char* arg, uint64_t* retval);
  82. extern void
  83. parse_double(const char* option_name, const char* arg, double* retval);
  84. extern void
  85. parse_percent(const char* option_name, const char* arg, double* retval);
  86. extern void
  87. parse_string(const char* option_name, const char* arg, const char** retval);
  88. extern void
  89. parse_optional_string(const char* option_name, const char* arg,
  90. const char** retval);
  91. extern void
  92. parse_dirlist(const char* option_name, const char* arg, Dir_list* retval);
  93. extern void
  94. parse_set(const char* option_name, const char* arg, String_set* retval);
  95. extern void
  96. parse_choices(const char* option_name, const char* arg, const char** retval,
  97. const char* choices[], int num_choices);
  98. struct Struct_var;
  99. // Most options have both a shortname (one letter) and a longname.
  100. // This enum controls how many dashes are expected for longname access
  101. // -- shortnames always use one dash. Most longnames will accept
  102. // either one dash or two; the only difference between ONE_DASH and
  103. // TWO_DASHES is how we print the option in --help. However, some
  104. // longnames require two dashes, and some require only one. The
  105. // special value DASH_Z means that the option is preceded by "-z".
  106. enum Dashes
  107. {
  108. ONE_DASH, TWO_DASHES, EXACTLY_ONE_DASH, EXACTLY_TWO_DASHES, DASH_Z
  109. };
  110. // LONGNAME is the long-name of the option with dashes converted to
  111. // underscores, or else the short-name if the option has no long-name.
  112. // It is never the empty string.
  113. // DASHES is an instance of the Dashes enum: ONE_DASH, TWO_DASHES, etc.
  114. // SHORTNAME is the short-name of the option, as a char, or '\0' if the
  115. // option has no short-name. If the option has no long-name, you
  116. // should specify the short-name in *both* VARNAME and here.
  117. // DEFAULT_VALUE is the value of the option if not specified on the
  118. // commandline, as a string.
  119. // HELPSTRING is the descriptive text used with the option via --help
  120. // HELPARG is how you define the argument to the option.
  121. // --help output is "-shortname HELPARG, --longname HELPARG: HELPSTRING"
  122. // HELPARG should be NULL iff the option is a bool and takes no arg.
  123. // OPTIONAL_ARG is true if this option takes an optional argument. An
  124. // optional argument must be specifid as --OPTION=VALUE, not
  125. // --OPTION VALUE.
  126. // READER provides parse_to_value, which is a function that will convert
  127. // a char* argument into the proper type and store it in some variable.
  128. // A One_option struct initializes itself with the global list of options
  129. // at constructor time, so be careful making one of these.
  130. struct One_option
  131. {
  132. std::string longname;
  133. Dashes dashes;
  134. char shortname;
  135. const char* default_value;
  136. const char* helpstring;
  137. const char* helparg;
  138. bool optional_arg;
  139. Struct_var* reader;
  140. One_option(const char* ln, Dashes d, char sn, const char* dv,
  141. const char* hs, const char* ha, bool oa, Struct_var* r)
  142. : longname(ln), dashes(d), shortname(sn), default_value(dv ? dv : ""),
  143. helpstring(hs), helparg(ha), optional_arg(oa), reader(r)
  144. {
  145. // In longname, we convert all underscores to dashes, since GNU
  146. // style uses dashes in option names. longname is likely to have
  147. // underscores in it because it's also used to declare a C++
  148. // function.
  149. const char* pos = strchr(this->longname.c_str(), '_');
  150. for (; pos; pos = strchr(pos, '_'))
  151. this->longname[pos - this->longname.c_str()] = '-';
  152. // We only register ourselves if our helpstring is not NULL. This
  153. // is to support the "no-VAR" boolean variables, which we
  154. // conditionally turn on by defining "no-VAR" help text.
  155. if (this->helpstring)
  156. this->register_option();
  157. }
  158. // This option takes an argument iff helparg is not NULL.
  159. bool
  160. takes_argument() const
  161. { return this->helparg != NULL; }
  162. // Whether the argument is optional.
  163. bool
  164. takes_optional_argument() const
  165. { return this->optional_arg; }
  166. // Register this option with the global list of options.
  167. void
  168. register_option();
  169. // Print this option to stdout (used with --help).
  170. void
  171. print() const;
  172. };
  173. // All options have a Struct_##varname that inherits from this and
  174. // actually implements parse_to_value for that option.
  175. struct Struct_var
  176. {
  177. // OPTION: the name of the option as specified on the commandline,
  178. // including leading dashes, and any text following the option:
  179. // "-O", "--defsym=mysym=0x1000", etc.
  180. // ARG: the arg associated with this option, or NULL if the option
  181. // takes no argument: "2", "mysym=0x1000", etc.
  182. // CMDLINE: the global Command_line object. Used by DEFINE_special.
  183. // OPTIONS: the global General_options object. Used by DEFINE_special.
  184. virtual void
  185. parse_to_value(const char* option, const char* arg,
  186. Command_line* cmdline, General_options* options) = 0;
  187. virtual
  188. ~Struct_var() // To make gcc happy.
  189. { }
  190. };
  191. // This is for "special" options that aren't of any predefined type.
  192. struct Struct_special : public Struct_var
  193. {
  194. // If you change this, change the parse-fn in DEFINE_special as well.
  195. typedef void (General_options::*Parse_function)(const char*, const char*,
  196. Command_line*);
  197. Struct_special(const char* varname, Dashes dashes, char shortname,
  198. Parse_function parse_function,
  199. const char* helpstring, const char* helparg)
  200. : option(varname, dashes, shortname, "", helpstring, helparg, false, this),
  201. parse(parse_function)
  202. { }
  203. void parse_to_value(const char* option, const char* arg,
  204. Command_line* cmdline, General_options* options)
  205. { (options->*(this->parse))(option, arg, cmdline); }
  206. One_option option;
  207. Parse_function parse;
  208. };
  209. } // End namespace options.
  210. // These are helper macros use by DEFINE_uint64/etc below.
  211. // This macro is used inside the General_options_ class, so defines
  212. // var() and set_var() as General_options methods. Arguments as are
  213. // for the constructor for One_option. param_type__ is the same as
  214. // type__ for built-in types, and "const type__ &" otherwise.
  215. //
  216. // When we define the linker command option "assert", the macro argument
  217. // varname__ of DEFINE_var below will be replaced by "assert". On Mac OSX
  218. // assert.h is included implicitly by one of the library headers we use. To
  219. // avoid unintended macro substitution of "assert()", we need to enclose
  220. // varname__ with parenthese.
  221. #define DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  222. default_value_as_string__, helpstring__, helparg__, \
  223. optional_arg__, type__, param_type__, parse_fn__) \
  224. public: \
  225. param_type__ \
  226. (varname__)() const \
  227. { return this->varname__##_.value; } \
  228. \
  229. bool \
  230. user_set_##varname__() const \
  231. { return this->varname__##_.user_set_via_option; } \
  232. \
  233. void \
  234. set_user_set_##varname__() \
  235. { this->varname__##_.user_set_via_option = true; } \
  236. \
  237. private: \
  238. struct Struct_##varname__ : public options::Struct_var \
  239. { \
  240. Struct_##varname__() \
  241. : option(#varname__, dashes__, shortname__, default_value_as_string__, \
  242. helpstring__, helparg__, optional_arg__, this), \
  243. user_set_via_option(false), value(default_value__) \
  244. { } \
  245. \
  246. void \
  247. parse_to_value(const char* option_name, const char* arg, \
  248. Command_line*, General_options*) \
  249. { \
  250. parse_fn__(option_name, arg, &this->value); \
  251. this->user_set_via_option = true; \
  252. } \
  253. \
  254. options::One_option option; \
  255. bool user_set_via_option; \
  256. type__ value; \
  257. }; \
  258. Struct_##varname__ varname__##_; \
  259. void \
  260. set_##varname__(param_type__ value) \
  261. { this->varname__##_.value = value; }
  262. // These macros allow for easy addition of a new commandline option.
  263. // If no_helpstring__ is not NULL, then in addition to creating
  264. // VARNAME, we also create an option called no-VARNAME (or, for a -z
  265. // option, noVARNAME).
  266. #define DEFINE_bool(varname__, dashes__, shortname__, default_value__, \
  267. helpstring__, no_helpstring__) \
  268. DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  269. default_value__ ? "true" : "false", helpstring__, NULL, \
  270. false, bool, bool, options::parse_bool) \
  271. struct Struct_no_##varname__ : public options::Struct_var \
  272. { \
  273. Struct_no_##varname__() : option((dashes__ == options::DASH_Z \
  274. ? "no" #varname__ \
  275. : "no-" #varname__), \
  276. dashes__, '\0', \
  277. default_value__ ? "false" : "true", \
  278. no_helpstring__, NULL, false, this) \
  279. { } \
  280. \
  281. void \
  282. parse_to_value(const char*, const char*, \
  283. Command_line*, General_options* options) \
  284. { \
  285. options->set_##varname__(false); \
  286. options->set_user_set_##varname__(); \
  287. } \
  288. \
  289. options::One_option option; \
  290. }; \
  291. Struct_no_##varname__ no_##varname__##_initializer_
  292. #define DEFINE_enable(varname__, dashes__, shortname__, default_value__, \
  293. helpstring__, no_helpstring__) \
  294. DEFINE_var(enable_##varname__, dashes__, shortname__, default_value__, \
  295. default_value__ ? "true" : "false", helpstring__, NULL, \
  296. false, bool, bool, options::parse_bool) \
  297. struct Struct_disable_##varname__ : public options::Struct_var \
  298. { \
  299. Struct_disable_##varname__() : option("disable-" #varname__, \
  300. dashes__, '\0', \
  301. default_value__ ? "false" : "true", \
  302. no_helpstring__, NULL, false, this) \
  303. { } \
  304. \
  305. void \
  306. parse_to_value(const char*, const char*, \
  307. Command_line*, General_options* options) \
  308. { options->set_enable_##varname__(false); } \
  309. \
  310. options::One_option option; \
  311. }; \
  312. Struct_disable_##varname__ disable_##varname__##_initializer_
  313. #define DEFINE_int(varname__, dashes__, shortname__, default_value__, \
  314. helpstring__, helparg__) \
  315. DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  316. #default_value__, helpstring__, helparg__, false, \
  317. int, int, options::parse_int)
  318. #define DEFINE_uint(varname__, dashes__, shortname__, default_value__, \
  319. helpstring__, helparg__) \
  320. DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  321. #default_value__, helpstring__, helparg__, false, \
  322. int, int, options::parse_uint)
  323. #define DEFINE_uint64(varname__, dashes__, shortname__, default_value__, \
  324. helpstring__, helparg__) \
  325. DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  326. #default_value__, helpstring__, helparg__, false, \
  327. uint64_t, uint64_t, options::parse_uint64)
  328. #define DEFINE_double(varname__, dashes__, shortname__, default_value__, \
  329. helpstring__, helparg__) \
  330. DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  331. #default_value__, helpstring__, helparg__, false, \
  332. double, double, options::parse_double)
  333. #define DEFINE_percent(varname__, dashes__, shortname__, default_value__, \
  334. helpstring__, helparg__) \
  335. DEFINE_var(varname__, dashes__, shortname__, default_value__ / 100.0, \
  336. #default_value__, helpstring__, helparg__, false, \
  337. double, double, options::parse_percent)
  338. #define DEFINE_string(varname__, dashes__, shortname__, default_value__, \
  339. helpstring__, helparg__) \
  340. DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  341. default_value__, helpstring__, helparg__, false, \
  342. const char*, const char*, options::parse_string)
  343. // This is like DEFINE_string, but we convert each occurrence to a
  344. // Search_directory and store it in a vector. Thus we also have the
  345. // add_to_VARNAME() method, to append to the vector.
  346. #define DEFINE_dirlist(varname__, dashes__, shortname__, \
  347. helpstring__, helparg__) \
  348. DEFINE_var(varname__, dashes__, shortname__, , \
  349. "", helpstring__, helparg__, false, options::Dir_list, \
  350. const options::Dir_list&, options::parse_dirlist) \
  351. void \
  352. add_to_##varname__(const char* new_value) \
  353. { options::parse_dirlist(NULL, new_value, &this->varname__##_.value); } \
  354. void \
  355. add_search_directory_to_##varname__(const Search_directory& dir) \
  356. { this->varname__##_.value.push_back(dir); }
  357. // This is like DEFINE_string, but we store a set of strings.
  358. #define DEFINE_set(varname__, dashes__, shortname__, \
  359. helpstring__, helparg__) \
  360. DEFINE_var(varname__, dashes__, shortname__, , \
  361. "", helpstring__, helparg__, false, options::String_set, \
  362. const options::String_set&, options::parse_set) \
  363. public: \
  364. bool \
  365. any_##varname__() const \
  366. { return !this->varname__##_.value.empty(); } \
  367. \
  368. bool \
  369. is_##varname__(const char* symbol) const \
  370. { \
  371. return (!this->varname__##_.value.empty() \
  372. && (this->varname__##_.value.find(std::string(symbol)) \
  373. != this->varname__##_.value.end())); \
  374. } \
  375. \
  376. options::String_set::const_iterator \
  377. varname__##_begin() const \
  378. { return this->varname__##_.value.begin(); } \
  379. \
  380. options::String_set::const_iterator \
  381. varname__##_end() const \
  382. { return this->varname__##_.value.end(); }
  383. // When you have a list of possible values (expressed as string)
  384. // After helparg__ should come an initializer list, like
  385. // {"foo", "bar", "baz"}
  386. #define DEFINE_enum(varname__, dashes__, shortname__, default_value__, \
  387. helpstring__, helparg__, ...) \
  388. DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  389. default_value__, helpstring__, helparg__, false, \
  390. const char*, const char*, parse_choices_##varname__) \
  391. private: \
  392. static void parse_choices_##varname__(const char* option_name, \
  393. const char* arg, \
  394. const char** retval) { \
  395. const char* choices[] = __VA_ARGS__; \
  396. options::parse_choices(option_name, arg, retval, \
  397. choices, sizeof(choices) / sizeof(*choices)); \
  398. }
  399. // This is like DEFINE_bool, but VARNAME is the name of a different
  400. // option. This option becomes an alias for that one. INVERT is true
  401. // if this option is an inversion of the other one.
  402. #define DEFINE_bool_alias(option__, varname__, dashes__, shortname__, \
  403. helpstring__, no_helpstring__, invert__) \
  404. private: \
  405. struct Struct_##option__ : public options::Struct_var \
  406. { \
  407. Struct_##option__() \
  408. : option(#option__, dashes__, shortname__, "", helpstring__, \
  409. NULL, false, this) \
  410. { } \
  411. \
  412. void \
  413. parse_to_value(const char*, const char*, \
  414. Command_line*, General_options* options) \
  415. { \
  416. options->set_##varname__(!invert__); \
  417. options->set_user_set_##varname__(); \
  418. } \
  419. \
  420. options::One_option option; \
  421. }; \
  422. Struct_##option__ option__##_; \
  423. \
  424. struct Struct_no_##option__ : public options::Struct_var \
  425. { \
  426. Struct_no_##option__() \
  427. : option((dashes__ == options::DASH_Z \
  428. ? "no" #option__ \
  429. : "no-" #option__), \
  430. dashes__, '\0', "", no_helpstring__, \
  431. NULL, false, this) \
  432. { } \
  433. \
  434. void \
  435. parse_to_value(const char*, const char*, \
  436. Command_line*, General_options* options) \
  437. { \
  438. options->set_##varname__(invert__); \
  439. options->set_user_set_##varname__(); \
  440. } \
  441. \
  442. options::One_option option; \
  443. }; \
  444. Struct_no_##option__ no_##option__##_initializer_
  445. // This is like DEFINE_uint64, but VARNAME is the name of a different
  446. // option. This option becomes an alias for that one.
  447. #define DEFINE_uint64_alias(option__, varname__, dashes__, shortname__, \
  448. helpstring__, helparg__) \
  449. private: \
  450. struct Struct_##option__ : public options::Struct_var \
  451. { \
  452. Struct_##option__() \
  453. : option(#option__, dashes__, shortname__, "", helpstring__, \
  454. helparg__, false, this) \
  455. { } \
  456. \
  457. void \
  458. parse_to_value(const char* option_name, const char* arg, \
  459. Command_line*, General_options* options) \
  460. { \
  461. uint64_t value; \
  462. options::parse_uint64(option_name, arg, &value); \
  463. options->set_##varname__(value); \
  464. options->set_user_set_##varname__(); \
  465. } \
  466. \
  467. options::One_option option; \
  468. }; \
  469. Struct_##option__ option__##_;
  470. // This is used for non-standard flags. It defines no functions; it
  471. // just calls General_options::parse_VARNAME whenever the flag is
  472. // seen. We declare parse_VARNAME as a static member of
  473. // General_options; you are responsible for defining it there.
  474. // helparg__ should be NULL iff this special-option is a boolean.
  475. #define DEFINE_special(varname__, dashes__, shortname__, \
  476. helpstring__, helparg__) \
  477. private: \
  478. void parse_##varname__(const char* option, const char* arg, \
  479. Command_line* inputs); \
  480. struct Struct_##varname__ : public options::Struct_special \
  481. { \
  482. Struct_##varname__() \
  483. : options::Struct_special(#varname__, dashes__, shortname__, \
  484. &General_options::parse_##varname__, \
  485. helpstring__, helparg__) \
  486. { } \
  487. }; \
  488. Struct_##varname__ varname__##_initializer_
  489. // An option that takes an optional string argument. If the option is
  490. // used with no argument, the value will be the default, and
  491. // user_set_via_option will be true.
  492. #define DEFINE_optional_string(varname__, dashes__, shortname__, \
  493. default_value__, \
  494. helpstring__, helparg__) \
  495. DEFINE_var(varname__, dashes__, shortname__, default_value__, \
  496. default_value__, helpstring__, helparg__, true, \
  497. const char*, const char*, options::parse_optional_string)
  498. // A directory to search. For each directory we record whether it is
  499. // in the sysroot. We need to know this so that, if a linker script
  500. // is found within the sysroot, we will apply the sysroot to any files
  501. // named by that script.
  502. class Search_directory
  503. {
  504. public:
  505. // We need a default constructor because we put this in a
  506. // std::vector.
  507. Search_directory()
  508. : name_(NULL), put_in_sysroot_(false), is_in_sysroot_(false)
  509. { }
  510. // This is the usual constructor.
  511. Search_directory(const std::string& name, bool put_in_sysroot)
  512. : name_(name), put_in_sysroot_(put_in_sysroot), is_in_sysroot_(false)
  513. {
  514. if (this->name_.empty())
  515. this->name_ = ".";
  516. }
  517. // This is called if we have a sysroot. The sysroot is prefixed to
  518. // any entries for which put_in_sysroot_ is true. is_in_sysroot_ is
  519. // set to true for any enries which are in the sysroot (this will
  520. // naturally include any entries for which put_in_sysroot_ is true).
  521. // SYSROOT is the sysroot, CANONICAL_SYSROOT is the result of
  522. // passing SYSROOT to lrealpath.
  523. void
  524. add_sysroot(const char* sysroot, const char* canonical_sysroot);
  525. // Get the directory name.
  526. const std::string&
  527. name() const
  528. { return this->name_; }
  529. // Return whether this directory is in the sysroot.
  530. bool
  531. is_in_sysroot() const
  532. { return this->is_in_sysroot_; }
  533. // Return whether this is considered a system directory.
  534. bool
  535. is_system_directory() const
  536. { return this->put_in_sysroot_ || this->is_in_sysroot_; }
  537. private:
  538. // The directory name.
  539. std::string name_;
  540. // True if the sysroot should be added as a prefix for this
  541. // directory (if there is a sysroot). This is true for system
  542. // directories that we search by default.
  543. bool put_in_sysroot_;
  544. // True if this directory is in the sysroot (if there is a sysroot).
  545. // This is true if there is a sysroot and either 1) put_in_sysroot_
  546. // is true, or 2) the directory happens to be in the sysroot based
  547. // on a pathname comparison.
  548. bool is_in_sysroot_;
  549. };
  550. class General_options
  551. {
  552. private:
  553. // NOTE: For every option that you add here, also consider if you
  554. // should add it to Position_dependent_options.
  555. DEFINE_special(help, options::TWO_DASHES, '\0',
  556. N_("Report usage information"), NULL);
  557. DEFINE_special(version, options::TWO_DASHES, 'v',
  558. N_("Report version information"), NULL);
  559. DEFINE_special(V, options::EXACTLY_ONE_DASH, '\0',
  560. N_("Report version and target information"), NULL);
  561. // These options are sorted approximately so that for each letter in
  562. // the alphabet, we show the option whose shortname is that letter
  563. // (if any) and then every longname that starts with that letter (in
  564. // alphabetical order). For both, lowercase sorts before uppercase.
  565. // The -z options come last.
  566. DEFINE_bool(add_needed, options::TWO_DASHES, '\0', false,
  567. N_("Not supported"),
  568. N_("Do not copy DT_NEEDED tags from shared libraries"));
  569. DEFINE_bool_alias(allow_multiple_definition, muldefs, options::TWO_DASHES,
  570. '\0', N_("Allow multiple definitions of symbols"),
  571. N_("Do not allow multiple definitions"), false);
  572. DEFINE_bool(allow_shlib_undefined, options::TWO_DASHES, '\0', false,
  573. N_("Allow unresolved references in shared libraries"),
  574. N_("Do not allow unresolved references in shared libraries"));
  575. DEFINE_bool(as_needed, options::TWO_DASHES, '\0', false,
  576. N_("Only set DT_NEEDED for shared libraries if used"),
  577. N_("Always DT_NEEDED for shared libraries"));
  578. DEFINE_enum(assert, options::ONE_DASH, '\0', NULL,
  579. N_("Ignored"), N_("[ignored]"),
  580. {"definitions", "nodefinitions", "nosymbolic", "pure-text"});
  581. // This should really be an "enum", but it's too easy for folks to
  582. // forget to update the list as they add new targets. So we just
  583. // accept any string. We'll fail later (when the string is parsed),
  584. // if the target isn't actually supported.
  585. DEFINE_string(format, options::TWO_DASHES, 'b', "elf",
  586. N_("Set input format"), ("[elf,binary]"));
  587. DEFINE_bool(Bdynamic, options::ONE_DASH, '\0', true,
  588. N_("-l searches for shared libraries"), NULL);
  589. DEFINE_bool_alias(Bstatic, Bdynamic, options::ONE_DASH, '\0',
  590. N_("-l does not search for shared libraries"), NULL,
  591. true);
  592. DEFINE_bool_alias(dy, Bdynamic, options::ONE_DASH, '\0',
  593. N_("alias for -Bdynamic"), NULL, false);
  594. DEFINE_bool_alias(dn, Bdynamic, options::ONE_DASH, '\0',
  595. N_("alias for -Bstatic"), NULL, true);
  596. DEFINE_bool(Bgroup, options::ONE_DASH, '\0', false,
  597. N_("Use group name lookup rules for shared library"), NULL);
  598. DEFINE_bool(Bsymbolic, options::ONE_DASH, '\0', false,
  599. N_("Bind defined symbols locally"), NULL);
  600. DEFINE_bool(Bsymbolic_functions, options::ONE_DASH, '\0', false,
  601. N_("Bind defined function symbols locally"), NULL);
  602. DEFINE_optional_string(build_id, options::TWO_DASHES, '\0', "tree",
  603. N_("Generate build ID note"),
  604. N_("[=STYLE]"));
  605. DEFINE_uint64(build_id_chunk_size_for_treehash,
  606. options::TWO_DASHES, '\0', 2 << 20,
  607. N_("Chunk size for '--build-id=tree'"), N_("SIZE"));
  608. DEFINE_uint64(build_id_min_file_size_for_treehash, options::TWO_DASHES,
  609. '\0', 40 << 20,
  610. N_("Minimum output file size for '--build-id=tree' to work"
  611. " differently than '--build-id=sha1'"), N_("SIZE"));
  612. DEFINE_bool(check_sections, options::TWO_DASHES, '\0', true,
  613. N_("Check segment addresses for overlaps (default)"),
  614. N_("Do not check segment addresses for overlaps"));
  615. DEFINE_enum(compress_debug_sections, options::TWO_DASHES, '\0', "none",
  616. N_("Compress .debug_* sections in the output file"),
  617. ("[none,zlib,zlib-gnu,zlib-gabi]"),
  618. {"none", "zlib", "zlib-gnu", "zlib-gabi"});
  619. DEFINE_bool(copy_dt_needed_entries, options::TWO_DASHES, '\0', false,
  620. N_("Not supported"),
  621. N_("Do not copy DT_NEEDED tags from shared libraries"));
  622. DEFINE_bool(cref, options::TWO_DASHES, '\0', false,
  623. N_("Output cross reference table"),
  624. N_("Do not output cross reference table"));
  625. DEFINE_bool(ctors_in_init_array, options::TWO_DASHES, '\0', true,
  626. N_("Use DT_INIT_ARRAY for all constructors (default)"),
  627. N_("Handle constructors as directed by compiler"));
  628. DEFINE_bool(define_common, options::TWO_DASHES, 'd', false,
  629. N_("Define common symbols"),
  630. N_("Do not define common symbols"));
  631. DEFINE_bool(dc, options::ONE_DASH, '\0', false,
  632. N_("Alias for -d"), NULL);
  633. DEFINE_bool(dp, options::ONE_DASH, '\0', false,
  634. N_("Alias for -d"), NULL);
  635. DEFINE_string(debug, options::TWO_DASHES, '\0', "",
  636. N_("Turn on debugging"),
  637. N_("[all,files,script,task][,...]"));
  638. DEFINE_special(defsym, options::TWO_DASHES, '\0',
  639. N_("Define a symbol"), N_("SYMBOL=EXPRESSION"));
  640. DEFINE_optional_string(demangle, options::TWO_DASHES, '\0', NULL,
  641. N_("Demangle C++ symbols in log messages"),
  642. N_("[=STYLE]"));
  643. DEFINE_bool(no_demangle, options::TWO_DASHES, '\0', false,
  644. N_("Do not demangle C++ symbols in log messages"),
  645. NULL);
  646. DEFINE_bool(detect_odr_violations, options::TWO_DASHES, '\0', false,
  647. N_("Look for violations of the C++ One Definition Rule"),
  648. N_("Do not look for violations of the C++ One Definition Rule"));
  649. DEFINE_special(discard_all, options::TWO_DASHES, 'x',
  650. N_("Delete all local symbols"), NULL);
  651. DEFINE_special(discard_locals, options::TWO_DASHES, 'X',
  652. N_("Delete all temporary local symbols"), NULL);
  653. DEFINE_special(discard_none, options::TWO_DASHES, '\0',
  654. N_("Keep all local symbols"), NULL);
  655. DEFINE_bool(dynamic_list_data, options::TWO_DASHES, '\0', false,
  656. N_("Add data symbols to dynamic symbols"), NULL);
  657. DEFINE_bool(dynamic_list_cpp_new, options::TWO_DASHES, '\0', false,
  658. N_("Add C++ operator new/delete to dynamic symbols"), NULL);
  659. DEFINE_bool(dynamic_list_cpp_typeinfo, options::TWO_DASHES, '\0', false,
  660. N_("Add C++ typeinfo to dynamic symbols"), NULL);
  661. DEFINE_special(dynamic_list, options::TWO_DASHES, '\0',
  662. N_("Read a list of dynamic symbols"), N_("FILE"));
  663. DEFINE_string(entry, options::TWO_DASHES, 'e', NULL,
  664. N_("Set program start address"), N_("ADDRESS"));
  665. DEFINE_special(exclude_libs, options::TWO_DASHES, '\0',
  666. N_("Exclude libraries from automatic export"),
  667. N_(("lib,lib ...")));
  668. DEFINE_bool(export_dynamic, options::TWO_DASHES, 'E', false,
  669. N_("Export all dynamic symbols"),
  670. N_("Do not export all dynamic symbols (default)"));
  671. DEFINE_set(export_dynamic_symbol, options::TWO_DASHES, '\0',
  672. N_("Export SYMBOL to dynamic symbol table"), N_("SYMBOL"));
  673. DEFINE_special(EB, options::ONE_DASH, '\0',
  674. N_("Link big-endian objects."), NULL);
  675. DEFINE_special(EL, options::ONE_DASH, '\0',
  676. N_("Link little-endian objects."), NULL);
  677. DEFINE_bool(eh_frame_hdr, options::TWO_DASHES, '\0', false,
  678. N_("Create exception frame header"), NULL);
  679. DEFINE_bool(enum_size_warning, options::TWO_DASHES, '\0', true, NULL,
  680. N_("(ARM only) Do not warn about objects with incompatible "
  681. "enum sizes"));
  682. DEFINE_set(auxiliary, options::TWO_DASHES, 'f',
  683. N_("Auxiliary filter for shared object symbol table"),
  684. N_("SHLIB"));
  685. DEFINE_string(filter, options::TWO_DASHES, 'F', NULL,
  686. N_("Filter for shared object symbol table"),
  687. N_("SHLIB"));
  688. DEFINE_bool(fatal_warnings, options::TWO_DASHES, '\0', false,
  689. N_("Treat warnings as errors"),
  690. N_("Do not treat warnings as errors"));
  691. DEFINE_string(fini, options::ONE_DASH, '\0', "_fini",
  692. N_("Call SYMBOL at unload-time"), N_("SYMBOL"));
  693. DEFINE_bool(fix_cortex_a8, options::TWO_DASHES, '\0', false,
  694. N_("(ARM only) Fix binaries for Cortex-A8 erratum."),
  695. N_("(ARM only) Do not fix binaries for Cortex-A8 erratum."));
  696. DEFINE_bool(fix_cortex_a53_843419, options::TWO_DASHES, '\0', false,
  697. N_("(AArch64 only) Fix Cortex-A53 erratum 843419."),
  698. N_("(AArch64 only) Do not fix Cortex-A53 erratum 843419."));
  699. DEFINE_bool(fix_cortex_a53_835769, options::TWO_DASHES, '\0', false,
  700. N_("(AArch64 only) Fix Cortex-A53 erratum 835769."),
  701. N_("(AArch64 only) Do not fix Cortex-A53 erratum 835769."));
  702. DEFINE_bool(fix_arm1176, options::TWO_DASHES, '\0', true,
  703. N_("(ARM only) Fix binaries for ARM1176 erratum."),
  704. N_("(ARM only) Do not fix binaries for ARM1176 erratum."));
  705. DEFINE_bool(merge_exidx_entries, options::TWO_DASHES, '\0', true,
  706. N_("(ARM only) Merge exidx entries in debuginfo."),
  707. N_("(ARM only) Do not merge exidx entries in debuginfo."));
  708. DEFINE_special(fix_v4bx, options::TWO_DASHES, '\0',
  709. N_("(ARM only) Rewrite BX rn as MOV pc, rn for ARMv4"),
  710. NULL);
  711. DEFINE_special(fix_v4bx_interworking, options::TWO_DASHES, '\0',
  712. N_("(ARM only) Rewrite BX rn branch to ARMv4 interworking "
  713. "veneer"),
  714. NULL);
  715. DEFINE_bool(g, options::EXACTLY_ONE_DASH, '\0', false,
  716. N_("Ignored"), NULL);
  717. DEFINE_bool(gdb_index, options::TWO_DASHES, '\0', false,
  718. N_("Generate .gdb_index section"),
  719. N_("Do not generate .gdb_index section"));
  720. DEFINE_bool(gnu_unique, options::TWO_DASHES, '\0', true,
  721. N_("Enable STB_GNU_UNIQUE symbol binding (default)"),
  722. N_("Disable STB_GNU_UNIQUE symbol binding"));
  723. DEFINE_string(soname, options::ONE_DASH, 'h', NULL,
  724. N_("Set shared library name"), N_("FILENAME"));
  725. DEFINE_double(hash_bucket_empty_fraction, options::TWO_DASHES, '\0', 0.0,
  726. N_("Min fraction of empty buckets in dynamic hash"),
  727. N_("FRACTION"));
  728. DEFINE_enum(hash_style, options::TWO_DASHES, '\0', "sysv",
  729. N_("Dynamic hash style"), N_("[sysv,gnu,both]"),
  730. {"sysv", "gnu", "both"});
  731. DEFINE_string(dynamic_linker, options::TWO_DASHES, 'I', NULL,
  732. N_("Set dynamic linker path"), N_("PROGRAM"));
  733. DEFINE_special(incremental, options::TWO_DASHES, '\0',
  734. N_("Do an incremental link if possible; "
  735. "otherwise, do a full link and prepare output "
  736. "for incremental linking"), NULL);
  737. DEFINE_special(no_incremental, options::TWO_DASHES, '\0',
  738. N_("Do a full link (default)"), NULL);
  739. DEFINE_special(incremental_full, options::TWO_DASHES, '\0',
  740. N_("Do a full link and "
  741. "prepare output for incremental linking"), NULL);
  742. DEFINE_special(incremental_update, options::TWO_DASHES, '\0',
  743. N_("Do an incremental link; exit if not possible"), NULL);
  744. DEFINE_string(incremental_base, options::TWO_DASHES, '\0', NULL,
  745. N_("Set base file for incremental linking"
  746. " (default is output file)"),
  747. N_("FILE"));
  748. DEFINE_special(incremental_changed, options::TWO_DASHES, '\0',
  749. N_("Assume files changed"), NULL);
  750. DEFINE_special(incremental_unchanged, options::TWO_DASHES, '\0',
  751. N_("Assume files didn't change"), NULL);
  752. DEFINE_special(incremental_unknown, options::TWO_DASHES, '\0',
  753. N_("Use timestamps to check files (default)"), NULL);
  754. DEFINE_special(incremental_startup_unchanged, options::TWO_DASHES, '\0',
  755. N_("Assume startup files unchanged "
  756. "(files preceding this option)"), NULL);
  757. DEFINE_percent(incremental_patch, options::TWO_DASHES, '\0', 10,
  758. N_("Amount of extra space to allocate for patches"),
  759. N_("PERCENT"));
  760. DEFINE_string(init, options::ONE_DASH, '\0', "_init",
  761. N_("Call SYMBOL at load-time"), N_("SYMBOL"));
  762. DEFINE_special(just_symbols, options::TWO_DASHES, '\0',
  763. N_("Read only symbol values from FILE"), N_("FILE"));
  764. DEFINE_bool(map_whole_files, options::TWO_DASHES, '\0',
  765. sizeof(void*) >= 8,
  766. N_("Map whole files to memory (default on 64-bit hosts)"),
  767. N_("Map relevant file parts to memory (default on 32-bit "
  768. "hosts)"));
  769. DEFINE_bool(keep_files_mapped, options::TWO_DASHES, '\0', true,
  770. N_("Keep files mapped across passes (default)"),
  771. N_("Release mapped files after each pass"));
  772. DEFINE_bool(ld_generated_unwind_info, options::TWO_DASHES, '\0', true,
  773. N_("Generate unwind information for PLT (default)"),
  774. N_("Do not generate unwind information for PLT"));
  775. DEFINE_special(library, options::TWO_DASHES, 'l',
  776. N_("Search for library LIBNAME"), N_("LIBNAME"));
  777. DEFINE_dirlist(library_path, options::TWO_DASHES, 'L',
  778. N_("Add directory to search path"), N_("DIR"));
  779. DEFINE_bool(text_reorder, options::TWO_DASHES, '\0', true,
  780. N_("Enable text section reordering for GCC section names "
  781. "(default)"),
  782. N_("Disable text section reordering for GCC section names"));
  783. DEFINE_bool(nostdlib, options::ONE_DASH, '\0', false,
  784. N_("Only search directories specified on the command line."),
  785. NULL);
  786. DEFINE_bool(rosegment, options::TWO_DASHES, '\0', false,
  787. N_("Put read-only non-executable sections in their own segment"),
  788. NULL);
  789. DEFINE_uint64(rosegment_gap, options::TWO_DASHES, '\0', -1U,
  790. N_("Set offset between executable and read-only segments"),
  791. N_("OFFSET"));
  792. DEFINE_string(m, options::EXACTLY_ONE_DASH, 'm', "",
  793. N_("Set GNU linker emulation"), N_("EMULATION"));
  794. DEFINE_bool(mmap_output_file, options::TWO_DASHES, '\0', true,
  795. N_("Map the output file for writing (default)."),
  796. N_("Do not map the output file for writing."));
  797. DEFINE_bool(print_map, options::TWO_DASHES, 'M', false,
  798. N_("Write map file on standard output"), NULL);
  799. DEFINE_string(Map, options::ONE_DASH, '\0', NULL, N_("Write map file"),
  800. N_("MAPFILENAME"));
  801. DEFINE_bool(nmagic, options::TWO_DASHES, 'n', false,
  802. N_("Do not page align data"), NULL);
  803. DEFINE_bool(omagic, options::EXACTLY_TWO_DASHES, 'N', false,
  804. N_("Do not page align data, do not make text readonly"),
  805. N_("Page align data, make text readonly"));
  806. DEFINE_enable(new_dtags, options::EXACTLY_TWO_DASHES, '\0', true,
  807. N_("Enable use of DT_RUNPATH and DT_FLAGS"),
  808. N_("Disable use of DT_RUNPATH and DT_FLAGS"));
  809. DEFINE_bool(noinhibit_exec, options::TWO_DASHES, '\0', false,
  810. N_("Create an output file even if errors occur"), NULL);
  811. DEFINE_bool_alias(no_undefined, defs, options::TWO_DASHES, '\0',
  812. N_("Report undefined symbols (even with --shared)"),
  813. NULL, false);
  814. DEFINE_string(output, options::TWO_DASHES, 'o', "a.out",
  815. N_("Set output file name"), N_("FILE"));
  816. DEFINE_uint(optimize, options::EXACTLY_ONE_DASH, 'O', 0,
  817. N_("Optimize output file size"), N_("LEVEL"));
  818. DEFINE_string(oformat, options::EXACTLY_TWO_DASHES, '\0', "elf",
  819. N_("Set output format"), N_("[binary]"));
  820. DEFINE_bool(p, options::ONE_DASH, '\0', false,
  821. N_("(ARM only) Ignore for backward compatibility"), NULL);
  822. DEFINE_bool(pie, options::ONE_DASH, '\0', false,
  823. N_("Create a position independent executable"),
  824. N_("Do not create a position independent executable"));
  825. DEFINE_bool_alias(pic_executable, pie, options::TWO_DASHES, '\0',
  826. N_("Create a position independent executable"),
  827. N_("Do not create a position independent executable"),
  828. false);
  829. DEFINE_bool(pic_veneer, options::TWO_DASHES, '\0', false,
  830. N_("Force PIC sequences for ARM/Thumb interworking veneers"),
  831. NULL);
  832. DEFINE_bool(pipeline_knowledge, options::ONE_DASH, '\0', false,
  833. NULL, N_("(ARM only) Ignore for backward compatibility"));
  834. DEFINE_var(plt_align, options::TWO_DASHES, '\0', 0, "5",
  835. N_("(PowerPC64 only) Align PLT call stubs to fit cache lines"),
  836. N_("[=P2ALIGN]"), true, int, int, options::parse_uint);
  837. DEFINE_bool(plt_static_chain, options::TWO_DASHES, '\0', false,
  838. N_("(PowerPC64 only) PLT call stubs should load r11"),
  839. N_("(PowerPC64 only) PLT call stubs should not load r11"));
  840. DEFINE_bool(plt_thread_safe, options::TWO_DASHES, '\0', false,
  841. N_("(PowerPC64 only) PLT call stubs with load-load barrier"),
  842. N_("(PowerPC64 only) PLT call stubs without barrier"));
  843. #ifdef ENABLE_PLUGINS
  844. DEFINE_special(plugin, options::TWO_DASHES, '\0',
  845. N_("Load a plugin library"), N_("PLUGIN"));
  846. DEFINE_special(plugin_opt, options::TWO_DASHES, '\0',
  847. N_("Pass an option to the plugin"), N_("OPTION"));
  848. #endif
  849. DEFINE_bool(posix_fallocate, options::TWO_DASHES, '\0', true,
  850. N_("Use posix_fallocate to reserve space in the output file"
  851. " (default)."),
  852. N_("Use fallocate or ftruncate to reserve space."));
  853. DEFINE_bool(preread_archive_symbols, options::TWO_DASHES, '\0', false,
  854. N_("Preread archive symbols when multi-threaded"), NULL);
  855. DEFINE_bool(print_output_format, options::TWO_DASHES, '\0', false,
  856. N_("Print default output format"), NULL);
  857. DEFINE_string(print_symbol_counts, options::TWO_DASHES, '\0', NULL,
  858. N_("Print symbols defined and used for each input"),
  859. N_("FILENAME"));
  860. DEFINE_bool(Qy, options::EXACTLY_ONE_DASH, '\0', false,
  861. N_("Ignored for SVR4 compatibility"), NULL);
  862. DEFINE_bool(emit_relocs, options::TWO_DASHES, 'q', false,
  863. N_("Generate relocations in output"), NULL);
  864. DEFINE_bool(relocatable, options::EXACTLY_ONE_DASH, 'r', false,
  865. N_("Generate relocatable output"), NULL);
  866. DEFINE_bool_alias(i, relocatable, options::EXACTLY_ONE_DASH, '\0',
  867. N_("Synonym for -r"), NULL, false);
  868. DEFINE_bool(relax, options::TWO_DASHES, '\0', false,
  869. N_("Relax branches on certain targets"), NULL);
  870. DEFINE_string(retain_symbols_file, options::TWO_DASHES, '\0', NULL,
  871. N_("keep only symbols listed in this file"), N_("FILE"));
  872. // -R really means -rpath, but can mean --just-symbols for
  873. // compatibility with GNU ld. -rpath is always -rpath, so we list
  874. // it separately.
  875. DEFINE_special(R, options::EXACTLY_ONE_DASH, 'R',
  876. N_("Add DIR to runtime search path"), N_("DIR"));
  877. DEFINE_dirlist(rpath, options::ONE_DASH, '\0',
  878. N_("Add DIR to runtime search path"), N_("DIR"));
  879. DEFINE_dirlist(rpath_link, options::TWO_DASHES, '\0',
  880. N_("Add DIR to link time shared library search path"),
  881. N_("DIR"));
  882. DEFINE_string(section_ordering_file, options::TWO_DASHES, '\0', NULL,
  883. N_("Layout sections in the order specified."),
  884. N_("FILENAME"));
  885. DEFINE_special(section_start, options::TWO_DASHES, '\0',
  886. N_("Set address of section"), N_("SECTION=ADDRESS"));
  887. DEFINE_optional_string(sort_common, options::TWO_DASHES, '\0', NULL,
  888. N_("Sort common symbols by alignment"),
  889. N_("[={ascending,descending}]"));
  890. DEFINE_enum(sort_section, options::TWO_DASHES, '\0', "none",
  891. N_("Sort sections by name. \'--no-text-reorder\'"
  892. " will override \'--sort-section=name\' for .text"),
  893. N_("[none,name]"),
  894. {"none", "name"});
  895. DEFINE_uint(spare_dynamic_tags, options::TWO_DASHES, '\0', 5,
  896. N_("Dynamic tag slots to reserve (default 5)"),
  897. N_("COUNT"));
  898. DEFINE_bool(strip_all, options::TWO_DASHES, 's', false,
  899. N_("Strip all symbols"), NULL);
  900. DEFINE_bool(strip_debug, options::TWO_DASHES, 'S', false,
  901. N_("Strip debugging information"), NULL);
  902. DEFINE_bool(strip_debug_non_line, options::TWO_DASHES, '\0', false,
  903. N_("Emit only debug line number information"), NULL);
  904. DEFINE_bool(strip_debug_gdb, options::TWO_DASHES, '\0', false,
  905. N_("Strip debug symbols that are unused by gdb "
  906. "(at least versions <= 7.4)"), NULL);
  907. DEFINE_bool(strip_lto_sections, options::TWO_DASHES, '\0', true,
  908. N_("Strip LTO intermediate code sections"), NULL);
  909. DEFINE_int(stub_group_size, options::TWO_DASHES , '\0', 1,
  910. N_("(ARM, PowerPC only) The maximum distance from instructions "
  911. "in a group of sections to their stubs. Negative values mean "
  912. "stubs are always after (PowerPC before) the group. 1 means "
  913. "use default size.\n"),
  914. N_("SIZE"));
  915. DEFINE_bool(no_keep_memory, options::TWO_DASHES, '\0', false,
  916. N_("Use less memory and more disk I/O "
  917. "(included only for compatibility with GNU ld)"), NULL);
  918. DEFINE_bool(shared, options::ONE_DASH, 'G', false,
  919. N_("Generate shared library"), NULL);
  920. DEFINE_bool(Bshareable, options::ONE_DASH, '\0', false,
  921. N_("Generate shared library"), NULL);
  922. DEFINE_uint(split_stack_adjust_size, options::TWO_DASHES, '\0', 0x4000,
  923. N_("Stack size when -fsplit-stack function calls non-split"),
  924. N_("SIZE"));
  925. // This is not actually special in any way, but I need to give it
  926. // a non-standard accessor-function name because 'static' is a keyword.
  927. DEFINE_special(static, options::ONE_DASH, '\0',
  928. N_("Do not link against shared libraries"), NULL);
  929. DEFINE_enum(icf, options::TWO_DASHES, '\0', "none",
  930. N_("Identical Code Folding. "
  931. "\'--icf=safe\' Folds ctors, dtors and functions whose"
  932. " pointers are definitely not taken."),
  933. ("[none,all,safe]"),
  934. {"none", "all", "safe"});
  935. DEFINE_uint(icf_iterations, options::TWO_DASHES , '\0', 0,
  936. N_("Number of iterations of ICF (default 2)"), N_("COUNT"));
  937. DEFINE_bool(print_icf_sections, options::TWO_DASHES, '\0', false,
  938. N_("List folded identical sections on stderr"),
  939. N_("Do not list folded identical sections"));
  940. DEFINE_set(keep_unique, options::TWO_DASHES, '\0',
  941. N_("Do not fold this symbol during ICF"), N_("SYMBOL"));
  942. DEFINE_bool(gc_sections, options::TWO_DASHES, '\0', false,
  943. N_("Remove unused sections"),
  944. N_("Don't remove unused sections (default)"));
  945. DEFINE_bool(print_gc_sections, options::TWO_DASHES, '\0', false,
  946. N_("List removed unused sections on stderr"),
  947. N_("Do not list removed unused sections"));
  948. DEFINE_bool(stats, options::TWO_DASHES, '\0', false,
  949. N_("Print resource usage statistics"), NULL);
  950. DEFINE_string(sysroot, options::TWO_DASHES, '\0', "",
  951. N_("Set target system root directory"), N_("DIR"));
  952. DEFINE_bool(trace, options::TWO_DASHES, 't', false,
  953. N_("Print the name of each input file"), NULL);
  954. DEFINE_special(script, options::TWO_DASHES, 'T',
  955. N_("Read linker script"), N_("FILE"));
  956. DEFINE_bool(threads, options::TWO_DASHES, '\0', false,
  957. N_("Run the linker multi-threaded"),
  958. N_("Do not run the linker multi-threaded"));
  959. DEFINE_uint(thread_count, options::TWO_DASHES, '\0', 0,
  960. N_("Number of threads to use"), N_("COUNT"));
  961. DEFINE_uint(thread_count_initial, options::TWO_DASHES, '\0', 0,
  962. N_("Number of threads to use in initial pass"), N_("COUNT"));
  963. DEFINE_uint(thread_count_middle, options::TWO_DASHES, '\0', 0,
  964. N_("Number of threads to use in middle pass"), N_("COUNT"));
  965. DEFINE_uint(thread_count_final, options::TWO_DASHES, '\0', 0,
  966. N_("Number of threads to use in final pass"), N_("COUNT"));
  967. DEFINE_uint64(Tbss, options::ONE_DASH, '\0', -1U,
  968. N_("Set the address of the bss segment"), N_("ADDRESS"));
  969. DEFINE_uint64(Tdata, options::ONE_DASH, '\0', -1U,
  970. N_("Set the address of the data segment"), N_("ADDRESS"));
  971. DEFINE_uint64(Ttext, options::ONE_DASH, '\0', -1U,
  972. N_("Set the address of the text segment"), N_("ADDRESS"));
  973. DEFINE_uint64_alias(Ttext_segment, Ttext, options::ONE_DASH, '\0',
  974. N_("Set the address of the text segment"),
  975. N_("ADDRESS"));
  976. DEFINE_uint64(Trodata_segment, options::ONE_DASH, '\0', -1U,
  977. N_("Set the address of the rodata segment"), N_("ADDRESS"));
  978. DEFINE_bool(toc_optimize, options::TWO_DASHES, '\0', true,
  979. N_("(PowerPC64 only) Optimize TOC code sequences"),
  980. N_("(PowerPC64 only) Don't optimize TOC code sequences"));
  981. DEFINE_bool(toc_sort, options::TWO_DASHES, '\0', true,
  982. N_("(PowerPC64 only) Sort TOC and GOT sections"),
  983. N_("(PowerPC64 only) Don't sort TOC and GOT sections"));
  984. DEFINE_set(undefined, options::TWO_DASHES, 'u',
  985. N_("Create undefined reference to SYMBOL"), N_("SYMBOL"));
  986. DEFINE_enum(unresolved_symbols, options::TWO_DASHES, '\0', NULL,
  987. N_("How to handle unresolved symbols"),
  988. ("ignore-all,report-all,ignore-in-object-files,"
  989. "ignore-in-shared-libs"),
  990. {"ignore-all", "report-all", "ignore-in-object-files",
  991. "ignore-in-shared-libs"});
  992. DEFINE_bool(verbose, options::TWO_DASHES, '\0', false,
  993. N_("Synonym for --debug=files"), NULL);
  994. DEFINE_special(version_script, options::TWO_DASHES, '\0',
  995. N_("Read version script"), N_("FILE"));
  996. DEFINE_bool(warn_common, options::TWO_DASHES, '\0', false,
  997. N_("Warn about duplicate common symbols"),
  998. N_("Do not warn about duplicate common symbols (default)"));
  999. DEFINE_bool(warn_constructors, options::TWO_DASHES, '\0', false,
  1000. N_("Ignored"), N_("Ignored"));
  1001. DEFINE_bool(warn_execstack, options::TWO_DASHES, '\0', false,
  1002. N_("Warn if the stack is executable"),
  1003. N_("Do not warn if the stack is executable (default)"));
  1004. DEFINE_bool(warn_mismatch, options::TWO_DASHES, '\0', true,
  1005. NULL, N_("Don't warn about mismatched input files"));
  1006. DEFINE_bool(warn_multiple_gp, options::TWO_DASHES, '\0', false,
  1007. N_("Ignored"), NULL);
  1008. DEFINE_bool(warn_search_mismatch, options::TWO_DASHES, '\0', true,
  1009. N_("Warn when skipping an incompatible library"),
  1010. N_("Don't warn when skipping an incompatible library"));
  1011. DEFINE_bool(warn_shared_textrel, options::TWO_DASHES, '\0', false,
  1012. N_("Warn if text segment is not shareable"),
  1013. N_("Do not warn if text segment is not shareable (default)"));
  1014. DEFINE_bool(warn_unresolved_symbols, options::TWO_DASHES, '\0', false,
  1015. N_("Report unresolved symbols as warnings"),
  1016. NULL);
  1017. DEFINE_bool_alias(error_unresolved_symbols, warn_unresolved_symbols,
  1018. options::TWO_DASHES, '\0',
  1019. N_("Report unresolved symbols as errors"),
  1020. NULL, true);
  1021. DEFINE_bool(weak_unresolved_symbols, options::TWO_DASHES, '\0', false,
  1022. N_("Convert unresolved symbols to weak references"),
  1023. NULL);
  1024. DEFINE_bool(wchar_size_warning, options::TWO_DASHES, '\0', true, NULL,
  1025. N_("(ARM only) Do not warn about objects with incompatible "
  1026. "wchar_t sizes"));
  1027. DEFINE_bool(whole_archive, options::TWO_DASHES, '\0', false,
  1028. N_("Include all archive contents"),
  1029. N_("Include only needed archive contents"));
  1030. DEFINE_set(wrap, options::TWO_DASHES, '\0',
  1031. N_("Use wrapper functions for SYMBOL"), N_("SYMBOL"));
  1032. DEFINE_set(trace_symbol, options::TWO_DASHES, 'y',
  1033. N_("Trace references to symbol"), N_("SYMBOL"));
  1034. DEFINE_bool(undefined_version, options::TWO_DASHES, '\0', true,
  1035. N_("Allow unused version in script (default)"),
  1036. N_("Do not allow unused version in script"));
  1037. DEFINE_string(Y, options::EXACTLY_ONE_DASH, 'Y', "",
  1038. N_("Default search path for Solaris compatibility"),
  1039. N_("PATH"));
  1040. DEFINE_special(start_group, options::TWO_DASHES, '(',
  1041. N_("Start a library search group"), NULL);
  1042. DEFINE_special(end_group, options::TWO_DASHES, ')',
  1043. N_("End a library search group"), NULL);
  1044. DEFINE_special(start_lib, options::TWO_DASHES, '\0',
  1045. N_("Start a library"), NULL);
  1046. DEFINE_special(end_lib, options::TWO_DASHES, '\0',
  1047. N_("End a library "), NULL);
  1048. DEFINE_string(fuse_ld, options::ONE_DASH, '\0', "",
  1049. N_("Ignored for GCC linker option compatibility"),
  1050. "");
  1051. // The -z options.
  1052. DEFINE_bool(combreloc, options::DASH_Z, '\0', true,
  1053. N_("Sort dynamic relocs"),
  1054. N_("Do not sort dynamic relocs"));
  1055. DEFINE_uint64(common_page_size, options::DASH_Z, '\0', 0,
  1056. N_("Set common page size to SIZE"), N_("SIZE"));
  1057. DEFINE_bool(defs, options::DASH_Z, '\0', false,
  1058. N_("Report undefined symbols (even with --shared)"),
  1059. NULL);
  1060. DEFINE_bool(execstack, options::DASH_Z, '\0', false,
  1061. N_("Mark output as requiring executable stack"), NULL);
  1062. DEFINE_bool(global, options::DASH_Z, '\0', false,
  1063. N_("Make symbols in DSO available for subsequently loaded "
  1064. "objects"), NULL);
  1065. DEFINE_bool(initfirst, options::DASH_Z, '\0', false,
  1066. N_("Mark DSO to be initialized first at runtime"),
  1067. NULL);
  1068. DEFINE_bool(interpose, options::DASH_Z, '\0', false,
  1069. N_("Mark object to interpose all DSOs but executable"),
  1070. NULL);
  1071. DEFINE_bool_alias(lazy, now, options::DASH_Z, '\0',
  1072. N_("Mark object for lazy runtime binding (default)"),
  1073. NULL, true);
  1074. DEFINE_bool(loadfltr, options::DASH_Z, '\0', false,
  1075. N_("Mark object requiring immediate process"),
  1076. NULL);
  1077. DEFINE_uint64(max_page_size, options::DASH_Z, '\0', 0,
  1078. N_("Set maximum page size to SIZE"), N_("SIZE"));
  1079. DEFINE_bool(muldefs, options::DASH_Z, '\0', false,
  1080. N_("Allow multiple definitions of symbols"),
  1081. NULL);
  1082. // copyreloc is here in the list because there is only -z
  1083. // nocopyreloc, not -z copyreloc.
  1084. DEFINE_bool(copyreloc, options::DASH_Z, '\0', true,
  1085. NULL,
  1086. N_("Do not create copy relocs"));
  1087. DEFINE_bool(nodefaultlib, options::DASH_Z, '\0', false,
  1088. N_("Mark object not to use default search paths"),
  1089. NULL);
  1090. DEFINE_bool(nodelete, options::DASH_Z, '\0', false,
  1091. N_("Mark DSO non-deletable at runtime"),
  1092. NULL);
  1093. DEFINE_bool(nodlopen, options::DASH_Z, '\0', false,
  1094. N_("Mark DSO not available to dlopen"),
  1095. NULL);
  1096. DEFINE_bool(nodump, options::DASH_Z, '\0', false,
  1097. N_("Mark DSO not available to dldump"),
  1098. NULL);
  1099. DEFINE_bool(noexecstack, options::DASH_Z, '\0', false,
  1100. N_("Mark output as not requiring executable stack"), NULL);
  1101. DEFINE_bool(now, options::DASH_Z, '\0', false,
  1102. N_("Mark object for immediate function binding"),
  1103. NULL);
  1104. DEFINE_bool(origin, options::DASH_Z, '\0', false,
  1105. N_("Mark DSO to indicate that needs immediate $ORIGIN "
  1106. "processing at runtime"), NULL);
  1107. DEFINE_bool(relro, options::DASH_Z, '\0', false,
  1108. N_("Where possible mark variables read-only after relocation"),
  1109. N_("Don't mark variables read-only after relocation"));
  1110. DEFINE_bool(text, options::DASH_Z, '\0', false,
  1111. N_("Do not permit relocations in read-only segments"),
  1112. N_("Permit relocations in read-only segments (default)"));
  1113. DEFINE_bool_alias(textoff, text, options::DASH_Z, '\0',
  1114. N_("Permit relocations in read-only segments (default)"),
  1115. NULL, true);
  1116. public:
  1117. typedef options::Dir_list Dir_list;
  1118. General_options();
  1119. // Does post-processing on flags, making sure they all have
  1120. // non-conflicting values. Also converts some flags from their
  1121. // "standard" types (string, etc), to another type (enum, DirList),
  1122. // which can be accessed via a separate method. Dies if it notices
  1123. // any problems.
  1124. void finalize();
  1125. // True if we printed the version information.
  1126. bool
  1127. printed_version() const
  1128. { return this->printed_version_; }
  1129. // The macro defines output() (based on --output), but that's a
  1130. // generic name. Provide this alternative name, which is clearer.
  1131. const char*
  1132. output_file_name() const
  1133. { return this->output(); }
  1134. // This is not defined via a flag, but combines flags to say whether
  1135. // the output is position-independent or not.
  1136. bool
  1137. output_is_position_independent() const
  1138. { return this->shared() || this->pie(); }
  1139. // Return true if the output is something that can be exec()ed, such
  1140. // as a static executable, or a position-dependent or
  1141. // position-independent executable, but not a dynamic library or an
  1142. // object file.
  1143. bool
  1144. output_is_executable() const
  1145. { return !this->shared() && !this->relocatable(); }
  1146. // This would normally be static(), and defined automatically, but
  1147. // since static is a keyword, we need to come up with our own name.
  1148. bool
  1149. is_static() const
  1150. { return static_; }
  1151. // In addition to getting the input and output formats as a string
  1152. // (via format() and oformat()), we also give access as an enum.
  1153. enum Object_format
  1154. {
  1155. // Ordinary ELF.
  1156. OBJECT_FORMAT_ELF,
  1157. // Straight binary format.
  1158. OBJECT_FORMAT_BINARY
  1159. };
  1160. // Convert a string to an Object_format. Gives an error if the
  1161. // string is not recognized.
  1162. static Object_format
  1163. string_to_object_format(const char* arg);
  1164. // Note: these functions are not very fast.
  1165. Object_format format_enum() const;
  1166. Object_format oformat_enum() const;
  1167. // Return whether FILENAME is in a system directory.
  1168. bool
  1169. is_in_system_directory(const std::string& name) const;
  1170. // RETURN whether SYMBOL_NAME should be kept, according to symbols_to_retain_.
  1171. bool
  1172. should_retain_symbol(const char* symbol_name) const
  1173. {
  1174. if (symbols_to_retain_.empty()) // means flag wasn't specified
  1175. return true;
  1176. return symbols_to_retain_.find(symbol_name) != symbols_to_retain_.end();
  1177. }
  1178. // These are the best way to get access to the execstack state,
  1179. // not execstack() and noexecstack() which are hard to use properly.
  1180. bool
  1181. is_execstack_set() const
  1182. { return this->execstack_status_ != EXECSTACK_FROM_INPUT; }
  1183. bool
  1184. is_stack_executable() const
  1185. { return this->execstack_status_ == EXECSTACK_YES; }
  1186. bool
  1187. icf_enabled() const
  1188. { return this->icf_status_ != ICF_NONE; }
  1189. bool
  1190. icf_safe_folding() const
  1191. { return this->icf_status_ == ICF_SAFE; }
  1192. // The --demangle option takes an optional string, and there is also
  1193. // a --no-demangle option. This is the best way to decide whether
  1194. // to demangle or not.
  1195. bool
  1196. do_demangle() const
  1197. { return this->do_demangle_; }
  1198. // Returns TRUE if any plugin libraries have been loaded.
  1199. bool
  1200. has_plugins() const
  1201. { return this->plugins_ != NULL; }
  1202. // Return a pointer to the plugin manager.
  1203. Plugin_manager*
  1204. plugins() const
  1205. { return this->plugins_; }
  1206. // True iff SYMBOL was found in the file specified by dynamic-list.
  1207. bool
  1208. in_dynamic_list(const char* symbol) const
  1209. { return this->dynamic_list_.version_script_info()->symbol_is_local(symbol); }
  1210. // True if a --dynamic-list script was provided.
  1211. bool
  1212. have_dynamic_list() const
  1213. { return this->have_dynamic_list_; }
  1214. // Finalize the dynamic list.
  1215. void
  1216. finalize_dynamic_list()
  1217. { this->dynamic_list_.version_script_info()->finalize(); }
  1218. // The mode selected by the --incremental options.
  1219. enum Incremental_mode
  1220. {
  1221. // No incremental linking (--no-incremental).
  1222. INCREMENTAL_OFF,
  1223. // Incremental update only (--incremental-update).
  1224. INCREMENTAL_UPDATE,
  1225. // Force a full link, but prepare for subsequent incremental link
  1226. // (--incremental-full).
  1227. INCREMENTAL_FULL,
  1228. // Incremental update if possible, fallback to full link (--incremental).
  1229. INCREMENTAL_AUTO
  1230. };
  1231. // The incremental linking mode.
  1232. Incremental_mode
  1233. incremental_mode() const
  1234. { return this->incremental_mode_; }
  1235. // The disposition given by the --incremental-changed,
  1236. // --incremental-unchanged or --incremental-unknown option. The
  1237. // value may change as we proceed parsing the command line flags.
  1238. Incremental_disposition
  1239. incremental_disposition() const
  1240. { return this->incremental_disposition_; }
  1241. // The disposition to use for startup files (those that precede the
  1242. // first --incremental-changed, etc. option).
  1243. Incremental_disposition
  1244. incremental_startup_disposition() const
  1245. { return this->incremental_startup_disposition_; }
  1246. // Return true if S is the name of a library excluded from automatic
  1247. // symbol export.
  1248. bool
  1249. check_excluded_libs(const std::string &s) const;
  1250. // If an explicit start address was given for section SECNAME with
  1251. // the --section-start option, return true and set *PADDR to the
  1252. // address. Otherwise return false.
  1253. bool
  1254. section_start(const char* secname, uint64_t* paddr) const;
  1255. // Return whether any --section-start option was used.
  1256. bool
  1257. any_section_start() const
  1258. { return !this->section_starts_.empty(); }
  1259. enum Fix_v4bx
  1260. {
  1261. // Leave original instruction.
  1262. FIX_V4BX_NONE,
  1263. // Replace instruction.
  1264. FIX_V4BX_REPLACE,
  1265. // Generate an interworking veneer.
  1266. FIX_V4BX_INTERWORKING
  1267. };
  1268. Fix_v4bx
  1269. fix_v4bx() const
  1270. { return (this->fix_v4bx_); }
  1271. enum Endianness
  1272. {
  1273. ENDIANNESS_NOT_SET,
  1274. ENDIANNESS_BIG,
  1275. ENDIANNESS_LITTLE
  1276. };
  1277. Endianness
  1278. endianness() const
  1279. { return this->endianness_; }
  1280. bool
  1281. discard_all() const
  1282. { return this->discard_locals_ == DISCARD_ALL; }
  1283. bool
  1284. discard_locals() const
  1285. { return this->discard_locals_ == DISCARD_LOCALS; }
  1286. bool
  1287. discard_sec_merge() const
  1288. { return this->discard_locals_ == DISCARD_SEC_MERGE; }
  1289. private:
  1290. // Don't copy this structure.
  1291. General_options(const General_options&);
  1292. General_options& operator=(const General_options&);
  1293. // What local symbols to discard.
  1294. enum Discard_locals
  1295. {
  1296. // Locals in merge sections (default).
  1297. DISCARD_SEC_MERGE,
  1298. // None (--discard-none).
  1299. DISCARD_NONE,
  1300. // Temporary locals (--discard-locals/-X).
  1301. DISCARD_LOCALS,
  1302. // All locals (--discard-all/-x).
  1303. DISCARD_ALL
  1304. };
  1305. // Whether to mark the stack as executable.
  1306. enum Execstack
  1307. {
  1308. // Not set on command line.
  1309. EXECSTACK_FROM_INPUT,
  1310. // Mark the stack as executable (-z execstack).
  1311. EXECSTACK_YES,
  1312. // Mark the stack as not executable (-z noexecstack).
  1313. EXECSTACK_NO
  1314. };
  1315. enum Icf_status
  1316. {
  1317. // Do not fold any functions (Default or --icf=none).
  1318. ICF_NONE,
  1319. // All functions are candidates for folding. (--icf=all).
  1320. ICF_ALL,
  1321. // Only ctors and dtors are candidates for folding. (--icf=safe).
  1322. ICF_SAFE
  1323. };
  1324. void
  1325. set_icf_status(Icf_status value)
  1326. { this->icf_status_ = value; }
  1327. void
  1328. set_execstack_status(Execstack value)
  1329. { this->execstack_status_ = value; }
  1330. void
  1331. set_do_demangle(bool value)
  1332. { this->do_demangle_ = value; }
  1333. void
  1334. set_static(bool value)
  1335. { static_ = value; }
  1336. // These are called by finalize() to set up the search-path correctly.
  1337. void
  1338. add_to_library_path_with_sysroot(const std::string& arg)
  1339. { this->add_search_directory_to_library_path(Search_directory(arg, true)); }
  1340. // Apply any sysroot to the directory lists.
  1341. void
  1342. add_sysroot();
  1343. // Add a plugin and its arguments to the list of plugins.
  1344. void
  1345. add_plugin(const char* filename);
  1346. // Add a plugin option.
  1347. void
  1348. add_plugin_option(const char* opt);
  1349. // Whether we printed version information.
  1350. bool printed_version_;
  1351. // Whether to mark the stack as executable.
  1352. Execstack execstack_status_;
  1353. // Whether to do code folding.
  1354. Icf_status icf_status_;
  1355. // Whether to do a static link.
  1356. bool static_;
  1357. // Whether to do demangling.
  1358. bool do_demangle_;
  1359. // List of plugin libraries.
  1360. Plugin_manager* plugins_;
  1361. // The parsed output of --dynamic-list files. For convenience in
  1362. // script.cc, we store this as a Script_options object, even though
  1363. // we only use a single Version_tree from it.
  1364. Script_options dynamic_list_;
  1365. // Whether a --dynamic-list file was provided.
  1366. bool have_dynamic_list_;
  1367. // The incremental linking mode.
  1368. Incremental_mode incremental_mode_;
  1369. // The disposition given by the --incremental-changed,
  1370. // --incremental-unchanged or --incremental-unknown option. The
  1371. // value may change as we proceed parsing the command line flags.
  1372. Incremental_disposition incremental_disposition_;
  1373. // The disposition to use for startup files (those marked
  1374. // INCREMENTAL_STARTUP).
  1375. Incremental_disposition incremental_startup_disposition_;
  1376. // Whether we have seen one of the options that require incremental
  1377. // build (--incremental-changed, --incremental-unchanged,
  1378. // --incremental-unknown, or --incremental-startup-unchanged).
  1379. bool implicit_incremental_;
  1380. // Libraries excluded from automatic export, via --exclude-libs.
  1381. Unordered_set<std::string> excluded_libs_;
  1382. // List of symbol-names to keep, via -retain-symbol-info.
  1383. Unordered_set<std::string> symbols_to_retain_;
  1384. // Map from section name to address from --section-start.
  1385. std::map<std::string, uint64_t> section_starts_;
  1386. // Whether to process armv4 bx instruction relocation.
  1387. Fix_v4bx fix_v4bx_;
  1388. // Endianness.
  1389. Endianness endianness_;
  1390. // What local symbols to discard.
  1391. Discard_locals discard_locals_;
  1392. };
  1393. // The position-dependent options. We use this to store the state of
  1394. // the commandline at a particular point in parsing for later
  1395. // reference. For instance, if we see "ld --whole-archive foo.a
  1396. // --no-whole-archive," we want to store the whole-archive option with
  1397. // foo.a, so when the time comes to parse foo.a we know we should do
  1398. // it in whole-archive mode. We could store all of General_options,
  1399. // but that's big, so we just pick the subset of flags that actually
  1400. // change in a position-dependent way.
  1401. #define DEFINE_posdep(varname__, type__) \
  1402. public: \
  1403. type__ \
  1404. varname__() const \
  1405. { return this->varname__##_; } \
  1406. \
  1407. void \
  1408. set_##varname__(type__ value) \
  1409. { this->varname__##_ = value; } \
  1410. private: \
  1411. type__ varname__##_
  1412. class Position_dependent_options
  1413. {
  1414. public:
  1415. Position_dependent_options(const General_options& options
  1416. = Position_dependent_options::default_options_)
  1417. { copy_from_options(options); }
  1418. void copy_from_options(const General_options& options)
  1419. {
  1420. this->set_as_needed(options.as_needed());
  1421. this->set_Bdynamic(options.Bdynamic());
  1422. this->set_format_enum(options.format_enum());
  1423. this->set_whole_archive(options.whole_archive());
  1424. this->set_incremental_disposition(options.incremental_disposition());
  1425. }
  1426. DEFINE_posdep(as_needed, bool);
  1427. DEFINE_posdep(Bdynamic, bool);
  1428. DEFINE_posdep(format_enum, General_options::Object_format);
  1429. DEFINE_posdep(whole_archive, bool);
  1430. DEFINE_posdep(incremental_disposition, Incremental_disposition);
  1431. private:
  1432. // This is a General_options with everything set to its default
  1433. // value. A Position_dependent_options created with no argument
  1434. // will take its values from here.
  1435. static General_options default_options_;
  1436. };
  1437. // A single file or library argument from the command line.
  1438. class Input_file_argument
  1439. {
  1440. public:
  1441. enum Input_file_type
  1442. {
  1443. // A regular file, name used as-is, not searched.
  1444. INPUT_FILE_TYPE_FILE,
  1445. // A library name. When used, "lib" will be prepended and ".so" or
  1446. // ".a" appended to make a filename, and that filename will be searched
  1447. // for using the -L paths.
  1448. INPUT_FILE_TYPE_LIBRARY,
  1449. // A regular file, name used as-is, but searched using the -L paths.
  1450. INPUT_FILE_TYPE_SEARCHED_FILE
  1451. };
  1452. // name: file name or library name
  1453. // type: the type of this input file.
  1454. // extra_search_path: an extra directory to look for the file, prior
  1455. // to checking the normal library search path. If this is "",
  1456. // then no extra directory is added.
  1457. // just_symbols: whether this file only defines symbols.
  1458. // options: The position dependent options at this point in the
  1459. // command line, such as --whole-archive.
  1460. Input_file_argument()
  1461. : name_(), type_(INPUT_FILE_TYPE_FILE), extra_search_path_(""),
  1462. just_symbols_(false), options_(), arg_serial_(0)
  1463. { }
  1464. Input_file_argument(const char* name, Input_file_type type,
  1465. const char* extra_search_path,
  1466. bool just_symbols,
  1467. const Position_dependent_options& options)
  1468. : name_(name), type_(type), extra_search_path_(extra_search_path),
  1469. just_symbols_(just_symbols), options_(options), arg_serial_(0)
  1470. { }
  1471. // You can also pass in a General_options instance instead of a
  1472. // Position_dependent_options. In that case, we extract the
  1473. // position-independent vars from the General_options and only store
  1474. // those.
  1475. Input_file_argument(const char* name, Input_file_type type,
  1476. const char* extra_search_path,
  1477. bool just_symbols,
  1478. const General_options& options)
  1479. : name_(name), type_(type), extra_search_path_(extra_search_path),
  1480. just_symbols_(just_symbols), options_(options), arg_serial_(0)
  1481. { }
  1482. const char*
  1483. name() const
  1484. { return this->name_.c_str(); }
  1485. const Position_dependent_options&
  1486. options() const
  1487. { return this->options_; }
  1488. bool
  1489. is_lib() const
  1490. { return type_ == INPUT_FILE_TYPE_LIBRARY; }
  1491. bool
  1492. is_searched_file() const
  1493. { return type_ == INPUT_FILE_TYPE_SEARCHED_FILE; }
  1494. const char*
  1495. extra_search_path() const
  1496. {
  1497. return (this->extra_search_path_.empty()
  1498. ? NULL
  1499. : this->extra_search_path_.c_str());
  1500. }
  1501. // Return whether we should only read symbols from this file.
  1502. bool
  1503. just_symbols() const
  1504. { return this->just_symbols_; }
  1505. // Return whether this file may require a search using the -L
  1506. // options.
  1507. bool
  1508. may_need_search() const
  1509. {
  1510. return (this->is_lib()
  1511. || this->is_searched_file()
  1512. || !this->extra_search_path_.empty());
  1513. }
  1514. // Set the serial number for this argument.
  1515. void
  1516. set_arg_serial(unsigned int arg_serial)
  1517. { this->arg_serial_ = arg_serial; }
  1518. // Get the serial number.
  1519. unsigned int
  1520. arg_serial() const
  1521. { return this->arg_serial_; }
  1522. private:
  1523. // We use std::string, not const char*, here for convenience when
  1524. // using script files, so that we do not have to preserve the string
  1525. // in that case.
  1526. std::string name_;
  1527. Input_file_type type_;
  1528. std::string extra_search_path_;
  1529. bool just_symbols_;
  1530. Position_dependent_options options_;
  1531. // A unique index for this file argument in the argument list.
  1532. unsigned int arg_serial_;
  1533. };
  1534. // A file or library, or a group, from the command line.
  1535. class Input_argument
  1536. {
  1537. public:
  1538. // Create a file or library argument.
  1539. explicit Input_argument(Input_file_argument file)
  1540. : is_file_(true), file_(file), group_(NULL), lib_(NULL), script_info_(NULL)
  1541. { }
  1542. // Create a group argument.
  1543. explicit Input_argument(Input_file_group* group)
  1544. : is_file_(false), group_(group), lib_(NULL), script_info_(NULL)
  1545. { }
  1546. // Create a lib argument.
  1547. explicit Input_argument(Input_file_lib* lib)
  1548. : is_file_(false), group_(NULL), lib_(lib), script_info_(NULL)
  1549. { }
  1550. // Return whether this is a file.
  1551. bool
  1552. is_file() const
  1553. { return this->is_file_; }
  1554. // Return whether this is a group.
  1555. bool
  1556. is_group() const
  1557. { return !this->is_file_ && this->lib_ == NULL; }
  1558. // Return whether this is a lib.
  1559. bool
  1560. is_lib() const
  1561. { return this->lib_ != NULL; }
  1562. // Return the information about the file.
  1563. const Input_file_argument&
  1564. file() const
  1565. {
  1566. gold_assert(this->is_file_);
  1567. return this->file_;
  1568. }
  1569. // Return the information about the group.
  1570. const Input_file_group*
  1571. group() const
  1572. {
  1573. gold_assert(!this->is_file_);
  1574. return this->group_;
  1575. }
  1576. Input_file_group*
  1577. group()
  1578. {
  1579. gold_assert(!this->is_file_);
  1580. return this->group_;
  1581. }
  1582. // Return the information about the lib.
  1583. const Input_file_lib*
  1584. lib() const
  1585. {
  1586. gold_assert(!this->is_file_);
  1587. gold_assert(this->lib_);
  1588. return this->lib_;
  1589. }
  1590. Input_file_lib*
  1591. lib()
  1592. {
  1593. gold_assert(!this->is_file_);
  1594. gold_assert(this->lib_);
  1595. return this->lib_;
  1596. }
  1597. // If a script generated this argument, store a pointer to the script info.
  1598. // Currently used only for recording incremental link information.
  1599. void
  1600. set_script_info(Script_info* info)
  1601. { this->script_info_ = info; }
  1602. Script_info*
  1603. script_info() const
  1604. { return this->script_info_; }
  1605. private:
  1606. bool is_file_;
  1607. Input_file_argument file_;
  1608. Input_file_group* group_;
  1609. Input_file_lib* lib_;
  1610. Script_info* script_info_;
  1611. };
  1612. typedef std::vector<Input_argument> Input_argument_list;
  1613. // A group from the command line. This is a set of arguments within
  1614. // --start-group ... --end-group.
  1615. class Input_file_group
  1616. {
  1617. public:
  1618. typedef Input_argument_list::const_iterator const_iterator;
  1619. Input_file_group()
  1620. : files_()
  1621. { }
  1622. // Add a file to the end of the group.
  1623. Input_argument&
  1624. add_file(const Input_file_argument& arg)
  1625. {
  1626. this->files_.push_back(Input_argument(arg));
  1627. return this->files_.back();
  1628. }
  1629. // Iterators to iterate over the group contents.
  1630. const_iterator
  1631. begin() const
  1632. { return this->files_.begin(); }
  1633. const_iterator
  1634. end() const
  1635. { return this->files_.end(); }
  1636. private:
  1637. Input_argument_list files_;
  1638. };
  1639. // A lib from the command line. This is a set of arguments within
  1640. // --start-lib ... --end-lib.
  1641. class Input_file_lib
  1642. {
  1643. public:
  1644. typedef Input_argument_list::const_iterator const_iterator;
  1645. Input_file_lib(const Position_dependent_options& options)
  1646. : files_(), options_(options)
  1647. { }
  1648. // Add a file to the end of the lib.
  1649. Input_argument&
  1650. add_file(const Input_file_argument& arg)
  1651. {
  1652. this->files_.push_back(Input_argument(arg));
  1653. return this->files_.back();
  1654. }
  1655. const Position_dependent_options&
  1656. options() const
  1657. { return this->options_; }
  1658. // Iterators to iterate over the lib contents.
  1659. const_iterator
  1660. begin() const
  1661. { return this->files_.begin(); }
  1662. const_iterator
  1663. end() const
  1664. { return this->files_.end(); }
  1665. size_t
  1666. size() const
  1667. { return this->files_.size(); }
  1668. private:
  1669. Input_argument_list files_;
  1670. Position_dependent_options options_;
  1671. };
  1672. // A list of files from the command line or a script.
  1673. class Input_arguments
  1674. {
  1675. public:
  1676. typedef Input_argument_list::const_iterator const_iterator;
  1677. Input_arguments()
  1678. : input_argument_list_(), in_group_(false), in_lib_(false), file_count_(0)
  1679. { }
  1680. // Add a file.
  1681. Input_argument&
  1682. add_file(Input_file_argument& arg);
  1683. // Start a group (the --start-group option).
  1684. void
  1685. start_group();
  1686. // End a group (the --end-group option).
  1687. void
  1688. end_group();
  1689. // Start a lib (the --start-lib option).
  1690. void
  1691. start_lib(const Position_dependent_options&);
  1692. // End a lib (the --end-lib option).
  1693. void
  1694. end_lib();
  1695. // Return whether we are currently in a group.
  1696. bool
  1697. in_group() const
  1698. { return this->in_group_; }
  1699. // Return whether we are currently in a lib.
  1700. bool
  1701. in_lib() const
  1702. { return this->in_lib_; }
  1703. // The number of entries in the list.
  1704. int
  1705. size() const
  1706. { return this->input_argument_list_.size(); }
  1707. // Iterators to iterate over the list of input files.
  1708. const_iterator
  1709. begin() const
  1710. { return this->input_argument_list_.begin(); }
  1711. const_iterator
  1712. end() const
  1713. { return this->input_argument_list_.end(); }
  1714. // Return whether the list is empty.
  1715. bool
  1716. empty() const
  1717. { return this->input_argument_list_.empty(); }
  1718. // Return the number of input files. This may be larger than
  1719. // input_argument_list_.size(), because of files that are part
  1720. // of groups or libs.
  1721. int
  1722. number_of_input_files() const
  1723. { return this->file_count_; }
  1724. private:
  1725. Input_argument_list input_argument_list_;
  1726. bool in_group_;
  1727. bool in_lib_;
  1728. unsigned int file_count_;
  1729. };
  1730. // All the information read from the command line. These are held in
  1731. // three separate structs: one to hold the options (--foo), one to
  1732. // hold the filenames listed on the commandline, and one to hold
  1733. // linker script information. This third is not a subset of the other
  1734. // two because linker scripts can be specified either as options (via
  1735. // -T) or as a file.
  1736. class Command_line
  1737. {
  1738. public:
  1739. typedef Input_arguments::const_iterator const_iterator;
  1740. Command_line();
  1741. // Process the command line options. This will exit with an
  1742. // appropriate error message if an unrecognized option is seen.
  1743. void
  1744. process(int argc, const char** argv);
  1745. // Process one command-line option. This takes the index of argv to
  1746. // process, and returns the index for the next option. no_more_options
  1747. // is set to true if argv[i] is "--".
  1748. int
  1749. process_one_option(int argc, const char** argv, int i,
  1750. bool* no_more_options);
  1751. // Get the general options.
  1752. const General_options&
  1753. options() const
  1754. { return this->options_; }
  1755. // Get the position dependent options.
  1756. const Position_dependent_options&
  1757. position_dependent_options() const
  1758. { return this->position_options_; }
  1759. // Get the linker-script options.
  1760. Script_options&
  1761. script_options()
  1762. { return this->script_options_; }
  1763. // Finalize the version-script options and return them.
  1764. const Version_script_info&
  1765. version_script();
  1766. // Get the input files.
  1767. Input_arguments&
  1768. inputs()
  1769. { return this->inputs_; }
  1770. // The number of input files.
  1771. int
  1772. number_of_input_files() const
  1773. { return this->inputs_.number_of_input_files(); }
  1774. // Iterators to iterate over the list of input files.
  1775. const_iterator
  1776. begin() const
  1777. { return this->inputs_.begin(); }
  1778. const_iterator
  1779. end() const
  1780. { return this->inputs_.end(); }
  1781. private:
  1782. Command_line(const Command_line&);
  1783. Command_line& operator=(const Command_line&);
  1784. // This is a dummy class to provide a constructor that runs before
  1785. // the constructor for the General_options. The Pre_options constructor
  1786. // is used as a hook to set the flag enabling the options to register
  1787. // themselves.
  1788. struct Pre_options {
  1789. Pre_options();
  1790. };
  1791. // This must come before options_!
  1792. Pre_options pre_options_;
  1793. General_options options_;
  1794. Position_dependent_options position_options_;
  1795. Script_options script_options_;
  1796. Input_arguments inputs_;
  1797. };
  1798. } // End namespace gold.
  1799. #endif // !defined(GOLD_OPTIONS_H)