Treat an empty directory argument as the current directory.
[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>
e5756efb
ILT
34#include <vector>
35
494e05f4 36#include "script-sections.h"
09124467 37
dbe717ef
ILT
38namespace gold
39{
40
41class General_options;
3c2fafa5 42class Command_line;
dbe717ef
ILT
43class Symbol_table;
44class Layout;
3c2fafa5 45class Input_argument;
dbe717ef
ILT
46class Input_objects;
47class Input_group;
48class Input_file;
a445fddf 49class Output_segment;
dbe717ef 50class Task_token;
3c2fafa5 51class Workqueue;
494e05f4
ILT
52struct Version_dependency_list;
53struct Version_expression_list;
54struct Version_tree;
dbe717ef 55
e5756efb
ILT
56// This class represents an expression in a linker script.
57
58class Expression
59{
60 protected:
61 // These should only be created by child classes.
62 Expression()
63 { }
64
65 public:
66 virtual ~Expression()
67 { }
68
a445fddf
ILT
69 // Return the value of the expression which is not permitted to
70 // refer to the dot symbol.
e5756efb
ILT
71 uint64_t
72 eval(const Symbol_table*, const Layout*);
73
a445fddf
ILT
74 // Return the value of an expression which is permitted to refer to
75 // the dot symbol. This sets *IS_ABSOLUTE to indicate whether this
76 // is an absolute value; it will be false if a non-absolute symbol
77 // was referenced in the expression; this is used to detect invalid
78 // uses when setting a section address.
79 uint64_t
80 eval_with_dot(const Symbol_table*, const Layout*, bool dot_has_value,
81 uint64_t dot_value, bool* is_absolute);
82
83 // Return the value of an expression which may or may not be
84 // permitted to refer to the dot symbol, depending on
85 // is_dot_available.
86 uint64_t
87 eval_maybe_dot(const Symbol_table*, const Layout*, bool is_dot_available,
88 bool dot_has_value, uint64_t dot_value, bool* is_absolute);
89
494e05f4
ILT
90 // Print the expression to the FILE. This is for debugging.
91 virtual void
92 print(FILE*) const = 0;
93
e5756efb
ILT
94 protected:
95 struct Expression_eval_info;
96
97 public:
98 // Compute the value of the expression (implemented by child class).
99 // This is public rather than protected because it is called
100 // directly by children of Expression on other Expression objects.
101 virtual uint64_t
102 value(const Expression_eval_info*) = 0;
103
104 private:
105 // May not be copied.
106 Expression(const Expression&);
107 Expression& operator=(const Expression&);
108};
109
09124467
ILT
110
111// Version_script_info stores information parsed from the version
112// script, either provided by --version-script or as part of a linker
113// script. A single Version_script_info object per target is owned by
114// Script_options.
115
494e05f4
ILT
116class Version_script_info
117{
09124467
ILT
118 public:
119 ~Version_script_info();
120
121 // Return whether any version were defined in the version script.
122 bool
123 empty() const
124 { return this->version_trees_.empty(); }
125
126 // Return the version associated with the given symbol name.
127 // Strings are allocated out of the stringpool given in the
128 // constructor. Strings are allocated out of the stringpool given
129 // in the constructor.
130 const std::string&
131 get_symbol_version(const char* symbol) const
132 { return get_symbol_version_helper(symbol, true); }
133
134 // Return whether this symbol matches the local: section of a
1dd940af 135 // version script (it doesn't matter which).
09124467
ILT
136 bool
137 symbol_is_local(const char* symbol) const
1dd940af
ILT
138 {
139 return (get_symbol_version(symbol).empty()
140 && !get_symbol_version_helper(symbol, false).empty());
141 }
09124467
ILT
142
143 // Return the names of versions defined in the version script.
144 // Strings are allocated out of the stringpool given in the
145 // constructor.
146 std::vector<std::string>
147 get_versions() const;
148
149 // Return the list of dependencies for this version.
150 std::vector<std::string>
151 get_dependencies(const char* version) const;
152
153 // The following functions should only be used by the bison helper
154 // functions. They allocate new structs whose memory belongs to
155 // Version_script_info. The bison functions copy the information
156 // from the version script into these structs.
157 struct Version_dependency_list*
158 allocate_dependency_list();
159
160 struct Version_expression_list*
161 allocate_expression_list();
162
163 struct Version_tree*
164 allocate_version_tree();
165
494e05f4
ILT
166 // Print contents to the FILE. This is for debugging.
167 void
168 print(FILE*) const;
169
09124467 170 private:
494e05f4
ILT
171 void
172 print_expression_list(FILE* f, const Version_expression_list*) const;
173
09124467
ILT
174 const std::string& get_symbol_version_helper(const char* symbol,
175 bool check_global) const;
176
177 std::vector<struct Version_dependency_list*> dependency_lists_;
178 std::vector<struct Version_expression_list*> expression_lists_;
179 std::vector<struct Version_tree*> version_trees_;
180};
181
494e05f4
ILT
182// This class manages assignments to symbols. These can appear in
183// three different locations in scripts: outside of a SECTIONS clause,
184// within a SECTIONS clause, and within an output section definition
185// within a SECTIONS clause. This can also appear on the command line
186// via the --defsym command line option.
187
188class Symbol_assignment
189{
190 public:
191 Symbol_assignment(const char* name, size_t namelen, Expression* val,
192 bool provide, bool hidden)
193 : name_(name, namelen), val_(val), provide_(provide), hidden_(hidden),
194 sym_(NULL)
195 { }
196
197 // Add the symbol to the symbol table.
198 void
9b07f471 199 add_to_table(Symbol_table*);
494e05f4
ILT
200
201 // Finalize the symbol value.
a445fddf
ILT
202 void
203 finalize(Symbol_table*, const Layout*);
204
205 // Finalize the symbol value when it can refer to the dot symbol.
206 void
207 finalize_with_dot(Symbol_table*, const Layout*, bool dot_has_value,
208 uint64_t dot_value);
209
210 // Set the symbol value, but only if the value is absolute. This is
211 // used while processing a SECTIONS clause.
212 void
213 set_if_absolute(Symbol_table*, const Layout*, bool is_dot_available,
214 bool dot_has_value, uint64_t dot_value);
494e05f4
ILT
215
216 // Print the assignment to the FILE. This is for debugging.
217 void
218 print(FILE*) const;
219
220 private:
a445fddf
ILT
221 // Shared by finalize and finalize_with_dot.
222 void
223 finalize_maybe_dot(Symbol_table*, const Layout*, bool is_dot_available,
224 bool dot_has_value, uint64_t dot_value);
225
494e05f4
ILT
226 // Sized version of finalize.
227 template<int size>
228 void
a445fddf
ILT
229 sized_finalize(Symbol_table*, const Layout*, bool is_dot_available,
230 bool dot_has_value, uint64_t dot_value);
494e05f4
ILT
231
232 // Symbol name.
233 std::string name_;
234 // Expression to assign to symbol.
235 Expression* val_;
236 // Whether the assignment should be provided (only set if there is
237 // an undefined reference to the symbol.
238 bool provide_;
239 // Whether the assignment should be hidden.
240 bool hidden_;
241 // The entry in the symbol table.
242 Symbol* sym_;
243};
244
245// This class manages assertions in linker scripts. These can appear
246// in all the places where a Symbol_assignment can appear.
247
248class Script_assertion
249{
250 public:
251 Script_assertion(Expression* check, const char* message,
252 size_t messagelen)
253 : check_(check), message_(message, messagelen)
254 { }
255
256 // Check the assertion.
257 void
258 check(const Symbol_table*, const Layout*);
259
260 // Print the assertion to the FILE. This is for debugging.
261 void
262 print(FILE*) const;
263
264 private:
265 // The expression to check.
266 Expression* check_;
267 // The message to issue if the expression fails.
268 std::string message_;
269};
270
e5756efb
ILT
271// We can read a linker script in two different contexts: when
272// initially parsing the command line, and when we find an input file
273// which is actually a linker script. Also some of the data which can
274// be set by a linker script can also be set via command line options
275// like -e and --defsym. This means that we have a type of data which
276// can be set both during command line option parsing and while
277// reading input files. We store that data in an instance of this
278// object. We will keep pointers to that instance in both the
279// Command_line and Layout objects.
280
281class Script_options
282{
283 public:
284 Script_options();
285
286 // The entry address.
287 const char*
288 entry() const
289 { return this->entry_.empty() ? NULL : this->entry_.c_str(); }
290
291 // Set the entry address.
292 void
293 set_entry(const char* entry, size_t length)
294 { this->entry_.assign(entry, length); }
295
494e05f4 296 // Add a symbol to be defined.
e5756efb
ILT
297 void
298 add_symbol_assignment(const char* name, size_t length, Expression* value,
494e05f4
ILT
299 bool provide, bool hidden);
300
301 // Add an assertion.
302 void
303 add_assertion(Expression* check, const char* message, size_t messagelen);
e5756efb
ILT
304
305 // Define a symbol from the command line.
306 bool
307 define_symbol(const char* definition);
308
309 // Add all symbol definitions to the symbol table.
310 void
9b07f471 311 add_symbols_to_table(Symbol_table*);
e5756efb 312
a445fddf 313 // Finalize the symbol values. Also check assertions.
e5756efb
ILT
314 void
315 finalize_symbols(Symbol_table*, const Layout*);
316
09124467
ILT
317 // Version information parsed from a version script. Everything
318 // else has a pointer to this object.
319 Version_script_info*
320 version_script_info()
494e05f4 321 { return &this->version_script_info_; }
09124467 322
494e05f4
ILT
323 // A SECTIONS clause parsed from a linker script. Everything else
324 // has a pointer to this object.
325 Script_sections*
326 script_sections()
327 { return &this->script_sections_; }
e5756efb 328
a445fddf
ILT
329 // Whether we saw a SECTIONS clause.
330 bool
331 saw_sections_clause() const
332 { return this->script_sections_.saw_sections_clause(); }
333
334 // Set section addresses using a SECTIONS clause. Return the
335 // segment which should hold the file header and segment headers;
336 // this may return NULL, in which case the headers are not in a
337 // loadable segment.
338 Output_segment*
339 set_section_addresses(Symbol_table*, Layout*);
340
494e05f4 341 // Print the script to the FILE. This is for debugging.
e5756efb 342 void
494e05f4
ILT
343 print(FILE*) const;
344
345 private:
346 // We keep a list of symbol assignments which occur outside of a
347 // SECTIONS clause.
348 typedef std::vector<Symbol_assignment*> Symbol_assignments;
349
350 // We keep a list of all assertions whcih occur outside of a
351 // SECTIONS clause.
352 typedef std::vector<Script_assertion*> Assertions;
e5756efb
ILT
353
354 // The entry address. This will be empty if not set.
355 std::string entry_;
356 // Symbols to set.
357 Symbol_assignments symbol_assignments_;
494e05f4
ILT
358 // Assertions to check.
359 Assertions assertions_;
09124467
ILT
360 // Version information parsed from a version script.
361 Version_script_info version_script_info_;
494e05f4
ILT
362 // Information from any SECTIONS clauses.
363 Script_sections script_sections_;
e5756efb
ILT
364};
365
dbe717ef
ILT
366// FILE was found as an argument on the command line, but was not
367// recognized as an ELF file. Try to read it as a script. We've
368// already read BYTES of data into P. Return true if the file was
369// handled. This has to handle /usr/lib/libc.so on a GNU/Linux
370// system.
371
372bool
373read_input_script(Workqueue*, const General_options&, Symbol_table*, Layout*,
17a1d0a9 374 Dirsearch*, Input_objects*, Input_group*,
dbe717ef
ILT
375 const Input_argument*, Input_file*, const unsigned char* p,
376 off_t bytes, Task_token* this_blocker,
377 Task_token* next_blocker);
378
3c2fafa5
ILT
379// FILE was found as an argument to --script (-T).
380// Read it as a script, and execute its contents immediately.
381
382bool
383read_commandline_script(const char* filename, Command_line*);
384
09124467
ILT
385// FILE was found as an argument to --version-script. Read it as a
386// version script, and store its contents in
387// cmdline->script_options()->version_script_info().
388
389bool
390read_version_script(const char* filename, Command_line* cmdline);
391
392
dbe717ef
ILT
393} // End namespace gold.
394
395#endif // !defined(GOLD_SCRIPT_H)
This page took 0.089524 seconds and 4 git commands to generate.