1 /* AVR-specific support for 32-bit ELF
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006
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 2 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 ((struct elf32_avr_link_hash_table *) ((p)->hash))
108 #define avr_stub_hash_entry(ent) \
109 ((struct elf32_avr_stub_hash_entry *)(ent))
111 #define avr_stub_hash_lookup(table, string, create, copy) \
112 ((struct elf32_avr_stub_hash_entry *) \
113 bfd_hash_lookup ((table), (string), (create), (copy)))
115 static reloc_howto_type elf_avr_howto_table
[] =
117 HOWTO (R_AVR_NONE
, /* type */
119 2, /* size (0 = byte, 1 = short, 2 = long) */
121 FALSE
, /* pc_relative */
123 complain_overflow_bitfield
, /* complain_on_overflow */
124 bfd_elf_generic_reloc
, /* special_function */
125 "R_AVR_NONE", /* name */
126 FALSE
, /* partial_inplace */
129 FALSE
), /* pcrel_offset */
131 HOWTO (R_AVR_32
, /* type */
133 2, /* size (0 = byte, 1 = short, 2 = long) */
135 FALSE
, /* pc_relative */
137 complain_overflow_bitfield
, /* complain_on_overflow */
138 bfd_elf_generic_reloc
, /* special_function */
139 "R_AVR_32", /* name */
140 FALSE
, /* partial_inplace */
141 0xffffffff, /* src_mask */
142 0xffffffff, /* dst_mask */
143 FALSE
), /* pcrel_offset */
145 /* A 7 bit PC relative relocation. */
146 HOWTO (R_AVR_7_PCREL
, /* type */
148 1, /* size (0 = byte, 1 = short, 2 = long) */
150 TRUE
, /* pc_relative */
152 complain_overflow_bitfield
, /* complain_on_overflow */
153 bfd_elf_generic_reloc
, /* special_function */
154 "R_AVR_7_PCREL", /* name */
155 FALSE
, /* partial_inplace */
156 0xffff, /* src_mask */
157 0xffff, /* dst_mask */
158 TRUE
), /* pcrel_offset */
160 /* A 13 bit PC relative relocation. */
161 HOWTO (R_AVR_13_PCREL
, /* type */
163 1, /* size (0 = byte, 1 = short, 2 = long) */
165 TRUE
, /* pc_relative */
167 complain_overflow_bitfield
, /* complain_on_overflow */
168 bfd_elf_generic_reloc
, /* special_function */
169 "R_AVR_13_PCREL", /* name */
170 FALSE
, /* partial_inplace */
171 0xfff, /* src_mask */
172 0xfff, /* dst_mask */
173 TRUE
), /* pcrel_offset */
175 /* A 16 bit absolute relocation. */
176 HOWTO (R_AVR_16
, /* type */
178 1, /* size (0 = byte, 1 = short, 2 = long) */
180 FALSE
, /* pc_relative */
182 complain_overflow_dont
, /* complain_on_overflow */
183 bfd_elf_generic_reloc
, /* special_function */
184 "R_AVR_16", /* name */
185 FALSE
, /* partial_inplace */
186 0xffff, /* src_mask */
187 0xffff, /* dst_mask */
188 FALSE
), /* pcrel_offset */
190 /* A 16 bit absolute relocation for command address
191 Will be changed when linker stubs are needed. */
192 HOWTO (R_AVR_16_PM
, /* type */
194 1, /* size (0 = byte, 1 = short, 2 = long) */
196 FALSE
, /* pc_relative */
198 complain_overflow_bitfield
, /* complain_on_overflow */
199 bfd_elf_generic_reloc
, /* special_function */
200 "R_AVR_16_PM", /* name */
201 FALSE
, /* partial_inplace */
202 0xffff, /* src_mask */
203 0xffff, /* dst_mask */
204 FALSE
), /* pcrel_offset */
205 /* A low 8 bit absolute relocation of 16 bit address.
207 HOWTO (R_AVR_LO8_LDI
, /* type */
209 1, /* size (0 = byte, 1 = short, 2 = long) */
211 FALSE
, /* pc_relative */
213 complain_overflow_dont
, /* complain_on_overflow */
214 bfd_elf_generic_reloc
, /* special_function */
215 "R_AVR_LO8_LDI", /* name */
216 FALSE
, /* partial_inplace */
217 0xffff, /* src_mask */
218 0xffff, /* dst_mask */
219 FALSE
), /* pcrel_offset */
220 /* A high 8 bit absolute relocation of 16 bit address.
222 HOWTO (R_AVR_HI8_LDI
, /* type */
224 1, /* size (0 = byte, 1 = short, 2 = long) */
226 FALSE
, /* pc_relative */
228 complain_overflow_dont
, /* complain_on_overflow */
229 bfd_elf_generic_reloc
, /* special_function */
230 "R_AVR_HI8_LDI", /* name */
231 FALSE
, /* partial_inplace */
232 0xffff, /* src_mask */
233 0xffff, /* dst_mask */
234 FALSE
), /* pcrel_offset */
235 /* A high 6 bit absolute relocation of 22 bit address.
236 For LDI command. As well second most significant 8 bit value of
237 a 32 bit link-time constant. */
238 HOWTO (R_AVR_HH8_LDI
, /* type */
240 1, /* size (0 = byte, 1 = short, 2 = long) */
242 FALSE
, /* pc_relative */
244 complain_overflow_dont
, /* complain_on_overflow */
245 bfd_elf_generic_reloc
, /* special_function */
246 "R_AVR_HH8_LDI", /* name */
247 FALSE
, /* partial_inplace */
248 0xffff, /* src_mask */
249 0xffff, /* dst_mask */
250 FALSE
), /* pcrel_offset */
251 /* A negative low 8 bit absolute relocation of 16 bit address.
253 HOWTO (R_AVR_LO8_LDI_NEG
, /* type */
255 1, /* size (0 = byte, 1 = short, 2 = long) */
257 FALSE
, /* pc_relative */
259 complain_overflow_dont
, /* complain_on_overflow */
260 bfd_elf_generic_reloc
, /* special_function */
261 "R_AVR_LO8_LDI_NEG", /* name */
262 FALSE
, /* partial_inplace */
263 0xffff, /* src_mask */
264 0xffff, /* dst_mask */
265 FALSE
), /* pcrel_offset */
266 /* A negative high 8 bit absolute relocation of 16 bit address.
268 HOWTO (R_AVR_HI8_LDI_NEG
, /* type */
270 1, /* size (0 = byte, 1 = short, 2 = long) */
272 FALSE
, /* pc_relative */
274 complain_overflow_dont
, /* complain_on_overflow */
275 bfd_elf_generic_reloc
, /* special_function */
276 "R_AVR_HI8_LDI_NEG", /* name */
277 FALSE
, /* partial_inplace */
278 0xffff, /* src_mask */
279 0xffff, /* dst_mask */
280 FALSE
), /* pcrel_offset */
281 /* A negative high 6 bit absolute relocation of 22 bit address.
283 HOWTO (R_AVR_HH8_LDI_NEG
, /* type */
285 1, /* size (0 = byte, 1 = short, 2 = long) */
287 FALSE
, /* pc_relative */
289 complain_overflow_dont
, /* complain_on_overflow */
290 bfd_elf_generic_reloc
, /* special_function */
291 "R_AVR_HH8_LDI_NEG", /* name */
292 FALSE
, /* partial_inplace */
293 0xffff, /* src_mask */
294 0xffff, /* dst_mask */
295 FALSE
), /* pcrel_offset */
296 /* A low 8 bit absolute relocation of 24 bit program memory address.
297 For LDI command. Will not be changed when linker stubs are needed. */
298 HOWTO (R_AVR_LO8_LDI_PM
, /* type */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
302 FALSE
, /* pc_relative */
304 complain_overflow_dont
, /* complain_on_overflow */
305 bfd_elf_generic_reloc
, /* special_function */
306 "R_AVR_LO8_LDI_PM", /* name */
307 FALSE
, /* partial_inplace */
308 0xffff, /* src_mask */
309 0xffff, /* dst_mask */
310 FALSE
), /* pcrel_offset */
311 /* A low 8 bit absolute relocation of 24 bit program memory address.
312 For LDI command. Will not be changed when linker stubs are needed. */
313 HOWTO (R_AVR_HI8_LDI_PM
, /* type */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
317 FALSE
, /* pc_relative */
319 complain_overflow_dont
, /* complain_on_overflow */
320 bfd_elf_generic_reloc
, /* special_function */
321 "R_AVR_HI8_LDI_PM", /* name */
322 FALSE
, /* partial_inplace */
323 0xffff, /* src_mask */
324 0xffff, /* dst_mask */
325 FALSE
), /* pcrel_offset */
326 /* A low 8 bit absolute relocation of 24 bit program memory address.
327 For LDI command. Will not be changed when linker stubs are needed. */
328 HOWTO (R_AVR_HH8_LDI_PM
, /* type */
330 1, /* size (0 = byte, 1 = short, 2 = long) */
332 FALSE
, /* pc_relative */
334 complain_overflow_dont
, /* complain_on_overflow */
335 bfd_elf_generic_reloc
, /* special_function */
336 "R_AVR_HH8_LDI_PM", /* name */
337 FALSE
, /* partial_inplace */
338 0xffff, /* src_mask */
339 0xffff, /* dst_mask */
340 FALSE
), /* pcrel_offset */
341 /* A low 8 bit absolute relocation of 24 bit program memory address.
342 For LDI command. Will not be changed when linker stubs are needed. */
343 HOWTO (R_AVR_LO8_LDI_PM_NEG
, /* type */
345 1, /* size (0 = byte, 1 = short, 2 = long) */
347 FALSE
, /* pc_relative */
349 complain_overflow_dont
, /* complain_on_overflow */
350 bfd_elf_generic_reloc
, /* special_function */
351 "R_AVR_LO8_LDI_PM_NEG", /* name */
352 FALSE
, /* partial_inplace */
353 0xffff, /* src_mask */
354 0xffff, /* dst_mask */
355 FALSE
), /* pcrel_offset */
356 /* A low 8 bit absolute relocation of 24 bit program memory address.
357 For LDI command. Will not be changed when linker stubs are needed. */
358 HOWTO (R_AVR_HI8_LDI_PM_NEG
, /* type */
360 1, /* size (0 = byte, 1 = short, 2 = long) */
362 FALSE
, /* pc_relative */
364 complain_overflow_dont
, /* complain_on_overflow */
365 bfd_elf_generic_reloc
, /* special_function */
366 "R_AVR_HI8_LDI_PM_NEG", /* name */
367 FALSE
, /* partial_inplace */
368 0xffff, /* src_mask */
369 0xffff, /* dst_mask */
370 FALSE
), /* pcrel_offset */
371 /* A low 8 bit absolute relocation of 24 bit program memory address.
372 For LDI command. Will not be changed when linker stubs are needed. */
373 HOWTO (R_AVR_HH8_LDI_PM_NEG
, /* type */
375 1, /* size (0 = byte, 1 = short, 2 = long) */
377 FALSE
, /* pc_relative */
379 complain_overflow_dont
, /* complain_on_overflow */
380 bfd_elf_generic_reloc
, /* special_function */
381 "R_AVR_HH8_LDI_PM_NEG", /* name */
382 FALSE
, /* partial_inplace */
383 0xffff, /* src_mask */
384 0xffff, /* dst_mask */
385 FALSE
), /* pcrel_offset */
386 /* Relocation for CALL command in ATmega. */
387 HOWTO (R_AVR_CALL
, /* type */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
391 FALSE
, /* pc_relative */
393 complain_overflow_dont
,/* complain_on_overflow */
394 bfd_elf_generic_reloc
, /* special_function */
395 "R_AVR_CALL", /* name */
396 FALSE
, /* partial_inplace */
397 0xffffffff, /* src_mask */
398 0xffffffff, /* dst_mask */
399 FALSE
), /* pcrel_offset */
400 /* A 16 bit absolute relocation of 16 bit address.
402 HOWTO (R_AVR_LDI
, /* type */
404 1, /* size (0 = byte, 1 = short, 2 = long) */
406 FALSE
, /* pc_relative */
408 complain_overflow_dont
,/* complain_on_overflow */
409 bfd_elf_generic_reloc
, /* special_function */
410 "R_AVR_LDI", /* name */
411 FALSE
, /* partial_inplace */
412 0xffff, /* src_mask */
413 0xffff, /* dst_mask */
414 FALSE
), /* pcrel_offset */
415 /* A 6 bit absolute relocation of 6 bit offset.
416 For ldd/sdd command. */
417 HOWTO (R_AVR_6
, /* type */
419 0, /* size (0 = byte, 1 = short, 2 = long) */
421 FALSE
, /* pc_relative */
423 complain_overflow_dont
,/* complain_on_overflow */
424 bfd_elf_generic_reloc
, /* special_function */
425 "R_AVR_6", /* name */
426 FALSE
, /* partial_inplace */
427 0xffff, /* src_mask */
428 0xffff, /* dst_mask */
429 FALSE
), /* pcrel_offset */
430 /* A 6 bit absolute relocation of 6 bit offset.
431 For sbiw/adiw command. */
432 HOWTO (R_AVR_6_ADIW
, /* type */
434 0, /* size (0 = byte, 1 = short, 2 = long) */
436 FALSE
, /* pc_relative */
438 complain_overflow_dont
,/* complain_on_overflow */
439 bfd_elf_generic_reloc
, /* special_function */
440 "R_AVR_6_ADIW", /* name */
441 FALSE
, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
444 FALSE
), /* pcrel_offset */
445 /* Most significant 8 bit value of a 32 bit link-time constant. */
446 HOWTO (R_AVR_MS8_LDI
, /* type */
448 1, /* size (0 = byte, 1 = short, 2 = long) */
450 FALSE
, /* pc_relative */
452 complain_overflow_dont
, /* complain_on_overflow */
453 bfd_elf_generic_reloc
, /* special_function */
454 "R_AVR_MS8_LDI", /* name */
455 FALSE
, /* partial_inplace */
456 0xffff, /* src_mask */
457 0xffff, /* dst_mask */
458 FALSE
), /* pcrel_offset */
459 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
460 HOWTO (R_AVR_MS8_LDI_NEG
, /* type */
462 1, /* size (0 = byte, 1 = short, 2 = long) */
464 FALSE
, /* pc_relative */
466 complain_overflow_dont
, /* complain_on_overflow */
467 bfd_elf_generic_reloc
, /* special_function */
468 "R_AVR_MS8_LDI_NEG", /* name */
469 FALSE
, /* partial_inplace */
470 0xffff, /* src_mask */
471 0xffff, /* dst_mask */
472 FALSE
), /* pcrel_offset */
473 /* A low 8 bit absolute relocation of 24 bit program memory address.
474 For LDI command. Will be changed when linker stubs are needed. */
475 HOWTO (R_AVR_LO8_LDI_GS
, /* type */
477 1, /* size (0 = byte, 1 = short, 2 = long) */
479 FALSE
, /* pc_relative */
481 complain_overflow_dont
, /* complain_on_overflow */
482 bfd_elf_generic_reloc
, /* special_function */
483 "R_AVR_LO8_LDI_GS", /* name */
484 FALSE
, /* partial_inplace */
485 0xffff, /* src_mask */
486 0xffff, /* dst_mask */
487 FALSE
), /* pcrel_offset */
488 /* A low 8 bit absolute relocation of 24 bit program memory address.
489 For LDI command. Will be changed when linker stubs are needed. */
490 HOWTO (R_AVR_HI8_LDI_GS
, /* type */
492 1, /* size (0 = byte, 1 = short, 2 = long) */
494 FALSE
, /* pc_relative */
496 complain_overflow_dont
, /* complain_on_overflow */
497 bfd_elf_generic_reloc
, /* special_function */
498 "R_AVR_HI8_LDI_GS", /* name */
499 FALSE
, /* partial_inplace */
500 0xffff, /* src_mask */
501 0xffff, /* dst_mask */
502 FALSE
) /* pcrel_offset */
505 /* Map BFD reloc types to AVR ELF reloc types. */
509 bfd_reloc_code_real_type bfd_reloc_val
;
510 unsigned int elf_reloc_val
;
513 static const struct avr_reloc_map avr_reloc_map
[] =
515 { BFD_RELOC_NONE
, R_AVR_NONE
},
516 { BFD_RELOC_32
, R_AVR_32
},
517 { BFD_RELOC_AVR_7_PCREL
, R_AVR_7_PCREL
},
518 { BFD_RELOC_AVR_13_PCREL
, R_AVR_13_PCREL
},
519 { BFD_RELOC_16
, R_AVR_16
},
520 { BFD_RELOC_AVR_16_PM
, R_AVR_16_PM
},
521 { BFD_RELOC_AVR_LO8_LDI
, R_AVR_LO8_LDI
},
522 { BFD_RELOC_AVR_HI8_LDI
, R_AVR_HI8_LDI
},
523 { BFD_RELOC_AVR_HH8_LDI
, R_AVR_HH8_LDI
},
524 { BFD_RELOC_AVR_MS8_LDI
, R_AVR_MS8_LDI
},
525 { BFD_RELOC_AVR_LO8_LDI_NEG
, R_AVR_LO8_LDI_NEG
},
526 { BFD_RELOC_AVR_HI8_LDI_NEG
, R_AVR_HI8_LDI_NEG
},
527 { BFD_RELOC_AVR_HH8_LDI_NEG
, R_AVR_HH8_LDI_NEG
},
528 { BFD_RELOC_AVR_MS8_LDI_NEG
, R_AVR_MS8_LDI_NEG
},
529 { BFD_RELOC_AVR_LO8_LDI_PM
, R_AVR_LO8_LDI_PM
},
530 { BFD_RELOC_AVR_LO8_LDI_GS
, R_AVR_LO8_LDI_GS
},
531 { BFD_RELOC_AVR_HI8_LDI_PM
, R_AVR_HI8_LDI_PM
},
532 { BFD_RELOC_AVR_HI8_LDI_GS
, R_AVR_HI8_LDI_GS
},
533 { BFD_RELOC_AVR_HH8_LDI_PM
, R_AVR_HH8_LDI_PM
},
534 { BFD_RELOC_AVR_LO8_LDI_PM_NEG
, R_AVR_LO8_LDI_PM_NEG
},
535 { BFD_RELOC_AVR_HI8_LDI_PM_NEG
, R_AVR_HI8_LDI_PM_NEG
},
536 { BFD_RELOC_AVR_HH8_LDI_PM_NEG
, R_AVR_HH8_LDI_PM_NEG
},
537 { BFD_RELOC_AVR_CALL
, R_AVR_CALL
},
538 { BFD_RELOC_AVR_LDI
, R_AVR_LDI
},
539 { BFD_RELOC_AVR_6
, R_AVR_6
},
540 { BFD_RELOC_AVR_6_ADIW
, R_AVR_6_ADIW
}
543 /* Meant to be filled one day with the wrap around address for the
544 specific device. I.e. should get the value 0x4000 for 16k devices,
545 0x8000 for 32k devices and so on.
547 We initialize it here with a value of 0x1000000 resulting in
548 that we will never suggest a wrap-around jump during relaxation.
549 The logic of the source code later on assumes that in
550 avr_pc_wrap_around one single bit is set. */
551 static bfd_vma avr_pc_wrap_around
= 0x10000000;
553 /* If this variable holds a value different from zero, the linker relaxation
554 machine will try to optimize call/ret sequences by a single jump
555 instruction. This option could be switched off by a linker switch. */
556 static int avr_replace_call_ret_sequences
= 1;
558 /* Initialize an entry in the stub hash table. */
560 static struct bfd_hash_entry
*
561 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
562 struct bfd_hash_table
*table
,
565 /* Allocate the structure if it has not already been allocated by a
569 entry
= bfd_hash_allocate (table
,
570 sizeof (struct elf32_avr_stub_hash_entry
));
575 /* Call the allocation method of the superclass. */
576 entry
= bfd_hash_newfunc (entry
, table
, string
);
579 struct elf32_avr_stub_hash_entry
*hsh
;
581 /* Initialize the local fields. */
582 hsh
= avr_stub_hash_entry (entry
);
583 hsh
->stub_offset
= 0;
584 hsh
->target_value
= 0;
590 /* Create the derived linker hash table. The AVR ELF port uses the derived
591 hash table to keep information specific to the AVR ELF linker (without
592 using static variables). */
594 static struct bfd_link_hash_table
*
595 elf32_avr_link_hash_table_create (bfd
*abfd
)
597 struct elf32_avr_link_hash_table
*htab
;
598 bfd_size_type amt
= sizeof (*htab
);
600 htab
= bfd_malloc (amt
);
604 if (!_bfd_elf_link_hash_table_init (&htab
->etab
, abfd
,
605 _bfd_elf_link_hash_newfunc
,
606 sizeof (struct elf_link_hash_entry
)))
612 /* Init the stub hash table too. */
613 if (!bfd_hash_table_init (&htab
->bstab
, stub_hash_newfunc
,
614 sizeof (struct elf32_avr_stub_hash_entry
)))
617 htab
->stub_bfd
= NULL
;
618 htab
->stub_sec
= NULL
;
620 /* Initialize the address mapping table. */
621 htab
->amt_stub_offsets
= NULL
;
622 htab
->amt_destination_addr
= NULL
;
623 htab
->amt_entry_cnt
= 0;
624 htab
->amt_max_entry_cnt
= 0;
626 return &htab
->etab
.root
;
629 /* Free the derived linker hash table. */
632 elf32_avr_link_hash_table_free (struct bfd_link_hash_table
*btab
)
634 struct elf32_avr_link_hash_table
*htab
635 = (struct elf32_avr_link_hash_table
*) btab
;
637 /* Free the address mapping table. */
638 if (htab
->amt_stub_offsets
!= NULL
)
639 free (htab
->amt_stub_offsets
);
640 if (htab
->amt_destination_addr
!= NULL
)
641 free (htab
->amt_destination_addr
);
643 bfd_hash_table_free (&htab
->bstab
);
644 _bfd_generic_link_hash_table_free (btab
);
647 /* Calculates the effective distance of a pc relative jump/call. */
650 avr_relative_distance_considering_wrap_around (unsigned int distance
)
652 unsigned int wrap_around_mask
= avr_pc_wrap_around
- 1;
653 int dist_with_wrap_around
= distance
& wrap_around_mask
;
655 if (dist_with_wrap_around
> ((int) (avr_pc_wrap_around
>> 1)))
656 dist_with_wrap_around
-= avr_pc_wrap_around
;
658 return dist_with_wrap_around
;
662 static reloc_howto_type
*
663 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
664 bfd_reloc_code_real_type code
)
669 i
< sizeof (avr_reloc_map
) / sizeof (struct avr_reloc_map
);
671 if (avr_reloc_map
[i
].bfd_reloc_val
== code
)
672 return &elf_avr_howto_table
[avr_reloc_map
[i
].elf_reloc_val
];
677 /* Set the howto pointer for an AVR ELF reloc. */
680 avr_info_to_howto_rela (bfd
*abfd ATTRIBUTE_UNUSED
,
682 Elf_Internal_Rela
*dst
)
686 r_type
= ELF32_R_TYPE (dst
->r_info
);
687 BFD_ASSERT (r_type
< (unsigned int) R_AVR_max
);
688 cache_ptr
->howto
= &elf_avr_howto_table
[r_type
];
692 elf32_avr_gc_mark_hook (asection
*sec
,
693 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
694 Elf_Internal_Rela
*rel
,
695 struct elf_link_hash_entry
*h
,
696 Elf_Internal_Sym
*sym
)
700 switch (ELF32_R_TYPE (rel
->r_info
))
703 switch (h
->root
.type
)
705 case bfd_link_hash_defined
:
706 case bfd_link_hash_defweak
:
707 return h
->root
.u
.def
.section
;
709 case bfd_link_hash_common
:
710 return h
->root
.u
.c
.p
->section
;
718 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
724 elf32_avr_gc_sweep_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
725 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
726 asection
*sec ATTRIBUTE_UNUSED
,
727 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
)
729 /* We don't use got and plt entries for avr. */
733 /* Look through the relocs for a section during the first phase.
734 Since we don't do .gots or .plts, we just need to consider the
735 virtual table relocs for gc. */
738 elf32_avr_check_relocs (bfd
*abfd
,
739 struct bfd_link_info
*info
,
741 const Elf_Internal_Rela
*relocs
)
743 Elf_Internal_Shdr
*symtab_hdr
;
744 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
745 const Elf_Internal_Rela
*rel
;
746 const Elf_Internal_Rela
*rel_end
;
748 if (info
->relocatable
)
751 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
752 sym_hashes
= elf_sym_hashes (abfd
);
753 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
754 if (!elf_bad_symtab (abfd
))
755 sym_hashes_end
-= symtab_hdr
->sh_info
;
757 rel_end
= relocs
+ sec
->reloc_count
;
758 for (rel
= relocs
; rel
< rel_end
; rel
++)
760 struct elf_link_hash_entry
*h
;
761 unsigned long r_symndx
;
763 r_symndx
= ELF32_R_SYM (rel
->r_info
);
764 if (r_symndx
< symtab_hdr
->sh_info
)
768 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
769 while (h
->root
.type
== bfd_link_hash_indirect
770 || h
->root
.type
== bfd_link_hash_warning
)
771 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
779 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation
)
781 return (relocation
>= 0x020000);
784 /* Returns the address of the corresponding stub if there is one.
785 Returns otherwise an address above 0x020000. This function
786 could also be used, if there is no knowledge on the section where
787 the destination is found. */
790 avr_get_stub_addr (bfd_vma srel
,
791 struct elf32_avr_link_hash_table
*htab
)
794 bfd_vma stub_sec_addr
=
795 (htab
->stub_sec
->output_section
->vma
+
796 htab
->stub_sec
->output_offset
);
798 for (index
= 0; index
< htab
->amt_max_entry_cnt
; index
++)
799 if (htab
->amt_destination_addr
[index
] == srel
)
800 return htab
->amt_stub_offsets
[index
] + stub_sec_addr
;
802 /* Return an address that could not be reached by 16 bit relocs. */
806 /* Perform a single relocation. By default we use the standard BFD
807 routines, but a few relocs, we have to do them ourselves. */
809 static bfd_reloc_status_type
810 avr_final_link_relocate (reloc_howto_type
* howto
,
812 asection
* input_section
,
814 Elf_Internal_Rela
* rel
,
816 struct elf32_avr_link_hash_table
* htab
)
818 bfd_reloc_status_type r
= bfd_reloc_ok
;
821 bfd_signed_vma reloc_addr
;
822 bfd_boolean use_stubs
= FALSE
;
823 /* Usually is 0, unless we are generating code for a bootloader. */
824 bfd_signed_vma base_addr
= htab
->vector_base
;
826 /* Absolute addr of the reloc in the final excecutable. */
827 reloc_addr
= rel
->r_offset
+ input_section
->output_section
->vma
828 + input_section
->output_offset
;
833 contents
+= rel
->r_offset
;
834 srel
= (bfd_signed_vma
) relocation
;
835 srel
+= rel
->r_addend
;
836 srel
-= rel
->r_offset
;
837 srel
-= 2; /* Branch instructions add 2 to the PC... */
838 srel
-= (input_section
->output_section
->vma
+
839 input_section
->output_offset
);
842 return bfd_reloc_outofrange
;
843 if (srel
> ((1 << 7) - 1) || (srel
< - (1 << 7)))
844 return bfd_reloc_overflow
;
845 x
= bfd_get_16 (input_bfd
, contents
);
846 x
= (x
& 0xfc07) | (((srel
>> 1) << 3) & 0x3f8);
847 bfd_put_16 (input_bfd
, x
, contents
);
851 contents
+= rel
->r_offset
;
852 srel
= (bfd_signed_vma
) relocation
;
853 srel
+= rel
->r_addend
;
854 srel
-= rel
->r_offset
;
855 srel
-= 2; /* Branch instructions add 2 to the PC... */
856 srel
-= (input_section
->output_section
->vma
+
857 input_section
->output_offset
);
860 return bfd_reloc_outofrange
;
862 srel
= avr_relative_distance_considering_wrap_around (srel
);
864 /* AVR addresses commands as words. */
867 /* Check for overflow. */
868 if (srel
< -2048 || srel
> 2047)
870 /* Relative distance is too large. */
872 /* Always apply WRAPAROUND for avr2 and avr4. */
873 switch (bfd_get_mach (input_bfd
))
880 return bfd_reloc_overflow
;
884 x
= bfd_get_16 (input_bfd
, contents
);
885 x
= (x
& 0xf000) | (srel
& 0xfff);
886 bfd_put_16 (input_bfd
, x
, contents
);
890 contents
+= rel
->r_offset
;
891 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
892 x
= bfd_get_16 (input_bfd
, contents
);
893 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
894 bfd_put_16 (input_bfd
, x
, contents
);
898 contents
+= rel
->r_offset
;
899 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
900 if (((srel
> 0) && (srel
& 0xffff) > 255)
901 || ((srel
< 0) && ((-srel
) & 0xffff) > 128))
902 /* Remove offset for data/eeprom section. */
903 return bfd_reloc_overflow
;
905 x
= bfd_get_16 (input_bfd
, contents
);
906 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
907 bfd_put_16 (input_bfd
, x
, contents
);
911 contents
+= rel
->r_offset
;
912 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
913 if (((srel
& 0xffff) > 63) || (srel
< 0))
914 /* Remove offset for data/eeprom section. */
915 return bfd_reloc_overflow
;
916 x
= bfd_get_16 (input_bfd
, contents
);
917 x
= (x
& 0xd3f8) | ((srel
& 7) | ((srel
& (3 << 3)) << 7)
918 | ((srel
& (1 << 5)) << 8));
919 bfd_put_16 (input_bfd
, x
, contents
);
923 contents
+= rel
->r_offset
;
924 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
925 if (((srel
& 0xffff) > 63) || (srel
< 0))
926 /* Remove offset for data/eeprom section. */
927 return bfd_reloc_overflow
;
928 x
= bfd_get_16 (input_bfd
, contents
);
929 x
= (x
& 0xff30) | (srel
& 0xf) | ((srel
& 0x30) << 2);
930 bfd_put_16 (input_bfd
, x
, contents
);
934 contents
+= rel
->r_offset
;
935 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
936 srel
= (srel
>> 8) & 0xff;
937 x
= bfd_get_16 (input_bfd
, contents
);
938 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
939 bfd_put_16 (input_bfd
, x
, contents
);
943 contents
+= rel
->r_offset
;
944 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
945 srel
= (srel
>> 16) & 0xff;
946 x
= bfd_get_16 (input_bfd
, contents
);
947 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
948 bfd_put_16 (input_bfd
, x
, contents
);
952 contents
+= rel
->r_offset
;
953 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
954 srel
= (srel
>> 24) & 0xff;
955 x
= bfd_get_16 (input_bfd
, contents
);
956 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
957 bfd_put_16 (input_bfd
, x
, contents
);
960 case R_AVR_LO8_LDI_NEG
:
961 contents
+= rel
->r_offset
;
962 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
964 x
= bfd_get_16 (input_bfd
, contents
);
965 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
966 bfd_put_16 (input_bfd
, x
, contents
);
969 case R_AVR_HI8_LDI_NEG
:
970 contents
+= rel
->r_offset
;
971 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
973 srel
= (srel
>> 8) & 0xff;
974 x
= bfd_get_16 (input_bfd
, contents
);
975 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
976 bfd_put_16 (input_bfd
, x
, contents
);
979 case R_AVR_HH8_LDI_NEG
:
980 contents
+= rel
->r_offset
;
981 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
983 srel
= (srel
>> 16) & 0xff;
984 x
= bfd_get_16 (input_bfd
, contents
);
985 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
986 bfd_put_16 (input_bfd
, x
, contents
);
989 case R_AVR_MS8_LDI_NEG
:
990 contents
+= rel
->r_offset
;
991 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
993 srel
= (srel
>> 24) & 0xff;
994 x
= bfd_get_16 (input_bfd
, contents
);
995 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
996 bfd_put_16 (input_bfd
, x
, contents
);
999 case R_AVR_LO8_LDI_GS
:
1000 use_stubs
= (!htab
->no_stubs
);
1002 case R_AVR_LO8_LDI_PM
:
1003 contents
+= rel
->r_offset
;
1004 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1007 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1009 bfd_vma old_srel
= srel
;
1011 /* We need to use the address of the stub instead. */
1012 srel
= avr_get_stub_addr (srel
, htab
);
1014 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1015 "reloc at address 0x%x.\n",
1016 (unsigned int) srel
,
1017 (unsigned int) old_srel
,
1018 (unsigned int) reloc_addr
);
1020 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1021 return bfd_reloc_outofrange
;
1025 return bfd_reloc_outofrange
;
1027 x
= bfd_get_16 (input_bfd
, contents
);
1028 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1029 bfd_put_16 (input_bfd
, x
, contents
);
1032 case R_AVR_HI8_LDI_GS
:
1033 use_stubs
= (!htab
->no_stubs
);
1035 case R_AVR_HI8_LDI_PM
:
1036 contents
+= rel
->r_offset
;
1037 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1040 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1042 bfd_vma old_srel
= srel
;
1044 /* We need to use the address of the stub instead. */
1045 srel
= avr_get_stub_addr (srel
, htab
);
1047 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1048 "reloc at address 0x%x.\n",
1049 (unsigned int) srel
,
1050 (unsigned int) old_srel
,
1051 (unsigned int) reloc_addr
);
1053 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1054 return bfd_reloc_outofrange
;
1058 return bfd_reloc_outofrange
;
1060 srel
= (srel
>> 8) & 0xff;
1061 x
= bfd_get_16 (input_bfd
, contents
);
1062 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1063 bfd_put_16 (input_bfd
, x
, contents
);
1066 case R_AVR_HH8_LDI_PM
:
1067 contents
+= rel
->r_offset
;
1068 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1070 return bfd_reloc_outofrange
;
1072 srel
= (srel
>> 16) & 0xff;
1073 x
= bfd_get_16 (input_bfd
, contents
);
1074 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1075 bfd_put_16 (input_bfd
, x
, contents
);
1078 case R_AVR_LO8_LDI_PM_NEG
:
1079 contents
+= rel
->r_offset
;
1080 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1083 return bfd_reloc_outofrange
;
1085 x
= bfd_get_16 (input_bfd
, contents
);
1086 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1087 bfd_put_16 (input_bfd
, x
, contents
);
1090 case R_AVR_HI8_LDI_PM_NEG
:
1091 contents
+= rel
->r_offset
;
1092 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1095 return bfd_reloc_outofrange
;
1097 srel
= (srel
>> 8) & 0xff;
1098 x
= bfd_get_16 (input_bfd
, contents
);
1099 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1100 bfd_put_16 (input_bfd
, x
, contents
);
1103 case R_AVR_HH8_LDI_PM_NEG
:
1104 contents
+= rel
->r_offset
;
1105 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1108 return bfd_reloc_outofrange
;
1110 srel
= (srel
>> 16) & 0xff;
1111 x
= bfd_get_16 (input_bfd
, contents
);
1112 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1113 bfd_put_16 (input_bfd
, x
, contents
);
1117 contents
+= rel
->r_offset
;
1118 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1120 return bfd_reloc_outofrange
;
1122 x
= bfd_get_16 (input_bfd
, contents
);
1123 x
|= ((srel
& 0x10000) | ((srel
<< 3) & 0x1f00000)) >> 16;
1124 bfd_put_16 (input_bfd
, x
, contents
);
1125 bfd_put_16 (input_bfd
, (bfd_vma
) srel
& 0xffff, contents
+2);
1129 use_stubs
= (!htab
->no_stubs
);
1130 contents
+= rel
->r_offset
;
1131 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1134 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1136 bfd_vma old_srel
= srel
;
1138 /* We need to use the address of the stub instead. */
1139 srel
= avr_get_stub_addr (srel
,htab
);
1141 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1142 "reloc at address 0x%x.\n",
1143 (unsigned int) srel
,
1144 (unsigned int) old_srel
,
1145 (unsigned int) reloc_addr
);
1147 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1148 return bfd_reloc_outofrange
;
1152 return bfd_reloc_outofrange
;
1154 bfd_put_16 (input_bfd
, (bfd_vma
) srel
&0x00ffff, contents
);
1158 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1159 contents
, rel
->r_offset
,
1160 relocation
, rel
->r_addend
);
1166 /* Relocate an AVR ELF section. */
1169 elf32_avr_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1170 struct bfd_link_info
*info
,
1172 asection
*input_section
,
1174 Elf_Internal_Rela
*relocs
,
1175 Elf_Internal_Sym
*local_syms
,
1176 asection
**local_sections
)
1178 Elf_Internal_Shdr
* symtab_hdr
;
1179 struct elf_link_hash_entry
** sym_hashes
;
1180 Elf_Internal_Rela
* rel
;
1181 Elf_Internal_Rela
* relend
;
1182 struct elf32_avr_link_hash_table
* htab
= avr_link_hash_table (info
);
1184 if (info
->relocatable
)
1187 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
1188 sym_hashes
= elf_sym_hashes (input_bfd
);
1189 relend
= relocs
+ input_section
->reloc_count
;
1191 for (rel
= relocs
; rel
< relend
; rel
++)
1193 reloc_howto_type
* howto
;
1194 unsigned long r_symndx
;
1195 Elf_Internal_Sym
* sym
;
1197 struct elf_link_hash_entry
* h
;
1199 bfd_reloc_status_type r
;
1203 /* This is a final link. */
1204 r_type
= ELF32_R_TYPE (rel
->r_info
);
1205 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1206 howto
= elf_avr_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
1211 if (r_symndx
< symtab_hdr
->sh_info
)
1213 sym
= local_syms
+ r_symndx
;
1214 sec
= local_sections
[r_symndx
];
1215 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1217 name
= bfd_elf_string_from_elf_section
1218 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1219 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1223 bfd_boolean unresolved_reloc
, warned
;
1225 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1226 r_symndx
, symtab_hdr
, sym_hashes
,
1228 unresolved_reloc
, warned
);
1230 name
= h
->root
.root
.string
;
1233 r
= avr_final_link_relocate (howto
, input_bfd
, input_section
,
1234 contents
, rel
, relocation
, htab
);
1236 if (r
!= bfd_reloc_ok
)
1238 const char * msg
= (const char *) NULL
;
1242 case bfd_reloc_overflow
:
1243 r
= info
->callbacks
->reloc_overflow
1244 (info
, (h
? &h
->root
: NULL
),
1245 name
, howto
->name
, (bfd_vma
) 0,
1246 input_bfd
, input_section
, rel
->r_offset
);
1249 case bfd_reloc_undefined
:
1250 r
= info
->callbacks
->undefined_symbol
1251 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1254 case bfd_reloc_outofrange
:
1255 msg
= _("internal error: out of range error");
1258 case bfd_reloc_notsupported
:
1259 msg
= _("internal error: unsupported relocation error");
1262 case bfd_reloc_dangerous
:
1263 msg
= _("internal error: dangerous relocation");
1267 msg
= _("internal error: unknown error");
1272 r
= info
->callbacks
->warning
1273 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
1283 /* The final processing done just before writing out a AVR ELF object
1284 file. This gets the AVR architecture right based on the machine
1288 bfd_elf_avr_final_write_processing (bfd
*abfd
,
1289 bfd_boolean linker ATTRIBUTE_UNUSED
)
1293 switch (bfd_get_mach (abfd
))
1297 val
= E_AVR_MACH_AVR2
;
1301 val
= E_AVR_MACH_AVR1
;
1305 val
= E_AVR_MACH_AVR3
;
1309 val
= E_AVR_MACH_AVR4
;
1313 val
= E_AVR_MACH_AVR5
;
1317 val
= E_AVR_MACH_AVR6
;
1321 elf_elfheader (abfd
)->e_machine
= EM_AVR
;
1322 elf_elfheader (abfd
)->e_flags
&= ~ EF_AVR_MACH
;
1323 elf_elfheader (abfd
)->e_flags
|= val
;
1324 elf_elfheader (abfd
)->e_flags
|= EF_AVR_LINKRELAX_PREPARED
;
1327 /* Set the right machine number. */
1330 elf32_avr_object_p (bfd
*abfd
)
1332 unsigned int e_set
= bfd_mach_avr2
;
1334 if (elf_elfheader (abfd
)->e_machine
== EM_AVR
1335 || elf_elfheader (abfd
)->e_machine
== EM_AVR_OLD
)
1337 int e_mach
= elf_elfheader (abfd
)->e_flags
& EF_AVR_MACH
;
1342 case E_AVR_MACH_AVR2
:
1343 e_set
= bfd_mach_avr2
;
1346 case E_AVR_MACH_AVR1
:
1347 e_set
= bfd_mach_avr1
;
1350 case E_AVR_MACH_AVR3
:
1351 e_set
= bfd_mach_avr3
;
1354 case E_AVR_MACH_AVR4
:
1355 e_set
= bfd_mach_avr4
;
1358 case E_AVR_MACH_AVR5
:
1359 e_set
= bfd_mach_avr5
;
1362 case E_AVR_MACH_AVR6
:
1363 e_set
= bfd_mach_avr6
;
1367 return bfd_default_set_arch_mach (abfd
, bfd_arch_avr
,
1372 /* Delete some bytes from a section while changing the size of an instruction.
1373 The parameter "addr" denotes the section-relative offset pointing just
1374 behind the shrinked instruction. "addr+count" point at the first
1375 byte just behind the original unshrinked instruction. */
1378 elf32_avr_relax_delete_bytes (bfd
*abfd
,
1383 Elf_Internal_Shdr
*symtab_hdr
;
1384 unsigned int sec_shndx
;
1386 Elf_Internal_Rela
*irel
, *irelend
;
1387 Elf_Internal_Rela
*irelalign
;
1388 Elf_Internal_Sym
*isym
;
1389 Elf_Internal_Sym
*isymbuf
= NULL
;
1390 Elf_Internal_Sym
*isymend
;
1392 struct elf_link_hash_entry
**sym_hashes
;
1393 struct elf_link_hash_entry
**end_hashes
;
1394 unsigned int symcount
;
1396 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1397 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1398 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1400 /* The deletion must stop at the next ALIGN reloc for an aligment
1401 power larger than the number of bytes we are deleting. */
1406 irel
= elf_section_data (sec
)->relocs
;
1407 irelend
= irel
+ sec
->reloc_count
;
1409 /* Actually delete the bytes. */
1410 if (toaddr
- addr
- count
> 0)
1411 memmove (contents
+ addr
, contents
+ addr
+ count
,
1412 (size_t) (toaddr
- addr
- count
));
1415 /* Adjust all the reloc addresses. */
1416 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1418 bfd_vma old_reloc_address
;
1419 bfd_vma shrinked_insn_address
;
1421 old_reloc_address
= (sec
->output_section
->vma
1422 + sec
->output_offset
+ irel
->r_offset
);
1423 shrinked_insn_address
= (sec
->output_section
->vma
1424 + sec
->output_offset
+ addr
- count
);
1426 /* Get the new reloc address. */
1427 if ((irel
->r_offset
> addr
1428 && irel
->r_offset
< toaddr
))
1431 printf ("Relocation at address 0x%x needs to be moved.\n"
1432 "Old section offset: 0x%x, New section offset: 0x%x \n",
1433 (unsigned int) old_reloc_address
,
1434 (unsigned int) irel
->r_offset
,
1435 (unsigned int) ((irel
->r_offset
) - count
));
1437 irel
->r_offset
-= count
;
1442 /* The reloc's own addresses are now ok. However, we need to readjust
1443 the reloc's addend, i.e. the reloc's value if two conditions are met:
1444 1.) the reloc is relative to a symbol in this section that
1445 is located in front of the shrinked instruction
1446 2.) symbol plus addend end up behind the shrinked instruction.
1448 The most common case where this happens are relocs relative to
1449 the section-start symbol.
1451 This step needs to be done for all of the sections of the bfd. */
1454 struct bfd_section
*isec
;
1456 for (isec
= abfd
->sections
; isec
; isec
= isec
->next
)
1459 bfd_vma shrinked_insn_address
;
1461 shrinked_insn_address
= (sec
->output_section
->vma
1462 + sec
->output_offset
+ addr
- count
);
1464 irelend
= elf_section_data (isec
)->relocs
+ isec
->reloc_count
;
1465 for (irel
= elf_section_data (isec
)->relocs
;
1469 /* Read this BFD's local symbols if we haven't done
1471 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1473 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1474 if (isymbuf
== NULL
)
1475 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1476 symtab_hdr
->sh_info
, 0,
1478 if (isymbuf
== NULL
)
1482 /* Get the value of the symbol referred to by the reloc. */
1483 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1485 /* A local symbol. */
1486 Elf_Internal_Sym
*isym
;
1489 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1490 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1491 symval
= isym
->st_value
;
1492 /* If the reloc is absolute, it will not have
1493 a symbol or section associated with it. */
1496 symval
+= sym_sec
->output_section
->vma
1497 + sym_sec
->output_offset
;
1500 printf ("Checking if the relocation's "
1501 "addend needs corrections.\n"
1502 "Address of anchor symbol: 0x%x \n"
1503 "Address of relocation target: 0x%x \n"
1504 "Address of relaxed insn: 0x%x \n",
1505 (unsigned int) symval
,
1506 (unsigned int) (symval
+ irel
->r_addend
),
1507 (unsigned int) shrinked_insn_address
);
1509 if (symval
<= shrinked_insn_address
1510 && (symval
+ irel
->r_addend
) > shrinked_insn_address
)
1512 irel
->r_addend
-= count
;
1515 printf ("Relocation's addend needed to be fixed \n");
1518 /* else...Reference symbol is absolute. No adjustment needed. */
1520 /* else...Reference symbol is extern. No need for adjusting
1526 /* Adjust the local symbols defined in this section. */
1527 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1528 isymend
= isym
+ symtab_hdr
->sh_info
;
1529 for (; isym
< isymend
; isym
++)
1531 if (isym
->st_shndx
== sec_shndx
1532 && isym
->st_value
> addr
1533 && isym
->st_value
< toaddr
)
1534 isym
->st_value
-= count
;
1537 /* Now adjust the global symbols defined in this section. */
1538 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1539 - symtab_hdr
->sh_info
);
1540 sym_hashes
= elf_sym_hashes (abfd
);
1541 end_hashes
= sym_hashes
+ symcount
;
1542 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1544 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1545 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1546 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1547 && sym_hash
->root
.u
.def
.section
== sec
1548 && sym_hash
->root
.u
.def
.value
> addr
1549 && sym_hash
->root
.u
.def
.value
< toaddr
)
1551 sym_hash
->root
.u
.def
.value
-= count
;
1558 /* This function handles relaxing for the avr.
1559 Many important relaxing opportunities within functions are already
1560 realized by the compiler itself.
1561 Here we try to replace call (4 bytes) -> rcall (2 bytes)
1562 and jump -> rjmp (safes also 2 bytes).
1563 As well we now optimize seqences of
1564 - call/rcall function
1569 . In case that within a sequence
1572 the ret could no longer be reached it is optimized away. In order
1573 to check if the ret is no longer needed, it is checked that the ret's address
1574 is not the target of a branch or jump within the same section, it is checked
1575 that there is no skip instruction before the jmp/rjmp and that there
1576 is no local or global label place at the address of the ret.
1578 We refrain from relaxing within sections ".vectors" and
1579 ".jumptables" in order to maintain the position of the instructions.
1580 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
1581 if possible. (In future one could possibly use the space of the nop
1582 for the first instruction of the irq service function.
1584 The .jumptables sections is meant to be used for a future tablejump variant
1585 for the devices with 3-byte program counter where the table itself
1586 contains 4-byte jump instructions whose relative offset must not
1590 elf32_avr_relax_section (bfd
*abfd
,
1592 struct bfd_link_info
*link_info
,
1595 Elf_Internal_Shdr
*symtab_hdr
;
1596 Elf_Internal_Rela
*internal_relocs
;
1597 Elf_Internal_Rela
*irel
, *irelend
;
1598 bfd_byte
*contents
= NULL
;
1599 Elf_Internal_Sym
*isymbuf
= NULL
;
1600 static asection
*last_input_section
= NULL
;
1601 static Elf_Internal_Rela
*last_reloc
= NULL
;
1602 struct elf32_avr_link_hash_table
*htab
;
1604 htab
= avr_link_hash_table (link_info
);
1606 /* Assume nothing changes. */
1609 if ((!htab
->no_stubs
) && (sec
== htab
->stub_sec
))
1611 /* We are just relaxing the stub section.
1612 Let's calculate the size needed again. */
1613 bfd_size_type last_estimated_stub_section_size
= htab
->stub_sec
->size
;
1616 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
1617 (int) last_estimated_stub_section_size
);
1619 elf32_avr_size_stubs (htab
->stub_sec
->output_section
->owner
,
1622 /* Check if the number of trampolines changed. */
1623 if (last_estimated_stub_section_size
!= htab
->stub_sec
->size
)
1627 printf ("Size of stub section after this pass: %i\n",
1628 (int) htab
->stub_sec
->size
);
1633 /* We don't have to do anything for a relocatable link, if
1634 this section does not have relocs, or if this is not a
1636 if (link_info
->relocatable
1637 || (sec
->flags
& SEC_RELOC
) == 0
1638 || sec
->reloc_count
== 0
1639 || (sec
->flags
& SEC_CODE
) == 0)
1642 /* Check if the object file to relax uses internal symbols so that we
1643 could fix up the relocations. */
1644 if (!(elf_elfheader (abfd
)->e_flags
& EF_AVR_LINKRELAX_PREPARED
))
1647 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1649 /* Get a copy of the native relocations. */
1650 internal_relocs
= (_bfd_elf_link_read_relocs
1651 (abfd
, sec
, NULL
, NULL
, link_info
->keep_memory
));
1652 if (internal_relocs
== NULL
)
1655 if (sec
!= last_input_section
)
1658 last_input_section
= sec
;
1660 /* Walk through the relocs looking for relaxing opportunities. */
1661 irelend
= internal_relocs
+ sec
->reloc_count
;
1662 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1666 if ( ELF32_R_TYPE (irel
->r_info
) != R_AVR_13_PCREL
1667 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_7_PCREL
1668 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_CALL
)
1671 /* Get the section contents if we haven't done so already. */
1672 if (contents
== NULL
)
1674 /* Get cached copy if it exists. */
1675 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1676 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1679 /* Go get them off disk. */
1680 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1685 /* Read this BFD's local symbols if we haven't done so already. */
1686 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1688 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1689 if (isymbuf
== NULL
)
1690 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1691 symtab_hdr
->sh_info
, 0,
1693 if (isymbuf
== NULL
)
1698 /* Get the value of the symbol referred to by the reloc. */
1699 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1701 /* A local symbol. */
1702 Elf_Internal_Sym
*isym
;
1705 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1706 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1707 symval
= isym
->st_value
;
1708 /* If the reloc is absolute, it will not have
1709 a symbol or section associated with it. */
1711 symval
+= sym_sec
->output_section
->vma
1712 + sym_sec
->output_offset
;
1717 struct elf_link_hash_entry
*h
;
1719 /* An external symbol. */
1720 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
1721 h
= elf_sym_hashes (abfd
)[indx
];
1722 BFD_ASSERT (h
!= NULL
);
1723 if (h
->root
.type
!= bfd_link_hash_defined
1724 && h
->root
.type
!= bfd_link_hash_defweak
)
1725 /* This appears to be a reference to an undefined
1726 symbol. Just ignore it--it will be caught by the
1727 regular reloc processing. */
1730 symval
= (h
->root
.u
.def
.value
1731 + h
->root
.u
.def
.section
->output_section
->vma
1732 + h
->root
.u
.def
.section
->output_offset
);
1735 /* For simplicity of coding, we are going to modify the section
1736 contents, the section relocs, and the BFD symbol table. We
1737 must tell the rest of the code not to free up this
1738 information. It would be possible to instead create a table
1739 of changes which have to be made, as is done in coff-mips.c;
1740 that would be more work, but would require less memory when
1741 the linker is run. */
1742 switch (ELF32_R_TYPE (irel
->r_info
))
1744 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1745 pc-relative rcall/rjmp. */
1748 bfd_vma value
= symval
+ irel
->r_addend
;
1750 int distance_short_enough
= 0;
1752 /* Get the address of this instruction. */
1753 dot
= (sec
->output_section
->vma
1754 + sec
->output_offset
+ irel
->r_offset
);
1756 /* Compute the distance from this insn to the branch target. */
1759 /* If the distance is within -4094..+4098 inclusive, then we can
1760 relax this jump/call. +4098 because the call/jump target
1761 will be closer after the relaxation. */
1762 if ((int) gap
>= -4094 && (int) gap
<= 4098)
1763 distance_short_enough
= 1;
1765 /* Here we handle the wrap-around case. E.g. for a 16k device
1766 we could use a rjmp to jump from address 0x100 to 0x3d00!
1767 In order to make this work properly, we need to fill the
1768 vaiable avr_pc_wrap_around with the appropriate value.
1769 I.e. 0x4000 for a 16k device. */
1771 /* Shrinking the code size makes the gaps larger in the
1772 case of wrap-arounds. So we use a heuristical safety
1773 margin to avoid that during relax the distance gets
1774 again too large for the short jumps. Let's assume
1775 a typical code-size reduction due to relax for a
1776 16k device of 600 bytes. So let's use twice the
1777 typical value as safety margin. */
1781 int assumed_shrink
= 600;
1782 if (avr_pc_wrap_around
> 0x4000)
1783 assumed_shrink
= 900;
1785 safety_margin
= 2 * assumed_shrink
;
1787 rgap
= avr_relative_distance_considering_wrap_around (gap
);
1789 if (rgap
>= (-4092 + safety_margin
)
1790 && rgap
<= (4094 - safety_margin
))
1791 distance_short_enough
= 1;
1794 if (distance_short_enough
)
1796 unsigned char code_msb
;
1797 unsigned char code_lsb
;
1800 printf ("shrinking jump/call instruction at address 0x%x"
1801 " in section %s\n\n",
1802 (int) dot
, sec
->name
);
1804 /* Note that we've changed the relocs, section contents,
1806 elf_section_data (sec
)->relocs
= internal_relocs
;
1807 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1808 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1810 /* Get the instruction code for relaxing. */
1811 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
);
1812 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1814 /* Mask out the relocation bits. */
1817 if (code_msb
== 0x94 && code_lsb
== 0x0E)
1819 /* we are changing call -> rcall . */
1820 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
1821 bfd_put_8 (abfd
, 0xD0, contents
+ irel
->r_offset
+ 1);
1823 else if (code_msb
== 0x94 && code_lsb
== 0x0C)
1825 /* we are changeing jump -> rjmp. */
1826 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
1827 bfd_put_8 (abfd
, 0xC0, contents
+ irel
->r_offset
+ 1);
1832 /* Fix the relocation's type. */
1833 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1836 /* Check for the vector section. There we don't want to
1837 modify the ordering! */
1839 if (!strcmp (sec
->name
,".vectors")
1840 || !strcmp (sec
->name
,".jumptables"))
1842 /* Let's insert a nop. */
1843 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 2);
1844 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 3);
1848 /* Delete two bytes of data. */
1849 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
1850 irel
->r_offset
+ 2, 2))
1853 /* That will change things, so, we should relax again.
1854 Note that this is not required, and it may be slow. */
1862 unsigned char code_msb
;
1863 unsigned char code_lsb
;
1866 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1867 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 0);
1869 /* Get the address of this instruction. */
1870 dot
= (sec
->output_section
->vma
1871 + sec
->output_offset
+ irel
->r_offset
);
1873 /* Here we look for rcall/ret or call/ret sequences that could be
1874 safely replaced by rjmp/ret or jmp/ret. */
1875 if (((code_msb
& 0xf0) == 0xd0)
1876 && avr_replace_call_ret_sequences
)
1878 /* This insn is a rcall. */
1879 unsigned char next_insn_msb
= 0;
1880 unsigned char next_insn_lsb
= 0;
1882 if (irel
->r_offset
+ 3 < sec
->size
)
1885 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 3);
1887 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 2);
1890 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1892 /* The next insn is a ret. We now convert the rcall insn
1893 into a rjmp instruction. */
1895 bfd_put_8 (abfd
, code_msb
, contents
+ irel
->r_offset
+ 1);
1897 printf ("converted rcall/ret sequence at address 0x%x"
1898 " into rjmp/ret sequence. Section is %s\n\n",
1899 (int) dot
, sec
->name
);
1904 else if ((0x94 == (code_msb
& 0xfe))
1905 && (0x0e == (code_lsb
& 0x0e))
1906 && avr_replace_call_ret_sequences
)
1908 /* This insn is a call. */
1909 unsigned char next_insn_msb
= 0;
1910 unsigned char next_insn_lsb
= 0;
1912 if (irel
->r_offset
+ 5 < sec
->size
)
1915 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 5);
1917 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 4);
1920 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1922 /* The next insn is a ret. We now convert the call insn
1923 into a jmp instruction. */
1926 bfd_put_8 (abfd
, code_lsb
, contents
+ irel
->r_offset
);
1928 printf ("converted call/ret sequence at address 0x%x"
1929 " into jmp/ret sequence. Section is %s\n\n",
1930 (int) dot
, sec
->name
);
1935 else if ((0xc0 == (code_msb
& 0xf0))
1936 || ((0x94 == (code_msb
& 0xfe))
1937 && (0x0c == (code_lsb
& 0x0e))))
1939 /* This insn is a rjmp or a jmp. */
1940 unsigned char next_insn_msb
= 0;
1941 unsigned char next_insn_lsb
= 0;
1944 if (0xc0 == (code_msb
& 0xf0))
1945 insn_size
= 2; /* rjmp insn */
1947 insn_size
= 4; /* jmp insn */
1949 if (irel
->r_offset
+ insn_size
+ 1 < sec
->size
)
1952 bfd_get_8 (abfd
, contents
+ irel
->r_offset
1955 bfd_get_8 (abfd
, contents
+ irel
->r_offset
1959 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1961 /* The next insn is a ret. We possibly could delete
1962 this ret. First we need to check for preceeding
1963 sbis/sbic/sbrs or cpse "skip" instructions. */
1965 int there_is_preceeding_non_skip_insn
= 1;
1966 bfd_vma address_of_ret
;
1968 address_of_ret
= dot
+ insn_size
;
1970 if (debug_relax
&& (insn_size
== 2))
1971 printf ("found rjmp / ret sequence at address 0x%x\n",
1973 if (debug_relax
&& (insn_size
== 4))
1974 printf ("found jmp / ret sequence at address 0x%x\n",
1977 /* We have to make sure that there is a preceeding insn. */
1978 if (irel
->r_offset
>= 2)
1980 unsigned char preceeding_msb
;
1981 unsigned char preceeding_lsb
;
1983 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1985 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1988 if (0x99 == preceeding_msb
)
1989 there_is_preceeding_non_skip_insn
= 0;
1992 if (0x9b == preceeding_msb
)
1993 there_is_preceeding_non_skip_insn
= 0;
1996 if ((0xfc == (preceeding_msb
& 0xfe)
1997 && (0x00 == (preceeding_lsb
& 0x08))))
1998 there_is_preceeding_non_skip_insn
= 0;
2001 if ((0xfe == (preceeding_msb
& 0xfe)
2002 && (0x00 == (preceeding_lsb
& 0x08))))
2003 there_is_preceeding_non_skip_insn
= 0;
2006 if (0x10 == (preceeding_msb
& 0xfc))
2007 there_is_preceeding_non_skip_insn
= 0;
2009 if (there_is_preceeding_non_skip_insn
== 0)
2011 printf ("preceeding skip insn prevents deletion of"
2012 " ret insn at addr 0x%x in section %s\n",
2013 (int) dot
+ 2, sec
->name
);
2017 /* There is no previous instruction. */
2018 there_is_preceeding_non_skip_insn
= 0;
2021 if (there_is_preceeding_non_skip_insn
)
2023 /* We now only have to make sure that there is no
2024 local label defined at the address of the ret
2025 instruction and that there is no local relocation
2026 in this section pointing to the ret. */
2028 int deleting_ret_is_safe
= 1;
2029 unsigned int section_offset_of_ret_insn
=
2030 irel
->r_offset
+ insn_size
;
2031 Elf_Internal_Sym
*isym
, *isymend
;
2032 unsigned int sec_shndx
;
2035 _bfd_elf_section_from_bfd_section (abfd
, sec
);
2037 /* Check for local symbols. */
2038 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2039 isymend
= isym
+ symtab_hdr
->sh_info
;
2040 for (; isym
< isymend
; isym
++)
2042 if (isym
->st_value
== section_offset_of_ret_insn
2043 && isym
->st_shndx
== sec_shndx
)
2045 deleting_ret_is_safe
= 0;
2047 printf ("local label prevents deletion of ret "
2048 "insn at address 0x%x\n",
2049 (int) dot
+ insn_size
);
2053 /* Now check for global symbols. */
2056 struct elf_link_hash_entry
**sym_hashes
;
2057 struct elf_link_hash_entry
**end_hashes
;
2059 symcount
= (symtab_hdr
->sh_size
2060 / sizeof (Elf32_External_Sym
)
2061 - symtab_hdr
->sh_info
);
2062 sym_hashes
= elf_sym_hashes (abfd
);
2063 end_hashes
= sym_hashes
+ symcount
;
2064 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2066 struct elf_link_hash_entry
*sym_hash
=
2068 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2069 || sym_hash
->root
.type
==
2070 bfd_link_hash_defweak
)
2071 && sym_hash
->root
.u
.def
.section
== sec
2072 && sym_hash
->root
.u
.def
.value
== section_offset_of_ret_insn
)
2074 deleting_ret_is_safe
= 0;
2076 printf ("global label prevents deletion of "
2077 "ret insn at address 0x%x\n",
2078 (int) dot
+ insn_size
);
2082 /* Now we check for relocations pointing to ret. */
2084 Elf_Internal_Rela
*irel
;
2085 Elf_Internal_Rela
*relend
;
2086 Elf_Internal_Shdr
*symtab_hdr
;
2088 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2089 relend
= elf_section_data (sec
)->relocs
2092 for (irel
= elf_section_data (sec
)->relocs
;
2093 irel
< relend
; irel
++)
2095 bfd_vma reloc_target
= 0;
2097 Elf_Internal_Sym
*isymbuf
= NULL
;
2099 /* Read this BFD's local symbols if we haven't
2101 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2103 isymbuf
= (Elf_Internal_Sym
*)
2104 symtab_hdr
->contents
;
2105 if (isymbuf
== NULL
)
2106 isymbuf
= bfd_elf_get_elf_syms
2109 symtab_hdr
->sh_info
, 0,
2111 if (isymbuf
== NULL
)
2115 /* Get the value of the symbol referred to
2117 if (ELF32_R_SYM (irel
->r_info
)
2118 < symtab_hdr
->sh_info
)
2120 /* A local symbol. */
2121 Elf_Internal_Sym
*isym
;
2125 + ELF32_R_SYM (irel
->r_info
);
2126 sym_sec
= bfd_section_from_elf_index
2127 (abfd
, isym
->st_shndx
);
2128 symval
= isym
->st_value
;
2130 /* If the reloc is absolute, it will not
2131 have a symbol or section associated
2137 sym_sec
->output_section
->vma
2138 + sym_sec
->output_offset
;
2139 reloc_target
= symval
+ irel
->r_addend
;
2143 reloc_target
= symval
+ irel
->r_addend
;
2144 /* Reference symbol is absolute. */
2147 /* else ... reference symbol is extern. */
2149 if (address_of_ret
== reloc_target
)
2151 deleting_ret_is_safe
= 0;
2154 "rjmp/jmp ret sequence at address"
2155 " 0x%x could not be deleted. ret"
2156 " is target of a relocation.\n",
2157 (int) address_of_ret
);
2162 if (deleting_ret_is_safe
)
2165 printf ("unreachable ret instruction "
2166 "at address 0x%x deleted.\n",
2167 (int) dot
+ insn_size
);
2169 /* Delete two bytes of data. */
2170 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
2171 irel
->r_offset
+ insn_size
, 2))
2174 /* That will change things, so, we should relax
2175 again. Note that this is not required, and it
2189 if (contents
!= NULL
2190 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
2192 if (! link_info
->keep_memory
)
2196 /* Cache the section contents for elf_link_input_bfd. */
2197 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2201 if (internal_relocs
!= NULL
2202 && elf_section_data (sec
)->relocs
!= internal_relocs
)
2203 free (internal_relocs
);
2209 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2211 if (contents
!= NULL
2212 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
2214 if (internal_relocs
!= NULL
2215 && elf_section_data (sec
)->relocs
!= internal_relocs
)
2216 free (internal_relocs
);
2221 /* This is a version of bfd_generic_get_relocated_section_contents
2222 which uses elf32_avr_relocate_section.
2224 For avr it's essentially a cut and paste taken from the H8300 port.
2225 The author of the relaxation support patch for avr had absolutely no
2226 clue what is happening here but found out that this part of the code
2227 seems to be important. */
2230 elf32_avr_get_relocated_section_contents (bfd
*output_bfd
,
2231 struct bfd_link_info
*link_info
,
2232 struct bfd_link_order
*link_order
,
2234 bfd_boolean relocatable
,
2237 Elf_Internal_Shdr
*symtab_hdr
;
2238 asection
*input_section
= link_order
->u
.indirect
.section
;
2239 bfd
*input_bfd
= input_section
->owner
;
2240 asection
**sections
= NULL
;
2241 Elf_Internal_Rela
*internal_relocs
= NULL
;
2242 Elf_Internal_Sym
*isymbuf
= NULL
;
2244 /* We only need to handle the case of relaxing, or of having a
2245 particular set of section contents, specially. */
2247 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
2248 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
2252 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2254 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
2255 (size_t) input_section
->size
);
2257 if ((input_section
->flags
& SEC_RELOC
) != 0
2258 && input_section
->reloc_count
> 0)
2261 Elf_Internal_Sym
*isym
, *isymend
;
2264 internal_relocs
= (_bfd_elf_link_read_relocs
2265 (input_bfd
, input_section
, NULL
, NULL
, FALSE
));
2266 if (internal_relocs
== NULL
)
2269 if (symtab_hdr
->sh_info
!= 0)
2271 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2272 if (isymbuf
== NULL
)
2273 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2274 symtab_hdr
->sh_info
, 0,
2276 if (isymbuf
== NULL
)
2280 amt
= symtab_hdr
->sh_info
;
2281 amt
*= sizeof (asection
*);
2282 sections
= bfd_malloc (amt
);
2283 if (sections
== NULL
&& amt
!= 0)
2286 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2287 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
2291 if (isym
->st_shndx
== SHN_UNDEF
)
2292 isec
= bfd_und_section_ptr
;
2293 else if (isym
->st_shndx
== SHN_ABS
)
2294 isec
= bfd_abs_section_ptr
;
2295 else if (isym
->st_shndx
== SHN_COMMON
)
2296 isec
= bfd_com_section_ptr
;
2298 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
2303 if (! elf32_avr_relocate_section (output_bfd
, link_info
, input_bfd
,
2304 input_section
, data
, internal_relocs
,
2308 if (sections
!= NULL
)
2311 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2313 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
2314 free (internal_relocs
);
2320 if (sections
!= NULL
)
2323 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2325 if (internal_relocs
!= NULL
2326 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
2327 free (internal_relocs
);
2332 /* Determines the hash entry name for a particular reloc. It consists of
2333 the identifier of the symbol section and the added reloc addend and
2334 symbol offset relative to the section the symbol is attached to. */
2337 avr_stub_name (const asection
*symbol_section
,
2338 const bfd_vma symbol_offset
,
2339 const Elf_Internal_Rela
*rela
)
2344 len
= 8 + 1 + 8 + 1 + 1;
2345 stub_name
= bfd_malloc (len
);
2347 sprintf (stub_name
, "%08x+%08x",
2348 symbol_section
->id
& 0xffffffff,
2349 (unsigned int) ((rela
->r_addend
& 0xffffffff) + symbol_offset
));
2355 /* Add a new stub entry to the stub hash. Not all fields of the new
2356 stub entry are initialised. */
2358 static struct elf32_avr_stub_hash_entry
*
2359 avr_add_stub (const char *stub_name
,
2360 struct elf32_avr_link_hash_table
*htab
)
2362 struct elf32_avr_stub_hash_entry
*hsh
;
2364 /* Enter this entry into the linker stub hash table. */
2365 hsh
= avr_stub_hash_lookup (&htab
->bstab
, stub_name
, TRUE
, FALSE
);
2369 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
2374 hsh
->stub_offset
= 0;
2378 /* We assume that there is already space allocated for the stub section
2379 contents and that before building the stubs the section size is
2380 initialized to 0. We assume that within the stub hash table entry,
2381 the absolute position of the jmp target has been written in the
2382 target_value field. We write here the offset of the generated jmp insn
2383 relative to the trampoline section start to the stub_offset entry in
2384 the stub hash table entry. */
2387 avr_build_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
2389 struct elf32_avr_stub_hash_entry
*hsh
;
2390 struct bfd_link_info
*info
;
2391 struct elf32_avr_link_hash_table
*htab
;
2398 bfd_vma jmp_insn
= 0x0000940c;
2400 /* Massage our args to the form they really have. */
2401 hsh
= avr_stub_hash_entry (bh
);
2403 if (!hsh
->is_actually_needed
)
2406 info
= (struct bfd_link_info
*) in_arg
;
2408 htab
= avr_link_hash_table (info
);
2410 target
= hsh
->target_value
;
2412 /* Make a note of the offset within the stubs for this entry. */
2413 hsh
->stub_offset
= htab
->stub_sec
->size
;
2414 loc
= htab
->stub_sec
->contents
+ hsh
->stub_offset
;
2416 stub_bfd
= htab
->stub_sec
->owner
;
2419 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
2420 (unsigned int) target
,
2421 (unsigned int) hsh
->stub_offset
);
2423 /* We now have to add the information on the jump target to the bare
2424 opcode bits already set in jmp_insn. */
2426 /* Check for the alignment of the address. */
2430 starget
= target
>> 1;
2431 jmp_insn
|= ((starget
& 0x10000) | ((starget
<< 3) & 0x1f00000)) >> 16;
2432 bfd_put_16 (stub_bfd
, jmp_insn
, loc
);
2433 bfd_put_16 (stub_bfd
, (bfd_vma
) starget
& 0xffff, loc
+ 2);
2435 htab
->stub_sec
->size
+= 4;
2437 /* Now add the entries in the address mapping table if there is still
2442 nr
= htab
->amt_entry_cnt
+ 1;
2443 if (nr
<= htab
->amt_max_entry_cnt
)
2445 htab
->amt_entry_cnt
= nr
;
2447 htab
->amt_stub_offsets
[nr
- 1] = hsh
->stub_offset
;
2448 htab
->amt_destination_addr
[nr
- 1] = target
;
2456 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry
*bh
,
2459 struct elf32_avr_stub_hash_entry
*hsh
;
2460 struct elf32_avr_link_hash_table
*htab
;
2463 hsh
= avr_stub_hash_entry (bh
);
2464 hsh
->is_actually_needed
= FALSE
;
2470 avr_size_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
2472 struct elf32_avr_stub_hash_entry
*hsh
;
2473 struct elf32_avr_link_hash_table
*htab
;
2476 /* Massage our args to the form they really have. */
2477 hsh
= avr_stub_hash_entry (bh
);
2480 if (hsh
->is_actually_needed
)
2485 htab
->stub_sec
->size
+= size
;
2490 elf32_avr_setup_params (struct bfd_link_info
*info
,
2492 asection
*avr_stub_section
,
2493 bfd_boolean no_stubs
,
2494 bfd_boolean deb_stubs
,
2495 bfd_boolean deb_relax
,
2496 bfd_vma pc_wrap_around
,
2497 bfd_boolean call_ret_replacement
)
2499 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table(info
);
2501 htab
->stub_sec
= avr_stub_section
;
2502 htab
->stub_bfd
= avr_stub_bfd
;
2503 htab
->no_stubs
= no_stubs
;
2505 debug_relax
= deb_relax
;
2506 debug_stubs
= deb_stubs
;
2507 avr_pc_wrap_around
= pc_wrap_around
;
2508 avr_replace_call_ret_sequences
= call_ret_replacement
;
2512 /* Set up various things so that we can make a list of input sections
2513 for each output section included in the link. Returns -1 on error,
2514 0 when no stubs will be needed, and 1 on success. It also sets
2515 information on the stubs bfd and the stub section in the info
2519 elf32_avr_setup_section_lists (bfd
*output_bfd
,
2520 struct bfd_link_info
*info
)
2523 unsigned int bfd_count
;
2524 int top_id
, top_index
;
2526 asection
**input_list
, **list
;
2528 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table(info
);
2533 /* Count the number of input BFDs and find the top input section id. */
2534 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
2536 input_bfd
= input_bfd
->link_next
)
2539 for (section
= input_bfd
->sections
;
2541 section
= section
->next
)
2542 if (top_id
< section
->id
)
2543 top_id
= section
->id
;
2546 htab
->bfd_count
= bfd_count
;
2548 /* We can't use output_bfd->section_count here to find the top output
2549 section index as some sections may have been removed, and
2550 strip_excluded_output_sections doesn't renumber the indices. */
2551 for (section
= output_bfd
->sections
, top_index
= 0;
2553 section
= section
->next
)
2554 if (top_index
< section
->index
)
2555 top_index
= section
->index
;
2557 htab
->top_index
= top_index
;
2558 amt
= sizeof (asection
*) * (top_index
+ 1);
2559 input_list
= bfd_malloc (amt
);
2560 htab
->input_list
= input_list
;
2561 if (input_list
== NULL
)
2564 /* For sections we aren't interested in, mark their entries with a
2565 value we can check later. */
2566 list
= input_list
+ top_index
;
2568 *list
= bfd_abs_section_ptr
;
2569 while (list
-- != input_list
);
2571 for (section
= output_bfd
->sections
;
2573 section
= section
->next
)
2574 if ((section
->flags
& SEC_CODE
) != 0)
2575 input_list
[section
->index
] = NULL
;
2581 /* Read in all local syms for all input bfds, and create hash entries
2582 for export stubs if we are building a multi-subspace shared lib.
2583 Returns -1 on error, 0 otherwise. */
2586 get_local_syms (bfd
*input_bfd
, struct bfd_link_info
*info
)
2588 unsigned int bfd_indx
;
2589 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
2590 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
2592 /* We want to read in symbol extension records only once. To do this
2593 we need to read in the local symbols in parallel and save them for
2594 later use; so hold pointers to the local symbols in an array. */
2595 bfd_size_type amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
2596 all_local_syms
= bfd_zmalloc (amt
);
2597 htab
->all_local_syms
= all_local_syms
;
2598 if (all_local_syms
== NULL
)
2601 /* Walk over all the input BFDs, swapping in local symbols.
2602 If we are creating a shared library, create hash entries for the
2606 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
2608 Elf_Internal_Shdr
*symtab_hdr
;
2610 /* We'll need the symbol table in a second. */
2611 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2612 if (symtab_hdr
->sh_info
== 0)
2615 /* We need an array of the local symbols attached to the input bfd. */
2616 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2617 if (local_syms
== NULL
)
2619 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2620 symtab_hdr
->sh_info
, 0,
2622 /* Cache them for elf_link_input_bfd. */
2623 symtab_hdr
->contents
= (unsigned char *) local_syms
;
2625 if (local_syms
== NULL
)
2628 all_local_syms
[bfd_indx
] = local_syms
;
2634 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
2637 elf32_avr_size_stubs (bfd
*output_bfd
,
2638 struct bfd_link_info
*info
,
2639 bfd_boolean is_prealloc_run
)
2641 struct elf32_avr_link_hash_table
*htab
;
2642 int stub_changed
= 0;
2644 htab
= avr_link_hash_table (info
);
2646 /* At this point we initialize htab->vector_base
2647 To the start of the text output section. */
2648 htab
->vector_base
= htab
->stub_sec
->output_section
->vma
;
2650 if (get_local_syms (info
->input_bfds
, info
))
2652 if (htab
->all_local_syms
)
2653 goto error_ret_free_local
;
2657 if (ADD_DUMMY_STUBS_FOR_DEBUGGING
)
2659 struct elf32_avr_stub_hash_entry
*test
;
2661 test
= avr_add_stub ("Hugo",htab
);
2662 test
->target_value
= 0x123456;
2663 test
->stub_offset
= 13;
2665 test
= avr_add_stub ("Hugo2",htab
);
2666 test
->target_value
= 0x84210;
2667 test
->stub_offset
= 14;
2673 unsigned int bfd_indx
;
2675 /* We will have to re-generate the stub hash table each time anything
2676 in memory has changed. */
2678 bfd_hash_traverse (&htab
->bstab
, avr_mark_stub_not_to_be_necessary
, htab
);
2679 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
2681 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
2683 Elf_Internal_Shdr
*symtab_hdr
;
2685 Elf_Internal_Sym
*local_syms
;
2687 /* We'll need the symbol table in a second. */
2688 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2689 if (symtab_hdr
->sh_info
== 0)
2692 local_syms
= htab
->all_local_syms
[bfd_indx
];
2694 /* Walk over each section attached to the input bfd. */
2695 for (section
= input_bfd
->sections
;
2697 section
= section
->next
)
2699 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
2701 /* If there aren't any relocs, then there's nothing more
2703 if ((section
->flags
& SEC_RELOC
) == 0
2704 || section
->reloc_count
== 0)
2707 /* If this section is a link-once section that will be
2708 discarded, then don't create any stubs. */
2709 if (section
->output_section
== NULL
2710 || section
->output_section
->owner
!= output_bfd
)
2713 /* Get the relocs. */
2715 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
2717 if (internal_relocs
== NULL
)
2718 goto error_ret_free_local
;
2720 /* Now examine each relocation. */
2721 irela
= internal_relocs
;
2722 irelaend
= irela
+ section
->reloc_count
;
2723 for (; irela
< irelaend
; irela
++)
2725 unsigned int r_type
, r_indx
;
2726 struct elf32_avr_stub_hash_entry
*hsh
;
2729 bfd_vma destination
;
2730 struct elf_link_hash_entry
*hh
;
2733 r_type
= ELF32_R_TYPE (irela
->r_info
);
2734 r_indx
= ELF32_R_SYM (irela
->r_info
);
2736 /* Only look for 16 bit GS relocs. No other reloc will need a
2738 if (!((r_type
== R_AVR_16_PM
)
2739 || (r_type
== R_AVR_LO8_LDI_GS
)
2740 || (r_type
== R_AVR_HI8_LDI_GS
)))
2743 /* Now determine the call target, its name, value,
2749 if (r_indx
< symtab_hdr
->sh_info
)
2751 /* It's a local symbol. */
2752 Elf_Internal_Sym
*sym
;
2753 Elf_Internal_Shdr
*hdr
;
2755 sym
= local_syms
+ r_indx
;
2756 hdr
= elf_elfsections (input_bfd
)[sym
->st_shndx
];
2757 sym_sec
= hdr
->bfd_section
;
2758 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2759 sym_value
= sym
->st_value
;
2760 destination
= (sym_value
+ irela
->r_addend
2761 + sym_sec
->output_offset
2762 + sym_sec
->output_section
->vma
);
2766 /* It's an external symbol. */
2769 e_indx
= r_indx
- symtab_hdr
->sh_info
;
2770 hh
= elf_sym_hashes (input_bfd
)[e_indx
];
2772 while (hh
->root
.type
== bfd_link_hash_indirect
2773 || hh
->root
.type
== bfd_link_hash_warning
)
2774 hh
= (struct elf_link_hash_entry
*)
2775 (hh
->root
.u
.i
.link
);
2777 if (hh
->root
.type
== bfd_link_hash_defined
2778 || hh
->root
.type
== bfd_link_hash_defweak
)
2780 sym_sec
= hh
->root
.u
.def
.section
;
2781 sym_value
= hh
->root
.u
.def
.value
;
2782 if (sym_sec
->output_section
!= NULL
)
2783 destination
= (sym_value
+ irela
->r_addend
2784 + sym_sec
->output_offset
2785 + sym_sec
->output_section
->vma
);
2787 else if (hh
->root
.type
== bfd_link_hash_undefweak
)
2792 else if (hh
->root
.type
== bfd_link_hash_undefined
)
2794 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
2795 && (ELF_ST_VISIBILITY (hh
->other
)
2801 bfd_set_error (bfd_error_bad_value
);
2803 error_ret_free_internal
:
2804 if (elf_section_data (section
)->relocs
== NULL
)
2805 free (internal_relocs
);
2806 goto error_ret_free_local
;
2810 if (! avr_stub_is_required_for_16_bit_reloc
2811 (destination
- htab
->vector_base
))
2813 if (!is_prealloc_run
)
2814 /* We are having a reloc that does't need a stub. */
2817 /* We don't right now know if a stub will be needed.
2818 Let's rather be on the safe side. */
2821 /* Get the name of this stub. */
2822 stub_name
= avr_stub_name (sym_sec
, sym_value
, irela
);
2825 goto error_ret_free_internal
;
2828 hsh
= avr_stub_hash_lookup (&htab
->bstab
,
2833 /* The proper stub has already been created. Mark it
2834 to be used and write the possibly changed destination
2836 hsh
->is_actually_needed
= TRUE
;
2837 hsh
->target_value
= destination
;
2842 hsh
= avr_add_stub (stub_name
, htab
);
2846 goto error_ret_free_internal
;
2849 hsh
->is_actually_needed
= TRUE
;
2850 hsh
->target_value
= destination
;
2853 printf ("Adding stub with destination 0x%x to the"
2854 " hash table.\n", (unsigned int) destination
);
2856 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run
);
2858 stub_changed
= TRUE
;
2861 /* We're done with the internal relocs, free them. */
2862 if (elf_section_data (section
)->relocs
== NULL
)
2863 free (internal_relocs
);
2867 /* Re-Calculate the number of needed stubs. */
2868 htab
->stub_sec
->size
= 0;
2869 bfd_hash_traverse (&htab
->bstab
, avr_size_one_stub
, htab
);
2874 stub_changed
= FALSE
;
2877 free (htab
->all_local_syms
);
2880 error_ret_free_local
:
2881 free (htab
->all_local_syms
);
2886 /* Build all the stubs associated with the current output file. The
2887 stubs are kept in a hash table attached to the main linker hash
2888 table. We also set up the .plt entries for statically linked PIC
2889 functions here. This function is called via hppaelf_finish in the
2893 elf32_avr_build_stubs (struct bfd_link_info
*info
)
2896 struct bfd_hash_table
*table
;
2897 struct elf32_avr_link_hash_table
*htab
;
2898 bfd_size_type total_size
= 0;
2900 htab
= avr_link_hash_table (info
);
2902 /* In case that there were several stub sections: */
2903 for (stub_sec
= htab
->stub_bfd
->sections
;
2905 stub_sec
= stub_sec
->next
)
2909 /* Allocate memory to hold the linker stubs. */
2910 size
= stub_sec
->size
;
2913 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
2914 if (stub_sec
->contents
== NULL
&& size
!= 0)
2919 /* Allocate memory for the adress mapping table. */
2920 htab
->amt_entry_cnt
= 0;
2921 htab
->amt_max_entry_cnt
= total_size
/ 4;
2922 htab
->amt_stub_offsets
= bfd_malloc (sizeof (bfd_vma
)
2923 * htab
->amt_max_entry_cnt
);
2924 htab
->amt_destination_addr
= bfd_malloc (sizeof (bfd_vma
)
2925 * htab
->amt_max_entry_cnt
);
2928 printf ("Allocating %i entries in the AMT\n", htab
->amt_max_entry_cnt
);
2930 /* Build the stubs as directed by the stub hash table. */
2931 table
= &htab
->bstab
;
2932 bfd_hash_traverse (table
, avr_build_one_stub
, info
);
2935 printf ("Final Stub section Size: %i\n", (int) htab
->stub_sec
->size
);
2940 #define ELF_ARCH bfd_arch_avr
2941 #define ELF_MACHINE_CODE EM_AVR
2942 #define ELF_MACHINE_ALT1 EM_AVR_OLD
2943 #define ELF_MAXPAGESIZE 1
2945 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
2946 #define TARGET_LITTLE_NAME "elf32-avr"
2948 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
2949 #define bfd_elf32_bfd_link_hash_table_free elf32_avr_link_hash_table_free
2951 #define elf_info_to_howto avr_info_to_howto_rela
2952 #define elf_info_to_howto_rel NULL
2953 #define elf_backend_relocate_section elf32_avr_relocate_section
2954 #define elf_backend_gc_mark_hook elf32_avr_gc_mark_hook
2955 #define elf_backend_gc_sweep_hook elf32_avr_gc_sweep_hook
2956 #define elf_backend_check_relocs elf32_avr_check_relocs
2957 #define elf_backend_can_gc_sections 1
2958 #define elf_backend_rela_normal 1
2959 #define elf_backend_final_write_processing \
2960 bfd_elf_avr_final_write_processing
2961 #define elf_backend_object_p elf32_avr_object_p
2963 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
2964 #define bfd_elf32_bfd_get_relocated_section_contents \
2965 elf32_avr_get_relocated_section_contents
2967 #include "elf32-target.h"