1 /* AVR-specific support for 32-bit ELF
2 Copyright (C) 1999-2017 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"
28 #include "bfd_stdint.h"
30 /* Enable debugging printout at stdout with this variable. */
31 static bfd_boolean debug_relax
= FALSE
;
33 /* Enable debugging printout at stdout with this variable. */
34 static bfd_boolean debug_stubs
= FALSE
;
36 static bfd_reloc_status_type
37 bfd_elf_avr_diff_reloc (bfd
*, arelent
*, asymbol
*, void *,
38 asection
*, bfd
*, char **);
40 /* Hash table initialization and handling. Code is taken from the hppa port
41 and adapted to the needs of AVR. */
43 /* We use two hash tables to hold information for linking avr objects.
45 The first is the elf32_avr_link_hash_table which is derived from the
46 stanard ELF linker hash table. We use this as a place to attach the other
47 hash table and some static information.
49 The second is the stub hash table which is derived from the base BFD
50 hash table. The stub hash table holds the information on the linker
53 struct elf32_avr_stub_hash_entry
55 /* Base hash table entry structure. */
56 struct bfd_hash_entry bh_root
;
58 /* Offset within stub_sec of the beginning of this stub. */
61 /* Given the symbol's value and its section we can determine its final
62 value when building the stubs (so the stub knows where to jump). */
65 /* This way we could mark stubs to be no longer necessary. */
66 bfd_boolean is_actually_needed
;
69 struct elf32_avr_link_hash_table
71 /* The main hash table. */
72 struct elf_link_hash_table etab
;
74 /* The stub hash table. */
75 struct bfd_hash_table bstab
;
79 /* Linker stub bfd. */
82 /* The stub section. */
85 /* Usually 0, unless we are generating code for a bootloader. Will
86 be initialized by elf32_avr_size_stubs to the vma offset of the
87 output section associated with the stub section. */
90 /* Assorted information used by elf32_avr_size_stubs. */
91 unsigned int bfd_count
;
92 unsigned int top_index
;
93 asection
** input_list
;
94 Elf_Internal_Sym
** all_local_syms
;
96 /* Tables for mapping vma beyond the 128k boundary to the address of the
97 corresponding stub. (AMT)
98 "amt_max_entry_cnt" reflects the number of entries that memory is allocated
99 for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
100 "amt_entry_cnt" informs how many of these entries actually contain
102 unsigned int amt_entry_cnt
;
103 unsigned int amt_max_entry_cnt
;
104 bfd_vma
* amt_stub_offsets
;
105 bfd_vma
* amt_destination_addr
;
108 /* Various hash macros and functions. */
109 #define avr_link_hash_table(p) \
110 /* PR 3874: Check that we have an AVR style hash table before using it. */\
111 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
112 == AVR_ELF_DATA ? ((struct elf32_avr_link_hash_table *) ((p)->hash)) : NULL)
114 #define avr_stub_hash_entry(ent) \
115 ((struct elf32_avr_stub_hash_entry *)(ent))
117 #define avr_stub_hash_lookup(table, string, create, copy) \
118 ((struct elf32_avr_stub_hash_entry *) \
119 bfd_hash_lookup ((table), (string), (create), (copy)))
121 static reloc_howto_type elf_avr_howto_table
[] =
123 HOWTO (R_AVR_NONE
, /* type */
125 3, /* size (0 = byte, 1 = short, 2 = long) */
127 FALSE
, /* pc_relative */
129 complain_overflow_dont
, /* complain_on_overflow */
130 bfd_elf_generic_reloc
, /* special_function */
131 "R_AVR_NONE", /* name */
132 FALSE
, /* partial_inplace */
135 FALSE
), /* pcrel_offset */
137 HOWTO (R_AVR_32
, /* type */
139 2, /* size (0 = byte, 1 = short, 2 = long) */
141 FALSE
, /* pc_relative */
143 complain_overflow_bitfield
, /* complain_on_overflow */
144 bfd_elf_generic_reloc
, /* special_function */
145 "R_AVR_32", /* name */
146 FALSE
, /* partial_inplace */
147 0xffffffff, /* src_mask */
148 0xffffffff, /* dst_mask */
149 FALSE
), /* pcrel_offset */
151 /* A 7 bit PC relative relocation. */
152 HOWTO (R_AVR_7_PCREL
, /* type */
154 1, /* size (0 = byte, 1 = short, 2 = long) */
156 TRUE
, /* pc_relative */
158 complain_overflow_bitfield
, /* complain_on_overflow */
159 bfd_elf_generic_reloc
, /* special_function */
160 "R_AVR_7_PCREL", /* name */
161 FALSE
, /* partial_inplace */
162 0xffff, /* src_mask */
163 0xffff, /* dst_mask */
164 TRUE
), /* pcrel_offset */
166 /* A 13 bit PC relative relocation. */
167 HOWTO (R_AVR_13_PCREL
, /* type */
169 1, /* size (0 = byte, 1 = short, 2 = long) */
171 TRUE
, /* pc_relative */
173 complain_overflow_bitfield
, /* complain_on_overflow */
174 bfd_elf_generic_reloc
, /* special_function */
175 "R_AVR_13_PCREL", /* name */
176 FALSE
, /* partial_inplace */
177 0xfff, /* src_mask */
178 0xfff, /* dst_mask */
179 TRUE
), /* pcrel_offset */
181 /* A 16 bit absolute relocation. */
182 HOWTO (R_AVR_16
, /* type */
184 1, /* size (0 = byte, 1 = short, 2 = long) */
186 FALSE
, /* pc_relative */
188 complain_overflow_dont
, /* complain_on_overflow */
189 bfd_elf_generic_reloc
, /* special_function */
190 "R_AVR_16", /* name */
191 FALSE
, /* partial_inplace */
192 0xffff, /* src_mask */
193 0xffff, /* dst_mask */
194 FALSE
), /* pcrel_offset */
196 /* A 16 bit absolute relocation for command address
197 Will be changed when linker stubs are needed. */
198 HOWTO (R_AVR_16_PM
, /* type */
200 1, /* size (0 = byte, 1 = short, 2 = long) */
202 FALSE
, /* pc_relative */
204 complain_overflow_bitfield
, /* complain_on_overflow */
205 bfd_elf_generic_reloc
, /* special_function */
206 "R_AVR_16_PM", /* name */
207 FALSE
, /* partial_inplace */
208 0xffff, /* src_mask */
209 0xffff, /* dst_mask */
210 FALSE
), /* pcrel_offset */
211 /* A low 8 bit absolute relocation of 16 bit address.
213 HOWTO (R_AVR_LO8_LDI
, /* type */
215 1, /* size (0 = byte, 1 = short, 2 = long) */
217 FALSE
, /* pc_relative */
219 complain_overflow_dont
, /* complain_on_overflow */
220 bfd_elf_generic_reloc
, /* special_function */
221 "R_AVR_LO8_LDI", /* name */
222 FALSE
, /* partial_inplace */
223 0xffff, /* src_mask */
224 0xffff, /* dst_mask */
225 FALSE
), /* pcrel_offset */
226 /* A high 8 bit absolute relocation of 16 bit address.
228 HOWTO (R_AVR_HI8_LDI
, /* type */
230 1, /* size (0 = byte, 1 = short, 2 = long) */
232 FALSE
, /* pc_relative */
234 complain_overflow_dont
, /* complain_on_overflow */
235 bfd_elf_generic_reloc
, /* special_function */
236 "R_AVR_HI8_LDI", /* name */
237 FALSE
, /* partial_inplace */
238 0xffff, /* src_mask */
239 0xffff, /* dst_mask */
240 FALSE
), /* pcrel_offset */
241 /* A high 6 bit absolute relocation of 22 bit address.
242 For LDI command. As well second most significant 8 bit value of
243 a 32 bit link-time constant. */
244 HOWTO (R_AVR_HH8_LDI
, /* type */
246 1, /* size (0 = byte, 1 = short, 2 = long) */
248 FALSE
, /* pc_relative */
250 complain_overflow_dont
, /* complain_on_overflow */
251 bfd_elf_generic_reloc
, /* special_function */
252 "R_AVR_HH8_LDI", /* name */
253 FALSE
, /* partial_inplace */
254 0xffff, /* src_mask */
255 0xffff, /* dst_mask */
256 FALSE
), /* pcrel_offset */
257 /* A negative low 8 bit absolute relocation of 16 bit address.
259 HOWTO (R_AVR_LO8_LDI_NEG
, /* type */
261 1, /* size (0 = byte, 1 = short, 2 = long) */
263 FALSE
, /* pc_relative */
265 complain_overflow_dont
, /* complain_on_overflow */
266 bfd_elf_generic_reloc
, /* special_function */
267 "R_AVR_LO8_LDI_NEG", /* name */
268 FALSE
, /* partial_inplace */
269 0xffff, /* src_mask */
270 0xffff, /* dst_mask */
271 FALSE
), /* pcrel_offset */
272 /* A negative high 8 bit absolute relocation of 16 bit address.
274 HOWTO (R_AVR_HI8_LDI_NEG
, /* type */
276 1, /* size (0 = byte, 1 = short, 2 = long) */
278 FALSE
, /* pc_relative */
280 complain_overflow_dont
, /* complain_on_overflow */
281 bfd_elf_generic_reloc
, /* special_function */
282 "R_AVR_HI8_LDI_NEG", /* name */
283 FALSE
, /* partial_inplace */
284 0xffff, /* src_mask */
285 0xffff, /* dst_mask */
286 FALSE
), /* pcrel_offset */
287 /* A negative high 6 bit absolute relocation of 22 bit address.
289 HOWTO (R_AVR_HH8_LDI_NEG
, /* type */
291 1, /* size (0 = byte, 1 = short, 2 = long) */
293 FALSE
, /* pc_relative */
295 complain_overflow_dont
, /* complain_on_overflow */
296 bfd_elf_generic_reloc
, /* special_function */
297 "R_AVR_HH8_LDI_NEG", /* name */
298 FALSE
, /* partial_inplace */
299 0xffff, /* src_mask */
300 0xffff, /* dst_mask */
301 FALSE
), /* pcrel_offset */
302 /* A low 8 bit absolute relocation of 24 bit program memory address.
303 For LDI command. Will not be changed when linker stubs are needed. */
304 HOWTO (R_AVR_LO8_LDI_PM
, /* type */
306 1, /* size (0 = byte, 1 = short, 2 = long) */
308 FALSE
, /* pc_relative */
310 complain_overflow_dont
, /* complain_on_overflow */
311 bfd_elf_generic_reloc
, /* special_function */
312 "R_AVR_LO8_LDI_PM", /* name */
313 FALSE
, /* partial_inplace */
314 0xffff, /* src_mask */
315 0xffff, /* dst_mask */
316 FALSE
), /* pcrel_offset */
317 /* A low 8 bit absolute relocation of 24 bit program memory address.
318 For LDI command. Will not be changed when linker stubs are needed. */
319 HOWTO (R_AVR_HI8_LDI_PM
, /* type */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
323 FALSE
, /* pc_relative */
325 complain_overflow_dont
, /* complain_on_overflow */
326 bfd_elf_generic_reloc
, /* special_function */
327 "R_AVR_HI8_LDI_PM", /* name */
328 FALSE
, /* partial_inplace */
329 0xffff, /* src_mask */
330 0xffff, /* dst_mask */
331 FALSE
), /* pcrel_offset */
332 /* A low 8 bit absolute relocation of 24 bit program memory address.
333 For LDI command. Will not be changed when linker stubs are needed. */
334 HOWTO (R_AVR_HH8_LDI_PM
, /* type */
336 1, /* size (0 = byte, 1 = short, 2 = long) */
338 FALSE
, /* pc_relative */
340 complain_overflow_dont
, /* complain_on_overflow */
341 bfd_elf_generic_reloc
, /* special_function */
342 "R_AVR_HH8_LDI_PM", /* name */
343 FALSE
, /* partial_inplace */
344 0xffff, /* src_mask */
345 0xffff, /* dst_mask */
346 FALSE
), /* pcrel_offset */
347 /* A low 8 bit absolute relocation of 24 bit program memory address.
348 For LDI command. Will not be changed when linker stubs are needed. */
349 HOWTO (R_AVR_LO8_LDI_PM_NEG
, /* type */
351 1, /* size (0 = byte, 1 = short, 2 = long) */
353 FALSE
, /* pc_relative */
355 complain_overflow_dont
, /* complain_on_overflow */
356 bfd_elf_generic_reloc
, /* special_function */
357 "R_AVR_LO8_LDI_PM_NEG", /* name */
358 FALSE
, /* partial_inplace */
359 0xffff, /* src_mask */
360 0xffff, /* dst_mask */
361 FALSE
), /* pcrel_offset */
362 /* A low 8 bit absolute relocation of 24 bit program memory address.
363 For LDI command. Will not be changed when linker stubs are needed. */
364 HOWTO (R_AVR_HI8_LDI_PM_NEG
, /* type */
366 1, /* size (0 = byte, 1 = short, 2 = long) */
368 FALSE
, /* pc_relative */
370 complain_overflow_dont
, /* complain_on_overflow */
371 bfd_elf_generic_reloc
, /* special_function */
372 "R_AVR_HI8_LDI_PM_NEG", /* name */
373 FALSE
, /* partial_inplace */
374 0xffff, /* src_mask */
375 0xffff, /* dst_mask */
376 FALSE
), /* pcrel_offset */
377 /* A low 8 bit absolute relocation of 24 bit program memory address.
378 For LDI command. Will not be changed when linker stubs are needed. */
379 HOWTO (R_AVR_HH8_LDI_PM_NEG
, /* type */
381 1, /* size (0 = byte, 1 = short, 2 = long) */
383 FALSE
, /* pc_relative */
385 complain_overflow_dont
, /* complain_on_overflow */
386 bfd_elf_generic_reloc
, /* special_function */
387 "R_AVR_HH8_LDI_PM_NEG", /* name */
388 FALSE
, /* partial_inplace */
389 0xffff, /* src_mask */
390 0xffff, /* dst_mask */
391 FALSE
), /* pcrel_offset */
392 /* Relocation for CALL command in ATmega. */
393 HOWTO (R_AVR_CALL
, /* type */
395 2, /* size (0 = byte, 1 = short, 2 = long) */
397 FALSE
, /* pc_relative */
399 complain_overflow_dont
,/* complain_on_overflow */
400 bfd_elf_generic_reloc
, /* special_function */
401 "R_AVR_CALL", /* name */
402 FALSE
, /* partial_inplace */
403 0xffffffff, /* src_mask */
404 0xffffffff, /* dst_mask */
405 FALSE
), /* pcrel_offset */
406 /* A 16 bit absolute relocation of 16 bit address.
408 HOWTO (R_AVR_LDI
, /* type */
410 1, /* size (0 = byte, 1 = short, 2 = long) */
412 FALSE
, /* pc_relative */
414 complain_overflow_dont
,/* complain_on_overflow */
415 bfd_elf_generic_reloc
, /* special_function */
416 "R_AVR_LDI", /* name */
417 FALSE
, /* partial_inplace */
418 0xffff, /* src_mask */
419 0xffff, /* dst_mask */
420 FALSE
), /* pcrel_offset */
421 /* A 6 bit absolute relocation of 6 bit offset.
422 For ldd/sdd command. */
423 HOWTO (R_AVR_6
, /* type */
425 0, /* size (0 = byte, 1 = short, 2 = long) */
427 FALSE
, /* pc_relative */
429 complain_overflow_dont
,/* complain_on_overflow */
430 bfd_elf_generic_reloc
, /* special_function */
431 "R_AVR_6", /* name */
432 FALSE
, /* partial_inplace */
433 0xffff, /* src_mask */
434 0xffff, /* dst_mask */
435 FALSE
), /* pcrel_offset */
436 /* A 6 bit absolute relocation of 6 bit offset.
437 For sbiw/adiw command. */
438 HOWTO (R_AVR_6_ADIW
, /* type */
440 0, /* size (0 = byte, 1 = short, 2 = long) */
442 FALSE
, /* pc_relative */
444 complain_overflow_dont
,/* complain_on_overflow */
445 bfd_elf_generic_reloc
, /* special_function */
446 "R_AVR_6_ADIW", /* name */
447 FALSE
, /* partial_inplace */
448 0xffff, /* src_mask */
449 0xffff, /* dst_mask */
450 FALSE
), /* pcrel_offset */
451 /* Most significant 8 bit value of a 32 bit link-time constant. */
452 HOWTO (R_AVR_MS8_LDI
, /* type */
454 1, /* size (0 = byte, 1 = short, 2 = long) */
456 FALSE
, /* pc_relative */
458 complain_overflow_dont
, /* complain_on_overflow */
459 bfd_elf_generic_reloc
, /* special_function */
460 "R_AVR_MS8_LDI", /* name */
461 FALSE
, /* partial_inplace */
462 0xffff, /* src_mask */
463 0xffff, /* dst_mask */
464 FALSE
), /* pcrel_offset */
465 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
466 HOWTO (R_AVR_MS8_LDI_NEG
, /* type */
468 1, /* size (0 = byte, 1 = short, 2 = long) */
470 FALSE
, /* pc_relative */
472 complain_overflow_dont
, /* complain_on_overflow */
473 bfd_elf_generic_reloc
, /* special_function */
474 "R_AVR_MS8_LDI_NEG", /* name */
475 FALSE
, /* partial_inplace */
476 0xffff, /* src_mask */
477 0xffff, /* dst_mask */
478 FALSE
), /* pcrel_offset */
479 /* A low 8 bit absolute relocation of 24 bit program memory address.
480 For LDI command. Will be changed when linker stubs are needed. */
481 HOWTO (R_AVR_LO8_LDI_GS
, /* type */
483 1, /* size (0 = byte, 1 = short, 2 = long) */
485 FALSE
, /* pc_relative */
487 complain_overflow_dont
, /* complain_on_overflow */
488 bfd_elf_generic_reloc
, /* special_function */
489 "R_AVR_LO8_LDI_GS", /* name */
490 FALSE
, /* partial_inplace */
491 0xffff, /* src_mask */
492 0xffff, /* dst_mask */
493 FALSE
), /* pcrel_offset */
494 /* A low 8 bit absolute relocation of 24 bit program memory address.
495 For LDI command. Will be changed when linker stubs are needed. */
496 HOWTO (R_AVR_HI8_LDI_GS
, /* type */
498 1, /* size (0 = byte, 1 = short, 2 = long) */
500 FALSE
, /* pc_relative */
502 complain_overflow_dont
, /* complain_on_overflow */
503 bfd_elf_generic_reloc
, /* special_function */
504 "R_AVR_HI8_LDI_GS", /* name */
505 FALSE
, /* partial_inplace */
506 0xffff, /* src_mask */
507 0xffff, /* dst_mask */
508 FALSE
), /* pcrel_offset */
510 HOWTO (R_AVR_8
, /* type */
512 0, /* size (0 = byte, 1 = short, 2 = long) */
514 FALSE
, /* pc_relative */
516 complain_overflow_bitfield
,/* complain_on_overflow */
517 bfd_elf_generic_reloc
, /* special_function */
518 "R_AVR_8", /* name */
519 FALSE
, /* partial_inplace */
520 0x000000ff, /* src_mask */
521 0x000000ff, /* dst_mask */
522 FALSE
), /* pcrel_offset */
523 /* lo8-part to use in .byte lo8(sym). */
524 HOWTO (R_AVR_8_LO8
, /* type */
526 0, /* size (0 = byte, 1 = short, 2 = long) */
528 FALSE
, /* pc_relative */
530 complain_overflow_dont
,/* complain_on_overflow */
531 bfd_elf_generic_reloc
, /* special_function */
532 "R_AVR_8_LO8", /* name */
533 FALSE
, /* partial_inplace */
534 0xffffff, /* src_mask */
535 0xffffff, /* dst_mask */
536 FALSE
), /* pcrel_offset */
537 /* hi8-part to use in .byte hi8(sym). */
538 HOWTO (R_AVR_8_HI8
, /* type */
540 0, /* size (0 = byte, 1 = short, 2 = long) */
542 FALSE
, /* pc_relative */
544 complain_overflow_dont
,/* complain_on_overflow */
545 bfd_elf_generic_reloc
, /* special_function */
546 "R_AVR_8_HI8", /* name */
547 FALSE
, /* partial_inplace */
548 0xffffff, /* src_mask */
549 0xffffff, /* dst_mask */
550 FALSE
), /* pcrel_offset */
551 /* hlo8-part to use in .byte hlo8(sym). */
552 HOWTO (R_AVR_8_HLO8
, /* type */
554 0, /* size (0 = byte, 1 = short, 2 = long) */
556 FALSE
, /* pc_relative */
558 complain_overflow_dont
,/* complain_on_overflow */
559 bfd_elf_generic_reloc
, /* special_function */
560 "R_AVR_8_HLO8", /* name */
561 FALSE
, /* partial_inplace */
562 0xffffff, /* src_mask */
563 0xffffff, /* dst_mask */
564 FALSE
), /* pcrel_offset */
565 HOWTO (R_AVR_DIFF8
, /* type */
567 0, /* size (0 = byte, 1 = short, 2 = long) */
569 FALSE
, /* pc_relative */
571 complain_overflow_bitfield
, /* complain_on_overflow */
572 bfd_elf_avr_diff_reloc
, /* special_function */
573 "R_AVR_DIFF8", /* name */
574 FALSE
, /* partial_inplace */
577 FALSE
), /* pcrel_offset */
578 HOWTO (R_AVR_DIFF16
, /* type */
580 1, /* size (0 = byte, 1 = short, 2 = long) */
582 FALSE
, /* pc_relative */
584 complain_overflow_bitfield
, /* complain_on_overflow */
585 bfd_elf_avr_diff_reloc
,/* special_function */
586 "R_AVR_DIFF16", /* name */
587 FALSE
, /* partial_inplace */
589 0xffff, /* dst_mask */
590 FALSE
), /* pcrel_offset */
591 HOWTO (R_AVR_DIFF32
, /* type */
593 2, /* size (0 = byte, 1 = short, 2 = long) */
595 FALSE
, /* pc_relative */
597 complain_overflow_bitfield
, /* complain_on_overflow */
598 bfd_elf_avr_diff_reloc
,/* special_function */
599 "R_AVR_DIFF32", /* name */
600 FALSE
, /* partial_inplace */
602 0xffffffff, /* dst_mask */
603 FALSE
), /* pcrel_offset */
604 /* 7 bit immediate for LDS/STS in Tiny core. */
605 HOWTO (R_AVR_LDS_STS_16
, /* type */
607 1, /* size (0 = byte, 1 = short, 2 = long) */
609 FALSE
, /* pc_relative */
611 complain_overflow_dont
,/* complain_on_overflow */
612 bfd_elf_generic_reloc
, /* special_function */
613 "R_AVR_LDS_STS_16", /* name */
614 FALSE
, /* partial_inplace */
615 0xffff, /* src_mask */
616 0xffff, /* dst_mask */
617 FALSE
), /* pcrel_offset */
619 HOWTO (R_AVR_PORT6
, /* type */
621 0, /* size (0 = byte, 1 = short, 2 = long) */
623 FALSE
, /* pc_relative */
625 complain_overflow_dont
,/* complain_on_overflow */
626 bfd_elf_generic_reloc
, /* special_function */
627 "R_AVR_PORT6", /* name */
628 FALSE
, /* partial_inplace */
629 0xffffff, /* src_mask */
630 0xffffff, /* dst_mask */
631 FALSE
), /* pcrel_offset */
632 HOWTO (R_AVR_PORT5
, /* type */
634 0, /* size (0 = byte, 1 = short, 2 = long) */
636 FALSE
, /* pc_relative */
638 complain_overflow_dont
,/* complain_on_overflow */
639 bfd_elf_generic_reloc
, /* special_function */
640 "R_AVR_PORT5", /* name */
641 FALSE
, /* partial_inplace */
642 0xffffff, /* src_mask */
643 0xffffff, /* dst_mask */
644 FALSE
), /* pcrel_offset */
646 /* A 32 bit PC relative relocation. */
647 HOWTO (R_AVR_32_PCREL
, /* type */
649 2, /* size (0 = byte, 1 = short, 2 = long) */
651 TRUE
, /* pc_relative */
653 complain_overflow_bitfield
, /* complain_on_overflow */
654 bfd_elf_generic_reloc
, /* special_function */
655 "R_AVR_32_PCREL", /* name */
656 FALSE
, /* partial_inplace */
657 0xffffffff, /* src_mask */
658 0xffffffff, /* dst_mask */
659 TRUE
), /* pcrel_offset */
662 /* Map BFD reloc types to AVR ELF reloc types. */
666 bfd_reloc_code_real_type bfd_reloc_val
;
667 unsigned int elf_reloc_val
;
670 static const struct avr_reloc_map avr_reloc_map
[] =
672 { BFD_RELOC_NONE
, R_AVR_NONE
},
673 { BFD_RELOC_32
, R_AVR_32
},
674 { BFD_RELOC_AVR_7_PCREL
, R_AVR_7_PCREL
},
675 { BFD_RELOC_AVR_13_PCREL
, R_AVR_13_PCREL
},
676 { BFD_RELOC_16
, R_AVR_16
},
677 { BFD_RELOC_AVR_16_PM
, R_AVR_16_PM
},
678 { BFD_RELOC_AVR_LO8_LDI
, R_AVR_LO8_LDI
},
679 { BFD_RELOC_AVR_HI8_LDI
, R_AVR_HI8_LDI
},
680 { BFD_RELOC_AVR_HH8_LDI
, R_AVR_HH8_LDI
},
681 { BFD_RELOC_AVR_MS8_LDI
, R_AVR_MS8_LDI
},
682 { BFD_RELOC_AVR_LO8_LDI_NEG
, R_AVR_LO8_LDI_NEG
},
683 { BFD_RELOC_AVR_HI8_LDI_NEG
, R_AVR_HI8_LDI_NEG
},
684 { BFD_RELOC_AVR_HH8_LDI_NEG
, R_AVR_HH8_LDI_NEG
},
685 { BFD_RELOC_AVR_MS8_LDI_NEG
, R_AVR_MS8_LDI_NEG
},
686 { BFD_RELOC_AVR_LO8_LDI_PM
, R_AVR_LO8_LDI_PM
},
687 { BFD_RELOC_AVR_LO8_LDI_GS
, R_AVR_LO8_LDI_GS
},
688 { BFD_RELOC_AVR_HI8_LDI_PM
, R_AVR_HI8_LDI_PM
},
689 { BFD_RELOC_AVR_HI8_LDI_GS
, R_AVR_HI8_LDI_GS
},
690 { BFD_RELOC_AVR_HH8_LDI_PM
, R_AVR_HH8_LDI_PM
},
691 { BFD_RELOC_AVR_LO8_LDI_PM_NEG
, R_AVR_LO8_LDI_PM_NEG
},
692 { BFD_RELOC_AVR_HI8_LDI_PM_NEG
, R_AVR_HI8_LDI_PM_NEG
},
693 { BFD_RELOC_AVR_HH8_LDI_PM_NEG
, R_AVR_HH8_LDI_PM_NEG
},
694 { BFD_RELOC_AVR_CALL
, R_AVR_CALL
},
695 { BFD_RELOC_AVR_LDI
, R_AVR_LDI
},
696 { BFD_RELOC_AVR_6
, R_AVR_6
},
697 { BFD_RELOC_AVR_6_ADIW
, R_AVR_6_ADIW
},
698 { BFD_RELOC_8
, R_AVR_8
},
699 { BFD_RELOC_AVR_8_LO
, R_AVR_8_LO8
},
700 { BFD_RELOC_AVR_8_HI
, R_AVR_8_HI8
},
701 { BFD_RELOC_AVR_8_HLO
, R_AVR_8_HLO8
},
702 { BFD_RELOC_AVR_DIFF8
, R_AVR_DIFF8
},
703 { BFD_RELOC_AVR_DIFF16
, R_AVR_DIFF16
},
704 { BFD_RELOC_AVR_DIFF32
, R_AVR_DIFF32
},
705 { BFD_RELOC_AVR_LDS_STS_16
, R_AVR_LDS_STS_16
},
706 { BFD_RELOC_AVR_PORT6
, R_AVR_PORT6
},
707 { BFD_RELOC_AVR_PORT5
, R_AVR_PORT5
},
708 { BFD_RELOC_32_PCREL
, R_AVR_32_PCREL
}
711 /* Meant to be filled one day with the wrap around address for the
712 specific device. I.e. should get the value 0x4000 for 16k devices,
713 0x8000 for 32k devices and so on.
715 We initialize it here with a value of 0x1000000 resulting in
716 that we will never suggest a wrap-around jump during relaxation.
717 The logic of the source code later on assumes that in
718 avr_pc_wrap_around one single bit is set. */
719 static bfd_vma avr_pc_wrap_around
= 0x10000000;
721 /* If this variable holds a value different from zero, the linker relaxation
722 machine will try to optimize call/ret sequences by a single jump
723 instruction. This option could be switched off by a linker switch. */
724 static int avr_replace_call_ret_sequences
= 1;
727 /* Per-section relaxation related information for avr. */
729 struct avr_relax_info
731 /* Track the avr property records that apply to this section. */
735 /* Number of records in the list. */
738 /* How many records worth of space have we allocated. */
741 /* The records, only COUNT records are initialised. */
742 struct avr_property_record
*items
;
746 /* Per section data, specialised for avr. */
748 struct elf_avr_section_data
750 /* The standard data must appear first. */
751 struct bfd_elf_section_data elf
;
753 /* Relaxation related information. */
754 struct avr_relax_info relax_info
;
757 /* Possibly initialise avr specific data for new section SEC from ABFD. */
760 elf_avr_new_section_hook (bfd
*abfd
, asection
*sec
)
762 if (!sec
->used_by_bfd
)
764 struct elf_avr_section_data
*sdata
;
765 bfd_size_type amt
= sizeof (*sdata
);
767 sdata
= bfd_zalloc (abfd
, amt
);
770 sec
->used_by_bfd
= sdata
;
773 return _bfd_elf_new_section_hook (abfd
, sec
);
776 /* Return a pointer to the relaxation information for SEC. */
778 static struct avr_relax_info
*
779 get_avr_relax_info (asection
*sec
)
781 struct elf_avr_section_data
*section_data
;
783 /* No info available if no section or if it is an output section. */
784 if (!sec
|| sec
== sec
->output_section
)
787 section_data
= (struct elf_avr_section_data
*) elf_section_data (sec
);
788 return §ion_data
->relax_info
;
791 /* Initialise the per section relaxation information for SEC. */
794 init_avr_relax_info (asection
*sec
)
796 struct avr_relax_info
*relax_info
= get_avr_relax_info (sec
);
798 relax_info
->records
.count
= 0;
799 relax_info
->records
.allocated
= 0;
800 relax_info
->records
.items
= NULL
;
803 /* Initialize an entry in the stub hash table. */
805 static struct bfd_hash_entry
*
806 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
807 struct bfd_hash_table
*table
,
810 /* Allocate the structure if it has not already been allocated by a
814 entry
= bfd_hash_allocate (table
,
815 sizeof (struct elf32_avr_stub_hash_entry
));
820 /* Call the allocation method of the superclass. */
821 entry
= bfd_hash_newfunc (entry
, table
, string
);
824 struct elf32_avr_stub_hash_entry
*hsh
;
826 /* Initialize the local fields. */
827 hsh
= avr_stub_hash_entry (entry
);
828 hsh
->stub_offset
= 0;
829 hsh
->target_value
= 0;
835 /* This function is just a straight passthrough to the real
836 function in linker.c. Its prupose is so that its address
837 can be compared inside the avr_link_hash_table macro. */
839 static struct bfd_hash_entry
*
840 elf32_avr_link_hash_newfunc (struct bfd_hash_entry
* entry
,
841 struct bfd_hash_table
* table
,
844 return _bfd_elf_link_hash_newfunc (entry
, table
, string
);
847 /* Free the derived linker hash table. */
850 elf32_avr_link_hash_table_free (bfd
*obfd
)
852 struct elf32_avr_link_hash_table
*htab
853 = (struct elf32_avr_link_hash_table
*) obfd
->link
.hash
;
855 /* Free the address mapping table. */
856 if (htab
->amt_stub_offsets
!= NULL
)
857 free (htab
->amt_stub_offsets
);
858 if (htab
->amt_destination_addr
!= NULL
)
859 free (htab
->amt_destination_addr
);
861 bfd_hash_table_free (&htab
->bstab
);
862 _bfd_elf_link_hash_table_free (obfd
);
865 /* Create the derived linker hash table. The AVR ELF port uses the derived
866 hash table to keep information specific to the AVR ELF linker (without
867 using static variables). */
869 static struct bfd_link_hash_table
*
870 elf32_avr_link_hash_table_create (bfd
*abfd
)
872 struct elf32_avr_link_hash_table
*htab
;
873 bfd_size_type amt
= sizeof (*htab
);
875 htab
= bfd_zmalloc (amt
);
879 if (!_bfd_elf_link_hash_table_init (&htab
->etab
, abfd
,
880 elf32_avr_link_hash_newfunc
,
881 sizeof (struct elf_link_hash_entry
),
888 /* Init the stub hash table too. */
889 if (!bfd_hash_table_init (&htab
->bstab
, stub_hash_newfunc
,
890 sizeof (struct elf32_avr_stub_hash_entry
)))
892 _bfd_elf_link_hash_table_free (abfd
);
895 htab
->etab
.root
.hash_table_free
= elf32_avr_link_hash_table_free
;
897 return &htab
->etab
.root
;
900 /* Calculates the effective distance of a pc relative jump/call. */
903 avr_relative_distance_considering_wrap_around (unsigned int distance
)
905 unsigned int wrap_around_mask
= avr_pc_wrap_around
- 1;
906 int dist_with_wrap_around
= distance
& wrap_around_mask
;
908 if (dist_with_wrap_around
> ((int) (avr_pc_wrap_around
>> 1)))
909 dist_with_wrap_around
-= avr_pc_wrap_around
;
911 return dist_with_wrap_around
;
915 static reloc_howto_type
*
916 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
917 bfd_reloc_code_real_type code
)
922 i
< sizeof (avr_reloc_map
) / sizeof (struct avr_reloc_map
);
924 if (avr_reloc_map
[i
].bfd_reloc_val
== code
)
925 return &elf_avr_howto_table
[avr_reloc_map
[i
].elf_reloc_val
];
930 static reloc_howto_type
*
931 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
937 i
< sizeof (elf_avr_howto_table
) / sizeof (elf_avr_howto_table
[0]);
939 if (elf_avr_howto_table
[i
].name
!= NULL
940 && strcasecmp (elf_avr_howto_table
[i
].name
, r_name
) == 0)
941 return &elf_avr_howto_table
[i
];
946 /* Set the howto pointer for an AVR ELF reloc. */
949 avr_info_to_howto_rela (bfd
*abfd ATTRIBUTE_UNUSED
,
951 Elf_Internal_Rela
*dst
)
955 r_type
= ELF32_R_TYPE (dst
->r_info
);
956 if (r_type
>= (unsigned int) R_AVR_max
)
958 /* xgettext:c-format */
959 _bfd_error_handler (_("%B: invalid AVR reloc number: %d"), abfd
, r_type
);
962 cache_ptr
->howto
= &elf_avr_howto_table
[r_type
];
966 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation
)
968 return (relocation
>= 0x020000);
971 /* Returns the address of the corresponding stub if there is one.
972 Returns otherwise an address above 0x020000. This function
973 could also be used, if there is no knowledge on the section where
974 the destination is found. */
977 avr_get_stub_addr (bfd_vma srel
,
978 struct elf32_avr_link_hash_table
*htab
)
981 bfd_vma stub_sec_addr
=
982 (htab
->stub_sec
->output_section
->vma
+
983 htab
->stub_sec
->output_offset
);
985 for (sindex
= 0; sindex
< htab
->amt_max_entry_cnt
; sindex
++)
986 if (htab
->amt_destination_addr
[sindex
] == srel
)
987 return htab
->amt_stub_offsets
[sindex
] + stub_sec_addr
;
989 /* Return an address that could not be reached by 16 bit relocs. */
993 /* Perform a diff relocation. Nothing to do, as the difference value is already
994 written into the section's contents. */
996 static bfd_reloc_status_type
997 bfd_elf_avr_diff_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
998 arelent
*reloc_entry ATTRIBUTE_UNUSED
,
999 asymbol
*symbol ATTRIBUTE_UNUSED
,
1000 void *data ATTRIBUTE_UNUSED
,
1001 asection
*input_section ATTRIBUTE_UNUSED
,
1002 bfd
*output_bfd ATTRIBUTE_UNUSED
,
1003 char **error_message ATTRIBUTE_UNUSED
)
1005 return bfd_reloc_ok
;
1009 /* Perform a single relocation. By default we use the standard BFD
1010 routines, but a few relocs, we have to do them ourselves. */
1012 static bfd_reloc_status_type
1013 avr_final_link_relocate (reloc_howto_type
* howto
,
1015 asection
* input_section
,
1016 bfd_byte
* contents
,
1017 Elf_Internal_Rela
* rel
,
1019 struct elf32_avr_link_hash_table
* htab
)
1021 bfd_reloc_status_type r
= bfd_reloc_ok
;
1023 bfd_signed_vma srel
;
1024 bfd_signed_vma reloc_addr
;
1025 bfd_boolean use_stubs
= FALSE
;
1026 /* Usually is 0, unless we are generating code for a bootloader. */
1027 bfd_signed_vma base_addr
= htab
->vector_base
;
1029 /* Absolute addr of the reloc in the final excecutable. */
1030 reloc_addr
= rel
->r_offset
+ input_section
->output_section
->vma
1031 + input_section
->output_offset
;
1033 switch (howto
->type
)
1036 contents
+= rel
->r_offset
;
1037 srel
= (bfd_signed_vma
) relocation
;
1038 srel
+= rel
->r_addend
;
1039 srel
-= rel
->r_offset
;
1040 srel
-= 2; /* Branch instructions add 2 to the PC... */
1041 srel
-= (input_section
->output_section
->vma
+
1042 input_section
->output_offset
);
1045 return bfd_reloc_outofrange
;
1046 if (srel
> ((1 << 7) - 1) || (srel
< - (1 << 7)))
1047 return bfd_reloc_overflow
;
1048 x
= bfd_get_16 (input_bfd
, contents
);
1049 x
= (x
& 0xfc07) | (((srel
>> 1) << 3) & 0x3f8);
1050 bfd_put_16 (input_bfd
, x
, contents
);
1053 case R_AVR_13_PCREL
:
1054 contents
+= rel
->r_offset
;
1055 srel
= (bfd_signed_vma
) relocation
;
1056 srel
+= rel
->r_addend
;
1057 srel
-= rel
->r_offset
;
1058 srel
-= 2; /* Branch instructions add 2 to the PC... */
1059 srel
-= (input_section
->output_section
->vma
+
1060 input_section
->output_offset
);
1063 return bfd_reloc_outofrange
;
1065 srel
= avr_relative_distance_considering_wrap_around (srel
);
1067 /* AVR addresses commands as words. */
1070 /* Check for overflow. */
1071 if (srel
< -2048 || srel
> 2047)
1073 /* Relative distance is too large. */
1075 /* Always apply WRAPAROUND for avr2, avr25, and avr4. */
1076 switch (bfd_get_mach (input_bfd
))
1079 case bfd_mach_avr25
:
1084 return bfd_reloc_overflow
;
1088 x
= bfd_get_16 (input_bfd
, contents
);
1089 x
= (x
& 0xf000) | (srel
& 0xfff);
1090 bfd_put_16 (input_bfd
, x
, contents
);
1094 contents
+= rel
->r_offset
;
1095 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1096 x
= bfd_get_16 (input_bfd
, contents
);
1097 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1098 bfd_put_16 (input_bfd
, x
, contents
);
1102 contents
+= rel
->r_offset
;
1103 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1104 if (((srel
> 0) && (srel
& 0xffff) > 255)
1105 || ((srel
< 0) && ((-srel
) & 0xffff) > 128))
1106 /* Remove offset for data/eeprom section. */
1107 return bfd_reloc_overflow
;
1109 x
= bfd_get_16 (input_bfd
, contents
);
1110 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1111 bfd_put_16 (input_bfd
, x
, contents
);
1115 contents
+= rel
->r_offset
;
1116 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1117 if (((srel
& 0xffff) > 63) || (srel
< 0))
1118 /* Remove offset for data/eeprom section. */
1119 return bfd_reloc_overflow
;
1120 x
= bfd_get_16 (input_bfd
, contents
);
1121 x
= (x
& 0xd3f8) | ((srel
& 7) | ((srel
& (3 << 3)) << 7)
1122 | ((srel
& (1 << 5)) << 8));
1123 bfd_put_16 (input_bfd
, x
, contents
);
1127 contents
+= rel
->r_offset
;
1128 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1129 if (((srel
& 0xffff) > 63) || (srel
< 0))
1130 /* Remove offset for data/eeprom section. */
1131 return bfd_reloc_overflow
;
1132 x
= bfd_get_16 (input_bfd
, contents
);
1133 x
= (x
& 0xff30) | (srel
& 0xf) | ((srel
& 0x30) << 2);
1134 bfd_put_16 (input_bfd
, x
, contents
);
1138 contents
+= rel
->r_offset
;
1139 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1140 srel
= (srel
>> 8) & 0xff;
1141 x
= bfd_get_16 (input_bfd
, contents
);
1142 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1143 bfd_put_16 (input_bfd
, x
, contents
);
1147 contents
+= rel
->r_offset
;
1148 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1149 srel
= (srel
>> 16) & 0xff;
1150 x
= bfd_get_16 (input_bfd
, contents
);
1151 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1152 bfd_put_16 (input_bfd
, x
, contents
);
1156 contents
+= rel
->r_offset
;
1157 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1158 srel
= (srel
>> 24) & 0xff;
1159 x
= bfd_get_16 (input_bfd
, contents
);
1160 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1161 bfd_put_16 (input_bfd
, x
, contents
);
1164 case R_AVR_LO8_LDI_NEG
:
1165 contents
+= rel
->r_offset
;
1166 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1168 x
= bfd_get_16 (input_bfd
, contents
);
1169 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1170 bfd_put_16 (input_bfd
, x
, contents
);
1173 case R_AVR_HI8_LDI_NEG
:
1174 contents
+= rel
->r_offset
;
1175 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1177 srel
= (srel
>> 8) & 0xff;
1178 x
= bfd_get_16 (input_bfd
, contents
);
1179 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1180 bfd_put_16 (input_bfd
, x
, contents
);
1183 case R_AVR_HH8_LDI_NEG
:
1184 contents
+= rel
->r_offset
;
1185 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1187 srel
= (srel
>> 16) & 0xff;
1188 x
= bfd_get_16 (input_bfd
, contents
);
1189 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1190 bfd_put_16 (input_bfd
, x
, contents
);
1193 case R_AVR_MS8_LDI_NEG
:
1194 contents
+= rel
->r_offset
;
1195 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1197 srel
= (srel
>> 24) & 0xff;
1198 x
= bfd_get_16 (input_bfd
, contents
);
1199 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1200 bfd_put_16 (input_bfd
, x
, contents
);
1203 case R_AVR_LO8_LDI_GS
:
1204 use_stubs
= (!htab
->no_stubs
);
1206 case R_AVR_LO8_LDI_PM
:
1207 contents
+= rel
->r_offset
;
1208 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1211 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1213 bfd_vma old_srel
= srel
;
1215 /* We need to use the address of the stub instead. */
1216 srel
= avr_get_stub_addr (srel
, htab
);
1218 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1219 "reloc at address 0x%x.\n",
1220 (unsigned int) srel
,
1221 (unsigned int) old_srel
,
1222 (unsigned int) reloc_addr
);
1224 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1225 return bfd_reloc_outofrange
;
1229 return bfd_reloc_outofrange
;
1231 x
= bfd_get_16 (input_bfd
, contents
);
1232 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1233 bfd_put_16 (input_bfd
, x
, contents
);
1236 case R_AVR_HI8_LDI_GS
:
1237 use_stubs
= (!htab
->no_stubs
);
1239 case R_AVR_HI8_LDI_PM
:
1240 contents
+= rel
->r_offset
;
1241 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1244 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1246 bfd_vma old_srel
= srel
;
1248 /* We need to use the address of the stub instead. */
1249 srel
= avr_get_stub_addr (srel
, htab
);
1251 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1252 "reloc at address 0x%x.\n",
1253 (unsigned int) srel
,
1254 (unsigned int) old_srel
,
1255 (unsigned int) reloc_addr
);
1257 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1258 return bfd_reloc_outofrange
;
1262 return bfd_reloc_outofrange
;
1264 srel
= (srel
>> 8) & 0xff;
1265 x
= bfd_get_16 (input_bfd
, contents
);
1266 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1267 bfd_put_16 (input_bfd
, x
, contents
);
1270 case R_AVR_HH8_LDI_PM
:
1271 contents
+= rel
->r_offset
;
1272 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1274 return bfd_reloc_outofrange
;
1276 srel
= (srel
>> 16) & 0xff;
1277 x
= bfd_get_16 (input_bfd
, contents
);
1278 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1279 bfd_put_16 (input_bfd
, x
, contents
);
1282 case R_AVR_LO8_LDI_PM_NEG
:
1283 contents
+= rel
->r_offset
;
1284 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1287 return bfd_reloc_outofrange
;
1289 x
= bfd_get_16 (input_bfd
, contents
);
1290 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1291 bfd_put_16 (input_bfd
, x
, contents
);
1294 case R_AVR_HI8_LDI_PM_NEG
:
1295 contents
+= rel
->r_offset
;
1296 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1299 return bfd_reloc_outofrange
;
1301 srel
= (srel
>> 8) & 0xff;
1302 x
= bfd_get_16 (input_bfd
, contents
);
1303 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1304 bfd_put_16 (input_bfd
, x
, contents
);
1307 case R_AVR_HH8_LDI_PM_NEG
:
1308 contents
+= rel
->r_offset
;
1309 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1312 return bfd_reloc_outofrange
;
1314 srel
= (srel
>> 16) & 0xff;
1315 x
= bfd_get_16 (input_bfd
, contents
);
1316 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1317 bfd_put_16 (input_bfd
, x
, contents
);
1321 contents
+= rel
->r_offset
;
1322 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1324 return bfd_reloc_outofrange
;
1326 x
= bfd_get_16 (input_bfd
, contents
);
1327 x
|= ((srel
& 0x10000) | ((srel
<< 3) & 0x1f00000)) >> 16;
1328 bfd_put_16 (input_bfd
, x
, contents
);
1329 bfd_put_16 (input_bfd
, (bfd_vma
) srel
& 0xffff, contents
+2);
1333 use_stubs
= (!htab
->no_stubs
);
1334 contents
+= rel
->r_offset
;
1335 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1338 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1340 bfd_vma old_srel
= srel
;
1342 /* We need to use the address of the stub instead. */
1343 srel
= avr_get_stub_addr (srel
,htab
);
1345 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1346 "reloc at address 0x%x.\n",
1347 (unsigned int) srel
,
1348 (unsigned int) old_srel
,
1349 (unsigned int) reloc_addr
);
1351 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1352 return bfd_reloc_outofrange
;
1356 return bfd_reloc_outofrange
;
1358 bfd_put_16 (input_bfd
, (bfd_vma
) srel
&0x00ffff, contents
);
1364 /* Nothing to do here, as contents already contains the diff value. */
1368 case R_AVR_LDS_STS_16
:
1369 contents
+= rel
->r_offset
;
1370 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1371 if ((srel
& 0xFFFF) < 0x40 || (srel
& 0xFFFF) > 0xbf)
1372 return bfd_reloc_outofrange
;
1374 x
= bfd_get_16 (input_bfd
, contents
);
1375 x
|= (srel
& 0x0f) | ((srel
& 0x30) << 5) | ((srel
& 0x40) << 2);
1376 bfd_put_16 (input_bfd
, x
, contents
);
1380 contents
+= rel
->r_offset
;
1381 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1382 if ((srel
& 0xffff) > 0x3f)
1383 return bfd_reloc_outofrange
;
1384 x
= bfd_get_16 (input_bfd
, contents
);
1385 x
= (x
& 0xf9f0) | ((srel
& 0x30) << 5) | (srel
& 0x0f);
1386 bfd_put_16 (input_bfd
, x
, contents
);
1390 contents
+= rel
->r_offset
;
1391 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1392 if ((srel
& 0xffff) > 0x1f)
1393 return bfd_reloc_outofrange
;
1394 x
= bfd_get_16 (input_bfd
, contents
);
1395 x
= (x
& 0xff07) | ((srel
& 0x1f) << 3);
1396 bfd_put_16 (input_bfd
, x
, contents
);
1400 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1401 contents
, rel
->r_offset
,
1402 relocation
, rel
->r_addend
);
1408 /* Relocate an AVR ELF section. */
1411 elf32_avr_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1412 struct bfd_link_info
*info
,
1414 asection
*input_section
,
1416 Elf_Internal_Rela
*relocs
,
1417 Elf_Internal_Sym
*local_syms
,
1418 asection
**local_sections
)
1420 Elf_Internal_Shdr
* symtab_hdr
;
1421 struct elf_link_hash_entry
** sym_hashes
;
1422 Elf_Internal_Rela
* rel
;
1423 Elf_Internal_Rela
* relend
;
1424 struct elf32_avr_link_hash_table
* htab
= avr_link_hash_table (info
);
1429 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
1430 sym_hashes
= elf_sym_hashes (input_bfd
);
1431 relend
= relocs
+ input_section
->reloc_count
;
1433 for (rel
= relocs
; rel
< relend
; rel
++)
1435 reloc_howto_type
* howto
;
1436 unsigned long r_symndx
;
1437 Elf_Internal_Sym
* sym
;
1439 struct elf_link_hash_entry
* h
;
1441 bfd_reloc_status_type r
;
1445 r_type
= ELF32_R_TYPE (rel
->r_info
);
1446 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1447 howto
= elf_avr_howto_table
+ r_type
;
1452 if (r_symndx
< symtab_hdr
->sh_info
)
1454 sym
= local_syms
+ r_symndx
;
1455 sec
= local_sections
[r_symndx
];
1456 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1458 name
= bfd_elf_string_from_elf_section
1459 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1460 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1464 bfd_boolean unresolved_reloc
, warned
, ignored
;
1466 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1467 r_symndx
, symtab_hdr
, sym_hashes
,
1469 unresolved_reloc
, warned
, ignored
);
1471 name
= h
->root
.root
.string
;
1474 if (sec
!= NULL
&& discarded_section (sec
))
1475 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1476 rel
, 1, relend
, howto
, 0, contents
);
1478 if (bfd_link_relocatable (info
))
1481 r
= avr_final_link_relocate (howto
, input_bfd
, input_section
,
1482 contents
, rel
, relocation
, htab
);
1484 if (r
!= bfd_reloc_ok
)
1486 const char * msg
= (const char *) NULL
;
1490 case bfd_reloc_overflow
:
1491 (*info
->callbacks
->reloc_overflow
)
1492 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
1493 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
1496 case bfd_reloc_undefined
:
1497 (*info
->callbacks
->undefined_symbol
)
1498 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1501 case bfd_reloc_outofrange
:
1502 msg
= _("internal error: out of range error");
1505 case bfd_reloc_notsupported
:
1506 msg
= _("internal error: unsupported relocation error");
1509 case bfd_reloc_dangerous
:
1510 msg
= _("internal error: dangerous relocation");
1514 msg
= _("internal error: unknown error");
1519 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
1520 input_section
, rel
->r_offset
);
1527 /* The final processing done just before writing out a AVR ELF object
1528 file. This gets the AVR architecture right based on the machine
1532 bfd_elf_avr_final_write_processing (bfd
*abfd
,
1533 bfd_boolean linker ATTRIBUTE_UNUSED
)
1537 switch (bfd_get_mach (abfd
))
1541 val
= E_AVR_MACH_AVR2
;
1545 val
= E_AVR_MACH_AVR1
;
1548 case bfd_mach_avr25
:
1549 val
= E_AVR_MACH_AVR25
;
1553 val
= E_AVR_MACH_AVR3
;
1556 case bfd_mach_avr31
:
1557 val
= E_AVR_MACH_AVR31
;
1560 case bfd_mach_avr35
:
1561 val
= E_AVR_MACH_AVR35
;
1565 val
= E_AVR_MACH_AVR4
;
1569 val
= E_AVR_MACH_AVR5
;
1572 case bfd_mach_avr51
:
1573 val
= E_AVR_MACH_AVR51
;
1577 val
= E_AVR_MACH_AVR6
;
1580 case bfd_mach_avrxmega1
:
1581 val
= E_AVR_MACH_XMEGA1
;
1584 case bfd_mach_avrxmega2
:
1585 val
= E_AVR_MACH_XMEGA2
;
1588 case bfd_mach_avrxmega3
:
1589 val
= E_AVR_MACH_XMEGA3
;
1592 case bfd_mach_avrxmega4
:
1593 val
= E_AVR_MACH_XMEGA4
;
1596 case bfd_mach_avrxmega5
:
1597 val
= E_AVR_MACH_XMEGA5
;
1600 case bfd_mach_avrxmega6
:
1601 val
= E_AVR_MACH_XMEGA6
;
1604 case bfd_mach_avrxmega7
:
1605 val
= E_AVR_MACH_XMEGA7
;
1608 case bfd_mach_avrtiny
:
1609 val
= E_AVR_MACH_AVRTINY
;
1613 elf_elfheader (abfd
)->e_machine
= EM_AVR
;
1614 elf_elfheader (abfd
)->e_flags
&= ~ EF_AVR_MACH
;
1615 elf_elfheader (abfd
)->e_flags
|= val
;
1618 /* Set the right machine number. */
1621 elf32_avr_object_p (bfd
*abfd
)
1623 unsigned int e_set
= bfd_mach_avr2
;
1625 if (elf_elfheader (abfd
)->e_machine
== EM_AVR
1626 || elf_elfheader (abfd
)->e_machine
== EM_AVR_OLD
)
1628 int e_mach
= elf_elfheader (abfd
)->e_flags
& EF_AVR_MACH
;
1633 case E_AVR_MACH_AVR2
:
1634 e_set
= bfd_mach_avr2
;
1637 case E_AVR_MACH_AVR1
:
1638 e_set
= bfd_mach_avr1
;
1641 case E_AVR_MACH_AVR25
:
1642 e_set
= bfd_mach_avr25
;
1645 case E_AVR_MACH_AVR3
:
1646 e_set
= bfd_mach_avr3
;
1649 case E_AVR_MACH_AVR31
:
1650 e_set
= bfd_mach_avr31
;
1653 case E_AVR_MACH_AVR35
:
1654 e_set
= bfd_mach_avr35
;
1657 case E_AVR_MACH_AVR4
:
1658 e_set
= bfd_mach_avr4
;
1661 case E_AVR_MACH_AVR5
:
1662 e_set
= bfd_mach_avr5
;
1665 case E_AVR_MACH_AVR51
:
1666 e_set
= bfd_mach_avr51
;
1669 case E_AVR_MACH_AVR6
:
1670 e_set
= bfd_mach_avr6
;
1673 case E_AVR_MACH_XMEGA1
:
1674 e_set
= bfd_mach_avrxmega1
;
1677 case E_AVR_MACH_XMEGA2
:
1678 e_set
= bfd_mach_avrxmega2
;
1681 case E_AVR_MACH_XMEGA3
:
1682 e_set
= bfd_mach_avrxmega3
;
1685 case E_AVR_MACH_XMEGA4
:
1686 e_set
= bfd_mach_avrxmega4
;
1689 case E_AVR_MACH_XMEGA5
:
1690 e_set
= bfd_mach_avrxmega5
;
1693 case E_AVR_MACH_XMEGA6
:
1694 e_set
= bfd_mach_avrxmega6
;
1697 case E_AVR_MACH_XMEGA7
:
1698 e_set
= bfd_mach_avrxmega7
;
1701 case E_AVR_MACH_AVRTINY
:
1702 e_set
= bfd_mach_avrtiny
;
1706 return bfd_default_set_arch_mach (abfd
, bfd_arch_avr
,
1710 /* Returns whether the relocation type passed is a diff reloc. */
1713 elf32_avr_is_diff_reloc (Elf_Internal_Rela
*irel
)
1715 return (ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF8
1716 ||ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF16
1717 || ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF32
);
1720 /* Reduce the diff value written in the section by count if the shrinked
1721 insn address happens to fall between the two symbols for which this
1722 diff reloc was emitted. */
1725 elf32_avr_adjust_diff_reloc_value (bfd
*abfd
,
1726 struct bfd_section
*isec
,
1727 Elf_Internal_Rela
*irel
,
1729 bfd_vma shrinked_insn_address
,
1732 unsigned char *reloc_contents
= NULL
;
1733 unsigned char *isec_contents
= elf_section_data (isec
)->this_hdr
.contents
;
1734 if (isec_contents
== NULL
)
1736 if (! bfd_malloc_and_get_section (abfd
, isec
, &isec_contents
))
1739 elf_section_data (isec
)->this_hdr
.contents
= isec_contents
;
1742 reloc_contents
= isec_contents
+ irel
->r_offset
;
1744 /* Read value written in object file. */
1745 bfd_signed_vma x
= 0;
1746 switch (ELF32_R_TYPE (irel
->r_info
))
1750 x
= bfd_get_signed_8 (abfd
, reloc_contents
);
1755 x
= bfd_get_signed_16 (abfd
, reloc_contents
);
1760 x
= bfd_get_signed_32 (abfd
, reloc_contents
);
1769 /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
1770 into the object file at the reloc offset. sym2's logical value is
1771 symval (<start_of_section>) + reloc addend. Compute the start and end
1772 addresses and check if the shrinked insn falls between sym1 and sym2. */
1774 bfd_vma sym2_address
= symval
+ irel
->r_addend
;
1775 bfd_vma sym1_address
= sym2_address
- x
;
1777 /* Don't assume sym2 is bigger than sym1 - the difference
1778 could be negative. Compute start and end addresses, and
1779 use those to see if they span shrinked_insn_address. */
1781 bfd_vma start_address
= sym1_address
< sym2_address
1782 ? sym1_address
: sym2_address
;
1783 bfd_vma end_address
= sym1_address
> sym2_address
1784 ? sym1_address
: sym2_address
;
1787 if (shrinked_insn_address
>= start_address
1788 && shrinked_insn_address
<= end_address
)
1790 /* Reduce the diff value by count bytes and write it back into section
1792 bfd_signed_vma new_diff
= x
< 0 ? x
+ count
: x
- count
;
1794 switch (ELF32_R_TYPE (irel
->r_info
))
1798 bfd_put_signed_8 (abfd
, new_diff
, reloc_contents
);
1803 bfd_put_signed_16 (abfd
, new_diff
& 0xFFFF, reloc_contents
);
1808 bfd_put_signed_32 (abfd
, new_diff
& 0xFFFFFFFF, reloc_contents
);
1821 elf32_avr_adjust_reloc_if_spans_insn (bfd
*abfd
,
1823 Elf_Internal_Rela
*irel
, bfd_vma symval
,
1824 bfd_vma shrinked_insn_address
,
1825 bfd_vma shrink_boundary
,
1829 if (elf32_avr_is_diff_reloc (irel
))
1831 elf32_avr_adjust_diff_reloc_value (abfd
, isec
, irel
,
1833 shrinked_insn_address
,
1838 bfd_vma reloc_value
= symval
+ irel
->r_addend
;
1839 bfd_boolean addend_within_shrink_boundary
=
1840 (reloc_value
<= shrink_boundary
);
1842 bfd_boolean reloc_spans_insn
=
1843 (symval
<= shrinked_insn_address
1844 && reloc_value
> shrinked_insn_address
1845 && addend_within_shrink_boundary
);
1847 if (! reloc_spans_insn
)
1850 irel
->r_addend
-= count
;
1853 printf ("Relocation's addend needed to be fixed \n");
1858 avr_should_move_sym (symvalue symval
,
1861 bfd_boolean did_pad
)
1863 bfd_boolean sym_within_boundary
=
1864 did_pad
? symval
< end
: symval
<= end
;
1865 return (symval
> start
&& sym_within_boundary
);
1869 avr_should_reduce_sym_size (symvalue symval
,
1873 bfd_boolean did_pad
)
1875 bfd_boolean sym_end_within_boundary
=
1876 did_pad
? symend
< end
: symend
<= end
;
1877 return (symval
<= start
&& symend
> start
&& sym_end_within_boundary
);
1881 avr_should_increase_sym_size (symvalue symval
,
1885 bfd_boolean did_pad
)
1887 return avr_should_move_sym (symval
, start
, end
, did_pad
)
1888 && symend
>= end
&& did_pad
;
1891 /* Delete some bytes from a section while changing the size of an instruction.
1892 The parameter "addr" denotes the section-relative offset pointing just
1893 behind the shrinked instruction. "addr+count" point at the first
1894 byte just behind the original unshrinked instruction. If delete_shrinks_insn
1895 is FALSE, we are deleting redundant padding bytes from relax_info prop
1896 record handling. In that case, addr is section-relative offset of start
1897 of padding, and count is the number of padding bytes to delete. */
1900 elf32_avr_relax_delete_bytes (bfd
*abfd
,
1904 bfd_boolean delete_shrinks_insn
)
1906 Elf_Internal_Shdr
*symtab_hdr
;
1907 unsigned int sec_shndx
;
1909 Elf_Internal_Rela
*irel
, *irelend
;
1910 Elf_Internal_Sym
*isym
;
1911 Elf_Internal_Sym
*isymbuf
= NULL
;
1913 struct elf_link_hash_entry
**sym_hashes
;
1914 struct elf_link_hash_entry
**end_hashes
;
1915 unsigned int symcount
;
1916 struct avr_relax_info
*relax_info
;
1917 struct avr_property_record
*prop_record
= NULL
;
1918 bfd_boolean did_shrink
= FALSE
;
1919 bfd_boolean did_pad
= FALSE
;
1921 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1922 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1923 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1924 relax_info
= get_avr_relax_info (sec
);
1928 if (relax_info
->records
.count
> 0)
1930 /* There should be no property record within the range of deleted
1931 bytes, however, there might be a property record for ADDR, this is
1932 how we handle alignment directives.
1933 Find the next (if any) property record after the deleted bytes. */
1936 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
1938 bfd_vma offset
= relax_info
->records
.items
[i
].offset
;
1940 BFD_ASSERT (offset
<= addr
|| offset
>= (addr
+ count
));
1941 if (offset
>= (addr
+ count
))
1943 prop_record
= &relax_info
->records
.items
[i
];
1950 irel
= elf_section_data (sec
)->relocs
;
1951 irelend
= irel
+ sec
->reloc_count
;
1953 /* Actually delete the bytes. */
1954 if (toaddr
- addr
- count
> 0)
1956 memmove (contents
+ addr
, contents
+ addr
+ count
,
1957 (size_t) (toaddr
- addr
- count
));
1960 if (prop_record
== NULL
)
1967 /* Use the property record to fill in the bytes we've opened up. */
1969 switch (prop_record
->type
)
1971 case RECORD_ORG_AND_FILL
:
1972 fill
= prop_record
->data
.org
.fill
;
1976 case RECORD_ALIGN_AND_FILL
:
1977 fill
= prop_record
->data
.align
.fill
;
1980 prop_record
->data
.align
.preceding_deleted
+= count
;
1983 /* If toaddr == (addr + count), then we didn't delete anything, yet
1984 we fill count bytes backwards from toaddr. This is still ok - we
1985 end up overwriting the bytes we would have deleted. We just need
1986 to remember we didn't delete anything i.e. don't set did_shrink,
1987 so that we don't corrupt reloc offsets or symbol values.*/
1988 memset (contents
+ toaddr
- count
, fill
, count
);
1995 /* Adjust all the reloc addresses. */
1996 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1998 bfd_vma old_reloc_address
;
2000 old_reloc_address
= (sec
->output_section
->vma
2001 + sec
->output_offset
+ irel
->r_offset
);
2003 /* Get the new reloc address. */
2004 if ((irel
->r_offset
> addr
2005 && irel
->r_offset
< toaddr
))
2008 printf ("Relocation at address 0x%x needs to be moved.\n"
2009 "Old section offset: 0x%x, New section offset: 0x%x \n",
2010 (unsigned int) old_reloc_address
,
2011 (unsigned int) irel
->r_offset
,
2012 (unsigned int) ((irel
->r_offset
) - count
));
2014 irel
->r_offset
-= count
;
2019 /* The reloc's own addresses are now ok. However, we need to readjust
2020 the reloc's addend, i.e. the reloc's value if two conditions are met:
2021 1.) the reloc is relative to a symbol in this section that
2022 is located in front of the shrinked instruction
2023 2.) symbol plus addend end up behind the shrinked instruction.
2025 The most common case where this happens are relocs relative to
2026 the section-start symbol.
2028 This step needs to be done for all of the sections of the bfd. */
2031 struct bfd_section
*isec
;
2033 for (isec
= abfd
->sections
; isec
; isec
= isec
->next
)
2036 bfd_vma shrinked_insn_address
;
2038 if (isec
->reloc_count
== 0)
2041 shrinked_insn_address
= (sec
->output_section
->vma
2042 + sec
->output_offset
+ addr
);
2043 if (delete_shrinks_insn
)
2044 shrinked_insn_address
-= count
;
2046 irel
= elf_section_data (isec
)->relocs
;
2047 /* PR 12161: Read in the relocs for this section if necessary. */
2049 irel
= _bfd_elf_link_read_relocs (abfd
, isec
, NULL
, NULL
, TRUE
);
2051 for (irelend
= irel
+ isec
->reloc_count
;
2055 /* Read this BFD's local symbols if we haven't done
2057 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2059 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2060 if (isymbuf
== NULL
)
2061 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2062 symtab_hdr
->sh_info
, 0,
2064 if (isymbuf
== NULL
)
2068 /* Get the value of the symbol referred to by the reloc. */
2069 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2071 /* A local symbol. */
2074 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
2075 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2076 symval
= isym
->st_value
;
2077 /* If the reloc is absolute, it will not have
2078 a symbol or section associated with it. */
2081 /* If there is an alignment boundary, we only need to
2082 adjust addends that end up below the boundary. */
2083 bfd_vma shrink_boundary
= (toaddr
2084 + sec
->output_section
->vma
2085 + sec
->output_offset
);
2087 symval
+= sym_sec
->output_section
->vma
2088 + sym_sec
->output_offset
;
2091 printf ("Checking if the relocation's "
2092 "addend needs corrections.\n"
2093 "Address of anchor symbol: 0x%x \n"
2094 "Address of relocation target: 0x%x \n"
2095 "Address of relaxed insn: 0x%x \n",
2096 (unsigned int) symval
,
2097 (unsigned int) (symval
+ irel
->r_addend
),
2098 (unsigned int) shrinked_insn_address
);
2100 elf32_avr_adjust_reloc_if_spans_insn (abfd
, isec
, irel
,
2102 shrinked_insn_address
,
2106 /* else...Reference symbol is absolute. No adjustment needed. */
2108 /* else...Reference symbol is extern. No need for adjusting
2114 /* Adjust the local symbols defined in this section. */
2115 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2116 /* Fix PR 9841, there may be no local symbols. */
2119 Elf_Internal_Sym
*isymend
;
2121 isymend
= isym
+ symtab_hdr
->sh_info
;
2122 for (; isym
< isymend
; isym
++)
2124 if (isym
->st_shndx
== sec_shndx
)
2126 symvalue symval
= isym
->st_value
;
2127 symvalue symend
= symval
+ isym
->st_size
;
2128 if (avr_should_reduce_sym_size (symval
, symend
,
2129 addr
, toaddr
, did_pad
))
2131 /* If this assert fires then we have a symbol that ends
2132 part way through an instruction. Does that make
2134 BFD_ASSERT (isym
->st_value
+ isym
->st_size
>= addr
+ count
);
2135 isym
->st_size
-= count
;
2137 else if (avr_should_increase_sym_size (symval
, symend
,
2138 addr
, toaddr
, did_pad
))
2139 isym
->st_size
+= count
;
2141 if (avr_should_move_sym (symval
, addr
, toaddr
, did_pad
))
2142 isym
->st_value
-= count
;
2147 /* Now adjust the global symbols defined in this section. */
2148 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2149 - symtab_hdr
->sh_info
);
2150 sym_hashes
= elf_sym_hashes (abfd
);
2151 end_hashes
= sym_hashes
+ symcount
;
2152 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2154 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
2155 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2156 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
2157 && sym_hash
->root
.u
.def
.section
== sec
)
2159 symvalue symval
= sym_hash
->root
.u
.def
.value
;
2160 symvalue symend
= symval
+ sym_hash
->size
;
2162 if (avr_should_reduce_sym_size (symval
, symend
,
2163 addr
, toaddr
, did_pad
))
2165 /* If this assert fires then we have a symbol that ends
2166 part way through an instruction. Does that make
2168 BFD_ASSERT (symend
>= addr
+ count
);
2169 sym_hash
->size
-= count
;
2171 else if (avr_should_increase_sym_size (symval
, symend
,
2172 addr
, toaddr
, did_pad
))
2173 sym_hash
->size
+= count
;
2175 if (avr_should_move_sym (symval
, addr
, toaddr
, did_pad
))
2176 sym_hash
->root
.u
.def
.value
-= count
;
2183 static Elf_Internal_Sym
*
2184 retrieve_local_syms (bfd
*input_bfd
)
2186 Elf_Internal_Shdr
*symtab_hdr
;
2187 Elf_Internal_Sym
*isymbuf
;
2190 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2191 locsymcount
= symtab_hdr
->sh_info
;
2193 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2194 if (isymbuf
== NULL
&& locsymcount
!= 0)
2195 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
2198 /* Save the symbols for this input file so they won't be read again. */
2199 if (isymbuf
&& isymbuf
!= (Elf_Internal_Sym
*) symtab_hdr
->contents
)
2200 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2205 /* Get the input section for a given symbol index.
2207 . a section symbol, return the section;
2208 . a common symbol, return the common section;
2209 . an undefined symbol, return the undefined section;
2210 . an indirect symbol, follow the links;
2211 . an absolute value, return the absolute section. */
2214 get_elf_r_symndx_section (bfd
*abfd
, unsigned long r_symndx
)
2216 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2217 asection
*target_sec
= NULL
;
2218 if (r_symndx
< symtab_hdr
->sh_info
)
2220 Elf_Internal_Sym
*isymbuf
;
2221 unsigned int section_index
;
2223 isymbuf
= retrieve_local_syms (abfd
);
2224 section_index
= isymbuf
[r_symndx
].st_shndx
;
2226 if (section_index
== SHN_UNDEF
)
2227 target_sec
= bfd_und_section_ptr
;
2228 else if (section_index
== SHN_ABS
)
2229 target_sec
= bfd_abs_section_ptr
;
2230 else if (section_index
== SHN_COMMON
)
2231 target_sec
= bfd_com_section_ptr
;
2233 target_sec
= bfd_section_from_elf_index (abfd
, section_index
);
2237 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
2238 struct elf_link_hash_entry
*h
= elf_sym_hashes (abfd
)[indx
];
2240 while (h
->root
.type
== bfd_link_hash_indirect
2241 || h
->root
.type
== bfd_link_hash_warning
)
2242 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2244 switch (h
->root
.type
)
2246 case bfd_link_hash_defined
:
2247 case bfd_link_hash_defweak
:
2248 target_sec
= h
->root
.u
.def
.section
;
2250 case bfd_link_hash_common
:
2251 target_sec
= bfd_com_section_ptr
;
2253 case bfd_link_hash_undefined
:
2254 case bfd_link_hash_undefweak
:
2255 target_sec
= bfd_und_section_ptr
;
2257 default: /* New indirect warning. */
2258 target_sec
= bfd_und_section_ptr
;
2265 /* Get the section-relative offset for a symbol number. */
2268 get_elf_r_symndx_offset (bfd
*abfd
, unsigned long r_symndx
)
2270 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2273 if (r_symndx
< symtab_hdr
->sh_info
)
2275 Elf_Internal_Sym
*isymbuf
;
2276 isymbuf
= retrieve_local_syms (abfd
);
2277 offset
= isymbuf
[r_symndx
].st_value
;
2281 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
2282 struct elf_link_hash_entry
*h
=
2283 elf_sym_hashes (abfd
)[indx
];
2285 while (h
->root
.type
== bfd_link_hash_indirect
2286 || h
->root
.type
== bfd_link_hash_warning
)
2287 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2288 if (h
->root
.type
== bfd_link_hash_defined
2289 || h
->root
.type
== bfd_link_hash_defweak
)
2290 offset
= h
->root
.u
.def
.value
;
2295 /* Iterate over the property records in R_LIST, and copy each record into
2296 the list of records within the relaxation information for the section to
2297 which the record applies. */
2300 avr_elf32_assign_records_to_sections (struct avr_property_record_list
*r_list
)
2304 for (i
= 0; i
< r_list
->record_count
; ++i
)
2306 struct avr_relax_info
*relax_info
;
2308 relax_info
= get_avr_relax_info (r_list
->records
[i
].section
);
2309 BFD_ASSERT (relax_info
!= NULL
);
2311 if (relax_info
->records
.count
2312 == relax_info
->records
.allocated
)
2314 /* Allocate more space. */
2317 relax_info
->records
.allocated
+= 10;
2318 size
= (sizeof (struct avr_property_record
)
2319 * relax_info
->records
.allocated
);
2320 relax_info
->records
.items
2321 = bfd_realloc (relax_info
->records
.items
, size
);
2324 memcpy (&relax_info
->records
.items
[relax_info
->records
.count
],
2325 &r_list
->records
[i
],
2326 sizeof (struct avr_property_record
));
2327 relax_info
->records
.count
++;
2331 /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the
2332 ordering callback from QSORT. */
2335 avr_property_record_compare (const void *ap
, const void *bp
)
2337 const struct avr_property_record
*a
2338 = (struct avr_property_record
*) ap
;
2339 const struct avr_property_record
*b
2340 = (struct avr_property_record
*) bp
;
2342 if (a
->offset
!= b
->offset
)
2343 return (a
->offset
- b
->offset
);
2345 if (a
->section
!= b
->section
)
2346 return (bfd_get_section_vma (a
->section
->owner
, a
->section
)
2347 - bfd_get_section_vma (b
->section
->owner
, b
->section
));
2349 return (a
->type
- b
->type
);
2352 /* Load all of the avr property sections from all of the bfd objects
2353 referenced from LINK_INFO. All of the records within each property
2354 section are assigned to the STRUCT AVR_RELAX_INFO within the section
2355 specific data of the appropriate section. */
2358 avr_load_all_property_sections (struct bfd_link_info
*link_info
)
2363 /* Initialize the per-section relaxation info. */
2364 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2365 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2367 init_avr_relax_info (sec
);
2370 /* Load the descriptor tables from .avr.prop sections. */
2371 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2373 struct avr_property_record_list
*r_list
;
2375 r_list
= avr_elf32_load_property_records (abfd
);
2377 avr_elf32_assign_records_to_sections (r_list
);
2382 /* Now, for every section, ensure that the descriptor list in the
2383 relaxation data is sorted by ascending offset within the section. */
2384 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2385 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2387 struct avr_relax_info
*relax_info
= get_avr_relax_info (sec
);
2388 if (relax_info
&& relax_info
->records
.count
> 0)
2392 qsort (relax_info
->records
.items
,
2393 relax_info
->records
.count
,
2394 sizeof (struct avr_property_record
),
2395 avr_property_record_compare
);
2397 /* For debug purposes, list all the descriptors. */
2398 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
2400 switch (relax_info
->records
.items
[i
].type
)
2404 case RECORD_ORG_AND_FILL
:
2408 case RECORD_ALIGN_AND_FILL
:
2416 /* This function handles relaxing for the avr.
2417 Many important relaxing opportunities within functions are already
2418 realized by the compiler itself.
2419 Here we try to replace call (4 bytes) -> rcall (2 bytes)
2420 and jump -> rjmp (safes also 2 bytes).
2421 As well we now optimize seqences of
2422 - call/rcall function
2427 . In case that within a sequence
2430 the ret could no longer be reached it is optimized away. In order
2431 to check if the ret is no longer needed, it is checked that the ret's address
2432 is not the target of a branch or jump within the same section, it is checked
2433 that there is no skip instruction before the jmp/rjmp and that there
2434 is no local or global label place at the address of the ret.
2436 We refrain from relaxing within sections ".vectors" and
2437 ".jumptables" in order to maintain the position of the instructions.
2438 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
2439 if possible. (In future one could possibly use the space of the nop
2440 for the first instruction of the irq service function.
2442 The .jumptables sections is meant to be used for a future tablejump variant
2443 for the devices with 3-byte program counter where the table itself
2444 contains 4-byte jump instructions whose relative offset must not
2448 elf32_avr_relax_section (bfd
*abfd
,
2450 struct bfd_link_info
*link_info
,
2453 Elf_Internal_Shdr
*symtab_hdr
;
2454 Elf_Internal_Rela
*internal_relocs
;
2455 Elf_Internal_Rela
*irel
, *irelend
;
2456 bfd_byte
*contents
= NULL
;
2457 Elf_Internal_Sym
*isymbuf
= NULL
;
2458 struct elf32_avr_link_hash_table
*htab
;
2459 static bfd_boolean relaxation_initialised
= FALSE
;
2461 if (!relaxation_initialised
)
2463 relaxation_initialised
= TRUE
;
2465 /* Load entries from the .avr.prop sections. */
2466 avr_load_all_property_sections (link_info
);
2469 /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
2470 relaxing. Such shrinking can cause issues for the sections such
2471 as .vectors and .jumptables. Instead the unused bytes should be
2472 filled with nop instructions. */
2473 bfd_boolean shrinkable
= TRUE
;
2475 if (!strcmp (sec
->name
,".vectors")
2476 || !strcmp (sec
->name
,".jumptables"))
2479 if (bfd_link_relocatable (link_info
))
2480 (*link_info
->callbacks
->einfo
)
2481 (_("%P%F: --relax and -r may not be used together\n"));
2483 htab
= avr_link_hash_table (link_info
);
2487 /* Assume nothing changes. */
2490 if ((!htab
->no_stubs
) && (sec
== htab
->stub_sec
))
2492 /* We are just relaxing the stub section.
2493 Let's calculate the size needed again. */
2494 bfd_size_type last_estimated_stub_section_size
= htab
->stub_sec
->size
;
2497 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
2498 (int) last_estimated_stub_section_size
);
2500 elf32_avr_size_stubs (htab
->stub_sec
->output_section
->owner
,
2503 /* Check if the number of trampolines changed. */
2504 if (last_estimated_stub_section_size
!= htab
->stub_sec
->size
)
2508 printf ("Size of stub section after this pass: %i\n",
2509 (int) htab
->stub_sec
->size
);
2514 /* We don't have to do anything for a relocatable link, if
2515 this section does not have relocs, or if this is not a
2517 if (bfd_link_relocatable (link_info
)
2518 || (sec
->flags
& SEC_RELOC
) == 0
2519 || sec
->reloc_count
== 0
2520 || (sec
->flags
& SEC_CODE
) == 0)
2523 /* Check if the object file to relax uses internal symbols so that we
2524 could fix up the relocations. */
2525 if (!(elf_elfheader (abfd
)->e_flags
& EF_AVR_LINKRELAX_PREPARED
))
2528 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2530 /* Get a copy of the native relocations. */
2531 internal_relocs
= (_bfd_elf_link_read_relocs
2532 (abfd
, sec
, NULL
, NULL
, link_info
->keep_memory
));
2533 if (internal_relocs
== NULL
)
2536 /* Walk through the relocs looking for relaxing opportunities. */
2537 irelend
= internal_relocs
+ sec
->reloc_count
;
2538 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2542 if ( ELF32_R_TYPE (irel
->r_info
) != R_AVR_13_PCREL
2543 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_7_PCREL
2544 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_CALL
)
2547 /* Get the section contents if we haven't done so already. */
2548 if (contents
== NULL
)
2550 /* Get cached copy if it exists. */
2551 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2552 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2555 /* Go get them off disk. */
2556 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2561 /* Read this BFD's local symbols if we haven't done so already. */
2562 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2564 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2565 if (isymbuf
== NULL
)
2566 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2567 symtab_hdr
->sh_info
, 0,
2569 if (isymbuf
== NULL
)
2574 /* Get the value of the symbol referred to by the reloc. */
2575 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2577 /* A local symbol. */
2578 Elf_Internal_Sym
*isym
;
2581 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
2582 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2583 symval
= isym
->st_value
;
2584 /* If the reloc is absolute, it will not have
2585 a symbol or section associated with it. */
2587 symval
+= sym_sec
->output_section
->vma
2588 + sym_sec
->output_offset
;
2593 struct elf_link_hash_entry
*h
;
2595 /* An external symbol. */
2596 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
2597 h
= elf_sym_hashes (abfd
)[indx
];
2598 BFD_ASSERT (h
!= NULL
);
2599 if (h
->root
.type
!= bfd_link_hash_defined
2600 && h
->root
.type
!= bfd_link_hash_defweak
)
2601 /* This appears to be a reference to an undefined
2602 symbol. Just ignore it--it will be caught by the
2603 regular reloc processing. */
2606 symval
= (h
->root
.u
.def
.value
2607 + h
->root
.u
.def
.section
->output_section
->vma
2608 + h
->root
.u
.def
.section
->output_offset
);
2611 /* For simplicity of coding, we are going to modify the section
2612 contents, the section relocs, and the BFD symbol table. We
2613 must tell the rest of the code not to free up this
2614 information. It would be possible to instead create a table
2615 of changes which have to be made, as is done in coff-mips.c;
2616 that would be more work, but would require less memory when
2617 the linker is run. */
2618 switch (ELF32_R_TYPE (irel
->r_info
))
2620 /* Try to turn a 22-bit absolute call/jump into an 13-bit
2621 pc-relative rcall/rjmp. */
2624 bfd_vma value
= symval
+ irel
->r_addend
;
2626 int distance_short_enough
= 0;
2628 /* Get the address of this instruction. */
2629 dot
= (sec
->output_section
->vma
2630 + sec
->output_offset
+ irel
->r_offset
);
2632 /* Compute the distance from this insn to the branch target. */
2635 /* Check if the gap falls in the range that can be accommodated
2636 in 13bits signed (It is 12bits when encoded, as we deal with
2637 word addressing). */
2638 if (!shrinkable
&& ((int) gap
>= -4096 && (int) gap
<= 4095))
2639 distance_short_enough
= 1;
2640 /* If shrinkable, then we can check for a range of distance which
2641 is two bytes farther on both the directions because the call
2642 or jump target will be closer by two bytes after the
2644 else if (shrinkable
&& ((int) gap
>= -4094 && (int) gap
<= 4097))
2645 distance_short_enough
= 1;
2647 /* Here we handle the wrap-around case. E.g. for a 16k device
2648 we could use a rjmp to jump from address 0x100 to 0x3d00!
2649 In order to make this work properly, we need to fill the
2650 vaiable avr_pc_wrap_around with the appropriate value.
2651 I.e. 0x4000 for a 16k device. */
2653 /* Shrinking the code size makes the gaps larger in the
2654 case of wrap-arounds. So we use a heuristical safety
2655 margin to avoid that during relax the distance gets
2656 again too large for the short jumps. Let's assume
2657 a typical code-size reduction due to relax for a
2658 16k device of 600 bytes. So let's use twice the
2659 typical value as safety margin. */
2663 int assumed_shrink
= 600;
2664 if (avr_pc_wrap_around
> 0x4000)
2665 assumed_shrink
= 900;
2667 safety_margin
= 2 * assumed_shrink
;
2669 rgap
= avr_relative_distance_considering_wrap_around (gap
);
2671 if (rgap
>= (-4092 + safety_margin
)
2672 && rgap
<= (4094 - safety_margin
))
2673 distance_short_enough
= 1;
2676 if (distance_short_enough
)
2678 unsigned char code_msb
;
2679 unsigned char code_lsb
;
2682 printf ("shrinking jump/call instruction at address 0x%x"
2683 " in section %s\n\n",
2684 (int) dot
, sec
->name
);
2686 /* Note that we've changed the relocs, section contents,
2688 elf_section_data (sec
)->relocs
= internal_relocs
;
2689 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2690 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2692 /* Get the instruction code for relaxing. */
2693 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
);
2694 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
2696 /* Mask out the relocation bits. */
2699 if (code_msb
== 0x94 && code_lsb
== 0x0E)
2701 /* we are changing call -> rcall . */
2702 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
2703 bfd_put_8 (abfd
, 0xD0, contents
+ irel
->r_offset
+ 1);
2705 else if (code_msb
== 0x94 && code_lsb
== 0x0C)
2707 /* we are changeing jump -> rjmp. */
2708 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
2709 bfd_put_8 (abfd
, 0xC0, contents
+ irel
->r_offset
+ 1);
2714 /* Fix the relocation's type. */
2715 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2718 /* We should not modify the ordering if 'shrinkable' is
2722 /* Let's insert a nop. */
2723 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 2);
2724 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 3);
2728 /* Delete two bytes of data. */
2729 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
2730 irel
->r_offset
+ 2, 2,
2734 /* That will change things, so, we should relax again.
2735 Note that this is not required, and it may be slow. */
2744 unsigned char code_msb
;
2745 unsigned char code_lsb
;
2748 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
2749 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 0);
2751 /* Get the address of this instruction. */
2752 dot
= (sec
->output_section
->vma
2753 + sec
->output_offset
+ irel
->r_offset
);
2755 /* Here we look for rcall/ret or call/ret sequences that could be
2756 safely replaced by rjmp/ret or jmp/ret. */
2757 if (((code_msb
& 0xf0) == 0xd0)
2758 && avr_replace_call_ret_sequences
)
2760 /* This insn is a rcall. */
2761 unsigned char next_insn_msb
= 0;
2762 unsigned char next_insn_lsb
= 0;
2764 if (irel
->r_offset
+ 3 < sec
->size
)
2767 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 3);
2769 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 2);
2772 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2774 /* The next insn is a ret. We now convert the rcall insn
2775 into a rjmp instruction. */
2777 bfd_put_8 (abfd
, code_msb
, contents
+ irel
->r_offset
+ 1);
2779 printf ("converted rcall/ret sequence at address 0x%x"
2780 " into rjmp/ret sequence. Section is %s\n\n",
2781 (int) dot
, sec
->name
);
2786 else if ((0x94 == (code_msb
& 0xfe))
2787 && (0x0e == (code_lsb
& 0x0e))
2788 && avr_replace_call_ret_sequences
)
2790 /* This insn is a call. */
2791 unsigned char next_insn_msb
= 0;
2792 unsigned char next_insn_lsb
= 0;
2794 if (irel
->r_offset
+ 5 < sec
->size
)
2797 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 5);
2799 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 4);
2802 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2804 /* The next insn is a ret. We now convert the call insn
2805 into a jmp instruction. */
2808 bfd_put_8 (abfd
, code_lsb
, contents
+ irel
->r_offset
);
2810 printf ("converted call/ret sequence at address 0x%x"
2811 " into jmp/ret sequence. Section is %s\n\n",
2812 (int) dot
, sec
->name
);
2817 else if ((0xc0 == (code_msb
& 0xf0))
2818 || ((0x94 == (code_msb
& 0xfe))
2819 && (0x0c == (code_lsb
& 0x0e))))
2821 /* This insn is a rjmp or a jmp. */
2822 unsigned char next_insn_msb
= 0;
2823 unsigned char next_insn_lsb
= 0;
2826 if (0xc0 == (code_msb
& 0xf0))
2827 insn_size
= 2; /* rjmp insn */
2829 insn_size
= 4; /* jmp insn */
2831 if (irel
->r_offset
+ insn_size
+ 1 < sec
->size
)
2834 bfd_get_8 (abfd
, contents
+ irel
->r_offset
2837 bfd_get_8 (abfd
, contents
+ irel
->r_offset
2841 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2843 /* The next insn is a ret. We possibly could delete
2844 this ret. First we need to check for preceding
2845 sbis/sbic/sbrs or cpse "skip" instructions. */
2847 int there_is_preceding_non_skip_insn
= 1;
2848 bfd_vma address_of_ret
;
2850 address_of_ret
= dot
+ insn_size
;
2852 if (debug_relax
&& (insn_size
== 2))
2853 printf ("found rjmp / ret sequence at address 0x%x\n",
2855 if (debug_relax
&& (insn_size
== 4))
2856 printf ("found jmp / ret sequence at address 0x%x\n",
2859 /* We have to make sure that there is a preceding insn. */
2860 if (irel
->r_offset
>= 2)
2862 unsigned char preceding_msb
;
2863 unsigned char preceding_lsb
;
2866 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2868 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2871 if (0x99 == preceding_msb
)
2872 there_is_preceding_non_skip_insn
= 0;
2875 if (0x9b == preceding_msb
)
2876 there_is_preceding_non_skip_insn
= 0;
2879 if ((0xfc == (preceding_msb
& 0xfe)
2880 && (0x00 == (preceding_lsb
& 0x08))))
2881 there_is_preceding_non_skip_insn
= 0;
2884 if ((0xfe == (preceding_msb
& 0xfe)
2885 && (0x00 == (preceding_lsb
& 0x08))))
2886 there_is_preceding_non_skip_insn
= 0;
2889 if (0x10 == (preceding_msb
& 0xfc))
2890 there_is_preceding_non_skip_insn
= 0;
2892 if (there_is_preceding_non_skip_insn
== 0)
2894 printf ("preceding skip insn prevents deletion of"
2895 " ret insn at Addy 0x%x in section %s\n",
2896 (int) dot
+ 2, sec
->name
);
2900 /* There is no previous instruction. */
2901 there_is_preceding_non_skip_insn
= 0;
2904 if (there_is_preceding_non_skip_insn
)
2906 /* We now only have to make sure that there is no
2907 local label defined at the address of the ret
2908 instruction and that there is no local relocation
2909 in this section pointing to the ret. */
2911 int deleting_ret_is_safe
= 1;
2912 unsigned int section_offset_of_ret_insn
=
2913 irel
->r_offset
+ insn_size
;
2914 Elf_Internal_Sym
*isym
, *isymend
;
2915 unsigned int sec_shndx
;
2916 struct bfd_section
*isec
;
2919 _bfd_elf_section_from_bfd_section (abfd
, sec
);
2921 /* Check for local symbols. */
2922 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2923 isymend
= isym
+ symtab_hdr
->sh_info
;
2924 /* PR 6019: There may not be any local symbols. */
2925 for (; isym
!= NULL
&& isym
< isymend
; isym
++)
2927 if (isym
->st_value
== section_offset_of_ret_insn
2928 && isym
->st_shndx
== sec_shndx
)
2930 deleting_ret_is_safe
= 0;
2932 printf ("local label prevents deletion of ret "
2933 "insn at address 0x%x\n",
2934 (int) dot
+ insn_size
);
2938 /* Now check for global symbols. */
2941 struct elf_link_hash_entry
**sym_hashes
;
2942 struct elf_link_hash_entry
**end_hashes
;
2944 symcount
= (symtab_hdr
->sh_size
2945 / sizeof (Elf32_External_Sym
)
2946 - symtab_hdr
->sh_info
);
2947 sym_hashes
= elf_sym_hashes (abfd
);
2948 end_hashes
= sym_hashes
+ symcount
;
2949 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2951 struct elf_link_hash_entry
*sym_hash
=
2953 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2954 || sym_hash
->root
.type
==
2955 bfd_link_hash_defweak
)
2956 && sym_hash
->root
.u
.def
.section
== sec
2957 && sym_hash
->root
.u
.def
.value
== section_offset_of_ret_insn
)
2959 deleting_ret_is_safe
= 0;
2961 printf ("global label prevents deletion of "
2962 "ret insn at address 0x%x\n",
2963 (int) dot
+ insn_size
);
2968 /* Now we check for relocations pointing to ret. */
2969 for (isec
= abfd
->sections
; isec
&& deleting_ret_is_safe
; isec
= isec
->next
)
2971 Elf_Internal_Rela
*rel
;
2972 Elf_Internal_Rela
*relend
;
2974 rel
= elf_section_data (isec
)->relocs
;
2976 rel
= _bfd_elf_link_read_relocs (abfd
, isec
, NULL
, NULL
, TRUE
);
2978 relend
= rel
+ isec
->reloc_count
;
2980 for (; rel
&& rel
< relend
; rel
++)
2982 bfd_vma reloc_target
= 0;
2984 /* Read this BFD's local symbols if we haven't
2986 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2988 isymbuf
= (Elf_Internal_Sym
*)
2989 symtab_hdr
->contents
;
2990 if (isymbuf
== NULL
)
2991 isymbuf
= bfd_elf_get_elf_syms
2994 symtab_hdr
->sh_info
, 0,
2996 if (isymbuf
== NULL
)
3000 /* Get the value of the symbol referred to
3002 if (ELF32_R_SYM (rel
->r_info
)
3003 < symtab_hdr
->sh_info
)
3005 /* A local symbol. */
3009 + ELF32_R_SYM (rel
->r_info
);
3010 sym_sec
= bfd_section_from_elf_index
3011 (abfd
, isym
->st_shndx
);
3012 symval
= isym
->st_value
;
3014 /* If the reloc is absolute, it will not
3015 have a symbol or section associated
3021 sym_sec
->output_section
->vma
3022 + sym_sec
->output_offset
;
3023 reloc_target
= symval
+ rel
->r_addend
;
3027 reloc_target
= symval
+ rel
->r_addend
;
3028 /* Reference symbol is absolute. */
3031 /* else ... reference symbol is extern. */
3033 if (address_of_ret
== reloc_target
)
3035 deleting_ret_is_safe
= 0;
3038 "rjmp/jmp ret sequence at address"
3039 " 0x%x could not be deleted. ret"
3040 " is target of a relocation.\n",
3041 (int) address_of_ret
);
3047 if (deleting_ret_is_safe
)
3050 printf ("unreachable ret instruction "
3051 "at address 0x%x deleted.\n",
3052 (int) dot
+ insn_size
);
3054 /* Delete two bytes of data. */
3055 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
3056 irel
->r_offset
+ insn_size
, 2,
3060 /* That will change things, so, we should relax
3061 again. Note that this is not required, and it
3076 /* Look through all the property records in this section to see if
3077 there's any alignment records that can be moved. */
3078 struct avr_relax_info
*relax_info
;
3080 relax_info
= get_avr_relax_info (sec
);
3081 if (relax_info
->records
.count
> 0)
3085 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
3087 switch (relax_info
->records
.items
[i
].type
)
3090 case RECORD_ORG_AND_FILL
:
3093 case RECORD_ALIGN_AND_FILL
:
3095 struct avr_property_record
*record
;
3096 unsigned long bytes_to_align
;
3099 /* Look for alignment directives that have had enough
3100 bytes deleted before them, such that the directive
3101 can be moved backwards and still maintain the
3102 required alignment. */
3103 record
= &relax_info
->records
.items
[i
];
3105 = (unsigned long) (1 << record
->data
.align
.bytes
);
3106 while (record
->data
.align
.preceding_deleted
>=
3109 record
->data
.align
.preceding_deleted
3111 count
+= bytes_to_align
;
3116 bfd_vma addr
= record
->offset
;
3118 /* We can delete COUNT bytes and this alignment
3119 directive will still be correctly aligned.
3120 First move the alignment directive, then delete
3122 record
->offset
-= count
;
3123 elf32_avr_relax_delete_bytes (abfd
, sec
,
3135 if (contents
!= NULL
3136 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3138 if (! link_info
->keep_memory
)
3142 /* Cache the section contents for elf_link_input_bfd. */
3143 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3147 if (internal_relocs
!= NULL
3148 && elf_section_data (sec
)->relocs
!= internal_relocs
)
3149 free (internal_relocs
);
3155 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3157 if (contents
!= NULL
3158 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3160 if (internal_relocs
!= NULL
3161 && elf_section_data (sec
)->relocs
!= internal_relocs
)
3162 free (internal_relocs
);
3167 /* This is a version of bfd_generic_get_relocated_section_contents
3168 which uses elf32_avr_relocate_section.
3170 For avr it's essentially a cut and paste taken from the H8300 port.
3171 The author of the relaxation support patch for avr had absolutely no
3172 clue what is happening here but found out that this part of the code
3173 seems to be important. */
3176 elf32_avr_get_relocated_section_contents (bfd
*output_bfd
,
3177 struct bfd_link_info
*link_info
,
3178 struct bfd_link_order
*link_order
,
3180 bfd_boolean relocatable
,
3183 Elf_Internal_Shdr
*symtab_hdr
;
3184 asection
*input_section
= link_order
->u
.indirect
.section
;
3185 bfd
*input_bfd
= input_section
->owner
;
3186 asection
**sections
= NULL
;
3187 Elf_Internal_Rela
*internal_relocs
= NULL
;
3188 Elf_Internal_Sym
*isymbuf
= NULL
;
3190 /* We only need to handle the case of relaxing, or of having a
3191 particular set of section contents, specially. */
3193 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
3194 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
3198 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3200 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
3201 (size_t) input_section
->size
);
3203 if ((input_section
->flags
& SEC_RELOC
) != 0
3204 && input_section
->reloc_count
> 0)
3207 Elf_Internal_Sym
*isym
, *isymend
;
3210 internal_relocs
= (_bfd_elf_link_read_relocs
3211 (input_bfd
, input_section
, NULL
, NULL
, FALSE
));
3212 if (internal_relocs
== NULL
)
3215 if (symtab_hdr
->sh_info
!= 0)
3217 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3218 if (isymbuf
== NULL
)
3219 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3220 symtab_hdr
->sh_info
, 0,
3222 if (isymbuf
== NULL
)
3226 amt
= symtab_hdr
->sh_info
;
3227 amt
*= sizeof (asection
*);
3228 sections
= bfd_malloc (amt
);
3229 if (sections
== NULL
&& amt
!= 0)
3232 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
3233 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
3237 if (isym
->st_shndx
== SHN_UNDEF
)
3238 isec
= bfd_und_section_ptr
;
3239 else if (isym
->st_shndx
== SHN_ABS
)
3240 isec
= bfd_abs_section_ptr
;
3241 else if (isym
->st_shndx
== SHN_COMMON
)
3242 isec
= bfd_com_section_ptr
;
3244 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
3249 if (! elf32_avr_relocate_section (output_bfd
, link_info
, input_bfd
,
3250 input_section
, data
, internal_relocs
,
3254 if (sections
!= NULL
)
3257 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3259 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
3260 free (internal_relocs
);
3266 if (sections
!= NULL
)
3269 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3271 if (internal_relocs
!= NULL
3272 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
3273 free (internal_relocs
);
3278 /* Determines the hash entry name for a particular reloc. It consists of
3279 the identifier of the symbol section and the added reloc addend and
3280 symbol offset relative to the section the symbol is attached to. */
3283 avr_stub_name (const asection
*symbol_section
,
3284 const bfd_vma symbol_offset
,
3285 const Elf_Internal_Rela
*rela
)
3290 len
= 8 + 1 + 8 + 1 + 1;
3291 stub_name
= bfd_malloc (len
);
3293 sprintf (stub_name
, "%08x+%08x",
3294 symbol_section
->id
& 0xffffffff,
3295 (unsigned int) ((rela
->r_addend
& 0xffffffff) + symbol_offset
));
3301 /* Add a new stub entry to the stub hash. Not all fields of the new
3302 stub entry are initialised. */
3304 static struct elf32_avr_stub_hash_entry
*
3305 avr_add_stub (const char *stub_name
,
3306 struct elf32_avr_link_hash_table
*htab
)
3308 struct elf32_avr_stub_hash_entry
*hsh
;
3310 /* Enter this entry into the linker stub hash table. */
3311 hsh
= avr_stub_hash_lookup (&htab
->bstab
, stub_name
, TRUE
, FALSE
);
3315 /* xgettext:c-format */
3316 _bfd_error_handler (_("cannot create stub entry %s"), stub_name
);
3320 hsh
->stub_offset
= 0;
3324 /* We assume that there is already space allocated for the stub section
3325 contents and that before building the stubs the section size is
3326 initialized to 0. We assume that within the stub hash table entry,
3327 the absolute position of the jmp target has been written in the
3328 target_value field. We write here the offset of the generated jmp insn
3329 relative to the trampoline section start to the stub_offset entry in
3330 the stub hash table entry. */
3333 avr_build_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
3335 struct elf32_avr_stub_hash_entry
*hsh
;
3336 struct bfd_link_info
*info
;
3337 struct elf32_avr_link_hash_table
*htab
;
3344 bfd_vma jmp_insn
= 0x0000940c;
3346 /* Massage our args to the form they really have. */
3347 hsh
= avr_stub_hash_entry (bh
);
3349 if (!hsh
->is_actually_needed
)
3352 info
= (struct bfd_link_info
*) in_arg
;
3354 htab
= avr_link_hash_table (info
);
3358 target
= hsh
->target_value
;
3360 /* Make a note of the offset within the stubs for this entry. */
3361 hsh
->stub_offset
= htab
->stub_sec
->size
;
3362 loc
= htab
->stub_sec
->contents
+ hsh
->stub_offset
;
3364 stub_bfd
= htab
->stub_sec
->owner
;
3367 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
3368 (unsigned int) target
,
3369 (unsigned int) hsh
->stub_offset
);
3371 /* We now have to add the information on the jump target to the bare
3372 opcode bits already set in jmp_insn. */
3374 /* Check for the alignment of the address. */
3378 starget
= target
>> 1;
3379 jmp_insn
|= ((starget
& 0x10000) | ((starget
<< 3) & 0x1f00000)) >> 16;
3380 bfd_put_16 (stub_bfd
, jmp_insn
, loc
);
3381 bfd_put_16 (stub_bfd
, (bfd_vma
) starget
& 0xffff, loc
+ 2);
3383 htab
->stub_sec
->size
+= 4;
3385 /* Now add the entries in the address mapping table if there is still
3390 nr
= htab
->amt_entry_cnt
+ 1;
3391 if (nr
<= htab
->amt_max_entry_cnt
)
3393 htab
->amt_entry_cnt
= nr
;
3395 htab
->amt_stub_offsets
[nr
- 1] = hsh
->stub_offset
;
3396 htab
->amt_destination_addr
[nr
- 1] = target
;
3404 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry
*bh
,
3405 void *in_arg ATTRIBUTE_UNUSED
)
3407 struct elf32_avr_stub_hash_entry
*hsh
;
3409 hsh
= avr_stub_hash_entry (bh
);
3410 hsh
->is_actually_needed
= FALSE
;
3416 avr_size_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
3418 struct elf32_avr_stub_hash_entry
*hsh
;
3419 struct elf32_avr_link_hash_table
*htab
;
3422 /* Massage our args to the form they really have. */
3423 hsh
= avr_stub_hash_entry (bh
);
3426 if (hsh
->is_actually_needed
)
3431 htab
->stub_sec
->size
+= size
;
3436 elf32_avr_setup_params (struct bfd_link_info
*info
,
3438 asection
*avr_stub_section
,
3439 bfd_boolean no_stubs
,
3440 bfd_boolean deb_stubs
,
3441 bfd_boolean deb_relax
,
3442 bfd_vma pc_wrap_around
,
3443 bfd_boolean call_ret_replacement
)
3445 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3449 htab
->stub_sec
= avr_stub_section
;
3450 htab
->stub_bfd
= avr_stub_bfd
;
3451 htab
->no_stubs
= no_stubs
;
3453 debug_relax
= deb_relax
;
3454 debug_stubs
= deb_stubs
;
3455 avr_pc_wrap_around
= pc_wrap_around
;
3456 avr_replace_call_ret_sequences
= call_ret_replacement
;
3460 /* Set up various things so that we can make a list of input sections
3461 for each output section included in the link. Returns -1 on error,
3462 0 when no stubs will be needed, and 1 on success. It also sets
3463 information on the stubs bfd and the stub section in the info
3467 elf32_avr_setup_section_lists (bfd
*output_bfd
,
3468 struct bfd_link_info
*info
)
3471 unsigned int bfd_count
;
3472 unsigned int top_id
, top_index
;
3474 asection
**input_list
, **list
;
3476 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3478 if (htab
== NULL
|| htab
->no_stubs
)
3481 /* Count the number of input BFDs and find the top input section id. */
3482 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
3484 input_bfd
= input_bfd
->link
.next
)
3487 for (section
= input_bfd
->sections
;
3489 section
= section
->next
)
3490 if (top_id
< section
->id
)
3491 top_id
= section
->id
;
3494 htab
->bfd_count
= bfd_count
;
3496 /* We can't use output_bfd->section_count here to find the top output
3497 section index as some sections may have been removed, and
3498 strip_excluded_output_sections doesn't renumber the indices. */
3499 for (section
= output_bfd
->sections
, top_index
= 0;
3501 section
= section
->next
)
3502 if (top_index
< section
->index
)
3503 top_index
= section
->index
;
3505 htab
->top_index
= top_index
;
3506 amt
= sizeof (asection
*) * (top_index
+ 1);
3507 input_list
= bfd_malloc (amt
);
3508 htab
->input_list
= input_list
;
3509 if (input_list
== NULL
)
3512 /* For sections we aren't interested in, mark their entries with a
3513 value we can check later. */
3514 list
= input_list
+ top_index
;
3516 *list
= bfd_abs_section_ptr
;
3517 while (list
-- != input_list
);
3519 for (section
= output_bfd
->sections
;
3521 section
= section
->next
)
3522 if ((section
->flags
& SEC_CODE
) != 0)
3523 input_list
[section
->index
] = NULL
;
3529 /* Read in all local syms for all input bfds, and create hash entries
3530 for export stubs if we are building a multi-subspace shared lib.
3531 Returns -1 on error, 0 otherwise. */
3534 get_local_syms (bfd
*input_bfd
, struct bfd_link_info
*info
)
3536 unsigned int bfd_indx
;
3537 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
3538 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3544 /* We want to read in symbol extension records only once. To do this
3545 we need to read in the local symbols in parallel and save them for
3546 later use; so hold pointers to the local symbols in an array. */
3547 amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
3548 all_local_syms
= bfd_zmalloc (amt
);
3549 htab
->all_local_syms
= all_local_syms
;
3550 if (all_local_syms
== NULL
)
3553 /* Walk over all the input BFDs, swapping in local symbols.
3554 If we are creating a shared library, create hash entries for the
3558 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3560 Elf_Internal_Shdr
*symtab_hdr
;
3562 /* We'll need the symbol table in a second. */
3563 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3564 if (symtab_hdr
->sh_info
== 0)
3567 /* We need an array of the local symbols attached to the input bfd. */
3568 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3569 if (local_syms
== NULL
)
3571 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3572 symtab_hdr
->sh_info
, 0,
3574 /* Cache them for elf_link_input_bfd. */
3575 symtab_hdr
->contents
= (unsigned char *) local_syms
;
3577 if (local_syms
== NULL
)
3580 all_local_syms
[bfd_indx
] = local_syms
;
3586 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
3589 elf32_avr_size_stubs (bfd
*output_bfd
,
3590 struct bfd_link_info
*info
,
3591 bfd_boolean is_prealloc_run
)
3593 struct elf32_avr_link_hash_table
*htab
;
3594 int stub_changed
= 0;
3596 htab
= avr_link_hash_table (info
);
3600 /* At this point we initialize htab->vector_base
3601 To the start of the text output section. */
3602 htab
->vector_base
= htab
->stub_sec
->output_section
->vma
;
3604 if (get_local_syms (info
->input_bfds
, info
))
3606 if (htab
->all_local_syms
)
3607 goto error_ret_free_local
;
3611 if (ADD_DUMMY_STUBS_FOR_DEBUGGING
)
3613 struct elf32_avr_stub_hash_entry
*test
;
3615 test
= avr_add_stub ("Hugo",htab
);
3616 test
->target_value
= 0x123456;
3617 test
->stub_offset
= 13;
3619 test
= avr_add_stub ("Hugo2",htab
);
3620 test
->target_value
= 0x84210;
3621 test
->stub_offset
= 14;
3627 unsigned int bfd_indx
;
3629 /* We will have to re-generate the stub hash table each time anything
3630 in memory has changed. */
3632 bfd_hash_traverse (&htab
->bstab
, avr_mark_stub_not_to_be_necessary
, htab
);
3633 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
3635 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3637 Elf_Internal_Shdr
*symtab_hdr
;
3639 Elf_Internal_Sym
*local_syms
;
3641 /* We'll need the symbol table in a second. */
3642 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3643 if (symtab_hdr
->sh_info
== 0)
3646 local_syms
= htab
->all_local_syms
[bfd_indx
];
3648 /* Walk over each section attached to the input bfd. */
3649 for (section
= input_bfd
->sections
;
3651 section
= section
->next
)
3653 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
3655 /* If there aren't any relocs, then there's nothing more
3657 if ((section
->flags
& SEC_RELOC
) == 0
3658 || section
->reloc_count
== 0)
3661 /* If this section is a link-once section that will be
3662 discarded, then don't create any stubs. */
3663 if (section
->output_section
== NULL
3664 || section
->output_section
->owner
!= output_bfd
)
3667 /* Get the relocs. */
3669 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
3671 if (internal_relocs
== NULL
)
3672 goto error_ret_free_local
;
3674 /* Now examine each relocation. */
3675 irela
= internal_relocs
;
3676 irelaend
= irela
+ section
->reloc_count
;
3677 for (; irela
< irelaend
; irela
++)
3679 unsigned int r_type
, r_indx
;
3680 struct elf32_avr_stub_hash_entry
*hsh
;
3683 bfd_vma destination
;
3684 struct elf_link_hash_entry
*hh
;
3687 r_type
= ELF32_R_TYPE (irela
->r_info
);
3688 r_indx
= ELF32_R_SYM (irela
->r_info
);
3690 /* Only look for 16 bit GS relocs. No other reloc will need a
3692 if (!((r_type
== R_AVR_16_PM
)
3693 || (r_type
== R_AVR_LO8_LDI_GS
)
3694 || (r_type
== R_AVR_HI8_LDI_GS
)))
3697 /* Now determine the call target, its name, value,
3703 if (r_indx
< symtab_hdr
->sh_info
)
3705 /* It's a local symbol. */
3706 Elf_Internal_Sym
*sym
;
3707 Elf_Internal_Shdr
*hdr
;
3710 sym
= local_syms
+ r_indx
;
3711 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3712 sym_value
= sym
->st_value
;
3713 shndx
= sym
->st_shndx
;
3714 if (shndx
< elf_numsections (input_bfd
))
3716 hdr
= elf_elfsections (input_bfd
)[shndx
];
3717 sym_sec
= hdr
->bfd_section
;
3718 destination
= (sym_value
+ irela
->r_addend
3719 + sym_sec
->output_offset
3720 + sym_sec
->output_section
->vma
);
3725 /* It's an external symbol. */
3728 e_indx
= r_indx
- symtab_hdr
->sh_info
;
3729 hh
= elf_sym_hashes (input_bfd
)[e_indx
];
3731 while (hh
->root
.type
== bfd_link_hash_indirect
3732 || hh
->root
.type
== bfd_link_hash_warning
)
3733 hh
= (struct elf_link_hash_entry
*)
3734 (hh
->root
.u
.i
.link
);
3736 if (hh
->root
.type
== bfd_link_hash_defined
3737 || hh
->root
.type
== bfd_link_hash_defweak
)
3739 sym_sec
= hh
->root
.u
.def
.section
;
3740 sym_value
= hh
->root
.u
.def
.value
;
3741 if (sym_sec
->output_section
!= NULL
)
3742 destination
= (sym_value
+ irela
->r_addend
3743 + sym_sec
->output_offset
3744 + sym_sec
->output_section
->vma
);
3746 else if (hh
->root
.type
== bfd_link_hash_undefweak
)
3748 if (! bfd_link_pic (info
))
3751 else if (hh
->root
.type
== bfd_link_hash_undefined
)
3753 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
3754 && (ELF_ST_VISIBILITY (hh
->other
)
3760 bfd_set_error (bfd_error_bad_value
);
3762 error_ret_free_internal
:
3763 if (elf_section_data (section
)->relocs
== NULL
)
3764 free (internal_relocs
);
3765 goto error_ret_free_local
;
3769 if (! avr_stub_is_required_for_16_bit_reloc
3770 (destination
- htab
->vector_base
))
3772 if (!is_prealloc_run
)
3773 /* We are having a reloc that does't need a stub. */
3776 /* We don't right now know if a stub will be needed.
3777 Let's rather be on the safe side. */
3780 /* Get the name of this stub. */
3781 stub_name
= avr_stub_name (sym_sec
, sym_value
, irela
);
3784 goto error_ret_free_internal
;
3787 hsh
= avr_stub_hash_lookup (&htab
->bstab
,
3792 /* The proper stub has already been created. Mark it
3793 to be used and write the possibly changed destination
3795 hsh
->is_actually_needed
= TRUE
;
3796 hsh
->target_value
= destination
;
3801 hsh
= avr_add_stub (stub_name
, htab
);
3805 goto error_ret_free_internal
;
3808 hsh
->is_actually_needed
= TRUE
;
3809 hsh
->target_value
= destination
;
3812 printf ("Adding stub with destination 0x%x to the"
3813 " hash table.\n", (unsigned int) destination
);
3815 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run
);
3817 stub_changed
= TRUE
;
3820 /* We're done with the internal relocs, free them. */
3821 if (elf_section_data (section
)->relocs
== NULL
)
3822 free (internal_relocs
);
3826 /* Re-Calculate the number of needed stubs. */
3827 htab
->stub_sec
->size
= 0;
3828 bfd_hash_traverse (&htab
->bstab
, avr_size_one_stub
, htab
);
3833 stub_changed
= FALSE
;
3836 free (htab
->all_local_syms
);
3839 error_ret_free_local
:
3840 free (htab
->all_local_syms
);
3845 /* Build all the stubs associated with the current output file. The
3846 stubs are kept in a hash table attached to the main linker hash
3847 table. We also set up the .plt entries for statically linked PIC
3848 functions here. This function is called via hppaelf_finish in the
3852 elf32_avr_build_stubs (struct bfd_link_info
*info
)
3855 struct bfd_hash_table
*table
;
3856 struct elf32_avr_link_hash_table
*htab
;
3857 bfd_size_type total_size
= 0;
3859 htab
= avr_link_hash_table (info
);
3863 /* In case that there were several stub sections: */
3864 for (stub_sec
= htab
->stub_bfd
->sections
;
3866 stub_sec
= stub_sec
->next
)
3870 /* Allocate memory to hold the linker stubs. */
3871 size
= stub_sec
->size
;
3874 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
3875 if (stub_sec
->contents
== NULL
&& size
!= 0)
3880 /* Allocate memory for the adress mapping table. */
3881 htab
->amt_entry_cnt
= 0;
3882 htab
->amt_max_entry_cnt
= total_size
/ 4;
3883 htab
->amt_stub_offsets
= bfd_malloc (sizeof (bfd_vma
)
3884 * htab
->amt_max_entry_cnt
);
3885 htab
->amt_destination_addr
= bfd_malloc (sizeof (bfd_vma
)
3886 * htab
->amt_max_entry_cnt
);
3889 printf ("Allocating %i entries in the AMT\n", htab
->amt_max_entry_cnt
);
3891 /* Build the stubs as directed by the stub hash table. */
3892 table
= &htab
->bstab
;
3893 bfd_hash_traverse (table
, avr_build_one_stub
, info
);
3896 printf ("Final Stub section Size: %i\n", (int) htab
->stub_sec
->size
);
3901 /* Callback used by QSORT to order relocations AP and BP. */
3904 internal_reloc_compare (const void *ap
, const void *bp
)
3906 const Elf_Internal_Rela
*a
= (const Elf_Internal_Rela
*) ap
;
3907 const Elf_Internal_Rela
*b
= (const Elf_Internal_Rela
*) bp
;
3909 if (a
->r_offset
!= b
->r_offset
)
3910 return (a
->r_offset
- b
->r_offset
);
3912 /* We don't need to sort on these criteria for correctness,
3913 but enforcing a more strict ordering prevents unstable qsort
3914 from behaving differently with different implementations.
3915 Without the code below we get correct but different results
3916 on Solaris 2.7 and 2.8. We would like to always produce the
3917 same results no matter the host. */
3919 if (a
->r_info
!= b
->r_info
)
3920 return (a
->r_info
- b
->r_info
);
3922 return (a
->r_addend
- b
->r_addend
);
3925 /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */
3928 avr_is_section_for_address (bfd
*abfd
, asection
*section
, bfd_vma address
)
3933 vma
= bfd_get_section_vma (abfd
, section
);
3937 size
= section
->size
;
3938 if (address
>= vma
+ size
)
3944 /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */
3946 struct avr_find_section_data
3948 /* The address we're looking for. */
3951 /* The section we've found. */
3955 /* Helper function to locate the section holding a certain virtual memory
3956 address. This is called via bfd_map_over_sections. The DATA is an
3957 instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which
3958 has been set to the address to search for, and the section field has
3959 been set to NULL. If SECTION from ABFD contains ADDRESS then the
3960 section field in DATA will be set to SECTION. As an optimisation, if
3961 the section field is already non-null then this function does not
3962 perform any checks, and just returns. */
3965 avr_find_section_for_address (bfd
*abfd
,
3966 asection
*section
, void *data
)
3968 struct avr_find_section_data
*fs_data
3969 = (struct avr_find_section_data
*) data
;
3971 /* Return if already found. */
3972 if (fs_data
->section
!= NULL
)
3975 /* If this section isn't part of the addressable code content, skip it. */
3976 if ((bfd_get_section_flags (abfd
, section
) & SEC_ALLOC
) == 0
3977 && (bfd_get_section_flags (abfd
, section
) & SEC_CODE
) == 0)
3980 if (avr_is_section_for_address (abfd
, section
, fs_data
->address
))
3981 fs_data
->section
= section
;
3984 /* Load all of the property records from SEC, a section from ABFD. Return
3985 a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The
3986 memory for the returned structure, and all of the records pointed too by
3987 the structure are allocated with a single call to malloc, so, only the
3988 pointer returned needs to be free'd. */
3990 static struct avr_property_record_list
*
3991 avr_elf32_load_records_from_section (bfd
*abfd
, asection
*sec
)
3993 char *contents
= NULL
, *ptr
;
3994 bfd_size_type size
, mem_size
;
3995 bfd_byte version
, flags
;
3996 uint16_t record_count
, i
;
3997 struct avr_property_record_list
*r_list
= NULL
;
3998 Elf_Internal_Rela
*internal_relocs
= NULL
, *rel
, *rel_end
;
3999 struct avr_find_section_data fs_data
;
4001 fs_data
.section
= NULL
;
4003 size
= bfd_get_section_size (sec
);
4004 contents
= bfd_malloc (size
);
4005 bfd_get_section_contents (abfd
, sec
, contents
, 0, size
);
4008 /* Load the relocations for the '.avr.prop' section if there are any, and
4010 internal_relocs
= (_bfd_elf_link_read_relocs
4011 (abfd
, sec
, NULL
, NULL
, FALSE
));
4012 if (internal_relocs
)
4013 qsort (internal_relocs
, sec
->reloc_count
,
4014 sizeof (Elf_Internal_Rela
), internal_reloc_compare
);
4016 /* There is a header at the start of the property record section SEC, the
4017 format of this header is:
4018 uint8_t : version number
4020 uint16_t : record counter
4023 /* Check we have at least got a headers worth of bytes. */
4024 if (size
< AVR_PROPERTY_SECTION_HEADER_SIZE
)
4027 version
= *((bfd_byte
*) ptr
);
4029 flags
= *((bfd_byte
*) ptr
);
4031 record_count
= *((uint16_t *) ptr
);
4033 BFD_ASSERT (ptr
- contents
== AVR_PROPERTY_SECTION_HEADER_SIZE
);
4035 /* Now allocate space for the list structure, and all of the list
4036 elements in a single block. */
4037 mem_size
= sizeof (struct avr_property_record_list
)
4038 + sizeof (struct avr_property_record
) * record_count
;
4039 r_list
= bfd_malloc (mem_size
);
4043 r_list
->version
= version
;
4044 r_list
->flags
= flags
;
4045 r_list
->section
= sec
;
4046 r_list
->record_count
= record_count
;
4047 r_list
->records
= (struct avr_property_record
*) (&r_list
[1]);
4048 size
-= AVR_PROPERTY_SECTION_HEADER_SIZE
;
4050 /* Check that we understand the version number. There is only one
4051 version number right now, anything else is an error. */
4052 if (r_list
->version
!= AVR_PROPERTY_RECORDS_VERSION
)
4055 rel
= internal_relocs
;
4056 rel_end
= rel
+ sec
->reloc_count
;
4057 for (i
= 0; i
< record_count
; ++i
)
4061 /* Each entry is a 32-bit address, followed by a single byte type.
4062 After that is the type specific data. We must take care to
4063 ensure that we don't read beyond the end of the section data. */
4067 r_list
->records
[i
].section
= NULL
;
4068 r_list
->records
[i
].offset
= 0;
4072 /* The offset of the address within the .avr.prop section. */
4073 size_t offset
= ptr
- contents
;
4075 while (rel
< rel_end
&& rel
->r_offset
< offset
)
4080 else if (rel
->r_offset
== offset
)
4082 /* Find section and section offset. */
4083 unsigned long r_symndx
;
4088 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4089 rel_sec
= get_elf_r_symndx_section (abfd
, r_symndx
);
4090 sec_offset
= get_elf_r_symndx_offset (abfd
, r_symndx
)
4093 r_list
->records
[i
].section
= rel_sec
;
4094 r_list
->records
[i
].offset
= sec_offset
;
4098 address
= *((uint32_t *) ptr
);
4102 if (r_list
->records
[i
].section
== NULL
)
4104 /* Try to find section and offset from address. */
4105 if (fs_data
.section
!= NULL
4106 && !avr_is_section_for_address (abfd
, fs_data
.section
,
4108 fs_data
.section
= NULL
;
4110 if (fs_data
.section
== NULL
)
4112 fs_data
.address
= address
;
4113 bfd_map_over_sections (abfd
, avr_find_section_for_address
,
4117 if (fs_data
.section
== NULL
)
4119 fprintf (stderr
, "Failed to find matching section.\n");
4123 r_list
->records
[i
].section
= fs_data
.section
;
4124 r_list
->records
[i
].offset
4125 = address
- bfd_get_section_vma (abfd
, fs_data
.section
);
4128 r_list
->records
[i
].type
= *((bfd_byte
*) ptr
);
4132 switch (r_list
->records
[i
].type
)
4135 /* Nothing else to load. */
4137 case RECORD_ORG_AND_FILL
:
4138 /* Just a 4-byte fill to load. */
4141 r_list
->records
[i
].data
.org
.fill
= *((uint32_t *) ptr
);
4146 /* Just a 4-byte alignment to load. */
4149 r_list
->records
[i
].data
.align
.bytes
= *((uint32_t *) ptr
);
4152 /* Just initialise PRECEDING_DELETED field, this field is
4153 used during linker relaxation. */
4154 r_list
->records
[i
].data
.align
.preceding_deleted
= 0;
4156 case RECORD_ALIGN_AND_FILL
:
4157 /* A 4-byte alignment, and a 4-byte fill to load. */
4160 r_list
->records
[i
].data
.align
.bytes
= *((uint32_t *) ptr
);
4162 r_list
->records
[i
].data
.align
.fill
= *((uint32_t *) ptr
);
4165 /* Just initialise PRECEDING_DELETED field, this field is
4166 used during linker relaxation. */
4167 r_list
->records
[i
].data
.align
.preceding_deleted
= 0;
4175 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
4176 free (internal_relocs
);
4180 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
4181 free (internal_relocs
);
4187 /* Load all of the property records from ABFD. See
4188 AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */
4190 struct avr_property_record_list
*
4191 avr_elf32_load_property_records (bfd
*abfd
)
4195 /* Find the '.avr.prop' section and load the contents into memory. */
4196 sec
= bfd_get_section_by_name (abfd
, AVR_PROPERTY_RECORD_SECTION_NAME
);
4199 return avr_elf32_load_records_from_section (abfd
, sec
);
4203 avr_elf32_property_record_name (struct avr_property_record
*rec
)
4212 case RECORD_ORG_AND_FILL
:
4218 case RECORD_ALIGN_AND_FILL
:
4229 #define ELF_ARCH bfd_arch_avr
4230 #define ELF_TARGET_ID AVR_ELF_DATA
4231 #define ELF_MACHINE_CODE EM_AVR
4232 #define ELF_MACHINE_ALT1 EM_AVR_OLD
4233 #define ELF_MAXPAGESIZE 1
4235 #define TARGET_LITTLE_SYM avr_elf32_vec
4236 #define TARGET_LITTLE_NAME "elf32-avr"
4238 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
4240 #define elf_info_to_howto avr_info_to_howto_rela
4241 #define elf_info_to_howto_rel NULL
4242 #define elf_backend_relocate_section elf32_avr_relocate_section
4243 #define elf_backend_can_gc_sections 1
4244 #define elf_backend_rela_normal 1
4245 #define elf_backend_final_write_processing \
4246 bfd_elf_avr_final_write_processing
4247 #define elf_backend_object_p elf32_avr_object_p
4249 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
4250 #define bfd_elf32_bfd_get_relocated_section_contents \
4251 elf32_avr_get_relocated_section_contents
4252 #define bfd_elf32_new_section_hook elf_avr_new_section_hook
4254 #include "elf32-target.h"