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