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