* object.h (class Relobj): Drop options parameter from
[deliverable/binutils-gdb.git] / gold / target.h
1 // target.h -- target support for gold -*- C++ -*-
2
3 // Copyright 2006, 2007, 2008, 2009 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 // 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
36 #include "elfcpp.h"
37 #include "options.h"
38 #include "parameters.h"
39 #include "debug.h"
40
41 namespace gold
42 {
43
44 class Object;
45 class Relobj;
46 template<int size, bool big_endian>
47 class Sized_relobj;
48 class Relocatable_relocs;
49 template<int size, bool big_endian>
50 class Relocate_info;
51 class Reloc_symbol_changes;
52 class Symbol;
53 template<int size>
54 class Sized_symbol;
55 class Symbol_table;
56 class Output_section;
57
58 // The abstract class for target specific handling.
59
60 class Target
61 {
62 public:
63 virtual ~Target()
64 { }
65
66 // Return the bit size that this target implements. This should
67 // return 32 or 64.
68 int
69 get_size() const
70 { return this->pti_->size; }
71
72 // Return whether this target is big-endian.
73 bool
74 is_big_endian() const
75 { return this->pti_->is_big_endian; }
76
77 // Machine code to store in e_machine field of ELF header.
78 elfcpp::EM
79 machine_code() const
80 { return this->pti_->machine_code; }
81
82 // Whether this target has a specific make_symbol function.
83 bool
84 has_make_symbol() const
85 { return this->pti_->has_make_symbol; }
86
87 // Whether this target has a specific resolve function.
88 bool
89 has_resolve() const
90 { return this->pti_->has_resolve; }
91
92 // Whether this target has a specific code fill function.
93 bool
94 has_code_fill() const
95 { return this->pti_->has_code_fill; }
96
97 // Return the default name of the dynamic linker.
98 const char*
99 dynamic_linker() const
100 { return this->pti_->dynamic_linker; }
101
102 // Return the default address to use for the text segment.
103 uint64_t
104 default_text_segment_address() const
105 { return this->pti_->default_text_segment_address; }
106
107 // Return the ABI specified page size.
108 uint64_t
109 abi_pagesize() const
110 {
111 if (parameters->options().max_page_size() > 0)
112 return parameters->options().max_page_size();
113 else
114 return this->pti_->abi_pagesize;
115 }
116
117 // Return the common page size used on actual systems.
118 uint64_t
119 common_pagesize() const
120 {
121 if (parameters->options().common_page_size() > 0)
122 return std::min(parameters->options().common_page_size(),
123 this->abi_pagesize());
124 else
125 return std::min(this->pti_->common_pagesize,
126 this->abi_pagesize());
127 }
128
129 // If we see some object files with .note.GNU-stack sections, and
130 // some objects files without them, this returns whether we should
131 // consider the object files without them to imply that the stack
132 // should be executable.
133 bool
134 is_default_stack_executable() const
135 { return this->pti_->is_default_stack_executable; }
136
137 // Return a character which may appear as a prefix for a wrap
138 // symbol. If this character appears, we strip it when checking for
139 // wrapping and add it back when forming the final symbol name.
140 // This should be '\0' if not special prefix is required, which is
141 // the normal case.
142 char
143 wrap_char() const
144 { return this->pti_->wrap_char; }
145
146 // Return the special section index which indicates a small common
147 // symbol. This will return SHN_UNDEF if there are no small common
148 // symbols.
149 elfcpp::Elf_Half
150 small_common_shndx() const
151 { return this->pti_->small_common_shndx; }
152
153 // Return values to add to the section flags for the section holding
154 // small common symbols.
155 elfcpp::Elf_Xword
156 small_common_section_flags() const
157 {
158 gold_assert(this->pti_->small_common_shndx != elfcpp::SHN_UNDEF);
159 return this->pti_->small_common_section_flags;
160 }
161
162 // Return the special section index which indicates a large common
163 // symbol. This will return SHN_UNDEF if there are no large common
164 // symbols.
165 elfcpp::Elf_Half
166 large_common_shndx() const
167 { return this->pti_->large_common_shndx; }
168
169 // Return values to add to the section flags for the section holding
170 // large common symbols.
171 elfcpp::Elf_Xword
172 large_common_section_flags() const
173 {
174 gold_assert(this->pti_->large_common_shndx != elfcpp::SHN_UNDEF);
175 return this->pti_->large_common_section_flags;
176 }
177
178 // This hook is called when an output section is created.
179 void
180 new_output_section(Output_section* os) const
181 { this->do_new_output_section(os); }
182
183 // This is called to tell the target to complete any sections it is
184 // handling. After this all sections must have their final size.
185 void
186 finalize_sections(Layout* layout)
187 { return this->do_finalize_sections(layout); }
188
189 // Return the value to use for a global symbol which needs a special
190 // value in the dynamic symbol table. This will only be called if
191 // the backend first calls symbol->set_needs_dynsym_value().
192 uint64_t
193 dynsym_value(const Symbol* sym) const
194 { return this->do_dynsym_value(sym); }
195
196 // Return a string to use to fill out a code section. This is
197 // basically one or more NOPS which must fill out the specified
198 // length in bytes.
199 std::string
200 code_fill(section_size_type length) const
201 { return this->do_code_fill(length); }
202
203 // Return whether SYM is known to be defined by the ABI. This is
204 // used to avoid inappropriate warnings about undefined symbols.
205 bool
206 is_defined_by_abi(const Symbol* sym) const
207 { return this->do_is_defined_by_abi(sym); }
208
209 // Adjust the output file header before it is written out. VIEW
210 // points to the header in external form. LEN is the length.
211 void
212 adjust_elf_header(unsigned char* view, int len) const
213 { return this->do_adjust_elf_header(view, len); }
214
215 // Return whether NAME is a local label name. This is used to implement the
216 // --discard-locals options.
217 bool
218 is_local_label_name(const char* name) const
219 { return this->do_is_local_label_name(name); }
220
221 // A function starts at OFFSET in section SHNDX in OBJECT. That
222 // function was compiled with -fsplit-stack, but it refers to a
223 // function which was compiled without -fsplit-stack. VIEW is a
224 // modifiable view of the section; VIEW_SIZE is the size of the
225 // view. The target has to adjust the function so that it allocates
226 // enough stack.
227 void
228 calls_non_split(Relobj* object, unsigned int shndx,
229 section_offset_type fnoffset, section_size_type fnsize,
230 unsigned char* view, section_size_type view_size,
231 std::string* from, std::string* to) const
232 {
233 this->do_calls_non_split(object, shndx, fnoffset, fnsize, view, view_size,
234 from, to);
235 }
236
237 // Make an ELF object.
238 template<int size, bool big_endian>
239 Object*
240 make_elf_object(const std::string& name, Input_file* input_file,
241 off_t offset, const elfcpp::Ehdr<size, big_endian>& ehdr)
242 { return this->do_make_elf_object(name, input_file, offset, ehdr); }
243
244 // Make an output section.
245 Output_section*
246 make_output_section(const char* name, elfcpp::Elf_Word type,
247 elfcpp::Elf_Xword flags)
248 { return this->do_make_output_section(name, type, flags); }
249
250 // Return true if target wants to perform relaxation.
251 bool
252 may_relax() const
253 {
254 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
255 if (is_debugging_enabled(DEBUG_RELAXATION))
256 return true;
257
258 return this->do_may_relax();
259 }
260
261 // Perform a relaxation pass. Return true if layout may be changed.
262 bool
263 relax(int pass, const Input_objects* input_objects, Symbol_table* symtab,
264 Layout* layout)
265 {
266 // Run the dummy relaxation pass twice if relaxation debugging is enabled.
267 if (is_debugging_enabled(DEBUG_RELAXATION))
268 return pass < 2;
269
270 return this->do_relax(pass, input_objects, symtab, layout);
271 }
272
273 protected:
274 // This struct holds the constant information for a child class. We
275 // use a struct to avoid the overhead of virtual function calls for
276 // simple information.
277 struct Target_info
278 {
279 // Address size (32 or 64).
280 int size;
281 // Whether the target is big endian.
282 bool is_big_endian;
283 // The code to store in the e_machine field of the ELF header.
284 elfcpp::EM machine_code;
285 // Whether this target has a specific make_symbol function.
286 bool has_make_symbol;
287 // Whether this target has a specific resolve function.
288 bool has_resolve;
289 // Whether this target has a specific code fill function.
290 bool has_code_fill;
291 // Whether an object file with no .note.GNU-stack sections implies
292 // that the stack should be executable.
293 bool is_default_stack_executable;
294 // Prefix character to strip when checking for wrapping.
295 char wrap_char;
296 // The default dynamic linker name.
297 const char* dynamic_linker;
298 // The default text segment address.
299 uint64_t default_text_segment_address;
300 // The ABI specified page size.
301 uint64_t abi_pagesize;
302 // The common page size used by actual implementations.
303 uint64_t common_pagesize;
304 // The special section index for small common symbols; SHN_UNDEF
305 // if none.
306 elfcpp::Elf_Half small_common_shndx;
307 // The special section index for large common symbols; SHN_UNDEF
308 // if none.
309 elfcpp::Elf_Half large_common_shndx;
310 // Section flags for small common section.
311 elfcpp::Elf_Xword small_common_section_flags;
312 // Section flags for large common section.
313 elfcpp::Elf_Xword large_common_section_flags;
314 };
315
316 Target(const Target_info* pti)
317 : pti_(pti)
318 { }
319
320 // Virtual function which may be implemented by the child class.
321 virtual void
322 do_new_output_section(Output_section*) const
323 { }
324
325 // Virtual function which may be implemented by the child class.
326 virtual void
327 do_finalize_sections(Layout*)
328 { }
329
330 // Virtual function which may be implemented by the child class.
331 virtual uint64_t
332 do_dynsym_value(const Symbol*) const
333 { gold_unreachable(); }
334
335 // Virtual function which must be implemented by the child class if
336 // needed.
337 virtual std::string
338 do_code_fill(section_size_type) const
339 { gold_unreachable(); }
340
341 // Virtual function which may be implemented by the child class.
342 virtual bool
343 do_is_defined_by_abi(const Symbol*) const
344 { return false; }
345
346 // Adjust the output file header before it is written out. VIEW
347 // points to the header in external form. LEN is the length, and
348 // will be one of the values of elfcpp::Elf_sizes<size>::ehdr_size.
349 // By default, we do nothing.
350 virtual void
351 do_adjust_elf_header(unsigned char*, int) const
352 { }
353
354 // Virtual function which may be overriden by the child class.
355 virtual bool
356 do_is_local_label_name(const char*) const;
357
358 // Virtual function which may be overridden by the child class.
359 virtual void
360 do_calls_non_split(Relobj* object, unsigned int, section_offset_type,
361 section_size_type, unsigned char*, section_size_type,
362 std::string*, std::string*) const;
363
364 // make_elf_object hooks. There are four versions of these for
365 // different address sizes and endianities.
366
367 #ifdef HAVE_TARGET_32_LITTLE
368 // Virtual functions which may be overriden by the child class.
369 virtual Object*
370 do_make_elf_object(const std::string&, Input_file*, off_t,
371 const elfcpp::Ehdr<32, false>&);
372 #endif
373
374 #ifdef HAVE_TARGET_32_BIG
375 // Virtual functions which may be overriden by the child class.
376 virtual Object*
377 do_make_elf_object(const std::string&, Input_file*, off_t,
378 const elfcpp::Ehdr<32, true>&);
379 #endif
380
381 #ifdef HAVE_TARGET_64_LITTLE
382 // Virtual functions which may be overriden by the child class.
383 virtual Object*
384 do_make_elf_object(const std::string&, Input_file*, off_t,
385 const elfcpp::Ehdr<64, false>& ehdr);
386 #endif
387
388 #ifdef HAVE_TARGET_64_BIG
389 // Virtual functions which may be overriden by the child class.
390 virtual Object*
391 do_make_elf_object(const std::string& name, Input_file* input_file,
392 off_t offset, const elfcpp::Ehdr<64, true>& ehdr);
393 #endif
394
395 // Virtual functions which may be overriden by the child class.
396 virtual Output_section*
397 do_make_output_section(const char* name, elfcpp::Elf_Word type,
398 elfcpp::Elf_Xword flags);
399
400 // Virtual function which may be overriden by the child class.
401 virtual bool
402 do_may_relax() const
403 { return parameters->options().relax(); }
404
405 // Virtual function which may be overriden by the child class.
406 virtual bool
407 do_relax(int, const Input_objects*, Symbol_table*, Layout*)
408 { return false; }
409
410 // A function for targets to call. Return whether BYTES/LEN matches
411 // VIEW/VIEW_SIZE at OFFSET.
412 bool
413 match_view(const unsigned char* view, section_size_type view_size,
414 section_offset_type offset, const char* bytes, size_t len) const;
415
416 // Set the contents of a VIEW/VIEW_SIZE to nops starting at OFFSET
417 // for LEN bytes.
418 void
419 set_view_to_nop(unsigned char* view, section_size_type view_size,
420 section_offset_type offset, size_t len) const;
421
422 private:
423 // The implementations of the four do_make_elf_object virtual functions are
424 // almost identical except for their sizes and endianity. We use a template.
425 // for their implementations.
426 template<int size, bool big_endian>
427 inline Object*
428 do_make_elf_object_implementation(const std::string&, Input_file*, off_t,
429 const elfcpp::Ehdr<size, big_endian>&);
430
431 Target(const Target&);
432 Target& operator=(const Target&);
433
434 // The target information.
435 const Target_info* pti_;
436 };
437
438 // The abstract class for a specific size and endianness of target.
439 // Each actual target implementation class should derive from an
440 // instantiation of Sized_target.
441
442 template<int size, bool big_endian>
443 class Sized_target : public Target
444 {
445 public:
446 // Make a new symbol table entry for the target. This should be
447 // overridden by a target which needs additional information in the
448 // symbol table. This will only be called if has_make_symbol()
449 // returns true.
450 virtual Sized_symbol<size>*
451 make_symbol() const
452 { gold_unreachable(); }
453
454 // Resolve a symbol for the target. This should be overridden by a
455 // target which needs to take special action. TO is the
456 // pre-existing symbol. SYM is the new symbol, seen in OBJECT.
457 // VERSION is the version of SYM. This will only be called if
458 // has_resolve() returns true.
459 virtual void
460 resolve(Symbol*, const elfcpp::Sym<size, big_endian>&, Object*,
461 const char*)
462 { gold_unreachable(); }
463
464 // Process the relocs for a section, and record information of the
465 // mapping from source to destination sections. This mapping is later
466 // used to determine unreferenced garbage sections. This procedure is
467 // only called during garbage collection.
468 virtual void
469 gc_process_relocs(Symbol_table* symtab,
470 Layout* layout,
471 Sized_relobj<size, big_endian>* object,
472 unsigned int data_shndx,
473 unsigned int sh_type,
474 const unsigned char* prelocs,
475 size_t reloc_count,
476 Output_section* output_section,
477 bool needs_special_offset_handling,
478 size_t local_symbol_count,
479 const unsigned char* plocal_symbols) = 0;
480
481 // Scan the relocs for a section, and record any information
482 // required for the symbol. SYMTAB is the symbol table. OBJECT is
483 // the object in which the section appears. DATA_SHNDX is the
484 // section index that these relocs apply to. SH_TYPE is the type of
485 // the relocation section, SHT_REL or SHT_RELA. PRELOCS points to
486 // the relocation data. RELOC_COUNT is the number of relocs.
487 // LOCAL_SYMBOL_COUNT is the number of local symbols.
488 // OUTPUT_SECTION is the output section.
489 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets to the output
490 // sections are not mapped as usual. PLOCAL_SYMBOLS points to the
491 // local symbol data from OBJECT. GLOBAL_SYMBOLS is the array of
492 // pointers to the global symbol table from OBJECT.
493 virtual void
494 scan_relocs(Symbol_table* symtab,
495 Layout* layout,
496 Sized_relobj<size, big_endian>* object,
497 unsigned int data_shndx,
498 unsigned int sh_type,
499 const unsigned char* prelocs,
500 size_t reloc_count,
501 Output_section* output_section,
502 bool needs_special_offset_handling,
503 size_t local_symbol_count,
504 const unsigned char* plocal_symbols) = 0;
505
506 // Relocate section data. SH_TYPE is the type of the relocation
507 // section, SHT_REL or SHT_RELA. PRELOCS points to the relocation
508 // information. RELOC_COUNT is the number of relocs.
509 // OUTPUT_SECTION is the output section.
510 // NEEDS_SPECIAL_OFFSET_HANDLING is true if offsets must be mapped
511 // to correspond to the output section. VIEW is a view into the
512 // output file holding the section contents, VIEW_ADDRESS is the
513 // virtual address of the view, and VIEW_SIZE is the size of the
514 // view. If NEEDS_SPECIAL_OFFSET_HANDLING is true, the VIEW_xx
515 // parameters refer to the complete output section data, not just
516 // the input section data.
517 virtual void
518 relocate_section(const Relocate_info<size, big_endian>*,
519 unsigned int sh_type,
520 const unsigned char* prelocs,
521 size_t reloc_count,
522 Output_section* output_section,
523 bool needs_special_offset_handling,
524 unsigned char* view,
525 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
526 section_size_type view_size,
527 const Reloc_symbol_changes*) = 0;
528
529 // Scan the relocs during a relocatable link. The parameters are
530 // like scan_relocs, with an additional Relocatable_relocs
531 // parameter, used to record the disposition of the relocs.
532 virtual void
533 scan_relocatable_relocs(Symbol_table* symtab,
534 Layout* layout,
535 Sized_relobj<size, big_endian>* object,
536 unsigned int data_shndx,
537 unsigned int sh_type,
538 const unsigned char* prelocs,
539 size_t reloc_count,
540 Output_section* output_section,
541 bool needs_special_offset_handling,
542 size_t local_symbol_count,
543 const unsigned char* plocal_symbols,
544 Relocatable_relocs*) = 0;
545
546 // Relocate a section during a relocatable link. The parameters are
547 // like relocate_section, with additional parameters for the view of
548 // the output reloc section.
549 virtual void
550 relocate_for_relocatable(const Relocate_info<size, big_endian>*,
551 unsigned int sh_type,
552 const unsigned char* prelocs,
553 size_t reloc_count,
554 Output_section* output_section,
555 off_t offset_in_output_section,
556 const Relocatable_relocs*,
557 unsigned char* view,
558 typename elfcpp::Elf_types<size>::Elf_Addr
559 view_address,
560 section_size_type view_size,
561 unsigned char* reloc_view,
562 section_size_type reloc_view_size) = 0;
563
564 protected:
565 Sized_target(const Target::Target_info* pti)
566 : Target(pti)
567 {
568 gold_assert(pti->size == size);
569 gold_assert(pti->is_big_endian ? big_endian : !big_endian);
570 }
571 };
572
573 } // End namespace gold.
574
575 #endif // !defined(GOLD_TARGET_H)
This page took 0.040959 seconds and 5 git commands to generate.