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