1 // script.cc -- handle linker scripts for gold.
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "filenames.h"
35 #include "dirsearch.h"
38 #include "workqueue.h"
40 #include "parameters.h"
49 // A token read from a script file. We don't implement keywords here;
50 // all keywords are simply represented as a string.
55 // Token classification.
60 // Token indicates end of input.
62 // Token is a string of characters.
64 // Token is a quoted string of characters.
66 // Token is an operator.
68 // Token is a number (an integer).
72 // We need an empty constructor so that we can put this STL objects.
74 : classification_(TOKEN_INVALID
), value_(NULL
), value_length_(0),
75 opcode_(0), lineno_(0), charpos_(0)
78 // A general token with no value.
79 Token(Classification classification
, int lineno
, int charpos
)
80 : classification_(classification
), value_(NULL
), value_length_(0),
81 opcode_(0), lineno_(lineno
), charpos_(charpos
)
83 gold_assert(classification
== TOKEN_INVALID
84 || classification
== TOKEN_EOF
);
87 // A general token with a value.
88 Token(Classification classification
, const char* value
, size_t length
,
89 int lineno
, int charpos
)
90 : classification_(classification
), value_(value
), value_length_(length
),
91 opcode_(0), lineno_(lineno
), charpos_(charpos
)
93 gold_assert(classification
!= TOKEN_INVALID
94 && classification
!= TOKEN_EOF
);
97 // A token representing an operator.
98 Token(int opcode
, int lineno
, int charpos
)
99 : classification_(TOKEN_OPERATOR
), value_(NULL
), value_length_(0),
100 opcode_(opcode
), lineno_(lineno
), charpos_(charpos
)
103 // Return whether the token is invalid.
106 { return this->classification_
== TOKEN_INVALID
; }
108 // Return whether this is an EOF token.
111 { return this->classification_
== TOKEN_EOF
; }
113 // Return the token classification.
115 classification() const
116 { return this->classification_
; }
118 // Return the line number at which the token starts.
121 { return this->lineno_
; }
123 // Return the character position at this the token starts.
126 { return this->charpos_
; }
128 // Get the value of a token.
131 string_value(size_t* length
) const
133 gold_assert(this->classification_
== TOKEN_STRING
134 || this->classification_
== TOKEN_QUOTED_STRING
);
135 *length
= this->value_length_
;
140 operator_value() const
142 gold_assert(this->classification_
== TOKEN_OPERATOR
);
143 return this->opcode_
;
147 integer_value() const
149 gold_assert(this->classification_
== TOKEN_INTEGER
);
151 std::string
s(this->value_
, this->value_length_
);
152 return strtoull(s
.c_str(), NULL
, 0);
156 // The token classification.
157 Classification classification_
;
158 // The token value, for TOKEN_STRING or TOKEN_QUOTED_STRING or
161 // The length of the token value.
162 size_t value_length_
;
163 // The token value, for TOKEN_OPERATOR.
165 // The line number where this token started (one based).
167 // The character position within the line where this token started
172 // This class handles lexing a file into a sequence of tokens.
177 // We unfortunately have to support different lexing modes, because
178 // when reading different parts of a linker script we need to parse
179 // things differently.
182 // Reading an ordinary linker script.
184 // Reading an expression in a linker script.
186 // Reading a version script.
188 // Reading a --dynamic-list file.
192 Lex(const char* input_string
, size_t input_length
, int parsing_token
)
193 : input_string_(input_string
), input_length_(input_length
),
194 current_(input_string
), mode_(LINKER_SCRIPT
),
195 first_token_(parsing_token
), token_(),
196 lineno_(1), linestart_(input_string
)
199 // Read a file into a string.
201 read_file(Input_file
*, std::string
*);
203 // Return the next token.
207 // Return the current lexing mode.
210 { return this->mode_
; }
212 // Set the lexing mode.
215 { this->mode_
= mode
; }
219 Lex
& operator=(const Lex
&);
221 // Make a general token with no value at the current location.
223 make_token(Token::Classification c
, const char* start
) const
224 { return Token(c
, this->lineno_
, start
- this->linestart_
+ 1); }
226 // Make a general token with a value at the current location.
228 make_token(Token::Classification c
, const char* v
, size_t len
,
231 { return Token(c
, v
, len
, this->lineno_
, start
- this->linestart_
+ 1); }
233 // Make an operator token at the current location.
235 make_token(int opcode
, const char* start
) const
236 { return Token(opcode
, this->lineno_
, start
- this->linestart_
+ 1); }
238 // Make an invalid token at the current location.
240 make_invalid_token(const char* start
)
241 { return this->make_token(Token::TOKEN_INVALID
, start
); }
243 // Make an EOF token at the current location.
245 make_eof_token(const char* start
)
246 { return this->make_token(Token::TOKEN_EOF
, start
); }
248 // Return whether C can be the first character in a name. C2 is the
249 // next character, since we sometimes need that.
251 can_start_name(char c
, char c2
);
253 // If C can appear in a name which has already started, return a
254 // pointer to a character later in the token or just past
255 // it. Otherwise, return NULL.
257 can_continue_name(const char* c
);
259 // Return whether C, C2, C3 can start a hex number.
261 can_start_hex(char c
, char c2
, char c3
);
263 // If C can appear in a hex number which has already started, return
264 // a pointer to a character later in the token or just past
265 // it. Otherwise, return NULL.
267 can_continue_hex(const char* c
);
269 // Return whether C can start a non-hex number.
271 can_start_number(char c
);
273 // If C can appear in a decimal number which has already started,
274 // return a pointer to a character later in the token or just past
275 // it. Otherwise, return NULL.
277 can_continue_number(const char* c
)
278 { return Lex::can_start_number(*c
) ? c
+ 1 : NULL
; }
280 // If C1 C2 C3 form a valid three character operator, return the
281 // opcode. Otherwise return 0.
283 three_char_operator(char c1
, char c2
, char c3
);
285 // If C1 C2 form a valid two character operator, return the opcode.
286 // Otherwise return 0.
288 two_char_operator(char c1
, char c2
);
290 // If C1 is a valid one character operator, return the opcode.
291 // Otherwise return 0.
293 one_char_operator(char c1
);
295 // Read the next token.
297 get_token(const char**);
299 // Skip a C style /* */ comment. Return false if the comment did
302 skip_c_comment(const char**);
304 // Skip a line # comment. Return false if there was no newline.
306 skip_line_comment(const char**);
308 // Build a token CLASSIFICATION from all characters that match
309 // CAN_CONTINUE_FN. The token starts at START. Start matching from
310 // MATCH. Set *PP to the character following the token.
312 gather_token(Token::Classification
,
313 const char* (Lex::*can_continue_fn
)(const char*),
314 const char* start
, const char* match
, const char** pp
);
316 // Build a token from a quoted string.
318 gather_quoted_string(const char** pp
);
320 // The string we are tokenizing.
321 const char* input_string_
;
322 // The length of the string.
323 size_t input_length_
;
324 // The current offset into the string.
325 const char* current_
;
326 // The current lexing mode.
328 // The code to use for the first token. This is set to 0 after it
331 // The current token.
333 // The current line number.
335 // The start of the current line in the string.
336 const char* linestart_
;
339 // Read the whole file into memory. We don't expect linker scripts to
340 // be large, so we just use a std::string as a buffer. We ignore the
341 // data we've already read, so that we read aligned buffers.
344 Lex::read_file(Input_file
* input_file
, std::string
* contents
)
346 off_t filesize
= input_file
->file().filesize();
348 contents
->reserve(filesize
);
351 unsigned char buf
[BUFSIZ
];
352 while (off
< filesize
)
355 if (get
> filesize
- off
)
356 get
= filesize
- off
;
357 input_file
->file().read(off
, get
, buf
);
358 contents
->append(reinterpret_cast<char*>(&buf
[0]), get
);
363 // Return whether C can be the start of a name, if the next character
364 // is C2. A name can being with a letter, underscore, period, or
365 // dollar sign. Because a name can be a file name, we also permit
366 // forward slash, backslash, and tilde. Tilde is the tricky case
367 // here; GNU ld also uses it as a bitwise not operator. It is only
368 // recognized as the operator if it is not immediately followed by
369 // some character which can appear in a symbol. That is, when we
370 // don't know that we are looking at an expression, "~0" is a file
371 // name, and "~ 0" is an expression using bitwise not. We are
375 Lex::can_start_name(char c
, char c2
)
379 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
380 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
381 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
382 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
384 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
385 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
386 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
387 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
389 case '_': case '.': case '$':
393 return this->mode_
== LINKER_SCRIPT
;
396 return this->mode_
== LINKER_SCRIPT
&& can_continue_name(&c2
);
399 return (this->mode_
== VERSION_SCRIPT
400 || this->mode_
== DYNAMIC_LIST
401 || (this->mode_
== LINKER_SCRIPT
402 && can_continue_name(&c2
)));
409 // Return whether C can continue a name which has already started.
410 // Subsequent characters in a name are the same as the leading
411 // characters, plus digits and "=+-:[],?*". So in general the linker
412 // script language requires spaces around operators, unless we know
413 // that we are parsing an expression.
416 Lex::can_continue_name(const char* c
)
420 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
421 case 'G': case 'H': case 'I': case 'J': case 'K': case 'L':
422 case 'M': case 'N': case 'O': case 'Q': case 'P': case 'R':
423 case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
425 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
426 case 'g': case 'h': case 'i': case 'j': case 'k': case 'l':
427 case 'm': case 'n': case 'o': case 'q': case 'p': case 'r':
428 case 's': case 't': case 'u': case 'v': case 'w': case 'x':
430 case '_': case '.': case '$':
431 case '0': case '1': case '2': case '3': case '4':
432 case '5': case '6': case '7': case '8': case '9':
435 // TODO(csilvers): why not allow ~ in names for version-scripts?
436 case '/': case '\\': case '~':
439 if (this->mode_
== LINKER_SCRIPT
)
443 case '[': case ']': case '*': case '?': case '-':
444 if (this->mode_
== LINKER_SCRIPT
|| this->mode_
== VERSION_SCRIPT
445 || this->mode_
== DYNAMIC_LIST
)
449 // TODO(csilvers): why allow this? ^ is meaningless in version scripts.
451 if (this->mode_
== VERSION_SCRIPT
|| this->mode_
== DYNAMIC_LIST
)
456 if (this->mode_
== LINKER_SCRIPT
)
458 else if ((this->mode_
== VERSION_SCRIPT
|| this->mode_
== DYNAMIC_LIST
)
461 // A name can have '::' in it, as that's a c++ namespace
462 // separator. But a single colon is not part of a name.
472 // For a number we accept 0x followed by hex digits, or any sequence
473 // of digits. The old linker accepts leading '$' for hex, and
474 // trailing HXBOD. Those are for MRI compatibility and we don't
475 // accept them. The old linker also accepts trailing MK for mega or
476 // kilo. FIXME: Those are mentioned in the documentation, and we
477 // should accept them.
479 // Return whether C1 C2 C3 can start a hex number.
482 Lex::can_start_hex(char c1
, char c2
, char c3
)
484 if (c1
== '0' && (c2
== 'x' || c2
== 'X'))
485 return this->can_continue_hex(&c3
);
489 // Return whether C can appear in a hex number.
492 Lex::can_continue_hex(const char* c
)
496 case '0': case '1': case '2': case '3': case '4':
497 case '5': case '6': case '7': case '8': case '9':
498 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
499 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
507 // Return whether C can start a non-hex number.
510 Lex::can_start_number(char c
)
514 case '0': case '1': case '2': case '3': case '4':
515 case '5': case '6': case '7': case '8': case '9':
523 // If C1 C2 C3 form a valid three character operator, return the
524 // opcode (defined in the yyscript.h file generated from yyscript.y).
525 // Otherwise return 0.
528 Lex::three_char_operator(char c1
, char c2
, char c3
)
533 if (c2
== '<' && c3
== '=')
537 if (c2
== '>' && c3
== '=')
546 // If C1 C2 form a valid two character operator, return the opcode
547 // (defined in the yyscript.h file generated from yyscript.y).
548 // Otherwise return 0.
551 Lex::two_char_operator(char c1
, char c2
)
609 // If C1 is a valid operator, return the opcode. Otherwise return 0.
612 Lex::one_char_operator(char c1
)
645 // Skip a C style comment. *PP points to just after the "/*". Return
646 // false if the comment did not end.
649 Lex::skip_c_comment(const char** pp
)
652 while (p
[0] != '*' || p
[1] != '/')
663 this->linestart_
= p
+ 1;
672 // Skip a line # comment. Return false if there was no newline.
675 Lex::skip_line_comment(const char** pp
)
678 size_t skip
= strcspn(p
, "\n");
687 this->linestart_
= p
;
693 // Build a token CLASSIFICATION from all characters that match
694 // CAN_CONTINUE_FN. Update *PP.
697 Lex::gather_token(Token::Classification classification
,
698 const char* (Lex::*can_continue_fn
)(const char*),
703 const char* new_match
= NULL
;
704 while ((new_match
= (this->*can_continue_fn
)(match
)))
707 return this->make_token(classification
, start
, match
- start
, start
);
710 // Build a token from a quoted string.
713 Lex::gather_quoted_string(const char** pp
)
715 const char* start
= *pp
;
716 const char* p
= start
;
718 size_t skip
= strcspn(p
, "\"\n");
720 return this->make_invalid_token(start
);
722 return this->make_token(Token::TOKEN_QUOTED_STRING
, p
, skip
, start
);
725 // Return the next token at *PP. Update *PP. General guideline: we
726 // require linker scripts to be simple ASCII. No unicode linker
727 // scripts. In particular we can assume that any '\0' is the end of
731 Lex::get_token(const char** pp
)
740 return this->make_eof_token(p
);
743 // Skip whitespace quickly.
744 while (*p
== ' ' || *p
== '\t')
751 this->linestart_
= p
;
755 // Skip C style comments.
756 if (p
[0] == '/' && p
[1] == '*')
758 int lineno
= this->lineno_
;
759 int charpos
= p
- this->linestart_
+ 1;
762 if (!this->skip_c_comment(pp
))
763 return Token(Token::TOKEN_INVALID
, lineno
, charpos
);
769 // Skip line comments.
773 if (!this->skip_line_comment(pp
))
774 return this->make_eof_token(p
);
780 if (this->can_start_name(p
[0], p
[1]))
781 return this->gather_token(Token::TOKEN_STRING
,
782 &Lex::can_continue_name
,
785 // We accept any arbitrary name in double quotes, as long as it
786 // does not cross a line boundary.
790 return this->gather_quoted_string(pp
);
793 // Check for a number.
795 if (this->can_start_hex(p
[0], p
[1], p
[2]))
796 return this->gather_token(Token::TOKEN_INTEGER
,
797 &Lex::can_continue_hex
,
800 if (Lex::can_start_number(p
[0]))
801 return this->gather_token(Token::TOKEN_INTEGER
,
802 &Lex::can_continue_number
,
805 // Check for operators.
807 int opcode
= Lex::three_char_operator(p
[0], p
[1], p
[2]);
811 return this->make_token(opcode
, p
);
814 opcode
= Lex::two_char_operator(p
[0], p
[1]);
818 return this->make_token(opcode
, p
);
821 opcode
= Lex::one_char_operator(p
[0]);
825 return this->make_token(opcode
, p
);
828 return this->make_token(Token::TOKEN_INVALID
, p
);
832 // Return the next token.
837 // The first token is special.
838 if (this->first_token_
!= 0)
840 this->token_
= Token(this->first_token_
, 0, 0);
841 this->first_token_
= 0;
842 return &this->token_
;
845 this->token_
= this->get_token(&this->current_
);
847 // Don't let an early null byte fool us into thinking that we've
848 // reached the end of the file.
849 if (this->token_
.is_eof()
850 && (static_cast<size_t>(this->current_
- this->input_string_
)
851 < this->input_length_
))
852 this->token_
= this->make_invalid_token(this->current_
);
854 return &this->token_
;
857 // class Symbol_assignment.
859 // Add the symbol to the symbol table. This makes sure the symbol is
860 // there and defined. The actual value is stored later. We can't
861 // determine the actual value at this point, because we can't
862 // necessarily evaluate the expression until all ordinary symbols have
865 // The GNU linker lets symbol assignments in the linker script
866 // silently override defined symbols in object files. We are
867 // compatible. FIXME: Should we issue a warning?
870 Symbol_assignment::add_to_table(Symbol_table
* symtab
)
872 elfcpp::STV vis
= this->hidden_
? elfcpp::STV_HIDDEN
: elfcpp::STV_DEFAULT
;
873 this->sym_
= symtab
->define_as_constant(this->name_
.c_str(),
882 true); // force_override
885 // Finalize a symbol value.
888 Symbol_assignment::finalize(Symbol_table
* symtab
, const Layout
* layout
)
890 this->finalize_maybe_dot(symtab
, layout
, false, 0, NULL
);
893 // Finalize a symbol value which can refer to the dot symbol.
896 Symbol_assignment::finalize_with_dot(Symbol_table
* symtab
,
897 const Layout
* layout
,
899 Output_section
* dot_section
)
901 this->finalize_maybe_dot(symtab
, layout
, true, dot_value
, dot_section
);
904 // Finalize a symbol value, internal version.
907 Symbol_assignment::finalize_maybe_dot(Symbol_table
* symtab
,
908 const Layout
* layout
,
909 bool is_dot_available
,
911 Output_section
* dot_section
)
913 // If we were only supposed to provide this symbol, the sym_ field
914 // will be NULL if the symbol was not referenced.
915 if (this->sym_
== NULL
)
917 gold_assert(this->provide_
);
921 if (parameters
->target().get_size() == 32)
923 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
924 this->sized_finalize
<32>(symtab
, layout
, is_dot_available
, dot_value
,
930 else if (parameters
->target().get_size() == 64)
932 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
933 this->sized_finalize
<64>(symtab
, layout
, is_dot_available
, dot_value
,
945 Symbol_assignment::sized_finalize(Symbol_table
* symtab
, const Layout
* layout
,
946 bool is_dot_available
, uint64_t dot_value
,
947 Output_section
* dot_section
)
949 Output_section
* section
;
950 uint64_t final_val
= this->val_
->eval_maybe_dot(symtab
, layout
, true,
952 dot_value
, dot_section
,
954 Sized_symbol
<size
>* ssym
= symtab
->get_sized_symbol
<size
>(this->sym_
);
955 ssym
->set_value(final_val
);
957 ssym
->set_output_section(section
);
960 // Set the symbol value if the expression yields an absolute value.
963 Symbol_assignment::set_if_absolute(Symbol_table
* symtab
, const Layout
* layout
,
964 bool is_dot_available
, uint64_t dot_value
)
966 if (this->sym_
== NULL
)
969 Output_section
* val_section
;
970 uint64_t val
= this->val_
->eval_maybe_dot(symtab
, layout
, false,
971 is_dot_available
, dot_value
,
973 if (val_section
!= NULL
)
976 if (parameters
->target().get_size() == 32)
978 #if defined(HAVE_TARGET_32_LITTLE) || defined(HAVE_TARGET_32_BIG)
979 Sized_symbol
<32>* ssym
= symtab
->get_sized_symbol
<32>(this->sym_
);
980 ssym
->set_value(val
);
985 else if (parameters
->target().get_size() == 64)
987 #if defined(HAVE_TARGET_64_LITTLE) || defined(HAVE_TARGET_64_BIG)
988 Sized_symbol
<64>* ssym
= symtab
->get_sized_symbol
<64>(this->sym_
);
989 ssym
->set_value(val
);
998 // Print for debugging.
1001 Symbol_assignment::print(FILE* f
) const
1003 if (this->provide_
&& this->hidden_
)
1004 fprintf(f
, "PROVIDE_HIDDEN(");
1005 else if (this->provide_
)
1006 fprintf(f
, "PROVIDE(");
1007 else if (this->hidden_
)
1010 fprintf(f
, "%s = ", this->name_
.c_str());
1011 this->val_
->print(f
);
1013 if (this->provide_
|| this->hidden_
)
1019 // Class Script_assertion.
1021 // Check the assertion.
1024 Script_assertion::check(const Symbol_table
* symtab
, const Layout
* layout
)
1026 if (!this->check_
->eval(symtab
, layout
, true))
1027 gold_error("%s", this->message_
.c_str());
1030 // Print for debugging.
1033 Script_assertion::print(FILE* f
) const
1035 fprintf(f
, "ASSERT(");
1036 this->check_
->print(f
);
1037 fprintf(f
, ", \"%s\")\n", this->message_
.c_str());
1040 // Class Script_options.
1042 Script_options::Script_options()
1043 : entry_(), symbol_assignments_(), version_script_info_(),
1048 // Add a symbol to be defined.
1051 Script_options::add_symbol_assignment(const char* name
, size_t length
,
1052 Expression
* value
, bool provide
,
1055 if (length
!= 1 || name
[0] != '.')
1057 if (this->script_sections_
.in_sections_clause())
1058 this->script_sections_
.add_symbol_assignment(name
, length
, value
,
1062 Symbol_assignment
* p
= new Symbol_assignment(name
, length
, value
,
1064 this->symbol_assignments_
.push_back(p
);
1069 if (provide
|| hidden
)
1070 gold_error(_("invalid use of PROVIDE for dot symbol"));
1071 if (!this->script_sections_
.in_sections_clause())
1072 gold_error(_("invalid assignment to dot outside of SECTIONS"));
1074 this->script_sections_
.add_dot_assignment(value
);
1078 // Add an assertion.
1081 Script_options::add_assertion(Expression
* check
, const char* message
,
1084 if (this->script_sections_
.in_sections_clause())
1085 this->script_sections_
.add_assertion(check
, message
, messagelen
);
1088 Script_assertion
* p
= new Script_assertion(check
, message
, messagelen
);
1089 this->assertions_
.push_back(p
);
1093 // Create sections required by any linker scripts.
1096 Script_options::create_script_sections(Layout
* layout
)
1098 if (this->saw_sections_clause())
1099 this->script_sections_
.create_sections(layout
);
1102 // Add any symbols we are defining to the symbol table.
1105 Script_options::add_symbols_to_table(Symbol_table
* symtab
)
1107 for (Symbol_assignments::iterator p
= this->symbol_assignments_
.begin();
1108 p
!= this->symbol_assignments_
.end();
1110 (*p
)->add_to_table(symtab
);
1111 this->script_sections_
.add_symbols_to_table(symtab
);
1114 // Finalize symbol values. Also check assertions.
1117 Script_options::finalize_symbols(Symbol_table
* symtab
, const Layout
* layout
)
1119 // We finalize the symbols defined in SECTIONS first, because they
1120 // are the ones which may have changed. This way if symbol outside
1121 // SECTIONS are defined in terms of symbols inside SECTIONS, they
1122 // will get the right value.
1123 this->script_sections_
.finalize_symbols(symtab
, layout
);
1125 for (Symbol_assignments::iterator p
= this->symbol_assignments_
.begin();
1126 p
!= this->symbol_assignments_
.end();
1128 (*p
)->finalize(symtab
, layout
);
1130 for (Assertions::iterator p
= this->assertions_
.begin();
1131 p
!= this->assertions_
.end();
1133 (*p
)->check(symtab
, layout
);
1136 // Set section addresses. We set all the symbols which have absolute
1137 // values. Then we let the SECTIONS clause do its thing. This
1138 // returns the segment which holds the file header and segment
1142 Script_options::set_section_addresses(Symbol_table
* symtab
, Layout
* layout
)
1144 for (Symbol_assignments::iterator p
= this->symbol_assignments_
.begin();
1145 p
!= this->symbol_assignments_
.end();
1147 (*p
)->set_if_absolute(symtab
, layout
, false, 0);
1149 return this->script_sections_
.set_section_addresses(symtab
, layout
);
1152 // This class holds data passed through the parser to the lexer and to
1153 // the parser support functions. This avoids global variables. We
1154 // can't use global variables because we need not be called by a
1155 // singleton thread.
1157 class Parser_closure
1160 Parser_closure(const char* filename
,
1161 const Position_dependent_options
& posdep_options
,
1162 bool in_group
, bool is_in_sysroot
,
1163 Command_line
* command_line
,
1164 Script_options
* script_options
,
1166 : filename_(filename
), posdep_options_(posdep_options
),
1167 in_group_(in_group
), is_in_sysroot_(is_in_sysroot
),
1168 command_line_(command_line
), script_options_(script_options
),
1169 version_script_info_(script_options
->version_script_info()),
1170 lex_(lex
), lineno_(0), charpos_(0), lex_mode_stack_(), inputs_(NULL
)
1172 // We start out processing C symbols in the default lex mode.
1173 language_stack_
.push_back("");
1174 lex_mode_stack_
.push_back(lex
->mode());
1177 // Return the file name.
1180 { return this->filename_
; }
1182 // Return the position dependent options. The caller may modify
1184 Position_dependent_options
&
1185 position_dependent_options()
1186 { return this->posdep_options_
; }
1188 // Return whether this script is being run in a group.
1191 { return this->in_group_
; }
1193 // Return whether this script was found using a directory in the
1196 is_in_sysroot() const
1197 { return this->is_in_sysroot_
; }
1199 // Returns the Command_line structure passed in at constructor time.
1200 // This value may be NULL. The caller may modify this, which modifies
1201 // the passed-in Command_line object (not a copy).
1204 { return this->command_line_
; }
1206 // Return the options which may be set by a script.
1209 { return this->script_options_
; }
1211 // Return the object in which version script information should be stored.
1212 Version_script_info
*
1214 { return this->version_script_info_
; }
1216 // Return the next token, and advance.
1220 const Token
* token
= this->lex_
->next_token();
1221 this->lineno_
= token
->lineno();
1222 this->charpos_
= token
->charpos();
1226 // Set a new lexer mode, pushing the current one.
1228 push_lex_mode(Lex::Mode mode
)
1230 this->lex_mode_stack_
.push_back(this->lex_
->mode());
1231 this->lex_
->set_mode(mode
);
1234 // Pop the lexer mode.
1238 gold_assert(!this->lex_mode_stack_
.empty());
1239 this->lex_
->set_mode(this->lex_mode_stack_
.back());
1240 this->lex_mode_stack_
.pop_back();
1243 // Return the current lexer mode.
1246 { return this->lex_mode_stack_
.back(); }
1248 // Return the line number of the last token.
1251 { return this->lineno_
; }
1253 // Return the character position in the line of the last token.
1256 { return this->charpos_
; }
1258 // Return the list of input files, creating it if necessary. This
1259 // is a space leak--we never free the INPUTS_ pointer.
1263 if (this->inputs_
== NULL
)
1264 this->inputs_
= new Input_arguments();
1265 return this->inputs_
;
1268 // Return whether we saw any input files.
1271 { return this->inputs_
!= NULL
&& !this->inputs_
->empty(); }
1273 // Return the current language being processed in a version script
1274 // (eg, "C++"). The empty string represents unmangled C names.
1276 get_current_language() const
1277 { return this->language_stack_
.back(); }
1279 // Push a language onto the stack when entering an extern block.
1280 void push_language(const std::string
& lang
)
1281 { this->language_stack_
.push_back(lang
); }
1283 // Pop a language off of the stack when exiting an extern block.
1286 gold_assert(!this->language_stack_
.empty());
1287 this->language_stack_
.pop_back();
1291 // The name of the file we are reading.
1292 const char* filename_
;
1293 // The position dependent options.
1294 Position_dependent_options posdep_options_
;
1295 // Whether we are currently in a --start-group/--end-group.
1297 // Whether the script was found in a sysrooted directory.
1298 bool is_in_sysroot_
;
1299 // May be NULL if the user chooses not to pass one in.
1300 Command_line
* command_line_
;
1301 // Options which may be set from any linker script.
1302 Script_options
* script_options_
;
1303 // Information parsed from a version script.
1304 Version_script_info
* version_script_info_
;
1307 // The line number of the last token returned by next_token.
1309 // The column number of the last token returned by next_token.
1311 // A stack of lexer modes.
1312 std::vector
<Lex::Mode
> lex_mode_stack_
;
1313 // A stack of which extern/language block we're inside. Can be C++,
1314 // java, or empty for C.
1315 std::vector
<std::string
> language_stack_
;
1316 // New input files found to add to the link.
1317 Input_arguments
* inputs_
;
1320 // FILE was found as an argument on the command line. Try to read it
1321 // as a script. Return true if the file was handled.
1324 read_input_script(Workqueue
* workqueue
, const General_options
& options
,
1325 Symbol_table
* symtab
, Layout
* layout
,
1326 Dirsearch
* dirsearch
, Input_objects
* input_objects
,
1327 Mapfile
* mapfile
, Input_group
* input_group
,
1328 const Input_argument
* input_argument
,
1329 Input_file
* input_file
, Task_token
* next_blocker
,
1330 bool* used_next_blocker
)
1332 *used_next_blocker
= false;
1334 std::string input_string
;
1335 Lex::read_file(input_file
, &input_string
);
1337 Lex
lex(input_string
.c_str(), input_string
.length(), PARSING_LINKER_SCRIPT
);
1339 Parser_closure
closure(input_file
->filename().c_str(),
1340 input_argument
->file().options(),
1341 input_group
!= NULL
,
1342 input_file
->is_in_sysroot(),
1344 layout
->script_options(),
1347 if (yyparse(&closure
) != 0)
1350 if (!closure
.saw_inputs())
1353 Task_token
* this_blocker
= NULL
;
1354 for (Input_arguments::const_iterator p
= closure
.inputs()->begin();
1355 p
!= closure
.inputs()->end();
1359 if (p
+ 1 == closure
.inputs()->end())
1363 nb
= new Task_token(true);
1366 workqueue
->queue_soon(new Read_symbols(options
, input_objects
, symtab
,
1367 layout
, dirsearch
, mapfile
, &*p
,
1368 input_group
, this_blocker
, nb
));
1372 *used_next_blocker
= true;
1377 // Helper function for read_version_script() and
1378 // read_commandline_script(). Processes the given file in the mode
1379 // indicated by first_token and lex_mode.
1382 read_script_file(const char* filename
, Command_line
* cmdline
,
1383 Script_options
* script_options
,
1384 int first_token
, Lex::Mode lex_mode
)
1386 // TODO: if filename is a relative filename, search for it manually
1387 // using "." + cmdline->options()->search_path() -- not dirsearch.
1388 Dirsearch dirsearch
;
1390 // The file locking code wants to record a Task, but we haven't
1391 // started the workqueue yet. This is only for debugging purposes,
1392 // so we invent a fake value.
1393 const Task
* task
= reinterpret_cast<const Task
*>(-1);
1395 // We don't want this file to be opened in binary mode.
1396 Position_dependent_options posdep
= cmdline
->position_dependent_options();
1397 if (posdep
.format_enum() == General_options::OBJECT_FORMAT_BINARY
)
1398 posdep
.set_format_enum(General_options::OBJECT_FORMAT_ELF
);
1399 Input_file_argument
input_argument(filename
, false, "", false, posdep
);
1400 Input_file
input_file(&input_argument
);
1401 if (!input_file
.open(cmdline
->options(), dirsearch
, task
))
1404 std::string input_string
;
1405 Lex::read_file(&input_file
, &input_string
);
1407 Lex
lex(input_string
.c_str(), input_string
.length(), first_token
);
1408 lex
.set_mode(lex_mode
);
1410 Parser_closure
closure(filename
,
1411 cmdline
->position_dependent_options(),
1413 input_file
.is_in_sysroot(),
1417 if (yyparse(&closure
) != 0)
1419 input_file
.file().unlock(task
);
1423 input_file
.file().unlock(task
);
1425 gold_assert(!closure
.saw_inputs());
1430 // FILENAME was found as an argument to --script (-T).
1431 // Read it as a script, and execute its contents immediately.
1434 read_commandline_script(const char* filename
, Command_line
* cmdline
)
1436 return read_script_file(filename
, cmdline
, &cmdline
->script_options(),
1437 PARSING_LINKER_SCRIPT
, Lex::LINKER_SCRIPT
);
1440 // FILENAME was found as an argument to --version-script. Read it as
1441 // a version script, and store its contents in
1442 // cmdline->script_options()->version_script_info().
1445 read_version_script(const char* filename
, Command_line
* cmdline
)
1447 return read_script_file(filename
, cmdline
, &cmdline
->script_options(),
1448 PARSING_VERSION_SCRIPT
, Lex::VERSION_SCRIPT
);
1451 // FILENAME was found as an argument to --dynamic-list. Read it as a
1452 // list of symbols, and store its contents in DYNAMIC_LIST.
1455 read_dynamic_list(const char* filename
, Command_line
* cmdline
,
1456 Script_options
* dynamic_list
)
1458 return read_script_file(filename
, cmdline
, dynamic_list
,
1459 PARSING_DYNAMIC_LIST
, Lex::DYNAMIC_LIST
);
1462 // Implement the --defsym option on the command line. Return true if
1466 Script_options::define_symbol(const char* definition
)
1468 Lex
lex(definition
, strlen(definition
), PARSING_DEFSYM
);
1469 lex
.set_mode(Lex::EXPRESSION
);
1472 Position_dependent_options posdep_options
;
1474 Parser_closure
closure("command line", posdep_options
, false, false, NULL
,
1477 if (yyparse(&closure
) != 0)
1480 gold_assert(!closure
.saw_inputs());
1485 // Print the script to F for debugging.
1488 Script_options::print(FILE* f
) const
1490 fprintf(f
, "%s: Dumping linker script\n", program_name
);
1492 if (!this->entry_
.empty())
1493 fprintf(f
, "ENTRY(%s)\n", this->entry_
.c_str());
1495 for (Symbol_assignments::const_iterator p
=
1496 this->symbol_assignments_
.begin();
1497 p
!= this->symbol_assignments_
.end();
1501 for (Assertions::const_iterator p
= this->assertions_
.begin();
1502 p
!= this->assertions_
.end();
1506 this->script_sections_
.print(f
);
1508 this->version_script_info_
.print(f
);
1511 // Manage mapping from keywords to the codes expected by the bison
1512 // parser. We construct one global object for each lex mode with
1515 class Keyword_to_parsecode
1518 // The structure which maps keywords to parsecodes.
1519 struct Keyword_parsecode
1522 const char* keyword
;
1523 // Corresponding parsecode.
1527 Keyword_to_parsecode(const Keyword_parsecode
* keywords
,
1529 : keyword_parsecodes_(keywords
), keyword_count_(keyword_count
)
1532 // Return the parsecode corresponding KEYWORD, or 0 if it is not a
1535 keyword_to_parsecode(const char* keyword
, size_t len
) const;
1538 const Keyword_parsecode
* keyword_parsecodes_
;
1539 const int keyword_count_
;
1542 // Mapping from keyword string to keyword parsecode. This array must
1543 // be kept in sorted order. Parsecodes are looked up using bsearch.
1544 // This array must correspond to the list of parsecodes in yyscript.y.
1546 static const Keyword_to_parsecode::Keyword_parsecode
1547 script_keyword_parsecodes
[] =
1549 { "ABSOLUTE", ABSOLUTE
},
1551 { "ALIGN", ALIGN_K
},
1552 { "ALIGNOF", ALIGNOF
},
1553 { "ASSERT", ASSERT_K
},
1554 { "AS_NEEDED", AS_NEEDED
},
1559 { "CONSTANT", CONSTANT
},
1560 { "CONSTRUCTORS", CONSTRUCTORS
},
1561 { "CREATE_OBJECT_SYMBOLS", CREATE_OBJECT_SYMBOLS
},
1562 { "DATA_SEGMENT_ALIGN", DATA_SEGMENT_ALIGN
},
1563 { "DATA_SEGMENT_END", DATA_SEGMENT_END
},
1564 { "DATA_SEGMENT_RELRO_END", DATA_SEGMENT_RELRO_END
},
1565 { "DEFINED", DEFINED
},
1567 { "EXCLUDE_FILE", EXCLUDE_FILE
},
1568 { "EXTERN", EXTERN
},
1571 { "FORCE_COMMON_ALLOCATION", FORCE_COMMON_ALLOCATION
},
1574 { "INCLUDE", INCLUDE
},
1575 { "INHIBIT_COMMON_ALLOCATION", INHIBIT_COMMON_ALLOCATION
},
1578 { "LENGTH", LENGTH
},
1579 { "LOADADDR", LOADADDR
},
1583 { "MEMORY", MEMORY
},
1586 { "NOCROSSREFS", NOCROSSREFS
},
1587 { "NOFLOAT", NOFLOAT
},
1588 { "ONLY_IF_RO", ONLY_IF_RO
},
1589 { "ONLY_IF_RW", ONLY_IF_RW
},
1590 { "OPTION", OPTION
},
1591 { "ORIGIN", ORIGIN
},
1592 { "OUTPUT", OUTPUT
},
1593 { "OUTPUT_ARCH", OUTPUT_ARCH
},
1594 { "OUTPUT_FORMAT", OUTPUT_FORMAT
},
1595 { "OVERLAY", OVERLAY
},
1597 { "PROVIDE", PROVIDE
},
1598 { "PROVIDE_HIDDEN", PROVIDE_HIDDEN
},
1600 { "SEARCH_DIR", SEARCH_DIR
},
1601 { "SECTIONS", SECTIONS
},
1602 { "SEGMENT_START", SEGMENT_START
},
1604 { "SIZEOF", SIZEOF
},
1605 { "SIZEOF_HEADERS", SIZEOF_HEADERS
},
1606 { "SORT", SORT_BY_NAME
},
1607 { "SORT_BY_ALIGNMENT", SORT_BY_ALIGNMENT
},
1608 { "SORT_BY_NAME", SORT_BY_NAME
},
1609 { "SPECIAL", SPECIAL
},
1611 { "STARTUP", STARTUP
},
1612 { "SUBALIGN", SUBALIGN
},
1613 { "SYSLIB", SYSLIB
},
1614 { "TARGET", TARGET_K
},
1615 { "TRUNCATE", TRUNCATE
},
1616 { "VERSION", VERSIONK
},
1617 { "global", GLOBAL
},
1623 { "sizeof_headers", SIZEOF_HEADERS
},
1626 static const Keyword_to_parsecode
1627 script_keywords(&script_keyword_parsecodes
[0],
1628 (sizeof(script_keyword_parsecodes
)
1629 / sizeof(script_keyword_parsecodes
[0])));
1631 static const Keyword_to_parsecode::Keyword_parsecode
1632 version_script_keyword_parsecodes
[] =
1634 { "extern", EXTERN
},
1635 { "global", GLOBAL
},
1639 static const Keyword_to_parsecode
1640 version_script_keywords(&version_script_keyword_parsecodes
[0],
1641 (sizeof(version_script_keyword_parsecodes
)
1642 / sizeof(version_script_keyword_parsecodes
[0])));
1644 static const Keyword_to_parsecode::Keyword_parsecode
1645 dynamic_list_keyword_parsecodes
[] =
1647 { "extern", EXTERN
},
1650 static const Keyword_to_parsecode
1651 dynamic_list_keywords(&dynamic_list_keyword_parsecodes
[0],
1652 (sizeof(dynamic_list_keyword_parsecodes
)
1653 / sizeof(dynamic_list_keyword_parsecodes
[0])));
1657 // Comparison function passed to bsearch.
1669 ktt_compare(const void* keyv
, const void* kttv
)
1671 const Ktt_key
* key
= static_cast<const Ktt_key
*>(keyv
);
1672 const Keyword_to_parsecode::Keyword_parsecode
* ktt
=
1673 static_cast<const Keyword_to_parsecode::Keyword_parsecode
*>(kttv
);
1674 int i
= strncmp(key
->str
, ktt
->keyword
, key
->len
);
1677 if (ktt
->keyword
[key
->len
] != '\0')
1682 } // End extern "C".
1685 Keyword_to_parsecode::keyword_to_parsecode(const char* keyword
,
1691 void* kttv
= bsearch(&key
,
1692 this->keyword_parsecodes_
,
1693 this->keyword_count_
,
1694 sizeof(this->keyword_parsecodes_
[0]),
1698 Keyword_parsecode
* ktt
= static_cast<Keyword_parsecode
*>(kttv
);
1699 return ktt
->parsecode
;
1702 // The following structs are used within the VersionInfo class as well
1703 // as in the bison helper functions. They store the information
1704 // parsed from the version script.
1706 // A single version expression.
1707 // For example, pattern="std::map*" and language="C++".
1708 // pattern and language should be from the stringpool
1709 struct Version_expression
{
1710 Version_expression(const std::string
& pattern
,
1711 const std::string
& language
,
1713 : pattern(pattern
), language(language
), exact_match(exact_match
) {}
1715 std::string pattern
;
1716 std::string language
;
1717 // If false, we use glob() to match pattern. If true, we use strcmp().
1722 // A list of expressions.
1723 struct Version_expression_list
{
1724 std::vector
<struct Version_expression
> expressions
;
1728 // A list of which versions upon which another version depends.
1729 // Strings should be from the Stringpool.
1730 struct Version_dependency_list
{
1731 std::vector
<std::string
> dependencies
;
1735 // The total definition of a version. It includes the tag for the
1736 // version, its global and local expressions, and any dependencies.
1737 struct Version_tree
{
1739 : tag(), global(NULL
), local(NULL
), dependencies(NULL
) {}
1742 const struct Version_expression_list
* global
;
1743 const struct Version_expression_list
* local
;
1744 const struct Version_dependency_list
* dependencies
;
1747 Version_script_info::~Version_script_info()
1753 Version_script_info::clear()
1755 for (size_t k
= 0; k
< dependency_lists_
.size(); ++k
)
1756 delete dependency_lists_
[k
];
1757 this->dependency_lists_
.clear();
1758 for (size_t k
= 0; k
< version_trees_
.size(); ++k
)
1759 delete version_trees_
[k
];
1760 this->version_trees_
.clear();
1761 for (size_t k
= 0; k
< expression_lists_
.size(); ++k
)
1762 delete expression_lists_
[k
];
1763 this->expression_lists_
.clear();
1766 std::vector
<std::string
>
1767 Version_script_info::get_versions() const
1769 std::vector
<std::string
> ret
;
1770 for (size_t j
= 0; j
< version_trees_
.size(); ++j
)
1771 if (!this->version_trees_
[j
]->tag
.empty())
1772 ret
.push_back(this->version_trees_
[j
]->tag
);
1776 std::vector
<std::string
>
1777 Version_script_info::get_dependencies(const char* version
) const
1779 std::vector
<std::string
> ret
;
1780 for (size_t j
= 0; j
< version_trees_
.size(); ++j
)
1781 if (version_trees_
[j
]->tag
== version
)
1783 const struct Version_dependency_list
* deps
=
1784 version_trees_
[j
]->dependencies
;
1786 for (size_t k
= 0; k
< deps
->dependencies
.size(); ++k
)
1787 ret
.push_back(deps
->dependencies
[k
]);
1793 // Look up SYMBOL_NAME in the list of versions. If CHECK_GLOBAL is
1794 // true look at the globally visible symbols, otherwise look at the
1795 // symbols listed as "local:". Return true if the symbol is found,
1796 // false otherwise. If the symbol is found, then if PVERSION is not
1797 // NULL, set *PVERSION to the version.
1800 Version_script_info::get_symbol_version_helper(const char* symbol_name
,
1802 std::string
* pversion
) const
1804 for (size_t j
= 0; j
< version_trees_
.size(); ++j
)
1806 // Is it a global symbol for this version?
1807 const Version_expression_list
* explist
=
1808 check_global
? version_trees_
[j
]->global
: version_trees_
[j
]->local
;
1809 if (explist
!= NULL
)
1810 for (size_t k
= 0; k
< explist
->expressions
.size(); ++k
)
1812 const char* name_to_match
= symbol_name
;
1813 const struct Version_expression
& exp
= explist
->expressions
[k
];
1814 char* demangled_name
= NULL
;
1815 if (exp
.language
== "C++")
1817 demangled_name
= cplus_demangle(symbol_name
,
1818 DMGL_ANSI
| DMGL_PARAMS
);
1819 // This isn't a C++ symbol.
1820 if (demangled_name
== NULL
)
1822 name_to_match
= demangled_name
;
1824 else if (exp
.language
== "Java")
1826 demangled_name
= cplus_demangle(symbol_name
,
1827 (DMGL_ANSI
| DMGL_PARAMS
1829 // This isn't a Java symbol.
1830 if (demangled_name
== NULL
)
1832 name_to_match
= demangled_name
;
1835 if (exp
.exact_match
)
1836 matched
= strcmp(exp
.pattern
.c_str(), name_to_match
) == 0;
1838 matched
= fnmatch(exp
.pattern
.c_str(), name_to_match
,
1840 if (demangled_name
!= NULL
)
1841 free(demangled_name
);
1844 if (pversion
!= NULL
)
1845 *pversion
= this->version_trees_
[j
]->tag
;
1853 struct Version_dependency_list
*
1854 Version_script_info::allocate_dependency_list()
1856 dependency_lists_
.push_back(new Version_dependency_list
);
1857 return dependency_lists_
.back();
1860 struct Version_expression_list
*
1861 Version_script_info::allocate_expression_list()
1863 expression_lists_
.push_back(new Version_expression_list
);
1864 return expression_lists_
.back();
1867 struct Version_tree
*
1868 Version_script_info::allocate_version_tree()
1870 version_trees_
.push_back(new Version_tree
);
1871 return version_trees_
.back();
1874 // Print for debugging.
1877 Version_script_info::print(FILE* f
) const
1882 fprintf(f
, "VERSION {");
1884 for (size_t i
= 0; i
< this->version_trees_
.size(); ++i
)
1886 const Version_tree
* vt
= this->version_trees_
[i
];
1888 if (vt
->tag
.empty())
1891 fprintf(f
, " %s {\n", vt
->tag
.c_str());
1893 if (vt
->global
!= NULL
)
1895 fprintf(f
, " global :\n");
1896 this->print_expression_list(f
, vt
->global
);
1899 if (vt
->local
!= NULL
)
1901 fprintf(f
, " local :\n");
1902 this->print_expression_list(f
, vt
->local
);
1906 if (vt
->dependencies
!= NULL
)
1908 const Version_dependency_list
* deps
= vt
->dependencies
;
1909 for (size_t j
= 0; j
< deps
->dependencies
.size(); ++j
)
1911 if (j
< deps
->dependencies
.size() - 1)
1913 fprintf(f
, " %s", deps
->dependencies
[j
].c_str());
1923 Version_script_info::print_expression_list(
1925 const Version_expression_list
* vel
) const
1927 std::string current_language
;
1928 for (size_t i
= 0; i
< vel
->expressions
.size(); ++i
)
1930 const Version_expression
& ve(vel
->expressions
[i
]);
1932 if (ve
.language
!= current_language
)
1934 if (!current_language
.empty())
1936 fprintf(f
, " extern \"%s\" {\n", ve
.language
.c_str());
1937 current_language
= ve
.language
;
1941 if (!current_language
.empty())
1946 fprintf(f
, "%s", ve
.pattern
.c_str());
1953 if (!current_language
.empty())
1957 } // End namespace gold.
1959 // The remaining functions are extern "C", so it's clearer to not put
1960 // them in namespace gold.
1962 using namespace gold
;
1964 // This function is called by the bison parser to return the next
1968 yylex(YYSTYPE
* lvalp
, void* closurev
)
1970 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
1971 const Token
* token
= closure
->next_token();
1972 switch (token
->classification())
1977 case Token::TOKEN_INVALID
:
1978 yyerror(closurev
, "invalid character");
1981 case Token::TOKEN_EOF
:
1984 case Token::TOKEN_STRING
:
1986 // This is either a keyword or a STRING.
1988 const char* str
= token
->string_value(&len
);
1990 switch (closure
->lex_mode())
1992 case Lex::LINKER_SCRIPT
:
1993 parsecode
= script_keywords
.keyword_to_parsecode(str
, len
);
1995 case Lex::VERSION_SCRIPT
:
1996 parsecode
= version_script_keywords
.keyword_to_parsecode(str
, len
);
1998 case Lex::DYNAMIC_LIST
:
1999 parsecode
= dynamic_list_keywords
.keyword_to_parsecode(str
, len
);
2006 lvalp
->string
.value
= str
;
2007 lvalp
->string
.length
= len
;
2011 case Token::TOKEN_QUOTED_STRING
:
2012 lvalp
->string
.value
= token
->string_value(&lvalp
->string
.length
);
2013 return QUOTED_STRING
;
2015 case Token::TOKEN_OPERATOR
:
2016 return token
->operator_value();
2018 case Token::TOKEN_INTEGER
:
2019 lvalp
->integer
= token
->integer_value();
2024 // This function is called by the bison parser to report an error.
2027 yyerror(void* closurev
, const char* message
)
2029 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2030 gold_error(_("%s:%d:%d: %s"), closure
->filename(), closure
->lineno(),
2031 closure
->charpos(), message
);
2034 // Called by the bison parser to add a file to the link.
2037 script_add_file(void* closurev
, const char* name
, size_t length
)
2039 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2041 // If this is an absolute path, and we found the script in the
2042 // sysroot, then we want to prepend the sysroot to the file name.
2043 // For example, this is how we handle a cross link to the x86_64
2044 // libc.so, which refers to /lib/libc.so.6.
2045 std::string
name_string(name
, length
);
2046 const char* extra_search_path
= ".";
2047 std::string script_directory
;
2048 if (IS_ABSOLUTE_PATH(name_string
.c_str()))
2050 if (closure
->is_in_sysroot())
2052 const std::string
& sysroot(parameters
->options().sysroot());
2053 gold_assert(!sysroot
.empty());
2054 name_string
= sysroot
+ name_string
;
2059 // In addition to checking the normal library search path, we
2060 // also want to check in the script-directory.
2061 const char *slash
= strrchr(closure
->filename(), '/');
2064 script_directory
.assign(closure
->filename(),
2065 slash
- closure
->filename() + 1);
2066 extra_search_path
= script_directory
.c_str();
2070 Input_file_argument
file(name_string
.c_str(), false, extra_search_path
,
2071 false, closure
->position_dependent_options());
2072 closure
->inputs()->add_file(file
);
2075 // Called by the bison parser to start a group. If we are already in
2076 // a group, that means that this script was invoked within a
2077 // --start-group --end-group sequence on the command line, or that
2078 // this script was found in a GROUP of another script. In that case,
2079 // we simply continue the existing group, rather than starting a new
2080 // one. It is possible to construct a case in which this will do
2081 // something other than what would happen if we did a recursive group,
2082 // but it's hard to imagine why the different behaviour would be
2083 // useful for a real program. Avoiding recursive groups is simpler
2084 // and more efficient.
2087 script_start_group(void* closurev
)
2089 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2090 if (!closure
->in_group())
2091 closure
->inputs()->start_group();
2094 // Called by the bison parser at the end of a group.
2097 script_end_group(void* closurev
)
2099 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2100 if (!closure
->in_group())
2101 closure
->inputs()->end_group();
2104 // Called by the bison parser to start an AS_NEEDED list.
2107 script_start_as_needed(void* closurev
)
2109 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2110 closure
->position_dependent_options().set_as_needed(true);
2113 // Called by the bison parser at the end of an AS_NEEDED list.
2116 script_end_as_needed(void* closurev
)
2118 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2119 closure
->position_dependent_options().set_as_needed(false);
2122 // Called by the bison parser to set the entry symbol.
2125 script_set_entry(void* closurev
, const char* entry
, size_t length
)
2127 // We'll parse this exactly the same as --entry=ENTRY on the commandline
2128 // TODO(csilvers): FIXME -- call set_entry directly.
2129 std::string
arg("--entry=");
2130 arg
.append(entry
, length
);
2131 script_parse_option(closurev
, arg
.c_str(), arg
.size());
2134 // Called by the bison parser to set whether to define common symbols.
2137 script_set_common_allocation(void* closurev
, int set
)
2139 const char* arg
= set
!= 0 ? "--define-common" : "--no-define-common";
2140 script_parse_option(closurev
, arg
, strlen(arg
));
2143 // Called by the bison parser to define a symbol.
2146 script_set_symbol(void* closurev
, const char* name
, size_t length
,
2147 Expression
* value
, int providei
, int hiddeni
)
2149 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2150 const bool provide
= providei
!= 0;
2151 const bool hidden
= hiddeni
!= 0;
2152 closure
->script_options()->add_symbol_assignment(name
, length
, value
,
2156 // Called by the bison parser to add an assertion.
2159 script_add_assertion(void* closurev
, Expression
* check
, const char* message
,
2162 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2163 closure
->script_options()->add_assertion(check
, message
, messagelen
);
2166 // Called by the bison parser to parse an OPTION.
2169 script_parse_option(void* closurev
, const char* option
, size_t length
)
2171 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2172 // We treat the option as a single command-line option, even if
2173 // it has internal whitespace.
2174 if (closure
->command_line() == NULL
)
2176 // There are some options that we could handle here--e.g.,
2177 // -lLIBRARY. Should we bother?
2178 gold_warning(_("%s:%d:%d: ignoring command OPTION; OPTION is only valid"
2179 " for scripts specified via -T/--script"),
2180 closure
->filename(), closure
->lineno(), closure
->charpos());
2184 bool past_a_double_dash_option
= false;
2185 const char* mutable_option
= strndup(option
, length
);
2186 gold_assert(mutable_option
!= NULL
);
2187 closure
->command_line()->process_one_option(1, &mutable_option
, 0,
2188 &past_a_double_dash_option
);
2189 // The General_options class will quite possibly store a pointer
2190 // into mutable_option, so we can't free it. In cases the class
2191 // does not store such a pointer, this is a memory leak. Alas. :(
2195 // Called by the bison parser to handle SEARCH_DIR. This is handled
2196 // exactly like a -L option.
2199 script_add_search_dir(void* closurev
, const char* option
, size_t length
)
2201 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2202 if (closure
->command_line() == NULL
)
2203 gold_warning(_("%s:%d:%d: ignoring SEARCH_DIR; SEARCH_DIR is only valid"
2204 " for scripts specified via -T/--script"),
2205 closure
->filename(), closure
->lineno(), closure
->charpos());
2208 std::string s
= "-L" + std::string(option
, length
);
2209 script_parse_option(closurev
, s
.c_str(), s
.size());
2213 /* Called by the bison parser to push the lexer into expression
2217 script_push_lex_into_expression_mode(void* closurev
)
2219 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2220 closure
->push_lex_mode(Lex::EXPRESSION
);
2223 /* Called by the bison parser to push the lexer into version
2227 script_push_lex_into_version_mode(void* closurev
)
2229 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2230 closure
->push_lex_mode(Lex::VERSION_SCRIPT
);
2233 /* Called by the bison parser to pop the lexer mode. */
2236 script_pop_lex_mode(void* closurev
)
2238 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2239 closure
->pop_lex_mode();
2242 // Register an entire version node. For example:
2248 // - tag is "GLIBC_2.1"
2249 // - tree contains the information "global: foo"
2250 // - deps contains "GLIBC_2.0"
2253 script_register_vers_node(void*,
2256 struct Version_tree
*tree
,
2257 struct Version_dependency_list
*deps
)
2259 gold_assert(tree
!= NULL
);
2260 tree
->dependencies
= deps
;
2262 tree
->tag
= std::string(tag
, taglen
);
2265 // Add a dependencies to the list of existing dependencies, if any,
2266 // and return the expanded list.
2268 extern "C" struct Version_dependency_list
*
2269 script_add_vers_depend(void* closurev
,
2270 struct Version_dependency_list
*all_deps
,
2271 const char *depend_to_add
, int deplen
)
2273 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2274 if (all_deps
== NULL
)
2275 all_deps
= closure
->version_script()->allocate_dependency_list();
2276 all_deps
->dependencies
.push_back(std::string(depend_to_add
, deplen
));
2280 // Add a pattern expression to an existing list of expressions, if any.
2281 // TODO: In the old linker, the last argument used to be a bool, but I
2282 // don't know what it meant.
2284 extern "C" struct Version_expression_list
*
2285 script_new_vers_pattern(void* closurev
,
2286 struct Version_expression_list
*expressions
,
2287 const char *pattern
, int patlen
, int exact_match
)
2289 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2290 if (expressions
== NULL
)
2291 expressions
= closure
->version_script()->allocate_expression_list();
2292 expressions
->expressions
.push_back(
2293 Version_expression(std::string(pattern
, patlen
),
2294 closure
->get_current_language(),
2295 static_cast<bool>(exact_match
)));
2299 // Attaches b to the end of a, and clears b. So a = a + b and b = {}.
2301 extern "C" struct Version_expression_list
*
2302 script_merge_expressions(struct Version_expression_list
*a
,
2303 struct Version_expression_list
*b
)
2305 a
->expressions
.insert(a
->expressions
.end(),
2306 b
->expressions
.begin(), b
->expressions
.end());
2307 // We could delete b and remove it from expressions_lists_, but
2308 // that's a lot of work. This works just as well.
2309 b
->expressions
.clear();
2313 // Combine the global and local expressions into a a Version_tree.
2315 extern "C" struct Version_tree
*
2316 script_new_vers_node(void* closurev
,
2317 struct Version_expression_list
*global
,
2318 struct Version_expression_list
*local
)
2320 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2321 Version_tree
* tree
= closure
->version_script()->allocate_version_tree();
2322 tree
->global
= global
;
2323 tree
->local
= local
;
2327 // Handle a transition in language, such as at the
2328 // start or end of 'extern "C++"'
2331 version_script_push_lang(void* closurev
, const char* lang
, int langlen
)
2333 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2334 closure
->push_language(std::string(lang
, langlen
));
2338 version_script_pop_lang(void* closurev
)
2340 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2341 closure
->pop_language();
2344 // Called by the bison parser to start a SECTIONS clause.
2347 script_start_sections(void* closurev
)
2349 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2350 closure
->script_options()->script_sections()->start_sections();
2353 // Called by the bison parser to finish a SECTIONS clause.
2356 script_finish_sections(void* closurev
)
2358 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2359 closure
->script_options()->script_sections()->finish_sections();
2362 // Start processing entries for an output section.
2365 script_start_output_section(void* closurev
, const char* name
, size_t namelen
,
2366 const struct Parser_output_section_header
* header
)
2368 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2369 closure
->script_options()->script_sections()->start_output_section(name
,
2374 // Finish processing entries for an output section.
2377 script_finish_output_section(void* closurev
,
2378 const struct Parser_output_section_trailer
* trail
)
2380 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2381 closure
->script_options()->script_sections()->finish_output_section(trail
);
2384 // Add a data item (e.g., "WORD (0)") to the current output section.
2387 script_add_data(void* closurev
, int data_token
, Expression
* val
)
2389 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2391 bool is_signed
= true;
2413 closure
->script_options()->script_sections()->add_data(size
, is_signed
, val
);
2416 // Add a clause setting the fill value to the current output section.
2419 script_add_fill(void* closurev
, Expression
* val
)
2421 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2422 closure
->script_options()->script_sections()->add_fill(val
);
2425 // Add a new input section specification to the current output
2429 script_add_input_section(void* closurev
,
2430 const struct Input_section_spec
* spec
,
2433 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2434 bool keep
= keepi
!= 0;
2435 closure
->script_options()->script_sections()->add_input_section(spec
, keep
);
2438 // When we see DATA_SEGMENT_ALIGN we record that following output
2439 // sections may be relro.
2442 script_data_segment_align(void* closurev
)
2444 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2445 if (!closure
->script_options()->saw_sections_clause())
2446 gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
2447 closure
->filename(), closure
->lineno(), closure
->charpos());
2449 closure
->script_options()->script_sections()->data_segment_align();
2452 // When we see DATA_SEGMENT_RELRO_END we know that all output sections
2453 // since DATA_SEGMENT_ALIGN should be relro.
2456 script_data_segment_relro_end(void* closurev
)
2458 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2459 if (!closure
->script_options()->saw_sections_clause())
2460 gold_error(_("%s:%d:%d: DATA_SEGMENT_ALIGN not in SECTIONS clause"),
2461 closure
->filename(), closure
->lineno(), closure
->charpos());
2463 closure
->script_options()->script_sections()->data_segment_relro_end();
2466 // Create a new list of string/sort pairs.
2468 extern "C" String_sort_list_ptr
2469 script_new_string_sort_list(const struct Wildcard_section
* string_sort
)
2471 return new String_sort_list(1, *string_sort
);
2474 // Add an entry to a list of string/sort pairs. The way the parser
2475 // works permits us to simply modify the first parameter, rather than
2478 extern "C" String_sort_list_ptr
2479 script_string_sort_list_add(String_sort_list_ptr pv
,
2480 const struct Wildcard_section
* string_sort
)
2483 return script_new_string_sort_list(string_sort
);
2486 pv
->push_back(*string_sort
);
2491 // Create a new list of strings.
2493 extern "C" String_list_ptr
2494 script_new_string_list(const char* str
, size_t len
)
2496 return new String_list(1, std::string(str
, len
));
2499 // Add an element to a list of strings. The way the parser works
2500 // permits us to simply modify the first parameter, rather than copy
2503 extern "C" String_list_ptr
2504 script_string_list_push_back(String_list_ptr pv
, const char* str
, size_t len
)
2507 return script_new_string_list(str
, len
);
2510 pv
->push_back(std::string(str
, len
));
2515 // Concatenate two string lists. Either or both may be NULL. The way
2516 // the parser works permits us to modify the parameters, rather than
2519 extern "C" String_list_ptr
2520 script_string_list_append(String_list_ptr pv1
, String_list_ptr pv2
)
2526 pv1
->insert(pv1
->end(), pv2
->begin(), pv2
->end());
2530 // Add a new program header.
2533 script_add_phdr(void* closurev
, const char* name
, size_t namelen
,
2534 unsigned int type
, const Phdr_info
* info
)
2536 Parser_closure
* closure
= static_cast<Parser_closure
*>(closurev
);
2537 bool includes_filehdr
= info
->includes_filehdr
!= 0;
2538 bool includes_phdrs
= info
->includes_phdrs
!= 0;
2539 bool is_flags_valid
= info
->is_flags_valid
!= 0;
2540 Script_sections
* ss
= closure
->script_options()->script_sections();
2541 ss
->add_phdr(name
, namelen
, type
, includes_filehdr
, includes_phdrs
,
2542 is_flags_valid
, info
->flags
, info
->load_address
);
2545 // Convert a program header string to a type.
2547 #define PHDR_TYPE(NAME) { #NAME, sizeof(#NAME) - 1, elfcpp::NAME }
2554 } phdr_type_names
[] =
2558 PHDR_TYPE(PT_DYNAMIC
),
2559 PHDR_TYPE(PT_INTERP
),
2561 PHDR_TYPE(PT_SHLIB
),
2564 PHDR_TYPE(PT_GNU_EH_FRAME
),
2565 PHDR_TYPE(PT_GNU_STACK
),
2566 PHDR_TYPE(PT_GNU_RELRO
)
2569 extern "C" unsigned int
2570 script_phdr_string_to_type(void* closurev
, const char* name
, size_t namelen
)
2572 for (unsigned int i
= 0;
2573 i
< sizeof(phdr_type_names
) / sizeof(phdr_type_names
[0]);
2575 if (namelen
== phdr_type_names
[i
].namelen
2576 && strncmp(name
, phdr_type_names
[i
].name
, namelen
) == 0)
2577 return phdr_type_names
[i
].val
;
2578 yyerror(closurev
, _("unknown PHDR type (try integer)"));
2579 return elfcpp::PT_NULL
;