Support assignments and expressions in linker scripts.
[external/binutils.git] / gold / script.h
1 // script.h -- handle linker scripts for gold   -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
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.
12
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.
17
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.
22
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.
29
30 #ifndef GOLD_SCRIPT_H
31 #define GOLD_SCRIPT_H
32
33 #include <vector>
34
35 namespace gold
36 {
37
38 class General_options;
39 class Command_line;
40 class Symbol_table;
41 class Layout;
42 class Input_argument;
43 class Input_objects;
44 class Input_group;
45 class Input_file;
46 class Target;
47 class Task_token;
48 class Workqueue;
49
50 // This class represents an expression in a linker script.
51
52 class Expression
53 {
54  protected:
55   // These should only be created by child classes.
56   Expression()
57   { }
58
59  public:
60   virtual ~Expression()
61   { }
62
63   // Return the value of the expression.
64   uint64_t
65   eval(const Symbol_table*, const Layout*);
66
67  protected:
68   struct Expression_eval_info;
69
70  public:
71   // Compute the value of the expression (implemented by child class).
72   // This is public rather than protected because it is called
73   // directly by children of Expression on other Expression objects.
74   virtual uint64_t
75   value(const Expression_eval_info*) = 0;
76
77  private:
78   // May not be copied.
79   Expression(const Expression&);
80   Expression& operator=(const Expression&);
81 };
82
83 // We can read a linker script in two different contexts: when
84 // initially parsing the command line, and when we find an input file
85 // which is actually a linker script.  Also some of the data which can
86 // be set by a linker script can also be set via command line options
87 // like -e and --defsym.  This means that we have a type of data which
88 // can be set both during command line option parsing and while
89 // reading input files.  We store that data in an instance of this
90 // object.  We will keep pointers to that instance in both the
91 // Command_line and Layout objects.
92
93 class Script_options
94 {
95  public:
96   Script_options();
97
98   // The entry address.
99   const char*
100   entry() const
101   { return this->entry_.empty() ? NULL : this->entry_.c_str(); }
102
103   // Set the entry address.
104   void
105   set_entry(const char* entry, size_t length)
106   { this->entry_.assign(entry, length); }
107
108   // Add a symbol to be defined.  These are for symbol definitions
109   // which appear outside of a SECTIONS clause.
110   void
111   add_symbol_assignment(const char* name, size_t length, Expression* value,
112                         bool provided, bool hidden)
113   {
114     this->symbol_assignments_.push_back(Symbol_assignment(name, length, value,
115                                                           provided, hidden));
116   }
117
118   // Define a symbol from the command line.
119   bool
120   define_symbol(const char* definition);
121
122   // Add all symbol definitions to the symbol table.
123   void
124   add_symbols_to_table(Symbol_table*, const Target*);
125
126   // Finalize the symbol values.
127   void
128   finalize_symbols(Symbol_table*, const Layout*);
129
130  private:
131   // We keep a list of symbol assignments.
132   struct Symbol_assignment
133   {
134     // Symbol name.
135     std::string name;
136     // Expression to assign to symbol.
137     Expression* value;
138     // Whether the assignment should be provided (only set if there is
139     // an undefined reference to the symbol.
140     bool provide;
141     // Whether the assignment should be hidden.
142     bool hidden;
143     // The entry in the symbol table.
144     Symbol* sym;
145
146     Symbol_assignment(const char* namea, size_t lengtha, Expression* valuea,
147                       bool providea, bool hiddena)
148       : name(namea, lengtha), value(valuea), provide(providea),
149         hidden(hiddena), sym(NULL)
150     { }
151   };
152
153   typedef std::vector<Symbol_assignment> Symbol_assignments;
154
155   template<int size>
156   void
157   sized_finalize_symbols(Symbol_table*, const Layout*);
158
159   // The entry address.  This will be empty if not set.
160   std::string entry_;
161   // Symbols to set.
162   Symbol_assignments symbol_assignments_;
163 };
164
165 // FILE was found as an argument on the command line, but was not
166 // recognized as an ELF file.  Try to read it as a script.  We've
167 // already read BYTES of data into P.  Return true if the file was
168 // handled.  This has to handle /usr/lib/libc.so on a GNU/Linux
169 // system.
170
171 bool
172 read_input_script(Workqueue*, const General_options&, Symbol_table*, Layout*,
173                   Dirsearch*, Input_objects*, Input_group*,
174                   const Input_argument*, Input_file*, const unsigned char* p,
175                   off_t bytes, Task_token* this_blocker,
176                   Task_token* next_blocker);
177
178 // FILE was found as an argument to --script (-T).
179 // Read it as a script, and execute its contents immediately.
180
181 bool
182 read_commandline_script(const char* filename, Command_line*);
183
184 } // End namespace gold.
185
186 #endif // !defined(GOLD_SCRIPT_H)