bfd/
[binutils.git] / gold / script.cc
blobf6e4a396f2c3142597d72d90c13b1f89a979cd82
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 <cstdio>
26 #include <cstdlib>
27 #include <cstring>
28 #include <fnmatch.h>
29 #include <string>
30 #include <vector>
31 #include "filenames.h"
33 #include "elfcpp.h"
34 #include "demangle.h"
35 #include "dirsearch.h"
36 #include "options.h"
37 #include "fileread.h"
38 #include "workqueue.h"
39 #include "readsyms.h"
40 #include "parameters.h"
41 #include "layout.h"
42 #include "symtab.h"
43 #include "script.h"
44 #include "script-c.h"
46 namespace gold
49 // A token read from a script file. We don't implement keywords here;
50 // all keywords are simply represented as a string.
52 class Token
54 public:
55 // Token classification.
56 enum Classification
58 // Token is invalid.
59 TOKEN_INVALID,
60 // Token indicates end of input.
61 TOKEN_EOF,
62 // Token is a string of characters.
63 TOKEN_STRING,
64 // Token is a quoted string of characters.
65 TOKEN_QUOTED_STRING,
66 // Token is an operator.
67 TOKEN_OPERATOR,
68 // Token is a number (an integer).
69 TOKEN_INTEGER
72 // We need an empty constructor so that we can put this STL objects.
73 Token()
74 : classification_(TOKEN_INVALID), value_(NULL), value_length_(0),
75 opcode_(0), lineno_(0), charpos_(0)
76 { }
78 // A general token with no value.
79 Token(Classification classification, int lineno, int charpos)
80 : classification_(classification), value_(NULL), value_length_(0),
81 opcode_(0), lineno_(lineno), charpos_(charpos)
83 gold_assert(classification == TOKEN_INVALID
84 || classification == TOKEN_EOF);
87 // A general token with a value.
88 Token(Classification classification, const char* value, size_t length,
89 int lineno, int charpos)
90 : classification_(classification), value_(value), value_length_(length),
91 opcode_(0), lineno_(lineno), charpos_(charpos)
93 gold_assert(classification != TOKEN_INVALID
94 && classification != TOKEN_EOF);
97 // A token representing an operator.
98 Token(int opcode, int lineno, int charpos)
99 : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0),
100 opcode_(opcode), lineno_(lineno), charpos_(charpos)
103 // Return whether the token is invalid.
104 bool
105 is_invalid() const
106 { return this->classification_ == TOKEN_INVALID; }
108 // Return whether this is an EOF token.
109 bool
110 is_eof() const
111 { return this->classification_ == TOKEN_EOF; }
113 // Return the token classification.
114 Classification
115 classification() const
116 { return this->classification_; }
118 // Return the line number at which the token starts.
120 lineno() const
121 { return this->lineno_; }
123 // Return the character position at this the token starts.
125 charpos() const
126 { return this->charpos_; }
128 // Get the value of a token.
130 const char*
131 string_value(size_t* length) const
133 gold_assert(this->classification_ == TOKEN_STRING
134 || this->classification_ == TOKEN_QUOTED_STRING);
135 *length = this->value_length_;
136 return this->value_;
140 operator_value() const
142 gold_assert(this->classification_ == TOKEN_OPERATOR);
143 return this->opcode_;
146 uint64_t
147 integer_value() const
149 gold_assert(this->classification_ == TOKEN_INTEGER);
150 // Null terminate.
151 std::string s(this->value_, this->value_length_);
152 return strtoull(s.c_str(), NULL, 0);
155 private:
156 // The token classification.
157 Classification classification_;
158 // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or
159 // TOKEN_INTEGER.
160 const char* value_;
161 // The length of the token value.
162 size_t value_length_;
163 // The token value, for TOKEN_OPERATOR.
164 int opcode_;
165 // The line number where this token started (one based).
166 int lineno_;
167 // The character position within the line where this token started
168 // (one based).
169 int charpos_;
172 // This class handles lexing a file into a sequence of tokens.
174 class Lex
176 public:
177 // We unfortunately have to support different lexing modes, because
178 // when reading different parts of a linker script we need to parse
179 // things differently.
180 enum Mode
182 // Reading an ordinary linker script.
183 LINKER_SCRIPT,
184 // Reading an expression in a linker script.
185 EXPRESSION,
186 // Reading a version script.
187 VERSION_SCRIPT
190 Lex(const char* input_string, size_t input_length, int parsing_token)
191 : input_string_(input_string), input_length_(input_length),
192 current_(input_string), mode_(LINKER_SCRIPT),
193 first_token_(parsing_token), token_(),
194 lineno_(1), linestart_(input_string)
197 // Read a file into a string.
198 static void
199 read_file(Input_file*, std::string*);
201 // Return the next token.
202 const Token*
203 next_token();
205 // Return the current lexing mode.
206 Lex::Mode
207 mode() const
208 { return this->mode_; }
210 // Set the lexing mode.
211 void
212 set_mode(Mode mode)
213 { this->mode_ = mode; }
215 private:
216 Lex(const Lex&);
217 Lex& operator=(const Lex&);
219 // Make a general token with no value at the current location.
220 Token
221 make_token(Token::Classification c, const char* start) const
222 { return Token(c, this->lineno_, start - this->linestart_ + 1); }
224 // Make a general token with a value at the current location.
225 Token
226 make_token(Token::Classification c, const char* v, size_t len,
227 const char* start)
228 const
229 { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); }
231 // Make an operator token at the current location.
232 Token
233 make_token(int opcode, const char* start) const
234 { return Token(opcode, this->lineno_, start - this->linestart_ + 1); }
236 // Make an invalid token at the current location.
237 Token
238 make_invalid_token(const char* start)
239 { return this->make_token(Token::TOKEN_INVALID, start); }
241 // Make an EOF token at the current location.
242 Token
243 make_eof_token(const char* start)
244 { return this->make_token(Token::TOKEN_EOF, start); }
246 // Return whether C can be the first character in a name. C2 is the
247 // next character, since we sometimes need that.
248 inline bool
249 can_start_name(char c, char c2);
251 // If C can appear in a name which has already started, return a
252 // pointer to a character later in the token or just past
253 // it. Otherwise, return NULL.
254 inline const char*
255 can_continue_name(const char* c);
257 // Return whether C, C2, C3 can start a hex number.
258 inline bool
259 can_start_hex(char c, char c2, char c3);
261 // If C can appear in a hex number which has already started, return
262 // a pointer to a character later in the token or just past
263 // it. Otherwise, return NULL.
264 inline const char*
265 can_continue_hex(const char* c);
267 // Return whether C can start a non-hex number.
268 static inline bool
269 can_start_number(char c);
271 // If C can appear in a decimal number which has already started,
272 // return a pointer to a character later in the token or just past
273 // it. Otherwise, return NULL.
274 inline const char*
275 can_continue_number(const char* c)
276 { return Lex::can_start_number(*c) ? c + 1 : NULL; }
278 // If C1 C2 C3 form a valid three character operator, return the
279 // opcode. Otherwise return 0.
280 static inline int
281 three_char_operator(char c1, char c2, char c3);
283 // If C1 C2 form a valid two character operator, return the opcode.
284 // Otherwise return 0.
285 static inline int
286 two_char_operator(char c1, char c2);
288 // If C1 is a valid one character operator, return the opcode.
289 // Otherwise return 0.
290 static inline int
291 one_char_operator(char c1);
293 // Read the next token.
294 Token
295 get_token(const char**);
297 // Skip a C style /* */ comment. Return false if the comment did
298 // not end.
299 bool
300 skip_c_comment(const char**);
302 // Skip a line # comment. Return false if there was no newline.
303 bool
304 skip_line_comment(const char**);
306 // Build a token CLASSIFICATION from all characters that match
307 // CAN_CONTINUE_FN. The token starts at START. Start matching from
308 // MATCH. Set *PP to the character following the token.
309 inline Token
310 gather_token(Token::Classification,
311 const char* (Lex::*can_continue_fn)(const char*),
312 const char* start, const char* match, const char** pp);
314 // Build a token from a quoted string.
315 Token
316 gather_quoted_string(const char** pp);
318 // The string we are tokenizing.
319 const char* input_string_;
320 // The length of the string.
321 size_t input_length_;
322 // The current offset into the string.
323 const char* current_;
324 // The current lexing mode.
325 Mode mode_;
326 // The code to use for the first token. This is set to 0 after it
327 // is used.
328 int first_token_;
329 // The current token.
330 Token token_;
331 // The current line number.
332 int lineno_;
333 // The start of the current line in the string.
334 const char* linestart_;
337 // Read the whole file into memory. We don't expect linker scripts to
338 // be large, so we just use a std::string as a buffer. We ignore the
339 // data we've already read, so that we read aligned buffers.
341 void
342 Lex::read_file(Input_file* input_file, std::string* contents)
344 off_t filesize = input_file->file().filesize();
345 contents->clear();
346 contents->reserve(filesize);
348 off_t off = 0;
349 unsigned char buf[BUFSIZ];
350 while (off < filesize)
352 off_t get = BUFSIZ;
353 if (get > filesize - off)
354 get = filesize - off;
355 input_file->file().read(off, get, buf);
356 contents->append(reinterpret_cast<char*>(&buf[0]), get);
357 off += get;
361 // Return whether C can be the start of a name, if the next character
362 // is C2. A name can being with a letter, underscore, period, or
363 // dollar sign. Because a name can be a file name, we also permit
364 // forward slash, backslash, and tilde. Tilde is the tricky case
365 // here; GNU ld also uses it as a bitwise not operator. It is only
366 // recognized as the operator if it is not immediately followed by
367 // some character which can appear in a symbol. That is, when we
368 // don't know that we are looking at an expression, "~0" is a file
369 // name, and "~ 0" is an expression using bitwise not. We are
370 // compatible.
372 inline bool
373 Lex::can_start_name(char c, char c2)
375 switch (c)
377 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
378 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
379 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
380 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
381 case 'Y': case 'Z':
382 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
383 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
384 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
385 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
386 case 'y': case 'z':
387 case '_': case '.': case '$':
388 return true;
390 case '/': case '\\':
391 return this->mode_ == LINKER_SCRIPT;
393 case '~':
394 return this->mode_ == LINKER_SCRIPT && can_continue_name(&c2);
396 case '*': case '[':
397 return (this->mode_ == VERSION_SCRIPT
398 || (this->mode_ == LINKER_SCRIPT
399 && can_continue_name(&c2)));
401 default:
402 return false;
406 // Return whether C can continue a name which has already started.
407 // Subsequent characters in a name are the same as the leading
408 // characters, plus digits and "=+-:[],?*". So in general the linker
409 // script language requires spaces around operators, unless we know
410 // that we are parsing an expression.
412 inline const char*
413 Lex::can_continue_name(const char* c)
415 switch (*c)
417 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
418 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
419 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
420 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
421 case 'Y': case 'Z':
422 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
423 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
424 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
425 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
426 case 'y': case 'z':
427 case '_': case '.': case '$':
428 case '0': case '1': case '2': case '3': case '4':
429 case '5': case '6': case '7': case '8': case '9':
430 return c + 1;
432 case '/': case '\\': case '~':
433 case '=': case '+':
434 case ',':
435 if (this->mode_ == LINKER_SCRIPT)
436 return c + 1;
437 return NULL;
439 case '[': case ']': case '*': case '?': case '-':
440 if (this->mode_ == LINKER_SCRIPT || this->mode_ == VERSION_SCRIPT)
441 return c + 1;
442 return NULL;
444 case '^':
445 if (this->mode_ == VERSION_SCRIPT)
446 return c + 1;
447 return NULL;
449 case ':':
450 if (this->mode_ == LINKER_SCRIPT)
451 return c + 1;
452 else if (this->mode_ == VERSION_SCRIPT && (c[1] == ':'))
454 // A name can have '::' in it, as that's a c++ namespace
455 // separator. But a single colon is not part of a name.
456 return c + 2;
458 return NULL;
460 default:
461 return NULL;
465 // For a number we accept 0x followed by hex digits, or any sequence
466 // of digits. The old linker accepts leading '$' for hex, and
467 // trailing HXBOD. Those are for MRI compatibility and we don't
468 // accept them. The old linker also accepts trailing MK for mega or
469 // kilo. FIXME: Those are mentioned in the documentation, and we
470 // should accept them.
472 // Return whether C1 C2 C3 can start a hex number.
474 inline bool
475 Lex::can_start_hex(char c1, char c2, char c3)
477 if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
478 return this->can_continue_hex(&c3);
479 return false;
482 // Return whether C can appear in a hex number.
484 inline const char*
485 Lex::can_continue_hex(const char* c)
487 switch (*c)
489 case '0': case '1': case '2': case '3': case '4':
490 case '5': case '6': case '7': case '8': case '9':
491 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
492 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
493 return c + 1;
495 default:
496 return NULL;
500 // Return whether C can start a non-hex number.
502 inline bool
503 Lex::can_start_number(char c)
505 switch (c)
507 case '0': case '1': case '2': case '3': case '4':
508 case '5': case '6': case '7': case '8': case '9':
509 return true;
511 default:
512 return false;
516 // If C1 C2 C3 form a valid three character operator, return the
517 // opcode (defined in the yyscript.h file generated from yyscript.y).
518 // Otherwise return 0.
520 inline int
521 Lex::three_char_operator(char c1, char c2, char c3)
523 switch (c1)
525 case '<':
526 if (c2 == '<' && c3 == '=')
527 return LSHIFTEQ;
528 break;
529 case '>':
530 if (c2 == '>' && c3 == '=')
531 return RSHIFTEQ;
532 break;
533 default:
534 break;
536 return 0;
539 // If C1 C2 form a valid two character operator, return the opcode
540 // (defined in the yyscript.h file generated from yyscript.y).
541 // Otherwise return 0.
543 inline int
544 Lex::two_char_operator(char c1, char c2)
546 switch (c1)
548 case '=':
549 if (c2 == '=')
550 return EQ;
551 break;
552 case '!':
553 if (c2 == '=')
554 return NE;
555 break;
556 case '+':
557 if (c2 == '=')
558 return PLUSEQ;
559 break;
560 case '-':
561 if (c2 == '=')
562 return MINUSEQ;
563 break;
564 case '*':
565 if (c2 == '=')
566 return MULTEQ;
567 break;
568 case '/':
569 if (c2 == '=')
570 return DIVEQ;
571 break;
572 case '|':
573 if (c2 == '=')
574 return OREQ;
575 if (c2 == '|')
576 return OROR;
577 break;
578 case '&':
579 if (c2 == '=')
580 return ANDEQ;
581 if (c2 == '&')
582 return ANDAND;
583 break;
584 case '>':
585 if (c2 == '=')
586 return GE;
587 if (c2 == '>')
588 return RSHIFT;
589 break;
590 case '<':
591 if (c2 == '=')
592 return LE;
593 if (c2 == '<')
594 return LSHIFT;
595 break;
596 default:
597 break;
599 return 0;
602 // If C1 is a valid operator, return the opcode. Otherwise return 0.
604 inline int
605 Lex::one_char_operator(char c1)
607 switch (c1)
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 case ';':
632 return c1;
633 default:
634 return 0;
638 // Skip a C style comment. *PP points to just after the "/*". Return
639 // false if the comment did not end.
641 bool
642 Lex::skip_c_comment(const char** pp)
644 const char* p = *pp;
645 while (p[0] != '*' || p[1] != '/')
647 if (*p == '\0')
649 *pp = p;
650 return false;
653 if (*p == '\n')
655 ++this->lineno_;
656 this->linestart_ = p + 1;
658 ++p;
661 *pp = p + 2;
662 return true;
665 // Skip a line # comment. Return false if there was no newline.
667 bool
668 Lex::skip_line_comment(const char** pp)
670 const char* p = *pp;
671 size_t skip = strcspn(p, "\n");
672 if (p[skip] == '\0')
674 *pp = p + skip;
675 return false;
678 p += skip + 1;
679 ++this->lineno_;
680 this->linestart_ = p;
681 *pp = p;
683 return true;
686 // Build a token CLASSIFICATION from all characters that match
687 // CAN_CONTINUE_FN. Update *PP.
689 inline Token
690 Lex::gather_token(Token::Classification classification,
691 const char* (Lex::*can_continue_fn)(const char*),
692 const char* start,
693 const char* match,
694 const char **pp)
696 const char* new_match = NULL;
697 while ((new_match = (this->*can_continue_fn)(match)))
698 match = new_match;
699 *pp = match;
700 return this->make_token(classification, start, match - start, start);
703 // Build a token from a quoted string.
705 Token
706 Lex::gather_quoted_string(const char** pp)
708 const char* start = *pp;
709 const char* p = start;
710 ++p;
711 size_t skip = strcspn(p, "\"\n");
712 if (p[skip] != '"')
713 return this->make_invalid_token(start);
714 *pp = p + skip + 1;
715 return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
718 // Return the next token at *PP. Update *PP. General guideline: we
719 // require linker scripts to be simple ASCII. No unicode linker
720 // scripts. In particular we can assume that any '\0' is the end of
721 // the input.
723 Token
724 Lex::get_token(const char** pp)
726 const char* p = *pp;
728 while (true)
730 if (*p == '\0')
732 *pp = p;
733 return this->make_eof_token(p);
736 // Skip whitespace quickly.
737 while (*p == ' ' || *p == '\t')
738 ++p;
740 if (*p == '\n')
742 ++p;
743 ++this->lineno_;
744 this->linestart_ = p;
745 continue;
748 // Skip C style comments.
749 if (p[0] == '/' && p[1] == '*')
751 int lineno = this->lineno_;
752 int charpos = p - this->linestart_ + 1;
754 *pp = p + 2;
755 if (!this->skip_c_comment(pp))
756 return Token(Token::TOKEN_INVALID, lineno, charpos);
757 p = *pp;
759 continue;
762 // Skip line comments.
763 if (*p == '#')
765 *pp = p + 1;
766 if (!this->skip_line_comment(pp))
767 return this->make_eof_token(p);
768 p = *pp;
769 continue;
772 // Check for a name.
773 if (this->can_start_name(p[0], p[1]))
774 return this->gather_token(Token::TOKEN_STRING,
775 &Lex::can_continue_name,
776 p, p + 1, pp);
778 // We accept any arbitrary name in double quotes, as long as it
779 // does not cross a line boundary.
780 if (*p == '"')
782 *pp = p;
783 return this->gather_quoted_string(pp);
786 // Check for a number.
788 if (this->can_start_hex(p[0], p[1], p[2]))
789 return this->gather_token(Token::TOKEN_INTEGER,
790 &Lex::can_continue_hex,
791 p, p + 3, pp);
793 if (Lex::can_start_number(p[0]))
794 return this->gather_token(Token::TOKEN_INTEGER,
795 &Lex::can_continue_number,
796 p, p + 1, pp);
798 // Check for operators.
800 int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
801 if (opcode != 0)
803 *pp = p + 3;
804 return this->make_token(opcode, p);
807 opcode = Lex::two_char_operator(p[0], p[1]);
808 if (opcode != 0)
810 *pp = p + 2;
811 return this->make_token(opcode, p);
814 opcode = Lex::one_char_operator(p[0]);
815 if (opcode != 0)
817 *pp = p + 1;
818 return this->make_token(opcode, p);
821 return this->make_token(Token::TOKEN_INVALID, p);
825 // Return the next token.
827 const Token*
828 Lex::next_token()
830 // The first token is special.
831 if (this->first_token_ != 0)
833 this->token_ = Token(this->first_token_, 0, 0);
834 this->first_token_ = 0;
835 return &this->token_;
838 this->token_ = this->get_token(&this->current_);
840 // Don't let an early null byte fool us into thinking that we've
841 // reached the end of the file.
842 if (this->token_.is_eof()
843 && (static_cast<size_t>(this->current_ - this->input_string_)
844 < this->input_length_))
845 this->token_ = this->make_invalid_token(this->current_);
847 return &this->token_;
850 // class Symbol_assignment.
852 // Add the symbol to the symbol table. This makes sure the symbol is
853 // there and defined. The actual value is stored later. We can't
854 // determine the actual value at this point, because we can't
855 // necessarily evaluate the expression until all ordinary symbols have
856 // been finalized.
858 // The GNU linker lets symbol assignments in the linker script
859 // silently override defined symbols in object files. We are
860 // compatible. FIXME: Should we issue a warning?
862 void
863 Symbol_assignment::add_to_table(Symbol_table* symtab)
865 elfcpp::STV vis = this->hidden_ ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
866 this->sym_ = symtab->define_as_constant(this->name_.c_str(),
867 NULL, // version
868 0, // value
869 0, // size
870 elfcpp::STT_NOTYPE,
871 elfcpp::STB_GLOBAL,
872 vis,
873 0, // nonvis
874 this->provide_,
875 true); // force_override
878 // Finalize a symbol value.
880 void
881 Symbol_assignment::finalize(Symbol_table* symtab, const Layout* layout)
883 this->finalize_maybe_dot(symtab, layout, false, 0, NULL);
886 // Finalize a symbol value which can refer to the dot symbol.
888 void
889 Symbol_assignment::finalize_with_dot(Symbol_table* symtab,
890 const Layout* layout,
891 uint64_t dot_value,
892 Output_section* dot_section)
894 this->finalize_maybe_dot(symtab, layout, true, dot_value, dot_section);
897 // Finalize a symbol value, internal version.
899 void
900 Symbol_assignment::finalize_maybe_dot(Symbol_table* symtab,
901 const Layout* layout,
902 bool is_dot_available,
903 uint64_t dot_value,
904 Output_section* dot_section)
906 // If we were only supposed to provide this symbol, the sym_ field
907 // will be NULL if the symbol was not referenced.
908 if (this->sym_ == NULL)
910 gold_assert(this->provide_);
911 return;
914 if (parameters->target().get_size() == 32)
916 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
917 this->sized_finalize<32>(symtab, layout, is_dot_available, dot_value,
918 dot_section);
919 #else
920 gold_unreachable();
921 #endif
923 else if (parameters->target().get_size() == 64)
925 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
926 this->sized_finalize<64>(symtab, layout, is_dot_available, dot_value,
927 dot_section);
928 #else
929 gold_unreachable();
930 #endif
932 else
933 gold_unreachable();
936 template<int size>
937 void
938 Symbol_assignment::sized_finalize(Symbol_table* symtab, const Layout* layout,
939 bool is_dot_available, uint64_t dot_value,
940 Output_section* dot_section)
942 Output_section* section;
943 uint64_t final_val = this->val_->eval_maybe_dot(symtab, layout, true,
944 is_dot_available,
945 dot_value, dot_section,
946 &section);
947 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(this->sym_);
948 ssym->set_value(final_val);
949 if (section != NULL)
950 ssym->set_output_section(section);
953 // Set the symbol value if the expression yields an absolute value.
955 void
956 Symbol_assignment::set_if_absolute(Symbol_table* symtab, const Layout* layout,
957 bool is_dot_available, uint64_t dot_value)
959 if (this->sym_ == NULL)
960 return;
962 Output_section* val_section;
963 uint64_t val = this->val_->eval_maybe_dot(symtab, layout, false,
964 is_dot_available, dot_value,
965 NULL, &val_section);
966 if (val_section != NULL)
967 return;
969 if (parameters->target().get_size() == 32)
971 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
972 Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(this->sym_);
973 ssym->set_value(val);
974 #else
975 gold_unreachable();
976 #endif
978 else if (parameters->target().get_size() == 64)
980 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
981 Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(this->sym_);
982 ssym->set_value(val);
983 #else
984 gold_unreachable();
985 #endif
987 else
988 gold_unreachable();
991 // Print for debugging.
993 void
994 Symbol_assignment::print(FILE* f) const
996 if (this->provide_ && this->hidden_)
997 fprintf(f, "PROVIDE_HIDDEN(");
998 else if (this->provide_)
999 fprintf(f, "PROVIDE(");
1000 else if (this->hidden_)
1001 gold_unreachable();
1003 fprintf(f, "%s = ", this->name_.c_str());
1004 this->val_->print(f);
1006 if (this->provide_ || this->hidden_)
1007 fprintf(f, ")");
1009 fprintf(f, "\n");
1012 // Class Script_assertion.
1014 // Check the assertion.
1016 void
1017 Script_assertion::check(const Symbol_table* symtab, const Layout* layout)
1019 if (!this->check_->eval(symtab, layout, true))
1020 gold_error("%s", this->message_.c_str());
1023 // Print for debugging.
1025 void
1026 Script_assertion::print(FILE* f) const
1028 fprintf(f, "ASSERT(");
1029 this->check_->print(f);
1030 fprintf(f, ", \"%s\")\n", this->message_.c_str());
1033 // Class Script_options.
1035 Script_options::Script_options()
1036 : entry_(), symbol_assignments_(), version_script_info_(),
1037 script_sections_()
1041 // Add a symbol to be defined.
1043 void
1044 Script_options::add_symbol_assignment(const char* name, size_t length,
1045 Expression* value, bool provide,
1046 bool hidden)
1048 if (length != 1 || name[0] != '.')
1050 if (this->script_sections_.in_sections_clause())
1051 this->script_sections_.add_symbol_assignment(name, length, value,
1052 provide, hidden);
1053 else
1055 Symbol_assignment* p = new Symbol_assignment(name, length, value,
1056 provide, hidden);
1057 this->symbol_assignments_.push_back(p);
1060 else
1062 if (provide || hidden)
1063 gold_error(_("invalid use of PROVIDE for dot symbol"));
1064 if (!this->script_sections_.in_sections_clause())
1065 gold_error(_("invalid assignment to dot outside of SECTIONS"));
1066 else
1067 this->script_sections_.add_dot_assignment(value);
1071 // Add an assertion.
1073 void
1074 Script_options::add_assertion(Expression* check, const char* message,
1075 size_t messagelen)
1077 if (this->script_sections_.in_sections_clause())
1078 this->script_sections_.add_assertion(check, message, messagelen);
1079 else
1081 Script_assertion* p = new Script_assertion(check, message, messagelen);
1082 this->assertions_.push_back(p);
1086 // Create sections required by any linker scripts.
1088 void
1089 Script_options::create_script_sections(Layout* layout)
1091 if (this->saw_sections_clause())
1092 this->script_sections_.create_sections(layout);
1095 // Add any symbols we are defining to the symbol table.
1097 void
1098 Script_options::add_symbols_to_table(Symbol_table* symtab)
1100 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1101 p != this->symbol_assignments_.end();
1102 ++p)
1103 (*p)->add_to_table(symtab);
1104 this->script_sections_.add_symbols_to_table(symtab);
1107 // Finalize symbol values. Also check assertions.
1109 void
1110 Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
1112 // We finalize the symbols defined in SECTIONS first, because they
1113 // are the ones which may have changed. This way if symbol outside
1114 // SECTIONS are defined in terms of symbols inside SECTIONS, they
1115 // will get the right value.
1116 this->script_sections_.finalize_symbols(symtab, layout);
1118 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1119 p != this->symbol_assignments_.end();
1120 ++p)
1121 (*p)->finalize(symtab, layout);
1123 for (Assertions::iterator p = this->assertions_.begin();
1124 p != this->assertions_.end();
1125 ++p)
1126 (*p)->check(symtab, layout);
1129 // Set section addresses. We set all the symbols which have absolute
1130 // values. Then we let the SECTIONS clause do its thing. This
1131 // returns the segment which holds the file header and segment
1132 // headers, if any.
1134 Output_segment*
1135 Script_options::set_section_addresses(Symbol_table* symtab, Layout* layout)
1137 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1138 p != this->symbol_assignments_.end();
1139 ++p)
1140 (*p)->set_if_absolute(symtab, layout, false, 0);
1142 return this->script_sections_.set_section_addresses(symtab, layout);
1145 // This class holds data passed through the parser to the lexer and to
1146 // the parser support functions. This avoids global variables. We
1147 // can't use global variables because we need not be called by a
1148 // singleton thread.
1150 class Parser_closure
1152 public:
1153 Parser_closure(const char* filename,
1154 const Position_dependent_options& posdep_options,
1155 bool in_group, bool is_in_sysroot,
1156 Command_line* command_line,
1157 Script_options* script_options,
1158 Lex* lex)
1159 : filename_(filename), posdep_options_(posdep_options),
1160 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
1161 command_line_(command_line), script_options_(script_options),
1162 version_script_info_(script_options->version_script_info()),
1163 lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL)
1165 // We start out processing C symbols in the default lex mode.
1166 language_stack_.push_back("");
1167 lex_mode_stack_.push_back(lex->mode());
1170 // Return the file name.
1171 const char*
1172 filename() const
1173 { return this->filename_; }
1175 // Return the position dependent options. The caller may modify
1176 // this.
1177 Position_dependent_options&
1178 position_dependent_options()
1179 { return this->posdep_options_; }
1181 // Return whether this script is being run in a group.
1182 bool
1183 in_group() const
1184 { return this->in_group_; }
1186 // Return whether this script was found using a directory in the
1187 // sysroot.
1188 bool
1189 is_in_sysroot() const
1190 { return this->is_in_sysroot_; }
1192 // Returns the Command_line structure passed in at constructor time.
1193 // This value may be NULL. The caller may modify this, which modifies
1194 // the passed-in Command_line object (not a copy).
1195 Command_line*
1196 command_line()
1197 { return this->command_line_; }
1199 // Return the options which may be set by a script.
1200 Script_options*
1201 script_options()
1202 { return this->script_options_; }
1204 // Return the object in which version script information should be stored.
1205 Version_script_info*
1206 version_script()
1207 { return this->version_script_info_; }
1209 // Return the next token, and advance.
1210 const Token*
1211 next_token()
1213 const Token* token = this->lex_->next_token();
1214 this->lineno_ = token->lineno();
1215 this->charpos_ = token->charpos();
1216 return token;
1219 // Set a new lexer mode, pushing the current one.
1220 void
1221 push_lex_mode(Lex::Mode mode)
1223 this->lex_mode_stack_.push_back(this->lex_->mode());
1224 this->lex_->set_mode(mode);
1227 // Pop the lexer mode.
1228 void
1229 pop_lex_mode()
1231 gold_assert(!this->lex_mode_stack_.empty());
1232 this->lex_->set_mode(this->lex_mode_stack_.back());
1233 this->lex_mode_stack_.pop_back();
1236 // Return the current lexer mode.
1237 Lex::Mode
1238 lex_mode() const
1239 { return this->lex_mode_stack_.back(); }
1241 // Return the line number of the last token.
1243 lineno() const
1244 { return this->lineno_; }
1246 // Return the character position in the line of the last token.
1248 charpos() const
1249 { return this->charpos_; }
1251 // Return the list of input files, creating it if necessary. This
1252 // is a space leak--we never free the INPUTS_ pointer.
1253 Input_arguments*
1254 inputs()
1256 if (this->inputs_ == NULL)
1257 this->inputs_ = new Input_arguments();
1258 return this->inputs_;
1261 // Return whether we saw any input files.
1262 bool
1263 saw_inputs() const
1264 { return this->inputs_ != NULL && !this->inputs_->empty(); }
1266 // Return the current language being processed in a version script
1267 // (eg, "C++"). The empty string represents unmangled C names.
1268 const std::string&
1269 get_current_language() const
1270 { return this->language_stack_.back(); }
1272 // Push a language onto the stack when entering an extern block.
1273 void push_language(const std::string& lang)
1274 { this->language_stack_.push_back(lang); }
1276 // Pop a language off of the stack when exiting an extern block.
1277 void pop_language()
1279 gold_assert(!this->language_stack_.empty());
1280 this->language_stack_.pop_back();
1283 private:
1284 // The name of the file we are reading.
1285 const char* filename_;
1286 // The position dependent options.
1287 Position_dependent_options posdep_options_;
1288 // Whether we are currently in a --start-group/--end-group.
1289 bool in_group_;
1290 // Whether the script was found in a sysrooted directory.
1291 bool is_in_sysroot_;
1292 // May be NULL if the user chooses not to pass one in.
1293 Command_line* command_line_;
1294 // Options which may be set from any linker script.
1295 Script_options* script_options_;
1296 // Information parsed from a version script.
1297 Version_script_info* version_script_info_;
1298 // The lexer.
1299 Lex* lex_;
1300 // The line number of the last token returned by next_token.
1301 int lineno_;
1302 // The column number of the last token returned by next_token.
1303 int charpos_;
1304 // A stack of lexer modes.
1305 std::vector<Lex::Mode> lex_mode_stack_;
1306 // A stack of which extern/language block we're inside. Can be C++,
1307 // java, or empty for C.
1308 std::vector<std::string> language_stack_;
1309 // New input files found to add to the link.
1310 Input_arguments* inputs_;
1313 // FILE was found as an argument on the command line. Try to read it
1314 // as a script. Return true if the file was handled.
1316 bool
1317 read_input_script(Workqueue* workqueue, const General_options& options,
1318 Symbol_table* symtab, Layout* layout,
1319 Dirsearch* dirsearch, Input_objects* input_objects,
1320 Mapfile* mapfile, Input_group* input_group,
1321 const Input_argument* input_argument,
1322 Input_file* input_file, Task_token* next_blocker,
1323 bool* used_next_blocker)
1325 *used_next_blocker = false;
1327 std::string input_string;
1328 Lex::read_file(input_file, &input_string);
1330 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1332 Parser_closure closure(input_file->filename().c_str(),
1333 input_argument->file().options(),
1334 input_group != NULL,
1335 input_file->is_in_sysroot(),
1336 NULL,
1337 layout->script_options(),
1338 &lex);
1340 if (yyparse(&closure) != 0)
1341 return false;
1343 if (!closure.saw_inputs())
1344 return true;
1346 Task_token* this_blocker = NULL;
1347 for (Input_arguments::const_iterator p = closure.inputs()->begin();
1348 p != closure.inputs()->end();
1349 ++p)
1351 Task_token* nb;
1352 if (p + 1 == closure.inputs()->end())
1353 nb = next_blocker;
1354 else
1356 nb = new Task_token(true);
1357 nb->add_blocker();
1359 workqueue->queue_soon(new Read_symbols(options, input_objects, symtab,
1360 layout, dirsearch, mapfile, &*p,
1361 input_group, this_blocker, nb));
1362 this_blocker = nb;
1365 *used_next_blocker = true;
1367 return true;
1370 // Helper function for read_version_script() and
1371 // read_commandline_script(). Processes the given file in the mode
1372 // indicated by first_token and lex_mode.
1374 static bool
1375 read_script_file(const char* filename, Command_line* cmdline,
1376 int first_token, Lex::Mode lex_mode)
1378 // TODO: if filename is a relative filename, search for it manually
1379 // using "." + cmdline->options()->search_path() -- not dirsearch.
1380 Dirsearch dirsearch;
1382 // The file locking code wants to record a Task, but we haven't
1383 // started the workqueue yet. This is only for debugging purposes,
1384 // so we invent a fake value.
1385 const Task* task = reinterpret_cast<const Task*>(-1);
1387 // We don't want this file to be opened in binary mode.
1388 Position_dependent_options posdep = cmdline->position_dependent_options();
1389 if (posdep.format_enum() == General_options::OBJECT_FORMAT_BINARY)
1390 posdep.set_format_enum(General_options::OBJECT_FORMAT_ELF);
1391 Input_file_argument input_argument(filename, false, "", false, posdep);
1392 Input_file input_file(&input_argument);
1393 if (!input_file.open(cmdline->options(), dirsearch, task))
1394 return false;
1396 std::string input_string;
1397 Lex::read_file(&input_file, &input_string);
1399 Lex lex(input_string.c_str(), input_string.length(), first_token);
1400 lex.set_mode(lex_mode);
1402 Parser_closure closure(filename,
1403 cmdline->position_dependent_options(),
1404 false,
1405 input_file.is_in_sysroot(),
1406 cmdline,
1407 &cmdline->script_options(),
1408 &lex);
1409 if (yyparse(&closure) != 0)
1411 input_file.file().unlock(task);
1412 return false;
1415 input_file.file().unlock(task);
1417 gold_assert(!closure.saw_inputs());
1419 return true;
1422 // FILENAME was found as an argument to --script (-T).
1423 // Read it as a script, and execute its contents immediately.
1425 bool
1426 read_commandline_script(const char* filename, Command_line* cmdline)
1428 return read_script_file(filename, cmdline,
1429 PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT);
1432 // FILE was found as an argument to --version-script. Read it as a
1433 // version script, and store its contents in
1434 // cmdline->script_options()->version_script_info().
1436 bool
1437 read_version_script(const char* filename, Command_line* cmdline)
1439 return read_script_file(filename, cmdline,
1440 PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT);
1443 // Implement the --defsym option on the command line. Return true if
1444 // all is well.
1446 bool
1447 Script_options::define_symbol(const char* definition)
1449 Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1450 lex.set_mode(Lex::EXPRESSION);
1452 // Dummy value.
1453 Position_dependent_options posdep_options;
1455 Parser_closure closure("command line", posdep_options, false, false, NULL,
1456 this, &lex);
1458 if (yyparse(&closure) != 0)
1459 return false;
1461 gold_assert(!closure.saw_inputs());
1463 return true;
1466 // Print the script to F for debugging.
1468 void
1469 Script_options::print(FILE* f) const
1471 fprintf(f, "%s: Dumping linker script\n", program_name);
1473 if (!this->entry_.empty())
1474 fprintf(f, "ENTRY(%s)\n", this->entry_.c_str());
1476 for (Symbol_assignments::const_iterator p =
1477 this->symbol_assignments_.begin();
1478 p != this->symbol_assignments_.end();
1479 ++p)
1480 (*p)->print(f);
1482 for (Assertions::const_iterator p = this->assertions_.begin();
1483 p != this->assertions_.end();
1484 ++p)
1485 (*p)->print(f);
1487 this->script_sections_.print(f);
1489 this->version_script_info_.print(f);
1492 // Manage mapping from keywords to the codes expected by the bison
1493 // parser. We construct one global object for each lex mode with
1494 // keywords.
1496 class Keyword_to_parsecode
1498 public:
1499 // The structure which maps keywords to parsecodes.
1500 struct Keyword_parsecode
1502 // Keyword.
1503 const char* keyword;
1504 // Corresponding parsecode.
1505 int parsecode;
1508 Keyword_to_parsecode(const Keyword_parsecode* keywords,
1509 int keyword_count)
1510 : keyword_parsecodes_(keywords), keyword_count_(keyword_count)
1513 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1514 // keyword.
1516 keyword_to_parsecode(const char* keyword, size_t len) const;
1518 private:
1519 const Keyword_parsecode* keyword_parsecodes_;
1520 const int keyword_count_;
1523 // Mapping from keyword string to keyword parsecode. This array must
1524 // be kept in sorted order. Parsecodes are looked up using bsearch.
1525 // This array must correspond to the list of parsecodes in yyscript.y.
1527 static const Keyword_to_parsecode::Keyword_parsecode
1528 script_keyword_parsecodes[] =
1530 { "ABSOLUTE", ABSOLUTE },
1531 { "ADDR", ADDR },
1532 { "ALIGN", ALIGN_K },
1533 { "ALIGNOF", ALIGNOF },
1534 { "ASSERT", ASSERT_K },
1535 { "AS_NEEDED", AS_NEEDED },
1536 { "AT", AT },
1537 { "BIND", BIND },
1538 { "BLOCK", BLOCK },
1539 { "BYTE", BYTE },
1540 { "CONSTANT", CONSTANT },
1541 { "CONSTRUCTORS", CONSTRUCTORS },
1542 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1543 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1544 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1545 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1546 { "DEFINED", DEFINED },
1547 { "ENTRY", ENTRY },
1548 { "EXCLUDE_FILE", EXCLUDE_FILE },
1549 { "EXTERN", EXTERN },
1550 { "FILL", FILL },
1551 { "FLOAT", FLOAT },
1552 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1553 { "GROUP", GROUP },
1554 { "HLL", HLL },
1555 { "INCLUDE", INCLUDE },
1556 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1557 { "INPUT", INPUT },
1558 { "KEEP", KEEP },
1559 { "LENGTH", LENGTH },
1560 { "LOADADDR", LOADADDR },
1561 { "LONG", LONG },
1562 { "MAP", MAP },
1563 { "MAX", MAX_K },
1564 { "MEMORY", MEMORY },
1565 { "MIN", MIN_K },
1566 { "NEXT", NEXT },
1567 { "NOCROSSREFS", NOCROSSREFS },
1568 { "NOFLOAT", NOFLOAT },
1569 { "ONLY_IF_RO", ONLY_IF_RO },
1570 { "ONLY_IF_RW", ONLY_IF_RW },
1571 { "OPTION", OPTION },
1572 { "ORIGIN", ORIGIN },
1573 { "OUTPUT", OUTPUT },
1574 { "OUTPUT_ARCH", OUTPUT_ARCH },
1575 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1576 { "OVERLAY", OVERLAY },
1577 { "PHDRS", PHDRS },
1578 { "PROVIDE", PROVIDE },
1579 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1580 { "QUAD", QUAD },
1581 { "SEARCH_DIR", SEARCH_DIR },
1582 { "SECTIONS", SECTIONS },
1583 { "SEGMENT_START", SEGMENT_START },
1584 { "SHORT", SHORT },
1585 { "SIZEOF", SIZEOF },
1586 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1587 { "SORT", SORT_BY_NAME },
1588 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1589 { "SORT_BY_NAME", SORT_BY_NAME },
1590 { "SPECIAL", SPECIAL },
1591 { "SQUAD", SQUAD },
1592 { "STARTUP", STARTUP },
1593 { "SUBALIGN", SUBALIGN },
1594 { "SYSLIB", SYSLIB },
1595 { "TARGET", TARGET_K },
1596 { "TRUNCATE", TRUNCATE },
1597 { "VERSION", VERSIONK },
1598 { "global", GLOBAL },
1599 { "l", LENGTH },
1600 { "len", LENGTH },
1601 { "local", LOCAL },
1602 { "o", ORIGIN },
1603 { "org", ORIGIN },
1604 { "sizeof_headers", SIZEOF_HEADERS },
1607 static const Keyword_to_parsecode
1608 script_keywords(&script_keyword_parsecodes[0],
1609 (sizeof(script_keyword_parsecodes)
1610 / sizeof(script_keyword_parsecodes[0])));
1612 static const Keyword_to_parsecode::Keyword_parsecode
1613 version_script_keyword_parsecodes[] =
1615 { "extern", EXTERN },
1616 { "global", GLOBAL },
1617 { "local", LOCAL },
1620 static const Keyword_to_parsecode
1621 version_script_keywords(&version_script_keyword_parsecodes[0],
1622 (sizeof(version_script_keyword_parsecodes)
1623 / sizeof(version_script_keyword_parsecodes[0])));
1625 // Comparison function passed to bsearch.
1627 extern "C"
1630 struct Ktt_key
1632 const char* str;
1633 size_t len;
1636 static int
1637 ktt_compare(const void* keyv, const void* kttv)
1639 const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
1640 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1641 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1642 int i = strncmp(key->str, ktt->keyword, key->len);
1643 if (i != 0)
1644 return i;
1645 if (ktt->keyword[key->len] != '\0')
1646 return -1;
1647 return 0;
1650 } // End extern "C".
1653 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword,
1654 size_t len) const
1656 Ktt_key key;
1657 key.str = keyword;
1658 key.len = len;
1659 void* kttv = bsearch(&key,
1660 this->keyword_parsecodes_,
1661 this->keyword_count_,
1662 sizeof(this->keyword_parsecodes_[0]),
1663 ktt_compare);
1664 if (kttv == NULL)
1665 return 0;
1666 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1667 return ktt->parsecode;
1670 // The following structs are used within the VersionInfo class as well
1671 // as in the bison helper functions. They store the information
1672 // parsed from the version script.
1674 // A single version expression.
1675 // For example, pattern="std::map*" and language="C++".
1676 // pattern and language should be from the stringpool
1677 struct Version_expression {
1678 Version_expression(const std::string& pattern,
1679 const std::string& language,
1680 bool exact_match)
1681 : pattern(pattern), language(language), exact_match(exact_match) {}
1683 std::string pattern;
1684 std::string language;
1685 // If false, we use glob() to match pattern. If true, we use strcmp().
1686 bool exact_match;
1690 // A list of expressions.
1691 struct Version_expression_list {
1692 std::vector<struct Version_expression> expressions;
1696 // A list of which versions upon which another version depends.
1697 // Strings should be from the Stringpool.
1698 struct Version_dependency_list {
1699 std::vector<std::string> dependencies;
1703 // The total definition of a version. It includes the tag for the
1704 // version, its global and local expressions, and any dependencies.
1705 struct Version_tree {
1706 Version_tree()
1707 : tag(), global(NULL), local(NULL), dependencies(NULL) {}
1709 std::string tag;
1710 const struct Version_expression_list* global;
1711 const struct Version_expression_list* local;
1712 const struct Version_dependency_list* dependencies;
1715 Version_script_info::~Version_script_info()
1717 this->clear();
1720 void
1721 Version_script_info::clear()
1723 for (size_t k = 0; k < dependency_lists_.size(); ++k)
1724 delete dependency_lists_[k];
1725 this->dependency_lists_.clear();
1726 for (size_t k = 0; k < version_trees_.size(); ++k)
1727 delete version_trees_[k];
1728 this->version_trees_.clear();
1729 for (size_t k = 0; k < expression_lists_.size(); ++k)
1730 delete expression_lists_[k];
1731 this->expression_lists_.clear();
1734 std::vector<std::string>
1735 Version_script_info::get_versions() const
1737 std::vector<std::string> ret;
1738 for (size_t j = 0; j < version_trees_.size(); ++j)
1739 if (!this->version_trees_[j]->tag.empty())
1740 ret.push_back(this->version_trees_[j]->tag);
1741 return ret;
1744 std::vector<std::string>
1745 Version_script_info::get_dependencies(const char* version) const
1747 std::vector<std::string> ret;
1748 for (size_t j = 0; j < version_trees_.size(); ++j)
1749 if (version_trees_[j]->tag == version)
1751 const struct Version_dependency_list* deps =
1752 version_trees_[j]->dependencies;
1753 if (deps != NULL)
1754 for (size_t k = 0; k < deps->dependencies.size(); ++k)
1755 ret.push_back(deps->dependencies[k]);
1756 return ret;
1758 return ret;
1761 // Look up SYMBOL_NAME in the list of versions. If CHECK_GLOBAL is
1762 // true look at the globally visible symbols, otherwise look at the
1763 // symbols listed as "local:". Return true if the symbol is found,
1764 // false otherwise. If the symbol is found, then if PVERSION is not
1765 // NULL, set *PVERSION to the version.
1767 bool
1768 Version_script_info::get_symbol_version_helper(const char* symbol_name,
1769 bool check_global,
1770 std::string* pversion) const
1772 for (size_t j = 0; j < version_trees_.size(); ++j)
1774 // Is it a global symbol for this version?
1775 const Version_expression_list* explist =
1776 check_global ? version_trees_[j]->global : version_trees_[j]->local;
1777 if (explist != NULL)
1778 for (size_t k = 0; k < explist->expressions.size(); ++k)
1780 const char* name_to_match = symbol_name;
1781 const struct Version_expression& exp = explist->expressions[k];
1782 char* demangled_name = NULL;
1783 if (exp.language == "C++")
1785 demangled_name = cplus_demangle(symbol_name,
1786 DMGL_ANSI | DMGL_PARAMS);
1787 // This isn't a C++ symbol.
1788 if (demangled_name == NULL)
1789 continue;
1790 name_to_match = demangled_name;
1792 else if (exp.language == "Java")
1794 demangled_name = cplus_demangle(symbol_name,
1795 (DMGL_ANSI | DMGL_PARAMS
1796 | DMGL_JAVA));
1797 // This isn't a Java symbol.
1798 if (demangled_name == NULL)
1799 continue;
1800 name_to_match = demangled_name;
1802 bool matched;
1803 if (exp.exact_match)
1804 matched = strcmp(exp.pattern.c_str(), name_to_match) == 0;
1805 else
1806 matched = fnmatch(exp.pattern.c_str(), name_to_match,
1807 FNM_NOESCAPE) == 0;
1808 if (demangled_name != NULL)
1809 free(demangled_name);
1810 if (matched)
1812 if (pversion != NULL)
1813 *pversion = this->version_trees_[j]->tag;
1814 return true;
1818 return false;
1821 struct Version_dependency_list*
1822 Version_script_info::allocate_dependency_list()
1824 dependency_lists_.push_back(new Version_dependency_list);
1825 return dependency_lists_.back();
1828 struct Version_expression_list*
1829 Version_script_info::allocate_expression_list()
1831 expression_lists_.push_back(new Version_expression_list);
1832 return expression_lists_.back();
1835 struct Version_tree*
1836 Version_script_info::allocate_version_tree()
1838 version_trees_.push_back(new Version_tree);
1839 return version_trees_.back();
1842 // Print for debugging.
1844 void
1845 Version_script_info::print(FILE* f) const
1847 if (this->empty())
1848 return;
1850 fprintf(f, "VERSION {");
1852 for (size_t i = 0; i < this->version_trees_.size(); ++i)
1854 const Version_tree* vt = this->version_trees_[i];
1856 if (vt->tag.empty())
1857 fprintf(f, " {\n");
1858 else
1859 fprintf(f, " %s {\n", vt->tag.c_str());
1861 if (vt->global != NULL)
1863 fprintf(f, " global :\n");
1864 this->print_expression_list(f, vt->global);
1867 if (vt->local != NULL)
1869 fprintf(f, " local :\n");
1870 this->print_expression_list(f, vt->local);
1873 fprintf(f, " }");
1874 if (vt->dependencies != NULL)
1876 const Version_dependency_list* deps = vt->dependencies;
1877 for (size_t j = 0; j < deps->dependencies.size(); ++j)
1879 if (j < deps->dependencies.size() - 1)
1880 fprintf(f, "\n");
1881 fprintf(f, " %s", deps->dependencies[j].c_str());
1884 fprintf(f, ";\n");
1887 fprintf(f, "}\n");
1890 void
1891 Version_script_info::print_expression_list(
1892 FILE* f,
1893 const Version_expression_list* vel) const
1895 std::string current_language;
1896 for (size_t i = 0; i < vel->expressions.size(); ++i)
1898 const Version_expression& ve(vel->expressions[i]);
1900 if (ve.language != current_language)
1902 if (!current_language.empty())
1903 fprintf(f, " }\n");
1904 fprintf(f, " extern \"%s\" {\n", ve.language.c_str());
1905 current_language = ve.language;
1908 fprintf(f, " ");
1909 if (!current_language.empty())
1910 fprintf(f, " ");
1912 if (ve.exact_match)
1913 fprintf(f, "\"");
1914 fprintf(f, "%s", ve.pattern.c_str());
1915 if (ve.exact_match)
1916 fprintf(f, "\"");
1918 fprintf(f, "\n");
1921 if (!current_language.empty())
1922 fprintf(f, " }\n");
1925 } // End namespace gold.
1927 // The remaining functions are extern "C", so it's clearer to not put
1928 // them in namespace gold.
1930 using namespace gold;
1932 // This function is called by the bison parser to return the next
1933 // token.
1935 extern "C" int
1936 yylex(YYSTYPE* lvalp, void* closurev)
1938 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1939 const Token* token = closure->next_token();
1940 switch (token->classification())
1942 default:
1943 gold_unreachable();
1945 case Token::TOKEN_INVALID:
1946 yyerror(closurev, "invalid character");
1947 return 0;
1949 case Token::TOKEN_EOF:
1950 return 0;
1952 case Token::TOKEN_STRING:
1954 // This is either a keyword or a STRING.
1955 size_t len;
1956 const char* str = token->string_value(&len);
1957 int parsecode = 0;
1958 switch (closure->lex_mode())
1960 case Lex::LINKER_SCRIPT:
1961 parsecode = script_keywords.keyword_to_parsecode(str, len);
1962 break;
1963 case Lex::VERSION_SCRIPT:
1964 parsecode = version_script_keywords.keyword_to_parsecode(str, len);
1965 break;
1966 default:
1967 break;
1969 if (parsecode != 0)
1970 return parsecode;
1971 lvalp->string.value = str;
1972 lvalp->string.length = len;
1973 return STRING;
1976 case Token::TOKEN_QUOTED_STRING:
1977 lvalp->string.value = token->string_value(&lvalp->string.length);
1978 return QUOTED_STRING;
1980 case Token::TOKEN_OPERATOR:
1981 return token->operator_value();
1983 case Token::TOKEN_INTEGER:
1984 lvalp->integer = token->integer_value();
1985 return INTEGER;
1989 // This function is called by the bison parser to report an error.
1991 extern "C" void
1992 yyerror(void* closurev, const char* message)
1994 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1995 gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
1996 closure->charpos(), message);
1999 // Called by the bison parser to add a file to the link.
2001 extern "C" void
2002 script_add_file(void* closurev, const char* name, size_t length)
2004 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2006 // If this is an absolute path, and we found the script in the
2007 // sysroot, then we want to prepend the sysroot to the file name.
2008 // For example, this is how we handle a cross link to the x86_64
2009 // libc.so, which refers to /lib/libc.so.6.
2010 std::string name_string(name, length);
2011 const char* extra_search_path = ".";
2012 std::string script_directory;
2013 if (IS_ABSOLUTE_PATH(name_string.c_str()))
2015 if (closure->is_in_sysroot())
2017 const std::string& sysroot(parameters->options().sysroot());
2018 gold_assert(!sysroot.empty());
2019 name_string = sysroot + name_string;
2022 else
2024 // In addition to checking the normal library search path, we
2025 // also want to check in the script-directory.
2026 const char *slash = strrchr(closure->filename(), '/');
2027 if (slash != NULL)
2029 script_directory.assign(closure->filename(),
2030 slash - closure->filename() + 1);
2031 extra_search_path = script_directory.c_str();
2035 Input_file_argument file(name_string.c_str(), false, extra_search_path,
2036 false, closure->position_dependent_options());
2037 closure->inputs()->add_file(file);
2040 // Called by the bison parser to start a group. If we are already in
2041 // a group, that means that this script was invoked within a
2042 // --start-group --end-group sequence on the command line, or that
2043 // this script was found in a GROUP of another script. In that case,
2044 // we simply continue the existing group, rather than starting a new
2045 // one. It is possible to construct a case in which this will do
2046 // something other than what would happen if we did a recursive group,
2047 // but it's hard to imagine why the different behaviour would be
2048 // useful for a real program. Avoiding recursive groups is simpler
2049 // and more efficient.
2051 extern "C" void
2052 script_start_group(void* closurev)
2054 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2055 if (!closure->in_group())
2056 closure->inputs()->start_group();
2059 // Called by the bison parser at the end of a group.
2061 extern "C" void
2062 script_end_group(void* closurev)
2064 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2065 if (!closure->in_group())
2066 closure->inputs()->end_group();
2069 // Called by the bison parser to start an AS_NEEDED list.
2071 extern "C" void
2072 script_start_as_needed(void* closurev)
2074 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2075 closure->position_dependent_options().set_as_needed(true);
2078 // Called by the bison parser at the end of an AS_NEEDED list.
2080 extern "C" void
2081 script_end_as_needed(void* closurev)
2083 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2084 closure->position_dependent_options().set_as_needed(false);
2087 // Called by the bison parser to set the entry symbol.
2089 extern "C" void
2090 script_set_entry(void* closurev, const char* entry, size_t length)
2092 // We'll parse this exactly the same as --entry=ENTRY on the commandline
2093 // TODO(csilvers): FIXME -- call set_entry directly.
2094 std::string arg("--entry=");
2095 arg.append(entry, length);
2096 script_parse_option(closurev, arg.c_str(), arg.size());
2099 // Called by the bison parser to set whether to define common symbols.
2101 extern "C" void
2102 script_set_common_allocation(void* closurev, int set)
2104 const char* arg = set != 0 ? "--define-common" : "--no-define-common";
2105 script_parse_option(closurev, arg, strlen(arg));
2108 // Called by the bison parser to define a symbol.
2110 extern "C" void
2111 script_set_symbol(void* closurev, const char* name, size_t length,
2112 Expression* value, int providei, int hiddeni)
2114 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2115 const bool provide = providei != 0;
2116 const bool hidden = hiddeni != 0;
2117 closure->script_options()->add_symbol_assignment(name, length, value,
2118 provide, hidden);
2121 // Called by the bison parser to add an assertion.
2123 extern "C" void
2124 script_add_assertion(void* closurev, Expression* check, const char* message,
2125 size_t messagelen)
2127 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2128 closure->script_options()->add_assertion(check, message, messagelen);
2131 // Called by the bison parser to parse an OPTION.
2133 extern "C" void
2134 script_parse_option(void* closurev, const char* option, size_t length)
2136 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2137 // We treat the option as a single command-line option, even if
2138 // it has internal whitespace.
2139 if (closure->command_line() == NULL)
2141 // There are some options that we could handle here--e.g.,
2142 // -lLIBRARY. Should we bother?
2143 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
2144 " for scripts specified via -T/--script"),
2145 closure->filename(), closure->lineno(), closure->charpos());
2147 else
2149 bool past_a_double_dash_option = false;
2150 const char* mutable_option = strndup(option, length);
2151 gold_assert(mutable_option != NULL);
2152 closure->command_line()->process_one_option(1, &mutable_option, 0,
2153 &past_a_double_dash_option);
2154 // The General_options class will quite possibly store a pointer
2155 // into mutable_option, so we can't free it. In cases the class
2156 // does not store such a pointer, this is a memory leak. Alas. :(
2160 // Called by the bison parser to handle SEARCH_DIR. This is handled
2161 // exactly like a -L option.
2163 extern "C" void
2164 script_add_search_dir(void* closurev, const char* option, size_t length)
2166 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2167 if (closure->command_line() == NULL)
2168 gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid"
2169 " for scripts specified via -T/--script"),
2170 closure->filename(), closure->lineno(), closure->charpos());
2171 else
2173 std::string s = "-L" + std::string(option, length);
2174 script_parse_option(closurev, s.c_str(), s.size());
2178 /* Called by the bison parser to push the lexer into expression
2179 mode. */
2181 extern "C" void
2182 script_push_lex_into_expression_mode(void* closurev)
2184 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2185 closure->push_lex_mode(Lex::EXPRESSION);
2188 /* Called by the bison parser to push the lexer into version
2189 mode. */
2191 extern "C" void
2192 script_push_lex_into_version_mode(void* closurev)
2194 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2195 closure->push_lex_mode(Lex::VERSION_SCRIPT);
2198 /* Called by the bison parser to pop the lexer mode. */
2200 extern "C" void
2201 script_pop_lex_mode(void* closurev)
2203 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2204 closure->pop_lex_mode();
2207 // Register an entire version node. For example:
2209 // GLIBC_2.1 {
2210 // global: foo;
2211 // } GLIBC_2.0;
2213 // - tag is "GLIBC_2.1"
2214 // - tree contains the information "global: foo"
2215 // - deps contains "GLIBC_2.0"
2217 extern "C" void
2218 script_register_vers_node(void*,
2219 const char* tag,
2220 int taglen,
2221 struct Version_tree *tree,
2222 struct Version_dependency_list *deps)
2224 gold_assert(tree != NULL);
2225 tree->dependencies = deps;
2226 if (tag != NULL)
2227 tree->tag = std::string(tag, taglen);
2230 // Add a dependencies to the list of existing dependencies, if any,
2231 // and return the expanded list.
2233 extern "C" struct Version_dependency_list *
2234 script_add_vers_depend(void* closurev,
2235 struct Version_dependency_list *all_deps,
2236 const char *depend_to_add, int deplen)
2238 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2239 if (all_deps == NULL)
2240 all_deps = closure->version_script()->allocate_dependency_list();
2241 all_deps->dependencies.push_back(std::string(depend_to_add, deplen));
2242 return all_deps;
2245 // Add a pattern expression to an existing list of expressions, if any.
2246 // TODO: In the old linker, the last argument used to be a bool, but I
2247 // don't know what it meant.
2249 extern "C" struct Version_expression_list *
2250 script_new_vers_pattern(void* closurev,
2251 struct Version_expression_list *expressions,
2252 const char *pattern, int patlen, int exact_match)
2254 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2255 if (expressions == NULL)
2256 expressions = closure->version_script()->allocate_expression_list();
2257 expressions->expressions.push_back(
2258 Version_expression(std::string(pattern, patlen),
2259 closure->get_current_language(),
2260 static_cast<bool>(exact_match)));
2261 return expressions;
2264 // Attaches b to the end of a, and clears b. So a = a + b and b = {}.
2266 extern "C" struct Version_expression_list*
2267 script_merge_expressions(struct Version_expression_list *a,
2268 struct Version_expression_list *b)
2270 a->expressions.insert(a->expressions.end(),
2271 b->expressions.begin(), b->expressions.end());
2272 // We could delete b and remove it from expressions_lists_, but
2273 // that's a lot of work. This works just as well.
2274 b->expressions.clear();
2275 return a;
2278 // Combine the global and local expressions into a a Version_tree.
2280 extern "C" struct Version_tree *
2281 script_new_vers_node(void* closurev,
2282 struct Version_expression_list *global,
2283 struct Version_expression_list *local)
2285 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2286 Version_tree* tree = closure->version_script()->allocate_version_tree();
2287 tree->global = global;
2288 tree->local = local;
2289 return tree;
2292 // Handle a transition in language, such as at the
2293 // start or end of 'extern "C++"'
2295 extern "C" void
2296 version_script_push_lang(void* closurev, const char* lang, int langlen)
2298 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2299 closure->push_language(std::string(lang, langlen));
2302 extern "C" void
2303 version_script_pop_lang(void* closurev)
2305 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2306 closure->pop_language();
2309 // Called by the bison parser to start a SECTIONS clause.
2311 extern "C" void
2312 script_start_sections(void* closurev)
2314 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2315 closure->script_options()->script_sections()->start_sections();
2318 // Called by the bison parser to finish a SECTIONS clause.
2320 extern "C" void
2321 script_finish_sections(void* closurev)
2323 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2324 closure->script_options()->script_sections()->finish_sections();
2327 // Start processing entries for an output section.
2329 extern "C" void
2330 script_start_output_section(void* closurev, const char* name, size_t namelen,
2331 const struct Parser_output_section_header* header)
2333 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2334 closure->script_options()->script_sections()->start_output_section(name,
2335 namelen,
2336 header);
2339 // Finish processing entries for an output section.
2341 extern "C" void
2342 script_finish_output_section(void* closurev,
2343 const struct Parser_output_section_trailer* trail)
2345 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2346 closure->script_options()->script_sections()->finish_output_section(trail);
2349 // Add a data item (e.g., "WORD (0)") to the current output section.
2351 extern "C" void
2352 script_add_data(void* closurev, int data_token, Expression* val)
2354 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2355 int size;
2356 bool is_signed = true;
2357 switch (data_token)
2359 case QUAD:
2360 size = 8;
2361 is_signed = false;
2362 break;
2363 case SQUAD:
2364 size = 8;
2365 break;
2366 case LONG:
2367 size = 4;
2368 break;
2369 case SHORT:
2370 size = 2;
2371 break;
2372 case BYTE:
2373 size = 1;
2374 break;
2375 default:
2376 gold_unreachable();
2378 closure->script_options()->script_sections()->add_data(size, is_signed, val);
2381 // Add a clause setting the fill value to the current output section.
2383 extern "C" void
2384 script_add_fill(void* closurev, Expression* val)
2386 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2387 closure->script_options()->script_sections()->add_fill(val);
2390 // Add a new input section specification to the current output
2391 // section.
2393 extern "C" void
2394 script_add_input_section(void* closurev,
2395 const struct Input_section_spec* spec,
2396 int keepi)
2398 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2399 bool keep = keepi != 0;
2400 closure->script_options()->script_sections()->add_input_section(spec, keep);
2403 // Create a new list of string/sort pairs.
2405 extern "C" String_sort_list_ptr
2406 script_new_string_sort_list(const struct Wildcard_section* string_sort)
2408 return new String_sort_list(1, *string_sort);
2411 // Add an entry to a list of string/sort pairs. The way the parser
2412 // works permits us to simply modify the first parameter, rather than
2413 // copy the vector.
2415 extern "C" String_sort_list_ptr
2416 script_string_sort_list_add(String_sort_list_ptr pv,
2417 const struct Wildcard_section* string_sort)
2419 if (pv == NULL)
2420 return script_new_string_sort_list(string_sort);
2421 else
2423 pv->push_back(*string_sort);
2424 return pv;
2428 // Create a new list of strings.
2430 extern "C" String_list_ptr
2431 script_new_string_list(const char* str, size_t len)
2433 return new String_list(1, std::string(str, len));
2436 // Add an element to a list of strings. The way the parser works
2437 // permits us to simply modify the first parameter, rather than copy
2438 // the vector.
2440 extern "C" String_list_ptr
2441 script_string_list_push_back(String_list_ptr pv, const char* str, size_t len)
2443 if (pv == NULL)
2444 return script_new_string_list(str, len);
2445 else
2447 pv->push_back(std::string(str, len));
2448 return pv;
2452 // Concatenate two string lists. Either or both may be NULL. The way
2453 // the parser works permits us to modify the parameters, rather than
2454 // copy the vector.
2456 extern "C" String_list_ptr
2457 script_string_list_append(String_list_ptr pv1, String_list_ptr pv2)
2459 if (pv1 == NULL)
2460 return pv2;
2461 if (pv2 == NULL)
2462 return pv1;
2463 pv1->insert(pv1->end(), pv2->begin(), pv2->end());
2464 return pv1;
2467 // Add a new program header.
2469 extern "C" void
2470 script_add_phdr(void* closurev, const char* name, size_t namelen,
2471 unsigned int type, const Phdr_info* info)
2473 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2474 bool includes_filehdr = info->includes_filehdr != 0;
2475 bool includes_phdrs = info->includes_phdrs != 0;
2476 bool is_flags_valid = info->is_flags_valid != 0;
2477 Script_sections* ss = closure->script_options()->script_sections();
2478 ss->add_phdr(name, namelen, type, includes_filehdr, includes_phdrs,
2479 is_flags_valid, info->flags, info->load_address);
2482 // Convert a program header string to a type.
2484 #define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME }
2486 static struct
2488 const char* name;
2489 size_t namelen;
2490 unsigned int val;
2491 } phdr_type_names[] =
2493 PHDR_TYPE(PT_NULL),
2494 PHDR_TYPE(PT_LOAD),
2495 PHDR_TYPE(PT_DYNAMIC),
2496 PHDR_TYPE(PT_INTERP),
2497 PHDR_TYPE(PT_NOTE),
2498 PHDR_TYPE(PT_SHLIB),
2499 PHDR_TYPE(PT_PHDR),
2500 PHDR_TYPE(PT_TLS),
2501 PHDR_TYPE(PT_GNU_EH_FRAME),
2502 PHDR_TYPE(PT_GNU_STACK),
2503 PHDR_TYPE(PT_GNU_RELRO)
2506 extern "C" unsigned int
2507 script_phdr_string_to_type(void* closurev, const char* name, size_t namelen)
2509 for (unsigned int i = 0;
2510 i < sizeof(phdr_type_names) / sizeof(phdr_type_names[0]);
2511 ++i)
2512 if (namelen == phdr_type_names[i].namelen
2513 && strncmp(name, phdr_type_names[i].name, namelen) == 0)
2514 return phdr_type_names[i].val;
2515 yyerror(closurev, _("unknown PHDR type (try integer)"));
2516 return elfcpp::PT_NULL;