script.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. // script.h -- handle linker scripts for gold -*- C++ -*-
  2. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  3. // Written by Ian Lance Taylor <iant@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. // 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. // Sets all symbols used in expressions as seen in a real ELF object.
  112. virtual void
  113. set_expr_sym_in_real_elf(Symbol_table*) const
  114. { return; }
  115. private:
  116. // May not be copied.
  117. Expression(const Expression&);
  118. Expression& operator=(const Expression&);
  119. };
  120. // Version_script_info stores information parsed from the version
  121. // script, either provided by --version-script or as part of a linker
  122. // script. A single Version_script_info object per target is owned by
  123. // Script_options.
  124. class Version_script_info
  125. {
  126. public:
  127. // The languages which can be specified in a versionn script.
  128. enum Language
  129. {
  130. LANGUAGE_C, // No demangling.
  131. LANGUAGE_CXX, // C++ demangling.
  132. LANGUAGE_JAVA, // Java demangling.
  133. LANGUAGE_COUNT
  134. };
  135. Version_script_info();
  136. ~Version_script_info();
  137. // Clear everything.
  138. void
  139. clear();
  140. // Finalize the version control information.
  141. void
  142. finalize();
  143. // Return whether the information is finalized.
  144. bool
  145. is_finalized() const
  146. { return this->is_finalized_; }
  147. // Return whether any version were defined in the version script.
  148. bool
  149. empty() const
  150. { return this->version_trees_.empty(); }
  151. // If there is a version associated with SYMBOL, return true, and
  152. // set *VERSION to the version, and *IS_GLOBAL to whether the symbol
  153. // should be global. Otherwise, return false.
  154. bool
  155. get_symbol_version(const char* symbol, std::string* version,
  156. bool* is_global) const;
  157. // Return whether this symbol matches the local: section of some
  158. // version.
  159. bool
  160. symbol_is_local(const char* symbol) const
  161. {
  162. bool is_global;
  163. return (this->get_symbol_version(symbol, NULL, &is_global)
  164. && !is_global);
  165. }
  166. // Return the names of versions defined in the version script.
  167. std::vector<std::string>
  168. get_versions() const;
  169. // Return the list of dependencies for this version.
  170. std::vector<std::string>
  171. get_dependencies(const char* version) const;
  172. // The following functions should only be used by the bison helper
  173. // functions. They allocate new structs whose memory belongs to
  174. // Version_script_info. The bison functions copy the information
  175. // from the version script into these structs.
  176. struct Version_dependency_list*
  177. allocate_dependency_list();
  178. struct Version_expression_list*
  179. allocate_expression_list();
  180. struct Version_tree*
  181. allocate_version_tree();
  182. // Build the lookup tables after all data have been read.
  183. void
  184. build_lookup_tables();
  185. // Give an error if there are any unmatched names in the version
  186. // script.
  187. void
  188. check_unmatched_names(const Symbol_table*) const;
  189. // Print contents to the FILE. This is for debugging.
  190. void
  191. print(FILE*) const;
  192. private:
  193. void
  194. print_expression_list(FILE* f, const Version_expression_list*) const;
  195. bool
  196. get_symbol_version_helper(const char* symbol,
  197. bool check_global,
  198. std::string* pversion) const;
  199. // Fast lookup information for a given language.
  200. // We map from exact match strings to Version_tree's. Historically
  201. // version scripts sometimes have the same symbol multiple times,
  202. // which is ambiguous. We warn about that case by storing the
  203. // second Version_tree we see.
  204. struct Version_tree_match
  205. {
  206. Version_tree_match(const Version_tree* r, bool ig,
  207. const Version_expression* e)
  208. : real(r), is_global(ig), expression(e), ambiguous(NULL)
  209. { }
  210. // The Version_tree that we return.
  211. const Version_tree* real;
  212. // True if this is a global match for the REAL member, false if it
  213. // is a local match.
  214. bool is_global;
  215. // Point back to the Version_expression for which we created this
  216. // match.
  217. const Version_expression* expression;
  218. // If not NULL, another Version_tree that defines the symbol.
  219. const Version_tree* ambiguous;
  220. };
  221. // Map from an exact match string to a Version_tree.
  222. typedef Unordered_map<std::string, Version_tree_match> Exact;
  223. // Fast lookup information for a glob pattern.
  224. struct Glob
  225. {
  226. Glob()
  227. : expression(NULL), version(NULL), is_global(false)
  228. { }
  229. Glob(const Version_expression* e, const Version_tree* v, bool ig)
  230. : expression(e), version(v), is_global(ig)
  231. { }
  232. // A pointer to the version expression holding the pattern to
  233. // match and the language to use for demangling the symbol before
  234. // doing the match.
  235. const Version_expression* expression;
  236. // The Version_tree we use if this pattern matches.
  237. const Version_tree* version;
  238. // True if this is a global symbol.
  239. bool is_global;
  240. };
  241. typedef std::vector<Glob> Globs;
  242. bool
  243. unquote(std::string*) const;
  244. void
  245. add_exact_match(const std::string&, const Version_tree*, bool is_global,
  246. const Version_expression*, Exact*);
  247. void
  248. build_expression_list_lookup(const Version_expression_list*,
  249. const Version_tree*, bool);
  250. const char*
  251. get_name_to_match(const char*, int,
  252. Lazy_demangler*, Lazy_demangler*) const;
  253. // All the version dependencies we allocate.
  254. std::vector<Version_dependency_list*> dependency_lists_;
  255. // All the version expressions we allocate.
  256. std::vector<Version_expression_list*> expression_lists_;
  257. // The list of versions.
  258. std::vector<Version_tree*> version_trees_;
  259. // Exact matches for global symbols, by language.
  260. Exact* exact_[LANGUAGE_COUNT];
  261. // A vector of glob patterns mapping to Version_trees.
  262. Globs globs_;
  263. // The default version to use, if there is one. This is from a
  264. // pattern of "*".
  265. const Version_tree* default_version_;
  266. // True if the default version is global.
  267. bool default_is_global_;
  268. // Whether this has been finalized.
  269. bool is_finalized_;
  270. };
  271. // This class manages assignments to symbols. These can appear in
  272. // three different locations in scripts: outside of a SECTIONS clause,
  273. // within a SECTIONS clause, and within an output section definition
  274. // within a SECTIONS clause. This can also appear on the command line
  275. // via the --defsym command line option.
  276. class Symbol_assignment
  277. {
  278. public:
  279. Symbol_assignment(const char* name, size_t namelen, bool is_defsym,
  280. Expression* val, bool provide, bool hidden)
  281. : name_(name, namelen), val_(val), is_defsym_(is_defsym),
  282. provide_(provide), hidden_(hidden), sym_(NULL)
  283. { }
  284. // Add the symbol to the symbol table.
  285. void
  286. add_to_table(Symbol_table*);
  287. // Finalize the symbol value.
  288. void
  289. finalize(Symbol_table*, const Layout*);
  290. bool
  291. is_defsym() const
  292. { return is_defsym_; }
  293. Expression *
  294. value() const
  295. { return val_; }
  296. // Finalize the symbol value when it can refer to the dot symbol.
  297. void
  298. finalize_with_dot(Symbol_table*, const Layout*, uint64_t dot_value,
  299. Output_section* dot_section);
  300. // Set the symbol value, but only if the value is absolute or relative to
  301. // DOT_SECTION. This is used while processing a SECTIONS clause.
  302. // We assume that dot is an absolute value here. We do not check assertions.
  303. void
  304. set_if_absolute(Symbol_table*, const Layout*, bool is_dot_available,
  305. uint64_t dot_value, Output_section* dot_section);
  306. const std::string&
  307. name() const
  308. { return this->name_; }
  309. // Print the assignment to the FILE. This is for debugging.
  310. void
  311. print(FILE*) const;
  312. private:
  313. // Shared by finalize and finalize_with_dot.
  314. void
  315. finalize_maybe_dot(Symbol_table*, const Layout*, bool is_dot_available,
  316. uint64_t dot_value, Output_section* dot_section);
  317. // Sized version of finalize.
  318. template<int size>
  319. void
  320. sized_finalize(Symbol_table*, const Layout*, bool is_dot_available,
  321. uint64_t dot_value, Output_section*);
  322. // Symbol name.
  323. std::string name_;
  324. // Expression to assign to symbol.
  325. Expression* val_;
  326. // True if this symbol is defined by a --defsym, false if it is
  327. // defined in a linker script.
  328. bool is_defsym_;
  329. // Whether the assignment should be provided (only set if there is
  330. // an undefined reference to the symbol.
  331. bool provide_;
  332. // Whether the assignment should be hidden.
  333. bool hidden_;
  334. // The entry in the symbol table.
  335. Symbol* sym_;
  336. };
  337. // This class manages assertions in linker scripts. These can appear
  338. // in all the places where a Symbol_assignment can appear.
  339. class Script_assertion
  340. {
  341. public:
  342. Script_assertion(Expression* check, const char* message,
  343. size_t messagelen)
  344. : check_(check), message_(message, messagelen)
  345. { }
  346. // Check the assertion.
  347. void
  348. check(const Symbol_table*, const Layout*);
  349. // Print the assertion to the FILE. This is for debugging.
  350. void
  351. print(FILE*) const;
  352. private:
  353. // The expression to check.
  354. Expression* check_;
  355. // The message to issue if the expression fails.
  356. std::string message_;
  357. };
  358. // We can read a linker script in two different contexts: when
  359. // initially parsing the command line, and when we find an input file
  360. // which is actually a linker script. Also some of the data which can
  361. // be set by a linker script can also be set via command line options
  362. // like -e and --defsym. This means that we have a type of data which
  363. // can be set both during command line option parsing and while
  364. // reading input files. We store that data in an instance of this
  365. // object. We will keep pointers to that instance in both the
  366. // Command_line and Layout objects.
  367. class Script_options
  368. {
  369. public:
  370. Script_options();
  371. // Add a symbol to be defined.
  372. void
  373. add_symbol_assignment(const char* name, size_t length, bool is_defsym,
  374. Expression* value, bool provide, bool hidden);
  375. // Look for an assigned symbol.
  376. bool
  377. is_pending_assignment(const char* name);
  378. // Add a reference to a symbol.
  379. void
  380. add_symbol_reference(const char* name, size_t length);
  381. // Add an assertion.
  382. void
  383. add_assertion(Expression* check, const char* message, size_t messagelen);
  384. // Define a symbol from the command line.
  385. bool
  386. define_symbol(const char* definition);
  387. // Populates the set with symbol names used in LHS of defsym.
  388. void
  389. find_defsym_defs(Unordered_set<std::string>&);
  390. // Set symbols used in defsym expressions as seen in a real ELF object.
  391. void set_defsym_uses_in_real_elf(Symbol_table*) const;
  392. // Create sections required by any linker scripts.
  393. void
  394. create_script_sections(Layout*);
  395. // Add all symbol definitions to the symbol table.
  396. void
  397. add_symbols_to_table(Symbol_table*);
  398. // Used to iterate over symbols which are referenced in expressions
  399. // but not defined.
  400. typedef Unordered_set<std::string>::const_iterator referenced_const_iterator;
  401. referenced_const_iterator
  402. referenced_begin() const
  403. { return this->symbol_references_.begin(); }
  404. referenced_const_iterator
  405. referenced_end() const
  406. { return this->symbol_references_.end(); }
  407. // Return whether a symbol is referenced but not defined.
  408. bool
  409. is_referenced(const std::string& name) const
  410. {
  411. return (this->symbol_references_.find(name)
  412. != this->symbol_references_.end());
  413. }
  414. // Return whether there are any symbols which were referenced but
  415. // not defined.
  416. bool
  417. any_unreferenced() const
  418. { return !this->symbol_references_.empty(); }
  419. // Finalize the symbol values. Also check assertions.
  420. void
  421. finalize_symbols(Symbol_table*, const Layout*);
  422. // Version information parsed from a version script. Everything
  423. // else has a pointer to this object.
  424. Version_script_info*
  425. version_script_info()
  426. { return &this->version_script_info_; }
  427. const Version_script_info*
  428. version_script_info() const
  429. { return &this->version_script_info_; }
  430. // A SECTIONS clause parsed from a linker script. Everything else
  431. // has a pointer to this object.
  432. Script_sections*
  433. script_sections()
  434. { return &this->script_sections_; }
  435. const Script_sections*
  436. script_sections() const
  437. { return &this->script_sections_; }
  438. // Whether we saw a SECTIONS clause.
  439. bool
  440. saw_sections_clause() const
  441. { return this->script_sections_.saw_sections_clause(); }
  442. // Whether we saw a PHDRS clause.
  443. bool
  444. saw_phdrs_clause() const
  445. { return this->script_sections_.saw_phdrs_clause(); }
  446. // Set section addresses using a SECTIONS clause. Return the
  447. // segment which should hold the file header and segment headers;
  448. // this may return NULL, in which case the headers are not in a
  449. // loadable segment.
  450. Output_segment*
  451. set_section_addresses(Symbol_table*, Layout*);
  452. // Print the script to the FILE. This is for debugging.
  453. void
  454. print(FILE*) const;
  455. private:
  456. // We keep a list of symbol assignments which occur outside of a
  457. // SECTIONS clause.
  458. typedef std::vector<Symbol_assignment*> Symbol_assignments;
  459. // We keep a list of all assertions which occur outside of a
  460. // SECTIONS clause.
  461. typedef std::vector<Script_assertion*> Assertions;
  462. // The entry address. This will be empty if not set.
  463. std::string entry_;
  464. // Symbols to set.
  465. Symbol_assignments symbol_assignments_;
  466. // Symbols defined in an expression, for faster lookup.
  467. Unordered_set<std::string> symbol_definitions_;
  468. // Symbols referenced in an expression.
  469. Unordered_set<std::string> symbol_references_;
  470. // Assertions to check.
  471. Assertions assertions_;
  472. // Version information parsed from a version script.
  473. Version_script_info version_script_info_;
  474. // Information from any SECTIONS clauses.
  475. Script_sections script_sections_;
  476. };
  477. // FILE was found as an argument on the command line, but was not
  478. // recognized as an ELF file. Try to read it as a script. Return
  479. // true if the file was handled. This has to handle /usr/lib/libc.so
  480. // on a GNU/Linux system. *USED_NEXT_BLOCKER is set to indicate
  481. // whether the function took over NEXT_BLOCKER.
  482. bool
  483. read_input_script(Workqueue*, Symbol_table*, Layout*, Dirsearch*, int,
  484. Input_objects*, Mapfile*, Input_group*,
  485. const Input_argument*, Input_file*,
  486. Task_token* next_blocker, bool* used_next_blocker);
  487. // FILE was found as an argument to --script (-T).
  488. // Read it as a script, and execute its contents immediately.
  489. bool
  490. read_commandline_script(const char* filename, Command_line* cmdline);
  491. // FILE was found as an argument to --version-script. Read it as a
  492. // version script, and store its contents in
  493. // cmdline->script_options()->version_script_info().
  494. bool
  495. read_version_script(const char* filename, Command_line* cmdline);
  496. // FILENAME was found as an argument to --dynamic-list. Read it as a
  497. // version script (actually, a versym_node from a version script), and
  498. // store its contents in DYNAMIC_LIST.
  499. bool
  500. read_dynamic_list(const char* filename, Command_line* cmdline,
  501. Script_options* dynamic_list);
  502. } // End namespace gold.
  503. #endif // !defined(GOLD_SCRIPT_H)