b003addec21bd7b01e63085091fb3f2bbdab7e80
[external/binutils.git] / gold / script.cc
1 // script.cc -- handle linker scripts for gold.
2
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
23 #include "gold.h"
24
25 #include <string>
26 #include <vector>
27 #include <cstdio>
28 #include <cstdlib>
29
30 #include "options.h"
31 #include "fileread.h"
32 #include "workqueue.h"
33 #include "readsyms.h"
34 #include "yyscript.h"
35 #include "script.h"
36 #include "script-c.h"
37
38 namespace gold
39 {
40
41 // A token read from a script file.  We don't implement keywords here;
42 // all keywords are simply represented as a string.
43
44 class Token
45 {
46  public:
47   // Token classification.
48   enum Classification
49   {
50     // Token is invalid.
51     TOKEN_INVALID,
52     // Token indicates end of input.
53     TOKEN_EOF,
54     // Token is a string of characters.
55     TOKEN_STRING,
56     // Token is an operator.
57     TOKEN_OPERATOR,
58     // Token is a number (an integer).
59     TOKEN_INTEGER
60   };
61
62   // We need an empty constructor so that we can put this STL objects.
63   Token()
64     : classification_(TOKEN_INVALID), value_(), opcode_(0),
65       lineno_(0), charpos_(0)
66   { }
67
68   // A general token with no value.
69   Token(Classification classification, int lineno, int charpos)
70     : classification_(classification), value_(), opcode_(0),
71       lineno_(lineno), charpos_(charpos)
72   {
73     gold_assert(classification == TOKEN_INVALID
74                 || classification == TOKEN_EOF);
75   }
76
77   // A general token with a value.
78   Token(Classification classification, const std::string& value,
79         int lineno, int charpos)
80     : classification_(classification), value_(value), opcode_(0),
81       lineno_(lineno), charpos_(charpos)
82   {
83     gold_assert(classification != TOKEN_INVALID
84                 && classification != TOKEN_EOF);
85   }
86
87   // A token representing a string of characters.
88   Token(const std::string& s, int lineno, int charpos)
89     : classification_(TOKEN_STRING), value_(s), opcode_(0),
90       lineno_(lineno), charpos_(charpos)
91   { }
92
93   // A token representing an operator.
94   Token(int opcode, int lineno, int charpos)
95     : classification_(TOKEN_OPERATOR), value_(), opcode_(opcode),
96       lineno_(lineno), charpos_(charpos)
97   { }
98
99   // Return whether the token is invalid.
100   bool
101   is_invalid() const
102   { return this->classification_ == TOKEN_INVALID; }
103
104   // Return whether this is an EOF token.
105   bool
106   is_eof() const
107   { return this->classification_ == TOKEN_EOF; }
108
109   // Return the token classification.
110   Classification
111   classification() const
112   { return this->classification_; }
113
114   // Return the line number at which the token starts.
115   int
116   lineno() const
117   { return this->lineno_; }
118
119   // Return the character position at this the token starts.
120   int
121   charpos() const
122   { return this->charpos_; }
123
124   // Get the value of a token.
125
126   const std::string&
127   string_value() const
128   {
129     gold_assert(this->classification_ == TOKEN_STRING);
130     return this->value_;
131   }
132
133   int
134   operator_value() const
135   {
136     gold_assert(this->classification_ == TOKEN_OPERATOR);
137     return this->opcode_;
138   }
139
140   int64_t
141   integer_value() const
142   {
143     gold_assert(this->classification_ == TOKEN_INTEGER);
144     return strtoll(this->value_.c_str(), NULL, 0);
145   }
146
147  private:
148   // The token classification.
149   Classification classification_;
150   // The token value, for TOKEN_STRING or TOKEN_INTEGER.
151   std::string value_;
152   // The token value, for TOKEN_OPERATOR.
153   int opcode_;
154   // The line number where this token started (one based).
155   int lineno_;
156   // The character position within the line where this token started
157   // (one based).
158   int charpos_;
159 };
160
161 // This class handles lexing a file into a sequence of tokens.  We
162 // don't expect linker scripts to be large, so we just read them and
163 // tokenize them all at once.
164
165 class Lex
166 {
167  public:
168   Lex(Input_file* input_file)
169     : input_file_(input_file), tokens_()
170   { }
171
172   // Tokenize the file.  Return the final token, which will be either
173   // an invalid token or an EOF token.  An invalid token indicates
174   // that tokenization failed.
175   Token
176   tokenize();
177
178   // A token sequence.
179   typedef std::vector<Token> Token_sequence;
180
181   // Return the tokens.
182   const Token_sequence&
183   tokens() const
184   { return this->tokens_; }
185
186  private:
187   Lex(const Lex&);
188   Lex& operator=(const Lex&);
189
190   // Read the file into a string buffer.
191   void
192   read_file(std::string*);
193
194   // Make a general token with no value at the current location.
195   Token
196   make_token(Token::Classification c, const char* p) const
197   { return Token(c, this->lineno_, p - this->linestart_ + 1); }
198
199   // Make a general token with a value at the current location.
200   Token
201   make_token(Token::Classification c, const std::string& v, const char* p)
202     const
203   { return Token(c, v, this->lineno_, p - this->linestart_ + 1); }
204
205   // Make an operator token at the current location.
206   Token
207   make_token(int opcode, const char* p) const
208   { return Token(opcode, this->lineno_, p - this->linestart_ + 1); }
209
210   // Make an invalid token at the current location.
211   Token
212   make_invalid_token(const char* p)
213   { return this->make_token(Token::TOKEN_INVALID, p); }
214
215   // Make an EOF token at the current location.
216   Token
217   make_eof_token(const char* p)
218   { return this->make_token(Token::TOKEN_EOF, p); }
219
220   // Return whether C can be the first character in a name.  C2 is the
221   // next character, since we sometimes need that.
222   static inline bool
223   can_start_name(char c, char c2);
224
225   // Return whether C can appear in a name which has already started.
226   static inline bool
227   can_continue_name(char c);
228
229   // Return whether C, C2, C3 can start a hex number.
230   static inline bool
231   can_start_hex(char c, char c2, char c3);
232
233   // Return whether C can appear in a hex number.
234   static inline bool
235   can_continue_hex(char c);
236
237   // Return whether C can start a non-hex number.
238   static inline bool
239   can_start_number(char c);
240
241   // Return whether C can appear in a non-hex number.
242   static inline bool
243   can_continue_number(char c)
244   { return Lex::can_start_number(c); }
245
246   // If C1 C2 C3 form a valid three character operator, return the
247   // opcode.  Otherwise return 0.
248   static inline int
249   three_char_operator(char c1, char c2, char c3);
250
251   // If C1 C2 form a valid two character operator, return the opcode.
252   // Otherwise return 0.
253   static inline int
254   two_char_operator(char c1, char c2);
255
256   // If C1 is a valid one character operator, return the opcode.
257   // Otherwise return 0.
258   static inline int
259   one_char_operator(char c1);
260
261   // Read the next token.
262   Token
263   get_token(const char**);
264
265   // Skip a C style /* */ comment.  Return false if the comment did
266   // not end.
267   bool
268   skip_c_comment(const char**);
269
270   // Skip a line # comment.  Return false if there was no newline.
271   bool
272   skip_line_comment(const char**);
273
274   // Build a token CLASSIFICATION from all characters that match
275   // CAN_CONTINUE_FN.  The token starts at START.  Start matching from
276   // MATCH.  Set *PP to the character following the token.
277   inline Token
278   gather_token(Token::Classification, bool (*can_continue_fn)(char),
279                const char* start, const char* match, const char** pp);
280
281   // Build a token from a quoted string.
282   Token
283   gather_quoted_string(const char** pp);
284
285   // The file we are reading.
286   Input_file* input_file_;
287   // The token sequence we create.
288   Token_sequence tokens_;
289   // The current line number.
290   int lineno_;
291   // The start of the current line in the buffer.
292   const char* linestart_;
293 };
294
295 // Read the whole file into memory.  We don't expect linker scripts to
296 // be large, so we just use a std::string as a buffer.  We ignore the
297 // data we've already read, so that we read aligned buffers.
298
299 void
300 Lex::read_file(std::string* contents)
301 {
302   off_t filesize = this->input_file_->file().filesize();
303   contents->clear();
304   contents->reserve(filesize);
305
306   off_t off = 0;
307   unsigned char buf[BUFSIZ];
308   while (off < filesize)
309     {
310       off_t get = BUFSIZ;
311       if (get > filesize - off)
312         get = filesize - off;
313       this->input_file_->file().read(off, get, buf);
314       contents->append(reinterpret_cast<char*>(&buf[0]), get);
315       off += get;
316     }
317 }
318
319 // Return whether C can be the start of a name, if the next character
320 // is C2.  A name can being with a letter, underscore, period, or
321 // dollar sign.  Because a name can be a file name, we also permit
322 // forward slash, backslash, and tilde.  Tilde is the tricky case
323 // here; GNU ld also uses it as a bitwise not operator.  It is only
324 // recognized as the operator if it is not immediately followed by
325 // some character which can appear in a symbol.  That is, "~0" is a
326 // symbol name, and "~ 0" is an expression using bitwise not.  We are
327 // compatible.
328
329 inline bool
330 Lex::can_start_name(char c, char c2)
331 {
332   switch (c)
333     {
334     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
335     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
336     case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
337     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
338     case 'Y': case 'Z':
339     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
340     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
341     case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
342     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
343     case 'y': case 'z':
344     case '_': case '.': case '$': case '/': case '\\':
345       return true;
346
347     case '~':
348       return can_continue_name(c2);
349
350     default:
351       return false;
352     }
353 }
354
355 // Return whether C can continue a name which has already started.
356 // Subsequent characters in a name are the same as the leading
357 // characters, plus digits and "=+-:[],?*".  So in general the linker
358 // script language requires spaces around operators.
359
360 inline bool
361 Lex::can_continue_name(char c)
362 {
363   switch (c)
364     {
365     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
366     case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
367     case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
368     case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
369     case 'Y': case 'Z':
370     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
371     case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
372     case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
373     case 's': case 't': case 'u': case 'v': case 'w': case 'x':
374     case 'y': case 'z':
375     case '_': case '.': case '$': case '/': case '\\':
376     case '~':
377     case '0': case '1': case '2': case '3': case '4':
378     case '5': case '6': case '7': case '8': case '9':
379     case '=': case '+': case '-': case ':': case '[': case ']':
380     case ',': case '?': case '*':
381       return true;
382
383     default:
384       return false;
385     }
386 }
387
388 // For a number we accept 0x followed by hex digits, or any sequence
389 // of digits.  The old linker accepts leading '$' for hex, and
390 // trailing HXBOD.  Those are for MRI compatibility and we don't
391 // accept them.  The old linker also accepts trailing MK for mega or
392 // kilo.  Those are mentioned in the documentation, and we accept
393 // them.
394
395 // Return whether C1 C2 C3 can start a hex number.
396
397 inline bool
398 Lex::can_start_hex(char c1, char c2, char c3)
399 {
400   if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
401     return Lex::can_continue_hex(c3);
402   return false;
403 }
404
405 // Return whether C can appear in a hex number.
406
407 inline bool
408 Lex::can_continue_hex(char c)
409 {
410   switch (c)
411     {
412     case '0': case '1': case '2': case '3': case '4':
413     case '5': case '6': case '7': case '8': case '9':
414     case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
415     case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
416       return true;
417
418     default:
419       return false;
420     }
421 }
422
423 // Return whether C can start a non-hex number.
424
425 inline bool
426 Lex::can_start_number(char c)
427 {
428   switch (c)
429     {
430     case '0': case '1': case '2': case '3': case '4':
431     case '5': case '6': case '7': case '8': case '9':
432       return true;
433
434     default:
435       return false;
436     }
437 }
438
439 // If C1 C2 C3 form a valid three character operator, return the
440 // opcode (defined in the yyscript.h file generated from yyscript.y).
441 // Otherwise return 0.
442
443 inline int
444 Lex::three_char_operator(char c1, char c2, char c3)
445 {
446   switch (c1)
447     {
448     case '<':
449       if (c2 == '<' && c3 == '=')
450         return LSHIFTEQ;
451       break;
452     case '>':
453       if (c2 == '>' && c3 == '=')
454         return RSHIFTEQ;
455       break;
456     default:
457       break;
458     }
459   return 0;
460 }
461
462 // If C1 C2 form a valid two character operator, return the opcode
463 // (defined in the yyscript.h file generated from yyscript.y).
464 // Otherwise return 0.
465
466 inline int
467 Lex::two_char_operator(char c1, char c2)
468 {
469   switch (c1)
470     {
471     case '=':
472       if (c2 == '=')
473         return EQ;
474       break;
475     case '!':
476       if (c2 == '=')
477         return NE;
478       break;
479     case '+':
480       if (c2 == '=')
481         return PLUSEQ;
482       break;
483     case '-':
484       if (c2 == '=')
485         return MINUSEQ;
486       break;
487     case '*':
488       if (c2 == '=')
489         return MULTEQ;
490       break;
491     case '/':
492       if (c2 == '=')
493         return DIVEQ;
494       break;
495     case '|':
496       if (c2 == '=')
497         return OREQ;
498       if (c2 == '|')
499         return OROR;
500       break;
501     case '&':
502       if (c2 == '=')
503         return ANDEQ;
504       if (c2 == '&')
505         return ANDAND;
506       break;
507     case '>':
508       if (c2 == '=')
509         return GE;
510       if (c2 == '>')
511         return RSHIFT;
512       break;
513     case '<':
514       if (c2 == '=')
515         return LE;
516       if (c2 == '<')
517         return LSHIFT;
518       break;
519     default:
520       break;
521     }
522   return 0;
523 }
524
525 // If C1 is a valid operator, return the opcode.  Otherwise return 0.
526
527 inline int
528 Lex::one_char_operator(char c1)
529 {
530   switch (c1)
531     {
532     case '+':
533     case '-':
534     case '*':
535     case '/':
536     case '%':
537     case '!':
538     case '&':
539     case '|':
540     case '^':
541     case '~':
542     case '<':
543     case '>':
544     case '=':
545     case '?':
546     case ',':
547     case '(':
548     case ')':
549     case '{':
550     case '}':
551     case '[':
552     case ']':
553     case ':':
554     case ';':
555       return c1;
556     default:
557       return 0;
558     }
559 }
560
561 // Skip a C style comment.  *PP points to just after the "/*".  Return
562 // false if the comment did not end.
563
564 bool
565 Lex::skip_c_comment(const char** pp)
566 {
567   const char* p = *pp;
568   while (p[0] != '*' || p[1] != '/')
569     {
570       if (*p == '\0')
571         {
572           *pp = p;
573           return false;
574         }
575
576       if (*p == '\n')
577         {
578           ++this->lineno_;
579           this->linestart_ = p + 1;
580         }
581       ++p;
582     }
583
584   *pp = p + 2;
585   return true;
586 }
587
588 // Skip a line # comment.  Return false if there was no newline.
589
590 bool
591 Lex::skip_line_comment(const char** pp)
592 {
593   const char* p = *pp;
594   size_t skip = strcspn(p, "\n");
595   if (p[skip] == '\0')
596     {
597       *pp = p + skip;
598       return false;
599     }
600
601   p += skip + 1;
602   ++this->lineno_;
603   this->linestart_ = p;
604   *pp = p;
605
606   return true;
607 }
608
609 // Build a token CLASSIFICATION from all characters that match
610 // CAN_CONTINUE_FN.  Update *PP.
611
612 inline Token
613 Lex::gather_token(Token::Classification classification,
614                   bool (*can_continue_fn)(char),
615                   const char* start,
616                   const char* match,
617                   const char **pp)
618 {
619   while ((*can_continue_fn)(*match))
620     ++match;
621   *pp = match;
622   return this->make_token(classification,
623                           std::string(start, match - start),
624                           start);
625 }
626
627 // Build a token from a quoted string.
628
629 Token
630 Lex::gather_quoted_string(const char** pp)
631 {
632   const char* start = *pp;
633   const char* p = start;
634   ++p;
635   size_t skip = strcspn(p, "\"\n");
636   if (p[skip] != '"')
637     return this->make_invalid_token(start);
638   *pp = p + skip + 1;
639   return this->make_token(Token::TOKEN_STRING,
640                           std::string(p, skip),
641                           start);
642 }
643
644 // Return the next token at *PP.  Update *PP.  General guideline: we
645 // require linker scripts to be simple ASCII.  No unicode linker
646 // scripts.  In particular we can assume that any '\0' is the end of
647 // the input.
648
649 Token
650 Lex::get_token(const char** pp)
651 {
652   const char* p = *pp;
653
654   while (true)
655     {
656       if (*p == '\0')
657         {
658           *pp = p;
659           return this->make_eof_token(p);
660         }
661
662       // Skip whitespace quickly.
663       while (*p == ' ' || *p == '\t')
664         ++p;
665
666       if (*p == '\n')
667         {
668           ++p;
669           ++this->lineno_;
670           this->linestart_ = p;
671           continue;
672         }
673
674       // Skip C style comments.
675       if (p[0] == '/' && p[1] == '*')
676         {
677           int lineno = this->lineno_;
678           int charpos = p - this->linestart_ + 1;
679
680           *pp = p + 2;
681           if (!this->skip_c_comment(pp))
682             return Token(Token::TOKEN_INVALID, lineno, charpos);
683           p = *pp;
684
685           continue;
686         }
687
688       // Skip line comments.
689       if (*p == '#')
690         {
691           *pp = p + 1;
692           if (!this->skip_line_comment(pp))
693             return this->make_eof_token(p);
694           p = *pp;
695           continue;
696         }
697
698       // Check for a name.
699       if (Lex::can_start_name(p[0], p[1]))
700         return this->gather_token(Token::TOKEN_STRING,
701                                   Lex::can_continue_name,
702                                   p, p + 2, pp);
703
704       // We accept any arbitrary name in double quotes, as long as it
705       // does not cross a line boundary.
706       if (*p == '"')
707         {
708           *pp = p;
709           return this->gather_quoted_string(pp);
710         }
711
712       // Check for a number.
713
714       if (Lex::can_start_hex(p[0], p[1], p[2]))
715         return this->gather_token(Token::TOKEN_INTEGER,
716                                   Lex::can_continue_hex,
717                                   p, p + 3, pp);
718
719       if (Lex::can_start_number(p[0]))
720         return this->gather_token(Token::TOKEN_INTEGER,
721                                   Lex::can_continue_number,
722                                   p, p + 1, pp);
723
724       // Check for operators.
725
726       int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
727       if (opcode != 0)
728         {
729           *pp = p + 3;
730           return this->make_token(opcode, p);
731         }
732
733       opcode = Lex::two_char_operator(p[0], p[1]);
734       if (opcode != 0)
735         {
736           *pp = p + 2;
737           return this->make_token(opcode, p);
738         }
739
740       opcode = Lex::one_char_operator(p[0]);
741       if (opcode != 0)
742         {
743           *pp = p + 1;
744           return this->make_token(opcode, p);
745         }
746
747       return this->make_token(Token::TOKEN_INVALID, p);
748     }
749 }
750
751 // Tokenize the file.  Return the final token.
752
753 Token
754 Lex::tokenize()
755 {
756   std::string contents;
757   this->read_file(&contents);
758
759   const char* p = contents.c_str();
760
761   this->lineno_ = 1;
762   this->linestart_ = p;
763
764   while (true)
765     {
766       Token t(this->get_token(&p));
767
768       // Don't let an early null byte fool us into thinking that we've
769       // reached the end of the file.
770       if (t.is_eof()
771           && static_cast<size_t>(p - contents.c_str()) < contents.length())
772         t = this->make_invalid_token(p);
773
774       if (t.is_invalid() || t.is_eof())
775         return t;
776
777       this->tokens_.push_back(t);
778     }
779 }
780
781 // A trivial task which waits for THIS_BLOCKER to be clear and then
782 // clears NEXT_BLOCKER.  THIS_BLOCKER may be NULL.
783
784 class Script_unblock : public Task
785 {
786  public:
787   Script_unblock(Task_token* this_blocker, Task_token* next_blocker)
788     : this_blocker_(this_blocker), next_blocker_(next_blocker)
789   { }
790
791   ~Script_unblock()
792   {
793     if (this->this_blocker_ != NULL)
794       delete this->this_blocker_;
795   }
796
797   Is_runnable_type
798   is_runnable(Workqueue*)
799   {
800     if (this->this_blocker_ != NULL && this->this_blocker_->is_blocked())
801       return IS_BLOCKED;
802     return IS_RUNNABLE;
803   }
804
805   Task_locker*
806   locks(Workqueue* workqueue)
807   {
808     return new Task_locker_block(*this->next_blocker_, workqueue);
809   }
810
811   void
812   run(Workqueue*)
813   { }
814
815  private:
816   Task_token* this_blocker_;
817   Task_token* next_blocker_;
818 };
819
820 // This class holds data passed through the parser to the lexer and to
821 // the parser support functions.  This avoids global variables.  We
822 // can't use global variables because we need not be called in the
823 // main thread.
824
825 class Parser_closure
826 {
827  public:
828   Parser_closure(const char* filename,
829                  const Position_dependent_options& posdep_options,
830                  bool in_group,
831                  const Lex::Token_sequence* tokens)
832     : filename_(filename), posdep_options_(posdep_options),
833       in_group_(in_group), tokens_(tokens),
834       next_token_index_(0), inputs_(NULL)
835   { }
836
837   // Return the file name.
838   const char*
839   filename() const
840   { return this->filename_; }
841
842   // Return the position dependent options.  The caller may modify
843   // this.
844   Position_dependent_options&
845   position_dependent_options()
846   { return this->posdep_options_; }
847
848   // Return whether this script is being run in a group.
849   bool
850   in_group() const
851   { return this->in_group_; }
852
853   // Whether we are at the end of the token list.
854   bool
855   at_eof() const
856   { return this->next_token_index_ >= this->tokens_->size(); }
857
858   // Return the next token.
859   const Token*
860   next_token()
861   {
862     const Token* ret = &(*this->tokens_)[this->next_token_index_];
863     ++this->next_token_index_;
864     return ret;
865   }
866
867   // Return the list of input files, creating it if necessary.  This
868   // is a space leak--we never free the INPUTS_ pointer.
869   Input_arguments*
870   inputs()
871   {
872     if (this->inputs_ == NULL)
873       this->inputs_ = new Input_arguments();
874     return this->inputs_;
875   }
876
877   // Return whether we saw any input files.
878   bool
879   saw_inputs() const
880   { return this->inputs_ != NULL && !this->inputs_->empty(); }
881
882  private:
883   // The name of the file we are reading.
884   const char* filename_;
885   // The position dependent options.
886   Position_dependent_options posdep_options_;
887   // Whether we are currently in a --start-group/--end-group.
888   bool in_group_;
889
890   // The tokens to be returned by the lexer.
891   const Lex::Token_sequence* tokens_;
892   // The index of the next token to return.
893   unsigned int next_token_index_;
894   // New input files found to add to the link.
895   Input_arguments* inputs_;
896 };
897
898 // FILE was found as an argument on the command line.  Try to read it
899 // as a script.  We've already read BYTES of data into P, but we
900 // ignore that.  Return true if the file was handled.
901
902 bool
903 read_input_script(Workqueue* workqueue, const General_options& options,
904                   Symbol_table* symtab, Layout* layout,
905                   const Dirsearch& dirsearch, Input_objects* input_objects,
906                   Input_group* input_group,
907                   const Input_argument* input_argument,
908                   Input_file* input_file, const unsigned char*, off_t,
909                   Task_token* this_blocker, Task_token* next_blocker)
910 {
911   Lex lex(input_file);
912   if (lex.tokenize().is_invalid())
913     return false;
914
915   Parser_closure closure(input_file->filename().c_str(),
916                          input_argument->file().options(),
917                          input_group != NULL,
918                          &lex.tokens());
919
920   if (yyparse(&closure) != 0)
921     return false;
922
923   // THIS_BLOCKER must be clear before we may add anything to the
924   // symbol table.  We are responsible for unblocking NEXT_BLOCKER
925   // when we are done.  We are responsible for deleting THIS_BLOCKER
926   // when it is unblocked.
927
928   if (!closure.saw_inputs())
929     {
930       // The script did not add any files to read.  Note that we are
931       // not permitted to call NEXT_BLOCKER->unblock() here even if
932       // THIS_BLOCKER is NULL, as we are not in the main thread.
933       workqueue->queue(new Script_unblock(this_blocker, next_blocker));
934       return true;
935     }
936
937   for (Input_arguments::const_iterator p = closure.inputs()->begin();
938        p != closure.inputs()->end();
939        ++p)
940     {
941       Task_token* nb;
942       if (p + 1 == closure.inputs()->end())
943         nb = next_blocker;
944       else
945         {
946           nb = new Task_token();
947           nb->add_blocker();
948         }
949       workqueue->queue(new Read_symbols(options, input_objects, symtab,
950                                         layout, dirsearch, &*p,
951                                         input_group, this_blocker, nb));
952       this_blocker = nb;
953     }
954
955   return true;
956 }
957
958 // Manage mapping from keywords to the codes expected by the bison
959 // parser.
960
961 class Keyword_to_parsecode
962 {
963  public:
964   // The structure which maps keywords to parsecodes.
965   struct Keyword_parsecode
966   {
967     // Keyword.
968     const char* keyword;
969     // Corresponding parsecode.
970     int parsecode;
971   };
972
973   // Return the parsecode corresponding KEYWORD, or 0 if it is not a
974   // keyword.
975   static int
976   keyword_to_parsecode(const char* keyword);
977
978  private:
979   // The array of all keywords.
980   static const Keyword_parsecode keyword_parsecodes_[];
981
982   // The number of keywords.
983   static const int keyword_count;
984 };
985
986 // Mapping from keyword string to keyword parsecode.  This array must
987 // be kept in sorted order.  Parsecodes are looked up using bsearch.
988 // This array must correspond to the list of parsecodes in yyscript.y.
989
990 const Keyword_to_parsecode::Keyword_parsecode
991 Keyword_to_parsecode::keyword_parsecodes_[] =
992 {
993   { "ABSOLUTE", ABSOLUTE },
994   { "ADDR", ADDR },
995   { "ALIGN", ALIGN_K },
996   { "ASSERT", ASSERT_K },
997   { "AS_NEEDED", AS_NEEDED },
998   { "AT", AT },
999   { "BIND", BIND },
1000   { "BLOCK", BLOCK },
1001   { "BYTE", BYTE },
1002   { "CONSTANT", CONSTANT },
1003   { "CONSTRUCTORS", CONSTRUCTORS },
1004   { "COPY", COPY },
1005   { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1006   { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1007   { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1008   { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1009   { "DEFINED", DEFINED },
1010   { "DSECT", DSECT },
1011   { "ENTRY", ENTRY },
1012   { "EXCLUDE_FILE", EXCLUDE_FILE },
1013   { "EXTERN", EXTERN },
1014   { "FILL", FILL },
1015   { "FLOAT", FLOAT },
1016   { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1017   { "GROUP", GROUP },
1018   { "HLL", HLL },
1019   { "INCLUDE", INCLUDE },
1020   { "INFO", INFO },
1021   { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1022   { "INPUT", INPUT },
1023   { "KEEP", KEEP },
1024   { "LENGTH", LENGTH },
1025   { "LOADADDR", LOADADDR },
1026   { "LONG", LONG },
1027   { "MAP", MAP },
1028   { "MAX", MAX_K },
1029   { "MEMORY", MEMORY },
1030   { "MIN", MIN_K },
1031   { "NEXT", NEXT },
1032   { "NOCROSSREFS", NOCROSSREFS },
1033   { "NOFLOAT", NOFLOAT },
1034   { "NOLOAD", NOLOAD },
1035   { "ONLY_IF_RO", ONLY_IF_RO },
1036   { "ONLY_IF_RW", ONLY_IF_RW },
1037   { "ORIGIN", ORIGIN },
1038   { "OUTPUT", OUTPUT },
1039   { "OUTPUT_ARCH", OUTPUT_ARCH },
1040   { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1041   { "OVERLAY", OVERLAY },
1042   { "PHDRS", PHDRS },
1043   { "PROVIDE", PROVIDE },
1044   { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1045   { "QUAD", QUAD },
1046   { "SEARCH_DIR", SEARCH_DIR },
1047   { "SECTIONS", SECTIONS },
1048   { "SEGMENT_START", SEGMENT_START },
1049   { "SHORT", SHORT },
1050   { "SIZEOF", SIZEOF },
1051   { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1052   { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1053   { "SORT_BY_NAME", SORT_BY_NAME },
1054   { "SPECIAL", SPECIAL },
1055   { "SQUAD", SQUAD },
1056   { "STARTUP", STARTUP },
1057   { "SUBALIGN", SUBALIGN },
1058   { "SYSLIB", SYSLIB },
1059   { "TARGET", TARGET_K },
1060   { "TRUNCATE", TRUNCATE },
1061   { "VERSION", VERSIONK },
1062   { "global", GLOBAL },
1063   { "l", LENGTH },
1064   { "len", LENGTH },
1065   { "local", LOCAL },
1066   { "o", ORIGIN },
1067   { "org", ORIGIN },
1068   { "sizeof_headers", SIZEOF_HEADERS },
1069 };
1070
1071 const int Keyword_to_parsecode::keyword_count =
1072   (sizeof(Keyword_to_parsecode::keyword_parsecodes_)
1073    / sizeof(Keyword_to_parsecode::keyword_parsecodes_[0]));
1074
1075 // Comparison function passed to bsearch.
1076
1077 extern "C"
1078 {
1079
1080 static int
1081 ktt_compare(const void* keyv, const void* kttv)
1082 {
1083   const char* key = static_cast<const char*>(keyv);
1084   const Keyword_to_parsecode::Keyword_parsecode* ktt =
1085     static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1086   return strcmp(key, ktt->keyword);
1087 }
1088
1089 } // End extern "C".
1090
1091 int
1092 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword)
1093 {
1094   void* kttv = bsearch(keyword,
1095                        Keyword_to_parsecode::keyword_parsecodes_,
1096                        Keyword_to_parsecode::keyword_count,
1097                        sizeof(Keyword_to_parsecode::keyword_parsecodes_[0]),
1098                        ktt_compare);
1099   if (kttv == NULL)
1100     return 0;
1101   Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1102   return ktt->parsecode;
1103 }
1104
1105 } // End namespace gold.
1106
1107 // The remaining functions are extern "C", so it's clearer to not put
1108 // them in namespace gold.
1109
1110 using namespace gold;
1111
1112 // This function is called by the bison parser to return the next
1113 // token.
1114
1115 extern "C" int
1116 yylex(YYSTYPE* lvalp, void* closurev)
1117 {
1118   Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1119
1120   if (closure->at_eof())
1121     return 0;
1122
1123   const Token* token = closure->next_token();
1124
1125   switch (token->classification())
1126     {
1127     default:
1128     case Token::TOKEN_INVALID:
1129     case Token::TOKEN_EOF:
1130       gold_unreachable();
1131
1132     case Token::TOKEN_STRING:
1133       {
1134         const char* str = token->string_value().c_str();
1135         int parsecode = Keyword_to_parsecode::keyword_to_parsecode(str);
1136         if (parsecode != 0)
1137           return parsecode;
1138         lvalp->string = str;
1139         return STRING;
1140       }
1141
1142     case Token::TOKEN_OPERATOR:
1143       return token->operator_value();
1144
1145     case Token::TOKEN_INTEGER:
1146       lvalp->integer = token->integer_value();
1147       return INTEGER;
1148     }
1149 }
1150
1151 // This function is called by the bison parser to report an error.
1152
1153 extern "C" void
1154 yyerror(void* closurev, const char* message)
1155 {
1156   Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1157
1158   fprintf(stderr, _("%s: %s: %s\n"),
1159           program_name, closure->filename(), message);
1160   gold_exit(false);
1161 }
1162
1163 // Called by the bison parser to add a file to the link.
1164
1165 extern "C" void
1166 script_add_file(void* closurev, const char* name)
1167 {
1168   Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1169   // In addition to checking the normal library search path, we also
1170   // want to check in the script-directory.
1171   const char *slash = strrchr(closure->filename(), '/');
1172   std::string script_directory(closure->filename(),
1173                                slash ? slash - closure->filename() + 1 : 0);
1174   Input_file_argument file(name, false,
1175                            slash ? script_directory.c_str() : ".",
1176                            closure->position_dependent_options());
1177   closure->inputs()->add_file(file);
1178 }
1179
1180 // Called by the bison parser to start a group.  If we are already in
1181 // a group, that means that this script was invoked within a
1182 // --start-group --end-group sequence on the command line, or that
1183 // this script was found in a GROUP of another script.  In that case,
1184 // we simply continue the existing group, rather than starting a new
1185 // one.  It is possible to construct a case in which this will do
1186 // something other than what would happen if we did a recursive group,
1187 // but it's hard to imagine why the different behaviour would be
1188 // useful for a real program.  Avoiding recursive groups is simpler
1189 // and more efficient.
1190
1191 extern "C" void
1192 script_start_group(void* closurev)
1193 {
1194   Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1195   if (!closure->in_group())
1196     closure->inputs()->start_group();
1197 }
1198
1199 // Called by the bison parser at the end of a group.
1200
1201 extern "C" void
1202 script_end_group(void* closurev)
1203 {
1204   Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1205   if (!closure->in_group())
1206     closure->inputs()->end_group();
1207 }
1208
1209 // Called by the bison parser to start an AS_NEEDED list.
1210
1211 extern "C" void
1212 script_start_as_needed(void* closurev)
1213 {
1214   Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1215   closure->position_dependent_options().set_as_needed();
1216 }
1217
1218 // Called by the bison parser at the end of an AS_NEEDED list.
1219
1220 extern "C" void
1221 script_end_as_needed(void* closurev)
1222 {
1223   Parser_closure* closure = static_cast<Parser_closure*>(closurev);
1224   closure->position_dependent_options().clear_as_needed();
1225 }