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