Read input scripts which look like input objects with proper
[deliverable/binutils-gdb.git] / gold / script.cc
CommitLineData
dbe717ef
ILT
1// script.cc -- handle linker scripts for gold.
2
e5756efb 3// Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
6cb15b7f
ILT
4// Written by Ian Lance Taylor <iant@google.com>.
5
6// This file is part of gold.
7
8// This program is free software; you can redistribute it and/or modify
9// it under the terms of the GNU General Public License as published by
10// the Free Software Foundation; either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU General Public License for more details.
17
18// You should have received a copy of the GNU General Public License
19// along with this program; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21// MA 02110-1301, USA.
22
dbe717ef
ILT
23#include "gold.h"
24
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
494e05f4 849// class Symbol_assignment.
e5756efb 850
494e05f4
ILT
851// Add the symbol to the symbol table. This makes sure the symbol is
852// there and defined. The actual value is stored later. We can't
853// determine the actual value at this point, because we can't
854// necessarily evaluate the expression until all ordinary symbols have
855// been finalized.
e5756efb 856
caa9d5d9
ILT
857// The GNU linker lets symbol assignments in the linker script
858// silently override defined symbols in object files. We are
859// compatible. FIXME: Should we issue a warning?
860
e5756efb 861void
9b07f471 862Symbol_assignment::add_to_table(Symbol_table* symtab)
e5756efb 863{
494e05f4 864 elfcpp::STV vis = this->hidden_ ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
9b07f471 865 this->sym_ = symtab->define_as_constant(this->name_.c_str(),
e5756efb
ILT
866 NULL, // version
867 0, // value
868 0, // size
869 elfcpp::STT_NOTYPE,
870 elfcpp::STB_GLOBAL,
871 vis,
872 0, // nonvis
caa9d5d9
ILT
873 this->provide_,
874 true); // force_override
e5756efb
ILT
875}
876
494e05f4 877// Finalize a symbol value.
e5756efb
ILT
878
879void
494e05f4 880Symbol_assignment::finalize(Symbol_table* symtab, const Layout* layout)
a445fddf 881{
77e65537 882 this->finalize_maybe_dot(symtab, layout, false, 0, NULL);
a445fddf
ILT
883}
884
885// Finalize a symbol value which can refer to the dot symbol.
886
887void
888Symbol_assignment::finalize_with_dot(Symbol_table* symtab,
889 const Layout* layout,
77e65537
ILT
890 uint64_t dot_value,
891 Output_section* dot_section)
a445fddf 892{
77e65537 893 this->finalize_maybe_dot(symtab, layout, true, dot_value, dot_section);
a445fddf
ILT
894}
895
896// Finalize a symbol value, internal version.
897
898void
899Symbol_assignment::finalize_maybe_dot(Symbol_table* symtab,
900 const Layout* layout,
901 bool is_dot_available,
77e65537
ILT
902 uint64_t dot_value,
903 Output_section* dot_section)
e5756efb 904{
494e05f4
ILT
905 // If we were only supposed to provide this symbol, the sym_ field
906 // will be NULL if the symbol was not referenced.
907 if (this->sym_ == NULL)
908 {
909 gold_assert(this->provide_);
910 return;
911 }
912
8851ecca 913 if (parameters->target().get_size() == 32)
e5756efb
ILT
914 {
915#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
77e65537
ILT
916 this->sized_finalize<32>(symtab, layout, is_dot_available, dot_value,
917 dot_section);
e5756efb
ILT
918#else
919 gold_unreachable();
920#endif
921 }
8851ecca 922 else if (parameters->target().get_size() == 64)
e5756efb
ILT
923 {
924#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
77e65537
ILT
925 this->sized_finalize<64>(symtab, layout, is_dot_available, dot_value,
926 dot_section);
e5756efb
ILT
927#else
928 gold_unreachable();
929#endif
930 }
931 else
932 gold_unreachable();
933}
934
935template<int size>
936void
a445fddf 937Symbol_assignment::sized_finalize(Symbol_table* symtab, const Layout* layout,
77e65537
ILT
938 bool is_dot_available, uint64_t dot_value,
939 Output_section* dot_section)
a445fddf 940{
77e65537 941 Output_section* section;
919ed24c 942 uint64_t final_val = this->val_->eval_maybe_dot(symtab, layout, true,
a445fddf 943 is_dot_available,
77e65537
ILT
944 dot_value, dot_section,
945 &section);
494e05f4 946 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(this->sym_);
a445fddf 947 ssym->set_value(final_val);
77e65537
ILT
948 if (section != NULL)
949 ssym->set_output_section(section);
a445fddf
ILT
950}
951
952// Set the symbol value if the expression yields an absolute value.
953
954void
955Symbol_assignment::set_if_absolute(Symbol_table* symtab, const Layout* layout,
77e65537 956 bool is_dot_available, uint64_t dot_value)
a445fddf
ILT
957{
958 if (this->sym_ == NULL)
959 return;
960
77e65537 961 Output_section* val_section;
919ed24c
ILT
962 uint64_t val = this->val_->eval_maybe_dot(symtab, layout, false,
963 is_dot_available, dot_value,
964 NULL, &val_section);
77e65537 965 if (val_section != NULL)
a445fddf
ILT
966 return;
967
8851ecca 968 if (parameters->target().get_size() == 32)
a445fddf
ILT
969 {
970#if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
971 Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(this->sym_);
972 ssym->set_value(val);
973#else
974 gold_unreachable();
975#endif
976 }
8851ecca 977 else if (parameters->target().get_size() == 64)
a445fddf
ILT
978 {
979#if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
980 Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(this->sym_);
981 ssym->set_value(val);
982#else
983 gold_unreachable();
984#endif
985 }
986 else
987 gold_unreachable();
494e05f4
ILT
988}
989
990// Print for debugging.
991
992void
993Symbol_assignment::print(FILE* f) const
994{
995 if (this->provide_ && this->hidden_)
996 fprintf(f, "PROVIDE_HIDDEN(");
997 else if (this->provide_)
998 fprintf(f, "PROVIDE(");
999 else if (this->hidden_)
1000 gold_unreachable();
1001
1002 fprintf(f, "%s = ", this->name_.c_str());
1003 this->val_->print(f);
1004
1005 if (this->provide_ || this->hidden_)
1006 fprintf(f, ")");
1007
1008 fprintf(f, "\n");
1009}
1010
1011// Class Script_assertion.
1012
1013// Check the assertion.
1014
1015void
1016Script_assertion::check(const Symbol_table* symtab, const Layout* layout)
1017{
919ed24c 1018 if (!this->check_->eval(symtab, layout, true))
494e05f4
ILT
1019 gold_error("%s", this->message_.c_str());
1020}
1021
1022// Print for debugging.
1023
1024void
1025Script_assertion::print(FILE* f) const
1026{
1027 fprintf(f, "ASSERT(");
1028 this->check_->print(f);
1029 fprintf(f, ", \"%s\")\n", this->message_.c_str());
1030}
1031
1032// Class Script_options.
1033
1034Script_options::Script_options()
1035 : entry_(), symbol_assignments_(), version_script_info_(),
1036 script_sections_()
1037{
1038}
1039
1040// Add a symbol to be defined.
1041
1042void
1043Script_options::add_symbol_assignment(const char* name, size_t length,
1044 Expression* value, bool provide,
1045 bool hidden)
1046{
a445fddf
ILT
1047 if (length != 1 || name[0] != '.')
1048 {
1049 if (this->script_sections_.in_sections_clause())
1050 this->script_sections_.add_symbol_assignment(name, length, value,
1051 provide, hidden);
1052 else
1053 {
1054 Symbol_assignment* p = new Symbol_assignment(name, length, value,
1055 provide, hidden);
1056 this->symbol_assignments_.push_back(p);
1057 }
1058 }
494e05f4
ILT
1059 else
1060 {
a445fddf
ILT
1061 if (provide || hidden)
1062 gold_error(_("invalid use of PROVIDE for dot symbol"));
1063 if (!this->script_sections_.in_sections_clause())
1064 gold_error(_("invalid assignment to dot outside of SECTIONS"));
1065 else
1066 this->script_sections_.add_dot_assignment(value);
494e05f4
ILT
1067 }
1068}
1069
1070// Add an assertion.
1071
1072void
1073Script_options::add_assertion(Expression* check, const char* message,
1074 size_t messagelen)
1075{
1076 if (this->script_sections_.in_sections_clause())
1077 this->script_sections_.add_assertion(check, message, messagelen);
1078 else
1079 {
1080 Script_assertion* p = new Script_assertion(check, message, messagelen);
1081 this->assertions_.push_back(p);
1082 }
1083}
1084
919ed24c
ILT
1085// Create sections required by any linker scripts.
1086
1087void
1088Script_options::create_script_sections(Layout* layout)
1089{
1090 if (this->saw_sections_clause())
1091 this->script_sections_.create_sections(layout);
1092}
1093
494e05f4
ILT
1094// Add any symbols we are defining to the symbol table.
1095
1096void
9b07f471 1097Script_options::add_symbols_to_table(Symbol_table* symtab)
e5756efb
ILT
1098{
1099 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1100 p != this->symbol_assignments_.end();
1101 ++p)
9b07f471 1102 (*p)->add_to_table(symtab);
a445fddf 1103 this->script_sections_.add_symbols_to_table(symtab);
494e05f4
ILT
1104}
1105
a445fddf 1106// Finalize symbol values. Also check assertions.
494e05f4
ILT
1107
1108void
1109Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
1110{
1111 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1112 p != this->symbol_assignments_.end();
1113 ++p)
1114 (*p)->finalize(symtab, layout);
a445fddf
ILT
1115
1116 for (Assertions::iterator p = this->assertions_.begin();
1117 p != this->assertions_.end();
1118 ++p)
1119 (*p)->check(symtab, layout);
1120
1121 this->script_sections_.finalize_symbols(symtab, layout);
1122}
1123
1124// Set section addresses. We set all the symbols which have absolute
1125// values. Then we let the SECTIONS clause do its thing. This
1126// returns the segment which holds the file header and segment
1127// headers, if any.
1128
1129Output_segment*
1130Script_options::set_section_addresses(Symbol_table* symtab, Layout* layout)
1131{
1132 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1133 p != this->symbol_assignments_.end();
1134 ++p)
77e65537 1135 (*p)->set_if_absolute(symtab, layout, false, 0);
a445fddf
ILT
1136
1137 return this->script_sections_.set_section_addresses(symtab, layout);
e5756efb
ILT
1138}
1139
dbe717ef
ILT
1140// This class holds data passed through the parser to the lexer and to
1141// the parser support functions. This avoids global variables. We
17a1d0a9
ILT
1142// can't use global variables because we need not be called by a
1143// singleton thread.
dbe717ef
ILT
1144
1145class Parser_closure
1146{
1147 public:
1148 Parser_closure(const char* filename,
1149 const Position_dependent_options& posdep_options,
ad2d6943 1150 bool in_group, bool is_in_sysroot,
a0451b38 1151 Command_line* command_line,
e5756efb
ILT
1152 Script_options* script_options,
1153 Lex* lex)
dbe717ef 1154 : filename_(filename), posdep_options_(posdep_options),
a0451b38 1155 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
e5756efb 1156 command_line_(command_line), script_options_(script_options),
09124467 1157 version_script_info_(script_options->version_script_info()),
e5756efb 1158 lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL)
09124467
ILT
1159 {
1160 // We start out processing C symbols in the default lex mode.
1161 language_stack_.push_back("");
1162 lex_mode_stack_.push_back(lex->mode());
1163 }
dbe717ef
ILT
1164
1165 // Return the file name.
1166 const char*
1167 filename() const
1168 { return this->filename_; }
1169
1170 // Return the position dependent options. The caller may modify
1171 // this.
1172 Position_dependent_options&
1173 position_dependent_options()
1174 { return this->posdep_options_; }
1175
1176 // Return whether this script is being run in a group.
1177 bool
1178 in_group() const
1179 { return this->in_group_; }
1180
ad2d6943
ILT
1181 // Return whether this script was found using a directory in the
1182 // sysroot.
1183 bool
1184 is_in_sysroot() const
1185 { return this->is_in_sysroot_; }
1186
a0451b38
ILT
1187 // Returns the Command_line structure passed in at constructor time.
1188 // This value may be NULL. The caller may modify this, which modifies
1189 // the passed-in Command_line object (not a copy).
e5756efb
ILT
1190 Command_line*
1191 command_line()
a0451b38
ILT
1192 { return this->command_line_; }
1193
e5756efb
ILT
1194 // Return the options which may be set by a script.
1195 Script_options*
1196 script_options()
1197 { return this->script_options_; }
dbe717ef 1198
09124467
ILT
1199 // Return the object in which version script information should be stored.
1200 Version_script_info*
1201 version_script()
1202 { return this->version_script_info_; }
1203
2dd3e587 1204 // Return the next token, and advance.
dbe717ef
ILT
1205 const Token*
1206 next_token()
1207 {
e5756efb
ILT
1208 const Token* token = this->lex_->next_token();
1209 this->lineno_ = token->lineno();
1210 this->charpos_ = token->charpos();
1211 return token;
dbe717ef
ILT
1212 }
1213
e5756efb
ILT
1214 // Set a new lexer mode, pushing the current one.
1215 void
1216 push_lex_mode(Lex::Mode mode)
1217 {
1218 this->lex_mode_stack_.push_back(this->lex_->mode());
1219 this->lex_->set_mode(mode);
1220 }
1221
1222 // Pop the lexer mode.
1223 void
1224 pop_lex_mode()
2dd3e587 1225 {
e5756efb
ILT
1226 gold_assert(!this->lex_mode_stack_.empty());
1227 this->lex_->set_mode(this->lex_mode_stack_.back());
1228 this->lex_mode_stack_.pop_back();
2dd3e587
ILT
1229 }
1230
09124467
ILT
1231 // Return the current lexer mode.
1232 Lex::Mode
1233 lex_mode() const
1234 { return this->lex_mode_stack_.back(); }
1235
e5756efb
ILT
1236 // Return the line number of the last token.
1237 int
1238 lineno() const
1239 { return this->lineno_; }
1240
1241 // Return the character position in the line of the last token.
1242 int
1243 charpos() const
1244 { return this->charpos_; }
1245
dbe717ef
ILT
1246 // Return the list of input files, creating it if necessary. This
1247 // is a space leak--we never free the INPUTS_ pointer.
1248 Input_arguments*
1249 inputs()
1250 {
1251 if (this->inputs_ == NULL)
1252 this->inputs_ = new Input_arguments();
1253 return this->inputs_;
1254 }
1255
1256 // Return whether we saw any input files.
1257 bool
1258 saw_inputs() const
1259 { return this->inputs_ != NULL && !this->inputs_->empty(); }
1260
09124467
ILT
1261 // Return the current language being processed in a version script
1262 // (eg, "C++"). The empty string represents unmangled C names.
1263 const std::string&
1264 get_current_language() const
1265 { return this->language_stack_.back(); }
1266
1267 // Push a language onto the stack when entering an extern block.
1268 void push_language(const std::string& lang)
1269 { this->language_stack_.push_back(lang); }
1270
1271 // Pop a language off of the stack when exiting an extern block.
1272 void pop_language()
1273 {
1274 gold_assert(!this->language_stack_.empty());
1275 this->language_stack_.pop_back();
1276 }
1277
dbe717ef
ILT
1278 private:
1279 // The name of the file we are reading.
1280 const char* filename_;
1281 // The position dependent options.
1282 Position_dependent_options posdep_options_;
1283 // Whether we are currently in a --start-group/--end-group.
1284 bool in_group_;
ad2d6943
ILT
1285 // Whether the script was found in a sysrooted directory.
1286 bool is_in_sysroot_;
a0451b38
ILT
1287 // May be NULL if the user chooses not to pass one in.
1288 Command_line* command_line_;
e5756efb
ILT
1289 // Options which may be set from any linker script.
1290 Script_options* script_options_;
09124467
ILT
1291 // Information parsed from a version script.
1292 Version_script_info* version_script_info_;
e5756efb
ILT
1293 // The lexer.
1294 Lex* lex_;
1295 // The line number of the last token returned by next_token.
1296 int lineno_;
1297 // The column number of the last token returned by next_token.
1298 int charpos_;
1299 // A stack of lexer modes.
1300 std::vector<Lex::Mode> lex_mode_stack_;
09124467
ILT
1301 // A stack of which extern/language block we're inside. Can be C++,
1302 // java, or empty for C.
1303 std::vector<std::string> language_stack_;
dbe717ef
ILT
1304 // New input files found to add to the link.
1305 Input_arguments* inputs_;
1306};
1307
1308// FILE was found as an argument on the command line. Try to read it
da769d56 1309// as a script. Return true if the file was handled.
dbe717ef
ILT
1310
1311bool
1312read_input_script(Workqueue* workqueue, const General_options& options,
1313 Symbol_table* symtab, Layout* layout,
17a1d0a9 1314 Dirsearch* dirsearch, Input_objects* input_objects,
dbe717ef
ILT
1315 Input_group* input_group,
1316 const Input_argument* input_argument,
da769d56
ILT
1317 Input_file* input_file, Task_token* next_blocker,
1318 bool* used_next_blocker)
dbe717ef 1319{
da769d56
ILT
1320 *used_next_blocker = false;
1321
e5756efb
ILT
1322 std::string input_string;
1323 Lex::read_file(input_file, &input_string);
1324
1325 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
dbe717ef
ILT
1326
1327 Parser_closure closure(input_file->filename().c_str(),
1328 input_argument->file().options(),
1329 input_group != NULL,
ad2d6943 1330 input_file->is_in_sysroot(),
a0451b38 1331 NULL,
e5756efb
ILT
1332 layout->script_options(),
1333 &lex);
dbe717ef
ILT
1334
1335 if (yyparse(&closure) != 0)
1336 return false;
1337
dbe717ef 1338 if (!closure.saw_inputs())
da769d56 1339 return true;
dbe717ef 1340
da769d56 1341 Task_token* this_blocker = NULL;
dbe717ef
ILT
1342 for (Input_arguments::const_iterator p = closure.inputs()->begin();
1343 p != closure.inputs()->end();
1344 ++p)
1345 {
1346 Task_token* nb;
1347 if (p + 1 == closure.inputs()->end())
1348 nb = next_blocker;
1349 else
1350 {
17a1d0a9 1351 nb = new Task_token(true);
dbe717ef
ILT
1352 nb->add_blocker();
1353 }
da769d56
ILT
1354 workqueue->queue_soon(new Read_symbols(options, input_objects, symtab,
1355 layout, dirsearch, &*p,
1356 input_group, this_blocker, nb));
dbe717ef
ILT
1357 this_blocker = nb;
1358 }
1359
da769d56
ILT
1360 *used_next_blocker = true;
1361
dbe717ef
ILT
1362 return true;
1363}
1364
09124467
ILT
1365// Helper function for read_version_script() and
1366// read_commandline_script(). Processes the given file in the mode
1367// indicated by first_token and lex_mode.
3c2fafa5 1368
09124467
ILT
1369static bool
1370read_script_file(const char* filename, Command_line* cmdline,
1371 int first_token, Lex::Mode lex_mode)
3c2fafa5 1372{
a0451b38
ILT
1373 // TODO: if filename is a relative filename, search for it manually
1374 // using "." + cmdline->options()->search_path() -- not dirsearch.
3c2fafa5
ILT
1375 Dirsearch dirsearch;
1376
17a1d0a9
ILT
1377 // The file locking code wants to record a Task, but we haven't
1378 // started the workqueue yet. This is only for debugging purposes,
1379 // so we invent a fake value.
1380 const Task* task = reinterpret_cast<const Task*>(-1);
1381
b0d8593d
ILT
1382 // We don't want this file to be opened in binary mode.
1383 Position_dependent_options posdep = cmdline->position_dependent_options();
45aa233b
ILT
1384 if (posdep.format() == General_options::OBJECT_FORMAT_BINARY)
1385 posdep.set_format("elf");
b0d8593d 1386 Input_file_argument input_argument(filename, false, "", false, posdep);
3c2fafa5 1387 Input_file input_file(&input_argument);
17a1d0a9 1388 if (!input_file.open(cmdline->options(), dirsearch, task))
3c2fafa5
ILT
1389 return false;
1390
e5756efb
ILT
1391 std::string input_string;
1392 Lex::read_file(&input_file, &input_string);
1393
09124467
ILT
1394 Lex lex(input_string.c_str(), input_string.length(), first_token);
1395 lex.set_mode(lex_mode);
3c2fafa5
ILT
1396
1397 Parser_closure closure(filename,
1398 cmdline->position_dependent_options(),
1399 false,
1400 input_file.is_in_sysroot(),
a0451b38 1401 cmdline,
a5dc0706 1402 &cmdline->script_options(),
e5756efb 1403 &lex);
3c2fafa5
ILT
1404 if (yyparse(&closure) != 0)
1405 {
17a1d0a9 1406 input_file.file().unlock(task);
3c2fafa5
ILT
1407 return false;
1408 }
1409
17a1d0a9 1410 input_file.file().unlock(task);
d391083d
ILT
1411
1412 gold_assert(!closure.saw_inputs());
1413
3c2fafa5
ILT
1414 return true;
1415}
1416
09124467
ILT
1417// FILENAME was found as an argument to --script (-T).
1418// Read it as a script, and execute its contents immediately.
1419
1420bool
1421read_commandline_script(const char* filename, Command_line* cmdline)
1422{
1423 return read_script_file(filename, cmdline,
1424 PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT);
1425}
1426
1427// FILE was found as an argument to --version-script. Read it as a
1428// version script, and store its contents in
1429// cmdline->script_options()->version_script_info().
1430
1431bool
1432read_version_script(const char* filename, Command_line* cmdline)
1433{
1434 return read_script_file(filename, cmdline,
1435 PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT);
1436}
1437
e5756efb
ILT
1438// Implement the --defsym option on the command line. Return true if
1439// all is well.
1440
1441bool
1442Script_options::define_symbol(const char* definition)
1443{
1444 Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1445 lex.set_mode(Lex::EXPRESSION);
1446
1447 // Dummy value.
1448 Position_dependent_options posdep_options;
1449
1450 Parser_closure closure("command line", posdep_options, false, false, NULL,
1451 this, &lex);
1452
1453 if (yyparse(&closure) != 0)
1454 return false;
1455
1456 gold_assert(!closure.saw_inputs());
1457
1458 return true;
1459}
1460
494e05f4
ILT
1461// Print the script to F for debugging.
1462
1463void
1464Script_options::print(FILE* f) const
1465{
1466 fprintf(f, "%s: Dumping linker script\n", program_name);
1467
1468 if (!this->entry_.empty())
1469 fprintf(f, "ENTRY(%s)\n", this->entry_.c_str());
1470
1471 for (Symbol_assignments::const_iterator p =
1472 this->symbol_assignments_.begin();
1473 p != this->symbol_assignments_.end();
1474 ++p)
1475 (*p)->print(f);
1476
1477 for (Assertions::const_iterator p = this->assertions_.begin();
1478 p != this->assertions_.end();
1479 ++p)
1480 (*p)->print(f);
1481
1482 this->script_sections_.print(f);
1483
1484 this->version_script_info_.print(f);
1485}
1486
dbe717ef 1487// Manage mapping from keywords to the codes expected by the bison
09124467
ILT
1488// parser. We construct one global object for each lex mode with
1489// keywords.
dbe717ef
ILT
1490
1491class Keyword_to_parsecode
1492{
1493 public:
1494 // The structure which maps keywords to parsecodes.
1495 struct Keyword_parsecode
1496 {
1497 // Keyword.
1498 const char* keyword;
1499 // Corresponding parsecode.
1500 int parsecode;
1501 };
1502
09124467
ILT
1503 Keyword_to_parsecode(const Keyword_parsecode* keywords,
1504 int keyword_count)
1505 : keyword_parsecodes_(keywords), keyword_count_(keyword_count)
1506 { }
1507
dbe717ef
ILT
1508 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1509 // keyword.
09124467
ILT
1510 int
1511 keyword_to_parsecode(const char* keyword, size_t len) const;
dbe717ef
ILT
1512
1513 private:
09124467
ILT
1514 const Keyword_parsecode* keyword_parsecodes_;
1515 const int keyword_count_;
dbe717ef
ILT
1516};
1517
1518// Mapping from keyword string to keyword parsecode. This array must
1519// be kept in sorted order. Parsecodes are looked up using bsearch.
1520// This array must correspond to the list of parsecodes in yyscript.y.
1521
09124467
ILT
1522static const Keyword_to_parsecode::Keyword_parsecode
1523script_keyword_parsecodes[] =
dbe717ef
ILT
1524{
1525 { "ABSOLUTE", ABSOLUTE },
1526 { "ADDR", ADDR },
1527 { "ALIGN", ALIGN_K },
e5756efb 1528 { "ALIGNOF", ALIGNOF },
dbe717ef
ILT
1529 { "ASSERT", ASSERT_K },
1530 { "AS_NEEDED", AS_NEEDED },
1531 { "AT", AT },
1532 { "BIND", BIND },
1533 { "BLOCK", BLOCK },
1534 { "BYTE", BYTE },
1535 { "CONSTANT", CONSTANT },
1536 { "CONSTRUCTORS", CONSTRUCTORS },
dbe717ef
ILT
1537 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1538 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1539 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1540 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1541 { "DEFINED", DEFINED },
dbe717ef
ILT
1542 { "ENTRY", ENTRY },
1543 { "EXCLUDE_FILE", EXCLUDE_FILE },
1544 { "EXTERN", EXTERN },
1545 { "FILL", FILL },
1546 { "FLOAT", FLOAT },
1547 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1548 { "GROUP", GROUP },
1549 { "HLL", HLL },
1550 { "INCLUDE", INCLUDE },
dbe717ef
ILT
1551 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1552 { "INPUT", INPUT },
1553 { "KEEP", KEEP },
1554 { "LENGTH", LENGTH },
1555 { "LOADADDR", LOADADDR },
1556 { "LONG", LONG },
1557 { "MAP", MAP },
1558 { "MAX", MAX_K },
1559 { "MEMORY", MEMORY },
1560 { "MIN", MIN_K },
1561 { "NEXT", NEXT },
1562 { "NOCROSSREFS", NOCROSSREFS },
1563 { "NOFLOAT", NOFLOAT },
dbe717ef
ILT
1564 { "ONLY_IF_RO", ONLY_IF_RO },
1565 { "ONLY_IF_RW", ONLY_IF_RW },
195e7dc6 1566 { "OPTION", OPTION },
dbe717ef
ILT
1567 { "ORIGIN", ORIGIN },
1568 { "OUTPUT", OUTPUT },
1569 { "OUTPUT_ARCH", OUTPUT_ARCH },
1570 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1571 { "OVERLAY", OVERLAY },
1572 { "PHDRS", PHDRS },
1573 { "PROVIDE", PROVIDE },
1574 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1575 { "QUAD", QUAD },
1576 { "SEARCH_DIR", SEARCH_DIR },
1577 { "SECTIONS", SECTIONS },
1578 { "SEGMENT_START", SEGMENT_START },
1579 { "SHORT", SHORT },
1580 { "SIZEOF", SIZEOF },
1581 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
3802b2dd 1582 { "SORT", SORT_BY_NAME },
dbe717ef
ILT
1583 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1584 { "SORT_BY_NAME", SORT_BY_NAME },
1585 { "SPECIAL", SPECIAL },
1586 { "SQUAD", SQUAD },
1587 { "STARTUP", STARTUP },
1588 { "SUBALIGN", SUBALIGN },
1589 { "SYSLIB", SYSLIB },
1590 { "TARGET", TARGET_K },
1591 { "TRUNCATE", TRUNCATE },
1592 { "VERSION", VERSIONK },
1593 { "global", GLOBAL },
1594 { "l", LENGTH },
1595 { "len", LENGTH },
1596 { "local", LOCAL },
1597 { "o", ORIGIN },
1598 { "org", ORIGIN },
1599 { "sizeof_headers", SIZEOF_HEADERS },
1600};
1601
09124467
ILT
1602static const Keyword_to_parsecode
1603script_keywords(&script_keyword_parsecodes[0],
1604 (sizeof(script_keyword_parsecodes)
1605 / sizeof(script_keyword_parsecodes[0])));
1606
1607static const Keyword_to_parsecode::Keyword_parsecode
1608version_script_keyword_parsecodes[] =
1609{
1610 { "extern", EXTERN },
1611 { "global", GLOBAL },
1612 { "local", LOCAL },
1613};
1614
1615static const Keyword_to_parsecode
1616version_script_keywords(&version_script_keyword_parsecodes[0],
1617 (sizeof(version_script_keyword_parsecodes)
1618 / sizeof(version_script_keyword_parsecodes[0])));
dbe717ef
ILT
1619
1620// Comparison function passed to bsearch.
1621
1622extern "C"
1623{
1624
e5756efb
ILT
1625struct Ktt_key
1626{
1627 const char* str;
1628 size_t len;
1629};
1630
dbe717ef
ILT
1631static int
1632ktt_compare(const void* keyv, const void* kttv)
1633{
e5756efb 1634 const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
dbe717ef
ILT
1635 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1636 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
e5756efb
ILT
1637 int i = strncmp(key->str, ktt->keyword, key->len);
1638 if (i != 0)
1639 return i;
1640 if (ktt->keyword[key->len] != '\0')
1641 return -1;
1642 return 0;
dbe717ef
ILT
1643}
1644
1645} // End extern "C".
1646
1647int
09124467
ILT
1648Keyword_to_parsecode::keyword_to_parsecode(const char* keyword,
1649 size_t len) const
dbe717ef 1650{
e5756efb
ILT
1651 Ktt_key key;
1652 key.str = keyword;
1653 key.len = len;
1654 void* kttv = bsearch(&key,
09124467
ILT
1655 this->keyword_parsecodes_,
1656 this->keyword_count_,
1657 sizeof(this->keyword_parsecodes_[0]),
1658 ktt_compare);
dbe717ef
ILT
1659 if (kttv == NULL)
1660 return 0;
1661 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1662 return ktt->parsecode;
1663}
1664
494e05f4
ILT
1665// The following structs are used within the VersionInfo class as well
1666// as in the bison helper functions. They store the information
1667// parsed from the version script.
dbe717ef 1668
494e05f4
ILT
1669// A single version expression.
1670// For example, pattern="std::map*" and language="C++".
1671// pattern and language should be from the stringpool
1672struct Version_expression {
1673 Version_expression(const std::string& pattern,
1674 const std::string& language,
1675 bool exact_match)
1676 : pattern(pattern), language(language), exact_match(exact_match) {}
dbe717ef 1677
494e05f4
ILT
1678 std::string pattern;
1679 std::string language;
1680 // If false, we use glob() to match pattern. If true, we use strcmp().
1681 bool exact_match;
1682};
dbe717ef 1683
dbe717ef 1684
494e05f4
ILT
1685// A list of expressions.
1686struct Version_expression_list {
1687 std::vector<struct Version_expression> expressions;
1688};
e5756efb 1689
e5756efb 1690
494e05f4
ILT
1691// A list of which versions upon which another version depends.
1692// Strings should be from the Stringpool.
1693struct Version_dependency_list {
1694 std::vector<std::string> dependencies;
1695};
dbe717ef 1696
dbe717ef 1697
494e05f4
ILT
1698// The total definition of a version. It includes the tag for the
1699// version, its global and local expressions, and any dependencies.
1700struct Version_tree {
1701 Version_tree()
1702 : tag(), global(NULL), local(NULL), dependencies(NULL) {}
e5756efb 1703
494e05f4
ILT
1704 std::string tag;
1705 const struct Version_expression_list* global;
1706 const struct Version_expression_list* local;
1707 const struct Version_dependency_list* dependencies;
1708};
dbe717ef 1709
494e05f4 1710Version_script_info::~Version_script_info()
1ef1f3d3
ILT
1711{
1712 this->clear();
1713}
1714
1715void
1716Version_script_info::clear()
494e05f4
ILT
1717{
1718 for (size_t k = 0; k < dependency_lists_.size(); ++k)
1719 delete dependency_lists_[k];
1ef1f3d3 1720 this->dependency_lists_.clear();
494e05f4
ILT
1721 for (size_t k = 0; k < version_trees_.size(); ++k)
1722 delete version_trees_[k];
1ef1f3d3 1723 this->version_trees_.clear();
494e05f4
ILT
1724 for (size_t k = 0; k < expression_lists_.size(); ++k)
1725 delete expression_lists_[k];
1ef1f3d3 1726 this->expression_lists_.clear();
dbe717ef
ILT
1727}
1728
494e05f4
ILT
1729std::vector<std::string>
1730Version_script_info::get_versions() const
dbe717ef 1731{
494e05f4
ILT
1732 std::vector<std::string> ret;
1733 for (size_t j = 0; j < version_trees_.size(); ++j)
1734 ret.push_back(version_trees_[j]->tag);
1735 return ret;
dbe717ef
ILT
1736}
1737
494e05f4
ILT
1738std::vector<std::string>
1739Version_script_info::get_dependencies(const char* version) const
dbe717ef 1740{
494e05f4
ILT
1741 std::vector<std::string> ret;
1742 for (size_t j = 0; j < version_trees_.size(); ++j)
1743 if (version_trees_[j]->tag == version)
1744 {
1745 const struct Version_dependency_list* deps =
1746 version_trees_[j]->dependencies;
1747 if (deps != NULL)
1748 for (size_t k = 0; k < deps->dependencies.size(); ++k)
1749 ret.push_back(deps->dependencies[k]);
1750 return ret;
1751 }
1752 return ret;
1753}
1754
1755const std::string&
1756Version_script_info::get_symbol_version_helper(const char* symbol_name,
1757 bool check_global) const
1758{
1759 for (size_t j = 0; j < version_trees_.size(); ++j)
1760 {
1761 // Is it a global symbol for this version?
1762 const Version_expression_list* explist =
1763 check_global ? version_trees_[j]->global : version_trees_[j]->local;
1764 if (explist != NULL)
1765 for (size_t k = 0; k < explist->expressions.size(); ++k)
1766 {
1767 const char* name_to_match = symbol_name;
1768 const struct Version_expression& exp = explist->expressions[k];
1769 char* demangled_name = NULL;
1770 if (exp.language == "C++")
1771 {
1772 demangled_name = cplus_demangle(symbol_name,
1773 DMGL_ANSI | DMGL_PARAMS);
1774 // This isn't a C++ symbol.
1775 if (demangled_name == NULL)
1776 continue;
1777 name_to_match = demangled_name;
1778 }
1779 else if (exp.language == "Java")
1780 {
1781 demangled_name = cplus_demangle(symbol_name,
1782 (DMGL_ANSI | DMGL_PARAMS
1783 | DMGL_JAVA));
1784 // This isn't a Java symbol.
1785 if (demangled_name == NULL)
1786 continue;
1787 name_to_match = demangled_name;
1788 }
1789 bool matched;
1790 if (exp.exact_match)
1791 matched = strcmp(exp.pattern.c_str(), name_to_match) == 0;
1792 else
1793 matched = fnmatch(exp.pattern.c_str(), name_to_match,
1794 FNM_NOESCAPE) == 0;
1795 if (demangled_name != NULL)
1796 free(demangled_name);
1797 if (matched)
1798 return version_trees_[j]->tag;
1799 }
1800 }
1801 static const std::string empty = "";
1802 return empty;
1803}
1804
1805struct Version_dependency_list*
1806Version_script_info::allocate_dependency_list()
1807{
1808 dependency_lists_.push_back(new Version_dependency_list);
1809 return dependency_lists_.back();
1810}
1811
1812struct Version_expression_list*
1813Version_script_info::allocate_expression_list()
1814{
1815 expression_lists_.push_back(new Version_expression_list);
1816 return expression_lists_.back();
1817}
1818
1819struct Version_tree*
1820Version_script_info::allocate_version_tree()
1821{
1822 version_trees_.push_back(new Version_tree);
1823 return version_trees_.back();
1824}
1825
1826// Print for debugging.
1827
1828void
1829Version_script_info::print(FILE* f) const
1830{
1831 if (this->empty())
1832 return;
1833
1834 fprintf(f, "VERSION {");
1835
1836 for (size_t i = 0; i < this->version_trees_.size(); ++i)
1837 {
1838 const Version_tree* vt = this->version_trees_[i];
1839
1840 if (vt->tag.empty())
1841 fprintf(f, " {\n");
1842 else
1843 fprintf(f, " %s {\n", vt->tag.c_str());
1844
1845 if (vt->global != NULL)
1846 {
1847 fprintf(f, " global :\n");
1848 this->print_expression_list(f, vt->global);
1849 }
1850
1851 if (vt->local != NULL)
1852 {
1853 fprintf(f, " local :\n");
1854 this->print_expression_list(f, vt->local);
1855 }
1856
1857 fprintf(f, " }");
1858 if (vt->dependencies != NULL)
1859 {
1860 const Version_dependency_list* deps = vt->dependencies;
1861 for (size_t j = 0; j < deps->dependencies.size(); ++j)
1862 {
1863 if (j < deps->dependencies.size() - 1)
1864 fprintf(f, "\n");
1865 fprintf(f, " %s", deps->dependencies[j].c_str());
1866 }
1867 }
1868 fprintf(f, ";\n");
1869 }
1870
1871 fprintf(f, "}\n");
1872}
1873
1874void
1875Version_script_info::print_expression_list(
1876 FILE* f,
1877 const Version_expression_list* vel) const
1878{
1879 std::string current_language;
1880 for (size_t i = 0; i < vel->expressions.size(); ++i)
1881 {
1882 const Version_expression& ve(vel->expressions[i]);
1883
1884 if (ve.language != current_language)
1885 {
1886 if (!current_language.empty())
1887 fprintf(f, " }\n");
1888 fprintf(f, " extern \"%s\" {\n", ve.language.c_str());
1889 current_language = ve.language;
1890 }
1891
1892 fprintf(f, " ");
1893 if (!current_language.empty())
1894 fprintf(f, " ");
1895
1896 if (ve.exact_match)
1897 fprintf(f, "\"");
1898 fprintf(f, "%s", ve.pattern.c_str());
1899 if (ve.exact_match)
1900 fprintf(f, "\"");
1901
1902 fprintf(f, "\n");
1903 }
1904
1905 if (!current_language.empty())
1906 fprintf(f, " }\n");
1907}
1908
1909} // End namespace gold.
1910
1911// The remaining functions are extern "C", so it's clearer to not put
1912// them in namespace gold.
1913
1914using namespace gold;
1915
1916// This function is called by the bison parser to return the next
1917// token.
1918
1919extern "C" int
1920yylex(YYSTYPE* lvalp, void* closurev)
1921{
1922 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1923 const Token* token = closure->next_token();
1924 switch (token->classification())
1925 {
1926 default:
1927 gold_unreachable();
1928
1929 case Token::TOKEN_INVALID:
1930 yyerror(closurev, "invalid character");
1931 return 0;
1932
1933 case Token::TOKEN_EOF:
1934 return 0;
1935
1936 case Token::TOKEN_STRING:
1937 {
1938 // This is either a keyword or a STRING.
1939 size_t len;
1940 const char* str = token->string_value(&len);
1941 int parsecode = 0;
1942 switch (closure->lex_mode())
1943 {
1944 case Lex::LINKER_SCRIPT:
1945 parsecode = script_keywords.keyword_to_parsecode(str, len);
1946 break;
1947 case Lex::VERSION_SCRIPT:
1948 parsecode = version_script_keywords.keyword_to_parsecode(str, len);
1949 break;
1950 default:
1951 break;
1952 }
1953 if (parsecode != 0)
1954 return parsecode;
1955 lvalp->string.value = str;
1956 lvalp->string.length = len;
1957 return STRING;
1958 }
1959
1960 case Token::TOKEN_QUOTED_STRING:
1961 lvalp->string.value = token->string_value(&lvalp->string.length);
1962 return QUOTED_STRING;
1963
1964 case Token::TOKEN_OPERATOR:
1965 return token->operator_value();
1966
1967 case Token::TOKEN_INTEGER:
1968 lvalp->integer = token->integer_value();
1969 return INTEGER;
1970 }
1971}
1972
1973// This function is called by the bison parser to report an error.
1974
1975extern "C" void
1976yyerror(void* closurev, const char* message)
1977{
1978 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1979 gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
1980 closure->charpos(), message);
1981}
1982
1983// Called by the bison parser to add a file to the link.
1984
1985extern "C" void
1986script_add_file(void* closurev, const char* name, size_t length)
1987{
1988 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1989
1990 // If this is an absolute path, and we found the script in the
1991 // sysroot, then we want to prepend the sysroot to the file name.
1992 // For example, this is how we handle a cross link to the x86_64
1993 // libc.so, which refers to /lib/libc.so.6.
1994 std::string name_string(name, length);
1995 const char* extra_search_path = ".";
1996 std::string script_directory;
1997 if (IS_ABSOLUTE_PATH(name_string.c_str()))
1998 {
1999 if (closure->is_in_sysroot())
2000 {
8851ecca 2001 const std::string& sysroot(parameters->options().sysroot());
494e05f4
ILT
2002 gold_assert(!sysroot.empty());
2003 name_string = sysroot + name_string;
2004 }
2005 }
2006 else
2007 {
2008 // In addition to checking the normal library search path, we
2009 // also want to check in the script-directory.
2010 const char *slash = strrchr(closure->filename(), '/');
2011 if (slash != NULL)
2012 {
2013 script_directory.assign(closure->filename(),
2014 slash - closure->filename() + 1);
2015 extra_search_path = script_directory.c_str();
2016 }
2017 }
2018
2019 Input_file_argument file(name_string.c_str(), false, extra_search_path,
88dd47ac 2020 false, closure->position_dependent_options());
494e05f4 2021 closure->inputs()->add_file(file);
dbe717ef
ILT
2022}
2023
2024// Called by the bison parser to start a group. If we are already in
2025// a group, that means that this script was invoked within a
2026// --start-group --end-group sequence on the command line, or that
2027// this script was found in a GROUP of another script. In that case,
2028// we simply continue the existing group, rather than starting a new
2029// one. It is possible to construct a case in which this will do
2030// something other than what would happen if we did a recursive group,
2031// but it's hard to imagine why the different behaviour would be
2032// useful for a real program. Avoiding recursive groups is simpler
2033// and more efficient.
2034
2035extern "C" void
2036script_start_group(void* closurev)
2037{
2038 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2039 if (!closure->in_group())
2040 closure->inputs()->start_group();
2041}
2042
2043// Called by the bison parser at the end of a group.
2044
2045extern "C" void
2046script_end_group(void* closurev)
2047{
2048 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2049 if (!closure->in_group())
2050 closure->inputs()->end_group();
2051}
2052
2053// Called by the bison parser to start an AS_NEEDED list.
2054
2055extern "C" void
2056script_start_as_needed(void* closurev)
2057{
2058 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
45aa233b 2059 closure->position_dependent_options().set_as_needed(true);
dbe717ef
ILT
2060}
2061
2062// Called by the bison parser at the end of an AS_NEEDED list.
2063
2064extern "C" void
2065script_end_as_needed(void* closurev)
2066{
2067 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
45aa233b 2068 closure->position_dependent_options().set_as_needed(false);
dbe717ef 2069}
195e7dc6 2070
d391083d
ILT
2071// Called by the bison parser to set the entry symbol.
2072
2073extern "C" void
e5756efb 2074script_set_entry(void* closurev, const char* entry, size_t length)
d391083d 2075{
a5dc0706
ILT
2076 // We'll parse this exactly the same as --entry=ENTRY on the commandline
2077 // TODO(csilvers): FIXME -- call set_entry directly.
1890b465 2078 std::string arg("--entry=");
a5dc0706
ILT
2079 arg.append(entry, length);
2080 script_parse_option(closurev, arg.c_str(), arg.size());
e5756efb
ILT
2081}
2082
2083// Called by the bison parser to define a symbol.
2084
2085extern "C" void
2086script_set_symbol(void* closurev, const char* name, size_t length,
2087 Expression* value, int providei, int hiddeni)
2088{
2089 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2090 const bool provide = providei != 0;
2091 const bool hidden = hiddeni != 0;
2092 closure->script_options()->add_symbol_assignment(name, length, value,
2093 provide, hidden);
d391083d
ILT
2094}
2095
494e05f4
ILT
2096// Called by the bison parser to add an assertion.
2097
2098extern "C" void
2099script_add_assertion(void* closurev, Expression* check, const char* message,
2100 size_t messagelen)
2101{
2102 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2103 closure->script_options()->add_assertion(check, message, messagelen);
2104}
2105
195e7dc6
ILT
2106// Called by the bison parser to parse an OPTION.
2107
2108extern "C" void
e5756efb 2109script_parse_option(void* closurev, const char* option, size_t length)
195e7dc6
ILT
2110{
2111 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
a0451b38
ILT
2112 // We treat the option as a single command-line option, even if
2113 // it has internal whitespace.
2114 if (closure->command_line() == NULL)
2115 {
2116 // There are some options that we could handle here--e.g.,
2117 // -lLIBRARY. Should we bother?
e5756efb 2118 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
d391083d 2119 " for scripts specified via -T/--script"),
e5756efb 2120 closure->filename(), closure->lineno(), closure->charpos());
a0451b38
ILT
2121 }
2122 else
2123 {
2124 bool past_a_double_dash_option = false;
e5756efb
ILT
2125 char* mutable_option = strndup(option, length);
2126 gold_assert(mutable_option != NULL);
a0451b38
ILT
2127 closure->command_line()->process_one_option(1, &mutable_option, 0,
2128 &past_a_double_dash_option);
a5dc0706
ILT
2129 // The General_options class will quite possibly store a pointer
2130 // into mutable_option, so we can't free it. In cases the class
2131 // does not store such a pointer, this is a memory leak. Alas. :(
a0451b38 2132 }
195e7dc6 2133}
e5756efb 2134
3802b2dd
ILT
2135// Called by the bison parser to handle SEARCH_DIR. This is handled
2136// exactly like a -L option.
2137
2138extern "C" void
2139script_add_search_dir(void* closurev, const char* option, size_t length)
2140{
2141 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2142 if (closure->command_line() == NULL)
2143 gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid"
2144 " for scripts specified via -T/--script"),
2145 closure->filename(), closure->lineno(), closure->charpos());
2146 else
2147 {
2148 std::string s = "-L" + std::string(option, length);
2149 script_parse_option(closurev, s.c_str(), s.size());
2150 }
2151}
2152
e5756efb
ILT
2153/* Called by the bison parser to push the lexer into expression
2154 mode. */
2155
494e05f4 2156extern "C" void
e5756efb
ILT
2157script_push_lex_into_expression_mode(void* closurev)
2158{
2159 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2160 closure->push_lex_mode(Lex::EXPRESSION);
2161}
2162
09124467
ILT
2163/* Called by the bison parser to push the lexer into version
2164 mode. */
2165
494e05f4 2166extern "C" void
09124467
ILT
2167script_push_lex_into_version_mode(void* closurev)
2168{
2169 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2170 closure->push_lex_mode(Lex::VERSION_SCRIPT);
2171}
2172
e5756efb
ILT
2173/* Called by the bison parser to pop the lexer mode. */
2174
494e05f4 2175extern "C" void
e5756efb
ILT
2176script_pop_lex_mode(void* closurev)
2177{
2178 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2179 closure->pop_lex_mode();
2180}
09124467 2181
09124467
ILT
2182// Register an entire version node. For example:
2183//
2184// GLIBC_2.1 {
2185// global: foo;
2186// } GLIBC_2.0;
2187//
2188// - tag is "GLIBC_2.1"
2189// - tree contains the information "global: foo"
2190// - deps contains "GLIBC_2.0"
2191
2192extern "C" void
2193script_register_vers_node(void*,
2194 const char* tag,
2195 int taglen,
2196 struct Version_tree *tree,
2197 struct Version_dependency_list *deps)
2198{
2199 gold_assert(tree != NULL);
2200 gold_assert(tag != NULL);
2201 tree->dependencies = deps;
2202 tree->tag = std::string(tag, taglen);
2203}
2204
2205// Add a dependencies to the list of existing dependencies, if any,
2206// and return the expanded list.
2207
2208extern "C" struct Version_dependency_list *
2209script_add_vers_depend(void* closurev,
2210 struct Version_dependency_list *all_deps,
2211 const char *depend_to_add, int deplen)
2212{
2213 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2214 if (all_deps == NULL)
2215 all_deps = closure->version_script()->allocate_dependency_list();
2216 all_deps->dependencies.push_back(std::string(depend_to_add, deplen));
2217 return all_deps;
2218}
2219
2220// Add a pattern expression to an existing list of expressions, if any.
2221// TODO: In the old linker, the last argument used to be a bool, but I
2222// don't know what it meant.
2223
2224extern "C" struct Version_expression_list *
2225script_new_vers_pattern(void* closurev,
2226 struct Version_expression_list *expressions,
10600224 2227 const char *pattern, int patlen, int exact_match)
09124467
ILT
2228{
2229 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2230 if (expressions == NULL)
2231 expressions = closure->version_script()->allocate_expression_list();
2232 expressions->expressions.push_back(
2233 Version_expression(std::string(pattern, patlen),
10600224
ILT
2234 closure->get_current_language(),
2235 static_cast<bool>(exact_match)));
09124467
ILT
2236 return expressions;
2237}
2238
10600224
ILT
2239// Attaches b to the end of a, and clears b. So a = a + b and b = {}.
2240
2241extern "C" struct Version_expression_list*
2242script_merge_expressions(struct Version_expression_list *a,
2243 struct Version_expression_list *b)
2244{
2245 a->expressions.insert(a->expressions.end(),
2246 b->expressions.begin(), b->expressions.end());
2247 // We could delete b and remove it from expressions_lists_, but
2248 // that's a lot of work. This works just as well.
2249 b->expressions.clear();
2250 return a;
2251}
2252
09124467
ILT
2253// Combine the global and local expressions into a a Version_tree.
2254
2255extern "C" struct Version_tree *
2256script_new_vers_node(void* closurev,
2257 struct Version_expression_list *global,
2258 struct Version_expression_list *local)
2259{
2260 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2261 Version_tree* tree = closure->version_script()->allocate_version_tree();
2262 tree->global = global;
2263 tree->local = local;
2264 return tree;
2265}
2266
10600224 2267// Handle a transition in language, such as at the
09124467
ILT
2268// start or end of 'extern "C++"'
2269
2270extern "C" void
2271version_script_push_lang(void* closurev, const char* lang, int langlen)
2272{
2273 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2274 closure->push_language(std::string(lang, langlen));
2275}
2276
2277extern "C" void
2278version_script_pop_lang(void* closurev)
2279{
2280 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2281 closure->pop_language();
2282}
494e05f4
ILT
2283
2284// Called by the bison parser to start a SECTIONS clause.
2285
2286extern "C" void
2287script_start_sections(void* closurev)
2288{
2289 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2290 closure->script_options()->script_sections()->start_sections();
2291}
2292
2293// Called by the bison parser to finish a SECTIONS clause.
2294
2295extern "C" void
2296script_finish_sections(void* closurev)
2297{
2298 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2299 closure->script_options()->script_sections()->finish_sections();
2300}
2301
2302// Start processing entries for an output section.
2303
2304extern "C" void
2305script_start_output_section(void* closurev, const char* name, size_t namelen,
2306 const struct Parser_output_section_header* header)
2307{
2308 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2309 closure->script_options()->script_sections()->start_output_section(name,
2310 namelen,
2311 header);
2312}
2313
2314// Finish processing entries for an output section.
2315
2316extern "C" void
2317script_finish_output_section(void* closurev,
2318 const struct Parser_output_section_trailer* trail)
2319{
2320 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2321 closure->script_options()->script_sections()->finish_output_section(trail);
2322}
2323
2324// Add a data item (e.g., "WORD (0)") to the current output section.
2325
2326extern "C" void
2327script_add_data(void* closurev, int data_token, Expression* val)
2328{
2329 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2330 int size;
2331 bool is_signed = true;
2332 switch (data_token)
2333 {
2334 case QUAD:
2335 size = 8;
2336 is_signed = false;
2337 break;
2338 case SQUAD:
2339 size = 8;
2340 break;
2341 case LONG:
2342 size = 4;
2343 break;
2344 case SHORT:
2345 size = 2;
2346 break;
2347 case BYTE:
2348 size = 1;
2349 break;
2350 default:
2351 gold_unreachable();
2352 }
2353 closure->script_options()->script_sections()->add_data(size, is_signed, val);
2354}
2355
2356// Add a clause setting the fill value to the current output section.
2357
2358extern "C" void
2359script_add_fill(void* closurev, Expression* val)
2360{
2361 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2362 closure->script_options()->script_sections()->add_fill(val);
2363}
2364
2365// Add a new input section specification to the current output
2366// section.
2367
2368extern "C" void
2369script_add_input_section(void* closurev,
2370 const struct Input_section_spec* spec,
2371 int keepi)
2372{
2373 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2374 bool keep = keepi != 0;
2375 closure->script_options()->script_sections()->add_input_section(spec, keep);
2376}
2377
2378// Create a new list of string/sort pairs.
2379
2380extern "C" String_sort_list_ptr
2381script_new_string_sort_list(const struct Wildcard_section* string_sort)
2382{
2383 return new String_sort_list(1, *string_sort);
2384}
2385
2386// Add an entry to a list of string/sort pairs. The way the parser
2387// works permits us to simply modify the first parameter, rather than
2388// copy the vector.
2389
2390extern "C" String_sort_list_ptr
2391script_string_sort_list_add(String_sort_list_ptr pv,
2392 const struct Wildcard_section* string_sort)
2393{
a445fddf
ILT
2394 if (pv == NULL)
2395 return script_new_string_sort_list(string_sort);
2396 else
2397 {
2398 pv->push_back(*string_sort);
2399 return pv;
2400 }
494e05f4
ILT
2401}
2402
2403// Create a new list of strings.
2404
2405extern "C" String_list_ptr
2406script_new_string_list(const char* str, size_t len)
2407{
2408 return new String_list(1, std::string(str, len));
2409}
2410
2411// Add an element to a list of strings. The way the parser works
2412// permits us to simply modify the first parameter, rather than copy
2413// the vector.
2414
2415extern "C" String_list_ptr
2416script_string_list_push_back(String_list_ptr pv, const char* str, size_t len)
2417{
1c4f3631
ILT
2418 if (pv == NULL)
2419 return script_new_string_list(str, len);
2420 else
2421 {
2422 pv->push_back(std::string(str, len));
2423 return pv;
2424 }
494e05f4
ILT
2425}
2426
2427// Concatenate two string lists. Either or both may be NULL. The way
2428// the parser works permits us to modify the parameters, rather than
2429// copy the vector.
2430
2431extern "C" String_list_ptr
2432script_string_list_append(String_list_ptr pv1, String_list_ptr pv2)
2433{
2434 if (pv1 == NULL)
2435 return pv2;
2436 if (pv2 == NULL)
2437 return pv1;
2438 pv1->insert(pv1->end(), pv2->begin(), pv2->end());
2439 return pv1;
2440}
1c4f3631
ILT
2441
2442// Add a new program header.
2443
2444extern "C" void
2445script_add_phdr(void* closurev, const char* name, size_t namelen,
2446 unsigned int type, const Phdr_info* info)
2447{
2448 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2449 bool includes_filehdr = info->includes_filehdr != 0;
2450 bool includes_phdrs = info->includes_phdrs != 0;
2451 bool is_flags_valid = info->is_flags_valid != 0;
2452 Script_sections* ss = closure->script_options()->script_sections();
2453 ss->add_phdr(name, namelen, type, includes_filehdr, includes_phdrs,
2454 is_flags_valid, info->flags, info->load_address);
2455}
2456
2457// Convert a program header string to a type.
2458
2459#define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME }
2460
2461static struct
2462{
2463 const char* name;
2464 size_t namelen;
2465 unsigned int val;
2466} phdr_type_names[] =
2467{
2468 PHDR_TYPE(PT_NULL),
2469 PHDR_TYPE(PT_LOAD),
2470 PHDR_TYPE(PT_DYNAMIC),
2471 PHDR_TYPE(PT_INTERP),
2472 PHDR_TYPE(PT_NOTE),
2473 PHDR_TYPE(PT_SHLIB),
2474 PHDR_TYPE(PT_PHDR),
2475 PHDR_TYPE(PT_TLS),
2476 PHDR_TYPE(PT_GNU_EH_FRAME),
2477 PHDR_TYPE(PT_GNU_STACK),
2478 PHDR_TYPE(PT_GNU_RELRO)
2479};
2480
2481extern "C" unsigned int
2482script_phdr_string_to_type(void* closurev, const char* name, size_t namelen)
2483{
2484 for (unsigned int i = 0;
2485 i < sizeof(phdr_type_names) / sizeof(phdr_type_names[0]);
2486 ++i)
2487 if (namelen == phdr_type_names[i].namelen
2488 && strncmp(name, phdr_type_names[i].name, namelen) == 0)
2489 return phdr_type_names[i].val;
2490 yyerror(closurev, _("unknown PHDR type (try integer)"));
2491 return elfcpp::PT_NULL;
2492}
This page took 0.178418 seconds and 4 git commands to generate.