1 // target-reloc.h -- target specific relocation support -*- C++ -*-
3 #ifndef GOLD_TARGET_RELOC_H
4 #define GOLD_TARGET_RELOC_H
13 // Pick the ELF relocation accessor class and the size based on
14 // SH_TYPE, which is either SHT_REL or SHT_RELA.
16 template<int sh_type, int size, bool big_endian>
19 template<int size, bool big_endian>
20 struct Reloc_types<elfcpp::SHT_REL, size, big_endian>
22 typedef typename elfcpp::Rel<size, big_endian> Reloc;
23 static const int reloc_size = elfcpp::Elf_sizes<size>::rel_size;
26 template<int size, bool big_endian>
27 struct Reloc_types<elfcpp::SHT_RELA, size, big_endian>
29 typedef typename elfcpp::Rela<size, big_endian> Reloc;
30 static const int reloc_size = elfcpp::Elf_sizes<size>::rela_size;
33 // This function implements the generic part of reloc scanning. This
34 // is an inline function which takes a class whose operator()
35 // implements the machine specific part of scanning. We do it this
36 // way to avoidmaking a function call for each relocation, and to
37 // avoid repeating the generic code for each target.
39 template<int size, bool big_endian, int sh_type, typename Scan>
42 const General_options& options,
44 Sized_object<size, big_endian>* object,
45 const unsigned char* prelocs,
48 const unsigned char* plocal_syms,
51 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
52 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
53 const int sym_size = elfcpp::Elf_sizes<size>::sym_size;
56 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
58 Reltype reloc(prelocs);
60 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
61 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
62 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
64 if (r_sym < local_count)
66 assert(plocal_syms != NULL);
67 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms
69 const unsigned int shndx = lsym.get_st_shndx();
70 if (shndx < elfcpp::SHN_LORESERVE
71 && !object->is_section_included(lsym.get_st_shndx()))
73 // RELOC is a relocation against a local symbol in a
74 // section we are discarding. We can ignore this
75 // relocation. It will eventually become a reloc
76 // against the value zero.
78 // FIXME: We should issue a warning if this is an
79 // allocated section; is this the best place to do it?
81 // FIXME: The old GNU linker would in some cases look
82 // for the linkonce section which caused this section to
83 // be discarded, and, if the other section was the same
84 // size, change the reloc to refer to the other section.
85 // That seems risky and weird to me, and I don't know of
86 // any case where it is actually required.
91 scan.local(options, object, reloc, r_type, lsym);
95 Symbol* gsym = global_syms[r_sym - local_count];
97 if (gsym->is_forwarder())
98 gsym = symtab->resolve_forwards(gsym);
100 scan.global(options, object, reloc, r_type, gsym);
105 // This function implements the generic part of relocation processing.
106 // This is an inline function which take a class whose operator()
107 // implements the machine specific part of relocation. We do it this
108 // way to avoid making a function call for each relocation, and to
109 // avoid repeating the generic relocation handling code for each
112 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of
113 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA.
114 // RELOCATE implements operator() to do a relocation.
116 // PRELOCS points to the relocation data. RELOC_COUNT is the number
117 // of relocs. VIEW is the section data, VIEW_ADDRESS is its memory
118 // address, and VIEW_SIZE is the size.
120 template<int size, bool big_endian, int sh_type, typename Relocate>
123 const Relocate_info<size, big_endian>* relinfo,
124 const unsigned char* prelocs,
127 typename elfcpp::Elf_types<size>::Elf_Addr view_address,
130 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype;
131 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size;
134 unsigned int local_count = relinfo->local_symbol_count;
135 typename elfcpp::Elf_types<size>::Elf_Addr *local_values = relinfo->values;
136 Symbol** global_syms = relinfo->symbols;
138 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size)
140 Reltype reloc(prelocs);
142 off_t offset = reloc.get_r_offset();
143 if (offset < 0 || offset >= view_size)
145 fprintf(stderr, _("%s: %s: reloc has bad offset %zu\n"),
146 program_name, relinfo->location(i, offset).c_str(),
147 static_cast<size_t>(offset));
151 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info();
152 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info);
153 unsigned int r_type = elfcpp::elf_r_type<size>(r_info);
155 Sized_symbol<size>* sym;
156 typename elfcpp::Elf_types<size>::Elf_Addr value;
158 if (r_sym < local_count)
161 value = local_values[r_sym];
165 Symbol* gsym = global_syms[r_sym - local_count];
166 assert(gsym != NULL);
167 if (gsym->is_forwarder())
168 gsym = relinfo->symtab->resolve_forwards(gsym);
170 sym = static_cast<Sized_symbol<size>*>(gsym);
171 value = sym->value();
173 if (sym->shnum() == elfcpp::SHN_UNDEF
174 && sym->binding() != elfcpp::STB_WEAK)
176 fprintf(stderr, _("%s: %s: undefined reference to '%s'\n"),
177 program_name, relinfo->location(i, offset).c_str(),
183 relocate.relocate(relinfo, i, reloc, r_type, sym, value, view + offset,
184 view_address + offset, view_size);
188 } // End namespace gold.
190 #endif // !defined(GOLD_TARGET_RELOC_H)