| 1 | // target-reloc.h -- target specific relocation support -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2006-2018 Free Software Foundation, Inc. |
| 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 | |
| 23 | #ifndef GOLD_TARGET_RELOC_H |
| 24 | #define GOLD_TARGET_RELOC_H |
| 25 | |
| 26 | #include "elfcpp.h" |
| 27 | #include "symtab.h" |
| 28 | #include "object.h" |
| 29 | #include "reloc.h" |
| 30 | #include "reloc-types.h" |
| 31 | |
| 32 | namespace gold |
| 33 | { |
| 34 | |
| 35 | // This function implements the generic part of reloc scanning. The |
| 36 | // template parameter Scan must be a class type which provides two |
| 37 | // functions: local() and global(). Those functions implement the |
| 38 | // machine specific part of scanning. We do it this way to |
| 39 | // avoid making a function call for each relocation, and to avoid |
| 40 | // repeating the generic code for each target. |
| 41 | |
| 42 | template<int size, bool big_endian, typename Target_type, |
| 43 | typename Scan, typename Classify_reloc> |
| 44 | inline void |
| 45 | scan_relocs( |
| 46 | Symbol_table* symtab, |
| 47 | Layout* layout, |
| 48 | Target_type* target, |
| 49 | Sized_relobj_file<size, big_endian>* object, |
| 50 | unsigned int data_shndx, |
| 51 | const unsigned char* prelocs, |
| 52 | size_t reloc_count, |
| 53 | Output_section* output_section, |
| 54 | bool needs_special_offset_handling, |
| 55 | size_t local_count, |
| 56 | const unsigned char* plocal_syms) |
| 57 | { |
| 58 | typedef typename Classify_reloc::Reltype Reltype; |
| 59 | const int reloc_size = Classify_reloc::reloc_size; |
| 60 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 61 | Scan scan; |
| 62 | |
| 63 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
| 64 | { |
| 65 | Reltype reloc(prelocs); |
| 66 | |
| 67 | if (needs_special_offset_handling |
| 68 | && !output_section->is_input_address_mapped(object, data_shndx, |
| 69 | reloc.get_r_offset())) |
| 70 | continue; |
| 71 | |
| 72 | unsigned int r_sym = Classify_reloc::get_r_sym(&reloc); |
| 73 | unsigned int r_type = Classify_reloc::get_r_type(&reloc); |
| 74 | |
| 75 | if (r_sym < local_count) |
| 76 | { |
| 77 | gold_assert(plocal_syms != NULL); |
| 78 | typename elfcpp::Sym<size, big_endian> lsym(plocal_syms |
| 79 | + r_sym * sym_size); |
| 80 | unsigned int shndx = lsym.get_st_shndx(); |
| 81 | bool is_ordinary; |
| 82 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); |
| 83 | // If RELOC is a relocation against a local symbol in a |
| 84 | // section we are discarding then we can ignore it. It will |
| 85 | // eventually become a reloc against the value zero. |
| 86 | // |
| 87 | // FIXME: We should issue a warning if this is an |
| 88 | // allocated section; is this the best place to do it? |
| 89 | // |
| 90 | // FIXME: The old GNU linker would in some cases look |
| 91 | // for the linkonce section which caused this section to |
| 92 | // be discarded, and, if the other section was the same |
| 93 | // size, change the reloc to refer to the other section. |
| 94 | // That seems risky and weird to me, and I don't know of |
| 95 | // any case where it is actually required. |
| 96 | bool is_discarded = (is_ordinary |
| 97 | && shndx != elfcpp::SHN_UNDEF |
| 98 | && !object->is_section_included(shndx) |
| 99 | && !symtab->is_section_folded(object, shndx)); |
| 100 | scan.local(symtab, layout, target, object, data_shndx, |
| 101 | output_section, reloc, r_type, lsym, is_discarded); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | Symbol* gsym = object->global_symbol(r_sym); |
| 106 | gold_assert(gsym != NULL); |
| 107 | if (gsym->is_forwarder()) |
| 108 | gsym = symtab->resolve_forwards(gsym); |
| 109 | |
| 110 | scan.global(symtab, layout, target, object, data_shndx, |
| 111 | output_section, reloc, r_type, gsym); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Behavior for relocations to discarded comdat sections. |
| 117 | |
| 118 | enum Comdat_behavior |
| 119 | { |
| 120 | CB_UNDETERMINED, // Not yet determined -- need to look at section name. |
| 121 | CB_PRETEND, // Attempt to map to the corresponding kept section. |
| 122 | CB_IGNORE, // Ignore the relocation. |
| 123 | CB_WARNING // Print a warning. |
| 124 | }; |
| 125 | |
| 126 | class Default_comdat_behavior |
| 127 | { |
| 128 | public: |
| 129 | // Decide what the linker should do for relocations that refer to |
| 130 | // discarded comdat sections. This decision is based on the name of |
| 131 | // the section being relocated. |
| 132 | |
| 133 | inline Comdat_behavior |
| 134 | get(const char* name) |
| 135 | { |
| 136 | if (Layout::is_debug_info_section(name)) |
| 137 | return CB_PRETEND; |
| 138 | if (strcmp(name, ".eh_frame") == 0 |
| 139 | || strcmp(name, ".gcc_except_table") == 0) |
| 140 | return CB_IGNORE; |
| 141 | return CB_WARNING; |
| 142 | } |
| 143 | }; |
| 144 | |
| 145 | // Give an error for a symbol with non-default visibility which is not |
| 146 | // defined locally. |
| 147 | |
| 148 | inline void |
| 149 | visibility_error(const Symbol* sym) |
| 150 | { |
| 151 | const char* v; |
| 152 | switch (sym->visibility()) |
| 153 | { |
| 154 | case elfcpp::STV_INTERNAL: |
| 155 | v = _("internal"); |
| 156 | break; |
| 157 | case elfcpp::STV_HIDDEN: |
| 158 | v = _("hidden"); |
| 159 | break; |
| 160 | case elfcpp::STV_PROTECTED: |
| 161 | v = _("protected"); |
| 162 | break; |
| 163 | default: |
| 164 | gold_unreachable(); |
| 165 | } |
| 166 | gold_error(_("%s symbol '%s' is not defined locally"), |
| 167 | v, sym->name()); |
| 168 | } |
| 169 | |
| 170 | // Return true if we are should issue an error saying that SYM is an |
| 171 | // undefined symbol. This is called if there is a relocation against |
| 172 | // SYM. |
| 173 | |
| 174 | inline bool |
| 175 | issue_undefined_symbol_error(const Symbol* sym) |
| 176 | { |
| 177 | // We only report global symbols. |
| 178 | if (sym == NULL) |
| 179 | return false; |
| 180 | |
| 181 | // We only report undefined symbols. |
| 182 | if (!sym->is_undefined() && !sym->is_placeholder()) |
| 183 | return false; |
| 184 | |
| 185 | // We don't report weak symbols. |
| 186 | if (sym->is_weak_undefined()) |
| 187 | return false; |
| 188 | |
| 189 | // We don't report symbols defined in discarded sections, |
| 190 | // unless they're placeholder symbols that should have been |
| 191 | // provided by a plugin. |
| 192 | if (sym->is_defined_in_discarded_section() && !sym->is_placeholder()) |
| 193 | return false; |
| 194 | |
| 195 | // If the target defines this symbol, don't report it here. |
| 196 | if (parameters->target().is_defined_by_abi(sym)) |
| 197 | return false; |
| 198 | |
| 199 | // See if we've been told to ignore whether this symbol is |
| 200 | // undefined. |
| 201 | const char* const u = parameters->options().unresolved_symbols(); |
| 202 | if (u != NULL) |
| 203 | { |
| 204 | if (strcmp(u, "ignore-all") == 0) |
| 205 | return false; |
| 206 | if (strcmp(u, "report-all") == 0) |
| 207 | return true; |
| 208 | if (strcmp(u, "ignore-in-object-files") == 0 && !sym->in_dyn()) |
| 209 | return false; |
| 210 | if (strcmp(u, "ignore-in-shared-libs") == 0 && !sym->in_reg()) |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | // If the symbol is hidden, report it. |
| 215 | if (sym->visibility() == elfcpp::STV_HIDDEN) |
| 216 | return true; |
| 217 | |
| 218 | // When creating a shared library, only report unresolved symbols if |
| 219 | // -z defs was used. |
| 220 | if (parameters->options().shared() && !parameters->options().defs()) |
| 221 | return false; |
| 222 | |
| 223 | // Otherwise issue a warning. |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | // This function implements the generic part of relocation processing. |
| 228 | // The template parameter Relocate must be a class type which provides |
| 229 | // a single function, relocate(), which implements the machine |
| 230 | // specific part of a relocation. |
| 231 | |
| 232 | // The template parameter Relocate_comdat_behavior is a class type |
| 233 | // which provides a single function, get(), which determines what the |
| 234 | // linker should do for relocations that refer to discarded comdat |
| 235 | // sections. |
| 236 | |
| 237 | // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of |
| 238 | // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA. |
| 239 | // RELOCATE implements operator() to do a relocation. |
| 240 | |
| 241 | // PRELOCS points to the relocation data. RELOC_COUNT is the number |
| 242 | // of relocs. OUTPUT_SECTION is the output section. |
| 243 | // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be |
| 244 | // mapped to output offsets. |
| 245 | |
| 246 | // VIEW is the section data, VIEW_ADDRESS is its memory address, and |
| 247 | // VIEW_SIZE is the size. These refer to the input section, unless |
| 248 | // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to |
| 249 | // the output section. |
| 250 | |
| 251 | // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is |
| 252 | // not NULL, it is a vector indexed by relocation index. If that |
| 253 | // entry is not NULL, it points to a global symbol which used as the |
| 254 | // symbol for the relocation, ignoring the symbol index in the |
| 255 | // relocation. |
| 256 | |
| 257 | template<int size, bool big_endian, typename Target_type, |
| 258 | typename Relocate, |
| 259 | typename Relocate_comdat_behavior, |
| 260 | typename Classify_reloc> |
| 261 | inline void |
| 262 | relocate_section( |
| 263 | const Relocate_info<size, big_endian>* relinfo, |
| 264 | Target_type* target, |
| 265 | const unsigned char* prelocs, |
| 266 | size_t reloc_count, |
| 267 | Output_section* output_section, |
| 268 | bool needs_special_offset_handling, |
| 269 | unsigned char* view, |
| 270 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
| 271 | section_size_type view_size, |
| 272 | const Reloc_symbol_changes* reloc_symbol_changes) |
| 273 | { |
| 274 | typedef typename Classify_reloc::Reltype Reltype; |
| 275 | const int reloc_size = Classify_reloc::reloc_size; |
| 276 | Relocate relocate; |
| 277 | Relocate_comdat_behavior relocate_comdat_behavior; |
| 278 | |
| 279 | Sized_relobj_file<size, big_endian>* object = relinfo->object; |
| 280 | unsigned int local_count = object->local_symbol_count(); |
| 281 | |
| 282 | Comdat_behavior comdat_behavior = CB_UNDETERMINED; |
| 283 | |
| 284 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
| 285 | { |
| 286 | Reltype reloc(prelocs); |
| 287 | |
| 288 | section_offset_type offset = |
| 289 | convert_to_section_size_type(reloc.get_r_offset()); |
| 290 | |
| 291 | if (needs_special_offset_handling) |
| 292 | { |
| 293 | offset = output_section->output_offset(relinfo->object, |
| 294 | relinfo->data_shndx, |
| 295 | offset); |
| 296 | if (offset == -1) |
| 297 | continue; |
| 298 | } |
| 299 | |
| 300 | unsigned int r_sym = Classify_reloc::get_r_sym(&reloc); |
| 301 | |
| 302 | const Sized_symbol<size>* sym; |
| 303 | |
| 304 | Symbol_value<size> symval; |
| 305 | const Symbol_value<size> *psymval; |
| 306 | bool is_defined_in_discarded_section; |
| 307 | unsigned int shndx; |
| 308 | if (r_sym < local_count |
| 309 | && (reloc_symbol_changes == NULL |
| 310 | || (*reloc_symbol_changes)[i] == NULL)) |
| 311 | { |
| 312 | sym = NULL; |
| 313 | psymval = object->local_symbol(r_sym); |
| 314 | |
| 315 | // If the local symbol belongs to a section we are discarding, |
| 316 | // and that section is a debug section, try to find the |
| 317 | // corresponding kept section and map this symbol to its |
| 318 | // counterpart in the kept section. The symbol must not |
| 319 | // correspond to a section we are folding. |
| 320 | bool is_ordinary; |
| 321 | shndx = psymval->input_shndx(&is_ordinary); |
| 322 | is_defined_in_discarded_section = |
| 323 | (is_ordinary |
| 324 | && shndx != elfcpp::SHN_UNDEF |
| 325 | && !object->is_section_included(shndx) |
| 326 | && !relinfo->symtab->is_section_folded(object, shndx)); |
| 327 | } |
| 328 | else |
| 329 | { |
| 330 | const Symbol* gsym; |
| 331 | if (reloc_symbol_changes != NULL |
| 332 | && (*reloc_symbol_changes)[i] != NULL) |
| 333 | gsym = (*reloc_symbol_changes)[i]; |
| 334 | else |
| 335 | { |
| 336 | gsym = object->global_symbol(r_sym); |
| 337 | gold_assert(gsym != NULL); |
| 338 | if (gsym->is_forwarder()) |
| 339 | gsym = relinfo->symtab->resolve_forwards(gsym); |
| 340 | } |
| 341 | |
| 342 | sym = static_cast<const Sized_symbol<size>*>(gsym); |
| 343 | if (sym->has_symtab_index() && sym->symtab_index() != -1U) |
| 344 | symval.set_output_symtab_index(sym->symtab_index()); |
| 345 | else |
| 346 | symval.set_no_output_symtab_entry(); |
| 347 | symval.set_output_value(sym->value()); |
| 348 | if (gsym->type() == elfcpp::STT_TLS) |
| 349 | symval.set_is_tls_symbol(); |
| 350 | else if (gsym->type() == elfcpp::STT_GNU_IFUNC) |
| 351 | symval.set_is_ifunc_symbol(); |
| 352 | psymval = &symval; |
| 353 | |
| 354 | is_defined_in_discarded_section = |
| 355 | (gsym->is_defined_in_discarded_section() |
| 356 | && gsym->is_undefined()); |
| 357 | shndx = 0; |
| 358 | } |
| 359 | |
| 360 | Symbol_value<size> symval2; |
| 361 | if (is_defined_in_discarded_section) |
| 362 | { |
| 363 | if (comdat_behavior == CB_UNDETERMINED) |
| 364 | { |
| 365 | std::string name = object->section_name(relinfo->data_shndx); |
| 366 | comdat_behavior = relocate_comdat_behavior.get(name.c_str()); |
| 367 | } |
| 368 | if (comdat_behavior == CB_PRETEND) |
| 369 | { |
| 370 | // FIXME: This case does not work for global symbols. |
| 371 | // We have no place to store the original section index. |
| 372 | // Fortunately this does not matter for comdat sections, |
| 373 | // only for sections explicitly discarded by a linker |
| 374 | // script. |
| 375 | bool found; |
| 376 | typename elfcpp::Elf_types<size>::Elf_Addr value = |
| 377 | object->map_to_kept_section(shndx, &found); |
| 378 | if (found) |
| 379 | symval2.set_output_value(value + psymval->input_value()); |
| 380 | else |
| 381 | symval2.set_output_value(0); |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | if (comdat_behavior == CB_WARNING) |
| 386 | gold_warning_at_location(relinfo, i, offset, |
| 387 | _("relocation refers to discarded " |
| 388 | "section")); |
| 389 | symval2.set_output_value(0); |
| 390 | } |
| 391 | symval2.set_no_output_symtab_entry(); |
| 392 | psymval = &symval2; |
| 393 | } |
| 394 | |
| 395 | // If OFFSET is out of range, still let the target decide to |
| 396 | // ignore the relocation. Pass in NULL as the VIEW argument so |
| 397 | // that it can return quickly without trashing an invalid memory |
| 398 | // address. |
| 399 | unsigned char *v = view + offset; |
| 400 | if (offset < 0 || static_cast<section_size_type>(offset) >= view_size) |
| 401 | v = NULL; |
| 402 | |
| 403 | if (!relocate.relocate(relinfo, Classify_reloc::sh_type, target, |
| 404 | output_section, i, prelocs, sym, psymval, |
| 405 | v, view_address + offset, view_size)) |
| 406 | continue; |
| 407 | |
| 408 | if (v == NULL) |
| 409 | { |
| 410 | gold_error_at_location(relinfo, i, offset, |
| 411 | _("reloc has bad offset %zu"), |
| 412 | static_cast<size_t>(offset)); |
| 413 | continue; |
| 414 | } |
| 415 | |
| 416 | if (issue_undefined_symbol_error(sym)) |
| 417 | gold_undefined_symbol_at_location(sym, relinfo, i, offset); |
| 418 | else if (sym != NULL |
| 419 | && sym->visibility() != elfcpp::STV_DEFAULT |
| 420 | && (sym->is_strong_undefined() || sym->is_from_dynobj())) |
| 421 | visibility_error(sym); |
| 422 | |
| 423 | if (sym != NULL && sym->has_warning()) |
| 424 | relinfo->symtab->issue_warning(sym, relinfo, i, offset); |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // Apply an incremental relocation. |
| 429 | |
| 430 | template<int size, bool big_endian, typename Target_type, |
| 431 | typename Relocate> |
| 432 | void |
| 433 | apply_relocation(const Relocate_info<size, big_endian>* relinfo, |
| 434 | Target_type* target, |
| 435 | typename elfcpp::Elf_types<size>::Elf_Addr r_offset, |
| 436 | unsigned int r_type, |
| 437 | typename elfcpp::Elf_types<size>::Elf_Swxword r_addend, |
| 438 | const Symbol* gsym, |
| 439 | unsigned char* view, |
| 440 | typename elfcpp::Elf_types<size>::Elf_Addr address, |
| 441 | section_size_type view_size) |
| 442 | { |
| 443 | // Construct the ELF relocation in a temporary buffer. |
| 444 | const int reloc_size = elfcpp::Elf_sizes<size>::rela_size; |
| 445 | unsigned char relbuf[reloc_size]; |
| 446 | elfcpp::Rela_write<size, big_endian> orel(relbuf); |
| 447 | orel.put_r_offset(r_offset); |
| 448 | orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type)); |
| 449 | orel.put_r_addend(r_addend); |
| 450 | |
| 451 | // Setup a Symbol_value for the global symbol. |
| 452 | const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym); |
| 453 | Symbol_value<size> symval; |
| 454 | gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U); |
| 455 | symval.set_output_symtab_index(sym->symtab_index()); |
| 456 | symval.set_output_value(sym->value()); |
| 457 | if (gsym->type() == elfcpp::STT_TLS) |
| 458 | symval.set_is_tls_symbol(); |
| 459 | else if (gsym->type() == elfcpp::STT_GNU_IFUNC) |
| 460 | symval.set_is_ifunc_symbol(); |
| 461 | |
| 462 | Relocate relocate; |
| 463 | relocate.relocate(relinfo, elfcpp::SHT_RELA, target, NULL, |
| 464 | -1U, relbuf, sym, &symval, |
| 465 | view + r_offset, address + r_offset, view_size); |
| 466 | } |
| 467 | |
| 468 | // A class for inquiring about properties of a relocation, |
| 469 | // used while scanning relocs during a relocatable link and |
| 470 | // garbage collection. This class may be used as the default |
| 471 | // for SHT_RELA targets, but SHT_REL targets must implement |
| 472 | // a derived class that overrides get_size_for_reloc. |
| 473 | // The MIPS-64 target also needs to override the methods |
| 474 | // for accessing the r_sym and r_type fields of a relocation, |
| 475 | // due to its non-standard use of the r_info field. |
| 476 | |
| 477 | template<int sh_type_, int size, bool big_endian> |
| 478 | class Default_classify_reloc |
| 479 | { |
| 480 | public: |
| 481 | typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc |
| 482 | Reltype; |
| 483 | typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc_write |
| 484 | Reltype_write; |
| 485 | static const int reloc_size = |
| 486 | Reloc_types<sh_type_, size, big_endian>::reloc_size; |
| 487 | static const int sh_type = sh_type_; |
| 488 | |
| 489 | // Return the symbol referred to by the relocation. |
| 490 | static inline unsigned int |
| 491 | get_r_sym(const Reltype* reloc) |
| 492 | { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); } |
| 493 | |
| 494 | // Return the type of the relocation. |
| 495 | static inline unsigned int |
| 496 | get_r_type(const Reltype* reloc) |
| 497 | { return elfcpp::elf_r_type<size>(reloc->get_r_info()); } |
| 498 | |
| 499 | // Return the explicit addend of the relocation (return 0 for SHT_REL). |
| 500 | static inline typename elfcpp::Elf_types<size>::Elf_Swxword |
| 501 | get_r_addend(const Reltype* reloc) |
| 502 | { return Reloc_types<sh_type_, size, big_endian>::get_reloc_addend(reloc); } |
| 503 | |
| 504 | // Write the r_info field to a new reloc, using the r_info field from |
| 505 | // the original reloc, replacing the r_sym field with R_SYM. |
| 506 | static inline void |
| 507 | put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym) |
| 508 | { |
| 509 | unsigned int r_type = elfcpp::elf_r_type<size>(reloc->get_r_info()); |
| 510 | new_reloc->put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type)); |
| 511 | } |
| 512 | |
| 513 | // Write the r_addend field to a new reloc. |
| 514 | static inline void |
| 515 | put_r_addend(Reltype_write* to, |
| 516 | typename elfcpp::Elf_types<size>::Elf_Swxword addend) |
| 517 | { Reloc_types<sh_type_, size, big_endian>::set_reloc_addend(to, addend); } |
| 518 | |
| 519 | // Return the size of the addend of the relocation (only used for SHT_REL). |
| 520 | static unsigned int |
| 521 | get_size_for_reloc(unsigned int, Relobj*) |
| 522 | { |
| 523 | gold_unreachable(); |
| 524 | return 0; |
| 525 | } |
| 526 | }; |
| 527 | |
| 528 | // This class may be used as a typical class for the |
| 529 | // Scan_relocatable_reloc parameter to scan_relocatable_relocs. |
| 530 | // This class is intended to capture the most typical target behaviour, |
| 531 | // while still permitting targets to define their own independent class |
| 532 | // for Scan_relocatable_reloc. |
| 533 | |
| 534 | template<typename Classify_reloc> |
| 535 | class Default_scan_relocatable_relocs |
| 536 | { |
| 537 | public: |
| 538 | typedef typename Classify_reloc::Reltype Reltype; |
| 539 | static const int reloc_size = Classify_reloc::reloc_size; |
| 540 | static const int sh_type = Classify_reloc::sh_type; |
| 541 | |
| 542 | // Return the symbol referred to by the relocation. |
| 543 | static inline unsigned int |
| 544 | get_r_sym(const Reltype* reloc) |
| 545 | { return Classify_reloc::get_r_sym(reloc); } |
| 546 | |
| 547 | // Return the type of the relocation. |
| 548 | static inline unsigned int |
| 549 | get_r_type(const Reltype* reloc) |
| 550 | { return Classify_reloc::get_r_type(reloc); } |
| 551 | |
| 552 | // Return the strategy to use for a local symbol which is not a |
| 553 | // section symbol, given the relocation type. |
| 554 | inline Relocatable_relocs::Reloc_strategy |
| 555 | local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym) |
| 556 | { |
| 557 | // We assume that relocation type 0 is NONE. Targets which are |
| 558 | // different must override. |
| 559 | if (r_type == 0 && r_sym == 0) |
| 560 | return Relocatable_relocs::RELOC_DISCARD; |
| 561 | return Relocatable_relocs::RELOC_COPY; |
| 562 | } |
| 563 | |
| 564 | // Return the strategy to use for a local symbol which is a section |
| 565 | // symbol, given the relocation type. |
| 566 | inline Relocatable_relocs::Reloc_strategy |
| 567 | local_section_strategy(unsigned int r_type, Relobj* object) |
| 568 | { |
| 569 | if (sh_type == elfcpp::SHT_RELA) |
| 570 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; |
| 571 | else |
| 572 | { |
| 573 | switch (Classify_reloc::get_size_for_reloc(r_type, object)) |
| 574 | { |
| 575 | case 0: |
| 576 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; |
| 577 | case 1: |
| 578 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1; |
| 579 | case 2: |
| 580 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2; |
| 581 | case 4: |
| 582 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4; |
| 583 | case 8: |
| 584 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8; |
| 585 | default: |
| 586 | gold_unreachable(); |
| 587 | } |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | // Return the strategy to use for a global symbol, given the |
| 592 | // relocation type, the object, and the symbol index. |
| 593 | inline Relocatable_relocs::Reloc_strategy |
| 594 | global_strategy(unsigned int, Relobj*, unsigned int) |
| 595 | { return Relocatable_relocs::RELOC_COPY; } |
| 596 | }; |
| 597 | |
| 598 | // This is a strategy class used with scan_relocatable_relocs |
| 599 | // and --emit-relocs. |
| 600 | |
| 601 | template<typename Classify_reloc> |
| 602 | class Default_emit_relocs_strategy |
| 603 | { |
| 604 | public: |
| 605 | typedef typename Classify_reloc::Reltype Reltype; |
| 606 | static const int reloc_size = Classify_reloc::reloc_size; |
| 607 | static const int sh_type = Classify_reloc::sh_type; |
| 608 | |
| 609 | // Return the symbol referred to by the relocation. |
| 610 | static inline unsigned int |
| 611 | get_r_sym(const Reltype* reloc) |
| 612 | { return Classify_reloc::get_r_sym(reloc); } |
| 613 | |
| 614 | // Return the type of the relocation. |
| 615 | static inline unsigned int |
| 616 | get_r_type(const Reltype* reloc) |
| 617 | { return Classify_reloc::get_r_type(reloc); } |
| 618 | |
| 619 | // A local non-section symbol. |
| 620 | inline Relocatable_relocs::Reloc_strategy |
| 621 | local_non_section_strategy(unsigned int, Relobj*, unsigned int) |
| 622 | { return Relocatable_relocs::RELOC_COPY; } |
| 623 | |
| 624 | // A local section symbol. |
| 625 | inline Relocatable_relocs::Reloc_strategy |
| 626 | local_section_strategy(unsigned int, Relobj*) |
| 627 | { |
| 628 | if (sh_type == elfcpp::SHT_RELA) |
| 629 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; |
| 630 | else |
| 631 | { |
| 632 | // The addend is stored in the section contents. Since this |
| 633 | // is not a relocatable link, we are going to apply the |
| 634 | // relocation contents to the section as usual. This means |
| 635 | // that we have no way to record the original addend. If the |
| 636 | // original addend is not zero, there is basically no way for |
| 637 | // the user to handle this correctly. Caveat emptor. |
| 638 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | // A global symbol. |
| 643 | inline Relocatable_relocs::Reloc_strategy |
| 644 | global_strategy(unsigned int, Relobj*, unsigned int) |
| 645 | { return Relocatable_relocs::RELOC_COPY; } |
| 646 | }; |
| 647 | |
| 648 | // Scan relocs during a relocatable link. This is a default |
| 649 | // definition which should work for most targets. |
| 650 | // Scan_relocatable_reloc must name a class type which provides three |
| 651 | // functions which return a Relocatable_relocs::Reloc_strategy code: |
| 652 | // global_strategy, local_non_section_strategy, and |
| 653 | // local_section_strategy. Most targets should be able to use |
| 654 | // Default_scan_relocatable_relocs as this class. |
| 655 | |
| 656 | template<int size, bool big_endian, typename Scan_relocatable_reloc> |
| 657 | void |
| 658 | scan_relocatable_relocs( |
| 659 | Symbol_table*, |
| 660 | Layout*, |
| 661 | Sized_relobj_file<size, big_endian>* object, |
| 662 | unsigned int data_shndx, |
| 663 | const unsigned char* prelocs, |
| 664 | size_t reloc_count, |
| 665 | Output_section* output_section, |
| 666 | bool needs_special_offset_handling, |
| 667 | size_t local_symbol_count, |
| 668 | const unsigned char* plocal_syms, |
| 669 | Relocatable_relocs* rr) |
| 670 | { |
| 671 | typedef typename Scan_relocatable_reloc::Reltype Reltype; |
| 672 | const int reloc_size = Scan_relocatable_reloc::reloc_size; |
| 673 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 674 | Scan_relocatable_reloc scan; |
| 675 | |
| 676 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
| 677 | { |
| 678 | Reltype reloc(prelocs); |
| 679 | |
| 680 | Relocatable_relocs::Reloc_strategy strategy; |
| 681 | |
| 682 | if (needs_special_offset_handling |
| 683 | && !output_section->is_input_address_mapped(object, data_shndx, |
| 684 | reloc.get_r_offset())) |
| 685 | strategy = Relocatable_relocs::RELOC_DISCARD; |
| 686 | else |
| 687 | { |
| 688 | const unsigned int r_sym = Scan_relocatable_reloc::get_r_sym(&reloc); |
| 689 | const unsigned int r_type = |
| 690 | Scan_relocatable_reloc::get_r_type(&reloc); |
| 691 | |
| 692 | if (r_sym >= local_symbol_count) |
| 693 | strategy = scan.global_strategy(r_type, object, r_sym); |
| 694 | else |
| 695 | { |
| 696 | gold_assert(plocal_syms != NULL); |
| 697 | typename elfcpp::Sym<size, big_endian> lsym(plocal_syms |
| 698 | + r_sym * sym_size); |
| 699 | unsigned int shndx = lsym.get_st_shndx(); |
| 700 | bool is_ordinary; |
| 701 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); |
| 702 | if (is_ordinary |
| 703 | && shndx != elfcpp::SHN_UNDEF |
| 704 | && !object->is_section_included(shndx)) |
| 705 | { |
| 706 | // RELOC is a relocation against a local symbol |
| 707 | // defined in a section we are discarding. Discard |
| 708 | // the reloc. FIXME: Should we issue a warning? |
| 709 | strategy = Relocatable_relocs::RELOC_DISCARD; |
| 710 | } |
| 711 | else if (lsym.get_st_type() != elfcpp::STT_SECTION) |
| 712 | strategy = scan.local_non_section_strategy(r_type, object, |
| 713 | r_sym); |
| 714 | else |
| 715 | { |
| 716 | strategy = scan.local_section_strategy(r_type, object); |
| 717 | if (strategy != Relocatable_relocs::RELOC_DISCARD) |
| 718 | object->output_section(shndx)->set_needs_symtab_index(); |
| 719 | } |
| 720 | |
| 721 | if (strategy == Relocatable_relocs::RELOC_COPY) |
| 722 | object->set_must_have_output_symtab_entry(r_sym); |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | rr->set_next_reloc_strategy(strategy); |
| 727 | } |
| 728 | } |
| 729 | |
| 730 | // Relocate relocs. Called for a relocatable link, and for --emit-relocs. |
| 731 | // This is a default definition which should work for most targets. |
| 732 | |
| 733 | template<int size, bool big_endian, typename Classify_reloc> |
| 734 | void |
| 735 | relocate_relocs( |
| 736 | const Relocate_info<size, big_endian>* relinfo, |
| 737 | const unsigned char* prelocs, |
| 738 | size_t reloc_count, |
| 739 | Output_section* output_section, |
| 740 | typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section, |
| 741 | unsigned char* view, |
| 742 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
| 743 | section_size_type view_size, |
| 744 | unsigned char* reloc_view, |
| 745 | section_size_type reloc_view_size) |
| 746 | { |
| 747 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
| 748 | typedef typename Classify_reloc::Reltype Reltype; |
| 749 | typedef typename Classify_reloc::Reltype_write Reltype_write; |
| 750 | const int reloc_size = Classify_reloc::reloc_size; |
| 751 | const Address invalid_address = static_cast<Address>(0) - 1; |
| 752 | |
| 753 | Sized_relobj_file<size, big_endian>* const object = relinfo->object; |
| 754 | const unsigned int local_count = object->local_symbol_count(); |
| 755 | |
| 756 | unsigned char* pwrite = reloc_view; |
| 757 | |
| 758 | const bool relocatable = parameters->options().relocatable(); |
| 759 | |
| 760 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
| 761 | { |
| 762 | Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i); |
| 763 | if (strategy == Relocatable_relocs::RELOC_DISCARD) |
| 764 | continue; |
| 765 | |
| 766 | if (strategy == Relocatable_relocs::RELOC_SPECIAL) |
| 767 | { |
| 768 | // Target wants to handle this relocation. |
| 769 | Sized_target<size, big_endian>* target = |
| 770 | parameters->sized_target<size, big_endian>(); |
| 771 | target->relocate_special_relocatable(relinfo, Classify_reloc::sh_type, |
| 772 | prelocs, i, output_section, |
| 773 | offset_in_output_section, |
| 774 | view, view_address, |
| 775 | view_size, pwrite); |
| 776 | pwrite += reloc_size; |
| 777 | continue; |
| 778 | } |
| 779 | Reltype reloc(prelocs); |
| 780 | Reltype_write reloc_write(pwrite); |
| 781 | |
| 782 | const unsigned int r_sym = Classify_reloc::get_r_sym(&reloc); |
| 783 | |
| 784 | // Get the new symbol index. |
| 785 | |
| 786 | Output_section* os = NULL; |
| 787 | unsigned int new_symndx; |
| 788 | if (r_sym < local_count) |
| 789 | { |
| 790 | switch (strategy) |
| 791 | { |
| 792 | case Relocatable_relocs::RELOC_COPY: |
| 793 | if (r_sym == 0) |
| 794 | new_symndx = 0; |
| 795 | else |
| 796 | { |
| 797 | new_symndx = object->symtab_index(r_sym); |
| 798 | gold_assert(new_symndx != -1U); |
| 799 | } |
| 800 | break; |
| 801 | |
| 802 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: |
| 803 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
| 804 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
| 805 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: |
| 806 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: |
| 807 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: |
| 808 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED: |
| 809 | { |
| 810 | // We are adjusting a section symbol. We need to find |
| 811 | // the symbol table index of the section symbol for |
| 812 | // the output section corresponding to input section |
| 813 | // in which this symbol is defined. |
| 814 | gold_assert(r_sym < local_count); |
| 815 | bool is_ordinary; |
| 816 | unsigned int shndx = |
| 817 | object->local_symbol_input_shndx(r_sym, &is_ordinary); |
| 818 | gold_assert(is_ordinary); |
| 819 | os = object->output_section(shndx); |
| 820 | gold_assert(os != NULL); |
| 821 | gold_assert(os->needs_symtab_index()); |
| 822 | new_symndx = os->symtab_index(); |
| 823 | } |
| 824 | break; |
| 825 | |
| 826 | default: |
| 827 | gold_unreachable(); |
| 828 | } |
| 829 | } |
| 830 | else |
| 831 | { |
| 832 | const Symbol* gsym = object->global_symbol(r_sym); |
| 833 | gold_assert(gsym != NULL); |
| 834 | if (gsym->is_forwarder()) |
| 835 | gsym = relinfo->symtab->resolve_forwards(gsym); |
| 836 | |
| 837 | gold_assert(gsym->has_symtab_index()); |
| 838 | new_symndx = gsym->symtab_index(); |
| 839 | } |
| 840 | |
| 841 | // Get the new offset--the location in the output section where |
| 842 | // this relocation should be applied. |
| 843 | |
| 844 | Address offset = reloc.get_r_offset(); |
| 845 | Address new_offset; |
| 846 | if (offset_in_output_section != invalid_address) |
| 847 | new_offset = offset + offset_in_output_section; |
| 848 | else |
| 849 | { |
| 850 | section_offset_type sot_offset = |
| 851 | convert_types<section_offset_type, Address>(offset); |
| 852 | section_offset_type new_sot_offset = |
| 853 | output_section->output_offset(object, relinfo->data_shndx, |
| 854 | sot_offset); |
| 855 | gold_assert(new_sot_offset != -1); |
| 856 | new_offset = new_sot_offset; |
| 857 | } |
| 858 | |
| 859 | // In an object file, r_offset is an offset within the section. |
| 860 | // In an executable or dynamic object, generated by |
| 861 | // --emit-relocs, r_offset is an absolute address. |
| 862 | if (!relocatable) |
| 863 | { |
| 864 | new_offset += view_address; |
| 865 | if (offset_in_output_section != invalid_address) |
| 866 | new_offset -= offset_in_output_section; |
| 867 | } |
| 868 | |
| 869 | reloc_write.put_r_offset(new_offset); |
| 870 | Classify_reloc::put_r_info(&reloc_write, &reloc, new_symndx); |
| 871 | |
| 872 | // Handle the reloc addend based on the strategy. |
| 873 | |
| 874 | if (strategy == Relocatable_relocs::RELOC_COPY) |
| 875 | { |
| 876 | if (Classify_reloc::sh_type == elfcpp::SHT_RELA) |
| 877 | Classify_reloc::put_r_addend(&reloc_write, |
| 878 | Classify_reloc::get_r_addend(&reloc)); |
| 879 | } |
| 880 | else |
| 881 | { |
| 882 | // The relocation uses a section symbol in the input file. |
| 883 | // We are adjusting it to use a section symbol in the output |
| 884 | // file. The input section symbol refers to some address in |
| 885 | // the input section. We need the relocation in the output |
| 886 | // file to refer to that same address. This adjustment to |
| 887 | // the addend is the same calculation we use for a simple |
| 888 | // absolute relocation for the input section symbol. |
| 889 | |
| 890 | const Symbol_value<size>* psymval = object->local_symbol(r_sym); |
| 891 | |
| 892 | unsigned char* padd = view + offset; |
| 893 | switch (strategy) |
| 894 | { |
| 895 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: |
| 896 | { |
| 897 | typename elfcpp::Elf_types<size>::Elf_Swxword addend |
| 898 | = Classify_reloc::get_r_addend(&reloc); |
| 899 | addend = psymval->value(object, addend); |
| 900 | // In a relocatable link, the symbol value is relative to |
| 901 | // the start of the output section. For a non-relocatable |
| 902 | // link, we need to adjust the addend. |
| 903 | if (!relocatable) |
| 904 | { |
| 905 | gold_assert(os != NULL); |
| 906 | addend -= os->address(); |
| 907 | } |
| 908 | Classify_reloc::put_r_addend(&reloc_write, addend); |
| 909 | } |
| 910 | break; |
| 911 | |
| 912 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
| 913 | break; |
| 914 | |
| 915 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
| 916 | Relocate_functions<size, big_endian>::rel8(padd, object, |
| 917 | psymval); |
| 918 | break; |
| 919 | |
| 920 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: |
| 921 | Relocate_functions<size, big_endian>::rel16(padd, object, |
| 922 | psymval); |
| 923 | break; |
| 924 | |
| 925 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: |
| 926 | Relocate_functions<size, big_endian>::rel32(padd, object, |
| 927 | psymval); |
| 928 | break; |
| 929 | |
| 930 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: |
| 931 | Relocate_functions<size, big_endian>::rel64(padd, object, |
| 932 | psymval); |
| 933 | break; |
| 934 | |
| 935 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED: |
| 936 | Relocate_functions<size, big_endian>::rel32_unaligned(padd, |
| 937 | object, |
| 938 | psymval); |
| 939 | break; |
| 940 | |
| 941 | default: |
| 942 | gold_unreachable(); |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | pwrite += reloc_size; |
| 947 | } |
| 948 | |
| 949 | gold_assert(static_cast<section_size_type>(pwrite - reloc_view) |
| 950 | == reloc_view_size); |
| 951 | } |
| 952 | |
| 953 | } // End namespace gold. |
| 954 | |
| 955 | #endif // !defined(GOLD_TARGET_RELOC_H) |