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