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