1 /* AVR-specific support for 32-bit ELF
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Denis Chertykov <denisc@overta.ru>
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor,
21 Boston, MA 02110-1301, USA. */
28 #include "elf32-avr.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 /* Hash table initialization and handling. Code is taken from the hppa port
37 and adapted to the needs of AVR. */
39 /* We use two hash tables to hold information for linking avr objects.
41 The first is the elf32_avr_link_hash_tablse which is derived from the
42 stanard ELF linker hash table. We use this as a place to attach the other
43 hash table and some static information.
45 The second is the stub hash table which is derived from the base BFD
46 hash table. The stub hash table holds the information on the linker
49 struct elf32_avr_stub_hash_entry
51 /* Base hash table entry structure. */
52 struct bfd_hash_entry bh_root;
54 /* Offset within stub_sec of the beginning of this stub. */
57 /* Given the symbol's value and its section we can determine its final
58 value when building the stubs (so the stub knows where to jump). */
61 /* This way we could mark stubs to be no longer necessary. */
62 bfd_boolean is_actually_needed;
65 struct elf32_avr_link_hash_table
67 /* The main hash table. */
68 struct elf_link_hash_table etab;
70 /* The stub hash table. */
71 struct bfd_hash_table bstab;
75 /* Linker stub bfd. */
78 /* The stub section. */
81 /* Usually 0, unless we are generating code for a bootloader. Will
82 be initialized by elf32_avr_size_stubs to the vma offset of the
83 output section associated with the stub section. */
86 /* Assorted information used by elf32_avr_size_stubs. */
87 unsigned int bfd_count;
89 asection ** input_list;
90 Elf_Internal_Sym ** all_local_syms;
92 /* Tables for mapping vma beyond the 128k boundary to the address of the
93 corresponding stub. (AMT)
94 "amt_max_entry_cnt" reflects the number of entries that memory is allocated
95 for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
96 "amt_entry_cnt" informs how many of these entries actually contain
98 unsigned int amt_entry_cnt;
99 unsigned int amt_max_entry_cnt;
100 bfd_vma * amt_stub_offsets;
101 bfd_vma * amt_destination_addr;
104 /* Various hash macros and functions. */
105 #define avr_link_hash_table(p) \
106 /* PR 3874: Check that we have an AVR style hash table before using it. */\
107 ((p)->hash->table.newfunc != elf32_avr_link_hash_newfunc ? NULL : \
108 ((struct elf32_avr_link_hash_table *) ((p)->hash)))
110 #define avr_stub_hash_entry(ent) \
111 ((struct elf32_avr_stub_hash_entry *)(ent))
113 #define avr_stub_hash_lookup(table, string, create, copy) \
114 ((struct elf32_avr_stub_hash_entry *) \
115 bfd_hash_lookup ((table), (string), (create), (copy)))
117 static reloc_howto_type elf_avr_howto_table[] =
119 HOWTO (R_AVR_NONE, /* type */
121 2, /* size (0 = byte, 1 = short, 2 = long) */
123 FALSE, /* pc_relative */
125 complain_overflow_bitfield, /* complain_on_overflow */
126 bfd_elf_generic_reloc, /* special_function */
127 "R_AVR_NONE", /* name */
128 FALSE, /* partial_inplace */
131 FALSE), /* pcrel_offset */
133 HOWTO (R_AVR_32, /* type */
135 2, /* size (0 = byte, 1 = short, 2 = long) */
137 FALSE, /* pc_relative */
139 complain_overflow_bitfield, /* complain_on_overflow */
140 bfd_elf_generic_reloc, /* special_function */
141 "R_AVR_32", /* name */
142 FALSE, /* partial_inplace */
143 0xffffffff, /* src_mask */
144 0xffffffff, /* dst_mask */
145 FALSE), /* pcrel_offset */
147 /* A 7 bit PC relative relocation. */
148 HOWTO (R_AVR_7_PCREL, /* type */
150 1, /* size (0 = byte, 1 = short, 2 = long) */
152 TRUE, /* pc_relative */
154 complain_overflow_bitfield, /* complain_on_overflow */
155 bfd_elf_generic_reloc, /* special_function */
156 "R_AVR_7_PCREL", /* name */
157 FALSE, /* partial_inplace */
158 0xffff, /* src_mask */
159 0xffff, /* dst_mask */
160 TRUE), /* pcrel_offset */
162 /* A 13 bit PC relative relocation. */
163 HOWTO (R_AVR_13_PCREL, /* type */
165 1, /* size (0 = byte, 1 = short, 2 = long) */
167 TRUE, /* pc_relative */
169 complain_overflow_bitfield, /* complain_on_overflow */
170 bfd_elf_generic_reloc, /* special_function */
171 "R_AVR_13_PCREL", /* name */
172 FALSE, /* partial_inplace */
173 0xfff, /* src_mask */
174 0xfff, /* dst_mask */
175 TRUE), /* pcrel_offset */
177 /* A 16 bit absolute relocation. */
178 HOWTO (R_AVR_16, /* type */
180 1, /* size (0 = byte, 1 = short, 2 = long) */
182 FALSE, /* pc_relative */
184 complain_overflow_dont, /* complain_on_overflow */
185 bfd_elf_generic_reloc, /* special_function */
186 "R_AVR_16", /* name */
187 FALSE, /* partial_inplace */
188 0xffff, /* src_mask */
189 0xffff, /* dst_mask */
190 FALSE), /* pcrel_offset */
192 /* A 16 bit absolute relocation for command address
193 Will be changed when linker stubs are needed. */
194 HOWTO (R_AVR_16_PM, /* type */
196 1, /* size (0 = byte, 1 = short, 2 = long) */
198 FALSE, /* pc_relative */
200 complain_overflow_bitfield, /* complain_on_overflow */
201 bfd_elf_generic_reloc, /* special_function */
202 "R_AVR_16_PM", /* name */
203 FALSE, /* partial_inplace */
204 0xffff, /* src_mask */
205 0xffff, /* dst_mask */
206 FALSE), /* pcrel_offset */
207 /* A low 8 bit absolute relocation of 16 bit address.
209 HOWTO (R_AVR_LO8_LDI, /* type */
211 1, /* size (0 = byte, 1 = short, 2 = long) */
213 FALSE, /* pc_relative */
215 complain_overflow_dont, /* complain_on_overflow */
216 bfd_elf_generic_reloc, /* special_function */
217 "R_AVR_LO8_LDI", /* name */
218 FALSE, /* partial_inplace */
219 0xffff, /* src_mask */
220 0xffff, /* dst_mask */
221 FALSE), /* pcrel_offset */
222 /* A high 8 bit absolute relocation of 16 bit address.
224 HOWTO (R_AVR_HI8_LDI, /* type */
226 1, /* size (0 = byte, 1 = short, 2 = long) */
228 FALSE, /* pc_relative */
230 complain_overflow_dont, /* complain_on_overflow */
231 bfd_elf_generic_reloc, /* special_function */
232 "R_AVR_HI8_LDI", /* name */
233 FALSE, /* partial_inplace */
234 0xffff, /* src_mask */
235 0xffff, /* dst_mask */
236 FALSE), /* pcrel_offset */
237 /* A high 6 bit absolute relocation of 22 bit address.
238 For LDI command. As well second most significant 8 bit value of
239 a 32 bit link-time constant. */
240 HOWTO (R_AVR_HH8_LDI, /* type */
242 1, /* size (0 = byte, 1 = short, 2 = long) */
244 FALSE, /* pc_relative */
246 complain_overflow_dont, /* complain_on_overflow */
247 bfd_elf_generic_reloc, /* special_function */
248 "R_AVR_HH8_LDI", /* name */
249 FALSE, /* partial_inplace */
250 0xffff, /* src_mask */
251 0xffff, /* dst_mask */
252 FALSE), /* pcrel_offset */
253 /* A negative low 8 bit absolute relocation of 16 bit address.
255 HOWTO (R_AVR_LO8_LDI_NEG, /* type */
257 1, /* size (0 = byte, 1 = short, 2 = long) */
259 FALSE, /* pc_relative */
261 complain_overflow_dont, /* complain_on_overflow */
262 bfd_elf_generic_reloc, /* special_function */
263 "R_AVR_LO8_LDI_NEG", /* name */
264 FALSE, /* partial_inplace */
265 0xffff, /* src_mask */
266 0xffff, /* dst_mask */
267 FALSE), /* pcrel_offset */
268 /* A negative high 8 bit absolute relocation of 16 bit address.
270 HOWTO (R_AVR_HI8_LDI_NEG, /* type */
272 1, /* size (0 = byte, 1 = short, 2 = long) */
274 FALSE, /* pc_relative */
276 complain_overflow_dont, /* complain_on_overflow */
277 bfd_elf_generic_reloc, /* special_function */
278 "R_AVR_HI8_LDI_NEG", /* name */
279 FALSE, /* partial_inplace */
280 0xffff, /* src_mask */
281 0xffff, /* dst_mask */
282 FALSE), /* pcrel_offset */
283 /* A negative high 6 bit absolute relocation of 22 bit address.
285 HOWTO (R_AVR_HH8_LDI_NEG, /* type */
287 1, /* size (0 = byte, 1 = short, 2 = long) */
289 FALSE, /* pc_relative */
291 complain_overflow_dont, /* complain_on_overflow */
292 bfd_elf_generic_reloc, /* special_function */
293 "R_AVR_HH8_LDI_NEG", /* name */
294 FALSE, /* partial_inplace */
295 0xffff, /* src_mask */
296 0xffff, /* dst_mask */
297 FALSE), /* pcrel_offset */
298 /* A low 8 bit absolute relocation of 24 bit program memory address.
299 For LDI command. Will not be changed when linker stubs are needed. */
300 HOWTO (R_AVR_LO8_LDI_PM, /* type */
302 1, /* size (0 = byte, 1 = short, 2 = long) */
304 FALSE, /* pc_relative */
306 complain_overflow_dont, /* complain_on_overflow */
307 bfd_elf_generic_reloc, /* special_function */
308 "R_AVR_LO8_LDI_PM", /* name */
309 FALSE, /* partial_inplace */
310 0xffff, /* src_mask */
311 0xffff, /* dst_mask */
312 FALSE), /* pcrel_offset */
313 /* A low 8 bit absolute relocation of 24 bit program memory address.
314 For LDI command. Will not be changed when linker stubs are needed. */
315 HOWTO (R_AVR_HI8_LDI_PM, /* type */
317 1, /* size (0 = byte, 1 = short, 2 = long) */
319 FALSE, /* pc_relative */
321 complain_overflow_dont, /* complain_on_overflow */
322 bfd_elf_generic_reloc, /* special_function */
323 "R_AVR_HI8_LDI_PM", /* name */
324 FALSE, /* partial_inplace */
325 0xffff, /* src_mask */
326 0xffff, /* dst_mask */
327 FALSE), /* pcrel_offset */
328 /* A low 8 bit absolute relocation of 24 bit program memory address.
329 For LDI command. Will not be changed when linker stubs are needed. */
330 HOWTO (R_AVR_HH8_LDI_PM, /* type */
332 1, /* size (0 = byte, 1 = short, 2 = long) */
334 FALSE, /* pc_relative */
336 complain_overflow_dont, /* complain_on_overflow */
337 bfd_elf_generic_reloc, /* special_function */
338 "R_AVR_HH8_LDI_PM", /* name */
339 FALSE, /* partial_inplace */
340 0xffff, /* src_mask */
341 0xffff, /* dst_mask */
342 FALSE), /* pcrel_offset */
343 /* A low 8 bit absolute relocation of 24 bit program memory address.
344 For LDI command. Will not be changed when linker stubs are needed. */
345 HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */
347 1, /* size (0 = byte, 1 = short, 2 = long) */
349 FALSE, /* pc_relative */
351 complain_overflow_dont, /* complain_on_overflow */
352 bfd_elf_generic_reloc, /* special_function */
353 "R_AVR_LO8_LDI_PM_NEG", /* name */
354 FALSE, /* partial_inplace */
355 0xffff, /* src_mask */
356 0xffff, /* dst_mask */
357 FALSE), /* pcrel_offset */
358 /* A low 8 bit absolute relocation of 24 bit program memory address.
359 For LDI command. Will not be changed when linker stubs are needed. */
360 HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */
362 1, /* size (0 = byte, 1 = short, 2 = long) */
364 FALSE, /* pc_relative */
366 complain_overflow_dont, /* complain_on_overflow */
367 bfd_elf_generic_reloc, /* special_function */
368 "R_AVR_HI8_LDI_PM_NEG", /* name */
369 FALSE, /* partial_inplace */
370 0xffff, /* src_mask */
371 0xffff, /* dst_mask */
372 FALSE), /* pcrel_offset */
373 /* A low 8 bit absolute relocation of 24 bit program memory address.
374 For LDI command. Will not be changed when linker stubs are needed. */
375 HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */
377 1, /* size (0 = byte, 1 = short, 2 = long) */
379 FALSE, /* pc_relative */
381 complain_overflow_dont, /* complain_on_overflow */
382 bfd_elf_generic_reloc, /* special_function */
383 "R_AVR_HH8_LDI_PM_NEG", /* name */
384 FALSE, /* partial_inplace */
385 0xffff, /* src_mask */
386 0xffff, /* dst_mask */
387 FALSE), /* pcrel_offset */
388 /* Relocation for CALL command in ATmega. */
389 HOWTO (R_AVR_CALL, /* type */
391 2, /* size (0 = byte, 1 = short, 2 = long) */
393 FALSE, /* pc_relative */
395 complain_overflow_dont,/* complain_on_overflow */
396 bfd_elf_generic_reloc, /* special_function */
397 "R_AVR_CALL", /* name */
398 FALSE, /* partial_inplace */
399 0xffffffff, /* src_mask */
400 0xffffffff, /* dst_mask */
401 FALSE), /* pcrel_offset */
402 /* A 16 bit absolute relocation of 16 bit address.
404 HOWTO (R_AVR_LDI, /* type */
406 1, /* size (0 = byte, 1 = short, 2 = long) */
408 FALSE, /* pc_relative */
410 complain_overflow_dont,/* complain_on_overflow */
411 bfd_elf_generic_reloc, /* special_function */
412 "R_AVR_LDI", /* name */
413 FALSE, /* partial_inplace */
414 0xffff, /* src_mask */
415 0xffff, /* dst_mask */
416 FALSE), /* pcrel_offset */
417 /* A 6 bit absolute relocation of 6 bit offset.
418 For ldd/sdd command. */
419 HOWTO (R_AVR_6, /* type */
421 0, /* size (0 = byte, 1 = short, 2 = long) */
423 FALSE, /* pc_relative */
425 complain_overflow_dont,/* complain_on_overflow */
426 bfd_elf_generic_reloc, /* special_function */
427 "R_AVR_6", /* name */
428 FALSE, /* partial_inplace */
429 0xffff, /* src_mask */
430 0xffff, /* dst_mask */
431 FALSE), /* pcrel_offset */
432 /* A 6 bit absolute relocation of 6 bit offset.
433 For sbiw/adiw command. */
434 HOWTO (R_AVR_6_ADIW, /* type */
436 0, /* size (0 = byte, 1 = short, 2 = long) */
438 FALSE, /* pc_relative */
440 complain_overflow_dont,/* complain_on_overflow */
441 bfd_elf_generic_reloc, /* special_function */
442 "R_AVR_6_ADIW", /* name */
443 FALSE, /* partial_inplace */
444 0xffff, /* src_mask */
445 0xffff, /* dst_mask */
446 FALSE), /* pcrel_offset */
447 /* Most significant 8 bit value of a 32 bit link-time constant. */
448 HOWTO (R_AVR_MS8_LDI, /* type */
450 1, /* size (0 = byte, 1 = short, 2 = long) */
452 FALSE, /* pc_relative */
454 complain_overflow_dont, /* complain_on_overflow */
455 bfd_elf_generic_reloc, /* special_function */
456 "R_AVR_MS8_LDI", /* name */
457 FALSE, /* partial_inplace */
458 0xffff, /* src_mask */
459 0xffff, /* dst_mask */
460 FALSE), /* pcrel_offset */
461 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
462 HOWTO (R_AVR_MS8_LDI_NEG, /* type */
464 1, /* size (0 = byte, 1 = short, 2 = long) */
466 FALSE, /* pc_relative */
468 complain_overflow_dont, /* complain_on_overflow */
469 bfd_elf_generic_reloc, /* special_function */
470 "R_AVR_MS8_LDI_NEG", /* name */
471 FALSE, /* partial_inplace */
472 0xffff, /* src_mask */
473 0xffff, /* dst_mask */
474 FALSE), /* pcrel_offset */
475 /* A low 8 bit absolute relocation of 24 bit program memory address.
476 For LDI command. Will be changed when linker stubs are needed. */
477 HOWTO (R_AVR_LO8_LDI_GS, /* type */
479 1, /* size (0 = byte, 1 = short, 2 = long) */
481 FALSE, /* pc_relative */
483 complain_overflow_dont, /* complain_on_overflow */
484 bfd_elf_generic_reloc, /* special_function */
485 "R_AVR_LO8_LDI_GS", /* name */
486 FALSE, /* partial_inplace */
487 0xffff, /* src_mask */
488 0xffff, /* dst_mask */
489 FALSE), /* pcrel_offset */
490 /* A low 8 bit absolute relocation of 24 bit program memory address.
491 For LDI command. Will be changed when linker stubs are needed. */
492 HOWTO (R_AVR_HI8_LDI_GS, /* type */
494 1, /* size (0 = byte, 1 = short, 2 = long) */
496 FALSE, /* pc_relative */
498 complain_overflow_dont, /* complain_on_overflow */
499 bfd_elf_generic_reloc, /* special_function */
500 "R_AVR_HI8_LDI_GS", /* name */
501 FALSE, /* partial_inplace */
502 0xffff, /* src_mask */
503 0xffff, /* dst_mask */
504 FALSE) /* pcrel_offset */
507 /* Map BFD reloc types to AVR ELF reloc types. */
511 bfd_reloc_code_real_type bfd_reloc_val;
512 unsigned int elf_reloc_val;
515 static const struct avr_reloc_map avr_reloc_map[] =
517 { BFD_RELOC_NONE, R_AVR_NONE },
518 { BFD_RELOC_32, R_AVR_32 },
519 { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL },
520 { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL },
521 { BFD_RELOC_16, R_AVR_16 },
522 { BFD_RELOC_AVR_16_PM, R_AVR_16_PM },
523 { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI},
524 { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI },
525 { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI },
526 { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI },
527 { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG },
528 { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG },
529 { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG },
530 { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG },
531 { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM },
532 { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS },
533 { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM },
534 { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS },
535 { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM },
536 { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG },
537 { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG },
538 { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG },
539 { BFD_RELOC_AVR_CALL, R_AVR_CALL },
540 { BFD_RELOC_AVR_LDI, R_AVR_LDI },
541 { BFD_RELOC_AVR_6, R_AVR_6 },
542 { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW }
545 /* Meant to be filled one day with the wrap around address for the
546 specific device. I.e. should get the value 0x4000 for 16k devices,
547 0x8000 for 32k devices and so on.
549 We initialize it here with a value of 0x1000000 resulting in
550 that we will never suggest a wrap-around jump during relaxation.
551 The logic of the source code later on assumes that in
552 avr_pc_wrap_around one single bit is set. */
553 static bfd_vma avr_pc_wrap_around = 0x10000000;
555 /* If this variable holds a value different from zero, the linker relaxation
556 machine will try to optimize call/ret sequences by a single jump
557 instruction. This option could be switched off by a linker switch. */
558 static int avr_replace_call_ret_sequences = 1;
560 /* Initialize an entry in the stub hash table. */
562 static struct bfd_hash_entry *
563 stub_hash_newfunc (struct bfd_hash_entry *entry,
564 struct bfd_hash_table *table,
567 /* Allocate the structure if it has not already been allocated by a
571 entry = bfd_hash_allocate (table,
572 sizeof (struct elf32_avr_stub_hash_entry));
577 /* Call the allocation method of the superclass. */
578 entry = bfd_hash_newfunc (entry, table, string);
581 struct elf32_avr_stub_hash_entry *hsh;
583 /* Initialize the local fields. */
584 hsh = avr_stub_hash_entry (entry);
585 hsh->stub_offset = 0;
586 hsh->target_value = 0;
592 /* This function is just a straight passthrough to the real
593 function in linker.c. Its prupose is so that its address
594 can be compared inside the avr_link_hash_table macro. */
596 static struct bfd_hash_entry *
597 elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry,
598 struct bfd_hash_table * table,
601 return _bfd_elf_link_hash_newfunc (entry, table, string);
604 /* Create the derived linker hash table. The AVR ELF port uses the derived
605 hash table to keep information specific to the AVR ELF linker (without
606 using static variables). */
608 static struct bfd_link_hash_table *
609 elf32_avr_link_hash_table_create (bfd *abfd)
611 struct elf32_avr_link_hash_table *htab;
612 bfd_size_type amt = sizeof (*htab);
614 htab = bfd_malloc (amt);
618 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
619 elf32_avr_link_hash_newfunc,
620 sizeof (struct elf_link_hash_entry)))
626 /* Init the stub hash table too. */
627 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
628 sizeof (struct elf32_avr_stub_hash_entry)))
631 htab->stub_bfd = NULL;
632 htab->stub_sec = NULL;
634 /* Initialize the address mapping table. */
635 htab->amt_stub_offsets = NULL;
636 htab->amt_destination_addr = NULL;
637 htab->amt_entry_cnt = 0;
638 htab->amt_max_entry_cnt = 0;
640 return &htab->etab.root;
643 /* Free the derived linker hash table. */
646 elf32_avr_link_hash_table_free (struct bfd_link_hash_table *btab)
648 struct elf32_avr_link_hash_table *htab
649 = (struct elf32_avr_link_hash_table *) btab;
651 /* Free the address mapping table. */
652 if (htab->amt_stub_offsets != NULL)
653 free (htab->amt_stub_offsets);
654 if (htab->amt_destination_addr != NULL)
655 free (htab->amt_destination_addr);
657 bfd_hash_table_free (&htab->bstab);
658 _bfd_generic_link_hash_table_free (btab);
661 /* Calculates the effective distance of a pc relative jump/call. */
664 avr_relative_distance_considering_wrap_around (unsigned int distance)
666 unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
667 int dist_with_wrap_around = distance & wrap_around_mask;
669 if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
670 dist_with_wrap_around -= avr_pc_wrap_around;
672 return dist_with_wrap_around;
676 static reloc_howto_type *
677 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
678 bfd_reloc_code_real_type code)
683 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
685 if (avr_reloc_map[i].bfd_reloc_val == code)
686 return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
691 static reloc_howto_type *
692 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
698 i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
700 if (elf_avr_howto_table[i].name != NULL
701 && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
702 return &elf_avr_howto_table[i];
707 /* Set the howto pointer for an AVR ELF reloc. */
710 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
712 Elf_Internal_Rela *dst)
716 r_type = ELF32_R_TYPE (dst->r_info);
717 BFD_ASSERT (r_type < (unsigned int) R_AVR_max);
718 cache_ptr->howto = &elf_avr_howto_table[r_type];
721 /* Look through the relocs for a section during the first phase.
722 Since we don't do .gots or .plts, we just need to consider the
723 virtual table relocs for gc. */
726 elf32_avr_check_relocs (bfd *abfd,
727 struct bfd_link_info *info,
729 const Elf_Internal_Rela *relocs)
731 Elf_Internal_Shdr *symtab_hdr;
732 struct elf_link_hash_entry **sym_hashes;
733 const Elf_Internal_Rela *rel;
734 const Elf_Internal_Rela *rel_end;
736 if (info->relocatable)
739 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
740 sym_hashes = elf_sym_hashes (abfd);
742 rel_end = relocs + sec->reloc_count;
743 for (rel = relocs; rel < rel_end; rel++)
745 struct elf_link_hash_entry *h;
746 unsigned long r_symndx;
748 r_symndx = ELF32_R_SYM (rel->r_info);
749 if (r_symndx < symtab_hdr->sh_info)
753 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
754 while (h->root.type == bfd_link_hash_indirect
755 || h->root.type == bfd_link_hash_warning)
756 h = (struct elf_link_hash_entry *) h->root.u.i.link;
764 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
766 return (relocation >= 0x020000);
769 /* Returns the address of the corresponding stub if there is one.
770 Returns otherwise an address above 0x020000. This function
771 could also be used, if there is no knowledge on the section where
772 the destination is found. */
775 avr_get_stub_addr (bfd_vma srel,
776 struct elf32_avr_link_hash_table *htab)
779 bfd_vma stub_sec_addr =
780 (htab->stub_sec->output_section->vma +
781 htab->stub_sec->output_offset);
783 for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++)
784 if (htab->amt_destination_addr[sindex] == srel)
785 return htab->amt_stub_offsets[sindex] + stub_sec_addr;
787 /* Return an address that could not be reached by 16 bit relocs. */
791 /* Perform a single relocation. By default we use the standard BFD
792 routines, but a few relocs, we have to do them ourselves. */
794 static bfd_reloc_status_type
795 avr_final_link_relocate (reloc_howto_type * howto,
797 asection * input_section,
799 Elf_Internal_Rela * rel,
801 struct elf32_avr_link_hash_table * htab)
803 bfd_reloc_status_type r = bfd_reloc_ok;
806 bfd_signed_vma reloc_addr;
807 bfd_boolean use_stubs = FALSE;
808 /* Usually is 0, unless we are generating code for a bootloader. */
809 bfd_signed_vma base_addr = htab->vector_base;
811 /* Absolute addr of the reloc in the final excecutable. */
812 reloc_addr = rel->r_offset + input_section->output_section->vma
813 + input_section->output_offset;
818 contents += rel->r_offset;
819 srel = (bfd_signed_vma) relocation;
820 srel += rel->r_addend;
821 srel -= rel->r_offset;
822 srel -= 2; /* Branch instructions add 2 to the PC... */
823 srel -= (input_section->output_section->vma +
824 input_section->output_offset);
827 return bfd_reloc_outofrange;
828 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
829 return bfd_reloc_overflow;
830 x = bfd_get_16 (input_bfd, contents);
831 x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
832 bfd_put_16 (input_bfd, x, contents);
836 contents += rel->r_offset;
837 srel = (bfd_signed_vma) relocation;
838 srel += rel->r_addend;
839 srel -= rel->r_offset;
840 srel -= 2; /* Branch instructions add 2 to the PC... */
841 srel -= (input_section->output_section->vma +
842 input_section->output_offset);
845 return bfd_reloc_outofrange;
847 srel = avr_relative_distance_considering_wrap_around (srel);
849 /* AVR addresses commands as words. */
852 /* Check for overflow. */
853 if (srel < -2048 || srel > 2047)
855 /* Relative distance is too large. */
857 /* Always apply WRAPAROUND for avr2, avr25, and avr4. */
858 switch (bfd_get_mach (input_bfd))
866 return bfd_reloc_overflow;
870 x = bfd_get_16 (input_bfd, contents);
871 x = (x & 0xf000) | (srel & 0xfff);
872 bfd_put_16 (input_bfd, x, contents);
876 contents += rel->r_offset;
877 srel = (bfd_signed_vma) relocation + rel->r_addend;
878 x = bfd_get_16 (input_bfd, contents);
879 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
880 bfd_put_16 (input_bfd, x, contents);
884 contents += rel->r_offset;
885 srel = (bfd_signed_vma) relocation + rel->r_addend;
886 if (((srel > 0) && (srel & 0xffff) > 255)
887 || ((srel < 0) && ((-srel) & 0xffff) > 128))
888 /* Remove offset for data/eeprom section. */
889 return bfd_reloc_overflow;
891 x = bfd_get_16 (input_bfd, contents);
892 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
893 bfd_put_16 (input_bfd, x, contents);
897 contents += rel->r_offset;
898 srel = (bfd_signed_vma) relocation + rel->r_addend;
899 if (((srel & 0xffff) > 63) || (srel < 0))
900 /* Remove offset for data/eeprom section. */
901 return bfd_reloc_overflow;
902 x = bfd_get_16 (input_bfd, contents);
903 x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
904 | ((srel & (1 << 5)) << 8));
905 bfd_put_16 (input_bfd, x, contents);
909 contents += rel->r_offset;
910 srel = (bfd_signed_vma) relocation + rel->r_addend;
911 if (((srel & 0xffff) > 63) || (srel < 0))
912 /* Remove offset for data/eeprom section. */
913 return bfd_reloc_overflow;
914 x = bfd_get_16 (input_bfd, contents);
915 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
916 bfd_put_16 (input_bfd, x, contents);
920 contents += rel->r_offset;
921 srel = (bfd_signed_vma) relocation + rel->r_addend;
922 srel = (srel >> 8) & 0xff;
923 x = bfd_get_16 (input_bfd, contents);
924 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
925 bfd_put_16 (input_bfd, x, contents);
929 contents += rel->r_offset;
930 srel = (bfd_signed_vma) relocation + rel->r_addend;
931 srel = (srel >> 16) & 0xff;
932 x = bfd_get_16 (input_bfd, contents);
933 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
934 bfd_put_16 (input_bfd, x, contents);
938 contents += rel->r_offset;
939 srel = (bfd_signed_vma) relocation + rel->r_addend;
940 srel = (srel >> 24) & 0xff;
941 x = bfd_get_16 (input_bfd, contents);
942 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
943 bfd_put_16 (input_bfd, x, contents);
946 case R_AVR_LO8_LDI_NEG:
947 contents += rel->r_offset;
948 srel = (bfd_signed_vma) relocation + rel->r_addend;
950 x = bfd_get_16 (input_bfd, contents);
951 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
952 bfd_put_16 (input_bfd, x, contents);
955 case R_AVR_HI8_LDI_NEG:
956 contents += rel->r_offset;
957 srel = (bfd_signed_vma) relocation + rel->r_addend;
959 srel = (srel >> 8) & 0xff;
960 x = bfd_get_16 (input_bfd, contents);
961 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
962 bfd_put_16 (input_bfd, x, contents);
965 case R_AVR_HH8_LDI_NEG:
966 contents += rel->r_offset;
967 srel = (bfd_signed_vma) relocation + rel->r_addend;
969 srel = (srel >> 16) & 0xff;
970 x = bfd_get_16 (input_bfd, contents);
971 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
972 bfd_put_16 (input_bfd, x, contents);
975 case R_AVR_MS8_LDI_NEG:
976 contents += rel->r_offset;
977 srel = (bfd_signed_vma) relocation + rel->r_addend;
979 srel = (srel >> 24) & 0xff;
980 x = bfd_get_16 (input_bfd, contents);
981 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
982 bfd_put_16 (input_bfd, x, contents);
985 case R_AVR_LO8_LDI_GS:
986 use_stubs = (!htab->no_stubs);
988 case R_AVR_LO8_LDI_PM:
989 contents += rel->r_offset;
990 srel = (bfd_signed_vma) relocation + rel->r_addend;
993 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
995 bfd_vma old_srel = srel;
997 /* We need to use the address of the stub instead. */
998 srel = avr_get_stub_addr (srel, htab);
1000 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1001 "reloc at address 0x%x.\n",
1002 (unsigned int) srel,
1003 (unsigned int) old_srel,
1004 (unsigned int) reloc_addr);
1006 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1007 return bfd_reloc_outofrange;
1011 return bfd_reloc_outofrange;
1013 x = bfd_get_16 (input_bfd, contents);
1014 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1015 bfd_put_16 (input_bfd, x, contents);
1018 case R_AVR_HI8_LDI_GS:
1019 use_stubs = (!htab->no_stubs);
1021 case R_AVR_HI8_LDI_PM:
1022 contents += rel->r_offset;
1023 srel = (bfd_signed_vma) relocation + rel->r_addend;
1026 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1028 bfd_vma old_srel = srel;
1030 /* We need to use the address of the stub instead. */
1031 srel = avr_get_stub_addr (srel, htab);
1033 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1034 "reloc at address 0x%x.\n",
1035 (unsigned int) srel,
1036 (unsigned int) old_srel,
1037 (unsigned int) reloc_addr);
1039 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1040 return bfd_reloc_outofrange;
1044 return bfd_reloc_outofrange;
1046 srel = (srel >> 8) & 0xff;
1047 x = bfd_get_16 (input_bfd, contents);
1048 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1049 bfd_put_16 (input_bfd, x, contents);
1052 case R_AVR_HH8_LDI_PM:
1053 contents += rel->r_offset;
1054 srel = (bfd_signed_vma) relocation + rel->r_addend;
1056 return bfd_reloc_outofrange;
1058 srel = (srel >> 16) & 0xff;
1059 x = bfd_get_16 (input_bfd, contents);
1060 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1061 bfd_put_16 (input_bfd, x, contents);
1064 case R_AVR_LO8_LDI_PM_NEG:
1065 contents += rel->r_offset;
1066 srel = (bfd_signed_vma) relocation + rel->r_addend;
1069 return bfd_reloc_outofrange;
1071 x = bfd_get_16 (input_bfd, contents);
1072 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1073 bfd_put_16 (input_bfd, x, contents);
1076 case R_AVR_HI8_LDI_PM_NEG:
1077 contents += rel->r_offset;
1078 srel = (bfd_signed_vma) relocation + rel->r_addend;
1081 return bfd_reloc_outofrange;
1083 srel = (srel >> 8) & 0xff;
1084 x = bfd_get_16 (input_bfd, contents);
1085 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1086 bfd_put_16 (input_bfd, x, contents);
1089 case R_AVR_HH8_LDI_PM_NEG:
1090 contents += rel->r_offset;
1091 srel = (bfd_signed_vma) relocation + rel->r_addend;
1094 return bfd_reloc_outofrange;
1096 srel = (srel >> 16) & 0xff;
1097 x = bfd_get_16 (input_bfd, contents);
1098 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1099 bfd_put_16 (input_bfd, x, contents);
1103 contents += rel->r_offset;
1104 srel = (bfd_signed_vma) relocation + rel->r_addend;
1106 return bfd_reloc_outofrange;
1108 x = bfd_get_16 (input_bfd, contents);
1109 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1110 bfd_put_16 (input_bfd, x, contents);
1111 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1115 use_stubs = (!htab->no_stubs);
1116 contents += rel->r_offset;
1117 srel = (bfd_signed_vma) relocation + rel->r_addend;
1120 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1122 bfd_vma old_srel = srel;
1124 /* We need to use the address of the stub instead. */
1125 srel = avr_get_stub_addr (srel,htab);
1127 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1128 "reloc at address 0x%x.\n",
1129 (unsigned int) srel,
1130 (unsigned int) old_srel,
1131 (unsigned int) reloc_addr);
1133 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1134 return bfd_reloc_outofrange;
1138 return bfd_reloc_outofrange;
1140 bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1144 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1145 contents, rel->r_offset,
1146 relocation, rel->r_addend);
1152 /* Relocate an AVR ELF section. */
1155 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1156 struct bfd_link_info *info,
1158 asection *input_section,
1160 Elf_Internal_Rela *relocs,
1161 Elf_Internal_Sym *local_syms,
1162 asection **local_sections)
1164 Elf_Internal_Shdr * symtab_hdr;
1165 struct elf_link_hash_entry ** sym_hashes;
1166 Elf_Internal_Rela * rel;
1167 Elf_Internal_Rela * relend;
1168 struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
1170 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1171 sym_hashes = elf_sym_hashes (input_bfd);
1172 relend = relocs + input_section->reloc_count;
1174 for (rel = relocs; rel < relend; rel ++)
1176 reloc_howto_type * howto;
1177 unsigned long r_symndx;
1178 Elf_Internal_Sym * sym;
1180 struct elf_link_hash_entry * h;
1182 bfd_reloc_status_type r;
1186 r_type = ELF32_R_TYPE (rel->r_info);
1187 r_symndx = ELF32_R_SYM (rel->r_info);
1188 howto = elf_avr_howto_table + ELF32_R_TYPE (rel->r_info);
1193 if (r_symndx < symtab_hdr->sh_info)
1195 sym = local_syms + r_symndx;
1196 sec = local_sections [r_symndx];
1197 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1199 name = bfd_elf_string_from_elf_section
1200 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1201 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1205 bfd_boolean unresolved_reloc, warned;
1207 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1208 r_symndx, symtab_hdr, sym_hashes,
1210 unresolved_reloc, warned);
1212 name = h->root.root.string;
1215 if (sec != NULL && elf_discarded_section (sec))
1217 /* For relocs against symbols from removed linkonce sections,
1218 or sections discarded by a linker script, we just want the
1219 section contents zeroed. Avoid any special processing. */
1220 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1226 if (info->relocatable)
1229 r = avr_final_link_relocate (howto, input_bfd, input_section,
1230 contents, rel, relocation, htab);
1232 if (r != bfd_reloc_ok)
1234 const char * msg = (const char *) NULL;
1238 case bfd_reloc_overflow:
1239 r = info->callbacks->reloc_overflow
1240 (info, (h ? &h->root : NULL),
1241 name, howto->name, (bfd_vma) 0,
1242 input_bfd, input_section, rel->r_offset);
1245 case bfd_reloc_undefined:
1246 r = info->callbacks->undefined_symbol
1247 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1250 case bfd_reloc_outofrange:
1251 msg = _("internal error: out of range error");
1254 case bfd_reloc_notsupported:
1255 msg = _("internal error: unsupported relocation error");
1258 case bfd_reloc_dangerous:
1259 msg = _("internal error: dangerous relocation");
1263 msg = _("internal error: unknown error");
1268 r = info->callbacks->warning
1269 (info, msg, name, input_bfd, input_section, rel->r_offset);
1279 /* The final processing done just before writing out a AVR ELF object
1280 file. This gets the AVR architecture right based on the machine
1284 bfd_elf_avr_final_write_processing (bfd *abfd,
1285 bfd_boolean linker ATTRIBUTE_UNUSED)
1289 switch (bfd_get_mach (abfd))
1293 val = E_AVR_MACH_AVR2;
1297 val = E_AVR_MACH_AVR1;
1300 case bfd_mach_avr25:
1301 val = E_AVR_MACH_AVR25;
1305 val = E_AVR_MACH_AVR3;
1308 case bfd_mach_avr31:
1309 val = E_AVR_MACH_AVR31;
1312 case bfd_mach_avr35:
1313 val = E_AVR_MACH_AVR35;
1317 val = E_AVR_MACH_AVR4;
1321 val = E_AVR_MACH_AVR5;
1324 case bfd_mach_avr51:
1325 val = E_AVR_MACH_AVR51;
1329 val = E_AVR_MACH_AVR6;
1333 elf_elfheader (abfd)->e_machine = EM_AVR;
1334 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1335 elf_elfheader (abfd)->e_flags |= val;
1336 elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
1339 /* Set the right machine number. */
1342 elf32_avr_object_p (bfd *abfd)
1344 unsigned int e_set = bfd_mach_avr2;
1346 if (elf_elfheader (abfd)->e_machine == EM_AVR
1347 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1349 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1354 case E_AVR_MACH_AVR2:
1355 e_set = bfd_mach_avr2;
1358 case E_AVR_MACH_AVR1:
1359 e_set = bfd_mach_avr1;
1362 case E_AVR_MACH_AVR25:
1363 e_set = bfd_mach_avr25;
1366 case E_AVR_MACH_AVR3:
1367 e_set = bfd_mach_avr3;
1370 case E_AVR_MACH_AVR31:
1371 e_set = bfd_mach_avr31;
1374 case E_AVR_MACH_AVR35:
1375 e_set = bfd_mach_avr35;
1378 case E_AVR_MACH_AVR4:
1379 e_set = bfd_mach_avr4;
1382 case E_AVR_MACH_AVR5:
1383 e_set = bfd_mach_avr5;
1386 case E_AVR_MACH_AVR51:
1387 e_set = bfd_mach_avr51;
1390 case E_AVR_MACH_AVR6:
1391 e_set = bfd_mach_avr6;
1395 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1400 /* Delete some bytes from a section while changing the size of an instruction.
1401 The parameter "addr" denotes the section-relative offset pointing just
1402 behind the shrinked instruction. "addr+count" point at the first
1403 byte just behind the original unshrinked instruction. */
1406 elf32_avr_relax_delete_bytes (bfd *abfd,
1411 Elf_Internal_Shdr *symtab_hdr;
1412 unsigned int sec_shndx;
1414 Elf_Internal_Rela *irel, *irelend;
1415 Elf_Internal_Rela *irelalign;
1416 Elf_Internal_Sym *isym;
1417 Elf_Internal_Sym *isymbuf = NULL;
1419 struct elf_link_hash_entry **sym_hashes;
1420 struct elf_link_hash_entry **end_hashes;
1421 unsigned int symcount;
1423 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1424 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1425 contents = elf_section_data (sec)->this_hdr.contents;
1427 /* The deletion must stop at the next ALIGN reloc for an aligment
1428 power larger than the number of bytes we are deleting. */
1433 irel = elf_section_data (sec)->relocs;
1434 irelend = irel + sec->reloc_count;
1436 /* Actually delete the bytes. */
1437 if (toaddr - addr - count > 0)
1438 memmove (contents + addr, contents + addr + count,
1439 (size_t) (toaddr - addr - count));
1442 /* Adjust all the reloc addresses. */
1443 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1445 bfd_vma old_reloc_address;
1446 bfd_vma shrinked_insn_address;
1448 old_reloc_address = (sec->output_section->vma
1449 + sec->output_offset + irel->r_offset);
1450 shrinked_insn_address = (sec->output_section->vma
1451 + sec->output_offset + addr - count);
1453 /* Get the new reloc address. */
1454 if ((irel->r_offset > addr
1455 && irel->r_offset < toaddr))
1458 printf ("Relocation at address 0x%x needs to be moved.\n"
1459 "Old section offset: 0x%x, New section offset: 0x%x \n",
1460 (unsigned int) old_reloc_address,
1461 (unsigned int) irel->r_offset,
1462 (unsigned int) ((irel->r_offset) - count));
1464 irel->r_offset -= count;
1469 /* The reloc's own addresses are now ok. However, we need to readjust
1470 the reloc's addend, i.e. the reloc's value if two conditions are met:
1471 1.) the reloc is relative to a symbol in this section that
1472 is located in front of the shrinked instruction
1473 2.) symbol plus addend end up behind the shrinked instruction.
1475 The most common case where this happens are relocs relative to
1476 the section-start symbol.
1478 This step needs to be done for all of the sections of the bfd. */
1481 struct bfd_section *isec;
1483 for (isec = abfd->sections; isec; isec = isec->next)
1486 bfd_vma shrinked_insn_address;
1488 shrinked_insn_address = (sec->output_section->vma
1489 + sec->output_offset + addr - count);
1491 irelend = elf_section_data (isec)->relocs + isec->reloc_count;
1492 for (irel = elf_section_data (isec)->relocs;
1496 /* Read this BFD's local symbols if we haven't done
1498 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1500 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1501 if (isymbuf == NULL)
1502 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1503 symtab_hdr->sh_info, 0,
1505 if (isymbuf == NULL)
1509 /* Get the value of the symbol referred to by the reloc. */
1510 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1512 /* A local symbol. */
1515 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1516 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1517 symval = isym->st_value;
1518 /* If the reloc is absolute, it will not have
1519 a symbol or section associated with it. */
1522 symval += sym_sec->output_section->vma
1523 + sym_sec->output_offset;
1526 printf ("Checking if the relocation's "
1527 "addend needs corrections.\n"
1528 "Address of anchor symbol: 0x%x \n"
1529 "Address of relocation target: 0x%x \n"
1530 "Address of relaxed insn: 0x%x \n",
1531 (unsigned int) symval,
1532 (unsigned int) (symval + irel->r_addend),
1533 (unsigned int) shrinked_insn_address);
1535 if (symval <= shrinked_insn_address
1536 && (symval + irel->r_addend) > shrinked_insn_address)
1538 irel->r_addend -= count;
1541 printf ("Relocation's addend needed to be fixed \n");
1544 /* else...Reference symbol is absolute. No adjustment needed. */
1546 /* else...Reference symbol is extern. No need for adjusting
1552 /* Adjust the local symbols defined in this section. */
1553 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1554 /* Fix PR 9841, there may be no local symbols. */
1557 Elf_Internal_Sym *isymend;
1559 isymend = isym + symtab_hdr->sh_info;
1560 for (; isym < isymend; isym++)
1562 if (isym->st_shndx == sec_shndx
1563 && isym->st_value > addr
1564 && isym->st_value < toaddr)
1565 isym->st_value -= count;
1569 /* Now adjust the global symbols defined in this section. */
1570 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1571 - symtab_hdr->sh_info);
1572 sym_hashes = elf_sym_hashes (abfd);
1573 end_hashes = sym_hashes + symcount;
1574 for (; sym_hashes < end_hashes; sym_hashes++)
1576 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1577 if ((sym_hash->root.type == bfd_link_hash_defined
1578 || sym_hash->root.type == bfd_link_hash_defweak)
1579 && sym_hash->root.u.def.section == sec
1580 && sym_hash->root.u.def.value > addr
1581 && sym_hash->root.u.def.value < toaddr)
1583 sym_hash->root.u.def.value -= count;
1590 /* This function handles relaxing for the avr.
1591 Many important relaxing opportunities within functions are already
1592 realized by the compiler itself.
1593 Here we try to replace call (4 bytes) -> rcall (2 bytes)
1594 and jump -> rjmp (safes also 2 bytes).
1595 As well we now optimize seqences of
1596 - call/rcall function
1601 . In case that within a sequence
1604 the ret could no longer be reached it is optimized away. In order
1605 to check if the ret is no longer needed, it is checked that the ret's address
1606 is not the target of a branch or jump within the same section, it is checked
1607 that there is no skip instruction before the jmp/rjmp and that there
1608 is no local or global label place at the address of the ret.
1610 We refrain from relaxing within sections ".vectors" and
1611 ".jumptables" in order to maintain the position of the instructions.
1612 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
1613 if possible. (In future one could possibly use the space of the nop
1614 for the first instruction of the irq service function.
1616 The .jumptables sections is meant to be used for a future tablejump variant
1617 for the devices with 3-byte program counter where the table itself
1618 contains 4-byte jump instructions whose relative offset must not
1622 elf32_avr_relax_section (bfd *abfd,
1624 struct bfd_link_info *link_info,
1627 Elf_Internal_Shdr *symtab_hdr;
1628 Elf_Internal_Rela *internal_relocs;
1629 Elf_Internal_Rela *irel, *irelend;
1630 bfd_byte *contents = NULL;
1631 Elf_Internal_Sym *isymbuf = NULL;
1632 static asection *last_input_section = NULL;
1633 static Elf_Internal_Rela *last_reloc = NULL;
1634 struct elf32_avr_link_hash_table *htab;
1636 if (link_info->relocatable)
1637 (*link_info->callbacks->einfo)
1638 (_("%P%F: --relax and -r may not be used together\n"));
1640 htab = avr_link_hash_table (link_info);
1644 /* Assume nothing changes. */
1647 if ((!htab->no_stubs) && (sec == htab->stub_sec))
1649 /* We are just relaxing the stub section.
1650 Let's calculate the size needed again. */
1651 bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
1654 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
1655 (int) last_estimated_stub_section_size);
1657 elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
1660 /* Check if the number of trampolines changed. */
1661 if (last_estimated_stub_section_size != htab->stub_sec->size)
1665 printf ("Size of stub section after this pass: %i\n",
1666 (int) htab->stub_sec->size);
1671 /* We don't have to do anything for a relocatable link, if
1672 this section does not have relocs, or if this is not a
1674 if (link_info->relocatable
1675 || (sec->flags & SEC_RELOC) == 0
1676 || sec->reloc_count == 0
1677 || (sec->flags & SEC_CODE) == 0)
1680 /* Check if the object file to relax uses internal symbols so that we
1681 could fix up the relocations. */
1682 if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
1685 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1687 /* Get a copy of the native relocations. */
1688 internal_relocs = (_bfd_elf_link_read_relocs
1689 (abfd, sec, NULL, NULL, link_info->keep_memory));
1690 if (internal_relocs == NULL)
1693 if (sec != last_input_section)
1696 last_input_section = sec;
1698 /* Walk through the relocs looking for relaxing opportunities. */
1699 irelend = internal_relocs + sec->reloc_count;
1700 for (irel = internal_relocs; irel < irelend; irel++)
1704 if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
1705 && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
1706 && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
1709 /* Get the section contents if we haven't done so already. */
1710 if (contents == NULL)
1712 /* Get cached copy if it exists. */
1713 if (elf_section_data (sec)->this_hdr.contents != NULL)
1714 contents = elf_section_data (sec)->this_hdr.contents;
1717 /* Go get them off disk. */
1718 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
1723 /* Read this BFD's local symbols if we haven't done so already. */
1724 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1726 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1727 if (isymbuf == NULL)
1728 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1729 symtab_hdr->sh_info, 0,
1731 if (isymbuf == NULL)
1736 /* Get the value of the symbol referred to by the reloc. */
1737 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1739 /* A local symbol. */
1740 Elf_Internal_Sym *isym;
1743 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1744 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1745 symval = isym->st_value;
1746 /* If the reloc is absolute, it will not have
1747 a symbol or section associated with it. */
1749 symval += sym_sec->output_section->vma
1750 + sym_sec->output_offset;
1755 struct elf_link_hash_entry *h;
1757 /* An external symbol. */
1758 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1759 h = elf_sym_hashes (abfd)[indx];
1760 BFD_ASSERT (h != NULL);
1761 if (h->root.type != bfd_link_hash_defined
1762 && h->root.type != bfd_link_hash_defweak)
1763 /* This appears to be a reference to an undefined
1764 symbol. Just ignore it--it will be caught by the
1765 regular reloc processing. */
1768 symval = (h->root.u.def.value
1769 + h->root.u.def.section->output_section->vma
1770 + h->root.u.def.section->output_offset);
1773 /* For simplicity of coding, we are going to modify the section
1774 contents, the section relocs, and the BFD symbol table. We
1775 must tell the rest of the code not to free up this
1776 information. It would be possible to instead create a table
1777 of changes which have to be made, as is done in coff-mips.c;
1778 that would be more work, but would require less memory when
1779 the linker is run. */
1780 switch (ELF32_R_TYPE (irel->r_info))
1782 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1783 pc-relative rcall/rjmp. */
1786 bfd_vma value = symval + irel->r_addend;
1788 int distance_short_enough = 0;
1790 /* Get the address of this instruction. */
1791 dot = (sec->output_section->vma
1792 + sec->output_offset + irel->r_offset);
1794 /* Compute the distance from this insn to the branch target. */
1797 /* If the distance is within -4094..+4098 inclusive, then we can
1798 relax this jump/call. +4098 because the call/jump target
1799 will be closer after the relaxation. */
1800 if ((int) gap >= -4094 && (int) gap <= 4098)
1801 distance_short_enough = 1;
1803 /* Here we handle the wrap-around case. E.g. for a 16k device
1804 we could use a rjmp to jump from address 0x100 to 0x3d00!
1805 In order to make this work properly, we need to fill the
1806 vaiable avr_pc_wrap_around with the appropriate value.
1807 I.e. 0x4000 for a 16k device. */
1809 /* Shrinking the code size makes the gaps larger in the
1810 case of wrap-arounds. So we use a heuristical safety
1811 margin to avoid that during relax the distance gets
1812 again too large for the short jumps. Let's assume
1813 a typical code-size reduction due to relax for a
1814 16k device of 600 bytes. So let's use twice the
1815 typical value as safety margin. */
1819 int assumed_shrink = 600;
1820 if (avr_pc_wrap_around > 0x4000)
1821 assumed_shrink = 900;
1823 safety_margin = 2 * assumed_shrink;
1825 rgap = avr_relative_distance_considering_wrap_around (gap);
1827 if (rgap >= (-4092 + safety_margin)
1828 && rgap <= (4094 - safety_margin))
1829 distance_short_enough = 1;
1832 if (distance_short_enough)
1834 unsigned char code_msb;
1835 unsigned char code_lsb;
1838 printf ("shrinking jump/call instruction at address 0x%x"
1839 " in section %s\n\n",
1840 (int) dot, sec->name);
1842 /* Note that we've changed the relocs, section contents,
1844 elf_section_data (sec)->relocs = internal_relocs;
1845 elf_section_data (sec)->this_hdr.contents = contents;
1846 symtab_hdr->contents = (unsigned char *) isymbuf;
1848 /* Get the instruction code for relaxing. */
1849 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
1850 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1852 /* Mask out the relocation bits. */
1855 if (code_msb == 0x94 && code_lsb == 0x0E)
1857 /* we are changing call -> rcall . */
1858 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1859 bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
1861 else if (code_msb == 0x94 && code_lsb == 0x0C)
1863 /* we are changeing jump -> rjmp. */
1864 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1865 bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
1870 /* Fix the relocation's type. */
1871 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1874 /* Check for the vector section. There we don't want to
1875 modify the ordering! */
1877 if (!strcmp (sec->name,".vectors")
1878 || !strcmp (sec->name,".jumptables"))
1880 /* Let's insert a nop. */
1881 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
1882 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
1886 /* Delete two bytes of data. */
1887 if (!elf32_avr_relax_delete_bytes (abfd, sec,
1888 irel->r_offset + 2, 2))
1891 /* That will change things, so, we should relax again.
1892 Note that this is not required, and it may be slow. */
1900 unsigned char code_msb;
1901 unsigned char code_lsb;
1904 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1905 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
1907 /* Get the address of this instruction. */
1908 dot = (sec->output_section->vma
1909 + sec->output_offset + irel->r_offset);
1911 /* Here we look for rcall/ret or call/ret sequences that could be
1912 safely replaced by rjmp/ret or jmp/ret. */
1913 if (((code_msb & 0xf0) == 0xd0)
1914 && avr_replace_call_ret_sequences)
1916 /* This insn is a rcall. */
1917 unsigned char next_insn_msb = 0;
1918 unsigned char next_insn_lsb = 0;
1920 if (irel->r_offset + 3 < sec->size)
1923 bfd_get_8 (abfd, contents + irel->r_offset + 3);
1925 bfd_get_8 (abfd, contents + irel->r_offset + 2);
1928 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1930 /* The next insn is a ret. We now convert the rcall insn
1931 into a rjmp instruction. */
1933 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
1935 printf ("converted rcall/ret sequence at address 0x%x"
1936 " into rjmp/ret sequence. Section is %s\n\n",
1937 (int) dot, sec->name);
1942 else if ((0x94 == (code_msb & 0xfe))
1943 && (0x0e == (code_lsb & 0x0e))
1944 && avr_replace_call_ret_sequences)
1946 /* This insn is a call. */
1947 unsigned char next_insn_msb = 0;
1948 unsigned char next_insn_lsb = 0;
1950 if (irel->r_offset + 5 < sec->size)
1953 bfd_get_8 (abfd, contents + irel->r_offset + 5);
1955 bfd_get_8 (abfd, contents + irel->r_offset + 4);
1958 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1960 /* The next insn is a ret. We now convert the call insn
1961 into a jmp instruction. */
1964 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
1966 printf ("converted call/ret sequence at address 0x%x"
1967 " into jmp/ret sequence. Section is %s\n\n",
1968 (int) dot, sec->name);
1973 else if ((0xc0 == (code_msb & 0xf0))
1974 || ((0x94 == (code_msb & 0xfe))
1975 && (0x0c == (code_lsb & 0x0e))))
1977 /* This insn is a rjmp or a jmp. */
1978 unsigned char next_insn_msb = 0;
1979 unsigned char next_insn_lsb = 0;
1982 if (0xc0 == (code_msb & 0xf0))
1983 insn_size = 2; /* rjmp insn */
1985 insn_size = 4; /* jmp insn */
1987 if (irel->r_offset + insn_size + 1 < sec->size)
1990 bfd_get_8 (abfd, contents + irel->r_offset
1993 bfd_get_8 (abfd, contents + irel->r_offset
1997 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1999 /* The next insn is a ret. We possibly could delete
2000 this ret. First we need to check for preceeding
2001 sbis/sbic/sbrs or cpse "skip" instructions. */
2003 int there_is_preceeding_non_skip_insn = 1;
2004 bfd_vma address_of_ret;
2006 address_of_ret = dot + insn_size;
2008 if (debug_relax && (insn_size == 2))
2009 printf ("found rjmp / ret sequence at address 0x%x\n",
2011 if (debug_relax && (insn_size == 4))
2012 printf ("found jmp / ret sequence at address 0x%x\n",
2015 /* We have to make sure that there is a preceeding insn. */
2016 if (irel->r_offset >= 2)
2018 unsigned char preceeding_msb;
2019 unsigned char preceeding_lsb;
2021 bfd_get_8 (abfd, contents + irel->r_offset - 1);
2023 bfd_get_8 (abfd, contents + irel->r_offset - 2);
2026 if (0x99 == preceeding_msb)
2027 there_is_preceeding_non_skip_insn = 0;
2030 if (0x9b == preceeding_msb)
2031 there_is_preceeding_non_skip_insn = 0;
2034 if ((0xfc == (preceeding_msb & 0xfe)
2035 && (0x00 == (preceeding_lsb & 0x08))))
2036 there_is_preceeding_non_skip_insn = 0;
2039 if ((0xfe == (preceeding_msb & 0xfe)
2040 && (0x00 == (preceeding_lsb & 0x08))))
2041 there_is_preceeding_non_skip_insn = 0;
2044 if (0x10 == (preceeding_msb & 0xfc))
2045 there_is_preceeding_non_skip_insn = 0;
2047 if (there_is_preceeding_non_skip_insn == 0)
2049 printf ("preceeding skip insn prevents deletion of"
2050 " ret insn at addr 0x%x in section %s\n",
2051 (int) dot + 2, sec->name);
2055 /* There is no previous instruction. */
2056 there_is_preceeding_non_skip_insn = 0;
2059 if (there_is_preceeding_non_skip_insn)
2061 /* We now only have to make sure that there is no
2062 local label defined at the address of the ret
2063 instruction and that there is no local relocation
2064 in this section pointing to the ret. */
2066 int deleting_ret_is_safe = 1;
2067 unsigned int section_offset_of_ret_insn =
2068 irel->r_offset + insn_size;
2069 Elf_Internal_Sym *isym, *isymend;
2070 unsigned int sec_shndx;
2073 _bfd_elf_section_from_bfd_section (abfd, sec);
2075 /* Check for local symbols. */
2076 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2077 isymend = isym + symtab_hdr->sh_info;
2078 /* PR 6019: There may not be any local symbols. */
2079 for (; isym != NULL && isym < isymend; isym++)
2081 if (isym->st_value == section_offset_of_ret_insn
2082 && isym->st_shndx == sec_shndx)
2084 deleting_ret_is_safe = 0;
2086 printf ("local label prevents deletion of ret "
2087 "insn at address 0x%x\n",
2088 (int) dot + insn_size);
2092 /* Now check for global symbols. */
2095 struct elf_link_hash_entry **sym_hashes;
2096 struct elf_link_hash_entry **end_hashes;
2098 symcount = (symtab_hdr->sh_size
2099 / sizeof (Elf32_External_Sym)
2100 - symtab_hdr->sh_info);
2101 sym_hashes = elf_sym_hashes (abfd);
2102 end_hashes = sym_hashes + symcount;
2103 for (; sym_hashes < end_hashes; sym_hashes++)
2105 struct elf_link_hash_entry *sym_hash =
2107 if ((sym_hash->root.type == bfd_link_hash_defined
2108 || sym_hash->root.type ==
2109 bfd_link_hash_defweak)
2110 && sym_hash->root.u.def.section == sec
2111 && sym_hash->root.u.def.value == section_offset_of_ret_insn)
2113 deleting_ret_is_safe = 0;
2115 printf ("global label prevents deletion of "
2116 "ret insn at address 0x%x\n",
2117 (int) dot + insn_size);
2121 /* Now we check for relocations pointing to ret. */
2123 Elf_Internal_Rela *rel;
2124 Elf_Internal_Rela *relend;
2126 relend = elf_section_data (sec)->relocs
2129 for (rel = elf_section_data (sec)->relocs;
2130 rel < relend; rel++)
2132 bfd_vma reloc_target = 0;
2134 /* Read this BFD's local symbols if we haven't
2136 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2138 isymbuf = (Elf_Internal_Sym *)
2139 symtab_hdr->contents;
2140 if (isymbuf == NULL)
2141 isymbuf = bfd_elf_get_elf_syms
2144 symtab_hdr->sh_info, 0,
2146 if (isymbuf == NULL)
2150 /* Get the value of the symbol referred to
2152 if (ELF32_R_SYM (rel->r_info)
2153 < symtab_hdr->sh_info)
2155 /* A local symbol. */
2159 + ELF32_R_SYM (rel->r_info);
2160 sym_sec = bfd_section_from_elf_index
2161 (abfd, isym->st_shndx);
2162 symval = isym->st_value;
2164 /* If the reloc is absolute, it will not
2165 have a symbol or section associated
2171 sym_sec->output_section->vma
2172 + sym_sec->output_offset;
2173 reloc_target = symval + rel->r_addend;
2177 reloc_target = symval + rel->r_addend;
2178 /* Reference symbol is absolute. */
2181 /* else ... reference symbol is extern. */
2183 if (address_of_ret == reloc_target)
2185 deleting_ret_is_safe = 0;
2188 "rjmp/jmp ret sequence at address"
2189 " 0x%x could not be deleted. ret"
2190 " is target of a relocation.\n",
2191 (int) address_of_ret);
2196 if (deleting_ret_is_safe)
2199 printf ("unreachable ret instruction "
2200 "at address 0x%x deleted.\n",
2201 (int) dot + insn_size);
2203 /* Delete two bytes of data. */
2204 if (!elf32_avr_relax_delete_bytes (abfd, sec,
2205 irel->r_offset + insn_size, 2))
2208 /* That will change things, so, we should relax
2209 again. Note that this is not required, and it
2223 if (contents != NULL
2224 && elf_section_data (sec)->this_hdr.contents != contents)
2226 if (! link_info->keep_memory)
2230 /* Cache the section contents for elf_link_input_bfd. */
2231 elf_section_data (sec)->this_hdr.contents = contents;
2235 if (internal_relocs != NULL
2236 && elf_section_data (sec)->relocs != internal_relocs)
2237 free (internal_relocs);
2243 && symtab_hdr->contents != (unsigned char *) isymbuf)
2245 if (contents != NULL
2246 && elf_section_data (sec)->this_hdr.contents != contents)
2248 if (internal_relocs != NULL
2249 && elf_section_data (sec)->relocs != internal_relocs)
2250 free (internal_relocs);
2255 /* This is a version of bfd_generic_get_relocated_section_contents
2256 which uses elf32_avr_relocate_section.
2258 For avr it's essentially a cut and paste taken from the H8300 port.
2259 The author of the relaxation support patch for avr had absolutely no
2260 clue what is happening here but found out that this part of the code
2261 seems to be important. */
2264 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
2265 struct bfd_link_info *link_info,
2266 struct bfd_link_order *link_order,
2268 bfd_boolean relocatable,
2271 Elf_Internal_Shdr *symtab_hdr;
2272 asection *input_section = link_order->u.indirect.section;
2273 bfd *input_bfd = input_section->owner;
2274 asection **sections = NULL;
2275 Elf_Internal_Rela *internal_relocs = NULL;
2276 Elf_Internal_Sym *isymbuf = NULL;
2278 /* We only need to handle the case of relaxing, or of having a
2279 particular set of section contents, specially. */
2281 || elf_section_data (input_section)->this_hdr.contents == NULL)
2282 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2286 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2288 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2289 (size_t) input_section->size);
2291 if ((input_section->flags & SEC_RELOC) != 0
2292 && input_section->reloc_count > 0)
2295 Elf_Internal_Sym *isym, *isymend;
2298 internal_relocs = (_bfd_elf_link_read_relocs
2299 (input_bfd, input_section, NULL, NULL, FALSE));
2300 if (internal_relocs == NULL)
2303 if (symtab_hdr->sh_info != 0)
2305 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2306 if (isymbuf == NULL)
2307 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2308 symtab_hdr->sh_info, 0,
2310 if (isymbuf == NULL)
2314 amt = symtab_hdr->sh_info;
2315 amt *= sizeof (asection *);
2316 sections = bfd_malloc (amt);
2317 if (sections == NULL && amt != 0)
2320 isymend = isymbuf + symtab_hdr->sh_info;
2321 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
2325 if (isym->st_shndx == SHN_UNDEF)
2326 isec = bfd_und_section_ptr;
2327 else if (isym->st_shndx == SHN_ABS)
2328 isec = bfd_abs_section_ptr;
2329 else if (isym->st_shndx == SHN_COMMON)
2330 isec = bfd_com_section_ptr;
2332 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2337 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
2338 input_section, data, internal_relocs,
2342 if (sections != NULL)
2345 && symtab_hdr->contents != (unsigned char *) isymbuf)
2347 if (elf_section_data (input_section)->relocs != internal_relocs)
2348 free (internal_relocs);
2354 if (sections != NULL)
2357 && symtab_hdr->contents != (unsigned char *) isymbuf)
2359 if (internal_relocs != NULL
2360 && elf_section_data (input_section)->relocs != internal_relocs)
2361 free (internal_relocs);
2366 /* Determines the hash entry name for a particular reloc. It consists of
2367 the identifier of the symbol section and the added reloc addend and
2368 symbol offset relative to the section the symbol is attached to. */
2371 avr_stub_name (const asection *symbol_section,
2372 const bfd_vma symbol_offset,
2373 const Elf_Internal_Rela *rela)
2378 len = 8 + 1 + 8 + 1 + 1;
2379 stub_name = bfd_malloc (len);
2381 sprintf (stub_name, "%08x+%08x",
2382 symbol_section->id & 0xffffffff,
2383 (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
2389 /* Add a new stub entry to the stub hash. Not all fields of the new
2390 stub entry are initialised. */
2392 static struct elf32_avr_stub_hash_entry *
2393 avr_add_stub (const char *stub_name,
2394 struct elf32_avr_link_hash_table *htab)
2396 struct elf32_avr_stub_hash_entry *hsh;
2398 /* Enter this entry into the linker stub hash table. */
2399 hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
2403 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
2408 hsh->stub_offset = 0;
2412 /* We assume that there is already space allocated for the stub section
2413 contents and that before building the stubs the section size is
2414 initialized to 0. We assume that within the stub hash table entry,
2415 the absolute position of the jmp target has been written in the
2416 target_value field. We write here the offset of the generated jmp insn
2417 relative to the trampoline section start to the stub_offset entry in
2418 the stub hash table entry. */
2421 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2423 struct elf32_avr_stub_hash_entry *hsh;
2424 struct bfd_link_info *info;
2425 struct elf32_avr_link_hash_table *htab;
2432 bfd_vma jmp_insn = 0x0000940c;
2434 /* Massage our args to the form they really have. */
2435 hsh = avr_stub_hash_entry (bh);
2437 if (!hsh->is_actually_needed)
2440 info = (struct bfd_link_info *) in_arg;
2442 htab = avr_link_hash_table (info);
2446 target = hsh->target_value;
2448 /* Make a note of the offset within the stubs for this entry. */
2449 hsh->stub_offset = htab->stub_sec->size;
2450 loc = htab->stub_sec->contents + hsh->stub_offset;
2452 stub_bfd = htab->stub_sec->owner;
2455 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
2456 (unsigned int) target,
2457 (unsigned int) hsh->stub_offset);
2459 /* We now have to add the information on the jump target to the bare
2460 opcode bits already set in jmp_insn. */
2462 /* Check for the alignment of the address. */
2466 starget = target >> 1;
2467 jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
2468 bfd_put_16 (stub_bfd, jmp_insn, loc);
2469 bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
2471 htab->stub_sec->size += 4;
2473 /* Now add the entries in the address mapping table if there is still
2478 nr = htab->amt_entry_cnt + 1;
2479 if (nr <= htab->amt_max_entry_cnt)
2481 htab->amt_entry_cnt = nr;
2483 htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
2484 htab->amt_destination_addr[nr - 1] = target;
2492 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
2495 struct elf32_avr_stub_hash_entry *hsh;
2496 struct elf32_avr_link_hash_table *htab;
2499 hsh = avr_stub_hash_entry (bh);
2500 hsh->is_actually_needed = FALSE;
2506 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2508 struct elf32_avr_stub_hash_entry *hsh;
2509 struct elf32_avr_link_hash_table *htab;
2512 /* Massage our args to the form they really have. */
2513 hsh = avr_stub_hash_entry (bh);
2516 if (hsh->is_actually_needed)
2521 htab->stub_sec->size += size;
2526 elf32_avr_setup_params (struct bfd_link_info *info,
2528 asection *avr_stub_section,
2529 bfd_boolean no_stubs,
2530 bfd_boolean deb_stubs,
2531 bfd_boolean deb_relax,
2532 bfd_vma pc_wrap_around,
2533 bfd_boolean call_ret_replacement)
2535 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
2539 htab->stub_sec = avr_stub_section;
2540 htab->stub_bfd = avr_stub_bfd;
2541 htab->no_stubs = no_stubs;
2543 debug_relax = deb_relax;
2544 debug_stubs = deb_stubs;
2545 avr_pc_wrap_around = pc_wrap_around;
2546 avr_replace_call_ret_sequences = call_ret_replacement;
2550 /* Set up various things so that we can make a list of input sections
2551 for each output section included in the link. Returns -1 on error,
2552 0 when no stubs will be needed, and 1 on success. It also sets
2553 information on the stubs bfd and the stub section in the info
2557 elf32_avr_setup_section_lists (bfd *output_bfd,
2558 struct bfd_link_info *info)
2561 unsigned int bfd_count;
2562 int top_id, top_index;
2564 asection **input_list, **list;
2566 struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
2568 if (htab == NULL || htab->no_stubs)
2571 /* Count the number of input BFDs and find the top input section id. */
2572 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2574 input_bfd = input_bfd->link_next)
2577 for (section = input_bfd->sections;
2579 section = section->next)
2580 if (top_id < section->id)
2581 top_id = section->id;
2584 htab->bfd_count = bfd_count;
2586 /* We can't use output_bfd->section_count here to find the top output
2587 section index as some sections may have been removed, and
2588 strip_excluded_output_sections doesn't renumber the indices. */
2589 for (section = output_bfd->sections, top_index = 0;
2591 section = section->next)
2592 if (top_index < section->index)
2593 top_index = section->index;
2595 htab->top_index = top_index;
2596 amt = sizeof (asection *) * (top_index + 1);
2597 input_list = bfd_malloc (amt);
2598 htab->input_list = input_list;
2599 if (input_list == NULL)
2602 /* For sections we aren't interested in, mark their entries with a
2603 value we can check later. */
2604 list = input_list + top_index;
2606 *list = bfd_abs_section_ptr;
2607 while (list-- != input_list);
2609 for (section = output_bfd->sections;
2611 section = section->next)
2612 if ((section->flags & SEC_CODE) != 0)
2613 input_list[section->index] = NULL;
2619 /* Read in all local syms for all input bfds, and create hash entries
2620 for export stubs if we are building a multi-subspace shared lib.
2621 Returns -1 on error, 0 otherwise. */
2624 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
2626 unsigned int bfd_indx;
2627 Elf_Internal_Sym *local_syms, **all_local_syms;
2628 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
2634 /* We want to read in symbol extension records only once. To do this
2635 we need to read in the local symbols in parallel and save them for
2636 later use; so hold pointers to the local symbols in an array. */
2637 amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2638 all_local_syms = bfd_zmalloc (amt);
2639 htab->all_local_syms = all_local_syms;
2640 if (all_local_syms == NULL)
2643 /* Walk over all the input BFDs, swapping in local symbols.
2644 If we are creating a shared library, create hash entries for the
2648 input_bfd = input_bfd->link_next, bfd_indx++)
2650 Elf_Internal_Shdr *symtab_hdr;
2652 /* We'll need the symbol table in a second. */
2653 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2654 if (symtab_hdr->sh_info == 0)
2657 /* We need an array of the local symbols attached to the input bfd. */
2658 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2659 if (local_syms == NULL)
2661 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2662 symtab_hdr->sh_info, 0,
2664 /* Cache them for elf_link_input_bfd. */
2665 symtab_hdr->contents = (unsigned char *) local_syms;
2667 if (local_syms == NULL)
2670 all_local_syms[bfd_indx] = local_syms;
2676 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
2679 elf32_avr_size_stubs (bfd *output_bfd,
2680 struct bfd_link_info *info,
2681 bfd_boolean is_prealloc_run)
2683 struct elf32_avr_link_hash_table *htab;
2684 int stub_changed = 0;
2686 htab = avr_link_hash_table (info);
2690 /* At this point we initialize htab->vector_base
2691 To the start of the text output section. */
2692 htab->vector_base = htab->stub_sec->output_section->vma;
2694 if (get_local_syms (info->input_bfds, info))
2696 if (htab->all_local_syms)
2697 goto error_ret_free_local;
2701 if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
2703 struct elf32_avr_stub_hash_entry *test;
2705 test = avr_add_stub ("Hugo",htab);
2706 test->target_value = 0x123456;
2707 test->stub_offset = 13;
2709 test = avr_add_stub ("Hugo2",htab);
2710 test->target_value = 0x84210;
2711 test->stub_offset = 14;
2717 unsigned int bfd_indx;
2719 /* We will have to re-generate the stub hash table each time anything
2720 in memory has changed. */
2722 bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
2723 for (input_bfd = info->input_bfds, bfd_indx = 0;
2725 input_bfd = input_bfd->link_next, bfd_indx++)
2727 Elf_Internal_Shdr *symtab_hdr;
2729 Elf_Internal_Sym *local_syms;
2731 /* We'll need the symbol table in a second. */
2732 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2733 if (symtab_hdr->sh_info == 0)
2736 local_syms = htab->all_local_syms[bfd_indx];
2738 /* Walk over each section attached to the input bfd. */
2739 for (section = input_bfd->sections;
2741 section = section->next)
2743 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2745 /* If there aren't any relocs, then there's nothing more
2747 if ((section->flags & SEC_RELOC) == 0
2748 || section->reloc_count == 0)
2751 /* If this section is a link-once section that will be
2752 discarded, then don't create any stubs. */
2753 if (section->output_section == NULL
2754 || section->output_section->owner != output_bfd)
2757 /* Get the relocs. */
2759 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2761 if (internal_relocs == NULL)
2762 goto error_ret_free_local;
2764 /* Now examine each relocation. */
2765 irela = internal_relocs;
2766 irelaend = irela + section->reloc_count;
2767 for (; irela < irelaend; irela++)
2769 unsigned int r_type, r_indx;
2770 struct elf32_avr_stub_hash_entry *hsh;
2773 bfd_vma destination;
2774 struct elf_link_hash_entry *hh;
2777 r_type = ELF32_R_TYPE (irela->r_info);
2778 r_indx = ELF32_R_SYM (irela->r_info);
2780 /* Only look for 16 bit GS relocs. No other reloc will need a
2782 if (!((r_type == R_AVR_16_PM)
2783 || (r_type == R_AVR_LO8_LDI_GS)
2784 || (r_type == R_AVR_HI8_LDI_GS)))
2787 /* Now determine the call target, its name, value,
2793 if (r_indx < symtab_hdr->sh_info)
2795 /* It's a local symbol. */
2796 Elf_Internal_Sym *sym;
2797 Elf_Internal_Shdr *hdr;
2800 sym = local_syms + r_indx;
2801 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2802 sym_value = sym->st_value;
2803 shndx = sym->st_shndx;
2804 if (shndx < elf_numsections (input_bfd))
2806 hdr = elf_elfsections (input_bfd)[shndx];
2807 sym_sec = hdr->bfd_section;
2808 destination = (sym_value + irela->r_addend
2809 + sym_sec->output_offset
2810 + sym_sec->output_section->vma);
2815 /* It's an external symbol. */
2818 e_indx = r_indx - symtab_hdr->sh_info;
2819 hh = elf_sym_hashes (input_bfd)[e_indx];
2821 while (hh->root.type == bfd_link_hash_indirect
2822 || hh->root.type == bfd_link_hash_warning)
2823 hh = (struct elf_link_hash_entry *)
2824 (hh->root.u.i.link);
2826 if (hh->root.type == bfd_link_hash_defined
2827 || hh->root.type == bfd_link_hash_defweak)
2829 sym_sec = hh->root.u.def.section;
2830 sym_value = hh->root.u.def.value;
2831 if (sym_sec->output_section != NULL)
2832 destination = (sym_value + irela->r_addend
2833 + sym_sec->output_offset
2834 + sym_sec->output_section->vma);
2836 else if (hh->root.type == bfd_link_hash_undefweak)
2841 else if (hh->root.type == bfd_link_hash_undefined)
2843 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2844 && (ELF_ST_VISIBILITY (hh->other)
2850 bfd_set_error (bfd_error_bad_value);
2852 error_ret_free_internal:
2853 if (elf_section_data (section)->relocs == NULL)
2854 free (internal_relocs);
2855 goto error_ret_free_local;
2859 if (! avr_stub_is_required_for_16_bit_reloc
2860 (destination - htab->vector_base))
2862 if (!is_prealloc_run)
2863 /* We are having a reloc that does't need a stub. */
2866 /* We don't right now know if a stub will be needed.
2867 Let's rather be on the safe side. */
2870 /* Get the name of this stub. */
2871 stub_name = avr_stub_name (sym_sec, sym_value, irela);
2874 goto error_ret_free_internal;
2877 hsh = avr_stub_hash_lookup (&htab->bstab,
2882 /* The proper stub has already been created. Mark it
2883 to be used and write the possibly changed destination
2885 hsh->is_actually_needed = TRUE;
2886 hsh->target_value = destination;
2891 hsh = avr_add_stub (stub_name, htab);
2895 goto error_ret_free_internal;
2898 hsh->is_actually_needed = TRUE;
2899 hsh->target_value = destination;
2902 printf ("Adding stub with destination 0x%x to the"
2903 " hash table.\n", (unsigned int) destination);
2905 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
2907 stub_changed = TRUE;
2910 /* We're done with the internal relocs, free them. */
2911 if (elf_section_data (section)->relocs == NULL)
2912 free (internal_relocs);
2916 /* Re-Calculate the number of needed stubs. */
2917 htab->stub_sec->size = 0;
2918 bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
2923 stub_changed = FALSE;
2926 free (htab->all_local_syms);
2929 error_ret_free_local:
2930 free (htab->all_local_syms);
2935 /* Build all the stubs associated with the current output file. The
2936 stubs are kept in a hash table attached to the main linker hash
2937 table. We also set up the .plt entries for statically linked PIC
2938 functions here. This function is called via hppaelf_finish in the
2942 elf32_avr_build_stubs (struct bfd_link_info *info)
2945 struct bfd_hash_table *table;
2946 struct elf32_avr_link_hash_table *htab;
2947 bfd_size_type total_size = 0;
2949 htab = avr_link_hash_table (info);
2953 /* In case that there were several stub sections: */
2954 for (stub_sec = htab->stub_bfd->sections;
2956 stub_sec = stub_sec->next)
2960 /* Allocate memory to hold the linker stubs. */
2961 size = stub_sec->size;
2964 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2965 if (stub_sec->contents == NULL && size != 0)
2970 /* Allocate memory for the adress mapping table. */
2971 htab->amt_entry_cnt = 0;
2972 htab->amt_max_entry_cnt = total_size / 4;
2973 htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
2974 * htab->amt_max_entry_cnt);
2975 htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
2976 * htab->amt_max_entry_cnt );
2979 printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
2981 /* Build the stubs as directed by the stub hash table. */
2982 table = &htab->bstab;
2983 bfd_hash_traverse (table, avr_build_one_stub, info);
2986 printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
2991 #define ELF_ARCH bfd_arch_avr
2992 #define ELF_MACHINE_CODE EM_AVR
2993 #define ELF_MACHINE_ALT1 EM_AVR_OLD
2994 #define ELF_MAXPAGESIZE 1
2996 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
2997 #define TARGET_LITTLE_NAME "elf32-avr"
2999 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
3000 #define bfd_elf32_bfd_link_hash_table_free elf32_avr_link_hash_table_free
3002 #define elf_info_to_howto avr_info_to_howto_rela
3003 #define elf_info_to_howto_rel NULL
3004 #define elf_backend_relocate_section elf32_avr_relocate_section
3005 #define elf_backend_check_relocs elf32_avr_check_relocs
3006 #define elf_backend_can_gc_sections 1
3007 #define elf_backend_rela_normal 1
3008 #define elf_backend_final_write_processing \
3009 bfd_elf_avr_final_write_processing
3010 #define elf_backend_object_p elf32_avr_object_p
3012 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
3013 #define bfd_elf32_bfd_get_relocated_section_contents \
3014 elf32_avr_get_relocated_section_contents
3016 #include "elf32-target.h"