1 /* AVR-specific support for 32-bit ELF
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 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_table 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 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
108 == AVR_ELF_DATA ? ((struct elf32_avr_link_hash_table *) ((p)->hash)) : NULL)
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 */
506 HOWTO (R_AVR_8
, /* type */
508 0, /* size (0 = byte, 1 = short, 2 = long) */
510 FALSE
, /* pc_relative */
512 complain_overflow_bitfield
,/* complain_on_overflow */
513 bfd_elf_generic_reloc
, /* special_function */
514 "R_AVR_8", /* name */
515 FALSE
, /* partial_inplace */
516 0x000000ff, /* src_mask */
517 0x000000ff, /* dst_mask */
518 FALSE
), /* pcrel_offset */
521 /* Map BFD reloc types to AVR ELF reloc types. */
525 bfd_reloc_code_real_type bfd_reloc_val
;
526 unsigned int elf_reloc_val
;
529 static const struct avr_reloc_map avr_reloc_map
[] =
531 { BFD_RELOC_NONE
, R_AVR_NONE
},
532 { BFD_RELOC_32
, R_AVR_32
},
533 { BFD_RELOC_AVR_7_PCREL
, R_AVR_7_PCREL
},
534 { BFD_RELOC_AVR_13_PCREL
, R_AVR_13_PCREL
},
535 { BFD_RELOC_16
, R_AVR_16
},
536 { BFD_RELOC_AVR_16_PM
, R_AVR_16_PM
},
537 { BFD_RELOC_AVR_LO8_LDI
, R_AVR_LO8_LDI
},
538 { BFD_RELOC_AVR_HI8_LDI
, R_AVR_HI8_LDI
},
539 { BFD_RELOC_AVR_HH8_LDI
, R_AVR_HH8_LDI
},
540 { BFD_RELOC_AVR_MS8_LDI
, R_AVR_MS8_LDI
},
541 { BFD_RELOC_AVR_LO8_LDI_NEG
, R_AVR_LO8_LDI_NEG
},
542 { BFD_RELOC_AVR_HI8_LDI_NEG
, R_AVR_HI8_LDI_NEG
},
543 { BFD_RELOC_AVR_HH8_LDI_NEG
, R_AVR_HH8_LDI_NEG
},
544 { BFD_RELOC_AVR_MS8_LDI_NEG
, R_AVR_MS8_LDI_NEG
},
545 { BFD_RELOC_AVR_LO8_LDI_PM
, R_AVR_LO8_LDI_PM
},
546 { BFD_RELOC_AVR_LO8_LDI_GS
, R_AVR_LO8_LDI_GS
},
547 { BFD_RELOC_AVR_HI8_LDI_PM
, R_AVR_HI8_LDI_PM
},
548 { BFD_RELOC_AVR_HI8_LDI_GS
, R_AVR_HI8_LDI_GS
},
549 { BFD_RELOC_AVR_HH8_LDI_PM
, R_AVR_HH8_LDI_PM
},
550 { BFD_RELOC_AVR_LO8_LDI_PM_NEG
, R_AVR_LO8_LDI_PM_NEG
},
551 { BFD_RELOC_AVR_HI8_LDI_PM_NEG
, R_AVR_HI8_LDI_PM_NEG
},
552 { BFD_RELOC_AVR_HH8_LDI_PM_NEG
, R_AVR_HH8_LDI_PM_NEG
},
553 { BFD_RELOC_AVR_CALL
, R_AVR_CALL
},
554 { BFD_RELOC_AVR_LDI
, R_AVR_LDI
},
555 { BFD_RELOC_AVR_6
, R_AVR_6
},
556 { BFD_RELOC_AVR_6_ADIW
, R_AVR_6_ADIW
},
557 { BFD_RELOC_8
, R_AVR_8
}
560 /* Meant to be filled one day with the wrap around address for the
561 specific device. I.e. should get the value 0x4000 for 16k devices,
562 0x8000 for 32k devices and so on.
564 We initialize it here with a value of 0x1000000 resulting in
565 that we will never suggest a wrap-around jump during relaxation.
566 The logic of the source code later on assumes that in
567 avr_pc_wrap_around one single bit is set. */
568 static bfd_vma avr_pc_wrap_around
= 0x10000000;
570 /* If this variable holds a value different from zero, the linker relaxation
571 machine will try to optimize call/ret sequences by a single jump
572 instruction. This option could be switched off by a linker switch. */
573 static int avr_replace_call_ret_sequences
= 1;
575 /* Initialize an entry in the stub hash table. */
577 static struct bfd_hash_entry
*
578 stub_hash_newfunc (struct bfd_hash_entry
*entry
,
579 struct bfd_hash_table
*table
,
582 /* Allocate the structure if it has not already been allocated by a
586 entry
= bfd_hash_allocate (table
,
587 sizeof (struct elf32_avr_stub_hash_entry
));
592 /* Call the allocation method of the superclass. */
593 entry
= bfd_hash_newfunc (entry
, table
, string
);
596 struct elf32_avr_stub_hash_entry
*hsh
;
598 /* Initialize the local fields. */
599 hsh
= avr_stub_hash_entry (entry
);
600 hsh
->stub_offset
= 0;
601 hsh
->target_value
= 0;
607 /* This function is just a straight passthrough to the real
608 function in linker.c. Its prupose is so that its address
609 can be compared inside the avr_link_hash_table macro. */
611 static struct bfd_hash_entry
*
612 elf32_avr_link_hash_newfunc (struct bfd_hash_entry
* entry
,
613 struct bfd_hash_table
* table
,
616 return _bfd_elf_link_hash_newfunc (entry
, table
, string
);
619 /* Create the derived linker hash table. The AVR ELF port uses the derived
620 hash table to keep information specific to the AVR ELF linker (without
621 using static variables). */
623 static struct bfd_link_hash_table
*
624 elf32_avr_link_hash_table_create (bfd
*abfd
)
626 struct elf32_avr_link_hash_table
*htab
;
627 bfd_size_type amt
= sizeof (*htab
);
629 htab
= bfd_malloc (amt
);
633 if (!_bfd_elf_link_hash_table_init (&htab
->etab
, abfd
,
634 elf32_avr_link_hash_newfunc
,
635 sizeof (struct elf_link_hash_entry
),
642 /* Init the stub hash table too. */
643 if (!bfd_hash_table_init (&htab
->bstab
, stub_hash_newfunc
,
644 sizeof (struct elf32_avr_stub_hash_entry
)))
647 htab
->stub_bfd
= NULL
;
648 htab
->stub_sec
= NULL
;
650 /* Initialize the address mapping table. */
651 htab
->amt_stub_offsets
= NULL
;
652 htab
->amt_destination_addr
= NULL
;
653 htab
->amt_entry_cnt
= 0;
654 htab
->amt_max_entry_cnt
= 0;
656 return &htab
->etab
.root
;
659 /* Free the derived linker hash table. */
662 elf32_avr_link_hash_table_free (struct bfd_link_hash_table
*btab
)
664 struct elf32_avr_link_hash_table
*htab
665 = (struct elf32_avr_link_hash_table
*) btab
;
667 /* Free the address mapping table. */
668 if (htab
->amt_stub_offsets
!= NULL
)
669 free (htab
->amt_stub_offsets
);
670 if (htab
->amt_destination_addr
!= NULL
)
671 free (htab
->amt_destination_addr
);
673 bfd_hash_table_free (&htab
->bstab
);
674 _bfd_generic_link_hash_table_free (btab
);
677 /* Calculates the effective distance of a pc relative jump/call. */
680 avr_relative_distance_considering_wrap_around (unsigned int distance
)
682 unsigned int wrap_around_mask
= avr_pc_wrap_around
- 1;
683 int dist_with_wrap_around
= distance
& wrap_around_mask
;
685 if (dist_with_wrap_around
> ((int) (avr_pc_wrap_around
>> 1)))
686 dist_with_wrap_around
-= avr_pc_wrap_around
;
688 return dist_with_wrap_around
;
692 static reloc_howto_type
*
693 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
694 bfd_reloc_code_real_type code
)
699 i
< sizeof (avr_reloc_map
) / sizeof (struct avr_reloc_map
);
701 if (avr_reloc_map
[i
].bfd_reloc_val
== code
)
702 return &elf_avr_howto_table
[avr_reloc_map
[i
].elf_reloc_val
];
707 static reloc_howto_type
*
708 bfd_elf32_bfd_reloc_name_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
714 i
< sizeof (elf_avr_howto_table
) / sizeof (elf_avr_howto_table
[0]);
716 if (elf_avr_howto_table
[i
].name
!= NULL
717 && strcasecmp (elf_avr_howto_table
[i
].name
, r_name
) == 0)
718 return &elf_avr_howto_table
[i
];
723 /* Set the howto pointer for an AVR ELF reloc. */
726 avr_info_to_howto_rela (bfd
*abfd ATTRIBUTE_UNUSED
,
728 Elf_Internal_Rela
*dst
)
732 r_type
= ELF32_R_TYPE (dst
->r_info
);
733 BFD_ASSERT (r_type
< (unsigned int) R_AVR_max
);
734 cache_ptr
->howto
= &elf_avr_howto_table
[r_type
];
737 /* Look through the relocs for a section during the first phase.
738 Since we don't do .gots or .plts, we just need to consider the
739 virtual table relocs for gc. */
742 elf32_avr_check_relocs (bfd
*abfd
,
743 struct bfd_link_info
*info
,
745 const Elf_Internal_Rela
*relocs
)
747 Elf_Internal_Shdr
*symtab_hdr
;
748 struct elf_link_hash_entry
**sym_hashes
;
749 const Elf_Internal_Rela
*rel
;
750 const Elf_Internal_Rela
*rel_end
;
752 if (info
->relocatable
)
755 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
756 sym_hashes
= elf_sym_hashes (abfd
);
758 rel_end
= relocs
+ sec
->reloc_count
;
759 for (rel
= relocs
; rel
< rel_end
; rel
++)
761 struct elf_link_hash_entry
*h
;
762 unsigned long r_symndx
;
764 r_symndx
= ELF32_R_SYM (rel
->r_info
);
765 if (r_symndx
< symtab_hdr
->sh_info
)
769 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
770 while (h
->root
.type
== bfd_link_hash_indirect
771 || h
->root
.type
== bfd_link_hash_warning
)
772 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
780 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation
)
782 return (relocation
>= 0x020000);
785 /* Returns the address of the corresponding stub if there is one.
786 Returns otherwise an address above 0x020000. This function
787 could also be used, if there is no knowledge on the section where
788 the destination is found. */
791 avr_get_stub_addr (bfd_vma srel
,
792 struct elf32_avr_link_hash_table
*htab
)
795 bfd_vma stub_sec_addr
=
796 (htab
->stub_sec
->output_section
->vma
+
797 htab
->stub_sec
->output_offset
);
799 for (sindex
= 0; sindex
< htab
->amt_max_entry_cnt
; sindex
++)
800 if (htab
->amt_destination_addr
[sindex
] == srel
)
801 return htab
->amt_stub_offsets
[sindex
] + stub_sec_addr
;
803 /* Return an address that could not be reached by 16 bit relocs. */
807 /* Perform a single relocation. By default we use the standard BFD
808 routines, but a few relocs, we have to do them ourselves. */
810 static bfd_reloc_status_type
811 avr_final_link_relocate (reloc_howto_type
* howto
,
813 asection
* input_section
,
815 Elf_Internal_Rela
* rel
,
817 struct elf32_avr_link_hash_table
* htab
)
819 bfd_reloc_status_type r
= bfd_reloc_ok
;
822 bfd_signed_vma reloc_addr
;
823 bfd_boolean use_stubs
= FALSE
;
824 /* Usually is 0, unless we are generating code for a bootloader. */
825 bfd_signed_vma base_addr
= htab
->vector_base
;
827 /* Absolute addr of the reloc in the final excecutable. */
828 reloc_addr
= rel
->r_offset
+ input_section
->output_section
->vma
829 + input_section
->output_offset
;
834 contents
+= rel
->r_offset
;
835 srel
= (bfd_signed_vma
) relocation
;
836 srel
+= rel
->r_addend
;
837 srel
-= rel
->r_offset
;
838 srel
-= 2; /* Branch instructions add 2 to the PC... */
839 srel
-= (input_section
->output_section
->vma
+
840 input_section
->output_offset
);
843 return bfd_reloc_outofrange
;
844 if (srel
> ((1 << 7) - 1) || (srel
< - (1 << 7)))
845 return bfd_reloc_overflow
;
846 x
= bfd_get_16 (input_bfd
, contents
);
847 x
= (x
& 0xfc07) | (((srel
>> 1) << 3) & 0x3f8);
848 bfd_put_16 (input_bfd
, x
, contents
);
852 contents
+= rel
->r_offset
;
853 srel
= (bfd_signed_vma
) relocation
;
854 srel
+= rel
->r_addend
;
855 srel
-= rel
->r_offset
;
856 srel
-= 2; /* Branch instructions add 2 to the PC... */
857 srel
-= (input_section
->output_section
->vma
+
858 input_section
->output_offset
);
861 return bfd_reloc_outofrange
;
863 srel
= avr_relative_distance_considering_wrap_around (srel
);
865 /* AVR addresses commands as words. */
868 /* Check for overflow. */
869 if (srel
< -2048 || srel
> 2047)
871 /* Relative distance is too large. */
873 /* Always apply WRAPAROUND for avr2, avr25, and avr4. */
874 switch (bfd_get_mach (input_bfd
))
882 return bfd_reloc_overflow
;
886 x
= bfd_get_16 (input_bfd
, contents
);
887 x
= (x
& 0xf000) | (srel
& 0xfff);
888 bfd_put_16 (input_bfd
, x
, contents
);
892 contents
+= rel
->r_offset
;
893 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
894 x
= bfd_get_16 (input_bfd
, contents
);
895 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
896 bfd_put_16 (input_bfd
, x
, contents
);
900 contents
+= rel
->r_offset
;
901 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
902 if (((srel
> 0) && (srel
& 0xffff) > 255)
903 || ((srel
< 0) && ((-srel
) & 0xffff) > 128))
904 /* Remove offset for data/eeprom section. */
905 return bfd_reloc_overflow
;
907 x
= bfd_get_16 (input_bfd
, contents
);
908 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
909 bfd_put_16 (input_bfd
, x
, contents
);
913 contents
+= rel
->r_offset
;
914 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
915 if (((srel
& 0xffff) > 63) || (srel
< 0))
916 /* Remove offset for data/eeprom section. */
917 return bfd_reloc_overflow
;
918 x
= bfd_get_16 (input_bfd
, contents
);
919 x
= (x
& 0xd3f8) | ((srel
& 7) | ((srel
& (3 << 3)) << 7)
920 | ((srel
& (1 << 5)) << 8));
921 bfd_put_16 (input_bfd
, x
, contents
);
925 contents
+= rel
->r_offset
;
926 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
927 if (((srel
& 0xffff) > 63) || (srel
< 0))
928 /* Remove offset for data/eeprom section. */
929 return bfd_reloc_overflow
;
930 x
= bfd_get_16 (input_bfd
, contents
);
931 x
= (x
& 0xff30) | (srel
& 0xf) | ((srel
& 0x30) << 2);
932 bfd_put_16 (input_bfd
, x
, contents
);
936 contents
+= rel
->r_offset
;
937 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
938 srel
= (srel
>> 8) & 0xff;
939 x
= bfd_get_16 (input_bfd
, contents
);
940 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
941 bfd_put_16 (input_bfd
, x
, contents
);
945 contents
+= rel
->r_offset
;
946 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
947 srel
= (srel
>> 16) & 0xff;
948 x
= bfd_get_16 (input_bfd
, contents
);
949 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
950 bfd_put_16 (input_bfd
, x
, contents
);
954 contents
+= rel
->r_offset
;
955 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
956 srel
= (srel
>> 24) & 0xff;
957 x
= bfd_get_16 (input_bfd
, contents
);
958 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
959 bfd_put_16 (input_bfd
, x
, contents
);
962 case R_AVR_LO8_LDI_NEG
:
963 contents
+= rel
->r_offset
;
964 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
966 x
= bfd_get_16 (input_bfd
, contents
);
967 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
968 bfd_put_16 (input_bfd
, x
, contents
);
971 case R_AVR_HI8_LDI_NEG
:
972 contents
+= rel
->r_offset
;
973 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
975 srel
= (srel
>> 8) & 0xff;
976 x
= bfd_get_16 (input_bfd
, contents
);
977 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
978 bfd_put_16 (input_bfd
, x
, contents
);
981 case R_AVR_HH8_LDI_NEG
:
982 contents
+= rel
->r_offset
;
983 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
985 srel
= (srel
>> 16) & 0xff;
986 x
= bfd_get_16 (input_bfd
, contents
);
987 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
988 bfd_put_16 (input_bfd
, x
, contents
);
991 case R_AVR_MS8_LDI_NEG
:
992 contents
+= rel
->r_offset
;
993 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
995 srel
= (srel
>> 24) & 0xff;
996 x
= bfd_get_16 (input_bfd
, contents
);
997 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
998 bfd_put_16 (input_bfd
, x
, contents
);
1001 case R_AVR_LO8_LDI_GS
:
1002 use_stubs
= (!htab
->no_stubs
);
1004 case R_AVR_LO8_LDI_PM
:
1005 contents
+= rel
->r_offset
;
1006 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1009 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1011 bfd_vma old_srel
= srel
;
1013 /* We need to use the address of the stub instead. */
1014 srel
= avr_get_stub_addr (srel
, htab
);
1016 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1017 "reloc at address 0x%x.\n",
1018 (unsigned int) srel
,
1019 (unsigned int) old_srel
,
1020 (unsigned int) reloc_addr
);
1022 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1023 return bfd_reloc_outofrange
;
1027 return bfd_reloc_outofrange
;
1029 x
= bfd_get_16 (input_bfd
, contents
);
1030 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1031 bfd_put_16 (input_bfd
, x
, contents
);
1034 case R_AVR_HI8_LDI_GS
:
1035 use_stubs
= (!htab
->no_stubs
);
1037 case R_AVR_HI8_LDI_PM
:
1038 contents
+= rel
->r_offset
;
1039 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1042 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1044 bfd_vma old_srel
= srel
;
1046 /* We need to use the address of the stub instead. */
1047 srel
= avr_get_stub_addr (srel
, htab
);
1049 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1050 "reloc at address 0x%x.\n",
1051 (unsigned int) srel
,
1052 (unsigned int) old_srel
,
1053 (unsigned int) reloc_addr
);
1055 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1056 return bfd_reloc_outofrange
;
1060 return bfd_reloc_outofrange
;
1062 srel
= (srel
>> 8) & 0xff;
1063 x
= bfd_get_16 (input_bfd
, contents
);
1064 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1065 bfd_put_16 (input_bfd
, x
, contents
);
1068 case R_AVR_HH8_LDI_PM
:
1069 contents
+= rel
->r_offset
;
1070 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1072 return bfd_reloc_outofrange
;
1074 srel
= (srel
>> 16) & 0xff;
1075 x
= bfd_get_16 (input_bfd
, contents
);
1076 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1077 bfd_put_16 (input_bfd
, x
, contents
);
1080 case R_AVR_LO8_LDI_PM_NEG
:
1081 contents
+= rel
->r_offset
;
1082 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1085 return bfd_reloc_outofrange
;
1087 x
= bfd_get_16 (input_bfd
, contents
);
1088 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1089 bfd_put_16 (input_bfd
, x
, contents
);
1092 case R_AVR_HI8_LDI_PM_NEG
:
1093 contents
+= rel
->r_offset
;
1094 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1097 return bfd_reloc_outofrange
;
1099 srel
= (srel
>> 8) & 0xff;
1100 x
= bfd_get_16 (input_bfd
, contents
);
1101 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1102 bfd_put_16 (input_bfd
, x
, contents
);
1105 case R_AVR_HH8_LDI_PM_NEG
:
1106 contents
+= rel
->r_offset
;
1107 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1110 return bfd_reloc_outofrange
;
1112 srel
= (srel
>> 16) & 0xff;
1113 x
= bfd_get_16 (input_bfd
, contents
);
1114 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
1115 bfd_put_16 (input_bfd
, x
, contents
);
1119 contents
+= rel
->r_offset
;
1120 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1122 return bfd_reloc_outofrange
;
1124 x
= bfd_get_16 (input_bfd
, contents
);
1125 x
|= ((srel
& 0x10000) | ((srel
<< 3) & 0x1f00000)) >> 16;
1126 bfd_put_16 (input_bfd
, x
, contents
);
1127 bfd_put_16 (input_bfd
, (bfd_vma
) srel
& 0xffff, contents
+2);
1131 use_stubs
= (!htab
->no_stubs
);
1132 contents
+= rel
->r_offset
;
1133 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
1136 && avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1138 bfd_vma old_srel
= srel
;
1140 /* We need to use the address of the stub instead. */
1141 srel
= avr_get_stub_addr (srel
,htab
);
1143 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1144 "reloc at address 0x%x.\n",
1145 (unsigned int) srel
,
1146 (unsigned int) old_srel
,
1147 (unsigned int) reloc_addr
);
1149 if (avr_stub_is_required_for_16_bit_reloc (srel
- base_addr
))
1150 return bfd_reloc_outofrange
;
1154 return bfd_reloc_outofrange
;
1156 bfd_put_16 (input_bfd
, (bfd_vma
) srel
&0x00ffff, contents
);
1160 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1161 contents
, rel
->r_offset
,
1162 relocation
, rel
->r_addend
);
1168 /* Relocate an AVR ELF section. */
1171 elf32_avr_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
1172 struct bfd_link_info
*info
,
1174 asection
*input_section
,
1176 Elf_Internal_Rela
*relocs
,
1177 Elf_Internal_Sym
*local_syms
,
1178 asection
**local_sections
)
1180 Elf_Internal_Shdr
* symtab_hdr
;
1181 struct elf_link_hash_entry
** sym_hashes
;
1182 Elf_Internal_Rela
* rel
;
1183 Elf_Internal_Rela
* relend
;
1184 struct elf32_avr_link_hash_table
* htab
= avr_link_hash_table (info
);
1189 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
1190 sym_hashes
= elf_sym_hashes (input_bfd
);
1191 relend
= relocs
+ input_section
->reloc_count
;
1193 for (rel
= relocs
; rel
< relend
; rel
++)
1195 reloc_howto_type
* howto
;
1196 unsigned long r_symndx
;
1197 Elf_Internal_Sym
* sym
;
1199 struct elf_link_hash_entry
* h
;
1201 bfd_reloc_status_type r
;
1205 r_type
= ELF32_R_TYPE (rel
->r_info
);
1206 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1207 howto
= elf_avr_howto_table
+ r_type
;
1212 if (r_symndx
< symtab_hdr
->sh_info
)
1214 sym
= local_syms
+ r_symndx
;
1215 sec
= local_sections
[r_symndx
];
1216 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
1218 name
= bfd_elf_string_from_elf_section
1219 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
1220 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
1224 bfd_boolean unresolved_reloc
, warned
;
1226 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
1227 r_symndx
, symtab_hdr
, sym_hashes
,
1229 unresolved_reloc
, warned
);
1231 name
= h
->root
.root
.string
;
1234 if (sec
!= NULL
&& elf_discarded_section (sec
))
1235 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1236 rel
, relend
, howto
, contents
);
1238 if (info
->relocatable
)
1241 r
= avr_final_link_relocate (howto
, input_bfd
, input_section
,
1242 contents
, rel
, relocation
, htab
);
1244 if (r
!= bfd_reloc_ok
)
1246 const char * msg
= (const char *) NULL
;
1250 case bfd_reloc_overflow
:
1251 r
= info
->callbacks
->reloc_overflow
1252 (info
, (h
? &h
->root
: NULL
),
1253 name
, howto
->name
, (bfd_vma
) 0,
1254 input_bfd
, input_section
, rel
->r_offset
);
1257 case bfd_reloc_undefined
:
1258 r
= info
->callbacks
->undefined_symbol
1259 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1262 case bfd_reloc_outofrange
:
1263 msg
= _("internal error: out of range error");
1266 case bfd_reloc_notsupported
:
1267 msg
= _("internal error: unsupported relocation error");
1270 case bfd_reloc_dangerous
:
1271 msg
= _("internal error: dangerous relocation");
1275 msg
= _("internal error: unknown error");
1280 r
= info
->callbacks
->warning
1281 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
1291 /* The final processing done just before writing out a AVR ELF object
1292 file. This gets the AVR architecture right based on the machine
1296 bfd_elf_avr_final_write_processing (bfd
*abfd
,
1297 bfd_boolean linker ATTRIBUTE_UNUSED
)
1301 switch (bfd_get_mach (abfd
))
1305 val
= E_AVR_MACH_AVR2
;
1309 val
= E_AVR_MACH_AVR1
;
1312 case bfd_mach_avr25
:
1313 val
= E_AVR_MACH_AVR25
;
1317 val
= E_AVR_MACH_AVR3
;
1320 case bfd_mach_avr31
:
1321 val
= E_AVR_MACH_AVR31
;
1324 case bfd_mach_avr35
:
1325 val
= E_AVR_MACH_AVR35
;
1329 val
= E_AVR_MACH_AVR4
;
1333 val
= E_AVR_MACH_AVR5
;
1336 case bfd_mach_avr51
:
1337 val
= E_AVR_MACH_AVR51
;
1341 val
= E_AVR_MACH_AVR6
;
1345 elf_elfheader (abfd
)->e_machine
= EM_AVR
;
1346 elf_elfheader (abfd
)->e_flags
&= ~ EF_AVR_MACH
;
1347 elf_elfheader (abfd
)->e_flags
|= val
;
1348 elf_elfheader (abfd
)->e_flags
|= EF_AVR_LINKRELAX_PREPARED
;
1351 /* Set the right machine number. */
1354 elf32_avr_object_p (bfd
*abfd
)
1356 unsigned int e_set
= bfd_mach_avr2
;
1358 if (elf_elfheader (abfd
)->e_machine
== EM_AVR
1359 || elf_elfheader (abfd
)->e_machine
== EM_AVR_OLD
)
1361 int e_mach
= elf_elfheader (abfd
)->e_flags
& EF_AVR_MACH
;
1366 case E_AVR_MACH_AVR2
:
1367 e_set
= bfd_mach_avr2
;
1370 case E_AVR_MACH_AVR1
:
1371 e_set
= bfd_mach_avr1
;
1374 case E_AVR_MACH_AVR25
:
1375 e_set
= bfd_mach_avr25
;
1378 case E_AVR_MACH_AVR3
:
1379 e_set
= bfd_mach_avr3
;
1382 case E_AVR_MACH_AVR31
:
1383 e_set
= bfd_mach_avr31
;
1386 case E_AVR_MACH_AVR35
:
1387 e_set
= bfd_mach_avr35
;
1390 case E_AVR_MACH_AVR4
:
1391 e_set
= bfd_mach_avr4
;
1394 case E_AVR_MACH_AVR5
:
1395 e_set
= bfd_mach_avr5
;
1398 case E_AVR_MACH_AVR51
:
1399 e_set
= bfd_mach_avr51
;
1402 case E_AVR_MACH_AVR6
:
1403 e_set
= bfd_mach_avr6
;
1407 return bfd_default_set_arch_mach (abfd
, bfd_arch_avr
,
1412 /* Delete some bytes from a section while changing the size of an instruction.
1413 The parameter "addr" denotes the section-relative offset pointing just
1414 behind the shrinked instruction. "addr+count" point at the first
1415 byte just behind the original unshrinked instruction. */
1418 elf32_avr_relax_delete_bytes (bfd
*abfd
,
1423 Elf_Internal_Shdr
*symtab_hdr
;
1424 unsigned int sec_shndx
;
1426 Elf_Internal_Rela
*irel
, *irelend
;
1427 Elf_Internal_Sym
*isym
;
1428 Elf_Internal_Sym
*isymbuf
= NULL
;
1430 struct elf_link_hash_entry
**sym_hashes
;
1431 struct elf_link_hash_entry
**end_hashes
;
1432 unsigned int symcount
;
1434 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1435 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1436 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1440 irel
= elf_section_data (sec
)->relocs
;
1441 irelend
= irel
+ sec
->reloc_count
;
1443 /* Actually delete the bytes. */
1444 if (toaddr
- addr
- count
> 0)
1445 memmove (contents
+ addr
, contents
+ addr
+ count
,
1446 (size_t) (toaddr
- addr
- count
));
1449 /* Adjust all the reloc addresses. */
1450 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1452 bfd_vma old_reloc_address
;
1454 old_reloc_address
= (sec
->output_section
->vma
1455 + sec
->output_offset
+ irel
->r_offset
);
1457 /* Get the new reloc address. */
1458 if ((irel
->r_offset
> addr
1459 && irel
->r_offset
< toaddr
))
1462 printf ("Relocation at address 0x%x needs to be moved.\n"
1463 "Old section offset: 0x%x, New section offset: 0x%x \n",
1464 (unsigned int) old_reloc_address
,
1465 (unsigned int) irel
->r_offset
,
1466 (unsigned int) ((irel
->r_offset
) - count
));
1468 irel
->r_offset
-= count
;
1473 /* The reloc's own addresses are now ok. However, we need to readjust
1474 the reloc's addend, i.e. the reloc's value if two conditions are met:
1475 1.) the reloc is relative to a symbol in this section that
1476 is located in front of the shrinked instruction
1477 2.) symbol plus addend end up behind the shrinked instruction.
1479 The most common case where this happens are relocs relative to
1480 the section-start symbol.
1482 This step needs to be done for all of the sections of the bfd. */
1485 struct bfd_section
*isec
;
1487 for (isec
= abfd
->sections
; isec
; isec
= isec
->next
)
1490 bfd_vma shrinked_insn_address
;
1492 shrinked_insn_address
= (sec
->output_section
->vma
1493 + sec
->output_offset
+ addr
- count
);
1495 irelend
= elf_section_data (isec
)->relocs
+ isec
->reloc_count
;
1496 for (irel
= elf_section_data (isec
)->relocs
;
1500 /* Read this BFD's local symbols if we haven't done
1502 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1504 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1505 if (isymbuf
== NULL
)
1506 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1507 symtab_hdr
->sh_info
, 0,
1509 if (isymbuf
== NULL
)
1513 /* Get the value of the symbol referred to by the reloc. */
1514 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1516 /* A local symbol. */
1519 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1520 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1521 symval
= isym
->st_value
;
1522 /* If the reloc is absolute, it will not have
1523 a symbol or section associated with it. */
1526 symval
+= sym_sec
->output_section
->vma
1527 + sym_sec
->output_offset
;
1530 printf ("Checking if the relocation's "
1531 "addend needs corrections.\n"
1532 "Address of anchor symbol: 0x%x \n"
1533 "Address of relocation target: 0x%x \n"
1534 "Address of relaxed insn: 0x%x \n",
1535 (unsigned int) symval
,
1536 (unsigned int) (symval
+ irel
->r_addend
),
1537 (unsigned int) shrinked_insn_address
);
1539 if (symval
<= shrinked_insn_address
1540 && (symval
+ irel
->r_addend
) > shrinked_insn_address
)
1542 irel
->r_addend
-= count
;
1545 printf ("Relocation's addend needed to be fixed \n");
1548 /* else...Reference symbol is absolute. No adjustment needed. */
1550 /* else...Reference symbol is extern. No need for adjusting
1556 /* Adjust the local symbols defined in this section. */
1557 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1558 /* Fix PR 9841, there may be no local symbols. */
1561 Elf_Internal_Sym
*isymend
;
1563 isymend
= isym
+ symtab_hdr
->sh_info
;
1564 for (; isym
< isymend
; isym
++)
1566 if (isym
->st_shndx
== sec_shndx
1567 && isym
->st_value
> addr
1568 && isym
->st_value
< toaddr
)
1569 isym
->st_value
-= count
;
1573 /* Now adjust the global symbols defined in this section. */
1574 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1575 - symtab_hdr
->sh_info
);
1576 sym_hashes
= elf_sym_hashes (abfd
);
1577 end_hashes
= sym_hashes
+ symcount
;
1578 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1580 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1581 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1582 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1583 && sym_hash
->root
.u
.def
.section
== sec
1584 && sym_hash
->root
.u
.def
.value
> addr
1585 && sym_hash
->root
.u
.def
.value
< toaddr
)
1587 sym_hash
->root
.u
.def
.value
-= count
;
1594 /* This function handles relaxing for the avr.
1595 Many important relaxing opportunities within functions are already
1596 realized by the compiler itself.
1597 Here we try to replace call (4 bytes) -> rcall (2 bytes)
1598 and jump -> rjmp (safes also 2 bytes).
1599 As well we now optimize seqences of
1600 - call/rcall function
1605 . In case that within a sequence
1608 the ret could no longer be reached it is optimized away. In order
1609 to check if the ret is no longer needed, it is checked that the ret's address
1610 is not the target of a branch or jump within the same section, it is checked
1611 that there is no skip instruction before the jmp/rjmp and that there
1612 is no local or global label place at the address of the ret.
1614 We refrain from relaxing within sections ".vectors" and
1615 ".jumptables" in order to maintain the position of the instructions.
1616 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
1617 if possible. (In future one could possibly use the space of the nop
1618 for the first instruction of the irq service function.
1620 The .jumptables sections is meant to be used for a future tablejump variant
1621 for the devices with 3-byte program counter where the table itself
1622 contains 4-byte jump instructions whose relative offset must not
1626 elf32_avr_relax_section (bfd
*abfd
,
1628 struct bfd_link_info
*link_info
,
1631 Elf_Internal_Shdr
*symtab_hdr
;
1632 Elf_Internal_Rela
*internal_relocs
;
1633 Elf_Internal_Rela
*irel
, *irelend
;
1634 bfd_byte
*contents
= NULL
;
1635 Elf_Internal_Sym
*isymbuf
= NULL
;
1636 struct elf32_avr_link_hash_table
*htab
;
1638 if (link_info
->relocatable
)
1639 (*link_info
->callbacks
->einfo
)
1640 (_("%P%F: --relax and -r may not be used together\n"));
1642 htab
= avr_link_hash_table (link_info
);
1646 /* Assume nothing changes. */
1649 if ((!htab
->no_stubs
) && (sec
== htab
->stub_sec
))
1651 /* We are just relaxing the stub section.
1652 Let's calculate the size needed again. */
1653 bfd_size_type last_estimated_stub_section_size
= htab
->stub_sec
->size
;
1656 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
1657 (int) last_estimated_stub_section_size
);
1659 elf32_avr_size_stubs (htab
->stub_sec
->output_section
->owner
,
1662 /* Check if the number of trampolines changed. */
1663 if (last_estimated_stub_section_size
!= htab
->stub_sec
->size
)
1667 printf ("Size of stub section after this pass: %i\n",
1668 (int) htab
->stub_sec
->size
);
1673 /* We don't have to do anything for a relocatable link, if
1674 this section does not have relocs, or if this is not a
1676 if (link_info
->relocatable
1677 || (sec
->flags
& SEC_RELOC
) == 0
1678 || sec
->reloc_count
== 0
1679 || (sec
->flags
& SEC_CODE
) == 0)
1682 /* Check if the object file to relax uses internal symbols so that we
1683 could fix up the relocations. */
1684 if (!(elf_elfheader (abfd
)->e_flags
& EF_AVR_LINKRELAX_PREPARED
))
1687 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1689 /* Get a copy of the native relocations. */
1690 internal_relocs
= (_bfd_elf_link_read_relocs
1691 (abfd
, sec
, NULL
, NULL
, link_info
->keep_memory
));
1692 if (internal_relocs
== NULL
)
1695 /* Walk through the relocs looking for relaxing opportunities. */
1696 irelend
= internal_relocs
+ sec
->reloc_count
;
1697 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1701 if ( ELF32_R_TYPE (irel
->r_info
) != R_AVR_13_PCREL
1702 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_7_PCREL
1703 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_CALL
)
1706 /* Get the section contents if we haven't done so already. */
1707 if (contents
== NULL
)
1709 /* Get cached copy if it exists. */
1710 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1711 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1714 /* Go get them off disk. */
1715 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1720 /* Read this BFD's local symbols if we haven't done so already. */
1721 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1723 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1724 if (isymbuf
== NULL
)
1725 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1726 symtab_hdr
->sh_info
, 0,
1728 if (isymbuf
== NULL
)
1733 /* Get the value of the symbol referred to by the reloc. */
1734 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1736 /* A local symbol. */
1737 Elf_Internal_Sym
*isym
;
1740 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1741 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1742 symval
= isym
->st_value
;
1743 /* If the reloc is absolute, it will not have
1744 a symbol or section associated with it. */
1746 symval
+= sym_sec
->output_section
->vma
1747 + sym_sec
->output_offset
;
1752 struct elf_link_hash_entry
*h
;
1754 /* An external symbol. */
1755 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
1756 h
= elf_sym_hashes (abfd
)[indx
];
1757 BFD_ASSERT (h
!= NULL
);
1758 if (h
->root
.type
!= bfd_link_hash_defined
1759 && h
->root
.type
!= bfd_link_hash_defweak
)
1760 /* This appears to be a reference to an undefined
1761 symbol. Just ignore it--it will be caught by the
1762 regular reloc processing. */
1765 symval
= (h
->root
.u
.def
.value
1766 + h
->root
.u
.def
.section
->output_section
->vma
1767 + h
->root
.u
.def
.section
->output_offset
);
1770 /* For simplicity of coding, we are going to modify the section
1771 contents, the section relocs, and the BFD symbol table. We
1772 must tell the rest of the code not to free up this
1773 information. It would be possible to instead create a table
1774 of changes which have to be made, as is done in coff-mips.c;
1775 that would be more work, but would require less memory when
1776 the linker is run. */
1777 switch (ELF32_R_TYPE (irel
->r_info
))
1779 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1780 pc-relative rcall/rjmp. */
1783 bfd_vma value
= symval
+ irel
->r_addend
;
1785 int distance_short_enough
= 0;
1787 /* Get the address of this instruction. */
1788 dot
= (sec
->output_section
->vma
1789 + sec
->output_offset
+ irel
->r_offset
);
1791 /* Compute the distance from this insn to the branch target. */
1794 /* If the distance is within -4094..+4098 inclusive, then we can
1795 relax this jump/call. +4098 because the call/jump target
1796 will be closer after the relaxation. */
1797 if ((int) gap
>= -4094 && (int) gap
<= 4098)
1798 distance_short_enough
= 1;
1800 /* Here we handle the wrap-around case. E.g. for a 16k device
1801 we could use a rjmp to jump from address 0x100 to 0x3d00!
1802 In order to make this work properly, we need to fill the
1803 vaiable avr_pc_wrap_around with the appropriate value.
1804 I.e. 0x4000 for a 16k device. */
1806 /* Shrinking the code size makes the gaps larger in the
1807 case of wrap-arounds. So we use a heuristical safety
1808 margin to avoid that during relax the distance gets
1809 again too large for the short jumps. Let's assume
1810 a typical code-size reduction due to relax for a
1811 16k device of 600 bytes. So let's use twice the
1812 typical value as safety margin. */
1816 int assumed_shrink
= 600;
1817 if (avr_pc_wrap_around
> 0x4000)
1818 assumed_shrink
= 900;
1820 safety_margin
= 2 * assumed_shrink
;
1822 rgap
= avr_relative_distance_considering_wrap_around (gap
);
1824 if (rgap
>= (-4092 + safety_margin
)
1825 && rgap
<= (4094 - safety_margin
))
1826 distance_short_enough
= 1;
1829 if (distance_short_enough
)
1831 unsigned char code_msb
;
1832 unsigned char code_lsb
;
1835 printf ("shrinking jump/call instruction at address 0x%x"
1836 " in section %s\n\n",
1837 (int) dot
, sec
->name
);
1839 /* Note that we've changed the relocs, section contents,
1841 elf_section_data (sec
)->relocs
= internal_relocs
;
1842 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1843 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1845 /* Get the instruction code for relaxing. */
1846 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
);
1847 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1849 /* Mask out the relocation bits. */
1852 if (code_msb
== 0x94 && code_lsb
== 0x0E)
1854 /* we are changing call -> rcall . */
1855 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
1856 bfd_put_8 (abfd
, 0xD0, contents
+ irel
->r_offset
+ 1);
1858 else if (code_msb
== 0x94 && code_lsb
== 0x0C)
1860 /* we are changeing jump -> rjmp. */
1861 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
1862 bfd_put_8 (abfd
, 0xC0, contents
+ irel
->r_offset
+ 1);
1867 /* Fix the relocation's type. */
1868 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1871 /* Check for the vector section. There we don't want to
1872 modify the ordering! */
1874 if (!strcmp (sec
->name
,".vectors")
1875 || !strcmp (sec
->name
,".jumptables"))
1877 /* Let's insert a nop. */
1878 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 2);
1879 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 3);
1883 /* Delete two bytes of data. */
1884 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
1885 irel
->r_offset
+ 2, 2))
1888 /* That will change things, so, we should relax again.
1889 Note that this is not required, and it may be slow. */
1897 unsigned char code_msb
;
1898 unsigned char code_lsb
;
1901 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1902 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 0);
1904 /* Get the address of this instruction. */
1905 dot
= (sec
->output_section
->vma
1906 + sec
->output_offset
+ irel
->r_offset
);
1908 /* Here we look for rcall/ret or call/ret sequences that could be
1909 safely replaced by rjmp/ret or jmp/ret. */
1910 if (((code_msb
& 0xf0) == 0xd0)
1911 && avr_replace_call_ret_sequences
)
1913 /* This insn is a rcall. */
1914 unsigned char next_insn_msb
= 0;
1915 unsigned char next_insn_lsb
= 0;
1917 if (irel
->r_offset
+ 3 < sec
->size
)
1920 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 3);
1922 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 2);
1925 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1927 /* The next insn is a ret. We now convert the rcall insn
1928 into a rjmp instruction. */
1930 bfd_put_8 (abfd
, code_msb
, contents
+ irel
->r_offset
+ 1);
1932 printf ("converted rcall/ret sequence at address 0x%x"
1933 " into rjmp/ret sequence. Section is %s\n\n",
1934 (int) dot
, sec
->name
);
1939 else if ((0x94 == (code_msb
& 0xfe))
1940 && (0x0e == (code_lsb
& 0x0e))
1941 && avr_replace_call_ret_sequences
)
1943 /* This insn is a call. */
1944 unsigned char next_insn_msb
= 0;
1945 unsigned char next_insn_lsb
= 0;
1947 if (irel
->r_offset
+ 5 < sec
->size
)
1950 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 5);
1952 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 4);
1955 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1957 /* The next insn is a ret. We now convert the call insn
1958 into a jmp instruction. */
1961 bfd_put_8 (abfd
, code_lsb
, contents
+ irel
->r_offset
);
1963 printf ("converted call/ret sequence at address 0x%x"
1964 " into jmp/ret sequence. Section is %s\n\n",
1965 (int) dot
, sec
->name
);
1970 else if ((0xc0 == (code_msb
& 0xf0))
1971 || ((0x94 == (code_msb
& 0xfe))
1972 && (0x0c == (code_lsb
& 0x0e))))
1974 /* This insn is a rjmp or a jmp. */
1975 unsigned char next_insn_msb
= 0;
1976 unsigned char next_insn_lsb
= 0;
1979 if (0xc0 == (code_msb
& 0xf0))
1980 insn_size
= 2; /* rjmp insn */
1982 insn_size
= 4; /* jmp insn */
1984 if (irel
->r_offset
+ insn_size
+ 1 < sec
->size
)
1987 bfd_get_8 (abfd
, contents
+ irel
->r_offset
1990 bfd_get_8 (abfd
, contents
+ irel
->r_offset
1994 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1996 /* The next insn is a ret. We possibly could delete
1997 this ret. First we need to check for preceeding
1998 sbis/sbic/sbrs or cpse "skip" instructions. */
2000 int there_is_preceeding_non_skip_insn
= 1;
2001 bfd_vma address_of_ret
;
2003 address_of_ret
= dot
+ insn_size
;
2005 if (debug_relax
&& (insn_size
== 2))
2006 printf ("found rjmp / ret sequence at address 0x%x\n",
2008 if (debug_relax
&& (insn_size
== 4))
2009 printf ("found jmp / ret sequence at address 0x%x\n",
2012 /* We have to make sure that there is a preceeding insn. */
2013 if (irel
->r_offset
>= 2)
2015 unsigned char preceeding_msb
;
2016 unsigned char preceeding_lsb
;
2018 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2020 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2023 if (0x99 == preceeding_msb
)
2024 there_is_preceeding_non_skip_insn
= 0;
2027 if (0x9b == preceeding_msb
)
2028 there_is_preceeding_non_skip_insn
= 0;
2031 if ((0xfc == (preceeding_msb
& 0xfe)
2032 && (0x00 == (preceeding_lsb
& 0x08))))
2033 there_is_preceeding_non_skip_insn
= 0;
2036 if ((0xfe == (preceeding_msb
& 0xfe)
2037 && (0x00 == (preceeding_lsb
& 0x08))))
2038 there_is_preceeding_non_skip_insn
= 0;
2041 if (0x10 == (preceeding_msb
& 0xfc))
2042 there_is_preceeding_non_skip_insn
= 0;
2044 if (there_is_preceeding_non_skip_insn
== 0)
2046 printf ("preceeding skip insn prevents deletion of"
2047 " ret insn at addr 0x%x in section %s\n",
2048 (int) dot
+ 2, sec
->name
);
2052 /* There is no previous instruction. */
2053 there_is_preceeding_non_skip_insn
= 0;
2056 if (there_is_preceeding_non_skip_insn
)
2058 /* We now only have to make sure that there is no
2059 local label defined at the address of the ret
2060 instruction and that there is no local relocation
2061 in this section pointing to the ret. */
2063 int deleting_ret_is_safe
= 1;
2064 unsigned int section_offset_of_ret_insn
=
2065 irel
->r_offset
+ insn_size
;
2066 Elf_Internal_Sym
*isym
, *isymend
;
2067 unsigned int sec_shndx
;
2070 _bfd_elf_section_from_bfd_section (abfd
, sec
);
2072 /* Check for local symbols. */
2073 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2074 isymend
= isym
+ symtab_hdr
->sh_info
;
2075 /* PR 6019: There may not be any local symbols. */
2076 for (; isym
!= NULL
&& isym
< isymend
; isym
++)
2078 if (isym
->st_value
== section_offset_of_ret_insn
2079 && isym
->st_shndx
== sec_shndx
)
2081 deleting_ret_is_safe
= 0;
2083 printf ("local label prevents deletion of ret "
2084 "insn at address 0x%x\n",
2085 (int) dot
+ insn_size
);
2089 /* Now check for global symbols. */
2092 struct elf_link_hash_entry
**sym_hashes
;
2093 struct elf_link_hash_entry
**end_hashes
;
2095 symcount
= (symtab_hdr
->sh_size
2096 / sizeof (Elf32_External_Sym
)
2097 - symtab_hdr
->sh_info
);
2098 sym_hashes
= elf_sym_hashes (abfd
);
2099 end_hashes
= sym_hashes
+ symcount
;
2100 for (; sym_hashes
< end_hashes
; sym_hashes
++)
2102 struct elf_link_hash_entry
*sym_hash
=
2104 if ((sym_hash
->root
.type
== bfd_link_hash_defined
2105 || sym_hash
->root
.type
==
2106 bfd_link_hash_defweak
)
2107 && sym_hash
->root
.u
.def
.section
== sec
2108 && sym_hash
->root
.u
.def
.value
== section_offset_of_ret_insn
)
2110 deleting_ret_is_safe
= 0;
2112 printf ("global label prevents deletion of "
2113 "ret insn at address 0x%x\n",
2114 (int) dot
+ insn_size
);
2118 /* Now we check for relocations pointing to ret. */
2120 Elf_Internal_Rela
*rel
;
2121 Elf_Internal_Rela
*relend
;
2123 relend
= elf_section_data (sec
)->relocs
2126 for (rel
= elf_section_data (sec
)->relocs
;
2127 rel
< relend
; rel
++)
2129 bfd_vma reloc_target
= 0;
2131 /* Read this BFD's local symbols if we haven't
2133 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2135 isymbuf
= (Elf_Internal_Sym
*)
2136 symtab_hdr
->contents
;
2137 if (isymbuf
== NULL
)
2138 isymbuf
= bfd_elf_get_elf_syms
2141 symtab_hdr
->sh_info
, 0,
2143 if (isymbuf
== NULL
)
2147 /* Get the value of the symbol referred to
2149 if (ELF32_R_SYM (rel
->r_info
)
2150 < symtab_hdr
->sh_info
)
2152 /* A local symbol. */
2156 + ELF32_R_SYM (rel
->r_info
);
2157 sym_sec
= bfd_section_from_elf_index
2158 (abfd
, isym
->st_shndx
);
2159 symval
= isym
->st_value
;
2161 /* If the reloc is absolute, it will not
2162 have a symbol or section associated
2168 sym_sec
->output_section
->vma
2169 + sym_sec
->output_offset
;
2170 reloc_target
= symval
+ rel
->r_addend
;
2174 reloc_target
= symval
+ rel
->r_addend
;
2175 /* Reference symbol is absolute. */
2178 /* else ... reference symbol is extern. */
2180 if (address_of_ret
== reloc_target
)
2182 deleting_ret_is_safe
= 0;
2185 "rjmp/jmp ret sequence at address"
2186 " 0x%x could not be deleted. ret"
2187 " is target of a relocation.\n",
2188 (int) address_of_ret
);
2193 if (deleting_ret_is_safe
)
2196 printf ("unreachable ret instruction "
2197 "at address 0x%x deleted.\n",
2198 (int) dot
+ insn_size
);
2200 /* Delete two bytes of data. */
2201 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
2202 irel
->r_offset
+ insn_size
, 2))
2205 /* That will change things, so, we should relax
2206 again. Note that this is not required, and it
2220 if (contents
!= NULL
2221 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
2223 if (! link_info
->keep_memory
)
2227 /* Cache the section contents for elf_link_input_bfd. */
2228 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2232 if (internal_relocs
!= NULL
2233 && elf_section_data (sec
)->relocs
!= internal_relocs
)
2234 free (internal_relocs
);
2240 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2242 if (contents
!= NULL
2243 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
2245 if (internal_relocs
!= NULL
2246 && elf_section_data (sec
)->relocs
!= internal_relocs
)
2247 free (internal_relocs
);
2252 /* This is a version of bfd_generic_get_relocated_section_contents
2253 which uses elf32_avr_relocate_section.
2255 For avr it's essentially a cut and paste taken from the H8300 port.
2256 The author of the relaxation support patch for avr had absolutely no
2257 clue what is happening here but found out that this part of the code
2258 seems to be important. */
2261 elf32_avr_get_relocated_section_contents (bfd
*output_bfd
,
2262 struct bfd_link_info
*link_info
,
2263 struct bfd_link_order
*link_order
,
2265 bfd_boolean relocatable
,
2268 Elf_Internal_Shdr
*symtab_hdr
;
2269 asection
*input_section
= link_order
->u
.indirect
.section
;
2270 bfd
*input_bfd
= input_section
->owner
;
2271 asection
**sections
= NULL
;
2272 Elf_Internal_Rela
*internal_relocs
= NULL
;
2273 Elf_Internal_Sym
*isymbuf
= NULL
;
2275 /* We only need to handle the case of relaxing, or of having a
2276 particular set of section contents, specially. */
2278 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
2279 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
2283 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2285 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
2286 (size_t) input_section
->size
);
2288 if ((input_section
->flags
& SEC_RELOC
) != 0
2289 && input_section
->reloc_count
> 0)
2292 Elf_Internal_Sym
*isym
, *isymend
;
2295 internal_relocs
= (_bfd_elf_link_read_relocs
2296 (input_bfd
, input_section
, NULL
, NULL
, FALSE
));
2297 if (internal_relocs
== NULL
)
2300 if (symtab_hdr
->sh_info
!= 0)
2302 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2303 if (isymbuf
== NULL
)
2304 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2305 symtab_hdr
->sh_info
, 0,
2307 if (isymbuf
== NULL
)
2311 amt
= symtab_hdr
->sh_info
;
2312 amt
*= sizeof (asection
*);
2313 sections
= bfd_malloc (amt
);
2314 if (sections
== NULL
&& amt
!= 0)
2317 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2318 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
2322 if (isym
->st_shndx
== SHN_UNDEF
)
2323 isec
= bfd_und_section_ptr
;
2324 else if (isym
->st_shndx
== SHN_ABS
)
2325 isec
= bfd_abs_section_ptr
;
2326 else if (isym
->st_shndx
== SHN_COMMON
)
2327 isec
= bfd_com_section_ptr
;
2329 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
2334 if (! elf32_avr_relocate_section (output_bfd
, link_info
, input_bfd
,
2335 input_section
, data
, internal_relocs
,
2339 if (sections
!= NULL
)
2342 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2344 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
2345 free (internal_relocs
);
2351 if (sections
!= NULL
)
2354 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2356 if (internal_relocs
!= NULL
2357 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
2358 free (internal_relocs
);
2363 /* Determines the hash entry name for a particular reloc. It consists of
2364 the identifier of the symbol section and the added reloc addend and
2365 symbol offset relative to the section the symbol is attached to. */
2368 avr_stub_name (const asection
*symbol_section
,
2369 const bfd_vma symbol_offset
,
2370 const Elf_Internal_Rela
*rela
)
2375 len
= 8 + 1 + 8 + 1 + 1;
2376 stub_name
= bfd_malloc (len
);
2378 sprintf (stub_name
, "%08x+%08x",
2379 symbol_section
->id
& 0xffffffff,
2380 (unsigned int) ((rela
->r_addend
& 0xffffffff) + symbol_offset
));
2386 /* Add a new stub entry to the stub hash. Not all fields of the new
2387 stub entry are initialised. */
2389 static struct elf32_avr_stub_hash_entry
*
2390 avr_add_stub (const char *stub_name
,
2391 struct elf32_avr_link_hash_table
*htab
)
2393 struct elf32_avr_stub_hash_entry
*hsh
;
2395 /* Enter this entry into the linker stub hash table. */
2396 hsh
= avr_stub_hash_lookup (&htab
->bstab
, stub_name
, TRUE
, FALSE
);
2400 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
2405 hsh
->stub_offset
= 0;
2409 /* We assume that there is already space allocated for the stub section
2410 contents and that before building the stubs the section size is
2411 initialized to 0. We assume that within the stub hash table entry,
2412 the absolute position of the jmp target has been written in the
2413 target_value field. We write here the offset of the generated jmp insn
2414 relative to the trampoline section start to the stub_offset entry in
2415 the stub hash table entry. */
2418 avr_build_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
2420 struct elf32_avr_stub_hash_entry
*hsh
;
2421 struct bfd_link_info
*info
;
2422 struct elf32_avr_link_hash_table
*htab
;
2429 bfd_vma jmp_insn
= 0x0000940c;
2431 /* Massage our args to the form they really have. */
2432 hsh
= avr_stub_hash_entry (bh
);
2434 if (!hsh
->is_actually_needed
)
2437 info
= (struct bfd_link_info
*) in_arg
;
2439 htab
= avr_link_hash_table (info
);
2443 target
= hsh
->target_value
;
2445 /* Make a note of the offset within the stubs for this entry. */
2446 hsh
->stub_offset
= htab
->stub_sec
->size
;
2447 loc
= htab
->stub_sec
->contents
+ hsh
->stub_offset
;
2449 stub_bfd
= htab
->stub_sec
->owner
;
2452 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
2453 (unsigned int) target
,
2454 (unsigned int) hsh
->stub_offset
);
2456 /* We now have to add the information on the jump target to the bare
2457 opcode bits already set in jmp_insn. */
2459 /* Check for the alignment of the address. */
2463 starget
= target
>> 1;
2464 jmp_insn
|= ((starget
& 0x10000) | ((starget
<< 3) & 0x1f00000)) >> 16;
2465 bfd_put_16 (stub_bfd
, jmp_insn
, loc
);
2466 bfd_put_16 (stub_bfd
, (bfd_vma
) starget
& 0xffff, loc
+ 2);
2468 htab
->stub_sec
->size
+= 4;
2470 /* Now add the entries in the address mapping table if there is still
2475 nr
= htab
->amt_entry_cnt
+ 1;
2476 if (nr
<= htab
->amt_max_entry_cnt
)
2478 htab
->amt_entry_cnt
= nr
;
2480 htab
->amt_stub_offsets
[nr
- 1] = hsh
->stub_offset
;
2481 htab
->amt_destination_addr
[nr
- 1] = target
;
2489 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry
*bh
,
2490 void *in_arg ATTRIBUTE_UNUSED
)
2492 struct elf32_avr_stub_hash_entry
*hsh
;
2494 hsh
= avr_stub_hash_entry (bh
);
2495 hsh
->is_actually_needed
= FALSE
;
2501 avr_size_one_stub (struct bfd_hash_entry
*bh
, void *in_arg
)
2503 struct elf32_avr_stub_hash_entry
*hsh
;
2504 struct elf32_avr_link_hash_table
*htab
;
2507 /* Massage our args to the form they really have. */
2508 hsh
= avr_stub_hash_entry (bh
);
2511 if (hsh
->is_actually_needed
)
2516 htab
->stub_sec
->size
+= size
;
2521 elf32_avr_setup_params (struct bfd_link_info
*info
,
2523 asection
*avr_stub_section
,
2524 bfd_boolean no_stubs
,
2525 bfd_boolean deb_stubs
,
2526 bfd_boolean deb_relax
,
2527 bfd_vma pc_wrap_around
,
2528 bfd_boolean call_ret_replacement
)
2530 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
2534 htab
->stub_sec
= avr_stub_section
;
2535 htab
->stub_bfd
= avr_stub_bfd
;
2536 htab
->no_stubs
= no_stubs
;
2538 debug_relax
= deb_relax
;
2539 debug_stubs
= deb_stubs
;
2540 avr_pc_wrap_around
= pc_wrap_around
;
2541 avr_replace_call_ret_sequences
= call_ret_replacement
;
2545 /* Set up various things so that we can make a list of input sections
2546 for each output section included in the link. Returns -1 on error,
2547 0 when no stubs will be needed, and 1 on success. It also sets
2548 information on the stubs bfd and the stub section in the info
2552 elf32_avr_setup_section_lists (bfd
*output_bfd
,
2553 struct bfd_link_info
*info
)
2556 unsigned int bfd_count
;
2557 int top_id
, top_index
;
2559 asection
**input_list
, **list
;
2561 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
2563 if (htab
== NULL
|| htab
->no_stubs
)
2566 /* Count the number of input BFDs and find the top input section id. */
2567 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
2569 input_bfd
= input_bfd
->link_next
)
2572 for (section
= input_bfd
->sections
;
2574 section
= section
->next
)
2575 if (top_id
< section
->id
)
2576 top_id
= section
->id
;
2579 htab
->bfd_count
= bfd_count
;
2581 /* We can't use output_bfd->section_count here to find the top output
2582 section index as some sections may have been removed, and
2583 strip_excluded_output_sections doesn't renumber the indices. */
2584 for (section
= output_bfd
->sections
, top_index
= 0;
2586 section
= section
->next
)
2587 if (top_index
< section
->index
)
2588 top_index
= section
->index
;
2590 htab
->top_index
= top_index
;
2591 amt
= sizeof (asection
*) * (top_index
+ 1);
2592 input_list
= bfd_malloc (amt
);
2593 htab
->input_list
= input_list
;
2594 if (input_list
== NULL
)
2597 /* For sections we aren't interested in, mark their entries with a
2598 value we can check later. */
2599 list
= input_list
+ top_index
;
2601 *list
= bfd_abs_section_ptr
;
2602 while (list
-- != input_list
);
2604 for (section
= output_bfd
->sections
;
2606 section
= section
->next
)
2607 if ((section
->flags
& SEC_CODE
) != 0)
2608 input_list
[section
->index
] = NULL
;
2614 /* Read in all local syms for all input bfds, and create hash entries
2615 for export stubs if we are building a multi-subspace shared lib.
2616 Returns -1 on error, 0 otherwise. */
2619 get_local_syms (bfd
*input_bfd
, struct bfd_link_info
*info
)
2621 unsigned int bfd_indx
;
2622 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
2623 struct elf32_avr_link_hash_table
*htab
= avr_link_hash_table (info
);
2629 /* We want to read in symbol extension records only once. To do this
2630 we need to read in the local symbols in parallel and save them for
2631 later use; so hold pointers to the local symbols in an array. */
2632 amt
= sizeof (Elf_Internal_Sym
*) * htab
->bfd_count
;
2633 all_local_syms
= bfd_zmalloc (amt
);
2634 htab
->all_local_syms
= all_local_syms
;
2635 if (all_local_syms
== NULL
)
2638 /* Walk over all the input BFDs, swapping in local symbols.
2639 If we are creating a shared library, create hash entries for the
2643 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
2645 Elf_Internal_Shdr
*symtab_hdr
;
2647 /* We'll need the symbol table in a second. */
2648 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2649 if (symtab_hdr
->sh_info
== 0)
2652 /* We need an array of the local symbols attached to the input bfd. */
2653 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2654 if (local_syms
== NULL
)
2656 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2657 symtab_hdr
->sh_info
, 0,
2659 /* Cache them for elf_link_input_bfd. */
2660 symtab_hdr
->contents
= (unsigned char *) local_syms
;
2662 if (local_syms
== NULL
)
2665 all_local_syms
[bfd_indx
] = local_syms
;
2671 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
2674 elf32_avr_size_stubs (bfd
*output_bfd
,
2675 struct bfd_link_info
*info
,
2676 bfd_boolean is_prealloc_run
)
2678 struct elf32_avr_link_hash_table
*htab
;
2679 int stub_changed
= 0;
2681 htab
= avr_link_hash_table (info
);
2685 /* At this point we initialize htab->vector_base
2686 To the start of the text output section. */
2687 htab
->vector_base
= htab
->stub_sec
->output_section
->vma
;
2689 if (get_local_syms (info
->input_bfds
, info
))
2691 if (htab
->all_local_syms
)
2692 goto error_ret_free_local
;
2696 if (ADD_DUMMY_STUBS_FOR_DEBUGGING
)
2698 struct elf32_avr_stub_hash_entry
*test
;
2700 test
= avr_add_stub ("Hugo",htab
);
2701 test
->target_value
= 0x123456;
2702 test
->stub_offset
= 13;
2704 test
= avr_add_stub ("Hugo2",htab
);
2705 test
->target_value
= 0x84210;
2706 test
->stub_offset
= 14;
2712 unsigned int bfd_indx
;
2714 /* We will have to re-generate the stub hash table each time anything
2715 in memory has changed. */
2717 bfd_hash_traverse (&htab
->bstab
, avr_mark_stub_not_to_be_necessary
, htab
);
2718 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
2720 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
2722 Elf_Internal_Shdr
*symtab_hdr
;
2724 Elf_Internal_Sym
*local_syms
;
2726 /* We'll need the symbol table in a second. */
2727 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2728 if (symtab_hdr
->sh_info
== 0)
2731 local_syms
= htab
->all_local_syms
[bfd_indx
];
2733 /* Walk over each section attached to the input bfd. */
2734 for (section
= input_bfd
->sections
;
2736 section
= section
->next
)
2738 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
2740 /* If there aren't any relocs, then there's nothing more
2742 if ((section
->flags
& SEC_RELOC
) == 0
2743 || section
->reloc_count
== 0)
2746 /* If this section is a link-once section that will be
2747 discarded, then don't create any stubs. */
2748 if (section
->output_section
== NULL
2749 || section
->output_section
->owner
!= output_bfd
)
2752 /* Get the relocs. */
2754 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
, NULL
,
2756 if (internal_relocs
== NULL
)
2757 goto error_ret_free_local
;
2759 /* Now examine each relocation. */
2760 irela
= internal_relocs
;
2761 irelaend
= irela
+ section
->reloc_count
;
2762 for (; irela
< irelaend
; irela
++)
2764 unsigned int r_type
, r_indx
;
2765 struct elf32_avr_stub_hash_entry
*hsh
;
2768 bfd_vma destination
;
2769 struct elf_link_hash_entry
*hh
;
2772 r_type
= ELF32_R_TYPE (irela
->r_info
);
2773 r_indx
= ELF32_R_SYM (irela
->r_info
);
2775 /* Only look for 16 bit GS relocs. No other reloc will need a
2777 if (!((r_type
== R_AVR_16_PM
)
2778 || (r_type
== R_AVR_LO8_LDI_GS
)
2779 || (r_type
== R_AVR_HI8_LDI_GS
)))
2782 /* Now determine the call target, its name, value,
2788 if (r_indx
< symtab_hdr
->sh_info
)
2790 /* It's a local symbol. */
2791 Elf_Internal_Sym
*sym
;
2792 Elf_Internal_Shdr
*hdr
;
2795 sym
= local_syms
+ r_indx
;
2796 if (ELF_ST_TYPE (sym
->st_info
) != STT_SECTION
)
2797 sym_value
= sym
->st_value
;
2798 shndx
= sym
->st_shndx
;
2799 if (shndx
< elf_numsections (input_bfd
))
2801 hdr
= elf_elfsections (input_bfd
)[shndx
];
2802 sym_sec
= hdr
->bfd_section
;
2803 destination
= (sym_value
+ irela
->r_addend
2804 + sym_sec
->output_offset
2805 + sym_sec
->output_section
->vma
);
2810 /* It's an external symbol. */
2813 e_indx
= r_indx
- symtab_hdr
->sh_info
;
2814 hh
= elf_sym_hashes (input_bfd
)[e_indx
];
2816 while (hh
->root
.type
== bfd_link_hash_indirect
2817 || hh
->root
.type
== bfd_link_hash_warning
)
2818 hh
= (struct elf_link_hash_entry
*)
2819 (hh
->root
.u
.i
.link
);
2821 if (hh
->root
.type
== bfd_link_hash_defined
2822 || hh
->root
.type
== bfd_link_hash_defweak
)
2824 sym_sec
= hh
->root
.u
.def
.section
;
2825 sym_value
= hh
->root
.u
.def
.value
;
2826 if (sym_sec
->output_section
!= NULL
)
2827 destination
= (sym_value
+ irela
->r_addend
2828 + sym_sec
->output_offset
2829 + sym_sec
->output_section
->vma
);
2831 else if (hh
->root
.type
== bfd_link_hash_undefweak
)
2836 else if (hh
->root
.type
== bfd_link_hash_undefined
)
2838 if (! (info
->unresolved_syms_in_objects
== RM_IGNORE
2839 && (ELF_ST_VISIBILITY (hh
->other
)
2845 bfd_set_error (bfd_error_bad_value
);
2847 error_ret_free_internal
:
2848 if (elf_section_data (section
)->relocs
== NULL
)
2849 free (internal_relocs
);
2850 goto error_ret_free_local
;
2854 if (! avr_stub_is_required_for_16_bit_reloc
2855 (destination
- htab
->vector_base
))
2857 if (!is_prealloc_run
)
2858 /* We are having a reloc that does't need a stub. */
2861 /* We don't right now know if a stub will be needed.
2862 Let's rather be on the safe side. */
2865 /* Get the name of this stub. */
2866 stub_name
= avr_stub_name (sym_sec
, sym_value
, irela
);
2869 goto error_ret_free_internal
;
2872 hsh
= avr_stub_hash_lookup (&htab
->bstab
,
2877 /* The proper stub has already been created. Mark it
2878 to be used and write the possibly changed destination
2880 hsh
->is_actually_needed
= TRUE
;
2881 hsh
->target_value
= destination
;
2886 hsh
= avr_add_stub (stub_name
, htab
);
2890 goto error_ret_free_internal
;
2893 hsh
->is_actually_needed
= TRUE
;
2894 hsh
->target_value
= destination
;
2897 printf ("Adding stub with destination 0x%x to the"
2898 " hash table.\n", (unsigned int) destination
);
2900 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run
);
2902 stub_changed
= TRUE
;
2905 /* We're done with the internal relocs, free them. */
2906 if (elf_section_data (section
)->relocs
== NULL
)
2907 free (internal_relocs
);
2911 /* Re-Calculate the number of needed stubs. */
2912 htab
->stub_sec
->size
= 0;
2913 bfd_hash_traverse (&htab
->bstab
, avr_size_one_stub
, htab
);
2918 stub_changed
= FALSE
;
2921 free (htab
->all_local_syms
);
2924 error_ret_free_local
:
2925 free (htab
->all_local_syms
);
2930 /* Build all the stubs associated with the current output file. The
2931 stubs are kept in a hash table attached to the main linker hash
2932 table. We also set up the .plt entries for statically linked PIC
2933 functions here. This function is called via hppaelf_finish in the
2937 elf32_avr_build_stubs (struct bfd_link_info
*info
)
2940 struct bfd_hash_table
*table
;
2941 struct elf32_avr_link_hash_table
*htab
;
2942 bfd_size_type total_size
= 0;
2944 htab
= avr_link_hash_table (info
);
2948 /* In case that there were several stub sections: */
2949 for (stub_sec
= htab
->stub_bfd
->sections
;
2951 stub_sec
= stub_sec
->next
)
2955 /* Allocate memory to hold the linker stubs. */
2956 size
= stub_sec
->size
;
2959 stub_sec
->contents
= bfd_zalloc (htab
->stub_bfd
, size
);
2960 if (stub_sec
->contents
== NULL
&& size
!= 0)
2965 /* Allocate memory for the adress mapping table. */
2966 htab
->amt_entry_cnt
= 0;
2967 htab
->amt_max_entry_cnt
= total_size
/ 4;
2968 htab
->amt_stub_offsets
= bfd_malloc (sizeof (bfd_vma
)
2969 * htab
->amt_max_entry_cnt
);
2970 htab
->amt_destination_addr
= bfd_malloc (sizeof (bfd_vma
)
2971 * htab
->amt_max_entry_cnt
);
2974 printf ("Allocating %i entries in the AMT\n", htab
->amt_max_entry_cnt
);
2976 /* Build the stubs as directed by the stub hash table. */
2977 table
= &htab
->bstab
;
2978 bfd_hash_traverse (table
, avr_build_one_stub
, info
);
2981 printf ("Final Stub section Size: %i\n", (int) htab
->stub_sec
->size
);
2986 #define ELF_ARCH bfd_arch_avr
2987 #define ELF_TARGET_ID AVR_ELF_DATA
2988 #define ELF_MACHINE_CODE EM_AVR
2989 #define ELF_MACHINE_ALT1 EM_AVR_OLD
2990 #define ELF_MAXPAGESIZE 1
2992 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
2993 #define TARGET_LITTLE_NAME "elf32-avr"
2995 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
2996 #define bfd_elf32_bfd_link_hash_table_free elf32_avr_link_hash_table_free
2998 #define elf_info_to_howto avr_info_to_howto_rela
2999 #define elf_info_to_howto_rel NULL
3000 #define elf_backend_relocate_section elf32_avr_relocate_section
3001 #define elf_backend_check_relocs elf32_avr_check_relocs
3002 #define elf_backend_can_gc_sections 1
3003 #define elf_backend_rela_normal 1
3004 #define elf_backend_final_write_processing \
3005 bfd_elf_avr_final_write_processing
3006 #define elf_backend_object_p elf32_avr_object_p
3008 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
3009 #define bfd_elf32_bfd_get_relocated_section_contents \
3010 elf32_avr_get_relocated_section_contents
3012 #include "elf32-target.h"