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