1 /* AVR-specific support for 32-bit ELF 2 Copyright (C) 1999-2016 Free Software Foundation, Inc. 3 Contributed by Denis Chertykov <denisc (at) overta.ru> 4 5 This file is part of BFD, the Binary File Descriptor library. 6 7 This program is free software; you can redistribute it and/or modify 8 it under the terms of the GNU General Public License as published by 9 the Free Software Foundation; either version 3 of the License, or 10 (at your option) any later version. 11 12 This program is distributed in the hope that it will be useful, 13 but WITHOUT ANY WARRANTY; without even the implied warranty of 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 GNU General Public License for more details. 16 17 You should have received a copy of the GNU General Public License 18 along with this program; if not, write to the Free Software 19 Foundation, Inc., 51 Franklin Street - Fifth Floor, 20 Boston, MA 02110-1301, USA. */ 21 22 #include "sysdep.h" 23 #include "bfd.h" 24 #include "libbfd.h" 25 #include "elf-bfd.h" 26 #include "elf/avr.h" 27 #include "elf32-avr.h" 28 #include "bfd_stdint.h" 29 30 /* Enable debugging printout at stdout with this variable. */ 31 static bfd_boolean debug_relax = FALSE; 32 33 /* Enable debugging printout at stdout with this variable. */ 34 static bfd_boolean debug_stubs = FALSE; 35 36 static bfd_reloc_status_type 37 bfd_elf_avr_diff_reloc (bfd *, arelent *, asymbol *, void *, 38 asection *, bfd *, char **); 39 40 /* Hash table initialization and handling. Code is taken from the hppa port 41 and adapted to the needs of AVR. */ 42 43 /* We use two hash tables to hold information for linking avr objects. 44 45 The first is the elf32_avr_link_hash_table which is derived from the 46 stanard ELF linker hash table. We use this as a place to attach the other 47 hash table and some static information. 48 49 The second is the stub hash table which is derived from the base BFD 50 hash table. The stub hash table holds the information on the linker 51 stubs. */ 52 53 struct elf32_avr_stub_hash_entry 54 { 55 /* Base hash table entry structure. */ 56 struct bfd_hash_entry bh_root; 57 58 /* Offset within stub_sec of the beginning of this stub. */ 59 bfd_vma stub_offset; 60 61 /* Given the symbol's value and its section we can determine its final 62 value when building the stubs (so the stub knows where to jump). */ 63 bfd_vma target_value; 64 65 /* This way we could mark stubs to be no longer necessary. */ 66 bfd_boolean is_actually_needed; 67 }; 68 69 struct elf32_avr_link_hash_table 70 { 71 /* The main hash table. */ 72 struct elf_link_hash_table etab; 73 74 /* The stub hash table. */ 75 struct bfd_hash_table bstab; 76 77 bfd_boolean no_stubs; 78 79 /* Linker stub bfd. */ 80 bfd *stub_bfd; 81 82 /* The stub section. */ 83 asection *stub_sec; 84 85 /* Usually 0, unless we are generating code for a bootloader. Will 86 be initialized by elf32_avr_size_stubs to the vma offset of the 87 output section associated with the stub section. */ 88 bfd_vma vector_base; 89 90 /* Assorted information used by elf32_avr_size_stubs. */ 91 unsigned int bfd_count; 92 unsigned int top_index; 93 asection ** input_list; 94 Elf_Internal_Sym ** all_local_syms; 95 96 /* Tables for mapping vma beyond the 128k boundary to the address of the 97 corresponding stub. (AMT) 98 "amt_max_entry_cnt" reflects the number of entries that memory is allocated 99 for in the "amt_stub_offsets" and "amt_destination_addr" arrays. 100 "amt_entry_cnt" informs how many of these entries actually contain 101 useful data. */ 102 unsigned int amt_entry_cnt; 103 unsigned int amt_max_entry_cnt; 104 bfd_vma * amt_stub_offsets; 105 bfd_vma * amt_destination_addr; 106 }; 107 108 /* Various hash macros and functions. */ 109 #define avr_link_hash_table(p) \ 110 /* PR 3874: Check that we have an AVR style hash table before using it. */\ 111 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \ 112 == AVR_ELF_DATA ? ((struct elf32_avr_link_hash_table *) ((p)->hash)) : NULL) 113 114 #define avr_stub_hash_entry(ent) \ 115 ((struct elf32_avr_stub_hash_entry *)(ent)) 116 117 #define avr_stub_hash_lookup(table, string, create, copy) \ 118 ((struct elf32_avr_stub_hash_entry *) \ 119 bfd_hash_lookup ((table), (string), (create), (copy))) 120 121 static reloc_howto_type elf_avr_howto_table[] = 122 { 123 HOWTO (R_AVR_NONE, /* type */ 124 0, /* rightshift */ 125 3, /* size (0 = byte, 1 = short, 2 = long) */ 126 0, /* bitsize */ 127 FALSE, /* pc_relative */ 128 0, /* bitpos */ 129 complain_overflow_dont, /* complain_on_overflow */ 130 bfd_elf_generic_reloc, /* special_function */ 131 "R_AVR_NONE", /* name */ 132 FALSE, /* partial_inplace */ 133 0, /* src_mask */ 134 0, /* dst_mask */ 135 FALSE), /* pcrel_offset */ 136 137 HOWTO (R_AVR_32, /* type */ 138 0, /* rightshift */ 139 2, /* size (0 = byte, 1 = short, 2 = long) */ 140 32, /* bitsize */ 141 FALSE, /* pc_relative */ 142 0, /* bitpos */ 143 complain_overflow_bitfield, /* complain_on_overflow */ 144 bfd_elf_generic_reloc, /* special_function */ 145 "R_AVR_32", /* name */ 146 FALSE, /* partial_inplace */ 147 0xffffffff, /* src_mask */ 148 0xffffffff, /* dst_mask */ 149 FALSE), /* pcrel_offset */ 150 151 /* A 7 bit PC relative relocation. */ 152 HOWTO (R_AVR_7_PCREL, /* type */ 153 1, /* rightshift */ 154 1, /* size (0 = byte, 1 = short, 2 = long) */ 155 7, /* bitsize */ 156 TRUE, /* pc_relative */ 157 3, /* bitpos */ 158 complain_overflow_bitfield, /* complain_on_overflow */ 159 bfd_elf_generic_reloc, /* special_function */ 160 "R_AVR_7_PCREL", /* name */ 161 FALSE, /* partial_inplace */ 162 0xffff, /* src_mask */ 163 0xffff, /* dst_mask */ 164 TRUE), /* pcrel_offset */ 165 166 /* A 13 bit PC relative relocation. */ 167 HOWTO (R_AVR_13_PCREL, /* type */ 168 1, /* rightshift */ 169 1, /* size (0 = byte, 1 = short, 2 = long) */ 170 13, /* bitsize */ 171 TRUE, /* pc_relative */ 172 0, /* bitpos */ 173 complain_overflow_bitfield, /* complain_on_overflow */ 174 bfd_elf_generic_reloc, /* special_function */ 175 "R_AVR_13_PCREL", /* name */ 176 FALSE, /* partial_inplace */ 177 0xfff, /* src_mask */ 178 0xfff, /* dst_mask */ 179 TRUE), /* pcrel_offset */ 180 181 /* A 16 bit absolute relocation. */ 182 HOWTO (R_AVR_16, /* type */ 183 0, /* rightshift */ 184 1, /* size (0 = byte, 1 = short, 2 = long) */ 185 16, /* bitsize */ 186 FALSE, /* pc_relative */ 187 0, /* bitpos */ 188 complain_overflow_dont, /* complain_on_overflow */ 189 bfd_elf_generic_reloc, /* special_function */ 190 "R_AVR_16", /* name */ 191 FALSE, /* partial_inplace */ 192 0xffff, /* src_mask */ 193 0xffff, /* dst_mask */ 194 FALSE), /* pcrel_offset */ 195 196 /* A 16 bit absolute relocation for command address 197 Will be changed when linker stubs are needed. */ 198 HOWTO (R_AVR_16_PM, /* type */ 199 1, /* rightshift */ 200 1, /* size (0 = byte, 1 = short, 2 = long) */ 201 16, /* bitsize */ 202 FALSE, /* pc_relative */ 203 0, /* bitpos */ 204 complain_overflow_bitfield, /* complain_on_overflow */ 205 bfd_elf_generic_reloc, /* special_function */ 206 "R_AVR_16_PM", /* name */ 207 FALSE, /* partial_inplace */ 208 0xffff, /* src_mask */ 209 0xffff, /* dst_mask */ 210 FALSE), /* pcrel_offset */ 211 /* A low 8 bit absolute relocation of 16 bit address. 212 For LDI command. */ 213 HOWTO (R_AVR_LO8_LDI, /* type */ 214 0, /* rightshift */ 215 1, /* size (0 = byte, 1 = short, 2 = long) */ 216 8, /* bitsize */ 217 FALSE, /* pc_relative */ 218 0, /* bitpos */ 219 complain_overflow_dont, /* complain_on_overflow */ 220 bfd_elf_generic_reloc, /* special_function */ 221 "R_AVR_LO8_LDI", /* name */ 222 FALSE, /* partial_inplace */ 223 0xffff, /* src_mask */ 224 0xffff, /* dst_mask */ 225 FALSE), /* pcrel_offset */ 226 /* A high 8 bit absolute relocation of 16 bit address. 227 For LDI command. */ 228 HOWTO (R_AVR_HI8_LDI, /* type */ 229 8, /* rightshift */ 230 1, /* size (0 = byte, 1 = short, 2 = long) */ 231 8, /* bitsize */ 232 FALSE, /* pc_relative */ 233 0, /* bitpos */ 234 complain_overflow_dont, /* complain_on_overflow */ 235 bfd_elf_generic_reloc, /* special_function */ 236 "R_AVR_HI8_LDI", /* name */ 237 FALSE, /* partial_inplace */ 238 0xffff, /* src_mask */ 239 0xffff, /* dst_mask */ 240 FALSE), /* pcrel_offset */ 241 /* A high 6 bit absolute relocation of 22 bit address. 242 For LDI command. As well second most significant 8 bit value of 243 a 32 bit link-time constant. */ 244 HOWTO (R_AVR_HH8_LDI, /* type */ 245 16, /* rightshift */ 246 1, /* size (0 = byte, 1 = short, 2 = long) */ 247 8, /* bitsize */ 248 FALSE, /* pc_relative */ 249 0, /* bitpos */ 250 complain_overflow_dont, /* complain_on_overflow */ 251 bfd_elf_generic_reloc, /* special_function */ 252 "R_AVR_HH8_LDI", /* name */ 253 FALSE, /* partial_inplace */ 254 0xffff, /* src_mask */ 255 0xffff, /* dst_mask */ 256 FALSE), /* pcrel_offset */ 257 /* A negative low 8 bit absolute relocation of 16 bit address. 258 For LDI command. */ 259 HOWTO (R_AVR_LO8_LDI_NEG, /* type */ 260 0, /* rightshift */ 261 1, /* size (0 = byte, 1 = short, 2 = long) */ 262 8, /* bitsize */ 263 FALSE, /* pc_relative */ 264 0, /* bitpos */ 265 complain_overflow_dont, /* complain_on_overflow */ 266 bfd_elf_generic_reloc, /* special_function */ 267 "R_AVR_LO8_LDI_NEG", /* name */ 268 FALSE, /* partial_inplace */ 269 0xffff, /* src_mask */ 270 0xffff, /* dst_mask */ 271 FALSE), /* pcrel_offset */ 272 /* A negative high 8 bit absolute relocation of 16 bit address. 273 For LDI command. */ 274 HOWTO (R_AVR_HI8_LDI_NEG, /* type */ 275 8, /* rightshift */ 276 1, /* size (0 = byte, 1 = short, 2 = long) */ 277 8, /* bitsize */ 278 FALSE, /* pc_relative */ 279 0, /* bitpos */ 280 complain_overflow_dont, /* complain_on_overflow */ 281 bfd_elf_generic_reloc, /* special_function */ 282 "R_AVR_HI8_LDI_NEG", /* name */ 283 FALSE, /* partial_inplace */ 284 0xffff, /* src_mask */ 285 0xffff, /* dst_mask */ 286 FALSE), /* pcrel_offset */ 287 /* A negative high 6 bit absolute relocation of 22 bit address. 288 For LDI command. */ 289 HOWTO (R_AVR_HH8_LDI_NEG, /* type */ 290 16, /* rightshift */ 291 1, /* size (0 = byte, 1 = short, 2 = long) */ 292 8, /* bitsize */ 293 FALSE, /* pc_relative */ 294 0, /* bitpos */ 295 complain_overflow_dont, /* complain_on_overflow */ 296 bfd_elf_generic_reloc, /* special_function */ 297 "R_AVR_HH8_LDI_NEG", /* name */ 298 FALSE, /* partial_inplace */ 299 0xffff, /* src_mask */ 300 0xffff, /* dst_mask */ 301 FALSE), /* pcrel_offset */ 302 /* A low 8 bit absolute relocation of 24 bit program memory address. 303 For LDI command. Will not be changed when linker stubs are needed. */ 304 HOWTO (R_AVR_LO8_LDI_PM, /* type */ 305 1, /* rightshift */ 306 1, /* size (0 = byte, 1 = short, 2 = long) */ 307 8, /* bitsize */ 308 FALSE, /* pc_relative */ 309 0, /* bitpos */ 310 complain_overflow_dont, /* complain_on_overflow */ 311 bfd_elf_generic_reloc, /* special_function */ 312 "R_AVR_LO8_LDI_PM", /* name */ 313 FALSE, /* partial_inplace */ 314 0xffff, /* src_mask */ 315 0xffff, /* dst_mask */ 316 FALSE), /* pcrel_offset */ 317 /* A low 8 bit absolute relocation of 24 bit program memory address. 318 For LDI command. Will not be changed when linker stubs are needed. */ 319 HOWTO (R_AVR_HI8_LDI_PM, /* type */ 320 9, /* rightshift */ 321 1, /* size (0 = byte, 1 = short, 2 = long) */ 322 8, /* bitsize */ 323 FALSE, /* pc_relative */ 324 0, /* bitpos */ 325 complain_overflow_dont, /* complain_on_overflow */ 326 bfd_elf_generic_reloc, /* special_function */ 327 "R_AVR_HI8_LDI_PM", /* name */ 328 FALSE, /* partial_inplace */ 329 0xffff, /* src_mask */ 330 0xffff, /* dst_mask */ 331 FALSE), /* pcrel_offset */ 332 /* A low 8 bit absolute relocation of 24 bit program memory address. 333 For LDI command. Will not be changed when linker stubs are needed. */ 334 HOWTO (R_AVR_HH8_LDI_PM, /* type */ 335 17, /* rightshift */ 336 1, /* size (0 = byte, 1 = short, 2 = long) */ 337 8, /* bitsize */ 338 FALSE, /* pc_relative */ 339 0, /* bitpos */ 340 complain_overflow_dont, /* complain_on_overflow */ 341 bfd_elf_generic_reloc, /* special_function */ 342 "R_AVR_HH8_LDI_PM", /* name */ 343 FALSE, /* partial_inplace */ 344 0xffff, /* src_mask */ 345 0xffff, /* dst_mask */ 346 FALSE), /* pcrel_offset */ 347 /* A low 8 bit absolute relocation of 24 bit program memory address. 348 For LDI command. Will not be changed when linker stubs are needed. */ 349 HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */ 350 1, /* rightshift */ 351 1, /* size (0 = byte, 1 = short, 2 = long) */ 352 8, /* bitsize */ 353 FALSE, /* pc_relative */ 354 0, /* bitpos */ 355 complain_overflow_dont, /* complain_on_overflow */ 356 bfd_elf_generic_reloc, /* special_function */ 357 "R_AVR_LO8_LDI_PM_NEG", /* name */ 358 FALSE, /* partial_inplace */ 359 0xffff, /* src_mask */ 360 0xffff, /* dst_mask */ 361 FALSE), /* pcrel_offset */ 362 /* A low 8 bit absolute relocation of 24 bit program memory address. 363 For LDI command. Will not be changed when linker stubs are needed. */ 364 HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */ 365 9, /* rightshift */ 366 1, /* size (0 = byte, 1 = short, 2 = long) */ 367 8, /* bitsize */ 368 FALSE, /* pc_relative */ 369 0, /* bitpos */ 370 complain_overflow_dont, /* complain_on_overflow */ 371 bfd_elf_generic_reloc, /* special_function */ 372 "R_AVR_HI8_LDI_PM_NEG", /* name */ 373 FALSE, /* partial_inplace */ 374 0xffff, /* src_mask */ 375 0xffff, /* dst_mask */ 376 FALSE), /* pcrel_offset */ 377 /* A low 8 bit absolute relocation of 24 bit program memory address. 378 For LDI command. Will not be changed when linker stubs are needed. */ 379 HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */ 380 17, /* rightshift */ 381 1, /* size (0 = byte, 1 = short, 2 = long) */ 382 8, /* bitsize */ 383 FALSE, /* pc_relative */ 384 0, /* bitpos */ 385 complain_overflow_dont, /* complain_on_overflow */ 386 bfd_elf_generic_reloc, /* special_function */ 387 "R_AVR_HH8_LDI_PM_NEG", /* name */ 388 FALSE, /* partial_inplace */ 389 0xffff, /* src_mask */ 390 0xffff, /* dst_mask */ 391 FALSE), /* pcrel_offset */ 392 /* Relocation for CALL command in ATmega. */ 393 HOWTO (R_AVR_CALL, /* type */ 394 1, /* rightshift */ 395 2, /* size (0 = byte, 1 = short, 2 = long) */ 396 23, /* bitsize */ 397 FALSE, /* pc_relative */ 398 0, /* bitpos */ 399 complain_overflow_dont,/* complain_on_overflow */ 400 bfd_elf_generic_reloc, /* special_function */ 401 "R_AVR_CALL", /* name */ 402 FALSE, /* partial_inplace */ 403 0xffffffff, /* src_mask */ 404 0xffffffff, /* dst_mask */ 405 FALSE), /* pcrel_offset */ 406 /* A 16 bit absolute relocation of 16 bit address. 407 For LDI command. */ 408 HOWTO (R_AVR_LDI, /* type */ 409 0, /* rightshift */ 410 1, /* size (0 = byte, 1 = short, 2 = long) */ 411 16, /* bitsize */ 412 FALSE, /* pc_relative */ 413 0, /* bitpos */ 414 complain_overflow_dont,/* complain_on_overflow */ 415 bfd_elf_generic_reloc, /* special_function */ 416 "R_AVR_LDI", /* name */ 417 FALSE, /* partial_inplace */ 418 0xffff, /* src_mask */ 419 0xffff, /* dst_mask */ 420 FALSE), /* pcrel_offset */ 421 /* A 6 bit absolute relocation of 6 bit offset. 422 For ldd/sdd command. */ 423 HOWTO (R_AVR_6, /* type */ 424 0, /* rightshift */ 425 0, /* size (0 = byte, 1 = short, 2 = long) */ 426 6, /* bitsize */ 427 FALSE, /* pc_relative */ 428 0, /* bitpos */ 429 complain_overflow_dont,/* complain_on_overflow */ 430 bfd_elf_generic_reloc, /* special_function */ 431 "R_AVR_6", /* name */ 432 FALSE, /* partial_inplace */ 433 0xffff, /* src_mask */ 434 0xffff, /* dst_mask */ 435 FALSE), /* pcrel_offset */ 436 /* A 6 bit absolute relocation of 6 bit offset. 437 For sbiw/adiw command. */ 438 HOWTO (R_AVR_6_ADIW, /* type */ 439 0, /* rightshift */ 440 0, /* size (0 = byte, 1 = short, 2 = long) */ 441 6, /* bitsize */ 442 FALSE, /* pc_relative */ 443 0, /* bitpos */ 444 complain_overflow_dont,/* complain_on_overflow */ 445 bfd_elf_generic_reloc, /* special_function */ 446 "R_AVR_6_ADIW", /* name */ 447 FALSE, /* partial_inplace */ 448 0xffff, /* src_mask */ 449 0xffff, /* dst_mask */ 450 FALSE), /* pcrel_offset */ 451 /* Most significant 8 bit value of a 32 bit link-time constant. */ 452 HOWTO (R_AVR_MS8_LDI, /* type */ 453 24, /* rightshift */ 454 1, /* size (0 = byte, 1 = short, 2 = long) */ 455 8, /* bitsize */ 456 FALSE, /* pc_relative */ 457 0, /* bitpos */ 458 complain_overflow_dont, /* complain_on_overflow */ 459 bfd_elf_generic_reloc, /* special_function */ 460 "R_AVR_MS8_LDI", /* name */ 461 FALSE, /* partial_inplace */ 462 0xffff, /* src_mask */ 463 0xffff, /* dst_mask */ 464 FALSE), /* pcrel_offset */ 465 /* Negative most significant 8 bit value of a 32 bit link-time constant. */ 466 HOWTO (R_AVR_MS8_LDI_NEG, /* type */ 467 24, /* rightshift */ 468 1, /* size (0 = byte, 1 = short, 2 = long) */ 469 8, /* bitsize */ 470 FALSE, /* pc_relative */ 471 0, /* bitpos */ 472 complain_overflow_dont, /* complain_on_overflow */ 473 bfd_elf_generic_reloc, /* special_function */ 474 "R_AVR_MS8_LDI_NEG", /* name */ 475 FALSE, /* partial_inplace */ 476 0xffff, /* src_mask */ 477 0xffff, /* dst_mask */ 478 FALSE), /* pcrel_offset */ 479 /* A low 8 bit absolute relocation of 24 bit program memory address. 480 For LDI command. Will be changed when linker stubs are needed. */ 481 HOWTO (R_AVR_LO8_LDI_GS, /* type */ 482 1, /* rightshift */ 483 1, /* size (0 = byte, 1 = short, 2 = long) */ 484 8, /* bitsize */ 485 FALSE, /* pc_relative */ 486 0, /* bitpos */ 487 complain_overflow_dont, /* complain_on_overflow */ 488 bfd_elf_generic_reloc, /* special_function */ 489 "R_AVR_LO8_LDI_GS", /* name */ 490 FALSE, /* partial_inplace */ 491 0xffff, /* src_mask */ 492 0xffff, /* dst_mask */ 493 FALSE), /* pcrel_offset */ 494 /* A low 8 bit absolute relocation of 24 bit program memory address. 495 For LDI command. Will be changed when linker stubs are needed. */ 496 HOWTO (R_AVR_HI8_LDI_GS, /* type */ 497 9, /* rightshift */ 498 1, /* size (0 = byte, 1 = short, 2 = long) */ 499 8, /* bitsize */ 500 FALSE, /* pc_relative */ 501 0, /* bitpos */ 502 complain_overflow_dont, /* complain_on_overflow */ 503 bfd_elf_generic_reloc, /* special_function */ 504 "R_AVR_HI8_LDI_GS", /* name */ 505 FALSE, /* partial_inplace */ 506 0xffff, /* src_mask */ 507 0xffff, /* dst_mask */ 508 FALSE), /* pcrel_offset */ 509 /* 8 bit offset. */ 510 HOWTO (R_AVR_8, /* type */ 511 0, /* rightshift */ 512 0, /* size (0 = byte, 1 = short, 2 = long) */ 513 8, /* bitsize */ 514 FALSE, /* pc_relative */ 515 0, /* bitpos */ 516 complain_overflow_bitfield,/* complain_on_overflow */ 517 bfd_elf_generic_reloc, /* special_function */ 518 "R_AVR_8", /* name */ 519 FALSE, /* partial_inplace */ 520 0x000000ff, /* src_mask */ 521 0x000000ff, /* dst_mask */ 522 FALSE), /* pcrel_offset */ 523 /* lo8-part to use in .byte lo8(sym). */ 524 HOWTO (R_AVR_8_LO8, /* type */ 525 0, /* rightshift */ 526 0, /* size (0 = byte, 1 = short, 2 = long) */ 527 8, /* bitsize */ 528 FALSE, /* pc_relative */ 529 0, /* bitpos */ 530 complain_overflow_dont,/* complain_on_overflow */ 531 bfd_elf_generic_reloc, /* special_function */ 532 "R_AVR_8_LO8", /* name */ 533 FALSE, /* partial_inplace */ 534 0xffffff, /* src_mask */ 535 0xffffff, /* dst_mask */ 536 FALSE), /* pcrel_offset */ 537 /* hi8-part to use in .byte hi8(sym). */ 538 HOWTO (R_AVR_8_HI8, /* type */ 539 8, /* rightshift */ 540 0, /* size (0 = byte, 1 = short, 2 = long) */ 541 8, /* bitsize */ 542 FALSE, /* pc_relative */ 543 0, /* bitpos */ 544 complain_overflow_dont,/* complain_on_overflow */ 545 bfd_elf_generic_reloc, /* special_function */ 546 "R_AVR_8_HI8", /* name */ 547 FALSE, /* partial_inplace */ 548 0xffffff, /* src_mask */ 549 0xffffff, /* dst_mask */ 550 FALSE), /* pcrel_offset */ 551 /* hlo8-part to use in .byte hlo8(sym). */ 552 HOWTO (R_AVR_8_HLO8, /* type */ 553 16, /* rightshift */ 554 0, /* size (0 = byte, 1 = short, 2 = long) */ 555 8, /* bitsize */ 556 FALSE, /* pc_relative */ 557 0, /* bitpos */ 558 complain_overflow_dont,/* complain_on_overflow */ 559 bfd_elf_generic_reloc, /* special_function */ 560 "R_AVR_8_HLO8", /* name */ 561 FALSE, /* partial_inplace */ 562 0xffffff, /* src_mask */ 563 0xffffff, /* dst_mask */ 564 FALSE), /* pcrel_offset */ 565 HOWTO (R_AVR_DIFF8, /* type */ 566 0, /* rightshift */ 567 0, /* size (0 = byte, 1 = short, 2 = long) */ 568 8, /* bitsize */ 569 FALSE, /* pc_relative */ 570 0, /* bitpos */ 571 complain_overflow_bitfield, /* complain_on_overflow */ 572 bfd_elf_avr_diff_reloc, /* special_function */ 573 "R_AVR_DIFF8", /* name */ 574 FALSE, /* partial_inplace */ 575 0, /* src_mask */ 576 0xff, /* dst_mask */ 577 FALSE), /* pcrel_offset */ 578 HOWTO (R_AVR_DIFF16, /* type */ 579 0, /* rightshift */ 580 1, /* size (0 = byte, 1 = short, 2 = long) */ 581 16, /* bitsize */ 582 FALSE, /* pc_relative */ 583 0, /* bitpos */ 584 complain_overflow_bitfield, /* complain_on_overflow */ 585 bfd_elf_avr_diff_reloc,/* special_function */ 586 "R_AVR_DIFF16", /* name */ 587 FALSE, /* partial_inplace */ 588 0, /* src_mask */ 589 0xffff, /* dst_mask */ 590 FALSE), /* pcrel_offset */ 591 HOWTO (R_AVR_DIFF32, /* type */ 592 0, /* rightshift */ 593 2, /* size (0 = byte, 1 = short, 2 = long) */ 594 32, /* bitsize */ 595 FALSE, /* pc_relative */ 596 0, /* bitpos */ 597 complain_overflow_bitfield, /* complain_on_overflow */ 598 bfd_elf_avr_diff_reloc,/* special_function */ 599 "R_AVR_DIFF32", /* name */ 600 FALSE, /* partial_inplace */ 601 0, /* src_mask */ 602 0xffffffff, /* dst_mask */ 603 FALSE), /* pcrel_offset */ 604 /* 7 bit immediate for LDS/STS in Tiny core. */ 605 HOWTO (R_AVR_LDS_STS_16, /* type */ 606 0, /* rightshift */ 607 1, /* size (0 = byte, 1 = short, 2 = long) */ 608 7, /* bitsize */ 609 FALSE, /* pc_relative */ 610 0, /* bitpos */ 611 complain_overflow_dont,/* complain_on_overflow */ 612 bfd_elf_generic_reloc, /* special_function */ 613 "R_AVR_LDS_STS_16", /* name */ 614 FALSE, /* partial_inplace */ 615 0xffff, /* src_mask */ 616 0xffff, /* dst_mask */ 617 FALSE), /* pcrel_offset */ 618 619 HOWTO (R_AVR_PORT6, /* type */ 620 0, /* rightshift */ 621 0, /* size (0 = byte, 1 = short, 2 = long) */ 622 6, /* bitsize */ 623 FALSE, /* pc_relative */ 624 0, /* bitpos */ 625 complain_overflow_dont,/* complain_on_overflow */ 626 bfd_elf_generic_reloc, /* special_function */ 627 "R_AVR_PORT6", /* name */ 628 FALSE, /* partial_inplace */ 629 0xffffff, /* src_mask */ 630 0xffffff, /* dst_mask */ 631 FALSE), /* pcrel_offset */ 632 HOWTO (R_AVR_PORT5, /* type */ 633 0, /* rightshift */ 634 0, /* size (0 = byte, 1 = short, 2 = long) */ 635 5, /* bitsize */ 636 FALSE, /* pc_relative */ 637 0, /* bitpos */ 638 complain_overflow_dont,/* complain_on_overflow */ 639 bfd_elf_generic_reloc, /* special_function */ 640 "R_AVR_PORT5", /* name */ 641 FALSE, /* partial_inplace */ 642 0xffffff, /* src_mask */ 643 0xffffff, /* dst_mask */ 644 FALSE), /* pcrel_offset */ 645 646 /* A 32 bit PC relative relocation. */ 647 HOWTO (R_AVR_32_PCREL, /* type */ 648 0, /* rightshift */ 649 2, /* size (0 = byte, 1 = short, 2 = long) */ 650 32, /* bitsize */ 651 TRUE, /* pc_relative */ 652 0, /* bitpos */ 653 complain_overflow_bitfield, /* complain_on_overflow */ 654 bfd_elf_generic_reloc, /* special_function */ 655 "R_AVR_32_PCREL", /* name */ 656 FALSE, /* partial_inplace */ 657 0xffffffff, /* src_mask */ 658 0xffffffff, /* dst_mask */ 659 TRUE), /* pcrel_offset */ 660 }; 661 662 /* Map BFD reloc types to AVR ELF reloc types. */ 663 664 struct avr_reloc_map 665 { 666 bfd_reloc_code_real_type bfd_reloc_val; 667 unsigned int elf_reloc_val; 668 }; 669 670 static const struct avr_reloc_map avr_reloc_map[] = 671 { 672 { BFD_RELOC_NONE, R_AVR_NONE }, 673 { BFD_RELOC_32, R_AVR_32 }, 674 { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL }, 675 { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL }, 676 { BFD_RELOC_16, R_AVR_16 }, 677 { BFD_RELOC_AVR_16_PM, R_AVR_16_PM }, 678 { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI}, 679 { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI }, 680 { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI }, 681 { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI }, 682 { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG }, 683 { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG }, 684 { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG }, 685 { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG }, 686 { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM }, 687 { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS }, 688 { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM }, 689 { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS }, 690 { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM }, 691 { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG }, 692 { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG }, 693 { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG }, 694 { BFD_RELOC_AVR_CALL, R_AVR_CALL }, 695 { BFD_RELOC_AVR_LDI, R_AVR_LDI }, 696 { BFD_RELOC_AVR_6, R_AVR_6 }, 697 { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW }, 698 { BFD_RELOC_8, R_AVR_8 }, 699 { BFD_RELOC_AVR_8_LO, R_AVR_8_LO8 }, 700 { BFD_RELOC_AVR_8_HI, R_AVR_8_HI8 }, 701 { BFD_RELOC_AVR_8_HLO, R_AVR_8_HLO8 }, 702 { BFD_RELOC_AVR_DIFF8, R_AVR_DIFF8 }, 703 { BFD_RELOC_AVR_DIFF16, R_AVR_DIFF16 }, 704 { BFD_RELOC_AVR_DIFF32, R_AVR_DIFF32 }, 705 { BFD_RELOC_AVR_LDS_STS_16, R_AVR_LDS_STS_16}, 706 { BFD_RELOC_AVR_PORT6, R_AVR_PORT6}, 707 { BFD_RELOC_AVR_PORT5, R_AVR_PORT5}, 708 { BFD_RELOC_32_PCREL, R_AVR_32_PCREL} 709 }; 710 711 /* Meant to be filled one day with the wrap around address for the 712 specific device. I.e. should get the value 0x4000 for 16k devices, 713 0x8000 for 32k devices and so on. 714 715 We initialize it here with a value of 0x1000000 resulting in 716 that we will never suggest a wrap-around jump during relaxation. 717 The logic of the source code later on assumes that in 718 avr_pc_wrap_around one single bit is set. */ 719 static bfd_vma avr_pc_wrap_around = 0x10000000; 720 721 /* If this variable holds a value different from zero, the linker relaxation 722 machine will try to optimize call/ret sequences by a single jump 723 instruction. This option could be switched off by a linker switch. */ 724 static int avr_replace_call_ret_sequences = 1; 725 726 728 /* Per-section relaxation related information for avr. */ 729 730 struct avr_relax_info 731 { 732 /* Track the avr property records that apply to this section. */ 733 734 struct 735 { 736 /* Number of records in the list. */ 737 unsigned count; 738 739 /* How many records worth of space have we allocated. */ 740 unsigned allocated; 741 742 /* The records, only COUNT records are initialised. */ 743 struct avr_property_record *items; 744 } records; 745 }; 746 747 /* Per section data, specialised for avr. */ 748 749 struct elf_avr_section_data 750 { 751 /* The standard data must appear first. */ 752 struct bfd_elf_section_data elf; 753 754 /* Relaxation related information. */ 755 struct avr_relax_info relax_info; 756 }; 757 758 /* Possibly initialise avr specific data for new section SEC from ABFD. */ 759 760 static bfd_boolean 761 elf_avr_new_section_hook (bfd *abfd, asection *sec) 762 { 763 if (!sec->used_by_bfd) 764 { 765 struct elf_avr_section_data *sdata; 766 bfd_size_type amt = sizeof (*sdata); 767 768 sdata = bfd_zalloc (abfd, amt); 769 if (sdata == NULL) 770 return FALSE; 771 sec->used_by_bfd = sdata; 772 } 773 774 return _bfd_elf_new_section_hook (abfd, sec); 775 } 776 777 /* Return a pointer to the relaxation information for SEC. */ 778 779 static struct avr_relax_info * 780 get_avr_relax_info (asection *sec) 781 { 782 struct elf_avr_section_data *section_data; 783 784 /* No info available if no section or if it is an output section. */ 785 if (!sec || sec == sec->output_section) 786 return NULL; 787 788 section_data = (struct elf_avr_section_data *) elf_section_data (sec); 789 return §ion_data->relax_info; 790 } 791 792 /* Initialise the per section relaxation information for SEC. */ 793 794 static void 795 init_avr_relax_info (asection *sec) 796 { 797 struct avr_relax_info *relax_info = get_avr_relax_info (sec); 798 799 relax_info->records.count = 0; 800 relax_info->records.allocated = 0; 801 relax_info->records.items = NULL; 802 } 803 804 /* Initialize an entry in the stub hash table. */ 805 806 static struct bfd_hash_entry * 807 stub_hash_newfunc (struct bfd_hash_entry *entry, 808 struct bfd_hash_table *table, 809 const char *string) 810 { 811 /* Allocate the structure if it has not already been allocated by a 812 subclass. */ 813 if (entry == NULL) 814 { 815 entry = bfd_hash_allocate (table, 816 sizeof (struct elf32_avr_stub_hash_entry)); 817 if (entry == NULL) 818 return entry; 819 } 820 821 /* Call the allocation method of the superclass. */ 822 entry = bfd_hash_newfunc (entry, table, string); 823 if (entry != NULL) 824 { 825 struct elf32_avr_stub_hash_entry *hsh; 826 827 /* Initialize the local fields. */ 828 hsh = avr_stub_hash_entry (entry); 829 hsh->stub_offset = 0; 830 hsh->target_value = 0; 831 } 832 833 return entry; 834 } 835 836 /* This function is just a straight passthrough to the real 837 function in linker.c. Its prupose is so that its address 838 can be compared inside the avr_link_hash_table macro. */ 839 840 static struct bfd_hash_entry * 841 elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry, 842 struct bfd_hash_table * table, 843 const char * string) 844 { 845 return _bfd_elf_link_hash_newfunc (entry, table, string); 846 } 847 848 /* Free the derived linker hash table. */ 849 850 static void 851 elf32_avr_link_hash_table_free (bfd *obfd) 852 { 853 struct elf32_avr_link_hash_table *htab 854 = (struct elf32_avr_link_hash_table *) obfd->link.hash; 855 856 /* Free the address mapping table. */ 857 if (htab->amt_stub_offsets != NULL) 858 free (htab->amt_stub_offsets); 859 if (htab->amt_destination_addr != NULL) 860 free (htab->amt_destination_addr); 861 862 bfd_hash_table_free (&htab->bstab); 863 _bfd_elf_link_hash_table_free (obfd); 864 } 865 866 /* Create the derived linker hash table. The AVR ELF port uses the derived 867 hash table to keep information specific to the AVR ELF linker (without 868 using static variables). */ 869 870 static struct bfd_link_hash_table * 871 elf32_avr_link_hash_table_create (bfd *abfd) 872 { 873 struct elf32_avr_link_hash_table *htab; 874 bfd_size_type amt = sizeof (*htab); 875 876 htab = bfd_zmalloc (amt); 877 if (htab == NULL) 878 return NULL; 879 880 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, 881 elf32_avr_link_hash_newfunc, 882 sizeof (struct elf_link_hash_entry), 883 AVR_ELF_DATA)) 884 { 885 free (htab); 886 return NULL; 887 } 888 889 /* Init the stub hash table too. */ 890 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc, 891 sizeof (struct elf32_avr_stub_hash_entry))) 892 { 893 _bfd_elf_link_hash_table_free (abfd); 894 return NULL; 895 } 896 htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free; 897 898 return &htab->etab.root; 899 } 900 901 /* Calculates the effective distance of a pc relative jump/call. */ 902 903 static int 904 avr_relative_distance_considering_wrap_around (unsigned int distance) 905 { 906 unsigned int wrap_around_mask = avr_pc_wrap_around - 1; 907 int dist_with_wrap_around = distance & wrap_around_mask; 908 909 if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1))) 910 dist_with_wrap_around -= avr_pc_wrap_around; 911 912 return dist_with_wrap_around; 913 } 914 915 916 static reloc_howto_type * 917 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, 918 bfd_reloc_code_real_type code) 919 { 920 unsigned int i; 921 922 for (i = 0; 923 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map); 924 i++) 925 if (avr_reloc_map[i].bfd_reloc_val == code) 926 return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val]; 927 928 return NULL; 929 } 930 931 static reloc_howto_type * 932 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, 933 const char *r_name) 934 { 935 unsigned int i; 936 937 for (i = 0; 938 i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]); 939 i++) 940 if (elf_avr_howto_table[i].name != NULL 941 && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0) 942 return &elf_avr_howto_table[i]; 943 944 return NULL; 945 } 946 947 /* Set the howto pointer for an AVR ELF reloc. */ 948 949 static void 950 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED, 951 arelent *cache_ptr, 952 Elf_Internal_Rela *dst) 953 { 954 unsigned int r_type; 955 956 r_type = ELF32_R_TYPE (dst->r_info); 957 if (r_type >= (unsigned int) R_AVR_max) 958 { 959 _bfd_error_handler (_("%B: invalid AVR reloc number: %d"), abfd, r_type); 960 r_type = 0; 961 } 962 cache_ptr->howto = &elf_avr_howto_table[r_type]; 963 } 964 965 static bfd_boolean 966 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation) 967 { 968 return (relocation >= 0x020000); 969 } 970 971 /* Returns the address of the corresponding stub if there is one. 972 Returns otherwise an address above 0x020000. This function 973 could also be used, if there is no knowledge on the section where 974 the destination is found. */ 975 976 static bfd_vma 977 avr_get_stub_addr (bfd_vma srel, 978 struct elf32_avr_link_hash_table *htab) 979 { 980 unsigned int sindex; 981 bfd_vma stub_sec_addr = 982 (htab->stub_sec->output_section->vma + 983 htab->stub_sec->output_offset); 984 985 for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++) 986 if (htab->amt_destination_addr[sindex] == srel) 987 return htab->amt_stub_offsets[sindex] + stub_sec_addr; 988 989 /* Return an address that could not be reached by 16 bit relocs. */ 990 return 0x020000; 991 } 992 993 /* Perform a diff relocation. Nothing to do, as the difference value is already 994 written into the section's contents. */ 995 996 static bfd_reloc_status_type 997 bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED, 998 arelent *reloc_entry ATTRIBUTE_UNUSED, 999 asymbol *symbol ATTRIBUTE_UNUSED, 1000 void *data ATTRIBUTE_UNUSED, 1001 asection *input_section ATTRIBUTE_UNUSED, 1002 bfd *output_bfd ATTRIBUTE_UNUSED, 1003 char **error_message ATTRIBUTE_UNUSED) 1004 { 1005 return bfd_reloc_ok; 1006 } 1007 1008 1009 /* Perform a single relocation. By default we use the standard BFD 1010 routines, but a few relocs, we have to do them ourselves. */ 1011 1012 static bfd_reloc_status_type 1013 avr_final_link_relocate (reloc_howto_type * howto, 1014 bfd * input_bfd, 1015 asection * input_section, 1016 bfd_byte * contents, 1017 Elf_Internal_Rela * rel, 1018 bfd_vma relocation, 1019 struct elf32_avr_link_hash_table * htab) 1020 { 1021 bfd_reloc_status_type r = bfd_reloc_ok; 1022 bfd_vma x; 1023 bfd_signed_vma srel; 1024 bfd_signed_vma reloc_addr; 1025 bfd_boolean use_stubs = FALSE; 1026 /* Usually is 0, unless we are generating code for a bootloader. */ 1027 bfd_signed_vma base_addr = htab->vector_base; 1028 1029 /* Absolute addr of the reloc in the final excecutable. */ 1030 reloc_addr = rel->r_offset + input_section->output_section->vma 1031 + input_section->output_offset; 1032 1033 switch (howto->type) 1034 { 1035 case R_AVR_7_PCREL: 1036 contents += rel->r_offset; 1037 srel = (bfd_signed_vma) relocation; 1038 srel += rel->r_addend; 1039 srel -= rel->r_offset; 1040 srel -= 2; /* Branch instructions add 2 to the PC... */ 1041 srel -= (input_section->output_section->vma + 1042 input_section->output_offset); 1043 1044 if (srel & 1) 1045 return bfd_reloc_outofrange; 1046 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7))) 1047 return bfd_reloc_overflow; 1048 x = bfd_get_16 (input_bfd, contents); 1049 x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8); 1050 bfd_put_16 (input_bfd, x, contents); 1051 break; 1052 1053 case R_AVR_13_PCREL: 1054 contents += rel->r_offset; 1055 srel = (bfd_signed_vma) relocation; 1056 srel += rel->r_addend; 1057 srel -= rel->r_offset; 1058 srel -= 2; /* Branch instructions add 2 to the PC... */ 1059 srel -= (input_section->output_section->vma + 1060 input_section->output_offset); 1061 1062 if (srel & 1) 1063 return bfd_reloc_outofrange; 1064 1065 srel = avr_relative_distance_considering_wrap_around (srel); 1066 1067 /* AVR addresses commands as words. */ 1068 srel >>= 1; 1069 1070 /* Check for overflow. */ 1071 if (srel < -2048 || srel > 2047) 1072 { 1073 /* Relative distance is too large. */ 1074 1075 /* Always apply WRAPAROUND for avr2, avr25, and avr4. */ 1076 switch (bfd_get_mach (input_bfd)) 1077 { 1078 case bfd_mach_avr2: 1079 case bfd_mach_avr25: 1080 case bfd_mach_avr4: 1081 break; 1082 1083 default: 1084 return bfd_reloc_overflow; 1085 } 1086 } 1087 1088 x = bfd_get_16 (input_bfd, contents); 1089 x = (x & 0xf000) | (srel & 0xfff); 1090 bfd_put_16 (input_bfd, x, contents); 1091 break; 1092 1093 case R_AVR_LO8_LDI: 1094 contents += rel->r_offset; 1095 srel = (bfd_signed_vma) relocation + rel->r_addend; 1096 x = bfd_get_16 (input_bfd, contents); 1097 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1098 bfd_put_16 (input_bfd, x, contents); 1099 break; 1100 1101 case R_AVR_LDI: 1102 contents += rel->r_offset; 1103 srel = (bfd_signed_vma) relocation + rel->r_addend; 1104 if (((srel > 0) && (srel & 0xffff) > 255) 1105 || ((srel < 0) && ((-srel) & 0xffff) > 128)) 1106 /* Remove offset for data/eeprom section. */ 1107 return bfd_reloc_overflow; 1108 1109 x = bfd_get_16 (input_bfd, contents); 1110 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1111 bfd_put_16 (input_bfd, x, contents); 1112 break; 1113 1114 case R_AVR_6: 1115 contents += rel->r_offset; 1116 srel = (bfd_signed_vma) relocation + rel->r_addend; 1117 if (((srel & 0xffff) > 63) || (srel < 0)) 1118 /* Remove offset for data/eeprom section. */ 1119 return bfd_reloc_overflow; 1120 x = bfd_get_16 (input_bfd, contents); 1121 x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7) 1122 | ((srel & (1 << 5)) << 8)); 1123 bfd_put_16 (input_bfd, x, contents); 1124 break; 1125 1126 case R_AVR_6_ADIW: 1127 contents += rel->r_offset; 1128 srel = (bfd_signed_vma) relocation + rel->r_addend; 1129 if (((srel & 0xffff) > 63) || (srel < 0)) 1130 /* Remove offset for data/eeprom section. */ 1131 return bfd_reloc_overflow; 1132 x = bfd_get_16 (input_bfd, contents); 1133 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2); 1134 bfd_put_16 (input_bfd, x, contents); 1135 break; 1136 1137 case R_AVR_HI8_LDI: 1138 contents += rel->r_offset; 1139 srel = (bfd_signed_vma) relocation + rel->r_addend; 1140 srel = (srel >> 8) & 0xff; 1141 x = bfd_get_16 (input_bfd, contents); 1142 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1143 bfd_put_16 (input_bfd, x, contents); 1144 break; 1145 1146 case R_AVR_HH8_LDI: 1147 contents += rel->r_offset; 1148 srel = (bfd_signed_vma) relocation + rel->r_addend; 1149 srel = (srel >> 16) & 0xff; 1150 x = bfd_get_16 (input_bfd, contents); 1151 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1152 bfd_put_16 (input_bfd, x, contents); 1153 break; 1154 1155 case R_AVR_MS8_LDI: 1156 contents += rel->r_offset; 1157 srel = (bfd_signed_vma) relocation + rel->r_addend; 1158 srel = (srel >> 24) & 0xff; 1159 x = bfd_get_16 (input_bfd, contents); 1160 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1161 bfd_put_16 (input_bfd, x, contents); 1162 break; 1163 1164 case R_AVR_LO8_LDI_NEG: 1165 contents += rel->r_offset; 1166 srel = (bfd_signed_vma) relocation + rel->r_addend; 1167 srel = -srel; 1168 x = bfd_get_16 (input_bfd, contents); 1169 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1170 bfd_put_16 (input_bfd, x, contents); 1171 break; 1172 1173 case R_AVR_HI8_LDI_NEG: 1174 contents += rel->r_offset; 1175 srel = (bfd_signed_vma) relocation + rel->r_addend; 1176 srel = -srel; 1177 srel = (srel >> 8) & 0xff; 1178 x = bfd_get_16 (input_bfd, contents); 1179 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1180 bfd_put_16 (input_bfd, x, contents); 1181 break; 1182 1183 case R_AVR_HH8_LDI_NEG: 1184 contents += rel->r_offset; 1185 srel = (bfd_signed_vma) relocation + rel->r_addend; 1186 srel = -srel; 1187 srel = (srel >> 16) & 0xff; 1188 x = bfd_get_16 (input_bfd, contents); 1189 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1190 bfd_put_16 (input_bfd, x, contents); 1191 break; 1192 1193 case R_AVR_MS8_LDI_NEG: 1194 contents += rel->r_offset; 1195 srel = (bfd_signed_vma) relocation + rel->r_addend; 1196 srel = -srel; 1197 srel = (srel >> 24) & 0xff; 1198 x = bfd_get_16 (input_bfd, contents); 1199 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1200 bfd_put_16 (input_bfd, x, contents); 1201 break; 1202 1203 case R_AVR_LO8_LDI_GS: 1204 use_stubs = (!htab->no_stubs); 1205 /* Fall through. */ 1206 case R_AVR_LO8_LDI_PM: 1207 contents += rel->r_offset; 1208 srel = (bfd_signed_vma) relocation + rel->r_addend; 1209 1210 if (use_stubs 1211 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1212 { 1213 bfd_vma old_srel = srel; 1214 1215 /* We need to use the address of the stub instead. */ 1216 srel = avr_get_stub_addr (srel, htab); 1217 if (debug_stubs) 1218 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " 1219 "reloc at address 0x%x.\n", 1220 (unsigned int) srel, 1221 (unsigned int) old_srel, 1222 (unsigned int) reloc_addr); 1223 1224 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1225 return bfd_reloc_outofrange; 1226 } 1227 1228 if (srel & 1) 1229 return bfd_reloc_outofrange; 1230 srel = srel >> 1; 1231 x = bfd_get_16 (input_bfd, contents); 1232 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1233 bfd_put_16 (input_bfd, x, contents); 1234 break; 1235 1236 case R_AVR_HI8_LDI_GS: 1237 use_stubs = (!htab->no_stubs); 1238 /* Fall through. */ 1239 case R_AVR_HI8_LDI_PM: 1240 contents += rel->r_offset; 1241 srel = (bfd_signed_vma) relocation + rel->r_addend; 1242 1243 if (use_stubs 1244 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1245 { 1246 bfd_vma old_srel = srel; 1247 1248 /* We need to use the address of the stub instead. */ 1249 srel = avr_get_stub_addr (srel, htab); 1250 if (debug_stubs) 1251 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " 1252 "reloc at address 0x%x.\n", 1253 (unsigned int) srel, 1254 (unsigned int) old_srel, 1255 (unsigned int) reloc_addr); 1256 1257 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1258 return bfd_reloc_outofrange; 1259 } 1260 1261 if (srel & 1) 1262 return bfd_reloc_outofrange; 1263 srel = srel >> 1; 1264 srel = (srel >> 8) & 0xff; 1265 x = bfd_get_16 (input_bfd, contents); 1266 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1267 bfd_put_16 (input_bfd, x, contents); 1268 break; 1269 1270 case R_AVR_HH8_LDI_PM: 1271 contents += rel->r_offset; 1272 srel = (bfd_signed_vma) relocation + rel->r_addend; 1273 if (srel & 1) 1274 return bfd_reloc_outofrange; 1275 srel = srel >> 1; 1276 srel = (srel >> 16) & 0xff; 1277 x = bfd_get_16 (input_bfd, contents); 1278 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1279 bfd_put_16 (input_bfd, x, contents); 1280 break; 1281 1282 case R_AVR_LO8_LDI_PM_NEG: 1283 contents += rel->r_offset; 1284 srel = (bfd_signed_vma) relocation + rel->r_addend; 1285 srel = -srel; 1286 if (srel & 1) 1287 return bfd_reloc_outofrange; 1288 srel = srel >> 1; 1289 x = bfd_get_16 (input_bfd, contents); 1290 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1291 bfd_put_16 (input_bfd, x, contents); 1292 break; 1293 1294 case R_AVR_HI8_LDI_PM_NEG: 1295 contents += rel->r_offset; 1296 srel = (bfd_signed_vma) relocation + rel->r_addend; 1297 srel = -srel; 1298 if (srel & 1) 1299 return bfd_reloc_outofrange; 1300 srel = srel >> 1; 1301 srel = (srel >> 8) & 0xff; 1302 x = bfd_get_16 (input_bfd, contents); 1303 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1304 bfd_put_16 (input_bfd, x, contents); 1305 break; 1306 1307 case R_AVR_HH8_LDI_PM_NEG: 1308 contents += rel->r_offset; 1309 srel = (bfd_signed_vma) relocation + rel->r_addend; 1310 srel = -srel; 1311 if (srel & 1) 1312 return bfd_reloc_outofrange; 1313 srel = srel >> 1; 1314 srel = (srel >> 16) & 0xff; 1315 x = bfd_get_16 (input_bfd, contents); 1316 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); 1317 bfd_put_16 (input_bfd, x, contents); 1318 break; 1319 1320 case R_AVR_CALL: 1321 contents += rel->r_offset; 1322 srel = (bfd_signed_vma) relocation + rel->r_addend; 1323 if (srel & 1) 1324 return bfd_reloc_outofrange; 1325 srel = srel >> 1; 1326 x = bfd_get_16 (input_bfd, contents); 1327 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16; 1328 bfd_put_16 (input_bfd, x, contents); 1329 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2); 1330 break; 1331 1332 case R_AVR_16_PM: 1333 use_stubs = (!htab->no_stubs); 1334 contents += rel->r_offset; 1335 srel = (bfd_signed_vma) relocation + rel->r_addend; 1336 1337 if (use_stubs 1338 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1339 { 1340 bfd_vma old_srel = srel; 1341 1342 /* We need to use the address of the stub instead. */ 1343 srel = avr_get_stub_addr (srel,htab); 1344 if (debug_stubs) 1345 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " 1346 "reloc at address 0x%x.\n", 1347 (unsigned int) srel, 1348 (unsigned int) old_srel, 1349 (unsigned int) reloc_addr); 1350 1351 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) 1352 return bfd_reloc_outofrange; 1353 } 1354 1355 if (srel & 1) 1356 return bfd_reloc_outofrange; 1357 srel = srel >> 1; 1358 bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents); 1359 break; 1360 1361 case R_AVR_DIFF8: 1362 case R_AVR_DIFF16: 1363 case R_AVR_DIFF32: 1364 /* Nothing to do here, as contents already contains the diff value. */ 1365 r = bfd_reloc_ok; 1366 break; 1367 1368 case R_AVR_LDS_STS_16: 1369 contents += rel->r_offset; 1370 srel = (bfd_signed_vma) relocation + rel->r_addend; 1371 if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf) 1372 return bfd_reloc_outofrange; 1373 srel = srel & 0x7f; 1374 x = bfd_get_16 (input_bfd, contents); 1375 x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2); 1376 bfd_put_16 (input_bfd, x, contents); 1377 break; 1378 1379 case R_AVR_PORT6: 1380 contents += rel->r_offset; 1381 srel = (bfd_signed_vma) relocation + rel->r_addend; 1382 if ((srel & 0xffff) > 0x3f) 1383 return bfd_reloc_outofrange; 1384 x = bfd_get_16 (input_bfd, contents); 1385 x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f); 1386 bfd_put_16 (input_bfd, x, contents); 1387 break; 1388 1389 case R_AVR_PORT5: 1390 contents += rel->r_offset; 1391 srel = (bfd_signed_vma) relocation + rel->r_addend; 1392 if ((srel & 0xffff) > 0x1f) 1393 return bfd_reloc_outofrange; 1394 x = bfd_get_16 (input_bfd, contents); 1395 x = (x & 0xff07) | ((srel & 0x1f) << 3); 1396 bfd_put_16 (input_bfd, x, contents); 1397 break; 1398 1399 default: 1400 r = _bfd_final_link_relocate (howto, input_bfd, input_section, 1401 contents, rel->r_offset, 1402 relocation, rel->r_addend); 1403 } 1404 1405 return r; 1406 } 1407 1408 /* Relocate an AVR ELF section. */ 1409 1410 static bfd_boolean 1411 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED, 1412 struct bfd_link_info *info, 1413 bfd *input_bfd, 1414 asection *input_section, 1415 bfd_byte *contents, 1416 Elf_Internal_Rela *relocs, 1417 Elf_Internal_Sym *local_syms, 1418 asection **local_sections) 1419 { 1420 Elf_Internal_Shdr * symtab_hdr; 1421 struct elf_link_hash_entry ** sym_hashes; 1422 Elf_Internal_Rela * rel; 1423 Elf_Internal_Rela * relend; 1424 struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info); 1425 1426 if (htab == NULL) 1427 return FALSE; 1428 1429 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; 1430 sym_hashes = elf_sym_hashes (input_bfd); 1431 relend = relocs + input_section->reloc_count; 1432 1433 for (rel = relocs; rel < relend; rel ++) 1434 { 1435 reloc_howto_type * howto; 1436 unsigned long r_symndx; 1437 Elf_Internal_Sym * sym; 1438 asection * sec; 1439 struct elf_link_hash_entry * h; 1440 bfd_vma relocation; 1441 bfd_reloc_status_type r; 1442 const char * name; 1443 int r_type; 1444 1445 r_type = ELF32_R_TYPE (rel->r_info); 1446 r_symndx = ELF32_R_SYM (rel->r_info); 1447 howto = elf_avr_howto_table + r_type; 1448 h = NULL; 1449 sym = NULL; 1450 sec = NULL; 1451 1452 if (r_symndx < symtab_hdr->sh_info) 1453 { 1454 sym = local_syms + r_symndx; 1455 sec = local_sections [r_symndx]; 1456 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); 1457 1458 name = bfd_elf_string_from_elf_section 1459 (input_bfd, symtab_hdr->sh_link, sym->st_name); 1460 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name; 1461 } 1462 else 1463 { 1464 bfd_boolean unresolved_reloc, warned, ignored; 1465 1466 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, 1467 r_symndx, symtab_hdr, sym_hashes, 1468 h, sec, relocation, 1469 unresolved_reloc, warned, ignored); 1470 1471 name = h->root.root.string; 1472 } 1473 1474 if (sec != NULL && discarded_section (sec)) 1475 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, 1476 rel, 1, relend, howto, 0, contents); 1477 1478 if (bfd_link_relocatable (info)) 1479 continue; 1480 1481 r = avr_final_link_relocate (howto, input_bfd, input_section, 1482 contents, rel, relocation, htab); 1483 1484 if (r != bfd_reloc_ok) 1485 { 1486 const char * msg = (const char *) NULL; 1487 1488 switch (r) 1489 { 1490 case bfd_reloc_overflow: 1491 (*info->callbacks->reloc_overflow) 1492 (info, (h ? &h->root : NULL), name, howto->name, 1493 (bfd_vma) 0, input_bfd, input_section, rel->r_offset); 1494 break; 1495 1496 case bfd_reloc_undefined: 1497 (*info->callbacks->undefined_symbol) 1498 (info, name, input_bfd, input_section, rel->r_offset, TRUE); 1499 break; 1500 1501 case bfd_reloc_outofrange: 1502 msg = _("internal error: out of range error"); 1503 break; 1504 1505 case bfd_reloc_notsupported: 1506 msg = _("internal error: unsupported relocation error"); 1507 break; 1508 1509 case bfd_reloc_dangerous: 1510 msg = _("internal error: dangerous relocation"); 1511 break; 1512 1513 default: 1514 msg = _("internal error: unknown error"); 1515 break; 1516 } 1517 1518 if (msg) 1519 (*info->callbacks->warning) (info, msg, name, input_bfd, 1520 input_section, rel->r_offset); 1521 } 1522 } 1523 1524 return TRUE; 1525 } 1526 1527 /* The final processing done just before writing out a AVR ELF object 1528 file. This gets the AVR architecture right based on the machine 1529 number. */ 1530 1531 static void 1532 bfd_elf_avr_final_write_processing (bfd *abfd, 1533 bfd_boolean linker ATTRIBUTE_UNUSED) 1534 { 1535 unsigned long val; 1536 1537 switch (bfd_get_mach (abfd)) 1538 { 1539 default: 1540 case bfd_mach_avr2: 1541 val = E_AVR_MACH_AVR2; 1542 break; 1543 1544 case bfd_mach_avr1: 1545 val = E_AVR_MACH_AVR1; 1546 break; 1547 1548 case bfd_mach_avr25: 1549 val = E_AVR_MACH_AVR25; 1550 break; 1551 1552 case bfd_mach_avr3: 1553 val = E_AVR_MACH_AVR3; 1554 break; 1555 1556 case bfd_mach_avr31: 1557 val = E_AVR_MACH_AVR31; 1558 break; 1559 1560 case bfd_mach_avr35: 1561 val = E_AVR_MACH_AVR35; 1562 break; 1563 1564 case bfd_mach_avr4: 1565 val = E_AVR_MACH_AVR4; 1566 break; 1567 1568 case bfd_mach_avr5: 1569 val = E_AVR_MACH_AVR5; 1570 break; 1571 1572 case bfd_mach_avr51: 1573 val = E_AVR_MACH_AVR51; 1574 break; 1575 1576 case bfd_mach_avr6: 1577 val = E_AVR_MACH_AVR6; 1578 break; 1579 1580 case bfd_mach_avrxmega1: 1581 val = E_AVR_MACH_XMEGA1; 1582 break; 1583 1584 case bfd_mach_avrxmega2: 1585 val = E_AVR_MACH_XMEGA2; 1586 break; 1587 1588 case bfd_mach_avrxmega3: 1589 val = E_AVR_MACH_XMEGA3; 1590 break; 1591 1592 case bfd_mach_avrxmega4: 1593 val = E_AVR_MACH_XMEGA4; 1594 break; 1595 1596 case bfd_mach_avrxmega5: 1597 val = E_AVR_MACH_XMEGA5; 1598 break; 1599 1600 case bfd_mach_avrxmega6: 1601 val = E_AVR_MACH_XMEGA6; 1602 break; 1603 1604 case bfd_mach_avrxmega7: 1605 val = E_AVR_MACH_XMEGA7; 1606 break; 1607 1608 case bfd_mach_avrtiny: 1609 val = E_AVR_MACH_AVRTINY; 1610 break; 1611 } 1612 1613 elf_elfheader (abfd)->e_machine = EM_AVR; 1614 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH; 1615 elf_elfheader (abfd)->e_flags |= val; 1616 } 1617 1618 /* Set the right machine number. */ 1619 1620 static bfd_boolean 1621 elf32_avr_object_p (bfd *abfd) 1622 { 1623 unsigned int e_set = bfd_mach_avr2; 1624 1625 if (elf_elfheader (abfd)->e_machine == EM_AVR 1626 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD) 1627 { 1628 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH; 1629 1630 switch (e_mach) 1631 { 1632 default: 1633 case E_AVR_MACH_AVR2: 1634 e_set = bfd_mach_avr2; 1635 break; 1636 1637 case E_AVR_MACH_AVR1: 1638 e_set = bfd_mach_avr1; 1639 break; 1640 1641 case E_AVR_MACH_AVR25: 1642 e_set = bfd_mach_avr25; 1643 break; 1644 1645 case E_AVR_MACH_AVR3: 1646 e_set = bfd_mach_avr3; 1647 break; 1648 1649 case E_AVR_MACH_AVR31: 1650 e_set = bfd_mach_avr31; 1651 break; 1652 1653 case E_AVR_MACH_AVR35: 1654 e_set = bfd_mach_avr35; 1655 break; 1656 1657 case E_AVR_MACH_AVR4: 1658 e_set = bfd_mach_avr4; 1659 break; 1660 1661 case E_AVR_MACH_AVR5: 1662 e_set = bfd_mach_avr5; 1663 break; 1664 1665 case E_AVR_MACH_AVR51: 1666 e_set = bfd_mach_avr51; 1667 break; 1668 1669 case E_AVR_MACH_AVR6: 1670 e_set = bfd_mach_avr6; 1671 break; 1672 1673 case E_AVR_MACH_XMEGA1: 1674 e_set = bfd_mach_avrxmega1; 1675 break; 1676 1677 case E_AVR_MACH_XMEGA2: 1678 e_set = bfd_mach_avrxmega2; 1679 break; 1680 1681 case E_AVR_MACH_XMEGA3: 1682 e_set = bfd_mach_avrxmega3; 1683 break; 1684 1685 case E_AVR_MACH_XMEGA4: 1686 e_set = bfd_mach_avrxmega4; 1687 break; 1688 1689 case E_AVR_MACH_XMEGA5: 1690 e_set = bfd_mach_avrxmega5; 1691 break; 1692 1693 case E_AVR_MACH_XMEGA6: 1694 e_set = bfd_mach_avrxmega6; 1695 break; 1696 1697 case E_AVR_MACH_XMEGA7: 1698 e_set = bfd_mach_avrxmega7; 1699 break; 1700 1701 case E_AVR_MACH_AVRTINY: 1702 e_set = bfd_mach_avrtiny; 1703 break; 1704 } 1705 } 1706 return bfd_default_set_arch_mach (abfd, bfd_arch_avr, 1707 e_set); 1708 } 1709 1710 /* Returns whether the relocation type passed is a diff reloc. */ 1711 1712 static bfd_boolean 1713 elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel) 1714 { 1715 return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8 1716 ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16 1717 || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32); 1718 } 1719 1720 /* Reduce the diff value written in the section by count if the shrinked 1721 insn address happens to fall between the two symbols for which this 1722 diff reloc was emitted. */ 1723 1724 static void 1725 elf32_avr_adjust_diff_reloc_value (bfd *abfd, 1726 struct bfd_section *isec, 1727 Elf_Internal_Rela *irel, 1728 bfd_vma symval, 1729 bfd_vma shrinked_insn_address, 1730 int count) 1731 { 1732 unsigned char *reloc_contents = NULL; 1733 unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents; 1734 if (isec_contents == NULL) 1735 { 1736 if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents)) 1737 return; 1738 1739 elf_section_data (isec)->this_hdr.contents = isec_contents; 1740 } 1741 1742 reloc_contents = isec_contents + irel->r_offset; 1743 1744 /* Read value written in object file. */ 1745 bfd_vma x = 0; 1746 switch (ELF32_R_TYPE (irel->r_info)) 1747 { 1748 case R_AVR_DIFF8: 1749 { 1750 x = *reloc_contents; 1751 break; 1752 } 1753 case R_AVR_DIFF16: 1754 { 1755 x = bfd_get_16 (abfd, reloc_contents); 1756 break; 1757 } 1758 case R_AVR_DIFF32: 1759 { 1760 x = bfd_get_32 (abfd, reloc_contents); 1761 break; 1762 } 1763 default: 1764 { 1765 BFD_FAIL(); 1766 } 1767 } 1768 1769 /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written 1770 into the object file at the reloc offset. sym2's logical value is 1771 symval (<start_of_section>) + reloc addend. Compute the start and end 1772 addresses and check if the shrinked insn falls between sym1 and sym2. */ 1773 1774 bfd_vma end_address = symval + irel->r_addend; 1775 bfd_vma start_address = end_address - x; 1776 1777 /* Reduce the diff value by count bytes and write it back into section 1778 contents. */ 1779 1780 if (shrinked_insn_address >= start_address 1781 && shrinked_insn_address <= end_address) 1782 { 1783 switch (ELF32_R_TYPE (irel->r_info)) 1784 { 1785 case R_AVR_DIFF8: 1786 { 1787 *reloc_contents = (x - count); 1788 break; 1789 } 1790 case R_AVR_DIFF16: 1791 { 1792 bfd_put_16 (abfd, (x - count) & 0xFFFF, reloc_contents); 1793 break; 1794 } 1795 case R_AVR_DIFF32: 1796 { 1797 bfd_put_32 (abfd, (x - count) & 0xFFFFFFFF, reloc_contents); 1798 break; 1799 } 1800 default: 1801 { 1802 BFD_FAIL(); 1803 } 1804 } 1805 1806 } 1807 } 1808 1809 /* Delete some bytes from a section while changing the size of an instruction. 1810 The parameter "addr" denotes the section-relative offset pointing just 1811 behind the shrinked instruction. "addr+count" point at the first 1812 byte just behind the original unshrinked instruction. If delete_shrinks_insn 1813 is FALSE, we are deleting redundant padding bytes from relax_info prop 1814 record handling. In that case, addr is section-relative offset of start 1815 of padding, and count is the number of padding bytes to delete. */ 1816 1817 static bfd_boolean 1818 elf32_avr_relax_delete_bytes (bfd *abfd, 1819 asection *sec, 1820 bfd_vma addr, 1821 int count, 1822 bfd_boolean delete_shrinks_insn) 1823 { 1824 Elf_Internal_Shdr *symtab_hdr; 1825 unsigned int sec_shndx; 1826 bfd_byte *contents; 1827 Elf_Internal_Rela *irel, *irelend; 1828 Elf_Internal_Sym *isym; 1829 Elf_Internal_Sym *isymbuf = NULL; 1830 bfd_vma toaddr, reloc_toaddr; 1831 struct elf_link_hash_entry **sym_hashes; 1832 struct elf_link_hash_entry **end_hashes; 1833 unsigned int symcount; 1834 struct avr_relax_info *relax_info; 1835 struct avr_property_record *prop_record = NULL; 1836 bfd_boolean did_shrink = FALSE; 1837 bfd_boolean did_pad = FALSE; 1838 1839 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 1840 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); 1841 contents = elf_section_data (sec)->this_hdr.contents; 1842 relax_info = get_avr_relax_info (sec); 1843 1844 toaddr = sec->size; 1845 1846 if (relax_info->records.count > 0) 1847 { 1848 /* There should be no property record within the range of deleted 1849 bytes, however, there might be a property record for ADDR, this is 1850 how we handle alignment directives. 1851 Find the next (if any) property record after the deleted bytes. */ 1852 unsigned int i; 1853 1854 for (i = 0; i < relax_info->records.count; ++i) 1855 { 1856 bfd_vma offset = relax_info->records.items [i].offset; 1857 1858 BFD_ASSERT (offset <= addr || offset >= (addr + count)); 1859 if (offset >= (addr + count)) 1860 { 1861 prop_record = &relax_info->records.items [i]; 1862 toaddr = offset; 1863 break; 1864 } 1865 } 1866 } 1867 1868 /* We need to look at all relocs with offsets less than toaddr. prop 1869 records handling adjusts toaddr downwards to avoid moving syms at the 1870 address of the property record, but all relocs with offsets between addr 1871 and the current value of toaddr need to have their offsets adjusted. 1872 Assume addr = 0, toaddr = 4 and count = 2. After prop records handling, 1873 toaddr becomes 2, but relocs with offsets 2 and 3 still need to be 1874 adjusted (to 0 and 1 respectively), as the first 2 bytes are now gone. 1875 So record the current value of toaddr here, and use it when adjusting 1876 reloc offsets. */ 1877 reloc_toaddr = toaddr; 1878 1879 irel = elf_section_data (sec)->relocs; 1880 irelend = irel + sec->reloc_count; 1881 1882 /* Actually delete the bytes. */ 1883 if (toaddr - addr - count > 0) 1884 { 1885 memmove (contents + addr, contents + addr + count, 1886 (size_t) (toaddr - addr - count)); 1887 did_shrink = TRUE; 1888 } 1889 if (prop_record == NULL) 1890 { 1891 sec->size -= count; 1892 did_shrink = TRUE; 1893 } 1894 else 1895 { 1896 /* Use the property record to fill in the bytes we've opened up. */ 1897 int fill = 0; 1898 switch (prop_record->type) 1899 { 1900 case RECORD_ORG_AND_FILL: 1901 fill = prop_record->data.org.fill; 1902 /* Fall through. */ 1903 case RECORD_ORG: 1904 break; 1905 case RECORD_ALIGN_AND_FILL: 1906 fill = prop_record->data.align.fill; 1907 /* Fall through. */ 1908 case RECORD_ALIGN: 1909 prop_record->data.align.preceding_deleted += count; 1910 break; 1911 }; 1912 /* If toaddr == (addr + count), then we didn't delete anything, yet 1913 we fill count bytes backwards from toaddr. This is still ok - we 1914 end up overwriting the bytes we would have deleted. We just need 1915 to remember we didn't delete anything i.e. don't set did_shrink, 1916 so that we don't corrupt reloc offsets or symbol values.*/ 1917 memset (contents + toaddr - count, fill, count); 1918 did_pad = TRUE; 1919 1920 /* Adjust the TOADDR to avoid moving symbols located at the address 1921 of the property record, which has not moved. */ 1922 toaddr -= count; 1923 } 1924 1925 if (!did_shrink) 1926 return TRUE; 1927 1928 /* Adjust all the reloc addresses. */ 1929 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) 1930 { 1931 bfd_vma old_reloc_address; 1932 1933 old_reloc_address = (sec->output_section->vma 1934 + sec->output_offset + irel->r_offset); 1935 1936 /* Get the new reloc address. */ 1937 if ((irel->r_offset > addr 1938 && irel->r_offset < reloc_toaddr)) 1939 { 1940 if (debug_relax) 1941 printf ("Relocation at address 0x%x needs to be moved.\n" 1942 "Old section offset: 0x%x, New section offset: 0x%x \n", 1943 (unsigned int) old_reloc_address, 1944 (unsigned int) irel->r_offset, 1945 (unsigned int) ((irel->r_offset) - count)); 1946 1947 irel->r_offset -= count; 1948 } 1949 1950 } 1951 1952 /* The reloc's own addresses are now ok. However, we need to readjust 1953 the reloc's addend, i.e. the reloc's value if two conditions are met: 1954 1.) the reloc is relative to a symbol in this section that 1955 is located in front of the shrinked instruction 1956 2.) symbol plus addend end up behind the shrinked instruction. 1957 1958 The most common case where this happens are relocs relative to 1959 the section-start symbol. 1960 1961 This step needs to be done for all of the sections of the bfd. */ 1962 1963 { 1964 struct bfd_section *isec; 1965 1966 for (isec = abfd->sections; isec; isec = isec->next) 1967 { 1968 bfd_vma symval; 1969 bfd_vma shrinked_insn_address; 1970 1971 if (isec->reloc_count == 0) 1972 continue; 1973 1974 shrinked_insn_address = (sec->output_section->vma 1975 + sec->output_offset + addr); 1976 if (delete_shrinks_insn) 1977 shrinked_insn_address -= count; 1978 1979 irel = elf_section_data (isec)->relocs; 1980 /* PR 12161: Read in the relocs for this section if necessary. */ 1981 if (irel == NULL) 1982 irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE); 1983 1984 for (irelend = irel + isec->reloc_count; 1985 irel < irelend; 1986 irel++) 1987 { 1988 /* Read this BFD's local symbols if we haven't done 1989 so already. */ 1990 if (isymbuf == NULL && symtab_hdr->sh_info != 0) 1991 { 1992 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 1993 if (isymbuf == NULL) 1994 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 1995 symtab_hdr->sh_info, 0, 1996 NULL, NULL, NULL); 1997 if (isymbuf == NULL) 1998 return FALSE; 1999 } 2000 2001 /* Get the value of the symbol referred to by the reloc. */ 2002 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) 2003 { 2004 /* A local symbol. */ 2005 asection *sym_sec; 2006 2007 isym = isymbuf + ELF32_R_SYM (irel->r_info); 2008 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 2009 symval = isym->st_value; 2010 /* If the reloc is absolute, it will not have 2011 a symbol or section associated with it. */ 2012 if (sym_sec == sec) 2013 { 2014 /* If there is an alignment boundary, we only need to 2015 adjust addends that end up below the boundary. */ 2016 bfd_vma shrink_boundary = (reloc_toaddr 2017 + sec->output_section->vma 2018 + sec->output_offset); 2019 bfd_boolean addend_within_shrink_boundary = FALSE; 2020 2021 symval += sym_sec->output_section->vma 2022 + sym_sec->output_offset; 2023 2024 if (debug_relax) 2025 printf ("Checking if the relocation's " 2026 "addend needs corrections.\n" 2027 "Address of anchor symbol: 0x%x \n" 2028 "Address of relocation target: 0x%x \n" 2029 "Address of relaxed insn: 0x%x \n", 2030 (unsigned int) symval, 2031 (unsigned int) (symval + irel->r_addend), 2032 (unsigned int) shrinked_insn_address); 2033 2034 /* If we padded bytes, then the boundary didn't change, 2035 so there's no need to adjust addends pointing at the boundary. 2036 If we didn't pad, then we actually shrank the boundary, so 2037 addends pointing at the boundary need to be adjusted too. */ 2038 addend_within_shrink_boundary = did_pad 2039 ? ((symval + irel->r_addend) < shrink_boundary) 2040 : ((symval + irel->r_addend) <= shrink_boundary); 2041 2042 if (symval <= shrinked_insn_address 2043 && (symval + irel->r_addend) > shrinked_insn_address 2044 && addend_within_shrink_boundary) 2045 { 2046 if (elf32_avr_is_diff_reloc (irel)) 2047 { 2048 elf32_avr_adjust_diff_reloc_value (abfd, isec, irel, 2049 symval, 2050 shrinked_insn_address, 2051 count); 2052 } 2053 2054 irel->r_addend -= count; 2055 2056 if (debug_relax) 2057 printf ("Relocation's addend needed to be fixed \n"); 2058 } 2059 } 2060 /* else...Reference symbol is absolute. No adjustment needed. */ 2061 } 2062 /* else...Reference symbol is extern. No need for adjusting 2063 the addend. */ 2064 } 2065 } 2066 } 2067 2068 /* Adjust the local symbols defined in this section. */ 2069 isym = (Elf_Internal_Sym *) symtab_hdr->contents; 2070 /* Fix PR 9841, there may be no local symbols. */ 2071 if (isym != NULL) 2072 { 2073 Elf_Internal_Sym *isymend; 2074 2075 isymend = isym + symtab_hdr->sh_info; 2076 for (; isym < isymend; isym++) 2077 { 2078 if (isym->st_shndx == sec_shndx) 2079 { 2080 if (isym->st_value > addr 2081 && isym->st_value <= toaddr) 2082 isym->st_value -= count; 2083 2084 if (isym->st_value <= addr 2085 && isym->st_value + isym->st_size > addr) 2086 { 2087 /* If this assert fires then we have a symbol that ends 2088 part way through an instruction. Does that make 2089 sense? */ 2090 BFD_ASSERT (isym->st_value + isym->st_size >= addr + count); 2091 isym->st_size -= count; 2092 } 2093 } 2094 } 2095 } 2096 2097 /* Now adjust the global symbols defined in this section. */ 2098 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) 2099 - symtab_hdr->sh_info); 2100 sym_hashes = elf_sym_hashes (abfd); 2101 end_hashes = sym_hashes + symcount; 2102 for (; sym_hashes < end_hashes; sym_hashes++) 2103 { 2104 struct elf_link_hash_entry *sym_hash = *sym_hashes; 2105 if ((sym_hash->root.type == bfd_link_hash_defined 2106 || sym_hash->root.type == bfd_link_hash_defweak) 2107 && sym_hash->root.u.def.section == sec) 2108 { 2109 if (sym_hash->root.u.def.value > addr 2110 && sym_hash->root.u.def.value <= toaddr) 2111 sym_hash->root.u.def.value -= count; 2112 2113 if (sym_hash->root.u.def.value <= addr 2114 && (sym_hash->root.u.def.value + sym_hash->size > addr)) 2115 { 2116 /* If this assert fires then we have a symbol that ends 2117 part way through an instruction. Does that make 2118 sense? */ 2119 BFD_ASSERT (sym_hash->root.u.def.value + sym_hash->size 2120 >= addr + count); 2121 sym_hash->size -= count; 2122 } 2123 } 2124 } 2125 2126 return TRUE; 2127 } 2128 2129 static Elf_Internal_Sym * 2130 retrieve_local_syms (bfd *input_bfd) 2131 { 2132 Elf_Internal_Shdr *symtab_hdr; 2133 Elf_Internal_Sym *isymbuf; 2134 size_t locsymcount; 2135 2136 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 2137 locsymcount = symtab_hdr->sh_info; 2138 2139 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 2140 if (isymbuf == NULL && locsymcount != 0) 2141 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, 2142 NULL, NULL, NULL); 2143 2144 /* Save the symbols for this input file so they won't be read again. */ 2145 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents) 2146 symtab_hdr->contents = (unsigned char *) isymbuf; 2147 2148 return isymbuf; 2149 } 2150 2151 /* Get the input section for a given symbol index. 2152 If the symbol is: 2153 . a section symbol, return the section; 2154 . a common symbol, return the common section; 2155 . an undefined symbol, return the undefined section; 2156 . an indirect symbol, follow the links; 2157 . an absolute value, return the absolute section. */ 2158 2159 static asection * 2160 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx) 2161 { 2162 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2163 asection *target_sec = NULL; 2164 if (r_symndx < symtab_hdr->sh_info) 2165 { 2166 Elf_Internal_Sym *isymbuf; 2167 unsigned int section_index; 2168 2169 isymbuf = retrieve_local_syms (abfd); 2170 section_index = isymbuf[r_symndx].st_shndx; 2171 2172 if (section_index == SHN_UNDEF) 2173 target_sec = bfd_und_section_ptr; 2174 else if (section_index == SHN_ABS) 2175 target_sec = bfd_abs_section_ptr; 2176 else if (section_index == SHN_COMMON) 2177 target_sec = bfd_com_section_ptr; 2178 else 2179 target_sec = bfd_section_from_elf_index (abfd, section_index); 2180 } 2181 else 2182 { 2183 unsigned long indx = r_symndx - symtab_hdr->sh_info; 2184 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx]; 2185 2186 while (h->root.type == bfd_link_hash_indirect 2187 || h->root.type == bfd_link_hash_warning) 2188 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2189 2190 switch (h->root.type) 2191 { 2192 case bfd_link_hash_defined: 2193 case bfd_link_hash_defweak: 2194 target_sec = h->root.u.def.section; 2195 break; 2196 case bfd_link_hash_common: 2197 target_sec = bfd_com_section_ptr; 2198 break; 2199 case bfd_link_hash_undefined: 2200 case bfd_link_hash_undefweak: 2201 target_sec = bfd_und_section_ptr; 2202 break; 2203 default: /* New indirect warning. */ 2204 target_sec = bfd_und_section_ptr; 2205 break; 2206 } 2207 } 2208 return target_sec; 2209 } 2210 2211 /* Get the section-relative offset for a symbol number. */ 2212 2213 static bfd_vma 2214 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx) 2215 { 2216 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2217 bfd_vma offset = 0; 2218 2219 if (r_symndx < symtab_hdr->sh_info) 2220 { 2221 Elf_Internal_Sym *isymbuf; 2222 isymbuf = retrieve_local_syms (abfd); 2223 offset = isymbuf[r_symndx].st_value; 2224 } 2225 else 2226 { 2227 unsigned long indx = r_symndx - symtab_hdr->sh_info; 2228 struct elf_link_hash_entry *h = 2229 elf_sym_hashes (abfd)[indx]; 2230 2231 while (h->root.type == bfd_link_hash_indirect 2232 || h->root.type == bfd_link_hash_warning) 2233 h = (struct elf_link_hash_entry *) h->root.u.i.link; 2234 if (h->root.type == bfd_link_hash_defined 2235 || h->root.type == bfd_link_hash_defweak) 2236 offset = h->root.u.def.value; 2237 } 2238 return offset; 2239 } 2240 2241 /* Iterate over the property records in R_LIST, and copy each record into 2242 the list of records within the relaxation information for the section to 2243 which the record applies. */ 2244 2245 static void 2246 avr_elf32_assign_records_to_sections (struct avr_property_record_list *r_list) 2247 { 2248 unsigned int i; 2249 2250 for (i = 0; i < r_list->record_count; ++i) 2251 { 2252 struct avr_relax_info *relax_info; 2253 2254 relax_info = get_avr_relax_info (r_list->records [i].section); 2255 BFD_ASSERT (relax_info != NULL); 2256 2257 if (relax_info->records.count 2258 == relax_info->records.allocated) 2259 { 2260 /* Allocate more space. */ 2261 bfd_size_type size; 2262 2263 relax_info->records.allocated += 10; 2264 size = (sizeof (struct avr_property_record) 2265 * relax_info->records.allocated); 2266 relax_info->records.items 2267 = bfd_realloc (relax_info->records.items, size); 2268 } 2269 2270 memcpy (&relax_info->records.items [relax_info->records.count], 2271 &r_list->records [i], 2272 sizeof (struct avr_property_record)); 2273 relax_info->records.count++; 2274 } 2275 } 2276 2277 /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the 2278 ordering callback from QSORT. */ 2279 2280 static int 2281 avr_property_record_compare (const void *ap, const void *bp) 2282 { 2283 const struct avr_property_record *a 2284 = (struct avr_property_record *) ap; 2285 const struct avr_property_record *b 2286 = (struct avr_property_record *) bp; 2287 2288 if (a->offset != b->offset) 2289 return (a->offset - b->offset); 2290 2291 if (a->section != b->section) 2292 return (bfd_get_section_vma (a->section->owner, a->section) 2293 - bfd_get_section_vma (b->section->owner, b->section)); 2294 2295 return (a->type - b->type); 2296 } 2297 2298 /* Load all of the avr property sections from all of the bfd objects 2299 referenced from LINK_INFO. All of the records within each property 2300 section are assigned to the STRUCT AVR_RELAX_INFO within the section 2301 specific data of the appropriate section. */ 2302 2303 static void 2304 avr_load_all_property_sections (struct bfd_link_info *link_info) 2305 { 2306 bfd *abfd; 2307 asection *sec; 2308 2309 /* Initialize the per-section relaxation info. */ 2310 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) 2311 for (sec = abfd->sections; sec != NULL; sec = sec->next) 2312 { 2313 init_avr_relax_info (sec); 2314 } 2315 2316 /* Load the descriptor tables from .avr.prop sections. */ 2317 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) 2318 { 2319 struct avr_property_record_list *r_list; 2320 2321 r_list = avr_elf32_load_property_records (abfd); 2322 if (r_list != NULL) 2323 avr_elf32_assign_records_to_sections (r_list); 2324 2325 free (r_list); 2326 } 2327 2328 /* Now, for every section, ensure that the descriptor list in the 2329 relaxation data is sorted by ascending offset within the section. */ 2330 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) 2331 for (sec = abfd->sections; sec != NULL; sec = sec->next) 2332 { 2333 struct avr_relax_info *relax_info = get_avr_relax_info (sec); 2334 if (relax_info && relax_info->records.count > 0) 2335 { 2336 unsigned int i; 2337 2338 qsort (relax_info->records.items, 2339 relax_info->records.count, 2340 sizeof (struct avr_property_record), 2341 avr_property_record_compare); 2342 2343 /* For debug purposes, list all the descriptors. */ 2344 for (i = 0; i < relax_info->records.count; ++i) 2345 { 2346 switch (relax_info->records.items [i].type) 2347 { 2348 case RECORD_ORG: 2349 break; 2350 case RECORD_ORG_AND_FILL: 2351 break; 2352 case RECORD_ALIGN: 2353 break; 2354 case RECORD_ALIGN_AND_FILL: 2355 break; 2356 }; 2357 } 2358 } 2359 } 2360 } 2361 2362 /* This function handles relaxing for the avr. 2363 Many important relaxing opportunities within functions are already 2364 realized by the compiler itself. 2365 Here we try to replace call (4 bytes) -> rcall (2 bytes) 2366 and jump -> rjmp (safes also 2 bytes). 2367 As well we now optimize seqences of 2368 - call/rcall function 2369 - ret 2370 to yield 2371 - jmp/rjmp function 2372 - ret 2373 . In case that within a sequence 2374 - jmp/rjmp label 2375 - ret 2376 the ret could no longer be reached it is optimized away. In order 2377 to check if the ret is no longer needed, it is checked that the ret's address 2378 is not the target of a branch or jump within the same section, it is checked 2379 that there is no skip instruction before the jmp/rjmp and that there 2380 is no local or global label place at the address of the ret. 2381 2382 We refrain from relaxing within sections ".vectors" and 2383 ".jumptables" in order to maintain the position of the instructions. 2384 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop 2385 if possible. (In future one could possibly use the space of the nop 2386 for the first instruction of the irq service function. 2387 2388 The .jumptables sections is meant to be used for a future tablejump variant 2389 for the devices with 3-byte program counter where the table itself 2390 contains 4-byte jump instructions whose relative offset must not 2391 be changed. */ 2392 2393 static bfd_boolean 2394 elf32_avr_relax_section (bfd *abfd, 2395 asection *sec, 2396 struct bfd_link_info *link_info, 2397 bfd_boolean *again) 2398 { 2399 Elf_Internal_Shdr *symtab_hdr; 2400 Elf_Internal_Rela *internal_relocs; 2401 Elf_Internal_Rela *irel, *irelend; 2402 bfd_byte *contents = NULL; 2403 Elf_Internal_Sym *isymbuf = NULL; 2404 struct elf32_avr_link_hash_table *htab; 2405 static bfd_boolean relaxation_initialised = FALSE; 2406 2407 if (!relaxation_initialised) 2408 { 2409 relaxation_initialised = TRUE; 2410 2411 /* Load entries from the .avr.prop sections. */ 2412 avr_load_all_property_sections (link_info); 2413 } 2414 2415 /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while 2416 relaxing. Such shrinking can cause issues for the sections such 2417 as .vectors and .jumptables. Instead the unused bytes should be 2418 filled with nop instructions. */ 2419 bfd_boolean shrinkable = TRUE; 2420 2421 if (!strcmp (sec->name,".vectors") 2422 || !strcmp (sec->name,".jumptables")) 2423 shrinkable = FALSE; 2424 2425 if (bfd_link_relocatable (link_info)) 2426 (*link_info->callbacks->einfo) 2427 (_("%P%F: --relax and -r may not be used together\n")); 2428 2429 htab = avr_link_hash_table (link_info); 2430 if (htab == NULL) 2431 return FALSE; 2432 2433 /* Assume nothing changes. */ 2434 *again = FALSE; 2435 2436 if ((!htab->no_stubs) && (sec == htab->stub_sec)) 2437 { 2438 /* We are just relaxing the stub section. 2439 Let's calculate the size needed again. */ 2440 bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size; 2441 2442 if (debug_relax) 2443 printf ("Relaxing the stub section. Size prior to this pass: %i\n", 2444 (int) last_estimated_stub_section_size); 2445 2446 elf32_avr_size_stubs (htab->stub_sec->output_section->owner, 2447 link_info, FALSE); 2448 2449 /* Check if the number of trampolines changed. */ 2450 if (last_estimated_stub_section_size != htab->stub_sec->size) 2451 *again = TRUE; 2452 2453 if (debug_relax) 2454 printf ("Size of stub section after this pass: %i\n", 2455 (int) htab->stub_sec->size); 2456 2457 return TRUE; 2458 } 2459 2460 /* We don't have to do anything for a relocatable link, if 2461 this section does not have relocs, or if this is not a 2462 code section. */ 2463 if (bfd_link_relocatable (link_info) 2464 || (sec->flags & SEC_RELOC) == 0 2465 || sec->reloc_count == 0 2466 || (sec->flags & SEC_CODE) == 0) 2467 return TRUE; 2468 2469 /* Check if the object file to relax uses internal symbols so that we 2470 could fix up the relocations. */ 2471 if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED)) 2472 return TRUE; 2473 2474 symtab_hdr = &elf_tdata (abfd)->symtab_hdr; 2475 2476 /* Get a copy of the native relocations. */ 2477 internal_relocs = (_bfd_elf_link_read_relocs 2478 (abfd, sec, NULL, NULL, link_info->keep_memory)); 2479 if (internal_relocs == NULL) 2480 goto error_return; 2481 2482 /* Walk through the relocs looking for relaxing opportunities. */ 2483 irelend = internal_relocs + sec->reloc_count; 2484 for (irel = internal_relocs; irel < irelend; irel++) 2485 { 2486 bfd_vma symval; 2487 2488 if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL 2489 && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL 2490 && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL) 2491 continue; 2492 2493 /* Get the section contents if we haven't done so already. */ 2494 if (contents == NULL) 2495 { 2496 /* Get cached copy if it exists. */ 2497 if (elf_section_data (sec)->this_hdr.contents != NULL) 2498 contents = elf_section_data (sec)->this_hdr.contents; 2499 else 2500 { 2501 /* Go get them off disk. */ 2502 if (! bfd_malloc_and_get_section (abfd, sec, &contents)) 2503 goto error_return; 2504 } 2505 } 2506 2507 /* Read this BFD's local symbols if we haven't done so already. */ 2508 if (isymbuf == NULL && symtab_hdr->sh_info != 0) 2509 { 2510 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 2511 if (isymbuf == NULL) 2512 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, 2513 symtab_hdr->sh_info, 0, 2514 NULL, NULL, NULL); 2515 if (isymbuf == NULL) 2516 goto error_return; 2517 } 2518 2519 2520 /* Get the value of the symbol referred to by the reloc. */ 2521 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) 2522 { 2523 /* A local symbol. */ 2524 Elf_Internal_Sym *isym; 2525 asection *sym_sec; 2526 2527 isym = isymbuf + ELF32_R_SYM (irel->r_info); 2528 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); 2529 symval = isym->st_value; 2530 /* If the reloc is absolute, it will not have 2531 a symbol or section associated with it. */ 2532 if (sym_sec) 2533 symval += sym_sec->output_section->vma 2534 + sym_sec->output_offset; 2535 } 2536 else 2537 { 2538 unsigned long indx; 2539 struct elf_link_hash_entry *h; 2540 2541 /* An external symbol. */ 2542 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; 2543 h = elf_sym_hashes (abfd)[indx]; 2544 BFD_ASSERT (h != NULL); 2545 if (h->root.type != bfd_link_hash_defined 2546 && h->root.type != bfd_link_hash_defweak) 2547 /* This appears to be a reference to an undefined 2548 symbol. Just ignore it--it will be caught by the 2549 regular reloc processing. */ 2550 continue; 2551 2552 symval = (h->root.u.def.value 2553 + h->root.u.def.section->output_section->vma 2554 + h->root.u.def.section->output_offset); 2555 } 2556 2557 /* For simplicity of coding, we are going to modify the section 2558 contents, the section relocs, and the BFD symbol table. We 2559 must tell the rest of the code not to free up this 2560 information. It would be possible to instead create a table 2561 of changes which have to be made, as is done in coff-mips.c; 2562 that would be more work, but would require less memory when 2563 the linker is run. */ 2564 switch (ELF32_R_TYPE (irel->r_info)) 2565 { 2566 /* Try to turn a 22-bit absolute call/jump into an 13-bit 2567 pc-relative rcall/rjmp. */ 2568 case R_AVR_CALL: 2569 { 2570 bfd_vma value = symval + irel->r_addend; 2571 bfd_vma dot, gap; 2572 int distance_short_enough = 0; 2573 2574 /* Get the address of this instruction. */ 2575 dot = (sec->output_section->vma 2576 + sec->output_offset + irel->r_offset); 2577 2578 /* Compute the distance from this insn to the branch target. */ 2579 gap = value - dot; 2580 2581 /* Check if the gap falls in the range that can be accommodated 2582 in 13bits signed (It is 12bits when encoded, as we deal with 2583 word addressing). */ 2584 if (!shrinkable && ((int) gap >= -4096 && (int) gap <= 4095)) 2585 distance_short_enough = 1; 2586 /* If shrinkable, then we can check for a range of distance which 2587 is two bytes farther on both the directions because the call 2588 or jump target will be closer by two bytes after the 2589 relaxation. */ 2590 else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4097)) 2591 distance_short_enough = 1; 2592 2593 /* Here we handle the wrap-around case. E.g. for a 16k device 2594 we could use a rjmp to jump from address 0x100 to 0x3d00! 2595 In order to make this work properly, we need to fill the 2596 vaiable avr_pc_wrap_around with the appropriate value. 2597 I.e. 0x4000 for a 16k device. */ 2598 { 2599 /* Shrinking the code size makes the gaps larger in the 2600 case of wrap-arounds. So we use a heuristical safety 2601 margin to avoid that during relax the distance gets 2602 again too large for the short jumps. Let's assume 2603 a typical code-size reduction due to relax for a 2604 16k device of 600 bytes. So let's use twice the 2605 typical value as safety margin. */ 2606 int rgap; 2607 int safety_margin; 2608 2609 int assumed_shrink = 600; 2610 if (avr_pc_wrap_around > 0x4000) 2611 assumed_shrink = 900; 2612 2613 safety_margin = 2 * assumed_shrink; 2614 2615 rgap = avr_relative_distance_considering_wrap_around (gap); 2616 2617 if (rgap >= (-4092 + safety_margin) 2618 && rgap <= (4094 - safety_margin)) 2619 distance_short_enough = 1; 2620 } 2621 2622 if (distance_short_enough) 2623 { 2624 unsigned char code_msb; 2625 unsigned char code_lsb; 2626 2627 if (debug_relax) 2628 printf ("shrinking jump/call instruction at address 0x%x" 2629 " in section %s\n\n", 2630 (int) dot, sec->name); 2631 2632 /* Note that we've changed the relocs, section contents, 2633 etc. */ 2634 elf_section_data (sec)->relocs = internal_relocs; 2635 elf_section_data (sec)->this_hdr.contents = contents; 2636 symtab_hdr->contents = (unsigned char *) isymbuf; 2637 2638 /* Get the instruction code for relaxing. */ 2639 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset); 2640 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1); 2641 2642 /* Mask out the relocation bits. */ 2643 code_msb &= 0x94; 2644 code_lsb &= 0x0E; 2645 if (code_msb == 0x94 && code_lsb == 0x0E) 2646 { 2647 /* we are changing call -> rcall . */ 2648 bfd_put_8 (abfd, 0x00, contents + irel->r_offset); 2649 bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1); 2650 } 2651 else if (code_msb == 0x94 && code_lsb == 0x0C) 2652 { 2653 /* we are changeing jump -> rjmp. */ 2654 bfd_put_8 (abfd, 0x00, contents + irel->r_offset); 2655 bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1); 2656 } 2657 else 2658 abort (); 2659 2660 /* Fix the relocation's type. */ 2661 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), 2662 R_AVR_13_PCREL); 2663 2664 /* We should not modify the ordering if 'shrinkable' is 2665 FALSE. */ 2666 if (!shrinkable) 2667 { 2668 /* Let's insert a nop. */ 2669 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2); 2670 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3); 2671 } 2672 else 2673 { 2674 /* Delete two bytes of data. */ 2675 if (!elf32_avr_relax_delete_bytes (abfd, sec, 2676 irel->r_offset + 2, 2, 2677 TRUE)) 2678 goto error_return; 2679 2680 /* That will change things, so, we should relax again. 2681 Note that this is not required, and it may be slow. */ 2682 *again = TRUE; 2683 } 2684 } 2685 } 2686 2687 default: 2688 { 2689 unsigned char code_msb; 2690 unsigned char code_lsb; 2691 bfd_vma dot; 2692 2693 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1); 2694 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0); 2695 2696 /* Get the address of this instruction. */ 2697 dot = (sec->output_section->vma 2698 + sec->output_offset + irel->r_offset); 2699 2700 /* Here we look for rcall/ret or call/ret sequences that could be 2701 safely replaced by rjmp/ret or jmp/ret. */ 2702 if (((code_msb & 0xf0) == 0xd0) 2703 && avr_replace_call_ret_sequences) 2704 { 2705 /* This insn is a rcall. */ 2706 unsigned char next_insn_msb = 0; 2707 unsigned char next_insn_lsb = 0; 2708 2709 if (irel->r_offset + 3 < sec->size) 2710 { 2711 next_insn_msb = 2712 bfd_get_8 (abfd, contents + irel->r_offset + 3); 2713 next_insn_lsb = 2714 bfd_get_8 (abfd, contents + irel->r_offset + 2); 2715 } 2716 2717 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) 2718 { 2719 /* The next insn is a ret. We now convert the rcall insn 2720 into a rjmp instruction. */ 2721 code_msb &= 0xef; 2722 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1); 2723 if (debug_relax) 2724 printf ("converted rcall/ret sequence at address 0x%x" 2725 " into rjmp/ret sequence. Section is %s\n\n", 2726 (int) dot, sec->name); 2727 *again = TRUE; 2728 break; 2729 } 2730 } 2731 else if ((0x94 == (code_msb & 0xfe)) 2732 && (0x0e == (code_lsb & 0x0e)) 2733 && avr_replace_call_ret_sequences) 2734 { 2735 /* This insn is a call. */ 2736 unsigned char next_insn_msb = 0; 2737 unsigned char next_insn_lsb = 0; 2738 2739 if (irel->r_offset + 5 < sec->size) 2740 { 2741 next_insn_msb = 2742 bfd_get_8 (abfd, contents + irel->r_offset + 5); 2743 next_insn_lsb = 2744 bfd_get_8 (abfd, contents + irel->r_offset + 4); 2745 } 2746 2747 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) 2748 { 2749 /* The next insn is a ret. We now convert the call insn 2750 into a jmp instruction. */ 2751 2752 code_lsb &= 0xfd; 2753 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset); 2754 if (debug_relax) 2755 printf ("converted call/ret sequence at address 0x%x" 2756 " into jmp/ret sequence. Section is %s\n\n", 2757 (int) dot, sec->name); 2758 *again = TRUE; 2759 break; 2760 } 2761 } 2762 else if ((0xc0 == (code_msb & 0xf0)) 2763 || ((0x94 == (code_msb & 0xfe)) 2764 && (0x0c == (code_lsb & 0x0e)))) 2765 { 2766 /* This insn is a rjmp or a jmp. */ 2767 unsigned char next_insn_msb = 0; 2768 unsigned char next_insn_lsb = 0; 2769 int insn_size; 2770 2771 if (0xc0 == (code_msb & 0xf0)) 2772 insn_size = 2; /* rjmp insn */ 2773 else 2774 insn_size = 4; /* jmp insn */ 2775 2776 if (irel->r_offset + insn_size + 1 < sec->size) 2777 { 2778 next_insn_msb = 2779 bfd_get_8 (abfd, contents + irel->r_offset 2780 + insn_size + 1); 2781 next_insn_lsb = 2782 bfd_get_8 (abfd, contents + irel->r_offset 2783 + insn_size); 2784 } 2785 2786 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) 2787 { 2788 /* The next insn is a ret. We possibly could delete 2789 this ret. First we need to check for preceding 2790 sbis/sbic/sbrs or cpse "skip" instructions. */ 2791 2792 int there_is_preceding_non_skip_insn = 1; 2793 bfd_vma address_of_ret; 2794 2795 address_of_ret = dot + insn_size; 2796 2797 if (debug_relax && (insn_size == 2)) 2798 printf ("found rjmp / ret sequence at address 0x%x\n", 2799 (int) dot); 2800 if (debug_relax && (insn_size == 4)) 2801 printf ("found jmp / ret sequence at address 0x%x\n", 2802 (int) dot); 2803 2804 /* We have to make sure that there is a preceding insn. */ 2805 if (irel->r_offset >= 2) 2806 { 2807 unsigned char preceding_msb; 2808 unsigned char preceding_lsb; 2809 2810 preceding_msb = 2811 bfd_get_8 (abfd, contents + irel->r_offset - 1); 2812 preceding_lsb = 2813 bfd_get_8 (abfd, contents + irel->r_offset - 2); 2814 2815 /* sbic. */ 2816 if (0x99 == preceding_msb) 2817 there_is_preceding_non_skip_insn = 0; 2818 2819 /* sbis. */ 2820 if (0x9b == preceding_msb) 2821 there_is_preceding_non_skip_insn = 0; 2822 2823 /* sbrc */ 2824 if ((0xfc == (preceding_msb & 0xfe) 2825 && (0x00 == (preceding_lsb & 0x08)))) 2826 there_is_preceding_non_skip_insn = 0; 2827 2828 /* sbrs */ 2829 if ((0xfe == (preceding_msb & 0xfe) 2830 && (0x00 == (preceding_lsb & 0x08)))) 2831 there_is_preceding_non_skip_insn = 0; 2832 2833 /* cpse */ 2834 if (0x10 == (preceding_msb & 0xfc)) 2835 there_is_preceding_non_skip_insn = 0; 2836 2837 if (there_is_preceding_non_skip_insn == 0) 2838 if (debug_relax) 2839 printf ("preceding skip insn prevents deletion of" 2840 " ret insn at Addy 0x%x in section %s\n", 2841 (int) dot + 2, sec->name); 2842 } 2843 else 2844 { 2845 /* There is no previous instruction. */ 2846 there_is_preceding_non_skip_insn = 0; 2847 } 2848 2849 if (there_is_preceding_non_skip_insn) 2850 { 2851 /* We now only have to make sure that there is no 2852 local label defined at the address of the ret 2853 instruction and that there is no local relocation 2854 in this section pointing to the ret. */ 2855 2856 int deleting_ret_is_safe = 1; 2857 unsigned int section_offset_of_ret_insn = 2858 irel->r_offset + insn_size; 2859 Elf_Internal_Sym *isym, *isymend; 2860 unsigned int sec_shndx; 2861 struct bfd_section *isec; 2862 2863 sec_shndx = 2864 _bfd_elf_section_from_bfd_section (abfd, sec); 2865 2866 /* Check for local symbols. */ 2867 isym = (Elf_Internal_Sym *) symtab_hdr->contents; 2868 isymend = isym + symtab_hdr->sh_info; 2869 /* PR 6019: There may not be any local symbols. */ 2870 for (; isym != NULL && isym < isymend; isym++) 2871 { 2872 if (isym->st_value == section_offset_of_ret_insn 2873 && isym->st_shndx == sec_shndx) 2874 { 2875 deleting_ret_is_safe = 0; 2876 if (debug_relax) 2877 printf ("local label prevents deletion of ret " 2878 "insn at address 0x%x\n", 2879 (int) dot + insn_size); 2880 } 2881 } 2882 2883 /* Now check for global symbols. */ 2884 { 2885 int symcount; 2886 struct elf_link_hash_entry **sym_hashes; 2887 struct elf_link_hash_entry **end_hashes; 2888 2889 symcount = (symtab_hdr->sh_size 2890 / sizeof (Elf32_External_Sym) 2891 - symtab_hdr->sh_info); 2892 sym_hashes = elf_sym_hashes (abfd); 2893 end_hashes = sym_hashes + symcount; 2894 for (; sym_hashes < end_hashes; sym_hashes++) 2895 { 2896 struct elf_link_hash_entry *sym_hash = 2897 *sym_hashes; 2898 if ((sym_hash->root.type == bfd_link_hash_defined 2899 || sym_hash->root.type == 2900 bfd_link_hash_defweak) 2901 && sym_hash->root.u.def.section == sec 2902 && sym_hash->root.u.def.value == section_offset_of_ret_insn) 2903 { 2904 deleting_ret_is_safe = 0; 2905 if (debug_relax) 2906 printf ("global label prevents deletion of " 2907 "ret insn at address 0x%x\n", 2908 (int) dot + insn_size); 2909 } 2910 } 2911 } 2912 2913 /* Now we check for relocations pointing to ret. */ 2914 for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next) 2915 { 2916 Elf_Internal_Rela *rel; 2917 Elf_Internal_Rela *relend; 2918 2919 rel = elf_section_data (isec)->relocs; 2920 if (rel == NULL) 2921 rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE); 2922 2923 relend = rel + isec->reloc_count; 2924 2925 for (; rel && rel < relend; rel++) 2926 { 2927 bfd_vma reloc_target = 0; 2928 2929 /* Read this BFD's local symbols if we haven't 2930 done so already. */ 2931 if (isymbuf == NULL && symtab_hdr->sh_info != 0) 2932 { 2933 isymbuf = (Elf_Internal_Sym *) 2934 symtab_hdr->contents; 2935 if (isymbuf == NULL) 2936 isymbuf = bfd_elf_get_elf_syms 2937 (abfd, 2938 symtab_hdr, 2939 symtab_hdr->sh_info, 0, 2940 NULL, NULL, NULL); 2941 if (isymbuf == NULL) 2942 break; 2943 } 2944 2945 /* Get the value of the symbol referred to 2946 by the reloc. */ 2947 if (ELF32_R_SYM (rel->r_info) 2948 < symtab_hdr->sh_info) 2949 { 2950 /* A local symbol. */ 2951 asection *sym_sec; 2952 2953 isym = isymbuf 2954 + ELF32_R_SYM (rel->r_info); 2955 sym_sec = bfd_section_from_elf_index 2956 (abfd, isym->st_shndx); 2957 symval = isym->st_value; 2958 2959 /* If the reloc is absolute, it will not 2960 have a symbol or section associated 2961 with it. */ 2962 2963 if (sym_sec) 2964 { 2965 symval += 2966 sym_sec->output_section->vma 2967 + sym_sec->output_offset; 2968 reloc_target = symval + rel->r_addend; 2969 } 2970 else 2971 { 2972 reloc_target = symval + rel->r_addend; 2973 /* Reference symbol is absolute. */ 2974 } 2975 } 2976 /* else ... reference symbol is extern. */ 2977 2978 if (address_of_ret == reloc_target) 2979 { 2980 deleting_ret_is_safe = 0; 2981 if (debug_relax) 2982 printf ("ret from " 2983 "rjmp/jmp ret sequence at address" 2984 " 0x%x could not be deleted. ret" 2985 " is target of a relocation.\n", 2986 (int) address_of_ret); 2987 break; 2988 } 2989 } 2990 } 2991 2992 if (deleting_ret_is_safe) 2993 { 2994 if (debug_relax) 2995 printf ("unreachable ret instruction " 2996 "at address 0x%x deleted.\n", 2997 (int) dot + insn_size); 2998 2999 /* Delete two bytes of data. */ 3000 if (!elf32_avr_relax_delete_bytes (abfd, sec, 3001 irel->r_offset + insn_size, 2, 3002 TRUE)) 3003 goto error_return; 3004 3005 /* That will change things, so, we should relax 3006 again. Note that this is not required, and it 3007 may be slow. */ 3008 *again = TRUE; 3009 break; 3010 } 3011 } 3012 } 3013 } 3014 break; 3015 } 3016 } 3017 } 3018 3019 if (!*again) 3020 { 3021 /* Look through all the property records in this section to see if 3022 there's any alignment records that can be moved. */ 3023 struct avr_relax_info *relax_info; 3024 3025 relax_info = get_avr_relax_info (sec); 3026 if (relax_info->records.count > 0) 3027 { 3028 unsigned int i; 3029 3030 for (i = 0; i < relax_info->records.count; ++i) 3031 { 3032 switch (relax_info->records.items [i].type) 3033 { 3034 case RECORD_ORG: 3035 case RECORD_ORG_AND_FILL: 3036 break; 3037 case RECORD_ALIGN: 3038 case RECORD_ALIGN_AND_FILL: 3039 { 3040 struct avr_property_record *record; 3041 unsigned long bytes_to_align; 3042 int count = 0; 3043 3044 /* Look for alignment directives that have had enough 3045 bytes deleted before them, such that the directive 3046 can be moved backwards and still maintain the 3047 required alignment. */ 3048 record = &relax_info->records.items [i]; 3049 bytes_to_align 3050 = (unsigned long) (1 << record->data.align.bytes); 3051 while (record->data.align.preceding_deleted >= 3052 bytes_to_align) 3053 { 3054 record->data.align.preceding_deleted 3055 -= bytes_to_align; 3056 count += bytes_to_align; 3057 } 3058 3059 if (count > 0) 3060 { 3061 bfd_vma addr = record->offset; 3062 3063 /* We can delete COUNT bytes and this alignment 3064 directive will still be correctly aligned. 3065 First move the alignment directive, then delete 3066 the bytes. */ 3067 record->offset -= count; 3068 elf32_avr_relax_delete_bytes (abfd, sec, 3069 addr - count, 3070 count, FALSE); 3071 *again = TRUE; 3072 } 3073 } 3074 break; 3075 } 3076 } 3077 } 3078 } 3079 3080 if (contents != NULL 3081 && elf_section_data (sec)->this_hdr.contents != contents) 3082 { 3083 if (! link_info->keep_memory) 3084 free (contents); 3085 else 3086 { 3087 /* Cache the section contents for elf_link_input_bfd. */ 3088 elf_section_data (sec)->this_hdr.contents = contents; 3089 } 3090 } 3091 3092 if (internal_relocs != NULL 3093 && elf_section_data (sec)->relocs != internal_relocs) 3094 free (internal_relocs); 3095 3096 return TRUE; 3097 3098 error_return: 3099 if (isymbuf != NULL 3100 && symtab_hdr->contents != (unsigned char *) isymbuf) 3101 free (isymbuf); 3102 if (contents != NULL 3103 && elf_section_data (sec)->this_hdr.contents != contents) 3104 free (contents); 3105 if (internal_relocs != NULL 3106 && elf_section_data (sec)->relocs != internal_relocs) 3107 free (internal_relocs); 3108 3109 return FALSE; 3110 } 3111 3112 /* This is a version of bfd_generic_get_relocated_section_contents 3113 which uses elf32_avr_relocate_section. 3114 3115 For avr it's essentially a cut and paste taken from the H8300 port. 3116 The author of the relaxation support patch for avr had absolutely no 3117 clue what is happening here but found out that this part of the code 3118 seems to be important. */ 3119 3120 static bfd_byte * 3121 elf32_avr_get_relocated_section_contents (bfd *output_bfd, 3122 struct bfd_link_info *link_info, 3123 struct bfd_link_order *link_order, 3124 bfd_byte *data, 3125 bfd_boolean relocatable, 3126 asymbol **symbols) 3127 { 3128 Elf_Internal_Shdr *symtab_hdr; 3129 asection *input_section = link_order->u.indirect.section; 3130 bfd *input_bfd = input_section->owner; 3131 asection **sections = NULL; 3132 Elf_Internal_Rela *internal_relocs = NULL; 3133 Elf_Internal_Sym *isymbuf = NULL; 3134 3135 /* We only need to handle the case of relaxing, or of having a 3136 particular set of section contents, specially. */ 3137 if (relocatable 3138 || elf_section_data (input_section)->this_hdr.contents == NULL) 3139 return bfd_generic_get_relocated_section_contents (output_bfd, link_info, 3140 link_order, data, 3141 relocatable, 3142 symbols); 3143 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 3144 3145 memcpy (data, elf_section_data (input_section)->this_hdr.contents, 3146 (size_t) input_section->size); 3147 3148 if ((input_section->flags & SEC_RELOC) != 0 3149 && input_section->reloc_count > 0) 3150 { 3151 asection **secpp; 3152 Elf_Internal_Sym *isym, *isymend; 3153 bfd_size_type amt; 3154 3155 internal_relocs = (_bfd_elf_link_read_relocs 3156 (input_bfd, input_section, NULL, NULL, FALSE)); 3157 if (internal_relocs == NULL) 3158 goto error_return; 3159 3160 if (symtab_hdr->sh_info != 0) 3161 { 3162 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; 3163 if (isymbuf == NULL) 3164 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 3165 symtab_hdr->sh_info, 0, 3166 NULL, NULL, NULL); 3167 if (isymbuf == NULL) 3168 goto error_return; 3169 } 3170 3171 amt = symtab_hdr->sh_info; 3172 amt *= sizeof (asection *); 3173 sections = bfd_malloc (amt); 3174 if (sections == NULL && amt != 0) 3175 goto error_return; 3176 3177 isymend = isymbuf + symtab_hdr->sh_info; 3178 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) 3179 { 3180 asection *isec; 3181 3182 if (isym->st_shndx == SHN_UNDEF) 3183 isec = bfd_und_section_ptr; 3184 else if (isym->st_shndx == SHN_ABS) 3185 isec = bfd_abs_section_ptr; 3186 else if (isym->st_shndx == SHN_COMMON) 3187 isec = bfd_com_section_ptr; 3188 else 3189 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); 3190 3191 *secpp = isec; 3192 } 3193 3194 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd, 3195 input_section, data, internal_relocs, 3196 isymbuf, sections)) 3197 goto error_return; 3198 3199 if (sections != NULL) 3200 free (sections); 3201 if (isymbuf != NULL 3202 && symtab_hdr->contents != (unsigned char *) isymbuf) 3203 free (isymbuf); 3204 if (elf_section_data (input_section)->relocs != internal_relocs) 3205 free (internal_relocs); 3206 } 3207 3208 return data; 3209 3210 error_return: 3211 if (sections != NULL) 3212 free (sections); 3213 if (isymbuf != NULL 3214 && symtab_hdr->contents != (unsigned char *) isymbuf) 3215 free (isymbuf); 3216 if (internal_relocs != NULL 3217 && elf_section_data (input_section)->relocs != internal_relocs) 3218 free (internal_relocs); 3219 return NULL; 3220 } 3221 3222 3223 /* Determines the hash entry name for a particular reloc. It consists of 3224 the identifier of the symbol section and the added reloc addend and 3225 symbol offset relative to the section the symbol is attached to. */ 3226 3227 static char * 3228 avr_stub_name (const asection *symbol_section, 3229 const bfd_vma symbol_offset, 3230 const Elf_Internal_Rela *rela) 3231 { 3232 char *stub_name; 3233 bfd_size_type len; 3234 3235 len = 8 + 1 + 8 + 1 + 1; 3236 stub_name = bfd_malloc (len); 3237 3238 sprintf (stub_name, "%08x+%08x", 3239 symbol_section->id & 0xffffffff, 3240 (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset)); 3241 3242 return stub_name; 3243 } 3244 3245 3246 /* Add a new stub entry to the stub hash. Not all fields of the new 3247 stub entry are initialised. */ 3248 3249 static struct elf32_avr_stub_hash_entry * 3250 avr_add_stub (const char *stub_name, 3251 struct elf32_avr_link_hash_table *htab) 3252 { 3253 struct elf32_avr_stub_hash_entry *hsh; 3254 3255 /* Enter this entry into the linker stub hash table. */ 3256 hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE); 3257 3258 if (hsh == NULL) 3259 { 3260 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"), 3261 NULL, stub_name); 3262 return NULL; 3263 } 3264 3265 hsh->stub_offset = 0; 3266 return hsh; 3267 } 3268 3269 /* We assume that there is already space allocated for the stub section 3270 contents and that before building the stubs the section size is 3271 initialized to 0. We assume that within the stub hash table entry, 3272 the absolute position of the jmp target has been written in the 3273 target_value field. We write here the offset of the generated jmp insn 3274 relative to the trampoline section start to the stub_offset entry in 3275 the stub hash table entry. */ 3276 3277 static bfd_boolean 3278 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg) 3279 { 3280 struct elf32_avr_stub_hash_entry *hsh; 3281 struct bfd_link_info *info; 3282 struct elf32_avr_link_hash_table *htab; 3283 bfd *stub_bfd; 3284 bfd_byte *loc; 3285 bfd_vma target; 3286 bfd_vma starget; 3287 3288 /* Basic opcode */ 3289 bfd_vma jmp_insn = 0x0000940c; 3290 3291 /* Massage our args to the form they really have. */ 3292 hsh = avr_stub_hash_entry (bh); 3293 3294 if (!hsh->is_actually_needed) 3295 return TRUE; 3296 3297 info = (struct bfd_link_info *) in_arg; 3298 3299 htab = avr_link_hash_table (info); 3300 if (htab == NULL) 3301 return FALSE; 3302 3303 target = hsh->target_value; 3304 3305 /* Make a note of the offset within the stubs for this entry. */ 3306 hsh->stub_offset = htab->stub_sec->size; 3307 loc = htab->stub_sec->contents + hsh->stub_offset; 3308 3309 stub_bfd = htab->stub_sec->owner; 3310 3311 if (debug_stubs) 3312 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n", 3313 (unsigned int) target, 3314 (unsigned int) hsh->stub_offset); 3315 3316 /* We now have to add the information on the jump target to the bare 3317 opcode bits already set in jmp_insn. */ 3318 3319 /* Check for the alignment of the address. */ 3320 if (target & 1) 3321 return FALSE; 3322 3323 starget = target >> 1; 3324 jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16; 3325 bfd_put_16 (stub_bfd, jmp_insn, loc); 3326 bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2); 3327 3328 htab->stub_sec->size += 4; 3329 3330 /* Now add the entries in the address mapping table if there is still 3331 space left. */ 3332 { 3333 unsigned int nr; 3334 3335 nr = htab->amt_entry_cnt + 1; 3336 if (nr <= htab->amt_max_entry_cnt) 3337 { 3338 htab->amt_entry_cnt = nr; 3339 3340 htab->amt_stub_offsets[nr - 1] = hsh->stub_offset; 3341 htab->amt_destination_addr[nr - 1] = target; 3342 } 3343 } 3344 3345 return TRUE; 3346 } 3347 3348 static bfd_boolean 3349 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh, 3350 void *in_arg ATTRIBUTE_UNUSED) 3351 { 3352 struct elf32_avr_stub_hash_entry *hsh; 3353 3354 hsh = avr_stub_hash_entry (bh); 3355 hsh->is_actually_needed = FALSE; 3356 3357 return TRUE; 3358 } 3359 3360 static bfd_boolean 3361 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg) 3362 { 3363 struct elf32_avr_stub_hash_entry *hsh; 3364 struct elf32_avr_link_hash_table *htab; 3365 int size; 3366 3367 /* Massage our args to the form they really have. */ 3368 hsh = avr_stub_hash_entry (bh); 3369 htab = in_arg; 3370 3371 if (hsh->is_actually_needed) 3372 size = 4; 3373 else 3374 size = 0; 3375 3376 htab->stub_sec->size += size; 3377 return TRUE; 3378 } 3379 3380 void 3381 elf32_avr_setup_params (struct bfd_link_info *info, 3382 bfd *avr_stub_bfd, 3383 asection *avr_stub_section, 3384 bfd_boolean no_stubs, 3385 bfd_boolean deb_stubs, 3386 bfd_boolean deb_relax, 3387 bfd_vma pc_wrap_around, 3388 bfd_boolean call_ret_replacement) 3389 { 3390 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); 3391 3392 if (htab == NULL) 3393 return; 3394 htab->stub_sec = avr_stub_section; 3395 htab->stub_bfd = avr_stub_bfd; 3396 htab->no_stubs = no_stubs; 3397 3398 debug_relax = deb_relax; 3399 debug_stubs = deb_stubs; 3400 avr_pc_wrap_around = pc_wrap_around; 3401 avr_replace_call_ret_sequences = call_ret_replacement; 3402 } 3403 3404 3405 /* Set up various things so that we can make a list of input sections 3406 for each output section included in the link. Returns -1 on error, 3407 0 when no stubs will be needed, and 1 on success. It also sets 3408 information on the stubs bfd and the stub section in the info 3409 struct. */ 3410 3411 int 3412 elf32_avr_setup_section_lists (bfd *output_bfd, 3413 struct bfd_link_info *info) 3414 { 3415 bfd *input_bfd; 3416 unsigned int bfd_count; 3417 unsigned int top_id, top_index; 3418 asection *section; 3419 asection **input_list, **list; 3420 bfd_size_type amt; 3421 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); 3422 3423 if (htab == NULL || htab->no_stubs) 3424 return 0; 3425 3426 /* Count the number of input BFDs and find the top input section id. */ 3427 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0; 3428 input_bfd != NULL; 3429 input_bfd = input_bfd->link.next) 3430 { 3431 bfd_count += 1; 3432 for (section = input_bfd->sections; 3433 section != NULL; 3434 section = section->next) 3435 if (top_id < section->id) 3436 top_id = section->id; 3437 } 3438 3439 htab->bfd_count = bfd_count; 3440 3441 /* We can't use output_bfd->section_count here to find the top output 3442 section index as some sections may have been removed, and 3443 strip_excluded_output_sections doesn't renumber the indices. */ 3444 for (section = output_bfd->sections, top_index = 0; 3445 section != NULL; 3446 section = section->next) 3447 if (top_index < section->index) 3448 top_index = section->index; 3449 3450 htab->top_index = top_index; 3451 amt = sizeof (asection *) * (top_index + 1); 3452 input_list = bfd_malloc (amt); 3453 htab->input_list = input_list; 3454 if (input_list == NULL) 3455 return -1; 3456 3457 /* For sections we aren't interested in, mark their entries with a 3458 value we can check later. */ 3459 list = input_list + top_index; 3460 do 3461 *list = bfd_abs_section_ptr; 3462 while (list-- != input_list); 3463 3464 for (section = output_bfd->sections; 3465 section != NULL; 3466 section = section->next) 3467 if ((section->flags & SEC_CODE) != 0) 3468 input_list[section->index] = NULL; 3469 3470 return 1; 3471 } 3472 3473 3474 /* Read in all local syms for all input bfds, and create hash entries 3475 for export stubs if we are building a multi-subspace shared lib. 3476 Returns -1 on error, 0 otherwise. */ 3477 3478 static int 3479 get_local_syms (bfd *input_bfd, struct bfd_link_info *info) 3480 { 3481 unsigned int bfd_indx; 3482 Elf_Internal_Sym *local_syms, **all_local_syms; 3483 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); 3484 bfd_size_type amt; 3485 3486 if (htab == NULL) 3487 return -1; 3488 3489 /* We want to read in symbol extension records only once. To do this 3490 we need to read in the local symbols in parallel and save them for 3491 later use; so hold pointers to the local symbols in an array. */ 3492 amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count; 3493 all_local_syms = bfd_zmalloc (amt); 3494 htab->all_local_syms = all_local_syms; 3495 if (all_local_syms == NULL) 3496 return -1; 3497 3498 /* Walk over all the input BFDs, swapping in local symbols. 3499 If we are creating a shared library, create hash entries for the 3500 export stubs. */ 3501 for (bfd_indx = 0; 3502 input_bfd != NULL; 3503 input_bfd = input_bfd->link.next, bfd_indx++) 3504 { 3505 Elf_Internal_Shdr *symtab_hdr; 3506 3507 /* We'll need the symbol table in a second. */ 3508 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 3509 if (symtab_hdr->sh_info == 0) 3510 continue; 3511 3512 /* We need an array of the local symbols attached to the input bfd. */ 3513 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents; 3514 if (local_syms == NULL) 3515 { 3516 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, 3517 symtab_hdr->sh_info, 0, 3518 NULL, NULL, NULL); 3519 /* Cache them for elf_link_input_bfd. */ 3520 symtab_hdr->contents = (unsigned char *) local_syms; 3521 } 3522 if (local_syms == NULL) 3523 return -1; 3524 3525 all_local_syms[bfd_indx] = local_syms; 3526 } 3527 3528 return 0; 3529 } 3530 3531 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0 3532 3533 bfd_boolean 3534 elf32_avr_size_stubs (bfd *output_bfd, 3535 struct bfd_link_info *info, 3536 bfd_boolean is_prealloc_run) 3537 { 3538 struct elf32_avr_link_hash_table *htab; 3539 int stub_changed = 0; 3540 3541 htab = avr_link_hash_table (info); 3542 if (htab == NULL) 3543 return FALSE; 3544 3545 /* At this point we initialize htab->vector_base 3546 To the start of the text output section. */ 3547 htab->vector_base = htab->stub_sec->output_section->vma; 3548 3549 if (get_local_syms (info->input_bfds, info)) 3550 { 3551 if (htab->all_local_syms) 3552 goto error_ret_free_local; 3553 return FALSE; 3554 } 3555 3556 if (ADD_DUMMY_STUBS_FOR_DEBUGGING) 3557 { 3558 struct elf32_avr_stub_hash_entry *test; 3559 3560 test = avr_add_stub ("Hugo",htab); 3561 test->target_value = 0x123456; 3562 test->stub_offset = 13; 3563 3564 test = avr_add_stub ("Hugo2",htab); 3565 test->target_value = 0x84210; 3566 test->stub_offset = 14; 3567 } 3568 3569 while (1) 3570 { 3571 bfd *input_bfd; 3572 unsigned int bfd_indx; 3573 3574 /* We will have to re-generate the stub hash table each time anything 3575 in memory has changed. */ 3576 3577 bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab); 3578 for (input_bfd = info->input_bfds, bfd_indx = 0; 3579 input_bfd != NULL; 3580 input_bfd = input_bfd->link.next, bfd_indx++) 3581 { 3582 Elf_Internal_Shdr *symtab_hdr; 3583 asection *section; 3584 Elf_Internal_Sym *local_syms; 3585 3586 /* We'll need the symbol table in a second. */ 3587 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; 3588 if (symtab_hdr->sh_info == 0) 3589 continue; 3590 3591 local_syms = htab->all_local_syms[bfd_indx]; 3592 3593 /* Walk over each section attached to the input bfd. */ 3594 for (section = input_bfd->sections; 3595 section != NULL; 3596 section = section->next) 3597 { 3598 Elf_Internal_Rela *internal_relocs, *irelaend, *irela; 3599 3600 /* If there aren't any relocs, then there's nothing more 3601 to do. */ 3602 if ((section->flags & SEC_RELOC) == 0 3603 || section->reloc_count == 0) 3604 continue; 3605 3606 /* If this section is a link-once section that will be 3607 discarded, then don't create any stubs. */ 3608 if (section->output_section == NULL 3609 || section->output_section->owner != output_bfd) 3610 continue; 3611 3612 /* Get the relocs. */ 3613 internal_relocs 3614 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL, 3615 info->keep_memory); 3616 if (internal_relocs == NULL) 3617 goto error_ret_free_local; 3618 3619 /* Now examine each relocation. */ 3620 irela = internal_relocs; 3621 irelaend = irela + section->reloc_count; 3622 for (; irela < irelaend; irela++) 3623 { 3624 unsigned int r_type, r_indx; 3625 struct elf32_avr_stub_hash_entry *hsh; 3626 asection *sym_sec; 3627 bfd_vma sym_value; 3628 bfd_vma destination; 3629 struct elf_link_hash_entry *hh; 3630 char *stub_name; 3631 3632 r_type = ELF32_R_TYPE (irela->r_info); 3633 r_indx = ELF32_R_SYM (irela->r_info); 3634 3635 /* Only look for 16 bit GS relocs. No other reloc will need a 3636 stub. */ 3637 if (!((r_type == R_AVR_16_PM) 3638 || (r_type == R_AVR_LO8_LDI_GS) 3639 || (r_type == R_AVR_HI8_LDI_GS))) 3640 continue; 3641 3642 /* Now determine the call target, its name, value, 3643 section. */ 3644 sym_sec = NULL; 3645 sym_value = 0; 3646 destination = 0; 3647 hh = NULL; 3648 if (r_indx < symtab_hdr->sh_info) 3649 { 3650 /* It's a local symbol. */ 3651 Elf_Internal_Sym *sym; 3652 Elf_Internal_Shdr *hdr; 3653 unsigned int shndx; 3654 3655 sym = local_syms + r_indx; 3656 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) 3657 sym_value = sym->st_value; 3658 shndx = sym->st_shndx; 3659 if (shndx < elf_numsections (input_bfd)) 3660 { 3661 hdr = elf_elfsections (input_bfd)[shndx]; 3662 sym_sec = hdr->bfd_section; 3663 destination = (sym_value + irela->r_addend 3664 + sym_sec->output_offset 3665 + sym_sec->output_section->vma); 3666 } 3667 } 3668 else 3669 { 3670 /* It's an external symbol. */ 3671 int e_indx; 3672 3673 e_indx = r_indx - symtab_hdr->sh_info; 3674 hh = elf_sym_hashes (input_bfd)[e_indx]; 3675 3676 while (hh->root.type == bfd_link_hash_indirect 3677 || hh->root.type == bfd_link_hash_warning) 3678 hh = (struct elf_link_hash_entry *) 3679 (hh->root.u.i.link); 3680 3681 if (hh->root.type == bfd_link_hash_defined 3682 || hh->root.type == bfd_link_hash_defweak) 3683 { 3684 sym_sec = hh->root.u.def.section; 3685 sym_value = hh->root.u.def.value; 3686 if (sym_sec->output_section != NULL) 3687 destination = (sym_value + irela->r_addend 3688 + sym_sec->output_offset 3689 + sym_sec->output_section->vma); 3690 } 3691 else if (hh->root.type == bfd_link_hash_undefweak) 3692 { 3693 if (! bfd_link_pic (info)) 3694 continue; 3695 } 3696 else if (hh->root.type == bfd_link_hash_undefined) 3697 { 3698 if (! (info->unresolved_syms_in_objects == RM_IGNORE 3699 && (ELF_ST_VISIBILITY (hh->other) 3700 == STV_DEFAULT))) 3701 continue; 3702 } 3703 else 3704 { 3705 bfd_set_error (bfd_error_bad_value); 3706 3707 error_ret_free_internal: 3708 if (elf_section_data (section)->relocs == NULL) 3709 free (internal_relocs); 3710 goto error_ret_free_local; 3711 } 3712 } 3713 3714 if (! avr_stub_is_required_for_16_bit_reloc 3715 (destination - htab->vector_base)) 3716 { 3717 if (!is_prealloc_run) 3718 /* We are having a reloc that does't need a stub. */ 3719 continue; 3720 3721 /* We don't right now know if a stub will be needed. 3722 Let's rather be on the safe side. */ 3723 } 3724 3725 /* Get the name of this stub. */ 3726 stub_name = avr_stub_name (sym_sec, sym_value, irela); 3727 3728 if (!stub_name) 3729 goto error_ret_free_internal; 3730 3731 3732 hsh = avr_stub_hash_lookup (&htab->bstab, 3733 stub_name, 3734 FALSE, FALSE); 3735 if (hsh != NULL) 3736 { 3737 /* The proper stub has already been created. Mark it 3738 to be used and write the possibly changed destination 3739 value. */ 3740 hsh->is_actually_needed = TRUE; 3741 hsh->target_value = destination; 3742 free (stub_name); 3743 continue; 3744 } 3745 3746 hsh = avr_add_stub (stub_name, htab); 3747 if (hsh == NULL) 3748 { 3749 free (stub_name); 3750 goto error_ret_free_internal; 3751 } 3752 3753 hsh->is_actually_needed = TRUE; 3754 hsh->target_value = destination; 3755 3756 if (debug_stubs) 3757 printf ("Adding stub with destination 0x%x to the" 3758 " hash table.\n", (unsigned int) destination); 3759 if (debug_stubs) 3760 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run); 3761 3762 stub_changed = TRUE; 3763 } 3764 3765 /* We're done with the internal relocs, free them. */ 3766 if (elf_section_data (section)->relocs == NULL) 3767 free (internal_relocs); 3768 } 3769 } 3770 3771 /* Re-Calculate the number of needed stubs. */ 3772 htab->stub_sec->size = 0; 3773 bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab); 3774 3775 if (!stub_changed) 3776 break; 3777 3778 stub_changed = FALSE; 3779 } 3780 3781 free (htab->all_local_syms); 3782 return TRUE; 3783 3784 error_ret_free_local: 3785 free (htab->all_local_syms); 3786 return FALSE; 3787 } 3788 3789 3790 /* Build all the stubs associated with the current output file. The 3791 stubs are kept in a hash table attached to the main linker hash 3792 table. We also set up the .plt entries for statically linked PIC 3793 functions here. This function is called via hppaelf_finish in the 3794 linker. */ 3795 3796 bfd_boolean 3797 elf32_avr_build_stubs (struct bfd_link_info *info) 3798 { 3799 asection *stub_sec; 3800 struct bfd_hash_table *table; 3801 struct elf32_avr_link_hash_table *htab; 3802 bfd_size_type total_size = 0; 3803 3804 htab = avr_link_hash_table (info); 3805 if (htab == NULL) 3806 return FALSE; 3807 3808 /* In case that there were several stub sections: */ 3809 for (stub_sec = htab->stub_bfd->sections; 3810 stub_sec != NULL; 3811 stub_sec = stub_sec->next) 3812 { 3813 bfd_size_type size; 3814 3815 /* Allocate memory to hold the linker stubs. */ 3816 size = stub_sec->size; 3817 total_size += size; 3818 3819 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size); 3820 if (stub_sec->contents == NULL && size != 0) 3821 return FALSE; 3822 stub_sec->size = 0; 3823 } 3824 3825 /* Allocate memory for the adress mapping table. */ 3826 htab->amt_entry_cnt = 0; 3827 htab->amt_max_entry_cnt = total_size / 4; 3828 htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma) 3829 * htab->amt_max_entry_cnt); 3830 htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma) 3831 * htab->amt_max_entry_cnt ); 3832 3833 if (debug_stubs) 3834 printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt); 3835 3836 /* Build the stubs as directed by the stub hash table. */ 3837 table = &htab->bstab; 3838 bfd_hash_traverse (table, avr_build_one_stub, info); 3839 3840 if (debug_stubs) 3841 printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size); 3842 3843 return TRUE; 3844 } 3845 3846 /* Callback used by QSORT to order relocations AP and BP. */ 3847 3848 static int 3849 internal_reloc_compare (const void *ap, const void *bp) 3850 { 3851 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap; 3852 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp; 3853 3854 if (a->r_offset != b->r_offset) 3855 return (a->r_offset - b->r_offset); 3856 3857 /* We don't need to sort on these criteria for correctness, 3858 but enforcing a more strict ordering prevents unstable qsort 3859 from behaving differently with different implementations. 3860 Without the code below we get correct but different results 3861 on Solaris 2.7 and 2.8. We would like to always produce the 3862 same results no matter the host. */ 3863 3864 if (a->r_info != b->r_info) 3865 return (a->r_info - b->r_info); 3866 3867 return (a->r_addend - b->r_addend); 3868 } 3869 3870 /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */ 3871 3872 static bfd_boolean 3873 avr_is_section_for_address (bfd *abfd, asection *section, bfd_vma address) 3874 { 3875 bfd_vma vma; 3876 bfd_size_type size; 3877 3878 vma = bfd_get_section_vma (abfd, section); 3879 if (address < vma) 3880 return FALSE; 3881 3882 size = section->size; 3883 if (address >= vma + size) 3884 return FALSE; 3885 3886 return TRUE; 3887 } 3888 3889 /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */ 3890 3891 struct avr_find_section_data 3892 { 3893 /* The address we're looking for. */ 3894 bfd_vma address; 3895 3896 /* The section we've found. */ 3897 asection *section; 3898 }; 3899 3900 /* Helper function to locate the section holding a certain virtual memory 3901 address. This is called via bfd_map_over_sections. The DATA is an 3902 instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which 3903 has been set to the address to search for, and the section field has 3904 been set to NULL. If SECTION from ABFD contains ADDRESS then the 3905 section field in DATA will be set to SECTION. As an optimisation, if 3906 the section field is already non-null then this function does not 3907 perform any checks, and just returns. */ 3908 3909 static void 3910 avr_find_section_for_address (bfd *abfd, 3911 asection *section, void *data) 3912 { 3913 struct avr_find_section_data *fs_data 3914 = (struct avr_find_section_data *) data; 3915 3916 /* Return if already found. */ 3917 if (fs_data->section != NULL) 3918 return; 3919 3920 /* If this section isn't part of the addressable code content, skip it. */ 3921 if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0 3922 && (bfd_get_section_flags (abfd, section) & SEC_CODE) == 0) 3923 return; 3924 3925 if (avr_is_section_for_address (abfd, section, fs_data->address)) 3926 fs_data->section = section; 3927 } 3928 3929 /* Load all of the property records from SEC, a section from ABFD. Return 3930 a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The 3931 memory for the returned structure, and all of the records pointed too by 3932 the structure are allocated with a single call to malloc, so, only the 3933 pointer returned needs to be free'd. */ 3934 3935 static struct avr_property_record_list * 3936 avr_elf32_load_records_from_section (bfd *abfd, asection *sec) 3937 { 3938 char *contents = NULL, *ptr; 3939 bfd_size_type size, mem_size; 3940 bfd_byte version, flags; 3941 uint16_t record_count, i; 3942 struct avr_property_record_list *r_list = NULL; 3943 Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end; 3944 struct avr_find_section_data fs_data; 3945 3946 fs_data.section = NULL; 3947 3948 size = bfd_get_section_size (sec); 3949 contents = bfd_malloc (size); 3950 bfd_get_section_contents (abfd, sec, contents, 0, size); 3951 ptr = contents; 3952 3953 /* Load the relocations for the '.avr.prop' section if there are any, and 3954 sort them. */ 3955 internal_relocs = (_bfd_elf_link_read_relocs 3956 (abfd, sec, NULL, NULL, FALSE)); 3957 if (internal_relocs) 3958 qsort (internal_relocs, sec->reloc_count, 3959 sizeof (Elf_Internal_Rela), internal_reloc_compare); 3960 3961 /* There is a header at the start of the property record section SEC, the 3962 format of this header is: 3963 uint8_t : version number 3964 uint8_t : flags 3965 uint16_t : record counter 3966 */ 3967 3968 /* Check we have at least got a headers worth of bytes. */ 3969 if (size < AVR_PROPERTY_SECTION_HEADER_SIZE) 3970 goto load_failed; 3971 3972 version = *((bfd_byte *) ptr); 3973 ptr++; 3974 flags = *((bfd_byte *) ptr); 3975 ptr++; 3976 record_count = *((uint16_t *) ptr); 3977 ptr+=2; 3978 BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE); 3979 3980 /* Now allocate space for the list structure, and all of the list 3981 elements in a single block. */ 3982 mem_size = sizeof (struct avr_property_record_list) 3983 + sizeof (struct avr_property_record) * record_count; 3984 r_list = bfd_malloc (mem_size); 3985 if (r_list == NULL) 3986 goto load_failed; 3987 3988 r_list->version = version; 3989 r_list->flags = flags; 3990 r_list->section = sec; 3991 r_list->record_count = record_count; 3992 r_list->records = (struct avr_property_record *) (&r_list [1]); 3993 size -= AVR_PROPERTY_SECTION_HEADER_SIZE; 3994 3995 /* Check that we understand the version number. There is only one 3996 version number right now, anything else is an error. */ 3997 if (r_list->version != AVR_PROPERTY_RECORDS_VERSION) 3998 goto load_failed; 3999 4000 rel = internal_relocs; 4001 rel_end = rel + sec->reloc_count; 4002 for (i = 0; i < record_count; ++i) 4003 { 4004 bfd_vma address; 4005 4006 /* Each entry is a 32-bit address, followed by a single byte type. 4007 After that is the type specific data. We must take care to 4008 ensure that we don't read beyond the end of the section data. */ 4009 if (size < 5) 4010 goto load_failed; 4011 4012 r_list->records [i].section = NULL; 4013 r_list->records [i].offset = 0; 4014 4015 if (rel) 4016 { 4017 /* The offset of the address within the .avr.prop section. */ 4018 size_t offset = ptr - contents; 4019 4020 while (rel < rel_end && rel->r_offset < offset) 4021 ++rel; 4022 4023 if (rel == rel_end) 4024 rel = NULL; 4025 else if (rel->r_offset == offset) 4026 { 4027 /* Find section and section offset. */ 4028 unsigned long r_symndx; 4029 4030 asection * rel_sec; 4031 bfd_vma sec_offset; 4032 4033 r_symndx = ELF32_R_SYM (rel->r_info); 4034 rel_sec = get_elf_r_symndx_section (abfd, r_symndx); 4035 sec_offset = get_elf_r_symndx_offset (abfd, r_symndx) 4036 + rel->r_addend; 4037 4038 r_list->records [i].section = rel_sec; 4039 r_list->records [i].offset = sec_offset; 4040 } 4041 } 4042 4043 address = *((uint32_t *) ptr); 4044 ptr += 4; 4045 size -= 4; 4046 4047 if (r_list->records [i].section == NULL) 4048 { 4049 /* Try to find section and offset from address. */ 4050 if (fs_data.section != NULL 4051 && !avr_is_section_for_address (abfd, fs_data.section, 4052 address)) 4053 fs_data.section = NULL; 4054 4055 if (fs_data.section == NULL) 4056 { 4057 fs_data.address = address; 4058 bfd_map_over_sections (abfd, avr_find_section_for_address, 4059 &fs_data); 4060 } 4061 4062 if (fs_data.section == NULL) 4063 { 4064 fprintf (stderr, "Failed to find matching section.\n"); 4065 goto load_failed; 4066 } 4067 4068 r_list->records [i].section = fs_data.section; 4069 r_list->records [i].offset 4070 = address - bfd_get_section_vma (abfd, fs_data.section); 4071 } 4072 4073 r_list->records [i].type = *((bfd_byte *) ptr); 4074 ptr += 1; 4075 size -= 1; 4076 4077 switch (r_list->records [i].type) 4078 { 4079 case RECORD_ORG: 4080 /* Nothing else to load. */ 4081 break; 4082 case RECORD_ORG_AND_FILL: 4083 /* Just a 4-byte fill to load. */ 4084 if (size < 4) 4085 goto load_failed; 4086 r_list->records [i].data.org.fill = *((uint32_t *) ptr); 4087 ptr += 4; 4088 size -= 4; 4089 break; 4090 case RECORD_ALIGN: 4091 /* Just a 4-byte alignment to load. */ 4092 if (size < 4) 4093 goto load_failed; 4094 r_list->records [i].data.align.bytes = *((uint32_t *) ptr); 4095 ptr += 4; 4096 size -= 4; 4097 /* Just initialise PRECEDING_DELETED field, this field is 4098 used during linker relaxation. */ 4099 r_list->records [i].data.align.preceding_deleted = 0; 4100 break; 4101 case RECORD_ALIGN_AND_FILL: 4102 /* A 4-byte alignment, and a 4-byte fill to load. */ 4103 if (size < 8) 4104 goto load_failed; 4105 r_list->records [i].data.align.bytes = *((uint32_t *) ptr); 4106 ptr += 4; 4107 r_list->records [i].data.align.fill = *((uint32_t *) ptr); 4108 ptr += 4; 4109 size -= 8; 4110 /* Just initialise PRECEDING_DELETED field, this field is 4111 used during linker relaxation. */ 4112 r_list->records [i].data.align.preceding_deleted = 0; 4113 break; 4114 default: 4115 goto load_failed; 4116 } 4117 } 4118 4119 free (contents); 4120 if (elf_section_data (sec)->relocs != internal_relocs) 4121 free (internal_relocs); 4122 return r_list; 4123 4124 load_failed: 4125 if (elf_section_data (sec)->relocs != internal_relocs) 4126 free (internal_relocs); 4127 free (contents); 4128 free (r_list); 4129 return NULL; 4130 } 4131 4132 /* Load all of the property records from ABFD. See 4133 AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */ 4134 4135 struct avr_property_record_list * 4136 avr_elf32_load_property_records (bfd *abfd) 4137 { 4138 asection *sec; 4139 4140 /* Find the '.avr.prop' section and load the contents into memory. */ 4141 sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME); 4142 if (sec == NULL) 4143 return NULL; 4144 return avr_elf32_load_records_from_section (abfd, sec); 4145 } 4146 4147 const char * 4148 avr_elf32_property_record_name (struct avr_property_record *rec) 4149 { 4150 const char *str; 4151 4152 switch (rec->type) 4153 { 4154 case RECORD_ORG: 4155 str = "ORG"; 4156 break; 4157 case RECORD_ORG_AND_FILL: 4158 str = "ORG+FILL"; 4159 break; 4160 case RECORD_ALIGN: 4161 str = "ALIGN"; 4162 break; 4163 case RECORD_ALIGN_AND_FILL: 4164 str = "ALIGN+FILL"; 4165 break; 4166 default: 4167 str = "unknown"; 4168 } 4169 4170 return str; 4171 } 4172 4173 4174 #define ELF_ARCH bfd_arch_avr 4175 #define ELF_TARGET_ID AVR_ELF_DATA 4176 #define ELF_MACHINE_CODE EM_AVR 4177 #define ELF_MACHINE_ALT1 EM_AVR_OLD 4178 #define ELF_MAXPAGESIZE 1 4179 4180 #define TARGET_LITTLE_SYM avr_elf32_vec 4181 #define TARGET_LITTLE_NAME "elf32-avr" 4182 4183 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create 4184 4185 #define elf_info_to_howto avr_info_to_howto_rela 4186 #define elf_info_to_howto_rel NULL 4187 #define elf_backend_relocate_section elf32_avr_relocate_section 4188 #define elf_backend_can_gc_sections 1 4189 #define elf_backend_rela_normal 1 4190 #define elf_backend_final_write_processing \ 4191 bfd_elf_avr_final_write_processing 4192 #define elf_backend_object_p elf32_avr_object_p 4193 4194 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section 4195 #define bfd_elf32_bfd_get_relocated_section_contents \ 4196 elf32_avr_get_relocated_section_contents 4197 #define bfd_elf32_new_section_hook elf_avr_new_section_hook 4198 4199 #include "elf32-target.h" 4200