+// This is a strategy class we use when scanning for --emit-relocs.
+
+template<int sh_type>
+class Emit_relocs_strategy
+{
+ public:
+ // A local non-section symbol.
+ inline Relocatable_relocs::Reloc_strategy
+ local_non_section_strategy(unsigned int, Relobj*, unsigned int)
+ { return Relocatable_relocs::RELOC_COPY; }
+
+ // A local section symbol.
+ inline Relocatable_relocs::Reloc_strategy
+ local_section_strategy(unsigned int, Relobj*)
+ {
+ if (sh_type == elfcpp::SHT_RELA)
+ return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA;
+ else
+ {
+ // The addend is stored in the section contents. Since this
+ // is not a relocatable link, we are going to apply the
+ // relocation contents to the section as usual. This means
+ // that we have no way to record the original addend. If the
+ // original addend is not zero, there is basically no way for
+ // the user to handle this correctly. Caveat emptor.
+ return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0;
+ }
+ }
+
+ // A global symbol.
+ inline Relocatable_relocs::Reloc_strategy
+ global_strategy(unsigned int, Relobj*, unsigned int)
+ { return Relocatable_relocs::RELOC_COPY; }
+};
+
+// Scan the input relocations for --emit-relocs.
+
+template<int size, bool big_endian>
+void
+Sized_relobj<size, big_endian>::emit_relocs_scan(
+ Symbol_table* symtab,
+ Layout* layout,
+ const unsigned char* plocal_syms,
+ const Read_relocs_data::Relocs_list::iterator& p)
+{
+ Relocatable_relocs* rr = this->relocatable_relocs(p->reloc_shndx);
+ gold_assert(rr != NULL);
+ rr->set_reloc_count(p->reloc_count);
+
+ if (p->sh_type == elfcpp::SHT_REL)
+ this->emit_relocs_scan_reltype<elfcpp::SHT_REL>(symtab, layout,
+ plocal_syms, p, rr);
+ else
+ {
+ gold_assert(p->sh_type == elfcpp::SHT_RELA);
+ this->emit_relocs_scan_reltype<elfcpp::SHT_RELA>(symtab, layout,
+ plocal_syms, p, rr);
+ }
+}
+
+// Scan the input relocation for --emit-relocs, templatized on the
+// type of the relocation section.
+
+template<int size, bool big_endian>
+template<int sh_type>
+void
+Sized_relobj<size, big_endian>::emit_relocs_scan_reltype(
+ Symbol_table* symtab,
+ Layout* layout,
+ const unsigned char* plocal_syms,
+ const Read_relocs_data::Relocs_list::iterator& p,
+ Relocatable_relocs* rr)
+{
+ scan_relocatable_relocs<size, big_endian, sh_type,
+ Emit_relocs_strategy<sh_type> >(
+ symtab,
+ layout,
+ this,
+ p->data_shndx,
+ p->contents->data(),
+ p->reloc_count,
+ p->output_section,
+ p->needs_special_offset_handling,
+ this->local_symbol_count_,
+ plocal_syms,
+ rr);
+}
+
+// Scan the input relocations for --incremental.
+
+template<int size, bool big_endian>
+void
+Sized_relobj<size, big_endian>::incremental_relocs_scan(
+ const Read_relocs_data::Relocs_list::iterator& p)
+{
+ if (p->sh_type == elfcpp::SHT_REL)
+ this->incremental_relocs_scan_reltype<elfcpp::SHT_REL>(p);
+ else
+ {
+ gold_assert(p->sh_type == elfcpp::SHT_RELA);
+ this->incremental_relocs_scan_reltype<elfcpp::SHT_RELA>(p);
+ }
+}
+
+// Scan the input relocation for --emit-relocs, templatized on the
+// type of the relocation section.
+
+template<int size, bool big_endian>
+template<int sh_type>
+void
+Sized_relobj<size, big_endian>::incremental_relocs_scan_reltype(
+ const Read_relocs_data::Relocs_list::iterator& p)
+{
+ typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
+ const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
+ const unsigned char* prelocs = p->contents->data();
+ size_t reloc_count = p->reloc_count;
+
+ for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
+ {
+ Reltype reloc(prelocs);
+
+ if (p->needs_special_offset_handling
+ && !p->output_section->is_input_address_mapped(this, p->data_shndx,
+ reloc.get_r_offset()))
+ continue;
+
+ typename elfcpp::Elf_types<size>::Elf_WXword r_info =
+ reloc.get_r_info();
+ const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
+
+ if (r_sym >= this->local_symbol_count_)
+ this->count_incremental_reloc(r_sym - this->local_symbol_count_);
+ }
+}
+