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