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