Support assignments and expressions in linker scripts.
[deliverable/binutils-gdb.git] / gold / script.cc
1 // script.cc -- handle linker scripts for gold.
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
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
23 #include "gold.h"
24
25 #include <string>
26 #include <vector>
27 #include <cstdio>
28 #include <cstdlib>
29 #include "filenames.h"
30
31 #include "elfcpp.h"
32 #include "dirsearch.h"
33 #include "options.h"
34 #include "fileread.h"
35 #include "workqueue.h"
36 #include "readsyms.h"
37 #include "parameters.h"
38 #include "layout.h"
39 #include "symtab.h"
40 #include "script.h"
41 #include "script-c.h"
42
43 namespace gold
44 {
45
46 // A token read from a script file. We don't implement keywords here;
47 // all keywords are simply represented as a string.
48
49 class Token
50 {
51 public:
52 // Token classification.
53 enum Classification
54 {
55 // Token is invalid.
56 TOKEN_INVALID,
57 // Token indicates end of input.
58 TOKEN_EOF,
59 // Token is a string of characters.
60 TOKEN_STRING,
61 // Token is a quoted string of characters.
62 TOKEN_QUOTED_STRING,
63 // Token is an operator.
64 TOKEN_OPERATOR,
65 // Token is a number (an integer).
66 TOKEN_INTEGER
67 };
68
69 // We need an empty constructor so that we can put this STL objects.
70 Token()
71 : classification_(TOKEN_INVALID), value_(NULL), value_length_(0),
72 opcode_(0), lineno_(0), charpos_(0)
73 { }
74
75 // A general token with no value.
76 Token(Classification classification, int lineno, int charpos)
77 : classification_(classification), value_(NULL), value_length_(0),
78 opcode_(0), lineno_(lineno), charpos_(charpos)
79 {
80 gold_assert(classification == TOKEN_INVALID
81 || classification == TOKEN_EOF);
82 }
83
84 // A general token with a value.
85 Token(Classification classification, const char* value, size_t length,
86 int lineno, int charpos)
87 : classification_(classification), value_(value), value_length_(length),
88 opcode_(0), lineno_(lineno), charpos_(charpos)
89 {
90 gold_assert(classification != TOKEN_INVALID
91 && classification != TOKEN_EOF);
92 }
93
94 // A token representing an operator.
95 Token(int opcode, int lineno, int charpos)
96 : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0),
97 opcode_(opcode), lineno_(lineno), charpos_(charpos)
98 { }
99
100 // Return whether the token is invalid.
101 bool
102 is_invalid() const
103 { return this->classification_ == TOKEN_INVALID; }
104
105 // Return whether this is an EOF token.
106 bool
107 is_eof() const
108 { return this->classification_ == TOKEN_EOF; }
109
110 // Return the token classification.
111 Classification
112 classification() const
113 { return this->classification_; }
114
115 // Return the line number at which the token starts.
116 int
117 lineno() const
118 { return this->lineno_; }
119
120 // Return the character position at this the token starts.
121 int
122 charpos() const
123 { return this->charpos_; }
124
125 // Get the value of a token.
126
127 const char*
128 string_value(size_t* length) const
129 {
130 gold_assert(this->classification_ == TOKEN_STRING
131 || this->classification_ == TOKEN_QUOTED_STRING);
132 *length = this->value_length_;
133 return this->value_;
134 }
135
136 int
137 operator_value() const
138 {
139 gold_assert(this->classification_ == TOKEN_OPERATOR);
140 return this->opcode_;
141 }
142
143 uint64_t
144 integer_value() const
145 {
146 gold_assert(this->classification_ == TOKEN_INTEGER);
147 // Null terminate.
148 std::string s(this->value_, this->value_length_);
149 return strtoull(s.c_str(), NULL, 0);
150 }
151
152 private:
153 // The token classification.
154 Classification classification_;
155 // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or
156 // TOKEN_INTEGER.
157 const char* value_;
158 // The length of the token value.
159 size_t value_length_;
160 // The token value, for TOKEN_OPERATOR.
161 int opcode_;
162 // The line number where this token started (one based).
163 int lineno_;
164 // The character position within the line where this token started
165 // (one based).
166 int charpos_;
167 };
168
169 // This class handles lexing a file into a sequence of tokens.
170
171 class Lex
172 {
173 public:
174 // We unfortunately have to support different lexing modes, because
175 // when reading different parts of a linker script we need to parse
176 // things differently.
177 enum Mode
178 {
179 // Reading an ordinary linker script.
180 LINKER_SCRIPT,
181 // Reading an expression in a linker script.
182 EXPRESSION,
183 // Reading a version script.
184 VERSION_SCRIPT
185 };
186
187 Lex(const char* input_string, size_t input_length, int parsing_token)
188 : input_string_(input_string), input_length_(input_length),
189 current_(input_string), mode_(LINKER_SCRIPT),
190 first_token_(parsing_token), token_(),
191 lineno_(1), linestart_(input_string)
192 { }
193
194 // Read a file into a string.
195 static void
196 read_file(Input_file*, std::string*);
197
198 // Return the next token.
199 const Token*
200 next_token();
201
202 // Return the current lexing mode.
203 Lex::Mode
204 mode() const
205 { return this->mode_; }
206
207 // Set the lexing mode.
208 void
209 set_mode(Mode mode)
210 { this->mode_ = mode; }
211
212 private:
213 Lex(const Lex&);
214 Lex& operator=(const Lex&);
215
216 // Make a general token with no value at the current location.
217 Token
218 make_token(Token::Classification c, const char* start) const
219 { return Token(c, this->lineno_, start - this->linestart_ + 1); }
220
221 // Make a general token with a value at the current location.
222 Token
223 make_token(Token::Classification c, const char* v, size_t len,
224 const char* start)
225 const
226 { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); }
227
228 // Make an operator token at the current location.
229 Token
230 make_token(int opcode, const char* start) const
231 { return Token(opcode, this->lineno_, start - this->linestart_ + 1); }
232
233 // Make an invalid token at the current location.
234 Token
235 make_invalid_token(const char* start)
236 { return this->make_token(Token::TOKEN_INVALID, start); }
237
238 // Make an EOF token at the current location.
239 Token
240 make_eof_token(const char* start)
241 { return this->make_token(Token::TOKEN_EOF, start); }
242
243 // Return whether C can be the first character in a name. C2 is the
244 // next character, since we sometimes need that.
245 inline bool
246 can_start_name(char c, char c2);
247
248 // Return whether C can appear in a name which has already started.
249 inline bool
250 can_continue_name(char c);
251
252 // Return whether C, C2, C3 can start a hex number.
253 inline bool
254 can_start_hex(char c, char c2, char c3);
255
256 // Return whether C can appear in a hex number.
257 inline bool
258 can_continue_hex(char c);
259
260 // Return whether C can start a non-hex number.
261 static inline bool
262 can_start_number(char c);
263
264 // Return whether C can appear in a non-hex number.
265 inline bool
266 can_continue_number(char c)
267 { return Lex::can_start_number(c); }
268
269 // If C1 C2 C3 form a valid three character operator, return the
270 // opcode. Otherwise return 0.
271 static inline int
272 three_char_operator(char c1, char c2, char c3);
273
274 // If C1 C2 form a valid two character operator, return the opcode.
275 // Otherwise return 0.
276 static inline int
277 two_char_operator(char c1, char c2);
278
279 // If C1 is a valid one character operator, return the opcode.
280 // Otherwise return 0.
281 static inline int
282 one_char_operator(char c1);
283
284 // Read the next token.
285 Token
286 get_token(const char**);
287
288 // Skip a C style /* */ comment. Return false if the comment did
289 // not end.
290 bool
291 skip_c_comment(const char**);
292
293 // Skip a line # comment. Return false if there was no newline.
294 bool
295 skip_line_comment(const char**);
296
297 // Build a token CLASSIFICATION from all characters that match
298 // CAN_CONTINUE_FN. The token starts at START. Start matching from
299 // MATCH. Set *PP to the character following the token.
300 inline Token
301 gather_token(Token::Classification,
302 bool (Lex::*can_continue_fn)(char),
303 const char* start, const char* match, const char** pp);
304
305 // Build a token from a quoted string.
306 Token
307 gather_quoted_string(const char** pp);
308
309 // The string we are tokenizing.
310 const char* input_string_;
311 // The length of the string.
312 size_t input_length_;
313 // The current offset into the string.
314 const char* current_;
315 // The current lexing mode.
316 Mode mode_;
317 // The code to use for the first token. This is set to 0 after it
318 // is used.
319 int first_token_;
320 // The current token.
321 Token token_;
322 // The current line number.
323 int lineno_;
324 // The start of the current line in the string.
325 const char* linestart_;
326 };
327
328 // Read the whole file into memory. We don't expect linker scripts to
329 // be large, so we just use a std::string as a buffer. We ignore the
330 // data we've already read, so that we read aligned buffers.
331
332 void
333 Lex::read_file(Input_file* input_file, std::string* contents)
334 {
335 off_t filesize = input_file->file().filesize();
336 contents->clear();
337 contents->reserve(filesize);
338
339 off_t off = 0;
340 unsigned char buf[BUFSIZ];
341 while (off < filesize)
342 {
343 off_t get = BUFSIZ;
344 if (get > filesize - off)
345 get = filesize - off;
346 input_file->file().read(off, get, buf);
347 contents->append(reinterpret_cast<char*>(&buf[0]), get);
348 off += get;
349 }
350 }
351
352 // Return whether C can be the start of a name, if the next character
353 // is C2. A name can being with a letter, underscore, period, or
354 // dollar sign. Because a name can be a file name, we also permit
355 // forward slash, backslash, and tilde. Tilde is the tricky case
356 // here; GNU ld also uses it as a bitwise not operator. It is only
357 // recognized as the operator if it is not immediately followed by
358 // some character which can appear in a symbol. That is, when we
359 // don't know that we are looking at an expression, "~0" is a file
360 // name, and "~ 0" is an expression using bitwise not. We are
361 // compatible.
362
363 inline bool
364 Lex::can_start_name(char c, char c2)
365 {
366 switch (c)
367 {
368 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
369 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
370 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
371 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
372 case 'Y': case 'Z':
373 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
374 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
375 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
376 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
377 case 'y': case 'z':
378 case '_': case '.': case '$':
379 return true;
380
381 case '/': case '\\':
382 return this->mode_ == LINKER_SCRIPT;
383
384 case '~':
385 return this->mode_ == LINKER_SCRIPT && can_continue_name(c2);
386
387 default:
388 return false;
389 }
390 }
391
392 // Return whether C can continue a name which has already started.
393 // Subsequent characters in a name are the same as the leading
394 // characters, plus digits and "=+-:[],?*". So in general the linker
395 // script language requires spaces around operators, unless we know
396 // that we are parsing an expression.
397
398 inline bool
399 Lex::can_continue_name(char c)
400 {
401 switch (c)
402 {
403 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
404 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
405 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
406 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
407 case 'Y': case 'Z':
408 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
409 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
410 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
411 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
412 case 'y': case 'z':
413 case '_': case '.': case '$':
414 case '0': case '1': case '2': case '3': case '4':
415 case '5': case '6': case '7': case '8': case '9':
416 return true;
417
418 case '/': case '\\': case '~':
419 case '=': case '+': case '-':
420 case ':': case '[': case ']':
421 case ',': case '?': case '*':
422 return this->mode_ == LINKER_SCRIPT;
423
424 default:
425 return false;
426 }
427 }
428
429 // For a number we accept 0x followed by hex digits, or any sequence
430 // of digits. The old linker accepts leading '$' for hex, and
431 // trailing HXBOD. Those are for MRI compatibility and we don't
432 // accept them. The old linker also accepts trailing MK for mega or
433 // kilo. FIXME: Those are mentioned in the documentation, and we
434 // should accept them.
435
436 // Return whether C1 C2 C3 can start a hex number.
437
438 inline bool
439 Lex::can_start_hex(char c1, char c2, char c3)
440 {
441 if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
442 return this->can_continue_hex(c3);
443 return false;
444 }
445
446 // Return whether C can appear in a hex number.
447
448 inline bool
449 Lex::can_continue_hex(char c)
450 {
451 switch (c)
452 {
453 case '0': case '1': case '2': case '3': case '4':
454 case '5': case '6': case '7': case '8': case '9':
455 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
456 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
457 return true;
458
459 default:
460 return false;
461 }
462 }
463
464 // Return whether C can start a non-hex number.
465
466 inline bool
467 Lex::can_start_number(char c)
468 {
469 switch (c)
470 {
471 case '0': case '1': case '2': case '3': case '4':
472 case '5': case '6': case '7': case '8': case '9':
473 return true;
474
475 default:
476 return false;
477 }
478 }
479
480 // If C1 C2 C3 form a valid three character operator, return the
481 // opcode (defined in the yyscript.h file generated from yyscript.y).
482 // Otherwise return 0.
483
484 inline int
485 Lex::three_char_operator(char c1, char c2, char c3)
486 {
487 switch (c1)
488 {
489 case '<':
490 if (c2 == '<' && c3 == '=')
491 return LSHIFTEQ;
492 break;
493 case '>':
494 if (c2 == '>' && c3 == '=')
495 return RSHIFTEQ;
496 break;
497 default:
498 break;
499 }
500 return 0;
501 }
502
503 // If C1 C2 form a valid two character operator, return the opcode
504 // (defined in the yyscript.h file generated from yyscript.y).
505 // Otherwise return 0.
506
507 inline int
508 Lex::two_char_operator(char c1, char c2)
509 {
510 switch (c1)
511 {
512 case '=':
513 if (c2 == '=')
514 return EQ;
515 break;
516 case '!':
517 if (c2 == '=')
518 return NE;
519 break;
520 case '+':
521 if (c2 == '=')
522 return PLUSEQ;
523 break;
524 case '-':
525 if (c2 == '=')
526 return MINUSEQ;
527 break;
528 case '*':
529 if (c2 == '=')
530 return MULTEQ;
531 break;
532 case '/':
533 if (c2 == '=')
534 return DIVEQ;
535 break;
536 case '|':
537 if (c2 == '=')
538 return OREQ;
539 if (c2 == '|')
540 return OROR;
541 break;
542 case '&':
543 if (c2 == '=')
544 return ANDEQ;
545 if (c2 == '&')
546 return ANDAND;
547 break;
548 case '>':
549 if (c2 == '=')
550 return GE;
551 if (c2 == '>')
552 return RSHIFT;
553 break;
554 case '<':
555 if (c2 == '=')
556 return LE;
557 if (c2 == '<')
558 return LSHIFT;
559 break;
560 default:
561 break;
562 }
563 return 0;
564 }
565
566 // If C1 is a valid operator, return the opcode. Otherwise return 0.
567
568 inline int
569 Lex::one_char_operator(char c1)
570 {
571 switch (c1)
572 {
573 case '+':
574 case '-':
575 case '*':
576 case '/':
577 case '%':
578 case '!':
579 case '&':
580 case '|':
581 case '^':
582 case '~':
583 case '<':
584 case '>':
585 case '=':
586 case '?':
587 case ',':
588 case '(':
589 case ')':
590 case '{':
591 case '}':
592 case '[':
593 case ']':
594 case ':':
595 case ';':
596 return c1;
597 default:
598 return 0;
599 }
600 }
601
602 // Skip a C style comment. *PP points to just after the "/*". Return
603 // false if the comment did not end.
604
605 bool
606 Lex::skip_c_comment(const char** pp)
607 {
608 const char* p = *pp;
609 while (p[0] != '*' || p[1] != '/')
610 {
611 if (*p == '\0')
612 {
613 *pp = p;
614 return false;
615 }
616
617 if (*p == '\n')
618 {
619 ++this->lineno_;
620 this->linestart_ = p + 1;
621 }
622 ++p;
623 }
624
625 *pp = p + 2;
626 return true;
627 }
628
629 // Skip a line # comment. Return false if there was no newline.
630
631 bool
632 Lex::skip_line_comment(const char** pp)
633 {
634 const char* p = *pp;
635 size_t skip = strcspn(p, "\n");
636 if (p[skip] == '\0')
637 {
638 *pp = p + skip;
639 return false;
640 }
641
642 p += skip + 1;
643 ++this->lineno_;
644 this->linestart_ = p;
645 *pp = p;
646
647 return true;
648 }
649
650 // Build a token CLASSIFICATION from all characters that match
651 // CAN_CONTINUE_FN. Update *PP.
652
653 inline Token
654 Lex::gather_token(Token::Classification classification,
655 bool (Lex::*can_continue_fn)(char),
656 const char* start,
657 const char* match,
658 const char **pp)
659 {
660 while ((this->*can_continue_fn)(*match))
661 ++match;
662 *pp = match;
663 return this->make_token(classification, start, match - start, start);
664 }
665
666 // Build a token from a quoted string.
667
668 Token
669 Lex::gather_quoted_string(const char** pp)
670 {
671 const char* start = *pp;
672 const char* p = start;
673 ++p;
674 size_t skip = strcspn(p, "\"\n");
675 if (p[skip] != '"')
676 return this->make_invalid_token(start);
677 *pp = p + skip + 1;
678 return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
679 }
680
681 // Return the next token at *PP. Update *PP. General guideline: we
682 // require linker scripts to be simple ASCII. No unicode linker
683 // scripts. In particular we can assume that any '\0' is the end of
684 // the input.
685
686 Token
687 Lex::get_token(const char** pp)
688 {
689 const char* p = *pp;
690
691 while (true)
692 {
693 if (*p == '\0')
694 {
695 *pp = p;
696 return this->make_eof_token(p);
697 }
698
699 // Skip whitespace quickly.
700 while (*p == ' ' || *p == '\t')
701 ++p;
702
703 if (*p == '\n')
704 {
705 ++p;
706 ++this->lineno_;
707 this->linestart_ = p;
708 continue;
709 }
710
711 // Skip C style comments.
712 if (p[0] == '/' && p[1] == '*')
713 {
714 int lineno = this->lineno_;
715 int charpos = p - this->linestart_ + 1;
716
717 *pp = p + 2;
718 if (!this->skip_c_comment(pp))
719 return Token(Token::TOKEN_INVALID, lineno, charpos);
720 p = *pp;
721
722 continue;
723 }
724
725 // Skip line comments.
726 if (*p == '#')
727 {
728 *pp = p + 1;
729 if (!this->skip_line_comment(pp))
730 return this->make_eof_token(p);
731 p = *pp;
732 continue;
733 }
734
735 // Check for a name.
736 if (this->can_start_name(p[0], p[1]))
737 return this->gather_token(Token::TOKEN_STRING,
738 &Lex::can_continue_name,
739 p, p + 1, pp);
740
741 // We accept any arbitrary name in double quotes, as long as it
742 // does not cross a line boundary.
743 if (*p == '"')
744 {
745 *pp = p;
746 return this->gather_quoted_string(pp);
747 }
748
749 // Check for a number.
750
751 if (this->can_start_hex(p[0], p[1], p[2]))
752 return this->gather_token(Token::TOKEN_INTEGER,
753 &Lex::can_continue_hex,
754 p, p + 3, pp);
755
756 if (Lex::can_start_number(p[0]))
757 return this->gather_token(Token::TOKEN_INTEGER,
758 &Lex::can_continue_number,
759 p, p + 1, pp);
760
761 // Check for operators.
762
763 int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
764 if (opcode != 0)
765 {
766 *pp = p + 3;
767 return this->make_token(opcode, p);
768 }
769
770 opcode = Lex::two_char_operator(p[0], p[1]);
771 if (opcode != 0)
772 {
773 *pp = p + 2;
774 return this->make_token(opcode, p);
775 }
776
777 opcode = Lex::one_char_operator(p[0]);
778 if (opcode != 0)
779 {
780 *pp = p + 1;
781 return this->make_token(opcode, p);
782 }
783
784 return this->make_token(Token::TOKEN_INVALID, p);
785 }
786 }
787
788 // Return the next token.
789
790 const Token*
791 Lex::next_token()
792 {
793 // The first token is special.
794 if (this->first_token_ != 0)
795 {
796 this->token_ = Token(this->first_token_, 0, 0);
797 this->first_token_ = 0;
798 return &this->token_;
799 }
800
801 this->token_ = this->get_token(&this->current_);
802
803 // Don't let an early null byte fool us into thinking that we've
804 // reached the end of the file.
805 if (this->token_.is_eof()
806 && (static_cast<size_t>(this->current_ - this->input_string_)
807 < this->input_length_))
808 this->token_ = this->make_invalid_token(this->current_);
809
810 return &this->token_;
811 }
812
813 // A trivial task which waits for THIS_BLOCKER to be clear and then
814 // clears NEXT_BLOCKER. THIS_BLOCKER may be NULL.
815
816 class Script_unblock : public Task
817 {
818 public:
819 Script_unblock(Task_token* this_blocker, Task_token* next_blocker)
820 : this_blocker_(this_blocker), next_blocker_(next_blocker)
821 { }
822
823 ~Script_unblock()
824 {
825 if (this->this_blocker_ != NULL)
826 delete this->this_blocker_;
827 }
828
829 Task_token*
830 is_runnable()
831 {
832 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
833 return this->this_blocker_;
834 return NULL;
835 }
836
837 void
838 locks(Task_locker* tl)
839 { tl->add(this, this->next_blocker_); }
840
841 void
842 run(Workqueue*)
843 { }
844
845 std::string
846 get_name() const
847 { return "Script_unblock"; }
848
849 private:
850 Task_token* this_blocker_;
851 Task_token* next_blocker_;
852 };
853
854 // Class Script_options.
855
856 Script_options::Script_options()
857 : entry_(), symbol_assignments_()
858 {
859 }
860
861 // Add any symbols we are defining to the symbol table.
862
863 void
864 Script_options::add_symbols_to_table(Symbol_table* symtab,
865 const Target* target)
866 {
867 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
868 p != this->symbol_assignments_.end();
869 ++p)
870 {
871 elfcpp::STV vis = p->hidden ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
872 p->sym = symtab->define_as_constant(target,
873 p->name.c_str(),
874 NULL, // version
875 0, // value
876 0, // size
877 elfcpp::STT_NOTYPE,
878 elfcpp::STB_GLOBAL,
879 vis,
880 0, // nonvis
881 p->provide);
882 }
883 }
884
885 // Finalize symbol values.
886
887 void
888 Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
889 {
890 if (parameters->get_size() == 32)
891 {
892 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
893 this->sized_finalize_symbols<32>(symtab, layout);
894 #else
895 gold_unreachable();
896 #endif
897 }
898 else if (parameters->get_size() == 64)
899 {
900 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
901 this->sized_finalize_symbols<64>(symtab, layout);
902 #else
903 gold_unreachable();
904 #endif
905 }
906 else
907 gold_unreachable();
908 }
909
910 template<int size>
911 void
912 Script_options::sized_finalize_symbols(Symbol_table* symtab,
913 const Layout* layout)
914 {
915 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
916 p != this->symbol_assignments_.end();
917 ++p)
918 {
919 if (p->sym != NULL)
920 {
921 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(p->sym);
922 ssym->set_value(p->value->eval(symtab, layout));
923 }
924 }
925 }
926
927 // This class holds data passed through the parser to the lexer and to
928 // the parser support functions. This avoids global variables. We
929 // can't use global variables because we need not be called by a
930 // singleton thread.
931
932 class Parser_closure
933 {
934 public:
935 Parser_closure(const char* filename,
936 const Position_dependent_options& posdep_options,
937 bool in_group, bool is_in_sysroot,
938 Command_line* command_line,
939 Script_options* script_options,
940 Lex* lex)
941 : filename_(filename), posdep_options_(posdep_options),
942 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
943 command_line_(command_line), script_options_(script_options),
944 lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL)
945 { }
946
947 // Return the file name.
948 const char*
949 filename() const
950 { return this->filename_; }
951
952 // Return the position dependent options. The caller may modify
953 // this.
954 Position_dependent_options&
955 position_dependent_options()
956 { return this->posdep_options_; }
957
958 // Return whether this script is being run in a group.
959 bool
960 in_group() const
961 { return this->in_group_; }
962
963 // Return whether this script was found using a directory in the
964 // sysroot.
965 bool
966 is_in_sysroot() const
967 { return this->is_in_sysroot_; }
968
969 // Returns the Command_line structure passed in at constructor time.
970 // This value may be NULL. The caller may modify this, which modifies
971 // the passed-in Command_line object (not a copy).
972 Command_line*
973 command_line()
974 { return this->command_line_; }
975
976 // Return the options which may be set by a script.
977 Script_options*
978 script_options()
979 { return this->script_options_; }
980
981 // Return the next token, and advance.
982 const Token*
983 next_token()
984 {
985 const Token* token = this->lex_->next_token();
986 this->lineno_ = token->lineno();
987 this->charpos_ = token->charpos();
988 return token;
989 }
990
991 // Set a new lexer mode, pushing the current one.
992 void
993 push_lex_mode(Lex::Mode mode)
994 {
995 this->lex_mode_stack_.push_back(this->lex_->mode());
996 this->lex_->set_mode(mode);
997 }
998
999 // Pop the lexer mode.
1000 void
1001 pop_lex_mode()
1002 {
1003 gold_assert(!this->lex_mode_stack_.empty());
1004 this->lex_->set_mode(this->lex_mode_stack_.back());
1005 this->lex_mode_stack_.pop_back();
1006 }
1007
1008 // Return the line number of the last token.
1009 int
1010 lineno() const
1011 { return this->lineno_; }
1012
1013 // Return the character position in the line of the last token.
1014 int
1015 charpos() const
1016 { return this->charpos_; }
1017
1018 // Return the list of input files, creating it if necessary. This
1019 // is a space leak--we never free the INPUTS_ pointer.
1020 Input_arguments*
1021 inputs()
1022 {
1023 if (this->inputs_ == NULL)
1024 this->inputs_ = new Input_arguments();
1025 return this->inputs_;
1026 }
1027
1028 // Return whether we saw any input files.
1029 bool
1030 saw_inputs() const
1031 { return this->inputs_ != NULL && !this->inputs_->empty(); }
1032
1033 private:
1034 // The name of the file we are reading.
1035 const char* filename_;
1036 // The position dependent options.
1037 Position_dependent_options posdep_options_;
1038 // Whether we are currently in a --start-group/--end-group.
1039 bool in_group_;
1040 // Whether the script was found in a sysrooted directory.
1041 bool is_in_sysroot_;
1042 // May be NULL if the user chooses not to pass one in.
1043 Command_line* command_line_;
1044 // Options which may be set from any linker script.
1045 Script_options* script_options_;
1046 // The lexer.
1047 Lex* lex_;
1048 // The line number of the last token returned by next_token.
1049 int lineno_;
1050 // The column number of the last token returned by next_token.
1051 int charpos_;
1052 // A stack of lexer modes.
1053 std::vector<Lex::Mode> lex_mode_stack_;
1054 // New input files found to add to the link.
1055 Input_arguments* inputs_;
1056 };
1057
1058 // FILE was found as an argument on the command line. Try to read it
1059 // as a script. We've already read BYTES of data into P, but we
1060 // ignore that. Return true if the file was handled.
1061
1062 bool
1063 read_input_script(Workqueue* workqueue, const General_options& options,
1064 Symbol_table* symtab, Layout* layout,
1065 Dirsearch* dirsearch, Input_objects* input_objects,
1066 Input_group* input_group,
1067 const Input_argument* input_argument,
1068 Input_file* input_file, const unsigned char*, off_t,
1069 Task_token* this_blocker, Task_token* next_blocker)
1070 {
1071 std::string input_string;
1072 Lex::read_file(input_file, &input_string);
1073
1074 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1075
1076 Parser_closure closure(input_file->filename().c_str(),
1077 input_argument->file().options(),
1078 input_group != NULL,
1079 input_file->is_in_sysroot(),
1080 NULL,
1081 layout->script_options(),
1082 &lex);
1083
1084 if (yyparse(&closure) != 0)
1085 return false;
1086
1087 // THIS_BLOCKER must be clear before we may add anything to the
1088 // symbol table. We are responsible for unblocking NEXT_BLOCKER
1089 // when we are done. We are responsible for deleting THIS_BLOCKER
1090 // when it is unblocked.
1091
1092 if (!closure.saw_inputs())
1093 {
1094 // The script did not add any files to read. Note that we are
1095 // not permitted to call NEXT_BLOCKER->unblock() here even if
1096 // THIS_BLOCKER is NULL, as we do not hold the workqueue lock.
1097 workqueue->queue(new Script_unblock(this_blocker, next_blocker));
1098 return true;
1099 }
1100
1101 for (Input_arguments::const_iterator p = closure.inputs()->begin();
1102 p != closure.inputs()->end();
1103 ++p)
1104 {
1105 Task_token* nb;
1106 if (p + 1 == closure.inputs()->end())
1107 nb = next_blocker;
1108 else
1109 {
1110 nb = new Task_token(true);
1111 nb->add_blocker();
1112 }
1113 workqueue->queue(new Read_symbols(options, input_objects, symtab,
1114 layout, dirsearch, &*p,
1115 input_group, this_blocker, nb));
1116 this_blocker = nb;
1117 }
1118
1119 return true;
1120 }
1121
1122 // FILENAME was found as an argument to --script (-T).
1123 // Read it as a script, and execute its contents immediately.
1124
1125 bool
1126 read_commandline_script(const char* filename, Command_line* cmdline)
1127 {
1128 // TODO: if filename is a relative filename, search for it manually
1129 // using "." + cmdline->options()->search_path() -- not dirsearch.
1130 Dirsearch dirsearch;
1131
1132 // The file locking code wants to record a Task, but we haven't
1133 // started the workqueue yet. This is only for debugging purposes,
1134 // so we invent a fake value.
1135 const Task* task = reinterpret_cast<const Task*>(-1);
1136
1137 Input_file_argument input_argument(filename, false, "",
1138 cmdline->position_dependent_options());
1139 Input_file input_file(&input_argument);
1140 if (!input_file.open(cmdline->options(), dirsearch, task))
1141 return false;
1142
1143 std::string input_string;
1144 Lex::read_file(&input_file, &input_string);
1145
1146 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1147
1148 Parser_closure closure(filename,
1149 cmdline->position_dependent_options(),
1150 false,
1151 input_file.is_in_sysroot(),
1152 cmdline,
1153 cmdline->script_options(),
1154 &lex);
1155 if (yyparse(&closure) != 0)
1156 {
1157 input_file.file().unlock(task);
1158 return false;
1159 }
1160
1161 input_file.file().unlock(task);
1162
1163 gold_assert(!closure.saw_inputs());
1164
1165 return true;
1166 }
1167
1168 // Implement the --defsym option on the command line. Return true if
1169 // all is well.
1170
1171 bool
1172 Script_options::define_symbol(const char* definition)
1173 {
1174 Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1175 lex.set_mode(Lex::EXPRESSION);
1176
1177 // Dummy value.
1178 Position_dependent_options posdep_options;
1179
1180 Parser_closure closure("command line", posdep_options, false, false, NULL,
1181 this, &lex);
1182
1183 if (yyparse(&closure) != 0)
1184 return false;
1185
1186 gold_assert(!closure.saw_inputs());
1187
1188 return true;
1189 }
1190
1191 // Manage mapping from keywords to the codes expected by the bison
1192 // parser.
1193
1194 class Keyword_to_parsecode
1195 {
1196 public:
1197 // The structure which maps keywords to parsecodes.
1198 struct Keyword_parsecode
1199 {
1200 // Keyword.
1201 const char* keyword;
1202 // Corresponding parsecode.
1203 int parsecode;
1204 };
1205
1206 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1207 // keyword.
1208 static int
1209 keyword_to_parsecode(const char* keyword, size_t len);
1210
1211 private:
1212 // The array of all keywords.
1213 static const Keyword_parsecode keyword_parsecodes_[];
1214
1215 // The number of keywords.
1216 static const int keyword_count;
1217 };
1218
1219 // Mapping from keyword string to keyword parsecode. This array must
1220 // be kept in sorted order. Parsecodes are looked up using bsearch.
1221 // This array must correspond to the list of parsecodes in yyscript.y.
1222
1223 const Keyword_to_parsecode::Keyword_parsecode
1224 Keyword_to_parsecode::keyword_parsecodes_[] =
1225 {
1226 { "ABSOLUTE", ABSOLUTE },
1227 { "ADDR", ADDR },
1228 { "ALIGN", ALIGN_K },
1229 { "ALIGNOF", ALIGNOF },
1230 { "ASSERT", ASSERT_K },
1231 { "AS_NEEDED", AS_NEEDED },
1232 { "AT", AT },
1233 { "BIND", BIND },
1234 { "BLOCK", BLOCK },
1235 { "BYTE", BYTE },
1236 { "CONSTANT", CONSTANT },
1237 { "CONSTRUCTORS", CONSTRUCTORS },
1238 { "COPY", COPY },
1239 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1240 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1241 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1242 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1243 { "DEFINED", DEFINED },
1244 { "DSECT", DSECT },
1245 { "ENTRY", ENTRY },
1246 { "EXCLUDE_FILE", EXCLUDE_FILE },
1247 { "EXTERN", EXTERN },
1248 { "FILL", FILL },
1249 { "FLOAT", FLOAT },
1250 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1251 { "GROUP", GROUP },
1252 { "HLL", HLL },
1253 { "INCLUDE", INCLUDE },
1254 { "INFO", INFO },
1255 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1256 { "INPUT", INPUT },
1257 { "KEEP", KEEP },
1258 { "LENGTH", LENGTH },
1259 { "LOADADDR", LOADADDR },
1260 { "LONG", LONG },
1261 { "MAP", MAP },
1262 { "MAX", MAX_K },
1263 { "MEMORY", MEMORY },
1264 { "MIN", MIN_K },
1265 { "NEXT", NEXT },
1266 { "NOCROSSREFS", NOCROSSREFS },
1267 { "NOFLOAT", NOFLOAT },
1268 { "NOLOAD", NOLOAD },
1269 { "ONLY_IF_RO", ONLY_IF_RO },
1270 { "ONLY_IF_RW", ONLY_IF_RW },
1271 { "OPTION", OPTION },
1272 { "ORIGIN", ORIGIN },
1273 { "OUTPUT", OUTPUT },
1274 { "OUTPUT_ARCH", OUTPUT_ARCH },
1275 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1276 { "OVERLAY", OVERLAY },
1277 { "PHDRS", PHDRS },
1278 { "PROVIDE", PROVIDE },
1279 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1280 { "QUAD", QUAD },
1281 { "SEARCH_DIR", SEARCH_DIR },
1282 { "SECTIONS", SECTIONS },
1283 { "SEGMENT_START", SEGMENT_START },
1284 { "SHORT", SHORT },
1285 { "SIZEOF", SIZEOF },
1286 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1287 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1288 { "SORT_BY_NAME", SORT_BY_NAME },
1289 { "SPECIAL", SPECIAL },
1290 { "SQUAD", SQUAD },
1291 { "STARTUP", STARTUP },
1292 { "SUBALIGN", SUBALIGN },
1293 { "SYSLIB", SYSLIB },
1294 { "TARGET", TARGET_K },
1295 { "TRUNCATE", TRUNCATE },
1296 { "VERSION", VERSIONK },
1297 { "global", GLOBAL },
1298 { "l", LENGTH },
1299 { "len", LENGTH },
1300 { "local", LOCAL },
1301 { "o", ORIGIN },
1302 { "org", ORIGIN },
1303 { "sizeof_headers", SIZEOF_HEADERS },
1304 };
1305
1306 const int Keyword_to_parsecode::keyword_count =
1307 (sizeof(Keyword_to_parsecode::keyword_parsecodes_)
1308 / sizeof(Keyword_to_parsecode::keyword_parsecodes_[0]));
1309
1310 // Comparison function passed to bsearch.
1311
1312 extern "C"
1313 {
1314
1315 struct Ktt_key
1316 {
1317 const char* str;
1318 size_t len;
1319 };
1320
1321 static int
1322 ktt_compare(const void* keyv, const void* kttv)
1323 {
1324 const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
1325 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1326 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1327 int i = strncmp(key->str, ktt->keyword, key->len);
1328 if (i != 0)
1329 return i;
1330 if (ktt->keyword[key->len] != '\0')
1331 return -1;
1332 return 0;
1333 }
1334
1335 } // End extern "C".
1336
1337 int
1338 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword, size_t len)
1339 {
1340 Ktt_key key;
1341 key.str = keyword;
1342 key.len = len;
1343 void* kttv = bsearch(&key,
1344 Keyword_to_parsecode::keyword_parsecodes_,
1345 Keyword_to_parsecode::keyword_count,
1346 sizeof(Keyword_to_parsecode::keyword_parsecodes_[0]),
1347 ktt_compare);
1348 if (kttv == NULL)
1349 return 0;
1350 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1351 return ktt->parsecode;
1352 }
1353
1354 } // End namespace gold.
1355
1356 // The remaining functions are extern "C", so it's clearer to not put
1357 // them in namespace gold.
1358
1359 using namespace gold;
1360
1361 // This function is called by the bison parser to return the next
1362 // token.
1363
1364 extern "C" int
1365 yylex(YYSTYPE* lvalp, void* closurev)
1366 {
1367 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1368 const Token* token = closure->next_token();
1369 switch (token->classification())
1370 {
1371 default:
1372 gold_unreachable();
1373
1374 case Token::TOKEN_INVALID:
1375 yyerror(closurev, "invalid character");
1376 return 0;
1377
1378 case Token::TOKEN_EOF:
1379 return 0;
1380
1381 case Token::TOKEN_STRING:
1382 {
1383 // This is either a keyword or a STRING.
1384 size_t len;
1385 const char* str = token->string_value(&len);
1386 int parsecode = Keyword_to_parsecode::keyword_to_parsecode(str, len);
1387 if (parsecode != 0)
1388 return parsecode;
1389 lvalp->string.value = str;
1390 lvalp->string.length = len;
1391 return STRING;
1392 }
1393
1394 case Token::TOKEN_QUOTED_STRING:
1395 lvalp->string.value = token->string_value(&lvalp->string.length);
1396 return STRING;
1397
1398 case Token::TOKEN_OPERATOR:
1399 return token->operator_value();
1400
1401 case Token::TOKEN_INTEGER:
1402 lvalp->integer = token->integer_value();
1403 return INTEGER;
1404 }
1405 }
1406
1407 // This function is called by the bison parser to report an error.
1408
1409 extern "C" void
1410 yyerror(void* closurev, const char* message)
1411 {
1412 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1413 gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
1414 closure->charpos(), message);
1415 }
1416
1417 // Called by the bison parser to add a file to the link.
1418
1419 extern "C" void
1420 script_add_file(void* closurev, const char* name, size_t length)
1421 {
1422 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1423
1424 // If this is an absolute path, and we found the script in the
1425 // sysroot, then we want to prepend the sysroot to the file name.
1426 // For example, this is how we handle a cross link to the x86_64
1427 // libc.so, which refers to /lib/libc.so.6.
1428 std::string name_string(name, length);
1429 const char* extra_search_path = ".";
1430 std::string script_directory;
1431 if (IS_ABSOLUTE_PATH(name_string.c_str()))
1432 {
1433 if (closure->is_in_sysroot())
1434 {
1435 const std::string& sysroot(parameters->sysroot());
1436 gold_assert(!sysroot.empty());
1437 name_string = sysroot + name_string;
1438 }
1439 }
1440 else
1441 {
1442 // In addition to checking the normal library search path, we
1443 // also want to check in the script-directory.
1444 const char *slash = strrchr(closure->filename(), '/');
1445 if (slash != NULL)
1446 {
1447 script_directory.assign(closure->filename(),
1448 slash - closure->filename() + 1);
1449 extra_search_path = script_directory.c_str();
1450 }
1451 }
1452
1453 Input_file_argument file(name_string.c_str(), false, extra_search_path,
1454 closure->position_dependent_options());
1455 closure->inputs()->add_file(file);
1456 }
1457
1458 // Called by the bison parser to start a group. If we are already in
1459 // a group, that means that this script was invoked within a
1460 // --start-group --end-group sequence on the command line, or that
1461 // this script was found in a GROUP of another script. In that case,
1462 // we simply continue the existing group, rather than starting a new
1463 // one. It is possible to construct a case in which this will do
1464 // something other than what would happen if we did a recursive group,
1465 // but it's hard to imagine why the different behaviour would be
1466 // useful for a real program. Avoiding recursive groups is simpler
1467 // and more efficient.
1468
1469 extern "C" void
1470 script_start_group(void* closurev)
1471 {
1472 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1473 if (!closure->in_group())
1474 closure->inputs()->start_group();
1475 }
1476
1477 // Called by the bison parser at the end of a group.
1478
1479 extern "C" void
1480 script_end_group(void* closurev)
1481 {
1482 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1483 if (!closure->in_group())
1484 closure->inputs()->end_group();
1485 }
1486
1487 // Called by the bison parser to start an AS_NEEDED list.
1488
1489 extern "C" void
1490 script_start_as_needed(void* closurev)
1491 {
1492 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1493 closure->position_dependent_options().set_as_needed();
1494 }
1495
1496 // Called by the bison parser at the end of an AS_NEEDED list.
1497
1498 extern "C" void
1499 script_end_as_needed(void* closurev)
1500 {
1501 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1502 closure->position_dependent_options().clear_as_needed();
1503 }
1504
1505 // Called by the bison parser to set the entry symbol.
1506
1507 extern "C" void
1508 script_set_entry(void* closurev, const char* entry, size_t length)
1509 {
1510 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1511 closure->script_options()->set_entry(entry, length);
1512 }
1513
1514 // Called by the bison parser to define a symbol.
1515
1516 extern "C" void
1517 script_set_symbol(void* closurev, const char* name, size_t length,
1518 Expression* value, int providei, int hiddeni)
1519 {
1520 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1521 const bool provide = providei != 0;
1522 const bool hidden = hiddeni != 0;
1523 closure->script_options()->add_symbol_assignment(name, length, value,
1524 provide, hidden);
1525 }
1526
1527 // Called by the bison parser to parse an OPTION.
1528
1529 extern "C" void
1530 script_parse_option(void* closurev, const char* option, size_t length)
1531 {
1532 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1533 // We treat the option as a single command-line option, even if
1534 // it has internal whitespace.
1535 if (closure->command_line() == NULL)
1536 {
1537 // There are some options that we could handle here--e.g.,
1538 // -lLIBRARY. Should we bother?
1539 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
1540 " for scripts specified via -T/--script"),
1541 closure->filename(), closure->lineno(), closure->charpos());
1542 }
1543 else
1544 {
1545 bool past_a_double_dash_option = false;
1546 char* mutable_option = strndup(option, length);
1547 gold_assert(mutable_option != NULL);
1548 closure->command_line()->process_one_option(1, &mutable_option, 0,
1549 &past_a_double_dash_option);
1550 free(mutable_option);
1551 }
1552 }
1553
1554 /* Called by the bison parser to push the lexer into expression
1555 mode. */
1556
1557 extern void
1558 script_push_lex_into_expression_mode(void* closurev)
1559 {
1560 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1561 closure->push_lex_mode(Lex::EXPRESSION);
1562 }
1563
1564 /* Called by the bison parser to pop the lexer mode. */
1565
1566 extern void
1567 script_pop_lex_mode(void* closurev)
1568 {
1569 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1570 closure->pop_lex_mode();
1571 }
This page took 0.071812 seconds and 5 git commands to generate.