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