PR 5646
[binutils.git] / gold / script.h
blob69906cb66e0d2a6aba0571e6dafbe66e3c799e19
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.
30 #ifndef GOLD_SCRIPT_H
31 #define GOLD_SCRIPT_H
33 #include <vector>
35 struct Version_dependency_list;
36 struct Version_expression_list;
37 struct Version_tree;
39 namespace gold
42 class General_options;
43 class Command_line;
44 class Symbol_table;
45 class Layout;
46 class Input_argument;
47 class Input_objects;
48 class Input_group;
49 class Input_file;
50 class Target;
51 class Task_token;
52 class Workqueue;
54 // This class represents an expression in a linker script.
56 class Expression
58 protected:
59 // These should only be created by child classes.
60 Expression()
61 { }
63 public:
64 virtual ~Expression()
65 { }
67 // Return the value of the expression.
68 uint64_t
69 eval(const Symbol_table*, const Layout*);
71 protected:
72 struct Expression_eval_info;
74 public:
75 // Compute the value of the expression (implemented by child class).
76 // This is public rather than protected because it is called
77 // directly by children of Expression on other Expression objects.
78 virtual uint64_t
79 value(const Expression_eval_info*) = 0;
81 private:
82 // May not be copied.
83 Expression(const Expression&);
84 Expression& operator=(const Expression&);
88 // Version_script_info stores information parsed from the version
89 // script, either provided by --version-script or as part of a linker
90 // script. A single Version_script_info object per target is owned by
91 // Script_options.
93 class Version_script_info {
94 public:
95 ~Version_script_info();
97 // Return whether any version were defined in the version script.
98 bool
99 empty() const
100 { return this->version_trees_.empty(); }
102 // Return the version associated with the given symbol name.
103 // Strings are allocated out of the stringpool given in the
104 // constructor. Strings are allocated out of the stringpool given
105 // in the constructor.
106 const std::string&
107 get_symbol_version(const char* symbol) const
108 { return get_symbol_version_helper(symbol, true); }
110 // Return whether this symbol matches the local: section of a
111 // version script (it doesn't matter which). This test is only
112 // valid if get_symbol_version() returns the empty string, as we
113 // don't test that here.
114 bool
115 symbol_is_local(const char* symbol) const
116 { return !get_symbol_version_helper(symbol, false).empty(); }
118 // Return the names of versions defined in the version script.
119 // Strings are allocated out of the stringpool given in the
120 // constructor.
121 std::vector<std::string>
122 get_versions() const;
124 // Return the list of dependencies for this version.
125 std::vector<std::string>
126 get_dependencies(const char* version) const;
128 // The following functions should only be used by the bison helper
129 // functions. They allocate new structs whose memory belongs to
130 // Version_script_info. The bison functions copy the information
131 // from the version script into these structs.
132 struct Version_dependency_list*
133 allocate_dependency_list();
135 struct Version_expression_list*
136 allocate_expression_list();
138 struct Version_tree*
139 allocate_version_tree();
141 private:
142 const std::string& get_symbol_version_helper(const char* symbol,
143 bool check_global) const;
145 std::vector<struct Version_dependency_list*> dependency_lists_;
146 std::vector<struct Version_expression_list*> expression_lists_;
147 std::vector<struct Version_tree*> version_trees_;
150 // We can read a linker script in two different contexts: when
151 // initially parsing the command line, and when we find an input file
152 // which is actually a linker script. Also some of the data which can
153 // be set by a linker script can also be set via command line options
154 // like -e and --defsym. This means that we have a type of data which
155 // can be set both during command line option parsing and while
156 // reading input files. We store that data in an instance of this
157 // object. We will keep pointers to that instance in both the
158 // Command_line and Layout objects.
160 class Script_options
162 public:
163 Script_options();
165 // The entry address.
166 const char*
167 entry() const
168 { return this->entry_.empty() ? NULL : this->entry_.c_str(); }
170 // Set the entry address.
171 void
172 set_entry(const char* entry, size_t length)
173 { this->entry_.assign(entry, length); }
175 // Add a symbol to be defined. These are for symbol definitions
176 // which appear outside of a SECTIONS clause.
177 void
178 add_symbol_assignment(const char* name, size_t length, Expression* value,
179 bool provided, bool hidden)
181 this->symbol_assignments_.push_back(Symbol_assignment(name, length, value,
182 provided, hidden));
185 // Define a symbol from the command line.
186 bool
187 define_symbol(const char* definition);
189 // Add all symbol definitions to the symbol table.
190 void
191 add_symbols_to_table(Symbol_table*, const Target*);
193 // Finalize the symbol values.
194 void
195 finalize_symbols(Symbol_table*, const Layout*);
197 // Version information parsed from a version script. Everything
198 // else has a pointer to this object.
199 Version_script_info*
200 version_script_info()
201 { return &version_script_info_; }
203 private:
204 // We keep a list of symbol assignments.
205 struct Symbol_assignment
207 // Symbol name.
208 std::string name;
209 // Expression to assign to symbol.
210 Expression* value;
211 // Whether the assignment should be provided (only set if there is
212 // an undefined reference to the symbol.
213 bool provide;
214 // Whether the assignment should be hidden.
215 bool hidden;
216 // The entry in the symbol table.
217 Symbol* sym;
219 Symbol_assignment(const char* namea, size_t lengtha, Expression* valuea,
220 bool providea, bool hiddena)
221 : name(namea, lengtha), value(valuea), provide(providea),
222 hidden(hiddena), sym(NULL)
226 typedef std::vector<Symbol_assignment> Symbol_assignments;
228 template<int size>
229 void
230 sized_finalize_symbols(Symbol_table*, const Layout*);
232 // The entry address. This will be empty if not set.
233 std::string entry_;
234 // Symbols to set.
235 Symbol_assignments symbol_assignments_;
236 // Version information parsed from a version script.
237 Version_script_info version_script_info_;
240 // FILE was found as an argument on the command line, but was not
241 // recognized as an ELF file. Try to read it as a script. We've
242 // already read BYTES of data into P. Return true if the file was
243 // handled. This has to handle /usr/lib/libc.so on a GNU/Linux
244 // system.
246 bool
247 read_input_script(Workqueue*, const General_options&, Symbol_table*, Layout*,
248 Dirsearch*, Input_objects*, Input_group*,
249 const Input_argument*, Input_file*, const unsigned char* p,
250 off_t bytes, Task_token* this_blocker,
251 Task_token* next_blocker);
253 // FILE was found as an argument to --script (-T).
254 // Read it as a script, and execute its contents immediately.
256 bool
257 read_commandline_script(const char* filename, Command_line*);
259 // FILE was found as an argument to --version-script. Read it as a
260 // version script, and store its contents in
261 // cmdline->script_options()->version_script_info().
263 bool
264 read_version_script(const char* filename, Command_line* cmdline);
267 } // End namespace gold.
269 #endif // !defined(GOLD_SCRIPT_H)