1 /* Meta support for 32-bit ELF 2 Copyright (C) 2013-2014 Free Software Foundation, Inc. 3 Contributed by Imagination Technologies Ltd. 4 5 This file is part of BFD, the Binary File Descriptor library. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 20 MA 02110-1301, USA. */ 21 22 #include "sysdep.h" 23 #include "bfd.h" 24 #include "libbfd.h" 25 #include "elf-bfd.h" 26 #include "elf32-metag.h" 27 #include "elf/metag.h" 28 29 #define GOT_ENTRY_SIZE 4 30 #define ELF_DYNAMIC_INTERPRETER "/lib/ld-uClibc.so.0" 31 32 /* ABI version: 33 0 - original 34 1 - with GOT offset */ 35 #define METAG_ELF_ABI_VERSION 1 36 37 static const unsigned int plt0_entry[] = 38 { 39 0x02000005, /* MOVT D0Re0, #HI(GOT+4) */ 40 0x02000000, /* ADD D0Re0, D0Re0, #LO(GOT+4) */ 41 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */ 42 0xc600012a, /* GETD PC, [D0Re0+#4] */ 43 0xa0fffffe /* NOP */ 44 }; 45 46 static const unsigned int plt0_pic_entry[] = 47 { 48 0x82900001, /* ADDT A0.2, CPC0, #0 */ 49 0x82100000, /* ADD A0.2, A0.2, #0 */ 50 0xa3100c20, /* MOV D0Re0, A0.2 */ 51 0xb70001e3, /* SETL [A0StP++], D0Re0, D1Re0 */ 52 0xc600012a, /* GETD PC, [D0Re0+#4] */ 53 }; 54 55 static const unsigned int plt_entry[] = 56 { 57 0x82100005, /* MOVT A0.2, #HI(GOT+off) */ 58 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */ 59 0xc600806a, /* GETD PC, [A0.2] */ 60 0x03000004, /* MOV D1Re0, #LO(offset) */ 61 0xa0000000 /* B PLT0 */ 62 }; 63 64 static const unsigned int plt_pic_entry[] = 65 { 66 0x82900001, /* ADDT A0.2, CPC0, #HI(GOT+off) */ 67 0x82100000, /* ADD A0.2, A0.2, #LO(GOT+off) */ 68 0xc600806a, /* GETD PC, [A0.2] */ 69 0x03000004, /* MOV D1Re0, #LO(offset) */ 70 0xa0000000 /* B PLT0 */ 71 }; 72 73 /* Variable names follow a coding style. 74 Please follow this (Apps Hungarian) style: 75 76 Structure/Variable Prefix 77 elf_link_hash_table "etab" 78 elf_link_hash_entry "eh" 79 80 elf_metag_link_hash_table "htab" 81 elf_metag_link_hash_entry "hh" 82 83 bfd_link_hash_table "btab" 84 bfd_link_hash_entry "bh" 85 86 bfd_hash_table containing stubs "bstab" 87 elf_metag_stub_hash_entry "hsh" 88 89 elf_metag_dyn_reloc_entry "hdh" 90 91 Always remember to use GNU Coding Style. */ 92 93 #define PLT_ENTRY_SIZE sizeof(plt_entry) 94 95 static reloc_howto_type elf_metag_howto_table[] = 96 { 97 /* High order 16 bit absolute. */ 98 HOWTO (R_METAG_HIADDR16, /* type */ 99 16, /* rightshift */ 100 2, /* size (0 = byte, 1 = short, 2 = long) */ 101 16, /* bitsize */ 102 FALSE, /* pc_relative */ 103 3, /* bitpos */ 104 complain_overflow_dont, /* complain_on_overflow */ 105 bfd_elf_generic_reloc, /* special_function */ 106 "R_METAG_HIADDR16", /* name */ 107 FALSE, /* partial_inplace */ 108 0, /* src_mask */ 109 0x0007fff8, /* dst_mask */ 110 FALSE), /* pcrel_offset */ 111 112 /* Low order 16 bit absolute. */ 113 HOWTO (R_METAG_LOADDR16, /* type */ 114 0, /* rightshift */ 115 2, /* size (0 = byte, 1 = short, 2 = long) */ 116 16, /* bitsize */ 117 FALSE, /* pc_relative */ 118 3, /* bitpos */ 119 complain_overflow_dont,/* complain_on_overflow */ 120 bfd_elf_generic_reloc, /* special_function */ 121 "R_METAG_LOADDR16", /* name */ 122 FALSE, /* partial_inplace */ 123 0, /* src_mask */ 124 0x0007fff8, /* dst_mask */ 125 FALSE), /* pcrel_offset */ 126 127 /* 32 bit absolute. */ 128 HOWTO (R_METAG_ADDR32, /* type */ 129 0, /* rightshift */ 130 2, /* size (0 = byte, 1 = short, 2 = long) */ 131 32, /* bitsize */ 132 FALSE, /* pc_relative */ 133 0, /* bitpos */ 134 complain_overflow_bitfield, /* complain_on_overflow */ 135 bfd_elf_generic_reloc, /* special_function */ 136 "R_METAG_ADDR32", /* name */ 137 FALSE, /* partial_inplace */ 138 0x00000000, /* src_mask */ 139 0xffffffff, /* dst_mask */ 140 FALSE), /* pcrel_offset */ 141 142 /* No relocation. */ 143 HOWTO (R_METAG_NONE, /* type */ 144 0, /* rightshift */ 145 0, /* size (0 = byte, 1 = short, 2 = long) */ 146 0, /* bitsize */ 147 FALSE, /* pc_relative */ 148 0, /* bitpos */ 149 complain_overflow_dont, /* complain_on_overflow */ 150 bfd_elf_generic_reloc, /* special_function */ 151 "R_METAG_NONE", /* name */ 152 FALSE, /* partial_inplace */ 153 0, /* src_mask */ 154 0, /* dst_mask */ 155 FALSE), /* pcrel_offset */ 156 157 /* 19 bit pc relative */ 158 HOWTO (R_METAG_RELBRANCH, /* type */ 159 2, /* rightshift */ 160 2, /* size (0 = byte, 1 = short, 2 = long) */ 161 19, /* bitsize */ 162 TRUE, /* pc_relative */ 163 5, /* bitpos */ 164 complain_overflow_signed, /* complain_on_overflow */ 165 bfd_elf_generic_reloc, /* special_function */ 166 "R_METAG_RELBRANCH", /* name */ 167 FALSE, /* partial_inplace */ 168 0, /* src_mask */ 169 0x00ffffe0, /* dst_mask */ 170 FALSE), /* pcrel_offset */ 171 172 /* GET/SET offset */ 173 HOWTO (R_METAG_GETSETOFF, /* type */ 174 0, /* rightshift */ 175 1, /* size (0 = byte, 1 = short, 2 = long) */ 176 12, /* bitsize */ 177 FALSE, /* pc_relative */ 178 7, /* bitpos */ 179 complain_overflow_dont, /* complain_on_overflow */ 180 bfd_elf_generic_reloc, /* special_function */ 181 "R_METAG_GETSETOFF", /* name */ 182 FALSE, /* partial_inplace */ 183 0, /* src_mask */ 184 0, /* dst_mask */ 185 FALSE), /* pcrel_offset */ 186 187 EMPTY_HOWTO (6), 188 EMPTY_HOWTO (7), 189 EMPTY_HOWTO (8), 190 EMPTY_HOWTO (9), 191 EMPTY_HOWTO (10), 192 EMPTY_HOWTO (11), 193 EMPTY_HOWTO (12), 194 EMPTY_HOWTO (13), 195 EMPTY_HOWTO (14), 196 EMPTY_HOWTO (15), 197 EMPTY_HOWTO (16), 198 EMPTY_HOWTO (17), 199 EMPTY_HOWTO (18), 200 EMPTY_HOWTO (19), 201 EMPTY_HOWTO (20), 202 EMPTY_HOWTO (21), 203 EMPTY_HOWTO (22), 204 EMPTY_HOWTO (23), 205 EMPTY_HOWTO (24), 206 EMPTY_HOWTO (25), 207 EMPTY_HOWTO (26), 208 EMPTY_HOWTO (27), 209 EMPTY_HOWTO (28), 210 EMPTY_HOWTO (29), 211 212 HOWTO (R_METAG_GNU_VTINHERIT, /* type */ 213 0, /* rightshift */ 214 2, /* size (0 = byte, 1 = short, 2 = long) */ 215 0, /* bitsize */ 216 FALSE, /* pc_relative */ 217 0, /* bitpos */ 218 complain_overflow_dont, /* complain_on_overflow */ 219 NULL, /* special_function */ 220 "R_METAG_GNU_VTINHERIT", /* name */ 221 FALSE, /* partial_inplace */ 222 0, /* src_mask */ 223 0, /* dst_mask */ 224 FALSE), /* pcrel_offset */ 225 226 HOWTO (R_METAG_GNU_VTENTRY, /* type */ 227 0, /* rightshift */ 228 2, /* size (0 = byte, 1 = short, 2 = long) */ 229 0, /* bitsize */ 230 FALSE, /* pc_relative */ 231 0, /* bitpos */ 232 complain_overflow_dont, /* complain_on_overflow */ 233 _bfd_elf_rel_vtable_reloc_fn, /* special_function */ 234 "R_METAG_GNU_VTENTRY", /* name */ 235 FALSE, /* partial_inplace */ 236 0, /* src_mask */ 237 0, /* dst_mask */ 238 FALSE), /* pcrel_offset */ 239 240 /* High order 16 bit GOT offset */ 241 HOWTO (R_METAG_HI16_GOTOFF, /* type */ 242 16, /* rightshift */ 243 2, /* size (0 = byte, 1 = short, 2 = long) */ 244 16, /* bitsize */ 245 FALSE, /* pc_relative */ 246 3, /* bitpos */ 247 complain_overflow_dont, /* complain_on_overflow */ 248 bfd_elf_generic_reloc, /* special_function */ 249 "R_METAG_HI16_GOTOFF", /* name */ 250 FALSE, /* partial_inplace */ 251 0, /* src_mask */ 252 0x0007fff8, /* dst_mask */ 253 FALSE), /* pcrel_offset */ 254 255 /* Low order 16 bit GOT offset */ 256 HOWTO (R_METAG_LO16_GOTOFF, /* type */ 257 0, /* rightshift */ 258 2, /* size (0 = byte, 1 = short, 2 = long) */ 259 16, /* bitsize */ 260 FALSE, /* pc_relative */ 261 3, /* bitpos */ 262 complain_overflow_dont, /* complain_on_overflow */ 263 bfd_elf_generic_reloc, /* special_function */ 264 "R_METAG_LO16_GOTOFF", /* name */ 265 FALSE, /* partial_inplace */ 266 0, /* src_mask */ 267 0x0007fff8, /* dst_mask */ 268 FALSE), /* pcrel_offset */ 269 270 /* GET/SET GOT offset */ 271 HOWTO (R_METAG_GETSET_GOTOFF, /* type */ 272 0, /* rightshift */ 273 1, /* size (0 = byte, 1 = short, 2 = long) */ 274 12, /* bitsize */ 275 FALSE, /* pc_relative */ 276 7, /* bitpos */ 277 complain_overflow_dont, /* complain_on_overflow */ 278 bfd_elf_generic_reloc, /* special_function */ 279 "R_METAG_GETSET_GOTOFF", /* name */ 280 FALSE, /* partial_inplace */ 281 0, /* src_mask */ 282 0, /* dst_mask */ 283 FALSE), /* pcrel_offset */ 284 285 /* GET/SET GOT relative */ 286 HOWTO (R_METAG_GETSET_GOT, /* type */ 287 0, /* rightshift */ 288 1, /* size (0 = byte, 1 = short, 2 = long) */ 289 12, /* bitsize */ 290 FALSE, /* pc_relative */ 291 7, /* bitpos */ 292 complain_overflow_dont, /* complain_on_overflow */ 293 bfd_elf_generic_reloc, /* special_function */ 294 "R_METAG_GETSET_GOT", /* name */ 295 FALSE, /* partial_inplace */ 296 0, /* src_mask */ 297 0, /* dst_mask */ 298 FALSE), /* pcrel_offset */ 299 300 /* High order 16 bit GOT reference */ 301 HOWTO (R_METAG_HI16_GOTPC, /* type */ 302 16, /* rightshift */ 303 2, /* size (0 = byte, 1 = short, 2 = long) */ 304 16, /* bitsize */ 305 FALSE, /* pc_relative */ 306 3, /* bitpos */ 307 complain_overflow_dont, /* complain_on_overflow */ 308 bfd_elf_generic_reloc, /* special_function */ 309 "R_METAG_HI16_GOTPC", /* name */ 310 FALSE, /* partial_inplace */ 311 0, /* src_mask */ 312 0x0007fff8, /* dst_mask */ 313 FALSE), /* pcrel_offset */ 314 315 /* Low order 16 bit GOT reference */ 316 HOWTO (R_METAG_LO16_GOTPC, /* type */ 317 0, /* rightshift */ 318 2, /* size (0 = byte, 1 = short, 2 = long) */ 319 16, /* bitsize */ 320 FALSE, /* pc_relative */ 321 3, /* bitpos */ 322 complain_overflow_dont, /* complain_on_overflow */ 323 bfd_elf_generic_reloc, /* special_function */ 324 "R_METAG_LO16_GOTPC", /* name */ 325 FALSE, /* partial_inplace */ 326 0, /* src_mask */ 327 0x0007fff8, /* dst_mask */ 328 FALSE), /* pcrel_offset */ 329 330 /* High order 16 bit PLT */ 331 HOWTO (R_METAG_HI16_PLT, /* type */ 332 16, /* rightshift */ 333 2, /* size (0 = byte, 1 = short, 2 = long) */ 334 16, /* bitsize */ 335 FALSE, /* pc_relative */ 336 3, /* bitpos */ 337 complain_overflow_dont, /* complain_on_overflow */ 338 bfd_elf_generic_reloc, /* special_function */ 339 "R_METAG_HI16_PLT", /* name */ 340 FALSE, /* partial_inplace */ 341 0, /* src_mask */ 342 0x0007fff8, /* dst_mask */ 343 FALSE), /* pcrel_offset */ 344 345 /* Low order 16 bit PLT */ 346 HOWTO (R_METAG_LO16_PLT, /* type */ 347 0, /* rightshift */ 348 2, /* size (0 = byte, 1 = short, 2 = long) */ 349 16, /* bitsize */ 350 FALSE, /* pc_relative */ 351 3, /* bitpos */ 352 complain_overflow_dont, /* complain_on_overflow */ 353 bfd_elf_generic_reloc, /* special_function */ 354 "R_METAG_LO16_PLT", /* name */ 355 FALSE, /* partial_inplace */ 356 0, /* src_mask */ 357 0xffffffff, /* dst_mask */ 358 FALSE), /* pcrel_offset */ 359 360 HOWTO (R_METAG_RELBRANCH_PLT, /* type */ 361 2, /* rightshift */ 362 2, /* size (0 = byte, 1 = short, 2 = long) */ 363 19, /* bitsize */ 364 TRUE, /* pc_relative */ 365 5, /* bitpos */ 366 complain_overflow_signed, /* complain_on_overflow */ 367 bfd_elf_generic_reloc, /* special_function */ 368 "R_METAG_RELBRANCH_PLT", /* name */ 369 FALSE, /* partial_inplace */ 370 0, /* src_mask */ 371 0x00ffffe0, /* dst_mask */ 372 FALSE), /* pcrel_offset */ 373 374 /* Dummy relocs used by the linker internally. */ 375 HOWTO (R_METAG_GOTOFF, /* type */ 376 0, /* rightshift */ 377 2, /* size (0 = byte, 1 = short, 2 = long) */ 378 32, /* bitsize */ 379 FALSE, /* pc_relative */ 380 0, /* bitpos */ 381 complain_overflow_bitfield, /* complain_on_overflow */ 382 bfd_elf_generic_reloc, /* special_function */ 383 "R_METAG_GOTOFF", /* name */ 384 FALSE, /* partial_inplace */ 385 0xffffffff, /* src_mask */ 386 0xffffffff, /* dst_mask */ 387 FALSE), /* pcrel_offset */ 388 389 HOWTO (R_METAG_PLT, /* type */ 390 0, /* rightshift */ 391 2, /* size (0 = byte, 1 = short, 2 = long) */ 392 32, /* bitsize */ 393 FALSE, /* pc_relative */ 394 0, /* bitpos */ 395 complain_overflow_bitfield, /* complain_on_overflow */ 396 bfd_elf_generic_reloc, /* special_function */ 397 "R_METAG_GOTOFF", /* name */ 398 FALSE, /* partial_inplace */ 399 0xffffffff, /* src_mask */ 400 0xffffffff, /* dst_mask */ 401 FALSE), /* pcrel_offset */ 402 403 /* This is used only by the dynamic linker. The symbol should exist 404 both in the object being run and in some shared library. The 405 dynamic linker copies the data addressed by the symbol from the 406 shared library into the object, because the object being 407 run has to have the data at some particular address. */ 408 HOWTO (R_METAG_COPY, /* type */ 409 0, /* rightshift */ 410 2, /* size (0 = byte, 1 = short, 2 = long) */ 411 32, /* bitsize */ 412 FALSE, /* pc_relative */ 413 0, /* bitpos */ 414 complain_overflow_bitfield, /* complain_on_overflow */ 415 bfd_elf_generic_reloc, /* special_function */ 416 "R_METAG_COPY", /* name */ 417 FALSE, /* partial_inplace */ 418 0xffffffff, /* src_mask */ 419 0xffffffff, /* dst_mask */ 420 FALSE), /* pcrel_offset */ 421 422 /* Marks a procedure linkage table entry for a symbol. */ 423 HOWTO (R_METAG_JMP_SLOT, /* type */ 424 0, /* rightshift */ 425 2, /* size (0 = byte, 1 = short, 2 = long) */ 426 32, /* bitsize */ 427 FALSE, /* pc_relative */ 428 0, /* bitpos */ 429 complain_overflow_bitfield, /* complain_on_overflow */ 430 bfd_elf_generic_reloc, /* special_function */ 431 "R_METAG_JMP_SLOT", /* name */ 432 FALSE, /* partial_inplace */ 433 0xffffffff, /* src_mask */ 434 0xffffffff, /* dst_mask */ 435 FALSE), /* pcrel_offset */ 436 437 /* Used only by the dynamic linker. When the object is run, this 438 longword is set to the load address of the object, plus the 439 addend. */ 440 HOWTO (R_METAG_RELATIVE, /* type */ 441 0, /* rightshift */ 442 2, /* size (0 = byte, 1 = short, 2 = long) */ 443 32, /* bitsize */ 444 FALSE, /* pc_relative */ 445 0, /* bitpos */ 446 complain_overflow_bitfield, /* complain_on_overflow */ 447 bfd_elf_generic_reloc, /* special_function */ 448 "R_METAG_RELATIVE", /* name */ 449 FALSE, /* partial_inplace */ 450 0xffffffff, /* src_mask */ 451 0xffffffff, /* dst_mask */ 452 FALSE), /* pcrel_offset */ 453 454 HOWTO (R_METAG_GLOB_DAT, /* type */ 455 0, /* rightshift */ 456 2, /* size (0 = byte, 1 = short, 2 = long) */ 457 32, /* bitsize */ 458 FALSE, /* pc_relative */ 459 0, /* bitpos */ 460 complain_overflow_bitfield, /* complain_on_overflow */ 461 bfd_elf_generic_reloc, /* special_function */ 462 "R_METAG_GLOB_DAT", /* name */ 463 FALSE, /* partial_inplace */ 464 0xffffffff, /* src_mask */ 465 0xffffffff, /* dst_mask */ 466 FALSE), /* pcrel_offset */ 467 468 HOWTO (R_METAG_TLS_GD, /* type */ 469 0, /* rightshift */ 470 2, /* size (0 = byte, 1 = short, 2 = long) */ 471 16, /* bitsize */ 472 FALSE, /* pc_relative */ 473 3, /* bitpos */ 474 complain_overflow_dont, /* complain_on_overflow */ 475 bfd_elf_generic_reloc, /* special_function */ 476 "R_METAG_TLS_GD", /* name */ 477 FALSE, /* partial_inplace */ 478 0, /* src_mask */ 479 0x0007fff8, /* dst_mask */ 480 FALSE), /* pcrel_offset */ 481 482 HOWTO (R_METAG_TLS_LDM, /* type */ 483 0, /* rightshift */ 484 2, /* size (0 = byte, 1 = short, 2 = long) */ 485 16, /* bitsize */ 486 FALSE, /* pc_relative */ 487 3, /* bitpos */ 488 complain_overflow_bitfield, /* complain_on_overflow */ 489 bfd_elf_generic_reloc, /* special_function */ 490 "R_METAG_TLS_LDM", /* name */ 491 FALSE, /* partial_inplace */ 492 0, /* src_mask */ 493 0x0007fff8, /* dst_mask */ 494 FALSE), /* pcrel_offset */ 495 496 HOWTO (R_METAG_TLS_LDO_HI16, /* type */ 497 16, /* rightshift */ 498 2, /* size (0 = byte, 1 = short, 2 = long) */ 499 16, /* bitsize */ 500 FALSE, /* pc_relative */ 501 3, /* bitpos */ 502 complain_overflow_bitfield, /* complain_on_overflow */ 503 bfd_elf_generic_reloc, /* special_function */ 504 "R_METAG_TLS_LDO_HI16", /* name */ 505 FALSE, /* partial_inplace */ 506 0, /* src_mask */ 507 0x0007fff8, /* dst_mask */ 508 FALSE), /* pcrel_offset */ 509 510 HOWTO (R_METAG_TLS_LDO_LO16, /* type */ 511 0, /* rightshift */ 512 2, /* size (0 = byte, 1 = short, 2 = long) */ 513 16, /* bitsize */ 514 FALSE, /* pc_relative */ 515 3, /* bitpos */ 516 complain_overflow_bitfield, /* complain_on_overflow */ 517 bfd_elf_generic_reloc, /* special_function */ 518 "R_METAG_TLS_LDO_LO16", /* name */ 519 FALSE, /* partial_inplace */ 520 0, /* src_mask */ 521 0x0007fff8, /* dst_mask */ 522 FALSE), /* pcrel_offset */ 523 524 /* Dummy reloc used by the linker internally. */ 525 HOWTO (R_METAG_TLS_LDO, /* type */ 526 0, /* rightshift */ 527 2, /* size (0 = byte, 1 = short, 2 = long) */ 528 16, /* bitsize */ 529 FALSE, /* pc_relative */ 530 3, /* bitpos */ 531 complain_overflow_bitfield, /* complain_on_overflow */ 532 bfd_elf_generic_reloc, /* special_function */ 533 "R_METAG_TLS_LDO", /* name */ 534 FALSE, /* partial_inplace */ 535 0, /* src_mask */ 536 0x0007fff8, /* dst_mask */ 537 FALSE), /* pcrel_offset */ 538 539 HOWTO (R_METAG_TLS_IE, /* type */ 540 2, /* rightshift */ 541 2, /* size (0 = byte, 1 = short, 2 = long) */ 542 12, /* bitsize */ 543 FALSE, /* pc_relative */ 544 7, /* bitpos */ 545 complain_overflow_dont, /* complain_on_overflow */ 546 bfd_elf_generic_reloc, /* special_function */ 547 "R_METAG_TLS_IE", /* name */ 548 FALSE, /* partial_inplace */ 549 0, /* src_mask */ 550 0x0007ff80, /* dst_mask */ 551 FALSE), /* pcrel_offset */ 552 553 /* Dummy reloc used by the linker internally. */ 554 HOWTO (R_METAG_TLS_IENONPIC, /* type */ 555 0, /* rightshift */ 556 2, /* size (0 = byte, 1 = short, 2 = long) */ 557 16, /* bitsize */ 558 FALSE, /* pc_relative */ 559 3, /* bitpos */ 560 complain_overflow_dont, /* complain_on_overflow */ 561 bfd_elf_generic_reloc, /* special_function */ 562 "R_METAG_TLS_IENONPIC", /* name */ 563 FALSE, /* partial_inplace */ 564 0, /* src_mask */ 565 0x0007fff8, /* dst_mask */ 566 FALSE), /* pcrel_offset */ 567 568 HOWTO (R_METAG_TLS_IENONPIC_HI16,/* type */ 569 16, /* rightshift */ 570 2, /* size (0 = byte, 1 = short, 2 = long) */ 571 16, /* bitsize */ 572 FALSE, /* pc_relative */ 573 3, /* bitpos */ 574 complain_overflow_dont, /* complain_on_overflow */ 575 bfd_elf_generic_reloc, /* special_function */ 576 "R_METAG_TLS_IENONPIC_HI16", /* name */ 577 FALSE, /* partial_inplace */ 578 0, /* src_mask */ 579 0x0007fff8, /* dst_mask */ 580 FALSE), /* pcrel_offset */ 581 582 HOWTO (R_METAG_TLS_IENONPIC_LO16,/* type */ 583 0, /* rightshift */ 584 2, /* size (0 = byte, 1 = short, 2 = long) */ 585 16, /* bitsize */ 586 FALSE, /* pc_relative */ 587 3, /* bitpos */ 588 complain_overflow_dont, /* complain_on_overflow */ 589 bfd_elf_generic_reloc, /* special_function */ 590 "R_METAG_TLS_IENONPIC_LO16", /* name */ 591 FALSE, /* partial_inplace */ 592 0, /* src_mask */ 593 0x0007fff8, /* dst_mask */ 594 FALSE), /* pcrel_offset */ 595 596 HOWTO (R_METAG_TLS_TPOFF, /* type */ 597 0, /* rightshift */ 598 2, /* size (0 = byte, 1 = short, 2 = long) */ 599 32, /* bitsize */ 600 FALSE, /* pc_relative */ 601 0, /* bitpos */ 602 complain_overflow_bitfield, /* complain_on_overflow */ 603 bfd_elf_generic_reloc, /* special_function */ 604 "R_METAG_TLS_TPOFF", /* name */ 605 FALSE, /* partial_inplace */ 606 0, /* src_mask */ 607 0xffffffff, /* dst_mask */ 608 FALSE), /* pcrel_offset */ 609 610 HOWTO (R_METAG_TLS_DTPMOD, /* type */ 611 0, /* rightshift */ 612 2, /* size (0 = byte, 1 = short, 2 = long) */ 613 32, /* bitsize */ 614 FALSE, /* pc_relative */ 615 0, /* bitpos */ 616 complain_overflow_bitfield, /* complain_on_overflow */ 617 bfd_elf_generic_reloc, /* special_function */ 618 "R_METAG_TLS_DTPMOD", /* name */ 619 FALSE, /* partial_inplace */ 620 0, /* src_mask */ 621 0xffffffff, /* dst_mask */ 622 FALSE), /* pcrel_offset */ 623 624 HOWTO (R_METAG_TLS_DTPOFF, /* type */ 625 0, /* rightshift */ 626 2, /* size (0 = byte, 1 = short, 2 = long) */ 627 32, /* bitsize */ 628 FALSE, /* pc_relative */ 629 0, /* bitpos */ 630 complain_overflow_bitfield, /* complain_on_overflow */ 631 bfd_elf_generic_reloc, /* special_function */ 632 "R_METAG_TLS_DTPOFF", /* name */ 633 FALSE, /* partial_inplace */ 634 0, /* src_mask */ 635 0xffffffff, /* dst_mask */ 636 FALSE), /* pcrel_offset */ 637 638 /* Dummy reloc used by the linker internally. */ 639 HOWTO (R_METAG_TLS_LE, /* type */ 640 0, /* rightshift */ 641 2, /* size (0 = byte, 1 = short, 2 = long) */ 642 32, /* bitsize */ 643 FALSE, /* pc_relative */ 644 0, /* bitpos */ 645 complain_overflow_bitfield, /* complain_on_overflow */ 646 bfd_elf_generic_reloc, /* special_function */ 647 "R_METAG_TLS_LE", /* name */ 648 FALSE, /* partial_inplace */ 649 0, /* src_mask */ 650 0xffffffff, /* dst_mask */ 651 FALSE), /* pcrel_offset */ 652 653 HOWTO (R_METAG_TLS_LE_HI16, /* type */ 654 16, /* rightshift */ 655 2, /* size (0 = byte, 1 = short, 2 = long) */ 656 16, /* bitsize */ 657 FALSE, /* pc_relative */ 658 3, /* bitpos */ 659 complain_overflow_dont, /* complain_on_overflow */ 660 bfd_elf_generic_reloc, /* special_function */ 661 "R_METAG_TLS_LE_HI16", /* name */ 662 FALSE, /* partial_inplace */ 663 0, /* src_mask */ 664 0x0007fff8, /* dst_mask */ 665 FALSE), /* pcrel_offset */ 666 667 HOWTO (R_METAG_TLS_LE_LO16, /* type */ 668 0, /* rightshift */ 669 2, /* size (0 = byte, 1 = short, 2 = long) */ 670 16, /* bitsize */ 671 FALSE, /* pc_relative */ 672 3, /* bitpos */ 673 complain_overflow_dont, /* complain_on_overflow */ 674 bfd_elf_generic_reloc, /* special_function */ 675 "R_METAG_TLS_LE_LO16", /* name */ 676 FALSE, /* partial_inplace */ 677 0, /* src_mask */ 678 0x0007fff8, /* dst_mask */ 679 FALSE), /* pcrel_offset */ 680 681 }; 682 683 #define BRANCH_BITS 19 684 685 /* The GOT is typically accessed using a [GS]ETD instruction. The size of the 686 immediate offset which can be used in such instructions therefore limits 687 the usable size of the GOT. If the base register for the [GS]ETD (A1LbP) 688 is pointing to the base of the GOT then the size is limited to the maximum 689 11 bits unsigned dword offset, or 2^13 = 0x2000 bytes. However the offset 690 in a [GS]ETD instruction is signed, so by setting the base address register 691 to an offset of that 0x2000 byte maximum unsigned offset from the base of 692 the GOT we can use negative offsets in addition to positive. This 693 effectively doubles the usable GOT size to 0x4000 bytes. */ 694 #define GOT_REG_OFFSET 0x2000 695 696 struct metag_reloc_map 697 { 698 bfd_reloc_code_real_type bfd_reloc_val; 699 unsigned int metag_reloc_val; 700 }; 701 702 static const struct metag_reloc_map metag_reloc_map [] = 703 { 704 { BFD_RELOC_NONE, R_METAG_NONE }, 705 { BFD_RELOC_32, R_METAG_ADDR32 }, 706 { BFD_RELOC_METAG_HIADDR16, R_METAG_HIADDR16 }, 707 { BFD_RELOC_METAG_LOADDR16, R_METAG_LOADDR16 }, 708 { BFD_RELOC_METAG_RELBRANCH, R_METAG_RELBRANCH }, 709 { BFD_RELOC_METAG_GETSETOFF, R_METAG_GETSETOFF }, 710 { BFD_RELOC_VTABLE_INHERIT, R_METAG_GNU_VTINHERIT }, 711 { BFD_RELOC_VTABLE_ENTRY, R_METAG_GNU_VTENTRY }, 712 { BFD_RELOC_METAG_REL8, R_METAG_REL8 }, 713 { BFD_RELOC_METAG_REL16, R_METAG_REL16 }, 714 { BFD_RELOC_METAG_HI16_GOTOFF, R_METAG_HI16_GOTOFF }, 715 { BFD_RELOC_METAG_LO16_GOTOFF, R_METAG_LO16_GOTOFF }, 716 { BFD_RELOC_METAG_GETSET_GOTOFF, R_METAG_GETSET_GOTOFF }, 717 { BFD_RELOC_METAG_GETSET_GOT, R_METAG_GETSET_GOT }, 718 { BFD_RELOC_METAG_HI16_GOTPC, R_METAG_HI16_GOTPC }, 719 { BFD_RELOC_METAG_LO16_GOTPC, R_METAG_LO16_GOTPC }, 720 { BFD_RELOC_METAG_HI16_PLT, R_METAG_HI16_PLT }, 721 { BFD_RELOC_METAG_LO16_PLT, R_METAG_LO16_PLT }, 722 { BFD_RELOC_METAG_RELBRANCH_PLT, R_METAG_RELBRANCH_PLT }, 723 { BFD_RELOC_METAG_GOTOFF, R_METAG_GOTOFF }, 724 { BFD_RELOC_METAG_PLT, R_METAG_PLT }, 725 { BFD_RELOC_METAG_COPY, R_METAG_COPY }, 726 { BFD_RELOC_METAG_JMP_SLOT, R_METAG_JMP_SLOT }, 727 { BFD_RELOC_METAG_RELATIVE, R_METAG_RELATIVE }, 728 { BFD_RELOC_METAG_GLOB_DAT, R_METAG_GLOB_DAT }, 729 { BFD_RELOC_METAG_TLS_GD, R_METAG_TLS_GD }, 730 { BFD_RELOC_METAG_TLS_LDM, R_METAG_TLS_LDM }, 731 { BFD_RELOC_METAG_TLS_LDO_HI16, R_METAG_TLS_LDO_HI16 }, 732 { BFD_RELOC_METAG_TLS_LDO_LO16, R_METAG_TLS_LDO_LO16 }, 733 { BFD_RELOC_METAG_TLS_LDO, R_METAG_TLS_LDO }, 734 { BFD_RELOC_METAG_TLS_IE, R_METAG_TLS_IE }, 735 { BFD_RELOC_METAG_TLS_IENONPIC, R_METAG_TLS_IENONPIC }, 736 { BFD_RELOC_METAG_TLS_IENONPIC_HI16, R_METAG_TLS_IENONPIC_HI16 }, 737 { BFD_RELOC_METAG_TLS_IENONPIC_LO16, R_METAG_TLS_IENONPIC_LO16 }, 738 { BFD_RELOC_METAG_TLS_TPOFF, R_METAG_TLS_TPOFF }, 739 { BFD_RELOC_METAG_TLS_DTPMOD, R_METAG_TLS_DTPMOD }, 740 { BFD_RELOC_METAG_TLS_DTPOFF, R_METAG_TLS_DTPOFF }, 741 { BFD_RELOC_METAG_TLS_LE, R_METAG_TLS_LE }, 742 { BFD_RELOC_METAG_TLS_LE_HI16, R_METAG_TLS_LE_HI16 }, 743 { BFD_RELOC_METAG_TLS_LE_LO16, R_METAG_TLS_LE_LO16 }, 744 }; 745 746 enum elf_metag_stub_type 747 { 748 metag_stub_long_branch, 749 metag_stub_long_branch_shared, 750 metag_stub_none 751 }; 752 753 struct elf_metag_stub_hash_entry 754 { 755 /* Base hash table entry structure. */ 756 struct bfd_hash_entry bh_root; 757 758 /* The stub section. */ 759 asection *stub_sec; 760 761 /* Offset within stub_sec of the beginning of this stub. */ 762 bfd_vma stub_offset; 763 764 /* Given the symbol's value and its section we can determine its final 765 value when building the stubs (so the stub knows where to jump. */ 766 bfd_vma target_value; 767 asection *target_section; 768 769 enum elf_metag_stub_type stub_type; 770 771 /* The symbol table entry, if any, that this was derived from. */ 772 struct elf_metag_link_hash_entry *hh; 773 774 /* And the reloc addend that this was derived from. */ 775 bfd_vma addend; 776 777 /* Where this stub is being called from, or, in the case of combined 778 stub sections, the first input section in the group. */ 779 asection *id_sec; 780 }; 781 782 struct elf_metag_link_hash_entry 783 { 784 struct elf_link_hash_entry eh; 785 786 /* A pointer to the most recently used stub hash entry against this 787 symbol. */ 788 struct elf_metag_stub_hash_entry *hsh_cache; 789 790 /* Used to count relocations for delayed sizing of relocation 791 sections. */ 792 struct elf_metag_dyn_reloc_entry { 793 794 /* Next relocation in the chain. */ 795 struct elf_metag_dyn_reloc_entry *hdh_next; 796 797 /* The input section of the reloc. */ 798 asection *sec; 799 800 /* Number of relocs copied in this section. */ 801 bfd_size_type count; 802 803 /* Number of relative relocs copied for the input section. */ 804 bfd_size_type relative_count; 805 } *dyn_relocs; 806 807 enum 808 { 809 GOT_UNKNOWN = 0, GOT_NORMAL = 1, GOT_TLS_IE = 2, GOT_TLS_LDM = 4, GOT_TLS_GD = 8 810 } tls_type; 811 }; 812 813 struct elf_metag_link_hash_table 814 { 815 /* The main hash table. */ 816 struct elf_link_hash_table etab; 817 818 /* The stub hash table. */ 819 struct bfd_hash_table bstab; 820 821 /* Linker stub bfd. */ 822 bfd *stub_bfd; 823 824 /* Linker call-backs. */ 825 asection * (*add_stub_section) (const char *, asection *); 826 void (*layout_sections_again) (void); 827 828 /* Array to keep track of which stub sections have been created, and 829 information on stub grouping. */ 830 struct map_stub 831 { 832 /* This is the section to which stubs in the group will be 833 attached. */ 834 asection *link_sec; 835 /* The stub section. */ 836 asection *stub_sec; 837 } *stub_group; 838 839 /* Assorted information used by elf_metag_size_stubs. */ 840 unsigned int bfd_count; 841 int top_index; 842 asection **input_list; 843 Elf_Internal_Sym **all_local_syms; 844 845 /* Short-cuts to get to dynamic linker sections. */ 846 asection *sgot; 847 asection *sgotplt; 848 asection *srelgot; 849 asection *splt; 850 asection *srelplt; 851 asection *sdynbss; 852 asection *srelbss; 853 854 /* Small local sym cache. */ 855 struct sym_cache sym_cache; 856 857 /* Data for LDM relocations. */ 858 union 859 { 860 bfd_signed_vma refcount; 861 bfd_vma offset; 862 } tls_ldm_got; 863 }; 864 865 /* Return the base vma address which should be subtracted from the 866 real address when resolving a dtpoff relocation. This is PT_TLS 867 segment p_vaddr. */ 868 static bfd_vma 869 dtpoff_base (struct bfd_link_info *info) 870 { 871 /* If tls_sec is NULL, we should have signalled an error already. */ 872 if (elf_hash_table (info)->tls_sec == NULL) 873 return 0; 874 return elf_hash_table (info)->tls_sec->vma; 875 } 876 877 /* Return the relocation value for R_METAG_TLS_IE */ 878 static bfd_vma 879 tpoff (struct bfd_link_info *info, bfd_vma address) 880 { 881 /* If tls_sec is NULL, we should have signalled an error already. */ 882 if (elf_hash_table (info)->tls_sec == NULL) 883 return 0; 884 /* METAG TLS ABI is variant I and static TLS blocks start just after 885 tcbhead structure which has 2 pointer fields. */ 886 return (address - elf_hash_table (info)->tls_sec->vma 887 + align_power ((bfd_vma) 8, 888 elf_hash_table (info)->tls_sec->alignment_power)); 889 } 890 891 static void 892 metag_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED, 893 arelent *cache_ptr, 894 Elf_Internal_Rela *dst) 895 { 896 unsigned int r_type; 897 898 r_type = ELF32_R_TYPE (dst->r_info); 899 BFD_ASSERT (r_type < (unsigned int) R_METAG_MAX); 900 cache_ptr->howto = & elf_metag_howto_table [r_type]; 901 } 902 903 static reloc_howto_type * 904 metag_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED, 905 bfd_reloc_code_real_type code) 906 { 907 unsigned int i; 908 909 for (i = 0; i < sizeof (metag_reloc_map) / sizeof (metag_reloc_map[0]); i++) 910 if (metag_reloc_map [i].bfd_reloc_val == code) 911 return & elf_metag_howto_table [metag_reloc_map[i].metag_reloc_val]; 912 913 return NULL; 914 } 915 916 static reloc_howto_type * 917 metag_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 918 const char *r_name) 919 { 920 unsigned int i; 921 922 for (i = 0; i < sizeof (elf_metag_howto_table) / sizeof (elf_metag_howto_table[0]); i++) 923 if (elf_metag_howto_table[i].name != NULL 924 && strcasecmp (elf_metag_howto_table[i].name, r_name) == 0) 925 return &elf_metag_howto_table[i]; 926 927 return NULL; 928 } 929 930 /* Various hash macros and functions. */ 931 #define metag_link_hash_table(p) \ 932 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \ 933 == METAG_ELF_DATA ? ((struct elf_metag_link_hash_table *) ((p)->hash)) : NULL) 934 935 #define metag_elf_hash_entry(ent) \ 936 ((struct elf_metag_link_hash_entry *)(ent)) 937 938 #define metag_stub_hash_entry(ent) \ 939 ((struct elf_metag_stub_hash_entry *)(ent)) 940 941 #define metag_stub_hash_lookup(table, string, create, copy) \ 942 ((struct elf_metag_stub_hash_entry *) \ 943 bfd_hash_lookup ((table), (string), (create), (copy))) 944 945 #define metag_elf_local_got_tls_type(abfd) \ 946 ((char *)(elf_local_got_offsets (abfd) + (elf_tdata (abfd)->symtab_hdr.sh_info))) 947 948 /* Assorted hash table functions. */ 949 950 /* Initialize an entry in the stub hash table. */ 951 952 static struct bfd_hash_entry * 953 stub_hash_newfunc (struct bfd_hash_entry *entry, 954 struct bfd_hash_table *table, 955 const char *string) 956 { 957 /* Allocate the structure if it has not already been allocated by a 958 subclass. */ 959 if (entry == NULL) 960 { 961 entry = bfd_hash_allocate (table, 962 sizeof (struct elf_metag_stub_hash_entry)); 963 if (entry == NULL) 964 return entry; 965 } 966 967 /* Call the allocation method of the superclass. */ 968 entry = bfd_hash_newfunc (entry, table, string); 969 if (entry != NULL) 970 { 971 struct elf_metag_stub_hash_entry *hsh; 972 973 /* Initialize the local fields. */ 974 hsh = (struct elf_metag_stub_hash_entry *) entry; 975 hsh->stub_sec = NULL; 976 hsh->stub_offset = 0; 977 hsh->target_value = 0; 978 hsh->target_section = NULL; 979 hsh->stub_type = metag_stub_long_branch; 980 hsh->hh = NULL; 981 hsh->id_sec = NULL; 982 } 983 984 return entry; 985 } 986 987 /* Initialize an entry in the link hash table. */ 988 989 static struct bfd_hash_entry * 990 metag_link_hash_newfunc (struct bfd_hash_entry *entry, 991 struct bfd_hash_table *table, 992 const char *string) 993 { 994 /* Allocate the structure if it has not already been allocated by a 995 subclass. */ 996 if (entry == NULL) 997 { 998 entry = bfd_hash_allocate (table, 999 sizeof (struct elf_metag_link_hash_entry)); 1000 if (entry == NULL) 1001 return entry; 1002 } 1003 1004 /* Call the allocation method of the superclass. */ 1005 entry = _bfd_elf_link_hash_newfunc (entry, table, string); 1006 if (entry != NULL) 1007 { 1008 struct elf_metag_link_hash_entry *hh; 1009 1010 /* Initialize the local fields. */ 1011 hh = (struct elf_metag_link_hash_entry *) entry; 1012 hh->hsh_cache = NULL; 1013 hh->dyn_relocs = NULL; 1014 hh->tls_type = GOT_UNKNOWN; 1015 } 1016 1017 return entry; 1018 } 1019 1020 /* Free the derived linker hash table. */ 1021 1022 static void 1023 elf_metag_link_hash_table_free (bfd *obfd) 1024 { 1025 struct elf_metag_link_hash_table *htab 1026 = (struct elf_metag_link_hash_table *) obfd->link.hash; 1027 1028 bfd_hash_table_free (&htab->bstab); 1029 _bfd_elf_link_hash_table_free (obfd); 1030 } 1031 1032 /* Create the derived linker hash table. The Meta ELF port uses the derived 1033 hash table to keep information specific to the Meta ELF linker (without 1034 using static variables). */ 1035 1036 static struct bfd_link_hash_table * 1037 elf_metag_link_hash_table_create (bfd *abfd) 1038 { 1039 struct elf_metag_link_hash_table *htab; 1040 bfd_size_type amt = sizeof (*htab); 1041 1042 htab = bfd_zmalloc (amt); 1043 if (htab == NULL) 1044 return NULL; 1045 1046 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, 1047 metag_link_hash_newfunc, 1048 sizeof (struct elf_metag_link_hash_entry), 1049 METAG_ELF_DATA)) 1050 { 1051 free (htab); 1052 return NULL; 1053 } 1054 1055 /* Init the stub hash table too. */ 1056 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc, 1057 sizeof (struct elf_metag_stub_hash_entry))) 1058 { 1059 _bfd_elf_link_hash_table_free (abfd); 1060 return NULL; 1061 } 1062 htab->etab.root.hash_table_free = elf_metag_link_hash_table_free; 1063 1064 return &htab->etab.root; 1065 } 1066 1067 /* Section name for stubs is the associated section name plus this 1068 string. */ 1069 #define STUB_SUFFIX ".stub" 1070 1071 /* Build a name for an entry in the stub hash table. */ 1072 1073 static char * 1074 metag_stub_name (const asection *input_section, 1075 const asection *sym_sec, 1076 const struct elf_metag_link_hash_entry *hh, 1077 const Elf_Internal_Rela *rel) 1078 { 1079 char *stub_name; 1080 bfd_size_type len; 1081 1082 if (hh) 1083 { 1084 len = 8 + 1 + strlen (hh->eh.root.root.string) + 1 + 8 + 1; 1085 stub_name = bfd_malloc (len); 1086 if (stub_name != NULL) 1087 { 1088 sprintf (stub_name, "%08x_%s+%x", 1089 input_section->id & 0xffffffff, 1090 hh->eh.root.root.string, 1091 (int) rel->r_addend & 0xffffffff); 1092 } 1093 } 1094 else 1095 { 1096 len = 8 + 1 + 8 + 1 + 8 + 1 + 8 + 1; 1097 stub_name = bfd_malloc (len); 1098 if (stub_name != NULL) 1099 { 1100 sprintf (stub_name, "%08x_%x:%x+%x", 1101 input_section->id & 0xffffffff, 1102 sym_sec->id & 0xffffffff, 1103 (int) ELF32_R_SYM (rel->r_info) & 0xffffffff, 1104 (int) rel->r_addend & 0xffffffff); 1105 } 1106 } 1107 return stub_name; 1108 } 1109 1110 /* Look up an entry in the stub hash. Stub entries are cached because 1111 creating the stub name takes a bit of time. */ 1112 1113 static struct elf_metag_stub_hash_entry * 1114 metag_get_stub_entry (const asection *input_section, 1115 const asection *sym_sec, 1116 struct elf_metag_link_hash_entry *hh, 1117 const Elf_Internal_Rela *rel, 1118 struct elf_metag_link_hash_table *htab) 1119 { 1120 struct elf_metag_stub_hash_entry *hsh; 1121 const asection *id_sec; 1122 1123 /* If this input section is part of a group of sections sharing one 1124 stub section, then use the id of the first section in the group. 1125 Stub names need to include a section id, as there may well be 1126 more than one stub used to reach say, printf, and we need to 1127 distinguish between them. */ 1128 id_sec = htab->stub_group[input_section->id].link_sec; 1129 1130 if (hh != NULL && hh->hsh_cache != NULL 1131 && hh->hsh_cache->hh == hh 1132 && hh->hsh_cache->id_sec == id_sec) 1133 { 1134 hsh = hh->hsh_cache; 1135 } 1136 else 1137 { 1138 char *stub_name; 1139 1140 stub_name = metag_stub_name (id_sec, sym_sec, hh, rel); 1141 if (stub_name == NULL) 1142 return NULL; 1143 1144 hsh = metag_stub_hash_lookup (&htab->bstab, 1145 stub_name, FALSE, FALSE); 1146 1147 if (hh != NULL) 1148 hh->hsh_cache = hsh; 1149 1150 free (stub_name); 1151 } 1152 1153 return hsh; 1154 } 1155 1156 /* Add a new stub entry to the stub hash. Not all fields of the new 1157 stub entry are initialised. */ 1158 1159 static struct elf_metag_stub_hash_entry * 1160 metag_add_stub (const char *stub_name, 1161 asection *section, 1162 struct elf_metag_link_hash_table *htab) 1163 { 1164 asection *link_sec; 1165 asection *stub_sec; 1166 struct elf_metag_stub_hash_entry *hsh; 1167 1168 link_sec = htab->stub_group[section->id].link_sec; 1169 stub_sec = htab->stub_group[section->id].stub_sec; 1170 if (stub_sec == NULL) 1171 { 1172 stub_sec = htab->stub_group[link_sec->id].stub_sec; 1173 if (stub_sec == NULL) 1174 { 1175 size_t namelen; 1176 bfd_size_type len; 1177 char *s_name; 1178 1179 namelen = strlen (link_sec->name); 1180 len = namelen + sizeof (STUB_SUFFIX); 1181 s_name = bfd_alloc (htab->stub_bfd, len); 1182 if (s_name == NULL) 1183 return NULL; 1184 1185 memcpy (s_name, link_sec->name, namelen); 1186 memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX)); 1187 1188 stub_sec = (*htab->add_stub_section) (s_name, link_sec); 1189 if (stub_sec == NULL) 1190 return NULL; 1191 htab->stub_group[link_sec->id].stub_sec = stub_sec; 1192 } 1193 htab->stub_group[section->id].stub_sec = stub_sec; 1194 } 1195 1196 /* Enter this entry into the linker stub hash table. */ 1197 hsh = metag_stub_hash_lookup (&htab->bstab, stub_name, 1198 TRUE, FALSE); 1199 if (hsh == NULL) 1200 { 1201 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"), 1202 section->owner, 1203 stub_name); 1204 return NULL; 1205 } 1206 1207 hsh->stub_sec = stub_sec; 1208 hsh->stub_offset = 0; 1209 hsh->id_sec = link_sec; 1210 return hsh; 1211 } 1212 1213 /* Check a signed integer value can be represented in the given number 1214 of bits. */ 1215 1216 static bfd_boolean 1217 within_signed_range (int value, unsigned int bits) 1218 { 1219 int min_val = -(1 << (bits - 1)); 1220 int max_val = (1 << (bits - 1)) - 1; 1221 return (value <= max_val) && (value >= min_val); 1222 } 1223 1224 /* Perform a relocation as part of a final link. */ 1225 1226 static bfd_reloc_status_type 1227 metag_final_link_relocate (reloc_howto_type *howto, 1228 bfd *input_bfd, 1229 asection *input_section, 1230 bfd_byte *contents, 1231 Elf_Internal_Rela *rel, 1232 bfd_vma relocation, 1233 struct elf_metag_link_hash_entry *hh, 1234 struct elf_metag_link_hash_table *htab, 1235 asection *sym_sec) 1236 { 1237 bfd_reloc_status_type r = bfd_reloc_ok; 1238 bfd_byte *hit_data = contents + rel->r_offset; 1239 int opcode, op_shift, op_extended, l1, l2; 1240 bfd_signed_vma srel, addend = rel->r_addend; 1241 struct elf_metag_stub_hash_entry *hsh = NULL; 1242 bfd_vma location; 1243 1244 /* Find out where we are and where we're going. */ 1245 location = (rel->r_offset + 1246 input_section->output_offset + 1247 input_section->output_section->vma); 1248 1249 switch (howto->type) 1250 { 1251 case R_METAG_RELBRANCH: 1252 case R_METAG_RELBRANCH_PLT: 1253 /* Make it a pc relative offset. */ 1254 relocation -= location; 1255 break; 1256 case R_METAG_TLS_GD: 1257 case R_METAG_TLS_IE: 1258 relocation -= elf_gp (input_section->output_section->owner); 1259 break; 1260 default: 1261 break; 1262 } 1263 1264 switch (howto->type) 1265 { 1266 case R_METAG_RELBRANCH_PLT: 1267 case R_METAG_RELBRANCH: 1268 opcode = bfd_get_32 (input_bfd, hit_data); 1269 1270 srel = (bfd_signed_vma) relocation; 1271 srel += addend; 1272 1273 /* If the branch is out of reach, then redirect the 1274 call to the local stub for this function. */ 1275 if (srel > ((1 << (BRANCH_BITS + 1)) - 1) || 1276 (srel < - (1 << (BRANCH_BITS + 1)))) 1277 { 1278 if (sym_sec == NULL) 1279 break; 1280 1281 hsh = metag_get_stub_entry (input_section, sym_sec, 1282 hh, rel, htab); 1283 if (hsh == NULL) 1284 return bfd_reloc_undefined; 1285 1286 /* Munge up the value and addend so that we call the stub 1287 rather than the procedure directly. */ 1288 srel = (hsh->stub_offset 1289 + hsh->stub_sec->output_offset 1290 + hsh->stub_sec->output_section->vma); 1291 srel -= location; 1292 } 1293 1294 srel = srel >> 2; 1295 1296 if (!within_signed_range (srel, BRANCH_BITS)) 1297 { 1298 if (hh && hh->eh.root.type == bfd_link_hash_undefweak) 1299 srel = 0; 1300 else 1301 return bfd_reloc_overflow; 1302 } 1303 1304 opcode &= ~(0x7ffff << 5); 1305 opcode |= ((srel & 0x7ffff) << 5); 1306 1307 bfd_put_32 (input_bfd, opcode, hit_data); 1308 break; 1309 case R_METAG_GETSETOFF: 1310 case R_METAG_GETSET_GOT: 1311 case R_METAG_GETSET_GOTOFF: 1312 opcode = bfd_get_32 (input_bfd, hit_data); 1313 1314 srel = (bfd_signed_vma) relocation; 1315 srel += addend; 1316 1317 /* Is this a standard or extended GET/SET? */ 1318 if ((opcode & 0xf0000000) == 0xa0000000) 1319 { 1320 /* Extended GET/SET. */ 1321 l1 = opcode & 0x2; 1322 l2 = opcode & 0x4; 1323 op_extended = 1; 1324 } 1325 else 1326 { 1327 /* Standard GET/SET. */ 1328 l1 = opcode & 0x01000000; 1329 l2 = opcode & 0x04000000; 1330 op_extended = 0; 1331 } 1332 1333 /* Calculate the width of the GET/SET and how much we need to 1334 shift the result by. */ 1335 if (l2) 1336 if (l1) 1337 op_shift = 3; 1338 else 1339 op_shift = 2; 1340 else 1341 if (l1) 1342 op_shift = 1; 1343 else 1344 op_shift = 0; 1345 1346 /* GET/SET offsets are scaled by the width of the transfer. */ 1347 srel = srel >> op_shift; 1348 1349 /* Extended GET/SET has signed 12 bits of offset, standard has 1350 signed 6 bits. */ 1351 if (op_extended) 1352 { 1353 if (!within_signed_range (srel, 12)) 1354 { 1355 if (hh && hh->eh.root.type == bfd_link_hash_undefweak) 1356 srel = 0; 1357 else 1358 return bfd_reloc_overflow; 1359 } 1360 opcode &= ~(0xfff << 7); 1361 opcode |= ((srel & 0xfff) << 7); 1362 } 1363 else 1364 { 1365 if (!within_signed_range (srel, 5)) 1366 { 1367 if (hh && hh->eh.root.type == bfd_link_hash_undefweak) 1368 srel = 0; 1369 else 1370 return bfd_reloc_overflow; 1371 } 1372 opcode &= ~(0x3f << 8); 1373 opcode |= ((srel & 0x3f) << 8); 1374 } 1375 1376 bfd_put_32 (input_bfd, opcode, hit_data); 1377 break; 1378 case R_METAG_TLS_GD: 1379 case R_METAG_TLS_LDM: 1380 opcode = bfd_get_32 (input_bfd, hit_data); 1381 1382 if ((bfd_signed_vma)relocation < 0) 1383 { 1384 /* sign extend immediate */ 1385 if ((opcode & 0xf2000001) == 0x02000000) 1386 { 1387 /* ADD De.e,Dx.r,#I16 */ 1388 /* set SE bit */ 1389 opcode |= (1 << 1); 1390 } else 1391 return bfd_reloc_overflow; 1392 } 1393 1394 bfd_put_32 (input_bfd, opcode, hit_data); 1395 1396 r = _bfd_final_link_relocate (howto, input_bfd, input_section, 1397 contents, rel->r_offset, 1398 relocation, rel->r_addend); 1399 break; 1400 default: 1401 r = _bfd_final_link_relocate (howto, input_bfd, input_section, 1402 contents, rel->r_offset, 1403 relocation, rel->r_addend); 1404 } 1405 1406 return r; 1407 } 1408 1409 /* This is defined because R_METAG_NONE != 0... 1410 See RELOC_AGAINST_DISCARDED_SECTION for details. */ 1411 #define METAG_RELOC_AGAINST_DISCARDED_SECTION(info, input_bfd, input_section, \ 1412 rel, relend, howto, contents) \ 1413 { \ 1414 _bfd_clear_contents (howto, input_bfd, input_section, \ 1415 contents + rel->r_offset); \ 1416 \ 1417 if (info->relocatable \ 1418 && (input_section->flags & SEC_DEBUGGING)) \ 1419 { \ 1420 /* Only remove relocations in debug sections since other \ 1421 sections may require relocations. */ \ 1422 Elf_Internal_Shdr *rel_hdr; \ 1423 \ 1424 rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section); \ 1425 \ 1426 /* Avoid empty output section. */ \ 1427 if (rel_hdr->sh_size > rel_hdr->sh_entsize) \ 1428 { \ 1429 rel_hdr->sh_size -= rel_hdr->sh_entsize; \ 1430 rel_hdr = _bfd_elf_single_rel_hdr (input_section); \ 1431 rel_hdr->sh_size -= rel_hdr->sh_entsize; \ 1432 \ 1433 memmove (rel, rel + 1, (relend - rel) * sizeof (*rel)); \ 1434 \ 1435 input_section->reloc_count--; \ 1436 relend--; \ 1437 rel--; \ 1438 continue; \ 1439 } \ 1440 } \ 1441 \ 1442 rel->r_info = R_METAG_NONE; \ 1443 rel->r_addend = 0; \ 1444 continue; \ 1445 } 1446 1447 /* Relocate a META ELF section. 1448 1449 The RELOCATE_SECTION function is called by the new ELF backend linker 1450 to handle the relocations for a section. 1451 1452 The relocs are always passed as Rela structures; if the section 1453 actually uses Rel structures, the r_addend field will always be 1454 zero. 1455 1456 This function is responsible for adjusting the section contents as 1457 necessary, and (if using Rela relocs and generating a relocatable 1458 output file) adjusting the reloc addend as necessary. 1459 1460 This function does not have to worry about setting the reloc 1461 address or the reloc symbol index. 1462 1463 LOCAL_SYMS is a pointer to the swapped in local symbols. 1464 1465 LOCAL_SECTIONS is an array giving the section in the input file 1466 corresponding to the st_shndx field of each local symbol. 1467 1468 The global hash table entry for the global symbols can be found 1469 via elf_sym_hashes (input_bfd). 1470 1471 When generating relocatable output, this function must handle 1472 STB_LOCAL/STT_SECTION symbols specially. The output symbol is 1473 going to be the section symbol corresponding to the output 1474 section, which means that the addend must be adjusted 1475 accordingly. */ 1476 1477 static bfd_boolean 1478 elf_metag_relocate_section (bfd *output_bfd, 1479 struct bfd_link_info *info, 1480 bfd *input_bfd, 1481 asection *input_section, 1482 bfd_byte *contents, 1483 Elf_Internal_Rela *relocs, 1484 Elf_Internal_Sym *local_syms, 1485 asection **local_sections) 1486 { 1487 bfd_vma *local_got_offsets; 1488 Elf_Internal_Shdr *symtab_hdr; 1489 struct elf_link_hash_entry **eh_syms; 1490 struct elf_metag_link_hash_table *htab; 1491 Elf_Internal_Rela *rel; 1492 Elf_Internal_Rela *relend; 1493 asection *sreloc; 1494 1495 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 1496 eh_syms = elf_sym_hashes (input_bfd); 1497 relend = relocs + input_section->reloc_count; 1498 1499 htab = metag_link_hash_table (info); 1500 local_got_offsets = elf_local_got_offsets (input_bfd); 1501 1502 sreloc = NULL; 1503 1504 for (rel = relocs; rel < relend; rel ++) 1505 { 1506 reloc_howto_type *howto; 1507 unsigned long r_symndx; 1508 Elf_Internal_Sym *sym; 1509 asection *sec; 1510 struct elf_metag_link_hash_entry *hh; 1511 bfd_vma relocation; 1512 bfd_reloc_status_type r; 1513 const char *name; 1514 int r_type; 1515 1516 r_type = ELF32_R_TYPE (rel->r_info); 1517 1518 if (r_type == R_METAG_GNU_VTINHERIT 1519 || r_type == R_METAG_GNU_VTENTRY 1520 || r_type == R_METAG_NONE) 1521 continue; 1522 1523 r_symndx = ELF32_R_SYM (rel->r_info); 1524 1525 howto = elf_metag_howto_table + ELF32_R_TYPE (rel->r_info); 1526 hh = NULL; 1527 sym = NULL; 1528 sec = NULL; 1529 1530 if (r_symndx < symtab_hdr->sh_info) 1531 { 1532 sym = local_syms + r_symndx; 1533 sec = local_sections [r_symndx]; 1534 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 1535 1536 name = bfd_elf_string_from_elf_section 1537 (input_bfd, symtab_hdr->sh_link, sym->st_name); 1538 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name; 1539 } 1540 else 1541 { 1542 struct elf_link_hash_entry *eh; 1543 bfd_boolean unresolved_reloc, warned, ignored; 1544 1545 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 1546 r_symndx, symtab_hdr, eh_syms, 1547 eh, sec, relocation, 1548 unresolved_reloc, warned, ignored); 1549 1550 name = eh->root.root.string; 1551 hh = (struct elf_metag_link_hash_entry *) eh; 1552 } 1553 1554 if (sec != NULL && discarded_section (sec)) 1555 METAG_RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, 1556 rel, relend, howto, contents); 1557 1558 if (info->relocatable) 1559 continue; 1560 1561 switch (r_type) 1562 { 1563 case R_METAG_ADDR32: 1564 case R_METAG_RELBRANCH: 1565 if ((input_section->flags & SEC_ALLOC) == 0) 1566 break; 1567 1568 if ((info->shared 1569 && r_symndx != STN_UNDEF 1570 && (input_section->flags & SEC_ALLOC) != 0 1571 && (r_type != R_METAG_RELBRANCH 1572 || !SYMBOL_CALLS_LOCAL (info, &hh->eh))) 1573 || (!info->shared 1574 && hh != NULL 1575 && hh->eh.dynindx != -1 1576 && !hh->eh.non_got_ref 1577 && ((hh->eh.def_dynamic 1578 && !hh->eh.def_regular) 1579 || hh->eh.root.type == bfd_link_hash_undefweak 1580 || hh->eh.root.type == bfd_link_hash_undefined))) 1581 { 1582 Elf_Internal_Rela outrel; 1583 bfd_boolean skip, relocate; 1584 bfd_byte *loc; 1585 1586 /* When generating a shared object, these relocations 1587 are copied into the output file to be resolved at run 1588 time. */ 1589 1590 sreloc = elf_section_data (input_section)->sreloc; 1591 BFD_ASSERT (sreloc != NULL); 1592 1593 skip = FALSE; 1594 relocate = FALSE; 1595 1596 outrel.r_offset = _bfd_elf_section_offset (output_bfd, 1597 info, 1598 input_section, 1599 rel->r_offset); 1600 if (outrel.r_offset == (bfd_vma) -1) 1601 skip = TRUE; 1602 else if (outrel.r_offset == (bfd_vma) -2) 1603 skip = TRUE, relocate = TRUE; 1604 outrel.r_offset += (input_section->output_section->vma 1605 + input_section->output_offset); 1606 1607 if (skip) 1608 { 1609 memset (&outrel, 0, sizeof outrel); 1610 outrel.r_info = ELF32_R_INFO (0, R_METAG_NONE); 1611 } 1612 else if (r_type == R_METAG_RELBRANCH) 1613 { 1614 BFD_ASSERT (hh != NULL && hh->eh.dynindx != -1); 1615 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type); 1616 outrel.r_addend = rel->r_addend; 1617 } 1618 else 1619 { 1620 /* h->dynindx may be -1 if this symbol was marked to 1621 become local. */ 1622 if (hh == NULL 1623 || ((info->symbolic || hh->eh.dynindx == -1) 1624 && hh->eh.def_regular)) 1625 { 1626 relocate = TRUE; 1627 outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE); 1628 outrel.r_addend = relocation + rel->r_addend; 1629 } 1630 else 1631 { 1632 BFD_ASSERT (hh->eh.dynindx != -1); 1633 outrel.r_info = ELF32_R_INFO (hh->eh.dynindx, r_type); 1634 outrel.r_addend = rel->r_addend; 1635 } 1636 } 1637 1638 loc = sreloc->contents; 1639 loc += sreloc->reloc_count * sizeof(Elf32_External_Rela); 1640 bfd_elf32_swap_reloca_out (output_bfd, &outrel,loc); 1641 ++sreloc->reloc_count; 1642 1643 /* If this reloc is against an external symbol, we do 1644 not want to fiddle with the addend. Otherwise, we 1645 need to include the symbol value so that it becomes 1646 an addend for the dynamic reloc. */ 1647 if (! relocate) 1648 continue; 1649 } 1650 break; 1651 1652 case R_METAG_RELBRANCH_PLT: 1653 /* Relocation is to the entry for this symbol in the 1654 procedure linkage table. */ 1655 1656 if (hh == NULL) 1657 break; 1658 1659 if (hh->eh.forced_local) 1660 break; 1661 1662 if (hh->eh.plt.offset == (bfd_vma) -1 || 1663 htab->splt == NULL) 1664 { 1665 /* We didn't make a PLT entry for this symbol. This 1666 happens when statically linking PIC code, or when 1667 using -Bsymbolic. */ 1668 break; 1669 } 1670 1671 relocation = (htab->splt->output_section->vma 1672 + htab->splt->output_offset 1673 + hh->eh.plt.offset); 1674 break; 1675 case R_METAG_HI16_GOTPC: 1676 case R_METAG_LO16_GOTPC: 1677 BFD_ASSERT (htab->sgot != NULL); 1678 1679 relocation = (htab->sgot->output_section->vma + 1680 htab->sgot->output_offset); 1681 relocation += GOT_REG_OFFSET; 1682 relocation -= (input_section->output_section->vma 1683 + input_section->output_offset 1684 + rel->r_offset); 1685 break; 1686 case R_METAG_HI16_GOTOFF: 1687 case R_METAG_LO16_GOTOFF: 1688 case R_METAG_GETSET_GOTOFF: 1689 BFD_ASSERT (htab->sgot != NULL); 1690 1691 relocation -= (htab->sgot->output_section->vma + 1692 htab->sgot->output_offset); 1693 relocation -= GOT_REG_OFFSET; 1694 break; 1695 case R_METAG_GETSET_GOT: 1696 { 1697 bfd_vma off; 1698 bfd_boolean do_got = 0; 1699 1700 /* Relocation is to the entry for this symbol in the 1701 global offset table. */ 1702 if (hh != NULL) 1703 { 1704 bfd_boolean dyn; 1705 1706 off = hh->eh.got.offset; 1707 dyn = htab->etab.dynamic_sections_created; 1708 if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, 1709 &hh->eh)) 1710 { 1711 /* If we aren't going to call finish_dynamic_symbol, 1712 then we need to handle initialisation of the .got 1713 entry and create needed relocs here. Since the 1714 offset must always be a multiple of 4, we use the 1715 least significant bit to record whether we have 1716 initialised it already. */ 1717 if ((off & 1) != 0) 1718 off &= ~1; 1719 else 1720 { 1721 hh->eh.got.offset |= 1; 1722 do_got = 1; 1723 } 1724 } 1725 } 1726 else 1727 { 1728 /* Local symbol case. */ 1729 if (local_got_offsets == NULL) 1730 abort (); 1731 1732 off = local_got_offsets[r_symndx]; 1733 1734 /* The offset must always be a multiple of 4. We use 1735 the least significant bit to record whether we have 1736 already generated the necessary reloc. */ 1737 if ((off & 1) != 0) 1738 off &= ~1; 1739 else 1740 { 1741 local_got_offsets[r_symndx] |= 1; 1742 do_got = 1; 1743 } 1744 } 1745 1746 if (do_got) 1747 { 1748 if (info->shared) 1749 { 1750 /* Output a dynamic relocation for this GOT entry. 1751 In this case it is relative to the base of the 1752 object because the symbol index is zero. */ 1753 Elf_Internal_Rela outrel; 1754 bfd_byte *loc; 1755 asection *s = htab->srelgot; 1756 1757 outrel.r_offset = (off 1758 + htab->sgot->output_offset 1759 + htab->sgot->output_section->vma); 1760 outrel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE); 1761 outrel.r_addend = relocation; 1762 loc = s->contents; 1763 loc += s->reloc_count++ * sizeof (Elf32_External_Rela); 1764 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 1765 } 1766 else 1767 bfd_put_32 (output_bfd, relocation, 1768 htab->sgot->contents + off); 1769 } 1770 1771 if (off >= (bfd_vma) -2) 1772 abort (); 1773 1774 relocation = off - GOT_REG_OFFSET; 1775 } 1776 break; 1777 case R_METAG_TLS_GD: 1778 case R_METAG_TLS_IE: 1779 { 1780 /* XXXMJF There is room here for optimisations. For example 1781 converting from GD->IE, etc. */ 1782 bfd_vma off; 1783 int indx; 1784 char tls_type; 1785 1786 if (htab->sgot == NULL) 1787 abort(); 1788 1789 indx = 0; 1790 if (hh != NULL) 1791 { 1792 bfd_boolean dyn; 1793 dyn = htab->etab.dynamic_sections_created; 1794 1795 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, &hh->eh) 1796 && (!info->shared 1797 || !SYMBOL_REFERENCES_LOCAL (info, &hh->eh))) 1798 { 1799 indx = hh->eh.dynindx; 1800 } 1801 off = hh->eh.got.offset; 1802 tls_type = hh->tls_type; 1803 } 1804 else 1805 { 1806 /* Local symbol case. */ 1807 if (local_got_offsets == NULL) 1808 abort (); 1809 1810 off = local_got_offsets[r_symndx]; 1811 tls_type = metag_elf_local_got_tls_type (input_bfd) [r_symndx]; 1812 } 1813 1814 if (tls_type == GOT_UNKNOWN) 1815 abort(); 1816 1817 if ((off & 1) != 0) 1818 off &= ~1; 1819 else 1820 { 1821 bfd_boolean need_relocs = FALSE; 1822 Elf_Internal_Rela outrel; 1823 bfd_byte *loc = NULL; 1824 int cur_off = off; 1825 1826 /* The GOT entries have not been initialized yet. Do it 1827 now, and emit any relocations. If both an IE GOT and a 1828 GD GOT are necessary, we emit the GD first. */ 1829 1830 if ((info->shared || indx != 0) 1831 && (hh == NULL 1832 || ELF_ST_VISIBILITY (hh->eh.other) == STV_DEFAULT 1833 || hh->eh.root.type != bfd_link_hash_undefweak)) 1834 { 1835 need_relocs = TRUE; 1836 loc = htab->srelgot->contents; 1837 /* FIXME (CAO): Should this be reloc_count++ ? */ 1838 loc += htab->srelgot->reloc_count * sizeof (Elf32_External_Rela); 1839 } 1840 1841 if (tls_type & GOT_TLS_GD) 1842 { 1843 if (need_relocs) 1844 { 1845 outrel.r_offset = (cur_off 1846 + htab->sgot->output_section->vma 1847 + htab->sgot->output_offset); 1848 outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_DTPMOD); 1849 outrel.r_addend = 0; 1850 bfd_put_32 (output_bfd, 0, htab->sgot->contents + cur_off); 1851 1852 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 1853 htab->srelgot->reloc_count++; 1854 loc += sizeof (Elf32_External_Rela); 1855 1856 if (indx == 0) 1857 bfd_put_32 (output_bfd, 0, 1858 htab->sgot->contents + cur_off + 4); 1859 else 1860 { 1861 bfd_put_32 (output_bfd, 0, 1862 htab->sgot->contents + cur_off + 4); 1863 outrel.r_info = ELF32_R_INFO (indx, 1864 R_METAG_TLS_DTPOFF); 1865 outrel.r_offset += 4; 1866 bfd_elf32_swap_reloca_out (output_bfd, 1867 &outrel, loc); 1868 htab->srelgot->reloc_count++; 1869 loc += sizeof (Elf32_External_Rela); 1870 } 1871 } 1872 else 1873 { 1874 /* We don't support changing the TLS model. */ 1875 abort (); 1876 } 1877 1878 cur_off += 8; 1879 } 1880 1881 if (tls_type & GOT_TLS_IE) 1882 { 1883 if (need_relocs) 1884 { 1885 outrel.r_offset = (cur_off 1886 + htab->sgot->output_section->vma 1887 + htab->sgot->output_offset); 1888 outrel.r_info = ELF32_R_INFO (indx, R_METAG_TLS_TPOFF); 1889 1890 if (indx == 0) 1891 outrel.r_addend = relocation - dtpoff_base (info); 1892 else 1893 outrel.r_addend = 0; 1894 1895 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 1896 htab->srelgot->reloc_count++; 1897 loc += sizeof (Elf32_External_Rela); 1898 } 1899 else 1900 bfd_put_32 (output_bfd, tpoff (info, relocation), 1901 htab->sgot->contents + cur_off); 1902 1903 cur_off += 4; 1904 } 1905 1906 if (hh != NULL) 1907 hh->eh.got.offset |= 1; 1908 else 1909 local_got_offsets[r_symndx] |= 1; 1910 } 1911 1912 /* Add the base of the GOT to the relocation value. */ 1913 relocation = off - GOT_REG_OFFSET; 1914 1915 break; 1916 } 1917 1918 case R_METAG_TLS_IENONPIC_HI16: 1919 case R_METAG_TLS_IENONPIC_LO16: 1920 case R_METAG_TLS_LE_HI16: 1921 case R_METAG_TLS_LE_LO16: 1922 if (info->shared) 1923 { 1924 (*_bfd_error_handler) 1925 (_("%B(%A+0x%lx): R_METAG_TLS_LE/IENONPIC relocation not permitted in shared object"), 1926 input_bfd, input_section, 1927 (long) rel->r_offset, howto->name); 1928 return FALSE; 1929 } 1930 else 1931 relocation = tpoff (info, relocation); 1932 break; 1933 case R_METAG_TLS_LDO_HI16: 1934 case R_METAG_TLS_LDO_LO16: 1935 if (! info->shared) 1936 relocation = tpoff (info, relocation); 1937 else 1938 relocation -= dtpoff_base (info); 1939 break; 1940 case R_METAG_TLS_LDM: 1941 { 1942 bfd_vma off; 1943 1944 if (htab->sgot == NULL) 1945 abort(); 1946 off = htab->tls_ldm_got.offset; 1947 if (off & 1) 1948 off &= ~1; 1949 else 1950 { 1951 Elf_Internal_Rela outrel; 1952 bfd_byte *loc; 1953 1954 outrel.r_offset = (off 1955 + htab->sgot->output_section->vma 1956 + htab->sgot->output_offset); 1957 1958 outrel.r_addend = 0; 1959 outrel.r_info = ELF32_R_INFO (0, R_METAG_TLS_DTPMOD); 1960 loc = htab->srelgot->contents; 1961 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela); 1962 bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc); 1963 htab->tls_ldm_got.offset |= 1; 1964 } 1965 1966 relocation = off - GOT_REG_OFFSET; 1967 break; 1968 } 1969 default: 1970 break; 1971 } 1972 1973 r = metag_final_link_relocate (howto, input_bfd, input_section, 1974 contents, rel, relocation, hh, htab, 1975 sec); 1976 1977 if (r != bfd_reloc_ok) 1978 { 1979 const char * msg = (const char *) NULL; 1980 1981 switch (r) 1982 { 1983 case bfd_reloc_overflow: 1984 r = info->callbacks->reloc_overflow 1985 (info, (hh ? &hh->eh.root : NULL), name, howto->name, 1986 (bfd_vma) 0, input_bfd, input_section, rel->r_offset); 1987 break; 1988 1989 case bfd_reloc_undefined: 1990 r = info->callbacks->undefined_symbol 1991 (info, name, input_bfd, input_section, rel->r_offset, 1992 TRUE); 1993 break; 1994 1995 case bfd_reloc_outofrange: 1996 msg = _("internal error: out of range error"); 1997 break; 1998 1999 case bfd_reloc_notsupported: 2000 msg = _("internal error: unsupported relocation error"); 2001 break; 2002 2003 case bfd_reloc_dangerous: 2004 msg = _("internal error: dangerous relocation"); 2005 break; 2006 2007 default: 2008 msg = _("internal error: unknown error"); 2009 break; 2010 } 2011 2012 if (msg) 2013 r = info->callbacks->warning 2014 (info, msg, name, input_bfd, input_section, rel->r_offset); 2015 2016 if (! r) 2017 return FALSE; 2018 } 2019 } 2020 2021 return TRUE; 2022 } 2023 2024 /* Create the .plt and .got sections, and set up our hash table 2025 short-cuts to various dynamic sections. */ 2026 2027 static bfd_boolean 2028 elf_metag_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 2029 { 2030 struct elf_metag_link_hash_table *htab; 2031 struct elf_link_hash_entry *eh; 2032 struct bfd_link_hash_entry *bh; 2033 const struct elf_backend_data *bed = get_elf_backend_data (abfd); 2034 2035 /* Don't try to create the .plt and .got twice. */ 2036 htab = metag_link_hash_table (info); 2037 if (htab->splt != NULL) 2038 return TRUE; 2039 2040 /* Call the generic code to do most of the work. */ 2041 if (! _bfd_elf_create_dynamic_sections (abfd, info)) 2042 return FALSE; 2043 2044 htab->sgot = bfd_get_linker_section (abfd, ".got"); 2045 if (! htab->sgot) 2046 return FALSE; 2047 2048 htab->sgotplt = bfd_make_section_with_flags (abfd, ".got.plt", 2049 (SEC_ALLOC | SEC_LOAD | 2050 SEC_HAS_CONTENTS | 2051 SEC_IN_MEMORY | 2052 SEC_LINKER_CREATED)); 2053 if (htab->sgotplt == NULL 2054 || !bfd_set_section_alignment (abfd, htab->sgotplt, 2)) 2055 return FALSE; 2056 2057 /* Define the symbol __GLOBAL_OFFSET_TABLE__ at the start of the .got 2058 section. We don't do this in the linker script because we don't want 2059 to define the symbol if we are not creating a global offset table. */ 2060 bh = NULL; 2061 if (!(_bfd_generic_link_add_one_symbol 2062 (info, abfd, "__GLOBAL_OFFSET_TABLE__", BSF_GLOBAL, htab->sgot, 2063 (bfd_vma) 0, NULL, FALSE, bed->collect, &bh))) 2064 return FALSE; 2065 eh = (struct elf_link_hash_entry *) bh; 2066 eh->def_regular = 1; 2067 eh->type = STT_OBJECT; 2068 eh->other = STV_HIDDEN; 2069 2070 if (! info->executable 2071 && ! bfd_elf_link_record_dynamic_symbol (info, eh)) 2072 return FALSE; 2073 2074 elf_hash_table (info)->hgot = eh; 2075 2076 htab->splt = bfd_get_linker_section (abfd, ".plt"); 2077 htab->srelplt = bfd_get_linker_section (abfd, ".rela.plt"); 2078 2079 htab->srelgot = bfd_get_linker_section (abfd, ".rela.got"); 2080 2081 htab->sdynbss = bfd_get_linker_section (abfd, ".dynbss"); 2082 htab->srelbss = bfd_get_linker_section (abfd, ".rela.bss"); 2083 2084 return TRUE; 2085 } 2086 2087 /* Look through the relocs for a section during the first phase, and 2088 calculate needed space in the global offset table, procedure linkage 2089 table, and dynamic reloc sections. At this point we haven't 2090 necessarily read all the input files. */ 2091 2092 static bfd_boolean 2093 elf_metag_check_relocs (bfd *abfd, 2094 struct bfd_link_info *info, 2095 asection *sec, 2096 const Elf_Internal_Rela *relocs) 2097 { 2098 Elf_Internal_Shdr *symtab_hdr; 2099 struct elf_link_hash_entry **eh_syms; 2100 const Elf_Internal_Rela *rel; 2101 const Elf_Internal_Rela *rel_end; 2102 struct elf_metag_link_hash_table *htab; 2103 asection *sreloc; 2104 bfd *dynobj; 2105 int tls_type = GOT_UNKNOWN, old_tls_type = GOT_UNKNOWN; 2106 2107 if (info->relocatable) 2108 return TRUE; 2109 2110 htab = metag_link_hash_table (info); 2111 dynobj = htab->etab.dynobj; 2112 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2113 eh_syms = elf_sym_hashes (abfd); 2114 sreloc = NULL; 2115 2116 if (htab == NULL) 2117 return FALSE; 2118 2119 rel_end = relocs + sec->reloc_count; 2120 for (rel = relocs; rel < rel_end; rel++) 2121 { 2122 int r_type; 2123 struct elf_metag_link_hash_entry *hh; 2124 Elf_Internal_Sym *isym; 2125 unsigned long r_symndx; 2126 2127 r_symndx = ELF32_R_SYM (rel->r_info); 2128 r_type = ELF32_R_TYPE (rel->r_info); 2129 if (r_symndx < symtab_hdr->sh_info) 2130 { 2131 /* A local symbol. */ 2132 isym = bfd_sym_from_r_symndx (&htab->sym_cache, 2133 abfd, r_symndx); 2134 if (isym == NULL) 2135 return FALSE; 2136 2137 hh = NULL; 2138 } 2139 else 2140 { 2141 isym = NULL; 2142 2143 hh = (struct elf_metag_link_hash_entry *) 2144 eh_syms[r_symndx - symtab_hdr->sh_info]; 2145 while (hh->eh.root.type == bfd_link_hash_indirect 2146 || hh->eh.root.type == bfd_link_hash_warning) 2147 hh = (struct elf_metag_link_hash_entry *) hh->eh.root.u.i.link; 2148 2149 /* PR15323, ref flags aren't set for references in the same 2150 object. */ 2151 hh->eh.root.non_ir_ref = 1; 2152 } 2153 2154 /* Some relocs require a global offset table. */ 2155 if (htab->sgot == NULL) 2156 { 2157 switch (r_type) 2158 { 2159 case R_METAG_TLS_GD: 2160 case R_METAG_TLS_LDM: 2161 case R_METAG_TLS_IE: 2162 if (info->shared) 2163 info->flags |= DF_STATIC_TLS; 2164 /* Fall through. */ 2165 2166 case R_METAG_HI16_GOTOFF: 2167 case R_METAG_LO16_GOTOFF: 2168 case R_METAG_GETSET_GOTOFF: 2169 case R_METAG_GETSET_GOT: 2170 case R_METAG_HI16_GOTPC: 2171 case R_METAG_LO16_GOTPC: 2172 if (dynobj == NULL) 2173 htab->etab.dynobj = dynobj = abfd; 2174 if (!elf_metag_create_dynamic_sections (dynobj, info)) 2175 return FALSE; 2176 break; 2177 2178 default: 2179 break; 2180 } 2181 } 2182 2183 switch (r_type) 2184 { 2185 case R_METAG_TLS_IE: 2186 case R_METAG_TLS_GD: 2187 case R_METAG_GETSET_GOT: 2188 switch (r_type) 2189 { 2190 default: 2191 tls_type = GOT_NORMAL; 2192 break; 2193 case R_METAG_TLS_IE: 2194 tls_type = GOT_TLS_IE; 2195 break; 2196 case R_METAG_TLS_GD: 2197 tls_type = GOT_TLS_GD; 2198 break; 2199 } 2200 2201 if (hh != NULL) 2202 { 2203 hh->eh.got.refcount += 1; 2204 old_tls_type = hh->tls_type; 2205 } 2206 else 2207 { 2208 bfd_signed_vma *local_got_refcounts; 2209 2210 /* This is a global offset table entry for a local 2211 symbol. */ 2212 local_got_refcounts = elf_local_got_refcounts (abfd); 2213 if (local_got_refcounts == NULL) 2214 { 2215 bfd_size_type size; 2216 2217 size = symtab_hdr->sh_info; 2218 size *= sizeof (bfd_signed_vma); 2219 /* Add in space to store the local GOT TLS types. */ 2220 size += symtab_hdr->sh_info; 2221 local_got_refcounts = ((bfd_signed_vma *) 2222 bfd_zalloc (abfd, size)); 2223 if (local_got_refcounts == NULL) 2224 return FALSE; 2225 elf_local_got_refcounts (abfd) = local_got_refcounts; 2226 memset (metag_elf_local_got_tls_type (abfd), 2227 GOT_UNKNOWN, symtab_hdr->sh_info); 2228 } 2229 local_got_refcounts[r_symndx] += 1; 2230 old_tls_type = metag_elf_local_got_tls_type (abfd) [r_symndx]; 2231 } 2232 2233 if (old_tls_type != tls_type) 2234 { 2235 if (hh != NULL) 2236 { 2237 hh->tls_type = tls_type; 2238 } 2239 else 2240 { 2241 metag_elf_local_got_tls_type (abfd) [r_symndx] = tls_type; 2242 } 2243 } 2244 2245 break; 2246 2247 case R_METAG_TLS_LDM: 2248 metag_link_hash_table (info)->tls_ldm_got.refcount += 1; 2249 break; 2250 2251 case R_METAG_RELBRANCH_PLT: 2252 /* This symbol requires a procedure linkage table entry. We 2253 actually build the entry in adjust_dynamic_symbol, 2254 because this might be a case of linking PIC code without 2255 linking in any dynamic objects, in which case we don't 2256 need to generate a procedure linkage table after all. */ 2257 2258 /* If this is a local symbol, we resolve it directly without 2259 creating a procedure linkage table entry. */ 2260 if (hh == NULL) 2261 continue; 2262 2263 if (hh->eh.forced_local) 2264 break; 2265 2266 hh->eh.needs_plt = 1; 2267 hh->eh.plt.refcount += 1; 2268 break; 2269 2270 case R_METAG_HIADDR16: 2271 case R_METAG_LOADDR16: 2272 /* Let's help debug shared library creation. These relocs 2273 cannot be used in shared libs. Don't error out for 2274 sections we don't care about, such as debug sections or 2275 non-constant sections. */ 2276 if (info->shared 2277 && (sec->flags & SEC_ALLOC) != 0 2278 && (sec->flags & SEC_READONLY) != 0) 2279 { 2280 const char *name; 2281 2282 if (hh) 2283 name = hh->eh.root.root.string; 2284 else 2285 name = bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL); 2286 (*_bfd_error_handler) 2287 (_("%B: relocation %s against `%s' can not be used when making a shared object; recompile with -fPIC"), 2288 abfd, elf_metag_howto_table[r_type].name, name); 2289 bfd_set_error (bfd_error_bad_value); 2290 return FALSE; 2291 } 2292 2293 /* Fall through. */ 2294 case R_METAG_ADDR32: 2295 case R_METAG_RELBRANCH: 2296 case R_METAG_GETSETOFF: 2297 if (hh != NULL && !info->shared) 2298 { 2299 hh->eh.non_got_ref = 1; 2300 hh->eh.plt.refcount += 1; 2301 } 2302 2303 /* If we are creating a shared library, and this is a reloc 2304 against a global symbol, or a non PC relative reloc 2305 against a local symbol, then we need to copy the reloc 2306 into the shared library. However, if we are linking with 2307 -Bsymbolic, we do not need to copy a reloc against a 2308 global symbol which is defined in an object we are 2309 including in the link (i.e., DEF_REGULAR is set). At 2310 this point we have not seen all the input files, so it is 2311 possible that DEF_REGULAR is not set now but will be set 2312 later (it is never cleared). We account for that 2313 possibility below by storing information in the 2314 dyn_relocs field of the hash table entry. A similar 2315 situation occurs when creating shared libraries and symbol 2316 visibility changes render the symbol local. 2317 2318 If on the other hand, we are creating an executable, we 2319 may need to keep relocations for symbols satisfied by a 2320 dynamic library if we manage to avoid copy relocs for the 2321 symbol. */ 2322 if ((info->shared 2323 && (sec->flags & SEC_ALLOC) != 0 2324 && (r_type != R_METAG_RELBRANCH 2325 || (hh != NULL 2326 && (! info->symbolic 2327 || hh->eh.root.type == bfd_link_hash_defweak 2328 || !hh->eh.def_regular)))) 2329 || (!info->shared 2330 && (sec->flags & SEC_ALLOC) != 0 2331 && hh != NULL 2332 && (hh->eh.root.type == bfd_link_hash_defweak 2333 || !hh->eh.def_regular))) 2334 { 2335 struct elf_metag_dyn_reloc_entry *hdh_p; 2336 struct elf_metag_dyn_reloc_entry **hdh_head; 2337 2338 if (dynobj == NULL) 2339 htab->etab.dynobj = dynobj = abfd; 2340 2341 /* When creating a shared object, we must copy these 2342 relocs into the output file. We create a reloc 2343 section in dynobj and make room for the reloc. */ 2344 if (sreloc == NULL) 2345 { 2346 sreloc = _bfd_elf_make_dynamic_reloc_section 2347 (sec, htab->etab.dynobj, 2, abfd, /*rela?*/ TRUE); 2348 2349 if (sreloc == NULL) 2350 { 2351 bfd_set_error (bfd_error_bad_value); 2352 return FALSE; 2353 } 2354 2355 elf_section_data (sec)->sreloc = sreloc; 2356 } 2357 2358 /* If this is a global symbol, we count the number of 2359 relocations we need for this symbol. */ 2360 if (hh != NULL) 2361 hdh_head = &((struct elf_metag_link_hash_entry *) hh)->dyn_relocs; 2362 else 2363 { 2364 /* Track dynamic relocs needed for local syms too. */ 2365 asection *sr; 2366 void *vpp; 2367 2368 sr = bfd_section_from_elf_index (abfd, isym->st_shndx); 2369 if (sr == NULL) 2370 sr = sec; 2371 2372 vpp = &elf_section_data (sr)->local_dynrel; 2373 hdh_head = (struct elf_metag_dyn_reloc_entry **) vpp; 2374 } 2375 2376 hdh_p = *hdh_head; 2377 if (hdh_p == NULL || hdh_p->sec != sec) 2378 { 2379 hdh_p = ((struct elf_metag_dyn_reloc_entry *) 2380 bfd_alloc (dynobj, sizeof *hdh_p)); 2381 if (hdh_p == NULL) 2382 return FALSE; 2383 hdh_p->hdh_next = *hdh_head; 2384 *hdh_head = hdh_p; 2385 hdh_p->sec = sec; 2386 hdh_p->count = 0; 2387 hdh_p->relative_count = 0; 2388 } 2389 2390 hdh_p->count += 1; 2391 if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH) 2392 hdh_p->relative_count += 1; 2393 } 2394 break; 2395 2396 /* This relocation describes the C++ object vtable hierarchy. 2397 Reconstruct it for later use during GC. */ 2398 case R_METAG_GNU_VTINHERIT: 2399 if (!bfd_elf_gc_record_vtinherit (abfd, sec, &hh->eh, 2400 rel->r_offset)) 2401 return FALSE; 2402 break; 2403 2404 /* This relocation describes which C++ vtable entries are actually 2405 used. Record for later use during GC. */ 2406 case R_METAG_GNU_VTENTRY: 2407 BFD_ASSERT (hh != NULL); 2408 if (hh != NULL 2409 && !bfd_elf_gc_record_vtentry (abfd, sec, &hh->eh, rel->r_addend)) 2410 return FALSE; 2411 break; 2412 } 2413 } 2414 2415 return TRUE; 2416 } 2417 2418 /* Copy the extra info we tack onto an elf_link_hash_entry. */ 2419 2420 static void 2421 elf_metag_copy_indirect_symbol (struct bfd_link_info *info, 2422 struct elf_link_hash_entry *eh_dir, 2423 struct elf_link_hash_entry *eh_ind) 2424 { 2425 struct elf_metag_link_hash_entry *hh_dir, *hh_ind; 2426 2427 hh_dir = metag_elf_hash_entry (eh_dir); 2428 hh_ind = metag_elf_hash_entry (eh_ind); 2429 2430 if (hh_ind->dyn_relocs != NULL) 2431 { 2432 if (hh_dir->dyn_relocs != NULL) 2433 { 2434 struct elf_metag_dyn_reloc_entry **hdh_pp; 2435 struct elf_metag_dyn_reloc_entry *hdh_p; 2436 2437 if (eh_ind->root.type == bfd_link_hash_indirect) 2438 abort (); 2439 2440 /* Add reloc counts against the weak sym to the strong sym 2441 list. Merge any entries against the same section. */ 2442 for (hdh_pp = &hh_ind->dyn_relocs; (hdh_p = *hdh_pp) != NULL; ) 2443 { 2444 struct elf_metag_dyn_reloc_entry *hdh_q; 2445 2446 for (hdh_q = hh_dir->dyn_relocs; hdh_q != NULL; 2447 hdh_q = hdh_q->hdh_next) 2448 if (hdh_q->sec == hdh_p->sec) 2449 { 2450 hdh_q->relative_count += hdh_p->relative_count; 2451 hdh_q->count += hdh_p->count; 2452 *hdh_pp = hdh_p->hdh_next; 2453 break; 2454 } 2455 if (hdh_q == NULL) 2456 hdh_pp = &hdh_p->hdh_next; 2457 } 2458 *hdh_pp = hh_dir->dyn_relocs; 2459 } 2460 2461 hh_dir->dyn_relocs = hh_ind->dyn_relocs; 2462 hh_ind->dyn_relocs = NULL; 2463 } 2464 2465 if (eh_ind->root.type == bfd_link_hash_indirect 2466 && eh_dir->got.refcount <= 0) 2467 { 2468 hh_dir->tls_type = hh_ind->tls_type; 2469 hh_ind->tls_type = GOT_UNKNOWN; 2470 } 2471 2472 _bfd_elf_link_hash_copy_indirect (info, eh_dir, eh_ind); 2473 } 2474 2475 /* Adjust a symbol defined by a dynamic object and referenced by a 2476 regular object. The current definition is in some section of the 2477 dynamic object, but we're not including those sections. We have to 2478 change the definition to something the rest of the link can 2479 understand. */ 2480 2481 static bfd_boolean 2482 elf_metag_adjust_dynamic_symbol (struct bfd_link_info *info, 2483 struct elf_link_hash_entry *eh) 2484 { 2485 struct elf_metag_link_hash_table *htab; 2486 struct elf_metag_link_hash_entry *hh; 2487 struct elf_metag_dyn_reloc_entry *hdh_p; 2488 asection *s; 2489 2490 /* If this is a function, put it in the procedure linkage table. We 2491 will fill in the contents of the procedure linkage table later, 2492 when we know the address of the .got section. */ 2493 if (eh->type == STT_FUNC 2494 || eh->needs_plt) 2495 { 2496 if (eh->plt.refcount <= 0 2497 || SYMBOL_CALLS_LOCAL (info, eh) 2498 || (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT 2499 && eh->root.type == bfd_link_hash_undefweak)) 2500 { 2501 /* This case can occur if we saw a PLT reloc in an input 2502 file, but the symbol was never referred to by a dynamic 2503 object. In such a case, we don't actually need to build 2504 a procedure linkage table, and we can just do a PCREL 2505 reloc instead. */ 2506 eh->plt.offset = (bfd_vma) -1; 2507 eh->needs_plt = 0; 2508 } 2509 2510 return TRUE; 2511 } 2512 else 2513 eh->plt.offset = (bfd_vma) -1; 2514 2515 /* If this is a weak symbol, and there is a real definition, the 2516 processor independent code will have arranged for us to see the 2517 real definition first, and we can just use the same value. */ 2518 if (eh->u.weakdef != NULL) 2519 { 2520 if (eh->u.weakdef->root.type != bfd_link_hash_defined 2521 && eh->u.weakdef->root.type != bfd_link_hash_defweak) 2522 abort (); 2523 eh->root.u.def.section = eh->u.weakdef->root.u.def.section; 2524 eh->root.u.def.value = eh->u.weakdef->root.u.def.value; 2525 eh->non_got_ref = eh->u.weakdef->non_got_ref; 2526 return TRUE; 2527 } 2528 2529 /* This is a reference to a symbol defined by a dynamic object which 2530 is not a function. */ 2531 2532 /* If we are creating a shared library, we must presume that the 2533 only references to the symbol are via the global offset table. 2534 For such cases we need not do anything here; the relocations will 2535 be handled correctly by relocate_section. */ 2536 if (info->shared) 2537 return TRUE; 2538 2539 /* If there are no references to this symbol that do not use the 2540 GOT, we don't need to generate a copy reloc. */ 2541 if (!eh->non_got_ref) 2542 return TRUE; 2543 2544 /* If -z nocopyreloc was given, we won't generate them either. */ 2545 if (info->nocopyreloc) 2546 { 2547 eh->non_got_ref = 0; 2548 return TRUE; 2549 } 2550 2551 hh = (struct elf_metag_link_hash_entry *) eh; 2552 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next) 2553 { 2554 s = hdh_p->sec->output_section; 2555 if (s != NULL && (s->flags & SEC_READONLY) != 0) 2556 break; 2557 } 2558 2559 /* If we didn't find any dynamic relocs in read-only sections, then 2560 we'll be keeping the dynamic relocs and avoiding the copy reloc. */ 2561 if (hdh_p == NULL) 2562 { 2563 eh->non_got_ref = 0; 2564 return TRUE; 2565 } 2566 2567 /* We must allocate the symbol in our .dynbss section, which will 2568 become part of the .bss section of the executable. There will be 2569 an entry for this symbol in the .dynsym section. The dynamic 2570 object will contain position independent code, so all references 2571 from the dynamic object to this symbol will go through the global 2572 offset table. The dynamic linker will use the .dynsym entry to 2573 determine the address it must put in the global offset table, so 2574 both the dynamic object and the regular object will refer to the 2575 same memory location for the variable. */ 2576 2577 htab = metag_link_hash_table (info); 2578 2579 /* We must generate a COPY reloc to tell the dynamic linker to 2580 copy the initial value out of the dynamic object and into the 2581 runtime process image. */ 2582 if ((eh->root.u.def.section->flags & SEC_ALLOC) != 0 && eh->size != 0) 2583 { 2584 htab->srelbss->size += sizeof (Elf32_External_Rela); 2585 eh->needs_copy = 1; 2586 } 2587 2588 s = htab->sdynbss; 2589 2590 return _bfd_elf_adjust_dynamic_copy (eh, s); 2591 } 2592 2593 /* Allocate space in .plt, .got and associated reloc sections for 2594 global syms. */ 2595 2596 static bfd_boolean 2597 allocate_dynrelocs (struct elf_link_hash_entry *eh, void *inf) 2598 { 2599 struct bfd_link_info *info; 2600 struct elf_metag_link_hash_table *htab; 2601 struct elf_metag_link_hash_entry *hh; 2602 struct elf_metag_dyn_reloc_entry *hdh_p; 2603 2604 if (eh->root.type == bfd_link_hash_indirect) 2605 return TRUE; 2606 2607 if (eh->root.type == bfd_link_hash_warning) 2608 eh = (struct elf_link_hash_entry *) eh->root.u.i.link; 2609 2610 info = inf; 2611 htab = metag_link_hash_table (info); 2612 2613 if (htab->etab.dynamic_sections_created 2614 && eh->plt.refcount > 0) 2615 { 2616 /* Make sure this symbol is output as a dynamic symbol. 2617 Undefined weak syms won't yet be marked as dynamic. */ 2618 if (eh->dynindx == -1 2619 && !eh->forced_local) 2620 { 2621 if (! bfd_elf_link_record_dynamic_symbol (info, eh)) 2622 return FALSE; 2623 } 2624 2625 if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, info->shared, eh)) 2626 { 2627 asection *s = htab->splt; 2628 2629 /* If this is the first .plt entry, make room for the special 2630 first entry. */ 2631 if (s->size == 0) 2632 s->size += PLT_ENTRY_SIZE; 2633 2634 eh->plt.offset = s->size; 2635 2636 /* If this symbol is not defined in a regular file, and we are 2637 not generating a shared library, then set the symbol to this 2638 location in the .plt. This is required to make function 2639 pointers compare as equal between the normal executable and 2640 the shared library. */ 2641 if (! info->shared 2642 && !eh->def_regular) 2643 { 2644 eh->root.u.def.section = s; 2645 eh->root.u.def.value = eh->plt.offset; 2646 } 2647 2648 /* Make room for this entry. */ 2649 s->size += PLT_ENTRY_SIZE; 2650 2651 /* We also need to make an entry in the .got.plt section, which 2652 will be placed in the .got section by the linker script. */ 2653 htab->sgotplt->size += 4; 2654 2655 /* We also need to make an entry in the .rel.plt section. */ 2656 htab->srelplt->size += sizeof (Elf32_External_Rela); 2657 } 2658 else 2659 { 2660 eh->plt.offset = (bfd_vma) -1; 2661 eh->needs_plt = 0; 2662 } 2663 } 2664 else 2665 { 2666 eh->plt.offset = (bfd_vma) -1; 2667 eh->needs_plt = 0; 2668 } 2669 2670 if (eh->got.refcount > 0) 2671 { 2672 asection *s; 2673 bfd_boolean dyn; 2674 int tls_type = metag_elf_hash_entry (eh)->tls_type; 2675 2676 /* Make sure this symbol is output as a dynamic symbol. 2677 Undefined weak syms won't yet be marked as dynamic. */ 2678 if (eh->dynindx == -1 2679 && !eh->forced_local) 2680 { 2681 if (! bfd_elf_link_record_dynamic_symbol (info, eh)) 2682 return FALSE; 2683 } 2684 2685 s = htab->sgot; 2686 2687 eh->got.offset = s->size; 2688 s->size += 4; 2689 /* R_METAG_TLS_GD needs 2 consecutive GOT slots. */ 2690 if (tls_type == GOT_TLS_GD) 2691 s->size += 4; 2692 dyn = htab->etab.dynamic_sections_created; 2693 /* R_METAG_TLS_IE needs one dynamic relocation if dynamic, 2694 R_METAG_TLS_GD needs one if local symbol and two if global. */ 2695 if ((tls_type == GOT_TLS_GD && eh->dynindx == -1) 2696 || (tls_type == GOT_TLS_IE && dyn)) 2697 htab->srelgot->size += sizeof (Elf32_External_Rela); 2698 else if (tls_type == GOT_TLS_GD) 2699 htab->srelgot->size += 2 * sizeof (Elf32_External_Rela); 2700 else if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, info->shared, eh)) 2701 htab->srelgot->size += sizeof (Elf32_External_Rela); 2702 } 2703 else 2704 eh->got.offset = (bfd_vma) -1; 2705 2706 hh = (struct elf_metag_link_hash_entry *) eh; 2707 if (hh->dyn_relocs == NULL) 2708 return TRUE; 2709 2710 /* If this is a -Bsymbolic shared link, then we need to discard all 2711 space allocated for dynamic pc-relative relocs against symbols 2712 defined in a regular object. For the normal shared case, discard 2713 space for relocs that have become local due to symbol visibility 2714 changes. */ 2715 if (info->shared) 2716 { 2717 if (SYMBOL_CALLS_LOCAL (info, eh)) 2718 { 2719 struct elf_metag_dyn_reloc_entry **hdh_pp; 2720 2721 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; ) 2722 { 2723 hdh_p->count -= hdh_p->relative_count; 2724 hdh_p->relative_count = 0; 2725 if (hdh_p->count == 0) 2726 *hdh_pp = hdh_p->hdh_next; 2727 else 2728 hdh_pp = &hdh_p->hdh_next; 2729 } 2730 } 2731 2732 /* Also discard relocs on undefined weak syms with non-default 2733 visibility. */ 2734 if (hh->dyn_relocs != NULL 2735 && eh->root.type == bfd_link_hash_undefweak) 2736 { 2737 if (ELF_ST_VISIBILITY (eh->other) != STV_DEFAULT) 2738 hh->dyn_relocs = NULL; 2739 2740 /* Make sure undefined weak symbols are output as a dynamic 2741 symbol in PIEs. */ 2742 else if (eh->dynindx == -1 2743 && !eh->forced_local) 2744 { 2745 if (! bfd_elf_link_record_dynamic_symbol (info, eh)) 2746 return FALSE; 2747 } 2748 } 2749 } 2750 else 2751 { 2752 /* For the non-shared case, discard space for relocs against 2753 symbols which turn out to need copy relocs or are not 2754 dynamic. */ 2755 if (!eh->non_got_ref 2756 && ((eh->def_dynamic 2757 && !eh->def_regular) 2758 || (htab->etab.dynamic_sections_created 2759 && (eh->root.type == bfd_link_hash_undefweak 2760 || eh->root.type == bfd_link_hash_undefined)))) 2761 { 2762 /* Make sure this symbol is output as a dynamic symbol. 2763 Undefined weak syms won't yet be marked as dynamic. */ 2764 if (eh->dynindx == -1 2765 && !eh->forced_local) 2766 { 2767 if (! bfd_elf_link_record_dynamic_symbol (info, eh)) 2768 return FALSE; 2769 } 2770 2771 /* If that succeeded, we know we'll be keeping all the 2772 relocs. */ 2773 if (eh->dynindx != -1) 2774 goto keep; 2775 } 2776 2777 hh->dyn_relocs = NULL; 2778 return TRUE; 2779 2780 keep: ; 2781 } 2782 2783 /* Finally, allocate space. */ 2784 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next) 2785 { 2786 asection *sreloc = elf_section_data (hdh_p->sec)->sreloc; 2787 sreloc->size += hdh_p->count * sizeof (Elf32_External_Rela); 2788 } 2789 2790 return TRUE; 2791 } 2792 2793 /* Find any dynamic relocs that apply to read-only sections. */ 2794 2795 static bfd_boolean 2796 readonly_dynrelocs (struct elf_link_hash_entry *eh, void *inf) 2797 { 2798 struct elf_metag_link_hash_entry *hh; 2799 struct elf_metag_dyn_reloc_entry *hdh_p; 2800 2801 if (eh->root.type == bfd_link_hash_warning) 2802 eh = (struct elf_link_hash_entry *) eh->root.u.i.link; 2803 2804 hh = (struct elf_metag_link_hash_entry *) eh; 2805 for (hdh_p = hh->dyn_relocs; hdh_p != NULL; hdh_p = hdh_p->hdh_next) 2806 { 2807 asection *s = hdh_p->sec->output_section; 2808 2809 if (s != NULL && (s->flags & SEC_READONLY) != 0) 2810 { 2811 struct bfd_link_info *info = inf; 2812 2813 info->flags |= DF_TEXTREL; 2814 2815 /* Not an error, just cut short the traversal. */ 2816 return FALSE; 2817 } 2818 } 2819 return TRUE; 2820 } 2821 2822 /* Set the sizes of the dynamic sections. */ 2823 2824 static bfd_boolean 2825 elf_metag_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED, 2826 struct bfd_link_info *info) 2827 { 2828 struct elf_metag_link_hash_table *htab; 2829 bfd *dynobj; 2830 bfd *ibfd; 2831 asection *s; 2832 bfd_boolean relocs; 2833 2834 htab = metag_link_hash_table (info); 2835 dynobj = htab->etab.dynobj; 2836 if (dynobj == NULL) 2837 abort (); 2838 2839 if (htab->etab.dynamic_sections_created) 2840 { 2841 /* Set the contents of the .interp section to the interpreter. */ 2842 if (info->executable) 2843 { 2844 s = bfd_get_linker_section (dynobj, ".interp"); 2845 if (s == NULL) 2846 abort (); 2847 s->size = sizeof ELF_DYNAMIC_INTERPRETER; 2848 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; 2849 } 2850 } 2851 2852 /* Set up .got offsets for local syms, and space for local dynamic 2853 relocs. */ 2854 for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next) 2855 { 2856 bfd_signed_vma *local_got; 2857 bfd_signed_vma *end_local_got; 2858 bfd_size_type locsymcount; 2859 Elf_Internal_Shdr *symtab_hdr; 2860 asection *srel; 2861 char *local_tls_type; 2862 2863 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour) 2864 continue; 2865 2866 for (s = ibfd->sections; s != NULL; s = s->next) 2867 { 2868 struct elf_metag_dyn_reloc_entry *hdh_p; 2869 2870 for (hdh_p = ((struct elf_metag_dyn_reloc_entry *) 2871 elf_section_data (s)->local_dynrel); 2872 hdh_p != NULL; 2873 hdh_p = hdh_p->hdh_next) 2874 { 2875 if (!bfd_is_abs_section (hdh_p->sec) 2876 && bfd_is_abs_section (hdh_p->sec->output_section)) 2877 { 2878 /* Input section has been discarded, either because 2879 it is a copy of a linkonce section or due to 2880 linker script /DISCARD/, so we'll be discarding 2881 the relocs too. */ 2882 } 2883 else if (hdh_p->count != 0) 2884 { 2885 srel = elf_section_data (hdh_p->sec)->sreloc; 2886 srel->size += hdh_p->count * sizeof (Elf32_External_Rela); 2887 if ((hdh_p->sec->output_section->flags & SEC_READONLY) != 0) 2888 info->flags |= DF_TEXTREL; 2889 } 2890 } 2891 } 2892 2893 local_got = elf_local_got_refcounts (ibfd); 2894 if (!local_got) 2895 continue; 2896 2897 symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; 2898 locsymcount = symtab_hdr->sh_info; 2899 end_local_got = local_got + locsymcount; 2900 local_tls_type = metag_elf_local_got_tls_type (ibfd); 2901 s = htab->sgot; 2902 srel = htab->srelgot; 2903 for (; local_got < end_local_got; ++local_got) 2904 { 2905 if (*local_got > 0) 2906 { 2907 *local_got = s->size; 2908 s->size += GOT_ENTRY_SIZE; 2909 /* R_METAG_TLS_GD relocs need 2 consecutive GOT entries. */ 2910 if (*local_tls_type == GOT_TLS_GD) 2911 s->size += 4; 2912 if (info->shared) 2913 srel->size += sizeof (Elf32_External_Rela); 2914 } 2915 else 2916 *local_got = (bfd_vma) -1; 2917 ++local_tls_type; 2918 } 2919 } 2920 2921 if (htab->tls_ldm_got.refcount > 0) 2922 { 2923 /* Allocate 2 got entries and 1 dynamic reloc for R_METAG_TLS_LDM 2924 reloc. */ 2925 htab->tls_ldm_got.offset = htab->sgot->size; 2926 htab->sgot->size += 8; 2927 htab->srelgot->size += sizeof (Elf32_External_Rela); 2928 } 2929 else 2930 htab->tls_ldm_got.offset = -1; 2931 2932 /* Allocate global sym .plt and .got entries, and space for global 2933 sym dynamic relocs. */ 2934 elf_link_hash_traverse (&htab->etab, allocate_dynrelocs, info); 2935 2936 /* We now have determined the sizes of the various dynamic sections. 2937 Allocate memory for them. */ 2938 relocs = FALSE; 2939 for (s = dynobj->sections; s != NULL; s = s->next) 2940 { 2941 bfd_boolean reloc_section = FALSE; 2942 2943 if ((s->flags & SEC_LINKER_CREATED) == 0) 2944 continue; 2945 2946 if (s == htab->splt 2947 || s == htab->sgot 2948 || s == htab->sgotplt 2949 || s == htab->sdynbss) 2950 { 2951 /* Strip this section if we don't need it; see the 2952 comment below. */ 2953 } 2954 else if (CONST_STRNEQ (bfd_get_section_name (dynobj, s), ".rela")) 2955 { 2956 if (s->size != 0 && s != htab->srelplt) 2957 relocs = TRUE; 2958 2959 /* We use the reloc_count field as a counter if we need 2960 to copy relocs into the output file. */ 2961 s->reloc_count = 0; 2962 reloc_section = TRUE; 2963 } 2964 else 2965 { 2966 /* It's not one of our sections, so don't allocate space. */ 2967 continue; 2968 } 2969 2970 if (s->size == 0) 2971 { 2972 /* If we don't need this section, strip it from the 2973 output file. This is mostly to handle .rela.bss and 2974 .rela.plt. We must create both sections in 2975 create_dynamic_sections, because they must be created 2976 before the linker maps input sections to output 2977 sections. The linker does that before 2978 adjust_dynamic_symbol is called, and it is that 2979 function which decides whether anything needs to go 2980 into these sections. */ 2981 s->flags |= SEC_EXCLUDE; 2982 continue; 2983 } 2984 2985 if ((s->flags & SEC_HAS_CONTENTS) == 0) 2986 continue; 2987 2988 /* Allocate memory for the section contents. */ 2989 s->contents = bfd_zalloc (dynobj, s->size); 2990 if (s->contents == NULL) 2991 return FALSE; 2992 else if (reloc_section) 2993 { 2994 unsigned char *contents = s->contents; 2995 Elf32_External_Rela reloc; 2996 2997 /* Fill the reloc section with a R_METAG_NONE type reloc. */ 2998 memset(&reloc, 0, sizeof(Elf32_External_Rela)); 2999 reloc.r_info[0] = R_METAG_NONE; 3000 for (; contents < (s->contents + s->size); 3001 contents += sizeof(Elf32_External_Rela)) 3002 { 3003 memcpy(contents, &reloc, sizeof(Elf32_External_Rela)); 3004 } 3005 } 3006 } 3007 3008 if (htab->etab.dynamic_sections_created) 3009 { 3010 /* Add some entries to the .dynamic section. We fill in the 3011 values later, in elf_metag_finish_dynamic_sections, but we 3012 must add the entries now so that we get the correct size for 3013 the .dynamic section. The DT_DEBUG entry is filled in by the 3014 dynamic linker and used by the debugger. */ 3015 #define add_dynamic_entry(TAG, VAL) \ 3016 _bfd_elf_add_dynamic_entry (info, TAG, VAL) 3017 3018 if (!add_dynamic_entry (DT_PLTGOT, 0)) 3019 return FALSE; 3020 3021 if (info->executable) 3022 { 3023 if (!add_dynamic_entry (DT_DEBUG, 0)) 3024 return FALSE; 3025 } 3026 3027 if (htab->srelplt->size != 0) 3028 { 3029 if (!add_dynamic_entry (DT_PLTRELSZ, 0) 3030 || !add_dynamic_entry (DT_PLTREL, DT_RELA) 3031 || !add_dynamic_entry (DT_JMPREL, 0)) 3032 return FALSE; 3033 } 3034 3035 if (relocs) 3036 { 3037 if (!add_dynamic_entry (DT_RELA, 0) 3038 || !add_dynamic_entry (DT_RELASZ, 0) 3039 || !add_dynamic_entry (DT_RELAENT, sizeof (Elf32_External_Rela))) 3040 return FALSE; 3041 3042 /* If any dynamic relocs apply to a read-only section, 3043 then we need a DT_TEXTREL entry. */ 3044 if ((info->flags & DF_TEXTREL) == 0) 3045 elf_link_hash_traverse (&htab->etab, readonly_dynrelocs, info); 3046 3047 if ((info->flags & DF_TEXTREL) != 0) 3048 { 3049 if (!add_dynamic_entry (DT_TEXTREL, 0)) 3050 return FALSE; 3051 } 3052 } 3053 } 3054 #undef add_dynamic_entry 3055 3056 return TRUE; 3057 } 3058 3059 /* Finish up dynamic symbol handling. We set the contents of various 3060 dynamic sections here. */ 3061 3062 static bfd_boolean 3063 elf_metag_finish_dynamic_symbol (bfd *output_bfd, 3064 struct bfd_link_info *info, 3065 struct elf_link_hash_entry *eh, 3066 Elf_Internal_Sym *sym) 3067 { 3068 struct elf_metag_link_hash_table *htab; 3069 Elf_Internal_Rela rel; 3070 bfd_byte *loc; 3071 3072 htab = metag_link_hash_table (info); 3073 3074 if (eh->plt.offset != (bfd_vma) -1) 3075 { 3076 asection *splt; 3077 asection *sgot; 3078 asection *srela; 3079 3080 bfd_vma plt_index; 3081 bfd_vma got_offset; 3082 bfd_vma got_entry; 3083 3084 if (eh->plt.offset & 1) 3085 abort (); 3086 3087 BFD_ASSERT (eh->dynindx != -1); 3088 3089 splt = htab->splt; 3090 sgot = htab->sgotplt; 3091 srela = htab->srelplt; 3092 BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL); 3093 3094 /* Get the index in the procedure linkage table which 3095 corresponds to this symbol. This is the index of this symbol 3096 in all the symbols for which we are making plt entries. The 3097 first entry in the procedure linkage table is reserved. */ 3098 plt_index = eh->plt.offset / PLT_ENTRY_SIZE - 1; 3099 3100 /* Get the offset into the .got.plt table of the entry that 3101 corresponds to this function. */ 3102 got_offset = plt_index * GOT_ENTRY_SIZE; 3103 3104 BFD_ASSERT (got_offset < (1 << 16)); 3105 3106 got_entry = sgot->output_section->vma 3107 + sgot->output_offset 3108 + got_offset; 3109 3110 BFD_ASSERT (plt_index < (1 << 16)); 3111 3112 /* Fill in the entry in the procedure linkage table. */ 3113 if (! info->shared) 3114 { 3115 bfd_put_32 (output_bfd, 3116 (plt_entry[0] 3117 | (((got_entry >> 16) & 0xffff) << 3)), 3118 splt->contents + eh->plt.offset); 3119 bfd_put_32 (output_bfd, 3120 (plt_entry[1] 3121 | ((got_entry & 0xffff) << 3)), 3122 splt->contents + eh->plt.offset + 4); 3123 bfd_put_32 (output_bfd, plt_entry[2], 3124 splt->contents + eh->plt.offset + 8); 3125 bfd_put_32 (output_bfd, 3126 (plt_entry[3] | (plt_index << 3)), 3127 splt->contents + eh->plt.offset + 12); 3128 bfd_put_32 (output_bfd, 3129 (plt_entry[4] 3130 | ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)), 3131 splt->contents + eh->plt.offset + 16); 3132 } 3133 else 3134 { 3135 bfd_vma addr = got_entry - (splt->output_section->vma + 3136 splt->output_offset + eh->plt.offset); 3137 3138 bfd_put_32 (output_bfd, 3139 plt_pic_entry[0] | (((addr >> 16) & 0xffff) << 3), 3140 splt->contents + eh->plt.offset); 3141 bfd_put_32 (output_bfd, 3142 plt_pic_entry[1] | ((addr & 0xffff) << 3), 3143 splt->contents + eh->plt.offset + 4); 3144 bfd_put_32 (output_bfd, plt_pic_entry[2], 3145 splt->contents + eh->plt.offset + 8); 3146 bfd_put_32 (output_bfd, 3147 (plt_pic_entry[3] | (plt_index << 3)), 3148 splt->contents + eh->plt.offset + 12); 3149 bfd_put_32 (output_bfd, 3150 (plt_pic_entry[4] 3151 + ((((unsigned int) ((- (eh->plt.offset + 16)) >> 2)) & 0x7ffff) << 5)), 3152 splt->contents + eh->plt.offset + 16); 3153 } 3154 3155 /* Fill in the entry in the global offset table. */ 3156 bfd_put_32 (output_bfd, 3157 (splt->output_section->vma 3158 + splt->output_offset 3159 + eh->plt.offset 3160 + 12), /* offset within PLT entry */ 3161 sgot->contents + got_offset); 3162 3163 /* Fill in the entry in the .rela.plt section. */ 3164 rel.r_offset = (sgot->output_section->vma 3165 + sgot->output_offset 3166 + got_offset); 3167 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_JMP_SLOT); 3168 rel.r_addend = 0; 3169 loc = htab->srelplt->contents; 3170 loc += plt_index * sizeof(Elf32_External_Rela); 3171 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 3172 3173 if (!eh->def_regular) 3174 { 3175 /* Mark the symbol as undefined, rather than as defined in 3176 the .plt section. Leave the value alone. */ 3177 sym->st_shndx = SHN_UNDEF; 3178 } 3179 } 3180 3181 if (eh->got.offset != (bfd_vma) -1 3182 && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_GD) == 0 3183 && (metag_elf_hash_entry (eh)->tls_type & GOT_TLS_IE) == 0) 3184 { 3185 /* This symbol has an entry in the global offset table. Set it 3186 up. */ 3187 3188 rel.r_offset = ((eh->got.offset &~ (bfd_vma) 1) 3189 + htab->sgot->output_offset 3190 + htab->sgot->output_section->vma); 3191 3192 /* If this is a -Bsymbolic link and the symbol is defined 3193 locally or was forced to be local because of a version file, 3194 we just want to emit a RELATIVE reloc. The entry in the 3195 global offset table will already have been initialized in the 3196 relocate_section function. */ 3197 if (info->shared 3198 && (info->symbolic || eh->dynindx == -1) 3199 && eh->def_regular) 3200 { 3201 rel.r_info = ELF32_R_INFO (0, R_METAG_RELATIVE); 3202 rel.r_addend = (eh->root.u.def.value 3203 + eh->root.u.def.section->output_offset 3204 + eh->root.u.def.section->output_section->vma); 3205 } 3206 else 3207 { 3208 if ((eh->got.offset & 1) != 0) 3209 abort (); 3210 bfd_put_32 (output_bfd, 0, htab->sgot->contents + eh->got.offset); 3211 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_GLOB_DAT); 3212 rel.r_addend = 0; 3213 } 3214 3215 loc = htab->srelgot->contents; 3216 loc += htab->srelgot->reloc_count++ * sizeof (Elf32_External_Rela); 3217 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 3218 } 3219 3220 if (eh->needs_copy) 3221 { 3222 asection *s; 3223 3224 /* This symbol needs a copy reloc. Set it up. */ 3225 3226 if (! (eh->dynindx != -1 3227 && (eh->root.type == bfd_link_hash_defined 3228 || eh->root.type == bfd_link_hash_defweak))) 3229 abort (); 3230 3231 s = htab->srelbss; 3232 3233 rel.r_offset = (eh->root.u.def.value 3234 + eh->root.u.def.section->output_offset 3235 + eh->root.u.def.section->output_section->vma); 3236 rel.r_addend = 0; 3237 rel.r_info = ELF32_R_INFO (eh->dynindx, R_METAG_COPY); 3238 loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela); 3239 bfd_elf32_swap_reloca_out (output_bfd, &rel, loc); 3240 } 3241 3242 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ 3243 if (eh->root.root.string[0] == '_' 3244 && (strcmp (eh->root.root.string, "_DYNAMIC") == 0 3245 || eh == htab->etab.hgot)) 3246 { 3247 sym->st_shndx = SHN_ABS; 3248 } 3249 3250 return TRUE; 3251 } 3252 3253 /* Set the Meta ELF ABI version. */ 3254 3255 static void 3256 elf_metag_post_process_headers (bfd * abfd, struct bfd_link_info * link_info) 3257 { 3258 Elf_Internal_Ehdr * i_ehdrp; /* ELF file header, internal form. */ 3259 3260 _bfd_elf_post_process_headers (abfd, link_info); 3261 i_ehdrp = elf_elfheader (abfd); 3262 i_ehdrp->e_ident[EI_ABIVERSION] = METAG_ELF_ABI_VERSION; 3263 } 3264 3265 /* Used to decide how to sort relocs in an optimal manner for the 3266 dynamic linker, before writing them out. */ 3267 3268 static enum elf_reloc_type_class 3269 elf_metag_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 3270 const asection *rel_sec ATTRIBUTE_UNUSED, 3271 const Elf_Internal_Rela *rela) 3272 { 3273 switch ((int) ELF32_R_TYPE (rela->r_info)) 3274 { 3275 case R_METAG_RELATIVE: 3276 return reloc_class_relative; 3277 case R_METAG_JMP_SLOT: 3278 return reloc_class_plt; 3279 case R_METAG_COPY: 3280 return reloc_class_copy; 3281 default: 3282 return reloc_class_normal; 3283 } 3284 } 3285 3286 /* Finish up the dynamic sections. */ 3287 3288 static bfd_boolean 3289 elf_metag_finish_dynamic_sections (bfd *output_bfd, 3290 struct bfd_link_info *info) 3291 { 3292 bfd *dynobj; 3293 struct elf_metag_link_hash_table *htab; 3294 asection *sdyn; 3295 3296 htab = metag_link_hash_table (info); 3297 dynobj = htab->etab.dynobj; 3298 3299 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 3300 3301 if (htab->etab.dynamic_sections_created) 3302 { 3303 asection *splt; 3304 Elf32_External_Dyn *dyncon, *dynconend; 3305 3306 if (sdyn == NULL) 3307 abort (); 3308 3309 dyncon = (Elf32_External_Dyn *) sdyn->contents; 3310 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); 3311 for (; dyncon < dynconend; dyncon++) 3312 { 3313 Elf_Internal_Dyn dyn; 3314 asection *s; 3315 3316 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); 3317 3318 switch (dyn.d_tag) 3319 { 3320 default: 3321 continue; 3322 3323 case DT_PLTGOT: 3324 s = htab->sgot->output_section; 3325 BFD_ASSERT (s != NULL); 3326 dyn.d_un.d_ptr = s->vma + htab->sgot->output_offset; 3327 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 3328 break; 3329 3330 case DT_JMPREL: 3331 s = htab->srelplt->output_section; 3332 BFD_ASSERT (s != NULL); 3333 dyn.d_un.d_ptr = s->vma; 3334 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 3335 break; 3336 3337 case DT_PLTRELSZ: 3338 s = htab->srelplt; 3339 dyn.d_un.d_val = s->size; 3340 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 3341 break; 3342 3343 case DT_RELASZ: 3344 /* Don't count procedure linkage table relocs in the 3345 overall reloc count. */ 3346 if (htab->srelplt) { 3347 s = htab->srelplt; 3348 dyn.d_un.d_val -= s->size; 3349 } 3350 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 3351 break; 3352 3353 case DT_RELA: 3354 /* We may not be using the standard ELF linker script. 3355 If .rela.plt is the first .rela section, we adjust 3356 DT_RELA to not include it. */ 3357 if (htab->srelplt) { 3358 s = htab->srelplt; 3359 if (dyn.d_un.d_ptr == s->output_section->vma + s->output_offset) 3360 dyn.d_un.d_ptr += s->size; 3361 } 3362 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 3363 break; 3364 } 3365 3366 } 3367 3368 /* Fill in the first entry in the procedure linkage table. */ 3369 splt = htab->splt; 3370 if (splt && splt->size > 0) 3371 { 3372 unsigned long addr; 3373 /* addr = .got + 4 */ 3374 addr = htab->sgot->output_section->vma + 3375 htab->sgot->output_offset + 4; 3376 if (info->shared) 3377 { 3378 addr -= splt->output_section->vma + splt->output_offset; 3379 bfd_put_32 (output_bfd, 3380 plt0_pic_entry[0] | (((addr >> 16) & 0xffff) << 3), 3381 splt->contents); 3382 bfd_put_32 (output_bfd, 3383 plt0_pic_entry[1] | ((addr & 0xffff) << 3), 3384 splt->contents + 4); 3385 bfd_put_32 (output_bfd, plt0_pic_entry[2], splt->contents + 8); 3386 bfd_put_32 (output_bfd, plt0_pic_entry[3], splt->contents + 12); 3387 bfd_put_32 (output_bfd, plt0_pic_entry[4], splt->contents + 16); 3388 } 3389 else 3390 { 3391 bfd_put_32 (output_bfd, 3392 plt0_entry[0] | (((addr >> 16) & 0xffff) << 3), 3393 splt->contents); 3394 bfd_put_32 (output_bfd, 3395 plt0_entry[1] | ((addr & 0xffff) << 3), 3396 splt->contents + 4); 3397 bfd_put_32 (output_bfd, plt0_entry[2], splt->contents + 8); 3398 bfd_put_32 (output_bfd, plt0_entry[3], splt->contents + 12); 3399 bfd_put_32 (output_bfd, plt0_entry[4], splt->contents + 16); 3400 } 3401 3402 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 3403 PLT_ENTRY_SIZE; 3404 } 3405 } 3406 3407 if (htab->sgot != NULL && htab->sgot->size != 0) 3408 { 3409 /* Fill in the first entry in the global offset table. 3410 We use it to point to our dynamic section, if we have one. */ 3411 bfd_put_32 (output_bfd, 3412 sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0, 3413 htab->sgot->contents); 3414 3415 /* The second entry is reserved for use by the dynamic linker. */ 3416 memset (htab->sgot->contents + GOT_ENTRY_SIZE, 0, GOT_ENTRY_SIZE); 3417 3418 /* Set .got entry size. */ 3419 elf_section_data (htab->sgot->output_section) 3420 ->this_hdr.sh_entsize = GOT_ENTRY_SIZE; 3421 } 3422 3423 return TRUE; 3424 } 3425 3426 /* Return the section that should be marked against GC for a given 3427 relocation. */ 3428 3429 static asection * 3430 elf_metag_gc_mark_hook (asection *sec, 3431 struct bfd_link_info *info, 3432 Elf_Internal_Rela *rela, 3433 struct elf_link_hash_entry *hh, 3434 Elf_Internal_Sym *sym) 3435 { 3436 if (hh != NULL) 3437 switch ((unsigned int) ELF32_R_TYPE (rela->r_info)) 3438 { 3439 case R_METAG_GNU_VTINHERIT: 3440 case R_METAG_GNU_VTENTRY: 3441 return NULL; 3442 } 3443 3444 return _bfd_elf_gc_mark_hook (sec, info, rela, hh, sym); 3445 } 3446 3447 /* Update the got and plt entry reference counts for the section being 3448 removed. */ 3449 3450 static bfd_boolean 3451 elf_metag_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED, 3452 struct bfd_link_info *info ATTRIBUTE_UNUSED, 3453 asection *sec ATTRIBUTE_UNUSED, 3454 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED) 3455 { 3456 Elf_Internal_Shdr *symtab_hdr; 3457 struct elf_link_hash_entry **eh_syms; 3458 bfd_signed_vma *local_got_refcounts; 3459 bfd_signed_vma *local_plt_refcounts; 3460 const Elf_Internal_Rela *rel, *relend; 3461 3462 if (info->relocatable) 3463 return TRUE; 3464 3465 elf_section_data (sec)->local_dynrel = NULL; 3466 3467 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 3468 eh_syms = elf_sym_hashes (abfd); 3469 local_got_refcounts = elf_local_got_refcounts (abfd); 3470 local_plt_refcounts = local_got_refcounts; 3471 if (local_plt_refcounts != NULL) 3472 local_plt_refcounts += symtab_hdr->sh_info; 3473 3474 relend = relocs + sec->reloc_count; 3475 for (rel = relocs; rel < relend; rel++) 3476 { 3477 unsigned long r_symndx; 3478 unsigned int r_type; 3479 struct elf_link_hash_entry *eh = NULL; 3480 3481 r_symndx = ELF32_R_SYM (rel->r_info); 3482 if (r_symndx >= symtab_hdr->sh_info) 3483 { 3484 struct elf_metag_link_hash_entry *hh; 3485 struct elf_metag_dyn_reloc_entry **hdh_pp; 3486 struct elf_metag_dyn_reloc_entry *hdh_p; 3487 3488 eh = eh_syms[r_symndx - symtab_hdr->sh_info]; 3489 while (eh->root.type == bfd_link_hash_indirect 3490 || eh->root.type == bfd_link_hash_warning) 3491 eh = (struct elf_link_hash_entry *) eh->root.u.i.link; 3492 hh = (struct elf_metag_link_hash_entry *) eh; 3493 3494 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; 3495 hdh_pp = &hdh_p->hdh_next) 3496 if (hdh_p->sec == sec) 3497 { 3498 /* Everything must go for SEC. */ 3499 *hdh_pp = hdh_p->hdh_next; 3500 break; 3501 } 3502 } 3503 3504 r_type = ELF32_R_TYPE (rel->r_info); 3505 switch (r_type) 3506 { 3507 case R_METAG_TLS_LDM: 3508 if (metag_link_hash_table (info)->tls_ldm_got.refcount > 0) 3509 metag_link_hash_table (info)->tls_ldm_got.refcount -= 1; 3510 break; 3511 case R_METAG_TLS_IE: 3512 case R_METAG_TLS_GD: 3513 case R_METAG_GETSET_GOT: 3514 if (eh != NULL) 3515 { 3516 if (eh->got.refcount > 0) 3517 eh->got.refcount -= 1; 3518 } 3519 else if (local_got_refcounts != NULL) 3520 { 3521 if (local_got_refcounts[r_symndx] > 0) 3522 local_got_refcounts[r_symndx] -= 1; 3523 } 3524 break; 3525 3526 case R_METAG_RELBRANCH_PLT: 3527 if (eh != NULL) 3528 { 3529 if (eh->plt.refcount > 0) 3530 eh->plt.refcount -= 1; 3531 } 3532 break; 3533 3534 case R_METAG_ADDR32: 3535 case R_METAG_HIADDR16: 3536 case R_METAG_LOADDR16: 3537 case R_METAG_GETSETOFF: 3538 case R_METAG_RELBRANCH: 3539 if (eh != NULL) 3540 { 3541 struct elf_metag_link_hash_entry *hh; 3542 struct elf_metag_dyn_reloc_entry **hdh_pp; 3543 struct elf_metag_dyn_reloc_entry *hdh_p; 3544 3545 if (!info->shared && eh->plt.refcount > 0) 3546 eh->plt.refcount -= 1; 3547 3548 hh = (struct elf_metag_link_hash_entry *) eh; 3549 3550 for (hdh_pp = &hh->dyn_relocs; (hdh_p = *hdh_pp) != NULL; 3551 hdh_pp = &hdh_p->hdh_next) 3552 if (hdh_p->sec == sec) 3553 { 3554 if (ELF32_R_TYPE (rel->r_info) == R_METAG_RELBRANCH) 3555 hdh_p->relative_count -= 1; 3556 hdh_p->count -= 1; 3557 if (hdh_p->count == 0) 3558 *hdh_pp = hdh_p->hdh_next; 3559 break; 3560 } 3561 } 3562 break; 3563 3564 default: 3565 break; 3566 } 3567 } 3568 3569 return TRUE; 3570 } 3571 3572 /* Determine the type of stub needed, if any, for a call. */ 3573 3574 static enum elf_metag_stub_type 3575 metag_type_of_stub (asection *input_sec, 3576 const Elf_Internal_Rela *rel, 3577 struct elf_metag_link_hash_entry *hh, 3578 bfd_vma destination, 3579 struct bfd_link_info *info ATTRIBUTE_UNUSED) 3580 { 3581 bfd_vma location; 3582 bfd_vma branch_offset; 3583 bfd_vma max_branch_offset; 3584 3585 if (hh != NULL && 3586 !(hh->eh.root.type == bfd_link_hash_defined 3587 || hh->eh.root.type == bfd_link_hash_defweak)) 3588 return metag_stub_none; 3589 3590 /* Determine where the call point is. */ 3591 location = (input_sec->output_offset 3592 + input_sec->output_section->vma 3593 + rel->r_offset); 3594 3595 branch_offset = destination - location; 3596 3597 /* Determine if a long branch stub is needed. Meta branch offsets 3598 are signed 19 bits 4 byte aligned. */ 3599 max_branch_offset = (1 << (BRANCH_BITS-1)) << 2; 3600 3601 if (branch_offset + max_branch_offset >= 2*max_branch_offset) 3602 { 3603 if (info->shared) 3604 return metag_stub_long_branch_shared; 3605 else 3606 return metag_stub_long_branch; 3607 } 3608 3609 return metag_stub_none; 3610 } 3611 3612 #define MOVT_A0_3 0x82180005 3613 #define JUMP_A0_3 0xac180003 3614 3615 #define MOVT_A1LBP 0x83080005 3616 #define ADD_A1LBP 0x83080000 3617 3618 #define ADDT_A0_3_CPC 0x82980001 3619 #define ADD_A0_3_A0_3 0x82180000 3620 #define MOV_PC_A0_3 0xa3180ca0 3621 3622 static bfd_boolean 3623 metag_build_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED) 3624 { 3625 struct elf_metag_stub_hash_entry *hsh; 3626 asection *stub_sec; 3627 bfd *stub_bfd; 3628 bfd_byte *loc; 3629 bfd_vma sym_value; 3630 int size; 3631 3632 /* Massage our args to the form they really have. */ 3633 hsh = (struct elf_metag_stub_hash_entry *) gen_entry; 3634 3635 stub_sec = hsh->stub_sec; 3636 3637 /* Make a note of the offset within the stubs for this entry. */ 3638 hsh->stub_offset = stub_sec->size; 3639 loc = stub_sec->contents + hsh->stub_offset; 3640 3641 stub_bfd = stub_sec->owner; 3642 3643 switch (hsh->stub_type) 3644 { 3645 case metag_stub_long_branch_shared: 3646 /* A PIC long branch stub is an ADDT and an ADD instruction used to 3647 calculate the jump target using A0.3 as a temporary. Then a MOV 3648 to PC carries out the jump. */ 3649 sym_value = (hsh->target_value 3650 + hsh->target_section->output_offset 3651 + hsh->target_section->output_section->vma 3652 + hsh->addend); 3653 3654 sym_value -= (hsh->stub_offset 3655 + stub_sec->output_offset 3656 + stub_sec->output_section->vma); 3657 3658 bfd_put_32 (stub_bfd, ADDT_A0_3_CPC | (((sym_value >> 16) & 0xffff) << 3), 3659 loc); 3660 3661 bfd_put_32 (stub_bfd, ADD_A0_3_A0_3 | ((sym_value & 0xffff) << 3), 3662 loc + 4); 3663 3664 bfd_put_32 (stub_bfd, MOV_PC_A0_3, loc + 8); 3665 3666 size = 12; 3667 break; 3668 case metag_stub_long_branch: 3669 /* A standard long branch stub is a MOVT instruction followed by a 3670 JUMP instruction using the A0.3 register as a temporary. This is 3671 the same method used by the LDLK linker (patch.c). */ 3672 sym_value = (hsh->target_value 3673 + hsh->target_section->output_offset 3674 + hsh->target_section->output_section->vma 3675 + hsh->addend); 3676 3677 bfd_put_32 (stub_bfd, MOVT_A0_3 | (((sym_value >> 16) & 0xffff) << 3), 3678 loc); 3679 3680 bfd_put_32 (stub_bfd, JUMP_A0_3 | ((sym_value & 0xffff) << 3), loc + 4); 3681 3682 size = 8; 3683 break; 3684 default: 3685 BFD_FAIL (); 3686 return FALSE; 3687 } 3688 3689 stub_sec->size += size; 3690 return TRUE; 3691 } 3692 3693 /* As above, but don't actually build the stub. Just bump offset so 3694 we know stub section sizes. */ 3695 3696 static bfd_boolean 3697 metag_size_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg ATTRIBUTE_UNUSED) 3698 { 3699 struct elf_metag_stub_hash_entry *hsh; 3700 int size = 0; 3701 3702 /* Massage our args to the form they really have. */ 3703 hsh = (struct elf_metag_stub_hash_entry *) gen_entry; 3704 3705 if (hsh->stub_type == metag_stub_long_branch) 3706 size = 8; 3707 else if (hsh->stub_type == metag_stub_long_branch_shared) 3708 size = 12; 3709 3710 hsh->stub_sec->size += size; 3711 return TRUE; 3712 } 3713 3714 /* Set up various things so that we can make a list of input sections 3715 for each output section included in the link. Returns -1 on error, 3716 0 when no stubs will be needed, and 1 on success. */ 3717 3718 int 3719 elf_metag_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info) 3720 { 3721 bfd *input_bfd; 3722 unsigned int bfd_count; 3723 int top_id, top_index; 3724 asection *section; 3725 asection **input_list, **list; 3726 bfd_size_type amt; 3727 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info); 3728 3729 /* Count the number of input BFDs and find the top input section id. */ 3730 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0; 3731 input_bfd != NULL; 3732 input_bfd = input_bfd->link.next) 3733 { 3734 bfd_count += 1; 3735 for (section = input_bfd->sections; 3736 section != NULL; 3737 section = section->next) 3738 { 3739 if (top_id < section->id) 3740 top_id = section->id; 3741 } 3742 } 3743 3744 htab->bfd_count = bfd_count; 3745 3746 amt = sizeof (struct map_stub) * (top_id + 1); 3747 htab->stub_group = bfd_zmalloc (amt); 3748 if (htab->stub_group == NULL) 3749 return -1; 3750 3751 /* We can't use output_bfd->section_count here to find the top output 3752 section index as some sections may have been removed, and 3753 strip_excluded_output_sections doesn't renumber the indices. */ 3754 for (section = output_bfd->sections, top_index = 0; 3755 section != NULL; 3756 section = section->next) 3757 { 3758 if (top_index < section->index) 3759 top_index = section->index; 3760 } 3761 3762 htab->top_index = top_index; 3763 amt = sizeof (asection *) * (top_index + 1); 3764 input_list = bfd_malloc (amt); 3765 htab->input_list = input_list; 3766 if (input_list == NULL) 3767 return -1; 3768 3769 /* For sections we aren't interested in, mark their entries with a 3770 value we can check later. */ 3771 list = input_list + top_index; 3772 do 3773 *list = bfd_abs_section_ptr; 3774 while (list-- != input_list); 3775 3776 for (section = output_bfd->sections; 3777 section != NULL; 3778 section = section->next) 3779 { 3780 /* FIXME: This is a bit of hack. Currently our .ctors and .dtors 3781 * have PC relative relocs in them but no code flag set. */ 3782 if (((section->flags & SEC_CODE) != 0) || 3783 strcmp(".ctors", section->name) || 3784 strcmp(".dtors", section->name)) 3785 input_list[section->index] = NULL; 3786 } 3787 3788 return 1; 3789 } 3790 3791 /* The linker repeatedly calls this function for each input section, 3792 in the order that input sections are linked into output sections. 3793 Build lists of input sections to determine groupings between which 3794 we may insert linker stubs. */ 3795 3796 void 3797 elf_metag_next_input_section (struct bfd_link_info *info, asection *isec) 3798 { 3799 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info); 3800 3801 if (isec->output_section->index <= htab->top_index) 3802 { 3803 asection **list = htab->input_list + isec->output_section->index; 3804 if (*list != bfd_abs_section_ptr) 3805 { 3806 /* Steal the link_sec pointer for our list. */ 3807 #define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec) 3808 /* This happens to make the list in reverse order, 3809 which is what we want. */ 3810 PREV_SEC (isec) = *list; 3811 *list = isec; 3812 } 3813 } 3814 } 3815 3816 /* See whether we can group stub sections together. Grouping stub 3817 sections may result in fewer stubs. More importantly, we need to 3818 put all .init* and .fini* stubs at the beginning of the .init or 3819 .fini output sections respectively, because glibc splits the 3820 _init and _fini functions into multiple parts. Putting a stub in 3821 the middle of a function is not a good idea. */ 3822 3823 static void 3824 group_sections (struct elf_metag_link_hash_table *htab, 3825 bfd_size_type stub_group_size, 3826 bfd_boolean stubs_always_before_branch) 3827 { 3828 asection **list = htab->input_list + htab->top_index; 3829 do 3830 { 3831 asection *tail = *list; 3832 if (tail == bfd_abs_section_ptr) 3833 continue; 3834 while (tail != NULL) 3835 { 3836 asection *curr; 3837 asection *prev; 3838 bfd_size_type total; 3839 bfd_boolean big_sec; 3840 3841 curr = tail; 3842 total = tail->size; 3843 big_sec = total >= stub_group_size; 3844 3845 while ((prev = PREV_SEC (curr)) != NULL 3846 && ((total += curr->output_offset - prev->output_offset) 3847 < stub_group_size)) 3848 curr = prev; 3849 3850 /* OK, the size from the start of CURR to the end is less 3851 than stub_group_size bytes and thus can be handled by one stub 3852 section. (or the tail section is itself larger than 3853 stub_group_size bytes, in which case we may be toast.) 3854 We should really be keeping track of the total size of 3855 stubs added here, as stubs contribute to the final output 3856 section size. */ 3857 do 3858 { 3859 prev = PREV_SEC (tail); 3860 /* Set up this stub group. */ 3861 htab->stub_group[tail->id].link_sec = curr; 3862 } 3863 while (tail != curr && (tail = prev) != NULL); 3864 3865 /* But wait, there's more! Input sections up to stub_group_size 3866 bytes before the stub section can be handled by it too. 3867 Don't do this if we have a really large section after the 3868 stubs, as adding more stubs increases the chance that 3869 branches may not reach into the stub section. */ 3870 if (!stubs_always_before_branch && !big_sec) 3871 { 3872 total = 0; 3873 while (prev != NULL 3874 && ((total += tail->output_offset - prev->output_offset) 3875 < stub_group_size)) 3876 { 3877 tail = prev; 3878 prev = PREV_SEC (tail); 3879 htab->stub_group[tail->id].link_sec = curr; 3880 } 3881 } 3882 tail = prev; 3883 } 3884 } 3885 while (list-- != htab->input_list); 3886 free (htab->input_list); 3887 #undef PREV_SEC 3888 } 3889 3890 /* Read in all local syms for all input bfds. 3891 Returns -1 on error, 0 otherwise. */ 3892 3893 static int 3894 get_local_syms (bfd *output_bfd ATTRIBUTE_UNUSED, bfd *input_bfd, 3895 struct bfd_link_info *info) 3896 { 3897 unsigned int bfd_indx; 3898 Elf_Internal_Sym *local_syms, **all_local_syms; 3899 int stub_changed = 0; 3900 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info); 3901 3902 /* We want to read in symbol extension records only once. To do this 3903 we need to read in the local symbols in parallel and save them for 3904 later use; so hold pointers to the local symbols in an array. */ 3905 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count; 3906 all_local_syms = bfd_zmalloc (amt); 3907 htab->all_local_syms = all_local_syms; 3908 if (all_local_syms == NULL) 3909 return -1; 3910 3911 /* Walk over all the input BFDs, swapping in local symbols. */ 3912 for (bfd_indx = 0; 3913 input_bfd != NULL; 3914 input_bfd = input_bfd->link.next, bfd_indx++) 3915 { 3916 Elf_Internal_Shdr *symtab_hdr; 3917 3918 /* We'll need the symbol table in a second. */ 3919 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 3920 if (symtab_hdr->sh_info == 0) 3921 continue; 3922 3923 /* We need an array of the local symbols attached to the input bfd. */ 3924 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents; 3925 if (local_syms == NULL) 3926 { 3927 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 3928 symtab_hdr->sh_info, 0, 3929 NULL, NULL, NULL); 3930 /* Cache them for elf_link_input_bfd. */ 3931 symtab_hdr->contents = (unsigned char *) local_syms; 3932 } 3933 if (local_syms == NULL) 3934 return -1; 3935 3936 all_local_syms[bfd_indx] = local_syms; 3937 } 3938 3939 return stub_changed; 3940 } 3941 3942 /* Determine and set the size of the stub section for a final link. 3943 3944 The basic idea here is to examine all the relocations looking for 3945 PC-relative calls to a target that is unreachable with a "CALLR" 3946 instruction. */ 3947 3948 /* See elf32-hppa.c and elf64-ppc.c. */ 3949 3950 bfd_boolean 3951 elf_metag_size_stubs(bfd *output_bfd, bfd *stub_bfd, 3952 struct bfd_link_info *info, 3953 bfd_signed_vma group_size, 3954 asection * (*add_stub_section) (const char *, asection *), 3955 void (*layout_sections_again) (void)) 3956 { 3957 bfd_size_type stub_group_size; 3958 bfd_boolean stubs_always_before_branch; 3959 bfd_boolean stub_changed; 3960 struct elf_metag_link_hash_table *htab = metag_link_hash_table (info); 3961 3962 /* Stash our params away. */ 3963 htab->stub_bfd = stub_bfd; 3964 htab->add_stub_section = add_stub_section; 3965 htab->layout_sections_again = layout_sections_again; 3966 stubs_always_before_branch = group_size < 0; 3967 if (group_size < 0) 3968 stub_group_size = -group_size; 3969 else 3970 stub_group_size = group_size; 3971 if (stub_group_size == 1) 3972 { 3973 /* Default values. */ 3974 /* FIXME: not sure what these values should be */ 3975 if (stubs_always_before_branch) 3976 { 3977 stub_group_size = (1 << BRANCH_BITS); 3978 } 3979 else 3980 { 3981 stub_group_size = (1 << BRANCH_BITS); 3982 } 3983 } 3984 3985 group_sections (htab, stub_group_size, stubs_always_before_branch); 3986 3987 switch (get_local_syms (output_bfd, info->input_bfds, info)) 3988 { 3989 default: 3990 if (htab->all_local_syms) 3991 goto error_ret_free_local; 3992 return FALSE; 3993 3994 case 0: 3995 stub_changed = FALSE; 3996 break; 3997 3998 case 1: 3999 stub_changed = TRUE; 4000 break; 4001 } 4002 4003 while (1) 4004 { 4005 bfd *input_bfd; 4006 unsigned int bfd_indx; 4007 asection *stub_sec; 4008 4009 for (input_bfd = info->input_bfds, bfd_indx = 0; 4010 input_bfd != NULL; 4011 input_bfd = input_bfd->link.next, bfd_indx++) 4012 { 4013 Elf_Internal_Shdr *symtab_hdr; 4014 asection *section; 4015 Elf_Internal_Sym *local_syms; 4016 4017 /* We'll need the symbol table in a second. */ 4018 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 4019 if (symtab_hdr->sh_info == 0) 4020 continue; 4021 4022 local_syms = htab->all_local_syms[bfd_indx]; 4023 4024 /* Walk over each section attached to the input bfd. */ 4025 for (section = input_bfd->sections; 4026 section != NULL; 4027 section = section->next) 4028 { 4029 Elf_Internal_Rela *internal_relocs, *irelaend, *irela; 4030 4031 /* If there aren't any relocs, then there's nothing more 4032 to do. */ 4033 if ((section->flags & SEC_RELOC) == 0 4034 || section->reloc_count == 0) 4035 continue; 4036 4037 /* If this section is a link-once section that will be 4038 discarded, then don't create any stubs. */ 4039 if (section->output_section == NULL 4040 || section->output_section->owner != output_bfd) 4041 continue; 4042 4043 /* Get the relocs. */ 4044 internal_relocs 4045 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL, 4046 info->keep_memory); 4047 if (internal_relocs == NULL) 4048 goto error_ret_free_local; 4049 4050 /* Now examine each relocation. */ 4051 irela = internal_relocs; 4052 irelaend = irela + section->reloc_count; 4053 for (; irela < irelaend; irela++) 4054 { 4055 unsigned int r_type, r_indx; 4056 enum elf_metag_stub_type stub_type; 4057 struct elf_metag_stub_hash_entry *hsh; 4058 asection *sym_sec; 4059 bfd_vma sym_value; 4060 bfd_vma destination; 4061 struct elf_metag_link_hash_entry *hh; 4062 char *stub_name; 4063 const asection *id_sec; 4064 4065 r_type = ELF32_R_TYPE (irela->r_info); 4066 r_indx = ELF32_R_SYM (irela->r_info); 4067 4068 if (r_type >= (unsigned int) R_METAG_MAX) 4069 { 4070 bfd_set_error (bfd_error_bad_value); 4071 error_ret_free_internal: 4072 if (elf_section_data (section)->relocs == NULL) 4073 free (internal_relocs); 4074 goto error_ret_free_local; 4075 } 4076 4077 /* Only look for stubs on CALLR and B instructions. */ 4078 if (!(r_type == (unsigned int) R_METAG_RELBRANCH || 4079 r_type == (unsigned int) R_METAG_RELBRANCH_PLT)) 4080 continue; 4081 4082 /* Now determine the call target, its name, value, 4083 section. */ 4084 sym_sec = NULL; 4085 sym_value = 0; 4086 destination = 0; 4087 hh = NULL; 4088 if (r_indx < symtab_hdr->sh_info) 4089 { 4090 /* It's a local symbol. */ 4091 Elf_Internal_Sym *sym; 4092 Elf_Internal_Shdr *hdr; 4093 unsigned int shndx; 4094 4095 sym = local_syms + r_indx; 4096 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) 4097 sym_value = sym->st_value; 4098 shndx = sym->st_shndx; 4099 if (shndx < elf_numsections (input_bfd)) 4100 { 4101 hdr = elf_elfsections (input_bfd)[shndx]; 4102 sym_sec = hdr->bfd_section; 4103 destination = (sym_value + irela->r_addend 4104 + sym_sec->output_offset 4105 + sym_sec->output_section->vma); 4106 } 4107 } 4108 else 4109 { 4110 /* It's an external symbol. */ 4111 int e_indx; 4112 4113 e_indx = r_indx - symtab_hdr->sh_info; 4114 hh = ((struct elf_metag_link_hash_entry *) 4115 elf_sym_hashes (input_bfd)[e_indx]); 4116 4117 while (hh->eh.root.type == bfd_link_hash_indirect 4118 || hh->eh.root.type == bfd_link_hash_warning) 4119 hh = ((struct elf_metag_link_hash_entry *) 4120 hh->eh.root.u.i.link); 4121 4122 if (hh->eh.root.type == bfd_link_hash_defined 4123 || hh->eh.root.type == bfd_link_hash_defweak) 4124 { 4125 sym_sec = hh->eh.root.u.def.section; 4126 sym_value = hh->eh.root.u.def.value; 4127 if (hh->eh.plt.offset != (bfd_vma) -1 4128 && hh->eh.dynindx != -1 4129 && r_type == (unsigned int) R_METAG_RELBRANCH_PLT) 4130 { 4131 sym_sec = htab->splt; 4132 sym_value = hh->eh.plt.offset; 4133 } 4134 4135 if (sym_sec->output_section != NULL) 4136 destination = (sym_value + irela->r_addend 4137 + sym_sec->output_offset 4138 + sym_sec->output_section->vma); 4139 else 4140 continue; 4141 } 4142 else if (hh->eh.root.type == bfd_link_hash_undefweak) 4143 { 4144 if (! info->shared) 4145 continue; 4146 } 4147 else if (hh->eh.root.type == bfd_link_hash_undefined) 4148 { 4149 if (! (info->unresolved_syms_in_objects == RM_IGNORE 4150 && (ELF_ST_VISIBILITY (hh->eh.other) 4151 == STV_DEFAULT))) 4152 continue; 4153 } 4154 else 4155 { 4156 bfd_set_error (bfd_error_bad_value); 4157 goto error_ret_free_internal; 4158 } 4159 } 4160 4161 /* Determine what (if any) linker stub is needed. */ 4162 stub_type = metag_type_of_stub (section, irela, hh, 4163 destination, info); 4164 if (stub_type == metag_stub_none) 4165 continue; 4166 4167 /* Support for grouping stub sections. */ 4168 id_sec = htab->stub_group[section->id].link_sec; 4169 4170 /* Get the name of this stub. */ 4171 stub_name = metag_stub_name (id_sec, sym_sec, hh, irela); 4172 if (!stub_name) 4173 goto error_ret_free_internal; 4174 4175 hsh = metag_stub_hash_lookup (&htab->bstab, 4176 stub_name, 4177 FALSE, FALSE); 4178 if (hsh != NULL) 4179 { 4180 /* The proper stub has already been created. */ 4181 free (stub_name); 4182 continue; 4183 } 4184 4185 hsh = metag_add_stub (stub_name, section, htab); 4186 if (hsh == NULL) 4187 { 4188 free (stub_name); 4189 goto error_ret_free_internal; 4190 } 4191 hsh->target_value = sym_value; 4192 hsh->target_section = sym_sec; 4193 hsh->stub_type = stub_type; 4194 hsh->hh = hh; 4195 hsh->addend = irela->r_addend; 4196 stub_changed = TRUE; 4197 } 4198 4199 /* We're done with the internal relocs, free them. */ 4200 if (elf_section_data (section)->relocs == NULL) 4201 free (internal_relocs); 4202 } 4203 } 4204 4205 if (!stub_changed) 4206 break; 4207 4208 /* OK, we've added some stubs. Find out the new size of the 4209 stub sections. */ 4210 for (stub_sec = htab->stub_bfd->sections; 4211 stub_sec != NULL; 4212 stub_sec = stub_sec->next) 4213 stub_sec->size = 0; 4214 4215 bfd_hash_traverse (&htab->bstab, metag_size_one_stub, htab); 4216 4217 /* Ask the linker to do its stuff. */ 4218 (*htab->layout_sections_again) (); 4219 stub_changed = FALSE; 4220 } 4221 4222 free (htab->all_local_syms); 4223 return TRUE; 4224 4225 error_ret_free_local: 4226 free (htab->all_local_syms); 4227 return FALSE; 4228 } 4229 4230 /* Build all the stubs associated with the current output file. The 4231 stubs are kept in a hash table attached to the main linker hash 4232 table. This function is called via metagelf_finish in the linker. */ 4233 4234 bfd_boolean 4235 elf_metag_build_stubs (struct bfd_link_info *info) 4236 { 4237 asection *stub_sec; 4238 struct bfd_hash_table *table; 4239 struct elf_metag_link_hash_table *htab; 4240 4241 htab = metag_link_hash_table (info); 4242 4243 for (stub_sec = htab->stub_bfd->sections; 4244 stub_sec != NULL; 4245 stub_sec = stub_sec->next) 4246 { 4247 bfd_size_type size; 4248 4249 /* Allocate memory to hold the linker stubs. */ 4250 size = stub_sec->size; 4251 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size); 4252 if (stub_sec->contents == NULL && size != 0) 4253 return FALSE; 4254 stub_sec->size = 0; 4255 } 4256 4257 /* Build the stubs as directed by the stub hash table. */ 4258 table = &htab->bstab; 4259 bfd_hash_traverse (table, metag_build_one_stub, info); 4260 4261 return TRUE; 4262 } 4263 4264 /* Return TRUE if SYM represents a local label symbol. */ 4265 4266 static bfd_boolean 4267 elf_metag_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED, const char *name) 4268 { 4269 if (name[0] == '$' && name[1] == 'L') 4270 return 1; 4271 return _bfd_elf_is_local_label_name (abfd, name); 4272 } 4273 4274 /* Return address for Ith PLT stub in section PLT, for relocation REL 4275 or (bfd_vma) -1 if it should not be included. */ 4276 4277 static bfd_vma 4278 elf_metag_plt_sym_val (bfd_vma i, const asection *plt, 4279 const arelent *rel ATTRIBUTE_UNUSED) 4280 { 4281 return plt->vma + (i + 1) * PLT_ENTRY_SIZE; 4282 } 4283 4284 #define ELF_ARCH bfd_arch_metag 4285 #define ELF_TARGET_ID METAG_ELF_DATA 4286 #define ELF_MACHINE_CODE EM_METAG 4287 #define ELF_MINPAGESIZE 0x1000 4288 #define ELF_MAXPAGESIZE 0x4000 4289 #define ELF_COMMONPAGESIZE 0x1000 4290 4291 #define TARGET_LITTLE_SYM metag_elf32_vec 4292 #define TARGET_LITTLE_NAME "elf32-metag" 4293 4294 #define elf_symbol_leading_char '_' 4295 4296 #define elf_info_to_howto_rel NULL 4297 #define elf_info_to_howto metag_info_to_howto_rela 4298 4299 #define bfd_elf32_bfd_is_local_label_name elf_metag_is_local_label_name 4300 #define bfd_elf32_bfd_link_hash_table_create \ 4301 elf_metag_link_hash_table_create 4302 #define elf_backend_relocate_section elf_metag_relocate_section 4303 #define elf_backend_gc_mark_hook elf_metag_gc_mark_hook 4304 #define elf_backend_gc_sweep_hook elf_metag_gc_sweep_hook 4305 #define elf_backend_check_relocs elf_metag_check_relocs 4306 #define elf_backend_create_dynamic_sections elf_metag_create_dynamic_sections 4307 #define elf_backend_adjust_dynamic_symbol elf_metag_adjust_dynamic_symbol 4308 #define elf_backend_finish_dynamic_symbol elf_metag_finish_dynamic_symbol 4309 #define elf_backend_finish_dynamic_sections elf_metag_finish_dynamic_sections 4310 #define elf_backend_size_dynamic_sections elf_metag_size_dynamic_sections 4311 #define elf_backend_omit_section_dynsym \ 4312 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true) 4313 #define elf_backend_post_process_headers elf_metag_post_process_headers 4314 #define elf_backend_reloc_type_class elf_metag_reloc_type_class 4315 #define elf_backend_copy_indirect_symbol elf_metag_copy_indirect_symbol 4316 #define elf_backend_plt_sym_val elf_metag_plt_sym_val 4317 4318 #define elf_backend_can_gc_sections 1 4319 #define elf_backend_can_refcount 1 4320 #define elf_backend_got_header_size 12 4321 #define elf_backend_rela_normal 1 4322 #define elf_backend_want_got_sym 0 4323 #define elf_backend_want_plt_sym 0 4324 #define elf_backend_plt_readonly 1 4325 4326 #define bfd_elf32_bfd_reloc_type_lookup metag_reloc_type_lookup 4327 #define bfd_elf32_bfd_reloc_name_lookup metag_reloc_name_lookup 4328 4329 #include "elf32-target.h" 4330