1*a9fa9459Szrj // script.cc -- handle linker scripts for gold.
2*a9fa9459Szrj
3*a9fa9459Szrj // Copyright (C) 2006-2016 Free Software Foundation, Inc.
4*a9fa9459Szrj // Written by Ian Lance Taylor <iant@google.com>.
5*a9fa9459Szrj
6*a9fa9459Szrj // This file is part of gold.
7*a9fa9459Szrj
8*a9fa9459Szrj // This program is free software; you can redistribute it and/or modify
9*a9fa9459Szrj // it under the terms of the GNU General Public License as published by
10*a9fa9459Szrj // the Free Software Foundation; either version 3 of the License, or
11*a9fa9459Szrj // (at your option) any later version.
12*a9fa9459Szrj
13*a9fa9459Szrj // This program is distributed in the hope that it will be useful,
14*a9fa9459Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of
15*a9fa9459Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16*a9fa9459Szrj // GNU General Public License for more details.
17*a9fa9459Szrj
18*a9fa9459Szrj // You should have received a copy of the GNU General Public License
19*a9fa9459Szrj // along with this program; if not, write to the Free Software
20*a9fa9459Szrj // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21*a9fa9459Szrj // MA 02110-1301, USA.
22*a9fa9459Szrj
23*a9fa9459Szrj #include "gold.h"
24*a9fa9459Szrj
25*a9fa9459Szrj #include <cstdio>
26*a9fa9459Szrj #include <cstdlib>
27*a9fa9459Szrj #include <cstring>
28*a9fa9459Szrj #include <fnmatch.h>
29*a9fa9459Szrj #include <string>
30*a9fa9459Szrj #include <vector>
31*a9fa9459Szrj #include "filenames.h"
32*a9fa9459Szrj
33*a9fa9459Szrj #include "elfcpp.h"
34*a9fa9459Szrj #include "demangle.h"
35*a9fa9459Szrj #include "dirsearch.h"
36*a9fa9459Szrj #include "options.h"
37*a9fa9459Szrj #include "fileread.h"
38*a9fa9459Szrj #include "workqueue.h"
39*a9fa9459Szrj #include "readsyms.h"
40*a9fa9459Szrj #include "parameters.h"
41*a9fa9459Szrj #include "layout.h"
42*a9fa9459Szrj #include "symtab.h"
43*a9fa9459Szrj #include "target-select.h"
44*a9fa9459Szrj #include "script.h"
45*a9fa9459Szrj #include "script-c.h"
46*a9fa9459Szrj #include "incremental.h"
47*a9fa9459Szrj
48*a9fa9459Szrj namespace gold
49*a9fa9459Szrj {
50*a9fa9459Szrj
51*a9fa9459Szrj // A token read from a script file. We don't implement keywords here;
52*a9fa9459Szrj // all keywords are simply represented as a string.
53*a9fa9459Szrj
54*a9fa9459Szrj class Token
55*a9fa9459Szrj {
56*a9fa9459Szrj public:
57*a9fa9459Szrj // Token classification.
58*a9fa9459Szrj enum Classification
59*a9fa9459Szrj {
60*a9fa9459Szrj // Token is invalid.
61*a9fa9459Szrj TOKEN_INVALID,
62*a9fa9459Szrj // Token indicates end of input.
63*a9fa9459Szrj TOKEN_EOF,
64*a9fa9459Szrj // Token is a string of characters.
65*a9fa9459Szrj TOKEN_STRING,
66*a9fa9459Szrj // Token is a quoted string of characters.
67*a9fa9459Szrj TOKEN_QUOTED_STRING,
68*a9fa9459Szrj // Token is an operator.
69*a9fa9459Szrj TOKEN_OPERATOR,
70*a9fa9459Szrj // Token is a number (an integer).
71*a9fa9459Szrj TOKEN_INTEGER
72*a9fa9459Szrj };
73*a9fa9459Szrj
74*a9fa9459Szrj // We need an empty constructor so that we can put this STL objects.
Token()75*a9fa9459Szrj Token()
76*a9fa9459Szrj : classification_(TOKEN_INVALID), value_(NULL), value_length_(0),
77*a9fa9459Szrj opcode_(0), lineno_(0), charpos_(0)
78*a9fa9459Szrj { }
79*a9fa9459Szrj
80*a9fa9459Szrj // A general token with no value.
Token(Classification classification,int lineno,int charpos)81*a9fa9459Szrj Token(Classification classification, int lineno, int charpos)
82*a9fa9459Szrj : classification_(classification), value_(NULL), value_length_(0),
83*a9fa9459Szrj opcode_(0), lineno_(lineno), charpos_(charpos)
84*a9fa9459Szrj {
85*a9fa9459Szrj gold_assert(classification == TOKEN_INVALID
86*a9fa9459Szrj || classification == TOKEN_EOF);
87*a9fa9459Szrj }
88*a9fa9459Szrj
89*a9fa9459Szrj // A general token with a value.
Token(Classification classification,const char * value,size_t length,int lineno,int charpos)90*a9fa9459Szrj Token(Classification classification, const char* value, size_t length,
91*a9fa9459Szrj int lineno, int charpos)
92*a9fa9459Szrj : classification_(classification), value_(value), value_length_(length),
93*a9fa9459Szrj opcode_(0), lineno_(lineno), charpos_(charpos)
94*a9fa9459Szrj {
95*a9fa9459Szrj gold_assert(classification != TOKEN_INVALID
96*a9fa9459Szrj && classification != TOKEN_EOF);
97*a9fa9459Szrj }
98*a9fa9459Szrj
99*a9fa9459Szrj // A token representing an operator.
Token(int opcode,int lineno,int charpos)100*a9fa9459Szrj Token(int opcode, int lineno, int charpos)
101*a9fa9459Szrj : classification_(TOKEN_OPERATOR), value_(NULL), value_length_(0),
102*a9fa9459Szrj opcode_(opcode), lineno_(lineno), charpos_(charpos)
103*a9fa9459Szrj { }
104*a9fa9459Szrj
105*a9fa9459Szrj // Return whether the token is invalid.
106*a9fa9459Szrj bool
is_invalid() const107*a9fa9459Szrj is_invalid() const
108*a9fa9459Szrj { return this->classification_ == TOKEN_INVALID; }
109*a9fa9459Szrj
110*a9fa9459Szrj // Return whether this is an EOF token.
111*a9fa9459Szrj bool
is_eof() const112*a9fa9459Szrj is_eof() const
113*a9fa9459Szrj { return this->classification_ == TOKEN_EOF; }
114*a9fa9459Szrj
115*a9fa9459Szrj // Return the token classification.
116*a9fa9459Szrj Classification
classification() const117*a9fa9459Szrj classification() const
118*a9fa9459Szrj { return this->classification_; }
119*a9fa9459Szrj
120*a9fa9459Szrj // Return the line number at which the token starts.
121*a9fa9459Szrj int
lineno() const122*a9fa9459Szrj lineno() const
123*a9fa9459Szrj { return this->lineno_; }
124*a9fa9459Szrj
125*a9fa9459Szrj // Return the character position at this the token starts.
126*a9fa9459Szrj int
charpos() const127*a9fa9459Szrj charpos() const
128*a9fa9459Szrj { return this->charpos_; }
129*a9fa9459Szrj
130*a9fa9459Szrj // Get the value of a token.
131*a9fa9459Szrj
132*a9fa9459Szrj const char*
string_value(size_t * length) const133*a9fa9459Szrj string_value(size_t* length) const
134*a9fa9459Szrj {
135*a9fa9459Szrj gold_assert(this->classification_ == TOKEN_STRING
136*a9fa9459Szrj || this->classification_ == TOKEN_QUOTED_STRING);
137*a9fa9459Szrj *length = this->value_length_;
138*a9fa9459Szrj return this->value_;
139*a9fa9459Szrj }
140*a9fa9459Szrj
141*a9fa9459Szrj int
operator_value() const142*a9fa9459Szrj operator_value() const
143*a9fa9459Szrj {
144*a9fa9459Szrj gold_assert(this->classification_ == TOKEN_OPERATOR);
145*a9fa9459Szrj return this->opcode_;
146*a9fa9459Szrj }
147*a9fa9459Szrj
148*a9fa9459Szrj uint64_t
149*a9fa9459Szrj integer_value() const;
150*a9fa9459Szrj
151*a9fa9459Szrj private:
152*a9fa9459Szrj // The token classification.
153*a9fa9459Szrj Classification classification_;
154*a9fa9459Szrj // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or
155*a9fa9459Szrj // TOKEN_INTEGER.
156*a9fa9459Szrj const char* value_;
157*a9fa9459Szrj // The length of the token value.
158*a9fa9459Szrj size_t value_length_;
159*a9fa9459Szrj // The token value, for TOKEN_OPERATOR.
160*a9fa9459Szrj int opcode_;
161*a9fa9459Szrj // The line number where this token started (one based).
162*a9fa9459Szrj int lineno_;
163*a9fa9459Szrj // The character position within the line where this token started
164*a9fa9459Szrj // (one based).
165*a9fa9459Szrj int charpos_;
166*a9fa9459Szrj };
167*a9fa9459Szrj
168*a9fa9459Szrj // Return the value of a TOKEN_INTEGER.
169*a9fa9459Szrj
170*a9fa9459Szrj uint64_t
integer_value() const171*a9fa9459Szrj Token::integer_value() const
172*a9fa9459Szrj {
173*a9fa9459Szrj gold_assert(this->classification_ == TOKEN_INTEGER);
174*a9fa9459Szrj
175*a9fa9459Szrj size_t len = this->value_length_;
176*a9fa9459Szrj
177*a9fa9459Szrj uint64_t multiplier = 1;
178*a9fa9459Szrj char last = this->value_[len - 1];
179*a9fa9459Szrj if (last == 'm' || last == 'M')
180*a9fa9459Szrj {
181*a9fa9459Szrj multiplier = 1024 * 1024;
182*a9fa9459Szrj --len;
183*a9fa9459Szrj }
184*a9fa9459Szrj else if (last == 'k' || last == 'K')
185*a9fa9459Szrj {
186*a9fa9459Szrj multiplier = 1024;
187*a9fa9459Szrj --len;
188*a9fa9459Szrj }
189*a9fa9459Szrj
190*a9fa9459Szrj char *end;
191*a9fa9459Szrj uint64_t ret = strtoull(this->value_, &end, 0);
192*a9fa9459Szrj gold_assert(static_cast<size_t>(end - this->value_) == len);
193*a9fa9459Szrj
194*a9fa9459Szrj return ret * multiplier;
195*a9fa9459Szrj }
196*a9fa9459Szrj
197*a9fa9459Szrj // This class handles lexing a file into a sequence of tokens.
198*a9fa9459Szrj
199*a9fa9459Szrj class Lex
200*a9fa9459Szrj {
201*a9fa9459Szrj public:
202*a9fa9459Szrj // We unfortunately have to support different lexing modes, because
203*a9fa9459Szrj // when reading different parts of a linker script we need to parse
204*a9fa9459Szrj // things differently.
205*a9fa9459Szrj enum Mode
206*a9fa9459Szrj {
207*a9fa9459Szrj // Reading an ordinary linker script.
208*a9fa9459Szrj LINKER_SCRIPT,
209*a9fa9459Szrj // Reading an expression in a linker script.
210*a9fa9459Szrj EXPRESSION,
211*a9fa9459Szrj // Reading a version script.
212*a9fa9459Szrj VERSION_SCRIPT,
213*a9fa9459Szrj // Reading a --dynamic-list file.
214*a9fa9459Szrj DYNAMIC_LIST
215*a9fa9459Szrj };
216*a9fa9459Szrj
Lex(const char * input_string,size_t input_length,int parsing_token)217*a9fa9459Szrj Lex(const char* input_string, size_t input_length, int parsing_token)
218*a9fa9459Szrj : input_string_(input_string), input_length_(input_length),
219*a9fa9459Szrj current_(input_string), mode_(LINKER_SCRIPT),
220*a9fa9459Szrj first_token_(parsing_token), token_(),
221*a9fa9459Szrj lineno_(1), linestart_(input_string)
222*a9fa9459Szrj { }
223*a9fa9459Szrj
224*a9fa9459Szrj // Read a file into a string.
225*a9fa9459Szrj static void
226*a9fa9459Szrj read_file(Input_file*, std::string*);
227*a9fa9459Szrj
228*a9fa9459Szrj // Return the next token.
229*a9fa9459Szrj const Token*
230*a9fa9459Szrj next_token();
231*a9fa9459Szrj
232*a9fa9459Szrj // Return the current lexing mode.
233*a9fa9459Szrj Lex::Mode
mode() const234*a9fa9459Szrj mode() const
235*a9fa9459Szrj { return this->mode_; }
236*a9fa9459Szrj
237*a9fa9459Szrj // Set the lexing mode.
238*a9fa9459Szrj void
set_mode(Mode mode)239*a9fa9459Szrj set_mode(Mode mode)
240*a9fa9459Szrj { this->mode_ = mode; }
241*a9fa9459Szrj
242*a9fa9459Szrj private:
243*a9fa9459Szrj Lex(const Lex&);
244*a9fa9459Szrj Lex& operator=(const Lex&);
245*a9fa9459Szrj
246*a9fa9459Szrj // Make a general token with no value at the current location.
247*a9fa9459Szrj Token
make_token(Token::Classification c,const char * start) const248*a9fa9459Szrj make_token(Token::Classification c, const char* start) const
249*a9fa9459Szrj { return Token(c, this->lineno_, start - this->linestart_ + 1); }
250*a9fa9459Szrj
251*a9fa9459Szrj // Make a general token with a value at the current location.
252*a9fa9459Szrj Token
make_token(Token::Classification c,const char * v,size_t len,const char * start) const253*a9fa9459Szrj make_token(Token::Classification c, const char* v, size_t len,
254*a9fa9459Szrj const char* start)
255*a9fa9459Szrj const
256*a9fa9459Szrj { return Token(c, v, len, this->lineno_, start - this->linestart_ + 1); }
257*a9fa9459Szrj
258*a9fa9459Szrj // Make an operator token at the current location.
259*a9fa9459Szrj Token
make_token(int opcode,const char * start) const260*a9fa9459Szrj make_token(int opcode, const char* start) const
261*a9fa9459Szrj { return Token(opcode, this->lineno_, start - this->linestart_ + 1); }
262*a9fa9459Szrj
263*a9fa9459Szrj // Make an invalid token at the current location.
264*a9fa9459Szrj Token
make_invalid_token(const char * start)265*a9fa9459Szrj make_invalid_token(const char* start)
266*a9fa9459Szrj { return this->make_token(Token::TOKEN_INVALID, start); }
267*a9fa9459Szrj
268*a9fa9459Szrj // Make an EOF token at the current location.
269*a9fa9459Szrj Token
make_eof_token(const char * start)270*a9fa9459Szrj make_eof_token(const char* start)
271*a9fa9459Szrj { return this->make_token(Token::TOKEN_EOF, start); }
272*a9fa9459Szrj
273*a9fa9459Szrj // Return whether C can be the first character in a name. C2 is the
274*a9fa9459Szrj // next character, since we sometimes need that.
275*a9fa9459Szrj inline bool
276*a9fa9459Szrj can_start_name(char c, char c2);
277*a9fa9459Szrj
278*a9fa9459Szrj // If C can appear in a name which has already started, return a
279*a9fa9459Szrj // pointer to a character later in the token or just past
280*a9fa9459Szrj // it. Otherwise, return NULL.
281*a9fa9459Szrj inline const char*
282*a9fa9459Szrj can_continue_name(const char* c);
283*a9fa9459Szrj
284*a9fa9459Szrj // Return whether C, C2, C3 can start a hex number.
285*a9fa9459Szrj inline bool
286*a9fa9459Szrj can_start_hex(char c, char c2, char c3);
287*a9fa9459Szrj
288*a9fa9459Szrj // If C can appear in a hex number which has already started, return
289*a9fa9459Szrj // a pointer to a character later in the token or just past
290*a9fa9459Szrj // it. Otherwise, return NULL.
291*a9fa9459Szrj inline const char*
292*a9fa9459Szrj can_continue_hex(const char* c);
293*a9fa9459Szrj
294*a9fa9459Szrj // Return whether C can start a non-hex number.
295*a9fa9459Szrj static inline bool
296*a9fa9459Szrj can_start_number(char c);
297*a9fa9459Szrj
298*a9fa9459Szrj // If C can appear in a decimal number which has already started,
299*a9fa9459Szrj // return a pointer to a character later in the token or just past
300*a9fa9459Szrj // it. Otherwise, return NULL.
301*a9fa9459Szrj inline const char*
can_continue_number(const char * c)302*a9fa9459Szrj can_continue_number(const char* c)
303*a9fa9459Szrj { return Lex::can_start_number(*c) ? c + 1 : NULL; }
304*a9fa9459Szrj
305*a9fa9459Szrj // If C1 C2 C3 form a valid three character operator, return the
306*a9fa9459Szrj // opcode. Otherwise return 0.
307*a9fa9459Szrj static inline int
308*a9fa9459Szrj three_char_operator(char c1, char c2, char c3);
309*a9fa9459Szrj
310*a9fa9459Szrj // If C1 C2 form a valid two character operator, return the opcode.
311*a9fa9459Szrj // Otherwise return 0.
312*a9fa9459Szrj static inline int
313*a9fa9459Szrj two_char_operator(char c1, char c2);
314*a9fa9459Szrj
315*a9fa9459Szrj // If C1 is a valid one character operator, return the opcode.
316*a9fa9459Szrj // Otherwise return 0.
317*a9fa9459Szrj static inline int
318*a9fa9459Szrj one_char_operator(char c1);
319*a9fa9459Szrj
320*a9fa9459Szrj // Read the next token.
321*a9fa9459Szrj Token
322*a9fa9459Szrj get_token(const char**);
323*a9fa9459Szrj
324*a9fa9459Szrj // Skip a C style /* */ comment. Return false if the comment did
325*a9fa9459Szrj // not end.
326*a9fa9459Szrj bool
327*a9fa9459Szrj skip_c_comment(const char**);
328*a9fa9459Szrj
329*a9fa9459Szrj // Skip a line # comment. Return false if there was no newline.
330*a9fa9459Szrj bool
331*a9fa9459Szrj skip_line_comment(const char**);
332*a9fa9459Szrj
333*a9fa9459Szrj // Build a token CLASSIFICATION from all characters that match
334*a9fa9459Szrj // CAN_CONTINUE_FN. The token starts at START. Start matching from
335*a9fa9459Szrj // MATCH. Set *PP to the character following the token.
336*a9fa9459Szrj inline Token
337*a9fa9459Szrj gather_token(Token::Classification,
338*a9fa9459Szrj const char* (Lex::*can_continue_fn)(const char*),
339*a9fa9459Szrj const char* start, const char* match, const char** pp);
340*a9fa9459Szrj
341*a9fa9459Szrj // Build a token from a quoted string.
342*a9fa9459Szrj Token
343*a9fa9459Szrj gather_quoted_string(const char** pp);
344*a9fa9459Szrj
345*a9fa9459Szrj // The string we are tokenizing.
346*a9fa9459Szrj const char* input_string_;
347*a9fa9459Szrj // The length of the string.
348*a9fa9459Szrj size_t input_length_;
349*a9fa9459Szrj // The current offset into the string.
350*a9fa9459Szrj const char* current_;
351*a9fa9459Szrj // The current lexing mode.
352*a9fa9459Szrj Mode mode_;
353*a9fa9459Szrj // The code to use for the first token. This is set to 0 after it
354*a9fa9459Szrj // is used.
355*a9fa9459Szrj int first_token_;
356*a9fa9459Szrj // The current token.
357*a9fa9459Szrj Token token_;
358*a9fa9459Szrj // The current line number.
359*a9fa9459Szrj int lineno_;
360*a9fa9459Szrj // The start of the current line in the string.
361*a9fa9459Szrj const char* linestart_;
362*a9fa9459Szrj };
363*a9fa9459Szrj
364*a9fa9459Szrj // Read the whole file into memory. We don't expect linker scripts to
365*a9fa9459Szrj // be large, so we just use a std::string as a buffer. We ignore the
366*a9fa9459Szrj // data we've already read, so that we read aligned buffers.
367*a9fa9459Szrj
368*a9fa9459Szrj void
read_file(Input_file * input_file,std::string * contents)369*a9fa9459Szrj Lex::read_file(Input_file* input_file, std::string* contents)
370*a9fa9459Szrj {
371*a9fa9459Szrj off_t filesize = input_file->file().filesize();
372*a9fa9459Szrj contents->clear();
373*a9fa9459Szrj contents->reserve(filesize);
374*a9fa9459Szrj
375*a9fa9459Szrj off_t off = 0;
376*a9fa9459Szrj unsigned char buf[BUFSIZ];
377*a9fa9459Szrj while (off < filesize)
378*a9fa9459Szrj {
379*a9fa9459Szrj off_t get = BUFSIZ;
380*a9fa9459Szrj if (get > filesize - off)
381*a9fa9459Szrj get = filesize - off;
382*a9fa9459Szrj input_file->file().read(off, get, buf);
383*a9fa9459Szrj contents->append(reinterpret_cast<char*>(&buf[0]), get);
384*a9fa9459Szrj off += get;
385*a9fa9459Szrj }
386*a9fa9459Szrj }
387*a9fa9459Szrj
388*a9fa9459Szrj // Return whether C can be the start of a name, if the next character
389*a9fa9459Szrj // is C2. A name can being with a letter, underscore, period, or
390*a9fa9459Szrj // dollar sign. Because a name can be a file name, we also permit
391*a9fa9459Szrj // forward slash, backslash, and tilde. Tilde is the tricky case
392*a9fa9459Szrj // here; GNU ld also uses it as a bitwise not operator. It is only
393*a9fa9459Szrj // recognized as the operator if it is not immediately followed by
394*a9fa9459Szrj // some character which can appear in a symbol. That is, when we
395*a9fa9459Szrj // don't know that we are looking at an expression, "~0" is a file
396*a9fa9459Szrj // name, and "~ 0" is an expression using bitwise not. We are
397*a9fa9459Szrj // compatible.
398*a9fa9459Szrj
399*a9fa9459Szrj inline bool
can_start_name(char c,char c2)400*a9fa9459Szrj Lex::can_start_name(char c, char c2)
401*a9fa9459Szrj {
402*a9fa9459Szrj switch (c)
403*a9fa9459Szrj {
404*a9fa9459Szrj case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
405*a9fa9459Szrj case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
406*a9fa9459Szrj case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
407*a9fa9459Szrj case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
408*a9fa9459Szrj case 'Y': case 'Z':
409*a9fa9459Szrj case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
410*a9fa9459Szrj case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
411*a9fa9459Szrj case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
412*a9fa9459Szrj case 's': case 't': case 'u': case 'v': case 'w': case 'x':
413*a9fa9459Szrj case 'y': case 'z':
414*a9fa9459Szrj case '_': case '.': case '$':
415*a9fa9459Szrj return true;
416*a9fa9459Szrj
417*a9fa9459Szrj case '/': case '\\':
418*a9fa9459Szrj return this->mode_ == LINKER_SCRIPT;
419*a9fa9459Szrj
420*a9fa9459Szrj case '~':
421*a9fa9459Szrj return this->mode_ == LINKER_SCRIPT && can_continue_name(&c2);
422*a9fa9459Szrj
423*a9fa9459Szrj case '*': case '[':
424*a9fa9459Szrj return (this->mode_ == VERSION_SCRIPT
425*a9fa9459Szrj || this->mode_ == DYNAMIC_LIST
426*a9fa9459Szrj || (this->mode_ == LINKER_SCRIPT
427*a9fa9459Szrj && can_continue_name(&c2)));
428*a9fa9459Szrj
429*a9fa9459Szrj default:
430*a9fa9459Szrj return false;
431*a9fa9459Szrj }
432*a9fa9459Szrj }
433*a9fa9459Szrj
434*a9fa9459Szrj // Return whether C can continue a name which has already started.
435*a9fa9459Szrj // Subsequent characters in a name are the same as the leading
436*a9fa9459Szrj // characters, plus digits and "=+-:[],?*". So in general the linker
437*a9fa9459Szrj // script language requires spaces around operators, unless we know
438*a9fa9459Szrj // that we are parsing an expression.
439*a9fa9459Szrj
440*a9fa9459Szrj inline const char*
can_continue_name(const char * c)441*a9fa9459Szrj Lex::can_continue_name(const char* c)
442*a9fa9459Szrj {
443*a9fa9459Szrj switch (*c)
444*a9fa9459Szrj {
445*a9fa9459Szrj case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
446*a9fa9459Szrj case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
447*a9fa9459Szrj case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
448*a9fa9459Szrj case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
449*a9fa9459Szrj case 'Y': case 'Z':
450*a9fa9459Szrj case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
451*a9fa9459Szrj case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
452*a9fa9459Szrj case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
453*a9fa9459Szrj case 's': case 't': case 'u': case 'v': case 'w': case 'x':
454*a9fa9459Szrj case 'y': case 'z':
455*a9fa9459Szrj case '_': case '.': case '$':
456*a9fa9459Szrj case '0': case '1': case '2': case '3': case '4':
457*a9fa9459Szrj case '5': case '6': case '7': case '8': case '9':
458*a9fa9459Szrj return c + 1;
459*a9fa9459Szrj
460*a9fa9459Szrj // TODO(csilvers): why not allow ~ in names for version-scripts?
461*a9fa9459Szrj case '/': case '\\': case '~':
462*a9fa9459Szrj case '=': case '+':
463*a9fa9459Szrj case ',':
464*a9fa9459Szrj if (this->mode_ == LINKER_SCRIPT)
465*a9fa9459Szrj return c + 1;
466*a9fa9459Szrj return NULL;
467*a9fa9459Szrj
468*a9fa9459Szrj case '[': case ']': case '*': case '?': case '-':
469*a9fa9459Szrj if (this->mode_ == LINKER_SCRIPT || this->mode_ == VERSION_SCRIPT
470*a9fa9459Szrj || this->mode_ == DYNAMIC_LIST)
471*a9fa9459Szrj return c + 1;
472*a9fa9459Szrj return NULL;
473*a9fa9459Szrj
474*a9fa9459Szrj // TODO(csilvers): why allow this? ^ is meaningless in version scripts.
475*a9fa9459Szrj case '^':
476*a9fa9459Szrj if (this->mode_ == VERSION_SCRIPT || this->mode_ == DYNAMIC_LIST)
477*a9fa9459Szrj return c + 1;
478*a9fa9459Szrj return NULL;
479*a9fa9459Szrj
480*a9fa9459Szrj case ':':
481*a9fa9459Szrj if (this->mode_ == LINKER_SCRIPT)
482*a9fa9459Szrj return c + 1;
483*a9fa9459Szrj else if ((this->mode_ == VERSION_SCRIPT || this->mode_ == DYNAMIC_LIST)
484*a9fa9459Szrj && (c[1] == ':'))
485*a9fa9459Szrj {
486*a9fa9459Szrj // A name can have '::' in it, as that's a c++ namespace
487*a9fa9459Szrj // separator. But a single colon is not part of a name.
488*a9fa9459Szrj return c + 2;
489*a9fa9459Szrj }
490*a9fa9459Szrj return NULL;
491*a9fa9459Szrj
492*a9fa9459Szrj default:
493*a9fa9459Szrj return NULL;
494*a9fa9459Szrj }
495*a9fa9459Szrj }
496*a9fa9459Szrj
497*a9fa9459Szrj // For a number we accept 0x followed by hex digits, or any sequence
498*a9fa9459Szrj // of digits. The old linker accepts leading '$' for hex, and
499*a9fa9459Szrj // trailing HXBOD. Those are for MRI compatibility and we don't
500*a9fa9459Szrj // accept them.
501*a9fa9459Szrj
502*a9fa9459Szrj // Return whether C1 C2 C3 can start a hex number.
503*a9fa9459Szrj
504*a9fa9459Szrj inline bool
can_start_hex(char c1,char c2,char c3)505*a9fa9459Szrj Lex::can_start_hex(char c1, char c2, char c3)
506*a9fa9459Szrj {
507*a9fa9459Szrj if (c1 == '0' && (c2 == 'x' || c2 == 'X'))
508*a9fa9459Szrj return this->can_continue_hex(&c3);
509*a9fa9459Szrj return false;
510*a9fa9459Szrj }
511*a9fa9459Szrj
512*a9fa9459Szrj // Return whether C can appear in a hex number.
513*a9fa9459Szrj
514*a9fa9459Szrj inline const char*
can_continue_hex(const char * c)515*a9fa9459Szrj Lex::can_continue_hex(const char* c)
516*a9fa9459Szrj {
517*a9fa9459Szrj switch (*c)
518*a9fa9459Szrj {
519*a9fa9459Szrj case '0': case '1': case '2': case '3': case '4':
520*a9fa9459Szrj case '5': case '6': case '7': case '8': case '9':
521*a9fa9459Szrj case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
522*a9fa9459Szrj case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
523*a9fa9459Szrj return c + 1;
524*a9fa9459Szrj
525*a9fa9459Szrj default:
526*a9fa9459Szrj return NULL;
527*a9fa9459Szrj }
528*a9fa9459Szrj }
529*a9fa9459Szrj
530*a9fa9459Szrj // Return whether C can start a non-hex number.
531*a9fa9459Szrj
532*a9fa9459Szrj inline bool
can_start_number(char c)533*a9fa9459Szrj Lex::can_start_number(char c)
534*a9fa9459Szrj {
535*a9fa9459Szrj switch (c)
536*a9fa9459Szrj {
537*a9fa9459Szrj case '0': case '1': case '2': case '3': case '4':
538*a9fa9459Szrj case '5': case '6': case '7': case '8': case '9':
539*a9fa9459Szrj return true;
540*a9fa9459Szrj
541*a9fa9459Szrj default:
542*a9fa9459Szrj return false;
543*a9fa9459Szrj }
544*a9fa9459Szrj }
545*a9fa9459Szrj
546*a9fa9459Szrj // If C1 C2 C3 form a valid three character operator, return the
547*a9fa9459Szrj // opcode (defined in the yyscript.h file generated from yyscript.y).
548*a9fa9459Szrj // Otherwise return 0.
549*a9fa9459Szrj
550*a9fa9459Szrj inline int
three_char_operator(char c1,char c2,char c3)551*a9fa9459Szrj Lex::three_char_operator(char c1, char c2, char c3)
552*a9fa9459Szrj {
553*a9fa9459Szrj switch (c1)
554*a9fa9459Szrj {
555*a9fa9459Szrj case '<':
556*a9fa9459Szrj if (c2 == '<' && c3 == '=')
557*a9fa9459Szrj return LSHIFTEQ;
558*a9fa9459Szrj break;
559*a9fa9459Szrj case '>':
560*a9fa9459Szrj if (c2 == '>' && c3 == '=')
561*a9fa9459Szrj return RSHIFTEQ;
562*a9fa9459Szrj break;
563*a9fa9459Szrj default:
564*a9fa9459Szrj break;
565*a9fa9459Szrj }
566*a9fa9459Szrj return 0;
567*a9fa9459Szrj }
568*a9fa9459Szrj
569*a9fa9459Szrj // If C1 C2 form a valid two character operator, return the opcode
570*a9fa9459Szrj // (defined in the yyscript.h file generated from yyscript.y).
571*a9fa9459Szrj // Otherwise return 0.
572*a9fa9459Szrj
573*a9fa9459Szrj inline int
two_char_operator(char c1,char c2)574*a9fa9459Szrj Lex::two_char_operator(char c1, char c2)
575*a9fa9459Szrj {
576*a9fa9459Szrj switch (c1)
577*a9fa9459Szrj {
578*a9fa9459Szrj case '=':
579*a9fa9459Szrj if (c2 == '=')
580*a9fa9459Szrj return EQ;
581*a9fa9459Szrj break;
582*a9fa9459Szrj case '!':
583*a9fa9459Szrj if (c2 == '=')
584*a9fa9459Szrj return NE;
585*a9fa9459Szrj break;
586*a9fa9459Szrj case '+':
587*a9fa9459Szrj if (c2 == '=')
588*a9fa9459Szrj return PLUSEQ;
589*a9fa9459Szrj break;
590*a9fa9459Szrj case '-':
591*a9fa9459Szrj if (c2 == '=')
592*a9fa9459Szrj return MINUSEQ;
593*a9fa9459Szrj break;
594*a9fa9459Szrj case '*':
595*a9fa9459Szrj if (c2 == '=')
596*a9fa9459Szrj return MULTEQ;
597*a9fa9459Szrj break;
598*a9fa9459Szrj case '/':
599*a9fa9459Szrj if (c2 == '=')
600*a9fa9459Szrj return DIVEQ;
601*a9fa9459Szrj break;
602*a9fa9459Szrj case '|':
603*a9fa9459Szrj if (c2 == '=')
604*a9fa9459Szrj return OREQ;
605*a9fa9459Szrj if (c2 == '|')
606*a9fa9459Szrj return OROR;
607*a9fa9459Szrj break;
608*a9fa9459Szrj case '&':
609*a9fa9459Szrj if (c2 == '=')
610*a9fa9459Szrj return ANDEQ;
611*a9fa9459Szrj if (c2 == '&')
612*a9fa9459Szrj return ANDAND;
613*a9fa9459Szrj break;
614*a9fa9459Szrj case '>':
615*a9fa9459Szrj if (c2 == '=')
616*a9fa9459Szrj return GE;
617*a9fa9459Szrj if (c2 == '>')
618*a9fa9459Szrj return RSHIFT;
619*a9fa9459Szrj break;
620*a9fa9459Szrj case '<':
621*a9fa9459Szrj if (c2 == '=')
622*a9fa9459Szrj return LE;
623*a9fa9459Szrj if (c2 == '<')
624*a9fa9459Szrj return LSHIFT;
625*a9fa9459Szrj break;
626*a9fa9459Szrj default:
627*a9fa9459Szrj break;
628*a9fa9459Szrj }
629*a9fa9459Szrj return 0;
630*a9fa9459Szrj }
631*a9fa9459Szrj
632*a9fa9459Szrj // If C1 is a valid operator, return the opcode. Otherwise return 0.
633*a9fa9459Szrj
634*a9fa9459Szrj inline int
one_char_operator(char c1)635*a9fa9459Szrj Lex::one_char_operator(char c1)
636*a9fa9459Szrj {
637*a9fa9459Szrj switch (c1)
638*a9fa9459Szrj {
639*a9fa9459Szrj case '+':
640*a9fa9459Szrj case '-':
641*a9fa9459Szrj case '*':
642*a9fa9459Szrj case '/':
643*a9fa9459Szrj case '%':
644*a9fa9459Szrj case '!':
645*a9fa9459Szrj case '&':
646*a9fa9459Szrj case '|':
647*a9fa9459Szrj case '^':
648*a9fa9459Szrj case '~':
649*a9fa9459Szrj case '<':
650*a9fa9459Szrj case '>':
651*a9fa9459Szrj case '=':
652*a9fa9459Szrj case '?':
653*a9fa9459Szrj case ',':
654*a9fa9459Szrj case '(':
655*a9fa9459Szrj case ')':
656*a9fa9459Szrj case '{':
657*a9fa9459Szrj case '}':
658*a9fa9459Szrj case '[':
659*a9fa9459Szrj case ']':
660*a9fa9459Szrj case ':':
661*a9fa9459Szrj case ';':
662*a9fa9459Szrj return c1;
663*a9fa9459Szrj default:
664*a9fa9459Szrj return 0;
665*a9fa9459Szrj }
666*a9fa9459Szrj }
667*a9fa9459Szrj
668*a9fa9459Szrj // Skip a C style comment. *PP points to just after the "/*". Return
669*a9fa9459Szrj // false if the comment did not end.
670*a9fa9459Szrj
671*a9fa9459Szrj bool
skip_c_comment(const char ** pp)672*a9fa9459Szrj Lex::skip_c_comment(const char** pp)
673*a9fa9459Szrj {
674*a9fa9459Szrj const char* p = *pp;
675*a9fa9459Szrj while (p[0] != '*' || p[1] != '/')
676*a9fa9459Szrj {
677*a9fa9459Szrj if (*p == '\0')
678*a9fa9459Szrj {
679*a9fa9459Szrj *pp = p;
680*a9fa9459Szrj return false;
681*a9fa9459Szrj }
682*a9fa9459Szrj
683*a9fa9459Szrj if (*p == '\n')
684*a9fa9459Szrj {
685*a9fa9459Szrj ++this->lineno_;
686*a9fa9459Szrj this->linestart_ = p + 1;
687*a9fa9459Szrj }
688*a9fa9459Szrj ++p;
689*a9fa9459Szrj }
690*a9fa9459Szrj
691*a9fa9459Szrj *pp = p + 2;
692*a9fa9459Szrj return true;
693*a9fa9459Szrj }
694*a9fa9459Szrj
695*a9fa9459Szrj // Skip a line # comment. Return false if there was no newline.
696*a9fa9459Szrj
697*a9fa9459Szrj bool
skip_line_comment(const char ** pp)698*a9fa9459Szrj Lex::skip_line_comment(const char** pp)
699*a9fa9459Szrj {
700*a9fa9459Szrj const char* p = *pp;
701*a9fa9459Szrj size_t skip = strcspn(p, "\n");
702*a9fa9459Szrj if (p[skip] == '\0')
703*a9fa9459Szrj {
704*a9fa9459Szrj *pp = p + skip;
705*a9fa9459Szrj return false;
706*a9fa9459Szrj }
707*a9fa9459Szrj
708*a9fa9459Szrj p += skip + 1;
709*a9fa9459Szrj ++this->lineno_;
710*a9fa9459Szrj this->linestart_ = p;
711*a9fa9459Szrj *pp = p;
712*a9fa9459Szrj
713*a9fa9459Szrj return true;
714*a9fa9459Szrj }
715*a9fa9459Szrj
716*a9fa9459Szrj // Build a token CLASSIFICATION from all characters that match
717*a9fa9459Szrj // CAN_CONTINUE_FN. Update *PP.
718*a9fa9459Szrj
719*a9fa9459Szrj inline Token
gather_token(Token::Classification classification,const char * (Lex::* can_continue_fn)(const char *),const char * start,const char * match,const char ** pp)720*a9fa9459Szrj Lex::gather_token(Token::Classification classification,
721*a9fa9459Szrj const char* (Lex::*can_continue_fn)(const char*),
722*a9fa9459Szrj const char* start,
723*a9fa9459Szrj const char* match,
724*a9fa9459Szrj const char** pp)
725*a9fa9459Szrj {
726*a9fa9459Szrj const char* new_match = NULL;
727*a9fa9459Szrj while ((new_match = (this->*can_continue_fn)(match)) != NULL)
728*a9fa9459Szrj match = new_match;
729*a9fa9459Szrj
730*a9fa9459Szrj // A special case: integers may be followed by a single M or K,
731*a9fa9459Szrj // case-insensitive.
732*a9fa9459Szrj if (classification == Token::TOKEN_INTEGER
733*a9fa9459Szrj && (*match == 'm' || *match == 'M' || *match == 'k' || *match == 'K'))
734*a9fa9459Szrj ++match;
735*a9fa9459Szrj
736*a9fa9459Szrj *pp = match;
737*a9fa9459Szrj return this->make_token(classification, start, match - start, start);
738*a9fa9459Szrj }
739*a9fa9459Szrj
740*a9fa9459Szrj // Build a token from a quoted string.
741*a9fa9459Szrj
742*a9fa9459Szrj Token
gather_quoted_string(const char ** pp)743*a9fa9459Szrj Lex::gather_quoted_string(const char** pp)
744*a9fa9459Szrj {
745*a9fa9459Szrj const char* start = *pp;
746*a9fa9459Szrj const char* p = start;
747*a9fa9459Szrj ++p;
748*a9fa9459Szrj size_t skip = strcspn(p, "\"\n");
749*a9fa9459Szrj if (p[skip] != '"')
750*a9fa9459Szrj return this->make_invalid_token(start);
751*a9fa9459Szrj *pp = p + skip + 1;
752*a9fa9459Szrj return this->make_token(Token::TOKEN_QUOTED_STRING, p, skip, start);
753*a9fa9459Szrj }
754*a9fa9459Szrj
755*a9fa9459Szrj // Return the next token at *PP. Update *PP. General guideline: we
756*a9fa9459Szrj // require linker scripts to be simple ASCII. No unicode linker
757*a9fa9459Szrj // scripts. In particular we can assume that any '\0' is the end of
758*a9fa9459Szrj // the input.
759*a9fa9459Szrj
760*a9fa9459Szrj Token
get_token(const char ** pp)761*a9fa9459Szrj Lex::get_token(const char** pp)
762*a9fa9459Szrj {
763*a9fa9459Szrj const char* p = *pp;
764*a9fa9459Szrj
765*a9fa9459Szrj while (true)
766*a9fa9459Szrj {
767*a9fa9459Szrj if (*p == '\0')
768*a9fa9459Szrj {
769*a9fa9459Szrj *pp = p;
770*a9fa9459Szrj return this->make_eof_token(p);
771*a9fa9459Szrj }
772*a9fa9459Szrj
773*a9fa9459Szrj // Skip whitespace quickly.
774*a9fa9459Szrj while (*p == ' ' || *p == '\t' || *p == '\r')
775*a9fa9459Szrj ++p;
776*a9fa9459Szrj
777*a9fa9459Szrj if (*p == '\n')
778*a9fa9459Szrj {
779*a9fa9459Szrj ++p;
780*a9fa9459Szrj ++this->lineno_;
781*a9fa9459Szrj this->linestart_ = p;
782*a9fa9459Szrj continue;
783*a9fa9459Szrj }
784*a9fa9459Szrj
785*a9fa9459Szrj // Skip C style comments.
786*a9fa9459Szrj if (p[0] == '/' && p[1] == '*')
787*a9fa9459Szrj {
788*a9fa9459Szrj int lineno = this->lineno_;
789*a9fa9459Szrj int charpos = p - this->linestart_ + 1;
790*a9fa9459Szrj
791*a9fa9459Szrj *pp = p + 2;
792*a9fa9459Szrj if (!this->skip_c_comment(pp))
793*a9fa9459Szrj return Token(Token::TOKEN_INVALID, lineno, charpos);
794*a9fa9459Szrj p = *pp;
795*a9fa9459Szrj
796*a9fa9459Szrj continue;
797*a9fa9459Szrj }
798*a9fa9459Szrj
799*a9fa9459Szrj // Skip line comments.
800*a9fa9459Szrj if (*p == '#')
801*a9fa9459Szrj {
802*a9fa9459Szrj *pp = p + 1;
803*a9fa9459Szrj if (!this->skip_line_comment(pp))
804*a9fa9459Szrj return this->make_eof_token(p);
805*a9fa9459Szrj p = *pp;
806*a9fa9459Szrj continue;
807*a9fa9459Szrj }
808*a9fa9459Szrj
809*a9fa9459Szrj // Check for a name.
810*a9fa9459Szrj if (this->can_start_name(p[0], p[1]))
811*a9fa9459Szrj return this->gather_token(Token::TOKEN_STRING,
812*a9fa9459Szrj &Lex::can_continue_name,
813*a9fa9459Szrj p, p + 1, pp);
814*a9fa9459Szrj
815*a9fa9459Szrj // We accept any arbitrary name in double quotes, as long as it
816*a9fa9459Szrj // does not cross a line boundary.
817*a9fa9459Szrj if (*p == '"')
818*a9fa9459Szrj {
819*a9fa9459Szrj *pp = p;
820*a9fa9459Szrj return this->gather_quoted_string(pp);
821*a9fa9459Szrj }
822*a9fa9459Szrj
823*a9fa9459Szrj // Check for a number.
824*a9fa9459Szrj
825*a9fa9459Szrj if (this->can_start_hex(p[0], p[1], p[2]))
826*a9fa9459Szrj return this->gather_token(Token::TOKEN_INTEGER,
827*a9fa9459Szrj &Lex::can_continue_hex,
828*a9fa9459Szrj p, p + 3, pp);
829*a9fa9459Szrj
830*a9fa9459Szrj if (Lex::can_start_number(p[0]))
831*a9fa9459Szrj return this->gather_token(Token::TOKEN_INTEGER,
832*a9fa9459Szrj &Lex::can_continue_number,
833*a9fa9459Szrj p, p + 1, pp);
834*a9fa9459Szrj
835*a9fa9459Szrj // Check for operators.
836*a9fa9459Szrj
837*a9fa9459Szrj int opcode = Lex::three_char_operator(p[0], p[1], p[2]);
838*a9fa9459Szrj if (opcode != 0)
839*a9fa9459Szrj {
840*a9fa9459Szrj *pp = p + 3;
841*a9fa9459Szrj return this->make_token(opcode, p);
842*a9fa9459Szrj }
843*a9fa9459Szrj
844*a9fa9459Szrj opcode = Lex::two_char_operator(p[0], p[1]);
845*a9fa9459Szrj if (opcode != 0)
846*a9fa9459Szrj {
847*a9fa9459Szrj *pp = p + 2;
848*a9fa9459Szrj return this->make_token(opcode, p);
849*a9fa9459Szrj }
850*a9fa9459Szrj
851*a9fa9459Szrj opcode = Lex::one_char_operator(p[0]);
852*a9fa9459Szrj if (opcode != 0)
853*a9fa9459Szrj {
854*a9fa9459Szrj *pp = p + 1;
855*a9fa9459Szrj return this->make_token(opcode, p);
856*a9fa9459Szrj }
857*a9fa9459Szrj
858*a9fa9459Szrj return this->make_token(Token::TOKEN_INVALID, p);
859*a9fa9459Szrj }
860*a9fa9459Szrj }
861*a9fa9459Szrj
862*a9fa9459Szrj // Return the next token.
863*a9fa9459Szrj
864*a9fa9459Szrj const Token*
next_token()865*a9fa9459Szrj Lex::next_token()
866*a9fa9459Szrj {
867*a9fa9459Szrj // The first token is special.
868*a9fa9459Szrj if (this->first_token_ != 0)
869*a9fa9459Szrj {
870*a9fa9459Szrj this->token_ = Token(this->first_token_, 0, 0);
871*a9fa9459Szrj this->first_token_ = 0;
872*a9fa9459Szrj return &this->token_;
873*a9fa9459Szrj }
874*a9fa9459Szrj
875*a9fa9459Szrj this->token_ = this->get_token(&this->current_);
876*a9fa9459Szrj
877*a9fa9459Szrj // Don't let an early null byte fool us into thinking that we've
878*a9fa9459Szrj // reached the end of the file.
879*a9fa9459Szrj if (this->token_.is_eof()
880*a9fa9459Szrj && (static_cast<size_t>(this->current_ - this->input_string_)
881*a9fa9459Szrj < this->input_length_))
882*a9fa9459Szrj this->token_ = this->make_invalid_token(this->current_);
883*a9fa9459Szrj
884*a9fa9459Szrj return &this->token_;
885*a9fa9459Szrj }
886*a9fa9459Szrj
887*a9fa9459Szrj // class Symbol_assignment.
888*a9fa9459Szrj
889*a9fa9459Szrj // Add the symbol to the symbol table. This makes sure the symbol is
890*a9fa9459Szrj // there and defined. The actual value is stored later. We can't
891*a9fa9459Szrj // determine the actual value at this point, because we can't
892*a9fa9459Szrj // necessarily evaluate the expression until all ordinary symbols have
893*a9fa9459Szrj // been finalized.
894*a9fa9459Szrj
895*a9fa9459Szrj // The GNU linker lets symbol assignments in the linker script
896*a9fa9459Szrj // silently override defined symbols in object files. We are
897*a9fa9459Szrj // compatible. FIXME: Should we issue a warning?
898*a9fa9459Szrj
899*a9fa9459Szrj void
add_to_table(Symbol_table * symtab)900*a9fa9459Szrj Symbol_assignment::add_to_table(Symbol_table* symtab)
901*a9fa9459Szrj {
902*a9fa9459Szrj elfcpp::STV vis = this->hidden_ ? elfcpp::STV_HIDDEN : elfcpp::STV_DEFAULT;
903*a9fa9459Szrj this->sym_ = symtab->define_as_constant(this->name_.c_str(),
904*a9fa9459Szrj NULL, // version
905*a9fa9459Szrj (this->is_defsym_
906*a9fa9459Szrj ? Symbol_table::DEFSYM
907*a9fa9459Szrj : Symbol_table::SCRIPT),
908*a9fa9459Szrj 0, // value
909*a9fa9459Szrj 0, // size
910*a9fa9459Szrj elfcpp::STT_NOTYPE,
911*a9fa9459Szrj elfcpp::STB_GLOBAL,
912*a9fa9459Szrj vis,
913*a9fa9459Szrj 0, // nonvis
914*a9fa9459Szrj this->provide_,
915*a9fa9459Szrj true); // force_override
916*a9fa9459Szrj }
917*a9fa9459Szrj
918*a9fa9459Szrj // Finalize a symbol value.
919*a9fa9459Szrj
920*a9fa9459Szrj void
finalize(Symbol_table * symtab,const Layout * layout)921*a9fa9459Szrj Symbol_assignment::finalize(Symbol_table* symtab, const Layout* layout)
922*a9fa9459Szrj {
923*a9fa9459Szrj this->finalize_maybe_dot(symtab, layout, false, 0, NULL);
924*a9fa9459Szrj }
925*a9fa9459Szrj
926*a9fa9459Szrj // Finalize a symbol value which can refer to the dot symbol.
927*a9fa9459Szrj
928*a9fa9459Szrj void
finalize_with_dot(Symbol_table * symtab,const Layout * layout,uint64_t dot_value,Output_section * dot_section)929*a9fa9459Szrj Symbol_assignment::finalize_with_dot(Symbol_table* symtab,
930*a9fa9459Szrj const Layout* layout,
931*a9fa9459Szrj uint64_t dot_value,
932*a9fa9459Szrj Output_section* dot_section)
933*a9fa9459Szrj {
934*a9fa9459Szrj this->finalize_maybe_dot(symtab, layout, true, dot_value, dot_section);
935*a9fa9459Szrj }
936*a9fa9459Szrj
937*a9fa9459Szrj // Finalize a symbol value, internal version.
938*a9fa9459Szrj
939*a9fa9459Szrj void
finalize_maybe_dot(Symbol_table * symtab,const Layout * layout,bool is_dot_available,uint64_t dot_value,Output_section * dot_section)940*a9fa9459Szrj Symbol_assignment::finalize_maybe_dot(Symbol_table* symtab,
941*a9fa9459Szrj const Layout* layout,
942*a9fa9459Szrj bool is_dot_available,
943*a9fa9459Szrj uint64_t dot_value,
944*a9fa9459Szrj Output_section* dot_section)
945*a9fa9459Szrj {
946*a9fa9459Szrj // If we were only supposed to provide this symbol, the sym_ field
947*a9fa9459Szrj // will be NULL if the symbol was not referenced.
948*a9fa9459Szrj if (this->sym_ == NULL)
949*a9fa9459Szrj {
950*a9fa9459Szrj gold_assert(this->provide_);
951*a9fa9459Szrj return;
952*a9fa9459Szrj }
953*a9fa9459Szrj
954*a9fa9459Szrj if (parameters->target().get_size() == 32)
955*a9fa9459Szrj {
956*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
957*a9fa9459Szrj this->sized_finalize<32>(symtab, layout, is_dot_available, dot_value,
958*a9fa9459Szrj dot_section);
959*a9fa9459Szrj #else
960*a9fa9459Szrj gold_unreachable();
961*a9fa9459Szrj #endif
962*a9fa9459Szrj }
963*a9fa9459Szrj else if (parameters->target().get_size() == 64)
964*a9fa9459Szrj {
965*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
966*a9fa9459Szrj this->sized_finalize<64>(symtab, layout, is_dot_available, dot_value,
967*a9fa9459Szrj dot_section);
968*a9fa9459Szrj #else
969*a9fa9459Szrj gold_unreachable();
970*a9fa9459Szrj #endif
971*a9fa9459Szrj }
972*a9fa9459Szrj else
973*a9fa9459Szrj gold_unreachable();
974*a9fa9459Szrj }
975*a9fa9459Szrj
976*a9fa9459Szrj template<int size>
977*a9fa9459Szrj void
sized_finalize(Symbol_table * symtab,const Layout * layout,bool is_dot_available,uint64_t dot_value,Output_section * dot_section)978*a9fa9459Szrj Symbol_assignment::sized_finalize(Symbol_table* symtab, const Layout* layout,
979*a9fa9459Szrj bool is_dot_available, uint64_t dot_value,
980*a9fa9459Szrj Output_section* dot_section)
981*a9fa9459Szrj {
982*a9fa9459Szrj Output_section* section;
983*a9fa9459Szrj elfcpp::STT type = elfcpp::STT_NOTYPE;
984*a9fa9459Szrj elfcpp::STV vis = elfcpp::STV_DEFAULT;
985*a9fa9459Szrj unsigned char nonvis = 0;
986*a9fa9459Szrj uint64_t final_val = this->val_->eval_maybe_dot(symtab, layout, true,
987*a9fa9459Szrj is_dot_available,
988*a9fa9459Szrj dot_value, dot_section,
989*a9fa9459Szrj §ion, NULL, &type,
990*a9fa9459Szrj &vis, &nonvis, false, NULL);
991*a9fa9459Szrj Sized_symbol<size>* ssym = symtab->get_sized_symbol<size>(this->sym_);
992*a9fa9459Szrj ssym->set_value(final_val);
993*a9fa9459Szrj ssym->set_type(type);
994*a9fa9459Szrj ssym->set_visibility(vis);
995*a9fa9459Szrj ssym->set_nonvis(nonvis);
996*a9fa9459Szrj if (section != NULL)
997*a9fa9459Szrj ssym->set_output_section(section);
998*a9fa9459Szrj }
999*a9fa9459Szrj
1000*a9fa9459Szrj // Set the symbol value if the expression yields an absolute value or
1001*a9fa9459Szrj // a value relative to DOT_SECTION.
1002*a9fa9459Szrj
1003*a9fa9459Szrj void
set_if_absolute(Symbol_table * symtab,const Layout * layout,bool is_dot_available,uint64_t dot_value,Output_section * dot_section)1004*a9fa9459Szrj Symbol_assignment::set_if_absolute(Symbol_table* symtab, const Layout* layout,
1005*a9fa9459Szrj bool is_dot_available, uint64_t dot_value,
1006*a9fa9459Szrj Output_section* dot_section)
1007*a9fa9459Szrj {
1008*a9fa9459Szrj if (this->sym_ == NULL)
1009*a9fa9459Szrj return;
1010*a9fa9459Szrj
1011*a9fa9459Szrj Output_section* val_section;
1012*a9fa9459Szrj bool is_valid;
1013*a9fa9459Szrj uint64_t val = this->val_->eval_maybe_dot(symtab, layout, false,
1014*a9fa9459Szrj is_dot_available, dot_value,
1015*a9fa9459Szrj dot_section, &val_section, NULL,
1016*a9fa9459Szrj NULL, NULL, NULL, false, &is_valid);
1017*a9fa9459Szrj if (!is_valid || (val_section != NULL && val_section != dot_section))
1018*a9fa9459Szrj return;
1019*a9fa9459Szrj
1020*a9fa9459Szrj if (parameters->target().get_size() == 32)
1021*a9fa9459Szrj {
1022*a9fa9459Szrj #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
1023*a9fa9459Szrj Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(this->sym_);
1024*a9fa9459Szrj ssym->set_value(val);
1025*a9fa9459Szrj #else
1026*a9fa9459Szrj gold_unreachable();
1027*a9fa9459Szrj #endif
1028*a9fa9459Szrj }
1029*a9fa9459Szrj else if (parameters->target().get_size() == 64)
1030*a9fa9459Szrj {
1031*a9fa9459Szrj #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
1032*a9fa9459Szrj Sized_symbol<64>* ssym = symtab->get_sized_symbol<64>(this->sym_);
1033*a9fa9459Szrj ssym->set_value(val);
1034*a9fa9459Szrj #else
1035*a9fa9459Szrj gold_unreachable();
1036*a9fa9459Szrj #endif
1037*a9fa9459Szrj }
1038*a9fa9459Szrj else
1039*a9fa9459Szrj gold_unreachable();
1040*a9fa9459Szrj if (val_section != NULL)
1041*a9fa9459Szrj this->sym_->set_output_section(val_section);
1042*a9fa9459Szrj }
1043*a9fa9459Szrj
1044*a9fa9459Szrj // Print for debugging.
1045*a9fa9459Szrj
1046*a9fa9459Szrj void
print(FILE * f) const1047*a9fa9459Szrj Symbol_assignment::print(FILE* f) const
1048*a9fa9459Szrj {
1049*a9fa9459Szrj if (this->provide_ && this->hidden_)
1050*a9fa9459Szrj fprintf(f, "PROVIDE_HIDDEN(");
1051*a9fa9459Szrj else if (this->provide_)
1052*a9fa9459Szrj fprintf(f, "PROVIDE(");
1053*a9fa9459Szrj else if (this->hidden_)
1054*a9fa9459Szrj gold_unreachable();
1055*a9fa9459Szrj
1056*a9fa9459Szrj fprintf(f, "%s = ", this->name_.c_str());
1057*a9fa9459Szrj this->val_->print(f);
1058*a9fa9459Szrj
1059*a9fa9459Szrj if (this->provide_ || this->hidden_)
1060*a9fa9459Szrj fprintf(f, ")");
1061*a9fa9459Szrj
1062*a9fa9459Szrj fprintf(f, "\n");
1063*a9fa9459Szrj }
1064*a9fa9459Szrj
1065*a9fa9459Szrj // Class Script_assertion.
1066*a9fa9459Szrj
1067*a9fa9459Szrj // Check the assertion.
1068*a9fa9459Szrj
1069*a9fa9459Szrj void
check(const Symbol_table * symtab,const Layout * layout)1070*a9fa9459Szrj Script_assertion::check(const Symbol_table* symtab, const Layout* layout)
1071*a9fa9459Szrj {
1072*a9fa9459Szrj if (!this->check_->eval(symtab, layout, true))
1073*a9fa9459Szrj gold_error("%s", this->message_.c_str());
1074*a9fa9459Szrj }
1075*a9fa9459Szrj
1076*a9fa9459Szrj // Print for debugging.
1077*a9fa9459Szrj
1078*a9fa9459Szrj void
print(FILE * f) const1079*a9fa9459Szrj Script_assertion::print(FILE* f) const
1080*a9fa9459Szrj {
1081*a9fa9459Szrj fprintf(f, "ASSERT(");
1082*a9fa9459Szrj this->check_->print(f);
1083*a9fa9459Szrj fprintf(f, ", \"%s\")\n", this->message_.c_str());
1084*a9fa9459Szrj }
1085*a9fa9459Szrj
1086*a9fa9459Szrj // Class Script_options.
1087*a9fa9459Szrj
Script_options()1088*a9fa9459Szrj Script_options::Script_options()
1089*a9fa9459Szrj : entry_(), symbol_assignments_(), symbol_definitions_(),
1090*a9fa9459Szrj symbol_references_(), version_script_info_(), script_sections_()
1091*a9fa9459Szrj {
1092*a9fa9459Szrj }
1093*a9fa9459Szrj
1094*a9fa9459Szrj // Returns true if NAME is on the list of symbol assignments waiting
1095*a9fa9459Szrj // to be processed.
1096*a9fa9459Szrj
1097*a9fa9459Szrj bool
is_pending_assignment(const char * name)1098*a9fa9459Szrj Script_options::is_pending_assignment(const char* name)
1099*a9fa9459Szrj {
1100*a9fa9459Szrj for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1101*a9fa9459Szrj p != this->symbol_assignments_.end();
1102*a9fa9459Szrj ++p)
1103*a9fa9459Szrj if ((*p)->name() == name)
1104*a9fa9459Szrj return true;
1105*a9fa9459Szrj return false;
1106*a9fa9459Szrj }
1107*a9fa9459Szrj
1108*a9fa9459Szrj // Add a symbol to be defined.
1109*a9fa9459Szrj
1110*a9fa9459Szrj void
add_symbol_assignment(const char * name,size_t length,bool is_defsym,Expression * value,bool provide,bool hidden)1111*a9fa9459Szrj Script_options::add_symbol_assignment(const char* name, size_t length,
1112*a9fa9459Szrj bool is_defsym, Expression* value,
1113*a9fa9459Szrj bool provide, bool hidden)
1114*a9fa9459Szrj {
1115*a9fa9459Szrj if (length != 1 || name[0] != '.')
1116*a9fa9459Szrj {
1117*a9fa9459Szrj if (this->script_sections_.in_sections_clause())
1118*a9fa9459Szrj {
1119*a9fa9459Szrj gold_assert(!is_defsym);
1120*a9fa9459Szrj this->script_sections_.add_symbol_assignment(name, length, value,
1121*a9fa9459Szrj provide, hidden);
1122*a9fa9459Szrj }
1123*a9fa9459Szrj else
1124*a9fa9459Szrj {
1125*a9fa9459Szrj Symbol_assignment* p = new Symbol_assignment(name, length, is_defsym,
1126*a9fa9459Szrj value, provide, hidden);
1127*a9fa9459Szrj this->symbol_assignments_.push_back(p);
1128*a9fa9459Szrj }
1129*a9fa9459Szrj
1130*a9fa9459Szrj if (!provide)
1131*a9fa9459Szrj {
1132*a9fa9459Szrj std::string n(name, length);
1133*a9fa9459Szrj this->symbol_definitions_.insert(n);
1134*a9fa9459Szrj this->symbol_references_.erase(n);
1135*a9fa9459Szrj }
1136*a9fa9459Szrj }
1137*a9fa9459Szrj else
1138*a9fa9459Szrj {
1139*a9fa9459Szrj if (provide || hidden)
1140*a9fa9459Szrj gold_error(_("invalid use of PROVIDE for dot symbol"));
1141*a9fa9459Szrj
1142*a9fa9459Szrj // The GNU linker permits assignments to dot outside of SECTIONS
1143*a9fa9459Szrj // clauses and treats them as occurring inside, so we don't
1144*a9fa9459Szrj // check in_sections_clause here.
1145*a9fa9459Szrj this->script_sections_.add_dot_assignment(value);
1146*a9fa9459Szrj }
1147*a9fa9459Szrj }
1148*a9fa9459Szrj
1149*a9fa9459Szrj // Add a reference to a symbol.
1150*a9fa9459Szrj
1151*a9fa9459Szrj void
add_symbol_reference(const char * name,size_t length)1152*a9fa9459Szrj Script_options::add_symbol_reference(const char* name, size_t length)
1153*a9fa9459Szrj {
1154*a9fa9459Szrj if (length != 1 || name[0] != '.')
1155*a9fa9459Szrj {
1156*a9fa9459Szrj std::string n(name, length);
1157*a9fa9459Szrj if (this->symbol_definitions_.find(n) == this->symbol_definitions_.end())
1158*a9fa9459Szrj this->symbol_references_.insert(n);
1159*a9fa9459Szrj }
1160*a9fa9459Szrj }
1161*a9fa9459Szrj
1162*a9fa9459Szrj // Add an assertion.
1163*a9fa9459Szrj
1164*a9fa9459Szrj void
add_assertion(Expression * check,const char * message,size_t messagelen)1165*a9fa9459Szrj Script_options::add_assertion(Expression* check, const char* message,
1166*a9fa9459Szrj size_t messagelen)
1167*a9fa9459Szrj {
1168*a9fa9459Szrj if (this->script_sections_.in_sections_clause())
1169*a9fa9459Szrj this->script_sections_.add_assertion(check, message, messagelen);
1170*a9fa9459Szrj else
1171*a9fa9459Szrj {
1172*a9fa9459Szrj Script_assertion* p = new Script_assertion(check, message, messagelen);
1173*a9fa9459Szrj this->assertions_.push_back(p);
1174*a9fa9459Szrj }
1175*a9fa9459Szrj }
1176*a9fa9459Szrj
1177*a9fa9459Szrj // Create sections required by any linker scripts.
1178*a9fa9459Szrj
1179*a9fa9459Szrj void
create_script_sections(Layout * layout)1180*a9fa9459Szrj Script_options::create_script_sections(Layout* layout)
1181*a9fa9459Szrj {
1182*a9fa9459Szrj if (this->saw_sections_clause())
1183*a9fa9459Szrj this->script_sections_.create_sections(layout);
1184*a9fa9459Szrj }
1185*a9fa9459Szrj
1186*a9fa9459Szrj // Add any symbols we are defining to the symbol table.
1187*a9fa9459Szrj
1188*a9fa9459Szrj void
add_symbols_to_table(Symbol_table * symtab)1189*a9fa9459Szrj Script_options::add_symbols_to_table(Symbol_table* symtab)
1190*a9fa9459Szrj {
1191*a9fa9459Szrj for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1192*a9fa9459Szrj p != this->symbol_assignments_.end();
1193*a9fa9459Szrj ++p)
1194*a9fa9459Szrj (*p)->add_to_table(symtab);
1195*a9fa9459Szrj this->script_sections_.add_symbols_to_table(symtab);
1196*a9fa9459Szrj }
1197*a9fa9459Szrj
1198*a9fa9459Szrj // Finalize symbol values. Also check assertions.
1199*a9fa9459Szrj
1200*a9fa9459Szrj void
finalize_symbols(Symbol_table * symtab,const Layout * layout)1201*a9fa9459Szrj Script_options::finalize_symbols(Symbol_table* symtab, const Layout* layout)
1202*a9fa9459Szrj {
1203*a9fa9459Szrj // We finalize the symbols defined in SECTIONS first, because they
1204*a9fa9459Szrj // are the ones which may have changed. This way if symbol outside
1205*a9fa9459Szrj // SECTIONS are defined in terms of symbols inside SECTIONS, they
1206*a9fa9459Szrj // will get the right value.
1207*a9fa9459Szrj this->script_sections_.finalize_symbols(symtab, layout);
1208*a9fa9459Szrj
1209*a9fa9459Szrj for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1210*a9fa9459Szrj p != this->symbol_assignments_.end();
1211*a9fa9459Szrj ++p)
1212*a9fa9459Szrj (*p)->finalize(symtab, layout);
1213*a9fa9459Szrj
1214*a9fa9459Szrj for (Assertions::iterator p = this->assertions_.begin();
1215*a9fa9459Szrj p != this->assertions_.end();
1216*a9fa9459Szrj ++p)
1217*a9fa9459Szrj (*p)->check(symtab, layout);
1218*a9fa9459Szrj }
1219*a9fa9459Szrj
1220*a9fa9459Szrj // Set section addresses. We set all the symbols which have absolute
1221*a9fa9459Szrj // values. Then we let the SECTIONS clause do its thing. This
1222*a9fa9459Szrj // returns the segment which holds the file header and segment
1223*a9fa9459Szrj // headers, if any.
1224*a9fa9459Szrj
1225*a9fa9459Szrj Output_segment*
set_section_addresses(Symbol_table * symtab,Layout * layout)1226*a9fa9459Szrj Script_options::set_section_addresses(Symbol_table* symtab, Layout* layout)
1227*a9fa9459Szrj {
1228*a9fa9459Szrj for (Symbol_assignments::iterator p = this->symbol_assignments_.begin();
1229*a9fa9459Szrj p != this->symbol_assignments_.end();
1230*a9fa9459Szrj ++p)
1231*a9fa9459Szrj (*p)->set_if_absolute(symtab, layout, false, 0, NULL);
1232*a9fa9459Szrj
1233*a9fa9459Szrj return this->script_sections_.set_section_addresses(symtab, layout);
1234*a9fa9459Szrj }
1235*a9fa9459Szrj
1236*a9fa9459Szrj // This class holds data passed through the parser to the lexer and to
1237*a9fa9459Szrj // the parser support functions. This avoids global variables. We
1238*a9fa9459Szrj // can't use global variables because we need not be called by a
1239*a9fa9459Szrj // singleton thread.
1240*a9fa9459Szrj
1241*a9fa9459Szrj class Parser_closure
1242*a9fa9459Szrj {
1243*a9fa9459Szrj public:
Parser_closure(const char * filename,const Position_dependent_options & posdep_options,bool parsing_defsym,bool in_group,bool is_in_sysroot,Command_line * command_line,Script_options * script_options,Lex * lex,bool skip_on_incompatible_target,Script_info * script_info)1244*a9fa9459Szrj Parser_closure(const char* filename,
1245*a9fa9459Szrj const Position_dependent_options& posdep_options,
1246*a9fa9459Szrj bool parsing_defsym, bool in_group, bool is_in_sysroot,
1247*a9fa9459Szrj Command_line* command_line,
1248*a9fa9459Szrj Script_options* script_options,
1249*a9fa9459Szrj Lex* lex,
1250*a9fa9459Szrj bool skip_on_incompatible_target,
1251*a9fa9459Szrj Script_info* script_info)
1252*a9fa9459Szrj : filename_(filename), posdep_options_(posdep_options),
1253*a9fa9459Szrj parsing_defsym_(parsing_defsym), in_group_(in_group),
1254*a9fa9459Szrj is_in_sysroot_(is_in_sysroot),
1255*a9fa9459Szrj skip_on_incompatible_target_(skip_on_incompatible_target),
1256*a9fa9459Szrj found_incompatible_target_(false),
1257*a9fa9459Szrj command_line_(command_line), script_options_(script_options),
1258*a9fa9459Szrj version_script_info_(script_options->version_script_info()),
1259*a9fa9459Szrj lex_(lex), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL),
1260*a9fa9459Szrj script_info_(script_info)
1261*a9fa9459Szrj {
1262*a9fa9459Szrj // We start out processing C symbols in the default lex mode.
1263*a9fa9459Szrj this->language_stack_.push_back(Version_script_info::LANGUAGE_C);
1264*a9fa9459Szrj this->lex_mode_stack_.push_back(lex->mode());
1265*a9fa9459Szrj }
1266*a9fa9459Szrj
1267*a9fa9459Szrj // Return the file name.
1268*a9fa9459Szrj const char*
filename() const1269*a9fa9459Szrj filename() const
1270*a9fa9459Szrj { return this->filename_; }
1271*a9fa9459Szrj
1272*a9fa9459Szrj // Return the position dependent options. The caller may modify
1273*a9fa9459Szrj // this.
1274*a9fa9459Szrj Position_dependent_options&
position_dependent_options()1275*a9fa9459Szrj position_dependent_options()
1276*a9fa9459Szrj { return this->posdep_options_; }
1277*a9fa9459Szrj
1278*a9fa9459Szrj // Whether we are parsing a --defsym.
1279*a9fa9459Szrj bool
parsing_defsym() const1280*a9fa9459Szrj parsing_defsym() const
1281*a9fa9459Szrj { return this->parsing_defsym_; }
1282*a9fa9459Szrj
1283*a9fa9459Szrj // Return whether this script is being run in a group.
1284*a9fa9459Szrj bool
in_group() const1285*a9fa9459Szrj in_group() const
1286*a9fa9459Szrj { return this->in_group_; }
1287*a9fa9459Szrj
1288*a9fa9459Szrj // Return whether this script was found using a directory in the
1289*a9fa9459Szrj // sysroot.
1290*a9fa9459Szrj bool
is_in_sysroot() const1291*a9fa9459Szrj is_in_sysroot() const
1292*a9fa9459Szrj { return this->is_in_sysroot_; }
1293*a9fa9459Szrj
1294*a9fa9459Szrj // Whether to skip to the next file with the same name if we find an
1295*a9fa9459Szrj // incompatible target in an OUTPUT_FORMAT statement.
1296*a9fa9459Szrj bool
skip_on_incompatible_target() const1297*a9fa9459Szrj skip_on_incompatible_target() const
1298*a9fa9459Szrj { return this->skip_on_incompatible_target_; }
1299*a9fa9459Szrj
1300*a9fa9459Szrj // Stop skipping to the next file on an incompatible target. This
1301*a9fa9459Szrj // is called when we make some unrevocable change to the data
1302*a9fa9459Szrj // structures.
1303*a9fa9459Szrj void
clear_skip_on_incompatible_target()1304*a9fa9459Szrj clear_skip_on_incompatible_target()
1305*a9fa9459Szrj { this->skip_on_incompatible_target_ = false; }
1306*a9fa9459Szrj
1307*a9fa9459Szrj // Whether we found an incompatible target in an OUTPUT_FORMAT
1308*a9fa9459Szrj // statement.
1309*a9fa9459Szrj bool
found_incompatible_target() const1310*a9fa9459Szrj found_incompatible_target() const
1311*a9fa9459Szrj { return this->found_incompatible_target_; }
1312*a9fa9459Szrj
1313*a9fa9459Szrj // Note that we found an incompatible target.
1314*a9fa9459Szrj void
set_found_incompatible_target()1315*a9fa9459Szrj set_found_incompatible_target()
1316*a9fa9459Szrj { this->found_incompatible_target_ = true; }
1317*a9fa9459Szrj
1318*a9fa9459Szrj // Returns the Command_line structure passed in at constructor time.
1319*a9fa9459Szrj // This value may be NULL. The caller may modify this, which modifies
1320*a9fa9459Szrj // the passed-in Command_line object (not a copy).
1321*a9fa9459Szrj Command_line*
command_line()1322*a9fa9459Szrj command_line()
1323*a9fa9459Szrj { return this->command_line_; }
1324*a9fa9459Szrj
1325*a9fa9459Szrj // Return the options which may be set by a script.
1326*a9fa9459Szrj Script_options*
script_options()1327*a9fa9459Szrj script_options()
1328*a9fa9459Szrj { return this->script_options_; }
1329*a9fa9459Szrj
1330*a9fa9459Szrj // Return the object in which version script information should be stored.
1331*a9fa9459Szrj Version_script_info*
version_script()1332*a9fa9459Szrj version_script()
1333*a9fa9459Szrj { return this->version_script_info_; }
1334*a9fa9459Szrj
1335*a9fa9459Szrj // Return the next token, and advance.
1336*a9fa9459Szrj const Token*
next_token()1337*a9fa9459Szrj next_token()
1338*a9fa9459Szrj {
1339*a9fa9459Szrj const Token* token = this->lex_->next_token();
1340*a9fa9459Szrj this->lineno_ = token->lineno();
1341*a9fa9459Szrj this->charpos_ = token->charpos();
1342*a9fa9459Szrj return token;
1343*a9fa9459Szrj }
1344*a9fa9459Szrj
1345*a9fa9459Szrj // Set a new lexer mode, pushing the current one.
1346*a9fa9459Szrj void
push_lex_mode(Lex::Mode mode)1347*a9fa9459Szrj push_lex_mode(Lex::Mode mode)
1348*a9fa9459Szrj {
1349*a9fa9459Szrj this->lex_mode_stack_.push_back(this->lex_->mode());
1350*a9fa9459Szrj this->lex_->set_mode(mode);
1351*a9fa9459Szrj }
1352*a9fa9459Szrj
1353*a9fa9459Szrj // Pop the lexer mode.
1354*a9fa9459Szrj void
pop_lex_mode()1355*a9fa9459Szrj pop_lex_mode()
1356*a9fa9459Szrj {
1357*a9fa9459Szrj gold_assert(!this->lex_mode_stack_.empty());
1358*a9fa9459Szrj this->lex_->set_mode(this->lex_mode_stack_.back());
1359*a9fa9459Szrj this->lex_mode_stack_.pop_back();
1360*a9fa9459Szrj }
1361*a9fa9459Szrj
1362*a9fa9459Szrj // Return the current lexer mode.
1363*a9fa9459Szrj Lex::Mode
lex_mode() const1364*a9fa9459Szrj lex_mode() const
1365*a9fa9459Szrj { return this->lex_mode_stack_.back(); }
1366*a9fa9459Szrj
1367*a9fa9459Szrj // Return the line number of the last token.
1368*a9fa9459Szrj int
lineno() const1369*a9fa9459Szrj lineno() const
1370*a9fa9459Szrj { return this->lineno_; }
1371*a9fa9459Szrj
1372*a9fa9459Szrj // Return the character position in the line of the last token.
1373*a9fa9459Szrj int
charpos() const1374*a9fa9459Szrj charpos() const
1375*a9fa9459Szrj { return this->charpos_; }
1376*a9fa9459Szrj
1377*a9fa9459Szrj // Return the list of input files, creating it if necessary. This
1378*a9fa9459Szrj // is a space leak--we never free the INPUTS_ pointer.
1379*a9fa9459Szrj Input_arguments*
inputs()1380*a9fa9459Szrj inputs()
1381*a9fa9459Szrj {
1382*a9fa9459Szrj if (this->inputs_ == NULL)
1383*a9fa9459Szrj this->inputs_ = new Input_arguments();
1384*a9fa9459Szrj return this->inputs_;
1385*a9fa9459Szrj }
1386*a9fa9459Szrj
1387*a9fa9459Szrj // Return whether we saw any input files.
1388*a9fa9459Szrj bool
saw_inputs() const1389*a9fa9459Szrj saw_inputs() const
1390*a9fa9459Szrj { return this->inputs_ != NULL && !this->inputs_->empty(); }
1391*a9fa9459Szrj
1392*a9fa9459Szrj // Return the current language being processed in a version script
1393*a9fa9459Szrj // (eg, "C++"). The empty string represents unmangled C names.
1394*a9fa9459Szrj Version_script_info::Language
get_current_language() const1395*a9fa9459Szrj get_current_language() const
1396*a9fa9459Szrj { return this->language_stack_.back(); }
1397*a9fa9459Szrj
1398*a9fa9459Szrj // Push a language onto the stack when entering an extern block.
1399*a9fa9459Szrj void
push_language(Version_script_info::Language lang)1400*a9fa9459Szrj push_language(Version_script_info::Language lang)
1401*a9fa9459Szrj { this->language_stack_.push_back(lang); }
1402*a9fa9459Szrj
1403*a9fa9459Szrj // Pop a language off of the stack when exiting an extern block.
1404*a9fa9459Szrj void
pop_language()1405*a9fa9459Szrj pop_language()
1406*a9fa9459Szrj {
1407*a9fa9459Szrj gold_assert(!this->language_stack_.empty());
1408*a9fa9459Szrj this->language_stack_.pop_back();
1409*a9fa9459Szrj }
1410*a9fa9459Szrj
1411*a9fa9459Szrj // Return a pointer to the incremental info.
1412*a9fa9459Szrj Script_info*
script_info()1413*a9fa9459Szrj script_info()
1414*a9fa9459Szrj { return this->script_info_; }
1415*a9fa9459Szrj
1416*a9fa9459Szrj private:
1417*a9fa9459Szrj // The name of the file we are reading.
1418*a9fa9459Szrj const char* filename_;
1419*a9fa9459Szrj // The position dependent options.
1420*a9fa9459Szrj Position_dependent_options posdep_options_;
1421*a9fa9459Szrj // True if we are parsing a --defsym.
1422*a9fa9459Szrj bool parsing_defsym_;
1423*a9fa9459Szrj // Whether we are currently in a --start-group/--end-group.
1424*a9fa9459Szrj bool in_group_;
1425*a9fa9459Szrj // Whether the script was found in a sysrooted directory.
1426*a9fa9459Szrj bool is_in_sysroot_;
1427*a9fa9459Szrj // If this is true, then if we find an OUTPUT_FORMAT with an
1428*a9fa9459Szrj // incompatible target, then we tell the parser to abort so that we
1429*a9fa9459Szrj // can search for the next file with the same name.
1430*a9fa9459Szrj bool skip_on_incompatible_target_;
1431*a9fa9459Szrj // True if we found an OUTPUT_FORMAT with an incompatible target.
1432*a9fa9459Szrj bool found_incompatible_target_;
1433*a9fa9459Szrj // May be NULL if the user chooses not to pass one in.
1434*a9fa9459Szrj Command_line* command_line_;
1435*a9fa9459Szrj // Options which may be set from any linker script.
1436*a9fa9459Szrj Script_options* script_options_;
1437*a9fa9459Szrj // Information parsed from a version script.
1438*a9fa9459Szrj Version_script_info* version_script_info_;
1439*a9fa9459Szrj // The lexer.
1440*a9fa9459Szrj Lex* lex_;
1441*a9fa9459Szrj // The line number of the last token returned by next_token.
1442*a9fa9459Szrj int lineno_;
1443*a9fa9459Szrj // The column number of the last token returned by next_token.
1444*a9fa9459Szrj int charpos_;
1445*a9fa9459Szrj // A stack of lexer modes.
1446*a9fa9459Szrj std::vector<Lex::Mode> lex_mode_stack_;
1447*a9fa9459Szrj // A stack of which extern/language block we're inside. Can be C++,
1448*a9fa9459Szrj // java, or empty for C.
1449*a9fa9459Szrj std::vector<Version_script_info::Language> language_stack_;
1450*a9fa9459Szrj // New input files found to add to the link.
1451*a9fa9459Szrj Input_arguments* inputs_;
1452*a9fa9459Szrj // Pointer to incremental linking info.
1453*a9fa9459Szrj Script_info* script_info_;
1454*a9fa9459Szrj };
1455*a9fa9459Szrj
1456*a9fa9459Szrj // FILE was found as an argument on the command line. Try to read it
1457*a9fa9459Szrj // as a script. Return true if the file was handled.
1458*a9fa9459Szrj
1459*a9fa9459Szrj bool
read_input_script(Workqueue * workqueue,Symbol_table * symtab,Layout * layout,Dirsearch * dirsearch,int dirindex,Input_objects * input_objects,Mapfile * mapfile,Input_group * input_group,const Input_argument * input_argument,Input_file * input_file,Task_token * next_blocker,bool * used_next_blocker)1460*a9fa9459Szrj read_input_script(Workqueue* workqueue, Symbol_table* symtab, Layout* layout,
1461*a9fa9459Szrj Dirsearch* dirsearch, int dirindex,
1462*a9fa9459Szrj Input_objects* input_objects, Mapfile* mapfile,
1463*a9fa9459Szrj Input_group* input_group,
1464*a9fa9459Szrj const Input_argument* input_argument,
1465*a9fa9459Szrj Input_file* input_file, Task_token* next_blocker,
1466*a9fa9459Szrj bool* used_next_blocker)
1467*a9fa9459Szrj {
1468*a9fa9459Szrj *used_next_blocker = false;
1469*a9fa9459Szrj
1470*a9fa9459Szrj std::string input_string;
1471*a9fa9459Szrj Lex::read_file(input_file, &input_string);
1472*a9fa9459Szrj
1473*a9fa9459Szrj Lex lex(input_string.c_str(), input_string.length(), PARSING_LINKER_SCRIPT);
1474*a9fa9459Szrj
1475*a9fa9459Szrj Script_info* script_info = NULL;
1476*a9fa9459Szrj if (layout->incremental_inputs() != NULL)
1477*a9fa9459Szrj {
1478*a9fa9459Szrj const std::string& filename = input_file->filename();
1479*a9fa9459Szrj Timespec mtime = input_file->file().get_mtime();
1480*a9fa9459Szrj unsigned int arg_serial = input_argument->file().arg_serial();
1481*a9fa9459Szrj script_info = new Script_info(filename);
1482*a9fa9459Szrj layout->incremental_inputs()->report_script(script_info, arg_serial,
1483*a9fa9459Szrj mtime);
1484*a9fa9459Szrj }
1485*a9fa9459Szrj
1486*a9fa9459Szrj Parser_closure closure(input_file->filename().c_str(),
1487*a9fa9459Szrj input_argument->file().options(),
1488*a9fa9459Szrj false,
1489*a9fa9459Szrj input_group != NULL,
1490*a9fa9459Szrj input_file->is_in_sysroot(),
1491*a9fa9459Szrj NULL,
1492*a9fa9459Szrj layout->script_options(),
1493*a9fa9459Szrj &lex,
1494*a9fa9459Szrj input_file->will_search_for(),
1495*a9fa9459Szrj script_info);
1496*a9fa9459Szrj
1497*a9fa9459Szrj bool old_saw_sections_clause =
1498*a9fa9459Szrj layout->script_options()->saw_sections_clause();
1499*a9fa9459Szrj
1500*a9fa9459Szrj if (yyparse(&closure) != 0)
1501*a9fa9459Szrj {
1502*a9fa9459Szrj if (closure.found_incompatible_target())
1503*a9fa9459Szrj {
1504*a9fa9459Szrj Read_symbols::incompatible_warning(input_argument, input_file);
1505*a9fa9459Szrj Read_symbols::requeue(workqueue, input_objects, symtab, layout,
1506*a9fa9459Szrj dirsearch, dirindex, mapfile, input_argument,
1507*a9fa9459Szrj input_group, next_blocker);
1508*a9fa9459Szrj return true;
1509*a9fa9459Szrj }
1510*a9fa9459Szrj return false;
1511*a9fa9459Szrj }
1512*a9fa9459Szrj
1513*a9fa9459Szrj if (!old_saw_sections_clause
1514*a9fa9459Szrj && layout->script_options()->saw_sections_clause()
1515*a9fa9459Szrj && layout->have_added_input_section())
1516*a9fa9459Szrj gold_error(_("%s: SECTIONS seen after other input files; try -T/--script"),
1517*a9fa9459Szrj input_file->filename().c_str());
1518*a9fa9459Szrj
1519*a9fa9459Szrj if (!closure.saw_inputs())
1520*a9fa9459Szrj return true;
1521*a9fa9459Szrj
1522*a9fa9459Szrj Task_token* this_blocker = NULL;
1523*a9fa9459Szrj for (Input_arguments::const_iterator p = closure.inputs()->begin();
1524*a9fa9459Szrj p != closure.inputs()->end();
1525*a9fa9459Szrj ++p)
1526*a9fa9459Szrj {
1527*a9fa9459Szrj Task_token* nb;
1528*a9fa9459Szrj if (p + 1 == closure.inputs()->end())
1529*a9fa9459Szrj nb = next_blocker;
1530*a9fa9459Szrj else
1531*a9fa9459Szrj {
1532*a9fa9459Szrj nb = new Task_token(true);
1533*a9fa9459Szrj nb->add_blocker();
1534*a9fa9459Szrj }
1535*a9fa9459Szrj workqueue->queue_soon(new Read_symbols(input_objects, symtab,
1536*a9fa9459Szrj layout, dirsearch, 0, mapfile, &*p,
1537*a9fa9459Szrj input_group, NULL, this_blocker, nb));
1538*a9fa9459Szrj this_blocker = nb;
1539*a9fa9459Szrj }
1540*a9fa9459Szrj
1541*a9fa9459Szrj *used_next_blocker = true;
1542*a9fa9459Szrj
1543*a9fa9459Szrj return true;
1544*a9fa9459Szrj }
1545*a9fa9459Szrj
1546*a9fa9459Szrj // Helper function for read_version_script(), read_commandline_script() and
1547*a9fa9459Szrj // script_include_directive(). Processes the given file in the mode indicated
1548*a9fa9459Szrj // by first_token and lex_mode.
1549*a9fa9459Szrj
1550*a9fa9459Szrj static bool
read_script_file(const char * filename,Command_line * cmdline,Script_options * script_options,int first_token,Lex::Mode lex_mode)1551*a9fa9459Szrj read_script_file(const char* filename, Command_line* cmdline,
1552*a9fa9459Szrj Script_options* script_options,
1553*a9fa9459Szrj int first_token, Lex::Mode lex_mode)
1554*a9fa9459Szrj {
1555*a9fa9459Szrj Dirsearch dirsearch;
1556*a9fa9459Szrj std::string name = filename;
1557*a9fa9459Szrj
1558*a9fa9459Szrj // If filename is a relative filename, search for it manually using "." +
1559*a9fa9459Szrj // cmdline->options()->library_path() -- not dirsearch.
1560*a9fa9459Szrj if (!IS_ABSOLUTE_PATH(filename))
1561*a9fa9459Szrj {
1562*a9fa9459Szrj const General_options::Dir_list& search_path =
1563*a9fa9459Szrj cmdline->options().library_path();
1564*a9fa9459Szrj name = Dirsearch::find_file_in_dir_list(name, search_path, ".");
1565*a9fa9459Szrj }
1566*a9fa9459Szrj
1567*a9fa9459Szrj // The file locking code wants to record a Task, but we haven't
1568*a9fa9459Szrj // started the workqueue yet. This is only for debugging purposes,
1569*a9fa9459Szrj // so we invent a fake value.
1570*a9fa9459Szrj const Task* task = reinterpret_cast<const Task*>(-1);
1571*a9fa9459Szrj
1572*a9fa9459Szrj // We don't want this file to be opened in binary mode.
1573*a9fa9459Szrj Position_dependent_options posdep = cmdline->position_dependent_options();
1574*a9fa9459Szrj if (posdep.format_enum() == General_options::OBJECT_FORMAT_BINARY)
1575*a9fa9459Szrj posdep.set_format_enum(General_options::OBJECT_FORMAT_ELF);
1576*a9fa9459Szrj Input_file_argument input_argument(name.c_str(),
1577*a9fa9459Szrj Input_file_argument::INPUT_FILE_TYPE_FILE,
1578*a9fa9459Szrj "", false, posdep);
1579*a9fa9459Szrj Input_file input_file(&input_argument);
1580*a9fa9459Szrj int dummy = 0;
1581*a9fa9459Szrj if (!input_file.open(dirsearch, task, &dummy))
1582*a9fa9459Szrj return false;
1583*a9fa9459Szrj
1584*a9fa9459Szrj std::string input_string;
1585*a9fa9459Szrj Lex::read_file(&input_file, &input_string);
1586*a9fa9459Szrj
1587*a9fa9459Szrj Lex lex(input_string.c_str(), input_string.length(), first_token);
1588*a9fa9459Szrj lex.set_mode(lex_mode);
1589*a9fa9459Szrj
1590*a9fa9459Szrj Parser_closure closure(filename,
1591*a9fa9459Szrj cmdline->position_dependent_options(),
1592*a9fa9459Szrj first_token == Lex::DYNAMIC_LIST,
1593*a9fa9459Szrj false,
1594*a9fa9459Szrj input_file.is_in_sysroot(),
1595*a9fa9459Szrj cmdline,
1596*a9fa9459Szrj script_options,
1597*a9fa9459Szrj &lex,
1598*a9fa9459Szrj false,
1599*a9fa9459Szrj NULL);
1600*a9fa9459Szrj if (yyparse(&closure) != 0)
1601*a9fa9459Szrj {
1602*a9fa9459Szrj input_file.file().unlock(task);
1603*a9fa9459Szrj return false;
1604*a9fa9459Szrj }
1605*a9fa9459Szrj
1606*a9fa9459Szrj input_file.file().unlock(task);
1607*a9fa9459Szrj
1608*a9fa9459Szrj gold_assert(!closure.saw_inputs());
1609*a9fa9459Szrj
1610*a9fa9459Szrj return true;
1611*a9fa9459Szrj }
1612*a9fa9459Szrj
1613*a9fa9459Szrj // FILENAME was found as an argument to --script (-T).
1614*a9fa9459Szrj // Read it as a script, and execute its contents immediately.
1615*a9fa9459Szrj
1616*a9fa9459Szrj bool
read_commandline_script(const char * filename,Command_line * cmdline)1617*a9fa9459Szrj read_commandline_script(const char* filename, Command_line* cmdline)
1618*a9fa9459Szrj {
1619*a9fa9459Szrj return read_script_file(filename, cmdline, &cmdline->script_options(),
1620*a9fa9459Szrj PARSING_LINKER_SCRIPT, Lex::LINKER_SCRIPT);
1621*a9fa9459Szrj }
1622*a9fa9459Szrj
1623*a9fa9459Szrj // FILENAME was found as an argument to --version-script. Read it as
1624*a9fa9459Szrj // a version script, and store its contents in
1625*a9fa9459Szrj // cmdline->script_options()->version_script_info().
1626*a9fa9459Szrj
1627*a9fa9459Szrj bool
read_version_script(const char * filename,Command_line * cmdline)1628*a9fa9459Szrj read_version_script(const char* filename, Command_line* cmdline)
1629*a9fa9459Szrj {
1630*a9fa9459Szrj return read_script_file(filename, cmdline, &cmdline->script_options(),
1631*a9fa9459Szrj PARSING_VERSION_SCRIPT, Lex::VERSION_SCRIPT);
1632*a9fa9459Szrj }
1633*a9fa9459Szrj
1634*a9fa9459Szrj // FILENAME was found as an argument to --dynamic-list. Read it as a
1635*a9fa9459Szrj // list of symbols, and store its contents in DYNAMIC_LIST.
1636*a9fa9459Szrj
1637*a9fa9459Szrj bool
read_dynamic_list(const char * filename,Command_line * cmdline,Script_options * dynamic_list)1638*a9fa9459Szrj read_dynamic_list(const char* filename, Command_line* cmdline,
1639*a9fa9459Szrj Script_options* dynamic_list)
1640*a9fa9459Szrj {
1641*a9fa9459Szrj return read_script_file(filename, cmdline, dynamic_list,
1642*a9fa9459Szrj PARSING_DYNAMIC_LIST, Lex::DYNAMIC_LIST);
1643*a9fa9459Szrj }
1644*a9fa9459Szrj
1645*a9fa9459Szrj // Implement the --defsym option on the command line. Return true if
1646*a9fa9459Szrj // all is well.
1647*a9fa9459Szrj
1648*a9fa9459Szrj bool
define_symbol(const char * definition)1649*a9fa9459Szrj Script_options::define_symbol(const char* definition)
1650*a9fa9459Szrj {
1651*a9fa9459Szrj Lex lex(definition, strlen(definition), PARSING_DEFSYM);
1652*a9fa9459Szrj lex.set_mode(Lex::EXPRESSION);
1653*a9fa9459Szrj
1654*a9fa9459Szrj // Dummy value.
1655*a9fa9459Szrj Position_dependent_options posdep_options;
1656*a9fa9459Szrj
1657*a9fa9459Szrj Parser_closure closure("command line", posdep_options, true,
1658*a9fa9459Szrj false, false, NULL, this, &lex, false, NULL);
1659*a9fa9459Szrj
1660*a9fa9459Szrj if (yyparse(&closure) != 0)
1661*a9fa9459Szrj return false;
1662*a9fa9459Szrj
1663*a9fa9459Szrj gold_assert(!closure.saw_inputs());
1664*a9fa9459Szrj
1665*a9fa9459Szrj return true;
1666*a9fa9459Szrj }
1667*a9fa9459Szrj
1668*a9fa9459Szrj // Print the script to F for debugging.
1669*a9fa9459Szrj
1670*a9fa9459Szrj void
print(FILE * f) const1671*a9fa9459Szrj Script_options::print(FILE* f) const
1672*a9fa9459Szrj {
1673*a9fa9459Szrj fprintf(f, "%s: Dumping linker script\n", program_name);
1674*a9fa9459Szrj
1675*a9fa9459Szrj if (!this->entry_.empty())
1676*a9fa9459Szrj fprintf(f, "ENTRY(%s)\n", this->entry_.c_str());
1677*a9fa9459Szrj
1678*a9fa9459Szrj for (Symbol_assignments::const_iterator p =
1679*a9fa9459Szrj this->symbol_assignments_.begin();
1680*a9fa9459Szrj p != this->symbol_assignments_.end();
1681*a9fa9459Szrj ++p)
1682*a9fa9459Szrj (*p)->print(f);
1683*a9fa9459Szrj
1684*a9fa9459Szrj for (Assertions::const_iterator p = this->assertions_.begin();
1685*a9fa9459Szrj p != this->assertions_.end();
1686*a9fa9459Szrj ++p)
1687*a9fa9459Szrj (*p)->print(f);
1688*a9fa9459Szrj
1689*a9fa9459Szrj this->script_sections_.print(f);
1690*a9fa9459Szrj
1691*a9fa9459Szrj this->version_script_info_.print(f);
1692*a9fa9459Szrj }
1693*a9fa9459Szrj
1694*a9fa9459Szrj // Manage mapping from keywords to the codes expected by the bison
1695*a9fa9459Szrj // parser. We construct one global object for each lex mode with
1696*a9fa9459Szrj // keywords.
1697*a9fa9459Szrj
1698*a9fa9459Szrj class Keyword_to_parsecode
1699*a9fa9459Szrj {
1700*a9fa9459Szrj public:
1701*a9fa9459Szrj // The structure which maps keywords to parsecodes.
1702*a9fa9459Szrj struct Keyword_parsecode
1703*a9fa9459Szrj {
1704*a9fa9459Szrj // Keyword.
1705*a9fa9459Szrj const char* keyword;
1706*a9fa9459Szrj // Corresponding parsecode.
1707*a9fa9459Szrj int parsecode;
1708*a9fa9459Szrj };
1709*a9fa9459Szrj
Keyword_to_parsecode(const Keyword_parsecode * keywords,int keyword_count)1710*a9fa9459Szrj Keyword_to_parsecode(const Keyword_parsecode* keywords,
1711*a9fa9459Szrj int keyword_count)
1712*a9fa9459Szrj : keyword_parsecodes_(keywords), keyword_count_(keyword_count)
1713*a9fa9459Szrj { }
1714*a9fa9459Szrj
1715*a9fa9459Szrj // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1716*a9fa9459Szrj // keyword.
1717*a9fa9459Szrj int
1718*a9fa9459Szrj keyword_to_parsecode(const char* keyword, size_t len) const;
1719*a9fa9459Szrj
1720*a9fa9459Szrj private:
1721*a9fa9459Szrj const Keyword_parsecode* keyword_parsecodes_;
1722*a9fa9459Szrj const int keyword_count_;
1723*a9fa9459Szrj };
1724*a9fa9459Szrj
1725*a9fa9459Szrj // Mapping from keyword string to keyword parsecode. This array must
1726*a9fa9459Szrj // be kept in sorted order. Parsecodes are looked up using bsearch.
1727*a9fa9459Szrj // This array must correspond to the list of parsecodes in yyscript.y.
1728*a9fa9459Szrj
1729*a9fa9459Szrj static const Keyword_to_parsecode::Keyword_parsecode
1730*a9fa9459Szrj script_keyword_parsecodes[] =
1731*a9fa9459Szrj {
1732*a9fa9459Szrj { "ABSOLUTE", ABSOLUTE },
1733*a9fa9459Szrj { "ADDR", ADDR },
1734*a9fa9459Szrj { "ALIGN", ALIGN_K },
1735*a9fa9459Szrj { "ALIGNOF", ALIGNOF },
1736*a9fa9459Szrj { "ASSERT", ASSERT_K },
1737*a9fa9459Szrj { "AS_NEEDED", AS_NEEDED },
1738*a9fa9459Szrj { "AT", AT },
1739*a9fa9459Szrj { "BIND", BIND },
1740*a9fa9459Szrj { "BLOCK", BLOCK },
1741*a9fa9459Szrj { "BYTE", BYTE },
1742*a9fa9459Szrj { "CONSTANT", CONSTANT },
1743*a9fa9459Szrj { "CONSTRUCTORS", CONSTRUCTORS },
1744*a9fa9459Szrj { "COPY", COPY },
1745*a9fa9459Szrj { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS },
1746*a9fa9459Szrj { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN },
1747*a9fa9459Szrj { "DATA_SEGMENT_END", DATA_SEGMENT_END },
1748*a9fa9459Szrj { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END },
1749*a9fa9459Szrj { "DEFINED", DEFINED },
1750*a9fa9459Szrj { "DSECT", DSECT },
1751*a9fa9459Szrj { "ENTRY", ENTRY },
1752*a9fa9459Szrj { "EXCLUDE_FILE", EXCLUDE_FILE },
1753*a9fa9459Szrj { "EXTERN", EXTERN },
1754*a9fa9459Szrj { "FILL", FILL },
1755*a9fa9459Szrj { "FLOAT", FLOAT },
1756*a9fa9459Szrj { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION },
1757*a9fa9459Szrj { "GROUP", GROUP },
1758*a9fa9459Szrj { "HLL", HLL },
1759*a9fa9459Szrj { "INCLUDE", INCLUDE },
1760*a9fa9459Szrj { "INFO", INFO },
1761*a9fa9459Szrj { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION },
1762*a9fa9459Szrj { "INPUT", INPUT },
1763*a9fa9459Szrj { "KEEP", KEEP },
1764*a9fa9459Szrj { "LENGTH", LENGTH },
1765*a9fa9459Szrj { "LOADADDR", LOADADDR },
1766*a9fa9459Szrj { "LONG", LONG },
1767*a9fa9459Szrj { "MAP", MAP },
1768*a9fa9459Szrj { "MAX", MAX_K },
1769*a9fa9459Szrj { "MEMORY", MEMORY },
1770*a9fa9459Szrj { "MIN", MIN_K },
1771*a9fa9459Szrj { "NEXT", NEXT },
1772*a9fa9459Szrj { "NOCROSSREFS", NOCROSSREFS },
1773*a9fa9459Szrj { "NOFLOAT", NOFLOAT },
1774*a9fa9459Szrj { "NOLOAD", NOLOAD },
1775*a9fa9459Szrj { "ONLY_IF_RO", ONLY_IF_RO },
1776*a9fa9459Szrj { "ONLY_IF_RW", ONLY_IF_RW },
1777*a9fa9459Szrj { "OPTION", OPTION },
1778*a9fa9459Szrj { "ORIGIN", ORIGIN },
1779*a9fa9459Szrj { "OUTPUT", OUTPUT },
1780*a9fa9459Szrj { "OUTPUT_ARCH", OUTPUT_ARCH },
1781*a9fa9459Szrj { "OUTPUT_FORMAT", OUTPUT_FORMAT },
1782*a9fa9459Szrj { "OVERLAY", OVERLAY },
1783*a9fa9459Szrj { "PHDRS", PHDRS },
1784*a9fa9459Szrj { "PROVIDE", PROVIDE },
1785*a9fa9459Szrj { "PROVIDE_HIDDEN", PROVIDE_HIDDEN },
1786*a9fa9459Szrj { "QUAD", QUAD },
1787*a9fa9459Szrj { "SEARCH_DIR", SEARCH_DIR },
1788*a9fa9459Szrj { "SECTIONS", SECTIONS },
1789*a9fa9459Szrj { "SEGMENT_START", SEGMENT_START },
1790*a9fa9459Szrj { "SHORT", SHORT },
1791*a9fa9459Szrj { "SIZEOF", SIZEOF },
1792*a9fa9459Szrj { "SIZEOF_HEADERS", SIZEOF_HEADERS },
1793*a9fa9459Szrj { "SORT", SORT_BY_NAME },
1794*a9fa9459Szrj { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT },
1795*a9fa9459Szrj { "SORT_BY_INIT_PRIORITY", SORT_BY_INIT_PRIORITY },
1796*a9fa9459Szrj { "SORT_BY_NAME", SORT_BY_NAME },
1797*a9fa9459Szrj { "SPECIAL", SPECIAL },
1798*a9fa9459Szrj { "SQUAD", SQUAD },
1799*a9fa9459Szrj { "STARTUP", STARTUP },
1800*a9fa9459Szrj { "SUBALIGN", SUBALIGN },
1801*a9fa9459Szrj { "SYSLIB", SYSLIB },
1802*a9fa9459Szrj { "TARGET", TARGET_K },
1803*a9fa9459Szrj { "TRUNCATE", TRUNCATE },
1804*a9fa9459Szrj { "VERSION", VERSIONK },
1805*a9fa9459Szrj { "global", GLOBAL },
1806*a9fa9459Szrj { "l", LENGTH },
1807*a9fa9459Szrj { "len", LENGTH },
1808*a9fa9459Szrj { "local", LOCAL },
1809*a9fa9459Szrj { "o", ORIGIN },
1810*a9fa9459Szrj { "org", ORIGIN },
1811*a9fa9459Szrj { "sizeof_headers", SIZEOF_HEADERS },
1812*a9fa9459Szrj };
1813*a9fa9459Szrj
1814*a9fa9459Szrj static const Keyword_to_parsecode
1815*a9fa9459Szrj script_keywords(&script_keyword_parsecodes[0],
1816*a9fa9459Szrj (sizeof(script_keyword_parsecodes)
1817*a9fa9459Szrj / sizeof(script_keyword_parsecodes[0])));
1818*a9fa9459Szrj
1819*a9fa9459Szrj static const Keyword_to_parsecode::Keyword_parsecode
1820*a9fa9459Szrj version_script_keyword_parsecodes[] =
1821*a9fa9459Szrj {
1822*a9fa9459Szrj { "extern", EXTERN },
1823*a9fa9459Szrj { "global", GLOBAL },
1824*a9fa9459Szrj { "local", LOCAL },
1825*a9fa9459Szrj };
1826*a9fa9459Szrj
1827*a9fa9459Szrj static const Keyword_to_parsecode
1828*a9fa9459Szrj version_script_keywords(&version_script_keyword_parsecodes[0],
1829*a9fa9459Szrj (sizeof(version_script_keyword_parsecodes)
1830*a9fa9459Szrj / sizeof(version_script_keyword_parsecodes[0])));
1831*a9fa9459Szrj
1832*a9fa9459Szrj static const Keyword_to_parsecode::Keyword_parsecode
1833*a9fa9459Szrj dynamic_list_keyword_parsecodes[] =
1834*a9fa9459Szrj {
1835*a9fa9459Szrj { "extern", EXTERN },
1836*a9fa9459Szrj };
1837*a9fa9459Szrj
1838*a9fa9459Szrj static const Keyword_to_parsecode
1839*a9fa9459Szrj dynamic_list_keywords(&dynamic_list_keyword_parsecodes[0],
1840*a9fa9459Szrj (sizeof(dynamic_list_keyword_parsecodes)
1841*a9fa9459Szrj / sizeof(dynamic_list_keyword_parsecodes[0])));
1842*a9fa9459Szrj
1843*a9fa9459Szrj
1844*a9fa9459Szrj
1845*a9fa9459Szrj // Comparison function passed to bsearch.
1846*a9fa9459Szrj
1847*a9fa9459Szrj extern "C"
1848*a9fa9459Szrj {
1849*a9fa9459Szrj
1850*a9fa9459Szrj struct Ktt_key
1851*a9fa9459Szrj {
1852*a9fa9459Szrj const char* str;
1853*a9fa9459Szrj size_t len;
1854*a9fa9459Szrj };
1855*a9fa9459Szrj
1856*a9fa9459Szrj static int
ktt_compare(const void * keyv,const void * kttv)1857*a9fa9459Szrj ktt_compare(const void* keyv, const void* kttv)
1858*a9fa9459Szrj {
1859*a9fa9459Szrj const Ktt_key* key = static_cast<const Ktt_key*>(keyv);
1860*a9fa9459Szrj const Keyword_to_parsecode::Keyword_parsecode* ktt =
1861*a9fa9459Szrj static_cast<const Keyword_to_parsecode::Keyword_parsecode*>(kttv);
1862*a9fa9459Szrj int i = strncmp(key->str, ktt->keyword, key->len);
1863*a9fa9459Szrj if (i != 0)
1864*a9fa9459Szrj return i;
1865*a9fa9459Szrj if (ktt->keyword[key->len] != '\0')
1866*a9fa9459Szrj return -1;
1867*a9fa9459Szrj return 0;
1868*a9fa9459Szrj }
1869*a9fa9459Szrj
1870*a9fa9459Szrj } // End extern "C".
1871*a9fa9459Szrj
1872*a9fa9459Szrj int
keyword_to_parsecode(const char * keyword,size_t len) const1873*a9fa9459Szrj Keyword_to_parsecode::keyword_to_parsecode(const char* keyword,
1874*a9fa9459Szrj size_t len) const
1875*a9fa9459Szrj {
1876*a9fa9459Szrj Ktt_key key;
1877*a9fa9459Szrj key.str = keyword;
1878*a9fa9459Szrj key.len = len;
1879*a9fa9459Szrj void* kttv = bsearch(&key,
1880*a9fa9459Szrj this->keyword_parsecodes_,
1881*a9fa9459Szrj this->keyword_count_,
1882*a9fa9459Szrj sizeof(this->keyword_parsecodes_[0]),
1883*a9fa9459Szrj ktt_compare);
1884*a9fa9459Szrj if (kttv == NULL)
1885*a9fa9459Szrj return 0;
1886*a9fa9459Szrj Keyword_parsecode* ktt = static_cast<Keyword_parsecode*>(kttv);
1887*a9fa9459Szrj return ktt->parsecode;
1888*a9fa9459Szrj }
1889*a9fa9459Szrj
1890*a9fa9459Szrj // The following structs are used within the VersionInfo class as well
1891*a9fa9459Szrj // as in the bison helper functions. They store the information
1892*a9fa9459Szrj // parsed from the version script.
1893*a9fa9459Szrj
1894*a9fa9459Szrj // A single version expression.
1895*a9fa9459Szrj // For example, pattern="std::map*" and language="C++".
1896*a9fa9459Szrj struct Version_expression
1897*a9fa9459Szrj {
Version_expressiongold::Version_expression1898*a9fa9459Szrj Version_expression(const std::string& a_pattern,
1899*a9fa9459Szrj Version_script_info::Language a_language,
1900*a9fa9459Szrj bool a_exact_match)
1901*a9fa9459Szrj : pattern(a_pattern), language(a_language), exact_match(a_exact_match),
1902*a9fa9459Szrj was_matched_by_symbol(false)
1903*a9fa9459Szrj { }
1904*a9fa9459Szrj
1905*a9fa9459Szrj std::string pattern;
1906*a9fa9459Szrj Version_script_info::Language language;
1907*a9fa9459Szrj // If false, we use glob() to match pattern. If true, we use strcmp().
1908*a9fa9459Szrj bool exact_match;
1909*a9fa9459Szrj // True if --no-undefined-version is in effect and we found this
1910*a9fa9459Szrj // version in get_symbol_version. We use mutable because this
1911*a9fa9459Szrj // struct is generally not modifiable after it has been created.
1912*a9fa9459Szrj mutable bool was_matched_by_symbol;
1913*a9fa9459Szrj };
1914*a9fa9459Szrj
1915*a9fa9459Szrj // A list of expressions.
1916*a9fa9459Szrj struct Version_expression_list
1917*a9fa9459Szrj {
1918*a9fa9459Szrj std::vector<struct Version_expression> expressions;
1919*a9fa9459Szrj };
1920*a9fa9459Szrj
1921*a9fa9459Szrj // A list of which versions upon which another version depends.
1922*a9fa9459Szrj // Strings should be from the Stringpool.
1923*a9fa9459Szrj struct Version_dependency_list
1924*a9fa9459Szrj {
1925*a9fa9459Szrj std::vector<std::string> dependencies;
1926*a9fa9459Szrj };
1927*a9fa9459Szrj
1928*a9fa9459Szrj // The total definition of a version. It includes the tag for the
1929*a9fa9459Szrj // version, its global and local expressions, and any dependencies.
1930*a9fa9459Szrj struct Version_tree
1931*a9fa9459Szrj {
Version_treegold::Version_tree1932*a9fa9459Szrj Version_tree()
1933*a9fa9459Szrj : tag(), global(NULL), local(NULL), dependencies(NULL)
1934*a9fa9459Szrj { }
1935*a9fa9459Szrj
1936*a9fa9459Szrj std::string tag;
1937*a9fa9459Szrj const struct Version_expression_list* global;
1938*a9fa9459Szrj const struct Version_expression_list* local;
1939*a9fa9459Szrj const struct Version_dependency_list* dependencies;
1940*a9fa9459Szrj };
1941*a9fa9459Szrj
1942*a9fa9459Szrj // Helper class that calls cplus_demangle when needed and takes care of freeing
1943*a9fa9459Szrj // the result.
1944*a9fa9459Szrj
1945*a9fa9459Szrj class Lazy_demangler
1946*a9fa9459Szrj {
1947*a9fa9459Szrj public:
Lazy_demangler(const char * symbol,int options)1948*a9fa9459Szrj Lazy_demangler(const char* symbol, int options)
1949*a9fa9459Szrj : symbol_(symbol), options_(options), demangled_(NULL), did_demangle_(false)
1950*a9fa9459Szrj { }
1951*a9fa9459Szrj
~Lazy_demangler()1952*a9fa9459Szrj ~Lazy_demangler()
1953*a9fa9459Szrj { free(this->demangled_); }
1954*a9fa9459Szrj
1955*a9fa9459Szrj // Return the demangled name. The actual demangling happens on the first call,
1956*a9fa9459Szrj // and the result is later cached.
1957*a9fa9459Szrj inline char*
1958*a9fa9459Szrj get();
1959*a9fa9459Szrj
1960*a9fa9459Szrj private:
1961*a9fa9459Szrj // The symbol to demangle.
1962*a9fa9459Szrj const char* symbol_;
1963*a9fa9459Szrj // Option flags to pass to cplus_demagle.
1964*a9fa9459Szrj const int options_;
1965*a9fa9459Szrj // The cached demangled value, or NULL if demangling didn't happen yet or
1966*a9fa9459Szrj // failed.
1967*a9fa9459Szrj char* demangled_;
1968*a9fa9459Szrj // Whether we already called cplus_demangle
1969*a9fa9459Szrj bool did_demangle_;
1970*a9fa9459Szrj };
1971*a9fa9459Szrj
1972*a9fa9459Szrj // Return the demangled name. The actual demangling happens on the first call,
1973*a9fa9459Szrj // and the result is later cached. Returns NULL if the symbol cannot be
1974*a9fa9459Szrj // demangled.
1975*a9fa9459Szrj
1976*a9fa9459Szrj inline char*
get()1977*a9fa9459Szrj Lazy_demangler::get()
1978*a9fa9459Szrj {
1979*a9fa9459Szrj if (!this->did_demangle_)
1980*a9fa9459Szrj {
1981*a9fa9459Szrj this->demangled_ = cplus_demangle(this->symbol_, this->options_);
1982*a9fa9459Szrj this->did_demangle_ = true;
1983*a9fa9459Szrj }
1984*a9fa9459Szrj return this->demangled_;
1985*a9fa9459Szrj }
1986*a9fa9459Szrj
1987*a9fa9459Szrj // Class Version_script_info.
1988*a9fa9459Szrj
Version_script_info()1989*a9fa9459Szrj Version_script_info::Version_script_info()
1990*a9fa9459Szrj : dependency_lists_(), expression_lists_(), version_trees_(), globs_(),
1991*a9fa9459Szrj default_version_(NULL), default_is_global_(false), is_finalized_(false)
1992*a9fa9459Szrj {
1993*a9fa9459Szrj for (int i = 0; i < LANGUAGE_COUNT; ++i)
1994*a9fa9459Szrj this->exact_[i] = NULL;
1995*a9fa9459Szrj }
1996*a9fa9459Szrj
~Version_script_info()1997*a9fa9459Szrj Version_script_info::~Version_script_info()
1998*a9fa9459Szrj {
1999*a9fa9459Szrj }
2000*a9fa9459Szrj
2001*a9fa9459Szrj // Forget all the known version script information.
2002*a9fa9459Szrj
2003*a9fa9459Szrj void
clear()2004*a9fa9459Szrj Version_script_info::clear()
2005*a9fa9459Szrj {
2006*a9fa9459Szrj for (size_t k = 0; k < this->dependency_lists_.size(); ++k)
2007*a9fa9459Szrj delete this->dependency_lists_[k];
2008*a9fa9459Szrj this->dependency_lists_.clear();
2009*a9fa9459Szrj for (size_t k = 0; k < this->version_trees_.size(); ++k)
2010*a9fa9459Szrj delete this->version_trees_[k];
2011*a9fa9459Szrj this->version_trees_.clear();
2012*a9fa9459Szrj for (size_t k = 0; k < this->expression_lists_.size(); ++k)
2013*a9fa9459Szrj delete this->expression_lists_[k];
2014*a9fa9459Szrj this->expression_lists_.clear();
2015*a9fa9459Szrj }
2016*a9fa9459Szrj
2017*a9fa9459Szrj // Finalize the version script information.
2018*a9fa9459Szrj
2019*a9fa9459Szrj void
finalize()2020*a9fa9459Szrj Version_script_info::finalize()
2021*a9fa9459Szrj {
2022*a9fa9459Szrj if (!this->is_finalized_)
2023*a9fa9459Szrj {
2024*a9fa9459Szrj this->build_lookup_tables();
2025*a9fa9459Szrj this->is_finalized_ = true;
2026*a9fa9459Szrj }
2027*a9fa9459Szrj }
2028*a9fa9459Szrj
2029*a9fa9459Szrj // Return all the versions.
2030*a9fa9459Szrj
2031*a9fa9459Szrj std::vector<std::string>
get_versions() const2032*a9fa9459Szrj Version_script_info::get_versions() const
2033*a9fa9459Szrj {
2034*a9fa9459Szrj std::vector<std::string> ret;
2035*a9fa9459Szrj for (size_t j = 0; j < this->version_trees_.size(); ++j)
2036*a9fa9459Szrj if (!this->version_trees_[j]->tag.empty())
2037*a9fa9459Szrj ret.push_back(this->version_trees_[j]->tag);
2038*a9fa9459Szrj return ret;
2039*a9fa9459Szrj }
2040*a9fa9459Szrj
2041*a9fa9459Szrj // Return the dependencies of VERSION.
2042*a9fa9459Szrj
2043*a9fa9459Szrj std::vector<std::string>
get_dependencies(const char * version) const2044*a9fa9459Szrj Version_script_info::get_dependencies(const char* version) const
2045*a9fa9459Szrj {
2046*a9fa9459Szrj std::vector<std::string> ret;
2047*a9fa9459Szrj for (size_t j = 0; j < this->version_trees_.size(); ++j)
2048*a9fa9459Szrj if (this->version_trees_[j]->tag == version)
2049*a9fa9459Szrj {
2050*a9fa9459Szrj const struct Version_dependency_list* deps =
2051*a9fa9459Szrj this->version_trees_[j]->dependencies;
2052*a9fa9459Szrj if (deps != NULL)
2053*a9fa9459Szrj for (size_t k = 0; k < deps->dependencies.size(); ++k)
2054*a9fa9459Szrj ret.push_back(deps->dependencies[k]);
2055*a9fa9459Szrj return ret;
2056*a9fa9459Szrj }
2057*a9fa9459Szrj return ret;
2058*a9fa9459Szrj }
2059*a9fa9459Szrj
2060*a9fa9459Szrj // A version script essentially maps a symbol name to a version tag
2061*a9fa9459Szrj // and an indication of whether symbol is global or local within that
2062*a9fa9459Szrj // version tag. Each symbol maps to at most one version tag.
2063*a9fa9459Szrj // Unfortunately, in practice, version scripts are ambiguous, and list
2064*a9fa9459Szrj // symbols multiple times. Thus, we have to document the matching
2065*a9fa9459Szrj // process.
2066*a9fa9459Szrj
2067*a9fa9459Szrj // This is a description of what the GNU linker does as of 2010-01-11.
2068*a9fa9459Szrj // It walks through the version tags in the order in which they appear
2069*a9fa9459Szrj // in the version script. For each tag, it first walks through the
2070*a9fa9459Szrj // global patterns for that tag, then the local patterns. When
2071*a9fa9459Szrj // looking at a single pattern, it first applies any language specific
2072*a9fa9459Szrj // demangling as specified for the pattern, and then matches the
2073*a9fa9459Szrj // resulting symbol name to the pattern. If it finds an exact match
2074*a9fa9459Szrj // for a literal pattern (a pattern enclosed in quotes or with no
2075*a9fa9459Szrj // wildcard characters), then that is the match that it uses. If
2076*a9fa9459Szrj // finds a match with a wildcard pattern, then it saves it and
2077*a9fa9459Szrj // continues searching. Wildcard patterns that are exactly "*" are
2078*a9fa9459Szrj // saved separately.
2079*a9fa9459Szrj
2080*a9fa9459Szrj // If no exact match with a literal pattern is ever found, then if a
2081*a9fa9459Szrj // wildcard match with a global pattern was found it is used,
2082*a9fa9459Szrj // otherwise if a wildcard match with a local pattern was found it is
2083*a9fa9459Szrj // used.
2084*a9fa9459Szrj
2085*a9fa9459Szrj // This is the result:
2086*a9fa9459Szrj // * If there is an exact match, then we use the first tag in the
2087*a9fa9459Szrj // version script where it matches.
2088*a9fa9459Szrj // + If the exact match in that tag is global, it is used.
2089*a9fa9459Szrj // + Otherwise the exact match in that tag is local, and is used.
2090*a9fa9459Szrj // * Otherwise, if there is any match with a global wildcard pattern:
2091*a9fa9459Szrj // + If there is any match with a wildcard pattern which is not
2092*a9fa9459Szrj // "*", then we use the tag in which the *last* such pattern
2093*a9fa9459Szrj // appears.
2094*a9fa9459Szrj // + Otherwise, we matched "*". If there is no match with a local
2095*a9fa9459Szrj // wildcard pattern which is not "*", then we use the *last*
2096*a9fa9459Szrj // match with a global "*". Otherwise, continue.
2097*a9fa9459Szrj // * Otherwise, if there is any match with a local wildcard pattern:
2098*a9fa9459Szrj // + If there is any match with a wildcard pattern which is not
2099*a9fa9459Szrj // "*", then we use the tag in which the *last* such pattern
2100*a9fa9459Szrj // appears.
2101*a9fa9459Szrj // + Otherwise, we matched "*", and we use the tag in which the
2102*a9fa9459Szrj // *last* such match occurred.
2103*a9fa9459Szrj
2104*a9fa9459Szrj // There is an additional wrinkle. When the GNU linker finds a symbol
2105*a9fa9459Szrj // with a version defined in an object file due to a .symver
2106*a9fa9459Szrj // directive, it looks up that symbol name in that version tag. If it
2107*a9fa9459Szrj // finds it, it matches the symbol name against the patterns for that
2108*a9fa9459Szrj // version. If there is no match with a global pattern, but there is
2109*a9fa9459Szrj // a match with a local pattern, then the GNU linker marks the symbol
2110*a9fa9459Szrj // as local.
2111*a9fa9459Szrj
2112*a9fa9459Szrj // We want gold to be generally compatible, but we also want gold to
2113*a9fa9459Szrj // be fast. These are the rules that gold implements:
2114*a9fa9459Szrj // * If there is an exact match for the mangled name, we use it.
2115*a9fa9459Szrj // + If there is more than one exact match, we give a warning, and
2116*a9fa9459Szrj // we use the first tag in the script which matches.
2117*a9fa9459Szrj // + If a symbol has an exact match as both global and local for
2118*a9fa9459Szrj // the same version tag, we give an error.
2119*a9fa9459Szrj // * Otherwise, we look for an extern C++ or an extern Java exact
2120*a9fa9459Szrj // match. If we find an exact match, we use it.
2121*a9fa9459Szrj // + If there is more than one exact match, we give a warning, and
2122*a9fa9459Szrj // we use the first tag in the script which matches.
2123*a9fa9459Szrj // + If a symbol has an exact match as both global and local for
2124*a9fa9459Szrj // the same version tag, we give an error.
2125*a9fa9459Szrj // * Otherwise, we look through the wildcard patterns, ignoring "*"
2126*a9fa9459Szrj // patterns. We look through the version tags in reverse order.
2127*a9fa9459Szrj // For each version tag, we look through the global patterns and
2128*a9fa9459Szrj // then the local patterns. We use the first match we find (i.e.,
2129*a9fa9459Szrj // the last matching version tag in the file).
2130*a9fa9459Szrj // * Otherwise, we use the "*" pattern if there is one. We give an
2131*a9fa9459Szrj // error if there are multiple "*" patterns.
2132*a9fa9459Szrj
2133*a9fa9459Szrj // At least for now, gold does not look up the version tag for a
2134*a9fa9459Szrj // symbol version found in an object file to see if it should be
2135*a9fa9459Szrj // forced local. There are other ways to force a symbol to be local,
2136*a9fa9459Szrj // and I don't understand why this one is useful.
2137*a9fa9459Szrj
2138*a9fa9459Szrj // Build a set of fast lookup tables for a version script.
2139*a9fa9459Szrj
2140*a9fa9459Szrj void
build_lookup_tables()2141*a9fa9459Szrj Version_script_info::build_lookup_tables()
2142*a9fa9459Szrj {
2143*a9fa9459Szrj size_t size = this->version_trees_.size();
2144*a9fa9459Szrj for (size_t j = 0; j < size; ++j)
2145*a9fa9459Szrj {
2146*a9fa9459Szrj const Version_tree* v = this->version_trees_[j];
2147*a9fa9459Szrj this->build_expression_list_lookup(v->local, v, false);
2148*a9fa9459Szrj this->build_expression_list_lookup(v->global, v, true);
2149*a9fa9459Szrj }
2150*a9fa9459Szrj }
2151*a9fa9459Szrj
2152*a9fa9459Szrj // If a pattern has backlashes but no unquoted wildcard characters,
2153*a9fa9459Szrj // then we apply backslash unquoting and look for an exact match.
2154*a9fa9459Szrj // Otherwise we treat it as a wildcard pattern. This function returns
2155*a9fa9459Szrj // true for a wildcard pattern. Otherwise, it does backslash
2156*a9fa9459Szrj // unquoting on *PATTERN and returns false. If this returns true,
2157*a9fa9459Szrj // *PATTERN may have been partially unquoted.
2158*a9fa9459Szrj
2159*a9fa9459Szrj bool
unquote(std::string * pattern) const2160*a9fa9459Szrj Version_script_info::unquote(std::string* pattern) const
2161*a9fa9459Szrj {
2162*a9fa9459Szrj bool saw_backslash = false;
2163*a9fa9459Szrj size_t len = pattern->length();
2164*a9fa9459Szrj size_t j = 0;
2165*a9fa9459Szrj for (size_t i = 0; i < len; ++i)
2166*a9fa9459Szrj {
2167*a9fa9459Szrj if (saw_backslash)
2168*a9fa9459Szrj saw_backslash = false;
2169*a9fa9459Szrj else
2170*a9fa9459Szrj {
2171*a9fa9459Szrj switch ((*pattern)[i])
2172*a9fa9459Szrj {
2173*a9fa9459Szrj case '?': case '[': case '*':
2174*a9fa9459Szrj return true;
2175*a9fa9459Szrj case '\\':
2176*a9fa9459Szrj saw_backslash = true;
2177*a9fa9459Szrj continue;
2178*a9fa9459Szrj default:
2179*a9fa9459Szrj break;
2180*a9fa9459Szrj }
2181*a9fa9459Szrj }
2182*a9fa9459Szrj
2183*a9fa9459Szrj if (i != j)
2184*a9fa9459Szrj (*pattern)[j] = (*pattern)[i];
2185*a9fa9459Szrj ++j;
2186*a9fa9459Szrj }
2187*a9fa9459Szrj return false;
2188*a9fa9459Szrj }
2189*a9fa9459Szrj
2190*a9fa9459Szrj // Add an exact match for MATCH to *PE. The result of the match is
2191*a9fa9459Szrj // V/IS_GLOBAL.
2192*a9fa9459Szrj
2193*a9fa9459Szrj void
add_exact_match(const std::string & match,const Version_tree * v,bool is_global,const Version_expression * ve,Exact * pe)2194*a9fa9459Szrj Version_script_info::add_exact_match(const std::string& match,
2195*a9fa9459Szrj const Version_tree* v, bool is_global,
2196*a9fa9459Szrj const Version_expression* ve,
2197*a9fa9459Szrj Exact* pe)
2198*a9fa9459Szrj {
2199*a9fa9459Szrj std::pair<Exact::iterator, bool> ins =
2200*a9fa9459Szrj pe->insert(std::make_pair(match, Version_tree_match(v, is_global, ve)));
2201*a9fa9459Szrj if (ins.second)
2202*a9fa9459Szrj {
2203*a9fa9459Szrj // This is the first time we have seen this match.
2204*a9fa9459Szrj return;
2205*a9fa9459Szrj }
2206*a9fa9459Szrj
2207*a9fa9459Szrj Version_tree_match& vtm(ins.first->second);
2208*a9fa9459Szrj if (vtm.real->tag != v->tag)
2209*a9fa9459Szrj {
2210*a9fa9459Szrj // This is an ambiguous match. We still return the
2211*a9fa9459Szrj // first version that we found in the script, but we
2212*a9fa9459Szrj // record the new version to issue a warning if we
2213*a9fa9459Szrj // wind up looking up this symbol.
2214*a9fa9459Szrj if (vtm.ambiguous == NULL)
2215*a9fa9459Szrj vtm.ambiguous = v;
2216*a9fa9459Szrj }
2217*a9fa9459Szrj else if (is_global != vtm.is_global)
2218*a9fa9459Szrj {
2219*a9fa9459Szrj // We have a match for both the global and local entries for a
2220*a9fa9459Szrj // version tag. That's got to be wrong.
2221*a9fa9459Szrj gold_error(_("'%s' appears as both a global and a local symbol "
2222*a9fa9459Szrj "for version '%s' in script"),
2223*a9fa9459Szrj match.c_str(), v->tag.c_str());
2224*a9fa9459Szrj }
2225*a9fa9459Szrj }
2226*a9fa9459Szrj
2227*a9fa9459Szrj // Build fast lookup information for EXPLIST and store it in LOOKUP.
2228*a9fa9459Szrj // All matches go to V, and IS_GLOBAL is true if they are global
2229*a9fa9459Szrj // matches.
2230*a9fa9459Szrj
2231*a9fa9459Szrj void
build_expression_list_lookup(const Version_expression_list * explist,const Version_tree * v,bool is_global)2232*a9fa9459Szrj Version_script_info::build_expression_list_lookup(
2233*a9fa9459Szrj const Version_expression_list* explist,
2234*a9fa9459Szrj const Version_tree* v,
2235*a9fa9459Szrj bool is_global)
2236*a9fa9459Szrj {
2237*a9fa9459Szrj if (explist == NULL)
2238*a9fa9459Szrj return;
2239*a9fa9459Szrj size_t size = explist->expressions.size();
2240*a9fa9459Szrj for (size_t i = 0; i < size; ++i)
2241*a9fa9459Szrj {
2242*a9fa9459Szrj const Version_expression& exp(explist->expressions[i]);
2243*a9fa9459Szrj
2244*a9fa9459Szrj if (exp.pattern.length() == 1 && exp.pattern[0] == '*')
2245*a9fa9459Szrj {
2246*a9fa9459Szrj if (this->default_version_ != NULL
2247*a9fa9459Szrj && this->default_version_->tag != v->tag)
2248*a9fa9459Szrj gold_warning(_("wildcard match appears in both version '%s' "
2249*a9fa9459Szrj "and '%s' in script"),
2250*a9fa9459Szrj this->default_version_->tag.c_str(), v->tag.c_str());
2251*a9fa9459Szrj else if (this->default_version_ != NULL
2252*a9fa9459Szrj && this->default_is_global_ != is_global)
2253*a9fa9459Szrj gold_error(_("wildcard match appears as both global and local "
2254*a9fa9459Szrj "in version '%s' in script"),
2255*a9fa9459Szrj v->tag.c_str());
2256*a9fa9459Szrj this->default_version_ = v;
2257*a9fa9459Szrj this->default_is_global_ = is_global;
2258*a9fa9459Szrj continue;
2259*a9fa9459Szrj }
2260*a9fa9459Szrj
2261*a9fa9459Szrj std::string pattern = exp.pattern;
2262*a9fa9459Szrj if (!exp.exact_match)
2263*a9fa9459Szrj {
2264*a9fa9459Szrj if (this->unquote(&pattern))
2265*a9fa9459Szrj {
2266*a9fa9459Szrj this->globs_.push_back(Glob(&exp, v, is_global));
2267*a9fa9459Szrj continue;
2268*a9fa9459Szrj }
2269*a9fa9459Szrj }
2270*a9fa9459Szrj
2271*a9fa9459Szrj if (this->exact_[exp.language] == NULL)
2272*a9fa9459Szrj this->exact_[exp.language] = new Exact();
2273*a9fa9459Szrj this->add_exact_match(pattern, v, is_global, &exp,
2274*a9fa9459Szrj this->exact_[exp.language]);
2275*a9fa9459Szrj }
2276*a9fa9459Szrj }
2277*a9fa9459Szrj
2278*a9fa9459Szrj // Return the name to match given a name, a language code, and two
2279*a9fa9459Szrj // lazy demanglers.
2280*a9fa9459Szrj
2281*a9fa9459Szrj const char*
get_name_to_match(const char * name,int language,Lazy_demangler * cpp_demangler,Lazy_demangler * java_demangler) const2282*a9fa9459Szrj Version_script_info::get_name_to_match(const char* name,
2283*a9fa9459Szrj int language,
2284*a9fa9459Szrj Lazy_demangler* cpp_demangler,
2285*a9fa9459Szrj Lazy_demangler* java_demangler) const
2286*a9fa9459Szrj {
2287*a9fa9459Szrj switch (language)
2288*a9fa9459Szrj {
2289*a9fa9459Szrj case LANGUAGE_C:
2290*a9fa9459Szrj return name;
2291*a9fa9459Szrj case LANGUAGE_CXX:
2292*a9fa9459Szrj return cpp_demangler->get();
2293*a9fa9459Szrj case LANGUAGE_JAVA:
2294*a9fa9459Szrj return java_demangler->get();
2295*a9fa9459Szrj default:
2296*a9fa9459Szrj gold_unreachable();
2297*a9fa9459Szrj }
2298*a9fa9459Szrj }
2299*a9fa9459Szrj
2300*a9fa9459Szrj // Look up SYMBOL_NAME in the list of versions. Return true if the
2301*a9fa9459Szrj // symbol is found, false if not. If the symbol is found, then if
2302*a9fa9459Szrj // PVERSION is not NULL, set *PVERSION to the version tag, and if
2303*a9fa9459Szrj // P_IS_GLOBAL is not NULL, set *P_IS_GLOBAL according to whether the
2304*a9fa9459Szrj // symbol is global or not.
2305*a9fa9459Szrj
2306*a9fa9459Szrj bool
get_symbol_version(const char * symbol_name,std::string * pversion,bool * p_is_global) const2307*a9fa9459Szrj Version_script_info::get_symbol_version(const char* symbol_name,
2308*a9fa9459Szrj std::string* pversion,
2309*a9fa9459Szrj bool* p_is_global) const
2310*a9fa9459Szrj {
2311*a9fa9459Szrj Lazy_demangler cpp_demangled_name(symbol_name, DMGL_ANSI | DMGL_PARAMS);
2312*a9fa9459Szrj Lazy_demangler java_demangled_name(symbol_name,
2313*a9fa9459Szrj DMGL_ANSI | DMGL_PARAMS | DMGL_JAVA);
2314*a9fa9459Szrj
2315*a9fa9459Szrj gold_assert(this->is_finalized_);
2316*a9fa9459Szrj for (int i = 0; i < LANGUAGE_COUNT; ++i)
2317*a9fa9459Szrj {
2318*a9fa9459Szrj Exact* exact = this->exact_[i];
2319*a9fa9459Szrj if (exact == NULL)
2320*a9fa9459Szrj continue;
2321*a9fa9459Szrj
2322*a9fa9459Szrj const char* name_to_match = this->get_name_to_match(symbol_name, i,
2323*a9fa9459Szrj &cpp_demangled_name,
2324*a9fa9459Szrj &java_demangled_name);
2325*a9fa9459Szrj if (name_to_match == NULL)
2326*a9fa9459Szrj {
2327*a9fa9459Szrj // If the name can not be demangled, the GNU linker goes
2328*a9fa9459Szrj // ahead and tries to match it anyhow. That does not
2329*a9fa9459Szrj // make sense to me and I have not implemented it.
2330*a9fa9459Szrj continue;
2331*a9fa9459Szrj }
2332*a9fa9459Szrj
2333*a9fa9459Szrj Exact::const_iterator pe = exact->find(name_to_match);
2334*a9fa9459Szrj if (pe != exact->end())
2335*a9fa9459Szrj {
2336*a9fa9459Szrj const Version_tree_match& vtm(pe->second);
2337*a9fa9459Szrj if (vtm.ambiguous != NULL)
2338*a9fa9459Szrj gold_warning(_("using '%s' as version for '%s' which is also "
2339*a9fa9459Szrj "named in version '%s' in script"),
2340*a9fa9459Szrj vtm.real->tag.c_str(), name_to_match,
2341*a9fa9459Szrj vtm.ambiguous->tag.c_str());
2342*a9fa9459Szrj
2343*a9fa9459Szrj if (pversion != NULL)
2344*a9fa9459Szrj *pversion = vtm.real->tag;
2345*a9fa9459Szrj if (p_is_global != NULL)
2346*a9fa9459Szrj *p_is_global = vtm.is_global;
2347*a9fa9459Szrj
2348*a9fa9459Szrj // If we are using --no-undefined-version, and this is a
2349*a9fa9459Szrj // global symbol, we have to record that we have found this
2350*a9fa9459Szrj // symbol, so that we don't warn about it. We have to do
2351*a9fa9459Szrj // this now, because otherwise we have no way to get from a
2352*a9fa9459Szrj // non-C language back to the demangled name that we
2353*a9fa9459Szrj // matched.
2354*a9fa9459Szrj if (p_is_global != NULL && vtm.is_global)
2355*a9fa9459Szrj vtm.expression->was_matched_by_symbol = true;
2356*a9fa9459Szrj
2357*a9fa9459Szrj return true;
2358*a9fa9459Szrj }
2359*a9fa9459Szrj }
2360*a9fa9459Szrj
2361*a9fa9459Szrj // Look through the glob patterns in reverse order.
2362*a9fa9459Szrj
2363*a9fa9459Szrj for (Globs::const_reverse_iterator p = this->globs_.rbegin();
2364*a9fa9459Szrj p != this->globs_.rend();
2365*a9fa9459Szrj ++p)
2366*a9fa9459Szrj {
2367*a9fa9459Szrj int language = p->expression->language;
2368*a9fa9459Szrj const char* name_to_match = this->get_name_to_match(symbol_name,
2369*a9fa9459Szrj language,
2370*a9fa9459Szrj &cpp_demangled_name,
2371*a9fa9459Szrj &java_demangled_name);
2372*a9fa9459Szrj if (name_to_match == NULL)
2373*a9fa9459Szrj continue;
2374*a9fa9459Szrj
2375*a9fa9459Szrj if (fnmatch(p->expression->pattern.c_str(), name_to_match,
2376*a9fa9459Szrj FNM_NOESCAPE) == 0)
2377*a9fa9459Szrj {
2378*a9fa9459Szrj if (pversion != NULL)
2379*a9fa9459Szrj *pversion = p->version->tag;
2380*a9fa9459Szrj if (p_is_global != NULL)
2381*a9fa9459Szrj *p_is_global = p->is_global;
2382*a9fa9459Szrj return true;
2383*a9fa9459Szrj }
2384*a9fa9459Szrj }
2385*a9fa9459Szrj
2386*a9fa9459Szrj // Finally, there may be a wildcard.
2387*a9fa9459Szrj if (this->default_version_ != NULL)
2388*a9fa9459Szrj {
2389*a9fa9459Szrj if (pversion != NULL)
2390*a9fa9459Szrj *pversion = this->default_version_->tag;
2391*a9fa9459Szrj if (p_is_global != NULL)
2392*a9fa9459Szrj *p_is_global = this->default_is_global_;
2393*a9fa9459Szrj return true;
2394*a9fa9459Szrj }
2395*a9fa9459Szrj
2396*a9fa9459Szrj return false;
2397*a9fa9459Szrj }
2398*a9fa9459Szrj
2399*a9fa9459Szrj // Give an error if any exact symbol names (not wildcards) appear in a
2400*a9fa9459Szrj // version script, but there is no such symbol.
2401*a9fa9459Szrj
2402*a9fa9459Szrj void
check_unmatched_names(const Symbol_table * symtab) const2403*a9fa9459Szrj Version_script_info::check_unmatched_names(const Symbol_table* symtab) const
2404*a9fa9459Szrj {
2405*a9fa9459Szrj for (size_t i = 0; i < this->version_trees_.size(); ++i)
2406*a9fa9459Szrj {
2407*a9fa9459Szrj const Version_tree* vt = this->version_trees_[i];
2408*a9fa9459Szrj if (vt->global == NULL)
2409*a9fa9459Szrj continue;
2410*a9fa9459Szrj for (size_t j = 0; j < vt->global->expressions.size(); ++j)
2411*a9fa9459Szrj {
2412*a9fa9459Szrj const Version_expression& expression(vt->global->expressions[j]);
2413*a9fa9459Szrj
2414*a9fa9459Szrj // Ignore cases where we used the version because we saw a
2415*a9fa9459Szrj // symbol that we looked up. Note that
2416*a9fa9459Szrj // WAS_MATCHED_BY_SYMBOL will be true even if the symbol was
2417*a9fa9459Szrj // not a definition. That's OK as in that case we most
2418*a9fa9459Szrj // likely gave an undefined symbol error anyhow.
2419*a9fa9459Szrj if (expression.was_matched_by_symbol)
2420*a9fa9459Szrj continue;
2421*a9fa9459Szrj
2422*a9fa9459Szrj // Just ignore names which are in languages other than C.
2423*a9fa9459Szrj // We have no way to look them up in the symbol table.
2424*a9fa9459Szrj if (expression.language != LANGUAGE_C)
2425*a9fa9459Szrj continue;
2426*a9fa9459Szrj
2427*a9fa9459Szrj // Remove backslash quoting, and ignore wildcard patterns.
2428*a9fa9459Szrj std::string pattern = expression.pattern;
2429*a9fa9459Szrj if (!expression.exact_match)
2430*a9fa9459Szrj {
2431*a9fa9459Szrj if (this->unquote(&pattern))
2432*a9fa9459Szrj continue;
2433*a9fa9459Szrj }
2434*a9fa9459Szrj
2435*a9fa9459Szrj if (symtab->lookup(pattern.c_str(), vt->tag.c_str()) == NULL)
2436*a9fa9459Szrj gold_error(_("version script assignment of %s to symbol %s "
2437*a9fa9459Szrj "failed: symbol not defined"),
2438*a9fa9459Szrj vt->tag.c_str(), pattern.c_str());
2439*a9fa9459Szrj }
2440*a9fa9459Szrj }
2441*a9fa9459Szrj }
2442*a9fa9459Szrj
2443*a9fa9459Szrj struct Version_dependency_list*
allocate_dependency_list()2444*a9fa9459Szrj Version_script_info::allocate_dependency_list()
2445*a9fa9459Szrj {
2446*a9fa9459Szrj dependency_lists_.push_back(new Version_dependency_list);
2447*a9fa9459Szrj return dependency_lists_.back();
2448*a9fa9459Szrj }
2449*a9fa9459Szrj
2450*a9fa9459Szrj struct Version_expression_list*
allocate_expression_list()2451*a9fa9459Szrj Version_script_info::allocate_expression_list()
2452*a9fa9459Szrj {
2453*a9fa9459Szrj expression_lists_.push_back(new Version_expression_list);
2454*a9fa9459Szrj return expression_lists_.back();
2455*a9fa9459Szrj }
2456*a9fa9459Szrj
2457*a9fa9459Szrj struct Version_tree*
allocate_version_tree()2458*a9fa9459Szrj Version_script_info::allocate_version_tree()
2459*a9fa9459Szrj {
2460*a9fa9459Szrj version_trees_.push_back(new Version_tree);
2461*a9fa9459Szrj return version_trees_.back();
2462*a9fa9459Szrj }
2463*a9fa9459Szrj
2464*a9fa9459Szrj // Print for debugging.
2465*a9fa9459Szrj
2466*a9fa9459Szrj void
print(FILE * f) const2467*a9fa9459Szrj Version_script_info::print(FILE* f) const
2468*a9fa9459Szrj {
2469*a9fa9459Szrj if (this->empty())
2470*a9fa9459Szrj return;
2471*a9fa9459Szrj
2472*a9fa9459Szrj fprintf(f, "VERSION {");
2473*a9fa9459Szrj
2474*a9fa9459Szrj for (size_t i = 0; i < this->version_trees_.size(); ++i)
2475*a9fa9459Szrj {
2476*a9fa9459Szrj const Version_tree* vt = this->version_trees_[i];
2477*a9fa9459Szrj
2478*a9fa9459Szrj if (vt->tag.empty())
2479*a9fa9459Szrj fprintf(f, " {\n");
2480*a9fa9459Szrj else
2481*a9fa9459Szrj fprintf(f, " %s {\n", vt->tag.c_str());
2482*a9fa9459Szrj
2483*a9fa9459Szrj if (vt->global != NULL)
2484*a9fa9459Szrj {
2485*a9fa9459Szrj fprintf(f, " global :\n");
2486*a9fa9459Szrj this->print_expression_list(f, vt->global);
2487*a9fa9459Szrj }
2488*a9fa9459Szrj
2489*a9fa9459Szrj if (vt->local != NULL)
2490*a9fa9459Szrj {
2491*a9fa9459Szrj fprintf(f, " local :\n");
2492*a9fa9459Szrj this->print_expression_list(f, vt->local);
2493*a9fa9459Szrj }
2494*a9fa9459Szrj
2495*a9fa9459Szrj fprintf(f, " }");
2496*a9fa9459Szrj if (vt->dependencies != NULL)
2497*a9fa9459Szrj {
2498*a9fa9459Szrj const Version_dependency_list* deps = vt->dependencies;
2499*a9fa9459Szrj for (size_t j = 0; j < deps->dependencies.size(); ++j)
2500*a9fa9459Szrj {
2501*a9fa9459Szrj if (j < deps->dependencies.size() - 1)
2502*a9fa9459Szrj fprintf(f, "\n");
2503*a9fa9459Szrj fprintf(f, " %s", deps->dependencies[j].c_str());
2504*a9fa9459Szrj }
2505*a9fa9459Szrj }
2506*a9fa9459Szrj fprintf(f, ";\n");
2507*a9fa9459Szrj }
2508*a9fa9459Szrj
2509*a9fa9459Szrj fprintf(f, "}\n");
2510*a9fa9459Szrj }
2511*a9fa9459Szrj
2512*a9fa9459Szrj void
print_expression_list(FILE * f,const Version_expression_list * vel) const2513*a9fa9459Szrj Version_script_info::print_expression_list(
2514*a9fa9459Szrj FILE* f,
2515*a9fa9459Szrj const Version_expression_list* vel) const
2516*a9fa9459Szrj {
2517*a9fa9459Szrj Version_script_info::Language current_language = LANGUAGE_C;
2518*a9fa9459Szrj for (size_t i = 0; i < vel->expressions.size(); ++i)
2519*a9fa9459Szrj {
2520*a9fa9459Szrj const Version_expression& ve(vel->expressions[i]);
2521*a9fa9459Szrj
2522*a9fa9459Szrj if (ve.language != current_language)
2523*a9fa9459Szrj {
2524*a9fa9459Szrj if (current_language != LANGUAGE_C)
2525*a9fa9459Szrj fprintf(f, " }\n");
2526*a9fa9459Szrj switch (ve.language)
2527*a9fa9459Szrj {
2528*a9fa9459Szrj case LANGUAGE_C:
2529*a9fa9459Szrj break;
2530*a9fa9459Szrj case LANGUAGE_CXX:
2531*a9fa9459Szrj fprintf(f, " extern \"C++\" {\n");
2532*a9fa9459Szrj break;
2533*a9fa9459Szrj case LANGUAGE_JAVA:
2534*a9fa9459Szrj fprintf(f, " extern \"Java\" {\n");
2535*a9fa9459Szrj break;
2536*a9fa9459Szrj default:
2537*a9fa9459Szrj gold_unreachable();
2538*a9fa9459Szrj }
2539*a9fa9459Szrj current_language = ve.language;
2540*a9fa9459Szrj }
2541*a9fa9459Szrj
2542*a9fa9459Szrj fprintf(f, " ");
2543*a9fa9459Szrj if (current_language != LANGUAGE_C)
2544*a9fa9459Szrj fprintf(f, " ");
2545*a9fa9459Szrj
2546*a9fa9459Szrj if (ve.exact_match)
2547*a9fa9459Szrj fprintf(f, "\"");
2548*a9fa9459Szrj fprintf(f, "%s", ve.pattern.c_str());
2549*a9fa9459Szrj if (ve.exact_match)
2550*a9fa9459Szrj fprintf(f, "\"");
2551*a9fa9459Szrj
2552*a9fa9459Szrj fprintf(f, "\n");
2553*a9fa9459Szrj }
2554*a9fa9459Szrj
2555*a9fa9459Szrj if (current_language != LANGUAGE_C)
2556*a9fa9459Szrj fprintf(f, " }\n");
2557*a9fa9459Szrj }
2558*a9fa9459Szrj
2559*a9fa9459Szrj } // End namespace gold.
2560*a9fa9459Szrj
2561*a9fa9459Szrj // The remaining functions are extern "C", so it's clearer to not put
2562*a9fa9459Szrj // them in namespace gold.
2563*a9fa9459Szrj
2564*a9fa9459Szrj using namespace gold;
2565*a9fa9459Szrj
2566*a9fa9459Szrj // This function is called by the bison parser to return the next
2567*a9fa9459Szrj // token.
2568*a9fa9459Szrj
2569*a9fa9459Szrj extern "C" int
yylex(YYSTYPE * lvalp,void * closurev)2570*a9fa9459Szrj yylex(YYSTYPE* lvalp, void* closurev)
2571*a9fa9459Szrj {
2572*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2573*a9fa9459Szrj const Token* token = closure->next_token();
2574*a9fa9459Szrj switch (token->classification())
2575*a9fa9459Szrj {
2576*a9fa9459Szrj default:
2577*a9fa9459Szrj gold_unreachable();
2578*a9fa9459Szrj
2579*a9fa9459Szrj case Token::TOKEN_INVALID:
2580*a9fa9459Szrj yyerror(closurev, "invalid character");
2581*a9fa9459Szrj return 0;
2582*a9fa9459Szrj
2583*a9fa9459Szrj case Token::TOKEN_EOF:
2584*a9fa9459Szrj return 0;
2585*a9fa9459Szrj
2586*a9fa9459Szrj case Token::TOKEN_STRING:
2587*a9fa9459Szrj {
2588*a9fa9459Szrj // This is either a keyword or a STRING.
2589*a9fa9459Szrj size_t len;
2590*a9fa9459Szrj const char* str = token->string_value(&len);
2591*a9fa9459Szrj int parsecode = 0;
2592*a9fa9459Szrj switch (closure->lex_mode())
2593*a9fa9459Szrj {
2594*a9fa9459Szrj case Lex::LINKER_SCRIPT:
2595*a9fa9459Szrj parsecode = script_keywords.keyword_to_parsecode(str, len);
2596*a9fa9459Szrj break;
2597*a9fa9459Szrj case Lex::VERSION_SCRIPT:
2598*a9fa9459Szrj parsecode = version_script_keywords.keyword_to_parsecode(str, len);
2599*a9fa9459Szrj break;
2600*a9fa9459Szrj case Lex::DYNAMIC_LIST:
2601*a9fa9459Szrj parsecode = dynamic_list_keywords.keyword_to_parsecode(str, len);
2602*a9fa9459Szrj break;
2603*a9fa9459Szrj default:
2604*a9fa9459Szrj break;
2605*a9fa9459Szrj }
2606*a9fa9459Szrj if (parsecode != 0)
2607*a9fa9459Szrj return parsecode;
2608*a9fa9459Szrj lvalp->string.value = str;
2609*a9fa9459Szrj lvalp->string.length = len;
2610*a9fa9459Szrj return STRING;
2611*a9fa9459Szrj }
2612*a9fa9459Szrj
2613*a9fa9459Szrj case Token::TOKEN_QUOTED_STRING:
2614*a9fa9459Szrj lvalp->string.value = token->string_value(&lvalp->string.length);
2615*a9fa9459Szrj return QUOTED_STRING;
2616*a9fa9459Szrj
2617*a9fa9459Szrj case Token::TOKEN_OPERATOR:
2618*a9fa9459Szrj return token->operator_value();
2619*a9fa9459Szrj
2620*a9fa9459Szrj case Token::TOKEN_INTEGER:
2621*a9fa9459Szrj lvalp->integer = token->integer_value();
2622*a9fa9459Szrj return INTEGER;
2623*a9fa9459Szrj }
2624*a9fa9459Szrj }
2625*a9fa9459Szrj
2626*a9fa9459Szrj // This function is called by the bison parser to report an error.
2627*a9fa9459Szrj
2628*a9fa9459Szrj extern "C" void
yyerror(void * closurev,const char * message)2629*a9fa9459Szrj yyerror(void* closurev, const char* message)
2630*a9fa9459Szrj {
2631*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2632*a9fa9459Szrj gold_error(_("%s:%d:%d: %s"), closure->filename(), closure->lineno(),
2633*a9fa9459Szrj closure->charpos(), message);
2634*a9fa9459Szrj }
2635*a9fa9459Szrj
2636*a9fa9459Szrj // Called by the bison parser to add an external symbol to the link.
2637*a9fa9459Szrj
2638*a9fa9459Szrj extern "C" void
script_add_extern(void * closurev,const char * name,size_t length)2639*a9fa9459Szrj script_add_extern(void* closurev, const char* name, size_t length)
2640*a9fa9459Szrj {
2641*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2642*a9fa9459Szrj closure->script_options()->add_symbol_reference(name, length);
2643*a9fa9459Szrj }
2644*a9fa9459Szrj
2645*a9fa9459Szrj // Called by the bison parser to add a file to the link.
2646*a9fa9459Szrj
2647*a9fa9459Szrj extern "C" void
script_add_file(void * closurev,const char * name,size_t length)2648*a9fa9459Szrj script_add_file(void* closurev, const char* name, size_t length)
2649*a9fa9459Szrj {
2650*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2651*a9fa9459Szrj
2652*a9fa9459Szrj // If this is an absolute path, and we found the script in the
2653*a9fa9459Szrj // sysroot, then we want to prepend the sysroot to the file name.
2654*a9fa9459Szrj // For example, this is how we handle a cross link to the x86_64
2655*a9fa9459Szrj // libc.so, which refers to /lib/libc.so.6.
2656*a9fa9459Szrj std::string name_string(name, length);
2657*a9fa9459Szrj const char* extra_search_path = ".";
2658*a9fa9459Szrj std::string script_directory;
2659*a9fa9459Szrj if (IS_ABSOLUTE_PATH(name_string.c_str()))
2660*a9fa9459Szrj {
2661*a9fa9459Szrj if (closure->is_in_sysroot())
2662*a9fa9459Szrj {
2663*a9fa9459Szrj const std::string& sysroot(parameters->options().sysroot());
2664*a9fa9459Szrj gold_assert(!sysroot.empty());
2665*a9fa9459Szrj name_string = sysroot + name_string;
2666*a9fa9459Szrj }
2667*a9fa9459Szrj }
2668*a9fa9459Szrj else
2669*a9fa9459Szrj {
2670*a9fa9459Szrj // In addition to checking the normal library search path, we
2671*a9fa9459Szrj // also want to check in the script-directory.
2672*a9fa9459Szrj const char* slash = strrchr(closure->filename(), '/');
2673*a9fa9459Szrj if (slash != NULL)
2674*a9fa9459Szrj {
2675*a9fa9459Szrj script_directory.assign(closure->filename(),
2676*a9fa9459Szrj slash - closure->filename() + 1);
2677*a9fa9459Szrj extra_search_path = script_directory.c_str();
2678*a9fa9459Szrj }
2679*a9fa9459Szrj }
2680*a9fa9459Szrj
2681*a9fa9459Szrj Input_file_argument file(name_string.c_str(),
2682*a9fa9459Szrj Input_file_argument::INPUT_FILE_TYPE_FILE,
2683*a9fa9459Szrj extra_search_path, false,
2684*a9fa9459Szrj closure->position_dependent_options());
2685*a9fa9459Szrj Input_argument& arg = closure->inputs()->add_file(file);
2686*a9fa9459Szrj arg.set_script_info(closure->script_info());
2687*a9fa9459Szrj }
2688*a9fa9459Szrj
2689*a9fa9459Szrj // Called by the bison parser to add a library to the link.
2690*a9fa9459Szrj
2691*a9fa9459Szrj extern "C" void
script_add_library(void * closurev,const char * name,size_t length)2692*a9fa9459Szrj script_add_library(void* closurev, const char* name, size_t length)
2693*a9fa9459Szrj {
2694*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2695*a9fa9459Szrj std::string name_string(name, length);
2696*a9fa9459Szrj
2697*a9fa9459Szrj if (name_string[0] != 'l')
2698*a9fa9459Szrj gold_error(_("library name must be prefixed with -l"));
2699*a9fa9459Szrj
2700*a9fa9459Szrj Input_file_argument file(name_string.c_str() + 1,
2701*a9fa9459Szrj Input_file_argument::INPUT_FILE_TYPE_LIBRARY,
2702*a9fa9459Szrj "", false,
2703*a9fa9459Szrj closure->position_dependent_options());
2704*a9fa9459Szrj Input_argument& arg = closure->inputs()->add_file(file);
2705*a9fa9459Szrj arg.set_script_info(closure->script_info());
2706*a9fa9459Szrj }
2707*a9fa9459Szrj
2708*a9fa9459Szrj // Called by the bison parser to start a group. If we are already in
2709*a9fa9459Szrj // a group, that means that this script was invoked within a
2710*a9fa9459Szrj // --start-group --end-group sequence on the command line, or that
2711*a9fa9459Szrj // this script was found in a GROUP of another script. In that case,
2712*a9fa9459Szrj // we simply continue the existing group, rather than starting a new
2713*a9fa9459Szrj // one. It is possible to construct a case in which this will do
2714*a9fa9459Szrj // something other than what would happen if we did a recursive group,
2715*a9fa9459Szrj // but it's hard to imagine why the different behaviour would be
2716*a9fa9459Szrj // useful for a real program. Avoiding recursive groups is simpler
2717*a9fa9459Szrj // and more efficient.
2718*a9fa9459Szrj
2719*a9fa9459Szrj extern "C" void
script_start_group(void * closurev)2720*a9fa9459Szrj script_start_group(void* closurev)
2721*a9fa9459Szrj {
2722*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2723*a9fa9459Szrj if (!closure->in_group())
2724*a9fa9459Szrj closure->inputs()->start_group();
2725*a9fa9459Szrj }
2726*a9fa9459Szrj
2727*a9fa9459Szrj // Called by the bison parser at the end of a group.
2728*a9fa9459Szrj
2729*a9fa9459Szrj extern "C" void
script_end_group(void * closurev)2730*a9fa9459Szrj script_end_group(void* closurev)
2731*a9fa9459Szrj {
2732*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2733*a9fa9459Szrj if (!closure->in_group())
2734*a9fa9459Szrj closure->inputs()->end_group();
2735*a9fa9459Szrj }
2736*a9fa9459Szrj
2737*a9fa9459Szrj // Called by the bison parser to start an AS_NEEDED list.
2738*a9fa9459Szrj
2739*a9fa9459Szrj extern "C" void
script_start_as_needed(void * closurev)2740*a9fa9459Szrj script_start_as_needed(void* closurev)
2741*a9fa9459Szrj {
2742*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2743*a9fa9459Szrj closure->position_dependent_options().set_as_needed(true);
2744*a9fa9459Szrj }
2745*a9fa9459Szrj
2746*a9fa9459Szrj // Called by the bison parser at the end of an AS_NEEDED list.
2747*a9fa9459Szrj
2748*a9fa9459Szrj extern "C" void
script_end_as_needed(void * closurev)2749*a9fa9459Szrj script_end_as_needed(void* closurev)
2750*a9fa9459Szrj {
2751*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2752*a9fa9459Szrj closure->position_dependent_options().set_as_needed(false);
2753*a9fa9459Szrj }
2754*a9fa9459Szrj
2755*a9fa9459Szrj // Called by the bison parser to set the entry symbol.
2756*a9fa9459Szrj
2757*a9fa9459Szrj extern "C" void
script_set_entry(void * closurev,const char * entry,size_t length)2758*a9fa9459Szrj script_set_entry(void* closurev, const char* entry, size_t length)
2759*a9fa9459Szrj {
2760*a9fa9459Szrj // We'll parse this exactly the same as --entry=ENTRY on the commandline
2761*a9fa9459Szrj // TODO(csilvers): FIXME -- call set_entry directly.
2762*a9fa9459Szrj std::string arg("--entry=");
2763*a9fa9459Szrj arg.append(entry, length);
2764*a9fa9459Szrj script_parse_option(closurev, arg.c_str(), arg.size());
2765*a9fa9459Szrj }
2766*a9fa9459Szrj
2767*a9fa9459Szrj // Called by the bison parser to set whether to define common symbols.
2768*a9fa9459Szrj
2769*a9fa9459Szrj extern "C" void
script_set_common_allocation(void * closurev,int set)2770*a9fa9459Szrj script_set_common_allocation(void* closurev, int set)
2771*a9fa9459Szrj {
2772*a9fa9459Szrj const char* arg = set != 0 ? "--define-common" : "--no-define-common";
2773*a9fa9459Szrj script_parse_option(closurev, arg, strlen(arg));
2774*a9fa9459Szrj }
2775*a9fa9459Szrj
2776*a9fa9459Szrj // Called by the bison parser to refer to a symbol.
2777*a9fa9459Szrj
2778*a9fa9459Szrj extern "C" Expression*
script_symbol(void * closurev,const char * name,size_t length)2779*a9fa9459Szrj script_symbol(void* closurev, const char* name, size_t length)
2780*a9fa9459Szrj {
2781*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2782*a9fa9459Szrj if (length != 1 || name[0] != '.')
2783*a9fa9459Szrj closure->script_options()->add_symbol_reference(name, length);
2784*a9fa9459Szrj return script_exp_string(name, length);
2785*a9fa9459Szrj }
2786*a9fa9459Szrj
2787*a9fa9459Szrj // Called by the bison parser to define a symbol.
2788*a9fa9459Szrj
2789*a9fa9459Szrj extern "C" void
script_set_symbol(void * closurev,const char * name,size_t length,Expression * value,int providei,int hiddeni)2790*a9fa9459Szrj script_set_symbol(void* closurev, const char* name, size_t length,
2791*a9fa9459Szrj Expression* value, int providei, int hiddeni)
2792*a9fa9459Szrj {
2793*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2794*a9fa9459Szrj const bool provide = providei != 0;
2795*a9fa9459Szrj const bool hidden = hiddeni != 0;
2796*a9fa9459Szrj closure->script_options()->add_symbol_assignment(name, length,
2797*a9fa9459Szrj closure->parsing_defsym(),
2798*a9fa9459Szrj value, provide, hidden);
2799*a9fa9459Szrj closure->clear_skip_on_incompatible_target();
2800*a9fa9459Szrj }
2801*a9fa9459Szrj
2802*a9fa9459Szrj // Called by the bison parser to add an assertion.
2803*a9fa9459Szrj
2804*a9fa9459Szrj extern "C" void
script_add_assertion(void * closurev,Expression * check,const char * message,size_t messagelen)2805*a9fa9459Szrj script_add_assertion(void* closurev, Expression* check, const char* message,
2806*a9fa9459Szrj size_t messagelen)
2807*a9fa9459Szrj {
2808*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2809*a9fa9459Szrj closure->script_options()->add_assertion(check, message, messagelen);
2810*a9fa9459Szrj closure->clear_skip_on_incompatible_target();
2811*a9fa9459Szrj }
2812*a9fa9459Szrj
2813*a9fa9459Szrj // Called by the bison parser to parse an OPTION.
2814*a9fa9459Szrj
2815*a9fa9459Szrj extern "C" void
script_parse_option(void * closurev,const char * option,size_t length)2816*a9fa9459Szrj script_parse_option(void* closurev, const char* option, size_t length)
2817*a9fa9459Szrj {
2818*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2819*a9fa9459Szrj // We treat the option as a single command-line option, even if
2820*a9fa9459Szrj // it has internal whitespace.
2821*a9fa9459Szrj if (closure->command_line() == NULL)
2822*a9fa9459Szrj {
2823*a9fa9459Szrj // There are some options that we could handle here--e.g.,
2824*a9fa9459Szrj // -lLIBRARY. Should we bother?
2825*a9fa9459Szrj gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
2826*a9fa9459Szrj " for scripts specified via -T/--script"),
2827*a9fa9459Szrj closure->filename(), closure->lineno(), closure->charpos());
2828*a9fa9459Szrj }
2829*a9fa9459Szrj else
2830*a9fa9459Szrj {
2831*a9fa9459Szrj bool past_a_double_dash_option = false;
2832*a9fa9459Szrj const char* mutable_option = strndup(option, length);
2833*a9fa9459Szrj gold_assert(mutable_option != NULL);
2834*a9fa9459Szrj closure->command_line()->process_one_option(1, &mutable_option, 0,
2835*a9fa9459Szrj &past_a_double_dash_option);
2836*a9fa9459Szrj // The General_options class will quite possibly store a pointer
2837*a9fa9459Szrj // into mutable_option, so we can't free it. In cases the class
2838*a9fa9459Szrj // does not store such a pointer, this is a memory leak. Alas. :(
2839*a9fa9459Szrj }
2840*a9fa9459Szrj closure->clear_skip_on_incompatible_target();
2841*a9fa9459Szrj }
2842*a9fa9459Szrj
2843*a9fa9459Szrj // Called by the bison parser to handle OUTPUT_FORMAT. OUTPUT_FORMAT
2844*a9fa9459Szrj // takes either one or three arguments. In the three argument case,
2845*a9fa9459Szrj // the format depends on the endianness option, which we don't
2846*a9fa9459Szrj // currently support (FIXME). If we see an OUTPUT_FORMAT for the
2847*a9fa9459Szrj // wrong format, then we want to search for a new file. Returning 0
2848*a9fa9459Szrj // here will cause the parser to immediately abort.
2849*a9fa9459Szrj
2850*a9fa9459Szrj extern "C" int
script_check_output_format(void * closurev,const char * default_name,size_t default_length,const char *,size_t,const char *,size_t)2851*a9fa9459Szrj script_check_output_format(void* closurev,
2852*a9fa9459Szrj const char* default_name, size_t default_length,
2853*a9fa9459Szrj const char*, size_t, const char*, size_t)
2854*a9fa9459Szrj {
2855*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2856*a9fa9459Szrj std::string name(default_name, default_length);
2857*a9fa9459Szrj Target* target = select_target_by_bfd_name(name.c_str());
2858*a9fa9459Szrj if (target == NULL || !parameters->is_compatible_target(target))
2859*a9fa9459Szrj {
2860*a9fa9459Szrj if (closure->skip_on_incompatible_target())
2861*a9fa9459Szrj {
2862*a9fa9459Szrj closure->set_found_incompatible_target();
2863*a9fa9459Szrj return 0;
2864*a9fa9459Szrj }
2865*a9fa9459Szrj // FIXME: Should we warn about the unknown target?
2866*a9fa9459Szrj }
2867*a9fa9459Szrj return 1;
2868*a9fa9459Szrj }
2869*a9fa9459Szrj
2870*a9fa9459Szrj // Called by the bison parser to handle TARGET.
2871*a9fa9459Szrj
2872*a9fa9459Szrj extern "C" void
script_set_target(void * closurev,const char * target,size_t len)2873*a9fa9459Szrj script_set_target(void* closurev, const char* target, size_t len)
2874*a9fa9459Szrj {
2875*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2876*a9fa9459Szrj std::string s(target, len);
2877*a9fa9459Szrj General_options::Object_format format_enum;
2878*a9fa9459Szrj format_enum = General_options::string_to_object_format(s.c_str());
2879*a9fa9459Szrj closure->position_dependent_options().set_format_enum(format_enum);
2880*a9fa9459Szrj }
2881*a9fa9459Szrj
2882*a9fa9459Szrj // Called by the bison parser to handle SEARCH_DIR. This is handled
2883*a9fa9459Szrj // exactly like a -L option.
2884*a9fa9459Szrj
2885*a9fa9459Szrj extern "C" void
script_add_search_dir(void * closurev,const char * option,size_t length)2886*a9fa9459Szrj script_add_search_dir(void* closurev, const char* option, size_t length)
2887*a9fa9459Szrj {
2888*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2889*a9fa9459Szrj if (closure->command_line() == NULL)
2890*a9fa9459Szrj gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid"
2891*a9fa9459Szrj " for scripts specified via -T/--script"),
2892*a9fa9459Szrj closure->filename(), closure->lineno(), closure->charpos());
2893*a9fa9459Szrj else if (!closure->command_line()->options().nostdlib())
2894*a9fa9459Szrj {
2895*a9fa9459Szrj std::string s = "-L" + std::string(option, length);
2896*a9fa9459Szrj script_parse_option(closurev, s.c_str(), s.size());
2897*a9fa9459Szrj }
2898*a9fa9459Szrj }
2899*a9fa9459Szrj
2900*a9fa9459Szrj /* Called by the bison parser to push the lexer into expression
2901*a9fa9459Szrj mode. */
2902*a9fa9459Szrj
2903*a9fa9459Szrj extern "C" void
script_push_lex_into_expression_mode(void * closurev)2904*a9fa9459Szrj script_push_lex_into_expression_mode(void* closurev)
2905*a9fa9459Szrj {
2906*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2907*a9fa9459Szrj closure->push_lex_mode(Lex::EXPRESSION);
2908*a9fa9459Szrj }
2909*a9fa9459Szrj
2910*a9fa9459Szrj /* Called by the bison parser to push the lexer into version
2911*a9fa9459Szrj mode. */
2912*a9fa9459Szrj
2913*a9fa9459Szrj extern "C" void
script_push_lex_into_version_mode(void * closurev)2914*a9fa9459Szrj script_push_lex_into_version_mode(void* closurev)
2915*a9fa9459Szrj {
2916*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2917*a9fa9459Szrj if (closure->version_script()->is_finalized())
2918*a9fa9459Szrj gold_error(_("%s:%d:%d: invalid use of VERSION in input file"),
2919*a9fa9459Szrj closure->filename(), closure->lineno(), closure->charpos());
2920*a9fa9459Szrj closure->push_lex_mode(Lex::VERSION_SCRIPT);
2921*a9fa9459Szrj }
2922*a9fa9459Szrj
2923*a9fa9459Szrj /* Called by the bison parser to pop the lexer mode. */
2924*a9fa9459Szrj
2925*a9fa9459Szrj extern "C" void
script_pop_lex_mode(void * closurev)2926*a9fa9459Szrj script_pop_lex_mode(void* closurev)
2927*a9fa9459Szrj {
2928*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2929*a9fa9459Szrj closure->pop_lex_mode();
2930*a9fa9459Szrj }
2931*a9fa9459Szrj
2932*a9fa9459Szrj // Register an entire version node. For example:
2933*a9fa9459Szrj //
2934*a9fa9459Szrj // GLIBC_2.1 {
2935*a9fa9459Szrj // global: foo;
2936*a9fa9459Szrj // } GLIBC_2.0;
2937*a9fa9459Szrj //
2938*a9fa9459Szrj // - tag is "GLIBC_2.1"
2939*a9fa9459Szrj // - tree contains the information "global: foo"
2940*a9fa9459Szrj // - deps contains "GLIBC_2.0"
2941*a9fa9459Szrj
2942*a9fa9459Szrj extern "C" void
script_register_vers_node(void *,const char * tag,int taglen,struct Version_tree * tree,struct Version_dependency_list * deps)2943*a9fa9459Szrj script_register_vers_node(void*,
2944*a9fa9459Szrj const char* tag,
2945*a9fa9459Szrj int taglen,
2946*a9fa9459Szrj struct Version_tree* tree,
2947*a9fa9459Szrj struct Version_dependency_list* deps)
2948*a9fa9459Szrj {
2949*a9fa9459Szrj gold_assert(tree != NULL);
2950*a9fa9459Szrj tree->dependencies = deps;
2951*a9fa9459Szrj if (tag != NULL)
2952*a9fa9459Szrj tree->tag = std::string(tag, taglen);
2953*a9fa9459Szrj }
2954*a9fa9459Szrj
2955*a9fa9459Szrj // Add a dependencies to the list of existing dependencies, if any,
2956*a9fa9459Szrj // and return the expanded list.
2957*a9fa9459Szrj
2958*a9fa9459Szrj extern "C" struct Version_dependency_list*
script_add_vers_depend(void * closurev,struct Version_dependency_list * all_deps,const char * depend_to_add,int deplen)2959*a9fa9459Szrj script_add_vers_depend(void* closurev,
2960*a9fa9459Szrj struct Version_dependency_list* all_deps,
2961*a9fa9459Szrj const char* depend_to_add, int deplen)
2962*a9fa9459Szrj {
2963*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2964*a9fa9459Szrj if (all_deps == NULL)
2965*a9fa9459Szrj all_deps = closure->version_script()->allocate_dependency_list();
2966*a9fa9459Szrj all_deps->dependencies.push_back(std::string(depend_to_add, deplen));
2967*a9fa9459Szrj return all_deps;
2968*a9fa9459Szrj }
2969*a9fa9459Szrj
2970*a9fa9459Szrj // Add a pattern expression to an existing list of expressions, if any.
2971*a9fa9459Szrj
2972*a9fa9459Szrj extern "C" struct Version_expression_list*
script_new_vers_pattern(void * closurev,struct Version_expression_list * expressions,const char * pattern,int patlen,int exact_match)2973*a9fa9459Szrj script_new_vers_pattern(void* closurev,
2974*a9fa9459Szrj struct Version_expression_list* expressions,
2975*a9fa9459Szrj const char* pattern, int patlen, int exact_match)
2976*a9fa9459Szrj {
2977*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
2978*a9fa9459Szrj if (expressions == NULL)
2979*a9fa9459Szrj expressions = closure->version_script()->allocate_expression_list();
2980*a9fa9459Szrj expressions->expressions.push_back(
2981*a9fa9459Szrj Version_expression(std::string(pattern, patlen),
2982*a9fa9459Szrj closure->get_current_language(),
2983*a9fa9459Szrj static_cast<bool>(exact_match)));
2984*a9fa9459Szrj return expressions;
2985*a9fa9459Szrj }
2986*a9fa9459Szrj
2987*a9fa9459Szrj // Attaches b to the end of a, and clears b. So a = a + b and b = {}.
2988*a9fa9459Szrj
2989*a9fa9459Szrj extern "C" struct Version_expression_list*
script_merge_expressions(struct Version_expression_list * a,struct Version_expression_list * b)2990*a9fa9459Szrj script_merge_expressions(struct Version_expression_list* a,
2991*a9fa9459Szrj struct Version_expression_list* b)
2992*a9fa9459Szrj {
2993*a9fa9459Szrj a->expressions.insert(a->expressions.end(),
2994*a9fa9459Szrj b->expressions.begin(), b->expressions.end());
2995*a9fa9459Szrj // We could delete b and remove it from expressions_lists_, but
2996*a9fa9459Szrj // that's a lot of work. This works just as well.
2997*a9fa9459Szrj b->expressions.clear();
2998*a9fa9459Szrj return a;
2999*a9fa9459Szrj }
3000*a9fa9459Szrj
3001*a9fa9459Szrj // Combine the global and local expressions into a a Version_tree.
3002*a9fa9459Szrj
3003*a9fa9459Szrj extern "C" struct Version_tree*
script_new_vers_node(void * closurev,struct Version_expression_list * global,struct Version_expression_list * local)3004*a9fa9459Szrj script_new_vers_node(void* closurev,
3005*a9fa9459Szrj struct Version_expression_list* global,
3006*a9fa9459Szrj struct Version_expression_list* local)
3007*a9fa9459Szrj {
3008*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3009*a9fa9459Szrj Version_tree* tree = closure->version_script()->allocate_version_tree();
3010*a9fa9459Szrj tree->global = global;
3011*a9fa9459Szrj tree->local = local;
3012*a9fa9459Szrj return tree;
3013*a9fa9459Szrj }
3014*a9fa9459Szrj
3015*a9fa9459Szrj // Handle a transition in language, such as at the
3016*a9fa9459Szrj // start or end of 'extern "C++"'
3017*a9fa9459Szrj
3018*a9fa9459Szrj extern "C" void
version_script_push_lang(void * closurev,const char * lang,int langlen)3019*a9fa9459Szrj version_script_push_lang(void* closurev, const char* lang, int langlen)
3020*a9fa9459Szrj {
3021*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3022*a9fa9459Szrj std::string language(lang, langlen);
3023*a9fa9459Szrj Version_script_info::Language code;
3024*a9fa9459Szrj if (language.empty() || language == "C")
3025*a9fa9459Szrj code = Version_script_info::LANGUAGE_C;
3026*a9fa9459Szrj else if (language == "C++")
3027*a9fa9459Szrj code = Version_script_info::LANGUAGE_CXX;
3028*a9fa9459Szrj else if (language == "Java")
3029*a9fa9459Szrj code = Version_script_info::LANGUAGE_JAVA;
3030*a9fa9459Szrj else
3031*a9fa9459Szrj {
3032*a9fa9459Szrj char* buf = new char[langlen + 100];
3033*a9fa9459Szrj snprintf(buf, langlen + 100,
3034*a9fa9459Szrj _("unrecognized version script language '%s'"),
3035*a9fa9459Szrj language.c_str());
3036*a9fa9459Szrj yyerror(closurev, buf);
3037*a9fa9459Szrj delete[] buf;
3038*a9fa9459Szrj code = Version_script_info::LANGUAGE_C;
3039*a9fa9459Szrj }
3040*a9fa9459Szrj closure->push_language(code);
3041*a9fa9459Szrj }
3042*a9fa9459Szrj
3043*a9fa9459Szrj extern "C" void
version_script_pop_lang(void * closurev)3044*a9fa9459Szrj version_script_pop_lang(void* closurev)
3045*a9fa9459Szrj {
3046*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3047*a9fa9459Szrj closure->pop_language();
3048*a9fa9459Szrj }
3049*a9fa9459Szrj
3050*a9fa9459Szrj // Called by the bison parser to start a SECTIONS clause.
3051*a9fa9459Szrj
3052*a9fa9459Szrj extern "C" void
script_start_sections(void * closurev)3053*a9fa9459Szrj script_start_sections(void* closurev)
3054*a9fa9459Szrj {
3055*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3056*a9fa9459Szrj closure->script_options()->script_sections()->start_sections();
3057*a9fa9459Szrj closure->clear_skip_on_incompatible_target();
3058*a9fa9459Szrj }
3059*a9fa9459Szrj
3060*a9fa9459Szrj // Called by the bison parser to finish a SECTIONS clause.
3061*a9fa9459Szrj
3062*a9fa9459Szrj extern "C" void
script_finish_sections(void * closurev)3063*a9fa9459Szrj script_finish_sections(void* closurev)
3064*a9fa9459Szrj {
3065*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3066*a9fa9459Szrj closure->script_options()->script_sections()->finish_sections();
3067*a9fa9459Szrj }
3068*a9fa9459Szrj
3069*a9fa9459Szrj // Start processing entries for an output section.
3070*a9fa9459Szrj
3071*a9fa9459Szrj extern "C" void
script_start_output_section(void * closurev,const char * name,size_t namelen,const struct Parser_output_section_header * header)3072*a9fa9459Szrj script_start_output_section(void* closurev, const char* name, size_t namelen,
3073*a9fa9459Szrj const struct Parser_output_section_header* header)
3074*a9fa9459Szrj {
3075*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3076*a9fa9459Szrj closure->script_options()->script_sections()->start_output_section(name,
3077*a9fa9459Szrj namelen,
3078*a9fa9459Szrj header);
3079*a9fa9459Szrj }
3080*a9fa9459Szrj
3081*a9fa9459Szrj // Finish processing entries for an output section.
3082*a9fa9459Szrj
3083*a9fa9459Szrj extern "C" void
script_finish_output_section(void * closurev,const struct Parser_output_section_trailer * trail)3084*a9fa9459Szrj script_finish_output_section(void* closurev,
3085*a9fa9459Szrj const struct Parser_output_section_trailer* trail)
3086*a9fa9459Szrj {
3087*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3088*a9fa9459Szrj closure->script_options()->script_sections()->finish_output_section(trail);
3089*a9fa9459Szrj }
3090*a9fa9459Szrj
3091*a9fa9459Szrj // Add a data item (e.g., "WORD (0)") to the current output section.
3092*a9fa9459Szrj
3093*a9fa9459Szrj extern "C" void
script_add_data(void * closurev,int data_token,Expression * val)3094*a9fa9459Szrj script_add_data(void* closurev, int data_token, Expression* val)
3095*a9fa9459Szrj {
3096*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3097*a9fa9459Szrj int size;
3098*a9fa9459Szrj bool is_signed = true;
3099*a9fa9459Szrj switch (data_token)
3100*a9fa9459Szrj {
3101*a9fa9459Szrj case QUAD:
3102*a9fa9459Szrj size = 8;
3103*a9fa9459Szrj is_signed = false;
3104*a9fa9459Szrj break;
3105*a9fa9459Szrj case SQUAD:
3106*a9fa9459Szrj size = 8;
3107*a9fa9459Szrj break;
3108*a9fa9459Szrj case LONG:
3109*a9fa9459Szrj size = 4;
3110*a9fa9459Szrj break;
3111*a9fa9459Szrj case SHORT:
3112*a9fa9459Szrj size = 2;
3113*a9fa9459Szrj break;
3114*a9fa9459Szrj case BYTE:
3115*a9fa9459Szrj size = 1;
3116*a9fa9459Szrj break;
3117*a9fa9459Szrj default:
3118*a9fa9459Szrj gold_unreachable();
3119*a9fa9459Szrj }
3120*a9fa9459Szrj closure->script_options()->script_sections()->add_data(size, is_signed, val);
3121*a9fa9459Szrj }
3122*a9fa9459Szrj
3123*a9fa9459Szrj // Add a clause setting the fill value to the current output section.
3124*a9fa9459Szrj
3125*a9fa9459Szrj extern "C" void
script_add_fill(void * closurev,Expression * val)3126*a9fa9459Szrj script_add_fill(void* closurev, Expression* val)
3127*a9fa9459Szrj {
3128*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3129*a9fa9459Szrj closure->script_options()->script_sections()->add_fill(val);
3130*a9fa9459Szrj }
3131*a9fa9459Szrj
3132*a9fa9459Szrj // Add a new input section specification to the current output
3133*a9fa9459Szrj // section.
3134*a9fa9459Szrj
3135*a9fa9459Szrj extern "C" void
script_add_input_section(void * closurev,const struct Input_section_spec * spec,int keepi)3136*a9fa9459Szrj script_add_input_section(void* closurev,
3137*a9fa9459Szrj const struct Input_section_spec* spec,
3138*a9fa9459Szrj int keepi)
3139*a9fa9459Szrj {
3140*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3141*a9fa9459Szrj bool keep = keepi != 0;
3142*a9fa9459Szrj closure->script_options()->script_sections()->add_input_section(spec, keep);
3143*a9fa9459Szrj }
3144*a9fa9459Szrj
3145*a9fa9459Szrj // When we see DATA_SEGMENT_ALIGN we record that following output
3146*a9fa9459Szrj // sections may be relro.
3147*a9fa9459Szrj
3148*a9fa9459Szrj extern "C" void
script_data_segment_align(void * closurev)3149*a9fa9459Szrj script_data_segment_align(void* closurev)
3150*a9fa9459Szrj {
3151*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3152*a9fa9459Szrj if (!closure->script_options()->saw_sections_clause())
3153*a9fa9459Szrj gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
3154*a9fa9459Szrj closure->filename(), closure->lineno(), closure->charpos());
3155*a9fa9459Szrj else
3156*a9fa9459Szrj closure->script_options()->script_sections()->data_segment_align();
3157*a9fa9459Szrj }
3158*a9fa9459Szrj
3159*a9fa9459Szrj // When we see DATA_SEGMENT_RELRO_END we know that all output sections
3160*a9fa9459Szrj // since DATA_SEGMENT_ALIGN should be relro.
3161*a9fa9459Szrj
3162*a9fa9459Szrj extern "C" void
script_data_segment_relro_end(void * closurev)3163*a9fa9459Szrj script_data_segment_relro_end(void* closurev)
3164*a9fa9459Szrj {
3165*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3166*a9fa9459Szrj if (!closure->script_options()->saw_sections_clause())
3167*a9fa9459Szrj gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
3168*a9fa9459Szrj closure->filename(), closure->lineno(), closure->charpos());
3169*a9fa9459Szrj else
3170*a9fa9459Szrj closure->script_options()->script_sections()->data_segment_relro_end();
3171*a9fa9459Szrj }
3172*a9fa9459Szrj
3173*a9fa9459Szrj // Create a new list of string/sort pairs.
3174*a9fa9459Szrj
3175*a9fa9459Szrj extern "C" String_sort_list_ptr
script_new_string_sort_list(const struct Wildcard_section * string_sort)3176*a9fa9459Szrj script_new_string_sort_list(const struct Wildcard_section* string_sort)
3177*a9fa9459Szrj {
3178*a9fa9459Szrj return new String_sort_list(1, *string_sort);
3179*a9fa9459Szrj }
3180*a9fa9459Szrj
3181*a9fa9459Szrj // Add an entry to a list of string/sort pairs. The way the parser
3182*a9fa9459Szrj // works permits us to simply modify the first parameter, rather than
3183*a9fa9459Szrj // copy the vector.
3184*a9fa9459Szrj
3185*a9fa9459Szrj extern "C" String_sort_list_ptr
script_string_sort_list_add(String_sort_list_ptr pv,const struct Wildcard_section * string_sort)3186*a9fa9459Szrj script_string_sort_list_add(String_sort_list_ptr pv,
3187*a9fa9459Szrj const struct Wildcard_section* string_sort)
3188*a9fa9459Szrj {
3189*a9fa9459Szrj if (pv == NULL)
3190*a9fa9459Szrj return script_new_string_sort_list(string_sort);
3191*a9fa9459Szrj else
3192*a9fa9459Szrj {
3193*a9fa9459Szrj pv->push_back(*string_sort);
3194*a9fa9459Szrj return pv;
3195*a9fa9459Szrj }
3196*a9fa9459Szrj }
3197*a9fa9459Szrj
3198*a9fa9459Szrj // Create a new list of strings.
3199*a9fa9459Szrj
3200*a9fa9459Szrj extern "C" String_list_ptr
script_new_string_list(const char * str,size_t len)3201*a9fa9459Szrj script_new_string_list(const char* str, size_t len)
3202*a9fa9459Szrj {
3203*a9fa9459Szrj return new String_list(1, std::string(str, len));
3204*a9fa9459Szrj }
3205*a9fa9459Szrj
3206*a9fa9459Szrj // Add an element to a list of strings. The way the parser works
3207*a9fa9459Szrj // permits us to simply modify the first parameter, rather than copy
3208*a9fa9459Szrj // the vector.
3209*a9fa9459Szrj
3210*a9fa9459Szrj extern "C" String_list_ptr
script_string_list_push_back(String_list_ptr pv,const char * str,size_t len)3211*a9fa9459Szrj script_string_list_push_back(String_list_ptr pv, const char* str, size_t len)
3212*a9fa9459Szrj {
3213*a9fa9459Szrj if (pv == NULL)
3214*a9fa9459Szrj return script_new_string_list(str, len);
3215*a9fa9459Szrj else
3216*a9fa9459Szrj {
3217*a9fa9459Szrj pv->push_back(std::string(str, len));
3218*a9fa9459Szrj return pv;
3219*a9fa9459Szrj }
3220*a9fa9459Szrj }
3221*a9fa9459Szrj
3222*a9fa9459Szrj // Concatenate two string lists. Either or both may be NULL. The way
3223*a9fa9459Szrj // the parser works permits us to modify the parameters, rather than
3224*a9fa9459Szrj // copy the vector.
3225*a9fa9459Szrj
3226*a9fa9459Szrj extern "C" String_list_ptr
script_string_list_append(String_list_ptr pv1,String_list_ptr pv2)3227*a9fa9459Szrj script_string_list_append(String_list_ptr pv1, String_list_ptr pv2)
3228*a9fa9459Szrj {
3229*a9fa9459Szrj if (pv1 == NULL)
3230*a9fa9459Szrj return pv2;
3231*a9fa9459Szrj if (pv2 == NULL)
3232*a9fa9459Szrj return pv1;
3233*a9fa9459Szrj pv1->insert(pv1->end(), pv2->begin(), pv2->end());
3234*a9fa9459Szrj return pv1;
3235*a9fa9459Szrj }
3236*a9fa9459Szrj
3237*a9fa9459Szrj // Add a new program header.
3238*a9fa9459Szrj
3239*a9fa9459Szrj extern "C" void
script_add_phdr(void * closurev,const char * name,size_t namelen,unsigned int type,const Phdr_info * info)3240*a9fa9459Szrj script_add_phdr(void* closurev, const char* name, size_t namelen,
3241*a9fa9459Szrj unsigned int type, const Phdr_info* info)
3242*a9fa9459Szrj {
3243*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3244*a9fa9459Szrj bool includes_filehdr = info->includes_filehdr != 0;
3245*a9fa9459Szrj bool includes_phdrs = info->includes_phdrs != 0;
3246*a9fa9459Szrj bool is_flags_valid = info->is_flags_valid != 0;
3247*a9fa9459Szrj Script_sections* ss = closure->script_options()->script_sections();
3248*a9fa9459Szrj ss->add_phdr(name, namelen, type, includes_filehdr, includes_phdrs,
3249*a9fa9459Szrj is_flags_valid, info->flags, info->load_address);
3250*a9fa9459Szrj closure->clear_skip_on_incompatible_target();
3251*a9fa9459Szrj }
3252*a9fa9459Szrj
3253*a9fa9459Szrj // Convert a program header string to a type.
3254*a9fa9459Szrj
3255*a9fa9459Szrj #define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME }
3256*a9fa9459Szrj
3257*a9fa9459Szrj static struct
3258*a9fa9459Szrj {
3259*a9fa9459Szrj const char* name;
3260*a9fa9459Szrj size_t namelen;
3261*a9fa9459Szrj unsigned int val;
3262*a9fa9459Szrj } phdr_type_names[] =
3263*a9fa9459Szrj {
3264*a9fa9459Szrj PHDR_TYPE(PT_NULL),
3265*a9fa9459Szrj PHDR_TYPE(PT_LOAD),
3266*a9fa9459Szrj PHDR_TYPE(PT_DYNAMIC),
3267*a9fa9459Szrj PHDR_TYPE(PT_INTERP),
3268*a9fa9459Szrj PHDR_TYPE(PT_NOTE),
3269*a9fa9459Szrj PHDR_TYPE(PT_SHLIB),
3270*a9fa9459Szrj PHDR_TYPE(PT_PHDR),
3271*a9fa9459Szrj PHDR_TYPE(PT_TLS),
3272*a9fa9459Szrj PHDR_TYPE(PT_GNU_EH_FRAME),
3273*a9fa9459Szrj PHDR_TYPE(PT_GNU_STACK),
3274*a9fa9459Szrj PHDR_TYPE(PT_GNU_RELRO)
3275*a9fa9459Szrj };
3276*a9fa9459Szrj
3277*a9fa9459Szrj extern "C" unsigned int
script_phdr_string_to_type(void * closurev,const char * name,size_t namelen)3278*a9fa9459Szrj script_phdr_string_to_type(void* closurev, const char* name, size_t namelen)
3279*a9fa9459Szrj {
3280*a9fa9459Szrj for (unsigned int i = 0;
3281*a9fa9459Szrj i < sizeof(phdr_type_names) / sizeof(phdr_type_names[0]);
3282*a9fa9459Szrj ++i)
3283*a9fa9459Szrj if (namelen == phdr_type_names[i].namelen
3284*a9fa9459Szrj && strncmp(name, phdr_type_names[i].name, namelen) == 0)
3285*a9fa9459Szrj return phdr_type_names[i].val;
3286*a9fa9459Szrj yyerror(closurev, _("unknown PHDR type (try integer)"));
3287*a9fa9459Szrj return elfcpp::PT_NULL;
3288*a9fa9459Szrj }
3289*a9fa9459Szrj
3290*a9fa9459Szrj extern "C" void
script_saw_segment_start_expression(void * closurev)3291*a9fa9459Szrj script_saw_segment_start_expression(void* closurev)
3292*a9fa9459Szrj {
3293*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3294*a9fa9459Szrj Script_sections* ss = closure->script_options()->script_sections();
3295*a9fa9459Szrj ss->set_saw_segment_start_expression(true);
3296*a9fa9459Szrj }
3297*a9fa9459Szrj
3298*a9fa9459Szrj extern "C" void
script_set_section_region(void * closurev,const char * name,size_t namelen,int set_vma)3299*a9fa9459Szrj script_set_section_region(void* closurev, const char* name, size_t namelen,
3300*a9fa9459Szrj int set_vma)
3301*a9fa9459Szrj {
3302*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3303*a9fa9459Szrj if (!closure->script_options()->saw_sections_clause())
3304*a9fa9459Szrj {
3305*a9fa9459Szrj gold_error(_("%s:%d:%d: MEMORY region '%.*s' referred to outside of "
3306*a9fa9459Szrj "SECTIONS clause"),
3307*a9fa9459Szrj closure->filename(), closure->lineno(), closure->charpos(),
3308*a9fa9459Szrj static_cast<int>(namelen), name);
3309*a9fa9459Szrj return;
3310*a9fa9459Szrj }
3311*a9fa9459Szrj
3312*a9fa9459Szrj Script_sections* ss = closure->script_options()->script_sections();
3313*a9fa9459Szrj Memory_region* mr = ss->find_memory_region(name, namelen);
3314*a9fa9459Szrj if (mr == NULL)
3315*a9fa9459Szrj {
3316*a9fa9459Szrj gold_error(_("%s:%d:%d: MEMORY region '%.*s' not declared"),
3317*a9fa9459Szrj closure->filename(), closure->lineno(), closure->charpos(),
3318*a9fa9459Szrj static_cast<int>(namelen), name);
3319*a9fa9459Szrj return;
3320*a9fa9459Szrj }
3321*a9fa9459Szrj
3322*a9fa9459Szrj ss->set_memory_region(mr, set_vma);
3323*a9fa9459Szrj }
3324*a9fa9459Szrj
3325*a9fa9459Szrj extern "C" void
script_add_memory(void * closurev,const char * name,size_t namelen,unsigned int attrs,Expression * origin,Expression * length)3326*a9fa9459Szrj script_add_memory(void* closurev, const char* name, size_t namelen,
3327*a9fa9459Szrj unsigned int attrs, Expression* origin, Expression* length)
3328*a9fa9459Szrj {
3329*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3330*a9fa9459Szrj Script_sections* ss = closure->script_options()->script_sections();
3331*a9fa9459Szrj ss->add_memory_region(name, namelen, attrs, origin, length);
3332*a9fa9459Szrj }
3333*a9fa9459Szrj
3334*a9fa9459Szrj extern "C" unsigned int
script_parse_memory_attr(void * closurev,const char * attrs,size_t attrlen,int invert)3335*a9fa9459Szrj script_parse_memory_attr(void* closurev, const char* attrs, size_t attrlen,
3336*a9fa9459Szrj int invert)
3337*a9fa9459Szrj {
3338*a9fa9459Szrj int attributes = 0;
3339*a9fa9459Szrj
3340*a9fa9459Szrj while (attrlen--)
3341*a9fa9459Szrj switch (*attrs++)
3342*a9fa9459Szrj {
3343*a9fa9459Szrj case 'R':
3344*a9fa9459Szrj case 'r':
3345*a9fa9459Szrj attributes |= MEM_READABLE; break;
3346*a9fa9459Szrj case 'W':
3347*a9fa9459Szrj case 'w':
3348*a9fa9459Szrj attributes |= MEM_READABLE | MEM_WRITEABLE; break;
3349*a9fa9459Szrj case 'X':
3350*a9fa9459Szrj case 'x':
3351*a9fa9459Szrj attributes |= MEM_EXECUTABLE; break;
3352*a9fa9459Szrj case 'A':
3353*a9fa9459Szrj case 'a':
3354*a9fa9459Szrj attributes |= MEM_ALLOCATABLE; break;
3355*a9fa9459Szrj case 'I':
3356*a9fa9459Szrj case 'i':
3357*a9fa9459Szrj case 'L':
3358*a9fa9459Szrj case 'l':
3359*a9fa9459Szrj attributes |= MEM_INITIALIZED; break;
3360*a9fa9459Szrj default:
3361*a9fa9459Szrj yyerror(closurev, _("unknown MEMORY attribute"));
3362*a9fa9459Szrj }
3363*a9fa9459Szrj
3364*a9fa9459Szrj if (invert)
3365*a9fa9459Szrj attributes = (~ attributes) & MEM_ATTR_MASK;
3366*a9fa9459Szrj
3367*a9fa9459Szrj return attributes;
3368*a9fa9459Szrj }
3369*a9fa9459Szrj
3370*a9fa9459Szrj extern "C" void
script_include_directive(int first_token,void * closurev,const char * filename,size_t length)3371*a9fa9459Szrj script_include_directive(int first_token, void* closurev,
3372*a9fa9459Szrj const char* filename, size_t length)
3373*a9fa9459Szrj {
3374*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3375*a9fa9459Szrj std::string name(filename, length);
3376*a9fa9459Szrj Command_line* cmdline = closure->command_line();
3377*a9fa9459Szrj read_script_file(name.c_str(), cmdline, &cmdline->script_options(),
3378*a9fa9459Szrj first_token, Lex::LINKER_SCRIPT);
3379*a9fa9459Szrj }
3380*a9fa9459Szrj
3381*a9fa9459Szrj // Functions for memory regions.
3382*a9fa9459Szrj
3383*a9fa9459Szrj extern "C" Expression*
script_exp_function_origin(void * closurev,const char * name,size_t namelen)3384*a9fa9459Szrj script_exp_function_origin(void* closurev, const char* name, size_t namelen)
3385*a9fa9459Szrj {
3386*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3387*a9fa9459Szrj Script_sections* ss = closure->script_options()->script_sections();
3388*a9fa9459Szrj Expression* origin = ss->find_memory_region_origin(name, namelen);
3389*a9fa9459Szrj
3390*a9fa9459Szrj if (origin == NULL)
3391*a9fa9459Szrj {
3392*a9fa9459Szrj gold_error(_("undefined memory region '%s' referenced "
3393*a9fa9459Szrj "in ORIGIN expression"),
3394*a9fa9459Szrj name);
3395*a9fa9459Szrj // Create a dummy expression to prevent crashes later on.
3396*a9fa9459Szrj origin = script_exp_integer(0);
3397*a9fa9459Szrj }
3398*a9fa9459Szrj
3399*a9fa9459Szrj return origin;
3400*a9fa9459Szrj }
3401*a9fa9459Szrj
3402*a9fa9459Szrj extern "C" Expression*
script_exp_function_length(void * closurev,const char * name,size_t namelen)3403*a9fa9459Szrj script_exp_function_length(void* closurev, const char* name, size_t namelen)
3404*a9fa9459Szrj {
3405*a9fa9459Szrj Parser_closure* closure = static_cast<Parser_closure*>(closurev);
3406*a9fa9459Szrj Script_sections* ss = closure->script_options()->script_sections();
3407*a9fa9459Szrj Expression* length = ss->find_memory_region_length(name, namelen);
3408*a9fa9459Szrj
3409*a9fa9459Szrj if (length == NULL)
3410*a9fa9459Szrj {
3411*a9fa9459Szrj gold_error(_("undefined memory region '%s' referenced "
3412*a9fa9459Szrj "in LENGTH expression"),
3413*a9fa9459Szrj name);
3414*a9fa9459Szrj // Create a dummy expression to prevent crashes later on.
3415*a9fa9459Szrj length = script_exp_integer(0);
3416*a9fa9459Szrj }
3417*a9fa9459Szrj
3418*a9fa9459Szrj return length;
3419*a9fa9459Szrj }
3420