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