]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gold/script.h
PR gas/11867
[thirdparty/binutils-gdb.git] / gold / script.h
CommitLineData
dbe717ef
ILT
1// script.h -- handle linker scripts for gold -*- C++ -*-
2
62dfdd4d 3// Copyright 2006, 2007, 2008, 2009, 2010 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;
98e090bd
ILT
58struct Version_expression;
59class Lazy_demangler;
dbe717ef 60
e5756efb
ILT
61// This class represents an expression in a linker script.
62
63class Expression
64{
65 protected:
66 // These should only be created by child classes.
67 Expression()
68 { }
69
70 public:
71 virtual ~Expression()
72 { }
73
a445fddf 74 // Return the value of the expression which is not permitted to
919ed24c
ILT
75 // refer to the dot symbol. CHECK_ASSERTIONS is true if we should
76 // check whether assertions are true.
e5756efb 77 uint64_t
919ed24c 78 eval(const Symbol_table*, const Layout*, bool check_assertions);
e5756efb 79
a445fddf 80 // Return the value of an expression which is permitted to refer to
77e65537
ILT
81 // the dot symbol. DOT_VALUE is the absolute value of the dot
82 // symbol. DOT_SECTION is the section in which dot is defined; it
83 // should be NULL if the dot symbol has an absolute value (e.g., is
84 // defined in a SECTIONS clause outside of any output section
85 // definition). This sets *RESULT_SECTION to indicate where the
86 // value is defined. If the value is absolute *RESULT_SECTION will
87 // be NULL. Note that the returned value is still an absolute
88 // value; to get a section relative value the caller must subtract
f6973bdc
ILT
89 // the section address. If RESULT_ALIGNMENT is not NULL, this sets
90 // *RESULT_ALIGNMENT to the alignment of the value of that alignment
91 // is larger than *RESULT_ALIGNMENT; this will only be non-zero if
92 // this is an ALIGN expression.
a445fddf 93 uint64_t
919ed24c
ILT
94 eval_with_dot(const Symbol_table*, const Layout*, bool check_assertions,
95 uint64_t dot_value, Output_section* dot_section,
f6973bdc 96 Output_section** result_section, uint64_t* result_alignment);
a445fddf
ILT
97
98 // Return the value of an expression which may or may not be
99 // permitted to refer to the dot symbol, depending on
100 // is_dot_available.
101 uint64_t
919ed24c
ILT
102 eval_maybe_dot(const Symbol_table*, const Layout*, bool check_assertions,
103 bool is_dot_available, uint64_t dot_value,
104 Output_section* dot_section,
f6973bdc 105 Output_section** result_section, uint64_t* result_alignment);
a445fddf 106
494e05f4
ILT
107 // Print the expression to the FILE. This is for debugging.
108 virtual void
109 print(FILE*) const = 0;
110
e5756efb
ILT
111 protected:
112 struct Expression_eval_info;
113
114 public:
115 // Compute the value of the expression (implemented by child class).
116 // This is public rather than protected because it is called
117 // directly by children of Expression on other Expression objects.
118 virtual uint64_t
119 value(const Expression_eval_info*) = 0;
120
121 private:
122 // May not be copied.
123 Expression(const Expression&);
124 Expression& operator=(const Expression&);
125};
126
09124467
ILT
127
128// Version_script_info stores information parsed from the version
129// script, either provided by --version-script or as part of a linker
130// script. A single Version_script_info object per target is owned by
131// Script_options.
132
494e05f4
ILT
133class Version_script_info
134{
09124467 135 public:
6affe781
ILT
136 // The languages which can be specified in a versionn script.
137 enum Language
138 {
139 LANGUAGE_C, // No demangling.
140 LANGUAGE_CXX, // C++ demangling.
141 LANGUAGE_JAVA, // Java demangling.
142 LANGUAGE_COUNT
143 };
144
145 Version_script_info();
146
09124467
ILT
147 ~Version_script_info();
148
1ef1f3d3
ILT
149 // Clear everything.
150 void
151 clear();
152
6affe781
ILT
153 // Finalize the version control information.
154 void
155 finalize();
156
157 // Return whether the information is finalized.
158 bool
159 is_finalized() const
160 { return this->is_finalized_; }
161
09124467
ILT
162 // Return whether any version were defined in the version script.
163 bool
164 empty() const
165 { return this->version_trees_.empty(); }
166
057ead22 167 // If there is a version associated with SYMBOL, return true, and
98e090bd
ILT
168 // set *VERSION to the version, and *IS_GLOBAL to whether the symbol
169 // should be global. Otherwise, return false.
057ead22 170 bool
98e090bd
ILT
171 get_symbol_version(const char* symbol, std::string* version,
172 bool* is_global) const;
057ead22
ILT
173
174 // Return whether this symbol matches the local: section of some
175 // version.
09124467
ILT
176 bool
177 symbol_is_local(const char* symbol) const
98e090bd
ILT
178 {
179 bool is_global;
180 return (this->get_symbol_version(symbol, NULL, &is_global)
181 && !is_global);
182 }
09124467
ILT
183
184 // Return the names of versions defined in the version script.
09124467
ILT
185 std::vector<std::string>
186 get_versions() const;
187
188 // Return the list of dependencies for this version.
189 std::vector<std::string>
190 get_dependencies(const char* version) const;
191
192 // The following functions should only be used by the bison helper
193 // functions. They allocate new structs whose memory belongs to
194 // Version_script_info. The bison functions copy the information
195 // from the version script into these structs.
196 struct Version_dependency_list*
197 allocate_dependency_list();
198
199 struct Version_expression_list*
200 allocate_expression_list();
201
202 struct Version_tree*
203 allocate_version_tree();
204
6affe781
ILT
205 // Build the lookup tables after all data have been read.
206 void
207 build_lookup_tables();
208
62dfdd4d
ILT
209 // Give an error if there are any unmatched names in the version
210 // script.
211 void
212 check_unmatched_names(const Symbol_table*) const;
213
494e05f4
ILT
214 // Print contents to the FILE. This is for debugging.
215 void
216 print(FILE*) const;
217
09124467 218 private:
494e05f4
ILT
219 void
220 print_expression_list(FILE* f, const Version_expression_list*) const;
221
6affe781
ILT
222 bool
223 get_symbol_version_helper(const char* symbol,
224 bool check_global,
225 std::string* pversion) const;
226
98e090bd
ILT
227 // Fast lookup information for a given language.
228
229 // We map from exact match strings to Version_tree's. Historically
230 // version scripts sometimes have the same symbol multiple times,
231 // which is ambiguous. We warn about that case by storing the
232 // second Version_tree we see.
233 struct Version_tree_match
234 {
235 Version_tree_match(const Version_tree* r, bool ig,
236 const Version_expression* e)
237 : real(r), is_global(ig), expression(e), ambiguous(NULL)
238 { }
239
240 // The Version_tree that we return.
241 const Version_tree* real;
242 // True if this is a global match for the REAL member, false if it
243 // is a local match.
244 bool is_global;
245 // Point back to the Version_expression for which we created this
246 // match.
247 const Version_expression* expression;
248 // If not NULL, another Version_tree that defines the symbol.
249 const Version_tree* ambiguous;
250 };
251
252 // Map from an exact match string to a Version_tree.
253
254 typedef Unordered_map<std::string, Version_tree_match> Exact;
62dfdd4d 255
6affe781
ILT
256 // Fast lookup information for a glob pattern.
257 struct Glob
258 {
259 Glob()
98e090bd 260 : expression(NULL), version(NULL), is_global(false)
6affe781
ILT
261 { }
262
98e090bd
ILT
263 Glob(const Version_expression* e, const Version_tree* v, bool ig)
264 : expression(e), version(v), is_global(ig)
6affe781
ILT
265 { }
266
98e090bd
ILT
267 // A pointer to the version expression holding the pattern to
268 // match and the language to use for demangling the symbol before
269 // doing the match.
270 const Version_expression* expression;
6affe781
ILT
271 // The Version_tree we use if this pattern matches.
272 const Version_tree* version;
98e090bd
ILT
273 // True if this is a global symbol.
274 bool is_global;
6affe781
ILT
275 };
276
98e090bd 277 typedef std::vector<Glob> Globs;
6affe781 278
98e090bd
ILT
279 bool
280 unquote(std::string*) const;
6affe781 281
98e090bd
ILT
282 void
283 add_exact_match(const std::string&, const Version_tree*, bool is_global,
284 const Version_expression*, Exact*);
09124467 285
6affe781
ILT
286 void
287 build_expression_list_lookup(const Version_expression_list*,
98e090bd
ILT
288 const Version_tree*, bool);
289
290 const char*
291 get_name_to_match(const char*, int,
292 Lazy_demangler*, Lazy_demangler*) const;
6affe781
ILT
293
294 // All the version dependencies we allocate.
295 std::vector<Version_dependency_list*> dependency_lists_;
296 // All the version expressions we allocate.
297 std::vector<Version_expression_list*> expression_lists_;
298 // The list of versions.
299 std::vector<Version_tree*> version_trees_;
98e090bd
ILT
300 // Exact matches for global symbols, by language.
301 Exact* exact_[LANGUAGE_COUNT];
302 // A vector of glob patterns mapping to Version_trees.
303 Globs globs_;
304 // The default version to use, if there is one. This is from a
305 // pattern of "*".
306 const Version_tree* default_version_;
307 // True if the default version is global.
308 bool default_is_global_;
6affe781
ILT
309 // Whether this has been finalized.
310 bool is_finalized_;
09124467
ILT
311};
312
494e05f4
ILT
313// This class manages assignments to symbols. These can appear in
314// three different locations in scripts: outside of a SECTIONS clause,
315// within a SECTIONS clause, and within an output section definition
316// within a SECTIONS clause. This can also appear on the command line
317// via the --defsym command line option.
318
319class Symbol_assignment
320{
321 public:
99fff23b
ILT
322 Symbol_assignment(const char* name, size_t namelen, bool is_defsym,
323 Expression* val, bool provide, bool hidden)
324 : name_(name, namelen), val_(val), is_defsym_(is_defsym),
325 provide_(provide), hidden_(hidden), sym_(NULL)
494e05f4
ILT
326 { }
327
328 // Add the symbol to the symbol table.
329 void
9b07f471 330 add_to_table(Symbol_table*);
494e05f4
ILT
331
332 // Finalize the symbol value.
a445fddf
ILT
333 void
334 finalize(Symbol_table*, const Layout*);
335
336 // Finalize the symbol value when it can refer to the dot symbol.
337 void
77e65537
ILT
338 finalize_with_dot(Symbol_table*, const Layout*, uint64_t dot_value,
339 Output_section* dot_section);
a445fddf
ILT
340
341 // Set the symbol value, but only if the value is absolute. This is
77e65537 342 // used while processing a SECTIONS clause. We assume that dot is
919ed24c 343 // an absolute value here. We do not check assertions.
a445fddf
ILT
344 void
345 set_if_absolute(Symbol_table*, const Layout*, bool is_dot_available,
77e65537 346 uint64_t dot_value);
494e05f4
ILT
347
348 // Print the assignment to the FILE. This is for debugging.
349 void
350 print(FILE*) const;
351
352 private:
a445fddf
ILT
353 // Shared by finalize and finalize_with_dot.
354 void
355 finalize_maybe_dot(Symbol_table*, const Layout*, bool is_dot_available,
77e65537 356 uint64_t dot_value, Output_section* dot_section);
a445fddf 357
494e05f4
ILT
358 // Sized version of finalize.
359 template<int size>
360 void
a445fddf 361 sized_finalize(Symbol_table*, const Layout*, bool is_dot_available,
77e65537 362 uint64_t dot_value, Output_section*);
494e05f4
ILT
363
364 // Symbol name.
365 std::string name_;
366 // Expression to assign to symbol.
367 Expression* val_;
99fff23b
ILT
368 // True if this symbol is defined by a --defsym, false if it is
369 // defined in a linker script.
370 bool is_defsym_;
494e05f4
ILT
371 // Whether the assignment should be provided (only set if there is
372 // an undefined reference to the symbol.
373 bool provide_;
374 // Whether the assignment should be hidden.
375 bool hidden_;
376 // The entry in the symbol table.
377 Symbol* sym_;
378};
379
380// This class manages assertions in linker scripts. These can appear
381// in all the places where a Symbol_assignment can appear.
382
383class Script_assertion
384{
385 public:
2ea97941 386 Script_assertion(Expression* check, const char* message,
494e05f4 387 size_t messagelen)
2ea97941 388 : check_(check), message_(message, messagelen)
494e05f4
ILT
389 { }
390
391 // Check the assertion.
392 void
393 check(const Symbol_table*, const Layout*);
394
395 // Print the assertion to the FILE. This is for debugging.
396 void
397 print(FILE*) const;
398
399 private:
400 // The expression to check.
401 Expression* check_;
402 // The message to issue if the expression fails.
403 std::string message_;
404};
405
e5756efb
ILT
406// We can read a linker script in two different contexts: when
407// initially parsing the command line, and when we find an input file
408// which is actually a linker script. Also some of the data which can
409// be set by a linker script can also be set via command line options
410// like -e and --defsym. This means that we have a type of data which
411// can be set both during command line option parsing and while
412// reading input files. We store that data in an instance of this
413// object. We will keep pointers to that instance in both the
414// Command_line and Layout objects.
415
416class Script_options
417{
418 public:
419 Script_options();
420
494e05f4 421 // Add a symbol to be defined.
e5756efb 422 void
99fff23b
ILT
423 add_symbol_assignment(const char* name, size_t length, bool is_defsym,
424 Expression* value, bool provide, bool hidden);
494e05f4
ILT
425
426 // Add an assertion.
427 void
428 add_assertion(Expression* check, const char* message, size_t messagelen);
e5756efb
ILT
429
430 // Define a symbol from the command line.
431 bool
432 define_symbol(const char* definition);
433
919ed24c
ILT
434 // Create sections required by any linker scripts.
435 void
436 create_script_sections(Layout*);
437
e5756efb
ILT
438 // Add all symbol definitions to the symbol table.
439 void
9b07f471 440 add_symbols_to_table(Symbol_table*);
e5756efb 441
a445fddf 442 // Finalize the symbol values. Also check assertions.
e5756efb
ILT
443 void
444 finalize_symbols(Symbol_table*, const Layout*);
445
09124467
ILT
446 // Version information parsed from a version script. Everything
447 // else has a pointer to this object.
448 Version_script_info*
449 version_script_info()
494e05f4 450 { return &this->version_script_info_; }
09124467 451
a5dc0706
ILT
452 const Version_script_info*
453 version_script_info() const
454 { return &this->version_script_info_; }
455
494e05f4
ILT
456 // A SECTIONS clause parsed from a linker script. Everything else
457 // has a pointer to this object.
458 Script_sections*
459 script_sections()
460 { return &this->script_sections_; }
e5756efb 461
8f2eb564
ILT
462 const Script_sections*
463 script_sections() const
464 { return &this->script_sections_; }
465
a445fddf
ILT
466 // Whether we saw a SECTIONS clause.
467 bool
468 saw_sections_clause() const
469 { return this->script_sections_.saw_sections_clause(); }
470
1c4f3631
ILT
471 // Whether we saw a PHDRS clause.
472 bool
473 saw_phdrs_clause() const
474 { return this->script_sections_.saw_phdrs_clause(); }
475
a445fddf
ILT
476 // Set section addresses using a SECTIONS clause. Return the
477 // segment which should hold the file header and segment headers;
478 // this may return NULL, in which case the headers are not in a
479 // loadable segment.
480 Output_segment*
481 set_section_addresses(Symbol_table*, Layout*);
482
494e05f4 483 // Print the script to the FILE. This is for debugging.
e5756efb 484 void
494e05f4
ILT
485 print(FILE*) const;
486
487 private:
488 // We keep a list of symbol assignments which occur outside of a
489 // SECTIONS clause.
490 typedef std::vector<Symbol_assignment*> Symbol_assignments;
491
492 // We keep a list of all assertions whcih occur outside of a
493 // SECTIONS clause.
494 typedef std::vector<Script_assertion*> Assertions;
e5756efb
ILT
495
496 // The entry address. This will be empty if not set.
497 std::string entry_;
498 // Symbols to set.
499 Symbol_assignments symbol_assignments_;
494e05f4
ILT
500 // Assertions to check.
501 Assertions assertions_;
09124467
ILT
502 // Version information parsed from a version script.
503 Version_script_info version_script_info_;
494e05f4
ILT
504 // Information from any SECTIONS clauses.
505 Script_sections script_sections_;
e5756efb
ILT
506};
507
072fe7ce
ILT
508// Information about a script input that will persist during the whole linker
509// run. Needed only during an incremental build to retrieve the input files
510// added by this script.
511
512class Script_info
513{
514 public:
2ea97941
ILT
515 Script_info(Input_arguments* inputs)
516 : inputs_(inputs)
072fe7ce
ILT
517 { }
518
519 // Returns the input files included because of this script.
520 Input_arguments*
521 inputs()
98fa85cb 522 { return this->inputs_; }
072fe7ce
ILT
523
524 private:
525 Input_arguments* inputs_;
526};
527
dbe717ef 528// FILE was found as an argument on the command line, but was not
da769d56
ILT
529// recognized as an ELF file. Try to read it as a script. Return
530// true if the file was handled. This has to handle /usr/lib/libc.so
531// on a GNU/Linux system. *USED_NEXT_BLOCKER is set to indicate
532// whether the function took over NEXT_BLOCKER.
dbe717ef
ILT
533
534bool
15f8229b 535read_input_script(Workqueue*, Symbol_table*, Layout*, Dirsearch*, int,
f1ed28fb 536 Input_objects*, Mapfile*, Input_group*,
da769d56
ILT
537 const Input_argument*, Input_file*,
538 Task_token* next_blocker, bool* used_next_blocker);
dbe717ef 539
3c2fafa5
ILT
540// FILE was found as an argument to --script (-T).
541// Read it as a script, and execute its contents immediately.
542
543bool
c82fbeee 544read_commandline_script(const char* filename, Command_line* cmdline);
3c2fafa5 545
09124467
ILT
546// FILE was found as an argument to --version-script. Read it as a
547// version script, and store its contents in
548// cmdline->script_options()->version_script_info().
549
550bool
551read_version_script(const char* filename, Command_line* cmdline);
552
c82fbeee
CS
553// FILENAME was found as an argument to --dynamic-list. Read it as a
554// version script (actually, a versym_node from a version script), and
555// store its contents in DYNAMIC_LIST.
556
557bool
558read_dynamic_list(const char* filename, Command_line* cmdline,
559 Script_options* dynamic_list);
560
dbe717ef
ILT
561} // End namespace gold.
562
563#endif // !defined(GOLD_SCRIPT_H)