]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-v850.c
* common.h (EM_AVR_OLD): Renamed from...
[thirdparty/binutils-gdb.git] / bfd / elf32-v850.c
CommitLineData
252b5132 1/* V850-specific support for 32-bit ELF
7898deda
NC
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
252b5132
RH
4
5This file is part of BFD, the Binary File Descriptor library.
6
7This program is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2 of the License, or
10(at your option) any later version.
11
12This program is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with this program; if not, write to the Free Software
19Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
252b5132
RH
21/* XXX FIXME: This code is littered with 32bit int, 16bit short, 8bit char
22 dependencies. As is the gas & simulator code or the v850. */
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. */
252b5132 33#define SEXT24(x) ((((x) & 0xffffff) ^ (~ 0x7fffff)) + 0x800000)
435b1e90 34
252b5132
RH
35static reloc_howto_type *v850_elf_reloc_type_lookup
36 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
37static void v850_elf_info_to_howto_rel
38 PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
39static void v850_elf_info_to_howto_rela
40 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
41static bfd_reloc_status_type v850_elf_reloc
42 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
43static boolean v850_elf_is_local_label_name
44 PARAMS ((bfd *, const char *));
45static boolean v850_elf_relocate_section
46 PARAMS((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
47 Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
48static bfd_reloc_status_type v850_elf_perform_relocation
49 PARAMS ((bfd *, int, bfd_vma, bfd_byte *));
50static boolean v850_elf_check_relocs
51 PARAMS ((bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *));
52static void remember_hi16s_reloc
53 PARAMS ((bfd *, bfd_vma, bfd_byte *));
54static bfd_byte * find_remembered_hi16s_reloc
55 PARAMS ((bfd_vma, boolean *));
56static 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));
59static boolean v850_elf_object_p
60 PARAMS ((bfd *));
61static boolean v850_elf_fake_sections
62 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *));
63static void v850_elf_final_write_processing
64 PARAMS ((bfd *, boolean));
65static boolean v850_elf_set_private_flags
66 PARAMS ((bfd *, flagword));
67static boolean v850_elf_copy_private_bfd_data
68 PARAMS ((bfd *, bfd *));
69static boolean v850_elf_merge_private_bfd_data
70 PARAMS ((bfd *, bfd *));
71static boolean v850_elf_print_private_bfd_data
72 PARAMS ((bfd *, PTR));
73static boolean v850_elf_section_from_bfd_section
74 PARAMS ((bfd *, Elf32_Internal_Shdr *, asection *, int *));
75static void v850_elf_symbol_processing
76 PARAMS ((bfd *, asymbol *));
77static boolean v850_elf_add_symbol_hook
78 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
79 const char **, flagword *, asection **, bfd_vma *));
80static boolean v850_elf_link_output_symbol_hook
81 PARAMS ((bfd *, struct bfd_link_info *, const char *,
82 Elf_Internal_Sym *, asection *));
83static boolean v850_elf_section_from_shdr
84 PARAMS ((bfd *, Elf_Internal_Shdr *, char *));
e12dd2ea
NC
85static boolean v850_elf_gc_sweep_hook
86 PARAMS ((bfd *, struct bfd_link_info *, asection *,
87 const Elf_Internal_Rela *));
88static asection * v850_elf_gc_mark_hook
89 PARAMS ((bfd *, struct bfd_link_info *,
90 Elf_Internal_Rela *, struct elf_link_hash_entry *,
91 Elf_Internal_Sym *));
92
93/* Note: It is REQUIRED that the 'type' value of each entry
94 in this array match the index of the entry in the array. */
252b5132
RH
95static reloc_howto_type v850_elf_howto_table[] =
96{
97 /* This reloc does nothing. */
98 HOWTO (R_V850_NONE, /* type */
99 0, /* rightshift */
100 2, /* size (0 = byte, 1 = short, 2 = long) */
101 32, /* bitsize */
102 false, /* pc_relative */
103 0, /* bitpos */
104 complain_overflow_bitfield, /* complain_on_overflow */
105 bfd_elf_generic_reloc, /* special_function */
106 "R_V850_NONE", /* name */
107 false, /* partial_inplace */
108 0, /* src_mask */
109 0, /* dst_mask */
110 false), /* pcrel_offset */
111
435b1e90 112 /* A PC relative 9 bit branch. */
252b5132
RH
113 HOWTO (R_V850_9_PCREL, /* type */
114 2, /* rightshift */
115 2, /* size (0 = byte, 1 = short, 2 = long) */
116 26, /* bitsize */
117 true, /* pc_relative */
118 0, /* bitpos */
119 complain_overflow_bitfield, /* complain_on_overflow */
120 v850_elf_reloc, /* special_function */
121 "R_V850_9_PCREL", /* name */
122 false, /* partial_inplace */
123 0x00ffffff, /* src_mask */
124 0x00ffffff, /* dst_mask */
125 true), /* pcrel_offset */
126
435b1e90 127 /* A PC relative 22 bit branch. */
252b5132
RH
128 HOWTO (R_V850_22_PCREL, /* type */
129 2, /* rightshift */
130 2, /* size (0 = byte, 1 = short, 2 = long) */
131 22, /* bitsize */
132 true, /* pc_relative */
133 7, /* bitpos */
134 complain_overflow_signed, /* complain_on_overflow */
135 v850_elf_reloc, /* special_function */
136 "R_V850_22_PCREL", /* name */
137 false, /* partial_inplace */
138 0x07ffff80, /* src_mask */
139 0x07ffff80, /* dst_mask */
140 true), /* pcrel_offset */
141
142 /* High 16 bits of symbol value. */
143 HOWTO (R_V850_HI16_S, /* type */
144 0, /* rightshift */
145 1, /* size (0 = byte, 1 = short, 2 = long) */
146 16, /* bitsize */
147 false, /* pc_relative */
148 0, /* bitpos */
149 complain_overflow_dont, /* complain_on_overflow */
150 v850_elf_reloc, /* special_function */
151 "R_V850_HI16_S", /* name */
5c746d90 152 false, /* partial_inplace */
252b5132
RH
153 0xffff, /* src_mask */
154 0xffff, /* dst_mask */
155 false), /* pcrel_offset */
156
157 /* High 16 bits of symbol value. */
158 HOWTO (R_V850_HI16, /* type */
159 0, /* rightshift */
160 1, /* size (0 = byte, 1 = short, 2 = long) */
161 16, /* bitsize */
162 false, /* pc_relative */
163 0, /* bitpos */
164 complain_overflow_dont, /* complain_on_overflow */
165 v850_elf_reloc, /* special_function */
166 "R_V850_HI16", /* name */
5c746d90 167 false, /* partial_inplace */
252b5132
RH
168 0xffff, /* src_mask */
169 0xffff, /* dst_mask */
170 false), /* pcrel_offset */
171
172 /* Low 16 bits of symbol value. */
173 HOWTO (R_V850_LO16, /* type */
174 0, /* rightshift */
175 1, /* size (0 = byte, 1 = short, 2 = long) */
176 16, /* bitsize */
177 false, /* pc_relative */
178 0, /* bitpos */
179 complain_overflow_dont, /* complain_on_overflow */
180 v850_elf_reloc, /* special_function */
181 "R_V850_LO16", /* name */
5c746d90 182 false, /* partial_inplace */
252b5132
RH
183 0xffff, /* src_mask */
184 0xffff, /* dst_mask */
185 false), /* pcrel_offset */
186
187 /* Simple 32bit reloc. */
188 HOWTO (R_V850_32, /* type */
189 0, /* rightshift */
190 2, /* size (0 = byte, 1 = short, 2 = long) */
191 32, /* bitsize */
192 false, /* pc_relative */
193 0, /* bitpos */
194 complain_overflow_dont, /* complain_on_overflow */
195 v850_elf_reloc, /* special_function */
196 "R_V850_32", /* name */
5c746d90 197 false, /* partial_inplace */
252b5132
RH
198 0xffffffff, /* src_mask */
199 0xffffffff, /* dst_mask */
200 false), /* pcrel_offset */
201
202 /* Simple 16bit reloc. */
203 HOWTO (R_V850_16, /* type */
204 0, /* rightshift */
205 1, /* size (0 = byte, 1 = short, 2 = long) */
206 16, /* bitsize */
207 false, /* pc_relative */
208 0, /* bitpos */
209 complain_overflow_dont, /* complain_on_overflow */
210 bfd_elf_generic_reloc, /* special_function */
211 "R_V850_16", /* name */
5c746d90 212 false, /* partial_inplace */
252b5132
RH
213 0xffff, /* src_mask */
214 0xffff, /* dst_mask */
215 false), /* pcrel_offset */
216
217 /* Simple 8bit reloc. */
218 HOWTO (R_V850_8, /* type */
219 0, /* rightshift */
220 0, /* size (0 = byte, 1 = short, 2 = long) */
221 8, /* bitsize */
222 false, /* pc_relative */
223 0, /* bitpos */
224 complain_overflow_dont, /* complain_on_overflow */
225 bfd_elf_generic_reloc, /* special_function */
226 "R_V850_8", /* name */
5c746d90 227 false, /* partial_inplace */
252b5132
RH
228 0xff, /* src_mask */
229 0xff, /* dst_mask */
230 false), /* pcrel_offset */
231
232 /* 16 bit offset from the short data area pointer. */
233 HOWTO (R_V850_SDA_16_16_OFFSET, /* type */
234 0, /* rightshift */
235 1, /* size (0 = byte, 1 = short, 2 = long) */
236 16, /* bitsize */
237 false, /* pc_relative */
238 0, /* bitpos */
239 complain_overflow_dont, /* complain_on_overflow */
240 v850_elf_reloc, /* special_function */
241 "R_V850_SDA_16_16_OFFSET", /* name */
242 false, /* partial_inplace */
243 0xffff, /* src_mask */
244 0xffff, /* dst_mask */
245 false), /* pcrel_offset */
246
247 /* 15 bit offset from the short data area pointer. */
248 HOWTO (R_V850_SDA_15_16_OFFSET, /* type */
249 1, /* rightshift */
250 1, /* size (0 = byte, 1 = short, 2 = long) */
251 16, /* bitsize */
252 false, /* pc_relative */
253 1, /* bitpos */
254 complain_overflow_dont, /* complain_on_overflow */
255 v850_elf_reloc, /* special_function */
256 "R_V850_SDA_15_16_OFFSET", /* name */
257 false, /* partial_inplace */
258 0xfffe, /* src_mask */
259 0xfffe, /* dst_mask */
260 false), /* pcrel_offset */
261
262 /* 16 bit offset from the zero data area pointer. */
263 HOWTO (R_V850_ZDA_16_16_OFFSET, /* type */
264 0, /* rightshift */
265 1, /* size (0 = byte, 1 = short, 2 = long) */
266 16, /* bitsize */
267 false, /* pc_relative */
268 0, /* bitpos */
269 complain_overflow_dont, /* complain_on_overflow */
270 v850_elf_reloc, /* special_function */
271 "R_V850_ZDA_16_16_OFFSET", /* name */
272 false, /* partial_inplace */
273 0xffff, /* src_mask */
274 0xffff, /* dst_mask */
275 false), /* pcrel_offset */
276
277 /* 15 bit offset from the zero data area pointer. */
278 HOWTO (R_V850_ZDA_15_16_OFFSET, /* type */
279 1, /* rightshift */
280 1, /* size (0 = byte, 1 = short, 2 = long) */
281 16, /* bitsize */
282 false, /* pc_relative */
283 1, /* bitpos */
284 complain_overflow_dont, /* complain_on_overflow */
285 v850_elf_reloc, /* special_function */
286 "R_V850_ZDA_15_16_OFFSET", /* name */
287 false, /* partial_inplace */
288 0xfffe, /* src_mask */
289 0xfffe, /* dst_mask */
290 false), /* pcrel_offset */
291
292 /* 6 bit offset from the tiny data area pointer. */
293 HOWTO (R_V850_TDA_6_8_OFFSET, /* type */
294 2, /* rightshift */
295 1, /* size (0 = byte, 1 = short, 2 = long) */
296 8, /* bitsize */
297 false, /* pc_relative */
298 1, /* bitpos */
299 complain_overflow_dont, /* complain_on_overflow */
300 v850_elf_reloc, /* special_function */
301 "R_V850_TDA_6_8_OFFSET", /* name */
302 false, /* partial_inplace */
303 0x7e, /* src_mask */
304 0x7e, /* dst_mask */
305 false), /* pcrel_offset */
306
307 /* 8 bit offset from the tiny data area pointer. */
308 HOWTO (R_V850_TDA_7_8_OFFSET, /* type */
309 1, /* rightshift */
310 1, /* size (0 = byte, 1 = short, 2 = long) */
311 8, /* bitsize */
312 false, /* pc_relative */
313 0, /* bitpos */
314 complain_overflow_dont, /* complain_on_overflow */
315 v850_elf_reloc, /* special_function */
316 "R_V850_TDA_7_8_OFFSET", /* name */
317 false, /* partial_inplace */
318 0x7f, /* src_mask */
319 0x7f, /* dst_mask */
320 false), /* pcrel_offset */
435b1e90 321
252b5132
RH
322 /* 7 bit offset from the tiny data area pointer. */
323 HOWTO (R_V850_TDA_7_7_OFFSET, /* type */
324 0, /* rightshift */
325 1, /* size (0 = byte, 1 = short, 2 = long) */
326 7, /* bitsize */
327 false, /* pc_relative */
328 0, /* bitpos */
329 complain_overflow_dont, /* complain_on_overflow */
330 v850_elf_reloc, /* special_function */
331 "R_V850_TDA_7_7_OFFSET", /* name */
332 false, /* partial_inplace */
333 0x7f, /* src_mask */
334 0x7f, /* dst_mask */
335 false), /* pcrel_offset */
336
337 /* 16 bit offset from the tiny data area pointer! */
338 HOWTO (R_V850_TDA_16_16_OFFSET, /* type */
339 0, /* rightshift */
340 1, /* size (0 = byte, 1 = short, 2 = long) */
341 16, /* bitsize */
342 false, /* pc_relative */
343 0, /* bitpos */
344 complain_overflow_dont, /* complain_on_overflow */
345 v850_elf_reloc, /* special_function */
346 "R_V850_TDA_16_16_OFFSET", /* name */
347 false, /* partial_inplace */
348 0xffff, /* src_mask */
349 0xfff, /* dst_mask */
350 false), /* pcrel_offset */
351
352 /* 5 bit offset from the tiny data area pointer. */
353 HOWTO (R_V850_TDA_4_5_OFFSET, /* type */
354 1, /* rightshift */
355 1, /* size (0 = byte, 1 = short, 2 = long) */
356 5, /* bitsize */
357 false, /* pc_relative */
358 0, /* bitpos */
359 complain_overflow_dont, /* complain_on_overflow */
360 v850_elf_reloc, /* special_function */
361 "R_V850_TDA_4_5_OFFSET", /* name */
362 false, /* partial_inplace */
363 0x0f, /* src_mask */
364 0x0f, /* dst_mask */
365 false), /* pcrel_offset */
366
367 /* 4 bit offset from the tiny data area pointer. */
368 HOWTO (R_V850_TDA_4_4_OFFSET, /* type */
369 0, /* rightshift */
370 1, /* size (0 = byte, 1 = short, 2 = long) */
371 4, /* bitsize */
372 false, /* pc_relative */
373 0, /* bitpos */
374 complain_overflow_dont, /* complain_on_overflow */
375 v850_elf_reloc, /* special_function */
376 "R_V850_TDA_4_4_OFFSET", /* name */
377 false, /* partial_inplace */
378 0x0f, /* src_mask */
379 0x0f, /* dst_mask */
380 false), /* pcrel_offset */
381
382 /* 16 bit offset from the short data area pointer. */
383 HOWTO (R_V850_SDA_16_16_SPLIT_OFFSET, /* type */
384 0, /* rightshift */
385 2, /* size (0 = byte, 1 = short, 2 = long) */
386 16, /* bitsize */
387 false, /* pc_relative */
388 0, /* bitpos */
389 complain_overflow_dont, /* complain_on_overflow */
390 v850_elf_reloc, /* special_function */
391 "R_V850_SDA_16_16_SPLIT_OFFSET",/* name */
392 false, /* partial_inplace */
393 0xfffe0020, /* src_mask */
394 0xfffe0020, /* dst_mask */
395 false), /* pcrel_offset */
396
397 /* 16 bit offset from the zero data area pointer. */
398 HOWTO (R_V850_ZDA_16_16_SPLIT_OFFSET, /* type */
399 0, /* rightshift */
400 2, /* size (0 = byte, 1 = short, 2 = long) */
401 16, /* bitsize */
402 false, /* pc_relative */
403 0, /* bitpos */
404 complain_overflow_dont, /* complain_on_overflow */
405 v850_elf_reloc, /* special_function */
406 "R_V850_ZDA_16_16_SPLIT_OFFSET",/* name */
407 false, /* partial_inplace */
408 0xfffe0020, /* src_mask */
409 0xfffe0020, /* dst_mask */
410 false), /* pcrel_offset */
411
412 /* 6 bit offset from the call table base pointer. */
413 HOWTO (R_V850_CALLT_6_7_OFFSET, /* type */
414 0, /* rightshift */
415 1, /* size (0 = byte, 1 = short, 2 = long) */
416 7, /* bitsize */
417 false, /* pc_relative */
418 0, /* bitpos */
419 complain_overflow_dont, /* complain_on_overflow */
420 v850_elf_reloc, /* special_function */
421 "R_V850_CALLT_6_7_OFFSET", /* name */
422 false, /* partial_inplace */
423 0x3f, /* src_mask */
424 0x3f, /* dst_mask */
425 false), /* pcrel_offset */
426
427 /* 16 bit offset from the call table base pointer. */
428 HOWTO (R_V850_CALLT_16_16_OFFSET, /* type */
429 0, /* rightshift */
430 1, /* size (0 = byte, 1 = short, 2 = long) */
431 16, /* bitsize */
432 false, /* pc_relative */
433 0, /* bitpos */
434 complain_overflow_dont, /* complain_on_overflow */
435 v850_elf_reloc, /* special_function */
436 "R_V850_CALLT_16_16_OFFSET", /* name */
437 false, /* partial_inplace */
438 0xffff, /* src_mask */
439 0xffff, /* dst_mask */
440 false), /* pcrel_offset */
441
442 /* GNU extension to record C++ vtable hierarchy */
443 HOWTO (R_V850_GNU_VTINHERIT, /* type */
444 0, /* rightshift */
445 2, /* size (0 = byte, 1 = short, 2 = long) */
446 0, /* bitsize */
447 false, /* pc_relative */
448 0, /* bitpos */
449 complain_overflow_dont, /* complain_on_overflow */
450 NULL, /* special_function */
451 "R_V850_GNU_VTINHERIT", /* name */
452 false, /* partial_inplace */
453 0, /* src_mask */
454 0, /* dst_mask */
455 false), /* pcrel_offset */
456
457 /* GNU extension to record C++ vtable member usage */
458 HOWTO (R_V850_GNU_VTENTRY, /* type */
459 0, /* rightshift */
460 2, /* size (0 = byte, 1 = short, 2 = long) */
461 0, /* bitsize */
462 false, /* pc_relative */
463 0, /* bitpos */
464 complain_overflow_dont, /* complain_on_overflow */
465 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
466 "R_V850_GNU_VTENTRY", /* name */
467 false, /* partial_inplace */
468 0, /* src_mask */
469 0, /* dst_mask */
470 false), /* pcrel_offset */
435b1e90 471
252b5132
RH
472};
473
474/* Map BFD reloc types to V850 ELF reloc types. */
475
476struct v850_elf_reloc_map
477{
478 /* BFD_RELOC_V850_CALLT_16_16_OFFSET is 258, which will not fix in an
479 unsigned char. */
480 bfd_reloc_code_real_type bfd_reloc_val;
917583ad 481 unsigned int elf_reloc_val;
252b5132
RH
482};
483
484static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
485{
486 { BFD_RELOC_NONE, R_V850_NONE },
487 { BFD_RELOC_V850_9_PCREL, R_V850_9_PCREL },
488 { BFD_RELOC_V850_22_PCREL, R_V850_22_PCREL },
489 { BFD_RELOC_HI16_S, R_V850_HI16_S },
490 { BFD_RELOC_HI16, R_V850_HI16 },
491 { BFD_RELOC_LO16, R_V850_LO16 },
492 { BFD_RELOC_32, R_V850_32 },
493 { BFD_RELOC_16, R_V850_16 },
494 { BFD_RELOC_8, R_V850_8 },
495 { BFD_RELOC_V850_SDA_16_16_OFFSET, R_V850_SDA_16_16_OFFSET },
496 { BFD_RELOC_V850_SDA_15_16_OFFSET, R_V850_SDA_15_16_OFFSET },
497 { BFD_RELOC_V850_ZDA_16_16_OFFSET, R_V850_ZDA_16_16_OFFSET },
498 { BFD_RELOC_V850_ZDA_15_16_OFFSET, R_V850_ZDA_15_16_OFFSET },
499 { BFD_RELOC_V850_TDA_6_8_OFFSET, R_V850_TDA_6_8_OFFSET },
500 { BFD_RELOC_V850_TDA_7_8_OFFSET, R_V850_TDA_7_8_OFFSET },
501 { BFD_RELOC_V850_TDA_7_7_OFFSET, R_V850_TDA_7_7_OFFSET },
502 { BFD_RELOC_V850_TDA_16_16_OFFSET, R_V850_TDA_16_16_OFFSET },
503 { BFD_RELOC_V850_TDA_4_5_OFFSET, R_V850_TDA_4_5_OFFSET },
504 { BFD_RELOC_V850_TDA_4_4_OFFSET, R_V850_TDA_4_4_OFFSET },
505 { BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET, R_V850_SDA_16_16_SPLIT_OFFSET },
506 { BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET, R_V850_ZDA_16_16_SPLIT_OFFSET },
507 { BFD_RELOC_V850_CALLT_6_7_OFFSET, R_V850_CALLT_6_7_OFFSET },
508 { BFD_RELOC_V850_CALLT_16_16_OFFSET, R_V850_CALLT_16_16_OFFSET },
509 { BFD_RELOC_VTABLE_INHERIT, R_V850_GNU_VTINHERIT },
510 { BFD_RELOC_VTABLE_ENTRY, R_V850_GNU_VTENTRY },
511
512};
252b5132 513\f
e12dd2ea
NC
514/* Map a bfd relocation into the appropriate howto structure. */
515
252b5132
RH
516static reloc_howto_type *
517v850_elf_reloc_type_lookup (abfd, code)
5f771d47 518 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
519 bfd_reloc_code_real_type code;
520{
521 unsigned int i;
522
e12dd2ea
NC
523 for (i = ARRAY_SIZE (v850_elf_reloc_map); i --;)
524 if (v850_elf_reloc_map[i].bfd_reloc_val == code)
525 {
917583ad 526 unsigned int elf_reloc_val = v850_elf_reloc_map[i].elf_reloc_val;
fbad9a25
NC
527
528 BFD_ASSERT (v850_elf_howto_table[elf_reloc_val].type == elf_reloc_val);
435b1e90 529
fbad9a25 530 return v850_elf_howto_table + elf_reloc_val;
e12dd2ea 531 }
252b5132
RH
532
533 return NULL;
534}
252b5132
RH
535\f
536/* Set the howto pointer for an V850 ELF reloc. */
e12dd2ea 537
252b5132
RH
538static void
539v850_elf_info_to_howto_rel (abfd, cache_ptr, dst)
5f771d47 540 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
541 arelent * cache_ptr;
542 Elf32_Internal_Rel * dst;
543{
544 unsigned int r_type;
545
546 r_type = ELF32_R_TYPE (dst->r_info);
547 BFD_ASSERT (r_type < (unsigned int) R_V850_max);
548 cache_ptr->howto = &v850_elf_howto_table[r_type];
549}
550
435b1e90 551/* Set the howto pointer for a V850 ELF reloc (type RELA). */
252b5132
RH
552static void
553v850_elf_info_to_howto_rela (abfd, cache_ptr, dst)
5f771d47 554 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
555 arelent * cache_ptr;
556 Elf32_Internal_Rela *dst;
557{
558 unsigned int r_type;
559
560 r_type = ELF32_R_TYPE (dst->r_info);
561 BFD_ASSERT (r_type < (unsigned int) R_V850_max);
562 cache_ptr->howto = &v850_elf_howto_table[r_type];
563}
252b5132
RH
564\f
565/* Look through the relocs for a section during the first phase, and
566 allocate space in the global offset table or procedure linkage
567 table. */
568
569static boolean
570v850_elf_check_relocs (abfd, info, sec, relocs)
571 bfd * abfd;
572 struct bfd_link_info * info;
573 asection * sec;
574 const Elf_Internal_Rela * relocs;
575{
576 boolean ret = true;
577 bfd *dynobj;
578 Elf_Internal_Shdr *symtab_hdr;
579 struct elf_link_hash_entry **sym_hashes;
580 const Elf_Internal_Rela *rel;
581 const Elf_Internal_Rela *rel_end;
582 asection *sreloc;
583 enum v850_reloc_type r_type;
584 int other = 0;
585 const char *common = (const char *)0;
586
587 if (info->relocateable)
588 return true;
589
590#ifdef DEBUG
591 fprintf (stderr, "v850_elf_check_relocs called for section %s in %s\n",
592 bfd_get_section_name (abfd, sec),
593 bfd_get_filename (abfd));
594#endif
595
596 dynobj = elf_hash_table (info)->dynobj;
597 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
598 sym_hashes = elf_sym_hashes (abfd);
599 sreloc = NULL;
600
601 rel_end = relocs + sec->reloc_count;
602 for (rel = relocs; rel < rel_end; rel++)
603 {
604 unsigned long r_symndx;
605 struct elf_link_hash_entry *h;
606
607 r_symndx = ELF32_R_SYM (rel->r_info);
608 if (r_symndx < symtab_hdr->sh_info)
609 h = NULL;
610 else
611 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
612
613 r_type = (enum v850_reloc_type) ELF32_R_TYPE (rel->r_info);
614 switch (r_type)
615 {
616 default:
617 case R_V850_NONE:
618 case R_V850_9_PCREL:
619 case R_V850_22_PCREL:
620 case R_V850_HI16_S:
621 case R_V850_HI16:
622 case R_V850_LO16:
623 case R_V850_32:
624 case R_V850_16:
625 case R_V850_8:
626 case R_V850_CALLT_6_7_OFFSET:
627 case R_V850_CALLT_16_16_OFFSET:
628 break;
629
630 /* This relocation describes the C++ object vtable hierarchy.
631 Reconstruct it for later use during GC. */
632 case R_V850_GNU_VTINHERIT:
633 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
634 return false;
635 break;
636
e12dd2ea
NC
637 /* This relocation describes which C++ vtable entries
638 are actually used. Record for later use during GC. */
252b5132
RH
639 case R_V850_GNU_VTENTRY:
640 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
641 return false;
642 break;
643
644 case R_V850_SDA_16_16_SPLIT_OFFSET:
645 case R_V850_SDA_16_16_OFFSET:
646 case R_V850_SDA_15_16_OFFSET:
647 other = V850_OTHER_SDA;
648 common = ".scommon";
649 goto small_data_common;
435b1e90 650
252b5132
RH
651 case R_V850_ZDA_16_16_SPLIT_OFFSET:
652 case R_V850_ZDA_16_16_OFFSET:
653 case R_V850_ZDA_15_16_OFFSET:
654 other = V850_OTHER_ZDA;
655 common = ".zcommon";
656 goto small_data_common;
435b1e90 657
252b5132
RH
658 case R_V850_TDA_4_5_OFFSET:
659 case R_V850_TDA_4_4_OFFSET:
660 case R_V850_TDA_6_8_OFFSET:
661 case R_V850_TDA_7_8_OFFSET:
662 case R_V850_TDA_7_7_OFFSET:
663 case R_V850_TDA_16_16_OFFSET:
664 other = V850_OTHER_TDA;
665 common = ".tcommon";
666 /* fall through */
667
668#define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA)
669
670 small_data_common:
671 if (h)
672 {
e12dd2ea
NC
673 /* Flag which type of relocation was used. */
674 h->other |= other;
252b5132
RH
675 if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK)
676 && (h->other & V850_OTHER_ERROR) == 0)
677 {
678 const char * msg;
679 static char buff[200]; /* XXX */
680
681 switch (h->other & V850_OTHER_MASK)
682 {
683 default:
684 msg = _("Variable `%s' cannot occupy in multiple small data regions");
685 break;
686 case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA:
687 msg = _("Variable `%s' can only be in one of the small, zero, and tiny data regions");
688 break;
689 case V850_OTHER_SDA | V850_OTHER_ZDA:
690 msg = _("Variable `%s' cannot be in both small and zero data regions simultaneously");
691 break;
692 case V850_OTHER_SDA | V850_OTHER_TDA:
693 msg = _("Variable `%s' cannot be in both small and tiny data regions simultaneously");
694 break;
695 case V850_OTHER_ZDA | V850_OTHER_TDA:
696 msg = _("Variable `%s' cannot be in both zero and tiny data regions simultaneously");
697 break;
698 }
699
700 sprintf (buff, msg, h->root.root.string);
701 info->callbacks->warning (info, buff, h->root.root.string,
702 abfd, h->root.u.def.section, 0);
703
704 bfd_set_error (bfd_error_bad_value);
705 h->other |= V850_OTHER_ERROR;
706 ret = false;
707 }
708 }
709
710 if (h && h->root.type == bfd_link_hash_common
711 && h->root.u.c.p
712 && !strcmp (bfd_get_section_name (abfd, h->root.u.c.p->section), "COMMON"))
713 {
e12dd2ea
NC
714 asection * section;
715
716 section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common);
252b5132
RH
717 section->flags |= SEC_IS_COMMON;
718 }
719
720#ifdef DEBUG
721 fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n",
722 v850_elf_howto_table[ (int)r_type ].name,
723 (h && h->root.root.string) ? h->root.root.string : "<unknown>",
724 (h->root.type == bfd_link_hash_common) ? ", symbol is common" : "");
725#endif
726 break;
727 }
728 }
729
730 return ret;
731}
732
e12dd2ea
NC
733/* In the old version, when an entry was checked out from the table,
734 it was deleted. This produced an error if the entry was needed
735 more than once, as the second attempted retry failed.
736
737 In the current version, the entry is not deleted, instead we set
738 the field 'found' to true. If a second lookup matches the same
739 entry, then we know that the hi16s reloc has already been updated
740 and does not need to be updated a second time.
741
742 TODO - TOFIX: If it is possible that we need to restore 2 different
743 addresses from the same table entry, where the first generates an
744 overflow, whilst the second do not, then this code will fail. */
252b5132
RH
745
746typedef struct hi16s_location
747{
748 bfd_vma addend;
749 bfd_byte * address;
750 unsigned long counter;
751 boolean found;
752 struct hi16s_location * next;
753}
754hi16s_location;
755
756static hi16s_location * previous_hi16s;
757static hi16s_location * free_hi16s;
758static unsigned long hi16s_counter;
759
760static void
761remember_hi16s_reloc (abfd, addend, address)
762 bfd * abfd;
763 bfd_vma addend;
764 bfd_byte * address;
765{
766 hi16s_location * entry = NULL;
435b1e90 767
252b5132
RH
768 /* Find a free structure. */
769 if (free_hi16s == NULL)
770 free_hi16s = (hi16s_location *) bfd_zalloc (abfd, sizeof (* free_hi16s));
771
772 entry = free_hi16s;
773 free_hi16s = free_hi16s->next;
435b1e90 774
252b5132
RH
775 entry->addend = addend;
776 entry->address = address;
777 entry->counter = hi16s_counter ++;
778 entry->found = false;
779 entry->next = previous_hi16s;
780 previous_hi16s = entry;
435b1e90 781
252b5132
RH
782 /* Cope with wrap around of our counter. */
783 if (hi16s_counter == 0)
784 {
785 /* XXX - Assume that all counter entries differ only in their low 16 bits. */
786 for (entry = previous_hi16s; entry != NULL; entry = entry->next)
787 entry->counter &= 0xffff;
788
789 hi16s_counter = 0x10000;
790 }
435b1e90 791
252b5132
RH
792 return;
793}
794
795static bfd_byte *
796find_remembered_hi16s_reloc (addend, already_found)
797 bfd_vma addend;
798 boolean * already_found;
799{
800 hi16s_location * match = NULL;
801 hi16s_location * entry;
802 hi16s_location * previous = NULL;
803 hi16s_location * prev;
252b5132 804 bfd_byte * addr;
435b1e90 805
252b5132
RH
806 /* Search the table. Record the most recent entry that matches. */
807 for (entry = previous_hi16s; entry; entry = entry->next)
808 {
809 if (entry->addend == addend
810 && (match == NULL || match->counter < entry->counter))
811 {
812 previous = prev;
813 match = entry;
814 }
435b1e90 815
252b5132
RH
816 prev = entry;
817 }
818
819 if (match == NULL)
820 return NULL;
821
822 /* Extract the address. */
823 addr = match->address;
824
825 /* Remeber if this entry has already been used before. */
826 if (already_found)
827 * already_found = match->found;
828
829 /* Note that this entry has now been used. */
830 match->found = true;
435b1e90 831
252b5132 832 return addr;
435b1e90 833}
252b5132
RH
834
835/* FIXME: The code here probably ought to be removed and the code in reloc.c
836 allowed to do its stuff instead. At least for most of the relocs, anwyay. */
e12dd2ea 837
252b5132
RH
838static bfd_reloc_status_type
839v850_elf_perform_relocation (abfd, r_type, addend, address)
840 bfd * abfd;
841 int r_type;
842 bfd_vma addend;
843 bfd_byte * address;
844{
845 unsigned long insn;
846 bfd_signed_vma saddend = (bfd_signed_vma) addend;
435b1e90 847
252b5132
RH
848 switch (r_type)
849 {
850 default:
851 /* fprintf (stderr, "reloc type %d not SUPPORTED\n", r_type ); */
852 return bfd_reloc_notsupported;
435b1e90 853
252b5132
RH
854 case R_V850_32:
855 bfd_put_32 (abfd, addend, address);
856 return bfd_reloc_ok;
435b1e90 857
252b5132
RH
858 case R_V850_22_PCREL:
859 if (saddend > 0x1fffff || saddend < -0x200000)
860 return bfd_reloc_overflow;
435b1e90 861
252b5132
RH
862 if ((addend % 2) != 0)
863 return bfd_reloc_dangerous;
435b1e90 864
252b5132
RH
865 insn = bfd_get_32 (abfd, address);
866 insn &= ~0xfffe003f;
867 insn |= (((addend & 0xfffe) << 16) | ((addend & 0x3f0000) >> 16));
868 bfd_put_32 (abfd, insn, address);
869 return bfd_reloc_ok;
435b1e90 870
252b5132
RH
871 case R_V850_9_PCREL:
872 if (saddend > 0xff || saddend < -0x100)
873 return bfd_reloc_overflow;
435b1e90 874
252b5132
RH
875 if ((addend % 2) != 0)
876 return bfd_reloc_dangerous;
435b1e90 877
252b5132
RH
878 insn = bfd_get_16 (abfd, address);
879 insn &= ~ 0xf870;
880 insn |= ((addend & 0x1f0) << 7) | ((addend & 0x0e) << 3);
881 break;
435b1e90 882
252b5132
RH
883 case R_V850_HI16:
884 addend += (bfd_get_16 (abfd, address) << 16);
885 addend = (addend >> 16);
886 insn = addend;
887 break;
435b1e90 888
252b5132
RH
889 case R_V850_HI16_S:
890 /* Remember where this relocation took place. */
891 remember_hi16s_reloc (abfd, addend, address);
892
893 addend += (bfd_get_16 (abfd, address) << 16);
894 addend = (addend >> 16) + ((addend & 0x8000) != 0);
435b1e90
KH
895
896 /* This relocation cannot overflow. */
252b5132
RH
897 if (addend > 0x7fff)
898 addend = 0;
435b1e90 899
252b5132
RH
900 insn = addend;
901 break;
435b1e90 902
252b5132
RH
903 case R_V850_LO16:
904 /* Calculate the sum of the value stored in the instruction and the
905 addend and check for overflow from the low 16 bits into the high
906 16 bits. The assembler has already done some of this: If the
907 value stored in the instruction has its 15th bit set, (counting
908 from zero) then the assembler will have added 1 to the value
909 stored in the associated HI16S reloc. So for example, these
910 relocations:
911
912 movhi hi( fred ), r0, r1
913 movea lo( fred ), r1, r1
914
915 will store 0 in the value fields for the MOVHI and MOVEA instructions
916 and addend will be the address of fred, but for these instructions:
917
918 movhi hi( fred + 0x123456), r0, r1
919 movea lo( fred + 0x123456), r1, r1
920
921 the value stored in the MOVHI instruction will be 0x12 and the value
922 stored in the MOVEA instruction will be 0x3456. If however the
923 instructions were:
924
925 movhi hi( fred + 0x10ffff), r0, r1
926 movea lo( fred + 0x10ffff), r1, r1
927
928 then the value stored in the MOVHI instruction would be 0x11 (not
929 0x10) and the value stored in the MOVEA instruction would be 0xffff.
930 Thus (assuming for the moment that the addend is 0), at run time the
931 MOVHI instruction loads 0x110000 into r1, then the MOVEA instruction
932 adds 0xffffffff (sign extension!) producing 0x10ffff. Similarly if
933 the instructions were:
934
935 movhi hi( fred - 1), r0, r1
936 movea lo( fred - 1), r1, r1
937
938 then 0 is stored in the MOVHI instruction and -1 is stored in the
939 MOVEA instruction.
940
941 Overflow can occur if the addition of the value stored in the
942 instruction plus the addend sets the 15th bit when before it was clear.
943 This is because the 15th bit will be sign extended into the high part,
944 thus reducing its value by one, but since the 15th bit was originally
945 clear, the assembler will not have added 1 to the previous HI16S reloc
946 to compensate for this effect. For example:
947
948 movhi hi( fred + 0x123456), r0, r1
949 movea lo( fred + 0x123456), r1, r1
950
951 The value stored in HI16S reloc is 0x12, the value stored in the LO16
952 reloc is 0x3456. If we assume that the address of fred is 0x00007000
953 then the relocations become:
954
955 HI16S: 0x0012 + (0x00007000 >> 16) = 0x12
956 LO16: 0x3456 + (0x00007000 & 0xffff) = 0xa456
957
958 but when the instructions are executed, the MOVEA instruction's value
959 is signed extended, so the sum becomes:
960
961 0x00120000
962 + 0xffffa456
963 ------------
964 0x0011a456 but 'fred + 0x123456' = 0x0012a456
965
966 Note that if the 15th bit was set in the value stored in the LO16
967 reloc, then we do not have to do anything:
968
969 movhi hi( fred + 0x10ffff), r0, r1
970 movea lo( fred + 0x10ffff), r1, r1
971
972 HI16S: 0x0011 + (0x00007000 >> 16) = 0x11
973 LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff
974
975 0x00110000
976 + 0x00006fff
977 ------------
978 0x00116fff = fred + 0x10ffff = 0x7000 + 0x10ffff
979
252b5132
RH
980 Overflow can also occur if the computation carries into the 16th bit
981 and it also results in the 15th bit having the same value as the 15th
982 bit of the original value. What happens is that the HI16S reloc
983 will have already examined the 15th bit of the original value and
984 added 1 to the high part if the bit is set. This compensates for the
985 sign extension of 15th bit of the result of the computation. But now
986 there is a carry into the 16th bit, and this has not been allowed for.
987
988 So, for example if fred is at address 0xf000:
989
990 movhi hi( fred + 0xffff), r0, r1 [bit 15 of the offset is set]
991 movea lo( fred + 0xffff), r1, r1
992
993 HI16S: 0x0001 + (0x0000f000 >> 16) = 0x0001
994 LO16: 0xffff + (0x0000f000 & 0xffff) = 0xefff (carry into bit 16 is lost)
995
996 0x00010000
997 + 0xffffefff
998 ------------
999 0x0000efff but 'fred + 0xffff' = 0x0001efff
1000
1001 Similarly, if the 15th bit remains clear, but overflow occurs into
1002 the 16th bit then (assuming the address of fred is 0xf000):
1003
1004 movhi hi( fred + 0x7000), r0, r1 [bit 15 of the offset is clear]
1005 movea lo( fred + 0x7000), r1, r1
1006
1007 HI16S: 0x0000 + (0x0000f000 >> 16) = 0x0000
1008 LO16: 0x7000 + (0x0000f000 & 0xffff) = 0x6fff (carry into bit 16 is lost)
1009
1010 0x00000000
1011 + 0x00006fff
1012 ------------
1013 0x00006fff but 'fred + 0x7000' = 0x00016fff
435b1e90 1014
252b5132
RH
1015 Note - there is no need to change anything if a carry occurs, and the
1016 15th bit changes its value from being set to being clear, as the HI16S
1017 reloc will have already added in 1 to the high part for us:
1018
1019 movhi hi( fred + 0xffff), r0, r1 [bit 15 of the offset is set]
1020 movea lo( fred + 0xffff), r1, r1
1021
1022 HI16S: 0x0001 + (0x00007000 >> 16)
1023 LO16: 0xffff + (0x00007000 & 0xffff) = 0x6fff (carry into bit 16 is lost)
1024
1025 0x00010000
1026 + 0x00006fff (bit 15 not set, so the top half is zero)
1027 ------------
1028 0x00016fff which is right (assuming that fred is at 0x7000)
1029
1030 but if the 15th bit goes from being clear to being set, then we must
1031 once again handle overflow:
1032
1033 movhi hi( fred + 0x7000), r0, r1 [bit 15 of the offset is clear]
1034 movea lo( fred + 0x7000), r1, r1
1035
1036 HI16S: 0x0000 + (0x0000ffff >> 16)
1037 LO16: 0x7000 + (0x0000ffff & 0xffff) = 0x6fff (carry into bit 16)
1038
1039 0x00000000
1040 + 0x00006fff (bit 15 not set, so the top half is zero)
1041 ------------
e12dd2ea 1042 0x00006fff which is wrong (assuming that fred is at 0xffff). */
252b5132
RH
1043 {
1044 long result;
1045
1046 insn = bfd_get_16 (abfd, address);
1047 result = insn + addend;
1048
1049#define BIT15_SET(x) ((x) & 0x8000)
1050#define OVERFLOWS(a,i) ((((a) & 0xffff) + (i)) > 0xffff)
435b1e90 1051
252b5132
RH
1052 if ((BIT15_SET (result) && ! BIT15_SET (addend))
1053 || (OVERFLOWS (addend, insn)
1054 && ((! BIT15_SET (insn)) || (BIT15_SET (addend)))))
1055 {
1056 boolean already_updated;
1057 bfd_byte * hi16s_address = find_remembered_hi16s_reloc
1058 (addend, & already_updated);
435b1e90 1059
252b5132
RH
1060 /* Amend the matching HI16_S relocation. */
1061 if (hi16s_address != NULL)
1062 {
1063 if (! already_updated)
1064 {
1065 insn = bfd_get_16 (abfd, hi16s_address);
1066 insn += 1;
1067 bfd_put_16 (abfd, insn, hi16s_address);
1068 }
1069 }
1070 else
1071 {
1072 fprintf (stderr, _("FAILED to find previous HI16 reloc\n"));
1073 return bfd_reloc_overflow;
1074 }
1075 }
435b1e90 1076
252b5132
RH
1077 /* Do not complain if value has top bit set, as this has been anticipated. */
1078 insn = result & 0xffff;
1079 break;
1080 }
1081
1082 case R_V850_8:
1083 addend += (char) bfd_get_8 (abfd, address);
1084
1085 saddend = (bfd_signed_vma) addend;
435b1e90 1086
252b5132
RH
1087 if (saddend > 0x7f || saddend < -0x80)
1088 return bfd_reloc_overflow;
1089
1090 bfd_put_8 (abfd, addend, address);
1091 return bfd_reloc_ok;
1092
1093 case R_V850_CALLT_16_16_OFFSET:
1094 addend += bfd_get_16 (abfd, address);
435b1e90 1095
252b5132 1096 saddend = (bfd_signed_vma) addend;
435b1e90 1097
252b5132
RH
1098 if (saddend > 0xffff || saddend < 0)
1099 return bfd_reloc_overflow;
1100
1101 insn = addend;
1102 break;
435b1e90 1103
252b5132
RH
1104 case R_V850_16:
1105
435b1e90 1106 /* drop through */
252b5132
RH
1107 case R_V850_SDA_16_16_OFFSET:
1108 case R_V850_ZDA_16_16_OFFSET:
1109 case R_V850_TDA_16_16_OFFSET:
1110 addend += bfd_get_16 (abfd, address);
435b1e90 1111
252b5132 1112 saddend = (bfd_signed_vma) addend;
435b1e90 1113
252b5132
RH
1114 if (saddend > 0x7fff || saddend < -0x8000)
1115 return bfd_reloc_overflow;
1116
1117 insn = addend;
1118 break;
435b1e90 1119
252b5132
RH
1120 case R_V850_SDA_15_16_OFFSET:
1121 case R_V850_ZDA_15_16_OFFSET:
1122 insn = bfd_get_16 (abfd, address);
1123 addend += (insn & 0xfffe);
435b1e90 1124
252b5132 1125 saddend = (bfd_signed_vma) addend;
435b1e90 1126
252b5132
RH
1127 if (saddend > 0x7ffe || saddend < -0x8000)
1128 return bfd_reloc_overflow;
435b1e90 1129
252b5132
RH
1130 if (addend & 1)
1131 return bfd_reloc_dangerous;
435b1e90 1132
252b5132
RH
1133 insn = (addend & ~1) | (insn & 1);
1134 break;
435b1e90 1135
252b5132
RH
1136 case R_V850_TDA_6_8_OFFSET:
1137 insn = bfd_get_16 (abfd, address);
1138 addend += ((insn & 0x7e) << 1);
435b1e90 1139
252b5132 1140 saddend = (bfd_signed_vma) addend;
435b1e90 1141
252b5132
RH
1142 if (saddend > 0xfc || saddend < 0)
1143 return bfd_reloc_overflow;
435b1e90 1144
252b5132
RH
1145 if (addend & 3)
1146 return bfd_reloc_dangerous;
435b1e90 1147
252b5132
RH
1148 insn &= 0xff81;
1149 insn |= (addend >> 1);
1150 break;
435b1e90 1151
252b5132
RH
1152 case R_V850_TDA_7_8_OFFSET:
1153 insn = bfd_get_16 (abfd, address);
1154 addend += ((insn & 0x7f) << 1);
435b1e90 1155
252b5132 1156 saddend = (bfd_signed_vma) addend;
435b1e90 1157
252b5132
RH
1158 if (saddend > 0xfe || saddend < 0)
1159 return bfd_reloc_overflow;
435b1e90 1160
252b5132
RH
1161 if (addend & 1)
1162 return bfd_reloc_dangerous;
435b1e90 1163
252b5132
RH
1164 insn &= 0xff80;
1165 insn |= (addend >> 1);
1166 break;
435b1e90 1167
252b5132
RH
1168 case R_V850_TDA_7_7_OFFSET:
1169 insn = bfd_get_16 (abfd, address);
1170 addend += insn & 0x7f;
435b1e90 1171
252b5132 1172 saddend = (bfd_signed_vma) addend;
435b1e90 1173
252b5132
RH
1174 if (saddend > 0x7f || saddend < 0)
1175 return bfd_reloc_overflow;
435b1e90 1176
252b5132
RH
1177 insn &= 0xff80;
1178 insn |= addend;
1179 break;
435b1e90 1180
252b5132
RH
1181 case R_V850_TDA_4_5_OFFSET:
1182 insn = bfd_get_16 (abfd, address);
1183 addend += ((insn & 0xf) << 1);
435b1e90 1184
252b5132 1185 saddend = (bfd_signed_vma) addend;
435b1e90 1186
252b5132
RH
1187 if (saddend > 0x1e || saddend < 0)
1188 return bfd_reloc_overflow;
435b1e90 1189
252b5132
RH
1190 if (addend & 1)
1191 return bfd_reloc_dangerous;
435b1e90 1192
252b5132
RH
1193 insn &= 0xfff0;
1194 insn |= (addend >> 1);
1195 break;
435b1e90 1196
252b5132
RH
1197 case R_V850_TDA_4_4_OFFSET:
1198 insn = bfd_get_16 (abfd, address);
1199 addend += insn & 0xf;
435b1e90 1200
252b5132 1201 saddend = (bfd_signed_vma) addend;
435b1e90 1202
252b5132
RH
1203 if (saddend > 0xf || saddend < 0)
1204 return bfd_reloc_overflow;
435b1e90 1205
252b5132
RH
1206 insn &= 0xfff0;
1207 insn |= addend;
1208 break;
435b1e90 1209
252b5132
RH
1210 case R_V850_ZDA_16_16_SPLIT_OFFSET:
1211 case R_V850_SDA_16_16_SPLIT_OFFSET:
1212 insn = bfd_get_32 (abfd, address);
1213 addend += ((insn & 0xfffe0000) >> 16) + ((insn & 0x20) >> 5);
435b1e90 1214
252b5132 1215 saddend = (bfd_signed_vma) addend;
435b1e90 1216
252b5132
RH
1217 if (saddend > 0x7fff || saddend < -0x8000)
1218 return bfd_reloc_overflow;
435b1e90 1219
252b5132
RH
1220 insn &= 0x0001ffdf;
1221 insn |= (addend & 1) << 5;
1222 insn |= (addend & ~1) << 16;
435b1e90 1223
252b5132
RH
1224 bfd_put_32 (abfd, insn, address);
1225 return bfd_reloc_ok;
435b1e90 1226
252b5132
RH
1227 case R_V850_CALLT_6_7_OFFSET:
1228 insn = bfd_get_16 (abfd, address);
1229 addend += ((insn & 0x3f) << 1);
435b1e90 1230
252b5132 1231 saddend = (bfd_signed_vma) addend;
435b1e90 1232
252b5132
RH
1233 if (saddend > 0x7e || saddend < 0)
1234 return bfd_reloc_overflow;
435b1e90 1235
252b5132
RH
1236 if (addend & 1)
1237 return bfd_reloc_dangerous;
435b1e90 1238
252b5132
RH
1239 insn &= 0xff80;
1240 insn |= (addend >> 1);
1241 break;
1242
1243 case R_V850_GNU_VTINHERIT:
1244 case R_V850_GNU_VTENTRY:
1245 return bfd_reloc_ok;
1246
1247 }
1248
1249 bfd_put_16 (abfd, insn, address);
1250 return bfd_reloc_ok;
1251}
252b5132
RH
1252\f
1253/* Insert the addend into the instruction. */
e12dd2ea 1254
252b5132
RH
1255static bfd_reloc_status_type
1256v850_elf_reloc (abfd, reloc, symbol, data, isection, obfd, err)
5f771d47 1257 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
1258 arelent * reloc;
1259 asymbol * symbol;
5f771d47 1260 PTR data ATTRIBUTE_UNUSED;
252b5132
RH
1261 asection * isection;
1262 bfd * obfd;
5f771d47 1263 char ** err ATTRIBUTE_UNUSED;
252b5132
RH
1264{
1265 long relocation;
435b1e90 1266
252b5132
RH
1267 /* If there is an output BFD,
1268 and the symbol is not a section name (which is only defined at final link time),
1269 and either we are not putting the addend into the instruction
e12dd2ea 1270 or the addend is zero, so there is nothing to add into the instruction
252b5132
RH
1271 then just fixup the address and return. */
1272 if (obfd != (bfd *) NULL
1273 && (symbol->flags & BSF_SECTION_SYM) == 0
1274 && (! reloc->howto->partial_inplace
1275 || reloc->addend == 0))
1276 {
1277 reloc->address += isection->output_offset;
1278 return bfd_reloc_ok;
1279 }
435b1e90 1280#if 0
252b5132 1281 else if (obfd != NULL)
e12dd2ea 1282 return bfd_reloc_continue;
252b5132 1283#endif
435b1e90 1284
252b5132
RH
1285 /* Catch relocs involving undefined symbols. */
1286 if (bfd_is_und_section (symbol->section)
1287 && (symbol->flags & BSF_WEAK) == 0
1288 && obfd == NULL)
1289 return bfd_reloc_undefined;
1290
1291 /* We handle final linking of some relocs ourselves. */
1292
1293 /* Is the address of the relocation really within the section? */
1294 if (reloc->address > isection->_cooked_size)
1295 return bfd_reloc_outofrange;
435b1e90 1296
252b5132
RH
1297 /* Work out which section the relocation is targetted at and the
1298 initial relocation command value. */
435b1e90 1299
252b5132
RH
1300 /* Get symbol value. (Common symbols are special.) */
1301 if (bfd_is_com_section (symbol->section))
1302 relocation = 0;
1303 else
1304 relocation = symbol->value;
435b1e90 1305
252b5132
RH
1306 /* Convert input-section-relative symbol value to absolute + addend. */
1307 relocation += symbol->section->output_section->vma;
1308 relocation += symbol->section->output_offset;
1309 relocation += reloc->addend;
435b1e90 1310
a0f2e916
NC
1311#if 0 /* Since this reloc is going to be processed later on, we should
1312 not make it pc-relative here. To test this, try assembling and
1313 linking this program:
1314
1315 .text
1316 .globl _start
1317 nop
1318 _start:
1319 jr foo
1320
1321 .section ".foo","ax"
1322 nop
1323 foo:
e12dd2ea 1324 nop */
252b5132
RH
1325 if (reloc->howto->pc_relative == true)
1326 {
1327 /* Here the variable relocation holds the final address of the
1328 symbol we are relocating against, plus any addend. */
1329 relocation -= isection->output_section->vma + isection->output_offset;
435b1e90 1330
e12dd2ea 1331 /* Deal with pcrel_offset. */
252b5132
RH
1332 relocation -= reloc->address;
1333 }
a0f2e916 1334#endif
435b1e90 1335 reloc->addend = relocation;
252b5132
RH
1336 return bfd_reloc_ok;
1337}
252b5132 1338\f
252b5132
RH
1339static boolean
1340v850_elf_is_local_label_name (abfd, name)
5f771d47 1341 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
1342 const char * name;
1343{
1344 return ( (name[0] == '.' && (name[1] == 'L' || name[1] == '.'))
1345 || (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_'));
1346}
252b5132
RH
1347\f
1348/* Perform a relocation as part of a final link. */
e12dd2ea 1349
252b5132
RH
1350static bfd_reloc_status_type
1351v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1352 input_section, contents, offset, value,
1353 addend, info, sym_sec, is_local)
1354 reloc_howto_type * howto;
1355 bfd * input_bfd;
5f771d47 1356 bfd * output_bfd ATTRIBUTE_UNUSED;
252b5132
RH
1357 asection * input_section;
1358 bfd_byte * contents;
1359 bfd_vma offset;
1360 bfd_vma value;
1361 bfd_vma addend;
1362 struct bfd_link_info * info;
1363 asection * sym_sec;
5f771d47 1364 int is_local ATTRIBUTE_UNUSED;
252b5132
RH
1365{
1366 unsigned long r_type = howto->type;
1367 bfd_byte * hit_data = contents + offset;
1368
1369 /* Adjust the value according to the relocation. */
1370 switch (r_type)
1371 {
1372 case R_V850_9_PCREL:
1373 value -= (input_section->output_section->vma
1374 + input_section->output_offset);
1375 value -= offset;
1376 break;
435b1e90 1377
252b5132
RH
1378 case R_V850_22_PCREL:
1379 value -= (input_section->output_section->vma
1380 + input_section->output_offset
1381 + offset);
1382
232fb1a3
NC
1383 /* If the sign extension will corrupt the value then we have overflowed. */
1384 if (((value & 0xff000000) != 0x0) && ((value & 0xff000000) != 0xff000000))
1385 return bfd_reloc_overflow;
435b1e90 1386
e12dd2ea
NC
1387 /* Only the bottom 24 bits of the PC are valid */
1388 value = SEXT24 (value);
252b5132 1389 break;
435b1e90 1390
252b5132
RH
1391 case R_V850_HI16_S:
1392 case R_V850_HI16:
1393 case R_V850_LO16:
1394 case R_V850_16:
1395 case R_V850_32:
1396 case R_V850_8:
1397 break;
1398
435b1e90 1399 case R_V850_ZDA_15_16_OFFSET:
252b5132
RH
1400 case R_V850_ZDA_16_16_OFFSET:
1401 case R_V850_ZDA_16_16_SPLIT_OFFSET:
1402 if (sym_sec == NULL)
1403 return bfd_reloc_undefined;
435b1e90 1404
252b5132
RH
1405 value -= sym_sec->output_section->vma;
1406 break;
1407
1408 case R_V850_SDA_15_16_OFFSET:
1409 case R_V850_SDA_16_16_OFFSET:
1410 case R_V850_SDA_16_16_SPLIT_OFFSET:
1411 {
1412 unsigned long gp;
1413 struct bfd_link_hash_entry * h;
1414
1415 if (sym_sec == NULL)
1416 return bfd_reloc_undefined;
435b1e90 1417
252b5132
RH
1418 /* Get the value of __gp. */
1419 h = bfd_link_hash_lookup (info->hash, "__gp", false, false, true);
1420 if (h == (struct bfd_link_hash_entry *) NULL
1421 || h->type != bfd_link_hash_defined)
1422 return bfd_reloc_other;
1423
1424 gp = (h->u.def.value
1425 + h->u.def.section->output_section->vma
1426 + h->u.def.section->output_offset);
1427
1428 value -= sym_sec->output_section->vma;
1429 value -= (gp - sym_sec->output_section->vma);
1430 }
1431 break;
1432
1433 case R_V850_TDA_4_4_OFFSET:
1434 case R_V850_TDA_4_5_OFFSET:
1435 case R_V850_TDA_16_16_OFFSET:
1436 case R_V850_TDA_7_7_OFFSET:
1437 case R_V850_TDA_7_8_OFFSET:
1438 case R_V850_TDA_6_8_OFFSET:
1439 {
1440 unsigned long ep;
1441 struct bfd_link_hash_entry * h;
435b1e90 1442
252b5132
RH
1443 /* Get the value of __ep. */
1444 h = bfd_link_hash_lookup (info->hash, "__ep", false, false, true);
1445 if (h == (struct bfd_link_hash_entry *) NULL
1446 || h->type != bfd_link_hash_defined)
e12dd2ea
NC
1447 /* Actually this indicates that __ep could not be found. */
1448 return bfd_reloc_continue;
252b5132
RH
1449
1450 ep = (h->u.def.value
1451 + h->u.def.section->output_section->vma
1452 + h->u.def.section->output_offset);
1453
1454 value -= ep;
1455 }
1456 break;
435b1e90 1457
252b5132
RH
1458 case R_V850_CALLT_6_7_OFFSET:
1459 {
1460 unsigned long ctbp;
1461 struct bfd_link_hash_entry * h;
435b1e90 1462
252b5132
RH
1463 /* Get the value of __ctbp. */
1464 h = bfd_link_hash_lookup (info->hash, "__ctbp", false, false, true);
1465 if (h == (struct bfd_link_hash_entry *) NULL
1466 || h->type != bfd_link_hash_defined)
e12dd2ea
NC
1467 /* Actually this indicates that __ctbp could not be found. */
1468 return bfd_reloc_dangerous + 1;
252b5132
RH
1469
1470 ctbp = (h->u.def.value
1471 + h->u.def.section->output_section->vma
1472 + h->u.def.section->output_offset);
1473 value -= ctbp;
1474 }
1475 break;
435b1e90 1476
252b5132
RH
1477 case R_V850_CALLT_16_16_OFFSET:
1478 {
1479 unsigned long ctbp;
1480 struct bfd_link_hash_entry * h;
1481
1482 if (sym_sec == NULL)
1483 return bfd_reloc_undefined;
435b1e90 1484
252b5132
RH
1485 /* Get the value of __ctbp. */
1486 h = bfd_link_hash_lookup (info->hash, "__ctbp", false, false, true);
1487 if (h == (struct bfd_link_hash_entry *) NULL
1488 || h->type != bfd_link_hash_defined)
1489 return (bfd_reloc_dangerous + 1);
1490
1491 ctbp = (h->u.def.value
1492 + h->u.def.section->output_section->vma
1493 + h->u.def.section->output_offset);
1494
1495 value -= sym_sec->output_section->vma;
1496 value -= (ctbp - sym_sec->output_section->vma);
1497 }
1498 break;
435b1e90 1499
252b5132
RH
1500 case R_V850_NONE:
1501 case R_V850_GNU_VTINHERIT:
1502 case R_V850_GNU_VTENTRY:
1503 return bfd_reloc_ok;
1504
1505 default:
1506 return bfd_reloc_notsupported;
1507 }
1508
1509 /* Perform the relocation. */
435b1e90 1510 return v850_elf_perform_relocation (input_bfd, r_type, value + addend, hit_data);
252b5132 1511}
252b5132
RH
1512\f
1513/* Relocate an V850 ELF section. */
e12dd2ea 1514
252b5132
RH
1515static boolean
1516v850_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1517 contents, relocs, local_syms, local_sections)
1518 bfd * output_bfd;
1519 struct bfd_link_info * info;
1520 bfd * input_bfd;
1521 asection * input_section;
1522 bfd_byte * contents;
1523 Elf_Internal_Rela * relocs;
1524 Elf_Internal_Sym * local_syms;
1525 asection ** local_sections;
1526{
1527 Elf_Internal_Shdr * symtab_hdr;
1528 struct elf_link_hash_entry ** sym_hashes;
1529 Elf_Internal_Rela * rel;
1530 Elf_Internal_Rela * relend;
1531
1532 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1533 sym_hashes = elf_sym_hashes (input_bfd);
1534
1535 if (sym_hashes == NULL)
1536 {
1537 info->callbacks->warning
1538 (info, "no hash table available", NULL, input_bfd, input_section, 0);
1539
1540 return false;
1541 }
435b1e90 1542
252b5132
RH
1543 /* Reset the list of remembered HI16S relocs to empty. */
1544 free_hi16s = previous_hi16s;
1545 previous_hi16s = NULL;
1546 hi16s_counter = 0;
435b1e90 1547
252b5132
RH
1548 rel = relocs;
1549 relend = relocs + input_section->reloc_count;
1550 for (; rel < relend; rel++)
1551 {
1552 int r_type;
1553 reloc_howto_type * howto;
1554 unsigned long r_symndx;
1555 Elf_Internal_Sym * sym;
1556 asection * sec;
1557 struct elf_link_hash_entry * h;
1558 bfd_vma relocation;
1559 bfd_reloc_status_type r;
1560
1561 r_symndx = ELF32_R_SYM (rel->r_info);
1562 r_type = ELF32_R_TYPE (rel->r_info);
1563
1564 if (r_type == R_V850_GNU_VTENTRY
1565 || r_type == R_V850_GNU_VTINHERIT)
1566 continue;
1567
1568 howto = v850_elf_howto_table + r_type;
1569
1570 if (info->relocateable)
1571 {
1572 /* This is a relocateable link. We don't have to change
1573 anything, unless the reloc is against a section symbol,
1574 in which case we have to adjust according to where the
1575 section symbol winds up in the output section. */
1576 if (r_symndx < symtab_hdr->sh_info)
1577 {
1578 sym = local_syms + r_symndx;
1579 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1580 {
1581 sec = local_sections[r_symndx];
1582 rel->r_addend += sec->output_offset + sym->st_value;
1583 }
1584 }
1585
1586 continue;
1587 }
1588
1589 /* This is a final link. */
1590 h = NULL;
1591 sym = NULL;
1592 sec = NULL;
1593 if (r_symndx < symtab_hdr->sh_info)
1594 {
1595 sym = local_syms + r_symndx;
1596 sec = local_sections[r_symndx];
1597 relocation = (sec->output_section->vma
1598 + sec->output_offset
1599 + sym->st_value);
1600#if 0
1601 {
1602 char * name;
e12dd2ea 1603
252b5132
RH
1604 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, sym->st_name);
1605 name = (name == NULL) ? "<none>" : name;
e12dd2ea
NC
1606 fprintf (stderr, "local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
1607 sec->name, name, sym->st_name,
1608 sec->output_section->vma, sec->output_offset, sym->st_value, rel->r_addend);
252b5132
RH
1609 }
1610#endif
1611 }
1612 else
1613 {
1614 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
435b1e90 1615
252b5132
RH
1616 while (h->root.type == bfd_link_hash_indirect
1617 || h->root.type == bfd_link_hash_warning)
1618 h = (struct elf_link_hash_entry *) h->root.u.i.link;
435b1e90 1619
252b5132
RH
1620 if (h->root.type == bfd_link_hash_defined
1621 || h->root.type == bfd_link_hash_defweak)
1622 {
1623 sec = h->root.u.def.section;
1624 relocation = (h->root.u.def.value
1625 + sec->output_section->vma
1626 + sec->output_offset);
1627#if 0
e12dd2ea
NC
1628 fprintf (stderr, "defined: sec: %s, name: %s, value: %x + %x + %x gives: %x\n",
1629 sec->name, h->root.root.string, h->root.u.def.value, sec->output_section->vma, sec->output_offset, relocation);
252b5132
RH
1630#endif
1631 }
1632 else if (h->root.type == bfd_link_hash_undefweak)
1633 {
1634#if 0
e12dd2ea
NC
1635 fprintf (stderr, "undefined: sec: %s, name: %s\n",
1636 sec->name, h->root.root.string);
252b5132
RH
1637#endif
1638 relocation = 0;
1639 }
1640 else
1641 {
1642 if (! ((*info->callbacks->undefined_symbol)
1643 (info, h->root.root.string, input_bfd,
5cc7c785 1644 input_section, rel->r_offset, true)))
252b5132
RH
1645 return false;
1646#if 0
e12dd2ea 1647 fprintf (stderr, "unknown: name: %s\n", h->root.root.string);
252b5132
RH
1648#endif
1649 relocation = 0;
1650 }
1651 }
1652
e12dd2ea 1653 /* FIXME: We should use the addend, but the COFF relocations don't. */
252b5132
RH
1654 r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1655 input_section,
1656 contents, rel->r_offset,
1657 relocation, rel->r_addend,
1658 info, sec, h == NULL);
1659
1660 if (r != bfd_reloc_ok)
1661 {
1662 const char * name;
1663 const char * msg = (const char *)0;
1664
1665 if (h != NULL)
1666 name = h->root.root.string;
1667 else
1668 {
1669 name = (bfd_elf_string_from_elf_section
1670 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1671 if (name == NULL || *name == '\0')
1672 name = bfd_section_name (input_bfd, sec);
1673 }
1674
1675 switch (r)
1676 {
1677 case bfd_reloc_overflow:
1678 if (! ((*info->callbacks->reloc_overflow)
1679 (info, name, howto->name, (bfd_vma) 0,
1680 input_bfd, input_section, rel->r_offset)))
1681 return false;
1682 break;
1683
1684 case bfd_reloc_undefined:
1685 if (! ((*info->callbacks->undefined_symbol)
1686 (info, name, input_bfd, input_section,
5cc7c785 1687 rel->r_offset, true)))
252b5132
RH
1688 return false;
1689 break;
1690
1691 case bfd_reloc_outofrange:
1692 msg = _("internal error: out of range error");
1693 goto common_error;
1694
1695 case bfd_reloc_notsupported:
1696 msg = _("internal error: unsupported relocation error");
1697 goto common_error;
1698
1699 case bfd_reloc_dangerous:
1700 msg = _("internal error: dangerous relocation");
1701 goto common_error;
1702
1703 case bfd_reloc_other:
1704 msg = _("could not locate special linker symbol __gp");
1705 goto common_error;
1706
1707 case bfd_reloc_continue:
1708 msg = _("could not locate special linker symbol __ep");
1709 goto common_error;
1710
1711 case (bfd_reloc_dangerous + 1):
1712 msg = _("could not locate special linker symbol __ctbp");
1713 goto common_error;
435b1e90 1714
252b5132
RH
1715 default:
1716 msg = _("internal error: unknown error");
1717 /* fall through */
1718
1719 common_error:
1720 if (!((*info->callbacks->warning)
1721 (info, msg, name, input_bfd, input_section,
1722 rel->r_offset)))
1723 return false;
1724 break;
1725 }
1726 }
1727 }
1728
1729 return true;
1730}
1731
1732static boolean
1733v850_elf_gc_sweep_hook (abfd, info, sec, relocs)
5f771d47
ILT
1734 bfd *abfd ATTRIBUTE_UNUSED;
1735 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1736 asection *sec ATTRIBUTE_UNUSED;
1737 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
252b5132 1738{
e12dd2ea 1739 /* No got and plt entries for v850-elf. */
252b5132
RH
1740 return true;
1741}
1742
1743static asection *
1744v850_elf_gc_mark_hook (abfd, info, rel, h, sym)
1745 bfd *abfd;
5f771d47 1746 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1747 Elf_Internal_Rela *rel;
1748 struct elf_link_hash_entry *h;
1749 Elf_Internal_Sym *sym;
1750{
1751 if (h != NULL)
1752 {
1753 switch (ELF32_R_TYPE (rel->r_info))
1754 {
1755 case R_V850_GNU_VTINHERIT:
1756 case R_V850_GNU_VTENTRY:
1757 break;
1758
1759 default:
1760 switch (h->root.type)
1761 {
1762 case bfd_link_hash_defined:
1763 case bfd_link_hash_defweak:
1764 return h->root.u.def.section;
1765
1766 case bfd_link_hash_common:
1767 return h->root.u.c.p->section;
e049a0de
ILT
1768
1769 default:
1770 break;
252b5132
RH
1771 }
1772 }
1773 }
1774 else
1775 {
1776 if (!(elf_bad_symtab (abfd)
1777 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
1778 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
1779 && sym->st_shndx != SHN_COMMON))
1780 {
1781 return bfd_section_from_elf_index (abfd, sym->st_shndx);
1782 }
1783 }
1784 return NULL;
1785}
e12dd2ea 1786
252b5132 1787/* Set the right machine number. */
e12dd2ea 1788
252b5132
RH
1789static boolean
1790v850_elf_object_p (abfd)
1791 bfd *abfd;
1792{
1793 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1794 {
1795 default:
1796 case E_V850_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, 0); break;
1797 case E_V850E_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e); break;
1798 case E_V850EA_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850ea); break;
1799 }
1800 return true;
1801}
1802
1803/* Store the machine number in the flags field. */
e12dd2ea 1804
252b5132
RH
1805static void
1806v850_elf_final_write_processing (abfd, linker)
1807 bfd * abfd;
5f771d47 1808 boolean linker ATTRIBUTE_UNUSED;
252b5132
RH
1809{
1810 unsigned long val;
1811
1812 switch (bfd_get_mach (abfd))
1813 {
1814 default:
1815 case 0: val = E_V850_ARCH; break;
1816 case bfd_mach_v850e: val = E_V850E_ARCH; break;
1817 case bfd_mach_v850ea: val = E_V850EA_ARCH; break;
1818 }
1819
1820 elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH;
1821 elf_elfheader (abfd)->e_flags |= val;
1822}
1823
435b1e90 1824/* Function to keep V850 specific file flags. */
e12dd2ea 1825
252b5132
RH
1826static boolean
1827v850_elf_set_private_flags (abfd, flags)
1828 bfd * abfd;
1829 flagword flags;
1830{
1831 BFD_ASSERT (!elf_flags_init (abfd)
1832 || elf_elfheader (abfd)->e_flags == flags);
1833
1834 elf_elfheader (abfd)->e_flags = flags;
1835 elf_flags_init (abfd) = true;
1836 return true;
1837}
1838
e12dd2ea
NC
1839/* Copy backend specific data from one object module to another. */
1840
252b5132
RH
1841static boolean
1842v850_elf_copy_private_bfd_data (ibfd, obfd)
1843 bfd * ibfd;
1844 bfd * obfd;
1845{
1846 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1847 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1848 return true;
1849
1850 BFD_ASSERT (!elf_flags_init (obfd)
1851 || (elf_elfheader (obfd)->e_flags
1852 == elf_elfheader (ibfd)->e_flags));
1853
1854 elf_gp (obfd) = elf_gp (ibfd);
1855 elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
1856 elf_flags_init (obfd) = true;
e12dd2ea 1857
252b5132
RH
1858 return true;
1859}
1860
e12dd2ea
NC
1861/* Merge backend specific data from an object file
1862 to the output object file when linking. */
252b5132
RH
1863static boolean
1864v850_elf_merge_private_bfd_data (ibfd, obfd)
1865 bfd * ibfd;
1866 bfd * obfd;
1867{
1868 flagword out_flags;
1869 flagword in_flags;
1870
1871 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1872 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1873 return true;
1874
1875 in_flags = elf_elfheader (ibfd)->e_flags;
1876 out_flags = elf_elfheader (obfd)->e_flags;
1877
1878 if (! elf_flags_init (obfd))
1879 {
1880 /* If the input is the default architecture then do not
1881 bother setting the flags for the output architecture,
1882 instead allow future merges to do this. If no future
1883 merges ever set these flags then they will retain their
1884 unitialised values, which surprise surprise, correspond
1885 to the default values. */
1886 if (bfd_get_arch_info (ibfd)->the_default)
1887 return true;
435b1e90 1888
252b5132
RH
1889 elf_flags_init (obfd) = true;
1890 elf_elfheader (obfd)->e_flags = in_flags;
1891
1892 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1893 && bfd_get_arch_info (obfd)->the_default)
e12dd2ea 1894 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
252b5132
RH
1895
1896 return true;
1897 }
1898
1899 /* Check flag compatibility. */
1900 if (in_flags == out_flags)
1901 return true;
1902
1903 if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH)
1904 && (in_flags & EF_V850_ARCH) != E_V850_ARCH)
1905 _bfd_error_handler (_("%s: Architecture mismatch with previous modules"),
1906 bfd_get_filename (ibfd));
1907
1908 return true;
1909}
e12dd2ea
NC
1910
1911/* Display the flags field. */
252b5132
RH
1912
1913static boolean
1914v850_elf_print_private_bfd_data (abfd, ptr)
1915 bfd * abfd;
1916 PTR ptr;
1917{
1918 FILE * file = (FILE *) ptr;
435b1e90 1919
252b5132 1920 BFD_ASSERT (abfd != NULL && ptr != NULL);
435b1e90 1921
252b5132 1922 _bfd_elf_print_private_bfd_data (abfd, ptr);
435b1e90 1923
252b5132
RH
1924 /* xgettext:c-format */
1925 fprintf (file, _("private flags = %lx: "), elf_elfheader (abfd)->e_flags);
435b1e90 1926
252b5132
RH
1927 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1928 {
1929 default:
1930 case E_V850_ARCH: fprintf (file, _("v850 architecture")); break;
1931 case E_V850E_ARCH: fprintf (file, _("v850e architecture")); break;
1932 case E_V850EA_ARCH: fprintf (file, _("v850ea architecture")); break;
1933 }
435b1e90 1934
252b5132 1935 fputc ('\n', file);
435b1e90 1936
252b5132
RH
1937 return true;
1938}
1939
1940/* V850 ELF uses four common sections. One is the usual one, and the
1941 others are for (small) objects in one of the special data areas:
1942 small, tiny and zero. All the objects are kept together, and then
1943 referenced via the gp register, the ep register or the r0 register
1944 respectively, which yields smaller, faster assembler code. This
1945 approach is copied from elf32-mips.c. */
1946
1947static asection v850_elf_scom_section;
1948static asymbol v850_elf_scom_symbol;
1949static asymbol * v850_elf_scom_symbol_ptr;
1950static asection v850_elf_tcom_section;
1951static asymbol v850_elf_tcom_symbol;
1952static asymbol * v850_elf_tcom_symbol_ptr;
1953static asection v850_elf_zcom_section;
1954static asymbol v850_elf_zcom_symbol;
1955static asymbol * v850_elf_zcom_symbol_ptr;
1956
e12dd2ea
NC
1957/* Given a BFD section, try to locate the
1958 corresponding ELF section index. */
252b5132
RH
1959
1960static boolean
1961v850_elf_section_from_bfd_section (abfd, hdr, sec, retval)
5f771d47
ILT
1962 bfd * abfd ATTRIBUTE_UNUSED;
1963 Elf32_Internal_Shdr * hdr ATTRIBUTE_UNUSED;
252b5132
RH
1964 asection * sec;
1965 int * retval;
1966{
1967 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1968 *retval = SHN_V850_SCOMMON;
1969 else if (strcmp (bfd_get_section_name (abfd, sec), ".tcommon") == 0)
1970 *retval = SHN_V850_TCOMMON;
1971 else if (strcmp (bfd_get_section_name (abfd, sec), ".zcommon") == 0)
1972 *retval = SHN_V850_ZCOMMON;
1973 else
1974 return false;
435b1e90 1975
252b5132
RH
1976 return true;
1977}
1978
1979/* Handle the special V850 section numbers that a symbol may use. */
1980
1981static void
1982v850_elf_symbol_processing (abfd, asym)
1983 bfd * abfd;
1984 asymbol * asym;
1985{
1986 elf_symbol_type * elfsym = (elf_symbol_type *) asym;
1987 unsigned short index;
435b1e90 1988
252b5132
RH
1989 index = elfsym->internal_elf_sym.st_shndx;
1990
1991 /* If the section index is an "ordinary" index, then it may
1992 refer to a v850 specific section created by the assembler.
1993 Check the section's type and change the index it matches.
435b1e90 1994
252b5132 1995 FIXME: Should we alter the st_shndx field as well ? */
435b1e90 1996
252b5132
RH
1997 if (index < elf_elfheader(abfd)[0].e_shnum)
1998 switch (elf_elfsections(abfd)[index]->sh_type)
1999 {
2000 case SHT_V850_SCOMMON:
2001 index = SHN_V850_SCOMMON;
2002 break;
435b1e90 2003
252b5132
RH
2004 case SHT_V850_TCOMMON:
2005 index = SHN_V850_TCOMMON;
2006 break;
435b1e90 2007
252b5132
RH
2008 case SHT_V850_ZCOMMON:
2009 index = SHN_V850_ZCOMMON;
2010 break;
435b1e90 2011
252b5132
RH
2012 default:
2013 break;
2014 }
435b1e90 2015
252b5132
RH
2016 switch (index)
2017 {
2018 case SHN_V850_SCOMMON:
2019 if (v850_elf_scom_section.name == NULL)
2020 {
2021 /* Initialize the small common section. */
2022 v850_elf_scom_section.name = ".scommon";
2023 v850_elf_scom_section.flags = SEC_IS_COMMON | SEC_ALLOC | SEC_DATA;
2024 v850_elf_scom_section.output_section = & v850_elf_scom_section;
2025 v850_elf_scom_section.symbol = & v850_elf_scom_symbol;
2026 v850_elf_scom_section.symbol_ptr_ptr = & v850_elf_scom_symbol_ptr;
2027 v850_elf_scom_symbol.name = ".scommon";
2028 v850_elf_scom_symbol.flags = BSF_SECTION_SYM;
2029 v850_elf_scom_symbol.section = & v850_elf_scom_section;
2030 v850_elf_scom_symbol_ptr = & v850_elf_scom_symbol;
2031 }
2032 asym->section = & v850_elf_scom_section;
2033 asym->value = elfsym->internal_elf_sym.st_size;
2034 break;
435b1e90 2035
252b5132
RH
2036 case SHN_V850_TCOMMON:
2037 if (v850_elf_tcom_section.name == NULL)
2038 {
2039 /* Initialize the tcommon section. */
2040 v850_elf_tcom_section.name = ".tcommon";
2041 v850_elf_tcom_section.flags = SEC_IS_COMMON;
2042 v850_elf_tcom_section.output_section = & v850_elf_tcom_section;
2043 v850_elf_tcom_section.symbol = & v850_elf_tcom_symbol;
2044 v850_elf_tcom_section.symbol_ptr_ptr = & v850_elf_tcom_symbol_ptr;
2045 v850_elf_tcom_symbol.name = ".tcommon";
2046 v850_elf_tcom_symbol.flags = BSF_SECTION_SYM;
2047 v850_elf_tcom_symbol.section = & v850_elf_tcom_section;
2048 v850_elf_tcom_symbol_ptr = & v850_elf_tcom_symbol;
2049 }
2050 asym->section = & v850_elf_tcom_section;
2051 asym->value = elfsym->internal_elf_sym.st_size;
2052 break;
2053
2054 case SHN_V850_ZCOMMON:
2055 if (v850_elf_zcom_section.name == NULL)
2056 {
2057 /* Initialize the zcommon section. */
2058 v850_elf_zcom_section.name = ".zcommon";
2059 v850_elf_zcom_section.flags = SEC_IS_COMMON;
2060 v850_elf_zcom_section.output_section = & v850_elf_zcom_section;
2061 v850_elf_zcom_section.symbol = & v850_elf_zcom_symbol;
2062 v850_elf_zcom_section.symbol_ptr_ptr = & v850_elf_zcom_symbol_ptr;
2063 v850_elf_zcom_symbol.name = ".zcommon";
2064 v850_elf_zcom_symbol.flags = BSF_SECTION_SYM;
2065 v850_elf_zcom_symbol.section = & v850_elf_zcom_section;
2066 v850_elf_zcom_symbol_ptr = & v850_elf_zcom_symbol;
2067 }
2068 asym->section = & v850_elf_zcom_section;
2069 asym->value = elfsym->internal_elf_sym.st_size;
2070 break;
2071 }
2072}
2073
2074/* Hook called by the linker routine which adds symbols from an object
2075 file. We must handle the special v850 section numbers here. */
2076
252b5132
RH
2077static boolean
2078v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2079 bfd * abfd;
5f771d47 2080 struct bfd_link_info * info ATTRIBUTE_UNUSED;
252b5132 2081 const Elf_Internal_Sym * sym;
5f771d47
ILT
2082 const char ** namep ATTRIBUTE_UNUSED;
2083 flagword * flagsp ATTRIBUTE_UNUSED;
252b5132
RH
2084 asection ** secp;
2085 bfd_vma * valp;
2086{
2087 int index = sym->st_shndx;
435b1e90 2088
252b5132
RH
2089 /* If the section index is an "ordinary" index, then it may
2090 refer to a v850 specific section created by the assembler.
2091 Check the section's type and change the index it matches.
435b1e90 2092
252b5132 2093 FIXME: Should we alter the st_shndx field as well ? */
435b1e90 2094
252b5132
RH
2095 if (index < elf_elfheader(abfd)[0].e_shnum)
2096 switch (elf_elfsections(abfd)[index]->sh_type)
2097 {
2098 case SHT_V850_SCOMMON:
2099 index = SHN_V850_SCOMMON;
2100 break;
435b1e90 2101
252b5132
RH
2102 case SHT_V850_TCOMMON:
2103 index = SHN_V850_TCOMMON;
2104 break;
435b1e90 2105
252b5132
RH
2106 case SHT_V850_ZCOMMON:
2107 index = SHN_V850_ZCOMMON;
2108 break;
435b1e90 2109
252b5132
RH
2110 default:
2111 break;
2112 }
435b1e90 2113
252b5132
RH
2114 switch (index)
2115 {
2116 case SHN_V850_SCOMMON:
2117 *secp = bfd_make_section_old_way (abfd, ".scommon");
2118 (*secp)->flags |= SEC_IS_COMMON;
2119 *valp = sym->st_size;
2120 break;
435b1e90 2121
252b5132
RH
2122 case SHN_V850_TCOMMON:
2123 *secp = bfd_make_section_old_way (abfd, ".tcommon");
2124 (*secp)->flags |= SEC_IS_COMMON;
2125 *valp = sym->st_size;
2126 break;
435b1e90 2127
252b5132
RH
2128 case SHN_V850_ZCOMMON:
2129 *secp = bfd_make_section_old_way (abfd, ".zcommon");
2130 (*secp)->flags |= SEC_IS_COMMON;
2131 *valp = sym->st_size;
2132 break;
2133 }
2134
2135 return true;
2136}
2137
252b5132
RH
2138static boolean
2139v850_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5f771d47
ILT
2140 bfd * abfd ATTRIBUTE_UNUSED;
2141 struct bfd_link_info * info ATTRIBUTE_UNUSED;
2142 const char * name ATTRIBUTE_UNUSED;
252b5132
RH
2143 Elf_Internal_Sym * sym;
2144 asection * input_sec;
2145{
2146 /* If we see a common symbol, which implies a relocatable link, then
2147 if a symbol was in a special common section in an input file, mark
2148 it as a special common in the output file. */
435b1e90 2149
252b5132
RH
2150 if (sym->st_shndx == SHN_COMMON)
2151 {
2152 if (strcmp (input_sec->name, ".scommon") == 0)
2153 sym->st_shndx = SHN_V850_SCOMMON;
2154 else if (strcmp (input_sec->name, ".tcommon") == 0)
2155 sym->st_shndx = SHN_V850_TCOMMON;
2156 else if (strcmp (input_sec->name, ".zcommon") == 0)
2157 sym->st_shndx = SHN_V850_ZCOMMON;
2158 }
2159
2160 return true;
2161}
2162
2163static boolean
2164v850_elf_section_from_shdr (abfd, hdr, name)
2165 bfd * abfd;
2166 Elf_Internal_Shdr * hdr;
2167 char * name;
2168{
2169 /* There ought to be a place to keep ELF backend specific flags, but
2170 at the moment there isn't one. We just keep track of the
2171 sections by their name, instead. */
2172
2173 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2174 return false;
2175
2176 switch (hdr->sh_type)
2177 {
2178 case SHT_V850_SCOMMON:
2179 case SHT_V850_TCOMMON:
2180 case SHT_V850_ZCOMMON:
2181 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2182 (bfd_get_section_flags (abfd,
2183 hdr->bfd_section)
2184 | SEC_IS_COMMON)))
2185 return false;
2186 }
2187
2188 return true;
2189}
2190
e12dd2ea
NC
2191/* Set the correct type for a V850 ELF section. We do this
2192 by the section name, which is a hack, but ought to work. */
2193
252b5132
RH
2194static boolean
2195v850_elf_fake_sections (abfd, hdr, sec)
5f771d47 2196 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
2197 Elf32_Internal_Shdr * hdr;
2198 asection * sec;
2199{
2200 register const char * name;
2201
2202 name = bfd_get_section_name (abfd, sec);
2203
2204 if (strcmp (name, ".scommon") == 0)
2205 {
2206 hdr->sh_type = SHT_V850_SCOMMON;
2207 }
2208 else if (strcmp (name, ".tcommon") == 0)
2209 {
2210 hdr->sh_type = SHT_V850_TCOMMON;
2211 }
2212 else if (strcmp (name, ".zcommon") == 0)
2213 hdr->sh_type = SHT_V850_ZCOMMON;
435b1e90 2214
252b5132
RH
2215 return true;
2216}
252b5132
RH
2217\f
2218#define TARGET_LITTLE_SYM bfd_elf32_v850_vec
2219#define TARGET_LITTLE_NAME "elf32-v850"
2220#define ELF_ARCH bfd_arch_v850
2221#define ELF_MACHINE_CODE EM_CYGNUS_V850
2222#define ELF_MAXPAGESIZE 0x1000
435b1e90 2223
252b5132
RH
2224#define elf_info_to_howto v850_elf_info_to_howto_rela
2225#define elf_info_to_howto_rel v850_elf_info_to_howto_rel
2226
2227#define elf_backend_check_relocs v850_elf_check_relocs
2228#define elf_backend_relocate_section v850_elf_relocate_section
2229#define elf_backend_object_p v850_elf_object_p
2230#define elf_backend_final_write_processing v850_elf_final_write_processing
2231#define elf_backend_section_from_bfd_section v850_elf_section_from_bfd_section
2232#define elf_backend_symbol_processing v850_elf_symbol_processing
2233#define elf_backend_add_symbol_hook v850_elf_add_symbol_hook
2234#define elf_backend_link_output_symbol_hook v850_elf_link_output_symbol_hook
2235#define elf_backend_section_from_shdr v850_elf_section_from_shdr
2236#define elf_backend_fake_sections v850_elf_fake_sections
2237#define elf_backend_gc_mark_hook v850_elf_gc_mark_hook
2238#define elf_backend_gc_sweep_hook v850_elf_gc_sweep_hook
2239
2240#define elf_backend_can_gc_sections 1
2241
252b5132
RH
2242#define bfd_elf32_bfd_is_local_label_name v850_elf_is_local_label_name
2243#define bfd_elf32_bfd_reloc_type_lookup v850_elf_reloc_type_lookup
2244#define bfd_elf32_bfd_copy_private_bfd_data v850_elf_copy_private_bfd_data
2245#define bfd_elf32_bfd_merge_private_bfd_data v850_elf_merge_private_bfd_data
2246#define bfd_elf32_bfd_set_private_flags v850_elf_set_private_flags
2247#define bfd_elf32_bfd_print_private_bfd_data v850_elf_print_private_bfd_data
2248
2249#define elf_symbol_leading_char '_'
2250
2251#include "elf32-target.h"