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