PR 10861
[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>
04bf7072 34#include <string>
e5756efb
ILT
35#include <vector>
36
494e05f4 37#include "script-sections.h"
09124467 38
dbe717ef
ILT
39namespace gold
40{
41
42class General_options;
3c2fafa5 43class Command_line;
dbe717ef
ILT
44class Symbol_table;
45class Layout;
7d9e3d98 46class Mapfile;
3c2fafa5 47class Input_argument;
072fe7ce 48class Input_arguments;
dbe717ef
ILT
49class Input_objects;
50class Input_group;
51class Input_file;
a445fddf 52class Output_segment;
dbe717ef 53class Task_token;
3c2fafa5 54class Workqueue;
494e05f4
ILT
55struct Version_dependency_list;
56struct Version_expression_list;
57struct Version_tree;
dbe717ef 58
e5756efb
ILT
59// This class represents an expression in a linker script.
60
61class Expression
62{
63 protected:
64 // These should only be created by child classes.
65 Expression()
66 { }
67
68 public:
69 virtual ~Expression()
70 { }
71
a445fddf 72 // Return the value of the expression which is not permitted to
919ed24c
ILT
73 // refer to the dot symbol. CHECK_ASSERTIONS is true if we should
74 // check whether assertions are true.
e5756efb 75 uint64_t
919ed24c 76 eval(const Symbol_table*, const Layout*, bool check_assertions);
e5756efb 77
a445fddf 78 // Return the value of an expression which is permitted to refer to
77e65537
ILT
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.
a445fddf 88 uint64_t
919ed24c
ILT
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);
a445fddf
ILT
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
919ed24c
ILT
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,
77e65537 100 Output_section** result_section);
a445fddf 101
494e05f4
ILT
102 // Print the expression to the FILE. This is for debugging.
103 virtual void
104 print(FILE*) const = 0;
105
e5756efb
ILT
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
09124467
ILT
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
494e05f4
ILT
128class Version_script_info
129{
09124467 130 public:
6affe781
ILT
131 // The languages which can be specified in a versionn script.
132 enum Language
133 {
134 LANGUAGE_C, // No demangling.
135 LANGUAGE_CXX, // C++ demangling.
136 LANGUAGE_JAVA, // Java demangling.
137 LANGUAGE_COUNT
138 };
139
140 Version_script_info();
141
09124467
ILT
142 ~Version_script_info();
143
1ef1f3d3
ILT
144 // Clear everything.
145 void
146 clear();
147
6affe781
ILT
148 // Finalize the version control information.
149 void
150 finalize();
151
152 // Return whether the information is finalized.
153 bool
154 is_finalized() const
155 { return this->is_finalized_; }
156
09124467
ILT
157 // Return whether any version were defined in the version script.
158 bool
159 empty() const
160 { return this->version_trees_.empty(); }
161
057ead22
ILT
162 // If there is a version associated with SYMBOL, return true, and
163 // set *VERSION to the version. Otherwise, return false.
164 bool
165 get_symbol_version(const char* symbol, std::string* version) const
166 { return this->get_symbol_version_helper(symbol, true, version); }
167
168 // Return whether this symbol matches the local: section of some
169 // version.
09124467
ILT
170 bool
171 symbol_is_local(const char* symbol) const
057ead22 172 { return this->get_symbol_version_helper(symbol, false, NULL); }
09124467
ILT
173
174 // Return the names of versions defined in the version script.
09124467
ILT
175 std::vector<std::string>
176 get_versions() const;
177
178 // Return the list of dependencies for this version.
179 std::vector<std::string>
180 get_dependencies(const char* version) const;
181
182 // The following functions should only be used by the bison helper
183 // functions. They allocate new structs whose memory belongs to
184 // Version_script_info. The bison functions copy the information
185 // from the version script into these structs.
186 struct Version_dependency_list*
187 allocate_dependency_list();
188
189 struct Version_expression_list*
190 allocate_expression_list();
191
192 struct Version_tree*
193 allocate_version_tree();
194
6affe781
ILT
195 // Build the lookup tables after all data have been read.
196 void
197 build_lookup_tables();
198
494e05f4
ILT
199 // Print contents to the FILE. This is for debugging.
200 void
201 print(FILE*) const;
202
09124467 203 private:
494e05f4
ILT
204 void
205 print_expression_list(FILE* f, const Version_expression_list*) const;
206
6affe781
ILT
207 bool
208 get_symbol_version_helper(const char* symbol,
209 bool check_global,
210 std::string* pversion) const;
211
212 // Fast lookup information for a glob pattern.
213 struct Glob
214 {
215 Glob()
216 : pattern(NULL), version(NULL)
217 { }
218
219 Glob(const char* p, const Version_tree* v)
220 : pattern(p), version(v)
221 { }
222
223 // A pointer to the glob pattern. The pattern itself lives in a
224 // Version_expression structure.
225 const char* pattern;
226 // The Version_tree we use if this pattern matches.
227 const Version_tree* version;
228 };
229
230 // Fast lookup information for a given language.
231
232 typedef Unordered_map<std::string, const Version_tree*> Exact;
233
234 struct Lookup
235 {
236 // A hash table of all exact match strings mapping to a
237 // Version_tree.
238 Exact exact;
239 // A vector of glob patterns mapping to Version_trees.
240 std::vector<Glob> globs;
241 };
09124467 242
6affe781
ILT
243 void
244 build_expression_list_lookup(const Version_expression_list*,
245 const Version_tree*, Lookup**);
246
247 // All the version dependencies we allocate.
248 std::vector<Version_dependency_list*> dependency_lists_;
249 // All the version expressions we allocate.
250 std::vector<Version_expression_list*> expression_lists_;
251 // The list of versions.
252 std::vector<Version_tree*> version_trees_;
253 // Lookup information for global symbols, by language.
254 Lookup* globals_[LANGUAGE_COUNT];
255 // Lookup information for local symbols, by language.
256 Lookup* locals_[LANGUAGE_COUNT];
257 // Whether this has been finalized.
258 bool is_finalized_;
09124467
ILT
259};
260
494e05f4
ILT
261// This class manages assignments to symbols. These can appear in
262// three different locations in scripts: outside of a SECTIONS clause,
263// within a SECTIONS clause, and within an output section definition
264// within a SECTIONS clause. This can also appear on the command line
265// via the --defsym command line option.
266
267class Symbol_assignment
268{
269 public:
99fff23b
ILT
270 Symbol_assignment(const char* name, size_t namelen, bool is_defsym,
271 Expression* val, bool provide, bool hidden)
272 : name_(name, namelen), val_(val), is_defsym_(is_defsym),
273 provide_(provide), hidden_(hidden), sym_(NULL)
494e05f4
ILT
274 { }
275
276 // Add the symbol to the symbol table.
277 void
9b07f471 278 add_to_table(Symbol_table*);
494e05f4
ILT
279
280 // Finalize the symbol value.
a445fddf
ILT
281 void
282 finalize(Symbol_table*, const Layout*);
283
284 // Finalize the symbol value when it can refer to the dot symbol.
285 void
77e65537
ILT
286 finalize_with_dot(Symbol_table*, const Layout*, uint64_t dot_value,
287 Output_section* dot_section);
a445fddf
ILT
288
289 // Set the symbol value, but only if the value is absolute. This is
77e65537 290 // used while processing a SECTIONS clause. We assume that dot is
919ed24c 291 // an absolute value here. We do not check assertions.
a445fddf
ILT
292 void
293 set_if_absolute(Symbol_table*, const Layout*, bool is_dot_available,
77e65537 294 uint64_t dot_value);
494e05f4
ILT
295
296 // Print the assignment to the FILE. This is for debugging.
297 void
298 print(FILE*) const;
299
300 private:
a445fddf
ILT
301 // Shared by finalize and finalize_with_dot.
302 void
303 finalize_maybe_dot(Symbol_table*, const Layout*, bool is_dot_available,
77e65537 304 uint64_t dot_value, Output_section* dot_section);
a445fddf 305
494e05f4
ILT
306 // Sized version of finalize.
307 template<int size>
308 void
a445fddf 309 sized_finalize(Symbol_table*, const Layout*, bool is_dot_available,
77e65537 310 uint64_t dot_value, Output_section*);
494e05f4
ILT
311
312 // Symbol name.
313 std::string name_;
314 // Expression to assign to symbol.
315 Expression* val_;
99fff23b
ILT
316 // True if this symbol is defined by a --defsym, false if it is
317 // defined in a linker script.
318 bool is_defsym_;
494e05f4
ILT
319 // Whether the assignment should be provided (only set if there is
320 // an undefined reference to the symbol.
321 bool provide_;
322 // Whether the assignment should be hidden.
323 bool hidden_;
324 // The entry in the symbol table.
325 Symbol* sym_;
326};
327
328// This class manages assertions in linker scripts. These can appear
329// in all the places where a Symbol_assignment can appear.
330
331class Script_assertion
332{
333 public:
2ea97941 334 Script_assertion(Expression* check, const char* message,
494e05f4 335 size_t messagelen)
2ea97941 336 : check_(check), message_(message, messagelen)
494e05f4
ILT
337 { }
338
339 // Check the assertion.
340 void
341 check(const Symbol_table*, const Layout*);
342
343 // Print the assertion to the FILE. This is for debugging.
344 void
345 print(FILE*) const;
346
347 private:
348 // The expression to check.
349 Expression* check_;
350 // The message to issue if the expression fails.
351 std::string message_;
352};
353
e5756efb
ILT
354// We can read a linker script in two different contexts: when
355// initially parsing the command line, and when we find an input file
356// which is actually a linker script. Also some of the data which can
357// be set by a linker script can also be set via command line options
358// like -e and --defsym. This means that we have a type of data which
359// can be set both during command line option parsing and while
360// reading input files. We store that data in an instance of this
361// object. We will keep pointers to that instance in both the
362// Command_line and Layout objects.
363
364class Script_options
365{
366 public:
367 Script_options();
368
494e05f4 369 // Add a symbol to be defined.
e5756efb 370 void
99fff23b
ILT
371 add_symbol_assignment(const char* name, size_t length, bool is_defsym,
372 Expression* value, bool provide, bool hidden);
494e05f4
ILT
373
374 // Add an assertion.
375 void
376 add_assertion(Expression* check, const char* message, size_t messagelen);
e5756efb
ILT
377
378 // Define a symbol from the command line.
379 bool
380 define_symbol(const char* definition);
381
919ed24c
ILT
382 // Create sections required by any linker scripts.
383 void
384 create_script_sections(Layout*);
385
e5756efb
ILT
386 // Add all symbol definitions to the symbol table.
387 void
9b07f471 388 add_symbols_to_table(Symbol_table*);
e5756efb 389
a445fddf 390 // Finalize the symbol values. Also check assertions.
e5756efb
ILT
391 void
392 finalize_symbols(Symbol_table*, const Layout*);
393
09124467
ILT
394 // Version information parsed from a version script. Everything
395 // else has a pointer to this object.
396 Version_script_info*
397 version_script_info()
494e05f4 398 { return &this->version_script_info_; }
09124467 399
a5dc0706
ILT
400 const Version_script_info*
401 version_script_info() const
402 { return &this->version_script_info_; }
403
494e05f4
ILT
404 // A SECTIONS clause parsed from a linker script. Everything else
405 // has a pointer to this object.
406 Script_sections*
407 script_sections()
408 { return &this->script_sections_; }
e5756efb 409
8f2eb564
ILT
410 const Script_sections*
411 script_sections() const
412 { return &this->script_sections_; }
413
a445fddf
ILT
414 // Whether we saw a SECTIONS clause.
415 bool
416 saw_sections_clause() const
417 { return this->script_sections_.saw_sections_clause(); }
418
1c4f3631
ILT
419 // Whether we saw a PHDRS clause.
420 bool
421 saw_phdrs_clause() const
422 { return this->script_sections_.saw_phdrs_clause(); }
423
a445fddf
ILT
424 // Set section addresses using a SECTIONS clause. Return the
425 // segment which should hold the file header and segment headers;
426 // this may return NULL, in which case the headers are not in a
427 // loadable segment.
428 Output_segment*
429 set_section_addresses(Symbol_table*, Layout*);
430
494e05f4 431 // Print the script to the FILE. This is for debugging.
e5756efb 432 void
494e05f4
ILT
433 print(FILE*) const;
434
435 private:
436 // We keep a list of symbol assignments which occur outside of a
437 // SECTIONS clause.
438 typedef std::vector<Symbol_assignment*> Symbol_assignments;
439
440 // We keep a list of all assertions whcih occur outside of a
441 // SECTIONS clause.
442 typedef std::vector<Script_assertion*> Assertions;
e5756efb
ILT
443
444 // The entry address. This will be empty if not set.
445 std::string entry_;
446 // Symbols to set.
447 Symbol_assignments symbol_assignments_;
494e05f4
ILT
448 // Assertions to check.
449 Assertions assertions_;
09124467
ILT
450 // Version information parsed from a version script.
451 Version_script_info version_script_info_;
494e05f4
ILT
452 // Information from any SECTIONS clauses.
453 Script_sections script_sections_;
e5756efb
ILT
454};
455
072fe7ce
ILT
456// Information about a script input that will persist during the whole linker
457// run. Needed only during an incremental build to retrieve the input files
458// added by this script.
459
460class Script_info
461{
462 public:
2ea97941
ILT
463 Script_info(Input_arguments* inputs)
464 : inputs_(inputs)
072fe7ce
ILT
465 { }
466
467 // Returns the input files included because of this script.
468 Input_arguments*
469 inputs()
98fa85cb 470 { return this->inputs_; }
072fe7ce
ILT
471
472 private:
473 Input_arguments* inputs_;
474};
475
dbe717ef 476// FILE was found as an argument on the command line, but was not
da769d56
ILT
477// recognized as an ELF file. Try to read it as a script. Return
478// true if the file was handled. This has to handle /usr/lib/libc.so
479// on a GNU/Linux system. *USED_NEXT_BLOCKER is set to indicate
480// whether the function took over NEXT_BLOCKER.
dbe717ef
ILT
481
482bool
15f8229b 483read_input_script(Workqueue*, Symbol_table*, Layout*, Dirsearch*, int,
f1ed28fb 484 Input_objects*, Mapfile*, Input_group*,
da769d56
ILT
485 const Input_argument*, Input_file*,
486 Task_token* next_blocker, bool* used_next_blocker);
dbe717ef 487
3c2fafa5
ILT
488// FILE was found as an argument to --script (-T).
489// Read it as a script, and execute its contents immediately.
490
491bool
c82fbeee 492read_commandline_script(const char* filename, Command_line* cmdline);
3c2fafa5 493
09124467
ILT
494// FILE was found as an argument to --version-script. Read it as a
495// version script, and store its contents in
496// cmdline->script_options()->version_script_info().
497
498bool
499read_version_script(const char* filename, Command_line* cmdline);
500
c82fbeee
CS
501// FILENAME was found as an argument to --dynamic-list. Read it as a
502// version script (actually, a versym_node from a version script), and
503// store its contents in DYNAMIC_LIST.
504
505bool
506read_dynamic_list(const char* filename, Command_line* cmdline,
507 Script_options* dynamic_list);
508
dbe717ef
ILT
509} // End namespace gold.
510
511#endif // !defined(GOLD_SCRIPT_H)
This page took 0.155868 seconds and 4 git commands to generate.