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.
30 #include "filenames.h"
34 #include "dirsearch.h"
37 #include "workqueue.h"
39 #include "parameters.h"
48 // A token read from a script file. We don't implement keywords here;
49 // all keywords are simply represented as a string.
54 // Token classification.
59 // Token indicates end of input.
61 // Token is a string of characters.
63 // Token is a quoted string of characters.
65 // Token is an operator.
67 // Token is a number (an integer).
71 // We need an empty constructor so that we can put this STL objects.
73 : classification_(TOKEN_INVALID), value_(NULL), value_length_(0),
74 opcode_(0), lineno_(0), charpos_(0)
77 // A general token with no value.
78 Token(Classification classification, int lineno, int charpos)
79 : classification_(classification), value_(NULL), value_length_(0),
80 opcode_(0), lineno_(lineno), charpos_(charpos)
82 gold_assert(classification == TOKEN_INVALID
83 || classification == TOKEN_EOF);
86 // A general token with a value.
87 Token(Classification classification, const char* value, size_t length,
88 int lineno, int charpos)
89 : classification_(classification), value_(value), value_length_(length),
90 opcode_(0), lineno_(lineno), charpos_(charpos)
92 gold_assert(classification != TOKEN_INVALID
93 && classification != TOKEN_EOF);
96 // A token representing an operator.
97 Token(int opcode, int lineno, int charpos)
98 : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0),
99 opcode_(opcode), lineno_(lineno), charpos_(charpos)
102 // Return whether the token is invalid.
105 { return this->classification_ == TOKEN_INVALID; }
107 // Return whether this is an EOF token.
110 { return this->classification_ == TOKEN_EOF; }
112 // Return the token classification.
114 classification() const
115 { return this->classification_; }
117 // Return the line number at which the token starts.
120 { return this->lineno_; }
122 // Return the character position at this the token starts.
125 { return this->charpos_; }
127 // Get the value of a token.
130 string_value(size_t* length) const
132 gold_assert(this->classification_ == TOKEN_STRING
133 || this->classification_ == TOKEN_QUOTED_STRING);
134 *length = this->value_length_;
139 operator_value() const
141 gold_assert(this->classification_ == TOKEN_OPERATOR);
142 return this->opcode_;
146 integer_value() const
148 gold_assert(this->classification_ == TOKEN_INTEGER);
150 std::string s(this->value_, this->value_length_);
151 return strtoull(s.c_str(), NULL, 0);
155 // The token classification.
156 Classification classification_;
157 // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or
160 // The length of the token value.
161 size_t value_length_;
162 // The token value, for TOKEN_OPERATOR.
164 // The line number where this token started (one based).
166 // The character position within the line where this token started
171 // This class handles lexing a file into a sequence of tokens.
176 // We unfortunately have to support different lexing modes, because
177 // when reading different parts of a linker script we need to parse
178 // things differently.
181 // Reading an ordinary linker script.
183 // Reading an expression in a linker script.
185 // Reading a version script.
189 Lex(const char* input_string, size_t input_length, int parsing_token)
190 : input_string_(input_string), input_length_(input_length),
191 current_(input_string), mode_(LINKER_SCRIPT),
192 first_token_(parsing_token), token_(),
193 lineno_(1), linestart_(input_string)
196 // Read a file into a string.
198 read_file(Input_file*, std::string*);
200 // Return the next token.
204 // Return the current lexing mode.
207 { return this->mode_; }
209 // Set the lexing mode.
212 { this->mode_ = mode; }
216 Lex& operator=(const Lex&);
218 // Make a general token with no value at the current location.
220 make_token(Token::Classification c, const char* start) const
221 { return Token(c, this->lineno_, start - this->linestart_ + 1); }
223 // Make a general token with a value at the current location.
225 make_token(Token::Classification c, const char* v, size_t len,
228 { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); }
230 // Make an operator token at the current location.
232 make_token(int opcode, const char* start) const
233 { return Token(opcode, this->lineno_, start - this->linestart_ + 1); }
235 // Make an invalid token at the current location.
237 make_invalid_token(const char* start)
238 { return this->make_token(Token::TOKEN_INVALID, start); }
240 // Make an EOF token at the current location.
242 make_eof_token(const char* start)
243 { return this->make_token(Token::TOKEN_EOF, start); }
245 // Return whether C can be the first character in a name. C2 is the
246 // next character, since we sometimes need that.
248 can_start_name(char c, char c2);
250 // If C can appear in a name which has already started, return a
251 // pointer to a character later in the token or just past
252 // it. Otherwise, return NULL.
254 can_continue_name(const char* c);
256 // Return whether C, C2, C3 can start a hex number.
258 can_start_hex(char c, char c2, char c3);
260 // If C can appear in a hex number which has already started, return
261 // a pointer to a character later in the token or just past
262 // it. Otherwise, return NULL.
264 can_continue_hex(const char* c);
266 // Return whether C can start a non-hex number.
268 can_start_number(char c);
270 // If C can appear in a decimal number which has already started,
271 // return a pointer to a character later in the token or just past
272 // it. Otherwise, return NULL.
274 can_continue_number(const char* c)
275 { return Lex::can_start_number(*c) ? c + 1 : NULL; }
277 // If C1 C2 C3 form a valid three character operator, return the
278 // opcode. Otherwise return 0.
280 three_char_operator(char c1, char c2, char c3);
282 // If C1 C2 form a valid two character operator, return the opcode.
283 // Otherwise return 0.
285 two_char_operator(char c1, char c2);
287 // If C1 is a valid one character operator, return the opcode.
288 // Otherwise return 0.
290 one_char_operator(char c1);
292 // Read the next token.
294 get_token(const char**);
296 // Skip a C style /* */ comment. Return false if the comment did
299 skip_c_comment(const char**);
301 // Skip a line # comment. Return false if there was no newline.
303 skip_line_comment(const char**);
305 // Build a token CLASSIFICATION from all characters that match
306 // CAN_CONTINUE_FN. The token starts at START. Start matching from
307 // MATCH. Set *PP to the character following the token.
309 gather_token(Token::Classification,
310 const char* (Lex::*can_continue_fn)(const char*),
311 const char* start, const char* match, const char** pp);
313 // Build a token from a quoted string.
315 gather_quoted_string(const char** pp);
317 // The string we are tokenizing.
318 const char* input_string_;
319 // The length of the string.
320 size_t input_length_;
321 // The current offset into the string.
322 const char* current_;
323 // The current lexing mode.
325 // The code to use for the first token. This is set to 0 after it
328 // The current token.
330 // The current line number.
332 // The start of the current line in the string.
333 const char* linestart_;
336 // Read the whole file into memory. We don't expect linker scripts to
337 // be large, so we just use a std::string as a buffer. We ignore the
338 // data we've already read, so that we read aligned buffers.
341 Lex::read_file(Input_file* input_file, std::string* contents)
343 off_t filesize = input_file->file().filesize();
345 contents->reserve(filesize);
348 unsigned char buf[BUFSIZ];
349 while (off < filesize)
352 if (get > filesize - off)
353 get = filesize - off;
354 input_file->file().read(off, get, buf);
355 contents->append(reinterpret_cast<char*>(&buf[0]), get);
360 // Return whether C can be the start of a name, if the next character
361 // is C2. A name can being with a letter, underscore, period, or
362 // dollar sign. Because a name can be a file name, we also permit
363 // forward slash, backslash, and tilde. Tilde is the tricky case
364 // here; GNU ld also uses it as a bitwise not operator. It is only
365 // recognized as the operator if it is not immediately followed by
366 // some character which can appear in a symbol. That is, when we
367 // don't know that we are looking at an expression, "~0" is a file
368 // name, and "~ 0" is an expression using bitwise not. We are
372 Lex::can_start_name(char c, char c2)
376 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
377 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
378 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
379 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
381 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
382 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
383 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
384 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
386 case '_': case '.': case '$':
390 return this->mode_ == LINKER_SCRIPT;
393 return this->mode_ == LINKER_SCRIPT && can_continue_name(&c2);
396 return (this->mode_ == VERSION_SCRIPT
397 || (this->mode_ == LINKER_SCRIPT
398 && can_continue_name(&c2)));
405 // Return whether C can continue a name which has already started.
406 // Subsequent characters in a name are the same as the leading
407 // characters, plus digits and "=+-:[],?*". So in general the linker
408 // script language requires spaces around operators, unless we know
409 // that we are parsing an expression.
412 Lex::can_continue_name(const char* c)
416 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
417 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
418 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
419 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
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':
426 case '_': case '.': case '$':
427 case '0': case '1': case '2': case '3': case '4':
428 case '5': case '6': case '7': case '8': case '9':
431 case '/': case '\\': case '~':
434 if (this->mode_ == LINKER_SCRIPT)
438 case '[': case ']': case '*': case '-':
439 if (this->mode_ == LINKER_SCRIPT || this->mode_ == VERSION_SCRIPT)
444 if (this->mode_ == VERSION_SCRIPT)
449 if (this->mode_ == LINKER_SCRIPT)
451 else if (this->mode_ == VERSION_SCRIPT && (c[1] == ':'))
453 // A name can have '::' in it, as that's a c++ namespace
454 // separator. But a single colon is not part of a name.
464 // For a number we accept 0x followed by hex digits, or any sequence
465 // of digits. The old linker accepts leading '$' for hex, and
466 // trailing HXBOD. Those are for MRI compatibility and we don't
467 // accept them. The old linker also accepts trailing MK for mega or
468 // kilo. FIXME: Those are mentioned in the documentation, and we
469 // should accept them.
471 // Return whether C1 C2 C3 can start a hex number.
474 Lex::can_start_hex(char c1, char c2, char c3)
476 if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
477 return this->can_continue_hex(&c3);
481 // Return whether C can appear in a hex number.
484 Lex::can_continue_hex(const char* c)
488 case '0': case '1': case '2': case '3': case '4':
489 case '5': case '6': case '7': case '8': case '9':
490 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
491 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
499 // Return whether C can start a non-hex number.
502 Lex::can_start_number(char c)
506 case '0': case '1': case '2': case '3': case '4':
507 case '5': case '6': case '7': case '8': case '9':
515 // If C1 C2 C3 form a valid three character operator, return the
516 // opcode (defined in the yyscript.h file generated from yyscript.y).
517 // Otherwise return 0.
520 Lex::three_char_operator(char c1, char c2, char c3)
525 if (c2 == '<' && c3 == '=')
529 if (c2 == '>' && c3 == '=')
538 // If C1 C2 form a valid two character operator, return the opcode
539 // (defined in the yyscript.h file generated from yyscript.y).
540 // Otherwise return 0.
543 Lex::two_char_operator(char c1, char c2)
601 // If C1 is a valid operator, return the opcode. Otherwise return 0.
604 Lex::one_char_operator(char c1)
637 // Skip a C style comment. *PP points to just after the "/*". Return
638 // false if the comment did not end.
641 Lex::skip_c_comment(const char** pp)
644 while (p[0] != '*' || p[1] != '/')
655 this->linestart_ = p + 1;
664 // Skip a line # comment. Return false if there was no newline.
667 Lex::skip_line_comment(const char** pp)
670 size_t skip = strcspn(p, "\n");
679 this->linestart_ = p;
685 // Build a token CLASSIFICATION from all characters that match
686 // CAN_CONTINUE_FN. Update *PP.
689 Lex::gather_token(Token::Classification classification,
690 const char* (Lex::*can_continue_fn)(const char*),
695 const char* new_match = NULL;
696 while ((new_match = (this->*can_continue_fn)(match)))
699 return this->make_token(classification, start, match - start, start);
702 // Build a token from a quoted string.
705 Lex::gather_quoted_string(const char** pp)
707 const char* start = *pp;
708 const char* p = start;
710 size_t skip = strcspn(p, "\"\n");
712 return this->make_invalid_token(start);
714 return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
717 // Return the next token at *PP. Update *PP. General guideline: we
718 // require linker scripts to be simple ASCII. No unicode linker
719 // scripts. In particular we can assume that any '\0' is the end of
723 Lex::get_token(const char** pp)
732 return this->make_eof_token(p);
735 // Skip whitespace quickly.
736 while (*p == ' ' || *p == '\t')
743 this->linestart_ = p;
747 // Skip C style comments.
748 if (p[0] == '/' && p[1] == '*')
750 int lineno = this->lineno_;
751 int charpos = p - this->linestart_ + 1;
754 if (!this->skip_c_comment(pp))
755 return Token(Token::TOKEN_INVALID, lineno, charpos);
761 // Skip line comments.
765 if (!this->skip_line_comment(pp))
766 return this->make_eof_token(p);
772 if (this->can_start_name(p[0], p[1]))
773 return this->gather_token(Token::TOKEN_STRING,
774 &Lex::can_continue_name,
777 // We accept any arbitrary name in double quotes, as long as it
778 // does not cross a line boundary.
782 return this->gather_quoted_string(pp);
785 // Check for a number.
787 if (this->can_start_hex(p[0], p[1], p[2]))
788 return this->gather_token(Token::TOKEN_INTEGER,
789 &Lex::can_continue_hex,
792 if (Lex::can_start_number(p[0]))
793 return this->gather_token(Token::TOKEN_INTEGER,
794 &Lex::can_continue_number,
797 // Check for operators.
799 int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
803 return this->make_token(opcode, p);
806 opcode = Lex::two_char_operator(p[0], p[1]);
810 return this->make_token(opcode, p);
813 opcode = Lex::one_char_operator(p[0]);
817 return this->make_token(opcode, p);
820 return this->make_token(Token::TOKEN_INVALID, p);
824 // Return the next token.
829 // The first token is special.
830 if (this->first_token_ != 0)
832 this->token_ = Token(this->first_token_, 0, 0);
833 this->first_token_ = 0;
834 return &this->token_;
837 this->token_ = this->get_token(&this->current_);
839 // Don't let an early null byte fool us into thinking that we've
840 // reached the end of the file.
841 if (this->token_.is_eof()
842 && (static_cast<size_t>(this->current_ - this->input_string_)
843 < this->input_length_))
844 this->token_ = this->make_invalid_token(this->current_);
846 return &this->token_;
849 // A trivial task which waits for THIS_BLOCKER to be clear and then
850 // clears NEXT_BLOCKER. THIS_BLOCKER may be NULL.
852 class Script_unblock : public Task
855 Script_unblock(Task_token* this_blocker, Task_token* next_blocker)
856 : this_blocker_(this_blocker), next_blocker_(next_blocker)
861 if (this->this_blocker_ != NULL)
862 delete this->this_blocker_;
868 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
869 return this->this_blocker_;
874 locks(Task_locker* tl)
875 { tl->add(this, this->next_blocker_); }
883 { return "Script_unblock"; }
886 Task_token* this_blocker_;
887 Task_token* next_blocker_;
890 // class Symbol_assignment.
892 // Add the symbol to the symbol table. This makes sure the symbol is
893 // there and defined. The actual value is stored later. We can't
894 // determine the actual value at this point, because we can't
895 // necessarily evaluate the expression until all ordinary symbols have
899 Symbol_assignment::add_to_table(Symbol_table* symtab)
901 elfcpp::STV vis = this->hidden_ ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
902 this->sym_ = symtab->define_as_constant(this->name_.c_str(),
913 // Finalize a symbol value.
916 Symbol_assignment::finalize(Symbol_table* symtab, const Layout* layout)
918 this->finalize_maybe_dot(symtab, layout, false, false, 0);
921 // Finalize a symbol value which can refer to the dot symbol.
924 Symbol_assignment::finalize_with_dot(Symbol_table* symtab,
925 const Layout* layout,
929 this->finalize_maybe_dot(symtab, layout, true, dot_has_value, dot_value);
932 // Finalize a symbol value, internal version.
935 Symbol_assignment::finalize_maybe_dot(Symbol_table* symtab,
936 const Layout* layout,
937 bool is_dot_available,
941 // If we were only supposed to provide this symbol, the sym_ field
942 // will be NULL if the symbol was not referenced.
943 if (this->sym_ == NULL)
945 gold_assert(this->provide_);
949 if (parameters->get_size() == 32)
951 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
952 this->sized_finalize<32>(symtab, layout, is_dot_available, dot_has_value,
958 else if (parameters->get_size() == 64)
960 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
961 this->sized_finalize<64>(symtab, layout, is_dot_available, dot_has_value,
973 Symbol_assignment::sized_finalize(Symbol_table* symtab, const Layout* layout,
974 bool is_dot_available, bool dot_has_value,
978 uint64_t final_val = this->val_->eval_maybe_dot(symtab, layout,
980 dot_has_value, dot_value,
982 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(this->sym_);
983 ssym->set_value(final_val);
986 // Set the symbol value if the expression yields an absolute value.
989 Symbol_assignment::set_if_absolute(Symbol_table* symtab, const Layout* layout,
990 bool is_dot_available, bool dot_has_value,
993 if (this->sym_ == NULL)
997 uint64_t val = this->val_->eval_maybe_dot(symtab, layout, is_dot_available,
998 dot_has_value, dot_value,
1003 if (parameters->get_size() == 32)
1005 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1006 Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(this->sym_);
1007 ssym->set_value(val);
1012 else if (parameters->get_size() == 64)
1014 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1015 Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(this->sym_);
1016 ssym->set_value(val);
1025 // Print for debugging.
1028 Symbol_assignment::print(FILE* f) const
1030 if (this->provide_ && this->hidden_)
1031 fprintf(f, "PROVIDE_HIDDEN(");
1032 else if (this->provide_)
1033 fprintf(f, "PROVIDE(");
1034 else if (this->hidden_)
1037 fprintf(f, "%s = ", this->name_.c_str());
1038 this->val_->print(f);
1040 if (this->provide_ || this->hidden_)
1046 // Class Script_assertion.
1048 // Check the assertion.
1051 Script_assertion::check(const Symbol_table* symtab, const Layout* layout)
1053 if (!this->check_->eval(symtab, layout))
1054 gold_error("%s", this->message_.c_str());
1057 // Print for debugging.
1060 Script_assertion::print(FILE* f) const
1062 fprintf(f, "ASSERT(");
1063 this->check_->print(f);
1064 fprintf(f, ", \"%s\")\n", this->message_.c_str());
1067 // Class Script_options.
1069 Script_options::Script_options()
1070 : entry_(), symbol_assignments_(), version_script_info_(),
1075 // Add a symbol to be defined.
1078 Script_options::add_symbol_assignment(const char* name, size_t length,
1079 Expression* value, bool provide,
1082 if (length != 1 || name[0] != '.')
1084 if (this->script_sections_.in_sections_clause())
1085 this->script_sections_.add_symbol_assignment(name, length, value,
1089 Symbol_assignment* p = new Symbol_assignment(name, length, value,
1091 this->symbol_assignments_.push_back(p);
1096 if (provide || hidden)
1097 gold_error(_("invalid use of PROVIDE for dot symbol"));
1098 if (!this->script_sections_.in_sections_clause())
1099 gold_error(_("invalid assignment to dot outside of SECTIONS"));
1101 this->script_sections_.add_dot_assignment(value);
1105 // Add an assertion.
1108 Script_options::add_assertion(Expression* check, const char* message,
1111 if (this->script_sections_.in_sections_clause())
1112 this->script_sections_.add_assertion(check, message, messagelen);
1115 Script_assertion* p = new Script_assertion(check, message, messagelen);
1116 this->assertions_.push_back(p);
1120 // Add any symbols we are defining to the symbol table.
1123 Script_options::add_symbols_to_table(Symbol_table* symtab)
1125 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1126 p != this->symbol_assignments_.end();
1128 (*p)->add_to_table(symtab);
1129 this->script_sections_.add_symbols_to_table(symtab);
1132 // Finalize symbol values. Also check assertions.
1135 Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
1137 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1138 p != this->symbol_assignments_.end();
1140 (*p)->finalize(symtab, layout);
1142 for (Assertions::iterator p = this->assertions_.begin();
1143 p != this->assertions_.end();
1145 (*p)->check(symtab, layout);
1147 this->script_sections_.finalize_symbols(symtab, layout);
1150 // Set section addresses. We set all the symbols which have absolute
1151 // values. Then we let the SECTIONS clause do its thing. This
1152 // returns the segment which holds the file header and segment
1156 Script_options::set_section_addresses(Symbol_table* symtab, Layout* layout)
1158 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1159 p != this->symbol_assignments_.end();
1161 (*p)->set_if_absolute(symtab, layout, false, false, 0);
1163 return this->script_sections_.set_section_addresses(symtab, layout);
1166 // This class holds data passed through the parser to the lexer and to
1167 // the parser support functions. This avoids global variables. We
1168 // can't use global variables because we need not be called by a
1169 // singleton thread.
1171 class Parser_closure
1174 Parser_closure(const char* filename,
1175 const Position_dependent_options& posdep_options,
1176 bool in_group, bool is_in_sysroot,
1177 Command_line* command_line,
1178 Script_options* script_options,
1180 : filename_(filename), posdep_options_(posdep_options),
1181 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
1182 command_line_(command_line), script_options_(script_options),
1183 version_script_info_(script_options->version_script_info()),
1184 lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL)
1186 // We start out processing C symbols in the default lex mode.
1187 language_stack_.push_back("");
1188 lex_mode_stack_.push_back(lex->mode());
1191 // Return the file name.
1194 { return this->filename_; }
1196 // Return the position dependent options. The caller may modify
1198 Position_dependent_options&
1199 position_dependent_options()
1200 { return this->posdep_options_; }
1202 // Return whether this script is being run in a group.
1205 { return this->in_group_; }
1207 // Return whether this script was found using a directory in the
1210 is_in_sysroot() const
1211 { return this->is_in_sysroot_; }
1213 // Returns the Command_line structure passed in at constructor time.
1214 // This value may be NULL. The caller may modify this, which modifies
1215 // the passed-in Command_line object (not a copy).
1218 { return this->command_line_; }
1220 // Return the options which may be set by a script.
1223 { return this->script_options_; }
1225 // Return the object in which version script information should be stored.
1226 Version_script_info*
1228 { return this->version_script_info_; }
1230 // Return the next token, and advance.
1234 const Token* token = this->lex_->next_token();
1235 this->lineno_ = token->lineno();
1236 this->charpos_ = token->charpos();
1240 // Set a new lexer mode, pushing the current one.
1242 push_lex_mode(Lex::Mode mode)
1244 this->lex_mode_stack_.push_back(this->lex_->mode());
1245 this->lex_->set_mode(mode);
1248 // Pop the lexer mode.
1252 gold_assert(!this->lex_mode_stack_.empty());
1253 this->lex_->set_mode(this->lex_mode_stack_.back());
1254 this->lex_mode_stack_.pop_back();
1257 // Return the current lexer mode.
1260 { return this->lex_mode_stack_.back(); }
1262 // Return the line number of the last token.
1265 { return this->lineno_; }
1267 // Return the character position in the line of the last token.
1270 { return this->charpos_; }
1272 // Return the list of input files, creating it if necessary. This
1273 // is a space leak--we never free the INPUTS_ pointer.
1277 if (this->inputs_ == NULL)
1278 this->inputs_ = new Input_arguments();
1279 return this->inputs_;
1282 // Return whether we saw any input files.
1285 { return this->inputs_ != NULL && !this->inputs_->empty(); }
1287 // Return the current language being processed in a version script
1288 // (eg, "C++"). The empty string represents unmangled C names.
1290 get_current_language() const
1291 { return this->language_stack_.back(); }
1293 // Push a language onto the stack when entering an extern block.
1294 void push_language(const std::string& lang)
1295 { this->language_stack_.push_back(lang); }
1297 // Pop a language off of the stack when exiting an extern block.
1300 gold_assert(!this->language_stack_.empty());
1301 this->language_stack_.pop_back();
1305 // The name of the file we are reading.
1306 const char* filename_;
1307 // The position dependent options.
1308 Position_dependent_options posdep_options_;
1309 // Whether we are currently in a --start-group/--end-group.
1311 // Whether the script was found in a sysrooted directory.
1312 bool is_in_sysroot_;
1313 // May be NULL if the user chooses not to pass one in.
1314 Command_line* command_line_;
1315 // Options which may be set from any linker script.
1316 Script_options* script_options_;
1317 // Information parsed from a version script.
1318 Version_script_info* version_script_info_;
1321 // The line number of the last token returned by next_token.
1323 // The column number of the last token returned by next_token.
1325 // A stack of lexer modes.
1326 std::vector<Lex::Mode> lex_mode_stack_;
1327 // A stack of which extern/language block we're inside. Can be C++,
1328 // java, or empty for C.
1329 std::vector<std::string> language_stack_;
1330 // New input files found to add to the link.
1331 Input_arguments* inputs_;
1334 // FILE was found as an argument on the command line. Try to read it
1335 // as a script. We've already read BYTES of data into P, but we
1336 // ignore that. Return true if the file was handled.
1339 read_input_script(Workqueue* workqueue, const General_options& options,
1340 Symbol_table* symtab, Layout* layout,
1341 Dirsearch* dirsearch, Input_objects* input_objects,
1342 Input_group* input_group,
1343 const Input_argument* input_argument,
1344 Input_file* input_file, const unsigned char*, off_t,
1345 Task_token* this_blocker, Task_token* next_blocker)
1347 std::string input_string;
1348 Lex::read_file(input_file, &input_string);
1350 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1352 Parser_closure closure(input_file->filename().c_str(),
1353 input_argument->file().options(),
1354 input_group != NULL,
1355 input_file->is_in_sysroot(),
1357 layout->script_options(),
1360 if (yyparse(&closure) != 0)
1363 // THIS_BLOCKER must be clear before we may add anything to the
1364 // symbol table. We are responsible for unblocking NEXT_BLOCKER
1365 // when we are done. We are responsible for deleting THIS_BLOCKER
1366 // when it is unblocked.
1368 if (!closure.saw_inputs())
1370 // The script did not add any files to read. Note that we are
1371 // not permitted to call NEXT_BLOCKER->unblock() here even if
1372 // THIS_BLOCKER is NULL, as we do not hold the workqueue lock.
1373 workqueue->queue(new Script_unblock(this_blocker, next_blocker));
1377 for (Input_arguments::const_iterator p = closure.inputs()->begin();
1378 p != closure.inputs()->end();
1382 if (p + 1 == closure.inputs()->end())
1386 nb = new Task_token(true);
1389 workqueue->queue(new Read_symbols(options, input_objects, symtab,
1390 layout, dirsearch, &*p,
1391 input_group, this_blocker, nb));
1398 // Helper function for read_version_script() and
1399 // read_commandline_script(). Processes the given file in the mode
1400 // indicated by first_token and lex_mode.
1403 read_script_file(const char* filename, Command_line* cmdline,
1404 int first_token, Lex::Mode lex_mode)
1406 // TODO: if filename is a relative filename, search for it manually
1407 // using "." + cmdline->options()->search_path() -- not dirsearch.
1408 Dirsearch dirsearch;
1410 // The file locking code wants to record a Task, but we haven't
1411 // started the workqueue yet. This is only for debugging purposes,
1412 // so we invent a fake value.
1413 const Task* task = reinterpret_cast<const Task*>(-1);
1415 Input_file_argument input_argument(filename, false, "", false,
1416 cmdline->position_dependent_options());
1417 Input_file input_file(&input_argument);
1418 if (!input_file.open(cmdline->options(), dirsearch, task))
1421 std::string input_string;
1422 Lex::read_file(&input_file, &input_string);
1424 Lex lex(input_string.c_str(), input_string.length(), first_token);
1425 lex.set_mode(lex_mode);
1427 Parser_closure closure(filename,
1428 cmdline->position_dependent_options(),
1430 input_file.is_in_sysroot(),
1432 cmdline->script_options(),
1434 if (yyparse(&closure) != 0)
1436 input_file.file().unlock(task);
1440 input_file.file().unlock(task);
1442 gold_assert(!closure.saw_inputs());
1447 // FILENAME was found as an argument to --script (-T).
1448 // Read it as a script, and execute its contents immediately.
1451 read_commandline_script(const char* filename, Command_line* cmdline)
1453 return read_script_file(filename, cmdline,
1454 PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT);
1457 // FILE was found as an argument to --version-script. Read it as a
1458 // version script, and store its contents in
1459 // cmdline->script_options()->version_script_info().
1462 read_version_script(const char* filename, Command_line* cmdline)
1464 return read_script_file(filename, cmdline,
1465 PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT);
1468 // Implement the --defsym option on the command line. Return true if
1472 Script_options::define_symbol(const char* definition)
1474 Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1475 lex.set_mode(Lex::EXPRESSION);
1478 Position_dependent_options posdep_options;
1480 Parser_closure closure("command line", posdep_options, false, false, NULL,
1483 if (yyparse(&closure) != 0)
1486 gold_assert(!closure.saw_inputs());
1491 // Print the script to F for debugging.
1494 Script_options::print(FILE* f) const
1496 fprintf(f, "%s: Dumping linker script\n", program_name);
1498 if (!this->entry_.empty())
1499 fprintf(f, "ENTRY(%s)\n", this->entry_.c_str());
1501 for (Symbol_assignments::const_iterator p =
1502 this->symbol_assignments_.begin();
1503 p != this->symbol_assignments_.end();
1507 for (Assertions::const_iterator p = this->assertions_.begin();
1508 p != this->assertions_.end();
1512 this->script_sections_.print(f);
1514 this->version_script_info_.print(f);
1517 // Manage mapping from keywords to the codes expected by the bison
1518 // parser. We construct one global object for each lex mode with
1521 class Keyword_to_parsecode
1524 // The structure which maps keywords to parsecodes.
1525 struct Keyword_parsecode
1528 const char* keyword;
1529 // Corresponding parsecode.
1533 Keyword_to_parsecode(const Keyword_parsecode* keywords,
1535 : keyword_parsecodes_(keywords), keyword_count_(keyword_count)
1538 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1541 keyword_to_parsecode(const char* keyword, size_t len) const;
1544 const Keyword_parsecode* keyword_parsecodes_;
1545 const int keyword_count_;
1548 // Mapping from keyword string to keyword parsecode. This array must
1549 // be kept in sorted order. Parsecodes are looked up using bsearch.
1550 // This array must correspond to the list of parsecodes in yyscript.y.
1552 static const Keyword_to_parsecode::Keyword_parsecode
1553 script_keyword_parsecodes[] =
1555 { "ABSOLUTE", ABSOLUTE },
1557 { "ALIGN", ALIGN_K },
1558 { "ALIGNOF", ALIGNOF },
1559 { "ASSERT", ASSERT_K },
1560 { "AS_NEEDED", AS_NEEDED },
1565 { "CONSTANT", CONSTANT },
1566 { "CONSTRUCTORS", CONSTRUCTORS },
1567 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1568 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1569 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1570 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1571 { "DEFINED", DEFINED },
1573 { "EXCLUDE_FILE", EXCLUDE_FILE },
1574 { "EXTERN", EXTERN },
1577 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1580 { "INCLUDE", INCLUDE },
1581 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1584 { "LENGTH", LENGTH },
1585 { "LOADADDR", LOADADDR },
1589 { "MEMORY", MEMORY },
1592 { "NOCROSSREFS", NOCROSSREFS },
1593 { "NOFLOAT", NOFLOAT },
1594 { "ONLY_IF_RO", ONLY_IF_RO },
1595 { "ONLY_IF_RW", ONLY_IF_RW },
1596 { "OPTION", OPTION },
1597 { "ORIGIN", ORIGIN },
1598 { "OUTPUT", OUTPUT },
1599 { "OUTPUT_ARCH", OUTPUT_ARCH },
1600 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1601 { "OVERLAY", OVERLAY },
1603 { "PROVIDE", PROVIDE },
1604 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1606 { "SEARCH_DIR", SEARCH_DIR },
1607 { "SECTIONS", SECTIONS },
1608 { "SEGMENT_START", SEGMENT_START },
1610 { "SIZEOF", SIZEOF },
1611 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1612 { "SORT", SORT_BY_NAME },
1613 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1614 { "SORT_BY_NAME", SORT_BY_NAME },
1615 { "SPECIAL", SPECIAL },
1617 { "STARTUP", STARTUP },
1618 { "SUBALIGN", SUBALIGN },
1619 { "SYSLIB", SYSLIB },
1620 { "TARGET", TARGET_K },
1621 { "TRUNCATE", TRUNCATE },
1622 { "VERSION", VERSIONK },
1623 { "global", GLOBAL },
1629 { "sizeof_headers", SIZEOF_HEADERS },
1632 static const Keyword_to_parsecode
1633 script_keywords(&script_keyword_parsecodes[0],
1634 (sizeof(script_keyword_parsecodes)
1635 / sizeof(script_keyword_parsecodes[0])));
1637 static const Keyword_to_parsecode::Keyword_parsecode
1638 version_script_keyword_parsecodes[] =
1640 { "extern", EXTERN },
1641 { "global", GLOBAL },
1645 static const Keyword_to_parsecode
1646 version_script_keywords(&version_script_keyword_parsecodes[0],
1647 (sizeof(version_script_keyword_parsecodes)
1648 / sizeof(version_script_keyword_parsecodes[0])));
1650 // Comparison function passed to bsearch.
1662 ktt_compare(const void* keyv, const void* kttv)
1664 const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
1665 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1666 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1667 int i = strncmp(key->str, ktt->keyword, key->len);
1670 if (ktt->keyword[key->len] != '\0')
1675 } // End extern "C".
1678 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword,
1684 void* kttv = bsearch(&key,
1685 this->keyword_parsecodes_,
1686 this->keyword_count_,
1687 sizeof(this->keyword_parsecodes_[0]),
1691 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1692 return ktt->parsecode;
1695 // The following structs are used within the VersionInfo class as well
1696 // as in the bison helper functions. They store the information
1697 // parsed from the version script.
1699 // A single version expression.
1700 // For example, pattern="std::map*" and language="C++".
1701 // pattern and language should be from the stringpool
1702 struct Version_expression {
1703 Version_expression(const std::string& pattern,
1704 const std::string& language,
1706 : pattern(pattern), language(language), exact_match(exact_match) {}
1708 std::string pattern;
1709 std::string language;
1710 // If false, we use glob() to match pattern. If true, we use strcmp().
1715 // A list of expressions.
1716 struct Version_expression_list {
1717 std::vector<struct Version_expression> expressions;
1721 // A list of which versions upon which another version depends.
1722 // Strings should be from the Stringpool.
1723 struct Version_dependency_list {
1724 std::vector<std::string> dependencies;
1728 // The total definition of a version. It includes the tag for the
1729 // version, its global and local expressions, and any dependencies.
1730 struct Version_tree {
1732 : tag(), global(NULL), local(NULL), dependencies(NULL) {}
1735 const struct Version_expression_list* global;
1736 const struct Version_expression_list* local;
1737 const struct Version_dependency_list* dependencies;
1740 Version_script_info::~Version_script_info()
1746 Version_script_info::clear()
1748 for (size_t k = 0; k < dependency_lists_.size(); ++k)
1749 delete dependency_lists_[k];
1750 this->dependency_lists_.clear();
1751 for (size_t k = 0; k < version_trees_.size(); ++k)
1752 delete version_trees_[k];
1753 this->version_trees_.clear();
1754 for (size_t k = 0; k < expression_lists_.size(); ++k)
1755 delete expression_lists_[k];
1756 this->expression_lists_.clear();
1759 std::vector<std::string>
1760 Version_script_info::get_versions() const
1762 std::vector<std::string> ret;
1763 for (size_t j = 0; j < version_trees_.size(); ++j)
1764 ret.push_back(version_trees_[j]->tag);
1768 std::vector<std::string>
1769 Version_script_info::get_dependencies(const char* version) const
1771 std::vector<std::string> ret;
1772 for (size_t j = 0; j < version_trees_.size(); ++j)
1773 if (version_trees_[j]->tag == version)
1775 const struct Version_dependency_list* deps =
1776 version_trees_[j]->dependencies;
1778 for (size_t k = 0; k < deps->dependencies.size(); ++k)
1779 ret.push_back(deps->dependencies[k]);
1786 Version_script_info::get_symbol_version_helper(const char* symbol_name,
1787 bool check_global) const
1789 for (size_t j = 0; j < version_trees_.size(); ++j)
1791 // Is it a global symbol for this version?
1792 const Version_expression_list* explist =
1793 check_global ? version_trees_[j]->global : version_trees_[j]->local;
1794 if (explist != NULL)
1795 for (size_t k = 0; k < explist->expressions.size(); ++k)
1797 const char* name_to_match = symbol_name;
1798 const struct Version_expression& exp = explist->expressions[k];
1799 char* demangled_name = NULL;
1800 if (exp.language == "C++")
1802 demangled_name = cplus_demangle(symbol_name,
1803 DMGL_ANSI | DMGL_PARAMS);
1804 // This isn't a C++ symbol.
1805 if (demangled_name == NULL)
1807 name_to_match = demangled_name;
1809 else if (exp.language == "Java")
1811 demangled_name = cplus_demangle(symbol_name,
1812 (DMGL_ANSI | DMGL_PARAMS
1814 // This isn't a Java symbol.
1815 if (demangled_name == NULL)
1817 name_to_match = demangled_name;
1820 if (exp.exact_match)
1821 matched = strcmp(exp.pattern.c_str(), name_to_match) == 0;
1823 matched = fnmatch(exp.pattern.c_str(), name_to_match,
1825 if (demangled_name != NULL)
1826 free(demangled_name);
1828 return version_trees_[j]->tag;
1831 static const std::string empty = "";
1835 struct Version_dependency_list*
1836 Version_script_info::allocate_dependency_list()
1838 dependency_lists_.push_back(new Version_dependency_list);
1839 return dependency_lists_.back();
1842 struct Version_expression_list*
1843 Version_script_info::allocate_expression_list()
1845 expression_lists_.push_back(new Version_expression_list);
1846 return expression_lists_.back();
1849 struct Version_tree*
1850 Version_script_info::allocate_version_tree()
1852 version_trees_.push_back(new Version_tree);
1853 return version_trees_.back();
1856 // Print for debugging.
1859 Version_script_info::print(FILE* f) const
1864 fprintf(f, "VERSION {");
1866 for (size_t i = 0; i < this->version_trees_.size(); ++i)
1868 const Version_tree* vt = this->version_trees_[i];
1870 if (vt->tag.empty())
1873 fprintf(f, " %s {\n", vt->tag.c_str());
1875 if (vt->global != NULL)
1877 fprintf(f, " global :\n");
1878 this->print_expression_list(f, vt->global);
1881 if (vt->local != NULL)
1883 fprintf(f, " local :\n");
1884 this->print_expression_list(f, vt->local);
1888 if (vt->dependencies != NULL)
1890 const Version_dependency_list* deps = vt->dependencies;
1891 for (size_t j = 0; j < deps->dependencies.size(); ++j)
1893 if (j < deps->dependencies.size() - 1)
1895 fprintf(f, " %s", deps->dependencies[j].c_str());
1905 Version_script_info::print_expression_list(
1907 const Version_expression_list* vel) const
1909 std::string current_language;
1910 for (size_t i = 0; i < vel->expressions.size(); ++i)
1912 const Version_expression& ve(vel->expressions[i]);
1914 if (ve.language != current_language)
1916 if (!current_language.empty())
1918 fprintf(f, " extern \"%s\" {\n", ve.language.c_str());
1919 current_language = ve.language;
1923 if (!current_language.empty())
1928 fprintf(f, "%s", ve.pattern.c_str());
1935 if (!current_language.empty())
1939 } // End namespace gold.
1941 // The remaining functions are extern "C", so it's clearer to not put
1942 // them in namespace gold.
1944 using namespace gold;
1946 // This function is called by the bison parser to return the next
1950 yylex(YYSTYPE* lvalp, void* closurev)
1952 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1953 const Token* token = closure->next_token();
1954 switch (token->classification())
1959 case Token::TOKEN_INVALID:
1960 yyerror(closurev, "invalid character");
1963 case Token::TOKEN_EOF:
1966 case Token::TOKEN_STRING:
1968 // This is either a keyword or a STRING.
1970 const char* str = token->string_value(&len);
1972 switch (closure->lex_mode())
1974 case Lex::LINKER_SCRIPT:
1975 parsecode = script_keywords.keyword_to_parsecode(str, len);
1977 case Lex::VERSION_SCRIPT:
1978 parsecode = version_script_keywords.keyword_to_parsecode(str, len);
1985 lvalp->string.value = str;
1986 lvalp->string.length = len;
1990 case Token::TOKEN_QUOTED_STRING:
1991 lvalp->string.value = token->string_value(&lvalp->string.length);
1992 return QUOTED_STRING;
1994 case Token::TOKEN_OPERATOR:
1995 return token->operator_value();
1997 case Token::TOKEN_INTEGER:
1998 lvalp->integer = token->integer_value();
2003 // This function is called by the bison parser to report an error.
2006 yyerror(void* closurev, const char* message)
2008 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2009 gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
2010 closure->charpos(), message);
2013 // Called by the bison parser to add a file to the link.
2016 script_add_file(void* closurev, const char* name, size_t length)
2018 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2020 // If this is an absolute path, and we found the script in the
2021 // sysroot, then we want to prepend the sysroot to the file name.
2022 // For example, this is how we handle a cross link to the x86_64
2023 // libc.so, which refers to /lib/libc.so.6.
2024 std::string name_string(name, length);
2025 const char* extra_search_path = ".";
2026 std::string script_directory;
2027 if (IS_ABSOLUTE_PATH(name_string.c_str()))
2029 if (closure->is_in_sysroot())
2031 const std::string& sysroot(parameters->sysroot());
2032 gold_assert(!sysroot.empty());
2033 name_string = sysroot + name_string;
2038 // In addition to checking the normal library search path, we
2039 // also want to check in the script-directory.
2040 const char *slash = strrchr(closure->filename(), '/');
2043 script_directory.assign(closure->filename(),
2044 slash - closure->filename() + 1);
2045 extra_search_path = script_directory.c_str();
2049 Input_file_argument file(name_string.c_str(), false, extra_search_path,
2050 false, closure->position_dependent_options());
2051 closure->inputs()->add_file(file);
2054 // Called by the bison parser to start a group. If we are already in
2055 // a group, that means that this script was invoked within a
2056 // --start-group --end-group sequence on the command line, or that
2057 // this script was found in a GROUP of another script. In that case,
2058 // we simply continue the existing group, rather than starting a new
2059 // one. It is possible to construct a case in which this will do
2060 // something other than what would happen if we did a recursive group,
2061 // but it's hard to imagine why the different behaviour would be
2062 // useful for a real program. Avoiding recursive groups is simpler
2063 // and more efficient.
2066 script_start_group(void* closurev)
2068 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2069 if (!closure->in_group())
2070 closure->inputs()->start_group();
2073 // Called by the bison parser at the end of a group.
2076 script_end_group(void* closurev)
2078 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2079 if (!closure->in_group())
2080 closure->inputs()->end_group();
2083 // Called by the bison parser to start an AS_NEEDED list.
2086 script_start_as_needed(void* closurev)
2088 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2089 closure->position_dependent_options().set_as_needed();
2092 // Called by the bison parser at the end of an AS_NEEDED list.
2095 script_end_as_needed(void* closurev)
2097 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2098 closure->position_dependent_options().clear_as_needed();
2101 // Called by the bison parser to set the entry symbol.
2104 script_set_entry(void* closurev, const char* entry, size_t length)
2106 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2107 closure->script_options()->set_entry(entry, length);
2110 // Called by the bison parser to define a symbol.
2113 script_set_symbol(void* closurev, const char* name, size_t length,
2114 Expression* value, int providei, int hiddeni)
2116 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2117 const bool provide = providei != 0;
2118 const bool hidden = hiddeni != 0;
2119 closure->script_options()->add_symbol_assignment(name, length, value,
2123 // Called by the bison parser to add an assertion.
2126 script_add_assertion(void* closurev, Expression* check, const char* message,
2129 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2130 closure->script_options()->add_assertion(check, message, messagelen);
2133 // Called by the bison parser to parse an OPTION.
2136 script_parse_option(void* closurev, const char* option, size_t length)
2138 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2139 // We treat the option as a single command-line option, even if
2140 // it has internal whitespace.
2141 if (closure->command_line() == NULL)
2143 // There are some options that we could handle here--e.g.,
2144 // -lLIBRARY. Should we bother?
2145 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
2146 " for scripts specified via -T/--script"),
2147 closure->filename(), closure->lineno(), closure->charpos());
2151 bool past_a_double_dash_option = false;
2152 char* mutable_option = strndup(option, length);
2153 gold_assert(mutable_option != NULL);
2154 closure->command_line()->process_one_option(1, &mutable_option, 0,
2155 &past_a_double_dash_option);
2156 free(mutable_option);
2160 // Called by the bison parser to handle SEARCH_DIR. This is handled
2161 // exactly like a -L option.
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());
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
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
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. */
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:
2213 // - tag is "GLIBC_2.1"
2214 // - tree contains the information "global: foo"
2215 // - deps contains "GLIBC_2.0"
2218 script_register_vers_node(void*,
2221 struct Version_tree *tree,
2222 struct Version_dependency_list *deps)
2224 gold_assert(tree != NULL);
2225 gold_assert(tag != NULL);
2226 tree->dependencies = deps;
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));
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)));
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();
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;
2292 // Handle a transition in language, such as at the
2293 // start or end of 'extern "C++"'
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));
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.
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.
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.
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,
2339 // Finish processing entries for an output section.
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.
2352 script_add_data(void* closurev, int data_token, Expression* val)
2354 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2356 bool is_signed = true;
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.
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
2394 script_add_input_section(void* closurev,
2395 const struct Input_section_spec* spec,
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
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)
2420 return script_new_string_sort_list(string_sort);
2423 pv->push_back(*string_sort);
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
2440 extern "C" String_list_ptr
2441 script_string_list_push_back(String_list_ptr pv, const char* str, size_t len)
2444 return script_new_string_list(str, len);
2447 pv->push_back(std::string(str, len));
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
2456 extern "C" String_list_ptr
2457 script_string_list_append(String_list_ptr pv1, String_list_ptr pv2)
2463 pv1->insert(pv1->end(), pv2->begin(), pv2->end());
2467 // Add a new program header.
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 }
2491 } phdr_type_names[] =
2495 PHDR_TYPE(PT_DYNAMIC),
2496 PHDR_TYPE(PT_INTERP),
2498 PHDR_TYPE(PT_SHLIB),
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]);
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;