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