| 1 | // target-reloc.h -- target specific relocation support -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2006-2019 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_ERROR // Print an error. |
| 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_ERROR; |
| 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 | template<int size, bool big_endian> |
| 228 | inline void |
| 229 | issue_discarded_error( |
| 230 | const Relocate_info<size, big_endian>* relinfo, |
| 231 | size_t shndx, |
| 232 | section_offset_type offset, |
| 233 | unsigned int r_sym, |
| 234 | const Symbol* gsym) |
| 235 | { |
| 236 | Sized_relobj_file<size, big_endian>* object = relinfo->object; |
| 237 | |
| 238 | if (gsym == NULL) |
| 239 | { |
| 240 | gold_error_at_location( |
| 241 | relinfo, shndx, offset, |
| 242 | _("relocation refers to local symbol \"%s\" [%u], " |
| 243 | "which is defined in a discarded section"), |
| 244 | object->get_symbol_name(r_sym), r_sym); |
| 245 | } |
| 246 | else |
| 247 | { |
| 248 | gold_error_at_location( |
| 249 | relinfo, shndx, offset, |
| 250 | _("relocation refers to global symbol \"%s\", " |
| 251 | "which is defined in a discarded section"), |
| 252 | gsym->demangled_name().c_str()); |
| 253 | } |
| 254 | |
| 255 | bool is_ordinary; |
| 256 | typename elfcpp::Elf_types<size>::Elf_Addr value; |
| 257 | unsigned int orig_shndx = object->symbol_section_and_value(r_sym, &value, |
| 258 | &is_ordinary); |
| 259 | if (orig_shndx != elfcpp::SHN_UNDEF) |
| 260 | { |
| 261 | unsigned int key_symndx; |
| 262 | Relobj* kept_obj = object->find_kept_section_object(orig_shndx, |
| 263 | &key_symndx); |
| 264 | if (key_symndx != 0) |
| 265 | gold_info(_(" section group signature: \"%s\""), |
| 266 | object->get_symbol_name(key_symndx)); |
| 267 | if (kept_obj != NULL) |
| 268 | gold_info(_(" prevailing definition is from %s"), |
| 269 | kept_obj->name().c_str()); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // This function implements the generic part of relocation processing. |
| 274 | // The template parameter Relocate must be a class type which provides |
| 275 | // a single function, relocate(), which implements the machine |
| 276 | // specific part of a relocation. |
| 277 | |
| 278 | // The template parameter Relocate_comdat_behavior is a class type |
| 279 | // which provides a single function, get(), which determines what the |
| 280 | // linker should do for relocations that refer to discarded comdat |
| 281 | // sections. |
| 282 | |
| 283 | // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of |
| 284 | // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA. |
| 285 | // RELOCATE implements operator() to do a relocation. |
| 286 | |
| 287 | // PRELOCS points to the relocation data. RELOC_COUNT is the number |
| 288 | // of relocs. OUTPUT_SECTION is the output section. |
| 289 | // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be |
| 290 | // mapped to output offsets. |
| 291 | |
| 292 | // VIEW is the section data, VIEW_ADDRESS is its memory address, and |
| 293 | // VIEW_SIZE is the size. These refer to the input section, unless |
| 294 | // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to |
| 295 | // the output section. |
| 296 | |
| 297 | // RELOC_SYMBOL_CHANGES is used for -fsplit-stack support. If it is |
| 298 | // not NULL, it is a vector indexed by relocation index. If that |
| 299 | // entry is not NULL, it points to a global symbol which used as the |
| 300 | // symbol for the relocation, ignoring the symbol index in the |
| 301 | // relocation. |
| 302 | |
| 303 | template<int size, bool big_endian, typename Target_type, |
| 304 | typename Relocate, |
| 305 | typename Relocate_comdat_behavior, |
| 306 | typename Classify_reloc> |
| 307 | inline void |
| 308 | relocate_section( |
| 309 | const Relocate_info<size, big_endian>* relinfo, |
| 310 | Target_type* target, |
| 311 | const unsigned char* prelocs, |
| 312 | size_t reloc_count, |
| 313 | Output_section* output_section, |
| 314 | bool needs_special_offset_handling, |
| 315 | unsigned char* view, |
| 316 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
| 317 | section_size_type view_size, |
| 318 | const Reloc_symbol_changes* reloc_symbol_changes) |
| 319 | { |
| 320 | typedef typename Classify_reloc::Reltype Reltype; |
| 321 | const int reloc_size = Classify_reloc::reloc_size; |
| 322 | Relocate relocate; |
| 323 | Relocate_comdat_behavior relocate_comdat_behavior; |
| 324 | |
| 325 | Sized_relobj_file<size, big_endian>* object = relinfo->object; |
| 326 | unsigned int local_count = object->local_symbol_count(); |
| 327 | |
| 328 | Comdat_behavior comdat_behavior = CB_UNDETERMINED; |
| 329 | |
| 330 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
| 331 | { |
| 332 | Reltype reloc(prelocs); |
| 333 | |
| 334 | section_offset_type offset = |
| 335 | convert_to_section_size_type(reloc.get_r_offset()); |
| 336 | |
| 337 | if (needs_special_offset_handling) |
| 338 | { |
| 339 | offset = output_section->output_offset(relinfo->object, |
| 340 | relinfo->data_shndx, |
| 341 | offset); |
| 342 | if (offset == -1) |
| 343 | continue; |
| 344 | } |
| 345 | |
| 346 | unsigned int r_sym = Classify_reloc::get_r_sym(&reloc); |
| 347 | |
| 348 | const Sized_symbol<size>* sym; |
| 349 | |
| 350 | Symbol_value<size> symval; |
| 351 | const Symbol_value<size> *psymval; |
| 352 | bool is_defined_in_discarded_section; |
| 353 | unsigned int shndx; |
| 354 | const Symbol* gsym = NULL; |
| 355 | if (r_sym < local_count |
| 356 | && (reloc_symbol_changes == NULL |
| 357 | || (*reloc_symbol_changes)[i] == NULL)) |
| 358 | { |
| 359 | sym = NULL; |
| 360 | psymval = object->local_symbol(r_sym); |
| 361 | |
| 362 | // If the local symbol belongs to a section we are discarding, |
| 363 | // and that section is a debug section, try to find the |
| 364 | // corresponding kept section and map this symbol to its |
| 365 | // counterpart in the kept section. The symbol must not |
| 366 | // correspond to a section we are folding. |
| 367 | bool is_ordinary; |
| 368 | shndx = psymval->input_shndx(&is_ordinary); |
| 369 | is_defined_in_discarded_section = |
| 370 | (is_ordinary |
| 371 | && shndx != elfcpp::SHN_UNDEF |
| 372 | && !object->is_section_included(shndx) |
| 373 | && !relinfo->symtab->is_section_folded(object, shndx)); |
| 374 | } |
| 375 | else |
| 376 | { |
| 377 | if (reloc_symbol_changes != NULL |
| 378 | && (*reloc_symbol_changes)[i] != NULL) |
| 379 | gsym = (*reloc_symbol_changes)[i]; |
| 380 | else |
| 381 | { |
| 382 | gsym = object->global_symbol(r_sym); |
| 383 | gold_assert(gsym != NULL); |
| 384 | if (gsym->is_forwarder()) |
| 385 | gsym = relinfo->symtab->resolve_forwards(gsym); |
| 386 | } |
| 387 | |
| 388 | sym = static_cast<const Sized_symbol<size>*>(gsym); |
| 389 | if (sym->has_symtab_index() && sym->symtab_index() != -1U) |
| 390 | symval.set_output_symtab_index(sym->symtab_index()); |
| 391 | else |
| 392 | symval.set_no_output_symtab_entry(); |
| 393 | symval.set_output_value(sym->value()); |
| 394 | if (gsym->type() == elfcpp::STT_TLS) |
| 395 | symval.set_is_tls_symbol(); |
| 396 | else if (gsym->type() == elfcpp::STT_GNU_IFUNC) |
| 397 | symval.set_is_ifunc_symbol(); |
| 398 | psymval = &symval; |
| 399 | |
| 400 | is_defined_in_discarded_section = |
| 401 | (gsym->is_defined_in_discarded_section() |
| 402 | && gsym->is_undefined()); |
| 403 | shndx = 0; |
| 404 | } |
| 405 | |
| 406 | Symbol_value<size> symval2; |
| 407 | if (is_defined_in_discarded_section) |
| 408 | { |
| 409 | std::string name = object->section_name(relinfo->data_shndx); |
| 410 | |
| 411 | if (comdat_behavior == CB_UNDETERMINED) |
| 412 | comdat_behavior = relocate_comdat_behavior.get(name.c_str()); |
| 413 | |
| 414 | if (comdat_behavior == CB_PRETEND) |
| 415 | { |
| 416 | // FIXME: This case does not work for global symbols. |
| 417 | // We have no place to store the original section index. |
| 418 | // Fortunately this does not matter for comdat sections, |
| 419 | // only for sections explicitly discarded by a linker |
| 420 | // script. |
| 421 | bool found; |
| 422 | typename elfcpp::Elf_types<size>::Elf_Addr value = |
| 423 | object->map_to_kept_section(shndx, name, &found); |
| 424 | if (found) |
| 425 | symval2.set_output_value(value + psymval->input_value()); |
| 426 | else |
| 427 | symval2.set_output_value(0); |
| 428 | } |
| 429 | else |
| 430 | { |
| 431 | if (comdat_behavior == CB_ERROR) |
| 432 | issue_discarded_error(relinfo, i, offset, r_sym, gsym); |
| 433 | symval2.set_output_value(0); |
| 434 | } |
| 435 | symval2.set_no_output_symtab_entry(); |
| 436 | psymval = &symval2; |
| 437 | } |
| 438 | |
| 439 | // If OFFSET is out of range, still let the target decide to |
| 440 | // ignore the relocation. Pass in NULL as the VIEW argument so |
| 441 | // that it can return quickly without trashing an invalid memory |
| 442 | // address. |
| 443 | unsigned char *v = view + offset; |
| 444 | if (offset < 0 || static_cast<section_size_type>(offset) >= view_size) |
| 445 | v = NULL; |
| 446 | |
| 447 | if (!relocate.relocate(relinfo, Classify_reloc::sh_type, target, |
| 448 | output_section, i, prelocs, sym, psymval, |
| 449 | v, view_address + offset, view_size)) |
| 450 | continue; |
| 451 | |
| 452 | if (v == NULL) |
| 453 | { |
| 454 | gold_error_at_location(relinfo, i, offset, |
| 455 | _("reloc has bad offset %zu"), |
| 456 | static_cast<size_t>(offset)); |
| 457 | continue; |
| 458 | } |
| 459 | |
| 460 | if (issue_undefined_symbol_error(sym)) |
| 461 | gold_undefined_symbol_at_location(sym, relinfo, i, offset); |
| 462 | else if (sym != NULL |
| 463 | && sym->visibility() != elfcpp::STV_DEFAULT |
| 464 | && (sym->is_strong_undefined() || sym->is_from_dynobj())) |
| 465 | visibility_error(sym); |
| 466 | |
| 467 | if (sym != NULL && sym->has_warning()) |
| 468 | relinfo->symtab->issue_warning(sym, relinfo, i, offset); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | // Apply an incremental relocation. |
| 473 | |
| 474 | template<int size, bool big_endian, typename Target_type, |
| 475 | typename Relocate> |
| 476 | void |
| 477 | apply_relocation(const Relocate_info<size, big_endian>* relinfo, |
| 478 | Target_type* target, |
| 479 | typename elfcpp::Elf_types<size>::Elf_Addr r_offset, |
| 480 | unsigned int r_type, |
| 481 | typename elfcpp::Elf_types<size>::Elf_Swxword r_addend, |
| 482 | const Symbol* gsym, |
| 483 | unsigned char* view, |
| 484 | typename elfcpp::Elf_types<size>::Elf_Addr address, |
| 485 | section_size_type view_size) |
| 486 | { |
| 487 | // Construct the ELF relocation in a temporary buffer. |
| 488 | const int reloc_size = elfcpp::Elf_sizes<size>::rela_size; |
| 489 | unsigned char relbuf[reloc_size]; |
| 490 | elfcpp::Rela_write<size, big_endian> orel(relbuf); |
| 491 | orel.put_r_offset(r_offset); |
| 492 | orel.put_r_info(elfcpp::elf_r_info<size>(0, r_type)); |
| 493 | orel.put_r_addend(r_addend); |
| 494 | |
| 495 | // Setup a Symbol_value for the global symbol. |
| 496 | const Sized_symbol<size>* sym = static_cast<const Sized_symbol<size>*>(gsym); |
| 497 | Symbol_value<size> symval; |
| 498 | gold_assert(sym->has_symtab_index() && sym->symtab_index() != -1U); |
| 499 | symval.set_output_symtab_index(sym->symtab_index()); |
| 500 | symval.set_output_value(sym->value()); |
| 501 | if (gsym->type() == elfcpp::STT_TLS) |
| 502 | symval.set_is_tls_symbol(); |
| 503 | else if (gsym->type() == elfcpp::STT_GNU_IFUNC) |
| 504 | symval.set_is_ifunc_symbol(); |
| 505 | |
| 506 | Relocate relocate; |
| 507 | relocate.relocate(relinfo, elfcpp::SHT_RELA, target, NULL, |
| 508 | -1U, relbuf, sym, &symval, |
| 509 | view + r_offset, address + r_offset, view_size); |
| 510 | } |
| 511 | |
| 512 | // A class for inquiring about properties of a relocation, |
| 513 | // used while scanning relocs during a relocatable link and |
| 514 | // garbage collection. This class may be used as the default |
| 515 | // for SHT_RELA targets, but SHT_REL targets must implement |
| 516 | // a derived class that overrides get_size_for_reloc. |
| 517 | // The MIPS-64 target also needs to override the methods |
| 518 | // for accessing the r_sym and r_type fields of a relocation, |
| 519 | // due to its non-standard use of the r_info field. |
| 520 | |
| 521 | template<int sh_type_, int size, bool big_endian> |
| 522 | class Default_classify_reloc |
| 523 | { |
| 524 | public: |
| 525 | typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc |
| 526 | Reltype; |
| 527 | typedef typename Reloc_types<sh_type_, size, big_endian>::Reloc_write |
| 528 | Reltype_write; |
| 529 | static const int reloc_size = |
| 530 | Reloc_types<sh_type_, size, big_endian>::reloc_size; |
| 531 | static const int sh_type = sh_type_; |
| 532 | |
| 533 | // Return the symbol referred to by the relocation. |
| 534 | static inline unsigned int |
| 535 | get_r_sym(const Reltype* reloc) |
| 536 | { return elfcpp::elf_r_sym<size>(reloc->get_r_info()); } |
| 537 | |
| 538 | // Return the type of the relocation. |
| 539 | static inline unsigned int |
| 540 | get_r_type(const Reltype* reloc) |
| 541 | { return elfcpp::elf_r_type<size>(reloc->get_r_info()); } |
| 542 | |
| 543 | // Return the explicit addend of the relocation (return 0 for SHT_REL). |
| 544 | static inline typename elfcpp::Elf_types<size>::Elf_Swxword |
| 545 | get_r_addend(const Reltype* reloc) |
| 546 | { return Reloc_types<sh_type_, size, big_endian>::get_reloc_addend(reloc); } |
| 547 | |
| 548 | // Write the r_info field to a new reloc, using the r_info field from |
| 549 | // the original reloc, replacing the r_sym field with R_SYM. |
| 550 | static inline void |
| 551 | put_r_info(Reltype_write* new_reloc, Reltype* reloc, unsigned int r_sym) |
| 552 | { |
| 553 | unsigned int r_type = elfcpp::elf_r_type<size>(reloc->get_r_info()); |
| 554 | new_reloc->put_r_info(elfcpp::elf_r_info<size>(r_sym, r_type)); |
| 555 | } |
| 556 | |
| 557 | // Write the r_addend field to a new reloc. |
| 558 | static inline void |
| 559 | put_r_addend(Reltype_write* to, |
| 560 | typename elfcpp::Elf_types<size>::Elf_Swxword addend) |
| 561 | { Reloc_types<sh_type_, size, big_endian>::set_reloc_addend(to, addend); } |
| 562 | |
| 563 | // Return the size of the addend of the relocation (only used for SHT_REL). |
| 564 | static unsigned int |
| 565 | get_size_for_reloc(unsigned int, Relobj*) |
| 566 | { |
| 567 | gold_unreachable(); |
| 568 | return 0; |
| 569 | } |
| 570 | }; |
| 571 | |
| 572 | // This class may be used as a typical class for the |
| 573 | // Scan_relocatable_reloc parameter to scan_relocatable_relocs. |
| 574 | // This class is intended to capture the most typical target behaviour, |
| 575 | // while still permitting targets to define their own independent class |
| 576 | // for Scan_relocatable_reloc. |
| 577 | |
| 578 | template<typename Classify_reloc> |
| 579 | class Default_scan_relocatable_relocs |
| 580 | { |
| 581 | public: |
| 582 | typedef typename Classify_reloc::Reltype Reltype; |
| 583 | static const int reloc_size = Classify_reloc::reloc_size; |
| 584 | static const int sh_type = Classify_reloc::sh_type; |
| 585 | |
| 586 | // Return the symbol referred to by the relocation. |
| 587 | static inline unsigned int |
| 588 | get_r_sym(const Reltype* reloc) |
| 589 | { return Classify_reloc::get_r_sym(reloc); } |
| 590 | |
| 591 | // Return the type of the relocation. |
| 592 | static inline unsigned int |
| 593 | get_r_type(const Reltype* reloc) |
| 594 | { return Classify_reloc::get_r_type(reloc); } |
| 595 | |
| 596 | // Return the strategy to use for a local symbol which is not a |
| 597 | // section symbol, given the relocation type. |
| 598 | inline Relocatable_relocs::Reloc_strategy |
| 599 | local_non_section_strategy(unsigned int r_type, Relobj*, unsigned int r_sym) |
| 600 | { |
| 601 | // We assume that relocation type 0 is NONE. Targets which are |
| 602 | // different must override. |
| 603 | if (r_type == 0 && r_sym == 0) |
| 604 | return Relocatable_relocs::RELOC_DISCARD; |
| 605 | return Relocatable_relocs::RELOC_COPY; |
| 606 | } |
| 607 | |
| 608 | // Return the strategy to use for a local symbol which is a section |
| 609 | // symbol, given the relocation type. |
| 610 | inline Relocatable_relocs::Reloc_strategy |
| 611 | local_section_strategy(unsigned int r_type, Relobj* object) |
| 612 | { |
| 613 | if (sh_type == elfcpp::SHT_RELA) |
| 614 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; |
| 615 | else |
| 616 | { |
| 617 | switch (Classify_reloc::get_size_for_reloc(r_type, object)) |
| 618 | { |
| 619 | case 0: |
| 620 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; |
| 621 | case 1: |
| 622 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1; |
| 623 | case 2: |
| 624 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2; |
| 625 | case 4: |
| 626 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4; |
| 627 | case 8: |
| 628 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8; |
| 629 | default: |
| 630 | gold_unreachable(); |
| 631 | } |
| 632 | } |
| 633 | } |
| 634 | |
| 635 | // Return the strategy to use for a global symbol, given the |
| 636 | // relocation type, the object, and the symbol index. |
| 637 | inline Relocatable_relocs::Reloc_strategy |
| 638 | global_strategy(unsigned int, Relobj*, unsigned int) |
| 639 | { return Relocatable_relocs::RELOC_COPY; } |
| 640 | }; |
| 641 | |
| 642 | // This is a strategy class used with scan_relocatable_relocs |
| 643 | // and --emit-relocs. |
| 644 | |
| 645 | template<typename Classify_reloc> |
| 646 | class Default_emit_relocs_strategy |
| 647 | { |
| 648 | public: |
| 649 | typedef typename Classify_reloc::Reltype Reltype; |
| 650 | static const int reloc_size = Classify_reloc::reloc_size; |
| 651 | static const int sh_type = Classify_reloc::sh_type; |
| 652 | |
| 653 | // Return the symbol referred to by the relocation. |
| 654 | static inline unsigned int |
| 655 | get_r_sym(const Reltype* reloc) |
| 656 | { return Classify_reloc::get_r_sym(reloc); } |
| 657 | |
| 658 | // Return the type of the relocation. |
| 659 | static inline unsigned int |
| 660 | get_r_type(const Reltype* reloc) |
| 661 | { return Classify_reloc::get_r_type(reloc); } |
| 662 | |
| 663 | // A local non-section symbol. |
| 664 | inline Relocatable_relocs::Reloc_strategy |
| 665 | local_non_section_strategy(unsigned int, Relobj*, unsigned int) |
| 666 | { return Relocatable_relocs::RELOC_COPY; } |
| 667 | |
| 668 | // A local section symbol. |
| 669 | inline Relocatable_relocs::Reloc_strategy |
| 670 | local_section_strategy(unsigned int, Relobj*) |
| 671 | { |
| 672 | if (sh_type == elfcpp::SHT_RELA) |
| 673 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; |
| 674 | else |
| 675 | { |
| 676 | // The addend is stored in the section contents. Since this |
| 677 | // is not a relocatable link, we are going to apply the |
| 678 | // relocation contents to the section as usual. This means |
| 679 | // that we have no way to record the original addend. If the |
| 680 | // original addend is not zero, there is basically no way for |
| 681 | // the user to handle this correctly. Caveat emptor. |
| 682 | return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | // A global symbol. |
| 687 | inline Relocatable_relocs::Reloc_strategy |
| 688 | global_strategy(unsigned int, Relobj*, unsigned int) |
| 689 | { return Relocatable_relocs::RELOC_COPY; } |
| 690 | }; |
| 691 | |
| 692 | // Scan relocs during a relocatable link. This is a default |
| 693 | // definition which should work for most targets. |
| 694 | // Scan_relocatable_reloc must name a class type which provides three |
| 695 | // functions which return a Relocatable_relocs::Reloc_strategy code: |
| 696 | // global_strategy, local_non_section_strategy, and |
| 697 | // local_section_strategy. Most targets should be able to use |
| 698 | // Default_scan_relocatable_relocs as this class. |
| 699 | |
| 700 | template<int size, bool big_endian, typename Scan_relocatable_reloc> |
| 701 | void |
| 702 | scan_relocatable_relocs( |
| 703 | Symbol_table*, |
| 704 | Layout*, |
| 705 | Sized_relobj_file<size, big_endian>* object, |
| 706 | unsigned int data_shndx, |
| 707 | const unsigned char* prelocs, |
| 708 | size_t reloc_count, |
| 709 | Output_section* output_section, |
| 710 | bool needs_special_offset_handling, |
| 711 | size_t local_symbol_count, |
| 712 | const unsigned char* plocal_syms, |
| 713 | Relocatable_relocs* rr) |
| 714 | { |
| 715 | typedef typename Scan_relocatable_reloc::Reltype Reltype; |
| 716 | const int reloc_size = Scan_relocatable_reloc::reloc_size; |
| 717 | const int sym_size = elfcpp::Elf_sizes<size>::sym_size; |
| 718 | Scan_relocatable_reloc scan; |
| 719 | |
| 720 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
| 721 | { |
| 722 | Reltype reloc(prelocs); |
| 723 | |
| 724 | Relocatable_relocs::Reloc_strategy strategy; |
| 725 | |
| 726 | if (needs_special_offset_handling |
| 727 | && !output_section->is_input_address_mapped(object, data_shndx, |
| 728 | reloc.get_r_offset())) |
| 729 | strategy = Relocatable_relocs::RELOC_DISCARD; |
| 730 | else |
| 731 | { |
| 732 | const unsigned int r_sym = Scan_relocatable_reloc::get_r_sym(&reloc); |
| 733 | const unsigned int r_type = |
| 734 | Scan_relocatable_reloc::get_r_type(&reloc); |
| 735 | |
| 736 | if (r_sym >= local_symbol_count) |
| 737 | strategy = scan.global_strategy(r_type, object, r_sym); |
| 738 | else |
| 739 | { |
| 740 | gold_assert(plocal_syms != NULL); |
| 741 | typename elfcpp::Sym<size, big_endian> lsym(plocal_syms |
| 742 | + r_sym * sym_size); |
| 743 | unsigned int shndx = lsym.get_st_shndx(); |
| 744 | bool is_ordinary; |
| 745 | shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); |
| 746 | if (is_ordinary |
| 747 | && shndx != elfcpp::SHN_UNDEF |
| 748 | && !object->is_section_included(shndx)) |
| 749 | { |
| 750 | // RELOC is a relocation against a local symbol |
| 751 | // defined in a section we are discarding. Discard |
| 752 | // the reloc. FIXME: Should we issue a warning? |
| 753 | strategy = Relocatable_relocs::RELOC_DISCARD; |
| 754 | } |
| 755 | else if (lsym.get_st_type() != elfcpp::STT_SECTION) |
| 756 | strategy = scan.local_non_section_strategy(r_type, object, |
| 757 | r_sym); |
| 758 | else |
| 759 | { |
| 760 | strategy = scan.local_section_strategy(r_type, object); |
| 761 | if (strategy != Relocatable_relocs::RELOC_DISCARD) |
| 762 | object->output_section(shndx)->set_needs_symtab_index(); |
| 763 | } |
| 764 | |
| 765 | if (strategy == Relocatable_relocs::RELOC_COPY) |
| 766 | object->set_must_have_output_symtab_entry(r_sym); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | rr->set_next_reloc_strategy(strategy); |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | // Relocate relocs. Called for a relocatable link, and for --emit-relocs. |
| 775 | // This is a default definition which should work for most targets. |
| 776 | |
| 777 | template<int size, bool big_endian, typename Classify_reloc> |
| 778 | void |
| 779 | relocate_relocs( |
| 780 | const Relocate_info<size, big_endian>* relinfo, |
| 781 | const unsigned char* prelocs, |
| 782 | size_t reloc_count, |
| 783 | Output_section* output_section, |
| 784 | typename elfcpp::Elf_types<size>::Elf_Off offset_in_output_section, |
| 785 | unsigned char* view, |
| 786 | typename elfcpp::Elf_types<size>::Elf_Addr view_address, |
| 787 | section_size_type view_size, |
| 788 | unsigned char* reloc_view, |
| 789 | section_size_type reloc_view_size) |
| 790 | { |
| 791 | typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; |
| 792 | typedef typename Classify_reloc::Reltype Reltype; |
| 793 | typedef typename Classify_reloc::Reltype_write Reltype_write; |
| 794 | const int reloc_size = Classify_reloc::reloc_size; |
| 795 | const Address invalid_address = static_cast<Address>(0) - 1; |
| 796 | |
| 797 | Sized_relobj_file<size, big_endian>* const object = relinfo->object; |
| 798 | const unsigned int local_count = object->local_symbol_count(); |
| 799 | |
| 800 | unsigned char* pwrite = reloc_view; |
| 801 | |
| 802 | const bool relocatable = parameters->options().relocatable(); |
| 803 | |
| 804 | for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) |
| 805 | { |
| 806 | Relocatable_relocs::Reloc_strategy strategy = relinfo->rr->strategy(i); |
| 807 | if (strategy == Relocatable_relocs::RELOC_DISCARD) |
| 808 | continue; |
| 809 | |
| 810 | if (strategy == Relocatable_relocs::RELOC_SPECIAL) |
| 811 | { |
| 812 | // Target wants to handle this relocation. |
| 813 | Sized_target<size, big_endian>* target = |
| 814 | parameters->sized_target<size, big_endian>(); |
| 815 | target->relocate_special_relocatable(relinfo, Classify_reloc::sh_type, |
| 816 | prelocs, i, output_section, |
| 817 | offset_in_output_section, |
| 818 | view, view_address, |
| 819 | view_size, pwrite); |
| 820 | pwrite += reloc_size; |
| 821 | continue; |
| 822 | } |
| 823 | Reltype reloc(prelocs); |
| 824 | Reltype_write reloc_write(pwrite); |
| 825 | |
| 826 | const unsigned int r_sym = Classify_reloc::get_r_sym(&reloc); |
| 827 | |
| 828 | // Get the new symbol index. |
| 829 | |
| 830 | Output_section* os = NULL; |
| 831 | unsigned int new_symndx; |
| 832 | if (r_sym < local_count) |
| 833 | { |
| 834 | switch (strategy) |
| 835 | { |
| 836 | case Relocatable_relocs::RELOC_COPY: |
| 837 | if (r_sym == 0) |
| 838 | new_symndx = 0; |
| 839 | else |
| 840 | { |
| 841 | new_symndx = object->symtab_index(r_sym); |
| 842 | gold_assert(new_symndx != -1U); |
| 843 | } |
| 844 | break; |
| 845 | |
| 846 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: |
| 847 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
| 848 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
| 849 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: |
| 850 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: |
| 851 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: |
| 852 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED: |
| 853 | { |
| 854 | // We are adjusting a section symbol. We need to find |
| 855 | // the symbol table index of the section symbol for |
| 856 | // the output section corresponding to input section |
| 857 | // in which this symbol is defined. |
| 858 | gold_assert(r_sym < local_count); |
| 859 | bool is_ordinary; |
| 860 | unsigned int shndx = |
| 861 | object->local_symbol_input_shndx(r_sym, &is_ordinary); |
| 862 | gold_assert(is_ordinary); |
| 863 | os = object->output_section(shndx); |
| 864 | gold_assert(os != NULL); |
| 865 | gold_assert(os->needs_symtab_index()); |
| 866 | new_symndx = os->symtab_index(); |
| 867 | } |
| 868 | break; |
| 869 | |
| 870 | default: |
| 871 | gold_unreachable(); |
| 872 | } |
| 873 | } |
| 874 | else |
| 875 | { |
| 876 | const Symbol* gsym = object->global_symbol(r_sym); |
| 877 | gold_assert(gsym != NULL); |
| 878 | if (gsym->is_forwarder()) |
| 879 | gsym = relinfo->symtab->resolve_forwards(gsym); |
| 880 | |
| 881 | gold_assert(gsym->has_symtab_index()); |
| 882 | new_symndx = gsym->symtab_index(); |
| 883 | } |
| 884 | |
| 885 | // Get the new offset--the location in the output section where |
| 886 | // this relocation should be applied. |
| 887 | |
| 888 | Address offset = reloc.get_r_offset(); |
| 889 | Address new_offset; |
| 890 | if (offset_in_output_section != invalid_address) |
| 891 | new_offset = offset + offset_in_output_section; |
| 892 | else |
| 893 | { |
| 894 | section_offset_type sot_offset = |
| 895 | convert_types<section_offset_type, Address>(offset); |
| 896 | section_offset_type new_sot_offset = |
| 897 | output_section->output_offset(object, relinfo->data_shndx, |
| 898 | sot_offset); |
| 899 | gold_assert(new_sot_offset != -1); |
| 900 | new_offset = new_sot_offset; |
| 901 | } |
| 902 | |
| 903 | // In an object file, r_offset is an offset within the section. |
| 904 | // In an executable or dynamic object, generated by |
| 905 | // --emit-relocs, r_offset is an absolute address. |
| 906 | if (!relocatable) |
| 907 | { |
| 908 | new_offset += view_address; |
| 909 | if (offset_in_output_section != invalid_address) |
| 910 | new_offset -= offset_in_output_section; |
| 911 | } |
| 912 | |
| 913 | reloc_write.put_r_offset(new_offset); |
| 914 | Classify_reloc::put_r_info(&reloc_write, &reloc, new_symndx); |
| 915 | |
| 916 | // Handle the reloc addend based on the strategy. |
| 917 | |
| 918 | if (strategy == Relocatable_relocs::RELOC_COPY) |
| 919 | { |
| 920 | if (Classify_reloc::sh_type == elfcpp::SHT_RELA) |
| 921 | Classify_reloc::put_r_addend(&reloc_write, |
| 922 | Classify_reloc::get_r_addend(&reloc)); |
| 923 | } |
| 924 | else |
| 925 | { |
| 926 | // The relocation uses a section symbol in the input file. |
| 927 | // We are adjusting it to use a section symbol in the output |
| 928 | // file. The input section symbol refers to some address in |
| 929 | // the input section. We need the relocation in the output |
| 930 | // file to refer to that same address. This adjustment to |
| 931 | // the addend is the same calculation we use for a simple |
| 932 | // absolute relocation for the input section symbol. |
| 933 | |
| 934 | const Symbol_value<size>* psymval = object->local_symbol(r_sym); |
| 935 | |
| 936 | unsigned char* padd = view + offset; |
| 937 | switch (strategy) |
| 938 | { |
| 939 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: |
| 940 | { |
| 941 | typename elfcpp::Elf_types<size>::Elf_Swxword addend |
| 942 | = Classify_reloc::get_r_addend(&reloc); |
| 943 | addend = psymval->value(object, addend); |
| 944 | // In a relocatable link, the symbol value is relative to |
| 945 | // the start of the output section. For a non-relocatable |
| 946 | // link, we need to adjust the addend. |
| 947 | if (!relocatable) |
| 948 | { |
| 949 | gold_assert(os != NULL); |
| 950 | addend -= os->address(); |
| 951 | } |
| 952 | Classify_reloc::put_r_addend(&reloc_write, addend); |
| 953 | } |
| 954 | break; |
| 955 | |
| 956 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: |
| 957 | break; |
| 958 | |
| 959 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: |
| 960 | Relocate_functions<size, big_endian>::rel8(padd, object, |
| 961 | psymval); |
| 962 | break; |
| 963 | |
| 964 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: |
| 965 | Relocate_functions<size, big_endian>::rel16(padd, object, |
| 966 | psymval); |
| 967 | break; |
| 968 | |
| 969 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: |
| 970 | Relocate_functions<size, big_endian>::rel32(padd, object, |
| 971 | psymval); |
| 972 | break; |
| 973 | |
| 974 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: |
| 975 | Relocate_functions<size, big_endian>::rel64(padd, object, |
| 976 | psymval); |
| 977 | break; |
| 978 | |
| 979 | case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4_UNALIGNED: |
| 980 | Relocate_functions<size, big_endian>::rel32_unaligned(padd, |
| 981 | object, |
| 982 | psymval); |
| 983 | break; |
| 984 | |
| 985 | default: |
| 986 | gold_unreachable(); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | pwrite += reloc_size; |
| 991 | } |
| 992 | |
| 993 | gold_assert(static_cast<section_size_type>(pwrite - reloc_view) |
| 994 | == reloc_view_size); |
| 995 | } |
| 996 | |
| 997 | } // End namespace gold. |
| 998 | |
| 999 | #endif // !defined(GOLD_TARGET_RELOC_H) |