xref: /netbsd-src/external/gpl3/binutils.old/dist/bfd/elf32-avr.c (revision 2718af68c3efc72c9769069b5c7f9ed36f6b9def)
1 /* AVR-specific support for 32-bit ELF
2    Copyright (C) 1999-2018 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   unsigned 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   /* A 32 bit PC relative relocation.  */
647   HOWTO (R_AVR_32_PCREL,	/* type */
648 	 0,			/* rightshift */
649 	 2,			/* size (0 = byte, 1 = short, 2 = long) */
650 	 32,			/* bitsize */
651 	 TRUE,			/* pc_relative */
652 	 0,			/* bitpos */
653 	 complain_overflow_bitfield, /* complain_on_overflow */
654 	 bfd_elf_generic_reloc, /* special_function */
655 	 "R_AVR_32_PCREL",	/* name */
656 	 FALSE,			/* partial_inplace */
657 	 0xffffffff,		/* src_mask */
658 	 0xffffffff,		/* dst_mask */
659 	 TRUE),			/* pcrel_offset */
660 };
661 
662 /* Map BFD reloc types to AVR ELF reloc types.  */
663 
664 struct avr_reloc_map
665 {
666   bfd_reloc_code_real_type bfd_reloc_val;
667   unsigned int elf_reloc_val;
668 };
669 
670 static const struct avr_reloc_map avr_reloc_map[] =
671 {
672   { BFD_RELOC_NONE,		    R_AVR_NONE },
673   { BFD_RELOC_32,		    R_AVR_32 },
674   { BFD_RELOC_AVR_7_PCREL,	    R_AVR_7_PCREL },
675   { BFD_RELOC_AVR_13_PCREL,	    R_AVR_13_PCREL },
676   { BFD_RELOC_16,		    R_AVR_16 },
677   { BFD_RELOC_AVR_16_PM,	    R_AVR_16_PM },
678   { BFD_RELOC_AVR_LO8_LDI,	    R_AVR_LO8_LDI},
679   { BFD_RELOC_AVR_HI8_LDI,	    R_AVR_HI8_LDI },
680   { BFD_RELOC_AVR_HH8_LDI,	    R_AVR_HH8_LDI },
681   { BFD_RELOC_AVR_MS8_LDI,	    R_AVR_MS8_LDI },
682   { BFD_RELOC_AVR_LO8_LDI_NEG,	    R_AVR_LO8_LDI_NEG },
683   { BFD_RELOC_AVR_HI8_LDI_NEG,	    R_AVR_HI8_LDI_NEG },
684   { BFD_RELOC_AVR_HH8_LDI_NEG,	    R_AVR_HH8_LDI_NEG },
685   { BFD_RELOC_AVR_MS8_LDI_NEG,	    R_AVR_MS8_LDI_NEG },
686   { BFD_RELOC_AVR_LO8_LDI_PM,	    R_AVR_LO8_LDI_PM },
687   { BFD_RELOC_AVR_LO8_LDI_GS,	    R_AVR_LO8_LDI_GS },
688   { BFD_RELOC_AVR_HI8_LDI_PM,	    R_AVR_HI8_LDI_PM },
689   { BFD_RELOC_AVR_HI8_LDI_GS,	    R_AVR_HI8_LDI_GS },
690   { BFD_RELOC_AVR_HH8_LDI_PM,	    R_AVR_HH8_LDI_PM },
691   { BFD_RELOC_AVR_LO8_LDI_PM_NEG,   R_AVR_LO8_LDI_PM_NEG },
692   { BFD_RELOC_AVR_HI8_LDI_PM_NEG,   R_AVR_HI8_LDI_PM_NEG },
693   { BFD_RELOC_AVR_HH8_LDI_PM_NEG,   R_AVR_HH8_LDI_PM_NEG },
694   { BFD_RELOC_AVR_CALL,		    R_AVR_CALL },
695   { BFD_RELOC_AVR_LDI,		    R_AVR_LDI  },
696   { BFD_RELOC_AVR_6,		    R_AVR_6    },
697   { BFD_RELOC_AVR_6_ADIW,	    R_AVR_6_ADIW },
698   { BFD_RELOC_8,		    R_AVR_8 },
699   { BFD_RELOC_AVR_8_LO,		    R_AVR_8_LO8 },
700   { BFD_RELOC_AVR_8_HI,		    R_AVR_8_HI8 },
701   { BFD_RELOC_AVR_8_HLO,	    R_AVR_8_HLO8 },
702   { BFD_RELOC_AVR_DIFF8,	    R_AVR_DIFF8 },
703   { BFD_RELOC_AVR_DIFF16,	    R_AVR_DIFF16 },
704   { BFD_RELOC_AVR_DIFF32,	    R_AVR_DIFF32 },
705   { BFD_RELOC_AVR_LDS_STS_16,	    R_AVR_LDS_STS_16},
706   { BFD_RELOC_AVR_PORT6,	    R_AVR_PORT6},
707   { BFD_RELOC_AVR_PORT5,	    R_AVR_PORT5},
708   { BFD_RELOC_32_PCREL,		    R_AVR_32_PCREL}
709 };
710 
711 /* Meant to be filled one day with the wrap around address for the
712    specific device.  I.e. should get the value 0x4000 for 16k devices,
713    0x8000 for 32k devices and so on.
714 
715    We initialize it here with a value of 0x1000000 resulting in
716    that we will never suggest a wrap-around jump during relaxation.
717    The logic of the source code later on assumes that in
718    avr_pc_wrap_around one single bit is set.  */
719 static bfd_vma avr_pc_wrap_around = 0x10000000;
720 
721 /* If this variable holds a value different from zero, the linker relaxation
722    machine will try to optimize call/ret sequences by a single jump
723    instruction. This option could be switched off by a linker switch.  */
724 static int avr_replace_call_ret_sequences = 1;
725 
726 
727 /* Per-section relaxation related information for avr.  */
728 
729 struct avr_relax_info
730 {
731   /* Track the avr property records that apply to this section.  */
732 
733   struct
734   {
735     /* Number of records in the list.  */
736     unsigned count;
737 
738     /* How many records worth of space have we allocated.  */
739     unsigned allocated;
740 
741     /* The records, only COUNT records are initialised.  */
742     struct avr_property_record *items;
743   } records;
744 };
745 
746 /* Per section data, specialised for avr.  */
747 
748 struct elf_avr_section_data
749 {
750   /* The standard data must appear first.  */
751   struct bfd_elf_section_data elf;
752 
753   /* Relaxation related information.  */
754   struct avr_relax_info relax_info;
755 };
756 
757 /* Possibly initialise avr specific data for new section SEC from ABFD.  */
758 
759 static bfd_boolean
760 elf_avr_new_section_hook (bfd *abfd, asection *sec)
761 {
762   if (!sec->used_by_bfd)
763     {
764       struct elf_avr_section_data *sdata;
765       bfd_size_type amt = sizeof (*sdata);
766 
767       sdata = bfd_zalloc (abfd, amt);
768       if (sdata == NULL)
769 	return FALSE;
770       sec->used_by_bfd = sdata;
771     }
772 
773   return _bfd_elf_new_section_hook (abfd, sec);
774 }
775 
776 /* Return a pointer to the relaxation information for SEC.  */
777 
778 static struct avr_relax_info *
779 get_avr_relax_info (asection *sec)
780 {
781   struct elf_avr_section_data *section_data;
782 
783   /* No info available if no section or if it is an output section.  */
784   if (!sec || sec == sec->output_section)
785     return NULL;
786 
787   section_data = (struct elf_avr_section_data *) elf_section_data (sec);
788   return &section_data->relax_info;
789 }
790 
791 /* Initialise the per section relaxation information for SEC.  */
792 
793 static void
794 init_avr_relax_info (asection *sec)
795 {
796   struct avr_relax_info *relax_info = get_avr_relax_info (sec);
797 
798   relax_info->records.count = 0;
799   relax_info->records.allocated = 0;
800   relax_info->records.items = NULL;
801 }
802 
803 /* Initialize an entry in the stub hash table.  */
804 
805 static struct bfd_hash_entry *
806 stub_hash_newfunc (struct bfd_hash_entry *entry,
807 		   struct bfd_hash_table *table,
808 		   const char *string)
809 {
810   /* Allocate the structure if it has not already been allocated by a
811      subclass.  */
812   if (entry == NULL)
813     {
814       entry = bfd_hash_allocate (table,
815 				 sizeof (struct elf32_avr_stub_hash_entry));
816       if (entry == NULL)
817 	return entry;
818     }
819 
820   /* Call the allocation method of the superclass.  */
821   entry = bfd_hash_newfunc (entry, table, string);
822   if (entry != NULL)
823     {
824       struct elf32_avr_stub_hash_entry *hsh;
825 
826       /* Initialize the local fields.  */
827       hsh = avr_stub_hash_entry (entry);
828       hsh->stub_offset = 0;
829       hsh->target_value = 0;
830     }
831 
832   return entry;
833 }
834 
835 /* This function is just a straight passthrough to the real
836    function in linker.c.  Its prupose is so that its address
837    can be compared inside the avr_link_hash_table macro.  */
838 
839 static struct bfd_hash_entry *
840 elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry,
841 			     struct bfd_hash_table * table,
842 			     const char * string)
843 {
844   return _bfd_elf_link_hash_newfunc (entry, table, string);
845 }
846 
847 /* Free the derived linker hash table.  */
848 
849 static void
850 elf32_avr_link_hash_table_free (bfd *obfd)
851 {
852   struct elf32_avr_link_hash_table *htab
853     = (struct elf32_avr_link_hash_table *) obfd->link.hash;
854 
855   /* Free the address mapping table.  */
856   if (htab->amt_stub_offsets != NULL)
857     free (htab->amt_stub_offsets);
858   if (htab->amt_destination_addr != NULL)
859     free (htab->amt_destination_addr);
860 
861   bfd_hash_table_free (&htab->bstab);
862   _bfd_elf_link_hash_table_free (obfd);
863 }
864 
865 /* Create the derived linker hash table.  The AVR ELF port uses the derived
866    hash table to keep information specific to the AVR ELF linker (without
867    using static variables).  */
868 
869 static struct bfd_link_hash_table *
870 elf32_avr_link_hash_table_create (bfd *abfd)
871 {
872   struct elf32_avr_link_hash_table *htab;
873   bfd_size_type amt = sizeof (*htab);
874 
875   htab = bfd_zmalloc (amt);
876   if (htab == NULL)
877     return NULL;
878 
879   if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
880 				      elf32_avr_link_hash_newfunc,
881 				      sizeof (struct elf_link_hash_entry),
882 				      AVR_ELF_DATA))
883     {
884       free (htab);
885       return NULL;
886     }
887 
888   /* Init the stub hash table too.  */
889   if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
890 			    sizeof (struct elf32_avr_stub_hash_entry)))
891     {
892       _bfd_elf_link_hash_table_free (abfd);
893       return NULL;
894     }
895   htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free;
896 
897   return &htab->etab.root;
898 }
899 
900 /* Calculates the effective distance of a pc relative jump/call.  */
901 
902 static int
903 avr_relative_distance_considering_wrap_around (unsigned int distance)
904 {
905   unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
906   int dist_with_wrap_around = distance & wrap_around_mask;
907 
908   if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
909     dist_with_wrap_around -= avr_pc_wrap_around;
910 
911   return dist_with_wrap_around;
912 }
913 
914 
915 static reloc_howto_type *
916 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
917 				 bfd_reloc_code_real_type code)
918 {
919   unsigned int i;
920 
921   for (i = 0;
922        i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
923        i++)
924     if (avr_reloc_map[i].bfd_reloc_val == code)
925       return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
926 
927   return NULL;
928 }
929 
930 static reloc_howto_type *
931 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
932 				 const char *r_name)
933 {
934   unsigned int i;
935 
936   for (i = 0;
937        i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
938        i++)
939     if (elf_avr_howto_table[i].name != NULL
940 	&& strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
941       return &elf_avr_howto_table[i];
942 
943   return NULL;
944 }
945 
946 /* Set the howto pointer for an AVR ELF reloc.  */
947 
948 static bfd_boolean
949 avr_info_to_howto_rela (bfd *abfd,
950 			arelent *cache_ptr,
951 			Elf_Internal_Rela *dst)
952 {
953   unsigned int r_type;
954 
955   r_type = ELF32_R_TYPE (dst->r_info);
956   if (r_type >= (unsigned int) R_AVR_max)
957     {
958       /* xgettext:c-format */
959       _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
960 			  abfd, r_type);
961       bfd_set_error (bfd_error_bad_value);
962       return FALSE;
963     }
964   cache_ptr->howto = &elf_avr_howto_table[r_type];
965   return TRUE;
966 }
967 
968 static bfd_boolean
969 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
970 {
971   return (relocation >= 0x020000);
972 }
973 
974 /* Returns the address of the corresponding stub if there is one.
975    Returns otherwise an address above 0x020000.  This function
976    could also be used, if there is no knowledge on the section where
977    the destination is found.  */
978 
979 static bfd_vma
980 avr_get_stub_addr (bfd_vma srel,
981 		   struct elf32_avr_link_hash_table *htab)
982 {
983   unsigned int sindex;
984   bfd_vma stub_sec_addr =
985 	      (htab->stub_sec->output_section->vma +
986 	       htab->stub_sec->output_offset);
987 
988   for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++)
989     if (htab->amt_destination_addr[sindex] == srel)
990       return htab->amt_stub_offsets[sindex] + stub_sec_addr;
991 
992   /* Return an address that could not be reached by 16 bit relocs.  */
993   return 0x020000;
994 }
995 
996 /* Perform a diff relocation. Nothing to do, as the difference value is already
997    written into the section's contents. */
998 
999 static bfd_reloc_status_type
1000 bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1001 		      arelent *reloc_entry ATTRIBUTE_UNUSED,
1002 	      asymbol *symbol ATTRIBUTE_UNUSED,
1003 	      void *data ATTRIBUTE_UNUSED,
1004 	      asection *input_section ATTRIBUTE_UNUSED,
1005 	      bfd *output_bfd ATTRIBUTE_UNUSED,
1006 	      char **error_message ATTRIBUTE_UNUSED)
1007 {
1008   return bfd_reloc_ok;
1009 }
1010 
1011 
1012 /* Perform a single relocation.  By default we use the standard BFD
1013    routines, but a few relocs, we have to do them ourselves.  */
1014 
1015 static bfd_reloc_status_type
1016 avr_final_link_relocate (reloc_howto_type *		    howto,
1017 			 bfd *				    input_bfd,
1018 			 asection *			    input_section,
1019 			 bfd_byte *			    contents,
1020 			 Elf_Internal_Rela *		    rel,
1021 			 bfd_vma			    relocation,
1022 			 struct elf32_avr_link_hash_table * htab)
1023 {
1024   bfd_reloc_status_type r = bfd_reloc_ok;
1025   bfd_vma		x;
1026   bfd_signed_vma	srel;
1027   bfd_signed_vma	reloc_addr;
1028   bfd_boolean		use_stubs = FALSE;
1029   /* Usually is 0, unless we are generating code for a bootloader.  */
1030   bfd_signed_vma	base_addr = htab->vector_base;
1031 
1032   /* Absolute addr of the reloc in the final excecutable.  */
1033   reloc_addr = rel->r_offset + input_section->output_section->vma
1034 	       + input_section->output_offset;
1035 
1036   switch (howto->type)
1037     {
1038     case R_AVR_7_PCREL:
1039       contents += rel->r_offset;
1040       srel = (bfd_signed_vma) relocation;
1041       srel += rel->r_addend;
1042       srel -= rel->r_offset;
1043       srel -= 2;	/* Branch instructions add 2 to the PC...  */
1044       srel -= (input_section->output_section->vma +
1045 	       input_section->output_offset);
1046 
1047       if (srel & 1)
1048 	return bfd_reloc_outofrange;
1049       if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
1050 	return bfd_reloc_overflow;
1051       x = bfd_get_16 (input_bfd, contents);
1052       x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
1053       bfd_put_16 (input_bfd, x, contents);
1054       break;
1055 
1056     case R_AVR_13_PCREL:
1057       contents   += rel->r_offset;
1058       srel = (bfd_signed_vma) relocation;
1059       srel += rel->r_addend;
1060       srel -= rel->r_offset;
1061       srel -= 2;	/* Branch instructions add 2 to the PC...  */
1062       srel -= (input_section->output_section->vma +
1063 	       input_section->output_offset);
1064 
1065       if (srel & 1)
1066 	return bfd_reloc_outofrange;
1067 
1068       srel = avr_relative_distance_considering_wrap_around (srel);
1069 
1070       /* AVR addresses commands as words.  */
1071       srel >>= 1;
1072 
1073       /* Check for overflow.  */
1074       if (srel < -2048 || srel > 2047)
1075 	{
1076 	  /* Relative distance is too large.  */
1077 
1078 	  /* Always apply WRAPAROUND for avr2, avr25, and avr4.  */
1079 	  switch (bfd_get_mach (input_bfd))
1080 	    {
1081 	    case bfd_mach_avr2:
1082 	    case bfd_mach_avr25:
1083 	    case bfd_mach_avr4:
1084 	      break;
1085 
1086 	    default:
1087 	      return bfd_reloc_overflow;
1088 	    }
1089 	}
1090 
1091       x = bfd_get_16 (input_bfd, contents);
1092       x = (x & 0xf000) | (srel & 0xfff);
1093       bfd_put_16 (input_bfd, x, contents);
1094       break;
1095 
1096     case R_AVR_LO8_LDI:
1097       contents += rel->r_offset;
1098       srel = (bfd_signed_vma) relocation + rel->r_addend;
1099       x = bfd_get_16 (input_bfd, contents);
1100       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1101       bfd_put_16 (input_bfd, x, contents);
1102       break;
1103 
1104     case R_AVR_LDI:
1105       contents += rel->r_offset;
1106       srel = (bfd_signed_vma) relocation + rel->r_addend;
1107       if (((srel > 0) && (srel & 0xffff) > 255)
1108 	  || ((srel < 0) && ((-srel) & 0xffff) > 128))
1109 	/* Remove offset for data/eeprom section.  */
1110 	return bfd_reloc_overflow;
1111 
1112       x = bfd_get_16 (input_bfd, contents);
1113       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1114       bfd_put_16 (input_bfd, x, contents);
1115       break;
1116 
1117     case R_AVR_6:
1118       contents += rel->r_offset;
1119       srel = (bfd_signed_vma) relocation + rel->r_addend;
1120       if (((srel & 0xffff) > 63) || (srel < 0))
1121 	/* Remove offset for data/eeprom section.  */
1122 	return bfd_reloc_overflow;
1123       x = bfd_get_16 (input_bfd, contents);
1124       x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
1125 		       | ((srel & (1 << 5)) << 8));
1126       bfd_put_16 (input_bfd, x, contents);
1127       break;
1128 
1129     case R_AVR_6_ADIW:
1130       contents += rel->r_offset;
1131       srel = (bfd_signed_vma) relocation + rel->r_addend;
1132       if (((srel & 0xffff) > 63) || (srel < 0))
1133 	/* Remove offset for data/eeprom section.  */
1134 	return bfd_reloc_overflow;
1135       x = bfd_get_16 (input_bfd, contents);
1136       x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
1137       bfd_put_16 (input_bfd, x, contents);
1138       break;
1139 
1140     case R_AVR_HI8_LDI:
1141       contents += rel->r_offset;
1142       srel = (bfd_signed_vma) relocation + rel->r_addend;
1143       srel = (srel >> 8) & 0xff;
1144       x = bfd_get_16 (input_bfd, contents);
1145       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1146       bfd_put_16 (input_bfd, x, contents);
1147       break;
1148 
1149     case R_AVR_HH8_LDI:
1150       contents += rel->r_offset;
1151       srel = (bfd_signed_vma) relocation + rel->r_addend;
1152       srel = (srel >> 16) & 0xff;
1153       x = bfd_get_16 (input_bfd, contents);
1154       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1155       bfd_put_16 (input_bfd, x, contents);
1156       break;
1157 
1158     case R_AVR_MS8_LDI:
1159       contents += rel->r_offset;
1160       srel = (bfd_signed_vma) relocation + rel->r_addend;
1161       srel = (srel >> 24) & 0xff;
1162       x = bfd_get_16 (input_bfd, contents);
1163       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1164       bfd_put_16 (input_bfd, x, contents);
1165       break;
1166 
1167     case R_AVR_LO8_LDI_NEG:
1168       contents += rel->r_offset;
1169       srel = (bfd_signed_vma) relocation + rel->r_addend;
1170       srel = -srel;
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_HI8_LDI_NEG:
1177       contents += rel->r_offset;
1178       srel = (bfd_signed_vma) relocation + rel->r_addend;
1179       srel = -srel;
1180       srel = (srel >> 8) & 0xff;
1181       x = bfd_get_16 (input_bfd, contents);
1182       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1183       bfd_put_16 (input_bfd, x, contents);
1184       break;
1185 
1186     case R_AVR_HH8_LDI_NEG:
1187       contents += rel->r_offset;
1188       srel = (bfd_signed_vma) relocation + rel->r_addend;
1189       srel = -srel;
1190       srel = (srel >> 16) & 0xff;
1191       x = bfd_get_16 (input_bfd, contents);
1192       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1193       bfd_put_16 (input_bfd, x, contents);
1194       break;
1195 
1196     case R_AVR_MS8_LDI_NEG:
1197       contents += rel->r_offset;
1198       srel = (bfd_signed_vma) relocation + rel->r_addend;
1199       srel = -srel;
1200       srel = (srel >> 24) & 0xff;
1201       x = bfd_get_16 (input_bfd, contents);
1202       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1203       bfd_put_16 (input_bfd, x, contents);
1204       break;
1205 
1206     case R_AVR_LO8_LDI_GS:
1207       use_stubs = (!htab->no_stubs);
1208       /* Fall through.  */
1209     case R_AVR_LO8_LDI_PM:
1210       contents += rel->r_offset;
1211       srel = (bfd_signed_vma) relocation + rel->r_addend;
1212 
1213       if (use_stubs
1214 	  && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1215 	{
1216 	  bfd_vma old_srel = srel;
1217 
1218 	  /* We need to use the address of the stub instead.  */
1219 	  srel = avr_get_stub_addr (srel, htab);
1220 	  if (debug_stubs)
1221 	    printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1222 		    "reloc at address 0x%x.\n",
1223 		    (unsigned int) srel,
1224 		    (unsigned int) old_srel,
1225 		    (unsigned int) reloc_addr);
1226 
1227 	  if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1228 	    return bfd_reloc_outofrange;
1229 	}
1230 
1231       if (srel & 1)
1232 	return bfd_reloc_outofrange;
1233       srel = srel >> 1;
1234       x = bfd_get_16 (input_bfd, contents);
1235       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1236       bfd_put_16 (input_bfd, x, contents);
1237       break;
1238 
1239     case R_AVR_HI8_LDI_GS:
1240       use_stubs = (!htab->no_stubs);
1241       /* Fall through.  */
1242     case R_AVR_HI8_LDI_PM:
1243       contents += rel->r_offset;
1244       srel = (bfd_signed_vma) relocation + rel->r_addend;
1245 
1246       if (use_stubs
1247 	  && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1248 	{
1249 	  bfd_vma old_srel = srel;
1250 
1251 	  /* We need to use the address of the stub instead.  */
1252 	  srel = avr_get_stub_addr (srel, htab);
1253 	  if (debug_stubs)
1254 	    printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1255 		    "reloc at address 0x%x.\n",
1256 		    (unsigned int) srel,
1257 		    (unsigned int) old_srel,
1258 		    (unsigned int) reloc_addr);
1259 
1260 	  if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1261 	    return bfd_reloc_outofrange;
1262 	}
1263 
1264       if (srel & 1)
1265 	return bfd_reloc_outofrange;
1266       srel = srel >> 1;
1267       srel = (srel >> 8) & 0xff;
1268       x = bfd_get_16 (input_bfd, contents);
1269       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1270       bfd_put_16 (input_bfd, x, contents);
1271       break;
1272 
1273     case R_AVR_HH8_LDI_PM:
1274       contents += rel->r_offset;
1275       srel = (bfd_signed_vma) relocation + rel->r_addend;
1276       if (srel & 1)
1277 	return bfd_reloc_outofrange;
1278       srel = srel >> 1;
1279       srel = (srel >> 16) & 0xff;
1280       x = bfd_get_16 (input_bfd, contents);
1281       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1282       bfd_put_16 (input_bfd, x, contents);
1283       break;
1284 
1285     case R_AVR_LO8_LDI_PM_NEG:
1286       contents += rel->r_offset;
1287       srel = (bfd_signed_vma) relocation + rel->r_addend;
1288       srel = -srel;
1289       if (srel & 1)
1290 	return bfd_reloc_outofrange;
1291       srel = srel >> 1;
1292       x = bfd_get_16 (input_bfd, contents);
1293       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1294       bfd_put_16 (input_bfd, x, contents);
1295       break;
1296 
1297     case R_AVR_HI8_LDI_PM_NEG:
1298       contents += rel->r_offset;
1299       srel = (bfd_signed_vma) relocation + rel->r_addend;
1300       srel = -srel;
1301       if (srel & 1)
1302 	return bfd_reloc_outofrange;
1303       srel = srel >> 1;
1304       srel = (srel >> 8) & 0xff;
1305       x = bfd_get_16 (input_bfd, contents);
1306       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1307       bfd_put_16 (input_bfd, x, contents);
1308       break;
1309 
1310     case R_AVR_HH8_LDI_PM_NEG:
1311       contents += rel->r_offset;
1312       srel = (bfd_signed_vma) relocation + rel->r_addend;
1313       srel = -srel;
1314       if (srel & 1)
1315 	return bfd_reloc_outofrange;
1316       srel = srel >> 1;
1317       srel = (srel >> 16) & 0xff;
1318       x = bfd_get_16 (input_bfd, contents);
1319       x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1320       bfd_put_16 (input_bfd, x, contents);
1321       break;
1322 
1323     case R_AVR_CALL:
1324       contents += rel->r_offset;
1325       srel = (bfd_signed_vma) relocation + rel->r_addend;
1326       if (srel & 1)
1327 	return bfd_reloc_outofrange;
1328       srel = srel >> 1;
1329       x = bfd_get_16 (input_bfd, contents);
1330       x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1331       bfd_put_16 (input_bfd, x, contents);
1332       bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1333       break;
1334 
1335     case R_AVR_16_PM:
1336       use_stubs = (!htab->no_stubs);
1337       contents += rel->r_offset;
1338       srel = (bfd_signed_vma) relocation + rel->r_addend;
1339 
1340       if (use_stubs
1341 	  && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1342 	{
1343 	  bfd_vma old_srel = srel;
1344 
1345 	  /* We need to use the address of the stub instead.  */
1346 	  srel = avr_get_stub_addr (srel,htab);
1347 	  if (debug_stubs)
1348 	    printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1349 		    "reloc at address 0x%x.\n",
1350 		    (unsigned int) srel,
1351 		    (unsigned int) old_srel,
1352 		    (unsigned int) reloc_addr);
1353 
1354 	  if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1355 	    return bfd_reloc_outofrange;
1356 	}
1357 
1358       if (srel & 1)
1359 	return bfd_reloc_outofrange;
1360       srel = srel >> 1;
1361       bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1362       break;
1363 
1364     case R_AVR_DIFF8:
1365     case R_AVR_DIFF16:
1366     case R_AVR_DIFF32:
1367       /* Nothing to do here, as contents already contains the diff value. */
1368       r = bfd_reloc_ok;
1369       break;
1370 
1371    case R_AVR_LDS_STS_16:
1372       contents += rel->r_offset;
1373       srel = (bfd_signed_vma) relocation + rel->r_addend;
1374       if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf)
1375 	return bfd_reloc_outofrange;
1376       srel = srel & 0x7f;
1377       x = bfd_get_16 (input_bfd, contents);
1378       x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2);
1379       bfd_put_16 (input_bfd, x, contents);
1380       break;
1381 
1382     case R_AVR_PORT6:
1383       contents += rel->r_offset;
1384       srel = (bfd_signed_vma) relocation + rel->r_addend;
1385       if ((srel & 0xffff) > 0x3f)
1386 	return bfd_reloc_outofrange;
1387       x = bfd_get_16 (input_bfd, contents);
1388       x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f);
1389       bfd_put_16 (input_bfd, x, contents);
1390       break;
1391 
1392     case R_AVR_PORT5:
1393       contents += rel->r_offset;
1394       srel = (bfd_signed_vma) relocation + rel->r_addend;
1395       if ((srel & 0xffff) > 0x1f)
1396 	return bfd_reloc_outofrange;
1397       x = bfd_get_16 (input_bfd, contents);
1398       x = (x & 0xff07) | ((srel & 0x1f) << 3);
1399       bfd_put_16 (input_bfd, x, contents);
1400       break;
1401 
1402     default:
1403       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1404 				    contents, rel->r_offset,
1405 				    relocation, rel->r_addend);
1406     }
1407 
1408   return r;
1409 }
1410 
1411 /* Relocate an AVR ELF section.  */
1412 
1413 static bfd_boolean
1414 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1415 			    struct bfd_link_info *info,
1416 			    bfd *input_bfd,
1417 			    asection *input_section,
1418 			    bfd_byte *contents,
1419 			    Elf_Internal_Rela *relocs,
1420 			    Elf_Internal_Sym *local_syms,
1421 			    asection **local_sections)
1422 {
1423   Elf_Internal_Shdr *		symtab_hdr;
1424   struct elf_link_hash_entry ** sym_hashes;
1425   Elf_Internal_Rela *		rel;
1426   Elf_Internal_Rela *		relend;
1427   struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
1428 
1429   if (htab == NULL)
1430     return FALSE;
1431 
1432   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1433   sym_hashes = elf_sym_hashes (input_bfd);
1434   relend     = relocs + input_section->reloc_count;
1435 
1436   for (rel = relocs; rel < relend; rel ++)
1437     {
1438       reloc_howto_type *	   howto;
1439       unsigned long		   r_symndx;
1440       Elf_Internal_Sym *	   sym;
1441       asection *		   sec;
1442       struct elf_link_hash_entry * h;
1443       bfd_vma			   relocation;
1444       bfd_reloc_status_type	   r;
1445       const char *		   name;
1446       int			   r_type;
1447 
1448       r_type = ELF32_R_TYPE (rel->r_info);
1449       r_symndx = ELF32_R_SYM (rel->r_info);
1450       howto  = elf_avr_howto_table + r_type;
1451       h      = NULL;
1452       sym    = NULL;
1453       sec    = NULL;
1454 
1455       if (r_symndx < symtab_hdr->sh_info)
1456 	{
1457 	  sym = local_syms + r_symndx;
1458 	  sec = local_sections [r_symndx];
1459 	  relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1460 
1461 	  name = bfd_elf_string_from_elf_section
1462 	    (input_bfd, symtab_hdr->sh_link, sym->st_name);
1463 	  name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1464 	}
1465       else
1466 	{
1467 	  bfd_boolean unresolved_reloc, warned, ignored;
1468 
1469 	  RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1470 				   r_symndx, symtab_hdr, sym_hashes,
1471 				   h, sec, relocation,
1472 				   unresolved_reloc, warned, ignored);
1473 
1474 	  name = h->root.root.string;
1475 	}
1476 
1477       if (sec != NULL && discarded_section (sec))
1478 	RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1479 					 rel, 1, relend, howto, 0, contents);
1480 
1481       if (bfd_link_relocatable (info))
1482 	continue;
1483 
1484       r = avr_final_link_relocate (howto, input_bfd, input_section,
1485 				   contents, rel, relocation, htab);
1486 
1487       if (r != bfd_reloc_ok)
1488 	{
1489 	  const char * msg = (const char *) NULL;
1490 
1491 	  switch (r)
1492 	    {
1493 	    case bfd_reloc_overflow:
1494 	      (*info->callbacks->reloc_overflow)
1495 		(info, (h ? &h->root : NULL), name, howto->name,
1496 		 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1497 	      break;
1498 
1499 	    case bfd_reloc_undefined:
1500 	      (*info->callbacks->undefined_symbol)
1501 		(info, name, input_bfd, input_section, rel->r_offset, TRUE);
1502 	      break;
1503 
1504 	    case bfd_reloc_outofrange:
1505 	      msg = _("internal error: out of range error");
1506 	      break;
1507 
1508 	    case bfd_reloc_notsupported:
1509 	      msg = _("internal error: unsupported relocation error");
1510 	      break;
1511 
1512 	    case bfd_reloc_dangerous:
1513 	      msg = _("internal error: dangerous relocation");
1514 	      break;
1515 
1516 	    default:
1517 	      msg = _("internal error: unknown error");
1518 	      break;
1519 	    }
1520 
1521 	  if (msg)
1522 	    (*info->callbacks->warning) (info, msg, name, input_bfd,
1523 					 input_section, rel->r_offset);
1524 	}
1525     }
1526 
1527   return TRUE;
1528 }
1529 
1530 /* The final processing done just before writing out a AVR ELF object
1531    file.  This gets the AVR architecture right based on the machine
1532    number.  */
1533 
1534 static void
1535 bfd_elf_avr_final_write_processing (bfd *abfd,
1536 				    bfd_boolean linker ATTRIBUTE_UNUSED)
1537 {
1538   unsigned long val;
1539 
1540   switch (bfd_get_mach (abfd))
1541     {
1542     default:
1543     case bfd_mach_avr2:
1544       val = E_AVR_MACH_AVR2;
1545       break;
1546 
1547     case bfd_mach_avr1:
1548       val = E_AVR_MACH_AVR1;
1549       break;
1550 
1551     case bfd_mach_avr25:
1552       val = E_AVR_MACH_AVR25;
1553       break;
1554 
1555     case bfd_mach_avr3:
1556       val = E_AVR_MACH_AVR3;
1557       break;
1558 
1559     case bfd_mach_avr31:
1560       val = E_AVR_MACH_AVR31;
1561       break;
1562 
1563     case bfd_mach_avr35:
1564       val = E_AVR_MACH_AVR35;
1565       break;
1566 
1567     case bfd_mach_avr4:
1568       val = E_AVR_MACH_AVR4;
1569       break;
1570 
1571     case bfd_mach_avr5:
1572       val = E_AVR_MACH_AVR5;
1573       break;
1574 
1575     case bfd_mach_avr51:
1576       val = E_AVR_MACH_AVR51;
1577       break;
1578 
1579     case bfd_mach_avr6:
1580       val = E_AVR_MACH_AVR6;
1581       break;
1582 
1583     case bfd_mach_avrxmega1:
1584       val = E_AVR_MACH_XMEGA1;
1585       break;
1586 
1587     case bfd_mach_avrxmega2:
1588       val = E_AVR_MACH_XMEGA2;
1589       break;
1590 
1591     case bfd_mach_avrxmega3:
1592       val = E_AVR_MACH_XMEGA3;
1593       break;
1594 
1595     case bfd_mach_avrxmega4:
1596       val = E_AVR_MACH_XMEGA4;
1597       break;
1598 
1599     case bfd_mach_avrxmega5:
1600       val = E_AVR_MACH_XMEGA5;
1601       break;
1602 
1603     case bfd_mach_avrxmega6:
1604       val = E_AVR_MACH_XMEGA6;
1605       break;
1606 
1607     case bfd_mach_avrxmega7:
1608       val = E_AVR_MACH_XMEGA7;
1609       break;
1610 
1611    case bfd_mach_avrtiny:
1612       val = E_AVR_MACH_AVRTINY;
1613       break;
1614     }
1615 
1616   elf_elfheader (abfd)->e_machine = EM_AVR;
1617   elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1618   elf_elfheader (abfd)->e_flags |= val;
1619 }
1620 
1621 /* Set the right machine number.  */
1622 
1623 static bfd_boolean
1624 elf32_avr_object_p (bfd *abfd)
1625 {
1626   unsigned int e_set = bfd_mach_avr2;
1627 
1628   if (elf_elfheader (abfd)->e_machine == EM_AVR
1629       || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1630     {
1631       int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1632 
1633       switch (e_mach)
1634 	{
1635 	default:
1636 	case E_AVR_MACH_AVR2:
1637 	  e_set = bfd_mach_avr2;
1638 	  break;
1639 
1640 	case E_AVR_MACH_AVR1:
1641 	  e_set = bfd_mach_avr1;
1642 	  break;
1643 
1644 	case E_AVR_MACH_AVR25:
1645 	  e_set = bfd_mach_avr25;
1646 	  break;
1647 
1648 	case E_AVR_MACH_AVR3:
1649 	  e_set = bfd_mach_avr3;
1650 	  break;
1651 
1652 	case E_AVR_MACH_AVR31:
1653 	  e_set = bfd_mach_avr31;
1654 	  break;
1655 
1656 	case E_AVR_MACH_AVR35:
1657 	  e_set = bfd_mach_avr35;
1658 	  break;
1659 
1660 	case E_AVR_MACH_AVR4:
1661 	  e_set = bfd_mach_avr4;
1662 	  break;
1663 
1664 	case E_AVR_MACH_AVR5:
1665 	  e_set = bfd_mach_avr5;
1666 	  break;
1667 
1668 	case E_AVR_MACH_AVR51:
1669 	  e_set = bfd_mach_avr51;
1670 	  break;
1671 
1672 	case E_AVR_MACH_AVR6:
1673 	  e_set = bfd_mach_avr6;
1674 	  break;
1675 
1676 	case E_AVR_MACH_XMEGA1:
1677 	  e_set = bfd_mach_avrxmega1;
1678 	  break;
1679 
1680 	case E_AVR_MACH_XMEGA2:
1681 	  e_set = bfd_mach_avrxmega2;
1682 	  break;
1683 
1684 	case E_AVR_MACH_XMEGA3:
1685 	  e_set = bfd_mach_avrxmega3;
1686 	  break;
1687 
1688 	case E_AVR_MACH_XMEGA4:
1689 	  e_set = bfd_mach_avrxmega4;
1690 	  break;
1691 
1692 	case E_AVR_MACH_XMEGA5:
1693 	  e_set = bfd_mach_avrxmega5;
1694 	  break;
1695 
1696 	case E_AVR_MACH_XMEGA6:
1697 	  e_set = bfd_mach_avrxmega6;
1698 	  break;
1699 
1700 	case E_AVR_MACH_XMEGA7:
1701 	  e_set = bfd_mach_avrxmega7;
1702 	  break;
1703 
1704     case E_AVR_MACH_AVRTINY:
1705       e_set = bfd_mach_avrtiny;
1706       break;
1707 	}
1708     }
1709   return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1710 				    e_set);
1711 }
1712 
1713 /* Returns whether the relocation type passed is a diff reloc. */
1714 
1715 static bfd_boolean
1716 elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel)
1717 {
1718   return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8
1719 	  ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16
1720 	  || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32);
1721 }
1722 
1723 /* Reduce the diff value written in the section by count if the shrinked
1724    insn address happens to fall between the two symbols for which this
1725    diff reloc was emitted.  */
1726 
1727 static void
1728 elf32_avr_adjust_diff_reloc_value (bfd *abfd,
1729 				   struct bfd_section *isec,
1730 				   Elf_Internal_Rela *irel,
1731 				   bfd_vma symval,
1732 				   bfd_vma shrinked_insn_address,
1733 				   int count)
1734 {
1735   unsigned char *reloc_contents = NULL;
1736   unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
1737   if (isec_contents == NULL)
1738   {
1739     if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
1740       return;
1741 
1742     elf_section_data (isec)->this_hdr.contents = isec_contents;
1743   }
1744 
1745   reloc_contents = isec_contents + irel->r_offset;
1746 
1747   /* Read value written in object file. */
1748   bfd_signed_vma x = 0;
1749   switch (ELF32_R_TYPE (irel->r_info))
1750   {
1751   case R_AVR_DIFF8:
1752     {
1753       x = bfd_get_signed_8 (abfd, reloc_contents);
1754       break;
1755     }
1756   case R_AVR_DIFF16:
1757     {
1758       x = bfd_get_signed_16 (abfd, reloc_contents);
1759       break;
1760     }
1761   case R_AVR_DIFF32:
1762     {
1763       x = bfd_get_signed_32 (abfd, reloc_contents);
1764       break;
1765     }
1766   default:
1767     {
1768       BFD_FAIL();
1769     }
1770   }
1771 
1772   /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
1773      into the object file at the reloc offset. sym2's logical value is
1774      symval (<start_of_section>) + reloc addend. Compute the start and end
1775      addresses and check if the shrinked insn falls between sym1 and sym2. */
1776 
1777   bfd_vma sym2_address = symval + irel->r_addend;
1778   bfd_vma sym1_address = sym2_address - x;
1779 
1780   /* Don't assume sym2 is bigger than sym1 - the difference
1781      could be negative. Compute start and end addresses, and
1782      use those to see if they span shrinked_insn_address. */
1783 
1784   bfd_vma start_address = sym1_address < sym2_address
1785     ? sym1_address : sym2_address;
1786   bfd_vma end_address = sym1_address > sym2_address
1787     ? sym1_address : sym2_address;
1788 
1789 
1790   if (shrinked_insn_address >= start_address
1791       && shrinked_insn_address < end_address)
1792   {
1793     /* Reduce the diff value by count bytes and write it back into section
1794        contents. */
1795     bfd_signed_vma new_diff = x < 0 ? x + count : x - count;
1796 
1797     if (sym2_address > shrinked_insn_address)
1798       irel->r_addend -= count;
1799 
1800     switch (ELF32_R_TYPE (irel->r_info))
1801     {
1802     case R_AVR_DIFF8:
1803       {
1804 	bfd_put_signed_8 (abfd, new_diff, reloc_contents);
1805 	break;
1806       }
1807     case R_AVR_DIFF16:
1808       {
1809 	bfd_put_signed_16 (abfd, new_diff & 0xFFFF, reloc_contents);
1810 	break;
1811       }
1812     case R_AVR_DIFF32:
1813       {
1814 	bfd_put_signed_32 (abfd, new_diff & 0xFFFFFFFF, reloc_contents);
1815 	break;
1816       }
1817     default:
1818       {
1819 	BFD_FAIL();
1820       }
1821     }
1822 
1823   }
1824 }
1825 
1826 static void
1827 elf32_avr_adjust_reloc_if_spans_insn (bfd *abfd,
1828 				      asection *isec,
1829 				      Elf_Internal_Rela *irel,  bfd_vma symval,
1830 				      bfd_vma shrinked_insn_address,
1831 				      bfd_vma shrink_boundary,
1832 				      int count)
1833 {
1834 
1835   if (elf32_avr_is_diff_reloc (irel))
1836     {
1837       elf32_avr_adjust_diff_reloc_value (abfd, isec, irel,
1838 					 symval,
1839 					 shrinked_insn_address,
1840 					 count);
1841     }
1842   else
1843     {
1844       bfd_vma reloc_value = symval + irel->r_addend;
1845       bfd_boolean addend_within_shrink_boundary =
1846 	(reloc_value <= shrink_boundary);
1847 
1848       bfd_boolean reloc_spans_insn =
1849 	(symval <= shrinked_insn_address
1850 	 && reloc_value > shrinked_insn_address
1851 	 && addend_within_shrink_boundary);
1852 
1853       if (! reloc_spans_insn)
1854 	return;
1855 
1856       irel->r_addend -= count;
1857 
1858       if (debug_relax)
1859 	printf ("Relocation's addend needed to be fixed \n");
1860     }
1861 }
1862 
1863 static bfd_boolean
1864 avr_should_move_sym (symvalue symval,
1865 		     bfd_vma start,
1866 		     bfd_vma end,
1867 		     bfd_boolean did_pad)
1868 {
1869   bfd_boolean sym_within_boundary =
1870 	  did_pad ? symval < end : symval <= end;
1871   return (symval > start && sym_within_boundary);
1872 }
1873 
1874 static bfd_boolean
1875 avr_should_reduce_sym_size (symvalue symval,
1876 			    symvalue symend,
1877 			    bfd_vma start,
1878 			    bfd_vma end,
1879 			    bfd_boolean did_pad)
1880 {
1881   bfd_boolean sym_end_within_boundary =
1882 	  did_pad ? symend < end : symend <= end;
1883   return (symval <= start && symend > start && sym_end_within_boundary);
1884 }
1885 
1886 static bfd_boolean
1887 avr_should_increase_sym_size (symvalue symval,
1888 			      symvalue symend,
1889 			      bfd_vma start,
1890 			      bfd_vma end,
1891 			      bfd_boolean did_pad)
1892 {
1893   return avr_should_move_sym (symval, start, end, did_pad)
1894 	  && symend >= end && did_pad;
1895 }
1896 
1897 /* Delete some bytes from a section while changing the size of an instruction.
1898    The parameter "addr" denotes the section-relative offset pointing just
1899    behind the shrinked instruction. "addr+count" point at the first
1900    byte just behind the original unshrinked instruction. If delete_shrinks_insn
1901    is FALSE, we are deleting redundant padding bytes from relax_info prop
1902    record handling. In that case, addr is section-relative offset of start
1903    of padding, and count is the number of padding bytes to delete. */
1904 
1905 static bfd_boolean
1906 elf32_avr_relax_delete_bytes (bfd *abfd,
1907 			      asection *sec,
1908 			      bfd_vma addr,
1909 			      int count,
1910 			      bfd_boolean delete_shrinks_insn)
1911 {
1912   Elf_Internal_Shdr *symtab_hdr;
1913   unsigned int sec_shndx;
1914   bfd_byte *contents;
1915   Elf_Internal_Rela *irel, *irelend;
1916   Elf_Internal_Sym *isym;
1917   Elf_Internal_Sym *isymbuf = NULL;
1918   bfd_vma toaddr;
1919   struct elf_link_hash_entry **sym_hashes;
1920   struct elf_link_hash_entry **end_hashes;
1921   unsigned int symcount;
1922   struct avr_relax_info *relax_info;
1923   struct avr_property_record *prop_record = NULL;
1924   bfd_boolean did_shrink = FALSE;
1925   bfd_boolean did_pad = FALSE;
1926 
1927   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1928   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1929   contents = elf_section_data (sec)->this_hdr.contents;
1930   relax_info = get_avr_relax_info (sec);
1931 
1932   toaddr = sec->size;
1933 
1934   if (relax_info->records.count > 0)
1935     {
1936       /* There should be no property record within the range of deleted
1937 	 bytes, however, there might be a property record for ADDR, this is
1938 	 how we handle alignment directives.
1939 	 Find the next (if any) property record after the deleted bytes.  */
1940       unsigned int i;
1941 
1942       for (i = 0; i < relax_info->records.count; ++i)
1943 	{
1944 	  bfd_vma offset = relax_info->records.items [i].offset;
1945 
1946 	  BFD_ASSERT (offset <= addr || offset >= (addr + count));
1947 	  if (offset >= (addr + count))
1948 	    {
1949 	      prop_record = &relax_info->records.items [i];
1950 	      toaddr = offset;
1951 	      break;
1952 	    }
1953 	}
1954     }
1955 
1956   irel = elf_section_data (sec)->relocs;
1957   irelend = irel + sec->reloc_count;
1958 
1959   /* Actually delete the bytes.  */
1960   if (toaddr - addr - count > 0)
1961     {
1962       memmove (contents + addr, contents + addr + count,
1963 	       (size_t) (toaddr - addr - count));
1964       did_shrink = TRUE;
1965     }
1966   if (prop_record == NULL)
1967     {
1968       sec->size -= count;
1969       did_shrink = TRUE;
1970     }
1971   else
1972     {
1973       /* Use the property record to fill in the bytes we've opened up.  */
1974       int fill = 0;
1975       switch (prop_record->type)
1976 	{
1977 	case RECORD_ORG_AND_FILL:
1978 	  fill = prop_record->data.org.fill;
1979 	  /* Fall through.  */
1980 	case RECORD_ORG:
1981 	  break;
1982 	case RECORD_ALIGN_AND_FILL:
1983 	  fill = prop_record->data.align.fill;
1984 	  /* Fall through.  */
1985 	case RECORD_ALIGN:
1986 	  prop_record->data.align.preceding_deleted += count;
1987 	  break;
1988 	};
1989       /* If toaddr == (addr + count), then we didn't delete anything, yet
1990 	 we fill count bytes backwards from toaddr. This is still ok - we
1991 	 end up overwriting the bytes we would have deleted. We just need
1992 	 to remember we didn't delete anything i.e. don't set did_shrink,
1993 	 so that we don't corrupt reloc offsets or symbol values.*/
1994       memset (contents + toaddr - count, fill, count);
1995       did_pad = TRUE;
1996     }
1997 
1998   if (!did_shrink)
1999     return TRUE;
2000 
2001   /* Adjust all the reloc addresses.  */
2002   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2003     {
2004       bfd_vma old_reloc_address;
2005 
2006       old_reloc_address = (sec->output_section->vma
2007 			   + sec->output_offset + irel->r_offset);
2008 
2009       /* Get the new reloc address.  */
2010       if ((irel->r_offset > addr
2011 	   && irel->r_offset < toaddr))
2012 	{
2013 	  if (debug_relax)
2014 	    printf ("Relocation at address 0x%x needs to be moved.\n"
2015 		    "Old section offset: 0x%x, New section offset: 0x%x \n",
2016 		    (unsigned int) old_reloc_address,
2017 		    (unsigned int) irel->r_offset,
2018 		    (unsigned int) ((irel->r_offset) - count));
2019 
2020 	  irel->r_offset -= count;
2021 	}
2022 
2023     }
2024 
2025    /* The reloc's own addresses are now ok. However, we need to readjust
2026       the reloc's addend, i.e. the reloc's value if two conditions are met:
2027       1.) the reloc is relative to a symbol in this section that
2028 	  is located in front of the shrinked instruction
2029       2.) symbol plus addend end up behind the shrinked instruction.
2030 
2031       The most common case where this happens are relocs relative to
2032       the section-start symbol.
2033 
2034       This step needs to be done for all of the sections of the bfd.  */
2035 
2036   {
2037     struct bfd_section *isec;
2038 
2039     for (isec = abfd->sections; isec; isec = isec->next)
2040      {
2041        bfd_vma symval;
2042        bfd_vma shrinked_insn_address;
2043 
2044        if (isec->reloc_count == 0)
2045 	 continue;
2046 
2047        shrinked_insn_address = (sec->output_section->vma
2048 				+ sec->output_offset + addr);
2049        if (delete_shrinks_insn)
2050 	 shrinked_insn_address -= count;
2051 
2052        irel = elf_section_data (isec)->relocs;
2053        /* PR 12161: Read in the relocs for this section if necessary.  */
2054        if (irel == NULL)
2055 	 irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
2056 
2057        for (irelend = irel + isec->reloc_count;
2058 	    irel < irelend;
2059 	    irel++)
2060 	 {
2061 	   /* Read this BFD's local symbols if we haven't done
2062 	      so already.  */
2063 	   if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2064 	     {
2065 	       isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2066 	       if (isymbuf == NULL)
2067 		 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2068 						 symtab_hdr->sh_info, 0,
2069 						 NULL, NULL, NULL);
2070 	       if (isymbuf == NULL)
2071 		 return FALSE;
2072 	     }
2073 
2074 	   /* Get the value of the symbol referred to by the reloc.  */
2075 	   if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2076 	     {
2077 	       /* A local symbol.  */
2078 	       asection *sym_sec;
2079 
2080 	       isym = isymbuf + ELF32_R_SYM (irel->r_info);
2081 	       sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2082 	       symval = isym->st_value;
2083 	       /* If the reloc is absolute, it will not have
2084 		  a symbol or section associated with it.  */
2085 	       if (sym_sec == sec)
2086 		 {
2087 		   /* If there is an alignment boundary, we only need to
2088 		      adjust addends that end up below the boundary. */
2089 		   bfd_vma shrink_boundary = (toaddr
2090 					      + sec->output_section->vma
2091 					      + sec->output_offset);
2092 
2093 		   symval += sym_sec->output_section->vma
2094 			     + sym_sec->output_offset;
2095 
2096 		   if (debug_relax)
2097 		     printf ("Checking if the relocation's "
2098 			     "addend needs corrections.\n"
2099 			     "Address of anchor symbol: 0x%x \n"
2100 			     "Address of relocation target: 0x%x \n"
2101 			     "Address of relaxed insn: 0x%x \n",
2102 			     (unsigned int) symval,
2103 			     (unsigned int) (symval + irel->r_addend),
2104 			     (unsigned int) shrinked_insn_address);
2105 
2106 		   elf32_avr_adjust_reloc_if_spans_insn (abfd, isec, irel,
2107 							 symval,
2108 							 shrinked_insn_address,
2109 							 shrink_boundary,
2110 							 count);
2111 		 }
2112 	       /* else...Reference symbol is absolute.  No adjustment needed.  */
2113 	     }
2114 	   /* else...Reference symbol is extern.  No need for adjusting
2115 	      the addend.  */
2116 	 }
2117      }
2118   }
2119 
2120   /* Adjust the local symbols defined in this section.  */
2121   isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2122   /* Fix PR 9841, there may be no local symbols.  */
2123   if (isym != NULL)
2124     {
2125       Elf_Internal_Sym *isymend;
2126 
2127       isymend = isym + symtab_hdr->sh_info;
2128       for (; isym < isymend; isym++)
2129 	{
2130 	  if (isym->st_shndx == sec_shndx)
2131 	    {
2132 	      symvalue symval = isym->st_value;
2133 	      symvalue symend = symval + isym->st_size;
2134 	      if (avr_should_reduce_sym_size (symval, symend,
2135 				      addr, toaddr, did_pad))
2136 		{
2137 		  /* If this assert fires then we have a symbol that ends
2138 		     part way through an instruction.  Does that make
2139 		     sense?  */
2140 		  BFD_ASSERT (isym->st_value + isym->st_size >= addr + count);
2141 		  isym->st_size -= count;
2142 		}
2143 	      else if (avr_should_increase_sym_size (symval, symend,
2144 				      addr, toaddr, did_pad))
2145 		isym->st_size += count;
2146 
2147 	      if (avr_should_move_sym (symval, addr, toaddr, did_pad))
2148 		isym->st_value -= count;
2149 	    }
2150 	}
2151     }
2152 
2153   /* Now adjust the global symbols defined in this section.  */
2154   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2155 	      - symtab_hdr->sh_info);
2156   sym_hashes = elf_sym_hashes (abfd);
2157   end_hashes = sym_hashes + symcount;
2158   for (; sym_hashes < end_hashes; sym_hashes++)
2159     {
2160       struct elf_link_hash_entry *sym_hash = *sym_hashes;
2161       if ((sym_hash->root.type == bfd_link_hash_defined
2162 	   || sym_hash->root.type == bfd_link_hash_defweak)
2163 	  && sym_hash->root.u.def.section == sec)
2164 	{
2165 	  symvalue symval = sym_hash->root.u.def.value;
2166 	  symvalue symend = symval + sym_hash->size;
2167 
2168 	  if (avr_should_reduce_sym_size (symval, symend,
2169 				  addr, toaddr, did_pad))
2170 	    {
2171 	      /* If this assert fires then we have a symbol that ends
2172 		 part way through an instruction.  Does that make
2173 		 sense?  */
2174 	      BFD_ASSERT (symend >= addr + count);
2175 	      sym_hash->size -= count;
2176 	    }
2177 	  else if (avr_should_increase_sym_size (symval, symend,
2178 				  addr, toaddr, did_pad))
2179 	      sym_hash->size += count;
2180 
2181 	  if (avr_should_move_sym (symval, addr, toaddr, did_pad))
2182 	    sym_hash->root.u.def.value -= count;
2183 	}
2184     }
2185 
2186   return TRUE;
2187 }
2188 
2189 static Elf_Internal_Sym *
2190 retrieve_local_syms (bfd *input_bfd)
2191 {
2192   Elf_Internal_Shdr *symtab_hdr;
2193   Elf_Internal_Sym *isymbuf;
2194   size_t locsymcount;
2195 
2196   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2197   locsymcount = symtab_hdr->sh_info;
2198 
2199   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2200   if (isymbuf == NULL && locsymcount != 0)
2201     isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
2202 				    NULL, NULL, NULL);
2203 
2204   /* Save the symbols for this input file so they won't be read again.  */
2205   if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
2206     symtab_hdr->contents = (unsigned char *) isymbuf;
2207 
2208   return isymbuf;
2209 }
2210 
2211 /* Get the input section for a given symbol index.
2212    If the symbol is:
2213    . a section symbol, return the section;
2214    . a common symbol, return the common section;
2215    . an undefined symbol, return the undefined section;
2216    . an indirect symbol, follow the links;
2217    . an absolute value, return the absolute section.  */
2218 
2219 static asection *
2220 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
2221 {
2222   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2223   asection *target_sec = NULL;
2224   if (r_symndx < symtab_hdr->sh_info)
2225     {
2226       Elf_Internal_Sym *isymbuf;
2227       unsigned int section_index;
2228 
2229       isymbuf = retrieve_local_syms (abfd);
2230       section_index = isymbuf[r_symndx].st_shndx;
2231 
2232       if (section_index == SHN_UNDEF)
2233 	target_sec = bfd_und_section_ptr;
2234       else if (section_index == SHN_ABS)
2235 	target_sec = bfd_abs_section_ptr;
2236       else if (section_index == SHN_COMMON)
2237 	target_sec = bfd_com_section_ptr;
2238       else
2239 	target_sec = bfd_section_from_elf_index (abfd, section_index);
2240     }
2241   else
2242     {
2243       unsigned long indx = r_symndx - symtab_hdr->sh_info;
2244       struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
2245 
2246       while (h->root.type == bfd_link_hash_indirect
2247 	     || h->root.type == bfd_link_hash_warning)
2248 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2249 
2250       switch (h->root.type)
2251 	{
2252 	case bfd_link_hash_defined:
2253 	case  bfd_link_hash_defweak:
2254 	  target_sec = h->root.u.def.section;
2255 	  break;
2256 	case bfd_link_hash_common:
2257 	  target_sec = bfd_com_section_ptr;
2258 	  break;
2259 	case bfd_link_hash_undefined:
2260 	case bfd_link_hash_undefweak:
2261 	  target_sec = bfd_und_section_ptr;
2262 	  break;
2263 	default: /* New indirect warning.  */
2264 	  target_sec = bfd_und_section_ptr;
2265 	  break;
2266 	}
2267     }
2268   return target_sec;
2269 }
2270 
2271 /* Get the section-relative offset for a symbol number.  */
2272 
2273 static bfd_vma
2274 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
2275 {
2276   Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2277   bfd_vma offset = 0;
2278 
2279   if (r_symndx < symtab_hdr->sh_info)
2280     {
2281       Elf_Internal_Sym *isymbuf;
2282       isymbuf = retrieve_local_syms (abfd);
2283       offset = isymbuf[r_symndx].st_value;
2284     }
2285   else
2286     {
2287       unsigned long indx = r_symndx - symtab_hdr->sh_info;
2288       struct elf_link_hash_entry *h =
2289 	elf_sym_hashes (abfd)[indx];
2290 
2291       while (h->root.type == bfd_link_hash_indirect
2292 	     || h->root.type == bfd_link_hash_warning)
2293 	h = (struct elf_link_hash_entry *) h->root.u.i.link;
2294       if (h->root.type == bfd_link_hash_defined
2295 	  || h->root.type == bfd_link_hash_defweak)
2296 	offset = h->root.u.def.value;
2297     }
2298   return offset;
2299 }
2300 
2301 /* Iterate over the property records in R_LIST, and copy each record into
2302    the list of records within the relaxation information for the section to
2303    which the record applies.  */
2304 
2305 static void
2306 avr_elf32_assign_records_to_sections (struct avr_property_record_list *r_list)
2307 {
2308   unsigned int i;
2309 
2310   for (i = 0; i < r_list->record_count; ++i)
2311     {
2312       struct avr_relax_info *relax_info;
2313 
2314       relax_info = get_avr_relax_info (r_list->records [i].section);
2315       BFD_ASSERT (relax_info != NULL);
2316 
2317       if (relax_info->records.count
2318 	  == relax_info->records.allocated)
2319 	{
2320 	  /* Allocate more space.  */
2321 	  bfd_size_type size;
2322 
2323 	  relax_info->records.allocated += 10;
2324 	  size = (sizeof (struct avr_property_record)
2325 		  * relax_info->records.allocated);
2326 	  relax_info->records.items
2327 	    = bfd_realloc (relax_info->records.items, size);
2328 	}
2329 
2330       memcpy (&relax_info->records.items [relax_info->records.count],
2331 	      &r_list->records [i],
2332 	      sizeof (struct avr_property_record));
2333       relax_info->records.count++;
2334     }
2335 }
2336 
2337 /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the
2338    ordering callback from QSORT.  */
2339 
2340 static int
2341 avr_property_record_compare (const void *ap, const void *bp)
2342 {
2343   const struct avr_property_record *a
2344     = (struct avr_property_record *) ap;
2345   const struct avr_property_record *b
2346     = (struct avr_property_record *) bp;
2347 
2348   if (a->offset != b->offset)
2349     return (a->offset - b->offset);
2350 
2351   if (a->section != b->section)
2352     return (bfd_get_section_vma (a->section->owner, a->section)
2353 	    - bfd_get_section_vma (b->section->owner, b->section));
2354 
2355   return (a->type - b->type);
2356 }
2357 
2358 /* Load all of the avr property sections from all of the bfd objects
2359    referenced from LINK_INFO.  All of the records within each property
2360    section are assigned to the STRUCT AVR_RELAX_INFO within the section
2361    specific data of the appropriate section.  */
2362 
2363 static void
2364 avr_load_all_property_sections (struct bfd_link_info *link_info)
2365 {
2366   bfd *abfd;
2367   asection *sec;
2368 
2369   /* Initialize the per-section relaxation info.  */
2370   for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2371     for (sec = abfd->sections; sec != NULL; sec = sec->next)
2372       {
2373 	init_avr_relax_info (sec);
2374       }
2375 
2376   /* Load the descriptor tables from .avr.prop sections.  */
2377   for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2378     {
2379       struct avr_property_record_list *r_list;
2380 
2381       r_list = avr_elf32_load_property_records (abfd);
2382       if (r_list != NULL)
2383 	avr_elf32_assign_records_to_sections (r_list);
2384 
2385       free (r_list);
2386     }
2387 
2388   /* Now, for every section, ensure that the descriptor list in the
2389      relaxation data is sorted by ascending offset within the section.  */
2390   for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2391     for (sec = abfd->sections; sec != NULL; sec = sec->next)
2392       {
2393 	struct avr_relax_info *relax_info = get_avr_relax_info (sec);
2394 	if (relax_info && relax_info->records.count > 0)
2395 	  {
2396 	    unsigned int i;
2397 
2398 	    qsort (relax_info->records.items,
2399 		   relax_info->records.count,
2400 		   sizeof (struct avr_property_record),
2401 		   avr_property_record_compare);
2402 
2403 	    /* For debug purposes, list all the descriptors.  */
2404 	    for (i = 0; i < relax_info->records.count; ++i)
2405 	      {
2406 		switch (relax_info->records.items [i].type)
2407 		  {
2408 		  case RECORD_ORG:
2409 		    break;
2410 		  case RECORD_ORG_AND_FILL:
2411 		    break;
2412 		  case RECORD_ALIGN:
2413 		    break;
2414 		  case RECORD_ALIGN_AND_FILL:
2415 		    break;
2416 		  };
2417 	      }
2418 	  }
2419       }
2420 }
2421 
2422 /* This function handles relaxing for the avr.
2423    Many important relaxing opportunities within functions are already
2424    realized by the compiler itself.
2425    Here we try to replace  call (4 bytes) ->  rcall (2 bytes)
2426    and jump -> rjmp (safes also 2 bytes).
2427    As well we now optimize seqences of
2428      - call/rcall function
2429      - ret
2430    to yield
2431      - jmp/rjmp function
2432      - ret
2433    . In case that within a sequence
2434      - jmp/rjmp label
2435      - ret
2436    the ret could no longer be reached it is optimized away. In order
2437    to check if the ret is no longer needed, it is checked that the ret's address
2438    is not the target of a branch or jump within the same section, it is checked
2439    that there is no skip instruction before the jmp/rjmp and that there
2440    is no local or global label place at the address of the ret.
2441 
2442    We refrain from relaxing within sections ".vectors" and
2443    ".jumptables" in order to maintain the position of the instructions.
2444    There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
2445    if possible. (In future one could possibly use the space of the nop
2446    for the first instruction of the irq service function.
2447 
2448    The .jumptables sections is meant to be used for a future tablejump variant
2449    for the devices with 3-byte program counter where the table itself
2450    contains 4-byte jump instructions whose relative offset must not
2451    be changed.  */
2452 
2453 static bfd_boolean
2454 elf32_avr_relax_section (bfd *abfd,
2455 			 asection *sec,
2456 			 struct bfd_link_info *link_info,
2457 			 bfd_boolean *again)
2458 {
2459   Elf_Internal_Shdr *symtab_hdr;
2460   Elf_Internal_Rela *internal_relocs;
2461   Elf_Internal_Rela *irel, *irelend;
2462   bfd_byte *contents = NULL;
2463   Elf_Internal_Sym *isymbuf = NULL;
2464   struct elf32_avr_link_hash_table *htab;
2465   static bfd_boolean relaxation_initialised = FALSE;
2466 
2467   if (!relaxation_initialised)
2468     {
2469       relaxation_initialised = TRUE;
2470 
2471       /* Load entries from the .avr.prop sections.  */
2472       avr_load_all_property_sections (link_info);
2473     }
2474 
2475   /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
2476      relaxing. Such shrinking can cause issues for the sections such
2477      as .vectors and .jumptables. Instead the unused bytes should be
2478      filled with nop instructions. */
2479   bfd_boolean shrinkable = TRUE;
2480 
2481   if (!strcmp (sec->name,".vectors")
2482       || !strcmp (sec->name,".jumptables"))
2483     shrinkable = FALSE;
2484 
2485   if (bfd_link_relocatable (link_info))
2486     (*link_info->callbacks->einfo)
2487       (_("%P%F: --relax and -r may not be used together\n"));
2488 
2489   htab = avr_link_hash_table (link_info);
2490   if (htab == NULL)
2491     return FALSE;
2492 
2493   /* Assume nothing changes.  */
2494   *again = FALSE;
2495 
2496   if ((!htab->no_stubs) && (sec == htab->stub_sec))
2497     {
2498       /* We are just relaxing the stub section.
2499 	 Let's calculate the size needed again.  */
2500       bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
2501 
2502       if (debug_relax)
2503 	printf ("Relaxing the stub section. Size prior to this pass: %i\n",
2504 		(int) last_estimated_stub_section_size);
2505 
2506       elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
2507 			    link_info, FALSE);
2508 
2509       /* Check if the number of trampolines changed.  */
2510       if (last_estimated_stub_section_size != htab->stub_sec->size)
2511 	*again = TRUE;
2512 
2513       if (debug_relax)
2514 	printf ("Size of stub section after this pass: %i\n",
2515 		(int) htab->stub_sec->size);
2516 
2517       return TRUE;
2518     }
2519 
2520   /* We don't have to do anything for a relocatable link, if
2521      this section does not have relocs, or if this is not a
2522      code section.  */
2523   if (bfd_link_relocatable (link_info)
2524       || (sec->flags & SEC_RELOC) == 0
2525       || sec->reloc_count == 0
2526       || (sec->flags & SEC_CODE) == 0)
2527     return TRUE;
2528 
2529   /* Check if the object file to relax uses internal symbols so that we
2530      could fix up the relocations.  */
2531   if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
2532     return TRUE;
2533 
2534   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2535 
2536   /* Get a copy of the native relocations.  */
2537   internal_relocs = (_bfd_elf_link_read_relocs
2538 		     (abfd, sec, NULL, NULL, link_info->keep_memory));
2539   if (internal_relocs == NULL)
2540     goto error_return;
2541 
2542   /* Walk through the relocs looking for relaxing opportunities.  */
2543   irelend = internal_relocs + sec->reloc_count;
2544   for (irel = internal_relocs; irel < irelend; irel++)
2545     {
2546       bfd_vma symval;
2547 
2548       if (   ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
2549 	  && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
2550 	  && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
2551 	continue;
2552 
2553       /* Get the section contents if we haven't done so already.  */
2554       if (contents == NULL)
2555 	{
2556 	  /* Get cached copy if it exists.  */
2557 	  if (elf_section_data (sec)->this_hdr.contents != NULL)
2558 	    contents = elf_section_data (sec)->this_hdr.contents;
2559 	  else
2560 	    {
2561 	      /* Go get them off disk.  */
2562 	      if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2563 		goto error_return;
2564 	    }
2565 	}
2566 
2567       /* Read this BFD's local symbols if we haven't done so already.  */
2568       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2569 	{
2570 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2571 	  if (isymbuf == NULL)
2572 	    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2573 					    symtab_hdr->sh_info, 0,
2574 					    NULL, NULL, NULL);
2575 	  if (isymbuf == NULL)
2576 	    goto error_return;
2577 	}
2578 
2579 
2580       /* Get the value of the symbol referred to by the reloc.  */
2581       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2582 	{
2583 	  /* A local symbol.  */
2584 	  Elf_Internal_Sym *isym;
2585 	  asection *sym_sec;
2586 
2587 	  isym = isymbuf + ELF32_R_SYM (irel->r_info);
2588 	  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2589 	  symval = isym->st_value;
2590 	  /* If the reloc is absolute, it will not have
2591 	     a symbol or section associated with it.  */
2592 	  if (sym_sec)
2593 	    symval += sym_sec->output_section->vma
2594 	      + sym_sec->output_offset;
2595 	}
2596       else
2597 	{
2598 	  unsigned long indx;
2599 	  struct elf_link_hash_entry *h;
2600 
2601 	  /* An external symbol.  */
2602 	  indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2603 	  h = elf_sym_hashes (abfd)[indx];
2604 	  BFD_ASSERT (h != NULL);
2605 	  if (h->root.type != bfd_link_hash_defined
2606 	      && h->root.type != bfd_link_hash_defweak)
2607 	    /* This appears to be a reference to an undefined
2608 	       symbol.  Just ignore it--it will be caught by the
2609 	       regular reloc processing.  */
2610 	    continue;
2611 
2612 	  symval = (h->root.u.def.value
2613 		    + h->root.u.def.section->output_section->vma
2614 		    + h->root.u.def.section->output_offset);
2615 	}
2616 
2617       /* For simplicity of coding, we are going to modify the section
2618 	 contents, the section relocs, and the BFD symbol table.  We
2619 	 must tell the rest of the code not to free up this
2620 	 information.  It would be possible to instead create a table
2621 	 of changes which have to be made, as is done in coff-mips.c;
2622 	 that would be more work, but would require less memory when
2623 	 the linker is run.  */
2624       switch (ELF32_R_TYPE (irel->r_info))
2625 	{
2626 	  /* Try to turn a 22-bit absolute call/jump into an 13-bit
2627 	     pc-relative rcall/rjmp.  */
2628 	case R_AVR_CALL:
2629 	  {
2630 	    bfd_vma value = symval + irel->r_addend;
2631 	    bfd_vma dot, gap;
2632 	    int distance_short_enough = 0;
2633 
2634 	    /* Get the address of this instruction.  */
2635 	    dot = (sec->output_section->vma
2636 		   + sec->output_offset + irel->r_offset);
2637 
2638 	    /* Compute the distance from this insn to the branch target.  */
2639 	    gap = value - dot;
2640 
2641 	    /* Check if the gap falls in the range that can be accommodated
2642 	       in 13bits signed (It is 12bits when encoded, as we deal with
2643 	       word addressing). */
2644 	    if (!shrinkable && ((int) gap >= -4096 && (int) gap <= 4095))
2645 	      distance_short_enough = 1;
2646 	    /* If shrinkable, then we can check for a range of distance which
2647 	       is two bytes farther on both the directions because the call
2648 	       or jump target will be closer by two bytes after the
2649 	       relaxation. */
2650 	    else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4097))
2651 	      distance_short_enough = 1;
2652 
2653 	    /* Here we handle the wrap-around case.  E.g. for a 16k device
2654 	       we could use a rjmp to jump from address 0x100 to 0x3d00!
2655 	       In order to make this work properly, we need to fill the
2656 	       vaiable avr_pc_wrap_around with the appropriate value.
2657 	       I.e. 0x4000 for a 16k device.  */
2658 	    {
2659 	      /* Shrinking the code size makes the gaps larger in the
2660 		 case of wrap-arounds.  So we use a heuristical safety
2661 		 margin to avoid that during relax the distance gets
2662 		 again too large for the short jumps.  Let's assume
2663 		 a typical code-size reduction due to relax for a
2664 		 16k device of 600 bytes.  So let's use twice the
2665 		 typical value as safety margin.  */
2666 	      int rgap;
2667 	      int safety_margin;
2668 
2669 	      int assumed_shrink = 600;
2670 	      if (avr_pc_wrap_around > 0x4000)
2671 		assumed_shrink = 900;
2672 
2673 	      safety_margin = 2 * assumed_shrink;
2674 
2675 	      rgap = avr_relative_distance_considering_wrap_around (gap);
2676 
2677 	      if (rgap >= (-4092 + safety_margin)
2678 		  && rgap <= (4094 - safety_margin))
2679 		distance_short_enough = 1;
2680 	    }
2681 
2682 	    if (distance_short_enough)
2683 	      {
2684 		unsigned char code_msb;
2685 		unsigned char code_lsb;
2686 
2687 		if (debug_relax)
2688 		  printf ("shrinking jump/call instruction at address 0x%x"
2689 			  " in section %s\n\n",
2690 			  (int) dot, sec->name);
2691 
2692 		/* Note that we've changed the relocs, section contents,
2693 		   etc.  */
2694 		elf_section_data (sec)->relocs = internal_relocs;
2695 		elf_section_data (sec)->this_hdr.contents = contents;
2696 		symtab_hdr->contents = (unsigned char *) isymbuf;
2697 
2698 		/* Get the instruction code for relaxing.  */
2699 		code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
2700 		code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2701 
2702 		/* Mask out the relocation bits.  */
2703 		code_msb &= 0x94;
2704 		code_lsb &= 0x0E;
2705 		if (code_msb == 0x94 && code_lsb == 0x0E)
2706 		  {
2707 		    /* we are changing call -> rcall .  */
2708 		    bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
2709 		    bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
2710 		  }
2711 		else if (code_msb == 0x94 && code_lsb == 0x0C)
2712 		  {
2713 		    /* we are changeing jump -> rjmp.  */
2714 		    bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
2715 		    bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
2716 		  }
2717 		else
2718 		  abort ();
2719 
2720 		/* Fix the relocation's type.  */
2721 		irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2722 					     R_AVR_13_PCREL);
2723 
2724 		/* We should not modify the ordering if 'shrinkable' is
2725 		   FALSE. */
2726 		if (!shrinkable)
2727 		  {
2728 		    /* Let's insert a nop.  */
2729 		    bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
2730 		    bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
2731 		  }
2732 		else
2733 		  {
2734 		    /* Delete two bytes of data.  */
2735 		    if (!elf32_avr_relax_delete_bytes (abfd, sec,
2736 						       irel->r_offset + 2, 2,
2737 						       TRUE))
2738 		      goto error_return;
2739 
2740 		    /* That will change things, so, we should relax again.
2741 		       Note that this is not required, and it may be slow.  */
2742 		    *again = TRUE;
2743 		  }
2744 	      }
2745 	  }
2746 	  /* Fall through.  */
2747 
2748 	default:
2749 	  {
2750 	    unsigned char code_msb;
2751 	    unsigned char code_lsb;
2752 	    bfd_vma dot;
2753 
2754 	    code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2755 	    code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
2756 
2757 	    /* Get the address of this instruction.  */
2758 	    dot = (sec->output_section->vma
2759 		   + sec->output_offset + irel->r_offset);
2760 
2761 	    /* Here we look for rcall/ret or call/ret sequences that could be
2762 	       safely replaced by rjmp/ret or jmp/ret.  */
2763 	    if (((code_msb & 0xf0) == 0xd0)
2764 		&& avr_replace_call_ret_sequences)
2765 	      {
2766 		/* This insn is a rcall.  */
2767 		unsigned char next_insn_msb = 0;
2768 		unsigned char next_insn_lsb = 0;
2769 
2770 		if (irel->r_offset + 3 < sec->size)
2771 		  {
2772 		    next_insn_msb =
2773 		      bfd_get_8 (abfd, contents + irel->r_offset + 3);
2774 		    next_insn_lsb =
2775 		      bfd_get_8 (abfd, contents + irel->r_offset + 2);
2776 		  }
2777 
2778 		if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2779 		  {
2780 		    /* The next insn is a ret. We now convert the rcall insn
2781 		       into a rjmp instruction.  */
2782 		    code_msb &= 0xef;
2783 		    bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
2784 		    if (debug_relax)
2785 		      printf ("converted rcall/ret sequence at address 0x%x"
2786 			      " into rjmp/ret sequence. Section is %s\n\n",
2787 			      (int) dot, sec->name);
2788 		    *again = TRUE;
2789 		    break;
2790 		  }
2791 	      }
2792 	    else if ((0x94 == (code_msb & 0xfe))
2793 		     && (0x0e == (code_lsb & 0x0e))
2794 		     && avr_replace_call_ret_sequences)
2795 	      {
2796 		/* This insn is a call.  */
2797 		unsigned char next_insn_msb = 0;
2798 		unsigned char next_insn_lsb = 0;
2799 
2800 		if (irel->r_offset + 5 < sec->size)
2801 		  {
2802 		    next_insn_msb =
2803 		      bfd_get_8 (abfd, contents + irel->r_offset + 5);
2804 		    next_insn_lsb =
2805 		      bfd_get_8 (abfd, contents + irel->r_offset + 4);
2806 		  }
2807 
2808 		if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2809 		  {
2810 		    /* The next insn is a ret. We now convert the call insn
2811 		       into a jmp instruction.  */
2812 
2813 		    code_lsb &= 0xfd;
2814 		    bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
2815 		    if (debug_relax)
2816 		      printf ("converted call/ret sequence at address 0x%x"
2817 			      " into jmp/ret sequence. Section is %s\n\n",
2818 			      (int) dot, sec->name);
2819 		    *again = TRUE;
2820 		    break;
2821 		  }
2822 	      }
2823 	    else if ((0xc0 == (code_msb & 0xf0))
2824 		     || ((0x94 == (code_msb & 0xfe))
2825 			 && (0x0c == (code_lsb & 0x0e))))
2826 	      {
2827 		/* This insn is a rjmp or a jmp.  */
2828 		unsigned char next_insn_msb = 0;
2829 		unsigned char next_insn_lsb = 0;
2830 		int insn_size;
2831 
2832 		if (0xc0 == (code_msb & 0xf0))
2833 		  insn_size = 2; /* rjmp insn */
2834 		else
2835 		  insn_size = 4; /* jmp insn */
2836 
2837 		if (irel->r_offset + insn_size + 1 < sec->size)
2838 		  {
2839 		    next_insn_msb =
2840 		      bfd_get_8 (abfd, contents + irel->r_offset
2841 				 + insn_size + 1);
2842 		    next_insn_lsb =
2843 		      bfd_get_8 (abfd, contents + irel->r_offset
2844 				 + insn_size);
2845 		  }
2846 
2847 		if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2848 		  {
2849 		    /* The next insn is a ret. We possibly could delete
2850 		       this ret. First we need to check for preceding
2851 		       sbis/sbic/sbrs or cpse "skip" instructions.  */
2852 
2853 		    int there_is_preceding_non_skip_insn = 1;
2854 		    bfd_vma address_of_ret;
2855 
2856 		    address_of_ret = dot + insn_size;
2857 
2858 		    if (debug_relax && (insn_size == 2))
2859 		      printf ("found rjmp / ret sequence at address 0x%x\n",
2860 			      (int) dot);
2861 		    if (debug_relax && (insn_size == 4))
2862 		      printf ("found jmp / ret sequence at address 0x%x\n",
2863 			      (int) dot);
2864 
2865 		    /* We have to make sure that there is a preceding insn.  */
2866 		    if (irel->r_offset >= 2)
2867 		      {
2868 			unsigned char preceding_msb;
2869 			unsigned char preceding_lsb;
2870 
2871 			preceding_msb =
2872 			  bfd_get_8 (abfd, contents + irel->r_offset - 1);
2873 			preceding_lsb =
2874 			  bfd_get_8 (abfd, contents + irel->r_offset - 2);
2875 
2876 			/* sbic.  */
2877 			if (0x99 == preceding_msb)
2878 			  there_is_preceding_non_skip_insn = 0;
2879 
2880 			/* sbis.  */
2881 			if (0x9b == preceding_msb)
2882 			  there_is_preceding_non_skip_insn = 0;
2883 
2884 			/* sbrc */
2885 			if ((0xfc == (preceding_msb & 0xfe)
2886 			     && (0x00 == (preceding_lsb & 0x08))))
2887 			  there_is_preceding_non_skip_insn = 0;
2888 
2889 			/* sbrs */
2890 			if ((0xfe == (preceding_msb & 0xfe)
2891 			     && (0x00 == (preceding_lsb & 0x08))))
2892 			  there_is_preceding_non_skip_insn = 0;
2893 
2894 			/* cpse */
2895 			if (0x10 == (preceding_msb & 0xfc))
2896 			  there_is_preceding_non_skip_insn = 0;
2897 
2898 			if (there_is_preceding_non_skip_insn == 0)
2899 			  if (debug_relax)
2900 			    printf ("preceding skip insn prevents deletion of"
2901 				    " ret insn at Addy 0x%x in section %s\n",
2902 				    (int) dot + 2, sec->name);
2903 		      }
2904 		    else
2905 		      {
2906 			/* There is no previous instruction.  */
2907 			there_is_preceding_non_skip_insn = 0;
2908 		      }
2909 
2910 		    if (there_is_preceding_non_skip_insn)
2911 		      {
2912 			/* We now only have to make sure that there is no
2913 			   local label defined at the address of the ret
2914 			   instruction and that there is no local relocation
2915 			   in this section pointing to the ret.  */
2916 
2917 			int deleting_ret_is_safe = 1;
2918 			unsigned int section_offset_of_ret_insn =
2919 			  irel->r_offset + insn_size;
2920 			Elf_Internal_Sym *isym, *isymend;
2921 			unsigned int sec_shndx;
2922 			struct bfd_section *isec;
2923 
2924 			sec_shndx =
2925 			  _bfd_elf_section_from_bfd_section (abfd, sec);
2926 
2927 			/* Check for local symbols.  */
2928 			isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2929 			isymend = isym + symtab_hdr->sh_info;
2930 			/* PR 6019: There may not be any local symbols.  */
2931 			for (; isym != NULL && isym < isymend; isym++)
2932 			  {
2933 			    if (isym->st_value == section_offset_of_ret_insn
2934 				&& isym->st_shndx == sec_shndx)
2935 			      {
2936 				deleting_ret_is_safe = 0;
2937 				if (debug_relax)
2938 				  printf ("local label prevents deletion of ret "
2939 					  "insn at address 0x%x\n",
2940 					  (int) dot + insn_size);
2941 			      }
2942 			  }
2943 
2944 			/* Now check for global symbols.  */
2945 			{
2946 			  int symcount;
2947 			  struct elf_link_hash_entry **sym_hashes;
2948 			  struct elf_link_hash_entry **end_hashes;
2949 
2950 			  symcount = (symtab_hdr->sh_size
2951 				      / sizeof (Elf32_External_Sym)
2952 				      - symtab_hdr->sh_info);
2953 			  sym_hashes = elf_sym_hashes (abfd);
2954 			  end_hashes = sym_hashes + symcount;
2955 			  for (; sym_hashes < end_hashes; sym_hashes++)
2956 			    {
2957 			      struct elf_link_hash_entry *sym_hash =
2958 				*sym_hashes;
2959 			      if ((sym_hash->root.type == bfd_link_hash_defined
2960 				   || sym_hash->root.type ==
2961 				   bfd_link_hash_defweak)
2962 				  && sym_hash->root.u.def.section == sec
2963 				  && sym_hash->root.u.def.value == section_offset_of_ret_insn)
2964 				{
2965 				  deleting_ret_is_safe = 0;
2966 				  if (debug_relax)
2967 				    printf ("global label prevents deletion of "
2968 					    "ret insn at address 0x%x\n",
2969 					    (int) dot + insn_size);
2970 				}
2971 			    }
2972 			}
2973 
2974 			/* Now we check for relocations pointing to ret.  */
2975 			for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next)
2976 			  {
2977 			    Elf_Internal_Rela *rel;
2978 			    Elf_Internal_Rela *relend;
2979 
2980 			    rel = elf_section_data (isec)->relocs;
2981 			    if (rel == NULL)
2982 			      rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
2983 
2984 			    relend = rel + isec->reloc_count;
2985 
2986 			    for (; rel && rel < relend; rel++)
2987 			      {
2988 				bfd_vma reloc_target = 0;
2989 
2990 				/* Read this BFD's local symbols if we haven't
2991 				   done so already.  */
2992 				if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2993 				  {
2994 				    isymbuf = (Elf_Internal_Sym *)
2995 				      symtab_hdr->contents;
2996 				    if (isymbuf == NULL)
2997 				      isymbuf = bfd_elf_get_elf_syms
2998 					(abfd,
2999 					 symtab_hdr,
3000 					 symtab_hdr->sh_info, 0,
3001 					 NULL, NULL, NULL);
3002 				    if (isymbuf == NULL)
3003 				      break;
3004 				  }
3005 
3006 				/* Get the value of the symbol referred to
3007 				   by the reloc.  */
3008 				if (ELF32_R_SYM (rel->r_info)
3009 				    < symtab_hdr->sh_info)
3010 				  {
3011 				    /* A local symbol.  */
3012 				    asection *sym_sec;
3013 
3014 				    isym = isymbuf
3015 				      + ELF32_R_SYM (rel->r_info);
3016 				    sym_sec = bfd_section_from_elf_index
3017 				      (abfd, isym->st_shndx);
3018 				    symval = isym->st_value;
3019 
3020 				    /* If the reloc is absolute, it will not
3021 				       have a symbol or section associated
3022 				       with it.  */
3023 
3024 				    if (sym_sec)
3025 				      {
3026 					symval +=
3027 					  sym_sec->output_section->vma
3028 					  + sym_sec->output_offset;
3029 					reloc_target = symval + rel->r_addend;
3030 				      }
3031 				    else
3032 				      {
3033 					reloc_target = symval + rel->r_addend;
3034 					/* Reference symbol is absolute.  */
3035 				      }
3036 				  }
3037 				/* else ... reference symbol is extern.  */
3038 
3039 				if (address_of_ret == reloc_target)
3040 				  {
3041 				    deleting_ret_is_safe = 0;
3042 				    if (debug_relax)
3043 				      printf ("ret from "
3044 					      "rjmp/jmp ret sequence at address"
3045 					      " 0x%x could not be deleted. ret"
3046 					      " is target of a relocation.\n",
3047 					      (int) address_of_ret);
3048 				    break;
3049 				  }
3050 			      }
3051 			  }
3052 
3053 			if (deleting_ret_is_safe)
3054 			  {
3055 			    if (debug_relax)
3056 			      printf ("unreachable ret instruction "
3057 				      "at address 0x%x deleted.\n",
3058 				      (int) dot + insn_size);
3059 
3060 			    /* Delete two bytes of data.  */
3061 			    if (!elf32_avr_relax_delete_bytes (abfd, sec,
3062 							       irel->r_offset + insn_size, 2,
3063 							       TRUE))
3064 			      goto error_return;
3065 
3066 			    /* That will change things, so, we should relax
3067 			       again. Note that this is not required, and it
3068 			       may be slow.  */
3069 			    *again = TRUE;
3070 			    break;
3071 			  }
3072 		      }
3073 		  }
3074 	      }
3075 	    break;
3076 	  }
3077 	}
3078     }
3079 
3080   if (!*again)
3081     {
3082       /* Look through all the property records in this section to see if
3083 	 there's any alignment records that can be moved.  */
3084       struct avr_relax_info *relax_info;
3085 
3086       relax_info = get_avr_relax_info (sec);
3087       if (relax_info->records.count > 0)
3088 	{
3089 	  unsigned int i;
3090 
3091 	  for (i = 0; i < relax_info->records.count; ++i)
3092 	    {
3093 	      switch (relax_info->records.items [i].type)
3094 		{
3095 		case RECORD_ORG:
3096 		case RECORD_ORG_AND_FILL:
3097 		  break;
3098 		case RECORD_ALIGN:
3099 		case RECORD_ALIGN_AND_FILL:
3100 		  {
3101 		    struct avr_property_record *record;
3102 		    unsigned long bytes_to_align;
3103 		    int count = 0;
3104 
3105 		    /* Look for alignment directives that have had enough
3106 		       bytes deleted before them, such that the directive
3107 		       can be moved backwards and still maintain the
3108 		       required alignment.  */
3109 		    record = &relax_info->records.items [i];
3110 		    bytes_to_align
3111 		      = (unsigned long) (1 << record->data.align.bytes);
3112 		    while (record->data.align.preceding_deleted >=
3113 			   bytes_to_align)
3114 		      {
3115 			record->data.align.preceding_deleted
3116 			  -= bytes_to_align;
3117 			count += bytes_to_align;
3118 		      }
3119 
3120 		    if (count > 0)
3121 		      {
3122 			bfd_vma addr = record->offset;
3123 
3124 			/* We can delete COUNT bytes and this alignment
3125 			   directive will still be correctly aligned.
3126 			   First move the alignment directive, then delete
3127 			   the bytes.  */
3128 			record->offset -= count;
3129 			elf32_avr_relax_delete_bytes (abfd, sec,
3130 						      addr - count,
3131 						      count, FALSE);
3132 			*again = TRUE;
3133 		      }
3134 		  }
3135 		  break;
3136 		}
3137 	    }
3138 	}
3139     }
3140 
3141   if (contents != NULL
3142       && elf_section_data (sec)->this_hdr.contents != contents)
3143     {
3144       if (! link_info->keep_memory)
3145 	free (contents);
3146       else
3147 	{
3148 	  /* Cache the section contents for elf_link_input_bfd.  */
3149 	  elf_section_data (sec)->this_hdr.contents = contents;
3150 	}
3151     }
3152 
3153   if (internal_relocs != NULL
3154       && elf_section_data (sec)->relocs != internal_relocs)
3155     free (internal_relocs);
3156 
3157   return TRUE;
3158 
3159  error_return:
3160   if (isymbuf != NULL
3161       && symtab_hdr->contents != (unsigned char *) isymbuf)
3162     free (isymbuf);
3163   if (contents != NULL
3164       && elf_section_data (sec)->this_hdr.contents != contents)
3165     free (contents);
3166   if (internal_relocs != NULL
3167       && elf_section_data (sec)->relocs != internal_relocs)
3168     free (internal_relocs);
3169 
3170   return FALSE;
3171 }
3172 
3173 /* This is a version of bfd_generic_get_relocated_section_contents
3174    which uses elf32_avr_relocate_section.
3175 
3176    For avr it's essentially a cut and paste taken from the H8300 port.
3177    The author of the relaxation support patch for avr had absolutely no
3178    clue what is happening here but found out that this part of the code
3179    seems to be important.  */
3180 
3181 static bfd_byte *
3182 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
3183 					  struct bfd_link_info *link_info,
3184 					  struct bfd_link_order *link_order,
3185 					  bfd_byte *data,
3186 					  bfd_boolean relocatable,
3187 					  asymbol **symbols)
3188 {
3189   Elf_Internal_Shdr *symtab_hdr;
3190   asection *input_section = link_order->u.indirect.section;
3191   bfd *input_bfd = input_section->owner;
3192   asection **sections = NULL;
3193   Elf_Internal_Rela *internal_relocs = NULL;
3194   Elf_Internal_Sym *isymbuf = NULL;
3195 
3196   /* We only need to handle the case of relaxing, or of having a
3197      particular set of section contents, specially.  */
3198   if (relocatable
3199       || elf_section_data (input_section)->this_hdr.contents == NULL)
3200     return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3201 						       link_order, data,
3202 						       relocatable,
3203 						       symbols);
3204   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3205 
3206   memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3207 	  (size_t) input_section->size);
3208 
3209   if ((input_section->flags & SEC_RELOC) != 0
3210       && input_section->reloc_count > 0)
3211     {
3212       asection **secpp;
3213       Elf_Internal_Sym *isym, *isymend;
3214       bfd_size_type amt;
3215 
3216       internal_relocs = (_bfd_elf_link_read_relocs
3217 			 (input_bfd, input_section, NULL, NULL, FALSE));
3218       if (internal_relocs == NULL)
3219 	goto error_return;
3220 
3221       if (symtab_hdr->sh_info != 0)
3222 	{
3223 	  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3224 	  if (isymbuf == NULL)
3225 	    isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3226 					    symtab_hdr->sh_info, 0,
3227 					    NULL, NULL, NULL);
3228 	  if (isymbuf == NULL)
3229 	    goto error_return;
3230 	}
3231 
3232       amt = symtab_hdr->sh_info;
3233       amt *= sizeof (asection *);
3234       sections = bfd_malloc (amt);
3235       if (sections == NULL && amt != 0)
3236 	goto error_return;
3237 
3238       isymend = isymbuf + symtab_hdr->sh_info;
3239       for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3240 	{
3241 	  asection *isec;
3242 
3243 	  if (isym->st_shndx == SHN_UNDEF)
3244 	    isec = bfd_und_section_ptr;
3245 	  else if (isym->st_shndx == SHN_ABS)
3246 	    isec = bfd_abs_section_ptr;
3247 	  else if (isym->st_shndx == SHN_COMMON)
3248 	    isec = bfd_com_section_ptr;
3249 	  else
3250 	    isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3251 
3252 	  *secpp = isec;
3253 	}
3254 
3255       if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
3256 					input_section, data, internal_relocs,
3257 					isymbuf, sections))
3258 	goto error_return;
3259 
3260       if (sections != NULL)
3261 	free (sections);
3262       if (isymbuf != NULL
3263 	  && symtab_hdr->contents != (unsigned char *) isymbuf)
3264 	free (isymbuf);
3265       if (elf_section_data (input_section)->relocs != internal_relocs)
3266 	free (internal_relocs);
3267     }
3268 
3269   return data;
3270 
3271  error_return:
3272   if (sections != NULL)
3273     free (sections);
3274   if (isymbuf != NULL
3275       && symtab_hdr->contents != (unsigned char *) isymbuf)
3276     free (isymbuf);
3277   if (internal_relocs != NULL
3278       && elf_section_data (input_section)->relocs != internal_relocs)
3279     free (internal_relocs);
3280   return NULL;
3281 }
3282 
3283 
3284 /* Determines the hash entry name for a particular reloc. It consists of
3285    the identifier of the symbol section and the added reloc addend and
3286    symbol offset relative to the section the symbol is attached to.  */
3287 
3288 static char *
3289 avr_stub_name (const asection *symbol_section,
3290 	       const bfd_vma symbol_offset,
3291 	       const Elf_Internal_Rela *rela)
3292 {
3293   char *stub_name;
3294   bfd_size_type len;
3295 
3296   len = 8 + 1 + 8 + 1 + 1;
3297   stub_name = bfd_malloc (len);
3298 
3299   sprintf (stub_name, "%08x+%08x",
3300 	   symbol_section->id & 0xffffffff,
3301 	   (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
3302 
3303   return stub_name;
3304 }
3305 
3306 
3307 /* Add a new stub entry to the stub hash.  Not all fields of the new
3308    stub entry are initialised.  */
3309 
3310 static struct elf32_avr_stub_hash_entry *
3311 avr_add_stub (const char *stub_name,
3312 	      struct elf32_avr_link_hash_table *htab)
3313 {
3314   struct elf32_avr_stub_hash_entry *hsh;
3315 
3316   /* Enter this entry into the linker stub hash table.  */
3317   hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
3318 
3319   if (hsh == NULL)
3320     {
3321       /* xgettext:c-format */
3322       _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
3323       return NULL;
3324     }
3325 
3326   hsh->stub_offset = 0;
3327   return hsh;
3328 }
3329 
3330 /* We assume that there is already space allocated for the stub section
3331    contents and that before building the stubs the section size is
3332    initialized to 0.  We assume that within the stub hash table entry,
3333    the absolute position of the jmp target has been written in the
3334    target_value field.  We write here the offset of the generated jmp insn
3335    relative to the trampoline section start to the stub_offset entry in
3336    the stub hash table entry.  */
3337 
3338 static  bfd_boolean
3339 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
3340 {
3341   struct elf32_avr_stub_hash_entry *hsh;
3342   struct bfd_link_info *info;
3343   struct elf32_avr_link_hash_table *htab;
3344   bfd *stub_bfd;
3345   bfd_byte *loc;
3346   bfd_vma target;
3347   bfd_vma starget;
3348 
3349   /* Basic opcode */
3350   bfd_vma jmp_insn = 0x0000940c;
3351 
3352   /* Massage our args to the form they really have.  */
3353   hsh = avr_stub_hash_entry (bh);
3354 
3355   if (!hsh->is_actually_needed)
3356     return TRUE;
3357 
3358   info = (struct bfd_link_info *) in_arg;
3359 
3360   htab = avr_link_hash_table (info);
3361   if (htab == NULL)
3362     return FALSE;
3363 
3364   target = hsh->target_value;
3365 
3366   /* Make a note of the offset within the stubs for this entry.  */
3367   hsh->stub_offset = htab->stub_sec->size;
3368   loc = htab->stub_sec->contents + hsh->stub_offset;
3369 
3370   stub_bfd = htab->stub_sec->owner;
3371 
3372   if (debug_stubs)
3373     printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
3374 	     (unsigned int) target,
3375 	     (unsigned int) hsh->stub_offset);
3376 
3377   /* We now have to add the information on the jump target to the bare
3378      opcode bits already set in jmp_insn.  */
3379 
3380   /* Check for the alignment of the address.  */
3381   if (target & 1)
3382      return FALSE;
3383 
3384   starget = target >> 1;
3385   jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
3386   bfd_put_16 (stub_bfd, jmp_insn, loc);
3387   bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
3388 
3389   htab->stub_sec->size += 4;
3390 
3391   /* Now add the entries in the address mapping table if there is still
3392      space left.  */
3393   {
3394     unsigned int nr;
3395 
3396     nr = htab->amt_entry_cnt + 1;
3397     if (nr <= htab->amt_max_entry_cnt)
3398       {
3399 	htab->amt_entry_cnt = nr;
3400 
3401 	htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
3402 	htab->amt_destination_addr[nr - 1] = target;
3403       }
3404   }
3405 
3406   return TRUE;
3407 }
3408 
3409 static bfd_boolean
3410 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
3411 				   void *in_arg ATTRIBUTE_UNUSED)
3412 {
3413   struct elf32_avr_stub_hash_entry *hsh;
3414 
3415   hsh = avr_stub_hash_entry (bh);
3416   hsh->is_actually_needed = FALSE;
3417 
3418   return TRUE;
3419 }
3420 
3421 static bfd_boolean
3422 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
3423 {
3424   struct elf32_avr_stub_hash_entry *hsh;
3425   struct elf32_avr_link_hash_table *htab;
3426   int size;
3427 
3428   /* Massage our args to the form they really have.  */
3429   hsh = avr_stub_hash_entry (bh);
3430   htab = in_arg;
3431 
3432   if (hsh->is_actually_needed)
3433     size = 4;
3434   else
3435     size = 0;
3436 
3437   htab->stub_sec->size += size;
3438   return TRUE;
3439 }
3440 
3441 void
3442 elf32_avr_setup_params (struct bfd_link_info *info,
3443 			bfd *avr_stub_bfd,
3444 			asection *avr_stub_section,
3445 			bfd_boolean no_stubs,
3446 			bfd_boolean deb_stubs,
3447 			bfd_boolean deb_relax,
3448 			bfd_vma pc_wrap_around,
3449 			bfd_boolean call_ret_replacement)
3450 {
3451   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3452 
3453   if (htab == NULL)
3454     return;
3455   htab->stub_sec = avr_stub_section;
3456   htab->stub_bfd = avr_stub_bfd;
3457   htab->no_stubs = no_stubs;
3458 
3459   debug_relax = deb_relax;
3460   debug_stubs = deb_stubs;
3461   avr_pc_wrap_around = pc_wrap_around;
3462   avr_replace_call_ret_sequences = call_ret_replacement;
3463 }
3464 
3465 
3466 /* Set up various things so that we can make a list of input sections
3467    for each output section included in the link.  Returns -1 on error,
3468    0 when no stubs will be needed, and 1 on success.  It also sets
3469    information on the stubs bfd and the stub section in the info
3470    struct.  */
3471 
3472 int
3473 elf32_avr_setup_section_lists (bfd *output_bfd,
3474 			       struct bfd_link_info *info)
3475 {
3476   bfd *input_bfd;
3477   unsigned int bfd_count;
3478   unsigned int top_id, top_index;
3479   asection *section;
3480   asection **input_list, **list;
3481   bfd_size_type amt;
3482   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3483 
3484   if (htab == NULL || htab->no_stubs)
3485     return 0;
3486 
3487   /* Count the number of input BFDs and find the top input section id.  */
3488   for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3489        input_bfd != NULL;
3490        input_bfd = input_bfd->link.next)
3491     {
3492       bfd_count += 1;
3493       for (section = input_bfd->sections;
3494 	   section != NULL;
3495 	   section = section->next)
3496 	if (top_id < section->id)
3497 	  top_id = section->id;
3498     }
3499 
3500   htab->bfd_count = bfd_count;
3501 
3502   /* We can't use output_bfd->section_count here to find the top output
3503      section index as some sections may have been removed, and
3504      strip_excluded_output_sections doesn't renumber the indices.  */
3505   for (section = output_bfd->sections, top_index = 0;
3506        section != NULL;
3507        section = section->next)
3508     if (top_index < section->index)
3509       top_index = section->index;
3510 
3511   htab->top_index = top_index;
3512   amt = sizeof (asection *) * (top_index + 1);
3513   input_list = bfd_malloc (amt);
3514   htab->input_list = input_list;
3515   if (input_list == NULL)
3516     return -1;
3517 
3518   /* For sections we aren't interested in, mark their entries with a
3519      value we can check later.  */
3520   list = input_list + top_index;
3521   do
3522     *list = bfd_abs_section_ptr;
3523   while (list-- != input_list);
3524 
3525   for (section = output_bfd->sections;
3526        section != NULL;
3527        section = section->next)
3528     if ((section->flags & SEC_CODE) != 0)
3529       input_list[section->index] = NULL;
3530 
3531   return 1;
3532 }
3533 
3534 
3535 /* Read in all local syms for all input bfds, and create hash entries
3536    for export stubs if we are building a multi-subspace shared lib.
3537    Returns -1 on error, 0 otherwise.  */
3538 
3539 static int
3540 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
3541 {
3542   unsigned int bfd_indx;
3543   Elf_Internal_Sym *local_syms, **all_local_syms;
3544   struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3545   bfd_size_type amt;
3546 
3547   if (htab == NULL)
3548     return -1;
3549 
3550   /* We want to read in symbol extension records only once.  To do this
3551      we need to read in the local symbols in parallel and save them for
3552      later use; so hold pointers to the local symbols in an array.  */
3553   amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3554   all_local_syms = bfd_zmalloc (amt);
3555   htab->all_local_syms = all_local_syms;
3556   if (all_local_syms == NULL)
3557     return -1;
3558 
3559   /* Walk over all the input BFDs, swapping in local symbols.
3560      If we are creating a shared library, create hash entries for the
3561      export stubs.  */
3562   for (bfd_indx = 0;
3563        input_bfd != NULL;
3564        input_bfd = input_bfd->link.next, bfd_indx++)
3565     {
3566       Elf_Internal_Shdr *symtab_hdr;
3567 
3568       /* We'll need the symbol table in a second.  */
3569       symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3570       if (symtab_hdr->sh_info == 0)
3571 	continue;
3572 
3573       /* We need an array of the local symbols attached to the input bfd.  */
3574       local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3575       if (local_syms == NULL)
3576 	{
3577 	  local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3578 					     symtab_hdr->sh_info, 0,
3579 					     NULL, NULL, NULL);
3580 	  /* Cache them for elf_link_input_bfd.  */
3581 	  symtab_hdr->contents = (unsigned char *) local_syms;
3582 	}
3583       if (local_syms == NULL)
3584 	return -1;
3585 
3586       all_local_syms[bfd_indx] = local_syms;
3587     }
3588 
3589   return 0;
3590 }
3591 
3592 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
3593 
3594 bfd_boolean
3595 elf32_avr_size_stubs (bfd *output_bfd,
3596 		      struct bfd_link_info *info,
3597 		      bfd_boolean is_prealloc_run)
3598 {
3599   struct elf32_avr_link_hash_table *htab;
3600   int stub_changed = 0;
3601 
3602   htab = avr_link_hash_table (info);
3603   if (htab == NULL)
3604     return FALSE;
3605 
3606   /* At this point we initialize htab->vector_base
3607      To the start of the text output section.  */
3608   htab->vector_base = htab->stub_sec->output_section->vma;
3609 
3610   if (get_local_syms (info->input_bfds, info))
3611     {
3612       if (htab->all_local_syms)
3613 	goto error_ret_free_local;
3614       return FALSE;
3615     }
3616 
3617   if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
3618     {
3619       struct elf32_avr_stub_hash_entry *test;
3620 
3621       test = avr_add_stub ("Hugo",htab);
3622       test->target_value = 0x123456;
3623       test->stub_offset = 13;
3624 
3625       test = avr_add_stub ("Hugo2",htab);
3626       test->target_value = 0x84210;
3627       test->stub_offset = 14;
3628     }
3629 
3630   while (1)
3631     {
3632       bfd *input_bfd;
3633       unsigned int bfd_indx;
3634 
3635       /* We will have to re-generate the stub hash table each time anything
3636 	 in memory has changed.  */
3637 
3638       bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
3639       for (input_bfd = info->input_bfds, bfd_indx = 0;
3640 	   input_bfd != NULL;
3641 	   input_bfd = input_bfd->link.next, bfd_indx++)
3642 	{
3643 	  Elf_Internal_Shdr *symtab_hdr;
3644 	  asection *section;
3645 	  Elf_Internal_Sym *local_syms;
3646 
3647 	  /* We'll need the symbol table in a second.  */
3648 	  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3649 	  if (symtab_hdr->sh_info == 0)
3650 	    continue;
3651 
3652 	  local_syms = htab->all_local_syms[bfd_indx];
3653 
3654 	  /* Walk over each section attached to the input bfd.  */
3655 	  for (section = input_bfd->sections;
3656 	       section != NULL;
3657 	       section = section->next)
3658 	    {
3659 	      Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3660 
3661 	      /* If there aren't any relocs, then there's nothing more
3662 		 to do.  */
3663 	      if ((section->flags & SEC_RELOC) == 0
3664 		  || section->reloc_count == 0)
3665 		continue;
3666 
3667 	      /* If this section is a link-once section that will be
3668 		 discarded, then don't create any stubs.  */
3669 	      if (section->output_section == NULL
3670 		  || section->output_section->owner != output_bfd)
3671 		continue;
3672 
3673 	      /* Get the relocs.  */
3674 	      internal_relocs
3675 		= _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
3676 					     info->keep_memory);
3677 	      if (internal_relocs == NULL)
3678 		goto error_ret_free_local;
3679 
3680 	      /* Now examine each relocation.  */
3681 	      irela = internal_relocs;
3682 	      irelaend = irela + section->reloc_count;
3683 	      for (; irela < irelaend; irela++)
3684 		{
3685 		  unsigned int r_type, r_indx;
3686 		  struct elf32_avr_stub_hash_entry *hsh;
3687 		  asection *sym_sec;
3688 		  bfd_vma sym_value;
3689 		  bfd_vma destination;
3690 		  struct elf_link_hash_entry *hh;
3691 		  char *stub_name;
3692 
3693 		  r_type = ELF32_R_TYPE (irela->r_info);
3694 		  r_indx = ELF32_R_SYM (irela->r_info);
3695 
3696 		  /* Only look for 16 bit GS relocs. No other reloc will need a
3697 		     stub.  */
3698 		  if (!((r_type == R_AVR_16_PM)
3699 			|| (r_type == R_AVR_LO8_LDI_GS)
3700 			|| (r_type == R_AVR_HI8_LDI_GS)))
3701 		    continue;
3702 
3703 		  /* Now determine the call target, its name, value,
3704 		     section.  */
3705 		  sym_sec = NULL;
3706 		  sym_value = 0;
3707 		  destination = 0;
3708 		  hh = NULL;
3709 		  if (r_indx < symtab_hdr->sh_info)
3710 		    {
3711 		      /* It's a local symbol.  */
3712 		      Elf_Internal_Sym *sym;
3713 		      Elf_Internal_Shdr *hdr;
3714 		      unsigned int shndx;
3715 
3716 		      sym = local_syms + r_indx;
3717 		      if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3718 			sym_value = sym->st_value;
3719 		      shndx = sym->st_shndx;
3720 		      if (shndx < elf_numsections (input_bfd))
3721 			{
3722 			  hdr = elf_elfsections (input_bfd)[shndx];
3723 			  sym_sec = hdr->bfd_section;
3724 			  destination = (sym_value + irela->r_addend
3725 					 + sym_sec->output_offset
3726 					 + sym_sec->output_section->vma);
3727 			}
3728 		    }
3729 		  else
3730 		    {
3731 		      /* It's an external symbol.  */
3732 		      int e_indx;
3733 
3734 		      e_indx = r_indx - symtab_hdr->sh_info;
3735 		      hh = elf_sym_hashes (input_bfd)[e_indx];
3736 
3737 		      while (hh->root.type == bfd_link_hash_indirect
3738 			     || hh->root.type == bfd_link_hash_warning)
3739 			hh = (struct elf_link_hash_entry *)
3740 			      (hh->root.u.i.link);
3741 
3742 		      if (hh->root.type == bfd_link_hash_defined
3743 			  || hh->root.type == bfd_link_hash_defweak)
3744 			{
3745 			  sym_sec = hh->root.u.def.section;
3746 			  sym_value = hh->root.u.def.value;
3747 			  if (sym_sec->output_section != NULL)
3748 			  destination = (sym_value + irela->r_addend
3749 					 + sym_sec->output_offset
3750 					 + sym_sec->output_section->vma);
3751 			}
3752 		      else if (hh->root.type == bfd_link_hash_undefweak)
3753 			{
3754 			  if (! bfd_link_pic (info))
3755 			    continue;
3756 			}
3757 		      else if (hh->root.type == bfd_link_hash_undefined)
3758 			{
3759 			  if (! (info->unresolved_syms_in_objects == RM_IGNORE
3760 				 && (ELF_ST_VISIBILITY (hh->other)
3761 				     == STV_DEFAULT)))
3762 			     continue;
3763 			}
3764 		      else
3765 			{
3766 			  bfd_set_error (bfd_error_bad_value);
3767 
3768 			  error_ret_free_internal:
3769 			  if (elf_section_data (section)->relocs == NULL)
3770 			    free (internal_relocs);
3771 			  goto error_ret_free_local;
3772 			}
3773 		    }
3774 
3775 		  if (! avr_stub_is_required_for_16_bit_reloc
3776 		      (destination - htab->vector_base))
3777 		    {
3778 		      if (!is_prealloc_run)
3779 			/* We are having a reloc that does't need a stub.  */
3780 			continue;
3781 
3782 		      /* We don't right now know if a stub will be needed.
3783 			 Let's rather be on the safe side.  */
3784 		    }
3785 
3786 		  /* Get the name of this stub.  */
3787 		  stub_name = avr_stub_name (sym_sec, sym_value, irela);
3788 
3789 		  if (!stub_name)
3790 		    goto error_ret_free_internal;
3791 
3792 
3793 		  hsh = avr_stub_hash_lookup (&htab->bstab,
3794 					      stub_name,
3795 					      FALSE, FALSE);
3796 		  if (hsh != NULL)
3797 		    {
3798 		      /* The proper stub has already been created.  Mark it
3799 			 to be used and write the possibly changed destination
3800 			 value.  */
3801 		      hsh->is_actually_needed = TRUE;
3802 		      hsh->target_value = destination;
3803 		      free (stub_name);
3804 		      continue;
3805 		    }
3806 
3807 		  hsh = avr_add_stub (stub_name, htab);
3808 		  if (hsh == NULL)
3809 		    {
3810 		      free (stub_name);
3811 		      goto error_ret_free_internal;
3812 		    }
3813 
3814 		  hsh->is_actually_needed = TRUE;
3815 		  hsh->target_value = destination;
3816 
3817 		  if (debug_stubs)
3818 		    printf ("Adding stub with destination 0x%x to the"
3819 			    " hash table.\n", (unsigned int) destination);
3820 		  if (debug_stubs)
3821 		    printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
3822 
3823 		  stub_changed = TRUE;
3824 		}
3825 
3826 	      /* We're done with the internal relocs, free them.  */
3827 	      if (elf_section_data (section)->relocs == NULL)
3828 		free (internal_relocs);
3829 	    }
3830 	}
3831 
3832       /* Re-Calculate the number of needed stubs.  */
3833       htab->stub_sec->size = 0;
3834       bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
3835 
3836       if (!stub_changed)
3837 	break;
3838 
3839       stub_changed = FALSE;
3840     }
3841 
3842   free (htab->all_local_syms);
3843   return TRUE;
3844 
3845  error_ret_free_local:
3846   free (htab->all_local_syms);
3847   return FALSE;
3848 }
3849 
3850 
3851 /* Build all the stubs associated with the current output file.  The
3852    stubs are kept in a hash table attached to the main linker hash
3853    table.  We also set up the .plt entries for statically linked PIC
3854    functions here.  This function is called via hppaelf_finish in the
3855    linker.  */
3856 
3857 bfd_boolean
3858 elf32_avr_build_stubs (struct bfd_link_info *info)
3859 {
3860   asection *stub_sec;
3861   struct bfd_hash_table *table;
3862   struct elf32_avr_link_hash_table *htab;
3863   bfd_size_type total_size = 0;
3864 
3865   htab = avr_link_hash_table (info);
3866   if (htab == NULL)
3867     return FALSE;
3868 
3869   /* In case that there were several stub sections:  */
3870   for (stub_sec = htab->stub_bfd->sections;
3871        stub_sec != NULL;
3872        stub_sec = stub_sec->next)
3873     {
3874       bfd_size_type size;
3875 
3876       /* Allocate memory to hold the linker stubs.  */
3877       size = stub_sec->size;
3878       total_size += size;
3879 
3880       stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3881       if (stub_sec->contents == NULL && size != 0)
3882 	return FALSE;
3883       stub_sec->size = 0;
3884     }
3885 
3886   /* Allocate memory for the adress mapping table.  */
3887   htab->amt_entry_cnt = 0;
3888   htab->amt_max_entry_cnt = total_size / 4;
3889   htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
3890 				       * htab->amt_max_entry_cnt);
3891   htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
3892 					   * htab->amt_max_entry_cnt );
3893 
3894   if (debug_stubs)
3895     printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
3896 
3897   /* Build the stubs as directed by the stub hash table.  */
3898   table = &htab->bstab;
3899   bfd_hash_traverse (table, avr_build_one_stub, info);
3900 
3901   if (debug_stubs)
3902     printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
3903 
3904   return TRUE;
3905 }
3906 
3907 /* Callback used by QSORT to order relocations AP and BP.  */
3908 
3909 static int
3910 internal_reloc_compare (const void *ap, const void *bp)
3911 {
3912   const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
3913   const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
3914 
3915   if (a->r_offset != b->r_offset)
3916     return (a->r_offset - b->r_offset);
3917 
3918   /* We don't need to sort on these criteria for correctness,
3919      but enforcing a more strict ordering prevents unstable qsort
3920      from behaving differently with different implementations.
3921      Without the code below we get correct but different results
3922      on Solaris 2.7 and 2.8.  We would like to always produce the
3923      same results no matter the host.  */
3924 
3925   if (a->r_info != b->r_info)
3926     return (a->r_info - b->r_info);
3927 
3928   return (a->r_addend - b->r_addend);
3929 }
3930 
3931 /* Return true if ADDRESS is within the vma range of SECTION from ABFD.  */
3932 
3933 static bfd_boolean
3934 avr_is_section_for_address (bfd *abfd, asection *section, bfd_vma address)
3935 {
3936   bfd_vma vma;
3937   bfd_size_type size;
3938 
3939   vma = bfd_get_section_vma (abfd, section);
3940   if (address < vma)
3941     return FALSE;
3942 
3943   size = section->size;
3944   if (address >= vma + size)
3945     return FALSE;
3946 
3947   return TRUE;
3948 }
3949 
3950 /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS.  */
3951 
3952 struct avr_find_section_data
3953 {
3954   /* The address we're looking for.  */
3955   bfd_vma address;
3956 
3957   /* The section we've found.  */
3958   asection *section;
3959 };
3960 
3961 /* Helper function to locate the section holding a certain virtual memory
3962    address.  This is called via bfd_map_over_sections.  The DATA is an
3963    instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which
3964    has been set to the address to search for, and the section field has
3965    been set to NULL.  If SECTION from ABFD contains ADDRESS then the
3966    section field in DATA will be set to SECTION.  As an optimisation, if
3967    the section field is already non-null then this function does not
3968    perform any checks, and just returns.  */
3969 
3970 static void
3971 avr_find_section_for_address (bfd *abfd,
3972 			      asection *section, void *data)
3973 {
3974   struct avr_find_section_data *fs_data
3975     = (struct avr_find_section_data *) data;
3976 
3977   /* Return if already found.  */
3978   if (fs_data->section != NULL)
3979     return;
3980 
3981   /* If this section isn't part of the addressable code content, skip it.  */
3982   if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0
3983       && (bfd_get_section_flags (abfd, section) & SEC_CODE) == 0)
3984     return;
3985 
3986   if (avr_is_section_for_address (abfd, section, fs_data->address))
3987     fs_data->section = section;
3988 }
3989 
3990 /* Load all of the property records from SEC, a section from ABFD.  Return
3991    a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records.  The
3992    memory for the returned structure, and all of the records pointed too by
3993    the structure are allocated with a single call to malloc, so, only the
3994    pointer returned needs to be free'd.  */
3995 
3996 static struct avr_property_record_list *
3997 avr_elf32_load_records_from_section (bfd *abfd, asection *sec)
3998 {
3999   char *contents = NULL, *ptr;
4000   bfd_size_type size, mem_size;
4001   bfd_byte version, flags;
4002   uint16_t record_count, i;
4003   struct avr_property_record_list *r_list = NULL;
4004   Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end;
4005   struct avr_find_section_data fs_data;
4006 
4007   fs_data.section = NULL;
4008 
4009   size = bfd_get_section_size (sec);
4010   contents = bfd_malloc (size);
4011   bfd_get_section_contents (abfd, sec, contents, 0, size);
4012   ptr = contents;
4013 
4014   /* Load the relocations for the '.avr.prop' section if there are any, and
4015      sort them.  */
4016   internal_relocs = (_bfd_elf_link_read_relocs
4017 		     (abfd, sec, NULL, NULL, FALSE));
4018   if (internal_relocs)
4019     qsort (internal_relocs, sec->reloc_count,
4020 	   sizeof (Elf_Internal_Rela), internal_reloc_compare);
4021 
4022   /* There is a header at the start of the property record section SEC, the
4023      format of this header is:
4024        uint8_t  : version number
4025        uint8_t  : flags
4026        uint16_t : record counter
4027   */
4028 
4029   /* Check we have at least got a headers worth of bytes.  */
4030   if (size < AVR_PROPERTY_SECTION_HEADER_SIZE)
4031     goto load_failed;
4032 
4033   version = *((bfd_byte *) ptr);
4034   ptr++;
4035   flags = *((bfd_byte *) ptr);
4036   ptr++;
4037   record_count = *((uint16_t *) ptr);
4038   ptr+=2;
4039   BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE);
4040 
4041   /* Now allocate space for the list structure, and all of the list
4042      elements in a single block.  */
4043   mem_size = sizeof (struct avr_property_record_list)
4044     + sizeof (struct avr_property_record) * record_count;
4045   r_list = bfd_malloc (mem_size);
4046   if (r_list == NULL)
4047     goto load_failed;
4048 
4049   r_list->version = version;
4050   r_list->flags = flags;
4051   r_list->section = sec;
4052   r_list->record_count = record_count;
4053   r_list->records = (struct avr_property_record *) (&r_list [1]);
4054   size -= AVR_PROPERTY_SECTION_HEADER_SIZE;
4055 
4056   /* Check that we understand the version number.  There is only one
4057      version number right now, anything else is an error.  */
4058   if (r_list->version != AVR_PROPERTY_RECORDS_VERSION)
4059     goto load_failed;
4060 
4061   rel = internal_relocs;
4062   rel_end = rel + sec->reloc_count;
4063   for (i = 0; i < record_count; ++i)
4064     {
4065       bfd_vma address;
4066 
4067       /* Each entry is a 32-bit address, followed by a single byte type.
4068 	 After that is the type specific data.  We must take care to
4069 	 ensure that we don't read beyond the end of the section data.  */
4070       if (size < 5)
4071 	goto load_failed;
4072 
4073       r_list->records [i].section = NULL;
4074       r_list->records [i].offset = 0;
4075 
4076       if (rel)
4077 	{
4078 	  /* The offset of the address within the .avr.prop section.  */
4079 	  size_t offset = ptr - contents;
4080 
4081 	  while (rel < rel_end && rel->r_offset < offset)
4082 	    ++rel;
4083 
4084 	  if (rel == rel_end)
4085 	    rel = NULL;
4086 	  else if (rel->r_offset == offset)
4087 	    {
4088 	      /* Find section and section offset.  */
4089 	      unsigned long r_symndx;
4090 
4091 	      asection * rel_sec;
4092 	      bfd_vma sec_offset;
4093 
4094 	      r_symndx = ELF32_R_SYM (rel->r_info);
4095 	      rel_sec = get_elf_r_symndx_section (abfd, r_symndx);
4096 	      sec_offset = get_elf_r_symndx_offset (abfd, r_symndx)
4097 		+ rel->r_addend;
4098 
4099 	      r_list->records [i].section = rel_sec;
4100 	      r_list->records [i].offset = sec_offset;
4101 	    }
4102 	}
4103 
4104       address = *((uint32_t *) ptr);
4105       ptr += 4;
4106       size -= 4;
4107 
4108       if (r_list->records [i].section == NULL)
4109 	{
4110 	  /* Try to find section and offset from address.  */
4111 	  if (fs_data.section != NULL
4112 	      && !avr_is_section_for_address (abfd, fs_data.section,
4113 					      address))
4114 	    fs_data.section = NULL;
4115 
4116 	  if (fs_data.section == NULL)
4117 	    {
4118 	      fs_data.address = address;
4119 	      bfd_map_over_sections (abfd, avr_find_section_for_address,
4120 				     &fs_data);
4121 	    }
4122 
4123 	  if (fs_data.section == NULL)
4124 	    {
4125 	      fprintf (stderr, "Failed to find matching section.\n");
4126 	      goto load_failed;
4127 	    }
4128 
4129 	  r_list->records [i].section = fs_data.section;
4130 	  r_list->records [i].offset
4131 	    = address - bfd_get_section_vma (abfd, fs_data.section);
4132 	}
4133 
4134       r_list->records [i].type = *((bfd_byte *) ptr);
4135       ptr += 1;
4136       size -= 1;
4137 
4138       switch (r_list->records [i].type)
4139 	{
4140 	case RECORD_ORG:
4141 	  /* Nothing else to load.  */
4142 	  break;
4143 	case RECORD_ORG_AND_FILL:
4144 	  /* Just a 4-byte fill to load.  */
4145 	  if (size < 4)
4146 	    goto load_failed;
4147 	  r_list->records [i].data.org.fill = *((uint32_t *) ptr);
4148 	  ptr += 4;
4149 	  size -= 4;
4150 	  break;
4151 	case RECORD_ALIGN:
4152 	  /* Just a 4-byte alignment to load.  */
4153 	  if (size < 4)
4154 	    goto load_failed;
4155 	  r_list->records [i].data.align.bytes = *((uint32_t *) ptr);
4156 	  ptr += 4;
4157 	  size -= 4;
4158 	  /* Just initialise PRECEDING_DELETED field, this field is
4159 	     used during linker relaxation.  */
4160 	  r_list->records [i].data.align.preceding_deleted = 0;
4161 	  break;
4162 	case RECORD_ALIGN_AND_FILL:
4163 	  /* A 4-byte alignment, and a 4-byte fill to load.  */
4164 	  if (size < 8)
4165 	    goto load_failed;
4166 	  r_list->records [i].data.align.bytes = *((uint32_t *) ptr);
4167 	  ptr += 4;
4168 	  r_list->records [i].data.align.fill = *((uint32_t *) ptr);
4169 	  ptr += 4;
4170 	  size -= 8;
4171 	  /* Just initialise PRECEDING_DELETED field, this field is
4172 	     used during linker relaxation.  */
4173 	  r_list->records [i].data.align.preceding_deleted = 0;
4174 	  break;
4175 	default:
4176 	  goto load_failed;
4177 	}
4178     }
4179 
4180   free (contents);
4181   if (elf_section_data (sec)->relocs != internal_relocs)
4182     free (internal_relocs);
4183   return r_list;
4184 
4185  load_failed:
4186   if (elf_section_data (sec)->relocs != internal_relocs)
4187     free (internal_relocs);
4188   free (contents);
4189   free (r_list);
4190   return NULL;
4191 }
4192 
4193 /* Load all of the property records from ABFD.  See
4194    AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value.  */
4195 
4196 struct avr_property_record_list *
4197 avr_elf32_load_property_records (bfd *abfd)
4198 {
4199   asection *sec;
4200 
4201   /* Find the '.avr.prop' section and load the contents into memory.  */
4202   sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME);
4203   if (sec == NULL)
4204     return NULL;
4205   return avr_elf32_load_records_from_section (abfd, sec);
4206 }
4207 
4208 const char *
4209 avr_elf32_property_record_name (struct avr_property_record *rec)
4210 {
4211   const char *str;
4212 
4213   switch (rec->type)
4214     {
4215     case RECORD_ORG:
4216       str = "ORG";
4217       break;
4218     case RECORD_ORG_AND_FILL:
4219       str = "ORG+FILL";
4220       break;
4221     case RECORD_ALIGN:
4222       str = "ALIGN";
4223       break;
4224     case RECORD_ALIGN_AND_FILL:
4225       str = "ALIGN+FILL";
4226       break;
4227     default:
4228       str = "unknown";
4229     }
4230 
4231   return str;
4232 }
4233 
4234 
4235 #define ELF_ARCH		bfd_arch_avr
4236 #define ELF_TARGET_ID		AVR_ELF_DATA
4237 #define ELF_MACHINE_CODE	EM_AVR
4238 #define ELF_MACHINE_ALT1	EM_AVR_OLD
4239 #define ELF_MAXPAGESIZE		1
4240 
4241 #define TARGET_LITTLE_SYM	avr_elf32_vec
4242 #define TARGET_LITTLE_NAME	"elf32-avr"
4243 
4244 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
4245 
4246 #define elf_info_to_howto		     avr_info_to_howto_rela
4247 #define elf_info_to_howto_rel		     NULL
4248 #define elf_backend_relocate_section	     elf32_avr_relocate_section
4249 #define elf_backend_can_gc_sections	     1
4250 #define elf_backend_rela_normal		     1
4251 #define elf_backend_final_write_processing \
4252 					bfd_elf_avr_final_write_processing
4253 #define elf_backend_object_p		elf32_avr_object_p
4254 
4255 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
4256 #define bfd_elf32_bfd_get_relocated_section_contents \
4257 					elf32_avr_get_relocated_section_contents
4258 #define bfd_elf32_new_section_hook	elf_avr_new_section_hook
4259 
4260 #include "elf32-target.h"
4261