1 /* ARC-specific support for 32-bit ELF 2 Copyright (C) 1994-2024 Free Software Foundation, Inc. 3 Contributed by Cupertino Miranda (cmiranda (at) synopsys.com). 4 5 This file is part of BFD, the Binary File Descriptor library. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 20 MA 02110-1301, USA. */ 21 22 #include "sysdep.h" 23 #include "bfd.h" 24 #include "libbfd.h" 25 #include "elf-bfd.h" 26 #include "elf/arc.h" 27 #include "libiberty.h" 28 #include "opcode/arc-func.h" 29 #include "opcode/arc.h" 30 #include "arc-plt.h" 31 32 #define FEATURE_LIST_NAME bfd_feature_list 33 #define CONFLICT_LIST bfd_conflict_list 34 #include "opcode/arc-attrs.h" 35 36 /* #define ARC_ENABLE_DEBUG 1 */ 37 #ifdef ARC_ENABLE_DEBUG 38 static const char * 39 name_for_global_symbol (struct elf_link_hash_entry *h) 40 { 41 static char *local_str = "(local)"; 42 if (h == NULL) 43 return local_str; 44 return h->root.root.string; 45 } 46 #define ARC_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args) 47 #else 48 #define ARC_DEBUG(...) 49 #endif 50 51 52 #define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND) \ 53 { \ 54 struct elf_link_hash_table *_htab = elf_hash_table (info); \ 55 Elf_Internal_Rela _rel; \ 56 bfd_byte * _loc; \ 57 \ 58 if (_htab->dynamic_sections_created) \ 59 { \ 60 BFD_ASSERT (_htab->srel##SECTION &&_htab->srel##SECTION->contents); \ 61 _loc = _htab->srel##SECTION->contents \ 62 + ((_htab->srel##SECTION->reloc_count) \ 63 * sizeof (Elf32_External_Rela)); \ 64 _htab->srel##SECTION->reloc_count++; \ 65 _rel.r_addend = ADDEND; \ 66 _rel.r_offset = (_htab->s##SECTION)->output_section->vma \ 67 + (_htab->s##SECTION)->output_offset + OFFSET; \ 68 BFD_ASSERT ((long) SYM_IDX != -1); \ 69 _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE); \ 70 bfd_elf32_swap_reloca_out (BFD, &_rel, _loc); \ 71 } \ 72 } 73 74 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \ 75 case VALUE: \ 76 return "R_" #TYPE; \ 77 break; 78 79 static ATTRIBUTE_UNUSED const char * 80 reloc_type_to_name (unsigned int type) 81 { 82 switch (type) 83 { 84 #include "elf/arc-reloc.def" 85 86 default: 87 return "UNKNOWN"; 88 break; 89 } 90 } 91 92 #undef ARC_RELOC_HOWTO 93 94 /* Try to minimize the amount of space occupied by relocation tables 95 on the ROM (not that the ROM won't be swamped by other ELF overhead). */ 96 97 #define USE_REL 1 98 99 /* Similar with bfd_get_32 but taking into account the 100 middle-endianess of the ARC CPUs. Only to be used in code 101 sections. */ 102 103 static bfd_vma 104 bfd_get_32_me (bfd * abfd,const unsigned char * data) 105 { 106 bfd_vma value = 0; 107 108 if (bfd_big_endian (abfd)) 109 value = bfd_get_32 (abfd, data); 110 else 111 { 112 value = ((bfd_get_8 (abfd, data) & 255) << 16); 113 value |= ((bfd_get_8 (abfd, data + 1) & 255) << 24); 114 value |= (bfd_get_8 (abfd, data + 2) & 255); 115 value |= ((bfd_get_8 (abfd, data + 3) & 255) << 8); 116 } 117 118 return value; 119 } 120 121 static void 122 bfd_put_32_me (bfd *abfd, bfd_vma value,unsigned char *data) 123 { 124 bfd_put_16 (abfd, (value & 0xffff0000) >> 16, data); 125 bfd_put_16 (abfd, value & 0xffff, data + 2); 126 } 127 128 static ATTRIBUTE_UNUSED bool 129 is_reloc_PC_relative (reloc_howto_type *howto) 130 { 131 return strstr (howto->name, "PC") != NULL; 132 } 133 134 static bool 135 is_reloc_SDA_relative (reloc_howto_type *howto) 136 { 137 return strstr (howto->name, "SDA") != NULL; 138 } 139 140 static bool 141 is_reloc_for_GOT (reloc_howto_type * howto) 142 { 143 if (strstr (howto->name, "TLS") != NULL) 144 return false; 145 return strstr (howto->name, "GOT") != NULL; 146 } 147 148 static bool 149 is_reloc_for_PLT (reloc_howto_type * howto) 150 { 151 return strstr (howto->name, "PLT") != NULL; 152 } 153 154 static bool 155 is_reloc_for_TLS (reloc_howto_type *howto) 156 { 157 return strstr (howto->name, "TLS") != NULL; 158 } 159 160 struct arc_relocation_data 161 { 162 bfd_signed_vma reloc_offset; 163 bfd_signed_vma reloc_addend; 164 bfd_signed_vma got_offset_value; 165 166 bfd_signed_vma sym_value; 167 asection *sym_section; 168 169 reloc_howto_type *howto; 170 171 asection *input_section; 172 173 bfd_signed_vma sdata_begin_symbol_vma; 174 bool sdata_begin_symbol_vma_set; 175 bfd_signed_vma got_symbol_vma; 176 177 bool should_relocate; 178 179 const char *symbol_name; 180 }; 181 182 /* ARC ELF linker hash entry. */ 183 struct elf_arc_link_hash_entry 184 { 185 struct elf_link_hash_entry root; 186 187 struct got_entry *got_ents; 188 }; 189 190 191 /* Should be included at this location due to static declarations 192 defined before this point. */ 193 #include "arc-got.h" 194 195 #define arc_bfd_get_8(A,B,C) bfd_get_8(A,B) 196 #define arc_bfd_get_16(A,B,C) bfd_get_16(A,B) 197 #define arc_bfd_get_32(A,B,C) bfd_get_32(A,B) 198 #define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C) 199 #define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C) 200 #define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C) 201 202 203 static bfd_reloc_status_type 204 arc_elf_reloc (bfd *abfd ATTRIBUTE_UNUSED, 205 arelent *reloc_entry, 206 asymbol *symbol_in, 207 void *data ATTRIBUTE_UNUSED, 208 asection *input_section, 209 bfd *output_bfd, 210 char ** error_message ATTRIBUTE_UNUSED) 211 { 212 if (output_bfd != NULL) 213 { 214 reloc_entry->address += input_section->output_offset; 215 216 /* In case of relocateable link and if the reloc is against a 217 section symbol, the addend needs to be adjusted according to 218 where the section symbol winds up in the output section. */ 219 if ((symbol_in->flags & BSF_SECTION_SYM) && symbol_in->section) 220 reloc_entry->addend += symbol_in->section->output_offset; 221 222 return bfd_reloc_ok; 223 } 224 225 return bfd_reloc_continue; 226 } 227 228 229 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \ 230 TYPE = VALUE, 231 232 enum howto_list 233 { 234 #include "elf/arc-reloc.def" 235 HOWTO_LIST_LAST 236 }; 237 238 #undef ARC_RELOC_HOWTO 239 240 #define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \ 241 [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, false, 0, \ 242 complain_overflow_##OVERFLOW, arc_elf_reloc, \ 243 "R_" #TYPE, false, 0, 0, false), 244 245 static struct reloc_howto_struct elf_arc_howto_table[] = 246 { 247 #include "elf/arc-reloc.def" 248 /* Example of what is generated by the preprocessor. Currently kept as an 249 example. 250 HOWTO (R_ARC_NONE, // Type. 251 0, // Rightshift. 252 4, // Size. 253 32, // Bitsize. 254 false, // PC_relative. 255 0, // Bitpos. 256 complain_overflow_bitfield, // Complain_on_overflow. 257 bfd_elf_generic_reloc, // Special_function. 258 "R_ARC_NONE", // Name. 259 true, // Partial_inplace. 260 0, // Src_mask. 261 0, // Dst_mask. 262 false), // PCrel_offset. 263 */ 264 }; 265 #undef ARC_RELOC_HOWTO 266 267 static void 268 arc_elf_howto_init (void) 269 { 270 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \ 271 elf_arc_howto_table[TYPE].pc_relative = \ 272 (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \ 273 elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0); \ 274 /* Only 32 bit data relocations should be marked as ME. */ \ 275 if (strstr (#FORMULA, " ME ") != NULL) \ 276 { \ 277 BFD_ASSERT (SIZE == 4); \ 278 } 279 280 #include "elf/arc-reloc.def" 281 282 } 283 #undef ARC_RELOC_HOWTO 284 285 286 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \ 287 [TYPE] = VALUE, 288 289 const int howto_table_lookup[] = 290 { 291 #include "elf/arc-reloc.def" 292 }; 293 294 #undef ARC_RELOC_HOWTO 295 296 static reloc_howto_type * 297 arc_elf_howto (unsigned int r_type) 298 { 299 if (elf_arc_howto_table[R_ARC_32].dst_mask == 0) 300 arc_elf_howto_init (); 301 return &elf_arc_howto_table[r_type]; 302 } 303 304 /* Map BFD reloc types to ARC ELF reloc types. */ 305 306 struct arc_reloc_map 307 { 308 bfd_reloc_code_real_type bfd_reloc_val; 309 unsigned char elf_reloc_val; 310 }; 311 312 /* ARC ELF linker hash table. */ 313 struct elf_arc_link_hash_table 314 { 315 struct elf_link_hash_table elf; 316 }; 317 318 static struct bfd_hash_entry * 319 elf_arc_link_hash_newfunc (struct bfd_hash_entry *entry, 320 struct bfd_hash_table *table, 321 const char *string) 322 { 323 struct elf_arc_link_hash_entry * ret = 324 (struct elf_arc_link_hash_entry *) entry; 325 326 /* Allocate the structure if it has not already been allocated by a 327 subclass. */ 328 if (ret == NULL) 329 ret = (struct elf_arc_link_hash_entry *) 330 bfd_hash_allocate (table, sizeof (struct elf_arc_link_hash_entry)); 331 if (ret == NULL) 332 return (struct bfd_hash_entry *) ret; 333 334 /* Call the allocation method of the superclass. */ 335 ret = ((struct elf_arc_link_hash_entry *) 336 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, 337 table, string)); 338 if (ret != NULL) 339 { 340 ret->got_ents = NULL; 341 } 342 343 return (struct bfd_hash_entry *) ret; 344 } 345 346 /* Destroy an ARC ELF linker hash table. */ 347 static void 348 elf_arc_link_hash_table_free (bfd *obfd) 349 { 350 _bfd_elf_link_hash_table_free (obfd); 351 } 352 353 /* Create an ARC ELF linker hash table. */ 354 355 static struct bfd_link_hash_table * 356 arc_elf_link_hash_table_create (bfd *abfd) 357 { 358 struct elf_arc_link_hash_table *ret; 359 360 ret = (struct elf_arc_link_hash_table *) bfd_zmalloc (sizeof (*ret)); 361 if (ret == NULL) 362 return NULL; 363 364 if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd, 365 elf_arc_link_hash_newfunc, 366 sizeof (struct elf_arc_link_hash_entry))) 367 { 368 free (ret); 369 return NULL; 370 } 371 372 ret->elf.root.hash_table_free = elf_arc_link_hash_table_free; 373 374 return &ret->elf.root; 375 } 376 377 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \ 378 { BFD_RELOC_##TYPE, R_##TYPE }, 379 380 static const struct arc_reloc_map arc_reloc_map[] = 381 { 382 #include "elf/arc-reloc.def" 383 384 {BFD_RELOC_NONE, R_ARC_NONE}, 385 {BFD_RELOC_8, R_ARC_8}, 386 {BFD_RELOC_16, R_ARC_16}, 387 {BFD_RELOC_24, R_ARC_24}, 388 {BFD_RELOC_32, R_ARC_32}, 389 }; 390 391 #undef ARC_RELOC_HOWTO 392 393 typedef ATTRIBUTE_UNUSED unsigned (*replace_func) (unsigned, int ATTRIBUTE_UNUSED); 394 395 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \ 396 case TYPE: \ 397 func = RELOC_FUNCTION; \ 398 break; 399 400 static replace_func 401 get_replace_function (bfd *abfd, unsigned int r_type) 402 { 403 replace_func func = NULL; 404 405 switch (r_type) 406 { 407 #include "elf/arc-reloc.def" 408 } 409 410 if (func == replace_bits24 && bfd_big_endian (abfd)) 411 func = replace_bits24_be; 412 413 return func; 414 } 415 #undef ARC_RELOC_HOWTO 416 417 static reloc_howto_type * 418 arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED, 419 bfd_reloc_code_real_type code) 420 { 421 unsigned int i; 422 423 for (i = ARRAY_SIZE (arc_reloc_map); i--;) 424 { 425 if (arc_reloc_map[i].bfd_reloc_val == code) 426 return arc_elf_howto (arc_reloc_map[i].elf_reloc_val); 427 } 428 429 return NULL; 430 } 431 432 /* Function to set the ELF flag bits. */ 433 static bool 434 arc_elf_set_private_flags (bfd *abfd, flagword flags) 435 { 436 elf_elfheader (abfd)->e_flags = flags; 437 elf_flags_init (abfd) = true; 438 return true; 439 } 440 441 /* Print private flags. */ 442 static bool 443 arc_elf_print_private_bfd_data (bfd *abfd, void * ptr) 444 { 445 FILE *file = (FILE *) ptr; 446 flagword flags; 447 448 BFD_ASSERT (abfd != NULL && ptr != NULL); 449 450 /* Print normal ELF private data. */ 451 _bfd_elf_print_private_bfd_data (abfd, ptr); 452 453 flags = elf_elfheader (abfd)->e_flags; 454 fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags); 455 456 switch (flags & EF_ARC_MACH_MSK) 457 { 458 case EF_ARC_CPU_ARCV2HS : fprintf (file, " -mcpu=ARCv2HS"); break; 459 case EF_ARC_CPU_ARCV2EM : fprintf (file, " -mcpu=ARCv2EM"); break; 460 case E_ARC_MACH_ARC600 : fprintf (file, " -mcpu=ARC600"); break; 461 case E_ARC_MACH_ARC601 : fprintf (file, " -mcpu=ARC601"); break; 462 case E_ARC_MACH_ARC700 : fprintf (file, " -mcpu=ARC700"); break; 463 default: 464 fprintf (file, "-mcpu=unknown"); 465 break; 466 } 467 468 switch (flags & EF_ARC_OSABI_MSK) 469 { 470 case E_ARC_OSABI_ORIG : fprintf (file, " (ABI:legacy)"); break; 471 case E_ARC_OSABI_V2 : fprintf (file, " (ABI:v2)"); break; 472 case E_ARC_OSABI_V3 : fprintf (file, " (ABI:v3)"); break; 473 case E_ARC_OSABI_V4 : fprintf (file, " (ABI:v4)"); break; 474 default: 475 fprintf (file, " (ABI:unknown)"); 476 break; 477 } 478 479 fputc ('\n', file); 480 return true; 481 } 482 483 /* Copy backend specific data from one object module to another. */ 484 485 static bool 486 arc_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) 487 { 488 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 489 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 490 return true; 491 492 BFD_ASSERT (!elf_flags_init (obfd) 493 || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags); 494 495 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; 496 elf_flags_init (obfd) = true; 497 498 /* Copy object attributes. */ 499 _bfd_elf_copy_obj_attributes (ibfd, obfd); 500 501 return _bfd_elf_copy_private_bfd_data (ibfd, obfd); 502 } 503 504 static reloc_howto_type * 505 bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, 506 const char *r_name) 507 { 508 unsigned int i; 509 510 for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++) 511 if (elf_arc_howto_table[i].name != NULL 512 && strcasecmp (elf_arc_howto_table[i].name, r_name) == 0) 513 return arc_elf_howto (i); 514 515 return NULL; 516 } 517 518 /* Set the howto pointer for an ARC ELF reloc. */ 519 520 static bool 521 arc_info_to_howto_rel (bfd * abfd, 522 arelent * cache_ptr, 523 Elf_Internal_Rela * dst) 524 { 525 unsigned int r_type; 526 527 r_type = ELF32_R_TYPE (dst->r_info); 528 if (r_type >= (unsigned int) R_ARC_max) 529 { 530 /* xgettext:c-format */ 531 _bfd_error_handler (_("%pB: unsupported relocation type %#x"), 532 abfd, r_type); 533 bfd_set_error (bfd_error_bad_value); 534 return false; 535 } 536 537 cache_ptr->howto = arc_elf_howto (r_type); 538 return true; 539 } 540 541 /* Extract CPU features from an NTBS. */ 542 543 static unsigned 544 arc_extract_features (const char *p) 545 { 546 unsigned i, r = 0; 547 548 if (!p) 549 return 0; 550 551 for (i = 0; i < ARRAY_SIZE (bfd_feature_list); i++) 552 { 553 char *t = strstr (p, bfd_feature_list[i].attr); 554 unsigned l = strlen (bfd_feature_list[i].attr); 555 if ((t != NULL) 556 && (t[l] == ',' 557 || t[l] == '\0')) 558 r |= bfd_feature_list[i].feature; 559 } 560 561 return r; 562 } 563 564 /* Concatenate two strings. s1 can be NULL but not 565 s2. */ 566 567 static char * 568 arc_stralloc (char * s1, const char * s2) 569 { 570 char *p; 571 572 /* Only s1 can be null. */ 573 BFD_ASSERT (s2); 574 575 p = s1 ? concat (s1, ",", s2, NULL) : (char *)s2; 576 577 return p; 578 } 579 580 /* Merge ARC object attributes from IBFD into OBFD. Raise an error if 581 there are conflicting attributes. */ 582 583 static bool 584 arc_elf_merge_attributes (bfd *ibfd, struct bfd_link_info *info) 585 { 586 bfd *obfd = info->output_bfd; 587 obj_attribute *in_attr; 588 obj_attribute *out_attr; 589 int i; 590 bool result = true; 591 const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section; 592 char *tagname = NULL; 593 594 /* Skip the linker stubs file. This preserves previous behavior 595 of accepting unknown attributes in the first input file - but 596 is that a bug? */ 597 if (ibfd->flags & BFD_LINKER_CREATED) 598 return true; 599 600 /* Skip any input that hasn't attribute section. 601 This enables to link object files without attribute section with 602 any others. */ 603 if (bfd_get_section_by_name (ibfd, sec_name) == NULL) 604 return true; 605 606 if (!elf_known_obj_attributes_proc (obfd)[0].i) 607 { 608 /* This is the first object. Copy the attributes. */ 609 _bfd_elf_copy_obj_attributes (ibfd, obfd); 610 611 out_attr = elf_known_obj_attributes_proc (obfd); 612 613 /* Use the Tag_null value to indicate the attributes have been 614 initialized. */ 615 out_attr[0].i = 1; 616 617 return true; 618 } 619 620 in_attr = elf_known_obj_attributes_proc (ibfd); 621 out_attr = elf_known_obj_attributes_proc (obfd); 622 623 for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++) 624 { 625 /* Merge this attribute with existing attributes. */ 626 switch (i) 627 { 628 case Tag_ARC_PCS_config: 629 if (out_attr[i].i == 0) 630 out_attr[i].i = in_attr[i].i; 631 else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i) 632 { 633 const char *tagval[] = { "Absent", "Bare-metal/mwdt", 634 "Bare-metal/newlib", "Linux/uclibc", 635 "Linux/glibc" }; 636 BFD_ASSERT (in_attr[i].i < 5); 637 BFD_ASSERT (out_attr[i].i < 5); 638 /* It's sometimes ok to mix different configs, so this is only 639 a warning. */ 640 _bfd_error_handler 641 (_("warning: %pB: conflicting platform configuration " 642 "%s with %s"), ibfd, 643 tagval[in_attr[i].i], 644 tagval[out_attr[i].i]); 645 } 646 break; 647 648 case Tag_ARC_CPU_base: 649 if (out_attr[i].i == 0) 650 out_attr[i].i = in_attr[i].i; 651 else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i 652 && ((out_attr[i].i + in_attr[i].i) < 6)) 653 { 654 const char *tagval[] = { "Absent", "ARC6xx", "ARC7xx", 655 "ARCEM", "ARCHS" }; 656 BFD_ASSERT (in_attr[i].i < 5); 657 BFD_ASSERT (out_attr[i].i < 5); 658 /* We cannot mix code for different CPUs. */ 659 _bfd_error_handler 660 (_("error: %pB: unable to merge CPU base attributes " 661 "%s with %s"), 662 obfd, 663 tagval[in_attr[i].i], 664 tagval[out_attr[i].i]); 665 result = false; 666 break; 667 } 668 else 669 { 670 /* The CPUs may be different, check if we can still mix 671 the objects against the output choosen CPU. */ 672 unsigned in_feature = 0; 673 unsigned out_feature = 0; 674 char *p1 = in_attr[Tag_ARC_ISA_config].s; 675 char *p2 = out_attr[Tag_ARC_ISA_config].s; 676 unsigned j; 677 unsigned cpu_out; 678 unsigned opcode_map[] = {0, ARC_OPCODE_ARC600, ARC_OPCODE_ARC700, 679 ARC_OPCODE_ARCv2EM, ARC_OPCODE_ARCv2HS}; 680 681 BFD_ASSERT (in_attr[i].i < (sizeof (opcode_map) 682 / sizeof (unsigned))); 683 BFD_ASSERT (out_attr[i].i < (sizeof (opcode_map) 684 / sizeof (unsigned))); 685 cpu_out = opcode_map[out_attr[i].i]; 686 687 in_feature = arc_extract_features (p1); 688 out_feature = arc_extract_features (p2); 689 690 /* First, check if a feature is compatible with the 691 output object chosen CPU. */ 692 for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++) 693 if (((in_feature | out_feature) & bfd_feature_list[j].feature) 694 && (!(cpu_out & bfd_feature_list[j].cpus))) 695 { 696 _bfd_error_handler 697 (_("error: %pB: unable to merge ISA extension attributes " 698 "%s"), 699 obfd, bfd_feature_list[j].name); 700 result = false; 701 break; 702 } 703 /* Second, if we have compatible features with the 704 chosen CPU, check if they are compatible among 705 them. */ 706 for (j = 0; j < ARRAY_SIZE (bfd_conflict_list); j++) 707 if (((in_feature | out_feature) & bfd_conflict_list[j]) 708 == bfd_conflict_list[j]) 709 { 710 unsigned k; 711 for (k = 0; k < ARRAY_SIZE (bfd_feature_list); k++) 712 { 713 if (in_feature & bfd_feature_list[k].feature 714 & bfd_conflict_list[j]) 715 p1 = (char *) bfd_feature_list[k].name; 716 if (out_feature & bfd_feature_list[k].feature 717 & bfd_conflict_list[j]) 718 p2 = (char *) bfd_feature_list[k].name; 719 } 720 _bfd_error_handler 721 (_("error: %pB: conflicting ISA extension attributes " 722 "%s with %s"), 723 obfd, p1, p2); 724 result = false; 725 break; 726 } 727 /* Everithing is alright. */ 728 out_feature |= in_feature; 729 p1 = NULL; 730 for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++) 731 if (out_feature & bfd_feature_list[j].feature) 732 p1 = arc_stralloc (p1, bfd_feature_list[j].attr); 733 if (p1) 734 out_attr[Tag_ARC_ISA_config].s = 735 _bfd_elf_attr_strdup (obfd, p1); 736 } 737 /* Fall through. */ 738 case Tag_ARC_CPU_variation: 739 case Tag_ARC_ISA_mpy_option: 740 case Tag_ARC_ABI_osver: 741 /* Use the largest value specified. */ 742 if (in_attr[i].i > out_attr[i].i) 743 out_attr[i].i = in_attr[i].i; 744 break; 745 746 /* The CPU name is given by the vendor, just choose an 747 existing one if missing or different. There are no fail 748 criteria if they different or both missing. */ 749 case Tag_ARC_CPU_name: 750 if (!out_attr[i].s && in_attr[i].s) 751 out_attr[i].s = _bfd_elf_attr_strdup (obfd, in_attr[i].s); 752 break; 753 754 case Tag_ARC_ABI_rf16: 755 if (out_attr[i].i == 0) 756 out_attr[i].i = in_attr[i].i; 757 else if (out_attr[i].i != in_attr[i].i) 758 { 759 /* We cannot mix code with rf16 and without. */ 760 _bfd_error_handler 761 (_("error: %pB: cannot mix rf16 with full register set %pB"), 762 obfd, ibfd); 763 result = false; 764 } 765 break; 766 767 case Tag_ARC_ABI_pic: 768 tagname = "PIC"; 769 /* fall through */ 770 case Tag_ARC_ABI_sda: 771 if (!tagname) 772 tagname = "SDA"; 773 /* fall through */ 774 case Tag_ARC_ABI_tls: 775 { 776 const char *tagval[] = { "Absent", "MWDT", "GNU" }; 777 778 if (!tagname) 779 tagname = "TLS"; 780 781 BFD_ASSERT (in_attr[i].i < 3); 782 BFD_ASSERT (out_attr[i].i < 3); 783 if (out_attr[i].i == 0) 784 out_attr[i].i = in_attr[i].i; 785 else if (out_attr[i].i != 0 && in_attr[i].i != 0 786 && out_attr[i].i != in_attr[i].i) 787 { 788 _bfd_error_handler 789 (_("error: %pB: conflicting attributes %s: %s with %s"), 790 obfd, tagname, 791 tagval[in_attr[i].i], 792 tagval[out_attr[i].i]); 793 result = false; 794 } 795 tagname = NULL; 796 break; 797 } 798 799 case Tag_ARC_ABI_double_size: 800 tagname = "Double size"; 801 /* fall through */ 802 case Tag_ARC_ABI_enumsize: 803 if (!tagname) 804 tagname = "Enum size"; 805 /* fall through */ 806 case Tag_ARC_ABI_exceptions: 807 if (!tagname) 808 tagname = "ABI exceptions"; 809 810 if (out_attr[i].i == 0) 811 out_attr[i].i = in_attr[i].i; 812 else if (out_attr[i].i != 0 && in_attr[i].i != 0 813 && out_attr[i].i != in_attr[i].i) 814 { 815 _bfd_error_handler 816 (_("error: %pB: conflicting attributes %s"), 817 obfd, tagname); 818 result = false; 819 } 820 break; 821 822 case Tag_ARC_ISA_apex: 823 break; /* Do nothing for APEX attributes. */ 824 825 case Tag_ARC_ISA_config: 826 /* It is handled in Tag_ARC_CPU_base. */ 827 break; 828 829 case Tag_ARC_ATR_version: 830 if (out_attr[i].i == 0) 831 out_attr[i].i = in_attr[i].i; 832 break; 833 834 default: 835 result 836 = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i); 837 } 838 839 /* If out_attr was copied from in_attr then it won't have a type yet. */ 840 if (in_attr[i].type && !out_attr[i].type) 841 out_attr[i].type = in_attr[i].type; 842 } 843 844 /* Merge Tag_compatibility attributes and any common GNU ones. */ 845 if (!_bfd_elf_merge_object_attributes (ibfd, info)) 846 return false; 847 848 /* Check for any attributes not known on ARC. */ 849 result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd); 850 851 return result; 852 } 853 854 /* Merge backend specific data from an object file to the output 855 object file when linking. */ 856 857 static bool 858 arc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info) 859 { 860 bfd *obfd = info->output_bfd; 861 unsigned short mach_ibfd; 862 static unsigned short mach_obfd = EM_NONE; 863 flagword out_flags; 864 flagword in_flags; 865 asection *sec; 866 867 /* Check if we have the same endianess. */ 868 if (! _bfd_generic_verify_endian_match (ibfd, info)) 869 return false; 870 871 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 872 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 873 return true; 874 875 /* Collect ELF flags. */ 876 in_flags = elf_elfheader (ibfd)->e_flags & EF_ARC_MACH_MSK; 877 out_flags = elf_elfheader (obfd)->e_flags & EF_ARC_MACH_MSK; 878 879 if (!elf_flags_init (obfd)) /* First call, no flags set. */ 880 { 881 elf_flags_init (obfd) = true; 882 out_flags = in_flags; 883 } 884 885 if (!arc_elf_merge_attributes (ibfd, info)) 886 return false; 887 888 /* Check to see if the input BFD actually contains any sections. Do 889 not short-circuit dynamic objects; their section list may be 890 emptied by elf_link_add_object_symbols. */ 891 if (!(ibfd->flags & DYNAMIC)) 892 { 893 bool null_input_bfd = true; 894 bool only_data_sections = true; 895 896 for (sec = ibfd->sections; sec != NULL; sec = sec->next) 897 { 898 if ((bfd_section_flags (sec) 899 & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS)) 900 == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS)) 901 only_data_sections = false; 902 903 null_input_bfd = false; 904 } 905 906 if (null_input_bfd || only_data_sections) 907 return true; 908 } 909 910 /* Complain about various flag/architecture mismatches. */ 911 mach_ibfd = elf_elfheader (ibfd)->e_machine; 912 if (mach_obfd == EM_NONE) 913 { 914 mach_obfd = mach_ibfd; 915 } 916 else 917 { 918 if (mach_ibfd != mach_obfd) 919 { 920 /* xgettext:c-format */ 921 _bfd_error_handler (_("error: attempting to link %pB " 922 "with a binary %pB of different architecture"), 923 ibfd, obfd); 924 return false; 925 } 926 else if ((in_flags != out_flags) 927 /* If we have object attributes, then we already 928 checked the objects compatibility, skip it. */ 929 && !bfd_elf_get_obj_attr_int (ibfd, OBJ_ATTR_PROC, 930 Tag_ARC_CPU_base)) 931 { 932 if (in_flags && out_flags) 933 { 934 /* Warn if different flags. */ 935 _bfd_error_handler 936 /* xgettext:c-format */ 937 (_("%pB: uses different e_flags (%#x) fields than " 938 "previous modules (%#x)"), 939 ibfd, in_flags, out_flags); 940 return false; 941 } 942 /* MWDT doesnt set the eflags hence make sure we choose the 943 eflags set by gcc. */ 944 in_flags = in_flags > out_flags ? in_flags : out_flags; 945 } 946 else 947 { 948 /* Everything is correct; don't change the output flags. */ 949 in_flags = out_flags; 950 } 951 } 952 953 /* Update the flags. */ 954 elf_elfheader (obfd)->e_flags = in_flags; 955 956 if (bfd_get_mach (obfd) < bfd_get_mach (ibfd)) 957 { 958 return bfd_set_arch_mach (obfd, bfd_arch_arc, bfd_get_mach (ibfd)); 959 } 960 961 return true; 962 } 963 964 /* Return a best guess for the machine number based on the attributes. */ 965 966 static unsigned int 967 bfd_arc_get_mach_from_attributes (bfd * abfd) 968 { 969 int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ARC_CPU_base); 970 unsigned e_machine = elf_elfheader (abfd)->e_machine; 971 972 switch (arch) 973 { 974 case TAG_CPU_ARC6xx: 975 return bfd_mach_arc_arc600; 976 case TAG_CPU_ARC7xx: 977 return bfd_mach_arc_arc700; 978 case TAG_CPU_ARCEM: 979 case TAG_CPU_ARCHS: 980 return bfd_mach_arc_arcv2; 981 default: 982 break; 983 } 984 return (e_machine == EM_ARC_COMPACT) 985 ? bfd_mach_arc_arc700 : bfd_mach_arc_arcv2; 986 } 987 988 /* Set the right machine number for an ARC ELF file. */ 989 static bool 990 arc_elf_object_p (bfd * abfd) 991 { 992 /* Make sure this is initialised, or you'll have the potential of passing 993 garbage---or misleading values---into the call to 994 bfd_default_set_arch_mach (). */ 995 unsigned int mach = bfd_mach_arc_arc700; 996 unsigned long arch = elf_elfheader (abfd)->e_flags & EF_ARC_MACH_MSK; 997 unsigned e_machine = elf_elfheader (abfd)->e_machine; 998 999 if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2) 1000 { 1001 switch (arch) 1002 { 1003 case E_ARC_MACH_ARC600: 1004 mach = bfd_mach_arc_arc600; 1005 break; 1006 case E_ARC_MACH_ARC601: 1007 mach = bfd_mach_arc_arc601; 1008 break; 1009 case E_ARC_MACH_ARC700: 1010 mach = bfd_mach_arc_arc700; 1011 break; 1012 case EF_ARC_CPU_ARCV2HS: 1013 case EF_ARC_CPU_ARCV2EM: 1014 mach = bfd_mach_arc_arcv2; 1015 break; 1016 default: 1017 mach = bfd_arc_get_mach_from_attributes (abfd); 1018 break; 1019 } 1020 } 1021 else 1022 { 1023 if (e_machine == EM_ARC) 1024 { 1025 _bfd_error_handler 1026 (_("error: the ARC4 architecture is no longer supported")); 1027 return false; 1028 } 1029 else 1030 { 1031 _bfd_error_handler 1032 (_("warning: unset or old architecture flags; " 1033 "use default machine")); 1034 } 1035 } 1036 1037 return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach); 1038 } 1039 1040 /* The final processing done just before writing out an ARC ELF object file. 1041 This gets the ARC architecture right based on the machine number. */ 1042 1043 static bool 1044 arc_elf_final_write_processing (bfd *abfd) 1045 { 1046 unsigned long emf; 1047 1048 switch (bfd_get_mach (abfd)) 1049 { 1050 case bfd_mach_arc_arcv2: 1051 emf = EM_ARC_COMPACT2; 1052 break; 1053 default: 1054 emf = EM_ARC_COMPACT; 1055 break; 1056 } 1057 1058 elf_elfheader (abfd)->e_machine = emf; 1059 1060 /* Record whatever is the current syscall ABI version. */ 1061 int osver = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, 1062 Tag_ARC_ABI_osver); 1063 flagword e_flags = elf_elfheader (abfd)->e_flags; 1064 if (osver) 1065 e_flags = (e_flags & ~EF_ARC_OSABI_MSK) | ((osver & 0x0f) << 8); 1066 else if ((e_flags & EF_ARC_OSABI_MSK) == 0) 1067 e_flags |= E_ARC_OSABI_V3; 1068 1069 elf_elfheader (abfd)->e_flags = e_flags; 1070 return _bfd_elf_final_write_processing (abfd); 1071 } 1072 1073 #ifdef ARC_ENABLE_DEBUG 1074 #define DEBUG_ARC_RELOC(A) debug_arc_reloc (A) 1075 1076 static void 1077 debug_arc_reloc (struct arc_relocation_data reloc_data) 1078 { 1079 ARC_DEBUG ("Reloc type=%s, should_relocate = %s\n", 1080 reloc_data.howto->name, 1081 reloc_data.should_relocate ? "true" : "false"); 1082 ARC_DEBUG (" offset = 0x%x, addend = 0x%x\n", 1083 (unsigned int) reloc_data.reloc_offset, 1084 (unsigned int) reloc_data.reloc_addend); 1085 ARC_DEBUG (" Symbol:\n"); 1086 ARC_DEBUG (" value = 0x%08x\n", 1087 (unsigned int) reloc_data.sym_value); 1088 if (reloc_data.sym_section != NULL) 1089 { 1090 ARC_DEBUG (" Symbol Section:\n"); 1091 ARC_DEBUG (" section name = %s, output_offset 0x%08x", 1092 reloc_data.sym_section->name, 1093 (unsigned int) reloc_data.sym_section->output_offset); 1094 if (reloc_data.sym_section->output_section != NULL) 1095 ARC_DEBUG (", output_section->vma = 0x%08x", 1096 ((unsigned int) reloc_data.sym_section->output_section->vma)); 1097 ARC_DEBUG ("\n"); 1098 if (reloc_data.sym_section->owner 1099 && reloc_data.sym_section->owner->filename) 1100 ARC_DEBUG (" file: %s\n", reloc_data.sym_section->owner->filename); 1101 } 1102 else 1103 { 1104 ARC_DEBUG (" symbol section is NULL\n"); 1105 } 1106 1107 ARC_DEBUG (" Input_section:\n"); 1108 if (reloc_data.input_section != NULL) 1109 { 1110 ARC_DEBUG (" section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n", 1111 reloc_data.input_section->name, 1112 (unsigned int) reloc_data.input_section->output_offset, 1113 (unsigned int) reloc_data.input_section->output_section->vma); 1114 ARC_DEBUG (" changed_address = 0x%08x\n", 1115 (unsigned int) (reloc_data.input_section->output_section->vma 1116 + reloc_data.input_section->output_offset 1117 + reloc_data.reloc_offset)); 1118 ARC_DEBUG (" file: %s\n", reloc_data.input_section->owner->filename); 1119 } 1120 else 1121 { 1122 ARC_DEBUG (" input section is NULL\n"); 1123 } 1124 } 1125 #else 1126 #define DEBUG_ARC_RELOC(A) 1127 #endif /* ARC_ENABLE_DEBUG */ 1128 1129 static bfd_vma 1130 middle_endian_convert (bfd_vma insn, bool do_it) 1131 { 1132 if (do_it) 1133 { 1134 insn 1135 = ((insn & 0xffff0000) >> 16) 1136 | ((insn & 0xffff) << 16); 1137 } 1138 return insn; 1139 } 1140 1141 /* This function is called for relocations that are otherwise marked as NOT 1142 requiring overflow checks. In here we perform non-standard checks of 1143 the relocation value. */ 1144 1145 static inline bfd_reloc_status_type 1146 arc_special_overflow_checks (const struct arc_relocation_data reloc_data, 1147 bfd_signed_vma relocation, 1148 struct bfd_link_info *info ATTRIBUTE_UNUSED) 1149 { 1150 switch (reloc_data.howto->type) 1151 { 1152 case R_ARC_NPS_CMEM16: 1153 if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE) 1154 { 1155 if (reloc_data.reloc_addend == 0) 1156 _bfd_error_handler 1157 /* xgettext:c-format */ 1158 (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s' is invalid, " 1159 "16 MSB should be %#x (value is %#" PRIx64 ")"), 1160 reloc_data.input_section->owner, 1161 reloc_data.input_section, 1162 (uint64_t) reloc_data.reloc_offset, 1163 reloc_data.symbol_name, 1164 NPS_CMEM_HIGH_VALUE, 1165 (uint64_t) relocation); 1166 else 1167 _bfd_error_handler 1168 /* xgettext:c-format */ 1169 (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s+%#" PRIx64 1170 "' is invalid, 16 MSB should be %#x (value is %#" PRIx64 ")"), 1171 reloc_data.input_section->owner, 1172 reloc_data.input_section, 1173 (uint64_t) reloc_data.reloc_offset, 1174 reloc_data.symbol_name, 1175 (uint64_t) reloc_data.reloc_addend, 1176 NPS_CMEM_HIGH_VALUE, 1177 (uint64_t) relocation); 1178 return bfd_reloc_overflow; 1179 } 1180 break; 1181 1182 default: 1183 break; 1184 } 1185 1186 return bfd_reloc_ok; 1187 } 1188 1189 #define ME(reloc) (reloc) 1190 1191 #define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \ 1192 && (!bfd_big_endian (BFD))) 1193 1194 #define S ((bfd_signed_vma) (reloc_data.sym_value \ 1195 + (reloc_data.sym_section->output_section != NULL ? \ 1196 (reloc_data.sym_section->output_offset \ 1197 + reloc_data.sym_section->output_section->vma) : 0))) 1198 #define L ((bfd_signed_vma) (reloc_data.sym_value \ 1199 + (reloc_data.sym_section->output_section != NULL ? \ 1200 (reloc_data.sym_section->output_offset \ 1201 + reloc_data.sym_section->output_section->vma) : 0))) 1202 #define A (reloc_data.reloc_addend) 1203 #define B (0) 1204 #define G (reloc_data.got_offset_value) 1205 #define GOT (reloc_data.got_symbol_vma) 1206 #define GOT_BEGIN (htab->sgot->output_section->vma) 1207 1208 #define MES (0) 1209 /* P: relative offset to PCL The offset should be to the 1210 current location aligned to 32 bits. */ 1211 #define P ((bfd_signed_vma) ( \ 1212 ( \ 1213 (reloc_data.input_section->output_section != NULL ? \ 1214 reloc_data.input_section->output_section->vma : 0) \ 1215 + reloc_data.input_section->output_offset \ 1216 + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0))) \ 1217 & ~0x3)) 1218 #define PDATA ((bfd_signed_vma) ( \ 1219 (reloc_data.input_section->output_section->vma \ 1220 + reloc_data.input_section->output_offset \ 1221 + (reloc_data.reloc_offset)))) 1222 #define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \ 1223 + reloc_data.sym_section->output_offset) 1224 #define FINAL_SECTSTART \ 1225 (bfd_signed_vma) (reloc_data.sym_section->output_section->vma) 1226 #define JLI (bfd_signed_vma) (reloc_data.sym_section->output_section->vma) 1227 #define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma) 1228 #define TLS_REL (bfd_signed_vma)(tls_sec->output_section->vma) 1229 #define TLS_TBSS (align_power (TCB_SIZE, tls_sec->alignment_power)) 1230 1231 #define none (0) 1232 1233 #ifdef ARC_ENABLE_DEBUG 1234 #define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \ 1235 do \ 1236 { \ 1237 asection *sym_section = reloc_data.sym_section; \ 1238 asection *input_section = reloc_data.input_section; \ 1239 ARC_DEBUG ("RELOC_TYPE = " TYPE "\n"); \ 1240 ARC_DEBUG ("FORMULA = " FORMULA "\n"); \ 1241 ARC_DEBUG ("S = %#lx\n", S); \ 1242 ARC_DEBUG ("A = %#lx\n", A); \ 1243 ARC_DEBUG ("L = %lx\n", L); \ 1244 if (sym_section->output_section != NULL) \ 1245 ARC_DEBUG ("symbol_section->vma = %#lx\n", \ 1246 sym_section->output_section->vma \ 1247 + sym_section->output_offset); \ 1248 else \ 1249 ARC_DEBUG ("symbol_section->vma = NULL\n"); \ 1250 if (input_section->output_section != NULL) \ 1251 ARC_DEBUG ("input_section->vma = %#lx\n", \ 1252 input_section->output_section->vma \ 1253 + input_section->output_offset); \ 1254 else \ 1255 ARC_DEBUG ("input_section->vma = NULL\n"); \ 1256 ARC_DEBUG ("PCL = %#lx\n", P); \ 1257 ARC_DEBUG ("P = %#lx\n", P); \ 1258 ARC_DEBUG ("G = %#lx\n", G); \ 1259 ARC_DEBUG ("SDA_OFFSET = %#lx\n", _SDA_BASE_); \ 1260 ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \ 1261 ARC_DEBUG ("GOT_OFFSET = %#lx\n", GOT); \ 1262 ARC_DEBUG ("relocation = %#08lx\n", relocation); \ 1263 ARC_DEBUG ("before = %#08x\n", (unsigned) insn); \ 1264 ARC_DEBUG ("data = %08x (%u) (%d)\n", (unsigned) relocation, \ 1265 (unsigned) relocation, (int) relocation); \ 1266 } \ 1267 while (0) 1268 1269 #define PRINT_DEBUG_RELOC_INFO_AFTER \ 1270 do \ 1271 { \ 1272 ARC_DEBUG ("after = 0x%08x\n", (unsigned int) insn); \ 1273 } \ 1274 while (0) 1275 1276 #else 1277 1278 #define PRINT_DEBUG_RELOC_INFO_BEFORE(...) 1279 #define PRINT_DEBUG_RELOC_INFO_AFTER 1280 1281 #endif /* ARC_ENABLE_DEBUG */ 1282 1283 #define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \ 1284 case R_##TYPE: \ 1285 { \ 1286 bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \ 1287 relocation = FORMULA ; \ 1288 PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE); \ 1289 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \ 1290 insn = (* get_replace_function (abfd, TYPE)) (insn, relocation); \ 1291 insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \ 1292 PRINT_DEBUG_RELOC_INFO_AFTER; \ 1293 } \ 1294 break; 1295 1296 static bfd_reloc_status_type 1297 arc_do_relocation (bfd_byte * contents, 1298 struct arc_relocation_data reloc_data, 1299 struct bfd_link_info *info) 1300 { 1301 bfd_signed_vma relocation = 0; 1302 bfd_vma insn; 1303 bfd_vma orig_insn ATTRIBUTE_UNUSED; 1304 bfd * abfd = reloc_data.input_section->owner; 1305 struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info); 1306 bfd_reloc_status_type flag; 1307 asection *tls_sec = htab->tls_sec; 1308 1309 if (!reloc_data.should_relocate) 1310 return bfd_reloc_ok; 1311 1312 switch (bfd_get_reloc_size (reloc_data.howto)) 1313 { 1314 case 4: 1315 insn = arc_bfd_get_32 (abfd, 1316 contents + reloc_data.reloc_offset, 1317 reloc_data.input_section); 1318 break; 1319 case 2: 1320 insn = arc_bfd_get_16 (abfd, 1321 contents + reloc_data.reloc_offset, 1322 reloc_data.input_section); 1323 break; 1324 case 1: 1325 insn = arc_bfd_get_8 (abfd, 1326 contents + reloc_data.reloc_offset, 1327 reloc_data.input_section); 1328 break; 1329 default: 1330 insn = 0; 1331 BFD_ASSERT (0); 1332 break; 1333 } 1334 1335 orig_insn = insn; 1336 1337 /* If we resolve a TLS relocation, make sure we do have a valid TLS 1338 section. */ 1339 switch (reloc_data.howto->type) 1340 { 1341 case R_ARC_TLS_LE_32: 1342 if (tls_sec == NULL) 1343 return bfd_reloc_notsupported; 1344 break; 1345 1346 default: 1347 break; 1348 } 1349 1350 1351 switch (reloc_data.howto->type) 1352 { 1353 #include "elf/arc-reloc.def" 1354 1355 default: 1356 BFD_ASSERT (0); 1357 break; 1358 } 1359 1360 /* Check for relocation overflow. */ 1361 if (reloc_data.howto->complain_on_overflow != complain_overflow_dont) 1362 flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow, 1363 reloc_data.howto->bitsize, 1364 reloc_data.howto->rightshift, 1365 bfd_arch_bits_per_address (abfd), 1366 relocation); 1367 else 1368 flag = arc_special_overflow_checks (reloc_data, relocation, info); 1369 1370 if (flag != bfd_reloc_ok) 1371 { 1372 ARC_DEBUG ("Relocation overflows !\n"); 1373 DEBUG_ARC_RELOC (reloc_data); 1374 ARC_DEBUG ("Relocation value = signed -> %d, unsigned -> %u" 1375 ", hex -> (0x%08x)\n", 1376 (int) relocation, (unsigned) relocation, (int) relocation); 1377 1378 return flag; 1379 } 1380 1381 /* Write updated instruction back to memory. */ 1382 switch (bfd_get_reloc_size (reloc_data.howto)) 1383 { 1384 case 4: 1385 arc_bfd_put_32 (abfd, insn, 1386 contents + reloc_data.reloc_offset, 1387 reloc_data.input_section); 1388 break; 1389 case 2: 1390 arc_bfd_put_16 (abfd, insn, 1391 contents + reloc_data.reloc_offset, 1392 reloc_data.input_section); 1393 break; 1394 case 1: 1395 arc_bfd_put_8 (abfd, insn, 1396 contents + reloc_data.reloc_offset, 1397 reloc_data.input_section); 1398 break; 1399 default: 1400 ARC_DEBUG ("size = %d\n", reloc_data.howto->size); 1401 BFD_ASSERT (0); 1402 break; 1403 } 1404 1405 return bfd_reloc_ok; 1406 } 1407 #undef S 1408 #undef A 1409 #undef B 1410 #undef G 1411 #undef GOT 1412 #undef L 1413 #undef MES 1414 #undef P 1415 #undef SECTSTAR 1416 #undef SECTSTART 1417 #undef JLI 1418 #undef _SDA_BASE_ 1419 #undef none 1420 1421 #undef ARC_RELOC_HOWTO 1422 1423 1424 /* Relocate an arc ELF section. 1425 Function : elf_arc_relocate_section 1426 Brief : Relocate an arc section, by handling all the relocations 1427 appearing in that section. 1428 Args : output_bfd : The bfd being written to. 1429 info : Link information. 1430 input_bfd : The input bfd. 1431 input_section : The section being relocated. 1432 contents : contents of the section being relocated. 1433 relocs : List of relocations in the section. 1434 local_syms : is a pointer to the swapped in local symbols. 1435 local_section : is an array giving the section in the input file 1436 corresponding to the st_shndx field of each 1437 local symbol. */ 1438 static int 1439 elf_arc_relocate_section (bfd * output_bfd, 1440 struct bfd_link_info * info, 1441 bfd * input_bfd, 1442 asection * input_section, 1443 bfd_byte * contents, 1444 Elf_Internal_Rela * relocs, 1445 Elf_Internal_Sym * local_syms, 1446 asection ** local_sections) 1447 { 1448 Elf_Internal_Shdr * symtab_hdr; 1449 struct elf_link_hash_entry ** sym_hashes; 1450 Elf_Internal_Rela * rel; 1451 Elf_Internal_Rela * wrel; 1452 Elf_Internal_Rela * relend; 1453 struct elf_link_hash_table * htab = elf_hash_table (info); 1454 1455 symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr); 1456 sym_hashes = elf_sym_hashes (input_bfd); 1457 1458 rel = wrel = relocs; 1459 relend = relocs + input_section->reloc_count; 1460 for (; rel < relend; wrel++, rel++) 1461 { 1462 enum elf_arc_reloc_type r_type; 1463 reloc_howto_type *howto; 1464 unsigned long r_symndx; 1465 struct elf_link_hash_entry *h; 1466 Elf_Internal_Sym *sym; 1467 asection *sec; 1468 struct elf_link_hash_entry *h2; 1469 const char *msg; 1470 bool unresolved_reloc = false; 1471 1472 struct arc_relocation_data reloc_data = 1473 { 1474 .reloc_offset = 0, 1475 .reloc_addend = 0, 1476 .got_offset_value = 0, 1477 .sym_value = 0, 1478 .sym_section = NULL, 1479 .howto = NULL, 1480 .input_section = NULL, 1481 .sdata_begin_symbol_vma = 0, 1482 .sdata_begin_symbol_vma_set = false, 1483 .got_symbol_vma = 0, 1484 .should_relocate = false 1485 }; 1486 1487 r_type = ELF32_R_TYPE (rel->r_info); 1488 1489 if (r_type >= (int) R_ARC_max) 1490 { 1491 bfd_set_error (bfd_error_bad_value); 1492 return false; 1493 } 1494 howto = arc_elf_howto (r_type); 1495 1496 r_symndx = ELF32_R_SYM (rel->r_info); 1497 1498 /* If we are generating another .o file and the symbol in not 1499 local, skip this relocation. */ 1500 if (bfd_link_relocatable (info)) 1501 { 1502 /* This is a relocateable link. We don't have to change 1503 anything, unless the reloc is against a section symbol, 1504 in which case we have to adjust according to where the 1505 section symbol winds up in the output section. */ 1506 1507 /* Checks if this is a local symbol and thus the reloc 1508 might (will??) be against a section symbol. */ 1509 if (r_symndx < symtab_hdr->sh_info) 1510 { 1511 sym = local_syms + r_symndx; 1512 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) 1513 { 1514 sec = local_sections[r_symndx]; 1515 1516 /* For RELA relocs. Just adjust the addend 1517 value in the relocation entry. */ 1518 rel->r_addend += sec->output_offset + sym->st_value; 1519 1520 ARC_DEBUG ("local symbols reloc (section=%d %s) seen in %s\n", 1521 (int) r_symndx, local_sections[r_symndx]->name, 1522 __PRETTY_FUNCTION__); 1523 } 1524 } 1525 } 1526 1527 h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__", 1528 false, false, true); 1529 1530 if (!reloc_data.sdata_begin_symbol_vma_set 1531 && h2 != NULL && h2->root.type != bfd_link_hash_undefined 1532 && h2->root.u.def.section->output_section != NULL) 1533 /* TODO: Verify this condition. */ 1534 { 1535 reloc_data.sdata_begin_symbol_vma = 1536 (h2->root.u.def.value 1537 + h2->root.u.def.section->output_section->vma); 1538 reloc_data.sdata_begin_symbol_vma_set = true; 1539 } 1540 1541 reloc_data.input_section = input_section; 1542 reloc_data.howto = howto; 1543 reloc_data.reloc_offset = rel->r_offset; 1544 reloc_data.reloc_addend = rel->r_addend; 1545 1546 /* This is a final link. */ 1547 h = NULL; 1548 sym = NULL; 1549 sec = NULL; 1550 1551 if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */ 1552 { 1553 sym = local_syms + r_symndx; 1554 sec = local_sections[r_symndx]; 1555 } 1556 else 1557 { 1558 bool warned, ignored; 1559 bfd_vma relocation ATTRIBUTE_UNUSED; 1560 1561 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 1562 r_symndx, symtab_hdr, sym_hashes, 1563 h, sec, relocation, 1564 unresolved_reloc, warned, ignored); 1565 1566 /* TODO: This code is repeated from below. We should 1567 clean it and remove duplications. 1568 Sec is used check for discarded sections. 1569 Need to redesign code below. */ 1570 1571 /* Get the symbol's entry in the symtab. */ 1572 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1573 1574 while (h->root.type == bfd_link_hash_indirect 1575 || h->root.type == bfd_link_hash_warning) 1576 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1577 1578 /* If we have encountered a definition for this symbol. */ 1579 if (h->root.type == bfd_link_hash_defined 1580 || h->root.type == bfd_link_hash_defweak) 1581 { 1582 reloc_data.sym_value = h->root.u.def.value; 1583 sec = h->root.u.def.section; 1584 } 1585 } 1586 1587 /* Clean relocs for symbols in discarded sections. */ 1588 if (sec != NULL && discarded_section (sec)) 1589 { 1590 _bfd_clear_contents (howto, input_bfd, input_section, 1591 contents, rel->r_offset); 1592 rel->r_info = 0; 1593 rel->r_addend = 0; 1594 1595 /* For ld -r, remove relocations in debug sections against 1596 sections defined in discarded sections. Not done for 1597 eh_frame editing code expects to be present. */ 1598 if (bfd_link_relocatable (info) 1599 && (input_section->flags & SEC_DEBUGGING)) 1600 wrel--; 1601 1602 continue; 1603 } 1604 1605 if (bfd_link_relocatable (info)) 1606 { 1607 if (wrel != rel) 1608 *wrel = *rel; 1609 continue; 1610 } 1611 1612 if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */ 1613 { 1614 reloc_data.sym_value = sym->st_value; 1615 reloc_data.sym_section = sec; 1616 reloc_data.symbol_name = 1617 bfd_elf_string_from_elf_section (input_bfd, 1618 symtab_hdr->sh_link, 1619 sym->st_name); 1620 1621 /* Mergeable section handling. */ 1622 if ((sec->flags & SEC_MERGE) 1623 && ELF_ST_TYPE (sym->st_info) == STT_SECTION) 1624 { 1625 asection *msec; 1626 msec = sec; 1627 rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym, 1628 &msec, rel->r_addend); 1629 rel->r_addend -= (sec->output_section->vma 1630 + sec->output_offset 1631 + sym->st_value); 1632 rel->r_addend += msec->output_section->vma + msec->output_offset; 1633 1634 reloc_data.reloc_addend = rel->r_addend; 1635 } 1636 1637 BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto)); 1638 if (htab->sgot != NULL) 1639 reloc_data.got_symbol_vma = htab->sgot->output_section->vma 1640 + htab->sgot->output_offset; 1641 1642 reloc_data.should_relocate = true; 1643 } 1644 else /* Global symbol. */ 1645 { 1646 /* FIXME: We should use the RELOC_FOR_GLOBAL_SYMBOL macro 1647 (defined in elf-bfd.h) here. */ 1648 1649 /* Get the symbol's entry in the symtab. */ 1650 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1651 1652 while (h->root.type == bfd_link_hash_indirect 1653 || h->root.type == bfd_link_hash_warning) 1654 { 1655 struct elf_arc_link_hash_entry *ah_old = 1656 (struct elf_arc_link_hash_entry *) h; 1657 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1658 struct elf_arc_link_hash_entry *ah = 1659 (struct elf_arc_link_hash_entry *) h; 1660 1661 if (ah->got_ents == 0 && ah_old->got_ents != ah->got_ents) 1662 ah->got_ents = ah_old->got_ents; 1663 } 1664 1665 /* TODO: Need to validate what was the intention. */ 1666 /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */ 1667 reloc_data.symbol_name = h->root.root.string; 1668 1669 /* If we have encountered a definition for this symbol. */ 1670 if (h->root.type == bfd_link_hash_defined 1671 || h->root.type == bfd_link_hash_defweak) 1672 { 1673 reloc_data.sym_value = h->root.u.def.value; 1674 reloc_data.sym_section = h->root.u.def.section; 1675 1676 reloc_data.should_relocate = true; 1677 1678 if (is_reloc_for_GOT (howto) && !bfd_link_pic (info)) 1679 { 1680 struct elf_arc_link_hash_entry *ah = 1681 (struct elf_arc_link_hash_entry *) h; 1682 /* TODO: Change it to use arc_do_relocation with 1683 ARC_32 reloc. Try to use ADD_RELA macro. */ 1684 bfd_vma relocation = 1685 reloc_data.sym_value + reloc_data.reloc_addend 1686 + (reloc_data.sym_section->output_section != NULL ? 1687 (reloc_data.sym_section->output_offset 1688 + reloc_data.sym_section->output_section->vma) 1689 : 0); 1690 1691 BFD_ASSERT (ah->got_ents); 1692 bfd_vma got_offset = ah->got_ents->offset; 1693 bfd_put_32 (output_bfd, relocation, 1694 htab->sgot->contents + got_offset); 1695 } 1696 if (is_reloc_for_PLT (howto) && h->plt.offset != (bfd_vma) -1) 1697 { 1698 /* TODO: This is repeated up here. */ 1699 reloc_data.sym_value = h->plt.offset; 1700 reloc_data.sym_section = htab->splt; 1701 } 1702 } 1703 else if (h->root.type == bfd_link_hash_undefweak) 1704 { 1705 /* Is weak symbol and has no definition. */ 1706 if (is_reloc_for_GOT (howto)) 1707 { 1708 reloc_data.sym_value = h->root.u.def.value; 1709 reloc_data.sym_section = htab->sgot; 1710 reloc_data.should_relocate = true; 1711 } 1712 else if (is_reloc_for_PLT (howto) 1713 && h->plt.offset != (bfd_vma) -1) 1714 { 1715 /* TODO: This is repeated up here. */ 1716 reloc_data.sym_value = h->plt.offset; 1717 reloc_data.sym_section = htab->splt; 1718 reloc_data.should_relocate = true; 1719 } 1720 else 1721 continue; 1722 } 1723 else 1724 { 1725 if (is_reloc_for_GOT (howto)) 1726 { 1727 reloc_data.sym_value = h->root.u.def.value; 1728 reloc_data.sym_section = htab->sgot; 1729 1730 reloc_data.should_relocate = true; 1731 } 1732 else if (is_reloc_for_PLT (howto)) 1733 { 1734 /* Fail if it is linking for PIE and the symbol is 1735 undefined. */ 1736 if (bfd_link_executable (info)) 1737 (*info->callbacks->undefined_symbol) 1738 (info, h->root.root.string, input_bfd, input_section, 1739 rel->r_offset, true); 1740 reloc_data.sym_value = h->plt.offset; 1741 reloc_data.sym_section = htab->splt; 1742 1743 reloc_data.should_relocate = true; 1744 } 1745 else if (!bfd_link_pic (info) || bfd_link_executable (info)) 1746 (*info->callbacks->undefined_symbol) 1747 (info, h->root.root.string, input_bfd, input_section, 1748 rel->r_offset, true); 1749 } 1750 1751 BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto)); 1752 if (htab->sgot != NULL) 1753 reloc_data.got_symbol_vma = htab->sgot->output_section->vma 1754 + htab->sgot->output_offset; 1755 } 1756 1757 if ((is_reloc_for_GOT (howto) 1758 || is_reloc_for_TLS (howto))) 1759 { 1760 reloc_data.should_relocate = true; 1761 1762 struct got_entry **list 1763 = get_got_entry_list_for_symbol (input_bfd, r_symndx, h); 1764 1765 reloc_data.got_offset_value 1766 = relocate_fix_got_relocs_for_got_info (list, 1767 tls_type_for_reloc (howto), 1768 info, 1769 output_bfd, 1770 r_symndx, 1771 local_syms, 1772 local_sections, 1773 h, 1774 &reloc_data); 1775 1776 if (h == NULL) 1777 { 1778 create_got_dynrelocs_for_single_entry ( 1779 got_entry_for_type (list, 1780 arc_got_entry_type_for_reloc (howto)), 1781 output_bfd, info, NULL); 1782 } 1783 } 1784 1785 1786 #define IS_ARC_PCREL_TYPE(TYPE) \ 1787 ( (TYPE == R_ARC_PC32) \ 1788 || (TYPE == R_ARC_32_PCREL)) 1789 1790 switch (r_type) 1791 { 1792 case R_ARC_32: 1793 case R_ARC_32_ME: 1794 case R_ARC_PC32: 1795 case R_ARC_32_PCREL: 1796 if (bfd_link_pic (info) 1797 && (input_section->flags & SEC_ALLOC) != 0 1798 && (!IS_ARC_PCREL_TYPE (r_type) 1799 || (h != NULL 1800 && h->dynindx != -1 1801 && !h->def_regular 1802 && (!info->symbolic || !h->def_regular)))) 1803 { 1804 Elf_Internal_Rela outrel; 1805 bfd_byte *loc; 1806 bool skip = false; 1807 bool relocate = false; 1808 asection *sreloc = _bfd_elf_get_dynamic_reloc_section 1809 (input_bfd, input_section, 1810 /*RELA*/ true); 1811 1812 BFD_ASSERT (sreloc != NULL); 1813 1814 outrel.r_offset = _bfd_elf_section_offset (output_bfd, 1815 info, 1816 input_section, 1817 rel->r_offset); 1818 1819 if (outrel.r_offset == (bfd_vma) -1) 1820 skip = true; 1821 1822 outrel.r_addend = rel->r_addend; 1823 outrel.r_offset += (input_section->output_section->vma 1824 + input_section->output_offset); 1825 1826 if (skip) 1827 { 1828 memset (&outrel, 0, sizeof outrel); 1829 relocate = false; 1830 } 1831 else if (h != NULL 1832 && h->dynindx != -1 1833 && (IS_ARC_PCREL_TYPE (r_type) 1834 || !(bfd_link_executable (info) 1835 || SYMBOLIC_BIND (info, h)) 1836 || ! h->def_regular)) 1837 { 1838 BFD_ASSERT (h != NULL); 1839 if ((input_section->flags & SEC_ALLOC) != 0) 1840 relocate = false; 1841 else 1842 relocate = true; 1843 1844 BFD_ASSERT (h->dynindx != -1); 1845 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); 1846 } 1847 else 1848 { 1849 /* Handle local symbols, they either do not have a 1850 global hash table entry (h == NULL), or are 1851 forced local due to a version script 1852 (h->forced_local), or the third condition is 1853 legacy, it appears to say something like, for 1854 links where we are pre-binding the symbols, or 1855 there's not an entry for this symbol in the 1856 dynamic symbol table, and it's a regular symbol 1857 not defined in a shared object, then treat the 1858 symbol as local, resolve it now. */ 1859 relocate = true; 1860 /* outrel.r_addend = 0; */ 1861 outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE); 1862 } 1863 1864 BFD_ASSERT (sreloc->contents != 0); 1865 1866 loc = sreloc->contents; 1867 loc += sreloc->reloc_count * sizeof (Elf32_External_Rela); 1868 sreloc->reloc_count += 1; 1869 1870 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 1871 1872 if (!relocate) 1873 continue; 1874 } 1875 break; 1876 default: 1877 break; 1878 } 1879 1880 if (is_reloc_SDA_relative (howto) 1881 && !reloc_data.sdata_begin_symbol_vma_set) 1882 { 1883 _bfd_error_handler 1884 ("error: linker symbol __SDATA_BEGIN__ not found"); 1885 bfd_set_error (bfd_error_bad_value); 1886 return false; 1887 } 1888 1889 DEBUG_ARC_RELOC (reloc_data); 1890 1891 /* Make sure we have with a dynamic linker. In case of GOT and PLT 1892 the sym_section should point to .got or .plt respectively. */ 1893 if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto)) 1894 && reloc_data.sym_section == NULL) 1895 { 1896 _bfd_error_handler 1897 (_("GOT and PLT relocations cannot be fixed with a non dynamic linker")); 1898 bfd_set_error (bfd_error_bad_value); 1899 return false; 1900 } 1901 1902 msg = NULL; 1903 switch (arc_do_relocation (contents, reloc_data, info)) 1904 { 1905 case bfd_reloc_ok: 1906 continue; /* The reloc processing loop. */ 1907 1908 case bfd_reloc_overflow: 1909 (*info->callbacks->reloc_overflow) 1910 (info, (h ? &h->root : NULL), reloc_data.symbol_name, howto->name, (bfd_vma) 0, 1911 input_bfd, input_section, rel->r_offset); 1912 break; 1913 1914 case bfd_reloc_undefined: 1915 (*info->callbacks->undefined_symbol) 1916 (info, reloc_data.symbol_name, input_bfd, input_section, rel->r_offset, true); 1917 break; 1918 1919 case bfd_reloc_other: 1920 /* xgettext:c-format */ 1921 msg = _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area"); 1922 break; 1923 1924 case bfd_reloc_outofrange: 1925 /* xgettext:c-format */ 1926 msg = _("%pB(%pA): internal error: out of range error"); 1927 break; 1928 1929 case bfd_reloc_notsupported: 1930 /* xgettext:c-format */ 1931 msg = _("%pB(%pA): internal error: unsupported relocation error"); 1932 break; 1933 1934 case bfd_reloc_dangerous: 1935 /* xgettext:c-format */ 1936 msg = _("%pB(%pA): internal error: dangerous relocation"); 1937 break; 1938 1939 default: 1940 /* xgettext:c-format */ 1941 msg = _("%pB(%pA): internal error: unknown error"); 1942 break; 1943 } 1944 1945 if (msg) 1946 _bfd_error_handler (msg, input_bfd, input_section, reloc_data.symbol_name); 1947 return false; 1948 } 1949 1950 return true; 1951 } 1952 1953 #define elf_arc_hash_table(p) \ 1954 ((is_elf_hash_table ((p)->hash) \ 1955 && elf_hash_table_id (elf_hash_table (p)) == ARC_ELF_DATA) \ 1956 ? (struct elf_arc_link_hash_table *) (p)->hash : NULL) 1957 1958 static bool 1959 elf_arc_check_relocs (bfd * abfd, 1960 struct bfd_link_info * info, 1961 asection * sec, 1962 const Elf_Internal_Rela * relocs) 1963 { 1964 Elf_Internal_Shdr * symtab_hdr; 1965 struct elf_link_hash_entry ** sym_hashes; 1966 const Elf_Internal_Rela * rel; 1967 const Elf_Internal_Rela * rel_end; 1968 bfd * dynobj; 1969 asection * sreloc = NULL; 1970 struct elf_link_hash_table * htab = elf_hash_table (info); 1971 1972 if (bfd_link_relocatable (info)) 1973 return true; 1974 1975 if (htab->dynobj == NULL) 1976 htab->dynobj = abfd; 1977 1978 dynobj = (elf_hash_table (info))->dynobj; 1979 symtab_hdr = &((elf_tdata (abfd))->symtab_hdr); 1980 sym_hashes = elf_sym_hashes (abfd); 1981 1982 rel_end = relocs + sec->reloc_count; 1983 for (rel = relocs; rel < rel_end; rel++) 1984 { 1985 enum elf_arc_reloc_type r_type; 1986 reloc_howto_type *howto; 1987 unsigned long r_symndx; 1988 struct elf_link_hash_entry *h; 1989 1990 r_type = ELF32_R_TYPE (rel->r_info); 1991 1992 if (r_type >= (int) R_ARC_max) 1993 { 1994 bfd_set_error (bfd_error_bad_value); 1995 return false; 1996 } 1997 howto = arc_elf_howto (r_type); 1998 1999 /* Load symbol information. */ 2000 r_symndx = ELF32_R_SYM (rel->r_info); 2001 if (r_symndx < symtab_hdr->sh_info) /* Is a local symbol. */ 2002 h = NULL; 2003 else /* Global one. */ 2004 { 2005 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 2006 while (h->root.type == bfd_link_hash_indirect 2007 || h->root.type == bfd_link_hash_warning) 2008 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2009 } 2010 2011 2012 switch (r_type) 2013 { 2014 case R_ARC_32: 2015 case R_ARC_32_ME: 2016 /* During shared library creation, these relocs should not 2017 appear in a shared library (as memory will be read only 2018 and the dynamic linker can not resolve these. However 2019 the error should not occur for e.g. debugging or 2020 non-readonly sections. */ 2021 if (h != NULL 2022 && (bfd_link_dll (info) && !bfd_link_pie (info)) 2023 && (sec->flags & SEC_ALLOC) != 0 2024 && (sec->flags & SEC_READONLY) != 0 2025 && ((sec->flags & SEC_CODE) != 0 2026 || (sec->flags & SEC_DEBUGGING) != 0)) 2027 { 2028 const char *name; 2029 if (h) 2030 name = h->root.root.string; 2031 else 2032 name = "UNKNOWN"; 2033 _bfd_error_handler 2034 /* xgettext:c-format */ 2035 (_("%pB: relocation %s against `%s' can not be used" 2036 " when making a shared object; recompile with -fPIC"), 2037 abfd, 2038 arc_elf_howto (r_type)->name, 2039 name); 2040 bfd_set_error (bfd_error_bad_value); 2041 return false; 2042 } 2043 2044 /* In some cases we are not setting the 'non_got_ref' 2045 flag, even though the relocations don't require a GOT 2046 access. We should extend the testing in this area to 2047 ensure that no significant cases are being missed. */ 2048 if (h) 2049 h->non_got_ref = 1; 2050 /* FALLTHROUGH */ 2051 case R_ARC_PC32: 2052 case R_ARC_32_PCREL: 2053 if ((bfd_link_pic (info)) 2054 && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL) 2055 || (h != NULL 2056 && (!info->symbolic || !h->def_regular)))) 2057 { 2058 if (sreloc == NULL) 2059 { 2060 if (info->dynamic 2061 && ! htab->dynamic_sections_created 2062 && ! _bfd_elf_link_create_dynamic_sections (abfd, info)) 2063 return false; 2064 sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj, 2065 2, abfd, 2066 /*rela*/ 2067 true); 2068 2069 if (sreloc == NULL) 2070 return false; 2071 } 2072 sreloc->size += sizeof (Elf32_External_Rela); 2073 2074 } 2075 default: 2076 break; 2077 } 2078 2079 if (is_reloc_for_PLT (howto)) 2080 { 2081 if (h == NULL) 2082 continue; 2083 else 2084 if (h->forced_local == 0) 2085 h->needs_plt = 1; 2086 } 2087 2088 /* Add info to the symbol got_entry_list. */ 2089 if (is_reloc_for_GOT (howto) 2090 || is_reloc_for_TLS (howto)) 2091 { 2092 if (bfd_link_dll (info) && !bfd_link_pie (info) 2093 && (r_type == R_ARC_TLS_LE_32 || r_type == R_ARC_TLS_LE_S9)) 2094 { 2095 const char *name; 2096 if (h) 2097 name = h->root.root.string; 2098 else 2099 /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); */ 2100 name = "UNKNOWN"; 2101 _bfd_error_handler 2102 /* xgettext:c-format */ 2103 (_("%pB: relocation %s against `%s' can not be used" 2104 " when making a shared object; recompile with -fPIC"), 2105 abfd, 2106 arc_elf_howto (r_type)->name, 2107 name); 2108 bfd_set_error (bfd_error_bad_value); 2109 return false; 2110 } 2111 if (! _bfd_elf_create_got_section (dynobj, info)) 2112 return false; 2113 2114 arc_fill_got_info_for_reloc ( 2115 arc_got_entry_type_for_reloc (howto), 2116 get_got_entry_list_for_symbol (abfd, r_symndx, h), 2117 info, 2118 h); 2119 } 2120 } 2121 2122 return true; 2123 } 2124 2125 #define ELF_DYNAMIC_INTERPRETER "/sbin/ld-uClibc.so" 2126 2127 static const struct plt_version_t * 2128 arc_get_plt_version (struct bfd_link_info *info) 2129 { 2130 int i; 2131 2132 for (i = 0; i < 1; i++) 2133 { 2134 ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i, 2135 (int) plt_versions[i].entry_size, 2136 (int) plt_versions[i].elem_size); 2137 } 2138 2139 if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2) 2140 { 2141 if (bfd_link_pic (info)) 2142 return &(plt_versions[ELF_ARCV2_PIC]); 2143 else 2144 return &(plt_versions[ELF_ARCV2_ABS]); 2145 } 2146 else 2147 { 2148 if (bfd_link_pic (info)) 2149 return &(plt_versions[ELF_ARC_PIC]); 2150 else 2151 return &(plt_versions[ELF_ARC_ABS]); 2152 } 2153 } 2154 2155 static bfd_vma 2156 add_symbol_to_plt (struct bfd_link_info *info) 2157 { 2158 struct elf_link_hash_table *htab = elf_hash_table (info); 2159 bfd_vma ret; 2160 2161 const struct plt_version_t *plt_data = arc_get_plt_version (info); 2162 2163 /* If this is the first .plt entry, make room for the special first 2164 entry. */ 2165 if (htab->splt->size == 0) 2166 htab->splt->size += plt_data->entry_size; 2167 2168 ret = htab->splt->size; 2169 2170 htab->splt->size += plt_data->elem_size; 2171 ARC_DEBUG ("PLT_SIZE = %d\n", (int) htab->splt->size); 2172 2173 htab->sgotplt->size += 4; 2174 htab->srelplt->size += sizeof (Elf32_External_Rela); 2175 2176 return ret; 2177 } 2178 2179 #define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \ 2180 plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0) 2181 2182 static void 2183 plt_do_relocs_for_symbol (bfd *abfd, 2184 struct elf_link_hash_table *htab, 2185 const struct plt_reloc *reloc, 2186 bfd_vma plt_offset, 2187 bfd_vma symbol_got_offset) 2188 { 2189 while (SYM_ONLY (reloc->symbol) != LAST_RELOC) 2190 { 2191 bfd_vma relocation = 0; 2192 2193 switch (SYM_ONLY (reloc->symbol)) 2194 { 2195 case SGOT: 2196 relocation 2197 = htab->sgotplt->output_section->vma 2198 + htab->sgotplt->output_offset + symbol_got_offset; 2199 break; 2200 } 2201 relocation += reloc->addend; 2202 2203 if (IS_RELATIVE (reloc->symbol)) 2204 { 2205 bfd_vma reloc_offset = reloc->offset; 2206 reloc_offset -= (IS_INSN_32 (reloc->symbol)) ? 4 : 0; 2207 reloc_offset -= (IS_INSN_24 (reloc->symbol)) ? 2 : 0; 2208 2209 relocation -= htab->splt->output_section->vma 2210 + htab->splt->output_offset 2211 + plt_offset + reloc_offset; 2212 } 2213 2214 /* TODO: being ME is not a property of the relocation but of the 2215 section of which is applying the relocation. */ 2216 if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd)) 2217 { 2218 relocation 2219 = ((relocation & 0xffff0000) >> 16) 2220 | ((relocation & 0xffff) << 16); 2221 } 2222 2223 switch (reloc->size) 2224 { 2225 case 32: 2226 bfd_put_32 (htab->splt->output_section->owner, 2227 relocation, 2228 htab->splt->contents + plt_offset + reloc->offset); 2229 break; 2230 } 2231 2232 reloc = &(reloc[1]); /* Jump to next relocation. */ 2233 } 2234 } 2235 2236 static void 2237 relocate_plt_for_symbol (bfd *output_bfd, 2238 struct bfd_link_info *info, 2239 struct elf_link_hash_entry *h) 2240 { 2241 const struct plt_version_t *plt_data = arc_get_plt_version (info); 2242 struct elf_link_hash_table *htab = elf_hash_table (info); 2243 2244 bfd_vma plt_index = (h->plt.offset - plt_data->entry_size) 2245 / plt_data->elem_size; 2246 bfd_vma got_offset = (plt_index + 3) * 4; 2247 2248 ARC_DEBUG ("arc_info: PLT_OFFSET = %#lx, PLT_ENTRY_VMA = %#lx, \ 2249 GOT_ENTRY_OFFSET = %#lx, GOT_ENTRY_VMA = %#lx, for symbol %s\n", 2250 (long) h->plt.offset, 2251 (long) (htab->splt->output_section->vma 2252 + htab->splt->output_offset 2253 + h->plt.offset), 2254 (long) got_offset, 2255 (long) (htab->sgotplt->output_section->vma 2256 + htab->sgotplt->output_offset 2257 + got_offset), 2258 h->root.root.string); 2259 2260 { 2261 bfd_vma i = 0; 2262 uint16_t *ptr = (uint16_t *) plt_data->elem; 2263 2264 for (i = 0; i < plt_data->elem_size/2; i++) 2265 { 2266 uint16_t data = ptr[i]; 2267 bfd_put_16 (output_bfd, 2268 (bfd_vma) data, 2269 htab->splt->contents + h->plt.offset + (i*2)); 2270 } 2271 } 2272 2273 plt_do_relocs_for_symbol (output_bfd, htab, 2274 plt_data->elem_relocs, 2275 h->plt.offset, 2276 got_offset); 2277 2278 /* Fill in the entry in the global offset table. */ 2279 bfd_put_32 (output_bfd, 2280 (bfd_vma) (htab->splt->output_section->vma 2281 + htab->splt->output_offset), 2282 htab->sgotplt->contents + got_offset); 2283 2284 /* TODO: Fill in the entry in the .rela.plt section. */ 2285 { 2286 Elf_Internal_Rela rel; 2287 bfd_byte *loc; 2288 2289 rel.r_offset = (htab->sgotplt->output_section->vma 2290 + htab->sgotplt->output_offset 2291 + got_offset); 2292 rel.r_addend = 0; 2293 2294 BFD_ASSERT (h->dynindx != -1); 2295 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_JMP_SLOT); 2296 2297 loc = htab->srelplt->contents; 2298 loc += plt_index * sizeof (Elf32_External_Rela); /* relA */ 2299 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 2300 } 2301 } 2302 2303 static void 2304 relocate_plt_for_entry (bfd *abfd, 2305 struct bfd_link_info *info) 2306 { 2307 const struct plt_version_t *plt_data = arc_get_plt_version (info); 2308 struct elf_link_hash_table *htab = elf_hash_table (info); 2309 2310 { 2311 bfd_vma i = 0; 2312 uint16_t *ptr = (uint16_t *) plt_data->entry; 2313 for (i = 0; i < plt_data->entry_size/2; i++) 2314 { 2315 uint16_t data = ptr[i]; 2316 bfd_put_16 (abfd, 2317 (bfd_vma) data, 2318 htab->splt->contents + (i*2)); 2319 } 2320 } 2321 PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs); 2322 } 2323 2324 /* Desc : Adjust a symbol defined by a dynamic object and referenced 2325 by a regular object. The current definition is in some section of 2326 the dynamic object, but we're not including those sections. We 2327 have to change the definition to something the rest of the link can 2328 understand. */ 2329 2330 static bool 2331 elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info, 2332 struct elf_link_hash_entry *h) 2333 { 2334 asection *s; 2335 bfd *dynobj = (elf_hash_table (info))->dynobj; 2336 struct elf_link_hash_table *htab = elf_hash_table (info); 2337 2338 if (h->type == STT_FUNC 2339 || h->type == STT_GNU_IFUNC 2340 || h->needs_plt == 1) 2341 { 2342 if (!bfd_link_pic (info) && !h->def_dynamic && !h->ref_dynamic) 2343 { 2344 /* This case can occur if we saw a PLT32 reloc in an input 2345 file, but the symbol was never referred to by a dynamic 2346 object. In such a case, we don't actually need to build 2347 a procedure linkage table, and we can just do a PC32 2348 reloc instead. */ 2349 BFD_ASSERT (h->needs_plt); 2350 return true; 2351 } 2352 2353 /* Make sure this symbol is output as a dynamic symbol. */ 2354 if (h->dynindx == -1 && !h->forced_local 2355 && !bfd_elf_link_record_dynamic_symbol (info, h)) 2356 return false; 2357 2358 if (bfd_link_pic (info) 2359 || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h)) 2360 { 2361 bfd_vma loc = add_symbol_to_plt (info); 2362 2363 if (bfd_link_executable (info) && !h->def_regular) 2364 { 2365 h->root.u.def.section = htab->splt; 2366 h->root.u.def.value = loc; 2367 } 2368 h->plt.offset = loc; 2369 } 2370 else 2371 { 2372 h->plt.offset = (bfd_vma) -1; 2373 h->needs_plt = 0; 2374 } 2375 return true; 2376 } 2377 2378 /* If this is a weak symbol, and there is a real definition, the 2379 processor independent code will have arranged for us to see the 2380 real definition first, and we can just use the same value. */ 2381 if (h->is_weakalias) 2382 { 2383 struct elf_link_hash_entry *def = weakdef (h); 2384 BFD_ASSERT (def->root.type == bfd_link_hash_defined); 2385 h->root.u.def.section = def->root.u.def.section; 2386 h->root.u.def.value = def->root.u.def.value; 2387 return true; 2388 } 2389 2390 /* This is a reference to a symbol defined by a dynamic object which 2391 is not a function. */ 2392 2393 /* If we are creating a shared library, we must presume that the 2394 only references to the symbol are via the global offset table. 2395 For such cases we need not do anything here; the relocations will 2396 be handled correctly by relocate_section. */ 2397 if (!bfd_link_executable (info)) 2398 return true; 2399 2400 /* If there are no non-GOT references, we do not need a copy 2401 relocation. */ 2402 if (!h->non_got_ref) 2403 return true; 2404 2405 /* If -z nocopyreloc was given, we won't generate them either. */ 2406 if (info->nocopyreloc) 2407 { 2408 h->non_got_ref = 0; 2409 return true; 2410 } 2411 2412 /* We must allocate the symbol in our .dynbss section, which will 2413 become part of the .bss section of the executable. There will be 2414 an entry for this symbol in the .dynsym section. The dynamic 2415 object will contain position independent code, so all references 2416 from the dynamic object to this symbol will go through the global 2417 offset table. The dynamic linker will use the .dynsym entry to 2418 determine the address it must put in the global offset table, so 2419 both the dynamic object and the regular object will refer to the 2420 same memory location for the variable. */ 2421 2422 if (htab == NULL) 2423 return false; 2424 2425 /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to 2426 copy the initial value out of the dynamic object and into the 2427 runtime process image. We need to remember the offset into the 2428 .rela.bss section we are going to use. */ 2429 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) 2430 { 2431 struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info); 2432 2433 BFD_ASSERT (arc_htab->elf.srelbss != NULL); 2434 arc_htab->elf.srelbss->size += sizeof (Elf32_External_Rela); 2435 h->needs_copy = 1; 2436 } 2437 2438 /* TODO: Move this also to arc_hash_table. */ 2439 s = bfd_get_section_by_name (dynobj, ".dynbss"); 2440 BFD_ASSERT (s != NULL); 2441 2442 return _bfd_elf_adjust_dynamic_copy (info, h, s); 2443 } 2444 2445 /* Function : elf_arc_finish_dynamic_symbol 2446 Brief : Finish up dynamic symbol handling. We set the 2447 contents of various dynamic sections here. 2448 Args : output_bfd : 2449 info : 2450 h : 2451 sym : 2452 Returns : True/False as the return status. */ 2453 2454 static bool 2455 elf_arc_finish_dynamic_symbol (bfd * output_bfd, 2456 struct bfd_link_info *info, 2457 struct elf_link_hash_entry *h, 2458 Elf_Internal_Sym * sym) 2459 { 2460 if (h->plt.offset != (bfd_vma) -1) 2461 { 2462 relocate_plt_for_symbol (output_bfd, info, h); 2463 2464 if (!h->def_regular) 2465 { 2466 /* Mark the symbol as undefined, rather than as defined in 2467 the .plt section. Leave the value alone. */ 2468 sym->st_shndx = SHN_UNDEF; 2469 } 2470 } 2471 2472 2473 /* This function traverses list of GOT entries and 2474 create respective dynamic relocs. */ 2475 /* TODO: Make function to get list and not access the list directly. */ 2476 /* TODO: Move function to relocate_section create this relocs eagerly. */ 2477 struct elf_arc_link_hash_entry *ah = 2478 (struct elf_arc_link_hash_entry *) h; 2479 create_got_dynrelocs_for_got_info (&ah->got_ents, 2480 output_bfd, 2481 info, 2482 h); 2483 2484 if (h->needs_copy) 2485 { 2486 struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info); 2487 2488 if (h->dynindx == -1 2489 || (h->root.type != bfd_link_hash_defined 2490 && h->root.type != bfd_link_hash_defweak) 2491 || arc_htab->elf.srelbss == NULL) 2492 abort (); 2493 2494 bfd_vma rel_offset = (h->root.u.def.value 2495 + h->root.u.def.section->output_section->vma 2496 + h->root.u.def.section->output_offset); 2497 2498 bfd_byte * loc = arc_htab->elf.srelbss->contents 2499 + (arc_htab->elf.srelbss->reloc_count * sizeof (Elf32_External_Rela)); 2500 arc_htab->elf.srelbss->reloc_count++; 2501 2502 Elf_Internal_Rela rel; 2503 rel.r_addend = 0; 2504 rel.r_offset = rel_offset; 2505 2506 BFD_ASSERT (h->dynindx != -1); 2507 rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_COPY); 2508 2509 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 2510 } 2511 2512 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ 2513 if (strcmp (h->root.root.string, "_DYNAMIC") == 0 2514 || strcmp (h->root.root.string, "__DYNAMIC") == 0 2515 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) 2516 sym->st_shndx = SHN_ABS; 2517 2518 return true; 2519 } 2520 2521 #define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION) \ 2522 case TAG: \ 2523 if (SYMBOL != NULL) \ 2524 h = elf_link_hash_lookup (elf_hash_table (info), \ 2525 SYMBOL, false, false, true); \ 2526 else if (SECTION != NULL) \ 2527 s = bfd_get_linker_section (dynobj, SECTION); \ 2528 break; 2529 2530 2531 struct obfd_info_group { 2532 bfd *output_bfd; 2533 struct bfd_link_info *info; 2534 }; 2535 2536 static bool 2537 arc_create_forced_local_got_entries_for_tls (struct bfd_hash_entry *bh, 2538 void *data) 2539 { 2540 struct elf_arc_link_hash_entry * h = 2541 (struct elf_arc_link_hash_entry *) bh; 2542 struct obfd_info_group *tmp = (struct obfd_info_group *) data; 2543 2544 if (h->got_ents != NULL) 2545 { 2546 BFD_ASSERT (h); 2547 2548 struct got_entry *list = h->got_ents; 2549 2550 while (list != NULL) 2551 { 2552 create_got_dynrelocs_for_single_entry (list, tmp->output_bfd, 2553 tmp->info, 2554 (struct elf_link_hash_entry *) h); 2555 list = list->next; 2556 } 2557 } 2558 2559 return true; 2560 } 2561 2562 2563 /* Function : elf_arc_finish_dynamic_sections 2564 Brief : Finish up the dynamic sections handling. 2565 Args : output_bfd : 2566 info : 2567 h : 2568 sym : 2569 Returns : True/False as the return status. */ 2570 2571 static bool 2572 elf_arc_finish_dynamic_sections (bfd * output_bfd, 2573 struct bfd_link_info *info) 2574 { 2575 struct elf_link_hash_table *htab = elf_hash_table (info); 2576 bfd *dynobj = (elf_hash_table (info))->dynobj; 2577 asection *sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 2578 2579 if (sdyn) 2580 { 2581 Elf32_External_Dyn *dyncon, *dynconend; 2582 2583 dyncon = (Elf32_External_Dyn *) sdyn->contents; 2584 dynconend 2585 = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); 2586 for (; dyncon < dynconend; dyncon++) 2587 { 2588 Elf_Internal_Dyn internal_dyn; 2589 bool do_it = false; 2590 2591 struct elf_link_hash_entry *h = NULL; 2592 asection *s = NULL; 2593 2594 bfd_elf32_swap_dyn_in (dynobj, dyncon, &internal_dyn); 2595 2596 switch (internal_dyn.d_tag) 2597 { 2598 GET_SYMBOL_OR_SECTION (DT_INIT, info->init_function, NULL) 2599 GET_SYMBOL_OR_SECTION (DT_FINI, info->fini_function, NULL) 2600 GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt") 2601 GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt") 2602 GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt") 2603 GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version") 2604 GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d") 2605 GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r") 2606 default: 2607 break; 2608 } 2609 2610 /* In case the dynamic symbols should be updated with a symbol. */ 2611 if (h != NULL 2612 && (h->root.type == bfd_link_hash_defined 2613 || h->root.type == bfd_link_hash_defweak)) 2614 { 2615 asection *asec_ptr; 2616 2617 internal_dyn.d_un.d_val = h->root.u.def.value; 2618 asec_ptr = h->root.u.def.section; 2619 if (asec_ptr->output_section != NULL) 2620 { 2621 internal_dyn.d_un.d_val += 2622 (asec_ptr->output_section->vma 2623 + asec_ptr->output_offset); 2624 } 2625 else 2626 { 2627 /* The symbol is imported from another shared 2628 library and does not apply to this one. */ 2629 internal_dyn.d_un.d_val = 0; 2630 } 2631 do_it = true; 2632 } 2633 else if (s != NULL) /* With a section information. */ 2634 { 2635 switch (internal_dyn.d_tag) 2636 { 2637 case DT_PLTGOT: 2638 case DT_JMPREL: 2639 case DT_VERSYM: 2640 case DT_VERDEF: 2641 case DT_VERNEED: 2642 internal_dyn.d_un.d_ptr = (s->output_section->vma 2643 + s->output_offset); 2644 do_it = true; 2645 break; 2646 2647 case DT_PLTRELSZ: 2648 internal_dyn.d_un.d_val = s->size; 2649 do_it = true; 2650 break; 2651 2652 default: 2653 break; 2654 } 2655 } 2656 2657 if (do_it) 2658 bfd_elf32_swap_dyn_out (output_bfd, &internal_dyn, dyncon); 2659 } 2660 2661 if (htab->splt->size > 0) 2662 { 2663 relocate_plt_for_entry (output_bfd, info); 2664 } 2665 2666 /* TODO: Validate this. */ 2667 if (htab->srelplt->output_section != bfd_abs_section_ptr) 2668 elf_section_data (htab->srelplt->output_section) 2669 ->this_hdr.sh_entsize = 12; 2670 } 2671 2672 /* Fill in the first three entries in the global offset table. */ 2673 if (htab->sgot) 2674 { 2675 struct elf_link_hash_entry *h; 2676 h = elf_link_hash_lookup (elf_hash_table (info), "_GLOBAL_OFFSET_TABLE_", 2677 false, false, true); 2678 2679 if (h != NULL && h->root.type != bfd_link_hash_undefined 2680 && h->root.u.def.section != NULL) 2681 { 2682 asection *sec = h->root.u.def.section; 2683 2684 if (sdyn == NULL) 2685 bfd_put_32 (output_bfd, (bfd_vma) 0, 2686 sec->contents); 2687 else 2688 bfd_put_32 (output_bfd, 2689 sdyn->output_section->vma + sdyn->output_offset, 2690 sec->contents); 2691 bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 4); 2692 bfd_put_32 (output_bfd, (bfd_vma) 0, sec->contents + 8); 2693 } 2694 } 2695 2696 struct obfd_info_group group; 2697 group.output_bfd = output_bfd; 2698 group.info = info; 2699 bfd_hash_traverse (&info->hash->table, 2700 arc_create_forced_local_got_entries_for_tls, &group); 2701 2702 return true; 2703 } 2704 2705 #define ADD_DYNAMIC_SYMBOL(NAME, TAG) \ 2706 h = elf_link_hash_lookup (elf_hash_table (info), \ 2707 NAME, false, false, false); \ 2708 if ((h != NULL && (h->ref_regular || h->def_regular))) \ 2709 if (! _bfd_elf_add_dynamic_entry (info, TAG, 0)) \ 2710 return false; 2711 2712 /* Set the sizes of the dynamic sections. */ 2713 static bool 2714 elf_arc_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED, 2715 struct bfd_link_info *info) 2716 { 2717 bfd *dynobj; 2718 asection *s; 2719 bool relocs_exist = false; 2720 struct elf_link_hash_table *htab = elf_hash_table (info); 2721 2722 dynobj = htab->dynobj; 2723 if (dynobj == NULL) 2724 return true; 2725 2726 if (htab->dynamic_sections_created) 2727 { 2728 struct elf_link_hash_entry *h; 2729 2730 /* Set the contents of the .interp section to the 2731 interpreter. */ 2732 if (bfd_link_executable (info) && !info->nointerp) 2733 { 2734 s = bfd_get_section_by_name (dynobj, ".interp"); 2735 BFD_ASSERT (s != NULL); 2736 s->size = sizeof (ELF_DYNAMIC_INTERPRETER); 2737 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; 2738 } 2739 2740 /* Add some entries to the .dynamic section. We fill in some of 2741 the values later, in elf_bfd_final_link, but we must add the 2742 entries now so that we know the final size of the .dynamic 2743 section. Checking if the .init section is present. We also 2744 create DT_INIT and DT_FINI entries if the init_str has been 2745 changed by the user. */ 2746 ADD_DYNAMIC_SYMBOL (info->init_function, DT_INIT); 2747 ADD_DYNAMIC_SYMBOL (info->fini_function, DT_FINI); 2748 } 2749 else 2750 { 2751 /* We may have created entries in the .rela.got section. 2752 However, if we are not creating the dynamic sections, we will 2753 not actually use these entries. Reset the size of .rela.got, 2754 which will cause it to get stripped from the output file 2755 below. */ 2756 if (htab->srelgot != NULL) 2757 htab->srelgot->size = 0; 2758 } 2759 2760 for (s = dynobj->sections; s != NULL; s = s->next) 2761 { 2762 if ((s->flags & SEC_LINKER_CREATED) == 0) 2763 continue; 2764 2765 if (s == htab->splt 2766 || s == htab->sgot 2767 || s == htab->sgotplt 2768 || s == htab->sdynbss) 2769 { 2770 /* Strip this section if we don't need it. */ 2771 } 2772 else if (startswith (s->name, ".rela")) 2773 { 2774 if (s->size != 0 && s != htab->srelplt) 2775 relocs_exist = true; 2776 2777 /* We use the reloc_count field as a counter if we need to 2778 copy relocs into the output file. */ 2779 s->reloc_count = 0; 2780 } 2781 else 2782 { 2783 /* It's not one of our sections, so don't allocate space. */ 2784 continue; 2785 } 2786 2787 if (s->size == 0) 2788 { 2789 s->flags |= SEC_EXCLUDE; 2790 continue; 2791 } 2792 2793 if ((s->flags & SEC_HAS_CONTENTS) == 0) 2794 continue; 2795 2796 /* Allocate memory for the section contents. */ 2797 s->contents = bfd_zalloc (dynobj, s->size); 2798 if (s->contents == NULL) 2799 return false; 2800 } 2801 2802 return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs_exist); 2803 } 2804 2805 2806 /* Classify dynamic relocs such that -z combreloc can reorder and combine 2807 them. */ 2808 static enum elf_reloc_type_class 2809 elf32_arc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 2810 const asection *rel_sec ATTRIBUTE_UNUSED, 2811 const Elf_Internal_Rela *rela) 2812 { 2813 switch ((int) ELF32_R_TYPE (rela->r_info)) 2814 { 2815 case R_ARC_RELATIVE: 2816 return reloc_class_relative; 2817 case R_ARC_JMP_SLOT: 2818 return reloc_class_plt; 2819 case R_ARC_COPY: 2820 return reloc_class_copy; 2821 /* TODO: Needed in future to support ifunc. */ 2822 /* 2823 case R_ARC_IRELATIVE: 2824 return reloc_class_ifunc; 2825 */ 2826 default: 2827 return reloc_class_normal; 2828 } 2829 } 2830 2831 const struct elf_size_info arc_elf32_size_info = 2832 { 2833 sizeof (Elf32_External_Ehdr), 2834 sizeof (Elf32_External_Phdr), 2835 sizeof (Elf32_External_Shdr), 2836 sizeof (Elf32_External_Rel), 2837 sizeof (Elf32_External_Rela), 2838 sizeof (Elf32_External_Sym), 2839 sizeof (Elf32_External_Dyn), 2840 sizeof (Elf_External_Note), 2841 4, 2842 1, 2843 32, 2, 2844 ELFCLASS32, EV_CURRENT, 2845 bfd_elf32_write_out_phdrs, 2846 bfd_elf32_write_shdrs_and_ehdr, 2847 bfd_elf32_checksum_contents, 2848 bfd_elf32_write_relocs, 2849 bfd_elf32_swap_symbol_in, 2850 bfd_elf32_swap_symbol_out, 2851 bfd_elf32_slurp_reloc_table, 2852 bfd_elf32_slurp_symbol_table, 2853 bfd_elf32_swap_dyn_in, 2854 bfd_elf32_swap_dyn_out, 2855 bfd_elf32_swap_reloc_in, 2856 bfd_elf32_swap_reloc_out, 2857 bfd_elf32_swap_reloca_in, 2858 bfd_elf32_swap_reloca_out 2859 }; 2860 2861 #define elf_backend_size_info arc_elf32_size_info 2862 2863 /* GDB expects general purpose registers to be in section .reg. However Linux 2864 kernel doesn't create this section and instead writes registers to NOTE 2865 section. It is up to the binutils to create a pseudo-section .reg from the 2866 contents of NOTE. Also BFD will read pid and signal number from NOTE. This 2867 function relies on offsets inside elf_prstatus structure in Linux to be 2868 stable. */ 2869 2870 static bool 2871 elf32_arc_grok_prstatus (bfd *abfd, Elf_Internal_Note *note) 2872 { 2873 int offset; 2874 size_t size; 2875 2876 switch (note->descsz) 2877 { 2878 default: 2879 return false; 2880 2881 case 236: /* sizeof (struct elf_prstatus) on Linux/arc. */ 2882 /* pr_cursig */ 2883 elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12); 2884 /* pr_pid */ 2885 elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24); 2886 /* pr_regs */ 2887 offset = 72; 2888 size = (40 * 4); /* There are 40 registers in user_regs_struct. */ 2889 break; 2890 } 2891 /* Make a ".reg/999" section. */ 2892 return _bfd_elfcore_make_pseudosection (abfd, ".reg", size, 2893 note->descpos + offset); 2894 } 2895 2896 /* Determine whether an object attribute tag takes an integer, a 2897 string or both. */ 2898 2899 static int 2900 elf32_arc_obj_attrs_arg_type (int tag) 2901 { 2902 if (tag == Tag_ARC_CPU_name 2903 || tag == Tag_ARC_ISA_config 2904 || tag == Tag_ARC_ISA_apex) 2905 return ATTR_TYPE_FLAG_STR_VAL; 2906 else if (tag < (Tag_ARC_ISA_mpy_option + 1)) 2907 return ATTR_TYPE_FLAG_INT_VAL; 2908 else 2909 return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL; 2910 } 2911 2912 /* Attribute numbers >=14 can be safely ignored. */ 2913 2914 static bool 2915 elf32_arc_obj_attrs_handle_unknown (bfd *abfd, int tag) 2916 { 2917 if ((tag & 127) < (Tag_ARC_ISA_mpy_option + 1)) 2918 { 2919 _bfd_error_handler 2920 (_("%pB: unknown mandatory ARC object attribute %d"), 2921 abfd, tag); 2922 bfd_set_error (bfd_error_bad_value); 2923 return false; 2924 } 2925 else 2926 { 2927 _bfd_error_handler 2928 (_("warning: %pB: unknown ARC object attribute %d"), 2929 abfd, tag); 2930 return true; 2931 } 2932 } 2933 2934 /* Handle an ARC specific section when reading an object file. This is 2935 called when bfd_section_from_shdr finds a section with an unknown 2936 type. */ 2937 2938 static bool 2939 elf32_arc_section_from_shdr (bfd *abfd, 2940 Elf_Internal_Shdr * hdr, 2941 const char *name, 2942 int shindex) 2943 { 2944 switch (hdr->sh_type) 2945 { 2946 case 0x0c: /* MWDT specific section, don't complain about it. */ 2947 case SHT_ARC_ATTRIBUTES: 2948 break; 2949 2950 default: 2951 return false; 2952 } 2953 2954 if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex)) 2955 return false; 2956 2957 return true; 2958 } 2959 2960 /* Relaxation hook. 2961 2962 These are the current relaxing opportunities available: 2963 2964 * R_ARC_GOTPC32 => R_ARC_PCREL. 2965 2966 */ 2967 2968 static bool 2969 arc_elf_relax_section (bfd *abfd, asection *sec, 2970 struct bfd_link_info *link_info, bool *again) 2971 { 2972 Elf_Internal_Shdr *symtab_hdr; 2973 Elf_Internal_Rela *internal_relocs; 2974 Elf_Internal_Rela *irel, *irelend; 2975 bfd_byte *contents = NULL; 2976 Elf_Internal_Sym *isymbuf = NULL; 2977 2978 /* Assume nothing changes. */ 2979 *again = false; 2980 2981 /* We don't have to do anything for a relocatable link, if this 2982 section does not have relocs, or if this is not a code 2983 section. */ 2984 if (bfd_link_relocatable (link_info) 2985 || sec->reloc_count == 0 2986 || (sec->flags & SEC_RELOC) == 0 2987 || (sec->flags & SEC_HAS_CONTENTS) == 0 2988 || (sec->flags & SEC_CODE) == 0) 2989 return true; 2990 2991 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2992 2993 /* Get a copy of the native relocations. */ 2994 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, 2995 link_info->keep_memory); 2996 if (internal_relocs == NULL) 2997 goto error_return; 2998 2999 /* Walk through them looking for relaxing opportunities. */ 3000 irelend = internal_relocs + sec->reloc_count; 3001 for (irel = internal_relocs; irel < irelend; irel++) 3002 { 3003 /* If this isn't something that can be relaxed, then ignore 3004 this reloc. */ 3005 if (ELF32_R_TYPE (irel->r_info) != (int) R_ARC_GOTPC32) 3006 continue; 3007 3008 /* Get the section contents if we haven't done so already. */ 3009 if (contents == NULL) 3010 { 3011 /* Get cached copy if it exists. */ 3012 if (elf_section_data (sec)->this_hdr.contents != NULL) 3013 contents = elf_section_data (sec)->this_hdr.contents; 3014 /* Go get them off disk. */ 3015 else if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 3016 goto error_return; 3017 } 3018 3019 /* Read this BFD's local symbols if we haven't done so already. */ 3020 if (isymbuf == NULL && symtab_hdr->sh_info != 0) 3021 { 3022 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 3023 if (isymbuf == NULL) 3024 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 3025 symtab_hdr->sh_info, 0, 3026 NULL, NULL, NULL); 3027 if (isymbuf == NULL) 3028 goto error_return; 3029 } 3030 3031 struct elf_link_hash_entry *htop = NULL; 3032 3033 if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info) 3034 { 3035 /* An external symbol. */ 3036 unsigned int indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; 3037 htop = elf_sym_hashes (abfd)[indx]; 3038 } 3039 3040 if (ELF32_R_TYPE (irel->r_info) == (int) R_ARC_GOTPC32 3041 && SYMBOL_REFERENCES_LOCAL (link_info, htop)) 3042 { 3043 unsigned int code; 3044 3045 /* Get the opcode. */ 3046 code = bfd_get_32_me (abfd, contents + irel->r_offset - 4); 3047 3048 /* Note that we've changed the relocs, section contents, etc. */ 3049 elf_section_data (sec)->relocs = internal_relocs; 3050 elf_section_data (sec)->this_hdr.contents = contents; 3051 symtab_hdr->contents = (unsigned char *) isymbuf; 3052 3053 /* Fix the relocation's type. */ 3054 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_ARC_PC32); 3055 3056 /* ld rA,[pcl,symbol@tgot] -> add rA,pcl,symbol@pcl. */ 3057 /* 0010 0bbb aa11 0ZZX DBBB 1111 10AA AAAA. 3058 111 00 000 0111 xx xxxx*/ 3059 code &= ~0x27307F80; 3060 BFD_ASSERT (code <= 62UL); 3061 code |= 0x27007F80; 3062 3063 /* Write back the new instruction. */ 3064 bfd_put_32_me (abfd, code, contents + irel->r_offset - 4); 3065 3066 /* The size isn't changed, don't redo. */ 3067 *again = false; 3068 } 3069 } 3070 3071 if (isymbuf != NULL 3072 && symtab_hdr->contents != (unsigned char *) isymbuf) 3073 { 3074 if (!link_info->keep_memory) 3075 free (isymbuf); 3076 else 3077 /* Cache the symbols for elf_link_input_bfd. */ 3078 symtab_hdr->contents = (unsigned char *) isymbuf; 3079 } 3080 3081 if (contents != NULL 3082 && elf_section_data (sec)->this_hdr.contents != contents) 3083 { 3084 if (!link_info->keep_memory) 3085 free (contents); 3086 else 3087 /* Cache the section contents for elf_link_input_bfd. */ 3088 elf_section_data (sec)->this_hdr.contents = contents; 3089 } 3090 3091 if (elf_section_data (sec)->relocs != internal_relocs) 3092 free (internal_relocs); 3093 3094 return true; 3095 3096 error_return: 3097 if (symtab_hdr->contents != (unsigned char *) isymbuf) 3098 free (isymbuf); 3099 if (elf_section_data (sec)->this_hdr.contents != contents) 3100 free (contents); 3101 if (elf_section_data (sec)->relocs != internal_relocs) 3102 free (internal_relocs); 3103 3104 return false; 3105 } 3106 3107 #define TARGET_LITTLE_SYM arc_elf32_le_vec 3108 #define TARGET_LITTLE_NAME "elf32-littlearc" 3109 #define TARGET_BIG_SYM arc_elf32_be_vec 3110 #define TARGET_BIG_NAME "elf32-bigarc" 3111 #define ELF_ARCH bfd_arch_arc 3112 #define ELF_TARGET_ID ARC_ELF_DATA 3113 #define ELF_MACHINE_CODE EM_ARC_COMPACT 3114 #define ELF_MACHINE_ALT1 EM_ARC_COMPACT2 3115 #define ELF_MAXPAGESIZE 0x2000 3116 3117 #define bfd_elf32_bfd_link_hash_table_create arc_elf_link_hash_table_create 3118 3119 #define bfd_elf32_bfd_merge_private_bfd_data arc_elf_merge_private_bfd_data 3120 #define bfd_elf32_bfd_reloc_type_lookup arc_elf32_bfd_reloc_type_lookup 3121 #define bfd_elf32_bfd_set_private_flags arc_elf_set_private_flags 3122 #define bfd_elf32_bfd_print_private_bfd_data arc_elf_print_private_bfd_data 3123 #define bfd_elf32_bfd_copy_private_bfd_data arc_elf_copy_private_bfd_data 3124 #define bfd_elf32_bfd_relax_section arc_elf_relax_section 3125 3126 #define elf_info_to_howto_rel arc_info_to_howto_rel 3127 #define elf_backend_object_p arc_elf_object_p 3128 #define elf_backend_final_write_processing arc_elf_final_write_processing 3129 3130 #define elf_backend_relocate_section elf_arc_relocate_section 3131 #define elf_backend_check_relocs elf_arc_check_relocs 3132 #define elf_backend_create_dynamic_sections _bfd_elf_create_dynamic_sections 3133 3134 #define elf_backend_reloc_type_class elf32_arc_reloc_type_class 3135 3136 #define elf_backend_adjust_dynamic_symbol elf_arc_adjust_dynamic_symbol 3137 #define elf_backend_finish_dynamic_symbol elf_arc_finish_dynamic_symbol 3138 3139 #define elf_backend_finish_dynamic_sections elf_arc_finish_dynamic_sections 3140 #define elf_backend_late_size_sections elf_arc_late_size_sections 3141 3142 #define elf_backend_can_gc_sections 1 3143 #define elf_backend_want_got_plt 1 3144 #define elf_backend_plt_readonly 1 3145 #define elf_backend_rela_plts_and_copies_p 1 3146 #define elf_backend_want_plt_sym 0 3147 #define elf_backend_got_header_size 12 3148 #define elf_backend_dtrel_excludes_plt 1 3149 3150 #define elf_backend_may_use_rel_p 0 3151 #define elf_backend_may_use_rela_p 1 3152 #define elf_backend_default_use_rela_p 1 3153 3154 #define elf_backend_grok_prstatus elf32_arc_grok_prstatus 3155 3156 #define elf_backend_default_execstack 0 3157 3158 #undef elf_backend_obj_attrs_vendor 3159 #define elf_backend_obj_attrs_vendor "ARC" 3160 #undef elf_backend_obj_attrs_section 3161 #define elf_backend_obj_attrs_section ".ARC.attributes" 3162 #undef elf_backend_obj_attrs_arg_type 3163 #define elf_backend_obj_attrs_arg_type elf32_arc_obj_attrs_arg_type 3164 #undef elf_backend_obj_attrs_section_type 3165 #define elf_backend_obj_attrs_section_type SHT_ARC_ATTRIBUTES 3166 #define elf_backend_obj_attrs_handle_unknown elf32_arc_obj_attrs_handle_unknown 3167 3168 #define elf_backend_section_from_shdr elf32_arc_section_from_shdr 3169 3170 #include "elf32-target.h" 3171