Merge branch 'vendor/GDB'
[dragonfly.git] / contrib / binutils-2.20 / gold / script.h
1 // script.h -- handle linker scripts for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@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 "script-sections.h"
38
39 namespace gold
40 {
41
42 class General_options;
43 class Command_line;
44 class Symbol_table;
45 class Layout;
46 class Mapfile;
47 class Input_argument;
48 class Input_arguments;
49 class Input_objects;
50 class Input_group;
51 class Input_file;
52 class Output_segment;
53 class Task_token;
54 class Workqueue;
55 struct Version_dependency_list;
56 struct Version_expression_list;
57 struct Version_tree;
58
59 // This class represents an expression in a linker script.
60
61 class Expression
62 {
63  protected:
64   // These should only be created by child classes.
65   Expression()
66   { }
67
68  public:
69   virtual ~Expression()
70   { }
71
72   // Return the value of the expression which is not permitted to
73   // refer to the dot symbol.  CHECK_ASSERTIONS is true if we should
74   // check whether assertions are true.
75   uint64_t
76   eval(const Symbol_table*, const Layout*, bool check_assertions);
77
78   // Return the value of an expression which is permitted to refer to
79   // the dot symbol.  DOT_VALUE is the absolute value of the dot
80   // symbol.  DOT_SECTION is the section in which dot is defined; it
81   // should be NULL if the dot symbol has an absolute value (e.g., is
82   // defined in a SECTIONS clause outside of any output section
83   // definition).  This sets *RESULT_SECTION to indicate where the
84   // value is defined.  If the value is absolute *RESULT_SECTION will
85   // be NULL.  Note that the returned value is still an absolute
86   // value; to get a section relative value the caller must subtract
87   // the section address.
88   uint64_t
89   eval_with_dot(const Symbol_table*, const Layout*, bool check_assertions,
90                 uint64_t dot_value, Output_section* dot_section,
91                 Output_section** result_section);
92
93   // Return the value of an expression which may or may not be
94   // permitted to refer to the dot symbol, depending on
95   // is_dot_available.
96   uint64_t
97   eval_maybe_dot(const Symbol_table*, const Layout*, bool check_assertions,
98                  bool is_dot_available, uint64_t dot_value,
99                  Output_section* dot_section,
100                  Output_section** result_section);
101
102   // Print the expression to the FILE.  This is for debugging.
103   virtual void
104   print(FILE*) const = 0;
105
106  protected:
107   struct Expression_eval_info;
108
109  public:
110   // Compute the value of the expression (implemented by child class).
111   // This is public rather than protected because it is called
112   // directly by children of Expression on other Expression objects.
113   virtual uint64_t
114   value(const Expression_eval_info*) = 0;
115
116  private:
117   // May not be copied.
118   Expression(const Expression&);
119   Expression& operator=(const Expression&);
120 };
121
122
123 // Version_script_info stores information parsed from the version
124 // script, either provided by --version-script or as part of a linker
125 // script.  A single Version_script_info object per target is owned by
126 // Script_options.
127
128 class Version_script_info
129 {
130  public:
131   ~Version_script_info();
132
133   // Clear everything.
134   void
135   clear();
136
137   // Return whether any version were defined in the version script.
138   bool
139   empty() const
140   { return this->version_trees_.empty(); }
141
142   // If there is a version associated with SYMBOL, return true, and
143   // set *VERSION to the version.  Otherwise, return false.
144   bool
145   get_symbol_version(const char* symbol, std::string* version) const
146   { return this->get_symbol_version_helper(symbol, true, version); }
147
148   // Return whether this symbol matches the local: section of some
149   // version.
150   bool
151   symbol_is_local(const char* symbol) const
152   { return this->get_symbol_version_helper(symbol, false, NULL); }
153
154   // Return the names of versions defined in the version script.
155   // Strings are allocated out of the stringpool given in the
156   // constructor.
157   std::vector<std::string>
158   get_versions() const;
159
160   // Return the list of dependencies for this version.
161   std::vector<std::string>
162   get_dependencies(const char* version) const;
163
164   // The following functions should only be used by the bison helper
165   // functions.  They allocate new structs whose memory belongs to
166   // Version_script_info.  The bison functions copy the information
167   // from the version script into these structs.
168   struct Version_dependency_list*
169   allocate_dependency_list();
170
171   struct Version_expression_list*
172   allocate_expression_list();
173
174   struct Version_tree*
175   allocate_version_tree();
176
177   // Print contents to the FILE.  This is for debugging.
178   void
179   print(FILE*) const;
180
181  private:
182   void
183   print_expression_list(FILE* f, const Version_expression_list*) const;
184
185   bool get_symbol_version_helper(const char* symbol,
186                                  bool check_global,
187                                  std::string* pversion) const;
188
189   std::vector<struct Version_dependency_list*> dependency_lists_;
190   std::vector<struct Version_expression_list*> expression_lists_;
191   std::vector<struct Version_tree*> version_trees_;
192 };
193
194 // This class manages assignments to symbols.  These can appear in
195 // three different locations in scripts: outside of a SECTIONS clause,
196 // within a SECTIONS clause, and within an output section definition
197 // within a SECTIONS clause.  This can also appear on the command line
198 // via the --defsym command line option.
199
200 class Symbol_assignment
201 {
202  public:
203   Symbol_assignment(const char* name, size_t namelen, Expression* val,
204                     bool provide, bool hidden)
205     : name_(name, namelen), val_(val), provide_(provide), hidden_(hidden),
206       sym_(NULL)
207   { }
208
209   // Add the symbol to the symbol table.
210   void
211   add_to_table(Symbol_table*);
212
213   // Finalize the symbol value.
214   void
215   finalize(Symbol_table*, const Layout*);
216
217   // Finalize the symbol value when it can refer to the dot symbol.
218   void
219   finalize_with_dot(Symbol_table*, const Layout*, uint64_t dot_value,
220                     Output_section* dot_section);
221
222   // Set the symbol value, but only if the value is absolute.  This is
223   // used while processing a SECTIONS clause.  We assume that dot is
224   // an absolute value here.  We do not check assertions.
225   void
226   set_if_absolute(Symbol_table*, const Layout*, bool is_dot_available,
227                   uint64_t dot_value);
228
229   // Print the assignment to the FILE.  This is for debugging.
230   void
231   print(FILE*) const;
232
233  private:
234   // Shared by finalize and finalize_with_dot.
235   void
236   finalize_maybe_dot(Symbol_table*, const Layout*, bool is_dot_available,
237                      uint64_t dot_value, Output_section* dot_section);
238
239   // Sized version of finalize.
240   template<int size>
241   void
242   sized_finalize(Symbol_table*, const Layout*, bool is_dot_available,
243                  uint64_t dot_value, Output_section*);
244
245   // Symbol name.
246   std::string name_;
247   // Expression to assign to symbol.
248   Expression* val_;
249   // Whether the assignment should be provided (only set if there is
250   // an undefined reference to the symbol.
251   bool provide_;
252   // Whether the assignment should be hidden.
253   bool hidden_;
254   // The entry in the symbol table.
255   Symbol* sym_;
256 };
257
258 // This class manages assertions in linker scripts.  These can appear
259 // in all the places where a Symbol_assignment can appear.
260
261 class Script_assertion
262 {
263  public:
264   Script_assertion(Expression* check, const char* message,
265                    size_t messagelen)
266     : check_(check), message_(message, messagelen)
267   { }
268
269   // Check the assertion.
270   void
271   check(const Symbol_table*, const Layout*);
272
273   // Print the assertion to the FILE.  This is for debugging.
274   void
275   print(FILE*) const;
276
277  private:
278   // The expression to check.
279   Expression* check_;
280   // The message to issue if the expression fails.
281   std::string message_;
282 };
283
284 // We can read a linker script in two different contexts: when
285 // initially parsing the command line, and when we find an input file
286 // which is actually a linker script.  Also some of the data which can
287 // be set by a linker script can also be set via command line options
288 // like -e and --defsym.  This means that we have a type of data which
289 // can be set both during command line option parsing and while
290 // reading input files.  We store that data in an instance of this
291 // object.  We will keep pointers to that instance in both the
292 // Command_line and Layout objects.
293
294 class Script_options
295 {
296  public:
297   Script_options();
298
299   // Add a symbol to be defined.
300   void
301   add_symbol_assignment(const char* name, size_t length, Expression* value,
302                         bool provide, bool hidden);
303
304   // Add an assertion.
305   void
306   add_assertion(Expression* check, const char* message, size_t messagelen);
307
308   // Define a symbol from the command line.
309   bool
310   define_symbol(const char* definition);
311
312   // Create sections required by any linker scripts.
313   void
314   create_script_sections(Layout*);
315
316   // Add all symbol definitions to the symbol table.
317   void
318   add_symbols_to_table(Symbol_table*);
319
320   // Finalize the symbol values.  Also check assertions.
321   void
322   finalize_symbols(Symbol_table*, const Layout*);
323
324   // Version information parsed from a version script.  Everything
325   // else has a pointer to this object.
326   Version_script_info*
327   version_script_info()
328   { return &this->version_script_info_; }
329
330   const Version_script_info*
331   version_script_info() const
332   { return &this->version_script_info_; }
333
334   // A SECTIONS clause parsed from a linker script.  Everything else
335   // has a pointer to this object.
336   Script_sections*
337   script_sections()
338   { return &this->script_sections_; }
339
340   const Script_sections*
341   script_sections() const
342   { return &this->script_sections_; }
343
344   // Whether we saw a SECTIONS clause.
345   bool
346   saw_sections_clause() const
347   { return this->script_sections_.saw_sections_clause(); }
348
349   // Whether we saw a PHDRS clause.
350   bool
351   saw_phdrs_clause() const
352   { return this->script_sections_.saw_phdrs_clause(); }
353
354   // Set section addresses using a SECTIONS clause.  Return the
355   // segment which should hold the file header and segment headers;
356   // this may return NULL, in which case the headers are not in a
357   // loadable segment.
358   Output_segment*
359   set_section_addresses(Symbol_table*, Layout*);
360
361   // Print the script to the FILE.  This is for debugging.
362   void
363   print(FILE*) const;
364
365  private:
366   // We keep a list of symbol assignments which occur outside of a
367   // SECTIONS clause.
368   typedef std::vector<Symbol_assignment*> Symbol_assignments;
369
370   // We keep a list of all assertions whcih occur outside of a
371   // SECTIONS clause.
372   typedef std::vector<Script_assertion*> Assertions;
373
374   // The entry address.  This will be empty if not set.
375   std::string entry_;
376   // Symbols to set.
377   Symbol_assignments symbol_assignments_;
378   // Assertions to check.
379   Assertions assertions_;
380   // Version information parsed from a version script.
381   Version_script_info version_script_info_;
382   // Information from any SECTIONS clauses.
383   Script_sections script_sections_;
384 };
385
386 // Information about a script input that will persist during the whole linker
387 // run. Needed only during an incremental build to retrieve the input files
388 // added by this script.
389
390 class Script_info
391 {
392  public:
393   Script_info(Input_arguments* inputs)
394     : inputs_(inputs)
395   { }
396
397   // Returns the input files included because of this script.
398   Input_arguments*
399   inputs()
400   { return this->inputs_; }
401
402  private:
403   Input_arguments* inputs_;
404 };
405
406 // FILE was found as an argument on the command line, but was not
407 // recognized as an ELF file.  Try to read it as a script.  Return
408 // true if the file was handled.  This has to handle /usr/lib/libc.so
409 // on a GNU/Linux system.  *USED_NEXT_BLOCKER is set to indicate
410 // whether the function took over NEXT_BLOCKER.
411
412 bool
413 read_input_script(Workqueue*, Symbol_table*, Layout*, Dirsearch*, int,
414                   Input_objects*, Mapfile*, Input_group*,
415                   const Input_argument*, Input_file*,
416                   Task_token* next_blocker, bool* used_next_blocker);
417
418 // FILE was found as an argument to --script (-T).
419 // Read it as a script, and execute its contents immediately.
420
421 bool
422 read_commandline_script(const char* filename, Command_line* cmdline);
423
424 // FILE was found as an argument to --version-script.  Read it as a
425 // version script, and store its contents in
426 // cmdline->script_options()->version_script_info().
427
428 bool
429 read_version_script(const char* filename, Command_line* cmdline);
430
431 // FILENAME was found as an argument to --dynamic-list.  Read it as a
432 // version script (actually, a versym_node from a version script), and
433 // store its contents in DYNAMIC_LIST.
434
435 bool
436 read_dynamic_list(const char* filename, Command_line* cmdline,
437                   Script_options* dynamic_list);
438
439 } // End namespace gold.
440
441 #endif // !defined(GOLD_SCRIPT_H)