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