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