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