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