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