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