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