2008-02-18 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / script.cc
blob321311570d6c02b25a5b4566c842881a6e04beb1
1 // script.cc -- handle linker scripts for gold.
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
23 #include "gold.h"
25 #include <fnmatch.h>
26 #include <string>
27 #include <vector>
28 #include <cstdio>
29 #include <cstdlib>
30 #include "filenames.h"
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"
45 namespace gold
48 // A token read from a script file. We don't implement keywords here;
49 // all keywords are simply represented as a string.
51 class Token
53 public:
54 // Token classification.
55 enum Classification
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
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 { }
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)
82 gold_assert(classification == TOKEN_INVALID
83 || classification == TOKEN_EOF);
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)
92 gold_assert(classification != TOKEN_INVALID
93 && classification != TOKEN_EOF);
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)
102 // Return whether the token is invalid.
103 bool
104 is_invalid() const
105 { return this->classification_ == TOKEN_INVALID; }
107 // Return whether this is an EOF token.
108 bool
109 is_eof() const
110 { return this->classification_ == TOKEN_EOF; }
112 // Return the token classification.
113 Classification
114 classification() const
115 { return this->classification_; }
117 // Return the line number at which the token starts.
119 lineno() const
120 { return this->lineno_; }
122 // Return the character position at this the token starts.
124 charpos() const
125 { return this->charpos_; }
127 // Get the value of a token.
129 const char*
130 string_value(size_t* length) const
132 gold_assert(this->classification_ == TOKEN_STRING
133 || this->classification_ == TOKEN_QUOTED_STRING);
134 *length = this->value_length_;
135 return this->value_;
139 operator_value() const
141 gold_assert(this->classification_ == TOKEN_OPERATOR);
142 return this->opcode_;
145 uint64_t
146 integer_value() const
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);
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_;
171 // This class handles lexing a file into a sequence of tokens.
173 class Lex
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
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
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)
196 // Read a file into a string.
197 static void
198 read_file(Input_file*, std::string*);
200 // Return the next token.
201 const Token*
202 next_token();
204 // Return the current lexing mode.
205 Lex::Mode
206 mode() const
207 { return this->mode_; }
209 // Set the lexing mode.
210 void
211 set_mode(Mode mode)
212 { this->mode_ = mode; }
214 private:
215 Lex(const Lex&);
216 Lex& operator=(const Lex&);
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); }
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); }
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); }
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); }
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); }
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);
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);
256 // Return whether C, C2, C3 can start a hex number.
257 inline bool
258 can_start_hex(char c, char c2, char c3);
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);
266 // Return whether C can start a non-hex number.
267 static inline bool
268 can_start_number(char c);
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; }
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);
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);
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);
292 // Read the next token.
293 Token
294 get_token(const char**);
296 // Skip a C style /* */ comment. Return false if the comment did
297 // not end.
298 bool
299 skip_c_comment(const char**);
301 // Skip a line # comment. Return false if there was no newline.
302 bool
303 skip_line_comment(const char**);
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);
313 // Build a token from a quoted string.
314 Token
315 gather_quoted_string(const char** pp);
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_;
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.
340 void
341 Lex::read_file(Input_file* input_file, std::string* contents)
343 off_t filesize = input_file->file().filesize();
344 contents->clear();
345 contents->reserve(filesize);
347 off_t off = 0;
348 unsigned char buf[BUFSIZ];
349 while (off < filesize)
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;
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.
371 inline bool
372 Lex::can_start_name(char c, char c2)
374 switch (c)
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;
389 case '/': case '\\':
390 return this->mode_ == LINKER_SCRIPT;
392 case '~':
393 return this->mode_ == LINKER_SCRIPT && can_continue_name(&c2);
395 case '*': case '[':
396 return (this->mode_ == VERSION_SCRIPT
397 || (this->mode_ == LINKER_SCRIPT
398 && can_continue_name(&c2)));
400 default:
401 return false;
405 // Return whether C can continue a name which has already started.
406 // Subsequent characters in a name are the same as the leading
407 // characters, plus digits and "=+-:[],?*". So in general the linker
408 // script language requires spaces around operators, unless we know
409 // that we are parsing an expression.
411 inline const char*
412 Lex::can_continue_name(const char* c)
414 switch (*c)
416 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
417 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
418 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
419 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
420 case 'Y': case 'Z':
421 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
422 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
423 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
424 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
425 case 'y': case 'z':
426 case '_': case '.': case '$':
427 case '0': case '1': case '2': case '3': case '4':
428 case '5': case '6': case '7': case '8': case '9':
429 return c + 1;
431 case '/': case '\\': case '~':
432 case '=': case '+':
433 case ',': case '?':
434 if (this->mode_ == LINKER_SCRIPT)
435 return c + 1;
436 return NULL;
438 case '[': case ']': case '*': case '-':
439 if (this->mode_ == LINKER_SCRIPT || this->mode_ == VERSION_SCRIPT)
440 return c + 1;
441 return NULL;
443 case '^':
444 if (this->mode_ == VERSION_SCRIPT)
445 return c + 1;
446 return NULL;
448 case ':':
449 if (this->mode_ == LINKER_SCRIPT)
450 return c + 1;
451 else if (this->mode_ == VERSION_SCRIPT && (c[1] == ':'))
453 // A name can have '::' in it, as that's a c++ namespace
454 // separator. But a single colon is not part of a name.
455 return c + 2;
457 return NULL;
459 default:
460 return NULL;
464 // For a number we accept 0x followed by hex digits, or any sequence
465 // of digits. The old linker accepts leading '$' for hex, and
466 // trailing HXBOD. Those are for MRI compatibility and we don't
467 // accept them. The old linker also accepts trailing MK for mega or
468 // kilo. FIXME: Those are mentioned in the documentation, and we
469 // should accept them.
471 // Return whether C1 C2 C3 can start a hex number.
473 inline bool
474 Lex::can_start_hex(char c1, char c2, char c3)
476 if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
477 return this->can_continue_hex(&c3);
478 return false;
481 // Return whether C can appear in a hex number.
483 inline const char*
484 Lex::can_continue_hex(const char* c)
486 switch (*c)
488 case '0': case '1': case '2': case '3': case '4':
489 case '5': case '6': case '7': case '8': case '9':
490 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
491 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
492 return c + 1;
494 default:
495 return NULL;
499 // Return whether C can start a non-hex number.
501 inline bool
502 Lex::can_start_number(char c)
504 switch (c)
506 case '0': case '1': case '2': case '3': case '4':
507 case '5': case '6': case '7': case '8': case '9':
508 return true;
510 default:
511 return false;
515 // If C1 C2 C3 form a valid three character operator, return the
516 // opcode (defined in the yyscript.h file generated from yyscript.y).
517 // Otherwise return 0.
519 inline int
520 Lex::three_char_operator(char c1, char c2, char c3)
522 switch (c1)
524 case '<':
525 if (c2 == '<' && c3 == '=')
526 return LSHIFTEQ;
527 break;
528 case '>':
529 if (c2 == '>' && c3 == '=')
530 return RSHIFTEQ;
531 break;
532 default:
533 break;
535 return 0;
538 // If C1 C2 form a valid two character operator, return the opcode
539 // (defined in the yyscript.h file generated from yyscript.y).
540 // Otherwise return 0.
542 inline int
543 Lex::two_char_operator(char c1, char c2)
545 switch (c1)
547 case '=':
548 if (c2 == '=')
549 return EQ;
550 break;
551 case '!':
552 if (c2 == '=')
553 return NE;
554 break;
555 case '+':
556 if (c2 == '=')
557 return PLUSEQ;
558 break;
559 case '-':
560 if (c2 == '=')
561 return MINUSEQ;
562 break;
563 case '*':
564 if (c2 == '=')
565 return MULTEQ;
566 break;
567 case '/':
568 if (c2 == '=')
569 return DIVEQ;
570 break;
571 case '|':
572 if (c2 == '=')
573 return OREQ;
574 if (c2 == '|')
575 return OROR;
576 break;
577 case '&':
578 if (c2 == '=')
579 return ANDEQ;
580 if (c2 == '&')
581 return ANDAND;
582 break;
583 case '>':
584 if (c2 == '=')
585 return GE;
586 if (c2 == '>')
587 return RSHIFT;
588 break;
589 case '<':
590 if (c2 == '=')
591 return LE;
592 if (c2 == '<')
593 return LSHIFT;
594 break;
595 default:
596 break;
598 return 0;
601 // If C1 is a valid operator, return the opcode. Otherwise return 0.
603 inline int
604 Lex::one_char_operator(char c1)
606 switch (c1)
608 case '+':
609 case '-':
610 case '*':
611 case '/':
612 case '%':
613 case '!':
614 case '&':
615 case '|':
616 case '^':
617 case '~':
618 case '<':
619 case '>':
620 case '=':
621 case '?':
622 case ',':
623 case '(':
624 case ')':
625 case '{':
626 case '}':
627 case '[':
628 case ']':
629 case ':':
630 case ';':
631 return c1;
632 default:
633 return 0;
637 // Skip a C style comment. *PP points to just after the "/*". Return
638 // false if the comment did not end.
640 bool
641 Lex::skip_c_comment(const char** pp)
643 const char* p = *pp;
644 while (p[0] != '*' || p[1] != '/')
646 if (*p == '\0')
648 *pp = p;
649 return false;
652 if (*p == '\n')
654 ++this->lineno_;
655 this->linestart_ = p + 1;
657 ++p;
660 *pp = p + 2;
661 return true;
664 // Skip a line # comment. Return false if there was no newline.
666 bool
667 Lex::skip_line_comment(const char** pp)
669 const char* p = *pp;
670 size_t skip = strcspn(p, "\n");
671 if (p[skip] == '\0')
673 *pp = p + skip;
674 return false;
677 p += skip + 1;
678 ++this->lineno_;
679 this->linestart_ = p;
680 *pp = p;
682 return true;
685 // Build a token CLASSIFICATION from all characters that match
686 // CAN_CONTINUE_FN. Update *PP.
688 inline Token
689 Lex::gather_token(Token::Classification classification,
690 const char* (Lex::*can_continue_fn)(const char*),
691 const char* start,
692 const char* match,
693 const char **pp)
695 const char* new_match = NULL;
696 while ((new_match = (this->*can_continue_fn)(match)))
697 match = new_match;
698 *pp = match;
699 return this->make_token(classification, start, match - start, start);
702 // Build a token from a quoted string.
704 Token
705 Lex::gather_quoted_string(const char** pp)
707 const char* start = *pp;
708 const char* p = start;
709 ++p;
710 size_t skip = strcspn(p, "\"\n");
711 if (p[skip] != '"')
712 return this->make_invalid_token(start);
713 *pp = p + skip + 1;
714 return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
717 // Return the next token at *PP. Update *PP. General guideline: we
718 // require linker scripts to be simple ASCII. No unicode linker
719 // scripts. In particular we can assume that any '\0' is the end of
720 // the input.
722 Token
723 Lex::get_token(const char** pp)
725 const char* p = *pp;
727 while (true)
729 if (*p == '\0')
731 *pp = p;
732 return this->make_eof_token(p);
735 // Skip whitespace quickly.
736 while (*p == ' ' || *p == '\t')
737 ++p;
739 if (*p == '\n')
741 ++p;
742 ++this->lineno_;
743 this->linestart_ = p;
744 continue;
747 // Skip C style comments.
748 if (p[0] == '/' && p[1] == '*')
750 int lineno = this->lineno_;
751 int charpos = p - this->linestart_ + 1;
753 *pp = p + 2;
754 if (!this->skip_c_comment(pp))
755 return Token(Token::TOKEN_INVALID, lineno, charpos);
756 p = *pp;
758 continue;
761 // Skip line comments.
762 if (*p == '#')
764 *pp = p + 1;
765 if (!this->skip_line_comment(pp))
766 return this->make_eof_token(p);
767 p = *pp;
768 continue;
771 // Check for a name.
772 if (this->can_start_name(p[0], p[1]))
773 return this->gather_token(Token::TOKEN_STRING,
774 &Lex::can_continue_name,
775 p, p + 1, pp);
777 // We accept any arbitrary name in double quotes, as long as it
778 // does not cross a line boundary.
779 if (*p == '"')
781 *pp = p;
782 return this->gather_quoted_string(pp);
785 // Check for a number.
787 if (this->can_start_hex(p[0], p[1], p[2]))
788 return this->gather_token(Token::TOKEN_INTEGER,
789 &Lex::can_continue_hex,
790 p, p + 3, pp);
792 if (Lex::can_start_number(p[0]))
793 return this->gather_token(Token::TOKEN_INTEGER,
794 &Lex::can_continue_number,
795 p, p + 1, pp);
797 // Check for operators.
799 int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
800 if (opcode != 0)
802 *pp = p + 3;
803 return this->make_token(opcode, p);
806 opcode = Lex::two_char_operator(p[0], p[1]);
807 if (opcode != 0)
809 *pp = p + 2;
810 return this->make_token(opcode, p);
813 opcode = Lex::one_char_operator(p[0]);
814 if (opcode != 0)
816 *pp = p + 1;
817 return this->make_token(opcode, p);
820 return this->make_token(Token::TOKEN_INVALID, p);
824 // Return the next token.
826 const Token*
827 Lex::next_token()
829 // The first token is special.
830 if (this->first_token_ != 0)
832 this->token_ = Token(this->first_token_, 0, 0);
833 this->first_token_ = 0;
834 return &this->token_;
837 this->token_ = this->get_token(&this->current_);
839 // Don't let an early null byte fool us into thinking that we've
840 // reached the end of the file.
841 if (this->token_.is_eof()
842 && (static_cast<size_t>(this->current_ - this->input_string_)
843 < this->input_length_))
844 this->token_ = this->make_invalid_token(this->current_);
846 return &this->token_;
849 // A trivial task which waits for THIS_BLOCKER to be clear and then
850 // clears NEXT_BLOCKER. THIS_BLOCKER may be NULL.
852 class Script_unblock : public Task
854 public:
855 Script_unblock(Task_token* this_blocker, Task_token* next_blocker)
856 : this_blocker_(this_blocker), next_blocker_(next_blocker)
859 ~Script_unblock()
861 if (this->this_blocker_ != NULL)
862 delete this->this_blocker_;
865 Task_token*
866 is_runnable()
868 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
869 return this->this_blocker_;
870 return NULL;
873 void
874 locks(Task_locker* tl)
875 { tl->add(this, this->next_blocker_); }
877 void
878 run(Workqueue*)
881 std::string
882 get_name() const
883 { return "Script_unblock"; }
885 private:
886 Task_token* this_blocker_;
887 Task_token* next_blocker_;
890 // class Symbol_assignment.
892 // Add the symbol to the symbol table. This makes sure the symbol is
893 // there and defined. The actual value is stored later. We can't
894 // determine the actual value at this point, because we can't
895 // necessarily evaluate the expression until all ordinary symbols have
896 // been finalized.
898 // The GNU linker lets symbol assignments in the linker script
899 // silently override defined symbols in object files. We are
900 // compatible. FIXME: Should we issue a warning?
902 void
903 Symbol_assignment::add_to_table(Symbol_table* symtab)
905 elfcpp::STV vis = this->hidden_ ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
906 this->sym_ = symtab->define_as_constant(this->name_.c_str(),
907 NULL, // version
908 0, // value
909 0, // size
910 elfcpp::STT_NOTYPE,
911 elfcpp::STB_GLOBAL,
912 vis,
913 0, // nonvis
914 this->provide_,
915 true); // force_override
918 // Finalize a symbol value.
920 void
921 Symbol_assignment::finalize(Symbol_table* symtab, const Layout* layout)
923 this->finalize_maybe_dot(symtab, layout, false, 0, NULL);
926 // Finalize a symbol value which can refer to the dot symbol.
928 void
929 Symbol_assignment::finalize_with_dot(Symbol_table* symtab,
930 const Layout* layout,
931 uint64_t dot_value,
932 Output_section* dot_section)
934 this->finalize_maybe_dot(symtab, layout, true, dot_value, dot_section);
937 // Finalize a symbol value, internal version.
939 void
940 Symbol_assignment::finalize_maybe_dot(Symbol_table* symtab,
941 const Layout* layout,
942 bool is_dot_available,
943 uint64_t dot_value,
944 Output_section* dot_section)
946 // If we were only supposed to provide this symbol, the sym_ field
947 // will be NULL if the symbol was not referenced.
948 if (this->sym_ == NULL)
950 gold_assert(this->provide_);
951 return;
954 if (parameters->get_size() == 32)
956 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
957 this->sized_finalize<32>(symtab, layout, is_dot_available, dot_value,
958 dot_section);
959 #else
960 gold_unreachable();
961 #endif
963 else if (parameters->get_size() == 64)
965 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
966 this->sized_finalize<64>(symtab, layout, is_dot_available, dot_value,
967 dot_section);
968 #else
969 gold_unreachable();
970 #endif
972 else
973 gold_unreachable();
976 template<int size>
977 void
978 Symbol_assignment::sized_finalize(Symbol_table* symtab, const Layout* layout,
979 bool is_dot_available, uint64_t dot_value,
980 Output_section* dot_section)
982 Output_section* section;
983 uint64_t final_val = this->val_->eval_maybe_dot(symtab, layout,
984 is_dot_available,
985 dot_value, dot_section,
986 &section);
987 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(this->sym_);
988 ssym->set_value(final_val);
989 if (section != NULL)
990 ssym->set_output_section(section);
993 // Set the symbol value if the expression yields an absolute value.
995 void
996 Symbol_assignment::set_if_absolute(Symbol_table* symtab, const Layout* layout,
997 bool is_dot_available, uint64_t dot_value)
999 if (this->sym_ == NULL)
1000 return;
1002 Output_section* val_section;
1003 uint64_t val = this->val_->eval_maybe_dot(symtab, layout, is_dot_available,
1004 dot_value, NULL, &val_section);
1005 if (val_section != NULL)
1006 return;
1008 if (parameters->get_size() == 32)
1010 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1011 Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(this->sym_);
1012 ssym->set_value(val);
1013 #else
1014 gold_unreachable();
1015 #endif
1017 else if (parameters->get_size() == 64)
1019 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1020 Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(this->sym_);
1021 ssym->set_value(val);
1022 #else
1023 gold_unreachable();
1024 #endif
1026 else
1027 gold_unreachable();
1030 // Print for debugging.
1032 void
1033 Symbol_assignment::print(FILE* f) const
1035 if (this->provide_ && this->hidden_)
1036 fprintf(f, "PROVIDE_HIDDEN(");
1037 else if (this->provide_)
1038 fprintf(f, "PROVIDE(");
1039 else if (this->hidden_)
1040 gold_unreachable();
1042 fprintf(f, "%s = ", this->name_.c_str());
1043 this->val_->print(f);
1045 if (this->provide_ || this->hidden_)
1046 fprintf(f, ")");
1048 fprintf(f, "\n");
1051 // Class Script_assertion.
1053 // Check the assertion.
1055 void
1056 Script_assertion::check(const Symbol_table* symtab, const Layout* layout)
1058 if (!this->check_->eval(symtab, layout))
1059 gold_error("%s", this->message_.c_str());
1062 // Print for debugging.
1064 void
1065 Script_assertion::print(FILE* f) const
1067 fprintf(f, "ASSERT(");
1068 this->check_->print(f);
1069 fprintf(f, ", \"%s\")\n", this->message_.c_str());
1072 // Class Script_options.
1074 Script_options::Script_options()
1075 : entry_(), symbol_assignments_(), version_script_info_(),
1076 script_sections_()
1080 // Add a symbol to be defined.
1082 void
1083 Script_options::add_symbol_assignment(const char* name, size_t length,
1084 Expression* value, bool provide,
1085 bool hidden)
1087 if (length != 1 || name[0] != '.')
1089 if (this->script_sections_.in_sections_clause())
1090 this->script_sections_.add_symbol_assignment(name, length, value,
1091 provide, hidden);
1092 else
1094 Symbol_assignment* p = new Symbol_assignment(name, length, value,
1095 provide, hidden);
1096 this->symbol_assignments_.push_back(p);
1099 else
1101 if (provide || hidden)
1102 gold_error(_("invalid use of PROVIDE for dot symbol"));
1103 if (!this->script_sections_.in_sections_clause())
1104 gold_error(_("invalid assignment to dot outside of SECTIONS"));
1105 else
1106 this->script_sections_.add_dot_assignment(value);
1110 // Add an assertion.
1112 void
1113 Script_options::add_assertion(Expression* check, const char* message,
1114 size_t messagelen)
1116 if (this->script_sections_.in_sections_clause())
1117 this->script_sections_.add_assertion(check, message, messagelen);
1118 else
1120 Script_assertion* p = new Script_assertion(check, message, messagelen);
1121 this->assertions_.push_back(p);
1125 // Add any symbols we are defining to the symbol table.
1127 void
1128 Script_options::add_symbols_to_table(Symbol_table* symtab)
1130 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1131 p != this->symbol_assignments_.end();
1132 ++p)
1133 (*p)->add_to_table(symtab);
1134 this->script_sections_.add_symbols_to_table(symtab);
1137 // Finalize symbol values. Also check assertions.
1139 void
1140 Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
1142 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1143 p != this->symbol_assignments_.end();
1144 ++p)
1145 (*p)->finalize(symtab, layout);
1147 for (Assertions::iterator p = this->assertions_.begin();
1148 p != this->assertions_.end();
1149 ++p)
1150 (*p)->check(symtab, layout);
1152 this->script_sections_.finalize_symbols(symtab, layout);
1155 // Set section addresses. We set all the symbols which have absolute
1156 // values. Then we let the SECTIONS clause do its thing. This
1157 // returns the segment which holds the file header and segment
1158 // headers, if any.
1160 Output_segment*
1161 Script_options::set_section_addresses(Symbol_table* symtab, Layout* layout)
1163 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1164 p != this->symbol_assignments_.end();
1165 ++p)
1166 (*p)->set_if_absolute(symtab, layout, false, 0);
1168 return this->script_sections_.set_section_addresses(symtab, layout);
1171 // This class holds data passed through the parser to the lexer and to
1172 // the parser support functions. This avoids global variables. We
1173 // can't use global variables because we need not be called by a
1174 // singleton thread.
1176 class Parser_closure
1178 public:
1179 Parser_closure(const char* filename,
1180 const Position_dependent_options& posdep_options,
1181 bool in_group, bool is_in_sysroot,
1182 Command_line* command_line,
1183 Script_options* script_options,
1184 Lex* lex)
1185 : filename_(filename), posdep_options_(posdep_options),
1186 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
1187 command_line_(command_line), script_options_(script_options),
1188 version_script_info_(script_options->version_script_info()),
1189 lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL)
1191 // We start out processing C symbols in the default lex mode.
1192 language_stack_.push_back("");
1193 lex_mode_stack_.push_back(lex->mode());
1196 // Return the file name.
1197 const char*
1198 filename() const
1199 { return this->filename_; }
1201 // Return the position dependent options. The caller may modify
1202 // this.
1203 Position_dependent_options&
1204 position_dependent_options()
1205 { return this->posdep_options_; }
1207 // Return whether this script is being run in a group.
1208 bool
1209 in_group() const
1210 { return this->in_group_; }
1212 // Return whether this script was found using a directory in the
1213 // sysroot.
1214 bool
1215 is_in_sysroot() const
1216 { return this->is_in_sysroot_; }
1218 // Returns the Command_line structure passed in at constructor time.
1219 // This value may be NULL. The caller may modify this, which modifies
1220 // the passed-in Command_line object (not a copy).
1221 Command_line*
1222 command_line()
1223 { return this->command_line_; }
1225 // Return the options which may be set by a script.
1226 Script_options*
1227 script_options()
1228 { return this->script_options_; }
1230 // Return the object in which version script information should be stored.
1231 Version_script_info*
1232 version_script()
1233 { return this->version_script_info_; }
1235 // Return the next token, and advance.
1236 const Token*
1237 next_token()
1239 const Token* token = this->lex_->next_token();
1240 this->lineno_ = token->lineno();
1241 this->charpos_ = token->charpos();
1242 return token;
1245 // Set a new lexer mode, pushing the current one.
1246 void
1247 push_lex_mode(Lex::Mode mode)
1249 this->lex_mode_stack_.push_back(this->lex_->mode());
1250 this->lex_->set_mode(mode);
1253 // Pop the lexer mode.
1254 void
1255 pop_lex_mode()
1257 gold_assert(!this->lex_mode_stack_.empty());
1258 this->lex_->set_mode(this->lex_mode_stack_.back());
1259 this->lex_mode_stack_.pop_back();
1262 // Return the current lexer mode.
1263 Lex::Mode
1264 lex_mode() const
1265 { return this->lex_mode_stack_.back(); }
1267 // Return the line number of the last token.
1269 lineno() const
1270 { return this->lineno_; }
1272 // Return the character position in the line of the last token.
1274 charpos() const
1275 { return this->charpos_; }
1277 // Return the list of input files, creating it if necessary. This
1278 // is a space leak--we never free the INPUTS_ pointer.
1279 Input_arguments*
1280 inputs()
1282 if (this->inputs_ == NULL)
1283 this->inputs_ = new Input_arguments();
1284 return this->inputs_;
1287 // Return whether we saw any input files.
1288 bool
1289 saw_inputs() const
1290 { return this->inputs_ != NULL && !this->inputs_->empty(); }
1292 // Return the current language being processed in a version script
1293 // (eg, "C++"). The empty string represents unmangled C names.
1294 const std::string&
1295 get_current_language() const
1296 { return this->language_stack_.back(); }
1298 // Push a language onto the stack when entering an extern block.
1299 void push_language(const std::string& lang)
1300 { this->language_stack_.push_back(lang); }
1302 // Pop a language off of the stack when exiting an extern block.
1303 void pop_language()
1305 gold_assert(!this->language_stack_.empty());
1306 this->language_stack_.pop_back();
1309 private:
1310 // The name of the file we are reading.
1311 const char* filename_;
1312 // The position dependent options.
1313 Position_dependent_options posdep_options_;
1314 // Whether we are currently in a --start-group/--end-group.
1315 bool in_group_;
1316 // Whether the script was found in a sysrooted directory.
1317 bool is_in_sysroot_;
1318 // May be NULL if the user chooses not to pass one in.
1319 Command_line* command_line_;
1320 // Options which may be set from any linker script.
1321 Script_options* script_options_;
1322 // Information parsed from a version script.
1323 Version_script_info* version_script_info_;
1324 // The lexer.
1325 Lex* lex_;
1326 // The line number of the last token returned by next_token.
1327 int lineno_;
1328 // The column number of the last token returned by next_token.
1329 int charpos_;
1330 // A stack of lexer modes.
1331 std::vector<Lex::Mode> lex_mode_stack_;
1332 // A stack of which extern/language block we're inside. Can be C++,
1333 // java, or empty for C.
1334 std::vector<std::string> language_stack_;
1335 // New input files found to add to the link.
1336 Input_arguments* inputs_;
1339 // FILE was found as an argument on the command line. Try to read it
1340 // as a script. We've already read BYTES of data into P, but we
1341 // ignore that. Return true if the file was handled.
1343 bool
1344 read_input_script(Workqueue* workqueue, const General_options& options,
1345 Symbol_table* symtab, Layout* layout,
1346 Dirsearch* dirsearch, Input_objects* input_objects,
1347 Input_group* input_group,
1348 const Input_argument* input_argument,
1349 Input_file* input_file, const unsigned char*, off_t,
1350 Task_token* this_blocker, Task_token* next_blocker)
1352 std::string input_string;
1353 Lex::read_file(input_file, &input_string);
1355 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1357 Parser_closure closure(input_file->filename().c_str(),
1358 input_argument->file().options(),
1359 input_group != NULL,
1360 input_file->is_in_sysroot(),
1361 NULL,
1362 layout->script_options(),
1363 &lex);
1365 if (yyparse(&closure) != 0)
1366 return false;
1368 // THIS_BLOCKER must be clear before we may add anything to the
1369 // symbol table. We are responsible for unblocking NEXT_BLOCKER
1370 // when we are done. We are responsible for deleting THIS_BLOCKER
1371 // when it is unblocked.
1373 if (!closure.saw_inputs())
1375 // The script did not add any files to read. Note that we are
1376 // not permitted to call NEXT_BLOCKER->unblock() here even if
1377 // THIS_BLOCKER is NULL, as we do not hold the workqueue lock.
1378 workqueue->queue(new Script_unblock(this_blocker, next_blocker));
1379 return true;
1382 for (Input_arguments::const_iterator p = closure.inputs()->begin();
1383 p != closure.inputs()->end();
1384 ++p)
1386 Task_token* nb;
1387 if (p + 1 == closure.inputs()->end())
1388 nb = next_blocker;
1389 else
1391 nb = new Task_token(true);
1392 nb->add_blocker();
1394 workqueue->queue(new Read_symbols(options, input_objects, symtab,
1395 layout, dirsearch, &*p,
1396 input_group, this_blocker, nb));
1397 this_blocker = nb;
1400 return true;
1403 // Helper function for read_version_script() and
1404 // read_commandline_script(). Processes the given file in the mode
1405 // indicated by first_token and lex_mode.
1407 static bool
1408 read_script_file(const char* filename, Command_line* cmdline,
1409 int first_token, Lex::Mode lex_mode)
1411 // TODO: if filename is a relative filename, search for it manually
1412 // using "." + cmdline->options()->search_path() -- not dirsearch.
1413 Dirsearch dirsearch;
1415 // The file locking code wants to record a Task, but we haven't
1416 // started the workqueue yet. This is only for debugging purposes,
1417 // so we invent a fake value.
1418 const Task* task = reinterpret_cast<const Task*>(-1);
1420 // We don't want this file to be opened in binary mode.
1421 Position_dependent_options posdep = cmdline->position_dependent_options();
1422 if (posdep.input_format() == General_options::OBJECT_FORMAT_BINARY)
1423 posdep.set_input_format("elf");
1424 Input_file_argument input_argument(filename, false, "", false, posdep);
1425 Input_file input_file(&input_argument);
1426 if (!input_file.open(cmdline->options(), dirsearch, task))
1427 return false;
1429 std::string input_string;
1430 Lex::read_file(&input_file, &input_string);
1432 Lex lex(input_string.c_str(), input_string.length(), first_token);
1433 lex.set_mode(lex_mode);
1435 Parser_closure closure(filename,
1436 cmdline->position_dependent_options(),
1437 false,
1438 input_file.is_in_sysroot(),
1439 cmdline,
1440 cmdline->script_options(),
1441 &lex);
1442 if (yyparse(&closure) != 0)
1444 input_file.file().unlock(task);
1445 return false;
1448 input_file.file().unlock(task);
1450 gold_assert(!closure.saw_inputs());
1452 return true;
1455 // FILENAME was found as an argument to --script (-T).
1456 // Read it as a script, and execute its contents immediately.
1458 bool
1459 read_commandline_script(const char* filename, Command_line* cmdline)
1461 return read_script_file(filename, cmdline,
1462 PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT);
1465 // FILE was found as an argument to --version-script. Read it as a
1466 // version script, and store its contents in
1467 // cmdline->script_options()->version_script_info().
1469 bool
1470 read_version_script(const char* filename, Command_line* cmdline)
1472 return read_script_file(filename, cmdline,
1473 PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT);
1476 // Implement the --defsym option on the command line. Return true if
1477 // all is well.
1479 bool
1480 Script_options::define_symbol(const char* definition)
1482 Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1483 lex.set_mode(Lex::EXPRESSION);
1485 // Dummy value.
1486 Position_dependent_options posdep_options;
1488 Parser_closure closure("command line", posdep_options, false, false, NULL,
1489 this, &lex);
1491 if (yyparse(&closure) != 0)
1492 return false;
1494 gold_assert(!closure.saw_inputs());
1496 return true;
1499 // Print the script to F for debugging.
1501 void
1502 Script_options::print(FILE* f) const
1504 fprintf(f, "%s: Dumping linker script\n", program_name);
1506 if (!this->entry_.empty())
1507 fprintf(f, "ENTRY(%s)\n", this->entry_.c_str());
1509 for (Symbol_assignments::const_iterator p =
1510 this->symbol_assignments_.begin();
1511 p != this->symbol_assignments_.end();
1512 ++p)
1513 (*p)->print(f);
1515 for (Assertions::const_iterator p = this->assertions_.begin();
1516 p != this->assertions_.end();
1517 ++p)
1518 (*p)->print(f);
1520 this->script_sections_.print(f);
1522 this->version_script_info_.print(f);
1525 // Manage mapping from keywords to the codes expected by the bison
1526 // parser. We construct one global object for each lex mode with
1527 // keywords.
1529 class Keyword_to_parsecode
1531 public:
1532 // The structure which maps keywords to parsecodes.
1533 struct Keyword_parsecode
1535 // Keyword.
1536 const char* keyword;
1537 // Corresponding parsecode.
1538 int parsecode;
1541 Keyword_to_parsecode(const Keyword_parsecode* keywords,
1542 int keyword_count)
1543 : keyword_parsecodes_(keywords), keyword_count_(keyword_count)
1546 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1547 // keyword.
1549 keyword_to_parsecode(const char* keyword, size_t len) const;
1551 private:
1552 const Keyword_parsecode* keyword_parsecodes_;
1553 const int keyword_count_;
1556 // Mapping from keyword string to keyword parsecode. This array must
1557 // be kept in sorted order. Parsecodes are looked up using bsearch.
1558 // This array must correspond to the list of parsecodes in yyscript.y.
1560 static const Keyword_to_parsecode::Keyword_parsecode
1561 script_keyword_parsecodes[] =
1563 { "ABSOLUTE", ABSOLUTE },
1564 { "ADDR", ADDR },
1565 { "ALIGN", ALIGN_K },
1566 { "ALIGNOF", ALIGNOF },
1567 { "ASSERT", ASSERT_K },
1568 { "AS_NEEDED", AS_NEEDED },
1569 { "AT", AT },
1570 { "BIND", BIND },
1571 { "BLOCK", BLOCK },
1572 { "BYTE", BYTE },
1573 { "CONSTANT", CONSTANT },
1574 { "CONSTRUCTORS", CONSTRUCTORS },
1575 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1576 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1577 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1578 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1579 { "DEFINED", DEFINED },
1580 { "ENTRY", ENTRY },
1581 { "EXCLUDE_FILE", EXCLUDE_FILE },
1582 { "EXTERN", EXTERN },
1583 { "FILL", FILL },
1584 { "FLOAT", FLOAT },
1585 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1586 { "GROUP", GROUP },
1587 { "HLL", HLL },
1588 { "INCLUDE", INCLUDE },
1589 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1590 { "INPUT", INPUT },
1591 { "KEEP", KEEP },
1592 { "LENGTH", LENGTH },
1593 { "LOADADDR", LOADADDR },
1594 { "LONG", LONG },
1595 { "MAP", MAP },
1596 { "MAX", MAX_K },
1597 { "MEMORY", MEMORY },
1598 { "MIN", MIN_K },
1599 { "NEXT", NEXT },
1600 { "NOCROSSREFS", NOCROSSREFS },
1601 { "NOFLOAT", NOFLOAT },
1602 { "ONLY_IF_RO", ONLY_IF_RO },
1603 { "ONLY_IF_RW", ONLY_IF_RW },
1604 { "OPTION", OPTION },
1605 { "ORIGIN", ORIGIN },
1606 { "OUTPUT", OUTPUT },
1607 { "OUTPUT_ARCH", OUTPUT_ARCH },
1608 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1609 { "OVERLAY", OVERLAY },
1610 { "PHDRS", PHDRS },
1611 { "PROVIDE", PROVIDE },
1612 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1613 { "QUAD", QUAD },
1614 { "SEARCH_DIR", SEARCH_DIR },
1615 { "SECTIONS", SECTIONS },
1616 { "SEGMENT_START", SEGMENT_START },
1617 { "SHORT", SHORT },
1618 { "SIZEOF", SIZEOF },
1619 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1620 { "SORT", SORT_BY_NAME },
1621 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1622 { "SORT_BY_NAME", SORT_BY_NAME },
1623 { "SPECIAL", SPECIAL },
1624 { "SQUAD", SQUAD },
1625 { "STARTUP", STARTUP },
1626 { "SUBALIGN", SUBALIGN },
1627 { "SYSLIB", SYSLIB },
1628 { "TARGET", TARGET_K },
1629 { "TRUNCATE", TRUNCATE },
1630 { "VERSION", VERSIONK },
1631 { "global", GLOBAL },
1632 { "l", LENGTH },
1633 { "len", LENGTH },
1634 { "local", LOCAL },
1635 { "o", ORIGIN },
1636 { "org", ORIGIN },
1637 { "sizeof_headers", SIZEOF_HEADERS },
1640 static const Keyword_to_parsecode
1641 script_keywords(&script_keyword_parsecodes[0],
1642 (sizeof(script_keyword_parsecodes)
1643 / sizeof(script_keyword_parsecodes[0])));
1645 static const Keyword_to_parsecode::Keyword_parsecode
1646 version_script_keyword_parsecodes[] =
1648 { "extern", EXTERN },
1649 { "global", GLOBAL },
1650 { "local", LOCAL },
1653 static const Keyword_to_parsecode
1654 version_script_keywords(&version_script_keyword_parsecodes[0],
1655 (sizeof(version_script_keyword_parsecodes)
1656 / sizeof(version_script_keyword_parsecodes[0])));
1658 // Comparison function passed to bsearch.
1660 extern "C"
1663 struct Ktt_key
1665 const char* str;
1666 size_t len;
1669 static int
1670 ktt_compare(const void* keyv, const void* kttv)
1672 const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
1673 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1674 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1675 int i = strncmp(key->str, ktt->keyword, key->len);
1676 if (i != 0)
1677 return i;
1678 if (ktt->keyword[key->len] != '\0')
1679 return -1;
1680 return 0;
1683 } // End extern "C".
1686 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword,
1687 size_t len) const
1689 Ktt_key key;
1690 key.str = keyword;
1691 key.len = len;
1692 void* kttv = bsearch(&key,
1693 this->keyword_parsecodes_,
1694 this->keyword_count_,
1695 sizeof(this->keyword_parsecodes_[0]),
1696 ktt_compare);
1697 if (kttv == NULL)
1698 return 0;
1699 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1700 return ktt->parsecode;
1703 // The following structs are used within the VersionInfo class as well
1704 // as in the bison helper functions. They store the information
1705 // parsed from the version script.
1707 // A single version expression.
1708 // For example, pattern="std::map*" and language="C++".
1709 // pattern and language should be from the stringpool
1710 struct Version_expression {
1711 Version_expression(const std::string& pattern,
1712 const std::string& language,
1713 bool exact_match)
1714 : pattern(pattern), language(language), exact_match(exact_match) {}
1716 std::string pattern;
1717 std::string language;
1718 // If false, we use glob() to match pattern. If true, we use strcmp().
1719 bool exact_match;
1723 // A list of expressions.
1724 struct Version_expression_list {
1725 std::vector<struct Version_expression> expressions;
1729 // A list of which versions upon which another version depends.
1730 // Strings should be from the Stringpool.
1731 struct Version_dependency_list {
1732 std::vector<std::string> dependencies;
1736 // The total definition of a version. It includes the tag for the
1737 // version, its global and local expressions, and any dependencies.
1738 struct Version_tree {
1739 Version_tree()
1740 : tag(), global(NULL), local(NULL), dependencies(NULL) {}
1742 std::string tag;
1743 const struct Version_expression_list* global;
1744 const struct Version_expression_list* local;
1745 const struct Version_dependency_list* dependencies;
1748 Version_script_info::~Version_script_info()
1750 this->clear();
1753 void
1754 Version_script_info::clear()
1756 for (size_t k = 0; k < dependency_lists_.size(); ++k)
1757 delete dependency_lists_[k];
1758 this->dependency_lists_.clear();
1759 for (size_t k = 0; k < version_trees_.size(); ++k)
1760 delete version_trees_[k];
1761 this->version_trees_.clear();
1762 for (size_t k = 0; k < expression_lists_.size(); ++k)
1763 delete expression_lists_[k];
1764 this->expression_lists_.clear();
1767 std::vector<std::string>
1768 Version_script_info::get_versions() const
1770 std::vector<std::string> ret;
1771 for (size_t j = 0; j < version_trees_.size(); ++j)
1772 ret.push_back(version_trees_[j]->tag);
1773 return ret;
1776 std::vector<std::string>
1777 Version_script_info::get_dependencies(const char* version) const
1779 std::vector<std::string> ret;
1780 for (size_t j = 0; j < version_trees_.size(); ++j)
1781 if (version_trees_[j]->tag == version)
1783 const struct Version_dependency_list* deps =
1784 version_trees_[j]->dependencies;
1785 if (deps != NULL)
1786 for (size_t k = 0; k < deps->dependencies.size(); ++k)
1787 ret.push_back(deps->dependencies[k]);
1788 return ret;
1790 return ret;
1793 const std::string&
1794 Version_script_info::get_symbol_version_helper(const char* symbol_name,
1795 bool check_global) const
1797 for (size_t j = 0; j < version_trees_.size(); ++j)
1799 // Is it a global symbol for this version?
1800 const Version_expression_list* explist =
1801 check_global ? version_trees_[j]->global : version_trees_[j]->local;
1802 if (explist != NULL)
1803 for (size_t k = 0; k < explist->expressions.size(); ++k)
1805 const char* name_to_match = symbol_name;
1806 const struct Version_expression& exp = explist->expressions[k];
1807 char* demangled_name = NULL;
1808 if (exp.language == "C++")
1810 demangled_name = cplus_demangle(symbol_name,
1811 DMGL_ANSI | DMGL_PARAMS);
1812 // This isn't a C++ symbol.
1813 if (demangled_name == NULL)
1814 continue;
1815 name_to_match = demangled_name;
1817 else if (exp.language == "Java")
1819 demangled_name = cplus_demangle(symbol_name,
1820 (DMGL_ANSI | DMGL_PARAMS
1821 | DMGL_JAVA));
1822 // This isn't a Java symbol.
1823 if (demangled_name == NULL)
1824 continue;
1825 name_to_match = demangled_name;
1827 bool matched;
1828 if (exp.exact_match)
1829 matched = strcmp(exp.pattern.c_str(), name_to_match) == 0;
1830 else
1831 matched = fnmatch(exp.pattern.c_str(), name_to_match,
1832 FNM_NOESCAPE) == 0;
1833 if (demangled_name != NULL)
1834 free(demangled_name);
1835 if (matched)
1836 return version_trees_[j]->tag;
1839 static const std::string empty = "";
1840 return empty;
1843 struct Version_dependency_list*
1844 Version_script_info::allocate_dependency_list()
1846 dependency_lists_.push_back(new Version_dependency_list);
1847 return dependency_lists_.back();
1850 struct Version_expression_list*
1851 Version_script_info::allocate_expression_list()
1853 expression_lists_.push_back(new Version_expression_list);
1854 return expression_lists_.back();
1857 struct Version_tree*
1858 Version_script_info::allocate_version_tree()
1860 version_trees_.push_back(new Version_tree);
1861 return version_trees_.back();
1864 // Print for debugging.
1866 void
1867 Version_script_info::print(FILE* f) const
1869 if (this->empty())
1870 return;
1872 fprintf(f, "VERSION {");
1874 for (size_t i = 0; i < this->version_trees_.size(); ++i)
1876 const Version_tree* vt = this->version_trees_[i];
1878 if (vt->tag.empty())
1879 fprintf(f, " {\n");
1880 else
1881 fprintf(f, " %s {\n", vt->tag.c_str());
1883 if (vt->global != NULL)
1885 fprintf(f, " global :\n");
1886 this->print_expression_list(f, vt->global);
1889 if (vt->local != NULL)
1891 fprintf(f, " local :\n");
1892 this->print_expression_list(f, vt->local);
1895 fprintf(f, " }");
1896 if (vt->dependencies != NULL)
1898 const Version_dependency_list* deps = vt->dependencies;
1899 for (size_t j = 0; j < deps->dependencies.size(); ++j)
1901 if (j < deps->dependencies.size() - 1)
1902 fprintf(f, "\n");
1903 fprintf(f, " %s", deps->dependencies[j].c_str());
1906 fprintf(f, ";\n");
1909 fprintf(f, "}\n");
1912 void
1913 Version_script_info::print_expression_list(
1914 FILE* f,
1915 const Version_expression_list* vel) const
1917 std::string current_language;
1918 for (size_t i = 0; i < vel->expressions.size(); ++i)
1920 const Version_expression& ve(vel->expressions[i]);
1922 if (ve.language != current_language)
1924 if (!current_language.empty())
1925 fprintf(f, " }\n");
1926 fprintf(f, " extern \"%s\" {\n", ve.language.c_str());
1927 current_language = ve.language;
1930 fprintf(f, " ");
1931 if (!current_language.empty())
1932 fprintf(f, " ");
1934 if (ve.exact_match)
1935 fprintf(f, "\"");
1936 fprintf(f, "%s", ve.pattern.c_str());
1937 if (ve.exact_match)
1938 fprintf(f, "\"");
1940 fprintf(f, "\n");
1943 if (!current_language.empty())
1944 fprintf(f, " }\n");
1947 } // End namespace gold.
1949 // The remaining functions are extern "C", so it's clearer to not put
1950 // them in namespace gold.
1952 using namespace gold;
1954 // This function is called by the bison parser to return the next
1955 // token.
1957 extern "C" int
1958 yylex(YYSTYPE* lvalp, void* closurev)
1960 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1961 const Token* token = closure->next_token();
1962 switch (token->classification())
1964 default:
1965 gold_unreachable();
1967 case Token::TOKEN_INVALID:
1968 yyerror(closurev, "invalid character");
1969 return 0;
1971 case Token::TOKEN_EOF:
1972 return 0;
1974 case Token::TOKEN_STRING:
1976 // This is either a keyword or a STRING.
1977 size_t len;
1978 const char* str = token->string_value(&len);
1979 int parsecode = 0;
1980 switch (closure->lex_mode())
1982 case Lex::LINKER_SCRIPT:
1983 parsecode = script_keywords.keyword_to_parsecode(str, len);
1984 break;
1985 case Lex::VERSION_SCRIPT:
1986 parsecode = version_script_keywords.keyword_to_parsecode(str, len);
1987 break;
1988 default:
1989 break;
1991 if (parsecode != 0)
1992 return parsecode;
1993 lvalp->string.value = str;
1994 lvalp->string.length = len;
1995 return STRING;
1998 case Token::TOKEN_QUOTED_STRING:
1999 lvalp->string.value = token->string_value(&lvalp->string.length);
2000 return QUOTED_STRING;
2002 case Token::TOKEN_OPERATOR:
2003 return token->operator_value();
2005 case Token::TOKEN_INTEGER:
2006 lvalp->integer = token->integer_value();
2007 return INTEGER;
2011 // This function is called by the bison parser to report an error.
2013 extern "C" void
2014 yyerror(void* closurev, const char* message)
2016 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2017 gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
2018 closure->charpos(), message);
2021 // Called by the bison parser to add a file to the link.
2023 extern "C" void
2024 script_add_file(void* closurev, const char* name, size_t length)
2026 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2028 // If this is an absolute path, and we found the script in the
2029 // sysroot, then we want to prepend the sysroot to the file name.
2030 // For example, this is how we handle a cross link to the x86_64
2031 // libc.so, which refers to /lib/libc.so.6.
2032 std::string name_string(name, length);
2033 const char* extra_search_path = ".";
2034 std::string script_directory;
2035 if (IS_ABSOLUTE_PATH(name_string.c_str()))
2037 if (closure->is_in_sysroot())
2039 const std::string& sysroot(parameters->sysroot());
2040 gold_assert(!sysroot.empty());
2041 name_string = sysroot + name_string;
2044 else
2046 // In addition to checking the normal library search path, we
2047 // also want to check in the script-directory.
2048 const char *slash = strrchr(closure->filename(), '/');
2049 if (slash != NULL)
2051 script_directory.assign(closure->filename(),
2052 slash - closure->filename() + 1);
2053 extra_search_path = script_directory.c_str();
2057 Input_file_argument file(name_string.c_str(), false, extra_search_path,
2058 false, closure->position_dependent_options());
2059 closure->inputs()->add_file(file);
2062 // Called by the bison parser to start a group. If we are already in
2063 // a group, that means that this script was invoked within a
2064 // --start-group --end-group sequence on the command line, or that
2065 // this script was found in a GROUP of another script. In that case,
2066 // we simply continue the existing group, rather than starting a new
2067 // one. It is possible to construct a case in which this will do
2068 // something other than what would happen if we did a recursive group,
2069 // but it's hard to imagine why the different behaviour would be
2070 // useful for a real program. Avoiding recursive groups is simpler
2071 // and more efficient.
2073 extern "C" void
2074 script_start_group(void* closurev)
2076 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2077 if (!closure->in_group())
2078 closure->inputs()->start_group();
2081 // Called by the bison parser at the end of a group.
2083 extern "C" void
2084 script_end_group(void* closurev)
2086 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2087 if (!closure->in_group())
2088 closure->inputs()->end_group();
2091 // Called by the bison parser to start an AS_NEEDED list.
2093 extern "C" void
2094 script_start_as_needed(void* closurev)
2096 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2097 closure->position_dependent_options().set_as_needed();
2100 // Called by the bison parser at the end of an AS_NEEDED list.
2102 extern "C" void
2103 script_end_as_needed(void* closurev)
2105 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2106 closure->position_dependent_options().clear_as_needed();
2109 // Called by the bison parser to set the entry symbol.
2111 extern "C" void
2112 script_set_entry(void* closurev, const char* entry, size_t length)
2114 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2115 closure->script_options()->set_entry(entry, length);
2118 // Called by the bison parser to define a symbol.
2120 extern "C" void
2121 script_set_symbol(void* closurev, const char* name, size_t length,
2122 Expression* value, int providei, int hiddeni)
2124 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2125 const bool provide = providei != 0;
2126 const bool hidden = hiddeni != 0;
2127 closure->script_options()->add_symbol_assignment(name, length, value,
2128 provide, hidden);
2131 // Called by the bison parser to add an assertion.
2133 extern "C" void
2134 script_add_assertion(void* closurev, Expression* check, const char* message,
2135 size_t messagelen)
2137 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2138 closure->script_options()->add_assertion(check, message, messagelen);
2141 // Called by the bison parser to parse an OPTION.
2143 extern "C" void
2144 script_parse_option(void* closurev, const char* option, size_t length)
2146 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2147 // We treat the option as a single command-line option, even if
2148 // it has internal whitespace.
2149 if (closure->command_line() == NULL)
2151 // There are some options that we could handle here--e.g.,
2152 // -lLIBRARY. Should we bother?
2153 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
2154 " for scripts specified via -T/--script"),
2155 closure->filename(), closure->lineno(), closure->charpos());
2157 else
2159 bool past_a_double_dash_option = false;
2160 char* mutable_option = strndup(option, length);
2161 gold_assert(mutable_option != NULL);
2162 closure->command_line()->process_one_option(1, &mutable_option, 0,
2163 &past_a_double_dash_option);
2164 free(mutable_option);
2168 // Called by the bison parser to handle SEARCH_DIR. This is handled
2169 // exactly like a -L option.
2171 extern "C" void
2172 script_add_search_dir(void* closurev, const char* option, size_t length)
2174 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2175 if (closure->command_line() == NULL)
2176 gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid"
2177 " for scripts specified via -T/--script"),
2178 closure->filename(), closure->lineno(), closure->charpos());
2179 else
2181 std::string s = "-L" + std::string(option, length);
2182 script_parse_option(closurev, s.c_str(), s.size());
2186 /* Called by the bison parser to push the lexer into expression
2187 mode. */
2189 extern "C" void
2190 script_push_lex_into_expression_mode(void* closurev)
2192 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2193 closure->push_lex_mode(Lex::EXPRESSION);
2196 /* Called by the bison parser to push the lexer into version
2197 mode. */
2199 extern "C" void
2200 script_push_lex_into_version_mode(void* closurev)
2202 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2203 closure->push_lex_mode(Lex::VERSION_SCRIPT);
2206 /* Called by the bison parser to pop the lexer mode. */
2208 extern "C" void
2209 script_pop_lex_mode(void* closurev)
2211 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2212 closure->pop_lex_mode();
2215 // Register an entire version node. For example:
2217 // GLIBC_2.1 {
2218 // global: foo;
2219 // } GLIBC_2.0;
2221 // - tag is "GLIBC_2.1"
2222 // - tree contains the information "global: foo"
2223 // - deps contains "GLIBC_2.0"
2225 extern "C" void
2226 script_register_vers_node(void*,
2227 const char* tag,
2228 int taglen,
2229 struct Version_tree *tree,
2230 struct Version_dependency_list *deps)
2232 gold_assert(tree != NULL);
2233 gold_assert(tag != NULL);
2234 tree->dependencies = deps;
2235 tree->tag = std::string(tag, taglen);
2238 // Add a dependencies to the list of existing dependencies, if any,
2239 // and return the expanded list.
2241 extern "C" struct Version_dependency_list *
2242 script_add_vers_depend(void* closurev,
2243 struct Version_dependency_list *all_deps,
2244 const char *depend_to_add, int deplen)
2246 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2247 if (all_deps == NULL)
2248 all_deps = closure->version_script()->allocate_dependency_list();
2249 all_deps->dependencies.push_back(std::string(depend_to_add, deplen));
2250 return all_deps;
2253 // Add a pattern expression to an existing list of expressions, if any.
2254 // TODO: In the old linker, the last argument used to be a bool, but I
2255 // don't know what it meant.
2257 extern "C" struct Version_expression_list *
2258 script_new_vers_pattern(void* closurev,
2259 struct Version_expression_list *expressions,
2260 const char *pattern, int patlen, int exact_match)
2262 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2263 if (expressions == NULL)
2264 expressions = closure->version_script()->allocate_expression_list();
2265 expressions->expressions.push_back(
2266 Version_expression(std::string(pattern, patlen),
2267 closure->get_current_language(),
2268 static_cast<bool>(exact_match)));
2269 return expressions;
2272 // Attaches b to the end of a, and clears b. So a = a + b and b = {}.
2274 extern "C" struct Version_expression_list*
2275 script_merge_expressions(struct Version_expression_list *a,
2276 struct Version_expression_list *b)
2278 a->expressions.insert(a->expressions.end(),
2279 b->expressions.begin(), b->expressions.end());
2280 // We could delete b and remove it from expressions_lists_, but
2281 // that's a lot of work. This works just as well.
2282 b->expressions.clear();
2283 return a;
2286 // Combine the global and local expressions into a a Version_tree.
2288 extern "C" struct Version_tree *
2289 script_new_vers_node(void* closurev,
2290 struct Version_expression_list *global,
2291 struct Version_expression_list *local)
2293 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2294 Version_tree* tree = closure->version_script()->allocate_version_tree();
2295 tree->global = global;
2296 tree->local = local;
2297 return tree;
2300 // Handle a transition in language, such as at the
2301 // start or end of 'extern "C++"'
2303 extern "C" void
2304 version_script_push_lang(void* closurev, const char* lang, int langlen)
2306 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2307 closure->push_language(std::string(lang, langlen));
2310 extern "C" void
2311 version_script_pop_lang(void* closurev)
2313 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2314 closure->pop_language();
2317 // Called by the bison parser to start a SECTIONS clause.
2319 extern "C" void
2320 script_start_sections(void* closurev)
2322 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2323 closure->script_options()->script_sections()->start_sections();
2326 // Called by the bison parser to finish a SECTIONS clause.
2328 extern "C" void
2329 script_finish_sections(void* closurev)
2331 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2332 closure->script_options()->script_sections()->finish_sections();
2335 // Start processing entries for an output section.
2337 extern "C" void
2338 script_start_output_section(void* closurev, const char* name, size_t namelen,
2339 const struct Parser_output_section_header* header)
2341 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2342 closure->script_options()->script_sections()->start_output_section(name,
2343 namelen,
2344 header);
2347 // Finish processing entries for an output section.
2349 extern "C" void
2350 script_finish_output_section(void* closurev,
2351 const struct Parser_output_section_trailer* trail)
2353 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2354 closure->script_options()->script_sections()->finish_output_section(trail);
2357 // Add a data item (e.g., "WORD (0)") to the current output section.
2359 extern "C" void
2360 script_add_data(void* closurev, int data_token, Expression* val)
2362 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2363 int size;
2364 bool is_signed = true;
2365 switch (data_token)
2367 case QUAD:
2368 size = 8;
2369 is_signed = false;
2370 break;
2371 case SQUAD:
2372 size = 8;
2373 break;
2374 case LONG:
2375 size = 4;
2376 break;
2377 case SHORT:
2378 size = 2;
2379 break;
2380 case BYTE:
2381 size = 1;
2382 break;
2383 default:
2384 gold_unreachable();
2386 closure->script_options()->script_sections()->add_data(size, is_signed, val);
2389 // Add a clause setting the fill value to the current output section.
2391 extern "C" void
2392 script_add_fill(void* closurev, Expression* val)
2394 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2395 closure->script_options()->script_sections()->add_fill(val);
2398 // Add a new input section specification to the current output
2399 // section.
2401 extern "C" void
2402 script_add_input_section(void* closurev,
2403 const struct Input_section_spec* spec,
2404 int keepi)
2406 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2407 bool keep = keepi != 0;
2408 closure->script_options()->script_sections()->add_input_section(spec, keep);
2411 // Create a new list of string/sort pairs.
2413 extern "C" String_sort_list_ptr
2414 script_new_string_sort_list(const struct Wildcard_section* string_sort)
2416 return new String_sort_list(1, *string_sort);
2419 // Add an entry to a list of string/sort pairs. The way the parser
2420 // works permits us to simply modify the first parameter, rather than
2421 // copy the vector.
2423 extern "C" String_sort_list_ptr
2424 script_string_sort_list_add(String_sort_list_ptr pv,
2425 const struct Wildcard_section* string_sort)
2427 if (pv == NULL)
2428 return script_new_string_sort_list(string_sort);
2429 else
2431 pv->push_back(*string_sort);
2432 return pv;
2436 // Create a new list of strings.
2438 extern "C" String_list_ptr
2439 script_new_string_list(const char* str, size_t len)
2441 return new String_list(1, std::string(str, len));
2444 // Add an element to a list of strings. The way the parser works
2445 // permits us to simply modify the first parameter, rather than copy
2446 // the vector.
2448 extern "C" String_list_ptr
2449 script_string_list_push_back(String_list_ptr pv, const char* str, size_t len)
2451 if (pv == NULL)
2452 return script_new_string_list(str, len);
2453 else
2455 pv->push_back(std::string(str, len));
2456 return pv;
2460 // Concatenate two string lists. Either or both may be NULL. The way
2461 // the parser works permits us to modify the parameters, rather than
2462 // copy the vector.
2464 extern "C" String_list_ptr
2465 script_string_list_append(String_list_ptr pv1, String_list_ptr pv2)
2467 if (pv1 == NULL)
2468 return pv2;
2469 if (pv2 == NULL)
2470 return pv1;
2471 pv1->insert(pv1->end(), pv2->begin(), pv2->end());
2472 return pv1;
2475 // Add a new program header.
2477 extern "C" void
2478 script_add_phdr(void* closurev, const char* name, size_t namelen,
2479 unsigned int type, const Phdr_info* info)
2481 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2482 bool includes_filehdr = info->includes_filehdr != 0;
2483 bool includes_phdrs = info->includes_phdrs != 0;
2484 bool is_flags_valid = info->is_flags_valid != 0;
2485 Script_sections* ss = closure->script_options()->script_sections();
2486 ss->add_phdr(name, namelen, type, includes_filehdr, includes_phdrs,
2487 is_flags_valid, info->flags, info->load_address);
2490 // Convert a program header string to a type.
2492 #define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME }
2494 static struct
2496 const char* name;
2497 size_t namelen;
2498 unsigned int val;
2499 } phdr_type_names[] =
2501 PHDR_TYPE(PT_NULL),
2502 PHDR_TYPE(PT_LOAD),
2503 PHDR_TYPE(PT_DYNAMIC),
2504 PHDR_TYPE(PT_INTERP),
2505 PHDR_TYPE(PT_NOTE),
2506 PHDR_TYPE(PT_SHLIB),
2507 PHDR_TYPE(PT_PHDR),
2508 PHDR_TYPE(PT_TLS),
2509 PHDR_TYPE(PT_GNU_EH_FRAME),
2510 PHDR_TYPE(PT_GNU_STACK),
2511 PHDR_TYPE(PT_GNU_RELRO)
2514 extern "C" unsigned int
2515 script_phdr_string_to_type(void* closurev, const char* name, size_t namelen)
2517 for (unsigned int i = 0;
2518 i < sizeof(phdr_type_names) / sizeof(phdr_type_names[0]);
2519 ++i)
2520 if (namelen == phdr_type_names[i].namelen
2521 && strncmp(name, phdr_type_names[i].name, namelen) == 0)
2522 return phdr_type_names[i].val;
2523 yyerror(closurev, _("unknown PHDR type (try integer)"));
2524 return elfcpp::PT_NULL;