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.
29 #include "filenames.h"
32 #include "dirsearch.h"
35 #include "workqueue.h"
37 #include "parameters.h"
46 // A token read from a script file. We don't implement keywords here;
47 // all keywords are simply represented as a string.
52 // Token classification.
57 // Token indicates end of input.
59 // Token is a string of characters.
61 // Token is a quoted string of characters.
63 // Token is an operator.
65 // Token is a number (an integer).
69 // We need an empty constructor so that we can put this STL objects.
71 : classification_(TOKEN_INVALID), value_(NULL), value_length_(0),
72 opcode_(0), lineno_(0), charpos_(0)
75 // A general token with no value.
76 Token(Classification classification, int lineno, int charpos)
77 : classification_(classification), value_(NULL), value_length_(0),
78 opcode_(0), lineno_(lineno), charpos_(charpos)
80 gold_assert(classification == TOKEN_INVALID
81 || classification == TOKEN_EOF);
84 // A general token with a value.
85 Token(Classification classification, const char* value, size_t length,
86 int lineno, int charpos)
87 : classification_(classification), value_(value), value_length_(length),
88 opcode_(0), lineno_(lineno), charpos_(charpos)
90 gold_assert(classification != TOKEN_INVALID
91 && classification != TOKEN_EOF);
94 // A token representing an operator.
95 Token(int opcode, int lineno, int charpos)
96 : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0),
97 opcode_(opcode), lineno_(lineno), charpos_(charpos)
100 // Return whether the token is invalid.
103 { return this->classification_ == TOKEN_INVALID; }
105 // Return whether this is an EOF token.
108 { return this->classification_ == TOKEN_EOF; }
110 // Return the token classification.
112 classification() const
113 { return this->classification_; }
115 // Return the line number at which the token starts.
118 { return this->lineno_; }
120 // Return the character position at this the token starts.
123 { return this->charpos_; }
125 // Get the value of a token.
128 string_value(size_t* length) const
130 gold_assert(this->classification_ == TOKEN_STRING
131 || this->classification_ == TOKEN_QUOTED_STRING);
132 *length = this->value_length_;
137 operator_value() const
139 gold_assert(this->classification_ == TOKEN_OPERATOR);
140 return this->opcode_;
144 integer_value() const
146 gold_assert(this->classification_ == TOKEN_INTEGER);
148 std::string s(this->value_, this->value_length_);
149 return strtoull(s.c_str(), NULL, 0);
153 // The token classification.
154 Classification classification_;
155 // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or
158 // The length of the token value.
159 size_t value_length_;
160 // The token value, for TOKEN_OPERATOR.
162 // The line number where this token started (one based).
164 // The character position within the line where this token started
169 // This class handles lexing a file into a sequence of tokens.
174 // We unfortunately have to support different lexing modes, because
175 // when reading different parts of a linker script we need to parse
176 // things differently.
179 // Reading an ordinary linker script.
181 // Reading an expression in a linker script.
183 // Reading a version script.
187 Lex(const char* input_string, size_t input_length, int parsing_token)
188 : input_string_(input_string), input_length_(input_length),
189 current_(input_string), mode_(LINKER_SCRIPT),
190 first_token_(parsing_token), token_(),
191 lineno_(1), linestart_(input_string)
194 // Read a file into a string.
196 read_file(Input_file*, std::string*);
198 // Return the next token.
202 // Return the current lexing mode.
205 { return this->mode_; }
207 // Set the lexing mode.
210 { this->mode_ = mode; }
214 Lex& operator=(const Lex&);
216 // Make a general token with no value at the current location.
218 make_token(Token::Classification c, const char* start) const
219 { return Token(c, this->lineno_, start - this->linestart_ + 1); }
221 // Make a general token with a value at the current location.
223 make_token(Token::Classification c, const char* v, size_t len,
226 { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); }
228 // Make an operator token at the current location.
230 make_token(int opcode, const char* start) const
231 { return Token(opcode, this->lineno_, start - this->linestart_ + 1); }
233 // Make an invalid token at the current location.
235 make_invalid_token(const char* start)
236 { return this->make_token(Token::TOKEN_INVALID, start); }
238 // Make an EOF token at the current location.
240 make_eof_token(const char* start)
241 { return this->make_token(Token::TOKEN_EOF, start); }
243 // Return whether C can be the first character in a name. C2 is the
244 // next character, since we sometimes need that.
246 can_start_name(char c, char c2);
248 // Return whether C can appear in a name which has already started.
250 can_continue_name(char c);
252 // Return whether C, C2, C3 can start a hex number.
254 can_start_hex(char c, char c2, char c3);
256 // Return whether C can appear in a hex number.
258 can_continue_hex(char c);
260 // Return whether C can start a non-hex number.
262 can_start_number(char c);
264 // Return whether C can appear in a non-hex number.
266 can_continue_number(char c)
267 { return Lex::can_start_number(c); }
269 // If C1 C2 C3 form a valid three character operator, return the
270 // opcode. Otherwise return 0.
272 three_char_operator(char c1, char c2, char c3);
274 // If C1 C2 form a valid two character operator, return the opcode.
275 // Otherwise return 0.
277 two_char_operator(char c1, char c2);
279 // If C1 is a valid one character operator, return the opcode.
280 // Otherwise return 0.
282 one_char_operator(char c1);
284 // Read the next token.
286 get_token(const char**);
288 // Skip a C style /* */ comment. Return false if the comment did
291 skip_c_comment(const char**);
293 // Skip a line # comment. Return false if there was no newline.
295 skip_line_comment(const char**);
297 // Build a token CLASSIFICATION from all characters that match
298 // CAN_CONTINUE_FN. The token starts at START. Start matching from
299 // MATCH. Set *PP to the character following the token.
301 gather_token(Token::Classification,
302 bool (Lex::*can_continue_fn)(char),
303 const char* start, const char* match, const char** pp);
305 // Build a token from a quoted string.
307 gather_quoted_string(const char** pp);
309 // The string we are tokenizing.
310 const char* input_string_;
311 // The length of the string.
312 size_t input_length_;
313 // The current offset into the string.
314 const char* current_;
315 // The current lexing mode.
317 // The code to use for the first token. This is set to 0 after it
320 // The current token.
322 // The current line number.
324 // The start of the current line in the string.
325 const char* linestart_;
328 // Read the whole file into memory. We don't expect linker scripts to
329 // be large, so we just use a std::string as a buffer. We ignore the
330 // data we've already read, so that we read aligned buffers.
333 Lex::read_file(Input_file* input_file, std::string* contents)
335 off_t filesize = input_file->file().filesize();
337 contents->reserve(filesize);
340 unsigned char buf[BUFSIZ];
341 while (off < filesize)
344 if (get > filesize - off)
345 get = filesize - off;
346 input_file->file().read(off, get, buf);
347 contents->append(reinterpret_cast<char*>(&buf[0]), get);
352 // Return whether C can be the start of a name, if the next character
353 // is C2. A name can being with a letter, underscore, period, or
354 // dollar sign. Because a name can be a file name, we also permit
355 // forward slash, backslash, and tilde. Tilde is the tricky case
356 // here; GNU ld also uses it as a bitwise not operator. It is only
357 // recognized as the operator if it is not immediately followed by
358 // some character which can appear in a symbol. That is, when we
359 // don't know that we are looking at an expression, "~0" is a file
360 // name, and "~ 0" is an expression using bitwise not. We are
364 Lex::can_start_name(char c, char c2)
368 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
369 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
370 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
371 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
373 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
374 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
375 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
376 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
378 case '_': case '.': case '$':
382 return this->mode_ == LINKER_SCRIPT;
385 return this->mode_ == LINKER_SCRIPT && can_continue_name(c2);
392 // Return whether C can continue a name which has already started.
393 // Subsequent characters in a name are the same as the leading
394 // characters, plus digits and "=+-:[],?*". So in general the linker
395 // script language requires spaces around operators, unless we know
396 // that we are parsing an expression.
399 Lex::can_continue_name(char c)
403 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
404 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
405 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
406 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
408 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
409 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
410 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
411 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
413 case '_': case '.': case '$':
414 case '0': case '1': case '2': case '3': case '4':
415 case '5': case '6': case '7': case '8': case '9':
418 case '/': case '\\': case '~':
419 case '=': case '+': case '-':
420 case ':': case '[': case ']':
421 case ',': case '?': case '*':
422 return this->mode_ == LINKER_SCRIPT;
429 // For a number we accept 0x followed by hex digits, or any sequence
430 // of digits. The old linker accepts leading '$' for hex, and
431 // trailing HXBOD. Those are for MRI compatibility and we don't
432 // accept them. The old linker also accepts trailing MK for mega or
433 // kilo. FIXME: Those are mentioned in the documentation, and we
434 // should accept them.
436 // Return whether C1 C2 C3 can start a hex number.
439 Lex::can_start_hex(char c1, char c2, char c3)
441 if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
442 return this->can_continue_hex(c3);
446 // Return whether C can appear in a hex number.
449 Lex::can_continue_hex(char c)
453 case '0': case '1': case '2': case '3': case '4':
454 case '5': case '6': case '7': case '8': case '9':
455 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
456 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
464 // Return whether C can start a non-hex number.
467 Lex::can_start_number(char c)
471 case '0': case '1': case '2': case '3': case '4':
472 case '5': case '6': case '7': case '8': case '9':
480 // If C1 C2 C3 form a valid three character operator, return the
481 // opcode (defined in the yyscript.h file generated from yyscript.y).
482 // Otherwise return 0.
485 Lex::three_char_operator(char c1, char c2, char c3)
490 if (c2 == '<' && c3 == '=')
494 if (c2 == '>' && c3 == '=')
503 // If C1 C2 form a valid two character operator, return the opcode
504 // (defined in the yyscript.h file generated from yyscript.y).
505 // Otherwise return 0.
508 Lex::two_char_operator(char c1, char c2)
566 // If C1 is a valid operator, return the opcode. Otherwise return 0.
569 Lex::one_char_operator(char c1)
602 // Skip a C style comment. *PP points to just after the "/*". Return
603 // false if the comment did not end.
606 Lex::skip_c_comment(const char** pp)
609 while (p[0] != '*' || p[1] != '/')
620 this->linestart_ = p + 1;
629 // Skip a line # comment. Return false if there was no newline.
632 Lex::skip_line_comment(const char** pp)
635 size_t skip = strcspn(p, "\n");
644 this->linestart_ = p;
650 // Build a token CLASSIFICATION from all characters that match
651 // CAN_CONTINUE_FN. Update *PP.
654 Lex::gather_token(Token::Classification classification,
655 bool (Lex::*can_continue_fn)(char),
660 while ((this->*can_continue_fn)(*match))
663 return this->make_token(classification, start, match - start, start);
666 // Build a token from a quoted string.
669 Lex::gather_quoted_string(const char** pp)
671 const char* start = *pp;
672 const char* p = start;
674 size_t skip = strcspn(p, "\"\n");
676 return this->make_invalid_token(start);
678 return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
681 // Return the next token at *PP. Update *PP. General guideline: we
682 // require linker scripts to be simple ASCII. No unicode linker
683 // scripts. In particular we can assume that any '\0' is the end of
687 Lex::get_token(const char** pp)
696 return this->make_eof_token(p);
699 // Skip whitespace quickly.
700 while (*p == ' ' || *p == '\t')
707 this->linestart_ = p;
711 // Skip C style comments.
712 if (p[0] == '/' && p[1] == '*')
714 int lineno = this->lineno_;
715 int charpos = p - this->linestart_ + 1;
718 if (!this->skip_c_comment(pp))
719 return Token(Token::TOKEN_INVALID, lineno, charpos);
725 // Skip line comments.
729 if (!this->skip_line_comment(pp))
730 return this->make_eof_token(p);
736 if (this->can_start_name(p[0], p[1]))
737 return this->gather_token(Token::TOKEN_STRING,
738 &Lex::can_continue_name,
741 // We accept any arbitrary name in double quotes, as long as it
742 // does not cross a line boundary.
746 return this->gather_quoted_string(pp);
749 // Check for a number.
751 if (this->can_start_hex(p[0], p[1], p[2]))
752 return this->gather_token(Token::TOKEN_INTEGER,
753 &Lex::can_continue_hex,
756 if (Lex::can_start_number(p[0]))
757 return this->gather_token(Token::TOKEN_INTEGER,
758 &Lex::can_continue_number,
761 // Check for operators.
763 int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
767 return this->make_token(opcode, p);
770 opcode = Lex::two_char_operator(p[0], p[1]);
774 return this->make_token(opcode, p);
777 opcode = Lex::one_char_operator(p[0]);
781 return this->make_token(opcode, p);
784 return this->make_token(Token::TOKEN_INVALID, p);
788 // Return the next token.
793 // The first token is special.
794 if (this->first_token_ != 0)
796 this->token_ = Token(this->first_token_, 0, 0);
797 this->first_token_ = 0;
798 return &this->token_;
801 this->token_ = this->get_token(&this->current_);
803 // Don't let an early null byte fool us into thinking that we've
804 // reached the end of the file.
805 if (this->token_.is_eof()
806 && (static_cast<size_t>(this->current_ - this->input_string_)
807 < this->input_length_))
808 this->token_ = this->make_invalid_token(this->current_);
810 return &this->token_;
813 // A trivial task which waits for THIS_BLOCKER to be clear and then
814 // clears NEXT_BLOCKER. THIS_BLOCKER may be NULL.
816 class Script_unblock : public Task
819 Script_unblock(Task_token* this_blocker, Task_token* next_blocker)
820 : this_blocker_(this_blocker), next_blocker_(next_blocker)
825 if (this->this_blocker_ != NULL)
826 delete this->this_blocker_;
832 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
833 return this->this_blocker_;
838 locks(Task_locker* tl)
839 { tl->add(this, this->next_blocker_); }
847 { return "Script_unblock"; }
850 Task_token* this_blocker_;
851 Task_token* next_blocker_;
854 // Class Script_options.
856 Script_options::Script_options()
857 : entry_(), symbol_assignments_()
861 // Add any symbols we are defining to the symbol table.
864 Script_options::add_symbols_to_table(Symbol_table* symtab,
865 const Target* target)
867 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
868 p != this->symbol_assignments_.end();
871 elfcpp::STV vis = p->hidden ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
872 p->sym = symtab->define_as_constant(target,
885 // Finalize symbol values.
888 Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
890 if (parameters->get_size() == 32)
892 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
893 this->sized_finalize_symbols<32>(symtab, layout);
898 else if (parameters->get_size() == 64)
900 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
901 this->sized_finalize_symbols<64>(symtab, layout);
912 Script_options::sized_finalize_symbols(Symbol_table* symtab,
913 const Layout* layout)
915 for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
916 p != this->symbol_assignments_.end();
921 Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(p->sym);
922 ssym->set_value(p->value->eval(symtab, layout));
927 // This class holds data passed through the parser to the lexer and to
928 // the parser support functions. This avoids global variables. We
929 // can't use global variables because we need not be called by a
935 Parser_closure(const char* filename,
936 const Position_dependent_options& posdep_options,
937 bool in_group, bool is_in_sysroot,
938 Command_line* command_line,
939 Script_options* script_options,
941 : filename_(filename), posdep_options_(posdep_options),
942 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
943 command_line_(command_line), script_options_(script_options),
944 lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL)
947 // Return the file name.
950 { return this->filename_; }
952 // Return the position dependent options. The caller may modify
954 Position_dependent_options&
955 position_dependent_options()
956 { return this->posdep_options_; }
958 // Return whether this script is being run in a group.
961 { return this->in_group_; }
963 // Return whether this script was found using a directory in the
966 is_in_sysroot() const
967 { return this->is_in_sysroot_; }
969 // Returns the Command_line structure passed in at constructor time.
970 // This value may be NULL. The caller may modify this, which modifies
971 // the passed-in Command_line object (not a copy).
974 { return this->command_line_; }
976 // Return the options which may be set by a script.
979 { return this->script_options_; }
981 // Return the next token, and advance.
985 const Token* token = this->lex_->next_token();
986 this->lineno_ = token->lineno();
987 this->charpos_ = token->charpos();
991 // Set a new lexer mode, pushing the current one.
993 push_lex_mode(Lex::Mode mode)
995 this->lex_mode_stack_.push_back(this->lex_->mode());
996 this->lex_->set_mode(mode);
999 // Pop the lexer mode.
1003 gold_assert(!this->lex_mode_stack_.empty());
1004 this->lex_->set_mode(this->lex_mode_stack_.back());
1005 this->lex_mode_stack_.pop_back();
1008 // Return the line number of the last token.
1011 { return this->lineno_; }
1013 // Return the character position in the line of the last token.
1016 { return this->charpos_; }
1018 // Return the list of input files, creating it if necessary. This
1019 // is a space leak--we never free the INPUTS_ pointer.
1023 if (this->inputs_ == NULL)
1024 this->inputs_ = new Input_arguments();
1025 return this->inputs_;
1028 // Return whether we saw any input files.
1031 { return this->inputs_ != NULL && !this->inputs_->empty(); }
1034 // The name of the file we are reading.
1035 const char* filename_;
1036 // The position dependent options.
1037 Position_dependent_options posdep_options_;
1038 // Whether we are currently in a --start-group/--end-group.
1040 // Whether the script was found in a sysrooted directory.
1041 bool is_in_sysroot_;
1042 // May be NULL if the user chooses not to pass one in.
1043 Command_line* command_line_;
1044 // Options which may be set from any linker script.
1045 Script_options* script_options_;
1048 // The line number of the last token returned by next_token.
1050 // The column number of the last token returned by next_token.
1052 // A stack of lexer modes.
1053 std::vector<Lex::Mode> lex_mode_stack_;
1054 // New input files found to add to the link.
1055 Input_arguments* inputs_;
1058 // FILE was found as an argument on the command line. Try to read it
1059 // as a script. We've already read BYTES of data into P, but we
1060 // ignore that. Return true if the file was handled.
1063 read_input_script(Workqueue* workqueue, const General_options& options,
1064 Symbol_table* symtab, Layout* layout,
1065 Dirsearch* dirsearch, Input_objects* input_objects,
1066 Input_group* input_group,
1067 const Input_argument* input_argument,
1068 Input_file* input_file, const unsigned char*, off_t,
1069 Task_token* this_blocker, Task_token* next_blocker)
1071 std::string input_string;
1072 Lex::read_file(input_file, &input_string);
1074 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1076 Parser_closure closure(input_file->filename().c_str(),
1077 input_argument->file().options(),
1078 input_group != NULL,
1079 input_file->is_in_sysroot(),
1081 layout->script_options(),
1084 if (yyparse(&closure) != 0)
1087 // THIS_BLOCKER must be clear before we may add anything to the
1088 // symbol table. We are responsible for unblocking NEXT_BLOCKER
1089 // when we are done. We are responsible for deleting THIS_BLOCKER
1090 // when it is unblocked.
1092 if (!closure.saw_inputs())
1094 // The script did not add any files to read. Note that we are
1095 // not permitted to call NEXT_BLOCKER->unblock() here even if
1096 // THIS_BLOCKER is NULL, as we do not hold the workqueue lock.
1097 workqueue->queue(new Script_unblock(this_blocker, next_blocker));
1101 for (Input_arguments::const_iterator p = closure.inputs()->begin();
1102 p != closure.inputs()->end();
1106 if (p + 1 == closure.inputs()->end())
1110 nb = new Task_token(true);
1113 workqueue->queue(new Read_symbols(options, input_objects, symtab,
1114 layout, dirsearch, &*p,
1115 input_group, this_blocker, nb));
1122 // FILENAME was found as an argument to --script (-T).
1123 // Read it as a script, and execute its contents immediately.
1126 read_commandline_script(const char* filename, Command_line* cmdline)
1128 // TODO: if filename is a relative filename, search for it manually
1129 // using "." + cmdline->options()->search_path() -- not dirsearch.
1130 Dirsearch dirsearch;
1132 // The file locking code wants to record a Task, but we haven't
1133 // started the workqueue yet. This is only for debugging purposes,
1134 // so we invent a fake value.
1135 const Task* task = reinterpret_cast<const Task*>(-1);
1137 Input_file_argument input_argument(filename, false, "",
1138 cmdline->position_dependent_options());
1139 Input_file input_file(&input_argument);
1140 if (!input_file.open(cmdline->options(), dirsearch, task))
1143 std::string input_string;
1144 Lex::read_file(&input_file, &input_string);
1146 Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1148 Parser_closure closure(filename,
1149 cmdline->position_dependent_options(),
1151 input_file.is_in_sysroot(),
1153 cmdline->script_options(),
1155 if (yyparse(&closure) != 0)
1157 input_file.file().unlock(task);
1161 input_file.file().unlock(task);
1163 gold_assert(!closure.saw_inputs());
1168 // Implement the --defsym option on the command line. Return true if
1172 Script_options::define_symbol(const char* definition)
1174 Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1175 lex.set_mode(Lex::EXPRESSION);
1178 Position_dependent_options posdep_options;
1180 Parser_closure closure("command line", posdep_options, false, false, NULL,
1183 if (yyparse(&closure) != 0)
1186 gold_assert(!closure.saw_inputs());
1191 // Manage mapping from keywords to the codes expected by the bison
1194 class Keyword_to_parsecode
1197 // The structure which maps keywords to parsecodes.
1198 struct Keyword_parsecode
1201 const char* keyword;
1202 // Corresponding parsecode.
1206 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1209 keyword_to_parsecode(const char* keyword, size_t len);
1212 // The array of all keywords.
1213 static const Keyword_parsecode keyword_parsecodes_[];
1215 // The number of keywords.
1216 static const int keyword_count;
1219 // Mapping from keyword string to keyword parsecode. This array must
1220 // be kept in sorted order. Parsecodes are looked up using bsearch.
1221 // This array must correspond to the list of parsecodes in yyscript.y.
1223 const Keyword_to_parsecode::Keyword_parsecode
1224 Keyword_to_parsecode::keyword_parsecodes_[] =
1226 { "ABSOLUTE", ABSOLUTE },
1228 { "ALIGN", ALIGN_K },
1229 { "ALIGNOF", ALIGNOF },
1230 { "ASSERT", ASSERT_K },
1231 { "AS_NEEDED", AS_NEEDED },
1236 { "CONSTANT", CONSTANT },
1237 { "CONSTRUCTORS", CONSTRUCTORS },
1239 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1240 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1241 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1242 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1243 { "DEFINED", DEFINED },
1246 { "EXCLUDE_FILE", EXCLUDE_FILE },
1247 { "EXTERN", EXTERN },
1250 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1253 { "INCLUDE", INCLUDE },
1255 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1258 { "LENGTH", LENGTH },
1259 { "LOADADDR", LOADADDR },
1263 { "MEMORY", MEMORY },
1266 { "NOCROSSREFS", NOCROSSREFS },
1267 { "NOFLOAT", NOFLOAT },
1268 { "NOLOAD", NOLOAD },
1269 { "ONLY_IF_RO", ONLY_IF_RO },
1270 { "ONLY_IF_RW", ONLY_IF_RW },
1271 { "OPTION", OPTION },
1272 { "ORIGIN", ORIGIN },
1273 { "OUTPUT", OUTPUT },
1274 { "OUTPUT_ARCH", OUTPUT_ARCH },
1275 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1276 { "OVERLAY", OVERLAY },
1278 { "PROVIDE", PROVIDE },
1279 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1281 { "SEARCH_DIR", SEARCH_DIR },
1282 { "SECTIONS", SECTIONS },
1283 { "SEGMENT_START", SEGMENT_START },
1285 { "SIZEOF", SIZEOF },
1286 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1287 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1288 { "SORT_BY_NAME", SORT_BY_NAME },
1289 { "SPECIAL", SPECIAL },
1291 { "STARTUP", STARTUP },
1292 { "SUBALIGN", SUBALIGN },
1293 { "SYSLIB", SYSLIB },
1294 { "TARGET", TARGET_K },
1295 { "TRUNCATE", TRUNCATE },
1296 { "VERSION", VERSIONK },
1297 { "global", GLOBAL },
1303 { "sizeof_headers", SIZEOF_HEADERS },
1306 const int Keyword_to_parsecode::keyword_count =
1307 (sizeof(Keyword_to_parsecode::keyword_parsecodes_)
1308 / sizeof(Keyword_to_parsecode::keyword_parsecodes_[0]));
1310 // Comparison function passed to bsearch.
1322 ktt_compare(const void* keyv, const void* kttv)
1324 const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
1325 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1326 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1327 int i = strncmp(key->str, ktt->keyword, key->len);
1330 if (ktt->keyword[key->len] != '\0')
1335 } // End extern "C".
1338 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword, size_t len)
1343 void* kttv = bsearch(&key,
1344 Keyword_to_parsecode::keyword_parsecodes_,
1345 Keyword_to_parsecode::keyword_count,
1346 sizeof(Keyword_to_parsecode::keyword_parsecodes_[0]),
1350 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1351 return ktt->parsecode;
1354 } // End namespace gold.
1356 // The remaining functions are extern "C", so it's clearer to not put
1357 // them in namespace gold.
1359 using namespace gold;
1361 // This function is called by the bison parser to return the next
1365 yylex(YYSTYPE* lvalp, void* closurev)
1367 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1368 const Token* token = closure->next_token();
1369 switch (token->classification())
1374 case Token::TOKEN_INVALID:
1375 yyerror(closurev, "invalid character");
1378 case Token::TOKEN_EOF:
1381 case Token::TOKEN_STRING:
1383 // This is either a keyword or a STRING.
1385 const char* str = token->string_value(&len);
1386 int parsecode = Keyword_to_parsecode::keyword_to_parsecode(str, len);
1389 lvalp->string.value = str;
1390 lvalp->string.length = len;
1394 case Token::TOKEN_QUOTED_STRING:
1395 lvalp->string.value = token->string_value(&lvalp->string.length);
1398 case Token::TOKEN_OPERATOR:
1399 return token->operator_value();
1401 case Token::TOKEN_INTEGER:
1402 lvalp->integer = token->integer_value();
1407 // This function is called by the bison parser to report an error.
1410 yyerror(void* closurev, const char* message)
1412 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1413 gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
1414 closure->charpos(), message);
1417 // Called by the bison parser to add a file to the link.
1420 script_add_file(void* closurev, const char* name, size_t length)
1422 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1424 // If this is an absolute path, and we found the script in the
1425 // sysroot, then we want to prepend the sysroot to the file name.
1426 // For example, this is how we handle a cross link to the x86_64
1427 // libc.so, which refers to /lib/libc.so.6.
1428 std::string name_string(name, length);
1429 const char* extra_search_path = ".";
1430 std::string script_directory;
1431 if (IS_ABSOLUTE_PATH(name_string.c_str()))
1433 if (closure->is_in_sysroot())
1435 const std::string& sysroot(parameters->sysroot());
1436 gold_assert(!sysroot.empty());
1437 name_string = sysroot + name_string;
1442 // In addition to checking the normal library search path, we
1443 // also want to check in the script-directory.
1444 const char *slash = strrchr(closure->filename(), '/');
1447 script_directory.assign(closure->filename(),
1448 slash - closure->filename() + 1);
1449 extra_search_path = script_directory.c_str();
1453 Input_file_argument file(name_string.c_str(), false, extra_search_path,
1454 closure->position_dependent_options());
1455 closure->inputs()->add_file(file);
1458 // Called by the bison parser to start a group. If we are already in
1459 // a group, that means that this script was invoked within a
1460 // --start-group --end-group sequence on the command line, or that
1461 // this script was found in a GROUP of another script. In that case,
1462 // we simply continue the existing group, rather than starting a new
1463 // one. It is possible to construct a case in which this will do
1464 // something other than what would happen if we did a recursive group,
1465 // but it's hard to imagine why the different behaviour would be
1466 // useful for a real program. Avoiding recursive groups is simpler
1467 // and more efficient.
1470 script_start_group(void* closurev)
1472 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1473 if (!closure->in_group())
1474 closure->inputs()->start_group();
1477 // Called by the bison parser at the end of a group.
1480 script_end_group(void* closurev)
1482 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1483 if (!closure->in_group())
1484 closure->inputs()->end_group();
1487 // Called by the bison parser to start an AS_NEEDED list.
1490 script_start_as_needed(void* closurev)
1492 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1493 closure->position_dependent_options().set_as_needed();
1496 // Called by the bison parser at the end of an AS_NEEDED list.
1499 script_end_as_needed(void* closurev)
1501 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1502 closure->position_dependent_options().clear_as_needed();
1505 // Called by the bison parser to set the entry symbol.
1508 script_set_entry(void* closurev, const char* entry, size_t length)
1510 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1511 closure->script_options()->set_entry(entry, length);
1514 // Called by the bison parser to define a symbol.
1517 script_set_symbol(void* closurev, const char* name, size_t length,
1518 Expression* value, int providei, int hiddeni)
1520 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1521 const bool provide = providei != 0;
1522 const bool hidden = hiddeni != 0;
1523 closure->script_options()->add_symbol_assignment(name, length, value,
1527 // Called by the bison parser to parse an OPTION.
1530 script_parse_option(void* closurev, const char* option, size_t length)
1532 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1533 // We treat the option as a single command-line option, even if
1534 // it has internal whitespace.
1535 if (closure->command_line() == NULL)
1537 // There are some options that we could handle here--e.g.,
1538 // -lLIBRARY. Should we bother?
1539 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
1540 " for scripts specified via -T/--script"),
1541 closure->filename(), closure->lineno(), closure->charpos());
1545 bool past_a_double_dash_option = false;
1546 char* mutable_option = strndup(option, length);
1547 gold_assert(mutable_option != NULL);
1548 closure->command_line()->process_one_option(1, &mutable_option, 0,
1549 &past_a_double_dash_option);
1550 free(mutable_option);
1554 /* Called by the bison parser to push the lexer into expression
1558 script_push_lex_into_expression_mode(void* closurev)
1560 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1561 closure->push_lex_mode(Lex::EXPRESSION);
1564 /* Called by the bison parser to pop the lexer mode. */
1567 script_pop_lex_mode(void* closurev)
1569 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1570 closure->pop_lex_mode();