3822c244a5c2d34bb136015b970dcd276ce26eab
[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 <fnmatch.h>
26 #include <string>
27 #include <vector>
28 #include <cstdio>
29 #include <cstdlib>
30 #include "filenames.h"
31
32 #include "elfcpp.h"
33 #include "demangle.h"
34 #include "dirsearch.h"
35 #include "options.h"
36 #include "fileread.h"
37 #include "workqueue.h"
38 #include "readsyms.h"
39 #include "parameters.h"
40 #include "layout.h"
41 #include "symtab.h"
42 #include "script.h"
43 #include "script-c.h"
44
45 namespace 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
51 class 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,
63 // Token is a quoted string of characters.
64 TOKEN_QUOTED_STRING,
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()
73 : classification_(TOKEN_INVALID), value_(NULL), value_length_(0),
74 opcode_(0), lineno_(0), charpos_(0)
75 { }
76
77 // A general token with no value.
78 Token(Classification classification, int lineno, int charpos)
79 : classification_(classification), value_(NULL), value_length_(0),
80 opcode_(0), lineno_(lineno), charpos_(charpos)
81 {
82 gold_assert(classification == TOKEN_INVALID
83 || classification == TOKEN_EOF);
84 }
85
86 // A general token with a value.
87 Token(Classification classification, const char* value, size_t length,
88 int lineno, int charpos)
89 : classification_(classification), value_(value), value_length_(length),
90 opcode_(0), lineno_(lineno), charpos_(charpos)
91 {
92 gold_assert(classification != TOKEN_INVALID
93 && classification != TOKEN_EOF);
94 }
95
96 // A token representing an operator.
97 Token(int opcode, int lineno, int charpos)
98 : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0),
99 opcode_(opcode), lineno_(lineno), charpos_(charpos)
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
129 const char*
130 string_value(size_t* length) const
131 {
132 gold_assert(this->classification_ == TOKEN_STRING
133 || this->classification_ == TOKEN_QUOTED_STRING);
134 *length = this->value_length_;
135 return this->value_;
136 }
137
138 int
139 operator_value() const
140 {
141 gold_assert(this->classification_ == TOKEN_OPERATOR);
142 return this->opcode_;
143 }
144
145 uint64_t
146 integer_value() const
147 {
148 gold_assert(this->classification_ == TOKEN_INTEGER);
149 // Null terminate.
150 std::string s(this->value_, this->value_length_);
151 return strtoull(s.c_str(), NULL, 0);
152 }
153
154 private:
155 // The token classification.
156 Classification classification_;
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_;
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
171 // This class handles lexing a file into a sequence of tokens.
172
173 class Lex
174 {
175 public:
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)
194 { }
195
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();
203
204 // Return the current lexing mode.
205 Lex::Mode
206 mode() const
207 { return this->mode_; }
208
209 // Set the lexing mode.
210 void
211 set_mode(Mode mode)
212 { this->mode_ = mode; }
213
214 private:
215 Lex(const Lex&);
216 Lex& operator=(const Lex&);
217
218 // Make a general token with no value at the current location.
219 Token
220 make_token(Token::Classification c, const char* start) const
221 { return Token(c, this->lineno_, start - this->linestart_ + 1); }
222
223 // Make a general token with a value at the current location.
224 Token
225 make_token(Token::Classification c, const char* v, size_t len,
226 const char* start)
227 const
228 { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); }
229
230 // Make an operator token at the current location.
231 Token
232 make_token(int opcode, const char* start) const
233 { return Token(opcode, this->lineno_, start - this->linestart_ + 1); }
234
235 // Make an invalid token at the current location.
236 Token
237 make_invalid_token(const char* start)
238 { return this->make_token(Token::TOKEN_INVALID, start); }
239
240 // Make an EOF token at the current location.
241 Token
242 make_eof_token(const char* start)
243 { return this->make_token(Token::TOKEN_EOF, start); }
244
245 // Return whether C can be the first character in a name. C2 is the
246 // next character, since we sometimes need that.
247 inline bool
248 can_start_name(char c, char c2);
249
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);
255
256 // Return whether C, C2, C3 can start a hex number.
257 inline bool
258 can_start_hex(char c, char c2, char c3);
259
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);
265
266 // Return whether C can start a non-hex number.
267 static inline bool
268 can_start_number(char c);
269
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; }
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
309 gather_token(Token::Classification,
310 const char* (Lex::*can_continue_fn)(const char*),
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
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_;
330 // The current line number.
331 int lineno_;
332 // The start of the current line in the string.
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
340 void
341 Lex::read_file(Input_file* input_file, std::string* contents)
342 {
343 off_t filesize = input_file->file().filesize();
344 contents->clear();
345 contents->reserve(filesize);
346
347 off_t off = 0;
348 unsigned char buf[BUFSIZ];
349 while (off < filesize)
350 {
351 off_t get = BUFSIZ;
352 if (get > filesize - off)
353 get = filesize - off;
354 input_file->file().read(off, get, buf);
355 contents->append(reinterpret_cast<char*>(&buf[0]), get);
356 off += get;
357 }
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
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
369 // compatible.
370
371 inline bool
372 Lex::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':
386 case '_': case '.': case '$':
387 return true;
388
389 case '/': case '\\':
390 return this->mode_ == LINKER_SCRIPT;
391
392 case '~':
393 return this->mode_ == LINKER_SCRIPT && can_continue_name(&c2);
394
395 case '*': case '[':
396 return this->mode_ == VERSION_SCRIPT;
397
398 default:
399 return false;
400 }
401 }
402
403 // Return whether C can continue a name which has already started.
404 // Subsequent characters in a name are the same as the leading
405 // characters, plus digits and "=+-:[],?*". So in general the linker
406 // script language requires spaces around operators, unless we know
407 // that we are parsing an expression.
408
409 inline const char*
410 Lex::can_continue_name(const char* c)
411 {
412 switch (*c)
413 {
414 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
415 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
416 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
417 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
418 case 'Y': case 'Z':
419 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
420 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
421 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
422 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
423 case 'y': case 'z':
424 case '_': case '.': case '$':
425 case '0': case '1': case '2': case '3': case '4':
426 case '5': case '6': case '7': case '8': case '9':
427 return c + 1;
428
429 case '/': case '\\': case '~':
430 case '=': case '+':
431 case ',': case '?':
432 if (this->mode_ == LINKER_SCRIPT)
433 return c + 1;
434 return NULL;
435
436 case '[': case ']': case '*': case '-':
437 if (this->mode_ == LINKER_SCRIPT || this->mode_ == VERSION_SCRIPT)
438 return c + 1;
439 return NULL;
440
441 case '^':
442 if (this->mode_ == VERSION_SCRIPT)
443 return c + 1;
444 return NULL;
445
446 case ':':
447 if (this->mode_ == LINKER_SCRIPT)
448 return c + 1;
449 else if (this->mode_ == VERSION_SCRIPT && (c[1] == ':'))
450 {
451 // A name can have '::' in it, as that's a c++ namespace
452 // separator. But a single colon is not part of a name.
453 return c + 2;
454 }
455 return NULL;
456
457 default:
458 return NULL;
459 }
460 }
461
462 // For a number we accept 0x followed by hex digits, or any sequence
463 // of digits. The old linker accepts leading '$' for hex, and
464 // trailing HXBOD. Those are for MRI compatibility and we don't
465 // accept them. The old linker also accepts trailing MK for mega or
466 // kilo. FIXME: Those are mentioned in the documentation, and we
467 // should accept them.
468
469 // Return whether C1 C2 C3 can start a hex number.
470
471 inline bool
472 Lex::can_start_hex(char c1, char c2, char c3)
473 {
474 if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
475 return this->can_continue_hex(&c3);
476 return false;
477 }
478
479 // Return whether C can appear in a hex number.
480
481 inline const char*
482 Lex::can_continue_hex(const char* c)
483 {
484 switch (*c)
485 {
486 case '0': case '1': case '2': case '3': case '4':
487 case '5': case '6': case '7': case '8': case '9':
488 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
489 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
490 return c + 1;
491
492 default:
493 return NULL;
494 }
495 }
496
497 // Return whether C can start a non-hex number.
498
499 inline bool
500 Lex::can_start_number(char c)
501 {
502 switch (c)
503 {
504 case '0': case '1': case '2': case '3': case '4':
505 case '5': case '6': case '7': case '8': case '9':
506 return true;
507
508 default:
509 return false;
510 }
511 }
512
513 // If C1 C2 C3 form a valid three character operator, return the
514 // opcode (defined in the yyscript.h file generated from yyscript.y).
515 // Otherwise return 0.
516
517 inline int
518 Lex::three_char_operator(char c1, char c2, char c3)
519 {
520 switch (c1)
521 {
522 case '<':
523 if (c2 == '<' && c3 == '=')
524 return LSHIFTEQ;
525 break;
526 case '>':
527 if (c2 == '>' && c3 == '=')
528 return RSHIFTEQ;
529 break;
530 default:
531 break;
532 }
533 return 0;
534 }
535
536 // If C1 C2 form a valid two character operator, return the opcode
537 // (defined in the yyscript.h file generated from yyscript.y).
538 // Otherwise return 0.
539
540 inline int
541 Lex::two_char_operator(char c1, char c2)
542 {
543 switch (c1)
544 {
545 case '=':
546 if (c2 == '=')
547 return EQ;
548 break;
549 case '!':
550 if (c2 == '=')
551 return NE;
552 break;
553 case '+':
554 if (c2 == '=')
555 return PLUSEQ;
556 break;
557 case '-':
558 if (c2 == '=')
559 return MINUSEQ;
560 break;
561 case '*':
562 if (c2 == '=')
563 return MULTEQ;
564 break;
565 case '/':
566 if (c2 == '=')
567 return DIVEQ;
568 break;
569 case '|':
570 if (c2 == '=')
571 return OREQ;
572 if (c2 == '|')
573 return OROR;
574 break;
575 case '&':
576 if (c2 == '=')
577 return ANDEQ;
578 if (c2 == '&')
579 return ANDAND;
580 break;
581 case '>':
582 if (c2 == '=')
583 return GE;
584 if (c2 == '>')
585 return RSHIFT;
586 break;
587 case '<':
588 if (c2 == '=')
589 return LE;
590 if (c2 == '<')
591 return LSHIFT;
592 break;
593 default:
594 break;
595 }
596 return 0;
597 }
598
599 // If C1 is a valid operator, return the opcode. Otherwise return 0.
600
601 inline int
602 Lex::one_char_operator(char c1)
603 {
604 switch (c1)
605 {
606 case '+':
607 case '-':
608 case '*':
609 case '/':
610 case '%':
611 case '!':
612 case '&':
613 case '|':
614 case '^':
615 case '~':
616 case '<':
617 case '>':
618 case '=':
619 case '?':
620 case ',':
621 case '(':
622 case ')':
623 case '{':
624 case '}':
625 case '[':
626 case ']':
627 case ':':
628 case ';':
629 return c1;
630 default:
631 return 0;
632 }
633 }
634
635 // Skip a C style comment. *PP points to just after the "/*". Return
636 // false if the comment did not end.
637
638 bool
639 Lex::skip_c_comment(const char** pp)
640 {
641 const char* p = *pp;
642 while (p[0] != '*' || p[1] != '/')
643 {
644 if (*p == '\0')
645 {
646 *pp = p;
647 return false;
648 }
649
650 if (*p == '\n')
651 {
652 ++this->lineno_;
653 this->linestart_ = p + 1;
654 }
655 ++p;
656 }
657
658 *pp = p + 2;
659 return true;
660 }
661
662 // Skip a line # comment. Return false if there was no newline.
663
664 bool
665 Lex::skip_line_comment(const char** pp)
666 {
667 const char* p = *pp;
668 size_t skip = strcspn(p, "\n");
669 if (p[skip] == '\0')
670 {
671 *pp = p + skip;
672 return false;
673 }
674
675 p += skip + 1;
676 ++this->lineno_;
677 this->linestart_ = p;
678 *pp = p;
679
680 return true;
681 }
682
683 // Build a token CLASSIFICATION from all characters that match
684 // CAN_CONTINUE_FN. Update *PP.
685
686 inline Token
687 Lex::gather_token(Token::Classification classification,
688 const char* (Lex::*can_continue_fn)(const char*),
689 const char* start,
690 const char* match,
691 const char **pp)
692 {
693 const char* new_match = NULL;
694 while ((new_match = (this->*can_continue_fn)(match)))
695 match = new_match;
696 *pp = match;
697 return this->make_token(classification, start, match - start, start);
698 }
699
700 // Build a token from a quoted string.
701
702 Token
703 Lex::gather_quoted_string(const char** pp)
704 {
705 const char* start = *pp;
706 const char* p = start;
707 ++p;
708 size_t skip = strcspn(p, "\"\n");
709 if (p[skip] != '"')
710 return this->make_invalid_token(start);
711 *pp = p + skip + 1;
712 return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
713 }
714
715 // Return the next token at *PP. Update *PP. General guideline: we
716 // require linker scripts to be simple ASCII. No unicode linker
717 // scripts. In particular we can assume that any '\0' is the end of
718 // the input.
719
720 Token
721 Lex::get_token(const char** pp)
722 {
723 const char* p = *pp;
724
725 while (true)
726 {
727 if (*p == '\0')
728 {
729 *pp = p;
730 return this->make_eof_token(p);
731 }
732
733 // Skip whitespace quickly.
734 while (*p == ' ' || *p == '\t')
735 ++p;
736
737 if (*p == '\n')
738 {
739 ++p;
740 ++this->lineno_;
741 this->linestart_ = p;
742 continue;
743 }
744
745 // Skip C style comments.
746 if (p[0] == '/' && p[1] == '*')
747 {
748 int lineno = this->lineno_;
749 int charpos = p - this->linestart_ + 1;
750
751 *pp = p + 2;
752 if (!this->skip_c_comment(pp))
753 return Token(Token::TOKEN_INVALID, lineno, charpos);
754 p = *pp;
755
756 continue;
757 }
758
759 // Skip line comments.
760 if (*p == '#')
761 {
762 *pp = p + 1;
763 if (!this->skip_line_comment(pp))
764 return this->make_eof_token(p);
765 p = *pp;
766 continue;
767 }
768
769 // Check for a name.
770 if (this->can_start_name(p[0], p[1]))
771 return this->gather_token(Token::TOKEN_STRING,
772 &Lex::can_continue_name,
773 p, p + 1, pp);
774
775 // We accept any arbitrary name in double quotes, as long as it
776 // does not cross a line boundary.
777 if (*p == '"')
778 {
779 *pp = p;
780 return this->gather_quoted_string(pp);
781 }
782
783 // Check for a number.
784
785 if (this->can_start_hex(p[0], p[1], p[2]))
786 return this->gather_token(Token::TOKEN_INTEGER,
787 &Lex::can_continue_hex,
788 p, p + 3, pp);
789
790 if (Lex::can_start_number(p[0]))
791 return this->gather_token(Token::TOKEN_INTEGER,
792 &Lex::can_continue_number,
793 p, p + 1, pp);
794
795 // Check for operators.
796
797 int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
798 if (opcode != 0)
799 {
800 *pp = p + 3;
801 return this->make_token(opcode, p);
802 }
803
804 opcode = Lex::two_char_operator(p[0], p[1]);
805 if (opcode != 0)
806 {
807 *pp = p + 2;
808 return this->make_token(opcode, p);
809 }
810
811 opcode = Lex::one_char_operator(p[0]);
812 if (opcode != 0)
813 {
814 *pp = p + 1;
815 return this->make_token(opcode, p);
816 }
817
818 return this->make_token(Token::TOKEN_INVALID, p);
819 }
820 }
821
822 // Return the next token.
823
824 const Token*
825 Lex::next_token()
826 {
827 // The first token is special.
828 if (this->first_token_ != 0)
829 {
830 this->token_ = Token(this->first_token_, 0, 0);
831 this->first_token_ = 0;
832 return &this->token_;
833 }
834
835 this->token_ = this->get_token(&this->current_);
836
837 // Don't let an early null byte fool us into thinking that we've
838 // reached the end of the file.
839 if (this->token_.is_eof()
840 && (static_cast<size_t>(this->current_ - this->input_string_)
841 < this->input_length_))
842 this->token_ = this->make_invalid_token(this->current_);
843
844 return &this->token_;
845 }
846
847 // A trivial task which waits for THIS_BLOCKER to be clear and then
848 // clears NEXT_BLOCKER. THIS_BLOCKER may be NULL.
849
850 class Script_unblock : public Task
851 {
852 public:
853 Script_unblock(Task_token* this_blocker, Task_token* next_blocker)
854 : this_blocker_(this_blocker), next_blocker_(next_blocker)
855 { }
856
857 ~Script_unblock()
858 {
859 if (this->this_blocker_ != NULL)
860 delete this->this_blocker_;
861 }
862
863 Task_token*
864 is_runnable()
865 {
866 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
867 return this->this_blocker_;
868 return NULL;
869 }
870
871 void
872 locks(Task_locker* tl)
873 { tl->add(this, this->next_blocker_); }
874
875 void
876 run(Workqueue*)
877 { }
878
879 std::string
880 get_name() const
881 { return "Script_unblock"; }
882
883 private:
884 Task_token* this_blocker_;
885 Task_token* next_blocker_;
886 };
887
888 // Class Script_options.
889
890 Script_options::Script_options()
891 : entry_(), symbol_assignments_()
892 {
893 }
894
895 // Add any symbols we are defining to the symbol table.
896
897 void
898 Script_options::add_symbols_to_table(Symbol_table* symtab,
899 const Target* target)
900 {
901 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
902 p != this->symbol_assignments_.end();
903 ++p)
904 {
905 elfcpp::STV vis = p->hidden ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
906 p->sym = symtab->define_as_constant(target,
907 p->name.c_str(),
908 NULL, // version
909 0, // value
910 0, // size
911 elfcpp::STT_NOTYPE,
912 elfcpp::STB_GLOBAL,
913 vis,
914 0, // nonvis
915 p->provide);
916 }
917 }
918
919 // Finalize symbol values.
920
921 void
922 Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
923 {
924 if (parameters->get_size() == 32)
925 {
926 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
927 this->sized_finalize_symbols<32>(symtab, layout);
928 #else
929 gold_unreachable();
930 #endif
931 }
932 else if (parameters->get_size() == 64)
933 {
934 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
935 this->sized_finalize_symbols<64>(symtab, layout);
936 #else
937 gold_unreachable();
938 #endif
939 }
940 else
941 gold_unreachable();
942 }
943
944 template<int size>
945 void
946 Script_options::sized_finalize_symbols(Symbol_table* symtab,
947 const Layout* layout)
948 {
949 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
950 p != this->symbol_assignments_.end();
951 ++p)
952 {
953 if (p->sym != NULL)
954 {
955 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(p->sym);
956 ssym->set_value(p->value->eval(symtab, layout));
957 }
958 }
959 }
960
961 // This class holds data passed through the parser to the lexer and to
962 // the parser support functions. This avoids global variables. We
963 // can't use global variables because we need not be called by a
964 // singleton thread.
965
966 class Parser_closure
967 {
968 public:
969 Parser_closure(const char* filename,
970 const Position_dependent_options& posdep_options,
971 bool in_group, bool is_in_sysroot,
972 Command_line* command_line,
973 Script_options* script_options,
974 Lex* lex)
975 : filename_(filename), posdep_options_(posdep_options),
976 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
977 command_line_(command_line), script_options_(script_options),
978 version_script_info_(script_options->version_script_info()),
979 lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL)
980 {
981 // We start out processing C symbols in the default lex mode.
982 language_stack_.push_back("");
983 lex_mode_stack_.push_back(lex->mode());
984 }
985
986 // Return the file name.
987 const char*
988 filename() const
989 { return this->filename_; }
990
991 // Return the position dependent options. The caller may modify
992 // this.
993 Position_dependent_options&
994 position_dependent_options()
995 { return this->posdep_options_; }
996
997 // Return whether this script is being run in a group.
998 bool
999 in_group() const
1000 { return this->in_group_; }
1001
1002 // Return whether this script was found using a directory in the
1003 // sysroot.
1004 bool
1005 is_in_sysroot() const
1006 { return this->is_in_sysroot_; }
1007
1008 // Returns the Command_line structure passed in at constructor time.
1009 // This value may be NULL. The caller may modify this, which modifies
1010 // the passed-in Command_line object (not a copy).
1011 Command_line*
1012 command_line()
1013 { return this->command_line_; }
1014
1015 // Return the options which may be set by a script.
1016 Script_options*
1017 script_options()
1018 { return this->script_options_; }
1019
1020 // Return the object in which version script information should be stored.
1021 Version_script_info*
1022 version_script()
1023 { return this->version_script_info_; }
1024
1025 // Return the next token, and advance.
1026 const Token*
1027 next_token()
1028 {
1029 const Token* token = this->lex_->next_token();
1030 this->lineno_ = token->lineno();
1031 this->charpos_ = token->charpos();
1032 return token;
1033 }
1034
1035 // Set a new lexer mode, pushing the current one.
1036 void
1037 push_lex_mode(Lex::Mode mode)
1038 {
1039 this->lex_mode_stack_.push_back(this->lex_->mode());
1040 this->lex_->set_mode(mode);
1041 }
1042
1043 // Pop the lexer mode.
1044 void
1045 pop_lex_mode()
1046 {
1047 gold_assert(!this->lex_mode_stack_.empty());
1048 this->lex_->set_mode(this->lex_mode_stack_.back());
1049 this->lex_mode_stack_.pop_back();
1050 }
1051
1052 // Return the current lexer mode.
1053 Lex::Mode
1054 lex_mode() const
1055 { return this->lex_mode_stack_.back(); }
1056
1057 // Return the line number of the last token.
1058 int
1059 lineno() const
1060 { return this->lineno_; }
1061
1062 // Return the character position in the line of the last token.
1063 int
1064 charpos() const
1065 { return this->charpos_; }
1066
1067 // Return the list of input files, creating it if necessary. This
1068 // is a space leak--we never free the INPUTS_ pointer.
1069 Input_arguments*
1070 inputs()
1071 {
1072 if (this->inputs_ == NULL)
1073 this->inputs_ = new Input_arguments();
1074 return this->inputs_;
1075 }
1076
1077 // Return whether we saw any input files.
1078 bool
1079 saw_inputs() const
1080 { return this->inputs_ != NULL && !this->inputs_->empty(); }
1081
1082 // Return the current language being processed in a version script
1083 // (eg, "C++"). The empty string represents unmangled C names.
1084 const std::string&
1085 get_current_language() const
1086 { return this->language_stack_.back(); }
1087
1088 // Push a language onto the stack when entering an extern block.
1089 void push_language(const std::string& lang)
1090 { this->language_stack_.push_back(lang); }
1091
1092 // Pop a language off of the stack when exiting an extern block.
1093 void pop_language()
1094 {
1095 gold_assert(!this->language_stack_.empty());
1096 this->language_stack_.pop_back();
1097 }
1098
1099 private:
1100 // The name of the file we are reading.
1101 const char* filename_;
1102 // The position dependent options.
1103 Position_dependent_options posdep_options_;
1104 // Whether we are currently in a --start-group/--end-group.
1105 bool in_group_;
1106 // Whether the script was found in a sysrooted directory.
1107 bool is_in_sysroot_;
1108 // May be NULL if the user chooses not to pass one in.
1109 Command_line* command_line_;
1110 // Options which may be set from any linker script.
1111 Script_options* script_options_;
1112 // Information parsed from a version script.
1113 Version_script_info* version_script_info_;
1114 // The lexer.
1115 Lex* lex_;
1116 // The line number of the last token returned by next_token.
1117 int lineno_;
1118 // The column number of the last token returned by next_token.
1119 int charpos_;
1120 // A stack of lexer modes.
1121 std::vector<Lex::Mode> lex_mode_stack_;
1122 // A stack of which extern/language block we're inside. Can be C++,
1123 // java, or empty for C.
1124 std::vector<std::string> language_stack_;
1125 // New input files found to add to the link.
1126 Input_arguments* inputs_;
1127 };
1128
1129 // FILE was found as an argument on the command line. Try to read it
1130 // as a script. We've already read BYTES of data into P, but we
1131 // ignore that. Return true if the file was handled.
1132
1133 bool
1134 read_input_script(Workqueue* workqueue, const General_options& options,
1135 Symbol_table* symtab, Layout* layout,
1136 Dirsearch* dirsearch, Input_objects* input_objects,
1137 Input_group* input_group,
1138 const Input_argument* input_argument,
1139 Input_file* input_file, const unsigned char*, off_t,
1140 Task_token* this_blocker, Task_token* next_blocker)
1141 {
1142 std::string input_string;
1143 Lex::read_file(input_file, &input_string);
1144
1145 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1146
1147 Parser_closure closure(input_file->filename().c_str(),
1148 input_argument->file().options(),
1149 input_group != NULL,
1150 input_file->is_in_sysroot(),
1151 NULL,
1152 layout->script_options(),
1153 &lex);
1154
1155 if (yyparse(&closure) != 0)
1156 return false;
1157
1158 // THIS_BLOCKER must be clear before we may add anything to the
1159 // symbol table. We are responsible for unblocking NEXT_BLOCKER
1160 // when we are done. We are responsible for deleting THIS_BLOCKER
1161 // when it is unblocked.
1162
1163 if (!closure.saw_inputs())
1164 {
1165 // The script did not add any files to read. Note that we are
1166 // not permitted to call NEXT_BLOCKER->unblock() here even if
1167 // THIS_BLOCKER is NULL, as we do not hold the workqueue lock.
1168 workqueue->queue(new Script_unblock(this_blocker, next_blocker));
1169 return true;
1170 }
1171
1172 for (Input_arguments::const_iterator p = closure.inputs()->begin();
1173 p != closure.inputs()->end();
1174 ++p)
1175 {
1176 Task_token* nb;
1177 if (p + 1 == closure.inputs()->end())
1178 nb = next_blocker;
1179 else
1180 {
1181 nb = new Task_token(true);
1182 nb->add_blocker();
1183 }
1184 workqueue->queue(new Read_symbols(options, input_objects, symtab,
1185 layout, dirsearch, &*p,
1186 input_group, this_blocker, nb));
1187 this_blocker = nb;
1188 }
1189
1190 return true;
1191 }
1192
1193 // Helper function for read_version_script() and
1194 // read_commandline_script(). Processes the given file in the mode
1195 // indicated by first_token and lex_mode.
1196
1197 static bool
1198 read_script_file(const char* filename, Command_line* cmdline,
1199 int first_token, Lex::Mode lex_mode)
1200 {
1201 // TODO: if filename is a relative filename, search for it manually
1202 // using "." + cmdline->options()->search_path() -- not dirsearch.
1203 Dirsearch dirsearch;
1204
1205 // The file locking code wants to record a Task, but we haven't
1206 // started the workqueue yet. This is only for debugging purposes,
1207 // so we invent a fake value.
1208 const Task* task = reinterpret_cast<const Task*>(-1);
1209
1210 Input_file_argument input_argument(filename, false, "",
1211 cmdline->position_dependent_options());
1212 Input_file input_file(&input_argument);
1213 if (!input_file.open(cmdline->options(), dirsearch, task))
1214 return false;
1215
1216 std::string input_string;
1217 Lex::read_file(&input_file, &input_string);
1218
1219 Lex lex(input_string.c_str(), input_string.length(), first_token);
1220 lex.set_mode(lex_mode);
1221
1222 Parser_closure closure(filename,
1223 cmdline->position_dependent_options(),
1224 false,
1225 input_file.is_in_sysroot(),
1226 cmdline,
1227 cmdline->script_options(),
1228 &lex);
1229 if (yyparse(&closure) != 0)
1230 {
1231 input_file.file().unlock(task);
1232 return false;
1233 }
1234
1235 input_file.file().unlock(task);
1236
1237 gold_assert(!closure.saw_inputs());
1238
1239 return true;
1240 }
1241
1242 // FILENAME was found as an argument to --script (-T).
1243 // Read it as a script, and execute its contents immediately.
1244
1245 bool
1246 read_commandline_script(const char* filename, Command_line* cmdline)
1247 {
1248 return read_script_file(filename, cmdline,
1249 PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT);
1250 }
1251
1252 // FILE was found as an argument to --version-script. Read it as a
1253 // version script, and store its contents in
1254 // cmdline->script_options()->version_script_info().
1255
1256 bool
1257 read_version_script(const char* filename, Command_line* cmdline)
1258 {
1259 return read_script_file(filename, cmdline,
1260 PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT);
1261 }
1262
1263 // Implement the --defsym option on the command line. Return true if
1264 // all is well.
1265
1266 bool
1267 Script_options::define_symbol(const char* definition)
1268 {
1269 Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1270 lex.set_mode(Lex::EXPRESSION);
1271
1272 // Dummy value.
1273 Position_dependent_options posdep_options;
1274
1275 Parser_closure closure("command line", posdep_options, false, false, NULL,
1276 this, &lex);
1277
1278 if (yyparse(&closure) != 0)
1279 return false;
1280
1281 gold_assert(!closure.saw_inputs());
1282
1283 return true;
1284 }
1285
1286 // Manage mapping from keywords to the codes expected by the bison
1287 // parser. We construct one global object for each lex mode with
1288 // keywords.
1289
1290 class Keyword_to_parsecode
1291 {
1292 public:
1293 // The structure which maps keywords to parsecodes.
1294 struct Keyword_parsecode
1295 {
1296 // Keyword.
1297 const char* keyword;
1298 // Corresponding parsecode.
1299 int parsecode;
1300 };
1301
1302 Keyword_to_parsecode(const Keyword_parsecode* keywords,
1303 int keyword_count)
1304 : keyword_parsecodes_(keywords), keyword_count_(keyword_count)
1305 { }
1306
1307 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1308 // keyword.
1309 int
1310 keyword_to_parsecode(const char* keyword, size_t len) const;
1311
1312 private:
1313 const Keyword_parsecode* keyword_parsecodes_;
1314 const int keyword_count_;
1315 };
1316
1317 // Mapping from keyword string to keyword parsecode. This array must
1318 // be kept in sorted order. Parsecodes are looked up using bsearch.
1319 // This array must correspond to the list of parsecodes in yyscript.y.
1320
1321 static const Keyword_to_parsecode::Keyword_parsecode
1322 script_keyword_parsecodes[] =
1323 {
1324 { "ABSOLUTE", ABSOLUTE },
1325 { "ADDR", ADDR },
1326 { "ALIGN", ALIGN_K },
1327 { "ALIGNOF", ALIGNOF },
1328 { "ASSERT", ASSERT_K },
1329 { "AS_NEEDED", AS_NEEDED },
1330 { "AT", AT },
1331 { "BIND", BIND },
1332 { "BLOCK", BLOCK },
1333 { "BYTE", BYTE },
1334 { "CONSTANT", CONSTANT },
1335 { "CONSTRUCTORS", CONSTRUCTORS },
1336 { "COPY", COPY },
1337 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1338 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1339 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1340 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1341 { "DEFINED", DEFINED },
1342 { "DSECT", DSECT },
1343 { "ENTRY", ENTRY },
1344 { "EXCLUDE_FILE", EXCLUDE_FILE },
1345 { "EXTERN", EXTERN },
1346 { "FILL", FILL },
1347 { "FLOAT", FLOAT },
1348 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1349 { "GROUP", GROUP },
1350 { "HLL", HLL },
1351 { "INCLUDE", INCLUDE },
1352 { "INFO", INFO },
1353 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1354 { "INPUT", INPUT },
1355 { "KEEP", KEEP },
1356 { "LENGTH", LENGTH },
1357 { "LOADADDR", LOADADDR },
1358 { "LONG", LONG },
1359 { "MAP", MAP },
1360 { "MAX", MAX_K },
1361 { "MEMORY", MEMORY },
1362 { "MIN", MIN_K },
1363 { "NEXT", NEXT },
1364 { "NOCROSSREFS", NOCROSSREFS },
1365 { "NOFLOAT", NOFLOAT },
1366 { "NOLOAD", NOLOAD },
1367 { "ONLY_IF_RO", ONLY_IF_RO },
1368 { "ONLY_IF_RW", ONLY_IF_RW },
1369 { "OPTION", OPTION },
1370 { "ORIGIN", ORIGIN },
1371 { "OUTPUT", OUTPUT },
1372 { "OUTPUT_ARCH", OUTPUT_ARCH },
1373 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1374 { "OVERLAY", OVERLAY },
1375 { "PHDRS", PHDRS },
1376 { "PROVIDE", PROVIDE },
1377 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1378 { "QUAD", QUAD },
1379 { "SEARCH_DIR", SEARCH_DIR },
1380 { "SECTIONS", SECTIONS },
1381 { "SEGMENT_START", SEGMENT_START },
1382 { "SHORT", SHORT },
1383 { "SIZEOF", SIZEOF },
1384 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1385 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1386 { "SORT_BY_NAME", SORT_BY_NAME },
1387 { "SPECIAL", SPECIAL },
1388 { "SQUAD", SQUAD },
1389 { "STARTUP", STARTUP },
1390 { "SUBALIGN", SUBALIGN },
1391 { "SYSLIB", SYSLIB },
1392 { "TARGET", TARGET_K },
1393 { "TRUNCATE", TRUNCATE },
1394 { "VERSION", VERSIONK },
1395 { "global", GLOBAL },
1396 { "l", LENGTH },
1397 { "len", LENGTH },
1398 { "local", LOCAL },
1399 { "o", ORIGIN },
1400 { "org", ORIGIN },
1401 { "sizeof_headers", SIZEOF_HEADERS },
1402 };
1403
1404 static const Keyword_to_parsecode
1405 script_keywords(&script_keyword_parsecodes[0],
1406 (sizeof(script_keyword_parsecodes)
1407 / sizeof(script_keyword_parsecodes[0])));
1408
1409 static const Keyword_to_parsecode::Keyword_parsecode
1410 version_script_keyword_parsecodes[] =
1411 {
1412 { "extern", EXTERN },
1413 { "global", GLOBAL },
1414 { "local", LOCAL },
1415 };
1416
1417 static const Keyword_to_parsecode
1418 version_script_keywords(&version_script_keyword_parsecodes[0],
1419 (sizeof(version_script_keyword_parsecodes)
1420 / sizeof(version_script_keyword_parsecodes[0])));
1421
1422 // Comparison function passed to bsearch.
1423
1424 extern "C"
1425 {
1426
1427 struct Ktt_key
1428 {
1429 const char* str;
1430 size_t len;
1431 };
1432
1433 static int
1434 ktt_compare(const void* keyv, const void* kttv)
1435 {
1436 const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
1437 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1438 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1439 int i = strncmp(key->str, ktt->keyword, key->len);
1440 if (i != 0)
1441 return i;
1442 if (ktt->keyword[key->len] != '\0')
1443 return -1;
1444 return 0;
1445 }
1446
1447 } // End extern "C".
1448
1449 int
1450 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword,
1451 size_t len) const
1452 {
1453 Ktt_key key;
1454 key.str = keyword;
1455 key.len = len;
1456 void* kttv = bsearch(&key,
1457 this->keyword_parsecodes_,
1458 this->keyword_count_,
1459 sizeof(this->keyword_parsecodes_[0]),
1460 ktt_compare);
1461 if (kttv == NULL)
1462 return 0;
1463 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1464 return ktt->parsecode;
1465 }
1466
1467 } // End namespace gold.
1468
1469 // The remaining functions are extern "C", so it's clearer to not put
1470 // them in namespace gold.
1471
1472 using namespace gold;
1473
1474 // This function is called by the bison parser to return the next
1475 // token.
1476
1477 extern "C" int
1478 yylex(YYSTYPE* lvalp, void* closurev)
1479 {
1480 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1481 const Token* token = closure->next_token();
1482 switch (token->classification())
1483 {
1484 default:
1485 gold_unreachable();
1486
1487 case Token::TOKEN_INVALID:
1488 yyerror(closurev, "invalid character");
1489 return 0;
1490
1491 case Token::TOKEN_EOF:
1492 return 0;
1493
1494 case Token::TOKEN_STRING:
1495 {
1496 // This is either a keyword or a STRING.
1497 size_t len;
1498 const char* str = token->string_value(&len);
1499 int parsecode = 0;
1500 switch (closure->lex_mode())
1501 {
1502 case Lex::LINKER_SCRIPT:
1503 parsecode = script_keywords.keyword_to_parsecode(str, len);
1504 break;
1505 case Lex::VERSION_SCRIPT:
1506 parsecode = version_script_keywords.keyword_to_parsecode(str, len);
1507 break;
1508 default:
1509 break;
1510 }
1511 if (parsecode != 0)
1512 return parsecode;
1513 lvalp->string.value = str;
1514 lvalp->string.length = len;
1515 return STRING;
1516 }
1517
1518 case Token::TOKEN_QUOTED_STRING:
1519 lvalp->string.value = token->string_value(&lvalp->string.length);
1520 return QUOTED_STRING;
1521
1522 case Token::TOKEN_OPERATOR:
1523 return token->operator_value();
1524
1525 case Token::TOKEN_INTEGER:
1526 lvalp->integer = token->integer_value();
1527 return INTEGER;
1528 }
1529 }
1530
1531 // This function is called by the bison parser to report an error.
1532
1533 extern "C" void
1534 yyerror(void* closurev, const char* message)
1535 {
1536 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1537 gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
1538 closure->charpos(), message);
1539 }
1540
1541 // Called by the bison parser to add a file to the link.
1542
1543 extern "C" void
1544 script_add_file(void* closurev, const char* name, size_t length)
1545 {
1546 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1547
1548 // If this is an absolute path, and we found the script in the
1549 // sysroot, then we want to prepend the sysroot to the file name.
1550 // For example, this is how we handle a cross link to the x86_64
1551 // libc.so, which refers to /lib/libc.so.6.
1552 std::string name_string(name, length);
1553 const char* extra_search_path = ".";
1554 std::string script_directory;
1555 if (IS_ABSOLUTE_PATH(name_string.c_str()))
1556 {
1557 if (closure->is_in_sysroot())
1558 {
1559 const std::string& sysroot(parameters->sysroot());
1560 gold_assert(!sysroot.empty());
1561 name_string = sysroot + name_string;
1562 }
1563 }
1564 else
1565 {
1566 // In addition to checking the normal library search path, we
1567 // also want to check in the script-directory.
1568 const char *slash = strrchr(closure->filename(), '/');
1569 if (slash != NULL)
1570 {
1571 script_directory.assign(closure->filename(),
1572 slash - closure->filename() + 1);
1573 extra_search_path = script_directory.c_str();
1574 }
1575 }
1576
1577 Input_file_argument file(name_string.c_str(), false, extra_search_path,
1578 closure->position_dependent_options());
1579 closure->inputs()->add_file(file);
1580 }
1581
1582 // Called by the bison parser to start a group. If we are already in
1583 // a group, that means that this script was invoked within a
1584 // --start-group --end-group sequence on the command line, or that
1585 // this script was found in a GROUP of another script. In that case,
1586 // we simply continue the existing group, rather than starting a new
1587 // one. It is possible to construct a case in which this will do
1588 // something other than what would happen if we did a recursive group,
1589 // but it's hard to imagine why the different behaviour would be
1590 // useful for a real program. Avoiding recursive groups is simpler
1591 // and more efficient.
1592
1593 extern "C" void
1594 script_start_group(void* closurev)
1595 {
1596 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1597 if (!closure->in_group())
1598 closure->inputs()->start_group();
1599 }
1600
1601 // Called by the bison parser at the end of a group.
1602
1603 extern "C" void
1604 script_end_group(void* closurev)
1605 {
1606 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1607 if (!closure->in_group())
1608 closure->inputs()->end_group();
1609 }
1610
1611 // Called by the bison parser to start an AS_NEEDED list.
1612
1613 extern "C" void
1614 script_start_as_needed(void* closurev)
1615 {
1616 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1617 closure->position_dependent_options().set_as_needed();
1618 }
1619
1620 // Called by the bison parser at the end of an AS_NEEDED list.
1621
1622 extern "C" void
1623 script_end_as_needed(void* closurev)
1624 {
1625 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1626 closure->position_dependent_options().clear_as_needed();
1627 }
1628
1629 // Called by the bison parser to set the entry symbol.
1630
1631 extern "C" void
1632 script_set_entry(void* closurev, const char* entry, size_t length)
1633 {
1634 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1635 closure->script_options()->set_entry(entry, length);
1636 }
1637
1638 // Called by the bison parser to define a symbol.
1639
1640 extern "C" void
1641 script_set_symbol(void* closurev, const char* name, size_t length,
1642 Expression* value, int providei, int hiddeni)
1643 {
1644 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1645 const bool provide = providei != 0;
1646 const bool hidden = hiddeni != 0;
1647 closure->script_options()->add_symbol_assignment(name, length, value,
1648 provide, hidden);
1649 }
1650
1651 // Called by the bison parser to parse an OPTION.
1652
1653 extern "C" void
1654 script_parse_option(void* closurev, const char* option, size_t length)
1655 {
1656 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1657 // We treat the option as a single command-line option, even if
1658 // it has internal whitespace.
1659 if (closure->command_line() == NULL)
1660 {
1661 // There are some options that we could handle here--e.g.,
1662 // -lLIBRARY. Should we bother?
1663 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
1664 " for scripts specified via -T/--script"),
1665 closure->filename(), closure->lineno(), closure->charpos());
1666 }
1667 else
1668 {
1669 bool past_a_double_dash_option = false;
1670 char* mutable_option = strndup(option, length);
1671 gold_assert(mutable_option != NULL);
1672 closure->command_line()->process_one_option(1, &mutable_option, 0,
1673 &past_a_double_dash_option);
1674 free(mutable_option);
1675 }
1676 }
1677
1678 /* Called by the bison parser to push the lexer into expression
1679 mode. */
1680
1681 extern void
1682 script_push_lex_into_expression_mode(void* closurev)
1683 {
1684 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1685 closure->push_lex_mode(Lex::EXPRESSION);
1686 }
1687
1688 /* Called by the bison parser to push the lexer into version
1689 mode. */
1690
1691 extern void
1692 script_push_lex_into_version_mode(void* closurev)
1693 {
1694 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1695 closure->push_lex_mode(Lex::VERSION_SCRIPT);
1696 }
1697
1698 /* Called by the bison parser to pop the lexer mode. */
1699
1700 extern void
1701 script_pop_lex_mode(void* closurev)
1702 {
1703 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1704 closure->pop_lex_mode();
1705 }
1706
1707 // The following structs are used within the VersionInfo class as well
1708 // as in the bison helper functions. They store the information
1709 // parsed from the version script.
1710
1711 // A single version expression.
1712 // For example, pattern="std::map*" and language="C++".
1713 // pattern and language should be from the stringpool
1714 struct Version_expression {
1715 Version_expression(const std::string& pattern,
1716 const std::string& language,
1717 bool exact_match)
1718 : pattern(pattern), language(language), exact_match(exact_match) {}
1719
1720 std::string pattern;
1721 std::string language;
1722 // If false, we use glob() to match pattern. If true, we use strcmp().
1723 bool exact_match;
1724 };
1725
1726
1727 // A list of expressions.
1728 struct Version_expression_list {
1729 std::vector<struct Version_expression> expressions;
1730 };
1731
1732
1733 // A list of which versions upon which another version depends.
1734 // Strings should be from the Stringpool.
1735 struct Version_dependency_list {
1736 std::vector<std::string> dependencies;
1737 };
1738
1739
1740 // The total definition of a version. It includes the tag for the
1741 // version, its global and local expressions, and any dependencies.
1742 struct Version_tree {
1743 Version_tree()
1744 : tag(), global(NULL), local(NULL), dependencies(NULL) {}
1745
1746 std::string tag;
1747 const struct Version_expression_list* global;
1748 const struct Version_expression_list* local;
1749 const struct Version_dependency_list* dependencies;
1750 };
1751
1752 Version_script_info::~Version_script_info()
1753 {
1754 for (size_t k = 0; k < dependency_lists_.size(); ++k)
1755 delete dependency_lists_[k];
1756 for (size_t k = 0; k < version_trees_.size(); ++k)
1757 delete version_trees_[k];
1758 for (size_t k = 0; k < expression_lists_.size(); ++k)
1759 delete expression_lists_[k];
1760 }
1761
1762 std::vector<std::string>
1763 Version_script_info::get_versions() const
1764 {
1765 std::vector<std::string> ret;
1766 for (size_t j = 0; j < version_trees_.size(); ++j)
1767 ret.push_back(version_trees_[j]->tag);
1768 return ret;
1769 }
1770
1771 std::vector<std::string>
1772 Version_script_info::get_dependencies(const char* version) const
1773 {
1774 std::vector<std::string> ret;
1775 for (size_t j = 0; j < version_trees_.size(); ++j)
1776 if (version_trees_[j]->tag == version)
1777 {
1778 const struct Version_dependency_list* deps =
1779 version_trees_[j]->dependencies;
1780 if (deps != NULL)
1781 for (size_t k = 0; k < deps->dependencies.size(); ++k)
1782 ret.push_back(deps->dependencies[k]);
1783 return ret;
1784 }
1785 return ret;
1786 }
1787
1788 const std::string&
1789 Version_script_info::get_symbol_version_helper(const char* symbol_name,
1790 bool check_global) const
1791 {
1792 for (size_t j = 0; j < version_trees_.size(); ++j)
1793 {
1794 // Is it a global symbol for this version?
1795 const Version_expression_list* explist =
1796 check_global ? version_trees_[j]->global : version_trees_[j]->local;
1797 if (explist != NULL)
1798 for (size_t k = 0; k < explist->expressions.size(); ++k)
1799 {
1800 const char* name_to_match = symbol_name;
1801 const struct Version_expression& exp = explist->expressions[k];
1802 char* demangled_name = NULL;
1803 if (exp.language == "C++")
1804 {
1805 demangled_name = cplus_demangle(symbol_name,
1806 DMGL_ANSI | DMGL_PARAMS);
1807 // This isn't a C++ symbol.
1808 if (demangled_name == NULL)
1809 continue;
1810 name_to_match = demangled_name;
1811 }
1812 else if (exp.language == "Java")
1813 {
1814 demangled_name = cplus_demangle(symbol_name,
1815 (DMGL_ANSI | DMGL_PARAMS
1816 | DMGL_JAVA));
1817 // This isn't a Java symbol.
1818 if (demangled_name == NULL)
1819 continue;
1820 name_to_match = demangled_name;
1821 }
1822 bool matched;
1823 if (exp.exact_match)
1824 matched = strcmp(exp.pattern.c_str(), name_to_match) == 0;
1825 else
1826 matched = fnmatch(exp.pattern.c_str(), name_to_match,
1827 FNM_NOESCAPE) == 0;
1828 if (demangled_name != NULL)
1829 free(demangled_name);
1830 if (matched)
1831 return version_trees_[j]->tag;
1832 }
1833 }
1834 static const std::string empty = "";
1835 return empty;
1836 }
1837
1838 struct Version_dependency_list*
1839 Version_script_info::allocate_dependency_list()
1840 {
1841 dependency_lists_.push_back(new Version_dependency_list);
1842 return dependency_lists_.back();
1843 }
1844
1845 struct Version_expression_list*
1846 Version_script_info::allocate_expression_list()
1847 {
1848 expression_lists_.push_back(new Version_expression_list);
1849 return expression_lists_.back();
1850 }
1851
1852 struct Version_tree*
1853 Version_script_info::allocate_version_tree()
1854 {
1855 version_trees_.push_back(new Version_tree);
1856 return version_trees_.back();
1857 }
1858
1859 // Register an entire version node. For example:
1860 //
1861 // GLIBC_2.1 {
1862 // global: foo;
1863 // } GLIBC_2.0;
1864 //
1865 // - tag is "GLIBC_2.1"
1866 // - tree contains the information "global: foo"
1867 // - deps contains "GLIBC_2.0"
1868
1869 extern "C" void
1870 script_register_vers_node(void*,
1871 const char* tag,
1872 int taglen,
1873 struct Version_tree *tree,
1874 struct Version_dependency_list *deps)
1875 {
1876 gold_assert(tree != NULL);
1877 gold_assert(tag != NULL);
1878 tree->dependencies = deps;
1879 tree->tag = std::string(tag, taglen);
1880 }
1881
1882 // Add a dependencies to the list of existing dependencies, if any,
1883 // and return the expanded list.
1884
1885 extern "C" struct Version_dependency_list *
1886 script_add_vers_depend(void* closurev,
1887 struct Version_dependency_list *all_deps,
1888 const char *depend_to_add, int deplen)
1889 {
1890 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1891 if (all_deps == NULL)
1892 all_deps = closure->version_script()->allocate_dependency_list();
1893 all_deps->dependencies.push_back(std::string(depend_to_add, deplen));
1894 return all_deps;
1895 }
1896
1897 // Add a pattern expression to an existing list of expressions, if any.
1898 // TODO: In the old linker, the last argument used to be a bool, but I
1899 // don't know what it meant.
1900
1901 extern "C" struct Version_expression_list *
1902 script_new_vers_pattern(void* closurev,
1903 struct Version_expression_list *expressions,
1904 const char *pattern, int patlen, int exact_match)
1905 {
1906 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1907 if (expressions == NULL)
1908 expressions = closure->version_script()->allocate_expression_list();
1909 expressions->expressions.push_back(
1910 Version_expression(std::string(pattern, patlen),
1911 closure->get_current_language(),
1912 static_cast<bool>(exact_match)));
1913 return expressions;
1914 }
1915
1916 // Attaches b to the end of a, and clears b. So a = a + b and b = {}.
1917
1918 extern "C" struct Version_expression_list*
1919 script_merge_expressions(struct Version_expression_list *a,
1920 struct Version_expression_list *b)
1921 {
1922 a->expressions.insert(a->expressions.end(),
1923 b->expressions.begin(), b->expressions.end());
1924 // We could delete b and remove it from expressions_lists_, but
1925 // that's a lot of work. This works just as well.
1926 b->expressions.clear();
1927 return a;
1928 }
1929
1930 // Combine the global and local expressions into a a Version_tree.
1931
1932 extern "C" struct Version_tree *
1933 script_new_vers_node(void* closurev,
1934 struct Version_expression_list *global,
1935 struct Version_expression_list *local)
1936 {
1937 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1938 Version_tree* tree = closure->version_script()->allocate_version_tree();
1939 tree->global = global;
1940 tree->local = local;
1941 return tree;
1942 }
1943
1944 // Handle a transition in language, such as at the
1945 // start or end of 'extern "C++"'
1946
1947 extern "C" void
1948 version_script_push_lang(void* closurev, const char* lang, int langlen)
1949 {
1950 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1951 closure->push_language(std::string(lang, langlen));
1952 }
1953
1954 extern "C" void
1955 version_script_pop_lang(void* closurev)
1956 {
1957 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1958 closure->pop_language();
1959 }
This page took 0.06994 seconds and 4 git commands to generate.