OSDN Git Service

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