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