1 /* AVR-specific support for 32-bit ELF
2 Copyright (C) 1999-2022 Free Software Foundation, Inc.
3 Contributed by Denis Chertykov <denisc@overta.ru>
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 #include "elf32-avr.h"
29 /* Enable debugging printout at stdout with this variable. */
30 static bool debug_relax
= false;
32 /* Enable debugging printout at stdout with this variable. */
33 static bool debug_stubs
= false;
35 static bfd_reloc_status_type
36 bfd_elf_avr_diff_reloc (bfd
*, arelent
*, asymbol
*, void *,
37 asection
*, bfd
*, char **);
39 /* Hash table initialization and handling. Code is taken from the hppa port
40 and adapted to the needs of AVR. */
42 /* We use two hash tables to hold information for linking avr objects.
44 The first is the elf32_avr_link_hash_table which is derived from the
45 stanard ELF linker hash table. We use this as a place to attach the other
46 hash table and some static information.
48 The second is the stub hash table which is derived from the base BFD
49 hash table. The stub hash table holds the information on the linker
52 struct elf32_avr_stub_hash_entry
54 /* Base hash table entry structure. */
55 struct bfd_hash_entry bh_root
;
57 /* Offset within stub_sec of the beginning of this stub. */
60 /* Given the symbol's value and its section we can determine its final
61 value when building the stubs (so the stub knows where to jump). */
64 /* This way we could mark stubs to be no longer necessary. */
65 bool is_actually_needed
;
68 struct elf32_avr_link_hash_table
70 /* The main hash table. */
71 struct elf_link_hash_table etab
;
73 /* The stub hash table. */
74 struct bfd_hash_table bstab
;
78 /* Linker stub bfd. */
81 /* The stub section. */
84 /* Usually 0, unless we are generating code for a bootloader. Will
85 be initialized by elf32_avr_size_stubs to the vma offset of the
86 output section associated with the stub section. */
89 /* Assorted information used by elf32_avr_size_stubs. */
90 unsigned int bfd_count
;
91 unsigned int top_index
;
92 asection
** input_list
;
93 Elf_Internal_Sym
** all_local_syms
;
95 /* Tables for mapping vma beyond the 128k boundary to the address of the
96 corresponding stub. (AMT)
97 "amt_max_entry_cnt" reflects the number of entries that memory is allocated
98 for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
99 "amt_entry_cnt" informs how many of these entries actually contain
101 unsigned int amt_entry_cnt
;
102 unsigned int amt_max_entry_cnt
;
103 bfd_vma
* amt_stub_offsets
;
104 bfd_vma
* amt_destination_addr
;
107 /* Various hash macros and functions. */
108 #define avr_link_hash_table(p) \
109 ((is_elf_hash_table ((p)->hash) \
110 && elf_hash_table_id (elf_hash_table (p)) == AVR_ELF_DATA) \
111 ? (struct elf32_avr_link_hash_table *) (p)->hash : NULL)
113 #define avr_stub_hash_entry(ent) \
114 ((struct elf32_avr_stub_hash_entry *)(ent))
116 #define avr_stub_hash_lookup(table, string, create, copy) \
117 ((struct elf32_avr_stub_hash_entry *) \
118 bfd_hash_lookup ((table), (string), (create), (copy)))
120 static reloc_howto_type elf_avr_howto_table
[] =
122 HOWTO (R_AVR_NONE
, /* type */
126 false, /* pc_relative */
128 complain_overflow_dont
, /* complain_on_overflow */
129 bfd_elf_generic_reloc
, /* special_function */
130 "R_AVR_NONE", /* name */
131 false, /* partial_inplace */
134 false), /* pcrel_offset */
136 HOWTO (R_AVR_32
, /* type */
140 false, /* pc_relative */
142 complain_overflow_bitfield
, /* complain_on_overflow */
143 bfd_elf_generic_reloc
, /* special_function */
144 "R_AVR_32", /* name */
145 false, /* partial_inplace */
146 0xffffffff, /* src_mask */
147 0xffffffff, /* dst_mask */
148 false), /* pcrel_offset */
150 /* A 7 bit PC relative relocation. */
151 HOWTO (R_AVR_7_PCREL
, /* type */
155 true, /* pc_relative */
157 complain_overflow_bitfield
, /* complain_on_overflow */
158 bfd_elf_generic_reloc
, /* special_function */
159 "R_AVR_7_PCREL", /* name */
160 false, /* partial_inplace */
161 0xffff, /* src_mask */
162 0xffff, /* dst_mask */
163 true), /* pcrel_offset */
165 /* A 13 bit PC relative relocation. */
166 HOWTO (R_AVR_13_PCREL
, /* type */
170 true, /* pc_relative */
172 complain_overflow_bitfield
, /* complain_on_overflow */
173 bfd_elf_generic_reloc
, /* special_function */
174 "R_AVR_13_PCREL", /* name */
175 false, /* partial_inplace */
176 0xfff, /* src_mask */
177 0xfff, /* dst_mask */
178 true), /* pcrel_offset */
180 /* A 16 bit absolute relocation. */
181 HOWTO (R_AVR_16
, /* type */
185 false, /* pc_relative */
187 complain_overflow_dont
, /* complain_on_overflow */
188 bfd_elf_generic_reloc
, /* special_function */
189 "R_AVR_16", /* name */
190 false, /* partial_inplace */
191 0xffff, /* src_mask */
192 0xffff, /* dst_mask */
193 false), /* pcrel_offset */
195 /* A 16 bit absolute relocation for command address
196 Will be changed when linker stubs are needed. */
197 HOWTO (R_AVR_16_PM
, /* type */
201 false, /* pc_relative */
203 complain_overflow_bitfield
, /* complain_on_overflow */
204 bfd_elf_generic_reloc
, /* special_function */
205 "R_AVR_16_PM", /* name */
206 false, /* partial_inplace */
207 0xffff, /* src_mask */
208 0xffff, /* dst_mask */
209 false), /* pcrel_offset */
210 /* A low 8 bit absolute relocation of 16 bit address.
212 HOWTO (R_AVR_LO8_LDI
, /* type */
216 false, /* pc_relative */
218 complain_overflow_dont
, /* complain_on_overflow */
219 bfd_elf_generic_reloc
, /* special_function */
220 "R_AVR_LO8_LDI", /* name */
221 false, /* partial_inplace */
222 0xffff, /* src_mask */
223 0xffff, /* dst_mask */
224 false), /* pcrel_offset */
225 /* A high 8 bit absolute relocation of 16 bit address.
227 HOWTO (R_AVR_HI8_LDI
, /* type */
231 false, /* pc_relative */
233 complain_overflow_dont
, /* complain_on_overflow */
234 bfd_elf_generic_reloc
, /* special_function */
235 "R_AVR_HI8_LDI", /* name */
236 false, /* partial_inplace */
237 0xffff, /* src_mask */
238 0xffff, /* dst_mask */
239 false), /* pcrel_offset */
240 /* A high 6 bit absolute relocation of 22 bit address.
241 For LDI command. As well second most significant 8 bit value of
242 a 32 bit link-time constant. */
243 HOWTO (R_AVR_HH8_LDI
, /* type */
247 false, /* pc_relative */
249 complain_overflow_dont
, /* complain_on_overflow */
250 bfd_elf_generic_reloc
, /* special_function */
251 "R_AVR_HH8_LDI", /* name */
252 false, /* partial_inplace */
253 0xffff, /* src_mask */
254 0xffff, /* dst_mask */
255 false), /* pcrel_offset */
256 /* A negative low 8 bit absolute relocation of 16 bit address.
258 HOWTO (R_AVR_LO8_LDI_NEG
, /* type */
262 false, /* pc_relative */
264 complain_overflow_dont
, /* complain_on_overflow */
265 bfd_elf_generic_reloc
, /* special_function */
266 "R_AVR_LO8_LDI_NEG", /* name */
267 false, /* partial_inplace */
268 0xffff, /* src_mask */
269 0xffff, /* dst_mask */
270 false), /* pcrel_offset */
271 /* A negative high 8 bit absolute relocation of 16 bit address.
273 HOWTO (R_AVR_HI8_LDI_NEG
, /* type */
277 false, /* pc_relative */
279 complain_overflow_dont
, /* complain_on_overflow */
280 bfd_elf_generic_reloc
, /* special_function */
281 "R_AVR_HI8_LDI_NEG", /* name */
282 false, /* partial_inplace */
283 0xffff, /* src_mask */
284 0xffff, /* dst_mask */
285 false), /* pcrel_offset */
286 /* A negative high 6 bit absolute relocation of 22 bit address.
288 HOWTO (R_AVR_HH8_LDI_NEG
, /* type */
292 false, /* pc_relative */
294 complain_overflow_dont
, /* complain_on_overflow */
295 bfd_elf_generic_reloc
, /* special_function */
296 "R_AVR_HH8_LDI_NEG", /* name */
297 false, /* partial_inplace */
298 0xffff, /* src_mask */
299 0xffff, /* dst_mask */
300 false), /* pcrel_offset */
301 /* A low 8 bit absolute relocation of 24 bit program memory address.
302 For LDI command. Will not be changed when linker stubs are needed. */
303 HOWTO (R_AVR_LO8_LDI_PM
, /* type */
307 false, /* pc_relative */
309 complain_overflow_dont
, /* complain_on_overflow */
310 bfd_elf_generic_reloc
, /* special_function */
311 "R_AVR_LO8_LDI_PM", /* name */
312 false, /* partial_inplace */
313 0xffff, /* src_mask */
314 0xffff, /* dst_mask */
315 false), /* pcrel_offset */
316 /* A low 8 bit absolute relocation of 24 bit program memory address.
317 For LDI command. Will not be changed when linker stubs are needed. */
318 HOWTO (R_AVR_HI8_LDI_PM
, /* type */
322 false, /* pc_relative */
324 complain_overflow_dont
, /* complain_on_overflow */
325 bfd_elf_generic_reloc
, /* special_function */
326 "R_AVR_HI8_LDI_PM", /* name */
327 false, /* partial_inplace */
328 0xffff, /* src_mask */
329 0xffff, /* dst_mask */
330 false), /* pcrel_offset */
331 /* A low 8 bit absolute relocation of 24 bit program memory address.
332 For LDI command. Will not be changed when linker stubs are needed. */
333 HOWTO (R_AVR_HH8_LDI_PM
, /* type */
337 false, /* pc_relative */
339 complain_overflow_dont
, /* complain_on_overflow */
340 bfd_elf_generic_reloc
, /* special_function */
341 "R_AVR_HH8_LDI_PM", /* name */
342 false, /* partial_inplace */
343 0xffff, /* src_mask */
344 0xffff, /* dst_mask */
345 false), /* pcrel_offset */
346 /* A low 8 bit absolute relocation of 24 bit program memory address.
347 For LDI command. Will not be changed when linker stubs are needed. */
348 HOWTO (R_AVR_LO8_LDI_PM_NEG
, /* type */
352 false, /* pc_relative */
354 complain_overflow_dont
, /* complain_on_overflow */
355 bfd_elf_generic_reloc
, /* special_function */
356 "R_AVR_LO8_LDI_PM_NEG", /* name */
357 false, /* partial_inplace */
358 0xffff, /* src_mask */
359 0xffff, /* dst_mask */
360 false), /* pcrel_offset */
361 /* A low 8 bit absolute relocation of 24 bit program memory address.
362 For LDI command. Will not be changed when linker stubs are needed. */
363 HOWTO (R_AVR_HI8_LDI_PM_NEG
, /* type */
367 false, /* pc_relative */
369 complain_overflow_dont
, /* complain_on_overflow */
370 bfd_elf_generic_reloc
, /* special_function */
371 "R_AVR_HI8_LDI_PM_NEG", /* name */
372 false, /* partial_inplace */
373 0xffff, /* src_mask */
374 0xffff, /* dst_mask */
375 false), /* pcrel_offset */
376 /* A low 8 bit absolute relocation of 24 bit program memory address.
377 For LDI command. Will not be changed when linker stubs are needed. */
378 HOWTO (R_AVR_HH8_LDI_PM_NEG
, /* type */
382 false, /* pc_relative */
384 complain_overflow_dont
, /* complain_on_overflow */
385 bfd_elf_generic_reloc
, /* special_function */
386 "R_AVR_HH8_LDI_PM_NEG", /* name */
387 false, /* partial_inplace */
388 0xffff, /* src_mask */
389 0xffff, /* dst_mask */
390 false), /* pcrel_offset */
391 /* Relocation for CALL command in ATmega. */
392 HOWTO (R_AVR_CALL
, /* type */
396 false, /* pc_relative */
398 complain_overflow_dont
,/* complain_on_overflow */
399 bfd_elf_generic_reloc
, /* special_function */
400 "R_AVR_CALL", /* name */
401 false, /* partial_inplace */
402 0xffffffff, /* src_mask */
403 0xffffffff, /* dst_mask */
404 false), /* pcrel_offset */
405 /* A 16 bit absolute relocation of 16 bit address.
407 HOWTO (R_AVR_LDI
, /* type */
411 false, /* pc_relative */
413 complain_overflow_dont
,/* complain_on_overflow */
414 bfd_elf_generic_reloc
, /* special_function */
415 "R_AVR_LDI", /* name */
416 false, /* partial_inplace */
417 0xffff, /* src_mask */
418 0xffff, /* dst_mask */
419 false), /* pcrel_offset */
420 /* A 6 bit absolute relocation of 6 bit offset.
421 For ldd/sdd command. */
422 HOWTO (R_AVR_6
, /* type */
426 false, /* pc_relative */
428 complain_overflow_dont
,/* complain_on_overflow */
429 bfd_elf_generic_reloc
, /* special_function */
430 "R_AVR_6", /* name */
431 false, /* partial_inplace */
432 0xffff, /* src_mask */
433 0xffff, /* dst_mask */
434 false), /* pcrel_offset */
435 /* A 6 bit absolute relocation of 6 bit offset.
436 For sbiw/adiw command. */
437 HOWTO (R_AVR_6_ADIW
, /* type */
441 false, /* pc_relative */
443 complain_overflow_dont
,/* complain_on_overflow */
444 bfd_elf_generic_reloc
, /* special_function */
445 "R_AVR_6_ADIW", /* name */
446 false, /* partial_inplace */
447 0xffff, /* src_mask */
448 0xffff, /* dst_mask */
449 false), /* pcrel_offset */
450 /* Most significant 8 bit value of a 32 bit link-time constant. */
451 HOWTO (R_AVR_MS8_LDI
, /* type */
455 false, /* pc_relative */
457 complain_overflow_dont
, /* complain_on_overflow */
458 bfd_elf_generic_reloc
, /* special_function */
459 "R_AVR_MS8_LDI", /* name */
460 false, /* partial_inplace */
461 0xffff, /* src_mask */
462 0xffff, /* dst_mask */
463 false), /* pcrel_offset */
464 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
465 HOWTO (R_AVR_MS8_LDI_NEG
, /* type */
469 false, /* pc_relative */
471 complain_overflow_dont
, /* complain_on_overflow */
472 bfd_elf_generic_reloc
, /* special_function */
473 "R_AVR_MS8_LDI_NEG", /* name */
474 false, /* partial_inplace */
475 0xffff, /* src_mask */
476 0xffff, /* dst_mask */
477 false), /* pcrel_offset */
478 /* A low 8 bit absolute relocation of 24 bit program memory address.
479 For LDI command. Will be changed when linker stubs are needed. */
480 HOWTO (R_AVR_LO8_LDI_GS
, /* type */
484 false, /* pc_relative */
486 complain_overflow_dont
, /* complain_on_overflow */
487 bfd_elf_generic_reloc
, /* special_function */
488 "R_AVR_LO8_LDI_GS", /* name */
489 false, /* partial_inplace */
490 0xffff, /* src_mask */
491 0xffff, /* dst_mask */
492 false), /* pcrel_offset */
493 /* A low 8 bit absolute relocation of 24 bit program memory address.
494 For LDI command. Will be changed when linker stubs are needed. */
495 HOWTO (R_AVR_HI8_LDI_GS
, /* type */
499 false, /* pc_relative */
501 complain_overflow_dont
, /* complain_on_overflow */
502 bfd_elf_generic_reloc
, /* special_function */
503 "R_AVR_HI8_LDI_GS", /* name */
504 false, /* partial_inplace */
505 0xffff, /* src_mask */
506 0xffff, /* dst_mask */
507 false), /* pcrel_offset */
509 HOWTO (R_AVR_8
, /* type */
513 false, /* pc_relative */
515 complain_overflow_bitfield
,/* complain_on_overflow */
516 bfd_elf_generic_reloc
, /* special_function */
517 "R_AVR_8", /* name */
518 false, /* partial_inplace */
519 0x000000ff, /* src_mask */
520 0x000000ff, /* dst_mask */
521 false), /* pcrel_offset */
522 /* lo8-part to use in .byte lo8(sym). */
523 HOWTO (R_AVR_8_LO8
, /* type */
527 false, /* pc_relative */
529 complain_overflow_dont
,/* complain_on_overflow */
530 bfd_elf_generic_reloc
, /* special_function */
531 "R_AVR_8_LO8", /* name */
532 false, /* partial_inplace */
533 0xffffff, /* src_mask */
534 0xffffff, /* dst_mask */
535 false), /* pcrel_offset */
536 /* hi8-part to use in .byte hi8(sym). */
537 HOWTO (R_AVR_8_HI8
, /* type */
541 false, /* pc_relative */
543 complain_overflow_dont
,/* complain_on_overflow */
544 bfd_elf_generic_reloc
, /* special_function */
545 "R_AVR_8_HI8", /* name */
546 false, /* partial_inplace */
547 0xffffff, /* src_mask */
548 0xffffff, /* dst_mask */
549 false), /* pcrel_offset */
550 /* hlo8-part to use in .byte hlo8(sym). */
551 HOWTO (R_AVR_8_HLO8
, /* type */
555 false, /* pc_relative */
557 complain_overflow_dont
,/* complain_on_overflow */
558 bfd_elf_generic_reloc
, /* special_function */
559 "R_AVR_8_HLO8", /* name */
560 false, /* partial_inplace */
561 0xffffff, /* src_mask */
562 0xffffff, /* dst_mask */
563 false), /* pcrel_offset */
564 HOWTO (R_AVR_DIFF8
, /* type */
568 false, /* pc_relative */
570 complain_overflow_bitfield
, /* complain_on_overflow */
571 bfd_elf_avr_diff_reloc
, /* special_function */
572 "R_AVR_DIFF8", /* name */
573 false, /* partial_inplace */
576 false), /* pcrel_offset */
577 HOWTO (R_AVR_DIFF16
, /* type */
581 false, /* pc_relative */
583 complain_overflow_bitfield
, /* complain_on_overflow */
584 bfd_elf_avr_diff_reloc
,/* special_function */
585 "R_AVR_DIFF16", /* name */
586 false, /* partial_inplace */
588 0xffff, /* dst_mask */
589 false), /* pcrel_offset */
590 HOWTO (R_AVR_DIFF32
, /* type */
594 false, /* pc_relative */
596 complain_overflow_bitfield
, /* complain_on_overflow */
597 bfd_elf_avr_diff_reloc
,/* special_function */
598 "R_AVR_DIFF32", /* name */
599 false, /* partial_inplace */
601 0xffffffff, /* dst_mask */
602 false), /* pcrel_offset */
603 /* 7 bit immediate for LDS/STS in Tiny core. */
604 HOWTO (R_AVR_LDS_STS_16
, /* type */
608 false, /* pc_relative */
610 complain_overflow_dont
,/* complain_on_overflow */
611 bfd_elf_generic_reloc
, /* special_function */
612 "R_AVR_LDS_STS_16", /* name */
613 false, /* partial_inplace */
614 0xffff, /* src_mask */
615 0xffff, /* dst_mask */
616 false), /* pcrel_offset */
618 HOWTO (R_AVR_PORT6
, /* type */
622 false, /* pc_relative */
624 complain_overflow_dont
,/* complain_on_overflow */
625 bfd_elf_generic_reloc
, /* special_function */
626 "R_AVR_PORT6", /* name */
627 false, /* partial_inplace */
628 0xffffff, /* src_mask */
629 0xffffff, /* dst_mask */
630 false), /* pcrel_offset */
631 HOWTO (R_AVR_PORT5
, /* type */
635 false, /* pc_relative */
637 complain_overflow_dont
,/* complain_on_overflow */
638 bfd_elf_generic_reloc
, /* special_function */
639 "R_AVR_PORT5", /* name */
640 false, /* partial_inplace */
641 0xffffff, /* src_mask */
642 0xffffff, /* dst_mask */
643 false), /* pcrel_offset */
645 /* A 32 bit PC relative relocation. */
646 HOWTO (R_AVR_32_PCREL
, /* type */
650 true, /* pc_relative */
652 complain_overflow_bitfield
, /* complain_on_overflow */
653 bfd_elf_generic_reloc
, /* special_function */
654 "R_AVR_32_PCREL", /* name */
655 false, /* partial_inplace */
656 0xffffffff, /* src_mask */
657 0xffffffff, /* dst_mask */
658 true), /* pcrel_offset */
661 /* Map BFD reloc types to AVR ELF reloc types. */
665 bfd_reloc_code_real_type bfd_reloc_val
;
666 unsigned int elf_reloc_val
;
669 static const struct avr_reloc_map avr_reloc_map
[] =
671 { BFD_RELOC_NONE
, R_AVR_NONE
},
672 { BFD_RELOC_32
, R_AVR_32
},
673 { BFD_RELOC_AVR_7_PCREL
, R_AVR_7_PCREL
},
674 { BFD_RELOC_AVR_13_PCREL
, R_AVR_13_PCREL
},
675 { BFD_RELOC_16
, R_AVR_16
},
676 { BFD_RELOC_AVR_16_PM
, R_AVR_16_PM
},
677 { BFD_RELOC_AVR_LO8_LDI
, R_AVR_LO8_LDI
},
678 { BFD_RELOC_AVR_HI8_LDI
, R_AVR_HI8_LDI
},
679 { BFD_RELOC_AVR_HH8_LDI
, R_AVR_HH8_LDI
},
680 { BFD_RELOC_AVR_MS8_LDI
, R_AVR_MS8_LDI
},
681 { BFD_RELOC_AVR_LO8_LDI_NEG
, R_AVR_LO8_LDI_NEG
},
682 { BFD_RELOC_AVR_HI8_LDI_NEG
, R_AVR_HI8_LDI_NEG
},
683 { BFD_RELOC_AVR_HH8_LDI_NEG
, R_AVR_HH8_LDI_NEG
},
684 { BFD_RELOC_AVR_MS8_LDI_NEG
, R_AVR_MS8_LDI_NEG
},
685 { BFD_RELOC_AVR_LO8_LDI_PM
, R_AVR_LO8_LDI_PM
},
686 { BFD_RELOC_AVR_LO8_LDI_GS
, R_AVR_LO8_LDI_GS
},
687 { BFD_RELOC_AVR_HI8_LDI_PM
, R_AVR_HI8_LDI_PM
},
688 { BFD_RELOC_AVR_HI8_LDI_GS
, R_AVR_HI8_LDI_GS
},
689 { BFD_RELOC_AVR_HH8_LDI_PM
, R_AVR_HH8_LDI_PM
},
690 { BFD_RELOC_AVR_LO8_LDI_PM_NEG
, R_AVR_LO8_LDI_PM_NEG
},
691 { BFD_RELOC_AVR_HI8_LDI_PM_NEG
, R_AVR_HI8_LDI_PM_NEG
},
692 { BFD_RELOC_AVR_HH8_LDI_PM_NEG
, R_AVR_HH8_LDI_PM_NEG
},
693 { BFD_RELOC_AVR_CALL
, R_AVR_CALL
},
694 { BFD_RELOC_AVR_LDI
, R_AVR_LDI
},
695 { BFD_RELOC_AVR_6
, R_AVR_6
},
696 { BFD_RELOC_AVR_6_ADIW
, R_AVR_6_ADIW
},
697 { BFD_RELOC_8
, R_AVR_8
},
698 { BFD_RELOC_AVR_8_LO
, R_AVR_8_LO8
},
699 { BFD_RELOC_AVR_8_HI
, R_AVR_8_HI8
},
700 { BFD_RELOC_AVR_8_HLO
, R_AVR_8_HLO8
},
701 { BFD_RELOC_AVR_DIFF8
, R_AVR_DIFF8
},
702 { BFD_RELOC_AVR_DIFF16
, R_AVR_DIFF16
},
703 { BFD_RELOC_AVR_DIFF32
, R_AVR_DIFF32
},
704 { BFD_RELOC_AVR_LDS_STS_16
, R_AVR_LDS_STS_16
},
705 { BFD_RELOC_AVR_PORT6
, R_AVR_PORT6
},
706 { BFD_RELOC_AVR_PORT5
, R_AVR_PORT5
},
707 { BFD_RELOC_32_PCREL
, R_AVR_32_PCREL
}
710 static const struct bfd_elf_special_section elf_avr_special_sections
[] =
712 { STRING_COMMA_LEN (".noinit"), 0, SHT_NOBITS
, SHF_ALLOC
+ SHF_WRITE
},
716 /* Meant to be filled one day with the wrap around address for the
717 specific device. I.e. should get the value 0x4000 for 16k devices,
718 0x8000 for 32k devices and so on.
720 We initialize it here with a value of 0x1000000 resulting in
721 that we will never suggest a wrap-around jump during relaxation.
722 The logic of the source code later on assumes that in
723 avr_pc_wrap_around one single bit is set. */
724 static bfd_vma avr_pc_wrap_around
= 0x10000000;
726 /* If this variable holds a value different from zero, the linker relaxation
727 machine will try to optimize call/ret sequences by a single jump
728 instruction. This option could be switched off by a linker switch. */
729 static int avr_replace_call_ret_sequences
= 1;
732 /* Per-section relaxation related information for avr. */
734 struct avr_relax_info
736 /* Track the avr property records that apply to this section. */
740 /* Number of records in the list. */
743 /* How many records worth of space have we allocated. */
746 /* The records, only COUNT records are initialised. */
747 struct avr_property_record
*items
;
751 /* Per section data, specialised for avr. */
753 struct elf_avr_section_data
755 /* The standard data must appear first. */
756 struct bfd_elf_section_data elf
;
758 /* Relaxation related information. */
759 struct avr_relax_info relax_info
;
762 /* Possibly initialise avr specific data for new section SEC from ABFD. */
765 elf_avr_new_section_hook (bfd
*abfd
, asection
*sec
)
767 if (!sec
->used_by_bfd
)
769 struct elf_avr_section_data
*sdata
;
770 size_t amt
= sizeof (*sdata
);
772 sdata
= bfd_zalloc (abfd
, amt
);
775 sec
->used_by_bfd
= sdata
;
778 return _bfd_elf_new_section_hook (abfd
, sec
);
781 /* Return a pointer to the relaxation information for SEC. */
783 static struct avr_relax_info
*
784 get_avr_relax_info (asection
*sec
)
786 struct elf_avr_section_data
*section_data
;
788 /* No info available if no section or if it is an output section. */
789 if (!sec
|| sec
== sec
->output_section
)
792 section_data
= (struct elf_avr_section_data
*) elf_section_data (sec
);
793 return §ion_data
->relax_info
;
796 /* Initialise the per section relaxation information for SEC. */
799 init_avr_relax_info (asection
*sec
)
801 struct avr_relax_info
*relax_info
= get_avr_relax_info (sec
);
803 relax_info
->records
.count
= 0;
804 relax_info
->records
.allocated
= 0;
805 relax_info
->records
.items
= NULL
;
808 /* Initialize an entry in the stub hash table. */
810 static struct bfd_hash_entry
*
811 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
812 struct bfd_hash_table
*table
,
815 /* Allocate the structure if it has not already been allocated by a
819 entry
= bfd_hash_allocate (table
,
820 sizeof (struct elf32_avr_stub_hash_entry
));
825 /* Call the allocation method of the superclass. */
826 entry
= bfd_hash_newfunc (entry
, table
, string
);
829 struct elf32_avr_stub_hash_entry
*hsh
;
831 /* Initialize the local fields. */
832 hsh
= avr_stub_hash_entry (entry
);
833 hsh
->stub_offset
= 0;
834 hsh
->target_value
= 0;
840 /* This function is just a straight passthrough to the real
841 function in linker.c. Its prupose is so that its address
842 can be compared inside the avr_link_hash_table macro. */
844 static struct bfd_hash_entry
*
845 elf32_avr_link_hash_newfunc (struct bfd_hash_entry
* entry
,
846 struct bfd_hash_table
* table
,
849 return _bfd_elf_link_hash_newfunc (entry
, table
, string
);
852 /* Free the derived linker hash table. */
855 elf32_avr_link_hash_table_free (bfd
*obfd
)
857 struct elf32_avr_link_hash_table
*htab
858 = (struct elf32_avr_link_hash_table
*) obfd
->link
.hash
;
860 /* Free the address mapping table. */
861 free (htab
->amt_stub_offsets
);
862 free (htab
->amt_destination_addr
);
864 bfd_hash_table_free (&htab
->bstab
);
865 _bfd_elf_link_hash_table_free (obfd
);
868 /* Create the derived linker hash table. The AVR ELF port uses the derived
869 hash table to keep information specific to the AVR ELF linker (without
870 using static variables). */
872 static struct bfd_link_hash_table
*
873 elf32_avr_link_hash_table_create (bfd
*abfd
)
875 struct elf32_avr_link_hash_table
*htab
;
876 size_t amt
= sizeof (*htab
);
878 htab
= bfd_zmalloc (amt
);
882 if (!_bfd_elf_link_hash_table_init (&htab
->etab
, abfd
,
883 elf32_avr_link_hash_newfunc
,
884 sizeof (struct elf_link_hash_entry
),
891 /* Init the stub hash table too. */
892 if (!bfd_hash_table_init (&htab
->bstab
, stub_hash_newfunc
,
893 sizeof (struct elf32_avr_stub_hash_entry
)))
895 _bfd_elf_link_hash_table_free (abfd
);
898 htab
->etab
.root
.hash_table_free
= elf32_avr_link_hash_table_free
;
900 return &htab
->etab
.root
;
903 /* Calculates the effective distance of a pc relative jump/call. */
906 avr_relative_distance_considering_wrap_around (unsigned int distance
)
908 unsigned int wrap_around_mask
= avr_pc_wrap_around
- 1;
909 int dist_with_wrap_around
= distance
& wrap_around_mask
;
911 if (dist_with_wrap_around
>= ((int) (avr_pc_wrap_around
>> 1)))
912 dist_with_wrap_around
-= avr_pc_wrap_around
;
914 return dist_with_wrap_around
;
918 static reloc_howto_type
*
919 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
920 bfd_reloc_code_real_type code
)
925 i
< sizeof (avr_reloc_map
) / sizeof (struct avr_reloc_map
);
927 if (avr_reloc_map
[i
].bfd_reloc_val
== code
)
928 return &elf_avr_howto_table
[avr_reloc_map
[i
].elf_reloc_val
];
933 static reloc_howto_type
*
934 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
940 i
< sizeof (elf_avr_howto_table
) / sizeof (elf_avr_howto_table
[0]);
942 if (elf_avr_howto_table
[i
].name
!= NULL
943 && strcasecmp (elf_avr_howto_table
[i
].name
, r_name
) == 0)
944 return &elf_avr_howto_table
[i
];
949 /* Set the howto pointer for an AVR ELF reloc. */
952 avr_info_to_howto_rela (bfd
*abfd
,
954 Elf_Internal_Rela
*dst
)
958 r_type
= ELF32_R_TYPE (dst
->r_info
);
959 if (r_type
>= (unsigned int) R_AVR_max
)
961 /* xgettext:c-format */
962 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
964 bfd_set_error (bfd_error_bad_value
);
967 cache_ptr
->howto
= &elf_avr_howto_table
[r_type
];
972 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation
)
974 return (relocation
>= 0x020000);
977 /* Returns the address of the corresponding stub if there is one.
978 Returns otherwise an address above 0x020000. This function
979 could also be used, if there is no knowledge on the section where
980 the destination is found. */
983 avr_get_stub_addr (bfd_vma srel
,
984 struct elf32_avr_link_hash_table
*htab
)
987 bfd_vma stub_sec_addr
=
988 (htab
->stub_sec
->output_section
->vma
+
989 htab
->stub_sec
->output_offset
);
991 for (sindex
= 0; sindex
< htab
->amt_max_entry_cnt
; sindex
++)
992 if (htab
->amt_destination_addr
[sindex
] == srel
)
993 return htab
->amt_stub_offsets
[sindex
] + stub_sec_addr
;
995 /* Return an address that could not be reached by 16 bit relocs. */
999 /* Perform a diff relocation. Nothing to do, as the difference value is already
1000 written into the section's contents. */
1002 static bfd_reloc_status_type
1003 bfd_elf_avr_diff_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
1004 arelent
*reloc_entry ATTRIBUTE_UNUSED
,
1005 asymbol
*symbol ATTRIBUTE_UNUSED
,
1006 void *data ATTRIBUTE_UNUSED
,
1007 asection
*input_section ATTRIBUTE_UNUSED
,
1008 bfd
*output_bfd ATTRIBUTE_UNUSED
,
1009 char **error_message ATTRIBUTE_UNUSED
)
1011 return bfd_reloc_ok
;
1015 /* Perform a single relocation. By default we use the standard BFD
1016 routines, but a few relocs, we have to do them ourselves. */
1018 static bfd_reloc_status_type
1019 avr_final_link_relocate (reloc_howto_type
* howto
,
1021 asection
* input_section
,
1022 bfd_byte
* contents
,
1023 Elf_Internal_Rela
* rel
,
1025 struct elf32_avr_link_hash_table
* htab
)
1027 bfd_reloc_status_type r
= bfd_reloc_ok
;
1029 bfd_signed_vma srel
;
1030 bfd_signed_vma reloc_addr
;
1031 bool use_stubs
= false;
1032 /* Usually is 0, unless we are generating code for a bootloader. */
1033 bfd_signed_vma base_addr
= htab
->vector_base
;
1035 /* Absolute addr of the reloc in the final excecutable. */
1036 reloc_addr
= rel
->r_offset
+ input_section
->output_section
->vma
1037 + input_section
->output_offset
;
1039 switch (howto
->type
)
1042 contents
+= rel
->r_offset
;
1043 srel
= (bfd_signed_vma
) relocation
;
1044 srel
+= rel
->r_addend
;
1045 srel
-= rel
->r_offset
;
1046 srel
-= 2; /* Branch instructions add 2 to the PC... */
1047 srel
-= (input_section
->output_section
->vma
+
1048 input_section
->output_offset
);
1051 return bfd_reloc_outofrange
;
1052 if (srel
> ((1 << 7) - 1) || (srel
< - (1 << 7)))
1053 return bfd_reloc_overflow
;
1054 x
= bfd_get_16 (input_bfd
, contents
);
1055 x
= (x
& 0xfc07) | (((srel
>> 1) * 8) & 0x3f8);
1056 bfd_put_16 (input_bfd
, x
, contents
);
1059 case R_AVR_13_PCREL
:
1060 contents
+= rel
->r_offset
;
1061 srel
= (bfd_signed_vma
) relocation
;
1062 srel
+= rel
->r_addend
;
1063 srel
-= rel
->r_offset
;
1064 srel
-= 2; /* Branch instructions add 2 to the PC... */
1065 srel
-= (input_section
->output_section
->vma
+
1066 input_section
->output_offset
);
1069 return bfd_reloc_outofrange
;
1071 srel
= avr_relative_distance_considering_wrap_around (srel
);
1073 /* AVR addresses commands as words. */
1076 /* Check for overflow. */
1077 if (srel
< -2048 || srel
> 2047)
1079 /* Relative distance is too large. */
1081 /* Always apply WRAPAROUND for avr2, avr25, and avr4. */
1082 switch (bfd_get_mach (input_bfd
))
1085 case bfd_mach_avr25
:
1090 return bfd_reloc_overflow
;
1094 x
= bfd_get_16 (input_bfd
, contents
);
1095 x
= (x
& 0xf000) | (srel
& 0xfff);
1096 bfd_put_16 (input_bfd
, x
, contents
);
1100 contents
+= rel
->r_offset
;
1101 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1102 x
= bfd_get_16 (input_bfd
, contents
);
1103 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1104 bfd_put_16 (input_bfd
, x
, contents
);
1108 contents
+= rel
->r_offset
;
1109 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1110 if (((srel
> 0) && (srel
& 0xffff) > 255)
1111 || ((srel
< 0) && ((-srel
) & 0xffff) > 128))
1112 /* Remove offset for data/eeprom section. */
1113 return bfd_reloc_overflow
;
1115 x
= bfd_get_16 (input_bfd
, contents
);
1116 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1117 bfd_put_16 (input_bfd
, x
, contents
);
1121 contents
+= rel
->r_offset
;
1122 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1123 if (((srel
& 0xffff) > 63) || (srel
< 0))
1124 /* Remove offset for data/eeprom section. */
1125 return bfd_reloc_overflow
;
1126 x
= bfd_get_16 (input_bfd
, contents
);
1127 x
= (x
& 0xd3f8) | ((srel
& 7) | ((srel
& (3 << 3)) << 7)
1128 | ((srel
& (1 << 5)) << 8));
1129 bfd_put_16 (input_bfd
, x
, contents
);
1133 contents
+= rel
->r_offset
;
1134 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1135 if (((srel
& 0xffff) > 63) || (srel
< 0))
1136 /* Remove offset for data/eeprom section. */
1137 return bfd_reloc_overflow
;
1138 x
= bfd_get_16 (input_bfd
, contents
);
1139 x
= (x
& 0xff30) | (srel
& 0xf) | ((srel
& 0x30) << 2);
1140 bfd_put_16 (input_bfd
, x
, contents
);
1144 contents
+= rel
->r_offset
;
1145 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1146 srel
= (srel
>> 8) & 0xff;
1147 x
= bfd_get_16 (input_bfd
, contents
);
1148 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1149 bfd_put_16 (input_bfd
, x
, contents
);
1153 contents
+= rel
->r_offset
;
1154 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1155 srel
= (srel
>> 16) & 0xff;
1156 x
= bfd_get_16 (input_bfd
, contents
);
1157 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1158 bfd_put_16 (input_bfd
, x
, contents
);
1162 contents
+= rel
->r_offset
;
1163 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1164 srel
= (srel
>> 24) & 0xff;
1165 x
= bfd_get_16 (input_bfd
, contents
);
1166 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1167 bfd_put_16 (input_bfd
, x
, contents
);
1170 case R_AVR_LO8_LDI_NEG
:
1171 contents
+= rel
->r_offset
;
1172 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1174 x
= bfd_get_16 (input_bfd
, contents
);
1175 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1176 bfd_put_16 (input_bfd
, x
, contents
);
1179 case R_AVR_HI8_LDI_NEG
:
1180 contents
+= rel
->r_offset
;
1181 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1183 srel
= (srel
>> 8) & 0xff;
1184 x
= bfd_get_16 (input_bfd
, contents
);
1185 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1186 bfd_put_16 (input_bfd
, x
, contents
);
1189 case R_AVR_HH8_LDI_NEG
:
1190 contents
+= rel
->r_offset
;
1191 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1193 srel
= (srel
>> 16) & 0xff;
1194 x
= bfd_get_16 (input_bfd
, contents
);
1195 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1196 bfd_put_16 (input_bfd
, x
, contents
);
1199 case R_AVR_MS8_LDI_NEG
:
1200 contents
+= rel
->r_offset
;
1201 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1203 srel
= (srel
>> 24) & 0xff;
1204 x
= bfd_get_16 (input_bfd
, contents
);
1205 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1206 bfd_put_16 (input_bfd
, x
, contents
);
1209 case R_AVR_LO8_LDI_GS
:
1210 use_stubs
= (!htab
->no_stubs
);
1212 case R_AVR_LO8_LDI_PM
:
1213 contents
+= rel
->r_offset
;
1214 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1217 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1219 bfd_vma old_srel
= srel
;
1221 /* We need to use the address of the stub instead. */
1222 srel
= avr_get_stub_addr (srel
, htab
);
1224 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1225 "reloc at address 0x%x.\n",
1226 (unsigned int) srel
,
1227 (unsigned int) old_srel
,
1228 (unsigned int) reloc_addr
);
1230 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1231 return bfd_reloc_outofrange
;
1235 return bfd_reloc_outofrange
;
1237 x
= bfd_get_16 (input_bfd
, contents
);
1238 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1239 bfd_put_16 (input_bfd
, x
, contents
);
1242 case R_AVR_HI8_LDI_GS
:
1243 use_stubs
= (!htab
->no_stubs
);
1245 case R_AVR_HI8_LDI_PM
:
1246 contents
+= rel
->r_offset
;
1247 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1250 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1252 bfd_vma old_srel
= srel
;
1254 /* We need to use the address of the stub instead. */
1255 srel
= avr_get_stub_addr (srel
, htab
);
1257 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1258 "reloc at address 0x%x.\n",
1259 (unsigned int) srel
,
1260 (unsigned int) old_srel
,
1261 (unsigned int) reloc_addr
);
1263 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1264 return bfd_reloc_outofrange
;
1268 return bfd_reloc_outofrange
;
1270 srel
= (srel
>> 8) & 0xff;
1271 x
= bfd_get_16 (input_bfd
, contents
);
1272 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1273 bfd_put_16 (input_bfd
, x
, contents
);
1276 case R_AVR_HH8_LDI_PM
:
1277 contents
+= rel
->r_offset
;
1278 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1280 return bfd_reloc_outofrange
;
1282 srel
= (srel
>> 16) & 0xff;
1283 x
= bfd_get_16 (input_bfd
, contents
);
1284 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1285 bfd_put_16 (input_bfd
, x
, contents
);
1288 case R_AVR_LO8_LDI_PM_NEG
:
1289 contents
+= rel
->r_offset
;
1290 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1293 return bfd_reloc_outofrange
;
1295 x
= bfd_get_16 (input_bfd
, contents
);
1296 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1297 bfd_put_16 (input_bfd
, x
, contents
);
1300 case R_AVR_HI8_LDI_PM_NEG
:
1301 contents
+= rel
->r_offset
;
1302 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1305 return bfd_reloc_outofrange
;
1307 srel
= (srel
>> 8) & 0xff;
1308 x
= bfd_get_16 (input_bfd
, contents
);
1309 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1310 bfd_put_16 (input_bfd
, x
, contents
);
1313 case R_AVR_HH8_LDI_PM_NEG
:
1314 contents
+= rel
->r_offset
;
1315 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1318 return bfd_reloc_outofrange
;
1320 srel
= (srel
>> 16) & 0xff;
1321 x
= bfd_get_16 (input_bfd
, contents
);
1322 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1323 bfd_put_16 (input_bfd
, x
, contents
);
1327 contents
+= rel
->r_offset
;
1328 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1330 return bfd_reloc_outofrange
;
1332 x
= bfd_get_16 (input_bfd
, contents
);
1333 x
|= ((srel
& 0x10000) | ((srel
<< 3) & 0x1f00000)) >> 16;
1334 bfd_put_16 (input_bfd
, x
, contents
);
1335 bfd_put_16 (input_bfd
, (bfd_vma
) srel
& 0xffff, contents
+2);
1339 use_stubs
= (!htab
->no_stubs
);
1340 contents
+= rel
->r_offset
;
1341 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1344 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1346 bfd_vma old_srel
= srel
;
1348 /* We need to use the address of the stub instead. */
1349 srel
= avr_get_stub_addr (srel
,htab
);
1351 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1352 "reloc at address 0x%x.\n",
1353 (unsigned int) srel
,
1354 (unsigned int) old_srel
,
1355 (unsigned int) reloc_addr
);
1357 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1358 return bfd_reloc_outofrange
;
1362 return bfd_reloc_outofrange
;
1364 bfd_put_16 (input_bfd
, (bfd_vma
) srel
&0x00ffff, contents
);
1370 /* Nothing to do here, as contents already contains the diff value. */
1374 case R_AVR_LDS_STS_16
:
1375 contents
+= rel
->r_offset
;
1376 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1377 if ((srel
& 0xFFFF) < 0x40 || (srel
& 0xFFFF) > 0xbf)
1378 return bfd_reloc_outofrange
;
1380 x
= bfd_get_16 (input_bfd
, contents
);
1381 x
|= (srel
& 0x0f) | ((srel
& 0x30) << 5) | ((srel
& 0x40) << 2);
1382 bfd_put_16 (input_bfd
, x
, contents
);
1386 contents
+= rel
->r_offset
;
1387 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1388 if ((srel
& 0xffff) > 0x3f)
1389 return bfd_reloc_outofrange
;
1390 x
= bfd_get_16 (input_bfd
, contents
);
1391 x
= (x
& 0xf9f0) | ((srel
& 0x30) << 5) | (srel
& 0x0f);
1392 bfd_put_16 (input_bfd
, x
, contents
);
1396 contents
+= rel
->r_offset
;
1397 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1398 if ((srel
& 0xffff) > 0x1f)
1399 return bfd_reloc_outofrange
;
1400 x
= bfd_get_16 (input_bfd
, contents
);
1401 x
= (x
& 0xff07) | ((srel
& 0x1f) << 3);
1402 bfd_put_16 (input_bfd
, x
, contents
);
1406 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1407 contents
, rel
->r_offset
,
1408 relocation
, rel
->r_addend
);
1414 /* Relocate an AVR ELF section. */
1417 elf32_avr_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1418 struct bfd_link_info
*info
,
1420 asection
*input_section
,
1422 Elf_Internal_Rela
*relocs
,
1423 Elf_Internal_Sym
*local_syms
,
1424 asection
**local_sections
)
1426 Elf_Internal_Shdr
* symtab_hdr
;
1427 struct elf_link_hash_entry
** sym_hashes
;
1428 Elf_Internal_Rela
* rel
;
1429 Elf_Internal_Rela
* relend
;
1430 struct elf32_avr_link_hash_table
* htab
= avr_link_hash_table (info
);
1435 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
1436 sym_hashes
= elf_sym_hashes (input_bfd
);
1437 relend
= relocs
+ input_section
->reloc_count
;
1439 for (rel
= relocs
; rel
< relend
; rel
++)
1441 reloc_howto_type
* howto
;
1442 unsigned long r_symndx
;
1443 Elf_Internal_Sym
* sym
;
1445 struct elf_link_hash_entry
* h
;
1447 bfd_reloc_status_type r
;
1451 r_type
= ELF32_R_TYPE (rel
->r_info
);
1452 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1453 howto
= elf_avr_howto_table
+ r_type
;
1458 if (r_symndx
< symtab_hdr
->sh_info
)
1460 sym
= local_syms
+ r_symndx
;
1461 sec
= local_sections
[r_symndx
];
1462 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1464 name
= bfd_elf_string_from_elf_section
1465 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1466 name
= name
== NULL
? bfd_section_name (sec
) : name
;
1470 bool unresolved_reloc
, warned
, ignored
;
1472 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1473 r_symndx
, symtab_hdr
, sym_hashes
,
1475 unresolved_reloc
, warned
, ignored
);
1477 name
= h
->root
.root
.string
;
1480 if (sec
!= NULL
&& discarded_section (sec
))
1481 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1482 rel
, 1, relend
, howto
, 0, contents
);
1484 if (bfd_link_relocatable (info
))
1487 r
= avr_final_link_relocate (howto
, input_bfd
, input_section
,
1488 contents
, rel
, relocation
, htab
);
1490 if (r
!= bfd_reloc_ok
)
1492 const char * msg
= (const char *) NULL
;
1496 case bfd_reloc_overflow
:
1497 (*info
->callbacks
->reloc_overflow
)
1498 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
1499 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
1502 case bfd_reloc_undefined
:
1503 (*info
->callbacks
->undefined_symbol
)
1504 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, true);
1507 case bfd_reloc_outofrange
:
1508 msg
= _("internal error: out of range error");
1511 case bfd_reloc_notsupported
:
1512 msg
= _("internal error: unsupported relocation error");
1515 case bfd_reloc_dangerous
:
1516 msg
= _("internal error: dangerous relocation");
1520 msg
= _("internal error: unknown error");
1525 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
1526 input_section
, rel
->r_offset
);
1533 /* The final processing done just before writing out a AVR ELF object
1534 file. This gets the AVR architecture right based on the machine
1538 bfd_elf_avr_final_write_processing (bfd
*abfd
)
1542 switch (bfd_get_mach (abfd
))
1546 val
= E_AVR_MACH_AVR2
;
1550 val
= E_AVR_MACH_AVR1
;
1553 case bfd_mach_avr25
:
1554 val
= E_AVR_MACH_AVR25
;
1558 val
= E_AVR_MACH_AVR3
;
1561 case bfd_mach_avr31
:
1562 val
= E_AVR_MACH_AVR31
;
1565 case bfd_mach_avr35
:
1566 val
= E_AVR_MACH_AVR35
;
1570 val
= E_AVR_MACH_AVR4
;
1574 val
= E_AVR_MACH_AVR5
;
1577 case bfd_mach_avr51
:
1578 val
= E_AVR_MACH_AVR51
;
1582 val
= E_AVR_MACH_AVR6
;
1585 case bfd_mach_avrxmega1
:
1586 val
= E_AVR_MACH_XMEGA1
;
1589 case bfd_mach_avrxmega2
:
1590 val
= E_AVR_MACH_XMEGA2
;
1593 case bfd_mach_avrxmega3
:
1594 val
= E_AVR_MACH_XMEGA3
;
1597 case bfd_mach_avrxmega4
:
1598 val
= E_AVR_MACH_XMEGA4
;
1601 case bfd_mach_avrxmega5
:
1602 val
= E_AVR_MACH_XMEGA5
;
1605 case bfd_mach_avrxmega6
:
1606 val
= E_AVR_MACH_XMEGA6
;
1609 case bfd_mach_avrxmega7
:
1610 val
= E_AVR_MACH_XMEGA7
;
1613 case bfd_mach_avrtiny
:
1614 val
= E_AVR_MACH_AVRTINY
;
1618 elf_elfheader (abfd
)->e_machine
= EM_AVR
;
1619 elf_elfheader (abfd
)->e_flags
&= ~ EF_AVR_MACH
;
1620 elf_elfheader (abfd
)->e_flags
|= val
;
1621 return _bfd_elf_final_write_processing (abfd
);
1624 /* Set the right machine number. */
1627 elf32_avr_object_p (bfd
*abfd
)
1629 unsigned int e_set
= bfd_mach_avr2
;
1631 if (elf_elfheader (abfd
)->e_machine
== EM_AVR
1632 || elf_elfheader (abfd
)->e_machine
== EM_AVR_OLD
)
1634 int e_mach
= elf_elfheader (abfd
)->e_flags
& EF_AVR_MACH
;
1639 case E_AVR_MACH_AVR2
:
1640 e_set
= bfd_mach_avr2
;
1643 case E_AVR_MACH_AVR1
:
1644 e_set
= bfd_mach_avr1
;
1647 case E_AVR_MACH_AVR25
:
1648 e_set
= bfd_mach_avr25
;
1651 case E_AVR_MACH_AVR3
:
1652 e_set
= bfd_mach_avr3
;
1655 case E_AVR_MACH_AVR31
:
1656 e_set
= bfd_mach_avr31
;
1659 case E_AVR_MACH_AVR35
:
1660 e_set
= bfd_mach_avr35
;
1663 case E_AVR_MACH_AVR4
:
1664 e_set
= bfd_mach_avr4
;
1667 case E_AVR_MACH_AVR5
:
1668 e_set
= bfd_mach_avr5
;
1671 case E_AVR_MACH_AVR51
:
1672 e_set
= bfd_mach_avr51
;
1675 case E_AVR_MACH_AVR6
:
1676 e_set
= bfd_mach_avr6
;
1679 case E_AVR_MACH_XMEGA1
:
1680 e_set
= bfd_mach_avrxmega1
;
1683 case E_AVR_MACH_XMEGA2
:
1684 e_set
= bfd_mach_avrxmega2
;
1687 case E_AVR_MACH_XMEGA3
:
1688 e_set
= bfd_mach_avrxmega3
;
1691 case E_AVR_MACH_XMEGA4
:
1692 e_set
= bfd_mach_avrxmega4
;
1695 case E_AVR_MACH_XMEGA5
:
1696 e_set
= bfd_mach_avrxmega5
;
1699 case E_AVR_MACH_XMEGA6
:
1700 e_set
= bfd_mach_avrxmega6
;
1703 case E_AVR_MACH_XMEGA7
:
1704 e_set
= bfd_mach_avrxmega7
;
1707 case E_AVR_MACH_AVRTINY
:
1708 e_set
= bfd_mach_avrtiny
;
1712 return bfd_default_set_arch_mach (abfd
, bfd_arch_avr
,
1716 /* Returns whether the relocation type passed is a diff reloc. */
1719 elf32_avr_is_diff_reloc (Elf_Internal_Rela
*irel
)
1721 return (ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF8
1722 ||ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF16
1723 || ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF32
);
1726 /* Reduce the diff value written in the section by count if the shrinked
1727 insn address happens to fall between the two symbols for which this
1728 diff reloc was emitted. */
1731 elf32_avr_adjust_diff_reloc_value (bfd
*abfd
,
1732 struct bfd_section
*isec
,
1733 Elf_Internal_Rela
*irel
,
1735 bfd_vma shrinked_insn_address
,
1738 unsigned char *reloc_contents
= NULL
;
1739 unsigned char *isec_contents
= elf_section_data (isec
)->this_hdr
.contents
;
1740 if (isec_contents
== NULL
)
1742 if (! bfd_malloc_and_get_section (abfd
, isec
, &isec_contents
))
1745 elf_section_data (isec
)->this_hdr
.contents
= isec_contents
;
1748 reloc_contents
= isec_contents
+ irel
->r_offset
;
1750 /* Read value written in object file. */
1751 bfd_signed_vma x
= 0;
1752 switch (ELF32_R_TYPE (irel
->r_info
))
1756 x
= bfd_get_signed_8 (abfd
, reloc_contents
);
1761 x
= bfd_get_signed_16 (abfd
, reloc_contents
);
1766 x
= bfd_get_signed_32 (abfd
, reloc_contents
);
1775 /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
1776 into the object file at the reloc offset. sym2's logical value is
1777 symval (<start_of_section>) + reloc addend. Compute the start and end
1778 addresses and check if the shrinked insn falls between sym1 and sym2. */
1780 bfd_vma sym2_address
= symval
+ irel
->r_addend
;
1781 bfd_vma sym1_address
= sym2_address
- x
;
1783 /* Don't assume sym2 is bigger than sym1 - the difference
1784 could be negative. Compute start and end addresses, and
1785 use those to see if they span shrinked_insn_address. */
1787 bfd_vma start_address
= sym1_address
< sym2_address
1788 ? sym1_address
: sym2_address
;
1789 bfd_vma end_address
= sym1_address
> sym2_address
1790 ? sym1_address
: sym2_address
;
1793 if (shrinked_insn_address
>= start_address
1794 && shrinked_insn_address
< end_address
)
1796 /* Reduce the diff value by count bytes and write it back into section
1798 bfd_signed_vma new_diff
= x
< 0 ? x
+ count
: x
- count
;
1800 if (sym2_address
> shrinked_insn_address
)
1801 irel
->r_addend
-= count
;
1803 switch (ELF32_R_TYPE (irel
->r_info
))
1807 bfd_put_signed_8 (abfd
, new_diff
, reloc_contents
);
1812 bfd_put_signed_16 (abfd
, new_diff
& 0xFFFF, reloc_contents
);
1817 bfd_put_signed_32 (abfd
, new_diff
& 0xFFFFFFFF, reloc_contents
);
1830 elf32_avr_adjust_reloc_if_spans_insn (bfd
*abfd
,
1832 Elf_Internal_Rela
*irel
, bfd_vma symval
,
1833 bfd_vma shrinked_insn_address
,
1834 bfd_vma shrink_boundary
,
1838 if (elf32_avr_is_diff_reloc (irel
))
1840 elf32_avr_adjust_diff_reloc_value (abfd
, isec
, irel
,
1842 shrinked_insn_address
,
1847 bfd_vma reloc_value
= symval
+ irel
->r_addend
;
1848 bool addend_within_shrink_boundary
= reloc_value
<= shrink_boundary
;
1850 bool reloc_spans_insn
=
1851 (symval
<= shrinked_insn_address
1852 && reloc_value
> shrinked_insn_address
1853 && addend_within_shrink_boundary
);
1855 if (! reloc_spans_insn
)
1858 irel
->r_addend
-= count
;
1861 printf ("Relocation's addend needed to be fixed \n");
1866 avr_should_move_sym (symvalue symval
,
1871 bool sym_within_boundary
= did_pad
? symval
< end
: symval
<= end
;
1872 return (symval
> start
&& sym_within_boundary
);
1876 avr_should_reduce_sym_size (symvalue symval
,
1882 bool sym_end_within_boundary
= did_pad
? symend
< end
: symend
<= end
;
1883 return (symval
<= start
&& symend
> start
&& sym_end_within_boundary
);
1887 avr_should_increase_sym_size (symvalue symval
,
1893 return (avr_should_move_sym (symval
, start
, end
, did_pad
)
1894 && symend
>= end
&& did_pad
);
1897 /* Delete some bytes from a section while changing the size of an instruction.
1898 The parameter "addr" denotes the section-relative offset pointing just
1899 behind the shrinked instruction. "addr+count" point at the first
1900 byte just behind the original unshrinked instruction. If delete_shrinks_insn
1901 is FALSE, we are deleting redundant padding bytes from relax_info prop
1902 record handling. In that case, addr is section-relative offset of start
1903 of padding, and count is the number of padding bytes to delete. */
1906 elf32_avr_relax_delete_bytes (bfd
*abfd
,
1910 bool delete_shrinks_insn
)
1912 Elf_Internal_Shdr
*symtab_hdr
;
1913 unsigned int sec_shndx
;
1915 Elf_Internal_Rela
*irel
, *irelend
;
1916 Elf_Internal_Sym
*isym
;
1917 Elf_Internal_Sym
*isymbuf
= NULL
;
1919 struct elf_link_hash_entry
**sym_hashes
;
1920 struct elf_link_hash_entry
**end_hashes
;
1921 unsigned int symcount
;
1922 struct avr_relax_info
*relax_info
;
1923 struct avr_property_record
*prop_record
= NULL
;
1924 bool did_shrink
= false;
1925 bool did_pad
= false;
1927 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1928 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1929 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1930 relax_info
= get_avr_relax_info (sec
);
1934 if (relax_info
->records
.count
> 0)
1936 /* There should be no property record within the range of deleted
1937 bytes, however, there might be a property record for ADDR, this is
1938 how we handle alignment directives.
1939 Find the next (if any) property record after the deleted bytes. */
1942 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
1944 bfd_vma offset
= relax_info
->records
.items
[i
].offset
;
1946 BFD_ASSERT (offset
<= addr
|| offset
>= (addr
+ count
));
1947 if (offset
>= (addr
+ count
))
1949 prop_record
= &relax_info
->records
.items
[i
];
1956 irel
= elf_section_data (sec
)->relocs
;
1957 irelend
= irel
+ sec
->reloc_count
;
1959 /* Actually delete the bytes. */
1960 if (toaddr
- addr
- count
> 0)
1962 memmove (contents
+ addr
, contents
+ addr
+ count
,
1963 (size_t) (toaddr
- addr
- count
));
1966 if (prop_record
== NULL
)
1973 /* Use the property record to fill in the bytes we've opened up. */
1975 switch (prop_record
->type
)
1977 case RECORD_ORG_AND_FILL
:
1978 fill
= prop_record
->data
.org
.fill
;
1982 case RECORD_ALIGN_AND_FILL
:
1983 fill
= prop_record
->data
.align
.fill
;
1986 prop_record
->data
.align
.preceding_deleted
+= count
;
1989 /* If toaddr == (addr + count), then we didn't delete anything, yet
1990 we fill count bytes backwards from toaddr. This is still ok - we
1991 end up overwriting the bytes we would have deleted. We just need
1992 to remember we didn't delete anything i.e. don't set did_shrink,
1993 so that we don't corrupt reloc offsets or symbol values.*/
1994 memset (contents
+ toaddr
- count
, fill
, count
);
2001 /* Adjust all the reloc addresses. */
2002 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
2004 bfd_vma old_reloc_address
;
2006 old_reloc_address
= (sec
->output_section
->vma
2007 + sec
->output_offset
+ irel
->r_offset
);
2009 /* Get the new reloc address. */
2010 if ((irel
->r_offset
> addr
2011 && irel
->r_offset
< toaddr
))
2014 printf ("Relocation at address 0x%x needs to be moved.\n"
2015 "Old section offset: 0x%x, New section offset: 0x%x \n",
2016 (unsigned int) old_reloc_address
,
2017 (unsigned int) irel
->r_offset
,
2018 (unsigned int) ((irel
->r_offset
) - count
));
2020 irel
->r_offset
-= count
;
2025 /* The reloc's own addresses are now ok. However, we need to readjust
2026 the reloc's addend, i.e. the reloc's value if two conditions are met:
2027 1.) the reloc is relative to a symbol in this section that
2028 is located in front of the shrinked instruction
2029 2.) symbol plus addend end up behind the shrinked instruction.
2031 The most common case where this happens are relocs relative to
2032 the section-start symbol.
2034 This step needs to be done for all of the sections of the bfd. */
2037 struct bfd_section
*isec
;
2039 for (isec
= abfd
->sections
; isec
; isec
= isec
->next
)
2042 bfd_vma shrinked_insn_address
;
2044 if (isec
->reloc_count
== 0)
2047 shrinked_insn_address
= (sec
->output_section
->vma
2048 + sec
->output_offset
+ addr
);
2049 if (delete_shrinks_insn
)
2050 shrinked_insn_address
-= count
;
2052 irel
= elf_section_data (isec
)->relocs
;
2053 /* PR 12161: Read in the relocs for this section if necessary. */
2055 irel
= _bfd_elf_link_read_relocs (abfd
, isec
, NULL
, NULL
, true);
2057 for (irelend
= irel
+ isec
->reloc_count
;
2061 /* Read this BFD's local symbols if we haven't done
2063 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2065 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2066 if (isymbuf
== NULL
)
2067 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2068 symtab_hdr
->sh_info
, 0,
2070 if (isymbuf
== NULL
)
2074 /* Get the value of the symbol referred to by the reloc. */
2075 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2077 /* A local symbol. */
2080 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
2081 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2082 symval
= isym
->st_value
;
2083 /* If the reloc is absolute, it will not have
2084 a symbol or section associated with it. */
2087 /* If there is an alignment boundary, we only need to
2088 adjust addends that end up below the boundary. */
2089 bfd_vma shrink_boundary
= (toaddr
2090 + sec
->output_section
->vma
2091 + sec
->output_offset
);
2093 symval
+= sym_sec
->output_section
->vma
2094 + sym_sec
->output_offset
;
2097 printf ("Checking if the relocation's "
2098 "addend needs corrections.\n"
2099 "Address of anchor symbol: 0x%x \n"
2100 "Address of relocation target: 0x%x \n"
2101 "Address of relaxed insn: 0x%x \n",
2102 (unsigned int) symval
,
2103 (unsigned int) (symval
+ irel
->r_addend
),
2104 (unsigned int) shrinked_insn_address
);
2106 elf32_avr_adjust_reloc_if_spans_insn (abfd
, isec
, irel
,
2108 shrinked_insn_address
,
2112 /* else...Reference symbol is absolute. No adjustment needed. */
2114 /* else...Reference symbol is extern. No need for adjusting
2120 /* Adjust the local symbols defined in this section. */
2121 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2122 /* Fix PR 9841, there may be no local symbols. */
2125 Elf_Internal_Sym
*isymend
;
2127 isymend
= isym
+ symtab_hdr
->sh_info
;
2128 for (; isym
< isymend
; isym
++)
2130 if (isym
->st_shndx
== sec_shndx
)
2132 symvalue symval
= isym
->st_value
;
2133 symvalue symend
= symval
+ isym
->st_size
;
2134 if (avr_should_reduce_sym_size (symval
, symend
,
2135 addr
, toaddr
, did_pad
))
2137 /* If this assert fires then we have a symbol that ends
2138 part way through an instruction. Does that make
2140 BFD_ASSERT (isym
->st_value
+ isym
->st_size
>= addr
+ count
);
2141 isym
->st_size
-= count
;
2143 else if (avr_should_increase_sym_size (symval
, symend
,
2144 addr
, toaddr
, did_pad
))
2145 isym
->st_size
+= count
;
2147 if (avr_should_move_sym (symval
, addr
, toaddr
, did_pad
))
2148 isym
->st_value
-= count
;
2153 /* Now adjust the global symbols defined in this section. */
2154 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2155 - symtab_hdr
->sh_info
);
2156 sym_hashes
= elf_sym_hashes (abfd
);
2157 end_hashes
= sym_hashes
+ symcount
;
2158 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2160 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
2161 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2162 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
2163 && sym_hash
->root
.u
.def
.section
== sec
)
2165 symvalue symval
= sym_hash
->root
.u
.def
.value
;
2166 symvalue symend
= symval
+ sym_hash
->size
;
2168 if (avr_should_reduce_sym_size (symval
, symend
,
2169 addr
, toaddr
, did_pad
))
2171 /* If this assert fires then we have a symbol that ends
2172 part way through an instruction. Does that make
2174 BFD_ASSERT (symend
>= addr
+ count
);
2175 sym_hash
->size
-= count
;
2177 else if (avr_should_increase_sym_size (symval
, symend
,
2178 addr
, toaddr
, did_pad
))
2179 sym_hash
->size
+= count
;
2181 if (avr_should_move_sym (symval
, addr
, toaddr
, did_pad
))
2182 sym_hash
->root
.u
.def
.value
-= count
;
2189 static Elf_Internal_Sym
*
2190 retrieve_local_syms (bfd
*input_bfd
)
2192 Elf_Internal_Shdr
*symtab_hdr
;
2193 Elf_Internal_Sym
*isymbuf
;
2196 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2197 locsymcount
= symtab_hdr
->sh_info
;
2199 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2200 if (isymbuf
== NULL
&& locsymcount
!= 0)
2201 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
2204 /* Save the symbols for this input file so they won't be read again. */
2205 if (isymbuf
&& isymbuf
!= (Elf_Internal_Sym
*) symtab_hdr
->contents
)
2206 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2211 /* Get the input section for a given symbol index.
2213 . a section symbol, return the section;
2214 . a common symbol, return the common section;
2215 . an undefined symbol, return the undefined section;
2216 . an indirect symbol, follow the links;
2217 . an absolute value, return the absolute section. */
2220 get_elf_r_symndx_section (bfd
*abfd
, unsigned long r_symndx
)
2222 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2223 asection
*target_sec
= NULL
;
2224 if (r_symndx
< symtab_hdr
->sh_info
)
2226 Elf_Internal_Sym
*isymbuf
;
2227 unsigned int section_index
;
2229 isymbuf
= retrieve_local_syms (abfd
);
2230 section_index
= isymbuf
[r_symndx
].st_shndx
;
2232 if (section_index
== SHN_UNDEF
)
2233 target_sec
= bfd_und_section_ptr
;
2234 else if (section_index
== SHN_ABS
)
2235 target_sec
= bfd_abs_section_ptr
;
2236 else if (section_index
== SHN_COMMON
)
2237 target_sec
= bfd_com_section_ptr
;
2239 target_sec
= bfd_section_from_elf_index (abfd
, section_index
);
2243 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
2244 struct elf_link_hash_entry
*h
= elf_sym_hashes (abfd
)[indx
];
2246 while (h
->root
.type
== bfd_link_hash_indirect
2247 || h
->root
.type
== bfd_link_hash_warning
)
2248 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2250 switch (h
->root
.type
)
2252 case bfd_link_hash_defined
:
2253 case bfd_link_hash_defweak
:
2254 target_sec
= h
->root
.u
.def
.section
;
2256 case bfd_link_hash_common
:
2257 target_sec
= bfd_com_section_ptr
;
2259 case bfd_link_hash_undefined
:
2260 case bfd_link_hash_undefweak
:
2261 target_sec
= bfd_und_section_ptr
;
2263 default: /* New indirect warning. */
2264 target_sec
= bfd_und_section_ptr
;
2271 /* Get the section-relative offset for a symbol number. */
2274 get_elf_r_symndx_offset (bfd
*abfd
, unsigned long r_symndx
)
2276 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2279 if (r_symndx
< symtab_hdr
->sh_info
)
2281 Elf_Internal_Sym
*isymbuf
;
2282 isymbuf
= retrieve_local_syms (abfd
);
2283 offset
= isymbuf
[r_symndx
].st_value
;
2287 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
2288 struct elf_link_hash_entry
*h
=
2289 elf_sym_hashes (abfd
)[indx
];
2291 while (h
->root
.type
== bfd_link_hash_indirect
2292 || h
->root
.type
== bfd_link_hash_warning
)
2293 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2294 if (h
->root
.type
== bfd_link_hash_defined
2295 || h
->root
.type
== bfd_link_hash_defweak
)
2296 offset
= h
->root
.u
.def
.value
;
2301 /* Iterate over the property records in R_LIST, and copy each record into
2302 the list of records within the relaxation information for the section to
2303 which the record applies. */
2306 avr_elf32_assign_records_to_sections (struct avr_property_record_list
*r_list
)
2310 for (i
= 0; i
< r_list
->record_count
; ++i
)
2312 struct avr_relax_info
*relax_info
;
2314 relax_info
= get_avr_relax_info (r_list
->records
[i
].section
);
2315 BFD_ASSERT (relax_info
!= NULL
);
2317 if (relax_info
->records
.count
2318 == relax_info
->records
.allocated
)
2320 /* Allocate more space. */
2323 relax_info
->records
.allocated
+= 10;
2324 size
= (sizeof (struct avr_property_record
)
2325 * relax_info
->records
.allocated
);
2326 relax_info
->records
.items
2327 = bfd_realloc (relax_info
->records
.items
, size
);
2330 memcpy (&relax_info
->records
.items
[relax_info
->records
.count
],
2331 &r_list
->records
[i
],
2332 sizeof (struct avr_property_record
));
2333 relax_info
->records
.count
++;
2337 /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the
2338 ordering callback from QSORT. */
2341 avr_property_record_compare (const void *ap
, const void *bp
)
2343 const struct avr_property_record
*a
2344 = (struct avr_property_record
*) ap
;
2345 const struct avr_property_record
*b
2346 = (struct avr_property_record
*) bp
;
2348 if (a
->offset
!= b
->offset
)
2349 return (a
->offset
- b
->offset
);
2351 if (a
->section
!= b
->section
)
2352 return bfd_section_vma (a
->section
) - bfd_section_vma (b
->section
);
2354 return (a
->type
- b
->type
);
2357 /* Load all of the avr property sections from all of the bfd objects
2358 referenced from LINK_INFO. All of the records within each property
2359 section are assigned to the STRUCT AVR_RELAX_INFO within the section
2360 specific data of the appropriate section. */
2363 avr_load_all_property_sections (struct bfd_link_info
*link_info
)
2368 /* Initialize the per-section relaxation info. */
2369 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2370 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2372 init_avr_relax_info (sec
);
2375 /* Load the descriptor tables from .avr.prop sections. */
2376 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2378 struct avr_property_record_list
*r_list
;
2380 r_list
= avr_elf32_load_property_records (abfd
);
2382 avr_elf32_assign_records_to_sections (r_list
);
2387 /* Now, for every section, ensure that the descriptor list in the
2388 relaxation data is sorted by ascending offset within the section. */
2389 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2390 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2392 struct avr_relax_info
*relax_info
= get_avr_relax_info (sec
);
2393 if (relax_info
&& relax_info
->records
.count
> 0)
2397 qsort (relax_info
->records
.items
,
2398 relax_info
->records
.count
,
2399 sizeof (struct avr_property_record
),
2400 avr_property_record_compare
);
2402 /* For debug purposes, list all the descriptors. */
2403 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
2405 switch (relax_info
->records
.items
[i
].type
)
2409 case RECORD_ORG_AND_FILL
:
2413 case RECORD_ALIGN_AND_FILL
:
2421 /* This function handles relaxing for the avr.
2422 Many important relaxing opportunities within functions are already
2423 realized by the compiler itself.
2424 Here we try to replace call (4 bytes) -> rcall (2 bytes)
2425 and jump -> rjmp (safes also 2 bytes).
2426 As well we now optimize seqences of
2427 - call/rcall function
2432 . In case that within a sequence
2435 the ret could no longer be reached it is optimized away. In order
2436 to check if the ret is no longer needed, it is checked that the ret's address
2437 is not the target of a branch or jump within the same section, it is checked
2438 that there is no skip instruction before the jmp/rjmp and that there
2439 is no local or global label place at the address of the ret.
2441 We refrain from relaxing within sections ".vectors" and
2442 ".jumptables" in order to maintain the position of the instructions.
2443 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
2444 if possible. (In future one could possibly use the space of the nop
2445 for the first instruction of the irq service function.
2447 The .jumptables sections is meant to be used for a future tablejump variant
2448 for the devices with 3-byte program counter where the table itself
2449 contains 4-byte jump instructions whose relative offset must not
2453 elf32_avr_relax_section (bfd
*abfd
,
2455 struct bfd_link_info
*link_info
,
2458 Elf_Internal_Shdr
*symtab_hdr
;
2459 Elf_Internal_Rela
*internal_relocs
;
2460 Elf_Internal_Rela
*irel
, *irelend
;
2461 bfd_byte
*contents
= NULL
;
2462 Elf_Internal_Sym
*isymbuf
= NULL
;
2463 struct elf32_avr_link_hash_table
*htab
;
2464 static bool relaxation_initialised
= false;
2466 if (!relaxation_initialised
)
2468 relaxation_initialised
= true;
2470 /* Load entries from the .avr.prop sections. */
2471 avr_load_all_property_sections (link_info
);
2474 /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
2475 relaxing. Such shrinking can cause issues for the sections such
2476 as .vectors and .jumptables. Instead the unused bytes should be
2477 filled with nop instructions. */
2478 bool shrinkable
= true;
2480 if (!strcmp (sec
->name
,".vectors")
2481 || !strcmp (sec
->name
,".jumptables"))
2484 if (bfd_link_relocatable (link_info
))
2485 (*link_info
->callbacks
->einfo
)
2486 (_("%P%F: --relax and -r may not be used together\n"));
2488 htab
= avr_link_hash_table (link_info
);
2492 /* Assume nothing changes. */
2495 if ((!htab
->no_stubs
) && (sec
== htab
->stub_sec
))
2497 /* We are just relaxing the stub section.
2498 Let's calculate the size needed again. */
2499 bfd_size_type last_estimated_stub_section_size
= htab
->stub_sec
->size
;
2502 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
2503 (int) last_estimated_stub_section_size
);
2505 elf32_avr_size_stubs (htab
->stub_sec
->output_section
->owner
,
2508 /* Check if the number of trampolines changed. */
2509 if (last_estimated_stub_section_size
!= htab
->stub_sec
->size
)
2513 printf ("Size of stub section after this pass: %i\n",
2514 (int) htab
->stub_sec
->size
);
2519 /* We don't have to do anything for a relocatable link, if
2520 this section does not have relocs, or if this is not a
2522 if (bfd_link_relocatable (link_info
)
2523 || (sec
->flags
& SEC_RELOC
) == 0
2524 || sec
->reloc_count
== 0
2525 || (sec
->flags
& SEC_CODE
) == 0)
2528 /* Check if the object file to relax uses internal symbols so that we
2529 could fix up the relocations. */
2530 if (!(elf_elfheader (abfd
)->e_flags
& EF_AVR_LINKRELAX_PREPARED
))
2533 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2535 /* Get a copy of the native relocations. */
2536 internal_relocs
= (_bfd_elf_link_read_relocs
2537 (abfd
, sec
, NULL
, NULL
, link_info
->keep_memory
));
2538 if (internal_relocs
== NULL
)
2541 /* Walk through the relocs looking for relaxing opportunities. */
2542 irelend
= internal_relocs
+ sec
->reloc_count
;
2543 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2547 if ( ELF32_R_TYPE (irel
->r_info
) != R_AVR_13_PCREL
2548 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_7_PCREL
2549 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_CALL
)
2552 /* Get the section contents if we haven't done so already. */
2553 if (contents
== NULL
)
2555 /* Get cached copy if it exists. */
2556 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2557 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2560 /* Go get them off disk. */
2561 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2566 /* Read this BFD's local symbols if we haven't done so already. */
2567 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2569 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2570 if (isymbuf
== NULL
)
2571 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2572 symtab_hdr
->sh_info
, 0,
2574 if (isymbuf
== NULL
)
2579 /* Get the value of the symbol referred to by the reloc. */
2580 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2582 /* A local symbol. */
2583 Elf_Internal_Sym
*isym
;
2586 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
2587 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2588 symval
= isym
->st_value
;
2589 /* If the reloc is absolute, it will not have
2590 a symbol or section associated with it. */
2592 symval
+= sym_sec
->output_section
->vma
2593 + sym_sec
->output_offset
;
2598 struct elf_link_hash_entry
*h
;
2600 /* An external symbol. */
2601 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
2602 h
= elf_sym_hashes (abfd
)[indx
];
2603 BFD_ASSERT (h
!= NULL
);
2604 if (h
->root
.type
!= bfd_link_hash_defined
2605 && h
->root
.type
!= bfd_link_hash_defweak
)
2606 /* This appears to be a reference to an undefined
2607 symbol. Just ignore it--it will be caught by the
2608 regular reloc processing. */
2611 symval
= (h
->root
.u
.def
.value
2612 + h
->root
.u
.def
.section
->output_section
->vma
2613 + h
->root
.u
.def
.section
->output_offset
);
2616 /* For simplicity of coding, we are going to modify the section
2617 contents, the section relocs, and the BFD symbol table. We
2618 must tell the rest of the code not to free up this
2619 information. It would be possible to instead create a table
2620 of changes which have to be made, as is done in coff-mips.c;
2621 that would be more work, but would require less memory when
2622 the linker is run. */
2623 switch (ELF32_R_TYPE (irel
->r_info
))
2625 /* Try to turn a 22-bit absolute call/jump into an 13-bit
2626 pc-relative rcall/rjmp. */
2629 bfd_vma value
= symval
+ irel
->r_addend
;
2631 int distance_short_enough
= 0;
2633 /* Get the address of this instruction. */
2634 dot
= (sec
->output_section
->vma
2635 + sec
->output_offset
+ irel
->r_offset
);
2637 /* Compute the distance from this insn to the branch target. */
2640 /* The ISA manual states that addressable range is PC - 2k + 1 to
2641 PC + 2k. In bytes, that would be -4094 <= PC <= 4096. The range
2642 is shifted one word to the right, because pc-relative instructions
2643 implicitly add one word i.e. rjmp 0 jumps to next insn, not the
2645 Therefore, for the !shrinkable case, the range is as above.
2646 If shrinkable, then the current code only deletes bytes 3 and
2647 4 of the absolute call/jmp, so the forward jump range increases
2648 by 2 bytes, but the backward (negative) jump range remains
2652 /* Check if the gap falls in the range that can be accommodated
2653 in 13bits signed (It is 12bits when encoded, as we deal with
2654 word addressing). */
2655 if (!shrinkable
&& ((int) gap
>= -4094 && (int) gap
<= 4096))
2656 distance_short_enough
= 1;
2657 /* If shrinkable, then we can check for a range of distance which
2658 is two bytes farther on the positive direction because the call
2659 or jump target will be closer by two bytes after the
2661 else if (shrinkable
&& ((int) gap
>= -4094 && (int) gap
<= 4098))
2662 distance_short_enough
= 1;
2664 /* Here we handle the wrap-around case. E.g. for a 16k device
2665 we could use a rjmp to jump from address 0x100 to 0x3d00!
2666 In order to make this work properly, we need to fill the
2667 vaiable avr_pc_wrap_around with the appropriate value.
2668 I.e. 0x4000 for a 16k device. */
2670 /* Shrinking the code size makes the gaps larger in the
2671 case of wrap-arounds. So we use a heuristical safety
2672 margin to avoid that during relax the distance gets
2673 again too large for the short jumps. Let's assume
2674 a typical code-size reduction due to relax for a
2675 16k device of 600 bytes. So let's use twice the
2676 typical value as safety margin. */
2680 int assumed_shrink
= 600;
2681 if (avr_pc_wrap_around
> 0x4000)
2682 assumed_shrink
= 900;
2684 safety_margin
= 2 * assumed_shrink
;
2686 rgap
= avr_relative_distance_considering_wrap_around (gap
);
2688 if (rgap
>= (-4092 + safety_margin
)
2689 && rgap
<= (4094 - safety_margin
))
2690 distance_short_enough
= 1;
2693 if (distance_short_enough
)
2695 unsigned char code_msb
;
2696 unsigned char code_lsb
;
2699 printf ("shrinking jump/call instruction at address 0x%x"
2700 " in section %s\n\n",
2701 (int) dot
, sec
->name
);
2703 /* Note that we've changed the relocs, section contents,
2705 elf_section_data (sec
)->relocs
= internal_relocs
;
2706 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2707 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2709 /* Get the instruction code for relaxing. */
2710 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
);
2711 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
2713 /* Mask out the relocation bits. */
2716 if (code_msb
== 0x94 && code_lsb
== 0x0E)
2718 /* we are changing call -> rcall . */
2719 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
2720 bfd_put_8 (abfd
, 0xD0, contents
+ irel
->r_offset
+ 1);
2722 else if (code_msb
== 0x94 && code_lsb
== 0x0C)
2724 /* we are changeing jump -> rjmp. */
2725 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
2726 bfd_put_8 (abfd
, 0xC0, contents
+ irel
->r_offset
+ 1);
2731 /* Fix the relocation's type. */
2732 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2735 /* We should not modify the ordering if 'shrinkable' is
2739 /* Let's insert a nop. */
2740 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 2);
2741 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 3);
2745 /* Delete two bytes of data. */
2746 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
2747 irel
->r_offset
+ 2, 2,
2751 /* That will change things, so, we should relax again.
2752 Note that this is not required, and it may be slow. */
2761 unsigned char code_msb
;
2762 unsigned char code_lsb
;
2765 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
2766 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 0);
2768 /* Get the address of this instruction. */
2769 dot
= (sec
->output_section
->vma
2770 + sec
->output_offset
+ irel
->r_offset
);
2772 /* Here we look for rcall/ret or call/ret sequences that could be
2773 safely replaced by rjmp/ret or jmp/ret. */
2774 if (((code_msb
& 0xf0) == 0xd0)
2775 && avr_replace_call_ret_sequences
)
2777 /* This insn is a rcall. */
2778 unsigned char next_insn_msb
= 0;
2779 unsigned char next_insn_lsb
= 0;
2781 if (irel
->r_offset
+ 3 < sec
->size
)
2784 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 3);
2786 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 2);
2789 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2791 /* The next insn is a ret. We now convert the rcall insn
2792 into a rjmp instruction. */
2794 bfd_put_8 (abfd
, code_msb
, contents
+ irel
->r_offset
+ 1);
2796 printf ("converted rcall/ret sequence at address 0x%x"
2797 " into rjmp/ret sequence. Section is %s\n\n",
2798 (int) dot
, sec
->name
);
2803 else if ((0x94 == (code_msb
& 0xfe))
2804 && (0x0e == (code_lsb
& 0x0e))
2805 && avr_replace_call_ret_sequences
)
2807 /* This insn is a call. */
2808 unsigned char next_insn_msb
= 0;
2809 unsigned char next_insn_lsb
= 0;
2811 if (irel
->r_offset
+ 5 < sec
->size
)
2814 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 5);
2816 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 4);
2819 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2821 /* The next insn is a ret. We now convert the call insn
2822 into a jmp instruction. */
2825 bfd_put_8 (abfd
, code_lsb
, contents
+ irel
->r_offset
);
2827 printf ("converted call/ret sequence at address 0x%x"
2828 " into jmp/ret sequence. Section is %s\n\n",
2829 (int) dot
, sec
->name
);
2834 else if ((0xc0 == (code_msb
& 0xf0))
2835 || ((0x94 == (code_msb
& 0xfe))
2836 && (0x0c == (code_lsb
& 0x0e))))
2838 /* This insn is a rjmp or a jmp. */
2839 unsigned char next_insn_msb
= 0;
2840 unsigned char next_insn_lsb
= 0;
2843 if (0xc0 == (code_msb
& 0xf0))
2844 insn_size
= 2; /* rjmp insn */
2846 insn_size
= 4; /* jmp insn */
2848 if (irel
->r_offset
+ insn_size
+ 1 < sec
->size
)
2851 bfd_get_8 (abfd
, contents
+ irel
->r_offset
2854 bfd_get_8 (abfd
, contents
+ irel
->r_offset
2858 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2860 /* The next insn is a ret. We possibly could delete
2861 this ret. First we need to check for preceding
2862 sbis/sbic/sbrs or cpse "skip" instructions. */
2864 int there_is_preceding_non_skip_insn
= 1;
2865 bfd_vma address_of_ret
;
2867 address_of_ret
= dot
+ insn_size
;
2869 if (debug_relax
&& (insn_size
== 2))
2870 printf ("found rjmp / ret sequence at address 0x%x\n",
2872 if (debug_relax
&& (insn_size
== 4))
2873 printf ("found jmp / ret sequence at address 0x%x\n",
2876 /* We have to make sure that there is a preceding insn. */
2877 if (irel
->r_offset
>= 2)
2879 unsigned char preceding_msb
;
2880 unsigned char preceding_lsb
;
2883 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2885 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2888 if (0x99 == preceding_msb
)
2889 there_is_preceding_non_skip_insn
= 0;
2892 if (0x9b == preceding_msb
)
2893 there_is_preceding_non_skip_insn
= 0;
2896 if ((0xfc == (preceding_msb
& 0xfe)
2897 && (0x00 == (preceding_lsb
& 0x08))))
2898 there_is_preceding_non_skip_insn
= 0;
2901 if ((0xfe == (preceding_msb
& 0xfe)
2902 && (0x00 == (preceding_lsb
& 0x08))))
2903 there_is_preceding_non_skip_insn
= 0;
2906 if (0x10 == (preceding_msb
& 0xfc))
2907 there_is_preceding_non_skip_insn
= 0;
2909 if (there_is_preceding_non_skip_insn
== 0)
2911 printf ("preceding skip insn prevents deletion of"
2912 " ret insn at Addy 0x%x in section %s\n",
2913 (int) dot
+ 2, sec
->name
);
2917 /* There is no previous instruction. */
2918 there_is_preceding_non_skip_insn
= 0;
2921 if (there_is_preceding_non_skip_insn
)
2923 /* We now only have to make sure that there is no
2924 local label defined at the address of the ret
2925 instruction and that there is no local relocation
2926 in this section pointing to the ret. */
2928 int deleting_ret_is_safe
= 1;
2929 unsigned int section_offset_of_ret_insn
=
2930 irel
->r_offset
+ insn_size
;
2931 Elf_Internal_Sym
*isym
, *isymend
;
2932 unsigned int sec_shndx
;
2933 struct bfd_section
*isec
;
2936 _bfd_elf_section_from_bfd_section (abfd
, sec
);
2938 /* Check for local symbols. */
2939 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2940 isymend
= isym
+ symtab_hdr
->sh_info
;
2941 /* PR 6019: There may not be any local symbols. */
2942 for (; isym
!= NULL
&& isym
< isymend
; isym
++)
2944 if (isym
->st_value
== section_offset_of_ret_insn
2945 && isym
->st_shndx
== sec_shndx
)
2947 deleting_ret_is_safe
= 0;
2949 printf ("local label prevents deletion of ret "
2950 "insn at address 0x%x\n",
2951 (int) dot
+ insn_size
);
2955 /* Now check for global symbols. */
2958 struct elf_link_hash_entry
**sym_hashes
;
2959 struct elf_link_hash_entry
**end_hashes
;
2961 symcount
= (symtab_hdr
->sh_size
2962 / sizeof (Elf32_External_Sym
)
2963 - symtab_hdr
->sh_info
);
2964 sym_hashes
= elf_sym_hashes (abfd
);
2965 end_hashes
= sym_hashes
+ symcount
;
2966 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2968 struct elf_link_hash_entry
*sym_hash
=
2970 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2971 || sym_hash
->root
.type
==
2972 bfd_link_hash_defweak
)
2973 && sym_hash
->root
.u
.def
.section
== sec
2974 && sym_hash
->root
.u
.def
.value
== section_offset_of_ret_insn
)
2976 deleting_ret_is_safe
= 0;
2978 printf ("global label prevents deletion of "
2979 "ret insn at address 0x%x\n",
2980 (int) dot
+ insn_size
);
2985 /* Now we check for relocations pointing to ret. */
2986 for (isec
= abfd
->sections
; isec
&& deleting_ret_is_safe
; isec
= isec
->next
)
2988 Elf_Internal_Rela
*rel
;
2989 Elf_Internal_Rela
*relend
;
2991 rel
= elf_section_data (isec
)->relocs
;
2993 rel
= _bfd_elf_link_read_relocs (abfd
, isec
, NULL
, NULL
, true);
2995 relend
= rel
+ isec
->reloc_count
;
2997 for (; rel
&& rel
< relend
; rel
++)
2999 bfd_vma reloc_target
= 0;
3001 /* Read this BFD's local symbols if we haven't
3003 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
3005 isymbuf
= (Elf_Internal_Sym
*)
3006 symtab_hdr
->contents
;
3007 if (isymbuf
== NULL
)
3008 isymbuf
= bfd_elf_get_elf_syms
3011 symtab_hdr
->sh_info
, 0,
3013 if (isymbuf
== NULL
)
3017 /* Get the value of the symbol referred to
3019 if (ELF32_R_SYM (rel
->r_info
)
3020 < symtab_hdr
->sh_info
)
3022 /* A local symbol. */
3026 + ELF32_R_SYM (rel
->r_info
);
3027 sym_sec
= bfd_section_from_elf_index
3028 (abfd
, isym
->st_shndx
);
3029 symval
= isym
->st_value
;
3031 /* If the reloc is absolute, it will not
3032 have a symbol or section associated
3038 sym_sec
->output_section
->vma
3039 + sym_sec
->output_offset
;
3040 reloc_target
= symval
+ rel
->r_addend
;
3044 reloc_target
= symval
+ rel
->r_addend
;
3045 /* Reference symbol is absolute. */
3048 /* else ... reference symbol is extern. */
3050 if (address_of_ret
== reloc_target
)
3052 deleting_ret_is_safe
= 0;
3055 "rjmp/jmp ret sequence at address"
3056 " 0x%x could not be deleted. ret"
3057 " is target of a relocation.\n",
3058 (int) address_of_ret
);
3064 if (deleting_ret_is_safe
)
3067 printf ("unreachable ret instruction "
3068 "at address 0x%x deleted.\n",
3069 (int) dot
+ insn_size
);
3071 /* Delete two bytes of data. */
3072 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
3073 irel
->r_offset
+ insn_size
, 2,
3077 /* That will change things, so, we should relax
3078 again. Note that this is not required, and it
3093 /* Look through all the property records in this section to see if
3094 there's any alignment records that can be moved. */
3095 struct avr_relax_info
*relax_info
;
3097 relax_info
= get_avr_relax_info (sec
);
3098 if (relax_info
->records
.count
> 0)
3102 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
3104 switch (relax_info
->records
.items
[i
].type
)
3107 case RECORD_ORG_AND_FILL
:
3110 case RECORD_ALIGN_AND_FILL
:
3112 struct avr_property_record
*record
;
3113 unsigned long bytes_to_align
;
3116 /* Look for alignment directives that have had enough
3117 bytes deleted before them, such that the directive
3118 can be moved backwards and still maintain the
3119 required alignment. */
3120 record
= &relax_info
->records
.items
[i
];
3122 = (unsigned long) (1 << record
->data
.align
.bytes
);
3123 while (record
->data
.align
.preceding_deleted
>=
3126 record
->data
.align
.preceding_deleted
3128 count
+= bytes_to_align
;
3133 bfd_vma addr
= record
->offset
;
3135 /* We can delete COUNT bytes and this alignment
3136 directive will still be correctly aligned.
3137 First move the alignment directive, then delete
3139 record
->offset
-= count
;
3140 elf32_avr_relax_delete_bytes (abfd
, sec
,
3152 if (contents
!= NULL
3153 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3155 if (! link_info
->keep_memory
)
3159 /* Cache the section contents for elf_link_input_bfd. */
3160 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3164 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
3165 free (internal_relocs
);
3170 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3172 if (elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3174 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
3175 free (internal_relocs
);
3180 /* This is a version of bfd_generic_get_relocated_section_contents
3181 which uses elf32_avr_relocate_section.
3183 For avr it's essentially a cut and paste taken from the H8300 port.
3184 The author of the relaxation support patch for avr had absolutely no
3185 clue what is happening here but found out that this part of the code
3186 seems to be important. */
3189 elf32_avr_get_relocated_section_contents (bfd
*output_bfd
,
3190 struct bfd_link_info
*link_info
,
3191 struct bfd_link_order
*link_order
,
3196 Elf_Internal_Shdr
*symtab_hdr
;
3197 asection
*input_section
= link_order
->u
.indirect
.section
;
3198 bfd
*input_bfd
= input_section
->owner
;
3199 asection
**sections
= NULL
;
3200 Elf_Internal_Rela
*internal_relocs
= NULL
;
3201 Elf_Internal_Sym
*isymbuf
= NULL
;
3203 /* We only need to handle the case of relaxing, or of having a
3204 particular set of section contents, specially. */
3206 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
3207 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
3211 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3213 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
3214 (size_t) input_section
->size
);
3216 if ((input_section
->flags
& SEC_RELOC
) != 0
3217 && input_section
->reloc_count
> 0)
3220 Elf_Internal_Sym
*isym
, *isymend
;
3223 internal_relocs
= (_bfd_elf_link_read_relocs
3224 (input_bfd
, input_section
, NULL
, NULL
, false));
3225 if (internal_relocs
== NULL
)
3228 if (symtab_hdr
->sh_info
!= 0)
3230 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3231 if (isymbuf
== NULL
)
3232 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3233 symtab_hdr
->sh_info
, 0,
3235 if (isymbuf
== NULL
)
3239 amt
= symtab_hdr
->sh_info
;
3240 amt
*= sizeof (asection
*);
3241 sections
= bfd_malloc (amt
);
3242 if (sections
== NULL
&& amt
!= 0)
3245 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
3246 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
3250 if (isym
->st_shndx
== SHN_UNDEF
)
3251 isec
= bfd_und_section_ptr
;
3252 else if (isym
->st_shndx
== SHN_ABS
)
3253 isec
= bfd_abs_section_ptr
;
3254 else if (isym
->st_shndx
== SHN_COMMON
)
3255 isec
= bfd_com_section_ptr
;
3257 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
3262 if (! elf32_avr_relocate_section (output_bfd
, link_info
, input_bfd
,
3263 input_section
, data
, internal_relocs
,
3268 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3270 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
3271 free (internal_relocs
);
3278 if (symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3280 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
3281 free (internal_relocs
);
3286 /* Determines the hash entry name for a particular reloc. It consists of
3287 the identifier of the symbol section and the added reloc addend and
3288 symbol offset relative to the section the symbol is attached to. */
3291 avr_stub_name (const asection
*symbol_section
,
3292 const bfd_vma symbol_offset
,
3293 const Elf_Internal_Rela
*rela
)
3298 len
= 8 + 1 + 8 + 1 + 1;
3299 stub_name
= bfd_malloc (len
);
3300 if (stub_name
!= NULL
)
3301 sprintf (stub_name
, "%08x+%08x",
3302 symbol_section
->id
& 0xffffffff,
3303 (unsigned int) ((rela
->r_addend
& 0xffffffff) + symbol_offset
));
3309 /* Add a new stub entry to the stub hash. Not all fields of the new
3310 stub entry are initialised. */
3312 static struct elf32_avr_stub_hash_entry
*
3313 avr_add_stub (const char *stub_name
,
3314 struct elf32_avr_link_hash_table
*htab
)
3316 struct elf32_avr_stub_hash_entry
*hsh
;
3318 /* Enter this entry into the linker stub hash table. */
3319 hsh
= avr_stub_hash_lookup (&htab
->bstab
, stub_name
, true, false);
3323 /* xgettext:c-format */
3324 _bfd_error_handler (_("cannot create stub entry %s"), stub_name
);
3328 hsh
->stub_offset
= 0;
3332 /* We assume that there is already space allocated for the stub section
3333 contents and that before building the stubs the section size is
3334 initialized to 0. We assume that within the stub hash table entry,
3335 the absolute position of the jmp target has been written in the
3336 target_value field. We write here the offset of the generated jmp insn
3337 relative to the trampoline section start to the stub_offset entry in
3338 the stub hash table entry. */
3341 avr_build_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
3343 struct elf32_avr_stub_hash_entry
*hsh
;
3344 struct bfd_link_info
*info
;
3345 struct elf32_avr_link_hash_table
*htab
;
3352 bfd_vma jmp_insn
= 0x0000940c;
3354 /* Massage our args to the form they really have. */
3355 hsh
= avr_stub_hash_entry (bh
);
3357 if (!hsh
->is_actually_needed
)
3360 info
= (struct bfd_link_info
*) in_arg
;
3362 htab
= avr_link_hash_table (info
);
3366 target
= hsh
->target_value
;
3368 /* Make a note of the offset within the stubs for this entry. */
3369 hsh
->stub_offset
= htab
->stub_sec
->size
;
3370 loc
= htab
->stub_sec
->contents
+ hsh
->stub_offset
;
3372 stub_bfd
= htab
->stub_sec
->owner
;
3375 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
3376 (unsigned int) target
,
3377 (unsigned int) hsh
->stub_offset
);
3379 /* We now have to add the information on the jump target to the bare
3380 opcode bits already set in jmp_insn. */
3382 /* Check for the alignment of the address. */
3386 starget
= target
>> 1;
3387 jmp_insn
|= ((starget
& 0x10000) | ((starget
<< 3) & 0x1f00000)) >> 16;
3388 bfd_put_16 (stub_bfd
, jmp_insn
, loc
);
3389 bfd_put_16 (stub_bfd
, (bfd_vma
) starget
& 0xffff, loc
+ 2);
3391 htab
->stub_sec
->size
+= 4;
3393 /* Now add the entries in the address mapping table if there is still
3398 nr
= htab
->amt_entry_cnt
+ 1;
3399 if (nr
<= htab
->amt_max_entry_cnt
)
3401 htab
->amt_entry_cnt
= nr
;
3403 htab
->amt_stub_offsets
[nr
- 1] = hsh
->stub_offset
;
3404 htab
->amt_destination_addr
[nr
- 1] = target
;
3412 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry
*bh
,
3413 void *in_arg ATTRIBUTE_UNUSED
)
3415 struct elf32_avr_stub_hash_entry
*hsh
;
3417 hsh
= avr_stub_hash_entry (bh
);
3418 hsh
->is_actually_needed
= false;
3424 avr_size_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
3426 struct elf32_avr_stub_hash_entry
*hsh
;
3427 struct elf32_avr_link_hash_table
*htab
;
3430 /* Massage our args to the form they really have. */
3431 hsh
= avr_stub_hash_entry (bh
);
3434 if (hsh
->is_actually_needed
)
3439 htab
->stub_sec
->size
+= size
;
3444 elf32_avr_setup_params (struct bfd_link_info
*info
,
3446 asection
*avr_stub_section
,
3450 bfd_vma pc_wrap_around
,
3451 bool call_ret_replacement
)
3453 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3457 htab
->stub_sec
= avr_stub_section
;
3458 htab
->stub_bfd
= avr_stub_bfd
;
3459 htab
->no_stubs
= no_stubs
;
3461 debug_relax
= deb_relax
;
3462 debug_stubs
= deb_stubs
;
3463 avr_pc_wrap_around
= pc_wrap_around
;
3464 avr_replace_call_ret_sequences
= call_ret_replacement
;
3468 /* Set up various things so that we can make a list of input sections
3469 for each output section included in the link. Returns -1 on error,
3470 0 when no stubs will be needed, and 1 on success. It also sets
3471 information on the stubs bfd and the stub section in the info
3475 elf32_avr_setup_section_lists (bfd
*output_bfd
,
3476 struct bfd_link_info
*info
)
3479 unsigned int bfd_count
;
3480 unsigned int top_id
, top_index
;
3482 asection
**input_list
, **list
;
3484 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3486 if (htab
== NULL
|| htab
->no_stubs
)
3489 /* Count the number of input BFDs and find the top input section id. */
3490 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
3492 input_bfd
= input_bfd
->link
.next
)
3495 for (section
= input_bfd
->sections
;
3497 section
= section
->next
)
3498 if (top_id
< section
->id
)
3499 top_id
= section
->id
;
3502 htab
->bfd_count
= bfd_count
;
3504 /* We can't use output_bfd->section_count here to find the top output
3505 section index as some sections may have been removed, and
3506 strip_excluded_output_sections doesn't renumber the indices. */
3507 for (section
= output_bfd
->sections
, top_index
= 0;
3509 section
= section
->next
)
3510 if (top_index
< section
->index
)
3511 top_index
= section
->index
;
3513 htab
->top_index
= top_index
;
3514 amt
= sizeof (asection
*) * (top_index
+ 1);
3515 input_list
= bfd_malloc (amt
);
3516 htab
->input_list
= input_list
;
3517 if (input_list
== NULL
)
3520 /* For sections we aren't interested in, mark their entries with a
3521 value we can check later. */
3522 list
= input_list
+ top_index
;
3524 *list
= bfd_abs_section_ptr
;
3525 while (list
-- != input_list
);
3527 for (section
= output_bfd
->sections
;
3529 section
= section
->next
)
3530 if ((section
->flags
& SEC_CODE
) != 0)
3531 input_list
[section
->index
] = NULL
;
3537 /* Read in all local syms for all input bfds, and create hash entries
3538 for export stubs if we are building a multi-subspace shared lib.
3539 Returns -1 on error, 0 otherwise. */
3542 get_local_syms (bfd
*input_bfd
, struct bfd_link_info
*info
)
3544 unsigned int bfd_indx
;
3545 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
3546 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3552 /* We want to read in symbol extension records only once. To do this
3553 we need to read in the local symbols in parallel and save them for
3554 later use; so hold pointers to the local symbols in an array. */
3555 amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
3556 all_local_syms
= bfd_zmalloc (amt
);
3557 htab
->all_local_syms
= all_local_syms
;
3558 if (all_local_syms
== NULL
)
3561 /* Walk over all the input BFDs, swapping in local symbols.
3562 If we are creating a shared library, create hash entries for the
3566 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3568 Elf_Internal_Shdr
*symtab_hdr
;
3570 /* We'll need the symbol table in a second. */
3571 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3572 if (symtab_hdr
->sh_info
== 0)
3575 /* We need an array of the local symbols attached to the input bfd. */
3576 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3577 if (local_syms
== NULL
)
3579 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3580 symtab_hdr
->sh_info
, 0,
3582 /* Cache them for elf_link_input_bfd. */
3583 symtab_hdr
->contents
= (unsigned char *) local_syms
;
3585 if (local_syms
== NULL
)
3588 all_local_syms
[bfd_indx
] = local_syms
;
3594 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
3597 elf32_avr_size_stubs (bfd
*output_bfd
,
3598 struct bfd_link_info
*info
,
3599 bool is_prealloc_run
)
3601 struct elf32_avr_link_hash_table
*htab
;
3602 int stub_changed
= 0;
3604 htab
= avr_link_hash_table (info
);
3608 /* At this point we initialize htab->vector_base
3609 To the start of the text output section. */
3610 htab
->vector_base
= htab
->stub_sec
->output_section
->vma
;
3612 if (get_local_syms (info
->input_bfds
, info
))
3614 if (htab
->all_local_syms
)
3615 goto error_ret_free_local
;
3619 if (ADD_DUMMY_STUBS_FOR_DEBUGGING
)
3621 struct elf32_avr_stub_hash_entry
*test
;
3623 test
= avr_add_stub ("Hugo",htab
);
3624 test
->target_value
= 0x123456;
3625 test
->stub_offset
= 13;
3627 test
= avr_add_stub ("Hugo2",htab
);
3628 test
->target_value
= 0x84210;
3629 test
->stub_offset
= 14;
3635 unsigned int bfd_indx
;
3637 /* We will have to re-generate the stub hash table each time anything
3638 in memory has changed. */
3640 bfd_hash_traverse (&htab
->bstab
, avr_mark_stub_not_to_be_necessary
, htab
);
3641 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
3643 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3645 Elf_Internal_Shdr
*symtab_hdr
;
3647 Elf_Internal_Sym
*local_syms
;
3649 /* We'll need the symbol table in a second. */
3650 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3651 if (symtab_hdr
->sh_info
== 0)
3654 local_syms
= htab
->all_local_syms
[bfd_indx
];
3656 /* Walk over each section attached to the input bfd. */
3657 for (section
= input_bfd
->sections
;
3659 section
= section
->next
)
3661 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
3663 /* If there aren't any relocs, then there's nothing more
3665 if ((section
->flags
& SEC_RELOC
) == 0
3666 || section
->reloc_count
== 0)
3669 /* If this section is a link-once section that will be
3670 discarded, then don't create any stubs. */
3671 if (section
->output_section
== NULL
3672 || section
->output_section
->owner
!= output_bfd
)
3675 /* Get the relocs. */
3677 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
3679 if (internal_relocs
== NULL
)
3680 goto error_ret_free_local
;
3682 /* Now examine each relocation. */
3683 irela
= internal_relocs
;
3684 irelaend
= irela
+ section
->reloc_count
;
3685 for (; irela
< irelaend
; irela
++)
3687 unsigned int r_type
, r_indx
;
3688 struct elf32_avr_stub_hash_entry
*hsh
;
3691 bfd_vma destination
;
3692 struct elf_link_hash_entry
*hh
;
3695 r_type
= ELF32_R_TYPE (irela
->r_info
);
3696 r_indx
= ELF32_R_SYM (irela
->r_info
);
3698 /* Only look for 16 bit GS relocs. No other reloc will need a
3700 if (!((r_type
== R_AVR_16_PM
)
3701 || (r_type
== R_AVR_LO8_LDI_GS
)
3702 || (r_type
== R_AVR_HI8_LDI_GS
)))
3705 /* Now determine the call target, its name, value,
3711 if (r_indx
< symtab_hdr
->sh_info
)
3713 /* It's a local symbol. */
3714 Elf_Internal_Sym
*sym
;
3715 Elf_Internal_Shdr
*hdr
;
3718 sym
= local_syms
+ r_indx
;
3719 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3720 sym_value
= sym
->st_value
;
3721 shndx
= sym
->st_shndx
;
3722 if (shndx
< elf_numsections (input_bfd
))
3724 hdr
= elf_elfsections (input_bfd
)[shndx
];
3725 sym_sec
= hdr
->bfd_section
;
3726 destination
= (sym_value
+ irela
->r_addend
3727 + sym_sec
->output_offset
3728 + sym_sec
->output_section
->vma
);
3733 /* It's an external symbol. */
3736 e_indx
= r_indx
- symtab_hdr
->sh_info
;
3737 hh
= elf_sym_hashes (input_bfd
)[e_indx
];
3739 while (hh
->root
.type
== bfd_link_hash_indirect
3740 || hh
->root
.type
== bfd_link_hash_warning
)
3741 hh
= (struct elf_link_hash_entry
*)
3742 (hh
->root
.u
.i
.link
);
3744 if (hh
->root
.type
== bfd_link_hash_defined
3745 || hh
->root
.type
== bfd_link_hash_defweak
)
3747 sym_sec
= hh
->root
.u
.def
.section
;
3748 sym_value
= hh
->root
.u
.def
.value
;
3749 if (sym_sec
->output_section
!= NULL
)
3750 destination
= (sym_value
+ irela
->r_addend
3751 + sym_sec
->output_offset
3752 + sym_sec
->output_section
->vma
);
3754 else if (hh
->root
.type
== bfd_link_hash_undefweak
)
3756 if (! bfd_link_pic (info
))
3759 else if (hh
->root
.type
== bfd_link_hash_undefined
)
3761 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
3762 && (ELF_ST_VISIBILITY (hh
->other
)
3768 bfd_set_error (bfd_error_bad_value
);
3770 error_ret_free_internal
:
3771 if (elf_section_data (section
)->relocs
== NULL
)
3772 free (internal_relocs
);
3773 goto error_ret_free_local
;
3777 if (! avr_stub_is_required_for_16_bit_reloc
3778 (destination
- htab
->vector_base
))
3780 if (!is_prealloc_run
)
3781 /* We are having a reloc that does't need a stub. */
3784 /* We don't right now know if a stub will be needed.
3785 Let's rather be on the safe side. */
3788 /* Get the name of this stub. */
3789 stub_name
= avr_stub_name (sym_sec
, sym_value
, irela
);
3792 goto error_ret_free_internal
;
3795 hsh
= avr_stub_hash_lookup (&htab
->bstab
,
3800 /* The proper stub has already been created. Mark it
3801 to be used and write the possibly changed destination
3803 hsh
->is_actually_needed
= true;
3804 hsh
->target_value
= destination
;
3809 hsh
= avr_add_stub (stub_name
, htab
);
3813 goto error_ret_free_internal
;
3816 hsh
->is_actually_needed
= true;
3817 hsh
->target_value
= destination
;
3820 printf ("Adding stub with destination 0x%x to the"
3821 " hash table.\n", (unsigned int) destination
);
3823 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run
);
3825 stub_changed
= true;
3828 /* We're done with the internal relocs, free them. */
3829 if (elf_section_data (section
)->relocs
== NULL
)
3830 free (internal_relocs
);
3834 /* Re-Calculate the number of needed stubs. */
3835 htab
->stub_sec
->size
= 0;
3836 bfd_hash_traverse (&htab
->bstab
, avr_size_one_stub
, htab
);
3841 stub_changed
= false;
3844 free (htab
->all_local_syms
);
3847 error_ret_free_local
:
3848 free (htab
->all_local_syms
);
3853 /* Build all the stubs associated with the current output file. The
3854 stubs are kept in a hash table attached to the main linker hash
3855 table. We also set up the .plt entries for statically linked PIC
3856 functions here. This function is called via hppaelf_finish in the
3860 elf32_avr_build_stubs (struct bfd_link_info
*info
)
3863 struct bfd_hash_table
*table
;
3864 struct elf32_avr_link_hash_table
*htab
;
3865 bfd_size_type total_size
= 0;
3867 htab
= avr_link_hash_table (info
);
3871 /* In case that there were several stub sections: */
3872 for (stub_sec
= htab
->stub_bfd
->sections
;
3874 stub_sec
= stub_sec
->next
)
3878 /* Allocate memory to hold the linker stubs. */
3879 size
= stub_sec
->size
;
3882 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
3883 if (stub_sec
->contents
== NULL
&& size
!= 0)
3888 /* Allocate memory for the adress mapping table. */
3889 htab
->amt_entry_cnt
= 0;
3890 htab
->amt_max_entry_cnt
= total_size
/ 4;
3891 htab
->amt_stub_offsets
= bfd_malloc (sizeof (bfd_vma
)
3892 * htab
->amt_max_entry_cnt
);
3893 htab
->amt_destination_addr
= bfd_malloc (sizeof (bfd_vma
)
3894 * htab
->amt_max_entry_cnt
);
3897 printf ("Allocating %i entries in the AMT\n", htab
->amt_max_entry_cnt
);
3899 /* Build the stubs as directed by the stub hash table. */
3900 table
= &htab
->bstab
;
3901 bfd_hash_traverse (table
, avr_build_one_stub
, info
);
3904 printf ("Final Stub section Size: %i\n", (int) htab
->stub_sec
->size
);
3909 /* Callback used by QSORT to order relocations AP and BP. */
3912 internal_reloc_compare (const void *ap
, const void *bp
)
3914 const Elf_Internal_Rela
*a
= (const Elf_Internal_Rela
*) ap
;
3915 const Elf_Internal_Rela
*b
= (const Elf_Internal_Rela
*) bp
;
3917 if (a
->r_offset
!= b
->r_offset
)
3918 return (a
->r_offset
- b
->r_offset
);
3920 /* We don't need to sort on these criteria for correctness,
3921 but enforcing a more strict ordering prevents unstable qsort
3922 from behaving differently with different implementations.
3923 Without the code below we get correct but different results
3924 on Solaris 2.7 and 2.8. We would like to always produce the
3925 same results no matter the host. */
3927 if (a
->r_info
!= b
->r_info
)
3928 return (a
->r_info
- b
->r_info
);
3930 return (a
->r_addend
- b
->r_addend
);
3933 /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */
3936 avr_is_section_for_address (asection
*section
, bfd_vma address
)
3941 vma
= bfd_section_vma (section
);
3945 size
= section
->size
;
3946 if (address
>= vma
+ size
)
3952 /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */
3954 struct avr_find_section_data
3956 /* The address we're looking for. */
3959 /* The section we've found. */
3963 /* Helper function to locate the section holding a certain virtual memory
3964 address. This is called via bfd_map_over_sections. The DATA is an
3965 instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which
3966 has been set to the address to search for, and the section field has
3967 been set to NULL. If SECTION from ABFD contains ADDRESS then the
3968 section field in DATA will be set to SECTION. As an optimisation, if
3969 the section field is already non-null then this function does not
3970 perform any checks, and just returns. */
3973 avr_find_section_for_address (bfd
*abfd ATTRIBUTE_UNUSED
,
3974 asection
*section
, void *data
)
3976 struct avr_find_section_data
*fs_data
3977 = (struct avr_find_section_data
*) data
;
3979 /* Return if already found. */
3980 if (fs_data
->section
!= NULL
)
3983 /* If this section isn't part of the addressable code content, skip it. */
3984 if ((bfd_section_flags (section
) & SEC_ALLOC
) == 0
3985 && (bfd_section_flags (section
) & SEC_CODE
) == 0)
3988 if (avr_is_section_for_address (section
, fs_data
->address
))
3989 fs_data
->section
= section
;
3992 /* Load all of the property records from SEC, a section from ABFD. Return
3993 a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The
3994 memory for the returned structure, and all of the records pointed too by
3995 the structure are allocated with a single call to malloc, so, only the
3996 pointer returned needs to be free'd. */
3998 static struct avr_property_record_list
*
3999 avr_elf32_load_records_from_section (bfd
*abfd
, asection
*sec
)
4001 bfd_byte
*contents
, *ptr
;
4002 bfd_size_type size
, mem_size
;
4003 bfd_byte version
, flags
;
4004 uint16_t record_count
, i
;
4005 struct avr_property_record_list
*r_list
= NULL
;
4006 Elf_Internal_Rela
*internal_relocs
= NULL
, *rel
, *rel_end
;
4007 struct avr_find_section_data fs_data
;
4009 fs_data
.section
= NULL
;
4011 if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
4015 /* Load the relocations for the '.avr.prop' section if there are any, and
4017 internal_relocs
= (_bfd_elf_link_read_relocs
4018 (abfd
, sec
, NULL
, NULL
, false));
4019 if (internal_relocs
)
4020 qsort (internal_relocs
, sec
->reloc_count
,
4021 sizeof (Elf_Internal_Rela
), internal_reloc_compare
);
4023 /* There is a header at the start of the property record section SEC, the
4024 format of this header is:
4025 uint8_t : version number
4027 uint16_t : record counter
4030 /* Check we have at least got a headers worth of bytes. */
4031 size
= bfd_section_size (sec
);
4032 if (size
< AVR_PROPERTY_SECTION_HEADER_SIZE
)
4039 record_count
= bfd_get_16 (abfd
, ptr
);
4041 BFD_ASSERT (ptr
- contents
== AVR_PROPERTY_SECTION_HEADER_SIZE
);
4043 /* Now allocate space for the list structure, and all of the list
4044 elements in a single block. */
4045 mem_size
= sizeof (struct avr_property_record_list
)
4046 + sizeof (struct avr_property_record
) * record_count
;
4047 r_list
= bfd_malloc (mem_size
);
4051 r_list
->version
= version
;
4052 r_list
->flags
= flags
;
4053 r_list
->section
= sec
;
4054 r_list
->record_count
= record_count
;
4055 r_list
->records
= (struct avr_property_record
*) (&r_list
[1]);
4056 size
-= AVR_PROPERTY_SECTION_HEADER_SIZE
;
4058 /* Check that we understand the version number. There is only one
4059 version number right now, anything else is an error. */
4060 if (r_list
->version
!= AVR_PROPERTY_RECORDS_VERSION
)
4063 rel
= internal_relocs
;
4064 rel_end
= rel
+ sec
->reloc_count
;
4065 for (i
= 0; i
< record_count
; ++i
)
4069 /* Each entry is a 32-bit address, followed by a single byte type.
4070 After that is the type specific data. We must take care to
4071 ensure that we don't read beyond the end of the section data. */
4075 r_list
->records
[i
].section
= NULL
;
4076 r_list
->records
[i
].offset
= 0;
4080 /* The offset of the address within the .avr.prop section. */
4081 size_t offset
= ptr
- contents
;
4083 while (rel
< rel_end
&& rel
->r_offset
< offset
)
4088 else if (rel
->r_offset
== offset
)
4090 /* Find section and section offset. */
4091 unsigned long r_symndx
;
4096 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4097 rel_sec
= get_elf_r_symndx_section (abfd
, r_symndx
);
4098 sec_offset
= get_elf_r_symndx_offset (abfd
, r_symndx
)
4101 r_list
->records
[i
].section
= rel_sec
;
4102 r_list
->records
[i
].offset
= sec_offset
;
4106 address
= bfd_get_32 (abfd
, ptr
);
4110 if (r_list
->records
[i
].section
== NULL
)
4112 /* Try to find section and offset from address. */
4113 if (fs_data
.section
!= NULL
4114 && !avr_is_section_for_address (fs_data
.section
, address
))
4115 fs_data
.section
= NULL
;
4117 if (fs_data
.section
== NULL
)
4119 fs_data
.address
= address
;
4120 bfd_map_over_sections (abfd
, avr_find_section_for_address
,
4124 if (fs_data
.section
== NULL
)
4126 fprintf (stderr
, "Failed to find matching section.\n");
4130 r_list
->records
[i
].section
= fs_data
.section
;
4131 r_list
->records
[i
].offset
4132 = address
- bfd_section_vma (fs_data
.section
);
4135 r_list
->records
[i
].type
= *ptr
;
4139 switch (r_list
->records
[i
].type
)
4142 /* Nothing else to load. */
4144 case RECORD_ORG_AND_FILL
:
4145 /* Just a 4-byte fill to load. */
4148 r_list
->records
[i
].data
.org
.fill
= bfd_get_32 (abfd
, ptr
);
4153 /* Just a 4-byte alignment to load. */
4156 r_list
->records
[i
].data
.align
.bytes
= bfd_get_32 (abfd
, ptr
);
4159 /* Just initialise PRECEDING_DELETED field, this field is
4160 used during linker relaxation. */
4161 r_list
->records
[i
].data
.align
.preceding_deleted
= 0;
4163 case RECORD_ALIGN_AND_FILL
:
4164 /* A 4-byte alignment, and a 4-byte fill to load. */
4167 r_list
->records
[i
].data
.align
.bytes
= bfd_get_32 (abfd
, ptr
);
4169 r_list
->records
[i
].data
.align
.fill
= bfd_get_32 (abfd
, ptr
);
4172 /* Just initialise PRECEDING_DELETED field, this field is
4173 used during linker relaxation. */
4174 r_list
->records
[i
].data
.align
.preceding_deleted
= 0;
4182 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
4183 free (internal_relocs
);
4187 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
4188 free (internal_relocs
);
4194 /* Load all of the property records from ABFD. See
4195 AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */
4197 struct avr_property_record_list
*
4198 avr_elf32_load_property_records (bfd
*abfd
)
4202 /* Find the '.avr.prop' section and load the contents into memory. */
4203 sec
= bfd_get_section_by_name (abfd
, AVR_PROPERTY_RECORD_SECTION_NAME
);
4206 return avr_elf32_load_records_from_section (abfd
, sec
);
4210 avr_elf32_property_record_name (struct avr_property_record
*rec
)
4219 case RECORD_ORG_AND_FILL
:
4225 case RECORD_ALIGN_AND_FILL
:
4236 #define ELF_ARCH bfd_arch_avr
4237 #define ELF_TARGET_ID AVR_ELF_DATA
4238 #define ELF_MACHINE_CODE EM_AVR
4239 #define ELF_MACHINE_ALT1 EM_AVR_OLD
4240 #define ELF_MAXPAGESIZE 1
4242 #define TARGET_LITTLE_SYM avr_elf32_vec
4243 #define TARGET_LITTLE_NAME "elf32-avr"
4245 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
4247 #define elf_info_to_howto avr_info_to_howto_rela
4248 #define elf_info_to_howto_rel NULL
4249 #define elf_backend_relocate_section elf32_avr_relocate_section
4250 #define elf_backend_can_gc_sections 1
4251 #define elf_backend_rela_normal 1
4252 #define elf_backend_final_write_processing \
4253 bfd_elf_avr_final_write_processing
4254 #define elf_backend_object_p elf32_avr_object_p
4256 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
4257 #define bfd_elf32_bfd_get_relocated_section_contents \
4258 elf32_avr_get_relocated_section_contents
4259 #define bfd_elf32_new_section_hook elf_avr_new_section_hook
4260 #define elf_backend_special_sections elf_avr_special_sections
4262 #include "elf32-target.h"