1 /* AVR-specific support for 32-bit ELF
2 Copyright (C) 1999-2016 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 _bfd_error_handler (_("%B: invalid AVR reloc number: %d"), abfd
, r_type
);
961 cache_ptr
->howto
= &elf_avr_howto_table
[r_type
];
965 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation
)
967 return (relocation
>= 0x020000);
970 /* Returns the address of the corresponding stub if there is one.
971 Returns otherwise an address above 0x020000. This function
972 could also be used, if there is no knowledge on the section where
973 the destination is found. */
976 avr_get_stub_addr (bfd_vma srel
,
977 struct elf32_avr_link_hash_table
*htab
)
980 bfd_vma stub_sec_addr
=
981 (htab
->stub_sec
->output_section
->vma
+
982 htab
->stub_sec
->output_offset
);
984 for (sindex
= 0; sindex
< htab
->amt_max_entry_cnt
; sindex
++)
985 if (htab
->amt_destination_addr
[sindex
] == srel
)
986 return htab
->amt_stub_offsets
[sindex
] + stub_sec_addr
;
988 /* Return an address that could not be reached by 16 bit relocs. */
992 /* Perform a diff relocation. Nothing to do, as the difference value is already
993 written into the section's contents. */
995 static bfd_reloc_status_type
996 bfd_elf_avr_diff_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
997 arelent
*reloc_entry ATTRIBUTE_UNUSED
,
998 asymbol
*symbol ATTRIBUTE_UNUSED
,
999 void *data ATTRIBUTE_UNUSED
,
1000 asection
*input_section ATTRIBUTE_UNUSED
,
1001 bfd
*output_bfd ATTRIBUTE_UNUSED
,
1002 char **error_message ATTRIBUTE_UNUSED
)
1004 return bfd_reloc_ok
;
1008 /* Perform a single relocation. By default we use the standard BFD
1009 routines, but a few relocs, we have to do them ourselves. */
1011 static bfd_reloc_status_type
1012 avr_final_link_relocate (reloc_howto_type
* howto
,
1014 asection
* input_section
,
1015 bfd_byte
* contents
,
1016 Elf_Internal_Rela
* rel
,
1018 struct elf32_avr_link_hash_table
* htab
)
1020 bfd_reloc_status_type r
= bfd_reloc_ok
;
1022 bfd_signed_vma srel
;
1023 bfd_signed_vma reloc_addr
;
1024 bfd_boolean use_stubs
= FALSE
;
1025 /* Usually is 0, unless we are generating code for a bootloader. */
1026 bfd_signed_vma base_addr
= htab
->vector_base
;
1028 /* Absolute addr of the reloc in the final excecutable. */
1029 reloc_addr
= rel
->r_offset
+ input_section
->output_section
->vma
1030 + input_section
->output_offset
;
1032 switch (howto
->type
)
1035 contents
+= rel
->r_offset
;
1036 srel
= (bfd_signed_vma
) relocation
;
1037 srel
+= rel
->r_addend
;
1038 srel
-= rel
->r_offset
;
1039 srel
-= 2; /* Branch instructions add 2 to the PC... */
1040 srel
-= (input_section
->output_section
->vma
+
1041 input_section
->output_offset
);
1044 return bfd_reloc_outofrange
;
1045 if (srel
> ((1 << 7) - 1) || (srel
< - (1 << 7)))
1046 return bfd_reloc_overflow
;
1047 x
= bfd_get_16 (input_bfd
, contents
);
1048 x
= (x
& 0xfc07) | (((srel
>> 1) << 3) & 0x3f8);
1049 bfd_put_16 (input_bfd
, x
, contents
);
1052 case R_AVR_13_PCREL
:
1053 contents
+= rel
->r_offset
;
1054 srel
= (bfd_signed_vma
) relocation
;
1055 srel
+= rel
->r_addend
;
1056 srel
-= rel
->r_offset
;
1057 srel
-= 2; /* Branch instructions add 2 to the PC... */
1058 srel
-= (input_section
->output_section
->vma
+
1059 input_section
->output_offset
);
1062 return bfd_reloc_outofrange
;
1064 srel
= avr_relative_distance_considering_wrap_around (srel
);
1066 /* AVR addresses commands as words. */
1069 /* Check for overflow. */
1070 if (srel
< -2048 || srel
> 2047)
1072 /* Relative distance is too large. */
1074 /* Always apply WRAPAROUND for avr2, avr25, and avr4. */
1075 switch (bfd_get_mach (input_bfd
))
1078 case bfd_mach_avr25
:
1083 return bfd_reloc_overflow
;
1087 x
= bfd_get_16 (input_bfd
, contents
);
1088 x
= (x
& 0xf000) | (srel
& 0xfff);
1089 bfd_put_16 (input_bfd
, x
, contents
);
1093 contents
+= rel
->r_offset
;
1094 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1095 x
= bfd_get_16 (input_bfd
, contents
);
1096 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1097 bfd_put_16 (input_bfd
, x
, contents
);
1101 contents
+= rel
->r_offset
;
1102 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1103 if (((srel
> 0) && (srel
& 0xffff) > 255)
1104 || ((srel
< 0) && ((-srel
) & 0xffff) > 128))
1105 /* Remove offset for data/eeprom section. */
1106 return bfd_reloc_overflow
;
1108 x
= bfd_get_16 (input_bfd
, contents
);
1109 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1110 bfd_put_16 (input_bfd
, x
, contents
);
1114 contents
+= rel
->r_offset
;
1115 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1116 if (((srel
& 0xffff) > 63) || (srel
< 0))
1117 /* Remove offset for data/eeprom section. */
1118 return bfd_reloc_overflow
;
1119 x
= bfd_get_16 (input_bfd
, contents
);
1120 x
= (x
& 0xd3f8) | ((srel
& 7) | ((srel
& (3 << 3)) << 7)
1121 | ((srel
& (1 << 5)) << 8));
1122 bfd_put_16 (input_bfd
, x
, contents
);
1126 contents
+= rel
->r_offset
;
1127 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1128 if (((srel
& 0xffff) > 63) || (srel
< 0))
1129 /* Remove offset for data/eeprom section. */
1130 return bfd_reloc_overflow
;
1131 x
= bfd_get_16 (input_bfd
, contents
);
1132 x
= (x
& 0xff30) | (srel
& 0xf) | ((srel
& 0x30) << 2);
1133 bfd_put_16 (input_bfd
, x
, contents
);
1137 contents
+= rel
->r_offset
;
1138 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1139 srel
= (srel
>> 8) & 0xff;
1140 x
= bfd_get_16 (input_bfd
, contents
);
1141 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1142 bfd_put_16 (input_bfd
, x
, contents
);
1146 contents
+= rel
->r_offset
;
1147 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1148 srel
= (srel
>> 16) & 0xff;
1149 x
= bfd_get_16 (input_bfd
, contents
);
1150 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1151 bfd_put_16 (input_bfd
, x
, contents
);
1155 contents
+= rel
->r_offset
;
1156 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1157 srel
= (srel
>> 24) & 0xff;
1158 x
= bfd_get_16 (input_bfd
, contents
);
1159 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1160 bfd_put_16 (input_bfd
, x
, contents
);
1163 case R_AVR_LO8_LDI_NEG
:
1164 contents
+= rel
->r_offset
;
1165 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1167 x
= bfd_get_16 (input_bfd
, contents
);
1168 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1169 bfd_put_16 (input_bfd
, x
, contents
);
1172 case R_AVR_HI8_LDI_NEG
:
1173 contents
+= rel
->r_offset
;
1174 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1176 srel
= (srel
>> 8) & 0xff;
1177 x
= bfd_get_16 (input_bfd
, contents
);
1178 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1179 bfd_put_16 (input_bfd
, x
, contents
);
1182 case R_AVR_HH8_LDI_NEG
:
1183 contents
+= rel
->r_offset
;
1184 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1186 srel
= (srel
>> 16) & 0xff;
1187 x
= bfd_get_16 (input_bfd
, contents
);
1188 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1189 bfd_put_16 (input_bfd
, x
, contents
);
1192 case R_AVR_MS8_LDI_NEG
:
1193 contents
+= rel
->r_offset
;
1194 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1196 srel
= (srel
>> 24) & 0xff;
1197 x
= bfd_get_16 (input_bfd
, contents
);
1198 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1199 bfd_put_16 (input_bfd
, x
, contents
);
1202 case R_AVR_LO8_LDI_GS
:
1203 use_stubs
= (!htab
->no_stubs
);
1205 case R_AVR_LO8_LDI_PM
:
1206 contents
+= rel
->r_offset
;
1207 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1210 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1212 bfd_vma old_srel
= srel
;
1214 /* We need to use the address of the stub instead. */
1215 srel
= avr_get_stub_addr (srel
, htab
);
1217 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1218 "reloc at address 0x%x.\n",
1219 (unsigned int) srel
,
1220 (unsigned int) old_srel
,
1221 (unsigned int) reloc_addr
);
1223 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1224 return bfd_reloc_outofrange
;
1228 return bfd_reloc_outofrange
;
1230 x
= bfd_get_16 (input_bfd
, contents
);
1231 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1232 bfd_put_16 (input_bfd
, x
, contents
);
1235 case R_AVR_HI8_LDI_GS
:
1236 use_stubs
= (!htab
->no_stubs
);
1238 case R_AVR_HI8_LDI_PM
:
1239 contents
+= rel
->r_offset
;
1240 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1243 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1245 bfd_vma old_srel
= srel
;
1247 /* We need to use the address of the stub instead. */
1248 srel
= avr_get_stub_addr (srel
, htab
);
1250 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1251 "reloc at address 0x%x.\n",
1252 (unsigned int) srel
,
1253 (unsigned int) old_srel
,
1254 (unsigned int) reloc_addr
);
1256 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1257 return bfd_reloc_outofrange
;
1261 return bfd_reloc_outofrange
;
1263 srel
= (srel
>> 8) & 0xff;
1264 x
= bfd_get_16 (input_bfd
, contents
);
1265 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1266 bfd_put_16 (input_bfd
, x
, contents
);
1269 case R_AVR_HH8_LDI_PM
:
1270 contents
+= rel
->r_offset
;
1271 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1273 return bfd_reloc_outofrange
;
1275 srel
= (srel
>> 16) & 0xff;
1276 x
= bfd_get_16 (input_bfd
, contents
);
1277 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1278 bfd_put_16 (input_bfd
, x
, contents
);
1281 case R_AVR_LO8_LDI_PM_NEG
:
1282 contents
+= rel
->r_offset
;
1283 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1286 return bfd_reloc_outofrange
;
1288 x
= bfd_get_16 (input_bfd
, contents
);
1289 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1290 bfd_put_16 (input_bfd
, x
, contents
);
1293 case R_AVR_HI8_LDI_PM_NEG
:
1294 contents
+= rel
->r_offset
;
1295 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1298 return bfd_reloc_outofrange
;
1300 srel
= (srel
>> 8) & 0xff;
1301 x
= bfd_get_16 (input_bfd
, contents
);
1302 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1303 bfd_put_16 (input_bfd
, x
, contents
);
1306 case R_AVR_HH8_LDI_PM_NEG
:
1307 contents
+= rel
->r_offset
;
1308 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1311 return bfd_reloc_outofrange
;
1313 srel
= (srel
>> 16) & 0xff;
1314 x
= bfd_get_16 (input_bfd
, contents
);
1315 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1316 bfd_put_16 (input_bfd
, x
, contents
);
1320 contents
+= rel
->r_offset
;
1321 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1323 return bfd_reloc_outofrange
;
1325 x
= bfd_get_16 (input_bfd
, contents
);
1326 x
|= ((srel
& 0x10000) | ((srel
<< 3) & 0x1f00000)) >> 16;
1327 bfd_put_16 (input_bfd
, x
, contents
);
1328 bfd_put_16 (input_bfd
, (bfd_vma
) srel
& 0xffff, contents
+2);
1332 use_stubs
= (!htab
->no_stubs
);
1333 contents
+= rel
->r_offset
;
1334 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1337 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1339 bfd_vma old_srel
= srel
;
1341 /* We need to use the address of the stub instead. */
1342 srel
= avr_get_stub_addr (srel
,htab
);
1344 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1345 "reloc at address 0x%x.\n",
1346 (unsigned int) srel
,
1347 (unsigned int) old_srel
,
1348 (unsigned int) reloc_addr
);
1350 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1351 return bfd_reloc_outofrange
;
1355 return bfd_reloc_outofrange
;
1357 bfd_put_16 (input_bfd
, (bfd_vma
) srel
&0x00ffff, contents
);
1363 /* Nothing to do here, as contents already contains the diff value. */
1367 case R_AVR_LDS_STS_16
:
1368 contents
+= rel
->r_offset
;
1369 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1370 if ((srel
& 0xFFFF) < 0x40 || (srel
& 0xFFFF) > 0xbf)
1371 return bfd_reloc_outofrange
;
1373 x
= bfd_get_16 (input_bfd
, contents
);
1374 x
|= (srel
& 0x0f) | ((srel
& 0x30) << 5) | ((srel
& 0x40) << 2);
1375 bfd_put_16 (input_bfd
, x
, contents
);
1379 contents
+= rel
->r_offset
;
1380 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1381 if ((srel
& 0xffff) > 0x3f)
1382 return bfd_reloc_outofrange
;
1383 x
= bfd_get_16 (input_bfd
, contents
);
1384 x
= (x
& 0xf9f0) | ((srel
& 0x30) << 5) | (srel
& 0x0f);
1385 bfd_put_16 (input_bfd
, x
, contents
);
1389 contents
+= rel
->r_offset
;
1390 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1391 if ((srel
& 0xffff) > 0x1f)
1392 return bfd_reloc_outofrange
;
1393 x
= bfd_get_16 (input_bfd
, contents
);
1394 x
= (x
& 0xff07) | ((srel
& 0x1f) << 3);
1395 bfd_put_16 (input_bfd
, x
, contents
);
1399 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1400 contents
, rel
->r_offset
,
1401 relocation
, rel
->r_addend
);
1407 /* Relocate an AVR ELF section. */
1410 elf32_avr_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1411 struct bfd_link_info
*info
,
1413 asection
*input_section
,
1415 Elf_Internal_Rela
*relocs
,
1416 Elf_Internal_Sym
*local_syms
,
1417 asection
**local_sections
)
1419 Elf_Internal_Shdr
* symtab_hdr
;
1420 struct elf_link_hash_entry
** sym_hashes
;
1421 Elf_Internal_Rela
* rel
;
1422 Elf_Internal_Rela
* relend
;
1423 struct elf32_avr_link_hash_table
* htab
= avr_link_hash_table (info
);
1428 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
1429 sym_hashes
= elf_sym_hashes (input_bfd
);
1430 relend
= relocs
+ input_section
->reloc_count
;
1432 for (rel
= relocs
; rel
< relend
; rel
++)
1434 reloc_howto_type
* howto
;
1435 unsigned long r_symndx
;
1436 Elf_Internal_Sym
* sym
;
1438 struct elf_link_hash_entry
* h
;
1440 bfd_reloc_status_type r
;
1444 r_type
= ELF32_R_TYPE (rel
->r_info
);
1445 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1446 howto
= elf_avr_howto_table
+ r_type
;
1451 if (r_symndx
< symtab_hdr
->sh_info
)
1453 sym
= local_syms
+ r_symndx
;
1454 sec
= local_sections
[r_symndx
];
1455 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1457 name
= bfd_elf_string_from_elf_section
1458 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1459 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1463 bfd_boolean unresolved_reloc
, warned
, ignored
;
1465 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1466 r_symndx
, symtab_hdr
, sym_hashes
,
1468 unresolved_reloc
, warned
, ignored
);
1470 name
= h
->root
.root
.string
;
1473 if (sec
!= NULL
&& discarded_section (sec
))
1474 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1475 rel
, 1, relend
, howto
, 0, contents
);
1477 if (bfd_link_relocatable (info
))
1480 r
= avr_final_link_relocate (howto
, input_bfd
, input_section
,
1481 contents
, rel
, relocation
, htab
);
1483 if (r
!= bfd_reloc_ok
)
1485 const char * msg
= (const char *) NULL
;
1489 case bfd_reloc_overflow
:
1490 (*info
->callbacks
->reloc_overflow
)
1491 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
1492 (bfd_vma
) 0, input_bfd
, input_section
, rel
->r_offset
);
1495 case bfd_reloc_undefined
:
1496 (*info
->callbacks
->undefined_symbol
)
1497 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1500 case bfd_reloc_outofrange
:
1501 msg
= _("internal error: out of range error");
1504 case bfd_reloc_notsupported
:
1505 msg
= _("internal error: unsupported relocation error");
1508 case bfd_reloc_dangerous
:
1509 msg
= _("internal error: dangerous relocation");
1513 msg
= _("internal error: unknown error");
1518 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
1519 input_section
, rel
->r_offset
);
1526 /* The final processing done just before writing out a AVR ELF object
1527 file. This gets the AVR architecture right based on the machine
1531 bfd_elf_avr_final_write_processing (bfd
*abfd
,
1532 bfd_boolean linker ATTRIBUTE_UNUSED
)
1536 switch (bfd_get_mach (abfd
))
1540 val
= E_AVR_MACH_AVR2
;
1544 val
= E_AVR_MACH_AVR1
;
1547 case bfd_mach_avr25
:
1548 val
= E_AVR_MACH_AVR25
;
1552 val
= E_AVR_MACH_AVR3
;
1555 case bfd_mach_avr31
:
1556 val
= E_AVR_MACH_AVR31
;
1559 case bfd_mach_avr35
:
1560 val
= E_AVR_MACH_AVR35
;
1564 val
= E_AVR_MACH_AVR4
;
1568 val
= E_AVR_MACH_AVR5
;
1571 case bfd_mach_avr51
:
1572 val
= E_AVR_MACH_AVR51
;
1576 val
= E_AVR_MACH_AVR6
;
1579 case bfd_mach_avrxmega1
:
1580 val
= E_AVR_MACH_XMEGA1
;
1583 case bfd_mach_avrxmega2
:
1584 val
= E_AVR_MACH_XMEGA2
;
1587 case bfd_mach_avrxmega3
:
1588 val
= E_AVR_MACH_XMEGA3
;
1591 case bfd_mach_avrxmega4
:
1592 val
= E_AVR_MACH_XMEGA4
;
1595 case bfd_mach_avrxmega5
:
1596 val
= E_AVR_MACH_XMEGA5
;
1599 case bfd_mach_avrxmega6
:
1600 val
= E_AVR_MACH_XMEGA6
;
1603 case bfd_mach_avrxmega7
:
1604 val
= E_AVR_MACH_XMEGA7
;
1607 case bfd_mach_avrtiny
:
1608 val
= E_AVR_MACH_AVRTINY
;
1612 elf_elfheader (abfd
)->e_machine
= EM_AVR
;
1613 elf_elfheader (abfd
)->e_flags
&= ~ EF_AVR_MACH
;
1614 elf_elfheader (abfd
)->e_flags
|= val
;
1617 /* Set the right machine number. */
1620 elf32_avr_object_p (bfd
*abfd
)
1622 unsigned int e_set
= bfd_mach_avr2
;
1624 if (elf_elfheader (abfd
)->e_machine
== EM_AVR
1625 || elf_elfheader (abfd
)->e_machine
== EM_AVR_OLD
)
1627 int e_mach
= elf_elfheader (abfd
)->e_flags
& EF_AVR_MACH
;
1632 case E_AVR_MACH_AVR2
:
1633 e_set
= bfd_mach_avr2
;
1636 case E_AVR_MACH_AVR1
:
1637 e_set
= bfd_mach_avr1
;
1640 case E_AVR_MACH_AVR25
:
1641 e_set
= bfd_mach_avr25
;
1644 case E_AVR_MACH_AVR3
:
1645 e_set
= bfd_mach_avr3
;
1648 case E_AVR_MACH_AVR31
:
1649 e_set
= bfd_mach_avr31
;
1652 case E_AVR_MACH_AVR35
:
1653 e_set
= bfd_mach_avr35
;
1656 case E_AVR_MACH_AVR4
:
1657 e_set
= bfd_mach_avr4
;
1660 case E_AVR_MACH_AVR5
:
1661 e_set
= bfd_mach_avr5
;
1664 case E_AVR_MACH_AVR51
:
1665 e_set
= bfd_mach_avr51
;
1668 case E_AVR_MACH_AVR6
:
1669 e_set
= bfd_mach_avr6
;
1672 case E_AVR_MACH_XMEGA1
:
1673 e_set
= bfd_mach_avrxmega1
;
1676 case E_AVR_MACH_XMEGA2
:
1677 e_set
= bfd_mach_avrxmega2
;
1680 case E_AVR_MACH_XMEGA3
:
1681 e_set
= bfd_mach_avrxmega3
;
1684 case E_AVR_MACH_XMEGA4
:
1685 e_set
= bfd_mach_avrxmega4
;
1688 case E_AVR_MACH_XMEGA5
:
1689 e_set
= bfd_mach_avrxmega5
;
1692 case E_AVR_MACH_XMEGA6
:
1693 e_set
= bfd_mach_avrxmega6
;
1696 case E_AVR_MACH_XMEGA7
:
1697 e_set
= bfd_mach_avrxmega7
;
1700 case E_AVR_MACH_AVRTINY
:
1701 e_set
= bfd_mach_avrtiny
;
1705 return bfd_default_set_arch_mach (abfd
, bfd_arch_avr
,
1709 /* Returns whether the relocation type passed is a diff reloc. */
1712 elf32_avr_is_diff_reloc (Elf_Internal_Rela
*irel
)
1714 return (ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF8
1715 ||ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF16
1716 || ELF32_R_TYPE (irel
->r_info
) == R_AVR_DIFF32
);
1719 /* Reduce the diff value written in the section by count if the shrinked
1720 insn address happens to fall between the two symbols for which this
1721 diff reloc was emitted. */
1724 elf32_avr_adjust_diff_reloc_value (bfd
*abfd
,
1725 struct bfd_section
*isec
,
1726 Elf_Internal_Rela
*irel
,
1728 bfd_vma shrinked_insn_address
,
1731 unsigned char *reloc_contents
= NULL
;
1732 unsigned char *isec_contents
= elf_section_data (isec
)->this_hdr
.contents
;
1733 if (isec_contents
== NULL
)
1735 if (! bfd_malloc_and_get_section (abfd
, isec
, &isec_contents
))
1738 elf_section_data (isec
)->this_hdr
.contents
= isec_contents
;
1741 reloc_contents
= isec_contents
+ irel
->r_offset
;
1743 /* Read value written in object file. */
1745 switch (ELF32_R_TYPE (irel
->r_info
))
1749 x
= *reloc_contents
;
1754 x
= bfd_get_16 (abfd
, reloc_contents
);
1759 x
= bfd_get_32 (abfd
, reloc_contents
);
1768 /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
1769 into the object file at the reloc offset. sym2's logical value is
1770 symval (<start_of_section>) + reloc addend. Compute the start and end
1771 addresses and check if the shrinked insn falls between sym1 and sym2. */
1773 bfd_vma end_address
= symval
+ irel
->r_addend
;
1774 bfd_vma start_address
= end_address
- x
;
1776 /* Reduce the diff value by count bytes and write it back into section
1779 if (shrinked_insn_address
>= start_address
1780 && shrinked_insn_address
<= end_address
)
1782 switch (ELF32_R_TYPE (irel
->r_info
))
1786 *reloc_contents
= (x
- count
);
1791 bfd_put_16 (abfd
, (x
- count
) & 0xFFFF, reloc_contents
);
1796 bfd_put_32 (abfd
, (x
- count
) & 0xFFFFFFFF, reloc_contents
);
1808 /* Delete some bytes from a section while changing the size of an instruction.
1809 The parameter "addr" denotes the section-relative offset pointing just
1810 behind the shrinked instruction. "addr+count" point at the first
1811 byte just behind the original unshrinked instruction. */
1814 elf32_avr_relax_delete_bytes (bfd
*abfd
,
1819 Elf_Internal_Shdr
*symtab_hdr
;
1820 unsigned int sec_shndx
;
1822 Elf_Internal_Rela
*irel
, *irelend
;
1823 Elf_Internal_Sym
*isym
;
1824 Elf_Internal_Sym
*isymbuf
= NULL
;
1825 bfd_vma toaddr
, reloc_toaddr
;
1826 struct elf_link_hash_entry
**sym_hashes
;
1827 struct elf_link_hash_entry
**end_hashes
;
1828 unsigned int symcount
;
1829 struct avr_relax_info
*relax_info
;
1830 struct avr_property_record
*prop_record
= NULL
;
1831 bfd_boolean did_shrink
= FALSE
;
1833 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1834 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1835 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1836 relax_info
= get_avr_relax_info (sec
);
1840 if (relax_info
->records
.count
> 0)
1842 /* There should be no property record within the range of deleted
1843 bytes, however, there might be a property record for ADDR, this is
1844 how we handle alignment directives.
1845 Find the next (if any) property record after the deleted bytes. */
1848 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
1850 bfd_vma offset
= relax_info
->records
.items
[i
].offset
;
1852 BFD_ASSERT (offset
<= addr
|| offset
>= (addr
+ count
));
1853 if (offset
>= (addr
+ count
))
1855 prop_record
= &relax_info
->records
.items
[i
];
1862 /* We need to look at all relocs with offsets less than toaddr. prop
1863 records handling adjusts toaddr downwards to avoid moving syms at the
1864 address of the property record, but all relocs with offsets between addr
1865 and the current value of toaddr need to have their offsets adjusted.
1866 Assume addr = 0, toaddr = 4 and count = 2. After prop records handling,
1867 toaddr becomes 2, but relocs with offsets 2 and 3 still need to be
1868 adjusted (to 0 and 1 respectively), as the first 2 bytes are now gone.
1869 So record the current value of toaddr here, and use it when adjusting
1871 reloc_toaddr
= toaddr
;
1873 irel
= elf_section_data (sec
)->relocs
;
1874 irelend
= irel
+ sec
->reloc_count
;
1876 /* Actually delete the bytes. */
1877 if (toaddr
- addr
- count
> 0)
1879 memmove (contents
+ addr
, contents
+ addr
+ count
,
1880 (size_t) (toaddr
- addr
- count
));
1883 if (prop_record
== NULL
)
1890 /* Use the property record to fill in the bytes we've opened up. */
1892 switch (prop_record
->type
)
1894 case RECORD_ORG_AND_FILL
:
1895 fill
= prop_record
->data
.org
.fill
;
1899 case RECORD_ALIGN_AND_FILL
:
1900 fill
= prop_record
->data
.align
.fill
;
1903 prop_record
->data
.align
.preceding_deleted
+= count
;
1906 /* If toaddr == (addr + count), then we didn't delete anything, yet
1907 we fill count bytes backwards from toaddr. This is still ok - we
1908 end up overwriting the bytes we would have deleted. We just need
1909 to remember we didn't delete anything i.e. don't set did_shrink,
1910 so that we don't corrupt reloc offsets or symbol values.*/
1911 memset (contents
+ toaddr
- count
, fill
, count
);
1913 /* Adjust the TOADDR to avoid moving symbols located at the address
1914 of the property record, which has not moved. */
1921 /* Adjust all the reloc addresses. */
1922 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1924 bfd_vma old_reloc_address
;
1926 old_reloc_address
= (sec
->output_section
->vma
1927 + sec
->output_offset
+ irel
->r_offset
);
1929 /* Get the new reloc address. */
1930 if ((irel
->r_offset
> addr
1931 && irel
->r_offset
< reloc_toaddr
))
1934 printf ("Relocation at address 0x%x needs to be moved.\n"
1935 "Old section offset: 0x%x, New section offset: 0x%x \n",
1936 (unsigned int) old_reloc_address
,
1937 (unsigned int) irel
->r_offset
,
1938 (unsigned int) ((irel
->r_offset
) - count
));
1940 irel
->r_offset
-= count
;
1945 /* The reloc's own addresses are now ok. However, we need to readjust
1946 the reloc's addend, i.e. the reloc's value if two conditions are met:
1947 1.) the reloc is relative to a symbol in this section that
1948 is located in front of the shrinked instruction
1949 2.) symbol plus addend end up behind the shrinked instruction.
1951 The most common case where this happens are relocs relative to
1952 the section-start symbol.
1954 This step needs to be done for all of the sections of the bfd. */
1957 struct bfd_section
*isec
;
1959 for (isec
= abfd
->sections
; isec
; isec
= isec
->next
)
1962 bfd_vma shrinked_insn_address
;
1964 if (isec
->reloc_count
== 0)
1967 shrinked_insn_address
= (sec
->output_section
->vma
1968 + sec
->output_offset
+ addr
- count
);
1970 irel
= elf_section_data (isec
)->relocs
;
1971 /* PR 12161: Read in the relocs for this section if necessary. */
1973 irel
= _bfd_elf_link_read_relocs (abfd
, isec
, NULL
, NULL
, TRUE
);
1975 for (irelend
= irel
+ isec
->reloc_count
;
1979 /* Read this BFD's local symbols if we haven't done
1981 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1983 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1984 if (isymbuf
== NULL
)
1985 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1986 symtab_hdr
->sh_info
, 0,
1988 if (isymbuf
== NULL
)
1992 /* Get the value of the symbol referred to by the reloc. */
1993 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1995 /* A local symbol. */
1998 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1999 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2000 symval
= isym
->st_value
;
2001 /* If the reloc is absolute, it will not have
2002 a symbol or section associated with it. */
2005 symval
+= sym_sec
->output_section
->vma
2006 + sym_sec
->output_offset
;
2009 printf ("Checking if the relocation's "
2010 "addend needs corrections.\n"
2011 "Address of anchor symbol: 0x%x \n"
2012 "Address of relocation target: 0x%x \n"
2013 "Address of relaxed insn: 0x%x \n",
2014 (unsigned int) symval
,
2015 (unsigned int) (symval
+ irel
->r_addend
),
2016 (unsigned int) shrinked_insn_address
);
2018 if (symval
<= shrinked_insn_address
2019 && (symval
+ irel
->r_addend
) > shrinked_insn_address
)
2021 if (elf32_avr_is_diff_reloc (irel
))
2023 elf32_avr_adjust_diff_reloc_value (abfd
, isec
, irel
,
2025 shrinked_insn_address
,
2029 irel
->r_addend
-= count
;
2032 printf ("Relocation's addend needed to be fixed \n");
2035 /* else...Reference symbol is absolute. No adjustment needed. */
2037 /* else...Reference symbol is extern. No need for adjusting
2043 /* Adjust the local symbols defined in this section. */
2044 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2045 /* Fix PR 9841, there may be no local symbols. */
2048 Elf_Internal_Sym
*isymend
;
2050 isymend
= isym
+ symtab_hdr
->sh_info
;
2051 for (; isym
< isymend
; isym
++)
2053 if (isym
->st_shndx
== sec_shndx
)
2055 if (isym
->st_value
> addr
2056 && isym
->st_value
<= toaddr
)
2057 isym
->st_value
-= count
;
2059 if (isym
->st_value
<= addr
2060 && isym
->st_value
+ isym
->st_size
> addr
)
2062 /* If this assert fires then we have a symbol that ends
2063 part way through an instruction. Does that make
2065 BFD_ASSERT (isym
->st_value
+ isym
->st_size
>= addr
+ count
);
2066 isym
->st_size
-= count
;
2072 /* Now adjust the global symbols defined in this section. */
2073 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2074 - symtab_hdr
->sh_info
);
2075 sym_hashes
= elf_sym_hashes (abfd
);
2076 end_hashes
= sym_hashes
+ symcount
;
2077 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2079 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
2080 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2081 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
2082 && sym_hash
->root
.u
.def
.section
== sec
)
2084 if (sym_hash
->root
.u
.def
.value
> addr
2085 && sym_hash
->root
.u
.def
.value
<= toaddr
)
2086 sym_hash
->root
.u
.def
.value
-= count
;
2088 if (sym_hash
->root
.u
.def
.value
<= addr
2089 && (sym_hash
->root
.u
.def
.value
+ sym_hash
->size
> addr
))
2091 /* If this assert fires then we have a symbol that ends
2092 part way through an instruction. Does that make
2094 BFD_ASSERT (sym_hash
->root
.u
.def
.value
+ sym_hash
->size
2096 sym_hash
->size
-= count
;
2104 static Elf_Internal_Sym
*
2105 retrieve_local_syms (bfd
*input_bfd
)
2107 Elf_Internal_Shdr
*symtab_hdr
;
2108 Elf_Internal_Sym
*isymbuf
;
2111 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2112 locsymcount
= symtab_hdr
->sh_info
;
2114 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2115 if (isymbuf
== NULL
&& locsymcount
!= 0)
2116 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
, locsymcount
, 0,
2119 /* Save the symbols for this input file so they won't be read again. */
2120 if (isymbuf
&& isymbuf
!= (Elf_Internal_Sym
*) symtab_hdr
->contents
)
2121 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2126 /* Get the input section for a given symbol index.
2128 . a section symbol, return the section;
2129 . a common symbol, return the common section;
2130 . an undefined symbol, return the undefined section;
2131 . an indirect symbol, follow the links;
2132 . an absolute value, return the absolute section. */
2135 get_elf_r_symndx_section (bfd
*abfd
, unsigned long r_symndx
)
2137 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2138 asection
*target_sec
= NULL
;
2139 if (r_symndx
< symtab_hdr
->sh_info
)
2141 Elf_Internal_Sym
*isymbuf
;
2142 unsigned int section_index
;
2144 isymbuf
= retrieve_local_syms (abfd
);
2145 section_index
= isymbuf
[r_symndx
].st_shndx
;
2147 if (section_index
== SHN_UNDEF
)
2148 target_sec
= bfd_und_section_ptr
;
2149 else if (section_index
== SHN_ABS
)
2150 target_sec
= bfd_abs_section_ptr
;
2151 else if (section_index
== SHN_COMMON
)
2152 target_sec
= bfd_com_section_ptr
;
2154 target_sec
= bfd_section_from_elf_index (abfd
, section_index
);
2158 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
2159 struct elf_link_hash_entry
*h
= elf_sym_hashes (abfd
)[indx
];
2161 while (h
->root
.type
== bfd_link_hash_indirect
2162 || h
->root
.type
== bfd_link_hash_warning
)
2163 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2165 switch (h
->root
.type
)
2167 case bfd_link_hash_defined
:
2168 case bfd_link_hash_defweak
:
2169 target_sec
= h
->root
.u
.def
.section
;
2171 case bfd_link_hash_common
:
2172 target_sec
= bfd_com_section_ptr
;
2174 case bfd_link_hash_undefined
:
2175 case bfd_link_hash_undefweak
:
2176 target_sec
= bfd_und_section_ptr
;
2178 default: /* New indirect warning. */
2179 target_sec
= bfd_und_section_ptr
;
2186 /* Get the section-relative offset for a symbol number. */
2189 get_elf_r_symndx_offset (bfd
*abfd
, unsigned long r_symndx
)
2191 Elf_Internal_Shdr
*symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2194 if (r_symndx
< symtab_hdr
->sh_info
)
2196 Elf_Internal_Sym
*isymbuf
;
2197 isymbuf
= retrieve_local_syms (abfd
);
2198 offset
= isymbuf
[r_symndx
].st_value
;
2202 unsigned long indx
= r_symndx
- symtab_hdr
->sh_info
;
2203 struct elf_link_hash_entry
*h
=
2204 elf_sym_hashes (abfd
)[indx
];
2206 while (h
->root
.type
== bfd_link_hash_indirect
2207 || h
->root
.type
== bfd_link_hash_warning
)
2208 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
2209 if (h
->root
.type
== bfd_link_hash_defined
2210 || h
->root
.type
== bfd_link_hash_defweak
)
2211 offset
= h
->root
.u
.def
.value
;
2216 /* Iterate over the property records in R_LIST, and copy each record into
2217 the list of records within the relaxation information for the section to
2218 which the record applies. */
2221 avr_elf32_assign_records_to_sections (struct avr_property_record_list
*r_list
)
2225 for (i
= 0; i
< r_list
->record_count
; ++i
)
2227 struct avr_relax_info
*relax_info
;
2229 relax_info
= get_avr_relax_info (r_list
->records
[i
].section
);
2230 BFD_ASSERT (relax_info
!= NULL
);
2232 if (relax_info
->records
.count
2233 == relax_info
->records
.allocated
)
2235 /* Allocate more space. */
2238 relax_info
->records
.allocated
+= 10;
2239 size
= (sizeof (struct avr_property_record
)
2240 * relax_info
->records
.allocated
);
2241 relax_info
->records
.items
2242 = bfd_realloc (relax_info
->records
.items
, size
);
2245 memcpy (&relax_info
->records
.items
[relax_info
->records
.count
],
2246 &r_list
->records
[i
],
2247 sizeof (struct avr_property_record
));
2248 relax_info
->records
.count
++;
2252 /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the
2253 ordering callback from QSORT. */
2256 avr_property_record_compare (const void *ap
, const void *bp
)
2258 const struct avr_property_record
*a
2259 = (struct avr_property_record
*) ap
;
2260 const struct avr_property_record
*b
2261 = (struct avr_property_record
*) bp
;
2263 if (a
->offset
!= b
->offset
)
2264 return (a
->offset
- b
->offset
);
2266 if (a
->section
!= b
->section
)
2267 return (bfd_get_section_vma (a
->section
->owner
, a
->section
)
2268 - bfd_get_section_vma (b
->section
->owner
, b
->section
));
2270 return (a
->type
- b
->type
);
2273 /* Load all of the avr property sections from all of the bfd objects
2274 referenced from LINK_INFO. All of the records within each property
2275 section are assigned to the STRUCT AVR_RELAX_INFO within the section
2276 specific data of the appropriate section. */
2279 avr_load_all_property_sections (struct bfd_link_info
*link_info
)
2284 /* Initialize the per-section relaxation info. */
2285 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2286 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2288 init_avr_relax_info (sec
);
2291 /* Load the descriptor tables from .avr.prop sections. */
2292 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2294 struct avr_property_record_list
*r_list
;
2296 r_list
= avr_elf32_load_property_records (abfd
);
2298 avr_elf32_assign_records_to_sections (r_list
);
2303 /* Now, for every section, ensure that the descriptor list in the
2304 relaxation data is sorted by ascending offset within the section. */
2305 for (abfd
= link_info
->input_bfds
; abfd
!= NULL
; abfd
= abfd
->link
.next
)
2306 for (sec
= abfd
->sections
; sec
!= NULL
; sec
= sec
->next
)
2308 struct avr_relax_info
*relax_info
= get_avr_relax_info (sec
);
2309 if (relax_info
&& relax_info
->records
.count
> 0)
2313 qsort (relax_info
->records
.items
,
2314 relax_info
->records
.count
,
2315 sizeof (struct avr_property_record
),
2316 avr_property_record_compare
);
2318 /* For debug purposes, list all the descriptors. */
2319 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
2321 switch (relax_info
->records
.items
[i
].type
)
2325 case RECORD_ORG_AND_FILL
:
2329 case RECORD_ALIGN_AND_FILL
:
2337 /* This function handles relaxing for the avr.
2338 Many important relaxing opportunities within functions are already
2339 realized by the compiler itself.
2340 Here we try to replace call (4 bytes) -> rcall (2 bytes)
2341 and jump -> rjmp (safes also 2 bytes).
2342 As well we now optimize seqences of
2343 - call/rcall function
2348 . In case that within a sequence
2351 the ret could no longer be reached it is optimized away. In order
2352 to check if the ret is no longer needed, it is checked that the ret's address
2353 is not the target of a branch or jump within the same section, it is checked
2354 that there is no skip instruction before the jmp/rjmp and that there
2355 is no local or global label place at the address of the ret.
2357 We refrain from relaxing within sections ".vectors" and
2358 ".jumptables" in order to maintain the position of the instructions.
2359 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
2360 if possible. (In future one could possibly use the space of the nop
2361 for the first instruction of the irq service function.
2363 The .jumptables sections is meant to be used for a future tablejump variant
2364 for the devices with 3-byte program counter where the table itself
2365 contains 4-byte jump instructions whose relative offset must not
2369 elf32_avr_relax_section (bfd
*abfd
,
2371 struct bfd_link_info
*link_info
,
2374 Elf_Internal_Shdr
*symtab_hdr
;
2375 Elf_Internal_Rela
*internal_relocs
;
2376 Elf_Internal_Rela
*irel
, *irelend
;
2377 bfd_byte
*contents
= NULL
;
2378 Elf_Internal_Sym
*isymbuf
= NULL
;
2379 struct elf32_avr_link_hash_table
*htab
;
2380 static bfd_boolean relaxation_initialised
= FALSE
;
2382 if (!relaxation_initialised
)
2384 relaxation_initialised
= TRUE
;
2386 /* Load entries from the .avr.prop sections. */
2387 avr_load_all_property_sections (link_info
);
2390 /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
2391 relaxing. Such shrinking can cause issues for the sections such
2392 as .vectors and .jumptables. Instead the unused bytes should be
2393 filled with nop instructions. */
2394 bfd_boolean shrinkable
= TRUE
;
2396 if (!strcmp (sec
->name
,".vectors")
2397 || !strcmp (sec
->name
,".jumptables"))
2400 if (bfd_link_relocatable (link_info
))
2401 (*link_info
->callbacks
->einfo
)
2402 (_("%P%F: --relax and -r may not be used together\n"));
2404 htab
= avr_link_hash_table (link_info
);
2408 /* Assume nothing changes. */
2411 if ((!htab
->no_stubs
) && (sec
== htab
->stub_sec
))
2413 /* We are just relaxing the stub section.
2414 Let's calculate the size needed again. */
2415 bfd_size_type last_estimated_stub_section_size
= htab
->stub_sec
->size
;
2418 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
2419 (int) last_estimated_stub_section_size
);
2421 elf32_avr_size_stubs (htab
->stub_sec
->output_section
->owner
,
2424 /* Check if the number of trampolines changed. */
2425 if (last_estimated_stub_section_size
!= htab
->stub_sec
->size
)
2429 printf ("Size of stub section after this pass: %i\n",
2430 (int) htab
->stub_sec
->size
);
2435 /* We don't have to do anything for a relocatable link, if
2436 this section does not have relocs, or if this is not a
2438 if (bfd_link_relocatable (link_info
)
2439 || (sec
->flags
& SEC_RELOC
) == 0
2440 || sec
->reloc_count
== 0
2441 || (sec
->flags
& SEC_CODE
) == 0)
2444 /* Check if the object file to relax uses internal symbols so that we
2445 could fix up the relocations. */
2446 if (!(elf_elfheader (abfd
)->e_flags
& EF_AVR_LINKRELAX_PREPARED
))
2449 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2451 /* Get a copy of the native relocations. */
2452 internal_relocs
= (_bfd_elf_link_read_relocs
2453 (abfd
, sec
, NULL
, NULL
, link_info
->keep_memory
));
2454 if (internal_relocs
== NULL
)
2457 /* Walk through the relocs looking for relaxing opportunities. */
2458 irelend
= internal_relocs
+ sec
->reloc_count
;
2459 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2463 if ( ELF32_R_TYPE (irel
->r_info
) != R_AVR_13_PCREL
2464 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_7_PCREL
2465 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_CALL
)
2468 /* Get the section contents if we haven't done so already. */
2469 if (contents
== NULL
)
2471 /* Get cached copy if it exists. */
2472 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2473 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2476 /* Go get them off disk. */
2477 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
2482 /* Read this BFD's local symbols if we haven't done so already. */
2483 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2485 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2486 if (isymbuf
== NULL
)
2487 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2488 symtab_hdr
->sh_info
, 0,
2490 if (isymbuf
== NULL
)
2495 /* Get the value of the symbol referred to by the reloc. */
2496 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2498 /* A local symbol. */
2499 Elf_Internal_Sym
*isym
;
2502 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
2503 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2504 symval
= isym
->st_value
;
2505 /* If the reloc is absolute, it will not have
2506 a symbol or section associated with it. */
2508 symval
+= sym_sec
->output_section
->vma
2509 + sym_sec
->output_offset
;
2514 struct elf_link_hash_entry
*h
;
2516 /* An external symbol. */
2517 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
2518 h
= elf_sym_hashes (abfd
)[indx
];
2519 BFD_ASSERT (h
!= NULL
);
2520 if (h
->root
.type
!= bfd_link_hash_defined
2521 && h
->root
.type
!= bfd_link_hash_defweak
)
2522 /* This appears to be a reference to an undefined
2523 symbol. Just ignore it--it will be caught by the
2524 regular reloc processing. */
2527 symval
= (h
->root
.u
.def
.value
2528 + h
->root
.u
.def
.section
->output_section
->vma
2529 + h
->root
.u
.def
.section
->output_offset
);
2532 /* For simplicity of coding, we are going to modify the section
2533 contents, the section relocs, and the BFD symbol table. We
2534 must tell the rest of the code not to free up this
2535 information. It would be possible to instead create a table
2536 of changes which have to be made, as is done in coff-mips.c;
2537 that would be more work, but would require less memory when
2538 the linker is run. */
2539 switch (ELF32_R_TYPE (irel
->r_info
))
2541 /* Try to turn a 22-bit absolute call/jump into an 13-bit
2542 pc-relative rcall/rjmp. */
2545 bfd_vma value
= symval
+ irel
->r_addend
;
2547 int distance_short_enough
= 0;
2549 /* Get the address of this instruction. */
2550 dot
= (sec
->output_section
->vma
2551 + sec
->output_offset
+ irel
->r_offset
);
2553 /* Compute the distance from this insn to the branch target. */
2556 /* Check if the gap falls in the range that can be accommodated
2557 in 13bits signed (It is 12bits when encoded, as we deal with
2558 word addressing). */
2559 if (!shrinkable
&& ((int) gap
>= -4096 && (int) gap
<= 4095))
2560 distance_short_enough
= 1;
2561 /* If shrinkable, then we can check for a range of distance which
2562 is two bytes farther on both the directions because the call
2563 or jump target will be closer by two bytes after the
2565 else if (shrinkable
&& ((int) gap
>= -4094 && (int) gap
<= 4097))
2566 distance_short_enough
= 1;
2568 /* Here we handle the wrap-around case. E.g. for a 16k device
2569 we could use a rjmp to jump from address 0x100 to 0x3d00!
2570 In order to make this work properly, we need to fill the
2571 vaiable avr_pc_wrap_around with the appropriate value.
2572 I.e. 0x4000 for a 16k device. */
2574 /* Shrinking the code size makes the gaps larger in the
2575 case of wrap-arounds. So we use a heuristical safety
2576 margin to avoid that during relax the distance gets
2577 again too large for the short jumps. Let's assume
2578 a typical code-size reduction due to relax for a
2579 16k device of 600 bytes. So let's use twice the
2580 typical value as safety margin. */
2584 int assumed_shrink
= 600;
2585 if (avr_pc_wrap_around
> 0x4000)
2586 assumed_shrink
= 900;
2588 safety_margin
= 2 * assumed_shrink
;
2590 rgap
= avr_relative_distance_considering_wrap_around (gap
);
2592 if (rgap
>= (-4092 + safety_margin
)
2593 && rgap
<= (4094 - safety_margin
))
2594 distance_short_enough
= 1;
2597 if (distance_short_enough
)
2599 unsigned char code_msb
;
2600 unsigned char code_lsb
;
2603 printf ("shrinking jump/call instruction at address 0x%x"
2604 " in section %s\n\n",
2605 (int) dot
, sec
->name
);
2607 /* Note that we've changed the relocs, section contents,
2609 elf_section_data (sec
)->relocs
= internal_relocs
;
2610 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2611 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2613 /* Get the instruction code for relaxing. */
2614 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
);
2615 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
2617 /* Mask out the relocation bits. */
2620 if (code_msb
== 0x94 && code_lsb
== 0x0E)
2622 /* we are changing call -> rcall . */
2623 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
2624 bfd_put_8 (abfd
, 0xD0, contents
+ irel
->r_offset
+ 1);
2626 else if (code_msb
== 0x94 && code_lsb
== 0x0C)
2628 /* we are changeing jump -> rjmp. */
2629 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
2630 bfd_put_8 (abfd
, 0xC0, contents
+ irel
->r_offset
+ 1);
2635 /* Fix the relocation's type. */
2636 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2639 /* We should not modify the ordering if 'shrinkable' is
2643 /* Let's insert a nop. */
2644 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 2);
2645 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 3);
2649 /* Delete two bytes of data. */
2650 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
2651 irel
->r_offset
+ 2, 2))
2654 /* That will change things, so, we should relax again.
2655 Note that this is not required, and it may be slow. */
2663 unsigned char code_msb
;
2664 unsigned char code_lsb
;
2667 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
2668 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 0);
2670 /* Get the address of this instruction. */
2671 dot
= (sec
->output_section
->vma
2672 + sec
->output_offset
+ irel
->r_offset
);
2674 /* Here we look for rcall/ret or call/ret sequences that could be
2675 safely replaced by rjmp/ret or jmp/ret. */
2676 if (((code_msb
& 0xf0) == 0xd0)
2677 && avr_replace_call_ret_sequences
)
2679 /* This insn is a rcall. */
2680 unsigned char next_insn_msb
= 0;
2681 unsigned char next_insn_lsb
= 0;
2683 if (irel
->r_offset
+ 3 < sec
->size
)
2686 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 3);
2688 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 2);
2691 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2693 /* The next insn is a ret. We now convert the rcall insn
2694 into a rjmp instruction. */
2696 bfd_put_8 (abfd
, code_msb
, contents
+ irel
->r_offset
+ 1);
2698 printf ("converted rcall/ret sequence at address 0x%x"
2699 " into rjmp/ret sequence. Section is %s\n\n",
2700 (int) dot
, sec
->name
);
2705 else if ((0x94 == (code_msb
& 0xfe))
2706 && (0x0e == (code_lsb
& 0x0e))
2707 && avr_replace_call_ret_sequences
)
2709 /* This insn is a call. */
2710 unsigned char next_insn_msb
= 0;
2711 unsigned char next_insn_lsb
= 0;
2713 if (irel
->r_offset
+ 5 < sec
->size
)
2716 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 5);
2718 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 4);
2721 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2723 /* The next insn is a ret. We now convert the call insn
2724 into a jmp instruction. */
2727 bfd_put_8 (abfd
, code_lsb
, contents
+ irel
->r_offset
);
2729 printf ("converted call/ret sequence at address 0x%x"
2730 " into jmp/ret sequence. Section is %s\n\n",
2731 (int) dot
, sec
->name
);
2736 else if ((0xc0 == (code_msb
& 0xf0))
2737 || ((0x94 == (code_msb
& 0xfe))
2738 && (0x0c == (code_lsb
& 0x0e))))
2740 /* This insn is a rjmp or a jmp. */
2741 unsigned char next_insn_msb
= 0;
2742 unsigned char next_insn_lsb
= 0;
2745 if (0xc0 == (code_msb
& 0xf0))
2746 insn_size
= 2; /* rjmp insn */
2748 insn_size
= 4; /* jmp insn */
2750 if (irel
->r_offset
+ insn_size
+ 1 < sec
->size
)
2753 bfd_get_8 (abfd
, contents
+ irel
->r_offset
2756 bfd_get_8 (abfd
, contents
+ irel
->r_offset
2760 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
2762 /* The next insn is a ret. We possibly could delete
2763 this ret. First we need to check for preceding
2764 sbis/sbic/sbrs or cpse "skip" instructions. */
2766 int there_is_preceding_non_skip_insn
= 1;
2767 bfd_vma address_of_ret
;
2769 address_of_ret
= dot
+ insn_size
;
2771 if (debug_relax
&& (insn_size
== 2))
2772 printf ("found rjmp / ret sequence at address 0x%x\n",
2774 if (debug_relax
&& (insn_size
== 4))
2775 printf ("found jmp / ret sequence at address 0x%x\n",
2778 /* We have to make sure that there is a preceding insn. */
2779 if (irel
->r_offset
>= 2)
2781 unsigned char preceding_msb
;
2782 unsigned char preceding_lsb
;
2785 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2787 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2790 if (0x99 == preceding_msb
)
2791 there_is_preceding_non_skip_insn
= 0;
2794 if (0x9b == preceding_msb
)
2795 there_is_preceding_non_skip_insn
= 0;
2798 if ((0xfc == (preceding_msb
& 0xfe)
2799 && (0x00 == (preceding_lsb
& 0x08))))
2800 there_is_preceding_non_skip_insn
= 0;
2803 if ((0xfe == (preceding_msb
& 0xfe)
2804 && (0x00 == (preceding_lsb
& 0x08))))
2805 there_is_preceding_non_skip_insn
= 0;
2808 if (0x10 == (preceding_msb
& 0xfc))
2809 there_is_preceding_non_skip_insn
= 0;
2811 if (there_is_preceding_non_skip_insn
== 0)
2813 printf ("preceding skip insn prevents deletion of"
2814 " ret insn at Addy 0x%x in section %s\n",
2815 (int) dot
+ 2, sec
->name
);
2819 /* There is no previous instruction. */
2820 there_is_preceding_non_skip_insn
= 0;
2823 if (there_is_preceding_non_skip_insn
)
2825 /* We now only have to make sure that there is no
2826 local label defined at the address of the ret
2827 instruction and that there is no local relocation
2828 in this section pointing to the ret. */
2830 int deleting_ret_is_safe
= 1;
2831 unsigned int section_offset_of_ret_insn
=
2832 irel
->r_offset
+ insn_size
;
2833 Elf_Internal_Sym
*isym
, *isymend
;
2834 unsigned int sec_shndx
;
2835 struct bfd_section
*isec
;
2838 _bfd_elf_section_from_bfd_section (abfd
, sec
);
2840 /* Check for local symbols. */
2841 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2842 isymend
= isym
+ symtab_hdr
->sh_info
;
2843 /* PR 6019: There may not be any local symbols. */
2844 for (; isym
!= NULL
&& isym
< isymend
; isym
++)
2846 if (isym
->st_value
== section_offset_of_ret_insn
2847 && isym
->st_shndx
== sec_shndx
)
2849 deleting_ret_is_safe
= 0;
2851 printf ("local label prevents deletion of ret "
2852 "insn at address 0x%x\n",
2853 (int) dot
+ insn_size
);
2857 /* Now check for global symbols. */
2860 struct elf_link_hash_entry
**sym_hashes
;
2861 struct elf_link_hash_entry
**end_hashes
;
2863 symcount
= (symtab_hdr
->sh_size
2864 / sizeof (Elf32_External_Sym
)
2865 - symtab_hdr
->sh_info
);
2866 sym_hashes
= elf_sym_hashes (abfd
);
2867 end_hashes
= sym_hashes
+ symcount
;
2868 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2870 struct elf_link_hash_entry
*sym_hash
=
2872 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2873 || sym_hash
->root
.type
==
2874 bfd_link_hash_defweak
)
2875 && sym_hash
->root
.u
.def
.section
== sec
2876 && sym_hash
->root
.u
.def
.value
== section_offset_of_ret_insn
)
2878 deleting_ret_is_safe
= 0;
2880 printf ("global label prevents deletion of "
2881 "ret insn at address 0x%x\n",
2882 (int) dot
+ insn_size
);
2887 /* Now we check for relocations pointing to ret. */
2888 for (isec
= abfd
->sections
; isec
&& deleting_ret_is_safe
; isec
= isec
->next
)
2890 Elf_Internal_Rela
*rel
;
2891 Elf_Internal_Rela
*relend
;
2893 rel
= elf_section_data (isec
)->relocs
;
2895 rel
= _bfd_elf_link_read_relocs (abfd
, isec
, NULL
, NULL
, TRUE
);
2897 relend
= rel
+ isec
->reloc_count
;
2899 for (; rel
&& rel
< relend
; rel
++)
2901 bfd_vma reloc_target
= 0;
2903 /* Read this BFD's local symbols if we haven't
2905 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2907 isymbuf
= (Elf_Internal_Sym
*)
2908 symtab_hdr
->contents
;
2909 if (isymbuf
== NULL
)
2910 isymbuf
= bfd_elf_get_elf_syms
2913 symtab_hdr
->sh_info
, 0,
2915 if (isymbuf
== NULL
)
2919 /* Get the value of the symbol referred to
2921 if (ELF32_R_SYM (rel
->r_info
)
2922 < symtab_hdr
->sh_info
)
2924 /* A local symbol. */
2928 + ELF32_R_SYM (rel
->r_info
);
2929 sym_sec
= bfd_section_from_elf_index
2930 (abfd
, isym
->st_shndx
);
2931 symval
= isym
->st_value
;
2933 /* If the reloc is absolute, it will not
2934 have a symbol or section associated
2940 sym_sec
->output_section
->vma
2941 + sym_sec
->output_offset
;
2942 reloc_target
= symval
+ rel
->r_addend
;
2946 reloc_target
= symval
+ rel
->r_addend
;
2947 /* Reference symbol is absolute. */
2950 /* else ... reference symbol is extern. */
2952 if (address_of_ret
== reloc_target
)
2954 deleting_ret_is_safe
= 0;
2957 "rjmp/jmp ret sequence at address"
2958 " 0x%x could not be deleted. ret"
2959 " is target of a relocation.\n",
2960 (int) address_of_ret
);
2966 if (deleting_ret_is_safe
)
2969 printf ("unreachable ret instruction "
2970 "at address 0x%x deleted.\n",
2971 (int) dot
+ insn_size
);
2973 /* Delete two bytes of data. */
2974 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
2975 irel
->r_offset
+ insn_size
, 2))
2978 /* That will change things, so, we should relax
2979 again. Note that this is not required, and it
2994 /* Look through all the property records in this section to see if
2995 there's any alignment records that can be moved. */
2996 struct avr_relax_info
*relax_info
;
2998 relax_info
= get_avr_relax_info (sec
);
2999 if (relax_info
->records
.count
> 0)
3003 for (i
= 0; i
< relax_info
->records
.count
; ++i
)
3005 switch (relax_info
->records
.items
[i
].type
)
3008 case RECORD_ORG_AND_FILL
:
3011 case RECORD_ALIGN_AND_FILL
:
3013 struct avr_property_record
*record
;
3014 unsigned long bytes_to_align
;
3017 /* Look for alignment directives that have had enough
3018 bytes deleted before them, such that the directive
3019 can be moved backwards and still maintain the
3020 required alignment. */
3021 record
= &relax_info
->records
.items
[i
];
3023 = (unsigned long) (1 << record
->data
.align
.bytes
);
3024 while (record
->data
.align
.preceding_deleted
>=
3027 record
->data
.align
.preceding_deleted
3029 count
+= bytes_to_align
;
3034 bfd_vma addr
= record
->offset
;
3036 /* We can delete COUNT bytes and this alignment
3037 directive will still be correctly aligned.
3038 First move the alignment directive, then delete
3040 record
->offset
-= count
;
3041 elf32_avr_relax_delete_bytes (abfd
, sec
,
3053 if (contents
!= NULL
3054 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3056 if (! link_info
->keep_memory
)
3060 /* Cache the section contents for elf_link_input_bfd. */
3061 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3065 if (internal_relocs
!= NULL
3066 && elf_section_data (sec
)->relocs
!= internal_relocs
)
3067 free (internal_relocs
);
3073 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3075 if (contents
!= NULL
3076 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3078 if (internal_relocs
!= NULL
3079 && elf_section_data (sec
)->relocs
!= internal_relocs
)
3080 free (internal_relocs
);
3085 /* This is a version of bfd_generic_get_relocated_section_contents
3086 which uses elf32_avr_relocate_section.
3088 For avr it's essentially a cut and paste taken from the H8300 port.
3089 The author of the relaxation support patch for avr had absolutely no
3090 clue what is happening here but found out that this part of the code
3091 seems to be important. */
3094 elf32_avr_get_relocated_section_contents (bfd
*output_bfd
,
3095 struct bfd_link_info
*link_info
,
3096 struct bfd_link_order
*link_order
,
3098 bfd_boolean relocatable
,
3101 Elf_Internal_Shdr
*symtab_hdr
;
3102 asection
*input_section
= link_order
->u
.indirect
.section
;
3103 bfd
*input_bfd
= input_section
->owner
;
3104 asection
**sections
= NULL
;
3105 Elf_Internal_Rela
*internal_relocs
= NULL
;
3106 Elf_Internal_Sym
*isymbuf
= NULL
;
3108 /* We only need to handle the case of relaxing, or of having a
3109 particular set of section contents, specially. */
3111 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
3112 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
3116 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3118 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
3119 (size_t) input_section
->size
);
3121 if ((input_section
->flags
& SEC_RELOC
) != 0
3122 && input_section
->reloc_count
> 0)
3125 Elf_Internal_Sym
*isym
, *isymend
;
3128 internal_relocs
= (_bfd_elf_link_read_relocs
3129 (input_bfd
, input_section
, NULL
, NULL
, FALSE
));
3130 if (internal_relocs
== NULL
)
3133 if (symtab_hdr
->sh_info
!= 0)
3135 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3136 if (isymbuf
== NULL
)
3137 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3138 symtab_hdr
->sh_info
, 0,
3140 if (isymbuf
== NULL
)
3144 amt
= symtab_hdr
->sh_info
;
3145 amt
*= sizeof (asection
*);
3146 sections
= bfd_malloc (amt
);
3147 if (sections
== NULL
&& amt
!= 0)
3150 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
3151 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
3155 if (isym
->st_shndx
== SHN_UNDEF
)
3156 isec
= bfd_und_section_ptr
;
3157 else if (isym
->st_shndx
== SHN_ABS
)
3158 isec
= bfd_abs_section_ptr
;
3159 else if (isym
->st_shndx
== SHN_COMMON
)
3160 isec
= bfd_com_section_ptr
;
3162 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
3167 if (! elf32_avr_relocate_section (output_bfd
, link_info
, input_bfd
,
3168 input_section
, data
, internal_relocs
,
3172 if (sections
!= NULL
)
3175 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3177 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
3178 free (internal_relocs
);
3184 if (sections
!= NULL
)
3187 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3189 if (internal_relocs
!= NULL
3190 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
3191 free (internal_relocs
);
3196 /* Determines the hash entry name for a particular reloc. It consists of
3197 the identifier of the symbol section and the added reloc addend and
3198 symbol offset relative to the section the symbol is attached to. */
3201 avr_stub_name (const asection
*symbol_section
,
3202 const bfd_vma symbol_offset
,
3203 const Elf_Internal_Rela
*rela
)
3208 len
= 8 + 1 + 8 + 1 + 1;
3209 stub_name
= bfd_malloc (len
);
3211 sprintf (stub_name
, "%08x+%08x",
3212 symbol_section
->id
& 0xffffffff,
3213 (unsigned int) ((rela
->r_addend
& 0xffffffff) + symbol_offset
));
3219 /* Add a new stub entry to the stub hash. Not all fields of the new
3220 stub entry are initialised. */
3222 static struct elf32_avr_stub_hash_entry
*
3223 avr_add_stub (const char *stub_name
,
3224 struct elf32_avr_link_hash_table
*htab
)
3226 struct elf32_avr_stub_hash_entry
*hsh
;
3228 /* Enter this entry into the linker stub hash table. */
3229 hsh
= avr_stub_hash_lookup (&htab
->bstab
, stub_name
, TRUE
, FALSE
);
3233 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
3238 hsh
->stub_offset
= 0;
3242 /* We assume that there is already space allocated for the stub section
3243 contents and that before building the stubs the section size is
3244 initialized to 0. We assume that within the stub hash table entry,
3245 the absolute position of the jmp target has been written in the
3246 target_value field. We write here the offset of the generated jmp insn
3247 relative to the trampoline section start to the stub_offset entry in
3248 the stub hash table entry. */
3251 avr_build_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
3253 struct elf32_avr_stub_hash_entry
*hsh
;
3254 struct bfd_link_info
*info
;
3255 struct elf32_avr_link_hash_table
*htab
;
3262 bfd_vma jmp_insn
= 0x0000940c;
3264 /* Massage our args to the form they really have. */
3265 hsh
= avr_stub_hash_entry (bh
);
3267 if (!hsh
->is_actually_needed
)
3270 info
= (struct bfd_link_info
*) in_arg
;
3272 htab
= avr_link_hash_table (info
);
3276 target
= hsh
->target_value
;
3278 /* Make a note of the offset within the stubs for this entry. */
3279 hsh
->stub_offset
= htab
->stub_sec
->size
;
3280 loc
= htab
->stub_sec
->contents
+ hsh
->stub_offset
;
3282 stub_bfd
= htab
->stub_sec
->owner
;
3285 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
3286 (unsigned int) target
,
3287 (unsigned int) hsh
->stub_offset
);
3289 /* We now have to add the information on the jump target to the bare
3290 opcode bits already set in jmp_insn. */
3292 /* Check for the alignment of the address. */
3296 starget
= target
>> 1;
3297 jmp_insn
|= ((starget
& 0x10000) | ((starget
<< 3) & 0x1f00000)) >> 16;
3298 bfd_put_16 (stub_bfd
, jmp_insn
, loc
);
3299 bfd_put_16 (stub_bfd
, (bfd_vma
) starget
& 0xffff, loc
+ 2);
3301 htab
->stub_sec
->size
+= 4;
3303 /* Now add the entries in the address mapping table if there is still
3308 nr
= htab
->amt_entry_cnt
+ 1;
3309 if (nr
<= htab
->amt_max_entry_cnt
)
3311 htab
->amt_entry_cnt
= nr
;
3313 htab
->amt_stub_offsets
[nr
- 1] = hsh
->stub_offset
;
3314 htab
->amt_destination_addr
[nr
- 1] = target
;
3322 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry
*bh
,
3323 void *in_arg ATTRIBUTE_UNUSED
)
3325 struct elf32_avr_stub_hash_entry
*hsh
;
3327 hsh
= avr_stub_hash_entry (bh
);
3328 hsh
->is_actually_needed
= FALSE
;
3334 avr_size_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
3336 struct elf32_avr_stub_hash_entry
*hsh
;
3337 struct elf32_avr_link_hash_table
*htab
;
3340 /* Massage our args to the form they really have. */
3341 hsh
= avr_stub_hash_entry (bh
);
3344 if (hsh
->is_actually_needed
)
3349 htab
->stub_sec
->size
+= size
;
3354 elf32_avr_setup_params (struct bfd_link_info
*info
,
3356 asection
*avr_stub_section
,
3357 bfd_boolean no_stubs
,
3358 bfd_boolean deb_stubs
,
3359 bfd_boolean deb_relax
,
3360 bfd_vma pc_wrap_around
,
3361 bfd_boolean call_ret_replacement
)
3363 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3367 htab
->stub_sec
= avr_stub_section
;
3368 htab
->stub_bfd
= avr_stub_bfd
;
3369 htab
->no_stubs
= no_stubs
;
3371 debug_relax
= deb_relax
;
3372 debug_stubs
= deb_stubs
;
3373 avr_pc_wrap_around
= pc_wrap_around
;
3374 avr_replace_call_ret_sequences
= call_ret_replacement
;
3378 /* Set up various things so that we can make a list of input sections
3379 for each output section included in the link. Returns -1 on error,
3380 0 when no stubs will be needed, and 1 on success. It also sets
3381 information on the stubs bfd and the stub section in the info
3385 elf32_avr_setup_section_lists (bfd
*output_bfd
,
3386 struct bfd_link_info
*info
)
3389 unsigned int bfd_count
;
3390 unsigned int top_id
, top_index
;
3392 asection
**input_list
, **list
;
3394 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3396 if (htab
== NULL
|| htab
->no_stubs
)
3399 /* Count the number of input BFDs and find the top input section id. */
3400 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
3402 input_bfd
= input_bfd
->link
.next
)
3405 for (section
= input_bfd
->sections
;
3407 section
= section
->next
)
3408 if (top_id
< section
->id
)
3409 top_id
= section
->id
;
3412 htab
->bfd_count
= bfd_count
;
3414 /* We can't use output_bfd->section_count here to find the top output
3415 section index as some sections may have been removed, and
3416 strip_excluded_output_sections doesn't renumber the indices. */
3417 for (section
= output_bfd
->sections
, top_index
= 0;
3419 section
= section
->next
)
3420 if (top_index
< section
->index
)
3421 top_index
= section
->index
;
3423 htab
->top_index
= top_index
;
3424 amt
= sizeof (asection
*) * (top_index
+ 1);
3425 input_list
= bfd_malloc (amt
);
3426 htab
->input_list
= input_list
;
3427 if (input_list
== NULL
)
3430 /* For sections we aren't interested in, mark their entries with a
3431 value we can check later. */
3432 list
= input_list
+ top_index
;
3434 *list
= bfd_abs_section_ptr
;
3435 while (list
-- != input_list
);
3437 for (section
= output_bfd
->sections
;
3439 section
= section
->next
)
3440 if ((section
->flags
& SEC_CODE
) != 0)
3441 input_list
[section
->index
] = NULL
;
3447 /* Read in all local syms for all input bfds, and create hash entries
3448 for export stubs if we are building a multi-subspace shared lib.
3449 Returns -1 on error, 0 otherwise. */
3452 get_local_syms (bfd
*input_bfd
, struct bfd_link_info
*info
)
3454 unsigned int bfd_indx
;
3455 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
3456 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
3462 /* We want to read in symbol extension records only once. To do this
3463 we need to read in the local symbols in parallel and save them for
3464 later use; so hold pointers to the local symbols in an array. */
3465 amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
3466 all_local_syms
= bfd_zmalloc (amt
);
3467 htab
->all_local_syms
= all_local_syms
;
3468 if (all_local_syms
== NULL
)
3471 /* Walk over all the input BFDs, swapping in local symbols.
3472 If we are creating a shared library, create hash entries for the
3476 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3478 Elf_Internal_Shdr
*symtab_hdr
;
3480 /* We'll need the symbol table in a second. */
3481 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3482 if (symtab_hdr
->sh_info
== 0)
3485 /* We need an array of the local symbols attached to the input bfd. */
3486 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3487 if (local_syms
== NULL
)
3489 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3490 symtab_hdr
->sh_info
, 0,
3492 /* Cache them for elf_link_input_bfd. */
3493 symtab_hdr
->contents
= (unsigned char *) local_syms
;
3495 if (local_syms
== NULL
)
3498 all_local_syms
[bfd_indx
] = local_syms
;
3504 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
3507 elf32_avr_size_stubs (bfd
*output_bfd
,
3508 struct bfd_link_info
*info
,
3509 bfd_boolean is_prealloc_run
)
3511 struct elf32_avr_link_hash_table
*htab
;
3512 int stub_changed
= 0;
3514 htab
= avr_link_hash_table (info
);
3518 /* At this point we initialize htab->vector_base
3519 To the start of the text output section. */
3520 htab
->vector_base
= htab
->stub_sec
->output_section
->vma
;
3522 if (get_local_syms (info
->input_bfds
, info
))
3524 if (htab
->all_local_syms
)
3525 goto error_ret_free_local
;
3529 if (ADD_DUMMY_STUBS_FOR_DEBUGGING
)
3531 struct elf32_avr_stub_hash_entry
*test
;
3533 test
= avr_add_stub ("Hugo",htab
);
3534 test
->target_value
= 0x123456;
3535 test
->stub_offset
= 13;
3537 test
= avr_add_stub ("Hugo2",htab
);
3538 test
->target_value
= 0x84210;
3539 test
->stub_offset
= 14;
3545 unsigned int bfd_indx
;
3547 /* We will have to re-generate the stub hash table each time anything
3548 in memory has changed. */
3550 bfd_hash_traverse (&htab
->bstab
, avr_mark_stub_not_to_be_necessary
, htab
);
3551 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
3553 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
3555 Elf_Internal_Shdr
*symtab_hdr
;
3557 Elf_Internal_Sym
*local_syms
;
3559 /* We'll need the symbol table in a second. */
3560 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3561 if (symtab_hdr
->sh_info
== 0)
3564 local_syms
= htab
->all_local_syms
[bfd_indx
];
3566 /* Walk over each section attached to the input bfd. */
3567 for (section
= input_bfd
->sections
;
3569 section
= section
->next
)
3571 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
3573 /* If there aren't any relocs, then there's nothing more
3575 if ((section
->flags
& SEC_RELOC
) == 0
3576 || section
->reloc_count
== 0)
3579 /* If this section is a link-once section that will be
3580 discarded, then don't create any stubs. */
3581 if (section
->output_section
== NULL
3582 || section
->output_section
->owner
!= output_bfd
)
3585 /* Get the relocs. */
3587 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
3589 if (internal_relocs
== NULL
)
3590 goto error_ret_free_local
;
3592 /* Now examine each relocation. */
3593 irela
= internal_relocs
;
3594 irelaend
= irela
+ section
->reloc_count
;
3595 for (; irela
< irelaend
; irela
++)
3597 unsigned int r_type
, r_indx
;
3598 struct elf32_avr_stub_hash_entry
*hsh
;
3601 bfd_vma destination
;
3602 struct elf_link_hash_entry
*hh
;
3605 r_type
= ELF32_R_TYPE (irela
->r_info
);
3606 r_indx
= ELF32_R_SYM (irela
->r_info
);
3608 /* Only look for 16 bit GS relocs. No other reloc will need a
3610 if (!((r_type
== R_AVR_16_PM
)
3611 || (r_type
== R_AVR_LO8_LDI_GS
)
3612 || (r_type
== R_AVR_HI8_LDI_GS
)))
3615 /* Now determine the call target, its name, value,
3621 if (r_indx
< symtab_hdr
->sh_info
)
3623 /* It's a local symbol. */
3624 Elf_Internal_Sym
*sym
;
3625 Elf_Internal_Shdr
*hdr
;
3628 sym
= local_syms
+ r_indx
;
3629 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
3630 sym_value
= sym
->st_value
;
3631 shndx
= sym
->st_shndx
;
3632 if (shndx
< elf_numsections (input_bfd
))
3634 hdr
= elf_elfsections (input_bfd
)[shndx
];
3635 sym_sec
= hdr
->bfd_section
;
3636 destination
= (sym_value
+ irela
->r_addend
3637 + sym_sec
->output_offset
3638 + sym_sec
->output_section
->vma
);
3643 /* It's an external symbol. */
3646 e_indx
= r_indx
- symtab_hdr
->sh_info
;
3647 hh
= elf_sym_hashes (input_bfd
)[e_indx
];
3649 while (hh
->root
.type
== bfd_link_hash_indirect
3650 || hh
->root
.type
== bfd_link_hash_warning
)
3651 hh
= (struct elf_link_hash_entry
*)
3652 (hh
->root
.u
.i
.link
);
3654 if (hh
->root
.type
== bfd_link_hash_defined
3655 || hh
->root
.type
== bfd_link_hash_defweak
)
3657 sym_sec
= hh
->root
.u
.def
.section
;
3658 sym_value
= hh
->root
.u
.def
.value
;
3659 if (sym_sec
->output_section
!= NULL
)
3660 destination
= (sym_value
+ irela
->r_addend
3661 + sym_sec
->output_offset
3662 + sym_sec
->output_section
->vma
);
3664 else if (hh
->root
.type
== bfd_link_hash_undefweak
)
3666 if (! bfd_link_pic (info
))
3669 else if (hh
->root
.type
== bfd_link_hash_undefined
)
3671 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
3672 && (ELF_ST_VISIBILITY (hh
->other
)
3678 bfd_set_error (bfd_error_bad_value
);
3680 error_ret_free_internal
:
3681 if (elf_section_data (section
)->relocs
== NULL
)
3682 free (internal_relocs
);
3683 goto error_ret_free_local
;
3687 if (! avr_stub_is_required_for_16_bit_reloc
3688 (destination
- htab
->vector_base
))
3690 if (!is_prealloc_run
)
3691 /* We are having a reloc that does't need a stub. */
3694 /* We don't right now know if a stub will be needed.
3695 Let's rather be on the safe side. */
3698 /* Get the name of this stub. */
3699 stub_name
= avr_stub_name (sym_sec
, sym_value
, irela
);
3702 goto error_ret_free_internal
;
3705 hsh
= avr_stub_hash_lookup (&htab
->bstab
,
3710 /* The proper stub has already been created. Mark it
3711 to be used and write the possibly changed destination
3713 hsh
->is_actually_needed
= TRUE
;
3714 hsh
->target_value
= destination
;
3719 hsh
= avr_add_stub (stub_name
, htab
);
3723 goto error_ret_free_internal
;
3726 hsh
->is_actually_needed
= TRUE
;
3727 hsh
->target_value
= destination
;
3730 printf ("Adding stub with destination 0x%x to the"
3731 " hash table.\n", (unsigned int) destination
);
3733 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run
);
3735 stub_changed
= TRUE
;
3738 /* We're done with the internal relocs, free them. */
3739 if (elf_section_data (section
)->relocs
== NULL
)
3740 free (internal_relocs
);
3744 /* Re-Calculate the number of needed stubs. */
3745 htab
->stub_sec
->size
= 0;
3746 bfd_hash_traverse (&htab
->bstab
, avr_size_one_stub
, htab
);
3751 stub_changed
= FALSE
;
3754 free (htab
->all_local_syms
);
3757 error_ret_free_local
:
3758 free (htab
->all_local_syms
);
3763 /* Build all the stubs associated with the current output file. The
3764 stubs are kept in a hash table attached to the main linker hash
3765 table. We also set up the .plt entries for statically linked PIC
3766 functions here. This function is called via hppaelf_finish in the
3770 elf32_avr_build_stubs (struct bfd_link_info
*info
)
3773 struct bfd_hash_table
*table
;
3774 struct elf32_avr_link_hash_table
*htab
;
3775 bfd_size_type total_size
= 0;
3777 htab
= avr_link_hash_table (info
);
3781 /* In case that there were several stub sections: */
3782 for (stub_sec
= htab
->stub_bfd
->sections
;
3784 stub_sec
= stub_sec
->next
)
3788 /* Allocate memory to hold the linker stubs. */
3789 size
= stub_sec
->size
;
3792 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
3793 if (stub_sec
->contents
== NULL
&& size
!= 0)
3798 /* Allocate memory for the adress mapping table. */
3799 htab
->amt_entry_cnt
= 0;
3800 htab
->amt_max_entry_cnt
= total_size
/ 4;
3801 htab
->amt_stub_offsets
= bfd_malloc (sizeof (bfd_vma
)
3802 * htab
->amt_max_entry_cnt
);
3803 htab
->amt_destination_addr
= bfd_malloc (sizeof (bfd_vma
)
3804 * htab
->amt_max_entry_cnt
);
3807 printf ("Allocating %i entries in the AMT\n", htab
->amt_max_entry_cnt
);
3809 /* Build the stubs as directed by the stub hash table. */
3810 table
= &htab
->bstab
;
3811 bfd_hash_traverse (table
, avr_build_one_stub
, info
);
3814 printf ("Final Stub section Size: %i\n", (int) htab
->stub_sec
->size
);
3819 /* Callback used by QSORT to order relocations AP and BP. */
3822 internal_reloc_compare (const void *ap
, const void *bp
)
3824 const Elf_Internal_Rela
*a
= (const Elf_Internal_Rela
*) ap
;
3825 const Elf_Internal_Rela
*b
= (const Elf_Internal_Rela
*) bp
;
3827 if (a
->r_offset
!= b
->r_offset
)
3828 return (a
->r_offset
- b
->r_offset
);
3830 /* We don't need to sort on these criteria for correctness,
3831 but enforcing a more strict ordering prevents unstable qsort
3832 from behaving differently with different implementations.
3833 Without the code below we get correct but different results
3834 on Solaris 2.7 and 2.8. We would like to always produce the
3835 same results no matter the host. */
3837 if (a
->r_info
!= b
->r_info
)
3838 return (a
->r_info
- b
->r_info
);
3840 return (a
->r_addend
- b
->r_addend
);
3843 /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */
3846 avr_is_section_for_address (bfd
*abfd
, asection
*section
, bfd_vma address
)
3851 vma
= bfd_get_section_vma (abfd
, section
);
3855 size
= section
->size
;
3856 if (address
>= vma
+ size
)
3862 /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */
3864 struct avr_find_section_data
3866 /* The address we're looking for. */
3869 /* The section we've found. */
3873 /* Helper function to locate the section holding a certain virtual memory
3874 address. This is called via bfd_map_over_sections. The DATA is an
3875 instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which
3876 has been set to the address to search for, and the section field has
3877 been set to NULL. If SECTION from ABFD contains ADDRESS then the
3878 section field in DATA will be set to SECTION. As an optimisation, if
3879 the section field is already non-null then this function does not
3880 perform any checks, and just returns. */
3883 avr_find_section_for_address (bfd
*abfd
,
3884 asection
*section
, void *data
)
3886 struct avr_find_section_data
*fs_data
3887 = (struct avr_find_section_data
*) data
;
3889 /* Return if already found. */
3890 if (fs_data
->section
!= NULL
)
3893 /* If this section isn't part of the addressable code content, skip it. */
3894 if ((bfd_get_section_flags (abfd
, section
) & SEC_ALLOC
) == 0
3895 && (bfd_get_section_flags (abfd
, section
) & SEC_CODE
) == 0)
3898 if (avr_is_section_for_address (abfd
, section
, fs_data
->address
))
3899 fs_data
->section
= section
;
3902 /* Load all of the property records from SEC, a section from ABFD. Return
3903 a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The
3904 memory for the returned structure, and all of the records pointed too by
3905 the structure are allocated with a single call to malloc, so, only the
3906 pointer returned needs to be free'd. */
3908 static struct avr_property_record_list
*
3909 avr_elf32_load_records_from_section (bfd
*abfd
, asection
*sec
)
3911 char *contents
= NULL
, *ptr
;
3912 bfd_size_type size
, mem_size
;
3913 bfd_byte version
, flags
;
3914 uint16_t record_count
, i
;
3915 struct avr_property_record_list
*r_list
= NULL
;
3916 Elf_Internal_Rela
*internal_relocs
= NULL
, *rel
, *rel_end
;
3917 struct avr_find_section_data fs_data
;
3919 fs_data
.section
= NULL
;
3921 size
= bfd_get_section_size (sec
);
3922 contents
= bfd_malloc (size
);
3923 bfd_get_section_contents (abfd
, sec
, contents
, 0, size
);
3926 /* Load the relocations for the '.avr.prop' section if there are any, and
3928 internal_relocs
= (_bfd_elf_link_read_relocs
3929 (abfd
, sec
, NULL
, NULL
, FALSE
));
3930 if (internal_relocs
)
3931 qsort (internal_relocs
, sec
->reloc_count
,
3932 sizeof (Elf_Internal_Rela
), internal_reloc_compare
);
3934 /* There is a header at the start of the property record section SEC, the
3935 format of this header is:
3936 uint8_t : version number
3938 uint16_t : record counter
3941 /* Check we have at least got a headers worth of bytes. */
3942 if (size
< AVR_PROPERTY_SECTION_HEADER_SIZE
)
3945 version
= *((bfd_byte
*) ptr
);
3947 flags
= *((bfd_byte
*) ptr
);
3949 record_count
= *((uint16_t *) ptr
);
3951 BFD_ASSERT (ptr
- contents
== AVR_PROPERTY_SECTION_HEADER_SIZE
);
3953 /* Now allocate space for the list structure, and all of the list
3954 elements in a single block. */
3955 mem_size
= sizeof (struct avr_property_record_list
)
3956 + sizeof (struct avr_property_record
) * record_count
;
3957 r_list
= bfd_malloc (mem_size
);
3961 r_list
->version
= version
;
3962 r_list
->flags
= flags
;
3963 r_list
->section
= sec
;
3964 r_list
->record_count
= record_count
;
3965 r_list
->records
= (struct avr_property_record
*) (&r_list
[1]);
3966 size
-= AVR_PROPERTY_SECTION_HEADER_SIZE
;
3968 /* Check that we understand the version number. There is only one
3969 version number right now, anything else is an error. */
3970 if (r_list
->version
!= AVR_PROPERTY_RECORDS_VERSION
)
3973 rel
= internal_relocs
;
3974 rel_end
= rel
+ sec
->reloc_count
;
3975 for (i
= 0; i
< record_count
; ++i
)
3979 /* Each entry is a 32-bit address, followed by a single byte type.
3980 After that is the type specific data. We must take care to
3981 ensure that we don't read beyond the end of the section data. */
3985 r_list
->records
[i
].section
= NULL
;
3986 r_list
->records
[i
].offset
= 0;
3990 /* The offset of the address within the .avr.prop section. */
3991 size_t offset
= ptr
- contents
;
3993 while (rel
< rel_end
&& rel
->r_offset
< offset
)
3998 else if (rel
->r_offset
== offset
)
4000 /* Find section and section offset. */
4001 unsigned long r_symndx
;
4006 r_symndx
= ELF32_R_SYM (rel
->r_info
);
4007 rel_sec
= get_elf_r_symndx_section (abfd
, r_symndx
);
4008 sec_offset
= get_elf_r_symndx_offset (abfd
, r_symndx
)
4011 r_list
->records
[i
].section
= rel_sec
;
4012 r_list
->records
[i
].offset
= sec_offset
;
4016 address
= *((uint32_t *) ptr
);
4020 if (r_list
->records
[i
].section
== NULL
)
4022 /* Try to find section and offset from address. */
4023 if (fs_data
.section
!= NULL
4024 && !avr_is_section_for_address (abfd
, fs_data
.section
,
4026 fs_data
.section
= NULL
;
4028 if (fs_data
.section
== NULL
)
4030 fs_data
.address
= address
;
4031 bfd_map_over_sections (abfd
, avr_find_section_for_address
,
4035 if (fs_data
.section
== NULL
)
4037 fprintf (stderr
, "Failed to find matching section.\n");
4041 r_list
->records
[i
].section
= fs_data
.section
;
4042 r_list
->records
[i
].offset
4043 = address
- bfd_get_section_vma (abfd
, fs_data
.section
);
4046 r_list
->records
[i
].type
= *((bfd_byte
*) ptr
);
4050 switch (r_list
->records
[i
].type
)
4053 /* Nothing else to load. */
4055 case RECORD_ORG_AND_FILL
:
4056 /* Just a 4-byte fill to load. */
4059 r_list
->records
[i
].data
.org
.fill
= *((uint32_t *) ptr
);
4064 /* Just a 4-byte alignment to load. */
4067 r_list
->records
[i
].data
.align
.bytes
= *((uint32_t *) ptr
);
4070 /* Just initialise PRECEDING_DELETED field, this field is
4071 used during linker relaxation. */
4072 r_list
->records
[i
].data
.align
.preceding_deleted
= 0;
4074 case RECORD_ALIGN_AND_FILL
:
4075 /* A 4-byte alignment, and a 4-byte fill to load. */
4078 r_list
->records
[i
].data
.align
.bytes
= *((uint32_t *) ptr
);
4080 r_list
->records
[i
].data
.align
.fill
= *((uint32_t *) ptr
);
4083 /* Just initialise PRECEDING_DELETED field, this field is
4084 used during linker relaxation. */
4085 r_list
->records
[i
].data
.align
.preceding_deleted
= 0;
4093 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
4094 free (internal_relocs
);
4098 if (elf_section_data (sec
)->relocs
!= internal_relocs
)
4099 free (internal_relocs
);
4105 /* Load all of the property records from ABFD. See
4106 AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */
4108 struct avr_property_record_list
*
4109 avr_elf32_load_property_records (bfd
*abfd
)
4113 /* Find the '.avr.prop' section and load the contents into memory. */
4114 sec
= bfd_get_section_by_name (abfd
, AVR_PROPERTY_RECORD_SECTION_NAME
);
4117 return avr_elf32_load_records_from_section (abfd
, sec
);
4121 avr_elf32_property_record_name (struct avr_property_record
*rec
)
4130 case RECORD_ORG_AND_FILL
:
4136 case RECORD_ALIGN_AND_FILL
:
4147 #define ELF_ARCH bfd_arch_avr
4148 #define ELF_TARGET_ID AVR_ELF_DATA
4149 #define ELF_MACHINE_CODE EM_AVR
4150 #define ELF_MACHINE_ALT1 EM_AVR_OLD
4151 #define ELF_MAXPAGESIZE 1
4153 #define TARGET_LITTLE_SYM avr_elf32_vec
4154 #define TARGET_LITTLE_NAME "elf32-avr"
4156 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
4158 #define elf_info_to_howto avr_info_to_howto_rela
4159 #define elf_info_to_howto_rel NULL
4160 #define elf_backend_relocate_section elf32_avr_relocate_section
4161 #define elf_backend_can_gc_sections 1
4162 #define elf_backend_rela_normal 1
4163 #define elf_backend_final_write_processing \
4164 bfd_elf_avr_final_write_processing
4165 #define elf_backend_object_p elf32_avr_object_p
4167 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
4168 #define bfd_elf32_bfd_get_relocated_section_contents \
4169 elf32_avr_get_relocated_section_contents
4170 #define bfd_elf32_new_section_hook elf_avr_new_section_hook
4172 #include "elf32-target.h"