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