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. */
29 static reloc_howto_type elf_avr_howto_table
[] =
31 HOWTO (R_AVR_NONE
, /* type */
33 2, /* size (0 = byte, 1 = short, 2 = long) */
35 FALSE
, /* pc_relative */
37 complain_overflow_bitfield
, /* complain_on_overflow */
38 bfd_elf_generic_reloc
, /* special_function */
39 "R_AVR_NONE", /* name */
40 FALSE
, /* partial_inplace */
43 FALSE
), /* pcrel_offset */
45 HOWTO (R_AVR_32
, /* type */
47 2, /* size (0 = byte, 1 = short, 2 = long) */
49 FALSE
, /* pc_relative */
51 complain_overflow_bitfield
, /* complain_on_overflow */
52 bfd_elf_generic_reloc
, /* special_function */
53 "R_AVR_32", /* name */
54 FALSE
, /* partial_inplace */
55 0xffffffff, /* src_mask */
56 0xffffffff, /* dst_mask */
57 FALSE
), /* pcrel_offset */
59 /* A 7 bit PC relative relocation. */
60 HOWTO (R_AVR_7_PCREL
, /* type */
62 1, /* size (0 = byte, 1 = short, 2 = long) */
64 TRUE
, /* pc_relative */
66 complain_overflow_bitfield
, /* complain_on_overflow */
67 bfd_elf_generic_reloc
, /* special_function */
68 "R_AVR_7_PCREL", /* name */
69 FALSE
, /* partial_inplace */
70 0xffff, /* src_mask */
71 0xffff, /* dst_mask */
72 TRUE
), /* pcrel_offset */
74 /* A 13 bit PC relative relocation. */
75 HOWTO (R_AVR_13_PCREL
, /* type */
77 1, /* size (0 = byte, 1 = short, 2 = long) */
79 TRUE
, /* pc_relative */
81 complain_overflow_bitfield
, /* complain_on_overflow */
82 bfd_elf_generic_reloc
, /* special_function */
83 "R_AVR_13_PCREL", /* name */
84 FALSE
, /* partial_inplace */
87 TRUE
), /* pcrel_offset */
89 /* A 16 bit absolute relocation. */
90 HOWTO (R_AVR_16
, /* type */
92 1, /* size (0 = byte, 1 = short, 2 = long) */
94 FALSE
, /* pc_relative */
96 complain_overflow_dont
, /* complain_on_overflow */
97 bfd_elf_generic_reloc
, /* special_function */
98 "R_AVR_16", /* name */
99 FALSE
, /* partial_inplace */
100 0xffff, /* src_mask */
101 0xffff, /* dst_mask */
102 FALSE
), /* pcrel_offset */
104 /* A 16 bit absolute relocation for command address. */
105 HOWTO (R_AVR_16_PM
, /* type */
107 1, /* size (0 = byte, 1 = short, 2 = long) */
109 FALSE
, /* pc_relative */
111 complain_overflow_bitfield
, /* complain_on_overflow */
112 bfd_elf_generic_reloc
, /* special_function */
113 "R_AVR_16_PM", /* name */
114 FALSE
, /* partial_inplace */
115 0xffff, /* src_mask */
116 0xffff, /* dst_mask */
117 FALSE
), /* pcrel_offset */
118 /* A low 8 bit absolute relocation of 16 bit address.
120 HOWTO (R_AVR_LO8_LDI
, /* type */
122 1, /* size (0 = byte, 1 = short, 2 = long) */
124 FALSE
, /* pc_relative */
126 complain_overflow_dont
, /* complain_on_overflow */
127 bfd_elf_generic_reloc
, /* special_function */
128 "R_AVR_LO8_LDI", /* name */
129 FALSE
, /* partial_inplace */
130 0xffff, /* src_mask */
131 0xffff, /* dst_mask */
132 FALSE
), /* pcrel_offset */
133 /* A high 8 bit absolute relocation of 16 bit address.
135 HOWTO (R_AVR_HI8_LDI
, /* type */
137 1, /* size (0 = byte, 1 = short, 2 = long) */
139 FALSE
, /* pc_relative */
141 complain_overflow_dont
, /* complain_on_overflow */
142 bfd_elf_generic_reloc
, /* special_function */
143 "R_AVR_HI8_LDI", /* name */
144 FALSE
, /* partial_inplace */
145 0xffff, /* src_mask */
146 0xffff, /* dst_mask */
147 FALSE
), /* pcrel_offset */
148 /* A high 6 bit absolute relocation of 22 bit address.
149 For LDI command. As well second most significant 8 bit value of
150 a 32 bit link-time constant. */
151 HOWTO (R_AVR_HH8_LDI
, /* type */
153 1, /* size (0 = byte, 1 = short, 2 = long) */
155 FALSE
, /* pc_relative */
157 complain_overflow_dont
, /* complain_on_overflow */
158 bfd_elf_generic_reloc
, /* special_function */
159 "R_AVR_HH8_LDI", /* name */
160 FALSE
, /* partial_inplace */
161 0xffff, /* src_mask */
162 0xffff, /* dst_mask */
163 FALSE
), /* pcrel_offset */
164 /* A negative low 8 bit absolute relocation of 16 bit address.
166 HOWTO (R_AVR_LO8_LDI_NEG
, /* type */
168 1, /* size (0 = byte, 1 = short, 2 = long) */
170 FALSE
, /* pc_relative */
172 complain_overflow_dont
, /* complain_on_overflow */
173 bfd_elf_generic_reloc
, /* special_function */
174 "R_AVR_LO8_LDI_NEG", /* name */
175 FALSE
, /* partial_inplace */
176 0xffff, /* src_mask */
177 0xffff, /* dst_mask */
178 FALSE
), /* pcrel_offset */
179 /* A negative high 8 bit absolute relocation of 16 bit address.
181 HOWTO (R_AVR_HI8_LDI_NEG
, /* type */
183 1, /* size (0 = byte, 1 = short, 2 = long) */
185 FALSE
, /* pc_relative */
187 complain_overflow_dont
, /* complain_on_overflow */
188 bfd_elf_generic_reloc
, /* special_function */
189 "R_AVR_HI8_LDI_NEG", /* name */
190 FALSE
, /* partial_inplace */
191 0xffff, /* src_mask */
192 0xffff, /* dst_mask */
193 FALSE
), /* pcrel_offset */
194 /* A negative high 6 bit absolute relocation of 22 bit address.
196 HOWTO (R_AVR_HH8_LDI_NEG
, /* type */
198 1, /* size (0 = byte, 1 = short, 2 = long) */
200 FALSE
, /* pc_relative */
202 complain_overflow_dont
, /* complain_on_overflow */
203 bfd_elf_generic_reloc
, /* special_function */
204 "R_AVR_HH8_LDI_NEG", /* name */
205 FALSE
, /* partial_inplace */
206 0xffff, /* src_mask */
207 0xffff, /* dst_mask */
208 FALSE
), /* pcrel_offset */
209 /* A low 8 bit absolute relocation of 24 bit program memory address.
211 HOWTO (R_AVR_LO8_LDI_PM
, /* type */
213 1, /* size (0 = byte, 1 = short, 2 = long) */
215 FALSE
, /* pc_relative */
217 complain_overflow_dont
, /* complain_on_overflow */
218 bfd_elf_generic_reloc
, /* special_function */
219 "R_AVR_LO8_LDI_PM", /* name */
220 FALSE
, /* partial_inplace */
221 0xffff, /* src_mask */
222 0xffff, /* dst_mask */
223 FALSE
), /* pcrel_offset */
224 /* A high 8 bit absolute relocation of 16 bit program memory address.
226 HOWTO (R_AVR_HI8_LDI_PM
, /* type */
228 1, /* size (0 = byte, 1 = short, 2 = long) */
230 FALSE
, /* pc_relative */
232 complain_overflow_dont
, /* complain_on_overflow */
233 bfd_elf_generic_reloc
, /* special_function */
234 "R_AVR_HI8_LDI_PM", /* name */
235 FALSE
, /* partial_inplace */
236 0xffff, /* src_mask */
237 0xffff, /* dst_mask */
238 FALSE
), /* pcrel_offset */
239 /* A high 8 bit absolute relocation of 24 bit program memory address.
241 HOWTO (R_AVR_HH8_LDI_PM
, /* type */
243 1, /* size (0 = byte, 1 = short, 2 = long) */
245 FALSE
, /* pc_relative */
247 complain_overflow_dont
, /* complain_on_overflow */
248 bfd_elf_generic_reloc
, /* special_function */
249 "R_AVR_HH8_LDI_PM", /* name */
250 FALSE
, /* partial_inplace */
251 0xffff, /* src_mask */
252 0xffff, /* dst_mask */
253 FALSE
), /* pcrel_offset */
254 /* A low 8 bit absolute relocation of a negative 24 bit
255 program memory address. For LDI command. */
256 HOWTO (R_AVR_LO8_LDI_PM_NEG
, /* type */
258 1, /* size (0 = byte, 1 = short, 2 = long) */
260 FALSE
, /* pc_relative */
262 complain_overflow_dont
, /* complain_on_overflow */
263 bfd_elf_generic_reloc
, /* special_function */
264 "R_AVR_LO8_LDI_PM_NEG", /* name */
265 FALSE
, /* partial_inplace */
266 0xffff, /* src_mask */
267 0xffff, /* dst_mask */
268 FALSE
), /* pcrel_offset */
269 /* A high 8 bit absolute relocation of a negative 16 bit
270 program memory address. For LDI command. */
271 HOWTO (R_AVR_HI8_LDI_PM_NEG
, /* type */
273 1, /* size (0 = byte, 1 = short, 2 = long) */
275 FALSE
, /* pc_relative */
277 complain_overflow_dont
, /* complain_on_overflow */
278 bfd_elf_generic_reloc
, /* special_function */
279 "R_AVR_HI8_LDI_PM_NEG", /* name */
280 FALSE
, /* partial_inplace */
281 0xffff, /* src_mask */
282 0xffff, /* dst_mask */
283 FALSE
), /* pcrel_offset */
284 /* A high 8 bit absolute relocation of a negative 24 bit
285 program memory address. For LDI command. */
286 HOWTO (R_AVR_HH8_LDI_PM_NEG
, /* type */
288 1, /* size (0 = byte, 1 = short, 2 = long) */
290 FALSE
, /* pc_relative */
292 complain_overflow_dont
, /* complain_on_overflow */
293 bfd_elf_generic_reloc
, /* special_function */
294 "R_AVR_HH8_LDI_PM_NEG", /* name */
295 FALSE
, /* partial_inplace */
296 0xffff, /* src_mask */
297 0xffff, /* dst_mask */
298 FALSE
), /* pcrel_offset */
299 /* Relocation for CALL command in ATmega. */
300 HOWTO (R_AVR_CALL
, /* type */
302 2, /* 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_CALL", /* name */
309 FALSE
, /* partial_inplace */
310 0xffffffff, /* src_mask */
311 0xffffffff, /* dst_mask */
312 FALSE
), /* pcrel_offset */
313 /* A 16 bit absolute relocation of 16 bit address.
315 HOWTO (R_AVR_LDI
, /* 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_LDI", /* name */
324 FALSE
, /* partial_inplace */
325 0xffff, /* src_mask */
326 0xffff, /* dst_mask */
327 FALSE
), /* pcrel_offset */
328 /* A 6 bit absolute relocation of 6 bit offset.
329 For ldd/sdd command. */
330 HOWTO (R_AVR_6
, /* type */
332 0, /* 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_6", /* name */
339 FALSE
, /* partial_inplace */
340 0xffff, /* src_mask */
341 0xffff, /* dst_mask */
342 FALSE
), /* pcrel_offset */
343 /* A 6 bit absolute relocation of 6 bit offset.
344 For sbiw/adiw command. */
345 HOWTO (R_AVR_6_ADIW
, /* type */
347 0, /* 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_6_ADIW", /* name */
354 FALSE
, /* partial_inplace */
355 0xffff, /* src_mask */
356 0xffff, /* dst_mask */
357 FALSE
), /* pcrel_offset */
358 /* Most significant 8 bit value of a 32 bit link-time constant. */
359 HOWTO (R_AVR_MS8_LDI
, /* type */
361 1, /* size (0 = byte, 1 = short, 2 = long) */
363 FALSE
, /* pc_relative */
365 complain_overflow_dont
, /* complain_on_overflow */
366 bfd_elf_generic_reloc
, /* special_function */
367 "R_AVR_MS8_LDI", /* name */
368 FALSE
, /* partial_inplace */
369 0xffff, /* src_mask */
370 0xffff, /* dst_mask */
371 FALSE
), /* pcrel_offset */
372 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
373 HOWTO (R_AVR_MS8_LDI_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_MS8_LDI_NEG", /* name */
382 FALSE
, /* partial_inplace */
383 0xffff, /* src_mask */
384 0xffff, /* dst_mask */
385 FALSE
) /* pcrel_offset */
388 /* Map BFD reloc types to AVR ELF reloc types. */
392 bfd_reloc_code_real_type bfd_reloc_val
;
393 unsigned int elf_reloc_val
;
396 static const struct avr_reloc_map avr_reloc_map
[] =
398 { BFD_RELOC_NONE
, R_AVR_NONE
},
399 { BFD_RELOC_32
, R_AVR_32
},
400 { BFD_RELOC_AVR_7_PCREL
, R_AVR_7_PCREL
},
401 { BFD_RELOC_AVR_13_PCREL
, R_AVR_13_PCREL
},
402 { BFD_RELOC_16
, R_AVR_16
},
403 { BFD_RELOC_AVR_16_PM
, R_AVR_16_PM
},
404 { BFD_RELOC_AVR_LO8_LDI
, R_AVR_LO8_LDI
},
405 { BFD_RELOC_AVR_HI8_LDI
, R_AVR_HI8_LDI
},
406 { BFD_RELOC_AVR_HH8_LDI
, R_AVR_HH8_LDI
},
407 { BFD_RELOC_AVR_MS8_LDI
, R_AVR_MS8_LDI
},
408 { BFD_RELOC_AVR_LO8_LDI_NEG
, R_AVR_LO8_LDI_NEG
},
409 { BFD_RELOC_AVR_HI8_LDI_NEG
, R_AVR_HI8_LDI_NEG
},
410 { BFD_RELOC_AVR_HH8_LDI_NEG
, R_AVR_HH8_LDI_NEG
},
411 { BFD_RELOC_AVR_MS8_LDI_NEG
, R_AVR_MS8_LDI_NEG
},
412 { BFD_RELOC_AVR_LO8_LDI_PM
, R_AVR_LO8_LDI_PM
},
413 { BFD_RELOC_AVR_HI8_LDI_PM
, R_AVR_HI8_LDI_PM
},
414 { BFD_RELOC_AVR_HH8_LDI_PM
, R_AVR_HH8_LDI_PM
},
415 { BFD_RELOC_AVR_LO8_LDI_PM_NEG
, R_AVR_LO8_LDI_PM_NEG
},
416 { BFD_RELOC_AVR_HI8_LDI_PM_NEG
, R_AVR_HI8_LDI_PM_NEG
},
417 { BFD_RELOC_AVR_HH8_LDI_PM_NEG
, R_AVR_HH8_LDI_PM_NEG
},
418 { BFD_RELOC_AVR_CALL
, R_AVR_CALL
},
419 { BFD_RELOC_AVR_LDI
, R_AVR_LDI
},
420 { BFD_RELOC_AVR_6
, R_AVR_6
},
421 { BFD_RELOC_AVR_6_ADIW
, R_AVR_6_ADIW
}
424 /* Meant to be filled one day with the wrap around address for the
425 specific device. I.e. should get the value 0x4000 for 16k devices,
426 0x8000 for 32k devices and so on.
428 We initialize it here with a value of 0x1000000 resulting in
429 that we will never suggest a wrap-around jump during relaxation.
430 The logic of the source code later on assumes that in
431 avr_pc_wrap_around one single bit is set. */
433 unsigned int avr_pc_wrap_around
= 0x10000000;
435 /* Calculates the effective distance of a pc relative jump/call. */
437 avr_relative_distance_considering_wrap_around (unsigned int distance
)
439 unsigned int wrap_around_mask
= avr_pc_wrap_around
- 1;
440 int dist_with_wrap_around
= distance
& wrap_around_mask
;
442 if (dist_with_wrap_around
> ((int) (avr_pc_wrap_around
>> 1)))
443 dist_with_wrap_around
-= avr_pc_wrap_around
;
445 return dist_with_wrap_around
;
449 static reloc_howto_type
*
450 bfd_elf32_bfd_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
451 bfd_reloc_code_real_type code
)
456 i
< sizeof (avr_reloc_map
) / sizeof (struct avr_reloc_map
);
459 if (avr_reloc_map
[i
].bfd_reloc_val
== code
)
460 return &elf_avr_howto_table
[avr_reloc_map
[i
].elf_reloc_val
];
466 /* Set the howto pointer for an AVR ELF reloc. */
469 avr_info_to_howto_rela (bfd
*abfd ATTRIBUTE_UNUSED
,
471 Elf_Internal_Rela
*dst
)
475 r_type
= ELF32_R_TYPE (dst
->r_info
);
476 BFD_ASSERT (r_type
< (unsigned int) R_AVR_max
);
477 cache_ptr
->howto
= &elf_avr_howto_table
[r_type
];
481 elf32_avr_gc_mark_hook (asection
*sec
,
482 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
483 Elf_Internal_Rela
*rel
,
484 struct elf_link_hash_entry
*h
,
485 Elf_Internal_Sym
*sym
)
489 switch (ELF32_R_TYPE (rel
->r_info
))
492 switch (h
->root
.type
)
494 case bfd_link_hash_defined
:
495 case bfd_link_hash_defweak
:
496 return h
->root
.u
.def
.section
;
498 case bfd_link_hash_common
:
499 return h
->root
.u
.c
.p
->section
;
507 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
513 elf32_avr_gc_sweep_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
514 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
515 asection
*sec ATTRIBUTE_UNUSED
,
516 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
)
518 /* We don't use got and plt entries for avr. */
522 /* Look through the relocs for a section during the first phase.
523 Since we don't do .gots or .plts, we just need to consider the
524 virtual table relocs for gc. */
527 elf32_avr_check_relocs (bfd
*abfd
,
528 struct bfd_link_info
*info
,
530 const Elf_Internal_Rela
*relocs
)
532 Elf_Internal_Shdr
*symtab_hdr
;
533 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
534 const Elf_Internal_Rela
*rel
;
535 const Elf_Internal_Rela
*rel_end
;
537 if (info
->relocatable
)
540 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
541 sym_hashes
= elf_sym_hashes (abfd
);
542 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
);
543 if (!elf_bad_symtab (abfd
))
544 sym_hashes_end
-= symtab_hdr
->sh_info
;
546 rel_end
= relocs
+ sec
->reloc_count
;
547 for (rel
= relocs
; rel
< rel_end
; rel
++)
549 struct elf_link_hash_entry
*h
;
550 unsigned long r_symndx
;
552 r_symndx
= ELF32_R_SYM (rel
->r_info
);
553 if (r_symndx
< symtab_hdr
->sh_info
)
557 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
558 while (h
->root
.type
== bfd_link_hash_indirect
559 || h
->root
.type
== bfd_link_hash_warning
)
560 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
567 /* Perform a single relocation. By default we use the standard BFD
568 routines, but a few relocs, we have to do them ourselves. */
570 static bfd_reloc_status_type
571 avr_final_link_relocate (reloc_howto_type
* howto
,
573 asection
* input_section
,
575 Elf_Internal_Rela
* rel
,
578 bfd_reloc_status_type r
= bfd_reloc_ok
;
585 contents
+= rel
->r_offset
;
586 srel
= (bfd_signed_vma
) relocation
;
587 srel
+= rel
->r_addend
;
588 srel
-= rel
->r_offset
;
589 srel
-= 2; /* Branch instructions add 2 to the PC... */
590 srel
-= (input_section
->output_section
->vma
+
591 input_section
->output_offset
);
594 return bfd_reloc_outofrange
;
595 if (srel
> ((1 << 7) - 1) || (srel
< - (1 << 7)))
596 return bfd_reloc_overflow
;
597 x
= bfd_get_16 (input_bfd
, contents
);
598 x
= (x
& 0xfc07) | (((srel
>> 1) << 3) & 0x3f8);
599 bfd_put_16 (input_bfd
, x
, contents
);
603 contents
+= rel
->r_offset
;
604 srel
= (bfd_signed_vma
) relocation
;
605 srel
+= rel
->r_addend
;
606 srel
-= rel
->r_offset
;
607 srel
-= 2; /* Branch instructions add 2 to the PC... */
608 srel
-= (input_section
->output_section
->vma
+
609 input_section
->output_offset
);
612 return bfd_reloc_outofrange
;
614 srel
= avr_relative_distance_considering_wrap_around (srel
);
616 /* AVR addresses commands as words. */
619 /* Check for overflow. */
620 if (srel
< -2048 || srel
> 2047)
622 /* Relative distance is too large. */
624 /* Always apply WRAPAROUND for avr2 and avr4. */
625 switch (bfd_get_mach (input_bfd
))
632 return bfd_reloc_overflow
;
636 x
= bfd_get_16 (input_bfd
, contents
);
637 x
= (x
& 0xf000) | (srel
& 0xfff);
638 bfd_put_16 (input_bfd
, x
, contents
);
642 contents
+= rel
->r_offset
;
643 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
644 x
= bfd_get_16 (input_bfd
, contents
);
645 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
646 bfd_put_16 (input_bfd
, x
, contents
);
650 contents
+= rel
->r_offset
;
651 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
652 if (((srel
> 0) && (srel
& 0xffff) > 255)
653 || ((srel
< 0) && ((-srel
) & 0xffff) > 128))
654 /* Remove offset for data/eeprom section. */
655 return bfd_reloc_overflow
;
657 x
= bfd_get_16 (input_bfd
, contents
);
658 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
659 bfd_put_16 (input_bfd
, x
, contents
);
663 contents
+= rel
->r_offset
;
664 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
665 if (((srel
& 0xffff) > 63) || (srel
< 0))
666 /* Remove offset for data/eeprom section. */
667 return bfd_reloc_overflow
;
668 x
= bfd_get_16 (input_bfd
, contents
);
669 x
= (x
& 0xd3f8) | ((srel
& 7) | ((srel
& (3 << 3)) << 7)
670 | ((srel
& (1 << 5)) << 8));
671 bfd_put_16 (input_bfd
, x
, contents
);
675 contents
+= rel
->r_offset
;
676 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
677 if (((srel
& 0xffff) > 63) || (srel
< 0))
678 /* Remove offset for data/eeprom section. */
679 return bfd_reloc_overflow
;
680 x
= bfd_get_16 (input_bfd
, contents
);
681 x
= (x
& 0xff30) | (srel
& 0xf) | ((srel
& 0x30) << 2);
682 bfd_put_16 (input_bfd
, x
, contents
);
686 contents
+= rel
->r_offset
;
687 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
688 srel
= (srel
>> 8) & 0xff;
689 x
= bfd_get_16 (input_bfd
, contents
);
690 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
691 bfd_put_16 (input_bfd
, x
, contents
);
695 contents
+= rel
->r_offset
;
696 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
697 srel
= (srel
>> 16) & 0xff;
698 x
= bfd_get_16 (input_bfd
, contents
);
699 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
700 bfd_put_16 (input_bfd
, x
, contents
);
704 contents
+= rel
->r_offset
;
705 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
706 srel
= (srel
>> 24) & 0xff;
707 x
= bfd_get_16 (input_bfd
, contents
);
708 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
709 bfd_put_16 (input_bfd
, x
, contents
);
712 case R_AVR_LO8_LDI_NEG
:
713 contents
+= rel
->r_offset
;
714 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
716 x
= bfd_get_16 (input_bfd
, contents
);
717 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
718 bfd_put_16 (input_bfd
, x
, contents
);
721 case R_AVR_HI8_LDI_NEG
:
722 contents
+= rel
->r_offset
;
723 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
725 srel
= (srel
>> 8) & 0xff;
726 x
= bfd_get_16 (input_bfd
, contents
);
727 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
728 bfd_put_16 (input_bfd
, x
, contents
);
731 case R_AVR_HH8_LDI_NEG
:
732 contents
+= rel
->r_offset
;
733 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
735 srel
= (srel
>> 16) & 0xff;
736 x
= bfd_get_16 (input_bfd
, contents
);
737 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
738 bfd_put_16 (input_bfd
, x
, contents
);
741 case R_AVR_MS8_LDI_NEG
:
742 contents
+= rel
->r_offset
;
743 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
745 srel
= (srel
>> 24) & 0xff;
746 x
= bfd_get_16 (input_bfd
, contents
);
747 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
748 bfd_put_16 (input_bfd
, x
, contents
);
751 case R_AVR_LO8_LDI_PM
:
752 contents
+= rel
->r_offset
;
753 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
755 return bfd_reloc_outofrange
;
757 x
= bfd_get_16 (input_bfd
, contents
);
758 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
759 bfd_put_16 (input_bfd
, x
, contents
);
762 case R_AVR_HI8_LDI_PM
:
763 contents
+= rel
->r_offset
;
764 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
766 return bfd_reloc_outofrange
;
768 srel
= (srel
>> 8) & 0xff;
769 x
= bfd_get_16 (input_bfd
, contents
);
770 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
771 bfd_put_16 (input_bfd
, x
, contents
);
774 case R_AVR_HH8_LDI_PM
:
775 contents
+= rel
->r_offset
;
776 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
778 return bfd_reloc_outofrange
;
780 srel
= (srel
>> 16) & 0xff;
781 x
= bfd_get_16 (input_bfd
, contents
);
782 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
783 bfd_put_16 (input_bfd
, x
, contents
);
786 case R_AVR_LO8_LDI_PM_NEG
:
787 contents
+= rel
->r_offset
;
788 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
791 return bfd_reloc_outofrange
;
793 x
= bfd_get_16 (input_bfd
, contents
);
794 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
795 bfd_put_16 (input_bfd
, x
, contents
);
798 case R_AVR_HI8_LDI_PM_NEG
:
799 contents
+= rel
->r_offset
;
800 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
803 return bfd_reloc_outofrange
;
805 srel
= (srel
>> 8) & 0xff;
806 x
= bfd_get_16 (input_bfd
, contents
);
807 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
808 bfd_put_16 (input_bfd
, x
, contents
);
811 case R_AVR_HH8_LDI_PM_NEG
:
812 contents
+= rel
->r_offset
;
813 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
816 return bfd_reloc_outofrange
;
818 srel
= (srel
>> 16) & 0xff;
819 x
= bfd_get_16 (input_bfd
, contents
);
820 x
= (x
& 0xf0f0) | (srel
& 0xf) | ((srel
<< 4) & 0xf00);
821 bfd_put_16 (input_bfd
, x
, contents
);
825 contents
+= rel
->r_offset
;
826 srel
= (bfd_signed_vma
) relocation
+ rel
->r_addend
;
828 return bfd_reloc_outofrange
;
830 x
= bfd_get_16 (input_bfd
, contents
);
831 x
|= ((srel
& 0x10000) | ((srel
<< 3) & 0x1f00000)) >> 16;
832 bfd_put_16 (input_bfd
, x
, contents
);
833 bfd_put_16 (input_bfd
, (bfd_vma
) srel
& 0xffff, contents
+2);
837 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
838 contents
, rel
->r_offset
,
839 relocation
, rel
->r_addend
);
845 /* Relocate an AVR ELF section. */
848 elf32_avr_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
849 struct bfd_link_info
*info
,
851 asection
*input_section
,
853 Elf_Internal_Rela
*relocs
,
854 Elf_Internal_Sym
*local_syms
,
855 asection
**local_sections
)
857 Elf_Internal_Shdr
* symtab_hdr
;
858 struct elf_link_hash_entry
** sym_hashes
;
859 Elf_Internal_Rela
* rel
;
860 Elf_Internal_Rela
* relend
;
862 if (info
->relocatable
)
865 symtab_hdr
= & elf_tdata (input_bfd
)->symtab_hdr
;
866 sym_hashes
= elf_sym_hashes (input_bfd
);
867 relend
= relocs
+ input_section
->reloc_count
;
869 for (rel
= relocs
; rel
< relend
; rel
++)
871 reloc_howto_type
* howto
;
872 unsigned long r_symndx
;
873 Elf_Internal_Sym
* sym
;
875 struct elf_link_hash_entry
* h
;
877 bfd_reloc_status_type r
;
881 /* This is a final link. */
882 r_type
= ELF32_R_TYPE (rel
->r_info
);
883 r_symndx
= ELF32_R_SYM (rel
->r_info
);
884 howto
= elf_avr_howto_table
+ ELF32_R_TYPE (rel
->r_info
);
889 if (r_symndx
< symtab_hdr
->sh_info
)
891 sym
= local_syms
+ r_symndx
;
892 sec
= local_sections
[r_symndx
];
893 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
895 name
= bfd_elf_string_from_elf_section
896 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
);
897 name
= (name
== NULL
) ? bfd_section_name (input_bfd
, sec
) : name
;
901 bfd_boolean unresolved_reloc
, warned
;
903 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
904 r_symndx
, symtab_hdr
, sym_hashes
,
906 unresolved_reloc
, warned
);
908 name
= h
->root
.root
.string
;
911 r
= avr_final_link_relocate (howto
, input_bfd
, input_section
,
912 contents
, rel
, relocation
);
914 if (r
!= bfd_reloc_ok
)
916 const char * msg
= (const char *) NULL
;
920 case bfd_reloc_overflow
:
921 r
= info
->callbacks
->reloc_overflow
922 (info
, (h
? &h
->root
: NULL
),
923 name
, howto
->name
, (bfd_vma
) 0,
924 input_bfd
, input_section
, rel
->r_offset
);
927 case bfd_reloc_undefined
:
928 r
= info
->callbacks
->undefined_symbol
929 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
932 case bfd_reloc_outofrange
:
933 msg
= _("internal error: out of range error");
936 case bfd_reloc_notsupported
:
937 msg
= _("internal error: unsupported relocation error");
940 case bfd_reloc_dangerous
:
941 msg
= _("internal error: dangerous relocation");
945 msg
= _("internal error: unknown error");
950 r
= info
->callbacks
->warning
951 (info
, msg
, name
, input_bfd
, input_section
, rel
->r_offset
);
961 /* The final processing done just before writing out a AVR ELF object
962 file. This gets the AVR architecture right based on the machine
966 bfd_elf_avr_final_write_processing (bfd
*abfd
,
967 bfd_boolean linker ATTRIBUTE_UNUSED
)
971 switch (bfd_get_mach (abfd
))
975 val
= E_AVR_MACH_AVR2
;
979 val
= E_AVR_MACH_AVR1
;
983 val
= E_AVR_MACH_AVR3
;
987 val
= E_AVR_MACH_AVR4
;
991 val
= E_AVR_MACH_AVR5
;
995 elf_elfheader (abfd
)->e_machine
= EM_AVR
;
996 elf_elfheader (abfd
)->e_flags
&= ~ EF_AVR_MACH
;
997 elf_elfheader (abfd
)->e_flags
|= val
;
998 elf_elfheader (abfd
)->e_flags
|= EF_AVR_LINKRELAX_PREPARED
;
1001 /* Set the right machine number. */
1004 elf32_avr_object_p (bfd
*abfd
)
1006 unsigned int e_set
= bfd_mach_avr2
;
1008 if (elf_elfheader (abfd
)->e_machine
== EM_AVR
1009 || elf_elfheader (abfd
)->e_machine
== EM_AVR_OLD
)
1011 int e_mach
= elf_elfheader (abfd
)->e_flags
& EF_AVR_MACH
;
1016 case E_AVR_MACH_AVR2
:
1017 e_set
= bfd_mach_avr2
;
1020 case E_AVR_MACH_AVR1
:
1021 e_set
= bfd_mach_avr1
;
1024 case E_AVR_MACH_AVR3
:
1025 e_set
= bfd_mach_avr3
;
1028 case E_AVR_MACH_AVR4
:
1029 e_set
= bfd_mach_avr4
;
1032 case E_AVR_MACH_AVR5
:
1033 e_set
= bfd_mach_avr5
;
1037 return bfd_default_set_arch_mach (abfd
, bfd_arch_avr
,
1042 /* Enable debugging printout at stdout with a value of 1. */
1043 #define DEBUG_RELAX 0
1045 /* Delete some bytes from a section while changing the size of an instruction.
1046 The parameter "addr" denotes the section-relative offset pointing just
1047 behind the shrinked instruction. "addr+count" point at the first
1048 byte just behind the original unshrinked instruction. */
1051 elf32_avr_relax_delete_bytes (bfd
*abfd
,
1056 Elf_Internal_Shdr
*symtab_hdr
;
1057 unsigned int sec_shndx
;
1059 Elf_Internal_Rela
*irel
, *irelend
;
1060 Elf_Internal_Rela
*irelalign
;
1061 Elf_Internal_Sym
*isym
;
1062 Elf_Internal_Sym
*isymbuf
= NULL
;
1063 Elf_Internal_Sym
*isymend
;
1065 struct elf_link_hash_entry
**sym_hashes
;
1066 struct elf_link_hash_entry
**end_hashes
;
1067 unsigned int symcount
;
1069 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1070 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
1071 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1073 /* The deletion must stop at the next ALIGN reloc for an aligment
1074 power larger than the number of bytes we are deleting. */
1079 irel
= elf_section_data (sec
)->relocs
;
1080 irelend
= irel
+ sec
->reloc_count
;
1082 /* Actually delete the bytes. */
1083 if (toaddr
- addr
- count
> 0)
1084 memmove (contents
+ addr
, contents
+ addr
+ count
,
1085 (size_t) (toaddr
- addr
- count
));
1088 /* Adjust all the relocs. */
1089 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
1092 bfd_vma old_reloc_address
;
1093 bfd_vma shrinked_insn_address
;
1095 old_reloc_address
= (sec
->output_section
->vma
1096 + sec
->output_offset
+ irel
->r_offset
);
1097 shrinked_insn_address
= (sec
->output_section
->vma
1098 + sec
->output_offset
+ addr
- count
);
1100 /* Get the new reloc address. */
1101 if ((irel
->r_offset
> addr
1102 && irel
->r_offset
< toaddr
))
1105 printf ("Relocation at address 0x%x needs to be moved.\n"
1106 "Old section offset: 0x%x, New section offset: 0x%x \n",
1107 (unsigned int) old_reloc_address
,
1108 (unsigned int) irel
->r_offset
,
1109 (unsigned int) ((irel
->r_offset
) - count
));
1111 irel
->r_offset
-= count
;
1114 /* The reloc's own addresses are now ok. However, we need to readjust
1115 the reloc's addend if two conditions are met:
1116 1.) the reloc is relative to a symbol in this section that
1117 is located in front of the shrinked instruction
1118 2.) symbol plus addend end up behind the shrinked instruction.
1120 This should happen only for local symbols that are progmem related. */
1122 /* Read this BFD's local symbols if we haven't done so already. */
1123 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1125 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1126 if (isymbuf
== NULL
)
1127 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1128 symtab_hdr
->sh_info
, 0,
1130 if (isymbuf
== NULL
)
1134 /* Get the value of the symbol referred to by the reloc. */
1135 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1137 /* A local symbol. */
1138 Elf_Internal_Sym
*isym
;
1141 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1142 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1143 symval
= isym
->st_value
;
1144 /* If the reloc is absolute, it will not have
1145 a symbol or section associated with it. */
1148 symval
+= sym_sec
->output_section
->vma
1149 + sym_sec
->output_offset
;
1152 printf ("Checking if the relocation's "
1153 "addend needs corrections.\n"
1154 "Address of anchor symbol: 0x%x \n"
1155 "Address of relocation target: 0x%x \n"
1156 "Address of relaxed insn: 0x%x \n",
1157 (unsigned int) symval
,
1158 (unsigned int) (symval
+ irel
->r_addend
),
1159 (unsigned int) shrinked_insn_address
);
1161 if (symval
<= shrinked_insn_address
1162 && (symval
+ irel
->r_addend
) > shrinked_insn_address
)
1164 irel
->r_addend
-= count
;
1167 printf ("Anchor symbol and relocation target bracket "
1168 "shrinked insn address.\n"
1169 "Need for new addend : 0x%x\n",
1170 (unsigned int) irel
->r_addend
);
1173 /* else ... Reference symbol is absolute. No adjustment needed. */
1175 /* else ... Reference symbol is extern. No need for adjusting the addend. */
1178 /* Adjust the local symbols defined in this section. */
1179 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1180 isymend
= isym
+ symtab_hdr
->sh_info
;
1181 for (; isym
< isymend
; isym
++)
1183 if (isym
->st_shndx
== sec_shndx
1184 && isym
->st_value
> addr
1185 && isym
->st_value
< toaddr
)
1186 isym
->st_value
-= count
;
1189 /* Now adjust the global symbols defined in this section. */
1190 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
1191 - symtab_hdr
->sh_info
);
1192 sym_hashes
= elf_sym_hashes (abfd
);
1193 end_hashes
= sym_hashes
+ symcount
;
1194 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1196 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
1197 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1198 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
1199 && sym_hash
->root
.u
.def
.section
== sec
1200 && sym_hash
->root
.u
.def
.value
> addr
1201 && sym_hash
->root
.u
.def
.value
< toaddr
)
1203 sym_hash
->root
.u
.def
.value
-= count
;
1210 /* This function handles relaxing for the avr.
1211 Many important relaxing opportunities within functions are already
1212 realized by the compiler itself.
1213 Here we try to replace call (4 bytes) -> rcall (2 bytes)
1214 and jump -> rjmp (safes also 2 bytes).
1215 As well we now optimize seqences of
1216 - call/rcall function
1221 . In case that within a sequence
1224 the ret could no longer be reached it is optimized away. In order
1225 to check if the ret is no longer needed, it is checked that the ret's address
1226 is not the target of a branch or jump within the same section, it is checked
1227 that there is no skip instruction before the jmp/rjmp and that there
1228 is no local or global label place at the address of the ret.
1230 We refrain from relaxing within sections ".vectors" and
1231 ".jumptables" in order to maintain the position of the instructions.
1232 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
1233 if possible. (In future one could possibly use the space of the nop
1234 for the first instruction of the irq service function.
1236 The .jumptables sections is meant to be used for a future tablejump variant
1237 for the devices with 3-byte program counter where the table itself
1238 contains 4-byte jump instructions whose relative offset must not
1242 elf32_avr_relax_section (bfd
*abfd
,
1244 struct bfd_link_info
*link_info
,
1247 Elf_Internal_Shdr
*symtab_hdr
;
1248 Elf_Internal_Rela
*internal_relocs
;
1249 Elf_Internal_Rela
*irel
, *irelend
;
1250 bfd_byte
*contents
= NULL
;
1251 Elf_Internal_Sym
*isymbuf
= NULL
;
1252 static asection
*last_input_section
= NULL
;
1253 static Elf_Internal_Rela
*last_reloc
= NULL
;
1255 /* Assume nothing changes. */
1258 /* We don't have to do anything for a relocatable link, if
1259 this section does not have relocs, or if this is not a
1261 if (link_info
->relocatable
1262 || (sec
->flags
& SEC_RELOC
) == 0
1263 || sec
->reloc_count
== 0
1264 || (sec
->flags
& SEC_CODE
) == 0)
1267 /* Check if the object file to relax uses internal symbols so that we
1268 could fix up the relocations. */
1269 if (!(elf_elfheader (abfd
)->e_flags
& EF_AVR_LINKRELAX_PREPARED
))
1272 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1274 /* Get a copy of the native relocations. */
1275 internal_relocs
= (_bfd_elf_link_read_relocs
1276 (abfd
, sec
, NULL
, NULL
, link_info
->keep_memory
));
1277 if (internal_relocs
== NULL
)
1280 if (sec
!= last_input_section
)
1283 last_input_section
= sec
;
1285 /* Walk through the relocs looking for relaxing opportunities. */
1286 irelend
= internal_relocs
+ sec
->reloc_count
;
1287 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
1291 if ( ELF32_R_TYPE (irel
->r_info
) != R_AVR_13_PCREL
1292 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_7_PCREL
1293 && ELF32_R_TYPE (irel
->r_info
) != R_AVR_CALL
)
1296 /* Get the section contents if we haven't done so already. */
1297 if (contents
== NULL
)
1299 /* Get cached copy if it exists. */
1300 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
1301 contents
= elf_section_data (sec
)->this_hdr
.contents
;
1304 /* Go get them off disk. */
1305 if (! bfd_malloc_and_get_section (abfd
, sec
, &contents
))
1310 /* Read this BFD's local symbols if we haven't done so already. */
1311 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1313 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1314 if (isymbuf
== NULL
)
1315 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
1316 symtab_hdr
->sh_info
, 0,
1318 if (isymbuf
== NULL
)
1323 /* Get the value of the symbol referred to by the reloc. */
1324 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
1326 /* A local symbol. */
1327 Elf_Internal_Sym
*isym
;
1330 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1331 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1332 symval
= isym
->st_value
;
1333 /* If the reloc is absolute, it will not have
1334 a symbol or section associated with it. */
1336 symval
+= sym_sec
->output_section
->vma
1337 + sym_sec
->output_offset
;
1342 struct elf_link_hash_entry
*h
;
1344 /* An external symbol. */
1345 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
1346 h
= elf_sym_hashes (abfd
)[indx
];
1347 BFD_ASSERT (h
!= NULL
);
1348 if (h
->root
.type
!= bfd_link_hash_defined
1349 && h
->root
.type
!= bfd_link_hash_defweak
)
1350 /* This appears to be a reference to an undefined
1351 symbol. Just ignore it--it will be caught by the
1352 regular reloc processing. */
1355 symval
= (h
->root
.u
.def
.value
1356 + h
->root
.u
.def
.section
->output_section
->vma
1357 + h
->root
.u
.def
.section
->output_offset
);
1360 /* For simplicity of coding, we are going to modify the section
1361 contents, the section relocs, and the BFD symbol table. We
1362 must tell the rest of the code not to free up this
1363 information. It would be possible to instead create a table
1364 of changes which have to be made, as is done in coff-mips.c;
1365 that would be more work, but would require less memory when
1366 the linker is run. */
1367 switch (ELF32_R_TYPE (irel
->r_info
))
1369 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1370 pc-relative rcall/rjmp. */
1373 bfd_vma value
= symval
+ irel
->r_addend
;
1375 int distance_short_enough
= 0;
1377 /* Get the address of this instruction. */
1378 dot
= (sec
->output_section
->vma
1379 + sec
->output_offset
+ irel
->r_offset
);
1381 /* Compute the distance from this insn to the branch target. */
1384 /* If the distance is within -4094..+4098 inclusive, then we can
1385 relax this jump/call. +4098 because the call/jump target
1386 will be closer after the relaxation. */
1387 if ((int) gap
>= -4094 && (int) gap
<= 4098)
1388 distance_short_enough
= 1;
1390 /* Here we handle the wrap-around case. E.g. for a 16k device
1391 we could use a rjmp to jump from address 0x100 to 0x3d00!
1392 In order to make this work properly, we need to fill the
1393 vaiable avr_pc_wrap_around with the appropriate value.
1394 I.e. 0x4000 for a 16k device. */
1396 /* Shrinking the code size makes the gaps larger in the
1397 case of wrap-arounds. So we use a heuristical safety
1398 margin to avoid that during relax the distance gets
1399 again too large for the short jumps. Let's assume
1400 a typical code-size reduction due to relax for a
1401 16k device of 600 bytes. So let's use twice the
1402 typical value as safety margin. */
1406 int assumed_shrink
= 600;
1407 if (avr_pc_wrap_around
> 0x4000)
1408 assumed_shrink
= 900;
1410 safety_margin
= 2 * assumed_shrink
;
1412 rgap
= avr_relative_distance_considering_wrap_around (gap
);
1414 if (rgap
>= (-4092 + safety_margin
)
1415 && rgap
<= (4094 - safety_margin
))
1416 distance_short_enough
= 1;
1419 if (distance_short_enough
)
1421 unsigned char code_msb
;
1422 unsigned char code_lsb
;
1425 printf ("shrinking jump/call instruction at address 0x%x"
1426 " in section %s\n\n",
1427 (int) dot
, sec
->name
);
1429 /* Note that we've changed the relocs, section contents,
1431 elf_section_data (sec
)->relocs
= internal_relocs
;
1432 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1433 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1435 /* Get the instruction code for relaxing. */
1436 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
);
1437 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1439 /* Mask out the relocation bits. */
1442 if (code_msb
== 0x94 && code_lsb
== 0x0E)
1444 /* we are changing call -> rcall . */
1445 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
1446 bfd_put_8 (abfd
, 0xD0, contents
+ irel
->r_offset
+ 1);
1448 else if (code_msb
== 0x94 && code_lsb
== 0x0C)
1450 /* we are changeing jump -> rjmp. */
1451 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
);
1452 bfd_put_8 (abfd
, 0xC0, contents
+ irel
->r_offset
+ 1);
1457 /* Fix the relocation's type. */
1458 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1461 /* Check for the vector section. There we don't want to
1462 modify the ordering! */
1464 if (!strcmp (sec
->name
,".vectors")
1465 || !strcmp (sec
->name
,".jumptables"))
1467 /* Let's insert a nop. */
1468 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 2);
1469 bfd_put_8 (abfd
, 0x00, contents
+ irel
->r_offset
+ 3);
1473 /* Delete two bytes of data. */
1474 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
1475 irel
->r_offset
+ 2, 2))
1478 /* That will change things, so, we should relax again.
1479 Note that this is not required, and it may be slow. */
1487 unsigned char code_msb
;
1488 unsigned char code_lsb
;
1491 code_msb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
1492 code_lsb
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 0);
1494 /* Get the address of this instruction. */
1495 dot
= (sec
->output_section
->vma
1496 + sec
->output_offset
+ irel
->r_offset
);
1498 /* Here we look for rcall/ret or call/ret sequences that could be
1499 safely replaced by rjmp/ret or jmp/ret */
1500 if (0xd0 == (code_msb
& 0xf0))
1502 /* This insn is a rcall. */
1503 unsigned char next_insn_msb
= 0;
1504 unsigned char next_insn_lsb
= 0;
1506 if (irel
->r_offset
+ 3 < sec
->size
)
1509 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 3);
1511 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 2);
1514 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1516 /* The next insn is a ret. We now convert the rcall insn
1517 into a rjmp instruction. */
1519 bfd_put_8 (abfd
, code_msb
, contents
+ irel
->r_offset
+ 1);
1521 printf ("converted rcall/ret sequence at address 0x%x"
1522 " into rjmp/ret sequence. Section is %s\n\n",
1523 (int) dot
, sec
->name
);
1528 else if ((0x94 == (code_msb
& 0xfe))
1529 && (0x0e == (code_lsb
& 0x0e)))
1531 /* This insn is a call. */
1532 unsigned char next_insn_msb
= 0;
1533 unsigned char next_insn_lsb
= 0;
1535 if (irel
->r_offset
+ 5 < sec
->size
)
1538 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 5);
1540 bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 4);
1543 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1545 /* The next insn is a ret. We now convert the call insn
1546 into a jmp instruction. */
1549 bfd_put_8 (abfd
, code_lsb
, contents
+ irel
->r_offset
);
1551 printf ("converted call/ret sequence at address 0x%x"
1552 " into jmp/ret sequence. Section is %s\n\n",
1553 (int) dot
, sec
->name
);
1558 else if ((0xc0 == (code_msb
& 0xf0))
1559 || ((0x94 == (code_msb
& 0xfe))
1560 && (0x0c == (code_lsb
& 0x0e))))
1562 /* This insn is a rjmp or a jmp. */
1563 unsigned char next_insn_msb
= 0;
1564 unsigned char next_insn_lsb
= 0;
1567 if (0xc0 == (code_msb
& 0xf0))
1568 insn_size
= 2; /* rjmp insn */
1570 insn_size
= 4; /* jmp insn */
1572 if (irel
->r_offset
+ insn_size
+ 1 < sec
->size
)
1575 bfd_get_8 (abfd
, contents
+ irel
->r_offset
1578 bfd_get_8 (abfd
, contents
+ irel
->r_offset
1582 if ((0x95 == next_insn_msb
) && (0x08 == next_insn_lsb
))
1584 /* The next insn is a ret. We possibly could delete
1585 this ret. First we need to check for preceeding
1586 sbis/sbic/sbrs or cpse "skip" instructions. */
1588 int there_is_preceeding_non_skip_insn
= 1;
1589 bfd_vma address_of_ret
;
1591 address_of_ret
= dot
+ insn_size
;
1593 if (DEBUG_RELAX
&& (insn_size
== 2))
1594 printf ("found rjmp / ret sequence at address 0x%x\n",
1596 if (DEBUG_RELAX
&& (insn_size
== 4))
1597 printf ("found jmp / ret sequence at address 0x%x\n",
1600 /* We have to make sure that there is a preceeding insn. */
1601 if (irel
->r_offset
>= 2)
1603 unsigned char preceeding_msb
;
1604 unsigned char preceeding_lsb
;
1606 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
1608 bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
1611 if (0x99 == preceeding_msb
)
1612 there_is_preceeding_non_skip_insn
= 0;
1615 if (0x9b == preceeding_msb
)
1616 there_is_preceeding_non_skip_insn
= 0;
1619 if ((0xfc == (preceeding_msb
& 0xfe)
1620 && (0x00 == (preceeding_lsb
& 0x08))))
1621 there_is_preceeding_non_skip_insn
= 0;
1624 if ((0xfe == (preceeding_msb
& 0xfe)
1625 && (0x00 == (preceeding_lsb
& 0x08))))
1626 there_is_preceeding_non_skip_insn
= 0;
1629 if (0x10 == (preceeding_msb
& 0xfc))
1630 there_is_preceeding_non_skip_insn
= 0;
1632 if (there_is_preceeding_non_skip_insn
== 0)
1634 printf ("preceeding skip insn prevents deletion of"
1635 " ret insn at addr 0x%x in section %s\n",
1636 (int) dot
+ 2, sec
->name
);
1640 /* There is no previous instruction. */
1641 there_is_preceeding_non_skip_insn
= 0;
1644 if (there_is_preceeding_non_skip_insn
)
1646 /* We now only have to make sure that there is no
1647 local label defined at the address of the ret
1648 instruction and that there is no local relocation
1649 in this section pointing to the ret. */
1651 int deleting_ret_is_safe
= 1;
1652 unsigned int section_offset_of_ret_insn
=
1653 irel
->r_offset
+ insn_size
;
1654 Elf_Internal_Sym
*isym
, *isymend
;
1655 unsigned int sec_shndx
;
1658 _bfd_elf_section_from_bfd_section (abfd
, sec
);
1660 /* Check for local symbols. */
1661 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1662 isymend
= isym
+ symtab_hdr
->sh_info
;
1663 for (; isym
< isymend
; isym
++)
1665 if (isym
->st_value
== section_offset_of_ret_insn
1666 && isym
->st_shndx
== sec_shndx
)
1668 deleting_ret_is_safe
= 0;
1670 printf ("local label prevents deletion of ret "
1671 "insn at address 0x%x\n",
1672 (int) dot
+ insn_size
);
1676 /* Now check for global symbols. */
1679 struct elf_link_hash_entry
**sym_hashes
;
1680 struct elf_link_hash_entry
**end_hashes
;
1682 symcount
= (symtab_hdr
->sh_size
1683 / sizeof (Elf32_External_Sym
)
1684 - symtab_hdr
->sh_info
);
1685 sym_hashes
= elf_sym_hashes (abfd
);
1686 end_hashes
= sym_hashes
+ symcount
;
1687 for (; sym_hashes
< end_hashes
; sym_hashes
++)
1689 struct elf_link_hash_entry
*sym_hash
=
1691 if ((sym_hash
->root
.type
== bfd_link_hash_defined
1692 || sym_hash
->root
.type
==
1693 bfd_link_hash_defweak
)
1694 && sym_hash
->root
.u
.def
.section
== sec
1695 && sym_hash
->root
.u
.def
.value
== section_offset_of_ret_insn
)
1697 deleting_ret_is_safe
= 0;
1699 printf ("global label prevents deletion of "
1700 "ret insn at address 0x%x\n",
1701 (int) dot
+ insn_size
);
1705 /* Now we check for relocations pointing to ret. */
1707 Elf_Internal_Rela
*irel
;
1708 Elf_Internal_Rela
*relend
;
1709 Elf_Internal_Shdr
*symtab_hdr
;
1711 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
1712 relend
= elf_section_data (sec
)->relocs
1715 for (irel
= elf_section_data (sec
)->relocs
;
1716 irel
< relend
; irel
++)
1718 bfd_vma reloc_target
= 0;
1720 Elf_Internal_Sym
*isymbuf
= NULL
;
1722 /* Read this BFD's local symbols if we haven't
1724 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
1726 isymbuf
= (Elf_Internal_Sym
*)
1727 symtab_hdr
->contents
;
1728 if (isymbuf
== NULL
)
1729 isymbuf
= bfd_elf_get_elf_syms
1732 symtab_hdr
->sh_info
, 0,
1734 if (isymbuf
== NULL
)
1738 /* Get the value of the symbol referred to
1740 if (ELF32_R_SYM (irel
->r_info
)
1741 < symtab_hdr
->sh_info
)
1743 /* A local symbol. */
1744 Elf_Internal_Sym
*isym
;
1748 + ELF32_R_SYM (irel
->r_info
);
1749 sym_sec
= bfd_section_from_elf_index
1750 (abfd
, isym
->st_shndx
);
1751 symval
= isym
->st_value
;
1753 /* If the reloc is absolute, it will not
1754 have a symbol or section associated
1760 sym_sec
->output_section
->vma
1761 + sym_sec
->output_offset
;
1762 reloc_target
= symval
+ irel
->r_addend
;
1766 reloc_target
= symval
+ irel
->r_addend
;
1767 /* Reference symbol is absolute. */
1770 /* else ... reference symbol is extern. */
1772 if (address_of_ret
== reloc_target
)
1774 deleting_ret_is_safe
= 0;
1777 "rjmp/jmp ret sequence at address"
1778 " 0x%x could not be deleted. ret"
1779 " is target of a relocation.\n",
1780 (int) address_of_ret
);
1785 if (deleting_ret_is_safe
)
1788 printf ("unreachable ret instruction "
1789 "at address 0x%x deleted.\n",
1790 (int) dot
+ insn_size
);
1792 /* Delete two bytes of data. */
1793 if (!elf32_avr_relax_delete_bytes (abfd
, sec
,
1794 irel
->r_offset
+ insn_size
, 2))
1797 /* That will change things, so, we should relax
1798 again. Note that this is not required, and it
1812 if (contents
!= NULL
1813 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1815 if (! link_info
->keep_memory
)
1819 /* Cache the section contents for elf_link_input_bfd. */
1820 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1824 if (internal_relocs
!= NULL
1825 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1826 free (internal_relocs
);
1832 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1834 if (contents
!= NULL
1835 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1837 if (internal_relocs
!= NULL
1838 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1839 free (internal_relocs
);
1844 /* This is a version of bfd_generic_get_relocated_section_contents
1845 which uses elf32_avr_relocate_section.
1847 For avr it's essentially a cut and paste taken from the H8300 port.
1848 The author of the relaxation support patch for avr had absolutely no
1849 clue what is happening here but found out that this part of the code
1850 seems to be important. */
1853 elf32_avr_get_relocated_section_contents (bfd
*output_bfd
,
1854 struct bfd_link_info
*link_info
,
1855 struct bfd_link_order
*link_order
,
1857 bfd_boolean relocatable
,
1860 Elf_Internal_Shdr
*symtab_hdr
;
1861 asection
*input_section
= link_order
->u
.indirect
.section
;
1862 bfd
*input_bfd
= input_section
->owner
;
1863 asection
**sections
= NULL
;
1864 Elf_Internal_Rela
*internal_relocs
= NULL
;
1865 Elf_Internal_Sym
*isymbuf
= NULL
;
1867 /* We only need to handle the case of relaxing, or of having a
1868 particular set of section contents, specially. */
1870 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
1871 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
1875 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1877 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
1878 (size_t) input_section
->size
);
1880 if ((input_section
->flags
& SEC_RELOC
) != 0
1881 && input_section
->reloc_count
> 0)
1884 Elf_Internal_Sym
*isym
, *isymend
;
1887 internal_relocs
= (_bfd_elf_link_read_relocs
1888 (input_bfd
, input_section
, NULL
, NULL
, FALSE
));
1889 if (internal_relocs
== NULL
)
1892 if (symtab_hdr
->sh_info
!= 0)
1894 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1895 if (isymbuf
== NULL
)
1896 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1897 symtab_hdr
->sh_info
, 0,
1899 if (isymbuf
== NULL
)
1903 amt
= symtab_hdr
->sh_info
;
1904 amt
*= sizeof (asection
*);
1905 sections
= bfd_malloc (amt
);
1906 if (sections
== NULL
&& amt
!= 0)
1909 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
1910 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
1914 if (isym
->st_shndx
== SHN_UNDEF
)
1915 isec
= bfd_und_section_ptr
;
1916 else if (isym
->st_shndx
== SHN_ABS
)
1917 isec
= bfd_abs_section_ptr
;
1918 else if (isym
->st_shndx
== SHN_COMMON
)
1919 isec
= bfd_com_section_ptr
;
1921 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
1926 if (! elf32_avr_relocate_section (output_bfd
, link_info
, input_bfd
,
1927 input_section
, data
, internal_relocs
,
1931 if (sections
!= NULL
)
1934 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1936 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
1937 free (internal_relocs
);
1943 if (sections
!= NULL
)
1946 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1948 if (internal_relocs
!= NULL
1949 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
1950 free (internal_relocs
);
1955 #define ELF_ARCH bfd_arch_avr
1956 #define ELF_MACHINE_CODE EM_AVR
1957 #define ELF_MACHINE_ALT1 EM_AVR_OLD
1958 #define ELF_MAXPAGESIZE 1
1960 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
1961 #define TARGET_LITTLE_NAME "elf32-avr"
1963 #define elf_info_to_howto avr_info_to_howto_rela
1964 #define elf_info_to_howto_rel NULL
1965 #define elf_backend_relocate_section elf32_avr_relocate_section
1966 #define elf_backend_gc_mark_hook elf32_avr_gc_mark_hook
1967 #define elf_backend_gc_sweep_hook elf32_avr_gc_sweep_hook
1968 #define elf_backend_check_relocs elf32_avr_check_relocs
1969 #define elf_backend_can_gc_sections 1
1970 #define elf_backend_rela_normal 1
1971 #define elf_backend_final_write_processing \
1972 bfd_elf_avr_final_write_processing
1973 #define elf_backend_object_p elf32_avr_object_p
1975 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
1976 #define bfd_elf32_bfd_get_relocated_section_contents \
1977 elf32_avr_get_relocated_section_contents
1979 #include "elf32-target.h"