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