Update formatting to ISO-C90
[deliverable/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 static int
437 avr_relative_distance_considering_wrap_around (unsigned int distance)
438 {
439 unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
440 int dist_with_wrap_around = distance & wrap_around_mask;
441
442 if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
443 dist_with_wrap_around -= avr_pc_wrap_around;
444
445 return dist_with_wrap_around;
446 }
447
448
449 static reloc_howto_type *
450 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
451 bfd_reloc_code_real_type code)
452 {
453 unsigned int i;
454
455 for (i = 0;
456 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
457 i++)
458 {
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
463 return NULL;
464 }
465
466 /* Set the howto pointer for an AVR ELF reloc. */
467
468 static void
469 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
470 arelent *cache_ptr,
471 Elf_Internal_Rela *dst)
472 {
473 unsigned int r_type;
474
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];
478 }
479
480 static asection *
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)
486 {
487 if (h != NULL)
488 {
489 switch (ELF32_R_TYPE (rel->r_info))
490 {
491 default:
492 switch (h->root.type)
493 {
494 case bfd_link_hash_defined:
495 case bfd_link_hash_defweak:
496 return h->root.u.def.section;
497
498 case bfd_link_hash_common:
499 return h->root.u.c.p->section;
500
501 default:
502 break;
503 }
504 }
505 }
506 else
507 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
508
509 return NULL;
510 }
511
512 static bfd_boolean
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)
517 {
518 /* We don't use got and plt entries for avr. */
519 return TRUE;
520 }
521
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. */
525
526 static bfd_boolean
527 elf32_avr_check_relocs (bfd *abfd,
528 struct bfd_link_info *info,
529 asection *sec,
530 const Elf_Internal_Rela *relocs)
531 {
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;
536
537 if (info->relocatable)
538 return TRUE;
539
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;
545
546 rel_end = relocs + sec->reloc_count;
547 for (rel = relocs; rel < rel_end; rel++)
548 {
549 struct elf_link_hash_entry *h;
550 unsigned long r_symndx;
551
552 r_symndx = ELF32_R_SYM (rel->r_info);
553 if (r_symndx < symtab_hdr->sh_info)
554 h = NULL;
555 else
556 {
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;
561 }
562 }
563
564 return TRUE;
565 }
566
567 /* Perform a single relocation. By default we use the standard BFD
568 routines, but a few relocs, we have to do them ourselves. */
569
570 static bfd_reloc_status_type
571 avr_final_link_relocate (reloc_howto_type * howto,
572 bfd * input_bfd,
573 asection * input_section,
574 bfd_byte * contents,
575 Elf_Internal_Rela * rel,
576 bfd_vma relocation)
577 {
578 bfd_reloc_status_type r = bfd_reloc_ok;
579 bfd_vma x;
580 bfd_signed_vma srel;
581
582 switch (howto->type)
583 {
584 case R_AVR_7_PCREL:
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);
592
593 if (srel & 1)
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);
600 break;
601
602 case R_AVR_13_PCREL:
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);
610
611 if (srel & 1)
612 return bfd_reloc_outofrange;
613
614 srel = avr_relative_distance_considering_wrap_around (srel);
615
616 /* AVR addresses commands as words. */
617 srel >>= 1;
618
619 /* Check for overflow. */
620 if (srel < -2048 || srel > 2047)
621 {
622 /* Relative distance is too large. */
623
624 /* Always apply WRAPAROUND for avr2 and avr4. */
625 switch (bfd_get_mach (input_bfd))
626 {
627 case bfd_mach_avr2:
628 case bfd_mach_avr4:
629 break;
630
631 default:
632 return bfd_reloc_overflow;
633 }
634 }
635
636 x = bfd_get_16 (input_bfd, contents);
637 x = (x & 0xf000) | (srel & 0xfff);
638 bfd_put_16 (input_bfd, x, contents);
639 break;
640
641 case R_AVR_LO8_LDI:
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);
647 break;
648
649 case R_AVR_LDI:
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;
656
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);
660 break;
661
662 case R_AVR_6:
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);
672 break;
673
674 case R_AVR_6_ADIW:
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);
683 break;
684
685 case R_AVR_HI8_LDI:
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);
692 break;
693
694 case R_AVR_HH8_LDI:
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);
701 break;
702
703 case R_AVR_MS8_LDI:
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);
710 break;
711
712 case R_AVR_LO8_LDI_NEG:
713 contents += rel->r_offset;
714 srel = (bfd_signed_vma) relocation + rel->r_addend;
715 srel = -srel;
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);
719 break;
720
721 case R_AVR_HI8_LDI_NEG:
722 contents += rel->r_offset;
723 srel = (bfd_signed_vma) relocation + rel->r_addend;
724 srel = -srel;
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);
729 break;
730
731 case R_AVR_HH8_LDI_NEG:
732 contents += rel->r_offset;
733 srel = (bfd_signed_vma) relocation + rel->r_addend;
734 srel = -srel;
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);
739 break;
740
741 case R_AVR_MS8_LDI_NEG:
742 contents += rel->r_offset;
743 srel = (bfd_signed_vma) relocation + rel->r_addend;
744 srel = -srel;
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);
749 break;
750
751 case R_AVR_LO8_LDI_PM:
752 contents += rel->r_offset;
753 srel = (bfd_signed_vma) relocation + rel->r_addend;
754 if (srel & 1)
755 return bfd_reloc_outofrange;
756 srel = srel >> 1;
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);
760 break;
761
762 case R_AVR_HI8_LDI_PM:
763 contents += rel->r_offset;
764 srel = (bfd_signed_vma) relocation + rel->r_addend;
765 if (srel & 1)
766 return bfd_reloc_outofrange;
767 srel = srel >> 1;
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);
772 break;
773
774 case R_AVR_HH8_LDI_PM:
775 contents += rel->r_offset;
776 srel = (bfd_signed_vma) relocation + rel->r_addend;
777 if (srel & 1)
778 return bfd_reloc_outofrange;
779 srel = srel >> 1;
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);
784 break;
785
786 case R_AVR_LO8_LDI_PM_NEG:
787 contents += rel->r_offset;
788 srel = (bfd_signed_vma) relocation + rel->r_addend;
789 srel = -srel;
790 if (srel & 1)
791 return bfd_reloc_outofrange;
792 srel = srel >> 1;
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);
796 break;
797
798 case R_AVR_HI8_LDI_PM_NEG:
799 contents += rel->r_offset;
800 srel = (bfd_signed_vma) relocation + rel->r_addend;
801 srel = -srel;
802 if (srel & 1)
803 return bfd_reloc_outofrange;
804 srel = srel >> 1;
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);
809 break;
810
811 case R_AVR_HH8_LDI_PM_NEG:
812 contents += rel->r_offset;
813 srel = (bfd_signed_vma) relocation + rel->r_addend;
814 srel = -srel;
815 if (srel & 1)
816 return bfd_reloc_outofrange;
817 srel = srel >> 1;
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);
822 break;
823
824 case R_AVR_CALL:
825 contents += rel->r_offset;
826 srel = (bfd_signed_vma) relocation + rel->r_addend;
827 if (srel & 1)
828 return bfd_reloc_outofrange;
829 srel = srel >> 1;
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);
834 break;
835
836 default:
837 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
838 contents, rel->r_offset,
839 relocation, rel->r_addend);
840 }
841
842 return r;
843 }
844
845 /* Relocate an AVR ELF section. */
846
847 static bfd_boolean
848 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
849 struct bfd_link_info *info,
850 bfd *input_bfd,
851 asection *input_section,
852 bfd_byte *contents,
853 Elf_Internal_Rela *relocs,
854 Elf_Internal_Sym *local_syms,
855 asection **local_sections)
856 {
857 Elf_Internal_Shdr * symtab_hdr;
858 struct elf_link_hash_entry ** sym_hashes;
859 Elf_Internal_Rela * rel;
860 Elf_Internal_Rela * relend;
861
862 if (info->relocatable)
863 return TRUE;
864
865 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
866 sym_hashes = elf_sym_hashes (input_bfd);
867 relend = relocs + input_section->reloc_count;
868
869 for (rel = relocs; rel < relend; rel ++)
870 {
871 reloc_howto_type * howto;
872 unsigned long r_symndx;
873 Elf_Internal_Sym * sym;
874 asection * sec;
875 struct elf_link_hash_entry * h;
876 bfd_vma relocation;
877 bfd_reloc_status_type r;
878 const char * name;
879 int r_type;
880
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);
885 h = NULL;
886 sym = NULL;
887 sec = NULL;
888
889 if (r_symndx < symtab_hdr->sh_info)
890 {
891 sym = local_syms + r_symndx;
892 sec = local_sections [r_symndx];
893 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
894
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;
898 }
899 else
900 {
901 bfd_boolean unresolved_reloc, warned;
902
903 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
904 r_symndx, symtab_hdr, sym_hashes,
905 h, sec, relocation,
906 unresolved_reloc, warned);
907
908 name = h->root.root.string;
909 }
910
911 r = avr_final_link_relocate (howto, input_bfd, input_section,
912 contents, rel, relocation);
913
914 if (r != bfd_reloc_ok)
915 {
916 const char * msg = (const char *) NULL;
917
918 switch (r)
919 {
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);
925 break;
926
927 case bfd_reloc_undefined:
928 r = info->callbacks->undefined_symbol
929 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
930 break;
931
932 case bfd_reloc_outofrange:
933 msg = _("internal error: out of range error");
934 break;
935
936 case bfd_reloc_notsupported:
937 msg = _("internal error: unsupported relocation error");
938 break;
939
940 case bfd_reloc_dangerous:
941 msg = _("internal error: dangerous relocation");
942 break;
943
944 default:
945 msg = _("internal error: unknown error");
946 break;
947 }
948
949 if (msg)
950 r = info->callbacks->warning
951 (info, msg, name, input_bfd, input_section, rel->r_offset);
952
953 if (! r)
954 return FALSE;
955 }
956 }
957
958 return TRUE;
959 }
960
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
963 number. */
964
965 static void
966 bfd_elf_avr_final_write_processing (bfd *abfd,
967 bfd_boolean linker ATTRIBUTE_UNUSED)
968 {
969 unsigned long val;
970
971 switch (bfd_get_mach (abfd))
972 {
973 default:
974 case bfd_mach_avr2:
975 val = E_AVR_MACH_AVR2;
976 break;
977
978 case bfd_mach_avr1:
979 val = E_AVR_MACH_AVR1;
980 break;
981
982 case bfd_mach_avr3:
983 val = E_AVR_MACH_AVR3;
984 break;
985
986 case bfd_mach_avr4:
987 val = E_AVR_MACH_AVR4;
988 break;
989
990 case bfd_mach_avr5:
991 val = E_AVR_MACH_AVR5;
992 break;
993 }
994
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;
999 }
1000
1001 /* Set the right machine number. */
1002
1003 static bfd_boolean
1004 elf32_avr_object_p (bfd *abfd)
1005 {
1006 unsigned int e_set = bfd_mach_avr2;
1007
1008 if (elf_elfheader (abfd)->e_machine == EM_AVR
1009 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1010 {
1011 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1012
1013 switch (e_mach)
1014 {
1015 default:
1016 case E_AVR_MACH_AVR2:
1017 e_set = bfd_mach_avr2;
1018 break;
1019
1020 case E_AVR_MACH_AVR1:
1021 e_set = bfd_mach_avr1;
1022 break;
1023
1024 case E_AVR_MACH_AVR3:
1025 e_set = bfd_mach_avr3;
1026 break;
1027
1028 case E_AVR_MACH_AVR4:
1029 e_set = bfd_mach_avr4;
1030 break;
1031
1032 case E_AVR_MACH_AVR5:
1033 e_set = bfd_mach_avr5;
1034 break;
1035 }
1036 }
1037 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1038 e_set);
1039 }
1040
1041
1042 /* Enable debugging printout at stdout with a value of 1. */
1043 #define DEBUG_RELAX 0
1044
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. */
1049
1050 static bfd_boolean
1051 elf32_avr_relax_delete_bytes (bfd *abfd,
1052 asection *sec,
1053 bfd_vma addr,
1054 int count)
1055 {
1056 Elf_Internal_Shdr *symtab_hdr;
1057 unsigned int sec_shndx;
1058 bfd_byte *contents;
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;
1064 bfd_vma toaddr;
1065 struct elf_link_hash_entry **sym_hashes;
1066 struct elf_link_hash_entry **end_hashes;
1067 unsigned int symcount;
1068
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;
1072
1073 /* The deletion must stop at the next ALIGN reloc for an aligment
1074 power larger than the number of bytes we are deleting. */
1075
1076 irelalign = NULL;
1077 toaddr = sec->size;
1078
1079 irel = elf_section_data (sec)->relocs;
1080 irelend = irel + sec->reloc_count;
1081
1082 /* Actually delete the bytes. */
1083 if (toaddr - addr - count > 0)
1084 memmove (contents + addr, contents + addr + count,
1085 (size_t) (toaddr - addr - count));
1086 sec->size -= count;
1087
1088 /* Adjust all the relocs. */
1089 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1090 {
1091 bfd_vma symval;
1092 bfd_vma old_reloc_address;
1093 bfd_vma shrinked_insn_address;
1094
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);
1099
1100 /* Get the new reloc address. */
1101 if ((irel->r_offset > addr
1102 && irel->r_offset < toaddr))
1103 {
1104 if (DEBUG_RELAX)
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));
1110
1111 irel->r_offset -= count;
1112 }
1113
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.
1119
1120 This should happen only for local symbols that are progmem related. */
1121
1122 /* Read this BFD's local symbols if we haven't done so already. */
1123 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1124 {
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,
1129 NULL, NULL, NULL);
1130 if (isymbuf == NULL)
1131 return FALSE;
1132 }
1133
1134 /* Get the value of the symbol referred to by the reloc. */
1135 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1136 {
1137 /* A local symbol. */
1138 Elf_Internal_Sym *isym;
1139 asection *sym_sec;
1140
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. */
1146 if (sym_sec)
1147 {
1148 symval += sym_sec->output_section->vma
1149 + sym_sec->output_offset;
1150
1151 if (DEBUG_RELAX)
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);
1160
1161 if (symval <= shrinked_insn_address
1162 && (symval + irel->r_addend) > shrinked_insn_address)
1163 {
1164 irel->r_addend -= count;
1165
1166 if (DEBUG_RELAX)
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);
1171 }
1172 }
1173 /* else ... Reference symbol is absolute. No adjustment needed. */
1174 }
1175 /* else ... Reference symbol is extern. No need for adjusting the addend. */
1176 }
1177
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++)
1182 {
1183 if (isym->st_shndx == sec_shndx
1184 && isym->st_value > addr
1185 && isym->st_value < toaddr)
1186 isym->st_value -= count;
1187 }
1188
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++)
1195 {
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)
1202 {
1203 sym_hash->root.u.def.value -= count;
1204 }
1205 }
1206
1207 return TRUE;
1208 }
1209
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
1217 - ret
1218 to yield
1219 - jmp/rjmp function
1220 - ret
1221 . In case that within a sequence
1222 - jmp/rjmp label
1223 - ret
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.
1229
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.
1235
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
1239 be changed. */
1240
1241 static bfd_boolean
1242 elf32_avr_relax_section (bfd *abfd,
1243 asection *sec,
1244 struct bfd_link_info *link_info,
1245 bfd_boolean *again)
1246 {
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;
1254
1255 /* Assume nothing changes. */
1256 *again = FALSE;
1257
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
1260 code section. */
1261 if (link_info->relocatable
1262 || (sec->flags & SEC_RELOC) == 0
1263 || sec->reloc_count == 0
1264 || (sec->flags & SEC_CODE) == 0)
1265 return TRUE;
1266
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))
1270 return TRUE;
1271
1272 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1273
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)
1278 goto error_return;
1279
1280 if (sec != last_input_section)
1281 last_reloc = NULL;
1282
1283 last_input_section = sec;
1284
1285 /* Walk through the relocs looking for relaxing opportunities. */
1286 irelend = internal_relocs + sec->reloc_count;
1287 for (irel = internal_relocs; irel < irelend; irel++)
1288 {
1289 bfd_vma symval;
1290
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)
1294 continue;
1295
1296 /* Get the section contents if we haven't done so already. */
1297 if (contents == NULL)
1298 {
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;
1302 else
1303 {
1304 /* Go get them off disk. */
1305 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
1306 goto error_return;
1307 }
1308 }
1309
1310 /* Read this BFD's local symbols if we haven't done so already. */
1311 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1312 {
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,
1317 NULL, NULL, NULL);
1318 if (isymbuf == NULL)
1319 goto error_return;
1320 }
1321
1322
1323 /* Get the value of the symbol referred to by the reloc. */
1324 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1325 {
1326 /* A local symbol. */
1327 Elf_Internal_Sym *isym;
1328 asection *sym_sec;
1329
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. */
1335 if (sym_sec)
1336 symval += sym_sec->output_section->vma
1337 + sym_sec->output_offset;
1338 }
1339 else
1340 {
1341 unsigned long indx;
1342 struct elf_link_hash_entry *h;
1343
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. */
1353 continue;
1354
1355 symval = (h->root.u.def.value
1356 + h->root.u.def.section->output_section->vma
1357 + h->root.u.def.section->output_offset);
1358 }
1359
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))
1368 {
1369 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1370 pc-relative rcall/rjmp. */
1371 case R_AVR_CALL:
1372 {
1373 bfd_vma value = symval + irel->r_addend;
1374 bfd_vma dot, gap;
1375 int distance_short_enough = 0;
1376
1377 /* Get the address of this instruction. */
1378 dot = (sec->output_section->vma
1379 + sec->output_offset + irel->r_offset);
1380
1381 /* Compute the distance from this insn to the branch target. */
1382 gap = value - dot;
1383
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;
1389
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. */
1395 {
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. */
1403 int rgap;
1404 int safety_margin;
1405
1406 int assumed_shrink = 600;
1407 if (avr_pc_wrap_around > 0x4000)
1408 assumed_shrink = 900;
1409
1410 safety_margin = 2 * assumed_shrink;
1411
1412 rgap = avr_relative_distance_considering_wrap_around (gap);
1413
1414 if (rgap >= (-4092 + safety_margin)
1415 && rgap <= (4094 - safety_margin))
1416 distance_short_enough = 1;
1417 }
1418
1419 if (distance_short_enough)
1420 {
1421 unsigned char code_msb;
1422 unsigned char code_lsb;
1423
1424 if (DEBUG_RELAX)
1425 printf ("shrinking jump/call instruction at address 0x%x"
1426 " in section %s\n\n",
1427 (int) dot, sec->name);
1428
1429 /* Note that we've changed the relocs, section contents,
1430 etc. */
1431 elf_section_data (sec)->relocs = internal_relocs;
1432 elf_section_data (sec)->this_hdr.contents = contents;
1433 symtab_hdr->contents = (unsigned char *) isymbuf;
1434
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);
1438
1439 /* Mask out the relocation bits. */
1440 code_msb &= 0x94;
1441 code_lsb &= 0x0E;
1442 if (code_msb == 0x94 && code_lsb == 0x0E)
1443 {
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);
1447 }
1448 else if (code_msb == 0x94 && code_lsb == 0x0C)
1449 {
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);
1453 }
1454 else
1455 abort ();
1456
1457 /* Fix the relocation's type. */
1458 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1459 R_AVR_13_PCREL);
1460
1461 /* Check for the vector section. There we don't want to
1462 modify the ordering! */
1463
1464 if (!strcmp (sec->name,".vectors")
1465 || !strcmp (sec->name,".jumptables"))
1466 {
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);
1470 }
1471 else
1472 {
1473 /* Delete two bytes of data. */
1474 if (!elf32_avr_relax_delete_bytes (abfd, sec,
1475 irel->r_offset + 2, 2))
1476 goto error_return;
1477
1478 /* That will change things, so, we should relax again.
1479 Note that this is not required, and it may be slow. */
1480 *again = TRUE;
1481 }
1482 }
1483 }
1484
1485 default:
1486 {
1487 unsigned char code_msb;
1488 unsigned char code_lsb;
1489 bfd_vma dot;
1490
1491 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1492 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
1493
1494 /* Get the address of this instruction. */
1495 dot = (sec->output_section->vma
1496 + sec->output_offset + irel->r_offset);
1497
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))
1501 {
1502 /* This insn is a rcall. */
1503 unsigned char next_insn_msb = 0;
1504 unsigned char next_insn_lsb = 0;
1505
1506 if (irel->r_offset + 3 < sec->size)
1507 {
1508 next_insn_msb =
1509 bfd_get_8 (abfd, contents + irel->r_offset + 3);
1510 next_insn_lsb =
1511 bfd_get_8 (abfd, contents + irel->r_offset + 2);
1512 }
1513
1514 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1515 {
1516 /* The next insn is a ret. We now convert the rcall insn
1517 into a rjmp instruction. */
1518 code_msb &= 0xef;
1519 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
1520 if (DEBUG_RELAX)
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);
1524 *again = TRUE;
1525 break;
1526 }
1527 }
1528 else if ((0x94 == (code_msb & 0xfe))
1529 && (0x0e == (code_lsb & 0x0e)))
1530 {
1531 /* This insn is a call. */
1532 unsigned char next_insn_msb = 0;
1533 unsigned char next_insn_lsb = 0;
1534
1535 if (irel->r_offset + 5 < sec->size)
1536 {
1537 next_insn_msb =
1538 bfd_get_8 (abfd, contents + irel->r_offset + 5);
1539 next_insn_lsb =
1540 bfd_get_8 (abfd, contents + irel->r_offset + 4);
1541 }
1542
1543 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1544 {
1545 /* The next insn is a ret. We now convert the call insn
1546 into a jmp instruction. */
1547
1548 code_lsb &= 0xfd;
1549 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
1550 if (DEBUG_RELAX)
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);
1554 *again = TRUE;
1555 break;
1556 }
1557 }
1558 else if ((0xc0 == (code_msb & 0xf0))
1559 || ((0x94 == (code_msb & 0xfe))
1560 && (0x0c == (code_lsb & 0x0e))))
1561 {
1562 /* This insn is a rjmp or a jmp. */
1563 unsigned char next_insn_msb = 0;
1564 unsigned char next_insn_lsb = 0;
1565 int insn_size;
1566
1567 if (0xc0 == (code_msb & 0xf0))
1568 insn_size = 2; /* rjmp insn */
1569 else
1570 insn_size = 4; /* jmp insn */
1571
1572 if (irel->r_offset + insn_size + 1 < sec->size)
1573 {
1574 next_insn_msb =
1575 bfd_get_8 (abfd, contents + irel->r_offset
1576 + insn_size + 1);
1577 next_insn_lsb =
1578 bfd_get_8 (abfd, contents + irel->r_offset
1579 + insn_size);
1580 }
1581
1582 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1583 {
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. */
1587
1588 int there_is_preceeding_non_skip_insn = 1;
1589 bfd_vma address_of_ret;
1590
1591 address_of_ret = dot + insn_size;
1592
1593 if (DEBUG_RELAX && (insn_size == 2))
1594 printf ("found rjmp / ret sequence at address 0x%x\n",
1595 (int) dot);
1596 if (DEBUG_RELAX && (insn_size == 4))
1597 printf ("found jmp / ret sequence at address 0x%x\n",
1598 (int) dot);
1599
1600 /* We have to make sure that there is a preceeding insn. */
1601 if (irel->r_offset >= 2)
1602 {
1603 unsigned char preceeding_msb;
1604 unsigned char preceeding_lsb;
1605 preceeding_msb =
1606 bfd_get_8 (abfd, contents + irel->r_offset - 1);
1607 preceeding_lsb =
1608 bfd_get_8 (abfd, contents + irel->r_offset - 2);
1609
1610 /* sbic. */
1611 if (0x99 == preceeding_msb)
1612 there_is_preceeding_non_skip_insn = 0;
1613
1614 /* sbis. */
1615 if (0x9b == preceeding_msb)
1616 there_is_preceeding_non_skip_insn = 0;
1617
1618 /* sbrc */
1619 if ((0xfc == (preceeding_msb & 0xfe)
1620 && (0x00 == (preceeding_lsb & 0x08))))
1621 there_is_preceeding_non_skip_insn = 0;
1622
1623 /* sbrs */
1624 if ((0xfe == (preceeding_msb & 0xfe)
1625 && (0x00 == (preceeding_lsb & 0x08))))
1626 there_is_preceeding_non_skip_insn = 0;
1627
1628 /* cpse */
1629 if (0x10 == (preceeding_msb & 0xfc))
1630 there_is_preceeding_non_skip_insn = 0;
1631
1632 if (there_is_preceeding_non_skip_insn == 0)
1633 if (DEBUG_RELAX)
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);
1637 }
1638 else
1639 {
1640 /* There is no previous instruction. */
1641 there_is_preceeding_non_skip_insn = 0;
1642 }
1643
1644 if (there_is_preceeding_non_skip_insn)
1645 {
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. */
1650
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;
1656
1657 sec_shndx =
1658 _bfd_elf_section_from_bfd_section (abfd, sec);
1659
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++)
1664 {
1665 if (isym->st_value == section_offset_of_ret_insn
1666 && isym->st_shndx == sec_shndx)
1667 {
1668 deleting_ret_is_safe = 0;
1669 if (DEBUG_RELAX)
1670 printf ("local label prevents deletion of ret "
1671 "insn at address 0x%x\n",
1672 (int) dot + insn_size);
1673 }
1674 }
1675
1676 /* Now check for global symbols. */
1677 {
1678 int symcount;
1679 struct elf_link_hash_entry **sym_hashes;
1680 struct elf_link_hash_entry **end_hashes;
1681
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++)
1688 {
1689 struct elf_link_hash_entry *sym_hash =
1690 *sym_hashes;
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)
1696 {
1697 deleting_ret_is_safe = 0;
1698 if (DEBUG_RELAX)
1699 printf ("global label prevents deletion of "
1700 "ret insn at address 0x%x\n",
1701 (int) dot + insn_size);
1702 }
1703 }
1704 }
1705 /* Now we check for relocations pointing to ret. */
1706 {
1707 Elf_Internal_Rela *irel;
1708 Elf_Internal_Rela *relend;
1709 Elf_Internal_Shdr *symtab_hdr;
1710
1711 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1712 relend = elf_section_data (sec)->relocs
1713 + sec->reloc_count;
1714
1715 for (irel = elf_section_data (sec)->relocs;
1716 irel < relend; irel++)
1717 {
1718 bfd_vma reloc_target = 0;
1719 bfd_vma symval;
1720 Elf_Internal_Sym *isymbuf = NULL;
1721
1722 /* Read this BFD's local symbols if we haven't
1723 done so already. */
1724 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1725 {
1726 isymbuf = (Elf_Internal_Sym *)
1727 symtab_hdr->contents;
1728 if (isymbuf == NULL)
1729 isymbuf = bfd_elf_get_elf_syms
1730 (abfd,
1731 symtab_hdr,
1732 symtab_hdr->sh_info, 0,
1733 NULL, NULL, NULL);
1734 if (isymbuf == NULL)
1735 break;
1736 }
1737
1738 /* Get the value of the symbol referred to
1739 by the reloc. */
1740 if (ELF32_R_SYM (irel->r_info)
1741 < symtab_hdr->sh_info)
1742 {
1743 /* A local symbol. */
1744 Elf_Internal_Sym *isym;
1745 asection *sym_sec;
1746
1747 isym = isymbuf
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;
1752
1753 /* If the reloc is absolute, it will not
1754 have a symbol or section associated
1755 with it. */
1756
1757 if (sym_sec)
1758 {
1759 symval +=
1760 sym_sec->output_section->vma
1761 + sym_sec->output_offset;
1762 reloc_target = symval + irel->r_addend;
1763 }
1764 else
1765 {
1766 reloc_target = symval + irel->r_addend;
1767 /* Reference symbol is absolute. */
1768 }
1769 }
1770 /* else ... reference symbol is extern. */
1771
1772 if (address_of_ret == reloc_target)
1773 {
1774 deleting_ret_is_safe = 0;
1775 if (DEBUG_RELAX)
1776 printf ("ret from "
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);
1781 }
1782 }
1783 }
1784
1785 if (deleting_ret_is_safe)
1786 {
1787 if (DEBUG_RELAX)
1788 printf ("unreachable ret instruction "
1789 "at address 0x%x deleted.\n",
1790 (int) dot + insn_size);
1791
1792 /* Delete two bytes of data. */
1793 if (!elf32_avr_relax_delete_bytes (abfd, sec,
1794 irel->r_offset + insn_size, 2))
1795 goto error_return;
1796
1797 /* That will change things, so, we should relax
1798 again. Note that this is not required, and it
1799 may be slow. */
1800 *again = TRUE;
1801 break;
1802 }
1803 }
1804
1805 }
1806 }
1807 break;
1808 }
1809 }
1810 }
1811
1812 if (contents != NULL
1813 && elf_section_data (sec)->this_hdr.contents != contents)
1814 {
1815 if (! link_info->keep_memory)
1816 free (contents);
1817 else
1818 {
1819 /* Cache the section contents for elf_link_input_bfd. */
1820 elf_section_data (sec)->this_hdr.contents = contents;
1821 }
1822 }
1823
1824 if (internal_relocs != NULL
1825 && elf_section_data (sec)->relocs != internal_relocs)
1826 free (internal_relocs);
1827
1828 return TRUE;
1829
1830 error_return:
1831 if (isymbuf != NULL
1832 && symtab_hdr->contents != (unsigned char *) isymbuf)
1833 free (isymbuf);
1834 if (contents != NULL
1835 && elf_section_data (sec)->this_hdr.contents != contents)
1836 free (contents);
1837 if (internal_relocs != NULL
1838 && elf_section_data (sec)->relocs != internal_relocs)
1839 free (internal_relocs);
1840
1841 return FALSE;
1842 }
1843
1844 /* This is a version of bfd_generic_get_relocated_section_contents
1845 which uses elf32_avr_relocate_section.
1846
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. */
1851
1852 static bfd_byte *
1853 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
1854 struct bfd_link_info *link_info,
1855 struct bfd_link_order *link_order,
1856 bfd_byte *data,
1857 bfd_boolean relocatable,
1858 asymbol **symbols)
1859 {
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;
1866
1867 /* We only need to handle the case of relaxing, or of having a
1868 particular set of section contents, specially. */
1869 if (relocatable
1870 || elf_section_data (input_section)->this_hdr.contents == NULL)
1871 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1872 link_order, data,
1873 relocatable,
1874 symbols);
1875 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1876
1877 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1878 (size_t) input_section->size);
1879
1880 if ((input_section->flags & SEC_RELOC) != 0
1881 && input_section->reloc_count > 0)
1882 {
1883 asection **secpp;
1884 Elf_Internal_Sym *isym, *isymend;
1885 bfd_size_type amt;
1886
1887 internal_relocs = (_bfd_elf_link_read_relocs
1888 (input_bfd, input_section, NULL, NULL, FALSE));
1889 if (internal_relocs == NULL)
1890 goto error_return;
1891
1892 if (symtab_hdr->sh_info != 0)
1893 {
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,
1898 NULL, NULL, NULL);
1899 if (isymbuf == NULL)
1900 goto error_return;
1901 }
1902
1903 amt = symtab_hdr->sh_info;
1904 amt *= sizeof (asection *);
1905 sections = bfd_malloc (amt);
1906 if (sections == NULL && amt != 0)
1907 goto error_return;
1908
1909 isymend = isymbuf + symtab_hdr->sh_info;
1910 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
1911 {
1912 asection *isec;
1913
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;
1920 else
1921 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1922
1923 *secpp = isec;
1924 }
1925
1926 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
1927 input_section, data, internal_relocs,
1928 isymbuf, sections))
1929 goto error_return;
1930
1931 if (sections != NULL)
1932 free (sections);
1933 if (isymbuf != NULL
1934 && symtab_hdr->contents != (unsigned char *) isymbuf)
1935 free (isymbuf);
1936 if (elf_section_data (input_section)->relocs != internal_relocs)
1937 free (internal_relocs);
1938 }
1939
1940 return data;
1941
1942 error_return:
1943 if (sections != NULL)
1944 free (sections);
1945 if (isymbuf != NULL
1946 && symtab_hdr->contents != (unsigned char *) isymbuf)
1947 free (isymbuf);
1948 if (internal_relocs != NULL
1949 && elf_section_data (input_section)->relocs != internal_relocs)
1950 free (internal_relocs);
1951 return NULL;
1952 }
1953
1954
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
1959
1960 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
1961 #define TARGET_LITTLE_NAME "elf32-avr"
1962
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
1974
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
1978
1979 #include "elf32-target.h"
This page took 0.08798 seconds and 5 git commands to generate.