Commit | Line | Data |
---|---|---|
adde6300 | 1 | /* AVR-specific support for 32-bit ELF |
b3adc24a | 2 | Copyright (C) 1999-2020 Free Software Foundation, Inc. |
adde6300 AM |
3 | Contributed by Denis Chertykov <denisc@overta.ru> |
4 | ||
750bce0e | 5 | This file is part of BFD, the Binary File Descriptor library. |
adde6300 | 6 | |
750bce0e NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
750bce0e | 10 | (at your option) any later version. |
adde6300 | 11 | |
750bce0e NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
adde6300 | 16 | |
750bce0e NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
4cdc7696 | 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, |
df406460 | 20 | Boston, MA 02110-1301, USA. */ |
adde6300 | 21 | |
adde6300 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
adde6300 AM |
24 | #include "libbfd.h" |
25 | #include "elf-bfd.h" | |
26 | #include "elf/avr.h" | |
28c9d252 NC |
27 | #include "elf32-avr.h" |
28 | ||
29 | /* Enable debugging printout at stdout with this variable. */ | |
30 | static bfd_boolean debug_relax = FALSE; | |
31 | ||
32 | /* Enable debugging printout at stdout with this variable. */ | |
33 | static bfd_boolean debug_stubs = FALSE; | |
34 | ||
e4ef1b6c | 35 | static bfd_reloc_status_type |
f36e8886 BS |
36 | bfd_elf_avr_diff_reloc (bfd *, arelent *, asymbol *, void *, |
37 | asection *, bfd *, char **); | |
e4ef1b6c | 38 | |
28c9d252 NC |
39 | /* Hash table initialization and handling. Code is taken from the hppa port |
40 | and adapted to the needs of AVR. */ | |
41 | ||
42 | /* We use two hash tables to hold information for linking avr objects. | |
43 | ||
4dfe6ac6 | 44 | The first is the elf32_avr_link_hash_table which is derived from the |
28c9d252 NC |
45 | stanard ELF linker hash table. We use this as a place to attach the other |
46 | hash table and some static information. | |
47 | ||
48 | The second is the stub hash table which is derived from the base BFD | |
49 | hash table. The stub hash table holds the information on the linker | |
50 | stubs. */ | |
51 | ||
52 | struct elf32_avr_stub_hash_entry | |
53 | { | |
54 | /* Base hash table entry structure. */ | |
55 | struct bfd_hash_entry bh_root; | |
56 | ||
57 | /* Offset within stub_sec of the beginning of this stub. */ | |
58 | bfd_vma stub_offset; | |
59 | ||
60 | /* Given the symbol's value and its section we can determine its final | |
61 | value when building the stubs (so the stub knows where to jump). */ | |
62 | bfd_vma target_value; | |
63 | ||
64 | /* This way we could mark stubs to be no longer necessary. */ | |
65 | bfd_boolean is_actually_needed; | |
66 | }; | |
67 | ||
68 | struct elf32_avr_link_hash_table | |
69 | { | |
70 | /* The main hash table. */ | |
71 | struct elf_link_hash_table etab; | |
72 | ||
73 | /* The stub hash table. */ | |
74 | struct bfd_hash_table bstab; | |
75 | ||
76 | bfd_boolean no_stubs; | |
77 | ||
78 | /* Linker stub bfd. */ | |
79 | bfd *stub_bfd; | |
80 | ||
81 | /* The stub section. */ | |
82 | asection *stub_sec; | |
83 | ||
84 | /* Usually 0, unless we are generating code for a bootloader. Will | |
85 | be initialized by elf32_avr_size_stubs to the vma offset of the | |
86 | output section associated with the stub section. */ | |
87 | bfd_vma vector_base; | |
88 | ||
89 | /* Assorted information used by elf32_avr_size_stubs. */ | |
07d6d2b8 AM |
90 | unsigned int bfd_count; |
91 | unsigned int top_index; | |
92 | asection ** input_list; | |
28c9d252 NC |
93 | Elf_Internal_Sym ** all_local_syms; |
94 | ||
95 | /* Tables for mapping vma beyond the 128k boundary to the address of the | |
96 | corresponding stub. (AMT) | |
97 | "amt_max_entry_cnt" reflects the number of entries that memory is allocated | |
98 | for in the "amt_stub_offsets" and "amt_destination_addr" arrays. | |
99 | "amt_entry_cnt" informs how many of these entries actually contain | |
100 | useful data. */ | |
101 | unsigned int amt_entry_cnt; | |
102 | unsigned int amt_max_entry_cnt; | |
103 | bfd_vma * amt_stub_offsets; | |
104 | bfd_vma * amt_destination_addr; | |
105 | }; | |
106 | ||
107 | /* Various hash macros and functions. */ | |
108 | #define avr_link_hash_table(p) \ | |
0f55320b AM |
109 | ((is_elf_hash_table ((p)->hash) \ |
110 | && elf_hash_table_id (elf_hash_table (p)) == AVR_ELF_DATA) \ | |
111 | ? (struct elf32_avr_link_hash_table *) (p)->hash : NULL) | |
28c9d252 NC |
112 | |
113 | #define avr_stub_hash_entry(ent) \ | |
114 | ((struct elf32_avr_stub_hash_entry *)(ent)) | |
115 | ||
116 | #define avr_stub_hash_lookup(table, string, create, copy) \ | |
117 | ((struct elf32_avr_stub_hash_entry *) \ | |
118 | bfd_hash_lookup ((table), (string), (create), (copy))) | |
adde6300 | 119 | |
adde6300 AM |
120 | static reloc_howto_type elf_avr_howto_table[] = |
121 | { | |
122 | HOWTO (R_AVR_NONE, /* type */ | |
123 | 0, /* rightshift */ | |
6346d5ca AM |
124 | 3, /* size (0 = byte, 1 = short, 2 = long) */ |
125 | 0, /* bitsize */ | |
b34976b6 | 126 | FALSE, /* pc_relative */ |
adde6300 | 127 | 0, /* bitpos */ |
6346d5ca | 128 | complain_overflow_dont, /* complain_on_overflow */ |
adde6300 AM |
129 | bfd_elf_generic_reloc, /* special_function */ |
130 | "R_AVR_NONE", /* name */ | |
b34976b6 | 131 | FALSE, /* partial_inplace */ |
adde6300 AM |
132 | 0, /* src_mask */ |
133 | 0, /* dst_mask */ | |
b34976b6 | 134 | FALSE), /* pcrel_offset */ |
adde6300 AM |
135 | |
136 | HOWTO (R_AVR_32, /* type */ | |
137 | 0, /* rightshift */ | |
138 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
139 | 32, /* bitsize */ | |
b34976b6 | 140 | FALSE, /* pc_relative */ |
adde6300 AM |
141 | 0, /* bitpos */ |
142 | complain_overflow_bitfield, /* complain_on_overflow */ | |
143 | bfd_elf_generic_reloc, /* special_function */ | |
144 | "R_AVR_32", /* name */ | |
b34976b6 | 145 | FALSE, /* partial_inplace */ |
adde6300 AM |
146 | 0xffffffff, /* src_mask */ |
147 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 148 | FALSE), /* pcrel_offset */ |
adde6300 AM |
149 | |
150 | /* A 7 bit PC relative relocation. */ | |
151 | HOWTO (R_AVR_7_PCREL, /* type */ | |
152 | 1, /* rightshift */ | |
153 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
154 | 7, /* bitsize */ | |
b34976b6 | 155 | TRUE, /* pc_relative */ |
adde6300 AM |
156 | 3, /* bitpos */ |
157 | complain_overflow_bitfield, /* complain_on_overflow */ | |
158 | bfd_elf_generic_reloc, /* special_function */ | |
159 | "R_AVR_7_PCREL", /* name */ | |
b34976b6 | 160 | FALSE, /* partial_inplace */ |
adde6300 AM |
161 | 0xffff, /* src_mask */ |
162 | 0xffff, /* dst_mask */ | |
b34976b6 | 163 | TRUE), /* pcrel_offset */ |
adde6300 AM |
164 | |
165 | /* A 13 bit PC relative relocation. */ | |
166 | HOWTO (R_AVR_13_PCREL, /* type */ | |
167 | 1, /* rightshift */ | |
168 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
169 | 13, /* bitsize */ | |
b34976b6 | 170 | TRUE, /* pc_relative */ |
adde6300 AM |
171 | 0, /* bitpos */ |
172 | complain_overflow_bitfield, /* complain_on_overflow */ | |
173 | bfd_elf_generic_reloc, /* special_function */ | |
174 | "R_AVR_13_PCREL", /* name */ | |
b34976b6 | 175 | FALSE, /* partial_inplace */ |
adde6300 AM |
176 | 0xfff, /* src_mask */ |
177 | 0xfff, /* dst_mask */ | |
b34976b6 | 178 | TRUE), /* pcrel_offset */ |
adde6300 AM |
179 | |
180 | /* A 16 bit absolute relocation. */ | |
181 | HOWTO (R_AVR_16, /* type */ | |
182 | 0, /* rightshift */ | |
183 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
184 | 16, /* bitsize */ | |
b34976b6 | 185 | FALSE, /* pc_relative */ |
adde6300 AM |
186 | 0, /* bitpos */ |
187 | complain_overflow_dont, /* complain_on_overflow */ | |
188 | bfd_elf_generic_reloc, /* special_function */ | |
189 | "R_AVR_16", /* name */ | |
b34976b6 | 190 | FALSE, /* partial_inplace */ |
adde6300 AM |
191 | 0xffff, /* src_mask */ |
192 | 0xffff, /* dst_mask */ | |
b34976b6 | 193 | FALSE), /* pcrel_offset */ |
adde6300 | 194 | |
28c9d252 NC |
195 | /* A 16 bit absolute relocation for command address |
196 | Will be changed when linker stubs are needed. */ | |
adde6300 AM |
197 | HOWTO (R_AVR_16_PM, /* type */ |
198 | 1, /* rightshift */ | |
199 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
200 | 16, /* bitsize */ | |
b34976b6 | 201 | FALSE, /* pc_relative */ |
adde6300 AM |
202 | 0, /* bitpos */ |
203 | complain_overflow_bitfield, /* complain_on_overflow */ | |
204 | bfd_elf_generic_reloc, /* special_function */ | |
205 | "R_AVR_16_PM", /* name */ | |
b34976b6 | 206 | FALSE, /* partial_inplace */ |
adde6300 AM |
207 | 0xffff, /* src_mask */ |
208 | 0xffff, /* dst_mask */ | |
b34976b6 | 209 | FALSE), /* pcrel_offset */ |
adde6300 AM |
210 | /* A low 8 bit absolute relocation of 16 bit address. |
211 | For LDI command. */ | |
212 | HOWTO (R_AVR_LO8_LDI, /* type */ | |
213 | 0, /* rightshift */ | |
214 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
215 | 8, /* bitsize */ | |
b34976b6 | 216 | FALSE, /* pc_relative */ |
adde6300 AM |
217 | 0, /* bitpos */ |
218 | complain_overflow_dont, /* complain_on_overflow */ | |
219 | bfd_elf_generic_reloc, /* special_function */ | |
220 | "R_AVR_LO8_LDI", /* name */ | |
b34976b6 | 221 | FALSE, /* partial_inplace */ |
adde6300 AM |
222 | 0xffff, /* src_mask */ |
223 | 0xffff, /* dst_mask */ | |
b34976b6 | 224 | FALSE), /* pcrel_offset */ |
adde6300 AM |
225 | /* A high 8 bit absolute relocation of 16 bit address. |
226 | For LDI command. */ | |
227 | HOWTO (R_AVR_HI8_LDI, /* type */ | |
228 | 8, /* rightshift */ | |
229 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
230 | 8, /* bitsize */ | |
b34976b6 | 231 | FALSE, /* pc_relative */ |
adde6300 AM |
232 | 0, /* bitpos */ |
233 | complain_overflow_dont, /* complain_on_overflow */ | |
234 | bfd_elf_generic_reloc, /* special_function */ | |
235 | "R_AVR_HI8_LDI", /* name */ | |
b34976b6 | 236 | FALSE, /* partial_inplace */ |
adde6300 AM |
237 | 0xffff, /* src_mask */ |
238 | 0xffff, /* dst_mask */ | |
b34976b6 | 239 | FALSE), /* pcrel_offset */ |
adde6300 | 240 | /* A high 6 bit absolute relocation of 22 bit address. |
4cdc7696 | 241 | For LDI command. As well second most significant 8 bit value of |
df406460 | 242 | a 32 bit link-time constant. */ |
adde6300 AM |
243 | HOWTO (R_AVR_HH8_LDI, /* type */ |
244 | 16, /* rightshift */ | |
245 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
246 | 8, /* bitsize */ | |
b34976b6 | 247 | FALSE, /* pc_relative */ |
adde6300 AM |
248 | 0, /* bitpos */ |
249 | complain_overflow_dont, /* complain_on_overflow */ | |
250 | bfd_elf_generic_reloc, /* special_function */ | |
251 | "R_AVR_HH8_LDI", /* name */ | |
b34976b6 | 252 | FALSE, /* partial_inplace */ |
adde6300 AM |
253 | 0xffff, /* src_mask */ |
254 | 0xffff, /* dst_mask */ | |
b34976b6 | 255 | FALSE), /* pcrel_offset */ |
adde6300 AM |
256 | /* A negative low 8 bit absolute relocation of 16 bit address. |
257 | For LDI command. */ | |
258 | HOWTO (R_AVR_LO8_LDI_NEG, /* type */ | |
259 | 0, /* rightshift */ | |
260 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
261 | 8, /* bitsize */ | |
b34976b6 | 262 | FALSE, /* pc_relative */ |
adde6300 AM |
263 | 0, /* bitpos */ |
264 | complain_overflow_dont, /* complain_on_overflow */ | |
265 | bfd_elf_generic_reloc, /* special_function */ | |
266 | "R_AVR_LO8_LDI_NEG", /* name */ | |
b34976b6 | 267 | FALSE, /* partial_inplace */ |
adde6300 AM |
268 | 0xffff, /* src_mask */ |
269 | 0xffff, /* dst_mask */ | |
b34976b6 | 270 | FALSE), /* pcrel_offset */ |
df406460 | 271 | /* A negative high 8 bit absolute relocation of 16 bit address. |
adde6300 AM |
272 | For LDI command. */ |
273 | HOWTO (R_AVR_HI8_LDI_NEG, /* type */ | |
274 | 8, /* rightshift */ | |
275 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
276 | 8, /* bitsize */ | |
b34976b6 | 277 | FALSE, /* pc_relative */ |
adde6300 AM |
278 | 0, /* bitpos */ |
279 | complain_overflow_dont, /* complain_on_overflow */ | |
280 | bfd_elf_generic_reloc, /* special_function */ | |
281 | "R_AVR_HI8_LDI_NEG", /* name */ | |
b34976b6 | 282 | FALSE, /* partial_inplace */ |
adde6300 AM |
283 | 0xffff, /* src_mask */ |
284 | 0xffff, /* dst_mask */ | |
b34976b6 | 285 | FALSE), /* pcrel_offset */ |
df406460 | 286 | /* A negative high 6 bit absolute relocation of 22 bit address. |
adde6300 AM |
287 | For LDI command. */ |
288 | HOWTO (R_AVR_HH8_LDI_NEG, /* type */ | |
289 | 16, /* rightshift */ | |
290 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
291 | 8, /* bitsize */ | |
b34976b6 | 292 | FALSE, /* pc_relative */ |
adde6300 AM |
293 | 0, /* bitpos */ |
294 | complain_overflow_dont, /* complain_on_overflow */ | |
295 | bfd_elf_generic_reloc, /* special_function */ | |
296 | "R_AVR_HH8_LDI_NEG", /* name */ | |
b34976b6 | 297 | FALSE, /* partial_inplace */ |
adde6300 AM |
298 | 0xffff, /* src_mask */ |
299 | 0xffff, /* dst_mask */ | |
b34976b6 | 300 | FALSE), /* pcrel_offset */ |
adde6300 | 301 | /* A low 8 bit absolute relocation of 24 bit program memory address. |
28c9d252 | 302 | For LDI command. Will not be changed when linker stubs are needed. */ |
adde6300 AM |
303 | HOWTO (R_AVR_LO8_LDI_PM, /* type */ |
304 | 1, /* rightshift */ | |
305 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
306 | 8, /* bitsize */ | |
b34976b6 | 307 | FALSE, /* pc_relative */ |
adde6300 AM |
308 | 0, /* bitpos */ |
309 | complain_overflow_dont, /* complain_on_overflow */ | |
310 | bfd_elf_generic_reloc, /* special_function */ | |
311 | "R_AVR_LO8_LDI_PM", /* name */ | |
b34976b6 | 312 | FALSE, /* partial_inplace */ |
adde6300 AM |
313 | 0xffff, /* src_mask */ |
314 | 0xffff, /* dst_mask */ | |
b34976b6 | 315 | FALSE), /* pcrel_offset */ |
28c9d252 NC |
316 | /* A low 8 bit absolute relocation of 24 bit program memory address. |
317 | For LDI command. Will not be changed when linker stubs are needed. */ | |
adde6300 AM |
318 | HOWTO (R_AVR_HI8_LDI_PM, /* type */ |
319 | 9, /* rightshift */ | |
320 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
321 | 8, /* bitsize */ | |
b34976b6 | 322 | FALSE, /* pc_relative */ |
adde6300 AM |
323 | 0, /* bitpos */ |
324 | complain_overflow_dont, /* complain_on_overflow */ | |
325 | bfd_elf_generic_reloc, /* special_function */ | |
326 | "R_AVR_HI8_LDI_PM", /* name */ | |
b34976b6 | 327 | FALSE, /* partial_inplace */ |
adde6300 AM |
328 | 0xffff, /* src_mask */ |
329 | 0xffff, /* dst_mask */ | |
b34976b6 | 330 | FALSE), /* pcrel_offset */ |
28c9d252 NC |
331 | /* A low 8 bit absolute relocation of 24 bit program memory address. |
332 | For LDI command. Will not be changed when linker stubs are needed. */ | |
adde6300 AM |
333 | HOWTO (R_AVR_HH8_LDI_PM, /* type */ |
334 | 17, /* rightshift */ | |
335 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
336 | 8, /* bitsize */ | |
b34976b6 | 337 | FALSE, /* pc_relative */ |
adde6300 AM |
338 | 0, /* bitpos */ |
339 | complain_overflow_dont, /* complain_on_overflow */ | |
340 | bfd_elf_generic_reloc, /* special_function */ | |
341 | "R_AVR_HH8_LDI_PM", /* name */ | |
b34976b6 | 342 | FALSE, /* partial_inplace */ |
adde6300 AM |
343 | 0xffff, /* src_mask */ |
344 | 0xffff, /* dst_mask */ | |
b34976b6 | 345 | FALSE), /* pcrel_offset */ |
28c9d252 NC |
346 | /* A low 8 bit absolute relocation of 24 bit program memory address. |
347 | For LDI command. Will not be changed when linker stubs are needed. */ | |
adde6300 AM |
348 | HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */ |
349 | 1, /* rightshift */ | |
350 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
351 | 8, /* bitsize */ | |
b34976b6 | 352 | FALSE, /* pc_relative */ |
adde6300 AM |
353 | 0, /* bitpos */ |
354 | complain_overflow_dont, /* complain_on_overflow */ | |
355 | bfd_elf_generic_reloc, /* special_function */ | |
356 | "R_AVR_LO8_LDI_PM_NEG", /* name */ | |
b34976b6 | 357 | FALSE, /* partial_inplace */ |
adde6300 AM |
358 | 0xffff, /* src_mask */ |
359 | 0xffff, /* dst_mask */ | |
b34976b6 | 360 | FALSE), /* pcrel_offset */ |
28c9d252 NC |
361 | /* A low 8 bit absolute relocation of 24 bit program memory address. |
362 | For LDI command. Will not be changed when linker stubs are needed. */ | |
adde6300 AM |
363 | HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */ |
364 | 9, /* rightshift */ | |
365 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
366 | 8, /* bitsize */ | |
b34976b6 | 367 | FALSE, /* pc_relative */ |
adde6300 AM |
368 | 0, /* bitpos */ |
369 | complain_overflow_dont, /* complain_on_overflow */ | |
370 | bfd_elf_generic_reloc, /* special_function */ | |
371 | "R_AVR_HI8_LDI_PM_NEG", /* name */ | |
b34976b6 | 372 | FALSE, /* partial_inplace */ |
adde6300 AM |
373 | 0xffff, /* src_mask */ |
374 | 0xffff, /* dst_mask */ | |
b34976b6 | 375 | FALSE), /* pcrel_offset */ |
28c9d252 NC |
376 | /* A low 8 bit absolute relocation of 24 bit program memory address. |
377 | For LDI command. Will not be changed when linker stubs are needed. */ | |
adde6300 AM |
378 | HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */ |
379 | 17, /* rightshift */ | |
380 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
381 | 8, /* bitsize */ | |
b34976b6 | 382 | FALSE, /* pc_relative */ |
adde6300 AM |
383 | 0, /* bitpos */ |
384 | complain_overflow_dont, /* complain_on_overflow */ | |
385 | bfd_elf_generic_reloc, /* special_function */ | |
386 | "R_AVR_HH8_LDI_PM_NEG", /* name */ | |
b34976b6 | 387 | FALSE, /* partial_inplace */ |
adde6300 AM |
388 | 0xffff, /* src_mask */ |
389 | 0xffff, /* dst_mask */ | |
b34976b6 | 390 | FALSE), /* pcrel_offset */ |
adde6300 AM |
391 | /* Relocation for CALL command in ATmega. */ |
392 | HOWTO (R_AVR_CALL, /* type */ | |
393 | 1, /* rightshift */ | |
394 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
395 | 23, /* bitsize */ | |
b34976b6 | 396 | FALSE, /* pc_relative */ |
adde6300 | 397 | 0, /* bitpos */ |
750bce0e | 398 | complain_overflow_dont,/* complain_on_overflow */ |
adde6300 AM |
399 | bfd_elf_generic_reloc, /* special_function */ |
400 | "R_AVR_CALL", /* name */ | |
b34976b6 | 401 | FALSE, /* partial_inplace */ |
adde6300 AM |
402 | 0xffffffff, /* src_mask */ |
403 | 0xffffffff, /* dst_mask */ | |
750bce0e NC |
404 | FALSE), /* pcrel_offset */ |
405 | /* A 16 bit absolute relocation of 16 bit address. | |
406 | For LDI command. */ | |
407 | HOWTO (R_AVR_LDI, /* type */ | |
408 | 0, /* rightshift */ | |
409 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
410 | 16, /* bitsize */ | |
411 | FALSE, /* pc_relative */ | |
412 | 0, /* bitpos */ | |
413 | complain_overflow_dont,/* complain_on_overflow */ | |
414 | bfd_elf_generic_reloc, /* special_function */ | |
415 | "R_AVR_LDI", /* name */ | |
416 | FALSE, /* partial_inplace */ | |
417 | 0xffff, /* src_mask */ | |
418 | 0xffff, /* dst_mask */ | |
419 | FALSE), /* pcrel_offset */ | |
420 | /* A 6 bit absolute relocation of 6 bit offset. | |
421 | For ldd/sdd command. */ | |
422 | HOWTO (R_AVR_6, /* type */ | |
423 | 0, /* rightshift */ | |
424 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
425 | 6, /* bitsize */ | |
426 | FALSE, /* pc_relative */ | |
427 | 0, /* bitpos */ | |
428 | complain_overflow_dont,/* complain_on_overflow */ | |
429 | bfd_elf_generic_reloc, /* special_function */ | |
430 | "R_AVR_6", /* name */ | |
431 | FALSE, /* partial_inplace */ | |
432 | 0xffff, /* src_mask */ | |
433 | 0xffff, /* dst_mask */ | |
434 | FALSE), /* pcrel_offset */ | |
435 | /* A 6 bit absolute relocation of 6 bit offset. | |
436 | For sbiw/adiw command. */ | |
437 | HOWTO (R_AVR_6_ADIW, /* type */ | |
438 | 0, /* rightshift */ | |
439 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
440 | 6, /* bitsize */ | |
441 | FALSE, /* pc_relative */ | |
442 | 0, /* bitpos */ | |
443 | complain_overflow_dont,/* complain_on_overflow */ | |
444 | bfd_elf_generic_reloc, /* special_function */ | |
445 | "R_AVR_6_ADIW", /* name */ | |
446 | FALSE, /* partial_inplace */ | |
447 | 0xffff, /* src_mask */ | |
448 | 0xffff, /* dst_mask */ | |
df406460 NC |
449 | FALSE), /* pcrel_offset */ |
450 | /* Most significant 8 bit value of a 32 bit link-time constant. */ | |
451 | HOWTO (R_AVR_MS8_LDI, /* type */ | |
452 | 24, /* rightshift */ | |
453 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
454 | 8, /* bitsize */ | |
455 | FALSE, /* pc_relative */ | |
456 | 0, /* bitpos */ | |
457 | complain_overflow_dont, /* complain_on_overflow */ | |
458 | bfd_elf_generic_reloc, /* special_function */ | |
459 | "R_AVR_MS8_LDI", /* name */ | |
460 | FALSE, /* partial_inplace */ | |
461 | 0xffff, /* src_mask */ | |
462 | 0xffff, /* dst_mask */ | |
463 | FALSE), /* pcrel_offset */ | |
464 | /* Negative most significant 8 bit value of a 32 bit link-time constant. */ | |
465 | HOWTO (R_AVR_MS8_LDI_NEG, /* type */ | |
466 | 24, /* rightshift */ | |
467 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
468 | 8, /* bitsize */ | |
469 | FALSE, /* pc_relative */ | |
470 | 0, /* bitpos */ | |
471 | complain_overflow_dont, /* complain_on_overflow */ | |
472 | bfd_elf_generic_reloc, /* special_function */ | |
473 | "R_AVR_MS8_LDI_NEG", /* name */ | |
474 | FALSE, /* partial_inplace */ | |
475 | 0xffff, /* src_mask */ | |
476 | 0xffff, /* dst_mask */ | |
07d6d2b8 | 477 | FALSE), /* pcrel_offset */ |
28c9d252 | 478 | /* A low 8 bit absolute relocation of 24 bit program memory address. |
17e57237 | 479 | For LDI command. Will be changed when linker stubs are needed. */ |
07d6d2b8 AM |
480 | HOWTO (R_AVR_LO8_LDI_GS, /* type */ |
481 | 1, /* rightshift */ | |
482 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
483 | 8, /* bitsize */ | |
484 | FALSE, /* pc_relative */ | |
485 | 0, /* bitpos */ | |
486 | complain_overflow_dont, /* complain_on_overflow */ | |
487 | bfd_elf_generic_reloc, /* special_function */ | |
488 | "R_AVR_LO8_LDI_GS", /* name */ | |
489 | FALSE, /* partial_inplace */ | |
490 | 0xffff, /* src_mask */ | |
491 | 0xffff, /* dst_mask */ | |
492 | FALSE), /* pcrel_offset */ | |
28c9d252 | 493 | /* A low 8 bit absolute relocation of 24 bit program memory address. |
17e57237 | 494 | For LDI command. Will be changed when linker stubs are needed. */ |
07d6d2b8 AM |
495 | HOWTO (R_AVR_HI8_LDI_GS, /* type */ |
496 | 9, /* rightshift */ | |
497 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
498 | 8, /* bitsize */ | |
499 | FALSE, /* pc_relative */ | |
500 | 0, /* bitpos */ | |
501 | complain_overflow_dont, /* complain_on_overflow */ | |
502 | bfd_elf_generic_reloc, /* special_function */ | |
503 | "R_AVR_HI8_LDI_GS", /* name */ | |
504 | FALSE, /* partial_inplace */ | |
505 | 0xffff, /* src_mask */ | |
506 | 0xffff, /* dst_mask */ | |
507 | FALSE), /* pcrel_offset */ | |
17e57237 NC |
508 | /* 8 bit offset. */ |
509 | HOWTO (R_AVR_8, /* type */ | |
510 | 0, /* rightshift */ | |
511 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
512 | 8, /* bitsize */ | |
513 | FALSE, /* pc_relative */ | |
514 | 0, /* bitpos */ | |
515 | complain_overflow_bitfield,/* complain_on_overflow */ | |
516 | bfd_elf_generic_reloc, /* special_function */ | |
517 | "R_AVR_8", /* name */ | |
518 | FALSE, /* partial_inplace */ | |
519 | 0x000000ff, /* src_mask */ | |
520 | 0x000000ff, /* dst_mask */ | |
521 | FALSE), /* pcrel_offset */ | |
99700d6f NC |
522 | /* lo8-part to use in .byte lo8(sym). */ |
523 | HOWTO (R_AVR_8_LO8, /* type */ | |
524 | 0, /* rightshift */ | |
525 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
526 | 8, /* bitsize */ | |
527 | FALSE, /* pc_relative */ | |
528 | 0, /* bitpos */ | |
529 | complain_overflow_dont,/* complain_on_overflow */ | |
530 | bfd_elf_generic_reloc, /* special_function */ | |
531 | "R_AVR_8_LO8", /* name */ | |
532 | FALSE, /* partial_inplace */ | |
533 | 0xffffff, /* src_mask */ | |
534 | 0xffffff, /* dst_mask */ | |
535 | FALSE), /* pcrel_offset */ | |
536 | /* hi8-part to use in .byte hi8(sym). */ | |
537 | HOWTO (R_AVR_8_HI8, /* type */ | |
538 | 8, /* rightshift */ | |
539 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
540 | 8, /* bitsize */ | |
541 | FALSE, /* pc_relative */ | |
542 | 0, /* bitpos */ | |
543 | complain_overflow_dont,/* complain_on_overflow */ | |
544 | bfd_elf_generic_reloc, /* special_function */ | |
545 | "R_AVR_8_HI8", /* name */ | |
546 | FALSE, /* partial_inplace */ | |
547 | 0xffffff, /* src_mask */ | |
548 | 0xffffff, /* dst_mask */ | |
549 | FALSE), /* pcrel_offset */ | |
40551fb8 NC |
550 | /* hlo8-part to use in .byte hlo8(sym). */ |
551 | HOWTO (R_AVR_8_HLO8, /* type */ | |
99700d6f NC |
552 | 16, /* rightshift */ |
553 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
554 | 8, /* bitsize */ | |
555 | FALSE, /* pc_relative */ | |
556 | 0, /* bitpos */ | |
557 | complain_overflow_dont,/* complain_on_overflow */ | |
558 | bfd_elf_generic_reloc, /* special_function */ | |
40551fb8 | 559 | "R_AVR_8_HLO8", /* name */ |
99700d6f NC |
560 | FALSE, /* partial_inplace */ |
561 | 0xffffff, /* src_mask */ | |
562 | 0xffffff, /* dst_mask */ | |
563 | FALSE), /* pcrel_offset */ | |
f36e8886 | 564 | HOWTO (R_AVR_DIFF8, /* type */ |
07d6d2b8 AM |
565 | 0, /* rightshift */ |
566 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
567 | 8, /* bitsize */ | |
568 | FALSE, /* pc_relative */ | |
569 | 0, /* bitpos */ | |
f36e8886 BS |
570 | complain_overflow_bitfield, /* complain_on_overflow */ |
571 | bfd_elf_avr_diff_reloc, /* special_function */ | |
07d6d2b8 AM |
572 | "R_AVR_DIFF8", /* name */ |
573 | FALSE, /* partial_inplace */ | |
574 | 0, /* src_mask */ | |
575 | 0xff, /* dst_mask */ | |
576 | FALSE), /* pcrel_offset */ | |
577 | HOWTO (R_AVR_DIFF16, /* type */ | |
578 | 0, /* rightshift */ | |
f36e8886 BS |
579 | 1, /* size (0 = byte, 1 = short, 2 = long) */ |
580 | 16, /* bitsize */ | |
07d6d2b8 AM |
581 | FALSE, /* pc_relative */ |
582 | 0, /* bitpos */ | |
f36e8886 BS |
583 | complain_overflow_bitfield, /* complain_on_overflow */ |
584 | bfd_elf_avr_diff_reloc,/* special_function */ | |
07d6d2b8 AM |
585 | "R_AVR_DIFF16", /* name */ |
586 | FALSE, /* partial_inplace */ | |
587 | 0, /* src_mask */ | |
588 | 0xffff, /* dst_mask */ | |
589 | FALSE), /* pcrel_offset */ | |
590 | HOWTO (R_AVR_DIFF32, /* type */ | |
591 | 0, /* rightshift */ | |
592 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
593 | 32, /* bitsize */ | |
594 | FALSE, /* pc_relative */ | |
595 | 0, /* bitpos */ | |
f36e8886 BS |
596 | complain_overflow_bitfield, /* complain_on_overflow */ |
597 | bfd_elf_avr_diff_reloc,/* special_function */ | |
07d6d2b8 AM |
598 | "R_AVR_DIFF32", /* name */ |
599 | FALSE, /* partial_inplace */ | |
600 | 0, /* src_mask */ | |
601 | 0xffffffff, /* dst_mask */ | |
602 | FALSE), /* pcrel_offset */ | |
f36e8886 BS |
603 | /* 7 bit immediate for LDS/STS in Tiny core. */ |
604 | HOWTO (R_AVR_LDS_STS_16, /* type */ | |
07d6d2b8 AM |
605 | 0, /* rightshift */ |
606 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
607 | 7, /* bitsize */ | |
608 | FALSE, /* pc_relative */ | |
609 | 0, /* bitpos */ | |
f36e8886 BS |
610 | complain_overflow_dont,/* complain_on_overflow */ |
611 | bfd_elf_generic_reloc, /* special_function */ | |
07d6d2b8 AM |
612 | "R_AVR_LDS_STS_16", /* name */ |
613 | FALSE, /* partial_inplace */ | |
614 | 0xffff, /* src_mask */ | |
615 | 0xffff, /* dst_mask */ | |
75f58085 BS |
616 | FALSE), /* pcrel_offset */ |
617 | ||
618 | HOWTO (R_AVR_PORT6, /* type */ | |
619 | 0, /* rightshift */ | |
620 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
621 | 6, /* bitsize */ | |
622 | FALSE, /* pc_relative */ | |
623 | 0, /* bitpos */ | |
624 | complain_overflow_dont,/* complain_on_overflow */ | |
625 | bfd_elf_generic_reloc, /* special_function */ | |
626 | "R_AVR_PORT6", /* name */ | |
627 | FALSE, /* partial_inplace */ | |
628 | 0xffffff, /* src_mask */ | |
629 | 0xffffff, /* dst_mask */ | |
630 | FALSE), /* pcrel_offset */ | |
631 | HOWTO (R_AVR_PORT5, /* type */ | |
632 | 0, /* rightshift */ | |
633 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
634 | 5, /* bitsize */ | |
635 | FALSE, /* pc_relative */ | |
636 | 0, /* bitpos */ | |
637 | complain_overflow_dont,/* complain_on_overflow */ | |
638 | bfd_elf_generic_reloc, /* special_function */ | |
639 | "R_AVR_PORT5", /* name */ | |
640 | FALSE, /* partial_inplace */ | |
641 | 0xffffff, /* src_mask */ | |
642 | 0xffffff, /* dst_mask */ | |
07d6d2b8 | 643 | FALSE), /* pcrel_offset */ |
328e7bfd DC |
644 | |
645 | /* A 32 bit PC relative relocation. */ | |
646 | HOWTO (R_AVR_32_PCREL, /* type */ | |
07d6d2b8 AM |
647 | 0, /* rightshift */ |
648 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
328e7bfd DC |
649 | 32, /* bitsize */ |
650 | TRUE, /* pc_relative */ | |
07d6d2b8 | 651 | 0, /* bitpos */ |
328e7bfd DC |
652 | complain_overflow_bitfield, /* complain_on_overflow */ |
653 | bfd_elf_generic_reloc, /* special_function */ | |
654 | "R_AVR_32_PCREL", /* name */ | |
655 | FALSE, /* partial_inplace */ | |
07d6d2b8 AM |
656 | 0xffffffff, /* src_mask */ |
657 | 0xffffffff, /* dst_mask */ | |
328e7bfd | 658 | TRUE), /* pcrel_offset */ |
adde6300 AM |
659 | }; |
660 | ||
661 | /* Map BFD reloc types to AVR ELF reloc types. */ | |
662 | ||
663 | struct avr_reloc_map | |
664 | { | |
665 | bfd_reloc_code_real_type bfd_reloc_val; | |
666 | unsigned int elf_reloc_val; | |
667 | }; | |
668 | ||
28c9d252 | 669 | static const struct avr_reloc_map avr_reloc_map[] = |
adde6300 | 670 | { |
07d6d2b8 AM |
671 | { BFD_RELOC_NONE, R_AVR_NONE }, |
672 | { BFD_RELOC_32, R_AVR_32 }, | |
673 | { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL }, | |
674 | { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL }, | |
675 | { BFD_RELOC_16, R_AVR_16 }, | |
676 | { BFD_RELOC_AVR_16_PM, R_AVR_16_PM }, | |
677 | { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI}, | |
678 | { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI }, | |
679 | { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI }, | |
680 | { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI }, | |
681 | { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG }, | |
682 | { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG }, | |
683 | { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG }, | |
684 | { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG }, | |
685 | { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM }, | |
686 | { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS }, | |
687 | { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM }, | |
688 | { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS }, | |
689 | { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM }, | |
adde6300 AM |
690 | { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG }, |
691 | { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG }, | |
692 | { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG }, | |
07d6d2b8 AM |
693 | { BFD_RELOC_AVR_CALL, R_AVR_CALL }, |
694 | { BFD_RELOC_AVR_LDI, R_AVR_LDI }, | |
695 | { BFD_RELOC_AVR_6, R_AVR_6 }, | |
696 | { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW }, | |
697 | { BFD_RELOC_8, R_AVR_8 }, | |
698 | { BFD_RELOC_AVR_8_LO, R_AVR_8_LO8 }, | |
699 | { BFD_RELOC_AVR_8_HI, R_AVR_8_HI8 }, | |
700 | { BFD_RELOC_AVR_8_HLO, R_AVR_8_HLO8 }, | |
701 | { BFD_RELOC_AVR_DIFF8, R_AVR_DIFF8 }, | |
702 | { BFD_RELOC_AVR_DIFF16, R_AVR_DIFF16 }, | |
703 | { BFD_RELOC_AVR_DIFF32, R_AVR_DIFF32 }, | |
704 | { BFD_RELOC_AVR_LDS_STS_16, R_AVR_LDS_STS_16}, | |
705 | { BFD_RELOC_AVR_PORT6, R_AVR_PORT6}, | |
706 | { BFD_RELOC_AVR_PORT5, R_AVR_PORT5}, | |
707 | { BFD_RELOC_32_PCREL, R_AVR_32_PCREL} | |
adde6300 AM |
708 | }; |
709 | ||
deee88e9 AM |
710 | static const struct bfd_elf_special_section elf_avr_special_sections[] = |
711 | { | |
712 | { STRING_COMMA_LEN (".noinit"), 0, SHT_NOBITS, SHF_ALLOC + SHF_WRITE }, | |
713 | { NULL, 0, 0, 0, 0 } | |
714 | }; | |
715 | ||
df406460 | 716 | /* Meant to be filled one day with the wrap around address for the |
4cdc7696 | 717 | specific device. I.e. should get the value 0x4000 for 16k devices, |
df406460 | 718 | 0x8000 for 32k devices and so on. |
4cdc7696 | 719 | |
df406460 | 720 | We initialize it here with a value of 0x1000000 resulting in |
4cdc7696 NC |
721 | that we will never suggest a wrap-around jump during relaxation. |
722 | The logic of the source code later on assumes that in | |
df406460 | 723 | avr_pc_wrap_around one single bit is set. */ |
28c9d252 NC |
724 | static bfd_vma avr_pc_wrap_around = 0x10000000; |
725 | ||
726 | /* If this variable holds a value different from zero, the linker relaxation | |
727 | machine will try to optimize call/ret sequences by a single jump | |
728 | instruction. This option could be switched off by a linker switch. */ | |
729 | static int avr_replace_call_ret_sequences = 1; | |
730 | \f | |
bac13f5a AB |
731 | |
732 | /* Per-section relaxation related information for avr. */ | |
733 | ||
734 | struct avr_relax_info | |
735 | { | |
736 | /* Track the avr property records that apply to this section. */ | |
737 | ||
738 | struct | |
739 | { | |
740 | /* Number of records in the list. */ | |
741 | unsigned count; | |
742 | ||
743 | /* How many records worth of space have we allocated. */ | |
744 | unsigned allocated; | |
745 | ||
746 | /* The records, only COUNT records are initialised. */ | |
747 | struct avr_property_record *items; | |
748 | } records; | |
749 | }; | |
750 | ||
751 | /* Per section data, specialised for avr. */ | |
752 | ||
753 | struct elf_avr_section_data | |
754 | { | |
755 | /* The standard data must appear first. */ | |
756 | struct bfd_elf_section_data elf; | |
757 | ||
758 | /* Relaxation related information. */ | |
759 | struct avr_relax_info relax_info; | |
760 | }; | |
761 | ||
762 | /* Possibly initialise avr specific data for new section SEC from ABFD. */ | |
763 | ||
764 | static bfd_boolean | |
765 | elf_avr_new_section_hook (bfd *abfd, asection *sec) | |
766 | { | |
767 | if (!sec->used_by_bfd) | |
768 | { | |
769 | struct elf_avr_section_data *sdata; | |
986f0783 | 770 | size_t amt = sizeof (*sdata); |
bac13f5a AB |
771 | |
772 | sdata = bfd_zalloc (abfd, amt); | |
773 | if (sdata == NULL) | |
774 | return FALSE; | |
775 | sec->used_by_bfd = sdata; | |
776 | } | |
777 | ||
778 | return _bfd_elf_new_section_hook (abfd, sec); | |
779 | } | |
780 | ||
781 | /* Return a pointer to the relaxation information for SEC. */ | |
782 | ||
783 | static struct avr_relax_info * | |
784 | get_avr_relax_info (asection *sec) | |
785 | { | |
786 | struct elf_avr_section_data *section_data; | |
787 | ||
788 | /* No info available if no section or if it is an output section. */ | |
789 | if (!sec || sec == sec->output_section) | |
790 | return NULL; | |
791 | ||
792 | section_data = (struct elf_avr_section_data *) elf_section_data (sec); | |
793 | return §ion_data->relax_info; | |
794 | } | |
795 | ||
796 | /* Initialise the per section relaxation information for SEC. */ | |
797 | ||
798 | static void | |
799 | init_avr_relax_info (asection *sec) | |
800 | { | |
801 | struct avr_relax_info *relax_info = get_avr_relax_info (sec); | |
802 | ||
803 | relax_info->records.count = 0; | |
804 | relax_info->records.allocated = 0; | |
805 | relax_info->records.items = NULL; | |
806 | } | |
807 | ||
28c9d252 NC |
808 | /* Initialize an entry in the stub hash table. */ |
809 | ||
810 | static struct bfd_hash_entry * | |
811 | stub_hash_newfunc (struct bfd_hash_entry *entry, | |
07d6d2b8 AM |
812 | struct bfd_hash_table *table, |
813 | const char *string) | |
28c9d252 NC |
814 | { |
815 | /* Allocate the structure if it has not already been allocated by a | |
816 | subclass. */ | |
817 | if (entry == NULL) | |
818 | { | |
819 | entry = bfd_hash_allocate (table, | |
07d6d2b8 | 820 | sizeof (struct elf32_avr_stub_hash_entry)); |
28c9d252 | 821 | if (entry == NULL) |
07d6d2b8 | 822 | return entry; |
28c9d252 NC |
823 | } |
824 | ||
825 | /* Call the allocation method of the superclass. */ | |
826 | entry = bfd_hash_newfunc (entry, table, string); | |
827 | if (entry != NULL) | |
828 | { | |
829 | struct elf32_avr_stub_hash_entry *hsh; | |
830 | ||
831 | /* Initialize the local fields. */ | |
832 | hsh = avr_stub_hash_entry (entry); | |
833 | hsh->stub_offset = 0; | |
834 | hsh->target_value = 0; | |
835 | } | |
836 | ||
837 | return entry; | |
838 | } | |
839 | ||
64ee10b6 NC |
840 | /* This function is just a straight passthrough to the real |
841 | function in linker.c. Its prupose is so that its address | |
842 | can be compared inside the avr_link_hash_table macro. */ | |
843 | ||
844 | static struct bfd_hash_entry * | |
845 | elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry, | |
846 | struct bfd_hash_table * table, | |
847 | const char * string) | |
848 | { | |
849 | return _bfd_elf_link_hash_newfunc (entry, table, string); | |
850 | } | |
851 | ||
68faa637 AM |
852 | /* Free the derived linker hash table. */ |
853 | ||
854 | static void | |
d495ab0d | 855 | elf32_avr_link_hash_table_free (bfd *obfd) |
68faa637 AM |
856 | { |
857 | struct elf32_avr_link_hash_table *htab | |
d495ab0d | 858 | = (struct elf32_avr_link_hash_table *) obfd->link.hash; |
68faa637 AM |
859 | |
860 | /* Free the address mapping table. */ | |
c9594989 AM |
861 | free (htab->amt_stub_offsets); |
862 | free (htab->amt_destination_addr); | |
68faa637 AM |
863 | |
864 | bfd_hash_table_free (&htab->bstab); | |
d495ab0d | 865 | _bfd_elf_link_hash_table_free (obfd); |
68faa637 AM |
866 | } |
867 | ||
28c9d252 NC |
868 | /* Create the derived linker hash table. The AVR ELF port uses the derived |
869 | hash table to keep information specific to the AVR ELF linker (without | |
870 | using static variables). */ | |
871 | ||
872 | static struct bfd_link_hash_table * | |
873 | elf32_avr_link_hash_table_create (bfd *abfd) | |
874 | { | |
875 | struct elf32_avr_link_hash_table *htab; | |
986f0783 | 876 | size_t amt = sizeof (*htab); |
28c9d252 | 877 | |
7bf52ea2 | 878 | htab = bfd_zmalloc (amt); |
28c9d252 NC |
879 | if (htab == NULL) |
880 | return NULL; | |
881 | ||
882 | if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd, | |
07d6d2b8 AM |
883 | elf32_avr_link_hash_newfunc, |
884 | sizeof (struct elf_link_hash_entry), | |
4dfe6ac6 | 885 | AVR_ELF_DATA)) |
28c9d252 NC |
886 | { |
887 | free (htab); | |
888 | return NULL; | |
889 | } | |
890 | ||
891 | /* Init the stub hash table too. */ | |
892 | if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc, | |
07d6d2b8 | 893 | sizeof (struct elf32_avr_stub_hash_entry))) |
d495ab0d AM |
894 | { |
895 | _bfd_elf_link_hash_table_free (abfd); | |
896 | return NULL; | |
897 | } | |
898 | htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free; | |
4cdc7696 | 899 | |
28c9d252 NC |
900 | return &htab->etab.root; |
901 | } | |
902 | ||
df406460 | 903 | /* Calculates the effective distance of a pc relative jump/call. */ |
73160847 | 904 | |
df406460 NC |
905 | static int |
906 | avr_relative_distance_considering_wrap_around (unsigned int distance) | |
4cdc7696 | 907 | { |
df406460 | 908 | unsigned int wrap_around_mask = avr_pc_wrap_around - 1; |
df406460 NC |
909 | int dist_with_wrap_around = distance & wrap_around_mask; |
910 | ||
338ba755 | 911 | if (dist_with_wrap_around >= ((int) (avr_pc_wrap_around >> 1))) |
df406460 NC |
912 | dist_with_wrap_around -= avr_pc_wrap_around; |
913 | ||
914 | return dist_with_wrap_around; | |
915 | } | |
916 | ||
917 | ||
adde6300 | 918 | static reloc_howto_type * |
4cdc7696 NC |
919 | bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
920 | bfd_reloc_code_real_type code) | |
adde6300 AM |
921 | { |
922 | unsigned int i; | |
923 | ||
924 | for (i = 0; | |
925 | i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map); | |
926 | i++) | |
73160847 NC |
927 | if (avr_reloc_map[i].bfd_reloc_val == code) |
928 | return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val]; | |
adde6300 AM |
929 | |
930 | return NULL; | |
931 | } | |
932 | ||
157090f7 AM |
933 | static reloc_howto_type * |
934 | bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, | |
935 | const char *r_name) | |
936 | { | |
937 | unsigned int i; | |
938 | ||
939 | for (i = 0; | |
940 | i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]); | |
941 | i++) | |
942 | if (elf_avr_howto_table[i].name != NULL | |
943 | && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0) | |
944 | return &elf_avr_howto_table[i]; | |
945 | ||
946 | return NULL; | |
947 | } | |
948 | ||
adde6300 AM |
949 | /* Set the howto pointer for an AVR ELF reloc. */ |
950 | ||
f3185997 | 951 | static bfd_boolean |
0aa13fee | 952 | avr_info_to_howto_rela (bfd *abfd, |
4cdc7696 NC |
953 | arelent *cache_ptr, |
954 | Elf_Internal_Rela *dst) | |
adde6300 AM |
955 | { |
956 | unsigned int r_type; | |
957 | ||
958 | r_type = ELF32_R_TYPE (dst->r_info); | |
5860e3f8 NC |
959 | if (r_type >= (unsigned int) R_AVR_max) |
960 | { | |
695344c0 | 961 | /* xgettext:c-format */ |
0aa13fee AM |
962 | _bfd_error_handler (_("%pB: unsupported relocation type %#x"), |
963 | abfd, r_type); | |
f3185997 NC |
964 | bfd_set_error (bfd_error_bad_value); |
965 | return FALSE; | |
5860e3f8 | 966 | } |
adde6300 | 967 | cache_ptr->howto = &elf_avr_howto_table[r_type]; |
f3185997 | 968 | return TRUE; |
adde6300 AM |
969 | } |
970 | ||
28c9d252 NC |
971 | static bfd_boolean |
972 | avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation) | |
973 | { | |
974 | return (relocation >= 0x020000); | |
975 | } | |
976 | ||
977 | /* Returns the address of the corresponding stub if there is one. | |
978 | Returns otherwise an address above 0x020000. This function | |
979 | could also be used, if there is no knowledge on the section where | |
980 | the destination is found. */ | |
981 | ||
982 | static bfd_vma | |
983 | avr_get_stub_addr (bfd_vma srel, | |
07d6d2b8 | 984 | struct elf32_avr_link_hash_table *htab) |
28c9d252 | 985 | { |
91d6fa6a | 986 | unsigned int sindex; |
28c9d252 | 987 | bfd_vma stub_sec_addr = |
07d6d2b8 | 988 | (htab->stub_sec->output_section->vma + |
28c9d252 NC |
989 | htab->stub_sec->output_offset); |
990 | ||
91d6fa6a NC |
991 | for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++) |
992 | if (htab->amt_destination_addr[sindex] == srel) | |
993 | return htab->amt_stub_offsets[sindex] + stub_sec_addr; | |
28c9d252 NC |
994 | |
995 | /* Return an address that could not be reached by 16 bit relocs. */ | |
996 | return 0x020000; | |
997 | } | |
998 | ||
e4ef1b6c DC |
999 | /* Perform a diff relocation. Nothing to do, as the difference value is already |
1000 | written into the section's contents. */ | |
1001 | ||
1002 | static bfd_reloc_status_type | |
1003 | bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED, | |
1004 | arelent *reloc_entry ATTRIBUTE_UNUSED, | |
07d6d2b8 AM |
1005 | asymbol *symbol ATTRIBUTE_UNUSED, |
1006 | void *data ATTRIBUTE_UNUSED, | |
1007 | asection *input_section ATTRIBUTE_UNUSED, | |
1008 | bfd *output_bfd ATTRIBUTE_UNUSED, | |
1009 | char **error_message ATTRIBUTE_UNUSED) | |
e4ef1b6c DC |
1010 | { |
1011 | return bfd_reloc_ok; | |
1012 | } | |
1013 | ||
1014 | ||
adde6300 AM |
1015 | /* Perform a single relocation. By default we use the standard BFD |
1016 | routines, but a few relocs, we have to do them ourselves. */ | |
1017 | ||
1018 | static bfd_reloc_status_type | |
07d6d2b8 AM |
1019 | avr_final_link_relocate (reloc_howto_type * howto, |
1020 | bfd * input_bfd, | |
1021 | asection * input_section, | |
1022 | bfd_byte * contents, | |
1023 | Elf_Internal_Rela * rel, | |
1024 | bfd_vma relocation, | |
1025 | struct elf32_avr_link_hash_table * htab) | |
adde6300 AM |
1026 | { |
1027 | bfd_reloc_status_type r = bfd_reloc_ok; | |
07d6d2b8 | 1028 | bfd_vma x; |
adde6300 | 1029 | bfd_signed_vma srel; |
28c9d252 | 1030 | bfd_signed_vma reloc_addr; |
07d6d2b8 | 1031 | bfd_boolean use_stubs = FALSE; |
28c9d252 | 1032 | /* Usually is 0, unless we are generating code for a bootloader. */ |
07d6d2b8 | 1033 | bfd_signed_vma base_addr = htab->vector_base; |
28c9d252 NC |
1034 | |
1035 | /* Absolute addr of the reloc in the final excecutable. */ | |
1036 | reloc_addr = rel->r_offset + input_section->output_section->vma | |
1037 | + input_section->output_offset; | |
adde6300 AM |
1038 | |
1039 | switch (howto->type) | |
1040 | { | |
1041 | case R_AVR_7_PCREL: | |
1042 | contents += rel->r_offset; | |
1043 | srel = (bfd_signed_vma) relocation; | |
1044 | srel += rel->r_addend; | |
1045 | srel -= rel->r_offset; | |
a7c10850 | 1046 | srel -= 2; /* Branch instructions add 2 to the PC... */ |
adde6300 AM |
1047 | srel -= (input_section->output_section->vma + |
1048 | input_section->output_offset); | |
1049 | ||
1050 | if (srel & 1) | |
1051 | return bfd_reloc_outofrange; | |
1052 | if (srel > ((1 << 7) - 1) || (srel < - (1 << 7))) | |
1053 | return bfd_reloc_overflow; | |
1054 | x = bfd_get_16 (input_bfd, contents); | |
8c51f2f2 | 1055 | x = (x & 0xfc07) | (((srel >> 1) * 8) & 0x3f8); |
adde6300 AM |
1056 | bfd_put_16 (input_bfd, x, contents); |
1057 | break; | |
1058 | ||
1059 | case R_AVR_13_PCREL: | |
1060 | contents += rel->r_offset; | |
1061 | srel = (bfd_signed_vma) relocation; | |
1062 | srel += rel->r_addend; | |
1063 | srel -= rel->r_offset; | |
a7c10850 | 1064 | srel -= 2; /* Branch instructions add 2 to the PC... */ |
adde6300 AM |
1065 | srel -= (input_section->output_section->vma + |
1066 | input_section->output_offset); | |
1067 | ||
1068 | if (srel & 1) | |
1069 | return bfd_reloc_outofrange; | |
1070 | ||
df406460 NC |
1071 | srel = avr_relative_distance_considering_wrap_around (srel); |
1072 | ||
adde6300 AM |
1073 | /* AVR addresses commands as words. */ |
1074 | srel >>= 1; | |
1075 | ||
1076 | /* Check for overflow. */ | |
1077 | if (srel < -2048 || srel > 2047) | |
1078 | { | |
07d6d2b8 | 1079 | /* Relative distance is too large. */ |
df406460 | 1080 | |
654c3c9f | 1081 | /* Always apply WRAPAROUND for avr2, avr25, and avr4. */ |
65aa24b6 | 1082 | switch (bfd_get_mach (input_bfd)) |
adde6300 | 1083 | { |
65aa24b6 | 1084 | case bfd_mach_avr2: |
654c3c9f | 1085 | case bfd_mach_avr25: |
65aa24b6 NC |
1086 | case bfd_mach_avr4: |
1087 | break; | |
1088 | ||
1089 | default: | |
1090 | return bfd_reloc_overflow; | |
adde6300 | 1091 | } |
adde6300 AM |
1092 | } |
1093 | ||
1094 | x = bfd_get_16 (input_bfd, contents); | |
1095 | x = (x & 0xf000) | (srel & 0xfff); | |
1096 | bfd_put_16 (input_bfd, x, contents); | |
1097 | break; | |
1098 | ||
1099 | case R_AVR_LO8_LDI: | |
1100 | contents += rel->r_offset; | |
1101 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1102 | x = bfd_get_16 (input_bfd, contents); | |
1103 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1104 | bfd_put_16 (input_bfd, x, contents); | |
1105 | break; | |
1106 | ||
750bce0e NC |
1107 | case R_AVR_LDI: |
1108 | contents += rel->r_offset; | |
1109 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
4cdc7696 NC |
1110 | if (((srel > 0) && (srel & 0xffff) > 255) |
1111 | || ((srel < 0) && ((-srel) & 0xffff) > 128)) | |
07d6d2b8 AM |
1112 | /* Remove offset for data/eeprom section. */ |
1113 | return bfd_reloc_overflow; | |
df406460 | 1114 | |
750bce0e NC |
1115 | x = bfd_get_16 (input_bfd, contents); |
1116 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1117 | bfd_put_16 (input_bfd, x, contents); | |
1118 | break; | |
1119 | ||
1120 | case R_AVR_6: | |
1121 | contents += rel->r_offset; | |
1122 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1123 | if (((srel & 0xffff) > 63) || (srel < 0)) | |
1124 | /* Remove offset for data/eeprom section. */ | |
1125 | return bfd_reloc_overflow; | |
1126 | x = bfd_get_16 (input_bfd, contents); | |
4cdc7696 | 1127 | x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7) |
07d6d2b8 | 1128 | | ((srel & (1 << 5)) << 8)); |
750bce0e NC |
1129 | bfd_put_16 (input_bfd, x, contents); |
1130 | break; | |
1131 | ||
1132 | case R_AVR_6_ADIW: | |
1133 | contents += rel->r_offset; | |
1134 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1135 | if (((srel & 0xffff) > 63) || (srel < 0)) | |
1136 | /* Remove offset for data/eeprom section. */ | |
1137 | return bfd_reloc_overflow; | |
1138 | x = bfd_get_16 (input_bfd, contents); | |
4cdc7696 | 1139 | x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2); |
750bce0e NC |
1140 | bfd_put_16 (input_bfd, x, contents); |
1141 | break; | |
1142 | ||
adde6300 AM |
1143 | case R_AVR_HI8_LDI: |
1144 | contents += rel->r_offset; | |
1145 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1146 | srel = (srel >> 8) & 0xff; | |
1147 | x = bfd_get_16 (input_bfd, contents); | |
1148 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1149 | bfd_put_16 (input_bfd, x, contents); | |
1150 | break; | |
1151 | ||
1152 | case R_AVR_HH8_LDI: | |
1153 | contents += rel->r_offset; | |
1154 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1155 | srel = (srel >> 16) & 0xff; | |
1156 | x = bfd_get_16 (input_bfd, contents); | |
1157 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1158 | bfd_put_16 (input_bfd, x, contents); | |
1159 | break; | |
1160 | ||
df406460 NC |
1161 | case R_AVR_MS8_LDI: |
1162 | contents += rel->r_offset; | |
1163 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1164 | srel = (srel >> 24) & 0xff; | |
1165 | x = bfd_get_16 (input_bfd, contents); | |
1166 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1167 | bfd_put_16 (input_bfd, x, contents); | |
1168 | break; | |
1169 | ||
adde6300 AM |
1170 | case R_AVR_LO8_LDI_NEG: |
1171 | contents += rel->r_offset; | |
1172 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1173 | srel = -srel; | |
1174 | x = bfd_get_16 (input_bfd, contents); | |
1175 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1176 | bfd_put_16 (input_bfd, x, contents); | |
1177 | break; | |
1178 | ||
1179 | case R_AVR_HI8_LDI_NEG: | |
1180 | contents += rel->r_offset; | |
1181 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1182 | srel = -srel; | |
1183 | srel = (srel >> 8) & 0xff; | |
1184 | x = bfd_get_16 (input_bfd, contents); | |
1185 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1186 | bfd_put_16 (input_bfd, x, contents); | |
1187 | break; | |
1188 | ||
1189 | case R_AVR_HH8_LDI_NEG: | |
1190 | contents += rel->r_offset; | |
1191 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1192 | srel = -srel; | |
1193 | srel = (srel >> 16) & 0xff; | |
1194 | x = bfd_get_16 (input_bfd, contents); | |
1195 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1196 | bfd_put_16 (input_bfd, x, contents); | |
1197 | break; | |
1198 | ||
df406460 NC |
1199 | case R_AVR_MS8_LDI_NEG: |
1200 | contents += rel->r_offset; | |
1201 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1202 | srel = -srel; | |
1203 | srel = (srel >> 24) & 0xff; | |
1204 | x = bfd_get_16 (input_bfd, contents); | |
1205 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1206 | bfd_put_16 (input_bfd, x, contents); | |
1207 | break; | |
1208 | ||
28c9d252 NC |
1209 | case R_AVR_LO8_LDI_GS: |
1210 | use_stubs = (!htab->no_stubs); | |
1211 | /* Fall through. */ | |
adde6300 AM |
1212 | case R_AVR_LO8_LDI_PM: |
1213 | contents += rel->r_offset; | |
1214 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
28c9d252 NC |
1215 | |
1216 | if (use_stubs | |
07d6d2b8 AM |
1217 | && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) |
1218 | { | |
1219 | bfd_vma old_srel = srel; | |
1220 | ||
1221 | /* We need to use the address of the stub instead. */ | |
1222 | srel = avr_get_stub_addr (srel, htab); | |
1223 | if (debug_stubs) | |
1224 | printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " | |
1225 | "reloc at address 0x%x.\n", | |
1226 | (unsigned int) srel, | |
1227 | (unsigned int) old_srel, | |
1228 | (unsigned int) reloc_addr); | |
28c9d252 NC |
1229 | |
1230 | if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) | |
1231 | return bfd_reloc_outofrange; | |
07d6d2b8 | 1232 | } |
28c9d252 | 1233 | |
adde6300 AM |
1234 | if (srel & 1) |
1235 | return bfd_reloc_outofrange; | |
1236 | srel = srel >> 1; | |
1237 | x = bfd_get_16 (input_bfd, contents); | |
1238 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1239 | bfd_put_16 (input_bfd, x, contents); | |
1240 | break; | |
1241 | ||
28c9d252 NC |
1242 | case R_AVR_HI8_LDI_GS: |
1243 | use_stubs = (!htab->no_stubs); | |
1244 | /* Fall through. */ | |
adde6300 AM |
1245 | case R_AVR_HI8_LDI_PM: |
1246 | contents += rel->r_offset; | |
1247 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
28c9d252 NC |
1248 | |
1249 | if (use_stubs | |
07d6d2b8 AM |
1250 | && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) |
1251 | { | |
1252 | bfd_vma old_srel = srel; | |
1253 | ||
1254 | /* We need to use the address of the stub instead. */ | |
1255 | srel = avr_get_stub_addr (srel, htab); | |
1256 | if (debug_stubs) | |
1257 | printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " | |
1258 | "reloc at address 0x%x.\n", | |
1259 | (unsigned int) srel, | |
1260 | (unsigned int) old_srel, | |
1261 | (unsigned int) reloc_addr); | |
28c9d252 NC |
1262 | |
1263 | if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) | |
1264 | return bfd_reloc_outofrange; | |
07d6d2b8 | 1265 | } |
28c9d252 | 1266 | |
adde6300 AM |
1267 | if (srel & 1) |
1268 | return bfd_reloc_outofrange; | |
1269 | srel = srel >> 1; | |
1270 | srel = (srel >> 8) & 0xff; | |
1271 | x = bfd_get_16 (input_bfd, contents); | |
1272 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1273 | bfd_put_16 (input_bfd, x, contents); | |
1274 | break; | |
1275 | ||
1276 | case R_AVR_HH8_LDI_PM: | |
1277 | contents += rel->r_offset; | |
1278 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1279 | if (srel & 1) | |
1280 | return bfd_reloc_outofrange; | |
1281 | srel = srel >> 1; | |
1282 | srel = (srel >> 16) & 0xff; | |
1283 | x = bfd_get_16 (input_bfd, contents); | |
1284 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1285 | bfd_put_16 (input_bfd, x, contents); | |
1286 | break; | |
1287 | ||
1288 | case R_AVR_LO8_LDI_PM_NEG: | |
1289 | contents += rel->r_offset; | |
1290 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1291 | srel = -srel; | |
1292 | if (srel & 1) | |
1293 | return bfd_reloc_outofrange; | |
1294 | srel = srel >> 1; | |
1295 | x = bfd_get_16 (input_bfd, contents); | |
1296 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1297 | bfd_put_16 (input_bfd, x, contents); | |
1298 | break; | |
1299 | ||
1300 | case R_AVR_HI8_LDI_PM_NEG: | |
1301 | contents += rel->r_offset; | |
1302 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1303 | srel = -srel; | |
1304 | if (srel & 1) | |
1305 | return bfd_reloc_outofrange; | |
1306 | srel = srel >> 1; | |
1307 | srel = (srel >> 8) & 0xff; | |
1308 | x = bfd_get_16 (input_bfd, contents); | |
1309 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1310 | bfd_put_16 (input_bfd, x, contents); | |
1311 | break; | |
1312 | ||
1313 | case R_AVR_HH8_LDI_PM_NEG: | |
1314 | contents += rel->r_offset; | |
1315 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1316 | srel = -srel; | |
1317 | if (srel & 1) | |
1318 | return bfd_reloc_outofrange; | |
1319 | srel = srel >> 1; | |
1320 | srel = (srel >> 16) & 0xff; | |
1321 | x = bfd_get_16 (input_bfd, contents); | |
1322 | x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00); | |
1323 | bfd_put_16 (input_bfd, x, contents); | |
1324 | break; | |
1325 | ||
1326 | case R_AVR_CALL: | |
1327 | contents += rel->r_offset; | |
1328 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1329 | if (srel & 1) | |
1330 | return bfd_reloc_outofrange; | |
1331 | srel = srel >> 1; | |
1332 | x = bfd_get_16 (input_bfd, contents); | |
1333 | x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16; | |
1334 | bfd_put_16 (input_bfd, x, contents); | |
dc810e39 | 1335 | bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2); |
adde6300 AM |
1336 | break; |
1337 | ||
28c9d252 NC |
1338 | case R_AVR_16_PM: |
1339 | use_stubs = (!htab->no_stubs); | |
1340 | contents += rel->r_offset; | |
1341 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1342 | ||
1343 | if (use_stubs | |
07d6d2b8 AM |
1344 | && avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) |
1345 | { | |
1346 | bfd_vma old_srel = srel; | |
1347 | ||
1348 | /* We need to use the address of the stub instead. */ | |
1349 | srel = avr_get_stub_addr (srel,htab); | |
1350 | if (debug_stubs) | |
1351 | printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for " | |
1352 | "reloc at address 0x%x.\n", | |
1353 | (unsigned int) srel, | |
1354 | (unsigned int) old_srel, | |
1355 | (unsigned int) reloc_addr); | |
28c9d252 NC |
1356 | |
1357 | if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr)) | |
1358 | return bfd_reloc_outofrange; | |
07d6d2b8 | 1359 | } |
28c9d252 NC |
1360 | |
1361 | if (srel & 1) | |
1362 | return bfd_reloc_outofrange; | |
1363 | srel = srel >> 1; | |
1364 | bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents); | |
1365 | break; | |
1366 | ||
e4ef1b6c DC |
1367 | case R_AVR_DIFF8: |
1368 | case R_AVR_DIFF16: | |
1369 | case R_AVR_DIFF32: | |
1370 | /* Nothing to do here, as contents already contains the diff value. */ | |
1371 | r = bfd_reloc_ok; | |
1372 | break; | |
1373 | ||
f36e8886 BS |
1374 | case R_AVR_LDS_STS_16: |
1375 | contents += rel->r_offset; | |
1376 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1377 | if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf) | |
07d6d2b8 | 1378 | return bfd_reloc_outofrange; |
f36e8886 BS |
1379 | srel = srel & 0x7f; |
1380 | x = bfd_get_16 (input_bfd, contents); | |
1381 | x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2); | |
1382 | bfd_put_16 (input_bfd, x, contents); | |
1383 | break; | |
1384 | ||
75f58085 BS |
1385 | case R_AVR_PORT6: |
1386 | contents += rel->r_offset; | |
1387 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1388 | if ((srel & 0xffff) > 0x3f) | |
07d6d2b8 | 1389 | return bfd_reloc_outofrange; |
75f58085 BS |
1390 | x = bfd_get_16 (input_bfd, contents); |
1391 | x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f); | |
1392 | bfd_put_16 (input_bfd, x, contents); | |
1393 | break; | |
1394 | ||
1395 | case R_AVR_PORT5: | |
1396 | contents += rel->r_offset; | |
1397 | srel = (bfd_signed_vma) relocation + rel->r_addend; | |
1398 | if ((srel & 0xffff) > 0x1f) | |
07d6d2b8 | 1399 | return bfd_reloc_outofrange; |
75f58085 BS |
1400 | x = bfd_get_16 (input_bfd, contents); |
1401 | x = (x & 0xff07) | ((srel & 0x1f) << 3); | |
1402 | bfd_put_16 (input_bfd, x, contents); | |
1403 | break; | |
1404 | ||
adde6300 AM |
1405 | default: |
1406 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
1407 | contents, rel->r_offset, | |
1408 | relocation, rel->r_addend); | |
1409 | } | |
1410 | ||
1411 | return r; | |
1412 | } | |
1413 | ||
1414 | /* Relocate an AVR ELF section. */ | |
4cdc7696 | 1415 | |
b34976b6 | 1416 | static bfd_boolean |
4cdc7696 NC |
1417 | elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED, |
1418 | struct bfd_link_info *info, | |
1419 | bfd *input_bfd, | |
1420 | asection *input_section, | |
1421 | bfd_byte *contents, | |
1422 | Elf_Internal_Rela *relocs, | |
1423 | Elf_Internal_Sym *local_syms, | |
1424 | asection **local_sections) | |
adde6300 | 1425 | { |
07d6d2b8 | 1426 | Elf_Internal_Shdr * symtab_hdr; |
adde6300 | 1427 | struct elf_link_hash_entry ** sym_hashes; |
07d6d2b8 AM |
1428 | Elf_Internal_Rela * rel; |
1429 | Elf_Internal_Rela * relend; | |
28c9d252 | 1430 | struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info); |
adde6300 | 1431 | |
4dfe6ac6 NC |
1432 | if (htab == NULL) |
1433 | return FALSE; | |
1434 | ||
adde6300 AM |
1435 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
1436 | sym_hashes = elf_sym_hashes (input_bfd); | |
1437 | relend = relocs + input_section->reloc_count; | |
1438 | ||
1439 | for (rel = relocs; rel < relend; rel ++) | |
1440 | { | |
07d6d2b8 AM |
1441 | reloc_howto_type * howto; |
1442 | unsigned long r_symndx; | |
1443 | Elf_Internal_Sym * sym; | |
1444 | asection * sec; | |
adde6300 | 1445 | struct elf_link_hash_entry * h; |
07d6d2b8 AM |
1446 | bfd_vma relocation; |
1447 | bfd_reloc_status_type r; | |
1448 | const char * name; | |
1449 | int r_type; | |
adde6300 AM |
1450 | |
1451 | r_type = ELF32_R_TYPE (rel->r_info); | |
1452 | r_symndx = ELF32_R_SYM (rel->r_info); | |
c7e2358a | 1453 | howto = elf_avr_howto_table + r_type; |
adde6300 AM |
1454 | h = NULL; |
1455 | sym = NULL; | |
1456 | sec = NULL; | |
1457 | ||
1458 | if (r_symndx < symtab_hdr->sh_info) | |
1459 | { | |
1460 | sym = local_syms + r_symndx; | |
1461 | sec = local_sections [r_symndx]; | |
8517fae7 | 1462 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel); |
adde6300 AM |
1463 | |
1464 | name = bfd_elf_string_from_elf_section | |
1465 | (input_bfd, symtab_hdr->sh_link, sym->st_name); | |
fd361982 | 1466 | name = name == NULL ? bfd_section_name (sec) : name; |
adde6300 AM |
1467 | } |
1468 | else | |
1469 | { | |
62d887d4 | 1470 | bfd_boolean unresolved_reloc, warned, ignored; |
adde6300 | 1471 | |
b2a8e766 AM |
1472 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
1473 | r_symndx, symtab_hdr, sym_hashes, | |
1474 | h, sec, relocation, | |
62d887d4 | 1475 | unresolved_reloc, warned, ignored); |
dfeffb9f L |
1476 | |
1477 | name = h->root.root.string; | |
adde6300 AM |
1478 | } |
1479 | ||
dbaa2011 | 1480 | if (sec != NULL && discarded_section (sec)) |
e4067dbb | 1481 | RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section, |
545fd46b | 1482 | rel, 1, relend, howto, 0, contents); |
ab96bf03 | 1483 | |
0e1862bb | 1484 | if (bfd_link_relocatable (info)) |
ab96bf03 AM |
1485 | continue; |
1486 | ||
adde6300 | 1487 | r = avr_final_link_relocate (howto, input_bfd, input_section, |
28c9d252 | 1488 | contents, rel, relocation, htab); |
adde6300 AM |
1489 | |
1490 | if (r != bfd_reloc_ok) | |
1491 | { | |
1492 | const char * msg = (const char *) NULL; | |
1493 | ||
1494 | switch (r) | |
1495 | { | |
1496 | case bfd_reloc_overflow: | |
1a72702b AM |
1497 | (*info->callbacks->reloc_overflow) |
1498 | (info, (h ? &h->root : NULL), name, howto->name, | |
1499 | (bfd_vma) 0, input_bfd, input_section, rel->r_offset); | |
adde6300 AM |
1500 | break; |
1501 | ||
1502 | case bfd_reloc_undefined: | |
1a72702b | 1503 | (*info->callbacks->undefined_symbol) |
b34976b6 | 1504 | (info, name, input_bfd, input_section, rel->r_offset, TRUE); |
adde6300 AM |
1505 | break; |
1506 | ||
1507 | case bfd_reloc_outofrange: | |
1508 | msg = _("internal error: out of range error"); | |
1509 | break; | |
1510 | ||
1511 | case bfd_reloc_notsupported: | |
1512 | msg = _("internal error: unsupported relocation error"); | |
1513 | break; | |
1514 | ||
1515 | case bfd_reloc_dangerous: | |
1516 | msg = _("internal error: dangerous relocation"); | |
1517 | break; | |
1518 | ||
1519 | default: | |
1520 | msg = _("internal error: unknown error"); | |
1521 | break; | |
1522 | } | |
1523 | ||
1524 | if (msg) | |
1a72702b AM |
1525 | (*info->callbacks->warning) (info, msg, name, input_bfd, |
1526 | input_section, rel->r_offset); | |
adde6300 AM |
1527 | } |
1528 | } | |
1529 | ||
b34976b6 | 1530 | return TRUE; |
adde6300 AM |
1531 | } |
1532 | ||
1533 | /* The final processing done just before writing out a AVR ELF object | |
1534 | file. This gets the AVR architecture right based on the machine | |
1535 | number. */ | |
1536 | ||
cc364be6 AM |
1537 | static bfd_boolean |
1538 | bfd_elf_avr_final_write_processing (bfd *abfd) | |
adde6300 AM |
1539 | { |
1540 | unsigned long val; | |
1541 | ||
1542 | switch (bfd_get_mach (abfd)) | |
1543 | { | |
1544 | default: | |
1545 | case bfd_mach_avr2: | |
1546 | val = E_AVR_MACH_AVR2; | |
1547 | break; | |
1548 | ||
1549 | case bfd_mach_avr1: | |
1550 | val = E_AVR_MACH_AVR1; | |
1551 | break; | |
1552 | ||
7b21ac3f EW |
1553 | case bfd_mach_avr25: |
1554 | val = E_AVR_MACH_AVR25; | |
28b02751 | 1555 | break; |
7b21ac3f | 1556 | |
adde6300 AM |
1557 | case bfd_mach_avr3: |
1558 | val = E_AVR_MACH_AVR3; | |
1559 | break; | |
1560 | ||
7b21ac3f EW |
1561 | case bfd_mach_avr31: |
1562 | val = E_AVR_MACH_AVR31; | |
28b02751 | 1563 | break; |
7b21ac3f EW |
1564 | |
1565 | case bfd_mach_avr35: | |
1566 | val = E_AVR_MACH_AVR35; | |
28b02751 | 1567 | break; |
7b21ac3f | 1568 | |
adde6300 AM |
1569 | case bfd_mach_avr4: |
1570 | val = E_AVR_MACH_AVR4; | |
1571 | break; | |
1572 | ||
65aa24b6 NC |
1573 | case bfd_mach_avr5: |
1574 | val = E_AVR_MACH_AVR5; | |
1575 | break; | |
28c9d252 | 1576 | |
7b21ac3f EW |
1577 | case bfd_mach_avr51: |
1578 | val = E_AVR_MACH_AVR51; | |
1579 | break; | |
1580 | ||
28c9d252 NC |
1581 | case bfd_mach_avr6: |
1582 | val = E_AVR_MACH_AVR6; | |
1583 | break; | |
8cc66334 EW |
1584 | |
1585 | case bfd_mach_avrxmega1: | |
1586 | val = E_AVR_MACH_XMEGA1; | |
1587 | break; | |
1588 | ||
1589 | case bfd_mach_avrxmega2: | |
1590 | val = E_AVR_MACH_XMEGA2; | |
1591 | break; | |
1592 | ||
1593 | case bfd_mach_avrxmega3: | |
1594 | val = E_AVR_MACH_XMEGA3; | |
1595 | break; | |
1596 | ||
1597 | case bfd_mach_avrxmega4: | |
1598 | val = E_AVR_MACH_XMEGA4; | |
1599 | break; | |
1600 | ||
1601 | case bfd_mach_avrxmega5: | |
1602 | val = E_AVR_MACH_XMEGA5; | |
1603 | break; | |
1604 | ||
1605 | case bfd_mach_avrxmega6: | |
1606 | val = E_AVR_MACH_XMEGA6; | |
1607 | break; | |
1608 | ||
1609 | case bfd_mach_avrxmega7: | |
1610 | val = E_AVR_MACH_XMEGA7; | |
1611 | break; | |
f36e8886 BS |
1612 | |
1613 | case bfd_mach_avrtiny: | |
1614 | val = E_AVR_MACH_AVRTINY; | |
1615 | break; | |
adde6300 AM |
1616 | } |
1617 | ||
1618 | elf_elfheader (abfd)->e_machine = EM_AVR; | |
1619 | elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH; | |
1620 | elf_elfheader (abfd)->e_flags |= val; | |
cc364be6 | 1621 | return _bfd_elf_final_write_processing (abfd); |
adde6300 AM |
1622 | } |
1623 | ||
1624 | /* Set the right machine number. */ | |
1625 | ||
b34976b6 | 1626 | static bfd_boolean |
4cdc7696 | 1627 | elf32_avr_object_p (bfd *abfd) |
adde6300 | 1628 | { |
dc810e39 | 1629 | unsigned int e_set = bfd_mach_avr2; |
4cdc7696 | 1630 | |
aa4f99bb AO |
1631 | if (elf_elfheader (abfd)->e_machine == EM_AVR |
1632 | || elf_elfheader (abfd)->e_machine == EM_AVR_OLD) | |
adde6300 AM |
1633 | { |
1634 | int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH; | |
4cdc7696 | 1635 | |
adde6300 AM |
1636 | switch (e_mach) |
1637 | { | |
1638 | default: | |
1639 | case E_AVR_MACH_AVR2: | |
1640 | e_set = bfd_mach_avr2; | |
1641 | break; | |
1642 | ||
1643 | case E_AVR_MACH_AVR1: | |
1644 | e_set = bfd_mach_avr1; | |
1645 | break; | |
1646 | ||
7b21ac3f EW |
1647 | case E_AVR_MACH_AVR25: |
1648 | e_set = bfd_mach_avr25; | |
1649 | break; | |
1650 | ||
adde6300 AM |
1651 | case E_AVR_MACH_AVR3: |
1652 | e_set = bfd_mach_avr3; | |
1653 | break; | |
1654 | ||
7b21ac3f EW |
1655 | case E_AVR_MACH_AVR31: |
1656 | e_set = bfd_mach_avr31; | |
1657 | break; | |
1658 | ||
1659 | case E_AVR_MACH_AVR35: | |
1660 | e_set = bfd_mach_avr35; | |
1661 | break; | |
1662 | ||
adde6300 AM |
1663 | case E_AVR_MACH_AVR4: |
1664 | e_set = bfd_mach_avr4; | |
1665 | break; | |
65aa24b6 NC |
1666 | |
1667 | case E_AVR_MACH_AVR5: | |
1668 | e_set = bfd_mach_avr5; | |
1669 | break; | |
28c9d252 | 1670 | |
7b21ac3f EW |
1671 | case E_AVR_MACH_AVR51: |
1672 | e_set = bfd_mach_avr51; | |
1673 | break; | |
1674 | ||
28c9d252 NC |
1675 | case E_AVR_MACH_AVR6: |
1676 | e_set = bfd_mach_avr6; | |
1677 | break; | |
8cc66334 EW |
1678 | |
1679 | case E_AVR_MACH_XMEGA1: | |
1680 | e_set = bfd_mach_avrxmega1; | |
1681 | break; | |
1682 | ||
1683 | case E_AVR_MACH_XMEGA2: | |
1684 | e_set = bfd_mach_avrxmega2; | |
1685 | break; | |
1686 | ||
1687 | case E_AVR_MACH_XMEGA3: | |
1688 | e_set = bfd_mach_avrxmega3; | |
1689 | break; | |
1690 | ||
1691 | case E_AVR_MACH_XMEGA4: | |
1692 | e_set = bfd_mach_avrxmega4; | |
1693 | break; | |
1694 | ||
1695 | case E_AVR_MACH_XMEGA5: | |
1696 | e_set = bfd_mach_avrxmega5; | |
1697 | break; | |
1698 | ||
1699 | case E_AVR_MACH_XMEGA6: | |
1700 | e_set = bfd_mach_avrxmega6; | |
1701 | break; | |
1702 | ||
1703 | case E_AVR_MACH_XMEGA7: | |
1704 | e_set = bfd_mach_avrxmega7; | |
1705 | break; | |
f36e8886 BS |
1706 | |
1707 | case E_AVR_MACH_AVRTINY: | |
1708 | e_set = bfd_mach_avrtiny; | |
1709 | break; | |
adde6300 AM |
1710 | } |
1711 | } | |
1712 | return bfd_default_set_arch_mach (abfd, bfd_arch_avr, | |
1713 | e_set); | |
1714 | } | |
1715 | ||
e4ef1b6c DC |
1716 | /* Returns whether the relocation type passed is a diff reloc. */ |
1717 | ||
1718 | static bfd_boolean | |
1719 | elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel) | |
1720 | { | |
1721 | return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8 | |
07d6d2b8 AM |
1722 | ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16 |
1723 | || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32); | |
e4ef1b6c DC |
1724 | } |
1725 | ||
f36e8886 BS |
1726 | /* Reduce the diff value written in the section by count if the shrinked |
1727 | insn address happens to fall between the two symbols for which this | |
1728 | diff reloc was emitted. */ | |
e4ef1b6c DC |
1729 | |
1730 | static void | |
1731 | elf32_avr_adjust_diff_reloc_value (bfd *abfd, | |
07d6d2b8 AM |
1732 | struct bfd_section *isec, |
1733 | Elf_Internal_Rela *irel, | |
1734 | bfd_vma symval, | |
1735 | bfd_vma shrinked_insn_address, | |
1736 | int count) | |
e4ef1b6c DC |
1737 | { |
1738 | unsigned char *reloc_contents = NULL; | |
1739 | unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents; | |
1740 | if (isec_contents == NULL) | |
1741 | { | |
1742 | if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents)) | |
1743 | return; | |
1744 | ||
1745 | elf_section_data (isec)->this_hdr.contents = isec_contents; | |
1746 | } | |
1747 | ||
1748 | reloc_contents = isec_contents + irel->r_offset; | |
1749 | ||
1750 | /* Read value written in object file. */ | |
4cb771f2 | 1751 | bfd_signed_vma x = 0; |
e4ef1b6c DC |
1752 | switch (ELF32_R_TYPE (irel->r_info)) |
1753 | { | |
1754 | case R_AVR_DIFF8: | |
1755 | { | |
4cb771f2 | 1756 | x = bfd_get_signed_8 (abfd, reloc_contents); |
e4ef1b6c DC |
1757 | break; |
1758 | } | |
1759 | case R_AVR_DIFF16: | |
1760 | { | |
4cb771f2 | 1761 | x = bfd_get_signed_16 (abfd, reloc_contents); |
e4ef1b6c DC |
1762 | break; |
1763 | } | |
1764 | case R_AVR_DIFF32: | |
1765 | { | |
4cb771f2 | 1766 | x = bfd_get_signed_32 (abfd, reloc_contents); |
e4ef1b6c DC |
1767 | break; |
1768 | } | |
1769 | default: | |
1770 | { | |
1771 | BFD_FAIL(); | |
1772 | } | |
1773 | } | |
1774 | ||
1775 | /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written | |
1776 | into the object file at the reloc offset. sym2's logical value is | |
1777 | symval (<start_of_section>) + reloc addend. Compute the start and end | |
1778 | addresses and check if the shrinked insn falls between sym1 and sym2. */ | |
1779 | ||
4cb771f2 SKS |
1780 | bfd_vma sym2_address = symval + irel->r_addend; |
1781 | bfd_vma sym1_address = sym2_address - x; | |
1782 | ||
1783 | /* Don't assume sym2 is bigger than sym1 - the difference | |
1784 | could be negative. Compute start and end addresses, and | |
1785 | use those to see if they span shrinked_insn_address. */ | |
1786 | ||
1787 | bfd_vma start_address = sym1_address < sym2_address | |
1788 | ? sym1_address : sym2_address; | |
1789 | bfd_vma end_address = sym1_address > sym2_address | |
1790 | ? sym1_address : sym2_address; | |
e4ef1b6c | 1791 | |
e4ef1b6c | 1792 | |
f36e8886 | 1793 | if (shrinked_insn_address >= start_address |
68063779 | 1794 | && shrinked_insn_address < end_address) |
e4ef1b6c | 1795 | { |
4cb771f2 SKS |
1796 | /* Reduce the diff value by count bytes and write it back into section |
1797 | contents. */ | |
1798 | bfd_signed_vma new_diff = x < 0 ? x + count : x - count; | |
1799 | ||
68063779 SKS |
1800 | if (sym2_address > shrinked_insn_address) |
1801 | irel->r_addend -= count; | |
1802 | ||
e4ef1b6c DC |
1803 | switch (ELF32_R_TYPE (irel->r_info)) |
1804 | { | |
1805 | case R_AVR_DIFF8: | |
1806 | { | |
07d6d2b8 AM |
1807 | bfd_put_signed_8 (abfd, new_diff, reloc_contents); |
1808 | break; | |
e4ef1b6c DC |
1809 | } |
1810 | case R_AVR_DIFF16: | |
1811 | { | |
07d6d2b8 AM |
1812 | bfd_put_signed_16 (abfd, new_diff & 0xFFFF, reloc_contents); |
1813 | break; | |
e4ef1b6c DC |
1814 | } |
1815 | case R_AVR_DIFF32: | |
1816 | { | |
07d6d2b8 AM |
1817 | bfd_put_signed_32 (abfd, new_diff & 0xFFFFFFFF, reloc_contents); |
1818 | break; | |
e4ef1b6c DC |
1819 | } |
1820 | default: | |
1821 | { | |
07d6d2b8 | 1822 | BFD_FAIL(); |
e4ef1b6c DC |
1823 | } |
1824 | } | |
1825 | ||
1826 | } | |
1827 | } | |
df406460 | 1828 | |
4cb771f2 SKS |
1829 | static void |
1830 | elf32_avr_adjust_reloc_if_spans_insn (bfd *abfd, | |
07d6d2b8 AM |
1831 | asection *isec, |
1832 | Elf_Internal_Rela *irel, bfd_vma symval, | |
1833 | bfd_vma shrinked_insn_address, | |
1834 | bfd_vma shrink_boundary, | |
1835 | int count) | |
4cb771f2 SKS |
1836 | { |
1837 | ||
1838 | if (elf32_avr_is_diff_reloc (irel)) | |
1839 | { | |
1840 | elf32_avr_adjust_diff_reloc_value (abfd, isec, irel, | |
07d6d2b8 AM |
1841 | symval, |
1842 | shrinked_insn_address, | |
1843 | count); | |
4cb771f2 SKS |
1844 | } |
1845 | else | |
1846 | { | |
1847 | bfd_vma reloc_value = symval + irel->r_addend; | |
1848 | bfd_boolean addend_within_shrink_boundary = | |
07d6d2b8 | 1849 | (reloc_value <= shrink_boundary); |
4cb771f2 SKS |
1850 | |
1851 | bfd_boolean reloc_spans_insn = | |
07d6d2b8 AM |
1852 | (symval <= shrinked_insn_address |
1853 | && reloc_value > shrinked_insn_address | |
1854 | && addend_within_shrink_boundary); | |
4cb771f2 SKS |
1855 | |
1856 | if (! reloc_spans_insn) | |
07d6d2b8 | 1857 | return; |
4cb771f2 SKS |
1858 | |
1859 | irel->r_addend -= count; | |
1860 | ||
1861 | if (debug_relax) | |
07d6d2b8 | 1862 | printf ("Relocation's addend needed to be fixed \n"); |
4cb771f2 SKS |
1863 | } |
1864 | } | |
1865 | ||
b660e9eb SKS |
1866 | static bfd_boolean |
1867 | avr_should_move_sym (symvalue symval, | |
07d6d2b8 AM |
1868 | bfd_vma start, |
1869 | bfd_vma end, | |
1870 | bfd_boolean did_pad) | |
b660e9eb SKS |
1871 | { |
1872 | bfd_boolean sym_within_boundary = | |
07d6d2b8 | 1873 | did_pad ? symval < end : symval <= end; |
b660e9eb SKS |
1874 | return (symval > start && sym_within_boundary); |
1875 | } | |
1876 | ||
1877 | static bfd_boolean | |
1878 | avr_should_reduce_sym_size (symvalue symval, | |
07d6d2b8 AM |
1879 | symvalue symend, |
1880 | bfd_vma start, | |
1881 | bfd_vma end, | |
1882 | bfd_boolean did_pad) | |
b660e9eb SKS |
1883 | { |
1884 | bfd_boolean sym_end_within_boundary = | |
07d6d2b8 | 1885 | did_pad ? symend < end : symend <= end; |
b660e9eb SKS |
1886 | return (symval <= start && symend > start && sym_end_within_boundary); |
1887 | } | |
1888 | ||
1889 | static bfd_boolean | |
1890 | avr_should_increase_sym_size (symvalue symval, | |
07d6d2b8 AM |
1891 | symvalue symend, |
1892 | bfd_vma start, | |
1893 | bfd_vma end, | |
1894 | bfd_boolean did_pad) | |
b660e9eb SKS |
1895 | { |
1896 | return avr_should_move_sym (symval, start, end, did_pad) | |
07d6d2b8 | 1897 | && symend >= end && did_pad; |
b660e9eb SKS |
1898 | } |
1899 | ||
4cdc7696 NC |
1900 | /* Delete some bytes from a section while changing the size of an instruction. |
1901 | The parameter "addr" denotes the section-relative offset pointing just | |
1902 | behind the shrinked instruction. "addr+count" point at the first | |
bf186506 SKS |
1903 | byte just behind the original unshrinked instruction. If delete_shrinks_insn |
1904 | is FALSE, we are deleting redundant padding bytes from relax_info prop | |
1905 | record handling. In that case, addr is section-relative offset of start | |
1906 | of padding, and count is the number of padding bytes to delete. */ | |
4cdc7696 NC |
1907 | |
1908 | static bfd_boolean | |
1909 | elf32_avr_relax_delete_bytes (bfd *abfd, | |
07d6d2b8 AM |
1910 | asection *sec, |
1911 | bfd_vma addr, | |
1912 | int count, | |
1913 | bfd_boolean delete_shrinks_insn) | |
4cdc7696 NC |
1914 | { |
1915 | Elf_Internal_Shdr *symtab_hdr; | |
1916 | unsigned int sec_shndx; | |
1917 | bfd_byte *contents; | |
1918 | Elf_Internal_Rela *irel, *irelend; | |
4cdc7696 NC |
1919 | Elf_Internal_Sym *isym; |
1920 | Elf_Internal_Sym *isymbuf = NULL; | |
b660e9eb | 1921 | bfd_vma toaddr; |
4cdc7696 NC |
1922 | struct elf_link_hash_entry **sym_hashes; |
1923 | struct elf_link_hash_entry **end_hashes; | |
1924 | unsigned int symcount; | |
bac13f5a AB |
1925 | struct avr_relax_info *relax_info; |
1926 | struct avr_property_record *prop_record = NULL; | |
5c41dbc3 | 1927 | bfd_boolean did_shrink = FALSE; |
b660e9eb | 1928 | bfd_boolean did_pad = FALSE; |
4cdc7696 NC |
1929 | |
1930 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
1931 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
1932 | contents = elf_section_data (sec)->this_hdr.contents; | |
bac13f5a | 1933 | relax_info = get_avr_relax_info (sec); |
4cdc7696 | 1934 | |
4cdc7696 NC |
1935 | toaddr = sec->size; |
1936 | ||
bac13f5a AB |
1937 | if (relax_info->records.count > 0) |
1938 | { | |
1939 | /* There should be no property record within the range of deleted | |
07d6d2b8 AM |
1940 | bytes, however, there might be a property record for ADDR, this is |
1941 | how we handle alignment directives. | |
1942 | Find the next (if any) property record after the deleted bytes. */ | |
bac13f5a AB |
1943 | unsigned int i; |
1944 | ||
1945 | for (i = 0; i < relax_info->records.count; ++i) | |
07d6d2b8 AM |
1946 | { |
1947 | bfd_vma offset = relax_info->records.items [i].offset; | |
1948 | ||
1949 | BFD_ASSERT (offset <= addr || offset >= (addr + count)); | |
1950 | if (offset >= (addr + count)) | |
1951 | { | |
1952 | prop_record = &relax_info->records.items [i]; | |
1953 | toaddr = offset; | |
1954 | break; | |
1955 | } | |
1956 | } | |
bac13f5a AB |
1957 | } |
1958 | ||
4cdc7696 NC |
1959 | irel = elf_section_data (sec)->relocs; |
1960 | irelend = irel + sec->reloc_count; | |
1961 | ||
1962 | /* Actually delete the bytes. */ | |
1963 | if (toaddr - addr - count > 0) | |
5c41dbc3 DC |
1964 | { |
1965 | memmove (contents + addr, contents + addr + count, | |
07d6d2b8 | 1966 | (size_t) (toaddr - addr - count)); |
5c41dbc3 DC |
1967 | did_shrink = TRUE; |
1968 | } | |
bac13f5a | 1969 | if (prop_record == NULL) |
5c41dbc3 DC |
1970 | { |
1971 | sec->size -= count; | |
1972 | did_shrink = TRUE; | |
1973 | } | |
bac13f5a AB |
1974 | else |
1975 | { | |
1976 | /* Use the property record to fill in the bytes we've opened up. */ | |
1977 | int fill = 0; | |
1978 | switch (prop_record->type) | |
07d6d2b8 AM |
1979 | { |
1980 | case RECORD_ORG_AND_FILL: | |
1981 | fill = prop_record->data.org.fill; | |
1982 | /* Fall through. */ | |
1983 | case RECORD_ORG: | |
1984 | break; | |
1985 | case RECORD_ALIGN_AND_FILL: | |
1986 | fill = prop_record->data.align.fill; | |
1987 | /* Fall through. */ | |
1988 | case RECORD_ALIGN: | |
1989 | prop_record->data.align.preceding_deleted += count; | |
1990 | break; | |
1991 | }; | |
5c41dbc3 | 1992 | /* If toaddr == (addr + count), then we didn't delete anything, yet |
07d6d2b8 AM |
1993 | we fill count bytes backwards from toaddr. This is still ok - we |
1994 | end up overwriting the bytes we would have deleted. We just need | |
1995 | to remember we didn't delete anything i.e. don't set did_shrink, | |
1996 | so that we don't corrupt reloc offsets or symbol values.*/ | |
bac13f5a | 1997 | memset (contents + toaddr - count, fill, count); |
b660e9eb | 1998 | did_pad = TRUE; |
bac13f5a | 1999 | } |
4cdc7696 | 2000 | |
5c41dbc3 DC |
2001 | if (!did_shrink) |
2002 | return TRUE; | |
2003 | ||
73160847 | 2004 | /* Adjust all the reloc addresses. */ |
4cdc7696 NC |
2005 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++) |
2006 | { | |
4cdc7696 | 2007 | bfd_vma old_reloc_address; |
4cdc7696 NC |
2008 | |
2009 | old_reloc_address = (sec->output_section->vma | |
07d6d2b8 | 2010 | + sec->output_offset + irel->r_offset); |
4cdc7696 NC |
2011 | |
2012 | /* Get the new reloc address. */ | |
2013 | if ((irel->r_offset > addr | |
07d6d2b8 AM |
2014 | && irel->r_offset < toaddr)) |
2015 | { | |
2016 | if (debug_relax) | |
2017 | printf ("Relocation at address 0x%x needs to be moved.\n" | |
2018 | "Old section offset: 0x%x, New section offset: 0x%x \n", | |
2019 | (unsigned int) old_reloc_address, | |
2020 | (unsigned int) irel->r_offset, | |
2021 | (unsigned int) ((irel->r_offset) - count)); | |
2022 | ||
2023 | irel->r_offset -= count; | |
2024 | } | |
4cdc7696 | 2025 | |
73160847 | 2026 | } |
4cdc7696 | 2027 | |
73160847 NC |
2028 | /* The reloc's own addresses are now ok. However, we need to readjust |
2029 | the reloc's addend, i.e. the reloc's value if two conditions are met: | |
2030 | 1.) the reloc is relative to a symbol in this section that | |
07d6d2b8 | 2031 | is located in front of the shrinked instruction |
28c9d252 NC |
2032 | 2.) symbol plus addend end up behind the shrinked instruction. |
2033 | ||
73160847 NC |
2034 | The most common case where this happens are relocs relative to |
2035 | the section-start symbol. | |
28c9d252 | 2036 | |
73160847 NC |
2037 | This step needs to be done for all of the sections of the bfd. */ |
2038 | ||
2039 | { | |
2040 | struct bfd_section *isec; | |
2041 | ||
2042 | for (isec = abfd->sections; isec; isec = isec->next) | |
2043 | { | |
2044 | bfd_vma symval; | |
2045 | bfd_vma shrinked_insn_address; | |
2046 | ||
a1c7aafb NC |
2047 | if (isec->reloc_count == 0) |
2048 | continue; | |
2049 | ||
73160847 | 2050 | shrinked_insn_address = (sec->output_section->vma |
07d6d2b8 | 2051 | + sec->output_offset + addr); |
bf186506 | 2052 | if (delete_shrinks_insn) |
07d6d2b8 | 2053 | shrinked_insn_address -= count; |
73160847 | 2054 | |
a1c7aafb NC |
2055 | irel = elf_section_data (isec)->relocs; |
2056 | /* PR 12161: Read in the relocs for this section if necessary. */ | |
2057 | if (irel == NULL) | |
07d6d2b8 | 2058 | irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE); |
a1c7aafb NC |
2059 | |
2060 | for (irelend = irel + isec->reloc_count; | |
07d6d2b8 AM |
2061 | irel < irelend; |
2062 | irel++) | |
2063 | { | |
2064 | /* Read this BFD's local symbols if we haven't done | |
2065 | so already. */ | |
2066 | if (isymbuf == NULL && symtab_hdr->sh_info != 0) | |
2067 | { | |
2068 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
2069 | if (isymbuf == NULL) | |
2070 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
2071 | symtab_hdr->sh_info, 0, | |
2072 | NULL, NULL, NULL); | |
2073 | if (isymbuf == NULL) | |
2074 | return FALSE; | |
2075 | } | |
2076 | ||
2077 | /* Get the value of the symbol referred to by the reloc. */ | |
2078 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
2079 | { | |
2080 | /* A local symbol. */ | |
2081 | asection *sym_sec; | |
2082 | ||
2083 | isym = isymbuf + ELF32_R_SYM (irel->r_info); | |
2084 | sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
2085 | symval = isym->st_value; | |
2086 | /* If the reloc is absolute, it will not have | |
2087 | a symbol or section associated with it. */ | |
2088 | if (sym_sec == sec) | |
2089 | { | |
2090 | /* If there is an alignment boundary, we only need to | |
2091 | adjust addends that end up below the boundary. */ | |
2092 | bfd_vma shrink_boundary = (toaddr | |
2093 | + sec->output_section->vma | |
2094 | + sec->output_offset); | |
2095 | ||
2096 | symval += sym_sec->output_section->vma | |
2097 | + sym_sec->output_offset; | |
2098 | ||
2099 | if (debug_relax) | |
2100 | printf ("Checking if the relocation's " | |
2101 | "addend needs corrections.\n" | |
2102 | "Address of anchor symbol: 0x%x \n" | |
2103 | "Address of relocation target: 0x%x \n" | |
2104 | "Address of relaxed insn: 0x%x \n", | |
2105 | (unsigned int) symval, | |
2106 | (unsigned int) (symval + irel->r_addend), | |
2107 | (unsigned int) shrinked_insn_address); | |
2108 | ||
2109 | elf32_avr_adjust_reloc_if_spans_insn (abfd, isec, irel, | |
2110 | symval, | |
2111 | shrinked_insn_address, | |
2112 | shrink_boundary, | |
2113 | count); | |
2114 | } | |
73160847 | 2115 | /* else...Reference symbol is absolute. No adjustment needed. */ |
28c9d252 NC |
2116 | } |
2117 | /* else...Reference symbol is extern. No need for adjusting | |
73160847 | 2118 | the addend. */ |
28c9d252 | 2119 | } |
73160847 NC |
2120 | } |
2121 | } | |
4cdc7696 NC |
2122 | |
2123 | /* Adjust the local symbols defined in this section. */ | |
2124 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
12123067 NC |
2125 | /* Fix PR 9841, there may be no local symbols. */ |
2126 | if (isym != NULL) | |
4cdc7696 | 2127 | { |
12123067 NC |
2128 | Elf_Internal_Sym *isymend; |
2129 | ||
2130 | isymend = isym + symtab_hdr->sh_info; | |
2131 | for (; isym < isymend; isym++) | |
2132 | { | |
931b79cc | 2133 | if (isym->st_shndx == sec_shndx) |
07d6d2b8 AM |
2134 | { |
2135 | symvalue symval = isym->st_value; | |
2136 | symvalue symend = symval + isym->st_size; | |
2137 | if (avr_should_reduce_sym_size (symval, symend, | |
2138 | addr, toaddr, did_pad)) | |
2139 | { | |
2140 | /* If this assert fires then we have a symbol that ends | |
2141 | part way through an instruction. Does that make | |
2142 | sense? */ | |
2143 | BFD_ASSERT (isym->st_value + isym->st_size >= addr + count); | |
2144 | isym->st_size -= count; | |
2145 | } | |
2146 | else if (avr_should_increase_sym_size (symval, symend, | |
2147 | addr, toaddr, did_pad)) | |
2148 | isym->st_size += count; | |
2149 | ||
2150 | if (avr_should_move_sym (symval, addr, toaddr, did_pad)) | |
2151 | isym->st_value -= count; | |
2152 | } | |
12123067 | 2153 | } |
4cdc7696 NC |
2154 | } |
2155 | ||
2156 | /* Now adjust the global symbols defined in this section. */ | |
2157 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) | |
07d6d2b8 | 2158 | - symtab_hdr->sh_info); |
4cdc7696 NC |
2159 | sym_hashes = elf_sym_hashes (abfd); |
2160 | end_hashes = sym_hashes + symcount; | |
2161 | for (; sym_hashes < end_hashes; sym_hashes++) | |
2162 | { | |
2163 | struct elf_link_hash_entry *sym_hash = *sym_hashes; | |
2164 | if ((sym_hash->root.type == bfd_link_hash_defined | |
07d6d2b8 AM |
2165 | || sym_hash->root.type == bfd_link_hash_defweak) |
2166 | && sym_hash->root.u.def.section == sec) | |
2167 | { | |
2168 | symvalue symval = sym_hash->root.u.def.value; | |
2169 | symvalue symend = symval + sym_hash->size; | |
2170 | ||
2171 | if (avr_should_reduce_sym_size (symval, symend, | |
2172 | addr, toaddr, did_pad)) | |
2173 | { | |
2174 | /* If this assert fires then we have a symbol that ends | |
2175 | part way through an instruction. Does that make | |
2176 | sense? */ | |
2177 | BFD_ASSERT (symend >= addr + count); | |
2178 | sym_hash->size -= count; | |
2179 | } | |
2180 | else if (avr_should_increase_sym_size (symval, symend, | |
2181 | addr, toaddr, did_pad)) | |
2182 | sym_hash->size += count; | |
2183 | ||
2184 | if (avr_should_move_sym (symval, addr, toaddr, did_pad)) | |
2185 | sym_hash->root.u.def.value -= count; | |
2186 | } | |
4cdc7696 NC |
2187 | } |
2188 | ||
2189 | return TRUE; | |
2190 | } | |
2191 | ||
137c83d6 AB |
2192 | static Elf_Internal_Sym * |
2193 | retrieve_local_syms (bfd *input_bfd) | |
2194 | { | |
2195 | Elf_Internal_Shdr *symtab_hdr; | |
2196 | Elf_Internal_Sym *isymbuf; | |
2197 | size_t locsymcount; | |
2198 | ||
2199 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
2200 | locsymcount = symtab_hdr->sh_info; | |
2201 | ||
2202 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
2203 | if (isymbuf == NULL && locsymcount != 0) | |
2204 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0, | |
2205 | NULL, NULL, NULL); | |
2206 | ||
2207 | /* Save the symbols for this input file so they won't be read again. */ | |
2208 | if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents) | |
2209 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
2210 | ||
2211 | return isymbuf; | |
2212 | } | |
2213 | ||
2214 | /* Get the input section for a given symbol index. | |
2215 | If the symbol is: | |
2216 | . a section symbol, return the section; | |
2217 | . a common symbol, return the common section; | |
2218 | . an undefined symbol, return the undefined section; | |
2219 | . an indirect symbol, follow the links; | |
2220 | . an absolute value, return the absolute section. */ | |
2221 | ||
2222 | static asection * | |
2223 | get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx) | |
2224 | { | |
2225 | Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2226 | asection *target_sec = NULL; | |
2227 | if (r_symndx < symtab_hdr->sh_info) | |
2228 | { | |
2229 | Elf_Internal_Sym *isymbuf; | |
2230 | unsigned int section_index; | |
2231 | ||
2232 | isymbuf = retrieve_local_syms (abfd); | |
2233 | section_index = isymbuf[r_symndx].st_shndx; | |
2234 | ||
2235 | if (section_index == SHN_UNDEF) | |
2236 | target_sec = bfd_und_section_ptr; | |
2237 | else if (section_index == SHN_ABS) | |
2238 | target_sec = bfd_abs_section_ptr; | |
2239 | else if (section_index == SHN_COMMON) | |
2240 | target_sec = bfd_com_section_ptr; | |
2241 | else | |
2242 | target_sec = bfd_section_from_elf_index (abfd, section_index); | |
2243 | } | |
2244 | else | |
2245 | { | |
2246 | unsigned long indx = r_symndx - symtab_hdr->sh_info; | |
2247 | struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx]; | |
2248 | ||
2249 | while (h->root.type == bfd_link_hash_indirect | |
07d6d2b8 AM |
2250 | || h->root.type == bfd_link_hash_warning) |
2251 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
137c83d6 AB |
2252 | |
2253 | switch (h->root.type) | |
2254 | { | |
2255 | case bfd_link_hash_defined: | |
2256 | case bfd_link_hash_defweak: | |
2257 | target_sec = h->root.u.def.section; | |
2258 | break; | |
2259 | case bfd_link_hash_common: | |
2260 | target_sec = bfd_com_section_ptr; | |
2261 | break; | |
2262 | case bfd_link_hash_undefined: | |
2263 | case bfd_link_hash_undefweak: | |
2264 | target_sec = bfd_und_section_ptr; | |
2265 | break; | |
2266 | default: /* New indirect warning. */ | |
2267 | target_sec = bfd_und_section_ptr; | |
2268 | break; | |
2269 | } | |
2270 | } | |
2271 | return target_sec; | |
2272 | } | |
2273 | ||
2274 | /* Get the section-relative offset for a symbol number. */ | |
2275 | ||
2276 | static bfd_vma | |
2277 | get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx) | |
2278 | { | |
2279 | Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2280 | bfd_vma offset = 0; | |
2281 | ||
2282 | if (r_symndx < symtab_hdr->sh_info) | |
2283 | { | |
2284 | Elf_Internal_Sym *isymbuf; | |
2285 | isymbuf = retrieve_local_syms (abfd); | |
2286 | offset = isymbuf[r_symndx].st_value; | |
2287 | } | |
2288 | else | |
2289 | { | |
2290 | unsigned long indx = r_symndx - symtab_hdr->sh_info; | |
2291 | struct elf_link_hash_entry *h = | |
2292 | elf_sym_hashes (abfd)[indx]; | |
2293 | ||
2294 | while (h->root.type == bfd_link_hash_indirect | |
07d6d2b8 | 2295 | || h->root.type == bfd_link_hash_warning) |
137c83d6 AB |
2296 | h = (struct elf_link_hash_entry *) h->root.u.i.link; |
2297 | if (h->root.type == bfd_link_hash_defined | |
07d6d2b8 | 2298 | || h->root.type == bfd_link_hash_defweak) |
137c83d6 AB |
2299 | offset = h->root.u.def.value; |
2300 | } | |
2301 | return offset; | |
2302 | } | |
2303 | ||
bac13f5a AB |
2304 | /* Iterate over the property records in R_LIST, and copy each record into |
2305 | the list of records within the relaxation information for the section to | |
2306 | which the record applies. */ | |
2307 | ||
2308 | static void | |
2309 | avr_elf32_assign_records_to_sections (struct avr_property_record_list *r_list) | |
2310 | { | |
2311 | unsigned int i; | |
2312 | ||
2313 | for (i = 0; i < r_list->record_count; ++i) | |
2314 | { | |
2315 | struct avr_relax_info *relax_info; | |
2316 | ||
2317 | relax_info = get_avr_relax_info (r_list->records [i].section); | |
2318 | BFD_ASSERT (relax_info != NULL); | |
2319 | ||
2320 | if (relax_info->records.count | |
07d6d2b8 AM |
2321 | == relax_info->records.allocated) |
2322 | { | |
2323 | /* Allocate more space. */ | |
2324 | bfd_size_type size; | |
2325 | ||
2326 | relax_info->records.allocated += 10; | |
2327 | size = (sizeof (struct avr_property_record) | |
2328 | * relax_info->records.allocated); | |
2329 | relax_info->records.items | |
2330 | = bfd_realloc (relax_info->records.items, size); | |
2331 | } | |
bac13f5a AB |
2332 | |
2333 | memcpy (&relax_info->records.items [relax_info->records.count], | |
07d6d2b8 AM |
2334 | &r_list->records [i], |
2335 | sizeof (struct avr_property_record)); | |
bac13f5a AB |
2336 | relax_info->records.count++; |
2337 | } | |
2338 | } | |
2339 | ||
2340 | /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the | |
2341 | ordering callback from QSORT. */ | |
2342 | ||
2343 | static int | |
2344 | avr_property_record_compare (const void *ap, const void *bp) | |
2345 | { | |
2346 | const struct avr_property_record *a | |
2347 | = (struct avr_property_record *) ap; | |
2348 | const struct avr_property_record *b | |
2349 | = (struct avr_property_record *) bp; | |
2350 | ||
2351 | if (a->offset != b->offset) | |
2352 | return (a->offset - b->offset); | |
2353 | ||
2354 | if (a->section != b->section) | |
fd361982 | 2355 | return bfd_section_vma (a->section) - bfd_section_vma (b->section); |
bac13f5a AB |
2356 | |
2357 | return (a->type - b->type); | |
2358 | } | |
2359 | ||
2360 | /* Load all of the avr property sections from all of the bfd objects | |
2361 | referenced from LINK_INFO. All of the records within each property | |
2362 | section are assigned to the STRUCT AVR_RELAX_INFO within the section | |
2363 | specific data of the appropriate section. */ | |
2364 | ||
2365 | static void | |
2366 | avr_load_all_property_sections (struct bfd_link_info *link_info) | |
2367 | { | |
2368 | bfd *abfd; | |
2369 | asection *sec; | |
2370 | ||
2371 | /* Initialize the per-section relaxation info. */ | |
2372 | for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) | |
2373 | for (sec = abfd->sections; sec != NULL; sec = sec->next) | |
2374 | { | |
2375 | init_avr_relax_info (sec); | |
2376 | } | |
2377 | ||
2378 | /* Load the descriptor tables from .avr.prop sections. */ | |
2379 | for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) | |
2380 | { | |
2381 | struct avr_property_record_list *r_list; | |
2382 | ||
2383 | r_list = avr_elf32_load_property_records (abfd); | |
2384 | if (r_list != NULL) | |
07d6d2b8 | 2385 | avr_elf32_assign_records_to_sections (r_list); |
bac13f5a AB |
2386 | |
2387 | free (r_list); | |
2388 | } | |
2389 | ||
2390 | /* Now, for every section, ensure that the descriptor list in the | |
2391 | relaxation data is sorted by ascending offset within the section. */ | |
2392 | for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next) | |
2393 | for (sec = abfd->sections; sec != NULL; sec = sec->next) | |
2394 | { | |
07d6d2b8 AM |
2395 | struct avr_relax_info *relax_info = get_avr_relax_info (sec); |
2396 | if (relax_info && relax_info->records.count > 0) | |
2397 | { | |
2398 | unsigned int i; | |
2399 | ||
2400 | qsort (relax_info->records.items, | |
2401 | relax_info->records.count, | |
2402 | sizeof (struct avr_property_record), | |
2403 | avr_property_record_compare); | |
2404 | ||
2405 | /* For debug purposes, list all the descriptors. */ | |
2406 | for (i = 0; i < relax_info->records.count; ++i) | |
2407 | { | |
2408 | switch (relax_info->records.items [i].type) | |
2409 | { | |
2410 | case RECORD_ORG: | |
2411 | break; | |
2412 | case RECORD_ORG_AND_FILL: | |
2413 | break; | |
2414 | case RECORD_ALIGN: | |
2415 | break; | |
2416 | case RECORD_ALIGN_AND_FILL: | |
2417 | break; | |
2418 | }; | |
2419 | } | |
2420 | } | |
bac13f5a AB |
2421 | } |
2422 | } | |
2423 | ||
df406460 NC |
2424 | /* This function handles relaxing for the avr. |
2425 | Many important relaxing opportunities within functions are already | |
2426 | realized by the compiler itself. | |
2427 | Here we try to replace call (4 bytes) -> rcall (2 bytes) | |
4cdc7696 NC |
2428 | and jump -> rjmp (safes also 2 bytes). |
2429 | As well we now optimize seqences of | |
df406460 NC |
2430 | - call/rcall function |
2431 | - ret | |
2432 | to yield | |
2433 | - jmp/rjmp function | |
2434 | - ret | |
2435 | . In case that within a sequence | |
2436 | - jmp/rjmp label | |
2437 | - ret | |
2438 | the ret could no longer be reached it is optimized away. In order | |
2439 | to check if the ret is no longer needed, it is checked that the ret's address | |
2440 | is not the target of a branch or jump within the same section, it is checked | |
2441 | that there is no skip instruction before the jmp/rjmp and that there | |
2442 | is no local or global label place at the address of the ret. | |
4cdc7696 | 2443 | |
df406460 | 2444 | We refrain from relaxing within sections ".vectors" and |
4cdc7696 | 2445 | ".jumptables" in order to maintain the position of the instructions. |
df406460 | 2446 | There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop |
4cdc7696 | 2447 | if possible. (In future one could possibly use the space of the nop |
df406460 NC |
2448 | for the first instruction of the irq service function. |
2449 | ||
2450 | The .jumptables sections is meant to be used for a future tablejump variant | |
2451 | for the devices with 3-byte program counter where the table itself | |
4cdc7696 | 2452 | contains 4-byte jump instructions whose relative offset must not |
df406460 | 2453 | be changed. */ |
4cdc7696 | 2454 | |
28c9d252 | 2455 | static bfd_boolean |
4cdc7696 NC |
2456 | elf32_avr_relax_section (bfd *abfd, |
2457 | asection *sec, | |
07d6d2b8 AM |
2458 | struct bfd_link_info *link_info, |
2459 | bfd_boolean *again) | |
df406460 NC |
2460 | { |
2461 | Elf_Internal_Shdr *symtab_hdr; | |
2462 | Elf_Internal_Rela *internal_relocs; | |
2463 | Elf_Internal_Rela *irel, *irelend; | |
2464 | bfd_byte *contents = NULL; | |
2465 | Elf_Internal_Sym *isymbuf = NULL; | |
28c9d252 | 2466 | struct elf32_avr_link_hash_table *htab; |
bac13f5a AB |
2467 | static bfd_boolean relaxation_initialised = FALSE; |
2468 | ||
2469 | if (!relaxation_initialised) | |
2470 | { | |
2471 | relaxation_initialised = TRUE; | |
2472 | ||
2473 | /* Load entries from the .avr.prop sections. */ | |
2474 | avr_load_all_property_sections (link_info); | |
2475 | } | |
28c9d252 | 2476 | |
526f25b2 | 2477 | /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while |
68ffbac6 L |
2478 | relaxing. Such shrinking can cause issues for the sections such |
2479 | as .vectors and .jumptables. Instead the unused bytes should be | |
526f25b2 EW |
2480 | filled with nop instructions. */ |
2481 | bfd_boolean shrinkable = TRUE; | |
2482 | ||
2483 | if (!strcmp (sec->name,".vectors") | |
2484 | || !strcmp (sec->name,".jumptables")) | |
2485 | shrinkable = FALSE; | |
2486 | ||
0e1862bb | 2487 | if (bfd_link_relocatable (link_info)) |
c8a1f254 NS |
2488 | (*link_info->callbacks->einfo) |
2489 | (_("%P%F: --relax and -r may not be used together\n")); | |
2490 | ||
28c9d252 | 2491 | htab = avr_link_hash_table (link_info); |
64ee10b6 NC |
2492 | if (htab == NULL) |
2493 | return FALSE; | |
df406460 NC |
2494 | |
2495 | /* Assume nothing changes. */ | |
2496 | *again = FALSE; | |
2497 | ||
28c9d252 NC |
2498 | if ((!htab->no_stubs) && (sec == htab->stub_sec)) |
2499 | { | |
2500 | /* We are just relaxing the stub section. | |
2501 | Let's calculate the size needed again. */ | |
2502 | bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size; | |
2503 | ||
2504 | if (debug_relax) | |
07d6d2b8 AM |
2505 | printf ("Relaxing the stub section. Size prior to this pass: %i\n", |
2506 | (int) last_estimated_stub_section_size); | |
28c9d252 NC |
2507 | |
2508 | elf32_avr_size_stubs (htab->stub_sec->output_section->owner, | |
07d6d2b8 | 2509 | link_info, FALSE); |
28c9d252 NC |
2510 | |
2511 | /* Check if the number of trampolines changed. */ | |
2512 | if (last_estimated_stub_section_size != htab->stub_sec->size) | |
07d6d2b8 | 2513 | *again = TRUE; |
28c9d252 NC |
2514 | |
2515 | if (debug_relax) | |
07d6d2b8 AM |
2516 | printf ("Size of stub section after this pass: %i\n", |
2517 | (int) htab->stub_sec->size); | |
28c9d252 NC |
2518 | |
2519 | return TRUE; | |
2520 | } | |
2521 | ||
df406460 NC |
2522 | /* We don't have to do anything for a relocatable link, if |
2523 | this section does not have relocs, or if this is not a | |
2524 | code section. */ | |
0e1862bb | 2525 | if (bfd_link_relocatable (link_info) |
df406460 NC |
2526 | || (sec->flags & SEC_RELOC) == 0 |
2527 | || sec->reloc_count == 0 | |
2528 | || (sec->flags & SEC_CODE) == 0) | |
2529 | return TRUE; | |
4cdc7696 | 2530 | |
df406460 NC |
2531 | /* Check if the object file to relax uses internal symbols so that we |
2532 | could fix up the relocations. */ | |
df406460 NC |
2533 | if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED)) |
2534 | return TRUE; | |
df406460 NC |
2535 | |
2536 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2537 | ||
2538 | /* Get a copy of the native relocations. */ | |
2539 | internal_relocs = (_bfd_elf_link_read_relocs | |
07d6d2b8 | 2540 | (abfd, sec, NULL, NULL, link_info->keep_memory)); |
df406460 NC |
2541 | if (internal_relocs == NULL) |
2542 | goto error_return; | |
2543 | ||
df406460 NC |
2544 | /* Walk through the relocs looking for relaxing opportunities. */ |
2545 | irelend = internal_relocs + sec->reloc_count; | |
2546 | for (irel = internal_relocs; irel < irelend; irel++) | |
2547 | { | |
2548 | bfd_vma symval; | |
2549 | ||
4cdc7696 | 2550 | if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL |
f36e8886 BS |
2551 | && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL |
2552 | && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL) | |
07d6d2b8 | 2553 | continue; |
4cdc7696 | 2554 | |
df406460 NC |
2555 | /* Get the section contents if we haven't done so already. */ |
2556 | if (contents == NULL) | |
07d6d2b8 AM |
2557 | { |
2558 | /* Get cached copy if it exists. */ | |
2559 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
2560 | contents = elf_section_data (sec)->this_hdr.contents; | |
2561 | else | |
2562 | { | |
2563 | /* Go get them off disk. */ | |
2564 | if (! bfd_malloc_and_get_section (abfd, sec, &contents)) | |
2565 | goto error_return; | |
2566 | } | |
2567 | } | |
df406460 | 2568 | |
91d6fa6a | 2569 | /* Read this BFD's local symbols if we haven't done so already. */ |
df406460 | 2570 | if (isymbuf == NULL && symtab_hdr->sh_info != 0) |
07d6d2b8 AM |
2571 | { |
2572 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
2573 | if (isymbuf == NULL) | |
2574 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
2575 | symtab_hdr->sh_info, 0, | |
2576 | NULL, NULL, NULL); | |
2577 | if (isymbuf == NULL) | |
2578 | goto error_return; | |
2579 | } | |
df406460 NC |
2580 | |
2581 | ||
2582 | /* Get the value of the symbol referred to by the reloc. */ | |
2583 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
07d6d2b8 AM |
2584 | { |
2585 | /* A local symbol. */ | |
2586 | Elf_Internal_Sym *isym; | |
2587 | asection *sym_sec; | |
2588 | ||
2589 | isym = isymbuf + ELF32_R_SYM (irel->r_info); | |
2590 | sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); | |
2591 | symval = isym->st_value; | |
2592 | /* If the reloc is absolute, it will not have | |
2593 | a symbol or section associated with it. */ | |
2594 | if (sym_sec) | |
2595 | symval += sym_sec->output_section->vma | |
2596 | + sym_sec->output_offset; | |
2597 | } | |
df406460 | 2598 | else |
07d6d2b8 AM |
2599 | { |
2600 | unsigned long indx; | |
2601 | struct elf_link_hash_entry *h; | |
2602 | ||
2603 | /* An external symbol. */ | |
2604 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; | |
2605 | h = elf_sym_hashes (abfd)[indx]; | |
2606 | BFD_ASSERT (h != NULL); | |
2607 | if (h->root.type != bfd_link_hash_defined | |
2608 | && h->root.type != bfd_link_hash_defweak) | |
4cdc7696 NC |
2609 | /* This appears to be a reference to an undefined |
2610 | symbol. Just ignore it--it will be caught by the | |
2611 | regular reloc processing. */ | |
2612 | continue; | |
2613 | ||
07d6d2b8 AM |
2614 | symval = (h->root.u.def.value |
2615 | + h->root.u.def.section->output_section->vma | |
2616 | + h->root.u.def.section->output_offset); | |
2617 | } | |
df406460 NC |
2618 | |
2619 | /* For simplicity of coding, we are going to modify the section | |
07d6d2b8 AM |
2620 | contents, the section relocs, and the BFD symbol table. We |
2621 | must tell the rest of the code not to free up this | |
2622 | information. It would be possible to instead create a table | |
2623 | of changes which have to be made, as is done in coff-mips.c; | |
2624 | that would be more work, but would require less memory when | |
2625 | the linker is run. */ | |
df406460 | 2626 | switch (ELF32_R_TYPE (irel->r_info)) |
07d6d2b8 | 2627 | { |
91d6fa6a NC |
2628 | /* Try to turn a 22-bit absolute call/jump into an 13-bit |
2629 | pc-relative rcall/rjmp. */ | |
2630 | case R_AVR_CALL: | |
07d6d2b8 AM |
2631 | { |
2632 | bfd_vma value = symval + irel->r_addend; | |
2633 | bfd_vma dot, gap; | |
2634 | int distance_short_enough = 0; | |
2635 | ||
2636 | /* Get the address of this instruction. */ | |
2637 | dot = (sec->output_section->vma | |
2638 | + sec->output_offset + irel->r_offset); | |
2639 | ||
2640 | /* Compute the distance from this insn to the branch target. */ | |
2641 | gap = value - dot; | |
2642 | ||
7622049e SKS |
2643 | /* The ISA manual states that addressable range is PC - 2k + 1 to |
2644 | PC + 2k. In bytes, that would be -4094 <= PC <= 4096. The range | |
2645 | is shifted one word to the right, because pc-relative instructions | |
2646 | implicitly add one word i.e. rjmp 0 jumps to next insn, not the | |
2647 | current one. | |
2648 | Therefore, for the !shrinkable case, the range is as above. | |
2649 | If shrinkable, then the current code only deletes bytes 3 and | |
2650 | 4 of the absolute call/jmp, so the forward jump range increases | |
2651 | by 2 bytes, but the backward (negative) jump range remains | |
2652 | the same. */ | |
2653 | ||
2654 | ||
07d6d2b8 AM |
2655 | /* Check if the gap falls in the range that can be accommodated |
2656 | in 13bits signed (It is 12bits when encoded, as we deal with | |
2657 | word addressing). */ | |
7622049e | 2658 | if (!shrinkable && ((int) gap >= -4094 && (int) gap <= 4096)) |
07d6d2b8 AM |
2659 | distance_short_enough = 1; |
2660 | /* If shrinkable, then we can check for a range of distance which | |
7622049e | 2661 | is two bytes farther on the positive direction because the call |
07d6d2b8 AM |
2662 | or jump target will be closer by two bytes after the |
2663 | relaxation. */ | |
7622049e | 2664 | else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4098)) |
07d6d2b8 AM |
2665 | distance_short_enough = 1; |
2666 | ||
2667 | /* Here we handle the wrap-around case. E.g. for a 16k device | |
2668 | we could use a rjmp to jump from address 0x100 to 0x3d00! | |
2669 | In order to make this work properly, we need to fill the | |
2670 | vaiable avr_pc_wrap_around with the appropriate value. | |
2671 | I.e. 0x4000 for a 16k device. */ | |
2672 | { | |
91d6fa6a NC |
2673 | /* Shrinking the code size makes the gaps larger in the |
2674 | case of wrap-arounds. So we use a heuristical safety | |
2675 | margin to avoid that during relax the distance gets | |
2676 | again too large for the short jumps. Let's assume | |
2677 | a typical code-size reduction due to relax for a | |
2678 | 16k device of 600 bytes. So let's use twice the | |
2679 | typical value as safety margin. */ | |
2680 | int rgap; | |
2681 | int safety_margin; | |
2682 | ||
2683 | int assumed_shrink = 600; | |
2684 | if (avr_pc_wrap_around > 0x4000) | |
2685 | assumed_shrink = 900; | |
2686 | ||
2687 | safety_margin = 2 * assumed_shrink; | |
2688 | ||
2689 | rgap = avr_relative_distance_considering_wrap_around (gap); | |
2690 | ||
2691 | if (rgap >= (-4092 + safety_margin) | |
2692 | && rgap <= (4094 - safety_margin)) | |
2693 | distance_short_enough = 1; | |
07d6d2b8 AM |
2694 | } |
2695 | ||
2696 | if (distance_short_enough) | |
2697 | { | |
2698 | unsigned char code_msb; | |
2699 | unsigned char code_lsb; | |
2700 | ||
2701 | if (debug_relax) | |
2702 | printf ("shrinking jump/call instruction at address 0x%x" | |
2703 | " in section %s\n\n", | |
2704 | (int) dot, sec->name); | |
2705 | ||
2706 | /* Note that we've changed the relocs, section contents, | |
2707 | etc. */ | |
2708 | elf_section_data (sec)->relocs = internal_relocs; | |
2709 | elf_section_data (sec)->this_hdr.contents = contents; | |
2710 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
2711 | ||
2712 | /* Get the instruction code for relaxing. */ | |
2713 | code_lsb = bfd_get_8 (abfd, contents + irel->r_offset); | |
2714 | code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1); | |
2715 | ||
2716 | /* Mask out the relocation bits. */ | |
2717 | code_msb &= 0x94; | |
2718 | code_lsb &= 0x0E; | |
2719 | if (code_msb == 0x94 && code_lsb == 0x0E) | |
2720 | { | |
2721 | /* we are changing call -> rcall . */ | |
2722 | bfd_put_8 (abfd, 0x00, contents + irel->r_offset); | |
2723 | bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1); | |
2724 | } | |
2725 | else if (code_msb == 0x94 && code_lsb == 0x0C) | |
2726 | { | |
2727 | /* we are changeing jump -> rjmp. */ | |
2728 | bfd_put_8 (abfd, 0x00, contents + irel->r_offset); | |
2729 | bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1); | |
2730 | } | |
2731 | else | |
2732 | abort (); | |
2733 | ||
2734 | /* Fix the relocation's type. */ | |
2735 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
2736 | R_AVR_13_PCREL); | |
2737 | ||
2738 | /* We should not modify the ordering if 'shrinkable' is | |
2739 | FALSE. */ | |
2740 | if (!shrinkable) | |
2741 | { | |
2742 | /* Let's insert a nop. */ | |
2743 | bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2); | |
2744 | bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3); | |
2745 | } | |
2746 | else | |
2747 | { | |
2748 | /* Delete two bytes of data. */ | |
2749 | if (!elf32_avr_relax_delete_bytes (abfd, sec, | |
2750 | irel->r_offset + 2, 2, | |
2751 | TRUE)) | |
2752 | goto error_return; | |
2753 | ||
2754 | /* That will change things, so, we should relax again. | |
2755 | Note that this is not required, and it may be slow. */ | |
2756 | *again = TRUE; | |
2757 | } | |
2758 | } | |
2759 | } | |
1a0670f3 | 2760 | /* Fall through. */ |
4cdc7696 | 2761 | |
07d6d2b8 AM |
2762 | default: |
2763 | { | |
2764 | unsigned char code_msb; | |
2765 | unsigned char code_lsb; | |
2766 | bfd_vma dot; | |
2767 | ||
2768 | code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1); | |
2769 | code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0); | |
2770 | ||
2771 | /* Get the address of this instruction. */ | |
2772 | dot = (sec->output_section->vma | |
2773 | + sec->output_offset + irel->r_offset); | |
2774 | ||
2775 | /* Here we look for rcall/ret or call/ret sequences that could be | |
2776 | safely replaced by rjmp/ret or jmp/ret. */ | |
2777 | if (((code_msb & 0xf0) == 0xd0) | |
2778 | && avr_replace_call_ret_sequences) | |
2779 | { | |
2780 | /* This insn is a rcall. */ | |
2781 | unsigned char next_insn_msb = 0; | |
2782 | unsigned char next_insn_lsb = 0; | |
2783 | ||
2784 | if (irel->r_offset + 3 < sec->size) | |
2785 | { | |
2786 | next_insn_msb = | |
91d6fa6a | 2787 | bfd_get_8 (abfd, contents + irel->r_offset + 3); |
07d6d2b8 | 2788 | next_insn_lsb = |
91d6fa6a | 2789 | bfd_get_8 (abfd, contents + irel->r_offset + 2); |
07d6d2b8 | 2790 | } |
4cdc7696 NC |
2791 | |
2792 | if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) | |
07d6d2b8 AM |
2793 | { |
2794 | /* The next insn is a ret. We now convert the rcall insn | |
2795 | into a rjmp instruction. */ | |
2796 | code_msb &= 0xef; | |
2797 | bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1); | |
2798 | if (debug_relax) | |
2799 | printf ("converted rcall/ret sequence at address 0x%x" | |
2800 | " into rjmp/ret sequence. Section is %s\n\n", | |
2801 | (int) dot, sec->name); | |
2802 | *again = TRUE; | |
2803 | break; | |
2804 | } | |
2805 | } | |
2806 | else if ((0x94 == (code_msb & 0xfe)) | |
28c9d252 NC |
2807 | && (0x0e == (code_lsb & 0x0e)) |
2808 | && avr_replace_call_ret_sequences) | |
07d6d2b8 AM |
2809 | { |
2810 | /* This insn is a call. */ | |
2811 | unsigned char next_insn_msb = 0; | |
2812 | unsigned char next_insn_lsb = 0; | |
2813 | ||
2814 | if (irel->r_offset + 5 < sec->size) | |
2815 | { | |
2816 | next_insn_msb = | |
91d6fa6a | 2817 | bfd_get_8 (abfd, contents + irel->r_offset + 5); |
07d6d2b8 | 2818 | next_insn_lsb = |
91d6fa6a | 2819 | bfd_get_8 (abfd, contents + irel->r_offset + 4); |
07d6d2b8 AM |
2820 | } |
2821 | ||
2822 | if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) | |
2823 | { | |
2824 | /* The next insn is a ret. We now convert the call insn | |
2825 | into a jmp instruction. */ | |
2826 | ||
2827 | code_lsb &= 0xfd; | |
2828 | bfd_put_8 (abfd, code_lsb, contents + irel->r_offset); | |
2829 | if (debug_relax) | |
2830 | printf ("converted call/ret sequence at address 0x%x" | |
2831 | " into jmp/ret sequence. Section is %s\n\n", | |
2832 | (int) dot, sec->name); | |
2833 | *again = TRUE; | |
2834 | break; | |
2835 | } | |
2836 | } | |
2837 | else if ((0xc0 == (code_msb & 0xf0)) | |
2838 | || ((0x94 == (code_msb & 0xfe)) | |
2839 | && (0x0c == (code_lsb & 0x0e)))) | |
2840 | { | |
2841 | /* This insn is a rjmp or a jmp. */ | |
2842 | unsigned char next_insn_msb = 0; | |
2843 | unsigned char next_insn_lsb = 0; | |
2844 | int insn_size; | |
2845 | ||
2846 | if (0xc0 == (code_msb & 0xf0)) | |
2847 | insn_size = 2; /* rjmp insn */ | |
2848 | else | |
2849 | insn_size = 4; /* jmp insn */ | |
2850 | ||
2851 | if (irel->r_offset + insn_size + 1 < sec->size) | |
2852 | { | |
2853 | next_insn_msb = | |
91d6fa6a NC |
2854 | bfd_get_8 (abfd, contents + irel->r_offset |
2855 | + insn_size + 1); | |
07d6d2b8 | 2856 | next_insn_lsb = |
91d6fa6a NC |
2857 | bfd_get_8 (abfd, contents + irel->r_offset |
2858 | + insn_size); | |
07d6d2b8 | 2859 | } |
cc643b88 | 2860 | |
07d6d2b8 AM |
2861 | if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb)) |
2862 | { | |
2863 | /* The next insn is a ret. We possibly could delete | |
2864 | this ret. First we need to check for preceding | |
2865 | sbis/sbic/sbrs or cpse "skip" instructions. */ | |
2866 | ||
2867 | int there_is_preceding_non_skip_insn = 1; | |
2868 | bfd_vma address_of_ret; | |
2869 | ||
2870 | address_of_ret = dot + insn_size; | |
2871 | ||
2872 | if (debug_relax && (insn_size == 2)) | |
2873 | printf ("found rjmp / ret sequence at address 0x%x\n", | |
2874 | (int) dot); | |
2875 | if (debug_relax && (insn_size == 4)) | |
2876 | printf ("found jmp / ret sequence at address 0x%x\n", | |
2877 | (int) dot); | |
2878 | ||
2879 | /* We have to make sure that there is a preceding insn. */ | |
2880 | if (irel->r_offset >= 2) | |
2881 | { | |
2882 | unsigned char preceding_msb; | |
2883 | unsigned char preceding_lsb; | |
2884 | ||
2885 | preceding_msb = | |
91d6fa6a | 2886 | bfd_get_8 (abfd, contents + irel->r_offset - 1); |
07d6d2b8 | 2887 | preceding_lsb = |
91d6fa6a | 2888 | bfd_get_8 (abfd, contents + irel->r_offset - 2); |
df406460 | 2889 | |
07d6d2b8 AM |
2890 | /* sbic. */ |
2891 | if (0x99 == preceding_msb) | |
2892 | there_is_preceding_non_skip_insn = 0; | |
df406460 | 2893 | |
07d6d2b8 AM |
2894 | /* sbis. */ |
2895 | if (0x9b == preceding_msb) | |
2896 | there_is_preceding_non_skip_insn = 0; | |
df406460 | 2897 | |
07d6d2b8 AM |
2898 | /* sbrc */ |
2899 | if ((0xfc == (preceding_msb & 0xfe) | |
cc643b88 | 2900 | && (0x00 == (preceding_lsb & 0x08)))) |
07d6d2b8 | 2901 | there_is_preceding_non_skip_insn = 0; |
df406460 | 2902 | |
07d6d2b8 AM |
2903 | /* sbrs */ |
2904 | if ((0xfe == (preceding_msb & 0xfe) | |
cc643b88 | 2905 | && (0x00 == (preceding_lsb & 0x08)))) |
07d6d2b8 AM |
2906 | there_is_preceding_non_skip_insn = 0; |
2907 | ||
2908 | /* cpse */ | |
2909 | if (0x10 == (preceding_msb & 0xfc)) | |
2910 | there_is_preceding_non_skip_insn = 0; | |
2911 | ||
2912 | if (there_is_preceding_non_skip_insn == 0) | |
2913 | if (debug_relax) | |
2914 | printf ("preceding skip insn prevents deletion of" | |
2915 | " ret insn at Addy 0x%x in section %s\n", | |
2916 | (int) dot + 2, sec->name); | |
2917 | } | |
2918 | else | |
2919 | { | |
2920 | /* There is no previous instruction. */ | |
2921 | there_is_preceding_non_skip_insn = 0; | |
2922 | } | |
2923 | ||
2924 | if (there_is_preceding_non_skip_insn) | |
2925 | { | |
2926 | /* We now only have to make sure that there is no | |
2927 | local label defined at the address of the ret | |
2928 | instruction and that there is no local relocation | |
2929 | in this section pointing to the ret. */ | |
2930 | ||
2931 | int deleting_ret_is_safe = 1; | |
2932 | unsigned int section_offset_of_ret_insn = | |
91d6fa6a | 2933 | irel->r_offset + insn_size; |
07d6d2b8 AM |
2934 | Elf_Internal_Sym *isym, *isymend; |
2935 | unsigned int sec_shndx; | |
8d6a12ee | 2936 | struct bfd_section *isec; |
4cdc7696 | 2937 | |
07d6d2b8 | 2938 | sec_shndx = |
4cdc7696 | 2939 | _bfd_elf_section_from_bfd_section (abfd, sec); |
df406460 | 2940 | |
07d6d2b8 AM |
2941 | /* Check for local symbols. */ |
2942 | isym = (Elf_Internal_Sym *) symtab_hdr->contents; | |
2943 | isymend = isym + symtab_hdr->sh_info; | |
696b7ad2 | 2944 | /* PR 6019: There may not be any local symbols. */ |
07d6d2b8 | 2945 | for (; isym != NULL && isym < isymend; isym++) |
91d6fa6a NC |
2946 | { |
2947 | if (isym->st_value == section_offset_of_ret_insn | |
2948 | && isym->st_shndx == sec_shndx) | |
2949 | { | |
2950 | deleting_ret_is_safe = 0; | |
2951 | if (debug_relax) | |
2952 | printf ("local label prevents deletion of ret " | |
2953 | "insn at address 0x%x\n", | |
2954 | (int) dot + insn_size); | |
2955 | } | |
2956 | } | |
2957 | ||
2958 | /* Now check for global symbols. */ | |
2959 | { | |
2960 | int symcount; | |
2961 | struct elf_link_hash_entry **sym_hashes; | |
2962 | struct elf_link_hash_entry **end_hashes; | |
2963 | ||
2964 | symcount = (symtab_hdr->sh_size | |
2965 | / sizeof (Elf32_External_Sym) | |
2966 | - symtab_hdr->sh_info); | |
2967 | sym_hashes = elf_sym_hashes (abfd); | |
2968 | end_hashes = sym_hashes + symcount; | |
2969 | for (; sym_hashes < end_hashes; sym_hashes++) | |
2970 | { | |
2971 | struct elf_link_hash_entry *sym_hash = | |
2972 | *sym_hashes; | |
2973 | if ((sym_hash->root.type == bfd_link_hash_defined | |
2974 | || sym_hash->root.type == | |
4cdc7696 | 2975 | bfd_link_hash_defweak) |
91d6fa6a NC |
2976 | && sym_hash->root.u.def.section == sec |
2977 | && sym_hash->root.u.def.value == section_offset_of_ret_insn) | |
2978 | { | |
2979 | deleting_ret_is_safe = 0; | |
2980 | if (debug_relax) | |
2981 | printf ("global label prevents deletion of " | |
2982 | "ret insn at address 0x%x\n", | |
2983 | (int) dot + insn_size); | |
2984 | } | |
2985 | } | |
2986 | } | |
91d6fa6a | 2987 | |
8d6a12ee NC |
2988 | /* Now we check for relocations pointing to ret. */ |
2989 | for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next) | |
2990 | { | |
2991 | Elf_Internal_Rela *rel; | |
2992 | Elf_Internal_Rela *relend; | |
f36e8886 | 2993 | |
8d6a12ee NC |
2994 | rel = elf_section_data (isec)->relocs; |
2995 | if (rel == NULL) | |
2996 | rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE); | |
91d6fa6a | 2997 | |
8d6a12ee | 2998 | relend = rel + isec->reloc_count; |
91d6fa6a | 2999 | |
8d6a12ee NC |
3000 | for (; rel && rel < relend; rel++) |
3001 | { | |
3002 | bfd_vma reloc_target = 0; | |
3003 | ||
3004 | /* Read this BFD's local symbols if we haven't | |
3005 | done so already. */ | |
3006 | if (isymbuf == NULL && symtab_hdr->sh_info != 0) | |
3007 | { | |
3008 | isymbuf = (Elf_Internal_Sym *) | |
3009 | symtab_hdr->contents; | |
3010 | if (isymbuf == NULL) | |
3011 | isymbuf = bfd_elf_get_elf_syms | |
3012 | (abfd, | |
3013 | symtab_hdr, | |
3014 | symtab_hdr->sh_info, 0, | |
3015 | NULL, NULL, NULL); | |
3016 | if (isymbuf == NULL) | |
3017 | break; | |
3018 | } | |
3019 | ||
3020 | /* Get the value of the symbol referred to | |
3021 | by the reloc. */ | |
3022 | if (ELF32_R_SYM (rel->r_info) | |
3023 | < symtab_hdr->sh_info) | |
3024 | { | |
3025 | /* A local symbol. */ | |
3026 | asection *sym_sec; | |
3027 | ||
3028 | isym = isymbuf | |
3029 | + ELF32_R_SYM (rel->r_info); | |
3030 | sym_sec = bfd_section_from_elf_index | |
3031 | (abfd, isym->st_shndx); | |
3032 | symval = isym->st_value; | |
3033 | ||
3034 | /* If the reloc is absolute, it will not | |
3035 | have a symbol or section associated | |
3036 | with it. */ | |
3037 | ||
3038 | if (sym_sec) | |
3039 | { | |
3040 | symval += | |
3041 | sym_sec->output_section->vma | |
3042 | + sym_sec->output_offset; | |
3043 | reloc_target = symval + rel->r_addend; | |
3044 | } | |
3045 | else | |
3046 | { | |
3047 | reloc_target = symval + rel->r_addend; | |
3048 | /* Reference symbol is absolute. */ | |
3049 | } | |
3050 | } | |
3051 | /* else ... reference symbol is extern. */ | |
3052 | ||
3053 | if (address_of_ret == reloc_target) | |
3054 | { | |
3055 | deleting_ret_is_safe = 0; | |
3056 | if (debug_relax) | |
3057 | printf ("ret from " | |
3058 | "rjmp/jmp ret sequence at address" | |
3059 | " 0x%x could not be deleted. ret" | |
3060 | " is target of a relocation.\n", | |
3061 | (int) address_of_ret); | |
91d6fa6a | 3062 | break; |
8d6a12ee NC |
3063 | } |
3064 | } | |
3065 | } | |
91d6fa6a NC |
3066 | |
3067 | if (deleting_ret_is_safe) | |
3068 | { | |
3069 | if (debug_relax) | |
3070 | printf ("unreachable ret instruction " | |
3071 | "at address 0x%x deleted.\n", | |
3072 | (int) dot + insn_size); | |
3073 | ||
3074 | /* Delete two bytes of data. */ | |
3075 | if (!elf32_avr_relax_delete_bytes (abfd, sec, | |
bf186506 SKS |
3076 | irel->r_offset + insn_size, 2, |
3077 | TRUE)) | |
91d6fa6a NC |
3078 | goto error_return; |
3079 | ||
3080 | /* That will change things, so, we should relax | |
3081 | again. Note that this is not required, and it | |
3082 | may be slow. */ | |
3083 | *again = TRUE; | |
3084 | break; | |
3085 | } | |
07d6d2b8 AM |
3086 | } |
3087 | } | |
3088 | } | |
3089 | break; | |
3090 | } | |
3091 | } | |
df406460 NC |
3092 | } |
3093 | ||
bac13f5a AB |
3094 | if (!*again) |
3095 | { | |
3096 | /* Look through all the property records in this section to see if | |
07d6d2b8 | 3097 | there's any alignment records that can be moved. */ |
bac13f5a AB |
3098 | struct avr_relax_info *relax_info; |
3099 | ||
3100 | relax_info = get_avr_relax_info (sec); | |
3101 | if (relax_info->records.count > 0) | |
07d6d2b8 AM |
3102 | { |
3103 | unsigned int i; | |
3104 | ||
3105 | for (i = 0; i < relax_info->records.count; ++i) | |
3106 | { | |
3107 | switch (relax_info->records.items [i].type) | |
3108 | { | |
3109 | case RECORD_ORG: | |
3110 | case RECORD_ORG_AND_FILL: | |
3111 | break; | |
3112 | case RECORD_ALIGN: | |
3113 | case RECORD_ALIGN_AND_FILL: | |
3114 | { | |
3115 | struct avr_property_record *record; | |
3116 | unsigned long bytes_to_align; | |
3117 | int count = 0; | |
3118 | ||
3119 | /* Look for alignment directives that have had enough | |
3120 | bytes deleted before them, such that the directive | |
3121 | can be moved backwards and still maintain the | |
3122 | required alignment. */ | |
3123 | record = &relax_info->records.items [i]; | |
3124 | bytes_to_align | |
3125 | = (unsigned long) (1 << record->data.align.bytes); | |
3126 | while (record->data.align.preceding_deleted >= | |
3127 | bytes_to_align) | |
3128 | { | |
3129 | record->data.align.preceding_deleted | |
3130 | -= bytes_to_align; | |
3131 | count += bytes_to_align; | |
3132 | } | |
3133 | ||
3134 | if (count > 0) | |
3135 | { | |
3136 | bfd_vma addr = record->offset; | |
3137 | ||
3138 | /* We can delete COUNT bytes and this alignment | |
3139 | directive will still be correctly aligned. | |
3140 | First move the alignment directive, then delete | |
3141 | the bytes. */ | |
3142 | record->offset -= count; | |
3143 | elf32_avr_relax_delete_bytes (abfd, sec, | |
3144 | addr - count, | |
3145 | count, FALSE); | |
3146 | *again = TRUE; | |
3147 | } | |
3148 | } | |
3149 | break; | |
3150 | } | |
3151 | } | |
3152 | } | |
bac13f5a AB |
3153 | } |
3154 | ||
df406460 NC |
3155 | if (contents != NULL |
3156 | && elf_section_data (sec)->this_hdr.contents != contents) | |
3157 | { | |
3158 | if (! link_info->keep_memory) | |
07d6d2b8 | 3159 | free (contents); |
df406460 | 3160 | else |
07d6d2b8 AM |
3161 | { |
3162 | /* Cache the section contents for elf_link_input_bfd. */ | |
3163 | elf_section_data (sec)->this_hdr.contents = contents; | |
3164 | } | |
df406460 NC |
3165 | } |
3166 | ||
c9594989 | 3167 | if (elf_section_data (sec)->relocs != internal_relocs) |
df406460 NC |
3168 | free (internal_relocs); |
3169 | ||
3170 | return TRUE; | |
3171 | ||
3172 | error_return: | |
c9594989 | 3173 | if (symtab_hdr->contents != (unsigned char *) isymbuf) |
df406460 | 3174 | free (isymbuf); |
c9594989 | 3175 | if (elf_section_data (sec)->this_hdr.contents != contents) |
df406460 | 3176 | free (contents); |
c9594989 | 3177 | if (elf_section_data (sec)->relocs != internal_relocs) |
df406460 NC |
3178 | free (internal_relocs); |
3179 | ||
4cdc7696 | 3180 | return FALSE; |
df406460 NC |
3181 | } |
3182 | ||
3183 | /* This is a version of bfd_generic_get_relocated_section_contents | |
4cdc7696 | 3184 | which uses elf32_avr_relocate_section. |
df406460 | 3185 | |
4cdc7696 | 3186 | For avr it's essentially a cut and paste taken from the H8300 port. |
df406460 | 3187 | The author of the relaxation support patch for avr had absolutely no |
4cdc7696 | 3188 | clue what is happening here but found out that this part of the code |
df406460 NC |
3189 | seems to be important. */ |
3190 | ||
3191 | static bfd_byte * | |
3192 | elf32_avr_get_relocated_section_contents (bfd *output_bfd, | |
07d6d2b8 AM |
3193 | struct bfd_link_info *link_info, |
3194 | struct bfd_link_order *link_order, | |
3195 | bfd_byte *data, | |
3196 | bfd_boolean relocatable, | |
3197 | asymbol **symbols) | |
df406460 NC |
3198 | { |
3199 | Elf_Internal_Shdr *symtab_hdr; | |
3200 | asection *input_section = link_order->u.indirect.section; | |
3201 | bfd *input_bfd = input_section->owner; | |
3202 | asection **sections = NULL; | |
3203 | Elf_Internal_Rela *internal_relocs = NULL; | |
3204 | Elf_Internal_Sym *isymbuf = NULL; | |
3205 | ||
3206 | /* We only need to handle the case of relaxing, or of having a | |
3207 | particular set of section contents, specially. */ | |
3208 | if (relocatable | |
3209 | || elf_section_data (input_section)->this_hdr.contents == NULL) | |
3210 | return bfd_generic_get_relocated_section_contents (output_bfd, link_info, | |
07d6d2b8 AM |
3211 | link_order, data, |
3212 | relocatable, | |
3213 | symbols); | |
df406460 NC |
3214 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
3215 | ||
3216 | memcpy (data, elf_section_data (input_section)->this_hdr.contents, | |
07d6d2b8 | 3217 | (size_t) input_section->size); |
df406460 NC |
3218 | |
3219 | if ((input_section->flags & SEC_RELOC) != 0 | |
3220 | && input_section->reloc_count > 0) | |
3221 | { | |
3222 | asection **secpp; | |
3223 | Elf_Internal_Sym *isym, *isymend; | |
3224 | bfd_size_type amt; | |
3225 | ||
3226 | internal_relocs = (_bfd_elf_link_read_relocs | |
07d6d2b8 | 3227 | (input_bfd, input_section, NULL, NULL, FALSE)); |
df406460 | 3228 | if (internal_relocs == NULL) |
07d6d2b8 | 3229 | goto error_return; |
df406460 NC |
3230 | |
3231 | if (symtab_hdr->sh_info != 0) | |
07d6d2b8 AM |
3232 | { |
3233 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
3234 | if (isymbuf == NULL) | |
3235 | isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, | |
3236 | symtab_hdr->sh_info, 0, | |
3237 | NULL, NULL, NULL); | |
3238 | if (isymbuf == NULL) | |
3239 | goto error_return; | |
3240 | } | |
df406460 NC |
3241 | |
3242 | amt = symtab_hdr->sh_info; | |
3243 | amt *= sizeof (asection *); | |
4cdc7696 | 3244 | sections = bfd_malloc (amt); |
df406460 | 3245 | if (sections == NULL && amt != 0) |
07d6d2b8 | 3246 | goto error_return; |
df406460 NC |
3247 | |
3248 | isymend = isymbuf + symtab_hdr->sh_info; | |
3249 | for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp) | |
07d6d2b8 AM |
3250 | { |
3251 | asection *isec; | |
3252 | ||
3253 | if (isym->st_shndx == SHN_UNDEF) | |
3254 | isec = bfd_und_section_ptr; | |
3255 | else if (isym->st_shndx == SHN_ABS) | |
3256 | isec = bfd_abs_section_ptr; | |
3257 | else if (isym->st_shndx == SHN_COMMON) | |
3258 | isec = bfd_com_section_ptr; | |
3259 | else | |
3260 | isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx); | |
3261 | ||
3262 | *secpp = isec; | |
3263 | } | |
df406460 NC |
3264 | |
3265 | if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd, | |
07d6d2b8 AM |
3266 | input_section, data, internal_relocs, |
3267 | isymbuf, sections)) | |
3268 | goto error_return; | |
df406460 | 3269 | |
c9594989 AM |
3270 | free (sections); |
3271 | if (symtab_hdr->contents != (unsigned char *) isymbuf) | |
07d6d2b8 | 3272 | free (isymbuf); |
df406460 | 3273 | if (elf_section_data (input_section)->relocs != internal_relocs) |
07d6d2b8 | 3274 | free (internal_relocs); |
df406460 NC |
3275 | } |
3276 | ||
3277 | return data; | |
3278 | ||
3279 | error_return: | |
c9594989 AM |
3280 | free (sections); |
3281 | if (symtab_hdr->contents != (unsigned char *) isymbuf) | |
df406460 | 3282 | free (isymbuf); |
c9594989 | 3283 | if (elf_section_data (input_section)->relocs != internal_relocs) |
df406460 NC |
3284 | free (internal_relocs); |
3285 | return NULL; | |
3286 | } | |
3287 | ||
3288 | ||
28c9d252 NC |
3289 | /* Determines the hash entry name for a particular reloc. It consists of |
3290 | the identifier of the symbol section and the added reloc addend and | |
3291 | symbol offset relative to the section the symbol is attached to. */ | |
3292 | ||
3293 | static char * | |
3294 | avr_stub_name (const asection *symbol_section, | |
07d6d2b8 AM |
3295 | const bfd_vma symbol_offset, |
3296 | const Elf_Internal_Rela *rela) | |
28c9d252 NC |
3297 | { |
3298 | char *stub_name; | |
3299 | bfd_size_type len; | |
3300 | ||
3301 | len = 8 + 1 + 8 + 1 + 1; | |
3302 | stub_name = bfd_malloc (len); | |
4a9f7d65 NC |
3303 | if (stub_name != NULL) |
3304 | sprintf (stub_name, "%08x+%08x", | |
3305 | symbol_section->id & 0xffffffff, | |
3306 | (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset)); | |
28c9d252 NC |
3307 | |
3308 | return stub_name; | |
3309 | } | |
3310 | ||
3311 | ||
3312 | /* Add a new stub entry to the stub hash. Not all fields of the new | |
3313 | stub entry are initialised. */ | |
3314 | ||
3315 | static struct elf32_avr_stub_hash_entry * | |
3316 | avr_add_stub (const char *stub_name, | |
07d6d2b8 | 3317 | struct elf32_avr_link_hash_table *htab) |
28c9d252 NC |
3318 | { |
3319 | struct elf32_avr_stub_hash_entry *hsh; | |
3320 | ||
3321 | /* Enter this entry into the linker stub hash table. */ | |
3322 | hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE); | |
3323 | ||
3324 | if (hsh == NULL) | |
3325 | { | |
695344c0 | 3326 | /* xgettext:c-format */ |
10463f39 | 3327 | _bfd_error_handler (_("cannot create stub entry %s"), stub_name); |
28c9d252 NC |
3328 | return NULL; |
3329 | } | |
3330 | ||
3331 | hsh->stub_offset = 0; | |
3332 | return hsh; | |
3333 | } | |
3334 | ||
3335 | /* We assume that there is already space allocated for the stub section | |
3336 | contents and that before building the stubs the section size is | |
3337 | initialized to 0. We assume that within the stub hash table entry, | |
3338 | the absolute position of the jmp target has been written in the | |
3339 | target_value field. We write here the offset of the generated jmp insn | |
3340 | relative to the trampoline section start to the stub_offset entry in | |
3341 | the stub hash table entry. */ | |
3342 | ||
3343 | static bfd_boolean | |
3344 | avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg) | |
3345 | { | |
3346 | struct elf32_avr_stub_hash_entry *hsh; | |
3347 | struct bfd_link_info *info; | |
3348 | struct elf32_avr_link_hash_table *htab; | |
3349 | bfd *stub_bfd; | |
3350 | bfd_byte *loc; | |
3351 | bfd_vma target; | |
3352 | bfd_vma starget; | |
3353 | ||
3354 | /* Basic opcode */ | |
3355 | bfd_vma jmp_insn = 0x0000940c; | |
3356 | ||
3357 | /* Massage our args to the form they really have. */ | |
3358 | hsh = avr_stub_hash_entry (bh); | |
3359 | ||
3360 | if (!hsh->is_actually_needed) | |
3361 | return TRUE; | |
3362 | ||
3363 | info = (struct bfd_link_info *) in_arg; | |
3364 | ||
3365 | htab = avr_link_hash_table (info); | |
64ee10b6 NC |
3366 | if (htab == NULL) |
3367 | return FALSE; | |
28c9d252 NC |
3368 | |
3369 | target = hsh->target_value; | |
3370 | ||
3371 | /* Make a note of the offset within the stubs for this entry. */ | |
3372 | hsh->stub_offset = htab->stub_sec->size; | |
3373 | loc = htab->stub_sec->contents + hsh->stub_offset; | |
3374 | ||
3375 | stub_bfd = htab->stub_sec->owner; | |
3376 | ||
3377 | if (debug_stubs) | |
3378 | printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n", | |
07d6d2b8 AM |
3379 | (unsigned int) target, |
3380 | (unsigned int) hsh->stub_offset); | |
28c9d252 NC |
3381 | |
3382 | /* We now have to add the information on the jump target to the bare | |
3383 | opcode bits already set in jmp_insn. */ | |
3384 | ||
3385 | /* Check for the alignment of the address. */ | |
3386 | if (target & 1) | |
3387 | return FALSE; | |
3388 | ||
3389 | starget = target >> 1; | |
3390 | jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16; | |
3391 | bfd_put_16 (stub_bfd, jmp_insn, loc); | |
3392 | bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2); | |
3393 | ||
3394 | htab->stub_sec->size += 4; | |
3395 | ||
3396 | /* Now add the entries in the address mapping table if there is still | |
3397 | space left. */ | |
3398 | { | |
3399 | unsigned int nr; | |
3400 | ||
3401 | nr = htab->amt_entry_cnt + 1; | |
3402 | if (nr <= htab->amt_max_entry_cnt) | |
3403 | { | |
07d6d2b8 | 3404 | htab->amt_entry_cnt = nr; |
28c9d252 | 3405 | |
07d6d2b8 AM |
3406 | htab->amt_stub_offsets[nr - 1] = hsh->stub_offset; |
3407 | htab->amt_destination_addr[nr - 1] = target; | |
28c9d252 NC |
3408 | } |
3409 | } | |
3410 | ||
3411 | return TRUE; | |
3412 | } | |
3413 | ||
3414 | static bfd_boolean | |
3415 | avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh, | |
07d6d2b8 | 3416 | void *in_arg ATTRIBUTE_UNUSED) |
28c9d252 NC |
3417 | { |
3418 | struct elf32_avr_stub_hash_entry *hsh; | |
28c9d252 | 3419 | |
28c9d252 NC |
3420 | hsh = avr_stub_hash_entry (bh); |
3421 | hsh->is_actually_needed = FALSE; | |
3422 | ||
3423 | return TRUE; | |
3424 | } | |
3425 | ||
3426 | static bfd_boolean | |
3427 | avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg) | |
3428 | { | |
3429 | struct elf32_avr_stub_hash_entry *hsh; | |
3430 | struct elf32_avr_link_hash_table *htab; | |
3431 | int size; | |
3432 | ||
3433 | /* Massage our args to the form they really have. */ | |
3434 | hsh = avr_stub_hash_entry (bh); | |
3435 | htab = in_arg; | |
3436 | ||
3437 | if (hsh->is_actually_needed) | |
3438 | size = 4; | |
3439 | else | |
3440 | size = 0; | |
3441 | ||
3442 | htab->stub_sec->size += size; | |
3443 | return TRUE; | |
3444 | } | |
3445 | ||
3446 | void | |
3447 | elf32_avr_setup_params (struct bfd_link_info *info, | |
07d6d2b8 AM |
3448 | bfd *avr_stub_bfd, |
3449 | asection *avr_stub_section, | |
3450 | bfd_boolean no_stubs, | |
3451 | bfd_boolean deb_stubs, | |
3452 | bfd_boolean deb_relax, | |
3453 | bfd_vma pc_wrap_around, | |
3454 | bfd_boolean call_ret_replacement) | |
28c9d252 | 3455 | { |
64ee10b6 | 3456 | struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); |
28c9d252 | 3457 | |
64ee10b6 NC |
3458 | if (htab == NULL) |
3459 | return; | |
28c9d252 NC |
3460 | htab->stub_sec = avr_stub_section; |
3461 | htab->stub_bfd = avr_stub_bfd; | |
3462 | htab->no_stubs = no_stubs; | |
3463 | ||
3464 | debug_relax = deb_relax; | |
3465 | debug_stubs = deb_stubs; | |
3466 | avr_pc_wrap_around = pc_wrap_around; | |
3467 | avr_replace_call_ret_sequences = call_ret_replacement; | |
3468 | } | |
3469 | ||
3470 | ||
3471 | /* Set up various things so that we can make a list of input sections | |
3472 | for each output section included in the link. Returns -1 on error, | |
3473 | 0 when no stubs will be needed, and 1 on success. It also sets | |
3474 | information on the stubs bfd and the stub section in the info | |
3475 | struct. */ | |
3476 | ||
3477 | int | |
3478 | elf32_avr_setup_section_lists (bfd *output_bfd, | |
07d6d2b8 | 3479 | struct bfd_link_info *info) |
28c9d252 NC |
3480 | { |
3481 | bfd *input_bfd; | |
3482 | unsigned int bfd_count; | |
7292b3ac | 3483 | unsigned int top_id, top_index; |
28c9d252 NC |
3484 | asection *section; |
3485 | asection **input_list, **list; | |
986f0783 | 3486 | size_t amt; |
4dfe6ac6 | 3487 | struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); |
28c9d252 | 3488 | |
64ee10b6 | 3489 | if (htab == NULL || htab->no_stubs) |
28c9d252 NC |
3490 | return 0; |
3491 | ||
3492 | /* Count the number of input BFDs and find the top input section id. */ | |
3493 | for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0; | |
3494 | input_bfd != NULL; | |
c72f2fb2 | 3495 | input_bfd = input_bfd->link.next) |
28c9d252 NC |
3496 | { |
3497 | bfd_count += 1; | |
3498 | for (section = input_bfd->sections; | |
07d6d2b8 AM |
3499 | section != NULL; |
3500 | section = section->next) | |
28c9d252 NC |
3501 | if (top_id < section->id) |
3502 | top_id = section->id; | |
3503 | } | |
3504 | ||
3505 | htab->bfd_count = bfd_count; | |
3506 | ||
3507 | /* We can't use output_bfd->section_count here to find the top output | |
3508 | section index as some sections may have been removed, and | |
3509 | strip_excluded_output_sections doesn't renumber the indices. */ | |
3510 | for (section = output_bfd->sections, top_index = 0; | |
3511 | section != NULL; | |
3512 | section = section->next) | |
3513 | if (top_index < section->index) | |
3514 | top_index = section->index; | |
3515 | ||
3516 | htab->top_index = top_index; | |
3517 | amt = sizeof (asection *) * (top_index + 1); | |
3518 | input_list = bfd_malloc (amt); | |
3519 | htab->input_list = input_list; | |
3520 | if (input_list == NULL) | |
3521 | return -1; | |
3522 | ||
3523 | /* For sections we aren't interested in, mark their entries with a | |
3524 | value we can check later. */ | |
3525 | list = input_list + top_index; | |
3526 | do | |
3527 | *list = bfd_abs_section_ptr; | |
3528 | while (list-- != input_list); | |
3529 | ||
3530 | for (section = output_bfd->sections; | |
3531 | section != NULL; | |
3532 | section = section->next) | |
3533 | if ((section->flags & SEC_CODE) != 0) | |
3534 | input_list[section->index] = NULL; | |
3535 | ||
3536 | return 1; | |
3537 | } | |
3538 | ||
3539 | ||
3540 | /* Read in all local syms for all input bfds, and create hash entries | |
3541 | for export stubs if we are building a multi-subspace shared lib. | |
3542 | Returns -1 on error, 0 otherwise. */ | |
3543 | ||
3544 | static int | |
3545 | get_local_syms (bfd *input_bfd, struct bfd_link_info *info) | |
3546 | { | |
3547 | unsigned int bfd_indx; | |
3548 | Elf_Internal_Sym *local_syms, **all_local_syms; | |
3549 | struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info); | |
986f0783 | 3550 | size_t amt; |
28c9d252 | 3551 | |
64ee10b6 NC |
3552 | if (htab == NULL) |
3553 | return -1; | |
3554 | ||
28c9d252 NC |
3555 | /* We want to read in symbol extension records only once. To do this |
3556 | we need to read in the local symbols in parallel and save them for | |
3557 | later use; so hold pointers to the local symbols in an array. */ | |
9a008db3 | 3558 | amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count; |
28c9d252 NC |
3559 | all_local_syms = bfd_zmalloc (amt); |
3560 | htab->all_local_syms = all_local_syms; | |
3561 | if (all_local_syms == NULL) | |
3562 | return -1; | |
3563 | ||
3564 | /* Walk over all the input BFDs, swapping in local symbols. | |
3565 | If we are creating a shared library, create hash entries for the | |
3566 | export stubs. */ | |
3567 | for (bfd_indx = 0; | |
3568 | input_bfd != NULL; | |
c72f2fb2 | 3569 | input_bfd = input_bfd->link.next, bfd_indx++) |
28c9d252 NC |
3570 | { |
3571 | Elf_Internal_Shdr *symtab_hdr; | |
3572 | ||
3573 | /* We'll need the symbol table in a second. */ | |
3574 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
3575 | if (symtab_hdr->sh_info == 0) | |
3576 | continue; | |
3577 | ||
3578 | /* We need an array of the local symbols attached to the input bfd. */ | |
3579 | local_syms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
3580 | if (local_syms == NULL) | |
3581 | { | |
3582 | local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, | |
3583 | symtab_hdr->sh_info, 0, | |
3584 | NULL, NULL, NULL); | |
3585 | /* Cache them for elf_link_input_bfd. */ | |
3586 | symtab_hdr->contents = (unsigned char *) local_syms; | |
3587 | } | |
3588 | if (local_syms == NULL) | |
3589 | return -1; | |
3590 | ||
3591 | all_local_syms[bfd_indx] = local_syms; | |
3592 | } | |
3593 | ||
3594 | return 0; | |
3595 | } | |
3596 | ||
3597 | #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0 | |
3598 | ||
3599 | bfd_boolean | |
3600 | elf32_avr_size_stubs (bfd *output_bfd, | |
07d6d2b8 AM |
3601 | struct bfd_link_info *info, |
3602 | bfd_boolean is_prealloc_run) | |
28c9d252 | 3603 | { |
64ee10b6 NC |
3604 | struct elf32_avr_link_hash_table *htab; |
3605 | int stub_changed = 0; | |
28c9d252 | 3606 | |
64ee10b6 NC |
3607 | htab = avr_link_hash_table (info); |
3608 | if (htab == NULL) | |
3609 | return FALSE; | |
28c9d252 | 3610 | |
64ee10b6 NC |
3611 | /* At this point we initialize htab->vector_base |
3612 | To the start of the text output section. */ | |
3613 | htab->vector_base = htab->stub_sec->output_section->vma; | |
28c9d252 | 3614 | |
64ee10b6 NC |
3615 | if (get_local_syms (info->input_bfds, info)) |
3616 | { | |
3617 | if (htab->all_local_syms) | |
3618 | goto error_ret_free_local; | |
3619 | return FALSE; | |
3620 | } | |
28c9d252 NC |
3621 | |
3622 | if (ADD_DUMMY_STUBS_FOR_DEBUGGING) | |
3623 | { | |
3624 | struct elf32_avr_stub_hash_entry *test; | |
3625 | ||
3626 | test = avr_add_stub ("Hugo",htab); | |
3627 | test->target_value = 0x123456; | |
3628 | test->stub_offset = 13; | |
3629 | ||
3630 | test = avr_add_stub ("Hugo2",htab); | |
3631 | test->target_value = 0x84210; | |
3632 | test->stub_offset = 14; | |
3633 | } | |
3634 | ||
3635 | while (1) | |
3636 | { | |
3637 | bfd *input_bfd; | |
3638 | unsigned int bfd_indx; | |
3639 | ||
3640 | /* We will have to re-generate the stub hash table each time anything | |
07d6d2b8 | 3641 | in memory has changed. */ |
28c9d252 NC |
3642 | |
3643 | bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab); | |
3644 | for (input_bfd = info->input_bfds, bfd_indx = 0; | |
07d6d2b8 AM |
3645 | input_bfd != NULL; |
3646 | input_bfd = input_bfd->link.next, bfd_indx++) | |
3647 | { | |
3648 | Elf_Internal_Shdr *symtab_hdr; | |
3649 | asection *section; | |
3650 | Elf_Internal_Sym *local_syms; | |
3651 | ||
3652 | /* We'll need the symbol table in a second. */ | |
3653 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
3654 | if (symtab_hdr->sh_info == 0) | |
3655 | continue; | |
3656 | ||
3657 | local_syms = htab->all_local_syms[bfd_indx]; | |
3658 | ||
3659 | /* Walk over each section attached to the input bfd. */ | |
3660 | for (section = input_bfd->sections; | |
3661 | section != NULL; | |
3662 | section = section->next) | |
3663 | { | |
3664 | Elf_Internal_Rela *internal_relocs, *irelaend, *irela; | |
3665 | ||
3666 | /* If there aren't any relocs, then there's nothing more | |
3667 | to do. */ | |
3668 | if ((section->flags & SEC_RELOC) == 0 | |
3669 | || section->reloc_count == 0) | |
3670 | continue; | |
3671 | ||
3672 | /* If this section is a link-once section that will be | |
3673 | discarded, then don't create any stubs. */ | |
3674 | if (section->output_section == NULL | |
3675 | || section->output_section->owner != output_bfd) | |
3676 | continue; | |
3677 | ||
3678 | /* Get the relocs. */ | |
3679 | internal_relocs | |
3680 | = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL, | |
3681 | info->keep_memory); | |
3682 | if (internal_relocs == NULL) | |
3683 | goto error_ret_free_local; | |
3684 | ||
3685 | /* Now examine each relocation. */ | |
3686 | irela = internal_relocs; | |
3687 | irelaend = irela + section->reloc_count; | |
3688 | for (; irela < irelaend; irela++) | |
3689 | { | |
3690 | unsigned int r_type, r_indx; | |
3691 | struct elf32_avr_stub_hash_entry *hsh; | |
3692 | asection *sym_sec; | |
3693 | bfd_vma sym_value; | |
3694 | bfd_vma destination; | |
3695 | struct elf_link_hash_entry *hh; | |
3696 | char *stub_name; | |
3697 | ||
3698 | r_type = ELF32_R_TYPE (irela->r_info); | |
3699 | r_indx = ELF32_R_SYM (irela->r_info); | |
3700 | ||
3701 | /* Only look for 16 bit GS relocs. No other reloc will need a | |
3702 | stub. */ | |
3703 | if (!((r_type == R_AVR_16_PM) | |
3704 | || (r_type == R_AVR_LO8_LDI_GS) | |
3705 | || (r_type == R_AVR_HI8_LDI_GS))) | |
3706 | continue; | |
3707 | ||
3708 | /* Now determine the call target, its name, value, | |
3709 | section. */ | |
3710 | sym_sec = NULL; | |
3711 | sym_value = 0; | |
3712 | destination = 0; | |
3713 | hh = NULL; | |
3714 | if (r_indx < symtab_hdr->sh_info) | |
3715 | { | |
3716 | /* It's a local symbol. */ | |
3717 | Elf_Internal_Sym *sym; | |
3718 | Elf_Internal_Shdr *hdr; | |
4fbb74a6 | 3719 | unsigned int shndx; |
28c9d252 | 3720 | |
07d6d2b8 AM |
3721 | sym = local_syms + r_indx; |
3722 | if (ELF_ST_TYPE (sym->st_info) != STT_SECTION) | |
3723 | sym_value = sym->st_value; | |
4fbb74a6 AM |
3724 | shndx = sym->st_shndx; |
3725 | if (shndx < elf_numsections (input_bfd)) | |
3726 | { | |
3727 | hdr = elf_elfsections (input_bfd)[shndx]; | |
3728 | sym_sec = hdr->bfd_section; | |
3729 | destination = (sym_value + irela->r_addend | |
3730 | + sym_sec->output_offset | |
3731 | + sym_sec->output_section->vma); | |
3732 | } | |
07d6d2b8 AM |
3733 | } |
3734 | else | |
3735 | { | |
3736 | /* It's an external symbol. */ | |
3737 | int e_indx; | |
3738 | ||
3739 | e_indx = r_indx - symtab_hdr->sh_info; | |
3740 | hh = elf_sym_hashes (input_bfd)[e_indx]; | |
3741 | ||
3742 | while (hh->root.type == bfd_link_hash_indirect | |
3743 | || hh->root.type == bfd_link_hash_warning) | |
3744 | hh = (struct elf_link_hash_entry *) | |
3745 | (hh->root.u.i.link); | |
3746 | ||
3747 | if (hh->root.type == bfd_link_hash_defined | |
3748 | || hh->root.type == bfd_link_hash_defweak) | |
3749 | { | |
3750 | sym_sec = hh->root.u.def.section; | |
3751 | sym_value = hh->root.u.def.value; | |
3752 | if (sym_sec->output_section != NULL) | |
3753 | destination = (sym_value + irela->r_addend | |
3754 | + sym_sec->output_offset | |
3755 | + sym_sec->output_section->vma); | |
3756 | } | |
3757 | else if (hh->root.type == bfd_link_hash_undefweak) | |
3758 | { | |
3759 | if (! bfd_link_pic (info)) | |
3760 | continue; | |
3761 | } | |
3762 | else if (hh->root.type == bfd_link_hash_undefined) | |
3763 | { | |
3764 | if (! (info->unresolved_syms_in_objects == RM_IGNORE | |
3765 | && (ELF_ST_VISIBILITY (hh->other) | |
3766 | == STV_DEFAULT))) | |
3767 | continue; | |
3768 | } | |
3769 | else | |
3770 | { | |
3771 | bfd_set_error (bfd_error_bad_value); | |
3772 | ||
3773 | error_ret_free_internal: | |
3774 | if (elf_section_data (section)->relocs == NULL) | |
3775 | free (internal_relocs); | |
3776 | goto error_ret_free_local; | |
3777 | } | |
3778 | } | |
3779 | ||
3780 | if (! avr_stub_is_required_for_16_bit_reloc | |
28c9d252 | 3781 | (destination - htab->vector_base)) |
07d6d2b8 AM |
3782 | { |
3783 | if (!is_prealloc_run) | |
28c9d252 NC |
3784 | /* We are having a reloc that does't need a stub. */ |
3785 | continue; | |
3786 | ||
3787 | /* We don't right now know if a stub will be needed. | |
3788 | Let's rather be on the safe side. */ | |
07d6d2b8 AM |
3789 | } |
3790 | ||
3791 | /* Get the name of this stub. */ | |
3792 | stub_name = avr_stub_name (sym_sec, sym_value, irela); | |
3793 | ||
3794 | if (!stub_name) | |
3795 | goto error_ret_free_internal; | |
3796 | ||
3797 | ||
3798 | hsh = avr_stub_hash_lookup (&htab->bstab, | |
3799 | stub_name, | |
3800 | FALSE, FALSE); | |
3801 | if (hsh != NULL) | |
3802 | { | |
3803 | /* The proper stub has already been created. Mark it | |
3804 | to be used and write the possibly changed destination | |
3805 | value. */ | |
3806 | hsh->is_actually_needed = TRUE; | |
3807 | hsh->target_value = destination; | |
3808 | free (stub_name); | |
3809 | continue; | |
3810 | } | |
3811 | ||
3812 | hsh = avr_add_stub (stub_name, htab); | |
3813 | if (hsh == NULL) | |
3814 | { | |
3815 | free (stub_name); | |
3816 | goto error_ret_free_internal; | |
3817 | } | |
3818 | ||
3819 | hsh->is_actually_needed = TRUE; | |
3820 | hsh->target_value = destination; | |
3821 | ||
3822 | if (debug_stubs) | |
3823 | printf ("Adding stub with destination 0x%x to the" | |
3824 | " hash table.\n", (unsigned int) destination); | |
3825 | if (debug_stubs) | |
3826 | printf ("(Pre-Alloc run: %i)\n", is_prealloc_run); | |
3827 | ||
3828 | stub_changed = TRUE; | |
3829 | } | |
3830 | ||
3831 | /* We're done with the internal relocs, free them. */ | |
3832 | if (elf_section_data (section)->relocs == NULL) | |
3833 | free (internal_relocs); | |
3834 | } | |
3835 | } | |
28c9d252 NC |
3836 | |
3837 | /* Re-Calculate the number of needed stubs. */ | |
3838 | htab->stub_sec->size = 0; | |
3839 | bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab); | |
3840 | ||
3841 | if (!stub_changed) | |
07d6d2b8 | 3842 | break; |
28c9d252 NC |
3843 | |
3844 | stub_changed = FALSE; | |
3845 | } | |
3846 | ||
3847 | free (htab->all_local_syms); | |
3848 | return TRUE; | |
3849 | ||
3850 | error_ret_free_local: | |
3851 | free (htab->all_local_syms); | |
3852 | return FALSE; | |
3853 | } | |
3854 | ||
3855 | ||
3856 | /* Build all the stubs associated with the current output file. The | |
3857 | stubs are kept in a hash table attached to the main linker hash | |
3858 | table. We also set up the .plt entries for statically linked PIC | |
3859 | functions here. This function is called via hppaelf_finish in the | |
3860 | linker. */ | |
3861 | ||
3862 | bfd_boolean | |
3863 | elf32_avr_build_stubs (struct bfd_link_info *info) | |
3864 | { | |
3865 | asection *stub_sec; | |
3866 | struct bfd_hash_table *table; | |
3867 | struct elf32_avr_link_hash_table *htab; | |
3868 | bfd_size_type total_size = 0; | |
3869 | ||
3870 | htab = avr_link_hash_table (info); | |
64ee10b6 NC |
3871 | if (htab == NULL) |
3872 | return FALSE; | |
28c9d252 NC |
3873 | |
3874 | /* In case that there were several stub sections: */ | |
3875 | for (stub_sec = htab->stub_bfd->sections; | |
3876 | stub_sec != NULL; | |
3877 | stub_sec = stub_sec->next) | |
3878 | { | |
3879 | bfd_size_type size; | |
3880 | ||
3881 | /* Allocate memory to hold the linker stubs. */ | |
3882 | size = stub_sec->size; | |
3883 | total_size += size; | |
3884 | ||
3885 | stub_sec->contents = bfd_zalloc (htab->stub_bfd, size); | |
3886 | if (stub_sec->contents == NULL && size != 0) | |
3887 | return FALSE; | |
3888 | stub_sec->size = 0; | |
3889 | } | |
3890 | ||
3891 | /* Allocate memory for the adress mapping table. */ | |
3892 | htab->amt_entry_cnt = 0; | |
3893 | htab->amt_max_entry_cnt = total_size / 4; | |
3894 | htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma) | |
07d6d2b8 | 3895 | * htab->amt_max_entry_cnt); |
28c9d252 NC |
3896 | htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma) |
3897 | * htab->amt_max_entry_cnt ); | |
3898 | ||
3899 | if (debug_stubs) | |
3900 | printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt); | |
3901 | ||
3902 | /* Build the stubs as directed by the stub hash table. */ | |
3903 | table = &htab->bstab; | |
3904 | bfd_hash_traverse (table, avr_build_one_stub, info); | |
3905 | ||
3906 | if (debug_stubs) | |
3907 | printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size); | |
3908 | ||
3909 | return TRUE; | |
3910 | } | |
3911 | ||
137c83d6 AB |
3912 | /* Callback used by QSORT to order relocations AP and BP. */ |
3913 | ||
3914 | static int | |
3915 | internal_reloc_compare (const void *ap, const void *bp) | |
3916 | { | |
3917 | const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap; | |
3918 | const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp; | |
3919 | ||
3920 | if (a->r_offset != b->r_offset) | |
3921 | return (a->r_offset - b->r_offset); | |
3922 | ||
3923 | /* We don't need to sort on these criteria for correctness, | |
3924 | but enforcing a more strict ordering prevents unstable qsort | |
3925 | from behaving differently with different implementations. | |
3926 | Without the code below we get correct but different results | |
3927 | on Solaris 2.7 and 2.8. We would like to always produce the | |
3928 | same results no matter the host. */ | |
3929 | ||
3930 | if (a->r_info != b->r_info) | |
3931 | return (a->r_info - b->r_info); | |
3932 | ||
3933 | return (a->r_addend - b->r_addend); | |
3934 | } | |
3935 | ||
3936 | /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */ | |
3937 | ||
3938 | static bfd_boolean | |
fd361982 | 3939 | avr_is_section_for_address (asection *section, bfd_vma address) |
137c83d6 AB |
3940 | { |
3941 | bfd_vma vma; | |
3942 | bfd_size_type size; | |
3943 | ||
fd361982 | 3944 | vma = bfd_section_vma (section); |
137c83d6 AB |
3945 | if (address < vma) |
3946 | return FALSE; | |
3947 | ||
3948 | size = section->size; | |
3949 | if (address >= vma + size) | |
3950 | return FALSE; | |
3951 | ||
3952 | return TRUE; | |
3953 | } | |
3954 | ||
3955 | /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */ | |
3956 | ||
3957 | struct avr_find_section_data | |
3958 | { | |
3959 | /* The address we're looking for. */ | |
3960 | bfd_vma address; | |
3961 | ||
3962 | /* The section we've found. */ | |
3963 | asection *section; | |
3964 | }; | |
3965 | ||
3966 | /* Helper function to locate the section holding a certain virtual memory | |
3967 | address. This is called via bfd_map_over_sections. The DATA is an | |
3968 | instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which | |
3969 | has been set to the address to search for, and the section field has | |
3970 | been set to NULL. If SECTION from ABFD contains ADDRESS then the | |
3971 | section field in DATA will be set to SECTION. As an optimisation, if | |
3972 | the section field is already non-null then this function does not | |
3973 | perform any checks, and just returns. */ | |
3974 | ||
3975 | static void | |
fd361982 | 3976 | avr_find_section_for_address (bfd *abfd ATTRIBUTE_UNUSED, |
07d6d2b8 | 3977 | asection *section, void *data) |
137c83d6 AB |
3978 | { |
3979 | struct avr_find_section_data *fs_data | |
3980 | = (struct avr_find_section_data *) data; | |
3981 | ||
3982 | /* Return if already found. */ | |
3983 | if (fs_data->section != NULL) | |
3984 | return; | |
3985 | ||
3986 | /* If this section isn't part of the addressable code content, skip it. */ | |
fd361982 AM |
3987 | if ((bfd_section_flags (section) & SEC_ALLOC) == 0 |
3988 | && (bfd_section_flags (section) & SEC_CODE) == 0) | |
137c83d6 AB |
3989 | return; |
3990 | ||
fd361982 | 3991 | if (avr_is_section_for_address (section, fs_data->address)) |
137c83d6 AB |
3992 | fs_data->section = section; |
3993 | } | |
3994 | ||
3995 | /* Load all of the property records from SEC, a section from ABFD. Return | |
3996 | a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The | |
3997 | memory for the returned structure, and all of the records pointed too by | |
3998 | the structure are allocated with a single call to malloc, so, only the | |
3999 | pointer returned needs to be free'd. */ | |
4000 | ||
4001 | static struct avr_property_record_list * | |
4002 | avr_elf32_load_records_from_section (bfd *abfd, asection *sec) | |
4003 | { | |
4004 | char *contents = NULL, *ptr; | |
4005 | bfd_size_type size, mem_size; | |
4006 | bfd_byte version, flags; | |
4007 | uint16_t record_count, i; | |
4008 | struct avr_property_record_list *r_list = NULL; | |
4009 | Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end; | |
4010 | struct avr_find_section_data fs_data; | |
4011 | ||
4012 | fs_data.section = NULL; | |
4013 | ||
fd361982 | 4014 | size = bfd_section_size (sec); |
137c83d6 AB |
4015 | contents = bfd_malloc (size); |
4016 | bfd_get_section_contents (abfd, sec, contents, 0, size); | |
4017 | ptr = contents; | |
4018 | ||
4019 | /* Load the relocations for the '.avr.prop' section if there are any, and | |
4020 | sort them. */ | |
4021 | internal_relocs = (_bfd_elf_link_read_relocs | |
07d6d2b8 | 4022 | (abfd, sec, NULL, NULL, FALSE)); |
137c83d6 AB |
4023 | if (internal_relocs) |
4024 | qsort (internal_relocs, sec->reloc_count, | |
07d6d2b8 | 4025 | sizeof (Elf_Internal_Rela), internal_reloc_compare); |
137c83d6 AB |
4026 | |
4027 | /* There is a header at the start of the property record section SEC, the | |
4028 | format of this header is: | |
4029 | uint8_t : version number | |
4030 | uint8_t : flags | |
4031 | uint16_t : record counter | |
4032 | */ | |
4033 | ||
4034 | /* Check we have at least got a headers worth of bytes. */ | |
4035 | if (size < AVR_PROPERTY_SECTION_HEADER_SIZE) | |
4036 | goto load_failed; | |
4037 | ||
4038 | version = *((bfd_byte *) ptr); | |
4039 | ptr++; | |
4040 | flags = *((bfd_byte *) ptr); | |
4041 | ptr++; | |
8c51f2f2 | 4042 | record_count = bfd_get_16 (abfd, ptr); |
137c83d6 AB |
4043 | ptr+=2; |
4044 | BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE); | |
4045 | ||
4046 | /* Now allocate space for the list structure, and all of the list | |
4047 | elements in a single block. */ | |
4048 | mem_size = sizeof (struct avr_property_record_list) | |
4049 | + sizeof (struct avr_property_record) * record_count; | |
4050 | r_list = bfd_malloc (mem_size); | |
4051 | if (r_list == NULL) | |
4052 | goto load_failed; | |
4053 | ||
4054 | r_list->version = version; | |
4055 | r_list->flags = flags; | |
4056 | r_list->section = sec; | |
4057 | r_list->record_count = record_count; | |
4058 | r_list->records = (struct avr_property_record *) (&r_list [1]); | |
4059 | size -= AVR_PROPERTY_SECTION_HEADER_SIZE; | |
4060 | ||
4061 | /* Check that we understand the version number. There is only one | |
4062 | version number right now, anything else is an error. */ | |
4063 | if (r_list->version != AVR_PROPERTY_RECORDS_VERSION) | |
4064 | goto load_failed; | |
4065 | ||
4066 | rel = internal_relocs; | |
4067 | rel_end = rel + sec->reloc_count; | |
4068 | for (i = 0; i < record_count; ++i) | |
4069 | { | |
4070 | bfd_vma address; | |
4071 | ||
4072 | /* Each entry is a 32-bit address, followed by a single byte type. | |
07d6d2b8 AM |
4073 | After that is the type specific data. We must take care to |
4074 | ensure that we don't read beyond the end of the section data. */ | |
137c83d6 | 4075 | if (size < 5) |
07d6d2b8 | 4076 | goto load_failed; |
137c83d6 AB |
4077 | |
4078 | r_list->records [i].section = NULL; | |
4079 | r_list->records [i].offset = 0; | |
4080 | ||
4081 | if (rel) | |
07d6d2b8 AM |
4082 | { |
4083 | /* The offset of the address within the .avr.prop section. */ | |
4084 | size_t offset = ptr - contents; | |
137c83d6 | 4085 | |
07d6d2b8 AM |
4086 | while (rel < rel_end && rel->r_offset < offset) |
4087 | ++rel; | |
137c83d6 | 4088 | |
07d6d2b8 AM |
4089 | if (rel == rel_end) |
4090 | rel = NULL; | |
4091 | else if (rel->r_offset == offset) | |
4092 | { | |
4093 | /* Find section and section offset. */ | |
4094 | unsigned long r_symndx; | |
137c83d6 | 4095 | |
07d6d2b8 AM |
4096 | asection * rel_sec; |
4097 | bfd_vma sec_offset; | |
137c83d6 | 4098 | |
07d6d2b8 AM |
4099 | r_symndx = ELF32_R_SYM (rel->r_info); |
4100 | rel_sec = get_elf_r_symndx_section (abfd, r_symndx); | |
4101 | sec_offset = get_elf_r_symndx_offset (abfd, r_symndx) | |
4102 | + rel->r_addend; | |
137c83d6 | 4103 | |
07d6d2b8 AM |
4104 | r_list->records [i].section = rel_sec; |
4105 | r_list->records [i].offset = sec_offset; | |
4106 | } | |
4107 | } | |
137c83d6 | 4108 | |
8c51f2f2 | 4109 | address = bfd_get_32 (abfd, ptr); |
137c83d6 AB |
4110 | ptr += 4; |
4111 | size -= 4; | |
4112 | ||
4113 | if (r_list->records [i].section == NULL) | |
07d6d2b8 AM |
4114 | { |
4115 | /* Try to find section and offset from address. */ | |
4116 | if (fs_data.section != NULL | |
fd361982 | 4117 | && !avr_is_section_for_address (fs_data.section, address)) |
07d6d2b8 AM |
4118 | fs_data.section = NULL; |
4119 | ||
4120 | if (fs_data.section == NULL) | |
4121 | { | |
4122 | fs_data.address = address; | |
4123 | bfd_map_over_sections (abfd, avr_find_section_for_address, | |
4124 | &fs_data); | |
4125 | } | |
4126 | ||
4127 | if (fs_data.section == NULL) | |
4128 | { | |
4129 | fprintf (stderr, "Failed to find matching section.\n"); | |
4130 | goto load_failed; | |
4131 | } | |
4132 | ||
4133 | r_list->records [i].section = fs_data.section; | |
4134 | r_list->records [i].offset | |
fd361982 | 4135 | = address - bfd_section_vma (fs_data.section); |
07d6d2b8 | 4136 | } |
137c83d6 AB |
4137 | |
4138 | r_list->records [i].type = *((bfd_byte *) ptr); | |
4139 | ptr += 1; | |
4140 | size -= 1; | |
4141 | ||
4142 | switch (r_list->records [i].type) | |
07d6d2b8 AM |
4143 | { |
4144 | case RECORD_ORG: | |
4145 | /* Nothing else to load. */ | |
4146 | break; | |
4147 | case RECORD_ORG_AND_FILL: | |
4148 | /* Just a 4-byte fill to load. */ | |
4149 | if (size < 4) | |
4150 | goto load_failed; | |
8c51f2f2 | 4151 | r_list->records [i].data.org.fill = bfd_get_32 (abfd, ptr); |
07d6d2b8 AM |
4152 | ptr += 4; |
4153 | size -= 4; | |
4154 | break; | |
4155 | case RECORD_ALIGN: | |
4156 | /* Just a 4-byte alignment to load. */ | |
4157 | if (size < 4) | |
4158 | goto load_failed; | |
8c51f2f2 | 4159 | r_list->records [i].data.align.bytes = bfd_get_32 (abfd, ptr); |
07d6d2b8 AM |
4160 | ptr += 4; |
4161 | size -= 4; | |
4162 | /* Just initialise PRECEDING_DELETED field, this field is | |
4163 | used during linker relaxation. */ | |
4164 | r_list->records [i].data.align.preceding_deleted = 0; | |
4165 | break; | |
4166 | case RECORD_ALIGN_AND_FILL: | |
4167 | /* A 4-byte alignment, and a 4-byte fill to load. */ | |
4168 | if (size < 8) | |
4169 | goto load_failed; | |
8c51f2f2 | 4170 | r_list->records [i].data.align.bytes = bfd_get_32 (abfd, ptr); |
07d6d2b8 | 4171 | ptr += 4; |
8c51f2f2 | 4172 | r_list->records [i].data.align.fill = bfd_get_32 (abfd, ptr); |
07d6d2b8 AM |
4173 | ptr += 4; |
4174 | size -= 8; | |
4175 | /* Just initialise PRECEDING_DELETED field, this field is | |
4176 | used during linker relaxation. */ | |
4177 | r_list->records [i].data.align.preceding_deleted = 0; | |
4178 | break; | |
4179 | default: | |
4180 | goto load_failed; | |
4181 | } | |
137c83d6 AB |
4182 | } |
4183 | ||
4184 | free (contents); | |
024ea11b SKS |
4185 | if (elf_section_data (sec)->relocs != internal_relocs) |
4186 | free (internal_relocs); | |
137c83d6 AB |
4187 | return r_list; |
4188 | ||
4189 | load_failed: | |
024ea11b SKS |
4190 | if (elf_section_data (sec)->relocs != internal_relocs) |
4191 | free (internal_relocs); | |
137c83d6 AB |
4192 | free (contents); |
4193 | free (r_list); | |
4194 | return NULL; | |
4195 | } | |
4196 | ||
4197 | /* Load all of the property records from ABFD. See | |
4198 | AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */ | |
4199 | ||
4200 | struct avr_property_record_list * | |
4201 | avr_elf32_load_property_records (bfd *abfd) | |
4202 | { | |
4203 | asection *sec; | |
4204 | ||
4205 | /* Find the '.avr.prop' section and load the contents into memory. */ | |
4206 | sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME); | |
4207 | if (sec == NULL) | |
4208 | return NULL; | |
4209 | return avr_elf32_load_records_from_section (abfd, sec); | |
4210 | } | |
4211 | ||
4212 | const char * | |
4213 | avr_elf32_property_record_name (struct avr_property_record *rec) | |
4214 | { | |
4215 | const char *str; | |
4216 | ||
4217 | switch (rec->type) | |
4218 | { | |
4219 | case RECORD_ORG: | |
4220 | str = "ORG"; | |
4221 | break; | |
4222 | case RECORD_ORG_AND_FILL: | |
4223 | str = "ORG+FILL"; | |
4224 | break; | |
4225 | case RECORD_ALIGN: | |
4226 | str = "ALIGN"; | |
4227 | break; | |
4228 | case RECORD_ALIGN_AND_FILL: | |
4229 | str = "ALIGN+FILL"; | |
4230 | break; | |
4231 | default: | |
4232 | str = "unknown"; | |
4233 | } | |
4234 | ||
4235 | return str; | |
4236 | } | |
4237 | ||
4238 | ||
adde6300 | 4239 | #define ELF_ARCH bfd_arch_avr |
ae95ffa6 | 4240 | #define ELF_TARGET_ID AVR_ELF_DATA |
adde6300 | 4241 | #define ELF_MACHINE_CODE EM_AVR |
aa4f99bb | 4242 | #define ELF_MACHINE_ALT1 EM_AVR_OLD |
adde6300 AM |
4243 | #define ELF_MAXPAGESIZE 1 |
4244 | ||
07d6d2b8 | 4245 | #define TARGET_LITTLE_SYM avr_elf32_vec |
adde6300 AM |
4246 | #define TARGET_LITTLE_NAME "elf32-avr" |
4247 | ||
28c9d252 | 4248 | #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create |
28c9d252 | 4249 | |
07d6d2b8 AM |
4250 | #define elf_info_to_howto avr_info_to_howto_rela |
4251 | #define elf_info_to_howto_rel NULL | |
4252 | #define elf_backend_relocate_section elf32_avr_relocate_section | |
4253 | #define elf_backend_can_gc_sections 1 | |
f0fe0e16 | 4254 | #define elf_backend_rela_normal 1 |
adde6300 AM |
4255 | #define elf_backend_final_write_processing \ |
4256 | bfd_elf_avr_final_write_processing | |
4257 | #define elf_backend_object_p elf32_avr_object_p | |
4258 | ||
df406460 NC |
4259 | #define bfd_elf32_bfd_relax_section elf32_avr_relax_section |
4260 | #define bfd_elf32_bfd_get_relocated_section_contents \ | |
07d6d2b8 | 4261 | elf32_avr_get_relocated_section_contents |
bac13f5a | 4262 | #define bfd_elf32_new_section_hook elf_avr_new_section_hook |
deee88e9 | 4263 | #define elf_backend_special_sections elf_avr_special_sections |
df406460 | 4264 | |
adde6300 | 4265 | #include "elf32-target.h" |