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