1 // script.h -- handle linker scripts for gold -*- C++ -*-
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.
23 // We implement a subset of the original GNU ld linker script language
24 // for compatibility. The goal is not to implement the entire
25 // language. It is merely to implement enough to handle common uses.
26 // In particular we need to handle /usr/lib/libc.so on a typical
27 // GNU/Linux system, and we want to handle linker scripts used by the
28 // Linux kernel build.
36 #include "script-sections.h"
41 class General_options;
52 struct Version_dependency_list;
53 struct Version_expression_list;
56 // This class represents an expression in a linker script.
61 // These should only be created by child classes.
69 // Return the value of the expression.
71 eval(const Symbol_table*, const Layout*);
73 // Print the expression to the FILE. This is for debugging.
75 print(FILE*) const = 0;
78 struct Expression_eval_info;
81 // Compute the value of the expression (implemented by child class).
82 // This is public rather than protected because it is called
83 // directly by children of Expression on other Expression objects.
85 value(const Expression_eval_info*) = 0;
89 Expression(const Expression&);
90 Expression& operator=(const Expression&);
94 // Version_script_info stores information parsed from the version
95 // script, either provided by --version-script or as part of a linker
96 // script. A single Version_script_info object per target is owned by
99 class Version_script_info
102 ~Version_script_info();
104 // Return whether any version were defined in the version script.
107 { return this->version_trees_.empty(); }
109 // Return the version associated with the given symbol name.
110 // Strings are allocated out of the stringpool given in the
111 // constructor. Strings are allocated out of the stringpool given
112 // in the constructor.
114 get_symbol_version(const char* symbol) const
115 { return get_symbol_version_helper(symbol, true); }
117 // Return whether this symbol matches the local: section of a
118 // version script (it doesn't matter which).
120 symbol_is_local(const char* symbol) const
122 return (get_symbol_version(symbol).empty()
123 && !get_symbol_version_helper(symbol, false).empty());
126 // Return the names of versions defined in the version script.
127 // Strings are allocated out of the stringpool given in the
129 std::vector<std::string>
130 get_versions() const;
132 // Return the list of dependencies for this version.
133 std::vector<std::string>
134 get_dependencies(const char* version) const;
136 // The following functions should only be used by the bison helper
137 // functions. They allocate new structs whose memory belongs to
138 // Version_script_info. The bison functions copy the information
139 // from the version script into these structs.
140 struct Version_dependency_list*
141 allocate_dependency_list();
143 struct Version_expression_list*
144 allocate_expression_list();
147 allocate_version_tree();
149 // Print contents to the FILE. This is for debugging.
155 print_expression_list(FILE* f, const Version_expression_list*) const;
157 const std::string& get_symbol_version_helper(const char* symbol,
158 bool check_global) const;
160 std::vector<struct Version_dependency_list*> dependency_lists_;
161 std::vector<struct Version_expression_list*> expression_lists_;
162 std::vector<struct Version_tree*> version_trees_;
165 // This class manages assignments to symbols. These can appear in
166 // three different locations in scripts: outside of a SECTIONS clause,
167 // within a SECTIONS clause, and within an output section definition
168 // within a SECTIONS clause. This can also appear on the command line
169 // via the --defsym command line option.
171 class Symbol_assignment
174 Symbol_assignment(const char* name, size_t namelen, Expression* val,
175 bool provide, bool hidden)
176 : name_(name, namelen), val_(val), provide_(provide), hidden_(hidden),
180 // Add the symbol to the symbol table.
182 add_to_table(Symbol_table*, const Target*);
184 // Finalize the symbol value.
185 void finalize(Symbol_table*, const Layout*);
187 // Print the assignment to the FILE. This is for debugging.
192 // Sized version of finalize.
195 sized_finalize(Symbol_table*, const Layout*);
199 // Expression to assign to symbol.
201 // Whether the assignment should be provided (only set if there is
202 // an undefined reference to the symbol.
204 // Whether the assignment should be hidden.
206 // The entry in the symbol table.
210 // This class manages assertions in linker scripts. These can appear
211 // in all the places where a Symbol_assignment can appear.
213 class Script_assertion
216 Script_assertion(Expression* check, const char* message,
218 : check_(check), message_(message, messagelen)
221 // Check the assertion.
223 check(const Symbol_table*, const Layout*);
225 // Print the assertion to the FILE. This is for debugging.
230 // The expression to check.
232 // The message to issue if the expression fails.
233 std::string message_;
236 // We can read a linker script in two different contexts: when
237 // initially parsing the command line, and when we find an input file
238 // which is actually a linker script. Also some of the data which can
239 // be set by a linker script can also be set via command line options
240 // like -e and --defsym. This means that we have a type of data which
241 // can be set both during command line option parsing and while
242 // reading input files. We store that data in an instance of this
243 // object. We will keep pointers to that instance in both the
244 // Command_line and Layout objects.
251 // The entry address.
254 { return this->entry_.empty() ? NULL : this->entry_.c_str(); }
256 // Set the entry address.
258 set_entry(const char* entry, size_t length)
259 { this->entry_.assign(entry, length); }
261 // Add a symbol to be defined.
263 add_symbol_assignment(const char* name, size_t length, Expression* value,
264 bool provide, bool hidden);
268 add_assertion(Expression* check, const char* message, size_t messagelen);
270 // Define a symbol from the command line.
272 define_symbol(const char* definition);
274 // Add all symbol definitions to the symbol table.
276 add_symbols_to_table(Symbol_table*, const Target*);
278 // Finalize the symbol values.
280 finalize_symbols(Symbol_table*, const Layout*);
282 // Version information parsed from a version script. Everything
283 // else has a pointer to this object.
285 version_script_info()
286 { return &this->version_script_info_; }
288 // A SECTIONS clause parsed from a linker script. Everything else
289 // has a pointer to this object.
292 { return &this->script_sections_; }
294 // Print the script to the FILE. This is for debugging.
299 // We keep a list of symbol assignments which occur outside of a
301 typedef std::vector<Symbol_assignment*> Symbol_assignments;
303 // We keep a list of all assertions whcih occur outside of a
305 typedef std::vector<Script_assertion*> Assertions;
307 // The entry address. This will be empty if not set.
310 Symbol_assignments symbol_assignments_;
311 // Assertions to check.
312 Assertions assertions_;
313 // Version information parsed from a version script.
314 Version_script_info version_script_info_;
315 // Information from any SECTIONS clauses.
316 Script_sections script_sections_;
319 // FILE was found as an argument on the command line, but was not
320 // recognized as an ELF file. Try to read it as a script. We've
321 // already read BYTES of data into P. Return true if the file was
322 // handled. This has to handle /usr/lib/libc.so on a GNU/Linux
326 read_input_script(Workqueue*, const General_options&, Symbol_table*, Layout*,
327 Dirsearch*, Input_objects*, Input_group*,
328 const Input_argument*, Input_file*, const unsigned char* p,
329 off_t bytes, Task_token* this_blocker,
330 Task_token* next_blocker);
332 // FILE was found as an argument to --script (-T).
333 // Read it as a script, and execute its contents immediately.
336 read_commandline_script(const char* filename, Command_line*);
338 // FILE was found as an argument to --version-script. Read it as a
339 // version script, and store its contents in
340 // cmdline->script_options()->version_script_info().
343 read_version_script(const char* filename, Command_line* cmdline);
346 } // End namespace gold.
348 #endif // !defined(GOLD_SCRIPT_H)