* configure.tgt (i[3-7]86-*-solaris2*, i[3-7]86-*-solaris*): Correct
[binutils.git] / bfd / elf32-avr.c
blob0028871d64b08ca0fbb1cc0f706f1898fb49477b
1 /* AVR-specific support for 32-bit ELF
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2006
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 2 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 "bfd.h"
24 #include "sysdep.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 ((struct elf32_avr_link_hash_table *) ((p)->hash))
108 #define avr_stub_hash_entry(ent) \
109 ((struct elf32_avr_stub_hash_entry *)(ent))
111 #define avr_stub_hash_lookup(table, string, create, copy) \
112 ((struct elf32_avr_stub_hash_entry *) \
113 bfd_hash_lookup ((table), (string), (create), (copy)))
115 static reloc_howto_type elf_avr_howto_table[] =
117 HOWTO (R_AVR_NONE, /* type */
118 0, /* rightshift */
119 2, /* size (0 = byte, 1 = short, 2 = long) */
120 32, /* bitsize */
121 FALSE, /* pc_relative */
122 0, /* bitpos */
123 complain_overflow_bitfield, /* complain_on_overflow */
124 bfd_elf_generic_reloc, /* special_function */
125 "R_AVR_NONE", /* name */
126 FALSE, /* partial_inplace */
127 0, /* src_mask */
128 0, /* dst_mask */
129 FALSE), /* pcrel_offset */
131 HOWTO (R_AVR_32, /* type */
132 0, /* rightshift */
133 2, /* size (0 = byte, 1 = short, 2 = long) */
134 32, /* bitsize */
135 FALSE, /* pc_relative */
136 0, /* bitpos */
137 complain_overflow_bitfield, /* complain_on_overflow */
138 bfd_elf_generic_reloc, /* special_function */
139 "R_AVR_32", /* name */
140 FALSE, /* partial_inplace */
141 0xffffffff, /* src_mask */
142 0xffffffff, /* dst_mask */
143 FALSE), /* pcrel_offset */
145 /* A 7 bit PC relative relocation. */
146 HOWTO (R_AVR_7_PCREL, /* type */
147 1, /* rightshift */
148 1, /* size (0 = byte, 1 = short, 2 = long) */
149 7, /* bitsize */
150 TRUE, /* pc_relative */
151 3, /* bitpos */
152 complain_overflow_bitfield, /* complain_on_overflow */
153 bfd_elf_generic_reloc, /* special_function */
154 "R_AVR_7_PCREL", /* name */
155 FALSE, /* partial_inplace */
156 0xffff, /* src_mask */
157 0xffff, /* dst_mask */
158 TRUE), /* pcrel_offset */
160 /* A 13 bit PC relative relocation. */
161 HOWTO (R_AVR_13_PCREL, /* type */
162 1, /* rightshift */
163 1, /* size (0 = byte, 1 = short, 2 = long) */
164 13, /* bitsize */
165 TRUE, /* pc_relative */
166 0, /* bitpos */
167 complain_overflow_bitfield, /* complain_on_overflow */
168 bfd_elf_generic_reloc, /* special_function */
169 "R_AVR_13_PCREL", /* name */
170 FALSE, /* partial_inplace */
171 0xfff, /* src_mask */
172 0xfff, /* dst_mask */
173 TRUE), /* pcrel_offset */
175 /* A 16 bit absolute relocation. */
176 HOWTO (R_AVR_16, /* type */
177 0, /* rightshift */
178 1, /* size (0 = byte, 1 = short, 2 = long) */
179 16, /* bitsize */
180 FALSE, /* pc_relative */
181 0, /* bitpos */
182 complain_overflow_dont, /* complain_on_overflow */
183 bfd_elf_generic_reloc, /* special_function */
184 "R_AVR_16", /* name */
185 FALSE, /* partial_inplace */
186 0xffff, /* src_mask */
187 0xffff, /* dst_mask */
188 FALSE), /* pcrel_offset */
190 /* A 16 bit absolute relocation for command address
191 Will be changed when linker stubs are needed. */
192 HOWTO (R_AVR_16_PM, /* type */
193 1, /* rightshift */
194 1, /* size (0 = byte, 1 = short, 2 = long) */
195 16, /* bitsize */
196 FALSE, /* pc_relative */
197 0, /* bitpos */
198 complain_overflow_bitfield, /* complain_on_overflow */
199 bfd_elf_generic_reloc, /* special_function */
200 "R_AVR_16_PM", /* name */
201 FALSE, /* partial_inplace */
202 0xffff, /* src_mask */
203 0xffff, /* dst_mask */
204 FALSE), /* pcrel_offset */
205 /* A low 8 bit absolute relocation of 16 bit address.
206 For LDI command. */
207 HOWTO (R_AVR_LO8_LDI, /* type */
208 0, /* rightshift */
209 1, /* size (0 = byte, 1 = short, 2 = long) */
210 8, /* bitsize */
211 FALSE, /* pc_relative */
212 0, /* bitpos */
213 complain_overflow_dont, /* complain_on_overflow */
214 bfd_elf_generic_reloc, /* special_function */
215 "R_AVR_LO8_LDI", /* name */
216 FALSE, /* partial_inplace */
217 0xffff, /* src_mask */
218 0xffff, /* dst_mask */
219 FALSE), /* pcrel_offset */
220 /* A high 8 bit absolute relocation of 16 bit address.
221 For LDI command. */
222 HOWTO (R_AVR_HI8_LDI, /* type */
223 8, /* rightshift */
224 1, /* size (0 = byte, 1 = short, 2 = long) */
225 8, /* bitsize */
226 FALSE, /* pc_relative */
227 0, /* bitpos */
228 complain_overflow_dont, /* complain_on_overflow */
229 bfd_elf_generic_reloc, /* special_function */
230 "R_AVR_HI8_LDI", /* name */
231 FALSE, /* partial_inplace */
232 0xffff, /* src_mask */
233 0xffff, /* dst_mask */
234 FALSE), /* pcrel_offset */
235 /* A high 6 bit absolute relocation of 22 bit address.
236 For LDI command. As well second most significant 8 bit value of
237 a 32 bit link-time constant. */
238 HOWTO (R_AVR_HH8_LDI, /* type */
239 16, /* rightshift */
240 1, /* size (0 = byte, 1 = short, 2 = long) */
241 8, /* bitsize */
242 FALSE, /* pc_relative */
243 0, /* bitpos */
244 complain_overflow_dont, /* complain_on_overflow */
245 bfd_elf_generic_reloc, /* special_function */
246 "R_AVR_HH8_LDI", /* name */
247 FALSE, /* partial_inplace */
248 0xffff, /* src_mask */
249 0xffff, /* dst_mask */
250 FALSE), /* pcrel_offset */
251 /* A negative low 8 bit absolute relocation of 16 bit address.
252 For LDI command. */
253 HOWTO (R_AVR_LO8_LDI_NEG, /* type */
254 0, /* rightshift */
255 1, /* size (0 = byte, 1 = short, 2 = long) */
256 8, /* bitsize */
257 FALSE, /* pc_relative */
258 0, /* bitpos */
259 complain_overflow_dont, /* complain_on_overflow */
260 bfd_elf_generic_reloc, /* special_function */
261 "R_AVR_LO8_LDI_NEG", /* name */
262 FALSE, /* partial_inplace */
263 0xffff, /* src_mask */
264 0xffff, /* dst_mask */
265 FALSE), /* pcrel_offset */
266 /* A negative high 8 bit absolute relocation of 16 bit address.
267 For LDI command. */
268 HOWTO (R_AVR_HI8_LDI_NEG, /* type */
269 8, /* rightshift */
270 1, /* size (0 = byte, 1 = short, 2 = long) */
271 8, /* bitsize */
272 FALSE, /* pc_relative */
273 0, /* bitpos */
274 complain_overflow_dont, /* complain_on_overflow */
275 bfd_elf_generic_reloc, /* special_function */
276 "R_AVR_HI8_LDI_NEG", /* name */
277 FALSE, /* partial_inplace */
278 0xffff, /* src_mask */
279 0xffff, /* dst_mask */
280 FALSE), /* pcrel_offset */
281 /* A negative high 6 bit absolute relocation of 22 bit address.
282 For LDI command. */
283 HOWTO (R_AVR_HH8_LDI_NEG, /* type */
284 16, /* rightshift */
285 1, /* size (0 = byte, 1 = short, 2 = long) */
286 8, /* bitsize */
287 FALSE, /* pc_relative */
288 0, /* bitpos */
289 complain_overflow_dont, /* complain_on_overflow */
290 bfd_elf_generic_reloc, /* special_function */
291 "R_AVR_HH8_LDI_NEG", /* name */
292 FALSE, /* partial_inplace */
293 0xffff, /* src_mask */
294 0xffff, /* dst_mask */
295 FALSE), /* pcrel_offset */
296 /* A low 8 bit absolute relocation of 24 bit program memory address.
297 For LDI command. Will not be changed when linker stubs are needed. */
298 HOWTO (R_AVR_LO8_LDI_PM, /* type */
299 1, /* rightshift */
300 1, /* size (0 = byte, 1 = short, 2 = long) */
301 8, /* bitsize */
302 FALSE, /* pc_relative */
303 0, /* bitpos */
304 complain_overflow_dont, /* complain_on_overflow */
305 bfd_elf_generic_reloc, /* special_function */
306 "R_AVR_LO8_LDI_PM", /* name */
307 FALSE, /* partial_inplace */
308 0xffff, /* src_mask */
309 0xffff, /* dst_mask */
310 FALSE), /* pcrel_offset */
311 /* A low 8 bit absolute relocation of 24 bit program memory address.
312 For LDI command. Will not be changed when linker stubs are needed. */
313 HOWTO (R_AVR_HI8_LDI_PM, /* type */
314 9, /* rightshift */
315 1, /* size (0 = byte, 1 = short, 2 = long) */
316 8, /* bitsize */
317 FALSE, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_dont, /* complain_on_overflow */
320 bfd_elf_generic_reloc, /* special_function */
321 "R_AVR_HI8_LDI_PM", /* name */
322 FALSE, /* partial_inplace */
323 0xffff, /* src_mask */
324 0xffff, /* dst_mask */
325 FALSE), /* pcrel_offset */
326 /* A low 8 bit absolute relocation of 24 bit program memory address.
327 For LDI command. Will not be changed when linker stubs are needed. */
328 HOWTO (R_AVR_HH8_LDI_PM, /* type */
329 17, /* rightshift */
330 1, /* size (0 = byte, 1 = short, 2 = long) */
331 8, /* bitsize */
332 FALSE, /* pc_relative */
333 0, /* bitpos */
334 complain_overflow_dont, /* complain_on_overflow */
335 bfd_elf_generic_reloc, /* special_function */
336 "R_AVR_HH8_LDI_PM", /* name */
337 FALSE, /* partial_inplace */
338 0xffff, /* src_mask */
339 0xffff, /* dst_mask */
340 FALSE), /* pcrel_offset */
341 /* A low 8 bit absolute relocation of 24 bit program memory address.
342 For LDI command. Will not be changed when linker stubs are needed. */
343 HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */
344 1, /* rightshift */
345 1, /* size (0 = byte, 1 = short, 2 = long) */
346 8, /* bitsize */
347 FALSE, /* pc_relative */
348 0, /* bitpos */
349 complain_overflow_dont, /* complain_on_overflow */
350 bfd_elf_generic_reloc, /* special_function */
351 "R_AVR_LO8_LDI_PM_NEG", /* name */
352 FALSE, /* partial_inplace */
353 0xffff, /* src_mask */
354 0xffff, /* dst_mask */
355 FALSE), /* pcrel_offset */
356 /* A low 8 bit absolute relocation of 24 bit program memory address.
357 For LDI command. Will not be changed when linker stubs are needed. */
358 HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */
359 9, /* rightshift */
360 1, /* size (0 = byte, 1 = short, 2 = long) */
361 8, /* bitsize */
362 FALSE, /* pc_relative */
363 0, /* bitpos */
364 complain_overflow_dont, /* complain_on_overflow */
365 bfd_elf_generic_reloc, /* special_function */
366 "R_AVR_HI8_LDI_PM_NEG", /* name */
367 FALSE, /* partial_inplace */
368 0xffff, /* src_mask */
369 0xffff, /* dst_mask */
370 FALSE), /* pcrel_offset */
371 /* A low 8 bit absolute relocation of 24 bit program memory address.
372 For LDI command. Will not be changed when linker stubs are needed. */
373 HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */
374 17, /* rightshift */
375 1, /* size (0 = byte, 1 = short, 2 = long) */
376 8, /* bitsize */
377 FALSE, /* pc_relative */
378 0, /* bitpos */
379 complain_overflow_dont, /* complain_on_overflow */
380 bfd_elf_generic_reloc, /* special_function */
381 "R_AVR_HH8_LDI_PM_NEG", /* name */
382 FALSE, /* partial_inplace */
383 0xffff, /* src_mask */
384 0xffff, /* dst_mask */
385 FALSE), /* pcrel_offset */
386 /* Relocation for CALL command in ATmega. */
387 HOWTO (R_AVR_CALL, /* type */
388 1, /* rightshift */
389 2, /* size (0 = byte, 1 = short, 2 = long) */
390 23, /* bitsize */
391 FALSE, /* pc_relative */
392 0, /* bitpos */
393 complain_overflow_dont,/* complain_on_overflow */
394 bfd_elf_generic_reloc, /* special_function */
395 "R_AVR_CALL", /* name */
396 FALSE, /* partial_inplace */
397 0xffffffff, /* src_mask */
398 0xffffffff, /* dst_mask */
399 FALSE), /* pcrel_offset */
400 /* A 16 bit absolute relocation of 16 bit address.
401 For LDI command. */
402 HOWTO (R_AVR_LDI, /* type */
403 0, /* rightshift */
404 1, /* size (0 = byte, 1 = short, 2 = long) */
405 16, /* bitsize */
406 FALSE, /* pc_relative */
407 0, /* bitpos */
408 complain_overflow_dont,/* complain_on_overflow */
409 bfd_elf_generic_reloc, /* special_function */
410 "R_AVR_LDI", /* name */
411 FALSE, /* partial_inplace */
412 0xffff, /* src_mask */
413 0xffff, /* dst_mask */
414 FALSE), /* pcrel_offset */
415 /* A 6 bit absolute relocation of 6 bit offset.
416 For ldd/sdd command. */
417 HOWTO (R_AVR_6, /* type */
418 0, /* rightshift */
419 0, /* size (0 = byte, 1 = short, 2 = long) */
420 6, /* bitsize */
421 FALSE, /* pc_relative */
422 0, /* bitpos */
423 complain_overflow_dont,/* complain_on_overflow */
424 bfd_elf_generic_reloc, /* special_function */
425 "R_AVR_6", /* name */
426 FALSE, /* partial_inplace */
427 0xffff, /* src_mask */
428 0xffff, /* dst_mask */
429 FALSE), /* pcrel_offset */
430 /* A 6 bit absolute relocation of 6 bit offset.
431 For sbiw/adiw command. */
432 HOWTO (R_AVR_6_ADIW, /* type */
433 0, /* rightshift */
434 0, /* size (0 = byte, 1 = short, 2 = long) */
435 6, /* bitsize */
436 FALSE, /* pc_relative */
437 0, /* bitpos */
438 complain_overflow_dont,/* complain_on_overflow */
439 bfd_elf_generic_reloc, /* special_function */
440 "R_AVR_6_ADIW", /* name */
441 FALSE, /* partial_inplace */
442 0xffff, /* src_mask */
443 0xffff, /* dst_mask */
444 FALSE), /* pcrel_offset */
445 /* Most significant 8 bit value of a 32 bit link-time constant. */
446 HOWTO (R_AVR_MS8_LDI, /* type */
447 24, /* rightshift */
448 1, /* size (0 = byte, 1 = short, 2 = long) */
449 8, /* bitsize */
450 FALSE, /* pc_relative */
451 0, /* bitpos */
452 complain_overflow_dont, /* complain_on_overflow */
453 bfd_elf_generic_reloc, /* special_function */
454 "R_AVR_MS8_LDI", /* name */
455 FALSE, /* partial_inplace */
456 0xffff, /* src_mask */
457 0xffff, /* dst_mask */
458 FALSE), /* pcrel_offset */
459 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
460 HOWTO (R_AVR_MS8_LDI_NEG, /* type */
461 24, /* rightshift */
462 1, /* size (0 = byte, 1 = short, 2 = long) */
463 8, /* bitsize */
464 FALSE, /* pc_relative */
465 0, /* bitpos */
466 complain_overflow_dont, /* complain_on_overflow */
467 bfd_elf_generic_reloc, /* special_function */
468 "R_AVR_MS8_LDI_NEG", /* name */
469 FALSE, /* partial_inplace */
470 0xffff, /* src_mask */
471 0xffff, /* dst_mask */
472 FALSE), /* pcrel_offset */
473 /* A low 8 bit absolute relocation of 24 bit program memory address.
474 For LDI command. Will be changed when linker stubs are needed. */
475 HOWTO (R_AVR_LO8_LDI_GS, /* type */
476 1, /* rightshift */
477 1, /* size (0 = byte, 1 = short, 2 = long) */
478 8, /* bitsize */
479 FALSE, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_dont, /* complain_on_overflow */
482 bfd_elf_generic_reloc, /* special_function */
483 "R_AVR_LO8_LDI_GS", /* name */
484 FALSE, /* partial_inplace */
485 0xffff, /* src_mask */
486 0xffff, /* dst_mask */
487 FALSE), /* pcrel_offset */
488 /* A low 8 bit absolute relocation of 24 bit program memory address.
489 For LDI command. Will be changed when linker stubs are needed. */
490 HOWTO (R_AVR_HI8_LDI_GS, /* type */
491 9, /* rightshift */
492 1, /* size (0 = byte, 1 = short, 2 = long) */
493 8, /* bitsize */
494 FALSE, /* pc_relative */
495 0, /* bitpos */
496 complain_overflow_dont, /* complain_on_overflow */
497 bfd_elf_generic_reloc, /* special_function */
498 "R_AVR_HI8_LDI_GS", /* name */
499 FALSE, /* partial_inplace */
500 0xffff, /* src_mask */
501 0xffff, /* dst_mask */
502 FALSE) /* pcrel_offset */
505 /* Map BFD reloc types to AVR ELF reloc types. */
507 struct avr_reloc_map
509 bfd_reloc_code_real_type bfd_reloc_val;
510 unsigned int elf_reloc_val;
513 static const struct avr_reloc_map avr_reloc_map[] =
515 { BFD_RELOC_NONE, R_AVR_NONE },
516 { BFD_RELOC_32, R_AVR_32 },
517 { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL },
518 { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL },
519 { BFD_RELOC_16, R_AVR_16 },
520 { BFD_RELOC_AVR_16_PM, R_AVR_16_PM },
521 { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI},
522 { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI },
523 { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI },
524 { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI },
525 { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG },
526 { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG },
527 { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG },
528 { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG },
529 { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM },
530 { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS },
531 { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM },
532 { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS },
533 { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM },
534 { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG },
535 { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG },
536 { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG },
537 { BFD_RELOC_AVR_CALL, R_AVR_CALL },
538 { BFD_RELOC_AVR_LDI, R_AVR_LDI },
539 { BFD_RELOC_AVR_6, R_AVR_6 },
540 { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW }
543 /* Meant to be filled one day with the wrap around address for the
544 specific device. I.e. should get the value 0x4000 for 16k devices,
545 0x8000 for 32k devices and so on.
547 We initialize it here with a value of 0x1000000 resulting in
548 that we will never suggest a wrap-around jump during relaxation.
549 The logic of the source code later on assumes that in
550 avr_pc_wrap_around one single bit is set. */
551 static bfd_vma avr_pc_wrap_around = 0x10000000;
553 /* If this variable holds a value different from zero, the linker relaxation
554 machine will try to optimize call/ret sequences by a single jump
555 instruction. This option could be switched off by a linker switch. */
556 static int avr_replace_call_ret_sequences = 1;
558 /* Initialize an entry in the stub hash table. */
560 static struct bfd_hash_entry *
561 stub_hash_newfunc (struct bfd_hash_entry *entry,
562 struct bfd_hash_table *table,
563 const char *string)
565 /* Allocate the structure if it has not already been allocated by a
566 subclass. */
567 if (entry == NULL)
569 entry = bfd_hash_allocate (table,
570 sizeof (struct elf32_avr_stub_hash_entry));
571 if (entry == NULL)
572 return entry;
575 /* Call the allocation method of the superclass. */
576 entry = bfd_hash_newfunc (entry, table, string);
577 if (entry != NULL)
579 struct elf32_avr_stub_hash_entry *hsh;
581 /* Initialize the local fields. */
582 hsh = avr_stub_hash_entry (entry);
583 hsh->stub_offset = 0;
584 hsh->target_value = 0;
587 return entry;
590 /* Create the derived linker hash table. The AVR ELF port uses the derived
591 hash table to keep information specific to the AVR ELF linker (without
592 using static variables). */
594 static struct bfd_link_hash_table *
595 elf32_avr_link_hash_table_create (bfd *abfd)
597 struct elf32_avr_link_hash_table *htab;
598 bfd_size_type amt = sizeof (*htab);
600 htab = bfd_malloc (amt);
601 if (htab == NULL)
602 return NULL;
604 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
605 _bfd_elf_link_hash_newfunc,
606 sizeof (struct elf_link_hash_entry)))
608 free (htab);
609 return NULL;
612 /* Init the stub hash table too. */
613 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
614 sizeof (struct elf32_avr_stub_hash_entry)))
615 return NULL;
617 htab->stub_bfd = NULL;
618 htab->stub_sec = NULL;
620 /* Initialize the address mapping table. */
621 htab->amt_stub_offsets = NULL;
622 htab->amt_destination_addr = NULL;
623 htab->amt_entry_cnt = 0;
624 htab->amt_max_entry_cnt = 0;
626 return &htab->etab.root;
629 /* Free the derived linker hash table. */
631 static void
632 elf32_avr_link_hash_table_free (struct bfd_link_hash_table *btab)
634 struct elf32_avr_link_hash_table *htab
635 = (struct elf32_avr_link_hash_table *) btab;
637 /* Free the address mapping table. */
638 if (htab->amt_stub_offsets != NULL)
639 free (htab->amt_stub_offsets);
640 if (htab->amt_destination_addr != NULL)
641 free (htab->amt_destination_addr);
643 bfd_hash_table_free (&htab->bstab);
644 _bfd_generic_link_hash_table_free (btab);
647 /* Calculates the effective distance of a pc relative jump/call. */
649 static int
650 avr_relative_distance_considering_wrap_around (unsigned int distance)
652 unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
653 int dist_with_wrap_around = distance & wrap_around_mask;
655 if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
656 dist_with_wrap_around -= avr_pc_wrap_around;
658 return dist_with_wrap_around;
662 static reloc_howto_type *
663 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
664 bfd_reloc_code_real_type code)
666 unsigned int i;
668 for (i = 0;
669 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
670 i++)
671 if (avr_reloc_map[i].bfd_reloc_val == code)
672 return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
674 return NULL;
677 /* Set the howto pointer for an AVR ELF reloc. */
679 static void
680 avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
681 arelent *cache_ptr,
682 Elf_Internal_Rela *dst)
684 unsigned int r_type;
686 r_type = ELF32_R_TYPE (dst->r_info);
687 BFD_ASSERT (r_type < (unsigned int) R_AVR_max);
688 cache_ptr->howto = &elf_avr_howto_table[r_type];
691 static asection *
692 elf32_avr_gc_mark_hook (asection *sec,
693 struct bfd_link_info *info ATTRIBUTE_UNUSED,
694 Elf_Internal_Rela *rel,
695 struct elf_link_hash_entry *h,
696 Elf_Internal_Sym *sym)
698 if (h != NULL)
700 switch (ELF32_R_TYPE (rel->r_info))
702 default:
703 switch (h->root.type)
705 case bfd_link_hash_defined:
706 case bfd_link_hash_defweak:
707 return h->root.u.def.section;
709 case bfd_link_hash_common:
710 return h->root.u.c.p->section;
712 default:
713 break;
717 else
718 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
720 return NULL;
723 static bfd_boolean
724 elf32_avr_gc_sweep_hook (bfd *abfd ATTRIBUTE_UNUSED,
725 struct bfd_link_info *info ATTRIBUTE_UNUSED,
726 asection *sec ATTRIBUTE_UNUSED,
727 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED)
729 /* We don't use got and plt entries for avr. */
730 return TRUE;
733 /* Look through the relocs for a section during the first phase.
734 Since we don't do .gots or .plts, we just need to consider the
735 virtual table relocs for gc. */
737 static bfd_boolean
738 elf32_avr_check_relocs (bfd *abfd,
739 struct bfd_link_info *info,
740 asection *sec,
741 const Elf_Internal_Rela *relocs)
743 Elf_Internal_Shdr *symtab_hdr;
744 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
745 const Elf_Internal_Rela *rel;
746 const Elf_Internal_Rela *rel_end;
748 if (info->relocatable)
749 return TRUE;
751 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
752 sym_hashes = elf_sym_hashes (abfd);
753 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
754 if (!elf_bad_symtab (abfd))
755 sym_hashes_end -= symtab_hdr->sh_info;
757 rel_end = relocs + sec->reloc_count;
758 for (rel = relocs; rel < rel_end; rel++)
760 struct elf_link_hash_entry *h;
761 unsigned long r_symndx;
763 r_symndx = ELF32_R_SYM (rel->r_info);
764 if (r_symndx < symtab_hdr->sh_info)
765 h = NULL;
766 else
768 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
769 while (h->root.type == bfd_link_hash_indirect
770 || h->root.type == bfd_link_hash_warning)
771 h = (struct elf_link_hash_entry *) h->root.u.i.link;
775 return TRUE;
778 static bfd_boolean
779 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
781 return (relocation >= 0x020000);
784 /* Returns the address of the corresponding stub if there is one.
785 Returns otherwise an address above 0x020000. This function
786 could also be used, if there is no knowledge on the section where
787 the destination is found. */
789 static bfd_vma
790 avr_get_stub_addr (bfd_vma srel,
791 struct elf32_avr_link_hash_table *htab)
793 unsigned int index;
794 bfd_vma stub_sec_addr =
795 (htab->stub_sec->output_section->vma +
796 htab->stub_sec->output_offset);
798 for (index = 0; index < htab->amt_max_entry_cnt; index ++)
799 if (htab->amt_destination_addr[index] == srel)
800 return htab->amt_stub_offsets[index] + stub_sec_addr;
802 /* Return an address that could not be reached by 16 bit relocs. */
803 return 0x020000;
806 /* Perform a single relocation. By default we use the standard BFD
807 routines, but a few relocs, we have to do them ourselves. */
809 static bfd_reloc_status_type
810 avr_final_link_relocate (reloc_howto_type * howto,
811 bfd * input_bfd,
812 asection * input_section,
813 bfd_byte * contents,
814 Elf_Internal_Rela * rel,
815 bfd_vma relocation,
816 struct elf32_avr_link_hash_table * htab)
818 bfd_reloc_status_type r = bfd_reloc_ok;
819 bfd_vma x;
820 bfd_signed_vma srel;
821 bfd_signed_vma reloc_addr;
822 bfd_boolean use_stubs = FALSE;
823 /* Usually is 0, unless we are generating code for a bootloader. */
824 bfd_signed_vma base_addr = htab->vector_base;
826 /* Absolute addr of the reloc in the final excecutable. */
827 reloc_addr = rel->r_offset + input_section->output_section->vma
828 + input_section->output_offset;
830 switch (howto->type)
832 case R_AVR_7_PCREL:
833 contents += rel->r_offset;
834 srel = (bfd_signed_vma) relocation;
835 srel += rel->r_addend;
836 srel -= rel->r_offset;
837 srel -= 2; /* Branch instructions add 2 to the PC... */
838 srel -= (input_section->output_section->vma +
839 input_section->output_offset);
841 if (srel & 1)
842 return bfd_reloc_outofrange;
843 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
844 return bfd_reloc_overflow;
845 x = bfd_get_16 (input_bfd, contents);
846 x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
847 bfd_put_16 (input_bfd, x, contents);
848 break;
850 case R_AVR_13_PCREL:
851 contents += rel->r_offset;
852 srel = (bfd_signed_vma) relocation;
853 srel += rel->r_addend;
854 srel -= rel->r_offset;
855 srel -= 2; /* Branch instructions add 2 to the PC... */
856 srel -= (input_section->output_section->vma +
857 input_section->output_offset);
859 if (srel & 1)
860 return bfd_reloc_outofrange;
862 srel = avr_relative_distance_considering_wrap_around (srel);
864 /* AVR addresses commands as words. */
865 srel >>= 1;
867 /* Check for overflow. */
868 if (srel < -2048 || srel > 2047)
870 /* Relative distance is too large. */
872 /* Always apply WRAPAROUND for avr2 and avr4. */
873 switch (bfd_get_mach (input_bfd))
875 case bfd_mach_avr2:
876 case bfd_mach_avr4:
877 break;
879 default:
880 return bfd_reloc_overflow;
884 x = bfd_get_16 (input_bfd, contents);
885 x = (x & 0xf000) | (srel & 0xfff);
886 bfd_put_16 (input_bfd, x, contents);
887 break;
889 case R_AVR_LO8_LDI:
890 contents += rel->r_offset;
891 srel = (bfd_signed_vma) relocation + rel->r_addend;
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_LDI:
898 contents += rel->r_offset;
899 srel = (bfd_signed_vma) relocation + rel->r_addend;
900 if (((srel > 0) && (srel & 0xffff) > 255)
901 || ((srel < 0) && ((-srel) & 0xffff) > 128))
902 /* Remove offset for data/eeprom section. */
903 return bfd_reloc_overflow;
905 x = bfd_get_16 (input_bfd, contents);
906 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
907 bfd_put_16 (input_bfd, x, contents);
908 break;
910 case R_AVR_6:
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 & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
918 | ((srel & (1 << 5)) << 8));
919 bfd_put_16 (input_bfd, x, contents);
920 break;
922 case R_AVR_6_ADIW:
923 contents += rel->r_offset;
924 srel = (bfd_signed_vma) relocation + rel->r_addend;
925 if (((srel & 0xffff) > 63) || (srel < 0))
926 /* Remove offset for data/eeprom section. */
927 return bfd_reloc_overflow;
928 x = bfd_get_16 (input_bfd, contents);
929 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
930 bfd_put_16 (input_bfd, x, contents);
931 break;
933 case R_AVR_HI8_LDI:
934 contents += rel->r_offset;
935 srel = (bfd_signed_vma) relocation + rel->r_addend;
936 srel = (srel >> 8) & 0xff;
937 x = bfd_get_16 (input_bfd, contents);
938 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
939 bfd_put_16 (input_bfd, x, contents);
940 break;
942 case R_AVR_HH8_LDI:
943 contents += rel->r_offset;
944 srel = (bfd_signed_vma) relocation + rel->r_addend;
945 srel = (srel >> 16) & 0xff;
946 x = bfd_get_16 (input_bfd, contents);
947 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
948 bfd_put_16 (input_bfd, x, contents);
949 break;
951 case R_AVR_MS8_LDI:
952 contents += rel->r_offset;
953 srel = (bfd_signed_vma) relocation + rel->r_addend;
954 srel = (srel >> 24) & 0xff;
955 x = bfd_get_16 (input_bfd, contents);
956 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
957 bfd_put_16 (input_bfd, x, contents);
958 break;
960 case R_AVR_LO8_LDI_NEG:
961 contents += rel->r_offset;
962 srel = (bfd_signed_vma) relocation + rel->r_addend;
963 srel = -srel;
964 x = bfd_get_16 (input_bfd, contents);
965 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
966 bfd_put_16 (input_bfd, x, contents);
967 break;
969 case R_AVR_HI8_LDI_NEG:
970 contents += rel->r_offset;
971 srel = (bfd_signed_vma) relocation + rel->r_addend;
972 srel = -srel;
973 srel = (srel >> 8) & 0xff;
974 x = bfd_get_16 (input_bfd, contents);
975 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
976 bfd_put_16 (input_bfd, x, contents);
977 break;
979 case R_AVR_HH8_LDI_NEG:
980 contents += rel->r_offset;
981 srel = (bfd_signed_vma) relocation + rel->r_addend;
982 srel = -srel;
983 srel = (srel >> 16) & 0xff;
984 x = bfd_get_16 (input_bfd, contents);
985 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
986 bfd_put_16 (input_bfd, x, contents);
987 break;
989 case R_AVR_MS8_LDI_NEG:
990 contents += rel->r_offset;
991 srel = (bfd_signed_vma) relocation + rel->r_addend;
992 srel = -srel;
993 srel = (srel >> 24) & 0xff;
994 x = bfd_get_16 (input_bfd, contents);
995 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
996 bfd_put_16 (input_bfd, x, contents);
997 break;
999 case R_AVR_LO8_LDI_GS:
1000 use_stubs = (!htab->no_stubs);
1001 /* Fall through. */
1002 case R_AVR_LO8_LDI_PM:
1003 contents += rel->r_offset;
1004 srel = (bfd_signed_vma) relocation + rel->r_addend;
1006 if (use_stubs
1007 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1009 bfd_vma old_srel = srel;
1011 /* We need to use the address of the stub instead. */
1012 srel = avr_get_stub_addr (srel, htab);
1013 if (debug_stubs)
1014 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1015 "reloc at address 0x%x.\n",
1016 (unsigned int) srel,
1017 (unsigned int) old_srel,
1018 (unsigned int) reloc_addr);
1020 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1021 return bfd_reloc_outofrange;
1024 if (srel & 1)
1025 return bfd_reloc_outofrange;
1026 srel = srel >> 1;
1027 x = bfd_get_16 (input_bfd, contents);
1028 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1029 bfd_put_16 (input_bfd, x, contents);
1030 break;
1032 case R_AVR_HI8_LDI_GS:
1033 use_stubs = (!htab->no_stubs);
1034 /* Fall through. */
1035 case R_AVR_HI8_LDI_PM:
1036 contents += rel->r_offset;
1037 srel = (bfd_signed_vma) relocation + rel->r_addend;
1039 if (use_stubs
1040 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1042 bfd_vma old_srel = srel;
1044 /* We need to use the address of the stub instead. */
1045 srel = avr_get_stub_addr (srel, htab);
1046 if (debug_stubs)
1047 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1048 "reloc at address 0x%x.\n",
1049 (unsigned int) srel,
1050 (unsigned int) old_srel,
1051 (unsigned int) reloc_addr);
1053 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1054 return bfd_reloc_outofrange;
1057 if (srel & 1)
1058 return bfd_reloc_outofrange;
1059 srel = srel >> 1;
1060 srel = (srel >> 8) & 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_HH8_LDI_PM:
1067 contents += rel->r_offset;
1068 srel = (bfd_signed_vma) relocation + rel->r_addend;
1069 if (srel & 1)
1070 return bfd_reloc_outofrange;
1071 srel = srel >> 1;
1072 srel = (srel >> 16) & 0xff;
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_LO8_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 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_HI8_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 >> 8) & 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_HH8_LDI_PM_NEG:
1104 contents += rel->r_offset;
1105 srel = (bfd_signed_vma) relocation + rel->r_addend;
1106 srel = -srel;
1107 if (srel & 1)
1108 return bfd_reloc_outofrange;
1109 srel = srel >> 1;
1110 srel = (srel >> 16) & 0xff;
1111 x = bfd_get_16 (input_bfd, contents);
1112 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1113 bfd_put_16 (input_bfd, x, contents);
1114 break;
1116 case R_AVR_CALL:
1117 contents += rel->r_offset;
1118 srel = (bfd_signed_vma) relocation + rel->r_addend;
1119 if (srel & 1)
1120 return bfd_reloc_outofrange;
1121 srel = srel >> 1;
1122 x = bfd_get_16 (input_bfd, contents);
1123 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1124 bfd_put_16 (input_bfd, x, contents);
1125 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1126 break;
1128 case R_AVR_16_PM:
1129 use_stubs = (!htab->no_stubs);
1130 contents += rel->r_offset;
1131 srel = (bfd_signed_vma) relocation + rel->r_addend;
1133 if (use_stubs
1134 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1136 bfd_vma old_srel = srel;
1138 /* We need to use the address of the stub instead. */
1139 srel = avr_get_stub_addr (srel,htab);
1140 if (debug_stubs)
1141 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1142 "reloc at address 0x%x.\n",
1143 (unsigned int) srel,
1144 (unsigned int) old_srel,
1145 (unsigned int) reloc_addr);
1147 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1148 return bfd_reloc_outofrange;
1151 if (srel & 1)
1152 return bfd_reloc_outofrange;
1153 srel = srel >> 1;
1154 bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1155 break;
1157 default:
1158 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1159 contents, rel->r_offset,
1160 relocation, rel->r_addend);
1163 return r;
1166 /* Relocate an AVR ELF section. */
1168 static bfd_boolean
1169 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1170 struct bfd_link_info *info,
1171 bfd *input_bfd,
1172 asection *input_section,
1173 bfd_byte *contents,
1174 Elf_Internal_Rela *relocs,
1175 Elf_Internal_Sym *local_syms,
1176 asection **local_sections)
1178 Elf_Internal_Shdr * symtab_hdr;
1179 struct elf_link_hash_entry ** sym_hashes;
1180 Elf_Internal_Rela * rel;
1181 Elf_Internal_Rela * relend;
1182 struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
1184 if (info->relocatable)
1185 return TRUE;
1187 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1188 sym_hashes = elf_sym_hashes (input_bfd);
1189 relend = relocs + input_section->reloc_count;
1191 for (rel = relocs; rel < relend; rel ++)
1193 reloc_howto_type * howto;
1194 unsigned long r_symndx;
1195 Elf_Internal_Sym * sym;
1196 asection * sec;
1197 struct elf_link_hash_entry * h;
1198 bfd_vma relocation;
1199 bfd_reloc_status_type r;
1200 const char * name;
1201 int r_type;
1203 /* This is a final link. */
1204 r_type = ELF32_R_TYPE (rel->r_info);
1205 r_symndx = ELF32_R_SYM (rel->r_info);
1206 howto = elf_avr_howto_table + ELF32_R_TYPE (rel->r_info);
1207 h = NULL;
1208 sym = NULL;
1209 sec = NULL;
1211 if (r_symndx < symtab_hdr->sh_info)
1213 sym = local_syms + r_symndx;
1214 sec = local_sections [r_symndx];
1215 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1217 name = bfd_elf_string_from_elf_section
1218 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1219 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1221 else
1223 bfd_boolean unresolved_reloc, warned;
1225 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1226 r_symndx, symtab_hdr, sym_hashes,
1227 h, sec, relocation,
1228 unresolved_reloc, warned);
1230 name = h->root.root.string;
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_avr3:
1305 val = E_AVR_MACH_AVR3;
1306 break;
1308 case bfd_mach_avr4:
1309 val = E_AVR_MACH_AVR4;
1310 break;
1312 case bfd_mach_avr5:
1313 val = E_AVR_MACH_AVR5;
1314 break;
1316 case bfd_mach_avr6:
1317 val = E_AVR_MACH_AVR6;
1318 break;
1321 elf_elfheader (abfd)->e_machine = EM_AVR;
1322 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1323 elf_elfheader (abfd)->e_flags |= val;
1324 elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
1327 /* Set the right machine number. */
1329 static bfd_boolean
1330 elf32_avr_object_p (bfd *abfd)
1332 unsigned int e_set = bfd_mach_avr2;
1334 if (elf_elfheader (abfd)->e_machine == EM_AVR
1335 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1337 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1339 switch (e_mach)
1341 default:
1342 case E_AVR_MACH_AVR2:
1343 e_set = bfd_mach_avr2;
1344 break;
1346 case E_AVR_MACH_AVR1:
1347 e_set = bfd_mach_avr1;
1348 break;
1350 case E_AVR_MACH_AVR3:
1351 e_set = bfd_mach_avr3;
1352 break;
1354 case E_AVR_MACH_AVR4:
1355 e_set = bfd_mach_avr4;
1356 break;
1358 case E_AVR_MACH_AVR5:
1359 e_set = bfd_mach_avr5;
1360 break;
1362 case E_AVR_MACH_AVR6:
1363 e_set = bfd_mach_avr6;
1364 break;
1367 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1368 e_set);
1372 /* Delete some bytes from a section while changing the size of an instruction.
1373 The parameter "addr" denotes the section-relative offset pointing just
1374 behind the shrinked instruction. "addr+count" point at the first
1375 byte just behind the original unshrinked instruction. */
1377 static bfd_boolean
1378 elf32_avr_relax_delete_bytes (bfd *abfd,
1379 asection *sec,
1380 bfd_vma addr,
1381 int count)
1383 Elf_Internal_Shdr *symtab_hdr;
1384 unsigned int sec_shndx;
1385 bfd_byte *contents;
1386 Elf_Internal_Rela *irel, *irelend;
1387 Elf_Internal_Rela *irelalign;
1388 Elf_Internal_Sym *isym;
1389 Elf_Internal_Sym *isymbuf = NULL;
1390 Elf_Internal_Sym *isymend;
1391 bfd_vma toaddr;
1392 struct elf_link_hash_entry **sym_hashes;
1393 struct elf_link_hash_entry **end_hashes;
1394 unsigned int symcount;
1396 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1397 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1398 contents = elf_section_data (sec)->this_hdr.contents;
1400 /* The deletion must stop at the next ALIGN reloc for an aligment
1401 power larger than the number of bytes we are deleting. */
1403 irelalign = NULL;
1404 toaddr = sec->size;
1406 irel = elf_section_data (sec)->relocs;
1407 irelend = irel + sec->reloc_count;
1409 /* Actually delete the bytes. */
1410 if (toaddr - addr - count > 0)
1411 memmove (contents + addr, contents + addr + count,
1412 (size_t) (toaddr - addr - count));
1413 sec->size -= count;
1415 /* Adjust all the reloc addresses. */
1416 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1418 bfd_vma old_reloc_address;
1419 bfd_vma shrinked_insn_address;
1421 old_reloc_address = (sec->output_section->vma
1422 + sec->output_offset + irel->r_offset);
1423 shrinked_insn_address = (sec->output_section->vma
1424 + sec->output_offset + addr - count);
1426 /* Get the new reloc address. */
1427 if ((irel->r_offset > addr
1428 && irel->r_offset < toaddr))
1430 if (debug_relax)
1431 printf ("Relocation at address 0x%x needs to be moved.\n"
1432 "Old section offset: 0x%x, New section offset: 0x%x \n",
1433 (unsigned int) old_reloc_address,
1434 (unsigned int) irel->r_offset,
1435 (unsigned int) ((irel->r_offset) - count));
1437 irel->r_offset -= count;
1442 /* The reloc's own addresses are now ok. However, we need to readjust
1443 the reloc's addend, i.e. the reloc's value if two conditions are met:
1444 1.) the reloc is relative to a symbol in this section that
1445 is located in front of the shrinked instruction
1446 2.) symbol plus addend end up behind the shrinked instruction.
1448 The most common case where this happens are relocs relative to
1449 the section-start symbol.
1451 This step needs to be done for all of the sections of the bfd. */
1454 struct bfd_section *isec;
1456 for (isec = abfd->sections; isec; isec = isec->next)
1458 bfd_vma symval;
1459 bfd_vma shrinked_insn_address;
1461 shrinked_insn_address = (sec->output_section->vma
1462 + sec->output_offset + addr - count);
1464 irelend = elf_section_data (isec)->relocs + isec->reloc_count;
1465 for (irel = elf_section_data (isec)->relocs;
1466 irel < irelend;
1467 irel++)
1469 /* Read this BFD's local symbols if we haven't done
1470 so already. */
1471 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1473 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1474 if (isymbuf == NULL)
1475 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1476 symtab_hdr->sh_info, 0,
1477 NULL, NULL, NULL);
1478 if (isymbuf == NULL)
1479 return FALSE;
1482 /* Get the value of the symbol referred to by the reloc. */
1483 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1485 /* A local symbol. */
1486 Elf_Internal_Sym *isym;
1487 asection *sym_sec;
1489 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1490 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1491 symval = isym->st_value;
1492 /* If the reloc is absolute, it will not have
1493 a symbol or section associated with it. */
1494 if (sym_sec == sec)
1496 symval += sym_sec->output_section->vma
1497 + sym_sec->output_offset;
1499 if (debug_relax)
1500 printf ("Checking if the relocation's "
1501 "addend needs corrections.\n"
1502 "Address of anchor symbol: 0x%x \n"
1503 "Address of relocation target: 0x%x \n"
1504 "Address of relaxed insn: 0x%x \n",
1505 (unsigned int) symval,
1506 (unsigned int) (symval + irel->r_addend),
1507 (unsigned int) shrinked_insn_address);
1509 if (symval <= shrinked_insn_address
1510 && (symval + irel->r_addend) > shrinked_insn_address)
1512 irel->r_addend -= count;
1514 if (debug_relax)
1515 printf ("Relocation's addend needed to be fixed \n");
1518 /* else...Reference symbol is absolute. No adjustment needed. */
1520 /* else...Reference symbol is extern. No need for adjusting
1521 the addend. */
1526 /* Adjust the local symbols defined in this section. */
1527 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1528 isymend = isym + symtab_hdr->sh_info;
1529 for (; isym < isymend; isym++)
1531 if (isym->st_shndx == sec_shndx
1532 && isym->st_value > addr
1533 && isym->st_value < toaddr)
1534 isym->st_value -= count;
1537 /* Now adjust the global symbols defined in this section. */
1538 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1539 - symtab_hdr->sh_info);
1540 sym_hashes = elf_sym_hashes (abfd);
1541 end_hashes = sym_hashes + symcount;
1542 for (; sym_hashes < end_hashes; sym_hashes++)
1544 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1545 if ((sym_hash->root.type == bfd_link_hash_defined
1546 || sym_hash->root.type == bfd_link_hash_defweak)
1547 && sym_hash->root.u.def.section == sec
1548 && sym_hash->root.u.def.value > addr
1549 && sym_hash->root.u.def.value < toaddr)
1551 sym_hash->root.u.def.value -= count;
1555 return TRUE;
1558 /* This function handles relaxing for the avr.
1559 Many important relaxing opportunities within functions are already
1560 realized by the compiler itself.
1561 Here we try to replace call (4 bytes) -> rcall (2 bytes)
1562 and jump -> rjmp (safes also 2 bytes).
1563 As well we now optimize seqences of
1564 - call/rcall function
1565 - ret
1566 to yield
1567 - jmp/rjmp function
1568 - ret
1569 . In case that within a sequence
1570 - jmp/rjmp label
1571 - ret
1572 the ret could no longer be reached it is optimized away. In order
1573 to check if the ret is no longer needed, it is checked that the ret's address
1574 is not the target of a branch or jump within the same section, it is checked
1575 that there is no skip instruction before the jmp/rjmp and that there
1576 is no local or global label place at the address of the ret.
1578 We refrain from relaxing within sections ".vectors" and
1579 ".jumptables" in order to maintain the position of the instructions.
1580 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
1581 if possible. (In future one could possibly use the space of the nop
1582 for the first instruction of the irq service function.
1584 The .jumptables sections is meant to be used for a future tablejump variant
1585 for the devices with 3-byte program counter where the table itself
1586 contains 4-byte jump instructions whose relative offset must not
1587 be changed. */
1589 static bfd_boolean
1590 elf32_avr_relax_section (bfd *abfd,
1591 asection *sec,
1592 struct bfd_link_info *link_info,
1593 bfd_boolean *again)
1595 Elf_Internal_Shdr *symtab_hdr;
1596 Elf_Internal_Rela *internal_relocs;
1597 Elf_Internal_Rela *irel, *irelend;
1598 bfd_byte *contents = NULL;
1599 Elf_Internal_Sym *isymbuf = NULL;
1600 static asection *last_input_section = NULL;
1601 static Elf_Internal_Rela *last_reloc = NULL;
1602 struct elf32_avr_link_hash_table *htab;
1604 htab = avr_link_hash_table (link_info);
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);
2410 target = hsh->target_value;
2412 /* Make a note of the offset within the stubs for this entry. */
2413 hsh->stub_offset = htab->stub_sec->size;
2414 loc = htab->stub_sec->contents + hsh->stub_offset;
2416 stub_bfd = htab->stub_sec->owner;
2418 if (debug_stubs)
2419 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
2420 (unsigned int) target,
2421 (unsigned int) hsh->stub_offset);
2423 /* We now have to add the information on the jump target to the bare
2424 opcode bits already set in jmp_insn. */
2426 /* Check for the alignment of the address. */
2427 if (target & 1)
2428 return FALSE;
2430 starget = target >> 1;
2431 jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
2432 bfd_put_16 (stub_bfd, jmp_insn, loc);
2433 bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
2435 htab->stub_sec->size += 4;
2437 /* Now add the entries in the address mapping table if there is still
2438 space left. */
2440 unsigned int nr;
2442 nr = htab->amt_entry_cnt + 1;
2443 if (nr <= htab->amt_max_entry_cnt)
2445 htab->amt_entry_cnt = nr;
2447 htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
2448 htab->amt_destination_addr[nr - 1] = target;
2452 return TRUE;
2455 static bfd_boolean
2456 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
2457 void *in_arg)
2459 struct elf32_avr_stub_hash_entry *hsh;
2460 struct elf32_avr_link_hash_table *htab;
2462 htab = in_arg;
2463 hsh = avr_stub_hash_entry (bh);
2464 hsh->is_actually_needed = FALSE;
2466 return TRUE;
2469 static bfd_boolean
2470 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2472 struct elf32_avr_stub_hash_entry *hsh;
2473 struct elf32_avr_link_hash_table *htab;
2474 int size;
2476 /* Massage our args to the form they really have. */
2477 hsh = avr_stub_hash_entry (bh);
2478 htab = in_arg;
2480 if (hsh->is_actually_needed)
2481 size = 4;
2482 else
2483 size = 0;
2485 htab->stub_sec->size += size;
2486 return TRUE;
2489 void
2490 elf32_avr_setup_params (struct bfd_link_info *info,
2491 bfd *avr_stub_bfd,
2492 asection *avr_stub_section,
2493 bfd_boolean no_stubs,
2494 bfd_boolean deb_stubs,
2495 bfd_boolean deb_relax,
2496 bfd_vma pc_wrap_around,
2497 bfd_boolean call_ret_replacement)
2499 struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
2501 htab->stub_sec = avr_stub_section;
2502 htab->stub_bfd = avr_stub_bfd;
2503 htab->no_stubs = no_stubs;
2505 debug_relax = deb_relax;
2506 debug_stubs = deb_stubs;
2507 avr_pc_wrap_around = pc_wrap_around;
2508 avr_replace_call_ret_sequences = call_ret_replacement;
2512 /* Set up various things so that we can make a list of input sections
2513 for each output section included in the link. Returns -1 on error,
2514 0 when no stubs will be needed, and 1 on success. It also sets
2515 information on the stubs bfd and the stub section in the info
2516 struct. */
2519 elf32_avr_setup_section_lists (bfd *output_bfd,
2520 struct bfd_link_info *info)
2522 bfd *input_bfd;
2523 unsigned int bfd_count;
2524 int top_id, top_index;
2525 asection *section;
2526 asection **input_list, **list;
2527 bfd_size_type amt;
2528 struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
2530 if (htab->no_stubs)
2531 return 0;
2533 /* Count the number of input BFDs and find the top input section id. */
2534 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2535 input_bfd != NULL;
2536 input_bfd = input_bfd->link_next)
2538 bfd_count += 1;
2539 for (section = input_bfd->sections;
2540 section != NULL;
2541 section = section->next)
2542 if (top_id < section->id)
2543 top_id = section->id;
2546 htab->bfd_count = bfd_count;
2548 /* We can't use output_bfd->section_count here to find the top output
2549 section index as some sections may have been removed, and
2550 strip_excluded_output_sections doesn't renumber the indices. */
2551 for (section = output_bfd->sections, top_index = 0;
2552 section != NULL;
2553 section = section->next)
2554 if (top_index < section->index)
2555 top_index = section->index;
2557 htab->top_index = top_index;
2558 amt = sizeof (asection *) * (top_index + 1);
2559 input_list = bfd_malloc (amt);
2560 htab->input_list = input_list;
2561 if (input_list == NULL)
2562 return -1;
2564 /* For sections we aren't interested in, mark their entries with a
2565 value we can check later. */
2566 list = input_list + top_index;
2568 *list = bfd_abs_section_ptr;
2569 while (list-- != input_list);
2571 for (section = output_bfd->sections;
2572 section != NULL;
2573 section = section->next)
2574 if ((section->flags & SEC_CODE) != 0)
2575 input_list[section->index] = NULL;
2577 return 1;
2581 /* Read in all local syms for all input bfds, and create hash entries
2582 for export stubs if we are building a multi-subspace shared lib.
2583 Returns -1 on error, 0 otherwise. */
2585 static int
2586 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
2588 unsigned int bfd_indx;
2589 Elf_Internal_Sym *local_syms, **all_local_syms;
2590 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
2592 /* We want to read in symbol extension records only once. To do this
2593 we need to read in the local symbols in parallel and save them for
2594 later use; so hold pointers to the local symbols in an array. */
2595 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2596 all_local_syms = bfd_zmalloc (amt);
2597 htab->all_local_syms = all_local_syms;
2598 if (all_local_syms == NULL)
2599 return -1;
2601 /* Walk over all the input BFDs, swapping in local symbols.
2602 If we are creating a shared library, create hash entries for the
2603 export stubs. */
2604 for (bfd_indx = 0;
2605 input_bfd != NULL;
2606 input_bfd = input_bfd->link_next, bfd_indx++)
2608 Elf_Internal_Shdr *symtab_hdr;
2610 /* We'll need the symbol table in a second. */
2611 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2612 if (symtab_hdr->sh_info == 0)
2613 continue;
2615 /* We need an array of the local symbols attached to the input bfd. */
2616 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2617 if (local_syms == NULL)
2619 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2620 symtab_hdr->sh_info, 0,
2621 NULL, NULL, NULL);
2622 /* Cache them for elf_link_input_bfd. */
2623 symtab_hdr->contents = (unsigned char *) local_syms;
2625 if (local_syms == NULL)
2626 return -1;
2628 all_local_syms[bfd_indx] = local_syms;
2631 return 0;
2634 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
2636 bfd_boolean
2637 elf32_avr_size_stubs (bfd *output_bfd,
2638 struct bfd_link_info *info,
2639 bfd_boolean is_prealloc_run)
2641 struct elf32_avr_link_hash_table *htab;
2642 int stub_changed = 0;
2644 htab = avr_link_hash_table (info);
2646 /* At this point we initialize htab->vector_base
2647 To the start of the text output section. */
2648 htab->vector_base = htab->stub_sec->output_section->vma;
2650 if (get_local_syms (info->input_bfds, info))
2652 if (htab->all_local_syms)
2653 goto error_ret_free_local;
2654 return FALSE;
2657 if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
2659 struct elf32_avr_stub_hash_entry *test;
2661 test = avr_add_stub ("Hugo",htab);
2662 test->target_value = 0x123456;
2663 test->stub_offset = 13;
2665 test = avr_add_stub ("Hugo2",htab);
2666 test->target_value = 0x84210;
2667 test->stub_offset = 14;
2670 while (1)
2672 bfd *input_bfd;
2673 unsigned int bfd_indx;
2675 /* We will have to re-generate the stub hash table each time anything
2676 in memory has changed. */
2678 bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
2679 for (input_bfd = info->input_bfds, bfd_indx = 0;
2680 input_bfd != NULL;
2681 input_bfd = input_bfd->link_next, bfd_indx++)
2683 Elf_Internal_Shdr *symtab_hdr;
2684 asection *section;
2685 Elf_Internal_Sym *local_syms;
2687 /* We'll need the symbol table in a second. */
2688 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2689 if (symtab_hdr->sh_info == 0)
2690 continue;
2692 local_syms = htab->all_local_syms[bfd_indx];
2694 /* Walk over each section attached to the input bfd. */
2695 for (section = input_bfd->sections;
2696 section != NULL;
2697 section = section->next)
2699 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2701 /* If there aren't any relocs, then there's nothing more
2702 to do. */
2703 if ((section->flags & SEC_RELOC) == 0
2704 || section->reloc_count == 0)
2705 continue;
2707 /* If this section is a link-once section that will be
2708 discarded, then don't create any stubs. */
2709 if (section->output_section == NULL
2710 || section->output_section->owner != output_bfd)
2711 continue;
2713 /* Get the relocs. */
2714 internal_relocs
2715 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2716 info->keep_memory);
2717 if (internal_relocs == NULL)
2718 goto error_ret_free_local;
2720 /* Now examine each relocation. */
2721 irela = internal_relocs;
2722 irelaend = irela + section->reloc_count;
2723 for (; irela < irelaend; irela++)
2725 unsigned int r_type, r_indx;
2726 struct elf32_avr_stub_hash_entry *hsh;
2727 asection *sym_sec;
2728 bfd_vma sym_value;
2729 bfd_vma destination;
2730 struct elf_link_hash_entry *hh;
2731 char *stub_name;
2733 r_type = ELF32_R_TYPE (irela->r_info);
2734 r_indx = ELF32_R_SYM (irela->r_info);
2736 /* Only look for 16 bit GS relocs. No other reloc will need a
2737 stub. */
2738 if (!((r_type == R_AVR_16_PM)
2739 || (r_type == R_AVR_LO8_LDI_GS)
2740 || (r_type == R_AVR_HI8_LDI_GS)))
2741 continue;
2743 /* Now determine the call target, its name, value,
2744 section. */
2745 sym_sec = NULL;
2746 sym_value = 0;
2747 destination = 0;
2748 hh = NULL;
2749 if (r_indx < symtab_hdr->sh_info)
2751 /* It's a local symbol. */
2752 Elf_Internal_Sym *sym;
2753 Elf_Internal_Shdr *hdr;
2755 sym = local_syms + r_indx;
2756 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2757 sym_sec = hdr->bfd_section;
2758 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2759 sym_value = sym->st_value;
2760 destination = (sym_value + irela->r_addend
2761 + sym_sec->output_offset
2762 + sym_sec->output_section->vma);
2764 else
2766 /* It's an external symbol. */
2767 int e_indx;
2769 e_indx = r_indx - symtab_hdr->sh_info;
2770 hh = elf_sym_hashes (input_bfd)[e_indx];
2772 while (hh->root.type == bfd_link_hash_indirect
2773 || hh->root.type == bfd_link_hash_warning)
2774 hh = (struct elf_link_hash_entry *)
2775 (hh->root.u.i.link);
2777 if (hh->root.type == bfd_link_hash_defined
2778 || hh->root.type == bfd_link_hash_defweak)
2780 sym_sec = hh->root.u.def.section;
2781 sym_value = hh->root.u.def.value;
2782 if (sym_sec->output_section != NULL)
2783 destination = (sym_value + irela->r_addend
2784 + sym_sec->output_offset
2785 + sym_sec->output_section->vma);
2787 else if (hh->root.type == bfd_link_hash_undefweak)
2789 if (! info->shared)
2790 continue;
2792 else if (hh->root.type == bfd_link_hash_undefined)
2794 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2795 && (ELF_ST_VISIBILITY (hh->other)
2796 == STV_DEFAULT)))
2797 continue;
2799 else
2801 bfd_set_error (bfd_error_bad_value);
2803 error_ret_free_internal:
2804 if (elf_section_data (section)->relocs == NULL)
2805 free (internal_relocs);
2806 goto error_ret_free_local;
2810 if (! avr_stub_is_required_for_16_bit_reloc
2811 (destination - htab->vector_base))
2813 if (!is_prealloc_run)
2814 /* We are having a reloc that does't need a stub. */
2815 continue;
2817 /* We don't right now know if a stub will be needed.
2818 Let's rather be on the safe side. */
2821 /* Get the name of this stub. */
2822 stub_name = avr_stub_name (sym_sec, sym_value, irela);
2824 if (!stub_name)
2825 goto error_ret_free_internal;
2828 hsh = avr_stub_hash_lookup (&htab->bstab,
2829 stub_name,
2830 FALSE, FALSE);
2831 if (hsh != NULL)
2833 /* The proper stub has already been created. Mark it
2834 to be used and write the possibly changed destination
2835 value. */
2836 hsh->is_actually_needed = TRUE;
2837 hsh->target_value = destination;
2838 free (stub_name);
2839 continue;
2842 hsh = avr_add_stub (stub_name, htab);
2843 if (hsh == NULL)
2845 free (stub_name);
2846 goto error_ret_free_internal;
2849 hsh->is_actually_needed = TRUE;
2850 hsh->target_value = destination;
2852 if (debug_stubs)
2853 printf ("Adding stub with destination 0x%x to the"
2854 " hash table.\n", (unsigned int) destination);
2855 if (debug_stubs)
2856 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
2858 stub_changed = TRUE;
2861 /* We're done with the internal relocs, free them. */
2862 if (elf_section_data (section)->relocs == NULL)
2863 free (internal_relocs);
2867 /* Re-Calculate the number of needed stubs. */
2868 htab->stub_sec->size = 0;
2869 bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
2871 if (!stub_changed)
2872 break;
2874 stub_changed = FALSE;
2877 free (htab->all_local_syms);
2878 return TRUE;
2880 error_ret_free_local:
2881 free (htab->all_local_syms);
2882 return FALSE;
2886 /* Build all the stubs associated with the current output file. The
2887 stubs are kept in a hash table attached to the main linker hash
2888 table. We also set up the .plt entries for statically linked PIC
2889 functions here. This function is called via hppaelf_finish in the
2890 linker. */
2892 bfd_boolean
2893 elf32_avr_build_stubs (struct bfd_link_info *info)
2895 asection *stub_sec;
2896 struct bfd_hash_table *table;
2897 struct elf32_avr_link_hash_table *htab;
2898 bfd_size_type total_size = 0;
2900 htab = avr_link_hash_table (info);
2902 /* In case that there were several stub sections: */
2903 for (stub_sec = htab->stub_bfd->sections;
2904 stub_sec != NULL;
2905 stub_sec = stub_sec->next)
2907 bfd_size_type size;
2909 /* Allocate memory to hold the linker stubs. */
2910 size = stub_sec->size;
2911 total_size += size;
2913 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2914 if (stub_sec->contents == NULL && size != 0)
2915 return FALSE;
2916 stub_sec->size = 0;
2919 /* Allocate memory for the adress mapping table. */
2920 htab->amt_entry_cnt = 0;
2921 htab->amt_max_entry_cnt = total_size / 4;
2922 htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
2923 * htab->amt_max_entry_cnt);
2924 htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
2925 * htab->amt_max_entry_cnt );
2927 if (debug_stubs)
2928 printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
2930 /* Build the stubs as directed by the stub hash table. */
2931 table = &htab->bstab;
2932 bfd_hash_traverse (table, avr_build_one_stub, info);
2934 if (debug_stubs)
2935 printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
2937 return TRUE;
2940 #define ELF_ARCH bfd_arch_avr
2941 #define ELF_MACHINE_CODE EM_AVR
2942 #define ELF_MACHINE_ALT1 EM_AVR_OLD
2943 #define ELF_MAXPAGESIZE 1
2945 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
2946 #define TARGET_LITTLE_NAME "elf32-avr"
2948 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
2949 #define bfd_elf32_bfd_link_hash_table_free elf32_avr_link_hash_table_free
2951 #define elf_info_to_howto avr_info_to_howto_rela
2952 #define elf_info_to_howto_rel NULL
2953 #define elf_backend_relocate_section elf32_avr_relocate_section
2954 #define elf_backend_gc_mark_hook elf32_avr_gc_mark_hook
2955 #define elf_backend_gc_sweep_hook elf32_avr_gc_sweep_hook
2956 #define elf_backend_check_relocs elf32_avr_check_relocs
2957 #define elf_backend_can_gc_sections 1
2958 #define elf_backend_rela_normal 1
2959 #define elf_backend_final_write_processing \
2960 bfd_elf_avr_final_write_processing
2961 #define elf_backend_object_p elf32_avr_object_p
2963 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
2964 #define bfd_elf32_bfd_get_relocated_section_contents \
2965 elf32_avr_get_relocated_section_contents
2967 #include "elf32-target.h"