* elf32-spu.c (mark_functions_via_relocs): Handle cycles in the
[binutils.git] / gold / script.cc
blob30b4e3c351a923e4b57e8aff1cd727f514631ba6
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 "target-select.h"
44 #include "script.h"
45 #include "script-c.h"
47 namespace gold
50 // A token read from a script file. We don't implement keywords here;
51 // all keywords are simply represented as a string.
53 class Token
55 public:
56 // Token classification.
57 enum Classification
59 // Token is invalid.
60 TOKEN_INVALID,
61 // Token indicates end of input.
62 TOKEN_EOF,
63 // Token is a string of characters.
64 TOKEN_STRING,
65 // Token is a quoted string of characters.
66 TOKEN_QUOTED_STRING,
67 // Token is an operator.
68 TOKEN_OPERATOR,
69 // Token is a number (an integer).
70 TOKEN_INTEGER
73 // We need an empty constructor so that we can put this STL objects.
74 Token()
75 : classification_(TOKEN_INVALID), value_(NULL), value_length_(0),
76 opcode_(0), lineno_(0), charpos_(0)
77 { }
79 // A general token with no value.
80 Token(Classification classification, int lineno, int charpos)
81 : classification_(classification), value_(NULL), value_length_(0),
82 opcode_(0), lineno_(lineno), charpos_(charpos)
84 gold_assert(classification == TOKEN_INVALID
85 || classification == TOKEN_EOF);
88 // A general token with a value.
89 Token(Classification classification, const char* value, size_t length,
90 int lineno, int charpos)
91 : classification_(classification), value_(value), value_length_(length),
92 opcode_(0), lineno_(lineno), charpos_(charpos)
94 gold_assert(classification != TOKEN_INVALID
95 && classification != TOKEN_EOF);
98 // A token representing an operator.
99 Token(int opcode, int lineno, int charpos)
100 : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0),
101 opcode_(opcode), lineno_(lineno), charpos_(charpos)
104 // Return whether the token is invalid.
105 bool
106 is_invalid() const
107 { return this->classification_ == TOKEN_INVALID; }
109 // Return whether this is an EOF token.
110 bool
111 is_eof() const
112 { return this->classification_ == TOKEN_EOF; }
114 // Return the token classification.
115 Classification
116 classification() const
117 { return this->classification_; }
119 // Return the line number at which the token starts.
121 lineno() const
122 { return this->lineno_; }
124 // Return the character position at this the token starts.
126 charpos() const
127 { return this->charpos_; }
129 // Get the value of a token.
131 const char*
132 string_value(size_t* length) const
134 gold_assert(this->classification_ == TOKEN_STRING
135 || this->classification_ == TOKEN_QUOTED_STRING);
136 *length = this->value_length_;
137 return this->value_;
141 operator_value() const
143 gold_assert(this->classification_ == TOKEN_OPERATOR);
144 return this->opcode_;
147 uint64_t
148 integer_value() const
150 gold_assert(this->classification_ == TOKEN_INTEGER);
151 // Null terminate.
152 std::string s(this->value_, this->value_length_);
153 return strtoull(s.c_str(), NULL, 0);
156 private:
157 // The token classification.
158 Classification classification_;
159 // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or
160 // TOKEN_INTEGER.
161 const char* value_;
162 // The length of the token value.
163 size_t value_length_;
164 // The token value, for TOKEN_OPERATOR.
165 int opcode_;
166 // The line number where this token started (one based).
167 int lineno_;
168 // The character position within the line where this token started
169 // (one based).
170 int charpos_;
173 // This class handles lexing a file into a sequence of tokens.
175 class Lex
177 public:
178 // We unfortunately have to support different lexing modes, because
179 // when reading different parts of a linker script we need to parse
180 // things differently.
181 enum Mode
183 // Reading an ordinary linker script.
184 LINKER_SCRIPT,
185 // Reading an expression in a linker script.
186 EXPRESSION,
187 // Reading a version script.
188 VERSION_SCRIPT,
189 // Reading a --dynamic-list file.
190 DYNAMIC_LIST
193 Lex(const char* input_string, size_t input_length, int parsing_token)
194 : input_string_(input_string), input_length_(input_length),
195 current_(input_string), mode_(LINKER_SCRIPT),
196 first_token_(parsing_token), token_(),
197 lineno_(1), linestart_(input_string)
200 // Read a file into a string.
201 static void
202 read_file(Input_file*, std::string*);
204 // Return the next token.
205 const Token*
206 next_token();
208 // Return the current lexing mode.
209 Lex::Mode
210 mode() const
211 { return this->mode_; }
213 // Set the lexing mode.
214 void
215 set_mode(Mode mode)
216 { this->mode_ = mode; }
218 private:
219 Lex(const Lex&);
220 Lex& operator=(const Lex&);
222 // Make a general token with no value at the current location.
223 Token
224 make_token(Token::Classification c, const char* start) const
225 { return Token(c, this->lineno_, start - this->linestart_ + 1); }
227 // Make a general token with a value at the current location.
228 Token
229 make_token(Token::Classification c, const char* v, size_t len,
230 const char* start)
231 const
232 { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); }
234 // Make an operator token at the current location.
235 Token
236 make_token(int opcode, const char* start) const
237 { return Token(opcode, this->lineno_, start - this->linestart_ + 1); }
239 // Make an invalid token at the current location.
240 Token
241 make_invalid_token(const char* start)
242 { return this->make_token(Token::TOKEN_INVALID, start); }
244 // Make an EOF token at the current location.
245 Token
246 make_eof_token(const char* start)
247 { return this->make_token(Token::TOKEN_EOF, start); }
249 // Return whether C can be the first character in a name. C2 is the
250 // next character, since we sometimes need that.
251 inline bool
252 can_start_name(char c, char c2);
254 // If C can appear in a name which has already started, return a
255 // pointer to a character later in the token or just past
256 // it. Otherwise, return NULL.
257 inline const char*
258 can_continue_name(const char* c);
260 // Return whether C, C2, C3 can start a hex number.
261 inline bool
262 can_start_hex(char c, char c2, char c3);
264 // If C can appear in a hex number which has already started, return
265 // a pointer to a character later in the token or just past
266 // it. Otherwise, return NULL.
267 inline const char*
268 can_continue_hex(const char* c);
270 // Return whether C can start a non-hex number.
271 static inline bool
272 can_start_number(char c);
274 // If C can appear in a decimal number which has already started,
275 // return a pointer to a character later in the token or just past
276 // it. Otherwise, return NULL.
277 inline const char*
278 can_continue_number(const char* c)
279 { return Lex::can_start_number(*c) ? c + 1 : NULL; }
281 // If C1 C2 C3 form a valid three character operator, return the
282 // opcode. Otherwise return 0.
283 static inline int
284 three_char_operator(char c1, char c2, char c3);
286 // If C1 C2 form a valid two character operator, return the opcode.
287 // Otherwise return 0.
288 static inline int
289 two_char_operator(char c1, char c2);
291 // If C1 is a valid one character operator, return the opcode.
292 // Otherwise return 0.
293 static inline int
294 one_char_operator(char c1);
296 // Read the next token.
297 Token
298 get_token(const char**);
300 // Skip a C style /* */ comment. Return false if the comment did
301 // not end.
302 bool
303 skip_c_comment(const char**);
305 // Skip a line # comment. Return false if there was no newline.
306 bool
307 skip_line_comment(const char**);
309 // Build a token CLASSIFICATION from all characters that match
310 // CAN_CONTINUE_FN. The token starts at START. Start matching from
311 // MATCH. Set *PP to the character following the token.
312 inline Token
313 gather_token(Token::Classification,
314 const char* (Lex::*can_continue_fn)(const char*),
315 const char* start, const char* match, const char** pp);
317 // Build a token from a quoted string.
318 Token
319 gather_quoted_string(const char** pp);
321 // The string we are tokenizing.
322 const char* input_string_;
323 // The length of the string.
324 size_t input_length_;
325 // The current offset into the string.
326 const char* current_;
327 // The current lexing mode.
328 Mode mode_;
329 // The code to use for the first token. This is set to 0 after it
330 // is used.
331 int first_token_;
332 // The current token.
333 Token token_;
334 // The current line number.
335 int lineno_;
336 // The start of the current line in the string.
337 const char* linestart_;
340 // Read the whole file into memory. We don't expect linker scripts to
341 // be large, so we just use a std::string as a buffer. We ignore the
342 // data we've already read, so that we read aligned buffers.
344 void
345 Lex::read_file(Input_file* input_file, std::string* contents)
347 off_t filesize = input_file->file().filesize();
348 contents->clear();
349 contents->reserve(filesize);
351 off_t off = 0;
352 unsigned char buf[BUFSIZ];
353 while (off < filesize)
355 off_t get = BUFSIZ;
356 if (get > filesize - off)
357 get = filesize - off;
358 input_file->file().read(off, get, buf);
359 contents->append(reinterpret_cast<char*>(&buf[0]), get);
360 off += get;
364 // Return whether C can be the start of a name, if the next character
365 // is C2. A name can being with a letter, underscore, period, or
366 // dollar sign. Because a name can be a file name, we also permit
367 // forward slash, backslash, and tilde. Tilde is the tricky case
368 // here; GNU ld also uses it as a bitwise not operator. It is only
369 // recognized as the operator if it is not immediately followed by
370 // some character which can appear in a symbol. That is, when we
371 // don't know that we are looking at an expression, "~0" is a file
372 // name, and "~ 0" is an expression using bitwise not. We are
373 // compatible.
375 inline bool
376 Lex::can_start_name(char c, char c2)
378 switch (c)
380 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
381 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
382 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
383 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
384 case 'Y': case 'Z':
385 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
386 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
387 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
388 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
389 case 'y': case 'z':
390 case '_': case '.': case '$':
391 return true;
393 case '/': case '\\':
394 return this->mode_ == LINKER_SCRIPT;
396 case '~':
397 return this->mode_ == LINKER_SCRIPT && can_continue_name(&c2);
399 case '*': case '[':
400 return (this->mode_ == VERSION_SCRIPT
401 || this->mode_ == DYNAMIC_LIST
402 || (this->mode_ == LINKER_SCRIPT
403 && can_continue_name(&c2)));
405 default:
406 return false;
410 // Return whether C can continue a name which has already started.
411 // Subsequent characters in a name are the same as the leading
412 // characters, plus digits and "=+-:[],?*". So in general the linker
413 // script language requires spaces around operators, unless we know
414 // that we are parsing an expression.
416 inline const char*
417 Lex::can_continue_name(const char* c)
419 switch (*c)
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 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
427 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
428 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
429 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
430 case 'y': case 'z':
431 case '_': case '.': case '$':
432 case '0': case '1': case '2': case '3': case '4':
433 case '5': case '6': case '7': case '8': case '9':
434 return c + 1;
436 // TODO(csilvers): why not allow ~ in names for version-scripts?
437 case '/': case '\\': case '~':
438 case '=': case '+':
439 case ',':
440 if (this->mode_ == LINKER_SCRIPT)
441 return c + 1;
442 return NULL;
444 case '[': case ']': case '*': case '?': case '-':
445 if (this->mode_ == LINKER_SCRIPT || this->mode_ == VERSION_SCRIPT
446 || this->mode_ == DYNAMIC_LIST)
447 return c + 1;
448 return NULL;
450 // TODO(csilvers): why allow this? ^ is meaningless in version scripts.
451 case '^':
452 if (this->mode_ == VERSION_SCRIPT || this->mode_ == DYNAMIC_LIST)
453 return c + 1;
454 return NULL;
456 case ':':
457 if (this->mode_ == LINKER_SCRIPT)
458 return c + 1;
459 else if ((this->mode_ == VERSION_SCRIPT || this->mode_ == DYNAMIC_LIST)
460 && (c[1] == ':'))
462 // A name can have '::' in it, as that's a c++ namespace
463 // separator. But a single colon is not part of a name.
464 return c + 2;
466 return NULL;
468 default:
469 return NULL;
473 // For a number we accept 0x followed by hex digits, or any sequence
474 // of digits. The old linker accepts leading '$' for hex, and
475 // trailing HXBOD. Those are for MRI compatibility and we don't
476 // accept them. The old linker also accepts trailing MK for mega or
477 // kilo. FIXME: Those are mentioned in the documentation, and we
478 // should accept them.
480 // Return whether C1 C2 C3 can start a hex number.
482 inline bool
483 Lex::can_start_hex(char c1, char c2, char c3)
485 if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
486 return this->can_continue_hex(&c3);
487 return false;
490 // Return whether C can appear in a hex number.
492 inline const char*
493 Lex::can_continue_hex(const char* c)
495 switch (*c)
497 case '0': case '1': case '2': case '3': case '4':
498 case '5': case '6': case '7': case '8': case '9':
499 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
500 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
501 return c + 1;
503 default:
504 return NULL;
508 // Return whether C can start a non-hex number.
510 inline bool
511 Lex::can_start_number(char c)
513 switch (c)
515 case '0': case '1': case '2': case '3': case '4':
516 case '5': case '6': case '7': case '8': case '9':
517 return true;
519 default:
520 return false;
524 // If C1 C2 C3 form a valid three character operator, return the
525 // opcode (defined in the yyscript.h file generated from yyscript.y).
526 // Otherwise return 0.
528 inline int
529 Lex::three_char_operator(char c1, char c2, char c3)
531 switch (c1)
533 case '<':
534 if (c2 == '<' && c3 == '=')
535 return LSHIFTEQ;
536 break;
537 case '>':
538 if (c2 == '>' && c3 == '=')
539 return RSHIFTEQ;
540 break;
541 default:
542 break;
544 return 0;
547 // If C1 C2 form a valid two character operator, return the opcode
548 // (defined in the yyscript.h file generated from yyscript.y).
549 // Otherwise return 0.
551 inline int
552 Lex::two_char_operator(char c1, char c2)
554 switch (c1)
556 case '=':
557 if (c2 == '=')
558 return EQ;
559 break;
560 case '!':
561 if (c2 == '=')
562 return NE;
563 break;
564 case '+':
565 if (c2 == '=')
566 return PLUSEQ;
567 break;
568 case '-':
569 if (c2 == '=')
570 return MINUSEQ;
571 break;
572 case '*':
573 if (c2 == '=')
574 return MULTEQ;
575 break;
576 case '/':
577 if (c2 == '=')
578 return DIVEQ;
579 break;
580 case '|':
581 if (c2 == '=')
582 return OREQ;
583 if (c2 == '|')
584 return OROR;
585 break;
586 case '&':
587 if (c2 == '=')
588 return ANDEQ;
589 if (c2 == '&')
590 return ANDAND;
591 break;
592 case '>':
593 if (c2 == '=')
594 return GE;
595 if (c2 == '>')
596 return RSHIFT;
597 break;
598 case '<':
599 if (c2 == '=')
600 return LE;
601 if (c2 == '<')
602 return LSHIFT;
603 break;
604 default:
605 break;
607 return 0;
610 // If C1 is a valid operator, return the opcode. Otherwise return 0.
612 inline int
613 Lex::one_char_operator(char c1)
615 switch (c1)
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 case '(':
633 case ')':
634 case '{':
635 case '}':
636 case '[':
637 case ']':
638 case ':':
639 case ';':
640 return c1;
641 default:
642 return 0;
646 // Skip a C style comment. *PP points to just after the "/*". Return
647 // false if the comment did not end.
649 bool
650 Lex::skip_c_comment(const char** pp)
652 const char* p = *pp;
653 while (p[0] != '*' || p[1] != '/')
655 if (*p == '\0')
657 *pp = p;
658 return false;
661 if (*p == '\n')
663 ++this->lineno_;
664 this->linestart_ = p + 1;
666 ++p;
669 *pp = p + 2;
670 return true;
673 // Skip a line # comment. Return false if there was no newline.
675 bool
676 Lex::skip_line_comment(const char** pp)
678 const char* p = *pp;
679 size_t skip = strcspn(p, "\n");
680 if (p[skip] == '\0')
682 *pp = p + skip;
683 return false;
686 p += skip + 1;
687 ++this->lineno_;
688 this->linestart_ = p;
689 *pp = p;
691 return true;
694 // Build a token CLASSIFICATION from all characters that match
695 // CAN_CONTINUE_FN. Update *PP.
697 inline Token
698 Lex::gather_token(Token::Classification classification,
699 const char* (Lex::*can_continue_fn)(const char*),
700 const char* start,
701 const char* match,
702 const char **pp)
704 const char* new_match = NULL;
705 while ((new_match = (this->*can_continue_fn)(match)))
706 match = new_match;
707 *pp = match;
708 return this->make_token(classification, start, match - start, start);
711 // Build a token from a quoted string.
713 Token
714 Lex::gather_quoted_string(const char** pp)
716 const char* start = *pp;
717 const char* p = start;
718 ++p;
719 size_t skip = strcspn(p, "\"\n");
720 if (p[skip] != '"')
721 return this->make_invalid_token(start);
722 *pp = p + skip + 1;
723 return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
726 // Return the next token at *PP. Update *PP. General guideline: we
727 // require linker scripts to be simple ASCII. No unicode linker
728 // scripts. In particular we can assume that any '\0' is the end of
729 // the input.
731 Token
732 Lex::get_token(const char** pp)
734 const char* p = *pp;
736 while (true)
738 if (*p == '\0')
740 *pp = p;
741 return this->make_eof_token(p);
744 // Skip whitespace quickly.
745 while (*p == ' ' || *p == '\t')
746 ++p;
748 if (*p == '\n')
750 ++p;
751 ++this->lineno_;
752 this->linestart_ = p;
753 continue;
756 // Skip C style comments.
757 if (p[0] == '/' && p[1] == '*')
759 int lineno = this->lineno_;
760 int charpos = p - this->linestart_ + 1;
762 *pp = p + 2;
763 if (!this->skip_c_comment(pp))
764 return Token(Token::TOKEN_INVALID, lineno, charpos);
765 p = *pp;
767 continue;
770 // Skip line comments.
771 if (*p == '#')
773 *pp = p + 1;
774 if (!this->skip_line_comment(pp))
775 return this->make_eof_token(p);
776 p = *pp;
777 continue;
780 // Check for a name.
781 if (this->can_start_name(p[0], p[1]))
782 return this->gather_token(Token::TOKEN_STRING,
783 &Lex::can_continue_name,
784 p, p + 1, pp);
786 // We accept any arbitrary name in double quotes, as long as it
787 // does not cross a line boundary.
788 if (*p == '"')
790 *pp = p;
791 return this->gather_quoted_string(pp);
794 // Check for a number.
796 if (this->can_start_hex(p[0], p[1], p[2]))
797 return this->gather_token(Token::TOKEN_INTEGER,
798 &Lex::can_continue_hex,
799 p, p + 3, pp);
801 if (Lex::can_start_number(p[0]))
802 return this->gather_token(Token::TOKEN_INTEGER,
803 &Lex::can_continue_number,
804 p, p + 1, pp);
806 // Check for operators.
808 int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
809 if (opcode != 0)
811 *pp = p + 3;
812 return this->make_token(opcode, p);
815 opcode = Lex::two_char_operator(p[0], p[1]);
816 if (opcode != 0)
818 *pp = p + 2;
819 return this->make_token(opcode, p);
822 opcode = Lex::one_char_operator(p[0]);
823 if (opcode != 0)
825 *pp = p + 1;
826 return this->make_token(opcode, p);
829 return this->make_token(Token::TOKEN_INVALID, p);
833 // Return the next token.
835 const Token*
836 Lex::next_token()
838 // The first token is special.
839 if (this->first_token_ != 0)
841 this->token_ = Token(this->first_token_, 0, 0);
842 this->first_token_ = 0;
843 return &this->token_;
846 this->token_ = this->get_token(&this->current_);
848 // Don't let an early null byte fool us into thinking that we've
849 // reached the end of the file.
850 if (this->token_.is_eof()
851 && (static_cast<size_t>(this->current_ - this->input_string_)
852 < this->input_length_))
853 this->token_ = this->make_invalid_token(this->current_);
855 return &this->token_;
858 // class Symbol_assignment.
860 // Add the symbol to the symbol table. This makes sure the symbol is
861 // there and defined. The actual value is stored later. We can't
862 // determine the actual value at this point, because we can't
863 // necessarily evaluate the expression until all ordinary symbols have
864 // been finalized.
866 // The GNU linker lets symbol assignments in the linker script
867 // silently override defined symbols in object files. We are
868 // compatible. FIXME: Should we issue a warning?
870 void
871 Symbol_assignment::add_to_table(Symbol_table* symtab)
873 elfcpp::STV vis = this->hidden_ ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
874 this->sym_ = symtab->define_as_constant(this->name_.c_str(),
875 NULL, // version
876 0, // value
877 0, // size
878 elfcpp::STT_NOTYPE,
879 elfcpp::STB_GLOBAL,
880 vis,
881 0, // nonvis
882 this->provide_,
883 true); // force_override
886 // Finalize a symbol value.
888 void
889 Symbol_assignment::finalize(Symbol_table* symtab, const Layout* layout)
891 this->finalize_maybe_dot(symtab, layout, false, 0, NULL);
894 // Finalize a symbol value which can refer to the dot symbol.
896 void
897 Symbol_assignment::finalize_with_dot(Symbol_table* symtab,
898 const Layout* layout,
899 uint64_t dot_value,
900 Output_section* dot_section)
902 this->finalize_maybe_dot(symtab, layout, true, dot_value, dot_section);
905 // Finalize a symbol value, internal version.
907 void
908 Symbol_assignment::finalize_maybe_dot(Symbol_table* symtab,
909 const Layout* layout,
910 bool is_dot_available,
911 uint64_t dot_value,
912 Output_section* dot_section)
914 // If we were only supposed to provide this symbol, the sym_ field
915 // will be NULL if the symbol was not referenced.
916 if (this->sym_ == NULL)
918 gold_assert(this->provide_);
919 return;
922 if (parameters->target().get_size() == 32)
924 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
925 this->sized_finalize<32>(symtab, layout, is_dot_available, dot_value,
926 dot_section);
927 #else
928 gold_unreachable();
929 #endif
931 else if (parameters->target().get_size() == 64)
933 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
934 this->sized_finalize<64>(symtab, layout, is_dot_available, dot_value,
935 dot_section);
936 #else
937 gold_unreachable();
938 #endif
940 else
941 gold_unreachable();
944 template<int size>
945 void
946 Symbol_assignment::sized_finalize(Symbol_table* symtab, const Layout* layout,
947 bool is_dot_available, uint64_t dot_value,
948 Output_section* dot_section)
950 Output_section* section;
951 uint64_t final_val = this->val_->eval_maybe_dot(symtab, layout, true,
952 is_dot_available,
953 dot_value, dot_section,
954 &section);
955 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(this->sym_);
956 ssym->set_value(final_val);
957 if (section != NULL)
958 ssym->set_output_section(section);
961 // Set the symbol value if the expression yields an absolute value.
963 void
964 Symbol_assignment::set_if_absolute(Symbol_table* symtab, const Layout* layout,
965 bool is_dot_available, uint64_t dot_value)
967 if (this->sym_ == NULL)
968 return;
970 Output_section* val_section;
971 uint64_t val = this->val_->eval_maybe_dot(symtab, layout, false,
972 is_dot_available, dot_value,
973 NULL, &val_section);
974 if (val_section != NULL)
975 return;
977 if (parameters->target().get_size() == 32)
979 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
980 Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(this->sym_);
981 ssym->set_value(val);
982 #else
983 gold_unreachable();
984 #endif
986 else if (parameters->target().get_size() == 64)
988 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
989 Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(this->sym_);
990 ssym->set_value(val);
991 #else
992 gold_unreachable();
993 #endif
995 else
996 gold_unreachable();
999 // Print for debugging.
1001 void
1002 Symbol_assignment::print(FILE* f) const
1004 if (this->provide_ && this->hidden_)
1005 fprintf(f, "PROVIDE_HIDDEN(");
1006 else if (this->provide_)
1007 fprintf(f, "PROVIDE(");
1008 else if (this->hidden_)
1009 gold_unreachable();
1011 fprintf(f, "%s = ", this->name_.c_str());
1012 this->val_->print(f);
1014 if (this->provide_ || this->hidden_)
1015 fprintf(f, ")");
1017 fprintf(f, "\n");
1020 // Class Script_assertion.
1022 // Check the assertion.
1024 void
1025 Script_assertion::check(const Symbol_table* symtab, const Layout* layout)
1027 if (!this->check_->eval(symtab, layout, true))
1028 gold_error("%s", this->message_.c_str());
1031 // Print for debugging.
1033 void
1034 Script_assertion::print(FILE* f) const
1036 fprintf(f, "ASSERT(");
1037 this->check_->print(f);
1038 fprintf(f, ", \"%s\")\n", this->message_.c_str());
1041 // Class Script_options.
1043 Script_options::Script_options()
1044 : entry_(), symbol_assignments_(), version_script_info_(),
1045 script_sections_()
1049 // Add a symbol to be defined.
1051 void
1052 Script_options::add_symbol_assignment(const char* name, size_t length,
1053 Expression* value, bool provide,
1054 bool hidden)
1056 if (length != 1 || name[0] != '.')
1058 if (this->script_sections_.in_sections_clause())
1059 this->script_sections_.add_symbol_assignment(name, length, value,
1060 provide, hidden);
1061 else
1063 Symbol_assignment* p = new Symbol_assignment(name, length, value,
1064 provide, hidden);
1065 this->symbol_assignments_.push_back(p);
1068 else
1070 if (provide || hidden)
1071 gold_error(_("invalid use of PROVIDE for dot symbol"));
1072 if (!this->script_sections_.in_sections_clause())
1073 gold_error(_("invalid assignment to dot outside of SECTIONS"));
1074 else
1075 this->script_sections_.add_dot_assignment(value);
1079 // Add an assertion.
1081 void
1082 Script_options::add_assertion(Expression* check, const char* message,
1083 size_t messagelen)
1085 if (this->script_sections_.in_sections_clause())
1086 this->script_sections_.add_assertion(check, message, messagelen);
1087 else
1089 Script_assertion* p = new Script_assertion(check, message, messagelen);
1090 this->assertions_.push_back(p);
1094 // Create sections required by any linker scripts.
1096 void
1097 Script_options::create_script_sections(Layout* layout)
1099 if (this->saw_sections_clause())
1100 this->script_sections_.create_sections(layout);
1103 // Add any symbols we are defining to the symbol table.
1105 void
1106 Script_options::add_symbols_to_table(Symbol_table* symtab)
1108 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1109 p != this->symbol_assignments_.end();
1110 ++p)
1111 (*p)->add_to_table(symtab);
1112 this->script_sections_.add_symbols_to_table(symtab);
1115 // Finalize symbol values. Also check assertions.
1117 void
1118 Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
1120 // We finalize the symbols defined in SECTIONS first, because they
1121 // are the ones which may have changed. This way if symbol outside
1122 // SECTIONS are defined in terms of symbols inside SECTIONS, they
1123 // will get the right value.
1124 this->script_sections_.finalize_symbols(symtab, layout);
1126 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1127 p != this->symbol_assignments_.end();
1128 ++p)
1129 (*p)->finalize(symtab, layout);
1131 for (Assertions::iterator p = this->assertions_.begin();
1132 p != this->assertions_.end();
1133 ++p)
1134 (*p)->check(symtab, layout);
1137 // Set section addresses. We set all the symbols which have absolute
1138 // values. Then we let the SECTIONS clause do its thing. This
1139 // returns the segment which holds the file header and segment
1140 // headers, if any.
1142 Output_segment*
1143 Script_options::set_section_addresses(Symbol_table* symtab, Layout* layout)
1145 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1146 p != this->symbol_assignments_.end();
1147 ++p)
1148 (*p)->set_if_absolute(symtab, layout, false, 0);
1150 return this->script_sections_.set_section_addresses(symtab, layout);
1153 // This class holds data passed through the parser to the lexer and to
1154 // the parser support functions. This avoids global variables. We
1155 // can't use global variables because we need not be called by a
1156 // singleton thread.
1158 class Parser_closure
1160 public:
1161 Parser_closure(const char* filename,
1162 const Position_dependent_options& posdep_options,
1163 bool in_group, bool is_in_sysroot,
1164 Command_line* command_line,
1165 Script_options* script_options,
1166 Lex* lex,
1167 bool skip_on_incompatible_target)
1168 : filename_(filename), posdep_options_(posdep_options),
1169 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
1170 skip_on_incompatible_target_(skip_on_incompatible_target),
1171 found_incompatible_target_(false),
1172 command_line_(command_line), script_options_(script_options),
1173 version_script_info_(script_options->version_script_info()),
1174 lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL)
1176 // We start out processing C symbols in the default lex mode.
1177 language_stack_.push_back("");
1178 lex_mode_stack_.push_back(lex->mode());
1181 // Return the file name.
1182 const char*
1183 filename() const
1184 { return this->filename_; }
1186 // Return the position dependent options. The caller may modify
1187 // this.
1188 Position_dependent_options&
1189 position_dependent_options()
1190 { return this->posdep_options_; }
1192 // Return whether this script is being run in a group.
1193 bool
1194 in_group() const
1195 { return this->in_group_; }
1197 // Return whether this script was found using a directory in the
1198 // sysroot.
1199 bool
1200 is_in_sysroot() const
1201 { return this->is_in_sysroot_; }
1203 // Whether to skip to the next file with the same name if we find an
1204 // incompatible target in an OUTPUT_FORMAT statement.
1205 bool
1206 skip_on_incompatible_target() const
1207 { return this->skip_on_incompatible_target_; }
1209 // Stop skipping to the next flie on an incompatible target. This
1210 // is called when we make some unrevocable change to the data
1211 // structures.
1212 void
1213 clear_skip_on_incompatible_target()
1214 { this->skip_on_incompatible_target_ = false; }
1216 // Whether we found an incompatible target in an OUTPUT_FORMAT
1217 // statement.
1218 bool
1219 found_incompatible_target() const
1220 { return this->found_incompatible_target_; }
1222 // Note that we found an incompatible target.
1223 void
1224 set_found_incompatible_target()
1225 { this->found_incompatible_target_ = true; }
1227 // Returns the Command_line structure passed in at constructor time.
1228 // This value may be NULL. The caller may modify this, which modifies
1229 // the passed-in Command_line object (not a copy).
1230 Command_line*
1231 command_line()
1232 { return this->command_line_; }
1234 // Return the options which may be set by a script.
1235 Script_options*
1236 script_options()
1237 { return this->script_options_; }
1239 // Return the object in which version script information should be stored.
1240 Version_script_info*
1241 version_script()
1242 { return this->version_script_info_; }
1244 // Return the next token, and advance.
1245 const Token*
1246 next_token()
1248 const Token* token = this->lex_->next_token();
1249 this->lineno_ = token->lineno();
1250 this->charpos_ = token->charpos();
1251 return token;
1254 // Set a new lexer mode, pushing the current one.
1255 void
1256 push_lex_mode(Lex::Mode mode)
1258 this->lex_mode_stack_.push_back(this->lex_->mode());
1259 this->lex_->set_mode(mode);
1262 // Pop the lexer mode.
1263 void
1264 pop_lex_mode()
1266 gold_assert(!this->lex_mode_stack_.empty());
1267 this->lex_->set_mode(this->lex_mode_stack_.back());
1268 this->lex_mode_stack_.pop_back();
1271 // Return the current lexer mode.
1272 Lex::Mode
1273 lex_mode() const
1274 { return this->lex_mode_stack_.back(); }
1276 // Return the line number of the last token.
1278 lineno() const
1279 { return this->lineno_; }
1281 // Return the character position in the line of the last token.
1283 charpos() const
1284 { return this->charpos_; }
1286 // Return the list of input files, creating it if necessary. This
1287 // is a space leak--we never free the INPUTS_ pointer.
1288 Input_arguments*
1289 inputs()
1291 if (this->inputs_ == NULL)
1292 this->inputs_ = new Input_arguments();
1293 return this->inputs_;
1296 // Return whether we saw any input files.
1297 bool
1298 saw_inputs() const
1299 { return this->inputs_ != NULL && !this->inputs_->empty(); }
1301 // Return the current language being processed in a version script
1302 // (eg, "C++"). The empty string represents unmangled C names.
1303 const std::string&
1304 get_current_language() const
1305 { return this->language_stack_.back(); }
1307 // Push a language onto the stack when entering an extern block.
1308 void push_language(const std::string& lang)
1309 { this->language_stack_.push_back(lang); }
1311 // Pop a language off of the stack when exiting an extern block.
1312 void pop_language()
1314 gold_assert(!this->language_stack_.empty());
1315 this->language_stack_.pop_back();
1318 private:
1319 // The name of the file we are reading.
1320 const char* filename_;
1321 // The position dependent options.
1322 Position_dependent_options posdep_options_;
1323 // Whether we are currently in a --start-group/--end-group.
1324 bool in_group_;
1325 // Whether the script was found in a sysrooted directory.
1326 bool is_in_sysroot_;
1327 // If this is true, then if we find an OUTPUT_FORMAT with an
1328 // incompatible target, then we tell the parser to abort so that we
1329 // can search for the next file with the same name.
1330 bool skip_on_incompatible_target_;
1331 // True if we found an OUTPUT_FORMAT with an incompatible target.
1332 bool found_incompatible_target_;
1333 // May be NULL if the user chooses not to pass one in.
1334 Command_line* command_line_;
1335 // Options which may be set from any linker script.
1336 Script_options* script_options_;
1337 // Information parsed from a version script.
1338 Version_script_info* version_script_info_;
1339 // The lexer.
1340 Lex* lex_;
1341 // The line number of the last token returned by next_token.
1342 int lineno_;
1343 // The column number of the last token returned by next_token.
1344 int charpos_;
1345 // A stack of lexer modes.
1346 std::vector<Lex::Mode> lex_mode_stack_;
1347 // A stack of which extern/language block we're inside. Can be C++,
1348 // java, or empty for C.
1349 std::vector<std::string> language_stack_;
1350 // New input files found to add to the link.
1351 Input_arguments* inputs_;
1354 // FILE was found as an argument on the command line. Try to read it
1355 // as a script. Return true if the file was handled.
1357 bool
1358 read_input_script(Workqueue* workqueue, Symbol_table* symtab, Layout* layout,
1359 Dirsearch* dirsearch, int dirindex,
1360 Input_objects* input_objects, Mapfile* mapfile,
1361 Input_group* input_group,
1362 const Input_argument* input_argument,
1363 Input_file* input_file, Task_token* next_blocker,
1364 bool* used_next_blocker)
1366 *used_next_blocker = false;
1368 std::string input_string;
1369 Lex::read_file(input_file, &input_string);
1371 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1373 Parser_closure closure(input_file->filename().c_str(),
1374 input_argument->file().options(),
1375 input_group != NULL,
1376 input_file->is_in_sysroot(),
1377 NULL,
1378 layout->script_options(),
1379 &lex,
1380 input_file->will_search_for());
1382 if (yyparse(&closure) != 0)
1384 if (closure.found_incompatible_target())
1386 Read_symbols::incompatible_warning(input_argument, input_file);
1387 Read_symbols::requeue(workqueue, input_objects, symtab, layout,
1388 dirsearch, dirindex, mapfile, input_argument,
1389 input_group, next_blocker);
1390 return true;
1392 return false;
1395 if (!closure.saw_inputs())
1396 return true;
1398 Task_token* this_blocker = NULL;
1399 for (Input_arguments::const_iterator p = closure.inputs()->begin();
1400 p != closure.inputs()->end();
1401 ++p)
1403 Task_token* nb;
1404 if (p + 1 == closure.inputs()->end())
1405 nb = next_blocker;
1406 else
1408 nb = new Task_token(true);
1409 nb->add_blocker();
1411 workqueue->queue_soon(new Read_symbols(input_objects, symtab,
1412 layout, dirsearch, 0, mapfile, &*p,
1413 input_group, this_blocker, nb));
1414 this_blocker = nb;
1417 *used_next_blocker = true;
1419 return true;
1422 // Helper function for read_version_script() and
1423 // read_commandline_script(). Processes the given file in the mode
1424 // indicated by first_token and lex_mode.
1426 static bool
1427 read_script_file(const char* filename, Command_line* cmdline,
1428 Script_options* script_options,
1429 int first_token, Lex::Mode lex_mode)
1431 // TODO: if filename is a relative filename, search for it manually
1432 // using "." + cmdline->options()->search_path() -- not dirsearch.
1433 Dirsearch dirsearch;
1435 // The file locking code wants to record a Task, but we haven't
1436 // started the workqueue yet. This is only for debugging purposes,
1437 // so we invent a fake value.
1438 const Task* task = reinterpret_cast<const Task*>(-1);
1440 // We don't want this file to be opened in binary mode.
1441 Position_dependent_options posdep = cmdline->position_dependent_options();
1442 if (posdep.format_enum() == General_options::OBJECT_FORMAT_BINARY)
1443 posdep.set_format_enum(General_options::OBJECT_FORMAT_ELF);
1444 Input_file_argument input_argument(filename, false, "", false, posdep);
1445 Input_file input_file(&input_argument);
1446 int dummy = 0;
1447 if (!input_file.open(dirsearch, task, &dummy))
1448 return false;
1450 std::string input_string;
1451 Lex::read_file(&input_file, &input_string);
1453 Lex lex(input_string.c_str(), input_string.length(), first_token);
1454 lex.set_mode(lex_mode);
1456 Parser_closure closure(filename,
1457 cmdline->position_dependent_options(),
1458 false,
1459 input_file.is_in_sysroot(),
1460 cmdline,
1461 script_options,
1462 &lex,
1463 false);
1464 if (yyparse(&closure) != 0)
1466 input_file.file().unlock(task);
1467 return false;
1470 input_file.file().unlock(task);
1472 gold_assert(!closure.saw_inputs());
1474 return true;
1477 // FILENAME was found as an argument to --script (-T).
1478 // Read it as a script, and execute its contents immediately.
1480 bool
1481 read_commandline_script(const char* filename, Command_line* cmdline)
1483 return read_script_file(filename, cmdline, &cmdline->script_options(),
1484 PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT);
1487 // FILENAME was found as an argument to --version-script. Read it as
1488 // a version script, and store its contents in
1489 // cmdline->script_options()->version_script_info().
1491 bool
1492 read_version_script(const char* filename, Command_line* cmdline)
1494 return read_script_file(filename, cmdline, &cmdline->script_options(),
1495 PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT);
1498 // FILENAME was found as an argument to --dynamic-list. Read it as a
1499 // list of symbols, and store its contents in DYNAMIC_LIST.
1501 bool
1502 read_dynamic_list(const char* filename, Command_line* cmdline,
1503 Script_options* dynamic_list)
1505 return read_script_file(filename, cmdline, dynamic_list,
1506 PARSING_DYNAMIC_LIST, Lex::DYNAMIC_LIST);
1509 // Implement the --defsym option on the command line. Return true if
1510 // all is well.
1512 bool
1513 Script_options::define_symbol(const char* definition)
1515 Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1516 lex.set_mode(Lex::EXPRESSION);
1518 // Dummy value.
1519 Position_dependent_options posdep_options;
1521 Parser_closure closure("command line", posdep_options, false, false, NULL,
1522 this, &lex, false);
1524 if (yyparse(&closure) != 0)
1525 return false;
1527 gold_assert(!closure.saw_inputs());
1529 return true;
1532 // Print the script to F for debugging.
1534 void
1535 Script_options::print(FILE* f) const
1537 fprintf(f, "%s: Dumping linker script\n", program_name);
1539 if (!this->entry_.empty())
1540 fprintf(f, "ENTRY(%s)\n", this->entry_.c_str());
1542 for (Symbol_assignments::const_iterator p =
1543 this->symbol_assignments_.begin();
1544 p != this->symbol_assignments_.end();
1545 ++p)
1546 (*p)->print(f);
1548 for (Assertions::const_iterator p = this->assertions_.begin();
1549 p != this->assertions_.end();
1550 ++p)
1551 (*p)->print(f);
1553 this->script_sections_.print(f);
1555 this->version_script_info_.print(f);
1558 // Manage mapping from keywords to the codes expected by the bison
1559 // parser. We construct one global object for each lex mode with
1560 // keywords.
1562 class Keyword_to_parsecode
1564 public:
1565 // The structure which maps keywords to parsecodes.
1566 struct Keyword_parsecode
1568 // Keyword.
1569 const char* keyword;
1570 // Corresponding parsecode.
1571 int parsecode;
1574 Keyword_to_parsecode(const Keyword_parsecode* keywords,
1575 int keyword_count)
1576 : keyword_parsecodes_(keywords), keyword_count_(keyword_count)
1579 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1580 // keyword.
1582 keyword_to_parsecode(const char* keyword, size_t len) const;
1584 private:
1585 const Keyword_parsecode* keyword_parsecodes_;
1586 const int keyword_count_;
1589 // Mapping from keyword string to keyword parsecode. This array must
1590 // be kept in sorted order. Parsecodes are looked up using bsearch.
1591 // This array must correspond to the list of parsecodes in yyscript.y.
1593 static const Keyword_to_parsecode::Keyword_parsecode
1594 script_keyword_parsecodes[] =
1596 { "ABSOLUTE", ABSOLUTE },
1597 { "ADDR", ADDR },
1598 { "ALIGN", ALIGN_K },
1599 { "ALIGNOF", ALIGNOF },
1600 { "ASSERT", ASSERT_K },
1601 { "AS_NEEDED", AS_NEEDED },
1602 { "AT", AT },
1603 { "BIND", BIND },
1604 { "BLOCK", BLOCK },
1605 { "BYTE", BYTE },
1606 { "CONSTANT", CONSTANT },
1607 { "CONSTRUCTORS", CONSTRUCTORS },
1608 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1609 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1610 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1611 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1612 { "DEFINED", DEFINED },
1613 { "ENTRY", ENTRY },
1614 { "EXCLUDE_FILE", EXCLUDE_FILE },
1615 { "EXTERN", EXTERN },
1616 { "FILL", FILL },
1617 { "FLOAT", FLOAT },
1618 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1619 { "GROUP", GROUP },
1620 { "HLL", HLL },
1621 { "INCLUDE", INCLUDE },
1622 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1623 { "INPUT", INPUT },
1624 { "KEEP", KEEP },
1625 { "LENGTH", LENGTH },
1626 { "LOADADDR", LOADADDR },
1627 { "LONG", LONG },
1628 { "MAP", MAP },
1629 { "MAX", MAX_K },
1630 { "MEMORY", MEMORY },
1631 { "MIN", MIN_K },
1632 { "NEXT", NEXT },
1633 { "NOCROSSREFS", NOCROSSREFS },
1634 { "NOFLOAT", NOFLOAT },
1635 { "ONLY_IF_RO", ONLY_IF_RO },
1636 { "ONLY_IF_RW", ONLY_IF_RW },
1637 { "OPTION", OPTION },
1638 { "ORIGIN", ORIGIN },
1639 { "OUTPUT", OUTPUT },
1640 { "OUTPUT_ARCH", OUTPUT_ARCH },
1641 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1642 { "OVERLAY", OVERLAY },
1643 { "PHDRS", PHDRS },
1644 { "PROVIDE", PROVIDE },
1645 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1646 { "QUAD", QUAD },
1647 { "SEARCH_DIR", SEARCH_DIR },
1648 { "SECTIONS", SECTIONS },
1649 { "SEGMENT_START", SEGMENT_START },
1650 { "SHORT", SHORT },
1651 { "SIZEOF", SIZEOF },
1652 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1653 { "SORT", SORT_BY_NAME },
1654 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1655 { "SORT_BY_NAME", SORT_BY_NAME },
1656 { "SPECIAL", SPECIAL },
1657 { "SQUAD", SQUAD },
1658 { "STARTUP", STARTUP },
1659 { "SUBALIGN", SUBALIGN },
1660 { "SYSLIB", SYSLIB },
1661 { "TARGET", TARGET_K },
1662 { "TRUNCATE", TRUNCATE },
1663 { "VERSION", VERSIONK },
1664 { "global", GLOBAL },
1665 { "l", LENGTH },
1666 { "len", LENGTH },
1667 { "local", LOCAL },
1668 { "o", ORIGIN },
1669 { "org", ORIGIN },
1670 { "sizeof_headers", SIZEOF_HEADERS },
1673 static const Keyword_to_parsecode
1674 script_keywords(&script_keyword_parsecodes[0],
1675 (sizeof(script_keyword_parsecodes)
1676 / sizeof(script_keyword_parsecodes[0])));
1678 static const Keyword_to_parsecode::Keyword_parsecode
1679 version_script_keyword_parsecodes[] =
1681 { "extern", EXTERN },
1682 { "global", GLOBAL },
1683 { "local", LOCAL },
1686 static const Keyword_to_parsecode
1687 version_script_keywords(&version_script_keyword_parsecodes[0],
1688 (sizeof(version_script_keyword_parsecodes)
1689 / sizeof(version_script_keyword_parsecodes[0])));
1691 static const Keyword_to_parsecode::Keyword_parsecode
1692 dynamic_list_keyword_parsecodes[] =
1694 { "extern", EXTERN },
1697 static const Keyword_to_parsecode
1698 dynamic_list_keywords(&dynamic_list_keyword_parsecodes[0],
1699 (sizeof(dynamic_list_keyword_parsecodes)
1700 / sizeof(dynamic_list_keyword_parsecodes[0])));
1704 // Comparison function passed to bsearch.
1706 extern "C"
1709 struct Ktt_key
1711 const char* str;
1712 size_t len;
1715 static int
1716 ktt_compare(const void* keyv, const void* kttv)
1718 const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
1719 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1720 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1721 int i = strncmp(key->str, ktt->keyword, key->len);
1722 if (i != 0)
1723 return i;
1724 if (ktt->keyword[key->len] != '\0')
1725 return -1;
1726 return 0;
1729 } // End extern "C".
1732 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword,
1733 size_t len) const
1735 Ktt_key key;
1736 key.str = keyword;
1737 key.len = len;
1738 void* kttv = bsearch(&key,
1739 this->keyword_parsecodes_,
1740 this->keyword_count_,
1741 sizeof(this->keyword_parsecodes_[0]),
1742 ktt_compare);
1743 if (kttv == NULL)
1744 return 0;
1745 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1746 return ktt->parsecode;
1749 // Helper class that calls cplus_demangle when needed and takes care of freeing
1750 // the result.
1752 class Lazy_demangler
1754 public:
1755 Lazy_demangler(const char* symbol, int options)
1756 : symbol_(symbol), options_(options), demangled_(NULL), did_demangle_(false)
1759 ~Lazy_demangler()
1760 { free(this->demangled_); }
1762 // Return the demangled name. The actual demangling happens on the first call,
1763 // and the result is later cached.
1765 inline char*
1766 get();
1768 private:
1769 // The symbol to demangle.
1770 const char *symbol_;
1771 // Option flags to pass to cplus_demagle.
1772 const int options_;
1773 // The cached demangled value, or NULL if demangling didn't happen yet or
1774 // failed.
1775 char *demangled_;
1776 // Whether we already called cplus_demangle
1777 bool did_demangle_;
1780 // Return the demangled name. The actual demangling happens on the first call,
1781 // and the result is later cached. Returns NULL if the symbol cannot be
1782 // demangled.
1784 inline char*
1785 Lazy_demangler::get()
1787 if (!this->did_demangle_)
1789 this->demangled_ = cplus_demangle(this->symbol_, this->options_);
1790 this->did_demangle_ = true;
1792 return this->demangled_;
1795 // The following structs are used within the VersionInfo class as well
1796 // as in the bison helper functions. They store the information
1797 // parsed from the version script.
1799 // A single version expression.
1800 // For example, pattern="std::map*" and language="C++".
1801 // pattern and language should be from the stringpool
1802 struct Version_expression {
1803 Version_expression(const std::string& pattern,
1804 const std::string& language,
1805 bool exact_match)
1806 : pattern(pattern), language(language), exact_match(exact_match) {}
1808 std::string pattern;
1809 std::string language;
1810 // If false, we use glob() to match pattern. If true, we use strcmp().
1811 bool exact_match;
1815 // A list of expressions.
1816 struct Version_expression_list {
1817 std::vector<struct Version_expression> expressions;
1821 // A list of which versions upon which another version depends.
1822 // Strings should be from the Stringpool.
1823 struct Version_dependency_list {
1824 std::vector<std::string> dependencies;
1828 // The total definition of a version. It includes the tag for the
1829 // version, its global and local expressions, and any dependencies.
1830 struct Version_tree {
1831 Version_tree()
1832 : tag(), global(NULL), local(NULL), dependencies(NULL) {}
1834 std::string tag;
1835 const struct Version_expression_list* global;
1836 const struct Version_expression_list* local;
1837 const struct Version_dependency_list* dependencies;
1840 Version_script_info::~Version_script_info()
1842 this->clear();
1845 void
1846 Version_script_info::clear()
1848 for (size_t k = 0; k < dependency_lists_.size(); ++k)
1849 delete dependency_lists_[k];
1850 this->dependency_lists_.clear();
1851 for (size_t k = 0; k < version_trees_.size(); ++k)
1852 delete version_trees_[k];
1853 this->version_trees_.clear();
1854 for (size_t k = 0; k < expression_lists_.size(); ++k)
1855 delete expression_lists_[k];
1856 this->expression_lists_.clear();
1859 std::vector<std::string>
1860 Version_script_info::get_versions() const
1862 std::vector<std::string> ret;
1863 for (size_t j = 0; j < version_trees_.size(); ++j)
1864 if (!this->version_trees_[j]->tag.empty())
1865 ret.push_back(this->version_trees_[j]->tag);
1866 return ret;
1869 std::vector<std::string>
1870 Version_script_info::get_dependencies(const char* version) const
1872 std::vector<std::string> ret;
1873 for (size_t j = 0; j < version_trees_.size(); ++j)
1874 if (version_trees_[j]->tag == version)
1876 const struct Version_dependency_list* deps =
1877 version_trees_[j]->dependencies;
1878 if (deps != NULL)
1879 for (size_t k = 0; k < deps->dependencies.size(); ++k)
1880 ret.push_back(deps->dependencies[k]);
1881 return ret;
1883 return ret;
1886 // Look up SYMBOL_NAME in the list of versions. If CHECK_GLOBAL is
1887 // true look at the globally visible symbols, otherwise look at the
1888 // symbols listed as "local:". Return true if the symbol is found,
1889 // false otherwise. If the symbol is found, then if PVERSION is not
1890 // NULL, set *PVERSION to the version.
1892 bool
1893 Version_script_info::get_symbol_version_helper(const char* symbol_name,
1894 bool check_global,
1895 std::string* pversion) const
1897 Lazy_demangler cpp_demangled_name(symbol_name, DMGL_ANSI | DMGL_PARAMS);
1898 Lazy_demangler java_demangled_name(symbol_name,
1899 DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
1900 for (size_t j = 0; j < version_trees_.size(); ++j)
1902 // Is it a global symbol for this version?
1903 const Version_expression_list* explist =
1904 check_global ? version_trees_[j]->global : version_trees_[j]->local;
1905 if (explist != NULL)
1906 for (size_t k = 0; k < explist->expressions.size(); ++k)
1908 const char* name_to_match = symbol_name;
1909 const struct Version_expression& exp = explist->expressions[k];
1910 if (exp.language == "C++")
1912 name_to_match = cpp_demangled_name.get();
1913 // This isn't a C++ symbol.
1914 if (name_to_match == NULL)
1915 continue;
1917 else if (exp.language == "Java")
1919 name_to_match = java_demangled_name.get();
1920 // This isn't a Java symbol.
1921 if (name_to_match == NULL)
1922 continue;
1924 bool matched;
1925 if (exp.exact_match)
1926 matched = strcmp(exp.pattern.c_str(), name_to_match) == 0;
1927 else
1928 matched = fnmatch(exp.pattern.c_str(), name_to_match,
1929 FNM_NOESCAPE) == 0;
1930 if (matched)
1932 if (pversion != NULL)
1933 *pversion = this->version_trees_[j]->tag;
1934 return true;
1938 return false;
1941 struct Version_dependency_list*
1942 Version_script_info::allocate_dependency_list()
1944 dependency_lists_.push_back(new Version_dependency_list);
1945 return dependency_lists_.back();
1948 struct Version_expression_list*
1949 Version_script_info::allocate_expression_list()
1951 expression_lists_.push_back(new Version_expression_list);
1952 return expression_lists_.back();
1955 struct Version_tree*
1956 Version_script_info::allocate_version_tree()
1958 version_trees_.push_back(new Version_tree);
1959 return version_trees_.back();
1962 // Print for debugging.
1964 void
1965 Version_script_info::print(FILE* f) const
1967 if (this->empty())
1968 return;
1970 fprintf(f, "VERSION {");
1972 for (size_t i = 0; i < this->version_trees_.size(); ++i)
1974 const Version_tree* vt = this->version_trees_[i];
1976 if (vt->tag.empty())
1977 fprintf(f, " {\n");
1978 else
1979 fprintf(f, " %s {\n", vt->tag.c_str());
1981 if (vt->global != NULL)
1983 fprintf(f, " global :\n");
1984 this->print_expression_list(f, vt->global);
1987 if (vt->local != NULL)
1989 fprintf(f, " local :\n");
1990 this->print_expression_list(f, vt->local);
1993 fprintf(f, " }");
1994 if (vt->dependencies != NULL)
1996 const Version_dependency_list* deps = vt->dependencies;
1997 for (size_t j = 0; j < deps->dependencies.size(); ++j)
1999 if (j < deps->dependencies.size() - 1)
2000 fprintf(f, "\n");
2001 fprintf(f, " %s", deps->dependencies[j].c_str());
2004 fprintf(f, ";\n");
2007 fprintf(f, "}\n");
2010 void
2011 Version_script_info::print_expression_list(
2012 FILE* f,
2013 const Version_expression_list* vel) const
2015 std::string current_language;
2016 for (size_t i = 0; i < vel->expressions.size(); ++i)
2018 const Version_expression& ve(vel->expressions[i]);
2020 if (ve.language != current_language)
2022 if (!current_language.empty())
2023 fprintf(f, " }\n");
2024 fprintf(f, " extern \"%s\" {\n", ve.language.c_str());
2025 current_language = ve.language;
2028 fprintf(f, " ");
2029 if (!current_language.empty())
2030 fprintf(f, " ");
2032 if (ve.exact_match)
2033 fprintf(f, "\"");
2034 fprintf(f, "%s", ve.pattern.c_str());
2035 if (ve.exact_match)
2036 fprintf(f, "\"");
2038 fprintf(f, "\n");
2041 if (!current_language.empty())
2042 fprintf(f, " }\n");
2045 } // End namespace gold.
2047 // The remaining functions are extern "C", so it's clearer to not put
2048 // them in namespace gold.
2050 using namespace gold;
2052 // This function is called by the bison parser to return the next
2053 // token.
2055 extern "C" int
2056 yylex(YYSTYPE* lvalp, void* closurev)
2058 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2059 const Token* token = closure->next_token();
2060 switch (token->classification())
2062 default:
2063 gold_unreachable();
2065 case Token::TOKEN_INVALID:
2066 yyerror(closurev, "invalid character");
2067 return 0;
2069 case Token::TOKEN_EOF:
2070 return 0;
2072 case Token::TOKEN_STRING:
2074 // This is either a keyword or a STRING.
2075 size_t len;
2076 const char* str = token->string_value(&len);
2077 int parsecode = 0;
2078 switch (closure->lex_mode())
2080 case Lex::LINKER_SCRIPT:
2081 parsecode = script_keywords.keyword_to_parsecode(str, len);
2082 break;
2083 case Lex::VERSION_SCRIPT:
2084 parsecode = version_script_keywords.keyword_to_parsecode(str, len);
2085 break;
2086 case Lex::DYNAMIC_LIST:
2087 parsecode = dynamic_list_keywords.keyword_to_parsecode(str, len);
2088 break;
2089 default:
2090 break;
2092 if (parsecode != 0)
2093 return parsecode;
2094 lvalp->string.value = str;
2095 lvalp->string.length = len;
2096 return STRING;
2099 case Token::TOKEN_QUOTED_STRING:
2100 lvalp->string.value = token->string_value(&lvalp->string.length);
2101 return QUOTED_STRING;
2103 case Token::TOKEN_OPERATOR:
2104 return token->operator_value();
2106 case Token::TOKEN_INTEGER:
2107 lvalp->integer = token->integer_value();
2108 return INTEGER;
2112 // This function is called by the bison parser to report an error.
2114 extern "C" void
2115 yyerror(void* closurev, const char* message)
2117 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2118 gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
2119 closure->charpos(), message);
2122 // Called by the bison parser to add an external symbol to the link.
2124 extern "C" void
2125 script_add_extern(void* closurev, const char* name, size_t length)
2127 // We treat exactly like -u NAME. FIXME: If it seems useful, we
2128 // could handle this after the command line has been read, by adding
2129 // entries to the symbol table directly.
2130 std::string arg("--undefined=");
2131 arg.append(name, length);
2132 script_parse_option(closurev, arg.c_str(), arg.size());
2135 // Called by the bison parser to add a file to the link.
2137 extern "C" void
2138 script_add_file(void* closurev, const char* name, size_t length)
2140 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2142 // If this is an absolute path, and we found the script in the
2143 // sysroot, then we want to prepend the sysroot to the file name.
2144 // For example, this is how we handle a cross link to the x86_64
2145 // libc.so, which refers to /lib/libc.so.6.
2146 std::string name_string(name, length);
2147 const char* extra_search_path = ".";
2148 std::string script_directory;
2149 if (IS_ABSOLUTE_PATH(name_string.c_str()))
2151 if (closure->is_in_sysroot())
2153 const std::string& sysroot(parameters->options().sysroot());
2154 gold_assert(!sysroot.empty());
2155 name_string = sysroot + name_string;
2158 else
2160 // In addition to checking the normal library search path, we
2161 // also want to check in the script-directory.
2162 const char *slash = strrchr(closure->filename(), '/');
2163 if (slash != NULL)
2165 script_directory.assign(closure->filename(),
2166 slash - closure->filename() + 1);
2167 extra_search_path = script_directory.c_str();
2171 Input_file_argument file(name_string.c_str(), false, extra_search_path,
2172 false, closure->position_dependent_options());
2173 closure->inputs()->add_file(file);
2176 // Called by the bison parser to start a group. If we are already in
2177 // a group, that means that this script was invoked within a
2178 // --start-group --end-group sequence on the command line, or that
2179 // this script was found in a GROUP of another script. In that case,
2180 // we simply continue the existing group, rather than starting a new
2181 // one. It is possible to construct a case in which this will do
2182 // something other than what would happen if we did a recursive group,
2183 // but it's hard to imagine why the different behaviour would be
2184 // useful for a real program. Avoiding recursive groups is simpler
2185 // and more efficient.
2187 extern "C" void
2188 script_start_group(void* closurev)
2190 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2191 if (!closure->in_group())
2192 closure->inputs()->start_group();
2195 // Called by the bison parser at the end of a group.
2197 extern "C" void
2198 script_end_group(void* closurev)
2200 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2201 if (!closure->in_group())
2202 closure->inputs()->end_group();
2205 // Called by the bison parser to start an AS_NEEDED list.
2207 extern "C" void
2208 script_start_as_needed(void* closurev)
2210 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2211 closure->position_dependent_options().set_as_needed(true);
2214 // Called by the bison parser at the end of an AS_NEEDED list.
2216 extern "C" void
2217 script_end_as_needed(void* closurev)
2219 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2220 closure->position_dependent_options().set_as_needed(false);
2223 // Called by the bison parser to set the entry symbol.
2225 extern "C" void
2226 script_set_entry(void* closurev, const char* entry, size_t length)
2228 // We'll parse this exactly the same as --entry=ENTRY on the commandline
2229 // TODO(csilvers): FIXME -- call set_entry directly.
2230 std::string arg("--entry=");
2231 arg.append(entry, length);
2232 script_parse_option(closurev, arg.c_str(), arg.size());
2235 // Called by the bison parser to set whether to define common symbols.
2237 extern "C" void
2238 script_set_common_allocation(void* closurev, int set)
2240 const char* arg = set != 0 ? "--define-common" : "--no-define-common";
2241 script_parse_option(closurev, arg, strlen(arg));
2244 // Called by the bison parser to define a symbol.
2246 extern "C" void
2247 script_set_symbol(void* closurev, const char* name, size_t length,
2248 Expression* value, int providei, int hiddeni)
2250 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2251 const bool provide = providei != 0;
2252 const bool hidden = hiddeni != 0;
2253 closure->script_options()->add_symbol_assignment(name, length, value,
2254 provide, hidden);
2255 closure->clear_skip_on_incompatible_target();
2258 // Called by the bison parser to add an assertion.
2260 extern "C" void
2261 script_add_assertion(void* closurev, Expression* check, const char* message,
2262 size_t messagelen)
2264 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2265 closure->script_options()->add_assertion(check, message, messagelen);
2266 closure->clear_skip_on_incompatible_target();
2269 // Called by the bison parser to parse an OPTION.
2271 extern "C" void
2272 script_parse_option(void* closurev, const char* option, size_t length)
2274 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2275 // We treat the option as a single command-line option, even if
2276 // it has internal whitespace.
2277 if (closure->command_line() == NULL)
2279 // There are some options that we could handle here--e.g.,
2280 // -lLIBRARY. Should we bother?
2281 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
2282 " for scripts specified via -T/--script"),
2283 closure->filename(), closure->lineno(), closure->charpos());
2285 else
2287 bool past_a_double_dash_option = false;
2288 const char* mutable_option = strndup(option, length);
2289 gold_assert(mutable_option != NULL);
2290 closure->command_line()->process_one_option(1, &mutable_option, 0,
2291 &past_a_double_dash_option);
2292 // The General_options class will quite possibly store a pointer
2293 // into mutable_option, so we can't free it. In cases the class
2294 // does not store such a pointer, this is a memory leak. Alas. :(
2296 closure->clear_skip_on_incompatible_target();
2299 // Called by the bison parser to handle OUTPUT_FORMAT. OUTPUT_FORMAT
2300 // takes either one or three arguments. In the three argument case,
2301 // the format depends on the endianness option, which we don't
2302 // currently support (FIXME). If we see an OUTPUT_FORMAT for the
2303 // wrong format, then we want to search for a new file. Returning 0
2304 // here will cause the parser to immediately abort.
2306 extern "C" int
2307 script_check_output_format(void* closurev,
2308 const char* default_name, size_t default_length,
2309 const char*, size_t, const char*, size_t)
2311 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2312 std::string name(default_name, default_length);
2313 Target* target = select_target_by_name(name.c_str());
2314 if (target == NULL || !parameters->is_compatible_target(target))
2316 if (closure->skip_on_incompatible_target())
2318 closure->set_found_incompatible_target();
2319 return 0;
2321 // FIXME: Should we warn about the unknown target?
2323 return 1;
2326 // Called by the bison parser to handle SEARCH_DIR. This is handled
2327 // exactly like a -L option.
2329 extern "C" void
2330 script_add_search_dir(void* closurev, const char* option, size_t length)
2332 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2333 if (closure->command_line() == NULL)
2334 gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid"
2335 " for scripts specified via -T/--script"),
2336 closure->filename(), closure->lineno(), closure->charpos());
2337 else
2339 std::string s = "-L" + std::string(option, length);
2340 script_parse_option(closurev, s.c_str(), s.size());
2344 /* Called by the bison parser to push the lexer into expression
2345 mode. */
2347 extern "C" void
2348 script_push_lex_into_expression_mode(void* closurev)
2350 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2351 closure->push_lex_mode(Lex::EXPRESSION);
2354 /* Called by the bison parser to push the lexer into version
2355 mode. */
2357 extern "C" void
2358 script_push_lex_into_version_mode(void* closurev)
2360 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2361 closure->push_lex_mode(Lex::VERSION_SCRIPT);
2364 /* Called by the bison parser to pop the lexer mode. */
2366 extern "C" void
2367 script_pop_lex_mode(void* closurev)
2369 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2370 closure->pop_lex_mode();
2373 // Register an entire version node. For example:
2375 // GLIBC_2.1 {
2376 // global: foo;
2377 // } GLIBC_2.0;
2379 // - tag is "GLIBC_2.1"
2380 // - tree contains the information "global: foo"
2381 // - deps contains "GLIBC_2.0"
2383 extern "C" void
2384 script_register_vers_node(void*,
2385 const char* tag,
2386 int taglen,
2387 struct Version_tree *tree,
2388 struct Version_dependency_list *deps)
2390 gold_assert(tree != NULL);
2391 tree->dependencies = deps;
2392 if (tag != NULL)
2393 tree->tag = std::string(tag, taglen);
2396 // Add a dependencies to the list of existing dependencies, if any,
2397 // and return the expanded list.
2399 extern "C" struct Version_dependency_list *
2400 script_add_vers_depend(void* closurev,
2401 struct Version_dependency_list *all_deps,
2402 const char *depend_to_add, int deplen)
2404 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2405 if (all_deps == NULL)
2406 all_deps = closure->version_script()->allocate_dependency_list();
2407 all_deps->dependencies.push_back(std::string(depend_to_add, deplen));
2408 return all_deps;
2411 // Add a pattern expression to an existing list of expressions, if any.
2412 // TODO: In the old linker, the last argument used to be a bool, but I
2413 // don't know what it meant.
2415 extern "C" struct Version_expression_list *
2416 script_new_vers_pattern(void* closurev,
2417 struct Version_expression_list *expressions,
2418 const char *pattern, int patlen, int exact_match)
2420 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2421 if (expressions == NULL)
2422 expressions = closure->version_script()->allocate_expression_list();
2423 expressions->expressions.push_back(
2424 Version_expression(std::string(pattern, patlen),
2425 closure->get_current_language(),
2426 static_cast<bool>(exact_match)));
2427 return expressions;
2430 // Attaches b to the end of a, and clears b. So a = a + b and b = {}.
2432 extern "C" struct Version_expression_list*
2433 script_merge_expressions(struct Version_expression_list *a,
2434 struct Version_expression_list *b)
2436 a->expressions.insert(a->expressions.end(),
2437 b->expressions.begin(), b->expressions.end());
2438 // We could delete b and remove it from expressions_lists_, but
2439 // that's a lot of work. This works just as well.
2440 b->expressions.clear();
2441 return a;
2444 // Combine the global and local expressions into a a Version_tree.
2446 extern "C" struct Version_tree *
2447 script_new_vers_node(void* closurev,
2448 struct Version_expression_list *global,
2449 struct Version_expression_list *local)
2451 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2452 Version_tree* tree = closure->version_script()->allocate_version_tree();
2453 tree->global = global;
2454 tree->local = local;
2455 return tree;
2458 // Handle a transition in language, such as at the
2459 // start or end of 'extern "C++"'
2461 extern "C" void
2462 version_script_push_lang(void* closurev, const char* lang, int langlen)
2464 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2465 closure->push_language(std::string(lang, langlen));
2468 extern "C" void
2469 version_script_pop_lang(void* closurev)
2471 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2472 closure->pop_language();
2475 // Called by the bison parser to start a SECTIONS clause.
2477 extern "C" void
2478 script_start_sections(void* closurev)
2480 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2481 closure->script_options()->script_sections()->start_sections();
2482 closure->clear_skip_on_incompatible_target();
2485 // Called by the bison parser to finish a SECTIONS clause.
2487 extern "C" void
2488 script_finish_sections(void* closurev)
2490 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2491 closure->script_options()->script_sections()->finish_sections();
2494 // Start processing entries for an output section.
2496 extern "C" void
2497 script_start_output_section(void* closurev, const char* name, size_t namelen,
2498 const struct Parser_output_section_header* header)
2500 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2501 closure->script_options()->script_sections()->start_output_section(name,
2502 namelen,
2503 header);
2506 // Finish processing entries for an output section.
2508 extern "C" void
2509 script_finish_output_section(void* closurev,
2510 const struct Parser_output_section_trailer* trail)
2512 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2513 closure->script_options()->script_sections()->finish_output_section(trail);
2516 // Add a data item (e.g., "WORD (0)") to the current output section.
2518 extern "C" void
2519 script_add_data(void* closurev, int data_token, Expression* val)
2521 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2522 int size;
2523 bool is_signed = true;
2524 switch (data_token)
2526 case QUAD:
2527 size = 8;
2528 is_signed = false;
2529 break;
2530 case SQUAD:
2531 size = 8;
2532 break;
2533 case LONG:
2534 size = 4;
2535 break;
2536 case SHORT:
2537 size = 2;
2538 break;
2539 case BYTE:
2540 size = 1;
2541 break;
2542 default:
2543 gold_unreachable();
2545 closure->script_options()->script_sections()->add_data(size, is_signed, val);
2548 // Add a clause setting the fill value to the current output section.
2550 extern "C" void
2551 script_add_fill(void* closurev, Expression* val)
2553 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2554 closure->script_options()->script_sections()->add_fill(val);
2557 // Add a new input section specification to the current output
2558 // section.
2560 extern "C" void
2561 script_add_input_section(void* closurev,
2562 const struct Input_section_spec* spec,
2563 int keepi)
2565 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2566 bool keep = keepi != 0;
2567 closure->script_options()->script_sections()->add_input_section(spec, keep);
2570 // When we see DATA_SEGMENT_ALIGN we record that following output
2571 // sections may be relro.
2573 extern "C" void
2574 script_data_segment_align(void* closurev)
2576 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2577 if (!closure->script_options()->saw_sections_clause())
2578 gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
2579 closure->filename(), closure->lineno(), closure->charpos());
2580 else
2581 closure->script_options()->script_sections()->data_segment_align();
2584 // When we see DATA_SEGMENT_RELRO_END we know that all output sections
2585 // since DATA_SEGMENT_ALIGN should be relro.
2587 extern "C" void
2588 script_data_segment_relro_end(void* closurev)
2590 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2591 if (!closure->script_options()->saw_sections_clause())
2592 gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
2593 closure->filename(), closure->lineno(), closure->charpos());
2594 else
2595 closure->script_options()->script_sections()->data_segment_relro_end();
2598 // Create a new list of string/sort pairs.
2600 extern "C" String_sort_list_ptr
2601 script_new_string_sort_list(const struct Wildcard_section* string_sort)
2603 return new String_sort_list(1, *string_sort);
2606 // Add an entry to a list of string/sort pairs. The way the parser
2607 // works permits us to simply modify the first parameter, rather than
2608 // copy the vector.
2610 extern "C" String_sort_list_ptr
2611 script_string_sort_list_add(String_sort_list_ptr pv,
2612 const struct Wildcard_section* string_sort)
2614 if (pv == NULL)
2615 return script_new_string_sort_list(string_sort);
2616 else
2618 pv->push_back(*string_sort);
2619 return pv;
2623 // Create a new list of strings.
2625 extern "C" String_list_ptr
2626 script_new_string_list(const char* str, size_t len)
2628 return new String_list(1, std::string(str, len));
2631 // Add an element to a list of strings. The way the parser works
2632 // permits us to simply modify the first parameter, rather than copy
2633 // the vector.
2635 extern "C" String_list_ptr
2636 script_string_list_push_back(String_list_ptr pv, const char* str, size_t len)
2638 if (pv == NULL)
2639 return script_new_string_list(str, len);
2640 else
2642 pv->push_back(std::string(str, len));
2643 return pv;
2647 // Concatenate two string lists. Either or both may be NULL. The way
2648 // the parser works permits us to modify the parameters, rather than
2649 // copy the vector.
2651 extern "C" String_list_ptr
2652 script_string_list_append(String_list_ptr pv1, String_list_ptr pv2)
2654 if (pv1 == NULL)
2655 return pv2;
2656 if (pv2 == NULL)
2657 return pv1;
2658 pv1->insert(pv1->end(), pv2->begin(), pv2->end());
2659 return pv1;
2662 // Add a new program header.
2664 extern "C" void
2665 script_add_phdr(void* closurev, const char* name, size_t namelen,
2666 unsigned int type, const Phdr_info* info)
2668 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2669 bool includes_filehdr = info->includes_filehdr != 0;
2670 bool includes_phdrs = info->includes_phdrs != 0;
2671 bool is_flags_valid = info->is_flags_valid != 0;
2672 Script_sections* ss = closure->script_options()->script_sections();
2673 ss->add_phdr(name, namelen, type, includes_filehdr, includes_phdrs,
2674 is_flags_valid, info->flags, info->load_address);
2675 closure->clear_skip_on_incompatible_target();
2678 // Convert a program header string to a type.
2680 #define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME }
2682 static struct
2684 const char* name;
2685 size_t namelen;
2686 unsigned int val;
2687 } phdr_type_names[] =
2689 PHDR_TYPE(PT_NULL),
2690 PHDR_TYPE(PT_LOAD),
2691 PHDR_TYPE(PT_DYNAMIC),
2692 PHDR_TYPE(PT_INTERP),
2693 PHDR_TYPE(PT_NOTE),
2694 PHDR_TYPE(PT_SHLIB),
2695 PHDR_TYPE(PT_PHDR),
2696 PHDR_TYPE(PT_TLS),
2697 PHDR_TYPE(PT_GNU_EH_FRAME),
2698 PHDR_TYPE(PT_GNU_STACK),
2699 PHDR_TYPE(PT_GNU_RELRO)
2702 extern "C" unsigned int
2703 script_phdr_string_to_type(void* closurev, const char* name, size_t namelen)
2705 for (unsigned int i = 0;
2706 i < sizeof(phdr_type_names) / sizeof(phdr_type_names[0]);
2707 ++i)
2708 if (namelen == phdr_type_names[i].namelen
2709 && strncmp(name, phdr_type_names[i].name, namelen) == 0)
2710 return phdr_type_names[i].val;
2711 yyerror(closurev, _("unknown PHDR type (try integer)"));
2712 return elfcpp::PT_NULL;