Automatic date update in version.in
[binutils-gdb.git] / bfd / elf32-avr.c
blob12b68ba63f39fb055761c8f3c7332f810ef399bc
1 /* AVR-specific support for 32-bit ELF
2 Copyright (C) 1999-2024 Free Software Foundation, Inc.
3 Contributed by Denis Chertykov <denisc@overta.ru>
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 Boston, MA 02110-1301, USA. */
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/avr.h"
27 #include "elf32-avr.h"
29 /* Enable debugging printout at stdout with this variable. */
30 static bool debug_relax = false;
32 /* Enable debugging printout at stdout with this variable. */
33 static bool debug_stubs = false;
35 static bfd_reloc_status_type
36 bfd_elf_avr_diff_reloc (bfd *, arelent *, asymbol *, void *,
37 asection *, bfd *, char **);
39 /* Hash table initialization and handling. Code is taken from the hppa port
40 and adapted to the needs of AVR. */
42 /* We use two hash tables to hold information for linking avr objects.
44 The first is the elf32_avr_link_hash_table which is derived from the
45 stanard ELF linker hash table. We use this as a place to attach the other
46 hash table and some static information.
48 The second is the stub hash table which is derived from the base BFD
49 hash table. The stub hash table holds the information on the linker
50 stubs. */
52 struct elf32_avr_stub_hash_entry
54 /* Base hash table entry structure. */
55 struct bfd_hash_entry bh_root;
57 /* Offset within stub_sec of the beginning of this stub. */
58 bfd_vma stub_offset;
60 /* Given the symbol's value and its section we can determine its final
61 value when building the stubs (so the stub knows where to jump). */
62 bfd_vma target_value;
64 /* This way we could mark stubs to be no longer necessary. */
65 bool is_actually_needed;
68 struct elf32_avr_link_hash_table
70 /* The main hash table. */
71 struct elf_link_hash_table etab;
73 /* The stub hash table. */
74 struct bfd_hash_table bstab;
76 bool no_stubs;
78 /* Linker stub bfd. */
79 bfd *stub_bfd;
81 /* The stub section. */
82 asection *stub_sec;
84 /* Usually 0, unless we are generating code for a bootloader. Will
85 be initialized by elf32_avr_size_stubs to the vma offset of the
86 output section associated with the stub section. */
87 bfd_vma vector_base;
89 /* Assorted information used by elf32_avr_size_stubs. */
90 unsigned int bfd_count;
91 unsigned int top_index;
92 asection ** input_list;
93 Elf_Internal_Sym ** all_local_syms;
95 /* Tables for mapping vma beyond the 128k boundary to the address of the
96 corresponding stub. (AMT)
97 "amt_max_entry_cnt" reflects the number of entries that memory is allocated
98 for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
99 "amt_entry_cnt" informs how many of these entries actually contain
100 useful data. */
101 unsigned int amt_entry_cnt;
102 unsigned int amt_max_entry_cnt;
103 bfd_vma * amt_stub_offsets;
104 bfd_vma * amt_destination_addr;
107 /* Various hash macros and functions. */
108 #define avr_link_hash_table(p) \
109 ((is_elf_hash_table ((p)->hash) \
110 && elf_hash_table_id (elf_hash_table (p)) == AVR_ELF_DATA) \
111 ? (struct elf32_avr_link_hash_table *) (p)->hash : NULL)
113 #define avr_stub_hash_entry(ent) \
114 ((struct elf32_avr_stub_hash_entry *)(ent))
116 #define avr_stub_hash_lookup(table, string, create, copy) \
117 ((struct elf32_avr_stub_hash_entry *) \
118 bfd_hash_lookup ((table), (string), (create), (copy)))
120 static reloc_howto_type elf_avr_howto_table[] =
122 HOWTO (R_AVR_NONE, /* type */
123 0, /* rightshift */
124 0, /* size */
125 0, /* bitsize */
126 false, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_dont, /* complain_on_overflow */
129 bfd_elf_generic_reloc, /* special_function */
130 "R_AVR_NONE", /* name */
131 false, /* partial_inplace */
132 0, /* src_mask */
133 0, /* dst_mask */
134 false), /* pcrel_offset */
136 HOWTO (R_AVR_32, /* type */
137 0, /* rightshift */
138 4, /* size */
139 32, /* bitsize */
140 false, /* pc_relative */
141 0, /* bitpos */
142 complain_overflow_bitfield, /* complain_on_overflow */
143 bfd_elf_generic_reloc, /* special_function */
144 "R_AVR_32", /* name */
145 false, /* partial_inplace */
146 0xffffffff, /* src_mask */
147 0xffffffff, /* dst_mask */
148 false), /* pcrel_offset */
150 /* A 7 bit PC relative relocation. */
151 HOWTO (R_AVR_7_PCREL, /* type */
152 1, /* rightshift */
153 2, /* size */
154 7, /* bitsize */
155 true, /* pc_relative */
156 3, /* bitpos */
157 complain_overflow_bitfield, /* complain_on_overflow */
158 bfd_elf_generic_reloc, /* special_function */
159 "R_AVR_7_PCREL", /* name */
160 false, /* partial_inplace */
161 0xffff, /* src_mask */
162 0xffff, /* dst_mask */
163 true), /* pcrel_offset */
165 /* A 13 bit PC relative relocation. */
166 HOWTO (R_AVR_13_PCREL, /* type */
167 1, /* rightshift */
168 2, /* size */
169 13, /* bitsize */
170 true, /* pc_relative */
171 0, /* bitpos */
172 complain_overflow_bitfield, /* complain_on_overflow */
173 bfd_elf_generic_reloc, /* special_function */
174 "R_AVR_13_PCREL", /* name */
175 false, /* partial_inplace */
176 0xfff, /* src_mask */
177 0xfff, /* dst_mask */
178 true), /* pcrel_offset */
180 /* A 16 bit absolute relocation. */
181 HOWTO (R_AVR_16, /* type */
182 0, /* rightshift */
183 2, /* size */
184 16, /* 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_16", /* name */
190 false, /* partial_inplace */
191 0xffff, /* src_mask */
192 0xffff, /* dst_mask */
193 false), /* pcrel_offset */
195 /* A 16 bit absolute relocation for command address
196 Will be changed when linker stubs are needed. */
197 HOWTO (R_AVR_16_PM, /* type */
198 1, /* rightshift */
199 2, /* size */
200 16, /* bitsize */
201 false, /* pc_relative */
202 0, /* bitpos */
203 complain_overflow_bitfield, /* complain_on_overflow */
204 bfd_elf_generic_reloc, /* special_function */
205 "R_AVR_16_PM", /* name */
206 false, /* partial_inplace */
207 0xffff, /* src_mask */
208 0xffff, /* dst_mask */
209 false), /* pcrel_offset */
210 /* A low 8 bit absolute relocation of 16 bit address.
211 For LDI command. */
212 HOWTO (R_AVR_LO8_LDI, /* type */
213 0, /* rightshift */
214 2, /* size */
215 8, /* bitsize */
216 false, /* pc_relative */
217 0, /* bitpos */
218 complain_overflow_dont, /* complain_on_overflow */
219 bfd_elf_generic_reloc, /* special_function */
220 "R_AVR_LO8_LDI", /* name */
221 false, /* partial_inplace */
222 0xffff, /* src_mask */
223 0xffff, /* dst_mask */
224 false), /* pcrel_offset */
225 /* A high 8 bit absolute relocation of 16 bit address.
226 For LDI command. */
227 HOWTO (R_AVR_HI8_LDI, /* type */
228 8, /* rightshift */
229 2, /* size */
230 8, /* bitsize */
231 false, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_dont, /* complain_on_overflow */
234 bfd_elf_generic_reloc, /* special_function */
235 "R_AVR_HI8_LDI", /* name */
236 false, /* partial_inplace */
237 0xffff, /* src_mask */
238 0xffff, /* dst_mask */
239 false), /* pcrel_offset */
240 /* A high 6 bit absolute relocation of 22 bit address.
241 For LDI command. As well second most significant 8 bit value of
242 a 32 bit link-time constant. */
243 HOWTO (R_AVR_HH8_LDI, /* type */
244 16, /* rightshift */
245 2, /* size */
246 8, /* bitsize */
247 false, /* pc_relative */
248 0, /* bitpos */
249 complain_overflow_dont, /* complain_on_overflow */
250 bfd_elf_generic_reloc, /* special_function */
251 "R_AVR_HH8_LDI", /* name */
252 false, /* partial_inplace */
253 0xffff, /* src_mask */
254 0xffff, /* dst_mask */
255 false), /* pcrel_offset */
256 /* A negative low 8 bit absolute relocation of 16 bit address.
257 For LDI command. */
258 HOWTO (R_AVR_LO8_LDI_NEG, /* type */
259 0, /* rightshift */
260 2, /* size */
261 8, /* bitsize */
262 false, /* pc_relative */
263 0, /* bitpos */
264 complain_overflow_dont, /* complain_on_overflow */
265 bfd_elf_generic_reloc, /* special_function */
266 "R_AVR_LO8_LDI_NEG", /* name */
267 false, /* partial_inplace */
268 0xffff, /* src_mask */
269 0xffff, /* dst_mask */
270 false), /* pcrel_offset */
271 /* A negative high 8 bit absolute relocation of 16 bit address.
272 For LDI command. */
273 HOWTO (R_AVR_HI8_LDI_NEG, /* type */
274 8, /* rightshift */
275 2, /* size */
276 8, /* bitsize */
277 false, /* pc_relative */
278 0, /* bitpos */
279 complain_overflow_dont, /* complain_on_overflow */
280 bfd_elf_generic_reloc, /* special_function */
281 "R_AVR_HI8_LDI_NEG", /* name */
282 false, /* partial_inplace */
283 0xffff, /* src_mask */
284 0xffff, /* dst_mask */
285 false), /* pcrel_offset */
286 /* A negative high 6 bit absolute relocation of 22 bit address.
287 For LDI command. */
288 HOWTO (R_AVR_HH8_LDI_NEG, /* type */
289 16, /* rightshift */
290 2, /* size */
291 8, /* bitsize */
292 false, /* pc_relative */
293 0, /* bitpos */
294 complain_overflow_dont, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_AVR_HH8_LDI_NEG", /* name */
297 false, /* partial_inplace */
298 0xffff, /* src_mask */
299 0xffff, /* dst_mask */
300 false), /* pcrel_offset */
301 /* A low 8 bit absolute relocation of 24 bit program memory address.
302 For LDI command. Will not be changed when linker stubs are needed. */
303 HOWTO (R_AVR_LO8_LDI_PM, /* type */
304 1, /* rightshift */
305 2, /* size */
306 8, /* bitsize */
307 false, /* pc_relative */
308 0, /* bitpos */
309 complain_overflow_dont, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_AVR_LO8_LDI_PM", /* name */
312 false, /* partial_inplace */
313 0xffff, /* src_mask */
314 0xffff, /* dst_mask */
315 false), /* pcrel_offset */
316 /* A low 8 bit absolute relocation of 24 bit program memory address.
317 For LDI command. Will not be changed when linker stubs are needed. */
318 HOWTO (R_AVR_HI8_LDI_PM, /* type */
319 9, /* rightshift */
320 2, /* size */
321 8, /* bitsize */
322 false, /* pc_relative */
323 0, /* bitpos */
324 complain_overflow_dont, /* complain_on_overflow */
325 bfd_elf_generic_reloc, /* special_function */
326 "R_AVR_HI8_LDI_PM", /* name */
327 false, /* partial_inplace */
328 0xffff, /* src_mask */
329 0xffff, /* dst_mask */
330 false), /* pcrel_offset */
331 /* A low 8 bit absolute relocation of 24 bit program memory address.
332 For LDI command. Will not be changed when linker stubs are needed. */
333 HOWTO (R_AVR_HH8_LDI_PM, /* type */
334 17, /* rightshift */
335 2, /* size */
336 8, /* bitsize */
337 false, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_dont, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_AVR_HH8_LDI_PM", /* name */
342 false, /* partial_inplace */
343 0xffff, /* src_mask */
344 0xffff, /* dst_mask */
345 false), /* pcrel_offset */
346 /* A low 8 bit absolute relocation of 24 bit program memory address.
347 For LDI command. Will not be changed when linker stubs are needed. */
348 HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */
349 1, /* rightshift */
350 2, /* size */
351 8, /* bitsize */
352 false, /* pc_relative */
353 0, /* bitpos */
354 complain_overflow_dont, /* complain_on_overflow */
355 bfd_elf_generic_reloc, /* special_function */
356 "R_AVR_LO8_LDI_PM_NEG", /* name */
357 false, /* partial_inplace */
358 0xffff, /* src_mask */
359 0xffff, /* dst_mask */
360 false), /* pcrel_offset */
361 /* A low 8 bit absolute relocation of 24 bit program memory address.
362 For LDI command. Will not be changed when linker stubs are needed. */
363 HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */
364 9, /* rightshift */
365 2, /* size */
366 8, /* bitsize */
367 false, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_dont, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_AVR_HI8_LDI_PM_NEG", /* name */
372 false, /* partial_inplace */
373 0xffff, /* src_mask */
374 0xffff, /* dst_mask */
375 false), /* pcrel_offset */
376 /* A low 8 bit absolute relocation of 24 bit program memory address.
377 For LDI command. Will not be changed when linker stubs are needed. */
378 HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */
379 17, /* rightshift */
380 2, /* size */
381 8, /* bitsize */
382 false, /* pc_relative */
383 0, /* bitpos */
384 complain_overflow_dont, /* complain_on_overflow */
385 bfd_elf_generic_reloc, /* special_function */
386 "R_AVR_HH8_LDI_PM_NEG", /* name */
387 false, /* partial_inplace */
388 0xffff, /* src_mask */
389 0xffff, /* dst_mask */
390 false), /* pcrel_offset */
391 /* Relocation for CALL command in ATmega. */
392 HOWTO (R_AVR_CALL, /* type */
393 1, /* rightshift */
394 4, /* size */
395 23, /* bitsize */
396 false, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_dont,/* complain_on_overflow */
399 bfd_elf_generic_reloc, /* special_function */
400 "R_AVR_CALL", /* name */
401 false, /* partial_inplace */
402 0xffffffff, /* src_mask */
403 0xffffffff, /* dst_mask */
404 false), /* pcrel_offset */
405 /* A 16 bit absolute relocation of 16 bit address.
406 For LDI command. */
407 HOWTO (R_AVR_LDI, /* type */
408 0, /* rightshift */
409 2, /* size */
410 16, /* bitsize */
411 false, /* pc_relative */
412 0, /* bitpos */
413 complain_overflow_dont,/* complain_on_overflow */
414 bfd_elf_generic_reloc, /* special_function */
415 "R_AVR_LDI", /* name */
416 false, /* partial_inplace */
417 0xffff, /* src_mask */
418 0xffff, /* dst_mask */
419 false), /* pcrel_offset */
420 /* A 6 bit absolute relocation of 6 bit offset.
421 For ldd/sdd command. */
422 HOWTO (R_AVR_6, /* type */
423 0, /* rightshift */
424 1, /* size */
425 6, /* bitsize */
426 false, /* pc_relative */
427 0, /* bitpos */
428 complain_overflow_dont,/* complain_on_overflow */
429 bfd_elf_generic_reloc, /* special_function */
430 "R_AVR_6", /* name */
431 false, /* partial_inplace */
432 0xffff, /* src_mask */
433 0xffff, /* dst_mask */
434 false), /* pcrel_offset */
435 /* A 6 bit absolute relocation of 6 bit offset.
436 For sbiw/adiw command. */
437 HOWTO (R_AVR_6_ADIW, /* type */
438 0, /* rightshift */
439 1, /* size */
440 6, /* bitsize */
441 false, /* pc_relative */
442 0, /* bitpos */
443 complain_overflow_dont,/* complain_on_overflow */
444 bfd_elf_generic_reloc, /* special_function */
445 "R_AVR_6_ADIW", /* name */
446 false, /* partial_inplace */
447 0xffff, /* src_mask */
448 0xffff, /* dst_mask */
449 false), /* pcrel_offset */
450 /* Most significant 8 bit value of a 32 bit link-time constant. */
451 HOWTO (R_AVR_MS8_LDI, /* type */
452 24, /* rightshift */
453 2, /* size */
454 8, /* bitsize */
455 false, /* pc_relative */
456 0, /* bitpos */
457 complain_overflow_dont, /* complain_on_overflow */
458 bfd_elf_generic_reloc, /* special_function */
459 "R_AVR_MS8_LDI", /* name */
460 false, /* partial_inplace */
461 0xffff, /* src_mask */
462 0xffff, /* dst_mask */
463 false), /* pcrel_offset */
464 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
465 HOWTO (R_AVR_MS8_LDI_NEG, /* type */
466 24, /* rightshift */
467 2, /* size */
468 8, /* bitsize */
469 false, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_dont, /* complain_on_overflow */
472 bfd_elf_generic_reloc, /* special_function */
473 "R_AVR_MS8_LDI_NEG", /* name */
474 false, /* partial_inplace */
475 0xffff, /* src_mask */
476 0xffff, /* dst_mask */
477 false), /* pcrel_offset */
478 /* A low 8 bit absolute relocation of 24 bit program memory address.
479 For LDI command. Will be changed when linker stubs are needed. */
480 HOWTO (R_AVR_LO8_LDI_GS, /* type */
481 1, /* rightshift */
482 2, /* size */
483 8, /* bitsize */
484 false, /* pc_relative */
485 0, /* bitpos */
486 complain_overflow_dont, /* complain_on_overflow */
487 bfd_elf_generic_reloc, /* special_function */
488 "R_AVR_LO8_LDI_GS", /* name */
489 false, /* partial_inplace */
490 0xffff, /* src_mask */
491 0xffff, /* dst_mask */
492 false), /* pcrel_offset */
493 /* A low 8 bit absolute relocation of 24 bit program memory address.
494 For LDI command. Will be changed when linker stubs are needed. */
495 HOWTO (R_AVR_HI8_LDI_GS, /* type */
496 9, /* rightshift */
497 2, /* size */
498 8, /* bitsize */
499 false, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_dont, /* complain_on_overflow */
502 bfd_elf_generic_reloc, /* special_function */
503 "R_AVR_HI8_LDI_GS", /* name */
504 false, /* partial_inplace */
505 0xffff, /* src_mask */
506 0xffff, /* dst_mask */
507 false), /* pcrel_offset */
508 /* 8 bit offset. */
509 HOWTO (R_AVR_8, /* type */
510 0, /* rightshift */
511 1, /* size */
512 8, /* bitsize */
513 false, /* pc_relative */
514 0, /* bitpos */
515 complain_overflow_bitfield,/* complain_on_overflow */
516 bfd_elf_generic_reloc, /* special_function */
517 "R_AVR_8", /* name */
518 false, /* partial_inplace */
519 0x000000ff, /* src_mask */
520 0x000000ff, /* dst_mask */
521 false), /* pcrel_offset */
522 /* lo8-part to use in .byte lo8(sym). */
523 HOWTO (R_AVR_8_LO8, /* type */
524 0, /* rightshift */
525 1, /* size */
526 8, /* bitsize */
527 false, /* pc_relative */
528 0, /* bitpos */
529 complain_overflow_dont,/* complain_on_overflow */
530 bfd_elf_generic_reloc, /* special_function */
531 "R_AVR_8_LO8", /* name */
532 false, /* partial_inplace */
533 0xffffff, /* src_mask */
534 0xffffff, /* dst_mask */
535 false), /* pcrel_offset */
536 /* hi8-part to use in .byte hi8(sym). */
537 HOWTO (R_AVR_8_HI8, /* type */
538 8, /* rightshift */
539 1, /* size */
540 8, /* bitsize */
541 false, /* pc_relative */
542 0, /* bitpos */
543 complain_overflow_dont,/* complain_on_overflow */
544 bfd_elf_generic_reloc, /* special_function */
545 "R_AVR_8_HI8", /* name */
546 false, /* partial_inplace */
547 0xffffff, /* src_mask */
548 0xffffff, /* dst_mask */
549 false), /* pcrel_offset */
550 /* hlo8-part to use in .byte hlo8(sym). */
551 HOWTO (R_AVR_8_HLO8, /* type */
552 16, /* rightshift */
553 1, /* size */
554 8, /* bitsize */
555 false, /* pc_relative */
556 0, /* bitpos */
557 complain_overflow_dont,/* complain_on_overflow */
558 bfd_elf_generic_reloc, /* special_function */
559 "R_AVR_8_HLO8", /* name */
560 false, /* partial_inplace */
561 0xffffff, /* src_mask */
562 0xffffff, /* dst_mask */
563 false), /* pcrel_offset */
564 HOWTO (R_AVR_DIFF8, /* type */
565 0, /* rightshift */
566 1, /* size */
567 8, /* bitsize */
568 false, /* pc_relative */
569 0, /* bitpos */
570 complain_overflow_bitfield, /* complain_on_overflow */
571 bfd_elf_avr_diff_reloc, /* special_function */
572 "R_AVR_DIFF8", /* name */
573 false, /* partial_inplace */
574 0, /* src_mask */
575 0xff, /* dst_mask */
576 false), /* pcrel_offset */
577 HOWTO (R_AVR_DIFF16, /* type */
578 0, /* rightshift */
579 2, /* size */
580 16, /* bitsize */
581 false, /* pc_relative */
582 0, /* bitpos */
583 complain_overflow_bitfield, /* complain_on_overflow */
584 bfd_elf_avr_diff_reloc,/* special_function */
585 "R_AVR_DIFF16", /* name */
586 false, /* partial_inplace */
587 0, /* src_mask */
588 0xffff, /* dst_mask */
589 false), /* pcrel_offset */
590 HOWTO (R_AVR_DIFF32, /* type */
591 0, /* rightshift */
592 4, /* size */
593 32, /* bitsize */
594 false, /* pc_relative */
595 0, /* bitpos */
596 complain_overflow_bitfield, /* complain_on_overflow */
597 bfd_elf_avr_diff_reloc,/* special_function */
598 "R_AVR_DIFF32", /* name */
599 false, /* partial_inplace */
600 0, /* src_mask */
601 0xffffffff, /* dst_mask */
602 false), /* pcrel_offset */
603 /* 7 bit immediate for LDS/STS in Tiny core. */
604 HOWTO (R_AVR_LDS_STS_16, /* type */
605 0, /* rightshift */
606 2, /* size */
607 7, /* bitsize */
608 false, /* pc_relative */
609 0, /* bitpos */
610 complain_overflow_dont,/* complain_on_overflow */
611 bfd_elf_generic_reloc, /* special_function */
612 "R_AVR_LDS_STS_16", /* name */
613 false, /* partial_inplace */
614 0xffff, /* src_mask */
615 0xffff, /* dst_mask */
616 false), /* pcrel_offset */
618 HOWTO (R_AVR_PORT6, /* type */
619 0, /* rightshift */
620 1, /* size */
621 6, /* bitsize */
622 false, /* pc_relative */
623 0, /* bitpos */
624 complain_overflow_dont,/* complain_on_overflow */
625 bfd_elf_generic_reloc, /* special_function */
626 "R_AVR_PORT6", /* name */
627 false, /* partial_inplace */
628 0xffffff, /* src_mask */
629 0xffffff, /* dst_mask */
630 false), /* pcrel_offset */
631 HOWTO (R_AVR_PORT5, /* type */
632 0, /* rightshift */
633 1, /* size */
634 5, /* bitsize */
635 false, /* pc_relative */
636 0, /* bitpos */
637 complain_overflow_dont,/* complain_on_overflow */
638 bfd_elf_generic_reloc, /* special_function */
639 "R_AVR_PORT5", /* name */
640 false, /* partial_inplace */
641 0xffffff, /* src_mask */
642 0xffffff, /* dst_mask */
643 false), /* pcrel_offset */
645 /* A 32 bit PC relative relocation. */
646 HOWTO (R_AVR_32_PCREL, /* type */
647 0, /* rightshift */
648 4, /* size */
649 32, /* bitsize */
650 true, /* pc_relative */
651 0, /* bitpos */
652 complain_overflow_bitfield, /* complain_on_overflow */
653 bfd_elf_generic_reloc, /* special_function */
654 "R_AVR_32_PCREL", /* name */
655 false, /* partial_inplace */
656 0xffffffff, /* src_mask */
657 0xffffffff, /* dst_mask */
658 true), /* pcrel_offset */
661 /* Map BFD reloc types to AVR ELF reloc types. */
663 struct avr_reloc_map
665 bfd_reloc_code_real_type bfd_reloc_val;
666 unsigned int elf_reloc_val;
669 static const struct avr_reloc_map avr_reloc_map[] =
671 { BFD_RELOC_NONE, R_AVR_NONE },
672 { BFD_RELOC_32, R_AVR_32 },
673 { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL },
674 { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL },
675 { BFD_RELOC_16, R_AVR_16 },
676 { BFD_RELOC_AVR_16_PM, R_AVR_16_PM },
677 { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI},
678 { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI },
679 { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI },
680 { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI },
681 { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG },
682 { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG },
683 { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG },
684 { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG },
685 { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM },
686 { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS },
687 { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM },
688 { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS },
689 { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM },
690 { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG },
691 { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG },
692 { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG },
693 { BFD_RELOC_AVR_CALL, R_AVR_CALL },
694 { BFD_RELOC_AVR_LDI, R_AVR_LDI },
695 { BFD_RELOC_AVR_6, R_AVR_6 },
696 { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW },
697 { BFD_RELOC_8, R_AVR_8 },
698 { BFD_RELOC_AVR_8_LO, R_AVR_8_LO8 },
699 { BFD_RELOC_AVR_8_HI, R_AVR_8_HI8 },
700 { BFD_RELOC_AVR_8_HLO, R_AVR_8_HLO8 },
701 { BFD_RELOC_AVR_DIFF8, R_AVR_DIFF8 },
702 { BFD_RELOC_AVR_DIFF16, R_AVR_DIFF16 },
703 { BFD_RELOC_AVR_DIFF32, R_AVR_DIFF32 },
704 { BFD_RELOC_AVR_LDS_STS_16, R_AVR_LDS_STS_16},
705 { BFD_RELOC_AVR_PORT6, R_AVR_PORT6},
706 { BFD_RELOC_AVR_PORT5, R_AVR_PORT5},
707 { BFD_RELOC_32_PCREL, R_AVR_32_PCREL}
710 static const struct bfd_elf_special_section elf_avr_special_sections[] =
712 { STRING_COMMA_LEN (".noinit"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
713 { NULL, 0, 0, 0, 0 }
716 /* Meant to be filled one day with the wrap around address for the
717 specific device. I.e. should get the value 0x4000 for 16k devices,
718 0x8000 for 32k devices and so on.
720 We initialize it here with a value of 0x1000000 resulting in
721 that we will never suggest a wrap-around jump during relaxation.
722 The logic of the source code later on assumes that in
723 avr_pc_wrap_around one single bit is set. */
724 static bfd_vma avr_pc_wrap_around = 0x10000000;
726 /* If this variable holds a value different from zero, the linker relaxation
727 machine will try to optimize call/ret sequences by a single jump
728 instruction. This option could be switched off by a linker switch. */
729 static int avr_replace_call_ret_sequences = 1;
732 /* Per-section relaxation related information for avr. */
734 struct avr_relax_info
736 /* Track the avr property records that apply to this section. */
738 struct
740 /* Number of records in the list. */
741 unsigned count;
743 /* How many records worth of space have we allocated. */
744 unsigned allocated;
746 /* The records, only COUNT records are initialised. */
747 struct avr_property_record *items;
748 } records;
751 /* Per section data, specialised for avr. */
753 struct elf_avr_section_data
755 /* The standard data must appear first. */
756 struct bfd_elf_section_data elf;
758 /* Relaxation related information. */
759 struct avr_relax_info relax_info;
762 /* Possibly initialise avr specific data for new section SEC from ABFD. */
764 static bool
765 elf_avr_new_section_hook (bfd *abfd, asection *sec)
767 struct elf_avr_section_data *sdata;
769 sdata = bfd_zalloc (abfd, sizeof (*sdata));
770 if (sdata == NULL)
771 return false;
772 sec->used_by_bfd = sdata;
774 return _bfd_elf_new_section_hook (abfd, sec);
777 /* Return a pointer to the relaxation information for SEC. */
779 static struct avr_relax_info *
780 get_avr_relax_info (asection *sec)
782 struct elf_avr_section_data *section_data;
784 /* No info available if no section or if it is an output section. */
785 if (!sec || sec == sec->output_section)
786 return NULL;
788 section_data = (struct elf_avr_section_data *) elf_section_data (sec);
789 return &section_data->relax_info;
792 /* Initialise the per section relaxation information for SEC. */
794 static void
795 init_avr_relax_info (asection *sec)
797 struct avr_relax_info *relax_info = get_avr_relax_info (sec);
799 relax_info->records.count = 0;
800 relax_info->records.allocated = 0;
801 relax_info->records.items = NULL;
804 /* Initialize an entry in the stub hash table. */
806 static struct bfd_hash_entry *
807 stub_hash_newfunc (struct bfd_hash_entry *entry,
808 struct bfd_hash_table *table,
809 const char *string)
811 /* Allocate the structure if it has not already been allocated by a
812 subclass. */
813 if (entry == NULL)
815 entry = bfd_hash_allocate (table,
816 sizeof (struct elf32_avr_stub_hash_entry));
817 if (entry == NULL)
818 return entry;
821 /* Call the allocation method of the superclass. */
822 entry = bfd_hash_newfunc (entry, table, string);
823 if (entry != NULL)
825 struct elf32_avr_stub_hash_entry *hsh;
827 /* Initialize the local fields. */
828 hsh = avr_stub_hash_entry (entry);
829 hsh->stub_offset = 0;
830 hsh->target_value = 0;
833 return entry;
836 /* This function is just a straight passthrough to the real
837 function in linker.c. Its prupose is so that its address
838 can be compared inside the avr_link_hash_table macro. */
840 static struct bfd_hash_entry *
841 elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry,
842 struct bfd_hash_table * table,
843 const char * string)
845 return _bfd_elf_link_hash_newfunc (entry, table, string);
848 /* Free the derived linker hash table. */
850 static void
851 elf32_avr_link_hash_table_free (bfd *obfd)
853 struct elf32_avr_link_hash_table *htab
854 = (struct elf32_avr_link_hash_table *) obfd->link.hash;
856 /* Free the address mapping table. */
857 free (htab->amt_stub_offsets);
858 free (htab->amt_destination_addr);
860 bfd_hash_table_free (&htab->bstab);
861 _bfd_elf_link_hash_table_free (obfd);
864 /* Create the derived linker hash table. The AVR ELF port uses the derived
865 hash table to keep information specific to the AVR ELF linker (without
866 using static variables). */
868 static struct bfd_link_hash_table *
869 elf32_avr_link_hash_table_create (bfd *abfd)
871 struct elf32_avr_link_hash_table *htab;
872 size_t amt = sizeof (*htab);
874 htab = bfd_zmalloc (amt);
875 if (htab == NULL)
876 return NULL;
878 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
879 elf32_avr_link_hash_newfunc,
880 sizeof (struct elf_link_hash_entry),
881 AVR_ELF_DATA))
883 free (htab);
884 return NULL;
887 /* Init the stub hash table too. */
888 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
889 sizeof (struct elf32_avr_stub_hash_entry)))
891 _bfd_elf_link_hash_table_free (abfd);
892 return NULL;
894 htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free;
896 return &htab->etab.root;
899 /* Calculates the effective distance of a pc relative jump/call. */
901 static int
902 avr_relative_distance_considering_wrap_around (unsigned int distance)
904 unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
905 int dist_with_wrap_around = distance & wrap_around_mask;
907 if (dist_with_wrap_around >= ((int) (avr_pc_wrap_around >> 1)))
908 dist_with_wrap_around -= avr_pc_wrap_around;
910 return dist_with_wrap_around;
914 static reloc_howto_type *
915 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
916 bfd_reloc_code_real_type code)
918 unsigned int i;
920 for (i = 0;
921 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
922 i++)
923 if (avr_reloc_map[i].bfd_reloc_val == code)
924 return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
926 return NULL;
929 static reloc_howto_type *
930 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
931 const char *r_name)
933 unsigned int i;
935 for (i = 0;
936 i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
937 i++)
938 if (elf_avr_howto_table[i].name != NULL
939 && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
940 return &elf_avr_howto_table[i];
942 return NULL;
945 /* Set the howto pointer for an AVR ELF reloc. */
947 static bool
948 avr_info_to_howto_rela (bfd *abfd,
949 arelent *cache_ptr,
950 Elf_Internal_Rela *dst)
952 unsigned int r_type;
954 r_type = ELF32_R_TYPE (dst->r_info);
955 if (r_type >= (unsigned int) R_AVR_max)
957 /* xgettext:c-format */
958 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
959 abfd, r_type);
960 bfd_set_error (bfd_error_bad_value);
961 return false;
963 cache_ptr->howto = &elf_avr_howto_table[r_type];
964 return true;
967 static bool
968 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
970 return (relocation >= 0x020000);
973 /* Returns the address of the corresponding stub if there is one.
974 Returns otherwise an address above 0x020000. This function
975 could also be used, if there is no knowledge on the section where
976 the destination is found. */
978 static bfd_vma
979 avr_get_stub_addr (bfd_vma srel,
980 struct elf32_avr_link_hash_table *htab)
982 unsigned int sindex;
983 bfd_vma stub_sec_addr =
984 (htab->stub_sec->output_section->vma +
985 htab->stub_sec->output_offset);
987 for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++)
988 if (htab->amt_destination_addr[sindex] == srel)
989 return htab->amt_stub_offsets[sindex] + stub_sec_addr;
991 /* Return an address that could not be reached by 16 bit relocs. */
992 return 0x020000;
995 /* Perform a diff relocation. Nothing to do, as the difference value is already
996 written into the section's contents. */
998 static bfd_reloc_status_type
999 bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1000 arelent *reloc_entry ATTRIBUTE_UNUSED,
1001 asymbol *symbol ATTRIBUTE_UNUSED,
1002 void *data ATTRIBUTE_UNUSED,
1003 asection *input_section ATTRIBUTE_UNUSED,
1004 bfd *output_bfd ATTRIBUTE_UNUSED,
1005 char **error_message ATTRIBUTE_UNUSED)
1007 return bfd_reloc_ok;
1011 /* Perform a single relocation. By default we use the standard BFD
1012 routines, but a few relocs, we have to do them ourselves. */
1014 static bfd_reloc_status_type
1015 avr_final_link_relocate (reloc_howto_type * howto,
1016 bfd * input_bfd,
1017 asection * input_section,
1018 bfd_byte * contents,
1019 Elf_Internal_Rela * rel,
1020 bfd_vma relocation,
1021 struct elf32_avr_link_hash_table * htab)
1023 bfd_reloc_status_type r = bfd_reloc_ok;
1024 bfd_vma x;
1025 bfd_signed_vma srel;
1026 bfd_signed_vma reloc_addr;
1027 bool use_stubs = false;
1028 /* Usually is 0, unless we are generating code for a bootloader. */
1029 bfd_signed_vma base_addr = htab->vector_base;
1031 /* Absolute addr of the reloc in the final excecutable. */
1032 reloc_addr = rel->r_offset + input_section->output_section->vma
1033 + input_section->output_offset;
1035 switch (howto->type)
1037 case R_AVR_7_PCREL:
1038 contents += rel->r_offset;
1039 srel = (bfd_signed_vma) relocation;
1040 srel += rel->r_addend;
1041 srel -= rel->r_offset;
1042 srel -= 2; /* Branch instructions add 2 to the PC... */
1043 srel -= (input_section->output_section->vma +
1044 input_section->output_offset);
1046 if (srel & 1)
1047 return bfd_reloc_other;
1048 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
1049 return bfd_reloc_overflow;
1050 x = bfd_get_16 (input_bfd, contents);
1051 x = (x & 0xfc07) | (((srel >> 1) * 8) & 0x3f8);
1052 bfd_put_16 (input_bfd, x, contents);
1053 break;
1055 case R_AVR_13_PCREL:
1056 contents += rel->r_offset;
1057 srel = (bfd_signed_vma) relocation;
1058 srel += rel->r_addend;
1059 srel -= rel->r_offset;
1060 srel -= 2; /* Branch instructions add 2 to the PC... */
1061 srel -= (input_section->output_section->vma +
1062 input_section->output_offset);
1064 if (srel & 1)
1065 return bfd_reloc_other;
1067 srel = avr_relative_distance_considering_wrap_around (srel);
1069 /* AVR addresses commands as words. */
1070 srel >>= 1;
1072 /* Check for overflow. */
1073 if (srel < -2048 || srel > 2047)
1075 /* Relative distance is too large. */
1077 /* Always apply WRAPAROUND for avr2, avr25, and avr4. */
1078 switch (bfd_get_mach (input_bfd))
1080 case bfd_mach_avr2:
1081 case bfd_mach_avr25:
1082 case bfd_mach_avr4:
1083 break;
1085 default:
1086 return bfd_reloc_overflow;
1090 x = bfd_get_16 (input_bfd, contents);
1091 x = (x & 0xf000) | (srel & 0xfff);
1092 bfd_put_16 (input_bfd, x, contents);
1093 break;
1095 case R_AVR_LO8_LDI:
1096 contents += rel->r_offset;
1097 srel = (bfd_signed_vma) relocation + rel->r_addend;
1098 x = bfd_get_16 (input_bfd, contents);
1099 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1100 bfd_put_16 (input_bfd, x, contents);
1101 break;
1103 case R_AVR_LDI:
1104 contents += rel->r_offset;
1105 srel = (bfd_signed_vma) relocation + rel->r_addend;
1106 if (((srel > 0) && (srel & 0xffff) > 255)
1107 || ((srel < 0) && ((-srel) & 0xffff) > 128))
1108 /* Remove offset for data/eeprom section. */
1109 return bfd_reloc_overflow;
1111 x = bfd_get_16 (input_bfd, contents);
1112 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1113 bfd_put_16 (input_bfd, x, contents);
1114 break;
1116 case R_AVR_6:
1117 contents += rel->r_offset;
1118 srel = (bfd_signed_vma) relocation + rel->r_addend;
1119 if (((srel & 0xffff) > 63) || (srel < 0))
1120 /* Remove offset for data/eeprom section. */
1121 return bfd_reloc_overflow;
1122 x = bfd_get_16 (input_bfd, contents);
1123 x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
1124 | ((srel & (1 << 5)) << 8));
1125 bfd_put_16 (input_bfd, x, contents);
1126 break;
1128 case R_AVR_6_ADIW:
1129 contents += rel->r_offset;
1130 srel = (bfd_signed_vma) relocation + rel->r_addend;
1131 if (((srel & 0xffff) > 63) || (srel < 0))
1132 /* Remove offset for data/eeprom section. */
1133 return bfd_reloc_overflow;
1134 x = bfd_get_16 (input_bfd, contents);
1135 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
1136 bfd_put_16 (input_bfd, x, contents);
1137 break;
1139 case R_AVR_HI8_LDI:
1140 contents += rel->r_offset;
1141 srel = (bfd_signed_vma) relocation + rel->r_addend;
1142 srel = (srel >> 8) & 0xff;
1143 x = bfd_get_16 (input_bfd, contents);
1144 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1145 bfd_put_16 (input_bfd, x, contents);
1146 break;
1148 case R_AVR_HH8_LDI:
1149 contents += rel->r_offset;
1150 srel = (bfd_signed_vma) relocation + rel->r_addend;
1151 srel = (srel >> 16) & 0xff;
1152 x = bfd_get_16 (input_bfd, contents);
1153 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1154 bfd_put_16 (input_bfd, x, contents);
1155 break;
1157 case R_AVR_MS8_LDI:
1158 contents += rel->r_offset;
1159 srel = (bfd_signed_vma) relocation + rel->r_addend;
1160 srel = (srel >> 24) & 0xff;
1161 x = bfd_get_16 (input_bfd, contents);
1162 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1163 bfd_put_16 (input_bfd, x, contents);
1164 break;
1166 case R_AVR_LO8_LDI_NEG:
1167 contents += rel->r_offset;
1168 srel = (bfd_signed_vma) relocation + rel->r_addend;
1169 srel = -srel;
1170 x = bfd_get_16 (input_bfd, contents);
1171 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1172 bfd_put_16 (input_bfd, x, contents);
1173 break;
1175 case R_AVR_HI8_LDI_NEG:
1176 contents += rel->r_offset;
1177 srel = (bfd_signed_vma) relocation + rel->r_addend;
1178 srel = -srel;
1179 srel = (srel >> 8) & 0xff;
1180 x = bfd_get_16 (input_bfd, contents);
1181 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1182 bfd_put_16 (input_bfd, x, contents);
1183 break;
1185 case R_AVR_HH8_LDI_NEG:
1186 contents += rel->r_offset;
1187 srel = (bfd_signed_vma) relocation + rel->r_addend;
1188 srel = -srel;
1189 srel = (srel >> 16) & 0xff;
1190 x = bfd_get_16 (input_bfd, contents);
1191 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1192 bfd_put_16 (input_bfd, x, contents);
1193 break;
1195 case R_AVR_MS8_LDI_NEG:
1196 contents += rel->r_offset;
1197 srel = (bfd_signed_vma) relocation + rel->r_addend;
1198 srel = -srel;
1199 srel = (srel >> 24) & 0xff;
1200 x = bfd_get_16 (input_bfd, contents);
1201 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1202 bfd_put_16 (input_bfd, x, contents);
1203 break;
1205 case R_AVR_LO8_LDI_GS:
1206 use_stubs = (!htab->no_stubs);
1207 /* Fall through. */
1208 case R_AVR_LO8_LDI_PM:
1209 contents += rel->r_offset;
1210 srel = (bfd_signed_vma) relocation + rel->r_addend;
1212 if (use_stubs
1213 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1215 bfd_vma old_srel = srel;
1217 /* We need to use the address of the stub instead. */
1218 srel = avr_get_stub_addr (srel, htab);
1219 if (debug_stubs)
1220 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1221 "reloc at address 0x%x.\n",
1222 (unsigned int) srel,
1223 (unsigned int) old_srel,
1224 (unsigned int) reloc_addr);
1226 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1227 return bfd_reloc_overflow;
1230 if (srel & 1)
1231 return bfd_reloc_other;
1232 srel = srel >> 1;
1233 x = bfd_get_16 (input_bfd, contents);
1234 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1235 bfd_put_16 (input_bfd, x, contents);
1236 break;
1238 case R_AVR_HI8_LDI_GS:
1239 use_stubs = (!htab->no_stubs);
1240 /* Fall through. */
1241 case R_AVR_HI8_LDI_PM:
1242 contents += rel->r_offset;
1243 srel = (bfd_signed_vma) relocation + rel->r_addend;
1245 if (use_stubs
1246 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1248 bfd_vma old_srel = srel;
1250 /* We need to use the address of the stub instead. */
1251 srel = avr_get_stub_addr (srel, htab);
1252 if (debug_stubs)
1253 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1254 "reloc at address 0x%x.\n",
1255 (unsigned int) srel,
1256 (unsigned int) old_srel,
1257 (unsigned int) reloc_addr);
1259 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1260 return bfd_reloc_overflow;
1263 if (srel & 1)
1264 return bfd_reloc_other;
1265 srel = srel >> 1;
1266 srel = (srel >> 8) & 0xff;
1267 x = bfd_get_16 (input_bfd, contents);
1268 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1269 bfd_put_16 (input_bfd, x, contents);
1270 break;
1272 case R_AVR_HH8_LDI_PM:
1273 contents += rel->r_offset;
1274 srel = (bfd_signed_vma) relocation + rel->r_addend;
1275 if (srel & 1)
1276 return bfd_reloc_other;
1277 srel = srel >> 1;
1278 srel = (srel >> 16) & 0xff;
1279 x = bfd_get_16 (input_bfd, contents);
1280 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1281 bfd_put_16 (input_bfd, x, contents);
1282 break;
1284 case R_AVR_LO8_LDI_PM_NEG:
1285 contents += rel->r_offset;
1286 srel = (bfd_signed_vma) relocation + rel->r_addend;
1287 srel = -srel;
1288 if (srel & 1)
1289 return bfd_reloc_other;
1290 srel = srel >> 1;
1291 x = bfd_get_16 (input_bfd, contents);
1292 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1293 bfd_put_16 (input_bfd, x, contents);
1294 break;
1296 case R_AVR_HI8_LDI_PM_NEG:
1297 contents += rel->r_offset;
1298 srel = (bfd_signed_vma) relocation + rel->r_addend;
1299 srel = -srel;
1300 if (srel & 1)
1301 return bfd_reloc_other;
1302 srel = srel >> 1;
1303 srel = (srel >> 8) & 0xff;
1304 x = bfd_get_16 (input_bfd, contents);
1305 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1306 bfd_put_16 (input_bfd, x, contents);
1307 break;
1309 case R_AVR_HH8_LDI_PM_NEG:
1310 contents += rel->r_offset;
1311 srel = (bfd_signed_vma) relocation + rel->r_addend;
1312 srel = -srel;
1313 if (srel & 1)
1314 return bfd_reloc_other;
1315 srel = srel >> 1;
1316 srel = (srel >> 16) & 0xff;
1317 x = bfd_get_16 (input_bfd, contents);
1318 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1319 bfd_put_16 (input_bfd, x, contents);
1320 break;
1322 case R_AVR_CALL:
1323 contents += rel->r_offset;
1324 srel = (bfd_signed_vma) relocation + rel->r_addend;
1325 if (srel & 1)
1326 return bfd_reloc_other;
1327 srel = srel >> 1;
1328 x = bfd_get_16 (input_bfd, contents);
1329 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1330 bfd_put_16 (input_bfd, x, contents);
1331 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1332 break;
1334 case R_AVR_16_PM:
1335 use_stubs = (!htab->no_stubs);
1336 contents += rel->r_offset;
1337 srel = (bfd_signed_vma) relocation + rel->r_addend;
1339 if (use_stubs
1340 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1342 bfd_vma old_srel = srel;
1344 /* We need to use the address of the stub instead. */
1345 srel = avr_get_stub_addr (srel,htab);
1346 if (debug_stubs)
1347 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1348 "reloc at address 0x%x.\n",
1349 (unsigned int) srel,
1350 (unsigned int) old_srel,
1351 (unsigned int) reloc_addr);
1353 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1354 return bfd_reloc_overflow;
1357 if (srel & 1)
1358 return bfd_reloc_other;
1359 srel = srel >> 1;
1360 bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1361 break;
1363 case R_AVR_DIFF8:
1364 case R_AVR_DIFF16:
1365 case R_AVR_DIFF32:
1366 /* Nothing to do here, as contents already contains the diff value. */
1367 r = bfd_reloc_ok;
1368 break;
1370 case R_AVR_LDS_STS_16:
1371 contents += rel->r_offset;
1372 srel = (bfd_signed_vma) relocation + rel->r_addend;
1373 if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf)
1374 return bfd_reloc_overflow;
1375 srel = srel & 0x7f;
1376 x = bfd_get_16 (input_bfd, contents);
1377 x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2);
1378 bfd_put_16 (input_bfd, x, contents);
1379 break;
1381 case R_AVR_PORT6:
1382 contents += rel->r_offset;
1383 srel = (bfd_signed_vma) relocation + rel->r_addend;
1384 if ((srel & 0xffff) > 0x3f)
1385 return bfd_reloc_overflow;
1386 x = bfd_get_16 (input_bfd, contents);
1387 x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f);
1388 bfd_put_16 (input_bfd, x, contents);
1389 break;
1391 case R_AVR_PORT5:
1392 contents += rel->r_offset;
1393 srel = (bfd_signed_vma) relocation + rel->r_addend;
1394 if ((srel & 0xffff) > 0x1f)
1395 return bfd_reloc_overflow;
1396 x = bfd_get_16 (input_bfd, contents);
1397 x = (x & 0xff07) | ((srel & 0x1f) << 3);
1398 bfd_put_16 (input_bfd, x, contents);
1399 break;
1401 default:
1402 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1403 contents, rel->r_offset,
1404 relocation, rel->r_addend);
1407 return r;
1410 /* Relocate an AVR ELF section. */
1412 static int
1413 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1414 struct bfd_link_info *info,
1415 bfd *input_bfd,
1416 asection *input_section,
1417 bfd_byte *contents,
1418 Elf_Internal_Rela *relocs,
1419 Elf_Internal_Sym *local_syms,
1420 asection **local_sections)
1422 Elf_Internal_Shdr * symtab_hdr;
1423 struct elf_link_hash_entry ** sym_hashes;
1424 Elf_Internal_Rela * rel;
1425 Elf_Internal_Rela * relend;
1426 struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
1428 if (htab == NULL)
1429 return false;
1431 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1432 sym_hashes = elf_sym_hashes (input_bfd);
1433 relend = relocs + input_section->reloc_count;
1435 for (rel = relocs; rel < relend; rel ++)
1437 reloc_howto_type * howto;
1438 unsigned long r_symndx;
1439 Elf_Internal_Sym * sym;
1440 asection * sec;
1441 struct elf_link_hash_entry * h;
1442 bfd_vma relocation;
1443 bfd_reloc_status_type r;
1444 const char * name;
1445 int r_type;
1447 r_type = ELF32_R_TYPE (rel->r_info);
1448 r_symndx = ELF32_R_SYM (rel->r_info);
1449 howto = elf_avr_howto_table + r_type;
1450 h = NULL;
1451 sym = NULL;
1452 sec = NULL;
1454 if (r_symndx < symtab_hdr->sh_info)
1456 sym = local_syms + r_symndx;
1457 sec = local_sections [r_symndx];
1458 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1460 name = bfd_elf_string_from_elf_section
1461 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1462 name = name == NULL ? bfd_section_name (sec) : name;
1464 else
1466 bool unresolved_reloc, warned, ignored;
1468 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1469 r_symndx, symtab_hdr, sym_hashes,
1470 h, sec, relocation,
1471 unresolved_reloc, warned, ignored);
1473 name = h->root.root.string;
1476 if (sec != NULL && discarded_section (sec))
1477 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1478 rel, 1, relend, howto, 0, contents);
1480 if (bfd_link_relocatable (info))
1481 continue;
1483 r = avr_final_link_relocate (howto, input_bfd, input_section,
1484 contents, rel, relocation, htab);
1486 if (r != bfd_reloc_ok)
1488 switch (r)
1490 case bfd_reloc_overflow:
1491 (*info->callbacks->reloc_overflow)
1492 (info, (h ? &h->root : NULL), name, howto->name,
1493 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1494 break;
1496 case bfd_reloc_undefined:
1497 (*info->callbacks->undefined_symbol)
1498 (info, name, input_bfd, input_section, rel->r_offset, true);
1499 break;
1501 case bfd_reloc_outofrange:
1502 /* xgettext:c-format */
1503 (*info->callbacks->einfo)
1504 (_("%X%H: %s against `%s':"
1505 " error: relocation applies outside section\n"),
1506 input_bfd, input_section, rel->r_offset, howto->name, name);
1507 break;
1509 case bfd_reloc_other:
1510 /* xgettext:c-format */
1511 (*info->callbacks->einfo)
1512 (_("%X%H: %s against `%s':"
1513 " error: relocation target address is odd\n"),
1514 input_bfd, input_section, rel->r_offset, howto->name, name);
1515 break;
1517 default:
1518 /* xgettext:c-format */
1519 (*info->callbacks->einfo)
1520 (_("%X%H: %s against `%s':"
1521 " internal error: unexpected relocation result %d\n"),
1522 input_bfd, input_section, rel->r_offset, howto->name, name, r);
1523 break;
1528 return true;
1531 /* The final processing done just before writing out a AVR ELF object
1532 file. This gets the AVR architecture right based on the machine
1533 number. */
1535 static bool
1536 bfd_elf_avr_final_write_processing (bfd *abfd)
1538 unsigned long val;
1540 switch (bfd_get_mach (abfd))
1542 default:
1543 case bfd_mach_avr2:
1544 val = E_AVR_MACH_AVR2;
1545 break;
1547 case bfd_mach_avr1:
1548 val = E_AVR_MACH_AVR1;
1549 break;
1551 case bfd_mach_avr25:
1552 val = E_AVR_MACH_AVR25;
1553 break;
1555 case bfd_mach_avr3:
1556 val = E_AVR_MACH_AVR3;
1557 break;
1559 case bfd_mach_avr31:
1560 val = E_AVR_MACH_AVR31;
1561 break;
1563 case bfd_mach_avr35:
1564 val = E_AVR_MACH_AVR35;
1565 break;
1567 case bfd_mach_avr4:
1568 val = E_AVR_MACH_AVR4;
1569 break;
1571 case bfd_mach_avr5:
1572 val = E_AVR_MACH_AVR5;
1573 break;
1575 case bfd_mach_avr51:
1576 val = E_AVR_MACH_AVR51;
1577 break;
1579 case bfd_mach_avr6:
1580 val = E_AVR_MACH_AVR6;
1581 break;
1583 case bfd_mach_avrxmega1:
1584 val = E_AVR_MACH_XMEGA1;
1585 break;
1587 case bfd_mach_avrxmega2:
1588 val = E_AVR_MACH_XMEGA2;
1589 break;
1591 case bfd_mach_avrxmega3:
1592 val = E_AVR_MACH_XMEGA3;
1593 break;
1595 case bfd_mach_avrxmega4:
1596 val = E_AVR_MACH_XMEGA4;
1597 break;
1599 case bfd_mach_avrxmega5:
1600 val = E_AVR_MACH_XMEGA5;
1601 break;
1603 case bfd_mach_avrxmega6:
1604 val = E_AVR_MACH_XMEGA6;
1605 break;
1607 case bfd_mach_avrxmega7:
1608 val = E_AVR_MACH_XMEGA7;
1609 break;
1611 case bfd_mach_avrtiny:
1612 val = E_AVR_MACH_AVRTINY;
1613 break;
1616 elf_elfheader (abfd)->e_machine = EM_AVR;
1617 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1618 elf_elfheader (abfd)->e_flags |= val;
1619 return _bfd_elf_final_write_processing (abfd);
1622 /* Set the right machine number. */
1624 static bool
1625 elf32_avr_object_p (bfd *abfd)
1627 unsigned int e_set = bfd_mach_avr2;
1629 if (elf_elfheader (abfd)->e_machine == EM_AVR
1630 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1632 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1634 switch (e_mach)
1636 default:
1637 case E_AVR_MACH_AVR2:
1638 e_set = bfd_mach_avr2;
1639 break;
1641 case E_AVR_MACH_AVR1:
1642 e_set = bfd_mach_avr1;
1643 break;
1645 case E_AVR_MACH_AVR25:
1646 e_set = bfd_mach_avr25;
1647 break;
1649 case E_AVR_MACH_AVR3:
1650 e_set = bfd_mach_avr3;
1651 break;
1653 case E_AVR_MACH_AVR31:
1654 e_set = bfd_mach_avr31;
1655 break;
1657 case E_AVR_MACH_AVR35:
1658 e_set = bfd_mach_avr35;
1659 break;
1661 case E_AVR_MACH_AVR4:
1662 e_set = bfd_mach_avr4;
1663 break;
1665 case E_AVR_MACH_AVR5:
1666 e_set = bfd_mach_avr5;
1667 break;
1669 case E_AVR_MACH_AVR51:
1670 e_set = bfd_mach_avr51;
1671 break;
1673 case E_AVR_MACH_AVR6:
1674 e_set = bfd_mach_avr6;
1675 break;
1677 case E_AVR_MACH_XMEGA1:
1678 e_set = bfd_mach_avrxmega1;
1679 break;
1681 case E_AVR_MACH_XMEGA2:
1682 e_set = bfd_mach_avrxmega2;
1683 break;
1685 case E_AVR_MACH_XMEGA3:
1686 e_set = bfd_mach_avrxmega3;
1687 break;
1689 case E_AVR_MACH_XMEGA4:
1690 e_set = bfd_mach_avrxmega4;
1691 break;
1693 case E_AVR_MACH_XMEGA5:
1694 e_set = bfd_mach_avrxmega5;
1695 break;
1697 case E_AVR_MACH_XMEGA6:
1698 e_set = bfd_mach_avrxmega6;
1699 break;
1701 case E_AVR_MACH_XMEGA7:
1702 e_set = bfd_mach_avrxmega7;
1703 break;
1705 case E_AVR_MACH_AVRTINY:
1706 e_set = bfd_mach_avrtiny;
1707 break;
1710 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1711 e_set);
1714 /* Returns whether the relocation type passed is a diff reloc. */
1716 static bool
1717 elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel)
1719 return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8
1720 ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16
1721 || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32);
1724 /* Reduce the diff value written in the section by count if the shrinked
1725 insn address happens to fall between the two symbols for which this
1726 diff reloc was emitted. */
1728 static void
1729 elf32_avr_adjust_diff_reloc_value (bfd *abfd,
1730 struct bfd_section *isec,
1731 Elf_Internal_Rela *irel,
1732 bfd_vma symval,
1733 bfd_vma shrinked_insn_address,
1734 int count)
1736 unsigned char *reloc_contents = NULL;
1737 unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
1738 if (isec_contents == NULL)
1740 if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
1741 return;
1743 elf_section_data (isec)->this_hdr.contents = isec_contents;
1746 reloc_contents = isec_contents + irel->r_offset;
1748 /* Read value written in object file. */
1749 bfd_signed_vma x = 0;
1750 switch (ELF32_R_TYPE (irel->r_info))
1752 case R_AVR_DIFF8:
1754 x = bfd_get_signed_8 (abfd, reloc_contents);
1755 break;
1757 case R_AVR_DIFF16:
1759 x = bfd_get_signed_16 (abfd, reloc_contents);
1760 break;
1762 case R_AVR_DIFF32:
1764 x = bfd_get_signed_32 (abfd, reloc_contents);
1765 break;
1767 default:
1769 BFD_FAIL();
1773 /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
1774 into the object file at the reloc offset. sym2's logical value is
1775 symval (<start_of_section>) + reloc addend. Compute the start and end
1776 addresses and check if the shrinked insn falls between sym1 and sym2. */
1778 bfd_vma sym2_address = symval + irel->r_addend;
1779 bfd_vma sym1_address = sym2_address - x;
1781 /* Don't assume sym2 is bigger than sym1 - the difference
1782 could be negative. Compute start and end addresses, and
1783 use those to see if they span shrinked_insn_address. */
1785 bfd_vma start_address = sym1_address < sym2_address
1786 ? sym1_address : sym2_address;
1787 bfd_vma end_address = sym1_address > sym2_address
1788 ? sym1_address : sym2_address;
1791 if (shrinked_insn_address >= start_address
1792 && shrinked_insn_address < end_address)
1794 /* Reduce the diff value by count bytes and write it back into section
1795 contents. */
1796 bfd_signed_vma new_diff = x < 0 ? x + count : x - count;
1798 if (sym2_address > shrinked_insn_address)
1799 irel->r_addend -= count;
1801 switch (ELF32_R_TYPE (irel->r_info))
1803 case R_AVR_DIFF8:
1805 bfd_put_signed_8 (abfd, new_diff, reloc_contents);
1806 break;
1808 case R_AVR_DIFF16:
1810 bfd_put_signed_16 (abfd, new_diff & 0xFFFF, reloc_contents);
1811 break;
1813 case R_AVR_DIFF32:
1815 bfd_put_signed_32 (abfd, new_diff & 0xFFFFFFFF, reloc_contents);
1816 break;
1818 default:
1820 BFD_FAIL();
1827 static void
1828 elf32_avr_adjust_reloc_if_spans_insn (bfd *abfd,
1829 asection *isec,
1830 Elf_Internal_Rela *irel, bfd_vma symval,
1831 bfd_vma shrinked_insn_address,
1832 bfd_vma shrink_boundary,
1833 int count)
1836 if (elf32_avr_is_diff_reloc (irel))
1838 elf32_avr_adjust_diff_reloc_value (abfd, isec, irel,
1839 symval,
1840 shrinked_insn_address,
1841 count);
1843 else
1845 bfd_vma reloc_value = symval + irel->r_addend;
1846 bool addend_within_shrink_boundary = reloc_value <= shrink_boundary;
1848 bool reloc_spans_insn =
1849 (symval <= shrinked_insn_address
1850 && reloc_value > shrinked_insn_address
1851 && addend_within_shrink_boundary);
1853 if (! reloc_spans_insn)
1854 return;
1856 irel->r_addend -= count;
1858 if (debug_relax)
1859 printf ("Relocation's addend needed to be fixed \n");
1863 static bool
1864 avr_should_move_sym (symvalue symval,
1865 bfd_vma start,
1866 bfd_vma end,
1867 bool did_pad)
1869 bool sym_within_boundary = did_pad ? symval < end : symval <= end;
1870 return (symval > start && sym_within_boundary);
1873 static bool
1874 avr_should_reduce_sym_size (symvalue symval,
1875 symvalue symend,
1876 bfd_vma start,
1877 bfd_vma end,
1878 bool did_pad)
1880 bool sym_end_within_boundary = did_pad ? symend < end : symend <= end;
1881 return (symval <= start && symend > start && sym_end_within_boundary);
1884 static bool
1885 avr_should_increase_sym_size (symvalue symval,
1886 symvalue symend,
1887 bfd_vma start,
1888 bfd_vma end,
1889 bool did_pad)
1891 return (avr_should_move_sym (symval, start, end, did_pad)
1892 && symend >= end && did_pad);
1895 /* Delete some bytes from a section while changing the size of an instruction.
1896 The parameter "addr" denotes the section-relative offset pointing just
1897 behind the shrinked instruction. "addr+count" point at the first
1898 byte just behind the original unshrinked instruction. If delete_shrinks_insn
1899 is FALSE, we are deleting redundant padding bytes from relax_info prop
1900 record handling. In that case, addr is section-relative offset of start
1901 of padding, and count is the number of padding bytes to delete. */
1903 static bool
1904 elf32_avr_relax_delete_bytes (bfd *abfd,
1905 asection *sec,
1906 bfd_vma addr,
1907 int count,
1908 bool delete_shrinks_insn)
1910 Elf_Internal_Shdr *symtab_hdr;
1911 unsigned int sec_shndx;
1912 bfd_byte *contents;
1913 Elf_Internal_Rela *irel, *irelend;
1914 Elf_Internal_Sym *isym;
1915 Elf_Internal_Sym *isymbuf = NULL;
1916 bfd_vma toaddr;
1917 struct elf_link_hash_entry **sym_hashes;
1918 struct elf_link_hash_entry **end_hashes;
1919 unsigned int symcount;
1920 struct avr_relax_info *relax_info;
1921 struct avr_property_record *prop_record = NULL;
1922 bool did_shrink = false;
1923 bool did_pad = false;
1925 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1926 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1927 contents = elf_section_data (sec)->this_hdr.contents;
1928 relax_info = get_avr_relax_info (sec);
1930 toaddr = sec->size;
1932 if (relax_info->records.count > 0)
1934 /* There should be no property record within the range of deleted
1935 bytes, however, there might be a property record for ADDR, this is
1936 how we handle alignment directives.
1937 Find the next (if any) property record after the deleted bytes. */
1938 unsigned int i;
1940 for (i = 0; i < relax_info->records.count; ++i)
1942 bfd_vma offset = relax_info->records.items [i].offset;
1944 BFD_ASSERT (offset <= addr || offset >= (addr + count));
1945 if (offset >= (addr + count))
1947 prop_record = &relax_info->records.items [i];
1948 toaddr = offset;
1949 break;
1954 irel = elf_section_data (sec)->relocs;
1955 irelend = irel + sec->reloc_count;
1957 /* Actually delete the bytes. */
1958 if (toaddr - addr - count > 0)
1960 memmove (contents + addr, contents + addr + count,
1961 (size_t) (toaddr - addr - count));
1962 did_shrink = true;
1964 if (prop_record == NULL)
1966 sec->size -= count;
1967 did_shrink = true;
1969 else
1971 /* Use the property record to fill in the bytes we've opened up. */
1972 int fill = 0;
1973 switch (prop_record->type)
1975 case RECORD_ORG_AND_FILL:
1976 fill = prop_record->data.org.fill;
1977 /* Fall through. */
1978 case RECORD_ORG:
1979 break;
1980 case RECORD_ALIGN_AND_FILL:
1981 fill = prop_record->data.align.fill;
1982 /* Fall through. */
1983 case RECORD_ALIGN:
1984 prop_record->data.align.preceding_deleted += count;
1985 break;
1987 /* If toaddr == (addr + count), then we didn't delete anything, yet
1988 we fill count bytes backwards from toaddr. This is still ok - we
1989 end up overwriting the bytes we would have deleted. We just need
1990 to remember we didn't delete anything i.e. don't set did_shrink,
1991 so that we don't corrupt reloc offsets or symbol values.*/
1992 memset (contents + toaddr - count, fill, count);
1993 did_pad = true;
1996 if (!did_shrink)
1997 return true;
1999 /* Adjust all the reloc addresses. */
2000 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2002 bfd_vma old_reloc_address;
2004 old_reloc_address = (sec->output_section->vma
2005 + sec->output_offset + irel->r_offset);
2007 /* Get the new reloc address. */
2008 if ((irel->r_offset > addr
2009 && irel->r_offset < toaddr))
2011 if (debug_relax)
2012 printf ("Relocation at address 0x%x needs to be moved.\n"
2013 "Old section offset: 0x%x, New section offset: 0x%x \n",
2014 (unsigned int) old_reloc_address,
2015 (unsigned int) irel->r_offset,
2016 (unsigned int) ((irel->r_offset) - count));
2018 irel->r_offset -= count;
2023 /* The reloc's own addresses are now ok. However, we need to readjust
2024 the reloc's addend, i.e. the reloc's value if two conditions are met:
2025 1.) the reloc is relative to a symbol in this section that
2026 is located in front of the shrinked instruction
2027 2.) symbol plus addend end up behind the shrinked instruction.
2029 The most common case where this happens are relocs relative to
2030 the section-start symbol.
2032 This step needs to be done for all of the sections of the bfd. */
2035 struct bfd_section *isec;
2037 for (isec = abfd->sections; isec; isec = isec->next)
2039 bfd_vma symval;
2040 bfd_vma shrinked_insn_address;
2042 if (isec->reloc_count == 0)
2043 continue;
2045 shrinked_insn_address = (sec->output_section->vma
2046 + sec->output_offset + addr);
2047 if (delete_shrinks_insn)
2048 shrinked_insn_address -= count;
2050 irel = elf_section_data (isec)->relocs;
2051 /* PR 12161: Read in the relocs for this section if necessary. */
2052 if (irel == NULL)
2053 irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, true);
2055 for (irelend = irel + isec->reloc_count;
2056 irel < irelend;
2057 irel++)
2059 /* Read this BFD's local symbols if we haven't done
2060 so already. */
2061 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2063 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2064 if (isymbuf == NULL)
2065 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2066 symtab_hdr->sh_info, 0,
2067 NULL, NULL, NULL);
2068 if (isymbuf == NULL)
2069 return false;
2072 /* Get the value of the symbol referred to by the reloc. */
2073 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2075 /* A local symbol. */
2076 asection *sym_sec;
2078 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2079 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2080 symval = isym->st_value;
2081 /* If the reloc is absolute, it will not have
2082 a symbol or section associated with it. */
2083 if (sym_sec == sec)
2085 /* If there is an alignment boundary, we only need to
2086 adjust addends that end up below the boundary. */
2087 bfd_vma shrink_boundary = (toaddr
2088 + sec->output_section->vma
2089 + sec->output_offset);
2091 symval += sym_sec->output_section->vma
2092 + sym_sec->output_offset;
2094 if (debug_relax)
2095 printf ("Checking if the relocation's "
2096 "addend needs corrections.\n"
2097 "Address of anchor symbol: 0x%x \n"
2098 "Address of relocation target: 0x%x \n"
2099 "Address of relaxed insn: 0x%x \n",
2100 (unsigned int) symval,
2101 (unsigned int) (symval + irel->r_addend),
2102 (unsigned int) shrinked_insn_address);
2104 elf32_avr_adjust_reloc_if_spans_insn (abfd, isec, irel,
2105 symval,
2106 shrinked_insn_address,
2107 shrink_boundary,
2108 count);
2110 /* else...Reference symbol is absolute. No adjustment needed. */
2112 /* else...Reference symbol is extern. No need for adjusting
2113 the addend. */
2118 /* Adjust the local symbols defined in this section. */
2119 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2120 /* Fix PR 9841, there may be no local symbols. */
2121 if (isym != NULL)
2123 Elf_Internal_Sym *isymend;
2125 isymend = isym + symtab_hdr->sh_info;
2126 for (; isym < isymend; isym++)
2128 if (isym->st_shndx == sec_shndx)
2130 symvalue symval = isym->st_value;
2131 symvalue symend = symval + isym->st_size;
2132 if (avr_should_reduce_sym_size (symval, symend,
2133 addr, toaddr, did_pad))
2135 /* If this assert fires then we have a symbol that ends
2136 part way through an instruction. Does that make
2137 sense? */
2138 BFD_ASSERT (isym->st_value + isym->st_size >= addr + count);
2139 isym->st_size -= count;
2141 else if (avr_should_increase_sym_size (symval, symend,
2142 addr, toaddr, did_pad))
2143 isym->st_size += count;
2145 if (avr_should_move_sym (symval, addr, toaddr, did_pad))
2146 isym->st_value -= count;
2151 /* Now adjust the global symbols defined in this section. */
2152 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2153 - symtab_hdr->sh_info);
2154 sym_hashes = elf_sym_hashes (abfd);
2155 end_hashes = sym_hashes + symcount;
2156 for (; sym_hashes < end_hashes; sym_hashes++)
2158 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2159 if ((sym_hash->root.type == bfd_link_hash_defined
2160 || sym_hash->root.type == bfd_link_hash_defweak)
2161 && sym_hash->root.u.def.section == sec)
2163 symvalue symval = sym_hash->root.u.def.value;
2164 symvalue symend = symval + sym_hash->size;
2166 if (avr_should_reduce_sym_size (symval, symend,
2167 addr, toaddr, did_pad))
2169 /* If this assert fires then we have a symbol that ends
2170 part way through an instruction. Does that make
2171 sense? */
2172 BFD_ASSERT (symend >= addr + count);
2173 sym_hash->size -= count;
2175 else if (avr_should_increase_sym_size (symval, symend,
2176 addr, toaddr, did_pad))
2177 sym_hash->size += count;
2179 if (avr_should_move_sym (symval, addr, toaddr, did_pad))
2180 sym_hash->root.u.def.value -= count;
2184 return true;
2187 static Elf_Internal_Sym *
2188 retrieve_local_syms (bfd *input_bfd)
2190 Elf_Internal_Shdr *symtab_hdr;
2191 Elf_Internal_Sym *isymbuf;
2192 size_t locsymcount;
2194 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2195 locsymcount = symtab_hdr->sh_info;
2197 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2198 if (isymbuf == NULL && locsymcount != 0)
2199 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
2200 NULL, NULL, NULL);
2202 /* Save the symbols for this input file so they won't be read again. */
2203 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
2204 symtab_hdr->contents = (unsigned char *) isymbuf;
2206 return isymbuf;
2209 /* Get the input section for a given symbol index.
2210 If the symbol is:
2211 . a section symbol, return the section;
2212 . a common symbol, return the common section;
2213 . an undefined symbol, return the undefined section;
2214 . an indirect symbol, follow the links;
2215 . an absolute value, return the absolute section. */
2217 static asection *
2218 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
2220 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2221 asection *target_sec = NULL;
2222 if (r_symndx < symtab_hdr->sh_info)
2224 Elf_Internal_Sym *isymbuf;
2225 unsigned int section_index;
2227 isymbuf = retrieve_local_syms (abfd);
2228 section_index = isymbuf[r_symndx].st_shndx;
2230 if (section_index == SHN_UNDEF)
2231 target_sec = bfd_und_section_ptr;
2232 else if (section_index == SHN_ABS)
2233 target_sec = bfd_abs_section_ptr;
2234 else if (section_index == SHN_COMMON)
2235 target_sec = bfd_com_section_ptr;
2236 else
2237 target_sec = bfd_section_from_elf_index (abfd, section_index);
2239 else
2241 unsigned long indx = r_symndx - symtab_hdr->sh_info;
2242 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
2244 while (h->root.type == bfd_link_hash_indirect
2245 || h->root.type == bfd_link_hash_warning)
2246 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2248 switch (h->root.type)
2250 case bfd_link_hash_defined:
2251 case bfd_link_hash_defweak:
2252 target_sec = h->root.u.def.section;
2253 break;
2254 case bfd_link_hash_common:
2255 target_sec = bfd_com_section_ptr;
2256 break;
2257 case bfd_link_hash_undefined:
2258 case bfd_link_hash_undefweak:
2259 target_sec = bfd_und_section_ptr;
2260 break;
2261 default: /* New indirect warning. */
2262 target_sec = bfd_und_section_ptr;
2263 break;
2266 return target_sec;
2269 /* Get the section-relative offset for a symbol number. */
2271 static bfd_vma
2272 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
2274 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2275 bfd_vma offset = 0;
2277 if (r_symndx < symtab_hdr->sh_info)
2279 Elf_Internal_Sym *isymbuf;
2280 isymbuf = retrieve_local_syms (abfd);
2281 offset = isymbuf[r_symndx].st_value;
2283 else
2285 unsigned long indx = r_symndx - symtab_hdr->sh_info;
2286 struct elf_link_hash_entry *h =
2287 elf_sym_hashes (abfd)[indx];
2289 while (h->root.type == bfd_link_hash_indirect
2290 || h->root.type == bfd_link_hash_warning)
2291 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2292 if (h->root.type == bfd_link_hash_defined
2293 || h->root.type == bfd_link_hash_defweak)
2294 offset = h->root.u.def.value;
2296 return offset;
2299 /* Iterate over the property records in R_LIST, and copy each record into
2300 the list of records within the relaxation information for the section to
2301 which the record applies. */
2303 static void
2304 avr_elf32_assign_records_to_sections (struct avr_property_record_list *r_list)
2306 unsigned int i;
2308 for (i = 0; i < r_list->record_count; ++i)
2310 struct avr_relax_info *relax_info;
2312 relax_info = get_avr_relax_info (r_list->records [i].section);
2313 BFD_ASSERT (relax_info != NULL);
2315 if (relax_info->records.count
2316 == relax_info->records.allocated)
2318 /* Allocate more space. */
2319 bfd_size_type size;
2321 relax_info->records.allocated += 10;
2322 size = (sizeof (struct avr_property_record)
2323 * relax_info->records.allocated);
2324 relax_info->records.items
2325 = bfd_realloc (relax_info->records.items, size);
2328 memcpy (&relax_info->records.items [relax_info->records.count],
2329 &r_list->records [i],
2330 sizeof (struct avr_property_record));
2331 relax_info->records.count++;
2335 /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the
2336 ordering callback from QSORT. */
2338 static int
2339 avr_property_record_compare (const void *ap, const void *bp)
2341 const struct avr_property_record *a
2342 = (struct avr_property_record *) ap;
2343 const struct avr_property_record *b
2344 = (struct avr_property_record *) bp;
2346 if (a->offset != b->offset)
2347 return (a->offset - b->offset);
2349 if (a->section != b->section)
2350 return bfd_section_vma (a->section) - bfd_section_vma (b->section);
2352 return (a->type - b->type);
2355 /* Load all of the avr property sections from all of the bfd objects
2356 referenced from LINK_INFO. All of the records within each property
2357 section are assigned to the STRUCT AVR_RELAX_INFO within the section
2358 specific data of the appropriate section. */
2360 static void
2361 avr_load_all_property_sections (struct bfd_link_info *link_info)
2363 bfd *abfd;
2364 asection *sec;
2366 /* Initialize the per-section relaxation info. */
2367 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2368 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2370 init_avr_relax_info (sec);
2373 /* Load the descriptor tables from .avr.prop sections. */
2374 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2376 struct avr_property_record_list *r_list;
2378 r_list = avr_elf32_load_property_records (abfd);
2379 if (r_list != NULL)
2380 avr_elf32_assign_records_to_sections (r_list);
2382 free (r_list);
2385 /* Now, for every section, ensure that the descriptor list in the
2386 relaxation data is sorted by ascending offset within the section. */
2387 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2388 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2390 struct avr_relax_info *relax_info = get_avr_relax_info (sec);
2391 if (relax_info && relax_info->records.count > 0)
2393 unsigned int i;
2395 qsort (relax_info->records.items,
2396 relax_info->records.count,
2397 sizeof (struct avr_property_record),
2398 avr_property_record_compare);
2400 /* For debug purposes, list all the descriptors. */
2401 for (i = 0; i < relax_info->records.count; ++i)
2403 switch (relax_info->records.items [i].type)
2405 case RECORD_ORG:
2406 break;
2407 case RECORD_ORG_AND_FILL:
2408 break;
2409 case RECORD_ALIGN:
2410 break;
2411 case RECORD_ALIGN_AND_FILL:
2412 break;
2419 /* This function handles relaxing for the avr.
2420 Many important relaxing opportunities within functions are already
2421 realized by the compiler itself.
2422 Here we try to replace call (4 bytes) -> rcall (2 bytes)
2423 and jump -> rjmp (safes also 2 bytes).
2424 As well we now optimize seqences of
2425 - call/rcall function
2426 - ret
2427 to yield
2428 - jmp/rjmp function
2429 - ret
2430 . In case that within a sequence
2431 - jmp/rjmp label
2432 - ret
2433 the ret could no longer be reached it is optimized away. In order
2434 to check if the ret is no longer needed, it is checked that the ret's address
2435 is not the target of a branch or jump within the same section, it is checked
2436 that there is no skip instruction before the jmp/rjmp and that there
2437 is no local or global label place at the address of the ret.
2439 We refrain from relaxing within sections ".vectors" and
2440 ".jumptables" in order to maintain the position of the instructions.
2441 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
2442 if possible. (In future one could possibly use the space of the nop
2443 for the first instruction of the irq service function.
2445 The .jumptables sections is meant to be used for a future tablejump variant
2446 for the devices with 3-byte program counter where the table itself
2447 contains 4-byte jump instructions whose relative offset must not
2448 be changed. */
2450 static bool
2451 elf32_avr_relax_section (bfd *abfd,
2452 asection *sec,
2453 struct bfd_link_info *link_info,
2454 bool *again)
2456 Elf_Internal_Shdr *symtab_hdr;
2457 Elf_Internal_Rela *internal_relocs;
2458 Elf_Internal_Rela *irel, *irelend;
2459 bfd_byte *contents = NULL;
2460 Elf_Internal_Sym *isymbuf = NULL;
2461 struct elf32_avr_link_hash_table *htab;
2462 static bool relaxation_initialised = false;
2464 if (!relaxation_initialised)
2466 relaxation_initialised = true;
2468 /* Load entries from the .avr.prop sections. */
2469 avr_load_all_property_sections (link_info);
2472 /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
2473 relaxing. Such shrinking can cause issues for the sections such
2474 as .vectors and .jumptables. Instead the unused bytes should be
2475 filled with nop instructions. */
2476 bool shrinkable = true;
2478 if (!strcmp (sec->name,".vectors")
2479 || !strcmp (sec->name,".jumptables"))
2480 shrinkable = false;
2482 if (bfd_link_relocatable (link_info))
2483 (*link_info->callbacks->einfo)
2484 (_("%P%F: --relax and -r may not be used together\n"));
2486 htab = avr_link_hash_table (link_info);
2487 if (htab == NULL)
2488 return false;
2490 /* Assume nothing changes. */
2491 *again = false;
2493 if ((!htab->no_stubs) && (sec == htab->stub_sec))
2495 /* We are just relaxing the stub section.
2496 Let's calculate the size needed again. */
2497 bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
2499 if (debug_relax)
2500 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
2501 (int) last_estimated_stub_section_size);
2503 elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
2504 link_info, false);
2506 /* Check if the number of trampolines changed. */
2507 if (last_estimated_stub_section_size != htab->stub_sec->size)
2508 *again = true;
2510 if (debug_relax)
2511 printf ("Size of stub section after this pass: %i\n",
2512 (int) htab->stub_sec->size);
2514 return true;
2517 /* We don't have to do anything for a relocatable link, if
2518 this section does not have relocs, or if this is not a
2519 code section. */
2520 if (bfd_link_relocatable (link_info)
2521 || sec->reloc_count == 0
2522 || (sec->flags & SEC_RELOC) == 0
2523 || (sec->flags & SEC_HAS_CONTENTS) == 0
2524 || (sec->flags & SEC_CODE) == 0)
2525 return true;
2527 /* Check if the object file to relax uses internal symbols so that we
2528 could fix up the relocations. */
2529 if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
2530 return true;
2532 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2534 /* Get a copy of the native relocations. */
2535 internal_relocs = (_bfd_elf_link_read_relocs
2536 (abfd, sec, NULL, NULL, link_info->keep_memory));
2537 if (internal_relocs == NULL)
2538 goto error_return;
2540 /* Walk through the relocs looking for relaxing opportunities. */
2541 irelend = internal_relocs + sec->reloc_count;
2542 for (irel = internal_relocs; irel < irelend; irel++)
2544 bfd_vma symval;
2546 if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
2547 && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
2548 && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
2549 continue;
2551 /* Get the section contents if we haven't done so already. */
2552 if (contents == NULL)
2554 /* Get cached copy if it exists. */
2555 if (elf_section_data (sec)->this_hdr.contents != NULL)
2556 contents = elf_section_data (sec)->this_hdr.contents;
2557 else
2559 /* Go get them off disk. */
2560 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2561 goto error_return;
2565 /* Read this BFD's local symbols if we haven't done so already. */
2566 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2568 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2569 if (isymbuf == NULL)
2570 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2571 symtab_hdr->sh_info, 0,
2572 NULL, NULL, NULL);
2573 if (isymbuf == NULL)
2574 goto error_return;
2578 /* Get the value of the symbol referred to by the reloc. */
2579 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2581 /* A local symbol. */
2582 Elf_Internal_Sym *isym;
2583 asection *sym_sec;
2585 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2586 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2587 symval = isym->st_value;
2588 /* If the reloc is absolute, it will not have
2589 a symbol or section associated with it. */
2590 if (sym_sec)
2591 symval += sym_sec->output_section->vma
2592 + sym_sec->output_offset;
2594 else
2596 unsigned long indx;
2597 struct elf_link_hash_entry *h;
2599 /* An external symbol. */
2600 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2601 h = elf_sym_hashes (abfd)[indx];
2602 BFD_ASSERT (h != NULL);
2603 if (h->root.type != bfd_link_hash_defined
2604 && h->root.type != bfd_link_hash_defweak)
2605 /* This appears to be a reference to an undefined
2606 symbol. Just ignore it--it will be caught by the
2607 regular reloc processing. */
2608 continue;
2610 symval = (h->root.u.def.value
2611 + h->root.u.def.section->output_section->vma
2612 + h->root.u.def.section->output_offset);
2615 /* For simplicity of coding, we are going to modify the section
2616 contents, the section relocs, and the BFD symbol table. We
2617 must tell the rest of the code not to free up this
2618 information. It would be possible to instead create a table
2619 of changes which have to be made, as is done in coff-mips.c;
2620 that would be more work, but would require less memory when
2621 the linker is run. */
2622 switch (ELF32_R_TYPE (irel->r_info))
2624 /* Try to turn a 22-bit absolute call/jump into an 13-bit
2625 pc-relative rcall/rjmp. */
2626 case R_AVR_CALL:
2628 bfd_vma value = symval + irel->r_addend;
2629 bfd_vma dot, gap;
2630 int distance_short_enough = 0;
2632 /* Get the address of this instruction. */
2633 dot = (sec->output_section->vma
2634 + sec->output_offset + irel->r_offset);
2636 /* Compute the distance from this insn to the branch target. */
2637 gap = value - dot;
2639 /* The ISA manual states that addressable range is PC - 2k + 1 to
2640 PC + 2k. In bytes, that would be -4094 <= PC <= 4096. The range
2641 is shifted one word to the right, because pc-relative instructions
2642 implicitly add one word i.e. rjmp 0 jumps to next insn, not the
2643 current one.
2644 Therefore, for the !shrinkable case, the range is as above.
2645 If shrinkable, then the current code only deletes bytes 3 and
2646 4 of the absolute call/jmp, so the forward jump range increases
2647 by 2 bytes, but the backward (negative) jump range remains
2648 the same. */
2651 /* Check if the gap falls in the range that can be accommodated
2652 in 13bits signed (It is 12bits when encoded, as we deal with
2653 word addressing). */
2654 if (!shrinkable && ((int) gap >= -4094 && (int) gap <= 4096))
2655 distance_short_enough = 1;
2656 /* If shrinkable, then we can check for a range of distance which
2657 is two bytes farther on the positive direction because the call
2658 or jump target will be closer by two bytes after the
2659 relaxation. */
2660 else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4098))
2661 distance_short_enough = 1;
2663 /* Here we handle the wrap-around case. E.g. for a 16k device
2664 we could use a rjmp to jump from address 0x100 to 0x3d00!
2665 In order to make this work properly, we need to fill the
2666 vaiable avr_pc_wrap_around with the appropriate value.
2667 I.e. 0x4000 for a 16k device. */
2669 /* Shrinking the code size makes the gaps larger in the
2670 case of wrap-arounds. So we use a heuristical safety
2671 margin to avoid that during relax the distance gets
2672 again too large for the short jumps. Let's assume
2673 a typical code-size reduction due to relax for a
2674 16k device of 600 bytes. So let's use twice the
2675 typical value as safety margin. */
2676 int rgap;
2677 int safety_margin;
2679 int assumed_shrink = 600;
2680 if (avr_pc_wrap_around > 0x4000)
2681 assumed_shrink = 900;
2683 safety_margin = 2 * assumed_shrink;
2685 rgap = avr_relative_distance_considering_wrap_around (gap);
2687 if (rgap >= (-4092 + safety_margin)
2688 && rgap <= (4094 - safety_margin))
2689 distance_short_enough = 1;
2692 if (distance_short_enough)
2694 unsigned char code_msb;
2695 unsigned char code_lsb;
2697 if (debug_relax)
2698 printf ("shrinking jump/call instruction at address 0x%x"
2699 " in section %s\n\n",
2700 (int) dot, sec->name);
2702 /* Note that we've changed the relocs, section contents,
2703 etc. */
2704 elf_section_data (sec)->relocs = internal_relocs;
2705 elf_section_data (sec)->this_hdr.contents = contents;
2706 symtab_hdr->contents = (unsigned char *) isymbuf;
2708 /* Get the instruction code for relaxing. */
2709 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
2710 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2712 /* Mask out the relocation bits. */
2713 code_msb &= 0x94;
2714 code_lsb &= 0x0E;
2715 if (code_msb == 0x94 && code_lsb == 0x0E)
2717 /* we are changing call -> rcall . */
2718 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
2719 bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
2721 else if (code_msb == 0x94 && code_lsb == 0x0C)
2723 /* we are changeing jump -> rjmp. */
2724 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
2725 bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
2727 else
2728 abort ();
2730 /* Fix the relocation's type. */
2731 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2732 R_AVR_13_PCREL);
2734 /* We should not modify the ordering if 'shrinkable' is
2735 FALSE. */
2736 if (!shrinkable)
2738 /* Let's insert a nop. */
2739 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
2740 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
2742 else
2744 /* Delete two bytes of data. */
2745 if (!elf32_avr_relax_delete_bytes (abfd, sec,
2746 irel->r_offset + 2, 2,
2747 true))
2748 goto error_return;
2750 /* That will change things, so, we should relax again.
2751 Note that this is not required, and it may be slow. */
2752 *again = true;
2756 /* Fall through. */
2758 default:
2760 unsigned char code_msb;
2761 unsigned char code_lsb;
2762 bfd_vma dot;
2764 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2765 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
2767 /* Get the address of this instruction. */
2768 dot = (sec->output_section->vma
2769 + sec->output_offset + irel->r_offset);
2771 /* Here we look for rcall/ret or call/ret sequences that could be
2772 safely replaced by rjmp/ret or jmp/ret. */
2773 if (((code_msb & 0xf0) == 0xd0)
2774 && avr_replace_call_ret_sequences)
2776 /* This insn is a rcall. */
2777 unsigned char next_insn_msb = 0;
2778 unsigned char next_insn_lsb = 0;
2780 if (irel->r_offset + 3 < sec->size)
2782 next_insn_msb =
2783 bfd_get_8 (abfd, contents + irel->r_offset + 3);
2784 next_insn_lsb =
2785 bfd_get_8 (abfd, contents + irel->r_offset + 2);
2788 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2790 /* The next insn is a ret. We now convert the rcall insn
2791 into a rjmp instruction. */
2792 code_msb &= 0xef;
2793 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
2794 if (debug_relax)
2795 printf ("converted rcall/ret sequence at address 0x%x"
2796 " into rjmp/ret sequence. Section is %s\n\n",
2797 (int) dot, sec->name);
2798 *again = true;
2799 break;
2802 else if ((0x94 == (code_msb & 0xfe))
2803 && (0x0e == (code_lsb & 0x0e))
2804 && avr_replace_call_ret_sequences)
2806 /* This insn is a call. */
2807 unsigned char next_insn_msb = 0;
2808 unsigned char next_insn_lsb = 0;
2810 if (irel->r_offset + 5 < sec->size)
2812 next_insn_msb =
2813 bfd_get_8 (abfd, contents + irel->r_offset + 5);
2814 next_insn_lsb =
2815 bfd_get_8 (abfd, contents + irel->r_offset + 4);
2818 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2820 /* The next insn is a ret. We now convert the call insn
2821 into a jmp instruction. */
2823 code_lsb &= 0xfd;
2824 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
2825 if (debug_relax)
2826 printf ("converted call/ret sequence at address 0x%x"
2827 " into jmp/ret sequence. Section is %s\n\n",
2828 (int) dot, sec->name);
2829 *again = true;
2830 break;
2833 else if ((0xc0 == (code_msb & 0xf0))
2834 || ((0x94 == (code_msb & 0xfe))
2835 && (0x0c == (code_lsb & 0x0e))))
2837 /* This insn is a rjmp or a jmp. */
2838 unsigned char next_insn_msb = 0;
2839 unsigned char next_insn_lsb = 0;
2840 int insn_size;
2842 if (0xc0 == (code_msb & 0xf0))
2843 insn_size = 2; /* rjmp insn */
2844 else
2845 insn_size = 4; /* jmp insn */
2847 if (irel->r_offset + insn_size + 1 < sec->size)
2849 next_insn_msb =
2850 bfd_get_8 (abfd, contents + irel->r_offset
2851 + insn_size + 1);
2852 next_insn_lsb =
2853 bfd_get_8 (abfd, contents + irel->r_offset
2854 + insn_size);
2857 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2859 /* The next insn is a ret. We possibly could delete
2860 this ret. First we need to check for preceding
2861 sbis/sbic/sbrs or cpse "skip" instructions. */
2863 int there_is_preceding_non_skip_insn = 1;
2864 bfd_vma address_of_ret;
2866 address_of_ret = dot + insn_size;
2868 if (debug_relax && (insn_size == 2))
2869 printf ("found rjmp / ret sequence at address 0x%x\n",
2870 (int) dot);
2871 if (debug_relax && (insn_size == 4))
2872 printf ("found jmp / ret sequence at address 0x%x\n",
2873 (int) dot);
2875 /* We have to make sure that there is a preceding insn. */
2876 if (irel->r_offset >= 2)
2878 unsigned char preceding_msb;
2879 unsigned char preceding_lsb;
2881 preceding_msb =
2882 bfd_get_8 (abfd, contents + irel->r_offset - 1);
2883 preceding_lsb =
2884 bfd_get_8 (abfd, contents + irel->r_offset - 2);
2886 /* sbic. */
2887 if (0x99 == preceding_msb)
2888 there_is_preceding_non_skip_insn = 0;
2890 /* sbis. */
2891 if (0x9b == preceding_msb)
2892 there_is_preceding_non_skip_insn = 0;
2894 /* sbrc */
2895 if ((0xfc == (preceding_msb & 0xfe)
2896 && (0x00 == (preceding_lsb & 0x08))))
2897 there_is_preceding_non_skip_insn = 0;
2899 /* sbrs */
2900 if ((0xfe == (preceding_msb & 0xfe)
2901 && (0x00 == (preceding_lsb & 0x08))))
2902 there_is_preceding_non_skip_insn = 0;
2904 /* cpse */
2905 if (0x10 == (preceding_msb & 0xfc))
2906 there_is_preceding_non_skip_insn = 0;
2908 if (there_is_preceding_non_skip_insn == 0)
2909 if (debug_relax)
2910 printf ("preceding skip insn prevents deletion of"
2911 " ret insn at Addy 0x%x in section %s\n",
2912 (int) dot + 2, sec->name);
2914 else
2916 /* There is no previous instruction. */
2917 there_is_preceding_non_skip_insn = 0;
2920 if (there_is_preceding_non_skip_insn)
2922 /* We now only have to make sure that there is no
2923 local label defined at the address of the ret
2924 instruction and that there is no local relocation
2925 in this section pointing to the ret. */
2927 int deleting_ret_is_safe = 1;
2928 unsigned int section_offset_of_ret_insn =
2929 irel->r_offset + insn_size;
2930 Elf_Internal_Sym *isym, *isymend;
2931 unsigned int sec_shndx;
2932 struct bfd_section *isec;
2934 sec_shndx =
2935 _bfd_elf_section_from_bfd_section (abfd, sec);
2937 /* Check for local symbols. */
2938 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2939 isymend = isym + symtab_hdr->sh_info;
2940 /* PR 6019: There may not be any local symbols. */
2941 for (; isym != NULL && isym < isymend; isym++)
2943 if (isym->st_value == section_offset_of_ret_insn
2944 && isym->st_shndx == sec_shndx)
2946 deleting_ret_is_safe = 0;
2947 if (debug_relax)
2948 printf ("local label prevents deletion of ret "
2949 "insn at address 0x%x\n",
2950 (int) dot + insn_size);
2954 /* Now check for global symbols. */
2956 int symcount;
2957 struct elf_link_hash_entry **sym_hashes;
2958 struct elf_link_hash_entry **end_hashes;
2960 symcount = (symtab_hdr->sh_size
2961 / sizeof (Elf32_External_Sym)
2962 - symtab_hdr->sh_info);
2963 sym_hashes = elf_sym_hashes (abfd);
2964 end_hashes = sym_hashes + symcount;
2965 for (; sym_hashes < end_hashes; sym_hashes++)
2967 struct elf_link_hash_entry *sym_hash =
2968 *sym_hashes;
2969 if ((sym_hash->root.type == bfd_link_hash_defined
2970 || sym_hash->root.type ==
2971 bfd_link_hash_defweak)
2972 && sym_hash->root.u.def.section == sec
2973 && sym_hash->root.u.def.value == section_offset_of_ret_insn)
2975 deleting_ret_is_safe = 0;
2976 if (debug_relax)
2977 printf ("global label prevents deletion of "
2978 "ret insn at address 0x%x\n",
2979 (int) dot + insn_size);
2984 /* Now we check for relocations pointing to ret. */
2985 for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next)
2987 Elf_Internal_Rela *rel;
2988 Elf_Internal_Rela *relend;
2990 rel = elf_section_data (isec)->relocs;
2991 if (rel == NULL)
2992 rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, true);
2994 relend = rel + isec->reloc_count;
2996 for (; rel && rel < relend; rel++)
2998 bfd_vma reloc_target = 0;
3000 /* Read this BFD's local symbols if we haven't
3001 done so already. */
3002 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3004 isymbuf = (Elf_Internal_Sym *)
3005 symtab_hdr->contents;
3006 if (isymbuf == NULL)
3007 isymbuf = bfd_elf_get_elf_syms
3008 (abfd,
3009 symtab_hdr,
3010 symtab_hdr->sh_info, 0,
3011 NULL, NULL, NULL);
3012 if (isymbuf == NULL)
3013 break;
3016 /* Get the value of the symbol referred to
3017 by the reloc. */
3018 if (ELF32_R_SYM (rel->r_info)
3019 < symtab_hdr->sh_info)
3021 /* A local symbol. */
3022 asection *sym_sec;
3024 isym = isymbuf
3025 + ELF32_R_SYM (rel->r_info);
3026 sym_sec = bfd_section_from_elf_index
3027 (abfd, isym->st_shndx);
3028 symval = isym->st_value;
3030 /* If the reloc is absolute, it will not
3031 have a symbol or section associated
3032 with it. */
3034 if (sym_sec)
3036 symval +=
3037 sym_sec->output_section->vma
3038 + sym_sec->output_offset;
3039 reloc_target = symval + rel->r_addend;
3041 else
3043 reloc_target = symval + rel->r_addend;
3044 /* Reference symbol is absolute. */
3047 /* else ... reference symbol is extern. */
3049 if (address_of_ret == reloc_target)
3051 deleting_ret_is_safe = 0;
3052 if (debug_relax)
3053 printf ("ret from "
3054 "rjmp/jmp ret sequence at address"
3055 " 0x%x could not be deleted. ret"
3056 " is target of a relocation.\n",
3057 (int) address_of_ret);
3058 break;
3063 if (deleting_ret_is_safe)
3065 if (debug_relax)
3066 printf ("unreachable ret instruction "
3067 "at address 0x%x deleted.\n",
3068 (int) dot + insn_size);
3070 elf_section_data (sec)->relocs = internal_relocs;
3071 elf_section_data (sec)->this_hdr.contents = contents;
3072 symtab_hdr->contents = (unsigned char *) isymbuf;
3074 /* Delete two bytes of data. */
3075 if (!elf32_avr_relax_delete_bytes (abfd, sec,
3076 irel->r_offset + insn_size, 2,
3077 true))
3078 goto error_return;
3080 /* That will change things, so, we should relax
3081 again. Note that this is not required, and it
3082 may be slow. */
3083 *again = true;
3084 break;
3089 break;
3094 if (!*again)
3096 /* Look through all the property records in this section to see if
3097 there's any alignment records that can be moved. */
3098 struct avr_relax_info *relax_info;
3100 relax_info = get_avr_relax_info (sec);
3101 if (relax_info->records.count > 0)
3103 unsigned int i;
3105 for (i = 0; i < relax_info->records.count; ++i)
3107 switch (relax_info->records.items [i].type)
3109 case RECORD_ORG:
3110 case RECORD_ORG_AND_FILL:
3111 break;
3112 case RECORD_ALIGN:
3113 case RECORD_ALIGN_AND_FILL:
3115 struct avr_property_record *record;
3116 unsigned long bytes_to_align;
3117 int count = 0;
3119 /* Look for alignment directives that have had enough
3120 bytes deleted before them, such that the directive
3121 can be moved backwards and still maintain the
3122 required alignment. */
3123 record = &relax_info->records.items [i];
3124 bytes_to_align
3125 = (unsigned long) (1 << record->data.align.bytes);
3126 while (record->data.align.preceding_deleted >=
3127 bytes_to_align)
3129 record->data.align.preceding_deleted
3130 -= bytes_to_align;
3131 count += bytes_to_align;
3134 if (count > 0)
3136 bfd_vma addr = record->offset;
3138 /* We can delete COUNT bytes and this alignment
3139 directive will still be correctly aligned.
3140 First move the alignment directive, then delete
3141 the bytes. */
3142 record->offset -= count;
3143 elf32_avr_relax_delete_bytes (abfd, sec,
3144 addr - count,
3145 count, false);
3146 *again = true;
3149 break;
3155 if (contents != NULL
3156 && elf_section_data (sec)->this_hdr.contents != contents)
3158 if (! link_info->keep_memory)
3159 free (contents);
3160 else
3162 /* Cache the section contents for elf_link_input_bfd. */
3163 elf_section_data (sec)->this_hdr.contents = contents;
3167 if (elf_section_data (sec)->relocs != internal_relocs)
3168 free (internal_relocs);
3170 return true;
3172 error_return:
3173 if (symtab_hdr->contents != (unsigned char *) isymbuf)
3174 free (isymbuf);
3175 if (elf_section_data (sec)->this_hdr.contents != contents)
3176 free (contents);
3177 if (elf_section_data (sec)->relocs != internal_relocs)
3178 free (internal_relocs);
3180 return false;
3183 /* This is a version of bfd_generic_get_relocated_section_contents
3184 which uses elf32_avr_relocate_section.
3186 For avr it's essentially a cut and paste taken from the H8300 port.
3187 The author of the relaxation support patch for avr had absolutely no
3188 clue what is happening here but found out that this part of the code
3189 seems to be important. */
3191 static bfd_byte *
3192 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
3193 struct bfd_link_info *link_info,
3194 struct bfd_link_order *link_order,
3195 bfd_byte *data,
3196 bool relocatable,
3197 asymbol **symbols)
3199 Elf_Internal_Shdr *symtab_hdr;
3200 asection *input_section = link_order->u.indirect.section;
3201 bfd *input_bfd = input_section->owner;
3202 asection **sections = NULL;
3203 Elf_Internal_Rela *internal_relocs = NULL;
3204 Elf_Internal_Sym *isymbuf = NULL;
3206 /* We only need to handle the case of relaxing, or of having a
3207 particular set of section contents, specially. */
3208 if (relocatable
3209 || elf_section_data (input_section)->this_hdr.contents == NULL)
3210 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3211 link_order, data,
3212 relocatable,
3213 symbols);
3214 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3216 bfd_byte *orig_data = data;
3217 if (data == NULL)
3219 data = bfd_malloc (input_section->size);
3220 if (data == NULL)
3221 return NULL;
3223 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3224 (size_t) input_section->size);
3226 if ((input_section->flags & SEC_RELOC) != 0
3227 && input_section->reloc_count > 0)
3229 asection **secpp;
3230 Elf_Internal_Sym *isym, *isymend;
3231 bfd_size_type amt;
3233 internal_relocs = (_bfd_elf_link_read_relocs
3234 (input_bfd, input_section, NULL, NULL, false));
3235 if (internal_relocs == NULL)
3236 goto error_return;
3238 if (symtab_hdr->sh_info != 0)
3240 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3241 if (isymbuf == NULL)
3242 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3243 symtab_hdr->sh_info, 0,
3244 NULL, NULL, NULL);
3245 if (isymbuf == NULL)
3246 goto error_return;
3249 amt = symtab_hdr->sh_info;
3250 amt *= sizeof (asection *);
3251 sections = bfd_malloc (amt);
3252 if (sections == NULL && amt != 0)
3253 goto error_return;
3255 isymend = isymbuf + symtab_hdr->sh_info;
3256 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3258 asection *isec;
3260 if (isym->st_shndx == SHN_UNDEF)
3261 isec = bfd_und_section_ptr;
3262 else if (isym->st_shndx == SHN_ABS)
3263 isec = bfd_abs_section_ptr;
3264 else if (isym->st_shndx == SHN_COMMON)
3265 isec = bfd_com_section_ptr;
3266 else
3267 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3269 *secpp = isec;
3272 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
3273 input_section, data, internal_relocs,
3274 isymbuf, sections))
3275 goto error_return;
3277 free (sections);
3278 if (symtab_hdr->contents != (unsigned char *) isymbuf)
3279 free (isymbuf);
3280 if (elf_section_data (input_section)->relocs != internal_relocs)
3281 free (internal_relocs);
3284 return data;
3286 error_return:
3287 free (sections);
3288 if (symtab_hdr->contents != (unsigned char *) isymbuf)
3289 free (isymbuf);
3290 if (elf_section_data (input_section)->relocs != internal_relocs)
3291 free (internal_relocs);
3292 if (orig_data == NULL)
3293 free (data);
3294 return NULL;
3298 /* Determines the hash entry name for a particular reloc. It consists of
3299 the identifier of the symbol section and the added reloc addend and
3300 symbol offset relative to the section the symbol is attached to. */
3302 static char *
3303 avr_stub_name (const asection *symbol_section,
3304 const bfd_vma symbol_offset,
3305 const Elf_Internal_Rela *rela)
3307 char *stub_name;
3308 bfd_size_type len;
3310 len = 8 + 1 + 8 + 1 + 1;
3311 stub_name = bfd_malloc (len);
3312 if (stub_name != NULL)
3313 sprintf (stub_name, "%08x+%08x",
3314 symbol_section->id & 0xffffffff,
3315 (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
3317 return stub_name;
3321 /* Add a new stub entry to the stub hash. Not all fields of the new
3322 stub entry are initialised. */
3324 static struct elf32_avr_stub_hash_entry *
3325 avr_add_stub (const char *stub_name,
3326 struct elf32_avr_link_hash_table *htab)
3328 struct elf32_avr_stub_hash_entry *hsh;
3330 /* Enter this entry into the linker stub hash table. */
3331 hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, true, false);
3333 if (hsh == NULL)
3335 /* xgettext:c-format */
3336 _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
3337 return NULL;
3340 hsh->stub_offset = 0;
3341 return hsh;
3344 /* We assume that there is already space allocated for the stub section
3345 contents and that before building the stubs the section size is
3346 initialized to 0. We assume that within the stub hash table entry,
3347 the absolute position of the jmp target has been written in the
3348 target_value field. We write here the offset of the generated jmp insn
3349 relative to the trampoline section start to the stub_offset entry in
3350 the stub hash table entry. */
3352 static bool
3353 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
3355 struct elf32_avr_stub_hash_entry *hsh;
3356 struct bfd_link_info *info;
3357 struct elf32_avr_link_hash_table *htab;
3358 bfd *stub_bfd;
3359 bfd_byte *loc;
3360 bfd_vma target;
3361 bfd_vma starget;
3363 /* Basic opcode */
3364 bfd_vma jmp_insn = 0x0000940c;
3366 /* Massage our args to the form they really have. */
3367 hsh = avr_stub_hash_entry (bh);
3369 if (!hsh->is_actually_needed)
3370 return true;
3372 info = (struct bfd_link_info *) in_arg;
3374 htab = avr_link_hash_table (info);
3375 if (htab == NULL)
3376 return false;
3378 target = hsh->target_value;
3380 /* Make a note of the offset within the stubs for this entry. */
3381 hsh->stub_offset = htab->stub_sec->size;
3382 loc = htab->stub_sec->contents + hsh->stub_offset;
3384 stub_bfd = htab->stub_sec->owner;
3386 if (debug_stubs)
3387 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
3388 (unsigned int) target,
3389 (unsigned int) hsh->stub_offset);
3391 /* We now have to add the information on the jump target to the bare
3392 opcode bits already set in jmp_insn. */
3394 /* Check for the alignment of the address. */
3395 if (target & 1)
3396 return false;
3398 starget = target >> 1;
3399 jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
3400 bfd_put_16 (stub_bfd, jmp_insn, loc);
3401 bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
3403 htab->stub_sec->size += 4;
3405 /* Now add the entries in the address mapping table if there is still
3406 space left. */
3408 unsigned int nr;
3410 nr = htab->amt_entry_cnt + 1;
3411 if (nr <= htab->amt_max_entry_cnt)
3413 htab->amt_entry_cnt = nr;
3415 htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
3416 htab->amt_destination_addr[nr - 1] = target;
3420 return true;
3423 static bool
3424 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
3425 void *in_arg ATTRIBUTE_UNUSED)
3427 struct elf32_avr_stub_hash_entry *hsh;
3429 hsh = avr_stub_hash_entry (bh);
3430 hsh->is_actually_needed = false;
3432 return true;
3435 static bool
3436 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
3438 struct elf32_avr_stub_hash_entry *hsh;
3439 struct elf32_avr_link_hash_table *htab;
3440 int size;
3442 /* Massage our args to the form they really have. */
3443 hsh = avr_stub_hash_entry (bh);
3444 htab = in_arg;
3446 if (hsh->is_actually_needed)
3447 size = 4;
3448 else
3449 size = 0;
3451 htab->stub_sec->size += size;
3452 return true;
3455 void
3456 elf32_avr_setup_params (struct bfd_link_info *info,
3457 bfd *avr_stub_bfd,
3458 asection *avr_stub_section,
3459 bool no_stubs,
3460 bool deb_stubs,
3461 bool deb_relax,
3462 bfd_vma pc_wrap_around,
3463 bool call_ret_replacement)
3465 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3467 if (htab == NULL)
3468 return;
3469 htab->stub_sec = avr_stub_section;
3470 htab->stub_bfd = avr_stub_bfd;
3471 htab->no_stubs = no_stubs;
3473 debug_relax = deb_relax;
3474 debug_stubs = deb_stubs;
3475 avr_pc_wrap_around = pc_wrap_around;
3476 avr_replace_call_ret_sequences = call_ret_replacement;
3480 /* Set up various things so that we can make a list of input sections
3481 for each output section included in the link. Returns -1 on error,
3482 0 when no stubs will be needed, and 1 on success. It also sets
3483 information on the stubs bfd and the stub section in the info
3484 struct. */
3487 elf32_avr_setup_section_lists (bfd *output_bfd,
3488 struct bfd_link_info *info)
3490 bfd *input_bfd;
3491 unsigned int bfd_count;
3492 unsigned int top_id, top_index;
3493 asection *section;
3494 asection **input_list, **list;
3495 size_t amt;
3496 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3498 if (htab == NULL || htab->no_stubs)
3499 return 0;
3501 /* Count the number of input BFDs and find the top input section id. */
3502 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3503 input_bfd != NULL;
3504 input_bfd = input_bfd->link.next)
3506 bfd_count += 1;
3507 for (section = input_bfd->sections;
3508 section != NULL;
3509 section = section->next)
3510 if (top_id < section->id)
3511 top_id = section->id;
3514 htab->bfd_count = bfd_count;
3516 /* We can't use output_bfd->section_count here to find the top output
3517 section index as some sections may have been removed, and
3518 strip_excluded_output_sections doesn't renumber the indices. */
3519 for (section = output_bfd->sections, top_index = 0;
3520 section != NULL;
3521 section = section->next)
3522 if (top_index < section->index)
3523 top_index = section->index;
3525 htab->top_index = top_index;
3526 amt = sizeof (asection *) * (top_index + 1);
3527 input_list = bfd_malloc (amt);
3528 htab->input_list = input_list;
3529 if (input_list == NULL)
3530 return -1;
3532 /* For sections we aren't interested in, mark their entries with a
3533 value we can check later. */
3534 list = input_list + top_index;
3536 *list = bfd_abs_section_ptr;
3537 while (list-- != input_list);
3539 for (section = output_bfd->sections;
3540 section != NULL;
3541 section = section->next)
3542 if ((section->flags & SEC_CODE) != 0)
3543 input_list[section->index] = NULL;
3545 return 1;
3549 /* Read in all local syms for all input bfds, and create hash entries
3550 for export stubs if we are building a multi-subspace shared lib.
3551 Returns -1 on error, 0 otherwise. */
3553 static int
3554 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
3556 unsigned int bfd_indx;
3557 Elf_Internal_Sym *local_syms, **all_local_syms;
3558 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3559 size_t amt;
3561 if (htab == NULL)
3562 return -1;
3564 /* We want to read in symbol extension records only once. To do this
3565 we need to read in the local symbols in parallel and save them for
3566 later use; so hold pointers to the local symbols in an array. */
3567 amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3568 all_local_syms = bfd_zmalloc (amt);
3569 htab->all_local_syms = all_local_syms;
3570 if (all_local_syms == NULL)
3571 return -1;
3573 /* Walk over all the input BFDs, swapping in local symbols.
3574 If we are creating a shared library, create hash entries for the
3575 export stubs. */
3576 for (bfd_indx = 0;
3577 input_bfd != NULL;
3578 input_bfd = input_bfd->link.next, bfd_indx++)
3580 Elf_Internal_Shdr *symtab_hdr;
3582 /* We'll need the symbol table in a second. */
3583 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3584 if (symtab_hdr->sh_info == 0)
3585 continue;
3587 /* We need an array of the local symbols attached to the input bfd. */
3588 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3589 if (local_syms == NULL)
3591 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3592 symtab_hdr->sh_info, 0,
3593 NULL, NULL, NULL);
3594 /* Cache them for elf_link_input_bfd. */
3595 symtab_hdr->contents = (unsigned char *) local_syms;
3597 if (local_syms == NULL)
3598 return -1;
3600 all_local_syms[bfd_indx] = local_syms;
3603 return 0;
3606 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
3608 bool
3609 elf32_avr_size_stubs (bfd *output_bfd,
3610 struct bfd_link_info *info,
3611 bool is_prealloc_run)
3613 struct elf32_avr_link_hash_table *htab;
3614 int stub_changed = 0;
3616 htab = avr_link_hash_table (info);
3617 if (htab == NULL)
3618 return false;
3620 /* At this point we initialize htab->vector_base
3621 To the start of the text output section. */
3622 htab->vector_base = htab->stub_sec->output_section->vma;
3624 if (get_local_syms (info->input_bfds, info))
3626 if (htab->all_local_syms)
3627 goto error_ret_free_local;
3628 return false;
3631 if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
3633 struct elf32_avr_stub_hash_entry *test;
3635 test = avr_add_stub ("Hugo",htab);
3636 test->target_value = 0x123456;
3637 test->stub_offset = 13;
3639 test = avr_add_stub ("Hugo2",htab);
3640 test->target_value = 0x84210;
3641 test->stub_offset = 14;
3644 while (1)
3646 bfd *input_bfd;
3647 unsigned int bfd_indx;
3649 /* We will have to re-generate the stub hash table each time anything
3650 in memory has changed. */
3652 bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
3653 for (input_bfd = info->input_bfds, bfd_indx = 0;
3654 input_bfd != NULL;
3655 input_bfd = input_bfd->link.next, bfd_indx++)
3657 Elf_Internal_Shdr *symtab_hdr;
3658 asection *section;
3659 Elf_Internal_Sym *local_syms;
3661 /* We'll need the symbol table in a second. */
3662 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3663 if (symtab_hdr->sh_info == 0)
3664 continue;
3666 local_syms = htab->all_local_syms[bfd_indx];
3668 /* Walk over each section attached to the input bfd. */
3669 for (section = input_bfd->sections;
3670 section != NULL;
3671 section = section->next)
3673 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3675 /* If there aren't any relocs, then there's nothing more
3676 to do. */
3677 if ((section->flags & SEC_RELOC) == 0
3678 || section->reloc_count == 0)
3679 continue;
3681 /* If this section is a link-once section that will be
3682 discarded, then don't create any stubs. */
3683 if (section->output_section == NULL
3684 || section->output_section->owner != output_bfd)
3685 continue;
3687 /* Get the relocs. */
3688 internal_relocs
3689 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
3690 info->keep_memory);
3691 if (internal_relocs == NULL)
3692 goto error_ret_free_local;
3694 /* Now examine each relocation. */
3695 irela = internal_relocs;
3696 irelaend = irela + section->reloc_count;
3697 for (; irela < irelaend; irela++)
3699 unsigned int r_type, r_indx;
3700 struct elf32_avr_stub_hash_entry *hsh;
3701 asection *sym_sec;
3702 bfd_vma sym_value;
3703 bfd_vma destination;
3704 struct elf_link_hash_entry *hh;
3705 char *stub_name;
3707 r_type = ELF32_R_TYPE (irela->r_info);
3708 r_indx = ELF32_R_SYM (irela->r_info);
3710 /* Only look for 16 bit GS relocs. No other reloc will need a
3711 stub. */
3712 if (!((r_type == R_AVR_16_PM)
3713 || (r_type == R_AVR_LO8_LDI_GS)
3714 || (r_type == R_AVR_HI8_LDI_GS)))
3715 continue;
3717 /* Now determine the call target, its name, value,
3718 section. */
3719 sym_sec = NULL;
3720 sym_value = 0;
3721 destination = 0;
3722 hh = NULL;
3723 if (r_indx < symtab_hdr->sh_info)
3725 /* It's a local symbol. */
3726 Elf_Internal_Sym *sym;
3727 Elf_Internal_Shdr *hdr;
3728 unsigned int shndx;
3730 sym = local_syms + r_indx;
3731 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3732 sym_value = sym->st_value;
3733 shndx = sym->st_shndx;
3734 if (shndx < elf_numsections (input_bfd))
3736 hdr = elf_elfsections (input_bfd)[shndx];
3737 sym_sec = hdr->bfd_section;
3738 destination = (sym_value + irela->r_addend
3739 + sym_sec->output_offset
3740 + sym_sec->output_section->vma);
3743 else
3745 /* It's an external symbol. */
3746 int e_indx;
3748 e_indx = r_indx - symtab_hdr->sh_info;
3749 hh = elf_sym_hashes (input_bfd)[e_indx];
3751 while (hh->root.type == bfd_link_hash_indirect
3752 || hh->root.type == bfd_link_hash_warning)
3753 hh = (struct elf_link_hash_entry *)
3754 (hh->root.u.i.link);
3756 if (hh->root.type == bfd_link_hash_defined
3757 || hh->root.type == bfd_link_hash_defweak)
3759 sym_sec = hh->root.u.def.section;
3760 sym_value = hh->root.u.def.value;
3761 if (sym_sec->output_section != NULL)
3762 destination = (sym_value + irela->r_addend
3763 + sym_sec->output_offset
3764 + sym_sec->output_section->vma);
3766 else if (hh->root.type == bfd_link_hash_undefweak)
3768 if (! bfd_link_pic (info))
3769 continue;
3771 else if (hh->root.type == bfd_link_hash_undefined)
3773 if (! (info->unresolved_syms_in_objects == RM_IGNORE
3774 && (ELF_ST_VISIBILITY (hh->other)
3775 == STV_DEFAULT)))
3776 continue;
3778 else
3780 bfd_set_error (bfd_error_bad_value);
3782 error_ret_free_internal:
3783 if (elf_section_data (section)->relocs == NULL)
3784 free (internal_relocs);
3785 goto error_ret_free_local;
3789 if (! avr_stub_is_required_for_16_bit_reloc
3790 (destination - htab->vector_base))
3792 if (!is_prealloc_run)
3793 /* We are having a reloc that does't need a stub. */
3794 continue;
3796 /* We don't right now know if a stub will be needed.
3797 Let's rather be on the safe side. */
3800 /* Get the name of this stub. */
3801 stub_name = avr_stub_name (sym_sec, sym_value, irela);
3803 if (!stub_name)
3804 goto error_ret_free_internal;
3807 hsh = avr_stub_hash_lookup (&htab->bstab,
3808 stub_name,
3809 false, false);
3810 if (hsh != NULL)
3812 /* The proper stub has already been created. Mark it
3813 to be used and write the possibly changed destination
3814 value. */
3815 hsh->is_actually_needed = true;
3816 hsh->target_value = destination;
3817 free (stub_name);
3818 continue;
3821 hsh = avr_add_stub (stub_name, htab);
3822 if (hsh == NULL)
3824 free (stub_name);
3825 goto error_ret_free_internal;
3828 hsh->is_actually_needed = true;
3829 hsh->target_value = destination;
3831 if (debug_stubs)
3832 printf ("Adding stub with destination 0x%x to the"
3833 " hash table.\n", (unsigned int) destination);
3834 if (debug_stubs)
3835 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
3837 stub_changed = true;
3840 /* We're done with the internal relocs, free them. */
3841 if (elf_section_data (section)->relocs == NULL)
3842 free (internal_relocs);
3846 /* Re-Calculate the number of needed stubs. */
3847 htab->stub_sec->size = 0;
3848 bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
3850 if (!stub_changed)
3851 break;
3853 stub_changed = false;
3856 free (htab->all_local_syms);
3857 return true;
3859 error_ret_free_local:
3860 free (htab->all_local_syms);
3861 return false;
3865 /* Build all the stubs associated with the current output file. The
3866 stubs are kept in a hash table attached to the main linker hash
3867 table. We also set up the .plt entries for statically linked PIC
3868 functions here. This function is called via hppaelf_finish in the
3869 linker. */
3871 bool
3872 elf32_avr_build_stubs (struct bfd_link_info *info)
3874 asection *stub_sec;
3875 struct bfd_hash_table *table;
3876 struct elf32_avr_link_hash_table *htab;
3877 bfd_size_type total_size = 0;
3879 htab = avr_link_hash_table (info);
3880 if (htab == NULL)
3881 return false;
3883 /* In case that there were several stub sections: */
3884 for (stub_sec = htab->stub_bfd->sections;
3885 stub_sec != NULL;
3886 stub_sec = stub_sec->next)
3888 bfd_size_type size;
3890 /* Allocate memory to hold the linker stubs. */
3891 size = stub_sec->size;
3892 total_size += size;
3894 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3895 if (stub_sec->contents == NULL && size != 0)
3896 return false;
3897 stub_sec->size = 0;
3900 /* Allocate memory for the adress mapping table. */
3901 htab->amt_entry_cnt = 0;
3902 htab->amt_max_entry_cnt = total_size / 4;
3903 htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
3904 * htab->amt_max_entry_cnt);
3905 htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
3906 * htab->amt_max_entry_cnt );
3908 if (debug_stubs)
3909 printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
3911 /* Build the stubs as directed by the stub hash table. */
3912 table = &htab->bstab;
3913 bfd_hash_traverse (table, avr_build_one_stub, info);
3915 if (debug_stubs)
3916 printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
3918 return true;
3921 /* Callback used by QSORT to order relocations AP and BP. */
3923 static int
3924 internal_reloc_compare (const void *ap, const void *bp)
3926 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
3927 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
3929 if (a->r_offset != b->r_offset)
3930 return (a->r_offset - b->r_offset);
3932 /* We don't need to sort on these criteria for correctness,
3933 but enforcing a more strict ordering prevents unstable qsort
3934 from behaving differently with different implementations.
3935 Without the code below we get correct but different results
3936 on Solaris 2.7 and 2.8. We would like to always produce the
3937 same results no matter the host. */
3939 if (a->r_info != b->r_info)
3940 return (a->r_info - b->r_info);
3942 return (a->r_addend - b->r_addend);
3945 /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */
3947 static bool
3948 avr_is_section_for_address (asection *section, bfd_vma address)
3950 bfd_vma vma;
3951 bfd_size_type size;
3953 vma = bfd_section_vma (section);
3954 if (address < vma)
3955 return false;
3957 size = section->size;
3958 if (address >= vma + size)
3959 return false;
3961 return true;
3964 /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */
3966 struct avr_find_section_data
3968 /* The address we're looking for. */
3969 bfd_vma address;
3971 /* The section we've found. */
3972 asection *section;
3975 /* Helper function to locate the section holding a certain virtual memory
3976 address. This is called via bfd_map_over_sections. The DATA is an
3977 instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which
3978 has been set to the address to search for, and the section field has
3979 been set to NULL. If SECTION from ABFD contains ADDRESS then the
3980 section field in DATA will be set to SECTION. As an optimisation, if
3981 the section field is already non-null then this function does not
3982 perform any checks, and just returns. */
3984 static void
3985 avr_find_section_for_address (bfd *abfd ATTRIBUTE_UNUSED,
3986 asection *section, void *data)
3988 struct avr_find_section_data *fs_data
3989 = (struct avr_find_section_data *) data;
3991 /* Return if already found. */
3992 if (fs_data->section != NULL)
3993 return;
3995 /* If this section isn't part of the addressable code content, skip it. */
3996 if ((bfd_section_flags (section) & SEC_ALLOC) == 0
3997 && (bfd_section_flags (section) & SEC_CODE) == 0)
3998 return;
4000 if (avr_is_section_for_address (section, fs_data->address))
4001 fs_data->section = section;
4004 /* Load all of the property records from SEC, a section from ABFD. Return
4005 a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The
4006 memory for the returned structure, and all of the records pointed too by
4007 the structure are allocated with a single call to malloc, so, only the
4008 pointer returned needs to be free'd. */
4010 static struct avr_property_record_list *
4011 avr_elf32_load_records_from_section (bfd *abfd, asection *sec)
4013 bfd_byte *contents, *ptr;
4014 bfd_size_type size, mem_size;
4015 bfd_byte version, flags;
4016 uint16_t record_count, i;
4017 struct avr_property_record_list *r_list = NULL;
4018 Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end;
4019 struct avr_find_section_data fs_data;
4021 fs_data.section = NULL;
4023 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
4024 goto load_failed;
4025 ptr = contents;
4027 /* Load the relocations for the '.avr.prop' section if there are any, and
4028 sort them. */
4029 internal_relocs = (_bfd_elf_link_read_relocs
4030 (abfd, sec, NULL, NULL, false));
4031 if (internal_relocs)
4032 qsort (internal_relocs, sec->reloc_count,
4033 sizeof (Elf_Internal_Rela), internal_reloc_compare);
4035 /* There is a header at the start of the property record section SEC, the
4036 format of this header is:
4037 uint8_t : version number
4038 uint8_t : flags
4039 uint16_t : record counter
4042 /* Check we have at least got a headers worth of bytes. */
4043 size = bfd_section_size (sec);
4044 if (size < AVR_PROPERTY_SECTION_HEADER_SIZE)
4045 goto load_failed;
4047 version = *ptr;
4048 ptr++;
4049 flags = *ptr;
4050 ptr++;
4051 record_count = bfd_get_16 (abfd, ptr);
4052 ptr += 2;
4053 BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE);
4055 /* Now allocate space for the list structure, and all of the list
4056 elements in a single block. */
4057 mem_size = sizeof (struct avr_property_record_list)
4058 + sizeof (struct avr_property_record) * record_count;
4059 r_list = bfd_malloc (mem_size);
4060 if (r_list == NULL)
4061 goto load_failed;
4063 r_list->version = version;
4064 r_list->flags = flags;
4065 r_list->section = sec;
4066 r_list->record_count = record_count;
4067 r_list->records = (struct avr_property_record *) (&r_list [1]);
4068 size -= AVR_PROPERTY_SECTION_HEADER_SIZE;
4070 /* Check that we understand the version number. There is only one
4071 version number right now, anything else is an error. */
4072 if (r_list->version != AVR_PROPERTY_RECORDS_VERSION)
4073 goto load_failed;
4075 rel = internal_relocs;
4076 rel_end = rel + sec->reloc_count;
4077 for (i = 0; i < record_count; ++i)
4079 bfd_vma address;
4081 /* Each entry is a 32-bit address, followed by a single byte type.
4082 After that is the type specific data. We must take care to
4083 ensure that we don't read beyond the end of the section data. */
4084 if (size < 5)
4085 goto load_failed;
4087 r_list->records [i].section = NULL;
4088 r_list->records [i].offset = 0;
4090 if (rel)
4092 /* The offset of the address within the .avr.prop section. */
4093 size_t offset = ptr - contents;
4095 while (rel < rel_end && rel->r_offset < offset)
4096 ++rel;
4098 if (rel == rel_end)
4099 rel = NULL;
4100 else if (rel->r_offset == offset)
4102 /* Find section and section offset. */
4103 unsigned long r_symndx;
4105 asection * rel_sec;
4106 bfd_vma sec_offset;
4108 r_symndx = ELF32_R_SYM (rel->r_info);
4109 rel_sec = get_elf_r_symndx_section (abfd, r_symndx);
4110 sec_offset = get_elf_r_symndx_offset (abfd, r_symndx)
4111 + rel->r_addend;
4113 r_list->records [i].section = rel_sec;
4114 r_list->records [i].offset = sec_offset;
4118 address = bfd_get_32 (abfd, ptr);
4119 ptr += 4;
4120 size -= 4;
4122 if (r_list->records [i].section == NULL)
4124 /* Try to find section and offset from address. */
4125 if (fs_data.section != NULL
4126 && !avr_is_section_for_address (fs_data.section, address))
4127 fs_data.section = NULL;
4129 if (fs_data.section == NULL)
4131 fs_data.address = address;
4132 bfd_map_over_sections (abfd, avr_find_section_for_address,
4133 &fs_data);
4136 if (fs_data.section == NULL)
4138 fprintf (stderr, "Failed to find matching section.\n");
4139 goto load_failed;
4142 r_list->records [i].section = fs_data.section;
4143 r_list->records [i].offset
4144 = address - bfd_section_vma (fs_data.section);
4147 r_list->records [i].type = *ptr;
4148 ptr += 1;
4149 size -= 1;
4151 switch (r_list->records [i].type)
4153 case RECORD_ORG:
4154 /* Nothing else to load. */
4155 break;
4156 case RECORD_ORG_AND_FILL:
4157 /* Just a 4-byte fill to load. */
4158 if (size < 4)
4159 goto load_failed;
4160 r_list->records [i].data.org.fill = bfd_get_32 (abfd, ptr);
4161 ptr += 4;
4162 size -= 4;
4163 break;
4164 case RECORD_ALIGN:
4165 /* Just a 4-byte alignment to load. */
4166 if (size < 4)
4167 goto load_failed;
4168 r_list->records [i].data.align.bytes = bfd_get_32 (abfd, ptr);
4169 ptr += 4;
4170 size -= 4;
4171 /* Just initialise PRECEDING_DELETED field, this field is
4172 used during linker relaxation. */
4173 r_list->records [i].data.align.preceding_deleted = 0;
4174 break;
4175 case RECORD_ALIGN_AND_FILL:
4176 /* A 4-byte alignment, and a 4-byte fill to load. */
4177 if (size < 8)
4178 goto load_failed;
4179 r_list->records [i].data.align.bytes = bfd_get_32 (abfd, ptr);
4180 ptr += 4;
4181 r_list->records [i].data.align.fill = bfd_get_32 (abfd, ptr);
4182 ptr += 4;
4183 size -= 8;
4184 /* Just initialise PRECEDING_DELETED field, this field is
4185 used during linker relaxation. */
4186 r_list->records [i].data.align.preceding_deleted = 0;
4187 break;
4188 default:
4189 goto load_failed;
4193 free (contents);
4194 if (elf_section_data (sec)->relocs != internal_relocs)
4195 free (internal_relocs);
4196 return r_list;
4198 load_failed:
4199 if (elf_section_data (sec)->relocs != internal_relocs)
4200 free (internal_relocs);
4201 free (contents);
4202 free (r_list);
4203 return NULL;
4206 /* Load all of the property records from ABFD. See
4207 AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */
4209 struct avr_property_record_list *
4210 avr_elf32_load_property_records (bfd *abfd)
4212 asection *sec;
4214 /* Find the '.avr.prop' section and load the contents into memory. */
4215 sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME);
4216 if (sec == NULL || (sec->flags & SEC_HAS_CONTENTS) == 0)
4217 return NULL;
4218 return avr_elf32_load_records_from_section (abfd, sec);
4221 const char *
4222 avr_elf32_property_record_name (struct avr_property_record *rec)
4224 const char *str;
4226 switch (rec->type)
4228 case RECORD_ORG:
4229 str = "ORG";
4230 break;
4231 case RECORD_ORG_AND_FILL:
4232 str = "ORG+FILL";
4233 break;
4234 case RECORD_ALIGN:
4235 str = "ALIGN";
4236 break;
4237 case RECORD_ALIGN_AND_FILL:
4238 str = "ALIGN+FILL";
4239 break;
4240 default:
4241 str = "unknown";
4244 return str;
4248 #define ELF_ARCH bfd_arch_avr
4249 #define ELF_TARGET_ID AVR_ELF_DATA
4250 #define ELF_MACHINE_CODE EM_AVR
4251 #define ELF_MACHINE_ALT1 EM_AVR_OLD
4252 #define ELF_MAXPAGESIZE 1
4254 #define TARGET_LITTLE_SYM avr_elf32_vec
4255 #define TARGET_LITTLE_NAME "elf32-avr"
4257 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
4259 #define elf_info_to_howto avr_info_to_howto_rela
4260 #define elf_info_to_howto_rel NULL
4261 #define elf_backend_relocate_section elf32_avr_relocate_section
4262 #define elf_backend_can_gc_sections 1
4263 #define elf_backend_rela_normal 1
4264 #define elf_backend_final_write_processing \
4265 bfd_elf_avr_final_write_processing
4266 #define elf_backend_object_p elf32_avr_object_p
4268 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
4269 #define bfd_elf32_bfd_get_relocated_section_contents \
4270 elf32_avr_get_relocated_section_contents
4271 #define bfd_elf32_new_section_hook elf_avr_new_section_hook
4272 #define elf_backend_special_sections elf_avr_special_sections
4274 #include "elf32-target.h"