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