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