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