Commit | Line | Data |
---|---|---|
14bfc3f5 | 1 | // target.h -- target support for gold -*- C++ -*- |
bae7f79e | 2 | |
ebdbb458 | 3 | // Copyright 2006, 2007, 2008 Free Software Foundation, Inc. |
6cb15b7f ILT |
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 | ||
bae7f79e ILT |
23 | // The abstract class Target is the interface for target specific |
24 | // support. It defines abstract methods which each target must | |
25 | // implement. Typically there will be one target per processor, but | |
26 | // in some cases it may be necessary to have subclasses. | |
27 | ||
28 | // For speed and consistency we want to use inline functions to handle | |
29 | // relocation processing. So besides implementations of the abstract | |
30 | // methods, each target is expected to define a template | |
31 | // specialization of the relocation functions. | |
32 | ||
33 | #ifndef GOLD_TARGET_H | |
34 | #define GOLD_TARGET_H | |
35 | ||
14bfc3f5 | 36 | #include "elfcpp.h" |
8851ecca | 37 | #include "options.h" |
cd72c291 | 38 | #include "parameters.h" |
14bfc3f5 | 39 | |
bae7f79e ILT |
40 | namespace gold |
41 | { | |
42 | ||
92e059d8 | 43 | class General_options; |
14bfc3f5 | 44 | class Object; |
61ba1cf9 | 45 | template<int size, bool big_endian> |
f6ce93d6 | 46 | class Sized_relobj; |
6a74a719 | 47 | class Relocatable_relocs; |
92e059d8 | 48 | template<int size, bool big_endian> |
730cdc88 | 49 | class Relocate_info; |
f6ce93d6 ILT |
50 | class Symbol; |
51 | template<int size> | |
52 | class Sized_symbol; | |
53 | class Symbol_table; | |
730cdc88 | 54 | class Output_section; |
14bfc3f5 ILT |
55 | |
56 | // The abstract class for target specific handling. | |
57 | ||
bae7f79e ILT |
58 | class Target |
59 | { | |
60 | public: | |
14bfc3f5 ILT |
61 | virtual ~Target() |
62 | { } | |
63 | ||
64 | // Return the bit size that this target implements. This should | |
65 | // return 32 or 64. | |
66 | int | |
67 | get_size() const | |
75f65a3e | 68 | { return this->pti_->size; } |
14bfc3f5 ILT |
69 | |
70 | // Return whether this target is big-endian. | |
71 | bool | |
72 | is_big_endian() const | |
75f65a3e | 73 | { return this->pti_->is_big_endian; } |
14bfc3f5 | 74 | |
61ba1cf9 ILT |
75 | // Machine code to store in e_machine field of ELF header. |
76 | elfcpp::EM | |
77 | machine_code() const | |
78 | { return this->pti_->machine_code; } | |
79 | ||
14bfc3f5 ILT |
80 | // Whether this target has a specific make_symbol function. |
81 | bool | |
82 | has_make_symbol() const | |
75f65a3e | 83 | { return this->pti_->has_make_symbol; } |
14bfc3f5 ILT |
84 | |
85 | // Whether this target has a specific resolve function. | |
86 | bool | |
87 | has_resolve() const | |
75f65a3e ILT |
88 | { return this->pti_->has_resolve; } |
89 | ||
c51e6221 ILT |
90 | // Whether this target has a specific code fill function. |
91 | bool | |
92 | has_code_fill() const | |
93 | { return this->pti_->has_code_fill; } | |
94 | ||
dbe717ef ILT |
95 | // Return the default name of the dynamic linker. |
96 | const char* | |
97 | dynamic_linker() const | |
98 | { return this->pti_->dynamic_linker; } | |
99 | ||
75f65a3e ILT |
100 | // Return the default address to use for the text segment. |
101 | uint64_t | |
0c5e9c22 ILT |
102 | default_text_segment_address() const |
103 | { return this->pti_->default_text_segment_address; } | |
75f65a3e ILT |
104 | |
105 | // Return the ABI specified page size. | |
106 | uint64_t | |
107 | abi_pagesize() const | |
cd72c291 | 108 | { |
8851ecca ILT |
109 | if (parameters->options().max_page_size() > 0) |
110 | return parameters->options().max_page_size(); | |
cd72c291 ILT |
111 | else |
112 | return this->pti_->abi_pagesize; | |
113 | } | |
75f65a3e ILT |
114 | |
115 | // Return the common page size used on actual systems. | |
116 | uint64_t | |
117 | common_pagesize() const | |
cd72c291 | 118 | { |
8851ecca ILT |
119 | if (parameters->options().common_page_size() > 0) |
120 | return std::min(parameters->options().common_page_size(), | |
cd72c291 ILT |
121 | this->abi_pagesize()); |
122 | else | |
123 | return std::min(this->pti_->common_pagesize, | |
124 | this->abi_pagesize()); | |
125 | } | |
14bfc3f5 | 126 | |
35cdfc9a ILT |
127 | // If we see some object files with .note.GNU-stack sections, and |
128 | // some objects files without them, this returns whether we should | |
129 | // consider the object files without them to imply that the stack | |
130 | // should be executable. | |
131 | bool | |
132 | is_default_stack_executable() const | |
133 | { return this->pti_->is_default_stack_executable; } | |
134 | ||
5a6f7e2d ILT |
135 | // This is called to tell the target to complete any sections it is |
136 | // handling. After this all sections must have their final size. | |
137 | void | |
7e1edb90 ILT |
138 | finalize_sections(Layout* layout) |
139 | { return this->do_finalize_sections(layout); } | |
5a6f7e2d | 140 | |
ab5c9e90 ILT |
141 | // Return the value to use for a global symbol which needs a special |
142 | // value in the dynamic symbol table. This will only be called if | |
143 | // the backend first calls symbol->set_needs_dynsym_value(). | |
144 | uint64_t | |
145 | dynsym_value(const Symbol* sym) const | |
146 | { return this->do_dynsym_value(sym); } | |
147 | ||
c51e6221 ILT |
148 | // Return a string to use to fill out a code section. This is |
149 | // basically one or more NOPS which must fill out the specified | |
150 | // length in bytes. | |
151 | std::string | |
8851ecca | 152 | code_fill(section_size_type length) const |
c51e6221 ILT |
153 | { return this->do_code_fill(length); } |
154 | ||
9a2d6984 ILT |
155 | // Return whether SYM is known to be defined by the ABI. This is |
156 | // used to avoid inappropriate warnings about undefined symbols. | |
157 | bool | |
158 | is_defined_by_abi(Symbol* sym) const | |
159 | { return this->do_is_defined_by_abi(sym); } | |
160 | ||
14bfc3f5 | 161 | protected: |
75f65a3e ILT |
162 | // This struct holds the constant information for a child class. We |
163 | // use a struct to avoid the overhead of virtual function calls for | |
164 | // simple information. | |
165 | struct Target_info | |
166 | { | |
167 | // Address size (32 or 64). | |
168 | int size; | |
169 | // Whether the target is big endian. | |
170 | bool is_big_endian; | |
61ba1cf9 ILT |
171 | // The code to store in the e_machine field of the ELF header. |
172 | elfcpp::EM machine_code; | |
75f65a3e ILT |
173 | // Whether this target has a specific make_symbol function. |
174 | bool has_make_symbol; | |
175 | // Whether this target has a specific resolve function. | |
176 | bool has_resolve; | |
c51e6221 ILT |
177 | // Whether this target has a specific code fill function. |
178 | bool has_code_fill; | |
35cdfc9a ILT |
179 | // Whether an object file with no .note.GNU-stack sections implies |
180 | // that the stack should be executable. | |
181 | bool is_default_stack_executable; | |
dbe717ef ILT |
182 | // The default dynamic linker name. |
183 | const char* dynamic_linker; | |
75f65a3e | 184 | // The default text segment address. |
0c5e9c22 | 185 | uint64_t default_text_segment_address; |
75f65a3e ILT |
186 | // The ABI specified page size. |
187 | uint64_t abi_pagesize; | |
188 | // The common page size used by actual implementations. | |
189 | uint64_t common_pagesize; | |
190 | }; | |
191 | ||
192 | Target(const Target_info* pti) | |
193 | : pti_(pti) | |
14bfc3f5 ILT |
194 | { } |
195 | ||
5a6f7e2d ILT |
196 | // Virtual function which may be implemented by the child class. |
197 | virtual void | |
7e1edb90 | 198 | do_finalize_sections(Layout*) |
5a6f7e2d ILT |
199 | { } |
200 | ||
ab5c9e90 ILT |
201 | // Virtual function which may be implemented by the child class. |
202 | virtual uint64_t | |
203 | do_dynsym_value(const Symbol*) const | |
204 | { gold_unreachable(); } | |
205 | ||
c51e6221 ILT |
206 | // Virtual function which must be implemented by the child class if |
207 | // needed. | |
208 | virtual std::string | |
8851ecca | 209 | do_code_fill(section_size_type) const |
c51e6221 ILT |
210 | { gold_unreachable(); } |
211 | ||
9a2d6984 ILT |
212 | // Virtual function which may be implemented by the child class. |
213 | virtual bool | |
214 | do_is_defined_by_abi(Symbol*) const | |
215 | { return false; } | |
216 | ||
14bfc3f5 ILT |
217 | private: |
218 | Target(const Target&); | |
219 | Target& operator=(const Target&); | |
220 | ||
75f65a3e ILT |
221 | // The target information. |
222 | const Target_info* pti_; | |
bae7f79e ILT |
223 | }; |
224 | ||
14bfc3f5 ILT |
225 | // The abstract class for a specific size and endianness of target. |
226 | // Each actual target implementation class should derive from an | |
227 | // instantiation of Sized_target. | |
228 | ||
229 | template<int size, bool big_endian> | |
230 | class Sized_target : public Target | |
231 | { | |
232 | public: | |
233 | // Make a new symbol table entry for the target. This should be | |
234 | // overridden by a target which needs additional information in the | |
235 | // symbol table. This will only be called if has_make_symbol() | |
236 | // returns true. | |
237 | virtual Sized_symbol<size>* | |
14b31740 | 238 | make_symbol() const |
a3ad94ed | 239 | { gold_unreachable(); } |
14bfc3f5 ILT |
240 | |
241 | // Resolve a symbol for the target. This should be overridden by a | |
242 | // target which needs to take special action. TO is the | |
243 | // pre-existing symbol. SYM is the new symbol, seen in OBJECT. | |
14b31740 ILT |
244 | // VERSION is the version of SYM. This will only be called if |
245 | // has_resolve() returns true. | |
14bfc3f5 | 246 | virtual void |
14b31740 ILT |
247 | resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*, |
248 | const char*) | |
a3ad94ed | 249 | { gold_unreachable(); } |
14bfc3f5 | 250 | |
92e059d8 ILT |
251 | // Scan the relocs for a section, and record any information |
252 | // required for the symbol. OPTIONS is the command line options. | |
253 | // SYMTAB is the symbol table. OBJECT is the object in which the | |
a3ad94ed ILT |
254 | // section appears. DATA_SHNDX is the section index that these |
255 | // relocs apply to. SH_TYPE is the type of the relocation section, | |
92e059d8 ILT |
256 | // SHT_REL or SHT_RELA. PRELOCS points to the relocation data. |
257 | // RELOC_COUNT is the number of relocs. LOCAL_SYMBOL_COUNT is the | |
730cdc88 ILT |
258 | // number of local symbols. OUTPUT_SECTION is the output section. |
259 | // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output | |
260 | // sections are not mapped as usual. PLOCAL_SYMBOLS points to the | |
261 | // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of | |
262 | // pointers to the global symbol table from OBJECT. | |
61ba1cf9 | 263 | virtual void |
92e059d8 ILT |
264 | scan_relocs(const General_options& options, |
265 | Symbol_table* symtab, | |
ead1e424 | 266 | Layout* layout, |
f6ce93d6 | 267 | Sized_relobj<size, big_endian>* object, |
a3ad94ed | 268 | unsigned int data_shndx, |
92e059d8 ILT |
269 | unsigned int sh_type, |
270 | const unsigned char* prelocs, | |
271 | size_t reloc_count, | |
730cdc88 ILT |
272 | Output_section* output_section, |
273 | bool needs_special_offset_handling, | |
92e059d8 | 274 | size_t local_symbol_count, |
730cdc88 | 275 | const unsigned char* plocal_symbols) = 0; |
92e059d8 ILT |
276 | |
277 | // Relocate section data. SH_TYPE is the type of the relocation | |
278 | // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation | |
730cdc88 ILT |
279 | // information. RELOC_COUNT is the number of relocs. |
280 | // OUTPUT_SECTION is the output section. | |
281 | // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped | |
282 | // to correspond to the output section. VIEW is a view into the | |
283 | // output file holding the section contents, VIEW_ADDRESS is the | |
284 | // virtual address of the view, and VIEW_SIZE is the size of the | |
285 | // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx | |
286 | // parameters refer to the complete output section data, not just | |
287 | // the input section data. | |
92e059d8 ILT |
288 | virtual void |
289 | relocate_section(const Relocate_info<size, big_endian>*, | |
290 | unsigned int sh_type, | |
291 | const unsigned char* prelocs, | |
292 | size_t reloc_count, | |
730cdc88 ILT |
293 | Output_section* output_section, |
294 | bool needs_special_offset_handling, | |
92e059d8 ILT |
295 | unsigned char* view, |
296 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, | |
fe8718a4 | 297 | section_size_type view_size) = 0; |
61ba1cf9 | 298 | |
6a74a719 ILT |
299 | // Scan the relocs during a relocatable link. The parameters are |
300 | // like scan_relocs, with an additional Relocatable_relocs | |
301 | // parameter, used to record the disposition of the relocs. | |
302 | virtual void | |
303 | scan_relocatable_relocs(const General_options& options, | |
304 | Symbol_table* symtab, | |
305 | Layout* layout, | |
306 | Sized_relobj<size, big_endian>* object, | |
307 | unsigned int data_shndx, | |
308 | unsigned int sh_type, | |
309 | const unsigned char* prelocs, | |
310 | size_t reloc_count, | |
311 | Output_section* output_section, | |
312 | bool needs_special_offset_handling, | |
313 | size_t local_symbol_count, | |
314 | const unsigned char* plocal_symbols, | |
315 | Relocatable_relocs*) = 0; | |
316 | ||
317 | // Relocate a section during a relocatable link. The parameters are | |
318 | // like relocate_section, with additional parameters for the view of | |
319 | // the output reloc section. | |
320 | virtual void | |
321 | relocate_for_relocatable(const Relocate_info<size, big_endian>*, | |
322 | unsigned int sh_type, | |
323 | const unsigned char* prelocs, | |
324 | size_t reloc_count, | |
325 | Output_section* output_section, | |
326 | off_t offset_in_output_section, | |
327 | const Relocatable_relocs*, | |
328 | unsigned char* view, | |
329 | typename elfcpp::Elf_types<size>::Elf_Addr | |
330 | view_address, | |
331 | section_size_type view_size, | |
332 | unsigned char* reloc_view, | |
333 | section_size_type reloc_view_size) = 0; | |
334 | ||
14bfc3f5 | 335 | protected: |
75f65a3e ILT |
336 | Sized_target(const Target::Target_info* pti) |
337 | : Target(pti) | |
338 | { | |
a3ad94ed ILT |
339 | gold_assert(pti->size == size); |
340 | gold_assert(pti->is_big_endian ? big_endian : !big_endian); | |
75f65a3e | 341 | } |
14bfc3f5 | 342 | }; |
bae7f79e ILT |
343 | |
344 | } // End namespace gold. | |
345 | ||
346 | #endif // !defined(GOLD_TARGET_H) |