script.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. // script.h -- handle linker scripts 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. // We implement a subset of the original GNU ld linker script language
  18. // for compatibility. The goal is not to implement the entire
  19. // language. It is merely to implement enough to handle common uses.
  20. // In particular we need to handle /usr/lib/libc.so on a typical
  21. // GNU/Linux system, and we want to handle linker scripts used by the
  22. // Linux kernel build.
  23. #ifndef GOLD_SCRIPT_H
  24. #define GOLD_SCRIPT_H
  25. #include <cstdio>
  26. #include <string>
  27. #include <vector>
  28. #include "elfcpp.h"
  29. #include "script-sections.h"
  30. namespace gold
  31. {
  32. class General_options;
  33. class Command_line;
  34. class Symbol_table;
  35. class Layout;
  36. class Mapfile;
  37. class Input_argument;
  38. class Input_arguments;
  39. class Input_objects;
  40. class Input_group;
  41. class Input_file;
  42. class Output_segment;
  43. class Task_token;
  44. class Workqueue;
  45. struct Version_dependency_list;
  46. struct Version_expression_list;
  47. struct Version_tree;
  48. struct Version_expression;
  49. class Lazy_demangler;
  50. class Incremental_script_entry;
  51. // This class represents an expression in a linker script.
  52. class Expression
  53. {
  54. protected:
  55. // These should only be created by child classes.
  56. Expression()
  57. { }
  58. public:
  59. virtual ~Expression()
  60. { }
  61. // Return the value of the expression which is not permitted to
  62. // refer to the dot symbol. CHECK_ASSERTIONS is true if we should
  63. // check whether assertions are true.
  64. uint64_t
  65. eval(const Symbol_table*, const Layout*, bool check_assertions);
  66. // Return the value of an expression which is permitted to refer to
  67. // the dot symbol. DOT_VALUE is the absolute value of the dot
  68. // symbol. DOT_SECTION is the section in which dot is defined; it
  69. // should be NULL if the dot symbol has an absolute value (e.g., is
  70. // defined in a SECTIONS clause outside of any output section
  71. // definition). This sets *RESULT_SECTION to indicate where the
  72. // value is defined. If the value is absolute *RESULT_SECTION will
  73. // be NULL. Note that the returned value is still an absolute
  74. // value; to get a section relative value the caller must subtract
  75. // the section address. If RESULT_ALIGNMENT is not NULL, this sets
  76. // *RESULT_ALIGNMENT to the alignment of the value of that alignment
  77. // is larger than *RESULT_ALIGNMENT; this will only be non-zero if
  78. // this is an ALIGN expression. If IS_SECTION_DOT_ASSIGMENT is true,
  79. // we are evaluating an assignment to dot within an output section,
  80. // and an absolute value should be interpreted as an offset within
  81. // the section.
  82. uint64_t
  83. eval_with_dot(const Symbol_table*, const Layout*, bool check_assertions,
  84. uint64_t dot_value, Output_section* dot_section,
  85. Output_section** result_section, uint64_t* result_alignment,
  86. bool is_section_dot_assignment);
  87. // Return the value of an expression which may or may not be
  88. // permitted to refer to the dot symbol, depending on
  89. // is_dot_available. If IS_SECTION_DOT_ASSIGMENT is true,
  90. // we are evaluating an assignment to dot within an output section,
  91. // and an absolute value should be interpreted as an offset within
  92. // the section.
  93. uint64_t
  94. eval_maybe_dot(const Symbol_table*, const Layout*, bool check_assertions,
  95. bool is_dot_available, uint64_t dot_value,
  96. Output_section* dot_section,
  97. Output_section** result_section, uint64_t* result_alignment,
  98. elfcpp::STT* type, elfcpp::STV* vis, unsigned char* nonvis,
  99. bool is_section_dot_assignment, bool* is_valid_pointer);
  100. // Print the expression to the FILE. This is for debugging.
  101. virtual void
  102. print(FILE*) const = 0;
  103. protected:
  104. struct Expression_eval_info;
  105. public:
  106. // Compute the value of the expression (implemented by child class).
  107. // This is public rather than protected because it is called
  108. // directly by children of Expression on other Expression objects.
  109. virtual uint64_t
  110. value(const Expression_eval_info*) = 0;
  111. private:
  112. // May not be copied.
  113. Expression(const Expression&);
  114. Expression& operator=(const Expression&);
  115. };
  116. // Version_script_info stores information parsed from the version
  117. // script, either provided by --version-script or as part of a linker
  118. // script. A single Version_script_info object per target is owned by
  119. // Script_options.
  120. class Version_script_info
  121. {
  122. public:
  123. // The languages which can be specified in a versionn script.
  124. enum Language
  125. {
  126. LANGUAGE_C, // No demangling.
  127. LANGUAGE_CXX, // C++ demangling.
  128. LANGUAGE_JAVA, // Java demangling.
  129. LANGUAGE_COUNT
  130. };
  131. Version_script_info();
  132. ~Version_script_info();
  133. // Clear everything.
  134. void
  135. clear();
  136. // Finalize the version control information.
  137. void
  138. finalize();
  139. // Return whether the information is finalized.
  140. bool
  141. is_finalized() const
  142. { return this->is_finalized_; }
  143. // Return whether any version were defined in the version script.
  144. bool
  145. empty() const
  146. { return this->version_trees_.empty(); }
  147. // If there is a version associated with SYMBOL, return true, and
  148. // set *VERSION to the version, and *IS_GLOBAL to whether the symbol
  149. // should be global. Otherwise, return false.
  150. bool
  151. get_symbol_version(const char* symbol, std::string* version,
  152. bool* is_global) const;
  153. // Return whether this symbol matches the local: section of some
  154. // version.
  155. bool
  156. symbol_is_local(const char* symbol) const
  157. {
  158. bool is_global;
  159. return (this->get_symbol_version(symbol, NULL, &is_global)
  160. && !is_global);
  161. }
  162. // Return the names of versions defined in the version script.
  163. std::vector<std::string>
  164. get_versions() const;
  165. // Return the list of dependencies for this version.
  166. std::vector<std::string>
  167. get_dependencies(const char* version) const;
  168. // The following functions should only be used by the bison helper
  169. // functions. They allocate new structs whose memory belongs to
  170. // Version_script_info. The bison functions copy the information
  171. // from the version script into these structs.
  172. struct Version_dependency_list*
  173. allocate_dependency_list();
  174. struct Version_expression_list*
  175. allocate_expression_list();
  176. struct Version_tree*
  177. allocate_version_tree();
  178. // Build the lookup tables after all data have been read.
  179. void
  180. build_lookup_tables();
  181. // Give an error if there are any unmatched names in the version
  182. // script.
  183. void
  184. check_unmatched_names(const Symbol_table*) const;
  185. // Print contents to the FILE. This is for debugging.
  186. void
  187. print(FILE*) const;
  188. private:
  189. void
  190. print_expression_list(FILE* f, const Version_expression_list*) const;
  191. bool
  192. get_symbol_version_helper(const char* symbol,
  193. bool check_global,
  194. std::string* pversion) const;
  195. // Fast lookup information for a given language.
  196. // We map from exact match strings to Version_tree's. Historically
  197. // version scripts sometimes have the same symbol multiple times,
  198. // which is ambiguous. We warn about that case by storing the
  199. // second Version_tree we see.
  200. struct Version_tree_match
  201. {
  202. Version_tree_match(const Version_tree* r, bool ig,
  203. const Version_expression* e)
  204. : real(r), is_global(ig), expression(e), ambiguous(NULL)
  205. { }
  206. // The Version_tree that we return.
  207. const Version_tree* real;
  208. // True if this is a global match for the REAL member, false if it
  209. // is a local match.
  210. bool is_global;
  211. // Point back to the Version_expression for which we created this
  212. // match.
  213. const Version_expression* expression;
  214. // If not NULL, another Version_tree that defines the symbol.
  215. const Version_tree* ambiguous;
  216. };
  217. // Map from an exact match string to a Version_tree.
  218. typedef Unordered_map<std::string, Version_tree_match> Exact;
  219. // Fast lookup information for a glob pattern.
  220. struct Glob
  221. {
  222. Glob()
  223. : expression(NULL), version(NULL), is_global(false)
  224. { }
  225. Glob(const Version_expression* e, const Version_tree* v, bool ig)
  226. : expression(e), version(v), is_global(ig)
  227. { }
  228. // A pointer to the version expression holding the pattern to
  229. // match and the language to use for demangling the symbol before
  230. // doing the match.
  231. const Version_expression* expression;
  232. // The Version_tree we use if this pattern matches.
  233. const Version_tree* version;
  234. // True if this is a global symbol.
  235. bool is_global;
  236. };
  237. typedef std::vector<Glob> Globs;
  238. bool
  239. unquote(std::string*) const;
  240. void
  241. add_exact_match(const std::string&, const Version_tree*, bool is_global,
  242. const Version_expression*, Exact*);
  243. void
  244. build_expression_list_lookup(const Version_expression_list*,
  245. const Version_tree*, bool);
  246. const char*
  247. get_name_to_match(const char*, int,
  248. Lazy_demangler*, Lazy_demangler*) const;
  249. // All the version dependencies we allocate.
  250. std::vector<Version_dependency_list*> dependency_lists_;
  251. // All the version expressions we allocate.
  252. std::vector<Version_expression_list*> expression_lists_;
  253. // The list of versions.
  254. std::vector<Version_tree*> version_trees_;
  255. // Exact matches for global symbols, by language.
  256. Exact* exact_[LANGUAGE_COUNT];
  257. // A vector of glob patterns mapping to Version_trees.
  258. Globs globs_;
  259. // The default version to use, if there is one. This is from a
  260. // pattern of "*".
  261. const Version_tree* default_version_;
  262. // True if the default version is global.
  263. bool default_is_global_;
  264. // Whether this has been finalized.
  265. bool is_finalized_;
  266. };
  267. // This class manages assignments to symbols. These can appear in
  268. // three different locations in scripts: outside of a SECTIONS clause,
  269. // within a SECTIONS clause, and within an output section definition
  270. // within a SECTIONS clause. This can also appear on the command line
  271. // via the --defsym command line option.
  272. class Symbol_assignment
  273. {
  274. public:
  275. Symbol_assignment(const char* name, size_t namelen, bool is_defsym,
  276. Expression* val, bool provide, bool hidden)
  277. : name_(name, namelen), val_(val), is_defsym_(is_defsym),
  278. provide_(provide), hidden_(hidden), sym_(NULL)
  279. { }
  280. // Add the symbol to the symbol table.
  281. void
  282. add_to_table(Symbol_table*);
  283. // Finalize the symbol value.
  284. void
  285. finalize(Symbol_table*, const Layout*);
  286. // Finalize the symbol value when it can refer to the dot symbol.
  287. void
  288. finalize_with_dot(Symbol_table*, const Layout*, uint64_t dot_value,
  289. Output_section* dot_section);
  290. // Set the symbol value, but only if the value is absolute or relative to
  291. // DOT_SECTION. This is used while processing a SECTIONS clause.
  292. // We assume that dot is an absolute value here. We do not check assertions.
  293. void
  294. set_if_absolute(Symbol_table*, const Layout*, bool is_dot_available,
  295. uint64_t dot_value, Output_section* dot_section);
  296. const std::string&
  297. name() const
  298. { return this->name_; }
  299. // Print the assignment to the FILE. This is for debugging.
  300. void
  301. print(FILE*) const;
  302. private:
  303. // Shared by finalize and finalize_with_dot.
  304. void
  305. finalize_maybe_dot(Symbol_table*, const Layout*, bool is_dot_available,
  306. uint64_t dot_value, Output_section* dot_section);
  307. // Sized version of finalize.
  308. template<int size>
  309. void
  310. sized_finalize(Symbol_table*, const Layout*, bool is_dot_available,
  311. uint64_t dot_value, Output_section*);
  312. // Symbol name.
  313. std::string name_;
  314. // Expression to assign to symbol.
  315. Expression* val_;
  316. // True if this symbol is defined by a --defsym, false if it is
  317. // defined in a linker script.
  318. bool is_defsym_;
  319. // Whether the assignment should be provided (only set if there is
  320. // an undefined reference to the symbol.
  321. bool provide_;
  322. // Whether the assignment should be hidden.
  323. bool hidden_;
  324. // The entry in the symbol table.
  325. Symbol* sym_;
  326. };
  327. // This class manages assertions in linker scripts. These can appear
  328. // in all the places where a Symbol_assignment can appear.
  329. class Script_assertion
  330. {
  331. public:
  332. Script_assertion(Expression* check, const char* message,
  333. size_t messagelen)
  334. : check_(check), message_(message, messagelen)
  335. { }
  336. // Check the assertion.
  337. void
  338. check(const Symbol_table*, const Layout*);
  339. // Print the assertion to the FILE. This is for debugging.
  340. void
  341. print(FILE*) const;
  342. private:
  343. // The expression to check.
  344. Expression* check_;
  345. // The message to issue if the expression fails.
  346. std::string message_;
  347. };
  348. // We can read a linker script in two different contexts: when
  349. // initially parsing the command line, and when we find an input file
  350. // which is actually a linker script. Also some of the data which can
  351. // be set by a linker script can also be set via command line options
  352. // like -e and --defsym. This means that we have a type of data which
  353. // can be set both during command line option parsing and while
  354. // reading input files. We store that data in an instance of this
  355. // object. We will keep pointers to that instance in both the
  356. // Command_line and Layout objects.
  357. class Script_options
  358. {
  359. public:
  360. Script_options();
  361. // Add a symbol to be defined.
  362. void
  363. add_symbol_assignment(const char* name, size_t length, bool is_defsym,
  364. Expression* value, bool provide, bool hidden);
  365. // Look for an assigned symbol.
  366. bool
  367. is_pending_assignment(const char* name);
  368. // Add a reference to a symbol.
  369. void
  370. add_symbol_reference(const char* name, size_t length);
  371. // Add an assertion.
  372. void
  373. add_assertion(Expression* check, const char* message, size_t messagelen);
  374. // Define a symbol from the command line.
  375. bool
  376. define_symbol(const char* definition);
  377. // Create sections required by any linker scripts.
  378. void
  379. create_script_sections(Layout*);
  380. // Add all symbol definitions to the symbol table.
  381. void
  382. add_symbols_to_table(Symbol_table*);
  383. // Used to iterate over symbols which are referenced in expressions
  384. // but not defined.
  385. typedef Unordered_set<std::string>::const_iterator referenced_const_iterator;
  386. referenced_const_iterator
  387. referenced_begin() const
  388. { return this->symbol_references_.begin(); }
  389. referenced_const_iterator
  390. referenced_end() const
  391. { return this->symbol_references_.end(); }
  392. // Return whether a symbol is referenced but not defined.
  393. bool
  394. is_referenced(const std::string& name) const
  395. {
  396. return (this->symbol_references_.find(name)
  397. != this->symbol_references_.end());
  398. }
  399. // Return whether there are any symbols which were referenced but
  400. // not defined.
  401. bool
  402. any_unreferenced() const
  403. { return !this->symbol_references_.empty(); }
  404. // Finalize the symbol values. Also check assertions.
  405. void
  406. finalize_symbols(Symbol_table*, const Layout*);
  407. // Version information parsed from a version script. Everything
  408. // else has a pointer to this object.
  409. Version_script_info*
  410. version_script_info()
  411. { return &this->version_script_info_; }
  412. const Version_script_info*
  413. version_script_info() const
  414. { return &this->version_script_info_; }
  415. // A SECTIONS clause parsed from a linker script. Everything else
  416. // has a pointer to this object.
  417. Script_sections*
  418. script_sections()
  419. { return &this->script_sections_; }
  420. const Script_sections*
  421. script_sections() const
  422. { return &this->script_sections_; }
  423. // Whether we saw a SECTIONS clause.
  424. bool
  425. saw_sections_clause() const
  426. { return this->script_sections_.saw_sections_clause(); }
  427. // Whether we saw a PHDRS clause.
  428. bool
  429. saw_phdrs_clause() const
  430. { return this->script_sections_.saw_phdrs_clause(); }
  431. // Set section addresses using a SECTIONS clause. Return the
  432. // segment which should hold the file header and segment headers;
  433. // this may return NULL, in which case the headers are not in a
  434. // loadable segment.
  435. Output_segment*
  436. set_section_addresses(Symbol_table*, Layout*);
  437. // Print the script to the FILE. This is for debugging.
  438. void
  439. print(FILE*) const;
  440. private:
  441. // We keep a list of symbol assignments which occur outside of a
  442. // SECTIONS clause.
  443. typedef std::vector<Symbol_assignment*> Symbol_assignments;
  444. // We keep a list of all assertions whcih occur outside of a
  445. // SECTIONS clause.
  446. typedef std::vector<Script_assertion*> Assertions;
  447. // The entry address. This will be empty if not set.
  448. std::string entry_;
  449. // Symbols to set.
  450. Symbol_assignments symbol_assignments_;
  451. // Symbols defined in an expression, for faster lookup.
  452. Unordered_set<std::string> symbol_definitions_;
  453. // Symbols referenced in an expression.
  454. Unordered_set<std::string> symbol_references_;
  455. // Assertions to check.
  456. Assertions assertions_;
  457. // Version information parsed from a version script.
  458. Version_script_info version_script_info_;
  459. // Information from any SECTIONS clauses.
  460. Script_sections script_sections_;
  461. };
  462. // FILE was found as an argument on the command line, but was not
  463. // recognized as an ELF file. Try to read it as a script. Return
  464. // true if the file was handled. This has to handle /usr/lib/libc.so
  465. // on a GNU/Linux system. *USED_NEXT_BLOCKER is set to indicate
  466. // whether the function took over NEXT_BLOCKER.
  467. bool
  468. read_input_script(Workqueue*, Symbol_table*, Layout*, Dirsearch*, int,
  469. Input_objects*, Mapfile*, Input_group*,
  470. const Input_argument*, Input_file*,
  471. Task_token* next_blocker, bool* used_next_blocker);
  472. // FILE was found as an argument to --script (-T).
  473. // Read it as a script, and execute its contents immediately.
  474. bool
  475. read_commandline_script(const char* filename, Command_line* cmdline);
  476. // FILE was found as an argument to --version-script. Read it as a
  477. // version script, and store its contents in
  478. // cmdline->script_options()->version_script_info().
  479. bool
  480. read_version_script(const char* filename, Command_line* cmdline);
  481. // FILENAME was found as an argument to --dynamic-list. Read it as a
  482. // version script (actually, a versym_node from a version script), and
  483. // store its contents in DYNAMIC_LIST.
  484. bool
  485. read_dynamic_list(const char* filename, Command_line* cmdline,
  486. Script_options* dynamic_list);
  487. } // End namespace gold.
  488. #endif // !defined(GOLD_SCRIPT_H)