1 // script.cc -- handle linker scripts for gold.
3 // Copyright 2006, 2007 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"
31 #include "dirsearch.h"
34 #include "workqueue.h"
36 #include "parameters.h"
45 // A token read from a script file. We don't implement keywords here;
46 // all keywords are simply represented as a string.
51 // Token classification.
56 // Token indicates end of input.
58 // Token is a string of characters.
60 // Token is an operator.
62 // Token is a number (an integer).
66 // We need an empty constructor so that we can put this STL objects.
68 : classification_(TOKEN_INVALID), value_(), opcode_(0),
69 lineno_(0), charpos_(0)
72 // A general token with no value.
73 Token(Classification classification, int lineno, int charpos)
74 : classification_(classification), value_(), opcode_(0),
75 lineno_(lineno), charpos_(charpos)
77 gold_assert(classification == TOKEN_INVALID
78 || classification == TOKEN_EOF);
81 // A general token with a value.
82 Token(Classification classification, const std::string& value,
83 int lineno, int charpos)
84 : classification_(classification), value_(value), opcode_(0),
85 lineno_(lineno), charpos_(charpos)
87 gold_assert(classification != TOKEN_INVALID
88 && classification != TOKEN_EOF);
91 // A token representing a string of characters.
92 Token(const std::string& s, int lineno, int charpos)
93 : classification_(TOKEN_STRING), value_(s), opcode_(0),
94 lineno_(lineno), charpos_(charpos)
97 // A token representing an operator.
98 Token(int opcode, int lineno, int charpos)
99 : classification_(TOKEN_OPERATOR), value_(), opcode_(opcode),
100 lineno_(lineno), charpos_(charpos)
103 // Return whether the token is invalid.
106 { return this->classification_ == TOKEN_INVALID; }
108 // Return whether this is an EOF token.
111 { return this->classification_ == TOKEN_EOF; }
113 // Return the token classification.
115 classification() const
116 { return this->classification_; }
118 // Return the line number at which the token starts.
121 { return this->lineno_; }
123 // Return the character position at this the token starts.
126 { return this->charpos_; }
128 // Get the value of a token.
133 gold_assert(this->classification_ == TOKEN_STRING);
138 operator_value() const
140 gold_assert(this->classification_ == TOKEN_OPERATOR);
141 return this->opcode_;
145 integer_value() const
147 gold_assert(this->classification_ == TOKEN_INTEGER);
148 return strtoll(this->value_.c_str(), NULL, 0);
152 // The token classification.
153 Classification classification_;
154 // The token value, for TOKEN_STRING or TOKEN_INTEGER.
156 // The token value, for TOKEN_OPERATOR.
158 // The line number where this token started (one based).
160 // The character position within the line where this token started
165 // This class handles lexing a file into a sequence of tokens. We
166 // don't expect linker scripts to be large, so we just read them and
167 // tokenize them all at once.
172 Lex(Input_file* input_file)
173 : input_file_(input_file), tokens_()
176 // Tokenize the file. Return the final token, which will be either
177 // an invalid token or an EOF token. An invalid token indicates
178 // that tokenization failed.
183 typedef std::vector<Token> Token_sequence;
185 // Return the tokens.
186 const Token_sequence&
188 { return this->tokens_; }
192 Lex& operator=(const Lex&);
194 // Read the file into a string buffer.
196 read_file(std::string*);
198 // Make a general token with no value at the current location.
200 make_token(Token::Classification c, const char* p) const
201 { return Token(c, this->lineno_, p - this->linestart_ + 1); }
203 // Make a general token with a value at the current location.
205 make_token(Token::Classification c, const std::string& v, const char* p)
207 { return Token(c, v, this->lineno_, p - this->linestart_ + 1); }
209 // Make an operator token at the current location.
211 make_token(int opcode, const char* p) const
212 { return Token(opcode, this->lineno_, p - this->linestart_ + 1); }
214 // Make an invalid token at the current location.
216 make_invalid_token(const char* p)
217 { return this->make_token(Token::TOKEN_INVALID, p); }
219 // Make an EOF token at the current location.
221 make_eof_token(const char* p)
222 { return this->make_token(Token::TOKEN_EOF, p); }
224 // Return whether C can be the first character in a name. C2 is the
225 // next character, since we sometimes need that.
227 can_start_name(char c, char c2);
229 // Return whether C can appear in a name which has already started.
231 can_continue_name(char c);
233 // Return whether C, C2, C3 can start a hex number.
235 can_start_hex(char c, char c2, char c3);
237 // Return whether C can appear in a hex number.
239 can_continue_hex(char c);
241 // Return whether C can start a non-hex number.
243 can_start_number(char c);
245 // Return whether C can appear in a non-hex number.
247 can_continue_number(char c)
248 { return Lex::can_start_number(c); }
250 // If C1 C2 C3 form a valid three character operator, return the
251 // opcode. Otherwise return 0.
253 three_char_operator(char c1, char c2, char c3);
255 // If C1 C2 form a valid two character operator, return the opcode.
256 // Otherwise return 0.
258 two_char_operator(char c1, char c2);
260 // If C1 is a valid one character operator, return the opcode.
261 // Otherwise return 0.
263 one_char_operator(char c1);
265 // Read the next token.
267 get_token(const char**);
269 // Skip a C style /* */ comment. Return false if the comment did
272 skip_c_comment(const char**);
274 // Skip a line # comment. Return false if there was no newline.
276 skip_line_comment(const char**);
278 // Build a token CLASSIFICATION from all characters that match
279 // CAN_CONTINUE_FN. The token starts at START. Start matching from
280 // MATCH. Set *PP to the character following the token.
282 gather_token(Token::Classification, bool (*can_continue_fn)(char),
283 const char* start, const char* match, const char** pp);
285 // Build a token from a quoted string.
287 gather_quoted_string(const char** pp);
289 // The file we are reading.
290 Input_file* input_file_;
291 // The token sequence we create.
292 Token_sequence tokens_;
293 // The current line number.
295 // The start of the current line in the buffer.
296 const char* linestart_;
299 // Read the whole file into memory. We don't expect linker scripts to
300 // be large, so we just use a std::string as a buffer. We ignore the
301 // data we've already read, so that we read aligned buffers.
304 Lex::read_file(std::string* contents)
306 off_t filesize = this->input_file_->file().filesize();
308 contents->reserve(filesize);
311 unsigned char buf[BUFSIZ];
312 while (off < filesize)
315 if (get > filesize - off)
316 get = filesize - off;
317 this->input_file_->file().read(off, get, buf);
318 contents->append(reinterpret_cast<char*>(&buf[0]), get);
323 // Return whether C can be the start of a name, if the next character
324 // is C2. A name can being with a letter, underscore, period, or
325 // dollar sign. Because a name can be a file name, we also permit
326 // forward slash, backslash, and tilde. Tilde is the tricky case
327 // here; GNU ld also uses it as a bitwise not operator. It is only
328 // recognized as the operator if it is not immediately followed by
329 // some character which can appear in a symbol. That is, "~0" is a
330 // symbol name, and "~ 0" is an expression using bitwise not. We are
334 Lex::can_start_name(char c, char c2)
338 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
339 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
340 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
341 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
343 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
344 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
345 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
346 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
348 case '_': case '.': case '$': case '/': case '\\':
352 return can_continue_name(c2);
359 // Return whether C can continue a name which has already started.
360 // Subsequent characters in a name are the same as the leading
361 // characters, plus digits and "=+-:[],?*". So in general the linker
362 // script language requires spaces around operators.
365 Lex::can_continue_name(char c)
369 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
370 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
371 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
372 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
374 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
375 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
376 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
377 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
379 case '_': case '.': case '$': case '/': case '\\':
381 case '0': case '1': case '2': case '3': case '4':
382 case '5': case '6': case '7': case '8': case '9':
383 case '=': case '+': case '-': case ':': case '[': case ']':
384 case ',': case '?': case '*':
392 // For a number we accept 0x followed by hex digits, or any sequence
393 // of digits. The old linker accepts leading '$' for hex, and
394 // trailing HXBOD. Those are for MRI compatibility and we don't
395 // accept them. The old linker also accepts trailing MK for mega or
396 // kilo. Those are mentioned in the documentation, and we accept
399 // Return whether C1 C2 C3 can start a hex number.
402 Lex::can_start_hex(char c1, char c2, char c3)
404 if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
405 return Lex::can_continue_hex(c3);
409 // Return whether C can appear in a hex number.
412 Lex::can_continue_hex(char c)
416 case '0': case '1': case '2': case '3': case '4':
417 case '5': case '6': case '7': case '8': case '9':
418 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
419 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
427 // Return whether C can start a non-hex number.
430 Lex::can_start_number(char c)
434 case '0': case '1': case '2': case '3': case '4':
435 case '5': case '6': case '7': case '8': case '9':
443 // If C1 C2 C3 form a valid three character operator, return the
444 // opcode (defined in the yyscript.h file generated from yyscript.y).
445 // Otherwise return 0.
448 Lex::three_char_operator(char c1, char c2, char c3)
453 if (c2 == '<' && c3 == '=')
457 if (c2 == '>' && c3 == '=')
466 // If C1 C2 form a valid two character operator, return the opcode
467 // (defined in the yyscript.h file generated from yyscript.y).
468 // Otherwise return 0.
471 Lex::two_char_operator(char c1, char c2)
529 // If C1 is a valid operator, return the opcode. Otherwise return 0.
532 Lex::one_char_operator(char c1)
565 // Skip a C style comment. *PP points to just after the "/*". Return
566 // false if the comment did not end.
569 Lex::skip_c_comment(const char** pp)
572 while (p[0] != '*' || p[1] != '/')
583 this->linestart_ = p + 1;
592 // Skip a line # comment. Return false if there was no newline.
595 Lex::skip_line_comment(const char** pp)
598 size_t skip = strcspn(p, "\n");
607 this->linestart_ = p;
613 // Build a token CLASSIFICATION from all characters that match
614 // CAN_CONTINUE_FN. Update *PP.
617 Lex::gather_token(Token::Classification classification,
618 bool (*can_continue_fn)(char),
623 while ((*can_continue_fn)(*match))
626 return this->make_token(classification,
627 std::string(start, match - start),
631 // Build a token from a quoted string.
634 Lex::gather_quoted_string(const char** pp)
636 const char* start = *pp;
637 const char* p = start;
639 size_t skip = strcspn(p, "\"\n");
641 return this->make_invalid_token(start);
643 return this->make_token(Token::TOKEN_STRING,
644 std::string(p, skip),
648 // Return the next token at *PP. Update *PP. General guideline: we
649 // require linker scripts to be simple ASCII. No unicode linker
650 // scripts. In particular we can assume that any '\0' is the end of
654 Lex::get_token(const char** pp)
663 return this->make_eof_token(p);
666 // Skip whitespace quickly.
667 while (*p == ' ' || *p == '\t')
674 this->linestart_ = p;
678 // Skip C style comments.
679 if (p[0] == '/' && p[1] == '*')
681 int lineno = this->lineno_;
682 int charpos = p - this->linestart_ + 1;
685 if (!this->skip_c_comment(pp))
686 return Token(Token::TOKEN_INVALID, lineno, charpos);
692 // Skip line comments.
696 if (!this->skip_line_comment(pp))
697 return this->make_eof_token(p);
703 if (Lex::can_start_name(p[0], p[1]))
704 return this->gather_token(Token::TOKEN_STRING,
705 Lex::can_continue_name,
708 // We accept any arbitrary name in double quotes, as long as it
709 // does not cross a line boundary.
713 return this->gather_quoted_string(pp);
716 // Check for a number.
718 if (Lex::can_start_hex(p[0], p[1], p[2]))
719 return this->gather_token(Token::TOKEN_INTEGER,
720 Lex::can_continue_hex,
723 if (Lex::can_start_number(p[0]))
724 return this->gather_token(Token::TOKEN_INTEGER,
725 Lex::can_continue_number,
728 // Check for operators.
730 int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
734 return this->make_token(opcode, p);
737 opcode = Lex::two_char_operator(p[0], p[1]);
741 return this->make_token(opcode, p);
744 opcode = Lex::one_char_operator(p[0]);
748 return this->make_token(opcode, p);
751 return this->make_token(Token::TOKEN_INVALID, p);
755 // Tokenize the file. Return the final token.
760 std::string contents;
761 this->read_file(&contents);
763 const char* p = contents.c_str();
766 this->linestart_ = p;
770 Token t(this->get_token(&p));
772 // Don't let an early null byte fool us into thinking that we've
773 // reached the end of the file.
775 && static_cast<size_t>(p - contents.c_str()) < contents.length())
776 t = this->make_invalid_token(p);
778 if (t.is_invalid() || t.is_eof())
781 this->tokens_.push_back(t);
785 // A trivial task which waits for THIS_BLOCKER to be clear and then
786 // clears NEXT_BLOCKER. THIS_BLOCKER may be NULL.
788 class Script_unblock : public Task
791 Script_unblock(Task_token* this_blocker, Task_token* next_blocker)
792 : this_blocker_(this_blocker), next_blocker_(next_blocker)
797 if (this->this_blocker_ != NULL)
798 delete this->this_blocker_;
804 if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
805 return this->this_blocker_;
810 locks(Task_locker* tl)
811 { tl->add(this, this->next_blocker_); }
819 { return "Script_unblock"; }
822 Task_token* this_blocker_;
823 Task_token* next_blocker_;
826 // This class holds data passed through the parser to the lexer and to
827 // the parser support functions. This avoids global variables. We
828 // can't use global variables because we need not be called by a
834 Parser_closure(const char* filename,
835 const Position_dependent_options& posdep_options,
836 bool in_group, bool is_in_sysroot,
837 Command_line* command_line,
839 const Lex::Token_sequence* tokens)
840 : filename_(filename), posdep_options_(posdep_options),
841 in_group_(in_group), is_in_sysroot_(is_in_sysroot),
842 command_line_(command_line), layout_(layout), tokens_(tokens),
843 next_token_index_(0), inputs_(NULL)
846 // Return the file name.
849 { return this->filename_; }
851 // Return the position dependent options. The caller may modify
853 Position_dependent_options&
854 position_dependent_options()
855 { return this->posdep_options_; }
857 // Return whether this script is being run in a group.
860 { return this->in_group_; }
862 // Return whether this script was found using a directory in the
865 is_in_sysroot() const
866 { return this->is_in_sysroot_; }
868 // Returns the Command_line structure passed in at constructor time.
869 // This value may be NULL. The caller may modify this, which modifies
870 // the passed-in Command_line object (not a copy).
871 Command_line* command_line()
872 { return this->command_line_; }
874 // Return the Layout structure passed in at constructor time. This
875 // value may be NULL.
877 { return this->layout_; }
879 // Whether we are at the end of the token list.
882 { return this->next_token_index_ >= this->tokens_->size(); }
884 // Return the next token.
888 const Token* ret = &(*this->tokens_)[this->next_token_index_];
889 ++this->next_token_index_;
893 // Return the list of input files, creating it if necessary. This
894 // is a space leak--we never free the INPUTS_ pointer.
898 if (this->inputs_ == NULL)
899 this->inputs_ = new Input_arguments();
900 return this->inputs_;
903 // Return whether we saw any input files.
906 { return this->inputs_ != NULL && !this->inputs_->empty(); }
909 // The name of the file we are reading.
910 const char* filename_;
911 // The position dependent options.
912 Position_dependent_options posdep_options_;
913 // Whether we are currently in a --start-group/--end-group.
915 // Whether the script was found in a sysrooted directory.
917 // May be NULL if the user chooses not to pass one in.
918 Command_line* command_line_;
919 // May be NULL if the user chooses not to pass one in.
922 // The tokens to be returned by the lexer.
923 const Lex::Token_sequence* tokens_;
924 // The index of the next token to return.
925 unsigned int next_token_index_;
926 // New input files found to add to the link.
927 Input_arguments* inputs_;
930 // FILE was found as an argument on the command line. Try to read it
931 // as a script. We've already read BYTES of data into P, but we
932 // ignore that. Return true if the file was handled.
935 read_input_script(Workqueue* workqueue, const General_options& options,
936 Symbol_table* symtab, Layout* layout,
937 Dirsearch* dirsearch, Input_objects* input_objects,
938 Input_group* input_group,
939 const Input_argument* input_argument,
940 Input_file* input_file, const unsigned char*, off_t,
941 Task_token* this_blocker, Task_token* next_blocker)
944 if (lex.tokenize().is_invalid())
947 Parser_closure closure(input_file->filename().c_str(),
948 input_argument->file().options(),
950 input_file->is_in_sysroot(),
955 if (yyparse(&closure) != 0)
958 // THIS_BLOCKER must be clear before we may add anything to the
959 // symbol table. We are responsible for unblocking NEXT_BLOCKER
960 // when we are done. We are responsible for deleting THIS_BLOCKER
961 // when it is unblocked.
963 if (!closure.saw_inputs())
965 // The script did not add any files to read. Note that we are
966 // not permitted to call NEXT_BLOCKER->unblock() here even if
967 // THIS_BLOCKER is NULL, as we do not hold the workqueue lock.
968 workqueue->queue(new Script_unblock(this_blocker, next_blocker));
972 for (Input_arguments::const_iterator p = closure.inputs()->begin();
973 p != closure.inputs()->end();
977 if (p + 1 == closure.inputs()->end())
981 nb = new Task_token(true);
984 workqueue->queue(new Read_symbols(options, input_objects, symtab,
985 layout, dirsearch, &*p,
986 input_group, this_blocker, nb));
993 // FILENAME was found as an argument to --script (-T).
994 // Read it as a script, and execute its contents immediately.
997 read_commandline_script(const char* filename, Command_line* cmdline)
999 // TODO: if filename is a relative filename, search for it manually
1000 // using "." + cmdline->options()->search_path() -- not dirsearch.
1001 Dirsearch dirsearch;
1003 // The file locking code wants to record a Task, but we haven't
1004 // started the workqueue yet. This is only for debugging purposes,
1005 // so we invent a fake value.
1006 const Task* task = reinterpret_cast<const Task*>(-1);
1008 Input_file_argument input_argument(filename, false, "",
1009 cmdline->position_dependent_options());
1010 Input_file input_file(&input_argument);
1011 if (!input_file.open(cmdline->options(), dirsearch, task))
1014 Lex lex(&input_file);
1015 if (lex.tokenize().is_invalid())
1017 // Opening the file locked it, so now we need to unlock it.
1018 input_file.file().unlock(task);
1022 Parser_closure closure(filename,
1023 cmdline->position_dependent_options(),
1025 input_file.is_in_sysroot(),
1029 if (yyparse(&closure) != 0)
1031 input_file.file().unlock(task);
1035 input_file.file().unlock(task);
1037 gold_assert(!closure.saw_inputs());
1042 // Manage mapping from keywords to the codes expected by the bison
1045 class Keyword_to_parsecode
1048 // The structure which maps keywords to parsecodes.
1049 struct Keyword_parsecode
1052 const char* keyword;
1053 // Corresponding parsecode.
1057 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1060 keyword_to_parsecode(const char* keyword);
1063 // The array of all keywords.
1064 static const Keyword_parsecode keyword_parsecodes_[];
1066 // The number of keywords.
1067 static const int keyword_count;
1070 // Mapping from keyword string to keyword parsecode. This array must
1071 // be kept in sorted order. Parsecodes are looked up using bsearch.
1072 // This array must correspond to the list of parsecodes in yyscript.y.
1074 const Keyword_to_parsecode::Keyword_parsecode
1075 Keyword_to_parsecode::keyword_parsecodes_[] =
1077 { "ABSOLUTE", ABSOLUTE },
1079 { "ALIGN", ALIGN_K },
1080 { "ASSERT", ASSERT_K },
1081 { "AS_NEEDED", AS_NEEDED },
1086 { "CONSTANT", CONSTANT },
1087 { "CONSTRUCTORS", CONSTRUCTORS },
1089 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1090 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1091 { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1092 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1093 { "DEFINED", DEFINED },
1096 { "EXCLUDE_FILE", EXCLUDE_FILE },
1097 { "EXTERN", EXTERN },
1100 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1103 { "INCLUDE", INCLUDE },
1105 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1108 { "LENGTH", LENGTH },
1109 { "LOADADDR", LOADADDR },
1113 { "MEMORY", MEMORY },
1116 { "NOCROSSREFS", NOCROSSREFS },
1117 { "NOFLOAT", NOFLOAT },
1118 { "NOLOAD", NOLOAD },
1119 { "ONLY_IF_RO", ONLY_IF_RO },
1120 { "ONLY_IF_RW", ONLY_IF_RW },
1121 { "OPTION", OPTION },
1122 { "ORIGIN", ORIGIN },
1123 { "OUTPUT", OUTPUT },
1124 { "OUTPUT_ARCH", OUTPUT_ARCH },
1125 { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1126 { "OVERLAY", OVERLAY },
1128 { "PROVIDE", PROVIDE },
1129 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1131 { "SEARCH_DIR", SEARCH_DIR },
1132 { "SECTIONS", SECTIONS },
1133 { "SEGMENT_START", SEGMENT_START },
1135 { "SIZEOF", SIZEOF },
1136 { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1137 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1138 { "SORT_BY_NAME", SORT_BY_NAME },
1139 { "SPECIAL", SPECIAL },
1141 { "STARTUP", STARTUP },
1142 { "SUBALIGN", SUBALIGN },
1143 { "SYSLIB", SYSLIB },
1144 { "TARGET", TARGET_K },
1145 { "TRUNCATE", TRUNCATE },
1146 { "VERSION", VERSIONK },
1147 { "global", GLOBAL },
1153 { "sizeof_headers", SIZEOF_HEADERS },
1156 const int Keyword_to_parsecode::keyword_count =
1157 (sizeof(Keyword_to_parsecode::keyword_parsecodes_)
1158 / sizeof(Keyword_to_parsecode::keyword_parsecodes_[0]));
1160 // Comparison function passed to bsearch.
1166 ktt_compare(const void* keyv, const void* kttv)
1168 const char* key = static_cast<const char*>(keyv);
1169 const Keyword_to_parsecode::Keyword_parsecode* ktt =
1170 static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1171 return strcmp(key, ktt->keyword);
1174 } // End extern "C".
1177 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword)
1179 void* kttv = bsearch(keyword,
1180 Keyword_to_parsecode::keyword_parsecodes_,
1181 Keyword_to_parsecode::keyword_count,
1182 sizeof(Keyword_to_parsecode::keyword_parsecodes_[0]),
1186 Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1187 return ktt->parsecode;
1190 } // End namespace gold.
1192 // The remaining functions are extern "C", so it's clearer to not put
1193 // them in namespace gold.
1195 using namespace gold;
1197 // This function is called by the bison parser to return the next
1201 yylex(YYSTYPE* lvalp, void* closurev)
1203 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1205 if (closure->at_eof())
1208 const Token* token = closure->next_token();
1210 switch (token->classification())
1213 case Token::TOKEN_INVALID:
1214 case Token::TOKEN_EOF:
1217 case Token::TOKEN_STRING:
1219 const char* str = token->string_value().c_str();
1220 int parsecode = Keyword_to_parsecode::keyword_to_parsecode(str);
1223 lvalp->string = str;
1227 case Token::TOKEN_OPERATOR:
1228 return token->operator_value();
1230 case Token::TOKEN_INTEGER:
1231 lvalp->integer = token->integer_value();
1236 // This function is called by the bison parser to report an error.
1239 yyerror(void* closurev, const char* message)
1241 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1243 gold_error(_("%s: %s"), closure->filename(), message);
1246 // Called by the bison parser to add a file to the link.
1249 script_add_file(void* closurev, const char* name)
1251 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1253 // If this is an absolute path, and we found the script in the
1254 // sysroot, then we want to prepend the sysroot to the file name.
1255 // For example, this is how we handle a cross link to the x86_64
1256 // libc.so, which refers to /lib/libc.so.6.
1257 std::string name_string;
1258 const char* extra_search_path = ".";
1259 std::string script_directory;
1260 if (IS_ABSOLUTE_PATH (name))
1262 if (closure->is_in_sysroot())
1264 const std::string& sysroot(parameters->sysroot());
1265 gold_assert(!sysroot.empty());
1266 name_string = sysroot + name;
1267 name = name_string.c_str();
1272 // In addition to checking the normal library search path, we
1273 // also want to check in the script-directory.
1274 const char *slash = strrchr(closure->filename(), '/');
1277 script_directory.assign(closure->filename(),
1278 slash - closure->filename() + 1);
1279 extra_search_path = script_directory.c_str();
1283 Input_file_argument file(name, false, extra_search_path,
1284 closure->position_dependent_options());
1285 closure->inputs()->add_file(file);
1288 // Called by the bison parser to start a group. If we are already in
1289 // a group, that means that this script was invoked within a
1290 // --start-group --end-group sequence on the command line, or that
1291 // this script was found in a GROUP of another script. In that case,
1292 // we simply continue the existing group, rather than starting a new
1293 // one. It is possible to construct a case in which this will do
1294 // something other than what would happen if we did a recursive group,
1295 // but it's hard to imagine why the different behaviour would be
1296 // useful for a real program. Avoiding recursive groups is simpler
1297 // and more efficient.
1300 script_start_group(void* closurev)
1302 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1303 if (!closure->in_group())
1304 closure->inputs()->start_group();
1307 // Called by the bison parser at the end of a group.
1310 script_end_group(void* closurev)
1312 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1313 if (!closure->in_group())
1314 closure->inputs()->end_group();
1317 // Called by the bison parser to start an AS_NEEDED list.
1320 script_start_as_needed(void* closurev)
1322 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1323 closure->position_dependent_options().set_as_needed();
1326 // Called by the bison parser at the end of an AS_NEEDED list.
1329 script_end_as_needed(void* closurev)
1331 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1332 closure->position_dependent_options().clear_as_needed();
1335 // Called by the bison parser to set the entry symbol.
1338 script_set_entry(void* closurev, const char* entry)
1340 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1341 if (closure->command_line() != NULL)
1342 closure->command_line()->set_entry(entry);
1344 closure->layout()->set_entry(entry);
1347 // Called by the bison parser to parse an OPTION.
1350 script_parse_option(void* closurev, const char* option)
1352 Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1353 // We treat the option as a single command-line option, even if
1354 // it has internal whitespace.
1355 if (closure->command_line() == NULL)
1357 // There are some options that we could handle here--e.g.,
1358 // -lLIBRARY. Should we bother?
1359 gold_warning(_("%s: ignoring command OPTION; OPTION is only valid"
1360 " for scripts specified via -T/--script"),
1361 closure->filename());
1365 bool past_a_double_dash_option = false;
1366 char* mutable_option = strdup(option);
1367 closure->command_line()->process_one_option(1, &mutable_option, 0,
1368 &past_a_double_dash_option);
1369 free(mutable_option);