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