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