* readelf.c (do_archive_index): New.
[binutils.git] / bfd / elf32-avr.c
blobbb1746d3f6e237e775c9caf6581892a82f9a8ab2
1 /* AVR-specific support for 32-bit ELF
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006, 2007
3 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_tablse 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 ((p)->hash->table.newfunc != elf32_avr_link_hash_newfunc ? NULL : \
108 ((struct elf32_avr_link_hash_table *) ((p)->hash)))
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)))
622 free (htab);
623 return NULL;
626 /* Init the stub hash table too. */
627 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
628 sizeof (struct elf32_avr_stub_hash_entry)))
629 return NULL;
631 htab->stub_bfd = NULL;
632 htab->stub_sec = NULL;
634 /* Initialize the address mapping table. */
635 htab->amt_stub_offsets = NULL;
636 htab->amt_destination_addr = NULL;
637 htab->amt_entry_cnt = 0;
638 htab->amt_max_entry_cnt = 0;
640 return &htab->etab.root;
643 /* Free the derived linker hash table. */
645 static void
646 elf32_avr_link_hash_table_free (struct bfd_link_hash_table *btab)
648 struct elf32_avr_link_hash_table *htab
649 = (struct elf32_avr_link_hash_table *) btab;
651 /* Free the address mapping table. */
652 if (htab->amt_stub_offsets != NULL)
653 free (htab->amt_stub_offsets);
654 if (htab->amt_destination_addr != NULL)
655 free (htab->amt_destination_addr);
657 bfd_hash_table_free (&htab->bstab);
658 _bfd_generic_link_hash_table_free (btab);
661 /* Calculates the effective distance of a pc relative jump/call. */
663 static int
664 avr_relative_distance_considering_wrap_around (unsigned int distance)
666 unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
667 int dist_with_wrap_around = distance & wrap_around_mask;
669 if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
670 dist_with_wrap_around -= avr_pc_wrap_around;
672 return dist_with_wrap_around;
676 static reloc_howto_type *
677 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
678 bfd_reloc_code_real_type code)
680 unsigned int i;
682 for (i = 0;
683 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
684 i++)
685 if (avr_reloc_map[i].bfd_reloc_val == code)
686 return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
688 return NULL;
691 static reloc_howto_type *
692 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
693 const char *r_name)
695 unsigned int i;
697 for (i = 0;
698 i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
699 i++)
700 if (elf_avr_howto_table[i].name != NULL
701 && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
702 return &elf_avr_howto_table[i];
704 return NULL;
707 /* Set the howto pointer for an AVR ELF reloc. */
709 static void
710 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
711 arelent *cache_ptr,
712 Elf_Internal_Rela *dst)
714 unsigned int r_type;
716 r_type = ELF32_R_TYPE (dst->r_info);
717 BFD_ASSERT (r_type < (unsigned int) R_AVR_max);
718 cache_ptr->howto = &elf_avr_howto_table[r_type];
721 /* Look through the relocs for a section during the first phase.
722 Since we don't do .gots or .plts, we just need to consider the
723 virtual table relocs for gc. */
725 static bfd_boolean
726 elf32_avr_check_relocs (bfd *abfd,
727 struct bfd_link_info *info,
728 asection *sec,
729 const Elf_Internal_Rela *relocs)
731 Elf_Internal_Shdr *symtab_hdr;
732 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
733 const Elf_Internal_Rela *rel;
734 const Elf_Internal_Rela *rel_end;
736 if (info->relocatable)
737 return TRUE;
739 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
740 sym_hashes = elf_sym_hashes (abfd);
741 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
742 if (!elf_bad_symtab (abfd))
743 sym_hashes_end -= symtab_hdr->sh_info;
745 rel_end = relocs + sec->reloc_count;
746 for (rel = relocs; rel < rel_end; rel++)
748 struct elf_link_hash_entry *h;
749 unsigned long r_symndx;
751 r_symndx = ELF32_R_SYM (rel->r_info);
752 if (r_symndx < symtab_hdr->sh_info)
753 h = NULL;
754 else
756 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
757 while (h->root.type == bfd_link_hash_indirect
758 || h->root.type == bfd_link_hash_warning)
759 h = (struct elf_link_hash_entry *) h->root.u.i.link;
763 return TRUE;
766 static bfd_boolean
767 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
769 return (relocation >= 0x020000);
772 /* Returns the address of the corresponding stub if there is one.
773 Returns otherwise an address above 0x020000. This function
774 could also be used, if there is no knowledge on the section where
775 the destination is found. */
777 static bfd_vma
778 avr_get_stub_addr (bfd_vma srel,
779 struct elf32_avr_link_hash_table *htab)
781 unsigned int index;
782 bfd_vma stub_sec_addr =
783 (htab->stub_sec->output_section->vma +
784 htab->stub_sec->output_offset);
786 for (index = 0; index < htab->amt_max_entry_cnt; index ++)
787 if (htab->amt_destination_addr[index] == srel)
788 return htab->amt_stub_offsets[index] + stub_sec_addr;
790 /* Return an address that could not be reached by 16 bit relocs. */
791 return 0x020000;
794 /* Perform a single relocation. By default we use the standard BFD
795 routines, but a few relocs, we have to do them ourselves. */
797 static bfd_reloc_status_type
798 avr_final_link_relocate (reloc_howto_type * howto,
799 bfd * input_bfd,
800 asection * input_section,
801 bfd_byte * contents,
802 Elf_Internal_Rela * rel,
803 bfd_vma relocation,
804 struct elf32_avr_link_hash_table * htab)
806 bfd_reloc_status_type r = bfd_reloc_ok;
807 bfd_vma x;
808 bfd_signed_vma srel;
809 bfd_signed_vma reloc_addr;
810 bfd_boolean use_stubs = FALSE;
811 /* Usually is 0, unless we are generating code for a bootloader. */
812 bfd_signed_vma base_addr = htab->vector_base;
814 /* Absolute addr of the reloc in the final excecutable. */
815 reloc_addr = rel->r_offset + input_section->output_section->vma
816 + input_section->output_offset;
818 switch (howto->type)
820 case R_AVR_7_PCREL:
821 contents += rel->r_offset;
822 srel = (bfd_signed_vma) relocation;
823 srel += rel->r_addend;
824 srel -= rel->r_offset;
825 srel -= 2; /* Branch instructions add 2 to the PC... */
826 srel -= (input_section->output_section->vma +
827 input_section->output_offset);
829 if (srel & 1)
830 return bfd_reloc_outofrange;
831 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
832 return bfd_reloc_overflow;
833 x = bfd_get_16 (input_bfd, contents);
834 x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
835 bfd_put_16 (input_bfd, x, contents);
836 break;
838 case R_AVR_13_PCREL:
839 contents += rel->r_offset;
840 srel = (bfd_signed_vma) relocation;
841 srel += rel->r_addend;
842 srel -= rel->r_offset;
843 srel -= 2; /* Branch instructions add 2 to the PC... */
844 srel -= (input_section->output_section->vma +
845 input_section->output_offset);
847 if (srel & 1)
848 return bfd_reloc_outofrange;
850 srel = avr_relative_distance_considering_wrap_around (srel);
852 /* AVR addresses commands as words. */
853 srel >>= 1;
855 /* Check for overflow. */
856 if (srel < -2048 || srel > 2047)
858 /* Relative distance is too large. */
860 /* Always apply WRAPAROUND for avr2 and avr4. */
861 switch (bfd_get_mach (input_bfd))
863 case bfd_mach_avr2:
864 case bfd_mach_avr4:
865 break;
867 default:
868 return bfd_reloc_overflow;
872 x = bfd_get_16 (input_bfd, contents);
873 x = (x & 0xf000) | (srel & 0xfff);
874 bfd_put_16 (input_bfd, x, contents);
875 break;
877 case R_AVR_LO8_LDI:
878 contents += rel->r_offset;
879 srel = (bfd_signed_vma) relocation + rel->r_addend;
880 x = bfd_get_16 (input_bfd, contents);
881 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
882 bfd_put_16 (input_bfd, x, contents);
883 break;
885 case R_AVR_LDI:
886 contents += rel->r_offset;
887 srel = (bfd_signed_vma) relocation + rel->r_addend;
888 if (((srel > 0) && (srel & 0xffff) > 255)
889 || ((srel < 0) && ((-srel) & 0xffff) > 128))
890 /* Remove offset for data/eeprom section. */
891 return bfd_reloc_overflow;
893 x = bfd_get_16 (input_bfd, contents);
894 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
895 bfd_put_16 (input_bfd, x, contents);
896 break;
898 case R_AVR_6:
899 contents += rel->r_offset;
900 srel = (bfd_signed_vma) relocation + rel->r_addend;
901 if (((srel & 0xffff) > 63) || (srel < 0))
902 /* Remove offset for data/eeprom section. */
903 return bfd_reloc_overflow;
904 x = bfd_get_16 (input_bfd, contents);
905 x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
906 | ((srel & (1 << 5)) << 8));
907 bfd_put_16 (input_bfd, x, contents);
908 break;
910 case R_AVR_6_ADIW:
911 contents += rel->r_offset;
912 srel = (bfd_signed_vma) relocation + rel->r_addend;
913 if (((srel & 0xffff) > 63) || (srel < 0))
914 /* Remove offset for data/eeprom section. */
915 return bfd_reloc_overflow;
916 x = bfd_get_16 (input_bfd, contents);
917 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
918 bfd_put_16 (input_bfd, x, contents);
919 break;
921 case R_AVR_HI8_LDI:
922 contents += rel->r_offset;
923 srel = (bfd_signed_vma) relocation + rel->r_addend;
924 srel = (srel >> 8) & 0xff;
925 x = bfd_get_16 (input_bfd, contents);
926 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
927 bfd_put_16 (input_bfd, x, contents);
928 break;
930 case R_AVR_HH8_LDI:
931 contents += rel->r_offset;
932 srel = (bfd_signed_vma) relocation + rel->r_addend;
933 srel = (srel >> 16) & 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;
939 case R_AVR_MS8_LDI:
940 contents += rel->r_offset;
941 srel = (bfd_signed_vma) relocation + rel->r_addend;
942 srel = (srel >> 24) & 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;
948 case R_AVR_LO8_LDI_NEG:
949 contents += rel->r_offset;
950 srel = (bfd_signed_vma) relocation + rel->r_addend;
951 srel = -srel;
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;
957 case R_AVR_HI8_LDI_NEG:
958 contents += rel->r_offset;
959 srel = (bfd_signed_vma) relocation + rel->r_addend;
960 srel = -srel;
961 srel = (srel >> 8) & 0xff;
962 x = bfd_get_16 (input_bfd, contents);
963 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
964 bfd_put_16 (input_bfd, x, contents);
965 break;
967 case R_AVR_HH8_LDI_NEG:
968 contents += rel->r_offset;
969 srel = (bfd_signed_vma) relocation + rel->r_addend;
970 srel = -srel;
971 srel = (srel >> 16) & 0xff;
972 x = bfd_get_16 (input_bfd, contents);
973 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
974 bfd_put_16 (input_bfd, x, contents);
975 break;
977 case R_AVR_MS8_LDI_NEG:
978 contents += rel->r_offset;
979 srel = (bfd_signed_vma) relocation + rel->r_addend;
980 srel = -srel;
981 srel = (srel >> 24) & 0xff;
982 x = bfd_get_16 (input_bfd, contents);
983 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
984 bfd_put_16 (input_bfd, x, contents);
985 break;
987 case R_AVR_LO8_LDI_GS:
988 use_stubs = (!htab->no_stubs);
989 /* Fall through. */
990 case R_AVR_LO8_LDI_PM:
991 contents += rel->r_offset;
992 srel = (bfd_signed_vma) relocation + rel->r_addend;
994 if (use_stubs
995 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
997 bfd_vma old_srel = srel;
999 /* We need to use the address of the stub instead. */
1000 srel = avr_get_stub_addr (srel, htab);
1001 if (debug_stubs)
1002 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1003 "reloc at address 0x%x.\n",
1004 (unsigned int) srel,
1005 (unsigned int) old_srel,
1006 (unsigned int) reloc_addr);
1008 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1009 return bfd_reloc_outofrange;
1012 if (srel & 1)
1013 return bfd_reloc_outofrange;
1014 srel = srel >> 1;
1015 x = bfd_get_16 (input_bfd, contents);
1016 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1017 bfd_put_16 (input_bfd, x, contents);
1018 break;
1020 case R_AVR_HI8_LDI_GS:
1021 use_stubs = (!htab->no_stubs);
1022 /* Fall through. */
1023 case R_AVR_HI8_LDI_PM:
1024 contents += rel->r_offset;
1025 srel = (bfd_signed_vma) relocation + rel->r_addend;
1027 if (use_stubs
1028 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1030 bfd_vma old_srel = srel;
1032 /* We need to use the address of the stub instead. */
1033 srel = avr_get_stub_addr (srel, htab);
1034 if (debug_stubs)
1035 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1036 "reloc at address 0x%x.\n",
1037 (unsigned int) srel,
1038 (unsigned int) old_srel,
1039 (unsigned int) reloc_addr);
1041 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1042 return bfd_reloc_outofrange;
1045 if (srel & 1)
1046 return bfd_reloc_outofrange;
1047 srel = srel >> 1;
1048 srel = (srel >> 8) & 0xff;
1049 x = bfd_get_16 (input_bfd, contents);
1050 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1051 bfd_put_16 (input_bfd, x, contents);
1052 break;
1054 case R_AVR_HH8_LDI_PM:
1055 contents += rel->r_offset;
1056 srel = (bfd_signed_vma) relocation + rel->r_addend;
1057 if (srel & 1)
1058 return bfd_reloc_outofrange;
1059 srel = srel >> 1;
1060 srel = (srel >> 16) & 0xff;
1061 x = bfd_get_16 (input_bfd, contents);
1062 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1063 bfd_put_16 (input_bfd, x, contents);
1064 break;
1066 case R_AVR_LO8_LDI_PM_NEG:
1067 contents += rel->r_offset;
1068 srel = (bfd_signed_vma) relocation + rel->r_addend;
1069 srel = -srel;
1070 if (srel & 1)
1071 return bfd_reloc_outofrange;
1072 srel = srel >> 1;
1073 x = bfd_get_16 (input_bfd, contents);
1074 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1075 bfd_put_16 (input_bfd, x, contents);
1076 break;
1078 case R_AVR_HI8_LDI_PM_NEG:
1079 contents += rel->r_offset;
1080 srel = (bfd_signed_vma) relocation + rel->r_addend;
1081 srel = -srel;
1082 if (srel & 1)
1083 return bfd_reloc_outofrange;
1084 srel = srel >> 1;
1085 srel = (srel >> 8) & 0xff;
1086 x = bfd_get_16 (input_bfd, contents);
1087 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1088 bfd_put_16 (input_bfd, x, contents);
1089 break;
1091 case R_AVR_HH8_LDI_PM_NEG:
1092 contents += rel->r_offset;
1093 srel = (bfd_signed_vma) relocation + rel->r_addend;
1094 srel = -srel;
1095 if (srel & 1)
1096 return bfd_reloc_outofrange;
1097 srel = srel >> 1;
1098 srel = (srel >> 16) & 0xff;
1099 x = bfd_get_16 (input_bfd, contents);
1100 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1101 bfd_put_16 (input_bfd, x, contents);
1102 break;
1104 case R_AVR_CALL:
1105 contents += rel->r_offset;
1106 srel = (bfd_signed_vma) relocation + rel->r_addend;
1107 if (srel & 1)
1108 return bfd_reloc_outofrange;
1109 srel = srel >> 1;
1110 x = bfd_get_16 (input_bfd, contents);
1111 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1112 bfd_put_16 (input_bfd, x, contents);
1113 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1114 break;
1116 case R_AVR_16_PM:
1117 use_stubs = (!htab->no_stubs);
1118 contents += rel->r_offset;
1119 srel = (bfd_signed_vma) relocation + rel->r_addend;
1121 if (use_stubs
1122 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1124 bfd_vma old_srel = srel;
1126 /* We need to use the address of the stub instead. */
1127 srel = avr_get_stub_addr (srel,htab);
1128 if (debug_stubs)
1129 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1130 "reloc at address 0x%x.\n",
1131 (unsigned int) srel,
1132 (unsigned int) old_srel,
1133 (unsigned int) reloc_addr);
1135 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1136 return bfd_reloc_outofrange;
1139 if (srel & 1)
1140 return bfd_reloc_outofrange;
1141 srel = srel >> 1;
1142 bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1143 break;
1145 default:
1146 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1147 contents, rel->r_offset,
1148 relocation, rel->r_addend);
1151 return r;
1154 /* Relocate an AVR ELF section. */
1156 static bfd_boolean
1157 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1158 struct bfd_link_info *info,
1159 bfd *input_bfd,
1160 asection *input_section,
1161 bfd_byte *contents,
1162 Elf_Internal_Rela *relocs,
1163 Elf_Internal_Sym *local_syms,
1164 asection **local_sections)
1166 Elf_Internal_Shdr * symtab_hdr;
1167 struct elf_link_hash_entry ** sym_hashes;
1168 Elf_Internal_Rela * rel;
1169 Elf_Internal_Rela * relend;
1170 struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
1172 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1173 sym_hashes = elf_sym_hashes (input_bfd);
1174 relend = relocs + input_section->reloc_count;
1176 for (rel = relocs; rel < relend; rel ++)
1178 reloc_howto_type * howto;
1179 unsigned long r_symndx;
1180 Elf_Internal_Sym * sym;
1181 asection * sec;
1182 struct elf_link_hash_entry * h;
1183 bfd_vma relocation;
1184 bfd_reloc_status_type r;
1185 const char * name;
1186 int r_type;
1188 r_type = ELF32_R_TYPE (rel->r_info);
1189 r_symndx = ELF32_R_SYM (rel->r_info);
1190 howto = elf_avr_howto_table + ELF32_R_TYPE (rel->r_info);
1191 h = NULL;
1192 sym = NULL;
1193 sec = NULL;
1195 if (r_symndx < symtab_hdr->sh_info)
1197 sym = local_syms + r_symndx;
1198 sec = local_sections [r_symndx];
1199 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1201 name = bfd_elf_string_from_elf_section
1202 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1203 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1205 else
1207 bfd_boolean unresolved_reloc, warned;
1209 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1210 r_symndx, symtab_hdr, sym_hashes,
1211 h, sec, relocation,
1212 unresolved_reloc, warned);
1214 name = h->root.root.string;
1217 if (sec != NULL && elf_discarded_section (sec))
1219 /* For relocs against symbols from removed linkonce sections,
1220 or sections discarded by a linker script, we just want the
1221 section contents zeroed. Avoid any special processing. */
1222 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1223 rel->r_info = 0;
1224 rel->r_addend = 0;
1225 continue;
1228 if (info->relocatable)
1229 continue;
1231 r = avr_final_link_relocate (howto, input_bfd, input_section,
1232 contents, rel, relocation, htab);
1234 if (r != bfd_reloc_ok)
1236 const char * msg = (const char *) NULL;
1238 switch (r)
1240 case bfd_reloc_overflow:
1241 r = info->callbacks->reloc_overflow
1242 (info, (h ? &h->root : NULL),
1243 name, howto->name, (bfd_vma) 0,
1244 input_bfd, input_section, rel->r_offset);
1245 break;
1247 case bfd_reloc_undefined:
1248 r = info->callbacks->undefined_symbol
1249 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1250 break;
1252 case bfd_reloc_outofrange:
1253 msg = _("internal error: out of range error");
1254 break;
1256 case bfd_reloc_notsupported:
1257 msg = _("internal error: unsupported relocation error");
1258 break;
1260 case bfd_reloc_dangerous:
1261 msg = _("internal error: dangerous relocation");
1262 break;
1264 default:
1265 msg = _("internal error: unknown error");
1266 break;
1269 if (msg)
1270 r = info->callbacks->warning
1271 (info, msg, name, input_bfd, input_section, rel->r_offset);
1273 if (! r)
1274 return FALSE;
1278 return TRUE;
1281 /* The final processing done just before writing out a AVR ELF object
1282 file. This gets the AVR architecture right based on the machine
1283 number. */
1285 static void
1286 bfd_elf_avr_final_write_processing (bfd *abfd,
1287 bfd_boolean linker ATTRIBUTE_UNUSED)
1289 unsigned long val;
1291 switch (bfd_get_mach (abfd))
1293 default:
1294 case bfd_mach_avr2:
1295 val = E_AVR_MACH_AVR2;
1296 break;
1298 case bfd_mach_avr1:
1299 val = E_AVR_MACH_AVR1;
1300 break;
1302 case bfd_mach_avr3:
1303 val = E_AVR_MACH_AVR3;
1304 break;
1306 case bfd_mach_avr4:
1307 val = E_AVR_MACH_AVR4;
1308 break;
1310 case bfd_mach_avr5:
1311 val = E_AVR_MACH_AVR5;
1312 break;
1314 case bfd_mach_avr6:
1315 val = E_AVR_MACH_AVR6;
1316 break;
1319 elf_elfheader (abfd)->e_machine = EM_AVR;
1320 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1321 elf_elfheader (abfd)->e_flags |= val;
1322 elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
1325 /* Set the right machine number. */
1327 static bfd_boolean
1328 elf32_avr_object_p (bfd *abfd)
1330 unsigned int e_set = bfd_mach_avr2;
1332 if (elf_elfheader (abfd)->e_machine == EM_AVR
1333 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1335 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1337 switch (e_mach)
1339 default:
1340 case E_AVR_MACH_AVR2:
1341 e_set = bfd_mach_avr2;
1342 break;
1344 case E_AVR_MACH_AVR1:
1345 e_set = bfd_mach_avr1;
1346 break;
1348 case E_AVR_MACH_AVR3:
1349 e_set = bfd_mach_avr3;
1350 break;
1352 case E_AVR_MACH_AVR4:
1353 e_set = bfd_mach_avr4;
1354 break;
1356 case E_AVR_MACH_AVR5:
1357 e_set = bfd_mach_avr5;
1358 break;
1360 case E_AVR_MACH_AVR6:
1361 e_set = bfd_mach_avr6;
1362 break;
1365 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1366 e_set);
1370 /* Delete some bytes from a section while changing the size of an instruction.
1371 The parameter "addr" denotes the section-relative offset pointing just
1372 behind the shrinked instruction. "addr+count" point at the first
1373 byte just behind the original unshrinked instruction. */
1375 static bfd_boolean
1376 elf32_avr_relax_delete_bytes (bfd *abfd,
1377 asection *sec,
1378 bfd_vma addr,
1379 int count)
1381 Elf_Internal_Shdr *symtab_hdr;
1382 unsigned int sec_shndx;
1383 bfd_byte *contents;
1384 Elf_Internal_Rela *irel, *irelend;
1385 Elf_Internal_Rela *irelalign;
1386 Elf_Internal_Sym *isym;
1387 Elf_Internal_Sym *isymbuf = NULL;
1388 Elf_Internal_Sym *isymend;
1389 bfd_vma toaddr;
1390 struct elf_link_hash_entry **sym_hashes;
1391 struct elf_link_hash_entry **end_hashes;
1392 unsigned int symcount;
1394 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1395 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1396 contents = elf_section_data (sec)->this_hdr.contents;
1398 /* The deletion must stop at the next ALIGN reloc for an aligment
1399 power larger than the number of bytes we are deleting. */
1401 irelalign = NULL;
1402 toaddr = sec->size;
1404 irel = elf_section_data (sec)->relocs;
1405 irelend = irel + sec->reloc_count;
1407 /* Actually delete the bytes. */
1408 if (toaddr - addr - count > 0)
1409 memmove (contents + addr, contents + addr + count,
1410 (size_t) (toaddr - addr - count));
1411 sec->size -= count;
1413 /* Adjust all the reloc addresses. */
1414 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1416 bfd_vma old_reloc_address;
1417 bfd_vma shrinked_insn_address;
1419 old_reloc_address = (sec->output_section->vma
1420 + sec->output_offset + irel->r_offset);
1421 shrinked_insn_address = (sec->output_section->vma
1422 + sec->output_offset + addr - count);
1424 /* Get the new reloc address. */
1425 if ((irel->r_offset > addr
1426 && irel->r_offset < toaddr))
1428 if (debug_relax)
1429 printf ("Relocation at address 0x%x needs to be moved.\n"
1430 "Old section offset: 0x%x, New section offset: 0x%x \n",
1431 (unsigned int) old_reloc_address,
1432 (unsigned int) irel->r_offset,
1433 (unsigned int) ((irel->r_offset) - count));
1435 irel->r_offset -= count;
1440 /* The reloc's own addresses are now ok. However, we need to readjust
1441 the reloc's addend, i.e. the reloc's value if two conditions are met:
1442 1.) the reloc is relative to a symbol in this section that
1443 is located in front of the shrinked instruction
1444 2.) symbol plus addend end up behind the shrinked instruction.
1446 The most common case where this happens are relocs relative to
1447 the section-start symbol.
1449 This step needs to be done for all of the sections of the bfd. */
1452 struct bfd_section *isec;
1454 for (isec = abfd->sections; isec; isec = isec->next)
1456 bfd_vma symval;
1457 bfd_vma shrinked_insn_address;
1459 shrinked_insn_address = (sec->output_section->vma
1460 + sec->output_offset + addr - count);
1462 irelend = elf_section_data (isec)->relocs + isec->reloc_count;
1463 for (irel = elf_section_data (isec)->relocs;
1464 irel < irelend;
1465 irel++)
1467 /* Read this BFD's local symbols if we haven't done
1468 so already. */
1469 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1471 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1472 if (isymbuf == NULL)
1473 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1474 symtab_hdr->sh_info, 0,
1475 NULL, NULL, NULL);
1476 if (isymbuf == NULL)
1477 return FALSE;
1480 /* Get the value of the symbol referred to by the reloc. */
1481 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1483 /* A local symbol. */
1484 Elf_Internal_Sym *isym;
1485 asection *sym_sec;
1487 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1488 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1489 symval = isym->st_value;
1490 /* If the reloc is absolute, it will not have
1491 a symbol or section associated with it. */
1492 if (sym_sec == sec)
1494 symval += sym_sec->output_section->vma
1495 + sym_sec->output_offset;
1497 if (debug_relax)
1498 printf ("Checking if the relocation's "
1499 "addend needs corrections.\n"
1500 "Address of anchor symbol: 0x%x \n"
1501 "Address of relocation target: 0x%x \n"
1502 "Address of relaxed insn: 0x%x \n",
1503 (unsigned int) symval,
1504 (unsigned int) (symval + irel->r_addend),
1505 (unsigned int) shrinked_insn_address);
1507 if (symval <= shrinked_insn_address
1508 && (symval + irel->r_addend) > shrinked_insn_address)
1510 irel->r_addend -= count;
1512 if (debug_relax)
1513 printf ("Relocation's addend needed to be fixed \n");
1516 /* else...Reference symbol is absolute. No adjustment needed. */
1518 /* else...Reference symbol is extern. No need for adjusting
1519 the addend. */
1524 /* Adjust the local symbols defined in this section. */
1525 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1526 isymend = isym + symtab_hdr->sh_info;
1527 for (; isym < isymend; isym++)
1529 if (isym->st_shndx == sec_shndx
1530 && isym->st_value > addr
1531 && isym->st_value < toaddr)
1532 isym->st_value -= count;
1535 /* Now adjust the global symbols defined in this section. */
1536 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1537 - symtab_hdr->sh_info);
1538 sym_hashes = elf_sym_hashes (abfd);
1539 end_hashes = sym_hashes + symcount;
1540 for (; sym_hashes < end_hashes; sym_hashes++)
1542 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1543 if ((sym_hash->root.type == bfd_link_hash_defined
1544 || sym_hash->root.type == bfd_link_hash_defweak)
1545 && sym_hash->root.u.def.section == sec
1546 && sym_hash->root.u.def.value > addr
1547 && sym_hash->root.u.def.value < toaddr)
1549 sym_hash->root.u.def.value -= count;
1553 return TRUE;
1556 /* This function handles relaxing for the avr.
1557 Many important relaxing opportunities within functions are already
1558 realized by the compiler itself.
1559 Here we try to replace call (4 bytes) -> rcall (2 bytes)
1560 and jump -> rjmp (safes also 2 bytes).
1561 As well we now optimize seqences of
1562 - call/rcall function
1563 - ret
1564 to yield
1565 - jmp/rjmp function
1566 - ret
1567 . In case that within a sequence
1568 - jmp/rjmp label
1569 - ret
1570 the ret could no longer be reached it is optimized away. In order
1571 to check if the ret is no longer needed, it is checked that the ret's address
1572 is not the target of a branch or jump within the same section, it is checked
1573 that there is no skip instruction before the jmp/rjmp and that there
1574 is no local or global label place at the address of the ret.
1576 We refrain from relaxing within sections ".vectors" and
1577 ".jumptables" in order to maintain the position of the instructions.
1578 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
1579 if possible. (In future one could possibly use the space of the nop
1580 for the first instruction of the irq service function.
1582 The .jumptables sections is meant to be used for a future tablejump variant
1583 for the devices with 3-byte program counter where the table itself
1584 contains 4-byte jump instructions whose relative offset must not
1585 be changed. */
1587 static bfd_boolean
1588 elf32_avr_relax_section (bfd *abfd,
1589 asection *sec,
1590 struct bfd_link_info *link_info,
1591 bfd_boolean *again)
1593 Elf_Internal_Shdr *symtab_hdr;
1594 Elf_Internal_Rela *internal_relocs;
1595 Elf_Internal_Rela *irel, *irelend;
1596 bfd_byte *contents = NULL;
1597 Elf_Internal_Sym *isymbuf = NULL;
1598 static asection *last_input_section = NULL;
1599 static Elf_Internal_Rela *last_reloc = NULL;
1600 struct elf32_avr_link_hash_table *htab;
1602 htab = avr_link_hash_table (link_info);
1603 if (htab == NULL)
1604 return FALSE;
1606 /* Assume nothing changes. */
1607 *again = FALSE;
1609 if ((!htab->no_stubs) && (sec == htab->stub_sec))
1611 /* We are just relaxing the stub section.
1612 Let's calculate the size needed again. */
1613 bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
1615 if (debug_relax)
1616 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
1617 (int) last_estimated_stub_section_size);
1619 elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
1620 link_info, FALSE);
1622 /* Check if the number of trampolines changed. */
1623 if (last_estimated_stub_section_size != htab->stub_sec->size)
1624 *again = TRUE;
1626 if (debug_relax)
1627 printf ("Size of stub section after this pass: %i\n",
1628 (int) htab->stub_sec->size);
1630 return TRUE;
1633 /* We don't have to do anything for a relocatable link, if
1634 this section does not have relocs, or if this is not a
1635 code section. */
1636 if (link_info->relocatable
1637 || (sec->flags & SEC_RELOC) == 0
1638 || sec->reloc_count == 0
1639 || (sec->flags & SEC_CODE) == 0)
1640 return TRUE;
1642 /* Check if the object file to relax uses internal symbols so that we
1643 could fix up the relocations. */
1644 if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
1645 return TRUE;
1647 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1649 /* Get a copy of the native relocations. */
1650 internal_relocs = (_bfd_elf_link_read_relocs
1651 (abfd, sec, NULL, NULL, link_info->keep_memory));
1652 if (internal_relocs == NULL)
1653 goto error_return;
1655 if (sec != last_input_section)
1656 last_reloc = NULL;
1658 last_input_section = sec;
1660 /* Walk through the relocs looking for relaxing opportunities. */
1661 irelend = internal_relocs + sec->reloc_count;
1662 for (irel = internal_relocs; irel < irelend; irel++)
1664 bfd_vma symval;
1666 if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
1667 && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
1668 && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
1669 continue;
1671 /* Get the section contents if we haven't done so already. */
1672 if (contents == NULL)
1674 /* Get cached copy if it exists. */
1675 if (elf_section_data (sec)->this_hdr.contents != NULL)
1676 contents = elf_section_data (sec)->this_hdr.contents;
1677 else
1679 /* Go get them off disk. */
1680 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
1681 goto error_return;
1685 /* Read this BFD's local symbols if we haven't done so already. */
1686 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1688 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1689 if (isymbuf == NULL)
1690 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1691 symtab_hdr->sh_info, 0,
1692 NULL, NULL, NULL);
1693 if (isymbuf == NULL)
1694 goto error_return;
1698 /* Get the value of the symbol referred to by the reloc. */
1699 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1701 /* A local symbol. */
1702 Elf_Internal_Sym *isym;
1703 asection *sym_sec;
1705 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1706 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1707 symval = isym->st_value;
1708 /* If the reloc is absolute, it will not have
1709 a symbol or section associated with it. */
1710 if (sym_sec)
1711 symval += sym_sec->output_section->vma
1712 + sym_sec->output_offset;
1714 else
1716 unsigned long indx;
1717 struct elf_link_hash_entry *h;
1719 /* An external symbol. */
1720 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1721 h = elf_sym_hashes (abfd)[indx];
1722 BFD_ASSERT (h != NULL);
1723 if (h->root.type != bfd_link_hash_defined
1724 && h->root.type != bfd_link_hash_defweak)
1725 /* This appears to be a reference to an undefined
1726 symbol. Just ignore it--it will be caught by the
1727 regular reloc processing. */
1728 continue;
1730 symval = (h->root.u.def.value
1731 + h->root.u.def.section->output_section->vma
1732 + h->root.u.def.section->output_offset);
1735 /* For simplicity of coding, we are going to modify the section
1736 contents, the section relocs, and the BFD symbol table. We
1737 must tell the rest of the code not to free up this
1738 information. It would be possible to instead create a table
1739 of changes which have to be made, as is done in coff-mips.c;
1740 that would be more work, but would require less memory when
1741 the linker is run. */
1742 switch (ELF32_R_TYPE (irel->r_info))
1744 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1745 pc-relative rcall/rjmp. */
1746 case R_AVR_CALL:
1748 bfd_vma value = symval + irel->r_addend;
1749 bfd_vma dot, gap;
1750 int distance_short_enough = 0;
1752 /* Get the address of this instruction. */
1753 dot = (sec->output_section->vma
1754 + sec->output_offset + irel->r_offset);
1756 /* Compute the distance from this insn to the branch target. */
1757 gap = value - dot;
1759 /* If the distance is within -4094..+4098 inclusive, then we can
1760 relax this jump/call. +4098 because the call/jump target
1761 will be closer after the relaxation. */
1762 if ((int) gap >= -4094 && (int) gap <= 4098)
1763 distance_short_enough = 1;
1765 /* Here we handle the wrap-around case. E.g. for a 16k device
1766 we could use a rjmp to jump from address 0x100 to 0x3d00!
1767 In order to make this work properly, we need to fill the
1768 vaiable avr_pc_wrap_around with the appropriate value.
1769 I.e. 0x4000 for a 16k device. */
1771 /* Shrinking the code size makes the gaps larger in the
1772 case of wrap-arounds. So we use a heuristical safety
1773 margin to avoid that during relax the distance gets
1774 again too large for the short jumps. Let's assume
1775 a typical code-size reduction due to relax for a
1776 16k device of 600 bytes. So let's use twice the
1777 typical value as safety margin. */
1778 int rgap;
1779 int safety_margin;
1781 int assumed_shrink = 600;
1782 if (avr_pc_wrap_around > 0x4000)
1783 assumed_shrink = 900;
1785 safety_margin = 2 * assumed_shrink;
1787 rgap = avr_relative_distance_considering_wrap_around (gap);
1789 if (rgap >= (-4092 + safety_margin)
1790 && rgap <= (4094 - safety_margin))
1791 distance_short_enough = 1;
1794 if (distance_short_enough)
1796 unsigned char code_msb;
1797 unsigned char code_lsb;
1799 if (debug_relax)
1800 printf ("shrinking jump/call instruction at address 0x%x"
1801 " in section %s\n\n",
1802 (int) dot, sec->name);
1804 /* Note that we've changed the relocs, section contents,
1805 etc. */
1806 elf_section_data (sec)->relocs = internal_relocs;
1807 elf_section_data (sec)->this_hdr.contents = contents;
1808 symtab_hdr->contents = (unsigned char *) isymbuf;
1810 /* Get the instruction code for relaxing. */
1811 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
1812 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1814 /* Mask out the relocation bits. */
1815 code_msb &= 0x94;
1816 code_lsb &= 0x0E;
1817 if (code_msb == 0x94 && code_lsb == 0x0E)
1819 /* we are changing call -> rcall . */
1820 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1821 bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
1823 else if (code_msb == 0x94 && code_lsb == 0x0C)
1825 /* we are changeing jump -> rjmp. */
1826 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1827 bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
1829 else
1830 abort ();
1832 /* Fix the relocation's type. */
1833 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1834 R_AVR_13_PCREL);
1836 /* Check for the vector section. There we don't want to
1837 modify the ordering! */
1839 if (!strcmp (sec->name,".vectors")
1840 || !strcmp (sec->name,".jumptables"))
1842 /* Let's insert a nop. */
1843 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
1844 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
1846 else
1848 /* Delete two bytes of data. */
1849 if (!elf32_avr_relax_delete_bytes (abfd, sec,
1850 irel->r_offset + 2, 2))
1851 goto error_return;
1853 /* That will change things, so, we should relax again.
1854 Note that this is not required, and it may be slow. */
1855 *again = TRUE;
1860 default:
1862 unsigned char code_msb;
1863 unsigned char code_lsb;
1864 bfd_vma dot;
1866 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1867 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
1869 /* Get the address of this instruction. */
1870 dot = (sec->output_section->vma
1871 + sec->output_offset + irel->r_offset);
1873 /* Here we look for rcall/ret or call/ret sequences that could be
1874 safely replaced by rjmp/ret or jmp/ret. */
1875 if (((code_msb & 0xf0) == 0xd0)
1876 && avr_replace_call_ret_sequences)
1878 /* This insn is a rcall. */
1879 unsigned char next_insn_msb = 0;
1880 unsigned char next_insn_lsb = 0;
1882 if (irel->r_offset + 3 < sec->size)
1884 next_insn_msb =
1885 bfd_get_8 (abfd, contents + irel->r_offset + 3);
1886 next_insn_lsb =
1887 bfd_get_8 (abfd, contents + irel->r_offset + 2);
1890 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1892 /* The next insn is a ret. We now convert the rcall insn
1893 into a rjmp instruction. */
1894 code_msb &= 0xef;
1895 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
1896 if (debug_relax)
1897 printf ("converted rcall/ret sequence at address 0x%x"
1898 " into rjmp/ret sequence. Section is %s\n\n",
1899 (int) dot, sec->name);
1900 *again = TRUE;
1901 break;
1904 else if ((0x94 == (code_msb & 0xfe))
1905 && (0x0e == (code_lsb & 0x0e))
1906 && avr_replace_call_ret_sequences)
1908 /* This insn is a call. */
1909 unsigned char next_insn_msb = 0;
1910 unsigned char next_insn_lsb = 0;
1912 if (irel->r_offset + 5 < sec->size)
1914 next_insn_msb =
1915 bfd_get_8 (abfd, contents + irel->r_offset + 5);
1916 next_insn_lsb =
1917 bfd_get_8 (abfd, contents + irel->r_offset + 4);
1920 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1922 /* The next insn is a ret. We now convert the call insn
1923 into a jmp instruction. */
1925 code_lsb &= 0xfd;
1926 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
1927 if (debug_relax)
1928 printf ("converted call/ret sequence at address 0x%x"
1929 " into jmp/ret sequence. Section is %s\n\n",
1930 (int) dot, sec->name);
1931 *again = TRUE;
1932 break;
1935 else if ((0xc0 == (code_msb & 0xf0))
1936 || ((0x94 == (code_msb & 0xfe))
1937 && (0x0c == (code_lsb & 0x0e))))
1939 /* This insn is a rjmp or a jmp. */
1940 unsigned char next_insn_msb = 0;
1941 unsigned char next_insn_lsb = 0;
1942 int insn_size;
1944 if (0xc0 == (code_msb & 0xf0))
1945 insn_size = 2; /* rjmp insn */
1946 else
1947 insn_size = 4; /* jmp insn */
1949 if (irel->r_offset + insn_size + 1 < sec->size)
1951 next_insn_msb =
1952 bfd_get_8 (abfd, contents + irel->r_offset
1953 + insn_size + 1);
1954 next_insn_lsb =
1955 bfd_get_8 (abfd, contents + irel->r_offset
1956 + insn_size);
1959 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1961 /* The next insn is a ret. We possibly could delete
1962 this ret. First we need to check for preceeding
1963 sbis/sbic/sbrs or cpse "skip" instructions. */
1965 int there_is_preceeding_non_skip_insn = 1;
1966 bfd_vma address_of_ret;
1968 address_of_ret = dot + insn_size;
1970 if (debug_relax && (insn_size == 2))
1971 printf ("found rjmp / ret sequence at address 0x%x\n",
1972 (int) dot);
1973 if (debug_relax && (insn_size == 4))
1974 printf ("found jmp / ret sequence at address 0x%x\n",
1975 (int) dot);
1977 /* We have to make sure that there is a preceeding insn. */
1978 if (irel->r_offset >= 2)
1980 unsigned char preceeding_msb;
1981 unsigned char preceeding_lsb;
1982 preceeding_msb =
1983 bfd_get_8 (abfd, contents + irel->r_offset - 1);
1984 preceeding_lsb =
1985 bfd_get_8 (abfd, contents + irel->r_offset - 2);
1987 /* sbic. */
1988 if (0x99 == preceeding_msb)
1989 there_is_preceeding_non_skip_insn = 0;
1991 /* sbis. */
1992 if (0x9b == preceeding_msb)
1993 there_is_preceeding_non_skip_insn = 0;
1995 /* sbrc */
1996 if ((0xfc == (preceeding_msb & 0xfe)
1997 && (0x00 == (preceeding_lsb & 0x08))))
1998 there_is_preceeding_non_skip_insn = 0;
2000 /* sbrs */
2001 if ((0xfe == (preceeding_msb & 0xfe)
2002 && (0x00 == (preceeding_lsb & 0x08))))
2003 there_is_preceeding_non_skip_insn = 0;
2005 /* cpse */
2006 if (0x10 == (preceeding_msb & 0xfc))
2007 there_is_preceeding_non_skip_insn = 0;
2009 if (there_is_preceeding_non_skip_insn == 0)
2010 if (debug_relax)
2011 printf ("preceeding skip insn prevents deletion of"
2012 " ret insn at addr 0x%x in section %s\n",
2013 (int) dot + 2, sec->name);
2015 else
2017 /* There is no previous instruction. */
2018 there_is_preceeding_non_skip_insn = 0;
2021 if (there_is_preceeding_non_skip_insn)
2023 /* We now only have to make sure that there is no
2024 local label defined at the address of the ret
2025 instruction and that there is no local relocation
2026 in this section pointing to the ret. */
2028 int deleting_ret_is_safe = 1;
2029 unsigned int section_offset_of_ret_insn =
2030 irel->r_offset + insn_size;
2031 Elf_Internal_Sym *isym, *isymend;
2032 unsigned int sec_shndx;
2034 sec_shndx =
2035 _bfd_elf_section_from_bfd_section (abfd, sec);
2037 /* Check for local symbols. */
2038 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2039 isymend = isym + symtab_hdr->sh_info;
2040 for (; isym < isymend; isym++)
2042 if (isym->st_value == section_offset_of_ret_insn
2043 && isym->st_shndx == sec_shndx)
2045 deleting_ret_is_safe = 0;
2046 if (debug_relax)
2047 printf ("local label prevents deletion of ret "
2048 "insn at address 0x%x\n",
2049 (int) dot + insn_size);
2053 /* Now check for global symbols. */
2055 int symcount;
2056 struct elf_link_hash_entry **sym_hashes;
2057 struct elf_link_hash_entry **end_hashes;
2059 symcount = (symtab_hdr->sh_size
2060 / sizeof (Elf32_External_Sym)
2061 - symtab_hdr->sh_info);
2062 sym_hashes = elf_sym_hashes (abfd);
2063 end_hashes = sym_hashes + symcount;
2064 for (; sym_hashes < end_hashes; sym_hashes++)
2066 struct elf_link_hash_entry *sym_hash =
2067 *sym_hashes;
2068 if ((sym_hash->root.type == bfd_link_hash_defined
2069 || sym_hash->root.type ==
2070 bfd_link_hash_defweak)
2071 && sym_hash->root.u.def.section == sec
2072 && sym_hash->root.u.def.value == section_offset_of_ret_insn)
2074 deleting_ret_is_safe = 0;
2075 if (debug_relax)
2076 printf ("global label prevents deletion of "
2077 "ret insn at address 0x%x\n",
2078 (int) dot + insn_size);
2082 /* Now we check for relocations pointing to ret. */
2084 Elf_Internal_Rela *irel;
2085 Elf_Internal_Rela *relend;
2086 Elf_Internal_Shdr *symtab_hdr;
2088 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2089 relend = elf_section_data (sec)->relocs
2090 + sec->reloc_count;
2092 for (irel = elf_section_data (sec)->relocs;
2093 irel < relend; irel++)
2095 bfd_vma reloc_target = 0;
2096 bfd_vma symval;
2097 Elf_Internal_Sym *isymbuf = NULL;
2099 /* Read this BFD's local symbols if we haven't
2100 done so already. */
2101 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2103 isymbuf = (Elf_Internal_Sym *)
2104 symtab_hdr->contents;
2105 if (isymbuf == NULL)
2106 isymbuf = bfd_elf_get_elf_syms
2107 (abfd,
2108 symtab_hdr,
2109 symtab_hdr->sh_info, 0,
2110 NULL, NULL, NULL);
2111 if (isymbuf == NULL)
2112 break;
2115 /* Get the value of the symbol referred to
2116 by the reloc. */
2117 if (ELF32_R_SYM (irel->r_info)
2118 < symtab_hdr->sh_info)
2120 /* A local symbol. */
2121 Elf_Internal_Sym *isym;
2122 asection *sym_sec;
2124 isym = isymbuf
2125 + ELF32_R_SYM (irel->r_info);
2126 sym_sec = bfd_section_from_elf_index
2127 (abfd, isym->st_shndx);
2128 symval = isym->st_value;
2130 /* If the reloc is absolute, it will not
2131 have a symbol or section associated
2132 with it. */
2134 if (sym_sec)
2136 symval +=
2137 sym_sec->output_section->vma
2138 + sym_sec->output_offset;
2139 reloc_target = symval + irel->r_addend;
2141 else
2143 reloc_target = symval + irel->r_addend;
2144 /* Reference symbol is absolute. */
2147 /* else ... reference symbol is extern. */
2149 if (address_of_ret == reloc_target)
2151 deleting_ret_is_safe = 0;
2152 if (debug_relax)
2153 printf ("ret from "
2154 "rjmp/jmp ret sequence at address"
2155 " 0x%x could not be deleted. ret"
2156 " is target of a relocation.\n",
2157 (int) address_of_ret);
2162 if (deleting_ret_is_safe)
2164 if (debug_relax)
2165 printf ("unreachable ret instruction "
2166 "at address 0x%x deleted.\n",
2167 (int) dot + insn_size);
2169 /* Delete two bytes of data. */
2170 if (!elf32_avr_relax_delete_bytes (abfd, sec,
2171 irel->r_offset + insn_size, 2))
2172 goto error_return;
2174 /* That will change things, so, we should relax
2175 again. Note that this is not required, and it
2176 may be slow. */
2177 *again = TRUE;
2178 break;
2184 break;
2189 if (contents != NULL
2190 && elf_section_data (sec)->this_hdr.contents != contents)
2192 if (! link_info->keep_memory)
2193 free (contents);
2194 else
2196 /* Cache the section contents for elf_link_input_bfd. */
2197 elf_section_data (sec)->this_hdr.contents = contents;
2201 if (internal_relocs != NULL
2202 && elf_section_data (sec)->relocs != internal_relocs)
2203 free (internal_relocs);
2205 return TRUE;
2207 error_return:
2208 if (isymbuf != NULL
2209 && symtab_hdr->contents != (unsigned char *) isymbuf)
2210 free (isymbuf);
2211 if (contents != NULL
2212 && elf_section_data (sec)->this_hdr.contents != contents)
2213 free (contents);
2214 if (internal_relocs != NULL
2215 && elf_section_data (sec)->relocs != internal_relocs)
2216 free (internal_relocs);
2218 return FALSE;
2221 /* This is a version of bfd_generic_get_relocated_section_contents
2222 which uses elf32_avr_relocate_section.
2224 For avr it's essentially a cut and paste taken from the H8300 port.
2225 The author of the relaxation support patch for avr had absolutely no
2226 clue what is happening here but found out that this part of the code
2227 seems to be important. */
2229 static bfd_byte *
2230 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
2231 struct bfd_link_info *link_info,
2232 struct bfd_link_order *link_order,
2233 bfd_byte *data,
2234 bfd_boolean relocatable,
2235 asymbol **symbols)
2237 Elf_Internal_Shdr *symtab_hdr;
2238 asection *input_section = link_order->u.indirect.section;
2239 bfd *input_bfd = input_section->owner;
2240 asection **sections = NULL;
2241 Elf_Internal_Rela *internal_relocs = NULL;
2242 Elf_Internal_Sym *isymbuf = NULL;
2244 /* We only need to handle the case of relaxing, or of having a
2245 particular set of section contents, specially. */
2246 if (relocatable
2247 || elf_section_data (input_section)->this_hdr.contents == NULL)
2248 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2249 link_order, data,
2250 relocatable,
2251 symbols);
2252 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2254 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2255 (size_t) input_section->size);
2257 if ((input_section->flags & SEC_RELOC) != 0
2258 && input_section->reloc_count > 0)
2260 asection **secpp;
2261 Elf_Internal_Sym *isym, *isymend;
2262 bfd_size_type amt;
2264 internal_relocs = (_bfd_elf_link_read_relocs
2265 (input_bfd, input_section, NULL, NULL, FALSE));
2266 if (internal_relocs == NULL)
2267 goto error_return;
2269 if (symtab_hdr->sh_info != 0)
2271 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2272 if (isymbuf == NULL)
2273 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2274 symtab_hdr->sh_info, 0,
2275 NULL, NULL, NULL);
2276 if (isymbuf == NULL)
2277 goto error_return;
2280 amt = symtab_hdr->sh_info;
2281 amt *= sizeof (asection *);
2282 sections = bfd_malloc (amt);
2283 if (sections == NULL && amt != 0)
2284 goto error_return;
2286 isymend = isymbuf + symtab_hdr->sh_info;
2287 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
2289 asection *isec;
2291 if (isym->st_shndx == SHN_UNDEF)
2292 isec = bfd_und_section_ptr;
2293 else if (isym->st_shndx == SHN_ABS)
2294 isec = bfd_abs_section_ptr;
2295 else if (isym->st_shndx == SHN_COMMON)
2296 isec = bfd_com_section_ptr;
2297 else
2298 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2300 *secpp = isec;
2303 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
2304 input_section, data, internal_relocs,
2305 isymbuf, sections))
2306 goto error_return;
2308 if (sections != NULL)
2309 free (sections);
2310 if (isymbuf != NULL
2311 && symtab_hdr->contents != (unsigned char *) isymbuf)
2312 free (isymbuf);
2313 if (elf_section_data (input_section)->relocs != internal_relocs)
2314 free (internal_relocs);
2317 return data;
2319 error_return:
2320 if (sections != NULL)
2321 free (sections);
2322 if (isymbuf != NULL
2323 && symtab_hdr->contents != (unsigned char *) isymbuf)
2324 free (isymbuf);
2325 if (internal_relocs != NULL
2326 && elf_section_data (input_section)->relocs != internal_relocs)
2327 free (internal_relocs);
2328 return NULL;
2332 /* Determines the hash entry name for a particular reloc. It consists of
2333 the identifier of the symbol section and the added reloc addend and
2334 symbol offset relative to the section the symbol is attached to. */
2336 static char *
2337 avr_stub_name (const asection *symbol_section,
2338 const bfd_vma symbol_offset,
2339 const Elf_Internal_Rela *rela)
2341 char *stub_name;
2342 bfd_size_type len;
2344 len = 8 + 1 + 8 + 1 + 1;
2345 stub_name = bfd_malloc (len);
2347 sprintf (stub_name, "%08x+%08x",
2348 symbol_section->id & 0xffffffff,
2349 (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
2351 return stub_name;
2355 /* Add a new stub entry to the stub hash. Not all fields of the new
2356 stub entry are initialised. */
2358 static struct elf32_avr_stub_hash_entry *
2359 avr_add_stub (const char *stub_name,
2360 struct elf32_avr_link_hash_table *htab)
2362 struct elf32_avr_stub_hash_entry *hsh;
2364 /* Enter this entry into the linker stub hash table. */
2365 hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
2367 if (hsh == NULL)
2369 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
2370 NULL, stub_name);
2371 return NULL;
2374 hsh->stub_offset = 0;
2375 return hsh;
2378 /* We assume that there is already space allocated for the stub section
2379 contents and that before building the stubs the section size is
2380 initialized to 0. We assume that within the stub hash table entry,
2381 the absolute position of the jmp target has been written in the
2382 target_value field. We write here the offset of the generated jmp insn
2383 relative to the trampoline section start to the stub_offset entry in
2384 the stub hash table entry. */
2386 static bfd_boolean
2387 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2389 struct elf32_avr_stub_hash_entry *hsh;
2390 struct bfd_link_info *info;
2391 struct elf32_avr_link_hash_table *htab;
2392 bfd *stub_bfd;
2393 bfd_byte *loc;
2394 bfd_vma target;
2395 bfd_vma starget;
2397 /* Basic opcode */
2398 bfd_vma jmp_insn = 0x0000940c;
2400 /* Massage our args to the form they really have. */
2401 hsh = avr_stub_hash_entry (bh);
2403 if (!hsh->is_actually_needed)
2404 return TRUE;
2406 info = (struct bfd_link_info *) in_arg;
2408 htab = avr_link_hash_table (info);
2409 if (htab == NULL)
2410 return FALSE;
2412 target = hsh->target_value;
2414 /* Make a note of the offset within the stubs for this entry. */
2415 hsh->stub_offset = htab->stub_sec->size;
2416 loc = htab->stub_sec->contents + hsh->stub_offset;
2418 stub_bfd = htab->stub_sec->owner;
2420 if (debug_stubs)
2421 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
2422 (unsigned int) target,
2423 (unsigned int) hsh->stub_offset);
2425 /* We now have to add the information on the jump target to the bare
2426 opcode bits already set in jmp_insn. */
2428 /* Check for the alignment of the address. */
2429 if (target & 1)
2430 return FALSE;
2432 starget = target >> 1;
2433 jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
2434 bfd_put_16 (stub_bfd, jmp_insn, loc);
2435 bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
2437 htab->stub_sec->size += 4;
2439 /* Now add the entries in the address mapping table if there is still
2440 space left. */
2442 unsigned int nr;
2444 nr = htab->amt_entry_cnt + 1;
2445 if (nr <= htab->amt_max_entry_cnt)
2447 htab->amt_entry_cnt = nr;
2449 htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
2450 htab->amt_destination_addr[nr - 1] = target;
2454 return TRUE;
2457 static bfd_boolean
2458 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
2459 void *in_arg)
2461 struct elf32_avr_stub_hash_entry *hsh;
2462 struct elf32_avr_link_hash_table *htab;
2464 htab = in_arg;
2465 hsh = avr_stub_hash_entry (bh);
2466 hsh->is_actually_needed = FALSE;
2468 return TRUE;
2471 static bfd_boolean
2472 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2474 struct elf32_avr_stub_hash_entry *hsh;
2475 struct elf32_avr_link_hash_table *htab;
2476 int size;
2478 /* Massage our args to the form they really have. */
2479 hsh = avr_stub_hash_entry (bh);
2480 htab = in_arg;
2482 if (hsh->is_actually_needed)
2483 size = 4;
2484 else
2485 size = 0;
2487 htab->stub_sec->size += size;
2488 return TRUE;
2491 void
2492 elf32_avr_setup_params (struct bfd_link_info *info,
2493 bfd *avr_stub_bfd,
2494 asection *avr_stub_section,
2495 bfd_boolean no_stubs,
2496 bfd_boolean deb_stubs,
2497 bfd_boolean deb_relax,
2498 bfd_vma pc_wrap_around,
2499 bfd_boolean call_ret_replacement)
2501 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
2503 if (htab == NULL)
2504 return;
2505 htab->stub_sec = avr_stub_section;
2506 htab->stub_bfd = avr_stub_bfd;
2507 htab->no_stubs = no_stubs;
2509 debug_relax = deb_relax;
2510 debug_stubs = deb_stubs;
2511 avr_pc_wrap_around = pc_wrap_around;
2512 avr_replace_call_ret_sequences = call_ret_replacement;
2516 /* Set up various things so that we can make a list of input sections
2517 for each output section included in the link. Returns -1 on error,
2518 0 when no stubs will be needed, and 1 on success. It also sets
2519 information on the stubs bfd and the stub section in the info
2520 struct. */
2523 elf32_avr_setup_section_lists (bfd *output_bfd,
2524 struct bfd_link_info *info)
2526 bfd *input_bfd;
2527 unsigned int bfd_count;
2528 int top_id, top_index;
2529 asection *section;
2530 asection **input_list, **list;
2531 bfd_size_type amt;
2532 struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
2534 if (htab == NULL || htab->no_stubs)
2535 return 0;
2537 /* Count the number of input BFDs and find the top input section id. */
2538 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2539 input_bfd != NULL;
2540 input_bfd = input_bfd->link_next)
2542 bfd_count += 1;
2543 for (section = input_bfd->sections;
2544 section != NULL;
2545 section = section->next)
2546 if (top_id < section->id)
2547 top_id = section->id;
2550 htab->bfd_count = bfd_count;
2552 /* We can't use output_bfd->section_count here to find the top output
2553 section index as some sections may have been removed, and
2554 strip_excluded_output_sections doesn't renumber the indices. */
2555 for (section = output_bfd->sections, top_index = 0;
2556 section != NULL;
2557 section = section->next)
2558 if (top_index < section->index)
2559 top_index = section->index;
2561 htab->top_index = top_index;
2562 amt = sizeof (asection *) * (top_index + 1);
2563 input_list = bfd_malloc (amt);
2564 htab->input_list = input_list;
2565 if (input_list == NULL)
2566 return -1;
2568 /* For sections we aren't interested in, mark their entries with a
2569 value we can check later. */
2570 list = input_list + top_index;
2572 *list = bfd_abs_section_ptr;
2573 while (list-- != input_list);
2575 for (section = output_bfd->sections;
2576 section != NULL;
2577 section = section->next)
2578 if ((section->flags & SEC_CODE) != 0)
2579 input_list[section->index] = NULL;
2581 return 1;
2585 /* Read in all local syms for all input bfds, and create hash entries
2586 for export stubs if we are building a multi-subspace shared lib.
2587 Returns -1 on error, 0 otherwise. */
2589 static int
2590 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
2592 unsigned int bfd_indx;
2593 Elf_Internal_Sym *local_syms, **all_local_syms;
2594 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
2596 if (htab == NULL)
2597 return -1;
2599 /* We want to read in symbol extension records only once. To do this
2600 we need to read in the local symbols in parallel and save them for
2601 later use; so hold pointers to the local symbols in an array. */
2602 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2603 all_local_syms = bfd_zmalloc (amt);
2604 htab->all_local_syms = all_local_syms;
2605 if (all_local_syms == NULL)
2606 return -1;
2608 /* Walk over all the input BFDs, swapping in local symbols.
2609 If we are creating a shared library, create hash entries for the
2610 export stubs. */
2611 for (bfd_indx = 0;
2612 input_bfd != NULL;
2613 input_bfd = input_bfd->link_next, bfd_indx++)
2615 Elf_Internal_Shdr *symtab_hdr;
2617 /* We'll need the symbol table in a second. */
2618 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2619 if (symtab_hdr->sh_info == 0)
2620 continue;
2622 /* We need an array of the local symbols attached to the input bfd. */
2623 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2624 if (local_syms == NULL)
2626 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2627 symtab_hdr->sh_info, 0,
2628 NULL, NULL, NULL);
2629 /* Cache them for elf_link_input_bfd. */
2630 symtab_hdr->contents = (unsigned char *) local_syms;
2632 if (local_syms == NULL)
2633 return -1;
2635 all_local_syms[bfd_indx] = local_syms;
2638 return 0;
2641 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
2643 bfd_boolean
2644 elf32_avr_size_stubs (bfd *output_bfd,
2645 struct bfd_link_info *info,
2646 bfd_boolean is_prealloc_run)
2648 struct elf32_avr_link_hash_table *htab;
2649 int stub_changed = 0;
2651 htab = avr_link_hash_table (info);
2652 if (htab == NULL)
2653 return FALSE;
2655 /* At this point we initialize htab->vector_base
2656 To the start of the text output section. */
2657 htab->vector_base = htab->stub_sec->output_section->vma;
2659 if (get_local_syms (info->input_bfds, info))
2661 if (htab->all_local_syms)
2662 goto error_ret_free_local;
2663 return FALSE;
2666 if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
2668 struct elf32_avr_stub_hash_entry *test;
2670 test = avr_add_stub ("Hugo",htab);
2671 test->target_value = 0x123456;
2672 test->stub_offset = 13;
2674 test = avr_add_stub ("Hugo2",htab);
2675 test->target_value = 0x84210;
2676 test->stub_offset = 14;
2679 while (1)
2681 bfd *input_bfd;
2682 unsigned int bfd_indx;
2684 /* We will have to re-generate the stub hash table each time anything
2685 in memory has changed. */
2687 bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
2688 for (input_bfd = info->input_bfds, bfd_indx = 0;
2689 input_bfd != NULL;
2690 input_bfd = input_bfd->link_next, bfd_indx++)
2692 Elf_Internal_Shdr *symtab_hdr;
2693 asection *section;
2694 Elf_Internal_Sym *local_syms;
2696 /* We'll need the symbol table in a second. */
2697 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2698 if (symtab_hdr->sh_info == 0)
2699 continue;
2701 local_syms = htab->all_local_syms[bfd_indx];
2703 /* Walk over each section attached to the input bfd. */
2704 for (section = input_bfd->sections;
2705 section != NULL;
2706 section = section->next)
2708 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2710 /* If there aren't any relocs, then there's nothing more
2711 to do. */
2712 if ((section->flags & SEC_RELOC) == 0
2713 || section->reloc_count == 0)
2714 continue;
2716 /* If this section is a link-once section that will be
2717 discarded, then don't create any stubs. */
2718 if (section->output_section == NULL
2719 || section->output_section->owner != output_bfd)
2720 continue;
2722 /* Get the relocs. */
2723 internal_relocs
2724 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2725 info->keep_memory);
2726 if (internal_relocs == NULL)
2727 goto error_ret_free_local;
2729 /* Now examine each relocation. */
2730 irela = internal_relocs;
2731 irelaend = irela + section->reloc_count;
2732 for (; irela < irelaend; irela++)
2734 unsigned int r_type, r_indx;
2735 struct elf32_avr_stub_hash_entry *hsh;
2736 asection *sym_sec;
2737 bfd_vma sym_value;
2738 bfd_vma destination;
2739 struct elf_link_hash_entry *hh;
2740 char *stub_name;
2742 r_type = ELF32_R_TYPE (irela->r_info);
2743 r_indx = ELF32_R_SYM (irela->r_info);
2745 /* Only look for 16 bit GS relocs. No other reloc will need a
2746 stub. */
2747 if (!((r_type == R_AVR_16_PM)
2748 || (r_type == R_AVR_LO8_LDI_GS)
2749 || (r_type == R_AVR_HI8_LDI_GS)))
2750 continue;
2752 /* Now determine the call target, its name, value,
2753 section. */
2754 sym_sec = NULL;
2755 sym_value = 0;
2756 destination = 0;
2757 hh = NULL;
2758 if (r_indx < symtab_hdr->sh_info)
2760 /* It's a local symbol. */
2761 Elf_Internal_Sym *sym;
2762 Elf_Internal_Shdr *hdr;
2764 sym = local_syms + r_indx;
2765 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2766 sym_sec = hdr->bfd_section;
2767 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2768 sym_value = sym->st_value;
2769 destination = (sym_value + irela->r_addend
2770 + sym_sec->output_offset
2771 + sym_sec->output_section->vma);
2773 else
2775 /* It's an external symbol. */
2776 int e_indx;
2778 e_indx = r_indx - symtab_hdr->sh_info;
2779 hh = elf_sym_hashes (input_bfd)[e_indx];
2781 while (hh->root.type == bfd_link_hash_indirect
2782 || hh->root.type == bfd_link_hash_warning)
2783 hh = (struct elf_link_hash_entry *)
2784 (hh->root.u.i.link);
2786 if (hh->root.type == bfd_link_hash_defined
2787 || hh->root.type == bfd_link_hash_defweak)
2789 sym_sec = hh->root.u.def.section;
2790 sym_value = hh->root.u.def.value;
2791 if (sym_sec->output_section != NULL)
2792 destination = (sym_value + irela->r_addend
2793 + sym_sec->output_offset
2794 + sym_sec->output_section->vma);
2796 else if (hh->root.type == bfd_link_hash_undefweak)
2798 if (! info->shared)
2799 continue;
2801 else if (hh->root.type == bfd_link_hash_undefined)
2803 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2804 && (ELF_ST_VISIBILITY (hh->other)
2805 == STV_DEFAULT)))
2806 continue;
2808 else
2810 bfd_set_error (bfd_error_bad_value);
2812 error_ret_free_internal:
2813 if (elf_section_data (section)->relocs == NULL)
2814 free (internal_relocs);
2815 goto error_ret_free_local;
2819 if (! avr_stub_is_required_for_16_bit_reloc
2820 (destination - htab->vector_base))
2822 if (!is_prealloc_run)
2823 /* We are having a reloc that does't need a stub. */
2824 continue;
2826 /* We don't right now know if a stub will be needed.
2827 Let's rather be on the safe side. */
2830 /* Get the name of this stub. */
2831 stub_name = avr_stub_name (sym_sec, sym_value, irela);
2833 if (!stub_name)
2834 goto error_ret_free_internal;
2837 hsh = avr_stub_hash_lookup (&htab->bstab,
2838 stub_name,
2839 FALSE, FALSE);
2840 if (hsh != NULL)
2842 /* The proper stub has already been created. Mark it
2843 to be used and write the possibly changed destination
2844 value. */
2845 hsh->is_actually_needed = TRUE;
2846 hsh->target_value = destination;
2847 free (stub_name);
2848 continue;
2851 hsh = avr_add_stub (stub_name, htab);
2852 if (hsh == NULL)
2854 free (stub_name);
2855 goto error_ret_free_internal;
2858 hsh->is_actually_needed = TRUE;
2859 hsh->target_value = destination;
2861 if (debug_stubs)
2862 printf ("Adding stub with destination 0x%x to the"
2863 " hash table.\n", (unsigned int) destination);
2864 if (debug_stubs)
2865 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
2867 stub_changed = TRUE;
2870 /* We're done with the internal relocs, free them. */
2871 if (elf_section_data (section)->relocs == NULL)
2872 free (internal_relocs);
2876 /* Re-Calculate the number of needed stubs. */
2877 htab->stub_sec->size = 0;
2878 bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
2880 if (!stub_changed)
2881 break;
2883 stub_changed = FALSE;
2886 free (htab->all_local_syms);
2887 return TRUE;
2889 error_ret_free_local:
2890 free (htab->all_local_syms);
2891 return FALSE;
2895 /* Build all the stubs associated with the current output file. The
2896 stubs are kept in a hash table attached to the main linker hash
2897 table. We also set up the .plt entries for statically linked PIC
2898 functions here. This function is called via hppaelf_finish in the
2899 linker. */
2901 bfd_boolean
2902 elf32_avr_build_stubs (struct bfd_link_info *info)
2904 asection *stub_sec;
2905 struct bfd_hash_table *table;
2906 struct elf32_avr_link_hash_table *htab;
2907 bfd_size_type total_size = 0;
2909 htab = avr_link_hash_table (info);
2910 if (htab == NULL)
2911 return FALSE;
2913 /* In case that there were several stub sections: */
2914 for (stub_sec = htab->stub_bfd->sections;
2915 stub_sec != NULL;
2916 stub_sec = stub_sec->next)
2918 bfd_size_type size;
2920 /* Allocate memory to hold the linker stubs. */
2921 size = stub_sec->size;
2922 total_size += size;
2924 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2925 if (stub_sec->contents == NULL && size != 0)
2926 return FALSE;
2927 stub_sec->size = 0;
2930 /* Allocate memory for the adress mapping table. */
2931 htab->amt_entry_cnt = 0;
2932 htab->amt_max_entry_cnt = total_size / 4;
2933 htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
2934 * htab->amt_max_entry_cnt);
2935 htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
2936 * htab->amt_max_entry_cnt );
2938 if (debug_stubs)
2939 printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
2941 /* Build the stubs as directed by the stub hash table. */
2942 table = &htab->bstab;
2943 bfd_hash_traverse (table, avr_build_one_stub, info);
2945 if (debug_stubs)
2946 printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
2948 return TRUE;
2951 #define ELF_ARCH bfd_arch_avr
2952 #define ELF_MACHINE_CODE EM_AVR
2953 #define ELF_MACHINE_ALT1 EM_AVR_OLD
2954 #define ELF_MAXPAGESIZE 1
2956 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
2957 #define TARGET_LITTLE_NAME "elf32-avr"
2959 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
2960 #define bfd_elf32_bfd_link_hash_table_free elf32_avr_link_hash_table_free
2962 #define elf_info_to_howto avr_info_to_howto_rela
2963 #define elf_info_to_howto_rel NULL
2964 #define elf_backend_relocate_section elf32_avr_relocate_section
2965 #define elf_backend_check_relocs elf32_avr_check_relocs
2966 #define elf_backend_can_gc_sections 1
2967 #define elf_backend_rela_normal 1
2968 #define elf_backend_final_write_processing \
2969 bfd_elf_avr_final_write_processing
2970 #define elf_backend_object_p elf32_avr_object_p
2972 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
2973 #define bfd_elf32_bfd_get_relocated_section_contents \
2974 elf32_avr_get_relocated_section_contents
2976 #include "elf32-target.h"