2009-07-13 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / target.h
blob597a4f93d07d3f70f82ad52962865fe5d555b7f6
1 // target.h -- target support for gold -*- C++ -*-
3 // Copyright 2006, 2007, 2008, 2009 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 // 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.
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.
33 #ifndef GOLD_TARGET_H
34 #define GOLD_TARGET_H
36 #include "elfcpp.h"
37 #include "options.h"
38 #include "parameters.h"
40 namespace gold
43 class General_options;
44 class Object;
45 template<int size, bool big_endian>
46 class Sized_relobj;
47 class Relocatable_relocs;
48 template<int size, bool big_endian>
49 class Relocate_info;
50 class Symbol;
51 template<int size>
52 class Sized_symbol;
53 class Symbol_table;
54 class Output_section;
56 // The abstract class for target specific handling.
58 class Target
60 public:
61 virtual ~Target()
62 { }
64 // Return the bit size that this target implements. This should
65 // return 32 or 64.
66 int
67 get_size() const
68 { return this->pti_->size; }
70 // Return whether this target is big-endian.
71 bool
72 is_big_endian() const
73 { return this->pti_->is_big_endian; }
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; }
80 // Whether this target has a specific make_symbol function.
81 bool
82 has_make_symbol() const
83 { return this->pti_->has_make_symbol; }
85 // Whether this target has a specific resolve function.
86 bool
87 has_resolve() const
88 { return this->pti_->has_resolve; }
90 // Whether this target has a specific code fill function.
91 bool
92 has_code_fill() const
93 { return this->pti_->has_code_fill; }
95 // Return the default name of the dynamic linker.
96 const char*
97 dynamic_linker() const
98 { return this->pti_->dynamic_linker; }
100 // Return the default address to use for the text segment.
101 uint64_t
102 default_text_segment_address() const
103 { return this->pti_->default_text_segment_address; }
105 // Return the ABI specified page size.
106 uint64_t
107 abi_pagesize() const
109 if (parameters->options().max_page_size() > 0)
110 return parameters->options().max_page_size();
111 else
112 return this->pti_->abi_pagesize;
115 // Return the common page size used on actual systems.
116 uint64_t
117 common_pagesize() const
119 if (parameters->options().common_page_size() > 0)
120 return std::min(parameters->options().common_page_size(),
121 this->abi_pagesize());
122 else
123 return std::min(this->pti_->common_pagesize,
124 this->abi_pagesize());
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; }
135 // Return a character which may appear as a prefix for a wrap
136 // symbol. If this character appears, we strip it when checking for
137 // wrapping and add it back when forming the final symbol name.
138 // This should be '\0' if not special prefix is required, which is
139 // the normal case.
140 char
141 wrap_char() const
142 { return this->pti_->wrap_char; }
144 // Return the special section index which indicates a small common
145 // symbol. This will return SHN_UNDEF if there are no small common
146 // symbols.
147 elfcpp::Elf_Half
148 small_common_shndx() const
149 { return this->pti_->small_common_shndx; }
151 // Return values to add to the section flags for the section holding
152 // small common symbols.
153 elfcpp::Elf_Xword
154 small_common_section_flags() const
156 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
157 return this->pti_->small_common_section_flags;
160 // Return the special section index which indicates a large common
161 // symbol. This will return SHN_UNDEF if there are no large common
162 // symbols.
163 elfcpp::Elf_Half
164 large_common_shndx() const
165 { return this->pti_->large_common_shndx; }
167 // Return values to add to the section flags for the section holding
168 // large common symbols.
169 elfcpp::Elf_Xword
170 large_common_section_flags() const
172 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
173 return this->pti_->large_common_section_flags;
176 // This hook is called when an output section is created.
177 void
178 new_output_section(Output_section* os) const
179 { this->do_new_output_section(os); }
181 // This is called to tell the target to complete any sections it is
182 // handling. After this all sections must have their final size.
183 void
184 finalize_sections(Layout* layout)
185 { return this->do_finalize_sections(layout); }
187 // Return the value to use for a global symbol which needs a special
188 // value in the dynamic symbol table. This will only be called if
189 // the backend first calls symbol->set_needs_dynsym_value().
190 uint64_t
191 dynsym_value(const Symbol* sym) const
192 { return this->do_dynsym_value(sym); }
194 // Return a string to use to fill out a code section. This is
195 // basically one or more NOPS which must fill out the specified
196 // length in bytes.
197 std::string
198 code_fill(section_size_type length) const
199 { return this->do_code_fill(length); }
201 // Return whether SYM is known to be defined by the ABI. This is
202 // used to avoid inappropriate warnings about undefined symbols.
203 bool
204 is_defined_by_abi(const Symbol* sym) const
205 { return this->do_is_defined_by_abi(sym); }
207 // Adjust the output file header before it is written out. VIEW
208 // points to the header in external form. LEN is the length.
209 void
210 adjust_elf_header(unsigned char* view, int len) const
211 { return this->do_adjust_elf_header(view, len); }
213 // Return whether NAME is a local label name. This is used to implement the
214 // --discard-locals options.
215 bool
216 is_local_label_name(const char* name) const
217 { return this->do_is_local_label_name(name); }
219 protected:
220 // This struct holds the constant information for a child class. We
221 // use a struct to avoid the overhead of virtual function calls for
222 // simple information.
223 struct Target_info
225 // Address size (32 or 64).
226 int size;
227 // Whether the target is big endian.
228 bool is_big_endian;
229 // The code to store in the e_machine field of the ELF header.
230 elfcpp::EM machine_code;
231 // Whether this target has a specific make_symbol function.
232 bool has_make_symbol;
233 // Whether this target has a specific resolve function.
234 bool has_resolve;
235 // Whether this target has a specific code fill function.
236 bool has_code_fill;
237 // Whether an object file with no .note.GNU-stack sections implies
238 // that the stack should be executable.
239 bool is_default_stack_executable;
240 // Prefix character to strip when checking for wrapping.
241 char wrap_char;
242 // The default dynamic linker name.
243 const char* dynamic_linker;
244 // The default text segment address.
245 uint64_t default_text_segment_address;
246 // The ABI specified page size.
247 uint64_t abi_pagesize;
248 // The common page size used by actual implementations.
249 uint64_t common_pagesize;
250 // The special section index for small common symbols; SHN_UNDEF
251 // if none.
252 elfcpp::Elf_Half small_common_shndx;
253 // The special section index for large common symbols; SHN_UNDEF
254 // if none.
255 elfcpp::Elf_Half large_common_shndx;
256 // Section flags for small common section.
257 elfcpp::Elf_Xword small_common_section_flags;
258 // Section flags for large common section.
259 elfcpp::Elf_Xword large_common_section_flags;
262 Target(const Target_info* pti)
263 : pti_(pti)
266 // Virtual function which may be implemented by the child class.
267 virtual void
268 do_new_output_section(Output_section*) const
271 // Virtual function which may be implemented by the child class.
272 virtual void
273 do_finalize_sections(Layout*)
276 // Virtual function which may be implemented by the child class.
277 virtual uint64_t
278 do_dynsym_value(const Symbol*) const
279 { gold_unreachable(); }
281 // Virtual function which must be implemented by the child class if
282 // needed.
283 virtual std::string
284 do_code_fill(section_size_type) const
285 { gold_unreachable(); }
287 // Virtual function which may be implemented by the child class.
288 virtual bool
289 do_is_defined_by_abi(const Symbol*) const
290 { return false; }
292 // Adjust the output file header before it is written out. VIEW
293 // points to the header in external form. LEN is the length, and
294 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
295 // By default, we do nothing.
296 virtual void
297 do_adjust_elf_header(unsigned char*, int) const
300 // Virtual function which may be overriden by the child class.
301 virtual bool
302 do_is_local_label_name(const char*) const;
304 private:
305 Target(const Target&);
306 Target& operator=(const Target&);
308 // The target information.
309 const Target_info* pti_;
312 // The abstract class for a specific size and endianness of target.
313 // Each actual target implementation class should derive from an
314 // instantiation of Sized_target.
316 template<int size, bool big_endian>
317 class Sized_target : public Target
319 public:
320 // Make a new symbol table entry for the target. This should be
321 // overridden by a target which needs additional information in the
322 // symbol table. This will only be called if has_make_symbol()
323 // returns true.
324 virtual Sized_symbol<size>*
325 make_symbol() const
326 { gold_unreachable(); }
328 // Resolve a symbol for the target. This should be overridden by a
329 // target which needs to take special action. TO is the
330 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
331 // VERSION is the version of SYM. This will only be called if
332 // has_resolve() returns true.
333 virtual void
334 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
335 const char*)
336 { gold_unreachable(); }
338 // Process the relocs for a section, and record information of the
339 // mapping from source to destination sections. This mapping is later
340 // used to determine unreferenced garbage sections. This procedure is
341 // only called during garbage collection.
342 virtual void
343 gc_process_relocs(const General_options& options,
344 Symbol_table* symtab,
345 Layout* layout,
346 Sized_relobj<size, big_endian>* object,
347 unsigned int data_shndx,
348 unsigned int sh_type,
349 const unsigned char* prelocs,
350 size_t reloc_count,
351 Output_section* output_section,
352 bool needs_special_offset_handling,
353 size_t local_symbol_count,
354 const unsigned char* plocal_symbols) = 0;
356 // Scan the relocs for a section, and record any information
357 // required for the symbol. OPTIONS is the command line options.
358 // SYMTAB is the symbol table. OBJECT is the object in which the
359 // section appears. DATA_SHNDX is the section index that these
360 // relocs apply to. SH_TYPE is the type of the relocation section,
361 // SHT_REL or SHT_RELA. PRELOCS points to the relocation data.
362 // RELOC_COUNT is the number of relocs. LOCAL_SYMBOL_COUNT is the
363 // number of local symbols. OUTPUT_SECTION is the output section.
364 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
365 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
366 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
367 // pointers to the global symbol table from OBJECT.
368 virtual void
369 scan_relocs(const General_options& options,
370 Symbol_table* symtab,
371 Layout* layout,
372 Sized_relobj<size, big_endian>* object,
373 unsigned int data_shndx,
374 unsigned int sh_type,
375 const unsigned char* prelocs,
376 size_t reloc_count,
377 Output_section* output_section,
378 bool needs_special_offset_handling,
379 size_t local_symbol_count,
380 const unsigned char* plocal_symbols) = 0;
382 // Relocate section data. SH_TYPE is the type of the relocation
383 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
384 // information. RELOC_COUNT is the number of relocs.
385 // OUTPUT_SECTION is the output section.
386 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
387 // to correspond to the output section. VIEW is a view into the
388 // output file holding the section contents, VIEW_ADDRESS is the
389 // virtual address of the view, and VIEW_SIZE is the size of the
390 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
391 // parameters refer to the complete output section data, not just
392 // the input section data.
393 virtual void
394 relocate_section(const Relocate_info<size, big_endian>*,
395 unsigned int sh_type,
396 const unsigned char* prelocs,
397 size_t reloc_count,
398 Output_section* output_section,
399 bool needs_special_offset_handling,
400 unsigned char* view,
401 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
402 section_size_type view_size) = 0;
404 // Scan the relocs during a relocatable link. The parameters are
405 // like scan_relocs, with an additional Relocatable_relocs
406 // parameter, used to record the disposition of the relocs.
407 virtual void
408 scan_relocatable_relocs(const General_options& options,
409 Symbol_table* symtab,
410 Layout* layout,
411 Sized_relobj<size, big_endian>* object,
412 unsigned int data_shndx,
413 unsigned int sh_type,
414 const unsigned char* prelocs,
415 size_t reloc_count,
416 Output_section* output_section,
417 bool needs_special_offset_handling,
418 size_t local_symbol_count,
419 const unsigned char* plocal_symbols,
420 Relocatable_relocs*) = 0;
422 // Relocate a section during a relocatable link. The parameters are
423 // like relocate_section, with additional parameters for the view of
424 // the output reloc section.
425 virtual void
426 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
427 unsigned int sh_type,
428 const unsigned char* prelocs,
429 size_t reloc_count,
430 Output_section* output_section,
431 off_t offset_in_output_section,
432 const Relocatable_relocs*,
433 unsigned char* view,
434 typename elfcpp::Elf_types<size>::Elf_Addr
435 view_address,
436 section_size_type view_size,
437 unsigned char* reloc_view,
438 section_size_type reloc_view_size) = 0;
440 protected:
441 Sized_target(const Target::Target_info* pti)
442 : Target(pti)
444 gold_assert(pti->size == size);
445 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
449 } // End namespace gold.
451 #endif // !defined(GOLD_TARGET_H)