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