* tc-score.c (data_op2): Check invalid operands.
[binutils.git] / bfd / elf32-avr.c
blob40cc0830fb7b28302e7f5c7883ced31525ea9e0b
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 /* Look through the relocs for a section during the first phase.
692 Since we don't do .gots or .plts, we just need to consider the
693 virtual table relocs for gc. */
695 static bfd_boolean
696 elf32_avr_check_relocs (bfd *abfd,
697 struct bfd_link_info *info,
698 asection *sec,
699 const Elf_Internal_Rela *relocs)
701 Elf_Internal_Shdr *symtab_hdr;
702 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
703 const Elf_Internal_Rela *rel;
704 const Elf_Internal_Rela *rel_end;
706 if (info->relocatable)
707 return TRUE;
709 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
710 sym_hashes = elf_sym_hashes (abfd);
711 sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
712 if (!elf_bad_symtab (abfd))
713 sym_hashes_end -= symtab_hdr->sh_info;
715 rel_end = relocs + sec->reloc_count;
716 for (rel = relocs; rel < rel_end; rel++)
718 struct elf_link_hash_entry *h;
719 unsigned long r_symndx;
721 r_symndx = ELF32_R_SYM (rel->r_info);
722 if (r_symndx < symtab_hdr->sh_info)
723 h = NULL;
724 else
726 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
727 while (h->root.type == bfd_link_hash_indirect
728 || h->root.type == bfd_link_hash_warning)
729 h = (struct elf_link_hash_entry *) h->root.u.i.link;
733 return TRUE;
736 static bfd_boolean
737 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
739 return (relocation >= 0x020000);
742 /* Returns the address of the corresponding stub if there is one.
743 Returns otherwise an address above 0x020000. This function
744 could also be used, if there is no knowledge on the section where
745 the destination is found. */
747 static bfd_vma
748 avr_get_stub_addr (bfd_vma srel,
749 struct elf32_avr_link_hash_table *htab)
751 unsigned int index;
752 bfd_vma stub_sec_addr =
753 (htab->stub_sec->output_section->vma +
754 htab->stub_sec->output_offset);
756 for (index = 0; index < htab->amt_max_entry_cnt; index ++)
757 if (htab->amt_destination_addr[index] == srel)
758 return htab->amt_stub_offsets[index] + stub_sec_addr;
760 /* Return an address that could not be reached by 16 bit relocs. */
761 return 0x020000;
764 /* Perform a single relocation. By default we use the standard BFD
765 routines, but a few relocs, we have to do them ourselves. */
767 static bfd_reloc_status_type
768 avr_final_link_relocate (reloc_howto_type * howto,
769 bfd * input_bfd,
770 asection * input_section,
771 bfd_byte * contents,
772 Elf_Internal_Rela * rel,
773 bfd_vma relocation,
774 struct elf32_avr_link_hash_table * htab)
776 bfd_reloc_status_type r = bfd_reloc_ok;
777 bfd_vma x;
778 bfd_signed_vma srel;
779 bfd_signed_vma reloc_addr;
780 bfd_boolean use_stubs = FALSE;
781 /* Usually is 0, unless we are generating code for a bootloader. */
782 bfd_signed_vma base_addr = htab->vector_base;
784 /* Absolute addr of the reloc in the final excecutable. */
785 reloc_addr = rel->r_offset + input_section->output_section->vma
786 + input_section->output_offset;
788 switch (howto->type)
790 case R_AVR_7_PCREL:
791 contents += rel->r_offset;
792 srel = (bfd_signed_vma) relocation;
793 srel += rel->r_addend;
794 srel -= rel->r_offset;
795 srel -= 2; /* Branch instructions add 2 to the PC... */
796 srel -= (input_section->output_section->vma +
797 input_section->output_offset);
799 if (srel & 1)
800 return bfd_reloc_outofrange;
801 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
802 return bfd_reloc_overflow;
803 x = bfd_get_16 (input_bfd, contents);
804 x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
805 bfd_put_16 (input_bfd, x, contents);
806 break;
808 case R_AVR_13_PCREL:
809 contents += rel->r_offset;
810 srel = (bfd_signed_vma) relocation;
811 srel += rel->r_addend;
812 srel -= rel->r_offset;
813 srel -= 2; /* Branch instructions add 2 to the PC... */
814 srel -= (input_section->output_section->vma +
815 input_section->output_offset);
817 if (srel & 1)
818 return bfd_reloc_outofrange;
820 srel = avr_relative_distance_considering_wrap_around (srel);
822 /* AVR addresses commands as words. */
823 srel >>= 1;
825 /* Check for overflow. */
826 if (srel < -2048 || srel > 2047)
828 /* Relative distance is too large. */
830 /* Always apply WRAPAROUND for avr2 and avr4. */
831 switch (bfd_get_mach (input_bfd))
833 case bfd_mach_avr2:
834 case bfd_mach_avr4:
835 break;
837 default:
838 return bfd_reloc_overflow;
842 x = bfd_get_16 (input_bfd, contents);
843 x = (x & 0xf000) | (srel & 0xfff);
844 bfd_put_16 (input_bfd, x, contents);
845 break;
847 case R_AVR_LO8_LDI:
848 contents += rel->r_offset;
849 srel = (bfd_signed_vma) relocation + rel->r_addend;
850 x = bfd_get_16 (input_bfd, contents);
851 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
852 bfd_put_16 (input_bfd, x, contents);
853 break;
855 case R_AVR_LDI:
856 contents += rel->r_offset;
857 srel = (bfd_signed_vma) relocation + rel->r_addend;
858 if (((srel > 0) && (srel & 0xffff) > 255)
859 || ((srel < 0) && ((-srel) & 0xffff) > 128))
860 /* Remove offset for data/eeprom section. */
861 return bfd_reloc_overflow;
863 x = bfd_get_16 (input_bfd, contents);
864 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
865 bfd_put_16 (input_bfd, x, contents);
866 break;
868 case R_AVR_6:
869 contents += rel->r_offset;
870 srel = (bfd_signed_vma) relocation + rel->r_addend;
871 if (((srel & 0xffff) > 63) || (srel < 0))
872 /* Remove offset for data/eeprom section. */
873 return bfd_reloc_overflow;
874 x = bfd_get_16 (input_bfd, contents);
875 x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
876 | ((srel & (1 << 5)) << 8));
877 bfd_put_16 (input_bfd, x, contents);
878 break;
880 case R_AVR_6_ADIW:
881 contents += rel->r_offset;
882 srel = (bfd_signed_vma) relocation + rel->r_addend;
883 if (((srel & 0xffff) > 63) || (srel < 0))
884 /* Remove offset for data/eeprom section. */
885 return bfd_reloc_overflow;
886 x = bfd_get_16 (input_bfd, contents);
887 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
888 bfd_put_16 (input_bfd, x, contents);
889 break;
891 case R_AVR_HI8_LDI:
892 contents += rel->r_offset;
893 srel = (bfd_signed_vma) relocation + rel->r_addend;
894 srel = (srel >> 8) & 0xff;
895 x = bfd_get_16 (input_bfd, contents);
896 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
897 bfd_put_16 (input_bfd, x, contents);
898 break;
900 case R_AVR_HH8_LDI:
901 contents += rel->r_offset;
902 srel = (bfd_signed_vma) relocation + rel->r_addend;
903 srel = (srel >> 16) & 0xff;
904 x = bfd_get_16 (input_bfd, contents);
905 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
906 bfd_put_16 (input_bfd, x, contents);
907 break;
909 case R_AVR_MS8_LDI:
910 contents += rel->r_offset;
911 srel = (bfd_signed_vma) relocation + rel->r_addend;
912 srel = (srel >> 24) & 0xff;
913 x = bfd_get_16 (input_bfd, contents);
914 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
915 bfd_put_16 (input_bfd, x, contents);
916 break;
918 case R_AVR_LO8_LDI_NEG:
919 contents += rel->r_offset;
920 srel = (bfd_signed_vma) relocation + rel->r_addend;
921 srel = -srel;
922 x = bfd_get_16 (input_bfd, contents);
923 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
924 bfd_put_16 (input_bfd, x, contents);
925 break;
927 case R_AVR_HI8_LDI_NEG:
928 contents += rel->r_offset;
929 srel = (bfd_signed_vma) relocation + rel->r_addend;
930 srel = -srel;
931 srel = (srel >> 8) & 0xff;
932 x = bfd_get_16 (input_bfd, contents);
933 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
934 bfd_put_16 (input_bfd, x, contents);
935 break;
937 case R_AVR_HH8_LDI_NEG:
938 contents += rel->r_offset;
939 srel = (bfd_signed_vma) relocation + rel->r_addend;
940 srel = -srel;
941 srel = (srel >> 16) & 0xff;
942 x = bfd_get_16 (input_bfd, contents);
943 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
944 bfd_put_16 (input_bfd, x, contents);
945 break;
947 case R_AVR_MS8_LDI_NEG:
948 contents += rel->r_offset;
949 srel = (bfd_signed_vma) relocation + rel->r_addend;
950 srel = -srel;
951 srel = (srel >> 24) & 0xff;
952 x = bfd_get_16 (input_bfd, contents);
953 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
954 bfd_put_16 (input_bfd, x, contents);
955 break;
957 case R_AVR_LO8_LDI_GS:
958 use_stubs = (!htab->no_stubs);
959 /* Fall through. */
960 case R_AVR_LO8_LDI_PM:
961 contents += rel->r_offset;
962 srel = (bfd_signed_vma) relocation + rel->r_addend;
964 if (use_stubs
965 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
967 bfd_vma old_srel = srel;
969 /* We need to use the address of the stub instead. */
970 srel = avr_get_stub_addr (srel, htab);
971 if (debug_stubs)
972 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
973 "reloc at address 0x%x.\n",
974 (unsigned int) srel,
975 (unsigned int) old_srel,
976 (unsigned int) reloc_addr);
978 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
979 return bfd_reloc_outofrange;
982 if (srel & 1)
983 return bfd_reloc_outofrange;
984 srel = srel >> 1;
985 x = bfd_get_16 (input_bfd, contents);
986 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
987 bfd_put_16 (input_bfd, x, contents);
988 break;
990 case R_AVR_HI8_LDI_GS:
991 use_stubs = (!htab->no_stubs);
992 /* Fall through. */
993 case R_AVR_HI8_LDI_PM:
994 contents += rel->r_offset;
995 srel = (bfd_signed_vma) relocation + rel->r_addend;
997 if (use_stubs
998 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1000 bfd_vma old_srel = srel;
1002 /* We need to use the address of the stub instead. */
1003 srel = avr_get_stub_addr (srel, htab);
1004 if (debug_stubs)
1005 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1006 "reloc at address 0x%x.\n",
1007 (unsigned int) srel,
1008 (unsigned int) old_srel,
1009 (unsigned int) reloc_addr);
1011 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1012 return bfd_reloc_outofrange;
1015 if (srel & 1)
1016 return bfd_reloc_outofrange;
1017 srel = srel >> 1;
1018 srel = (srel >> 8) & 0xff;
1019 x = bfd_get_16 (input_bfd, contents);
1020 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1021 bfd_put_16 (input_bfd, x, contents);
1022 break;
1024 case R_AVR_HH8_LDI_PM:
1025 contents += rel->r_offset;
1026 srel = (bfd_signed_vma) relocation + rel->r_addend;
1027 if (srel & 1)
1028 return bfd_reloc_outofrange;
1029 srel = srel >> 1;
1030 srel = (srel >> 16) & 0xff;
1031 x = bfd_get_16 (input_bfd, contents);
1032 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1033 bfd_put_16 (input_bfd, x, contents);
1034 break;
1036 case R_AVR_LO8_LDI_PM_NEG:
1037 contents += rel->r_offset;
1038 srel = (bfd_signed_vma) relocation + rel->r_addend;
1039 srel = -srel;
1040 if (srel & 1)
1041 return bfd_reloc_outofrange;
1042 srel = srel >> 1;
1043 x = bfd_get_16 (input_bfd, contents);
1044 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1045 bfd_put_16 (input_bfd, x, contents);
1046 break;
1048 case R_AVR_HI8_LDI_PM_NEG:
1049 contents += rel->r_offset;
1050 srel = (bfd_signed_vma) relocation + rel->r_addend;
1051 srel = -srel;
1052 if (srel & 1)
1053 return bfd_reloc_outofrange;
1054 srel = srel >> 1;
1055 srel = (srel >> 8) & 0xff;
1056 x = bfd_get_16 (input_bfd, contents);
1057 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1058 bfd_put_16 (input_bfd, x, contents);
1059 break;
1061 case R_AVR_HH8_LDI_PM_NEG:
1062 contents += rel->r_offset;
1063 srel = (bfd_signed_vma) relocation + rel->r_addend;
1064 srel = -srel;
1065 if (srel & 1)
1066 return bfd_reloc_outofrange;
1067 srel = srel >> 1;
1068 srel = (srel >> 16) & 0xff;
1069 x = bfd_get_16 (input_bfd, contents);
1070 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1071 bfd_put_16 (input_bfd, x, contents);
1072 break;
1074 case R_AVR_CALL:
1075 contents += rel->r_offset;
1076 srel = (bfd_signed_vma) relocation + rel->r_addend;
1077 if (srel & 1)
1078 return bfd_reloc_outofrange;
1079 srel = srel >> 1;
1080 x = bfd_get_16 (input_bfd, contents);
1081 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1082 bfd_put_16 (input_bfd, x, contents);
1083 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1084 break;
1086 case R_AVR_16_PM:
1087 use_stubs = (!htab->no_stubs);
1088 contents += rel->r_offset;
1089 srel = (bfd_signed_vma) relocation + rel->r_addend;
1091 if (use_stubs
1092 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1094 bfd_vma old_srel = srel;
1096 /* We need to use the address of the stub instead. */
1097 srel = avr_get_stub_addr (srel,htab);
1098 if (debug_stubs)
1099 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1100 "reloc at address 0x%x.\n",
1101 (unsigned int) srel,
1102 (unsigned int) old_srel,
1103 (unsigned int) reloc_addr);
1105 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1106 return bfd_reloc_outofrange;
1109 if (srel & 1)
1110 return bfd_reloc_outofrange;
1111 srel = srel >> 1;
1112 bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1113 break;
1115 default:
1116 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1117 contents, rel->r_offset,
1118 relocation, rel->r_addend);
1121 return r;
1124 /* Relocate an AVR ELF section. */
1126 static bfd_boolean
1127 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1128 struct bfd_link_info *info,
1129 bfd *input_bfd,
1130 asection *input_section,
1131 bfd_byte *contents,
1132 Elf_Internal_Rela *relocs,
1133 Elf_Internal_Sym *local_syms,
1134 asection **local_sections)
1136 Elf_Internal_Shdr * symtab_hdr;
1137 struct elf_link_hash_entry ** sym_hashes;
1138 Elf_Internal_Rela * rel;
1139 Elf_Internal_Rela * relend;
1140 struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
1142 if (info->relocatable)
1143 return TRUE;
1145 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1146 sym_hashes = elf_sym_hashes (input_bfd);
1147 relend = relocs + input_section->reloc_count;
1149 for (rel = relocs; rel < relend; rel ++)
1151 reloc_howto_type * howto;
1152 unsigned long r_symndx;
1153 Elf_Internal_Sym * sym;
1154 asection * sec;
1155 struct elf_link_hash_entry * h;
1156 bfd_vma relocation;
1157 bfd_reloc_status_type r;
1158 const char * name;
1159 int r_type;
1161 /* This is a final link. */
1162 r_type = ELF32_R_TYPE (rel->r_info);
1163 r_symndx = ELF32_R_SYM (rel->r_info);
1164 howto = elf_avr_howto_table + ELF32_R_TYPE (rel->r_info);
1165 h = NULL;
1166 sym = NULL;
1167 sec = NULL;
1169 if (r_symndx < symtab_hdr->sh_info)
1171 sym = local_syms + r_symndx;
1172 sec = local_sections [r_symndx];
1173 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1175 name = bfd_elf_string_from_elf_section
1176 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1177 name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
1179 else
1181 bfd_boolean unresolved_reloc, warned;
1183 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1184 r_symndx, symtab_hdr, sym_hashes,
1185 h, sec, relocation,
1186 unresolved_reloc, warned);
1188 name = h->root.root.string;
1191 r = avr_final_link_relocate (howto, input_bfd, input_section,
1192 contents, rel, relocation, htab);
1194 if (r != bfd_reloc_ok)
1196 const char * msg = (const char *) NULL;
1198 switch (r)
1200 case bfd_reloc_overflow:
1201 r = info->callbacks->reloc_overflow
1202 (info, (h ? &h->root : NULL),
1203 name, howto->name, (bfd_vma) 0,
1204 input_bfd, input_section, rel->r_offset);
1205 break;
1207 case bfd_reloc_undefined:
1208 r = info->callbacks->undefined_symbol
1209 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1210 break;
1212 case bfd_reloc_outofrange:
1213 msg = _("internal error: out of range error");
1214 break;
1216 case bfd_reloc_notsupported:
1217 msg = _("internal error: unsupported relocation error");
1218 break;
1220 case bfd_reloc_dangerous:
1221 msg = _("internal error: dangerous relocation");
1222 break;
1224 default:
1225 msg = _("internal error: unknown error");
1226 break;
1229 if (msg)
1230 r = info->callbacks->warning
1231 (info, msg, name, input_bfd, input_section, rel->r_offset);
1233 if (! r)
1234 return FALSE;
1238 return TRUE;
1241 /* The final processing done just before writing out a AVR ELF object
1242 file. This gets the AVR architecture right based on the machine
1243 number. */
1245 static void
1246 bfd_elf_avr_final_write_processing (bfd *abfd,
1247 bfd_boolean linker ATTRIBUTE_UNUSED)
1249 unsigned long val;
1251 switch (bfd_get_mach (abfd))
1253 default:
1254 case bfd_mach_avr2:
1255 val = E_AVR_MACH_AVR2;
1256 break;
1258 case bfd_mach_avr1:
1259 val = E_AVR_MACH_AVR1;
1260 break;
1262 case bfd_mach_avr3:
1263 val = E_AVR_MACH_AVR3;
1264 break;
1266 case bfd_mach_avr4:
1267 val = E_AVR_MACH_AVR4;
1268 break;
1270 case bfd_mach_avr5:
1271 val = E_AVR_MACH_AVR5;
1272 break;
1274 case bfd_mach_avr6:
1275 val = E_AVR_MACH_AVR6;
1276 break;
1279 elf_elfheader (abfd)->e_machine = EM_AVR;
1280 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1281 elf_elfheader (abfd)->e_flags |= val;
1282 elf_elfheader (abfd)->e_flags |= EF_AVR_LINKRELAX_PREPARED;
1285 /* Set the right machine number. */
1287 static bfd_boolean
1288 elf32_avr_object_p (bfd *abfd)
1290 unsigned int e_set = bfd_mach_avr2;
1292 if (elf_elfheader (abfd)->e_machine == EM_AVR
1293 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1295 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1297 switch (e_mach)
1299 default:
1300 case E_AVR_MACH_AVR2:
1301 e_set = bfd_mach_avr2;
1302 break;
1304 case E_AVR_MACH_AVR1:
1305 e_set = bfd_mach_avr1;
1306 break;
1308 case E_AVR_MACH_AVR3:
1309 e_set = bfd_mach_avr3;
1310 break;
1312 case E_AVR_MACH_AVR4:
1313 e_set = bfd_mach_avr4;
1314 break;
1316 case E_AVR_MACH_AVR5:
1317 e_set = bfd_mach_avr5;
1318 break;
1320 case E_AVR_MACH_AVR6:
1321 e_set = bfd_mach_avr6;
1322 break;
1325 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1326 e_set);
1330 /* Delete some bytes from a section while changing the size of an instruction.
1331 The parameter "addr" denotes the section-relative offset pointing just
1332 behind the shrinked instruction. "addr+count" point at the first
1333 byte just behind the original unshrinked instruction. */
1335 static bfd_boolean
1336 elf32_avr_relax_delete_bytes (bfd *abfd,
1337 asection *sec,
1338 bfd_vma addr,
1339 int count)
1341 Elf_Internal_Shdr *symtab_hdr;
1342 unsigned int sec_shndx;
1343 bfd_byte *contents;
1344 Elf_Internal_Rela *irel, *irelend;
1345 Elf_Internal_Rela *irelalign;
1346 Elf_Internal_Sym *isym;
1347 Elf_Internal_Sym *isymbuf = NULL;
1348 Elf_Internal_Sym *isymend;
1349 bfd_vma toaddr;
1350 struct elf_link_hash_entry **sym_hashes;
1351 struct elf_link_hash_entry **end_hashes;
1352 unsigned int symcount;
1354 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1355 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1356 contents = elf_section_data (sec)->this_hdr.contents;
1358 /* The deletion must stop at the next ALIGN reloc for an aligment
1359 power larger than the number of bytes we are deleting. */
1361 irelalign = NULL;
1362 toaddr = sec->size;
1364 irel = elf_section_data (sec)->relocs;
1365 irelend = irel + sec->reloc_count;
1367 /* Actually delete the bytes. */
1368 if (toaddr - addr - count > 0)
1369 memmove (contents + addr, contents + addr + count,
1370 (size_t) (toaddr - addr - count));
1371 sec->size -= count;
1373 /* Adjust all the reloc addresses. */
1374 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1376 bfd_vma old_reloc_address;
1377 bfd_vma shrinked_insn_address;
1379 old_reloc_address = (sec->output_section->vma
1380 + sec->output_offset + irel->r_offset);
1381 shrinked_insn_address = (sec->output_section->vma
1382 + sec->output_offset + addr - count);
1384 /* Get the new reloc address. */
1385 if ((irel->r_offset > addr
1386 && irel->r_offset < toaddr))
1388 if (debug_relax)
1389 printf ("Relocation at address 0x%x needs to be moved.\n"
1390 "Old section offset: 0x%x, New section offset: 0x%x \n",
1391 (unsigned int) old_reloc_address,
1392 (unsigned int) irel->r_offset,
1393 (unsigned int) ((irel->r_offset) - count));
1395 irel->r_offset -= count;
1400 /* The reloc's own addresses are now ok. However, we need to readjust
1401 the reloc's addend, i.e. the reloc's value if two conditions are met:
1402 1.) the reloc is relative to a symbol in this section that
1403 is located in front of the shrinked instruction
1404 2.) symbol plus addend end up behind the shrinked instruction.
1406 The most common case where this happens are relocs relative to
1407 the section-start symbol.
1409 This step needs to be done for all of the sections of the bfd. */
1412 struct bfd_section *isec;
1414 for (isec = abfd->sections; isec; isec = isec->next)
1416 bfd_vma symval;
1417 bfd_vma shrinked_insn_address;
1419 shrinked_insn_address = (sec->output_section->vma
1420 + sec->output_offset + addr - count);
1422 irelend = elf_section_data (isec)->relocs + isec->reloc_count;
1423 for (irel = elf_section_data (isec)->relocs;
1424 irel < irelend;
1425 irel++)
1427 /* Read this BFD's local symbols if we haven't done
1428 so already. */
1429 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1431 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1432 if (isymbuf == NULL)
1433 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1434 symtab_hdr->sh_info, 0,
1435 NULL, NULL, NULL);
1436 if (isymbuf == NULL)
1437 return FALSE;
1440 /* Get the value of the symbol referred to by the reloc. */
1441 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1443 /* A local symbol. */
1444 Elf_Internal_Sym *isym;
1445 asection *sym_sec;
1447 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1448 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1449 symval = isym->st_value;
1450 /* If the reloc is absolute, it will not have
1451 a symbol or section associated with it. */
1452 if (sym_sec == sec)
1454 symval += sym_sec->output_section->vma
1455 + sym_sec->output_offset;
1457 if (debug_relax)
1458 printf ("Checking if the relocation's "
1459 "addend needs corrections.\n"
1460 "Address of anchor symbol: 0x%x \n"
1461 "Address of relocation target: 0x%x \n"
1462 "Address of relaxed insn: 0x%x \n",
1463 (unsigned int) symval,
1464 (unsigned int) (symval + irel->r_addend),
1465 (unsigned int) shrinked_insn_address);
1467 if (symval <= shrinked_insn_address
1468 && (symval + irel->r_addend) > shrinked_insn_address)
1470 irel->r_addend -= count;
1472 if (debug_relax)
1473 printf ("Relocation's addend needed to be fixed \n");
1476 /* else...Reference symbol is absolute. No adjustment needed. */
1478 /* else...Reference symbol is extern. No need for adjusting
1479 the addend. */
1484 /* Adjust the local symbols defined in this section. */
1485 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1486 isymend = isym + symtab_hdr->sh_info;
1487 for (; isym < isymend; isym++)
1489 if (isym->st_shndx == sec_shndx
1490 && isym->st_value > addr
1491 && isym->st_value < toaddr)
1492 isym->st_value -= count;
1495 /* Now adjust the global symbols defined in this section. */
1496 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1497 - symtab_hdr->sh_info);
1498 sym_hashes = elf_sym_hashes (abfd);
1499 end_hashes = sym_hashes + symcount;
1500 for (; sym_hashes < end_hashes; sym_hashes++)
1502 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1503 if ((sym_hash->root.type == bfd_link_hash_defined
1504 || sym_hash->root.type == bfd_link_hash_defweak)
1505 && sym_hash->root.u.def.section == sec
1506 && sym_hash->root.u.def.value > addr
1507 && sym_hash->root.u.def.value < toaddr)
1509 sym_hash->root.u.def.value -= count;
1513 return TRUE;
1516 /* This function handles relaxing for the avr.
1517 Many important relaxing opportunities within functions are already
1518 realized by the compiler itself.
1519 Here we try to replace call (4 bytes) -> rcall (2 bytes)
1520 and jump -> rjmp (safes also 2 bytes).
1521 As well we now optimize seqences of
1522 - call/rcall function
1523 - ret
1524 to yield
1525 - jmp/rjmp function
1526 - ret
1527 . In case that within a sequence
1528 - jmp/rjmp label
1529 - ret
1530 the ret could no longer be reached it is optimized away. In order
1531 to check if the ret is no longer needed, it is checked that the ret's address
1532 is not the target of a branch or jump within the same section, it is checked
1533 that there is no skip instruction before the jmp/rjmp and that there
1534 is no local or global label place at the address of the ret.
1536 We refrain from relaxing within sections ".vectors" and
1537 ".jumptables" in order to maintain the position of the instructions.
1538 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
1539 if possible. (In future one could possibly use the space of the nop
1540 for the first instruction of the irq service function.
1542 The .jumptables sections is meant to be used for a future tablejump variant
1543 for the devices with 3-byte program counter where the table itself
1544 contains 4-byte jump instructions whose relative offset must not
1545 be changed. */
1547 static bfd_boolean
1548 elf32_avr_relax_section (bfd *abfd,
1549 asection *sec,
1550 struct bfd_link_info *link_info,
1551 bfd_boolean *again)
1553 Elf_Internal_Shdr *symtab_hdr;
1554 Elf_Internal_Rela *internal_relocs;
1555 Elf_Internal_Rela *irel, *irelend;
1556 bfd_byte *contents = NULL;
1557 Elf_Internal_Sym *isymbuf = NULL;
1558 static asection *last_input_section = NULL;
1559 static Elf_Internal_Rela *last_reloc = NULL;
1560 struct elf32_avr_link_hash_table *htab;
1562 htab = avr_link_hash_table (link_info);
1564 /* Assume nothing changes. */
1565 *again = FALSE;
1567 if ((!htab->no_stubs) && (sec == htab->stub_sec))
1569 /* We are just relaxing the stub section.
1570 Let's calculate the size needed again. */
1571 bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
1573 if (debug_relax)
1574 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
1575 (int) last_estimated_stub_section_size);
1577 elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
1578 link_info, FALSE);
1580 /* Check if the number of trampolines changed. */
1581 if (last_estimated_stub_section_size != htab->stub_sec->size)
1582 *again = TRUE;
1584 if (debug_relax)
1585 printf ("Size of stub section after this pass: %i\n",
1586 (int) htab->stub_sec->size);
1588 return TRUE;
1591 /* We don't have to do anything for a relocatable link, if
1592 this section does not have relocs, or if this is not a
1593 code section. */
1594 if (link_info->relocatable
1595 || (sec->flags & SEC_RELOC) == 0
1596 || sec->reloc_count == 0
1597 || (sec->flags & SEC_CODE) == 0)
1598 return TRUE;
1600 /* Check if the object file to relax uses internal symbols so that we
1601 could fix up the relocations. */
1602 if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
1603 return TRUE;
1605 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1607 /* Get a copy of the native relocations. */
1608 internal_relocs = (_bfd_elf_link_read_relocs
1609 (abfd, sec, NULL, NULL, link_info->keep_memory));
1610 if (internal_relocs == NULL)
1611 goto error_return;
1613 if (sec != last_input_section)
1614 last_reloc = NULL;
1616 last_input_section = sec;
1618 /* Walk through the relocs looking for relaxing opportunities. */
1619 irelend = internal_relocs + sec->reloc_count;
1620 for (irel = internal_relocs; irel < irelend; irel++)
1622 bfd_vma symval;
1624 if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
1625 && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
1626 && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
1627 continue;
1629 /* Get the section contents if we haven't done so already. */
1630 if (contents == NULL)
1632 /* Get cached copy if it exists. */
1633 if (elf_section_data (sec)->this_hdr.contents != NULL)
1634 contents = elf_section_data (sec)->this_hdr.contents;
1635 else
1637 /* Go get them off disk. */
1638 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
1639 goto error_return;
1643 /* Read this BFD's local symbols if we haven't done so already. */
1644 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1646 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1647 if (isymbuf == NULL)
1648 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1649 symtab_hdr->sh_info, 0,
1650 NULL, NULL, NULL);
1651 if (isymbuf == NULL)
1652 goto error_return;
1656 /* Get the value of the symbol referred to by the reloc. */
1657 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1659 /* A local symbol. */
1660 Elf_Internal_Sym *isym;
1661 asection *sym_sec;
1663 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1664 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1665 symval = isym->st_value;
1666 /* If the reloc is absolute, it will not have
1667 a symbol or section associated with it. */
1668 if (sym_sec)
1669 symval += sym_sec->output_section->vma
1670 + sym_sec->output_offset;
1672 else
1674 unsigned long indx;
1675 struct elf_link_hash_entry *h;
1677 /* An external symbol. */
1678 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1679 h = elf_sym_hashes (abfd)[indx];
1680 BFD_ASSERT (h != NULL);
1681 if (h->root.type != bfd_link_hash_defined
1682 && h->root.type != bfd_link_hash_defweak)
1683 /* This appears to be a reference to an undefined
1684 symbol. Just ignore it--it will be caught by the
1685 regular reloc processing. */
1686 continue;
1688 symval = (h->root.u.def.value
1689 + h->root.u.def.section->output_section->vma
1690 + h->root.u.def.section->output_offset);
1693 /* For simplicity of coding, we are going to modify the section
1694 contents, the section relocs, and the BFD symbol table. We
1695 must tell the rest of the code not to free up this
1696 information. It would be possible to instead create a table
1697 of changes which have to be made, as is done in coff-mips.c;
1698 that would be more work, but would require less memory when
1699 the linker is run. */
1700 switch (ELF32_R_TYPE (irel->r_info))
1702 /* Try to turn a 22-bit absolute call/jump into an 13-bit
1703 pc-relative rcall/rjmp. */
1704 case R_AVR_CALL:
1706 bfd_vma value = symval + irel->r_addend;
1707 bfd_vma dot, gap;
1708 int distance_short_enough = 0;
1710 /* Get the address of this instruction. */
1711 dot = (sec->output_section->vma
1712 + sec->output_offset + irel->r_offset);
1714 /* Compute the distance from this insn to the branch target. */
1715 gap = value - dot;
1717 /* If the distance is within -4094..+4098 inclusive, then we can
1718 relax this jump/call. +4098 because the call/jump target
1719 will be closer after the relaxation. */
1720 if ((int) gap >= -4094 && (int) gap <= 4098)
1721 distance_short_enough = 1;
1723 /* Here we handle the wrap-around case. E.g. for a 16k device
1724 we could use a rjmp to jump from address 0x100 to 0x3d00!
1725 In order to make this work properly, we need to fill the
1726 vaiable avr_pc_wrap_around with the appropriate value.
1727 I.e. 0x4000 for a 16k device. */
1729 /* Shrinking the code size makes the gaps larger in the
1730 case of wrap-arounds. So we use a heuristical safety
1731 margin to avoid that during relax the distance gets
1732 again too large for the short jumps. Let's assume
1733 a typical code-size reduction due to relax for a
1734 16k device of 600 bytes. So let's use twice the
1735 typical value as safety margin. */
1736 int rgap;
1737 int safety_margin;
1739 int assumed_shrink = 600;
1740 if (avr_pc_wrap_around > 0x4000)
1741 assumed_shrink = 900;
1743 safety_margin = 2 * assumed_shrink;
1745 rgap = avr_relative_distance_considering_wrap_around (gap);
1747 if (rgap >= (-4092 + safety_margin)
1748 && rgap <= (4094 - safety_margin))
1749 distance_short_enough = 1;
1752 if (distance_short_enough)
1754 unsigned char code_msb;
1755 unsigned char code_lsb;
1757 if (debug_relax)
1758 printf ("shrinking jump/call instruction at address 0x%x"
1759 " in section %s\n\n",
1760 (int) dot, sec->name);
1762 /* Note that we've changed the relocs, section contents,
1763 etc. */
1764 elf_section_data (sec)->relocs = internal_relocs;
1765 elf_section_data (sec)->this_hdr.contents = contents;
1766 symtab_hdr->contents = (unsigned char *) isymbuf;
1768 /* Get the instruction code for relaxing. */
1769 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
1770 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1772 /* Mask out the relocation bits. */
1773 code_msb &= 0x94;
1774 code_lsb &= 0x0E;
1775 if (code_msb == 0x94 && code_lsb == 0x0E)
1777 /* we are changing call -> rcall . */
1778 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1779 bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
1781 else if (code_msb == 0x94 && code_lsb == 0x0C)
1783 /* we are changeing jump -> rjmp. */
1784 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
1785 bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
1787 else
1788 abort ();
1790 /* Fix the relocation's type. */
1791 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1792 R_AVR_13_PCREL);
1794 /* Check for the vector section. There we don't want to
1795 modify the ordering! */
1797 if (!strcmp (sec->name,".vectors")
1798 || !strcmp (sec->name,".jumptables"))
1800 /* Let's insert a nop. */
1801 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
1802 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
1804 else
1806 /* Delete two bytes of data. */
1807 if (!elf32_avr_relax_delete_bytes (abfd, sec,
1808 irel->r_offset + 2, 2))
1809 goto error_return;
1811 /* That will change things, so, we should relax again.
1812 Note that this is not required, and it may be slow. */
1813 *again = TRUE;
1818 default:
1820 unsigned char code_msb;
1821 unsigned char code_lsb;
1822 bfd_vma dot;
1824 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1825 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
1827 /* Get the address of this instruction. */
1828 dot = (sec->output_section->vma
1829 + sec->output_offset + irel->r_offset);
1831 /* Here we look for rcall/ret or call/ret sequences that could be
1832 safely replaced by rjmp/ret or jmp/ret. */
1833 if (((code_msb & 0xf0) == 0xd0)
1834 && avr_replace_call_ret_sequences)
1836 /* This insn is a rcall. */
1837 unsigned char next_insn_msb = 0;
1838 unsigned char next_insn_lsb = 0;
1840 if (irel->r_offset + 3 < sec->size)
1842 next_insn_msb =
1843 bfd_get_8 (abfd, contents + irel->r_offset + 3);
1844 next_insn_lsb =
1845 bfd_get_8 (abfd, contents + irel->r_offset + 2);
1848 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1850 /* The next insn is a ret. We now convert the rcall insn
1851 into a rjmp instruction. */
1852 code_msb &= 0xef;
1853 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
1854 if (debug_relax)
1855 printf ("converted rcall/ret sequence at address 0x%x"
1856 " into rjmp/ret sequence. Section is %s\n\n",
1857 (int) dot, sec->name);
1858 *again = TRUE;
1859 break;
1862 else if ((0x94 == (code_msb & 0xfe))
1863 && (0x0e == (code_lsb & 0x0e))
1864 && avr_replace_call_ret_sequences)
1866 /* This insn is a call. */
1867 unsigned char next_insn_msb = 0;
1868 unsigned char next_insn_lsb = 0;
1870 if (irel->r_offset + 5 < sec->size)
1872 next_insn_msb =
1873 bfd_get_8 (abfd, contents + irel->r_offset + 5);
1874 next_insn_lsb =
1875 bfd_get_8 (abfd, contents + irel->r_offset + 4);
1878 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1880 /* The next insn is a ret. We now convert the call insn
1881 into a jmp instruction. */
1883 code_lsb &= 0xfd;
1884 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
1885 if (debug_relax)
1886 printf ("converted call/ret sequence at address 0x%x"
1887 " into jmp/ret sequence. Section is %s\n\n",
1888 (int) dot, sec->name);
1889 *again = TRUE;
1890 break;
1893 else if ((0xc0 == (code_msb & 0xf0))
1894 || ((0x94 == (code_msb & 0xfe))
1895 && (0x0c == (code_lsb & 0x0e))))
1897 /* This insn is a rjmp or a jmp. */
1898 unsigned char next_insn_msb = 0;
1899 unsigned char next_insn_lsb = 0;
1900 int insn_size;
1902 if (0xc0 == (code_msb & 0xf0))
1903 insn_size = 2; /* rjmp insn */
1904 else
1905 insn_size = 4; /* jmp insn */
1907 if (irel->r_offset + insn_size + 1 < sec->size)
1909 next_insn_msb =
1910 bfd_get_8 (abfd, contents + irel->r_offset
1911 + insn_size + 1);
1912 next_insn_lsb =
1913 bfd_get_8 (abfd, contents + irel->r_offset
1914 + insn_size);
1917 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
1919 /* The next insn is a ret. We possibly could delete
1920 this ret. First we need to check for preceeding
1921 sbis/sbic/sbrs or cpse "skip" instructions. */
1923 int there_is_preceeding_non_skip_insn = 1;
1924 bfd_vma address_of_ret;
1926 address_of_ret = dot + insn_size;
1928 if (debug_relax && (insn_size == 2))
1929 printf ("found rjmp / ret sequence at address 0x%x\n",
1930 (int) dot);
1931 if (debug_relax && (insn_size == 4))
1932 printf ("found jmp / ret sequence at address 0x%x\n",
1933 (int) dot);
1935 /* We have to make sure that there is a preceeding insn. */
1936 if (irel->r_offset >= 2)
1938 unsigned char preceeding_msb;
1939 unsigned char preceeding_lsb;
1940 preceeding_msb =
1941 bfd_get_8 (abfd, contents + irel->r_offset - 1);
1942 preceeding_lsb =
1943 bfd_get_8 (abfd, contents + irel->r_offset - 2);
1945 /* sbic. */
1946 if (0x99 == preceeding_msb)
1947 there_is_preceeding_non_skip_insn = 0;
1949 /* sbis. */
1950 if (0x9b == preceeding_msb)
1951 there_is_preceeding_non_skip_insn = 0;
1953 /* sbrc */
1954 if ((0xfc == (preceeding_msb & 0xfe)
1955 && (0x00 == (preceeding_lsb & 0x08))))
1956 there_is_preceeding_non_skip_insn = 0;
1958 /* sbrs */
1959 if ((0xfe == (preceeding_msb & 0xfe)
1960 && (0x00 == (preceeding_lsb & 0x08))))
1961 there_is_preceeding_non_skip_insn = 0;
1963 /* cpse */
1964 if (0x10 == (preceeding_msb & 0xfc))
1965 there_is_preceeding_non_skip_insn = 0;
1967 if (there_is_preceeding_non_skip_insn == 0)
1968 if (debug_relax)
1969 printf ("preceeding skip insn prevents deletion of"
1970 " ret insn at addr 0x%x in section %s\n",
1971 (int) dot + 2, sec->name);
1973 else
1975 /* There is no previous instruction. */
1976 there_is_preceeding_non_skip_insn = 0;
1979 if (there_is_preceeding_non_skip_insn)
1981 /* We now only have to make sure that there is no
1982 local label defined at the address of the ret
1983 instruction and that there is no local relocation
1984 in this section pointing to the ret. */
1986 int deleting_ret_is_safe = 1;
1987 unsigned int section_offset_of_ret_insn =
1988 irel->r_offset + insn_size;
1989 Elf_Internal_Sym *isym, *isymend;
1990 unsigned int sec_shndx;
1992 sec_shndx =
1993 _bfd_elf_section_from_bfd_section (abfd, sec);
1995 /* Check for local symbols. */
1996 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1997 isymend = isym + symtab_hdr->sh_info;
1998 for (; isym < isymend; isym++)
2000 if (isym->st_value == section_offset_of_ret_insn
2001 && isym->st_shndx == sec_shndx)
2003 deleting_ret_is_safe = 0;
2004 if (debug_relax)
2005 printf ("local label prevents deletion of ret "
2006 "insn at address 0x%x\n",
2007 (int) dot + insn_size);
2011 /* Now check for global symbols. */
2013 int symcount;
2014 struct elf_link_hash_entry **sym_hashes;
2015 struct elf_link_hash_entry **end_hashes;
2017 symcount = (symtab_hdr->sh_size
2018 / sizeof (Elf32_External_Sym)
2019 - symtab_hdr->sh_info);
2020 sym_hashes = elf_sym_hashes (abfd);
2021 end_hashes = sym_hashes + symcount;
2022 for (; sym_hashes < end_hashes; sym_hashes++)
2024 struct elf_link_hash_entry *sym_hash =
2025 *sym_hashes;
2026 if ((sym_hash->root.type == bfd_link_hash_defined
2027 || sym_hash->root.type ==
2028 bfd_link_hash_defweak)
2029 && sym_hash->root.u.def.section == sec
2030 && sym_hash->root.u.def.value == section_offset_of_ret_insn)
2032 deleting_ret_is_safe = 0;
2033 if (debug_relax)
2034 printf ("global label prevents deletion of "
2035 "ret insn at address 0x%x\n",
2036 (int) dot + insn_size);
2040 /* Now we check for relocations pointing to ret. */
2042 Elf_Internal_Rela *irel;
2043 Elf_Internal_Rela *relend;
2044 Elf_Internal_Shdr *symtab_hdr;
2046 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2047 relend = elf_section_data (sec)->relocs
2048 + sec->reloc_count;
2050 for (irel = elf_section_data (sec)->relocs;
2051 irel < relend; irel++)
2053 bfd_vma reloc_target = 0;
2054 bfd_vma symval;
2055 Elf_Internal_Sym *isymbuf = NULL;
2057 /* Read this BFD's local symbols if we haven't
2058 done so already. */
2059 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2061 isymbuf = (Elf_Internal_Sym *)
2062 symtab_hdr->contents;
2063 if (isymbuf == NULL)
2064 isymbuf = bfd_elf_get_elf_syms
2065 (abfd,
2066 symtab_hdr,
2067 symtab_hdr->sh_info, 0,
2068 NULL, NULL, NULL);
2069 if (isymbuf == NULL)
2070 break;
2073 /* Get the value of the symbol referred to
2074 by the reloc. */
2075 if (ELF32_R_SYM (irel->r_info)
2076 < symtab_hdr->sh_info)
2078 /* A local symbol. */
2079 Elf_Internal_Sym *isym;
2080 asection *sym_sec;
2082 isym = isymbuf
2083 + ELF32_R_SYM (irel->r_info);
2084 sym_sec = bfd_section_from_elf_index
2085 (abfd, isym->st_shndx);
2086 symval = isym->st_value;
2088 /* If the reloc is absolute, it will not
2089 have a symbol or section associated
2090 with it. */
2092 if (sym_sec)
2094 symval +=
2095 sym_sec->output_section->vma
2096 + sym_sec->output_offset;
2097 reloc_target = symval + irel->r_addend;
2099 else
2101 reloc_target = symval + irel->r_addend;
2102 /* Reference symbol is absolute. */
2105 /* else ... reference symbol is extern. */
2107 if (address_of_ret == reloc_target)
2109 deleting_ret_is_safe = 0;
2110 if (debug_relax)
2111 printf ("ret from "
2112 "rjmp/jmp ret sequence at address"
2113 " 0x%x could not be deleted. ret"
2114 " is target of a relocation.\n",
2115 (int) address_of_ret);
2120 if (deleting_ret_is_safe)
2122 if (debug_relax)
2123 printf ("unreachable ret instruction "
2124 "at address 0x%x deleted.\n",
2125 (int) dot + insn_size);
2127 /* Delete two bytes of data. */
2128 if (!elf32_avr_relax_delete_bytes (abfd, sec,
2129 irel->r_offset + insn_size, 2))
2130 goto error_return;
2132 /* That will change things, so, we should relax
2133 again. Note that this is not required, and it
2134 may be slow. */
2135 *again = TRUE;
2136 break;
2142 break;
2147 if (contents != NULL
2148 && elf_section_data (sec)->this_hdr.contents != contents)
2150 if (! link_info->keep_memory)
2151 free (contents);
2152 else
2154 /* Cache the section contents for elf_link_input_bfd. */
2155 elf_section_data (sec)->this_hdr.contents = contents;
2159 if (internal_relocs != NULL
2160 && elf_section_data (sec)->relocs != internal_relocs)
2161 free (internal_relocs);
2163 return TRUE;
2165 error_return:
2166 if (isymbuf != NULL
2167 && symtab_hdr->contents != (unsigned char *) isymbuf)
2168 free (isymbuf);
2169 if (contents != NULL
2170 && elf_section_data (sec)->this_hdr.contents != contents)
2171 free (contents);
2172 if (internal_relocs != NULL
2173 && elf_section_data (sec)->relocs != internal_relocs)
2174 free (internal_relocs);
2176 return FALSE;
2179 /* This is a version of bfd_generic_get_relocated_section_contents
2180 which uses elf32_avr_relocate_section.
2182 For avr it's essentially a cut and paste taken from the H8300 port.
2183 The author of the relaxation support patch for avr had absolutely no
2184 clue what is happening here but found out that this part of the code
2185 seems to be important. */
2187 static bfd_byte *
2188 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
2189 struct bfd_link_info *link_info,
2190 struct bfd_link_order *link_order,
2191 bfd_byte *data,
2192 bfd_boolean relocatable,
2193 asymbol **symbols)
2195 Elf_Internal_Shdr *symtab_hdr;
2196 asection *input_section = link_order->u.indirect.section;
2197 bfd *input_bfd = input_section->owner;
2198 asection **sections = NULL;
2199 Elf_Internal_Rela *internal_relocs = NULL;
2200 Elf_Internal_Sym *isymbuf = NULL;
2202 /* We only need to handle the case of relaxing, or of having a
2203 particular set of section contents, specially. */
2204 if (relocatable
2205 || elf_section_data (input_section)->this_hdr.contents == NULL)
2206 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2207 link_order, data,
2208 relocatable,
2209 symbols);
2210 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2212 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2213 (size_t) input_section->size);
2215 if ((input_section->flags & SEC_RELOC) != 0
2216 && input_section->reloc_count > 0)
2218 asection **secpp;
2219 Elf_Internal_Sym *isym, *isymend;
2220 bfd_size_type amt;
2222 internal_relocs = (_bfd_elf_link_read_relocs
2223 (input_bfd, input_section, NULL, NULL, FALSE));
2224 if (internal_relocs == NULL)
2225 goto error_return;
2227 if (symtab_hdr->sh_info != 0)
2229 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2230 if (isymbuf == NULL)
2231 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2232 symtab_hdr->sh_info, 0,
2233 NULL, NULL, NULL);
2234 if (isymbuf == NULL)
2235 goto error_return;
2238 amt = symtab_hdr->sh_info;
2239 amt *= sizeof (asection *);
2240 sections = bfd_malloc (amt);
2241 if (sections == NULL && amt != 0)
2242 goto error_return;
2244 isymend = isymbuf + symtab_hdr->sh_info;
2245 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
2247 asection *isec;
2249 if (isym->st_shndx == SHN_UNDEF)
2250 isec = bfd_und_section_ptr;
2251 else if (isym->st_shndx == SHN_ABS)
2252 isec = bfd_abs_section_ptr;
2253 else if (isym->st_shndx == SHN_COMMON)
2254 isec = bfd_com_section_ptr;
2255 else
2256 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2258 *secpp = isec;
2261 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
2262 input_section, data, internal_relocs,
2263 isymbuf, sections))
2264 goto error_return;
2266 if (sections != NULL)
2267 free (sections);
2268 if (isymbuf != NULL
2269 && symtab_hdr->contents != (unsigned char *) isymbuf)
2270 free (isymbuf);
2271 if (elf_section_data (input_section)->relocs != internal_relocs)
2272 free (internal_relocs);
2275 return data;
2277 error_return:
2278 if (sections != NULL)
2279 free (sections);
2280 if (isymbuf != NULL
2281 && symtab_hdr->contents != (unsigned char *) isymbuf)
2282 free (isymbuf);
2283 if (internal_relocs != NULL
2284 && elf_section_data (input_section)->relocs != internal_relocs)
2285 free (internal_relocs);
2286 return NULL;
2290 /* Determines the hash entry name for a particular reloc. It consists of
2291 the identifier of the symbol section and the added reloc addend and
2292 symbol offset relative to the section the symbol is attached to. */
2294 static char *
2295 avr_stub_name (const asection *symbol_section,
2296 const bfd_vma symbol_offset,
2297 const Elf_Internal_Rela *rela)
2299 char *stub_name;
2300 bfd_size_type len;
2302 len = 8 + 1 + 8 + 1 + 1;
2303 stub_name = bfd_malloc (len);
2305 sprintf (stub_name, "%08x+%08x",
2306 symbol_section->id & 0xffffffff,
2307 (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
2309 return stub_name;
2313 /* Add a new stub entry to the stub hash. Not all fields of the new
2314 stub entry are initialised. */
2316 static struct elf32_avr_stub_hash_entry *
2317 avr_add_stub (const char *stub_name,
2318 struct elf32_avr_link_hash_table *htab)
2320 struct elf32_avr_stub_hash_entry *hsh;
2322 /* Enter this entry into the linker stub hash table. */
2323 hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
2325 if (hsh == NULL)
2327 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
2328 NULL, stub_name);
2329 return NULL;
2332 hsh->stub_offset = 0;
2333 return hsh;
2336 /* We assume that there is already space allocated for the stub section
2337 contents and that before building the stubs the section size is
2338 initialized to 0. We assume that within the stub hash table entry,
2339 the absolute position of the jmp target has been written in the
2340 target_value field. We write here the offset of the generated jmp insn
2341 relative to the trampoline section start to the stub_offset entry in
2342 the stub hash table entry. */
2344 static bfd_boolean
2345 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2347 struct elf32_avr_stub_hash_entry *hsh;
2348 struct bfd_link_info *info;
2349 struct elf32_avr_link_hash_table *htab;
2350 bfd *stub_bfd;
2351 bfd_byte *loc;
2352 bfd_vma target;
2353 bfd_vma starget;
2355 /* Basic opcode */
2356 bfd_vma jmp_insn = 0x0000940c;
2358 /* Massage our args to the form they really have. */
2359 hsh = avr_stub_hash_entry (bh);
2361 if (!hsh->is_actually_needed)
2362 return TRUE;
2364 info = (struct bfd_link_info *) in_arg;
2366 htab = avr_link_hash_table (info);
2368 target = hsh->target_value;
2370 /* Make a note of the offset within the stubs for this entry. */
2371 hsh->stub_offset = htab->stub_sec->size;
2372 loc = htab->stub_sec->contents + hsh->stub_offset;
2374 stub_bfd = htab->stub_sec->owner;
2376 if (debug_stubs)
2377 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
2378 (unsigned int) target,
2379 (unsigned int) hsh->stub_offset);
2381 /* We now have to add the information on the jump target to the bare
2382 opcode bits already set in jmp_insn. */
2384 /* Check for the alignment of the address. */
2385 if (target & 1)
2386 return FALSE;
2388 starget = target >> 1;
2389 jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
2390 bfd_put_16 (stub_bfd, jmp_insn, loc);
2391 bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
2393 htab->stub_sec->size += 4;
2395 /* Now add the entries in the address mapping table if there is still
2396 space left. */
2398 unsigned int nr;
2400 nr = htab->amt_entry_cnt + 1;
2401 if (nr <= htab->amt_max_entry_cnt)
2403 htab->amt_entry_cnt = nr;
2405 htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
2406 htab->amt_destination_addr[nr - 1] = target;
2410 return TRUE;
2413 static bfd_boolean
2414 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
2415 void *in_arg)
2417 struct elf32_avr_stub_hash_entry *hsh;
2418 struct elf32_avr_link_hash_table *htab;
2420 htab = in_arg;
2421 hsh = avr_stub_hash_entry (bh);
2422 hsh->is_actually_needed = FALSE;
2424 return TRUE;
2427 static bfd_boolean
2428 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
2430 struct elf32_avr_stub_hash_entry *hsh;
2431 struct elf32_avr_link_hash_table *htab;
2432 int size;
2434 /* Massage our args to the form they really have. */
2435 hsh = avr_stub_hash_entry (bh);
2436 htab = in_arg;
2438 if (hsh->is_actually_needed)
2439 size = 4;
2440 else
2441 size = 0;
2443 htab->stub_sec->size += size;
2444 return TRUE;
2447 void
2448 elf32_avr_setup_params (struct bfd_link_info *info,
2449 bfd *avr_stub_bfd,
2450 asection *avr_stub_section,
2451 bfd_boolean no_stubs,
2452 bfd_boolean deb_stubs,
2453 bfd_boolean deb_relax,
2454 bfd_vma pc_wrap_around,
2455 bfd_boolean call_ret_replacement)
2457 struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
2459 htab->stub_sec = avr_stub_section;
2460 htab->stub_bfd = avr_stub_bfd;
2461 htab->no_stubs = no_stubs;
2463 debug_relax = deb_relax;
2464 debug_stubs = deb_stubs;
2465 avr_pc_wrap_around = pc_wrap_around;
2466 avr_replace_call_ret_sequences = call_ret_replacement;
2470 /* Set up various things so that we can make a list of input sections
2471 for each output section included in the link. Returns -1 on error,
2472 0 when no stubs will be needed, and 1 on success. It also sets
2473 information on the stubs bfd and the stub section in the info
2474 struct. */
2477 elf32_avr_setup_section_lists (bfd *output_bfd,
2478 struct bfd_link_info *info)
2480 bfd *input_bfd;
2481 unsigned int bfd_count;
2482 int top_id, top_index;
2483 asection *section;
2484 asection **input_list, **list;
2485 bfd_size_type amt;
2486 struct elf32_avr_link_hash_table *htab = avr_link_hash_table(info);
2488 if (htab->no_stubs)
2489 return 0;
2491 /* Count the number of input BFDs and find the top input section id. */
2492 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
2493 input_bfd != NULL;
2494 input_bfd = input_bfd->link_next)
2496 bfd_count += 1;
2497 for (section = input_bfd->sections;
2498 section != NULL;
2499 section = section->next)
2500 if (top_id < section->id)
2501 top_id = section->id;
2504 htab->bfd_count = bfd_count;
2506 /* We can't use output_bfd->section_count here to find the top output
2507 section index as some sections may have been removed, and
2508 strip_excluded_output_sections doesn't renumber the indices. */
2509 for (section = output_bfd->sections, top_index = 0;
2510 section != NULL;
2511 section = section->next)
2512 if (top_index < section->index)
2513 top_index = section->index;
2515 htab->top_index = top_index;
2516 amt = sizeof (asection *) * (top_index + 1);
2517 input_list = bfd_malloc (amt);
2518 htab->input_list = input_list;
2519 if (input_list == NULL)
2520 return -1;
2522 /* For sections we aren't interested in, mark their entries with a
2523 value we can check later. */
2524 list = input_list + top_index;
2526 *list = bfd_abs_section_ptr;
2527 while (list-- != input_list);
2529 for (section = output_bfd->sections;
2530 section != NULL;
2531 section = section->next)
2532 if ((section->flags & SEC_CODE) != 0)
2533 input_list[section->index] = NULL;
2535 return 1;
2539 /* Read in all local syms for all input bfds, and create hash entries
2540 for export stubs if we are building a multi-subspace shared lib.
2541 Returns -1 on error, 0 otherwise. */
2543 static int
2544 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
2546 unsigned int bfd_indx;
2547 Elf_Internal_Sym *local_syms, **all_local_syms;
2548 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
2550 /* We want to read in symbol extension records only once. To do this
2551 we need to read in the local symbols in parallel and save them for
2552 later use; so hold pointers to the local symbols in an array. */
2553 bfd_size_type amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
2554 all_local_syms = bfd_zmalloc (amt);
2555 htab->all_local_syms = all_local_syms;
2556 if (all_local_syms == NULL)
2557 return -1;
2559 /* Walk over all the input BFDs, swapping in local symbols.
2560 If we are creating a shared library, create hash entries for the
2561 export stubs. */
2562 for (bfd_indx = 0;
2563 input_bfd != NULL;
2564 input_bfd = input_bfd->link_next, bfd_indx++)
2566 Elf_Internal_Shdr *symtab_hdr;
2568 /* We'll need the symbol table in a second. */
2569 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2570 if (symtab_hdr->sh_info == 0)
2571 continue;
2573 /* We need an array of the local symbols attached to the input bfd. */
2574 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
2575 if (local_syms == NULL)
2577 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2578 symtab_hdr->sh_info, 0,
2579 NULL, NULL, NULL);
2580 /* Cache them for elf_link_input_bfd. */
2581 symtab_hdr->contents = (unsigned char *) local_syms;
2583 if (local_syms == NULL)
2584 return -1;
2586 all_local_syms[bfd_indx] = local_syms;
2589 return 0;
2592 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
2594 bfd_boolean
2595 elf32_avr_size_stubs (bfd *output_bfd,
2596 struct bfd_link_info *info,
2597 bfd_boolean is_prealloc_run)
2599 struct elf32_avr_link_hash_table *htab;
2600 int stub_changed = 0;
2602 htab = avr_link_hash_table (info);
2604 /* At this point we initialize htab->vector_base
2605 To the start of the text output section. */
2606 htab->vector_base = htab->stub_sec->output_section->vma;
2608 if (get_local_syms (info->input_bfds, info))
2610 if (htab->all_local_syms)
2611 goto error_ret_free_local;
2612 return FALSE;
2615 if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
2617 struct elf32_avr_stub_hash_entry *test;
2619 test = avr_add_stub ("Hugo",htab);
2620 test->target_value = 0x123456;
2621 test->stub_offset = 13;
2623 test = avr_add_stub ("Hugo2",htab);
2624 test->target_value = 0x84210;
2625 test->stub_offset = 14;
2628 while (1)
2630 bfd *input_bfd;
2631 unsigned int bfd_indx;
2633 /* We will have to re-generate the stub hash table each time anything
2634 in memory has changed. */
2636 bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
2637 for (input_bfd = info->input_bfds, bfd_indx = 0;
2638 input_bfd != NULL;
2639 input_bfd = input_bfd->link_next, bfd_indx++)
2641 Elf_Internal_Shdr *symtab_hdr;
2642 asection *section;
2643 Elf_Internal_Sym *local_syms;
2645 /* We'll need the symbol table in a second. */
2646 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2647 if (symtab_hdr->sh_info == 0)
2648 continue;
2650 local_syms = htab->all_local_syms[bfd_indx];
2652 /* Walk over each section attached to the input bfd. */
2653 for (section = input_bfd->sections;
2654 section != NULL;
2655 section = section->next)
2657 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
2659 /* If there aren't any relocs, then there's nothing more
2660 to do. */
2661 if ((section->flags & SEC_RELOC) == 0
2662 || section->reloc_count == 0)
2663 continue;
2665 /* If this section is a link-once section that will be
2666 discarded, then don't create any stubs. */
2667 if (section->output_section == NULL
2668 || section->output_section->owner != output_bfd)
2669 continue;
2671 /* Get the relocs. */
2672 internal_relocs
2673 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
2674 info->keep_memory);
2675 if (internal_relocs == NULL)
2676 goto error_ret_free_local;
2678 /* Now examine each relocation. */
2679 irela = internal_relocs;
2680 irelaend = irela + section->reloc_count;
2681 for (; irela < irelaend; irela++)
2683 unsigned int r_type, r_indx;
2684 struct elf32_avr_stub_hash_entry *hsh;
2685 asection *sym_sec;
2686 bfd_vma sym_value;
2687 bfd_vma destination;
2688 struct elf_link_hash_entry *hh;
2689 char *stub_name;
2691 r_type = ELF32_R_TYPE (irela->r_info);
2692 r_indx = ELF32_R_SYM (irela->r_info);
2694 /* Only look for 16 bit GS relocs. No other reloc will need a
2695 stub. */
2696 if (!((r_type == R_AVR_16_PM)
2697 || (r_type == R_AVR_LO8_LDI_GS)
2698 || (r_type == R_AVR_HI8_LDI_GS)))
2699 continue;
2701 /* Now determine the call target, its name, value,
2702 section. */
2703 sym_sec = NULL;
2704 sym_value = 0;
2705 destination = 0;
2706 hh = NULL;
2707 if (r_indx < symtab_hdr->sh_info)
2709 /* It's a local symbol. */
2710 Elf_Internal_Sym *sym;
2711 Elf_Internal_Shdr *hdr;
2713 sym = local_syms + r_indx;
2714 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
2715 sym_sec = hdr->bfd_section;
2716 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
2717 sym_value = sym->st_value;
2718 destination = (sym_value + irela->r_addend
2719 + sym_sec->output_offset
2720 + sym_sec->output_section->vma);
2722 else
2724 /* It's an external symbol. */
2725 int e_indx;
2727 e_indx = r_indx - symtab_hdr->sh_info;
2728 hh = elf_sym_hashes (input_bfd)[e_indx];
2730 while (hh->root.type == bfd_link_hash_indirect
2731 || hh->root.type == bfd_link_hash_warning)
2732 hh = (struct elf_link_hash_entry *)
2733 (hh->root.u.i.link);
2735 if (hh->root.type == bfd_link_hash_defined
2736 || hh->root.type == bfd_link_hash_defweak)
2738 sym_sec = hh->root.u.def.section;
2739 sym_value = hh->root.u.def.value;
2740 if (sym_sec->output_section != NULL)
2741 destination = (sym_value + irela->r_addend
2742 + sym_sec->output_offset
2743 + sym_sec->output_section->vma);
2745 else if (hh->root.type == bfd_link_hash_undefweak)
2747 if (! info->shared)
2748 continue;
2750 else if (hh->root.type == bfd_link_hash_undefined)
2752 if (! (info->unresolved_syms_in_objects == RM_IGNORE
2753 && (ELF_ST_VISIBILITY (hh->other)
2754 == STV_DEFAULT)))
2755 continue;
2757 else
2759 bfd_set_error (bfd_error_bad_value);
2761 error_ret_free_internal:
2762 if (elf_section_data (section)->relocs == NULL)
2763 free (internal_relocs);
2764 goto error_ret_free_local;
2768 if (! avr_stub_is_required_for_16_bit_reloc
2769 (destination - htab->vector_base))
2771 if (!is_prealloc_run)
2772 /* We are having a reloc that does't need a stub. */
2773 continue;
2775 /* We don't right now know if a stub will be needed.
2776 Let's rather be on the safe side. */
2779 /* Get the name of this stub. */
2780 stub_name = avr_stub_name (sym_sec, sym_value, irela);
2782 if (!stub_name)
2783 goto error_ret_free_internal;
2786 hsh = avr_stub_hash_lookup (&htab->bstab,
2787 stub_name,
2788 FALSE, FALSE);
2789 if (hsh != NULL)
2791 /* The proper stub has already been created. Mark it
2792 to be used and write the possibly changed destination
2793 value. */
2794 hsh->is_actually_needed = TRUE;
2795 hsh->target_value = destination;
2796 free (stub_name);
2797 continue;
2800 hsh = avr_add_stub (stub_name, htab);
2801 if (hsh == NULL)
2803 free (stub_name);
2804 goto error_ret_free_internal;
2807 hsh->is_actually_needed = TRUE;
2808 hsh->target_value = destination;
2810 if (debug_stubs)
2811 printf ("Adding stub with destination 0x%x to the"
2812 " hash table.\n", (unsigned int) destination);
2813 if (debug_stubs)
2814 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
2816 stub_changed = TRUE;
2819 /* We're done with the internal relocs, free them. */
2820 if (elf_section_data (section)->relocs == NULL)
2821 free (internal_relocs);
2825 /* Re-Calculate the number of needed stubs. */
2826 htab->stub_sec->size = 0;
2827 bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
2829 if (!stub_changed)
2830 break;
2832 stub_changed = FALSE;
2835 free (htab->all_local_syms);
2836 return TRUE;
2838 error_ret_free_local:
2839 free (htab->all_local_syms);
2840 return FALSE;
2844 /* Build all the stubs associated with the current output file. The
2845 stubs are kept in a hash table attached to the main linker hash
2846 table. We also set up the .plt entries for statically linked PIC
2847 functions here. This function is called via hppaelf_finish in the
2848 linker. */
2850 bfd_boolean
2851 elf32_avr_build_stubs (struct bfd_link_info *info)
2853 asection *stub_sec;
2854 struct bfd_hash_table *table;
2855 struct elf32_avr_link_hash_table *htab;
2856 bfd_size_type total_size = 0;
2858 htab = avr_link_hash_table (info);
2860 /* In case that there were several stub sections: */
2861 for (stub_sec = htab->stub_bfd->sections;
2862 stub_sec != NULL;
2863 stub_sec = stub_sec->next)
2865 bfd_size_type size;
2867 /* Allocate memory to hold the linker stubs. */
2868 size = stub_sec->size;
2869 total_size += size;
2871 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
2872 if (stub_sec->contents == NULL && size != 0)
2873 return FALSE;
2874 stub_sec->size = 0;
2877 /* Allocate memory for the adress mapping table. */
2878 htab->amt_entry_cnt = 0;
2879 htab->amt_max_entry_cnt = total_size / 4;
2880 htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
2881 * htab->amt_max_entry_cnt);
2882 htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
2883 * htab->amt_max_entry_cnt );
2885 if (debug_stubs)
2886 printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
2888 /* Build the stubs as directed by the stub hash table. */
2889 table = &htab->bstab;
2890 bfd_hash_traverse (table, avr_build_one_stub, info);
2892 if (debug_stubs)
2893 printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
2895 return TRUE;
2898 #define ELF_ARCH bfd_arch_avr
2899 #define ELF_MACHINE_CODE EM_AVR
2900 #define ELF_MACHINE_ALT1 EM_AVR_OLD
2901 #define ELF_MAXPAGESIZE 1
2903 #define TARGET_LITTLE_SYM bfd_elf32_avr_vec
2904 #define TARGET_LITTLE_NAME "elf32-avr"
2906 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
2907 #define bfd_elf32_bfd_link_hash_table_free elf32_avr_link_hash_table_free
2909 #define elf_info_to_howto avr_info_to_howto_rela
2910 #define elf_info_to_howto_rel NULL
2911 #define elf_backend_relocate_section elf32_avr_relocate_section
2912 #define elf_backend_check_relocs elf32_avr_check_relocs
2913 #define elf_backend_can_gc_sections 1
2914 #define elf_backend_rela_normal 1
2915 #define elf_backend_final_write_processing \
2916 bfd_elf_avr_final_write_processing
2917 #define elf_backend_object_p elf32_avr_object_p
2919 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
2920 #define bfd_elf32_bfd_get_relocated_section_contents \
2921 elf32_avr_get_relocated_section_contents
2923 #include "elf32-target.h"