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