1 // arm.cc -- arm target support for gold.
3 // Copyright 2009 Free Software Foundation, Inc.
4 // Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5 // by Ian Lance Taylor <iant@google.com>.
7 // This file is part of gold.
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 3 of the License, or
12 // (at your option) any later version.
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22 // MA 02110-1301, USA.
32 #include "parameters.h"
39 #include "copy-relocs.h"
41 #include "target-reloc.h"
42 #include "target-select.h"
51 // The arm target class.
53 // This is a very simple port of gold for ARM-EABI. It is intended for
54 // supporting Android only for the time being. Only these relocation types
74 // Coming soon (pending patches):
75 // - Support for dynamic symbols (GOT, PLT and etc).
79 // - Defining section symbols __exidx_start and __exidx_stop.
80 // - Support interworking.
81 // - Mergeing all .ARM.xxx.yyy sections into .ARM.xxx. Currently, they
82 // are incorrectly merged into an .ARM section.
85 // - Create a PT_ARM_EXIDX program header for a shared object that
86 // might throw an exception.
87 // - Support more relocation types as needed.
89 template<bool big_endian
>
90 class Target_arm
: public Sized_target
<32, big_endian
>
93 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, big_endian
>
97 : Sized_target
<32, big_endian
>(&arm_info
)
100 // Process the relocations to determine unreferenced sections for
101 // garbage collection.
103 gc_process_relocs(const General_options
& options
,
104 Symbol_table
* symtab
,
106 Sized_relobj
<32, big_endian
>* object
,
107 unsigned int data_shndx
,
108 unsigned int sh_type
,
109 const unsigned char* prelocs
,
111 Output_section
* output_section
,
112 bool needs_special_offset_handling
,
113 size_t local_symbol_count
,
114 const unsigned char* plocal_symbols
);
116 // Scan the relocations to look for symbol adjustments.
118 scan_relocs(const General_options
& options
,
119 Symbol_table
* symtab
,
121 Sized_relobj
<32, big_endian
>* object
,
122 unsigned int data_shndx
,
123 unsigned int sh_type
,
124 const unsigned char* prelocs
,
126 Output_section
* output_section
,
127 bool needs_special_offset_handling
,
128 size_t local_symbol_count
,
129 const unsigned char* plocal_symbols
);
131 // Finalize the sections.
133 do_finalize_sections(Layout
*);
135 // Return the value to use for a dynamic which requires special
138 do_dynsym_value(const Symbol
*) const;
140 // Relocate a section.
142 relocate_section(const Relocate_info
<32, big_endian
>*,
143 unsigned int sh_type
,
144 const unsigned char* prelocs
,
146 Output_section
* output_section
,
147 bool needs_special_offset_handling
,
149 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
150 section_size_type view_size
);
152 // Scan the relocs during a relocatable link.
154 scan_relocatable_relocs(const General_options
& options
,
155 Symbol_table
* symtab
,
157 Sized_relobj
<32, big_endian
>* object
,
158 unsigned int data_shndx
,
159 unsigned int sh_type
,
160 const unsigned char* prelocs
,
162 Output_section
* output_section
,
163 bool needs_special_offset_handling
,
164 size_t local_symbol_count
,
165 const unsigned char* plocal_symbols
,
166 Relocatable_relocs
*);
168 // Relocate a section during a relocatable link.
170 relocate_for_relocatable(const Relocate_info
<32, big_endian
>*,
171 unsigned int sh_type
,
172 const unsigned char* prelocs
,
174 Output_section
* output_section
,
175 off_t offset_in_output_section
,
176 const Relocatable_relocs
*,
178 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
179 section_size_type view_size
,
180 unsigned char* reloc_view
,
181 section_size_type reloc_view_size
);
183 // Return whether SYM is defined by the ABI.
185 do_is_defined_by_abi(Symbol
* sym
) const
186 { return strcmp(sym
->name(), "__tls_get_addr") == 0; }
188 // Map platform-specific reloc types
190 get_real_reloc_type (unsigned int r_type
);
193 // The class which scans relocations.
201 local(const General_options
& options
, Symbol_table
* symtab
,
202 Layout
* layout
, Target_arm
* target
,
203 Sized_relobj
<32, big_endian
>* object
,
204 unsigned int data_shndx
,
205 Output_section
* output_section
,
206 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
207 const elfcpp::Sym
<32, big_endian
>& lsym
);
210 global(const General_options
& options
, Symbol_table
* symtab
,
211 Layout
* layout
, Target_arm
* target
,
212 Sized_relobj
<32, big_endian
>* object
,
213 unsigned int data_shndx
,
214 Output_section
* output_section
,
215 const elfcpp::Rel
<32, big_endian
>& reloc
, unsigned int r_type
,
220 unsupported_reloc_local(Sized_relobj
<32, big_endian
>*,
221 unsigned int r_type
);
224 unsupported_reloc_global(Sized_relobj
<32, big_endian
>*,
225 unsigned int r_type
, Symbol
*);
228 // The class which implements relocation.
238 // Do a relocation. Return false if the caller should not issue
239 // any warnings about this relocation.
241 relocate(const Relocate_info
<32, big_endian
>*, Target_arm
*,
242 Output_section
*, size_t relnum
,
243 const elfcpp::Rel
<32, big_endian
>&,
244 unsigned int r_type
, const Sized_symbol
<32>*,
245 const Symbol_value
<32>*,
246 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
250 // A class which returns the size required for a relocation type,
251 // used while scanning relocs during a relocatable link.
252 class Relocatable_size_for_reloc
256 get_size_for_reloc(unsigned int, Relobj
*);
259 // Information about this specific target which we pass to the
260 // general Target structure.
261 static const Target::Target_info arm_info
;
264 template<bool big_endian
>
265 const Target::Target_info Target_arm
<big_endian
>::arm_info
=
268 big_endian
, // is_big_endian
269 elfcpp::EM_ARM
, // machine_code
270 false, // has_make_symbol
271 false, // has_resolve
272 false, // has_code_fill
273 true, // is_default_stack_executable
275 "/usr/lib/libc.so.1", // dynamic_linker
276 0x8000, // default_text_segment_address
277 0x1000, // abi_pagesize (overridable by -z max-page-size)
278 0x1000 // common_pagesize (overridable by -z common-page-size)
281 // Report an unsupported relocation against a local symbol.
283 template<bool big_endian
>
285 Target_arm
<big_endian
>::Scan::unsupported_reloc_local(
286 Sized_relobj
<32, big_endian
>* object
,
289 gold_error(_("%s: unsupported reloc %u against local symbol"),
290 object
->name().c_str(), r_type
);
293 // Scan a relocation for a local symbol.
295 template<bool big_endian
>
297 Target_arm
<big_endian
>::Scan::local(const General_options
&,
298 Symbol_table
* /* symtab */,
299 Layout
* /* layout */,
300 Target_arm
* /* target */,
301 Sized_relobj
<32, big_endian
>* object
,
302 unsigned int /* data_shndx */,
303 Output_section
* /* output_section */,
304 const elfcpp::Rel
<32, big_endian
>& /* reloc */,
306 const elfcpp::Sym
<32, big_endian
>&)
308 r_type
= get_real_reloc_type(r_type
);
311 case elfcpp::R_ARM_NONE
:
315 unsupported_reloc_local(object
, r_type
);
320 // Report an unsupported relocation against a global symbol.
322 template<bool big_endian
>
324 Target_arm
<big_endian
>::Scan::unsupported_reloc_global(
325 Sized_relobj
<32, big_endian
>* object
,
329 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
330 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
333 // Scan a relocation for a global symbol.
335 template<bool big_endian
>
337 Target_arm
<big_endian
>::Scan::global(const General_options
&,
338 Symbol_table
* /* symtab */,
339 Layout
* /* layout */,
340 Target_arm
* /* target */,
341 Sized_relobj
<32, big_endian
>* object
,
342 unsigned int /* data_shndx */,
343 Output_section
* /* output_section */,
344 const elfcpp::Rel
<32, big_endian
>& /* reloc */,
348 r_type
= get_real_reloc_type(r_type
);
351 case elfcpp::R_ARM_NONE
:
355 unsupported_reloc_global(object
, r_type
, gsym
);
360 // Process relocations for gc.
362 template<bool big_endian
>
364 Target_arm
<big_endian
>::gc_process_relocs(const General_options
& options
,
365 Symbol_table
* symtab
,
367 Sized_relobj
<32, big_endian
>* object
,
368 unsigned int data_shndx
,
370 const unsigned char* prelocs
,
372 Output_section
* output_section
,
373 bool needs_special_offset_handling
,
374 size_t local_symbol_count
,
375 const unsigned char* plocal_symbols
)
377 typedef Target_arm
<big_endian
> Arm
;
378 typedef typename Target_arm
<big_endian
>::Scan Scan
;
380 gold::gc_process_relocs
<32, big_endian
, Arm
, elfcpp::SHT_REL
, Scan
>(
390 needs_special_offset_handling
,
395 // Scan relocations for a section.
397 template<bool big_endian
>
399 Target_arm
<big_endian
>::scan_relocs(const General_options
& options
,
400 Symbol_table
* symtab
,
402 Sized_relobj
<32, big_endian
>* object
,
403 unsigned int data_shndx
,
404 unsigned int sh_type
,
405 const unsigned char* prelocs
,
407 Output_section
* output_section
,
408 bool needs_special_offset_handling
,
409 size_t local_symbol_count
,
410 const unsigned char* plocal_symbols
)
412 typedef typename Target_arm
<big_endian
>::Scan Scan
;
413 if (sh_type
== elfcpp::SHT_RELA
)
415 gold_error(_("%s: unsupported RELA reloc section"),
416 object
->name().c_str());
420 gold::scan_relocs
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
, Scan
>(
430 needs_special_offset_handling
,
435 // Finalize the sections.
437 template<bool big_endian
>
439 Target_arm
<big_endian
>::do_finalize_sections(Layout
* /* layout */)
444 // Perform a relocation.
446 template<bool big_endian
>
448 Target_arm
<big_endian
>::Relocate::relocate(
449 const Relocate_info
<32, big_endian
>* /* relinfo */,
450 Target_arm
* /* target */,
451 Output_section
* /* output_section */,
453 const elfcpp::Rel
<32, big_endian
>& /* rel */,
455 const Sized_symbol
<32>* /* gsym */,
456 const Symbol_value
<32>* /* psymval */,
457 unsigned char* /* view */,
458 elfcpp::Elf_types
<32>::Elf_Addr
/* address */,
459 section_size_type
/* view_size */ )
463 case elfcpp::R_ARM_NONE
:
473 // Relocate section data.
475 template<bool big_endian
>
477 Target_arm
<big_endian
>::relocate_section(
478 const Relocate_info
<32, big_endian
>* relinfo
,
479 unsigned int sh_type
,
480 const unsigned char* prelocs
,
482 Output_section
* output_section
,
483 bool needs_special_offset_handling
,
485 elfcpp::Elf_types
<32>::Elf_Addr address
,
486 section_size_type view_size
)
488 typedef typename Target_arm
<big_endian
>::Relocate Arm_relocate
;
489 gold_assert(sh_type
== elfcpp::SHT_REL
);
491 gold::relocate_section
<32, big_endian
, Target_arm
, elfcpp::SHT_REL
,
498 needs_special_offset_handling
,
504 // Return the size of a relocation while scanning during a relocatable
507 template<bool big_endian
>
509 Target_arm
<big_endian
>::Relocatable_size_for_reloc::get_size_for_reloc(
513 r_type
= get_real_reloc_type(r_type
);
516 case elfcpp::R_ARM_NONE
:
519 case elfcpp::R_ARM_ABS32
:
520 case elfcpp::R_ARM_REL32
:
521 case elfcpp::R_ARM_THM_CALL
:
522 case elfcpp::R_ARM_GOTOFF32
:
523 case elfcpp::R_ARM_BASE_PREL
:
524 case elfcpp::R_ARM_GOT_BREL
:
525 case elfcpp::R_ARM_PLT32
:
526 case elfcpp::R_ARM_CALL
:
527 case elfcpp::R_ARM_JUMP24
:
528 case elfcpp::R_ARM_PREL31
:
531 case elfcpp::R_ARM_TARGET1
:
532 // This should have been mapped to another type already.
534 case elfcpp::R_ARM_COPY
:
535 case elfcpp::R_ARM_GLOB_DAT
:
536 case elfcpp::R_ARM_JUMP_SLOT
:
537 case elfcpp::R_ARM_RELATIVE
:
538 // These are relocations which should only be seen by the
539 // dynamic linker, and should never be seen here.
540 gold_error(_("%s: unexpected reloc %u in object file"),
541 object
->name().c_str(), r_type
);
545 object
->error(_("unsupported reloc %u in object file"), r_type
);
550 // Scan the relocs during a relocatable link.
552 template<bool big_endian
>
554 Target_arm
<big_endian
>::scan_relocatable_relocs(
555 const General_options
& options
,
556 Symbol_table
* symtab
,
558 Sized_relobj
<32, big_endian
>* object
,
559 unsigned int data_shndx
,
560 unsigned int sh_type
,
561 const unsigned char* prelocs
,
563 Output_section
* output_section
,
564 bool needs_special_offset_handling
,
565 size_t local_symbol_count
,
566 const unsigned char* plocal_symbols
,
567 Relocatable_relocs
* rr
)
569 gold_assert(sh_type
== elfcpp::SHT_REL
);
571 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
572 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
574 gold::scan_relocatable_relocs
<32, big_endian
, elfcpp::SHT_REL
,
575 Scan_relocatable_relocs
>(
584 needs_special_offset_handling
,
590 // Relocate a section during a relocatable link.
592 template<bool big_endian
>
594 Target_arm
<big_endian
>::relocate_for_relocatable(
595 const Relocate_info
<32, big_endian
>* relinfo
,
596 unsigned int sh_type
,
597 const unsigned char* prelocs
,
599 Output_section
* output_section
,
600 off_t offset_in_output_section
,
601 const Relocatable_relocs
* rr
,
603 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
604 section_size_type view_size
,
605 unsigned char* reloc_view
,
606 section_size_type reloc_view_size
)
608 gold_assert(sh_type
== elfcpp::SHT_REL
);
610 gold::relocate_for_relocatable
<32, big_endian
, elfcpp::SHT_REL
>(
615 offset_in_output_section
,
624 template<bool big_endian
>
626 Target_arm
<big_endian
>::do_dynsym_value(const Symbol
* /*gsym*/) const
632 // Map platform-specific relocs to real relocs
634 template<bool big_endian
>
636 Target_arm
<big_endian
>::get_real_reloc_type (unsigned int r_type
)
640 case elfcpp::R_ARM_TARGET1
:
641 // This is either R_ARM_ABS32 or R_ARM_REL32;
642 return elfcpp::R_ARM_ABS32
;
644 case elfcpp::R_ARM_TARGET2
:
645 // This can be any reloc type but ususally is R_ARM_GOT_PREL
646 return elfcpp::R_ARM_GOT_PREL
;
653 // The selector for arm object files.
655 template<bool big_endian
>
656 class Target_selector_arm
: public Target_selector
659 Target_selector_arm()
660 : Target_selector(elfcpp::EM_ARM
, 32, big_endian
,
661 (big_endian
? "elf32-bigarm" : "elf32-littlearm"))
665 do_instantiate_target()
666 { return new Target_arm
<big_endian
>(); }
669 Target_selector_arm
<false> target_selector_arm
;
670 Target_selector_arm
<true> target_selector_armbe
;
672 } // End anonymous namespace.