avr/objdump: Support dumping .avr.prop section.
[external/binutils.git] / bfd / elf32-avr.c
1 /* AVR-specific support for 32-bit ELF
2    Copyright (C) 1999-2015 Free Software Foundation, Inc.
3    Contributed by Denis Chertykov <denisc@overta.ru>
4
5    This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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.  */
21
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"
28 #include "bfd_stdint.h"
29
30 /* Enable debugging printout at stdout with this variable.  */
31 static bfd_boolean debug_relax = FALSE;
32
33 /* Enable debugging printout at stdout with this variable.  */
34 static bfd_boolean debug_stubs = FALSE;
35
36 static bfd_reloc_status_type
37 bfd_elf_avr_diff_reloc (bfd *, arelent *, asymbol *, void *,
38                         asection *, bfd *, char **);
39
40 /* Hash table initialization and handling.  Code is taken from the hppa port
41    and adapted to the needs of AVR.  */
42
43 /* We use two hash tables to hold information for linking avr objects.
44
45    The first is the elf32_avr_link_hash_table which is derived from the
46    stanard ELF linker hash table.  We use this as a place to attach the other
47    hash table and some static information.
48
49    The second is the stub hash table which is derived from the base BFD
50    hash table.  The stub hash table holds the information on the linker
51    stubs.  */
52
53 struct elf32_avr_stub_hash_entry
54 {
55   /* Base hash table entry structure.  */
56   struct bfd_hash_entry bh_root;
57
58   /* Offset within stub_sec of the beginning of this stub.  */
59   bfd_vma stub_offset;
60
61   /* Given the symbol's value and its section we can determine its final
62      value when building the stubs (so the stub knows where to jump).  */
63   bfd_vma target_value;
64
65   /* This way we could mark stubs to be no longer necessary.  */
66   bfd_boolean is_actually_needed;
67 };
68
69 struct elf32_avr_link_hash_table
70 {
71   /* The main hash table.  */
72   struct elf_link_hash_table etab;
73
74   /* The stub hash table.  */
75   struct bfd_hash_table bstab;
76
77   bfd_boolean no_stubs;
78
79   /* Linker stub bfd.  */
80   bfd *stub_bfd;
81
82   /* The stub section.  */
83   asection *stub_sec;
84
85   /* Usually 0, unless we are generating code for a bootloader.  Will
86      be initialized by elf32_avr_size_stubs to the vma offset of the
87      output section associated with the stub section.  */
88   bfd_vma vector_base;
89
90   /* Assorted information used by elf32_avr_size_stubs.  */
91   unsigned int        bfd_count;
92   int                 top_index;
93   asection **         input_list;
94   Elf_Internal_Sym ** all_local_syms;
95
96   /* Tables for mapping vma beyond the 128k boundary to the address of the
97      corresponding stub.  (AMT)
98      "amt_max_entry_cnt" reflects the number of entries that memory is allocated
99      for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
100      "amt_entry_cnt" informs how many of these entries actually contain
101      useful data.  */
102   unsigned int amt_entry_cnt;
103   unsigned int amt_max_entry_cnt;
104   bfd_vma *    amt_stub_offsets;
105   bfd_vma *    amt_destination_addr;
106 };
107
108 /* Various hash macros and functions.  */
109 #define avr_link_hash_table(p) \
110   /* PR 3874: Check that we have an AVR style hash table before using it.  */\
111   (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
112   == AVR_ELF_DATA ? ((struct elf32_avr_link_hash_table *) ((p)->hash)) : NULL)
113
114 #define avr_stub_hash_entry(ent) \
115   ((struct elf32_avr_stub_hash_entry *)(ent))
116
117 #define avr_stub_hash_lookup(table, string, create, copy) \
118   ((struct elf32_avr_stub_hash_entry *) \
119    bfd_hash_lookup ((table), (string), (create), (copy)))
120
121 static reloc_howto_type elf_avr_howto_table[] =
122 {
123   HOWTO (R_AVR_NONE,            /* type */
124          0,                     /* rightshift */
125          3,                     /* size (0 = byte, 1 = short, 2 = long) */
126          0,                     /* bitsize */
127          FALSE,                 /* pc_relative */
128          0,                     /* bitpos */
129          complain_overflow_dont, /* complain_on_overflow */
130          bfd_elf_generic_reloc, /* special_function */
131          "R_AVR_NONE",          /* name */
132          FALSE,                 /* partial_inplace */
133          0,                     /* src_mask */
134          0,                     /* dst_mask */
135          FALSE),                /* pcrel_offset */
136
137   HOWTO (R_AVR_32,              /* type */
138          0,                     /* rightshift */
139          2,                     /* size (0 = byte, 1 = short, 2 = long) */
140          32,                    /* bitsize */
141          FALSE,                 /* pc_relative */
142          0,                     /* bitpos */
143          complain_overflow_bitfield, /* complain_on_overflow */
144          bfd_elf_generic_reloc, /* special_function */
145          "R_AVR_32",            /* name */
146          FALSE,                 /* partial_inplace */
147          0xffffffff,            /* src_mask */
148          0xffffffff,            /* dst_mask */
149          FALSE),                /* pcrel_offset */
150
151   /* A 7 bit PC relative relocation.  */
152   HOWTO (R_AVR_7_PCREL,         /* type */
153          1,                     /* rightshift */
154          1,                     /* size (0 = byte, 1 = short, 2 = long) */
155          7,                     /* bitsize */
156          TRUE,                  /* pc_relative */
157          3,                     /* bitpos */
158          complain_overflow_bitfield, /* complain_on_overflow */
159          bfd_elf_generic_reloc, /* special_function */
160          "R_AVR_7_PCREL",       /* name */
161          FALSE,                 /* partial_inplace */
162          0xffff,                /* src_mask */
163          0xffff,                /* dst_mask */
164          TRUE),                 /* pcrel_offset */
165
166   /* A 13 bit PC relative relocation.  */
167   HOWTO (R_AVR_13_PCREL,        /* type */
168          1,                     /* rightshift */
169          1,                     /* size (0 = byte, 1 = short, 2 = long) */
170          13,                    /* bitsize */
171          TRUE,                  /* pc_relative */
172          0,                     /* bitpos */
173          complain_overflow_bitfield, /* complain_on_overflow */
174          bfd_elf_generic_reloc, /* special_function */
175          "R_AVR_13_PCREL",      /* name */
176          FALSE,                 /* partial_inplace */
177          0xfff,                 /* src_mask */
178          0xfff,                 /* dst_mask */
179          TRUE),                 /* pcrel_offset */
180
181   /* A 16 bit absolute relocation.  */
182   HOWTO (R_AVR_16,              /* type */
183          0,                     /* rightshift */
184          1,                     /* size (0 = byte, 1 = short, 2 = long) */
185          16,                    /* bitsize */
186          FALSE,                 /* pc_relative */
187          0,                     /* bitpos */
188          complain_overflow_dont, /* complain_on_overflow */
189          bfd_elf_generic_reloc, /* special_function */
190          "R_AVR_16",            /* name */
191          FALSE,                 /* partial_inplace */
192          0xffff,                /* src_mask */
193          0xffff,                /* dst_mask */
194          FALSE),                /* pcrel_offset */
195
196   /* A 16 bit absolute relocation for command address
197      Will be changed when linker stubs are needed.  */
198   HOWTO (R_AVR_16_PM,           /* type */
199          1,                     /* rightshift */
200          1,                     /* size (0 = byte, 1 = short, 2 = long) */
201          16,                    /* bitsize */
202          FALSE,                 /* pc_relative */
203          0,                     /* bitpos */
204          complain_overflow_bitfield, /* complain_on_overflow */
205          bfd_elf_generic_reloc, /* special_function */
206          "R_AVR_16_PM",         /* name */
207          FALSE,                 /* partial_inplace */
208          0xffff,                /* src_mask */
209          0xffff,                /* dst_mask */
210          FALSE),                /* pcrel_offset */
211   /* A low 8 bit absolute relocation of 16 bit address.
212      For LDI command.  */
213   HOWTO (R_AVR_LO8_LDI,         /* type */
214          0,                     /* rightshift */
215          1,                     /* size (0 = byte, 1 = short, 2 = long) */
216          8,                     /* bitsize */
217          FALSE,                 /* pc_relative */
218          0,                     /* bitpos */
219          complain_overflow_dont, /* complain_on_overflow */
220          bfd_elf_generic_reloc, /* special_function */
221          "R_AVR_LO8_LDI",       /* name */
222          FALSE,                 /* partial_inplace */
223          0xffff,                /* src_mask */
224          0xffff,                /* dst_mask */
225          FALSE),                /* pcrel_offset */
226   /* A high 8 bit absolute relocation of 16 bit address.
227      For LDI command.  */
228   HOWTO (R_AVR_HI8_LDI,         /* type */
229          8,                     /* rightshift */
230          1,                     /* size (0 = byte, 1 = short, 2 = long) */
231          8,                     /* bitsize */
232          FALSE,                 /* pc_relative */
233          0,                     /* bitpos */
234          complain_overflow_dont, /* complain_on_overflow */
235          bfd_elf_generic_reloc, /* special_function */
236          "R_AVR_HI8_LDI",       /* name */
237          FALSE,                 /* partial_inplace */
238          0xffff,                /* src_mask */
239          0xffff,                /* dst_mask */
240          FALSE),                /* pcrel_offset */
241   /* A high 6 bit absolute relocation of 22 bit address.
242      For LDI command.  As well second most significant 8 bit value of
243      a 32 bit link-time constant.  */
244   HOWTO (R_AVR_HH8_LDI,         /* type */
245          16,                    /* rightshift */
246          1,                     /* size (0 = byte, 1 = short, 2 = long) */
247          8,                     /* bitsize */
248          FALSE,                 /* pc_relative */
249          0,                     /* bitpos */
250          complain_overflow_dont, /* complain_on_overflow */
251          bfd_elf_generic_reloc, /* special_function */
252          "R_AVR_HH8_LDI",       /* name */
253          FALSE,                 /* partial_inplace */
254          0xffff,                /* src_mask */
255          0xffff,                /* dst_mask */
256          FALSE),                /* pcrel_offset */
257   /* A negative low 8 bit absolute relocation of 16 bit address.
258      For LDI command.  */
259   HOWTO (R_AVR_LO8_LDI_NEG,     /* type */
260          0,                     /* rightshift */
261          1,                     /* size (0 = byte, 1 = short, 2 = long) */
262          8,                     /* bitsize */
263          FALSE,                 /* pc_relative */
264          0,                     /* bitpos */
265          complain_overflow_dont, /* complain_on_overflow */
266          bfd_elf_generic_reloc, /* special_function */
267          "R_AVR_LO8_LDI_NEG",   /* name */
268          FALSE,                 /* partial_inplace */
269          0xffff,                /* src_mask */
270          0xffff,                /* dst_mask */
271          FALSE),                /* pcrel_offset */
272   /* A negative high 8 bit absolute relocation of 16 bit address.
273      For LDI command.  */
274   HOWTO (R_AVR_HI8_LDI_NEG,     /* type */
275          8,                     /* rightshift */
276          1,                     /* size (0 = byte, 1 = short, 2 = long) */
277          8,                     /* bitsize */
278          FALSE,                 /* pc_relative */
279          0,                     /* bitpos */
280          complain_overflow_dont, /* complain_on_overflow */
281          bfd_elf_generic_reloc, /* special_function */
282          "R_AVR_HI8_LDI_NEG",   /* name */
283          FALSE,                 /* partial_inplace */
284          0xffff,                /* src_mask */
285          0xffff,                /* dst_mask */
286          FALSE),                /* pcrel_offset */
287   /* A negative high 6 bit absolute relocation of 22 bit address.
288      For LDI command.  */
289   HOWTO (R_AVR_HH8_LDI_NEG,     /* type */
290          16,                    /* rightshift */
291          1,                     /* size (0 = byte, 1 = short, 2 = long) */
292          8,                     /* bitsize */
293          FALSE,                 /* pc_relative */
294          0,                     /* bitpos */
295          complain_overflow_dont, /* complain_on_overflow */
296          bfd_elf_generic_reloc, /* special_function */
297          "R_AVR_HH8_LDI_NEG",   /* name */
298          FALSE,                 /* partial_inplace */
299          0xffff,                /* src_mask */
300          0xffff,                /* dst_mask */
301          FALSE),                /* pcrel_offset */
302   /* A low 8 bit absolute relocation of 24 bit program memory address.
303      For LDI command.  Will not be changed when linker stubs are needed. */
304   HOWTO (R_AVR_LO8_LDI_PM,      /* type */
305          1,                     /* rightshift */
306          1,                     /* size (0 = byte, 1 = short, 2 = long) */
307          8,                     /* bitsize */
308          FALSE,                 /* pc_relative */
309          0,                     /* bitpos */
310          complain_overflow_dont, /* complain_on_overflow */
311          bfd_elf_generic_reloc, /* special_function */
312          "R_AVR_LO8_LDI_PM",    /* name */
313          FALSE,                 /* partial_inplace */
314          0xffff,                /* src_mask */
315          0xffff,                /* dst_mask */
316          FALSE),                /* pcrel_offset */
317   /* A low 8 bit absolute relocation of 24 bit program memory address.
318      For LDI command.  Will not be changed when linker stubs are needed. */
319   HOWTO (R_AVR_HI8_LDI_PM,      /* type */
320          9,                     /* rightshift */
321          1,                     /* size (0 = byte, 1 = short, 2 = long) */
322          8,                     /* bitsize */
323          FALSE,                 /* pc_relative */
324          0,                     /* bitpos */
325          complain_overflow_dont, /* complain_on_overflow */
326          bfd_elf_generic_reloc, /* special_function */
327          "R_AVR_HI8_LDI_PM",    /* name */
328          FALSE,                 /* partial_inplace */
329          0xffff,                /* src_mask */
330          0xffff,                /* dst_mask */
331          FALSE),                /* pcrel_offset */
332   /* A low 8 bit absolute relocation of 24 bit program memory address.
333      For LDI command.  Will not be changed when linker stubs are needed. */
334   HOWTO (R_AVR_HH8_LDI_PM,      /* type */
335          17,                    /* rightshift */
336          1,                     /* size (0 = byte, 1 = short, 2 = long) */
337          8,                     /* bitsize */
338          FALSE,                 /* pc_relative */
339          0,                     /* bitpos */
340          complain_overflow_dont, /* complain_on_overflow */
341          bfd_elf_generic_reloc, /* special_function */
342          "R_AVR_HH8_LDI_PM",    /* name */
343          FALSE,                 /* partial_inplace */
344          0xffff,                /* src_mask */
345          0xffff,                /* dst_mask */
346          FALSE),                /* pcrel_offset */
347   /* A low 8 bit absolute relocation of 24 bit program memory address.
348      For LDI command.  Will not be changed when linker stubs are needed. */
349   HOWTO (R_AVR_LO8_LDI_PM_NEG,  /* type */
350          1,                     /* rightshift */
351          1,                     /* size (0 = byte, 1 = short, 2 = long) */
352          8,                     /* bitsize */
353          FALSE,                 /* pc_relative */
354          0,                     /* bitpos */
355          complain_overflow_dont, /* complain_on_overflow */
356          bfd_elf_generic_reloc, /* special_function */
357          "R_AVR_LO8_LDI_PM_NEG", /* name */
358          FALSE,                 /* partial_inplace */
359          0xffff,                /* src_mask */
360          0xffff,                /* dst_mask */
361          FALSE),                /* pcrel_offset */
362   /* A low 8 bit absolute relocation of 24 bit program memory address.
363      For LDI command.  Will not be changed when linker stubs are needed. */
364   HOWTO (R_AVR_HI8_LDI_PM_NEG,  /* type */
365          9,                     /* rightshift */
366          1,                     /* size (0 = byte, 1 = short, 2 = long) */
367          8,                     /* bitsize */
368          FALSE,                 /* pc_relative */
369          0,                     /* bitpos */
370          complain_overflow_dont, /* complain_on_overflow */
371          bfd_elf_generic_reloc, /* special_function */
372          "R_AVR_HI8_LDI_PM_NEG", /* name */
373          FALSE,                 /* partial_inplace */
374          0xffff,                /* src_mask */
375          0xffff,                /* dst_mask */
376          FALSE),                /* pcrel_offset */
377   /* A low 8 bit absolute relocation of 24 bit program memory address.
378      For LDI command.  Will not be changed when linker stubs are needed. */
379   HOWTO (R_AVR_HH8_LDI_PM_NEG,  /* type */
380          17,                    /* rightshift */
381          1,                     /* size (0 = byte, 1 = short, 2 = long) */
382          8,                     /* bitsize */
383          FALSE,                 /* pc_relative */
384          0,                     /* bitpos */
385          complain_overflow_dont, /* complain_on_overflow */
386          bfd_elf_generic_reloc, /* special_function */
387          "R_AVR_HH8_LDI_PM_NEG", /* name */
388          FALSE,                 /* partial_inplace */
389          0xffff,                /* src_mask */
390          0xffff,                /* dst_mask */
391          FALSE),                /* pcrel_offset */
392   /* Relocation for CALL command in ATmega.  */
393   HOWTO (R_AVR_CALL,            /* type */
394          1,                     /* rightshift */
395          2,                     /* size (0 = byte, 1 = short, 2 = long) */
396          23,                    /* bitsize */
397          FALSE,                 /* pc_relative */
398          0,                     /* bitpos */
399          complain_overflow_dont,/* complain_on_overflow */
400          bfd_elf_generic_reloc, /* special_function */
401          "R_AVR_CALL",          /* name */
402          FALSE,                 /* partial_inplace */
403          0xffffffff,            /* src_mask */
404          0xffffffff,            /* dst_mask */
405          FALSE),                        /* pcrel_offset */
406   /* A 16 bit absolute relocation of 16 bit address.
407      For LDI command.  */
408   HOWTO (R_AVR_LDI,             /* type */
409          0,                     /* rightshift */
410          1,                     /* size (0 = byte, 1 = short, 2 = long) */
411          16,                    /* bitsize */
412          FALSE,                 /* pc_relative */
413          0,                     /* bitpos */
414          complain_overflow_dont,/* complain_on_overflow */
415          bfd_elf_generic_reloc, /* special_function */
416          "R_AVR_LDI",           /* name */
417          FALSE,                 /* partial_inplace */
418          0xffff,                /* src_mask */
419          0xffff,                /* dst_mask */
420          FALSE),                /* pcrel_offset */
421   /* A 6 bit absolute relocation of 6 bit offset.
422      For ldd/sdd command.  */
423   HOWTO (R_AVR_6,               /* type */
424          0,                     /* rightshift */
425          0,                     /* size (0 = byte, 1 = short, 2 = long) */
426          6,                     /* bitsize */
427          FALSE,                 /* pc_relative */
428          0,                     /* bitpos */
429          complain_overflow_dont,/* complain_on_overflow */
430          bfd_elf_generic_reloc, /* special_function */
431          "R_AVR_6",             /* name */
432          FALSE,                 /* partial_inplace */
433          0xffff,                /* src_mask */
434          0xffff,                /* dst_mask */
435          FALSE),                /* pcrel_offset */
436   /* A 6 bit absolute relocation of 6 bit offset.
437      For sbiw/adiw command.  */
438   HOWTO (R_AVR_6_ADIW,          /* type */
439          0,                     /* rightshift */
440          0,                     /* size (0 = byte, 1 = short, 2 = long) */
441          6,                     /* bitsize */
442          FALSE,                 /* pc_relative */
443          0,                     /* bitpos */
444          complain_overflow_dont,/* complain_on_overflow */
445          bfd_elf_generic_reloc, /* special_function */
446          "R_AVR_6_ADIW",        /* name */
447          FALSE,                 /* partial_inplace */
448          0xffff,                /* src_mask */
449          0xffff,                /* dst_mask */
450          FALSE),                /* pcrel_offset */
451   /* Most significant 8 bit value of a 32 bit link-time constant.  */
452   HOWTO (R_AVR_MS8_LDI,         /* type */
453          24,                    /* rightshift */
454          1,                     /* size (0 = byte, 1 = short, 2 = long) */
455          8,                     /* bitsize */
456          FALSE,                 /* pc_relative */
457          0,                     /* bitpos */
458          complain_overflow_dont, /* complain_on_overflow */
459          bfd_elf_generic_reloc, /* special_function */
460          "R_AVR_MS8_LDI",       /* name */
461          FALSE,                 /* partial_inplace */
462          0xffff,                /* src_mask */
463          0xffff,                /* dst_mask */
464          FALSE),                /* pcrel_offset */
465   /* Negative most significant 8 bit value of a 32 bit link-time constant.  */
466   HOWTO (R_AVR_MS8_LDI_NEG,     /* type */
467          24,                    /* rightshift */
468          1,                     /* size (0 = byte, 1 = short, 2 = long) */
469          8,                     /* bitsize */
470          FALSE,                 /* pc_relative */
471          0,                     /* bitpos */
472          complain_overflow_dont, /* complain_on_overflow */
473          bfd_elf_generic_reloc, /* special_function */
474          "R_AVR_MS8_LDI_NEG",   /* name */
475          FALSE,                 /* partial_inplace */
476          0xffff,                /* src_mask */
477          0xffff,                /* dst_mask */
478          FALSE),                /* pcrel_offset */
479   /* A low 8 bit absolute relocation of 24 bit program memory address.
480      For LDI command.  Will be changed when linker stubs are needed.  */
481   HOWTO (R_AVR_LO8_LDI_GS,      /* type */
482          1,                     /* rightshift */
483          1,                     /* size (0 = byte, 1 = short, 2 = long) */
484          8,                     /* bitsize */
485          FALSE,                 /* pc_relative */
486          0,                     /* bitpos */
487          complain_overflow_dont, /* complain_on_overflow */
488          bfd_elf_generic_reloc, /* special_function */
489          "R_AVR_LO8_LDI_GS",    /* name */
490          FALSE,                 /* partial_inplace */
491          0xffff,                /* src_mask */
492          0xffff,                /* dst_mask */
493          FALSE),                /* pcrel_offset */
494   /* A low 8 bit absolute relocation of 24 bit program memory address.
495      For LDI command.  Will be changed when linker stubs are needed.  */
496   HOWTO (R_AVR_HI8_LDI_GS,      /* type */
497          9,                     /* rightshift */
498          1,                     /* size (0 = byte, 1 = short, 2 = long) */
499          8,                     /* bitsize */
500          FALSE,                 /* pc_relative */
501          0,                     /* bitpos */
502          complain_overflow_dont, /* complain_on_overflow */
503          bfd_elf_generic_reloc, /* special_function */
504          "R_AVR_HI8_LDI_GS",    /* name */
505          FALSE,                 /* partial_inplace */
506          0xffff,                /* src_mask */
507          0xffff,                /* dst_mask */
508          FALSE),                /* pcrel_offset */
509   /* 8 bit offset.  */
510   HOWTO (R_AVR_8,               /* type */
511          0,                     /* rightshift */
512          0,                     /* size (0 = byte, 1 = short, 2 = long) */
513          8,                     /* bitsize */
514          FALSE,                 /* pc_relative */
515          0,                     /* bitpos */
516          complain_overflow_bitfield,/* complain_on_overflow */
517          bfd_elf_generic_reloc, /* special_function */
518          "R_AVR_8",             /* name */
519          FALSE,                 /* partial_inplace */
520          0x000000ff,            /* src_mask */
521          0x000000ff,            /* dst_mask */
522          FALSE),                /* pcrel_offset */
523   /* lo8-part to use in  .byte lo8(sym).  */
524   HOWTO (R_AVR_8_LO8,           /* type */
525          0,                     /* rightshift */
526          0,                     /* size (0 = byte, 1 = short, 2 = long) */
527          8,                     /* bitsize */
528          FALSE,                 /* pc_relative */
529          0,                     /* bitpos */
530          complain_overflow_dont,/* complain_on_overflow */
531          bfd_elf_generic_reloc, /* special_function */
532          "R_AVR_8_LO8",         /* name */
533          FALSE,                 /* partial_inplace */
534          0xffffff,              /* src_mask */
535          0xffffff,              /* dst_mask */
536          FALSE),                /* pcrel_offset */
537   /* hi8-part to use in  .byte hi8(sym).  */
538   HOWTO (R_AVR_8_HI8,           /* type */
539          8,                     /* rightshift */
540          0,                     /* size (0 = byte, 1 = short, 2 = long) */
541          8,                     /* bitsize */
542          FALSE,                 /* pc_relative */
543          0,                     /* bitpos */
544          complain_overflow_dont,/* complain_on_overflow */
545          bfd_elf_generic_reloc, /* special_function */
546          "R_AVR_8_HI8",         /* name */
547          FALSE,                 /* partial_inplace */
548          0xffffff,              /* src_mask */
549          0xffffff,              /* dst_mask */
550          FALSE),                /* pcrel_offset */
551   /* hlo8-part to use in  .byte hlo8(sym).  */
552   HOWTO (R_AVR_8_HLO8,          /* type */
553          16,                    /* rightshift */
554          0,                     /* size (0 = byte, 1 = short, 2 = long) */
555          8,                     /* bitsize */
556          FALSE,                 /* pc_relative */
557          0,                     /* bitpos */
558          complain_overflow_dont,/* complain_on_overflow */
559          bfd_elf_generic_reloc, /* special_function */
560          "R_AVR_8_HLO8",        /* name */
561          FALSE,                 /* partial_inplace */
562          0xffffff,              /* src_mask */
563          0xffffff,              /* dst_mask */
564          FALSE),                /* pcrel_offset */
565   HOWTO (R_AVR_DIFF8,           /* type */
566          0,                     /* rightshift */
567          0,                     /* size (0 = byte, 1 = short, 2 = long) */
568          8,                     /* bitsize */
569          FALSE,                 /* pc_relative */
570          0,                     /* bitpos */
571          complain_overflow_bitfield, /* complain_on_overflow */
572          bfd_elf_avr_diff_reloc, /* special_function */
573          "R_AVR_DIFF8",         /* name */
574          FALSE,                 /* partial_inplace */
575          0,                     /* src_mask */
576          0xff,                  /* dst_mask */
577          FALSE),                /* pcrel_offset */
578   HOWTO (R_AVR_DIFF16,          /* type */
579          0,                     /* rightshift */
580          1,                     /* size (0 = byte, 1 = short, 2 = long) */
581          16,                    /* bitsize */
582          FALSE,                 /* pc_relative */
583          0,                     /* bitpos */
584          complain_overflow_bitfield, /* complain_on_overflow */
585          bfd_elf_avr_diff_reloc,/* special_function */
586          "R_AVR_DIFF16",        /* name */
587          FALSE,                 /* partial_inplace */
588          0,                     /* src_mask */
589          0xffff,                /* dst_mask */
590          FALSE),                /* pcrel_offset */
591   HOWTO (R_AVR_DIFF32,          /* type */
592          0,                     /* rightshift */
593          2,                     /* size (0 = byte, 1 = short, 2 = long) */
594          32,                    /* bitsize */
595          FALSE,                 /* pc_relative */
596          0,                     /* bitpos */
597          complain_overflow_bitfield, /* complain_on_overflow */
598          bfd_elf_avr_diff_reloc,/* special_function */
599          "R_AVR_DIFF32",        /* name */
600          FALSE,                 /* partial_inplace */
601          0,                     /* src_mask */
602          0xffffffff,            /* dst_mask */
603          FALSE),                /* pcrel_offset */
604   /* 7 bit immediate for LDS/STS in Tiny core.  */
605   HOWTO (R_AVR_LDS_STS_16,  /* type */
606          0,                     /* rightshift */
607          1,                     /* size (0 = byte, 1 = short, 2 = long) */
608          7,                     /* bitsize */
609          FALSE,                 /* pc_relative */
610          0,                     /* bitpos */
611          complain_overflow_dont,/* complain_on_overflow */
612          bfd_elf_generic_reloc, /* special_function */
613          "R_AVR_LDS_STS_16",    /* name */
614          FALSE,                 /* partial_inplace */
615          0xffff,                /* src_mask */
616          0xffff,                /* dst_mask */
617          FALSE),                /* pcrel_offset */
618
619   HOWTO (R_AVR_PORT6,           /* type */
620          0,                     /* rightshift */
621          0,                     /* size (0 = byte, 1 = short, 2 = long) */
622          6,                     /* bitsize */
623          FALSE,                 /* pc_relative */
624          0,                     /* bitpos */
625          complain_overflow_dont,/* complain_on_overflow */
626          bfd_elf_generic_reloc, /* special_function */
627          "R_AVR_PORT6",         /* name */
628          FALSE,                 /* partial_inplace */
629          0xffffff,              /* src_mask */
630          0xffffff,              /* dst_mask */
631          FALSE),                /* pcrel_offset */
632   HOWTO (R_AVR_PORT5,           /* type */
633          0,                     /* rightshift */
634          0,                     /* size (0 = byte, 1 = short, 2 = long) */
635          5,                     /* bitsize */
636          FALSE,                 /* pc_relative */
637          0,                     /* bitpos */
638          complain_overflow_dont,/* complain_on_overflow */
639          bfd_elf_generic_reloc, /* special_function */
640          "R_AVR_PORT5",         /* name */
641          FALSE,                 /* partial_inplace */
642          0xffffff,              /* src_mask */
643          0xffffff,              /* dst_mask */
644          FALSE)                 /* pcrel_offset */
645 };
646
647 /* Map BFD reloc types to AVR ELF reloc types.  */
648
649 struct avr_reloc_map
650 {
651   bfd_reloc_code_real_type bfd_reloc_val;
652   unsigned int elf_reloc_val;
653 };
654
655 static const struct avr_reloc_map avr_reloc_map[] =
656 {
657   { BFD_RELOC_NONE,                 R_AVR_NONE },
658   { BFD_RELOC_32,                   R_AVR_32 },
659   { BFD_RELOC_AVR_7_PCREL,          R_AVR_7_PCREL },
660   { BFD_RELOC_AVR_13_PCREL,         R_AVR_13_PCREL },
661   { BFD_RELOC_16,                   R_AVR_16 },
662   { BFD_RELOC_AVR_16_PM,            R_AVR_16_PM },
663   { BFD_RELOC_AVR_LO8_LDI,          R_AVR_LO8_LDI},
664   { BFD_RELOC_AVR_HI8_LDI,          R_AVR_HI8_LDI },
665   { BFD_RELOC_AVR_HH8_LDI,          R_AVR_HH8_LDI },
666   { BFD_RELOC_AVR_MS8_LDI,          R_AVR_MS8_LDI },
667   { BFD_RELOC_AVR_LO8_LDI_NEG,      R_AVR_LO8_LDI_NEG },
668   { BFD_RELOC_AVR_HI8_LDI_NEG,      R_AVR_HI8_LDI_NEG },
669   { BFD_RELOC_AVR_HH8_LDI_NEG,      R_AVR_HH8_LDI_NEG },
670   { BFD_RELOC_AVR_MS8_LDI_NEG,      R_AVR_MS8_LDI_NEG },
671   { BFD_RELOC_AVR_LO8_LDI_PM,       R_AVR_LO8_LDI_PM },
672   { BFD_RELOC_AVR_LO8_LDI_GS,       R_AVR_LO8_LDI_GS },
673   { BFD_RELOC_AVR_HI8_LDI_PM,       R_AVR_HI8_LDI_PM },
674   { BFD_RELOC_AVR_HI8_LDI_GS,       R_AVR_HI8_LDI_GS },
675   { BFD_RELOC_AVR_HH8_LDI_PM,       R_AVR_HH8_LDI_PM },
676   { BFD_RELOC_AVR_LO8_LDI_PM_NEG,   R_AVR_LO8_LDI_PM_NEG },
677   { BFD_RELOC_AVR_HI8_LDI_PM_NEG,   R_AVR_HI8_LDI_PM_NEG },
678   { BFD_RELOC_AVR_HH8_LDI_PM_NEG,   R_AVR_HH8_LDI_PM_NEG },
679   { BFD_RELOC_AVR_CALL,             R_AVR_CALL },
680   { BFD_RELOC_AVR_LDI,              R_AVR_LDI  },
681   { BFD_RELOC_AVR_6,                R_AVR_6    },
682   { BFD_RELOC_AVR_6_ADIW,           R_AVR_6_ADIW },
683   { BFD_RELOC_8,                    R_AVR_8 },
684   { BFD_RELOC_AVR_8_LO,             R_AVR_8_LO8 },
685   { BFD_RELOC_AVR_8_HI,             R_AVR_8_HI8 },
686   { BFD_RELOC_AVR_8_HLO,            R_AVR_8_HLO8 },
687   { BFD_RELOC_AVR_DIFF8,            R_AVR_DIFF8 },
688   { BFD_RELOC_AVR_DIFF16,           R_AVR_DIFF16 },
689   { BFD_RELOC_AVR_DIFF32,           R_AVR_DIFF32 },
690   { BFD_RELOC_AVR_LDS_STS_16,       R_AVR_LDS_STS_16},
691   { BFD_RELOC_AVR_PORT6,            R_AVR_PORT6},
692   { BFD_RELOC_AVR_PORT5,            R_AVR_PORT5}
693 };
694
695 /* Meant to be filled one day with the wrap around address for the
696    specific device.  I.e. should get the value 0x4000 for 16k devices,
697    0x8000 for 32k devices and so on.
698
699    We initialize it here with a value of 0x1000000 resulting in
700    that we will never suggest a wrap-around jump during relaxation.
701    The logic of the source code later on assumes that in
702    avr_pc_wrap_around one single bit is set.  */
703 static bfd_vma avr_pc_wrap_around = 0x10000000;
704
705 /* If this variable holds a value different from zero, the linker relaxation
706    machine will try to optimize call/ret sequences by a single jump
707    instruction. This option could be switched off by a linker switch.  */
708 static int avr_replace_call_ret_sequences = 1;
709 \f
710 /* Initialize an entry in the stub hash table.  */
711
712 static struct bfd_hash_entry *
713 stub_hash_newfunc (struct bfd_hash_entry *entry,
714                    struct bfd_hash_table *table,
715                    const char *string)
716 {
717   /* Allocate the structure if it has not already been allocated by a
718      subclass.  */
719   if (entry == NULL)
720     {
721       entry = bfd_hash_allocate (table,
722                                  sizeof (struct elf32_avr_stub_hash_entry));
723       if (entry == NULL)
724         return entry;
725     }
726
727   /* Call the allocation method of the superclass.  */
728   entry = bfd_hash_newfunc (entry, table, string);
729   if (entry != NULL)
730     {
731       struct elf32_avr_stub_hash_entry *hsh;
732
733       /* Initialize the local fields.  */
734       hsh = avr_stub_hash_entry (entry);
735       hsh->stub_offset = 0;
736       hsh->target_value = 0;
737     }
738
739   return entry;
740 }
741
742 /* This function is just a straight passthrough to the real
743    function in linker.c.  Its prupose is so that its address
744    can be compared inside the avr_link_hash_table macro.  */
745
746 static struct bfd_hash_entry *
747 elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry,
748                              struct bfd_hash_table * table,
749                              const char * string)
750 {
751   return _bfd_elf_link_hash_newfunc (entry, table, string);
752 }
753
754 /* Free the derived linker hash table.  */
755
756 static void
757 elf32_avr_link_hash_table_free (bfd *obfd)
758 {
759   struct elf32_avr_link_hash_table *htab
760     = (struct elf32_avr_link_hash_table *) obfd->link.hash;
761
762   /* Free the address mapping table.  */
763   if (htab->amt_stub_offsets != NULL)
764     free (htab->amt_stub_offsets);
765   if (htab->amt_destination_addr != NULL)
766     free (htab->amt_destination_addr);
767
768   bfd_hash_table_free (&htab->bstab);
769   _bfd_elf_link_hash_table_free (obfd);
770 }
771
772 /* Create the derived linker hash table.  The AVR ELF port uses the derived
773    hash table to keep information specific to the AVR ELF linker (without
774    using static variables).  */
775
776 static struct bfd_link_hash_table *
777 elf32_avr_link_hash_table_create (bfd *abfd)
778 {
779   struct elf32_avr_link_hash_table *htab;
780   bfd_size_type amt = sizeof (*htab);
781
782   htab = bfd_zmalloc (amt);
783   if (htab == NULL)
784     return NULL;
785
786   if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
787                                       elf32_avr_link_hash_newfunc,
788                                       sizeof (struct elf_link_hash_entry),
789                                       AVR_ELF_DATA))
790     {
791       free (htab);
792       return NULL;
793     }
794
795   /* Init the stub hash table too.  */
796   if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
797                             sizeof (struct elf32_avr_stub_hash_entry)))
798     {
799       _bfd_elf_link_hash_table_free (abfd);
800       return NULL;
801     }
802   htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free;
803
804   return &htab->etab.root;
805 }
806
807 /* Calculates the effective distance of a pc relative jump/call.  */
808
809 static int
810 avr_relative_distance_considering_wrap_around (unsigned int distance)
811 {
812   unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
813   int dist_with_wrap_around = distance & wrap_around_mask;
814
815   if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
816     dist_with_wrap_around -= avr_pc_wrap_around;
817
818   return dist_with_wrap_around;
819 }
820
821
822 static reloc_howto_type *
823 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
824                                  bfd_reloc_code_real_type code)
825 {
826   unsigned int i;
827
828   for (i = 0;
829        i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
830        i++)
831     if (avr_reloc_map[i].bfd_reloc_val == code)
832       return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
833
834   return NULL;
835 }
836
837 static reloc_howto_type *
838 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
839                                  const char *r_name)
840 {
841   unsigned int i;
842
843   for (i = 0;
844        i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
845        i++)
846     if (elf_avr_howto_table[i].name != NULL
847         && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
848       return &elf_avr_howto_table[i];
849
850   return NULL;
851 }
852
853 /* Set the howto pointer for an AVR ELF reloc.  */
854
855 static void
856 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
857                         arelent *cache_ptr,
858                         Elf_Internal_Rela *dst)
859 {
860   unsigned int r_type;
861
862   r_type = ELF32_R_TYPE (dst->r_info);
863   if (r_type >= (unsigned int) R_AVR_max)
864     {
865       _bfd_error_handler (_("%B: invalid AVR reloc number: %d"), abfd, r_type);
866       r_type = 0;
867     }
868   cache_ptr->howto = &elf_avr_howto_table[r_type];
869 }
870
871 static bfd_boolean
872 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
873 {
874   return (relocation >= 0x020000);
875 }
876
877 /* Returns the address of the corresponding stub if there is one.
878    Returns otherwise an address above 0x020000.  This function
879    could also be used, if there is no knowledge on the section where
880    the destination is found.  */
881
882 static bfd_vma
883 avr_get_stub_addr (bfd_vma srel,
884                    struct elf32_avr_link_hash_table *htab)
885 {
886   unsigned int sindex;
887   bfd_vma stub_sec_addr =
888               (htab->stub_sec->output_section->vma +
889                htab->stub_sec->output_offset);
890
891   for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++)
892     if (htab->amt_destination_addr[sindex] == srel)
893       return htab->amt_stub_offsets[sindex] + stub_sec_addr;
894
895   /* Return an address that could not be reached by 16 bit relocs.  */
896   return 0x020000;
897 }
898
899 /* Perform a diff relocation. Nothing to do, as the difference value is already
900    written into the section's contents. */
901
902 static bfd_reloc_status_type
903 bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
904                       arelent *reloc_entry ATTRIBUTE_UNUSED,
905               asymbol *symbol ATTRIBUTE_UNUSED,
906               void *data ATTRIBUTE_UNUSED,
907               asection *input_section ATTRIBUTE_UNUSED,
908               bfd *output_bfd ATTRIBUTE_UNUSED,
909               char **error_message ATTRIBUTE_UNUSED)
910 {
911   return bfd_reloc_ok;
912 }
913
914
915 /* Perform a single relocation.  By default we use the standard BFD
916    routines, but a few relocs, we have to do them ourselves.  */
917
918 static bfd_reloc_status_type
919 avr_final_link_relocate (reloc_howto_type *                 howto,
920                          bfd *                              input_bfd,
921                          asection *                         input_section,
922                          bfd_byte *                         contents,
923                          Elf_Internal_Rela *                rel,
924                          bfd_vma                            relocation,
925                          struct elf32_avr_link_hash_table * htab)
926 {
927   bfd_reloc_status_type r = bfd_reloc_ok;
928   bfd_vma               x;
929   bfd_signed_vma        srel;
930   bfd_signed_vma        reloc_addr;
931   bfd_boolean           use_stubs = FALSE;
932   /* Usually is 0, unless we are generating code for a bootloader.  */
933   bfd_signed_vma        base_addr = htab->vector_base;
934
935   /* Absolute addr of the reloc in the final excecutable.  */
936   reloc_addr = rel->r_offset + input_section->output_section->vma
937                + input_section->output_offset;
938
939   switch (howto->type)
940     {
941     case R_AVR_7_PCREL:
942       contents += rel->r_offset;
943       srel = (bfd_signed_vma) relocation;
944       srel += rel->r_addend;
945       srel -= rel->r_offset;
946       srel -= 2;        /* Branch instructions add 2 to the PC...  */
947       srel -= (input_section->output_section->vma +
948                input_section->output_offset);
949
950       if (srel & 1)
951         return bfd_reloc_outofrange;
952       if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
953         return bfd_reloc_overflow;
954       x = bfd_get_16 (input_bfd, contents);
955       x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
956       bfd_put_16 (input_bfd, x, contents);
957       break;
958
959     case R_AVR_13_PCREL:
960       contents   += rel->r_offset;
961       srel = (bfd_signed_vma) relocation;
962       srel += rel->r_addend;
963       srel -= rel->r_offset;
964       srel -= 2;        /* Branch instructions add 2 to the PC...  */
965       srel -= (input_section->output_section->vma +
966                input_section->output_offset);
967
968       if (srel & 1)
969         return bfd_reloc_outofrange;
970
971       srel = avr_relative_distance_considering_wrap_around (srel);
972
973       /* AVR addresses commands as words.  */
974       srel >>= 1;
975
976       /* Check for overflow.  */
977       if (srel < -2048 || srel > 2047)
978         {
979           /* Relative distance is too large.  */
980
981           /* Always apply WRAPAROUND for avr2, avr25, and avr4.  */
982           switch (bfd_get_mach (input_bfd))
983             {
984             case bfd_mach_avr2:
985             case bfd_mach_avr25:
986             case bfd_mach_avr4:
987               break;
988
989             default:
990               return bfd_reloc_overflow;
991             }
992         }
993
994       x = bfd_get_16 (input_bfd, contents);
995       x = (x & 0xf000) | (srel & 0xfff);
996       bfd_put_16 (input_bfd, x, contents);
997       break;
998
999     case R_AVR_LO8_LDI:
1000       contents += rel->r_offset;
1001       srel = (bfd_signed_vma) relocation + rel->r_addend;
1002       x = bfd_get_16 (input_bfd, contents);
1003       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1004       bfd_put_16 (input_bfd, x, contents);
1005       break;
1006
1007     case R_AVR_LDI:
1008       contents += rel->r_offset;
1009       srel = (bfd_signed_vma) relocation + rel->r_addend;
1010       if (((srel > 0) && (srel & 0xffff) > 255)
1011           || ((srel < 0) && ((-srel) & 0xffff) > 128))
1012         /* Remove offset for data/eeprom section.  */
1013         return bfd_reloc_overflow;
1014
1015       x = bfd_get_16 (input_bfd, contents);
1016       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1017       bfd_put_16 (input_bfd, x, contents);
1018       break;
1019
1020     case R_AVR_6:
1021       contents += rel->r_offset;
1022       srel = (bfd_signed_vma) relocation + rel->r_addend;
1023       if (((srel & 0xffff) > 63) || (srel < 0))
1024         /* Remove offset for data/eeprom section.  */
1025         return bfd_reloc_overflow;
1026       x = bfd_get_16 (input_bfd, contents);
1027       x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
1028                        | ((srel & (1 << 5)) << 8));
1029       bfd_put_16 (input_bfd, x, contents);
1030       break;
1031
1032     case R_AVR_6_ADIW:
1033       contents += rel->r_offset;
1034       srel = (bfd_signed_vma) relocation + rel->r_addend;
1035       if (((srel & 0xffff) > 63) || (srel < 0))
1036         /* Remove offset for data/eeprom section.  */
1037         return bfd_reloc_overflow;
1038       x = bfd_get_16 (input_bfd, contents);
1039       x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
1040       bfd_put_16 (input_bfd, x, contents);
1041       break;
1042
1043     case R_AVR_HI8_LDI:
1044       contents += rel->r_offset;
1045       srel = (bfd_signed_vma) relocation + rel->r_addend;
1046       srel = (srel >> 8) & 0xff;
1047       x = bfd_get_16 (input_bfd, contents);
1048       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1049       bfd_put_16 (input_bfd, x, contents);
1050       break;
1051
1052     case R_AVR_HH8_LDI:
1053       contents += rel->r_offset;
1054       srel = (bfd_signed_vma) relocation + rel->r_addend;
1055       srel = (srel >> 16) & 0xff;
1056       x = bfd_get_16 (input_bfd, contents);
1057       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1058       bfd_put_16 (input_bfd, x, contents);
1059       break;
1060
1061     case R_AVR_MS8_LDI:
1062       contents += rel->r_offset;
1063       srel = (bfd_signed_vma) relocation + rel->r_addend;
1064       srel = (srel >> 24) & 0xff;
1065       x = bfd_get_16 (input_bfd, contents);
1066       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1067       bfd_put_16 (input_bfd, x, contents);
1068       break;
1069
1070     case R_AVR_LO8_LDI_NEG:
1071       contents += rel->r_offset;
1072       srel = (bfd_signed_vma) relocation + rel->r_addend;
1073       srel = -srel;
1074       x = bfd_get_16 (input_bfd, contents);
1075       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1076       bfd_put_16 (input_bfd, x, contents);
1077       break;
1078
1079     case R_AVR_HI8_LDI_NEG:
1080       contents += rel->r_offset;
1081       srel = (bfd_signed_vma) relocation + rel->r_addend;
1082       srel = -srel;
1083       srel = (srel >> 8) & 0xff;
1084       x = bfd_get_16 (input_bfd, contents);
1085       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1086       bfd_put_16 (input_bfd, x, contents);
1087       break;
1088
1089     case R_AVR_HH8_LDI_NEG:
1090       contents += rel->r_offset;
1091       srel = (bfd_signed_vma) relocation + rel->r_addend;
1092       srel = -srel;
1093       srel = (srel >> 16) & 0xff;
1094       x = bfd_get_16 (input_bfd, contents);
1095       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1096       bfd_put_16 (input_bfd, x, contents);
1097       break;
1098
1099     case R_AVR_MS8_LDI_NEG:
1100       contents += rel->r_offset;
1101       srel = (bfd_signed_vma) relocation + rel->r_addend;
1102       srel = -srel;
1103       srel = (srel >> 24) & 0xff;
1104       x = bfd_get_16 (input_bfd, contents);
1105       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1106       bfd_put_16 (input_bfd, x, contents);
1107       break;
1108
1109     case R_AVR_LO8_LDI_GS:
1110       use_stubs = (!htab->no_stubs);
1111       /* Fall through.  */
1112     case R_AVR_LO8_LDI_PM:
1113       contents += rel->r_offset;
1114       srel = (bfd_signed_vma) relocation + rel->r_addend;
1115
1116       if (use_stubs
1117           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1118         {
1119           bfd_vma old_srel = srel;
1120
1121           /* We need to use the address of the stub instead.  */
1122           srel = avr_get_stub_addr (srel, htab);
1123           if (debug_stubs)
1124             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1125                     "reloc at address 0x%x.\n",
1126                     (unsigned int) srel,
1127                     (unsigned int) old_srel,
1128                     (unsigned int) reloc_addr);
1129
1130           if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1131             return bfd_reloc_outofrange;
1132         }
1133
1134       if (srel & 1)
1135         return bfd_reloc_outofrange;
1136       srel = srel >> 1;
1137       x = bfd_get_16 (input_bfd, contents);
1138       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1139       bfd_put_16 (input_bfd, x, contents);
1140       break;
1141
1142     case R_AVR_HI8_LDI_GS:
1143       use_stubs = (!htab->no_stubs);
1144       /* Fall through.  */
1145     case R_AVR_HI8_LDI_PM:
1146       contents += rel->r_offset;
1147       srel = (bfd_signed_vma) relocation + rel->r_addend;
1148
1149       if (use_stubs
1150           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1151         {
1152           bfd_vma old_srel = srel;
1153
1154           /* We need to use the address of the stub instead.  */
1155           srel = avr_get_stub_addr (srel, htab);
1156           if (debug_stubs)
1157             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1158                     "reloc at address 0x%x.\n",
1159                     (unsigned int) srel,
1160                     (unsigned int) old_srel,
1161                     (unsigned int) reloc_addr);
1162
1163           if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1164             return bfd_reloc_outofrange;
1165         }
1166
1167       if (srel & 1)
1168         return bfd_reloc_outofrange;
1169       srel = srel >> 1;
1170       srel = (srel >> 8) & 0xff;
1171       x = bfd_get_16 (input_bfd, contents);
1172       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1173       bfd_put_16 (input_bfd, x, contents);
1174       break;
1175
1176     case R_AVR_HH8_LDI_PM:
1177       contents += rel->r_offset;
1178       srel = (bfd_signed_vma) relocation + rel->r_addend;
1179       if (srel & 1)
1180         return bfd_reloc_outofrange;
1181       srel = srel >> 1;
1182       srel = (srel >> 16) & 0xff;
1183       x = bfd_get_16 (input_bfd, contents);
1184       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1185       bfd_put_16 (input_bfd, x, contents);
1186       break;
1187
1188     case R_AVR_LO8_LDI_PM_NEG:
1189       contents += rel->r_offset;
1190       srel = (bfd_signed_vma) relocation + rel->r_addend;
1191       srel = -srel;
1192       if (srel & 1)
1193         return bfd_reloc_outofrange;
1194       srel = srel >> 1;
1195       x = bfd_get_16 (input_bfd, contents);
1196       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1197       bfd_put_16 (input_bfd, x, contents);
1198       break;
1199
1200     case R_AVR_HI8_LDI_PM_NEG:
1201       contents += rel->r_offset;
1202       srel = (bfd_signed_vma) relocation + rel->r_addend;
1203       srel = -srel;
1204       if (srel & 1)
1205         return bfd_reloc_outofrange;
1206       srel = srel >> 1;
1207       srel = (srel >> 8) & 0xff;
1208       x = bfd_get_16 (input_bfd, contents);
1209       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1210       bfd_put_16 (input_bfd, x, contents);
1211       break;
1212
1213     case R_AVR_HH8_LDI_PM_NEG:
1214       contents += rel->r_offset;
1215       srel = (bfd_signed_vma) relocation + rel->r_addend;
1216       srel = -srel;
1217       if (srel & 1)
1218         return bfd_reloc_outofrange;
1219       srel = srel >> 1;
1220       srel = (srel >> 16) & 0xff;
1221       x = bfd_get_16 (input_bfd, contents);
1222       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1223       bfd_put_16 (input_bfd, x, contents);
1224       break;
1225
1226     case R_AVR_CALL:
1227       contents += rel->r_offset;
1228       srel = (bfd_signed_vma) relocation + rel->r_addend;
1229       if (srel & 1)
1230         return bfd_reloc_outofrange;
1231       srel = srel >> 1;
1232       x = bfd_get_16 (input_bfd, contents);
1233       x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1234       bfd_put_16 (input_bfd, x, contents);
1235       bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1236       break;
1237
1238     case R_AVR_16_PM:
1239       use_stubs = (!htab->no_stubs);
1240       contents += rel->r_offset;
1241       srel = (bfd_signed_vma) relocation + rel->r_addend;
1242
1243       if (use_stubs
1244           && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1245         {
1246           bfd_vma old_srel = srel;
1247
1248           /* We need to use the address of the stub instead.  */
1249           srel = avr_get_stub_addr (srel,htab);
1250           if (debug_stubs)
1251             printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1252                     "reloc at address 0x%x.\n",
1253                     (unsigned int) srel,
1254                     (unsigned int) old_srel,
1255                     (unsigned int) reloc_addr);
1256
1257           if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1258             return bfd_reloc_outofrange;
1259         }
1260
1261       if (srel & 1)
1262         return bfd_reloc_outofrange;
1263       srel = srel >> 1;
1264       bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1265       break;
1266
1267     case R_AVR_DIFF8:
1268     case R_AVR_DIFF16:
1269     case R_AVR_DIFF32:
1270       /* Nothing to do here, as contents already contains the diff value. */
1271       r = bfd_reloc_ok;
1272       break;
1273
1274    case R_AVR_LDS_STS_16:
1275       contents += rel->r_offset;
1276       srel = (bfd_signed_vma) relocation + rel->r_addend;
1277       if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf)
1278         return bfd_reloc_outofrange;
1279       srel = srel & 0x7f;
1280       x = bfd_get_16 (input_bfd, contents);
1281       x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2);
1282       bfd_put_16 (input_bfd, x, contents);
1283       break;
1284
1285     case R_AVR_PORT6:
1286       contents += rel->r_offset;
1287       srel = (bfd_signed_vma) relocation + rel->r_addend;
1288       if ((srel & 0xffff) > 0x3f)
1289         return bfd_reloc_outofrange;
1290       x = bfd_get_16 (input_bfd, contents);
1291       x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f);
1292       bfd_put_16 (input_bfd, x, contents);
1293       break;
1294
1295     case R_AVR_PORT5:
1296       contents += rel->r_offset;
1297       srel = (bfd_signed_vma) relocation + rel->r_addend;
1298       if ((srel & 0xffff) > 0x1f)
1299         return bfd_reloc_outofrange;
1300       x = bfd_get_16 (input_bfd, contents);
1301       x = (x & 0xff07) | ((srel & 0x1f) << 3);
1302       bfd_put_16 (input_bfd, x, contents);
1303       break;
1304
1305     default:
1306       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1307                                     contents, rel->r_offset,
1308                                     relocation, rel->r_addend);
1309     }
1310
1311   return r;
1312 }
1313
1314 /* Relocate an AVR ELF section.  */
1315
1316 static bfd_boolean
1317 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1318                             struct bfd_link_info *info,
1319                             bfd *input_bfd,
1320                             asection *input_section,
1321                             bfd_byte *contents,
1322                             Elf_Internal_Rela *relocs,
1323                             Elf_Internal_Sym *local_syms,
1324                             asection **local_sections)
1325 {
1326   Elf_Internal_Shdr *           symtab_hdr;
1327   struct elf_link_hash_entry ** sym_hashes;
1328   Elf_Internal_Rela *           rel;
1329   Elf_Internal_Rela *           relend;
1330   struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
1331
1332   if (htab == NULL)
1333     return FALSE;
1334
1335   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1336   sym_hashes = elf_sym_hashes (input_bfd);
1337   relend     = relocs + input_section->reloc_count;
1338
1339   for (rel = relocs; rel < relend; rel ++)
1340     {
1341       reloc_howto_type *           howto;
1342       unsigned long                r_symndx;
1343       Elf_Internal_Sym *           sym;
1344       asection *                   sec;
1345       struct elf_link_hash_entry * h;
1346       bfd_vma                      relocation;
1347       bfd_reloc_status_type        r;
1348       const char *                 name;
1349       int                          r_type;
1350
1351       r_type = ELF32_R_TYPE (rel->r_info);
1352       r_symndx = ELF32_R_SYM (rel->r_info);
1353       howto  = elf_avr_howto_table + r_type;
1354       h      = NULL;
1355       sym    = NULL;
1356       sec    = NULL;
1357
1358       if (r_symndx < symtab_hdr->sh_info)
1359         {
1360           sym = local_syms + r_symndx;
1361           sec = local_sections [r_symndx];
1362           relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1363
1364           name = bfd_elf_string_from_elf_section
1365             (input_bfd, symtab_hdr->sh_link, sym->st_name);
1366           name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1367         }
1368       else
1369         {
1370           bfd_boolean unresolved_reloc, warned, ignored;
1371
1372           RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1373                                    r_symndx, symtab_hdr, sym_hashes,
1374                                    h, sec, relocation,
1375                                    unresolved_reloc, warned, ignored);
1376
1377           name = h->root.root.string;
1378         }
1379
1380       if (sec != NULL && discarded_section (sec))
1381         RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1382                                          rel, 1, relend, howto, 0, contents);
1383
1384       if (info->relocatable)
1385         continue;
1386
1387       r = avr_final_link_relocate (howto, input_bfd, input_section,
1388                                    contents, rel, relocation, htab);
1389
1390       if (r != bfd_reloc_ok)
1391         {
1392           const char * msg = (const char *) NULL;
1393
1394           switch (r)
1395             {
1396             case bfd_reloc_overflow:
1397               r = info->callbacks->reloc_overflow
1398                 (info, (h ? &h->root : NULL),
1399                  name, howto->name, (bfd_vma) 0,
1400                  input_bfd, input_section, rel->r_offset);
1401               break;
1402
1403             case bfd_reloc_undefined:
1404               r = info->callbacks->undefined_symbol
1405                 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1406               break;
1407
1408             case bfd_reloc_outofrange:
1409               msg = _("internal error: out of range error");
1410               break;
1411
1412             case bfd_reloc_notsupported:
1413               msg = _("internal error: unsupported relocation error");
1414               break;
1415
1416             case bfd_reloc_dangerous:
1417               msg = _("internal error: dangerous relocation");
1418               break;
1419
1420             default:
1421               msg = _("internal error: unknown error");
1422               break;
1423             }
1424
1425           if (msg)
1426             r = info->callbacks->warning
1427               (info, msg, name, input_bfd, input_section, rel->r_offset);
1428
1429           if (! r)
1430             return FALSE;
1431         }
1432     }
1433
1434   return TRUE;
1435 }
1436
1437 /* The final processing done just before writing out a AVR ELF object
1438    file.  This gets the AVR architecture right based on the machine
1439    number.  */
1440
1441 static void
1442 bfd_elf_avr_final_write_processing (bfd *abfd,
1443                                     bfd_boolean linker ATTRIBUTE_UNUSED)
1444 {
1445   unsigned long val;
1446
1447   switch (bfd_get_mach (abfd))
1448     {
1449     default:
1450     case bfd_mach_avr2:
1451       val = E_AVR_MACH_AVR2;
1452       break;
1453
1454     case bfd_mach_avr1:
1455       val = E_AVR_MACH_AVR1;
1456       break;
1457
1458     case bfd_mach_avr25:
1459       val = E_AVR_MACH_AVR25;
1460       break;
1461
1462     case bfd_mach_avr3:
1463       val = E_AVR_MACH_AVR3;
1464       break;
1465
1466     case bfd_mach_avr31:
1467       val = E_AVR_MACH_AVR31;
1468       break;
1469
1470     case bfd_mach_avr35:
1471       val = E_AVR_MACH_AVR35;
1472       break;
1473
1474     case bfd_mach_avr4:
1475       val = E_AVR_MACH_AVR4;
1476       break;
1477
1478     case bfd_mach_avr5:
1479       val = E_AVR_MACH_AVR5;
1480       break;
1481
1482     case bfd_mach_avr51:
1483       val = E_AVR_MACH_AVR51;
1484       break;
1485
1486     case bfd_mach_avr6:
1487       val = E_AVR_MACH_AVR6;
1488       break;
1489
1490     case bfd_mach_avrxmega1:
1491       val = E_AVR_MACH_XMEGA1;
1492       break;
1493
1494     case bfd_mach_avrxmega2:
1495       val = E_AVR_MACH_XMEGA2;
1496       break;
1497
1498     case bfd_mach_avrxmega3:
1499       val = E_AVR_MACH_XMEGA3;
1500       break;
1501
1502     case bfd_mach_avrxmega4:
1503       val = E_AVR_MACH_XMEGA4;
1504       break;
1505
1506     case bfd_mach_avrxmega5:
1507       val = E_AVR_MACH_XMEGA5;
1508       break;
1509
1510     case bfd_mach_avrxmega6:
1511       val = E_AVR_MACH_XMEGA6;
1512       break;
1513
1514     case bfd_mach_avrxmega7:
1515       val = E_AVR_MACH_XMEGA7;
1516       break;
1517
1518    case bfd_mach_avrtiny:
1519       val = E_AVR_MACH_AVRTINY;
1520       break;
1521     }
1522
1523   elf_elfheader (abfd)->e_machine = EM_AVR;
1524   elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1525   elf_elfheader (abfd)->e_flags |= val;
1526 }
1527
1528 /* Set the right machine number.  */
1529
1530 static bfd_boolean
1531 elf32_avr_object_p (bfd *abfd)
1532 {
1533   unsigned int e_set = bfd_mach_avr2;
1534
1535   if (elf_elfheader (abfd)->e_machine == EM_AVR
1536       || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1537     {
1538       int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1539
1540       switch (e_mach)
1541         {
1542         default:
1543         case E_AVR_MACH_AVR2:
1544           e_set = bfd_mach_avr2;
1545           break;
1546
1547         case E_AVR_MACH_AVR1:
1548           e_set = bfd_mach_avr1;
1549           break;
1550
1551         case E_AVR_MACH_AVR25:
1552           e_set = bfd_mach_avr25;
1553           break;
1554
1555         case E_AVR_MACH_AVR3:
1556           e_set = bfd_mach_avr3;
1557           break;
1558
1559         case E_AVR_MACH_AVR31:
1560           e_set = bfd_mach_avr31;
1561           break;
1562
1563         case E_AVR_MACH_AVR35:
1564           e_set = bfd_mach_avr35;
1565           break;
1566
1567         case E_AVR_MACH_AVR4:
1568           e_set = bfd_mach_avr4;
1569           break;
1570
1571         case E_AVR_MACH_AVR5:
1572           e_set = bfd_mach_avr5;
1573           break;
1574
1575         case E_AVR_MACH_AVR51:
1576           e_set = bfd_mach_avr51;
1577           break;
1578
1579         case E_AVR_MACH_AVR6:
1580           e_set = bfd_mach_avr6;
1581           break;
1582
1583         case E_AVR_MACH_XMEGA1:
1584           e_set = bfd_mach_avrxmega1;
1585           break;
1586
1587         case E_AVR_MACH_XMEGA2:
1588           e_set = bfd_mach_avrxmega2;
1589           break;
1590
1591         case E_AVR_MACH_XMEGA3:
1592           e_set = bfd_mach_avrxmega3;
1593           break;
1594
1595         case E_AVR_MACH_XMEGA4:
1596           e_set = bfd_mach_avrxmega4;
1597           break;
1598
1599         case E_AVR_MACH_XMEGA5:
1600           e_set = bfd_mach_avrxmega5;
1601           break;
1602
1603         case E_AVR_MACH_XMEGA6:
1604           e_set = bfd_mach_avrxmega6;
1605           break;
1606
1607         case E_AVR_MACH_XMEGA7:
1608           e_set = bfd_mach_avrxmega7;
1609           break;
1610
1611     case E_AVR_MACH_AVRTINY:
1612       e_set = bfd_mach_avrtiny;
1613       break;
1614         }
1615     }
1616   return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1617                                     e_set);
1618 }
1619
1620 /* Returns whether the relocation type passed is a diff reloc. */
1621
1622 static bfd_boolean
1623 elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel)
1624 {
1625   return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8
1626           ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16
1627           || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32);
1628 }
1629
1630 /* Reduce the diff value written in the section by count if the shrinked
1631    insn address happens to fall between the two symbols for which this
1632    diff reloc was emitted.  */
1633
1634 static void
1635 elf32_avr_adjust_diff_reloc_value (bfd *abfd,
1636                                    struct bfd_section *isec,
1637                                    Elf_Internal_Rela *irel,
1638                                    bfd_vma symval,
1639                                    bfd_vma shrinked_insn_address,
1640                                    int count)
1641 {
1642   unsigned char *reloc_contents = NULL;
1643   unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
1644   if (isec_contents == NULL)
1645   {
1646     if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
1647       return;
1648
1649     elf_section_data (isec)->this_hdr.contents = isec_contents;
1650   }
1651
1652   reloc_contents = isec_contents + irel->r_offset;
1653
1654   /* Read value written in object file. */
1655  bfd_vma x = 0;
1656   switch (ELF32_R_TYPE (irel->r_info))
1657   {
1658   case R_AVR_DIFF8:
1659     {
1660       x = *reloc_contents;
1661       break;
1662     }
1663   case R_AVR_DIFF16:
1664     {
1665       x = bfd_get_16 (abfd, reloc_contents);
1666       break;
1667     }
1668   case R_AVR_DIFF32:
1669     {
1670       x = bfd_get_32 (abfd, reloc_contents);
1671       break;
1672     }
1673   default:
1674     {
1675       BFD_FAIL();
1676     }
1677   }
1678
1679   /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
1680      into the object file at the reloc offset. sym2's logical value is
1681      symval (<start_of_section>) + reloc addend. Compute the start and end
1682      addresses and check if the shrinked insn falls between sym1 and sym2. */
1683
1684   bfd_vma end_address = symval + irel->r_addend;
1685   bfd_vma start_address = end_address - x;
1686
1687   /* Reduce the diff value by count bytes and write it back into section
1688     contents. */
1689
1690   if (shrinked_insn_address >= start_address
1691       && shrinked_insn_address <= end_address)
1692   {
1693     switch (ELF32_R_TYPE (irel->r_info))
1694     {
1695     case R_AVR_DIFF8:
1696       {
1697         *reloc_contents = (x - count);
1698         break;
1699       }
1700     case R_AVR_DIFF16:
1701       {
1702         bfd_put_16 (abfd, (x - count) & 0xFFFF, reloc_contents);
1703         break;
1704       }
1705     case R_AVR_DIFF32:
1706       {
1707         bfd_put_32 (abfd, (x - count) & 0xFFFFFFFF, reloc_contents);
1708         break;
1709       }
1710     default:
1711       {
1712         BFD_FAIL();
1713       }
1714     }
1715
1716   }
1717 }
1718
1719 /* Delete some bytes from a section while changing the size of an instruction.
1720    The parameter "addr" denotes the section-relative offset pointing just
1721    behind the shrinked instruction. "addr+count" point at the first
1722    byte just behind the original unshrinked instruction.  */
1723
1724 static bfd_boolean
1725 elf32_avr_relax_delete_bytes (bfd *abfd,
1726                               asection *sec,
1727                               bfd_vma addr,
1728                               int count)
1729 {
1730   Elf_Internal_Shdr *symtab_hdr;
1731   unsigned int sec_shndx;
1732   bfd_byte *contents;
1733   Elf_Internal_Rela *irel, *irelend;
1734   Elf_Internal_Sym *isym;
1735   Elf_Internal_Sym *isymbuf = NULL;
1736   bfd_vma toaddr;
1737   struct elf_link_hash_entry **sym_hashes;
1738   struct elf_link_hash_entry **end_hashes;
1739   unsigned int symcount;
1740
1741   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1742   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1743   contents = elf_section_data (sec)->this_hdr.contents;
1744
1745   toaddr = sec->size;
1746
1747   irel = elf_section_data (sec)->relocs;
1748   irelend = irel + sec->reloc_count;
1749
1750   /* Actually delete the bytes.  */
1751   if (toaddr - addr - count > 0)
1752     memmove (contents + addr, contents + addr + count,
1753              (size_t) (toaddr - addr - count));
1754   sec->size -= count;
1755
1756   /* Adjust all the reloc addresses.  */
1757   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1758     {
1759       bfd_vma old_reloc_address;
1760
1761       old_reloc_address = (sec->output_section->vma
1762                            + sec->output_offset + irel->r_offset);
1763
1764       /* Get the new reloc address.  */
1765       if ((irel->r_offset > addr
1766            && irel->r_offset < toaddr))
1767         {
1768           if (debug_relax)
1769             printf ("Relocation at address 0x%x needs to be moved.\n"
1770                     "Old section offset: 0x%x, New section offset: 0x%x \n",
1771                     (unsigned int) old_reloc_address,
1772                     (unsigned int) irel->r_offset,
1773                     (unsigned int) ((irel->r_offset) - count));
1774
1775           irel->r_offset -= count;
1776         }
1777
1778     }
1779
1780    /* The reloc's own addresses are now ok. However, we need to readjust
1781       the reloc's addend, i.e. the reloc's value if two conditions are met:
1782       1.) the reloc is relative to a symbol in this section that
1783           is located in front of the shrinked instruction
1784       2.) symbol plus addend end up behind the shrinked instruction.
1785
1786       The most common case where this happens are relocs relative to
1787       the section-start symbol.
1788
1789       This step needs to be done for all of the sections of the bfd.  */
1790
1791   {
1792     struct bfd_section *isec;
1793
1794     for (isec = abfd->sections; isec; isec = isec->next)
1795      {
1796        bfd_vma symval;
1797        bfd_vma shrinked_insn_address;
1798
1799        if (isec->reloc_count == 0)
1800          continue;
1801
1802        shrinked_insn_address = (sec->output_section->vma
1803                                 + sec->output_offset + addr - count);
1804
1805        irel = elf_section_data (isec)->relocs;
1806        /* PR 12161: Read in the relocs for this section if necessary.  */
1807        if (irel == NULL)
1808          irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
1809
1810        for (irelend = irel + isec->reloc_count;
1811             irel < irelend;
1812             irel++)
1813          {
1814            /* Read this BFD's local symbols if we haven't done
1815               so already.  */
1816            if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1817              {
1818                isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1819                if (isymbuf == NULL)
1820                  isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1821                                                  symtab_hdr->sh_info, 0,
1822                                                  NULL, NULL, NULL);
1823                if (isymbuf == NULL)
1824                  return FALSE;
1825              }
1826
1827            /* Get the value of the symbol referred to by the reloc.  */
1828            if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1829              {
1830                /* A local symbol.  */
1831                asection *sym_sec;
1832
1833                isym = isymbuf + ELF32_R_SYM (irel->r_info);
1834                sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1835                symval = isym->st_value;
1836                /* If the reloc is absolute, it will not have
1837                   a symbol or section associated with it.  */
1838                if (sym_sec == sec)
1839                  {
1840                    symval += sym_sec->output_section->vma
1841                              + sym_sec->output_offset;
1842
1843                    if (debug_relax)
1844                      printf ("Checking if the relocation's "
1845                              "addend needs corrections.\n"
1846                              "Address of anchor symbol: 0x%x \n"
1847                              "Address of relocation target: 0x%x \n"
1848                              "Address of relaxed insn: 0x%x \n",
1849                              (unsigned int) symval,
1850                              (unsigned int) (symval + irel->r_addend),
1851                              (unsigned int) shrinked_insn_address);
1852
1853                    if (symval <= shrinked_insn_address
1854                        && (symval + irel->r_addend) > shrinked_insn_address)
1855                      {
1856                        if (elf32_avr_is_diff_reloc (irel))
1857                          {
1858                            elf32_avr_adjust_diff_reloc_value (abfd, isec, irel,
1859                                                          symval,
1860                                                          shrinked_insn_address,
1861                                                         count);
1862                          }
1863
1864                        irel->r_addend -= count;
1865
1866                        if (debug_relax)
1867                          printf ("Relocation's addend needed to be fixed \n");
1868                      }
1869                  }
1870                /* else...Reference symbol is absolute.  No adjustment needed.  */
1871              }
1872            /* else...Reference symbol is extern.  No need for adjusting
1873               the addend.  */
1874          }
1875      }
1876   }
1877
1878   /* Adjust the local symbols defined in this section.  */
1879   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1880   /* Fix PR 9841, there may be no local symbols.  */
1881   if (isym != NULL)
1882     {
1883       Elf_Internal_Sym *isymend;
1884
1885       isymend = isym + symtab_hdr->sh_info;
1886       for (; isym < isymend; isym++)
1887         {
1888           if (isym->st_shndx == sec_shndx)
1889             {
1890               if (isym->st_value > addr
1891                   && isym->st_value <= toaddr)
1892                 isym->st_value -= count;
1893
1894               if (isym->st_value <= addr
1895                   && isym->st_value + isym->st_size > addr)
1896                 {
1897                   /* If this assert fires then we have a symbol that ends
1898                      part way through an instruction.  Does that make
1899                      sense?  */
1900                   BFD_ASSERT (isym->st_value + isym->st_size >= addr + count);
1901                   isym->st_size -= count;
1902                 }
1903             }
1904         }
1905     }
1906
1907   /* Now adjust the global symbols defined in this section.  */
1908   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1909               - symtab_hdr->sh_info);
1910   sym_hashes = elf_sym_hashes (abfd);
1911   end_hashes = sym_hashes + symcount;
1912   for (; sym_hashes < end_hashes; sym_hashes++)
1913     {
1914       struct elf_link_hash_entry *sym_hash = *sym_hashes;
1915       if ((sym_hash->root.type == bfd_link_hash_defined
1916            || sym_hash->root.type == bfd_link_hash_defweak)
1917           && sym_hash->root.u.def.section == sec)
1918         {
1919           if (sym_hash->root.u.def.value > addr
1920               && sym_hash->root.u.def.value <= toaddr)
1921             sym_hash->root.u.def.value -= count;
1922
1923           if (sym_hash->root.u.def.value <= addr
1924               && (sym_hash->root.u.def.value + sym_hash->size > addr))
1925             {
1926               /* If this assert fires then we have a symbol that ends
1927                  part way through an instruction.  Does that make
1928                  sense?  */
1929               BFD_ASSERT (sym_hash->root.u.def.value + sym_hash->size
1930                           >= addr + count);
1931               sym_hash->size -= count;
1932             }
1933         }
1934     }
1935
1936   return TRUE;
1937 }
1938
1939 static Elf_Internal_Sym *
1940 retrieve_local_syms (bfd *input_bfd)
1941 {
1942   Elf_Internal_Shdr *symtab_hdr;
1943   Elf_Internal_Sym *isymbuf;
1944   size_t locsymcount;
1945
1946   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1947   locsymcount = symtab_hdr->sh_info;
1948
1949   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1950   if (isymbuf == NULL && locsymcount != 0)
1951     isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
1952                                     NULL, NULL, NULL);
1953
1954   /* Save the symbols for this input file so they won't be read again.  */
1955   if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
1956     symtab_hdr->contents = (unsigned char *) isymbuf;
1957
1958   return isymbuf;
1959 }
1960
1961 /* Get the input section for a given symbol index.
1962    If the symbol is:
1963    . a section symbol, return the section;
1964    . a common symbol, return the common section;
1965    . an undefined symbol, return the undefined section;
1966    . an indirect symbol, follow the links;
1967    . an absolute value, return the absolute section.  */
1968
1969 static asection *
1970 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
1971 {
1972   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1973   asection *target_sec = NULL;
1974   if (r_symndx < symtab_hdr->sh_info)
1975     {
1976       Elf_Internal_Sym *isymbuf;
1977       unsigned int section_index;
1978
1979       isymbuf = retrieve_local_syms (abfd);
1980       section_index = isymbuf[r_symndx].st_shndx;
1981
1982       if (section_index == SHN_UNDEF)
1983         target_sec = bfd_und_section_ptr;
1984       else if (section_index == SHN_ABS)
1985         target_sec = bfd_abs_section_ptr;
1986       else if (section_index == SHN_COMMON)
1987         target_sec = bfd_com_section_ptr;
1988       else
1989         target_sec = bfd_section_from_elf_index (abfd, section_index);
1990     }
1991   else
1992     {
1993       unsigned long indx = r_symndx - symtab_hdr->sh_info;
1994       struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
1995
1996       while (h->root.type == bfd_link_hash_indirect
1997              || h->root.type == bfd_link_hash_warning)
1998         h = (struct elf_link_hash_entry *) h->root.u.i.link;
1999
2000       switch (h->root.type)
2001         {
2002         case bfd_link_hash_defined:
2003         case  bfd_link_hash_defweak:
2004           target_sec = h->root.u.def.section;
2005           break;
2006         case bfd_link_hash_common:
2007           target_sec = bfd_com_section_ptr;
2008           break;
2009         case bfd_link_hash_undefined:
2010         case bfd_link_hash_undefweak:
2011           target_sec = bfd_und_section_ptr;
2012           break;
2013         default: /* New indirect warning.  */
2014           target_sec = bfd_und_section_ptr;
2015           break;
2016         }
2017     }
2018   return target_sec;
2019 }
2020
2021 /* Get the section-relative offset for a symbol number.  */
2022
2023 static bfd_vma
2024 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
2025 {
2026   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2027   bfd_vma offset = 0;
2028
2029   if (r_symndx < symtab_hdr->sh_info)
2030     {
2031       Elf_Internal_Sym *isymbuf;
2032       isymbuf = retrieve_local_syms (abfd);
2033       offset = isymbuf[r_symndx].st_value;
2034     }
2035   else
2036     {
2037       unsigned long indx = r_symndx - symtab_hdr->sh_info;
2038       struct elf_link_hash_entry *h =
2039         elf_sym_hashes (abfd)[indx];
2040
2041       while (h->root.type == bfd_link_hash_indirect
2042              || h->root.type == bfd_link_hash_warning)
2043         h = (struct elf_link_hash_entry *) h->root.u.i.link;
2044       if (h->root.type == bfd_link_hash_defined
2045           || h->root.type == bfd_link_hash_defweak)
2046         offset = h->root.u.def.value;
2047     }
2048   return offset;
2049 }
2050
2051 /* This function handles relaxing for the avr.
2052    Many important relaxing opportunities within functions are already
2053    realized by the compiler itself.
2054    Here we try to replace  call (4 bytes) ->  rcall (2 bytes)
2055    and jump -> rjmp (safes also 2 bytes).
2056    As well we now optimize seqences of
2057      - call/rcall function
2058      - ret
2059    to yield
2060      - jmp/rjmp function
2061      - ret
2062    . In case that within a sequence
2063      - jmp/rjmp label
2064      - ret
2065    the ret could no longer be reached it is optimized away. In order
2066    to check if the ret is no longer needed, it is checked that the ret's address
2067    is not the target of a branch or jump within the same section, it is checked
2068    that there is no skip instruction before the jmp/rjmp and that there
2069    is no local or global label place at the address of the ret.
2070
2071    We refrain from relaxing within sections ".vectors" and
2072    ".jumptables" in order to maintain the position of the instructions.
2073    There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
2074    if possible. (In future one could possibly use the space of the nop
2075    for the first instruction of the irq service function.
2076
2077    The .jumptables sections is meant to be used for a future tablejump variant
2078    for the devices with 3-byte program counter where the table itself
2079    contains 4-byte jump instructions whose relative offset must not
2080    be changed.  */
2081
2082 static bfd_boolean
2083 elf32_avr_relax_section (bfd *abfd,
2084                          asection *sec,
2085                          struct bfd_link_info *link_info,
2086                          bfd_boolean *again)
2087 {
2088   Elf_Internal_Shdr *symtab_hdr;
2089   Elf_Internal_Rela *internal_relocs;
2090   Elf_Internal_Rela *irel, *irelend;
2091   bfd_byte *contents = NULL;
2092   Elf_Internal_Sym *isymbuf = NULL;
2093   struct elf32_avr_link_hash_table *htab;
2094
2095   /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
2096      relaxing. Such shrinking can cause issues for the sections such
2097      as .vectors and .jumptables. Instead the unused bytes should be
2098      filled with nop instructions. */
2099   bfd_boolean shrinkable = TRUE;
2100
2101   if (!strcmp (sec->name,".vectors")
2102       || !strcmp (sec->name,".jumptables"))
2103     shrinkable = FALSE;
2104
2105   if (link_info->relocatable)
2106     (*link_info->callbacks->einfo)
2107       (_("%P%F: --relax and -r may not be used together\n"));
2108
2109   htab = avr_link_hash_table (link_info);
2110   if (htab == NULL)
2111     return FALSE;
2112
2113   /* Assume nothing changes.  */
2114   *again = FALSE;
2115
2116   if ((!htab->no_stubs) && (sec == htab->stub_sec))
2117     {
2118       /* We are just relaxing the stub section.
2119          Let's calculate the size needed again.  */
2120       bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
2121
2122       if (debug_relax)
2123         printf ("Relaxing the stub section. Size prior to this pass: %i\n",
2124                 (int) last_estimated_stub_section_size);
2125
2126       elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
2127                             link_info, FALSE);
2128
2129       /* Check if the number of trampolines changed.  */
2130       if (last_estimated_stub_section_size != htab->stub_sec->size)
2131         *again = TRUE;
2132
2133       if (debug_relax)
2134         printf ("Size of stub section after this pass: %i\n",
2135                 (int) htab->stub_sec->size);
2136
2137       return TRUE;
2138     }
2139
2140   /* We don't have to do anything for a relocatable link, if
2141      this section does not have relocs, or if this is not a
2142      code section.  */
2143   if (link_info->relocatable
2144       || (sec->flags & SEC_RELOC) == 0
2145       || sec->reloc_count == 0
2146       || (sec->flags & SEC_CODE) == 0)
2147     return TRUE;
2148
2149   /* Check if the object file to relax uses internal symbols so that we
2150      could fix up the relocations.  */
2151   if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
2152     return TRUE;
2153
2154   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2155
2156   /* Get a copy of the native relocations.  */
2157   internal_relocs = (_bfd_elf_link_read_relocs
2158                      (abfd, sec, NULL, NULL, link_info->keep_memory));
2159   if (internal_relocs == NULL)
2160     goto error_return;
2161
2162   /* Walk through the relocs looking for relaxing opportunities.  */
2163   irelend = internal_relocs + sec->reloc_count;
2164   for (irel = internal_relocs; irel < irelend; irel++)
2165     {
2166       bfd_vma symval;
2167
2168       if (   ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
2169           && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
2170           && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
2171         continue;
2172
2173       /* Get the section contents if we haven't done so already.  */
2174       if (contents == NULL)
2175         {
2176           /* Get cached copy if it exists.  */
2177           if (elf_section_data (sec)->this_hdr.contents != NULL)
2178             contents = elf_section_data (sec)->this_hdr.contents;
2179           else
2180             {
2181               /* Go get them off disk.  */
2182               if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2183                 goto error_return;
2184             }
2185         }
2186
2187       /* Read this BFD's local symbols if we haven't done so already.  */
2188       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2189         {
2190           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2191           if (isymbuf == NULL)
2192             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2193                                             symtab_hdr->sh_info, 0,
2194                                             NULL, NULL, NULL);
2195           if (isymbuf == NULL)
2196             goto error_return;
2197         }
2198
2199
2200       /* Get the value of the symbol referred to by the reloc.  */
2201       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2202         {
2203           /* A local symbol.  */
2204           Elf_Internal_Sym *isym;
2205           asection *sym_sec;
2206
2207           isym = isymbuf + ELF32_R_SYM (irel->r_info);
2208           sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2209           symval = isym->st_value;
2210           /* If the reloc is absolute, it will not have
2211              a symbol or section associated with it.  */
2212           if (sym_sec)
2213             symval += sym_sec->output_section->vma
2214               + sym_sec->output_offset;
2215         }
2216       else
2217         {
2218           unsigned long indx;
2219           struct elf_link_hash_entry *h;
2220
2221           /* An external symbol.  */
2222           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2223           h = elf_sym_hashes (abfd)[indx];
2224           BFD_ASSERT (h != NULL);
2225           if (h->root.type != bfd_link_hash_defined
2226               && h->root.type != bfd_link_hash_defweak)
2227             /* This appears to be a reference to an undefined
2228                symbol.  Just ignore it--it will be caught by the
2229                regular reloc processing.  */
2230             continue;
2231
2232           symval = (h->root.u.def.value
2233                     + h->root.u.def.section->output_section->vma
2234                     + h->root.u.def.section->output_offset);
2235         }
2236
2237       /* For simplicity of coding, we are going to modify the section
2238          contents, the section relocs, and the BFD symbol table.  We
2239          must tell the rest of the code not to free up this
2240          information.  It would be possible to instead create a table
2241          of changes which have to be made, as is done in coff-mips.c;
2242          that would be more work, but would require less memory when
2243          the linker is run.  */
2244       switch (ELF32_R_TYPE (irel->r_info))
2245         {
2246           /* Try to turn a 22-bit absolute call/jump into an 13-bit
2247              pc-relative rcall/rjmp.  */
2248         case R_AVR_CALL:
2249           {
2250             bfd_vma value = symval + irel->r_addend;
2251             bfd_vma dot, gap;
2252             int distance_short_enough = 0;
2253
2254             /* Get the address of this instruction.  */
2255             dot = (sec->output_section->vma
2256                    + sec->output_offset + irel->r_offset);
2257
2258             /* Compute the distance from this insn to the branch target.  */
2259             gap = value - dot;
2260
2261             /* Check if the gap falls in the range that can be accommodated
2262                in 13bits signed (It is 12bits when encoded, as we deal with
2263                word addressing). */
2264             if (!shrinkable && ((int) gap >= -4096 && (int) gap <= 4095))
2265               distance_short_enough = 1;
2266             /* If shrinkable, then we can check for a range of distance which
2267                is two bytes farther on both the directions because the call
2268                or jump target will be closer by two bytes after the
2269                relaxation. */
2270             else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4097))
2271               distance_short_enough = 1;
2272
2273             /* Here we handle the wrap-around case.  E.g. for a 16k device
2274                we could use a rjmp to jump from address 0x100 to 0x3d00!
2275                In order to make this work properly, we need to fill the
2276                vaiable avr_pc_wrap_around with the appropriate value.
2277                I.e. 0x4000 for a 16k device.  */
2278             {
2279               /* Shrinking the code size makes the gaps larger in the
2280                  case of wrap-arounds.  So we use a heuristical safety
2281                  margin to avoid that during relax the distance gets
2282                  again too large for the short jumps.  Let's assume
2283                  a typical code-size reduction due to relax for a
2284                  16k device of 600 bytes.  So let's use twice the
2285                  typical value as safety margin.  */
2286               int rgap;
2287               int safety_margin;
2288
2289               int assumed_shrink = 600;
2290               if (avr_pc_wrap_around > 0x4000)
2291                 assumed_shrink = 900;
2292
2293               safety_margin = 2 * assumed_shrink;
2294
2295               rgap = avr_relative_distance_considering_wrap_around (gap);
2296
2297               if (rgap >= (-4092 + safety_margin)
2298                   && rgap <= (4094 - safety_margin))
2299                 distance_short_enough = 1;
2300             }
2301
2302             if (distance_short_enough)
2303               {
2304                 unsigned char code_msb;
2305                 unsigned char code_lsb;
2306
2307                 if (debug_relax)
2308                   printf ("shrinking jump/call instruction at address 0x%x"
2309                           " in section %s\n\n",
2310                           (int) dot, sec->name);
2311
2312                 /* Note that we've changed the relocs, section contents,
2313                    etc.  */
2314                 elf_section_data (sec)->relocs = internal_relocs;
2315                 elf_section_data (sec)->this_hdr.contents = contents;
2316                 symtab_hdr->contents = (unsigned char *) isymbuf;
2317
2318                 /* Get the instruction code for relaxing.  */
2319                 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
2320                 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2321
2322                 /* Mask out the relocation bits.  */
2323                 code_msb &= 0x94;
2324                 code_lsb &= 0x0E;
2325                 if (code_msb == 0x94 && code_lsb == 0x0E)
2326                   {
2327                     /* we are changing call -> rcall .  */
2328                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
2329                     bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
2330                   }
2331                 else if (code_msb == 0x94 && code_lsb == 0x0C)
2332                   {
2333                     /* we are changeing jump -> rjmp.  */
2334                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
2335                     bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
2336                   }
2337                 else
2338                   abort ();
2339
2340                 /* Fix the relocation's type.  */
2341                 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2342                                              R_AVR_13_PCREL);
2343
2344                 /* We should not modify the ordering if 'shrinkable' is
2345                    FALSE. */
2346                 if (!shrinkable)
2347                   {
2348                     /* Let's insert a nop.  */
2349                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
2350                     bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
2351                   }
2352                 else
2353                   {
2354                     /* Delete two bytes of data.  */
2355                     if (!elf32_avr_relax_delete_bytes (abfd, sec,
2356                                                        irel->r_offset + 2, 2))
2357                       goto error_return;
2358
2359                     /* That will change things, so, we should relax again.
2360                        Note that this is not required, and it may be slow.  */
2361                     *again = TRUE;
2362                   }
2363               }
2364           }
2365
2366         default:
2367           {
2368             unsigned char code_msb;
2369             unsigned char code_lsb;
2370             bfd_vma dot;
2371
2372             code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2373             code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
2374
2375             /* Get the address of this instruction.  */
2376             dot = (sec->output_section->vma
2377                    + sec->output_offset + irel->r_offset);
2378
2379             /* Here we look for rcall/ret or call/ret sequences that could be
2380                safely replaced by rjmp/ret or jmp/ret.  */
2381             if (((code_msb & 0xf0) == 0xd0)
2382                 && avr_replace_call_ret_sequences)
2383               {
2384                 /* This insn is a rcall.  */
2385                 unsigned char next_insn_msb = 0;
2386                 unsigned char next_insn_lsb = 0;
2387
2388                 if (irel->r_offset + 3 < sec->size)
2389                   {
2390                     next_insn_msb =
2391                       bfd_get_8 (abfd, contents + irel->r_offset + 3);
2392                     next_insn_lsb =
2393                       bfd_get_8 (abfd, contents + irel->r_offset + 2);
2394                   }
2395
2396                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2397                   {
2398                     /* The next insn is a ret. We now convert the rcall insn
2399                        into a rjmp instruction.  */
2400                     code_msb &= 0xef;
2401                     bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
2402                     if (debug_relax)
2403                       printf ("converted rcall/ret sequence at address 0x%x"
2404                               " into rjmp/ret sequence. Section is %s\n\n",
2405                               (int) dot, sec->name);
2406                     *again = TRUE;
2407                     break;
2408                   }
2409               }
2410             else if ((0x94 == (code_msb & 0xfe))
2411                      && (0x0e == (code_lsb & 0x0e))
2412                      && avr_replace_call_ret_sequences)
2413               {
2414                 /* This insn is a call.  */
2415                 unsigned char next_insn_msb = 0;
2416                 unsigned char next_insn_lsb = 0;
2417
2418                 if (irel->r_offset + 5 < sec->size)
2419                   {
2420                     next_insn_msb =
2421                       bfd_get_8 (abfd, contents + irel->r_offset + 5);
2422                     next_insn_lsb =
2423                       bfd_get_8 (abfd, contents + irel->r_offset + 4);
2424                   }
2425
2426                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2427                   {
2428                     /* The next insn is a ret. We now convert the call insn
2429                        into a jmp instruction.  */
2430
2431                     code_lsb &= 0xfd;
2432                     bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
2433                     if (debug_relax)
2434                       printf ("converted call/ret sequence at address 0x%x"
2435                               " into jmp/ret sequence. Section is %s\n\n",
2436                               (int) dot, sec->name);
2437                     *again = TRUE;
2438                     break;
2439                   }
2440               }
2441             else if ((0xc0 == (code_msb & 0xf0))
2442                      || ((0x94 == (code_msb & 0xfe))
2443                          && (0x0c == (code_lsb & 0x0e))))
2444               {
2445                 /* This insn is a rjmp or a jmp.  */
2446                 unsigned char next_insn_msb = 0;
2447                 unsigned char next_insn_lsb = 0;
2448                 int insn_size;
2449
2450                 if (0xc0 == (code_msb & 0xf0))
2451                   insn_size = 2; /* rjmp insn */
2452                 else
2453                   insn_size = 4; /* jmp insn */
2454
2455                 if (irel->r_offset + insn_size + 1 < sec->size)
2456                   {
2457                     next_insn_msb =
2458                       bfd_get_8 (abfd, contents + irel->r_offset
2459                                  + insn_size + 1);
2460                     next_insn_lsb =
2461                       bfd_get_8 (abfd, contents + irel->r_offset
2462                                  + insn_size);
2463                   }
2464
2465                 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2466                   {
2467                     /* The next insn is a ret. We possibly could delete
2468                        this ret. First we need to check for preceding
2469                        sbis/sbic/sbrs or cpse "skip" instructions.  */
2470
2471                     int there_is_preceding_non_skip_insn = 1;
2472                     bfd_vma address_of_ret;
2473
2474                     address_of_ret = dot + insn_size;
2475
2476                     if (debug_relax && (insn_size == 2))
2477                       printf ("found rjmp / ret sequence at address 0x%x\n",
2478                               (int) dot);
2479                     if (debug_relax && (insn_size == 4))
2480                       printf ("found jmp / ret sequence at address 0x%x\n",
2481                               (int) dot);
2482
2483                     /* We have to make sure that there is a preceding insn.  */
2484                     if (irel->r_offset >= 2)
2485                       {
2486                         unsigned char preceding_msb;
2487                         unsigned char preceding_lsb;
2488
2489                         preceding_msb =
2490                           bfd_get_8 (abfd, contents + irel->r_offset - 1);
2491                         preceding_lsb =
2492                           bfd_get_8 (abfd, contents + irel->r_offset - 2);
2493
2494                         /* sbic.  */
2495                         if (0x99 == preceding_msb)
2496                           there_is_preceding_non_skip_insn = 0;
2497
2498                         /* sbis.  */
2499                         if (0x9b == preceding_msb)
2500                           there_is_preceding_non_skip_insn = 0;
2501
2502                         /* sbrc */
2503                         if ((0xfc == (preceding_msb & 0xfe)
2504                              && (0x00 == (preceding_lsb & 0x08))))
2505                           there_is_preceding_non_skip_insn = 0;
2506
2507                         /* sbrs */
2508                         if ((0xfe == (preceding_msb & 0xfe)
2509                              && (0x00 == (preceding_lsb & 0x08))))
2510                           there_is_preceding_non_skip_insn = 0;
2511
2512                         /* cpse */
2513                         if (0x10 == (preceding_msb & 0xfc))
2514                           there_is_preceding_non_skip_insn = 0;
2515
2516                         if (there_is_preceding_non_skip_insn == 0)
2517                           if (debug_relax)
2518                             printf ("preceding skip insn prevents deletion of"
2519                                     " ret insn at Addy 0x%x in section %s\n",
2520                                     (int) dot + 2, sec->name);
2521                       }
2522                     else
2523                       {
2524                         /* There is no previous instruction.  */
2525                         there_is_preceding_non_skip_insn = 0;
2526                       }
2527
2528                     if (there_is_preceding_non_skip_insn)
2529                       {
2530                         /* We now only have to make sure that there is no
2531                            local label defined at the address of the ret
2532                            instruction and that there is no local relocation
2533                            in this section pointing to the ret.  */
2534
2535                         int deleting_ret_is_safe = 1;
2536                         unsigned int section_offset_of_ret_insn =
2537                           irel->r_offset + insn_size;
2538                         Elf_Internal_Sym *isym, *isymend;
2539                         unsigned int sec_shndx;
2540                         struct bfd_section *isec;
2541
2542                         sec_shndx =
2543                           _bfd_elf_section_from_bfd_section (abfd, sec);
2544
2545                         /* Check for local symbols.  */
2546                         isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2547                         isymend = isym + symtab_hdr->sh_info;
2548                         /* PR 6019: There may not be any local symbols.  */
2549                         for (; isym != NULL && isym < isymend; isym++)
2550                           {
2551                             if (isym->st_value == section_offset_of_ret_insn
2552                                 && isym->st_shndx == sec_shndx)
2553                               {
2554                                 deleting_ret_is_safe = 0;
2555                                 if (debug_relax)
2556                                   printf ("local label prevents deletion of ret "
2557                                           "insn at address 0x%x\n",
2558                                           (int) dot + insn_size);
2559                               }
2560                           }
2561
2562                         /* Now check for global symbols.  */
2563                         {
2564                           int symcount;
2565                           struct elf_link_hash_entry **sym_hashes;
2566                           struct elf_link_hash_entry **end_hashes;
2567
2568                           symcount = (symtab_hdr->sh_size
2569                                       / sizeof (Elf32_External_Sym)
2570                                       - symtab_hdr->sh_info);
2571                           sym_hashes = elf_sym_hashes (abfd);
2572                           end_hashes = sym_hashes + symcount;
2573                           for (; sym_hashes < end_hashes; sym_hashes++)
2574                             {
2575                               struct elf_link_hash_entry *sym_hash =
2576                                 *sym_hashes;
2577                               if ((sym_hash->root.type == bfd_link_hash_defined
2578                                    || sym_hash->root.type ==
2579                                    bfd_link_hash_defweak)
2580                                   && sym_hash->root.u.def.section == sec
2581                                   && sym_hash->root.u.def.value == section_offset_of_ret_insn)
2582                                 {
2583                                   deleting_ret_is_safe = 0;
2584                                   if (debug_relax)
2585                                     printf ("global label prevents deletion of "
2586                                             "ret insn at address 0x%x\n",
2587                                             (int) dot + insn_size);
2588                                 }
2589                             }
2590                         }
2591
2592                         /* Now we check for relocations pointing to ret.  */
2593                         for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next)
2594                           {
2595                             Elf_Internal_Rela *rel;
2596                             Elf_Internal_Rela *relend;
2597
2598                             rel = elf_section_data (isec)->relocs;
2599                             if (rel == NULL)
2600                               rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
2601
2602                             relend = rel + isec->reloc_count;
2603
2604                             for (; rel && rel < relend; rel++)
2605                               {
2606                                 bfd_vma reloc_target = 0;
2607
2608                                 /* Read this BFD's local symbols if we haven't
2609                                    done so already.  */
2610                                 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2611                                   {
2612                                     isymbuf = (Elf_Internal_Sym *)
2613                                       symtab_hdr->contents;
2614                                     if (isymbuf == NULL)
2615                                       isymbuf = bfd_elf_get_elf_syms
2616                                         (abfd,
2617                                          symtab_hdr,
2618                                          symtab_hdr->sh_info, 0,
2619                                          NULL, NULL, NULL);
2620                                     if (isymbuf == NULL)
2621                                       break;
2622                                   }
2623
2624                                 /* Get the value of the symbol referred to
2625                                    by the reloc.  */
2626                                 if (ELF32_R_SYM (rel->r_info)
2627                                     < symtab_hdr->sh_info)
2628                                   {
2629                                     /* A local symbol.  */
2630                                     asection *sym_sec;
2631
2632                                     isym = isymbuf
2633                                       + ELF32_R_SYM (rel->r_info);
2634                                     sym_sec = bfd_section_from_elf_index
2635                                       (abfd, isym->st_shndx);
2636                                     symval = isym->st_value;
2637
2638                                     /* If the reloc is absolute, it will not
2639                                        have a symbol or section associated
2640                                        with it.  */
2641
2642                                     if (sym_sec)
2643                                       {
2644                                         symval +=
2645                                           sym_sec->output_section->vma
2646                                           + sym_sec->output_offset;
2647                                         reloc_target = symval + rel->r_addend;
2648                                       }
2649                                     else
2650                                       {
2651                                         reloc_target = symval + rel->r_addend;
2652                                         /* Reference symbol is absolute.  */
2653                                       }
2654                                   }
2655                                 /* else ... reference symbol is extern.  */
2656
2657                                 if (address_of_ret == reloc_target)
2658                                   {
2659                                     deleting_ret_is_safe = 0;
2660                                     if (debug_relax)
2661                                       printf ("ret from "
2662                                               "rjmp/jmp ret sequence at address"
2663                                               " 0x%x could not be deleted. ret"
2664                                               " is target of a relocation.\n",
2665                                               (int) address_of_ret);
2666                                     break;
2667                                   }
2668                               }
2669                           }
2670
2671                         if (deleting_ret_is_safe)
2672                           {
2673                             if (debug_relax)
2674                               printf ("unreachable ret instruction "
2675                                       "at address 0x%x deleted.\n",
2676                                       (int) dot + insn_size);
2677
2678                             /* Delete two bytes of data.  */
2679                             if (!elf32_avr_relax_delete_bytes (abfd, sec,
2680                                                                irel->r_offset + insn_size, 2))
2681                               goto error_return;
2682
2683                             /* That will change things, so, we should relax
2684                                again. Note that this is not required, and it
2685                                may be slow.  */
2686                             *again = TRUE;
2687                             break;
2688                           }
2689                       }
2690                   }
2691               }
2692             break;
2693           }
2694         }
2695     }
2696
2697   if (contents != NULL
2698       && elf_section_data (sec)->this_hdr.contents != contents)
2699     {
2700       if (! link_info->keep_memory)
2701         free (contents);
2702       else
2703         {
2704           /* Cache the section contents for elf_link_input_bfd.  */
2705           elf_section_data (sec)->this_hdr.contents = contents;
2706         }
2707     }
2708
2709   if (internal_relocs != NULL
2710       && elf_section_data (sec)->relocs != internal_relocs)
2711     free (internal_relocs);
2712
2713   return TRUE;
2714
2715  error_return:
2716   if (isymbuf != NULL
2717       && symtab_hdr->contents != (unsigned char *) isymbuf)
2718     free (isymbuf);
2719   if (contents != NULL
2720       && elf_section_data (sec)->this_hdr.contents != contents)
2721     free (contents);
2722   if (internal_relocs != NULL
2723       && elf_section_data (sec)->relocs != internal_relocs)
2724     free (internal_relocs);
2725
2726   return FALSE;
2727 }
2728
2729 /* This is a version of bfd_generic_get_relocated_section_contents
2730    which uses elf32_avr_relocate_section.
2731
2732    For avr it's essentially a cut and paste taken from the H8300 port.
2733    The author of the relaxation support patch for avr had absolutely no
2734    clue what is happening here but found out that this part of the code
2735    seems to be important.  */
2736
2737 static bfd_byte *
2738 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
2739                                           struct bfd_link_info *link_info,
2740                                           struct bfd_link_order *link_order,
2741                                           bfd_byte *data,
2742                                           bfd_boolean relocatable,
2743                                           asymbol **symbols)
2744 {
2745   Elf_Internal_Shdr *symtab_hdr;
2746   asection *input_section = link_order->u.indirect.section;
2747   bfd *input_bfd = input_section->owner;
2748   asection **sections = NULL;
2749   Elf_Internal_Rela *internal_relocs = NULL;
2750   Elf_Internal_Sym *isymbuf = NULL;
2751
2752   /* We only need to handle the case of relaxing, or of having a
2753      particular set of section contents, specially.  */
2754   if (relocatable
2755       || elf_section_data (input_section)->this_hdr.contents == NULL)
2756     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2757                                                        link_order, data,
2758                                                        relocatable,
2759                                                        symbols);
2760   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2761
2762   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2763           (size_t) input_section->size);
2764
2765   if ((input_section->flags & SEC_RELOC) != 0
2766       && input_section->reloc_count > 0)
2767     {
2768       asection **secpp;
2769       Elf_Internal_Sym *isym, *isymend;
2770       bfd_size_type amt;
2771
2772       internal_relocs = (_bfd_elf_link_read_relocs
2773                          (input_bfd, input_section, NULL, NULL, FALSE));
2774       if (internal_relocs == NULL)
2775         goto error_return;
2776
2777       if (symtab_hdr->sh_info != 0)
2778         {
2779           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2780           if (isymbuf == NULL)
2781             isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2782                                             symtab_hdr->sh_info, 0,
2783                                             NULL, NULL, NULL);
2784           if (isymbuf == NULL)
2785             goto error_return;
2786         }
2787
2788       amt = symtab_hdr->sh_info;
2789       amt *= sizeof (asection *);
2790       sections = bfd_malloc (amt);
2791       if (sections == NULL && amt != 0)
2792         goto error_return;
2793
2794       isymend = isymbuf + symtab_hdr->sh_info;
2795       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
2796         {
2797           asection *isec;
2798
2799           if (isym->st_shndx == SHN_UNDEF)
2800             isec = bfd_und_section_ptr;
2801           else if (isym->st_shndx == SHN_ABS)
2802             isec = bfd_abs_section_ptr;
2803           else if (isym->st_shndx == SHN_COMMON)
2804             isec = bfd_com_section_ptr;
2805           else
2806             isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2807
2808           *secpp = isec;
2809         }
2810
2811       if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
2812                                         input_section, data, internal_relocs,
2813                                         isymbuf, sections))
2814         goto error_return;
2815
2816       if (sections != NULL)
2817         free (sections);
2818       if (isymbuf != NULL
2819           && symtab_hdr->contents != (unsigned char *) isymbuf)
2820         free (isymbuf);
2821       if (elf_section_data (input_section)->relocs != internal_relocs)
2822         free (internal_relocs);
2823     }
2824
2825   return data;
2826
2827  error_return:
2828   if (sections != NULL)
2829     free (sections);
2830   if (isymbuf != NULL
2831       && symtab_hdr->contents != (unsigned char *) isymbuf)
2832     free (isymbuf);
2833   if (internal_relocs != NULL
2834       && elf_section_data (input_section)->relocs != internal_relocs)
2835     free (internal_relocs);
2836   return NULL;
2837 }
2838
2839
2840 /* Determines the hash entry name for a particular reloc. It consists of
2841    the identifier of the symbol section and the added reloc addend and
2842    symbol offset relative to the section the symbol is attached to.  */
2843
2844 static char *
2845 avr_stub_name (const asection *symbol_section,
2846                const bfd_vma symbol_offset,
2847                const Elf_Internal_Rela *rela)
2848 {
2849   char *stub_name;
2850   bfd_size_type len;
2851
2852   len = 8 + 1 + 8 + 1 + 1;
2853   stub_name = bfd_malloc (len);
2854
2855   sprintf (stub_name, "%08x+%08x",
2856            symbol_section->id & 0xffffffff,
2857            (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
2858
2859   return stub_name;
2860 }
2861
2862
2863 /* Add a new stub entry to the stub hash.  Not all fields of the new
2864    stub entry are initialised.  */
2865
2866 static struct elf32_avr_stub_hash_entry *
2867 avr_add_stub (const char *stub_name,
2868               struct elf32_avr_link_hash_table *htab)
2869 {
2870   struct elf32_avr_stub_hash_entry *hsh;
2871
2872   /* Enter this entry into the linker stub hash table.  */
2873   hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
2874
2875   if (hsh == NULL)
2876     {
2877       (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
2878                              NULL, stub_name);
2879       return NULL;
2880     }
2881
2882   hsh->stub_offset = 0;
2883   return hsh;
2884 }
2885
2886 /* We assume that there is already space allocated for the stub section
2887    contents and that before building the stubs the section size is
2888    initialized to 0.  We assume that within the stub hash table entry,
2889    the absolute position of the jmp target has been written in the
2890    target_value field.  We write here the offset of the generated jmp insn
2891    relative to the trampoline section start to the stub_offset entry in
2892    the stub hash table entry.  */
2893
2894 static  bfd_boolean
2895 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2896 {
2897   struct elf32_avr_stub_hash_entry *hsh;
2898   struct bfd_link_info *info;
2899   struct elf32_avr_link_hash_table *htab;
2900   bfd *stub_bfd;
2901   bfd_byte *loc;
2902   bfd_vma target;
2903   bfd_vma starget;
2904
2905   /* Basic opcode */
2906   bfd_vma jmp_insn = 0x0000940c;
2907
2908   /* Massage our args to the form they really have.  */
2909   hsh = avr_stub_hash_entry (bh);
2910
2911   if (!hsh->is_actually_needed)
2912     return TRUE;
2913
2914   info = (struct bfd_link_info *) in_arg;
2915
2916   htab = avr_link_hash_table (info);
2917   if (htab == NULL)
2918     return FALSE;
2919
2920   target = hsh->target_value;
2921
2922   /* Make a note of the offset within the stubs for this entry.  */
2923   hsh->stub_offset = htab->stub_sec->size;
2924   loc = htab->stub_sec->contents + hsh->stub_offset;
2925
2926   stub_bfd = htab->stub_sec->owner;
2927
2928   if (debug_stubs)
2929     printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
2930              (unsigned int) target,
2931              (unsigned int) hsh->stub_offset);
2932
2933   /* We now have to add the information on the jump target to the bare
2934      opcode bits already set in jmp_insn.  */
2935
2936   /* Check for the alignment of the address.  */
2937   if (target & 1)
2938      return FALSE;
2939
2940   starget = target >> 1;
2941   jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
2942   bfd_put_16 (stub_bfd, jmp_insn, loc);
2943   bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
2944
2945   htab->stub_sec->size += 4;
2946
2947   /* Now add the entries in the address mapping table if there is still
2948      space left.  */
2949   {
2950     unsigned int nr;
2951
2952     nr = htab->amt_entry_cnt + 1;
2953     if (nr <= htab->amt_max_entry_cnt)
2954       {
2955         htab->amt_entry_cnt = nr;
2956
2957         htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
2958         htab->amt_destination_addr[nr - 1] = target;
2959       }
2960   }
2961
2962   return TRUE;
2963 }
2964
2965 static bfd_boolean
2966 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
2967                                    void *in_arg ATTRIBUTE_UNUSED)
2968 {
2969   struct elf32_avr_stub_hash_entry *hsh;
2970
2971   hsh = avr_stub_hash_entry (bh);
2972   hsh->is_actually_needed = FALSE;
2973
2974   return TRUE;
2975 }
2976
2977 static bfd_boolean
2978 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2979 {
2980   struct elf32_avr_stub_hash_entry *hsh;
2981   struct elf32_avr_link_hash_table *htab;
2982   int size;
2983
2984   /* Massage our args to the form they really have.  */
2985   hsh = avr_stub_hash_entry (bh);
2986   htab = in_arg;
2987
2988   if (hsh->is_actually_needed)
2989     size = 4;
2990   else
2991     size = 0;
2992
2993   htab->stub_sec->size += size;
2994   return TRUE;
2995 }
2996
2997 void
2998 elf32_avr_setup_params (struct bfd_link_info *info,
2999                         bfd *avr_stub_bfd,
3000                         asection *avr_stub_section,
3001                         bfd_boolean no_stubs,
3002                         bfd_boolean deb_stubs,
3003                         bfd_boolean deb_relax,
3004                         bfd_vma pc_wrap_around,
3005                         bfd_boolean call_ret_replacement)
3006 {
3007   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3008
3009   if (htab == NULL)
3010     return;
3011   htab->stub_sec = avr_stub_section;
3012   htab->stub_bfd = avr_stub_bfd;
3013   htab->no_stubs = no_stubs;
3014
3015   debug_relax = deb_relax;
3016   debug_stubs = deb_stubs;
3017   avr_pc_wrap_around = pc_wrap_around;
3018   avr_replace_call_ret_sequences = call_ret_replacement;
3019 }
3020
3021
3022 /* Set up various things so that we can make a list of input sections
3023    for each output section included in the link.  Returns -1 on error,
3024    0 when no stubs will be needed, and 1 on success.  It also sets
3025    information on the stubs bfd and the stub section in the info
3026    struct.  */
3027
3028 int
3029 elf32_avr_setup_section_lists (bfd *output_bfd,
3030                                struct bfd_link_info *info)
3031 {
3032   bfd *input_bfd;
3033   unsigned int bfd_count;
3034   int top_id, top_index;
3035   asection *section;
3036   asection **input_list, **list;
3037   bfd_size_type amt;
3038   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3039
3040   if (htab == NULL || htab->no_stubs)
3041     return 0;
3042
3043   /* Count the number of input BFDs and find the top input section id.  */
3044   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3045        input_bfd != NULL;
3046        input_bfd = input_bfd->link.next)
3047     {
3048       bfd_count += 1;
3049       for (section = input_bfd->sections;
3050            section != NULL;
3051            section = section->next)
3052         if (top_id < section->id)
3053           top_id = section->id;
3054     }
3055
3056   htab->bfd_count = bfd_count;
3057
3058   /* We can't use output_bfd->section_count here to find the top output
3059      section index as some sections may have been removed, and
3060      strip_excluded_output_sections doesn't renumber the indices.  */
3061   for (section = output_bfd->sections, top_index = 0;
3062        section != NULL;
3063        section = section->next)
3064     if (top_index < section->index)
3065       top_index = section->index;
3066
3067   htab->top_index = top_index;
3068   amt = sizeof (asection *) * (top_index + 1);
3069   input_list = bfd_malloc (amt);
3070   htab->input_list = input_list;
3071   if (input_list == NULL)
3072     return -1;
3073
3074   /* For sections we aren't interested in, mark their entries with a
3075      value we can check later.  */
3076   list = input_list + top_index;
3077   do
3078     *list = bfd_abs_section_ptr;
3079   while (list-- != input_list);
3080
3081   for (section = output_bfd->sections;
3082        section != NULL;
3083        section = section->next)
3084     if ((section->flags & SEC_CODE) != 0)
3085       input_list[section->index] = NULL;
3086
3087   return 1;
3088 }
3089
3090
3091 /* Read in all local syms for all input bfds, and create hash entries
3092    for export stubs if we are building a multi-subspace shared lib.
3093    Returns -1 on error, 0 otherwise.  */
3094
3095 static int
3096 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
3097 {
3098   unsigned int bfd_indx;
3099   Elf_Internal_Sym *local_syms, **all_local_syms;
3100   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3101   bfd_size_type amt;
3102
3103   if (htab == NULL)
3104     return -1;
3105
3106   /* We want to read in symbol extension records only once.  To do this
3107      we need to read in the local symbols in parallel and save them for
3108      later use; so hold pointers to the local symbols in an array.  */
3109   amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3110   all_local_syms = bfd_zmalloc (amt);
3111   htab->all_local_syms = all_local_syms;
3112   if (all_local_syms == NULL)
3113     return -1;
3114
3115   /* Walk over all the input BFDs, swapping in local symbols.
3116      If we are creating a shared library, create hash entries for the
3117      export stubs.  */
3118   for (bfd_indx = 0;
3119        input_bfd != NULL;
3120        input_bfd = input_bfd->link.next, bfd_indx++)
3121     {
3122       Elf_Internal_Shdr *symtab_hdr;
3123
3124       /* We'll need the symbol table in a second.  */
3125       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3126       if (symtab_hdr->sh_info == 0)
3127         continue;
3128
3129       /* We need an array of the local symbols attached to the input bfd.  */
3130       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3131       if (local_syms == NULL)
3132         {
3133           local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3134                                              symtab_hdr->sh_info, 0,
3135                                              NULL, NULL, NULL);
3136           /* Cache them for elf_link_input_bfd.  */
3137           symtab_hdr->contents = (unsigned char *) local_syms;
3138         }
3139       if (local_syms == NULL)
3140         return -1;
3141
3142       all_local_syms[bfd_indx] = local_syms;
3143     }
3144
3145   return 0;
3146 }
3147
3148 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
3149
3150 bfd_boolean
3151 elf32_avr_size_stubs (bfd *output_bfd,
3152                       struct bfd_link_info *info,
3153                       bfd_boolean is_prealloc_run)
3154 {
3155   struct elf32_avr_link_hash_table *htab;
3156   int stub_changed = 0;
3157
3158   htab = avr_link_hash_table (info);
3159   if (htab == NULL)
3160     return FALSE;
3161
3162   /* At this point we initialize htab->vector_base
3163      To the start of the text output section.  */
3164   htab->vector_base = htab->stub_sec->output_section->vma;
3165
3166   if (get_local_syms (info->input_bfds, info))
3167     {
3168       if (htab->all_local_syms)
3169         goto error_ret_free_local;
3170       return FALSE;
3171     }
3172
3173   if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
3174     {
3175       struct elf32_avr_stub_hash_entry *test;
3176
3177       test = avr_add_stub ("Hugo",htab);
3178       test->target_value = 0x123456;
3179       test->stub_offset = 13;
3180
3181       test = avr_add_stub ("Hugo2",htab);
3182       test->target_value = 0x84210;
3183       test->stub_offset = 14;
3184     }
3185
3186   while (1)
3187     {
3188       bfd *input_bfd;
3189       unsigned int bfd_indx;
3190
3191       /* We will have to re-generate the stub hash table each time anything
3192          in memory has changed.  */
3193
3194       bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
3195       for (input_bfd = info->input_bfds, bfd_indx = 0;
3196            input_bfd != NULL;
3197            input_bfd = input_bfd->link.next, bfd_indx++)
3198         {
3199           Elf_Internal_Shdr *symtab_hdr;
3200           asection *section;
3201           Elf_Internal_Sym *local_syms;
3202
3203           /* We'll need the symbol table in a second.  */
3204           symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3205           if (symtab_hdr->sh_info == 0)
3206             continue;
3207
3208           local_syms = htab->all_local_syms[bfd_indx];
3209
3210           /* Walk over each section attached to the input bfd.  */
3211           for (section = input_bfd->sections;
3212                section != NULL;
3213                section = section->next)
3214             {
3215               Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3216
3217               /* If there aren't any relocs, then there's nothing more
3218                  to do.  */
3219               if ((section->flags & SEC_RELOC) == 0
3220                   || section->reloc_count == 0)
3221                 continue;
3222
3223               /* If this section is a link-once section that will be
3224                  discarded, then don't create any stubs.  */
3225               if (section->output_section == NULL
3226                   || section->output_section->owner != output_bfd)
3227                 continue;
3228
3229               /* Get the relocs.  */
3230               internal_relocs
3231                 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
3232                                              info->keep_memory);
3233               if (internal_relocs == NULL)
3234                 goto error_ret_free_local;
3235
3236               /* Now examine each relocation.  */
3237               irela = internal_relocs;
3238               irelaend = irela + section->reloc_count;
3239               for (; irela < irelaend; irela++)
3240                 {
3241                   unsigned int r_type, r_indx;
3242                   struct elf32_avr_stub_hash_entry *hsh;
3243                   asection *sym_sec;
3244                   bfd_vma sym_value;
3245                   bfd_vma destination;
3246                   struct elf_link_hash_entry *hh;
3247                   char *stub_name;
3248
3249                   r_type = ELF32_R_TYPE (irela->r_info);
3250                   r_indx = ELF32_R_SYM (irela->r_info);
3251
3252                   /* Only look for 16 bit GS relocs. No other reloc will need a
3253                      stub.  */
3254                   if (!((r_type == R_AVR_16_PM)
3255                         || (r_type == R_AVR_LO8_LDI_GS)
3256                         || (r_type == R_AVR_HI8_LDI_GS)))
3257                     continue;
3258
3259                   /* Now determine the call target, its name, value,
3260                      section.  */
3261                   sym_sec = NULL;
3262                   sym_value = 0;
3263                   destination = 0;
3264                   hh = NULL;
3265                   if (r_indx < symtab_hdr->sh_info)
3266                     {
3267                       /* It's a local symbol.  */
3268                       Elf_Internal_Sym *sym;
3269                       Elf_Internal_Shdr *hdr;
3270                       unsigned int shndx;
3271
3272                       sym = local_syms + r_indx;
3273                       if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3274                         sym_value = sym->st_value;
3275                       shndx = sym->st_shndx;
3276                       if (shndx < elf_numsections (input_bfd))
3277                         {
3278                           hdr = elf_elfsections (input_bfd)[shndx];
3279                           sym_sec = hdr->bfd_section;
3280                           destination = (sym_value + irela->r_addend
3281                                          + sym_sec->output_offset
3282                                          + sym_sec->output_section->vma);
3283                         }
3284                     }
3285                   else
3286                     {
3287                       /* It's an external symbol.  */
3288                       int e_indx;
3289
3290                       e_indx = r_indx - symtab_hdr->sh_info;
3291                       hh = elf_sym_hashes (input_bfd)[e_indx];
3292
3293                       while (hh->root.type == bfd_link_hash_indirect
3294                              || hh->root.type == bfd_link_hash_warning)
3295                         hh = (struct elf_link_hash_entry *)
3296                               (hh->root.u.i.link);
3297
3298                       if (hh->root.type == bfd_link_hash_defined
3299                           || hh->root.type == bfd_link_hash_defweak)
3300                         {
3301                           sym_sec = hh->root.u.def.section;
3302                           sym_value = hh->root.u.def.value;
3303                           if (sym_sec->output_section != NULL)
3304                           destination = (sym_value + irela->r_addend
3305                                          + sym_sec->output_offset
3306                                          + sym_sec->output_section->vma);
3307                         }
3308                       else if (hh->root.type == bfd_link_hash_undefweak)
3309                         {
3310                           if (! info->shared)
3311                             continue;
3312                         }
3313                       else if (hh->root.type == bfd_link_hash_undefined)
3314                         {
3315                           if (! (info->unresolved_syms_in_objects == RM_IGNORE
3316                                  && (ELF_ST_VISIBILITY (hh->other)
3317                                      == STV_DEFAULT)))
3318                              continue;
3319                         }
3320                       else
3321                         {
3322                           bfd_set_error (bfd_error_bad_value);
3323
3324                           error_ret_free_internal:
3325                           if (elf_section_data (section)->relocs == NULL)
3326                             free (internal_relocs);
3327                           goto error_ret_free_local;
3328                         }
3329                     }
3330
3331                   if (! avr_stub_is_required_for_16_bit_reloc
3332                       (destination - htab->vector_base))
3333                     {
3334                       if (!is_prealloc_run)
3335                         /* We are having a reloc that does't need a stub.  */
3336                         continue;
3337
3338                       /* We don't right now know if a stub will be needed.
3339                          Let's rather be on the safe side.  */
3340                     }
3341
3342                   /* Get the name of this stub.  */
3343                   stub_name = avr_stub_name (sym_sec, sym_value, irela);
3344
3345                   if (!stub_name)
3346                     goto error_ret_free_internal;
3347
3348
3349                   hsh = avr_stub_hash_lookup (&htab->bstab,
3350                                               stub_name,
3351                                               FALSE, FALSE);
3352                   if (hsh != NULL)
3353                     {
3354                       /* The proper stub has already been created.  Mark it
3355                          to be used and write the possibly changed destination
3356                          value.  */
3357                       hsh->is_actually_needed = TRUE;
3358                       hsh->target_value = destination;
3359                       free (stub_name);
3360                       continue;
3361                     }
3362
3363                   hsh = avr_add_stub (stub_name, htab);
3364                   if (hsh == NULL)
3365                     {
3366                       free (stub_name);
3367                       goto error_ret_free_internal;
3368                     }
3369
3370                   hsh->is_actually_needed = TRUE;
3371                   hsh->target_value = destination;
3372
3373                   if (debug_stubs)
3374                     printf ("Adding stub with destination 0x%x to the"
3375                             " hash table.\n", (unsigned int) destination);
3376                   if (debug_stubs)
3377                     printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
3378
3379                   stub_changed = TRUE;
3380                 }
3381
3382               /* We're done with the internal relocs, free them.  */
3383               if (elf_section_data (section)->relocs == NULL)
3384                 free (internal_relocs);
3385             }
3386         }
3387
3388       /* Re-Calculate the number of needed stubs.  */
3389       htab->stub_sec->size = 0;
3390       bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
3391
3392       if (!stub_changed)
3393         break;
3394
3395       stub_changed = FALSE;
3396     }
3397
3398   free (htab->all_local_syms);
3399   return TRUE;
3400
3401  error_ret_free_local:
3402   free (htab->all_local_syms);
3403   return FALSE;
3404 }
3405
3406
3407 /* Build all the stubs associated with the current output file.  The
3408    stubs are kept in a hash table attached to the main linker hash
3409    table.  We also set up the .plt entries for statically linked PIC
3410    functions here.  This function is called via hppaelf_finish in the
3411    linker.  */
3412
3413 bfd_boolean
3414 elf32_avr_build_stubs (struct bfd_link_info *info)
3415 {
3416   asection *stub_sec;
3417   struct bfd_hash_table *table;
3418   struct elf32_avr_link_hash_table *htab;
3419   bfd_size_type total_size = 0;
3420
3421   htab = avr_link_hash_table (info);
3422   if (htab == NULL)
3423     return FALSE;
3424
3425   /* In case that there were several stub sections:  */
3426   for (stub_sec = htab->stub_bfd->sections;
3427        stub_sec != NULL;
3428        stub_sec = stub_sec->next)
3429     {
3430       bfd_size_type size;
3431
3432       /* Allocate memory to hold the linker stubs.  */
3433       size = stub_sec->size;
3434       total_size += size;
3435
3436       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3437       if (stub_sec->contents == NULL && size != 0)
3438         return FALSE;
3439       stub_sec->size = 0;
3440     }
3441
3442   /* Allocate memory for the adress mapping table.  */
3443   htab->amt_entry_cnt = 0;
3444   htab->amt_max_entry_cnt = total_size / 4;
3445   htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
3446                                        * htab->amt_max_entry_cnt);
3447   htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
3448                                            * htab->amt_max_entry_cnt );
3449
3450   if (debug_stubs)
3451     printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
3452
3453   /* Build the stubs as directed by the stub hash table.  */
3454   table = &htab->bstab;
3455   bfd_hash_traverse (table, avr_build_one_stub, info);
3456
3457   if (debug_stubs)
3458     printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
3459
3460   return TRUE;
3461 }
3462
3463 /* Callback used by QSORT to order relocations AP and BP.  */
3464
3465 static int
3466 internal_reloc_compare (const void *ap, const void *bp)
3467 {
3468   const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
3469   const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
3470
3471   if (a->r_offset != b->r_offset)
3472     return (a->r_offset - b->r_offset);
3473
3474   /* We don't need to sort on these criteria for correctness,
3475      but enforcing a more strict ordering prevents unstable qsort
3476      from behaving differently with different implementations.
3477      Without the code below we get correct but different results
3478      on Solaris 2.7 and 2.8.  We would like to always produce the
3479      same results no matter the host.  */
3480
3481   if (a->r_info != b->r_info)
3482     return (a->r_info - b->r_info);
3483
3484   return (a->r_addend - b->r_addend);
3485 }
3486
3487 /* Return true if ADDRESS is within the vma range of SECTION from ABFD.  */
3488
3489 static bfd_boolean
3490 avr_is_section_for_address (bfd *abfd, asection *section, bfd_vma address)
3491 {
3492   bfd_vma vma;
3493   bfd_size_type size;
3494
3495   vma = bfd_get_section_vma (abfd, section);
3496   if (address < vma)
3497     return FALSE;
3498
3499   size = section->size;
3500   if (address >= vma + size)
3501     return FALSE;
3502
3503   return TRUE;
3504 }
3505
3506 /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS.  */
3507
3508 struct avr_find_section_data
3509 {
3510   /* The address we're looking for.  */
3511   bfd_vma address;
3512
3513   /* The section we've found.  */
3514   asection *section;
3515 };
3516
3517 /* Helper function to locate the section holding a certain virtual memory
3518    address.  This is called via bfd_map_over_sections.  The DATA is an
3519    instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which
3520    has been set to the address to search for, and the section field has
3521    been set to NULL.  If SECTION from ABFD contains ADDRESS then the
3522    section field in DATA will be set to SECTION.  As an optimisation, if
3523    the section field is already non-null then this function does not
3524    perform any checks, and just returns.  */
3525
3526 static void
3527 avr_find_section_for_address (bfd *abfd,
3528                               asection *section, void *data)
3529 {
3530   struct avr_find_section_data *fs_data
3531     = (struct avr_find_section_data *) data;
3532
3533   /* Return if already found.  */
3534   if (fs_data->section != NULL)
3535     return;
3536
3537   /* If this section isn't part of the addressable code content, skip it.  */
3538   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0
3539       && (bfd_get_section_flags (abfd, section) & SEC_CODE) == 0)
3540     return;
3541
3542   if (avr_is_section_for_address (abfd, section, fs_data->address))
3543     fs_data->section = section;
3544 }
3545
3546 /* Load all of the property records from SEC, a section from ABFD.  Return
3547    a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records.  The
3548    memory for the returned structure, and all of the records pointed too by
3549    the structure are allocated with a single call to malloc, so, only the
3550    pointer returned needs to be free'd.  */
3551
3552 static struct avr_property_record_list *
3553 avr_elf32_load_records_from_section (bfd *abfd, asection *sec)
3554 {
3555   char *contents = NULL, *ptr;
3556   bfd_size_type size, mem_size;
3557   bfd_byte version, flags;
3558   uint16_t record_count, i;
3559   struct avr_property_record_list *r_list = NULL;
3560   Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end;
3561   struct avr_find_section_data fs_data;
3562
3563   fs_data.section = NULL;
3564
3565   size = bfd_get_section_size (sec);
3566   contents = bfd_malloc (size);
3567   bfd_get_section_contents (abfd, sec, contents, 0, size);
3568   ptr = contents;
3569
3570   /* Load the relocations for the '.avr.prop' section if there are any, and
3571      sort them.  */
3572   internal_relocs = (_bfd_elf_link_read_relocs
3573                      (abfd, sec, NULL, NULL, FALSE));
3574   if (internal_relocs)
3575     qsort (internal_relocs, sec->reloc_count,
3576            sizeof (Elf_Internal_Rela), internal_reloc_compare);
3577
3578   /* There is a header at the start of the property record section SEC, the
3579      format of this header is:
3580        uint8_t  : version number
3581        uint8_t  : flags
3582        uint16_t : record counter
3583   */
3584
3585   /* Check we have at least got a headers worth of bytes.  */
3586   if (size < AVR_PROPERTY_SECTION_HEADER_SIZE)
3587     goto load_failed;
3588
3589   version = *((bfd_byte *) ptr);
3590   ptr++;
3591   flags = *((bfd_byte *) ptr);
3592   ptr++;
3593   record_count = *((uint16_t *) ptr);
3594   ptr+=2;
3595   BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE);
3596
3597   /* Now allocate space for the list structure, and all of the list
3598      elements in a single block.  */
3599   mem_size = sizeof (struct avr_property_record_list)
3600     + sizeof (struct avr_property_record) * record_count;
3601   r_list = bfd_malloc (mem_size);
3602   if (r_list == NULL)
3603     goto load_failed;
3604
3605   r_list->version = version;
3606   r_list->flags = flags;
3607   r_list->section = sec;
3608   r_list->record_count = record_count;
3609   r_list->records = (struct avr_property_record *) (&r_list [1]);
3610   size -= AVR_PROPERTY_SECTION_HEADER_SIZE;
3611
3612   /* Check that we understand the version number.  There is only one
3613      version number right now, anything else is an error.  */
3614   if (r_list->version != AVR_PROPERTY_RECORDS_VERSION)
3615     goto load_failed;
3616
3617   rel = internal_relocs;
3618   rel_end = rel + sec->reloc_count;
3619   for (i = 0; i < record_count; ++i)
3620     {
3621       bfd_vma address;
3622
3623       /* Each entry is a 32-bit address, followed by a single byte type.
3624          After that is the type specific data.  We must take care to
3625          ensure that we don't read beyond the end of the section data.  */
3626       if (size < 5)
3627         goto load_failed;
3628
3629       r_list->records [i].section = NULL;
3630       r_list->records [i].offset = 0;
3631
3632       if (rel)
3633         {
3634           /* The offset of the address within the .avr.prop section.  */
3635           size_t offset = ptr - contents;
3636
3637           while (rel < rel_end && rel->r_offset < offset)
3638             ++rel;
3639
3640           if (rel == rel_end)
3641             rel = NULL;
3642           else if (rel->r_offset == offset)
3643             {
3644               /* Find section and section offset.  */
3645               unsigned long r_symndx;
3646
3647               asection * rel_sec;
3648               bfd_vma sec_offset;
3649
3650               r_symndx = ELF32_R_SYM (rel->r_info);
3651               rel_sec = get_elf_r_symndx_section (abfd, r_symndx);
3652               sec_offset = get_elf_r_symndx_offset (abfd, r_symndx)
3653                 + rel->r_addend;
3654
3655               r_list->records [i].section = rel_sec;
3656               r_list->records [i].offset = sec_offset;
3657             }
3658         }
3659
3660       address = *((uint32_t *) ptr);
3661       ptr += 4;
3662       size -= 4;
3663
3664       if (r_list->records [i].section == NULL)
3665         {
3666           /* Try to find section and offset from address.  */
3667           if (fs_data.section != NULL
3668               && !avr_is_section_for_address (abfd, fs_data.section,
3669                                               address))
3670             fs_data.section = NULL;
3671
3672           if (fs_data.section == NULL)
3673             {
3674               fs_data.address = address;
3675               bfd_map_over_sections (abfd, avr_find_section_for_address,
3676                                      &fs_data);
3677             }
3678
3679           if (fs_data.section == NULL)
3680             {
3681               fprintf (stderr, "Failed to find matching section.\n");
3682               goto load_failed;
3683             }
3684
3685           r_list->records [i].section = fs_data.section;
3686           r_list->records [i].offset
3687             = address - bfd_get_section_vma (abfd, fs_data.section);
3688         }
3689
3690       r_list->records [i].type = *((bfd_byte *) ptr);
3691       ptr += 1;
3692       size -= 1;
3693
3694       switch (r_list->records [i].type)
3695         {
3696         case RECORD_ORG:
3697           /* Nothing else to load.  */
3698           break;
3699         case RECORD_ORG_AND_FILL:
3700           /* Just a 4-byte fill to load.  */
3701           if (size < 4)
3702             goto load_failed;
3703           r_list->records [i].data.org.fill = *((uint32_t *) ptr);
3704           ptr += 4;
3705           size -= 4;
3706           break;
3707         case RECORD_ALIGN:
3708           /* Just a 4-byte alignment to load.  */
3709           if (size < 4)
3710             goto load_failed;
3711           r_list->records [i].data.align.bytes = *((uint32_t *) ptr);
3712           ptr += 4;
3713           size -= 4;
3714           /* Just initialise PRECEDING_DELETED field, this field is
3715              used during linker relaxation.  */
3716           r_list->records [i].data.align.preceding_deleted = 0;
3717           break;
3718         case RECORD_ALIGN_AND_FILL:
3719           /* A 4-byte alignment, and a 4-byte fill to load.  */
3720           if (size < 8)
3721             goto load_failed;
3722           r_list->records [i].data.align.bytes = *((uint32_t *) ptr);
3723           ptr += 4;
3724           r_list->records [i].data.align.fill = *((uint32_t *) ptr);
3725           ptr += 4;
3726           size -= 8;
3727           /* Just initialise PRECEDING_DELETED field, this field is
3728              used during linker relaxation.  */
3729           r_list->records [i].data.align.preceding_deleted = 0;
3730           break;
3731         default:
3732           goto load_failed;
3733         }
3734     }
3735
3736   free (contents);
3737   free (internal_relocs);
3738   return r_list;
3739
3740  load_failed:
3741   free (internal_relocs);
3742   free (contents);
3743   free (r_list);
3744   return NULL;
3745 }
3746
3747 /* Load all of the property records from ABFD.  See
3748    AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value.  */
3749
3750 struct avr_property_record_list *
3751 avr_elf32_load_property_records (bfd *abfd)
3752 {
3753   asection *sec;
3754
3755   /* Find the '.avr.prop' section and load the contents into memory.  */
3756   sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME);
3757   if (sec == NULL)
3758     return NULL;
3759   return avr_elf32_load_records_from_section (abfd, sec);
3760 }
3761
3762 const char *
3763 avr_elf32_property_record_name (struct avr_property_record *rec)
3764 {
3765   const char *str;
3766
3767   switch (rec->type)
3768     {
3769     case RECORD_ORG:
3770       str = "ORG";
3771       break;
3772     case RECORD_ORG_AND_FILL:
3773       str = "ORG+FILL";
3774       break;
3775     case RECORD_ALIGN:
3776       str = "ALIGN";
3777       break;
3778     case RECORD_ALIGN_AND_FILL:
3779       str = "ALIGN+FILL";
3780       break;
3781     default:
3782       str = "unknown";
3783     }
3784
3785   return str;
3786 }
3787
3788
3789 #define ELF_ARCH                bfd_arch_avr
3790 #define ELF_TARGET_ID           AVR_ELF_DATA
3791 #define ELF_MACHINE_CODE        EM_AVR
3792 #define ELF_MACHINE_ALT1        EM_AVR_OLD
3793 #define ELF_MAXPAGESIZE         1
3794
3795 #define TARGET_LITTLE_SYM       avr_elf32_vec
3796 #define TARGET_LITTLE_NAME      "elf32-avr"
3797
3798 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
3799
3800 #define elf_info_to_howto                    avr_info_to_howto_rela
3801 #define elf_info_to_howto_rel                NULL
3802 #define elf_backend_relocate_section         elf32_avr_relocate_section
3803 #define elf_backend_can_gc_sections          1
3804 #define elf_backend_rela_normal              1
3805 #define elf_backend_final_write_processing \
3806                                         bfd_elf_avr_final_write_processing
3807 #define elf_backend_object_p            elf32_avr_object_p
3808
3809 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
3810 #define bfd_elf32_bfd_get_relocated_section_contents \
3811                                         elf32_avr_get_relocated_section_contents
3812
3813 #include "elf32-target.h"