Commit | Line | Data |
---|---|---|
dbe717ef ILT |
1 | // script.cc -- handle linker scripts for gold. |
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 | #include "gold.h" |
24 | ||
04bf7072 ILT |
25 | #include <cstdio> |
26 | #include <cstdlib> | |
27 | #include <cstring> | |
09124467 | 28 | #include <fnmatch.h> |
dbe717ef ILT |
29 | #include <string> |
30 | #include <vector> | |
ad2d6943 | 31 | #include "filenames.h" |
dbe717ef | 32 | |
e5756efb | 33 | #include "elfcpp.h" |
09124467 | 34 | #include "demangle.h" |
3c2fafa5 | 35 | #include "dirsearch.h" |
dbe717ef ILT |
36 | #include "options.h" |
37 | #include "fileread.h" | |
38 | #include "workqueue.h" | |
39 | #include "readsyms.h" | |
ad2d6943 | 40 | #include "parameters.h" |
d391083d | 41 | #include "layout.h" |
e5756efb | 42 | #include "symtab.h" |
15f8229b | 43 | #include "target-select.h" |
dbe717ef ILT |
44 | #include "script.h" |
45 | #include "script-c.h" | |
46 | ||
47 | namespace gold | |
48 | { | |
49 | ||
50 | // A token read from a script file. We don't implement keywords here; | |
51 | // all keywords are simply represented as a string. | |
52 | ||
53 | class Token | |
54 | { | |
55 | public: | |
56 | // Token classification. | |
57 | enum Classification | |
58 | { | |
59 | // Token is invalid. | |
60 | TOKEN_INVALID, | |
61 | // Token indicates end of input. | |
62 | TOKEN_EOF, | |
63 | // Token is a string of characters. | |
64 | TOKEN_STRING, | |
e5756efb ILT |
65 | // Token is a quoted string of characters. |
66 | TOKEN_QUOTED_STRING, | |
dbe717ef ILT |
67 | // Token is an operator. |
68 | TOKEN_OPERATOR, | |
69 | // Token is a number (an integer). | |
70 | TOKEN_INTEGER | |
71 | }; | |
72 | ||
73 | // We need an empty constructor so that we can put this STL objects. | |
74 | Token() | |
e5756efb ILT |
75 | : classification_(TOKEN_INVALID), value_(NULL), value_length_(0), |
76 | opcode_(0), lineno_(0), charpos_(0) | |
dbe717ef ILT |
77 | { } |
78 | ||
79 | // A general token with no value. | |
80 | Token(Classification classification, int lineno, int charpos) | |
e5756efb ILT |
81 | : classification_(classification), value_(NULL), value_length_(0), |
82 | opcode_(0), lineno_(lineno), charpos_(charpos) | |
a3ad94ed ILT |
83 | { |
84 | gold_assert(classification == TOKEN_INVALID | |
85 | || classification == TOKEN_EOF); | |
86 | } | |
dbe717ef ILT |
87 | |
88 | // A general token with a value. | |
e5756efb | 89 | Token(Classification classification, const char* value, size_t length, |
dbe717ef | 90 | int lineno, int charpos) |
e5756efb ILT |
91 | : classification_(classification), value_(value), value_length_(length), |
92 | opcode_(0), lineno_(lineno), charpos_(charpos) | |
a3ad94ed ILT |
93 | { |
94 | gold_assert(classification != TOKEN_INVALID | |
95 | && classification != TOKEN_EOF); | |
96 | } | |
dbe717ef | 97 | |
dbe717ef ILT |
98 | // A token representing an operator. |
99 | Token(int opcode, int lineno, int charpos) | |
e5756efb ILT |
100 | : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0), |
101 | opcode_(opcode), lineno_(lineno), charpos_(charpos) | |
dbe717ef ILT |
102 | { } |
103 | ||
104 | // Return whether the token is invalid. | |
105 | bool | |
106 | is_invalid() const | |
107 | { return this->classification_ == TOKEN_INVALID; } | |
108 | ||
109 | // Return whether this is an EOF token. | |
110 | bool | |
111 | is_eof() const | |
112 | { return this->classification_ == TOKEN_EOF; } | |
113 | ||
114 | // Return the token classification. | |
115 | Classification | |
116 | classification() const | |
117 | { return this->classification_; } | |
118 | ||
119 | // Return the line number at which the token starts. | |
120 | int | |
121 | lineno() const | |
122 | { return this->lineno_; } | |
123 | ||
124 | // Return the character position at this the token starts. | |
125 | int | |
126 | charpos() const | |
127 | { return this->charpos_; } | |
128 | ||
129 | // Get the value of a token. | |
130 | ||
e5756efb ILT |
131 | const char* |
132 | string_value(size_t* length) const | |
dbe717ef | 133 | { |
e5756efb ILT |
134 | gold_assert(this->classification_ == TOKEN_STRING |
135 | || this->classification_ == TOKEN_QUOTED_STRING); | |
136 | *length = this->value_length_; | |
dbe717ef ILT |
137 | return this->value_; |
138 | } | |
139 | ||
140 | int | |
141 | operator_value() const | |
142 | { | |
a3ad94ed | 143 | gold_assert(this->classification_ == TOKEN_OPERATOR); |
dbe717ef ILT |
144 | return this->opcode_; |
145 | } | |
146 | ||
e5756efb | 147 | uint64_t |
dbe717ef ILT |
148 | integer_value() const |
149 | { | |
a3ad94ed | 150 | gold_assert(this->classification_ == TOKEN_INTEGER); |
e5756efb ILT |
151 | // Null terminate. |
152 | std::string s(this->value_, this->value_length_); | |
153 | return strtoull(s.c_str(), NULL, 0); | |
dbe717ef ILT |
154 | } |
155 | ||
156 | private: | |
157 | // The token classification. | |
158 | Classification classification_; | |
e5756efb ILT |
159 | // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or |
160 | // TOKEN_INTEGER. | |
161 | const char* value_; | |
162 | // The length of the token value. | |
163 | size_t value_length_; | |
dbe717ef ILT |
164 | // The token value, for TOKEN_OPERATOR. |
165 | int opcode_; | |
166 | // The line number where this token started (one based). | |
167 | int lineno_; | |
168 | // The character position within the line where this token started | |
169 | // (one based). | |
170 | int charpos_; | |
171 | }; | |
172 | ||
e5756efb | 173 | // This class handles lexing a file into a sequence of tokens. |
dbe717ef ILT |
174 | |
175 | class Lex | |
176 | { | |
177 | public: | |
e5756efb ILT |
178 | // We unfortunately have to support different lexing modes, because |
179 | // when reading different parts of a linker script we need to parse | |
180 | // things differently. | |
181 | enum Mode | |
182 | { | |
183 | // Reading an ordinary linker script. | |
184 | LINKER_SCRIPT, | |
185 | // Reading an expression in a linker script. | |
186 | EXPRESSION, | |
187 | // Reading a version script. | |
c82fbeee CS |
188 | VERSION_SCRIPT, |
189 | // Reading a --dynamic-list file. | |
190 | DYNAMIC_LIST | |
e5756efb ILT |
191 | }; |
192 | ||
193 | Lex(const char* input_string, size_t input_length, int parsing_token) | |
194 | : input_string_(input_string), input_length_(input_length), | |
195 | current_(input_string), mode_(LINKER_SCRIPT), | |
196 | first_token_(parsing_token), token_(), | |
197 | lineno_(1), linestart_(input_string) | |
dbe717ef ILT |
198 | { } |
199 | ||
e5756efb ILT |
200 | // Read a file into a string. |
201 | static void | |
202 | read_file(Input_file*, std::string*); | |
203 | ||
204 | // Return the next token. | |
205 | const Token* | |
206 | next_token(); | |
dbe717ef | 207 | |
e5756efb ILT |
208 | // Return the current lexing mode. |
209 | Lex::Mode | |
210 | mode() const | |
211 | { return this->mode_; } | |
dbe717ef | 212 | |
e5756efb ILT |
213 | // Set the lexing mode. |
214 | void | |
215 | set_mode(Mode mode) | |
216 | { this->mode_ = mode; } | |
dbe717ef ILT |
217 | |
218 | private: | |
219 | Lex(const Lex&); | |
220 | Lex& operator=(const Lex&); | |
221 | ||
dbe717ef ILT |
222 | // Make a general token with no value at the current location. |
223 | Token | |
e5756efb ILT |
224 | make_token(Token::Classification c, const char* start) const |
225 | { return Token(c, this->lineno_, start - this->linestart_ + 1); } | |
dbe717ef ILT |
226 | |
227 | // Make a general token with a value at the current location. | |
228 | Token | |
e5756efb ILT |
229 | make_token(Token::Classification c, const char* v, size_t len, |
230 | const char* start) | |
dbe717ef | 231 | const |
e5756efb | 232 | { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); } |
dbe717ef ILT |
233 | |
234 | // Make an operator token at the current location. | |
235 | Token | |
e5756efb ILT |
236 | make_token(int opcode, const char* start) const |
237 | { return Token(opcode, this->lineno_, start - this->linestart_ + 1); } | |
dbe717ef ILT |
238 | |
239 | // Make an invalid token at the current location. | |
240 | Token | |
e5756efb ILT |
241 | make_invalid_token(const char* start) |
242 | { return this->make_token(Token::TOKEN_INVALID, start); } | |
dbe717ef ILT |
243 | |
244 | // Make an EOF token at the current location. | |
245 | Token | |
e5756efb ILT |
246 | make_eof_token(const char* start) |
247 | { return this->make_token(Token::TOKEN_EOF, start); } | |
dbe717ef ILT |
248 | |
249 | // Return whether C can be the first character in a name. C2 is the | |
250 | // next character, since we sometimes need that. | |
e5756efb | 251 | inline bool |
dbe717ef ILT |
252 | can_start_name(char c, char c2); |
253 | ||
09124467 ILT |
254 | // If C can appear in a name which has already started, return a |
255 | // pointer to a character later in the token or just past | |
256 | // it. Otherwise, return NULL. | |
257 | inline const char* | |
258 | can_continue_name(const char* c); | |
dbe717ef ILT |
259 | |
260 | // Return whether C, C2, C3 can start a hex number. | |
e5756efb | 261 | inline bool |
dbe717ef ILT |
262 | can_start_hex(char c, char c2, char c3); |
263 | ||
09124467 ILT |
264 | // If C can appear in a hex number which has already started, return |
265 | // a pointer to a character later in the token or just past | |
266 | // it. Otherwise, return NULL. | |
267 | inline const char* | |
268 | can_continue_hex(const char* c); | |
dbe717ef ILT |
269 | |
270 | // Return whether C can start a non-hex number. | |
271 | static inline bool | |
272 | can_start_number(char c); | |
273 | ||
09124467 ILT |
274 | // If C can appear in a decimal number which has already started, |
275 | // return a pointer to a character later in the token or just past | |
276 | // it. Otherwise, return NULL. | |
277 | inline const char* | |
278 | can_continue_number(const char* c) | |
279 | { return Lex::can_start_number(*c) ? c + 1 : NULL; } | |
dbe717ef ILT |
280 | |
281 | // If C1 C2 C3 form a valid three character operator, return the | |
282 | // opcode. Otherwise return 0. | |
283 | static inline int | |
284 | three_char_operator(char c1, char c2, char c3); | |
285 | ||
286 | // If C1 C2 form a valid two character operator, return the opcode. | |
287 | // Otherwise return 0. | |
288 | static inline int | |
289 | two_char_operator(char c1, char c2); | |
290 | ||
291 | // If C1 is a valid one character operator, return the opcode. | |
292 | // Otherwise return 0. | |
293 | static inline int | |
294 | one_char_operator(char c1); | |
295 | ||
296 | // Read the next token. | |
297 | Token | |
298 | get_token(const char**); | |
299 | ||
300 | // Skip a C style /* */ comment. Return false if the comment did | |
301 | // not end. | |
302 | bool | |
303 | skip_c_comment(const char**); | |
304 | ||
305 | // Skip a line # comment. Return false if there was no newline. | |
306 | bool | |
307 | skip_line_comment(const char**); | |
308 | ||
309 | // Build a token CLASSIFICATION from all characters that match | |
310 | // CAN_CONTINUE_FN. The token starts at START. Start matching from | |
311 | // MATCH. Set *PP to the character following the token. | |
312 | inline Token | |
e5756efb | 313 | gather_token(Token::Classification, |
09124467 | 314 | const char* (Lex::*can_continue_fn)(const char*), |
dbe717ef ILT |
315 | const char* start, const char* match, const char** pp); |
316 | ||
317 | // Build a token from a quoted string. | |
318 | Token | |
319 | gather_quoted_string(const char** pp); | |
320 | ||
e5756efb ILT |
321 | // The string we are tokenizing. |
322 | const char* input_string_; | |
323 | // The length of the string. | |
324 | size_t input_length_; | |
325 | // The current offset into the string. | |
326 | const char* current_; | |
327 | // The current lexing mode. | |
328 | Mode mode_; | |
329 | // The code to use for the first token. This is set to 0 after it | |
330 | // is used. | |
331 | int first_token_; | |
332 | // The current token. | |
333 | Token token_; | |
dbe717ef ILT |
334 | // The current line number. |
335 | int lineno_; | |
e5756efb | 336 | // The start of the current line in the string. |
dbe717ef ILT |
337 | const char* linestart_; |
338 | }; | |
339 | ||
340 | // Read the whole file into memory. We don't expect linker scripts to | |
341 | // be large, so we just use a std::string as a buffer. We ignore the | |
342 | // data we've already read, so that we read aligned buffers. | |
343 | ||
344 | void | |
e5756efb | 345 | Lex::read_file(Input_file* input_file, std::string* contents) |
dbe717ef | 346 | { |
e5756efb | 347 | off_t filesize = input_file->file().filesize(); |
dbe717ef | 348 | contents->clear(); |
82dcae9d ILT |
349 | contents->reserve(filesize); |
350 | ||
dbe717ef | 351 | off_t off = 0; |
dbe717ef | 352 | unsigned char buf[BUFSIZ]; |
82dcae9d | 353 | while (off < filesize) |
dbe717ef | 354 | { |
82dcae9d ILT |
355 | off_t get = BUFSIZ; |
356 | if (get > filesize - off) | |
357 | get = filesize - off; | |
e5756efb | 358 | input_file->file().read(off, get, buf); |
82dcae9d ILT |
359 | contents->append(reinterpret_cast<char*>(&buf[0]), get); |
360 | off += get; | |
dbe717ef | 361 | } |
dbe717ef ILT |
362 | } |
363 | ||
364 | // Return whether C can be the start of a name, if the next character | |
365 | // is C2. A name can being with a letter, underscore, period, or | |
366 | // dollar sign. Because a name can be a file name, we also permit | |
367 | // forward slash, backslash, and tilde. Tilde is the tricky case | |
368 | // here; GNU ld also uses it as a bitwise not operator. It is only | |
369 | // recognized as the operator if it is not immediately followed by | |
e5756efb ILT |
370 | // some character which can appear in a symbol. That is, when we |
371 | // don't know that we are looking at an expression, "~0" is a file | |
372 | // name, and "~ 0" is an expression using bitwise not. We are | |
dbe717ef ILT |
373 | // compatible. |
374 | ||
375 | inline bool | |
376 | Lex::can_start_name(char c, char c2) | |
377 | { | |
378 | switch (c) | |
379 | { | |
380 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
381 | case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': | |
382 | case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R': | |
383 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': | |
384 | case 'Y': case 'Z': | |
385 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
386 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
387 | case 'm': case 'n': case 'o': case 'q': case 'p': case 'r': | |
388 | case 's': case 't': case 'u': case 'v': case 'w': case 'x': | |
389 | case 'y': case 'z': | |
e5756efb | 390 | case '_': case '.': case '$': |
dbe717ef ILT |
391 | return true; |
392 | ||
e5756efb ILT |
393 | case '/': case '\\': |
394 | return this->mode_ == LINKER_SCRIPT; | |
395 | ||
dbe717ef | 396 | case '~': |
09124467 ILT |
397 | return this->mode_ == LINKER_SCRIPT && can_continue_name(&c2); |
398 | ||
c82fbeee | 399 | case '*': case '[': |
3802b2dd | 400 | return (this->mode_ == VERSION_SCRIPT |
c82fbeee | 401 | || this->mode_ == DYNAMIC_LIST |
3802b2dd ILT |
402 | || (this->mode_ == LINKER_SCRIPT |
403 | && can_continue_name(&c2))); | |
dbe717ef ILT |
404 | |
405 | default: | |
406 | return false; | |
407 | } | |
408 | } | |
409 | ||
410 | // Return whether C can continue a name which has already started. | |
411 | // Subsequent characters in a name are the same as the leading | |
412 | // characters, plus digits and "=+-:[],?*". So in general the linker | |
e5756efb ILT |
413 | // script language requires spaces around operators, unless we know |
414 | // that we are parsing an expression. | |
dbe717ef | 415 | |
09124467 ILT |
416 | inline const char* |
417 | Lex::can_continue_name(const char* c) | |
dbe717ef | 418 | { |
09124467 | 419 | switch (*c) |
dbe717ef ILT |
420 | { |
421 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
422 | case 'G': case 'H': case 'I': case 'J': case 'K': case 'L': | |
423 | case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R': | |
424 | case 'S': case 'T': case 'U': case 'V': case 'W': case 'X': | |
425 | case 'Y': case 'Z': | |
426 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
427 | case 'g': case 'h': case 'i': case 'j': case 'k': case 'l': | |
428 | case 'm': case 'n': case 'o': case 'q': case 'p': case 'r': | |
429 | case 's': case 't': case 'u': case 'v': case 'w': case 'x': | |
430 | case 'y': case 'z': | |
e5756efb | 431 | case '_': case '.': case '$': |
dbe717ef ILT |
432 | case '0': case '1': case '2': case '3': case '4': |
433 | case '5': case '6': case '7': case '8': case '9': | |
09124467 | 434 | return c + 1; |
dbe717ef | 435 | |
c82fbeee | 436 | // TODO(csilvers): why not allow ~ in names for version-scripts? |
e5756efb | 437 | case '/': case '\\': case '~': |
09124467 | 438 | case '=': case '+': |
afe47622 | 439 | case ',': |
09124467 ILT |
440 | if (this->mode_ == LINKER_SCRIPT) |
441 | return c + 1; | |
442 | return NULL; | |
443 | ||
afe47622 | 444 | case '[': case ']': case '*': case '?': case '-': |
c82fbeee CS |
445 | if (this->mode_ == LINKER_SCRIPT || this->mode_ == VERSION_SCRIPT |
446 | || this->mode_ == DYNAMIC_LIST) | |
09124467 ILT |
447 | return c + 1; |
448 | return NULL; | |
449 | ||
c82fbeee | 450 | // TODO(csilvers): why allow this? ^ is meaningless in version scripts. |
09124467 | 451 | case '^': |
c82fbeee | 452 | if (this->mode_ == VERSION_SCRIPT || this->mode_ == DYNAMIC_LIST) |
09124467 ILT |
453 | return c + 1; |
454 | return NULL; | |
455 | ||
456 | case ':': | |
457 | if (this->mode_ == LINKER_SCRIPT) | |
458 | return c + 1; | |
c82fbeee CS |
459 | else if ((this->mode_ == VERSION_SCRIPT || this->mode_ == DYNAMIC_LIST) |
460 | && (c[1] == ':')) | |
09124467 ILT |
461 | { |
462 | // A name can have '::' in it, as that's a c++ namespace | |
463 | // separator. But a single colon is not part of a name. | |
464 | return c + 2; | |
465 | } | |
466 | return NULL; | |
e5756efb | 467 | |
dbe717ef | 468 | default: |
09124467 | 469 | return NULL; |
dbe717ef ILT |
470 | } |
471 | } | |
472 | ||
473 | // For a number we accept 0x followed by hex digits, or any sequence | |
474 | // of digits. The old linker accepts leading '$' for hex, and | |
475 | // trailing HXBOD. Those are for MRI compatibility and we don't | |
476 | // accept them. The old linker also accepts trailing MK for mega or | |
e5756efb ILT |
477 | // kilo. FIXME: Those are mentioned in the documentation, and we |
478 | // should accept them. | |
dbe717ef ILT |
479 | |
480 | // Return whether C1 C2 C3 can start a hex number. | |
481 | ||
482 | inline bool | |
483 | Lex::can_start_hex(char c1, char c2, char c3) | |
484 | { | |
485 | if (c1 == '0' && (c2 == 'x' || c2 == 'X')) | |
09124467 | 486 | return this->can_continue_hex(&c3); |
dbe717ef ILT |
487 | return false; |
488 | } | |
489 | ||
490 | // Return whether C can appear in a hex number. | |
491 | ||
09124467 ILT |
492 | inline const char* |
493 | Lex::can_continue_hex(const char* c) | |
dbe717ef | 494 | { |
09124467 | 495 | switch (*c) |
dbe717ef ILT |
496 | { |
497 | case '0': case '1': case '2': case '3': case '4': | |
498 | case '5': case '6': case '7': case '8': case '9': | |
499 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': | |
500 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': | |
09124467 | 501 | return c + 1; |
dbe717ef ILT |
502 | |
503 | default: | |
09124467 | 504 | return NULL; |
dbe717ef ILT |
505 | } |
506 | } | |
507 | ||
508 | // Return whether C can start a non-hex number. | |
509 | ||
510 | inline bool | |
511 | Lex::can_start_number(char c) | |
512 | { | |
513 | switch (c) | |
514 | { | |
515 | case '0': case '1': case '2': case '3': case '4': | |
516 | case '5': case '6': case '7': case '8': case '9': | |
517 | return true; | |
518 | ||
519 | default: | |
520 | return false; | |
521 | } | |
522 | } | |
523 | ||
524 | // If C1 C2 C3 form a valid three character operator, return the | |
525 | // opcode (defined in the yyscript.h file generated from yyscript.y). | |
526 | // Otherwise return 0. | |
527 | ||
528 | inline int | |
529 | Lex::three_char_operator(char c1, char c2, char c3) | |
530 | { | |
531 | switch (c1) | |
532 | { | |
533 | case '<': | |
534 | if (c2 == '<' && c3 == '=') | |
535 | return LSHIFTEQ; | |
536 | break; | |
537 | case '>': | |
538 | if (c2 == '>' && c3 == '=') | |
539 | return RSHIFTEQ; | |
540 | break; | |
541 | default: | |
542 | break; | |
543 | } | |
544 | return 0; | |
545 | } | |
546 | ||
547 | // If C1 C2 form a valid two character operator, return the opcode | |
548 | // (defined in the yyscript.h file generated from yyscript.y). | |
549 | // Otherwise return 0. | |
550 | ||
551 | inline int | |
552 | Lex::two_char_operator(char c1, char c2) | |
553 | { | |
554 | switch (c1) | |
555 | { | |
556 | case '=': | |
557 | if (c2 == '=') | |
558 | return EQ; | |
559 | break; | |
560 | case '!': | |
561 | if (c2 == '=') | |
562 | return NE; | |
563 | break; | |
564 | case '+': | |
565 | if (c2 == '=') | |
566 | return PLUSEQ; | |
567 | break; | |
568 | case '-': | |
569 | if (c2 == '=') | |
570 | return MINUSEQ; | |
571 | break; | |
572 | case '*': | |
573 | if (c2 == '=') | |
574 | return MULTEQ; | |
575 | break; | |
576 | case '/': | |
577 | if (c2 == '=') | |
578 | return DIVEQ; | |
579 | break; | |
580 | case '|': | |
581 | if (c2 == '=') | |
582 | return OREQ; | |
583 | if (c2 == '|') | |
584 | return OROR; | |
585 | break; | |
586 | case '&': | |
587 | if (c2 == '=') | |
588 | return ANDEQ; | |
589 | if (c2 == '&') | |
590 | return ANDAND; | |
591 | break; | |
592 | case '>': | |
593 | if (c2 == '=') | |
594 | return GE; | |
595 | if (c2 == '>') | |
596 | return RSHIFT; | |
597 | break; | |
598 | case '<': | |
599 | if (c2 == '=') | |
600 | return LE; | |
601 | if (c2 == '<') | |
602 | return LSHIFT; | |
603 | break; | |
604 | default: | |
605 | break; | |
606 | } | |
607 | return 0; | |
608 | } | |
609 | ||
610 | // If C1 is a valid operator, return the opcode. Otherwise return 0. | |
611 | ||
612 | inline int | |
613 | Lex::one_char_operator(char c1) | |
614 | { | |
615 | switch (c1) | |
616 | { | |
617 | case '+': | |
618 | case '-': | |
619 | case '*': | |
620 | case '/': | |
621 | case '%': | |
622 | case '!': | |
623 | case '&': | |
624 | case '|': | |
625 | case '^': | |
626 | case '~': | |
627 | case '<': | |
628 | case '>': | |
629 | case '=': | |
630 | case '?': | |
631 | case ',': | |
632 | case '(': | |
633 | case ')': | |
634 | case '{': | |
635 | case '}': | |
636 | case '[': | |
637 | case ']': | |
638 | case ':': | |
639 | case ';': | |
640 | return c1; | |
641 | default: | |
642 | return 0; | |
643 | } | |
644 | } | |
645 | ||
646 | // Skip a C style comment. *PP points to just after the "/*". Return | |
647 | // false if the comment did not end. | |
648 | ||
649 | bool | |
650 | Lex::skip_c_comment(const char** pp) | |
651 | { | |
652 | const char* p = *pp; | |
653 | while (p[0] != '*' || p[1] != '/') | |
654 | { | |
655 | if (*p == '\0') | |
656 | { | |
657 | *pp = p; | |
658 | return false; | |
659 | } | |
660 | ||
661 | if (*p == '\n') | |
662 | { | |
663 | ++this->lineno_; | |
664 | this->linestart_ = p + 1; | |
665 | } | |
666 | ++p; | |
667 | } | |
668 | ||
669 | *pp = p + 2; | |
670 | return true; | |
671 | } | |
672 | ||
673 | // Skip a line # comment. Return false if there was no newline. | |
674 | ||
675 | bool | |
676 | Lex::skip_line_comment(const char** pp) | |
677 | { | |
678 | const char* p = *pp; | |
679 | size_t skip = strcspn(p, "\n"); | |
680 | if (p[skip] == '\0') | |
681 | { | |
682 | *pp = p + skip; | |
683 | return false; | |
684 | } | |
685 | ||
686 | p += skip + 1; | |
687 | ++this->lineno_; | |
688 | this->linestart_ = p; | |
689 | *pp = p; | |
690 | ||
691 | return true; | |
692 | } | |
693 | ||
694 | // Build a token CLASSIFICATION from all characters that match | |
695 | // CAN_CONTINUE_FN. Update *PP. | |
696 | ||
697 | inline Token | |
698 | Lex::gather_token(Token::Classification classification, | |
09124467 | 699 | const char* (Lex::*can_continue_fn)(const char*), |
dbe717ef ILT |
700 | const char* start, |
701 | const char* match, | |
702 | const char **pp) | |
703 | { | |
09124467 ILT |
704 | const char* new_match = NULL; |
705 | while ((new_match = (this->*can_continue_fn)(match))) | |
706 | match = new_match; | |
dbe717ef | 707 | *pp = match; |
e5756efb | 708 | return this->make_token(classification, start, match - start, start); |
dbe717ef ILT |
709 | } |
710 | ||
711 | // Build a token from a quoted string. | |
712 | ||
713 | Token | |
714 | Lex::gather_quoted_string(const char** pp) | |
715 | { | |
716 | const char* start = *pp; | |
717 | const char* p = start; | |
718 | ++p; | |
719 | size_t skip = strcspn(p, "\"\n"); | |
720 | if (p[skip] != '"') | |
721 | return this->make_invalid_token(start); | |
722 | *pp = p + skip + 1; | |
e5756efb | 723 | return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start); |
dbe717ef ILT |
724 | } |
725 | ||
726 | // Return the next token at *PP. Update *PP. General guideline: we | |
727 | // require linker scripts to be simple ASCII. No unicode linker | |
728 | // scripts. In particular we can assume that any '\0' is the end of | |
729 | // the input. | |
730 | ||
731 | Token | |
732 | Lex::get_token(const char** pp) | |
733 | { | |
734 | const char* p = *pp; | |
735 | ||
736 | while (true) | |
737 | { | |
738 | if (*p == '\0') | |
739 | { | |
740 | *pp = p; | |
741 | return this->make_eof_token(p); | |
742 | } | |
743 | ||
744 | // Skip whitespace quickly. | |
745 | while (*p == ' ' || *p == '\t') | |
746 | ++p; | |
747 | ||
748 | if (*p == '\n') | |
749 | { | |
750 | ++p; | |
751 | ++this->lineno_; | |
752 | this->linestart_ = p; | |
753 | continue; | |
754 | } | |
755 | ||
756 | // Skip C style comments. | |
757 | if (p[0] == '/' && p[1] == '*') | |
758 | { | |
759 | int lineno = this->lineno_; | |
760 | int charpos = p - this->linestart_ + 1; | |
761 | ||
762 | *pp = p + 2; | |
763 | if (!this->skip_c_comment(pp)) | |
764 | return Token(Token::TOKEN_INVALID, lineno, charpos); | |
765 | p = *pp; | |
766 | ||
767 | continue; | |
768 | } | |
769 | ||
770 | // Skip line comments. | |
771 | if (*p == '#') | |
772 | { | |
773 | *pp = p + 1; | |
774 | if (!this->skip_line_comment(pp)) | |
775 | return this->make_eof_token(p); | |
776 | p = *pp; | |
777 | continue; | |
778 | } | |
779 | ||
780 | // Check for a name. | |
e5756efb | 781 | if (this->can_start_name(p[0], p[1])) |
dbe717ef | 782 | return this->gather_token(Token::TOKEN_STRING, |
e5756efb ILT |
783 | &Lex::can_continue_name, |
784 | p, p + 1, pp); | |
dbe717ef ILT |
785 | |
786 | // We accept any arbitrary name in double quotes, as long as it | |
787 | // does not cross a line boundary. | |
788 | if (*p == '"') | |
789 | { | |
790 | *pp = p; | |
791 | return this->gather_quoted_string(pp); | |
792 | } | |
793 | ||
794 | // Check for a number. | |
795 | ||
e5756efb | 796 | if (this->can_start_hex(p[0], p[1], p[2])) |
dbe717ef | 797 | return this->gather_token(Token::TOKEN_INTEGER, |
e5756efb | 798 | &Lex::can_continue_hex, |
dbe717ef ILT |
799 | p, p + 3, pp); |
800 | ||
801 | if (Lex::can_start_number(p[0])) | |
802 | return this->gather_token(Token::TOKEN_INTEGER, | |
e5756efb | 803 | &Lex::can_continue_number, |
dbe717ef ILT |
804 | p, p + 1, pp); |
805 | ||
806 | // Check for operators. | |
807 | ||
808 | int opcode = Lex::three_char_operator(p[0], p[1], p[2]); | |
809 | if (opcode != 0) | |
810 | { | |
811 | *pp = p + 3; | |
812 | return this->make_token(opcode, p); | |
813 | } | |
814 | ||
815 | opcode = Lex::two_char_operator(p[0], p[1]); | |
816 | if (opcode != 0) | |
817 | { | |
818 | *pp = p + 2; | |
819 | return this->make_token(opcode, p); | |
820 | } | |
821 | ||
822 | opcode = Lex::one_char_operator(p[0]); | |
823 | if (opcode != 0) | |
824 | { | |
825 | *pp = p + 1; | |
826 | return this->make_token(opcode, p); | |
827 | } | |
828 | ||
829 | return this->make_token(Token::TOKEN_INVALID, p); | |
830 | } | |
831 | } | |
832 | ||
e5756efb | 833 | // Return the next token. |
dbe717ef | 834 | |
e5756efb ILT |
835 | const Token* |
836 | Lex::next_token() | |
dbe717ef | 837 | { |
e5756efb ILT |
838 | // The first token is special. |
839 | if (this->first_token_ != 0) | |
dbe717ef | 840 | { |
e5756efb ILT |
841 | this->token_ = Token(this->first_token_, 0, 0); |
842 | this->first_token_ = 0; | |
843 | return &this->token_; | |
844 | } | |
dbe717ef | 845 | |
e5756efb | 846 | this->token_ = this->get_token(&this->current_); |
dbe717ef | 847 | |
e5756efb ILT |
848 | // Don't let an early null byte fool us into thinking that we've |
849 | // reached the end of the file. | |
850 | if (this->token_.is_eof() | |
851 | && (static_cast<size_t>(this->current_ - this->input_string_) | |
852 | < this->input_length_)) | |
853 | this->token_ = this->make_invalid_token(this->current_); | |
dbe717ef | 854 | |
e5756efb | 855 | return &this->token_; |
dbe717ef ILT |
856 | } |
857 | ||
494e05f4 | 858 | // class Symbol_assignment. |
e5756efb | 859 | |
494e05f4 ILT |
860 | // Add the symbol to the symbol table. This makes sure the symbol is |
861 | // there and defined. The actual value is stored later. We can't | |
862 | // determine the actual value at this point, because we can't | |
863 | // necessarily evaluate the expression until all ordinary symbols have | |
864 | // been finalized. | |
e5756efb | 865 | |
caa9d5d9 ILT |
866 | // The GNU linker lets symbol assignments in the linker script |
867 | // silently override defined symbols in object files. We are | |
868 | // compatible. FIXME: Should we issue a warning? | |
869 | ||
e5756efb | 870 | void |
9b07f471 | 871 | Symbol_assignment::add_to_table(Symbol_table* symtab) |
e5756efb | 872 | { |
494e05f4 | 873 | elfcpp::STV vis = this->hidden_ ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT; |
9b07f471 | 874 | this->sym_ = symtab->define_as_constant(this->name_.c_str(), |
e5756efb ILT |
875 | NULL, // version |
876 | 0, // value | |
877 | 0, // size | |
878 | elfcpp::STT_NOTYPE, | |
879 | elfcpp::STB_GLOBAL, | |
880 | vis, | |
881 | 0, // nonvis | |
caa9d5d9 ILT |
882 | this->provide_, |
883 | true); // force_override | |
e5756efb ILT |
884 | } |
885 | ||
494e05f4 | 886 | // Finalize a symbol value. |
e5756efb ILT |
887 | |
888 | void | |
494e05f4 | 889 | Symbol_assignment::finalize(Symbol_table* symtab, const Layout* layout) |
a445fddf | 890 | { |
77e65537 | 891 | this->finalize_maybe_dot(symtab, layout, false, 0, NULL); |
a445fddf ILT |
892 | } |
893 | ||
894 | // Finalize a symbol value which can refer to the dot symbol. | |
895 | ||
896 | void | |
897 | Symbol_assignment::finalize_with_dot(Symbol_table* symtab, | |
898 | const Layout* layout, | |
77e65537 ILT |
899 | uint64_t dot_value, |
900 | Output_section* dot_section) | |
a445fddf | 901 | { |
77e65537 | 902 | this->finalize_maybe_dot(symtab, layout, true, dot_value, dot_section); |
a445fddf ILT |
903 | } |
904 | ||
905 | // Finalize a symbol value, internal version. | |
906 | ||
907 | void | |
908 | Symbol_assignment::finalize_maybe_dot(Symbol_table* symtab, | |
909 | const Layout* layout, | |
910 | bool is_dot_available, | |
77e65537 ILT |
911 | uint64_t dot_value, |
912 | Output_section* dot_section) | |
e5756efb | 913 | { |
494e05f4 ILT |
914 | // If we were only supposed to provide this symbol, the sym_ field |
915 | // will be NULL if the symbol was not referenced. | |
916 | if (this->sym_ == NULL) | |
917 | { | |
918 | gold_assert(this->provide_); | |
919 | return; | |
920 | } | |
921 | ||
8851ecca | 922 | if (parameters->target().get_size() == 32) |
e5756efb ILT |
923 | { |
924 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
77e65537 ILT |
925 | this->sized_finalize<32>(symtab, layout, is_dot_available, dot_value, |
926 | dot_section); | |
e5756efb ILT |
927 | #else |
928 | gold_unreachable(); | |
929 | #endif | |
930 | } | |
8851ecca | 931 | else if (parameters->target().get_size() == 64) |
e5756efb ILT |
932 | { |
933 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
77e65537 ILT |
934 | this->sized_finalize<64>(symtab, layout, is_dot_available, dot_value, |
935 | dot_section); | |
e5756efb ILT |
936 | #else |
937 | gold_unreachable(); | |
938 | #endif | |
939 | } | |
940 | else | |
941 | gold_unreachable(); | |
942 | } | |
943 | ||
944 | template<int size> | |
945 | void | |
a445fddf | 946 | Symbol_assignment::sized_finalize(Symbol_table* symtab, const Layout* layout, |
77e65537 ILT |
947 | bool is_dot_available, uint64_t dot_value, |
948 | Output_section* dot_section) | |
a445fddf | 949 | { |
77e65537 | 950 | Output_section* section; |
919ed24c | 951 | uint64_t final_val = this->val_->eval_maybe_dot(symtab, layout, true, |
a445fddf | 952 | is_dot_available, |
77e65537 ILT |
953 | dot_value, dot_section, |
954 | §ion); | |
494e05f4 | 955 | Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(this->sym_); |
a445fddf | 956 | ssym->set_value(final_val); |
77e65537 ILT |
957 | if (section != NULL) |
958 | ssym->set_output_section(section); | |
a445fddf ILT |
959 | } |
960 | ||
961 | // Set the symbol value if the expression yields an absolute value. | |
962 | ||
963 | void | |
964 | Symbol_assignment::set_if_absolute(Symbol_table* symtab, const Layout* layout, | |
77e65537 | 965 | bool is_dot_available, uint64_t dot_value) |
a445fddf ILT |
966 | { |
967 | if (this->sym_ == NULL) | |
968 | return; | |
969 | ||
77e65537 | 970 | Output_section* val_section; |
919ed24c ILT |
971 | uint64_t val = this->val_->eval_maybe_dot(symtab, layout, false, |
972 | is_dot_available, dot_value, | |
973 | NULL, &val_section); | |
77e65537 | 974 | if (val_section != NULL) |
a445fddf ILT |
975 | return; |
976 | ||
8851ecca | 977 | if (parameters->target().get_size() == 32) |
a445fddf ILT |
978 | { |
979 | #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG) | |
980 | Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(this->sym_); | |
981 | ssym->set_value(val); | |
982 | #else | |
983 | gold_unreachable(); | |
984 | #endif | |
985 | } | |
8851ecca | 986 | else if (parameters->target().get_size() == 64) |
a445fddf ILT |
987 | { |
988 | #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG) | |
989 | Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(this->sym_); | |
990 | ssym->set_value(val); | |
991 | #else | |
992 | gold_unreachable(); | |
993 | #endif | |
994 | } | |
995 | else | |
996 | gold_unreachable(); | |
494e05f4 ILT |
997 | } |
998 | ||
999 | // Print for debugging. | |
1000 | ||
1001 | void | |
1002 | Symbol_assignment::print(FILE* f) const | |
1003 | { | |
1004 | if (this->provide_ && this->hidden_) | |
1005 | fprintf(f, "PROVIDE_HIDDEN("); | |
1006 | else if (this->provide_) | |
1007 | fprintf(f, "PROVIDE("); | |
1008 | else if (this->hidden_) | |
1009 | gold_unreachable(); | |
1010 | ||
1011 | fprintf(f, "%s = ", this->name_.c_str()); | |
1012 | this->val_->print(f); | |
1013 | ||
1014 | if (this->provide_ || this->hidden_) | |
1015 | fprintf(f, ")"); | |
1016 | ||
1017 | fprintf(f, "\n"); | |
1018 | } | |
1019 | ||
1020 | // Class Script_assertion. | |
1021 | ||
1022 | // Check the assertion. | |
1023 | ||
1024 | void | |
1025 | Script_assertion::check(const Symbol_table* symtab, const Layout* layout) | |
1026 | { | |
919ed24c | 1027 | if (!this->check_->eval(symtab, layout, true)) |
494e05f4 ILT |
1028 | gold_error("%s", this->message_.c_str()); |
1029 | } | |
1030 | ||
1031 | // Print for debugging. | |
1032 | ||
1033 | void | |
1034 | Script_assertion::print(FILE* f) const | |
1035 | { | |
1036 | fprintf(f, "ASSERT("); | |
1037 | this->check_->print(f); | |
1038 | fprintf(f, ", \"%s\")\n", this->message_.c_str()); | |
1039 | } | |
1040 | ||
1041 | // Class Script_options. | |
1042 | ||
1043 | Script_options::Script_options() | |
1044 | : entry_(), symbol_assignments_(), version_script_info_(), | |
1045 | script_sections_() | |
1046 | { | |
1047 | } | |
1048 | ||
1049 | // Add a symbol to be defined. | |
1050 | ||
1051 | void | |
1052 | Script_options::add_symbol_assignment(const char* name, size_t length, | |
1053 | Expression* value, bool provide, | |
1054 | bool hidden) | |
1055 | { | |
a445fddf ILT |
1056 | if (length != 1 || name[0] != '.') |
1057 | { | |
1058 | if (this->script_sections_.in_sections_clause()) | |
1059 | this->script_sections_.add_symbol_assignment(name, length, value, | |
1060 | provide, hidden); | |
1061 | else | |
1062 | { | |
1063 | Symbol_assignment* p = new Symbol_assignment(name, length, value, | |
1064 | provide, hidden); | |
1065 | this->symbol_assignments_.push_back(p); | |
1066 | } | |
1067 | } | |
494e05f4 ILT |
1068 | else |
1069 | { | |
a445fddf ILT |
1070 | if (provide || hidden) |
1071 | gold_error(_("invalid use of PROVIDE for dot symbol")); | |
1072 | if (!this->script_sections_.in_sections_clause()) | |
1073 | gold_error(_("invalid assignment to dot outside of SECTIONS")); | |
1074 | else | |
1075 | this->script_sections_.add_dot_assignment(value); | |
494e05f4 ILT |
1076 | } |
1077 | } | |
1078 | ||
1079 | // Add an assertion. | |
1080 | ||
1081 | void | |
1082 | Script_options::add_assertion(Expression* check, const char* message, | |
1083 | size_t messagelen) | |
1084 | { | |
1085 | if (this->script_sections_.in_sections_clause()) | |
1086 | this->script_sections_.add_assertion(check, message, messagelen); | |
1087 | else | |
1088 | { | |
1089 | Script_assertion* p = new Script_assertion(check, message, messagelen); | |
1090 | this->assertions_.push_back(p); | |
1091 | } | |
1092 | } | |
1093 | ||
919ed24c ILT |
1094 | // Create sections required by any linker scripts. |
1095 | ||
1096 | void | |
1097 | Script_options::create_script_sections(Layout* layout) | |
1098 | { | |
1099 | if (this->saw_sections_clause()) | |
1100 | this->script_sections_.create_sections(layout); | |
1101 | } | |
1102 | ||
494e05f4 ILT |
1103 | // Add any symbols we are defining to the symbol table. |
1104 | ||
1105 | void | |
9b07f471 | 1106 | Script_options::add_symbols_to_table(Symbol_table* symtab) |
e5756efb ILT |
1107 | { |
1108 | for (Symbol_assignments::iterator p = this->symbol_assignments_.begin(); | |
1109 | p != this->symbol_assignments_.end(); | |
1110 | ++p) | |
9b07f471 | 1111 | (*p)->add_to_table(symtab); |
a445fddf | 1112 | this->script_sections_.add_symbols_to_table(symtab); |
494e05f4 ILT |
1113 | } |
1114 | ||
a445fddf | 1115 | // Finalize symbol values. Also check assertions. |
494e05f4 ILT |
1116 | |
1117 | void | |
1118 | Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout) | |
1119 | { | |
7c07ecec ILT |
1120 | // We finalize the symbols defined in SECTIONS first, because they |
1121 | // are the ones which may have changed. This way if symbol outside | |
1122 | // SECTIONS are defined in terms of symbols inside SECTIONS, they | |
1123 | // will get the right value. | |
1124 | this->script_sections_.finalize_symbols(symtab, layout); | |
1125 | ||
494e05f4 ILT |
1126 | for (Symbol_assignments::iterator p = this->symbol_assignments_.begin(); |
1127 | p != this->symbol_assignments_.end(); | |
1128 | ++p) | |
1129 | (*p)->finalize(symtab, layout); | |
a445fddf ILT |
1130 | |
1131 | for (Assertions::iterator p = this->assertions_.begin(); | |
1132 | p != this->assertions_.end(); | |
1133 | ++p) | |
1134 | (*p)->check(symtab, layout); | |
a445fddf ILT |
1135 | } |
1136 | ||
1137 | // Set section addresses. We set all the symbols which have absolute | |
1138 | // values. Then we let the SECTIONS clause do its thing. This | |
1139 | // returns the segment which holds the file header and segment | |
1140 | // headers, if any. | |
1141 | ||
1142 | Output_segment* | |
1143 | Script_options::set_section_addresses(Symbol_table* symtab, Layout* layout) | |
1144 | { | |
1145 | for (Symbol_assignments::iterator p = this->symbol_assignments_.begin(); | |
1146 | p != this->symbol_assignments_.end(); | |
1147 | ++p) | |
77e65537 | 1148 | (*p)->set_if_absolute(symtab, layout, false, 0); |
a445fddf ILT |
1149 | |
1150 | return this->script_sections_.set_section_addresses(symtab, layout); | |
e5756efb ILT |
1151 | } |
1152 | ||
dbe717ef ILT |
1153 | // This class holds data passed through the parser to the lexer and to |
1154 | // the parser support functions. This avoids global variables. We | |
17a1d0a9 ILT |
1155 | // can't use global variables because we need not be called by a |
1156 | // singleton thread. | |
dbe717ef ILT |
1157 | |
1158 | class Parser_closure | |
1159 | { | |
1160 | public: | |
1161 | Parser_closure(const char* filename, | |
1162 | const Position_dependent_options& posdep_options, | |
ad2d6943 | 1163 | bool in_group, bool is_in_sysroot, |
a0451b38 | 1164 | Command_line* command_line, |
e5756efb | 1165 | Script_options* script_options, |
15f8229b ILT |
1166 | Lex* lex, |
1167 | bool skip_on_incompatible_target) | |
dbe717ef | 1168 | : filename_(filename), posdep_options_(posdep_options), |
a0451b38 | 1169 | in_group_(in_group), is_in_sysroot_(is_in_sysroot), |
15f8229b ILT |
1170 | skip_on_incompatible_target_(skip_on_incompatible_target), |
1171 | found_incompatible_target_(false), | |
e5756efb | 1172 | command_line_(command_line), script_options_(script_options), |
09124467 | 1173 | version_script_info_(script_options->version_script_info()), |
e5756efb | 1174 | lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL) |
c82fbeee | 1175 | { |
09124467 ILT |
1176 | // We start out processing C symbols in the default lex mode. |
1177 | language_stack_.push_back(""); | |
1178 | lex_mode_stack_.push_back(lex->mode()); | |
1179 | } | |
dbe717ef ILT |
1180 | |
1181 | // Return the file name. | |
1182 | const char* | |
1183 | filename() const | |
1184 | { return this->filename_; } | |
1185 | ||
1186 | // Return the position dependent options. The caller may modify | |
1187 | // this. | |
1188 | Position_dependent_options& | |
1189 | position_dependent_options() | |
1190 | { return this->posdep_options_; } | |
1191 | ||
1192 | // Return whether this script is being run in a group. | |
1193 | bool | |
1194 | in_group() const | |
1195 | { return this->in_group_; } | |
1196 | ||
ad2d6943 ILT |
1197 | // Return whether this script was found using a directory in the |
1198 | // sysroot. | |
1199 | bool | |
1200 | is_in_sysroot() const | |
1201 | { return this->is_in_sysroot_; } | |
1202 | ||
15f8229b ILT |
1203 | // Whether to skip to the next file with the same name if we find an |
1204 | // incompatible target in an OUTPUT_FORMAT statement. | |
1205 | bool | |
1206 | skip_on_incompatible_target() const | |
1207 | { return this->skip_on_incompatible_target_; } | |
1208 | ||
1209 | // Stop skipping to the next flie on an incompatible target. This | |
1210 | // is called when we make some unrevocable change to the data | |
1211 | // structures. | |
1212 | void | |
1213 | clear_skip_on_incompatible_target() | |
1214 | { this->skip_on_incompatible_target_ = false; } | |
1215 | ||
1216 | // Whether we found an incompatible target in an OUTPUT_FORMAT | |
1217 | // statement. | |
1218 | bool | |
1219 | found_incompatible_target() const | |
1220 | { return this->found_incompatible_target_; } | |
1221 | ||
1222 | // Note that we found an incompatible target. | |
1223 | void | |
1224 | set_found_incompatible_target() | |
1225 | { this->found_incompatible_target_ = true; } | |
1226 | ||
a0451b38 ILT |
1227 | // Returns the Command_line structure passed in at constructor time. |
1228 | // This value may be NULL. The caller may modify this, which modifies | |
1229 | // the passed-in Command_line object (not a copy). | |
e5756efb ILT |
1230 | Command_line* |
1231 | command_line() | |
a0451b38 ILT |
1232 | { return this->command_line_; } |
1233 | ||
e5756efb ILT |
1234 | // Return the options which may be set by a script. |
1235 | Script_options* | |
1236 | script_options() | |
1237 | { return this->script_options_; } | |
dbe717ef | 1238 | |
09124467 ILT |
1239 | // Return the object in which version script information should be stored. |
1240 | Version_script_info* | |
1241 | version_script() | |
1242 | { return this->version_script_info_; } | |
1243 | ||
2dd3e587 | 1244 | // Return the next token, and advance. |
dbe717ef ILT |
1245 | const Token* |
1246 | next_token() | |
1247 | { | |
e5756efb ILT |
1248 | const Token* token = this->lex_->next_token(); |
1249 | this->lineno_ = token->lineno(); | |
1250 | this->charpos_ = token->charpos(); | |
1251 | return token; | |
dbe717ef ILT |
1252 | } |
1253 | ||
e5756efb ILT |
1254 | // Set a new lexer mode, pushing the current one. |
1255 | void | |
1256 | push_lex_mode(Lex::Mode mode) | |
1257 | { | |
1258 | this->lex_mode_stack_.push_back(this->lex_->mode()); | |
1259 | this->lex_->set_mode(mode); | |
1260 | } | |
1261 | ||
1262 | // Pop the lexer mode. | |
1263 | void | |
1264 | pop_lex_mode() | |
2dd3e587 | 1265 | { |
e5756efb ILT |
1266 | gold_assert(!this->lex_mode_stack_.empty()); |
1267 | this->lex_->set_mode(this->lex_mode_stack_.back()); | |
1268 | this->lex_mode_stack_.pop_back(); | |
2dd3e587 ILT |
1269 | } |
1270 | ||
09124467 ILT |
1271 | // Return the current lexer mode. |
1272 | Lex::Mode | |
1273 | lex_mode() const | |
1274 | { return this->lex_mode_stack_.back(); } | |
1275 | ||
e5756efb ILT |
1276 | // Return the line number of the last token. |
1277 | int | |
1278 | lineno() const | |
1279 | { return this->lineno_; } | |
1280 | ||
1281 | // Return the character position in the line of the last token. | |
1282 | int | |
1283 | charpos() const | |
1284 | { return this->charpos_; } | |
1285 | ||
dbe717ef ILT |
1286 | // Return the list of input files, creating it if necessary. This |
1287 | // is a space leak--we never free the INPUTS_ pointer. | |
1288 | Input_arguments* | |
1289 | inputs() | |
1290 | { | |
1291 | if (this->inputs_ == NULL) | |
1292 | this->inputs_ = new Input_arguments(); | |
1293 | return this->inputs_; | |
1294 | } | |
1295 | ||
1296 | // Return whether we saw any input files. | |
1297 | bool | |
1298 | saw_inputs() const | |
1299 | { return this->inputs_ != NULL && !this->inputs_->empty(); } | |
1300 | ||
09124467 ILT |
1301 | // Return the current language being processed in a version script |
1302 | // (eg, "C++"). The empty string represents unmangled C names. | |
1303 | const std::string& | |
1304 | get_current_language() const | |
1305 | { return this->language_stack_.back(); } | |
1306 | ||
1307 | // Push a language onto the stack when entering an extern block. | |
1308 | void push_language(const std::string& lang) | |
1309 | { this->language_stack_.push_back(lang); } | |
1310 | ||
1311 | // Pop a language off of the stack when exiting an extern block. | |
1312 | void pop_language() | |
1313 | { | |
1314 | gold_assert(!this->language_stack_.empty()); | |
1315 | this->language_stack_.pop_back(); | |
1316 | } | |
1317 | ||
dbe717ef ILT |
1318 | private: |
1319 | // The name of the file we are reading. | |
1320 | const char* filename_; | |
1321 | // The position dependent options. | |
1322 | Position_dependent_options posdep_options_; | |
1323 | // Whether we are currently in a --start-group/--end-group. | |
1324 | bool in_group_; | |
ad2d6943 ILT |
1325 | // Whether the script was found in a sysrooted directory. |
1326 | bool is_in_sysroot_; | |
15f8229b ILT |
1327 | // If this is true, then if we find an OUTPUT_FORMAT with an |
1328 | // incompatible target, then we tell the parser to abort so that we | |
1329 | // can search for the next file with the same name. | |
1330 | bool skip_on_incompatible_target_; | |
1331 | // True if we found an OUTPUT_FORMAT with an incompatible target. | |
1332 | bool found_incompatible_target_; | |
a0451b38 ILT |
1333 | // May be NULL if the user chooses not to pass one in. |
1334 | Command_line* command_line_; | |
e5756efb ILT |
1335 | // Options which may be set from any linker script. |
1336 | Script_options* script_options_; | |
09124467 ILT |
1337 | // Information parsed from a version script. |
1338 | Version_script_info* version_script_info_; | |
e5756efb ILT |
1339 | // The lexer. |
1340 | Lex* lex_; | |
1341 | // The line number of the last token returned by next_token. | |
1342 | int lineno_; | |
1343 | // The column number of the last token returned by next_token. | |
1344 | int charpos_; | |
1345 | // A stack of lexer modes. | |
1346 | std::vector<Lex::Mode> lex_mode_stack_; | |
09124467 ILT |
1347 | // A stack of which extern/language block we're inside. Can be C++, |
1348 | // java, or empty for C. | |
1349 | std::vector<std::string> language_stack_; | |
dbe717ef ILT |
1350 | // New input files found to add to the link. |
1351 | Input_arguments* inputs_; | |
1352 | }; | |
1353 | ||
1354 | // FILE was found as an argument on the command line. Try to read it | |
da769d56 | 1355 | // as a script. Return true if the file was handled. |
dbe717ef ILT |
1356 | |
1357 | bool | |
f1ed28fb | 1358 | read_input_script(Workqueue* workqueue, Symbol_table* symtab, Layout* layout, |
15f8229b ILT |
1359 | Dirsearch* dirsearch, int dirindex, |
1360 | Input_objects* input_objects, Mapfile* mapfile, | |
1361 | Input_group* input_group, | |
dbe717ef | 1362 | const Input_argument* input_argument, |
da769d56 ILT |
1363 | Input_file* input_file, Task_token* next_blocker, |
1364 | bool* used_next_blocker) | |
dbe717ef | 1365 | { |
da769d56 ILT |
1366 | *used_next_blocker = false; |
1367 | ||
e5756efb ILT |
1368 | std::string input_string; |
1369 | Lex::read_file(input_file, &input_string); | |
1370 | ||
1371 | Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT); | |
dbe717ef ILT |
1372 | |
1373 | Parser_closure closure(input_file->filename().c_str(), | |
1374 | input_argument->file().options(), | |
1375 | input_group != NULL, | |
ad2d6943 | 1376 | input_file->is_in_sysroot(), |
a0451b38 | 1377 | NULL, |
e5756efb | 1378 | layout->script_options(), |
15f8229b ILT |
1379 | &lex, |
1380 | input_file->will_search_for()); | |
dbe717ef ILT |
1381 | |
1382 | if (yyparse(&closure) != 0) | |
15f8229b ILT |
1383 | { |
1384 | if (closure.found_incompatible_target()) | |
1385 | { | |
1386 | Read_symbols::incompatible_warning(input_argument, input_file); | |
1387 | Read_symbols::requeue(workqueue, input_objects, symtab, layout, | |
1388 | dirsearch, dirindex, mapfile, input_argument, | |
1389 | input_group, next_blocker); | |
1390 | return true; | |
1391 | } | |
1392 | return false; | |
1393 | } | |
dbe717ef | 1394 | |
dbe717ef | 1395 | if (!closure.saw_inputs()) |
da769d56 | 1396 | return true; |
dbe717ef | 1397 | |
da769d56 | 1398 | Task_token* this_blocker = NULL; |
dbe717ef ILT |
1399 | for (Input_arguments::const_iterator p = closure.inputs()->begin(); |
1400 | p != closure.inputs()->end(); | |
1401 | ++p) | |
1402 | { | |
1403 | Task_token* nb; | |
1404 | if (p + 1 == closure.inputs()->end()) | |
1405 | nb = next_blocker; | |
1406 | else | |
1407 | { | |
17a1d0a9 | 1408 | nb = new Task_token(true); |
dbe717ef ILT |
1409 | nb->add_blocker(); |
1410 | } | |
f1ed28fb | 1411 | workqueue->queue_soon(new Read_symbols(input_objects, symtab, |
15f8229b | 1412 | layout, dirsearch, 0, mapfile, &*p, |
da769d56 | 1413 | input_group, this_blocker, nb)); |
dbe717ef ILT |
1414 | this_blocker = nb; |
1415 | } | |
1416 | ||
da769d56 ILT |
1417 | *used_next_blocker = true; |
1418 | ||
dbe717ef ILT |
1419 | return true; |
1420 | } | |
1421 | ||
09124467 ILT |
1422 | // Helper function for read_version_script() and |
1423 | // read_commandline_script(). Processes the given file in the mode | |
1424 | // indicated by first_token and lex_mode. | |
3c2fafa5 | 1425 | |
09124467 ILT |
1426 | static bool |
1427 | read_script_file(const char* filename, Command_line* cmdline, | |
c82fbeee | 1428 | Script_options* script_options, |
09124467 | 1429 | int first_token, Lex::Mode lex_mode) |
3c2fafa5 | 1430 | { |
a0451b38 ILT |
1431 | // TODO: if filename is a relative filename, search for it manually |
1432 | // using "." + cmdline->options()->search_path() -- not dirsearch. | |
3c2fafa5 ILT |
1433 | Dirsearch dirsearch; |
1434 | ||
17a1d0a9 ILT |
1435 | // The file locking code wants to record a Task, but we haven't |
1436 | // started the workqueue yet. This is only for debugging purposes, | |
1437 | // so we invent a fake value. | |
1438 | const Task* task = reinterpret_cast<const Task*>(-1); | |
1439 | ||
b0d8593d ILT |
1440 | // We don't want this file to be opened in binary mode. |
1441 | Position_dependent_options posdep = cmdline->position_dependent_options(); | |
7cc619c3 ILT |
1442 | if (posdep.format_enum() == General_options::OBJECT_FORMAT_BINARY) |
1443 | posdep.set_format_enum(General_options::OBJECT_FORMAT_ELF); | |
b0d8593d | 1444 | Input_file_argument input_argument(filename, false, "", false, posdep); |
3c2fafa5 | 1445 | Input_file input_file(&input_argument); |
15f8229b ILT |
1446 | int dummy = 0; |
1447 | if (!input_file.open(dirsearch, task, &dummy)) | |
3c2fafa5 ILT |
1448 | return false; |
1449 | ||
e5756efb ILT |
1450 | std::string input_string; |
1451 | Lex::read_file(&input_file, &input_string); | |
1452 | ||
09124467 ILT |
1453 | Lex lex(input_string.c_str(), input_string.length(), first_token); |
1454 | lex.set_mode(lex_mode); | |
3c2fafa5 ILT |
1455 | |
1456 | Parser_closure closure(filename, | |
1457 | cmdline->position_dependent_options(), | |
1458 | false, | |
1459 | input_file.is_in_sysroot(), | |
a0451b38 | 1460 | cmdline, |
c82fbeee | 1461 | script_options, |
15f8229b ILT |
1462 | &lex, |
1463 | false); | |
3c2fafa5 ILT |
1464 | if (yyparse(&closure) != 0) |
1465 | { | |
17a1d0a9 | 1466 | input_file.file().unlock(task); |
3c2fafa5 ILT |
1467 | return false; |
1468 | } | |
1469 | ||
17a1d0a9 | 1470 | input_file.file().unlock(task); |
d391083d ILT |
1471 | |
1472 | gold_assert(!closure.saw_inputs()); | |
1473 | ||
3c2fafa5 ILT |
1474 | return true; |
1475 | } | |
1476 | ||
09124467 ILT |
1477 | // FILENAME was found as an argument to --script (-T). |
1478 | // Read it as a script, and execute its contents immediately. | |
1479 | ||
1480 | bool | |
1481 | read_commandline_script(const char* filename, Command_line* cmdline) | |
1482 | { | |
c82fbeee | 1483 | return read_script_file(filename, cmdline, &cmdline->script_options(), |
09124467 ILT |
1484 | PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT); |
1485 | } | |
1486 | ||
c82fbeee CS |
1487 | // FILENAME was found as an argument to --version-script. Read it as |
1488 | // a version script, and store its contents in | |
09124467 ILT |
1489 | // cmdline->script_options()->version_script_info(). |
1490 | ||
1491 | bool | |
1492 | read_version_script(const char* filename, Command_line* cmdline) | |
1493 | { | |
c82fbeee | 1494 | return read_script_file(filename, cmdline, &cmdline->script_options(), |
09124467 ILT |
1495 | PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT); |
1496 | } | |
1497 | ||
c82fbeee CS |
1498 | // FILENAME was found as an argument to --dynamic-list. Read it as a |
1499 | // list of symbols, and store its contents in DYNAMIC_LIST. | |
1500 | ||
1501 | bool | |
1502 | read_dynamic_list(const char* filename, Command_line* cmdline, | |
1503 | Script_options* dynamic_list) | |
1504 | { | |
1505 | return read_script_file(filename, cmdline, dynamic_list, | |
1506 | PARSING_DYNAMIC_LIST, Lex::DYNAMIC_LIST); | |
1507 | } | |
1508 | ||
e5756efb ILT |
1509 | // Implement the --defsym option on the command line. Return true if |
1510 | // all is well. | |
1511 | ||
1512 | bool | |
1513 | Script_options::define_symbol(const char* definition) | |
1514 | { | |
1515 | Lex lex(definition, strlen(definition), PARSING_DEFSYM); | |
1516 | lex.set_mode(Lex::EXPRESSION); | |
1517 | ||
1518 | // Dummy value. | |
1519 | Position_dependent_options posdep_options; | |
1520 | ||
1521 | Parser_closure closure("command line", posdep_options, false, false, NULL, | |
15f8229b | 1522 | this, &lex, false); |
e5756efb ILT |
1523 | |
1524 | if (yyparse(&closure) != 0) | |
1525 | return false; | |
1526 | ||
1527 | gold_assert(!closure.saw_inputs()); | |
1528 | ||
1529 | return true; | |
1530 | } | |
1531 | ||
494e05f4 ILT |
1532 | // Print the script to F for debugging. |
1533 | ||
1534 | void | |
1535 | Script_options::print(FILE* f) const | |
1536 | { | |
1537 | fprintf(f, "%s: Dumping linker script\n", program_name); | |
1538 | ||
1539 | if (!this->entry_.empty()) | |
1540 | fprintf(f, "ENTRY(%s)\n", this->entry_.c_str()); | |
1541 | ||
1542 | for (Symbol_assignments::const_iterator p = | |
1543 | this->symbol_assignments_.begin(); | |
1544 | p != this->symbol_assignments_.end(); | |
1545 | ++p) | |
1546 | (*p)->print(f); | |
1547 | ||
1548 | for (Assertions::const_iterator p = this->assertions_.begin(); | |
1549 | p != this->assertions_.end(); | |
1550 | ++p) | |
1551 | (*p)->print(f); | |
1552 | ||
1553 | this->script_sections_.print(f); | |
1554 | ||
1555 | this->version_script_info_.print(f); | |
1556 | } | |
1557 | ||
dbe717ef | 1558 | // Manage mapping from keywords to the codes expected by the bison |
09124467 ILT |
1559 | // parser. We construct one global object for each lex mode with |
1560 | // keywords. | |
dbe717ef ILT |
1561 | |
1562 | class Keyword_to_parsecode | |
1563 | { | |
1564 | public: | |
1565 | // The structure which maps keywords to parsecodes. | |
1566 | struct Keyword_parsecode | |
1567 | { | |
1568 | // Keyword. | |
1569 | const char* keyword; | |
1570 | // Corresponding parsecode. | |
1571 | int parsecode; | |
1572 | }; | |
1573 | ||
09124467 ILT |
1574 | Keyword_to_parsecode(const Keyword_parsecode* keywords, |
1575 | int keyword_count) | |
1576 | : keyword_parsecodes_(keywords), keyword_count_(keyword_count) | |
1577 | { } | |
1578 | ||
dbe717ef ILT |
1579 | // Return the parsecode corresponding KEYWORD, or 0 if it is not a |
1580 | // keyword. | |
09124467 ILT |
1581 | int |
1582 | keyword_to_parsecode(const char* keyword, size_t len) const; | |
dbe717ef ILT |
1583 | |
1584 | private: | |
09124467 ILT |
1585 | const Keyword_parsecode* keyword_parsecodes_; |
1586 | const int keyword_count_; | |
dbe717ef ILT |
1587 | }; |
1588 | ||
1589 | // Mapping from keyword string to keyword parsecode. This array must | |
1590 | // be kept in sorted order. Parsecodes are looked up using bsearch. | |
1591 | // This array must correspond to the list of parsecodes in yyscript.y. | |
1592 | ||
09124467 ILT |
1593 | static const Keyword_to_parsecode::Keyword_parsecode |
1594 | script_keyword_parsecodes[] = | |
dbe717ef ILT |
1595 | { |
1596 | { "ABSOLUTE", ABSOLUTE }, | |
1597 | { "ADDR", ADDR }, | |
1598 | { "ALIGN", ALIGN_K }, | |
e5756efb | 1599 | { "ALIGNOF", ALIGNOF }, |
dbe717ef ILT |
1600 | { "ASSERT", ASSERT_K }, |
1601 | { "AS_NEEDED", AS_NEEDED }, | |
1602 | { "AT", AT }, | |
1603 | { "BIND", BIND }, | |
1604 | { "BLOCK", BLOCK }, | |
1605 | { "BYTE", BYTE }, | |
1606 | { "CONSTANT", CONSTANT }, | |
1607 | { "CONSTRUCTORS", CONSTRUCTORS }, | |
dbe717ef ILT |
1608 | { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS }, |
1609 | { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN }, | |
1610 | { "DATA_SEGMENT_END", DATA_SEGMENT_END }, | |
1611 | { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END }, | |
1612 | { "DEFINED", DEFINED }, | |
dbe717ef ILT |
1613 | { "ENTRY", ENTRY }, |
1614 | { "EXCLUDE_FILE", EXCLUDE_FILE }, | |
1615 | { "EXTERN", EXTERN }, | |
1616 | { "FILL", FILL }, | |
1617 | { "FLOAT", FLOAT }, | |
1618 | { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION }, | |
1619 | { "GROUP", GROUP }, | |
1620 | { "HLL", HLL }, | |
1621 | { "INCLUDE", INCLUDE }, | |
dbe717ef ILT |
1622 | { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION }, |
1623 | { "INPUT", INPUT }, | |
1624 | { "KEEP", KEEP }, | |
1625 | { "LENGTH", LENGTH }, | |
1626 | { "LOADADDR", LOADADDR }, | |
1627 | { "LONG", LONG }, | |
1628 | { "MAP", MAP }, | |
1629 | { "MAX", MAX_K }, | |
1630 | { "MEMORY", MEMORY }, | |
1631 | { "MIN", MIN_K }, | |
1632 | { "NEXT", NEXT }, | |
1633 | { "NOCROSSREFS", NOCROSSREFS }, | |
1634 | { "NOFLOAT", NOFLOAT }, | |
dbe717ef ILT |
1635 | { "ONLY_IF_RO", ONLY_IF_RO }, |
1636 | { "ONLY_IF_RW", ONLY_IF_RW }, | |
195e7dc6 | 1637 | { "OPTION", OPTION }, |
dbe717ef ILT |
1638 | { "ORIGIN", ORIGIN }, |
1639 | { "OUTPUT", OUTPUT }, | |
1640 | { "OUTPUT_ARCH", OUTPUT_ARCH }, | |
1641 | { "OUTPUT_FORMAT", OUTPUT_FORMAT }, | |
1642 | { "OVERLAY", OVERLAY }, | |
1643 | { "PHDRS", PHDRS }, | |
1644 | { "PROVIDE", PROVIDE }, | |
1645 | { "PROVIDE_HIDDEN", PROVIDE_HIDDEN }, | |
1646 | { "QUAD", QUAD }, | |
1647 | { "SEARCH_DIR", SEARCH_DIR }, | |
1648 | { "SECTIONS", SECTIONS }, | |
1649 | { "SEGMENT_START", SEGMENT_START }, | |
1650 | { "SHORT", SHORT }, | |
1651 | { "SIZEOF", SIZEOF }, | |
1652 | { "SIZEOF_HEADERS", SIZEOF_HEADERS }, | |
3802b2dd | 1653 | { "SORT", SORT_BY_NAME }, |
dbe717ef ILT |
1654 | { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT }, |
1655 | { "SORT_BY_NAME", SORT_BY_NAME }, | |
1656 | { "SPECIAL", SPECIAL }, | |
1657 | { "SQUAD", SQUAD }, | |
1658 | { "STARTUP", STARTUP }, | |
1659 | { "SUBALIGN", SUBALIGN }, | |
1660 | { "SYSLIB", SYSLIB }, | |
1661 | { "TARGET", TARGET_K }, | |
1662 | { "TRUNCATE", TRUNCATE }, | |
1663 | { "VERSION", VERSIONK }, | |
1664 | { "global", GLOBAL }, | |
1665 | { "l", LENGTH }, | |
1666 | { "len", LENGTH }, | |
1667 | { "local", LOCAL }, | |
1668 | { "o", ORIGIN }, | |
1669 | { "org", ORIGIN }, | |
1670 | { "sizeof_headers", SIZEOF_HEADERS }, | |
1671 | }; | |
1672 | ||
09124467 ILT |
1673 | static const Keyword_to_parsecode |
1674 | script_keywords(&script_keyword_parsecodes[0], | |
1675 | (sizeof(script_keyword_parsecodes) | |
1676 | / sizeof(script_keyword_parsecodes[0]))); | |
1677 | ||
1678 | static const Keyword_to_parsecode::Keyword_parsecode | |
1679 | version_script_keyword_parsecodes[] = | |
1680 | { | |
1681 | { "extern", EXTERN }, | |
1682 | { "global", GLOBAL }, | |
1683 | { "local", LOCAL }, | |
1684 | }; | |
1685 | ||
1686 | static const Keyword_to_parsecode | |
1687 | version_script_keywords(&version_script_keyword_parsecodes[0], | |
1688 | (sizeof(version_script_keyword_parsecodes) | |
1689 | / sizeof(version_script_keyword_parsecodes[0]))); | |
dbe717ef | 1690 | |
c82fbeee CS |
1691 | static const Keyword_to_parsecode::Keyword_parsecode |
1692 | dynamic_list_keyword_parsecodes[] = | |
1693 | { | |
1694 | { "extern", EXTERN }, | |
1695 | }; | |
1696 | ||
1697 | static const Keyword_to_parsecode | |
1698 | dynamic_list_keywords(&dynamic_list_keyword_parsecodes[0], | |
1699 | (sizeof(dynamic_list_keyword_parsecodes) | |
1700 | / sizeof(dynamic_list_keyword_parsecodes[0]))); | |
1701 | ||
1702 | ||
1703 | ||
dbe717ef ILT |
1704 | // Comparison function passed to bsearch. |
1705 | ||
1706 | extern "C" | |
1707 | { | |
1708 | ||
e5756efb ILT |
1709 | struct Ktt_key |
1710 | { | |
1711 | const char* str; | |
1712 | size_t len; | |
1713 | }; | |
1714 | ||
dbe717ef ILT |
1715 | static int |
1716 | ktt_compare(const void* keyv, const void* kttv) | |
1717 | { | |
e5756efb | 1718 | const Ktt_key* key = static_cast<const Ktt_key*>(keyv); |
dbe717ef ILT |
1719 | const Keyword_to_parsecode::Keyword_parsecode* ktt = |
1720 | static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv); | |
e5756efb ILT |
1721 | int i = strncmp(key->str, ktt->keyword, key->len); |
1722 | if (i != 0) | |
1723 | return i; | |
1724 | if (ktt->keyword[key->len] != '\0') | |
1725 | return -1; | |
1726 | return 0; | |
dbe717ef ILT |
1727 | } |
1728 | ||
1729 | } // End extern "C". | |
1730 | ||
1731 | int | |
09124467 ILT |
1732 | Keyword_to_parsecode::keyword_to_parsecode(const char* keyword, |
1733 | size_t len) const | |
dbe717ef | 1734 | { |
e5756efb ILT |
1735 | Ktt_key key; |
1736 | key.str = keyword; | |
1737 | key.len = len; | |
1738 | void* kttv = bsearch(&key, | |
09124467 ILT |
1739 | this->keyword_parsecodes_, |
1740 | this->keyword_count_, | |
1741 | sizeof(this->keyword_parsecodes_[0]), | |
1742 | ktt_compare); | |
dbe717ef ILT |
1743 | if (kttv == NULL) |
1744 | return 0; | |
1745 | Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv); | |
1746 | return ktt->parsecode; | |
1747 | } | |
1748 | ||
88a0e15b ILT |
1749 | // Helper class that calls cplus_demangle when needed and takes care of freeing |
1750 | // the result. | |
1751 | ||
1752 | class Lazy_demangler | |
1753 | { | |
1754 | public: | |
1755 | Lazy_demangler(const char* symbol, int options) | |
1756 | : symbol_(symbol), options_(options), demangled_(NULL), did_demangle_(false) | |
1757 | { } | |
1758 | ||
1759 | ~Lazy_demangler() | |
1760 | { free(this->demangled_); } | |
1761 | ||
1762 | // Return the demangled name. The actual demangling happens on the first call, | |
1763 | // and the result is later cached. | |
1764 | ||
1765 | inline char* | |
1766 | get(); | |
1767 | ||
1768 | private: | |
1769 | // The symbol to demangle. | |
1770 | const char *symbol_; | |
1771 | // Option flags to pass to cplus_demagle. | |
1772 | const int options_; | |
1773 | // The cached demangled value, or NULL if demangling didn't happen yet or | |
1774 | // failed. | |
1775 | char *demangled_; | |
1776 | // Whether we already called cplus_demangle | |
1777 | bool did_demangle_; | |
1778 | }; | |
1779 | ||
1780 | // Return the demangled name. The actual demangling happens on the first call, | |
1781 | // and the result is later cached. Returns NULL if the symbol cannot be | |
1782 | // demangled. | |
1783 | ||
1784 | inline char* | |
1785 | Lazy_demangler::get() | |
1786 | { | |
1787 | if (!this->did_demangle_) | |
1788 | { | |
1789 | this->demangled_ = cplus_demangle(this->symbol_, this->options_); | |
1790 | this->did_demangle_ = true; | |
1791 | } | |
1792 | return this->demangled_; | |
1793 | } | |
1794 | ||
494e05f4 ILT |
1795 | // The following structs are used within the VersionInfo class as well |
1796 | // as in the bison helper functions. They store the information | |
1797 | // parsed from the version script. | |
dbe717ef | 1798 | |
494e05f4 ILT |
1799 | // A single version expression. |
1800 | // For example, pattern="std::map*" and language="C++". | |
1801 | // pattern and language should be from the stringpool | |
1802 | struct Version_expression { | |
1803 | Version_expression(const std::string& pattern, | |
1804 | const std::string& language, | |
1805 | bool exact_match) | |
1806 | : pattern(pattern), language(language), exact_match(exact_match) {} | |
dbe717ef | 1807 | |
494e05f4 ILT |
1808 | std::string pattern; |
1809 | std::string language; | |
1810 | // If false, we use glob() to match pattern. If true, we use strcmp(). | |
1811 | bool exact_match; | |
1812 | }; | |
dbe717ef | 1813 | |
dbe717ef | 1814 | |
494e05f4 ILT |
1815 | // A list of expressions. |
1816 | struct Version_expression_list { | |
1817 | std::vector<struct Version_expression> expressions; | |
1818 | }; | |
e5756efb | 1819 | |
e5756efb | 1820 | |
494e05f4 ILT |
1821 | // A list of which versions upon which another version depends. |
1822 | // Strings should be from the Stringpool. | |
1823 | struct Version_dependency_list { | |
1824 | std::vector<std::string> dependencies; | |
1825 | }; | |
dbe717ef | 1826 | |
dbe717ef | 1827 | |
494e05f4 ILT |
1828 | // The total definition of a version. It includes the tag for the |
1829 | // version, its global and local expressions, and any dependencies. | |
1830 | struct Version_tree { | |
1831 | Version_tree() | |
1832 | : tag(), global(NULL), local(NULL), dependencies(NULL) {} | |
e5756efb | 1833 | |
494e05f4 ILT |
1834 | std::string tag; |
1835 | const struct Version_expression_list* global; | |
1836 | const struct Version_expression_list* local; | |
1837 | const struct Version_dependency_list* dependencies; | |
1838 | }; | |
dbe717ef | 1839 | |
494e05f4 | 1840 | Version_script_info::~Version_script_info() |
1ef1f3d3 ILT |
1841 | { |
1842 | this->clear(); | |
1843 | } | |
1844 | ||
1845 | void | |
1846 | Version_script_info::clear() | |
494e05f4 ILT |
1847 | { |
1848 | for (size_t k = 0; k < dependency_lists_.size(); ++k) | |
1849 | delete dependency_lists_[k]; | |
1ef1f3d3 | 1850 | this->dependency_lists_.clear(); |
494e05f4 ILT |
1851 | for (size_t k = 0; k < version_trees_.size(); ++k) |
1852 | delete version_trees_[k]; | |
1ef1f3d3 | 1853 | this->version_trees_.clear(); |
494e05f4 ILT |
1854 | for (size_t k = 0; k < expression_lists_.size(); ++k) |
1855 | delete expression_lists_[k]; | |
1ef1f3d3 | 1856 | this->expression_lists_.clear(); |
dbe717ef ILT |
1857 | } |
1858 | ||
494e05f4 ILT |
1859 | std::vector<std::string> |
1860 | Version_script_info::get_versions() const | |
dbe717ef | 1861 | { |
494e05f4 ILT |
1862 | std::vector<std::string> ret; |
1863 | for (size_t j = 0; j < version_trees_.size(); ++j) | |
057ead22 ILT |
1864 | if (!this->version_trees_[j]->tag.empty()) |
1865 | ret.push_back(this->version_trees_[j]->tag); | |
494e05f4 | 1866 | return ret; |
dbe717ef ILT |
1867 | } |
1868 | ||
494e05f4 ILT |
1869 | std::vector<std::string> |
1870 | Version_script_info::get_dependencies(const char* version) const | |
dbe717ef | 1871 | { |
494e05f4 ILT |
1872 | std::vector<std::string> ret; |
1873 | for (size_t j = 0; j < version_trees_.size(); ++j) | |
1874 | if (version_trees_[j]->tag == version) | |
1875 | { | |
1876 | const struct Version_dependency_list* deps = | |
1877 | version_trees_[j]->dependencies; | |
1878 | if (deps != NULL) | |
1879 | for (size_t k = 0; k < deps->dependencies.size(); ++k) | |
1880 | ret.push_back(deps->dependencies[k]); | |
1881 | return ret; | |
1882 | } | |
1883 | return ret; | |
1884 | } | |
1885 | ||
057ead22 ILT |
1886 | // Look up SYMBOL_NAME in the list of versions. If CHECK_GLOBAL is |
1887 | // true look at the globally visible symbols, otherwise look at the | |
1888 | // symbols listed as "local:". Return true if the symbol is found, | |
1889 | // false otherwise. If the symbol is found, then if PVERSION is not | |
1890 | // NULL, set *PVERSION to the version. | |
1891 | ||
1892 | bool | |
494e05f4 | 1893 | Version_script_info::get_symbol_version_helper(const char* symbol_name, |
057ead22 ILT |
1894 | bool check_global, |
1895 | std::string* pversion) const | |
494e05f4 | 1896 | { |
88a0e15b ILT |
1897 | Lazy_demangler cpp_demangled_name(symbol_name, DMGL_ANSI | DMGL_PARAMS); |
1898 | Lazy_demangler java_demangled_name(symbol_name, | |
1899 | DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA); | |
494e05f4 ILT |
1900 | for (size_t j = 0; j < version_trees_.size(); ++j) |
1901 | { | |
1902 | // Is it a global symbol for this version? | |
1903 | const Version_expression_list* explist = | |
1904 | check_global ? version_trees_[j]->global : version_trees_[j]->local; | |
1905 | if (explist != NULL) | |
1906 | for (size_t k = 0; k < explist->expressions.size(); ++k) | |
1907 | { | |
1908 | const char* name_to_match = symbol_name; | |
1909 | const struct Version_expression& exp = explist->expressions[k]; | |
494e05f4 ILT |
1910 | if (exp.language == "C++") |
1911 | { | |
88a0e15b | 1912 | name_to_match = cpp_demangled_name.get(); |
494e05f4 | 1913 | // This isn't a C++ symbol. |
88a0e15b | 1914 | if (name_to_match == NULL) |
494e05f4 | 1915 | continue; |
494e05f4 ILT |
1916 | } |
1917 | else if (exp.language == "Java") | |
1918 | { | |
88a0e15b | 1919 | name_to_match = java_demangled_name.get(); |
494e05f4 | 1920 | // This isn't a Java symbol. |
88a0e15b | 1921 | if (name_to_match == NULL) |
494e05f4 | 1922 | continue; |
494e05f4 ILT |
1923 | } |
1924 | bool matched; | |
1925 | if (exp.exact_match) | |
1926 | matched = strcmp(exp.pattern.c_str(), name_to_match) == 0; | |
1927 | else | |
1928 | matched = fnmatch(exp.pattern.c_str(), name_to_match, | |
1929 | FNM_NOESCAPE) == 0; | |
494e05f4 | 1930 | if (matched) |
057ead22 ILT |
1931 | { |
1932 | if (pversion != NULL) | |
1933 | *pversion = this->version_trees_[j]->tag; | |
1934 | return true; | |
1935 | } | |
494e05f4 ILT |
1936 | } |
1937 | } | |
057ead22 | 1938 | return false; |
494e05f4 ILT |
1939 | } |
1940 | ||
1941 | struct Version_dependency_list* | |
1942 | Version_script_info::allocate_dependency_list() | |
1943 | { | |
1944 | dependency_lists_.push_back(new Version_dependency_list); | |
1945 | return dependency_lists_.back(); | |
1946 | } | |
1947 | ||
1948 | struct Version_expression_list* | |
1949 | Version_script_info::allocate_expression_list() | |
1950 | { | |
1951 | expression_lists_.push_back(new Version_expression_list); | |
1952 | return expression_lists_.back(); | |
1953 | } | |
1954 | ||
1955 | struct Version_tree* | |
1956 | Version_script_info::allocate_version_tree() | |
1957 | { | |
1958 | version_trees_.push_back(new Version_tree); | |
1959 | return version_trees_.back(); | |
1960 | } | |
1961 | ||
1962 | // Print for debugging. | |
1963 | ||
1964 | void | |
1965 | Version_script_info::print(FILE* f) const | |
1966 | { | |
1967 | if (this->empty()) | |
1968 | return; | |
1969 | ||
1970 | fprintf(f, "VERSION {"); | |
1971 | ||
1972 | for (size_t i = 0; i < this->version_trees_.size(); ++i) | |
1973 | { | |
1974 | const Version_tree* vt = this->version_trees_[i]; | |
1975 | ||
1976 | if (vt->tag.empty()) | |
1977 | fprintf(f, " {\n"); | |
1978 | else | |
1979 | fprintf(f, " %s {\n", vt->tag.c_str()); | |
1980 | ||
1981 | if (vt->global != NULL) | |
1982 | { | |
1983 | fprintf(f, " global :\n"); | |
1984 | this->print_expression_list(f, vt->global); | |
1985 | } | |
1986 | ||
1987 | if (vt->local != NULL) | |
1988 | { | |
1989 | fprintf(f, " local :\n"); | |
1990 | this->print_expression_list(f, vt->local); | |
1991 | } | |
1992 | ||
1993 | fprintf(f, " }"); | |
1994 | if (vt->dependencies != NULL) | |
1995 | { | |
1996 | const Version_dependency_list* deps = vt->dependencies; | |
1997 | for (size_t j = 0; j < deps->dependencies.size(); ++j) | |
1998 | { | |
1999 | if (j < deps->dependencies.size() - 1) | |
2000 | fprintf(f, "\n"); | |
2001 | fprintf(f, " %s", deps->dependencies[j].c_str()); | |
2002 | } | |
2003 | } | |
2004 | fprintf(f, ";\n"); | |
2005 | } | |
2006 | ||
2007 | fprintf(f, "}\n"); | |
2008 | } | |
2009 | ||
2010 | void | |
2011 | Version_script_info::print_expression_list( | |
2012 | FILE* f, | |
2013 | const Version_expression_list* vel) const | |
2014 | { | |
2015 | std::string current_language; | |
2016 | for (size_t i = 0; i < vel->expressions.size(); ++i) | |
2017 | { | |
2018 | const Version_expression& ve(vel->expressions[i]); | |
2019 | ||
2020 | if (ve.language != current_language) | |
2021 | { | |
2022 | if (!current_language.empty()) | |
2023 | fprintf(f, " }\n"); | |
2024 | fprintf(f, " extern \"%s\" {\n", ve.language.c_str()); | |
2025 | current_language = ve.language; | |
2026 | } | |
2027 | ||
2028 | fprintf(f, " "); | |
2029 | if (!current_language.empty()) | |
2030 | fprintf(f, " "); | |
2031 | ||
2032 | if (ve.exact_match) | |
2033 | fprintf(f, "\""); | |
2034 | fprintf(f, "%s", ve.pattern.c_str()); | |
2035 | if (ve.exact_match) | |
2036 | fprintf(f, "\""); | |
2037 | ||
2038 | fprintf(f, "\n"); | |
2039 | } | |
2040 | ||
2041 | if (!current_language.empty()) | |
2042 | fprintf(f, " }\n"); | |
2043 | } | |
2044 | ||
2045 | } // End namespace gold. | |
2046 | ||
2047 | // The remaining functions are extern "C", so it's clearer to not put | |
2048 | // them in namespace gold. | |
2049 | ||
2050 | using namespace gold; | |
2051 | ||
2052 | // This function is called by the bison parser to return the next | |
2053 | // token. | |
2054 | ||
2055 | extern "C" int | |
2056 | yylex(YYSTYPE* lvalp, void* closurev) | |
2057 | { | |
2058 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2059 | const Token* token = closure->next_token(); | |
2060 | switch (token->classification()) | |
2061 | { | |
2062 | default: | |
2063 | gold_unreachable(); | |
2064 | ||
2065 | case Token::TOKEN_INVALID: | |
2066 | yyerror(closurev, "invalid character"); | |
2067 | return 0; | |
2068 | ||
2069 | case Token::TOKEN_EOF: | |
2070 | return 0; | |
2071 | ||
2072 | case Token::TOKEN_STRING: | |
2073 | { | |
2074 | // This is either a keyword or a STRING. | |
2075 | size_t len; | |
2076 | const char* str = token->string_value(&len); | |
2077 | int parsecode = 0; | |
2078 | switch (closure->lex_mode()) | |
2079 | { | |
2080 | case Lex::LINKER_SCRIPT: | |
2081 | parsecode = script_keywords.keyword_to_parsecode(str, len); | |
2082 | break; | |
2083 | case Lex::VERSION_SCRIPT: | |
2084 | parsecode = version_script_keywords.keyword_to_parsecode(str, len); | |
2085 | break; | |
c82fbeee CS |
2086 | case Lex::DYNAMIC_LIST: |
2087 | parsecode = dynamic_list_keywords.keyword_to_parsecode(str, len); | |
2088 | break; | |
494e05f4 ILT |
2089 | default: |
2090 | break; | |
2091 | } | |
2092 | if (parsecode != 0) | |
2093 | return parsecode; | |
2094 | lvalp->string.value = str; | |
2095 | lvalp->string.length = len; | |
2096 | return STRING; | |
2097 | } | |
2098 | ||
2099 | case Token::TOKEN_QUOTED_STRING: | |
2100 | lvalp->string.value = token->string_value(&lvalp->string.length); | |
2101 | return QUOTED_STRING; | |
2102 | ||
2103 | case Token::TOKEN_OPERATOR: | |
2104 | return token->operator_value(); | |
2105 | ||
2106 | case Token::TOKEN_INTEGER: | |
2107 | lvalp->integer = token->integer_value(); | |
2108 | return INTEGER; | |
2109 | } | |
2110 | } | |
2111 | ||
2112 | // This function is called by the bison parser to report an error. | |
2113 | ||
2114 | extern "C" void | |
2115 | yyerror(void* closurev, const char* message) | |
2116 | { | |
2117 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2118 | gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(), | |
2119 | closure->charpos(), message); | |
2120 | } | |
2121 | ||
afc06bb8 ILT |
2122 | // Called by the bison parser to add an external symbol to the link. |
2123 | ||
2124 | extern "C" void | |
2125 | script_add_extern(void* closurev, const char* name, size_t length) | |
2126 | { | |
2127 | // We treat exactly like -u NAME. FIXME: If it seems useful, we | |
2128 | // could handle this after the command line has been read, by adding | |
2129 | // entries to the symbol table directly. | |
2130 | std::string arg("--undefined="); | |
2131 | arg.append(name, length); | |
2132 | script_parse_option(closurev, arg.c_str(), arg.size()); | |
2133 | } | |
2134 | ||
494e05f4 ILT |
2135 | // Called by the bison parser to add a file to the link. |
2136 | ||
2137 | extern "C" void | |
2138 | script_add_file(void* closurev, const char* name, size_t length) | |
2139 | { | |
2140 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2141 | ||
2142 | // If this is an absolute path, and we found the script in the | |
2143 | // sysroot, then we want to prepend the sysroot to the file name. | |
2144 | // For example, this is how we handle a cross link to the x86_64 | |
2145 | // libc.so, which refers to /lib/libc.so.6. | |
2146 | std::string name_string(name, length); | |
2147 | const char* extra_search_path = "."; | |
2148 | std::string script_directory; | |
2149 | if (IS_ABSOLUTE_PATH(name_string.c_str())) | |
2150 | { | |
2151 | if (closure->is_in_sysroot()) | |
2152 | { | |
8851ecca | 2153 | const std::string& sysroot(parameters->options().sysroot()); |
494e05f4 ILT |
2154 | gold_assert(!sysroot.empty()); |
2155 | name_string = sysroot + name_string; | |
2156 | } | |
2157 | } | |
2158 | else | |
2159 | { | |
2160 | // In addition to checking the normal library search path, we | |
2161 | // also want to check in the script-directory. | |
2162 | const char *slash = strrchr(closure->filename(), '/'); | |
2163 | if (slash != NULL) | |
2164 | { | |
2165 | script_directory.assign(closure->filename(), | |
2166 | slash - closure->filename() + 1); | |
2167 | extra_search_path = script_directory.c_str(); | |
2168 | } | |
2169 | } | |
2170 | ||
2171 | Input_file_argument file(name_string.c_str(), false, extra_search_path, | |
88dd47ac | 2172 | false, closure->position_dependent_options()); |
494e05f4 | 2173 | closure->inputs()->add_file(file); |
dbe717ef ILT |
2174 | } |
2175 | ||
2176 | // Called by the bison parser to start a group. If we are already in | |
2177 | // a group, that means that this script was invoked within a | |
2178 | // --start-group --end-group sequence on the command line, or that | |
2179 | // this script was found in a GROUP of another script. In that case, | |
2180 | // we simply continue the existing group, rather than starting a new | |
2181 | // one. It is possible to construct a case in which this will do | |
2182 | // something other than what would happen if we did a recursive group, | |
2183 | // but it's hard to imagine why the different behaviour would be | |
2184 | // useful for a real program. Avoiding recursive groups is simpler | |
2185 | // and more efficient. | |
2186 | ||
2187 | extern "C" void | |
2188 | script_start_group(void* closurev) | |
2189 | { | |
2190 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2191 | if (!closure->in_group()) | |
2192 | closure->inputs()->start_group(); | |
2193 | } | |
2194 | ||
2195 | // Called by the bison parser at the end of a group. | |
2196 | ||
2197 | extern "C" void | |
2198 | script_end_group(void* closurev) | |
2199 | { | |
2200 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2201 | if (!closure->in_group()) | |
2202 | closure->inputs()->end_group(); | |
2203 | } | |
2204 | ||
2205 | // Called by the bison parser to start an AS_NEEDED list. | |
2206 | ||
2207 | extern "C" void | |
2208 | script_start_as_needed(void* closurev) | |
2209 | { | |
2210 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
45aa233b | 2211 | closure->position_dependent_options().set_as_needed(true); |
dbe717ef ILT |
2212 | } |
2213 | ||
2214 | // Called by the bison parser at the end of an AS_NEEDED list. | |
2215 | ||
2216 | extern "C" void | |
2217 | script_end_as_needed(void* closurev) | |
2218 | { | |
2219 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
45aa233b | 2220 | closure->position_dependent_options().set_as_needed(false); |
dbe717ef | 2221 | } |
195e7dc6 | 2222 | |
d391083d ILT |
2223 | // Called by the bison parser to set the entry symbol. |
2224 | ||
2225 | extern "C" void | |
e5756efb | 2226 | script_set_entry(void* closurev, const char* entry, size_t length) |
d391083d | 2227 | { |
a5dc0706 ILT |
2228 | // We'll parse this exactly the same as --entry=ENTRY on the commandline |
2229 | // TODO(csilvers): FIXME -- call set_entry directly. | |
1890b465 | 2230 | std::string arg("--entry="); |
a5dc0706 ILT |
2231 | arg.append(entry, length); |
2232 | script_parse_option(closurev, arg.c_str(), arg.size()); | |
e5756efb ILT |
2233 | } |
2234 | ||
0dfbdef4 ILT |
2235 | // Called by the bison parser to set whether to define common symbols. |
2236 | ||
2237 | extern "C" void | |
2238 | script_set_common_allocation(void* closurev, int set) | |
2239 | { | |
2240 | const char* arg = set != 0 ? "--define-common" : "--no-define-common"; | |
2241 | script_parse_option(closurev, arg, strlen(arg)); | |
2242 | } | |
2243 | ||
e5756efb ILT |
2244 | // Called by the bison parser to define a symbol. |
2245 | ||
2246 | extern "C" void | |
2247 | script_set_symbol(void* closurev, const char* name, size_t length, | |
2248 | Expression* value, int providei, int hiddeni) | |
2249 | { | |
2250 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2251 | const bool provide = providei != 0; | |
2252 | const bool hidden = hiddeni != 0; | |
2253 | closure->script_options()->add_symbol_assignment(name, length, value, | |
2254 | provide, hidden); | |
15f8229b | 2255 | closure->clear_skip_on_incompatible_target(); |
d391083d ILT |
2256 | } |
2257 | ||
494e05f4 ILT |
2258 | // Called by the bison parser to add an assertion. |
2259 | ||
2260 | extern "C" void | |
2261 | script_add_assertion(void* closurev, Expression* check, const char* message, | |
2262 | size_t messagelen) | |
2263 | { | |
2264 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2265 | closure->script_options()->add_assertion(check, message, messagelen); | |
15f8229b | 2266 | closure->clear_skip_on_incompatible_target(); |
494e05f4 ILT |
2267 | } |
2268 | ||
195e7dc6 ILT |
2269 | // Called by the bison parser to parse an OPTION. |
2270 | ||
2271 | extern "C" void | |
e5756efb | 2272 | script_parse_option(void* closurev, const char* option, size_t length) |
195e7dc6 ILT |
2273 | { |
2274 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
a0451b38 ILT |
2275 | // We treat the option as a single command-line option, even if |
2276 | // it has internal whitespace. | |
2277 | if (closure->command_line() == NULL) | |
2278 | { | |
2279 | // There are some options that we could handle here--e.g., | |
2280 | // -lLIBRARY. Should we bother? | |
e5756efb | 2281 | gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid" |
d391083d | 2282 | " for scripts specified via -T/--script"), |
e5756efb | 2283 | closure->filename(), closure->lineno(), closure->charpos()); |
a0451b38 ILT |
2284 | } |
2285 | else | |
2286 | { | |
2287 | bool past_a_double_dash_option = false; | |
ee1fe73e | 2288 | const char* mutable_option = strndup(option, length); |
e5756efb | 2289 | gold_assert(mutable_option != NULL); |
a0451b38 ILT |
2290 | closure->command_line()->process_one_option(1, &mutable_option, 0, |
2291 | &past_a_double_dash_option); | |
a5dc0706 ILT |
2292 | // The General_options class will quite possibly store a pointer |
2293 | // into mutable_option, so we can't free it. In cases the class | |
2294 | // does not store such a pointer, this is a memory leak. Alas. :( | |
a0451b38 | 2295 | } |
15f8229b ILT |
2296 | closure->clear_skip_on_incompatible_target(); |
2297 | } | |
2298 | ||
2299 | // Called by the bison parser to handle OUTPUT_FORMAT. OUTPUT_FORMAT | |
2300 | // takes either one or three arguments. In the three argument case, | |
2301 | // the format depends on the endianness option, which we don't | |
2302 | // currently support (FIXME). If we see an OUTPUT_FORMAT for the | |
2303 | // wrong format, then we want to search for a new file. Returning 0 | |
2304 | // here will cause the parser to immediately abort. | |
2305 | ||
2306 | extern "C" int | |
2307 | script_check_output_format(void* closurev, | |
2308 | const char* default_name, size_t default_length, | |
2309 | const char*, size_t, const char*, size_t) | |
2310 | { | |
2311 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2312 | std::string name(default_name, default_length); | |
2313 | Target* target = select_target_by_name(name.c_str()); | |
2314 | if (target == NULL || !parameters->is_compatible_target(target)) | |
2315 | { | |
2316 | if (closure->skip_on_incompatible_target()) | |
2317 | { | |
2318 | closure->set_found_incompatible_target(); | |
2319 | return 0; | |
2320 | } | |
2321 | // FIXME: Should we warn about the unknown target? | |
2322 | } | |
2323 | return 1; | |
195e7dc6 | 2324 | } |
e5756efb | 2325 | |
3802b2dd ILT |
2326 | // Called by the bison parser to handle SEARCH_DIR. This is handled |
2327 | // exactly like a -L option. | |
2328 | ||
2329 | extern "C" void | |
2330 | script_add_search_dir(void* closurev, const char* option, size_t length) | |
2331 | { | |
2332 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2333 | if (closure->command_line() == NULL) | |
2334 | gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid" | |
2335 | " for scripts specified via -T/--script"), | |
2336 | closure->filename(), closure->lineno(), closure->charpos()); | |
2337 | else | |
2338 | { | |
2339 | std::string s = "-L" + std::string(option, length); | |
2340 | script_parse_option(closurev, s.c_str(), s.size()); | |
2341 | } | |
2342 | } | |
2343 | ||
e5756efb ILT |
2344 | /* Called by the bison parser to push the lexer into expression |
2345 | mode. */ | |
2346 | ||
494e05f4 | 2347 | extern "C" void |
e5756efb ILT |
2348 | script_push_lex_into_expression_mode(void* closurev) |
2349 | { | |
2350 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2351 | closure->push_lex_mode(Lex::EXPRESSION); | |
2352 | } | |
2353 | ||
09124467 ILT |
2354 | /* Called by the bison parser to push the lexer into version |
2355 | mode. */ | |
2356 | ||
494e05f4 | 2357 | extern "C" void |
09124467 ILT |
2358 | script_push_lex_into_version_mode(void* closurev) |
2359 | { | |
2360 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2361 | closure->push_lex_mode(Lex::VERSION_SCRIPT); | |
2362 | } | |
2363 | ||
e5756efb ILT |
2364 | /* Called by the bison parser to pop the lexer mode. */ |
2365 | ||
494e05f4 | 2366 | extern "C" void |
e5756efb ILT |
2367 | script_pop_lex_mode(void* closurev) |
2368 | { | |
2369 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2370 | closure->pop_lex_mode(); | |
2371 | } | |
09124467 | 2372 | |
09124467 ILT |
2373 | // Register an entire version node. For example: |
2374 | // | |
2375 | // GLIBC_2.1 { | |
2376 | // global: foo; | |
2377 | // } GLIBC_2.0; | |
2378 | // | |
2379 | // - tag is "GLIBC_2.1" | |
2380 | // - tree contains the information "global: foo" | |
2381 | // - deps contains "GLIBC_2.0" | |
2382 | ||
2383 | extern "C" void | |
2384 | script_register_vers_node(void*, | |
2385 | const char* tag, | |
2386 | int taglen, | |
2387 | struct Version_tree *tree, | |
2388 | struct Version_dependency_list *deps) | |
2389 | { | |
2390 | gold_assert(tree != NULL); | |
09124467 | 2391 | tree->dependencies = deps; |
057ead22 ILT |
2392 | if (tag != NULL) |
2393 | tree->tag = std::string(tag, taglen); | |
09124467 ILT |
2394 | } |
2395 | ||
2396 | // Add a dependencies to the list of existing dependencies, if any, | |
2397 | // and return the expanded list. | |
2398 | ||
2399 | extern "C" struct Version_dependency_list * | |
2400 | script_add_vers_depend(void* closurev, | |
2401 | struct Version_dependency_list *all_deps, | |
2402 | const char *depend_to_add, int deplen) | |
2403 | { | |
2404 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2405 | if (all_deps == NULL) | |
2406 | all_deps = closure->version_script()->allocate_dependency_list(); | |
2407 | all_deps->dependencies.push_back(std::string(depend_to_add, deplen)); | |
2408 | return all_deps; | |
2409 | } | |
2410 | ||
2411 | // Add a pattern expression to an existing list of expressions, if any. | |
2412 | // TODO: In the old linker, the last argument used to be a bool, but I | |
2413 | // don't know what it meant. | |
2414 | ||
2415 | extern "C" struct Version_expression_list * | |
2416 | script_new_vers_pattern(void* closurev, | |
2417 | struct Version_expression_list *expressions, | |
10600224 | 2418 | const char *pattern, int patlen, int exact_match) |
09124467 ILT |
2419 | { |
2420 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2421 | if (expressions == NULL) | |
2422 | expressions = closure->version_script()->allocate_expression_list(); | |
2423 | expressions->expressions.push_back( | |
2424 | Version_expression(std::string(pattern, patlen), | |
10600224 ILT |
2425 | closure->get_current_language(), |
2426 | static_cast<bool>(exact_match))); | |
09124467 ILT |
2427 | return expressions; |
2428 | } | |
2429 | ||
10600224 ILT |
2430 | // Attaches b to the end of a, and clears b. So a = a + b and b = {}. |
2431 | ||
2432 | extern "C" struct Version_expression_list* | |
2433 | script_merge_expressions(struct Version_expression_list *a, | |
2434 | struct Version_expression_list *b) | |
2435 | { | |
2436 | a->expressions.insert(a->expressions.end(), | |
2437 | b->expressions.begin(), b->expressions.end()); | |
2438 | // We could delete b and remove it from expressions_lists_, but | |
2439 | // that's a lot of work. This works just as well. | |
2440 | b->expressions.clear(); | |
2441 | return a; | |
2442 | } | |
2443 | ||
09124467 ILT |
2444 | // Combine the global and local expressions into a a Version_tree. |
2445 | ||
2446 | extern "C" struct Version_tree * | |
2447 | script_new_vers_node(void* closurev, | |
2448 | struct Version_expression_list *global, | |
2449 | struct Version_expression_list *local) | |
2450 | { | |
2451 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2452 | Version_tree* tree = closure->version_script()->allocate_version_tree(); | |
2453 | tree->global = global; | |
2454 | tree->local = local; | |
2455 | return tree; | |
2456 | } | |
2457 | ||
10600224 | 2458 | // Handle a transition in language, such as at the |
09124467 ILT |
2459 | // start or end of 'extern "C++"' |
2460 | ||
2461 | extern "C" void | |
2462 | version_script_push_lang(void* closurev, const char* lang, int langlen) | |
2463 | { | |
2464 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2465 | closure->push_language(std::string(lang, langlen)); | |
2466 | } | |
2467 | ||
2468 | extern "C" void | |
2469 | version_script_pop_lang(void* closurev) | |
2470 | { | |
2471 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2472 | closure->pop_language(); | |
2473 | } | |
494e05f4 ILT |
2474 | |
2475 | // Called by the bison parser to start a SECTIONS clause. | |
2476 | ||
2477 | extern "C" void | |
2478 | script_start_sections(void* closurev) | |
2479 | { | |
2480 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2481 | closure->script_options()->script_sections()->start_sections(); | |
15f8229b | 2482 | closure->clear_skip_on_incompatible_target(); |
494e05f4 ILT |
2483 | } |
2484 | ||
2485 | // Called by the bison parser to finish a SECTIONS clause. | |
2486 | ||
2487 | extern "C" void | |
2488 | script_finish_sections(void* closurev) | |
2489 | { | |
2490 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2491 | closure->script_options()->script_sections()->finish_sections(); | |
2492 | } | |
2493 | ||
2494 | // Start processing entries for an output section. | |
2495 | ||
2496 | extern "C" void | |
2497 | script_start_output_section(void* closurev, const char* name, size_t namelen, | |
2498 | const struct Parser_output_section_header* header) | |
2499 | { | |
2500 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2501 | closure->script_options()->script_sections()->start_output_section(name, | |
2502 | namelen, | |
2503 | header); | |
2504 | } | |
2505 | ||
2506 | // Finish processing entries for an output section. | |
2507 | ||
2508 | extern "C" void | |
c82fbeee | 2509 | script_finish_output_section(void* closurev, |
494e05f4 ILT |
2510 | const struct Parser_output_section_trailer* trail) |
2511 | { | |
2512 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2513 | closure->script_options()->script_sections()->finish_output_section(trail); | |
2514 | } | |
2515 | ||
2516 | // Add a data item (e.g., "WORD (0)") to the current output section. | |
2517 | ||
2518 | extern "C" void | |
2519 | script_add_data(void* closurev, int data_token, Expression* val) | |
2520 | { | |
2521 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2522 | int size; | |
2523 | bool is_signed = true; | |
2524 | switch (data_token) | |
2525 | { | |
2526 | case QUAD: | |
2527 | size = 8; | |
2528 | is_signed = false; | |
2529 | break; | |
2530 | case SQUAD: | |
2531 | size = 8; | |
2532 | break; | |
2533 | case LONG: | |
2534 | size = 4; | |
2535 | break; | |
2536 | case SHORT: | |
2537 | size = 2; | |
2538 | break; | |
2539 | case BYTE: | |
2540 | size = 1; | |
2541 | break; | |
2542 | default: | |
2543 | gold_unreachable(); | |
2544 | } | |
2545 | closure->script_options()->script_sections()->add_data(size, is_signed, val); | |
2546 | } | |
2547 | ||
2548 | // Add a clause setting the fill value to the current output section. | |
2549 | ||
2550 | extern "C" void | |
2551 | script_add_fill(void* closurev, Expression* val) | |
2552 | { | |
2553 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2554 | closure->script_options()->script_sections()->add_fill(val); | |
2555 | } | |
2556 | ||
2557 | // Add a new input section specification to the current output | |
2558 | // section. | |
2559 | ||
2560 | extern "C" void | |
2561 | script_add_input_section(void* closurev, | |
2562 | const struct Input_section_spec* spec, | |
2563 | int keepi) | |
2564 | { | |
2565 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2566 | bool keep = keepi != 0; | |
2567 | closure->script_options()->script_sections()->add_input_section(spec, keep); | |
2568 | } | |
2569 | ||
2d924fd9 ILT |
2570 | // When we see DATA_SEGMENT_ALIGN we record that following output |
2571 | // sections may be relro. | |
2572 | ||
2573 | extern "C" void | |
2574 | script_data_segment_align(void* closurev) | |
2575 | { | |
2576 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2577 | if (!closure->script_options()->saw_sections_clause()) | |
2578 | gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"), | |
2579 | closure->filename(), closure->lineno(), closure->charpos()); | |
2580 | else | |
2581 | closure->script_options()->script_sections()->data_segment_align(); | |
2582 | } | |
2583 | ||
2584 | // When we see DATA_SEGMENT_RELRO_END we know that all output sections | |
2585 | // since DATA_SEGMENT_ALIGN should be relro. | |
2586 | ||
2587 | extern "C" void | |
2588 | script_data_segment_relro_end(void* closurev) | |
2589 | { | |
2590 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2591 | if (!closure->script_options()->saw_sections_clause()) | |
2592 | gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"), | |
2593 | closure->filename(), closure->lineno(), closure->charpos()); | |
2594 | else | |
2595 | closure->script_options()->script_sections()->data_segment_relro_end(); | |
2596 | } | |
2597 | ||
494e05f4 ILT |
2598 | // Create a new list of string/sort pairs. |
2599 | ||
2600 | extern "C" String_sort_list_ptr | |
2601 | script_new_string_sort_list(const struct Wildcard_section* string_sort) | |
2602 | { | |
2603 | return new String_sort_list(1, *string_sort); | |
2604 | } | |
2605 | ||
2606 | // Add an entry to a list of string/sort pairs. The way the parser | |
2607 | // works permits us to simply modify the first parameter, rather than | |
2608 | // copy the vector. | |
2609 | ||
2610 | extern "C" String_sort_list_ptr | |
2611 | script_string_sort_list_add(String_sort_list_ptr pv, | |
2612 | const struct Wildcard_section* string_sort) | |
2613 | { | |
a445fddf ILT |
2614 | if (pv == NULL) |
2615 | return script_new_string_sort_list(string_sort); | |
2616 | else | |
2617 | { | |
2618 | pv->push_back(*string_sort); | |
2619 | return pv; | |
2620 | } | |
494e05f4 ILT |
2621 | } |
2622 | ||
2623 | // Create a new list of strings. | |
2624 | ||
2625 | extern "C" String_list_ptr | |
2626 | script_new_string_list(const char* str, size_t len) | |
2627 | { | |
2628 | return new String_list(1, std::string(str, len)); | |
2629 | } | |
2630 | ||
2631 | // Add an element to a list of strings. The way the parser works | |
2632 | // permits us to simply modify the first parameter, rather than copy | |
2633 | // the vector. | |
2634 | ||
2635 | extern "C" String_list_ptr | |
2636 | script_string_list_push_back(String_list_ptr pv, const char* str, size_t len) | |
2637 | { | |
1c4f3631 ILT |
2638 | if (pv == NULL) |
2639 | return script_new_string_list(str, len); | |
2640 | else | |
2641 | { | |
2642 | pv->push_back(std::string(str, len)); | |
2643 | return pv; | |
2644 | } | |
494e05f4 ILT |
2645 | } |
2646 | ||
2647 | // Concatenate two string lists. Either or both may be NULL. The way | |
2648 | // the parser works permits us to modify the parameters, rather than | |
2649 | // copy the vector. | |
2650 | ||
2651 | extern "C" String_list_ptr | |
2652 | script_string_list_append(String_list_ptr pv1, String_list_ptr pv2) | |
2653 | { | |
2654 | if (pv1 == NULL) | |
2655 | return pv2; | |
2656 | if (pv2 == NULL) | |
2657 | return pv1; | |
2658 | pv1->insert(pv1->end(), pv2->begin(), pv2->end()); | |
2659 | return pv1; | |
2660 | } | |
1c4f3631 ILT |
2661 | |
2662 | // Add a new program header. | |
2663 | ||
2664 | extern "C" void | |
2665 | script_add_phdr(void* closurev, const char* name, size_t namelen, | |
2666 | unsigned int type, const Phdr_info* info) | |
2667 | { | |
2668 | Parser_closure* closure = static_cast<Parser_closure*>(closurev); | |
2669 | bool includes_filehdr = info->includes_filehdr != 0; | |
2670 | bool includes_phdrs = info->includes_phdrs != 0; | |
2671 | bool is_flags_valid = info->is_flags_valid != 0; | |
2672 | Script_sections* ss = closure->script_options()->script_sections(); | |
2673 | ss->add_phdr(name, namelen, type, includes_filehdr, includes_phdrs, | |
2674 | is_flags_valid, info->flags, info->load_address); | |
15f8229b | 2675 | closure->clear_skip_on_incompatible_target(); |
1c4f3631 ILT |
2676 | } |
2677 | ||
2678 | // Convert a program header string to a type. | |
2679 | ||
2680 | #define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME } | |
2681 | ||
2682 | static struct | |
2683 | { | |
2684 | const char* name; | |
2685 | size_t namelen; | |
2686 | unsigned int val; | |
2687 | } phdr_type_names[] = | |
2688 | { | |
2689 | PHDR_TYPE(PT_NULL), | |
2690 | PHDR_TYPE(PT_LOAD), | |
2691 | PHDR_TYPE(PT_DYNAMIC), | |
2692 | PHDR_TYPE(PT_INTERP), | |
2693 | PHDR_TYPE(PT_NOTE), | |
2694 | PHDR_TYPE(PT_SHLIB), | |
2695 | PHDR_TYPE(PT_PHDR), | |
2696 | PHDR_TYPE(PT_TLS), | |
2697 | PHDR_TYPE(PT_GNU_EH_FRAME), | |
2698 | PHDR_TYPE(PT_GNU_STACK), | |
2699 | PHDR_TYPE(PT_GNU_RELRO) | |
2700 | }; | |
2701 | ||
2702 | extern "C" unsigned int | |
2703 | script_phdr_string_to_type(void* closurev, const char* name, size_t namelen) | |
2704 | { | |
2705 | for (unsigned int i = 0; | |
2706 | i < sizeof(phdr_type_names) / sizeof(phdr_type_names[0]); | |
2707 | ++i) | |
2708 | if (namelen == phdr_type_names[i].namelen | |
2709 | && strncmp(name, phdr_type_names[i].name, namelen) == 0) | |
2710 | return phdr_type_names[i].val; | |
2711 | yyerror(closurev, _("unknown PHDR type (try integer)")); | |
2712 | return elfcpp::PT_NULL; | |
2713 | } |