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