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