1/* Matsushita 10300 specific support for 32-bit ELF 2 Copyright (C) 1996-2017 Free Software Foundation, Inc. 3 4 This file is part of BFD, the Binary File Descriptor library. 5 6 This program is free software; you can redistribute it and/or modify 7 it under the terms of the GNU General Public License as published by 8 the Free Software Foundation; either version 3 of the License, or 9 (at your option) any later version. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License 17 along with this program; if not, write to the Free Software 18 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, 19 MA 02110-1301, USA. */ 20 21#include "sysdep.h" 22#include "bfd.h" 23#include "libbfd.h" 24#include "elf-bfd.h" 25#include "elf/mn10300.h" 26#include "libiberty.h" 27 28/* The mn10300 linker needs to keep track of the number of relocs that 29 it decides to copy in check_relocs for each symbol. This is so 30 that it can discard PC relative relocs if it doesn't need them when 31 linking with -Bsymbolic. We store the information in a field 32 extending the regular ELF linker hash table. */ 33 34struct elf32_mn10300_link_hash_entry 35{ 36 /* The basic elf link hash table entry. */ 37 struct elf_link_hash_entry root; 38 39 /* For function symbols, the number of times this function is 40 called directly (ie by name). */ 41 unsigned int direct_calls; 42 43 /* For function symbols, the size of this function's stack 44 (if <= 255 bytes). We stuff this into "call" instructions 45 to this target when it's valid and profitable to do so. 46 47 This does not include stack allocated by movm! */ 48 unsigned char stack_size; 49 50 /* For function symbols, arguments (if any) for movm instruction 51 in the prologue. We stuff this value into "call" instructions 52 to the target when it's valid and profitable to do so. */ 53 unsigned char movm_args; 54 55 /* For function symbols, the amount of stack space that would be allocated 56 by the movm instruction. This is redundant with movm_args, but we 57 add it to the hash table to avoid computing it over and over. */ 58 unsigned char movm_stack_size; 59 60/* When set, convert all "call" instructions to this target into "calls" 61 instructions. */ 62#define MN10300_CONVERT_CALL_TO_CALLS 0x1 63 64/* Used to mark functions which have had redundant parts of their 65 prologue deleted. */ 66#define MN10300_DELETED_PROLOGUE_BYTES 0x2 67 unsigned char flags; 68 69 /* Calculated value. */ 70 bfd_vma value; 71 72#define GOT_UNKNOWN 0 73#define GOT_NORMAL 1 74#define GOT_TLS_GD 2 75#define GOT_TLS_LD 3 76#define GOT_TLS_IE 4 77 /* Used to distinguish GOT entries for TLS types from normal GOT entries. */ 78 unsigned char tls_type; 79}; 80 81/* We derive a hash table from the main elf linker hash table so 82 we can store state variables and a secondary hash table without 83 resorting to global variables. */ 84struct elf32_mn10300_link_hash_table 85{ 86 /* The main hash table. */ 87 struct elf_link_hash_table root; 88 89 /* A hash table for static functions. We could derive a new hash table 90 instead of using the full elf32_mn10300_link_hash_table if we wanted 91 to save some memory. */ 92 struct elf32_mn10300_link_hash_table *static_hash_table; 93 94 /* Random linker state flags. */ 95#define MN10300_HASH_ENTRIES_INITIALIZED 0x1 96 char flags; 97 struct 98 { 99 bfd_signed_vma refcount; 100 bfd_vma offset; 101 char got_allocated; 102 char rel_emitted; 103 } tls_ldm_got; 104}; 105 106#define elf_mn10300_hash_entry(ent) ((struct elf32_mn10300_link_hash_entry *)(ent)) 107 108struct elf_mn10300_obj_tdata 109{ 110 struct elf_obj_tdata root; 111 112 /* tls_type for each local got entry. */ 113 char * local_got_tls_type; 114}; 115 116#define elf_mn10300_tdata(abfd) \ 117 ((struct elf_mn10300_obj_tdata *) (abfd)->tdata.any) 118 119#define elf_mn10300_local_got_tls_type(abfd) \ 120 (elf_mn10300_tdata (abfd)->local_got_tls_type) 121 122#ifndef streq 123#define streq(a, b) (strcmp ((a),(b)) == 0) 124#endif 125 126/* For MN10300 linker hash table. */ 127 128/* Get the MN10300 ELF linker hash table from a link_info structure. */ 129 130#define elf32_mn10300_hash_table(p) \ 131 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \ 132 == MN10300_ELF_DATA ? ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) : NULL) 133 134#define elf32_mn10300_link_hash_traverse(table, func, info) \ 135 (elf_link_hash_traverse \ 136 (&(table)->root, \ 137 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \ 138 (info))) 139 140static reloc_howto_type elf_mn10300_howto_table[] = 141{ 142 /* Dummy relocation. Does nothing. */ 143 HOWTO (R_MN10300_NONE, 144 0, 145 3, 146 0, 147 FALSE, 148 0, 149 complain_overflow_dont, 150 bfd_elf_generic_reloc, 151 "R_MN10300_NONE", 152 FALSE, 153 0, 154 0, 155 FALSE), 156 /* Standard 32 bit reloc. */ 157 HOWTO (R_MN10300_32, 158 0, 159 2, 160 32, 161 FALSE, 162 0, 163 complain_overflow_bitfield, 164 bfd_elf_generic_reloc, 165 "R_MN10300_32", 166 FALSE, 167 0xffffffff, 168 0xffffffff, 169 FALSE), 170 /* Standard 16 bit reloc. */ 171 HOWTO (R_MN10300_16, 172 0, 173 1, 174 16, 175 FALSE, 176 0, 177 complain_overflow_bitfield, 178 bfd_elf_generic_reloc, 179 "R_MN10300_16", 180 FALSE, 181 0xffff, 182 0xffff, 183 FALSE), 184 /* Standard 8 bit reloc. */ 185 HOWTO (R_MN10300_8, 186 0, 187 0, 188 8, 189 FALSE, 190 0, 191 complain_overflow_bitfield, 192 bfd_elf_generic_reloc, 193 "R_MN10300_8", 194 FALSE, 195 0xff, 196 0xff, 197 FALSE), 198 /* Standard 32bit pc-relative reloc. */ 199 HOWTO (R_MN10300_PCREL32, 200 0, 201 2, 202 32, 203 TRUE, 204 0, 205 complain_overflow_bitfield, 206 bfd_elf_generic_reloc, 207 "R_MN10300_PCREL32", 208 FALSE, 209 0xffffffff, 210 0xffffffff, 211 TRUE), 212 /* Standard 16bit pc-relative reloc. */ 213 HOWTO (R_MN10300_PCREL16, 214 0, 215 1, 216 16, 217 TRUE, 218 0, 219 complain_overflow_bitfield, 220 bfd_elf_generic_reloc, 221 "R_MN10300_PCREL16", 222 FALSE, 223 0xffff, 224 0xffff, 225 TRUE), 226 /* Standard 8 pc-relative reloc. */ 227 HOWTO (R_MN10300_PCREL8, 228 0, 229 0, 230 8, 231 TRUE, 232 0, 233 complain_overflow_bitfield, 234 bfd_elf_generic_reloc, 235 "R_MN10300_PCREL8", 236 FALSE, 237 0xff, 238 0xff, 239 TRUE), 240 241 /* GNU extension to record C++ vtable hierarchy. */ 242 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */ 243 0, /* rightshift */ 244 0, /* size (0 = byte, 1 = short, 2 = long) */ 245 0, /* bitsize */ 246 FALSE, /* pc_relative */ 247 0, /* bitpos */ 248 complain_overflow_dont, /* complain_on_overflow */ 249 NULL, /* special_function */ 250 "R_MN10300_GNU_VTINHERIT", /* name */ 251 FALSE, /* partial_inplace */ 252 0, /* src_mask */ 253 0, /* dst_mask */ 254 FALSE), /* pcrel_offset */ 255 256 /* GNU extension to record C++ vtable member usage */ 257 HOWTO (R_MN10300_GNU_VTENTRY, /* type */ 258 0, /* rightshift */ 259 0, /* size (0 = byte, 1 = short, 2 = long) */ 260 0, /* bitsize */ 261 FALSE, /* pc_relative */ 262 0, /* bitpos */ 263 complain_overflow_dont, /* complain_on_overflow */ 264 NULL, /* special_function */ 265 "R_MN10300_GNU_VTENTRY", /* name */ 266 FALSE, /* partial_inplace */ 267 0, /* src_mask */ 268 0, /* dst_mask */ 269 FALSE), /* pcrel_offset */ 270 271 /* Standard 24 bit reloc. */ 272 HOWTO (R_MN10300_24, 273 0, 274 2, 275 24, 276 FALSE, 277 0, 278 complain_overflow_bitfield, 279 bfd_elf_generic_reloc, 280 "R_MN10300_24", 281 FALSE, 282 0xffffff, 283 0xffffff, 284 FALSE), 285 HOWTO (R_MN10300_GOTPC32, /* type */ 286 0, /* rightshift */ 287 2, /* size (0 = byte, 1 = short, 2 = long) */ 288 32, /* bitsize */ 289 TRUE, /* pc_relative */ 290 0, /* bitpos */ 291 complain_overflow_bitfield, /* complain_on_overflow */ 292 bfd_elf_generic_reloc, /* */ 293 "R_MN10300_GOTPC32", /* name */ 294 FALSE, /* partial_inplace */ 295 0xffffffff, /* src_mask */ 296 0xffffffff, /* dst_mask */ 297 TRUE), /* pcrel_offset */ 298 299 HOWTO (R_MN10300_GOTPC16, /* type */ 300 0, /* rightshift */ 301 1, /* size (0 = byte, 1 = short, 2 = long) */ 302 16, /* bitsize */ 303 TRUE, /* pc_relative */ 304 0, /* bitpos */ 305 complain_overflow_bitfield, /* complain_on_overflow */ 306 bfd_elf_generic_reloc, /* */ 307 "R_MN10300_GOTPC16", /* name */ 308 FALSE, /* partial_inplace */ 309 0xffff, /* src_mask */ 310 0xffff, /* dst_mask */ 311 TRUE), /* pcrel_offset */ 312 313 HOWTO (R_MN10300_GOTOFF32, /* type */ 314 0, /* rightshift */ 315 2, /* size (0 = byte, 1 = short, 2 = long) */ 316 32, /* bitsize */ 317 FALSE, /* pc_relative */ 318 0, /* bitpos */ 319 complain_overflow_bitfield, /* complain_on_overflow */ 320 bfd_elf_generic_reloc, /* */ 321 "R_MN10300_GOTOFF32", /* name */ 322 FALSE, /* partial_inplace */ 323 0xffffffff, /* src_mask */ 324 0xffffffff, /* dst_mask */ 325 FALSE), /* pcrel_offset */ 326 327 HOWTO (R_MN10300_GOTOFF24, /* type */ 328 0, /* rightshift */ 329 2, /* size (0 = byte, 1 = short, 2 = long) */ 330 24, /* bitsize */ 331 FALSE, /* pc_relative */ 332 0, /* bitpos */ 333 complain_overflow_bitfield, /* complain_on_overflow */ 334 bfd_elf_generic_reloc, /* */ 335 "R_MN10300_GOTOFF24", /* name */ 336 FALSE, /* partial_inplace */ 337 0xffffff, /* src_mask */ 338 0xffffff, /* dst_mask */ 339 FALSE), /* pcrel_offset */ 340 341 HOWTO (R_MN10300_GOTOFF16, /* type */ 342 0, /* rightshift */ 343 1, /* size (0 = byte, 1 = short, 2 = long) */ 344 16, /* bitsize */ 345 FALSE, /* pc_relative */ 346 0, /* bitpos */ 347 complain_overflow_bitfield, /* complain_on_overflow */ 348 bfd_elf_generic_reloc, /* */ 349 "R_MN10300_GOTOFF16", /* name */ 350 FALSE, /* partial_inplace */ 351 0xffff, /* src_mask */ 352 0xffff, /* dst_mask */ 353 FALSE), /* pcrel_offset */ 354 355 HOWTO (R_MN10300_PLT32, /* type */ 356 0, /* rightshift */ 357 2, /* size (0 = byte, 1 = short, 2 = long) */ 358 32, /* bitsize */ 359 TRUE, /* pc_relative */ 360 0, /* bitpos */ 361 complain_overflow_bitfield, /* complain_on_overflow */ 362 bfd_elf_generic_reloc, /* */ 363 "R_MN10300_PLT32", /* name */ 364 FALSE, /* partial_inplace */ 365 0xffffffff, /* src_mask */ 366 0xffffffff, /* dst_mask */ 367 TRUE), /* pcrel_offset */ 368 369 HOWTO (R_MN10300_PLT16, /* type */ 370 0, /* rightshift */ 371 1, /* size (0 = byte, 1 = short, 2 = long) */ 372 16, /* bitsize */ 373 TRUE, /* pc_relative */ 374 0, /* bitpos */ 375 complain_overflow_bitfield, /* complain_on_overflow */ 376 bfd_elf_generic_reloc, /* */ 377 "R_MN10300_PLT16", /* name */ 378 FALSE, /* partial_inplace */ 379 0xffff, /* src_mask */ 380 0xffff, /* dst_mask */ 381 TRUE), /* pcrel_offset */ 382 383 HOWTO (R_MN10300_GOT32, /* type */ 384 0, /* rightshift */ 385 2, /* size (0 = byte, 1 = short, 2 = long) */ 386 32, /* bitsize */ 387 FALSE, /* pc_relative */ 388 0, /* bitpos */ 389 complain_overflow_bitfield, /* complain_on_overflow */ 390 bfd_elf_generic_reloc, /* */ 391 "R_MN10300_GOT32", /* name */ 392 FALSE, /* partial_inplace */ 393 0xffffffff, /* src_mask */ 394 0xffffffff, /* dst_mask */ 395 FALSE), /* pcrel_offset */ 396 397 HOWTO (R_MN10300_GOT24, /* type */ 398 0, /* rightshift */ 399 2, /* size (0 = byte, 1 = short, 2 = long) */ 400 24, /* bitsize */ 401 FALSE, /* pc_relative */ 402 0, /* bitpos */ 403 complain_overflow_bitfield, /* complain_on_overflow */ 404 bfd_elf_generic_reloc, /* */ 405 "R_MN10300_GOT24", /* name */ 406 FALSE, /* partial_inplace */ 407 0xffffffff, /* src_mask */ 408 0xffffffff, /* dst_mask */ 409 FALSE), /* pcrel_offset */ 410 411 HOWTO (R_MN10300_GOT16, /* type */ 412 0, /* rightshift */ 413 1, /* size (0 = byte, 1 = short, 2 = long) */ 414 16, /* bitsize */ 415 FALSE, /* pc_relative */ 416 0, /* bitpos */ 417 complain_overflow_bitfield, /* complain_on_overflow */ 418 bfd_elf_generic_reloc, /* */ 419 "R_MN10300_GOT16", /* name */ 420 FALSE, /* partial_inplace */ 421 0xffffffff, /* src_mask */ 422 0xffffffff, /* dst_mask */ 423 FALSE), /* pcrel_offset */ 424 425 HOWTO (R_MN10300_COPY, /* type */ 426 0, /* rightshift */ 427 2, /* size (0 = byte, 1 = short, 2 = long) */ 428 32, /* bitsize */ 429 FALSE, /* pc_relative */ 430 0, /* bitpos */ 431 complain_overflow_bitfield, /* complain_on_overflow */ 432 bfd_elf_generic_reloc, /* */ 433 "R_MN10300_COPY", /* name */ 434 FALSE, /* partial_inplace */ 435 0xffffffff, /* src_mask */ 436 0xffffffff, /* dst_mask */ 437 FALSE), /* pcrel_offset */ 438 439 HOWTO (R_MN10300_GLOB_DAT, /* type */ 440 0, /* rightshift */ 441 2, /* size (0 = byte, 1 = short, 2 = long) */ 442 32, /* bitsize */ 443 FALSE, /* pc_relative */ 444 0, /* bitpos */ 445 complain_overflow_bitfield, /* complain_on_overflow */ 446 bfd_elf_generic_reloc, /* */ 447 "R_MN10300_GLOB_DAT", /* name */ 448 FALSE, /* partial_inplace */ 449 0xffffffff, /* src_mask */ 450 0xffffffff, /* dst_mask */ 451 FALSE), /* pcrel_offset */ 452 453 HOWTO (R_MN10300_JMP_SLOT, /* type */ 454 0, /* rightshift */ 455 2, /* size (0 = byte, 1 = short, 2 = long) */ 456 32, /* bitsize */ 457 FALSE, /* pc_relative */ 458 0, /* bitpos */ 459 complain_overflow_bitfield, /* complain_on_overflow */ 460 bfd_elf_generic_reloc, /* */ 461 "R_MN10300_JMP_SLOT", /* name */ 462 FALSE, /* partial_inplace */ 463 0xffffffff, /* src_mask */ 464 0xffffffff, /* dst_mask */ 465 FALSE), /* pcrel_offset */ 466 467 HOWTO (R_MN10300_RELATIVE, /* type */ 468 0, /* rightshift */ 469 2, /* size (0 = byte, 1 = short, 2 = long) */ 470 32, /* bitsize */ 471 FALSE, /* pc_relative */ 472 0, /* bitpos */ 473 complain_overflow_bitfield, /* complain_on_overflow */ 474 bfd_elf_generic_reloc, /* */ 475 "R_MN10300_RELATIVE", /* name */ 476 FALSE, /* partial_inplace */ 477 0xffffffff, /* src_mask */ 478 0xffffffff, /* dst_mask */ 479 FALSE), /* pcrel_offset */ 480 481 HOWTO (R_MN10300_TLS_GD, /* type */ 482 0, /* rightshift */ 483 2, /* size (0 = byte, 1 = short, 2 = long) */ 484 32, /* bitsize */ 485 FALSE, /* pc_relative */ 486 0, /* bitpos */ 487 complain_overflow_bitfield, /* complain_on_overflow */ 488 bfd_elf_generic_reloc, /* */ 489 "R_MN10300_TLS_GD", /* name */ 490 FALSE, /* partial_inplace */ 491 0xffffffff, /* src_mask */ 492 0xffffffff, /* dst_mask */ 493 FALSE), /* pcrel_offset */ 494 495 HOWTO (R_MN10300_TLS_LD, /* type */ 496 0, /* rightshift */ 497 2, /* size (0 = byte, 1 = short, 2 = long) */ 498 32, /* bitsize */ 499 FALSE, /* pc_relative */ 500 0, /* bitpos */ 501 complain_overflow_bitfield, /* complain_on_overflow */ 502 bfd_elf_generic_reloc, /* */ 503 "R_MN10300_TLS_LD", /* name */ 504 FALSE, /* partial_inplace */ 505 0xffffffff, /* src_mask */ 506 0xffffffff, /* dst_mask */ 507 FALSE), /* pcrel_offset */ 508 509 HOWTO (R_MN10300_TLS_LDO, /* type */ 510 0, /* rightshift */ 511 2, /* size (0 = byte, 1 = short, 2 = long) */ 512 32, /* bitsize */ 513 FALSE, /* pc_relative */ 514 0, /* bitpos */ 515 complain_overflow_bitfield, /* complain_on_overflow */ 516 bfd_elf_generic_reloc, /* */ 517 "R_MN10300_TLS_LDO", /* name */ 518 FALSE, /* partial_inplace */ 519 0xffffffff, /* src_mask */ 520 0xffffffff, /* dst_mask */ 521 FALSE), /* pcrel_offset */ 522 523 HOWTO (R_MN10300_TLS_GOTIE, /* type */ 524 0, /* rightshift */ 525 2, /* size (0 = byte, 1 = short, 2 = long) */ 526 32, /* bitsize */ 527 FALSE, /* pc_relative */ 528 0, /* bitpos */ 529 complain_overflow_bitfield, /* complain_on_overflow */ 530 bfd_elf_generic_reloc, /* */ 531 "R_MN10300_TLS_GOTIE", /* name */ 532 FALSE, /* partial_inplace */ 533 0xffffffff, /* src_mask */ 534 0xffffffff, /* dst_mask */ 535 FALSE), /* pcrel_offset */ 536 537 HOWTO (R_MN10300_TLS_IE, /* type */ 538 0, /* rightshift */ 539 2, /* size (0 = byte, 1 = short, 2 = long) */ 540 32, /* bitsize */ 541 FALSE, /* pc_relative */ 542 0, /* bitpos */ 543 complain_overflow_bitfield, /* complain_on_overflow */ 544 bfd_elf_generic_reloc, /* */ 545 "R_MN10300_TLS_IE", /* name */ 546 FALSE, /* partial_inplace */ 547 0xffffffff, /* src_mask */ 548 0xffffffff, /* dst_mask */ 549 FALSE), /* pcrel_offset */ 550 551 HOWTO (R_MN10300_TLS_LE, /* type */ 552 0, /* rightshift */ 553 2, /* size (0 = byte, 1 = short, 2 = long) */ 554 32, /* bitsize */ 555 FALSE, /* pc_relative */ 556 0, /* bitpos */ 557 complain_overflow_bitfield, /* complain_on_overflow */ 558 bfd_elf_generic_reloc, /* */ 559 "R_MN10300_TLS_LE", /* name */ 560 FALSE, /* partial_inplace */ 561 0xffffffff, /* src_mask */ 562 0xffffffff, /* dst_mask */ 563 FALSE), /* pcrel_offset */ 564 565 HOWTO (R_MN10300_TLS_DTPMOD, /* type */ 566 0, /* rightshift */ 567 2, /* size (0 = byte, 1 = short, 2 = long) */ 568 32, /* bitsize */ 569 FALSE, /* pc_relative */ 570 0, /* bitpos */ 571 complain_overflow_bitfield, /* complain_on_overflow */ 572 bfd_elf_generic_reloc, /* */ 573 "R_MN10300_TLS_DTPMOD", /* name */ 574 FALSE, /* partial_inplace */ 575 0xffffffff, /* src_mask */ 576 0xffffffff, /* dst_mask */ 577 FALSE), /* pcrel_offset */ 578 579 HOWTO (R_MN10300_TLS_DTPOFF, /* type */ 580 0, /* rightshift */ 581 2, /* size (0 = byte, 1 = short, 2 = long) */ 582 32, /* bitsize */ 583 FALSE, /* pc_relative */ 584 0, /* bitpos */ 585 complain_overflow_bitfield, /* complain_on_overflow */ 586 bfd_elf_generic_reloc, /* */ 587 "R_MN10300_TLS_DTPOFF", /* name */ 588 FALSE, /* partial_inplace */ 589 0xffffffff, /* src_mask */ 590 0xffffffff, /* dst_mask */ 591 FALSE), /* pcrel_offset */ 592 593 HOWTO (R_MN10300_TLS_TPOFF, /* type */ 594 0, /* rightshift */ 595 2, /* size (0 = byte, 1 = short, 2 = long) */ 596 32, /* bitsize */ 597 FALSE, /* pc_relative */ 598 0, /* bitpos */ 599 complain_overflow_bitfield, /* complain_on_overflow */ 600 bfd_elf_generic_reloc, /* */ 601 "R_MN10300_TLS_TPOFF", /* name */ 602 FALSE, /* partial_inplace */ 603 0xffffffff, /* src_mask */ 604 0xffffffff, /* dst_mask */ 605 FALSE), /* pcrel_offset */ 606 607 HOWTO (R_MN10300_SYM_DIFF, /* type */ 608 0, /* rightshift */ 609 2, /* size (0 = byte, 1 = short, 2 = long) */ 610 32, /* bitsize */ 611 FALSE, /* pc_relative */ 612 0, /* bitpos */ 613 complain_overflow_dont,/* complain_on_overflow */ 614 NULL, /* special handler. */ 615 "R_MN10300_SYM_DIFF", /* name */ 616 FALSE, /* partial_inplace */ 617 0xffffffff, /* src_mask */ 618 0xffffffff, /* dst_mask */ 619 FALSE), /* pcrel_offset */ 620 621 HOWTO (R_MN10300_ALIGN, /* type */ 622 0, /* rightshift */ 623 0, /* size (0 = byte, 1 = short, 2 = long) */ 624 32, /* bitsize */ 625 FALSE, /* pc_relative */ 626 0, /* bitpos */ 627 complain_overflow_dont,/* complain_on_overflow */ 628 NULL, /* special handler. */ 629 "R_MN10300_ALIGN", /* name */ 630 FALSE, /* partial_inplace */ 631 0, /* src_mask */ 632 0, /* dst_mask */ 633 FALSE) /* pcrel_offset */ 634}; 635 636struct mn10300_reloc_map 637{ 638 bfd_reloc_code_real_type bfd_reloc_val; 639 unsigned char elf_reloc_val; 640}; 641 642static const struct mn10300_reloc_map mn10300_reloc_map[] = 643{ 644 { BFD_RELOC_NONE, R_MN10300_NONE, }, 645 { BFD_RELOC_32, R_MN10300_32, }, 646 { BFD_RELOC_16, R_MN10300_16, }, 647 { BFD_RELOC_8, R_MN10300_8, }, 648 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, }, 649 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, }, 650 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, }, 651 { BFD_RELOC_24, R_MN10300_24, }, 652 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT }, 653 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY }, 654 { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 }, 655 { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 }, 656 { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 }, 657 { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 }, 658 { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 }, 659 { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 }, 660 { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 }, 661 { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 }, 662 { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 }, 663 { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 }, 664 { BFD_RELOC_MN10300_COPY, R_MN10300_COPY }, 665 { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT }, 666 { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT }, 667 { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE }, 668 { BFD_RELOC_MN10300_TLS_GD, R_MN10300_TLS_GD }, 669 { BFD_RELOC_MN10300_TLS_LD, R_MN10300_TLS_LD }, 670 { BFD_RELOC_MN10300_TLS_LDO, R_MN10300_TLS_LDO }, 671 { BFD_RELOC_MN10300_TLS_GOTIE, R_MN10300_TLS_GOTIE }, 672 { BFD_RELOC_MN10300_TLS_IE, R_MN10300_TLS_IE }, 673 { BFD_RELOC_MN10300_TLS_LE, R_MN10300_TLS_LE }, 674 { BFD_RELOC_MN10300_TLS_DTPMOD, R_MN10300_TLS_DTPMOD }, 675 { BFD_RELOC_MN10300_TLS_DTPOFF, R_MN10300_TLS_DTPOFF }, 676 { BFD_RELOC_MN10300_TLS_TPOFF, R_MN10300_TLS_TPOFF }, 677 { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF }, 678 { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN } 679}; 680 681/* Create the GOT section. */ 682 683static bfd_boolean 684_bfd_mn10300_elf_create_got_section (bfd * abfd, 685 struct bfd_link_info * info) 686{ 687 flagword flags; 688 flagword pltflags; 689 asection * s; 690 struct elf_link_hash_entry * h; 691 const struct elf_backend_data * bed = get_elf_backend_data (abfd); 692 struct elf_link_hash_table *htab; 693 int ptralign; 694 695 /* This function may be called more than once. */ 696 htab = elf_hash_table (info); 697 if (htab->sgot != NULL) 698 return TRUE; 699 700 switch (bed->s->arch_size) 701 { 702 case 32: 703 ptralign = 2; 704 break; 705 706 case 64: 707 ptralign = 3; 708 break; 709 710 default: 711 bfd_set_error (bfd_error_bad_value); 712 return FALSE; 713 } 714 715 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY 716 | SEC_LINKER_CREATED); 717 718 pltflags = flags; 719 pltflags |= SEC_CODE; 720 if (bed->plt_not_loaded) 721 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS); 722 if (bed->plt_readonly) 723 pltflags |= SEC_READONLY; 724 725 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags); 726 htab->splt = s; 727 if (s == NULL 728 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment)) 729 return FALSE; 730 731 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the 732 .plt section. */ 733 if (bed->want_plt_sym) 734 { 735 h = _bfd_elf_define_linkage_sym (abfd, info, s, 736 "_PROCEDURE_LINKAGE_TABLE_"); 737 htab->hplt = h; 738 if (h == NULL) 739 return FALSE; 740 } 741 742 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags); 743 htab->sgot = s; 744 if (s == NULL 745 || ! bfd_set_section_alignment (abfd, s, ptralign)) 746 return FALSE; 747 748 if (bed->want_got_plt) 749 { 750 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags); 751 htab->sgotplt = s; 752 if (s == NULL 753 || ! bfd_set_section_alignment (abfd, s, ptralign)) 754 return FALSE; 755 } 756 757 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got 758 (or .got.plt) section. We don't do this in the linker script 759 because we don't want to define the symbol if we are not creating 760 a global offset table. */ 761 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_"); 762 htab->hgot = h; 763 if (h == NULL) 764 return FALSE; 765 766 /* The first bit of the global offset table is the header. */ 767 s->size += bed->got_header_size; 768 769 return TRUE; 770} 771 772static reloc_howto_type * 773bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, 774 bfd_reloc_code_real_type code) 775{ 776 unsigned int i; 777 778 for (i = ARRAY_SIZE (mn10300_reloc_map); i--;) 779 if (mn10300_reloc_map[i].bfd_reloc_val == code) 780 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val]; 781 782 return NULL; 783} 784 785static reloc_howto_type * 786bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 787 const char *r_name) 788{ 789 unsigned int i; 790 791 for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;) 792 if (elf_mn10300_howto_table[i].name != NULL 793 && strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0) 794 return elf_mn10300_howto_table + i; 795 796 return NULL; 797} 798 799/* Set the howto pointer for an MN10300 ELF reloc. */ 800 801static void 802mn10300_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, 803 arelent *cache_ptr, 804 Elf_Internal_Rela *dst) 805{ 806 unsigned int r_type; 807 808 r_type = ELF32_R_TYPE (dst->r_info); 809 if (r_type >= R_MN10300_MAX) 810 { 811 /* xgettext:c-format */ 812 _bfd_error_handler (_("%B: unrecognised MN10300 reloc number: %d"), 813 abfd, r_type); 814 bfd_set_error (bfd_error_bad_value); 815 r_type = R_MN10300_NONE; 816 } 817 cache_ptr->howto = elf_mn10300_howto_table + r_type; 818} 819 820static int 821elf_mn10300_tls_transition (struct bfd_link_info * info, 822 int r_type, 823 struct elf_link_hash_entry * h, 824 asection * sec, 825 bfd_boolean counting) 826{ 827 bfd_boolean is_local; 828 829 if (r_type == R_MN10300_TLS_GD 830 && h != NULL 831 && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_IE) 832 return R_MN10300_TLS_GOTIE; 833 834 if (bfd_link_pic (info)) 835 return r_type; 836 837 if (! (sec->flags & SEC_CODE)) 838 return r_type; 839 840 if (! counting && h != NULL && ! elf_hash_table (info)->dynamic_sections_created) 841 is_local = TRUE; 842 else 843 is_local = SYMBOL_CALLS_LOCAL (info, h); 844 845 /* For the main program, these are the transitions we do. */ 846 switch (r_type) 847 { 848 case R_MN10300_TLS_GD: return is_local ? R_MN10300_TLS_LE : R_MN10300_TLS_GOTIE; 849 case R_MN10300_TLS_LD: return R_MN10300_NONE; 850 case R_MN10300_TLS_LDO: return R_MN10300_TLS_LE; 851 case R_MN10300_TLS_IE: 852 case R_MN10300_TLS_GOTIE: return is_local ? R_MN10300_TLS_LE : r_type; 853 } 854 855 return r_type; 856} 857 858/* Return the relocation value for @tpoff relocation 859 if STT_TLS virtual address is ADDRESS. */ 860 861static bfd_vma 862dtpoff (struct bfd_link_info * info, bfd_vma address) 863{ 864 struct elf_link_hash_table *htab = elf_hash_table (info); 865 866 /* If tls_sec is NULL, we should have signalled an error already. */ 867 if (htab->tls_sec == NULL) 868 return 0; 869 return address - htab->tls_sec->vma; 870} 871 872/* Return the relocation value for @tpoff relocation 873 if STT_TLS virtual address is ADDRESS. */ 874 875static bfd_vma 876tpoff (struct bfd_link_info * info, bfd_vma address) 877{ 878 struct elf_link_hash_table *htab = elf_hash_table (info); 879 880 /* If tls_sec is NULL, we should have signalled an error already. */ 881 if (htab->tls_sec == NULL) 882 return 0; 883 return address - (htab->tls_size + htab->tls_sec->vma); 884} 885 886/* Returns nonzero if there's a R_MN10300_PLT32 reloc that we now need 887 to skip, after this one. The actual value is the offset between 888 this reloc and the PLT reloc. */ 889 890static int 891mn10300_do_tls_transition (bfd * input_bfd, 892 unsigned int r_type, 893 unsigned int tls_r_type, 894 bfd_byte * contents, 895 bfd_vma offset) 896{ 897 bfd_byte *op = contents + offset; 898 int gotreg = 0; 899 900#define TLS_PAIR(r1,r2) ((r1) * R_MN10300_MAX + (r2)) 901 902 /* This is common to all GD/LD transitions, so break it out. */ 903 if (r_type == R_MN10300_TLS_GD 904 || r_type == R_MN10300_TLS_LD) 905 { 906 op -= 2; 907 /* mov imm,d0. */ 908 BFD_ASSERT (bfd_get_8 (input_bfd, op) == 0xFC); 909 BFD_ASSERT (bfd_get_8 (input_bfd, op + 1) == 0xCC); 910 /* add aN,d0. */ 911 BFD_ASSERT (bfd_get_8 (input_bfd, op + 6) == 0xF1); 912 gotreg = (bfd_get_8 (input_bfd, op + 7) & 0x0c) >> 2; 913 /* Call. */ 914 BFD_ASSERT (bfd_get_8 (input_bfd, op + 8) == 0xDD); 915 } 916 917 switch (TLS_PAIR (r_type, tls_r_type)) 918 { 919 case TLS_PAIR (R_MN10300_TLS_GD, R_MN10300_TLS_GOTIE): 920 { 921 /* Keep track of which register we put GOTptr in. */ 922 /* mov (_x@indntpoff,a2),a0. */ 923 memcpy (op, "\xFC\x20\x00\x00\x00\x00", 6); 924 op[1] |= gotreg; 925 /* add e2,a0. */ 926 memcpy (op+6, "\xF9\x78\x28", 3); 927 /* or 0x00000000, d0 - six byte nop. */ 928 memcpy (op+9, "\xFC\xE4\x00\x00\x00\x00", 6); 929 } 930 return 7; 931 932 case TLS_PAIR (R_MN10300_TLS_GD, R_MN10300_TLS_LE): 933 { 934 /* Register is *always* a0. */ 935 /* mov _x@tpoff,a0. */ 936 memcpy (op, "\xFC\xDC\x00\x00\x00\x00", 6); 937 /* add e2,a0. */ 938 memcpy (op+6, "\xF9\x78\x28", 3); 939 /* or 0x00000000, d0 - six byte nop. */ 940 memcpy (op+9, "\xFC\xE4\x00\x00\x00\x00", 6); 941 } 942 return 7; 943 case TLS_PAIR (R_MN10300_TLS_LD, R_MN10300_NONE): 944 { 945 /* Register is *always* a0. */ 946 /* mov e2,a0. */ 947 memcpy (op, "\xF5\x88", 2); 948 /* or 0x00000000, d0 - six byte nop. */ 949 memcpy (op+2, "\xFC\xE4\x00\x00\x00\x00", 6); 950 /* or 0x00000000, e2 - seven byte nop. */ 951 memcpy (op+8, "\xFE\x19\x22\x00\x00\x00\x00", 7); 952 } 953 return 7; 954 955 case TLS_PAIR (R_MN10300_TLS_LDO, R_MN10300_TLS_LE): 956 /* No changes needed, just the reloc change. */ 957 return 0; 958 959 /* These are a little tricky, because we have to detect which 960 opcode is being used (they're different sizes, with the reloc 961 at different offsets within the opcode) and convert each 962 accordingly, copying the operands as needed. The conversions 963 we do are as follows (IE,GOTIE,LE): 964 965 1111 1100 1010 01Dn [-- abs32 --] MOV (x@indntpoff),Dn 966 1111 1100 0000 DnAm [-- abs32 --] MOV (x@gotntpoff,Am),Dn 967 1111 1100 1100 11Dn [-- abs32 --] MOV x@tpoff,Dn 968 969 1111 1100 1010 00An [-- abs32 --] MOV (x@indntpoff),An 970 1111 1100 0010 AnAm [-- abs32 --] MOV (x@gotntpoff,Am),An 971 1111 1100 1101 11An [-- abs32 --] MOV x@tpoff,An 972 973 1111 1110 0000 1110 Rnnn Xxxx [-- abs32 --] MOV (x@indntpoff),Rn 974 1111 1110 0000 1010 Rnnn Rmmm [-- abs32 --] MOV (x@indntpoff,Rm),Rn 975 1111 1110 0000 1000 Rnnn Xxxx [-- abs32 --] MOV x@tpoff,Rn 976 977 Since the GOT pointer is always $a2, we assume the last 978 normally won't happen, but let's be paranoid and plan for the 979 day that GCC optimizes it somewhow. */ 980 981 case TLS_PAIR (R_MN10300_TLS_IE, R_MN10300_TLS_LE): 982 if (op[-2] == 0xFC) 983 { 984 op -= 2; 985 if ((op[1] & 0xFC) == 0xA4) /* Dn */ 986 { 987 op[1] &= 0x03; /* Leaves Dn. */ 988 op[1] |= 0xCC; 989 } 990 else /* An */ 991 { 992 op[1] &= 0x03; /* Leaves An. */ 993 op[1] |= 0xDC; 994 } 995 } 996 else if (op[-3] == 0xFE) 997 op[-2] = 0x08; 998 else 999 abort (); 1000 break; 1001 1002 case TLS_PAIR (R_MN10300_TLS_GOTIE, R_MN10300_TLS_LE): 1003 if (op[-2] == 0xFC) 1004 { 1005 op -= 2; 1006 if ((op[1] & 0xF0) == 0x00) /* Dn */ 1007 { 1008 op[1] &= 0x0C; /* Leaves Dn. */ 1009 op[1] >>= 2; 1010 op[1] |= 0xCC; 1011 } 1012 else /* An */ 1013 { 1014 op[1] &= 0x0C; /* Leaves An. */ 1015 op[1] >>= 2; 1016 op[1] |= 0xDC; 1017 } 1018 } 1019 else if (op[-3] == 0xFE) 1020 op[-2] = 0x08; 1021 else 1022 abort (); 1023 break; 1024 1025 default: 1026 _bfd_error_handler 1027 /* xgettext:c-format */ 1028 (_("%s: Unsupported transition from %s to %s"), 1029 bfd_get_filename (input_bfd), 1030 elf_mn10300_howto_table[r_type].name, 1031 elf_mn10300_howto_table[tls_r_type].name); 1032 break; 1033 } 1034#undef TLS_PAIR 1035 return 0; 1036} 1037 1038/* Look through the relocs for a section during the first phase. 1039 Since we don't do .gots or .plts, we just need to consider the 1040 virtual table relocs for gc. */ 1041 1042static bfd_boolean 1043mn10300_elf_check_relocs (bfd *abfd, 1044 struct bfd_link_info *info, 1045 asection *sec, 1046 const Elf_Internal_Rela *relocs) 1047{ 1048 struct elf32_mn10300_link_hash_table * htab = elf32_mn10300_hash_table (info); 1049 bfd_boolean sym_diff_reloc_seen; 1050 Elf_Internal_Shdr *symtab_hdr; 1051 Elf_Internal_Sym * isymbuf = NULL; 1052 struct elf_link_hash_entry **sym_hashes; 1053 const Elf_Internal_Rela *rel; 1054 const Elf_Internal_Rela *rel_end; 1055 bfd * dynobj; 1056 bfd_vma * local_got_offsets; 1057 asection * sgot; 1058 asection * srelgot; 1059 asection * sreloc; 1060 bfd_boolean result = FALSE; 1061 1062 sgot = NULL; 1063 srelgot = NULL; 1064 sreloc = NULL; 1065 1066 if (bfd_link_relocatable (info)) 1067 return TRUE; 1068 1069 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 1070 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 1071 sym_hashes = elf_sym_hashes (abfd); 1072 1073 dynobj = elf_hash_table (info)->dynobj; 1074 local_got_offsets = elf_local_got_offsets (abfd); 1075 rel_end = relocs + sec->reloc_count; 1076 sym_diff_reloc_seen = FALSE; 1077 1078 for (rel = relocs; rel < rel_end; rel++) 1079 { 1080 struct elf_link_hash_entry *h; 1081 unsigned long r_symndx; 1082 unsigned int r_type; 1083 int tls_type = GOT_NORMAL; 1084 1085 r_symndx = ELF32_R_SYM (rel->r_info); 1086 if (r_symndx < symtab_hdr->sh_info) 1087 h = NULL; 1088 else 1089 { 1090 h = sym_hashes[r_symndx - symtab_hdr->sh_info]; 1091 while (h->root.type == bfd_link_hash_indirect 1092 || h->root.type == bfd_link_hash_warning) 1093 h = (struct elf_link_hash_entry *) h->root.u.i.link; 1094 1095 /* PR15323, ref flags aren't set for references in the same 1096 object. */ 1097 h->root.non_ir_ref = 1; 1098 } 1099 1100 r_type = ELF32_R_TYPE (rel->r_info); 1101 r_type = elf_mn10300_tls_transition (info, r_type, h, sec, TRUE); 1102 1103 /* Some relocs require a global offset table. */ 1104 if (dynobj == NULL) 1105 { 1106 switch (r_type) 1107 { 1108 case R_MN10300_GOT32: 1109 case R_MN10300_GOT24: 1110 case R_MN10300_GOT16: 1111 case R_MN10300_GOTOFF32: 1112 case R_MN10300_GOTOFF24: 1113 case R_MN10300_GOTOFF16: 1114 case R_MN10300_GOTPC32: 1115 case R_MN10300_GOTPC16: 1116 case R_MN10300_TLS_GD: 1117 case R_MN10300_TLS_LD: 1118 case R_MN10300_TLS_GOTIE: 1119 case R_MN10300_TLS_IE: 1120 elf_hash_table (info)->dynobj = dynobj = abfd; 1121 if (! _bfd_mn10300_elf_create_got_section (dynobj, info)) 1122 goto fail; 1123 break; 1124 1125 default: 1126 break; 1127 } 1128 } 1129 1130 switch (r_type) 1131 { 1132 /* This relocation describes the C++ object vtable hierarchy. 1133 Reconstruct it for later use during GC. */ 1134 case R_MN10300_GNU_VTINHERIT: 1135 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) 1136 goto fail; 1137 break; 1138 1139 /* This relocation describes which C++ vtable entries are actually 1140 used. Record for later use during GC. */ 1141 case R_MN10300_GNU_VTENTRY: 1142 BFD_ASSERT (h != NULL); 1143 if (h != NULL 1144 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) 1145 goto fail; 1146 break; 1147 1148 case R_MN10300_TLS_LD: 1149 htab->tls_ldm_got.refcount ++; 1150 tls_type = GOT_TLS_LD; 1151 1152 if (htab->tls_ldm_got.got_allocated) 1153 break; 1154 goto create_got; 1155 1156 case R_MN10300_TLS_IE: 1157 case R_MN10300_TLS_GOTIE: 1158 if (bfd_link_pic (info)) 1159 info->flags |= DF_STATIC_TLS; 1160 /* Fall through */ 1161 1162 case R_MN10300_TLS_GD: 1163 case R_MN10300_GOT32: 1164 case R_MN10300_GOT24: 1165 case R_MN10300_GOT16: 1166 create_got: 1167 /* This symbol requires a global offset table entry. */ 1168 1169 switch (r_type) 1170 { 1171 case R_MN10300_TLS_IE: 1172 case R_MN10300_TLS_GOTIE: tls_type = GOT_TLS_IE; break; 1173 case R_MN10300_TLS_GD: tls_type = GOT_TLS_GD; break; 1174 default: tls_type = GOT_NORMAL; break; 1175 } 1176 1177 sgot = htab->root.sgot; 1178 srelgot = htab->root.srelgot; 1179 BFD_ASSERT (sgot != NULL && srelgot != NULL); 1180 1181 if (r_type == R_MN10300_TLS_LD) 1182 { 1183 htab->tls_ldm_got.offset = sgot->size; 1184 htab->tls_ldm_got.got_allocated ++; 1185 } 1186 else if (h != NULL) 1187 { 1188 if (elf_mn10300_hash_entry (h)->tls_type != tls_type 1189 && elf_mn10300_hash_entry (h)->tls_type != GOT_UNKNOWN) 1190 { 1191 if (tls_type == GOT_TLS_IE 1192 && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_GD) 1193 /* No change - this is ok. */; 1194 else if (tls_type == GOT_TLS_GD 1195 && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_IE) 1196 /* Transition GD->IE. */ 1197 tls_type = GOT_TLS_IE; 1198 else 1199 _bfd_error_handler 1200 /* xgettext:c-format */ 1201 (_("%B: %s' accessed both as normal and thread local symbol"), 1202 abfd, h ? h->root.root.string : "<local>"); 1203 } 1204 1205 elf_mn10300_hash_entry (h)->tls_type = tls_type; 1206 1207 if (h->got.offset != (bfd_vma) -1) 1208 /* We have already allocated space in the .got. */ 1209 break; 1210 1211 h->got.offset = sgot->size; 1212 1213 if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 1214 /* Make sure this symbol is output as a dynamic symbol. */ 1215 && h->dynindx == -1) 1216 { 1217 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 1218 goto fail; 1219 } 1220 1221 srelgot->size += sizeof (Elf32_External_Rela); 1222 if (r_type == R_MN10300_TLS_GD) 1223 srelgot->size += sizeof (Elf32_External_Rela); 1224 } 1225 else 1226 { 1227 /* This is a global offset table entry for a local 1228 symbol. */ 1229 if (local_got_offsets == NULL) 1230 { 1231 size_t size; 1232 unsigned int i; 1233 1234 size = symtab_hdr->sh_info * (sizeof (bfd_vma) + sizeof (char)); 1235 local_got_offsets = bfd_alloc (abfd, size); 1236 1237 if (local_got_offsets == NULL) 1238 goto fail; 1239 1240 elf_local_got_offsets (abfd) = local_got_offsets; 1241 elf_mn10300_local_got_tls_type (abfd) 1242 = (char *) (local_got_offsets + symtab_hdr->sh_info); 1243 1244 for (i = 0; i < symtab_hdr->sh_info; i++) 1245 local_got_offsets[i] = (bfd_vma) -1; 1246 } 1247 1248 if (local_got_offsets[r_symndx] != (bfd_vma) -1) 1249 /* We have already allocated space in the .got. */ 1250 break; 1251 1252 local_got_offsets[r_symndx] = sgot->size; 1253 1254 if (bfd_link_pic (info)) 1255 { 1256 /* If we are generating a shared object, we need to 1257 output a R_MN10300_RELATIVE reloc so that the dynamic 1258 linker can adjust this GOT entry. */ 1259 srelgot->size += sizeof (Elf32_External_Rela); 1260 1261 if (r_type == R_MN10300_TLS_GD) 1262 /* And a R_MN10300_TLS_DTPOFF reloc as well. */ 1263 srelgot->size += sizeof (Elf32_External_Rela); 1264 } 1265 1266 elf_mn10300_local_got_tls_type (abfd) [r_symndx] = tls_type; 1267 } 1268 1269 sgot->size += 4; 1270 if (r_type == R_MN10300_TLS_GD 1271 || r_type == R_MN10300_TLS_LD) 1272 sgot->size += 4; 1273 1274 goto need_shared_relocs; 1275 1276 case R_MN10300_PLT32: 1277 case R_MN10300_PLT16: 1278 /* This symbol requires a procedure linkage table entry. We 1279 actually build the entry in adjust_dynamic_symbol, 1280 because this might be a case of linking PIC code which is 1281 never referenced by a dynamic object, in which case we 1282 don't need to generate a procedure linkage table entry 1283 after all. */ 1284 1285 /* If this is a local symbol, we resolve it directly without 1286 creating a procedure linkage table entry. */ 1287 if (h == NULL) 1288 continue; 1289 1290 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL 1291 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN) 1292 break; 1293 1294 h->needs_plt = 1; 1295 break; 1296 1297 case R_MN10300_24: 1298 case R_MN10300_16: 1299 case R_MN10300_8: 1300 case R_MN10300_PCREL32: 1301 case R_MN10300_PCREL16: 1302 case R_MN10300_PCREL8: 1303 if (h != NULL) 1304 h->non_got_ref = 1; 1305 break; 1306 1307 case R_MN10300_SYM_DIFF: 1308 sym_diff_reloc_seen = TRUE; 1309 break; 1310 1311 case R_MN10300_32: 1312 if (h != NULL) 1313 h->non_got_ref = 1; 1314 1315 need_shared_relocs: 1316 /* If we are creating a shared library, then we 1317 need to copy the reloc into the shared library. */ 1318 if (bfd_link_pic (info) 1319 && (sec->flags & SEC_ALLOC) != 0 1320 /* Do not generate a dynamic reloc for a 1321 reloc associated with a SYM_DIFF operation. */ 1322 && ! sym_diff_reloc_seen) 1323 { 1324 asection * sym_section = NULL; 1325 1326 /* Find the section containing the 1327 symbol involved in the relocation. */ 1328 if (h == NULL) 1329 { 1330 Elf_Internal_Sym * isym; 1331 1332 if (isymbuf == NULL) 1333 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 1334 symtab_hdr->sh_info, 0, 1335 NULL, NULL, NULL); 1336 if (isymbuf) 1337 { 1338 isym = isymbuf + r_symndx; 1339 /* All we care about is whether this local symbol is absolute. */ 1340 if (isym->st_shndx == SHN_ABS) 1341 sym_section = bfd_abs_section_ptr; 1342 } 1343 } 1344 else 1345 { 1346 if (h->root.type == bfd_link_hash_defined 1347 || h->root.type == bfd_link_hash_defweak) 1348 sym_section = h->root.u.def.section; 1349 } 1350 1351 /* If the symbol is absolute then the relocation can 1352 be resolved during linking and there is no need for 1353 a dynamic reloc. */ 1354 if (sym_section != bfd_abs_section_ptr) 1355 { 1356 /* When creating a shared object, we must copy these 1357 reloc types into the output file. We create a reloc 1358 section in dynobj and make room for this reloc. */ 1359 if (sreloc == NULL) 1360 { 1361 sreloc = _bfd_elf_make_dynamic_reloc_section 1362 (sec, dynobj, 2, abfd, /*rela?*/ TRUE); 1363 if (sreloc == NULL) 1364 goto fail; 1365 } 1366 1367 sreloc->size += sizeof (Elf32_External_Rela); 1368 } 1369 } 1370 1371 break; 1372 } 1373 1374 if (ELF32_R_TYPE (rel->r_info) != R_MN10300_SYM_DIFF) 1375 sym_diff_reloc_seen = FALSE; 1376 } 1377 1378 result = TRUE; 1379 fail: 1380 if (isymbuf != NULL) 1381 free (isymbuf); 1382 1383 return result; 1384} 1385 1386/* Return the section that should be marked against GC for a given 1387 relocation. */ 1388 1389static asection * 1390mn10300_elf_gc_mark_hook (asection *sec, 1391 struct bfd_link_info *info, 1392 Elf_Internal_Rela *rel, 1393 struct elf_link_hash_entry *h, 1394 Elf_Internal_Sym *sym) 1395{ 1396 if (h != NULL) 1397 switch (ELF32_R_TYPE (rel->r_info)) 1398 { 1399 case R_MN10300_GNU_VTINHERIT: 1400 case R_MN10300_GNU_VTENTRY: 1401 return NULL; 1402 } 1403 1404 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); 1405} 1406 1407/* Perform a relocation as part of a final link. */ 1408 1409static bfd_reloc_status_type 1410mn10300_elf_final_link_relocate (reloc_howto_type *howto, 1411 bfd *input_bfd, 1412 bfd *output_bfd ATTRIBUTE_UNUSED, 1413 asection *input_section, 1414 bfd_byte *contents, 1415 bfd_vma offset, 1416 bfd_vma value, 1417 bfd_vma addend, 1418 struct elf_link_hash_entry * h, 1419 unsigned long symndx, 1420 struct bfd_link_info *info, 1421 asection *sym_sec ATTRIBUTE_UNUSED, 1422 int is_local ATTRIBUTE_UNUSED) 1423{ 1424 struct elf32_mn10300_link_hash_table * htab = elf32_mn10300_hash_table (info); 1425 static asection * sym_diff_section; 1426 static bfd_vma sym_diff_value; 1427 bfd_boolean is_sym_diff_reloc; 1428 unsigned long r_type = howto->type; 1429 bfd_byte * hit_data = contents + offset; 1430 bfd * dynobj; 1431 asection * sgot; 1432 asection * splt; 1433 asection * sreloc; 1434 1435 dynobj = elf_hash_table (info)->dynobj; 1436 sgot = NULL; 1437 splt = NULL; 1438 sreloc = NULL; 1439 1440 switch (r_type) 1441 { 1442 case R_MN10300_24: 1443 case R_MN10300_16: 1444 case R_MN10300_8: 1445 case R_MN10300_PCREL8: 1446 case R_MN10300_PCREL16: 1447 case R_MN10300_PCREL32: 1448 case R_MN10300_GOTOFF32: 1449 case R_MN10300_GOTOFF24: 1450 case R_MN10300_GOTOFF16: 1451 if (bfd_link_pic (info) 1452 && (input_section->flags & SEC_ALLOC) != 0 1453 && h != NULL 1454 && ! SYMBOL_REFERENCES_LOCAL (info, h)) 1455 return bfd_reloc_dangerous; 1456 /* Fall through. */ 1457 case R_MN10300_GOT32: 1458 /* Issue 2052223: 1459 Taking the address of a protected function in a shared library 1460 is illegal. Issue an error message here. */ 1461 if (bfd_link_pic (info) 1462 && (input_section->flags & SEC_ALLOC) != 0 1463 && h != NULL 1464 && ELF_ST_VISIBILITY (h->other) == STV_PROTECTED 1465 && (h->type == STT_FUNC || h->type == STT_GNU_IFUNC) 1466 && ! SYMBOL_REFERENCES_LOCAL (info, h)) 1467 return bfd_reloc_dangerous; 1468 } 1469 1470 is_sym_diff_reloc = FALSE; 1471 if (sym_diff_section != NULL) 1472 { 1473 BFD_ASSERT (sym_diff_section == input_section); 1474 1475 switch (r_type) 1476 { 1477 case R_MN10300_32: 1478 case R_MN10300_24: 1479 case R_MN10300_16: 1480 case R_MN10300_8: 1481 value -= sym_diff_value; 1482 /* If we are computing a 32-bit value for the location lists 1483 and the result is 0 then we add one to the value. A zero 1484 value can result because of linker relaxation deleteing 1485 prologue instructions and using a value of 1 (for the begin 1486 and end offsets in the location list entry) results in a 1487 nul entry which does not prevent the following entries from 1488 being parsed. */ 1489 if (r_type == R_MN10300_32 1490 && value == 0 1491 && strcmp (input_section->name, ".debug_loc") == 0) 1492 value = 1; 1493 sym_diff_section = NULL; 1494 is_sym_diff_reloc = TRUE; 1495 break; 1496 1497 default: 1498 sym_diff_section = NULL; 1499 break; 1500 } 1501 } 1502 1503 switch (r_type) 1504 { 1505 case R_MN10300_SYM_DIFF: 1506 BFD_ASSERT (addend == 0); 1507 /* Cache the input section and value. 1508 The offset is unreliable, since relaxation may 1509 have reduced the following reloc's offset. */ 1510 sym_diff_section = input_section; 1511 sym_diff_value = value; 1512 return bfd_reloc_ok; 1513 1514 case R_MN10300_ALIGN: 1515 case R_MN10300_NONE: 1516 return bfd_reloc_ok; 1517 1518 case R_MN10300_32: 1519 if (bfd_link_pic (info) 1520 /* Do not generate relocs when an R_MN10300_32 has been used 1521 with an R_MN10300_SYM_DIFF to compute a difference of two 1522 symbols. */ 1523 && is_sym_diff_reloc == FALSE 1524 /* Also, do not generate a reloc when the symbol associated 1525 with the R_MN10300_32 reloc is absolute - there is no 1526 need for a run time computation in this case. */ 1527 && sym_sec != bfd_abs_section_ptr 1528 /* If the section is not going to be allocated at load time 1529 then there is no need to generate relocs for it. */ 1530 && (input_section->flags & SEC_ALLOC) != 0) 1531 { 1532 Elf_Internal_Rela outrel; 1533 bfd_boolean skip, relocate; 1534 1535 /* When generating a shared object, these relocations are 1536 copied into the output file to be resolved at run 1537 time. */ 1538 if (sreloc == NULL) 1539 { 1540 sreloc = _bfd_elf_get_dynamic_reloc_section 1541 (input_bfd, input_section, /*rela?*/ TRUE); 1542 if (sreloc == NULL) 1543 return FALSE; 1544 } 1545 1546 skip = FALSE; 1547 1548 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info, 1549 input_section, offset); 1550 if (outrel.r_offset == (bfd_vma) -1) 1551 skip = TRUE; 1552 1553 outrel.r_offset += (input_section->output_section->vma 1554 + input_section->output_offset); 1555 1556 if (skip) 1557 { 1558 memset (&outrel, 0, sizeof outrel); 1559 relocate = FALSE; 1560 } 1561 else 1562 { 1563 /* h->dynindx may be -1 if this symbol was marked to 1564 become local. */ 1565 if (h == NULL 1566 || SYMBOL_REFERENCES_LOCAL (info, h)) 1567 { 1568 relocate = TRUE; 1569 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE); 1570 outrel.r_addend = value + addend; 1571 } 1572 else 1573 { 1574 BFD_ASSERT (h->dynindx != -1); 1575 relocate = FALSE; 1576 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32); 1577 outrel.r_addend = value + addend; 1578 } 1579 } 1580 1581 bfd_elf32_swap_reloca_out (output_bfd, &outrel, 1582 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents) 1583 + sreloc->reloc_count)); 1584 ++sreloc->reloc_count; 1585 1586 /* If this reloc is against an external symbol, we do 1587 not want to fiddle with the addend. Otherwise, we 1588 need to include the symbol value so that it becomes 1589 an addend for the dynamic reloc. */ 1590 if (! relocate) 1591 return bfd_reloc_ok; 1592 } 1593 value += addend; 1594 bfd_put_32 (input_bfd, value, hit_data); 1595 return bfd_reloc_ok; 1596 1597 case R_MN10300_24: 1598 value += addend; 1599 1600 if ((long) value > 0x7fffff || (long) value < -0x800000) 1601 return bfd_reloc_overflow; 1602 1603 bfd_put_8 (input_bfd, value & 0xff, hit_data); 1604 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1); 1605 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2); 1606 return bfd_reloc_ok; 1607 1608 case R_MN10300_16: 1609 value += addend; 1610 1611 if ((long) value > 0x7fff || (long) value < -0x8000) 1612 return bfd_reloc_overflow; 1613 1614 bfd_put_16 (input_bfd, value, hit_data); 1615 return bfd_reloc_ok; 1616 1617 case R_MN10300_8: 1618 value += addend; 1619 1620 if ((long) value > 0x7f || (long) value < -0x80) 1621 return bfd_reloc_overflow; 1622 1623 bfd_put_8 (input_bfd, value, hit_data); 1624 return bfd_reloc_ok; 1625 1626 case R_MN10300_PCREL8: 1627 value -= (input_section->output_section->vma 1628 + input_section->output_offset); 1629 value -= offset; 1630 value += addend; 1631 1632 if ((long) value > 0x7f || (long) value < -0x80) 1633 return bfd_reloc_overflow; 1634 1635 bfd_put_8 (input_bfd, value, hit_data); 1636 return bfd_reloc_ok; 1637 1638 case R_MN10300_PCREL16: 1639 value -= (input_section->output_section->vma 1640 + input_section->output_offset); 1641 value -= offset; 1642 value += addend; 1643 1644 if ((long) value > 0x7fff || (long) value < -0x8000) 1645 return bfd_reloc_overflow; 1646 1647 bfd_put_16 (input_bfd, value, hit_data); 1648 return bfd_reloc_ok; 1649 1650 case R_MN10300_PCREL32: 1651 value -= (input_section->output_section->vma 1652 + input_section->output_offset); 1653 value -= offset; 1654 value += addend; 1655 1656 bfd_put_32 (input_bfd, value, hit_data); 1657 return bfd_reloc_ok; 1658 1659 case R_MN10300_GNU_VTINHERIT: 1660 case R_MN10300_GNU_VTENTRY: 1661 return bfd_reloc_ok; 1662 1663 case R_MN10300_GOTPC32: 1664 if (dynobj == NULL) 1665 return bfd_reloc_dangerous; 1666 1667 /* Use global offset table as symbol value. */ 1668 value = htab->root.sgot->output_section->vma; 1669 value -= (input_section->output_section->vma 1670 + input_section->output_offset); 1671 value -= offset; 1672 value += addend; 1673 1674 bfd_put_32 (input_bfd, value, hit_data); 1675 return bfd_reloc_ok; 1676 1677 case R_MN10300_GOTPC16: 1678 if (dynobj == NULL) 1679 return bfd_reloc_dangerous; 1680 1681 /* Use global offset table as symbol value. */ 1682 value = htab->root.sgot->output_section->vma; 1683 value -= (input_section->output_section->vma 1684 + input_section->output_offset); 1685 value -= offset; 1686 value += addend; 1687 1688 if ((long) value > 0x7fff || (long) value < -0x8000) 1689 return bfd_reloc_overflow; 1690 1691 bfd_put_16 (input_bfd, value, hit_data); 1692 return bfd_reloc_ok; 1693 1694 case R_MN10300_GOTOFF32: 1695 if (dynobj == NULL) 1696 return bfd_reloc_dangerous; 1697 1698 value -= htab->root.sgot->output_section->vma; 1699 value += addend; 1700 1701 bfd_put_32 (input_bfd, value, hit_data); 1702 return bfd_reloc_ok; 1703 1704 case R_MN10300_GOTOFF24: 1705 if (dynobj == NULL) 1706 return bfd_reloc_dangerous; 1707 1708 value -= htab->root.sgot->output_section->vma; 1709 value += addend; 1710 1711 if ((long) value > 0x7fffff || (long) value < -0x800000) 1712 return bfd_reloc_overflow; 1713 1714 bfd_put_8 (input_bfd, value, hit_data); 1715 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1); 1716 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2); 1717 return bfd_reloc_ok; 1718 1719 case R_MN10300_GOTOFF16: 1720 if (dynobj == NULL) 1721 return bfd_reloc_dangerous; 1722 1723 value -= htab->root.sgot->output_section->vma; 1724 value += addend; 1725 1726 if ((long) value > 0x7fff || (long) value < -0x8000) 1727 return bfd_reloc_overflow; 1728 1729 bfd_put_16 (input_bfd, value, hit_data); 1730 return bfd_reloc_ok; 1731 1732 case R_MN10300_PLT32: 1733 if (h != NULL 1734 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 1735 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN 1736 && h->plt.offset != (bfd_vma) -1) 1737 { 1738 if (dynobj == NULL) 1739 return bfd_reloc_dangerous; 1740 1741 splt = htab->root.splt; 1742 value = (splt->output_section->vma 1743 + splt->output_offset 1744 + h->plt.offset) - value; 1745 } 1746 1747 value -= (input_section->output_section->vma 1748 + input_section->output_offset); 1749 value -= offset; 1750 value += addend; 1751 1752 bfd_put_32 (input_bfd, value, hit_data); 1753 return bfd_reloc_ok; 1754 1755 case R_MN10300_PLT16: 1756 if (h != NULL 1757 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL 1758 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN 1759 && h->plt.offset != (bfd_vma) -1) 1760 { 1761 if (dynobj == NULL) 1762 return bfd_reloc_dangerous; 1763 1764 splt = htab->root.splt; 1765 value = (splt->output_section->vma 1766 + splt->output_offset 1767 + h->plt.offset) - value; 1768 } 1769 1770 value -= (input_section->output_section->vma 1771 + input_section->output_offset); 1772 value -= offset; 1773 value += addend; 1774 1775 if ((long) value > 0x7fff || (long) value < -0x8000) 1776 return bfd_reloc_overflow; 1777 1778 bfd_put_16 (input_bfd, value, hit_data); 1779 return bfd_reloc_ok; 1780 1781 case R_MN10300_TLS_LDO: 1782 value = dtpoff (info, value); 1783 bfd_put_32 (input_bfd, value + addend, hit_data); 1784 return bfd_reloc_ok; 1785 1786 case R_MN10300_TLS_LE: 1787 value = tpoff (info, value); 1788 bfd_put_32 (input_bfd, value + addend, hit_data); 1789 return bfd_reloc_ok; 1790 1791 case R_MN10300_TLS_LD: 1792 if (dynobj == NULL) 1793 return bfd_reloc_dangerous; 1794 1795 sgot = htab->root.sgot; 1796 BFD_ASSERT (sgot != NULL); 1797 value = htab->tls_ldm_got.offset + sgot->output_offset; 1798 bfd_put_32 (input_bfd, value, hit_data); 1799 1800 if (!htab->tls_ldm_got.rel_emitted) 1801 { 1802 asection *srelgot = htab->root.srelgot; 1803 Elf_Internal_Rela rel; 1804 1805 BFD_ASSERT (srelgot != NULL); 1806 htab->tls_ldm_got.rel_emitted ++; 1807 rel.r_offset = (sgot->output_section->vma 1808 + sgot->output_offset 1809 + htab->tls_ldm_got.offset); 1810 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + htab->tls_ldm_got.offset); 1811 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + htab->tls_ldm_got.offset+4); 1812 rel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPMOD); 1813 rel.r_addend = 0; 1814 bfd_elf32_swap_reloca_out (output_bfd, & rel, 1815 (bfd_byte *) ((Elf32_External_Rela *) srelgot->contents 1816 + srelgot->reloc_count)); 1817 ++ srelgot->reloc_count; 1818 } 1819 1820 return bfd_reloc_ok; 1821 1822 case R_MN10300_TLS_GOTIE: 1823 value = tpoff (info, value); 1824 /* Fall Through. */ 1825 1826 case R_MN10300_TLS_GD: 1827 case R_MN10300_TLS_IE: 1828 case R_MN10300_GOT32: 1829 case R_MN10300_GOT24: 1830 case R_MN10300_GOT16: 1831 if (dynobj == NULL) 1832 return bfd_reloc_dangerous; 1833 1834 sgot = htab->root.sgot; 1835 if (r_type == R_MN10300_TLS_GD) 1836 value = dtpoff (info, value); 1837 1838 if (h != NULL) 1839 { 1840 bfd_vma off; 1841 1842 off = h->got.offset; 1843 /* Offsets in the GOT are allocated in check_relocs 1844 which is not called for shared libraries... */ 1845 if (off == (bfd_vma) -1) 1846 off = 0; 1847 1848 if (sgot->contents != NULL 1849 && (! elf_hash_table (info)->dynamic_sections_created 1850 || SYMBOL_REFERENCES_LOCAL (info, h))) 1851 /* This is actually a static link, or it is a 1852 -Bsymbolic link and the symbol is defined 1853 locally, or the symbol was forced to be local 1854 because of a version file. We must initialize 1855 this entry in the global offset table. 1856 1857 When doing a dynamic link, we create a .rela.got 1858 relocation entry to initialize the value. This 1859 is done in the finish_dynamic_symbol routine. */ 1860 bfd_put_32 (output_bfd, value, 1861 sgot->contents + off); 1862 1863 value = sgot->output_offset + off; 1864 } 1865 else 1866 { 1867 bfd_vma off; 1868 1869 off = elf_local_got_offsets (input_bfd)[symndx]; 1870 1871 if (off & 1) 1872 bfd_put_32 (output_bfd, value, sgot->contents + (off & ~ 1)); 1873 else 1874 { 1875 bfd_put_32 (output_bfd, value, sgot->contents + off); 1876 1877 if (bfd_link_pic (info)) 1878 { 1879 asection *srelgot = htab->root.srelgot;; 1880 Elf_Internal_Rela outrel; 1881 1882 BFD_ASSERT (srelgot != NULL); 1883 1884 outrel.r_offset = (sgot->output_section->vma 1885 + sgot->output_offset 1886 + off); 1887 switch (r_type) 1888 { 1889 case R_MN10300_TLS_GD: 1890 outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPOFF); 1891 outrel.r_offset = (sgot->output_section->vma 1892 + sgot->output_offset 1893 + off + 4); 1894 bfd_elf32_swap_reloca_out (output_bfd, & outrel, 1895 (bfd_byte *) (((Elf32_External_Rela *) 1896 srelgot->contents) 1897 + srelgot->reloc_count)); 1898 ++ srelgot->reloc_count; 1899 outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_DTPMOD); 1900 break; 1901 case R_MN10300_TLS_GOTIE: 1902 case R_MN10300_TLS_IE: 1903 outrel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_TPOFF); 1904 break; 1905 default: 1906 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE); 1907 break; 1908 } 1909 1910 outrel.r_addend = value; 1911 bfd_elf32_swap_reloca_out (output_bfd, &outrel, 1912 (bfd_byte *) (((Elf32_External_Rela *) 1913 srelgot->contents) 1914 + srelgot->reloc_count)); 1915 ++ srelgot->reloc_count; 1916 elf_local_got_offsets (input_bfd)[symndx] |= 1; 1917 } 1918 1919 value = sgot->output_offset + (off & ~(bfd_vma) 1); 1920 } 1921 } 1922 1923 value += addend; 1924 1925 if (r_type == R_MN10300_TLS_IE) 1926 { 1927 value += sgot->output_section->vma; 1928 bfd_put_32 (input_bfd, value, hit_data); 1929 return bfd_reloc_ok; 1930 } 1931 else if (r_type == R_MN10300_TLS_GOTIE 1932 || r_type == R_MN10300_TLS_GD 1933 || r_type == R_MN10300_TLS_LD) 1934 { 1935 bfd_put_32 (input_bfd, value, hit_data); 1936 return bfd_reloc_ok; 1937 } 1938 else if (r_type == R_MN10300_GOT32) 1939 { 1940 bfd_put_32 (input_bfd, value, hit_data); 1941 return bfd_reloc_ok; 1942 } 1943 else if (r_type == R_MN10300_GOT24) 1944 { 1945 if ((long) value > 0x7fffff || (long) value < -0x800000) 1946 return bfd_reloc_overflow; 1947 1948 bfd_put_8 (input_bfd, value & 0xff, hit_data); 1949 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1); 1950 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2); 1951 return bfd_reloc_ok; 1952 } 1953 else if (r_type == R_MN10300_GOT16) 1954 { 1955 if ((long) value > 0x7fff || (long) value < -0x8000) 1956 return bfd_reloc_overflow; 1957 1958 bfd_put_16 (input_bfd, value, hit_data); 1959 return bfd_reloc_ok; 1960 } 1961 /* Fall through. */ 1962 1963 default: 1964 return bfd_reloc_notsupported; 1965 } 1966} 1967 1968/* Relocate an MN10300 ELF section. */ 1969 1970static bfd_boolean 1971mn10300_elf_relocate_section (bfd *output_bfd, 1972 struct bfd_link_info *info, 1973 bfd *input_bfd, 1974 asection *input_section, 1975 bfd_byte *contents, 1976 Elf_Internal_Rela *relocs, 1977 Elf_Internal_Sym *local_syms, 1978 asection **local_sections) 1979{ 1980 Elf_Internal_Shdr *symtab_hdr; 1981 struct elf_link_hash_entry **sym_hashes; 1982 Elf_Internal_Rela *rel, *relend; 1983 Elf_Internal_Rela * trel; 1984 1985 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 1986 sym_hashes = elf_sym_hashes (input_bfd); 1987 1988 rel = relocs; 1989 relend = relocs + input_section->reloc_count; 1990 for (; rel < relend; rel++) 1991 { 1992 int r_type; 1993 reloc_howto_type *howto; 1994 unsigned long r_symndx; 1995 Elf_Internal_Sym *sym; 1996 asection *sec; 1997 struct elf32_mn10300_link_hash_entry *h; 1998 bfd_vma relocation; 1999 bfd_reloc_status_type r; 2000 int tls_r_type; 2001 bfd_boolean unresolved_reloc = FALSE; 2002 bfd_boolean warned, ignored; 2003 struct elf_link_hash_entry * hh; 2004 2005 relocation = 0; 2006 r_symndx = ELF32_R_SYM (rel->r_info); 2007 r_type = ELF32_R_TYPE (rel->r_info); 2008 howto = elf_mn10300_howto_table + r_type; 2009 2010 /* Just skip the vtable gc relocs. */ 2011 if (r_type == R_MN10300_GNU_VTINHERIT 2012 || r_type == R_MN10300_GNU_VTENTRY) 2013 continue; 2014 2015 h = NULL; 2016 sym = NULL; 2017 sec = NULL; 2018 if (r_symndx < symtab_hdr->sh_info) 2019 hh = NULL; 2020 else 2021 { 2022 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 2023 r_symndx, symtab_hdr, sym_hashes, 2024 hh, sec, relocation, 2025 unresolved_reloc, warned, ignored); 2026 } 2027 h = elf_mn10300_hash_entry (hh); 2028 2029 tls_r_type = elf_mn10300_tls_transition (info, r_type, hh, input_section, 0); 2030 if (tls_r_type != r_type) 2031 { 2032 bfd_boolean had_plt; 2033 2034 had_plt = mn10300_do_tls_transition (input_bfd, r_type, tls_r_type, 2035 contents, rel->r_offset); 2036 r_type = tls_r_type; 2037 howto = elf_mn10300_howto_table + r_type; 2038 2039 if (had_plt) 2040 for (trel = rel+1; trel < relend; trel++) 2041 if ((ELF32_R_TYPE (trel->r_info) == R_MN10300_PLT32 2042 || ELF32_R_TYPE (trel->r_info) == R_MN10300_PCREL32) 2043 && rel->r_offset + had_plt == trel->r_offset) 2044 trel->r_info = ELF32_R_INFO (0, R_MN10300_NONE); 2045 } 2046 2047 if (r_symndx < symtab_hdr->sh_info) 2048 { 2049 sym = local_syms + r_symndx; 2050 sec = local_sections[r_symndx]; 2051 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 2052 } 2053 else 2054 { 2055 if ((h->root.root.type == bfd_link_hash_defined 2056 || h->root.root.type == bfd_link_hash_defweak) 2057 && ( r_type == R_MN10300_GOTPC32 2058 || r_type == R_MN10300_GOTPC16 2059 || (( r_type == R_MN10300_PLT32 2060 || r_type == R_MN10300_PLT16) 2061 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL 2062 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN 2063 && h->root.plt.offset != (bfd_vma) -1) 2064 || (( r_type == R_MN10300_GOT32 2065 || r_type == R_MN10300_GOT24 2066 || r_type == R_MN10300_TLS_GD 2067 || r_type == R_MN10300_TLS_LD 2068 || r_type == R_MN10300_TLS_GOTIE 2069 || r_type == R_MN10300_TLS_IE 2070 || r_type == R_MN10300_GOT16) 2071 && elf_hash_table (info)->dynamic_sections_created 2072 && !SYMBOL_REFERENCES_LOCAL (info, hh)) 2073 || (r_type == R_MN10300_32 2074 /* _32 relocs in executables force _COPY relocs, 2075 such that the address of the symbol ends up 2076 being local. */ 2077 && !bfd_link_executable (info) 2078 && !SYMBOL_REFERENCES_LOCAL (info, hh) 2079 && ((input_section->flags & SEC_ALLOC) != 0 2080 /* DWARF will emit R_MN10300_32 relocations 2081 in its sections against symbols defined 2082 externally in shared libraries. We can't 2083 do anything with them here. */ 2084 || ((input_section->flags & SEC_DEBUGGING) != 0 2085 && h->root.def_dynamic))))) 2086 /* In these cases, we don't need the relocation 2087 value. We check specially because in some 2088 obscure cases sec->output_section will be NULL. */ 2089 relocation = 0; 2090 2091 else if (!bfd_link_relocatable (info) && unresolved_reloc 2092 && _bfd_elf_section_offset (output_bfd, info, input_section, 2093 rel->r_offset) != (bfd_vma) -1) 2094 2095 _bfd_error_handler 2096 /* xgettext:c-format */ 2097 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"), 2098 input_bfd, 2099 input_section, 2100 (long) rel->r_offset, 2101 howto->name, 2102 h->root.root.root.string); 2103 } 2104 2105 if (sec != NULL && discarded_section (sec)) 2106 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, 2107 rel, 1, relend, howto, 0, contents); 2108 2109 if (bfd_link_relocatable (info)) 2110 continue; 2111 2112 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd, 2113 input_section, 2114 contents, rel->r_offset, 2115 relocation, rel->r_addend, 2116 (struct elf_link_hash_entry *) h, 2117 r_symndx, 2118 info, sec, h == NULL); 2119 2120 if (r != bfd_reloc_ok) 2121 { 2122 const char *name; 2123 const char *msg = NULL; 2124 2125 if (h != NULL) 2126 name = h->root.root.root.string; 2127 else 2128 { 2129 name = (bfd_elf_string_from_elf_section 2130 (input_bfd, symtab_hdr->sh_link, sym->st_name)); 2131 if (name == NULL || *name == '\0') 2132 name = bfd_section_name (input_bfd, sec); 2133 } 2134 2135 switch (r) 2136 { 2137 case bfd_reloc_overflow: 2138 (*info->callbacks->reloc_overflow) 2139 (info, (h ? &h->root.root : NULL), name, howto->name, 2140 (bfd_vma) 0, input_bfd, input_section, rel->r_offset); 2141 break; 2142 2143 case bfd_reloc_undefined: 2144 (*info->callbacks->undefined_symbol) 2145 (info, name, input_bfd, input_section, rel->r_offset, TRUE); 2146 break; 2147 2148 case bfd_reloc_outofrange: 2149 msg = _("internal error: out of range error"); 2150 goto common_error; 2151 2152 case bfd_reloc_notsupported: 2153 msg = _("internal error: unsupported relocation error"); 2154 goto common_error; 2155 2156 case bfd_reloc_dangerous: 2157 if (r_type == R_MN10300_PCREL32) 2158 msg = _("error: inappropriate relocation type for shared" 2159 " library (did you forget -fpic?)"); 2160 else if (r_type == R_MN10300_GOT32) 2161 /* xgettext:c-format */ 2162 msg = _("%B: taking the address of protected function" 2163 " '%s' cannot be done when making a shared library"); 2164 else 2165 msg = _("internal error: suspicious relocation type used" 2166 " in shared library"); 2167 goto common_error; 2168 2169 default: 2170 msg = _("internal error: unknown error"); 2171 /* Fall through. */ 2172 2173 common_error: 2174 _bfd_error_handler (msg, input_bfd, name); 2175 bfd_set_error (bfd_error_bad_value); 2176 return FALSE; 2177 } 2178 } 2179 } 2180 2181 return TRUE; 2182} 2183 2184/* Finish initializing one hash table entry. */ 2185 2186static bfd_boolean 2187elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry, 2188 void * in_args) 2189{ 2190 struct elf32_mn10300_link_hash_entry *entry; 2191 struct bfd_link_info *link_info = (struct bfd_link_info *) in_args; 2192 unsigned int byte_count = 0; 2193 2194 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry; 2195 2196 /* If we already know we want to convert "call" to "calls" for calls 2197 to this symbol, then return now. */ 2198 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS) 2199 return TRUE; 2200 2201 /* If there are no named calls to this symbol, or there's nothing we 2202 can move from the function itself into the "call" instruction, 2203 then note that all "call" instructions should be converted into 2204 "calls" instructions and return. If a symbol is available for 2205 dynamic symbol resolution (overridable or overriding), avoid 2206 custom calling conventions. */ 2207 if (entry->direct_calls == 0 2208 || (entry->stack_size == 0 && entry->movm_args == 0) 2209 || (elf_hash_table (link_info)->dynamic_sections_created 2210 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL 2211 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN)) 2212 { 2213 /* Make a note that we should convert "call" instructions to "calls" 2214 instructions for calls to this symbol. */ 2215 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS; 2216 return TRUE; 2217 } 2218 2219 /* We may be able to move some instructions from the function itself into 2220 the "call" instruction. Count how many bytes we might be able to 2221 eliminate in the function itself. */ 2222 2223 /* A movm instruction is two bytes. */ 2224 if (entry->movm_args) 2225 byte_count += 2; 2226 2227 /* Count the insn to allocate stack space too. */ 2228 if (entry->stack_size > 0) 2229 { 2230 if (entry->stack_size <= 128) 2231 byte_count += 3; 2232 else 2233 byte_count += 4; 2234 } 2235 2236 /* If using "call" will result in larger code, then turn all 2237 the associated "call" instructions into "calls" instructions. */ 2238 if (byte_count < entry->direct_calls) 2239 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS; 2240 2241 /* This routine never fails. */ 2242 return TRUE; 2243} 2244 2245/* Used to count hash table entries. */ 2246 2247static bfd_boolean 2248elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED, 2249 void * in_args) 2250{ 2251 int *count = (int *) in_args; 2252 2253 (*count) ++; 2254 return TRUE; 2255} 2256 2257/* Used to enumerate hash table entries into a linear array. */ 2258 2259static bfd_boolean 2260elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry, 2261 void * in_args) 2262{ 2263 struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args; 2264 2265 **ptr = gen_entry; 2266 (*ptr) ++; 2267 return TRUE; 2268} 2269 2270/* Used to sort the array created by the above. */ 2271 2272static int 2273sort_by_value (const void *va, const void *vb) 2274{ 2275 struct elf32_mn10300_link_hash_entry *a 2276 = *(struct elf32_mn10300_link_hash_entry **) va; 2277 struct elf32_mn10300_link_hash_entry *b 2278 = *(struct elf32_mn10300_link_hash_entry **) vb; 2279 2280 return a->value - b->value; 2281} 2282 2283/* Compute the stack size and movm arguments for the function 2284 referred to by HASH at address ADDR in section with 2285 contents CONTENTS, store the information in the hash table. */ 2286 2287static void 2288compute_function_info (bfd *abfd, 2289 struct elf32_mn10300_link_hash_entry *hash, 2290 bfd_vma addr, 2291 unsigned char *contents) 2292{ 2293 unsigned char byte1, byte2; 2294 /* We only care about a very small subset of the possible prologue 2295 sequences here. Basically we look for: 2296 2297 movm [d2,d3,a2,a3],sp (optional) 2298 add <size>,sp (optional, and only for sizes which fit in an unsigned 2299 8 bit number) 2300 2301 If we find anything else, we quit. */ 2302 2303 /* Look for movm [regs],sp. */ 2304 byte1 = bfd_get_8 (abfd, contents + addr); 2305 byte2 = bfd_get_8 (abfd, contents + addr + 1); 2306 2307 if (byte1 == 0xcf) 2308 { 2309 hash->movm_args = byte2; 2310 addr += 2; 2311 byte1 = bfd_get_8 (abfd, contents + addr); 2312 byte2 = bfd_get_8 (abfd, contents + addr + 1); 2313 } 2314 2315 /* Now figure out how much stack space will be allocated by the movm 2316 instruction. We need this kept separate from the function's normal 2317 stack space. */ 2318 if (hash->movm_args) 2319 { 2320 /* Space for d2. */ 2321 if (hash->movm_args & 0x80) 2322 hash->movm_stack_size += 4; 2323 2324 /* Space for d3. */ 2325 if (hash->movm_args & 0x40) 2326 hash->movm_stack_size += 4; 2327 2328 /* Space for a2. */ 2329 if (hash->movm_args & 0x20) 2330 hash->movm_stack_size += 4; 2331 2332 /* Space for a3. */ 2333 if (hash->movm_args & 0x10) 2334 hash->movm_stack_size += 4; 2335 2336 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */ 2337 if (hash->movm_args & 0x08) 2338 hash->movm_stack_size += 8 * 4; 2339 2340 if (bfd_get_mach (abfd) == bfd_mach_am33 2341 || bfd_get_mach (abfd) == bfd_mach_am33_2) 2342 { 2343 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */ 2344 if (hash->movm_args & 0x1) 2345 hash->movm_stack_size += 6 * 4; 2346 2347 /* exreg1 space. e4, e5, e6, e7 */ 2348 if (hash->movm_args & 0x2) 2349 hash->movm_stack_size += 4 * 4; 2350 2351 /* exreg0 space. e2, e3 */ 2352 if (hash->movm_args & 0x4) 2353 hash->movm_stack_size += 2 * 4; 2354 } 2355 } 2356 2357 /* Now look for the two stack adjustment variants. */ 2358 if (byte1 == 0xf8 && byte2 == 0xfe) 2359 { 2360 int temp = bfd_get_8 (abfd, contents + addr + 2); 2361 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80; 2362 2363 hash->stack_size = -temp; 2364 } 2365 else if (byte1 == 0xfa && byte2 == 0xfe) 2366 { 2367 int temp = bfd_get_16 (abfd, contents + addr + 2); 2368 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000; 2369 temp = -temp; 2370 2371 if (temp < 255) 2372 hash->stack_size = temp; 2373 } 2374 2375 /* If the total stack to be allocated by the call instruction is more 2376 than 255 bytes, then we can't remove the stack adjustment by using 2377 "call" (we might still be able to remove the "movm" instruction. */ 2378 if (hash->stack_size + hash->movm_stack_size > 255) 2379 hash->stack_size = 0; 2380} 2381 2382/* Delete some bytes from a section while relaxing. */ 2383 2384static bfd_boolean 2385mn10300_elf_relax_delete_bytes (bfd *abfd, 2386 asection *sec, 2387 bfd_vma addr, 2388 int count) 2389{ 2390 Elf_Internal_Shdr *symtab_hdr; 2391 unsigned int sec_shndx; 2392 bfd_byte *contents; 2393 Elf_Internal_Rela *irel, *irelend; 2394 Elf_Internal_Rela *irelalign; 2395 bfd_vma toaddr; 2396 Elf_Internal_Sym *isym, *isymend; 2397 struct elf_link_hash_entry **sym_hashes; 2398 struct elf_link_hash_entry **end_hashes; 2399 unsigned int symcount; 2400 2401 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 2402 2403 contents = elf_section_data (sec)->this_hdr.contents; 2404 2405 irelalign = NULL; 2406 toaddr = sec->size; 2407 2408 irel = elf_section_data (sec)->relocs; 2409 irelend = irel + sec->reloc_count; 2410 2411 if (sec->reloc_count > 0) 2412 { 2413 /* If there is an align reloc at the end of the section ignore it. 2414 GAS creates these relocs for reasons of its own, and they just 2415 serve to keep the section artifically inflated. */ 2416 if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN) 2417 --irelend; 2418 2419 /* The deletion must stop at the next ALIGN reloc for an aligment 2420 power larger than, or not a multiple of, the number of bytes we 2421 are deleting. */ 2422 for (; irel < irelend; irel++) 2423 { 2424 int alignment = 1 << irel->r_addend; 2425 2426 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN 2427 && irel->r_offset > addr 2428 && irel->r_offset < toaddr 2429 && (count < alignment 2430 || alignment % count != 0)) 2431 { 2432 irelalign = irel; 2433 toaddr = irel->r_offset; 2434 break; 2435 } 2436 } 2437 } 2438 2439 /* Actually delete the bytes. */ 2440 memmove (contents + addr, contents + addr + count, 2441 (size_t) (toaddr - addr - count)); 2442 2443 /* Adjust the section's size if we are shrinking it, or else 2444 pad the bytes between the end of the shrunken region and 2445 the start of the next region with NOP codes. */ 2446 if (irelalign == NULL) 2447 { 2448 sec->size -= count; 2449 /* Include symbols at the end of the section, but 2450 not at the end of a sub-region of the section. */ 2451 toaddr ++; 2452 } 2453 else 2454 { 2455 int i; 2456 2457#define NOP_OPCODE 0xcb 2458 2459 for (i = 0; i < count; i ++) 2460 bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i); 2461 } 2462 2463 /* Adjust all the relocs. */ 2464 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) 2465 { 2466 /* Get the new reloc address. */ 2467 if ((irel->r_offset > addr 2468 && irel->r_offset < toaddr) 2469 || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN 2470 && irel->r_offset == toaddr)) 2471 irel->r_offset -= count; 2472 } 2473 2474 /* Adjust the local symbols in the section, reducing their value 2475 by the number of bytes deleted. Note - symbols within the deleted 2476 region are moved to the address of the start of the region, which 2477 actually means that they will address the byte beyond the end of 2478 the region once the deletion has been completed. */ 2479 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2480 isym = (Elf_Internal_Sym *) symtab_hdr->contents; 2481 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++) 2482 { 2483 if (isym->st_shndx == sec_shndx 2484 && isym->st_value > addr 2485 && isym->st_value < toaddr) 2486 { 2487 if (isym->st_value < addr + count) 2488 isym->st_value = addr; 2489 else 2490 isym->st_value -= count; 2491 } 2492 /* Adjust the function symbol's size as well. */ 2493 else if (isym->st_shndx == sec_shndx 2494 && ELF_ST_TYPE (isym->st_info) == STT_FUNC 2495 && isym->st_value + isym->st_size > addr 2496 && isym->st_value + isym->st_size < toaddr) 2497 isym->st_size -= count; 2498 } 2499 2500 /* Now adjust the global symbols defined in this section. */ 2501 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 2502 - symtab_hdr->sh_info); 2503 sym_hashes = elf_sym_hashes (abfd); 2504 end_hashes = sym_hashes + symcount; 2505 for (; sym_hashes < end_hashes; sym_hashes++) 2506 { 2507 struct elf_link_hash_entry *sym_hash = *sym_hashes; 2508 2509 if ((sym_hash->root.type == bfd_link_hash_defined 2510 || sym_hash->root.type == bfd_link_hash_defweak) 2511 && sym_hash->root.u.def.section == sec 2512 && sym_hash->root.u.def.value > addr 2513 && sym_hash->root.u.def.value < toaddr) 2514 { 2515 if (sym_hash->root.u.def.value < addr + count) 2516 sym_hash->root.u.def.value = addr; 2517 else 2518 sym_hash->root.u.def.value -= count; 2519 } 2520 /* Adjust the function symbol's size as well. */ 2521 else if (sym_hash->root.type == bfd_link_hash_defined 2522 && sym_hash->root.u.def.section == sec 2523 && sym_hash->type == STT_FUNC 2524 && sym_hash->root.u.def.value + sym_hash->size > addr 2525 && sym_hash->root.u.def.value + sym_hash->size < toaddr) 2526 sym_hash->size -= count; 2527 } 2528 2529 /* See if we can move the ALIGN reloc forward. 2530 We have adjusted r_offset for it already. */ 2531 if (irelalign != NULL) 2532 { 2533 bfd_vma alignto, alignaddr; 2534 2535 if ((int) irelalign->r_addend > 0) 2536 { 2537 /* This is the old address. */ 2538 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend); 2539 /* This is where the align points to now. */ 2540 alignaddr = BFD_ALIGN (irelalign->r_offset, 2541 1 << irelalign->r_addend); 2542 if (alignaddr < alignto) 2543 /* Tail recursion. */ 2544 return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr, 2545 (int) (alignto - alignaddr)); 2546 } 2547 } 2548 2549 return TRUE; 2550} 2551 2552/* Return TRUE if a symbol exists at the given address, else return 2553 FALSE. */ 2554 2555static bfd_boolean 2556mn10300_elf_symbol_address_p (bfd *abfd, 2557 asection *sec, 2558 Elf_Internal_Sym *isym, 2559 bfd_vma addr) 2560{ 2561 Elf_Internal_Shdr *symtab_hdr; 2562 unsigned int sec_shndx; 2563 Elf_Internal_Sym *isymend; 2564 struct elf_link_hash_entry **sym_hashes; 2565 struct elf_link_hash_entry **end_hashes; 2566 unsigned int symcount; 2567 2568 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 2569 2570 /* Examine all the symbols. */ 2571 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2572 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++) 2573 if (isym->st_shndx == sec_shndx 2574 && isym->st_value == addr) 2575 return TRUE; 2576 2577 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 2578 - symtab_hdr->sh_info); 2579 sym_hashes = elf_sym_hashes (abfd); 2580 end_hashes = sym_hashes + symcount; 2581 for (; sym_hashes < end_hashes; sym_hashes++) 2582 { 2583 struct elf_link_hash_entry *sym_hash = *sym_hashes; 2584 2585 if ((sym_hash->root.type == bfd_link_hash_defined 2586 || sym_hash->root.type == bfd_link_hash_defweak) 2587 && sym_hash->root.u.def.section == sec 2588 && sym_hash->root.u.def.value == addr) 2589 return TRUE; 2590 } 2591 2592 return FALSE; 2593} 2594 2595/* This function handles relaxing for the mn10300. 2596 2597 There are quite a few relaxing opportunities available on the mn10300: 2598 2599 * calls:32 -> calls:16 2 bytes 2600 * call:32 -> call:16 2 bytes 2601 2602 * call:32 -> calls:32 1 byte 2603 * call:16 -> calls:16 1 byte 2604 * These are done anytime using "calls" would result 2605 in smaller code, or when necessary to preserve the 2606 meaning of the program. 2607 2608 * call:32 varies 2609 * call:16 2610 * In some circumstances we can move instructions 2611 from a function prologue into a "call" instruction. 2612 This is only done if the resulting code is no larger 2613 than the original code. 2614 2615 * jmp:32 -> jmp:16 2 bytes 2616 * jmp:16 -> bra:8 1 byte 2617 2618 * If the previous instruction is a conditional branch 2619 around the jump/bra, we may be able to reverse its condition 2620 and change its target to the jump's target. The jump/bra 2621 can then be deleted. 2 bytes 2622 2623 * mov abs32 -> mov abs16 1 or 2 bytes 2624 2625 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes 2626 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes 2627 2628 * Most instructions which accept d32 can relax to d16 1 or 2 bytes 2629 - Most instructions which accept d16 can relax to d8 1 or 2 bytes 2630 2631 We don't handle imm16->imm8 or d16->d8 as they're very rare 2632 and somewhat more difficult to support. */ 2633 2634static bfd_boolean 2635mn10300_elf_relax_section (bfd *abfd, 2636 asection *sec, 2637 struct bfd_link_info *link_info, 2638 bfd_boolean *again) 2639{ 2640 Elf_Internal_Shdr *symtab_hdr; 2641 Elf_Internal_Rela *internal_relocs = NULL; 2642 Elf_Internal_Rela *irel, *irelend; 2643 bfd_byte *contents = NULL; 2644 Elf_Internal_Sym *isymbuf = NULL; 2645 struct elf32_mn10300_link_hash_table *hash_table; 2646 asection *section = sec; 2647 bfd_vma align_gap_adjustment; 2648 2649 if (bfd_link_relocatable (link_info)) 2650 (*link_info->callbacks->einfo) 2651 (_("%P%F: --relax and -r may not be used together\n")); 2652 2653 /* Assume nothing changes. */ 2654 *again = FALSE; 2655 2656 /* We need a pointer to the mn10300 specific hash table. */ 2657 hash_table = elf32_mn10300_hash_table (link_info); 2658 if (hash_table == NULL) 2659 return FALSE; 2660 2661 /* Initialize fields in each hash table entry the first time through. */ 2662 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0) 2663 { 2664 bfd *input_bfd; 2665 2666 /* Iterate over all the input bfds. */ 2667 for (input_bfd = link_info->input_bfds; 2668 input_bfd != NULL; 2669 input_bfd = input_bfd->link.next) 2670 { 2671 /* We're going to need all the symbols for each bfd. */ 2672 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 2673 if (symtab_hdr->sh_info != 0) 2674 { 2675 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 2676 if (isymbuf == NULL) 2677 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 2678 symtab_hdr->sh_info, 0, 2679 NULL, NULL, NULL); 2680 if (isymbuf == NULL) 2681 goto error_return; 2682 } 2683 2684 /* Iterate over each section in this bfd. */ 2685 for (section = input_bfd->sections; 2686 section != NULL; 2687 section = section->next) 2688 { 2689 struct elf32_mn10300_link_hash_entry *hash; 2690 asection *sym_sec = NULL; 2691 const char *sym_name; 2692 char *new_name; 2693 2694 /* If there's nothing to do in this section, skip it. */ 2695 if (! ((section->flags & SEC_RELOC) != 0 2696 && section->reloc_count != 0)) 2697 continue; 2698 if ((section->flags & SEC_ALLOC) == 0) 2699 continue; 2700 2701 /* Get cached copy of section contents if it exists. */ 2702 if (elf_section_data (section)->this_hdr.contents != NULL) 2703 contents = elf_section_data (section)->this_hdr.contents; 2704 else if (section->size != 0) 2705 { 2706 /* Go get them off disk. */ 2707 if (!bfd_malloc_and_get_section (input_bfd, section, 2708 &contents)) 2709 goto error_return; 2710 } 2711 else 2712 contents = NULL; 2713 2714 /* If there aren't any relocs, then there's nothing to do. */ 2715 if ((section->flags & SEC_RELOC) != 0 2716 && section->reloc_count != 0) 2717 { 2718 /* Get a copy of the native relocations. */ 2719 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section, 2720 NULL, NULL, 2721 link_info->keep_memory); 2722 if (internal_relocs == NULL) 2723 goto error_return; 2724 2725 /* Now examine each relocation. */ 2726 irel = internal_relocs; 2727 irelend = irel + section->reloc_count; 2728 for (; irel < irelend; irel++) 2729 { 2730 long r_type; 2731 unsigned long r_index; 2732 unsigned char code; 2733 2734 r_type = ELF32_R_TYPE (irel->r_info); 2735 r_index = ELF32_R_SYM (irel->r_info); 2736 2737 if (r_type < 0 || r_type >= (int) R_MN10300_MAX) 2738 goto error_return; 2739 2740 /* We need the name and hash table entry of the target 2741 symbol! */ 2742 hash = NULL; 2743 sym_sec = NULL; 2744 2745 if (r_index < symtab_hdr->sh_info) 2746 { 2747 /* A local symbol. */ 2748 Elf_Internal_Sym *isym; 2749 struct elf_link_hash_table *elftab; 2750 bfd_size_type amt; 2751 2752 isym = isymbuf + r_index; 2753 if (isym->st_shndx == SHN_UNDEF) 2754 sym_sec = bfd_und_section_ptr; 2755 else if (isym->st_shndx == SHN_ABS) 2756 sym_sec = bfd_abs_section_ptr; 2757 else if (isym->st_shndx == SHN_COMMON) 2758 sym_sec = bfd_com_section_ptr; 2759 else 2760 sym_sec 2761 = bfd_section_from_elf_index (input_bfd, 2762 isym->st_shndx); 2763 2764 sym_name 2765 = bfd_elf_string_from_elf_section (input_bfd, 2766 (symtab_hdr 2767 ->sh_link), 2768 isym->st_name); 2769 2770 /* If it isn't a function, then we don't care 2771 about it. */ 2772 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC) 2773 continue; 2774 2775 /* Tack on an ID so we can uniquely identify this 2776 local symbol in the global hash table. */ 2777 amt = strlen (sym_name) + 10; 2778 new_name = bfd_malloc (amt); 2779 if (new_name == NULL) 2780 goto error_return; 2781 2782 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id); 2783 sym_name = new_name; 2784 2785 elftab = &hash_table->static_hash_table->root; 2786 hash = ((struct elf32_mn10300_link_hash_entry *) 2787 elf_link_hash_lookup (elftab, sym_name, 2788 TRUE, TRUE, FALSE)); 2789 free (new_name); 2790 } 2791 else 2792 { 2793 r_index -= symtab_hdr->sh_info; 2794 hash = (struct elf32_mn10300_link_hash_entry *) 2795 elf_sym_hashes (input_bfd)[r_index]; 2796 } 2797 2798 sym_name = hash->root.root.root.string; 2799 if ((section->flags & SEC_CODE) != 0) 2800 { 2801 /* If this is not a "call" instruction, then we 2802 should convert "call" instructions to "calls" 2803 instructions. */ 2804 code = bfd_get_8 (input_bfd, 2805 contents + irel->r_offset - 1); 2806 if (code != 0xdd && code != 0xcd) 2807 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS; 2808 } 2809 2810 /* If this is a jump/call, then bump the 2811 direct_calls counter. Else force "call" to 2812 "calls" conversions. */ 2813 if (r_type == R_MN10300_PCREL32 2814 || r_type == R_MN10300_PLT32 2815 || r_type == R_MN10300_PLT16 2816 || r_type == R_MN10300_PCREL16) 2817 hash->direct_calls++; 2818 else 2819 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS; 2820 } 2821 } 2822 2823 /* Now look at the actual contents to get the stack size, 2824 and a list of what registers were saved in the prologue 2825 (ie movm_args). */ 2826 if ((section->flags & SEC_CODE) != 0) 2827 { 2828 Elf_Internal_Sym *isym, *isymend; 2829 unsigned int sec_shndx; 2830 struct elf_link_hash_entry **hashes; 2831 struct elf_link_hash_entry **end_hashes; 2832 unsigned int symcount; 2833 2834 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd, 2835 section); 2836 2837 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 2838 - symtab_hdr->sh_info); 2839 hashes = elf_sym_hashes (input_bfd); 2840 end_hashes = hashes + symcount; 2841 2842 /* Look at each function defined in this section and 2843 update info for that function. */ 2844 isymend = isymbuf + symtab_hdr->sh_info; 2845 for (isym = isymbuf; isym < isymend; isym++) 2846 { 2847 if (isym->st_shndx == sec_shndx 2848 && ELF_ST_TYPE (isym->st_info) == STT_FUNC) 2849 { 2850 struct elf_link_hash_table *elftab; 2851 bfd_size_type amt; 2852 struct elf_link_hash_entry **lhashes = hashes; 2853 2854 /* Skip a local symbol if it aliases a 2855 global one. */ 2856 for (; lhashes < end_hashes; lhashes++) 2857 { 2858 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes; 2859 if ((hash->root.root.type == bfd_link_hash_defined 2860 || hash->root.root.type == bfd_link_hash_defweak) 2861 && hash->root.root.u.def.section == section 2862 && hash->root.type == STT_FUNC 2863 && hash->root.root.u.def.value == isym->st_value) 2864 break; 2865 } 2866 if (lhashes != end_hashes) 2867 continue; 2868 2869 if (isym->st_shndx == SHN_UNDEF) 2870 sym_sec = bfd_und_section_ptr; 2871 else if (isym->st_shndx == SHN_ABS) 2872 sym_sec = bfd_abs_section_ptr; 2873 else if (isym->st_shndx == SHN_COMMON) 2874 sym_sec = bfd_com_section_ptr; 2875 else 2876 sym_sec 2877 = bfd_section_from_elf_index (input_bfd, 2878 isym->st_shndx); 2879 2880 sym_name = (bfd_elf_string_from_elf_section 2881 (input_bfd, symtab_hdr->sh_link, 2882 isym->st_name)); 2883 2884 /* Tack on an ID so we can uniquely identify this 2885 local symbol in the global hash table. */ 2886 amt = strlen (sym_name) + 10; 2887 new_name = bfd_malloc (amt); 2888 if (new_name == NULL) 2889 goto error_return; 2890 2891 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id); 2892 sym_name = new_name; 2893 2894 elftab = &hash_table->static_hash_table->root; 2895 hash = ((struct elf32_mn10300_link_hash_entry *) 2896 elf_link_hash_lookup (elftab, sym_name, 2897 TRUE, TRUE, FALSE)); 2898 free (new_name); 2899 compute_function_info (input_bfd, hash, 2900 isym->st_value, contents); 2901 hash->value = isym->st_value; 2902 } 2903 } 2904 2905 for (; hashes < end_hashes; hashes++) 2906 { 2907 hash = (struct elf32_mn10300_link_hash_entry *) *hashes; 2908 if ((hash->root.root.type == bfd_link_hash_defined 2909 || hash->root.root.type == bfd_link_hash_defweak) 2910 && hash->root.root.u.def.section == section 2911 && hash->root.type == STT_FUNC) 2912 compute_function_info (input_bfd, hash, 2913 (hash)->root.root.u.def.value, 2914 contents); 2915 } 2916 } 2917 2918 /* Cache or free any memory we allocated for the relocs. */ 2919 if (internal_relocs != NULL 2920 && elf_section_data (section)->relocs != internal_relocs) 2921 free (internal_relocs); 2922 internal_relocs = NULL; 2923 2924 /* Cache or free any memory we allocated for the contents. */ 2925 if (contents != NULL 2926 && elf_section_data (section)->this_hdr.contents != contents) 2927 { 2928 if (! link_info->keep_memory) 2929 free (contents); 2930 else 2931 { 2932 /* Cache the section contents for elf_link_input_bfd. */ 2933 elf_section_data (section)->this_hdr.contents = contents; 2934 } 2935 } 2936 contents = NULL; 2937 } 2938 2939 /* Cache or free any memory we allocated for the symbols. */ 2940 if (isymbuf != NULL 2941 && symtab_hdr->contents != (unsigned char *) isymbuf) 2942 { 2943 if (! link_info->keep_memory) 2944 free (isymbuf); 2945 else 2946 { 2947 /* Cache the symbols for elf_link_input_bfd. */ 2948 symtab_hdr->contents = (unsigned char *) isymbuf; 2949 } 2950 } 2951 isymbuf = NULL; 2952 } 2953 2954 /* Now iterate on each symbol in the hash table and perform 2955 the final initialization steps on each. */ 2956 elf32_mn10300_link_hash_traverse (hash_table, 2957 elf32_mn10300_finish_hash_table_entry, 2958 link_info); 2959 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table, 2960 elf32_mn10300_finish_hash_table_entry, 2961 link_info); 2962 2963 { 2964 /* This section of code collects all our local symbols, sorts 2965 them by value, and looks for multiple symbols referring to 2966 the same address. For those symbols, the flags are merged. 2967 At this point, the only flag that can be set is 2968 MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags 2969 together. */ 2970 int static_count = 0, i; 2971 struct elf32_mn10300_link_hash_entry **entries; 2972 struct elf32_mn10300_link_hash_entry **ptr; 2973 2974 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table, 2975 elf32_mn10300_count_hash_table_entries, 2976 &static_count); 2977 2978 entries = bfd_malloc (static_count * sizeof (* ptr)); 2979 2980 ptr = entries; 2981 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table, 2982 elf32_mn10300_list_hash_table_entries, 2983 & ptr); 2984 2985 qsort (entries, static_count, sizeof (entries[0]), sort_by_value); 2986 2987 for (i = 0; i < static_count - 1; i++) 2988 if (entries[i]->value && entries[i]->value == entries[i+1]->value) 2989 { 2990 int v = entries[i]->flags; 2991 int j; 2992 2993 for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++) 2994 v |= entries[j]->flags; 2995 2996 for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++) 2997 entries[j]->flags = v; 2998 2999 i = j - 1; 3000 } 3001 } 3002 3003 /* All entries in the hash table are fully initialized. */ 3004 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED; 3005 3006 /* Now that everything has been initialized, go through each 3007 code section and delete any prologue insns which will be 3008 redundant because their operations will be performed by 3009 a "call" instruction. */ 3010 for (input_bfd = link_info->input_bfds; 3011 input_bfd != NULL; 3012 input_bfd = input_bfd->link.next) 3013 { 3014 /* We're going to need all the local symbols for each bfd. */ 3015 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 3016 if (symtab_hdr->sh_info != 0) 3017 { 3018 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 3019 if (isymbuf == NULL) 3020 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 3021 symtab_hdr->sh_info, 0, 3022 NULL, NULL, NULL); 3023 if (isymbuf == NULL) 3024 goto error_return; 3025 } 3026 3027 /* Walk over each section in this bfd. */ 3028 for (section = input_bfd->sections; 3029 section != NULL; 3030 section = section->next) 3031 { 3032 unsigned int sec_shndx; 3033 Elf_Internal_Sym *isym, *isymend; 3034 struct elf_link_hash_entry **hashes; 3035 struct elf_link_hash_entry **end_hashes; 3036 unsigned int symcount; 3037 3038 /* Skip non-code sections and empty sections. */ 3039 if ((section->flags & SEC_CODE) == 0 || section->size == 0) 3040 continue; 3041 3042 if (section->reloc_count != 0) 3043 { 3044 /* Get a copy of the native relocations. */ 3045 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section, 3046 NULL, NULL, 3047 link_info->keep_memory); 3048 if (internal_relocs == NULL) 3049 goto error_return; 3050 } 3051 3052 /* Get cached copy of section contents if it exists. */ 3053 if (elf_section_data (section)->this_hdr.contents != NULL) 3054 contents = elf_section_data (section)->this_hdr.contents; 3055 else 3056 { 3057 /* Go get them off disk. */ 3058 if (!bfd_malloc_and_get_section (input_bfd, section, 3059 &contents)) 3060 goto error_return; 3061 } 3062 3063 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd, 3064 section); 3065 3066 /* Now look for any function in this section which needs 3067 insns deleted from its prologue. */ 3068 isymend = isymbuf + symtab_hdr->sh_info; 3069 for (isym = isymbuf; isym < isymend; isym++) 3070 { 3071 struct elf32_mn10300_link_hash_entry *sym_hash; 3072 asection *sym_sec = NULL; 3073 const char *sym_name; 3074 char *new_name; 3075 struct elf_link_hash_table *elftab; 3076 bfd_size_type amt; 3077 3078 if (isym->st_shndx != sec_shndx) 3079 continue; 3080 3081 if (isym->st_shndx == SHN_UNDEF) 3082 sym_sec = bfd_und_section_ptr; 3083 else if (isym->st_shndx == SHN_ABS) 3084 sym_sec = bfd_abs_section_ptr; 3085 else if (isym->st_shndx == SHN_COMMON) 3086 sym_sec = bfd_com_section_ptr; 3087 else 3088 sym_sec 3089 = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 3090 3091 sym_name 3092 = bfd_elf_string_from_elf_section (input_bfd, 3093 symtab_hdr->sh_link, 3094 isym->st_name); 3095 3096 /* Tack on an ID so we can uniquely identify this 3097 local symbol in the global hash table. */ 3098 amt = strlen (sym_name) + 10; 3099 new_name = bfd_malloc (amt); 3100 if (new_name == NULL) 3101 goto error_return; 3102 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id); 3103 sym_name = new_name; 3104 3105 elftab = & hash_table->static_hash_table->root; 3106 sym_hash = (struct elf32_mn10300_link_hash_entry *) 3107 elf_link_hash_lookup (elftab, sym_name, 3108 FALSE, FALSE, FALSE); 3109 3110 free (new_name); 3111 if (sym_hash == NULL) 3112 continue; 3113 3114 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS) 3115 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES)) 3116 { 3117 int bytes = 0; 3118 3119 /* Note that we've changed things. */ 3120 elf_section_data (section)->relocs = internal_relocs; 3121 elf_section_data (section)->this_hdr.contents = contents; 3122 symtab_hdr->contents = (unsigned char *) isymbuf; 3123 3124 /* Count how many bytes we're going to delete. */ 3125 if (sym_hash->movm_args) 3126 bytes += 2; 3127 3128 if (sym_hash->stack_size > 0) 3129 { 3130 if (sym_hash->stack_size <= 128) 3131 bytes += 3; 3132 else 3133 bytes += 4; 3134 } 3135 3136 /* Note that we've deleted prologue bytes for this 3137 function. */ 3138 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES; 3139 3140 /* Actually delete the bytes. */ 3141 if (!mn10300_elf_relax_delete_bytes (input_bfd, 3142 section, 3143 isym->st_value, 3144 bytes)) 3145 goto error_return; 3146 3147 /* Something changed. Not strictly necessary, but 3148 may lead to more relaxing opportunities. */ 3149 *again = TRUE; 3150 } 3151 } 3152 3153 /* Look for any global functions in this section which 3154 need insns deleted from their prologues. */ 3155 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 3156 - symtab_hdr->sh_info); 3157 hashes = elf_sym_hashes (input_bfd); 3158 end_hashes = hashes + symcount; 3159 for (; hashes < end_hashes; hashes++) 3160 { 3161 struct elf32_mn10300_link_hash_entry *sym_hash; 3162 3163 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes; 3164 if ((sym_hash->root.root.type == bfd_link_hash_defined 3165 || sym_hash->root.root.type == bfd_link_hash_defweak) 3166 && sym_hash->root.root.u.def.section == section 3167 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS) 3168 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES)) 3169 { 3170 int bytes = 0; 3171 bfd_vma symval; 3172 struct elf_link_hash_entry **hh; 3173 3174 /* Note that we've changed things. */ 3175 elf_section_data (section)->relocs = internal_relocs; 3176 elf_section_data (section)->this_hdr.contents = contents; 3177 symtab_hdr->contents = (unsigned char *) isymbuf; 3178 3179 /* Count how many bytes we're going to delete. */ 3180 if (sym_hash->movm_args) 3181 bytes += 2; 3182 3183 if (sym_hash->stack_size > 0) 3184 { 3185 if (sym_hash->stack_size <= 128) 3186 bytes += 3; 3187 else 3188 bytes += 4; 3189 } 3190 3191 /* Note that we've deleted prologue bytes for this 3192 function. */ 3193 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES; 3194 3195 /* Actually delete the bytes. */ 3196 symval = sym_hash->root.root.u.def.value; 3197 if (!mn10300_elf_relax_delete_bytes (input_bfd, 3198 section, 3199 symval, 3200 bytes)) 3201 goto error_return; 3202 3203 /* There may be other C++ functions symbols with the same 3204 address. If so then mark these as having had their 3205 prologue bytes deleted as well. */ 3206 for (hh = elf_sym_hashes (input_bfd); hh < end_hashes; hh++) 3207 { 3208 struct elf32_mn10300_link_hash_entry *h; 3209 3210 h = (struct elf32_mn10300_link_hash_entry *) * hh; 3211 3212 if (h != sym_hash 3213 && (h->root.root.type == bfd_link_hash_defined 3214 || h->root.root.type == bfd_link_hash_defweak) 3215 && h->root.root.u.def.section == section 3216 && ! (h->flags & MN10300_CONVERT_CALL_TO_CALLS) 3217 && h->root.root.u.def.value == symval 3218 && h->root.type == STT_FUNC) 3219 h->flags |= MN10300_DELETED_PROLOGUE_BYTES; 3220 } 3221 3222 /* Something changed. Not strictly necessary, but 3223 may lead to more relaxing opportunities. */ 3224 *again = TRUE; 3225 } 3226 } 3227 3228 /* Cache or free any memory we allocated for the relocs. */ 3229 if (internal_relocs != NULL 3230 && elf_section_data (section)->relocs != internal_relocs) 3231 free (internal_relocs); 3232 internal_relocs = NULL; 3233 3234 /* Cache or free any memory we allocated for the contents. */ 3235 if (contents != NULL 3236 && elf_section_data (section)->this_hdr.contents != contents) 3237 { 3238 if (! link_info->keep_memory) 3239 free (contents); 3240 else 3241 /* Cache the section contents for elf_link_input_bfd. */ 3242 elf_section_data (section)->this_hdr.contents = contents; 3243 } 3244 contents = NULL; 3245 } 3246 3247 /* Cache or free any memory we allocated for the symbols. */ 3248 if (isymbuf != NULL 3249 && symtab_hdr->contents != (unsigned char *) isymbuf) 3250 { 3251 if (! link_info->keep_memory) 3252 free (isymbuf); 3253 else 3254 /* Cache the symbols for elf_link_input_bfd. */ 3255 symtab_hdr->contents = (unsigned char *) isymbuf; 3256 } 3257 isymbuf = NULL; 3258 } 3259 } 3260 3261 /* (Re)initialize for the basic instruction shortening/relaxing pass. */ 3262 contents = NULL; 3263 internal_relocs = NULL; 3264 isymbuf = NULL; 3265 /* For error_return. */ 3266 section = sec; 3267 3268 /* We don't have to do anything for a relocatable link, if 3269 this section does not have relocs, or if this is not a 3270 code section. */ 3271 if (bfd_link_relocatable (link_info) 3272 || (sec->flags & SEC_RELOC) == 0 3273 || sec->reloc_count == 0 3274 || (sec->flags & SEC_CODE) == 0) 3275 return TRUE; 3276 3277 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 3278 3279 /* Get a copy of the native relocations. */ 3280 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, 3281 link_info->keep_memory); 3282 if (internal_relocs == NULL) 3283 goto error_return; 3284 3285 /* Scan for worst case alignment gap changes. Note that this logic 3286 is not ideal; what we should do is run this scan for every 3287 opcode/address range and adjust accordingly, but that's 3288 expensive. Worst case is that for an alignment of N bytes, we 3289 move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc 3290 all before it. Plus, this still doesn't cover cross-section 3291 jumps with section alignment. */ 3292 irelend = internal_relocs + sec->reloc_count; 3293 align_gap_adjustment = 0; 3294 for (irel = internal_relocs; irel < irelend; irel++) 3295 { 3296 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN) 3297 { 3298 bfd_vma adj = 1 << irel->r_addend; 3299 bfd_vma aend = irel->r_offset; 3300 3301 aend = BFD_ALIGN (aend, 1 << irel->r_addend); 3302 adj = 2 * adj - adj - 1; 3303 3304 /* Record the biggest adjustmnet. Skip any alignment at the 3305 end of our section. */ 3306 if (align_gap_adjustment < adj 3307 && aend < sec->output_section->vma + sec->output_offset + sec->size) 3308 align_gap_adjustment = adj; 3309 } 3310 } 3311 3312 /* Walk through them looking for relaxing opportunities. */ 3313 irelend = internal_relocs + sec->reloc_count; 3314 for (irel = internal_relocs; irel < irelend; irel++) 3315 { 3316 bfd_vma symval; 3317 bfd_signed_vma jump_offset; 3318 asection *sym_sec = NULL; 3319 struct elf32_mn10300_link_hash_entry *h = NULL; 3320 3321 /* If this isn't something that can be relaxed, then ignore 3322 this reloc. */ 3323 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE 3324 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8 3325 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX) 3326 continue; 3327 3328 /* Get the section contents if we haven't done so already. */ 3329 if (contents == NULL) 3330 { 3331 /* Get cached copy if it exists. */ 3332 if (elf_section_data (sec)->this_hdr.contents != NULL) 3333 contents = elf_section_data (sec)->this_hdr.contents; 3334 else 3335 { 3336 /* Go get them off disk. */ 3337 if (!bfd_malloc_and_get_section (abfd, sec, &contents)) 3338 goto error_return; 3339 } 3340 } 3341 3342 /* Read this BFD's symbols if we haven't done so already. */ 3343 if (isymbuf == NULL && symtab_hdr->sh_info != 0) 3344 { 3345 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 3346 if (isymbuf == NULL) 3347 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 3348 symtab_hdr->sh_info, 0, 3349 NULL, NULL, NULL); 3350 if (isymbuf == NULL) 3351 goto error_return; 3352 } 3353 3354 /* Get the value of the symbol referred to by the reloc. */ 3355 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) 3356 { 3357 Elf_Internal_Sym *isym; 3358 const char *sym_name; 3359 char *new_name; 3360 3361 /* A local symbol. */ 3362 isym = isymbuf + ELF32_R_SYM (irel->r_info); 3363 if (isym->st_shndx == SHN_UNDEF) 3364 sym_sec = bfd_und_section_ptr; 3365 else if (isym->st_shndx == SHN_ABS) 3366 sym_sec = bfd_abs_section_ptr; 3367 else if (isym->st_shndx == SHN_COMMON) 3368 sym_sec = bfd_com_section_ptr; 3369 else 3370 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 3371 3372 sym_name = bfd_elf_string_from_elf_section (abfd, 3373 symtab_hdr->sh_link, 3374 isym->st_name); 3375 3376 if ((sym_sec->flags & SEC_MERGE) 3377 && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE) 3378 { 3379 symval = isym->st_value; 3380 3381 /* GAS may reduce relocations against symbols in SEC_MERGE 3382 sections to a relocation against the section symbol when 3383 the original addend was zero. When the reloc is against 3384 a section symbol we should include the addend in the 3385 offset passed to _bfd_merged_section_offset, since the 3386 location of interest is the original symbol. On the 3387 other hand, an access to "sym+addend" where "sym" is not 3388 a section symbol should not include the addend; Such an 3389 access is presumed to be an offset from "sym"; The 3390 location of interest is just "sym". */ 3391 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION) 3392 symval += irel->r_addend; 3393 3394 symval = _bfd_merged_section_offset (abfd, & sym_sec, 3395 elf_section_data (sym_sec)->sec_info, 3396 symval); 3397 3398 if (ELF_ST_TYPE (isym->st_info) != STT_SECTION) 3399 symval += irel->r_addend; 3400 3401 symval += sym_sec->output_section->vma 3402 + sym_sec->output_offset - irel->r_addend; 3403 } 3404 else 3405 symval = (isym->st_value 3406 + sym_sec->output_section->vma 3407 + sym_sec->output_offset); 3408 3409 /* Tack on an ID so we can uniquely identify this 3410 local symbol in the global hash table. */ 3411 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10); 3412 if (new_name == NULL) 3413 goto error_return; 3414 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id); 3415 sym_name = new_name; 3416 3417 h = (struct elf32_mn10300_link_hash_entry *) 3418 elf_link_hash_lookup (&hash_table->static_hash_table->root, 3419 sym_name, FALSE, FALSE, FALSE); 3420 free (new_name); 3421 } 3422 else 3423 { 3424 unsigned long indx; 3425 3426 /* An external symbol. */ 3427 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; 3428 h = (struct elf32_mn10300_link_hash_entry *) 3429 (elf_sym_hashes (abfd)[indx]); 3430 BFD_ASSERT (h != NULL); 3431 if (h->root.root.type != bfd_link_hash_defined 3432 && h->root.root.type != bfd_link_hash_defweak) 3433 /* This appears to be a reference to an undefined 3434 symbol. Just ignore it--it will be caught by the 3435 regular reloc processing. */ 3436 continue; 3437 3438 /* Check for a reference to a discarded symbol and ignore it. */ 3439 if (h->root.root.u.def.section->output_section == NULL) 3440 continue; 3441 3442 sym_sec = h->root.root.u.def.section->output_section; 3443 3444 symval = (h->root.root.u.def.value 3445 + h->root.root.u.def.section->output_section->vma 3446 + h->root.root.u.def.section->output_offset); 3447 } 3448 3449 /* For simplicity of coding, we are going to modify the section 3450 contents, the section relocs, and the BFD symbol table. We 3451 must tell the rest of the code not to free up this 3452 information. It would be possible to instead create a table 3453 of changes which have to be made, as is done in coff-mips.c; 3454 that would be more work, but would require less memory when 3455 the linker is run. */ 3456 3457 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative 3458 branch/call, also deal with "call" -> "calls" conversions and 3459 insertion of prologue data into "call" instructions. */ 3460 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32 3461 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32) 3462 { 3463 bfd_vma value = symval; 3464 3465 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32 3466 && h != NULL 3467 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL 3468 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN 3469 && h->root.plt.offset != (bfd_vma) -1) 3470 { 3471 asection * splt; 3472 3473 splt = hash_table->root.splt; 3474 value = ((splt->output_section->vma 3475 + splt->output_offset 3476 + h->root.plt.offset) 3477 - (sec->output_section->vma 3478 + sec->output_offset 3479 + irel->r_offset)); 3480 } 3481 3482 /* If we've got a "call" instruction that needs to be turned 3483 into a "calls" instruction, do so now. It saves a byte. */ 3484 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS)) 3485 { 3486 unsigned char code; 3487 3488 /* Get the opcode. */ 3489 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 3490 3491 /* Make sure we're working with a "call" instruction! */ 3492 if (code == 0xdd) 3493 { 3494 /* Note that we've changed the relocs, section contents, 3495 etc. */ 3496 elf_section_data (sec)->relocs = internal_relocs; 3497 elf_section_data (sec)->this_hdr.contents = contents; 3498 symtab_hdr->contents = (unsigned char *) isymbuf; 3499 3500 /* Fix the opcode. */ 3501 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1); 3502 bfd_put_8 (abfd, 0xff, contents + irel->r_offset); 3503 3504 /* Fix irel->r_offset and irel->r_addend. */ 3505 irel->r_offset += 1; 3506 irel->r_addend += 1; 3507 3508 /* Delete one byte of data. */ 3509 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 3510 irel->r_offset + 3, 1)) 3511 goto error_return; 3512 3513 /* That will change things, so, we should relax again. 3514 Note that this is not required, and it may be slow. */ 3515 *again = TRUE; 3516 } 3517 } 3518 else if (h) 3519 { 3520 /* We've got a "call" instruction which needs some data 3521 from target function filled in. */ 3522 unsigned char code; 3523 3524 /* Get the opcode. */ 3525 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 3526 3527 /* Insert data from the target function into the "call" 3528 instruction if needed. */ 3529 if (code == 0xdd) 3530 { 3531 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4); 3532 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size, 3533 contents + irel->r_offset + 5); 3534 } 3535 } 3536 3537 /* Deal with pc-relative gunk. */ 3538 value -= (sec->output_section->vma + sec->output_offset); 3539 value -= irel->r_offset; 3540 value += irel->r_addend; 3541 3542 /* See if the value will fit in 16 bits, note the high value is 3543 0x7fff + 2 as the target will be two bytes closer if we are 3544 able to relax, if it's in the same section. */ 3545 if (sec->output_section == sym_sec->output_section) 3546 jump_offset = 0x8001; 3547 else 3548 jump_offset = 0x7fff; 3549 3550 /* Account for jumps across alignment boundaries using 3551 align_gap_adjustment. */ 3552 if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment 3553 && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment)) 3554 { 3555 unsigned char code; 3556 3557 /* Get the opcode. */ 3558 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 3559 3560 if (code != 0xdc && code != 0xdd && code != 0xff) 3561 continue; 3562 3563 /* Note that we've changed the relocs, section contents, etc. */ 3564 elf_section_data (sec)->relocs = internal_relocs; 3565 elf_section_data (sec)->this_hdr.contents = contents; 3566 symtab_hdr->contents = (unsigned char *) isymbuf; 3567 3568 /* Fix the opcode. */ 3569 if (code == 0xdc) 3570 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1); 3571 else if (code == 0xdd) 3572 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1); 3573 else if (code == 0xff) 3574 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); 3575 3576 /* Fix the relocation's type. */ 3577 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 3578 (ELF32_R_TYPE (irel->r_info) 3579 == (int) R_MN10300_PLT32) 3580 ? R_MN10300_PLT16 : 3581 R_MN10300_PCREL16); 3582 3583 /* Delete two bytes of data. */ 3584 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 3585 irel->r_offset + 1, 2)) 3586 goto error_return; 3587 3588 /* That will change things, so, we should relax again. 3589 Note that this is not required, and it may be slow. */ 3590 *again = TRUE; 3591 } 3592 } 3593 3594 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative 3595 branch. */ 3596 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16) 3597 { 3598 bfd_vma value = symval; 3599 3600 /* If we've got a "call" instruction that needs to be turned 3601 into a "calls" instruction, do so now. It saves a byte. */ 3602 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS)) 3603 { 3604 unsigned char code; 3605 3606 /* Get the opcode. */ 3607 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 3608 3609 /* Make sure we're working with a "call" instruction! */ 3610 if (code == 0xcd) 3611 { 3612 /* Note that we've changed the relocs, section contents, 3613 etc. */ 3614 elf_section_data (sec)->relocs = internal_relocs; 3615 elf_section_data (sec)->this_hdr.contents = contents; 3616 symtab_hdr->contents = (unsigned char *) isymbuf; 3617 3618 /* Fix the opcode. */ 3619 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1); 3620 bfd_put_8 (abfd, 0xff, contents + irel->r_offset); 3621 3622 /* Fix irel->r_offset and irel->r_addend. */ 3623 irel->r_offset += 1; 3624 irel->r_addend += 1; 3625 3626 /* Delete one byte of data. */ 3627 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 3628 irel->r_offset + 1, 1)) 3629 goto error_return; 3630 3631 /* That will change things, so, we should relax again. 3632 Note that this is not required, and it may be slow. */ 3633 *again = TRUE; 3634 } 3635 } 3636 else if (h) 3637 { 3638 unsigned char code; 3639 3640 /* Get the opcode. */ 3641 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 3642 3643 /* Insert data from the target function into the "call" 3644 instruction if needed. */ 3645 if (code == 0xcd) 3646 { 3647 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2); 3648 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size, 3649 contents + irel->r_offset + 3); 3650 } 3651 } 3652 3653 /* Deal with pc-relative gunk. */ 3654 value -= (sec->output_section->vma + sec->output_offset); 3655 value -= irel->r_offset; 3656 value += irel->r_addend; 3657 3658 /* See if the value will fit in 8 bits, note the high value is 3659 0x7f + 1 as the target will be one bytes closer if we are 3660 able to relax. */ 3661 if ((long) value < 0x80 && (long) value > -0x80) 3662 { 3663 unsigned char code; 3664 3665 /* Get the opcode. */ 3666 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 3667 3668 if (code != 0xcc) 3669 continue; 3670 3671 /* Note that we've changed the relocs, section contents, etc. */ 3672 elf_section_data (sec)->relocs = internal_relocs; 3673 elf_section_data (sec)->this_hdr.contents = contents; 3674 symtab_hdr->contents = (unsigned char *) isymbuf; 3675 3676 /* Fix the opcode. */ 3677 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1); 3678 3679 /* Fix the relocation's type. */ 3680 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 3681 R_MN10300_PCREL8); 3682 3683 /* Delete one byte of data. */ 3684 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 3685 irel->r_offset + 1, 1)) 3686 goto error_return; 3687 3688 /* That will change things, so, we should relax again. 3689 Note that this is not required, and it may be slow. */ 3690 *again = TRUE; 3691 } 3692 } 3693 3694 /* Try to eliminate an unconditional 8 bit pc-relative branch 3695 which immediately follows a conditional 8 bit pc-relative 3696 branch around the unconditional branch. 3697 3698 original: new: 3699 bCC lab1 bCC' lab2 3700 bra lab2 3701 lab1: lab1: 3702 3703 This happens when the bCC can't reach lab2 at assembly time, 3704 but due to other relaxations it can reach at link time. */ 3705 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8) 3706 { 3707 Elf_Internal_Rela *nrel; 3708 bfd_vma value = symval; 3709 unsigned char code; 3710 3711 /* Deal with pc-relative gunk. */ 3712 value -= (sec->output_section->vma + sec->output_offset); 3713 value -= irel->r_offset; 3714 value += irel->r_addend; 3715 3716 /* Do nothing if this reloc is the last byte in the section. */ 3717 if (irel->r_offset == sec->size) 3718 continue; 3719 3720 /* See if the next instruction is an unconditional pc-relative 3721 branch, more often than not this test will fail, so we 3722 test it first to speed things up. */ 3723 code = bfd_get_8 (abfd, contents + irel->r_offset + 1); 3724 if (code != 0xca) 3725 continue; 3726 3727 /* Also make sure the next relocation applies to the next 3728 instruction and that it's a pc-relative 8 bit branch. */ 3729 nrel = irel + 1; 3730 if (nrel == irelend 3731 || irel->r_offset + 2 != nrel->r_offset 3732 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8) 3733 continue; 3734 3735 /* Make sure our destination immediately follows the 3736 unconditional branch. */ 3737 if (symval != (sec->output_section->vma + sec->output_offset 3738 + irel->r_offset + 3)) 3739 continue; 3740 3741 /* Now make sure we are a conditional branch. This may not 3742 be necessary, but why take the chance. 3743 3744 Note these checks assume that R_MN10300_PCREL8 relocs 3745 only occur on bCC and bCCx insns. If they occured 3746 elsewhere, we'd need to know the start of this insn 3747 for this check to be accurate. */ 3748 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 3749 if (code != 0xc0 && code != 0xc1 && code != 0xc2 3750 && code != 0xc3 && code != 0xc4 && code != 0xc5 3751 && code != 0xc6 && code != 0xc7 && code != 0xc8 3752 && code != 0xc9 && code != 0xe8 && code != 0xe9 3753 && code != 0xea && code != 0xeb) 3754 continue; 3755 3756 /* We also have to be sure there is no symbol/label 3757 at the unconditional branch. */ 3758 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf, 3759 irel->r_offset + 1)) 3760 continue; 3761 3762 /* Note that we've changed the relocs, section contents, etc. */ 3763 elf_section_data (sec)->relocs = internal_relocs; 3764 elf_section_data (sec)->this_hdr.contents = contents; 3765 symtab_hdr->contents = (unsigned char *) isymbuf; 3766 3767 /* Reverse the condition of the first branch. */ 3768 switch (code) 3769 { 3770 case 0xc8: 3771 code = 0xc9; 3772 break; 3773 case 0xc9: 3774 code = 0xc8; 3775 break; 3776 case 0xc0: 3777 code = 0xc2; 3778 break; 3779 case 0xc2: 3780 code = 0xc0; 3781 break; 3782 case 0xc3: 3783 code = 0xc1; 3784 break; 3785 case 0xc1: 3786 code = 0xc3; 3787 break; 3788 case 0xc4: 3789 code = 0xc6; 3790 break; 3791 case 0xc6: 3792 code = 0xc4; 3793 break; 3794 case 0xc7: 3795 code = 0xc5; 3796 break; 3797 case 0xc5: 3798 code = 0xc7; 3799 break; 3800 case 0xe8: 3801 code = 0xe9; 3802 break; 3803 case 0x9d: 3804 code = 0xe8; 3805 break; 3806 case 0xea: 3807 code = 0xeb; 3808 break; 3809 case 0xeb: 3810 code = 0xea; 3811 break; 3812 } 3813 bfd_put_8 (abfd, code, contents + irel->r_offset - 1); 3814 3815 /* Set the reloc type and symbol for the first branch 3816 from the second branch. */ 3817 irel->r_info = nrel->r_info; 3818 3819 /* Make the reloc for the second branch a null reloc. */ 3820 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info), 3821 R_MN10300_NONE); 3822 3823 /* Delete two bytes of data. */ 3824 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 3825 irel->r_offset + 1, 2)) 3826 goto error_return; 3827 3828 /* That will change things, so, we should relax again. 3829 Note that this is not required, and it may be slow. */ 3830 *again = TRUE; 3831 } 3832 3833 /* Try to turn a 24 immediate, displacement or absolute address 3834 into a 8 immediate, displacement or absolute address. */ 3835 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24) 3836 { 3837 bfd_vma value = symval; 3838 value += irel->r_addend; 3839 3840 /* See if the value will fit in 8 bits. */ 3841 if ((long) value < 0x7f && (long) value > -0x80) 3842 { 3843 unsigned char code; 3844 3845 /* AM33 insns which have 24 operands are 6 bytes long and 3846 will have 0xfd as the first byte. */ 3847 3848 /* Get the first opcode. */ 3849 code = bfd_get_8 (abfd, contents + irel->r_offset - 3); 3850 3851 if (code == 0xfd) 3852 { 3853 /* Get the second opcode. */ 3854 code = bfd_get_8 (abfd, contents + irel->r_offset - 2); 3855 3856 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit 3857 equivalent instructions exists. */ 3858 if (code != 0x6b && code != 0x7b 3859 && code != 0x8b && code != 0x9b 3860 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08 3861 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b 3862 || (code & 0x0f) == 0x0e)) 3863 { 3864 /* Not safe if the high bit is on as relaxing may 3865 move the value out of high mem and thus not fit 3866 in a signed 8bit value. This is currently over 3867 conservative. */ 3868 if ((value & 0x80) == 0) 3869 { 3870 /* Note that we've changed the relocation contents, 3871 etc. */ 3872 elf_section_data (sec)->relocs = internal_relocs; 3873 elf_section_data (sec)->this_hdr.contents = contents; 3874 symtab_hdr->contents = (unsigned char *) isymbuf; 3875 3876 /* Fix the opcode. */ 3877 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3); 3878 bfd_put_8 (abfd, code, contents + irel->r_offset - 2); 3879 3880 /* Fix the relocation's type. */ 3881 irel->r_info = 3882 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 3883 R_MN10300_8); 3884 3885 /* Delete two bytes of data. */ 3886 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 3887 irel->r_offset + 1, 2)) 3888 goto error_return; 3889 3890 /* That will change things, so, we should relax 3891 again. Note that this is not required, and it 3892 may be slow. */ 3893 *again = TRUE; 3894 break; 3895 } 3896 } 3897 } 3898 } 3899 } 3900 3901 /* Try to turn a 32bit immediate, displacement or absolute address 3902 into a 16bit immediate, displacement or absolute address. */ 3903 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32 3904 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32 3905 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32) 3906 { 3907 bfd_vma value = symval; 3908 3909 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32) 3910 { 3911 asection * sgot; 3912 3913 sgot = hash_table->root.sgot; 3914 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32) 3915 { 3916 value = sgot->output_offset; 3917 3918 if (h) 3919 value += h->root.got.offset; 3920 else 3921 value += (elf_local_got_offsets 3922 (abfd)[ELF32_R_SYM (irel->r_info)]); 3923 } 3924 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32) 3925 value -= sgot->output_section->vma; 3926 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32) 3927 value = (sgot->output_section->vma 3928 - (sec->output_section->vma 3929 + sec->output_offset 3930 + irel->r_offset)); 3931 else 3932 abort (); 3933 } 3934 3935 value += irel->r_addend; 3936 3937 /* See if the value will fit in 24 bits. 3938 We allow any 16bit match here. We prune those we can't 3939 handle below. */ 3940 if ((long) value < 0x7fffff && (long) value > -0x800000) 3941 { 3942 unsigned char code; 3943 3944 /* AM33 insns which have 32bit operands are 7 bytes long and 3945 will have 0xfe as the first byte. */ 3946 3947 /* Get the first opcode. */ 3948 code = bfd_get_8 (abfd, contents + irel->r_offset - 3); 3949 3950 if (code == 0xfe) 3951 { 3952 /* Get the second opcode. */ 3953 code = bfd_get_8 (abfd, contents + irel->r_offset - 2); 3954 3955 /* All the am33 32 -> 24 relaxing possibilities. */ 3956 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit 3957 equivalent instructions exists. */ 3958 if (code != 0x6b && code != 0x7b 3959 && code != 0x8b && code != 0x9b 3960 && (ELF32_R_TYPE (irel->r_info) 3961 != (int) R_MN10300_GOTPC32) 3962 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08 3963 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b 3964 || (code & 0x0f) == 0x0e)) 3965 { 3966 /* Not safe if the high bit is on as relaxing may 3967 move the value out of high mem and thus not fit 3968 in a signed 16bit value. This is currently over 3969 conservative. */ 3970 if ((value & 0x8000) == 0) 3971 { 3972 /* Note that we've changed the relocation contents, 3973 etc. */ 3974 elf_section_data (sec)->relocs = internal_relocs; 3975 elf_section_data (sec)->this_hdr.contents = contents; 3976 symtab_hdr->contents = (unsigned char *) isymbuf; 3977 3978 /* Fix the opcode. */ 3979 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3); 3980 bfd_put_8 (abfd, code, contents + irel->r_offset - 2); 3981 3982 /* Fix the relocation's type. */ 3983 irel->r_info = 3984 ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 3985 (ELF32_R_TYPE (irel->r_info) 3986 == (int) R_MN10300_GOTOFF32) 3987 ? R_MN10300_GOTOFF24 3988 : (ELF32_R_TYPE (irel->r_info) 3989 == (int) R_MN10300_GOT32) 3990 ? R_MN10300_GOT24 : 3991 R_MN10300_24); 3992 3993 /* Delete one byte of data. */ 3994 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 3995 irel->r_offset + 3, 1)) 3996 goto error_return; 3997 3998 /* That will change things, so, we should relax 3999 again. Note that this is not required, and it 4000 may be slow. */ 4001 *again = TRUE; 4002 break; 4003 } 4004 } 4005 } 4006 } 4007 4008 /* See if the value will fit in 16 bits. 4009 We allow any 16bit match here. We prune those we can't 4010 handle below. */ 4011 if ((long) value < 0x7fff && (long) value > -0x8000) 4012 { 4013 unsigned char code; 4014 4015 /* Most insns which have 32bit operands are 6 bytes long; 4016 exceptions are pcrel insns and bit insns. 4017 4018 We handle pcrel insns above. We don't bother trying 4019 to handle the bit insns here. 4020 4021 The first byte of the remaining insns will be 0xfc. */ 4022 4023 /* Get the first opcode. */ 4024 code = bfd_get_8 (abfd, contents + irel->r_offset - 2); 4025 4026 if (code != 0xfc) 4027 continue; 4028 4029 /* Get the second opcode. */ 4030 code = bfd_get_8 (abfd, contents + irel->r_offset - 1); 4031 4032 if ((code & 0xf0) < 0x80) 4033 switch (code & 0xf0) 4034 { 4035 /* mov (d32,am),dn -> mov (d32,am),dn 4036 mov dm,(d32,am) -> mov dn,(d32,am) 4037 mov (d32,am),an -> mov (d32,am),an 4038 mov dm,(d32,am) -> mov dn,(d32,am) 4039 movbu (d32,am),dn -> movbu (d32,am),dn 4040 movbu dm,(d32,am) -> movbu dn,(d32,am) 4041 movhu (d32,am),dn -> movhu (d32,am),dn 4042 movhu dm,(d32,am) -> movhu dn,(d32,am) */ 4043 case 0x00: 4044 case 0x10: 4045 case 0x20: 4046 case 0x30: 4047 case 0x40: 4048 case 0x50: 4049 case 0x60: 4050 case 0x70: 4051 /* Not safe if the high bit is on as relaxing may 4052 move the value out of high mem and thus not fit 4053 in a signed 16bit value. */ 4054 if (code == 0xcc 4055 && (value & 0x8000)) 4056 continue; 4057 4058 /* Note that we've changed the relocation contents, etc. */ 4059 elf_section_data (sec)->relocs = internal_relocs; 4060 elf_section_data (sec)->this_hdr.contents = contents; 4061 symtab_hdr->contents = (unsigned char *) isymbuf; 4062 4063 /* Fix the opcode. */ 4064 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); 4065 bfd_put_8 (abfd, code, contents + irel->r_offset - 1); 4066 4067 /* Fix the relocation's type. */ 4068 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 4069 (ELF32_R_TYPE (irel->r_info) 4070 == (int) R_MN10300_GOTOFF32) 4071 ? R_MN10300_GOTOFF16 4072 : (ELF32_R_TYPE (irel->r_info) 4073 == (int) R_MN10300_GOT32) 4074 ? R_MN10300_GOT16 4075 : (ELF32_R_TYPE (irel->r_info) 4076 == (int) R_MN10300_GOTPC32) 4077 ? R_MN10300_GOTPC16 : 4078 R_MN10300_16); 4079 4080 /* Delete two bytes of data. */ 4081 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 4082 irel->r_offset + 2, 2)) 4083 goto error_return; 4084 4085 /* That will change things, so, we should relax again. 4086 Note that this is not required, and it may be slow. */ 4087 *again = TRUE; 4088 break; 4089 } 4090 else if ((code & 0xf0) == 0x80 4091 || (code & 0xf0) == 0x90) 4092 switch (code & 0xf3) 4093 { 4094 /* mov dn,(abs32) -> mov dn,(abs16) 4095 movbu dn,(abs32) -> movbu dn,(abs16) 4096 movhu dn,(abs32) -> movhu dn,(abs16) */ 4097 case 0x81: 4098 case 0x82: 4099 case 0x83: 4100 /* Note that we've changed the relocation contents, etc. */ 4101 elf_section_data (sec)->relocs = internal_relocs; 4102 elf_section_data (sec)->this_hdr.contents = contents; 4103 symtab_hdr->contents = (unsigned char *) isymbuf; 4104 4105 if ((code & 0xf3) == 0x81) 4106 code = 0x01 + (code & 0x0c); 4107 else if ((code & 0xf3) == 0x82) 4108 code = 0x02 + (code & 0x0c); 4109 else if ((code & 0xf3) == 0x83) 4110 code = 0x03 + (code & 0x0c); 4111 else 4112 abort (); 4113 4114 /* Fix the opcode. */ 4115 bfd_put_8 (abfd, code, contents + irel->r_offset - 2); 4116 4117 /* Fix the relocation's type. */ 4118 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 4119 (ELF32_R_TYPE (irel->r_info) 4120 == (int) R_MN10300_GOTOFF32) 4121 ? R_MN10300_GOTOFF16 4122 : (ELF32_R_TYPE (irel->r_info) 4123 == (int) R_MN10300_GOT32) 4124 ? R_MN10300_GOT16 4125 : (ELF32_R_TYPE (irel->r_info) 4126 == (int) R_MN10300_GOTPC32) 4127 ? R_MN10300_GOTPC16 : 4128 R_MN10300_16); 4129 4130 /* The opcode got shorter too, so we have to fix the 4131 addend and offset too! */ 4132 irel->r_offset -= 1; 4133 4134 /* Delete three bytes of data. */ 4135 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 4136 irel->r_offset + 1, 3)) 4137 goto error_return; 4138 4139 /* That will change things, so, we should relax again. 4140 Note that this is not required, and it may be slow. */ 4141 *again = TRUE; 4142 break; 4143 4144 /* mov am,(abs32) -> mov am,(abs16) 4145 mov am,(d32,sp) -> mov am,(d16,sp) 4146 mov dm,(d32,sp) -> mov dm,(d32,sp) 4147 movbu dm,(d32,sp) -> movbu dm,(d32,sp) 4148 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */ 4149 case 0x80: 4150 case 0x90: 4151 case 0x91: 4152 case 0x92: 4153 case 0x93: 4154 /* sp-based offsets are zero-extended. */ 4155 if (code >= 0x90 && code <= 0x93 4156 && (long) value < 0) 4157 continue; 4158 4159 /* Note that we've changed the relocation contents, etc. */ 4160 elf_section_data (sec)->relocs = internal_relocs; 4161 elf_section_data (sec)->this_hdr.contents = contents; 4162 symtab_hdr->contents = (unsigned char *) isymbuf; 4163 4164 /* Fix the opcode. */ 4165 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); 4166 bfd_put_8 (abfd, code, contents + irel->r_offset - 1); 4167 4168 /* Fix the relocation's type. */ 4169 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 4170 (ELF32_R_TYPE (irel->r_info) 4171 == (int) R_MN10300_GOTOFF32) 4172 ? R_MN10300_GOTOFF16 4173 : (ELF32_R_TYPE (irel->r_info) 4174 == (int) R_MN10300_GOT32) 4175 ? R_MN10300_GOT16 4176 : (ELF32_R_TYPE (irel->r_info) 4177 == (int) R_MN10300_GOTPC32) 4178 ? R_MN10300_GOTPC16 : 4179 R_MN10300_16); 4180 4181 /* Delete two bytes of data. */ 4182 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 4183 irel->r_offset + 2, 2)) 4184 goto error_return; 4185 4186 /* That will change things, so, we should relax again. 4187 Note that this is not required, and it may be slow. */ 4188 *again = TRUE; 4189 break; 4190 } 4191 else if ((code & 0xf0) < 0xf0) 4192 switch (code & 0xfc) 4193 { 4194 /* mov imm32,dn -> mov imm16,dn 4195 mov imm32,an -> mov imm16,an 4196 mov (abs32),dn -> mov (abs16),dn 4197 movbu (abs32),dn -> movbu (abs16),dn 4198 movhu (abs32),dn -> movhu (abs16),dn */ 4199 case 0xcc: 4200 case 0xdc: 4201 case 0xa4: 4202 case 0xa8: 4203 case 0xac: 4204 /* Not safe if the high bit is on as relaxing may 4205 move the value out of high mem and thus not fit 4206 in a signed 16bit value. */ 4207 if (code == 0xcc 4208 && (value & 0x8000)) 4209 continue; 4210 4211 /* "mov imm16, an" zero-extends the immediate. */ 4212 if ((code & 0xfc) == 0xdc 4213 && (long) value < 0) 4214 continue; 4215 4216 /* Note that we've changed the relocation contents, etc. */ 4217 elf_section_data (sec)->relocs = internal_relocs; 4218 elf_section_data (sec)->this_hdr.contents = contents; 4219 symtab_hdr->contents = (unsigned char *) isymbuf; 4220 4221 if ((code & 0xfc) == 0xcc) 4222 code = 0x2c + (code & 0x03); 4223 else if ((code & 0xfc) == 0xdc) 4224 code = 0x24 + (code & 0x03); 4225 else if ((code & 0xfc) == 0xa4) 4226 code = 0x30 + (code & 0x03); 4227 else if ((code & 0xfc) == 0xa8) 4228 code = 0x34 + (code & 0x03); 4229 else if ((code & 0xfc) == 0xac) 4230 code = 0x38 + (code & 0x03); 4231 else 4232 abort (); 4233 4234 /* Fix the opcode. */ 4235 bfd_put_8 (abfd, code, contents + irel->r_offset - 2); 4236 4237 /* Fix the relocation's type. */ 4238 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 4239 (ELF32_R_TYPE (irel->r_info) 4240 == (int) R_MN10300_GOTOFF32) 4241 ? R_MN10300_GOTOFF16 4242 : (ELF32_R_TYPE (irel->r_info) 4243 == (int) R_MN10300_GOT32) 4244 ? R_MN10300_GOT16 4245 : (ELF32_R_TYPE (irel->r_info) 4246 == (int) R_MN10300_GOTPC32) 4247 ? R_MN10300_GOTPC16 : 4248 R_MN10300_16); 4249 4250 /* The opcode got shorter too, so we have to fix the 4251 addend and offset too! */ 4252 irel->r_offset -= 1; 4253 4254 /* Delete three bytes of data. */ 4255 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 4256 irel->r_offset + 1, 3)) 4257 goto error_return; 4258 4259 /* That will change things, so, we should relax again. 4260 Note that this is not required, and it may be slow. */ 4261 *again = TRUE; 4262 break; 4263 4264 /* mov (abs32),an -> mov (abs16),an 4265 mov (d32,sp),an -> mov (d16,sp),an 4266 mov (d32,sp),dn -> mov (d16,sp),dn 4267 movbu (d32,sp),dn -> movbu (d16,sp),dn 4268 movhu (d32,sp),dn -> movhu (d16,sp),dn 4269 add imm32,dn -> add imm16,dn 4270 cmp imm32,dn -> cmp imm16,dn 4271 add imm32,an -> add imm16,an 4272 cmp imm32,an -> cmp imm16,an 4273 and imm32,dn -> and imm16,dn 4274 or imm32,dn -> or imm16,dn 4275 xor imm32,dn -> xor imm16,dn 4276 btst imm32,dn -> btst imm16,dn */ 4277 4278 case 0xa0: 4279 case 0xb0: 4280 case 0xb1: 4281 case 0xb2: 4282 case 0xb3: 4283 case 0xc0: 4284 case 0xc8: 4285 4286 case 0xd0: 4287 case 0xd8: 4288 case 0xe0: 4289 case 0xe1: 4290 case 0xe2: 4291 case 0xe3: 4292 /* cmp imm16, an zero-extends the immediate. */ 4293 if (code == 0xdc 4294 && (long) value < 0) 4295 continue; 4296 4297 /* So do sp-based offsets. */ 4298 if (code >= 0xb0 && code <= 0xb3 4299 && (long) value < 0) 4300 continue; 4301 4302 /* Note that we've changed the relocation contents, etc. */ 4303 elf_section_data (sec)->relocs = internal_relocs; 4304 elf_section_data (sec)->this_hdr.contents = contents; 4305 symtab_hdr->contents = (unsigned char *) isymbuf; 4306 4307 /* Fix the opcode. */ 4308 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); 4309 bfd_put_8 (abfd, code, contents + irel->r_offset - 1); 4310 4311 /* Fix the relocation's type. */ 4312 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 4313 (ELF32_R_TYPE (irel->r_info) 4314 == (int) R_MN10300_GOTOFF32) 4315 ? R_MN10300_GOTOFF16 4316 : (ELF32_R_TYPE (irel->r_info) 4317 == (int) R_MN10300_GOT32) 4318 ? R_MN10300_GOT16 4319 : (ELF32_R_TYPE (irel->r_info) 4320 == (int) R_MN10300_GOTPC32) 4321 ? R_MN10300_GOTPC16 : 4322 R_MN10300_16); 4323 4324 /* Delete two bytes of data. */ 4325 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 4326 irel->r_offset + 2, 2)) 4327 goto error_return; 4328 4329 /* That will change things, so, we should relax again. 4330 Note that this is not required, and it may be slow. */ 4331 *again = TRUE; 4332 break; 4333 } 4334 else if (code == 0xfe) 4335 { 4336 /* add imm32,sp -> add imm16,sp */ 4337 4338 /* Note that we've changed the relocation contents, etc. */ 4339 elf_section_data (sec)->relocs = internal_relocs; 4340 elf_section_data (sec)->this_hdr.contents = contents; 4341 symtab_hdr->contents = (unsigned char *) isymbuf; 4342 4343 /* Fix the opcode. */ 4344 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2); 4345 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1); 4346 4347 /* Fix the relocation's type. */ 4348 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 4349 (ELF32_R_TYPE (irel->r_info) 4350 == (int) R_MN10300_GOT32) 4351 ? R_MN10300_GOT16 4352 : (ELF32_R_TYPE (irel->r_info) 4353 == (int) R_MN10300_GOTOFF32) 4354 ? R_MN10300_GOTOFF16 4355 : (ELF32_R_TYPE (irel->r_info) 4356 == (int) R_MN10300_GOTPC32) 4357 ? R_MN10300_GOTPC16 : 4358 R_MN10300_16); 4359 4360 /* Delete two bytes of data. */ 4361 if (!mn10300_elf_relax_delete_bytes (abfd, sec, 4362 irel->r_offset + 2, 2)) 4363 goto error_return; 4364 4365 /* That will change things, so, we should relax again. 4366 Note that this is not required, and it may be slow. */ 4367 *again = TRUE; 4368 break; 4369 } 4370 } 4371 } 4372 } 4373 4374 if (isymbuf != NULL 4375 && symtab_hdr->contents != (unsigned char *) isymbuf) 4376 { 4377 if (! link_info->keep_memory) 4378 free (isymbuf); 4379 else 4380 { 4381 /* Cache the symbols for elf_link_input_bfd. */ 4382 symtab_hdr->contents = (unsigned char *) isymbuf; 4383 } 4384 } 4385 4386 if (contents != NULL 4387 && elf_section_data (sec)->this_hdr.contents != contents) 4388 { 4389 if (! link_info->keep_memory) 4390 free (contents); 4391 else 4392 { 4393 /* Cache the section contents for elf_link_input_bfd. */ 4394 elf_section_data (sec)->this_hdr.contents = contents; 4395 } 4396 } 4397 4398 if (internal_relocs != NULL 4399 && elf_section_data (sec)->relocs != internal_relocs) 4400 free (internal_relocs); 4401 4402 return TRUE; 4403 4404 error_return: 4405 if (isymbuf != NULL 4406 && symtab_hdr->contents != (unsigned char *) isymbuf) 4407 free (isymbuf); 4408 if (contents != NULL 4409 && elf_section_data (section)->this_hdr.contents != contents) 4410 free (contents); 4411 if (internal_relocs != NULL 4412 && elf_section_data (section)->relocs != internal_relocs) 4413 free (internal_relocs); 4414 4415 return FALSE; 4416} 4417 4418/* This is a version of bfd_generic_get_relocated_section_contents 4419 which uses mn10300_elf_relocate_section. */ 4420 4421static bfd_byte * 4422mn10300_elf_get_relocated_section_contents (bfd *output_bfd, 4423 struct bfd_link_info *link_info, 4424 struct bfd_link_order *link_order, 4425 bfd_byte *data, 4426 bfd_boolean relocatable, 4427 asymbol **symbols) 4428{ 4429 Elf_Internal_Shdr *symtab_hdr; 4430 asection *input_section = link_order->u.indirect.section; 4431 bfd *input_bfd = input_section->owner; 4432 asection **sections = NULL; 4433 Elf_Internal_Rela *internal_relocs = NULL; 4434 Elf_Internal_Sym *isymbuf = NULL; 4435 4436 /* We only need to handle the case of relaxing, or of having a 4437 particular set of section contents, specially. */ 4438 if (relocatable 4439 || elf_section_data (input_section)->this_hdr.contents == NULL) 4440 return bfd_generic_get_relocated_section_contents (output_bfd, link_info, 4441 link_order, data, 4442 relocatable, 4443 symbols); 4444 4445 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 4446 4447 memcpy (data, elf_section_data (input_section)->this_hdr.contents, 4448 (size_t) input_section->size); 4449 4450 if ((input_section->flags & SEC_RELOC) != 0 4451 && input_section->reloc_count > 0) 4452 { 4453 asection **secpp; 4454 Elf_Internal_Sym *isym, *isymend; 4455 bfd_size_type amt; 4456 4457 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section, 4458 NULL, NULL, FALSE); 4459 if (internal_relocs == NULL) 4460 goto error_return; 4461 4462 if (symtab_hdr->sh_info != 0) 4463 { 4464 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 4465 if (isymbuf == NULL) 4466 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 4467 symtab_hdr->sh_info, 0, 4468 NULL, NULL, NULL); 4469 if (isymbuf == NULL) 4470 goto error_return; 4471 } 4472 4473 amt = symtab_hdr->sh_info; 4474 amt *= sizeof (asection *); 4475 sections = bfd_malloc (amt); 4476 if (sections == NULL && amt != 0) 4477 goto error_return; 4478 4479 isymend = isymbuf + symtab_hdr->sh_info; 4480 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) 4481 { 4482 asection *isec; 4483 4484 if (isym->st_shndx == SHN_UNDEF) 4485 isec = bfd_und_section_ptr; 4486 else if (isym->st_shndx == SHN_ABS) 4487 isec = bfd_abs_section_ptr; 4488 else if (isym->st_shndx == SHN_COMMON) 4489 isec = bfd_com_section_ptr; 4490 else 4491 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 4492 4493 *secpp = isec; 4494 } 4495 4496 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd, 4497 input_section, data, internal_relocs, 4498 isymbuf, sections)) 4499 goto error_return; 4500 4501 if (sections != NULL) 4502 free (sections); 4503 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf) 4504 free (isymbuf); 4505 if (internal_relocs != elf_section_data (input_section)->relocs) 4506 free (internal_relocs); 4507 } 4508 4509 return data; 4510 4511 error_return: 4512 if (sections != NULL) 4513 free (sections); 4514 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf) 4515 free (isymbuf); 4516 if (internal_relocs != NULL 4517 && internal_relocs != elf_section_data (input_section)->relocs) 4518 free (internal_relocs); 4519 return NULL; 4520} 4521 4522/* Assorted hash table functions. */ 4523 4524/* Initialize an entry in the link hash table. */ 4525 4526/* Create an entry in an MN10300 ELF linker hash table. */ 4527 4528static struct bfd_hash_entry * 4529elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry, 4530 struct bfd_hash_table *table, 4531 const char *string) 4532{ 4533 struct elf32_mn10300_link_hash_entry *ret = 4534 (struct elf32_mn10300_link_hash_entry *) entry; 4535 4536 /* Allocate the structure if it has not already been allocated by a 4537 subclass. */ 4538 if (ret == NULL) 4539 ret = (struct elf32_mn10300_link_hash_entry *) 4540 bfd_hash_allocate (table, sizeof (* ret)); 4541 if (ret == NULL) 4542 return (struct bfd_hash_entry *) ret; 4543 4544 /* Call the allocation method of the superclass. */ 4545 ret = (struct elf32_mn10300_link_hash_entry *) 4546 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, 4547 table, string); 4548 if (ret != NULL) 4549 { 4550 ret->direct_calls = 0; 4551 ret->stack_size = 0; 4552 ret->movm_args = 0; 4553 ret->movm_stack_size = 0; 4554 ret->flags = 0; 4555 ret->value = 0; 4556 ret->tls_type = GOT_UNKNOWN; 4557 } 4558 4559 return (struct bfd_hash_entry *) ret; 4560} 4561 4562static void 4563_bfd_mn10300_copy_indirect_symbol (struct bfd_link_info * info, 4564 struct elf_link_hash_entry * dir, 4565 struct elf_link_hash_entry * ind) 4566{ 4567 struct elf32_mn10300_link_hash_entry * edir; 4568 struct elf32_mn10300_link_hash_entry * eind; 4569 4570 edir = elf_mn10300_hash_entry (dir); 4571 eind = elf_mn10300_hash_entry (ind); 4572 4573 if (ind->root.type == bfd_link_hash_indirect 4574 && dir->got.refcount <= 0) 4575 { 4576 edir->tls_type = eind->tls_type; 4577 eind->tls_type = GOT_UNKNOWN; 4578 } 4579 edir->direct_calls = eind->direct_calls; 4580 edir->stack_size = eind->stack_size; 4581 edir->movm_args = eind->movm_args; 4582 edir->movm_stack_size = eind->movm_stack_size; 4583 edir->flags = eind->flags; 4584 4585 _bfd_elf_link_hash_copy_indirect (info, dir, ind); 4586} 4587 4588/* Destroy an mn10300 ELF linker hash table. */ 4589 4590static void 4591elf32_mn10300_link_hash_table_free (bfd *obfd) 4592{ 4593 struct elf32_mn10300_link_hash_table *ret 4594 = (struct elf32_mn10300_link_hash_table *) obfd->link.hash; 4595 4596 obfd->link.hash = &ret->static_hash_table->root.root; 4597 _bfd_elf_link_hash_table_free (obfd); 4598 obfd->is_linker_output = TRUE; 4599 obfd->link.hash = &ret->root.root; 4600 _bfd_elf_link_hash_table_free (obfd); 4601} 4602 4603/* Create an mn10300 ELF linker hash table. */ 4604 4605static struct bfd_link_hash_table * 4606elf32_mn10300_link_hash_table_create (bfd *abfd) 4607{ 4608 struct elf32_mn10300_link_hash_table *ret; 4609 bfd_size_type amt = sizeof (* ret); 4610 4611 ret = bfd_zmalloc (amt); 4612 if (ret == NULL) 4613 return NULL; 4614 4615 amt = sizeof (struct elf_link_hash_table); 4616 ret->static_hash_table = bfd_zmalloc (amt); 4617 if (ret->static_hash_table == NULL) 4618 { 4619 free (ret); 4620 return NULL; 4621 } 4622 4623 if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd, 4624 elf32_mn10300_link_hash_newfunc, 4625 sizeof (struct elf32_mn10300_link_hash_entry), 4626 MN10300_ELF_DATA)) 4627 { 4628 free (ret->static_hash_table); 4629 free (ret); 4630 return NULL; 4631 } 4632 4633 abfd->is_linker_output = FALSE; 4634 abfd->link.hash = NULL; 4635 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd, 4636 elf32_mn10300_link_hash_newfunc, 4637 sizeof (struct elf32_mn10300_link_hash_entry), 4638 MN10300_ELF_DATA)) 4639 { 4640 abfd->is_linker_output = TRUE; 4641 abfd->link.hash = &ret->static_hash_table->root.root; 4642 _bfd_elf_link_hash_table_free (abfd); 4643 free (ret); 4644 return NULL; 4645 } 4646 ret->root.root.hash_table_free = elf32_mn10300_link_hash_table_free; 4647 4648 ret->tls_ldm_got.offset = -1; 4649 4650 return & ret->root.root; 4651} 4652 4653static unsigned long 4654elf_mn10300_mach (flagword flags) 4655{ 4656 switch (flags & EF_MN10300_MACH) 4657 { 4658 case E_MN10300_MACH_MN10300: 4659 default: 4660 return bfd_mach_mn10300; 4661 4662 case E_MN10300_MACH_AM33: 4663 return bfd_mach_am33; 4664 4665 case E_MN10300_MACH_AM33_2: 4666 return bfd_mach_am33_2; 4667 } 4668} 4669 4670/* The final processing done just before writing out a MN10300 ELF object 4671 file. This gets the MN10300 architecture right based on the machine 4672 number. */ 4673 4674static void 4675_bfd_mn10300_elf_final_write_processing (bfd *abfd, 4676 bfd_boolean linker ATTRIBUTE_UNUSED) 4677{ 4678 unsigned long val; 4679 4680 switch (bfd_get_mach (abfd)) 4681 { 4682 default: 4683 case bfd_mach_mn10300: 4684 val = E_MN10300_MACH_MN10300; 4685 break; 4686 4687 case bfd_mach_am33: 4688 val = E_MN10300_MACH_AM33; 4689 break; 4690 4691 case bfd_mach_am33_2: 4692 val = E_MN10300_MACH_AM33_2; 4693 break; 4694 } 4695 4696 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH); 4697 elf_elfheader (abfd)->e_flags |= val; 4698} 4699 4700static bfd_boolean 4701_bfd_mn10300_elf_object_p (bfd *abfd) 4702{ 4703 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300, 4704 elf_mn10300_mach (elf_elfheader (abfd)->e_flags)); 4705 return TRUE; 4706} 4707 4708/* Merge backend specific data from an object file to the output 4709 object file when linking. */ 4710 4711static bfd_boolean 4712_bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info) 4713{ 4714 bfd *obfd = info->output_bfd; 4715 4716 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour 4717 || bfd_get_flavour (obfd) != bfd_target_elf_flavour) 4718 return TRUE; 4719 4720 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) 4721 && bfd_get_mach (obfd) < bfd_get_mach (ibfd)) 4722 { 4723 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), 4724 bfd_get_mach (ibfd))) 4725 return FALSE; 4726 } 4727 4728 return TRUE; 4729} 4730 4731#define PLT0_ENTRY_SIZE 15 4732#define PLT_ENTRY_SIZE 20 4733#define PIC_PLT_ENTRY_SIZE 24 4734 4735static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] = 4736{ 4737 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */ 4738 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */ 4739 0xf0, 0xf4, /* jmp (a0) */ 4740}; 4741 4742static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] = 4743{ 4744 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */ 4745 0xf0, 0xf4, /* jmp (a0) */ 4746 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */ 4747 0xdc, 0, 0, 0, 0, /* jmp .plt0 */ 4748}; 4749 4750static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] = 4751{ 4752 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */ 4753 0xf0, 0xf4, /* jmp (a0) */ 4754 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */ 4755 0xf8, 0x22, 8, /* mov (8,a2),a0 */ 4756 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */ 4757 0xf0, 0xf4, /* jmp (a0) */ 4758}; 4759 4760/* Return size of the first PLT entry. */ 4761#define elf_mn10300_sizeof_plt0(info) \ 4762 (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE) 4763 4764/* Return size of a PLT entry. */ 4765#define elf_mn10300_sizeof_plt(info) \ 4766 (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE) 4767 4768/* Return offset of the PLT0 address in an absolute PLT entry. */ 4769#define elf_mn10300_plt_plt0_offset(info) 16 4770 4771/* Return offset of the linker in PLT0 entry. */ 4772#define elf_mn10300_plt0_linker_offset(info) 2 4773 4774/* Return offset of the GOT id in PLT0 entry. */ 4775#define elf_mn10300_plt0_gotid_offset(info) 9 4776 4777/* Return offset of the temporary in PLT entry. */ 4778#define elf_mn10300_plt_temp_offset(info) 8 4779 4780/* Return offset of the symbol in PLT entry. */ 4781#define elf_mn10300_plt_symbol_offset(info) 2 4782 4783/* Return offset of the relocation in PLT entry. */ 4784#define elf_mn10300_plt_reloc_offset(info) 11 4785 4786/* The name of the dynamic interpreter. This is put in the .interp 4787 section. */ 4788 4789#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1" 4790 4791/* Create dynamic sections when linking against a dynamic object. */ 4792 4793static bfd_boolean 4794_bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info) 4795{ 4796 flagword flags; 4797 asection * s; 4798 const struct elf_backend_data * bed = get_elf_backend_data (abfd); 4799 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info); 4800 int ptralign = 0; 4801 4802 switch (bed->s->arch_size) 4803 { 4804 case 32: 4805 ptralign = 2; 4806 break; 4807 4808 case 64: 4809 ptralign = 3; 4810 break; 4811 4812 default: 4813 bfd_set_error (bfd_error_bad_value); 4814 return FALSE; 4815 } 4816 4817 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and 4818 .rel[a].bss sections. */ 4819 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY 4820 | SEC_LINKER_CREATED); 4821 4822 s = bfd_make_section_anyway_with_flags (abfd, 4823 (bed->default_use_rela_p 4824 ? ".rela.plt" : ".rel.plt"), 4825 flags | SEC_READONLY); 4826 htab->root.srelplt = s; 4827 if (s == NULL 4828 || ! bfd_set_section_alignment (abfd, s, ptralign)) 4829 return FALSE; 4830 4831 if (! _bfd_mn10300_elf_create_got_section (abfd, info)) 4832 return FALSE; 4833 4834 if (bed->want_dynbss) 4835 { 4836 /* The .dynbss section is a place to put symbols which are defined 4837 by dynamic objects, are referenced by regular objects, and are 4838 not functions. We must allocate space for them in the process 4839 image and use a R_*_COPY reloc to tell the dynamic linker to 4840 initialize them at run time. The linker script puts the .dynbss 4841 section into the .bss section of the final image. */ 4842 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss", 4843 SEC_ALLOC | SEC_LINKER_CREATED); 4844 if (s == NULL) 4845 return FALSE; 4846 4847 /* The .rel[a].bss section holds copy relocs. This section is not 4848 normally needed. We need to create it here, though, so that the 4849 linker will map it to an output section. We can't just create it 4850 only if we need it, because we will not know whether we need it 4851 until we have seen all the input files, and the first time the 4852 main linker code calls BFD after examining all the input files 4853 (size_dynamic_sections) the input sections have already been 4854 mapped to the output sections. If the section turns out not to 4855 be needed, we can discard it later. We will never need this 4856 section when generating a shared object, since they do not use 4857 copy relocs. */ 4858 if (! bfd_link_pic (info)) 4859 { 4860 s = bfd_make_section_anyway_with_flags (abfd, 4861 (bed->default_use_rela_p 4862 ? ".rela.bss" : ".rel.bss"), 4863 flags | SEC_READONLY); 4864 if (s == NULL 4865 || ! bfd_set_section_alignment (abfd, s, ptralign)) 4866 return FALSE; 4867 } 4868 } 4869 4870 return TRUE; 4871} 4872 4873/* Adjust a symbol defined by a dynamic object and referenced by a 4874 regular object. The current definition is in some section of the 4875 dynamic object, but we're not including those sections. We have to 4876 change the definition to something the rest of the link can 4877 understand. */ 4878 4879static bfd_boolean 4880_bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info, 4881 struct elf_link_hash_entry * h) 4882{ 4883 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info); 4884 bfd * dynobj; 4885 asection * s; 4886 4887 dynobj = htab->root.dynobj; 4888 4889 /* Make sure we know what is going on here. */ 4890 BFD_ASSERT (dynobj != NULL 4891 && (h->needs_plt 4892 || h->u.weakdef != NULL 4893 || (h->def_dynamic 4894 && h->ref_regular 4895 && !h->def_regular))); 4896 4897 /* If this is a function, put it in the procedure linkage table. We 4898 will fill in the contents of the procedure linkage table later, 4899 when we know the address of the .got section. */ 4900 if (h->type == STT_FUNC 4901 || h->needs_plt) 4902 { 4903 if (! bfd_link_pic (info) 4904 && !h->def_dynamic 4905 && !h->ref_dynamic) 4906 { 4907 /* This case can occur if we saw a PLT reloc in an input 4908 file, but the symbol was never referred to by a dynamic 4909 object. In such a case, we don't actually need to build 4910 a procedure linkage table, and we can just do a REL32 4911 reloc instead. */ 4912 BFD_ASSERT (h->needs_plt); 4913 return TRUE; 4914 } 4915 4916 /* Make sure this symbol is output as a dynamic symbol. */ 4917 if (h->dynindx == -1) 4918 { 4919 if (! bfd_elf_link_record_dynamic_symbol (info, h)) 4920 return FALSE; 4921 } 4922 4923 s = htab->root.splt; 4924 BFD_ASSERT (s != NULL); 4925 4926 /* If this is the first .plt entry, make room for the special 4927 first entry. */ 4928 if (s->size == 0) 4929 s->size += elf_mn10300_sizeof_plt0 (info); 4930 4931 /* If this symbol is not defined in a regular file, and we are 4932 not generating a shared library, then set the symbol to this 4933 location in the .plt. This is required to make function 4934 pointers compare as equal between the normal executable and 4935 the shared library. */ 4936 if (! bfd_link_pic (info) 4937 && !h->def_regular) 4938 { 4939 h->root.u.def.section = s; 4940 h->root.u.def.value = s->size; 4941 } 4942 4943 h->plt.offset = s->size; 4944 4945 /* Make room for this entry. */ 4946 s->size += elf_mn10300_sizeof_plt (info); 4947 4948 /* We also need to make an entry in the .got.plt section, which 4949 will be placed in the .got section by the linker script. */ 4950 s = htab->root.sgotplt; 4951 BFD_ASSERT (s != NULL); 4952 s->size += 4; 4953 4954 /* We also need to make an entry in the .rela.plt section. */ 4955 s = htab->root.srelplt; 4956 BFD_ASSERT (s != NULL); 4957 s->size += sizeof (Elf32_External_Rela); 4958 4959 return TRUE; 4960 } 4961 4962 /* If this is a weak symbol, and there is a real definition, the 4963 processor independent code will have arranged for us to see the 4964 real definition first, and we can just use the same value. */ 4965 if (h->u.weakdef != NULL) 4966 { 4967 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined 4968 || h->u.weakdef->root.type == bfd_link_hash_defweak); 4969 h->root.u.def.section = h->u.weakdef->root.u.def.section; 4970 h->root.u.def.value = h->u.weakdef->root.u.def.value; 4971 return TRUE; 4972 } 4973 4974 /* This is a reference to a symbol defined by a dynamic object which 4975 is not a function. */ 4976 4977 /* If we are creating a shared library, we must presume that the 4978 only references to the symbol are via the global offset table. 4979 For such cases we need not do anything here; the relocations will 4980 be handled correctly by relocate_section. */ 4981 if (bfd_link_pic (info)) 4982 return TRUE; 4983 4984 /* If there are no references to this symbol that do not use the 4985 GOT, we don't need to generate a copy reloc. */ 4986 if (!h->non_got_ref) 4987 return TRUE; 4988 4989 /* We must allocate the symbol in our .dynbss section, which will 4990 become part of the .bss section of the executable. There will be 4991 an entry for this symbol in the .dynsym section. The dynamic 4992 object will contain position independent code, so all references 4993 from the dynamic object to this symbol will go through the global 4994 offset table. The dynamic linker will use the .dynsym entry to 4995 determine the address it must put in the global offset table, so 4996 both the dynamic object and the regular object will refer to the 4997 same memory location for the variable. */ 4998 4999 s = bfd_get_linker_section (dynobj, ".dynbss"); 5000 BFD_ASSERT (s != NULL); 5001 5002 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to 5003 copy the initial value out of the dynamic object and into the 5004 runtime process image. We need to remember the offset into the 5005 .rela.bss section we are going to use. */ 5006 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0) 5007 { 5008 asection * srel; 5009 5010 srel = bfd_get_linker_section (dynobj, ".rela.bss"); 5011 BFD_ASSERT (srel != NULL); 5012 srel->size += sizeof (Elf32_External_Rela); 5013 h->needs_copy = 1; 5014 } 5015 5016 return _bfd_elf_adjust_dynamic_copy (info, h, s); 5017} 5018 5019/* Set the sizes of the dynamic sections. */ 5020 5021static bfd_boolean 5022_bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd, 5023 struct bfd_link_info * info) 5024{ 5025 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info); 5026 bfd * dynobj; 5027 asection * s; 5028 bfd_boolean plt; 5029 bfd_boolean relocs; 5030 bfd_boolean reltext; 5031 5032 dynobj = htab->root.dynobj; 5033 BFD_ASSERT (dynobj != NULL); 5034 5035 if (elf_hash_table (info)->dynamic_sections_created) 5036 { 5037 /* Set the contents of the .interp section to the interpreter. */ 5038 if (bfd_link_executable (info) && !info->nointerp) 5039 { 5040 s = bfd_get_linker_section (dynobj, ".interp"); 5041 BFD_ASSERT (s != NULL); 5042 s->size = sizeof ELF_DYNAMIC_INTERPRETER; 5043 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; 5044 } 5045 } 5046 else 5047 { 5048 /* We may have created entries in the .rela.got section. 5049 However, if we are not creating the dynamic sections, we will 5050 not actually use these entries. Reset the size of .rela.got, 5051 which will cause it to get stripped from the output file 5052 below. */ 5053 s = htab->root.sgot; 5054 if (s != NULL) 5055 s->size = 0; 5056 } 5057 5058 if (htab->tls_ldm_got.refcount > 0) 5059 { 5060 s = htab->root.srelgot; 5061 BFD_ASSERT (s != NULL); 5062 s->size += sizeof (Elf32_External_Rela); 5063 } 5064 5065 /* The check_relocs and adjust_dynamic_symbol entry points have 5066 determined the sizes of the various dynamic sections. Allocate 5067 memory for them. */ 5068 plt = FALSE; 5069 relocs = FALSE; 5070 reltext = FALSE; 5071 for (s = dynobj->sections; s != NULL; s = s->next) 5072 { 5073 const char * name; 5074 5075 if ((s->flags & SEC_LINKER_CREATED) == 0) 5076 continue; 5077 5078 /* It's OK to base decisions on the section name, because none 5079 of the dynobj section names depend upon the input files. */ 5080 name = bfd_get_section_name (dynobj, s); 5081 5082 if (streq (name, ".plt")) 5083 { 5084 /* Remember whether there is a PLT. */ 5085 plt = s->size != 0; 5086 } 5087 else if (CONST_STRNEQ (name, ".rela")) 5088 { 5089 if (s->size != 0) 5090 { 5091 asection * target; 5092 5093 /* Remember whether there are any reloc sections other 5094 than .rela.plt. */ 5095 if (! streq (name, ".rela.plt")) 5096 { 5097 const char * outname; 5098 5099 relocs = TRUE; 5100 5101 /* If this relocation section applies to a read only 5102 section, then we probably need a DT_TEXTREL 5103 entry. The entries in the .rela.plt section 5104 really apply to the .got section, which we 5105 created ourselves and so know is not readonly. */ 5106 outname = bfd_get_section_name (output_bfd, 5107 s->output_section); 5108 target = bfd_get_section_by_name (output_bfd, outname + 5); 5109 if (target != NULL 5110 && (target->flags & SEC_READONLY) != 0 5111 && (target->flags & SEC_ALLOC) != 0) 5112 reltext = TRUE; 5113 } 5114 5115 /* We use the reloc_count field as a counter if we need 5116 to copy relocs into the output file. */ 5117 s->reloc_count = 0; 5118 } 5119 } 5120 else if (! CONST_STRNEQ (name, ".got") 5121 && ! streq (name, ".dynbss")) 5122 /* It's not one of our sections, so don't allocate space. */ 5123 continue; 5124 5125 if (s->size == 0) 5126 { 5127 /* If we don't need this section, strip it from the 5128 output file. This is mostly to handle .rela.bss and 5129 .rela.plt. We must create both sections in 5130 create_dynamic_sections, because they must be created 5131 before the linker maps input sections to output 5132 sections. The linker does that before 5133 adjust_dynamic_symbol is called, and it is that 5134 function which decides whether anything needs to go 5135 into these sections. */ 5136 s->flags |= SEC_EXCLUDE; 5137 continue; 5138 } 5139 5140 if ((s->flags & SEC_HAS_CONTENTS) == 0) 5141 continue; 5142 5143 /* Allocate memory for the section contents. We use bfd_zalloc 5144 here in case unused entries are not reclaimed before the 5145 section's contents are written out. This should not happen, 5146 but this way if it does, we get a R_MN10300_NONE reloc 5147 instead of garbage. */ 5148 s->contents = bfd_zalloc (dynobj, s->size); 5149 if (s->contents == NULL) 5150 return FALSE; 5151 } 5152 5153 if (elf_hash_table (info)->dynamic_sections_created) 5154 { 5155 /* Add some entries to the .dynamic section. We fill in the 5156 values later, in _bfd_mn10300_elf_finish_dynamic_sections, 5157 but we must add the entries now so that we get the correct 5158 size for the .dynamic section. The DT_DEBUG entry is filled 5159 in by the dynamic linker and used by the debugger. */ 5160 if (! bfd_link_pic (info)) 5161 { 5162 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0)) 5163 return FALSE; 5164 } 5165 5166 if (plt) 5167 { 5168 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0) 5169 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0) 5170 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA) 5171 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0)) 5172 return FALSE; 5173 } 5174 5175 if (relocs) 5176 { 5177 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0) 5178 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0) 5179 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT, 5180 sizeof (Elf32_External_Rela))) 5181 return FALSE; 5182 } 5183 5184 if (reltext) 5185 { 5186 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0)) 5187 return FALSE; 5188 } 5189 } 5190 5191 return TRUE; 5192} 5193 5194/* Finish up dynamic symbol handling. We set the contents of various 5195 dynamic sections here. */ 5196 5197static bfd_boolean 5198_bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd, 5199 struct bfd_link_info * info, 5200 struct elf_link_hash_entry * h, 5201 Elf_Internal_Sym * sym) 5202{ 5203 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info); 5204 bfd * dynobj; 5205 5206 dynobj = htab->root.dynobj; 5207 5208 if (h->plt.offset != (bfd_vma) -1) 5209 { 5210 asection * splt; 5211 asection * sgot; 5212 asection * srel; 5213 bfd_vma plt_index; 5214 bfd_vma got_offset; 5215 Elf_Internal_Rela rel; 5216 5217 /* This symbol has an entry in the procedure linkage table. Set 5218 it up. */ 5219 5220 BFD_ASSERT (h->dynindx != -1); 5221 5222 splt = htab->root.splt; 5223 sgot = htab->root.sgotplt; 5224 srel = htab->root.srelplt; 5225 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL); 5226 5227 /* Get the index in the procedure linkage table which 5228 corresponds to this symbol. This is the index of this symbol 5229 in all the symbols for which we are making plt entries. The 5230 first entry in the procedure linkage table is reserved. */ 5231 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info)) 5232 / elf_mn10300_sizeof_plt (info)); 5233 5234 /* Get the offset into the .got table of the entry that 5235 corresponds to this function. Each .got entry is 4 bytes. 5236 The first three are reserved. */ 5237 got_offset = (plt_index + 3) * 4; 5238 5239 /* Fill in the entry in the procedure linkage table. */ 5240 if (! bfd_link_pic (info)) 5241 { 5242 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry, 5243 elf_mn10300_sizeof_plt (info)); 5244 bfd_put_32 (output_bfd, 5245 (sgot->output_section->vma 5246 + sgot->output_offset 5247 + got_offset), 5248 (splt->contents + h->plt.offset 5249 + elf_mn10300_plt_symbol_offset (info))); 5250 5251 bfd_put_32 (output_bfd, 5252 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)), 5253 (splt->contents + h->plt.offset 5254 + elf_mn10300_plt_plt0_offset (info))); 5255 } 5256 else 5257 { 5258 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry, 5259 elf_mn10300_sizeof_plt (info)); 5260 5261 bfd_put_32 (output_bfd, got_offset, 5262 (splt->contents + h->plt.offset 5263 + elf_mn10300_plt_symbol_offset (info))); 5264 } 5265 5266 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela), 5267 (splt->contents + h->plt.offset 5268 + elf_mn10300_plt_reloc_offset (info))); 5269 5270 /* Fill in the entry in the global offset table. */ 5271 bfd_put_32 (output_bfd, 5272 (splt->output_section->vma 5273 + splt->output_offset 5274 + h->plt.offset 5275 + elf_mn10300_plt_temp_offset (info)), 5276 sgot->contents + got_offset); 5277 5278 /* Fill in the entry in the .rela.plt section. */ 5279 rel.r_offset = (sgot->output_section->vma 5280 + sgot->output_offset 5281 + got_offset); 5282 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT); 5283 rel.r_addend = 0; 5284 bfd_elf32_swap_reloca_out (output_bfd, &rel, 5285 (bfd_byte *) ((Elf32_External_Rela *) srel->contents 5286 + plt_index)); 5287 5288 if (!h->def_regular) 5289 /* Mark the symbol as undefined, rather than as defined in 5290 the .plt section. Leave the value alone. */ 5291 sym->st_shndx = SHN_UNDEF; 5292 } 5293 5294 if (h->got.offset != (bfd_vma) -1) 5295 { 5296 asection * sgot; 5297 asection * srel; 5298 Elf_Internal_Rela rel; 5299 5300 /* This symbol has an entry in the global offset table. Set it up. */ 5301 sgot = htab->root.sgot; 5302 srel = htab->root.srelgot; 5303 BFD_ASSERT (sgot != NULL && srel != NULL); 5304 5305 rel.r_offset = (sgot->output_section->vma 5306 + sgot->output_offset 5307 + (h->got.offset & ~1)); 5308 5309 switch (elf_mn10300_hash_entry (h)->tls_type) 5310 { 5311 case GOT_TLS_GD: 5312 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset); 5313 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset + 4); 5314 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPMOD); 5315 rel.r_addend = 0; 5316 bfd_elf32_swap_reloca_out (output_bfd, & rel, 5317 (bfd_byte *) ((Elf32_External_Rela *) srel->contents 5318 + srel->reloc_count)); 5319 ++ srel->reloc_count; 5320 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPOFF); 5321 rel.r_offset += 4; 5322 rel.r_addend = 0; 5323 break; 5324 5325 case GOT_TLS_IE: 5326 /* We originally stored the addend in the GOT, but at this 5327 point, we want to move it to the reloc instead as that's 5328 where the dynamic linker wants it. */ 5329 rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + h->got.offset); 5330 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset); 5331 if (h->dynindx == -1) 5332 rel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_TPOFF); 5333 else 5334 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_TPOFF); 5335 break; 5336 5337 default: 5338 /* If this is a -Bsymbolic link, and the symbol is defined 5339 locally, we just want to emit a RELATIVE reloc. Likewise if 5340 the symbol was forced to be local because of a version file. 5341 The entry in the global offset table will already have been 5342 initialized in the relocate_section function. */ 5343 if (bfd_link_pic (info) 5344 && (info->symbolic || h->dynindx == -1) 5345 && h->def_regular) 5346 { 5347 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE); 5348 rel.r_addend = (h->root.u.def.value 5349 + h->root.u.def.section->output_section->vma 5350 + h->root.u.def.section->output_offset); 5351 } 5352 else 5353 { 5354 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset); 5355 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT); 5356 rel.r_addend = 0; 5357 } 5358 } 5359 5360 if (ELF32_R_TYPE (rel.r_info) != R_MN10300_NONE) 5361 { 5362 bfd_elf32_swap_reloca_out (output_bfd, &rel, 5363 (bfd_byte *) ((Elf32_External_Rela *) srel->contents 5364 + srel->reloc_count)); 5365 ++ srel->reloc_count; 5366 } 5367 } 5368 5369 if (h->needs_copy) 5370 { 5371 asection * s; 5372 Elf_Internal_Rela rel; 5373 5374 /* This symbol needs a copy reloc. Set it up. */ 5375 BFD_ASSERT (h->dynindx != -1 5376 && (h->root.type == bfd_link_hash_defined 5377 || h->root.type == bfd_link_hash_defweak)); 5378 5379 s = bfd_get_linker_section (dynobj, ".rela.bss"); 5380 BFD_ASSERT (s != NULL); 5381 5382 rel.r_offset = (h->root.u.def.value 5383 + h->root.u.def.section->output_section->vma 5384 + h->root.u.def.section->output_offset); 5385 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY); 5386 rel.r_addend = 0; 5387 bfd_elf32_swap_reloca_out (output_bfd, & rel, 5388 (bfd_byte *) ((Elf32_External_Rela *) s->contents 5389 + s->reloc_count)); 5390 ++ s->reloc_count; 5391 } 5392 5393 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ 5394 if (h == elf_hash_table (info)->hdynamic 5395 || h == elf_hash_table (info)->hgot) 5396 sym->st_shndx = SHN_ABS; 5397 5398 return TRUE; 5399} 5400 5401/* Finish up the dynamic sections. */ 5402 5403static bfd_boolean 5404_bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd, 5405 struct bfd_link_info * info) 5406{ 5407 bfd * dynobj; 5408 asection * sgot; 5409 asection * sdyn; 5410 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info); 5411 5412 dynobj = htab->root.dynobj; 5413 sgot = htab->root.sgotplt; 5414 BFD_ASSERT (sgot != NULL); 5415 sdyn = bfd_get_linker_section (dynobj, ".dynamic"); 5416 5417 if (elf_hash_table (info)->dynamic_sections_created) 5418 { 5419 asection * splt; 5420 Elf32_External_Dyn * dyncon; 5421 Elf32_External_Dyn * dynconend; 5422 5423 BFD_ASSERT (sdyn != NULL); 5424 5425 dyncon = (Elf32_External_Dyn *) sdyn->contents; 5426 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size); 5427 5428 for (; dyncon < dynconend; dyncon++) 5429 { 5430 Elf_Internal_Dyn dyn; 5431 asection * s; 5432 5433 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); 5434 5435 switch (dyn.d_tag) 5436 { 5437 default: 5438 break; 5439 5440 case DT_PLTGOT: 5441 s = htab->root.sgot; 5442 goto get_vma; 5443 5444 case DT_JMPREL: 5445 s = htab->root.srelplt; 5446 get_vma: 5447 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset; 5448 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 5449 break; 5450 5451 case DT_PLTRELSZ: 5452 s = htab->root.srelplt; 5453 dyn.d_un.d_val = s->size; 5454 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); 5455 break; 5456 } 5457 } 5458 5459 /* Fill in the first entry in the procedure linkage table. */ 5460 splt = htab->root.splt; 5461 if (splt && splt->size > 0) 5462 { 5463 if (bfd_link_pic (info)) 5464 { 5465 memcpy (splt->contents, elf_mn10300_pic_plt_entry, 5466 elf_mn10300_sizeof_plt (info)); 5467 } 5468 else 5469 { 5470 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE); 5471 bfd_put_32 (output_bfd, 5472 sgot->output_section->vma + sgot->output_offset + 4, 5473 splt->contents + elf_mn10300_plt0_gotid_offset (info)); 5474 bfd_put_32 (output_bfd, 5475 sgot->output_section->vma + sgot->output_offset + 8, 5476 splt->contents + elf_mn10300_plt0_linker_offset (info)); 5477 } 5478 5479 /* UnixWare sets the entsize of .plt to 4, although that doesn't 5480 really seem like the right value. */ 5481 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4; 5482 5483 /* UnixWare sets the entsize of .plt to 4, but this is incorrect 5484 as it means that the size of the PLT0 section (15 bytes) is not 5485 a multiple of the sh_entsize. Some ELF tools flag this as an 5486 error. We could pad PLT0 to 16 bytes, but that would introduce 5487 compatibilty issues with previous toolchains, so instead we 5488 just set the entry size to 1. */ 5489 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 1; 5490 } 5491 } 5492 5493 /* Fill in the first three entries in the global offset table. */ 5494 if (sgot->size > 0) 5495 { 5496 if (sdyn == NULL) 5497 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents); 5498 else 5499 bfd_put_32 (output_bfd, 5500 sdyn->output_section->vma + sdyn->output_offset, 5501 sgot->contents); 5502 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4); 5503 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8); 5504 } 5505 5506 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; 5507 5508 return TRUE; 5509} 5510 5511/* Classify relocation types, such that combreloc can sort them 5512 properly. */ 5513 5514static enum elf_reloc_type_class 5515_bfd_mn10300_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED, 5516 const asection *rel_sec ATTRIBUTE_UNUSED, 5517 const Elf_Internal_Rela *rela) 5518{ 5519 switch ((int) ELF32_R_TYPE (rela->r_info)) 5520 { 5521 case R_MN10300_RELATIVE: return reloc_class_relative; 5522 case R_MN10300_JMP_SLOT: return reloc_class_plt; 5523 case R_MN10300_COPY: return reloc_class_copy; 5524 default: return reloc_class_normal; 5525 } 5526} 5527 5528/* Allocate space for an MN10300 extension to the bfd elf data structure. */ 5529 5530static bfd_boolean 5531mn10300_elf_mkobject (bfd *abfd) 5532{ 5533 return bfd_elf_allocate_object (abfd, sizeof (struct elf_mn10300_obj_tdata), 5534 MN10300_ELF_DATA); 5535} 5536 5537#define bfd_elf32_mkobject mn10300_elf_mkobject 5538 5539#ifndef ELF_ARCH 5540#define TARGET_LITTLE_SYM mn10300_elf32_vec 5541#define TARGET_LITTLE_NAME "elf32-mn10300" 5542#define ELF_ARCH bfd_arch_mn10300 5543#define ELF_TARGET_ID MN10300_ELF_DATA 5544#define ELF_MACHINE_CODE EM_MN10300 5545#define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300 5546#define ELF_MAXPAGESIZE 0x1000 5547#endif 5548 5549#define elf_info_to_howto mn10300_info_to_howto 5550#define elf_info_to_howto_rel 0 5551#define elf_backend_can_gc_sections 1 5552#define elf_backend_rela_normal 1 5553#define elf_backend_check_relocs mn10300_elf_check_relocs 5554#define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook 5555#define elf_backend_relocate_section mn10300_elf_relocate_section 5556#define bfd_elf32_bfd_relax_section mn10300_elf_relax_section 5557#define bfd_elf32_bfd_get_relocated_section_contents \ 5558 mn10300_elf_get_relocated_section_contents 5559#define bfd_elf32_bfd_link_hash_table_create \ 5560 elf32_mn10300_link_hash_table_create 5561 5562#ifndef elf_symbol_leading_char 5563#define elf_symbol_leading_char '_' 5564#endif 5565 5566/* So we can set bits in e_flags. */ 5567#define elf_backend_final_write_processing \ 5568 _bfd_mn10300_elf_final_write_processing 5569#define elf_backend_object_p _bfd_mn10300_elf_object_p 5570 5571#define bfd_elf32_bfd_merge_private_bfd_data \ 5572 _bfd_mn10300_elf_merge_private_bfd_data 5573 5574#define elf_backend_can_gc_sections 1 5575#define elf_backend_create_dynamic_sections \ 5576 _bfd_mn10300_elf_create_dynamic_sections 5577#define elf_backend_adjust_dynamic_symbol \ 5578 _bfd_mn10300_elf_adjust_dynamic_symbol 5579#define elf_backend_size_dynamic_sections \ 5580 _bfd_mn10300_elf_size_dynamic_sections 5581#define elf_backend_omit_section_dynsym \ 5582 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true) 5583#define elf_backend_finish_dynamic_symbol \ 5584 _bfd_mn10300_elf_finish_dynamic_symbol 5585#define elf_backend_finish_dynamic_sections \ 5586 _bfd_mn10300_elf_finish_dynamic_sections 5587#define elf_backend_copy_indirect_symbol \ 5588 _bfd_mn10300_copy_indirect_symbol 5589#define elf_backend_reloc_type_class \ 5590 _bfd_mn10300_elf_reloc_type_class 5591 5592#define elf_backend_want_got_plt 1 5593#define elf_backend_plt_readonly 1 5594#define elf_backend_want_plt_sym 0 5595#define elf_backend_got_header_size 12 5596#define elf_backend_dtrel_excludes_plt 1 5597 5598#include "elf32-target.h" 5599