1 // target-reloc.h -- target specific relocation support -*- C++ -*- 2 3 // Copyright 2006, 2007, 2008 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 "reloc.h" 29 #include "reloc-types.h" 30 31 namespace gold 32 { 33 34 // This function implements the generic part of reloc scanning. The 35 // template parameter Scan must be a class type which provides two 36 // functions: local() and global(). Those functions implement the 37 // machine specific part of scanning. We do it this way to 38 // avoidmaking a function call for each relocation, and to avoid 39 // repeating the generic code for each target. 40 41 template<int size, bool big_endian, typename Target_type, int sh_type, 42 typename Scan> 43 inline void 44 scan_relocs( 45 const General_options& options, 46 Symbol_table* symtab, 47 Layout* layout, 48 Target_type* target, 49 Sized_relobj<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 Reloc_types<sh_type, size, big_endian>::Reloc Reltype; 59 const int reloc_size = Reloc_types<sh_type, size, big_endian>::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 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); 73 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 74 unsigned int r_type = elfcpp::elf_r_type<size>(r_info); 75 76 if (r_sym < local_count) 77 { 78 gold_assert(plocal_syms != NULL); 79 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms 80 + r_sym * sym_size); 81 unsigned int shndx = lsym.get_st_shndx(); 82 bool is_ordinary; 83 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); 84 if (is_ordinary 85 && shndx != elfcpp::SHN_UNDEF 86 && !object->is_section_included(shndx)) 87 { 88 // RELOC is a relocation against a local symbol in a 89 // section we are discarding. We can ignore this 90 // relocation. It will eventually become a reloc 91 // against the value zero. 92 // 93 // FIXME: We should issue a warning if this is an 94 // allocated section; is this the best place to do it? 95 // 96 // FIXME: The old GNU linker would in some cases look 97 // for the linkonce section which caused this section to 98 // be discarded, and, if the other section was the same 99 // size, change the reloc to refer to the other section. 100 // That seems risky and weird to me, and I don't know of 101 // any case where it is actually required. 102 103 continue; 104 } 105 106 scan.local(options, symtab, layout, target, object, data_shndx, 107 output_section, reloc, r_type, lsym); 108 } 109 else 110 { 111 Symbol* gsym = object->global_symbol(r_sym); 112 gold_assert(gsym != NULL); 113 if (gsym->is_forwarder()) 114 gsym = symtab->resolve_forwards(gsym); 115 116 scan.global(options, symtab, layout, target, object, data_shndx, 117 output_section, reloc, r_type, gsym); 118 } 119 } 120 } 121 122 // Behavior for relocations to discarded comdat sections. 123 124 enum Comdat_behavior 125 { 126 CB_UNDETERMINED, // Not yet determined -- need to look at section name. 127 CB_PRETEND, // Attempt to map to the corresponding kept section. 128 CB_IGNORE, // Ignore the relocation. 129 CB_WARNING // Print a warning. 130 }; 131 132 // Decide what the linker should do for relocations that refer to discarded 133 // comdat sections. This decision is based on the name of the section being 134 // relocated. 135 136 inline Comdat_behavior 137 get_comdat_behavior(const char* name) 138 { 139 if (Layout::is_debug_info_section(name)) 140 return CB_PRETEND; 141 if (strcmp(name, ".eh_frame") == 0 142 || strcmp(name, ".gcc_except_table") == 0) 143 return CB_IGNORE; 144 return CB_WARNING; 145 } 146 147 // This function implements the generic part of relocation processing. 148 // The template parameter Relocate must be a class type which provides 149 // a single function, relocate(), which implements the machine 150 // specific part of a relocation. 151 152 // SIZE is the ELF size: 32 or 64. BIG_ENDIAN is the endianness of 153 // the data. SH_TYPE is the section type: SHT_REL or SHT_RELA. 154 // RELOCATE implements operator() to do a relocation. 155 156 // PRELOCS points to the relocation data. RELOC_COUNT is the number 157 // of relocs. OUTPUT_SECTION is the output section. 158 // NEEDS_SPECIAL_OFFSET_HANDLING is true if input offsets need to be 159 // mapped to output offsets. 160 161 // VIEW is the section data, VIEW_ADDRESS is its memory address, and 162 // VIEW_SIZE is the size. These refer to the input section, unless 163 // NEEDS_SPECIAL_OFFSET_HANDLING is true, in which case they refer to 164 // the output section. 165 166 template<int size, bool big_endian, typename Target_type, int sh_type, 167 typename Relocate> 168 inline void 169 relocate_section( 170 const Relocate_info<size, big_endian>* relinfo, 171 Target_type* target, 172 const unsigned char* prelocs, 173 size_t reloc_count, 174 Output_section* output_section, 175 bool needs_special_offset_handling, 176 unsigned char* view, 177 typename elfcpp::Elf_types<size>::Elf_Addr view_address, 178 section_size_type view_size) 179 { 180 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; 181 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; 182 Relocate relocate; 183 184 Sized_relobj<size, big_endian>* object = relinfo->object; 185 unsigned int local_count = object->local_symbol_count(); 186 187 Comdat_behavior comdat_behavior = CB_UNDETERMINED; 188 189 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) 190 { 191 Reltype reloc(prelocs); 192 193 section_offset_type offset = 194 convert_to_section_size_type(reloc.get_r_offset()); 195 196 if (needs_special_offset_handling) 197 { 198 offset = output_section->output_offset(relinfo->object, 199 relinfo->data_shndx, 200 offset); 201 if (offset == -1) 202 continue; 203 } 204 205 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); 206 unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 207 unsigned int r_type = elfcpp::elf_r_type<size>(r_info); 208 209 const Sized_symbol<size>* sym; 210 211 Symbol_value<size> symval; 212 const Symbol_value<size> *psymval; 213 if (r_sym < local_count) 214 { 215 sym = NULL; 216 psymval = object->local_symbol(r_sym); 217 218 // If the local symbol belongs to a section we are discarding, 219 // and that section is a debug section, try to find the 220 // corresponding kept section and map this symbol to its 221 // counterpart in the kept section. 222 bool is_ordinary; 223 unsigned int shndx = psymval->input_shndx(&is_ordinary); 224 if (is_ordinary 225 && shndx != elfcpp::SHN_UNDEF 226 && !object->is_section_included(shndx)) 227 { 228 if (comdat_behavior == CB_UNDETERMINED) 229 { 230 std::string name = object->section_name(relinfo->data_shndx); 231 comdat_behavior = get_comdat_behavior(name.c_str()); 232 } 233 if (comdat_behavior == CB_PRETEND) 234 { 235 bool found; 236 typename elfcpp::Elf_types<size>::Elf_Addr value = 237 object->map_to_kept_section(shndx, &found); 238 if (found) 239 symval.set_output_value(value + psymval->input_value()); 240 else 241 symval.set_output_value(0); 242 } 243 else 244 { 245 if (comdat_behavior == CB_WARNING) 246 gold_warning_at_location(relinfo, i, offset, 247 _("Relocation refers to discarded " 248 "comdat section")); 249 symval.set_output_value(0); 250 } 251 symval.set_no_output_symtab_entry(); 252 psymval = &symval; 253 } 254 } 255 else 256 { 257 const Symbol* gsym = object->global_symbol(r_sym); 258 gold_assert(gsym != NULL); 259 if (gsym->is_forwarder()) 260 gsym = relinfo->symtab->resolve_forwards(gsym); 261 262 sym = static_cast<const Sized_symbol<size>*>(gsym); 263 if (sym->has_symtab_index()) 264 symval.set_output_symtab_index(sym->symtab_index()); 265 else 266 symval.set_no_output_symtab_entry(); 267 symval.set_output_value(sym->value()); 268 psymval = &symval; 269 } 270 271 if (!relocate.relocate(relinfo, target, i, reloc, r_type, sym, psymval, 272 view + offset, view_address + offset, view_size)) 273 continue; 274 275 if (offset < 0 || static_cast<section_size_type>(offset) >= view_size) 276 { 277 gold_error_at_location(relinfo, i, offset, 278 _("reloc has bad offset %zu"), 279 static_cast<size_t>(offset)); 280 continue; 281 } 282 283 if (sym != NULL 284 && sym->is_undefined() 285 && sym->binding() != elfcpp::STB_WEAK 286 && (!parameters->options().shared() // -shared 287 || parameters->options().defs())) // -z defs 288 gold_undefined_symbol(sym, relinfo, i, offset); 289 290 if (sym != NULL && sym->has_warning()) 291 relinfo->symtab->issue_warning(sym, relinfo, i, offset); 292 } 293 } 294 295 // This class may be used as a typical class for the 296 // Scan_relocatable_reloc parameter to scan_relocatable_relocs. The 297 // template parameter Classify_reloc must be a class type which 298 // provides a function get_size_for_reloc which returns the number of 299 // bytes to which a reloc applies. This class is intended to capture 300 // the most typical target behaviour, while still permitting targets 301 // to define their own independent class for Scan_relocatable_reloc. 302 303 template<int sh_type, typename Classify_reloc> 304 class Default_scan_relocatable_relocs 305 { 306 public: 307 // Return the strategy to use for a local symbol which is not a 308 // section symbol, given the relocation type. 309 inline Relocatable_relocs::Reloc_strategy 310 local_non_section_strategy(unsigned int, Relobj*) 311 { return Relocatable_relocs::RELOC_COPY; } 312 313 // Return the strategy to use for a local symbol which is a section 314 // symbol, given the relocation type. 315 inline Relocatable_relocs::Reloc_strategy 316 local_section_strategy(unsigned int r_type, Relobj* object) 317 { 318 if (sh_type == elfcpp::SHT_RELA) 319 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA; 320 else 321 { 322 Classify_reloc classify; 323 switch (classify.get_size_for_reloc(r_type, object)) 324 { 325 case 0: 326 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0; 327 case 1: 328 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1; 329 case 2: 330 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2; 331 case 4: 332 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4; 333 case 8: 334 return Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8; 335 default: 336 gold_unreachable(); 337 } 338 } 339 } 340 341 // Return the strategy to use for a global symbol, given the 342 // relocation type, the object, and the symbol index. 343 inline Relocatable_relocs::Reloc_strategy 344 global_strategy(unsigned int, Relobj*, unsigned int) 345 { return Relocatable_relocs::RELOC_COPY; } 346 }; 347 348 // Scan relocs during a relocatable link. This is a default 349 // definition which should work for most targets. 350 // Scan_relocatable_reloc must name a class type which provides three 351 // functions which return a Relocatable_relocs::Reloc_strategy code: 352 // global_strategy, local_non_section_strategy, and 353 // local_section_strategy. Most targets should be able to use 354 // Default_scan_relocatable_relocs as this class. 355 356 template<int size, bool big_endian, int sh_type, 357 typename Scan_relocatable_reloc> 358 void 359 scan_relocatable_relocs( 360 const General_options&, 361 Symbol_table*, 362 Layout*, 363 Sized_relobj<size, big_endian>* object, 364 unsigned int data_shndx, 365 const unsigned char* prelocs, 366 size_t reloc_count, 367 Output_section* output_section, 368 bool needs_special_offset_handling, 369 size_t local_symbol_count, 370 const unsigned char* plocal_syms, 371 Relocatable_relocs* rr) 372 { 373 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; 374 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; 375 const int sym_size = elfcpp::Elf_sizes<size>::sym_size; 376 Scan_relocatable_reloc scan; 377 378 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) 379 { 380 Reltype reloc(prelocs); 381 382 Relocatable_relocs::Reloc_strategy strategy; 383 384 if (needs_special_offset_handling 385 && !output_section->is_input_address_mapped(object, data_shndx, 386 reloc.get_r_offset())) 387 strategy = Relocatable_relocs::RELOC_DISCARD; 388 else 389 { 390 typename elfcpp::Elf_types<size>::Elf_WXword r_info = 391 reloc.get_r_info(); 392 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 393 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); 394 395 if (r_sym >= local_symbol_count) 396 strategy = scan.global_strategy(r_type, object, r_sym); 397 else 398 { 399 gold_assert(plocal_syms != NULL); 400 typename elfcpp::Sym<size, big_endian> lsym(plocal_syms 401 + r_sym * sym_size); 402 unsigned int shndx = lsym.get_st_shndx(); 403 bool is_ordinary; 404 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary); 405 if (is_ordinary 406 && shndx != elfcpp::SHN_UNDEF 407 && !object->is_section_included(shndx)) 408 { 409 // RELOC is a relocation against a local symbol 410 // defined in a section we are discarding. Discard 411 // the reloc. FIXME: Should we issue a warning? 412 strategy = Relocatable_relocs::RELOC_DISCARD; 413 } 414 else if (lsym.get_st_type() != elfcpp::STT_SECTION) 415 strategy = scan.local_non_section_strategy(r_type, object); 416 else 417 { 418 strategy = scan.local_section_strategy(r_type, object); 419 if (strategy != Relocatable_relocs::RELOC_DISCARD) 420 object->output_section(shndx)->set_needs_symtab_index(); 421 } 422 } 423 } 424 425 rr->set_next_reloc_strategy(strategy); 426 } 427 } 428 429 // Relocate relocs during a relocatable link. This is a default 430 // definition which should work for most targets. 431 432 template<int size, bool big_endian, int sh_type> 433 void 434 relocate_for_relocatable( 435 const Relocate_info<size, big_endian>* relinfo, 436 const unsigned char* prelocs, 437 size_t reloc_count, 438 Output_section* output_section, 439 typename elfcpp::Elf_types<size>::Elf_Addr offset_in_output_section, 440 const Relocatable_relocs* rr, 441 unsigned char* view, 442 typename elfcpp::Elf_types<size>::Elf_Addr view_address, 443 section_size_type, 444 unsigned char* reloc_view, 445 section_size_type reloc_view_size) 446 { 447 typedef typename elfcpp::Elf_types<size>::Elf_Addr Address; 448 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc Reltype; 449 typedef typename Reloc_types<sh_type, size, big_endian>::Reloc_write 450 Reltype_write; 451 const int reloc_size = Reloc_types<sh_type, size, big_endian>::reloc_size; 452 453 Sized_relobj<size, big_endian>* const object = relinfo->object; 454 const unsigned int local_count = object->local_symbol_count(); 455 456 unsigned char* pwrite = reloc_view; 457 458 for (size_t i = 0; i < reloc_count; ++i, prelocs += reloc_size) 459 { 460 Relocatable_relocs::Reloc_strategy strategy = rr->strategy(i); 461 if (strategy == Relocatable_relocs::RELOC_DISCARD) 462 continue; 463 464 Reltype reloc(prelocs); 465 Reltype_write reloc_write(pwrite); 466 467 typename elfcpp::Elf_types<size>::Elf_WXword r_info = reloc.get_r_info(); 468 const unsigned int r_sym = elfcpp::elf_r_sym<size>(r_info); 469 const unsigned int r_type = elfcpp::elf_r_type<size>(r_info); 470 471 // Get the new symbol index. 472 473 unsigned int new_symndx; 474 if (r_sym < local_count) 475 { 476 switch (strategy) 477 { 478 case Relocatable_relocs::RELOC_COPY: 479 new_symndx = object->symtab_index(r_sym); 480 gold_assert(new_symndx != -1U); 481 break; 482 483 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: 484 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: 485 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: 486 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: 487 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: 488 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: 489 { 490 // We are adjusting a section symbol. We need to find 491 // the symbol table index of the section symbol for 492 // the output section corresponding to input section 493 // in which this symbol is defined. 494 gold_assert(r_sym < local_count); 495 bool is_ordinary; 496 unsigned int shndx = 497 object->local_symbol_input_shndx(r_sym, &is_ordinary); 498 gold_assert(is_ordinary); 499 Output_section* os = object->output_section(shndx); 500 gold_assert(os != NULL); 501 gold_assert(os->needs_symtab_index()); 502 new_symndx = os->symtab_index(); 503 } 504 break; 505 506 default: 507 gold_unreachable(); 508 } 509 } 510 else 511 { 512 const Symbol* gsym = object->global_symbol(r_sym); 513 gold_assert(gsym != NULL); 514 if (gsym->is_forwarder()) 515 gsym = relinfo->symtab->resolve_forwards(gsym); 516 517 gold_assert(gsym->has_symtab_index()); 518 new_symndx = gsym->symtab_index(); 519 } 520 521 // Get the new offset--the location in the output section where 522 // this relocation should be applied. 523 524 Address offset = reloc.get_r_offset(); 525 Address new_offset; 526 if (offset_in_output_section != -1U) 527 new_offset = offset + offset_in_output_section; 528 else 529 { 530 section_offset_type sot_offset = 531 convert_types<section_offset_type, Address>(offset); 532 section_offset_type new_sot_offset = 533 output_section->output_offset(object, relinfo->data_shndx, 534 sot_offset); 535 gold_assert(new_sot_offset != -1); 536 new_offset = new_sot_offset; 537 } 538 539 // In an object file, r_offset is an offset within the section. 540 // In an executable or dynamic object, generated by 541 // --emit-relocs, r_offset is an absolute address. 542 if (!parameters->options().relocatable()) 543 { 544 new_offset += view_address; 545 if (offset_in_output_section != -1U) 546 new_offset -= offset_in_output_section; 547 } 548 549 reloc_write.put_r_offset(new_offset); 550 reloc_write.put_r_info(elfcpp::elf_r_info<size>(new_symndx, r_type)); 551 552 // Handle the reloc addend based on the strategy. 553 554 if (strategy == Relocatable_relocs::RELOC_COPY) 555 { 556 if (sh_type == elfcpp::SHT_RELA) 557 Reloc_types<sh_type, size, big_endian>:: 558 copy_reloc_addend(&reloc_write, 559 &reloc); 560 } 561 else 562 { 563 // The relocation uses a section symbol in the input file. 564 // We are adjusting it to use a section symbol in the output 565 // file. The input section symbol refers to some address in 566 // the input section. We need the relocation in the output 567 // file to refer to that same address. This adjustment to 568 // the addend is the same calculation we use for a simple 569 // absolute relocation for the input section symbol. 570 571 const Symbol_value<size>* psymval = object->local_symbol(r_sym); 572 573 unsigned char* padd = view + offset; 574 switch (strategy) 575 { 576 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_RELA: 577 { 578 typename elfcpp::Elf_types<size>::Elf_Swxword addend; 579 addend = Reloc_types<sh_type, size, big_endian>:: 580 get_reloc_addend(&reloc); 581 addend = psymval->value(object, addend); 582 Reloc_types<sh_type, size, big_endian>:: 583 set_reloc_addend(&reloc_write, addend); 584 } 585 break; 586 587 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_0: 588 break; 589 590 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_1: 591 Relocate_functions<size, big_endian>::rel8(padd, object, 592 psymval); 593 break; 594 595 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_2: 596 Relocate_functions<size, big_endian>::rel16(padd, object, 597 psymval); 598 break; 599 600 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_4: 601 Relocate_functions<size, big_endian>::rel32(padd, object, 602 psymval); 603 break; 604 605 case Relocatable_relocs::RELOC_ADJUST_FOR_SECTION_8: 606 Relocate_functions<size, big_endian>::rel64(padd, object, 607 psymval); 608 break; 609 610 default: 611 gold_unreachable(); 612 } 613 } 614 615 pwrite += reloc_size; 616 } 617 618 gold_assert(static_cast<section_size_type>(pwrite - reloc_view) 619 == reloc_view_size); 620 } 621 622 } // End namespace gold. 623 624 #endif // !defined(GOLD_TARGET_RELOC_H) 625