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