PR23652, Use symbols from debug bfd for _bfd_elf_find_function
[deliverable/binutils-gdb.git] / bfd / elf32-avr.c
1 /* AVR-specific support for 32-bit ELF
2 Copyright (C) 1999-2019 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 unsigned 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 3, /* size (0 = byte, 1 = short, 2 = long) */
125 0, /* bitsize */
126 FALSE, /* pc_relative */
127 0, /* bitpos */
128 complain_overflow_dont, /* complain_on_overflow */
129 bfd_elf_generic_reloc, /* special_function */
130 "R_AVR_NONE", /* name */
131 FALSE, /* partial_inplace */
132 0, /* src_mask */
133 0, /* dst_mask */
134 FALSE), /* pcrel_offset */
135
136 HOWTO (R_AVR_32, /* type */
137 0, /* rightshift */
138 2, /* size (0 = byte, 1 = short, 2 = long) */
139 32, /* bitsize */
140 FALSE, /* pc_relative */
141 0, /* bitpos */
142 complain_overflow_bitfield, /* complain_on_overflow */
143 bfd_elf_generic_reloc, /* special_function */
144 "R_AVR_32", /* name */
145 FALSE, /* partial_inplace */
146 0xffffffff, /* src_mask */
147 0xffffffff, /* dst_mask */
148 FALSE), /* pcrel_offset */
149
150 /* A 7 bit PC relative relocation. */
151 HOWTO (R_AVR_7_PCREL, /* type */
152 1, /* rightshift */
153 1, /* size (0 = byte, 1 = short, 2 = long) */
154 7, /* bitsize */
155 TRUE, /* pc_relative */
156 3, /* bitpos */
157 complain_overflow_bitfield, /* complain_on_overflow */
158 bfd_elf_generic_reloc, /* special_function */
159 "R_AVR_7_PCREL", /* name */
160 FALSE, /* partial_inplace */
161 0xffff, /* src_mask */
162 0xffff, /* dst_mask */
163 TRUE), /* pcrel_offset */
164
165 /* A 13 bit PC relative relocation. */
166 HOWTO (R_AVR_13_PCREL, /* type */
167 1, /* rightshift */
168 1, /* size (0 = byte, 1 = short, 2 = long) */
169 13, /* bitsize */
170 TRUE, /* pc_relative */
171 0, /* bitpos */
172 complain_overflow_bitfield, /* complain_on_overflow */
173 bfd_elf_generic_reloc, /* special_function */
174 "R_AVR_13_PCREL", /* name */
175 FALSE, /* partial_inplace */
176 0xfff, /* src_mask */
177 0xfff, /* dst_mask */
178 TRUE), /* pcrel_offset */
179
180 /* A 16 bit absolute relocation. */
181 HOWTO (R_AVR_16, /* type */
182 0, /* rightshift */
183 1, /* size (0 = byte, 1 = short, 2 = long) */
184 16, /* bitsize */
185 FALSE, /* pc_relative */
186 0, /* bitpos */
187 complain_overflow_dont, /* complain_on_overflow */
188 bfd_elf_generic_reloc, /* special_function */
189 "R_AVR_16", /* name */
190 FALSE, /* partial_inplace */
191 0xffff, /* src_mask */
192 0xffff, /* dst_mask */
193 FALSE), /* pcrel_offset */
194
195 /* A 16 bit absolute relocation for command address
196 Will be changed when linker stubs are needed. */
197 HOWTO (R_AVR_16_PM, /* type */
198 1, /* rightshift */
199 1, /* size (0 = byte, 1 = short, 2 = long) */
200 16, /* bitsize */
201 FALSE, /* pc_relative */
202 0, /* bitpos */
203 complain_overflow_bitfield, /* complain_on_overflow */
204 bfd_elf_generic_reloc, /* special_function */
205 "R_AVR_16_PM", /* name */
206 FALSE, /* partial_inplace */
207 0xffff, /* src_mask */
208 0xffff, /* dst_mask */
209 FALSE), /* pcrel_offset */
210 /* A low 8 bit absolute relocation of 16 bit address.
211 For LDI command. */
212 HOWTO (R_AVR_LO8_LDI, /* type */
213 0, /* rightshift */
214 1, /* size (0 = byte, 1 = short, 2 = long) */
215 8, /* bitsize */
216 FALSE, /* pc_relative */
217 0, /* bitpos */
218 complain_overflow_dont, /* complain_on_overflow */
219 bfd_elf_generic_reloc, /* special_function */
220 "R_AVR_LO8_LDI", /* name */
221 FALSE, /* partial_inplace */
222 0xffff, /* src_mask */
223 0xffff, /* dst_mask */
224 FALSE), /* pcrel_offset */
225 /* A high 8 bit absolute relocation of 16 bit address.
226 For LDI command. */
227 HOWTO (R_AVR_HI8_LDI, /* type */
228 8, /* rightshift */
229 1, /* size (0 = byte, 1 = short, 2 = long) */
230 8, /* bitsize */
231 FALSE, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_dont, /* complain_on_overflow */
234 bfd_elf_generic_reloc, /* special_function */
235 "R_AVR_HI8_LDI", /* name */
236 FALSE, /* partial_inplace */
237 0xffff, /* src_mask */
238 0xffff, /* dst_mask */
239 FALSE), /* pcrel_offset */
240 /* A high 6 bit absolute relocation of 22 bit address.
241 For LDI command. As well second most significant 8 bit value of
242 a 32 bit link-time constant. */
243 HOWTO (R_AVR_HH8_LDI, /* type */
244 16, /* rightshift */
245 1, /* size (0 = byte, 1 = short, 2 = long) */
246 8, /* bitsize */
247 FALSE, /* pc_relative */
248 0, /* bitpos */
249 complain_overflow_dont, /* complain_on_overflow */
250 bfd_elf_generic_reloc, /* special_function */
251 "R_AVR_HH8_LDI", /* name */
252 FALSE, /* partial_inplace */
253 0xffff, /* src_mask */
254 0xffff, /* dst_mask */
255 FALSE), /* pcrel_offset */
256 /* A negative low 8 bit absolute relocation of 16 bit address.
257 For LDI command. */
258 HOWTO (R_AVR_LO8_LDI_NEG, /* type */
259 0, /* rightshift */
260 1, /* size (0 = byte, 1 = short, 2 = long) */
261 8, /* bitsize */
262 FALSE, /* pc_relative */
263 0, /* bitpos */
264 complain_overflow_dont, /* complain_on_overflow */
265 bfd_elf_generic_reloc, /* special_function */
266 "R_AVR_LO8_LDI_NEG", /* name */
267 FALSE, /* partial_inplace */
268 0xffff, /* src_mask */
269 0xffff, /* dst_mask */
270 FALSE), /* pcrel_offset */
271 /* A negative high 8 bit absolute relocation of 16 bit address.
272 For LDI command. */
273 HOWTO (R_AVR_HI8_LDI_NEG, /* type */
274 8, /* rightshift */
275 1, /* size (0 = byte, 1 = short, 2 = long) */
276 8, /* bitsize */
277 FALSE, /* pc_relative */
278 0, /* bitpos */
279 complain_overflow_dont, /* complain_on_overflow */
280 bfd_elf_generic_reloc, /* special_function */
281 "R_AVR_HI8_LDI_NEG", /* name */
282 FALSE, /* partial_inplace */
283 0xffff, /* src_mask */
284 0xffff, /* dst_mask */
285 FALSE), /* pcrel_offset */
286 /* A negative high 6 bit absolute relocation of 22 bit address.
287 For LDI command. */
288 HOWTO (R_AVR_HH8_LDI_NEG, /* type */
289 16, /* rightshift */
290 1, /* size (0 = byte, 1 = short, 2 = long) */
291 8, /* bitsize */
292 FALSE, /* pc_relative */
293 0, /* bitpos */
294 complain_overflow_dont, /* complain_on_overflow */
295 bfd_elf_generic_reloc, /* special_function */
296 "R_AVR_HH8_LDI_NEG", /* name */
297 FALSE, /* partial_inplace */
298 0xffff, /* src_mask */
299 0xffff, /* dst_mask */
300 FALSE), /* pcrel_offset */
301 /* A low 8 bit absolute relocation of 24 bit program memory address.
302 For LDI command. Will not be changed when linker stubs are needed. */
303 HOWTO (R_AVR_LO8_LDI_PM, /* type */
304 1, /* rightshift */
305 1, /* size (0 = byte, 1 = short, 2 = long) */
306 8, /* bitsize */
307 FALSE, /* pc_relative */
308 0, /* bitpos */
309 complain_overflow_dont, /* complain_on_overflow */
310 bfd_elf_generic_reloc, /* special_function */
311 "R_AVR_LO8_LDI_PM", /* name */
312 FALSE, /* partial_inplace */
313 0xffff, /* src_mask */
314 0xffff, /* dst_mask */
315 FALSE), /* pcrel_offset */
316 /* A low 8 bit absolute relocation of 24 bit program memory address.
317 For LDI command. Will not be changed when linker stubs are needed. */
318 HOWTO (R_AVR_HI8_LDI_PM, /* type */
319 9, /* rightshift */
320 1, /* size (0 = byte, 1 = short, 2 = long) */
321 8, /* bitsize */
322 FALSE, /* pc_relative */
323 0, /* bitpos */
324 complain_overflow_dont, /* complain_on_overflow */
325 bfd_elf_generic_reloc, /* special_function */
326 "R_AVR_HI8_LDI_PM", /* name */
327 FALSE, /* partial_inplace */
328 0xffff, /* src_mask */
329 0xffff, /* dst_mask */
330 FALSE), /* pcrel_offset */
331 /* A low 8 bit absolute relocation of 24 bit program memory address.
332 For LDI command. Will not be changed when linker stubs are needed. */
333 HOWTO (R_AVR_HH8_LDI_PM, /* type */
334 17, /* rightshift */
335 1, /* size (0 = byte, 1 = short, 2 = long) */
336 8, /* bitsize */
337 FALSE, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_dont, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* special_function */
341 "R_AVR_HH8_LDI_PM", /* name */
342 FALSE, /* partial_inplace */
343 0xffff, /* src_mask */
344 0xffff, /* dst_mask */
345 FALSE), /* pcrel_offset */
346 /* A low 8 bit absolute relocation of 24 bit program memory address.
347 For LDI command. Will not be changed when linker stubs are needed. */
348 HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */
349 1, /* rightshift */
350 1, /* size (0 = byte, 1 = short, 2 = long) */
351 8, /* bitsize */
352 FALSE, /* pc_relative */
353 0, /* bitpos */
354 complain_overflow_dont, /* complain_on_overflow */
355 bfd_elf_generic_reloc, /* special_function */
356 "R_AVR_LO8_LDI_PM_NEG", /* name */
357 FALSE, /* partial_inplace */
358 0xffff, /* src_mask */
359 0xffff, /* dst_mask */
360 FALSE), /* pcrel_offset */
361 /* A low 8 bit absolute relocation of 24 bit program memory address.
362 For LDI command. Will not be changed when linker stubs are needed. */
363 HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */
364 9, /* rightshift */
365 1, /* size (0 = byte, 1 = short, 2 = long) */
366 8, /* bitsize */
367 FALSE, /* pc_relative */
368 0, /* bitpos */
369 complain_overflow_dont, /* complain_on_overflow */
370 bfd_elf_generic_reloc, /* special_function */
371 "R_AVR_HI8_LDI_PM_NEG", /* name */
372 FALSE, /* partial_inplace */
373 0xffff, /* src_mask */
374 0xffff, /* dst_mask */
375 FALSE), /* pcrel_offset */
376 /* A low 8 bit absolute relocation of 24 bit program memory address.
377 For LDI command. Will not be changed when linker stubs are needed. */
378 HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */
379 17, /* rightshift */
380 1, /* size (0 = byte, 1 = short, 2 = long) */
381 8, /* bitsize */
382 FALSE, /* pc_relative */
383 0, /* bitpos */
384 complain_overflow_dont, /* complain_on_overflow */
385 bfd_elf_generic_reloc, /* special_function */
386 "R_AVR_HH8_LDI_PM_NEG", /* name */
387 FALSE, /* partial_inplace */
388 0xffff, /* src_mask */
389 0xffff, /* dst_mask */
390 FALSE), /* pcrel_offset */
391 /* Relocation for CALL command in ATmega. */
392 HOWTO (R_AVR_CALL, /* type */
393 1, /* rightshift */
394 2, /* size (0 = byte, 1 = short, 2 = long) */
395 23, /* bitsize */
396 FALSE, /* pc_relative */
397 0, /* bitpos */
398 complain_overflow_dont,/* complain_on_overflow */
399 bfd_elf_generic_reloc, /* special_function */
400 "R_AVR_CALL", /* name */
401 FALSE, /* partial_inplace */
402 0xffffffff, /* src_mask */
403 0xffffffff, /* dst_mask */
404 FALSE), /* pcrel_offset */
405 /* A 16 bit absolute relocation of 16 bit address.
406 For LDI command. */
407 HOWTO (R_AVR_LDI, /* type */
408 0, /* rightshift */
409 1, /* size (0 = byte, 1 = short, 2 = long) */
410 16, /* bitsize */
411 FALSE, /* pc_relative */
412 0, /* bitpos */
413 complain_overflow_dont,/* complain_on_overflow */
414 bfd_elf_generic_reloc, /* special_function */
415 "R_AVR_LDI", /* name */
416 FALSE, /* partial_inplace */
417 0xffff, /* src_mask */
418 0xffff, /* dst_mask */
419 FALSE), /* pcrel_offset */
420 /* A 6 bit absolute relocation of 6 bit offset.
421 For ldd/sdd command. */
422 HOWTO (R_AVR_6, /* type */
423 0, /* rightshift */
424 0, /* size (0 = byte, 1 = short, 2 = long) */
425 6, /* bitsize */
426 FALSE, /* pc_relative */
427 0, /* bitpos */
428 complain_overflow_dont,/* complain_on_overflow */
429 bfd_elf_generic_reloc, /* special_function */
430 "R_AVR_6", /* name */
431 FALSE, /* partial_inplace */
432 0xffff, /* src_mask */
433 0xffff, /* dst_mask */
434 FALSE), /* pcrel_offset */
435 /* A 6 bit absolute relocation of 6 bit offset.
436 For sbiw/adiw command. */
437 HOWTO (R_AVR_6_ADIW, /* type */
438 0, /* rightshift */
439 0, /* size (0 = byte, 1 = short, 2 = long) */
440 6, /* bitsize */
441 FALSE, /* pc_relative */
442 0, /* bitpos */
443 complain_overflow_dont,/* complain_on_overflow */
444 bfd_elf_generic_reloc, /* special_function */
445 "R_AVR_6_ADIW", /* name */
446 FALSE, /* partial_inplace */
447 0xffff, /* src_mask */
448 0xffff, /* dst_mask */
449 FALSE), /* pcrel_offset */
450 /* Most significant 8 bit value of a 32 bit link-time constant. */
451 HOWTO (R_AVR_MS8_LDI, /* type */
452 24, /* rightshift */
453 1, /* size (0 = byte, 1 = short, 2 = long) */
454 8, /* bitsize */
455 FALSE, /* pc_relative */
456 0, /* bitpos */
457 complain_overflow_dont, /* complain_on_overflow */
458 bfd_elf_generic_reloc, /* special_function */
459 "R_AVR_MS8_LDI", /* name */
460 FALSE, /* partial_inplace */
461 0xffff, /* src_mask */
462 0xffff, /* dst_mask */
463 FALSE), /* pcrel_offset */
464 /* Negative most significant 8 bit value of a 32 bit link-time constant. */
465 HOWTO (R_AVR_MS8_LDI_NEG, /* type */
466 24, /* rightshift */
467 1, /* size (0 = byte, 1 = short, 2 = long) */
468 8, /* bitsize */
469 FALSE, /* pc_relative */
470 0, /* bitpos */
471 complain_overflow_dont, /* complain_on_overflow */
472 bfd_elf_generic_reloc, /* special_function */
473 "R_AVR_MS8_LDI_NEG", /* name */
474 FALSE, /* partial_inplace */
475 0xffff, /* src_mask */
476 0xffff, /* dst_mask */
477 FALSE), /* pcrel_offset */
478 /* A low 8 bit absolute relocation of 24 bit program memory address.
479 For LDI command. Will be changed when linker stubs are needed. */
480 HOWTO (R_AVR_LO8_LDI_GS, /* type */
481 1, /* rightshift */
482 1, /* size (0 = byte, 1 = short, 2 = long) */
483 8, /* bitsize */
484 FALSE, /* pc_relative */
485 0, /* bitpos */
486 complain_overflow_dont, /* complain_on_overflow */
487 bfd_elf_generic_reloc, /* special_function */
488 "R_AVR_LO8_LDI_GS", /* name */
489 FALSE, /* partial_inplace */
490 0xffff, /* src_mask */
491 0xffff, /* dst_mask */
492 FALSE), /* pcrel_offset */
493 /* A low 8 bit absolute relocation of 24 bit program memory address.
494 For LDI command. Will be changed when linker stubs are needed. */
495 HOWTO (R_AVR_HI8_LDI_GS, /* type */
496 9, /* rightshift */
497 1, /* size (0 = byte, 1 = short, 2 = long) */
498 8, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_dont, /* complain_on_overflow */
502 bfd_elf_generic_reloc, /* special_function */
503 "R_AVR_HI8_LDI_GS", /* name */
504 FALSE, /* partial_inplace */
505 0xffff, /* src_mask */
506 0xffff, /* dst_mask */
507 FALSE), /* pcrel_offset */
508 /* 8 bit offset. */
509 HOWTO (R_AVR_8, /* type */
510 0, /* rightshift */
511 0, /* size (0 = byte, 1 = short, 2 = long) */
512 8, /* bitsize */
513 FALSE, /* pc_relative */
514 0, /* bitpos */
515 complain_overflow_bitfield,/* complain_on_overflow */
516 bfd_elf_generic_reloc, /* special_function */
517 "R_AVR_8", /* name */
518 FALSE, /* partial_inplace */
519 0x000000ff, /* src_mask */
520 0x000000ff, /* dst_mask */
521 FALSE), /* pcrel_offset */
522 /* lo8-part to use in .byte lo8(sym). */
523 HOWTO (R_AVR_8_LO8, /* type */
524 0, /* rightshift */
525 0, /* size (0 = byte, 1 = short, 2 = long) */
526 8, /* bitsize */
527 FALSE, /* pc_relative */
528 0, /* bitpos */
529 complain_overflow_dont,/* complain_on_overflow */
530 bfd_elf_generic_reloc, /* special_function */
531 "R_AVR_8_LO8", /* name */
532 FALSE, /* partial_inplace */
533 0xffffff, /* src_mask */
534 0xffffff, /* dst_mask */
535 FALSE), /* pcrel_offset */
536 /* hi8-part to use in .byte hi8(sym). */
537 HOWTO (R_AVR_8_HI8, /* type */
538 8, /* rightshift */
539 0, /* size (0 = byte, 1 = short, 2 = long) */
540 8, /* bitsize */
541 FALSE, /* pc_relative */
542 0, /* bitpos */
543 complain_overflow_dont,/* complain_on_overflow */
544 bfd_elf_generic_reloc, /* special_function */
545 "R_AVR_8_HI8", /* name */
546 FALSE, /* partial_inplace */
547 0xffffff, /* src_mask */
548 0xffffff, /* dst_mask */
549 FALSE), /* pcrel_offset */
550 /* hlo8-part to use in .byte hlo8(sym). */
551 HOWTO (R_AVR_8_HLO8, /* type */
552 16, /* rightshift */
553 0, /* size (0 = byte, 1 = short, 2 = long) */
554 8, /* bitsize */
555 FALSE, /* pc_relative */
556 0, /* bitpos */
557 complain_overflow_dont,/* complain_on_overflow */
558 bfd_elf_generic_reloc, /* special_function */
559 "R_AVR_8_HLO8", /* name */
560 FALSE, /* partial_inplace */
561 0xffffff, /* src_mask */
562 0xffffff, /* dst_mask */
563 FALSE), /* pcrel_offset */
564 HOWTO (R_AVR_DIFF8, /* type */
565 0, /* rightshift */
566 0, /* size (0 = byte, 1 = short, 2 = long) */
567 8, /* bitsize */
568 FALSE, /* pc_relative */
569 0, /* bitpos */
570 complain_overflow_bitfield, /* complain_on_overflow */
571 bfd_elf_avr_diff_reloc, /* special_function */
572 "R_AVR_DIFF8", /* name */
573 FALSE, /* partial_inplace */
574 0, /* src_mask */
575 0xff, /* dst_mask */
576 FALSE), /* pcrel_offset */
577 HOWTO (R_AVR_DIFF16, /* type */
578 0, /* rightshift */
579 1, /* size (0 = byte, 1 = short, 2 = long) */
580 16, /* bitsize */
581 FALSE, /* pc_relative */
582 0, /* bitpos */
583 complain_overflow_bitfield, /* complain_on_overflow */
584 bfd_elf_avr_diff_reloc,/* special_function */
585 "R_AVR_DIFF16", /* name */
586 FALSE, /* partial_inplace */
587 0, /* src_mask */
588 0xffff, /* dst_mask */
589 FALSE), /* pcrel_offset */
590 HOWTO (R_AVR_DIFF32, /* type */
591 0, /* rightshift */
592 2, /* size (0 = byte, 1 = short, 2 = long) */
593 32, /* bitsize */
594 FALSE, /* pc_relative */
595 0, /* bitpos */
596 complain_overflow_bitfield, /* complain_on_overflow */
597 bfd_elf_avr_diff_reloc,/* special_function */
598 "R_AVR_DIFF32", /* name */
599 FALSE, /* partial_inplace */
600 0, /* src_mask */
601 0xffffffff, /* dst_mask */
602 FALSE), /* pcrel_offset */
603 /* 7 bit immediate for LDS/STS in Tiny core. */
604 HOWTO (R_AVR_LDS_STS_16, /* type */
605 0, /* rightshift */
606 1, /* size (0 = byte, 1 = short, 2 = long) */
607 7, /* bitsize */
608 FALSE, /* pc_relative */
609 0, /* bitpos */
610 complain_overflow_dont,/* complain_on_overflow */
611 bfd_elf_generic_reloc, /* special_function */
612 "R_AVR_LDS_STS_16", /* name */
613 FALSE, /* partial_inplace */
614 0xffff, /* src_mask */
615 0xffff, /* dst_mask */
616 FALSE), /* pcrel_offset */
617
618 HOWTO (R_AVR_PORT6, /* type */
619 0, /* rightshift */
620 0, /* size (0 = byte, 1 = short, 2 = long) */
621 6, /* bitsize */
622 FALSE, /* pc_relative */
623 0, /* bitpos */
624 complain_overflow_dont,/* complain_on_overflow */
625 bfd_elf_generic_reloc, /* special_function */
626 "R_AVR_PORT6", /* name */
627 FALSE, /* partial_inplace */
628 0xffffff, /* src_mask */
629 0xffffff, /* dst_mask */
630 FALSE), /* pcrel_offset */
631 HOWTO (R_AVR_PORT5, /* type */
632 0, /* rightshift */
633 0, /* size (0 = byte, 1 = short, 2 = long) */
634 5, /* bitsize */
635 FALSE, /* pc_relative */
636 0, /* bitpos */
637 complain_overflow_dont,/* complain_on_overflow */
638 bfd_elf_generic_reloc, /* special_function */
639 "R_AVR_PORT5", /* name */
640 FALSE, /* partial_inplace */
641 0xffffff, /* src_mask */
642 0xffffff, /* dst_mask */
643 FALSE), /* pcrel_offset */
644
645 /* A 32 bit PC relative relocation. */
646 HOWTO (R_AVR_32_PCREL, /* type */
647 0, /* rightshift */
648 2, /* size (0 = byte, 1 = short, 2 = long) */
649 32, /* bitsize */
650 TRUE, /* pc_relative */
651 0, /* bitpos */
652 complain_overflow_bitfield, /* complain_on_overflow */
653 bfd_elf_generic_reloc, /* special_function */
654 "R_AVR_32_PCREL", /* name */
655 FALSE, /* partial_inplace */
656 0xffffffff, /* src_mask */
657 0xffffffff, /* dst_mask */
658 TRUE), /* pcrel_offset */
659 };
660
661 /* Map BFD reloc types to AVR ELF reloc types. */
662
663 struct avr_reloc_map
664 {
665 bfd_reloc_code_real_type bfd_reloc_val;
666 unsigned int elf_reloc_val;
667 };
668
669 static const struct avr_reloc_map avr_reloc_map[] =
670 {
671 { BFD_RELOC_NONE, R_AVR_NONE },
672 { BFD_RELOC_32, R_AVR_32 },
673 { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL },
674 { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL },
675 { BFD_RELOC_16, R_AVR_16 },
676 { BFD_RELOC_AVR_16_PM, R_AVR_16_PM },
677 { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI},
678 { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI },
679 { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI },
680 { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI },
681 { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG },
682 { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG },
683 { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG },
684 { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG },
685 { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM },
686 { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS },
687 { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM },
688 { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS },
689 { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM },
690 { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG },
691 { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG },
692 { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG },
693 { BFD_RELOC_AVR_CALL, R_AVR_CALL },
694 { BFD_RELOC_AVR_LDI, R_AVR_LDI },
695 { BFD_RELOC_AVR_6, R_AVR_6 },
696 { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW },
697 { BFD_RELOC_8, R_AVR_8 },
698 { BFD_RELOC_AVR_8_LO, R_AVR_8_LO8 },
699 { BFD_RELOC_AVR_8_HI, R_AVR_8_HI8 },
700 { BFD_RELOC_AVR_8_HLO, R_AVR_8_HLO8 },
701 { BFD_RELOC_AVR_DIFF8, R_AVR_DIFF8 },
702 { BFD_RELOC_AVR_DIFF16, R_AVR_DIFF16 },
703 { BFD_RELOC_AVR_DIFF32, R_AVR_DIFF32 },
704 { BFD_RELOC_AVR_LDS_STS_16, R_AVR_LDS_STS_16},
705 { BFD_RELOC_AVR_PORT6, R_AVR_PORT6},
706 { BFD_RELOC_AVR_PORT5, R_AVR_PORT5},
707 { BFD_RELOC_32_PCREL, R_AVR_32_PCREL}
708 };
709
710 static const struct bfd_elf_special_section elf_avr_special_sections[] =
711 {
712 { STRING_COMMA_LEN (".noinit"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE },
713 { NULL, 0, 0, 0, 0 }
714 };
715
716 /* Meant to be filled one day with the wrap around address for the
717 specific device. I.e. should get the value 0x4000 for 16k devices,
718 0x8000 for 32k devices and so on.
719
720 We initialize it here with a value of 0x1000000 resulting in
721 that we will never suggest a wrap-around jump during relaxation.
722 The logic of the source code later on assumes that in
723 avr_pc_wrap_around one single bit is set. */
724 static bfd_vma avr_pc_wrap_around = 0x10000000;
725
726 /* If this variable holds a value different from zero, the linker relaxation
727 machine will try to optimize call/ret sequences by a single jump
728 instruction. This option could be switched off by a linker switch. */
729 static int avr_replace_call_ret_sequences = 1;
730 \f
731
732 /* Per-section relaxation related information for avr. */
733
734 struct avr_relax_info
735 {
736 /* Track the avr property records that apply to this section. */
737
738 struct
739 {
740 /* Number of records in the list. */
741 unsigned count;
742
743 /* How many records worth of space have we allocated. */
744 unsigned allocated;
745
746 /* The records, only COUNT records are initialised. */
747 struct avr_property_record *items;
748 } records;
749 };
750
751 /* Per section data, specialised for avr. */
752
753 struct elf_avr_section_data
754 {
755 /* The standard data must appear first. */
756 struct bfd_elf_section_data elf;
757
758 /* Relaxation related information. */
759 struct avr_relax_info relax_info;
760 };
761
762 /* Possibly initialise avr specific data for new section SEC from ABFD. */
763
764 static bfd_boolean
765 elf_avr_new_section_hook (bfd *abfd, asection *sec)
766 {
767 if (!sec->used_by_bfd)
768 {
769 struct elf_avr_section_data *sdata;
770 bfd_size_type amt = sizeof (*sdata);
771
772 sdata = bfd_zalloc (abfd, amt);
773 if (sdata == NULL)
774 return FALSE;
775 sec->used_by_bfd = sdata;
776 }
777
778 return _bfd_elf_new_section_hook (abfd, sec);
779 }
780
781 /* Return a pointer to the relaxation information for SEC. */
782
783 static struct avr_relax_info *
784 get_avr_relax_info (asection *sec)
785 {
786 struct elf_avr_section_data *section_data;
787
788 /* No info available if no section or if it is an output section. */
789 if (!sec || sec == sec->output_section)
790 return NULL;
791
792 section_data = (struct elf_avr_section_data *) elf_section_data (sec);
793 return &section_data->relax_info;
794 }
795
796 /* Initialise the per section relaxation information for SEC. */
797
798 static void
799 init_avr_relax_info (asection *sec)
800 {
801 struct avr_relax_info *relax_info = get_avr_relax_info (sec);
802
803 relax_info->records.count = 0;
804 relax_info->records.allocated = 0;
805 relax_info->records.items = NULL;
806 }
807
808 /* Initialize an entry in the stub hash table. */
809
810 static struct bfd_hash_entry *
811 stub_hash_newfunc (struct bfd_hash_entry *entry,
812 struct bfd_hash_table *table,
813 const char *string)
814 {
815 /* Allocate the structure if it has not already been allocated by a
816 subclass. */
817 if (entry == NULL)
818 {
819 entry = bfd_hash_allocate (table,
820 sizeof (struct elf32_avr_stub_hash_entry));
821 if (entry == NULL)
822 return entry;
823 }
824
825 /* Call the allocation method of the superclass. */
826 entry = bfd_hash_newfunc (entry, table, string);
827 if (entry != NULL)
828 {
829 struct elf32_avr_stub_hash_entry *hsh;
830
831 /* Initialize the local fields. */
832 hsh = avr_stub_hash_entry (entry);
833 hsh->stub_offset = 0;
834 hsh->target_value = 0;
835 }
836
837 return entry;
838 }
839
840 /* This function is just a straight passthrough to the real
841 function in linker.c. Its prupose is so that its address
842 can be compared inside the avr_link_hash_table macro. */
843
844 static struct bfd_hash_entry *
845 elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry,
846 struct bfd_hash_table * table,
847 const char * string)
848 {
849 return _bfd_elf_link_hash_newfunc (entry, table, string);
850 }
851
852 /* Free the derived linker hash table. */
853
854 static void
855 elf32_avr_link_hash_table_free (bfd *obfd)
856 {
857 struct elf32_avr_link_hash_table *htab
858 = (struct elf32_avr_link_hash_table *) obfd->link.hash;
859
860 /* Free the address mapping table. */
861 if (htab->amt_stub_offsets != NULL)
862 free (htab->amt_stub_offsets);
863 if (htab->amt_destination_addr != NULL)
864 free (htab->amt_destination_addr);
865
866 bfd_hash_table_free (&htab->bstab);
867 _bfd_elf_link_hash_table_free (obfd);
868 }
869
870 /* Create the derived linker hash table. The AVR ELF port uses the derived
871 hash table to keep information specific to the AVR ELF linker (without
872 using static variables). */
873
874 static struct bfd_link_hash_table *
875 elf32_avr_link_hash_table_create (bfd *abfd)
876 {
877 struct elf32_avr_link_hash_table *htab;
878 bfd_size_type amt = sizeof (*htab);
879
880 htab = bfd_zmalloc (amt);
881 if (htab == NULL)
882 return NULL;
883
884 if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
885 elf32_avr_link_hash_newfunc,
886 sizeof (struct elf_link_hash_entry),
887 AVR_ELF_DATA))
888 {
889 free (htab);
890 return NULL;
891 }
892
893 /* Init the stub hash table too. */
894 if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
895 sizeof (struct elf32_avr_stub_hash_entry)))
896 {
897 _bfd_elf_link_hash_table_free (abfd);
898 return NULL;
899 }
900 htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free;
901
902 return &htab->etab.root;
903 }
904
905 /* Calculates the effective distance of a pc relative jump/call. */
906
907 static int
908 avr_relative_distance_considering_wrap_around (unsigned int distance)
909 {
910 unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
911 int dist_with_wrap_around = distance & wrap_around_mask;
912
913 if (dist_with_wrap_around >= ((int) (avr_pc_wrap_around >> 1)))
914 dist_with_wrap_around -= avr_pc_wrap_around;
915
916 return dist_with_wrap_around;
917 }
918
919
920 static reloc_howto_type *
921 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
922 bfd_reloc_code_real_type code)
923 {
924 unsigned int i;
925
926 for (i = 0;
927 i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
928 i++)
929 if (avr_reloc_map[i].bfd_reloc_val == code)
930 return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
931
932 return NULL;
933 }
934
935 static reloc_howto_type *
936 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
937 const char *r_name)
938 {
939 unsigned int i;
940
941 for (i = 0;
942 i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
943 i++)
944 if (elf_avr_howto_table[i].name != NULL
945 && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
946 return &elf_avr_howto_table[i];
947
948 return NULL;
949 }
950
951 /* Set the howto pointer for an AVR ELF reloc. */
952
953 static bfd_boolean
954 avr_info_to_howto_rela (bfd *abfd,
955 arelent *cache_ptr,
956 Elf_Internal_Rela *dst)
957 {
958 unsigned int r_type;
959
960 r_type = ELF32_R_TYPE (dst->r_info);
961 if (r_type >= (unsigned int) R_AVR_max)
962 {
963 /* xgettext:c-format */
964 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
965 abfd, r_type);
966 bfd_set_error (bfd_error_bad_value);
967 return FALSE;
968 }
969 cache_ptr->howto = &elf_avr_howto_table[r_type];
970 return TRUE;
971 }
972
973 static bfd_boolean
974 avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
975 {
976 return (relocation >= 0x020000);
977 }
978
979 /* Returns the address of the corresponding stub if there is one.
980 Returns otherwise an address above 0x020000. This function
981 could also be used, if there is no knowledge on the section where
982 the destination is found. */
983
984 static bfd_vma
985 avr_get_stub_addr (bfd_vma srel,
986 struct elf32_avr_link_hash_table *htab)
987 {
988 unsigned int sindex;
989 bfd_vma stub_sec_addr =
990 (htab->stub_sec->output_section->vma +
991 htab->stub_sec->output_offset);
992
993 for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++)
994 if (htab->amt_destination_addr[sindex] == srel)
995 return htab->amt_stub_offsets[sindex] + stub_sec_addr;
996
997 /* Return an address that could not be reached by 16 bit relocs. */
998 return 0x020000;
999 }
1000
1001 /* Perform a diff relocation. Nothing to do, as the difference value is already
1002 written into the section's contents. */
1003
1004 static bfd_reloc_status_type
1005 bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
1006 arelent *reloc_entry ATTRIBUTE_UNUSED,
1007 asymbol *symbol ATTRIBUTE_UNUSED,
1008 void *data ATTRIBUTE_UNUSED,
1009 asection *input_section ATTRIBUTE_UNUSED,
1010 bfd *output_bfd ATTRIBUTE_UNUSED,
1011 char **error_message ATTRIBUTE_UNUSED)
1012 {
1013 return bfd_reloc_ok;
1014 }
1015
1016
1017 /* Perform a single relocation. By default we use the standard BFD
1018 routines, but a few relocs, we have to do them ourselves. */
1019
1020 static bfd_reloc_status_type
1021 avr_final_link_relocate (reloc_howto_type * howto,
1022 bfd * input_bfd,
1023 asection * input_section,
1024 bfd_byte * contents,
1025 Elf_Internal_Rela * rel,
1026 bfd_vma relocation,
1027 struct elf32_avr_link_hash_table * htab)
1028 {
1029 bfd_reloc_status_type r = bfd_reloc_ok;
1030 bfd_vma x;
1031 bfd_signed_vma srel;
1032 bfd_signed_vma reloc_addr;
1033 bfd_boolean use_stubs = FALSE;
1034 /* Usually is 0, unless we are generating code for a bootloader. */
1035 bfd_signed_vma base_addr = htab->vector_base;
1036
1037 /* Absolute addr of the reloc in the final excecutable. */
1038 reloc_addr = rel->r_offset + input_section->output_section->vma
1039 + input_section->output_offset;
1040
1041 switch (howto->type)
1042 {
1043 case R_AVR_7_PCREL:
1044 contents += rel->r_offset;
1045 srel = (bfd_signed_vma) relocation;
1046 srel += rel->r_addend;
1047 srel -= rel->r_offset;
1048 srel -= 2; /* Branch instructions add 2 to the PC... */
1049 srel -= (input_section->output_section->vma +
1050 input_section->output_offset);
1051
1052 if (srel & 1)
1053 return bfd_reloc_outofrange;
1054 if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
1055 return bfd_reloc_overflow;
1056 x = bfd_get_16 (input_bfd, contents);
1057 x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
1058 bfd_put_16 (input_bfd, x, contents);
1059 break;
1060
1061 case R_AVR_13_PCREL:
1062 contents += rel->r_offset;
1063 srel = (bfd_signed_vma) relocation;
1064 srel += rel->r_addend;
1065 srel -= rel->r_offset;
1066 srel -= 2; /* Branch instructions add 2 to the PC... */
1067 srel -= (input_section->output_section->vma +
1068 input_section->output_offset);
1069
1070 if (srel & 1)
1071 return bfd_reloc_outofrange;
1072
1073 srel = avr_relative_distance_considering_wrap_around (srel);
1074
1075 /* AVR addresses commands as words. */
1076 srel >>= 1;
1077
1078 /* Check for overflow. */
1079 if (srel < -2048 || srel > 2047)
1080 {
1081 /* Relative distance is too large. */
1082
1083 /* Always apply WRAPAROUND for avr2, avr25, and avr4. */
1084 switch (bfd_get_mach (input_bfd))
1085 {
1086 case bfd_mach_avr2:
1087 case bfd_mach_avr25:
1088 case bfd_mach_avr4:
1089 break;
1090
1091 default:
1092 return bfd_reloc_overflow;
1093 }
1094 }
1095
1096 x = bfd_get_16 (input_bfd, contents);
1097 x = (x & 0xf000) | (srel & 0xfff);
1098 bfd_put_16 (input_bfd, x, contents);
1099 break;
1100
1101 case R_AVR_LO8_LDI:
1102 contents += rel->r_offset;
1103 srel = (bfd_signed_vma) relocation + rel->r_addend;
1104 x = bfd_get_16 (input_bfd, contents);
1105 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1106 bfd_put_16 (input_bfd, x, contents);
1107 break;
1108
1109 case R_AVR_LDI:
1110 contents += rel->r_offset;
1111 srel = (bfd_signed_vma) relocation + rel->r_addend;
1112 if (((srel > 0) && (srel & 0xffff) > 255)
1113 || ((srel < 0) && ((-srel) & 0xffff) > 128))
1114 /* Remove offset for data/eeprom section. */
1115 return bfd_reloc_overflow;
1116
1117 x = bfd_get_16 (input_bfd, contents);
1118 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1119 bfd_put_16 (input_bfd, x, contents);
1120 break;
1121
1122 case R_AVR_6:
1123 contents += rel->r_offset;
1124 srel = (bfd_signed_vma) relocation + rel->r_addend;
1125 if (((srel & 0xffff) > 63) || (srel < 0))
1126 /* Remove offset for data/eeprom section. */
1127 return bfd_reloc_overflow;
1128 x = bfd_get_16 (input_bfd, contents);
1129 x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
1130 | ((srel & (1 << 5)) << 8));
1131 bfd_put_16 (input_bfd, x, contents);
1132 break;
1133
1134 case R_AVR_6_ADIW:
1135 contents += rel->r_offset;
1136 srel = (bfd_signed_vma) relocation + rel->r_addend;
1137 if (((srel & 0xffff) > 63) || (srel < 0))
1138 /* Remove offset for data/eeprom section. */
1139 return bfd_reloc_overflow;
1140 x = bfd_get_16 (input_bfd, contents);
1141 x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
1142 bfd_put_16 (input_bfd, x, contents);
1143 break;
1144
1145 case R_AVR_HI8_LDI:
1146 contents += rel->r_offset;
1147 srel = (bfd_signed_vma) relocation + rel->r_addend;
1148 srel = (srel >> 8) & 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_HH8_LDI:
1155 contents += rel->r_offset;
1156 srel = (bfd_signed_vma) relocation + rel->r_addend;
1157 srel = (srel >> 16) & 0xff;
1158 x = bfd_get_16 (input_bfd, contents);
1159 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1160 bfd_put_16 (input_bfd, x, contents);
1161 break;
1162
1163 case R_AVR_MS8_LDI:
1164 contents += rel->r_offset;
1165 srel = (bfd_signed_vma) relocation + rel->r_addend;
1166 srel = (srel >> 24) & 0xff;
1167 x = bfd_get_16 (input_bfd, contents);
1168 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1169 bfd_put_16 (input_bfd, x, contents);
1170 break;
1171
1172 case R_AVR_LO8_LDI_NEG:
1173 contents += rel->r_offset;
1174 srel = (bfd_signed_vma) relocation + rel->r_addend;
1175 srel = -srel;
1176 x = bfd_get_16 (input_bfd, contents);
1177 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1178 bfd_put_16 (input_bfd, x, contents);
1179 break;
1180
1181 case R_AVR_HI8_LDI_NEG:
1182 contents += rel->r_offset;
1183 srel = (bfd_signed_vma) relocation + rel->r_addend;
1184 srel = -srel;
1185 srel = (srel >> 8) & 0xff;
1186 x = bfd_get_16 (input_bfd, contents);
1187 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1188 bfd_put_16 (input_bfd, x, contents);
1189 break;
1190
1191 case R_AVR_HH8_LDI_NEG:
1192 contents += rel->r_offset;
1193 srel = (bfd_signed_vma) relocation + rel->r_addend;
1194 srel = -srel;
1195 srel = (srel >> 16) & 0xff;
1196 x = bfd_get_16 (input_bfd, contents);
1197 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1198 bfd_put_16 (input_bfd, x, contents);
1199 break;
1200
1201 case R_AVR_MS8_LDI_NEG:
1202 contents += rel->r_offset;
1203 srel = (bfd_signed_vma) relocation + rel->r_addend;
1204 srel = -srel;
1205 srel = (srel >> 24) & 0xff;
1206 x = bfd_get_16 (input_bfd, contents);
1207 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1208 bfd_put_16 (input_bfd, x, contents);
1209 break;
1210
1211 case R_AVR_LO8_LDI_GS:
1212 use_stubs = (!htab->no_stubs);
1213 /* Fall through. */
1214 case R_AVR_LO8_LDI_PM:
1215 contents += rel->r_offset;
1216 srel = (bfd_signed_vma) relocation + rel->r_addend;
1217
1218 if (use_stubs
1219 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1220 {
1221 bfd_vma old_srel = srel;
1222
1223 /* We need to use the address of the stub instead. */
1224 srel = avr_get_stub_addr (srel, htab);
1225 if (debug_stubs)
1226 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1227 "reloc at address 0x%x.\n",
1228 (unsigned int) srel,
1229 (unsigned int) old_srel,
1230 (unsigned int) reloc_addr);
1231
1232 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1233 return bfd_reloc_outofrange;
1234 }
1235
1236 if (srel & 1)
1237 return bfd_reloc_outofrange;
1238 srel = srel >> 1;
1239 x = bfd_get_16 (input_bfd, contents);
1240 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1241 bfd_put_16 (input_bfd, x, contents);
1242 break;
1243
1244 case R_AVR_HI8_LDI_GS:
1245 use_stubs = (!htab->no_stubs);
1246 /* Fall through. */
1247 case R_AVR_HI8_LDI_PM:
1248 contents += rel->r_offset;
1249 srel = (bfd_signed_vma) relocation + rel->r_addend;
1250
1251 if (use_stubs
1252 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1253 {
1254 bfd_vma old_srel = srel;
1255
1256 /* We need to use the address of the stub instead. */
1257 srel = avr_get_stub_addr (srel, htab);
1258 if (debug_stubs)
1259 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1260 "reloc at address 0x%x.\n",
1261 (unsigned int) srel,
1262 (unsigned int) old_srel,
1263 (unsigned int) reloc_addr);
1264
1265 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1266 return bfd_reloc_outofrange;
1267 }
1268
1269 if (srel & 1)
1270 return bfd_reloc_outofrange;
1271 srel = srel >> 1;
1272 srel = (srel >> 8) & 0xff;
1273 x = bfd_get_16 (input_bfd, contents);
1274 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1275 bfd_put_16 (input_bfd, x, contents);
1276 break;
1277
1278 case R_AVR_HH8_LDI_PM:
1279 contents += rel->r_offset;
1280 srel = (bfd_signed_vma) relocation + rel->r_addend;
1281 if (srel & 1)
1282 return bfd_reloc_outofrange;
1283 srel = srel >> 1;
1284 srel = (srel >> 16) & 0xff;
1285 x = bfd_get_16 (input_bfd, contents);
1286 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1287 bfd_put_16 (input_bfd, x, contents);
1288 break;
1289
1290 case R_AVR_LO8_LDI_PM_NEG:
1291 contents += rel->r_offset;
1292 srel = (bfd_signed_vma) relocation + rel->r_addend;
1293 srel = -srel;
1294 if (srel & 1)
1295 return bfd_reloc_outofrange;
1296 srel = srel >> 1;
1297 x = bfd_get_16 (input_bfd, contents);
1298 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1299 bfd_put_16 (input_bfd, x, contents);
1300 break;
1301
1302 case R_AVR_HI8_LDI_PM_NEG:
1303 contents += rel->r_offset;
1304 srel = (bfd_signed_vma) relocation + rel->r_addend;
1305 srel = -srel;
1306 if (srel & 1)
1307 return bfd_reloc_outofrange;
1308 srel = srel >> 1;
1309 srel = (srel >> 8) & 0xff;
1310 x = bfd_get_16 (input_bfd, contents);
1311 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1312 bfd_put_16 (input_bfd, x, contents);
1313 break;
1314
1315 case R_AVR_HH8_LDI_PM_NEG:
1316 contents += rel->r_offset;
1317 srel = (bfd_signed_vma) relocation + rel->r_addend;
1318 srel = -srel;
1319 if (srel & 1)
1320 return bfd_reloc_outofrange;
1321 srel = srel >> 1;
1322 srel = (srel >> 16) & 0xff;
1323 x = bfd_get_16 (input_bfd, contents);
1324 x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
1325 bfd_put_16 (input_bfd, x, contents);
1326 break;
1327
1328 case R_AVR_CALL:
1329 contents += rel->r_offset;
1330 srel = (bfd_signed_vma) relocation + rel->r_addend;
1331 if (srel & 1)
1332 return bfd_reloc_outofrange;
1333 srel = srel >> 1;
1334 x = bfd_get_16 (input_bfd, contents);
1335 x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
1336 bfd_put_16 (input_bfd, x, contents);
1337 bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
1338 break;
1339
1340 case R_AVR_16_PM:
1341 use_stubs = (!htab->no_stubs);
1342 contents += rel->r_offset;
1343 srel = (bfd_signed_vma) relocation + rel->r_addend;
1344
1345 if (use_stubs
1346 && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1347 {
1348 bfd_vma old_srel = srel;
1349
1350 /* We need to use the address of the stub instead. */
1351 srel = avr_get_stub_addr (srel,htab);
1352 if (debug_stubs)
1353 printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
1354 "reloc at address 0x%x.\n",
1355 (unsigned int) srel,
1356 (unsigned int) old_srel,
1357 (unsigned int) reloc_addr);
1358
1359 if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
1360 return bfd_reloc_outofrange;
1361 }
1362
1363 if (srel & 1)
1364 return bfd_reloc_outofrange;
1365 srel = srel >> 1;
1366 bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
1367 break;
1368
1369 case R_AVR_DIFF8:
1370 case R_AVR_DIFF16:
1371 case R_AVR_DIFF32:
1372 /* Nothing to do here, as contents already contains the diff value. */
1373 r = bfd_reloc_ok;
1374 break;
1375
1376 case R_AVR_LDS_STS_16:
1377 contents += rel->r_offset;
1378 srel = (bfd_signed_vma) relocation + rel->r_addend;
1379 if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf)
1380 return bfd_reloc_outofrange;
1381 srel = srel & 0x7f;
1382 x = bfd_get_16 (input_bfd, contents);
1383 x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2);
1384 bfd_put_16 (input_bfd, x, contents);
1385 break;
1386
1387 case R_AVR_PORT6:
1388 contents += rel->r_offset;
1389 srel = (bfd_signed_vma) relocation + rel->r_addend;
1390 if ((srel & 0xffff) > 0x3f)
1391 return bfd_reloc_outofrange;
1392 x = bfd_get_16 (input_bfd, contents);
1393 x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f);
1394 bfd_put_16 (input_bfd, x, contents);
1395 break;
1396
1397 case R_AVR_PORT5:
1398 contents += rel->r_offset;
1399 srel = (bfd_signed_vma) relocation + rel->r_addend;
1400 if ((srel & 0xffff) > 0x1f)
1401 return bfd_reloc_outofrange;
1402 x = bfd_get_16 (input_bfd, contents);
1403 x = (x & 0xff07) | ((srel & 0x1f) << 3);
1404 bfd_put_16 (input_bfd, x, contents);
1405 break;
1406
1407 default:
1408 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1409 contents, rel->r_offset,
1410 relocation, rel->r_addend);
1411 }
1412
1413 return r;
1414 }
1415
1416 /* Relocate an AVR ELF section. */
1417
1418 static bfd_boolean
1419 elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
1420 struct bfd_link_info *info,
1421 bfd *input_bfd,
1422 asection *input_section,
1423 bfd_byte *contents,
1424 Elf_Internal_Rela *relocs,
1425 Elf_Internal_Sym *local_syms,
1426 asection **local_sections)
1427 {
1428 Elf_Internal_Shdr * symtab_hdr;
1429 struct elf_link_hash_entry ** sym_hashes;
1430 Elf_Internal_Rela * rel;
1431 Elf_Internal_Rela * relend;
1432 struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
1433
1434 if (htab == NULL)
1435 return FALSE;
1436
1437 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1438 sym_hashes = elf_sym_hashes (input_bfd);
1439 relend = relocs + input_section->reloc_count;
1440
1441 for (rel = relocs; rel < relend; rel ++)
1442 {
1443 reloc_howto_type * howto;
1444 unsigned long r_symndx;
1445 Elf_Internal_Sym * sym;
1446 asection * sec;
1447 struct elf_link_hash_entry * h;
1448 bfd_vma relocation;
1449 bfd_reloc_status_type r;
1450 const char * name;
1451 int r_type;
1452
1453 r_type = ELF32_R_TYPE (rel->r_info);
1454 r_symndx = ELF32_R_SYM (rel->r_info);
1455 howto = elf_avr_howto_table + r_type;
1456 h = NULL;
1457 sym = NULL;
1458 sec = NULL;
1459
1460 if (r_symndx < symtab_hdr->sh_info)
1461 {
1462 sym = local_syms + r_symndx;
1463 sec = local_sections [r_symndx];
1464 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1465
1466 name = bfd_elf_string_from_elf_section
1467 (input_bfd, symtab_hdr->sh_link, sym->st_name);
1468 name = name == NULL ? bfd_section_name (sec) : name;
1469 }
1470 else
1471 {
1472 bfd_boolean unresolved_reloc, warned, ignored;
1473
1474 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1475 r_symndx, symtab_hdr, sym_hashes,
1476 h, sec, relocation,
1477 unresolved_reloc, warned, ignored);
1478
1479 name = h->root.root.string;
1480 }
1481
1482 if (sec != NULL && discarded_section (sec))
1483 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1484 rel, 1, relend, howto, 0, contents);
1485
1486 if (bfd_link_relocatable (info))
1487 continue;
1488
1489 r = avr_final_link_relocate (howto, input_bfd, input_section,
1490 contents, rel, relocation, htab);
1491
1492 if (r != bfd_reloc_ok)
1493 {
1494 const char * msg = (const char *) NULL;
1495
1496 switch (r)
1497 {
1498 case bfd_reloc_overflow:
1499 (*info->callbacks->reloc_overflow)
1500 (info, (h ? &h->root : NULL), name, howto->name,
1501 (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1502 break;
1503
1504 case bfd_reloc_undefined:
1505 (*info->callbacks->undefined_symbol)
1506 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
1507 break;
1508
1509 case bfd_reloc_outofrange:
1510 msg = _("internal error: out of range error");
1511 break;
1512
1513 case bfd_reloc_notsupported:
1514 msg = _("internal error: unsupported relocation error");
1515 break;
1516
1517 case bfd_reloc_dangerous:
1518 msg = _("internal error: dangerous relocation");
1519 break;
1520
1521 default:
1522 msg = _("internal error: unknown error");
1523 break;
1524 }
1525
1526 if (msg)
1527 (*info->callbacks->warning) (info, msg, name, input_bfd,
1528 input_section, rel->r_offset);
1529 }
1530 }
1531
1532 return TRUE;
1533 }
1534
1535 /* The final processing done just before writing out a AVR ELF object
1536 file. This gets the AVR architecture right based on the machine
1537 number. */
1538
1539 static bfd_boolean
1540 bfd_elf_avr_final_write_processing (bfd *abfd)
1541 {
1542 unsigned long val;
1543
1544 switch (bfd_get_mach (abfd))
1545 {
1546 default:
1547 case bfd_mach_avr2:
1548 val = E_AVR_MACH_AVR2;
1549 break;
1550
1551 case bfd_mach_avr1:
1552 val = E_AVR_MACH_AVR1;
1553 break;
1554
1555 case bfd_mach_avr25:
1556 val = E_AVR_MACH_AVR25;
1557 break;
1558
1559 case bfd_mach_avr3:
1560 val = E_AVR_MACH_AVR3;
1561 break;
1562
1563 case bfd_mach_avr31:
1564 val = E_AVR_MACH_AVR31;
1565 break;
1566
1567 case bfd_mach_avr35:
1568 val = E_AVR_MACH_AVR35;
1569 break;
1570
1571 case bfd_mach_avr4:
1572 val = E_AVR_MACH_AVR4;
1573 break;
1574
1575 case bfd_mach_avr5:
1576 val = E_AVR_MACH_AVR5;
1577 break;
1578
1579 case bfd_mach_avr51:
1580 val = E_AVR_MACH_AVR51;
1581 break;
1582
1583 case bfd_mach_avr6:
1584 val = E_AVR_MACH_AVR6;
1585 break;
1586
1587 case bfd_mach_avrxmega1:
1588 val = E_AVR_MACH_XMEGA1;
1589 break;
1590
1591 case bfd_mach_avrxmega2:
1592 val = E_AVR_MACH_XMEGA2;
1593 break;
1594
1595 case bfd_mach_avrxmega3:
1596 val = E_AVR_MACH_XMEGA3;
1597 break;
1598
1599 case bfd_mach_avrxmega4:
1600 val = E_AVR_MACH_XMEGA4;
1601 break;
1602
1603 case bfd_mach_avrxmega5:
1604 val = E_AVR_MACH_XMEGA5;
1605 break;
1606
1607 case bfd_mach_avrxmega6:
1608 val = E_AVR_MACH_XMEGA6;
1609 break;
1610
1611 case bfd_mach_avrxmega7:
1612 val = E_AVR_MACH_XMEGA7;
1613 break;
1614
1615 case bfd_mach_avrtiny:
1616 val = E_AVR_MACH_AVRTINY;
1617 break;
1618 }
1619
1620 elf_elfheader (abfd)->e_machine = EM_AVR;
1621 elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
1622 elf_elfheader (abfd)->e_flags |= val;
1623 return _bfd_elf_final_write_processing (abfd);
1624 }
1625
1626 /* Set the right machine number. */
1627
1628 static bfd_boolean
1629 elf32_avr_object_p (bfd *abfd)
1630 {
1631 unsigned int e_set = bfd_mach_avr2;
1632
1633 if (elf_elfheader (abfd)->e_machine == EM_AVR
1634 || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
1635 {
1636 int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
1637
1638 switch (e_mach)
1639 {
1640 default:
1641 case E_AVR_MACH_AVR2:
1642 e_set = bfd_mach_avr2;
1643 break;
1644
1645 case E_AVR_MACH_AVR1:
1646 e_set = bfd_mach_avr1;
1647 break;
1648
1649 case E_AVR_MACH_AVR25:
1650 e_set = bfd_mach_avr25;
1651 break;
1652
1653 case E_AVR_MACH_AVR3:
1654 e_set = bfd_mach_avr3;
1655 break;
1656
1657 case E_AVR_MACH_AVR31:
1658 e_set = bfd_mach_avr31;
1659 break;
1660
1661 case E_AVR_MACH_AVR35:
1662 e_set = bfd_mach_avr35;
1663 break;
1664
1665 case E_AVR_MACH_AVR4:
1666 e_set = bfd_mach_avr4;
1667 break;
1668
1669 case E_AVR_MACH_AVR5:
1670 e_set = bfd_mach_avr5;
1671 break;
1672
1673 case E_AVR_MACH_AVR51:
1674 e_set = bfd_mach_avr51;
1675 break;
1676
1677 case E_AVR_MACH_AVR6:
1678 e_set = bfd_mach_avr6;
1679 break;
1680
1681 case E_AVR_MACH_XMEGA1:
1682 e_set = bfd_mach_avrxmega1;
1683 break;
1684
1685 case E_AVR_MACH_XMEGA2:
1686 e_set = bfd_mach_avrxmega2;
1687 break;
1688
1689 case E_AVR_MACH_XMEGA3:
1690 e_set = bfd_mach_avrxmega3;
1691 break;
1692
1693 case E_AVR_MACH_XMEGA4:
1694 e_set = bfd_mach_avrxmega4;
1695 break;
1696
1697 case E_AVR_MACH_XMEGA5:
1698 e_set = bfd_mach_avrxmega5;
1699 break;
1700
1701 case E_AVR_MACH_XMEGA6:
1702 e_set = bfd_mach_avrxmega6;
1703 break;
1704
1705 case E_AVR_MACH_XMEGA7:
1706 e_set = bfd_mach_avrxmega7;
1707 break;
1708
1709 case E_AVR_MACH_AVRTINY:
1710 e_set = bfd_mach_avrtiny;
1711 break;
1712 }
1713 }
1714 return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
1715 e_set);
1716 }
1717
1718 /* Returns whether the relocation type passed is a diff reloc. */
1719
1720 static bfd_boolean
1721 elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel)
1722 {
1723 return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8
1724 ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16
1725 || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32);
1726 }
1727
1728 /* Reduce the diff value written in the section by count if the shrinked
1729 insn address happens to fall between the two symbols for which this
1730 diff reloc was emitted. */
1731
1732 static void
1733 elf32_avr_adjust_diff_reloc_value (bfd *abfd,
1734 struct bfd_section *isec,
1735 Elf_Internal_Rela *irel,
1736 bfd_vma symval,
1737 bfd_vma shrinked_insn_address,
1738 int count)
1739 {
1740 unsigned char *reloc_contents = NULL;
1741 unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
1742 if (isec_contents == NULL)
1743 {
1744 if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
1745 return;
1746
1747 elf_section_data (isec)->this_hdr.contents = isec_contents;
1748 }
1749
1750 reloc_contents = isec_contents + irel->r_offset;
1751
1752 /* Read value written in object file. */
1753 bfd_signed_vma x = 0;
1754 switch (ELF32_R_TYPE (irel->r_info))
1755 {
1756 case R_AVR_DIFF8:
1757 {
1758 x = bfd_get_signed_8 (abfd, reloc_contents);
1759 break;
1760 }
1761 case R_AVR_DIFF16:
1762 {
1763 x = bfd_get_signed_16 (abfd, reloc_contents);
1764 break;
1765 }
1766 case R_AVR_DIFF32:
1767 {
1768 x = bfd_get_signed_32 (abfd, reloc_contents);
1769 break;
1770 }
1771 default:
1772 {
1773 BFD_FAIL();
1774 }
1775 }
1776
1777 /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
1778 into the object file at the reloc offset. sym2's logical value is
1779 symval (<start_of_section>) + reloc addend. Compute the start and end
1780 addresses and check if the shrinked insn falls between sym1 and sym2. */
1781
1782 bfd_vma sym2_address = symval + irel->r_addend;
1783 bfd_vma sym1_address = sym2_address - x;
1784
1785 /* Don't assume sym2 is bigger than sym1 - the difference
1786 could be negative. Compute start and end addresses, and
1787 use those to see if they span shrinked_insn_address. */
1788
1789 bfd_vma start_address = sym1_address < sym2_address
1790 ? sym1_address : sym2_address;
1791 bfd_vma end_address = sym1_address > sym2_address
1792 ? sym1_address : sym2_address;
1793
1794
1795 if (shrinked_insn_address >= start_address
1796 && shrinked_insn_address < end_address)
1797 {
1798 /* Reduce the diff value by count bytes and write it back into section
1799 contents. */
1800 bfd_signed_vma new_diff = x < 0 ? x + count : x - count;
1801
1802 if (sym2_address > shrinked_insn_address)
1803 irel->r_addend -= count;
1804
1805 switch (ELF32_R_TYPE (irel->r_info))
1806 {
1807 case R_AVR_DIFF8:
1808 {
1809 bfd_put_signed_8 (abfd, new_diff, reloc_contents);
1810 break;
1811 }
1812 case R_AVR_DIFF16:
1813 {
1814 bfd_put_signed_16 (abfd, new_diff & 0xFFFF, reloc_contents);
1815 break;
1816 }
1817 case R_AVR_DIFF32:
1818 {
1819 bfd_put_signed_32 (abfd, new_diff & 0xFFFFFFFF, reloc_contents);
1820 break;
1821 }
1822 default:
1823 {
1824 BFD_FAIL();
1825 }
1826 }
1827
1828 }
1829 }
1830
1831 static void
1832 elf32_avr_adjust_reloc_if_spans_insn (bfd *abfd,
1833 asection *isec,
1834 Elf_Internal_Rela *irel, bfd_vma symval,
1835 bfd_vma shrinked_insn_address,
1836 bfd_vma shrink_boundary,
1837 int count)
1838 {
1839
1840 if (elf32_avr_is_diff_reloc (irel))
1841 {
1842 elf32_avr_adjust_diff_reloc_value (abfd, isec, irel,
1843 symval,
1844 shrinked_insn_address,
1845 count);
1846 }
1847 else
1848 {
1849 bfd_vma reloc_value = symval + irel->r_addend;
1850 bfd_boolean addend_within_shrink_boundary =
1851 (reloc_value <= shrink_boundary);
1852
1853 bfd_boolean reloc_spans_insn =
1854 (symval <= shrinked_insn_address
1855 && reloc_value > shrinked_insn_address
1856 && addend_within_shrink_boundary);
1857
1858 if (! reloc_spans_insn)
1859 return;
1860
1861 irel->r_addend -= count;
1862
1863 if (debug_relax)
1864 printf ("Relocation's addend needed to be fixed \n");
1865 }
1866 }
1867
1868 static bfd_boolean
1869 avr_should_move_sym (symvalue symval,
1870 bfd_vma start,
1871 bfd_vma end,
1872 bfd_boolean did_pad)
1873 {
1874 bfd_boolean sym_within_boundary =
1875 did_pad ? symval < end : symval <= end;
1876 return (symval > start && sym_within_boundary);
1877 }
1878
1879 static bfd_boolean
1880 avr_should_reduce_sym_size (symvalue symval,
1881 symvalue symend,
1882 bfd_vma start,
1883 bfd_vma end,
1884 bfd_boolean did_pad)
1885 {
1886 bfd_boolean sym_end_within_boundary =
1887 did_pad ? symend < end : symend <= end;
1888 return (symval <= start && symend > start && sym_end_within_boundary);
1889 }
1890
1891 static bfd_boolean
1892 avr_should_increase_sym_size (symvalue symval,
1893 symvalue symend,
1894 bfd_vma start,
1895 bfd_vma end,
1896 bfd_boolean did_pad)
1897 {
1898 return avr_should_move_sym (symval, start, end, did_pad)
1899 && symend >= end && did_pad;
1900 }
1901
1902 /* Delete some bytes from a section while changing the size of an instruction.
1903 The parameter "addr" denotes the section-relative offset pointing just
1904 behind the shrinked instruction. "addr+count" point at the first
1905 byte just behind the original unshrinked instruction. If delete_shrinks_insn
1906 is FALSE, we are deleting redundant padding bytes from relax_info prop
1907 record handling. In that case, addr is section-relative offset of start
1908 of padding, and count is the number of padding bytes to delete. */
1909
1910 static bfd_boolean
1911 elf32_avr_relax_delete_bytes (bfd *abfd,
1912 asection *sec,
1913 bfd_vma addr,
1914 int count,
1915 bfd_boolean delete_shrinks_insn)
1916 {
1917 Elf_Internal_Shdr *symtab_hdr;
1918 unsigned int sec_shndx;
1919 bfd_byte *contents;
1920 Elf_Internal_Rela *irel, *irelend;
1921 Elf_Internal_Sym *isym;
1922 Elf_Internal_Sym *isymbuf = NULL;
1923 bfd_vma toaddr;
1924 struct elf_link_hash_entry **sym_hashes;
1925 struct elf_link_hash_entry **end_hashes;
1926 unsigned int symcount;
1927 struct avr_relax_info *relax_info;
1928 struct avr_property_record *prop_record = NULL;
1929 bfd_boolean did_shrink = FALSE;
1930 bfd_boolean did_pad = FALSE;
1931
1932 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1933 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1934 contents = elf_section_data (sec)->this_hdr.contents;
1935 relax_info = get_avr_relax_info (sec);
1936
1937 toaddr = sec->size;
1938
1939 if (relax_info->records.count > 0)
1940 {
1941 /* There should be no property record within the range of deleted
1942 bytes, however, there might be a property record for ADDR, this is
1943 how we handle alignment directives.
1944 Find the next (if any) property record after the deleted bytes. */
1945 unsigned int i;
1946
1947 for (i = 0; i < relax_info->records.count; ++i)
1948 {
1949 bfd_vma offset = relax_info->records.items [i].offset;
1950
1951 BFD_ASSERT (offset <= addr || offset >= (addr + count));
1952 if (offset >= (addr + count))
1953 {
1954 prop_record = &relax_info->records.items [i];
1955 toaddr = offset;
1956 break;
1957 }
1958 }
1959 }
1960
1961 irel = elf_section_data (sec)->relocs;
1962 irelend = irel + sec->reloc_count;
1963
1964 /* Actually delete the bytes. */
1965 if (toaddr - addr - count > 0)
1966 {
1967 memmove (contents + addr, contents + addr + count,
1968 (size_t) (toaddr - addr - count));
1969 did_shrink = TRUE;
1970 }
1971 if (prop_record == NULL)
1972 {
1973 sec->size -= count;
1974 did_shrink = TRUE;
1975 }
1976 else
1977 {
1978 /* Use the property record to fill in the bytes we've opened up. */
1979 int fill = 0;
1980 switch (prop_record->type)
1981 {
1982 case RECORD_ORG_AND_FILL:
1983 fill = prop_record->data.org.fill;
1984 /* Fall through. */
1985 case RECORD_ORG:
1986 break;
1987 case RECORD_ALIGN_AND_FILL:
1988 fill = prop_record->data.align.fill;
1989 /* Fall through. */
1990 case RECORD_ALIGN:
1991 prop_record->data.align.preceding_deleted += count;
1992 break;
1993 };
1994 /* If toaddr == (addr + count), then we didn't delete anything, yet
1995 we fill count bytes backwards from toaddr. This is still ok - we
1996 end up overwriting the bytes we would have deleted. We just need
1997 to remember we didn't delete anything i.e. don't set did_shrink,
1998 so that we don't corrupt reloc offsets or symbol values.*/
1999 memset (contents + toaddr - count, fill, count);
2000 did_pad = TRUE;
2001 }
2002
2003 if (!did_shrink)
2004 return TRUE;
2005
2006 /* Adjust all the reloc addresses. */
2007 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2008 {
2009 bfd_vma old_reloc_address;
2010
2011 old_reloc_address = (sec->output_section->vma
2012 + sec->output_offset + irel->r_offset);
2013
2014 /* Get the new reloc address. */
2015 if ((irel->r_offset > addr
2016 && irel->r_offset < toaddr))
2017 {
2018 if (debug_relax)
2019 printf ("Relocation at address 0x%x needs to be moved.\n"
2020 "Old section offset: 0x%x, New section offset: 0x%x \n",
2021 (unsigned int) old_reloc_address,
2022 (unsigned int) irel->r_offset,
2023 (unsigned int) ((irel->r_offset) - count));
2024
2025 irel->r_offset -= count;
2026 }
2027
2028 }
2029
2030 /* The reloc's own addresses are now ok. However, we need to readjust
2031 the reloc's addend, i.e. the reloc's value if two conditions are met:
2032 1.) the reloc is relative to a symbol in this section that
2033 is located in front of the shrinked instruction
2034 2.) symbol plus addend end up behind the shrinked instruction.
2035
2036 The most common case where this happens are relocs relative to
2037 the section-start symbol.
2038
2039 This step needs to be done for all of the sections of the bfd. */
2040
2041 {
2042 struct bfd_section *isec;
2043
2044 for (isec = abfd->sections; isec; isec = isec->next)
2045 {
2046 bfd_vma symval;
2047 bfd_vma shrinked_insn_address;
2048
2049 if (isec->reloc_count == 0)
2050 continue;
2051
2052 shrinked_insn_address = (sec->output_section->vma
2053 + sec->output_offset + addr);
2054 if (delete_shrinks_insn)
2055 shrinked_insn_address -= count;
2056
2057 irel = elf_section_data (isec)->relocs;
2058 /* PR 12161: Read in the relocs for this section if necessary. */
2059 if (irel == NULL)
2060 irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
2061
2062 for (irelend = irel + isec->reloc_count;
2063 irel < irelend;
2064 irel++)
2065 {
2066 /* Read this BFD's local symbols if we haven't done
2067 so already. */
2068 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2069 {
2070 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2071 if (isymbuf == NULL)
2072 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2073 symtab_hdr->sh_info, 0,
2074 NULL, NULL, NULL);
2075 if (isymbuf == NULL)
2076 return FALSE;
2077 }
2078
2079 /* Get the value of the symbol referred to by the reloc. */
2080 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2081 {
2082 /* A local symbol. */
2083 asection *sym_sec;
2084
2085 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2086 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2087 symval = isym->st_value;
2088 /* If the reloc is absolute, it will not have
2089 a symbol or section associated with it. */
2090 if (sym_sec == sec)
2091 {
2092 /* If there is an alignment boundary, we only need to
2093 adjust addends that end up below the boundary. */
2094 bfd_vma shrink_boundary = (toaddr
2095 + sec->output_section->vma
2096 + sec->output_offset);
2097
2098 symval += sym_sec->output_section->vma
2099 + sym_sec->output_offset;
2100
2101 if (debug_relax)
2102 printf ("Checking if the relocation's "
2103 "addend needs corrections.\n"
2104 "Address of anchor symbol: 0x%x \n"
2105 "Address of relocation target: 0x%x \n"
2106 "Address of relaxed insn: 0x%x \n",
2107 (unsigned int) symval,
2108 (unsigned int) (symval + irel->r_addend),
2109 (unsigned int) shrinked_insn_address);
2110
2111 elf32_avr_adjust_reloc_if_spans_insn (abfd, isec, irel,
2112 symval,
2113 shrinked_insn_address,
2114 shrink_boundary,
2115 count);
2116 }
2117 /* else...Reference symbol is absolute. No adjustment needed. */
2118 }
2119 /* else...Reference symbol is extern. No need for adjusting
2120 the addend. */
2121 }
2122 }
2123 }
2124
2125 /* Adjust the local symbols defined in this section. */
2126 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2127 /* Fix PR 9841, there may be no local symbols. */
2128 if (isym != NULL)
2129 {
2130 Elf_Internal_Sym *isymend;
2131
2132 isymend = isym + symtab_hdr->sh_info;
2133 for (; isym < isymend; isym++)
2134 {
2135 if (isym->st_shndx == sec_shndx)
2136 {
2137 symvalue symval = isym->st_value;
2138 symvalue symend = symval + isym->st_size;
2139 if (avr_should_reduce_sym_size (symval, symend,
2140 addr, toaddr, did_pad))
2141 {
2142 /* If this assert fires then we have a symbol that ends
2143 part way through an instruction. Does that make
2144 sense? */
2145 BFD_ASSERT (isym->st_value + isym->st_size >= addr + count);
2146 isym->st_size -= count;
2147 }
2148 else if (avr_should_increase_sym_size (symval, symend,
2149 addr, toaddr, did_pad))
2150 isym->st_size += count;
2151
2152 if (avr_should_move_sym (symval, addr, toaddr, did_pad))
2153 isym->st_value -= count;
2154 }
2155 }
2156 }
2157
2158 /* Now adjust the global symbols defined in this section. */
2159 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2160 - symtab_hdr->sh_info);
2161 sym_hashes = elf_sym_hashes (abfd);
2162 end_hashes = sym_hashes + symcount;
2163 for (; sym_hashes < end_hashes; sym_hashes++)
2164 {
2165 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2166 if ((sym_hash->root.type == bfd_link_hash_defined
2167 || sym_hash->root.type == bfd_link_hash_defweak)
2168 && sym_hash->root.u.def.section == sec)
2169 {
2170 symvalue symval = sym_hash->root.u.def.value;
2171 symvalue symend = symval + sym_hash->size;
2172
2173 if (avr_should_reduce_sym_size (symval, symend,
2174 addr, toaddr, did_pad))
2175 {
2176 /* If this assert fires then we have a symbol that ends
2177 part way through an instruction. Does that make
2178 sense? */
2179 BFD_ASSERT (symend >= addr + count);
2180 sym_hash->size -= count;
2181 }
2182 else if (avr_should_increase_sym_size (symval, symend,
2183 addr, toaddr, did_pad))
2184 sym_hash->size += count;
2185
2186 if (avr_should_move_sym (symval, addr, toaddr, did_pad))
2187 sym_hash->root.u.def.value -= count;
2188 }
2189 }
2190
2191 return TRUE;
2192 }
2193
2194 static Elf_Internal_Sym *
2195 retrieve_local_syms (bfd *input_bfd)
2196 {
2197 Elf_Internal_Shdr *symtab_hdr;
2198 Elf_Internal_Sym *isymbuf;
2199 size_t locsymcount;
2200
2201 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2202 locsymcount = symtab_hdr->sh_info;
2203
2204 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2205 if (isymbuf == NULL && locsymcount != 0)
2206 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
2207 NULL, NULL, NULL);
2208
2209 /* Save the symbols for this input file so they won't be read again. */
2210 if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
2211 symtab_hdr->contents = (unsigned char *) isymbuf;
2212
2213 return isymbuf;
2214 }
2215
2216 /* Get the input section for a given symbol index.
2217 If the symbol is:
2218 . a section symbol, return the section;
2219 . a common symbol, return the common section;
2220 . an undefined symbol, return the undefined section;
2221 . an indirect symbol, follow the links;
2222 . an absolute value, return the absolute section. */
2223
2224 static asection *
2225 get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
2226 {
2227 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2228 asection *target_sec = NULL;
2229 if (r_symndx < symtab_hdr->sh_info)
2230 {
2231 Elf_Internal_Sym *isymbuf;
2232 unsigned int section_index;
2233
2234 isymbuf = retrieve_local_syms (abfd);
2235 section_index = isymbuf[r_symndx].st_shndx;
2236
2237 if (section_index == SHN_UNDEF)
2238 target_sec = bfd_und_section_ptr;
2239 else if (section_index == SHN_ABS)
2240 target_sec = bfd_abs_section_ptr;
2241 else if (section_index == SHN_COMMON)
2242 target_sec = bfd_com_section_ptr;
2243 else
2244 target_sec = bfd_section_from_elf_index (abfd, section_index);
2245 }
2246 else
2247 {
2248 unsigned long indx = r_symndx - symtab_hdr->sh_info;
2249 struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
2250
2251 while (h->root.type == bfd_link_hash_indirect
2252 || h->root.type == bfd_link_hash_warning)
2253 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2254
2255 switch (h->root.type)
2256 {
2257 case bfd_link_hash_defined:
2258 case bfd_link_hash_defweak:
2259 target_sec = h->root.u.def.section;
2260 break;
2261 case bfd_link_hash_common:
2262 target_sec = bfd_com_section_ptr;
2263 break;
2264 case bfd_link_hash_undefined:
2265 case bfd_link_hash_undefweak:
2266 target_sec = bfd_und_section_ptr;
2267 break;
2268 default: /* New indirect warning. */
2269 target_sec = bfd_und_section_ptr;
2270 break;
2271 }
2272 }
2273 return target_sec;
2274 }
2275
2276 /* Get the section-relative offset for a symbol number. */
2277
2278 static bfd_vma
2279 get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
2280 {
2281 Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2282 bfd_vma offset = 0;
2283
2284 if (r_symndx < symtab_hdr->sh_info)
2285 {
2286 Elf_Internal_Sym *isymbuf;
2287 isymbuf = retrieve_local_syms (abfd);
2288 offset = isymbuf[r_symndx].st_value;
2289 }
2290 else
2291 {
2292 unsigned long indx = r_symndx - symtab_hdr->sh_info;
2293 struct elf_link_hash_entry *h =
2294 elf_sym_hashes (abfd)[indx];
2295
2296 while (h->root.type == bfd_link_hash_indirect
2297 || h->root.type == bfd_link_hash_warning)
2298 h = (struct elf_link_hash_entry *) h->root.u.i.link;
2299 if (h->root.type == bfd_link_hash_defined
2300 || h->root.type == bfd_link_hash_defweak)
2301 offset = h->root.u.def.value;
2302 }
2303 return offset;
2304 }
2305
2306 /* Iterate over the property records in R_LIST, and copy each record into
2307 the list of records within the relaxation information for the section to
2308 which the record applies. */
2309
2310 static void
2311 avr_elf32_assign_records_to_sections (struct avr_property_record_list *r_list)
2312 {
2313 unsigned int i;
2314
2315 for (i = 0; i < r_list->record_count; ++i)
2316 {
2317 struct avr_relax_info *relax_info;
2318
2319 relax_info = get_avr_relax_info (r_list->records [i].section);
2320 BFD_ASSERT (relax_info != NULL);
2321
2322 if (relax_info->records.count
2323 == relax_info->records.allocated)
2324 {
2325 /* Allocate more space. */
2326 bfd_size_type size;
2327
2328 relax_info->records.allocated += 10;
2329 size = (sizeof (struct avr_property_record)
2330 * relax_info->records.allocated);
2331 relax_info->records.items
2332 = bfd_realloc (relax_info->records.items, size);
2333 }
2334
2335 memcpy (&relax_info->records.items [relax_info->records.count],
2336 &r_list->records [i],
2337 sizeof (struct avr_property_record));
2338 relax_info->records.count++;
2339 }
2340 }
2341
2342 /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the
2343 ordering callback from QSORT. */
2344
2345 static int
2346 avr_property_record_compare (const void *ap, const void *bp)
2347 {
2348 const struct avr_property_record *a
2349 = (struct avr_property_record *) ap;
2350 const struct avr_property_record *b
2351 = (struct avr_property_record *) bp;
2352
2353 if (a->offset != b->offset)
2354 return (a->offset - b->offset);
2355
2356 if (a->section != b->section)
2357 return bfd_section_vma (a->section) - bfd_section_vma (b->section);
2358
2359 return (a->type - b->type);
2360 }
2361
2362 /* Load all of the avr property sections from all of the bfd objects
2363 referenced from LINK_INFO. All of the records within each property
2364 section are assigned to the STRUCT AVR_RELAX_INFO within the section
2365 specific data of the appropriate section. */
2366
2367 static void
2368 avr_load_all_property_sections (struct bfd_link_info *link_info)
2369 {
2370 bfd *abfd;
2371 asection *sec;
2372
2373 /* Initialize the per-section relaxation info. */
2374 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2375 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2376 {
2377 init_avr_relax_info (sec);
2378 }
2379
2380 /* Load the descriptor tables from .avr.prop sections. */
2381 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2382 {
2383 struct avr_property_record_list *r_list;
2384
2385 r_list = avr_elf32_load_property_records (abfd);
2386 if (r_list != NULL)
2387 avr_elf32_assign_records_to_sections (r_list);
2388
2389 free (r_list);
2390 }
2391
2392 /* Now, for every section, ensure that the descriptor list in the
2393 relaxation data is sorted by ascending offset within the section. */
2394 for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
2395 for (sec = abfd->sections; sec != NULL; sec = sec->next)
2396 {
2397 struct avr_relax_info *relax_info = get_avr_relax_info (sec);
2398 if (relax_info && relax_info->records.count > 0)
2399 {
2400 unsigned int i;
2401
2402 qsort (relax_info->records.items,
2403 relax_info->records.count,
2404 sizeof (struct avr_property_record),
2405 avr_property_record_compare);
2406
2407 /* For debug purposes, list all the descriptors. */
2408 for (i = 0; i < relax_info->records.count; ++i)
2409 {
2410 switch (relax_info->records.items [i].type)
2411 {
2412 case RECORD_ORG:
2413 break;
2414 case RECORD_ORG_AND_FILL:
2415 break;
2416 case RECORD_ALIGN:
2417 break;
2418 case RECORD_ALIGN_AND_FILL:
2419 break;
2420 };
2421 }
2422 }
2423 }
2424 }
2425
2426 /* This function handles relaxing for the avr.
2427 Many important relaxing opportunities within functions are already
2428 realized by the compiler itself.
2429 Here we try to replace call (4 bytes) -> rcall (2 bytes)
2430 and jump -> rjmp (safes also 2 bytes).
2431 As well we now optimize seqences of
2432 - call/rcall function
2433 - ret
2434 to yield
2435 - jmp/rjmp function
2436 - ret
2437 . In case that within a sequence
2438 - jmp/rjmp label
2439 - ret
2440 the ret could no longer be reached it is optimized away. In order
2441 to check if the ret is no longer needed, it is checked that the ret's address
2442 is not the target of a branch or jump within the same section, it is checked
2443 that there is no skip instruction before the jmp/rjmp and that there
2444 is no local or global label place at the address of the ret.
2445
2446 We refrain from relaxing within sections ".vectors" and
2447 ".jumptables" in order to maintain the position of the instructions.
2448 There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
2449 if possible. (In future one could possibly use the space of the nop
2450 for the first instruction of the irq service function.
2451
2452 The .jumptables sections is meant to be used for a future tablejump variant
2453 for the devices with 3-byte program counter where the table itself
2454 contains 4-byte jump instructions whose relative offset must not
2455 be changed. */
2456
2457 static bfd_boolean
2458 elf32_avr_relax_section (bfd *abfd,
2459 asection *sec,
2460 struct bfd_link_info *link_info,
2461 bfd_boolean *again)
2462 {
2463 Elf_Internal_Shdr *symtab_hdr;
2464 Elf_Internal_Rela *internal_relocs;
2465 Elf_Internal_Rela *irel, *irelend;
2466 bfd_byte *contents = NULL;
2467 Elf_Internal_Sym *isymbuf = NULL;
2468 struct elf32_avr_link_hash_table *htab;
2469 static bfd_boolean relaxation_initialised = FALSE;
2470
2471 if (!relaxation_initialised)
2472 {
2473 relaxation_initialised = TRUE;
2474
2475 /* Load entries from the .avr.prop sections. */
2476 avr_load_all_property_sections (link_info);
2477 }
2478
2479 /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
2480 relaxing. Such shrinking can cause issues for the sections such
2481 as .vectors and .jumptables. Instead the unused bytes should be
2482 filled with nop instructions. */
2483 bfd_boolean shrinkable = TRUE;
2484
2485 if (!strcmp (sec->name,".vectors")
2486 || !strcmp (sec->name,".jumptables"))
2487 shrinkable = FALSE;
2488
2489 if (bfd_link_relocatable (link_info))
2490 (*link_info->callbacks->einfo)
2491 (_("%P%F: --relax and -r may not be used together\n"));
2492
2493 htab = avr_link_hash_table (link_info);
2494 if (htab == NULL)
2495 return FALSE;
2496
2497 /* Assume nothing changes. */
2498 *again = FALSE;
2499
2500 if ((!htab->no_stubs) && (sec == htab->stub_sec))
2501 {
2502 /* We are just relaxing the stub section.
2503 Let's calculate the size needed again. */
2504 bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
2505
2506 if (debug_relax)
2507 printf ("Relaxing the stub section. Size prior to this pass: %i\n",
2508 (int) last_estimated_stub_section_size);
2509
2510 elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
2511 link_info, FALSE);
2512
2513 /* Check if the number of trampolines changed. */
2514 if (last_estimated_stub_section_size != htab->stub_sec->size)
2515 *again = TRUE;
2516
2517 if (debug_relax)
2518 printf ("Size of stub section after this pass: %i\n",
2519 (int) htab->stub_sec->size);
2520
2521 return TRUE;
2522 }
2523
2524 /* We don't have to do anything for a relocatable link, if
2525 this section does not have relocs, or if this is not a
2526 code section. */
2527 if (bfd_link_relocatable (link_info)
2528 || (sec->flags & SEC_RELOC) == 0
2529 || sec->reloc_count == 0
2530 || (sec->flags & SEC_CODE) == 0)
2531 return TRUE;
2532
2533 /* Check if the object file to relax uses internal symbols so that we
2534 could fix up the relocations. */
2535 if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
2536 return TRUE;
2537
2538 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2539
2540 /* Get a copy of the native relocations. */
2541 internal_relocs = (_bfd_elf_link_read_relocs
2542 (abfd, sec, NULL, NULL, link_info->keep_memory));
2543 if (internal_relocs == NULL)
2544 goto error_return;
2545
2546 /* Walk through the relocs looking for relaxing opportunities. */
2547 irelend = internal_relocs + sec->reloc_count;
2548 for (irel = internal_relocs; irel < irelend; irel++)
2549 {
2550 bfd_vma symval;
2551
2552 if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
2553 && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
2554 && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
2555 continue;
2556
2557 /* Get the section contents if we haven't done so already. */
2558 if (contents == NULL)
2559 {
2560 /* Get cached copy if it exists. */
2561 if (elf_section_data (sec)->this_hdr.contents != NULL)
2562 contents = elf_section_data (sec)->this_hdr.contents;
2563 else
2564 {
2565 /* Go get them off disk. */
2566 if (! bfd_malloc_and_get_section (abfd, sec, &contents))
2567 goto error_return;
2568 }
2569 }
2570
2571 /* Read this BFD's local symbols if we haven't done so already. */
2572 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2573 {
2574 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2575 if (isymbuf == NULL)
2576 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2577 symtab_hdr->sh_info, 0,
2578 NULL, NULL, NULL);
2579 if (isymbuf == NULL)
2580 goto error_return;
2581 }
2582
2583
2584 /* Get the value of the symbol referred to by the reloc. */
2585 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2586 {
2587 /* A local symbol. */
2588 Elf_Internal_Sym *isym;
2589 asection *sym_sec;
2590
2591 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2592 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2593 symval = isym->st_value;
2594 /* If the reloc is absolute, it will not have
2595 a symbol or section associated with it. */
2596 if (sym_sec)
2597 symval += sym_sec->output_section->vma
2598 + sym_sec->output_offset;
2599 }
2600 else
2601 {
2602 unsigned long indx;
2603 struct elf_link_hash_entry *h;
2604
2605 /* An external symbol. */
2606 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2607 h = elf_sym_hashes (abfd)[indx];
2608 BFD_ASSERT (h != NULL);
2609 if (h->root.type != bfd_link_hash_defined
2610 && h->root.type != bfd_link_hash_defweak)
2611 /* This appears to be a reference to an undefined
2612 symbol. Just ignore it--it will be caught by the
2613 regular reloc processing. */
2614 continue;
2615
2616 symval = (h->root.u.def.value
2617 + h->root.u.def.section->output_section->vma
2618 + h->root.u.def.section->output_offset);
2619 }
2620
2621 /* For simplicity of coding, we are going to modify the section
2622 contents, the section relocs, and the BFD symbol table. We
2623 must tell the rest of the code not to free up this
2624 information. It would be possible to instead create a table
2625 of changes which have to be made, as is done in coff-mips.c;
2626 that would be more work, but would require less memory when
2627 the linker is run. */
2628 switch (ELF32_R_TYPE (irel->r_info))
2629 {
2630 /* Try to turn a 22-bit absolute call/jump into an 13-bit
2631 pc-relative rcall/rjmp. */
2632 case R_AVR_CALL:
2633 {
2634 bfd_vma value = symval + irel->r_addend;
2635 bfd_vma dot, gap;
2636 int distance_short_enough = 0;
2637
2638 /* Get the address of this instruction. */
2639 dot = (sec->output_section->vma
2640 + sec->output_offset + irel->r_offset);
2641
2642 /* Compute the distance from this insn to the branch target. */
2643 gap = value - dot;
2644
2645 /* The ISA manual states that addressable range is PC - 2k + 1 to
2646 PC + 2k. In bytes, that would be -4094 <= PC <= 4096. The range
2647 is shifted one word to the right, because pc-relative instructions
2648 implicitly add one word i.e. rjmp 0 jumps to next insn, not the
2649 current one.
2650 Therefore, for the !shrinkable case, the range is as above.
2651 If shrinkable, then the current code only deletes bytes 3 and
2652 4 of the absolute call/jmp, so the forward jump range increases
2653 by 2 bytes, but the backward (negative) jump range remains
2654 the same. */
2655
2656
2657 /* Check if the gap falls in the range that can be accommodated
2658 in 13bits signed (It is 12bits when encoded, as we deal with
2659 word addressing). */
2660 if (!shrinkable && ((int) gap >= -4094 && (int) gap <= 4096))
2661 distance_short_enough = 1;
2662 /* If shrinkable, then we can check for a range of distance which
2663 is two bytes farther on the positive direction because the call
2664 or jump target will be closer by two bytes after the
2665 relaxation. */
2666 else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4098))
2667 distance_short_enough = 1;
2668
2669 /* Here we handle the wrap-around case. E.g. for a 16k device
2670 we could use a rjmp to jump from address 0x100 to 0x3d00!
2671 In order to make this work properly, we need to fill the
2672 vaiable avr_pc_wrap_around with the appropriate value.
2673 I.e. 0x4000 for a 16k device. */
2674 {
2675 /* Shrinking the code size makes the gaps larger in the
2676 case of wrap-arounds. So we use a heuristical safety
2677 margin to avoid that during relax the distance gets
2678 again too large for the short jumps. Let's assume
2679 a typical code-size reduction due to relax for a
2680 16k device of 600 bytes. So let's use twice the
2681 typical value as safety margin. */
2682 int rgap;
2683 int safety_margin;
2684
2685 int assumed_shrink = 600;
2686 if (avr_pc_wrap_around > 0x4000)
2687 assumed_shrink = 900;
2688
2689 safety_margin = 2 * assumed_shrink;
2690
2691 rgap = avr_relative_distance_considering_wrap_around (gap);
2692
2693 if (rgap >= (-4092 + safety_margin)
2694 && rgap <= (4094 - safety_margin))
2695 distance_short_enough = 1;
2696 }
2697
2698 if (distance_short_enough)
2699 {
2700 unsigned char code_msb;
2701 unsigned char code_lsb;
2702
2703 if (debug_relax)
2704 printf ("shrinking jump/call instruction at address 0x%x"
2705 " in section %s\n\n",
2706 (int) dot, sec->name);
2707
2708 /* Note that we've changed the relocs, section contents,
2709 etc. */
2710 elf_section_data (sec)->relocs = internal_relocs;
2711 elf_section_data (sec)->this_hdr.contents = contents;
2712 symtab_hdr->contents = (unsigned char *) isymbuf;
2713
2714 /* Get the instruction code for relaxing. */
2715 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
2716 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2717
2718 /* Mask out the relocation bits. */
2719 code_msb &= 0x94;
2720 code_lsb &= 0x0E;
2721 if (code_msb == 0x94 && code_lsb == 0x0E)
2722 {
2723 /* we are changing call -> rcall . */
2724 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
2725 bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
2726 }
2727 else if (code_msb == 0x94 && code_lsb == 0x0C)
2728 {
2729 /* we are changeing jump -> rjmp. */
2730 bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
2731 bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
2732 }
2733 else
2734 abort ();
2735
2736 /* Fix the relocation's type. */
2737 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2738 R_AVR_13_PCREL);
2739
2740 /* We should not modify the ordering if 'shrinkable' is
2741 FALSE. */
2742 if (!shrinkable)
2743 {
2744 /* Let's insert a nop. */
2745 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
2746 bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
2747 }
2748 else
2749 {
2750 /* Delete two bytes of data. */
2751 if (!elf32_avr_relax_delete_bytes (abfd, sec,
2752 irel->r_offset + 2, 2,
2753 TRUE))
2754 goto error_return;
2755
2756 /* That will change things, so, we should relax again.
2757 Note that this is not required, and it may be slow. */
2758 *again = TRUE;
2759 }
2760 }
2761 }
2762 /* Fall through. */
2763
2764 default:
2765 {
2766 unsigned char code_msb;
2767 unsigned char code_lsb;
2768 bfd_vma dot;
2769
2770 code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2771 code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
2772
2773 /* Get the address of this instruction. */
2774 dot = (sec->output_section->vma
2775 + sec->output_offset + irel->r_offset);
2776
2777 /* Here we look for rcall/ret or call/ret sequences that could be
2778 safely replaced by rjmp/ret or jmp/ret. */
2779 if (((code_msb & 0xf0) == 0xd0)
2780 && avr_replace_call_ret_sequences)
2781 {
2782 /* This insn is a rcall. */
2783 unsigned char next_insn_msb = 0;
2784 unsigned char next_insn_lsb = 0;
2785
2786 if (irel->r_offset + 3 < sec->size)
2787 {
2788 next_insn_msb =
2789 bfd_get_8 (abfd, contents + irel->r_offset + 3);
2790 next_insn_lsb =
2791 bfd_get_8 (abfd, contents + irel->r_offset + 2);
2792 }
2793
2794 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2795 {
2796 /* The next insn is a ret. We now convert the rcall insn
2797 into a rjmp instruction. */
2798 code_msb &= 0xef;
2799 bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
2800 if (debug_relax)
2801 printf ("converted rcall/ret sequence at address 0x%x"
2802 " into rjmp/ret sequence. Section is %s\n\n",
2803 (int) dot, sec->name);
2804 *again = TRUE;
2805 break;
2806 }
2807 }
2808 else if ((0x94 == (code_msb & 0xfe))
2809 && (0x0e == (code_lsb & 0x0e))
2810 && avr_replace_call_ret_sequences)
2811 {
2812 /* This insn is a call. */
2813 unsigned char next_insn_msb = 0;
2814 unsigned char next_insn_lsb = 0;
2815
2816 if (irel->r_offset + 5 < sec->size)
2817 {
2818 next_insn_msb =
2819 bfd_get_8 (abfd, contents + irel->r_offset + 5);
2820 next_insn_lsb =
2821 bfd_get_8 (abfd, contents + irel->r_offset + 4);
2822 }
2823
2824 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2825 {
2826 /* The next insn is a ret. We now convert the call insn
2827 into a jmp instruction. */
2828
2829 code_lsb &= 0xfd;
2830 bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
2831 if (debug_relax)
2832 printf ("converted call/ret sequence at address 0x%x"
2833 " into jmp/ret sequence. Section is %s\n\n",
2834 (int) dot, sec->name);
2835 *again = TRUE;
2836 break;
2837 }
2838 }
2839 else if ((0xc0 == (code_msb & 0xf0))
2840 || ((0x94 == (code_msb & 0xfe))
2841 && (0x0c == (code_lsb & 0x0e))))
2842 {
2843 /* This insn is a rjmp or a jmp. */
2844 unsigned char next_insn_msb = 0;
2845 unsigned char next_insn_lsb = 0;
2846 int insn_size;
2847
2848 if (0xc0 == (code_msb & 0xf0))
2849 insn_size = 2; /* rjmp insn */
2850 else
2851 insn_size = 4; /* jmp insn */
2852
2853 if (irel->r_offset + insn_size + 1 < sec->size)
2854 {
2855 next_insn_msb =
2856 bfd_get_8 (abfd, contents + irel->r_offset
2857 + insn_size + 1);
2858 next_insn_lsb =
2859 bfd_get_8 (abfd, contents + irel->r_offset
2860 + insn_size);
2861 }
2862
2863 if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
2864 {
2865 /* The next insn is a ret. We possibly could delete
2866 this ret. First we need to check for preceding
2867 sbis/sbic/sbrs or cpse "skip" instructions. */
2868
2869 int there_is_preceding_non_skip_insn = 1;
2870 bfd_vma address_of_ret;
2871
2872 address_of_ret = dot + insn_size;
2873
2874 if (debug_relax && (insn_size == 2))
2875 printf ("found rjmp / ret sequence at address 0x%x\n",
2876 (int) dot);
2877 if (debug_relax && (insn_size == 4))
2878 printf ("found jmp / ret sequence at address 0x%x\n",
2879 (int) dot);
2880
2881 /* We have to make sure that there is a preceding insn. */
2882 if (irel->r_offset >= 2)
2883 {
2884 unsigned char preceding_msb;
2885 unsigned char preceding_lsb;
2886
2887 preceding_msb =
2888 bfd_get_8 (abfd, contents + irel->r_offset - 1);
2889 preceding_lsb =
2890 bfd_get_8 (abfd, contents + irel->r_offset - 2);
2891
2892 /* sbic. */
2893 if (0x99 == preceding_msb)
2894 there_is_preceding_non_skip_insn = 0;
2895
2896 /* sbis. */
2897 if (0x9b == preceding_msb)
2898 there_is_preceding_non_skip_insn = 0;
2899
2900 /* sbrc */
2901 if ((0xfc == (preceding_msb & 0xfe)
2902 && (0x00 == (preceding_lsb & 0x08))))
2903 there_is_preceding_non_skip_insn = 0;
2904
2905 /* sbrs */
2906 if ((0xfe == (preceding_msb & 0xfe)
2907 && (0x00 == (preceding_lsb & 0x08))))
2908 there_is_preceding_non_skip_insn = 0;
2909
2910 /* cpse */
2911 if (0x10 == (preceding_msb & 0xfc))
2912 there_is_preceding_non_skip_insn = 0;
2913
2914 if (there_is_preceding_non_skip_insn == 0)
2915 if (debug_relax)
2916 printf ("preceding skip insn prevents deletion of"
2917 " ret insn at Addy 0x%x in section %s\n",
2918 (int) dot + 2, sec->name);
2919 }
2920 else
2921 {
2922 /* There is no previous instruction. */
2923 there_is_preceding_non_skip_insn = 0;
2924 }
2925
2926 if (there_is_preceding_non_skip_insn)
2927 {
2928 /* We now only have to make sure that there is no
2929 local label defined at the address of the ret
2930 instruction and that there is no local relocation
2931 in this section pointing to the ret. */
2932
2933 int deleting_ret_is_safe = 1;
2934 unsigned int section_offset_of_ret_insn =
2935 irel->r_offset + insn_size;
2936 Elf_Internal_Sym *isym, *isymend;
2937 unsigned int sec_shndx;
2938 struct bfd_section *isec;
2939
2940 sec_shndx =
2941 _bfd_elf_section_from_bfd_section (abfd, sec);
2942
2943 /* Check for local symbols. */
2944 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2945 isymend = isym + symtab_hdr->sh_info;
2946 /* PR 6019: There may not be any local symbols. */
2947 for (; isym != NULL && isym < isymend; isym++)
2948 {
2949 if (isym->st_value == section_offset_of_ret_insn
2950 && isym->st_shndx == sec_shndx)
2951 {
2952 deleting_ret_is_safe = 0;
2953 if (debug_relax)
2954 printf ("local label prevents deletion of ret "
2955 "insn at address 0x%x\n",
2956 (int) dot + insn_size);
2957 }
2958 }
2959
2960 /* Now check for global symbols. */
2961 {
2962 int symcount;
2963 struct elf_link_hash_entry **sym_hashes;
2964 struct elf_link_hash_entry **end_hashes;
2965
2966 symcount = (symtab_hdr->sh_size
2967 / sizeof (Elf32_External_Sym)
2968 - symtab_hdr->sh_info);
2969 sym_hashes = elf_sym_hashes (abfd);
2970 end_hashes = sym_hashes + symcount;
2971 for (; sym_hashes < end_hashes; sym_hashes++)
2972 {
2973 struct elf_link_hash_entry *sym_hash =
2974 *sym_hashes;
2975 if ((sym_hash->root.type == bfd_link_hash_defined
2976 || sym_hash->root.type ==
2977 bfd_link_hash_defweak)
2978 && sym_hash->root.u.def.section == sec
2979 && sym_hash->root.u.def.value == section_offset_of_ret_insn)
2980 {
2981 deleting_ret_is_safe = 0;
2982 if (debug_relax)
2983 printf ("global label prevents deletion of "
2984 "ret insn at address 0x%x\n",
2985 (int) dot + insn_size);
2986 }
2987 }
2988 }
2989
2990 /* Now we check for relocations pointing to ret. */
2991 for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next)
2992 {
2993 Elf_Internal_Rela *rel;
2994 Elf_Internal_Rela *relend;
2995
2996 rel = elf_section_data (isec)->relocs;
2997 if (rel == NULL)
2998 rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
2999
3000 relend = rel + isec->reloc_count;
3001
3002 for (; rel && rel < relend; rel++)
3003 {
3004 bfd_vma reloc_target = 0;
3005
3006 /* Read this BFD's local symbols if we haven't
3007 done so already. */
3008 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3009 {
3010 isymbuf = (Elf_Internal_Sym *)
3011 symtab_hdr->contents;
3012 if (isymbuf == NULL)
3013 isymbuf = bfd_elf_get_elf_syms
3014 (abfd,
3015 symtab_hdr,
3016 symtab_hdr->sh_info, 0,
3017 NULL, NULL, NULL);
3018 if (isymbuf == NULL)
3019 break;
3020 }
3021
3022 /* Get the value of the symbol referred to
3023 by the reloc. */
3024 if (ELF32_R_SYM (rel->r_info)
3025 < symtab_hdr->sh_info)
3026 {
3027 /* A local symbol. */
3028 asection *sym_sec;
3029
3030 isym = isymbuf
3031 + ELF32_R_SYM (rel->r_info);
3032 sym_sec = bfd_section_from_elf_index
3033 (abfd, isym->st_shndx);
3034 symval = isym->st_value;
3035
3036 /* If the reloc is absolute, it will not
3037 have a symbol or section associated
3038 with it. */
3039
3040 if (sym_sec)
3041 {
3042 symval +=
3043 sym_sec->output_section->vma
3044 + sym_sec->output_offset;
3045 reloc_target = symval + rel->r_addend;
3046 }
3047 else
3048 {
3049 reloc_target = symval + rel->r_addend;
3050 /* Reference symbol is absolute. */
3051 }
3052 }
3053 /* else ... reference symbol is extern. */
3054
3055 if (address_of_ret == reloc_target)
3056 {
3057 deleting_ret_is_safe = 0;
3058 if (debug_relax)
3059 printf ("ret from "
3060 "rjmp/jmp ret sequence at address"
3061 " 0x%x could not be deleted. ret"
3062 " is target of a relocation.\n",
3063 (int) address_of_ret);
3064 break;
3065 }
3066 }
3067 }
3068
3069 if (deleting_ret_is_safe)
3070 {
3071 if (debug_relax)
3072 printf ("unreachable ret instruction "
3073 "at address 0x%x deleted.\n",
3074 (int) dot + insn_size);
3075
3076 /* Delete two bytes of data. */
3077 if (!elf32_avr_relax_delete_bytes (abfd, sec,
3078 irel->r_offset + insn_size, 2,
3079 TRUE))
3080 goto error_return;
3081
3082 /* That will change things, so, we should relax
3083 again. Note that this is not required, and it
3084 may be slow. */
3085 *again = TRUE;
3086 break;
3087 }
3088 }
3089 }
3090 }
3091 break;
3092 }
3093 }
3094 }
3095
3096 if (!*again)
3097 {
3098 /* Look through all the property records in this section to see if
3099 there's any alignment records that can be moved. */
3100 struct avr_relax_info *relax_info;
3101
3102 relax_info = get_avr_relax_info (sec);
3103 if (relax_info->records.count > 0)
3104 {
3105 unsigned int i;
3106
3107 for (i = 0; i < relax_info->records.count; ++i)
3108 {
3109 switch (relax_info->records.items [i].type)
3110 {
3111 case RECORD_ORG:
3112 case RECORD_ORG_AND_FILL:
3113 break;
3114 case RECORD_ALIGN:
3115 case RECORD_ALIGN_AND_FILL:
3116 {
3117 struct avr_property_record *record;
3118 unsigned long bytes_to_align;
3119 int count = 0;
3120
3121 /* Look for alignment directives that have had enough
3122 bytes deleted before them, such that the directive
3123 can be moved backwards and still maintain the
3124 required alignment. */
3125 record = &relax_info->records.items [i];
3126 bytes_to_align
3127 = (unsigned long) (1 << record->data.align.bytes);
3128 while (record->data.align.preceding_deleted >=
3129 bytes_to_align)
3130 {
3131 record->data.align.preceding_deleted
3132 -= bytes_to_align;
3133 count += bytes_to_align;
3134 }
3135
3136 if (count > 0)
3137 {
3138 bfd_vma addr = record->offset;
3139
3140 /* We can delete COUNT bytes and this alignment
3141 directive will still be correctly aligned.
3142 First move the alignment directive, then delete
3143 the bytes. */
3144 record->offset -= count;
3145 elf32_avr_relax_delete_bytes (abfd, sec,
3146 addr - count,
3147 count, FALSE);
3148 *again = TRUE;
3149 }
3150 }
3151 break;
3152 }
3153 }
3154 }
3155 }
3156
3157 if (contents != NULL
3158 && elf_section_data (sec)->this_hdr.contents != contents)
3159 {
3160 if (! link_info->keep_memory)
3161 free (contents);
3162 else
3163 {
3164 /* Cache the section contents for elf_link_input_bfd. */
3165 elf_section_data (sec)->this_hdr.contents = contents;
3166 }
3167 }
3168
3169 if (internal_relocs != NULL
3170 && elf_section_data (sec)->relocs != internal_relocs)
3171 free (internal_relocs);
3172
3173 return TRUE;
3174
3175 error_return:
3176 if (isymbuf != NULL
3177 && symtab_hdr->contents != (unsigned char *) isymbuf)
3178 free (isymbuf);
3179 if (contents != NULL
3180 && elf_section_data (sec)->this_hdr.contents != contents)
3181 free (contents);
3182 if (internal_relocs != NULL
3183 && elf_section_data (sec)->relocs != internal_relocs)
3184 free (internal_relocs);
3185
3186 return FALSE;
3187 }
3188
3189 /* This is a version of bfd_generic_get_relocated_section_contents
3190 which uses elf32_avr_relocate_section.
3191
3192 For avr it's essentially a cut and paste taken from the H8300 port.
3193 The author of the relaxation support patch for avr had absolutely no
3194 clue what is happening here but found out that this part of the code
3195 seems to be important. */
3196
3197 static bfd_byte *
3198 elf32_avr_get_relocated_section_contents (bfd *output_bfd,
3199 struct bfd_link_info *link_info,
3200 struct bfd_link_order *link_order,
3201 bfd_byte *data,
3202 bfd_boolean relocatable,
3203 asymbol **symbols)
3204 {
3205 Elf_Internal_Shdr *symtab_hdr;
3206 asection *input_section = link_order->u.indirect.section;
3207 bfd *input_bfd = input_section->owner;
3208 asection **sections = NULL;
3209 Elf_Internal_Rela *internal_relocs = NULL;
3210 Elf_Internal_Sym *isymbuf = NULL;
3211
3212 /* We only need to handle the case of relaxing, or of having a
3213 particular set of section contents, specially. */
3214 if (relocatable
3215 || elf_section_data (input_section)->this_hdr.contents == NULL)
3216 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3217 link_order, data,
3218 relocatable,
3219 symbols);
3220 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3221
3222 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3223 (size_t) input_section->size);
3224
3225 if ((input_section->flags & SEC_RELOC) != 0
3226 && input_section->reloc_count > 0)
3227 {
3228 asection **secpp;
3229 Elf_Internal_Sym *isym, *isymend;
3230 bfd_size_type amt;
3231
3232 internal_relocs = (_bfd_elf_link_read_relocs
3233 (input_bfd, input_section, NULL, NULL, FALSE));
3234 if (internal_relocs == NULL)
3235 goto error_return;
3236
3237 if (symtab_hdr->sh_info != 0)
3238 {
3239 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3240 if (isymbuf == NULL)
3241 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3242 symtab_hdr->sh_info, 0,
3243 NULL, NULL, NULL);
3244 if (isymbuf == NULL)
3245 goto error_return;
3246 }
3247
3248 amt = symtab_hdr->sh_info;
3249 amt *= sizeof (asection *);
3250 sections = bfd_malloc (amt);
3251 if (sections == NULL && amt != 0)
3252 goto error_return;
3253
3254 isymend = isymbuf + symtab_hdr->sh_info;
3255 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3256 {
3257 asection *isec;
3258
3259 if (isym->st_shndx == SHN_UNDEF)
3260 isec = bfd_und_section_ptr;
3261 else if (isym->st_shndx == SHN_ABS)
3262 isec = bfd_abs_section_ptr;
3263 else if (isym->st_shndx == SHN_COMMON)
3264 isec = bfd_com_section_ptr;
3265 else
3266 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3267
3268 *secpp = isec;
3269 }
3270
3271 if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
3272 input_section, data, internal_relocs,
3273 isymbuf, sections))
3274 goto error_return;
3275
3276 if (sections != NULL)
3277 free (sections);
3278 if (isymbuf != NULL
3279 && symtab_hdr->contents != (unsigned char *) isymbuf)
3280 free (isymbuf);
3281 if (elf_section_data (input_section)->relocs != internal_relocs)
3282 free (internal_relocs);
3283 }
3284
3285 return data;
3286
3287 error_return:
3288 if (sections != NULL)
3289 free (sections);
3290 if (isymbuf != NULL
3291 && symtab_hdr->contents != (unsigned char *) isymbuf)
3292 free (isymbuf);
3293 if (internal_relocs != NULL
3294 && elf_section_data (input_section)->relocs != internal_relocs)
3295 free (internal_relocs);
3296 return NULL;
3297 }
3298
3299
3300 /* Determines the hash entry name for a particular reloc. It consists of
3301 the identifier of the symbol section and the added reloc addend and
3302 symbol offset relative to the section the symbol is attached to. */
3303
3304 static char *
3305 avr_stub_name (const asection *symbol_section,
3306 const bfd_vma symbol_offset,
3307 const Elf_Internal_Rela *rela)
3308 {
3309 char *stub_name;
3310 bfd_size_type len;
3311
3312 len = 8 + 1 + 8 + 1 + 1;
3313 stub_name = bfd_malloc (len);
3314 if (stub_name != NULL)
3315 sprintf (stub_name, "%08x+%08x",
3316 symbol_section->id & 0xffffffff,
3317 (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
3318
3319 return stub_name;
3320 }
3321
3322
3323 /* Add a new stub entry to the stub hash. Not all fields of the new
3324 stub entry are initialised. */
3325
3326 static struct elf32_avr_stub_hash_entry *
3327 avr_add_stub (const char *stub_name,
3328 struct elf32_avr_link_hash_table *htab)
3329 {
3330 struct elf32_avr_stub_hash_entry *hsh;
3331
3332 /* Enter this entry into the linker stub hash table. */
3333 hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
3334
3335 if (hsh == NULL)
3336 {
3337 /* xgettext:c-format */
3338 _bfd_error_handler (_("cannot create stub entry %s"), stub_name);
3339 return NULL;
3340 }
3341
3342 hsh->stub_offset = 0;
3343 return hsh;
3344 }
3345
3346 /* We assume that there is already space allocated for the stub section
3347 contents and that before building the stubs the section size is
3348 initialized to 0. We assume that within the stub hash table entry,
3349 the absolute position of the jmp target has been written in the
3350 target_value field. We write here the offset of the generated jmp insn
3351 relative to the trampoline section start to the stub_offset entry in
3352 the stub hash table entry. */
3353
3354 static bfd_boolean
3355 avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
3356 {
3357 struct elf32_avr_stub_hash_entry *hsh;
3358 struct bfd_link_info *info;
3359 struct elf32_avr_link_hash_table *htab;
3360 bfd *stub_bfd;
3361 bfd_byte *loc;
3362 bfd_vma target;
3363 bfd_vma starget;
3364
3365 /* Basic opcode */
3366 bfd_vma jmp_insn = 0x0000940c;
3367
3368 /* Massage our args to the form they really have. */
3369 hsh = avr_stub_hash_entry (bh);
3370
3371 if (!hsh->is_actually_needed)
3372 return TRUE;
3373
3374 info = (struct bfd_link_info *) in_arg;
3375
3376 htab = avr_link_hash_table (info);
3377 if (htab == NULL)
3378 return FALSE;
3379
3380 target = hsh->target_value;
3381
3382 /* Make a note of the offset within the stubs for this entry. */
3383 hsh->stub_offset = htab->stub_sec->size;
3384 loc = htab->stub_sec->contents + hsh->stub_offset;
3385
3386 stub_bfd = htab->stub_sec->owner;
3387
3388 if (debug_stubs)
3389 printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
3390 (unsigned int) target,
3391 (unsigned int) hsh->stub_offset);
3392
3393 /* We now have to add the information on the jump target to the bare
3394 opcode bits already set in jmp_insn. */
3395
3396 /* Check for the alignment of the address. */
3397 if (target & 1)
3398 return FALSE;
3399
3400 starget = target >> 1;
3401 jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
3402 bfd_put_16 (stub_bfd, jmp_insn, loc);
3403 bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
3404
3405 htab->stub_sec->size += 4;
3406
3407 /* Now add the entries in the address mapping table if there is still
3408 space left. */
3409 {
3410 unsigned int nr;
3411
3412 nr = htab->amt_entry_cnt + 1;
3413 if (nr <= htab->amt_max_entry_cnt)
3414 {
3415 htab->amt_entry_cnt = nr;
3416
3417 htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
3418 htab->amt_destination_addr[nr - 1] = target;
3419 }
3420 }
3421
3422 return TRUE;
3423 }
3424
3425 static bfd_boolean
3426 avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
3427 void *in_arg ATTRIBUTE_UNUSED)
3428 {
3429 struct elf32_avr_stub_hash_entry *hsh;
3430
3431 hsh = avr_stub_hash_entry (bh);
3432 hsh->is_actually_needed = FALSE;
3433
3434 return TRUE;
3435 }
3436
3437 static bfd_boolean
3438 avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
3439 {
3440 struct elf32_avr_stub_hash_entry *hsh;
3441 struct elf32_avr_link_hash_table *htab;
3442 int size;
3443
3444 /* Massage our args to the form they really have. */
3445 hsh = avr_stub_hash_entry (bh);
3446 htab = in_arg;
3447
3448 if (hsh->is_actually_needed)
3449 size = 4;
3450 else
3451 size = 0;
3452
3453 htab->stub_sec->size += size;
3454 return TRUE;
3455 }
3456
3457 void
3458 elf32_avr_setup_params (struct bfd_link_info *info,
3459 bfd *avr_stub_bfd,
3460 asection *avr_stub_section,
3461 bfd_boolean no_stubs,
3462 bfd_boolean deb_stubs,
3463 bfd_boolean deb_relax,
3464 bfd_vma pc_wrap_around,
3465 bfd_boolean call_ret_replacement)
3466 {
3467 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3468
3469 if (htab == NULL)
3470 return;
3471 htab->stub_sec = avr_stub_section;
3472 htab->stub_bfd = avr_stub_bfd;
3473 htab->no_stubs = no_stubs;
3474
3475 debug_relax = deb_relax;
3476 debug_stubs = deb_stubs;
3477 avr_pc_wrap_around = pc_wrap_around;
3478 avr_replace_call_ret_sequences = call_ret_replacement;
3479 }
3480
3481
3482 /* Set up various things so that we can make a list of input sections
3483 for each output section included in the link. Returns -1 on error,
3484 0 when no stubs will be needed, and 1 on success. It also sets
3485 information on the stubs bfd and the stub section in the info
3486 struct. */
3487
3488 int
3489 elf32_avr_setup_section_lists (bfd *output_bfd,
3490 struct bfd_link_info *info)
3491 {
3492 bfd *input_bfd;
3493 unsigned int bfd_count;
3494 unsigned int top_id, top_index;
3495 asection *section;
3496 asection **input_list, **list;
3497 bfd_size_type amt;
3498 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3499
3500 if (htab == NULL || htab->no_stubs)
3501 return 0;
3502
3503 /* Count the number of input BFDs and find the top input section id. */
3504 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
3505 input_bfd != NULL;
3506 input_bfd = input_bfd->link.next)
3507 {
3508 bfd_count += 1;
3509 for (section = input_bfd->sections;
3510 section != NULL;
3511 section = section->next)
3512 if (top_id < section->id)
3513 top_id = section->id;
3514 }
3515
3516 htab->bfd_count = bfd_count;
3517
3518 /* We can't use output_bfd->section_count here to find the top output
3519 section index as some sections may have been removed, and
3520 strip_excluded_output_sections doesn't renumber the indices. */
3521 for (section = output_bfd->sections, top_index = 0;
3522 section != NULL;
3523 section = section->next)
3524 if (top_index < section->index)
3525 top_index = section->index;
3526
3527 htab->top_index = top_index;
3528 amt = sizeof (asection *) * (top_index + 1);
3529 input_list = bfd_malloc (amt);
3530 htab->input_list = input_list;
3531 if (input_list == NULL)
3532 return -1;
3533
3534 /* For sections we aren't interested in, mark their entries with a
3535 value we can check later. */
3536 list = input_list + top_index;
3537 do
3538 *list = bfd_abs_section_ptr;
3539 while (list-- != input_list);
3540
3541 for (section = output_bfd->sections;
3542 section != NULL;
3543 section = section->next)
3544 if ((section->flags & SEC_CODE) != 0)
3545 input_list[section->index] = NULL;
3546
3547 return 1;
3548 }
3549
3550
3551 /* Read in all local syms for all input bfds, and create hash entries
3552 for export stubs if we are building a multi-subspace shared lib.
3553 Returns -1 on error, 0 otherwise. */
3554
3555 static int
3556 get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
3557 {
3558 unsigned int bfd_indx;
3559 Elf_Internal_Sym *local_syms, **all_local_syms;
3560 struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
3561 bfd_size_type amt;
3562
3563 if (htab == NULL)
3564 return -1;
3565
3566 /* We want to read in symbol extension records only once. To do this
3567 we need to read in the local symbols in parallel and save them for
3568 later use; so hold pointers to the local symbols in an array. */
3569 amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
3570 all_local_syms = bfd_zmalloc (amt);
3571 htab->all_local_syms = all_local_syms;
3572 if (all_local_syms == NULL)
3573 return -1;
3574
3575 /* Walk over all the input BFDs, swapping in local symbols.
3576 If we are creating a shared library, create hash entries for the
3577 export stubs. */
3578 for (bfd_indx = 0;
3579 input_bfd != NULL;
3580 input_bfd = input_bfd->link.next, bfd_indx++)
3581 {
3582 Elf_Internal_Shdr *symtab_hdr;
3583
3584 /* We'll need the symbol table in a second. */
3585 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3586 if (symtab_hdr->sh_info == 0)
3587 continue;
3588
3589 /* We need an array of the local symbols attached to the input bfd. */
3590 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
3591 if (local_syms == NULL)
3592 {
3593 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3594 symtab_hdr->sh_info, 0,
3595 NULL, NULL, NULL);
3596 /* Cache them for elf_link_input_bfd. */
3597 symtab_hdr->contents = (unsigned char *) local_syms;
3598 }
3599 if (local_syms == NULL)
3600 return -1;
3601
3602 all_local_syms[bfd_indx] = local_syms;
3603 }
3604
3605 return 0;
3606 }
3607
3608 #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
3609
3610 bfd_boolean
3611 elf32_avr_size_stubs (bfd *output_bfd,
3612 struct bfd_link_info *info,
3613 bfd_boolean is_prealloc_run)
3614 {
3615 struct elf32_avr_link_hash_table *htab;
3616 int stub_changed = 0;
3617
3618 htab = avr_link_hash_table (info);
3619 if (htab == NULL)
3620 return FALSE;
3621
3622 /* At this point we initialize htab->vector_base
3623 To the start of the text output section. */
3624 htab->vector_base = htab->stub_sec->output_section->vma;
3625
3626 if (get_local_syms (info->input_bfds, info))
3627 {
3628 if (htab->all_local_syms)
3629 goto error_ret_free_local;
3630 return FALSE;
3631 }
3632
3633 if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
3634 {
3635 struct elf32_avr_stub_hash_entry *test;
3636
3637 test = avr_add_stub ("Hugo",htab);
3638 test->target_value = 0x123456;
3639 test->stub_offset = 13;
3640
3641 test = avr_add_stub ("Hugo2",htab);
3642 test->target_value = 0x84210;
3643 test->stub_offset = 14;
3644 }
3645
3646 while (1)
3647 {
3648 bfd *input_bfd;
3649 unsigned int bfd_indx;
3650
3651 /* We will have to re-generate the stub hash table each time anything
3652 in memory has changed. */
3653
3654 bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
3655 for (input_bfd = info->input_bfds, bfd_indx = 0;
3656 input_bfd != NULL;
3657 input_bfd = input_bfd->link.next, bfd_indx++)
3658 {
3659 Elf_Internal_Shdr *symtab_hdr;
3660 asection *section;
3661 Elf_Internal_Sym *local_syms;
3662
3663 /* We'll need the symbol table in a second. */
3664 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3665 if (symtab_hdr->sh_info == 0)
3666 continue;
3667
3668 local_syms = htab->all_local_syms[bfd_indx];
3669
3670 /* Walk over each section attached to the input bfd. */
3671 for (section = input_bfd->sections;
3672 section != NULL;
3673 section = section->next)
3674 {
3675 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
3676
3677 /* If there aren't any relocs, then there's nothing more
3678 to do. */
3679 if ((section->flags & SEC_RELOC) == 0
3680 || section->reloc_count == 0)
3681 continue;
3682
3683 /* If this section is a link-once section that will be
3684 discarded, then don't create any stubs. */
3685 if (section->output_section == NULL
3686 || section->output_section->owner != output_bfd)
3687 continue;
3688
3689 /* Get the relocs. */
3690 internal_relocs
3691 = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
3692 info->keep_memory);
3693 if (internal_relocs == NULL)
3694 goto error_ret_free_local;
3695
3696 /* Now examine each relocation. */
3697 irela = internal_relocs;
3698 irelaend = irela + section->reloc_count;
3699 for (; irela < irelaend; irela++)
3700 {
3701 unsigned int r_type, r_indx;
3702 struct elf32_avr_stub_hash_entry *hsh;
3703 asection *sym_sec;
3704 bfd_vma sym_value;
3705 bfd_vma destination;
3706 struct elf_link_hash_entry *hh;
3707 char *stub_name;
3708
3709 r_type = ELF32_R_TYPE (irela->r_info);
3710 r_indx = ELF32_R_SYM (irela->r_info);
3711
3712 /* Only look for 16 bit GS relocs. No other reloc will need a
3713 stub. */
3714 if (!((r_type == R_AVR_16_PM)
3715 || (r_type == R_AVR_LO8_LDI_GS)
3716 || (r_type == R_AVR_HI8_LDI_GS)))
3717 continue;
3718
3719 /* Now determine the call target, its name, value,
3720 section. */
3721 sym_sec = NULL;
3722 sym_value = 0;
3723 destination = 0;
3724 hh = NULL;
3725 if (r_indx < symtab_hdr->sh_info)
3726 {
3727 /* It's a local symbol. */
3728 Elf_Internal_Sym *sym;
3729 Elf_Internal_Shdr *hdr;
3730 unsigned int shndx;
3731
3732 sym = local_syms + r_indx;
3733 if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
3734 sym_value = sym->st_value;
3735 shndx = sym->st_shndx;
3736 if (shndx < elf_numsections (input_bfd))
3737 {
3738 hdr = elf_elfsections (input_bfd)[shndx];
3739 sym_sec = hdr->bfd_section;
3740 destination = (sym_value + irela->r_addend
3741 + sym_sec->output_offset
3742 + sym_sec->output_section->vma);
3743 }
3744 }
3745 else
3746 {
3747 /* It's an external symbol. */
3748 int e_indx;
3749
3750 e_indx = r_indx - symtab_hdr->sh_info;
3751 hh = elf_sym_hashes (input_bfd)[e_indx];
3752
3753 while (hh->root.type == bfd_link_hash_indirect
3754 || hh->root.type == bfd_link_hash_warning)
3755 hh = (struct elf_link_hash_entry *)
3756 (hh->root.u.i.link);
3757
3758 if (hh->root.type == bfd_link_hash_defined
3759 || hh->root.type == bfd_link_hash_defweak)
3760 {
3761 sym_sec = hh->root.u.def.section;
3762 sym_value = hh->root.u.def.value;
3763 if (sym_sec->output_section != NULL)
3764 destination = (sym_value + irela->r_addend
3765 + sym_sec->output_offset
3766 + sym_sec->output_section->vma);
3767 }
3768 else if (hh->root.type == bfd_link_hash_undefweak)
3769 {
3770 if (! bfd_link_pic (info))
3771 continue;
3772 }
3773 else if (hh->root.type == bfd_link_hash_undefined)
3774 {
3775 if (! (info->unresolved_syms_in_objects == RM_IGNORE
3776 && (ELF_ST_VISIBILITY (hh->other)
3777 == STV_DEFAULT)))
3778 continue;
3779 }
3780 else
3781 {
3782 bfd_set_error (bfd_error_bad_value);
3783
3784 error_ret_free_internal:
3785 if (elf_section_data (section)->relocs == NULL)
3786 free (internal_relocs);
3787 goto error_ret_free_local;
3788 }
3789 }
3790
3791 if (! avr_stub_is_required_for_16_bit_reloc
3792 (destination - htab->vector_base))
3793 {
3794 if (!is_prealloc_run)
3795 /* We are having a reloc that does't need a stub. */
3796 continue;
3797
3798 /* We don't right now know if a stub will be needed.
3799 Let's rather be on the safe side. */
3800 }
3801
3802 /* Get the name of this stub. */
3803 stub_name = avr_stub_name (sym_sec, sym_value, irela);
3804
3805 if (!stub_name)
3806 goto error_ret_free_internal;
3807
3808
3809 hsh = avr_stub_hash_lookup (&htab->bstab,
3810 stub_name,
3811 FALSE, FALSE);
3812 if (hsh != NULL)
3813 {
3814 /* The proper stub has already been created. Mark it
3815 to be used and write the possibly changed destination
3816 value. */
3817 hsh->is_actually_needed = TRUE;
3818 hsh->target_value = destination;
3819 free (stub_name);
3820 continue;
3821 }
3822
3823 hsh = avr_add_stub (stub_name, htab);
3824 if (hsh == NULL)
3825 {
3826 free (stub_name);
3827 goto error_ret_free_internal;
3828 }
3829
3830 hsh->is_actually_needed = TRUE;
3831 hsh->target_value = destination;
3832
3833 if (debug_stubs)
3834 printf ("Adding stub with destination 0x%x to the"
3835 " hash table.\n", (unsigned int) destination);
3836 if (debug_stubs)
3837 printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
3838
3839 stub_changed = TRUE;
3840 }
3841
3842 /* We're done with the internal relocs, free them. */
3843 if (elf_section_data (section)->relocs == NULL)
3844 free (internal_relocs);
3845 }
3846 }
3847
3848 /* Re-Calculate the number of needed stubs. */
3849 htab->stub_sec->size = 0;
3850 bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
3851
3852 if (!stub_changed)
3853 break;
3854
3855 stub_changed = FALSE;
3856 }
3857
3858 free (htab->all_local_syms);
3859 return TRUE;
3860
3861 error_ret_free_local:
3862 free (htab->all_local_syms);
3863 return FALSE;
3864 }
3865
3866
3867 /* Build all the stubs associated with the current output file. The
3868 stubs are kept in a hash table attached to the main linker hash
3869 table. We also set up the .plt entries for statically linked PIC
3870 functions here. This function is called via hppaelf_finish in the
3871 linker. */
3872
3873 bfd_boolean
3874 elf32_avr_build_stubs (struct bfd_link_info *info)
3875 {
3876 asection *stub_sec;
3877 struct bfd_hash_table *table;
3878 struct elf32_avr_link_hash_table *htab;
3879 bfd_size_type total_size = 0;
3880
3881 htab = avr_link_hash_table (info);
3882 if (htab == NULL)
3883 return FALSE;
3884
3885 /* In case that there were several stub sections: */
3886 for (stub_sec = htab->stub_bfd->sections;
3887 stub_sec != NULL;
3888 stub_sec = stub_sec->next)
3889 {
3890 bfd_size_type size;
3891
3892 /* Allocate memory to hold the linker stubs. */
3893 size = stub_sec->size;
3894 total_size += size;
3895
3896 stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
3897 if (stub_sec->contents == NULL && size != 0)
3898 return FALSE;
3899 stub_sec->size = 0;
3900 }
3901
3902 /* Allocate memory for the adress mapping table. */
3903 htab->amt_entry_cnt = 0;
3904 htab->amt_max_entry_cnt = total_size / 4;
3905 htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
3906 * htab->amt_max_entry_cnt);
3907 htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
3908 * htab->amt_max_entry_cnt );
3909
3910 if (debug_stubs)
3911 printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
3912
3913 /* Build the stubs as directed by the stub hash table. */
3914 table = &htab->bstab;
3915 bfd_hash_traverse (table, avr_build_one_stub, info);
3916
3917 if (debug_stubs)
3918 printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
3919
3920 return TRUE;
3921 }
3922
3923 /* Callback used by QSORT to order relocations AP and BP. */
3924
3925 static int
3926 internal_reloc_compare (const void *ap, const void *bp)
3927 {
3928 const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
3929 const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
3930
3931 if (a->r_offset != b->r_offset)
3932 return (a->r_offset - b->r_offset);
3933
3934 /* We don't need to sort on these criteria for correctness,
3935 but enforcing a more strict ordering prevents unstable qsort
3936 from behaving differently with different implementations.
3937 Without the code below we get correct but different results
3938 on Solaris 2.7 and 2.8. We would like to always produce the
3939 same results no matter the host. */
3940
3941 if (a->r_info != b->r_info)
3942 return (a->r_info - b->r_info);
3943
3944 return (a->r_addend - b->r_addend);
3945 }
3946
3947 /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */
3948
3949 static bfd_boolean
3950 avr_is_section_for_address (asection *section, bfd_vma address)
3951 {
3952 bfd_vma vma;
3953 bfd_size_type size;
3954
3955 vma = bfd_section_vma (section);
3956 if (address < vma)
3957 return FALSE;
3958
3959 size = section->size;
3960 if (address >= vma + size)
3961 return FALSE;
3962
3963 return TRUE;
3964 }
3965
3966 /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */
3967
3968 struct avr_find_section_data
3969 {
3970 /* The address we're looking for. */
3971 bfd_vma address;
3972
3973 /* The section we've found. */
3974 asection *section;
3975 };
3976
3977 /* Helper function to locate the section holding a certain virtual memory
3978 address. This is called via bfd_map_over_sections. The DATA is an
3979 instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which
3980 has been set to the address to search for, and the section field has
3981 been set to NULL. If SECTION from ABFD contains ADDRESS then the
3982 section field in DATA will be set to SECTION. As an optimisation, if
3983 the section field is already non-null then this function does not
3984 perform any checks, and just returns. */
3985
3986 static void
3987 avr_find_section_for_address (bfd *abfd ATTRIBUTE_UNUSED,
3988 asection *section, void *data)
3989 {
3990 struct avr_find_section_data *fs_data
3991 = (struct avr_find_section_data *) data;
3992
3993 /* Return if already found. */
3994 if (fs_data->section != NULL)
3995 return;
3996
3997 /* If this section isn't part of the addressable code content, skip it. */
3998 if ((bfd_section_flags (section) & SEC_ALLOC) == 0
3999 && (bfd_section_flags (section) & SEC_CODE) == 0)
4000 return;
4001
4002 if (avr_is_section_for_address (section, fs_data->address))
4003 fs_data->section = section;
4004 }
4005
4006 /* Load all of the property records from SEC, a section from ABFD. Return
4007 a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The
4008 memory for the returned structure, and all of the records pointed too by
4009 the structure are allocated with a single call to malloc, so, only the
4010 pointer returned needs to be free'd. */
4011
4012 static struct avr_property_record_list *
4013 avr_elf32_load_records_from_section (bfd *abfd, asection *sec)
4014 {
4015 char *contents = NULL, *ptr;
4016 bfd_size_type size, mem_size;
4017 bfd_byte version, flags;
4018 uint16_t record_count, i;
4019 struct avr_property_record_list *r_list = NULL;
4020 Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end;
4021 struct avr_find_section_data fs_data;
4022
4023 fs_data.section = NULL;
4024
4025 size = bfd_section_size (sec);
4026 contents = bfd_malloc (size);
4027 bfd_get_section_contents (abfd, sec, contents, 0, size);
4028 ptr = contents;
4029
4030 /* Load the relocations for the '.avr.prop' section if there are any, and
4031 sort them. */
4032 internal_relocs = (_bfd_elf_link_read_relocs
4033 (abfd, sec, NULL, NULL, FALSE));
4034 if (internal_relocs)
4035 qsort (internal_relocs, sec->reloc_count,
4036 sizeof (Elf_Internal_Rela), internal_reloc_compare);
4037
4038 /* There is a header at the start of the property record section SEC, the
4039 format of this header is:
4040 uint8_t : version number
4041 uint8_t : flags
4042 uint16_t : record counter
4043 */
4044
4045 /* Check we have at least got a headers worth of bytes. */
4046 if (size < AVR_PROPERTY_SECTION_HEADER_SIZE)
4047 goto load_failed;
4048
4049 version = *((bfd_byte *) ptr);
4050 ptr++;
4051 flags = *((bfd_byte *) ptr);
4052 ptr++;
4053 record_count = *((uint16_t *) ptr);
4054 ptr+=2;
4055 BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE);
4056
4057 /* Now allocate space for the list structure, and all of the list
4058 elements in a single block. */
4059 mem_size = sizeof (struct avr_property_record_list)
4060 + sizeof (struct avr_property_record) * record_count;
4061 r_list = bfd_malloc (mem_size);
4062 if (r_list == NULL)
4063 goto load_failed;
4064
4065 r_list->version = version;
4066 r_list->flags = flags;
4067 r_list->section = sec;
4068 r_list->record_count = record_count;
4069 r_list->records = (struct avr_property_record *) (&r_list [1]);
4070 size -= AVR_PROPERTY_SECTION_HEADER_SIZE;
4071
4072 /* Check that we understand the version number. There is only one
4073 version number right now, anything else is an error. */
4074 if (r_list->version != AVR_PROPERTY_RECORDS_VERSION)
4075 goto load_failed;
4076
4077 rel = internal_relocs;
4078 rel_end = rel + sec->reloc_count;
4079 for (i = 0; i < record_count; ++i)
4080 {
4081 bfd_vma address;
4082
4083 /* Each entry is a 32-bit address, followed by a single byte type.
4084 After that is the type specific data. We must take care to
4085 ensure that we don't read beyond the end of the section data. */
4086 if (size < 5)
4087 goto load_failed;
4088
4089 r_list->records [i].section = NULL;
4090 r_list->records [i].offset = 0;
4091
4092 if (rel)
4093 {
4094 /* The offset of the address within the .avr.prop section. */
4095 size_t offset = ptr - contents;
4096
4097 while (rel < rel_end && rel->r_offset < offset)
4098 ++rel;
4099
4100 if (rel == rel_end)
4101 rel = NULL;
4102 else if (rel->r_offset == offset)
4103 {
4104 /* Find section and section offset. */
4105 unsigned long r_symndx;
4106
4107 asection * rel_sec;
4108 bfd_vma sec_offset;
4109
4110 r_symndx = ELF32_R_SYM (rel->r_info);
4111 rel_sec = get_elf_r_symndx_section (abfd, r_symndx);
4112 sec_offset = get_elf_r_symndx_offset (abfd, r_symndx)
4113 + rel->r_addend;
4114
4115 r_list->records [i].section = rel_sec;
4116 r_list->records [i].offset = sec_offset;
4117 }
4118 }
4119
4120 address = *((uint32_t *) ptr);
4121 ptr += 4;
4122 size -= 4;
4123
4124 if (r_list->records [i].section == NULL)
4125 {
4126 /* Try to find section and offset from address. */
4127 if (fs_data.section != NULL
4128 && !avr_is_section_for_address (fs_data.section, address))
4129 fs_data.section = NULL;
4130
4131 if (fs_data.section == NULL)
4132 {
4133 fs_data.address = address;
4134 bfd_map_over_sections (abfd, avr_find_section_for_address,
4135 &fs_data);
4136 }
4137
4138 if (fs_data.section == NULL)
4139 {
4140 fprintf (stderr, "Failed to find matching section.\n");
4141 goto load_failed;
4142 }
4143
4144 r_list->records [i].section = fs_data.section;
4145 r_list->records [i].offset
4146 = address - bfd_section_vma (fs_data.section);
4147 }
4148
4149 r_list->records [i].type = *((bfd_byte *) ptr);
4150 ptr += 1;
4151 size -= 1;
4152
4153 switch (r_list->records [i].type)
4154 {
4155 case RECORD_ORG:
4156 /* Nothing else to load. */
4157 break;
4158 case RECORD_ORG_AND_FILL:
4159 /* Just a 4-byte fill to load. */
4160 if (size < 4)
4161 goto load_failed;
4162 r_list->records [i].data.org.fill = *((uint32_t *) ptr);
4163 ptr += 4;
4164 size -= 4;
4165 break;
4166 case RECORD_ALIGN:
4167 /* Just a 4-byte alignment to load. */
4168 if (size < 4)
4169 goto load_failed;
4170 r_list->records [i].data.align.bytes = *((uint32_t *) ptr);
4171 ptr += 4;
4172 size -= 4;
4173 /* Just initialise PRECEDING_DELETED field, this field is
4174 used during linker relaxation. */
4175 r_list->records [i].data.align.preceding_deleted = 0;
4176 break;
4177 case RECORD_ALIGN_AND_FILL:
4178 /* A 4-byte alignment, and a 4-byte fill to load. */
4179 if (size < 8)
4180 goto load_failed;
4181 r_list->records [i].data.align.bytes = *((uint32_t *) ptr);
4182 ptr += 4;
4183 r_list->records [i].data.align.fill = *((uint32_t *) ptr);
4184 ptr += 4;
4185 size -= 8;
4186 /* Just initialise PRECEDING_DELETED field, this field is
4187 used during linker relaxation. */
4188 r_list->records [i].data.align.preceding_deleted = 0;
4189 break;
4190 default:
4191 goto load_failed;
4192 }
4193 }
4194
4195 free (contents);
4196 if (elf_section_data (sec)->relocs != internal_relocs)
4197 free (internal_relocs);
4198 return r_list;
4199
4200 load_failed:
4201 if (elf_section_data (sec)->relocs != internal_relocs)
4202 free (internal_relocs);
4203 free (contents);
4204 free (r_list);
4205 return NULL;
4206 }
4207
4208 /* Load all of the property records from ABFD. See
4209 AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */
4210
4211 struct avr_property_record_list *
4212 avr_elf32_load_property_records (bfd *abfd)
4213 {
4214 asection *sec;
4215
4216 /* Find the '.avr.prop' section and load the contents into memory. */
4217 sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME);
4218 if (sec == NULL)
4219 return NULL;
4220 return avr_elf32_load_records_from_section (abfd, sec);
4221 }
4222
4223 const char *
4224 avr_elf32_property_record_name (struct avr_property_record *rec)
4225 {
4226 const char *str;
4227
4228 switch (rec->type)
4229 {
4230 case RECORD_ORG:
4231 str = "ORG";
4232 break;
4233 case RECORD_ORG_AND_FILL:
4234 str = "ORG+FILL";
4235 break;
4236 case RECORD_ALIGN:
4237 str = "ALIGN";
4238 break;
4239 case RECORD_ALIGN_AND_FILL:
4240 str = "ALIGN+FILL";
4241 break;
4242 default:
4243 str = "unknown";
4244 }
4245
4246 return str;
4247 }
4248
4249
4250 #define ELF_ARCH bfd_arch_avr
4251 #define ELF_TARGET_ID AVR_ELF_DATA
4252 #define ELF_MACHINE_CODE EM_AVR
4253 #define ELF_MACHINE_ALT1 EM_AVR_OLD
4254 #define ELF_MAXPAGESIZE 1
4255
4256 #define TARGET_LITTLE_SYM avr_elf32_vec
4257 #define TARGET_LITTLE_NAME "elf32-avr"
4258
4259 #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
4260
4261 #define elf_info_to_howto avr_info_to_howto_rela
4262 #define elf_info_to_howto_rel NULL
4263 #define elf_backend_relocate_section elf32_avr_relocate_section
4264 #define elf_backend_can_gc_sections 1
4265 #define elf_backend_rela_normal 1
4266 #define elf_backend_final_write_processing \
4267 bfd_elf_avr_final_write_processing
4268 #define elf_backend_object_p elf32_avr_object_p
4269
4270 #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
4271 #define bfd_elf32_bfd_get_relocated_section_contents \
4272 elf32_avr_get_relocated_section_contents
4273 #define bfd_elf32_new_section_hook elf_avr_new_section_hook
4274 #define elf_backend_special_sections elf_avr_special_sections
4275
4276 #include "elf32-target.h"
This page took 0.117786 seconds and 4 git commands to generate.