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