[ gas/ChangeLog ]
[binutils-gdb.git] / bfd / elf32-avr.c
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>
5
6 This file is part of BFD, the Binary File Descriptor library.
7
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.
12
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.
17
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. */
22
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
26 #include "elf-bfd.h"
27 #include "elf/avr.h"
28
29 static reloc_howto_type elf_avr_howto_table[] =
30 {
31 HOWTO (R_AVR_NONE, /* type */
32 0, /* rightshift */
33 2, /* size (0 = byte, 1 = short, 2 = long) */
34 32, /* bitsize */
35 FALSE, /* pc_relative */
36 0, /* bitpos */
37 complain_overflow_bitfield, /* complain_on_overflow */
38 bfd_elf_generic_reloc, /* special_function */
39 "R_AVR_NONE", /* name */
40 FALSE, /* partial_inplace */
41 0, /* src_mask */
42 0, /* dst_mask */
43 FALSE), /* pcrel_offset */
44
45 HOWTO (R_AVR_32, /* type */
46 0, /* rightshift */
47 2, /* size (0 = byte, 1 = short, 2 = long) */
48 32, /* bitsize */
49 FALSE, /* pc_relative */
50 0, /* bitpos */
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 */
58
59 /* A 7 bit PC relative relocation. */
60 HOWTO (R_AVR_7_PCREL, /* type */
61 1, /* rightshift */
62 1, /* size (0 = byte, 1 = short, 2 = long) */
63 7, /* bitsize */
64 TRUE, /* pc_relative */
65 3, /* bitpos */
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 */
73
74 /* A 13 bit PC relative relocation. */
75 HOWTO (R_AVR_13_PCREL, /* type */
76 1, /* rightshift */
77 1, /* size (0 = byte, 1 = short, 2 = long) */
78 13, /* bitsize */
79 TRUE, /* pc_relative */
80 0, /* bitpos */
81 complain_overflow_bitfield, /* complain_on_overflow */
82 bfd_elf_generic_reloc, /* special_function */
83 "R_AVR_13_PCREL", /* name */
84 FALSE, /* partial_inplace */
85 0xfff, /* src_mask */
86 0xfff, /* dst_mask */
87 TRUE), /* pcrel_offset */
88
89 /* A 16 bit absolute relocation. */
90 HOWTO (R_AVR_16, /* type */
91 0, /* rightshift */
92 1, /* size (0 = byte, 1 = short, 2 = long) */
93 16, /* bitsize */
94 FALSE, /* pc_relative */
95 0, /* bitpos */
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 */
103
104 /* A 16 bit absolute relocation for command address. */
105 HOWTO (R_AVR_16_PM, /* type */
106 1, /* rightshift */
107 1, /* size (0 = byte, 1 = short, 2 = long) */
108 16, /* bitsize */
109 FALSE, /* pc_relative */
110 0, /* bitpos */
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.
119 For LDI command. */
120 HOWTO (R_AVR_LO8_LDI, /* type */
121 0, /* rightshift */
122 1, /* size (0 = byte, 1 = short, 2 = long) */
123 8, /* bitsize */
124 FALSE, /* pc_relative */
125 0, /* bitpos */
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.
134 For LDI command. */
135 HOWTO (R_AVR_HI8_LDI, /* type */
136 8, /* rightshift */
137 1, /* size (0 = byte, 1 = short, 2 = long) */
138 8, /* bitsize */
139 FALSE, /* pc_relative */
140 0, /* bitpos */
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 */
152 16, /* rightshift */
153 1, /* size (0 = byte, 1 = short, 2 = long) */
154 8, /* bitsize */
155 FALSE, /* pc_relative */
156 0, /* bitpos */
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.
165 For LDI command. */
166 HOWTO (R_AVR_LO8_LDI_NEG, /* type */
167 0, /* rightshift */
168 1, /* size (0 = byte, 1 = short, 2 = long) */
169 8, /* bitsize */
170 FALSE, /* pc_relative */
171 0, /* bitpos */
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.
180 For LDI command. */
181 HOWTO (R_AVR_HI8_LDI_NEG, /* type */
182 8, /* rightshift */
183 1, /* size (0 = byte, 1 = short, 2 = long) */
184 8, /* bitsize */
185 FALSE, /* pc_relative */
186 0, /* bitpos */
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.
195 For LDI command. */
196 HOWTO (R_AVR_HH8_LDI_NEG, /* type */
197 16, /* rightshift */
198 1, /* size (0 = byte, 1 = short, 2 = long) */
199 8, /* bitsize */
200 FALSE, /* pc_relative */
201 0, /* bitpos */
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.
210 For LDI command. */
211 HOWTO (R_AVR_LO8_LDI_PM, /* type */
212 1, /* rightshift */
213 1, /* size (0 = byte, 1 = short, 2 = long) */
214 8, /* bitsize */
215 FALSE, /* pc_relative */
216 0, /* bitpos */
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.
225 For LDI command. */
226 HOWTO (R_AVR_HI8_LDI_PM, /* type */
227 9, /* rightshift */
228 1, /* size (0 = byte, 1 = short, 2 = long) */
229 8, /* bitsize */
230 FALSE, /* pc_relative */
231 0, /* bitpos */
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.
240 For LDI command. */
241 HOWTO (R_AVR_HH8_LDI_PM, /* type */
242 17, /* rightshift */
243 1, /* size (0 = byte, 1 = short, 2 = long) */
244 8, /* bitsize */
245 FALSE, /* pc_relative */
246 0, /* bitpos */
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 */
257 1, /* rightshift */
258 1, /* size (0 = byte, 1 = short, 2 = long) */
259 8, /* bitsize */
260 FALSE, /* pc_relative */
261 0, /* bitpos */
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 */
272 9, /* rightshift */
273 1, /* size (0 = byte, 1 = short, 2 = long) */
274 8, /* bitsize */
275 FALSE, /* pc_relative */
276 0, /* bitpos */
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 */
287 17, /* rightshift */
288 1, /* size (0 = byte, 1 = short, 2 = long) */
289 8, /* bitsize */
290 FALSE, /* pc_relative */
291 0, /* bitpos */
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 */
301 1, /* rightshift */
302 2, /* size (0 = byte, 1 = short, 2 = long) */
303 23, /* bitsize */
304 FALSE, /* pc_relative */
305 0, /* bitpos */
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.
314 For LDI command. */
315 HOWTO (R_AVR_LDI, /* type */
316 0, /* rightshift */
317 1, /* size (0 = byte, 1 = short, 2 = long) */
318 16, /* bitsize */
319 FALSE, /* pc_relative */
320 0, /* bitpos */
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 */
331 0, /* rightshift */
332 0, /* size (0 = byte, 1 = short, 2 = long) */
333 6, /* bitsize */
334 FALSE, /* pc_relative */
335 0, /* bitpos */
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 */
346 0, /* rightshift */
347 0, /* size (0 = byte, 1 = short, 2 = long) */
348 6, /* bitsize */
349 FALSE, /* pc_relative */
350 0, /* bitpos */
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 */
360 24, /* rightshift */
361 1, /* size (0 = byte, 1 = short, 2 = long) */
362 8, /* bitsize */
363 FALSE, /* pc_relative */
364 0, /* bitpos */
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 */
374 24, /* rightshift */
375 1, /* size (0 = byte, 1 = short, 2 = long) */
376 8, /* bitsize */
377 FALSE, /* pc_relative */
378 0, /* bitpos */
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 */
386 };
387
388 /* Map BFD reloc types to AVR ELF reloc types. */
389
390 struct avr_reloc_map
391 {
392 bfd_reloc_code_real_type bfd_reloc_val;
393 unsigned int elf_reloc_val;
394 };
395
396 static const struct avr_reloc_map avr_reloc_map[] =
397 {
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 }
422 };
423
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.
427
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. */
432
433 unsigned int avr_pc_wrap_around = 0x10000000;
434
435 /* Calculates the effective distance of a pc relative jump/call. */
436
437 static int
438 avr_relative_distance_considering_wrap_around (unsigned int distance)
439 {
440 unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
441 int dist_with_wrap_around = distance & wrap_around_mask;
442
443 if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
444 dist_with_wrap_around -= avr_pc_wrap_around;
445
446 return dist_with_wrap_around;
447 }
448
449
450 static reloc_howto_type *
451 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
452 bfd_reloc_code_real_type code)
453 {
454 unsigned int i;
455
456 for (i = 0;
457 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
458 i++)
459 if (avr_reloc_map[i].bfd_reloc_val == code)
460 return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
461
462 return NULL;
463 }
464
465 /* Set the howto pointer for an AVR ELF reloc. */
466
467 static void
468 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
469 arelent *cache_ptr,
470 Elf_Internal_Rela *dst)
471 {
472 unsigned int r_type;
473
474 r_type = ELF32_R_TYPE (dst->r_info);
475 BFD_ASSERT (r_type < (unsigned int) R_AVR_max);
476 cache_ptr->howto = &elf_avr_howto_table[r_type];
477 }
478
479 static asection *
480 elf32_avr_gc_mark_hook (asection *sec,
481 struct bfd_link_info *info ATTRIBUTE_UNUSED,
482 Elf_Internal_Rela *rel,
483 struct elf_link_hash_entry *h,
484 Elf_Internal_Sym *sym)
485 {
486 if (h != NULL)
487 {
488 switch (ELF32_R_TYPE (rel->r_info))
489 {
490 default:
491 switch (h->root.type)
492 {
493 case bfd_link_hash_defined:
494 case bfd_link_hash_defweak:
495 return h->root.u.def.section;
496
497 case bfd_link_hash_common:
498 return h->root.u.c.p->section;
499
500 default:
501 break;
502 }
503 }
504 }
505 else
506 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
507
508 return NULL;
509 }
510
511 static bfd_boolean
512 elf32_avr_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
513 struct bfd_link_info *info ATTRIBUTE_UNUSED,
514 asection *sec ATTRIBUTE_UNUSED,
515 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
516 {
517 /* We don't use got and plt entries for avr. */
518 return TRUE;
519 }
520
521 /* Look through the relocs for a section during the first phase.
522 Since we don't do .gots or .plts, we just need to consider the
523 virtual table relocs for gc. */
524
525 static bfd_boolean
526 elf32_avr_check_relocs (bfd *abfd,
527 struct bfd_link_info *info,
528 asection *sec,
529 const Elf_Internal_Rela *relocs)
530 {
531 Elf_Internal_Shdr *symtab_hdr;
532 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
533 const Elf_Internal_Rela *rel;
534 const Elf_Internal_Rela *rel_end;
535
536 if (info->relocatable)
537 return TRUE;
538
539 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
540 sym_hashes = elf_sym_hashes (abfd);
541 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
542 if (!elf_bad_symtab (abfd))
543 sym_hashes_end -= symtab_hdr->sh_info;
544
545 rel_end = relocs + sec->reloc_count;
546 for (rel = relocs; rel < rel_end; rel++)
547 {
548 struct elf_link_hash_entry *h;
549 unsigned long r_symndx;
550
551 r_symndx = ELF32_R_SYM (rel->r_info);
552 if (r_symndx < symtab_hdr->sh_info)
553 h = NULL;
554 else
555 {
556 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
557 while (h->root.type == bfd_link_hash_indirect
558 || h->root.type == bfd_link_hash_warning)
559 h = (struct elf_link_hash_entry *) h->root.u.i.link;
560 }
561 }
562
563 return TRUE;
564 }
565
566 /* Perform a single relocation. By default we use the standard BFD
567 routines, but a few relocs, we have to do them ourselves. */
568
569 static bfd_reloc_status_type
570 avr_final_link_relocate (reloc_howto_type * howto,
571 bfd * input_bfd,
572 asection * input_section,
573 bfd_byte * contents,
574 Elf_Internal_Rela * rel,
575 bfd_vma relocation)
576 {
577 bfd_reloc_status_type r = bfd_reloc_ok;
578 bfd_vma x;
579 bfd_signed_vma srel;
580
581 switch (howto->type)
582 {
583 case R_AVR_7_PCREL:
584 contents += rel->r_offset;
585 srel = (bfd_signed_vma) relocation;
586 srel += rel->r_addend;
587 srel -= rel->r_offset;
588 srel -= 2; /* Branch instructions add 2 to the PC... */
589 srel -= (input_section->output_section->vma +
590 input_section->output_offset);
591
592 if (srel & 1)
593 return bfd_reloc_outofrange;
594 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
595 return bfd_reloc_overflow;
596 x = bfd_get_16 (input_bfd, contents);
597 x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
598 bfd_put_16 (input_bfd, x, contents);
599 break;
600
601 case R_AVR_13_PCREL:
602 contents += rel->r_offset;
603 srel = (bfd_signed_vma) relocation;
604 srel += rel->r_addend;
605 srel -= rel->r_offset;
606 srel -= 2; /* Branch instructions add 2 to the PC... */
607 srel -= (input_section->output_section->vma +
608 input_section->output_offset);
609
610 if (srel & 1)
611 return bfd_reloc_outofrange;
612
613 srel = avr_relative_distance_considering_wrap_around (srel);
614
615 /* AVR addresses commands as words. */
616 srel >>= 1;
617
618 /* Check for overflow. */
619 if (srel < -2048 || srel > 2047)
620 {
621 /* Relative distance is too large. */
622
623 /* Always apply WRAPAROUND for avr2 and avr4. */
624 switch (bfd_get_mach (input_bfd))
625 {
626 case bfd_mach_avr2:
627 case bfd_mach_avr4:
628 break;
629
630 default:
631 return bfd_reloc_overflow;
632 }
633 }
634
635 x = bfd_get_16 (input_bfd, contents);
636 x = (x & 0xf000) | (srel & 0xfff);
637 bfd_put_16 (input_bfd, x, contents);
638 break;
639
640 case R_AVR_LO8_LDI:
641 contents += rel->r_offset;
642 srel = (bfd_signed_vma) relocation + rel->r_addend;
643 x = bfd_get_16 (input_bfd, contents);
644 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
645 bfd_put_16 (input_bfd, x, contents);
646 break;
647
648 case R_AVR_LDI:
649 contents += rel->r_offset;
650 srel = (bfd_signed_vma) relocation + rel->r_addend;
651 if (((srel > 0) && (srel & 0xffff) > 255)
652 || ((srel < 0) && ((-srel) & 0xffff) > 128))
653 /* Remove offset for data/eeprom section. */
654 return bfd_reloc_overflow;
655
656 x = bfd_get_16 (input_bfd, contents);
657 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
658 bfd_put_16 (input_bfd, x, contents);
659 break;
660
661 case R_AVR_6:
662 contents += rel->r_offset;
663 srel = (bfd_signed_vma) relocation + rel->r_addend;
664 if (((srel & 0xffff) > 63) || (srel < 0))
665 /* Remove offset for data/eeprom section. */
666 return bfd_reloc_overflow;
667 x = bfd_get_16 (input_bfd, contents);
668 x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
669 | ((srel & (1 << 5)) << 8));
670 bfd_put_16 (input_bfd, x, contents);
671 break;
672
673 case R_AVR_6_ADIW:
674 contents += rel->r_offset;
675 srel = (bfd_signed_vma) relocation + rel->r_addend;
676 if (((srel & 0xffff) > 63) || (srel < 0))
677 /* Remove offset for data/eeprom section. */
678 return bfd_reloc_overflow;
679 x = bfd_get_16 (input_bfd, contents);
680 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
681 bfd_put_16 (input_bfd, x, contents);
682 break;
683
684 case R_AVR_HI8_LDI:
685 contents += rel->r_offset;
686 srel = (bfd_signed_vma) relocation + rel->r_addend;
687 srel = (srel >> 8) & 0xff;
688 x = bfd_get_16 (input_bfd, contents);
689 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
690 bfd_put_16 (input_bfd, x, contents);
691 break;
692
693 case R_AVR_HH8_LDI:
694 contents += rel->r_offset;
695 srel = (bfd_signed_vma) relocation + rel->r_addend;
696 srel = (srel >> 16) & 0xff;
697 x = bfd_get_16 (input_bfd, contents);
698 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
699 bfd_put_16 (input_bfd, x, contents);
700 break;
701
702 case R_AVR_MS8_LDI:
703 contents += rel->r_offset;
704 srel = (bfd_signed_vma) relocation + rel->r_addend;
705 srel = (srel >> 24) & 0xff;
706 x = bfd_get_16 (input_bfd, contents);
707 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
708 bfd_put_16 (input_bfd, x, contents);
709 break;
710
711 case R_AVR_LO8_LDI_NEG:
712 contents += rel->r_offset;
713 srel = (bfd_signed_vma) relocation + rel->r_addend;
714 srel = -srel;
715 x = bfd_get_16 (input_bfd, contents);
716 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
717 bfd_put_16 (input_bfd, x, contents);
718 break;
719
720 case R_AVR_HI8_LDI_NEG:
721 contents += rel->r_offset;
722 srel = (bfd_signed_vma) relocation + rel->r_addend;
723 srel = -srel;
724 srel = (srel >> 8) & 0xff;
725 x = bfd_get_16 (input_bfd, contents);
726 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
727 bfd_put_16 (input_bfd, x, contents);
728 break;
729
730 case R_AVR_HH8_LDI_NEG:
731 contents += rel->r_offset;
732 srel = (bfd_signed_vma) relocation + rel->r_addend;
733 srel = -srel;
734 srel = (srel >> 16) & 0xff;
735 x = bfd_get_16 (input_bfd, contents);
736 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
737 bfd_put_16 (input_bfd, x, contents);
738 break;
739
740 case R_AVR_MS8_LDI_NEG:
741 contents += rel->r_offset;
742 srel = (bfd_signed_vma) relocation + rel->r_addend;
743 srel = -srel;
744 srel = (srel >> 24) & 0xff;
745 x = bfd_get_16 (input_bfd, contents);
746 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
747 bfd_put_16 (input_bfd, x, contents);
748 break;
749
750 case R_AVR_LO8_LDI_PM:
751 contents += rel->r_offset;
752 srel = (bfd_signed_vma) relocation + rel->r_addend;
753 if (srel & 1)
754 return bfd_reloc_outofrange;
755 srel = srel >> 1;
756 x = bfd_get_16 (input_bfd, contents);
757 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
758 bfd_put_16 (input_bfd, x, contents);
759 break;
760
761 case R_AVR_HI8_LDI_PM:
762 contents += rel->r_offset;
763 srel = (bfd_signed_vma) relocation + rel->r_addend;
764 if (srel & 1)
765 return bfd_reloc_outofrange;
766 srel = srel >> 1;
767 srel = (srel >> 8) & 0xff;
768 x = bfd_get_16 (input_bfd, contents);
769 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
770 bfd_put_16 (input_bfd, x, contents);
771 break;
772
773 case R_AVR_HH8_LDI_PM:
774 contents += rel->r_offset;
775 srel = (bfd_signed_vma) relocation + rel->r_addend;
776 if (srel & 1)
777 return bfd_reloc_outofrange;
778 srel = srel >> 1;
779 srel = (srel >> 16) & 0xff;
780 x = bfd_get_16 (input_bfd, contents);
781 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
782 bfd_put_16 (input_bfd, x, contents);
783 break;
784
785 case R_AVR_LO8_LDI_PM_NEG:
786 contents += rel->r_offset;
787 srel = (bfd_signed_vma) relocation + rel->r_addend;
788 srel = -srel;
789 if (srel & 1)
790 return bfd_reloc_outofrange;
791 srel = srel >> 1;
792 x = bfd_get_16 (input_bfd, contents);
793 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
794 bfd_put_16 (input_bfd, x, contents);
795 break;
796
797 case R_AVR_HI8_LDI_PM_NEG:
798 contents += rel->r_offset;
799 srel = (bfd_signed_vma) relocation + rel->r_addend;
800 srel = -srel;
801 if (srel & 1)
802 return bfd_reloc_outofrange;
803 srel = srel >> 1;
804 srel = (srel >> 8) & 0xff;
805 x = bfd_get_16 (input_bfd, contents);
806 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
807 bfd_put_16 (input_bfd, x, contents);
808 break;
809
810 case R_AVR_HH8_LDI_PM_NEG:
811 contents += rel->r_offset;
812 srel = (bfd_signed_vma) relocation + rel->r_addend;
813 srel = -srel;
814 if (srel & 1)
815 return bfd_reloc_outofrange;
816 srel = srel >> 1;
817 srel = (srel >> 16) & 0xff;
818 x = bfd_get_16 (input_bfd, contents);
819 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
820 bfd_put_16 (input_bfd, x, contents);
821 break;
822
823 case R_AVR_CALL:
824 contents += rel->r_offset;
825 srel = (bfd_signed_vma) relocation + rel->r_addend;
826 if (srel & 1)
827 return bfd_reloc_outofrange;
828 srel = srel >> 1;
829 x = bfd_get_16 (input_bfd, contents);
830 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
831 bfd_put_16 (input_bfd, x, contents);
832 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
833 break;
834
835 default:
836 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
837 contents, rel->r_offset,
838 relocation, rel->r_addend);
839 }
840
841 return r;
842 }
843
844 /* Relocate an AVR ELF section. */
845
846 static bfd_boolean
847 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
848 struct bfd_link_info *info,
849 bfd *input_bfd,
850 asection *input_section,
851 bfd_byte *contents,
852 Elf_Internal_Rela *relocs,
853 Elf_Internal_Sym *local_syms,
854 asection **local_sections)
855 {
856 Elf_Internal_Shdr * symtab_hdr;
857 struct elf_link_hash_entry ** sym_hashes;
858 Elf_Internal_Rela * rel;
859 Elf_Internal_Rela * relend;
860
861 if (info->relocatable)
862 return TRUE;
863
864 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
865 sym_hashes = elf_sym_hashes (input_bfd);
866 relend = relocs + input_section->reloc_count;
867
868 for (rel = relocs; rel < relend; rel ++)
869 {
870 reloc_howto_type * howto;
871 unsigned long r_symndx;
872 Elf_Internal_Sym * sym;
873 asection * sec;
874 struct elf_link_hash_entry * h;
875 bfd_vma relocation;
876 bfd_reloc_status_type r;
877 const char * name;
878 int r_type;
879
880 /* This is a final link. */
881 r_type = ELF32_R_TYPE (rel->r_info);
882 r_symndx = ELF32_R_SYM (rel->r_info);
883 howto = elf_avr_howto_table + ELF32_R_TYPE (rel->r_info);
884 h = NULL;
885 sym = NULL;
886 sec = NULL;
887
888 if (r_symndx < symtab_hdr->sh_info)
889 {
890 sym = local_syms + r_symndx;
891 sec = local_sections [r_symndx];
892 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
893
894 name = bfd_elf_string_from_elf_section
895 (input_bfd, symtab_hdr->sh_link, sym->st_name);
896 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
897 }
898 else
899 {
900 bfd_boolean unresolved_reloc, warned;
901
902 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
903 r_symndx, symtab_hdr, sym_hashes,
904 h, sec, relocation,
905 unresolved_reloc, warned);
906
907 name = h->root.root.string;
908 }
909
910 r = avr_final_link_relocate (howto, input_bfd, input_section,
911 contents, rel, relocation);
912
913 if (r != bfd_reloc_ok)
914 {
915 const char * msg = (const char *) NULL;
916
917 switch (r)
918 {
919 case bfd_reloc_overflow:
920 r = info->callbacks->reloc_overflow
921 (info, (h ? &h->root : NULL),
922 name, howto->name, (bfd_vma) 0,
923 input_bfd, input_section, rel->r_offset);
924 break;
925
926 case bfd_reloc_undefined:
927 r = info->callbacks->undefined_symbol
928 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
929 break;
930
931 case bfd_reloc_outofrange:
932 msg = _("internal error: out of range error");
933 break;
934
935 case bfd_reloc_notsupported:
936 msg = _("internal error: unsupported relocation error");
937 break;
938
939 case bfd_reloc_dangerous:
940 msg = _("internal error: dangerous relocation");
941 break;
942
943 default:
944 msg = _("internal error: unknown error");
945 break;
946 }
947
948 if (msg)
949 r = info->callbacks->warning
950 (info, msg, name, input_bfd, input_section, rel->r_offset);
951
952 if (! r)
953 return FALSE;
954 }
955 }
956
957 return TRUE;
958 }
959
960 /* The final processing done just before writing out a AVR ELF object
961 file. This gets the AVR architecture right based on the machine
962 number. */
963
964 static void
965 bfd_elf_avr_final_write_processing (bfd *abfd,
966 bfd_boolean linker ATTRIBUTE_UNUSED)
967 {
968 unsigned long val;
969
970 switch (bfd_get_mach (abfd))
971 {
972 default:
973 case bfd_mach_avr2:
974 val = E_AVR_MACH_AVR2;
975 break;
976
977 case bfd_mach_avr1:
978 val = E_AVR_MACH_AVR1;
979 break;
980
981 case bfd_mach_avr3:
982 val = E_AVR_MACH_AVR3;
983 break;
984
985 case bfd_mach_avr4:
986 val = E_AVR_MACH_AVR4;
987 break;
988
989 case bfd_mach_avr5:
990 val = E_AVR_MACH_AVR5;
991 break;
992 }
993
994 elf_elfheader (abfd)->e_machine = EM_AVR;
995 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
996 elf_elfheader (abfd)->e_flags |= val;
997 elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
998 }
999
1000 /* Set the right machine number. */
1001
1002 static bfd_boolean
1003 elf32_avr_object_p (bfd *abfd)
1004 {
1005 unsigned int e_set = bfd_mach_avr2;
1006
1007 if (elf_elfheader (abfd)->e_machine == EM_AVR
1008 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1009 {
1010 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1011
1012 switch (e_mach)
1013 {
1014 default:
1015 case E_AVR_MACH_AVR2:
1016 e_set = bfd_mach_avr2;
1017 break;
1018
1019 case E_AVR_MACH_AVR1:
1020 e_set = bfd_mach_avr1;
1021 break;
1022
1023 case E_AVR_MACH_AVR3:
1024 e_set = bfd_mach_avr3;
1025 break;
1026
1027 case E_AVR_MACH_AVR4:
1028 e_set = bfd_mach_avr4;
1029 break;
1030
1031 case E_AVR_MACH_AVR5:
1032 e_set = bfd_mach_avr5;
1033 break;
1034 }
1035 }
1036 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1037 e_set);
1038 }
1039
1040
1041 /* Enable debugging printout at stdout with a value of 1. */
1042 #define DEBUG_RELAX 0
1043
1044 /* Delete some bytes from a section while changing the size of an instruction.
1045 The parameter "addr" denotes the section-relative offset pointing just
1046 behind the shrinked instruction. "addr+count" point at the first
1047 byte just behind the original unshrinked instruction. */
1048
1049 static bfd_boolean
1050 elf32_avr_relax_delete_bytes (bfd *abfd,
1051 asection *sec,
1052 bfd_vma addr,
1053 int count)
1054 {
1055 Elf_Internal_Shdr *symtab_hdr;
1056 unsigned int sec_shndx;
1057 bfd_byte *contents;
1058 Elf_Internal_Rela *irel, *irelend;
1059 Elf_Internal_Rela *irelalign;
1060 Elf_Internal_Sym *isym;
1061 Elf_Internal_Sym *isymbuf = NULL;
1062 Elf_Internal_Sym *isymend;
1063 bfd_vma toaddr;
1064 struct elf_link_hash_entry **sym_hashes;
1065 struct elf_link_hash_entry **end_hashes;
1066 unsigned int symcount;
1067
1068 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1069 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1070 contents = elf_section_data (sec)->this_hdr.contents;
1071
1072 /* The deletion must stop at the next ALIGN reloc for an aligment
1073 power larger than the number of bytes we are deleting. */
1074
1075 irelalign = NULL;
1076 toaddr = sec->size;
1077
1078 irel = elf_section_data (sec)->relocs;
1079 irelend = irel + sec->reloc_count;
1080
1081 /* Actually delete the bytes. */
1082 if (toaddr - addr - count > 0)
1083 memmove (contents + addr, contents + addr + count,
1084 (size_t) (toaddr - addr - count));
1085 sec->size -= count;
1086
1087 /* Adjust all the reloc addresses. */
1088 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1089 {
1090 bfd_vma old_reloc_address;
1091 bfd_vma shrinked_insn_address;
1092
1093 old_reloc_address = (sec->output_section->vma
1094 + sec->output_offset + irel->r_offset);
1095 shrinked_insn_address = (sec->output_section->vma
1096 + sec->output_offset + addr - count);
1097
1098 /* Get the new reloc address. */
1099 if ((irel->r_offset > addr
1100 && irel->r_offset < toaddr))
1101 {
1102 if (DEBUG_RELAX)
1103 printf ("Relocation at address 0x%x needs to be moved.\n"
1104 "Old section offset: 0x%x, New section offset: 0x%x \n",
1105 (unsigned int) old_reloc_address,
1106 (unsigned int) irel->r_offset,
1107 (unsigned int) ((irel->r_offset) - count));
1108
1109 irel->r_offset -= count;
1110 }
1111
1112 }
1113
1114 /* The reloc's own addresses are now ok. However, we need to readjust
1115 the reloc's addend, i.e. the reloc's value 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.
1119
1120 The most common case where this happens are relocs relative to
1121 the section-start symbol.
1122
1123 This step needs to be done for all of the sections of the bfd. */
1124
1125 {
1126 struct bfd_section *isec;
1127
1128 for (isec = abfd->sections; isec; isec = isec->next)
1129 {
1130 bfd_vma symval;
1131 bfd_vma shrinked_insn_address;
1132
1133 shrinked_insn_address = (sec->output_section->vma
1134 + sec->output_offset + addr - count);
1135
1136 irelend = elf_section_data (isec)->relocs + isec->reloc_count;
1137 for (irel = elf_section_data (isec)->relocs;
1138 irel < irelend;
1139 irel++)
1140 {
1141 /* Read this BFD's local symbols if we haven't done
1142 so already. */
1143 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1144 {
1145 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1146 if (isymbuf == NULL)
1147 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1148 symtab_hdr->sh_info, 0,
1149 NULL, NULL, NULL);
1150 if (isymbuf == NULL)
1151 return FALSE;
1152 }
1153
1154 /* Get the value of the symbol referred to by the reloc. */
1155 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1156 {
1157 /* A local symbol. */
1158 Elf_Internal_Sym *isym;
1159 asection *sym_sec;
1160
1161 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1162 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1163 symval = isym->st_value;
1164 /* If the reloc is absolute, it will not have
1165 a symbol or section associated with it. */
1166 if (sym_sec == sec)
1167 {
1168 symval += sym_sec->output_section->vma
1169 + sym_sec->output_offset;
1170
1171 if (DEBUG_RELAX)
1172 printf ("Checking if the relocation's "
1173 "addend needs corrections.\n"
1174 "Address of anchor symbol: 0x%x \n"
1175 "Address of relocation target: 0x%x \n"
1176 "Address of relaxed insn: 0x%x \n",
1177 (unsigned int) symval,
1178 (unsigned int) (symval + irel->r_addend),
1179 (unsigned int) shrinked_insn_address);
1180
1181 if (symval <= shrinked_insn_address
1182 && (symval + irel->r_addend) > shrinked_insn_address)
1183 {
1184 irel->r_addend -= count;
1185
1186 if (DEBUG_RELAX)
1187 printf ("Relocation's addend needed to be fixed \n");
1188 }
1189 }
1190 /* else...Reference symbol is absolute. No adjustment needed. */
1191 }
1192 /* else...Reference symbol is extern. No need for adjusting
1193 the addend. */
1194 }
1195 }
1196 }
1197
1198 /* Adjust the local symbols defined in this section. */
1199 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1200 isymend = isym + symtab_hdr->sh_info;
1201 for (; isym < isymend; isym++)
1202 {
1203 if (isym->st_shndx == sec_shndx
1204 && isym->st_value > addr
1205 && isym->st_value < toaddr)
1206 isym->st_value -= count;
1207 }
1208
1209 /* Now adjust the global symbols defined in this section. */
1210 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1211 - symtab_hdr->sh_info);
1212 sym_hashes = elf_sym_hashes (abfd);
1213 end_hashes = sym_hashes + symcount;
1214 for (; sym_hashes < end_hashes; sym_hashes++)
1215 {
1216 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1217 if ((sym_hash->root.type == bfd_link_hash_defined
1218 || sym_hash->root.type == bfd_link_hash_defweak)
1219 && sym_hash->root.u.def.section == sec
1220 && sym_hash->root.u.def.value > addr
1221 && sym_hash->root.u.def.value < toaddr)
1222 {
1223 sym_hash->root.u.def.value -= count;
1224 }
1225 }
1226
1227 return TRUE;
1228 }
1229
1230 /* This function handles relaxing for the avr.
1231 Many important relaxing opportunities within functions are already
1232 realized by the compiler itself.
1233 Here we try to replace call (4 bytes) -> rcall (2 bytes)
1234 and jump -> rjmp (safes also 2 bytes).
1235 As well we now optimize seqences of
1236 - call/rcall function
1237 - ret
1238 to yield
1239 - jmp/rjmp function
1240 - ret
1241 . In case that within a sequence
1242 - jmp/rjmp label
1243 - ret
1244 the ret could no longer be reached it is optimized away. In order
1245 to check if the ret is no longer needed, it is checked that the ret's address
1246 is not the target of a branch or jump within the same section, it is checked
1247 that there is no skip instruction before the jmp/rjmp and that there
1248 is no local or global label place at the address of the ret.
1249
1250 We refrain from relaxing within sections ".vectors" and
1251 ".jumptables" in order to maintain the position of the instructions.
1252 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
1253 if possible. (In future one could possibly use the space of the nop
1254 for the first instruction of the irq service function.
1255
1256 The .jumptables sections is meant to be used for a future tablejump variant
1257 for the devices with 3-byte program counter where the table itself
1258 contains 4-byte jump instructions whose relative offset must not
1259 be changed. */
1260
1261 static bfd_boolean
1262 elf32_avr_relax_section (bfd *abfd,
1263 asection *sec,
1264 struct bfd_link_info *link_info,
1265 bfd_boolean *again)
1266 {
1267 Elf_Internal_Shdr *symtab_hdr;
1268 Elf_Internal_Rela *internal_relocs;
1269 Elf_Internal_Rela *irel, *irelend;
1270 bfd_byte *contents = NULL;
1271 Elf_Internal_Sym *isymbuf = NULL;
1272 static asection *last_input_section = NULL;
1273 static Elf_Internal_Rela *last_reloc = NULL;
1274
1275 /* Assume nothing changes. */
1276 *again = FALSE;
1277
1278 /* We don't have to do anything for a relocatable link, if
1279 this section does not have relocs, or if this is not a
1280 code section. */
1281 if (link_info->relocatable
1282 || (sec->flags & SEC_RELOC) == 0
1283 || sec->reloc_count == 0
1284 || (sec->flags & SEC_CODE) == 0)
1285 return TRUE;
1286
1287 /* Check if the object file to relax uses internal symbols so that we
1288 could fix up the relocations. */
1289 if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
1290 return TRUE;
1291
1292 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1293
1294 /* Get a copy of the native relocations. */
1295 internal_relocs = (_bfd_elf_link_read_relocs
1296 (abfd, sec, NULL, NULL, link_info->keep_memory));
1297 if (internal_relocs == NULL)
1298 goto error_return;
1299
1300 if (sec != last_input_section)
1301 last_reloc = NULL;
1302
1303 last_input_section = sec;
1304
1305 /* Walk through the relocs looking for relaxing opportunities. */
1306 irelend = internal_relocs + sec->reloc_count;
1307 for (irel = internal_relocs; irel < irelend; irel++)
1308 {
1309 bfd_vma symval;
1310
1311 if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
1312 && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
1313 && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
1314 continue;
1315
1316 /* Get the section contents if we haven't done so already. */
1317 if (contents == NULL)
1318 {
1319 /* Get cached copy if it exists. */
1320 if (elf_section_data (sec)->this_hdr.contents != NULL)
1321 contents = elf_section_data (sec)->this_hdr.contents;
1322 else
1323 {
1324 /* Go get them off disk. */
1325 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
1326 goto error_return;
1327 }
1328 }
1329
1330 /* Read this BFD's local symbols if we haven't done so already. */
1331 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1332 {
1333 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1334 if (isymbuf == NULL)
1335 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1336 symtab_hdr->sh_info, 0,
1337 NULL, NULL, NULL);
1338 if (isymbuf == NULL)
1339 goto error_return;
1340 }
1341
1342
1343 /* Get the value of the symbol referred to by the reloc. */
1344 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1345 {
1346 /* A local symbol. */
1347 Elf_Internal_Sym *isym;
1348 asection *sym_sec;
1349
1350 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1351 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1352 symval = isym->st_value;
1353 /* If the reloc is absolute, it will not have
1354 a symbol or section associated with it. */
1355 if (sym_sec)
1356 symval += sym_sec->output_section->vma
1357 + sym_sec->output_offset;
1358 }
1359 else
1360 {
1361 unsigned long indx;
1362 struct elf_link_hash_entry *h;
1363
1364 /* An external symbol. */
1365 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1366 h = elf_sym_hashes (abfd)[indx];
1367 BFD_ASSERT (h != NULL);
1368 if (h->root.type != bfd_link_hash_defined
1369 && h->root.type != bfd_link_hash_defweak)
1370 /* This appears to be a reference to an undefined
1371 symbol. Just ignore it--it will be caught by the
1372 regular reloc processing. */
1373 continue;
1374
1375 symval = (h->root.u.def.value
1376 + h->root.u.def.section->output_section->vma
1377 + h->root.u.def.section->output_offset);
1378 }
1379
1380 /* For simplicity of coding, we are going to modify the section
1381 contents, the section relocs, and the BFD symbol table. We
1382 must tell the rest of the code not to free up this
1383 information. It would be possible to instead create a table
1384 of changes which have to be made, as is done in coff-mips.c;
1385 that would be more work, but would require less memory when
1386 the linker is run. */
1387 switch (ELF32_R_TYPE (irel->r_info))
1388 {
1389 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1390 pc-relative rcall/rjmp. */
1391 case R_AVR_CALL:
1392 {
1393 bfd_vma value = symval + irel->r_addend;
1394 bfd_vma dot, gap;
1395 int distance_short_enough = 0;
1396
1397 /* Get the address of this instruction. */
1398 dot = (sec->output_section->vma
1399 + sec->output_offset + irel->r_offset);
1400
1401 /* Compute the distance from this insn to the branch target. */
1402 gap = value - dot;
1403
1404 /* If the distance is within -4094..+4098 inclusive, then we can
1405 relax this jump/call. +4098 because the call/jump target
1406 will be closer after the relaxation. */
1407 if ((int) gap >= -4094 && (int) gap <= 4098)
1408 distance_short_enough = 1;
1409
1410 /* Here we handle the wrap-around case. E.g. for a 16k device
1411 we could use a rjmp to jump from address 0x100 to 0x3d00!
1412 In order to make this work properly, we need to fill the
1413 vaiable avr_pc_wrap_around with the appropriate value.
1414 I.e. 0x4000 for a 16k device. */
1415 {
1416 /* Shrinking the code size makes the gaps larger in the
1417 case of wrap-arounds. So we use a heuristical safety
1418 margin to avoid that during relax the distance gets
1419 again too large for the short jumps. Let's assume
1420 a typical code-size reduction due to relax for a
1421 16k device of 600 bytes. So let's use twice the
1422 typical value as safety margin. */
1423 int rgap;
1424 int safety_margin;
1425
1426 int assumed_shrink = 600;
1427 if (avr_pc_wrap_around > 0x4000)
1428 assumed_shrink = 900;
1429
1430 safety_margin = 2 * assumed_shrink;
1431
1432 rgap = avr_relative_distance_considering_wrap_around (gap);
1433
1434 if (rgap >= (-4092 + safety_margin)
1435 && rgap <= (4094 - safety_margin))
1436 distance_short_enough = 1;
1437 }
1438
1439 if (distance_short_enough)
1440 {
1441 unsigned char code_msb;
1442 unsigned char code_lsb;
1443
1444 if (DEBUG_RELAX)
1445 printf ("shrinking jump/call instruction at address 0x%x"
1446 " in section %s\n\n",
1447 (int) dot, sec->name);
1448
1449 /* Note that we've changed the relocs, section contents,
1450 etc. */
1451 elf_section_data (sec)->relocs = internal_relocs;
1452 elf_section_data (sec)->this_hdr.contents = contents;
1453 symtab_hdr->contents = (unsigned char *) isymbuf;
1454
1455 /* Get the instruction code for relaxing. */
1456 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
1457 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1458
1459 /* Mask out the relocation bits. */
1460 code_msb &= 0x94;
1461 code_lsb &= 0x0E;
1462 if (code_msb == 0x94 && code_lsb == 0x0E)
1463 {
1464 /* we are changing call -> rcall . */
1465 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1466 bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
1467 }
1468 else if (code_msb == 0x94 && code_lsb == 0x0C)
1469 {
1470 /* we are changeing jump -> rjmp. */
1471 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1472 bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
1473 }
1474 else
1475 abort ();
1476
1477 /* Fix the relocation's type. */
1478 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1479 R_AVR_13_PCREL);
1480
1481 /* Check for the vector section. There we don't want to
1482 modify the ordering! */
1483
1484 if (!strcmp (sec->name,".vectors")
1485 || !strcmp (sec->name,".jumptables"))
1486 {
1487 /* Let's insert a nop. */
1488 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
1489 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
1490 }
1491 else
1492 {
1493 /* Delete two bytes of data. */
1494 if (!elf32_avr_relax_delete_bytes (abfd, sec,
1495 irel->r_offset + 2, 2))
1496 goto error_return;
1497
1498 /* That will change things, so, we should relax again.
1499 Note that this is not required, and it may be slow. */
1500 *again = TRUE;
1501 }
1502 }
1503 }
1504
1505 default:
1506 {
1507 unsigned char code_msb;
1508 unsigned char code_lsb;
1509 bfd_vma dot;
1510
1511 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1512 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
1513
1514 /* Get the address of this instruction. */
1515 dot = (sec->output_section->vma
1516 + sec->output_offset + irel->r_offset);
1517
1518 /* Here we look for rcall/ret or call/ret sequences that could be
1519 safely replaced by rjmp/ret or jmp/ret */
1520 if (0xd0 == (code_msb & 0xf0))
1521 {
1522 /* This insn is a rcall. */
1523 unsigned char next_insn_msb = 0;
1524 unsigned char next_insn_lsb = 0;
1525
1526 if (irel->r_offset + 3 < sec->size)
1527 {
1528 next_insn_msb =
1529 bfd_get_8 (abfd, contents + irel->r_offset + 3);
1530 next_insn_lsb =
1531 bfd_get_8 (abfd, contents + irel->r_offset + 2);
1532 }
1533
1534 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1535 {
1536 /* The next insn is a ret. We now convert the rcall insn
1537 into a rjmp instruction. */
1538 code_msb &= 0xef;
1539 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
1540 if (DEBUG_RELAX)
1541 printf ("converted rcall/ret sequence at address 0x%x"
1542 " into rjmp/ret sequence. Section is %s\n\n",
1543 (int) dot, sec->name);
1544 *again = TRUE;
1545 break;
1546 }
1547 }
1548 else if ((0x94 == (code_msb & 0xfe))
1549 && (0x0e == (code_lsb & 0x0e)))
1550 {
1551 /* This insn is a call. */
1552 unsigned char next_insn_msb = 0;
1553 unsigned char next_insn_lsb = 0;
1554
1555 if (irel->r_offset + 5 < sec->size)
1556 {
1557 next_insn_msb =
1558 bfd_get_8 (abfd, contents + irel->r_offset + 5);
1559 next_insn_lsb =
1560 bfd_get_8 (abfd, contents + irel->r_offset + 4);
1561 }
1562
1563 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1564 {
1565 /* The next insn is a ret. We now convert the call insn
1566 into a jmp instruction. */
1567
1568 code_lsb &= 0xfd;
1569 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
1570 if (DEBUG_RELAX)
1571 printf ("converted call/ret sequence at address 0x%x"
1572 " into jmp/ret sequence. Section is %s\n\n",
1573 (int) dot, sec->name);
1574 *again = TRUE;
1575 break;
1576 }
1577 }
1578 else if ((0xc0 == (code_msb & 0xf0))
1579 || ((0x94 == (code_msb & 0xfe))
1580 && (0x0c == (code_lsb & 0x0e))))
1581 {
1582 /* This insn is a rjmp or a jmp. */
1583 unsigned char next_insn_msb = 0;
1584 unsigned char next_insn_lsb = 0;
1585 int insn_size;
1586
1587 if (0xc0 == (code_msb & 0xf0))
1588 insn_size = 2; /* rjmp insn */
1589 else
1590 insn_size = 4; /* jmp insn */
1591
1592 if (irel->r_offset + insn_size + 1 < sec->size)
1593 {
1594 next_insn_msb =
1595 bfd_get_8 (abfd, contents + irel->r_offset
1596 + insn_size + 1);
1597 next_insn_lsb =
1598 bfd_get_8 (abfd, contents + irel->r_offset
1599 + insn_size);
1600 }
1601
1602 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1603 {
1604 /* The next insn is a ret. We possibly could delete
1605 this ret. First we need to check for preceeding
1606 sbis/sbic/sbrs or cpse "skip" instructions. */
1607
1608 int there_is_preceeding_non_skip_insn = 1;
1609 bfd_vma address_of_ret;
1610
1611 address_of_ret = dot + insn_size;
1612
1613 if (DEBUG_RELAX && (insn_size == 2))
1614 printf ("found rjmp / ret sequence at address 0x%x\n",
1615 (int) dot);
1616 if (DEBUG_RELAX && (insn_size == 4))
1617 printf ("found jmp / ret sequence at address 0x%x\n",
1618 (int) dot);
1619
1620 /* We have to make sure that there is a preceeding insn. */
1621 if (irel->r_offset >= 2)
1622 {
1623 unsigned char preceeding_msb;
1624 unsigned char preceeding_lsb;
1625 preceeding_msb =
1626 bfd_get_8 (abfd, contents + irel->r_offset - 1);
1627 preceeding_lsb =
1628 bfd_get_8 (abfd, contents + irel->r_offset - 2);
1629
1630 /* sbic. */
1631 if (0x99 == preceeding_msb)
1632 there_is_preceeding_non_skip_insn = 0;
1633
1634 /* sbis. */
1635 if (0x9b == preceeding_msb)
1636 there_is_preceeding_non_skip_insn = 0;
1637
1638 /* sbrc */
1639 if ((0xfc == (preceeding_msb & 0xfe)
1640 && (0x00 == (preceeding_lsb & 0x08))))
1641 there_is_preceeding_non_skip_insn = 0;
1642
1643 /* sbrs */
1644 if ((0xfe == (preceeding_msb & 0xfe)
1645 && (0x00 == (preceeding_lsb & 0x08))))
1646 there_is_preceeding_non_skip_insn = 0;
1647
1648 /* cpse */
1649 if (0x10 == (preceeding_msb & 0xfc))
1650 there_is_preceeding_non_skip_insn = 0;
1651
1652 if (there_is_preceeding_non_skip_insn == 0)
1653 if (DEBUG_RELAX)
1654 printf ("preceeding skip insn prevents deletion of"
1655 " ret insn at addr 0x%x in section %s\n",
1656 (int) dot + 2, sec->name);
1657 }
1658 else
1659 {
1660 /* There is no previous instruction. */
1661 there_is_preceeding_non_skip_insn = 0;
1662 }
1663
1664 if (there_is_preceeding_non_skip_insn)
1665 {
1666 /* We now only have to make sure that there is no
1667 local label defined at the address of the ret
1668 instruction and that there is no local relocation
1669 in this section pointing to the ret. */
1670
1671 int deleting_ret_is_safe = 1;
1672 unsigned int section_offset_of_ret_insn =
1673 irel->r_offset + insn_size;
1674 Elf_Internal_Sym *isym, *isymend;
1675 unsigned int sec_shndx;
1676
1677 sec_shndx =
1678 _bfd_elf_section_from_bfd_section (abfd, sec);
1679
1680 /* Check for local symbols. */
1681 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1682 isymend = isym + symtab_hdr->sh_info;
1683 for (; isym < isymend; isym++)
1684 {
1685 if (isym->st_value == section_offset_of_ret_insn
1686 && isym->st_shndx == sec_shndx)
1687 {
1688 deleting_ret_is_safe = 0;
1689 if (DEBUG_RELAX)
1690 printf ("local label prevents deletion of ret "
1691 "insn at address 0x%x\n",
1692 (int) dot + insn_size);
1693 }
1694 }
1695
1696 /* Now check for global symbols. */
1697 {
1698 int symcount;
1699 struct elf_link_hash_entry **sym_hashes;
1700 struct elf_link_hash_entry **end_hashes;
1701
1702 symcount = (symtab_hdr->sh_size
1703 / sizeof (Elf32_External_Sym)
1704 - symtab_hdr->sh_info);
1705 sym_hashes = elf_sym_hashes (abfd);
1706 end_hashes = sym_hashes + symcount;
1707 for (; sym_hashes < end_hashes; sym_hashes++)
1708 {
1709 struct elf_link_hash_entry *sym_hash =
1710 *sym_hashes;
1711 if ((sym_hash->root.type == bfd_link_hash_defined
1712 || sym_hash->root.type ==
1713 bfd_link_hash_defweak)
1714 && sym_hash->root.u.def.section == sec
1715 && sym_hash->root.u.def.value == section_offset_of_ret_insn)
1716 {
1717 deleting_ret_is_safe = 0;
1718 if (DEBUG_RELAX)
1719 printf ("global label prevents deletion of "
1720 "ret insn at address 0x%x\n",
1721 (int) dot + insn_size);
1722 }
1723 }
1724 }
1725 /* Now we check for relocations pointing to ret. */
1726 {
1727 Elf_Internal_Rela *irel;
1728 Elf_Internal_Rela *relend;
1729 Elf_Internal_Shdr *symtab_hdr;
1730
1731 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1732 relend = elf_section_data (sec)->relocs
1733 + sec->reloc_count;
1734
1735 for (irel = elf_section_data (sec)->relocs;
1736 irel < relend; irel++)
1737 {
1738 bfd_vma reloc_target = 0;
1739 bfd_vma symval;
1740 Elf_Internal_Sym *isymbuf = NULL;
1741
1742 /* Read this BFD's local symbols if we haven't
1743 done so already. */
1744 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1745 {
1746 isymbuf = (Elf_Internal_Sym *)
1747 symtab_hdr->contents;
1748 if (isymbuf == NULL)
1749 isymbuf = bfd_elf_get_elf_syms
1750 (abfd,
1751 symtab_hdr,
1752 symtab_hdr->sh_info, 0,
1753 NULL, NULL, NULL);
1754 if (isymbuf == NULL)
1755 break;
1756 }
1757
1758 /* Get the value of the symbol referred to
1759 by the reloc. */
1760 if (ELF32_R_SYM (irel->r_info)
1761 < symtab_hdr->sh_info)
1762 {
1763 /* A local symbol. */
1764 Elf_Internal_Sym *isym;
1765 asection *sym_sec;
1766
1767 isym = isymbuf
1768 + ELF32_R_SYM (irel->r_info);
1769 sym_sec = bfd_section_from_elf_index
1770 (abfd, isym->st_shndx);
1771 symval = isym->st_value;
1772
1773 /* If the reloc is absolute, it will not
1774 have a symbol or section associated
1775 with it. */
1776
1777 if (sym_sec)
1778 {
1779 symval +=
1780 sym_sec->output_section->vma
1781 + sym_sec->output_offset;
1782 reloc_target = symval + irel->r_addend;
1783 }
1784 else
1785 {
1786 reloc_target = symval + irel->r_addend;
1787 /* Reference symbol is absolute. */
1788 }
1789 }
1790 /* else ... reference symbol is extern. */
1791
1792 if (address_of_ret == reloc_target)
1793 {
1794 deleting_ret_is_safe = 0;
1795 if (DEBUG_RELAX)
1796 printf ("ret from "
1797 "rjmp/jmp ret sequence at address"
1798 " 0x%x could not be deleted. ret"
1799 " is target of a relocation.\n",
1800 (int) address_of_ret);
1801 }
1802 }
1803 }
1804
1805 if (deleting_ret_is_safe)
1806 {
1807 if (DEBUG_RELAX)
1808 printf ("unreachable ret instruction "
1809 "at address 0x%x deleted.\n",
1810 (int) dot + insn_size);
1811
1812 /* Delete two bytes of data. */
1813 if (!elf32_avr_relax_delete_bytes (abfd, sec,
1814 irel->r_offset + insn_size, 2))
1815 goto error_return;
1816
1817 /* That will change things, so, we should relax
1818 again. Note that this is not required, and it
1819 may be slow. */
1820 *again = TRUE;
1821 break;
1822 }
1823 }
1824
1825 }
1826 }
1827 break;
1828 }
1829 }
1830 }
1831
1832 if (contents != NULL
1833 && elf_section_data (sec)->this_hdr.contents != contents)
1834 {
1835 if (! link_info->keep_memory)
1836 free (contents);
1837 else
1838 {
1839 /* Cache the section contents for elf_link_input_bfd. */
1840 elf_section_data (sec)->this_hdr.contents = contents;
1841 }
1842 }
1843
1844 if (internal_relocs != NULL
1845 && elf_section_data (sec)->relocs != internal_relocs)
1846 free (internal_relocs);
1847
1848 return TRUE;
1849
1850 error_return:
1851 if (isymbuf != NULL
1852 && symtab_hdr->contents != (unsigned char *) isymbuf)
1853 free (isymbuf);
1854 if (contents != NULL
1855 && elf_section_data (sec)->this_hdr.contents != contents)
1856 free (contents);
1857 if (internal_relocs != NULL
1858 && elf_section_data (sec)->relocs != internal_relocs)
1859 free (internal_relocs);
1860
1861 return FALSE;
1862 }
1863
1864 /* This is a version of bfd_generic_get_relocated_section_contents
1865 which uses elf32_avr_relocate_section.
1866
1867 For avr it's essentially a cut and paste taken from the H8300 port.
1868 The author of the relaxation support patch for avr had absolutely no
1869 clue what is happening here but found out that this part of the code
1870 seems to be important. */
1871
1872 static bfd_byte *
1873 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
1874 struct bfd_link_info *link_info,
1875 struct bfd_link_order *link_order,
1876 bfd_byte *data,
1877 bfd_boolean relocatable,
1878 asymbol **symbols)
1879 {
1880 Elf_Internal_Shdr *symtab_hdr;
1881 asection *input_section = link_order->u.indirect.section;
1882 bfd *input_bfd = input_section->owner;
1883 asection **sections = NULL;
1884 Elf_Internal_Rela *internal_relocs = NULL;
1885 Elf_Internal_Sym *isymbuf = NULL;
1886
1887 /* We only need to handle the case of relaxing, or of having a
1888 particular set of section contents, specially. */
1889 if (relocatable
1890 || elf_section_data (input_section)->this_hdr.contents == NULL)
1891 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1892 link_order, data,
1893 relocatable,
1894 symbols);
1895 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1896
1897 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1898 (size_t) input_section->size);
1899
1900 if ((input_section->flags & SEC_RELOC) != 0
1901 && input_section->reloc_count > 0)
1902 {
1903 asection **secpp;
1904 Elf_Internal_Sym *isym, *isymend;
1905 bfd_size_type amt;
1906
1907 internal_relocs = (_bfd_elf_link_read_relocs
1908 (input_bfd, input_section, NULL, NULL, FALSE));
1909 if (internal_relocs == NULL)
1910 goto error_return;
1911
1912 if (symtab_hdr->sh_info != 0)
1913 {
1914 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1915 if (isymbuf == NULL)
1916 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1917 symtab_hdr->sh_info, 0,
1918 NULL, NULL, NULL);
1919 if (isymbuf == NULL)
1920 goto error_return;
1921 }
1922
1923 amt = symtab_hdr->sh_info;
1924 amt *= sizeof (asection *);
1925 sections = bfd_malloc (amt);
1926 if (sections == NULL && amt != 0)
1927 goto error_return;
1928
1929 isymend = isymbuf + symtab_hdr->sh_info;
1930 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
1931 {
1932 asection *isec;
1933
1934 if (isym->st_shndx == SHN_UNDEF)
1935 isec = bfd_und_section_ptr;
1936 else if (isym->st_shndx == SHN_ABS)
1937 isec = bfd_abs_section_ptr;
1938 else if (isym->st_shndx == SHN_COMMON)
1939 isec = bfd_com_section_ptr;
1940 else
1941 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1942
1943 *secpp = isec;
1944 }
1945
1946 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
1947 input_section, data, internal_relocs,
1948 isymbuf, sections))
1949 goto error_return;
1950
1951 if (sections != NULL)
1952 free (sections);
1953 if (isymbuf != NULL
1954 && symtab_hdr->contents != (unsigned char *) isymbuf)
1955 free (isymbuf);
1956 if (elf_section_data (input_section)->relocs != internal_relocs)
1957 free (internal_relocs);
1958 }
1959
1960 return data;
1961
1962 error_return:
1963 if (sections != NULL)
1964 free (sections);
1965 if (isymbuf != NULL
1966 && symtab_hdr->contents != (unsigned char *) isymbuf)
1967 free (isymbuf);
1968 if (internal_relocs != NULL
1969 && elf_section_data (input_section)->relocs != internal_relocs)
1970 free (internal_relocs);
1971 return NULL;
1972 }
1973
1974
1975 #define ELF_ARCH bfd_arch_avr
1976 #define ELF_MACHINE_CODE EM_AVR
1977 #define ELF_MACHINE_ALT1 EM_AVR_OLD
1978 #define ELF_MAXPAGESIZE 1
1979
1980 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
1981 #define TARGET_LITTLE_NAME "elf32-avr"
1982
1983 #define elf_info_to_howto avr_info_to_howto_rela
1984 #define elf_info_to_howto_rel NULL
1985 #define elf_backend_relocate_section elf32_avr_relocate_section
1986 #define elf_backend_gc_mark_hook elf32_avr_gc_mark_hook
1987 #define elf_backend_gc_sweep_hook elf32_avr_gc_sweep_hook
1988 #define elf_backend_check_relocs elf32_avr_check_relocs
1989 #define elf_backend_can_gc_sections 1
1990 #define elf_backend_rela_normal 1
1991 #define elf_backend_final_write_processing \
1992 bfd_elf_avr_final_write_processing
1993 #define elf_backend_object_p elf32_avr_object_p
1994
1995 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
1996 #define bfd_elf32_bfd_get_relocated_section_contents \
1997 elf32_avr_get_relocated_section_contents
1998
1999 #include "elf32-target.h"