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