]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-v850.c
daily update
[thirdparty/binutils-gdb.git] / bfd / elf32-v850.c
CommitLineData
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
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. */
dc810e39 33#define SEXT24(x) ((((x) & 0xffffff) ^ 0x800000) - 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
dc810e39 49 PARAMS ((bfd *, unsigned int, bfd_vma, bfd_byte *));
252b5132
RH
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));
252b5132
RH
67static boolean v850_elf_merge_private_bfd_data
68 PARAMS ((bfd *, bfd *));
69static boolean v850_elf_print_private_bfd_data
70 PARAMS ((bfd *, PTR));
71static boolean v850_elf_section_from_bfd_section
af746e92 72 PARAMS ((bfd *, asection *, int *));
252b5132
RH
73static void v850_elf_symbol_processing
74 PARAMS ((bfd *, asymbol *));
75static boolean v850_elf_add_symbol_hook
76 PARAMS ((bfd *, struct bfd_link_info *, const Elf_Internal_Sym *,
77 const char **, flagword *, asection **, bfd_vma *));
78static boolean v850_elf_link_output_symbol_hook
79 PARAMS ((bfd *, struct bfd_link_info *, const char *,
80 Elf_Internal_Sym *, asection *));
81static boolean v850_elf_section_from_shdr
90937f86 82 PARAMS ((bfd *, Elf_Internal_Shdr *, const char *));
e12dd2ea
NC
83static boolean v850_elf_gc_sweep_hook
84 PARAMS ((bfd *, struct bfd_link_info *, asection *,
85 const Elf_Internal_Rela *));
86static 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 *));
90
91/* Note: It is REQUIRED that the 'type' value of each entry
92 in this array match the index of the entry in the array. */
252b5132
RH
93static reloc_howto_type v850_elf_howto_table[] =
94{
95 /* This reloc does nothing. */
96 HOWTO (R_V850_NONE, /* type */
97 0, /* rightshift */
98 2, /* size (0 = byte, 1 = short, 2 = long) */
99 32, /* bitsize */
100 false, /* pc_relative */
101 0, /* bitpos */
102 complain_overflow_bitfield, /* complain_on_overflow */
103 bfd_elf_generic_reloc, /* special_function */
104 "R_V850_NONE", /* name */
105 false, /* partial_inplace */
106 0, /* src_mask */
107 0, /* dst_mask */
108 false), /* pcrel_offset */
109
435b1e90 110 /* A PC relative 9 bit branch. */
252b5132
RH
111 HOWTO (R_V850_9_PCREL, /* type */
112 2, /* rightshift */
113 2, /* size (0 = byte, 1 = short, 2 = long) */
114 26, /* bitsize */
115 true, /* pc_relative */
116 0, /* bitpos */
117 complain_overflow_bitfield, /* complain_on_overflow */
118 v850_elf_reloc, /* special_function */
119 "R_V850_9_PCREL", /* name */
120 false, /* partial_inplace */
121 0x00ffffff, /* src_mask */
122 0x00ffffff, /* dst_mask */
123 true), /* pcrel_offset */
124
435b1e90 125 /* A PC relative 22 bit branch. */
252b5132
RH
126 HOWTO (R_V850_22_PCREL, /* type */
127 2, /* rightshift */
128 2, /* size (0 = byte, 1 = short, 2 = long) */
129 22, /* bitsize */
130 true, /* pc_relative */
131 7, /* bitpos */
132 complain_overflow_signed, /* complain_on_overflow */
133 v850_elf_reloc, /* special_function */
134 "R_V850_22_PCREL", /* name */
135 false, /* partial_inplace */
136 0x07ffff80, /* src_mask */
137 0x07ffff80, /* dst_mask */
138 true), /* pcrel_offset */
139
140 /* High 16 bits of symbol value. */
141 HOWTO (R_V850_HI16_S, /* type */
142 0, /* rightshift */
143 1, /* size (0 = byte, 1 = short, 2 = long) */
144 16, /* bitsize */
145 false, /* pc_relative */
146 0, /* bitpos */
147 complain_overflow_dont, /* complain_on_overflow */
148 v850_elf_reloc, /* special_function */
149 "R_V850_HI16_S", /* name */
5c746d90 150 false, /* partial_inplace */
252b5132
RH
151 0xffff, /* src_mask */
152 0xffff, /* dst_mask */
153 false), /* pcrel_offset */
154
155 /* High 16 bits of symbol value. */
156 HOWTO (R_V850_HI16, /* type */
157 0, /* rightshift */
158 1, /* size (0 = byte, 1 = short, 2 = long) */
159 16, /* bitsize */
160 false, /* pc_relative */
161 0, /* bitpos */
162 complain_overflow_dont, /* complain_on_overflow */
163 v850_elf_reloc, /* special_function */
164 "R_V850_HI16", /* name */
5c746d90 165 false, /* partial_inplace */
252b5132
RH
166 0xffff, /* src_mask */
167 0xffff, /* dst_mask */
168 false), /* pcrel_offset */
169
170 /* Low 16 bits of symbol value. */
171 HOWTO (R_V850_LO16, /* type */
172 0, /* rightshift */
173 1, /* size (0 = byte, 1 = short, 2 = long) */
174 16, /* bitsize */
175 false, /* pc_relative */
176 0, /* bitpos */
177 complain_overflow_dont, /* complain_on_overflow */
178 v850_elf_reloc, /* special_function */
179 "R_V850_LO16", /* name */
5c746d90 180 false, /* partial_inplace */
252b5132
RH
181 0xffff, /* src_mask */
182 0xffff, /* dst_mask */
183 false), /* pcrel_offset */
184
185 /* Simple 32bit reloc. */
186 HOWTO (R_V850_32, /* type */
187 0, /* rightshift */
188 2, /* size (0 = byte, 1 = short, 2 = long) */
189 32, /* bitsize */
190 false, /* pc_relative */
191 0, /* bitpos */
192 complain_overflow_dont, /* complain_on_overflow */
193 v850_elf_reloc, /* special_function */
194 "R_V850_32", /* name */
5c746d90 195 false, /* partial_inplace */
252b5132
RH
196 0xffffffff, /* src_mask */
197 0xffffffff, /* dst_mask */
198 false), /* pcrel_offset */
199
200 /* Simple 16bit reloc. */
201 HOWTO (R_V850_16, /* type */
202 0, /* rightshift */
203 1, /* size (0 = byte, 1 = short, 2 = long) */
204 16, /* bitsize */
205 false, /* pc_relative */
206 0, /* bitpos */
207 complain_overflow_dont, /* complain_on_overflow */
208 bfd_elf_generic_reloc, /* special_function */
209 "R_V850_16", /* name */
5c746d90 210 false, /* partial_inplace */
252b5132
RH
211 0xffff, /* src_mask */
212 0xffff, /* dst_mask */
213 false), /* pcrel_offset */
214
215 /* Simple 8bit reloc. */
216 HOWTO (R_V850_8, /* type */
217 0, /* rightshift */
218 0, /* size (0 = byte, 1 = short, 2 = long) */
219 8, /* bitsize */
220 false, /* pc_relative */
221 0, /* bitpos */
222 complain_overflow_dont, /* complain_on_overflow */
223 bfd_elf_generic_reloc, /* special_function */
224 "R_V850_8", /* name */
5c746d90 225 false, /* partial_inplace */
252b5132
RH
226 0xff, /* src_mask */
227 0xff, /* dst_mask */
228 false), /* pcrel_offset */
229
230 /* 16 bit offset from the short data area pointer. */
231 HOWTO (R_V850_SDA_16_16_OFFSET, /* type */
232 0, /* rightshift */
233 1, /* size (0 = byte, 1 = short, 2 = long) */
234 16, /* bitsize */
235 false, /* pc_relative */
236 0, /* bitpos */
237 complain_overflow_dont, /* complain_on_overflow */
238 v850_elf_reloc, /* special_function */
239 "R_V850_SDA_16_16_OFFSET", /* name */
240 false, /* partial_inplace */
241 0xffff, /* src_mask */
242 0xffff, /* dst_mask */
243 false), /* pcrel_offset */
244
245 /* 15 bit offset from the short data area pointer. */
246 HOWTO (R_V850_SDA_15_16_OFFSET, /* type */
247 1, /* rightshift */
248 1, /* size (0 = byte, 1 = short, 2 = long) */
249 16, /* bitsize */
250 false, /* pc_relative */
251 1, /* bitpos */
252 complain_overflow_dont, /* complain_on_overflow */
253 v850_elf_reloc, /* special_function */
254 "R_V850_SDA_15_16_OFFSET", /* name */
255 false, /* partial_inplace */
256 0xfffe, /* src_mask */
257 0xfffe, /* dst_mask */
258 false), /* pcrel_offset */
259
260 /* 16 bit offset from the zero data area pointer. */
261 HOWTO (R_V850_ZDA_16_16_OFFSET, /* type */
262 0, /* rightshift */
263 1, /* size (0 = byte, 1 = short, 2 = long) */
264 16, /* bitsize */
265 false, /* pc_relative */
266 0, /* bitpos */
267 complain_overflow_dont, /* complain_on_overflow */
268 v850_elf_reloc, /* special_function */
269 "R_V850_ZDA_16_16_OFFSET", /* name */
270 false, /* partial_inplace */
271 0xffff, /* src_mask */
272 0xffff, /* dst_mask */
273 false), /* pcrel_offset */
274
275 /* 15 bit offset from the zero data area pointer. */
276 HOWTO (R_V850_ZDA_15_16_OFFSET, /* type */
277 1, /* rightshift */
278 1, /* size (0 = byte, 1 = short, 2 = long) */
279 16, /* bitsize */
280 false, /* pc_relative */
281 1, /* bitpos */
282 complain_overflow_dont, /* complain_on_overflow */
283 v850_elf_reloc, /* special_function */
284 "R_V850_ZDA_15_16_OFFSET", /* name */
285 false, /* partial_inplace */
286 0xfffe, /* src_mask */
287 0xfffe, /* dst_mask */
288 false), /* pcrel_offset */
289
290 /* 6 bit offset from the tiny data area pointer. */
291 HOWTO (R_V850_TDA_6_8_OFFSET, /* type */
292 2, /* rightshift */
293 1, /* size (0 = byte, 1 = short, 2 = long) */
294 8, /* bitsize */
295 false, /* pc_relative */
296 1, /* bitpos */
297 complain_overflow_dont, /* complain_on_overflow */
298 v850_elf_reloc, /* special_function */
299 "R_V850_TDA_6_8_OFFSET", /* name */
300 false, /* partial_inplace */
301 0x7e, /* src_mask */
302 0x7e, /* dst_mask */
303 false), /* pcrel_offset */
304
305 /* 8 bit offset from the tiny data area pointer. */
306 HOWTO (R_V850_TDA_7_8_OFFSET, /* type */
307 1, /* rightshift */
308 1, /* size (0 = byte, 1 = short, 2 = long) */
309 8, /* bitsize */
310 false, /* pc_relative */
311 0, /* bitpos */
312 complain_overflow_dont, /* complain_on_overflow */
313 v850_elf_reloc, /* special_function */
314 "R_V850_TDA_7_8_OFFSET", /* name */
315 false, /* partial_inplace */
316 0x7f, /* src_mask */
317 0x7f, /* dst_mask */
318 false), /* pcrel_offset */
435b1e90 319
252b5132
RH
320 /* 7 bit offset from the tiny data area pointer. */
321 HOWTO (R_V850_TDA_7_7_OFFSET, /* type */
322 0, /* rightshift */
323 1, /* size (0 = byte, 1 = short, 2 = long) */
324 7, /* bitsize */
325 false, /* pc_relative */
326 0, /* bitpos */
327 complain_overflow_dont, /* complain_on_overflow */
328 v850_elf_reloc, /* special_function */
329 "R_V850_TDA_7_7_OFFSET", /* name */
330 false, /* partial_inplace */
331 0x7f, /* src_mask */
332 0x7f, /* dst_mask */
333 false), /* pcrel_offset */
334
335 /* 16 bit offset from the tiny data area pointer! */
336 HOWTO (R_V850_TDA_16_16_OFFSET, /* type */
337 0, /* rightshift */
338 1, /* size (0 = byte, 1 = short, 2 = long) */
339 16, /* bitsize */
340 false, /* pc_relative */
341 0, /* bitpos */
342 complain_overflow_dont, /* complain_on_overflow */
343 v850_elf_reloc, /* special_function */
344 "R_V850_TDA_16_16_OFFSET", /* name */
345 false, /* partial_inplace */
346 0xffff, /* src_mask */
347 0xfff, /* dst_mask */
348 false), /* pcrel_offset */
349
350 /* 5 bit offset from the tiny data area pointer. */
351 HOWTO (R_V850_TDA_4_5_OFFSET, /* type */
352 1, /* rightshift */
353 1, /* size (0 = byte, 1 = short, 2 = long) */
354 5, /* bitsize */
355 false, /* pc_relative */
356 0, /* bitpos */
357 complain_overflow_dont, /* complain_on_overflow */
358 v850_elf_reloc, /* special_function */
359 "R_V850_TDA_4_5_OFFSET", /* name */
360 false, /* partial_inplace */
361 0x0f, /* src_mask */
362 0x0f, /* dst_mask */
363 false), /* pcrel_offset */
364
365 /* 4 bit offset from the tiny data area pointer. */
366 HOWTO (R_V850_TDA_4_4_OFFSET, /* type */
367 0, /* rightshift */
368 1, /* size (0 = byte, 1 = short, 2 = long) */
369 4, /* bitsize */
370 false, /* pc_relative */
371 0, /* bitpos */
372 complain_overflow_dont, /* complain_on_overflow */
373 v850_elf_reloc, /* special_function */
374 "R_V850_TDA_4_4_OFFSET", /* name */
375 false, /* partial_inplace */
376 0x0f, /* src_mask */
377 0x0f, /* dst_mask */
378 false), /* pcrel_offset */
379
380 /* 16 bit offset from the short data area pointer. */
381 HOWTO (R_V850_SDA_16_16_SPLIT_OFFSET, /* type */
382 0, /* rightshift */
383 2, /* size (0 = byte, 1 = short, 2 = long) */
384 16, /* bitsize */
385 false, /* pc_relative */
386 0, /* bitpos */
387 complain_overflow_dont, /* complain_on_overflow */
388 v850_elf_reloc, /* special_function */
389 "R_V850_SDA_16_16_SPLIT_OFFSET",/* name */
390 false, /* partial_inplace */
391 0xfffe0020, /* src_mask */
392 0xfffe0020, /* dst_mask */
393 false), /* pcrel_offset */
394
395 /* 16 bit offset from the zero data area pointer. */
396 HOWTO (R_V850_ZDA_16_16_SPLIT_OFFSET, /* type */
397 0, /* rightshift */
398 2, /* size (0 = byte, 1 = short, 2 = long) */
399 16, /* bitsize */
400 false, /* pc_relative */
401 0, /* bitpos */
402 complain_overflow_dont, /* complain_on_overflow */
403 v850_elf_reloc, /* special_function */
404 "R_V850_ZDA_16_16_SPLIT_OFFSET",/* name */
405 false, /* partial_inplace */
406 0xfffe0020, /* src_mask */
407 0xfffe0020, /* dst_mask */
408 false), /* pcrel_offset */
409
410 /* 6 bit offset from the call table base pointer. */
411 HOWTO (R_V850_CALLT_6_7_OFFSET, /* type */
412 0, /* rightshift */
413 1, /* size (0 = byte, 1 = short, 2 = long) */
414 7, /* bitsize */
415 false, /* pc_relative */
416 0, /* bitpos */
417 complain_overflow_dont, /* complain_on_overflow */
418 v850_elf_reloc, /* special_function */
419 "R_V850_CALLT_6_7_OFFSET", /* name */
420 false, /* partial_inplace */
421 0x3f, /* src_mask */
422 0x3f, /* dst_mask */
423 false), /* pcrel_offset */
424
425 /* 16 bit offset from the call table base pointer. */
426 HOWTO (R_V850_CALLT_16_16_OFFSET, /* type */
427 0, /* rightshift */
428 1, /* size (0 = byte, 1 = short, 2 = long) */
429 16, /* bitsize */
430 false, /* pc_relative */
431 0, /* bitpos */
432 complain_overflow_dont, /* complain_on_overflow */
433 v850_elf_reloc, /* special_function */
434 "R_V850_CALLT_16_16_OFFSET", /* name */
435 false, /* partial_inplace */
436 0xffff, /* src_mask */
437 0xffff, /* dst_mask */
438 false), /* pcrel_offset */
439
440 /* GNU extension to record C++ vtable hierarchy */
441 HOWTO (R_V850_GNU_VTINHERIT, /* type */
442 0, /* rightshift */
443 2, /* size (0 = byte, 1 = short, 2 = long) */
444 0, /* bitsize */
445 false, /* pc_relative */
446 0, /* bitpos */
447 complain_overflow_dont, /* complain_on_overflow */
448 NULL, /* special_function */
449 "R_V850_GNU_VTINHERIT", /* name */
450 false, /* partial_inplace */
451 0, /* src_mask */
452 0, /* dst_mask */
453 false), /* pcrel_offset */
454
455 /* GNU extension to record C++ vtable member usage */
456 HOWTO (R_V850_GNU_VTENTRY, /* type */
457 0, /* rightshift */
458 2, /* size (0 = byte, 1 = short, 2 = long) */
459 0, /* bitsize */
460 false, /* pc_relative */
461 0, /* bitpos */
462 complain_overflow_dont, /* complain_on_overflow */
463 _bfd_elf_rel_vtable_reloc_fn, /* special_function */
464 "R_V850_GNU_VTENTRY", /* name */
465 false, /* partial_inplace */
466 0, /* src_mask */
467 0, /* dst_mask */
468 false), /* pcrel_offset */
435b1e90 469
252b5132
RH
470};
471
472/* Map BFD reloc types to V850 ELF reloc types. */
473
474struct v850_elf_reloc_map
475{
476 /* BFD_RELOC_V850_CALLT_16_16_OFFSET is 258, which will not fix in an
477 unsigned char. */
478 bfd_reloc_code_real_type bfd_reloc_val;
917583ad 479 unsigned int elf_reloc_val;
252b5132
RH
480};
481
482static const struct v850_elf_reloc_map v850_elf_reloc_map[] =
483{
484 { BFD_RELOC_NONE, R_V850_NONE },
485 { BFD_RELOC_V850_9_PCREL, R_V850_9_PCREL },
486 { BFD_RELOC_V850_22_PCREL, R_V850_22_PCREL },
487 { BFD_RELOC_HI16_S, R_V850_HI16_S },
488 { BFD_RELOC_HI16, R_V850_HI16 },
489 { BFD_RELOC_LO16, R_V850_LO16 },
490 { BFD_RELOC_32, R_V850_32 },
491 { BFD_RELOC_16, R_V850_16 },
492 { BFD_RELOC_8, R_V850_8 },
493 { BFD_RELOC_V850_SDA_16_16_OFFSET, R_V850_SDA_16_16_OFFSET },
494 { BFD_RELOC_V850_SDA_15_16_OFFSET, R_V850_SDA_15_16_OFFSET },
495 { BFD_RELOC_V850_ZDA_16_16_OFFSET, R_V850_ZDA_16_16_OFFSET },
496 { BFD_RELOC_V850_ZDA_15_16_OFFSET, R_V850_ZDA_15_16_OFFSET },
497 { BFD_RELOC_V850_TDA_6_8_OFFSET, R_V850_TDA_6_8_OFFSET },
498 { BFD_RELOC_V850_TDA_7_8_OFFSET, R_V850_TDA_7_8_OFFSET },
499 { BFD_RELOC_V850_TDA_7_7_OFFSET, R_V850_TDA_7_7_OFFSET },
500 { BFD_RELOC_V850_TDA_16_16_OFFSET, R_V850_TDA_16_16_OFFSET },
501 { BFD_RELOC_V850_TDA_4_5_OFFSET, R_V850_TDA_4_5_OFFSET },
502 { BFD_RELOC_V850_TDA_4_4_OFFSET, R_V850_TDA_4_4_OFFSET },
503 { BFD_RELOC_V850_SDA_16_16_SPLIT_OFFSET, R_V850_SDA_16_16_SPLIT_OFFSET },
504 { BFD_RELOC_V850_ZDA_16_16_SPLIT_OFFSET, R_V850_ZDA_16_16_SPLIT_OFFSET },
505 { BFD_RELOC_V850_CALLT_6_7_OFFSET, R_V850_CALLT_6_7_OFFSET },
506 { BFD_RELOC_V850_CALLT_16_16_OFFSET, R_V850_CALLT_16_16_OFFSET },
507 { BFD_RELOC_VTABLE_INHERIT, R_V850_GNU_VTINHERIT },
508 { BFD_RELOC_VTABLE_ENTRY, R_V850_GNU_VTENTRY },
509
510};
252b5132 511\f
e12dd2ea
NC
512/* Map a bfd relocation into the appropriate howto structure. */
513
252b5132
RH
514static reloc_howto_type *
515v850_elf_reloc_type_lookup (abfd, code)
5f771d47 516 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
517 bfd_reloc_code_real_type code;
518{
519 unsigned int i;
520
e12dd2ea
NC
521 for (i = ARRAY_SIZE (v850_elf_reloc_map); i --;)
522 if (v850_elf_reloc_map[i].bfd_reloc_val == code)
523 {
917583ad 524 unsigned int elf_reloc_val = v850_elf_reloc_map[i].elf_reloc_val;
dc810e39 525
fbad9a25 526 BFD_ASSERT (v850_elf_howto_table[elf_reloc_val].type == elf_reloc_val);
435b1e90 527
fbad9a25 528 return v850_elf_howto_table + elf_reloc_val;
e12dd2ea 529 }
252b5132
RH
530
531 return NULL;
532}
252b5132
RH
533\f
534/* Set the howto pointer for an V850 ELF reloc. */
e12dd2ea 535
252b5132
RH
536static void
537v850_elf_info_to_howto_rel (abfd, cache_ptr, dst)
5f771d47 538 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
539 arelent * cache_ptr;
540 Elf32_Internal_Rel * dst;
541{
542 unsigned int r_type;
543
544 r_type = ELF32_R_TYPE (dst->r_info);
545 BFD_ASSERT (r_type < (unsigned int) R_V850_max);
546 cache_ptr->howto = &v850_elf_howto_table[r_type];
547}
548
435b1e90 549/* Set the howto pointer for a V850 ELF reloc (type RELA). */
252b5132
RH
550static void
551v850_elf_info_to_howto_rela (abfd, cache_ptr, dst)
5f771d47 552 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
553 arelent * cache_ptr;
554 Elf32_Internal_Rela *dst;
555{
556 unsigned int r_type;
557
558 r_type = ELF32_R_TYPE (dst->r_info);
559 BFD_ASSERT (r_type < (unsigned int) R_V850_max);
560 cache_ptr->howto = &v850_elf_howto_table[r_type];
561}
252b5132
RH
562\f
563/* Look through the relocs for a section during the first phase, and
564 allocate space in the global offset table or procedure linkage
565 table. */
566
567static boolean
568v850_elf_check_relocs (abfd, info, sec, relocs)
569 bfd * abfd;
570 struct bfd_link_info * info;
571 asection * sec;
572 const Elf_Internal_Rela * relocs;
573{
574 boolean ret = true;
575 bfd *dynobj;
576 Elf_Internal_Shdr *symtab_hdr;
577 struct elf_link_hash_entry **sym_hashes;
578 const Elf_Internal_Rela *rel;
579 const Elf_Internal_Rela *rel_end;
580 asection *sreloc;
581 enum v850_reloc_type r_type;
582 int other = 0;
583 const char *common = (const char *)0;
584
585 if (info->relocateable)
586 return true;
587
588#ifdef DEBUG
589 fprintf (stderr, "v850_elf_check_relocs called for section %s in %s\n",
590 bfd_get_section_name (abfd, sec),
8f615d07 591 bfd_archive_filename (abfd));
252b5132
RH
592#endif
593
594 dynobj = elf_hash_table (info)->dynobj;
595 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
596 sym_hashes = elf_sym_hashes (abfd);
597 sreloc = NULL;
598
599 rel_end = relocs + sec->reloc_count;
600 for (rel = relocs; rel < rel_end; rel++)
601 {
602 unsigned long r_symndx;
603 struct elf_link_hash_entry *h;
604
605 r_symndx = ELF32_R_SYM (rel->r_info);
606 if (r_symndx < symtab_hdr->sh_info)
607 h = NULL;
608 else
609 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
610
611 r_type = (enum v850_reloc_type) ELF32_R_TYPE (rel->r_info);
612 switch (r_type)
613 {
614 default:
615 case R_V850_NONE:
616 case R_V850_9_PCREL:
617 case R_V850_22_PCREL:
618 case R_V850_HI16_S:
619 case R_V850_HI16:
620 case R_V850_LO16:
621 case R_V850_32:
622 case R_V850_16:
623 case R_V850_8:
624 case R_V850_CALLT_6_7_OFFSET:
625 case R_V850_CALLT_16_16_OFFSET:
626 break;
627
628 /* This relocation describes the C++ object vtable hierarchy.
629 Reconstruct it for later use during GC. */
630 case R_V850_GNU_VTINHERIT:
631 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
632 return false;
633 break;
634
e12dd2ea
NC
635 /* This relocation describes which C++ vtable entries
636 are actually used. Record for later use during GC. */
252b5132
RH
637 case R_V850_GNU_VTENTRY:
638 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
639 return false;
640 break;
641
642 case R_V850_SDA_16_16_SPLIT_OFFSET:
643 case R_V850_SDA_16_16_OFFSET:
644 case R_V850_SDA_15_16_OFFSET:
645 other = V850_OTHER_SDA;
646 common = ".scommon";
647 goto small_data_common;
435b1e90 648
252b5132
RH
649 case R_V850_ZDA_16_16_SPLIT_OFFSET:
650 case R_V850_ZDA_16_16_OFFSET:
651 case R_V850_ZDA_15_16_OFFSET:
652 other = V850_OTHER_ZDA;
653 common = ".zcommon";
654 goto small_data_common;
435b1e90 655
252b5132
RH
656 case R_V850_TDA_4_5_OFFSET:
657 case R_V850_TDA_4_4_OFFSET:
658 case R_V850_TDA_6_8_OFFSET:
659 case R_V850_TDA_7_8_OFFSET:
660 case R_V850_TDA_7_7_OFFSET:
661 case R_V850_TDA_16_16_OFFSET:
662 other = V850_OTHER_TDA;
663 common = ".tcommon";
664 /* fall through */
665
666#define V850_OTHER_MASK (V850_OTHER_TDA | V850_OTHER_SDA | V850_OTHER_ZDA)
667
668 small_data_common:
669 if (h)
670 {
e12dd2ea
NC
671 /* Flag which type of relocation was used. */
672 h->other |= other;
252b5132
RH
673 if ((h->other & V850_OTHER_MASK) != (other & V850_OTHER_MASK)
674 && (h->other & V850_OTHER_ERROR) == 0)
675 {
676 const char * msg;
677 static char buff[200]; /* XXX */
678
679 switch (h->other & V850_OTHER_MASK)
680 {
681 default:
682 msg = _("Variable `%s' cannot occupy in multiple small data regions");
683 break;
684 case V850_OTHER_SDA | V850_OTHER_ZDA | V850_OTHER_TDA:
685 msg = _("Variable `%s' can only be in one of the small, zero, and tiny data regions");
686 break;
687 case V850_OTHER_SDA | V850_OTHER_ZDA:
688 msg = _("Variable `%s' cannot be in both small and zero data regions simultaneously");
689 break;
690 case V850_OTHER_SDA | V850_OTHER_TDA:
691 msg = _("Variable `%s' cannot be in both small and tiny data regions simultaneously");
692 break;
693 case V850_OTHER_ZDA | V850_OTHER_TDA:
694 msg = _("Variable `%s' cannot be in both zero and tiny data regions simultaneously");
695 break;
696 }
697
698 sprintf (buff, msg, h->root.root.string);
699 info->callbacks->warning (info, buff, h->root.root.string,
dc810e39
AM
700 abfd, h->root.u.def.section,
701 (bfd_vma) 0);
252b5132
RH
702
703 bfd_set_error (bfd_error_bad_value);
704 h->other |= V850_OTHER_ERROR;
705 ret = false;
706 }
707 }
708
709 if (h && h->root.type == bfd_link_hash_common
710 && h->root.u.c.p
711 && !strcmp (bfd_get_section_name (abfd, h->root.u.c.p->section), "COMMON"))
712 {
e12dd2ea
NC
713 asection * section;
714
715 section = h->root.u.c.p->section = bfd_make_section_old_way (abfd, common);
252b5132
RH
716 section->flags |= SEC_IS_COMMON;
717 }
718
719#ifdef DEBUG
720 fprintf (stderr, "v850_elf_check_relocs, found %s relocation for %s%s\n",
721 v850_elf_howto_table[ (int)r_type ].name,
722 (h && h->root.root.string) ? h->root.root.string : "<unknown>",
723 (h->root.type == bfd_link_hash_common) ? ", symbol is common" : "");
724#endif
725 break;
726 }
727 }
728
729 return ret;
730}
731
e12dd2ea
NC
732/* In the old version, when an entry was checked out from the table,
733 it was deleted. This produced an error if the entry was needed
734 more than once, as the second attempted retry failed.
735
736 In the current version, the entry is not deleted, instead we set
737 the field 'found' to true. If a second lookup matches the same
738 entry, then we know that the hi16s reloc has already been updated
739 and does not need to be updated a second time.
740
741 TODO - TOFIX: If it is possible that we need to restore 2 different
742 addresses from the same table entry, where the first generates an
743 overflow, whilst the second do not, then this code will fail. */
252b5132
RH
744
745typedef struct hi16s_location
746{
747 bfd_vma addend;
748 bfd_byte * address;
749 unsigned long counter;
750 boolean found;
751 struct hi16s_location * next;
752}
753hi16s_location;
754
755static hi16s_location * previous_hi16s;
756static hi16s_location * free_hi16s;
757static unsigned long hi16s_counter;
758
759static void
760remember_hi16s_reloc (abfd, addend, address)
761 bfd * abfd;
762 bfd_vma addend;
763 bfd_byte * address;
764{
765 hi16s_location * entry = NULL;
dc810e39 766 bfd_size_type amt = sizeof (* free_hi16s);
435b1e90 767
252b5132
RH
768 /* Find a free structure. */
769 if (free_hi16s == NULL)
dc810e39 770 free_hi16s = (hi16s_location *) bfd_zalloc (abfd, amt);
252b5132
RH
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)
dc810e39
AM
840 bfd *abfd;
841 unsigned int r_type;
842 bfd_vma addend;
843 bfd_byte *address;
252b5132
RH
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));
dc810e39 868 bfd_put_32 (abfd, (bfd_vma) insn, address);
252b5132 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;
dc810e39 1067 bfd_put_16 (abfd, (bfd_vma) insn, hi16s_address);
252b5132
RH
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
dc810e39 1133 insn = (addend &~ (bfd_vma) 1) | (insn & 1);
252b5132 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;
dc810e39 1222 insn |= (addend &~ (bfd_vma) 1) << 16;
435b1e90 1223
dc810e39 1224 bfd_put_32 (abfd, (bfd_vma) insn, address);
252b5132 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
dc810e39 1249 bfd_put_16 (abfd, (bfd_vma) insn, address);
252b5132
RH
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
dc810e39 1318 _start:
a0f2e916
NC
1319 jr foo
1320
1321 .section ".foo","ax"
1322 nop
1323 foo:
e12dd2ea 1324 nop */
82e51918 1325 if (reloc->howto->pc_relative)
252b5132
RH
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 1365{
dc810e39 1366 unsigned int r_type = howto->type;
252b5132
RH
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
f0fe0e16
AM
1532 if (info->relocateable)
1533 return true;
1534
252b5132
RH
1535 symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
1536 sym_hashes = elf_sym_hashes (input_bfd);
1537
1538 if (sym_hashes == NULL)
1539 {
1540 info->callbacks->warning
dc810e39
AM
1541 (info, "no hash table available",
1542 NULL, input_bfd, input_section, (bfd_vma) 0);
252b5132
RH
1543
1544 return false;
1545 }
435b1e90 1546
252b5132
RH
1547 /* Reset the list of remembered HI16S relocs to empty. */
1548 free_hi16s = previous_hi16s;
1549 previous_hi16s = NULL;
1550 hi16s_counter = 0;
435b1e90 1551
252b5132
RH
1552 rel = relocs;
1553 relend = relocs + input_section->reloc_count;
1554 for (; rel < relend; rel++)
1555 {
1556 int r_type;
1557 reloc_howto_type * howto;
1558 unsigned long r_symndx;
1559 Elf_Internal_Sym * sym;
1560 asection * sec;
1561 struct elf_link_hash_entry * h;
1562 bfd_vma relocation;
1563 bfd_reloc_status_type r;
1564
1565 r_symndx = ELF32_R_SYM (rel->r_info);
1566 r_type = ELF32_R_TYPE (rel->r_info);
1567
1568 if (r_type == R_V850_GNU_VTENTRY
1569 || r_type == R_V850_GNU_VTINHERIT)
1570 continue;
1571
252b5132 1572 /* This is a final link. */
f0fe0e16 1573 howto = v850_elf_howto_table + r_type;
252b5132
RH
1574 h = NULL;
1575 sym = NULL;
1576 sec = NULL;
1577 if (r_symndx < symtab_hdr->sh_info)
1578 {
1579 sym = local_syms + r_symndx;
1580 sec = local_sections[r_symndx];
f8df10f4 1581 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
252b5132
RH
1582#if 0
1583 {
1584 char * name;
e12dd2ea 1585
252b5132
RH
1586 name = bfd_elf_string_from_elf_section (input_bfd, symtab_hdr->sh_link, sym->st_name);
1587 name = (name == NULL) ? "<none>" : name;
e12dd2ea
NC
1588 fprintf (stderr, "local: sec: %s, sym: %s (%d), value: %x + %x + %x addend %x\n",
1589 sec->name, name, sym->st_name,
1590 sec->output_section->vma, sec->output_offset, sym->st_value, rel->r_addend);
252b5132
RH
1591 }
1592#endif
1593 }
1594 else
1595 {
1596 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
435b1e90 1597
252b5132
RH
1598 while (h->root.type == bfd_link_hash_indirect
1599 || h->root.type == bfd_link_hash_warning)
1600 h = (struct elf_link_hash_entry *) h->root.u.i.link;
435b1e90 1601
252b5132
RH
1602 if (h->root.type == bfd_link_hash_defined
1603 || h->root.type == bfd_link_hash_defweak)
1604 {
1605 sec = h->root.u.def.section;
1606 relocation = (h->root.u.def.value
1607 + sec->output_section->vma
1608 + sec->output_offset);
1609#if 0
e12dd2ea
NC
1610 fprintf (stderr, "defined: sec: %s, name: %s, value: %x + %x + %x gives: %x\n",
1611 sec->name, h->root.root.string, h->root.u.def.value, sec->output_section->vma, sec->output_offset, relocation);
252b5132
RH
1612#endif
1613 }
1614 else if (h->root.type == bfd_link_hash_undefweak)
1615 {
1616#if 0
e12dd2ea
NC
1617 fprintf (stderr, "undefined: sec: %s, name: %s\n",
1618 sec->name, h->root.root.string);
252b5132
RH
1619#endif
1620 relocation = 0;
1621 }
1622 else
1623 {
1624 if (! ((*info->callbacks->undefined_symbol)
1625 (info, h->root.root.string, input_bfd,
5cc7c785 1626 input_section, rel->r_offset, true)))
252b5132
RH
1627 return false;
1628#if 0
e12dd2ea 1629 fprintf (stderr, "unknown: name: %s\n", h->root.root.string);
252b5132
RH
1630#endif
1631 relocation = 0;
1632 }
1633 }
1634
e12dd2ea 1635 /* FIXME: We should use the addend, but the COFF relocations don't. */
252b5132
RH
1636 r = v850_elf_final_link_relocate (howto, input_bfd, output_bfd,
1637 input_section,
1638 contents, rel->r_offset,
1639 relocation, rel->r_addend,
1640 info, sec, h == NULL);
1641
1642 if (r != bfd_reloc_ok)
1643 {
1644 const char * name;
1645 const char * msg = (const char *)0;
1646
1647 if (h != NULL)
1648 name = h->root.root.string;
1649 else
1650 {
1651 name = (bfd_elf_string_from_elf_section
1652 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1653 if (name == NULL || *name == '\0')
1654 name = bfd_section_name (input_bfd, sec);
1655 }
1656
1657 switch (r)
1658 {
1659 case bfd_reloc_overflow:
1660 if (! ((*info->callbacks->reloc_overflow)
1661 (info, name, howto->name, (bfd_vma) 0,
1662 input_bfd, input_section, rel->r_offset)))
1663 return false;
1664 break;
1665
1666 case bfd_reloc_undefined:
1667 if (! ((*info->callbacks->undefined_symbol)
1668 (info, name, input_bfd, input_section,
5cc7c785 1669 rel->r_offset, true)))
252b5132
RH
1670 return false;
1671 break;
1672
1673 case bfd_reloc_outofrange:
1674 msg = _("internal error: out of range error");
1675 goto common_error;
1676
1677 case bfd_reloc_notsupported:
1678 msg = _("internal error: unsupported relocation error");
1679 goto common_error;
1680
1681 case bfd_reloc_dangerous:
1682 msg = _("internal error: dangerous relocation");
1683 goto common_error;
1684
1685 case bfd_reloc_other:
1686 msg = _("could not locate special linker symbol __gp");
1687 goto common_error;
1688
1689 case bfd_reloc_continue:
1690 msg = _("could not locate special linker symbol __ep");
1691 goto common_error;
1692
1693 case (bfd_reloc_dangerous + 1):
1694 msg = _("could not locate special linker symbol __ctbp");
1695 goto common_error;
435b1e90 1696
252b5132
RH
1697 default:
1698 msg = _("internal error: unknown error");
1699 /* fall through */
1700
1701 common_error:
1702 if (!((*info->callbacks->warning)
1703 (info, msg, name, input_bfd, input_section,
1704 rel->r_offset)))
1705 return false;
1706 break;
1707 }
1708 }
1709 }
1710
1711 return true;
1712}
1713
1714static boolean
1715v850_elf_gc_sweep_hook (abfd, info, sec, relocs)
5f771d47
ILT
1716 bfd *abfd ATTRIBUTE_UNUSED;
1717 struct bfd_link_info *info ATTRIBUTE_UNUSED;
1718 asection *sec ATTRIBUTE_UNUSED;
1719 const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
252b5132 1720{
e12dd2ea 1721 /* No got and plt entries for v850-elf. */
252b5132
RH
1722 return true;
1723}
1724
1725static asection *
1e2f5b6e
AM
1726v850_elf_gc_mark_hook (sec, info, rel, h, sym)
1727 asection *sec;
5f771d47 1728 struct bfd_link_info *info ATTRIBUTE_UNUSED;
252b5132
RH
1729 Elf_Internal_Rela *rel;
1730 struct elf_link_hash_entry *h;
1731 Elf_Internal_Sym *sym;
1732{
1733 if (h != NULL)
1734 {
1735 switch (ELF32_R_TYPE (rel->r_info))
1736 {
1737 case R_V850_GNU_VTINHERIT:
1738 case R_V850_GNU_VTENTRY:
1739 break;
1740
1741 default:
1742 switch (h->root.type)
1743 {
1744 case bfd_link_hash_defined:
1745 case bfd_link_hash_defweak:
1746 return h->root.u.def.section;
1747
1748 case bfd_link_hash_common:
1749 return h->root.u.c.p->section;
e049a0de
ILT
1750
1751 default:
1752 break;
252b5132
RH
1753 }
1754 }
1755 }
1756 else
1e2f5b6e 1757 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
9ad5cbcf 1758
252b5132
RH
1759 return NULL;
1760}
e12dd2ea 1761
252b5132 1762/* Set the right machine number. */
e12dd2ea 1763
252b5132
RH
1764static boolean
1765v850_elf_object_p (abfd)
1766 bfd *abfd;
1767{
1768 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1769 {
1770 default:
1771 case E_V850_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, 0); break;
1772 case E_V850E_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850e); break;
1773 case E_V850EA_ARCH: (void) bfd_default_set_arch_mach (abfd, bfd_arch_v850, bfd_mach_v850ea); break;
1774 }
1775 return true;
1776}
1777
1778/* Store the machine number in the flags field. */
e12dd2ea 1779
252b5132
RH
1780static void
1781v850_elf_final_write_processing (abfd, linker)
1782 bfd * abfd;
5f771d47 1783 boolean linker ATTRIBUTE_UNUSED;
252b5132
RH
1784{
1785 unsigned long val;
1786
1787 switch (bfd_get_mach (abfd))
1788 {
1789 default:
1790 case 0: val = E_V850_ARCH; break;
1791 case bfd_mach_v850e: val = E_V850E_ARCH; break;
1792 case bfd_mach_v850ea: val = E_V850EA_ARCH; break;
1793 }
1794
1795 elf_elfheader (abfd)->e_flags &=~ EF_V850_ARCH;
1796 elf_elfheader (abfd)->e_flags |= val;
1797}
1798
435b1e90 1799/* Function to keep V850 specific file flags. */
e12dd2ea 1800
252b5132
RH
1801static boolean
1802v850_elf_set_private_flags (abfd, flags)
1803 bfd * abfd;
1804 flagword flags;
1805{
1806 BFD_ASSERT (!elf_flags_init (abfd)
1807 || elf_elfheader (abfd)->e_flags == flags);
1808
1809 elf_elfheader (abfd)->e_flags = flags;
1810 elf_flags_init (abfd) = true;
1811 return true;
1812}
1813
e12dd2ea
NC
1814/* Merge backend specific data from an object file
1815 to the output object file when linking. */
252b5132
RH
1816static boolean
1817v850_elf_merge_private_bfd_data (ibfd, obfd)
1818 bfd * ibfd;
1819 bfd * obfd;
1820{
1821 flagword out_flags;
1822 flagword in_flags;
1823
1824 if ( bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1825 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1826 return true;
1827
1828 in_flags = elf_elfheader (ibfd)->e_flags;
1829 out_flags = elf_elfheader (obfd)->e_flags;
1830
1831 if (! elf_flags_init (obfd))
1832 {
1833 /* If the input is the default architecture then do not
1834 bother setting the flags for the output architecture,
1835 instead allow future merges to do this. If no future
1836 merges ever set these flags then they will retain their
1837 unitialised values, which surprise surprise, correspond
1838 to the default values. */
1839 if (bfd_get_arch_info (ibfd)->the_default)
1840 return true;
435b1e90 1841
252b5132
RH
1842 elf_flags_init (obfd) = true;
1843 elf_elfheader (obfd)->e_flags = in_flags;
1844
1845 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1846 && bfd_get_arch_info (obfd)->the_default)
e12dd2ea 1847 return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd), bfd_get_mach (ibfd));
252b5132
RH
1848
1849 return true;
1850 }
1851
1852 /* Check flag compatibility. */
1853 if (in_flags == out_flags)
1854 return true;
1855
1856 if ((in_flags & EF_V850_ARCH) != (out_flags & EF_V850_ARCH)
1857 && (in_flags & EF_V850_ARCH) != E_V850_ARCH)
1858 _bfd_error_handler (_("%s: Architecture mismatch with previous modules"),
8f615d07 1859 bfd_archive_filename (ibfd));
252b5132
RH
1860
1861 return true;
1862}
e12dd2ea
NC
1863
1864/* Display the flags field. */
252b5132
RH
1865
1866static boolean
1867v850_elf_print_private_bfd_data (abfd, ptr)
1868 bfd * abfd;
1869 PTR ptr;
1870{
1871 FILE * file = (FILE *) ptr;
435b1e90 1872
252b5132 1873 BFD_ASSERT (abfd != NULL && ptr != NULL);
435b1e90 1874
252b5132 1875 _bfd_elf_print_private_bfd_data (abfd, ptr);
435b1e90 1876
252b5132
RH
1877 /* xgettext:c-format */
1878 fprintf (file, _("private flags = %lx: "), elf_elfheader (abfd)->e_flags);
435b1e90 1879
252b5132
RH
1880 switch (elf_elfheader (abfd)->e_flags & EF_V850_ARCH)
1881 {
1882 default:
1883 case E_V850_ARCH: fprintf (file, _("v850 architecture")); break;
1884 case E_V850E_ARCH: fprintf (file, _("v850e architecture")); break;
1885 case E_V850EA_ARCH: fprintf (file, _("v850ea architecture")); break;
1886 }
435b1e90 1887
252b5132 1888 fputc ('\n', file);
435b1e90 1889
252b5132
RH
1890 return true;
1891}
1892
1893/* V850 ELF uses four common sections. One is the usual one, and the
1894 others are for (small) objects in one of the special data areas:
1895 small, tiny and zero. All the objects are kept together, and then
1896 referenced via the gp register, the ep register or the r0 register
1897 respectively, which yields smaller, faster assembler code. This
1898 approach is copied from elf32-mips.c. */
1899
1900static asection v850_elf_scom_section;
1901static asymbol v850_elf_scom_symbol;
1902static asymbol * v850_elf_scom_symbol_ptr;
1903static asection v850_elf_tcom_section;
1904static asymbol v850_elf_tcom_symbol;
1905static asymbol * v850_elf_tcom_symbol_ptr;
1906static asection v850_elf_zcom_section;
1907static asymbol v850_elf_zcom_symbol;
1908static asymbol * v850_elf_zcom_symbol_ptr;
1909
e12dd2ea
NC
1910/* Given a BFD section, try to locate the
1911 corresponding ELF section index. */
252b5132
RH
1912
1913static boolean
af746e92 1914v850_elf_section_from_bfd_section (abfd, sec, retval)
5f771d47 1915 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
1916 asection * sec;
1917 int * retval;
1918{
1919 if (strcmp (bfd_get_section_name (abfd, sec), ".scommon") == 0)
1920 *retval = SHN_V850_SCOMMON;
1921 else if (strcmp (bfd_get_section_name (abfd, sec), ".tcommon") == 0)
1922 *retval = SHN_V850_TCOMMON;
1923 else if (strcmp (bfd_get_section_name (abfd, sec), ".zcommon") == 0)
1924 *retval = SHN_V850_ZCOMMON;
1925 else
1926 return false;
435b1e90 1927
252b5132
RH
1928 return true;
1929}
1930
1931/* Handle the special V850 section numbers that a symbol may use. */
1932
1933static void
1934v850_elf_symbol_processing (abfd, asym)
1935 bfd * abfd;
1936 asymbol * asym;
1937{
1938 elf_symbol_type * elfsym = (elf_symbol_type *) asym;
9ad5cbcf 1939 unsigned int indx;
435b1e90 1940
9ad5cbcf 1941 indx = elfsym->internal_elf_sym.st_shndx;
252b5132
RH
1942
1943 /* If the section index is an "ordinary" index, then it may
1944 refer to a v850 specific section created by the assembler.
1945 Check the section's type and change the index it matches.
435b1e90 1946
252b5132 1947 FIXME: Should we alter the st_shndx field as well ? */
435b1e90 1948
9ad5cbcf
AM
1949 if (indx < elf_numsections (abfd))
1950 switch (elf_elfsections(abfd)[indx]->sh_type)
252b5132
RH
1951 {
1952 case SHT_V850_SCOMMON:
9ad5cbcf 1953 indx = SHN_V850_SCOMMON;
252b5132 1954 break;
435b1e90 1955
252b5132 1956 case SHT_V850_TCOMMON:
9ad5cbcf 1957 indx = SHN_V850_TCOMMON;
252b5132 1958 break;
435b1e90 1959
252b5132 1960 case SHT_V850_ZCOMMON:
9ad5cbcf 1961 indx = SHN_V850_ZCOMMON;
252b5132 1962 break;
435b1e90 1963
252b5132
RH
1964 default:
1965 break;
1966 }
435b1e90 1967
9ad5cbcf 1968 switch (indx)
252b5132
RH
1969 {
1970 case SHN_V850_SCOMMON:
1971 if (v850_elf_scom_section.name == NULL)
1972 {
1973 /* Initialize the small common section. */
1974 v850_elf_scom_section.name = ".scommon";
1975 v850_elf_scom_section.flags = SEC_IS_COMMON | SEC_ALLOC | SEC_DATA;
1976 v850_elf_scom_section.output_section = & v850_elf_scom_section;
1977 v850_elf_scom_section.symbol = & v850_elf_scom_symbol;
1978 v850_elf_scom_section.symbol_ptr_ptr = & v850_elf_scom_symbol_ptr;
1979 v850_elf_scom_symbol.name = ".scommon";
1980 v850_elf_scom_symbol.flags = BSF_SECTION_SYM;
1981 v850_elf_scom_symbol.section = & v850_elf_scom_section;
1982 v850_elf_scom_symbol_ptr = & v850_elf_scom_symbol;
1983 }
1984 asym->section = & v850_elf_scom_section;
1985 asym->value = elfsym->internal_elf_sym.st_size;
1986 break;
435b1e90 1987
252b5132
RH
1988 case SHN_V850_TCOMMON:
1989 if (v850_elf_tcom_section.name == NULL)
1990 {
1991 /* Initialize the tcommon section. */
1992 v850_elf_tcom_section.name = ".tcommon";
1993 v850_elf_tcom_section.flags = SEC_IS_COMMON;
1994 v850_elf_tcom_section.output_section = & v850_elf_tcom_section;
1995 v850_elf_tcom_section.symbol = & v850_elf_tcom_symbol;
1996 v850_elf_tcom_section.symbol_ptr_ptr = & v850_elf_tcom_symbol_ptr;
1997 v850_elf_tcom_symbol.name = ".tcommon";
1998 v850_elf_tcom_symbol.flags = BSF_SECTION_SYM;
1999 v850_elf_tcom_symbol.section = & v850_elf_tcom_section;
2000 v850_elf_tcom_symbol_ptr = & v850_elf_tcom_symbol;
2001 }
2002 asym->section = & v850_elf_tcom_section;
2003 asym->value = elfsym->internal_elf_sym.st_size;
2004 break;
2005
2006 case SHN_V850_ZCOMMON:
2007 if (v850_elf_zcom_section.name == NULL)
2008 {
2009 /* Initialize the zcommon section. */
2010 v850_elf_zcom_section.name = ".zcommon";
2011 v850_elf_zcom_section.flags = SEC_IS_COMMON;
2012 v850_elf_zcom_section.output_section = & v850_elf_zcom_section;
2013 v850_elf_zcom_section.symbol = & v850_elf_zcom_symbol;
2014 v850_elf_zcom_section.symbol_ptr_ptr = & v850_elf_zcom_symbol_ptr;
2015 v850_elf_zcom_symbol.name = ".zcommon";
2016 v850_elf_zcom_symbol.flags = BSF_SECTION_SYM;
2017 v850_elf_zcom_symbol.section = & v850_elf_zcom_section;
2018 v850_elf_zcom_symbol_ptr = & v850_elf_zcom_symbol;
2019 }
2020 asym->section = & v850_elf_zcom_section;
2021 asym->value = elfsym->internal_elf_sym.st_size;
2022 break;
2023 }
2024}
2025
2026/* Hook called by the linker routine which adds symbols from an object
2027 file. We must handle the special v850 section numbers here. */
2028
252b5132
RH
2029static boolean
2030v850_elf_add_symbol_hook (abfd, info, sym, namep, flagsp, secp, valp)
2031 bfd * abfd;
5f771d47 2032 struct bfd_link_info * info ATTRIBUTE_UNUSED;
252b5132 2033 const Elf_Internal_Sym * sym;
5f771d47
ILT
2034 const char ** namep ATTRIBUTE_UNUSED;
2035 flagword * flagsp ATTRIBUTE_UNUSED;
252b5132
RH
2036 asection ** secp;
2037 bfd_vma * valp;
2038{
9ad5cbcf 2039 unsigned int indx = sym->st_shndx;
435b1e90 2040
252b5132
RH
2041 /* If the section index is an "ordinary" index, then it may
2042 refer to a v850 specific section created by the assembler.
2043 Check the section's type and change the index it matches.
435b1e90 2044
252b5132 2045 FIXME: Should we alter the st_shndx field as well ? */
435b1e90 2046
9ad5cbcf
AM
2047 if (indx < elf_numsections (abfd))
2048 switch (elf_elfsections(abfd)[indx]->sh_type)
252b5132
RH
2049 {
2050 case SHT_V850_SCOMMON:
9ad5cbcf 2051 indx = SHN_V850_SCOMMON;
252b5132 2052 break;
435b1e90 2053
252b5132 2054 case SHT_V850_TCOMMON:
9ad5cbcf 2055 indx = SHN_V850_TCOMMON;
252b5132 2056 break;
435b1e90 2057
252b5132 2058 case SHT_V850_ZCOMMON:
9ad5cbcf 2059 indx = SHN_V850_ZCOMMON;
252b5132 2060 break;
435b1e90 2061
252b5132
RH
2062 default:
2063 break;
2064 }
435b1e90 2065
9ad5cbcf 2066 switch (indx)
252b5132
RH
2067 {
2068 case SHN_V850_SCOMMON:
2069 *secp = bfd_make_section_old_way (abfd, ".scommon");
2070 (*secp)->flags |= SEC_IS_COMMON;
2071 *valp = sym->st_size;
2072 break;
435b1e90 2073
252b5132
RH
2074 case SHN_V850_TCOMMON:
2075 *secp = bfd_make_section_old_way (abfd, ".tcommon");
2076 (*secp)->flags |= SEC_IS_COMMON;
2077 *valp = sym->st_size;
2078 break;
435b1e90 2079
252b5132
RH
2080 case SHN_V850_ZCOMMON:
2081 *secp = bfd_make_section_old_way (abfd, ".zcommon");
2082 (*secp)->flags |= SEC_IS_COMMON;
2083 *valp = sym->st_size;
2084 break;
2085 }
2086
2087 return true;
2088}
2089
252b5132
RH
2090static boolean
2091v850_elf_link_output_symbol_hook (abfd, info, name, sym, input_sec)
5f771d47
ILT
2092 bfd * abfd ATTRIBUTE_UNUSED;
2093 struct bfd_link_info * info ATTRIBUTE_UNUSED;
2094 const char * name ATTRIBUTE_UNUSED;
252b5132
RH
2095 Elf_Internal_Sym * sym;
2096 asection * input_sec;
2097{
2098 /* If we see a common symbol, which implies a relocatable link, then
2099 if a symbol was in a special common section in an input file, mark
2100 it as a special common in the output file. */
435b1e90 2101
252b5132
RH
2102 if (sym->st_shndx == SHN_COMMON)
2103 {
2104 if (strcmp (input_sec->name, ".scommon") == 0)
2105 sym->st_shndx = SHN_V850_SCOMMON;
2106 else if (strcmp (input_sec->name, ".tcommon") == 0)
2107 sym->st_shndx = SHN_V850_TCOMMON;
2108 else if (strcmp (input_sec->name, ".zcommon") == 0)
2109 sym->st_shndx = SHN_V850_ZCOMMON;
2110 }
2111
2112 return true;
2113}
2114
2115static boolean
2116v850_elf_section_from_shdr (abfd, hdr, name)
2117 bfd * abfd;
2118 Elf_Internal_Shdr * hdr;
90937f86 2119 const char * name;
252b5132
RH
2120{
2121 /* There ought to be a place to keep ELF backend specific flags, but
2122 at the moment there isn't one. We just keep track of the
2123 sections by their name, instead. */
2124
2125 if (! _bfd_elf_make_section_from_shdr (abfd, hdr, name))
2126 return false;
2127
2128 switch (hdr->sh_type)
2129 {
2130 case SHT_V850_SCOMMON:
2131 case SHT_V850_TCOMMON:
2132 case SHT_V850_ZCOMMON:
2133 if (! bfd_set_section_flags (abfd, hdr->bfd_section,
2134 (bfd_get_section_flags (abfd,
2135 hdr->bfd_section)
2136 | SEC_IS_COMMON)))
2137 return false;
2138 }
2139
2140 return true;
2141}
2142
e12dd2ea
NC
2143/* Set the correct type for a V850 ELF section. We do this
2144 by the section name, which is a hack, but ought to work. */
2145
252b5132
RH
2146static boolean
2147v850_elf_fake_sections (abfd, hdr, sec)
5f771d47 2148 bfd * abfd ATTRIBUTE_UNUSED;
252b5132
RH
2149 Elf32_Internal_Shdr * hdr;
2150 asection * sec;
2151{
2152 register const char * name;
2153
2154 name = bfd_get_section_name (abfd, sec);
2155
2156 if (strcmp (name, ".scommon") == 0)
2157 {
2158 hdr->sh_type = SHT_V850_SCOMMON;
2159 }
2160 else if (strcmp (name, ".tcommon") == 0)
2161 {
2162 hdr->sh_type = SHT_V850_TCOMMON;
2163 }
2164 else if (strcmp (name, ".zcommon") == 0)
2165 hdr->sh_type = SHT_V850_ZCOMMON;
435b1e90 2166
252b5132
RH
2167 return true;
2168}
252b5132
RH
2169\f
2170#define TARGET_LITTLE_SYM bfd_elf32_v850_vec
2171#define TARGET_LITTLE_NAME "elf32-v850"
2172#define ELF_ARCH bfd_arch_v850
aa4f99bb
AO
2173#define ELF_MACHINE_CODE EM_V850
2174#define ELF_MACHINE_ALT1 EM_CYGNUS_V850
252b5132 2175#define ELF_MAXPAGESIZE 0x1000
435b1e90 2176
252b5132
RH
2177#define elf_info_to_howto v850_elf_info_to_howto_rela
2178#define elf_info_to_howto_rel v850_elf_info_to_howto_rel
2179
2180#define elf_backend_check_relocs v850_elf_check_relocs
2181#define elf_backend_relocate_section v850_elf_relocate_section
2182#define elf_backend_object_p v850_elf_object_p
2183#define elf_backend_final_write_processing v850_elf_final_write_processing
2184#define elf_backend_section_from_bfd_section v850_elf_section_from_bfd_section
2185#define elf_backend_symbol_processing v850_elf_symbol_processing
2186#define elf_backend_add_symbol_hook v850_elf_add_symbol_hook
2187#define elf_backend_link_output_symbol_hook v850_elf_link_output_symbol_hook
2188#define elf_backend_section_from_shdr v850_elf_section_from_shdr
2189#define elf_backend_fake_sections v850_elf_fake_sections
2190#define elf_backend_gc_mark_hook v850_elf_gc_mark_hook
2191#define elf_backend_gc_sweep_hook v850_elf_gc_sweep_hook
2192
2193#define elf_backend_can_gc_sections 1
f0fe0e16 2194#define elf_backend_rela_normal 1
252b5132 2195
252b5132
RH
2196#define bfd_elf32_bfd_is_local_label_name v850_elf_is_local_label_name
2197#define bfd_elf32_bfd_reloc_type_lookup v850_elf_reloc_type_lookup
252b5132
RH
2198#define bfd_elf32_bfd_merge_private_bfd_data v850_elf_merge_private_bfd_data
2199#define bfd_elf32_bfd_set_private_flags v850_elf_set_private_flags
2200#define bfd_elf32_bfd_print_private_bfd_data v850_elf_print_private_bfd_data
2201
2202#define elf_symbol_leading_char '_'
2203
2204#include "elf32-target.h"