Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* V850-specific support for 32-bit ELF |
90937f86 | 2 | Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002 |
7898deda | 3 | Free Software Foundation, Inc. |
252b5132 | 4 | |
86aba9db | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
86aba9db 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 | |
9 | the Free Software Foundation; either version 2 of the License, or | |
10 | (at your option) any later version. | |
252b5132 | 11 | |
86aba9db 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. | |
252b5132 | 16 | |
86aba9db 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 | |
19 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
252b5132 | 20 | |
252b5132 | 21 | /* XXX FIXME: This code is littered with 32bit int, 16bit short, 8bit char |
86aba9db | 22 | dependencies. As is the gas & simulator code for the v850. */ |
252b5132 | 23 | |
252b5132 RH |
24 | #include "bfd.h" |
25 | #include "sysdep.h" | |
26 | #include "bfdlink.h" | |
27 | #include "libbfd.h" | |
28 | #include "elf-bfd.h" | |
29 | #include "elf/v850.h" | |
e12dd2ea | 30 | #include "libiberty.h" |
252b5132 | 31 | |
e12dd2ea | 32 | /* Sign-extend a 24-bit number. */ |
dc810e39 | 33 | #define SEXT24(x) ((((x) & 0xffffff) ^ 0x800000) - 0x800000) |
435b1e90 | 34 | |
252b5132 RH |
35 | static reloc_howto_type *v850_elf_reloc_type_lookup |
36 | PARAMS ((bfd *abfd, bfd_reloc_code_real_type code)); | |
37 | static void v850_elf_info_to_howto_rel | |
947216bf | 38 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); |
252b5132 | 39 | static void v850_elf_info_to_howto_rela |
947216bf | 40 | PARAMS ((bfd *, arelent *, Elf_Internal_Rela *)); |
252b5132 RH |
41 | static bfd_reloc_status_type v850_elf_reloc |
42 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
b34976b6 | 43 | static bfd_boolean v850_elf_is_local_label_name |
252b5132 | 44 | PARAMS ((bfd *, const char *)); |
b34976b6 | 45 | static bfd_boolean v850_elf_relocate_section |
252b5132 RH |
46 | PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, |
47 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); | |
48 | static bfd_reloc_status_type v850_elf_perform_relocation | |
dc810e39 | 49 | PARAMS ((bfd *, unsigned int, bfd_vma, bfd_byte *)); |
b34976b6 | 50 | static bfd_boolean v850_elf_check_relocs |
252b5132 RH |
51 | PARAMS ((bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *)); |
52 | static void remember_hi16s_reloc | |
53 | PARAMS ((bfd *, bfd_vma, bfd_byte *)); | |
54 | static bfd_byte * find_remembered_hi16s_reloc | |
b34976b6 | 55 | PARAMS ((bfd_vma, bfd_boolean *)); |
252b5132 RH |
56 | static bfd_reloc_status_type v850_elf_final_link_relocate |
57 | PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *, bfd_byte *, bfd_vma, | |
58 | bfd_vma, bfd_vma, struct bfd_link_info *, asection *, int)); | |
b34976b6 | 59 | static bfd_boolean v850_elf_object_p |
252b5132 | 60 | PARAMS ((bfd *)); |
b34976b6 | 61 | static bfd_boolean v850_elf_fake_sections |
947216bf | 62 | PARAMS ((bfd *, Elf_Internal_Shdr *, asection *)); |
252b5132 | 63 | static void v850_elf_final_write_processing |
b34976b6 AM |
64 | PARAMS ((bfd *, bfd_boolean)); |
65 | static bfd_boolean v850_elf_set_private_flags | |
252b5132 | 66 | PARAMS ((bfd *, flagword)); |
b34976b6 | 67 | static bfd_boolean v850_elf_merge_private_bfd_data |
252b5132 | 68 | PARAMS ((bfd *, bfd *)); |
b34976b6 | 69 | static bfd_boolean v850_elf_print_private_bfd_data |
252b5132 | 70 | PARAMS ((bfd *, PTR)); |
b34976b6 | 71 | static bfd_boolean v850_elf_section_from_bfd_section |
af746e92 | 72 | PARAMS ((bfd *, asection *, int *)); |
252b5132 RH |
73 | static void v850_elf_symbol_processing |
74 | PARAMS ((bfd *, asymbol *)); | |
b34976b6 | 75 | static bfd_boolean v850_elf_add_symbol_hook |
252b5132 RH |
76 | PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *, |
77 | const char **, flagword *, asection **, bfd_vma *)); | |
b34976b6 | 78 | static bfd_boolean v850_elf_link_output_symbol_hook |
252b5132 RH |
79 | PARAMS ((bfd *, struct bfd_link_info *, const char *, |
80 | Elf_Internal_Sym *, asection *)); | |
b34976b6 | 81 | static bfd_boolean v850_elf_section_from_shdr |
90937f86 | 82 | PARAMS ((bfd *, Elf_Internal_Shdr *, const char *)); |
b34976b6 | 83 | static bfd_boolean v850_elf_gc_sweep_hook |
e12dd2ea NC |
84 | PARAMS ((bfd *, struct bfd_link_info *, asection *, |
85 | const Elf_Internal_Rela *)); | |
86 | static asection * v850_elf_gc_mark_hook | |
1e2f5b6e | 87 | PARAMS ((asection *, struct bfd_link_info *, |
e12dd2ea NC |
88 | Elf_Internal_Rela *, struct elf_link_hash_entry *, |
89 | Elf_Internal_Sym *)); | |
86aba9db NC |
90 | static bfd_reloc_status_type v850_elf_ignore_reloc |
91 | PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **)); | |
b34976b6 | 92 | static bfd_boolean v850_elf_relax_delete_bytes |
86aba9db | 93 | PARAMS ((bfd *, asection *, bfd_vma, bfd_vma, int)); |
b34976b6 AM |
94 | static bfd_boolean v850_elf_relax_section |
95 | PARAMS ((bfd *, asection *, struct bfd_link_info *, bfd_boolean *)); | |
e12dd2ea NC |
96 | |
97 | /* Note: It is REQUIRED that the 'type' value of each entry | |
98 | in this array match the index of the entry in the array. */ | |
252b5132 RH |
99 | static reloc_howto_type v850_elf_howto_table[] = |
100 | { | |
101 | /* This reloc does nothing. */ | |
102 | HOWTO (R_V850_NONE, /* type */ | |
103 | 0, /* rightshift */ | |
104 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
105 | 32, /* bitsize */ | |
b34976b6 | 106 | FALSE, /* pc_relative */ |
252b5132 RH |
107 | 0, /* bitpos */ |
108 | complain_overflow_bitfield, /* complain_on_overflow */ | |
109 | bfd_elf_generic_reloc, /* special_function */ | |
110 | "R_V850_NONE", /* name */ | |
b34976b6 | 111 | FALSE, /* partial_inplace */ |
252b5132 RH |
112 | 0, /* src_mask */ |
113 | 0, /* dst_mask */ | |
b34976b6 | 114 | FALSE), /* pcrel_offset */ |
252b5132 | 115 | |
435b1e90 | 116 | /* A PC relative 9 bit branch. */ |
252b5132 RH |
117 | HOWTO (R_V850_9_PCREL, /* type */ |
118 | 2, /* rightshift */ | |
119 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
120 | 26, /* bitsize */ | |
b34976b6 | 121 | TRUE, /* pc_relative */ |
252b5132 RH |
122 | 0, /* bitpos */ |
123 | complain_overflow_bitfield, /* complain_on_overflow */ | |
124 | v850_elf_reloc, /* special_function */ | |
125 | "R_V850_9_PCREL", /* name */ | |
b34976b6 | 126 | FALSE, /* partial_inplace */ |
252b5132 RH |
127 | 0x00ffffff, /* src_mask */ |
128 | 0x00ffffff, /* dst_mask */ | |
b34976b6 | 129 | TRUE), /* pcrel_offset */ |
252b5132 | 130 | |
435b1e90 | 131 | /* A PC relative 22 bit branch. */ |
252b5132 RH |
132 | HOWTO (R_V850_22_PCREL, /* type */ |
133 | 2, /* rightshift */ | |
134 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
135 | 22, /* bitsize */ | |
b34976b6 | 136 | TRUE, /* pc_relative */ |
252b5132 RH |
137 | 7, /* bitpos */ |
138 | complain_overflow_signed, /* complain_on_overflow */ | |
139 | v850_elf_reloc, /* special_function */ | |
140 | "R_V850_22_PCREL", /* name */ | |
b34976b6 | 141 | FALSE, /* partial_inplace */ |
252b5132 RH |
142 | 0x07ffff80, /* src_mask */ |
143 | 0x07ffff80, /* dst_mask */ | |
b34976b6 | 144 | TRUE), /* pcrel_offset */ |
252b5132 RH |
145 | |
146 | /* High 16 bits of symbol value. */ | |
147 | HOWTO (R_V850_HI16_S, /* type */ | |
148 | 0, /* rightshift */ | |
149 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
150 | 16, /* bitsize */ | |
b34976b6 | 151 | FALSE, /* pc_relative */ |
252b5132 RH |
152 | 0, /* bitpos */ |
153 | complain_overflow_dont, /* complain_on_overflow */ | |
154 | v850_elf_reloc, /* special_function */ | |
155 | "R_V850_HI16_S", /* name */ | |
b34976b6 | 156 | FALSE, /* partial_inplace */ |
252b5132 RH |
157 | 0xffff, /* src_mask */ |
158 | 0xffff, /* dst_mask */ | |
b34976b6 | 159 | FALSE), /* pcrel_offset */ |
252b5132 RH |
160 | |
161 | /* High 16 bits of symbol value. */ | |
162 | HOWTO (R_V850_HI16, /* type */ | |
163 | 0, /* rightshift */ | |
164 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
165 | 16, /* bitsize */ | |
b34976b6 | 166 | FALSE, /* pc_relative */ |
252b5132 RH |
167 | 0, /* bitpos */ |
168 | complain_overflow_dont, /* complain_on_overflow */ | |
169 | v850_elf_reloc, /* special_function */ | |
170 | "R_V850_HI16", /* name */ | |
b34976b6 | 171 | FALSE, /* partial_inplace */ |
252b5132 RH |
172 | 0xffff, /* src_mask */ |
173 | 0xffff, /* dst_mask */ | |
b34976b6 | 174 | FALSE), /* pcrel_offset */ |
252b5132 RH |
175 | |
176 | /* Low 16 bits of symbol value. */ | |
177 | HOWTO (R_V850_LO16, /* type */ | |
178 | 0, /* rightshift */ | |
179 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
180 | 16, /* bitsize */ | |
b34976b6 | 181 | FALSE, /* pc_relative */ |
252b5132 RH |
182 | 0, /* bitpos */ |
183 | complain_overflow_dont, /* complain_on_overflow */ | |
184 | v850_elf_reloc, /* special_function */ | |
185 | "R_V850_LO16", /* name */ | |
b34976b6 | 186 | FALSE, /* partial_inplace */ |
252b5132 RH |
187 | 0xffff, /* src_mask */ |
188 | 0xffff, /* dst_mask */ | |
b34976b6 | 189 | FALSE), /* pcrel_offset */ |
252b5132 RH |
190 | |
191 | /* Simple 32bit reloc. */ | |
192 | HOWTO (R_V850_32, /* type */ | |
193 | 0, /* rightshift */ | |
194 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
195 | 32, /* bitsize */ | |
b34976b6 | 196 | FALSE, /* pc_relative */ |
252b5132 RH |
197 | 0, /* bitpos */ |
198 | complain_overflow_dont, /* complain_on_overflow */ | |
199 | v850_elf_reloc, /* special_function */ | |
200 | "R_V850_32", /* name */ | |
b34976b6 | 201 | FALSE, /* partial_inplace */ |
252b5132 RH |
202 | 0xffffffff, /* src_mask */ |
203 | 0xffffffff, /* dst_mask */ | |
b34976b6 | 204 | FALSE), /* pcrel_offset */ |
252b5132 RH |
205 | |
206 | /* Simple 16bit reloc. */ | |
207 | HOWTO (R_V850_16, /* type */ | |
208 | 0, /* rightshift */ | |
209 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
210 | 16, /* bitsize */ | |
b34976b6 | 211 | FALSE, /* pc_relative */ |
252b5132 RH |
212 | 0, /* bitpos */ |
213 | complain_overflow_dont, /* complain_on_overflow */ | |
214 | bfd_elf_generic_reloc, /* special_function */ | |
215 | "R_V850_16", /* name */ | |
b34976b6 | 216 | FALSE, /* partial_inplace */ |
252b5132 RH |
217 | 0xffff, /* src_mask */ |
218 | 0xffff, /* dst_mask */ | |
b34976b6 | 219 | FALSE), /* pcrel_offset */ |
252b5132 RH |
220 | |
221 | /* Simple 8bit reloc. */ | |
222 | HOWTO (R_V850_8, /* type */ | |
223 | 0, /* rightshift */ | |
224 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
225 | 8, /* bitsize */ | |
b34976b6 | 226 | FALSE, /* pc_relative */ |
252b5132 RH |
227 | 0, /* bitpos */ |
228 | complain_overflow_dont, /* complain_on_overflow */ | |
229 | bfd_elf_generic_reloc, /* special_function */ | |
230 | "R_V850_8", /* name */ | |
b34976b6 | 231 | FALSE, /* partial_inplace */ |
252b5132 RH |
232 | 0xff, /* src_mask */ |
233 | 0xff, /* dst_mask */ | |
b34976b6 | 234 | FALSE), /* pcrel_offset */ |
252b5132 RH |
235 | |
236 | /* 16 bit offset from the short data area pointer. */ | |
237 | HOWTO (R_V850_SDA_16_16_OFFSET, /* type */ | |
238 | 0, /* rightshift */ | |
239 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
240 | 16, /* bitsize */ | |
b34976b6 | 241 | FALSE, /* pc_relative */ |
252b5132 RH |
242 | 0, /* bitpos */ |
243 | complain_overflow_dont, /* complain_on_overflow */ | |
244 | v850_elf_reloc, /* special_function */ | |
245 | "R_V850_SDA_16_16_OFFSET", /* name */ | |
b34976b6 | 246 | FALSE, /* partial_inplace */ |
252b5132 RH |
247 | 0xffff, /* src_mask */ |
248 | 0xffff, /* dst_mask */ | |
b34976b6 | 249 | FALSE), /* pcrel_offset */ |
252b5132 RH |
250 | |
251 | /* 15 bit offset from the short data area pointer. */ | |
252 | HOWTO (R_V850_SDA_15_16_OFFSET, /* type */ | |
253 | 1, /* rightshift */ | |
254 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
255 | 16, /* bitsize */ | |
b34976b6 | 256 | FALSE, /* pc_relative */ |
252b5132 RH |
257 | 1, /* bitpos */ |
258 | complain_overflow_dont, /* complain_on_overflow */ | |
259 | v850_elf_reloc, /* special_function */ | |
260 | "R_V850_SDA_15_16_OFFSET", /* name */ | |
b34976b6 | 261 | FALSE, /* partial_inplace */ |
252b5132 RH |
262 | 0xfffe, /* src_mask */ |
263 | 0xfffe, /* dst_mask */ | |
b34976b6 | 264 | FALSE), /* pcrel_offset */ |
252b5132 RH |
265 | |
266 | /* 16 bit offset from the zero data area pointer. */ | |
267 | HOWTO (R_V850_ZDA_16_16_OFFSET, /* type */ | |
268 | 0, /* rightshift */ | |
269 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
270 | 16, /* bitsize */ | |
b34976b6 | 271 | FALSE, /* pc_relative */ |
252b5132 RH |
272 | 0, /* bitpos */ |
273 | complain_overflow_dont, /* complain_on_overflow */ | |
274 | v850_elf_reloc, /* special_function */ | |
275 | "R_V850_ZDA_16_16_OFFSET", /* name */ | |
b34976b6 | 276 | FALSE, /* partial_inplace */ |
252b5132 RH |
277 | 0xffff, /* src_mask */ |
278 | 0xffff, /* dst_mask */ | |
b34976b6 | 279 | FALSE), /* pcrel_offset */ |
252b5132 RH |
280 | |
281 | /* 15 bit offset from the zero data area pointer. */ | |
282 | HOWTO (R_V850_ZDA_15_16_OFFSET, /* type */ | |
283 | 1, /* rightshift */ | |
284 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
285 | 16, /* bitsize */ | |
b34976b6 | 286 | FALSE, /* pc_relative */ |
252b5132 RH |
287 | 1, /* bitpos */ |
288 | complain_overflow_dont, /* complain_on_overflow */ | |
289 | v850_elf_reloc, /* special_function */ | |
290 | "R_V850_ZDA_15_16_OFFSET", /* name */ | |
b34976b6 | 291 | FALSE, /* partial_inplace */ |
252b5132 RH |
292 | 0xfffe, /* src_mask */ |
293 | 0xfffe, /* dst_mask */ | |
b34976b6 | 294 | FALSE), /* pcrel_offset */ |
252b5132 RH |
295 | |
296 | /* 6 bit offset from the tiny data area pointer. */ | |
297 | HOWTO (R_V850_TDA_6_8_OFFSET, /* type */ | |
298 | 2, /* rightshift */ | |
299 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
300 | 8, /* bitsize */ | |
b34976b6 | 301 | FALSE, /* pc_relative */ |
252b5132 RH |
302 | 1, /* bitpos */ |
303 | complain_overflow_dont, /* complain_on_overflow */ | |
304 | v850_elf_reloc, /* special_function */ | |
305 | "R_V850_TDA_6_8_OFFSET", /* name */ | |
b34976b6 | 306 | FALSE, /* partial_inplace */ |
252b5132 RH |
307 | 0x7e, /* src_mask */ |
308 | 0x7e, /* dst_mask */ | |
b34976b6 | 309 | FALSE), /* pcrel_offset */ |
252b5132 RH |
310 | |
311 | /* 8 bit offset from the tiny data area pointer. */ | |
312 | HOWTO (R_V850_TDA_7_8_OFFSET, /* type */ | |
313 | 1, /* rightshift */ | |
314 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
315 | 8, /* bitsize */ | |
b34976b6 | 316 | FALSE, /* pc_relative */ |
252b5132 RH |
317 | 0, /* bitpos */ |
318 | complain_overflow_dont, /* complain_on_overflow */ | |
319 | v850_elf_reloc, /* special_function */ | |
320 | "R_V850_TDA_7_8_OFFSET", /* name */ | |
b34976b6 | 321 | FALSE, /* partial_inplace */ |
252b5132 RH |
322 | 0x7f, /* src_mask */ |
323 | 0x7f, /* dst_mask */ | |
b34976b6 | 324 | FALSE), /* pcrel_offset */ |
435b1e90 | 325 | |
252b5132 RH |
326 | /* 7 bit offset from the tiny data area pointer. */ |
327 | HOWTO (R_V850_TDA_7_7_OFFSET, /* type */ | |
328 | 0, /* rightshift */ | |
329 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
330 | 7, /* bitsize */ | |
b34976b6 | 331 | FALSE, /* pc_relative */ |
252b5132 RH |
332 | 0, /* bitpos */ |
333 | complain_overflow_dont, /* complain_on_overflow */ | |
334 | v850_elf_reloc, /* special_function */ | |
335 | "R_V850_TDA_7_7_OFFSET", /* name */ | |
b34976b6 | 336 | FALSE, /* partial_inplace */ |
252b5132 RH |
337 | 0x7f, /* src_mask */ |
338 | 0x7f, /* dst_mask */ | |
b34976b6 | 339 | FALSE), /* pcrel_offset */ |
252b5132 RH |
340 | |
341 | /* 16 bit offset from the tiny data area pointer! */ | |
342 | HOWTO (R_V850_TDA_16_16_OFFSET, /* type */ | |
343 | 0, /* rightshift */ | |
344 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
345 | 16, /* bitsize */ | |
b34976b6 | 346 | FALSE, /* pc_relative */ |
252b5132 RH |
347 | 0, /* bitpos */ |
348 | complain_overflow_dont, /* complain_on_overflow */ | |
349 | v850_elf_reloc, /* special_function */ | |
350 | "R_V850_TDA_16_16_OFFSET", /* name */ | |
b34976b6 | 351 | FALSE, /* partial_inplace */ |
252b5132 RH |
352 | 0xffff, /* src_mask */ |
353 | 0xfff, /* dst_mask */ | |
b34976b6 | 354 | FALSE), /* pcrel_offset */ |
252b5132 RH |
355 | |
356 | /* 5 bit offset from the tiny data area pointer. */ | |
357 | HOWTO (R_V850_TDA_4_5_OFFSET, /* type */ | |
358 | 1, /* rightshift */ | |
359 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
360 | 5, /* bitsize */ | |
b34976b6 | 361 | FALSE, /* pc_relative */ |
252b5132 RH |
362 | 0, /* bitpos */ |
363 | complain_overflow_dont, /* complain_on_overflow */ | |
364 | v850_elf_reloc, /* special_function */ | |
365 | "R_V850_TDA_4_5_OFFSET", /* name */ | |
b34976b6 | 366 | FALSE, /* partial_inplace */ |
252b5132 RH |
367 | 0x0f, /* src_mask */ |
368 | 0x0f, /* dst_mask */ | |
b34976b6 | 369 | FALSE), /* pcrel_offset */ |
252b5132 RH |
370 | |
371 | /* 4 bit offset from the tiny data area pointer. */ | |
372 | HOWTO (R_V850_TDA_4_4_OFFSET, /* type */ | |
373 | 0, /* rightshift */ | |
374 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
375 | 4, /* bitsize */ | |
b34976b6 | 376 | FALSE, /* pc_relative */ |
252b5132 RH |
377 | 0, /* bitpos */ |
378 | complain_overflow_dont, /* complain_on_overflow */ | |
379 | v850_elf_reloc, /* special_function */ | |
380 | "R_V850_TDA_4_4_OFFSET", /* name */ | |
b34976b6 | 381 | FALSE, /* partial_inplace */ |
252b5132 RH |
382 | 0x0f, /* src_mask */ |
383 | 0x0f, /* dst_mask */ | |
b34976b6 | 384 | FALSE), /* pcrel_offset */ |
252b5132 RH |
385 | |
386 | /* 16 bit offset from the short data area pointer. */ | |
387 | HOWTO (R_V850_SDA_16_16_SPLIT_OFFSET, /* type */ | |
388 | 0, /* rightshift */ | |
389 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
390 | 16, /* bitsize */ | |
b34976b6 | 391 | FALSE, /* pc_relative */ |
252b5132 RH |
392 | 0, /* bitpos */ |
393 | complain_overflow_dont, /* complain_on_overflow */ | |
394 | v850_elf_reloc, /* special_function */ | |
395 | "R_V850_SDA_16_16_SPLIT_OFFSET",/* name */ | |
b34976b6 | 396 | FALSE, /* partial_inplace */ |
252b5132 RH |
397 | 0xfffe0020, /* src_mask */ |
398 | 0xfffe0020, /* dst_mask */ | |
b34976b6 | 399 | FALSE), /* pcrel_offset */ |
252b5132 RH |
400 | |
401 | /* 16 bit offset from the zero data area pointer. */ | |
402 | HOWTO (R_V850_ZDA_16_16_SPLIT_OFFSET, /* type */ | |
403 | 0, /* rightshift */ | |
404 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
405 | 16, /* bitsize */ | |
b34976b6 | 406 | FALSE, /* pc_relative */ |
252b5132 RH |
407 | 0, /* bitpos */ |
408 | complain_overflow_dont, /* complain_on_overflow */ | |
409 | v850_elf_reloc, /* special_function */ | |
410 | "R_V850_ZDA_16_16_SPLIT_OFFSET",/* name */ | |
b34976b6 | 411 | FALSE, /* partial_inplace */ |
252b5132 RH |
412 | 0xfffe0020, /* src_mask */ |
413 | 0xfffe0020, /* dst_mask */ | |
b34976b6 | 414 | FALSE), /* pcrel_offset */ |
252b5132 RH |
415 | |
416 | /* 6 bit offset from the call table base pointer. */ | |
417 | HOWTO (R_V850_CALLT_6_7_OFFSET, /* type */ | |
418 | 0, /* rightshift */ | |
419 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
420 | 7, /* bitsize */ | |
b34976b6 | 421 | FALSE, /* pc_relative */ |
252b5132 RH |
422 | 0, /* bitpos */ |
423 | complain_overflow_dont, /* complain_on_overflow */ | |
424 | v850_elf_reloc, /* special_function */ | |
425 | "R_V850_CALLT_6_7_OFFSET", /* name */ | |
b34976b6 | 426 | FALSE, /* partial_inplace */ |
252b5132 RH |
427 | 0x3f, /* src_mask */ |
428 | 0x3f, /* dst_mask */ | |
b34976b6 | 429 | FALSE), /* pcrel_offset */ |
252b5132 RH |
430 | |
431 | /* 16 bit offset from the call table base pointer. */ | |
432 | HOWTO (R_V850_CALLT_16_16_OFFSET, /* type */ | |
433 | 0, /* rightshift */ | |
434 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
435 | 16, /* bitsize */ | |
b34976b6 | 436 | FALSE, /* pc_relative */ |
252b5132 RH |
437 | 0, /* bitpos */ |
438 | complain_overflow_dont, /* complain_on_overflow */ | |
439 | v850_elf_reloc, /* special_function */ | |
440 | "R_V850_CALLT_16_16_OFFSET", /* name */ | |
b34976b6 | 441 | FALSE, /* partial_inplace */ |
252b5132 RH |
442 | 0xffff, /* src_mask */ |
443 | 0xffff, /* dst_mask */ | |
b34976b6 | 444 | FALSE), /* pcrel_offset */ |
252b5132 RH |
445 | |
446 | /* GNU extension to record C++ vtable hierarchy */ | |
447 | HOWTO (R_V850_GNU_VTINHERIT, /* type */ | |
448 | 0, /* rightshift */ | |
449 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
450 | 0, /* bitsize */ | |
b34976b6 | 451 | FALSE, /* pc_relative */ |
252b5132 RH |
452 | 0, /* bitpos */ |
453 | complain_overflow_dont, /* complain_on_overflow */ | |
454 | NULL, /* special_function */ | |
455 | "R_V850_GNU_VTINHERIT", /* name */ | |
b34976b6 | 456 | FALSE, /* partial_inplace */ |
252b5132 RH |
457 | 0, /* src_mask */ |
458 | 0, /* dst_mask */ | |
b34976b6 | 459 | FALSE), /* pcrel_offset */ |
252b5132 RH |
460 | |
461 | /* GNU extension to record C++ vtable member usage */ | |
462 | HOWTO (R_V850_GNU_VTENTRY, /* type */ | |
463 | 0, /* rightshift */ | |
464 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
465 | 0, /* bitsize */ | |
b34976b6 | 466 | FALSE, /* pc_relative */ |
252b5132 RH |
467 | 0, /* bitpos */ |
468 | complain_overflow_dont, /* complain_on_overflow */ | |
469 | _bfd_elf_rel_vtable_reloc_fn, /* special_function */ | |
470 | "R_V850_GNU_VTENTRY", /* name */ | |
b34976b6 | 471 | FALSE, /* partial_inplace */ |
252b5132 RH |
472 | 0, /* src_mask */ |
473 | 0, /* dst_mask */ | |
b34976b6 | 474 | FALSE), /* pcrel_offset */ |
435b1e90 | 475 | |
86aba9db NC |
476 | /* Indicates a .longcall pseudo-op. The compiler will generate a .longcall |
477 | pseudo-op when it finds a function call which can be relaxed. */ | |
478 | HOWTO (R_V850_LONGCALL, /* type */ | |
479 | 0, /* rightshift */ | |
480 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
481 | 32, /* bitsize */ | |
b34976b6 | 482 | TRUE, /* pc_relative */ |
86aba9db NC |
483 | 0, /* bitpos */ |
484 | complain_overflow_signed, /* complain_on_overflow */ | |
485 | v850_elf_ignore_reloc, /* special_function */ | |
486 | "R_V850_LONGCALL", /* name */ | |
b34976b6 | 487 | FALSE, /* partial_inplace */ |
86aba9db NC |
488 | 0, /* src_mask */ |
489 | 0, /* dst_mask */ | |
b34976b6 | 490 | TRUE), /* pcrel_offset */ |
86aba9db NC |
491 | |
492 | /* Indicates a .longjump pseudo-op. The compiler will generate a | |
493 | .longjump pseudo-op when it finds a branch which can be relaxed. */ | |
494 | HOWTO (R_V850_LONGJUMP, /* type */ | |
495 | 0, /* rightshift */ | |
496 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
497 | 32, /* bitsize */ | |
b34976b6 | 498 | TRUE, /* pc_relative */ |
86aba9db NC |
499 | 0, /* bitpos */ |
500 | complain_overflow_signed, /* complain_on_overflow */ | |
501 | v850_elf_ignore_reloc, /* special_function */ | |
502 | "R_V850_LONGJUMP", /* name */ | |
b34976b6 | 503 | FALSE, /* partial_inplace */ |
86aba9db NC |
504 | 0, /* src_mask */ |
505 | 0, /* dst_mask */ | |
b34976b6 | 506 | TRUE), /* pcrel_offset */ |
86aba9db NC |
507 | |
508 | HOWTO (R_V850_ALIGN, /* type */ | |
509 | 0, /* rightshift */ | |
510 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
511 | 0, /* bitsize */ | |
b34976b6 | 512 | FALSE, /* pc_relative */ |
86aba9db NC |
513 | 0, /* bitpos */ |
514 | complain_overflow_unsigned, /* complain_on_overflow */ | |
515 | v850_elf_ignore_reloc, /* special_function */ | |
516 | "R_V850_ALIGN", /* name */ | |
b34976b6 | 517 | FALSE, /* partial_inplace */ |
86aba9db NC |
518 | 0, /* src_mask */ |
519 | 0, /* dst_mask */ | |
b34976b6 | 520 | TRUE), /* pcrel_offset */ |
252b5132 RH |
521 | }; |
522 | ||
523 | /* Map BFD reloc types to V850 ELF reloc types. */ | |
524 | ||
525 | struct v850_elf_reloc_map | |
526 | { | |
527 | /* BFD_RELOC_V850_CALLT_16_16_OFFSET is 258, which will not fix in an | |
528 | unsigned char. */ | |
529 | bfd_reloc_code_real_type bfd_reloc_val; | |
917583ad | 530 | unsigned int elf_reloc_val; |
252b5132 RH |
531 | }; |
532 | ||
533 | static const struct v850_elf_reloc_map v850_elf_reloc_map[] = | |
534 | { | |
86aba9db NC |
535 | { BFD_RELOC_NONE, R_V850_NONE }, |
536 | { BFD_RELOC_V850_9_PCREL, R_V850_9_PCREL }, | |
537 | { BFD_RELOC_V850_22_PCREL, R_V850_22_PCREL }, | |
538 | { BFD_RELOC_HI16_S, R_V850_HI16_S }, | |
539 | { BFD_RELOC_HI16, R_V850_HI16 }, | |
540 | { BFD_RELOC_LO16, R_V850_LO16 }, | |
541 | { BFD_RELOC_32, R_V850_32 }, | |
542 | { BFD_RELOC_16, R_V850_16 }, | |
543 | { BFD_RELOC_8, R_V850_8 }, | |
544 | { BFD_RELOC_V850_SDA_16_16_OFFSET, R_V850_SDA_16_16_OFFSET }, | |
545 | { BFD_RELOC_V850_SDA_15_16_OFFSET, R_V850_SDA_15_16_OFFSET }, | |
546 | { BFD_RELOC_V850_ZDA_16_16_OFFSET, R_V850_ZDA_16_16_OFFSET }, | |
547 | { BFD_RELOC_V850_ZDA_15_16_OFFSET, R_V850_ZDA_15_16_OFFSET }, | |
548 | { BFD_RELOC_V850_TDA_6_8_OFFSET, R_V850_TDA_6_8_OFFSET }, | |
549 | { BFD_RELOC_V850_TDA_7_8_OFFSET, R_V850_TDA_7_8_OFFSET }, | |
550 | { BFD_RELOC_V850_TDA_7_7_OFFSET, R_V850_TDA_7_7_OFFSET }, | |
551 | { BFD_RELOC_V850_TDA_16_16_OFFSET, R_V850_TDA_16_16_OFFSET }, | |
252b5132 RH |
552 | { BFD_RELOC_V850_TDA_4_5_OFFSET, R_V850_TDA_4_5_OFFSET }, |
553 | { BFD_RELOC_V850_TDA_4_4_OFFSET, R_V850_TDA_4_4_OFFSET }, | |
554 | { BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET, R_V850_SDA_16_16_SPLIT_OFFSET }, | |
555 | { BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET, R_V850_ZDA_16_16_SPLIT_OFFSET }, | |
556 | { BFD_RELOC_V850_CALLT_6_7_OFFSET, R_V850_CALLT_6_7_OFFSET }, | |
557 | { BFD_RELOC_V850_CALLT_16_16_OFFSET, R_V850_CALLT_16_16_OFFSET }, | |
86aba9db NC |
558 | { BFD_RELOC_VTABLE_INHERIT, R_V850_GNU_VTINHERIT }, |
559 | { BFD_RELOC_VTABLE_ENTRY, R_V850_GNU_VTENTRY }, | |
560 | { BFD_RELOC_V850_LONGCALL, R_V850_LONGCALL }, | |
561 | { BFD_RELOC_V850_LONGJUMP, R_V850_LONGJUMP }, | |
562 | { BFD_RELOC_V850_ALIGN, R_V850_ALIGN }, | |
252b5132 RH |
563 | |
564 | }; | |
252b5132 | 565 | \f |
e12dd2ea NC |
566 | /* Map a bfd relocation into the appropriate howto structure. */ |
567 | ||
252b5132 RH |
568 | static reloc_howto_type * |
569 | v850_elf_reloc_type_lookup (abfd, code) | |
b34976b6 AM |
570 | bfd *abfd ATTRIBUTE_UNUSED; |
571 | bfd_reloc_code_real_type code; | |
252b5132 RH |
572 | { |
573 | unsigned int i; | |
574 | ||
e12dd2ea NC |
575 | for (i = ARRAY_SIZE (v850_elf_reloc_map); i --;) |
576 | if (v850_elf_reloc_map[i].bfd_reloc_val == code) | |
577 | { | |
917583ad | 578 | unsigned int elf_reloc_val = v850_elf_reloc_map[i].elf_reloc_val; |
dc810e39 | 579 | |
fbad9a25 | 580 | BFD_ASSERT (v850_elf_howto_table[elf_reloc_val].type == elf_reloc_val); |
435b1e90 | 581 | |
fbad9a25 | 582 | return v850_elf_howto_table + elf_reloc_val; |
e12dd2ea | 583 | } |
252b5132 RH |
584 | |
585 | return NULL; | |
586 | } | |
252b5132 RH |
587 | \f |
588 | /* Set the howto pointer for an V850 ELF reloc. */ | |
e12dd2ea | 589 | |
252b5132 RH |
590 | static void |
591 | v850_elf_info_to_howto_rel (abfd, cache_ptr, dst) | |
b34976b6 AM |
592 | bfd *abfd ATTRIBUTE_UNUSED; |
593 | arelent *cache_ptr; | |
594 | Elf_Internal_Rela *dst; | |
252b5132 RH |
595 | { |
596 | unsigned int r_type; | |
597 | ||
598 | r_type = ELF32_R_TYPE (dst->r_info); | |
599 | BFD_ASSERT (r_type < (unsigned int) R_V850_max); | |
600 | cache_ptr->howto = &v850_elf_howto_table[r_type]; | |
601 | } | |
602 | ||
435b1e90 | 603 | /* Set the howto pointer for a V850 ELF reloc (type RELA). */ |
252b5132 RH |
604 | static void |
605 | v850_elf_info_to_howto_rela (abfd, cache_ptr, dst) | |
b34976b6 AM |
606 | bfd *abfd ATTRIBUTE_UNUSED; |
607 | arelent * cache_ptr; | |
608 | Elf_Internal_Rela *dst; | |
252b5132 RH |
609 | { |
610 | unsigned int r_type; | |
611 | ||
612 | r_type = ELF32_R_TYPE (dst->r_info); | |
613 | BFD_ASSERT (r_type < (unsigned int) R_V850_max); | |
614 | cache_ptr->howto = &v850_elf_howto_table[r_type]; | |
615 | } | |
252b5132 RH |
616 | \f |
617 | /* Look through the relocs for a section during the first phase, and | |
618 | allocate space in the global offset table or procedure linkage | |
619 | table. */ | |
620 | ||
b34976b6 | 621 | static bfd_boolean |
252b5132 | 622 | v850_elf_check_relocs (abfd, info, sec, relocs) |
b34976b6 AM |
623 | bfd *abfd; |
624 | struct bfd_link_info *info; | |
625 | asection *sec; | |
626 | const Elf_Internal_Rela *relocs; | |
252b5132 | 627 | { |
b34976b6 | 628 | bfd_boolean ret = TRUE; |
252b5132 RH |
629 | bfd *dynobj; |
630 | Elf_Internal_Shdr *symtab_hdr; | |
631 | struct elf_link_hash_entry **sym_hashes; | |
632 | const Elf_Internal_Rela *rel; | |
633 | const Elf_Internal_Rela *rel_end; | |
634 | asection *sreloc; | |
635 | enum v850_reloc_type r_type; | |
636 | int other = 0; | |
637 | const char *common = (const char *)0; | |
638 | ||
639 | if (info->relocateable) | |
b34976b6 | 640 | return TRUE; |
252b5132 RH |
641 | |
642 | #ifdef DEBUG | |
643 | fprintf (stderr, "v850_elf_check_relocs called for section %s in %s\n", | |
644 | bfd_get_section_name (abfd, sec), | |
8f615d07 | 645 | bfd_archive_filename (abfd)); |
252b5132 RH |
646 | #endif |
647 | ||
648 | dynobj = elf_hash_table (info)->dynobj; | |
649 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
650 | sym_hashes = elf_sym_hashes (abfd); | |
651 | sreloc = NULL; | |
652 | ||
653 | rel_end = relocs + sec->reloc_count; | |
654 | for (rel = relocs; rel < rel_end; rel++) | |
655 | { | |
656 | unsigned long r_symndx; | |
657 | struct elf_link_hash_entry *h; | |
658 | ||
659 | r_symndx = ELF32_R_SYM (rel->r_info); | |
660 | if (r_symndx < symtab_hdr->sh_info) | |
661 | h = NULL; | |
662 | else | |
663 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
664 | ||
665 | r_type = (enum v850_reloc_type) ELF32_R_TYPE (rel->r_info); | |
666 | switch (r_type) | |
667 | { | |
668 | default: | |
669 | case R_V850_NONE: | |
670 | case R_V850_9_PCREL: | |
671 | case R_V850_22_PCREL: | |
672 | case R_V850_HI16_S: | |
673 | case R_V850_HI16: | |
674 | case R_V850_LO16: | |
675 | case R_V850_32: | |
676 | case R_V850_16: | |
677 | case R_V850_8: | |
678 | case R_V850_CALLT_6_7_OFFSET: | |
679 | case R_V850_CALLT_16_16_OFFSET: | |
680 | break; | |
681 | ||
682 | /* This relocation describes the C++ object vtable hierarchy. | |
683 | Reconstruct it for later use during GC. */ | |
684 | case R_V850_GNU_VTINHERIT: | |
685 | if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) | |
b34976b6 | 686 | return FALSE; |
252b5132 RH |
687 | break; |
688 | ||
e12dd2ea NC |
689 | /* This relocation describes which C++ vtable entries |
690 | are actually used. Record for later use during GC. */ | |
252b5132 RH |
691 | case R_V850_GNU_VTENTRY: |
692 | if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend)) | |
b34976b6 | 693 | return FALSE; |
252b5132 RH |
694 | break; |
695 | ||
696 | case R_V850_SDA_16_16_SPLIT_OFFSET: | |
697 | case R_V850_SDA_16_16_OFFSET: | |
698 | case R_V850_SDA_15_16_OFFSET: | |
699 | other = V850_OTHER_SDA; | |
700 | common = ".scommon"; | |
701 | goto small_data_common; | |
435b1e90 | 702 | |
252b5132 RH |
703 | case R_V850_ZDA_16_16_SPLIT_OFFSET: |
704 | case R_V850_ZDA_16_16_OFFSET: | |
705 | case R_V850_ZDA_15_16_OFFSET: | |
706 | other = V850_OTHER_ZDA; | |
707 | common = ".zcommon"; | |
708 | goto small_data_common; | |
435b1e90 | 709 | |
252b5132 RH |
710 | case R_V850_TDA_4_5_OFFSET: |
711 | case R_V850_TDA_4_4_OFFSET: | |
712 | case R_V850_TDA_6_8_OFFSET: | |
713 | case R_V850_TDA_7_8_OFFSET: | |
714 | case R_V850_TDA_7_7_OFFSET: | |
715 | case R_V850_TDA_16_16_OFFSET: | |
716 | other = V850_OTHER_TDA; | |
717 | common = ".tcommon"; | |
718 | /* fall through */ | |
719 | ||
720 | #define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA) | |
721 | ||
722 | small_data_common: | |
723 | if (h) | |
724 | { | |
e12dd2ea NC |
725 | /* Flag which type of relocation was used. */ |
726 | h->other |= other; | |
252b5132 RH |
727 | if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK) |
728 | && (h->other & V850_OTHER_ERROR) == 0) | |
729 | { | |
730 | const char * msg; | |
731 | static char buff[200]; /* XXX */ | |
732 | ||
733 | switch (h->other & V850_OTHER_MASK) | |
734 | { | |
735 | default: | |
736 | msg = _("Variable `%s' cannot occupy in multiple small data regions"); | |
737 | break; | |
738 | case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA: | |
739 | msg = _("Variable `%s' can only be in one of the small, zero, and tiny data regions"); | |
740 | break; | |
741 | case V850_OTHER_SDA | V850_OTHER_ZDA: | |
742 | msg = _("Variable `%s' cannot be in both small and zero data regions simultaneously"); | |
743 | break; | |
744 | case V850_OTHER_SDA | V850_OTHER_TDA: | |
745 | msg = _("Variable `%s' cannot be in both small and tiny data regions simultaneously"); | |
746 | break; | |
747 | case V850_OTHER_ZDA | V850_OTHER_TDA: | |
748 | msg = _("Variable `%s' cannot be in both zero and tiny data regions simultaneously"); | |
749 | break; | |
750 | } | |
751 | ||
752 | sprintf (buff, msg, h->root.root.string); | |
753 | info->callbacks->warning (info, buff, h->root.root.string, | |
dc810e39 AM |
754 | abfd, h->root.u.def.section, |
755 | (bfd_vma) 0); | |
252b5132 RH |
756 | |
757 | bfd_set_error (bfd_error_bad_value); | |
758 | h->other |= V850_OTHER_ERROR; | |
b34976b6 | 759 | ret = FALSE; |
252b5132 RH |
760 | } |
761 | } | |
762 | ||
763 | if (h && h->root.type == bfd_link_hash_common | |
764 | && h->root.u.c.p | |
765 | && !strcmp (bfd_get_section_name (abfd, h->root.u.c.p->section), "COMMON")) | |
766 | { | |
e12dd2ea NC |
767 | asection * section; |
768 | ||
769 | section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common); | |
252b5132 RH |
770 | section->flags |= SEC_IS_COMMON; |
771 | } | |
772 | ||
773 | #ifdef DEBUG | |
774 | fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n", | |
775 | v850_elf_howto_table[ (int)r_type ].name, | |
776 | (h && h->root.root.string) ? h->root.root.string : "<unknown>", | |
777 | (h->root.type == bfd_link_hash_common) ? ", symbol is common" : ""); | |
778 | #endif | |
779 | break; | |
780 | } | |
781 | } | |
782 | ||
783 | return ret; | |
784 | } | |
785 | ||
e12dd2ea NC |
786 | /* In the old version, when an entry was checked out from the table, |
787 | it was deleted. This produced an error if the entry was needed | |
788 | more than once, as the second attempted retry failed. | |
789 | ||
790 | In the current version, the entry is not deleted, instead we set | |
b34976b6 | 791 | the field 'found' to TRUE. If a second lookup matches the same |
e12dd2ea NC |
792 | entry, then we know that the hi16s reloc has already been updated |
793 | and does not need to be updated a second time. | |
794 | ||
795 | TODO - TOFIX: If it is possible that we need to restore 2 different | |
796 | addresses from the same table entry, where the first generates an | |
797 | overflow, whilst the second do not, then this code will fail. */ | |
252b5132 RH |
798 | |
799 | typedef struct hi16s_location | |
800 | { | |
b34976b6 AM |
801 | bfd_vma addend; |
802 | bfd_byte *address; | |
252b5132 | 803 | unsigned long counter; |
b34976b6 AM |
804 | bfd_boolean found; |
805 | struct hi16s_location *next; | |
252b5132 RH |
806 | } |
807 | hi16s_location; | |
808 | ||
b34976b6 AM |
809 | static hi16s_location *previous_hi16s; |
810 | static hi16s_location *free_hi16s; | |
811 | static unsigned long hi16s_counter; | |
252b5132 RH |
812 | |
813 | static void | |
814 | remember_hi16s_reloc (abfd, addend, address) | |
b34976b6 AM |
815 | bfd *abfd; |
816 | bfd_vma addend; | |
817 | bfd_byte *address; | |
252b5132 RH |
818 | { |
819 | hi16s_location * entry = NULL; | |
dc810e39 | 820 | bfd_size_type amt = sizeof (* free_hi16s); |
435b1e90 | 821 | |
252b5132 RH |
822 | /* Find a free structure. */ |
823 | if (free_hi16s == NULL) | |
dc810e39 | 824 | free_hi16s = (hi16s_location *) bfd_zalloc (abfd, amt); |
252b5132 RH |
825 | |
826 | entry = free_hi16s; | |
827 | free_hi16s = free_hi16s->next; | |
435b1e90 | 828 | |
252b5132 RH |
829 | entry->addend = addend; |
830 | entry->address = address; | |
831 | entry->counter = hi16s_counter ++; | |
b34976b6 | 832 | entry->found = FALSE; |
252b5132 RH |
833 | entry->next = previous_hi16s; |
834 | previous_hi16s = entry; | |
435b1e90 | 835 | |
252b5132 RH |
836 | /* Cope with wrap around of our counter. */ |
837 | if (hi16s_counter == 0) | |
838 | { | |
839 | /* XXX - Assume that all counter entries differ only in their low 16 bits. */ | |
840 | for (entry = previous_hi16s; entry != NULL; entry = entry->next) | |
841 | entry->counter &= 0xffff; | |
842 | ||
843 | hi16s_counter = 0x10000; | |
844 | } | |
435b1e90 | 845 | |
252b5132 RH |
846 | return; |
847 | } | |
848 | ||
849 | static bfd_byte * | |
850 | find_remembered_hi16s_reloc (addend, already_found) | |
b34976b6 AM |
851 | bfd_vma addend; |
852 | bfd_boolean *already_found; | |
252b5132 | 853 | { |
b34976b6 AM |
854 | hi16s_location *match = NULL; |
855 | hi16s_location *entry; | |
856 | hi16s_location *previous = NULL; | |
857 | hi16s_location *prev; | |
858 | bfd_byte *addr; | |
435b1e90 | 859 | |
252b5132 RH |
860 | /* Search the table. Record the most recent entry that matches. */ |
861 | for (entry = previous_hi16s; entry; entry = entry->next) | |
862 | { | |
863 | if (entry->addend == addend | |
864 | && (match == NULL || match->counter < entry->counter)) | |
865 | { | |
866 | previous = prev; | |
867 | match = entry; | |
868 | } | |
435b1e90 | 869 | |
252b5132 RH |
870 | prev = entry; |
871 | } | |
872 | ||
873 | if (match == NULL) | |
874 | return NULL; | |
875 | ||
876 | /* Extract the address. */ | |
877 | addr = match->address; | |
878 | ||
879 | /* Remeber if this entry has already been used before. */ | |
880 | if (already_found) | |
881 | * already_found = match->found; | |
882 | ||
883 | /* Note that this entry has now been used. */ | |
b34976b6 | 884 | match->found = TRUE; |
435b1e90 | 885 | |
252b5132 | 886 | return addr; |
435b1e90 | 887 | } |
252b5132 RH |
888 | |
889 | /* FIXME: The code here probably ought to be removed and the code in reloc.c | |
890 | allowed to do its stuff instead. At least for most of the relocs, anwyay. */ | |
e12dd2ea | 891 | |
252b5132 RH |
892 | static bfd_reloc_status_type |
893 | v850_elf_perform_relocation (abfd, r_type, addend, address) | |
dc810e39 AM |
894 | bfd *abfd; |
895 | unsigned int r_type; | |
896 | bfd_vma addend; | |
897 | bfd_byte *address; | |
252b5132 RH |
898 | { |
899 | unsigned long insn; | |
900 | bfd_signed_vma saddend = (bfd_signed_vma) addend; | |
435b1e90 | 901 | |
252b5132 RH |
902 | switch (r_type) |
903 | { | |
904 | default: | |
905 | /* fprintf (stderr, "reloc type %d not SUPPORTED\n", r_type ); */ | |
906 | return bfd_reloc_notsupported; | |
435b1e90 | 907 | |
252b5132 RH |
908 | case R_V850_32: |
909 | bfd_put_32 (abfd, addend, address); | |
910 | return bfd_reloc_ok; | |
435b1e90 | 911 | |
252b5132 RH |
912 | case R_V850_22_PCREL: |
913 | if (saddend > 0x1fffff || saddend < -0x200000) | |
914 | return bfd_reloc_overflow; | |
435b1e90 | 915 | |
252b5132 RH |
916 | if ((addend % 2) != 0) |
917 | return bfd_reloc_dangerous; | |
435b1e90 | 918 | |
252b5132 RH |
919 | insn = bfd_get_32 (abfd, address); |
920 | insn &= ~0xfffe003f; | |
921 | insn |= (((addend & 0xfffe) << 16) | ((addend & 0x3f0000) >> 16)); | |
dc810e39 | 922 | bfd_put_32 (abfd, (bfd_vma) insn, address); |
252b5132 | 923 | return bfd_reloc_ok; |
435b1e90 | 924 | |
252b5132 RH |
925 | case R_V850_9_PCREL: |
926 | if (saddend > 0xff || saddend < -0x100) | |
927 | return bfd_reloc_overflow; | |
435b1e90 | 928 | |
252b5132 RH |
929 | if ((addend % 2) != 0) |
930 | return bfd_reloc_dangerous; | |
435b1e90 | 931 | |
252b5132 RH |
932 | insn = bfd_get_16 (abfd, address); |
933 | insn &= ~ 0xf870; | |
934 | insn |= ((addend & 0x1f0) << 7) | ((addend & 0x0e) << 3); | |
935 | break; | |
435b1e90 | 936 | |
252b5132 RH |
937 | case R_V850_HI16: |
938 | addend += (bfd_get_16 (abfd, address) << 16); | |
939 | addend = (addend >> 16); | |
940 | insn = addend; | |
941 | break; | |
435b1e90 | 942 | |
252b5132 RH |
943 | case R_V850_HI16_S: |
944 | /* Remember where this relocation took place. */ | |
945 | remember_hi16s_reloc (abfd, addend, address); | |
946 | ||
947 | addend += (bfd_get_16 (abfd, address) << 16); | |
948 | addend = (addend >> 16) + ((addend & 0x8000) != 0); | |
435b1e90 KH |
949 | |
950 | /* This relocation cannot overflow. */ | |
252b5132 RH |
951 | if (addend > 0x7fff) |
952 | addend = 0; | |
435b1e90 | 953 | |
252b5132 RH |
954 | insn = addend; |
955 | break; | |
435b1e90 | 956 | |
252b5132 RH |
957 | case R_V850_LO16: |
958 | /* Calculate the sum of the value stored in the instruction and the | |
959 | addend and check for overflow from the low 16 bits into the high | |
960 | 16 bits. The assembler has already done some of this: If the | |
961 | value stored in the instruction has its 15th bit set, (counting | |
962 | from zero) then the assembler will have added 1 to the value | |
963 | stored in the associated HI16S reloc. So for example, these | |
964 | relocations: | |
965 | ||
966 | movhi hi( fred ), r0, r1 | |
967 | movea lo( fred ), r1, r1 | |
968 | ||
969 | will store 0 in the value fields for the MOVHI and MOVEA instructions | |
970 | and addend will be the address of fred, but for these instructions: | |
971 | ||
972 | movhi hi( fred + 0x123456), r0, r1 | |
973 | movea lo( fred + 0x123456), r1, r1 | |
974 | ||
975 | the value stored in the MOVHI instruction will be 0x12 and the value | |
976 | stored in the MOVEA instruction will be 0x3456. If however the | |
977 | instructions were: | |
978 | ||
979 | movhi hi( fred + 0x10ffff), r0, r1 | |
980 | movea lo( fred + 0x10ffff), r1, r1 | |
981 | ||
982 | then the value stored in the MOVHI instruction would be 0x11 (not | |
983 | 0x10) and the value stored in the MOVEA instruction would be 0xffff. | |
984 | Thus (assuming for the moment that the addend is 0), at run time the | |
985 | MOVHI instruction loads 0x110000 into r1, then the MOVEA instruction | |
986 | adds 0xffffffff (sign extension!) producing 0x10ffff. Similarly if | |
987 | the instructions were: | |
988 | ||
989 | movhi hi( fred - 1), r0, r1 | |
990 | movea lo( fred - 1), r1, r1 | |
991 | ||
992 | then 0 is stored in the MOVHI instruction and -1 is stored in the | |
993 | MOVEA instruction. | |
994 | ||
995 | Overflow can occur if the addition of the value stored in the | |
996 | instruction plus the addend sets the 15th bit when before it was clear. | |
997 | This is because the 15th bit will be sign extended into the high part, | |
998 | thus reducing its value by one, but since the 15th bit was originally | |
999 | clear, the assembler will not have added 1 to the previous HI16S reloc | |
1000 | to compensate for this effect. For example: | |
1001 | ||
1002 | movhi hi( fred + 0x123456), r0, r1 | |
1003 | movea lo( fred + 0x123456), r1, r1 | |
1004 | ||
1005 | The value stored in HI16S reloc is 0x12, the value stored in the LO16 | |
1006 | reloc is 0x3456. If we assume that the address of fred is 0x00007000 | |
1007 | then the relocations become: | |
1008 | ||
1009 | HI16S: 0x0012 + (0x00007000 >> 16) = 0x12 | |
1010 | LO16: 0x3456 + (0x00007000 & 0xffff) = 0xa456 | |
1011 | ||
1012 | but when the instructions are executed, the MOVEA instruction's value | |
1013 | is signed extended, so the sum becomes: | |
1014 | ||
1015 | 0x00120000 | |
1016 | + 0xffffa456 | |
1017 | ------------ | |
1018 | 0x0011a456 but 'fred + 0x123456' = 0x0012a456 | |
1019 | ||
1020 | Note that if the 15th bit was set in the value stored in the LO16 | |
1021 | reloc, then we do not have to do anything: | |
1022 | ||
1023 | movhi hi( fred + 0x10ffff), r0, r1 | |
1024 | movea lo( fred + 0x10ffff), r1, r1 | |
1025 | ||
1026 | HI16S: 0x0011 + (0x00007000 >> 16) = 0x11 | |
1027 | LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff | |
1028 | ||
1029 | 0x00110000 | |
1030 | + 0x00006fff | |
1031 | ------------ | |
1032 | 0x00116fff = fred + 0x10ffff = 0x7000 + 0x10ffff | |
1033 | ||
252b5132 RH |
1034 | Overflow can also occur if the computation carries into the 16th bit |
1035 | and it also results in the 15th bit having the same value as the 15th | |
1036 | bit of the original value. What happens is that the HI16S reloc | |
1037 | will have already examined the 15th bit of the original value and | |
1038 | added 1 to the high part if the bit is set. This compensates for the | |
1039 | sign extension of 15th bit of the result of the computation. But now | |
1040 | there is a carry into the 16th bit, and this has not been allowed for. | |
1041 | ||
1042 | So, for example if fred is at address 0xf000: | |
1043 | ||
1044 | movhi hi( fred + 0xffff), r0, r1 [bit 15 of the offset is set] | |
1045 | movea lo( fred + 0xffff), r1, r1 | |
1046 | ||
1047 | HI16S: 0x0001 + (0x0000f000 >> 16) = 0x0001 | |
1048 | LO16: 0xffff + (0x0000f000 & 0xffff) = 0xefff (carry into bit 16 is lost) | |
1049 | ||
1050 | 0x00010000 | |
1051 | + 0xffffefff | |
1052 | ------------ | |
1053 | 0x0000efff but 'fred + 0xffff' = 0x0001efff | |
1054 | ||
1055 | Similarly, if the 15th bit remains clear, but overflow occurs into | |
1056 | the 16th bit then (assuming the address of fred is 0xf000): | |
1057 | ||
1058 | movhi hi( fred + 0x7000), r0, r1 [bit 15 of the offset is clear] | |
1059 | movea lo( fred + 0x7000), r1, r1 | |
1060 | ||
1061 | HI16S: 0x0000 + (0x0000f000 >> 16) = 0x0000 | |
1062 | LO16: 0x7000 + (0x0000f000 & 0xffff) = 0x6fff (carry into bit 16 is lost) | |
1063 | ||
1064 | 0x00000000 | |
1065 | + 0x00006fff | |
1066 | ------------ | |
1067 | 0x00006fff but 'fred + 0x7000' = 0x00016fff | |
435b1e90 | 1068 | |
252b5132 RH |
1069 | Note - there is no need to change anything if a carry occurs, and the |
1070 | 15th bit changes its value from being set to being clear, as the HI16S | |
1071 | reloc will have already added in 1 to the high part for us: | |
1072 | ||
1073 | movhi hi( fred + 0xffff), r0, r1 [bit 15 of the offset is set] | |
1074 | movea lo( fred + 0xffff), r1, r1 | |
1075 | ||
1076 | HI16S: 0x0001 + (0x00007000 >> 16) | |
1077 | LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff (carry into bit 16 is lost) | |
1078 | ||
1079 | 0x00010000 | |
1080 | + 0x00006fff (bit 15 not set, so the top half is zero) | |
1081 | ------------ | |
1082 | 0x00016fff which is right (assuming that fred is at 0x7000) | |
1083 | ||
1084 | but if the 15th bit goes from being clear to being set, then we must | |
1085 | once again handle overflow: | |
1086 | ||
1087 | movhi hi( fred + 0x7000), r0, r1 [bit 15 of the offset is clear] | |
1088 | movea lo( fred + 0x7000), r1, r1 | |
1089 | ||
1090 | HI16S: 0x0000 + (0x0000ffff >> 16) | |
1091 | LO16: 0x7000 + (0x0000ffff & 0xffff) = 0x6fff (carry into bit 16) | |
1092 | ||
1093 | 0x00000000 | |
1094 | + 0x00006fff (bit 15 not set, so the top half is zero) | |
1095 | ------------ | |
e12dd2ea | 1096 | 0x00006fff which is wrong (assuming that fred is at 0xffff). */ |
252b5132 RH |
1097 | { |
1098 | long result; | |
1099 | ||
1100 | insn = bfd_get_16 (abfd, address); | |
1101 | result = insn + addend; | |
1102 | ||
1103 | #define BIT15_SET(x) ((x) & 0x8000) | |
1104 | #define OVERFLOWS(a,i) ((((a) & 0xffff) + (i)) > 0xffff) | |
435b1e90 | 1105 | |
252b5132 RH |
1106 | if ((BIT15_SET (result) && ! BIT15_SET (addend)) |
1107 | || (OVERFLOWS (addend, insn) | |
1108 | && ((! BIT15_SET (insn)) || (BIT15_SET (addend))))) | |
1109 | { | |
b34976b6 AM |
1110 | bfd_boolean already_updated; |
1111 | bfd_byte *hi16s_address = find_remembered_hi16s_reloc | |
252b5132 | 1112 | (addend, & already_updated); |
435b1e90 | 1113 | |
252b5132 RH |
1114 | /* Amend the matching HI16_S relocation. */ |
1115 | if (hi16s_address != NULL) | |
1116 | { | |
1117 | if (! already_updated) | |
1118 | { | |
1119 | insn = bfd_get_16 (abfd, hi16s_address); | |
1120 | insn += 1; | |
dc810e39 | 1121 | bfd_put_16 (abfd, (bfd_vma) insn, hi16s_address); |
252b5132 RH |
1122 | } |
1123 | } | |
1124 | else | |
1125 | { | |
1126 | fprintf (stderr, _("FAILED to find previous HI16 reloc\n")); | |
1127 | return bfd_reloc_overflow; | |
1128 | } | |
1129 | } | |
435b1e90 | 1130 | |
252b5132 RH |
1131 | /* Do not complain if value has top bit set, as this has been anticipated. */ |
1132 | insn = result & 0xffff; | |
1133 | break; | |
1134 | } | |
1135 | ||
1136 | case R_V850_8: | |
1137 | addend += (char) bfd_get_8 (abfd, address); | |
1138 | ||
1139 | saddend = (bfd_signed_vma) addend; | |
435b1e90 | 1140 | |
252b5132 RH |
1141 | if (saddend > 0x7f || saddend < -0x80) |
1142 | return bfd_reloc_overflow; | |
1143 | ||
1144 | bfd_put_8 (abfd, addend, address); | |
1145 | return bfd_reloc_ok; | |
1146 | ||
1147 | case R_V850_CALLT_16_16_OFFSET: | |
1148 | addend += bfd_get_16 (abfd, address); | |
435b1e90 | 1149 | |
252b5132 | 1150 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1151 | |
252b5132 RH |
1152 | if (saddend > 0xffff || saddend < 0) |
1153 | return bfd_reloc_overflow; | |
1154 | ||
1155 | insn = addend; | |
1156 | break; | |
435b1e90 | 1157 | |
252b5132 RH |
1158 | case R_V850_16: |
1159 | ||
435b1e90 | 1160 | /* drop through */ |
252b5132 RH |
1161 | case R_V850_SDA_16_16_OFFSET: |
1162 | case R_V850_ZDA_16_16_OFFSET: | |
1163 | case R_V850_TDA_16_16_OFFSET: | |
1164 | addend += bfd_get_16 (abfd, address); | |
435b1e90 | 1165 | |
252b5132 | 1166 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1167 | |
252b5132 RH |
1168 | if (saddend > 0x7fff || saddend < -0x8000) |
1169 | return bfd_reloc_overflow; | |
1170 | ||
1171 | insn = addend; | |
1172 | break; | |
435b1e90 | 1173 | |
252b5132 RH |
1174 | case R_V850_SDA_15_16_OFFSET: |
1175 | case R_V850_ZDA_15_16_OFFSET: | |
1176 | insn = bfd_get_16 (abfd, address); | |
1177 | addend += (insn & 0xfffe); | |
435b1e90 | 1178 | |
252b5132 | 1179 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1180 | |
252b5132 RH |
1181 | if (saddend > 0x7ffe || saddend < -0x8000) |
1182 | return bfd_reloc_overflow; | |
435b1e90 | 1183 | |
252b5132 RH |
1184 | if (addend & 1) |
1185 | return bfd_reloc_dangerous; | |
435b1e90 | 1186 | |
dc810e39 | 1187 | insn = (addend &~ (bfd_vma) 1) | (insn & 1); |
252b5132 | 1188 | break; |
435b1e90 | 1189 | |
252b5132 RH |
1190 | case R_V850_TDA_6_8_OFFSET: |
1191 | insn = bfd_get_16 (abfd, address); | |
1192 | addend += ((insn & 0x7e) << 1); | |
435b1e90 | 1193 | |
252b5132 | 1194 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1195 | |
252b5132 RH |
1196 | if (saddend > 0xfc || saddend < 0) |
1197 | return bfd_reloc_overflow; | |
435b1e90 | 1198 | |
252b5132 RH |
1199 | if (addend & 3) |
1200 | return bfd_reloc_dangerous; | |
435b1e90 | 1201 | |
252b5132 RH |
1202 | insn &= 0xff81; |
1203 | insn |= (addend >> 1); | |
1204 | break; | |
435b1e90 | 1205 | |
252b5132 RH |
1206 | case R_V850_TDA_7_8_OFFSET: |
1207 | insn = bfd_get_16 (abfd, address); | |
1208 | addend += ((insn & 0x7f) << 1); | |
435b1e90 | 1209 | |
252b5132 | 1210 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1211 | |
252b5132 RH |
1212 | if (saddend > 0xfe || saddend < 0) |
1213 | return bfd_reloc_overflow; | |
435b1e90 | 1214 | |
252b5132 RH |
1215 | if (addend & 1) |
1216 | return bfd_reloc_dangerous; | |
435b1e90 | 1217 | |
252b5132 RH |
1218 | insn &= 0xff80; |
1219 | insn |= (addend >> 1); | |
1220 | break; | |
435b1e90 | 1221 | |
252b5132 RH |
1222 | case R_V850_TDA_7_7_OFFSET: |
1223 | insn = bfd_get_16 (abfd, address); | |
1224 | addend += insn & 0x7f; | |
435b1e90 | 1225 | |
252b5132 | 1226 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1227 | |
252b5132 RH |
1228 | if (saddend > 0x7f || saddend < 0) |
1229 | return bfd_reloc_overflow; | |
435b1e90 | 1230 | |
252b5132 RH |
1231 | insn &= 0xff80; |
1232 | insn |= addend; | |
1233 | break; | |
435b1e90 | 1234 | |
252b5132 RH |
1235 | case R_V850_TDA_4_5_OFFSET: |
1236 | insn = bfd_get_16 (abfd, address); | |
1237 | addend += ((insn & 0xf) << 1); | |
435b1e90 | 1238 | |
252b5132 | 1239 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1240 | |
252b5132 RH |
1241 | if (saddend > 0x1e || saddend < 0) |
1242 | return bfd_reloc_overflow; | |
435b1e90 | 1243 | |
252b5132 RH |
1244 | if (addend & 1) |
1245 | return bfd_reloc_dangerous; | |
435b1e90 | 1246 | |
252b5132 RH |
1247 | insn &= 0xfff0; |
1248 | insn |= (addend >> 1); | |
1249 | break; | |
435b1e90 | 1250 | |
252b5132 RH |
1251 | case R_V850_TDA_4_4_OFFSET: |
1252 | insn = bfd_get_16 (abfd, address); | |
1253 | addend += insn & 0xf; | |
435b1e90 | 1254 | |
252b5132 | 1255 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1256 | |
252b5132 RH |
1257 | if (saddend > 0xf || saddend < 0) |
1258 | return bfd_reloc_overflow; | |
435b1e90 | 1259 | |
252b5132 RH |
1260 | insn &= 0xfff0; |
1261 | insn |= addend; | |
1262 | break; | |
435b1e90 | 1263 | |
252b5132 RH |
1264 | case R_V850_ZDA_16_16_SPLIT_OFFSET: |
1265 | case R_V850_SDA_16_16_SPLIT_OFFSET: | |
1266 | insn = bfd_get_32 (abfd, address); | |
1267 | addend += ((insn & 0xfffe0000) >> 16) + ((insn & 0x20) >> 5); | |
435b1e90 | 1268 | |
252b5132 | 1269 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1270 | |
252b5132 RH |
1271 | if (saddend > 0x7fff || saddend < -0x8000) |
1272 | return bfd_reloc_overflow; | |
435b1e90 | 1273 | |
252b5132 RH |
1274 | insn &= 0x0001ffdf; |
1275 | insn |= (addend & 1) << 5; | |
dc810e39 | 1276 | insn |= (addend &~ (bfd_vma) 1) << 16; |
435b1e90 | 1277 | |
dc810e39 | 1278 | bfd_put_32 (abfd, (bfd_vma) insn, address); |
252b5132 | 1279 | return bfd_reloc_ok; |
435b1e90 | 1280 | |
252b5132 RH |
1281 | case R_V850_CALLT_6_7_OFFSET: |
1282 | insn = bfd_get_16 (abfd, address); | |
1283 | addend += ((insn & 0x3f) << 1); | |
435b1e90 | 1284 | |
252b5132 | 1285 | saddend = (bfd_signed_vma) addend; |
435b1e90 | 1286 | |
252b5132 RH |
1287 | if (saddend > 0x7e || saddend < 0) |
1288 | return bfd_reloc_overflow; | |
435b1e90 | 1289 | |
252b5132 RH |
1290 | if (addend & 1) |
1291 | return bfd_reloc_dangerous; | |
435b1e90 | 1292 | |
252b5132 RH |
1293 | insn &= 0xff80; |
1294 | insn |= (addend >> 1); | |
1295 | break; | |
1296 | ||
1297 | case R_V850_GNU_VTINHERIT: | |
1298 | case R_V850_GNU_VTENTRY: | |
1299 | return bfd_reloc_ok; | |
1300 | ||
1301 | } | |
1302 | ||
dc810e39 | 1303 | bfd_put_16 (abfd, (bfd_vma) insn, address); |
252b5132 RH |
1304 | return bfd_reloc_ok; |
1305 | } | |
252b5132 RH |
1306 | \f |
1307 | /* Insert the addend into the instruction. */ | |
e12dd2ea | 1308 | |
252b5132 RH |
1309 | static bfd_reloc_status_type |
1310 | v850_elf_reloc (abfd, reloc, symbol, data, isection, obfd, err) | |
b34976b6 AM |
1311 | bfd *abfd ATTRIBUTE_UNUSED; |
1312 | arelent *reloc; | |
1313 | asymbol *symbol; | |
1314 | PTR data ATTRIBUTE_UNUSED; | |
1315 | asection *isection; | |
1316 | bfd *obfd; | |
1317 | char **err ATTRIBUTE_UNUSED; | |
252b5132 RH |
1318 | { |
1319 | long relocation; | |
435b1e90 | 1320 | |
252b5132 RH |
1321 | /* If there is an output BFD, |
1322 | and the symbol is not a section name (which is only defined at final link time), | |
1323 | and either we are not putting the addend into the instruction | |
e12dd2ea | 1324 | or the addend is zero, so there is nothing to add into the instruction |
252b5132 RH |
1325 | then just fixup the address and return. */ |
1326 | if (obfd != (bfd *) NULL | |
1327 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
1328 | && (! reloc->howto->partial_inplace | |
1329 | || reloc->addend == 0)) | |
1330 | { | |
1331 | reloc->address += isection->output_offset; | |
1332 | return bfd_reloc_ok; | |
1333 | } | |
435b1e90 | 1334 | |
252b5132 RH |
1335 | /* Catch relocs involving undefined symbols. */ |
1336 | if (bfd_is_und_section (symbol->section) | |
1337 | && (symbol->flags & BSF_WEAK) == 0 | |
1338 | && obfd == NULL) | |
1339 | return bfd_reloc_undefined; | |
1340 | ||
1341 | /* We handle final linking of some relocs ourselves. */ | |
1342 | ||
1343 | /* Is the address of the relocation really within the section? */ | |
1344 | if (reloc->address > isection->_cooked_size) | |
1345 | return bfd_reloc_outofrange; | |
435b1e90 | 1346 | |
252b5132 RH |
1347 | /* Work out which section the relocation is targetted at and the |
1348 | initial relocation command value. */ | |
435b1e90 | 1349 | |
b34976b6 | 1350 | if (reloc->howto->pc_relative) |
86aba9db NC |
1351 | return bfd_reloc_ok; |
1352 | ||
252b5132 RH |
1353 | /* Get symbol value. (Common symbols are special.) */ |
1354 | if (bfd_is_com_section (symbol->section)) | |
1355 | relocation = 0; | |
1356 | else | |
1357 | relocation = symbol->value; | |
435b1e90 | 1358 | |
252b5132 RH |
1359 | /* Convert input-section-relative symbol value to absolute + addend. */ |
1360 | relocation += symbol->section->output_section->vma; | |
1361 | relocation += symbol->section->output_offset; | |
1362 | relocation += reloc->addend; | |
435b1e90 | 1363 | |
a0f2e916 NC |
1364 | #if 0 /* Since this reloc is going to be processed later on, we should |
1365 | not make it pc-relative here. To test this, try assembling and | |
1366 | linking this program: | |
1367 | ||
1368 | .text | |
1369 | .globl _start | |
1370 | nop | |
dc810e39 | 1371 | _start: |
a0f2e916 NC |
1372 | jr foo |
1373 | ||
1374 | .section ".foo","ax" | |
1375 | nop | |
1376 | foo: | |
e12dd2ea | 1377 | nop */ |
82e51918 | 1378 | if (reloc->howto->pc_relative) |
252b5132 RH |
1379 | { |
1380 | /* Here the variable relocation holds the final address of the | |
1381 | symbol we are relocating against, plus any addend. */ | |
1382 | relocation -= isection->output_section->vma + isection->output_offset; | |
435b1e90 | 1383 | |
e12dd2ea | 1384 | /* Deal with pcrel_offset. */ |
252b5132 RH |
1385 | relocation -= reloc->address; |
1386 | } | |
a0f2e916 | 1387 | #endif |
435b1e90 | 1388 | reloc->addend = relocation; |
252b5132 RH |
1389 | return bfd_reloc_ok; |
1390 | } | |
86aba9db NC |
1391 | |
1392 | /* This function is used for relocs which are only used | |
1393 | for relaxing, which the linker should otherwise ignore. */ | |
1394 | ||
1395 | static bfd_reloc_status_type | |
1396 | v850_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section, | |
1397 | output_bfd, error_message) | |
b34976b6 AM |
1398 | bfd *abfd ATTRIBUTE_UNUSED; |
1399 | arelent *reloc_entry; | |
1400 | asymbol *symbol ATTRIBUTE_UNUSED; | |
1401 | PTR data ATTRIBUTE_UNUSED; | |
1402 | asection *input_section; | |
1403 | bfd *output_bfd; | |
1404 | char **error_message ATTRIBUTE_UNUSED; | |
86aba9db NC |
1405 | { |
1406 | if (output_bfd != NULL) | |
1407 | reloc_entry->address += input_section->output_offset; | |
1408 | ||
1409 | return bfd_reloc_ok; | |
1410 | } | |
252b5132 | 1411 | \f |
b34976b6 | 1412 | static bfd_boolean |
252b5132 | 1413 | v850_elf_is_local_label_name (abfd, name) |
b34976b6 AM |
1414 | bfd *abfd ATTRIBUTE_UNUSED; |
1415 | const char *name; | |
252b5132 RH |
1416 | { |
1417 | return ( (name[0] == '.' && (name[1] == 'L' || name[1] == '.')) | |
1418 | || (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')); | |
1419 | } | |
252b5132 | 1420 | \f |
5cec6941 NC |
1421 | /* We overload some of the bfd_reloc error codes for own purposes. */ |
1422 | #define bfd_reloc_gp_not_found bfd_reloc_other | |
1423 | #define bfd_reloc_ep_not_found bfd_reloc_continue | |
1424 | #define bfd_reloc_ctbp_not_found (bfd_reloc_dangerous + 1) | |
1425 | ||
252b5132 | 1426 | /* Perform a relocation as part of a final link. */ |
e12dd2ea | 1427 | |
252b5132 RH |
1428 | static bfd_reloc_status_type |
1429 | v850_elf_final_link_relocate (howto, input_bfd, output_bfd, | |
86aba9db NC |
1430 | input_section, contents, offset, value, |
1431 | addend, info, sym_sec, is_local) | |
b34976b6 AM |
1432 | reloc_howto_type *howto; |
1433 | bfd *input_bfd; | |
1434 | bfd *output_bfd ATTRIBUTE_UNUSED; | |
1435 | asection *input_section; | |
1436 | bfd_byte *contents; | |
1437 | bfd_vma offset; | |
1438 | bfd_vma value; | |
1439 | bfd_vma addend; | |
1440 | struct bfd_link_info *info; | |
1441 | asection *sym_sec; | |
1442 | int is_local ATTRIBUTE_UNUSED; | |
252b5132 | 1443 | { |
b34976b6 AM |
1444 | unsigned int r_type = howto->type; |
1445 | bfd_byte *hit_data = contents + offset; | |
252b5132 RH |
1446 | |
1447 | /* Adjust the value according to the relocation. */ | |
1448 | switch (r_type) | |
1449 | { | |
1450 | case R_V850_9_PCREL: | |
1451 | value -= (input_section->output_section->vma | |
1452 | + input_section->output_offset); | |
1453 | value -= offset; | |
1454 | break; | |
435b1e90 | 1455 | |
252b5132 RH |
1456 | case R_V850_22_PCREL: |
1457 | value -= (input_section->output_section->vma | |
1458 | + input_section->output_offset | |
1459 | + offset); | |
1460 | ||
232fb1a3 NC |
1461 | /* If the sign extension will corrupt the value then we have overflowed. */ |
1462 | if (((value & 0xff000000) != 0x0) && ((value & 0xff000000) != 0xff000000)) | |
1463 | return bfd_reloc_overflow; | |
435b1e90 | 1464 | |
e12dd2ea NC |
1465 | /* Only the bottom 24 bits of the PC are valid */ |
1466 | value = SEXT24 (value); | |
252b5132 | 1467 | break; |
435b1e90 | 1468 | |
252b5132 RH |
1469 | case R_V850_HI16_S: |
1470 | case R_V850_HI16: | |
1471 | case R_V850_LO16: | |
1472 | case R_V850_16: | |
1473 | case R_V850_32: | |
1474 | case R_V850_8: | |
1475 | break; | |
1476 | ||
435b1e90 | 1477 | case R_V850_ZDA_15_16_OFFSET: |
252b5132 RH |
1478 | case R_V850_ZDA_16_16_OFFSET: |
1479 | case R_V850_ZDA_16_16_SPLIT_OFFSET: | |
1480 | if (sym_sec == NULL) | |
1481 | return bfd_reloc_undefined; | |
435b1e90 | 1482 | |
252b5132 RH |
1483 | value -= sym_sec->output_section->vma; |
1484 | break; | |
1485 | ||
1486 | case R_V850_SDA_15_16_OFFSET: | |
1487 | case R_V850_SDA_16_16_OFFSET: | |
1488 | case R_V850_SDA_16_16_SPLIT_OFFSET: | |
1489 | { | |
1490 | unsigned long gp; | |
1491 | struct bfd_link_hash_entry * h; | |
1492 | ||
1493 | if (sym_sec == NULL) | |
1494 | return bfd_reloc_undefined; | |
435b1e90 | 1495 | |
252b5132 | 1496 | /* Get the value of __gp. */ |
b34976b6 | 1497 | h = bfd_link_hash_lookup (info->hash, "__gp", FALSE, FALSE, TRUE); |
252b5132 RH |
1498 | if (h == (struct bfd_link_hash_entry *) NULL |
1499 | || h->type != bfd_link_hash_defined) | |
5cec6941 | 1500 | return bfd_reloc_gp_not_found; |
252b5132 RH |
1501 | |
1502 | gp = (h->u.def.value | |
1503 | + h->u.def.section->output_section->vma | |
1504 | + h->u.def.section->output_offset); | |
1505 | ||
1506 | value -= sym_sec->output_section->vma; | |
1507 | value -= (gp - sym_sec->output_section->vma); | |
1508 | } | |
1509 | break; | |
1510 | ||
1511 | case R_V850_TDA_4_4_OFFSET: | |
1512 | case R_V850_TDA_4_5_OFFSET: | |
1513 | case R_V850_TDA_16_16_OFFSET: | |
1514 | case R_V850_TDA_7_7_OFFSET: | |
1515 | case R_V850_TDA_7_8_OFFSET: | |
1516 | case R_V850_TDA_6_8_OFFSET: | |
1517 | { | |
1518 | unsigned long ep; | |
1519 | struct bfd_link_hash_entry * h; | |
435b1e90 | 1520 | |
252b5132 | 1521 | /* Get the value of __ep. */ |
b34976b6 | 1522 | h = bfd_link_hash_lookup (info->hash, "__ep", FALSE, FALSE, TRUE); |
252b5132 RH |
1523 | if (h == (struct bfd_link_hash_entry *) NULL |
1524 | || h->type != bfd_link_hash_defined) | |
5cec6941 | 1525 | return bfd_reloc_ep_not_found; |
252b5132 RH |
1526 | |
1527 | ep = (h->u.def.value | |
1528 | + h->u.def.section->output_section->vma | |
1529 | + h->u.def.section->output_offset); | |
1530 | ||
1531 | value -= ep; | |
1532 | } | |
1533 | break; | |
435b1e90 | 1534 | |
252b5132 RH |
1535 | case R_V850_CALLT_6_7_OFFSET: |
1536 | { | |
1537 | unsigned long ctbp; | |
1538 | struct bfd_link_hash_entry * h; | |
435b1e90 | 1539 | |
252b5132 | 1540 | /* Get the value of __ctbp. */ |
b34976b6 | 1541 | h = bfd_link_hash_lookup (info->hash, "__ctbp", FALSE, FALSE, TRUE); |
252b5132 RH |
1542 | if (h == (struct bfd_link_hash_entry *) NULL |
1543 | || h->type != bfd_link_hash_defined) | |
5cec6941 | 1544 | return bfd_reloc_ctbp_not_found; |
252b5132 RH |
1545 | |
1546 | ctbp = (h->u.def.value | |
1547 | + h->u.def.section->output_section->vma | |
1548 | + h->u.def.section->output_offset); | |
1549 | value -= ctbp; | |
1550 | } | |
1551 | break; | |
435b1e90 | 1552 | |
252b5132 RH |
1553 | case R_V850_CALLT_16_16_OFFSET: |
1554 | { | |
1555 | unsigned long ctbp; | |
1556 | struct bfd_link_hash_entry * h; | |
1557 | ||
1558 | if (sym_sec == NULL) | |
1559 | return bfd_reloc_undefined; | |
435b1e90 | 1560 | |
252b5132 | 1561 | /* Get the value of __ctbp. */ |
b34976b6 | 1562 | h = bfd_link_hash_lookup (info->hash, "__ctbp", FALSE, FALSE, TRUE); |
252b5132 RH |
1563 | if (h == (struct bfd_link_hash_entry *) NULL |
1564 | || h->type != bfd_link_hash_defined) | |
5cec6941 | 1565 | return bfd_reloc_ctbp_not_found; |
252b5132 RH |
1566 | |
1567 | ctbp = (h->u.def.value | |
1568 | + h->u.def.section->output_section->vma | |
1569 | + h->u.def.section->output_offset); | |
1570 | ||
1571 | value -= sym_sec->output_section->vma; | |
1572 | value -= (ctbp - sym_sec->output_section->vma); | |
1573 | } | |
1574 | break; | |
435b1e90 | 1575 | |
252b5132 RH |
1576 | case R_V850_NONE: |
1577 | case R_V850_GNU_VTINHERIT: | |
1578 | case R_V850_GNU_VTENTRY: | |
86aba9db NC |
1579 | case R_V850_LONGCALL: |
1580 | case R_V850_LONGJUMP: | |
1581 | case R_V850_ALIGN: | |
252b5132 RH |
1582 | return bfd_reloc_ok; |
1583 | ||
1584 | default: | |
1585 | return bfd_reloc_notsupported; | |
1586 | } | |
1587 | ||
1588 | /* Perform the relocation. */ | |
435b1e90 | 1589 | return v850_elf_perform_relocation (input_bfd, r_type, value + addend, hit_data); |
252b5132 | 1590 | } |
252b5132 RH |
1591 | \f |
1592 | /* Relocate an V850 ELF section. */ | |
e12dd2ea | 1593 | |
b34976b6 | 1594 | static bfd_boolean |
252b5132 RH |
1595 | v850_elf_relocate_section (output_bfd, info, input_bfd, input_section, |
1596 | contents, relocs, local_syms, local_sections) | |
b34976b6 AM |
1597 | bfd *output_bfd; |
1598 | struct bfd_link_info *info; | |
1599 | bfd *input_bfd; | |
1600 | asection *input_section; | |
1601 | bfd_byte *contents; | |
1602 | Elf_Internal_Rela *relocs; | |
1603 | Elf_Internal_Sym *local_syms; | |
1604 | asection **local_sections; | |
252b5132 | 1605 | { |
b34976b6 AM |
1606 | Elf_Internal_Shdr *symtab_hdr; |
1607 | struct elf_link_hash_entry **sym_hashes; | |
1608 | Elf_Internal_Rela *rel; | |
1609 | Elf_Internal_Rela *relend; | |
252b5132 | 1610 | |
f0fe0e16 | 1611 | if (info->relocateable) |
b34976b6 | 1612 | return TRUE; |
f0fe0e16 | 1613 | |
252b5132 RH |
1614 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; |
1615 | sym_hashes = elf_sym_hashes (input_bfd); | |
1616 | ||
1617 | if (sym_hashes == NULL) | |
1618 | { | |
1619 | info->callbacks->warning | |
dc810e39 AM |
1620 | (info, "no hash table available", |
1621 | NULL, input_bfd, input_section, (bfd_vma) 0); | |
252b5132 | 1622 | |
b34976b6 | 1623 | return FALSE; |
252b5132 | 1624 | } |
435b1e90 | 1625 | |
252b5132 RH |
1626 | /* Reset the list of remembered HI16S relocs to empty. */ |
1627 | free_hi16s = previous_hi16s; | |
1628 | previous_hi16s = NULL; | |
1629 | hi16s_counter = 0; | |
435b1e90 | 1630 | |
252b5132 RH |
1631 | rel = relocs; |
1632 | relend = relocs + input_section->reloc_count; | |
1633 | for (; rel < relend; rel++) | |
1634 | { | |
b34976b6 AM |
1635 | int r_type; |
1636 | reloc_howto_type *howto; | |
1637 | unsigned long r_symndx; | |
1638 | Elf_Internal_Sym *sym; | |
1639 | asection *sec; | |
1640 | struct elf_link_hash_entry *h; | |
1641 | bfd_vma relocation; | |
1642 | bfd_reloc_status_type r; | |
252b5132 RH |
1643 | |
1644 | r_symndx = ELF32_R_SYM (rel->r_info); | |
1645 | r_type = ELF32_R_TYPE (rel->r_info); | |
1646 | ||
1647 | if (r_type == R_V850_GNU_VTENTRY | |
1648 | || r_type == R_V850_GNU_VTINHERIT) | |
1649 | continue; | |
1650 | ||
252b5132 | 1651 | /* This is a final link. */ |
f0fe0e16 | 1652 | howto = v850_elf_howto_table + r_type; |
252b5132 RH |
1653 | h = NULL; |
1654 | sym = NULL; | |
1655 | sec = NULL; | |
1656 | if (r_symndx < symtab_hdr->sh_info) | |
1657 | { | |
1658 | sym = local_syms + r_symndx; | |
1659 | sec = local_sections[r_symndx]; | |
f8df10f4 | 1660 | relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel); |
252b5132 RH |
1661 | #if 0 |
1662 | { | |
1663 | char * name; | |
e12dd2ea | 1664 | |
252b5132 RH |
1665 | name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, sym->st_name); |
1666 | name = (name == NULL) ? "<none>" : name; | |
e12dd2ea NC |
1667 | fprintf (stderr, "local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n", |
1668 | sec->name, name, sym->st_name, | |
1669 | sec->output_section->vma, sec->output_offset, sym->st_value, rel->r_addend); | |
252b5132 RH |
1670 | } |
1671 | #endif | |
1672 | } | |
1673 | else | |
1674 | { | |
1675 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
435b1e90 | 1676 | |
252b5132 RH |
1677 | while (h->root.type == bfd_link_hash_indirect |
1678 | || h->root.type == bfd_link_hash_warning) | |
1679 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
435b1e90 | 1680 | |
252b5132 RH |
1681 | if (h->root.type == bfd_link_hash_defined |
1682 | || h->root.type == bfd_link_hash_defweak) | |
1683 | { | |
1684 | sec = h->root.u.def.section; | |
1685 | relocation = (h->root.u.def.value | |
1686 | + sec->output_section->vma | |
1687 | + sec->output_offset); | |
1688 | #if 0 | |
e12dd2ea NC |
1689 | fprintf (stderr, "defined: sec: %s, name: %s, value: %x + %x + %x gives: %x\n", |
1690 | sec->name, h->root.root.string, h->root.u.def.value, sec->output_section->vma, sec->output_offset, relocation); | |
252b5132 RH |
1691 | #endif |
1692 | } | |
1693 | else if (h->root.type == bfd_link_hash_undefweak) | |
1694 | { | |
1695 | #if 0 | |
e12dd2ea NC |
1696 | fprintf (stderr, "undefined: sec: %s, name: %s\n", |
1697 | sec->name, h->root.root.string); | |
252b5132 RH |
1698 | #endif |
1699 | relocation = 0; | |
1700 | } | |
1701 | else | |
1702 | { | |
1703 | if (! ((*info->callbacks->undefined_symbol) | |
1704 | (info, h->root.root.string, input_bfd, | |
b34976b6 AM |
1705 | input_section, rel->r_offset, TRUE))) |
1706 | return FALSE; | |
252b5132 | 1707 | #if 0 |
e12dd2ea | 1708 | fprintf (stderr, "unknown: name: %s\n", h->root.root.string); |
252b5132 RH |
1709 | #endif |
1710 | relocation = 0; | |
1711 | } | |
1712 | } | |
1713 | ||
e12dd2ea | 1714 | /* FIXME: We should use the addend, but the COFF relocations don't. */ |
252b5132 RH |
1715 | r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd, |
1716 | input_section, | |
1717 | contents, rel->r_offset, | |
1718 | relocation, rel->r_addend, | |
1719 | info, sec, h == NULL); | |
1720 | ||
1721 | if (r != bfd_reloc_ok) | |
1722 | { | |
1723 | const char * name; | |
1724 | const char * msg = (const char *)0; | |
1725 | ||
1726 | if (h != NULL) | |
1727 | name = h->root.root.string; | |
1728 | else | |
1729 | { | |
1730 | name = (bfd_elf_string_from_elf_section | |
1731 | (input_bfd, symtab_hdr->sh_link, sym->st_name)); | |
1732 | if (name == NULL || *name == '\0') | |
1733 | name = bfd_section_name (input_bfd, sec); | |
1734 | } | |
1735 | ||
1736 | switch (r) | |
1737 | { | |
1738 | case bfd_reloc_overflow: | |
1739 | if (! ((*info->callbacks->reloc_overflow) | |
1740 | (info, name, howto->name, (bfd_vma) 0, | |
1741 | input_bfd, input_section, rel->r_offset))) | |
b34976b6 | 1742 | return FALSE; |
252b5132 RH |
1743 | break; |
1744 | ||
1745 | case bfd_reloc_undefined: | |
1746 | if (! ((*info->callbacks->undefined_symbol) | |
1747 | (info, name, input_bfd, input_section, | |
b34976b6 AM |
1748 | rel->r_offset, TRUE))) |
1749 | return FALSE; | |
252b5132 RH |
1750 | break; |
1751 | ||
1752 | case bfd_reloc_outofrange: | |
1753 | msg = _("internal error: out of range error"); | |
1754 | goto common_error; | |
1755 | ||
1756 | case bfd_reloc_notsupported: | |
1757 | msg = _("internal error: unsupported relocation error"); | |
1758 | goto common_error; | |
1759 | ||
1760 | case bfd_reloc_dangerous: | |
1761 | msg = _("internal error: dangerous relocation"); | |
1762 | goto common_error; | |
1763 | ||
5cec6941 | 1764 | case bfd_reloc_gp_not_found: |
252b5132 RH |
1765 | msg = _("could not locate special linker symbol __gp"); |
1766 | goto common_error; | |
1767 | ||
5cec6941 | 1768 | case bfd_reloc_ep_not_found: |
252b5132 RH |
1769 | msg = _("could not locate special linker symbol __ep"); |
1770 | goto common_error; | |
1771 | ||
5cec6941 | 1772 | case bfd_reloc_ctbp_not_found: |
252b5132 RH |
1773 | msg = _("could not locate special linker symbol __ctbp"); |
1774 | goto common_error; | |
435b1e90 | 1775 | |
252b5132 RH |
1776 | default: |
1777 | msg = _("internal error: unknown error"); | |
1778 | /* fall through */ | |
1779 | ||
1780 | common_error: | |
1781 | if (!((*info->callbacks->warning) | |
1782 | (info, msg, name, input_bfd, input_section, | |
1783 | rel->r_offset))) | |
b34976b6 | 1784 | return FALSE; |
252b5132 RH |
1785 | break; |
1786 | } | |
1787 | } | |
1788 | } | |
1789 | ||
b34976b6 | 1790 | return TRUE; |
252b5132 RH |
1791 | } |
1792 | ||
b34976b6 | 1793 | static bfd_boolean |
252b5132 | 1794 | v850_elf_gc_sweep_hook (abfd, info, sec, relocs) |
5f771d47 ILT |
1795 | bfd *abfd ATTRIBUTE_UNUSED; |
1796 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
1797 | asection *sec ATTRIBUTE_UNUSED; | |
1798 | const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED; | |
252b5132 | 1799 | { |
e12dd2ea | 1800 | /* No got and plt entries for v850-elf. */ |
b34976b6 | 1801 | return TRUE; |
252b5132 RH |
1802 | } |
1803 | ||
1804 | static asection * | |
1e2f5b6e AM |
1805 | v850_elf_gc_mark_hook (sec, info, rel, h, sym) |
1806 | asection *sec; | |
5f771d47 | 1807 | struct bfd_link_info *info ATTRIBUTE_UNUSED; |
252b5132 RH |
1808 | Elf_Internal_Rela *rel; |
1809 | struct elf_link_hash_entry *h; | |
1810 | Elf_Internal_Sym *sym; | |
1811 | { | |
1812 | if (h != NULL) | |
1813 | { | |
1814 | switch (ELF32_R_TYPE (rel->r_info)) | |
1815 | { | |
1816 | case R_V850_GNU_VTINHERIT: | |
1817 | case R_V850_GNU_VTENTRY: | |
1818 | break; | |
1819 | ||
1820 | default: | |
1821 | switch (h->root.type) | |
1822 | { | |
1823 | case bfd_link_hash_defined: | |
1824 | case bfd_link_hash_defweak: | |
1825 | return h->root.u.def.section; | |
1826 | ||
1827 | case bfd_link_hash_common: | |
1828 | return h->root.u.c.p->section; | |
e049a0de ILT |
1829 | |
1830 | default: | |
1831 | break; | |
252b5132 RH |
1832 | } |
1833 | } | |
1834 | } | |
1835 | else | |
1e2f5b6e | 1836 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); |
9ad5cbcf | 1837 | |
252b5132 RH |
1838 | return NULL; |
1839 | } | |
e12dd2ea | 1840 | |
252b5132 | 1841 | /* Set the right machine number. */ |
e12dd2ea | 1842 | |
b34976b6 | 1843 | static bfd_boolean |
252b5132 RH |
1844 | v850_elf_object_p (abfd) |
1845 | bfd *abfd; | |
1846 | { | |
1847 | switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH) | |
1848 | { | |
1849 | default: | |
250d94fd AM |
1850 | case E_V850_ARCH: |
1851 | bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850); | |
1852 | break; | |
1853 | case E_V850E_ARCH: | |
1854 | bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e); | |
1855 | break; | |
252b5132 | 1856 | } |
b34976b6 | 1857 | return TRUE; |
252b5132 RH |
1858 | } |
1859 | ||
1860 | /* Store the machine number in the flags field. */ | |
e12dd2ea | 1861 | |
252b5132 RH |
1862 | static void |
1863 | v850_elf_final_write_processing (abfd, linker) | |
b34976b6 AM |
1864 | bfd *abfd; |
1865 | bfd_boolean linker ATTRIBUTE_UNUSED; | |
252b5132 RH |
1866 | { |
1867 | unsigned long val; | |
1868 | ||
1869 | switch (bfd_get_mach (abfd)) | |
1870 | { | |
b34976b6 | 1871 | default: |
250d94fd AM |
1872 | case bfd_mach_v850: val = E_V850_ARCH; break; |
1873 | case bfd_mach_v850e: val = E_V850E_ARCH; break; | |
252b5132 RH |
1874 | } |
1875 | ||
1876 | elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH; | |
1877 | elf_elfheader (abfd)->e_flags |= val; | |
1878 | } | |
1879 | ||
435b1e90 | 1880 | /* Function to keep V850 specific file flags. */ |
e12dd2ea | 1881 | |
b34976b6 | 1882 | static bfd_boolean |
252b5132 | 1883 | v850_elf_set_private_flags (abfd, flags) |
b34976b6 | 1884 | bfd *abfd; |
252b5132 RH |
1885 | flagword flags; |
1886 | { | |
1887 | BFD_ASSERT (!elf_flags_init (abfd) | |
1888 | || elf_elfheader (abfd)->e_flags == flags); | |
1889 | ||
1890 | elf_elfheader (abfd)->e_flags = flags; | |
b34976b6 AM |
1891 | elf_flags_init (abfd) = TRUE; |
1892 | return TRUE; | |
252b5132 RH |
1893 | } |
1894 | ||
e12dd2ea NC |
1895 | /* Merge backend specific data from an object file |
1896 | to the output object file when linking. */ | |
b34976b6 | 1897 | static bfd_boolean |
252b5132 | 1898 | v850_elf_merge_private_bfd_data (ibfd, obfd) |
b34976b6 AM |
1899 | bfd *ibfd; |
1900 | bfd *obfd; | |
252b5132 RH |
1901 | { |
1902 | flagword out_flags; | |
1903 | flagword in_flags; | |
1904 | ||
1905 | if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
1906 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
b34976b6 | 1907 | return TRUE; |
252b5132 RH |
1908 | |
1909 | in_flags = elf_elfheader (ibfd)->e_flags; | |
1910 | out_flags = elf_elfheader (obfd)->e_flags; | |
1911 | ||
1912 | if (! elf_flags_init (obfd)) | |
1913 | { | |
1914 | /* If the input is the default architecture then do not | |
1915 | bother setting the flags for the output architecture, | |
1916 | instead allow future merges to do this. If no future | |
1917 | merges ever set these flags then they will retain their | |
1918 | unitialised values, which surprise surprise, correspond | |
1919 | to the default values. */ | |
1920 | if (bfd_get_arch_info (ibfd)->the_default) | |
b34976b6 | 1921 | return TRUE; |
435b1e90 | 1922 | |
b34976b6 | 1923 | elf_flags_init (obfd) = TRUE; |
252b5132 RH |
1924 | elf_elfheader (obfd)->e_flags = in_flags; |
1925 | ||
1926 | if (bfd_get_arch (obfd) == bfd_get_arch (ibfd) | |
1927 | && bfd_get_arch_info (obfd)->the_default) | |
e12dd2ea | 1928 | return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd)); |
252b5132 | 1929 | |
b34976b6 | 1930 | return TRUE; |
252b5132 RH |
1931 | } |
1932 | ||
1933 | /* Check flag compatibility. */ | |
1934 | if (in_flags == out_flags) | |
b34976b6 | 1935 | return TRUE; |
252b5132 RH |
1936 | |
1937 | if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH) | |
1938 | && (in_flags & EF_V850_ARCH) != E_V850_ARCH) | |
1939 | _bfd_error_handler (_("%s: Architecture mismatch with previous modules"), | |
8f615d07 | 1940 | bfd_archive_filename (ibfd)); |
252b5132 | 1941 | |
b34976b6 | 1942 | return TRUE; |
252b5132 | 1943 | } |
e12dd2ea NC |
1944 | |
1945 | /* Display the flags field. */ | |
252b5132 | 1946 | |
b34976b6 | 1947 | static bfd_boolean |
252b5132 | 1948 | v850_elf_print_private_bfd_data (abfd, ptr) |
b34976b6 AM |
1949 | bfd *abfd; |
1950 | PTR ptr; | |
252b5132 RH |
1951 | { |
1952 | FILE * file = (FILE *) ptr; | |
435b1e90 | 1953 | |
252b5132 | 1954 | BFD_ASSERT (abfd != NULL && ptr != NULL); |
435b1e90 | 1955 | |
252b5132 | 1956 | _bfd_elf_print_private_bfd_data (abfd, ptr); |
435b1e90 | 1957 | |
252b5132 RH |
1958 | /* xgettext:c-format */ |
1959 | fprintf (file, _("private flags = %lx: "), elf_elfheader (abfd)->e_flags); | |
435b1e90 | 1960 | |
252b5132 RH |
1961 | switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH) |
1962 | { | |
1963 | default: | |
1964 | case E_V850_ARCH: fprintf (file, _("v850 architecture")); break; | |
1965 | case E_V850E_ARCH: fprintf (file, _("v850e architecture")); break; | |
252b5132 | 1966 | } |
435b1e90 | 1967 | |
252b5132 | 1968 | fputc ('\n', file); |
435b1e90 | 1969 | |
b34976b6 | 1970 | return TRUE; |
252b5132 RH |
1971 | } |
1972 | ||
1973 | /* V850 ELF uses four common sections. One is the usual one, and the | |
1974 | others are for (small) objects in one of the special data areas: | |
1975 | small, tiny and zero. All the objects are kept together, and then | |
1976 | referenced via the gp register, the ep register or the r0 register | |
1977 | respectively, which yields smaller, faster assembler code. This | |
1978 | approach is copied from elf32-mips.c. */ | |
1979 | ||
1980 | static asection v850_elf_scom_section; | |
1981 | static asymbol v850_elf_scom_symbol; | |
1982 | static asymbol * v850_elf_scom_symbol_ptr; | |
1983 | static asection v850_elf_tcom_section; | |
1984 | static asymbol v850_elf_tcom_symbol; | |
1985 | static asymbol * v850_elf_tcom_symbol_ptr; | |
1986 | static asection v850_elf_zcom_section; | |
1987 | static asymbol v850_elf_zcom_symbol; | |
1988 | static asymbol * v850_elf_zcom_symbol_ptr; | |
1989 | ||
e12dd2ea NC |
1990 | /* Given a BFD section, try to locate the |
1991 | corresponding ELF section index. */ | |
252b5132 | 1992 | |
b34976b6 | 1993 | static bfd_boolean |
af746e92 | 1994 | v850_elf_section_from_bfd_section (abfd, sec, retval) |
b34976b6 AM |
1995 | bfd *abfd ATTRIBUTE_UNUSED; |
1996 | asection *sec; | |
1997 | int *retval; | |
252b5132 RH |
1998 | { |
1999 | if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0) | |
2000 | *retval = SHN_V850_SCOMMON; | |
2001 | else if (strcmp (bfd_get_section_name (abfd, sec), ".tcommon") == 0) | |
2002 | *retval = SHN_V850_TCOMMON; | |
2003 | else if (strcmp (bfd_get_section_name (abfd, sec), ".zcommon") == 0) | |
2004 | *retval = SHN_V850_ZCOMMON; | |
2005 | else | |
b34976b6 | 2006 | return FALSE; |
435b1e90 | 2007 | |
b34976b6 | 2008 | return TRUE; |
252b5132 RH |
2009 | } |
2010 | ||
2011 | /* Handle the special V850 section numbers that a symbol may use. */ | |
2012 | ||
2013 | static void | |
2014 | v850_elf_symbol_processing (abfd, asym) | |
b34976b6 AM |
2015 | bfd *abfd; |
2016 | asymbol *asym; | |
252b5132 RH |
2017 | { |
2018 | elf_symbol_type * elfsym = (elf_symbol_type *) asym; | |
9ad5cbcf | 2019 | unsigned int indx; |
435b1e90 | 2020 | |
9ad5cbcf | 2021 | indx = elfsym->internal_elf_sym.st_shndx; |
252b5132 RH |
2022 | |
2023 | /* If the section index is an "ordinary" index, then it may | |
2024 | refer to a v850 specific section created by the assembler. | |
2025 | Check the section's type and change the index it matches. | |
435b1e90 | 2026 | |
252b5132 | 2027 | FIXME: Should we alter the st_shndx field as well ? */ |
435b1e90 | 2028 | |
9ad5cbcf AM |
2029 | if (indx < elf_numsections (abfd)) |
2030 | switch (elf_elfsections(abfd)[indx]->sh_type) | |
252b5132 RH |
2031 | { |
2032 | case SHT_V850_SCOMMON: | |
9ad5cbcf | 2033 | indx = SHN_V850_SCOMMON; |
252b5132 | 2034 | break; |
435b1e90 | 2035 | |
252b5132 | 2036 | case SHT_V850_TCOMMON: |
9ad5cbcf | 2037 | indx = SHN_V850_TCOMMON; |
252b5132 | 2038 | break; |
435b1e90 | 2039 | |
252b5132 | 2040 | case SHT_V850_ZCOMMON: |
9ad5cbcf | 2041 | indx = SHN_V850_ZCOMMON; |
252b5132 | 2042 | break; |
435b1e90 | 2043 | |
252b5132 RH |
2044 | default: |
2045 | break; | |
2046 | } | |
435b1e90 | 2047 | |
9ad5cbcf | 2048 | switch (indx) |
252b5132 RH |
2049 | { |
2050 | case SHN_V850_SCOMMON: | |
2051 | if (v850_elf_scom_section.name == NULL) | |
2052 | { | |
2053 | /* Initialize the small common section. */ | |
2054 | v850_elf_scom_section.name = ".scommon"; | |
2055 | v850_elf_scom_section.flags = SEC_IS_COMMON | SEC_ALLOC | SEC_DATA; | |
2056 | v850_elf_scom_section.output_section = & v850_elf_scom_section; | |
2057 | v850_elf_scom_section.symbol = & v850_elf_scom_symbol; | |
2058 | v850_elf_scom_section.symbol_ptr_ptr = & v850_elf_scom_symbol_ptr; | |
2059 | v850_elf_scom_symbol.name = ".scommon"; | |
2060 | v850_elf_scom_symbol.flags = BSF_SECTION_SYM; | |
2061 | v850_elf_scom_symbol.section = & v850_elf_scom_section; | |
2062 | v850_elf_scom_symbol_ptr = & v850_elf_scom_symbol; | |
2063 | } | |
2064 | asym->section = & v850_elf_scom_section; | |
2065 | asym->value = elfsym->internal_elf_sym.st_size; | |
2066 | break; | |
435b1e90 | 2067 | |
252b5132 RH |
2068 | case SHN_V850_TCOMMON: |
2069 | if (v850_elf_tcom_section.name == NULL) | |
2070 | { | |
2071 | /* Initialize the tcommon section. */ | |
2072 | v850_elf_tcom_section.name = ".tcommon"; | |
2073 | v850_elf_tcom_section.flags = SEC_IS_COMMON; | |
2074 | v850_elf_tcom_section.output_section = & v850_elf_tcom_section; | |
2075 | v850_elf_tcom_section.symbol = & v850_elf_tcom_symbol; | |
2076 | v850_elf_tcom_section.symbol_ptr_ptr = & v850_elf_tcom_symbol_ptr; | |
2077 | v850_elf_tcom_symbol.name = ".tcommon"; | |
2078 | v850_elf_tcom_symbol.flags = BSF_SECTION_SYM; | |
2079 | v850_elf_tcom_symbol.section = & v850_elf_tcom_section; | |
2080 | v850_elf_tcom_symbol_ptr = & v850_elf_tcom_symbol; | |
2081 | } | |
2082 | asym->section = & v850_elf_tcom_section; | |
2083 | asym->value = elfsym->internal_elf_sym.st_size; | |
2084 | break; | |
2085 | ||
2086 | case SHN_V850_ZCOMMON: | |
2087 | if (v850_elf_zcom_section.name == NULL) | |
2088 | { | |
2089 | /* Initialize the zcommon section. */ | |
2090 | v850_elf_zcom_section.name = ".zcommon"; | |
2091 | v850_elf_zcom_section.flags = SEC_IS_COMMON; | |
2092 | v850_elf_zcom_section.output_section = & v850_elf_zcom_section; | |
2093 | v850_elf_zcom_section.symbol = & v850_elf_zcom_symbol; | |
2094 | v850_elf_zcom_section.symbol_ptr_ptr = & v850_elf_zcom_symbol_ptr; | |
2095 | v850_elf_zcom_symbol.name = ".zcommon"; | |
2096 | v850_elf_zcom_symbol.flags = BSF_SECTION_SYM; | |
2097 | v850_elf_zcom_symbol.section = & v850_elf_zcom_section; | |
2098 | v850_elf_zcom_symbol_ptr = & v850_elf_zcom_symbol; | |
2099 | } | |
2100 | asym->section = & v850_elf_zcom_section; | |
2101 | asym->value = elfsym->internal_elf_sym.st_size; | |
2102 | break; | |
2103 | } | |
2104 | } | |
2105 | ||
2106 | /* Hook called by the linker routine which adds symbols from an object | |
2107 | file. We must handle the special v850 section numbers here. */ | |
2108 | ||
b34976b6 | 2109 | static bfd_boolean |
252b5132 | 2110 | v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp) |
b34976b6 AM |
2111 | bfd *abfd; |
2112 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
2113 | const Elf_Internal_Sym *sym; | |
2114 | const char **namep ATTRIBUTE_UNUSED; | |
2115 | flagword *flagsp ATTRIBUTE_UNUSED; | |
2116 | asection **secp; | |
2117 | bfd_vma *valp; | |
252b5132 | 2118 | { |
9ad5cbcf | 2119 | unsigned int indx = sym->st_shndx; |
435b1e90 | 2120 | |
252b5132 RH |
2121 | /* If the section index is an "ordinary" index, then it may |
2122 | refer to a v850 specific section created by the assembler. | |
2123 | Check the section's type and change the index it matches. | |
435b1e90 | 2124 | |
252b5132 | 2125 | FIXME: Should we alter the st_shndx field as well ? */ |
435b1e90 | 2126 | |
9ad5cbcf AM |
2127 | if (indx < elf_numsections (abfd)) |
2128 | switch (elf_elfsections(abfd)[indx]->sh_type) | |
252b5132 RH |
2129 | { |
2130 | case SHT_V850_SCOMMON: | |
9ad5cbcf | 2131 | indx = SHN_V850_SCOMMON; |
252b5132 | 2132 | break; |
435b1e90 | 2133 | |
252b5132 | 2134 | case SHT_V850_TCOMMON: |
9ad5cbcf | 2135 | indx = SHN_V850_TCOMMON; |
252b5132 | 2136 | break; |
435b1e90 | 2137 | |
252b5132 | 2138 | case SHT_V850_ZCOMMON: |
9ad5cbcf | 2139 | indx = SHN_V850_ZCOMMON; |
252b5132 | 2140 | break; |
435b1e90 | 2141 | |
252b5132 RH |
2142 | default: |
2143 | break; | |
2144 | } | |
435b1e90 | 2145 | |
9ad5cbcf | 2146 | switch (indx) |
252b5132 RH |
2147 | { |
2148 | case SHN_V850_SCOMMON: | |
2149 | *secp = bfd_make_section_old_way (abfd, ".scommon"); | |
2150 | (*secp)->flags |= SEC_IS_COMMON; | |
2151 | *valp = sym->st_size; | |
2152 | break; | |
435b1e90 | 2153 | |
252b5132 RH |
2154 | case SHN_V850_TCOMMON: |
2155 | *secp = bfd_make_section_old_way (abfd, ".tcommon"); | |
2156 | (*secp)->flags |= SEC_IS_COMMON; | |
2157 | *valp = sym->st_size; | |
2158 | break; | |
435b1e90 | 2159 | |
252b5132 RH |
2160 | case SHN_V850_ZCOMMON: |
2161 | *secp = bfd_make_section_old_way (abfd, ".zcommon"); | |
2162 | (*secp)->flags |= SEC_IS_COMMON; | |
2163 | *valp = sym->st_size; | |
2164 | break; | |
2165 | } | |
2166 | ||
b34976b6 | 2167 | return TRUE; |
252b5132 RH |
2168 | } |
2169 | ||
b34976b6 | 2170 | static bfd_boolean |
252b5132 | 2171 | v850_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec) |
b34976b6 AM |
2172 | bfd *abfd ATTRIBUTE_UNUSED; |
2173 | struct bfd_link_info *info ATTRIBUTE_UNUSED; | |
2174 | const char *name ATTRIBUTE_UNUSED; | |
2175 | Elf_Internal_Sym *sym; | |
2176 | asection *input_sec; | |
252b5132 RH |
2177 | { |
2178 | /* If we see a common symbol, which implies a relocatable link, then | |
2179 | if a symbol was in a special common section in an input file, mark | |
2180 | it as a special common in the output file. */ | |
435b1e90 | 2181 | |
252b5132 RH |
2182 | if (sym->st_shndx == SHN_COMMON) |
2183 | { | |
2184 | if (strcmp (input_sec->name, ".scommon") == 0) | |
2185 | sym->st_shndx = SHN_V850_SCOMMON; | |
2186 | else if (strcmp (input_sec->name, ".tcommon") == 0) | |
2187 | sym->st_shndx = SHN_V850_TCOMMON; | |
2188 | else if (strcmp (input_sec->name, ".zcommon") == 0) | |
2189 | sym->st_shndx = SHN_V850_ZCOMMON; | |
2190 | } | |
2191 | ||
b34976b6 | 2192 | return TRUE; |
252b5132 RH |
2193 | } |
2194 | ||
b34976b6 | 2195 | static bfd_boolean |
252b5132 | 2196 | v850_elf_section_from_shdr (abfd, hdr, name) |
b34976b6 AM |
2197 | bfd *abfd; |
2198 | Elf_Internal_Shdr *hdr; | |
2199 | const char *name; | |
252b5132 RH |
2200 | { |
2201 | /* There ought to be a place to keep ELF backend specific flags, but | |
2202 | at the moment there isn't one. We just keep track of the | |
2203 | sections by their name, instead. */ | |
2204 | ||
2205 | if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name)) | |
b34976b6 | 2206 | return FALSE; |
252b5132 RH |
2207 | |
2208 | switch (hdr->sh_type) | |
2209 | { | |
2210 | case SHT_V850_SCOMMON: | |
2211 | case SHT_V850_TCOMMON: | |
2212 | case SHT_V850_ZCOMMON: | |
2213 | if (! bfd_set_section_flags (abfd, hdr->bfd_section, | |
2214 | (bfd_get_section_flags (abfd, | |
2215 | hdr->bfd_section) | |
2216 | | SEC_IS_COMMON))) | |
b34976b6 | 2217 | return FALSE; |
252b5132 RH |
2218 | } |
2219 | ||
b34976b6 | 2220 | return TRUE; |
252b5132 RH |
2221 | } |
2222 | ||
e12dd2ea NC |
2223 | /* Set the correct type for a V850 ELF section. We do this |
2224 | by the section name, which is a hack, but ought to work. */ | |
2225 | ||
b34976b6 | 2226 | static bfd_boolean |
252b5132 | 2227 | v850_elf_fake_sections (abfd, hdr, sec) |
b34976b6 AM |
2228 | bfd *abfd ATTRIBUTE_UNUSED; |
2229 | Elf_Internal_Shdr *hdr; | |
2230 | asection *sec; | |
252b5132 RH |
2231 | { |
2232 | register const char * name; | |
2233 | ||
2234 | name = bfd_get_section_name (abfd, sec); | |
2235 | ||
2236 | if (strcmp (name, ".scommon") == 0) | |
2237 | { | |
2238 | hdr->sh_type = SHT_V850_SCOMMON; | |
2239 | } | |
2240 | else if (strcmp (name, ".tcommon") == 0) | |
2241 | { | |
2242 | hdr->sh_type = SHT_V850_TCOMMON; | |
2243 | } | |
2244 | else if (strcmp (name, ".zcommon") == 0) | |
2245 | hdr->sh_type = SHT_V850_ZCOMMON; | |
435b1e90 | 2246 | |
b34976b6 | 2247 | return TRUE; |
252b5132 | 2248 | } |
86aba9db NC |
2249 | |
2250 | /* Delete some bytes from a section while relaxing. */ | |
2251 | ||
b34976b6 | 2252 | static bfd_boolean |
86aba9db | 2253 | v850_elf_relax_delete_bytes (abfd, sec, addr, toaddr, count) |
b34976b6 AM |
2254 | bfd *abfd; |
2255 | asection *sec; | |
2256 | bfd_vma addr; | |
2257 | bfd_vma toaddr; | |
2258 | int count; | |
86aba9db | 2259 | { |
b34976b6 AM |
2260 | Elf_Internal_Shdr *symtab_hdr; |
2261 | Elf32_External_Sym *extsyms; | |
2262 | Elf32_External_Sym *esym; | |
2263 | Elf32_External_Sym *esymend; | |
2264 | int index; | |
2265 | unsigned int sec_shndx; | |
2266 | bfd_byte *contents; | |
2267 | Elf_Internal_Rela *irel; | |
2268 | Elf_Internal_Rela *irelend; | |
2269 | struct elf_link_hash_entry *sym_hash; | |
2270 | Elf_Internal_Shdr *shndx_hdr; | |
2271 | Elf_External_Sym_Shndx *shndx; | |
86aba9db NC |
2272 | |
2273 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
2274 | extsyms = (Elf32_External_Sym *) symtab_hdr->contents; | |
2275 | ||
2276 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
2277 | ||
2278 | contents = elf_section_data (sec)->this_hdr.contents; | |
2279 | ||
2280 | /* The deletion must stop at the next ALIGN reloc for an alignment | |
2281 | power larger than the number of bytes we are deleting. */ | |
2282 | ||
2283 | /* Actually delete the bytes. */ | |
2284 | #if (DEBUG_RELAX & 2) | |
2285 | fprintf (stderr, "relax_delete: contents: sec: %s %p .. %p %x\n", | |
2286 | sec->name, addr, toaddr, count ); | |
2287 | #endif | |
2288 | memmove (contents + addr, contents + addr + count, | |
2289 | toaddr - addr - count); | |
2290 | memset (contents + toaddr-count, 0, count); | |
2291 | ||
2292 | /* Adjust all the relocs. */ | |
2293 | irel = elf_section_data (sec)->relocs; | |
2294 | irelend = irel + sec->reloc_count; | |
2295 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
2296 | shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
2297 | ||
2298 | for (; irel < irelend; irel++) | |
2299 | { | |
2300 | bfd_vma raddr, paddr, symval; | |
2301 | Elf_Internal_Sym isym; | |
2302 | ||
2303 | /* Get the new reloc address. */ | |
2304 | raddr = irel->r_offset; | |
2305 | if ((raddr >= (addr + count) && raddr < toaddr)) | |
b34976b6 | 2306 | irel->r_offset -= count; |
86aba9db NC |
2307 | |
2308 | if (raddr >= addr && raddr < addr + count) | |
2309 | { | |
2310 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), | |
2311 | (int) R_V850_NONE); | |
2312 | continue; | |
2313 | } | |
b34976b6 | 2314 | |
86aba9db NC |
2315 | if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN) |
2316 | continue; | |
2317 | ||
2318 | bfd_elf32_swap_symbol_in (abfd, | |
2319 | extsyms + ELF32_R_SYM (irel->r_info), | |
2320 | shndx ? shndx + ELF32_R_SYM (irel->r_info) : NULL, | |
2321 | & isym); | |
b34976b6 | 2322 | |
86aba9db NC |
2323 | if (isym.st_shndx != sec_shndx) |
2324 | continue; | |
2325 | ||
2326 | /* Get the value of the symbol referred to by the reloc. */ | |
2327 | if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info) | |
2328 | { | |
2329 | symval = isym.st_value; | |
2330 | #if (DEBUG_RELAX & 2) | |
2331 | { | |
2332 | char * name = bfd_elf_string_from_elf_section | |
2333 | (abfd, symtab_hdr->sh_link, isym.st_name); | |
2334 | fprintf (stderr, | |
2335 | "relax_delete: local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n", | |
2336 | sec->name, name, isym.st_name, | |
2337 | sec->output_section->vma, sec->output_offset, | |
2338 | isym.st_value, irel->r_addend); | |
2339 | } | |
2340 | #endif | |
2341 | } | |
2342 | else | |
2343 | { | |
2344 | unsigned long indx; | |
2345 | struct elf_link_hash_entry * h; | |
2346 | ||
2347 | /* An external symbol. */ | |
2348 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; | |
2349 | ||
2350 | h = elf_sym_hashes (abfd) [indx]; | |
2351 | BFD_ASSERT (h != NULL); | |
2352 | ||
2353 | symval = h->root.u.def.value; | |
2354 | #if (DEBUG_RELAX & 2) | |
2355 | fprintf (stderr, | |
2356 | "relax_delete: defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n", | |
2357 | sec->name, h->root.root.string, h->root.u.def.value, | |
2358 | sec->output_section->vma, sec->output_offset, irel->r_addend); | |
2359 | #endif | |
2360 | } | |
b34976b6 | 2361 | |
86aba9db | 2362 | paddr = symval + irel->r_addend; |
b34976b6 | 2363 | |
86aba9db NC |
2364 | if ( (symval >= addr + count && symval < toaddr) |
2365 | && (paddr < addr + count || paddr >= toaddr)) | |
2366 | irel->r_addend += count; | |
2367 | else if ( (symval < addr + count || symval >= toaddr) | |
2368 | && (paddr >= addr + count && paddr < toaddr)) | |
2369 | irel->r_addend -= count; | |
2370 | } | |
2371 | ||
2372 | /* Adjust the local symbols defined in this section. */ | |
2373 | esym = extsyms; | |
2374 | esymend = esym + symtab_hdr->sh_info; | |
2375 | ||
2376 | for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL)) | |
2377 | { | |
2378 | Elf_Internal_Sym isym; | |
2379 | ||
2380 | bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym); | |
2381 | ||
2382 | if (isym.st_shndx == sec_shndx | |
2383 | && isym.st_value >= addr + count | |
2384 | && isym.st_value < toaddr) | |
2385 | { | |
2386 | isym.st_value -= count; | |
2387 | ||
2388 | if (isym.st_value + isym.st_size >= toaddr) | |
b34976b6 AM |
2389 | isym.st_size += count; |
2390 | ||
63a23799 | 2391 | bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx); |
86aba9db NC |
2392 | } |
2393 | else if (isym.st_shndx == sec_shndx | |
2394 | && isym.st_value < addr + count) | |
2395 | { | |
2396 | if (isym.st_value+isym.st_size >= addr + count | |
2397 | && isym.st_value+isym.st_size < toaddr) | |
2398 | isym.st_size -= count; | |
2399 | ||
2400 | if (isym.st_value >= addr | |
2401 | && isym.st_value < addr + count) | |
2402 | isym.st_value = addr; | |
2403 | ||
63a23799 | 2404 | bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx); |
86aba9db NC |
2405 | } |
2406 | } | |
2407 | ||
2408 | /* Now adjust the global symbols defined in this section. */ | |
2409 | esym = extsyms + symtab_hdr->sh_info; | |
2410 | esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)); | |
2411 | ||
2412 | for (index = 0; esym < esymend; esym ++, index ++) | |
2413 | { | |
2414 | Elf_Internal_Sym isym; | |
2415 | ||
2416 | bfd_elf32_swap_symbol_in (abfd, esym, shndx, & isym); | |
2417 | sym_hash = elf_sym_hashes (abfd) [index]; | |
2418 | ||
2419 | if (isym.st_shndx == sec_shndx | |
2420 | && ((sym_hash)->root.type == bfd_link_hash_defined | |
2421 | || (sym_hash)->root.type == bfd_link_hash_defweak) | |
2422 | && (sym_hash)->root.u.def.section == sec | |
2423 | && (sym_hash)->root.u.def.value >= addr + count | |
2424 | && (sym_hash)->root.u.def.value < toaddr) | |
2425 | { | |
2426 | if ((sym_hash)->root.u.def.value + isym.st_size >= toaddr) | |
2427 | { | |
2428 | isym.st_size += count; | |
63a23799 | 2429 | bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx); |
86aba9db NC |
2430 | } |
2431 | ||
2432 | (sym_hash)->root.u.def.value -= count; | |
2433 | } | |
2434 | else if (isym.st_shndx == sec_shndx | |
2435 | && ((sym_hash)->root.type == bfd_link_hash_defined | |
2436 | || (sym_hash)->root.type == bfd_link_hash_defweak) | |
2437 | && (sym_hash)->root.u.def.section == sec | |
2438 | && (sym_hash)->root.u.def.value < addr + count) | |
2439 | { | |
2440 | if ((sym_hash)->root.u.def.value+isym.st_size >= addr + count | |
2441 | && (sym_hash)->root.u.def.value+isym.st_size < toaddr) | |
2442 | isym.st_size -= count; | |
2443 | ||
2444 | if ((sym_hash)->root.u.def.value >= addr | |
2445 | && (sym_hash)->root.u.def.value < addr + count) | |
2446 | (sym_hash)->root.u.def.value = addr; | |
2447 | ||
63a23799 | 2448 | bfd_elf32_swap_symbol_out (abfd, & isym, esym, shndx); |
86aba9db NC |
2449 | } |
2450 | ||
2451 | if (shndx) | |
2452 | ++ shndx; | |
2453 | } | |
2454 | ||
b34976b6 | 2455 | return TRUE; |
86aba9db NC |
2456 | } |
2457 | ||
2458 | #define NOP_OPCODE (0x0000) | |
b34976b6 | 2459 | #define MOVHI 0x0640 /* 4byte */ |
86aba9db | 2460 | #define MOVHI_MASK 0x07e0 |
b34976b6 | 2461 | #define MOVHI_R1(insn) ((insn) & 0x1f) /* 4byte */ |
86aba9db NC |
2462 | #define MOVHI_R2(insn) ((insn) >> 11) |
2463 | #define MOVEA 0x0620 /* 2byte */ | |
2464 | #define MOVEA_MASK 0x07e0 | |
2465 | #define MOVEA_R1(insn) ((insn) & 0x1f) | |
2466 | #define MOVEA_R2(insn) ((insn) >> 11) | |
2467 | #define JARL_4 0x00040780 /* 4byte */ | |
2468 | #define JARL_4_MASK 0xFFFF07FF | |
2469 | #define JARL_R2(insn) (int)(((insn) & (~JARL_4_MASK)) >> 11) | |
2470 | #define ADD_I 0x0240 /* 2byte */ | |
2471 | #define ADD_I_MASK 0x07e0 | |
2472 | #define ADD_I5(insn) ((((insn) & 0x001f) << 11) >> 11) /* 2byte */ | |
2473 | #define ADD_R2(insn) ((insn) >> 11) | |
2474 | #define JMP_R 0x0060 /* 2byte */ | |
2475 | #define JMP_R_MASK 0xFFE0 | |
2476 | #define JMP_R1(insn) ((insn) & 0x1f) | |
2477 | ||
b34976b6 | 2478 | static bfd_boolean |
86aba9db | 2479 | v850_elf_relax_section (abfd, sec, link_info, again) |
b34976b6 AM |
2480 | bfd *abfd; |
2481 | asection *sec; | |
2482 | struct bfd_link_info *link_info; | |
2483 | bfd_boolean *again; | |
86aba9db | 2484 | { |
b34976b6 AM |
2485 | Elf_Internal_Shdr *symtab_hdr; |
2486 | Elf_Internal_Rela *internal_relocs; | |
2487 | Elf_Internal_Rela *irel; | |
2488 | Elf_Internal_Rela *irelend; | |
2489 | Elf_Internal_Rela *irelalign = NULL; | |
2490 | Elf_Internal_Sym *isymbuf = NULL; | |
2491 | bfd_byte *contents = NULL; | |
2492 | bfd_vma addr = 0; | |
2493 | bfd_vma toaddr; | |
2494 | int align_pad_size = 0; | |
2495 | bfd_boolean result = TRUE; | |
2496 | ||
2497 | *again = FALSE; | |
86aba9db NC |
2498 | |
2499 | if (link_info->relocateable | |
2500 | || (sec->flags & SEC_RELOC) == 0 | |
2501 | || sec->reloc_count == 0) | |
b34976b6 | 2502 | return TRUE; |
86aba9db NC |
2503 | |
2504 | /* If this is the first time we have been called | |
2505 | for this section, initialize the cooked size. */ | |
2506 | if (sec->_cooked_size == 0) | |
2507 | sec->_cooked_size = sec->_raw_size; | |
2508 | ||
2509 | symtab_hdr = & elf_tdata (abfd)->symtab_hdr; | |
2510 | ||
2511 | internal_relocs = (_bfd_elf32_link_read_relocs | |
2512 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
2513 | link_info->keep_memory)); | |
2514 | if (internal_relocs == NULL) | |
2515 | goto error_return; | |
86aba9db NC |
2516 | |
2517 | irelend = internal_relocs + sec->reloc_count; | |
b34976b6 | 2518 | |
86aba9db NC |
2519 | while (addr < sec->_cooked_size) |
2520 | { | |
2521 | toaddr = sec->_cooked_size; | |
2522 | ||
2523 | for (irel = internal_relocs; irel < irelend; irel ++) | |
2524 | if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN | |
2525 | && irel->r_offset > addr | |
2526 | && irel->r_offset < toaddr) | |
2527 | toaddr = irel->r_offset; | |
b34976b6 | 2528 | |
86aba9db NC |
2529 | #ifdef DEBUG_RELAX |
2530 | fprintf (stderr, "relax region 0x%x to 0x%x align pad %d\n", | |
2531 | addr, toaddr, align_pad_size); | |
2532 | #endif | |
2533 | if (irelalign) | |
2534 | { | |
2535 | bfd_vma alignto; | |
2536 | bfd_vma alignmoveto; | |
2537 | ||
2538 | alignmoveto = BFD_ALIGN (addr - align_pad_size, 1 << irelalign->r_addend); | |
2539 | alignto = BFD_ALIGN (addr, 1 << irelalign->r_addend); | |
2540 | ||
2541 | if (alignmoveto < alignto) | |
2542 | { | |
2543 | unsigned int i; | |
2544 | ||
2545 | align_pad_size = alignto - alignmoveto; | |
2546 | #ifdef DEBUG_RELAX | |
2547 | fprintf (stderr, "relax move region 0x%x to 0x%x delete size 0x%x\n", | |
2548 | alignmoveto, toaddr, align_pad_size); | |
2549 | #endif | |
2550 | if (!v850_elf_relax_delete_bytes (abfd, sec, alignmoveto, | |
2551 | toaddr, align_pad_size)) | |
b34976b6 | 2552 | goto error_return; |
86aba9db NC |
2553 | |
2554 | for (i = BFD_ALIGN (toaddr - align_pad_size, 1); | |
2555 | (i + 1) < toaddr; i += 2) | |
2556 | bfd_put_16 (abfd, NOP_OPCODE, contents + i); | |
2557 | ||
2558 | addr = alignmoveto; | |
2559 | } | |
2560 | else | |
2561 | align_pad_size = 0; | |
2562 | } | |
2563 | ||
2564 | for (irel = internal_relocs; irel < irelend; irel++) | |
2565 | { | |
b34976b6 AM |
2566 | bfd_vma laddr; |
2567 | bfd_vma addend; | |
2568 | bfd_vma symval; | |
2569 | int insn[5]; | |
2570 | int no_match = -1; | |
2571 | Elf_Internal_Rela *hi_irelfn; | |
2572 | Elf_Internal_Rela *lo_irelfn; | |
2573 | Elf_Internal_Rela *irelcall; | |
2574 | bfd_signed_vma foff; | |
86aba9db NC |
2575 | |
2576 | if (! (irel->r_offset >= addr && irel->r_offset < toaddr | |
2577 | && (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL | |
2578 | || ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP))) | |
2579 | continue; | |
2580 | ||
2581 | #ifdef DEBUG_RELAX | |
2582 | fprintf (stderr, "relax check r_info 0x%x r_offset 0x%x r_addend 0x%x\n", | |
2583 | irel->r_info, | |
2584 | irel->r_offset, | |
2585 | irel->r_addend ); | |
2586 | #endif | |
2587 | ||
2588 | /* Get the section contents. */ | |
2589 | if (contents == NULL) | |
2590 | { | |
2591 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
2592 | contents = elf_section_data (sec)->this_hdr.contents; | |
2593 | else | |
2594 | { | |
2595 | contents = (bfd_byte *) bfd_malloc (sec->_raw_size); | |
2596 | if (contents == NULL) | |
2597 | goto error_return; | |
2598 | ||
86aba9db NC |
2599 | if (! bfd_get_section_contents (abfd, sec, contents, |
2600 | (file_ptr) 0, sec->_raw_size)) | |
2601 | goto error_return; | |
2602 | } | |
2603 | } | |
2604 | ||
5cec6941 NC |
2605 | /* Read this BFD's local symbols if we haven't done so already. */ |
2606 | if (isymbuf == NULL && symtab_hdr->sh_info != 0) | |
86aba9db | 2607 | { |
5cec6941 NC |
2608 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; |
2609 | if (isymbuf == NULL) | |
2610 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
2611 | symtab_hdr->sh_info, 0, | |
2612 | NULL, NULL, NULL); | |
2613 | if (isymbuf == NULL) | |
2614 | goto error_return; | |
86aba9db NC |
2615 | } |
2616 | ||
2617 | laddr = irel->r_offset; | |
2618 | ||
2619 | if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGCALL) | |
2620 | { | |
2621 | /* Check code for -mlong-calls output. */ | |
2622 | if (laddr + 16 <= (bfd_vma) sec->_raw_size) | |
2623 | { | |
2624 | insn[0] = bfd_get_16 (abfd, contents + laddr); | |
2625 | insn[1] = bfd_get_16 (abfd, contents + laddr + 4); | |
2626 | insn[2] = bfd_get_32 (abfd, contents + laddr + 8); | |
2627 | insn[3] = bfd_get_16 (abfd, contents + laddr + 12); | |
2628 | insn[4] = bfd_get_16 (abfd, contents + laddr + 14); | |
b34976b6 | 2629 | |
86aba9db NC |
2630 | if ((insn[0] & MOVHI_MASK) != MOVHI |
2631 | || MOVHI_R1 (insn[0]) != 0) | |
2632 | no_match = 0; | |
2633 | ||
2634 | if (no_match < 0 | |
2635 | && ((insn[1] & MOVEA_MASK) != MOVEA | |
2636 | || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1]))) | |
2637 | no_match = 1; | |
2638 | ||
2639 | if (no_match < 0 | |
2640 | && (insn[2] & JARL_4_MASK) != JARL_4) | |
2641 | no_match = 2; | |
2642 | ||
2643 | if (no_match < 0 | |
2644 | && ((insn[3] & ADD_I_MASK) != ADD_I | |
2645 | || ADD_I5 (insn[3]) != 4 | |
2646 | || JARL_R2 (insn[2]) != ADD_R2 (insn[3]))) | |
2647 | no_match = 3; | |
2648 | ||
2649 | if (no_match < 0 | |
2650 | && ((insn[4] & JMP_R_MASK) != JMP_R | |
2651 | || MOVEA_R2 (insn[1]) != JMP_R1 (insn[4]))) | |
2652 | no_match = 4; | |
2653 | } | |
2654 | else | |
2655 | { | |
2656 | ((*_bfd_error_handler) | |
2657 | ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized insns", | |
2658 | bfd_get_filename (abfd), (unsigned long) irel->r_offset)); | |
2659 | ||
2660 | continue; | |
2661 | } | |
2662 | ||
2663 | if (no_match >= 0) | |
2664 | { | |
2665 | ((*_bfd_error_handler) | |
2666 | ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized insn 0x%x", | |
2667 | bfd_get_filename (abfd), (unsigned long) irel->r_offset+no_match, insn[no_match])); | |
2668 | ||
2669 | continue; | |
b34976b6 | 2670 | } |
86aba9db NC |
2671 | |
2672 | /* Get the reloc for the address from which the register is | |
2673 | being loaded. This reloc will tell us which function is | |
2674 | actually being called. */ | |
2675 | for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++) | |
2676 | if (hi_irelfn->r_offset == laddr + 2 | |
b34976b6 | 2677 | && ELF32_R_TYPE (hi_irelfn->r_info) |
86aba9db NC |
2678 | == (int) R_V850_HI16_S) |
2679 | break; | |
2680 | ||
2681 | for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++) | |
2682 | if (lo_irelfn->r_offset == laddr + 6 | |
2683 | && ELF32_R_TYPE (lo_irelfn->r_info) | |
2684 | == (int) R_V850_LO16) | |
2685 | break; | |
2686 | ||
2687 | for (irelcall = internal_relocs; irelcall < irelend; irelcall ++) | |
2688 | if (irelcall->r_offset == laddr + 8 | |
2689 | && ELF32_R_TYPE (irelcall->r_info) | |
2690 | == (int) R_V850_22_PCREL) | |
2691 | break; | |
2692 | ||
2693 | if ( hi_irelfn == irelend | |
2694 | || lo_irelfn == irelend | |
2695 | || irelcall == irelend) | |
2696 | { | |
2697 | ((*_bfd_error_handler) | |
2698 | ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized reloc", | |
2699 | bfd_get_filename (abfd), (unsigned long) irel->r_offset )); | |
2700 | ||
2701 | continue; | |
2702 | } | |
b34976b6 | 2703 | |
86aba9db NC |
2704 | if (ELF32_R_SYM (irelcall->r_info) < symtab_hdr->sh_info) |
2705 | { | |
5cec6941 | 2706 | Elf_Internal_Sym * isym; |
86aba9db NC |
2707 | |
2708 | /* A local symbol. */ | |
5cec6941 | 2709 | isym = isymbuf + ELF32_R_SYM (irelcall->r_info); |
86aba9db | 2710 | |
5cec6941 | 2711 | symval = isym->st_value; |
86aba9db NC |
2712 | } |
2713 | else | |
2714 | { | |
2715 | unsigned long indx; | |
2716 | struct elf_link_hash_entry * h; | |
2717 | ||
2718 | /* An external symbol. */ | |
2719 | indx = ELF32_R_SYM (irelcall->r_info) - symtab_hdr->sh_info; | |
2720 | h = elf_sym_hashes (abfd)[indx]; | |
2721 | BFD_ASSERT (h != NULL); | |
2722 | ||
2723 | if ( h->root.type != bfd_link_hash_defined | |
2724 | && h->root.type != bfd_link_hash_defweak) | |
2725 | /* This appears to be a reference to an undefined | |
2726 | symbol. Just ignore it--it will be caught by the | |
2727 | regular reloc processing. */ | |
2728 | continue; | |
2729 | ||
2730 | symval = h->root.u.def.value; | |
2731 | } | |
2732 | ||
2733 | if (symval + irelcall->r_addend != irelcall->r_offset + 4) | |
2734 | { | |
2735 | ((*_bfd_error_handler) | |
2736 | ("%s: 0x%lx: warning: R_V850_LONGCALL points to unrecognized reloc 0x%lx", | |
2737 | bfd_get_filename (abfd), (unsigned long) irel->r_offset, irelcall->r_offset )); | |
2738 | ||
2739 | continue; | |
2740 | } | |
2741 | ||
2742 | /* Get the value of the symbol referred to by the reloc. */ | |
2743 | if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info) | |
2744 | { | |
b34976b6 AM |
2745 | Elf_Internal_Sym *isym; |
2746 | asection *sym_sec; | |
86aba9db NC |
2747 | |
2748 | /* A local symbol. */ | |
5cec6941 | 2749 | isym = isymbuf + ELF32_R_SYM (hi_irelfn->r_info); |
b34976b6 | 2750 | |
5cec6941 | 2751 | if (isym->st_shndx == SHN_UNDEF) |
86aba9db | 2752 | sym_sec = bfd_und_section_ptr; |
5cec6941 | 2753 | else if (isym->st_shndx == SHN_ABS) |
86aba9db | 2754 | sym_sec = bfd_abs_section_ptr; |
5cec6941 | 2755 | else if (isym->st_shndx == SHN_COMMON) |
86aba9db NC |
2756 | sym_sec = bfd_com_section_ptr; |
2757 | else | |
5cec6941 NC |
2758 | sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); |
2759 | symval = (isym->st_value | |
86aba9db NC |
2760 | + sym_sec->output_section->vma |
2761 | + sym_sec->output_offset); | |
2762 | } | |
2763 | else | |
2764 | { | |
2765 | unsigned long indx; | |
b34976b6 | 2766 | struct elf_link_hash_entry *h; |
86aba9db NC |
2767 | |
2768 | /* An external symbol. */ | |
5cec6941 | 2769 | indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info; |
86aba9db NC |
2770 | h = elf_sym_hashes (abfd)[indx]; |
2771 | BFD_ASSERT (h != NULL); | |
2772 | ||
2773 | if ( h->root.type != bfd_link_hash_defined | |
2774 | && h->root.type != bfd_link_hash_defweak) | |
2775 | /* This appears to be a reference to an undefined | |
2776 | symbol. Just ignore it--it will be caught by the | |
2777 | regular reloc processing. */ | |
2778 | continue; | |
2779 | ||
2780 | symval = (h->root.u.def.value | |
2781 | + h->root.u.def.section->output_section->vma | |
2782 | + h->root.u.def.section->output_offset); | |
2783 | } | |
2784 | ||
2785 | addend = irel->r_addend; | |
2786 | ||
2787 | foff = (symval + addend | |
2788 | - (irel->r_offset | |
2789 | + sec->output_section->vma | |
2790 | + sec->output_offset | |
2791 | + 4)); | |
2792 | #ifdef DEBUG_RELAX | |
2793 | fprintf (stderr, "relax longcall r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n", | |
2794 | irel->r_offset, | |
2795 | (irel->r_offset | |
2796 | + sec->output_section->vma | |
2797 | + sec->output_offset), | |
2798 | symval, addend, foff); | |
2799 | #endif | |
2800 | ||
2801 | if (foff < -0x100000 || foff >= 0x100000) | |
2802 | /* After all that work, we can't shorten this function call. */ | |
2803 | continue; | |
2804 | ||
2805 | /* For simplicity of coding, we are going to modify the section | |
2806 | contents, the section relocs, and the BFD symbol table. We | |
2807 | must tell the rest of the code not to free up this | |
2808 | information. It would be possible to instead create a table | |
2809 | of changes which have to be made, as is done in coff-mips.c; | |
2810 | that would be more work, but would require less memory when | |
2811 | the linker is run. */ | |
2812 | elf_section_data (sec)->relocs = internal_relocs; | |
86aba9db | 2813 | elf_section_data (sec)->this_hdr.contents = contents; |
5cec6941 | 2814 | symtab_hdr->contents = (bfd_byte *) isymbuf; |
b34976b6 | 2815 | |
86aba9db NC |
2816 | /* Replace the long call with a jarl. */ |
2817 | irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_22_PCREL); | |
2818 | ||
2819 | addend = 0; | |
2820 | ||
2821 | if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info) | |
2822 | /* If this needs to be changed because of future relaxing, | |
2823 | it will be handled here like other internal IND12W | |
2824 | relocs. */ | |
2825 | bfd_put_32 (abfd, | |
2826 | 0x00000780 | (JARL_R2 (insn[2])<<11) | ((addend << 16) & 0xffff) | ((addend >> 16) & 0xf), | |
2827 | contents + irel->r_offset); | |
2828 | else | |
2829 | /* We can't fully resolve this yet, because the external | |
2830 | symbol value may be changed by future relaxing. | |
2831 | We let the final link phase handle it. */ | |
2832 | bfd_put_32 (abfd, 0x00000780 | (JARL_R2 (insn[2])<<11), | |
2833 | contents + irel->r_offset); | |
2834 | ||
b34976b6 | 2835 | hi_irelfn->r_info = |
86aba9db NC |
2836 | ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE); |
2837 | lo_irelfn->r_info = | |
2838 | ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE); | |
2839 | irelcall->r_info = | |
2840 | ELF32_R_INFO (ELF32_R_SYM (irelcall->r_info), R_V850_NONE); | |
2841 | ||
2842 | if (! v850_elf_relax_delete_bytes (abfd, sec, | |
2843 | irel->r_offset + 4, toaddr, 12)) | |
2844 | goto error_return; | |
2845 | ||
2846 | align_pad_size += 12; | |
2847 | } | |
2848 | else if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_LONGJUMP) | |
2849 | { | |
2850 | /* Check code for -mlong-jumps output. */ | |
2851 | if (laddr + 10 <= (bfd_vma) sec->_raw_size) | |
2852 | { | |
2853 | insn[0] = bfd_get_16 (abfd, contents + laddr); | |
2854 | insn[1] = bfd_get_16 (abfd, contents + laddr + 4); | |
2855 | insn[2] = bfd_get_16 (abfd, contents + laddr + 8); | |
2856 | ||
2857 | if ((insn[0] & MOVHI_MASK) != MOVHI | |
2858 | || MOVHI_R1 (insn[0]) != 0) | |
2859 | no_match = 0; | |
2860 | ||
2861 | if (no_match < 0 | |
2862 | && ((insn[1] & MOVEA_MASK) != MOVEA | |
2863 | || MOVHI_R2 (insn[0]) != MOVEA_R1 (insn[1]))) | |
2864 | no_match = 1; | |
2865 | ||
2866 | if (no_match < 0 | |
2867 | && ((insn[2] & JMP_R_MASK) != JMP_R | |
2868 | || MOVEA_R2 (insn[1]) != JMP_R1 (insn[2]))) | |
2869 | no_match = 4; | |
2870 | } | |
2871 | else | |
2872 | { | |
2873 | ((*_bfd_error_handler) | |
2874 | ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized insns", | |
2875 | bfd_get_filename (abfd), (unsigned long) irel->r_offset)); | |
2876 | ||
2877 | continue; | |
2878 | } | |
2879 | ||
2880 | if (no_match >= 0) | |
2881 | { | |
2882 | ((*_bfd_error_handler) | |
2883 | ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized insn 0x%x", | |
2884 | bfd_get_filename (abfd), (unsigned long) irel->r_offset+no_match, insn[no_match])); | |
2885 | ||
2886 | continue; | |
2887 | } | |
2888 | ||
2889 | /* Get the reloc for the address from which the register is | |
2890 | being loaded. This reloc will tell us which function is | |
2891 | actually being called. */ | |
2892 | for (hi_irelfn = internal_relocs; hi_irelfn < irelend; hi_irelfn ++) | |
2893 | if (hi_irelfn->r_offset == laddr + 2 | |
b34976b6 | 2894 | && ELF32_R_TYPE (hi_irelfn->r_info) == (int) R_V850_HI16_S) |
86aba9db NC |
2895 | break; |
2896 | ||
2897 | for (lo_irelfn = internal_relocs; lo_irelfn < irelend; lo_irelfn ++) | |
2898 | if (lo_irelfn->r_offset == laddr + 6 | |
2899 | && ELF32_R_TYPE (lo_irelfn->r_info) == (int) R_V850_LO16) | |
2900 | break; | |
2901 | ||
2902 | if ( hi_irelfn == irelend | |
2903 | || lo_irelfn == irelend) | |
2904 | { | |
2905 | ((*_bfd_error_handler) | |
2906 | ("%s: 0x%lx: warning: R_V850_LONGJUMP points to unrecognized reloc", | |
2907 | bfd_get_filename (abfd), (unsigned long) irel->r_offset )); | |
2908 | ||
2909 | continue; | |
2910 | } | |
b34976b6 | 2911 | |
86aba9db NC |
2912 | /* Get the value of the symbol referred to by the reloc. */ |
2913 | if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info) | |
2914 | { | |
5cec6941 NC |
2915 | Elf_Internal_Sym * isym; |
2916 | asection * sym_sec; | |
86aba9db NC |
2917 | |
2918 | /* A local symbol. */ | |
5cec6941 NC |
2919 | isym = isymbuf + ELF32_R_SYM (hi_irelfn->r_info); |
2920 | ||
2921 | if (isym->st_shndx == SHN_UNDEF) | |
86aba9db | 2922 | sym_sec = bfd_und_section_ptr; |
5cec6941 | 2923 | else if (isym->st_shndx == SHN_ABS) |
86aba9db | 2924 | sym_sec = bfd_abs_section_ptr; |
5cec6941 | 2925 | else if (isym->st_shndx == SHN_COMMON) |
86aba9db NC |
2926 | sym_sec = bfd_com_section_ptr; |
2927 | else | |
5cec6941 NC |
2928 | sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx); |
2929 | symval = (isym->st_value | |
86aba9db NC |
2930 | + sym_sec->output_section->vma |
2931 | + sym_sec->output_offset); | |
2932 | #ifdef DEBUG_RELAX | |
2933 | { | |
2934 | char * name = bfd_elf_string_from_elf_section | |
5cec6941 | 2935 | (abfd, symtab_hdr->sh_link, isym->st_name); |
86aba9db NC |
2936 | |
2937 | fprintf (stderr, "relax long jump local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n", | |
5cec6941 NC |
2938 | sym_sec->name, name, isym->st_name, |
2939 | sym_sec->output_section->vma, | |
2940 | sym_sec->output_offset, | |
2941 | isym->st_value, irel->r_addend); | |
86aba9db NC |
2942 | } |
2943 | #endif | |
2944 | } | |
2945 | else | |
2946 | { | |
2947 | unsigned long indx; | |
2948 | struct elf_link_hash_entry * h; | |
2949 | ||
2950 | /* An external symbol. */ | |
2951 | indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info; | |
2952 | h = elf_sym_hashes (abfd)[indx]; | |
2953 | BFD_ASSERT (h != NULL); | |
2954 | ||
2955 | if ( h->root.type != bfd_link_hash_defined | |
2956 | && h->root.type != bfd_link_hash_defweak) | |
2957 | /* This appears to be a reference to an undefined | |
2958 | symbol. Just ignore it--it will be caught by the | |
2959 | regular reloc processing. */ | |
2960 | continue; | |
2961 | ||
2962 | symval = (h->root.u.def.value | |
2963 | + h->root.u.def.section->output_section->vma | |
2964 | + h->root.u.def.section->output_offset); | |
2965 | #ifdef DEBUG_RELAX | |
2966 | fprintf (stderr, | |
2967 | "relax longjump defined: sec: %s, name: %s, value: %x + %x + %x addend %x\n", | |
2968 | sec->name, h->root.root.string, h->root.u.def.value, | |
2969 | sec->output_section->vma, sec->output_offset, irel->r_addend); | |
2970 | #endif | |
2971 | } | |
2972 | ||
2973 | addend = irel->r_addend; | |
2974 | ||
2975 | foff = (symval + addend | |
2976 | - (irel->r_offset | |
2977 | + sec->output_section->vma | |
2978 | + sec->output_offset | |
2979 | + 4)); | |
2980 | #ifdef DEBUG_RELAX | |
2981 | fprintf (stderr, "relax longjump r_offset 0x%x ptr 0x%x symbol 0x%x addend 0x%x distance 0x%x\n", | |
2982 | irel->r_offset, | |
2983 | (irel->r_offset | |
2984 | + sec->output_section->vma | |
2985 | + sec->output_offset), | |
2986 | symval, addend, foff); | |
2987 | #endif | |
2988 | if (foff < -0x100000 || foff >= 0x100000) | |
2989 | /* After all that work, we can't shorten this function call. */ | |
2990 | continue; | |
2991 | ||
2992 | /* For simplicity of coding, we are going to modify the section | |
2993 | contents, the section relocs, and the BFD symbol table. We | |
2994 | must tell the rest of the code not to free up this | |
2995 | information. It would be possible to instead create a table | |
2996 | of changes which have to be made, as is done in coff-mips.c; | |
2997 | that would be more work, but would require less memory when | |
2998 | the linker is run. */ | |
2999 | elf_section_data (sec)->relocs = internal_relocs; | |
86aba9db | 3000 | elf_section_data (sec)->this_hdr.contents = contents; |
5cec6941 | 3001 | symtab_hdr->contents = (bfd_byte *) isymbuf; |
86aba9db NC |
3002 | |
3003 | if (foff < -0x100 || foff >= 0x100) | |
3004 | { | |
3005 | /* Replace the long jump with a jr. */ | |
3006 | ||
3007 | irel->r_info = | |
b34976b6 AM |
3008 | ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_22_PCREL); |
3009 | ||
86aba9db NC |
3010 | irel->r_addend = addend; |
3011 | addend = 0; | |
3012 | ||
3013 | if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info) | |
3014 | /* If this needs to be changed because of future relaxing, | |
3015 | it will be handled here like other internal IND12W | |
3016 | relocs. */ | |
3017 | bfd_put_32 (abfd, | |
3018 | 0x00000780 | ((addend << 15) & 0xffff0000) | ((addend >> 17) & 0xf), | |
3019 | contents + irel->r_offset); | |
3020 | else | |
3021 | /* We can't fully resolve this yet, because the external | |
3022 | symbol value may be changed by future relaxing. | |
3023 | We let the final link phase handle it. */ | |
3024 | bfd_put_32 (abfd, 0x00000780, contents + irel->r_offset); | |
3025 | ||
3026 | hi_irelfn->r_info = | |
3027 | ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE); | |
3028 | lo_irelfn->r_info = | |
3029 | ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE); | |
3030 | if (!v850_elf_relax_delete_bytes (abfd, sec, | |
3031 | irel->r_offset + 4, toaddr, 6)) | |
3032 | goto error_return; | |
3033 | ||
3034 | align_pad_size += 6; | |
3035 | } | |
3036 | else | |
3037 | { | |
3038 | /* Replace the long jump with a br. */ | |
3039 | ||
3040 | irel->r_info = | |
3041 | ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_V850_9_PCREL); | |
3042 | ||
3043 | irel->r_addend = addend; | |
3044 | addend = 0; | |
3045 | ||
3046 | if (ELF32_R_SYM (hi_irelfn->r_info) < symtab_hdr->sh_info) | |
3047 | /* If this needs to be changed because of future relaxing, | |
3048 | it will be handled here like other internal IND12W | |
3049 | relocs. */ | |
3050 | bfd_put_16 (abfd, | |
3051 | 0x0585 | ((addend << 10) & 0xf800) | ((addend << 3) & 0x0070), | |
3052 | contents + irel->r_offset); | |
3053 | else | |
3054 | /* We can't fully resolve this yet, because the external | |
3055 | symbol value may be changed by future relaxing. | |
3056 | We let the final link phase handle it. */ | |
3057 | bfd_put_16 (abfd, 0x0585, contents + irel->r_offset); | |
3058 | ||
3059 | hi_irelfn->r_info = | |
3060 | ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_V850_NONE); | |
3061 | lo_irelfn->r_info = | |
3062 | ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_V850_NONE); | |
3063 | if (!v850_elf_relax_delete_bytes (abfd, sec, | |
3064 | irel->r_offset + 2, toaddr, 8)) | |
3065 | goto error_return; | |
3066 | ||
3067 | align_pad_size += 8; | |
3068 | } | |
3069 | } | |
3070 | } | |
3071 | ||
3072 | irelalign = NULL; | |
3073 | for (irel = internal_relocs; irel < irelend; irel++) | |
3074 | { | |
3075 | if (ELF32_R_TYPE (irel->r_info) == (int) R_V850_ALIGN | |
3076 | && irel->r_offset == toaddr) | |
3077 | { | |
3078 | irel->r_offset -= align_pad_size; | |
3079 | ||
3080 | if (irelalign == NULL || irelalign->r_addend > irel->r_addend) | |
3081 | irelalign = irel; | |
3082 | } | |
3083 | } | |
3084 | ||
3085 | addr = toaddr; | |
3086 | } | |
3087 | ||
3088 | if (!irelalign) | |
3089 | { | |
3090 | #ifdef DEBUG_RELAX | |
3091 | fprintf (stderr, "relax pad %d shorten %d -> %d\n", | |
3092 | align_pad_size, | |
3093 | sec->_cooked_size, | |
3094 | sec->_cooked_size - align_pad_size); | |
3095 | #endif | |
3096 | sec->_cooked_size -= align_pad_size; | |
3097 | } | |
3098 | ||
5cec6941 NC |
3099 | finish: |
3100 | if (internal_relocs != NULL | |
3101 | && elf_section_data (sec)->relocs != internal_relocs) | |
3102 | free (internal_relocs); | |
86aba9db | 3103 | |
5cec6941 NC |
3104 | if (contents != NULL |
3105 | && elf_section_data (sec)->this_hdr.contents != (unsigned char *) contents) | |
3106 | free (contents); | |
86aba9db | 3107 | |
5cec6941 NC |
3108 | if (isymbuf != NULL |
3109 | && symtab_hdr->contents != (bfd_byte *) isymbuf) | |
3110 | free (isymbuf); | |
86aba9db | 3111 | |
5cec6941 | 3112 | return result; |
86aba9db | 3113 | |
5cec6941 | 3114 | error_return: |
b34976b6 | 3115 | result = FALSE; |
5cec6941 | 3116 | goto finish; |
86aba9db | 3117 | } |
252b5132 RH |
3118 | \f |
3119 | #define TARGET_LITTLE_SYM bfd_elf32_v850_vec | |
3120 | #define TARGET_LITTLE_NAME "elf32-v850" | |
3121 | #define ELF_ARCH bfd_arch_v850 | |
aa4f99bb AO |
3122 | #define ELF_MACHINE_CODE EM_V850 |
3123 | #define ELF_MACHINE_ALT1 EM_CYGNUS_V850 | |
252b5132 | 3124 | #define ELF_MAXPAGESIZE 0x1000 |
435b1e90 | 3125 | |
252b5132 RH |
3126 | #define elf_info_to_howto v850_elf_info_to_howto_rela |
3127 | #define elf_info_to_howto_rel v850_elf_info_to_howto_rel | |
3128 | ||
3129 | #define elf_backend_check_relocs v850_elf_check_relocs | |
3130 | #define elf_backend_relocate_section v850_elf_relocate_section | |
3131 | #define elf_backend_object_p v850_elf_object_p | |
3132 | #define elf_backend_final_write_processing v850_elf_final_write_processing | |
3133 | #define elf_backend_section_from_bfd_section v850_elf_section_from_bfd_section | |
3134 | #define elf_backend_symbol_processing v850_elf_symbol_processing | |
3135 | #define elf_backend_add_symbol_hook v850_elf_add_symbol_hook | |
3136 | #define elf_backend_link_output_symbol_hook v850_elf_link_output_symbol_hook | |
3137 | #define elf_backend_section_from_shdr v850_elf_section_from_shdr | |
3138 | #define elf_backend_fake_sections v850_elf_fake_sections | |
3139 | #define elf_backend_gc_mark_hook v850_elf_gc_mark_hook | |
3140 | #define elf_backend_gc_sweep_hook v850_elf_gc_sweep_hook | |
3141 | ||
3142 | #define elf_backend_can_gc_sections 1 | |
f0fe0e16 | 3143 | #define elf_backend_rela_normal 1 |
252b5132 | 3144 | |
252b5132 RH |
3145 | #define bfd_elf32_bfd_is_local_label_name v850_elf_is_local_label_name |
3146 | #define bfd_elf32_bfd_reloc_type_lookup v850_elf_reloc_type_lookup | |
252b5132 RH |
3147 | #define bfd_elf32_bfd_merge_private_bfd_data v850_elf_merge_private_bfd_data |
3148 | #define bfd_elf32_bfd_set_private_flags v850_elf_set_private_flags | |
3149 | #define bfd_elf32_bfd_print_private_bfd_data v850_elf_print_private_bfd_data | |
86aba9db | 3150 | #define bfd_elf32_bfd_relax_section v850_elf_relax_section |
252b5132 RH |
3151 | |
3152 | #define elf_symbol_leading_char '_' | |
3153 | ||
3154 | #include "elf32-target.h" |