1/* CRIS-specific support for 32-bit ELF. 2 Copyright 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc. 3 Contributed by Axis Communications AB. 4 Written by Hans-Peter Nilsson, based on elf32-fr30.c 5 PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c. 6 7 This file is part of BFD, the Binary File Descriptor library. 8 9 This program is free software; you can redistribute it and/or modify 10 it under the terms of the GNU General Public License as published by 11 the Free Software Foundation; either version 2 of the License, or 12 (at your option) any later version. 13 14 This program is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with this program; if not, write to the Free Software 21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ 22 23#include "bfd.h" 24#include "sysdep.h" 25#include "libbfd.h" 26#include "elf-bfd.h" 27#include "elf/cris.h" 28 29/* Forward declarations. */ 30static reloc_howto_type * cris_reloc_type_lookup 31 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code)); 32 33static void cris_info_to_howto_rela 34 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); 35 36static bfd_boolean cris_elf_grok_prstatus 37 PARAMS ((bfd *abfd, Elf_Internal_Note *note)); 38 39static bfd_boolean cris_elf_grok_psinfo 40 PARAMS ((bfd *abfd, Elf_Internal_Note *note)); 41 42static bfd_boolean cris_elf_relocate_section 43 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, 44 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); 45 46static bfd_reloc_status_type cris_final_link_relocate 47 PARAMS ((reloc_howto_type *, bfd *, asection *, bfd_byte *, 48 Elf_Internal_Rela *, bfd_vma)); 49 50static bfd_boolean cris_elf_gc_sweep_hook 51 PARAMS ((bfd *, struct bfd_link_info *, asection *, 52 const Elf_Internal_Rela *)); 53 54static asection * cris_elf_gc_mark_hook 55 PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *, 56 struct elf_link_hash_entry *, Elf_Internal_Sym *)); 57 58static bfd_boolean cris_elf_object_p PARAMS ((bfd *)); 59 60static void cris_elf_final_write_processing PARAMS ((bfd *, bfd_boolean)); 61 62static bfd_boolean cris_elf_print_private_bfd_data PARAMS ((bfd *, PTR)); 63 64static bfd_boolean cris_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *)); 65 66struct elf_cris_link_hash_entry; 67static bfd_boolean elf_cris_discard_excess_dso_dynamics 68 PARAMS ((struct elf_cris_link_hash_entry *, PTR)); 69static bfd_boolean elf_cris_discard_excess_program_dynamics 70 PARAMS ((struct elf_cris_link_hash_entry *, PTR)); 71static bfd_boolean elf_cris_adjust_gotplt_to_got 72 PARAMS ((struct elf_cris_link_hash_entry *, PTR)); 73static bfd_boolean elf_cris_try_fold_plt_to_got 74 PARAMS ((struct elf_cris_link_hash_entry *, PTR)); 75static struct bfd_hash_entry *elf_cris_link_hash_newfunc 76 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *)); 77static struct bfd_link_hash_table *elf_cris_link_hash_table_create 78 PARAMS ((bfd *)); 79static bfd_boolean elf_cris_adjust_dynamic_symbol 80 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); 81static bfd_boolean cris_elf_check_relocs 82 PARAMS ((bfd *, struct bfd_link_info *, asection *, 83 const Elf_Internal_Rela *)); 84 85static bfd_boolean elf_cris_size_dynamic_sections 86 PARAMS ((bfd *, struct bfd_link_info *)); 87static bfd_boolean elf_cris_finish_dynamic_symbol 88 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, 89 Elf_Internal_Sym *)); 90static bfd_boolean elf_cris_finish_dynamic_sections 91 PARAMS ((bfd *, struct bfd_link_info *)); 92static void elf_cris_hide_symbol 93 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *, bfd_boolean)); 94static enum elf_reloc_type_class elf_cris_reloc_type_class 95 PARAMS ((const Elf_Internal_Rela *)); 96 97static reloc_howto_type cris_elf_howto_table [] = 98{ 99 /* This reloc does nothing. */ 100 HOWTO (R_CRIS_NONE, /* type */ 101 0, /* rightshift */ 102 2, /* size (0 = byte, 1 = short, 2 = long) */ 103 32, /* bitsize */ 104 FALSE, /* pc_relative */ 105 0, /* bitpos */ 106 complain_overflow_bitfield, /* complain_on_overflow */ 107 bfd_elf_generic_reloc, /* special_function */ 108 "R_CRIS_NONE", /* name */ 109 FALSE, /* partial_inplace */ 110 0, /* src_mask */ 111 0, /* dst_mask */ 112 FALSE), /* pcrel_offset */ 113 114 /* An 8 bit absolute relocation. */ 115 HOWTO (R_CRIS_8, /* type */ 116 0, /* rightshift */ 117 0, /* size (0 = byte, 1 = short, 2 = long) */ 118 8, /* bitsize */ 119 FALSE, /* pc_relative */ 120 0, /* bitpos */ 121 complain_overflow_bitfield, /* complain_on_overflow */ 122 bfd_elf_generic_reloc, /* special_function */ 123 "R_CRIS_8", /* name */ 124 FALSE, /* partial_inplace */ 125 0x0000, /* src_mask */ 126 0x00ff, /* dst_mask */ 127 FALSE), /* pcrel_offset */ 128 129 /* A 16 bit absolute relocation. */ 130 HOWTO (R_CRIS_16, /* type */ 131 0, /* rightshift */ 132 1, /* size (0 = byte, 1 = short, 2 = long) */ 133 16, /* bitsize */ 134 FALSE, /* pc_relative */ 135 0, /* bitpos */ 136 complain_overflow_bitfield, /* complain_on_overflow */ 137 bfd_elf_generic_reloc, /* special_function */ 138 "R_CRIS_16", /* name */ 139 FALSE, /* partial_inplace */ 140 0x00000000, /* src_mask */ 141 0x0000ffff, /* dst_mask */ 142 FALSE), /* pcrel_offset */ 143 144 /* A 32 bit absolute relocation. */ 145 HOWTO (R_CRIS_32, /* type */ 146 0, /* rightshift */ 147 2, /* size (0 = byte, 1 = short, 2 = long) */ 148 32, /* bitsize */ 149 FALSE, /* pc_relative */ 150 0, /* bitpos */ 151 complain_overflow_bitfield, /* complain_on_overflow */ 152 bfd_elf_generic_reloc, /* special_function */ 153 "R_CRIS_32", /* name */ 154 FALSE, /* partial_inplace */ 155 0x00000000, /* src_mask */ 156 0xffffffff, /* dst_mask */ 157 FALSE), /* pcrel_offset */ 158 159 /* An 8 bit PC-relative relocation. */ 160 HOWTO (R_CRIS_8_PCREL, /* type */ 161 0, /* rightshift */ 162 0, /* size (0 = byte, 1 = short, 2 = long) */ 163 8, /* bitsize */ 164 TRUE, /* pc_relative */ 165 0, /* bitpos */ 166 complain_overflow_bitfield, /* complain_on_overflow */ 167 bfd_elf_generic_reloc, /* special_function */ 168 "R_CRIS_8_PCREL", /* name */ 169 FALSE, /* partial_inplace */ 170 0x0000, /* src_mask */ 171 0x00ff, /* dst_mask */ 172 TRUE), /* pcrel_offset */ 173 174 /* A 16 bit PC-relative relocation. */ 175 HOWTO (R_CRIS_16_PCREL, /* type */ 176 0, /* rightshift */ 177 1, /* size (0 = byte, 1 = short, 2 = long) */ 178 16, /* bitsize */ 179 TRUE, /* pc_relative */ 180 0, /* bitpos */ 181 complain_overflow_bitfield, /* complain_on_overflow */ 182 bfd_elf_generic_reloc, /* special_function */ 183 "R_CRIS_16_PCREL", /* name */ 184 FALSE, /* partial_inplace */ 185 0x00000000, /* src_mask */ 186 0x0000ffff, /* dst_mask */ 187 TRUE), /* pcrel_offset */ 188 189 /* A 32 bit PC-relative relocation. */ 190 HOWTO (R_CRIS_32_PCREL, /* type */ 191 0, /* rightshift */ 192 2, /* size (0 = byte, 1 = short, 2 = long) */ 193 32, /* bitsize */ 194 TRUE, /* pc_relative */ 195 0, /* bitpos */ 196 complain_overflow_bitfield, /* complain_on_overflow */ 197 bfd_elf_generic_reloc, /* special_function */ 198 "R_CRIS_32_PCREL", /* name */ 199 FALSE, /* partial_inplace */ 200 0x00000000, /* src_mask */ 201 0xffffffff, /* dst_mask */ 202 TRUE), /* pcrel_offset */ 203 204 /* GNU extension to record C++ vtable hierarchy. */ 205 HOWTO (R_CRIS_GNU_VTINHERIT, /* type */ 206 0, /* rightshift */ 207 2, /* size (0 = byte, 1 = short, 2 = long) */ 208 0, /* bitsize */ 209 FALSE, /* pc_relative */ 210 0, /* bitpos */ 211 complain_overflow_dont, /* complain_on_overflow */ 212 NULL, /* special_function */ 213 "R_CRIS_GNU_VTINHERIT", /* name */ 214 FALSE, /* partial_inplace */ 215 0, /* src_mask */ 216 0, /* dst_mask */ 217 FALSE), /* pcrel_offset */ 218 219 /* GNU extension to record C++ vtable member usage. */ 220 HOWTO (R_CRIS_GNU_VTENTRY, /* type */ 221 0, /* rightshift */ 222 2, /* size (0 = byte, 1 = short, 2 = long) */ 223 0, /* bitsize */ 224 FALSE, /* pc_relative */ 225 0, /* bitpos */ 226 complain_overflow_dont, /* complain_on_overflow */ 227 _bfd_elf_rel_vtable_reloc_fn, /* special_function */ 228 "R_CRIS_GNU_VTENTRY", /* name */ 229 FALSE, /* partial_inplace */ 230 0, /* src_mask */ 231 0, /* dst_mask */ 232 FALSE), /* pcrel_offset */ 233 234 /* This is used only by the dynamic linker. The symbol should exist 235 both in the object being run and in some shared library. The 236 dynamic linker copies the data addressed by the symbol from the 237 shared library into the object, because the object being 238 run has to have the data at some particular address. */ 239 HOWTO (R_CRIS_COPY, /* type */ 240 0, /* rightshift */ 241 2, /* size (0 = byte, 1 = short, 2 = long) */ 242 32, /* bitsize */ 243 FALSE, /* pc_relative */ 244 0, /* bitpos */ 245 complain_overflow_bitfield, /* complain_on_overflow */ 246 bfd_elf_generic_reloc, /* special_function */ 247 "R_CRIS_COPY", /* name */ 248 FALSE, /* partial_inplace */ 249 0, /* src_mask */ 250 0, /* dst_mask */ 251 FALSE), /* pcrel_offset */ 252 253 /* Like R_CRIS_32, but used when setting global offset table entries. */ 254 HOWTO (R_CRIS_GLOB_DAT, /* type */ 255 0, /* rightshift */ 256 2, /* size (0 = byte, 1 = short, 2 = long) */ 257 32, /* bitsize */ 258 FALSE, /* pc_relative */ 259 0, /* bitpos */ 260 complain_overflow_bitfield, /* complain_on_overflow */ 261 bfd_elf_generic_reloc, /* special_function */ 262 "R_CRIS_GLOB_DAT", /* name */ 263 FALSE, /* partial_inplace */ 264 0, /* src_mask */ 265 0xffffffff, /* dst_mask */ 266 FALSE), /* pcrel_offset */ 267 268 /* Marks a procedure linkage table entry for a symbol. */ 269 HOWTO (R_CRIS_JUMP_SLOT, /* type */ 270 0, /* rightshift */ 271 2, /* size (0 = byte, 1 = short, 2 = long) */ 272 32, /* bitsize */ 273 FALSE, /* pc_relative */ 274 0, /* bitpos */ 275 complain_overflow_bitfield, /* complain_on_overflow */ 276 bfd_elf_generic_reloc, /* special_function */ 277 "R_CRIS_JUMP_SLOT", /* name */ 278 FALSE, /* partial_inplace */ 279 0, /* src_mask */ 280 0, /* dst_mask */ 281 FALSE), /* pcrel_offset */ 282 283 /* Used only by the dynamic linker. When the object is run, this 284 longword is set to the load address of the object, plus the 285 addend. */ 286 HOWTO (R_CRIS_RELATIVE, /* type */ 287 0, /* rightshift */ 288 2, /* size (0 = byte, 1 = short, 2 = long) */ 289 32, /* bitsize */ 290 FALSE, /* pc_relative */ 291 0, /* bitpos */ 292 complain_overflow_bitfield, /* complain_on_overflow */ 293 bfd_elf_generic_reloc, /* special_function */ 294 "R_CRIS_RELATIVE", /* name */ 295 FALSE, /* partial_inplace */ 296 0, /* src_mask */ 297 0xffffffff, /* dst_mask */ 298 FALSE), /* pcrel_offset */ 299 300 /* Like R_CRIS_32, but referring to the GOT table entry for the symbol. */ 301 HOWTO (R_CRIS_16_GOT, /* type */ 302 0, /* rightshift */ 303 1, /* size (0 = byte, 1 = short, 2 = long) */ 304 16, /* bitsize */ 305 FALSE, /* pc_relative */ 306 0, /* bitpos */ 307 complain_overflow_bitfield, /* complain_on_overflow */ 308 bfd_elf_generic_reloc, /* special_function */ 309 "R_CRIS_16_GOT", /* name */ 310 FALSE, /* partial_inplace */ 311 0, /* src_mask */ 312 0xffff, /* dst_mask */ 313 FALSE), /* pcrel_offset */ 314 315 HOWTO (R_CRIS_32_GOT, /* type */ 316 0, /* rightshift */ 317 2, /* size (0 = byte, 1 = short, 2 = long) */ 318 32, /* bitsize */ 319 FALSE, /* pc_relative */ 320 0, /* bitpos */ 321 complain_overflow_bitfield, /* complain_on_overflow */ 322 bfd_elf_generic_reloc, /* special_function */ 323 "R_CRIS_32_GOT", /* name */ 324 FALSE, /* partial_inplace */ 325 0, /* src_mask */ 326 0xffffffff, /* dst_mask */ 327 FALSE), /* pcrel_offset */ 328 329 /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of 330 the GOT table for the symbol. */ 331 HOWTO (R_CRIS_16_GOTPLT, /* type */ 332 0, /* rightshift */ 333 1, /* size (0 = byte, 1 = short, 2 = long) */ 334 16, /* bitsize */ 335 FALSE, /* pc_relative */ 336 0, /* bitpos */ 337 complain_overflow_bitfield, /* complain_on_overflow */ 338 bfd_elf_generic_reloc, /* special_function */ 339 "R_CRIS_16_GOTPLT", /* name */ 340 FALSE, /* partial_inplace */ 341 0, /* src_mask */ 342 0xffff, /* dst_mask */ 343 FALSE), /* pcrel_offset */ 344 345 HOWTO (R_CRIS_32_GOTPLT, /* type */ 346 0, /* rightshift */ 347 2, /* size (0 = byte, 1 = short, 2 = long) */ 348 32, /* bitsize */ 349 FALSE, /* pc_relative */ 350 0, /* bitpos */ 351 complain_overflow_bitfield, /* complain_on_overflow */ 352 bfd_elf_generic_reloc, /* special_function */ 353 "R_CRIS_32_GOTPLT", /* name */ 354 FALSE, /* partial_inplace */ 355 0, /* src_mask */ 356 0xffffffff, /* dst_mask */ 357 FALSE), /* pcrel_offset */ 358 359 /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should 360 be necessary. */ 361 HOWTO (R_CRIS_32_GOTREL, /* type */ 362 0, /* rightshift */ 363 2, /* size (0 = byte, 1 = short, 2 = long) */ 364 32, /* bitsize */ 365 FALSE, /* pc_relative */ 366 0, /* bitpos */ 367 complain_overflow_bitfield, /* complain_on_overflow */ 368 bfd_elf_generic_reloc, /* special_function */ 369 "R_CRIS_32_GOTREL", /* name */ 370 FALSE, /* partial_inplace */ 371 0, /* src_mask */ 372 0xffffffff, /* dst_mask */ 373 FALSE), /* pcrel_offset */ 374 375 /* A 32-bit offset from GOT to entry for this symbol in PLT and request 376 to create PLT entry for symbol. */ 377 HOWTO (R_CRIS_32_PLT_GOTREL, /* type */ 378 0, /* rightshift */ 379 2, /* size (0 = byte, 1 = short, 2 = long) */ 380 32, /* bitsize */ 381 FALSE, /* pc_relative */ 382 0, /* bitpos */ 383 complain_overflow_bitfield, /* complain_on_overflow */ 384 bfd_elf_generic_reloc, /* special_function */ 385 "R_CRIS_32_PLT_GOTREL", /* name */ 386 FALSE, /* partial_inplace */ 387 0, /* src_mask */ 388 0xffffffff, /* dst_mask */ 389 FALSE), /* pcrel_offset */ 390 391 /* A 32-bit offset from PC (location after the relocation) + addend to 392 entry for this symbol in PLT and request to create PLT entry for 393 symbol. */ 394 HOWTO (R_CRIS_32_PLT_PCREL, /* type */ 395 0, /* rightshift */ 396 2, /* size (0 = byte, 1 = short, 2 = long) */ 397 32, /* bitsize */ 398 TRUE, /* pc_relative */ 399 0, /* bitpos */ 400 complain_overflow_bitfield, /* complain_on_overflow */ 401 bfd_elf_generic_reloc, /* special_function */ 402 "R_CRIS_32_PLT_PCREL", /* name */ 403 FALSE, /* partial_inplace */ 404 0, /* src_mask */ 405 0xffffffff, /* dst_mask */ 406 TRUE) /* pcrel_offset */ 407}; 408 409/* Map BFD reloc types to CRIS ELF reloc types. */ 410 411struct cris_reloc_map 412{ 413 bfd_reloc_code_real_type bfd_reloc_val; 414 unsigned int cris_reloc_val; 415}; 416 417static const struct cris_reloc_map cris_reloc_map [] = 418{ 419 { BFD_RELOC_NONE, R_CRIS_NONE }, 420 { BFD_RELOC_8, R_CRIS_8 }, 421 { BFD_RELOC_16, R_CRIS_16 }, 422 { BFD_RELOC_32, R_CRIS_32 }, 423 { BFD_RELOC_8_PCREL, R_CRIS_8_PCREL }, 424 { BFD_RELOC_16_PCREL, R_CRIS_16_PCREL }, 425 { BFD_RELOC_32_PCREL, R_CRIS_32_PCREL }, 426 { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT }, 427 { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY }, 428 { BFD_RELOC_CRIS_COPY, R_CRIS_COPY }, 429 { BFD_RELOC_CRIS_GLOB_DAT, R_CRIS_GLOB_DAT }, 430 { BFD_RELOC_CRIS_JUMP_SLOT, R_CRIS_JUMP_SLOT }, 431 { BFD_RELOC_CRIS_RELATIVE, R_CRIS_RELATIVE }, 432 { BFD_RELOC_CRIS_16_GOT, R_CRIS_16_GOT }, 433 { BFD_RELOC_CRIS_32_GOT, R_CRIS_32_GOT }, 434 { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT }, 435 { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT }, 436 { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL }, 437 { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL }, 438 { BFD_RELOC_CRIS_32_PLT_PCREL, R_CRIS_32_PLT_PCREL } 439}; 440 441static reloc_howto_type * 442cris_reloc_type_lookup (abfd, code) 443 bfd * abfd ATTRIBUTE_UNUSED; 444 bfd_reloc_code_real_type code; 445{ 446 unsigned int i; 447 448 for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++) 449 if (cris_reloc_map [i].bfd_reloc_val == code) 450 return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val]; 451 452 return NULL; 453} 454 455/* Set the howto pointer for an CRIS ELF reloc. */ 456 457static void 458cris_info_to_howto_rela (abfd, cache_ptr, dst) 459 bfd * abfd ATTRIBUTE_UNUSED; 460 arelent * cache_ptr; 461 Elf_Internal_Rela * dst; 462{ 463 unsigned int r_type; 464 465 r_type = ELF32_R_TYPE (dst->r_info); 466 BFD_ASSERT (r_type < (unsigned int) R_CRIS_max); 467 cache_ptr->howto = & cris_elf_howto_table [r_type]; 468} 469 470/* Support for core dump NOTE sections. */ 471 472static bfd_boolean 473cris_elf_grok_prstatus (abfd, note) 474 bfd *abfd; 475 Elf_Internal_Note *note; 476{ 477 int offset; 478 size_t size; 479 480 switch (note->descsz) 481 { 482 default: 483 return FALSE; 484 485 case 214: /* Linux/CRIS */ 486 /* pr_cursig */ 487 elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12); 488 489 /* pr_pid */ 490 elf_tdata (abfd)->core_pid = bfd_get_32 (abfd, note->descdata + 22); 491 492 /* pr_reg */ 493 offset = 70; 494 size = 140; 495 496 break; 497 } 498 499 /* Make a ".reg/999" section. */ 500 return _bfd_elfcore_make_pseudosection (abfd, ".reg", 501 size, note->descpos + offset); 502} 503 504static bfd_boolean 505cris_elf_grok_psinfo (abfd, note) 506 bfd *abfd; 507 Elf_Internal_Note *note; 508{ 509 switch (note->descsz) 510 { 511 default: 512 return FALSE; 513 514 case 124: /* Linux/CRIS elf_prpsinfo */ 515 elf_tdata (abfd)->core_program 516 = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16); 517 elf_tdata (abfd)->core_command 518 = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80); 519 } 520 521 /* Note that for some reason, a spurious space is tacked 522 onto the end of the args in some (at least one anyway) 523 implementations, so strip it off if it exists. */ 524 525 { 526 char *command = elf_tdata (abfd)->core_command; 527 int n = strlen (command); 528 529 if (0 < n && command[n - 1] == ' ') 530 command[n - 1] = '\0'; 531 } 532 533 return TRUE; 534} 535 536/* The name of the dynamic interpreter. This is put in the .interp 537 section. */ 538 539#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1" 540 541/* The size in bytes of an entry in the procedure linkage table. */ 542 543#define PLT_ENTRY_SIZE 20 544 545/* The first entry in an absolute procedure linkage table looks like this. */ 546 547static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] = 548{ 549 0xfc, 0xe1, 550 0x7e, 0x7e, /* push mof. */ 551 0x7f, 0x0d, /* (dip [pc+]) */ 552 0, 0, 0, 0, /* Replaced with address of .got + 4. */ 553 0x30, 0x7a, /* move [...],mof */ 554 0x7f, 0x0d, /* (dip [pc+]) */ 555 0, 0, 0, 0, /* Replaced with address of .got + 8. */ 556 0x30, 0x09, /* jump [...] */ 557}; 558 559/* Subsequent entries in an absolute procedure linkage table look like 560 this. */ 561 562static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] = 563{ 564 0x7f, 0x0d, /* (dip [pc+]) */ 565 0, 0, 0, 0, /* Replaced with address of this symbol in .got. */ 566 0x30, 0x09, /* jump [...] */ 567 0x3f, 0x7e, /* move [pc+],mof */ 568 0, 0, 0, 0, /* Replaced with offset into relocation table. */ 569 0x2f, 0xfe, /* add.d [pc+],pc */ 570 0xec, 0xff, 571 0xff, 0xff /* Replaced with offset to start of .plt. */ 572}; 573 574/* The first entry in a PIC procedure linkage table looks like this. */ 575 576static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] = 577{ 578 0xfc, 0xe1, 0x7e, 0x7e, /* push mof */ 579 0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */ 580 0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */ 581 0, 0, 0, 0, 0, 0, 0, 0, /* Pad out to 20 bytes. */ 582}; 583 584/* Subsequent entries in a PIC procedure linkage table look like this. */ 585 586static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] = 587{ 588 0x6f, 0x0d, /* (bdap [pc+].d,r0) */ 589 0, 0, 0, 0, /* Replaced with offset of this symbol in .got. */ 590 0x30, 0x09, /* jump [...] */ 591 0x3f, 0x7e, /* move [pc+],mof */ 592 0, 0, 0, 0, /* Replaced with offset into relocation table. */ 593 0x2f, 0xfe, /* add.d [pc+],pc */ 594 0xec, 0xff, /* Replaced with offset to start of .plt. */ 595 0xff, 0xff 596}; 597 598/* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits 599 (and most other PIC/shlib stuff). Check that we don't drift away 600 without reason. 601 602 The CRIS linker, like the m68k and i386 linkers (and probably the rest 603 too) needs to keep track of the number of relocs that it decides to 604 copy in check_relocs for each symbol. This is so that it can discard 605 PC relative relocs if it doesn't need them when linking with 606 -Bsymbolic. We store the information in a field extending the regular 607 ELF linker hash table. */ 608 609/* This structure keeps track of the number of PC relative relocs we have 610 copied for a given symbol. */ 611 612struct elf_cris_pcrel_relocs_copied 613{ 614 /* Next section. */ 615 struct elf_cris_pcrel_relocs_copied *next; 616 /* A section in dynobj. */ 617 asection *section; 618 /* Number of relocs copied in this section. */ 619 bfd_size_type count; 620}; 621 622/* CRIS ELF linker hash entry. */ 623 624struct elf_cris_link_hash_entry 625{ 626 struct elf_link_hash_entry root; 627 628 /* Number of PC relative relocs copied for this symbol. */ 629 struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied; 630 631 /* The GOTPLT references are CRIS-specific; the goal is to avoid having 632 both a general GOT and a PLT-specific GOT entry for the same symbol, 633 when it is referenced both as a function and as a function pointer. 634 635 Number of GOTPLT references for a function. */ 636 bfd_signed_vma gotplt_refcount; 637 638 /* Actual GOTPLT index for this symbol, if applicable, or zero if not 639 (zero is never used as an index). FIXME: We should be able to fold 640 this with gotplt_refcount in a union, like the got and plt unions in 641 elf_link_hash_entry. */ 642 bfd_size_type gotplt_offset; 643}; 644 645/* CRIS ELF linker hash table. */ 646 647struct elf_cris_link_hash_table 648{ 649 struct elf_link_hash_table root; 650 651 /* We can't use the PLT offset and calculate to get the GOTPLT offset, 652 since we try and avoid creating GOTPLT:s when there's already a GOT. 653 Instead, we keep and update the next available index here. */ 654 bfd_size_type next_gotplt_entry; 655}; 656 657/* Traverse a CRIS ELF linker hash table. */ 658 659#define elf_cris_link_hash_traverse(table, func, info) \ 660 (elf_link_hash_traverse \ 661 (&(table)->root, \ 662 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \ 663 (info))) 664 665/* Get the CRIS ELF linker hash table from a link_info structure. */ 666 667#define elf_cris_hash_table(p) \ 668 ((struct elf_cris_link_hash_table *) (p)->hash) 669 670/* Create an entry in a CRIS ELF linker hash table. */ 671 672static struct bfd_hash_entry * 673elf_cris_link_hash_newfunc (entry, table, string) 674 struct bfd_hash_entry *entry; 675 struct bfd_hash_table *table; 676 const char *string; 677{ 678 struct elf_cris_link_hash_entry *ret = 679 (struct elf_cris_link_hash_entry *) entry; 680 681 /* Allocate the structure if it has not already been allocated by a 682 subclass. */ 683 if (ret == (struct elf_cris_link_hash_entry *) NULL) 684 ret = ((struct elf_cris_link_hash_entry *) 685 bfd_hash_allocate (table, 686 sizeof (struct elf_cris_link_hash_entry))); 687 if (ret == (struct elf_cris_link_hash_entry *) NULL) 688 return (struct bfd_hash_entry *) ret; 689 690 /* Call the allocation method of the superclass. */ 691 ret = ((struct elf_cris_link_hash_entry *) 692 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, 693 table, string)); 694 if (ret != (struct elf_cris_link_hash_entry *) NULL) 695 { 696 ret->pcrel_relocs_copied = NULL; 697 ret->gotplt_refcount = 0; 698 ret->gotplt_offset = 0; 699 } 700 701 return (struct bfd_hash_entry *) ret; 702} 703 704/* Create a CRIS ELF linker hash table. */ 705 706static struct bfd_link_hash_table * 707elf_cris_link_hash_table_create (abfd) 708 bfd *abfd; 709{ 710 struct elf_cris_link_hash_table *ret; 711 bfd_size_type amt = sizeof (struct elf_cris_link_hash_table); 712 713 ret = ((struct elf_cris_link_hash_table *) bfd_malloc (amt)); 714 if (ret == (struct elf_cris_link_hash_table *) NULL) 715 return NULL; 716 717 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd, 718 elf_cris_link_hash_newfunc)) 719 { 720 free (ret); 721 return NULL; 722 } 723 724 /* Initialize to skip over the first three entries in the gotplt; they 725 are used for run-time symbol evaluation. */ 726 ret->next_gotplt_entry = 12; 727 728 return &ret->root.root; 729} 730 731/* Perform a single relocation. By default we use the standard BFD 732 routines, with a few tweaks. */ 733 734static bfd_reloc_status_type 735cris_final_link_relocate (howto, input_bfd, input_section, contents, rel, 736 relocation) 737 reloc_howto_type * howto; 738 bfd * input_bfd; 739 asection * input_section; 740 bfd_byte * contents; 741 Elf_Internal_Rela * rel; 742 bfd_vma relocation; 743{ 744 bfd_reloc_status_type r; 745 746 /* PC-relative relocations are relative to the position *after* 747 the reloc. Note that for R_CRIS_8_PCREL the adjustment is 748 not a single byte, since PC must be 16-bit-aligned. */ 749 switch (ELF32_R_TYPE (rel->r_info)) 750 { 751 /* Check that the 16-bit GOT relocs are positive. */ 752 case R_CRIS_16_GOTPLT: 753 case R_CRIS_16_GOT: 754 if ((bfd_signed_vma) relocation < 0) 755 return bfd_reloc_overflow; 756 break; 757 758 case R_CRIS_32_PLT_PCREL: 759 case R_CRIS_32_PCREL: 760 relocation -= 2; 761 /* Fall through. */ 762 case R_CRIS_8_PCREL: 763 case R_CRIS_16_PCREL: 764 relocation -= 2; 765 break; 766 767 default: 768 break; 769 } 770 771 r = _bfd_final_link_relocate (howto, input_bfd, input_section, 772 contents, rel->r_offset, 773 relocation, rel->r_addend); 774 return r; 775} 776 777/* Relocate an CRIS ELF section. See elf32-fr30.c, from where this was 778 copied, for further comments. */ 779 780static bfd_boolean 781cris_elf_relocate_section (output_bfd, info, input_bfd, input_section, 782 contents, relocs, local_syms, local_sections) 783 bfd *output_bfd ATTRIBUTE_UNUSED; 784 struct bfd_link_info *info; 785 bfd *input_bfd; 786 asection *input_section; 787 bfd_byte *contents; 788 Elf_Internal_Rela *relocs; 789 Elf_Internal_Sym *local_syms; 790 asection **local_sections; 791{ 792 bfd *dynobj; 793 Elf_Internal_Shdr *symtab_hdr; 794 struct elf_link_hash_entry **sym_hashes; 795 bfd_vma *local_got_offsets; 796 asection *sgot; 797 asection *splt; 798 asection *sreloc; 799 Elf_Internal_Rela *rel; 800 Elf_Internal_Rela *relend; 801 802 if (info->relocatable) 803 return TRUE; 804 805 dynobj = elf_hash_table (info)->dynobj; 806 local_got_offsets = elf_local_got_offsets (input_bfd); 807 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 808 sym_hashes = elf_sym_hashes (input_bfd); 809 relend = relocs + input_section->reloc_count; 810 811 sgot = NULL; 812 splt = NULL; 813 sreloc = NULL; 814 815 if (dynobj != NULL) 816 { 817 splt = bfd_get_section_by_name (dynobj, ".plt"); 818 sgot = bfd_get_section_by_name (dynobj, ".got"); 819 } 820 821 for (rel = relocs; rel < relend; rel ++) 822 { 823 reloc_howto_type *howto; 824 unsigned long r_symndx; 825 Elf_Internal_Sym *sym; 826 asection *sec; 827 struct elf_link_hash_entry *h; 828 bfd_vma relocation; 829 bfd_reloc_status_type r; 830 const char *symname = NULL; 831 int r_type; 832 833 r_type = ELF32_R_TYPE (rel->r_info); 834 835 if ( r_type == R_CRIS_GNU_VTINHERIT 836 || r_type == R_CRIS_GNU_VTENTRY) 837 continue; 838 839 /* This is a final link. */ 840 r_symndx = ELF32_R_SYM (rel->r_info); 841 howto = cris_elf_howto_table + r_type; 842 h = NULL; 843 sym = NULL; 844 sec = NULL; 845 846 if (r_symndx < symtab_hdr->sh_info) 847 { 848 sym = local_syms + r_symndx; 849 sec = local_sections [r_symndx]; 850 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 851 852 symname = (bfd_elf_string_from_elf_section 853 (input_bfd, symtab_hdr->sh_link, sym->st_name)); 854 if (symname == NULL) 855 symname = bfd_section_name (input_bfd, sec); 856 } 857 else 858 { 859 bfd_boolean warned; 860 bfd_boolean unresolved_reloc; 861 862 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 863 r_symndx, symtab_hdr, sym_hashes, 864 h, sec, relocation, 865 unresolved_reloc, warned); 866 867 if (unresolved_reloc 868 /* Perhaps we should detect the cases that 869 sec->output_section is expected to be NULL like i386 and 870 m68k, but apparently (and according to elfxx-ia64.c) all 871 valid cases are where the symbol is defined in a shared 872 object which we link dynamically against. This includes 873 PLT relocs for which we've created a PLT entry and other 874 relocs for which we're prepared to create dynamic 875 relocations. 876 877 For now, new situations cause us to just err when 878 sec->output_offset is NULL but the object with the symbol 879 is *not* dynamically linked against. Thus this will 880 automatically remind us so we can see if there are other 881 valid cases we need to revisit. */ 882 && (sec->owner->flags & DYNAMIC) != 0) 883 relocation = 0; 884 885 else if (h->root.type == bfd_link_hash_defined 886 || h->root.type == bfd_link_hash_defweak) 887 { 888 /* Here follow the cases where the relocation value must 889 be zero (or when further handling is simplified when 890 zero). I can't claim to understand the various 891 conditions and they weren't described in the files 892 where I copied them from (elf32-m68k.c and 893 elf32-i386.c), but let's mention examples of where 894 they happen. FIXME: Perhaps define and use a 895 dynamic_symbol_p function like ia64. 896 897 - When creating a shared library, we can have an 898 ordinary relocation for a symbol defined in a shared 899 library (perhaps the one we create). We then make 900 the relocation value zero, as the value seen now will 901 be added into the relocation addend in this shared 902 library, but must be handled only at dynamic-link 903 time. FIXME: Not sure this example covers the 904 h->elf_link_hash_flags test, though it's there in 905 other targets. */ 906 if (info->shared 907 && ((! info->symbolic && h->dynindx != -1) 908 || !h->def_regular) 909 && (input_section->flags & SEC_ALLOC) != 0 910 && (r_type == R_CRIS_8 911 || r_type == R_CRIS_16 912 || r_type == R_CRIS_32 913 || r_type == R_CRIS_8_PCREL 914 || r_type == R_CRIS_16_PCREL 915 || r_type == R_CRIS_32_PCREL)) 916 relocation = 0; 917 else if (unresolved_reloc) 918 { 919 _bfd_error_handler 920 (_("%B(%A): unresolvable relocation %s against symbol `%s'"), 921 input_bfd, 922 input_section, 923 cris_elf_howto_table[r_type].name, 924 symname); 925 bfd_set_error (bfd_error_bad_value); 926 return FALSE; 927 } 928 } 929 } 930 931 switch (r_type) 932 { 933 case R_CRIS_16_GOTPLT: 934 case R_CRIS_32_GOTPLT: 935 /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT, 936 but we require a PLT, and the PLT handling will take care of 937 filling in the PLT-specific GOT entry. For the GOT offset, 938 calculate it as we do when filling it in for the .got.plt 939 section. If we don't have a PLT, punt to GOT handling. */ 940 if (h != NULL 941 && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0) 942 { 943 asection *sgotplt 944 = bfd_get_section_by_name (dynobj, ".got.plt"); 945 bfd_vma got_offset; 946 947 BFD_ASSERT (h->dynindx != -1); 948 BFD_ASSERT (sgotplt != NULL); 949 950 got_offset 951 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset; 952 953 relocation = got_offset; 954 break; 955 } 956 957 /* We didn't make a PLT entry for this symbol. Maybe everything is 958 folded into the GOT. Other than folding, this happens when 959 statically linking PIC code, or when using -Bsymbolic. Check 960 that we instead have a GOT entry as done for us by 961 elf_cris_adjust_dynamic_symbol, and drop through into the 962 ordinary GOT cases. This must not happen for the 963 executable, because any reference it does to a function 964 that is satisfied by a DSO must generate a PLT. We assume 965 these call-specific relocs don't address non-functions. */ 966 if (h != NULL 967 && (h->got.offset == (bfd_vma) -1 968 || (!info->shared 969 && !(h->def_regular 970 || (!h->def_dynamic 971 && h->root.type == bfd_link_hash_undefweak))))) 972 { 973 (*_bfd_error_handler) 974 ((h->got.offset == (bfd_vma) -1) 975 ? _("%B(%A): No PLT nor GOT for relocation %s" 976 " against symbol `%s'") 977 : _("%B(%A): No PLT for relocation %s" 978 " against symbol `%s'"), 979 input_bfd, 980 input_section, 981 cris_elf_howto_table[r_type].name, 982 (symname != NULL && symname[0] != '\0' 983 ? symname : _("[whose name is lost]"))); 984 985 /* FIXME: Perhaps blaming input is not the right thing to 986 do; this is probably an internal error. But it is true 987 that we didn't like that particular input. */ 988 bfd_set_error (bfd_error_bad_value); 989 return FALSE; 990 } 991 /* Fall through. */ 992 993 /* The size of the actual relocation is not used here; we only 994 fill in the GOT table here. */ 995 case R_CRIS_16_GOT: 996 case R_CRIS_32_GOT: 997 { 998 bfd_vma off; 999 1000 /* Note that despite using RELA relocations, the .got contents 1001 is always filled in with the link-relative relocation 1002 value; the addend. */ 1003 1004 if (h != NULL) 1005 { 1006 off = h->got.offset; 1007 BFD_ASSERT (off != (bfd_vma) -1); 1008 1009 if (!elf_hash_table (info)->dynamic_sections_created 1010 || (! info->shared 1011 && (h->def_regular 1012 || h->type == STT_FUNC 1013 || h->needs_plt)) 1014 || (info->shared 1015 && (info->symbolic || h->dynindx == -1) 1016 && h->def_regular)) 1017 { 1018 /* This wasn't checked above for ! info->shared, but 1019 must hold there if we get here; the symbol must 1020 be defined in the regular program or be undefweak 1021 or be a function or otherwise need a PLT. */ 1022 BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created 1023 || info->shared 1024 || h->def_regular 1025 || h->type == STT_FUNC 1026 || h->needs_plt 1027 || h->root.type == bfd_link_hash_undefweak); 1028 1029 /* This is actually a static link, or it is a 1030 -Bsymbolic link and the symbol is defined locally, 1031 or is undefweak, or the symbol was forced to be 1032 local because of a version file, or we're not 1033 creating a dynamic object. We must initialize this 1034 entry in the global offset table. Since the offset 1035 must always be a multiple of 4, we use the least 1036 significant bit to record whether we have 1037 initialized it already. 1038 1039 If this GOT entry should be runtime-initialized, we 1040 will create a .rela.got relocation entry to 1041 initialize the value. This is done in the 1042 finish_dynamic_symbol routine. */ 1043 if ((off & 1) != 0) 1044 off &= ~1; 1045 else 1046 { 1047 bfd_put_32 (output_bfd, relocation, 1048 sgot->contents + off); 1049 h->got.offset |= 1; 1050 } 1051 } 1052 } 1053 else 1054 { 1055 BFD_ASSERT (local_got_offsets != NULL 1056 && local_got_offsets[r_symndx] != (bfd_vma) -1); 1057 1058 off = local_got_offsets[r_symndx]; 1059 1060 /* The offset must always be a multiple of 4. We use 1061 the least significant bit to record whether we have 1062 already generated the necessary reloc. */ 1063 if ((off & 1) != 0) 1064 off &= ~1; 1065 else 1066 { 1067 bfd_put_32 (output_bfd, relocation, sgot->contents + off); 1068 1069 if (info->shared) 1070 { 1071 asection *s; 1072 Elf_Internal_Rela outrel; 1073 bfd_byte *loc; 1074 1075 s = bfd_get_section_by_name (dynobj, ".rela.got"); 1076 BFD_ASSERT (s != NULL); 1077 1078 outrel.r_offset = (sgot->output_section->vma 1079 + sgot->output_offset 1080 + off); 1081 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE); 1082 outrel.r_addend = relocation; 1083 loc = s->contents; 1084 loc += s->reloc_count++ * sizeof (Elf32_External_Rela); 1085 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 1086 } 1087 1088 local_got_offsets[r_symndx] |= 1; 1089 } 1090 } 1091 1092 relocation = sgot->output_offset + off; 1093 if (rel->r_addend != 0) 1094 { 1095 /* We can't do anything for a relocation which is against 1096 a symbol *plus offset*. GOT holds relocations for 1097 symbols. Make this an error; the compiler isn't 1098 allowed to pass us these kinds of things. */ 1099 if (h == NULL) 1100 (*_bfd_error_handler) 1101 (_("%B(%A): relocation %s with non-zero addend %d" 1102 " against local symbol"), 1103 input_bfd, 1104 input_section, 1105 cris_elf_howto_table[r_type].name, 1106 rel->r_addend); 1107 else 1108 (*_bfd_error_handler) 1109 (_("%B(%A): relocation %s with non-zero addend %d" 1110 " against symbol `%s'"), 1111 input_bfd, 1112 input_section, 1113 cris_elf_howto_table[r_type].name, 1114 rel->r_addend, 1115 symname[0] != '\0' ? symname : _("[whose name is lost]")); 1116 1117 bfd_set_error (bfd_error_bad_value); 1118 return FALSE; 1119 } 1120 } 1121 break; 1122 1123 case R_CRIS_32_GOTREL: 1124 /* This relocation must only be performed against local symbols. 1125 It's also ok when we link a program and the symbol is either 1126 defined in an ordinary (non-DSO) object or is undefined weak. */ 1127 if (h != NULL 1128 && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 1129 && !(!info->shared 1130 && (h->def_regular 1131 || (!h->def_dynamic 1132 && h->root.type == bfd_link_hash_undefweak)))) 1133 { 1134 (*_bfd_error_handler) 1135 (_("%B(%A): relocation %s is not allowed for global symbol: `%s'"), 1136 input_bfd, 1137 input_section, 1138 cris_elf_howto_table[r_type].name, 1139 symname); 1140 bfd_set_error (bfd_error_bad_value); 1141 return FALSE; 1142 } 1143 1144 /* This can happen if we get a link error with the input ELF 1145 variant mismatching the output variant. Emit an error so 1146 it's noticed if it happens elsewhere. */ 1147 if (sgot == NULL) 1148 { 1149 (*_bfd_error_handler) 1150 (_("%B: relocation %s in section %A with no GOT created"), 1151 input_bfd, 1152 input_section, 1153 cris_elf_howto_table[r_type].name); 1154 bfd_set_error (bfd_error_bad_value); 1155 return FALSE; 1156 } 1157 1158 /* This relocation is like a PC-relative one, except the 1159 reference point is the location of GOT. Note that 1160 sgot->output_offset is not involved in this calculation. We 1161 always want the start of entire .got section, not the 1162 position after the reserved header. */ 1163 relocation -= sgot->output_section->vma; 1164 break; 1165 1166 case R_CRIS_32_PLT_PCREL: 1167 /* Relocation is to the entry for this symbol in the 1168 procedure linkage table. */ 1169 1170 /* Resolve a PLT_PCREL reloc against a local symbol directly, 1171 without using the procedure linkage table. */ 1172 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 1173 break; 1174 1175 if (h->plt.offset == (bfd_vma) -1 1176 || splt == NULL) 1177 { 1178 /* We didn't make a PLT entry for this symbol. This 1179 happens when statically linking PIC code, or when 1180 using -Bsymbolic. */ 1181 break; 1182 } 1183 1184 relocation = (splt->output_section->vma 1185 + splt->output_offset 1186 + h->plt.offset); 1187 break; 1188 1189 case R_CRIS_32_PLT_GOTREL: 1190 /* Like R_CRIS_32_PLT_PCREL, but the reference point is the 1191 start of the .got section. See also comment at 1192 R_CRIS_32_GOT. */ 1193 relocation -= sgot->output_section->vma; 1194 1195 /* Resolve a PLT_GOTREL reloc against a local symbol directly, 1196 without using the procedure linkage table. */ 1197 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 1198 break; 1199 1200 if (h->plt.offset == (bfd_vma) -1 1201 || splt == NULL) 1202 { 1203 /* We didn't make a PLT entry for this symbol. This 1204 happens when statically linking PIC code, or when 1205 using -Bsymbolic. */ 1206 break; 1207 } 1208 1209 relocation = (splt->output_section->vma 1210 + splt->output_offset 1211 + h->plt.offset 1212 - sgot->output_section->vma); 1213 break; 1214 1215 case R_CRIS_8_PCREL: 1216 case R_CRIS_16_PCREL: 1217 case R_CRIS_32_PCREL: 1218 /* If the symbol was local, we need no shlib-specific handling. */ 1219 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 1220 break; 1221 1222 /* Fall through. */ 1223 case R_CRIS_8: 1224 case R_CRIS_16: 1225 case R_CRIS_32: 1226 if (info->shared 1227 && r_symndx != 0 1228 && (input_section->flags & SEC_ALLOC) != 0 1229 && ((r_type != R_CRIS_8_PCREL 1230 && r_type != R_CRIS_16_PCREL 1231 && r_type != R_CRIS_32_PCREL) 1232 || (!info->symbolic 1233 || !h->def_regular))) 1234 { 1235 Elf_Internal_Rela outrel; 1236 bfd_byte *loc; 1237 bfd_boolean skip, relocate; 1238 1239 /* When generating a shared object, these relocations 1240 are copied into the output file to be resolved at run 1241 time. */ 1242 1243 if (sreloc == NULL) 1244 { 1245 const char *name; 1246 1247 name = (bfd_elf_string_from_elf_section 1248 (input_bfd, 1249 elf_elfheader (input_bfd)->e_shstrndx, 1250 elf_section_data (input_section)->rel_hdr.sh_name)); 1251 if (name == NULL) 1252 return FALSE; 1253 1254 BFD_ASSERT (strncmp (name, ".rela", 5) == 0 1255 && strcmp (bfd_get_section_name (input_bfd, 1256 input_section), 1257 name + 5) == 0); 1258 1259 sreloc = bfd_get_section_by_name (dynobj, name); 1260 1261 /* That section should have been created in 1262 cris_elf_check_relocs, but that function will not be 1263 called for objects which fail in 1264 cris_elf_merge_private_bfd_data. */ 1265 if (sreloc == NULL) 1266 { 1267 (*_bfd_error_handler) 1268 (_("%B: Internal inconsistency; no relocation section %s"), 1269 input_bfd, 1270 name); 1271 1272 bfd_set_error (bfd_error_bad_value); 1273 return FALSE; 1274 } 1275 } 1276 1277 skip = FALSE; 1278 relocate = FALSE; 1279 1280 outrel.r_offset = 1281 _bfd_elf_section_offset (output_bfd, info, input_section, 1282 rel->r_offset); 1283 if (outrel.r_offset == (bfd_vma) -1) 1284 skip = TRUE; 1285 else if (outrel.r_offset == (bfd_vma) -2) 1286 skip = TRUE, relocate = TRUE; 1287 outrel.r_offset += (input_section->output_section->vma 1288 + input_section->output_offset); 1289 1290 if (skip) 1291 memset (&outrel, 0, sizeof outrel); 1292 /* h->dynindx may be -1 if the symbol was marked to 1293 become local. */ 1294 else if (h != NULL 1295 && ((! info->symbolic && h->dynindx != -1) 1296 || !h->def_regular)) 1297 { 1298 BFD_ASSERT (h->dynindx != -1); 1299 outrel.r_info = ELF32_R_INFO (h->dynindx, r_type); 1300 outrel.r_addend = relocation + rel->r_addend; 1301 } 1302 else 1303 { 1304 if (r_type == R_CRIS_32) 1305 { 1306 relocate = TRUE; 1307 outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE); 1308 outrel.r_addend = relocation + rel->r_addend; 1309 } 1310 else 1311 { 1312 long indx; 1313 1314 if (bfd_is_abs_section (sec)) 1315 indx = 0; 1316 else if (sec == NULL || sec->owner == NULL) 1317 { 1318 bfd_set_error (bfd_error_bad_value); 1319 return FALSE; 1320 } 1321 else 1322 { 1323 asection *osec; 1324 1325 osec = sec->output_section; 1326 indx = elf_section_data (osec)->dynindx; 1327 BFD_ASSERT (indx > 0); 1328 } 1329 1330 outrel.r_info = ELF32_R_INFO (indx, r_type); 1331 outrel.r_addend = relocation + rel->r_addend; 1332 } 1333 } 1334 1335 loc = sreloc->contents; 1336 loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela); 1337 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 1338 1339 /* This reloc will be computed at runtime, so there's no 1340 need to do anything now, except for R_CRIS_32 relocations 1341 that have been turned into R_CRIS_RELATIVE. */ 1342 if (!relocate) 1343 continue; 1344 } 1345 1346 break; 1347 } 1348 1349 r = cris_final_link_relocate (howto, input_bfd, input_section, 1350 contents, rel, relocation); 1351 1352 if (r != bfd_reloc_ok) 1353 { 1354 const char * msg = (const char *) NULL; 1355 1356 switch (r) 1357 { 1358 case bfd_reloc_overflow: 1359 r = info->callbacks->reloc_overflow 1360 (info, symname, howto->name, (bfd_vma) 0, 1361 input_bfd, input_section, rel->r_offset); 1362 break; 1363 1364 case bfd_reloc_undefined: 1365 r = info->callbacks->undefined_symbol 1366 (info, symname, input_bfd, input_section, rel->r_offset, 1367 TRUE); 1368 break; 1369 1370 case bfd_reloc_outofrange: 1371 msg = _("internal error: out of range error"); 1372 break; 1373 1374 case bfd_reloc_notsupported: 1375 msg = _("internal error: unsupported relocation error"); 1376 break; 1377 1378 case bfd_reloc_dangerous: 1379 msg = _("internal error: dangerous relocation"); 1380 break; 1381 1382 default: 1383 msg = _("internal error: unknown error"); 1384 break; 1385 } 1386 1387 if (msg) 1388 r = info->callbacks->warning 1389 (info, msg, symname, input_bfd, input_section, rel->r_offset); 1390 1391 if (! r) 1392 return FALSE; 1393 } 1394 } 1395 1396 return TRUE; 1397} 1398 1399/* Finish up dynamic symbol handling. We set the contents of various 1400 dynamic sections here. */ 1401 1402static bfd_boolean 1403elf_cris_finish_dynamic_symbol (output_bfd, info, h, sym) 1404 bfd *output_bfd; 1405 struct bfd_link_info *info; 1406 struct elf_link_hash_entry *h; 1407 Elf_Internal_Sym *sym; 1408{ 1409 bfd *dynobj; 1410 int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16; 1411 1412 dynobj = elf_hash_table (info)->dynobj; 1413 1414 if (h->plt.offset != (bfd_vma) -1) 1415 { 1416 asection *splt; 1417 asection *sgotplt; 1418 asection *sgot; 1419 asection *srela; 1420 bfd_vma got_base; 1421 1422 bfd_vma gotplt_offset 1423 = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset; 1424 Elf_Internal_Rela rela; 1425 bfd_byte *loc; 1426 bfd_boolean has_gotplt = gotplt_offset != 0; 1427 1428 /* Get the index in the procedure linkage table which 1429 corresponds to this symbol. This is the index of this symbol 1430 in all the symbols for which we are making plt entries. The 1431 first entry in the procedure linkage table is reserved. */ 1432 /* We have to count backwards here, and the result is only valid as 1433 an index into .got.plt and its relocations. FIXME: Constants... */ 1434 bfd_vma gotplt_index = gotplt_offset/4 - 3; 1435 1436 /* Get the offset into the .got table of the entry that corresponds 1437 to this function. Note that we embed knowledge that "incoming" 1438 .got goes after .got.plt in the output without padding (pointer 1439 aligned). However, that knowledge is present in several other 1440 places too. */ 1441 bfd_vma got_offset 1442 = (has_gotplt 1443 ? gotplt_offset 1444 : h->got.offset + elf_cris_hash_table(info)->next_gotplt_entry); 1445 1446 /* This symbol has an entry in the procedure linkage table. Set it 1447 up. */ 1448 1449 BFD_ASSERT (h->dynindx != -1); 1450 1451 splt = bfd_get_section_by_name (dynobj, ".plt"); 1452 sgot = bfd_get_section_by_name (dynobj, ".got"); 1453 sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); 1454 srela = bfd_get_section_by_name (dynobj, ".rela.plt"); 1455 BFD_ASSERT (splt != NULL && sgotplt != NULL 1456 && (! has_gotplt || srela != NULL)); 1457 1458 got_base = sgotplt->output_section->vma + sgotplt->output_offset; 1459 1460 /* Fill in the entry in the procedure linkage table. */ 1461 if (! info->shared) 1462 { 1463 memcpy (splt->contents + h->plt.offset, elf_cris_plt_entry, 1464 PLT_ENTRY_SIZE); 1465 1466 /* We need to enter the absolute address of the GOT entry here. */ 1467 bfd_put_32 (output_bfd, got_base + got_offset, 1468 splt->contents + h->plt.offset + plt_off1); 1469 } 1470 else 1471 { 1472 memcpy (splt->contents + h->plt.offset, elf_cris_pic_plt_entry, 1473 PLT_ENTRY_SIZE); 1474 bfd_put_32 (output_bfd, got_offset, 1475 splt->contents + h->plt.offset + plt_off1); 1476 } 1477 1478 /* Fill in the plt entry and make a relocation, if this is a "real" 1479 PLT entry. */ 1480 if (has_gotplt) 1481 { 1482 /* Fill in the offset into the reloc table. */ 1483 bfd_put_32 (output_bfd, 1484 gotplt_index * sizeof (Elf32_External_Rela), 1485 splt->contents + h->plt.offset + plt_off2); 1486 1487 /* Fill in the offset to the first PLT entry, where to "jump". */ 1488 bfd_put_32 (output_bfd, - (h->plt.offset + plt_off3 + 4), 1489 splt->contents + h->plt.offset + plt_off3); 1490 1491 /* Fill in the entry in the global offset table with the address of 1492 the relocating stub. */ 1493 bfd_put_32 (output_bfd, 1494 (splt->output_section->vma 1495 + splt->output_offset 1496 + h->plt.offset 1497 + 8), 1498 sgotplt->contents + got_offset); 1499 1500 /* Fill in the entry in the .rela.plt section. */ 1501 rela.r_offset = (sgotplt->output_section->vma 1502 + sgotplt->output_offset 1503 + got_offset); 1504 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT); 1505 rela.r_addend = 0; 1506 loc = srela->contents + gotplt_index * sizeof (Elf32_External_Rela); 1507 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); 1508 } 1509 1510 if (!h->def_regular) 1511 { 1512 /* Mark the symbol as undefined, rather than as defined in 1513 the .plt section. Leave the value alone. */ 1514 sym->st_shndx = SHN_UNDEF; 1515 1516 /* FIXME: From elf32-sparc.c 2001-02-19 (1.18). I still don't 1517 know whether resetting the value is significant; if it really 1518 is, rather than a quirk or bug in the sparc port, then I 1519 believe we'd see this elsewhere. */ 1520 /* If the symbol is weak, we do need to clear the value. 1521 Otherwise, the PLT entry would provide a definition for 1522 the symbol even if the symbol wasn't defined anywhere, 1523 and so the symbol would never be NULL. */ 1524 if (!h->ref_regular_nonweak) 1525 sym->st_value = 0; 1526 } 1527 } 1528 1529 /* For an ordinary program, we emit .got relocs only for symbols that 1530 are in the dynamic-symbols table and are either defined by the 1531 program or are undefined weak symbols, or are function symbols 1532 where we do not output a PLT: the PLT reloc was output above and all 1533 references to the function symbol are redirected to the PLT. */ 1534 if (h->got.offset != (bfd_vma) -1 1535 && (info->shared 1536 || (h->dynindx != -1 1537 && h->plt.offset == (bfd_vma) -1 1538 && !h->def_regular 1539 && h->root.type != bfd_link_hash_undefweak))) 1540 { 1541 asection *sgot; 1542 asection *srela; 1543 Elf_Internal_Rela rela; 1544 bfd_byte *loc; 1545 bfd_byte *where; 1546 1547 /* This symbol has an entry in the global offset table. Set it up. */ 1548 1549 sgot = bfd_get_section_by_name (dynobj, ".got"); 1550 srela = bfd_get_section_by_name (dynobj, ".rela.got"); 1551 BFD_ASSERT (sgot != NULL && srela != NULL); 1552 1553 rela.r_offset = (sgot->output_section->vma 1554 + sgot->output_offset 1555 + (h->got.offset &~ (bfd_vma) 1)); 1556 1557 /* If this is a static link, or it is a -Bsymbolic link and the 1558 symbol is defined locally or was forced to be local because 1559 of a version file, we just want to emit a RELATIVE reloc. 1560 The entry in the global offset table will already have been 1561 initialized in the relocate_section function. */ 1562 where = sgot->contents + (h->got.offset &~ (bfd_vma) 1); 1563 if (! elf_hash_table (info)->dynamic_sections_created 1564 || (info->shared 1565 && (info->symbolic || h->dynindx == -1) 1566 && h->def_regular)) 1567 { 1568 rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE); 1569 rela.r_addend = bfd_get_signed_32 (output_bfd, where); 1570 } 1571 else 1572 { 1573 bfd_put_32 (output_bfd, (bfd_vma) 0, where); 1574 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT); 1575 rela.r_addend = 0; 1576 } 1577 1578 loc = srela->contents; 1579 loc += srela->reloc_count++ * sizeof (Elf32_External_Rela); 1580 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); 1581 } 1582 1583 if (h->needs_copy) 1584 { 1585 asection *s; 1586 Elf_Internal_Rela rela; 1587 bfd_byte *loc; 1588 1589 /* This symbol needs a copy reloc. Set it up. */ 1590 1591 BFD_ASSERT (h->dynindx != -1 1592 && (h->root.type == bfd_link_hash_defined 1593 || h->root.type == bfd_link_hash_defweak)); 1594 1595 s = bfd_get_section_by_name (h->root.u.def.section->owner, 1596 ".rela.bss"); 1597 BFD_ASSERT (s != NULL); 1598 1599 rela.r_offset = (h->root.u.def.value 1600 + h->root.u.def.section->output_section->vma 1601 + h->root.u.def.section->output_offset); 1602 rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY); 1603 rela.r_addend = 0; 1604 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela); 1605 bfd_elf32_swap_reloca_out (output_bfd, &rela, loc); 1606 } 1607 1608 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ 1609 if (strcmp (h->root.root.string, "_DYNAMIC") == 0 1610 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) 1611 sym->st_shndx = SHN_ABS; 1612 1613 return TRUE; 1614} 1615 1616/* Finish up the dynamic sections. */ 1617 1618static bfd_boolean 1619elf_cris_finish_dynamic_sections (output_bfd, info) 1620 bfd *output_bfd; 1621 struct bfd_link_info *info; 1622{ 1623 bfd *dynobj; 1624 asection *sgot; 1625 asection *sdyn; 1626 1627 dynobj = elf_hash_table (info)->dynobj; 1628 1629 sgot = bfd_get_section_by_name (dynobj, ".got.plt"); 1630 BFD_ASSERT (sgot != NULL); 1631 sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); 1632 1633 if (elf_hash_table (info)->dynamic_sections_created) 1634 { 1635 asection *splt; 1636 Elf32_External_Dyn *dyncon, *dynconend; 1637 1638 splt = bfd_get_section_by_name (dynobj, ".plt"); 1639 BFD_ASSERT (splt != NULL && sdyn != NULL); 1640 1641 dyncon = (Elf32_External_Dyn *) sdyn->contents; 1642 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); 1643 for (; dyncon < dynconend; dyncon++) 1644 { 1645 Elf_Internal_Dyn dyn; 1646 asection *s; 1647 1648 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); 1649 1650 switch (dyn.d_tag) 1651 { 1652 default: 1653 break; 1654 1655 case DT_PLTGOT: 1656 s = bfd_get_section_by_name (output_bfd, ".got"); 1657 BFD_ASSERT (s != NULL); 1658 dyn.d_un.d_ptr = s->vma; 1659 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 1660 break; 1661 1662 case DT_JMPREL: 1663 /* Yes, we *can* have a .plt and no .plt.rela, for instance 1664 if all symbols are found in the .got (not .got.plt). */ 1665 s = bfd_get_section_by_name (output_bfd, ".rela.plt"); 1666 dyn.d_un.d_ptr = s != NULL ? s->vma : 0; 1667 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 1668 break; 1669 1670 case DT_PLTRELSZ: 1671 s = bfd_get_section_by_name (output_bfd, ".rela.plt"); 1672 if (s == NULL) 1673 dyn.d_un.d_val = 0; 1674 else 1675 dyn.d_un.d_val = s->size; 1676 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 1677 break; 1678 1679 case DT_RELASZ: 1680 /* The procedure linkage table relocs (DT_JMPREL) should 1681 not be included in the overall relocs (DT_RELA). 1682 Therefore, we override the DT_RELASZ entry here to 1683 make it not include the JMPREL relocs. Since the 1684 linker script arranges for .rela.plt to follow all 1685 other relocation sections, we don't have to worry 1686 about changing the DT_RELA entry. */ 1687 s = bfd_get_section_by_name (output_bfd, ".rela.plt"); 1688 if (s != NULL) 1689 dyn.d_un.d_val -= s->size; 1690 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 1691 break; 1692 } 1693 } 1694 1695 /* Fill in the first entry in the procedure linkage table. */ 1696 if (splt->size > 0) 1697 { 1698 if (info->shared) 1699 memcpy (splt->contents, elf_cris_pic_plt0_entry, PLT_ENTRY_SIZE); 1700 else 1701 { 1702 memcpy (splt->contents, elf_cris_plt0_entry, PLT_ENTRY_SIZE); 1703 bfd_put_32 (output_bfd, 1704 sgot->output_section->vma + sgot->output_offset + 4, 1705 splt->contents + 6); 1706 bfd_put_32 (output_bfd, 1707 sgot->output_section->vma + sgot->output_offset + 8, 1708 splt->contents + 14); 1709 1710 elf_section_data (splt->output_section)->this_hdr.sh_entsize 1711 = PLT_ENTRY_SIZE; 1712 } 1713 } 1714 } 1715 1716 /* Fill in the first three entries in the global offset table. */ 1717 if (sgot->size > 0) 1718 { 1719 if (sdyn == NULL) 1720 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents); 1721 else 1722 bfd_put_32 (output_bfd, 1723 sdyn->output_section->vma + sdyn->output_offset, 1724 sgot->contents); 1725 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4); 1726 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8); 1727 } 1728 1729 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; 1730 1731 return TRUE; 1732} 1733 1734/* Return the section that should be marked against GC for a given 1735 relocation. */ 1736 1737static asection * 1738cris_elf_gc_mark_hook (sec, info, rel, h, sym) 1739 asection *sec; 1740 struct bfd_link_info *info ATTRIBUTE_UNUSED; 1741 Elf_Internal_Rela *rel; 1742 struct elf_link_hash_entry *h; 1743 Elf_Internal_Sym *sym; 1744{ 1745 if (h != NULL) 1746 { 1747 switch (ELF32_R_TYPE (rel->r_info)) 1748 { 1749 case R_CRIS_GNU_VTINHERIT: 1750 case R_CRIS_GNU_VTENTRY: 1751 break; 1752 1753 default: 1754 switch (h->root.type) 1755 { 1756 case bfd_link_hash_defined: 1757 case bfd_link_hash_defweak: 1758 return h->root.u.def.section; 1759 1760 case bfd_link_hash_common: 1761 return h->root.u.c.p->section; 1762 1763 default: 1764 break; 1765 } 1766 } 1767 } 1768 else 1769 return bfd_section_from_elf_index (sec->owner, sym->st_shndx); 1770 1771 return NULL; 1772} 1773 1774/* Update the got entry reference counts for the section being removed. */ 1775 1776static bfd_boolean 1777cris_elf_gc_sweep_hook (abfd, info, sec, relocs) 1778 bfd *abfd ATTRIBUTE_UNUSED; 1779 struct bfd_link_info *info ATTRIBUTE_UNUSED; 1780 asection *sec ATTRIBUTE_UNUSED; 1781 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED; 1782{ 1783 Elf_Internal_Shdr *symtab_hdr; 1784 struct elf_link_hash_entry **sym_hashes; 1785 bfd_signed_vma *local_got_refcounts; 1786 const Elf_Internal_Rela *rel, *relend; 1787 bfd *dynobj; 1788 asection *sgot; 1789 asection *srelgot; 1790 1791 dynobj = elf_hash_table (info)->dynobj; 1792 if (dynobj == NULL) 1793 return TRUE; 1794 1795 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 1796 sym_hashes = elf_sym_hashes (abfd); 1797 local_got_refcounts = elf_local_got_refcounts (abfd); 1798 1799 sgot = bfd_get_section_by_name (dynobj, ".got"); 1800 srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); 1801 1802 relend = relocs + sec->reloc_count; 1803 for (rel = relocs; rel < relend; rel++) 1804 { 1805 unsigned long r_symndx; 1806 struct elf_link_hash_entry *h; 1807 1808 switch (ELF32_R_TYPE (rel->r_info)) 1809 { 1810 case R_CRIS_16_GOT: 1811 case R_CRIS_32_GOT: 1812 r_symndx = ELF32_R_SYM (rel->r_info); 1813 if (r_symndx >= symtab_hdr->sh_info) 1814 { 1815 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1816 if (h->got.refcount > 0) 1817 { 1818 --h->got.refcount; 1819 if (h->got.refcount == 0) 1820 { 1821 /* We don't need the .got entry any more. */ 1822 sgot->size -= 4; 1823 srelgot->size -= sizeof (Elf32_External_Rela); 1824 } 1825 } 1826 break; 1827 } 1828 1829 local_got_reloc: 1830 if (local_got_refcounts != NULL) 1831 { 1832 if (local_got_refcounts[r_symndx] > 0) 1833 { 1834 --local_got_refcounts[r_symndx]; 1835 if (local_got_refcounts[r_symndx] == 0) 1836 { 1837 /* We don't need the .got entry any more. */ 1838 sgot->size -= 4; 1839 if (info->shared) 1840 srelgot->size -= sizeof (Elf32_External_Rela); 1841 } 1842 } 1843 } 1844 break; 1845 1846 case R_CRIS_16_GOTPLT: 1847 case R_CRIS_32_GOTPLT: 1848 /* For local symbols, treat these like GOT relocs. */ 1849 r_symndx = ELF32_R_SYM (rel->r_info); 1850 if (r_symndx < symtab_hdr->sh_info) 1851 goto local_got_reloc; 1852 /* Fall through. */ 1853 case R_CRIS_32_PLT_GOTREL: 1854 /* FIXME: We don't garbage-collect away the .got section. */ 1855 if (local_got_refcounts != NULL) 1856 local_got_refcounts[-1]--; 1857 /* Fall through. */ 1858 case R_CRIS_8_PCREL: 1859 case R_CRIS_16_PCREL: 1860 case R_CRIS_32_PCREL: 1861 case R_CRIS_32_PLT_PCREL: 1862 r_symndx = ELF32_R_SYM (rel->r_info); 1863 if (r_symndx >= symtab_hdr->sh_info) 1864 { 1865 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1866 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT 1867 && h->plt.refcount > 0) 1868 --h->plt.refcount; 1869 } 1870 break; 1871 1872 default: 1873 break; 1874 } 1875 } 1876 1877 return TRUE; 1878} 1879 1880/* Make sure we emit a GOT entry if the symbol was supposed to have a PLT 1881 entry but we found we will not create any. Called when we find we will 1882 not have any PLT for this symbol, by for example 1883 elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link, 1884 or elf_cris_size_dynamic_sections if no dynamic sections will be 1885 created (we're only linking static objects). */ 1886 1887static bfd_boolean 1888elf_cris_adjust_gotplt_to_got (h, p) 1889 struct elf_cris_link_hash_entry *h; 1890 PTR p; 1891{ 1892 struct bfd_link_info *info = (struct bfd_link_info *) p; 1893 bfd *dynobj = elf_hash_table (info)->dynobj; 1894 1895 BFD_ASSERT (dynobj != NULL); 1896 1897 if (h->root.root.type == bfd_link_hash_warning) 1898 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link; 1899 1900 /* If nobody wanted a GOTPLT with this symbol, we're done. */ 1901 if (h->gotplt_refcount <= 0) 1902 return TRUE; 1903 1904 if (h->root.got.refcount > 0) 1905 { 1906 /* There's a GOT entry for this symbol. Just adjust the refcount. 1907 Probably not necessary at this stage, but keeping it accurate 1908 helps avoiding surprises later. */ 1909 h->root.got.refcount += h->gotplt_refcount; 1910 h->gotplt_refcount = 0; 1911 } 1912 else 1913 { 1914 /* No GOT entry for this symbol. We need to create one. */ 1915 asection *sgot = bfd_get_section_by_name (dynobj, ".got"); 1916 asection *srelgot 1917 = bfd_get_section_by_name (dynobj, ".rela.got"); 1918 1919 /* Put an accurate refcount there. */ 1920 h->root.got.refcount = h->gotplt_refcount; 1921 1922 h->gotplt_refcount = 0; 1923 1924 /* We always have a .got and a .rela.got section if there were 1925 GOTPLT relocs in input. */ 1926 BFD_ASSERT (sgot != NULL && srelgot != NULL); 1927 1928 /* Allocate space in the .got section. */ 1929 sgot->size += 4; 1930 1931 /* Allocate relocation space. */ 1932 srelgot->size += sizeof (Elf32_External_Rela); 1933 } 1934 1935 return TRUE; 1936} 1937 1938/* Try to fold PLT entries with GOT entries. There are two cases when we 1939 want to do this: 1940 1941 - When all PLT references are GOTPLT references, and there are GOT 1942 references, and this is not the executable. We don't have to 1943 generate a PLT at all. 1944 1945 - When there are both (ordinary) PLT references and GOT references, 1946 and this isn't the executable. 1947 We want to make the PLT reference use the ordinary GOT entry rather 1948 than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry, 1949 since the GOT entry will have to be resolved at startup anyway. 1950 1951 Though the latter case is handled when room for the PLT is allocated, 1952 not here. 1953 1954 By folding into the GOT, we may need a round-trip to a PLT in the 1955 executable for calls, a loss in performance. Still, losing a 1956 reloc is a win in size and at least in start-up time. 1957 1958 Note that this function is called before symbols are forced local by 1959 version scripts. The differing cases are handled by 1960 elf_cris_hide_symbol. */ 1961 1962static bfd_boolean 1963elf_cris_try_fold_plt_to_got (h, p) 1964 struct elf_cris_link_hash_entry *h; 1965 PTR p; 1966{ 1967 struct bfd_link_info *info = (struct bfd_link_info *) p; 1968 1969 /* If there are no GOT references for this symbol, we can't fold any 1970 other reference so there's nothing to do. Likewise if there are no 1971 PLT references; GOTPLT references included. */ 1972 if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0) 1973 return TRUE; 1974 1975 /* GOTPLT relocs are supposed to be included into the PLT refcount. */ 1976 BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount); 1977 1978 if (h->gotplt_refcount == h->root.plt.refcount) 1979 { 1980 /* The only PLT references are GOTPLT references, and there are GOT 1981 references. Convert PLT to GOT references. */ 1982 if (! elf_cris_adjust_gotplt_to_got (h, info)) 1983 return FALSE; 1984 1985 /* Clear the PLT references, so no PLT will be created. */ 1986 h->root.plt.offset = (bfd_vma) -1; 1987 } 1988 1989 return TRUE; 1990} 1991 1992/* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc 1993 to use a GOT entry (and create one) rather than requiring a GOTPLT 1994 entry. */ 1995 1996static void 1997elf_cris_hide_symbol (info, h, force_local) 1998 struct bfd_link_info *info; 1999 struct elf_link_hash_entry *h; 2000 bfd_boolean force_local; 2001{ 2002 elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info); 2003 2004 _bfd_elf_link_hash_hide_symbol (info, h, force_local); 2005} 2006 2007/* Adjust a symbol defined by a dynamic object and referenced by a 2008 regular object. The current definition is in some section of the 2009 dynamic object, but we're not including those sections. We have to 2010 change the definition to something the rest of the link can 2011 understand. */ 2012 2013static bfd_boolean 2014elf_cris_adjust_dynamic_symbol (info, h) 2015 struct bfd_link_info *info; 2016 struct elf_link_hash_entry *h; 2017{ 2018 bfd *dynobj; 2019 asection *s; 2020 unsigned int power_of_two; 2021 2022 dynobj = elf_hash_table (info)->dynobj; 2023 2024 /* Make sure we know what is going on here. */ 2025 BFD_ASSERT (dynobj != NULL 2026 && (h->needs_plt 2027 || h->u.weakdef != NULL 2028 || (h->def_dynamic 2029 && h->ref_regular 2030 && !h->def_regular))); 2031 2032 /* If this is a function, put it in the procedure linkage table. We 2033 will fill in the contents of the procedure linkage table later, 2034 when we know the address of the .got section. */ 2035 if (h->type == STT_FUNC 2036 || h->needs_plt) 2037 { 2038 /* If we link a program (not a DSO), we'll get rid of unnecessary 2039 PLT entries; we point to the actual symbols -- even for pic 2040 relocs, because a program built with -fpic should have the same 2041 result as one built without -fpic, specifically considering weak 2042 symbols. 2043 FIXME: m68k and i386 differ here, for unclear reasons. */ 2044 if (! info->shared 2045 && !h->def_dynamic) 2046 { 2047 /* This case can occur if we saw a PLT reloc in an input file, 2048 but the symbol was not defined by a dynamic object. In such 2049 a case, we don't actually need to build a procedure linkage 2050 table, and we can just do an absolute or PC reloc instead, or 2051 change a .got.plt index to a .got index for GOTPLT relocs. */ 2052 BFD_ASSERT (h->needs_plt); 2053 h->needs_plt = 0; 2054 h->plt.offset = (bfd_vma) -1; 2055 return 2056 elf_cris_adjust_gotplt_to_got ((struct 2057 elf_cris_link_hash_entry *) h, 2058 info); 2059 } 2060 2061 /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT; 2062 where a pointer-equivalent symbol was unimportant (i.e. more 2063 like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid 2064 of the PLT. We can't for the executable, because the GOT 2065 entries will point to the PLT there (and be constant). */ 2066 if (info->shared 2067 && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*) 2068 h, info)) 2069 return FALSE; 2070 2071 /* GC or folding may have rendered this entry unused. */ 2072 if (h->plt.refcount <= 0) 2073 { 2074 h->needs_plt = 0; 2075 h->plt.offset = (bfd_vma) -1; 2076 return TRUE; 2077 } 2078 2079 /* Make sure this symbol is output as a dynamic symbol. */ 2080 if (h->dynindx == -1) 2081 { 2082 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 2083 return FALSE; 2084 } 2085 2086 s = bfd_get_section_by_name (dynobj, ".plt"); 2087 BFD_ASSERT (s != NULL); 2088 2089 /* If this is the first .plt entry, make room for the special 2090 first entry. */ 2091 if (s->size == 0) 2092 s->size += PLT_ENTRY_SIZE; 2093 2094 /* If this symbol is not defined in a regular file, and we are 2095 not generating a shared library, then set the symbol to this 2096 location in the .plt. */ 2097 if (!info->shared 2098 && !h->def_regular) 2099 { 2100 h->root.u.def.section = s; 2101 h->root.u.def.value = s->size; 2102 } 2103 2104 /* If there's already a GOT entry, use that, not a .got.plt. A 2105 GOT field still has a reference count when we get here; it's 2106 not yet changed to an offset. We can't do this for an 2107 executable, because then the reloc associated with the PLT 2108 would get a non-PLT reloc pointing to the PLT. FIXME: Move 2109 this to elf_cris_try_fold_plt_to_got. */ 2110 if (info->shared && h->got.refcount > 0) 2111 { 2112 h->got.refcount += h->plt.refcount; 2113 2114 /* Mark the PLT offset to use the GOT entry by setting the low 2115 bit in the plt offset; it is always a multiple of 2116 pointer-size. */ 2117 BFD_ASSERT ((s->size & 3) == 0); 2118 2119 /* Change the PLT refcount to an offset. */ 2120 h->plt.offset = s->size; 2121 2122 /* By not setting gotplt_offset (i.e. it remains at 0), we signal 2123 that the got entry should be used instead. */ 2124 BFD_ASSERT (((struct elf_cris_link_hash_entry *) 2125 h)->gotplt_offset == 0); 2126 2127 /* Make room for this entry. */ 2128 s->size += PLT_ENTRY_SIZE; 2129 2130 return TRUE; 2131 } 2132 2133 /* No GOT reference for this symbol; prepare for an ordinary PLT. */ 2134 h->plt.offset = s->size; 2135 2136 /* Make room for this entry. */ 2137 s->size += PLT_ENTRY_SIZE; 2138 2139 /* We also need to make an entry in the .got.plt section, which 2140 will be placed in the .got section by the linker script. */ 2141 ((struct elf_cris_link_hash_entry *) h)->gotplt_offset 2142 = elf_cris_hash_table (info)->next_gotplt_entry; 2143 elf_cris_hash_table (info)->next_gotplt_entry += 4; 2144 2145 s = bfd_get_section_by_name (dynobj, ".got.plt"); 2146 BFD_ASSERT (s != NULL); 2147 s->size += 4; 2148 2149 /* We also need to make an entry in the .rela.plt section. */ 2150 2151 s = bfd_get_section_by_name (dynobj, ".rela.plt"); 2152 BFD_ASSERT (s != NULL); 2153 s->size += sizeof (Elf32_External_Rela); 2154 2155 return TRUE; 2156 } 2157 2158 /* Reinitialize the plt offset now that it is not used as a reference 2159 count any more. */ 2160 h->plt.offset = (bfd_vma) -1; 2161 2162 /* If this is a weak symbol, and there is a real definition, the 2163 processor independent code will have arranged for us to see the 2164 real definition first, and we can just use the same value. */ 2165 if (h->u.weakdef != NULL) 2166 { 2167 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined 2168 || h->u.weakdef->root.type == bfd_link_hash_defweak); 2169 h->root.u.def.section = h->u.weakdef->root.u.def.section; 2170 h->root.u.def.value = h->u.weakdef->root.u.def.value; 2171 return TRUE; 2172 } 2173 2174 /* This is a reference to a symbol defined by a dynamic object which 2175 is not a function. */ 2176 2177 /* If we are creating a shared library, we must presume that the 2178 only references to the symbol are via the global offset table. 2179 For such cases we need not do anything here; the relocations will 2180 be handled correctly by relocate_section. */ 2181 if (info->shared) 2182 return TRUE; 2183 2184 /* If there are no references to this symbol that do not use the 2185 GOT, we don't need to generate a copy reloc. */ 2186 if (!h->non_got_ref) 2187 return TRUE; 2188 2189 /* We must allocate the symbol in our .dynbss section, which will 2190 become part of the .bss section of the executable. There will be 2191 an entry for this symbol in the .dynsym section. The dynamic 2192 object will contain position independent code, so all references 2193 from the dynamic object to this symbol will go through the global 2194 offset table. The dynamic linker will use the .dynsym entry to 2195 determine the address it must put in the global offset table, so 2196 both the dynamic object and the regular object will refer to the 2197 same memory location for the variable. */ 2198 2199 s = bfd_get_section_by_name (dynobj, ".dynbss"); 2200 BFD_ASSERT (s != NULL); 2201 2202 /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to 2203 copy the initial value out of the dynamic object and into the 2204 runtime process image. We need to remember the offset into the 2205 .rela.bss section we are going to use. */ 2206 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0) 2207 { 2208 asection *srel; 2209 2210 srel = bfd_get_section_by_name (dynobj, ".rela.bss"); 2211 BFD_ASSERT (srel != NULL); 2212 srel->size += sizeof (Elf32_External_Rela); 2213 h->needs_copy = 1; 2214 } 2215 2216 /* Historic precedent: m68k and i386 allow max 8-byte alignment for the 2217 thing to copy; so do we. */ 2218 2219 /* We need to figure out the alignment required for this symbol. I 2220 have no idea how ELF linkers handle this. */ 2221 power_of_two = bfd_log2 (h->size); 2222 if (power_of_two > 3) 2223 power_of_two = 3; 2224 2225 /* Apply the required alignment. */ 2226 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two)); 2227 if (power_of_two > bfd_get_section_alignment (dynobj, s)) 2228 { 2229 if (!bfd_set_section_alignment (dynobj, s, power_of_two)) 2230 return FALSE; 2231 } 2232 2233 /* Define the symbol as being at this point in the section. */ 2234 h->root.u.def.section = s; 2235 h->root.u.def.value = s->size; 2236 2237 /* Increment the section size to make room for the symbol. */ 2238 s->size += h->size; 2239 2240 return TRUE; 2241} 2242 2243/* Look through the relocs for a section during the first phase. */ 2244 2245static bfd_boolean 2246cris_elf_check_relocs (abfd, info, sec, relocs) 2247 bfd *abfd; 2248 struct bfd_link_info *info; 2249 asection *sec; 2250 const Elf_Internal_Rela *relocs; 2251{ 2252 bfd *dynobj; 2253 Elf_Internal_Shdr *symtab_hdr; 2254 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; 2255 bfd_signed_vma *local_got_refcounts; 2256 const Elf_Internal_Rela *rel; 2257 const Elf_Internal_Rela *rel_end; 2258 asection *sgot; 2259 asection *srelgot; 2260 asection *sreloc; 2261 2262 if (info->relocatable) 2263 return TRUE; 2264 2265 dynobj = elf_hash_table (info)->dynobj; 2266 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2267 sym_hashes = elf_sym_hashes (abfd); 2268 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym); 2269 local_got_refcounts = elf_local_got_refcounts (abfd); 2270 2271 sgot = NULL; 2272 srelgot = NULL; 2273 sreloc = NULL; 2274 2275 if (!elf_bad_symtab (abfd)) 2276 sym_hashes_end -= symtab_hdr->sh_info; 2277 2278 rel_end = relocs + sec->reloc_count; 2279 for (rel = relocs; rel < rel_end; rel++) 2280 { 2281 struct elf_link_hash_entry *h; 2282 unsigned long r_symndx; 2283 enum elf_cris_reloc_type r_type; 2284 2285 r_symndx = ELF32_R_SYM (rel->r_info); 2286 if (r_symndx < symtab_hdr->sh_info) 2287 h = NULL; 2288 else 2289 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 2290 2291 r_type = ELF32_R_TYPE (rel->r_info); 2292 2293 /* Some relocs require linker-created sections; we need to hang them 2294 on the first input bfd we found that contained dynamic relocs. */ 2295 switch (r_type) 2296 { 2297 case R_CRIS_16_GOT: 2298 case R_CRIS_32_GOT: 2299 case R_CRIS_32_GOTREL: 2300 case R_CRIS_32_PLT_GOTREL: 2301 case R_CRIS_32_PLT_PCREL: 2302 case R_CRIS_16_GOTPLT: 2303 case R_CRIS_32_GOTPLT: 2304 if (dynobj == NULL) 2305 { 2306 elf_hash_table (info)->dynobj = dynobj = abfd; 2307 2308 /* Create the .got section, so we can assume it's always 2309 present whenever there's a dynobj. */ 2310 if (!_bfd_elf_create_got_section (dynobj, info)) 2311 return FALSE; 2312 } 2313 break; 2314 2315 default: 2316 break; 2317 } 2318 2319 /* Some relocs require a global offset table (but perhaps not a 2320 specific GOT entry). */ 2321 switch (r_type) 2322 { 2323 /* For R_CRIS_16_GOTPLT and R_CRIS_32_GOTPLT, we need a GOT 2324 entry only for local symbols. Unfortunately, we don't know 2325 until later on if there's a version script that forces the 2326 symbol local. We must have the .rela.got section in place 2327 before we know if the symbol looks global now, so we need 2328 to treat the reloc just like for R_CRIS_16_GOT and 2329 R_CRIS_32_GOT. */ 2330 case R_CRIS_16_GOTPLT: 2331 case R_CRIS_32_GOTPLT: 2332 case R_CRIS_16_GOT: 2333 case R_CRIS_32_GOT: 2334 if (srelgot == NULL 2335 && (h != NULL || info->shared)) 2336 { 2337 srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); 2338 if (srelgot == NULL) 2339 { 2340 srelgot = bfd_make_section (dynobj, ".rela.got"); 2341 if (srelgot == NULL 2342 || !bfd_set_section_flags (dynobj, srelgot, 2343 (SEC_ALLOC 2344 | SEC_LOAD 2345 | SEC_HAS_CONTENTS 2346 | SEC_IN_MEMORY 2347 | SEC_LINKER_CREATED 2348 | SEC_READONLY)) 2349 || !bfd_set_section_alignment (dynobj, srelgot, 2)) 2350 return FALSE; 2351 } 2352 } 2353 /* Fall through. */ 2354 2355 case R_CRIS_32_GOTREL: 2356 case R_CRIS_32_PLT_GOTREL: 2357 if (sgot == NULL) 2358 sgot = bfd_get_section_by_name (dynobj, ".got"); 2359 2360 if (local_got_refcounts == NULL) 2361 { 2362 bfd_size_type amt; 2363 2364 /* We use index local_got_refcounts[-1] to count all 2365 GOT-relative relocations that do not have explicit 2366 GOT entries. */ 2367 amt = symtab_hdr->sh_info + 1; 2368 amt *= sizeof (bfd_signed_vma); 2369 local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt)); 2370 if (local_got_refcounts == NULL) 2371 return FALSE; 2372 2373 local_got_refcounts++; 2374 elf_local_got_refcounts (abfd) = local_got_refcounts; 2375 } 2376 break; 2377 2378 default: 2379 break; 2380 } 2381 2382 switch (r_type) 2383 { 2384 case R_CRIS_16_GOTPLT: 2385 case R_CRIS_32_GOTPLT: 2386 /* Mark that we need a GOT entry if the PLT entry (and its GOT 2387 entry) is eliminated. We can only do this for a non-local 2388 symbol. */ 2389 if (h != NULL) 2390 { 2391 ((struct elf_cris_link_hash_entry *) h)->gotplt_refcount++; 2392 goto handle_gotplt_reloc; 2393 } 2394 /* If h is NULL then this is a local symbol, and we must make a 2395 GOT entry for it, so handle it like a GOT reloc. */ 2396 /* Fall through. */ 2397 2398 case R_CRIS_16_GOT: 2399 case R_CRIS_32_GOT: 2400 /* This symbol requires a global offset table entry. */ 2401 if (h != NULL) 2402 { 2403 if (h->got.refcount == 0) 2404 { 2405 /* Make sure this symbol is output as a dynamic symbol. */ 2406 if (h->dynindx == -1) 2407 { 2408 if (!bfd_elf_link_record_dynamic_symbol (info, h)) 2409 return FALSE; 2410 } 2411 2412 /* Allocate space in the .got section. */ 2413 sgot->size += 4; 2414 /* Allocate relocation space. */ 2415 srelgot->size += sizeof (Elf32_External_Rela); 2416 } 2417 h->got.refcount++; 2418 } 2419 else 2420 { 2421 /* This is a global offset table entry for a local symbol. */ 2422 if (local_got_refcounts[r_symndx] == 0) 2423 { 2424 sgot->size += 4; 2425 if (info->shared) 2426 { 2427 /* If we are generating a shared object, we need to 2428 output a R_CRIS_RELATIVE reloc so that the dynamic 2429 linker can adjust this GOT entry. */ 2430 srelgot->size += sizeof (Elf32_External_Rela); 2431 } 2432 } 2433 local_got_refcounts[r_symndx]++; 2434 } 2435 break; 2436 2437 case R_CRIS_32_GOTREL: 2438 /* This reference requires a global offset table. 2439 FIXME: The actual refcount isn't used currently; the .got 2440 section can't be removed if there were any references in the 2441 input. */ 2442 local_got_refcounts[-1]++; 2443 break; 2444 2445 handle_gotplt_reloc: 2446 2447 case R_CRIS_32_PLT_GOTREL: 2448 /* This reference requires a global offset table. */ 2449 local_got_refcounts[-1]++; 2450 /* Fall through. */ 2451 2452 case R_CRIS_32_PLT_PCREL: 2453 /* This symbol requires a procedure linkage table entry. We 2454 actually build the entry in adjust_dynamic_symbol, 2455 because this might be a case of linking PIC code which is 2456 never referenced by a dynamic object, in which case we 2457 don't need to generate a procedure linkage table entry 2458 after all. */ 2459 2460 /* Beware: if we'd check for visibility of the symbol here 2461 (and not marking the need for a PLT when non-visible), we'd 2462 get into trouble with keeping handling consistent with 2463 regards to relocs found before definition and GOTPLT 2464 handling. Eliminable PLT entries will be dealt with later 2465 anyway. */ 2466 if (h == NULL) 2467 continue; 2468 2469 h->needs_plt = 1; 2470 h->plt.refcount++; 2471 break; 2472 2473 case R_CRIS_8: 2474 case R_CRIS_16: 2475 case R_CRIS_32: 2476 /* Let's help debug shared library creation. Any of these 2477 relocs can be used in shared libs, but pages containing them 2478 cannot be shared. Don't warn for sections we don't care 2479 about, such as debug sections or non-constant sections. We 2480 can't help tables of (global) function pointers, for example, 2481 though they must be emitted in a data section to avoid having 2482 impure text sections. */ 2483 if (info->shared 2484 && (sec->flags & SEC_ALLOC) != 0 2485 && (sec->flags & SEC_READONLY) != 0) 2486 { 2487 /* FIXME: How do we make this optionally a warning only? */ 2488 (*_bfd_error_handler) 2489 (_("%B, section %A:\n relocation %s should not be used in a shared object; recompile with -fPIC"), 2490 abfd, 2491 sec, 2492 cris_elf_howto_table[r_type].name); 2493 } 2494 /* Fall through. */ 2495 2496 case R_CRIS_8_PCREL: 2497 case R_CRIS_16_PCREL: 2498 case R_CRIS_32_PCREL: 2499 if (h != NULL) 2500 { 2501 h->non_got_ref = 1; 2502 2503 /* Make sure a plt entry is created for this symbol if it 2504 turns out to be a function defined by a dynamic object. */ 2505 if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT) 2506 h->plt.refcount++; 2507 } 2508 2509 /* If we are creating a shared library and this is not a local 2510 symbol, we need to copy the reloc into the shared library. 2511 However when linking with -Bsymbolic and this is a global 2512 symbol which is defined in an object we are including in the 2513 link (i.e., DEF_REGULAR is set), then we can resolve the 2514 reloc directly. At this point we have not seen all the input 2515 files, so it is possible that DEF_REGULAR is not set now but 2516 will be set later (it is never cleared). In case of a weak 2517 definition, DEF_REGULAR may be cleared later by a strong 2518 definition in a shared library. We account for that 2519 possibility below by storing information in the relocs_copied 2520 field of the hash table entry. A similar situation occurs 2521 when creating shared libraries and symbol visibility changes 2522 render the symbol local. */ 2523 2524 /* No need to do anything if we're not creating a shared object. */ 2525 if (! info->shared) 2526 break; 2527 2528 /* We don't need to handle relocs into sections not going into 2529 the "real" output. */ 2530 if ((sec->flags & SEC_ALLOC) == 0) 2531 break; 2532 2533 /* We can only eliminate PC-relative relocs. */ 2534 if (r_type == R_CRIS_8_PCREL 2535 || r_type == R_CRIS_16_PCREL 2536 || r_type == R_CRIS_32_PCREL) 2537 { 2538 /* If the symbol is local, then we can eliminate the reloc. */ 2539 if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT) 2540 break; 2541 2542 /* If this is with -Bsymbolic and the symbol isn't weak, and 2543 is defined by an ordinary object (the ones we include in 2544 this shared library) then we can also eliminate the 2545 reloc. See comment above for more eliminable cases which 2546 we can't identify at this time. */ 2547 if (info->symbolic 2548 && h->root.type != bfd_link_hash_defweak 2549 && h->def_regular) 2550 break; 2551 } 2552 2553 /* We create a reloc section in dynobj and make room for this 2554 reloc. */ 2555 if (sreloc == NULL) 2556 { 2557 const char *name; 2558 2559 name = (bfd_elf_string_from_elf_section 2560 (abfd, 2561 elf_elfheader (abfd)->e_shstrndx, 2562 elf_section_data (sec)->rel_hdr.sh_name)); 2563 if (name == NULL) 2564 return FALSE; 2565 2566 BFD_ASSERT (strncmp (name, ".rela", 5) == 0 2567 && strcmp (bfd_get_section_name (abfd, sec), 2568 name + 5) == 0); 2569 2570 sreloc = bfd_get_section_by_name (dynobj, name); 2571 if (sreloc == NULL) 2572 { 2573 sreloc = bfd_make_section (dynobj, name); 2574 if (sreloc == NULL 2575 || !bfd_set_section_flags (dynobj, sreloc, 2576 (SEC_ALLOC 2577 | SEC_LOAD 2578 | SEC_HAS_CONTENTS 2579 | SEC_IN_MEMORY 2580 | SEC_LINKER_CREATED 2581 | SEC_READONLY)) 2582 || !bfd_set_section_alignment (dynobj, sreloc, 2)) 2583 return FALSE; 2584 } 2585 if (sec->flags & SEC_READONLY) 2586 info->flags |= DF_TEXTREL; 2587 } 2588 2589 sreloc->size += sizeof (Elf32_External_Rela); 2590 2591 /* If we are linking with -Bsymbolic, we count the number of PC 2592 relative relocations we have entered for this symbol, so that 2593 we can discard them again if the symbol is later defined by a 2594 regular object. We know that h is really a pointer to an 2595 elf_cris_link_hash_entry. */ 2596 if ((r_type == R_CRIS_8_PCREL 2597 || r_type == R_CRIS_16_PCREL 2598 || r_type == R_CRIS_32_PCREL) 2599 && info->symbolic) 2600 { 2601 struct elf_cris_link_hash_entry *eh; 2602 struct elf_cris_pcrel_relocs_copied *p; 2603 2604 eh = (struct elf_cris_link_hash_entry *) h; 2605 2606 for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next) 2607 if (p->section == sreloc) 2608 break; 2609 2610 if (p == NULL) 2611 { 2612 p = ((struct elf_cris_pcrel_relocs_copied *) 2613 bfd_alloc (dynobj, (bfd_size_type) sizeof *p)); 2614 if (p == NULL) 2615 return FALSE; 2616 p->next = eh->pcrel_relocs_copied; 2617 eh->pcrel_relocs_copied = p; 2618 p->section = sreloc; 2619 p->count = 0; 2620 } 2621 2622 ++p->count; 2623 } 2624 break; 2625 2626 /* This relocation describes the C++ object vtable hierarchy. 2627 Reconstruct it for later use during GC. */ 2628 case R_CRIS_GNU_VTINHERIT: 2629 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) 2630 return FALSE; 2631 break; 2632 2633 /* This relocation describes which C++ vtable entries are actually 2634 used. Record for later use during GC. */ 2635 case R_CRIS_GNU_VTENTRY: 2636 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) 2637 return FALSE; 2638 break; 2639 2640 default: 2641 /* Other relocs do not appear here. */ 2642 bfd_set_error (bfd_error_bad_value); 2643 return FALSE; 2644 } 2645 } 2646 2647 return TRUE; 2648} 2649 2650/* Set the sizes of the dynamic sections. */ 2651 2652static bfd_boolean 2653elf_cris_size_dynamic_sections (output_bfd, info) 2654 bfd *output_bfd ATTRIBUTE_UNUSED; 2655 struct bfd_link_info *info; 2656{ 2657 bfd *dynobj; 2658 asection *s; 2659 bfd_boolean plt; 2660 bfd_boolean relocs; 2661 2662 dynobj = elf_hash_table (info)->dynobj; 2663 BFD_ASSERT (dynobj != NULL); 2664 2665 if (elf_hash_table (info)->dynamic_sections_created) 2666 { 2667 /* Set the contents of the .interp section to the interpreter. */ 2668 if (info->executable) 2669 { 2670 s = bfd_get_section_by_name (dynobj, ".interp"); 2671 BFD_ASSERT (s != NULL); 2672 s->size = sizeof ELF_DYNAMIC_INTERPRETER; 2673 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; 2674 } 2675 } 2676 else 2677 { 2678 /* Adjust all expected GOTPLT uses to use a GOT entry instead. */ 2679 elf_cris_link_hash_traverse (elf_cris_hash_table (info), 2680 elf_cris_adjust_gotplt_to_got, 2681 (PTR) info); 2682 2683 /* We may have created entries in the .rela.got section. 2684 However, if we are not creating the dynamic sections, we will 2685 not actually use these entries. Reset the size of .rela.got, 2686 which will cause it to get stripped from the output file 2687 below. */ 2688 s = bfd_get_section_by_name (dynobj, ".rela.got"); 2689 if (s != NULL) 2690 s->size = 0; 2691 } 2692 2693 /* If this is a -Bsymbolic shared link, then we need to discard all PC 2694 relative relocs against symbols defined in a regular object. We 2695 allocated space for them in the check_relocs routine, but we will not 2696 fill them in in the relocate_section routine. We also discard space 2697 for relocs that have become for local symbols due to symbol 2698 visibility changes. For programs, we discard space for relocs for 2699 symbols not referenced by any dynamic object. */ 2700 if (info->shared) 2701 elf_cris_link_hash_traverse (elf_cris_hash_table (info), 2702 elf_cris_discard_excess_dso_dynamics, 2703 (PTR) info); 2704 else 2705 elf_cris_link_hash_traverse (elf_cris_hash_table (info), 2706 elf_cris_discard_excess_program_dynamics, 2707 (PTR) info); 2708 2709 /* The check_relocs and adjust_dynamic_symbol entry points have 2710 determined the sizes of the various dynamic sections. Allocate 2711 memory for them. */ 2712 plt = FALSE; 2713 relocs = FALSE; 2714 for (s = dynobj->sections; s != NULL; s = s->next) 2715 { 2716 const char *name; 2717 bfd_boolean strip; 2718 2719 if ((s->flags & SEC_LINKER_CREATED) == 0) 2720 continue; 2721 2722 /* It's OK to base decisions on the section name, because none 2723 of the dynobj section names depend upon the input files. */ 2724 name = bfd_get_section_name (dynobj, s); 2725 2726 strip = FALSE; 2727 2728 if (strcmp (name, ".plt") == 0) 2729 { 2730 if (s->size == 0) 2731 { 2732 /* Strip this section if we don't need it; see the 2733 comment below. */ 2734 strip = TRUE; 2735 } 2736 else 2737 { 2738 /* Remember whether there is a PLT. */ 2739 plt = TRUE; 2740 } 2741 } 2742 else if (strncmp (name, ".rela", 5) == 0) 2743 { 2744 if (s->size == 0) 2745 { 2746 /* If we don't need this section, strip it from the 2747 output file. This is mostly to handle .rela.bss and 2748 .rela.plt. We must create both sections in 2749 create_dynamic_sections, because they must be created 2750 before the linker maps input sections to output 2751 sections. The linker does that before 2752 adjust_dynamic_symbol is called, and it is that 2753 function which decides whether anything needs to go 2754 into these sections. */ 2755 strip = TRUE; 2756 } 2757 else 2758 { 2759 /* Remember whether there are any reloc sections other 2760 than .rela.plt. */ 2761 if (strcmp (name, ".rela.plt") != 0) 2762 relocs = TRUE; 2763 2764 /* We use the reloc_count field as a counter if we need 2765 to copy relocs into the output file. */ 2766 s->reloc_count = 0; 2767 } 2768 } 2769 else if (strncmp (name, ".got", 4) != 0) 2770 { 2771 /* It's not one of our sections, so don't allocate space. */ 2772 continue; 2773 } 2774 2775 if (strip) 2776 { 2777 _bfd_strip_section_from_output (info, s); 2778 continue; 2779 } 2780 2781 /* Allocate memory for the section contents. We use bfd_zalloc here 2782 in case unused entries are not reclaimed before the section's 2783 contents are written out. This should not happen, but this way 2784 if it does, we will not write out garbage. For reloc sections, 2785 this will make entries have the type R_CRIS_NONE. */ 2786 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size); 2787 if (s->contents == NULL && s->size != 0) 2788 return FALSE; 2789 } 2790 2791 if (elf_hash_table (info)->dynamic_sections_created) 2792 { 2793 /* Add some entries to the .dynamic section. We fill in the 2794 values later, in elf_cris_finish_dynamic_sections, but we 2795 must add the entries now so that we get the correct size for 2796 the .dynamic section. The DT_DEBUG entry is filled in by the 2797 dynamic linker and used by the debugger. */ 2798#define add_dynamic_entry(TAG, VAL) \ 2799 _bfd_elf_add_dynamic_entry (info, TAG, VAL) 2800 2801 if (!info->shared) 2802 { 2803 if (!add_dynamic_entry (DT_DEBUG, 0)) 2804 return FALSE; 2805 } 2806 2807 if (plt) 2808 { 2809 if (!add_dynamic_entry (DT_PLTGOT, 0) 2810 || !add_dynamic_entry (DT_PLTRELSZ, 0) 2811 || !add_dynamic_entry (DT_PLTREL, DT_RELA) 2812 || !add_dynamic_entry (DT_JMPREL, 0)) 2813 return FALSE; 2814 } 2815 2816 if (relocs) 2817 { 2818 if (!add_dynamic_entry (DT_RELA, 0) 2819 || !add_dynamic_entry (DT_RELASZ, 0) 2820 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))) 2821 return FALSE; 2822 } 2823 2824 if ((info->flags & DF_TEXTREL) != 0) 2825 { 2826 if (!add_dynamic_entry (DT_TEXTREL, 0)) 2827 return FALSE; 2828 info->flags |= DF_TEXTREL; 2829 } 2830 } 2831#undef add_dynamic_entry 2832 2833 return TRUE; 2834} 2835 2836/* This function is called via elf_cris_link_hash_traverse if we are 2837 creating a shared object. In the -Bsymbolic case, it discards the 2838 space allocated to copy PC relative relocs against symbols which 2839 are defined in regular objects. For the normal non-symbolic case, 2840 we also discard space for relocs that have become local due to 2841 symbol visibility changes. We allocated space for them in the 2842 check_relocs routine, but we won't fill them in in the 2843 relocate_section routine. */ 2844 2845static bfd_boolean 2846elf_cris_discard_excess_dso_dynamics (h, inf) 2847 struct elf_cris_link_hash_entry *h; 2848 PTR inf; 2849{ 2850 struct elf_cris_pcrel_relocs_copied *s; 2851 struct bfd_link_info *info = (struct bfd_link_info *) inf; 2852 2853 if (h->root.root.type == bfd_link_hash_warning) 2854 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link; 2855 2856 /* If a symbol has been forced local or we have found a regular 2857 definition for the symbolic link case, then we won't be needing 2858 any relocs. */ 2859 if (h->root.def_regular 2860 && (h->root.forced_local 2861 || info->symbolic)) 2862 { 2863 for (s = h->pcrel_relocs_copied; s != NULL; s = s->next) 2864 s->section->size -= s->count * sizeof (Elf32_External_Rela); 2865 } 2866 2867 return TRUE; 2868} 2869 2870/* This function is called via elf_cris_link_hash_traverse if we are *not* 2871 creating a shared object. We discard space for relocs for symbols put 2872 in the .got, but which we found we do not have to resolve at run-time. */ 2873 2874static bfd_boolean 2875elf_cris_discard_excess_program_dynamics (h, inf) 2876 struct elf_cris_link_hash_entry *h; 2877 PTR inf; 2878{ 2879 struct bfd_link_info *info = (struct bfd_link_info *) inf; 2880 2881 if (h->root.root.type == bfd_link_hash_warning) 2882 h = (struct elf_cris_link_hash_entry *) h->root.root.u.i.link; 2883 2884 /* If we're not creating a shared library and have a symbol which is 2885 referred to by .got references, but the symbol is defined locally, 2886 (or rather, not defined by a DSO) then lose the reloc for the .got 2887 (don't allocate room for it). Likewise for relocs for something 2888 for which we create a PLT. */ 2889 if (!h->root.def_dynamic 2890 || h->root.plt.refcount > 0) 2891 { 2892 if (h->root.got.refcount > 0 2893 /* The size of this section is only valid and in sync with the 2894 various reference counts if we do dynamic; don't decrement it 2895 otherwise. */ 2896 && elf_hash_table (info)->dynamic_sections_created) 2897 { 2898 bfd *dynobj = elf_hash_table (info)->dynobj; 2899 asection *srelgot; 2900 2901 BFD_ASSERT (dynobj != NULL); 2902 2903 srelgot = bfd_get_section_by_name (dynobj, ".rela.got"); 2904 2905 BFD_ASSERT (srelgot != NULL); 2906 2907 srelgot->size -= sizeof (Elf32_External_Rela); 2908 } 2909 2910 /* If the locally-defined symbol isn't used by a DSO, then we don't 2911 have to export it as a dynamic symbol. This was already done for 2912 functions; doing this for all symbols would presumably not 2913 introduce new problems. Of course we don't do this if we're 2914 exporting all dynamic symbols. */ 2915 if (! info->export_dynamic 2916 && !h->root.def_dynamic 2917 && !h->root.ref_dynamic) 2918 { 2919 h->root.dynindx = -1; 2920 _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr, 2921 h->root.dynstr_index); 2922 } 2923 } 2924 2925 return TRUE; 2926} 2927 2928/* Reject a file depending on presence and expectation of prefixed 2929 underscores on symbols. */ 2930 2931static bfd_boolean 2932cris_elf_object_p (abfd) 2933 bfd *abfd; 2934{ 2935 if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)) 2936 return (bfd_get_symbol_leading_char (abfd) == '_'); 2937 else 2938 return (bfd_get_symbol_leading_char (abfd) == 0); 2939} 2940 2941/* Mark presence or absence of leading underscore. */ 2942 2943static void 2944cris_elf_final_write_processing (abfd, linker) 2945 bfd *abfd; 2946 bfd_boolean linker ATTRIBUTE_UNUSED; 2947{ 2948 if (bfd_get_symbol_leading_char (abfd) == '_') 2949 elf_elfheader (abfd)->e_flags |= EF_CRIS_UNDERSCORE; 2950 else 2951 elf_elfheader (abfd)->e_flags &= ~EF_CRIS_UNDERSCORE; 2952} 2953 2954/* Display the flags field. */ 2955 2956static bfd_boolean 2957cris_elf_print_private_bfd_data (abfd, ptr) 2958 bfd *abfd; 2959 PTR ptr; 2960{ 2961 FILE *file = (FILE *) ptr; 2962 2963 BFD_ASSERT (abfd != NULL && ptr != NULL); 2964 2965 _bfd_elf_print_private_bfd_data (abfd, ptr); 2966 2967 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags); 2968 2969 if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE) 2970 fprintf (file, _(" [symbols have a _ prefix]")); 2971 2972 fputc ('\n', file); 2973 return TRUE; 2974} 2975 2976/* Don't mix files with and without a leading underscore. */ 2977 2978static bfd_boolean 2979cris_elf_merge_private_bfd_data (ibfd, obfd) 2980 bfd *ibfd; 2981 bfd *obfd; 2982{ 2983 flagword old_flags, new_flags; 2984 2985 if (! _bfd_generic_verify_endian_match (ibfd, obfd)) 2986 return FALSE; 2987 2988 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 2989 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 2990 return TRUE; 2991 2992 if (! elf_flags_init (obfd)) 2993 { 2994 /* This happens when ld starts out with a 'blank' output file. */ 2995 elf_flags_init (obfd) = TRUE; 2996 2997 /* Set flags according to current bfd_target. */ 2998 cris_elf_final_write_processing (obfd, FALSE); 2999 } 3000 3001 old_flags = elf_elfheader (obfd)->e_flags; 3002 new_flags = elf_elfheader (ibfd)->e_flags; 3003 3004 /* Is this good or bad? We'll follow with other excluding flags. */ 3005 if ((old_flags & EF_CRIS_UNDERSCORE) != (new_flags & EF_CRIS_UNDERSCORE)) 3006 { 3007 (*_bfd_error_handler) 3008 ((new_flags & EF_CRIS_UNDERSCORE) 3009 ? _("%B: uses _-prefixed symbols, but writing file with non-prefixed symbols") 3010 : _("%B: uses non-prefixed symbols, but writing file with _-prefixed symbols"), 3011 ibfd); 3012 bfd_set_error (bfd_error_bad_value); 3013 return FALSE; 3014 } 3015 3016 return TRUE; 3017} 3018 3019 3020static enum elf_reloc_type_class 3021elf_cris_reloc_type_class (rela) 3022 const Elf_Internal_Rela *rela; 3023{ 3024 switch ((int) ELF32_R_TYPE (rela->r_info)) 3025 { 3026 case R_CRIS_RELATIVE: 3027 return reloc_class_relative; 3028 case R_CRIS_JUMP_SLOT: 3029 return reloc_class_plt; 3030 case R_CRIS_COPY: 3031 return reloc_class_copy; 3032 default: 3033 return reloc_class_normal; 3034 } 3035} 3036 3037#define ELF_ARCH bfd_arch_cris 3038#define ELF_MACHINE_CODE EM_CRIS 3039#define ELF_MAXPAGESIZE 0x2000 3040 3041#define TARGET_LITTLE_SYM bfd_elf32_cris_vec 3042#define TARGET_LITTLE_NAME "elf32-cris" 3043#define elf_symbol_leading_char 0 3044 3045#define elf_info_to_howto_rel NULL 3046#define elf_info_to_howto cris_info_to_howto_rela 3047#define elf_backend_relocate_section cris_elf_relocate_section 3048#define elf_backend_gc_mark_hook cris_elf_gc_mark_hook 3049#define elf_backend_gc_sweep_hook cris_elf_gc_sweep_hook 3050#define elf_backend_check_relocs cris_elf_check_relocs 3051#define elf_backend_grok_prstatus cris_elf_grok_prstatus 3052#define elf_backend_grok_psinfo cris_elf_grok_psinfo 3053 3054#define elf_backend_can_gc_sections 1 3055#define elf_backend_can_refcount 1 3056 3057#define elf_backend_object_p cris_elf_object_p 3058#define elf_backend_final_write_processing \ 3059 cris_elf_final_write_processing 3060#define bfd_elf32_bfd_print_private_bfd_data \ 3061 cris_elf_print_private_bfd_data 3062#define bfd_elf32_bfd_merge_private_bfd_data \ 3063 cris_elf_merge_private_bfd_data 3064 3065#define bfd_elf32_bfd_reloc_type_lookup cris_reloc_type_lookup 3066 3067#define bfd_elf32_bfd_link_hash_table_create \ 3068 elf_cris_link_hash_table_create 3069#define elf_backend_adjust_dynamic_symbol \ 3070 elf_cris_adjust_dynamic_symbol 3071#define elf_backend_size_dynamic_sections \ 3072 elf_cris_size_dynamic_sections 3073#define elf_backend_finish_dynamic_symbol \ 3074 elf_cris_finish_dynamic_symbol 3075#define elf_backend_finish_dynamic_sections \ 3076 elf_cris_finish_dynamic_sections 3077#define elf_backend_create_dynamic_sections \ 3078 _bfd_elf_create_dynamic_sections 3079#define bfd_elf32_bfd_final_link \ 3080 bfd_elf_gc_common_final_link 3081#define elf_backend_hide_symbol elf_cris_hide_symbol 3082#define elf_backend_reloc_type_class elf_cris_reloc_type_class 3083 3084#define elf_backend_want_got_plt 1 3085#define elf_backend_plt_readonly 1 3086#define elf_backend_want_plt_sym 0 3087#define elf_backend_got_header_size 12 3088 3089/* Later, we my want to optimize RELA entries into REL entries for dynamic 3090 linking and libraries (if it's a win of any significance). Until then, 3091 take the easy route. */ 3092#define elf_backend_may_use_rel_p 0 3093#define elf_backend_may_use_rela_p 1 3094#define elf_backend_rela_normal 1 3095 3096#include "elf32-target.h" 3097 3098#define INCLUDED_TARGET_FILE 3099 3100#undef TARGET_LITTLE_SYM 3101#undef TARGET_LITTLE_NAME 3102#undef elf_symbol_leading_char 3103 3104#define TARGET_LITTLE_SYM bfd_elf32_us_cris_vec 3105#define TARGET_LITTLE_NAME "elf32-us-cris" 3106#define elf_symbol_leading_char '_' 3107 3108#include "elf32-target.h" 3109