]>
Commit | Line | Data |
---|---|---|
a75473eb | 1 | /* IQ2000-specific support for 32-bit ELF. |
ab96bf03 AM |
2 | Copyright (C) 2003, 2004, 2005, 2006, 2007 |
3 | Free Software Foundation, Inc. | |
a75473eb | 4 | |
58852c27 | 5 | This file is part of BFD, the Binary File Descriptor library. |
a75473eb | 6 | |
58852c27 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. | |
a75473eb | 11 | |
58852c27 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. | |
a75473eb | 16 | |
58852c27 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 | |
3e110533 | 19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */ |
a75473eb | 20 | |
a75473eb | 21 | #include "sysdep.h" |
3db64b00 | 22 | #include "bfd.h" |
a75473eb SC |
23 | #include "libbfd.h" |
24 | #include "elf-bfd.h" | |
25 | #include "elf/iq2000.h" | |
26 | ||
27 | /* Forward declarations. */ | |
28 | ||
58852c27 | 29 | static bfd_reloc_status_type iq2000_elf_howto_hi16_reloc (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **); |
a75473eb SC |
30 | |
31 | \f | |
32 | static reloc_howto_type iq2000_elf_howto_table [] = | |
33 | { | |
34 | /* This reloc does nothing. */ | |
35 | ||
36 | HOWTO (R_IQ2000_NONE, /* type */ | |
37 | 0, /* rightshift */ | |
38 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
39 | 32, /* bitsize */ | |
40 | FALSE, /* pc_relative */ | |
41 | 0, /* bitpos */ | |
42 | complain_overflow_bitfield, /* complain_on_overflow */ | |
43 | bfd_elf_generic_reloc, /* special_function */ | |
44 | "R_IQ2000_NONE", /* name */ | |
45 | FALSE, /* partial_inplace */ | |
46 | 0, /* src_mask */ | |
47 | 0, /* dst_mask */ | |
48 | FALSE), /* pcrel_offset */ | |
49 | ||
50 | /* A 16 bit absolute relocation. */ | |
51 | HOWTO (R_IQ2000_16, /* type */ | |
52 | 0, /* rightshift */ | |
53 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
54 | 16, /* bitsize */ | |
55 | FALSE, /* pc_relative */ | |
56 | 0, /* bitpos */ | |
57 | complain_overflow_bitfield, /* complain_on_overflow */ | |
58 | bfd_elf_generic_reloc, /* special_function */ | |
59 | "R_IQ2000_16", /* name */ | |
60 | FALSE, /* partial_inplace */ | |
61 | 0x0000, /* src_mask */ | |
62 | 0xffff, /* dst_mask */ | |
63 | FALSE), /* pcrel_offset */ | |
64 | ||
65 | /* A 32 bit absolute relocation. */ | |
66 | HOWTO (R_IQ2000_32, /* type */ | |
67 | 0, /* rightshift */ | |
68 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
69 | 31, /* bitsize */ | |
70 | FALSE, /* pc_relative */ | |
71 | 0, /* bitpos */ | |
72 | complain_overflow_bitfield, /* complain_on_overflow */ | |
73 | bfd_elf_generic_reloc, /* special_function */ | |
74 | "R_IQ2000_32", /* name */ | |
75 | FALSE, /* partial_inplace */ | |
76 | 0x00000000, /* src_mask */ | |
77 | 0x7fffffff, /* dst_mask */ | |
78 | FALSE), /* pcrel_offset */ | |
79 | ||
80 | /* 26 bit branch address. */ | |
81 | HOWTO (R_IQ2000_26, /* type */ | |
82 | 2, /* rightshift */ | |
83 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
84 | 26, /* bitsize */ | |
85 | FALSE, /* pc_relative */ | |
86 | 0, /* bitpos */ | |
87 | complain_overflow_dont, /* complain_on_overflow */ | |
88 | /* This needs complex overflow | |
89 | detection, because the upper four | |
90 | bits must match the PC. */ | |
91 | bfd_elf_generic_reloc, /* special_function */ | |
92 | "R_IQ2000_26", /* name */ | |
93 | FALSE, /* partial_inplace */ | |
94 | 0x00000000, /* src_mask */ | |
95 | 0x03ffffff, /* dst_mask */ | |
96 | FALSE), /* pcrel_offset */ | |
97 | ||
98 | /* 16 bit PC relative reference. */ | |
99 | HOWTO (R_IQ2000_PC16, /* type */ | |
100 | 2, /* rightshift */ | |
101 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
102 | 16, /* bitsize */ | |
103 | TRUE, /* pc_relative */ | |
104 | 0, /* bitpos */ | |
105 | complain_overflow_signed, /* complain_on_overflow */ | |
106 | bfd_elf_generic_reloc, /* special_function */ | |
107 | "R_IQ2000_PC16", /* name */ | |
108 | FALSE, /* partial_inplace */ | |
109 | 0x0000, /* src_mask */ | |
110 | 0xffff, /* dst_mask */ | |
111 | TRUE), /* pcrel_offset */ | |
112 | ||
113 | /* high 16 bits of symbol value. */ | |
114 | HOWTO (R_IQ2000_HI16, /* type */ | |
115 | 16, /* rightshift */ | |
116 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
117 | 15, /* bitsize */ | |
118 | FALSE, /* pc_relative */ | |
119 | 0, /* bitpos */ | |
120 | complain_overflow_dont, /* complain_on_overflow */ | |
121 | iq2000_elf_howto_hi16_reloc, /* special_function */ | |
122 | "R_IQ2000_HI16", /* name */ | |
123 | FALSE, /* partial_inplace */ | |
124 | 0x0000, /* src_mask */ | |
125 | 0x7fff, /* dst_mask */ | |
126 | FALSE), /* pcrel_offset */ | |
127 | ||
128 | /* Low 16 bits of symbol value. */ | |
129 | HOWTO (R_IQ2000_LO16, /* type */ | |
130 | 0, /* rightshift */ | |
131 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
132 | 16, /* bitsize */ | |
133 | FALSE, /* pc_relative */ | |
134 | 0, /* bitpos */ | |
135 | complain_overflow_dont, /* complain_on_overflow */ | |
136 | bfd_elf_generic_reloc, /* special_function */ | |
137 | "R_IQ2000_LO16", /* name */ | |
138 | FALSE, /* partial_inplace */ | |
139 | 0x0000, /* src_mask */ | |
140 | 0xffff, /* dst_mask */ | |
141 | FALSE), /* pcrel_offset */ | |
142 | ||
143 | /* 16-bit jump offset. */ | |
144 | HOWTO (R_IQ2000_OFFSET_16, /* type */ | |
145 | 2, /* rightshift */ | |
146 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
147 | 16, /* bitsize */ | |
148 | FALSE, /* pc_relative */ | |
149 | 0, /* bitpos */ | |
150 | complain_overflow_dont, /* complain_on_overflow */ | |
151 | bfd_elf_generic_reloc, /* special_function */ | |
152 | "R_IQ2000_OFFSET_16", /* name */ | |
153 | FALSE, /* partial_inplace */ | |
154 | 0x0000, /* src_mask */ | |
155 | 0xffff, /* dst_mask */ | |
156 | FALSE), /* pcrel_offset */ | |
157 | ||
158 | /* 21-bit jump offset. */ | |
159 | HOWTO (R_IQ2000_OFFSET_21, /* type */ | |
160 | 2, /* rightshift */ | |
161 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
162 | 21, /* bitsize */ | |
163 | FALSE, /* pc_relative */ | |
164 | 0, /* bitpos */ | |
165 | complain_overflow_dont, /* complain_on_overflow */ | |
166 | bfd_elf_generic_reloc, /* special_function */ | |
167 | "R_IQ2000_OFFSET_21", /* name */ | |
168 | FALSE, /* partial_inplace */ | |
169 | 0x000000, /* src_mask */ | |
170 | 0x1fffff, /* dst_mask */ | |
171 | FALSE), /* pcrel_offset */ | |
172 | ||
173 | /* unsigned high 16 bits of value. */ | |
174 | HOWTO (R_IQ2000_OFFSET_21, /* type */ | |
175 | 16, /* rightshift */ | |
176 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
177 | 16, /* bitsize */ | |
178 | FALSE, /* pc_relative */ | |
179 | 0, /* bitpos */ | |
180 | complain_overflow_dont, /* complain_on_overflow */ | |
181 | bfd_elf_generic_reloc, /* special_function */ | |
182 | "R_IQ2000_UHI16", /* name */ | |
183 | FALSE, /* partial_inplace */ | |
184 | 0x0000, /* src_mask */ | |
185 | 0x7fff, /* dst_mask */ | |
186 | FALSE), /* pcrel_offset */ | |
187 | ||
188 | /* A 32 bit absolute debug relocation. */ | |
189 | HOWTO (R_IQ2000_32_DEBUG, /* type */ | |
190 | 0, /* rightshift */ | |
191 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
192 | 32, /* bitsize */ | |
193 | FALSE, /* pc_relative */ | |
194 | 0, /* bitpos */ | |
195 | complain_overflow_bitfield, /* complain_on_overflow */ | |
196 | bfd_elf_generic_reloc, /* special_function */ | |
197 | "R_IQ2000_32", /* name */ | |
198 | FALSE, /* partial_inplace */ | |
199 | 0x00000000, /* src_mask */ | |
200 | 0xffffffff, /* dst_mask */ | |
201 | FALSE), /* pcrel_offset */ | |
202 | ||
203 | }; | |
204 | ||
205 | /* GNU extension to record C++ vtable hierarchy. */ | |
206 | static reloc_howto_type iq2000_elf_vtinherit_howto = | |
207 | HOWTO (R_IQ2000_GNU_VTINHERIT, /* type */ | |
208 | 0, /* rightshift */ | |
209 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
210 | 0, /* bitsize */ | |
211 | FALSE, /* pc_relative */ | |
212 | 0, /* bitpos */ | |
213 | complain_overflow_dont, /* complain_on_overflow */ | |
214 | NULL, /* special_function */ | |
215 | "R_IQ2000_GNU_VTINHERIT", /* name */ | |
216 | FALSE, /* partial_inplace */ | |
217 | 0, /* src_mask */ | |
218 | 0, /* dst_mask */ | |
219 | FALSE); /* pcrel_offset */ | |
220 | ||
221 | /* GNU extension to record C++ vtable member usage. */ | |
222 | static reloc_howto_type iq2000_elf_vtentry_howto = | |
223 | HOWTO (R_IQ2000_GNU_VTENTRY, /* type */ | |
224 | 0, /* rightshift */ | |
225 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
226 | 0, /* bitsize */ | |
227 | FALSE, /* pc_relative */ | |
228 | 0, /* bitpos */ | |
229 | complain_overflow_dont, /* complain_on_overflow */ | |
230 | NULL, /* special_function */ | |
231 | "R_IQ2000_GNU_VTENTRY", /* name */ | |
232 | FALSE, /* partial_inplace */ | |
233 | 0, /* src_mask */ | |
234 | 0, /* dst_mask */ | |
235 | FALSE); /* pcrel_offset */ | |
236 | ||
237 | \f | |
a75473eb | 238 | static bfd_reloc_status_type |
58852c27 NC |
239 | iq2000_elf_howto_hi16_reloc (bfd *abfd ATTRIBUTE_UNUSED, |
240 | arelent *reloc_entry, | |
241 | asymbol *symbol, | |
242 | void * data, | |
243 | asection *input_section, | |
244 | bfd *output_bfd, | |
245 | char **error_message ATTRIBUTE_UNUSED) | |
a75473eb SC |
246 | { |
247 | bfd_reloc_status_type ret; | |
248 | bfd_vma relocation; | |
249 | ||
58852c27 NC |
250 | /* If we're relocating and this an external symbol, |
251 | we don't want to change anything. */ | |
a75473eb SC |
252 | if (output_bfd != (bfd *) NULL |
253 | && (symbol->flags & BSF_SECTION_SYM) == 0 | |
254 | && reloc_entry->addend == 0) | |
255 | { | |
256 | reloc_entry->address += input_section->output_offset; | |
257 | return bfd_reloc_ok; | |
258 | } | |
259 | ||
260 | if (bfd_is_com_section (symbol->section)) | |
261 | relocation = 0; | |
262 | else | |
263 | relocation = symbol->value; | |
264 | ||
265 | relocation += symbol->section->output_section->vma; | |
266 | relocation += symbol->section->output_offset; | |
267 | relocation += reloc_entry->addend; | |
268 | ||
58852c27 | 269 | /* If %lo will have sign-extension, compensate by add 0x10000 to hi portion. */ |
a75473eb SC |
270 | if (relocation & 0x8000) |
271 | reloc_entry->addend += 0x10000; | |
272 | ||
273 | /* Now do the reloc in the usual way. */ | |
274 | ret = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data, | |
275 | input_section, output_bfd, error_message); | |
276 | ||
58852c27 | 277 | /* Put it back the way it was. */ |
a75473eb SC |
278 | if (relocation & 0x8000) |
279 | reloc_entry->addend -= 0x10000; | |
280 | ||
281 | return ret; | |
282 | } | |
283 | ||
284 | static bfd_reloc_status_type | |
58852c27 NC |
285 | iq2000_elf_relocate_hi16 (bfd *input_bfd, |
286 | Elf_Internal_Rela *relhi, | |
287 | bfd_byte *contents, | |
288 | bfd_vma value) | |
a75473eb SC |
289 | { |
290 | bfd_vma insn; | |
291 | ||
292 | insn = bfd_get_32 (input_bfd, contents + relhi->r_offset); | |
293 | ||
294 | value += relhi->r_addend; | |
58852c27 | 295 | value &= 0x7fffffff; /* Mask off top-bit which is Harvard mask bit. */ |
a75473eb | 296 | |
58852c27 NC |
297 | /* If top-bit of %lo value is on, this means that %lo will |
298 | sign-propagate and so we compensate by adding 1 to %hi value. */ | |
a75473eb SC |
299 | if (value & 0x8000) |
300 | value += 0x10000; | |
301 | ||
302 | value >>= 16; | |
303 | insn = ((insn & ~0xFFFF) | value); | |
304 | ||
305 | bfd_put_32 (input_bfd, insn, contents + relhi->r_offset); | |
306 | return bfd_reloc_ok; | |
307 | } | |
308 | ||
6c408f53 NC |
309 | /* Map BFD reloc types to IQ2000 ELF reloc types. */ |
310 | ||
a75473eb | 311 | static reloc_howto_type * |
de52dba4 AM |
312 | iq2000_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED, |
313 | bfd_reloc_code_real_type code) | |
a75473eb SC |
314 | { |
315 | /* Note that the iq2000_elf_howto_table is indxed by the R_ | |
316 | constants. Thus, the order that the howto records appear in the | |
317 | table *must* match the order of the relocation types defined in | |
318 | include/elf/iq2000.h. */ | |
319 | ||
320 | switch (code) | |
321 | { | |
322 | case BFD_RELOC_NONE: | |
323 | return &iq2000_elf_howto_table[ (int) R_IQ2000_NONE]; | |
324 | case BFD_RELOC_16: | |
325 | return &iq2000_elf_howto_table[ (int) R_IQ2000_16]; | |
326 | case BFD_RELOC_32: | |
327 | return &iq2000_elf_howto_table[ (int) R_IQ2000_32]; | |
328 | case BFD_RELOC_MIPS_JMP: | |
329 | return &iq2000_elf_howto_table[ (int) R_IQ2000_26]; | |
330 | case BFD_RELOC_IQ2000_OFFSET_16: | |
331 | return &iq2000_elf_howto_table[ (int) R_IQ2000_OFFSET_16]; | |
332 | case BFD_RELOC_IQ2000_OFFSET_21: | |
333 | return &iq2000_elf_howto_table[ (int) R_IQ2000_OFFSET_21]; | |
334 | case BFD_RELOC_16_PCREL_S2: | |
335 | return &iq2000_elf_howto_table[ (int) R_IQ2000_PC16]; | |
336 | case BFD_RELOC_HI16: | |
337 | return &iq2000_elf_howto_table[ (int) R_IQ2000_HI16]; | |
338 | case BFD_RELOC_IQ2000_UHI16: | |
339 | return &iq2000_elf_howto_table[ (int) R_IQ2000_UHI16]; | |
340 | case BFD_RELOC_LO16: | |
341 | return &iq2000_elf_howto_table[ (int) R_IQ2000_LO16]; | |
342 | case BFD_RELOC_VTABLE_INHERIT: | |
343 | return &iq2000_elf_vtinherit_howto; | |
344 | case BFD_RELOC_VTABLE_ENTRY: | |
345 | return &iq2000_elf_vtentry_howto; | |
346 | default: | |
347 | /* Pacify gcc -Wall. */ | |
348 | return NULL; | |
349 | } | |
350 | return NULL; | |
351 | } | |
352 | ||
157090f7 AM |
353 | static reloc_howto_type * |
354 | iq2000_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name) | |
355 | { | |
356 | unsigned int i; | |
357 | ||
358 | for (i = 0; | |
359 | i < (sizeof (iq2000_elf_howto_table) | |
360 | / sizeof (iq2000_elf_howto_table[0])); | |
361 | i++) | |
362 | if (iq2000_elf_howto_table[i].name != NULL | |
363 | && strcasecmp (iq2000_elf_howto_table[i].name, r_name) == 0) | |
364 | return &iq2000_elf_howto_table[i]; | |
365 | ||
366 | if (strcasecmp (iq2000_elf_vtinherit_howto.name, r_name) == 0) | |
367 | return &iq2000_elf_vtinherit_howto; | |
368 | if (strcasecmp (iq2000_elf_vtentry_howto.name, r_name) == 0) | |
369 | return &iq2000_elf_vtentry_howto; | |
370 | ||
371 | return NULL; | |
372 | } | |
a75473eb SC |
373 | \f |
374 | /* Perform a single relocation. By default we use the standard BFD | |
375 | routines. */ | |
376 | ||
377 | static bfd_reloc_status_type | |
58852c27 NC |
378 | iq2000_final_link_relocate (reloc_howto_type * howto, |
379 | bfd * input_bfd, | |
380 | asection * input_section, | |
381 | bfd_byte * contents, | |
382 | Elf_Internal_Rela * rel, | |
383 | bfd_vma relocation) | |
a75473eb SC |
384 | { |
385 | return _bfd_final_link_relocate (howto, input_bfd, input_section, | |
386 | contents, rel->r_offset, | |
387 | relocation, rel->r_addend); | |
388 | } | |
389 | \f | |
390 | /* Set the howto pointer for a IQ2000 ELF reloc. */ | |
391 | ||
392 | static void | |
58852c27 NC |
393 | iq2000_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED, |
394 | arelent * cache_ptr, | |
395 | Elf_Internal_Rela * dst) | |
a75473eb SC |
396 | { |
397 | unsigned int r_type; | |
398 | ||
399 | r_type = ELF32_R_TYPE (dst->r_info); | |
400 | switch (r_type) | |
401 | { | |
402 | case R_IQ2000_GNU_VTINHERIT: | |
403 | cache_ptr->howto = & iq2000_elf_vtinherit_howto; | |
404 | break; | |
405 | ||
406 | case R_IQ2000_GNU_VTENTRY: | |
407 | cache_ptr->howto = & iq2000_elf_vtentry_howto; | |
408 | break; | |
409 | ||
410 | default: | |
411 | cache_ptr->howto = & iq2000_elf_howto_table [r_type]; | |
412 | break; | |
413 | } | |
414 | } | |
415 | ||
416 | /* Look through the relocs for a section during the first phase. | |
417 | Since we don't do .gots or .plts, we just need to consider the | |
418 | virtual table relocs for gc. */ | |
419 | ||
420 | static bfd_boolean | |
58852c27 NC |
421 | iq2000_elf_check_relocs (bfd *abfd, |
422 | struct bfd_link_info *info, | |
423 | asection *sec, | |
424 | const Elf_Internal_Rela *relocs) | |
a75473eb SC |
425 | { |
426 | Elf_Internal_Shdr *symtab_hdr; | |
427 | struct elf_link_hash_entry **sym_hashes, **sym_hashes_end; | |
60d8b524 AM |
428 | const Elf_Internal_Rela *rel; |
429 | const Elf_Internal_Rela *rel_end; | |
430 | bfd_boolean changed = FALSE; | |
a75473eb | 431 | |
1049f94e | 432 | if (info->relocatable) |
a75473eb SC |
433 | return TRUE; |
434 | ||
435 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
436 | sym_hashes = elf_sym_hashes (abfd); | |
58852c27 | 437 | sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym); |
a75473eb SC |
438 | if (!elf_bad_symtab (abfd)) |
439 | sym_hashes_end -= symtab_hdr->sh_info; | |
440 | ||
441 | rel_end = relocs + sec->reloc_count; | |
442 | for (rel = relocs; rel < rel_end; rel++) | |
443 | { | |
444 | struct elf_link_hash_entry *h; | |
445 | unsigned long r_symndx; | |
446 | ||
447 | r_symndx = ELF32_R_SYM (rel->r_info); | |
448 | if (r_symndx < symtab_hdr->sh_info) | |
449 | h = NULL; | |
450 | else | |
973a3492 L |
451 | { |
452 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
453 | while (h->root.type == bfd_link_hash_indirect | |
454 | || h->root.type == bfd_link_hash_warning) | |
455 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
456 | } | |
a75473eb SC |
457 | |
458 | switch (ELF32_R_TYPE (rel->r_info)) | |
459 | { | |
58852c27 NC |
460 | /* This relocation describes the C++ object vtable |
461 | hierarchy. Reconstruct it for later use during GC. */ | |
a75473eb | 462 | case R_IQ2000_GNU_VTINHERIT: |
c152c796 | 463 | if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset)) |
a75473eb SC |
464 | return FALSE; |
465 | break; | |
466 | ||
58852c27 NC |
467 | /* This relocation describes which C++ vtable entries |
468 | are actually used. Record for later use during GC. */ | |
a75473eb | 469 | case R_IQ2000_GNU_VTENTRY: |
c152c796 | 470 | if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend)) |
a75473eb SC |
471 | return FALSE; |
472 | break; | |
473 | ||
474 | case R_IQ2000_32: | |
58852c27 | 475 | /* For debug section, change to special harvard-aware relocations. */ |
0112cd26 NC |
476 | if (CONST_STRNEQ (sec->name, ".debug") |
477 | || CONST_STRNEQ (sec->name, ".stab") | |
478 | || CONST_STRNEQ (sec->name, ".eh_frame")) | |
60d8b524 AM |
479 | { |
480 | ((Elf_Internal_Rela *) rel)->r_info | |
481 | = ELF32_R_INFO (ELF32_R_SYM (rel->r_info), R_IQ2000_32_DEBUG); | |
482 | changed = TRUE; | |
483 | } | |
a75473eb SC |
484 | break; |
485 | } | |
486 | } | |
60d8b524 AM |
487 | |
488 | if (changed) | |
489 | /* Note that we've changed relocs, otherwise if !info->keep_memory | |
490 | we'll free the relocs and lose our changes. */ | |
d5cb4bb8 | 491 | elf_section_data (sec)->relocs = (Elf_Internal_Rela *) relocs; |
60d8b524 | 492 | |
a75473eb SC |
493 | return TRUE; |
494 | } | |
495 | ||
496 | \f | |
497 | /* Relocate a IQ2000 ELF section. | |
498 | There is some attempt to make this function usable for many architectures, | |
499 | both USE_REL and USE_RELA ['twould be nice if such a critter existed], | |
500 | if only to serve as a learning tool. | |
501 | ||
502 | The RELOCATE_SECTION function is called by the new ELF backend linker | |
503 | to handle the relocations for a section. | |
504 | ||
505 | The relocs are always passed as Rela structures; if the section | |
506 | actually uses Rel structures, the r_addend field will always be | |
507 | zero. | |
508 | ||
509 | This function is responsible for adjusting the section contents as | |
1049f94e | 510 | necessary, and (if using Rela relocs and generating a relocatable |
a75473eb SC |
511 | output file) adjusting the reloc addend as necessary. |
512 | ||
513 | This function does not have to worry about setting the reloc | |
514 | address or the reloc symbol index. | |
515 | ||
516 | LOCAL_SYMS is a pointer to the swapped in local symbols. | |
517 | ||
518 | LOCAL_SECTIONS is an array giving the section in the input file | |
519 | corresponding to the st_shndx field of each local symbol. | |
520 | ||
521 | The global hash table entry for the global symbols can be found | |
522 | via elf_sym_hashes (input_bfd). | |
523 | ||
1049f94e | 524 | When generating relocatable output, this function must handle |
a75473eb SC |
525 | STB_LOCAL/STT_SECTION symbols specially. The output symbol is |
526 | going to be the section symbol corresponding to the output | |
527 | section, which means that the addend must be adjusted | |
528 | accordingly. */ | |
529 | ||
530 | static bfd_boolean | |
58852c27 NC |
531 | iq2000_elf_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED, |
532 | struct bfd_link_info * info, | |
533 | bfd * input_bfd, | |
534 | asection * input_section, | |
535 | bfd_byte * contents, | |
536 | Elf_Internal_Rela * relocs, | |
537 | Elf_Internal_Sym * local_syms, | |
538 | asection ** local_sections) | |
a75473eb SC |
539 | { |
540 | Elf_Internal_Shdr * symtab_hdr; | |
541 | struct elf_link_hash_entry ** sym_hashes; | |
542 | Elf_Internal_Rela * rel; | |
543 | Elf_Internal_Rela * relend; | |
544 | ||
545 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
546 | sym_hashes = elf_sym_hashes (input_bfd); | |
547 | relend = relocs + input_section->reloc_count; | |
548 | ||
549 | for (rel = relocs; rel < relend; rel ++) | |
550 | { | |
551 | reloc_howto_type * howto; | |
552 | unsigned long r_symndx; | |
553 | Elf_Internal_Sym * sym; | |
554 | asection * sec; | |
555 | struct elf_link_hash_entry * h; | |
556 | bfd_vma relocation; | |
557 | bfd_reloc_status_type r; | |
558 | const char * name = NULL; | |
559 | int r_type; | |
560 | ||
561 | r_type = ELF32_R_TYPE (rel->r_info); | |
562 | ||
563 | if ( r_type == R_IQ2000_GNU_VTINHERIT | |
564 | || r_type == R_IQ2000_GNU_VTENTRY) | |
565 | continue; | |
566 | ||
567 | r_symndx = ELF32_R_SYM (rel->r_info); | |
568 | ||
a75473eb SC |
569 | howto = iq2000_elf_howto_table + ELF32_R_TYPE (rel->r_info); |
570 | h = NULL; | |
571 | sym = NULL; | |
572 | sec = NULL; | |
573 | ||
574 | if (r_symndx < symtab_hdr->sh_info) | |
575 | { | |
576 | sym = local_syms + r_symndx; | |
577 | sec = local_sections [r_symndx]; | |
578 | relocation = (sec->output_section->vma | |
579 | + sec->output_offset | |
580 | + sym->st_value); | |
581 | ||
582 | name = bfd_elf_string_from_elf_section | |
583 | (input_bfd, symtab_hdr->sh_link, sym->st_name); | |
584 | name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name; | |
a75473eb SC |
585 | } |
586 | else | |
587 | { | |
560e09e9 NC |
588 | bfd_boolean unresolved_reloc; |
589 | bfd_boolean warned; | |
590 | ||
b2a8e766 AM |
591 | RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel, |
592 | r_symndx, symtab_hdr, sym_hashes, | |
593 | h, sec, relocation, | |
594 | unresolved_reloc, warned); | |
a75473eb SC |
595 | |
596 | name = h->root.root.string; | |
a75473eb SC |
597 | } |
598 | ||
ab96bf03 AM |
599 | if (sec != NULL && elf_discarded_section (sec)) |
600 | { | |
601 | /* For relocs against symbols from removed linkonce sections, | |
602 | or sections discarded by a linker script, we just want the | |
603 | section contents zeroed. Avoid any special processing. */ | |
604 | _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset); | |
605 | rel->r_info = 0; | |
606 | rel->r_addend = 0; | |
607 | continue; | |
608 | } | |
609 | ||
610 | if (info->relocatable) | |
611 | continue; | |
612 | ||
a75473eb SC |
613 | switch (r_type) |
614 | { | |
615 | case R_IQ2000_HI16: | |
616 | r = iq2000_elf_relocate_hi16 (input_bfd, rel, contents, relocation); | |
617 | break; | |
618 | ||
619 | case R_IQ2000_PC16: | |
620 | rel->r_addend -= 4; | |
621 | /* Fall through. */ | |
622 | ||
623 | default: | |
624 | r = iq2000_final_link_relocate (howto, input_bfd, input_section, | |
625 | contents, rel, relocation); | |
626 | break; | |
627 | } | |
628 | ||
629 | if (r != bfd_reloc_ok) | |
630 | { | |
631 | const char * msg = (const char *) NULL; | |
632 | ||
633 | switch (r) | |
634 | { | |
635 | case bfd_reloc_overflow: | |
636 | r = info->callbacks->reloc_overflow | |
dfeffb9f L |
637 | (info, (h ? &h->root : NULL), name, howto->name, |
638 | (bfd_vma) 0, input_bfd, input_section, rel->r_offset); | |
a75473eb SC |
639 | break; |
640 | ||
641 | case bfd_reloc_undefined: | |
642 | r = info->callbacks->undefined_symbol | |
643 | (info, name, input_bfd, input_section, rel->r_offset, TRUE); | |
644 | break; | |
645 | ||
646 | case bfd_reloc_outofrange: | |
647 | msg = _("internal error: out of range error"); | |
648 | break; | |
649 | ||
650 | case bfd_reloc_notsupported: | |
651 | msg = _("internal error: unsupported relocation error"); | |
652 | break; | |
653 | ||
654 | case bfd_reloc_dangerous: | |
655 | msg = _("internal error: dangerous relocation"); | |
656 | break; | |
657 | ||
658 | default: | |
659 | msg = _("internal error: unknown error"); | |
660 | break; | |
661 | } | |
662 | ||
663 | if (msg) | |
664 | r = info->callbacks->warning | |
665 | (info, msg, name, input_bfd, input_section, rel->r_offset); | |
666 | ||
667 | if (! r) | |
668 | return FALSE; | |
669 | } | |
670 | } | |
671 | ||
672 | return TRUE; | |
673 | } | |
674 | \f | |
675 | ||
a75473eb SC |
676 | /* Return the section that should be marked against GC for a given |
677 | relocation. */ | |
678 | ||
679 | static asection * | |
07adf181 AM |
680 | iq2000_elf_gc_mark_hook (asection *sec, |
681 | struct bfd_link_info *info, | |
682 | Elf_Internal_Rela *rel, | |
683 | struct elf_link_hash_entry *h, | |
684 | Elf_Internal_Sym *sym) | |
a75473eb | 685 | { |
07adf181 AM |
686 | if (h != NULL) |
687 | switch (ELF32_R_TYPE (rel->r_info)) | |
688 | { | |
689 | case R_IQ2000_GNU_VTINHERIT: | |
690 | case R_IQ2000_GNU_VTENTRY: | |
691 | return NULL; | |
692 | } | |
693 | ||
694 | return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym); | |
a75473eb SC |
695 | } |
696 | ||
697 | \f | |
698 | /* Return the MACH for an e_flags value. */ | |
699 | ||
700 | static int | |
58852c27 | 701 | elf32_iq2000_machine (bfd *abfd) |
a75473eb SC |
702 | { |
703 | switch (elf_elfheader (abfd)->e_flags & EF_IQ2000_CPU_MASK) | |
704 | { | |
58852c27 NC |
705 | case EF_IQ2000_CPU_IQ10: |
706 | return bfd_mach_iq10; | |
a75473eb | 707 | |
58852c27 NC |
708 | case EF_IQ2000_CPU_IQ2000: |
709 | default: | |
710 | return bfd_mach_iq2000; | |
711 | } | |
a75473eb SC |
712 | } |
713 | ||
714 | \f | |
715 | /* Function to set the ELF flag bits. */ | |
716 | ||
717 | static bfd_boolean | |
58852c27 | 718 | iq2000_elf_set_private_flags (bfd *abfd, flagword flags) |
a75473eb SC |
719 | { |
720 | elf_elfheader (abfd)->e_flags = flags; | |
721 | elf_flags_init (abfd) = TRUE; | |
722 | return TRUE; | |
723 | } | |
724 | ||
725 | /* Copy backend specific data from one object module to another. */ | |
726 | ||
727 | static bfd_boolean | |
58852c27 | 728 | iq2000_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd) |
a75473eb SC |
729 | { |
730 | if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour | |
731 | || bfd_get_flavour (obfd) != bfd_target_elf_flavour) | |
732 | return TRUE; | |
733 | ||
734 | BFD_ASSERT (!elf_flags_init (obfd) | |
735 | || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags); | |
736 | ||
737 | elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags; | |
738 | elf_flags_init (obfd) = TRUE; | |
739 | return TRUE; | |
740 | } | |
741 | ||
58852c27 NC |
742 | /* Merge backend specific data from an object |
743 | file to the output object file when linking. */ | |
a75473eb SC |
744 | |
745 | static bfd_boolean | |
58852c27 | 746 | iq2000_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd) |
a75473eb SC |
747 | { |
748 | flagword old_flags, old_partial; | |
749 | flagword new_flags, new_partial; | |
750 | bfd_boolean error = FALSE; | |
751 | char new_opt[80]; | |
752 | char old_opt[80]; | |
753 | ||
754 | new_opt[0] = old_opt[0] = '\0'; | |
755 | new_flags = elf_elfheader (ibfd)->e_flags; | |
756 | old_flags = elf_elfheader (obfd)->e_flags; | |
757 | ||
a75473eb SC |
758 | if (!elf_flags_init (obfd)) |
759 | { | |
760 | /* First call, no flags set. */ | |
761 | elf_flags_init (obfd) = TRUE; | |
762 | elf_elfheader (obfd)->e_flags = new_flags; | |
763 | } | |
764 | ||
58852c27 | 765 | else if (new_flags != old_flags) |
a75473eb | 766 | { |
58852c27 NC |
767 | /* Warn if different cpu is used, but allow a |
768 | specific cpu to override the generic cpu. */ | |
a75473eb SC |
769 | new_partial = (new_flags & EF_IQ2000_CPU_MASK); |
770 | old_partial = (old_flags & EF_IQ2000_CPU_MASK); | |
a75473eb | 771 | |
58852c27 | 772 | if (new_partial != old_partial) |
a75473eb SC |
773 | { |
774 | switch (new_partial) | |
775 | { | |
58852c27 NC |
776 | case EF_IQ2000_CPU_IQ10: |
777 | strcat (new_opt, " -m10"); | |
778 | break; | |
779 | ||
780 | default: | |
781 | case EF_IQ2000_CPU_IQ2000: | |
782 | strcat (new_opt, " -m2000"); | |
783 | break; | |
a75473eb SC |
784 | } |
785 | ||
786 | switch (old_partial) | |
787 | { | |
58852c27 NC |
788 | case EF_IQ2000_CPU_IQ10: |
789 | strcat (old_opt, " -m10"); | |
790 | break; | |
791 | ||
792 | default: | |
793 | case EF_IQ2000_CPU_IQ2000: | |
794 | strcat (old_opt, " -m2000"); | |
795 | break; | |
a75473eb SC |
796 | } |
797 | } | |
798 | ||
799 | /* Print out any mismatches from above. */ | |
800 | if (new_opt[0]) | |
801 | { | |
802 | error = TRUE; | |
58852c27 | 803 | _bfd_error_handler |
a75473eb SC |
804 | (_("%s: compiled with %s and linked with modules compiled with %s"), |
805 | bfd_get_filename (ibfd), new_opt, old_opt); | |
806 | } | |
807 | ||
808 | new_flags &= ~ EF_IQ2000_ALL_FLAGS; | |
809 | old_flags &= ~ EF_IQ2000_ALL_FLAGS; | |
810 | ||
811 | /* Warn about any other mismatches. */ | |
812 | if (new_flags != old_flags) | |
813 | { | |
814 | error = TRUE; | |
58852c27 NC |
815 | |
816 | _bfd_error_handler | |
a75473eb SC |
817 | (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"), |
818 | bfd_get_filename (ibfd), (long)new_flags, (long)old_flags); | |
819 | } | |
820 | } | |
821 | ||
822 | if (error) | |
823 | bfd_set_error (bfd_error_bad_value); | |
824 | ||
825 | return !error; | |
826 | } | |
827 | ||
828 | \f | |
829 | static bfd_boolean | |
58852c27 | 830 | iq2000_elf_print_private_bfd_data (bfd *abfd, void * ptr) |
a75473eb SC |
831 | { |
832 | FILE *file = (FILE *) ptr; | |
833 | flagword flags; | |
834 | ||
835 | BFD_ASSERT (abfd != NULL && ptr != NULL); | |
836 | ||
837 | /* Print normal ELF private data. */ | |
838 | _bfd_elf_print_private_bfd_data (abfd, ptr); | |
839 | ||
840 | flags = elf_elfheader (abfd)->e_flags; | |
841 | fprintf (file, _("private flags = 0x%lx:"), (long)flags); | |
842 | ||
843 | switch (flags & EF_IQ2000_CPU_MASK) | |
844 | { | |
58852c27 NC |
845 | case EF_IQ2000_CPU_IQ10: |
846 | fprintf (file, " -m10"); | |
847 | break; | |
848 | case EF_IQ2000_CPU_IQ2000: | |
849 | fprintf (file, " -m2000"); | |
850 | break; | |
851 | default: | |
852 | break; | |
a75473eb SC |
853 | } |
854 | ||
855 | fputc ('\n', file); | |
60d8b524 | 856 | return TRUE; |
a75473eb SC |
857 | } |
858 | ||
859 | static | |
860 | bfd_boolean | |
58852c27 | 861 | iq2000_elf_object_p (bfd *abfd) |
a75473eb | 862 | { |
a75473eb SC |
863 | bfd_default_set_arch_mach (abfd, bfd_arch_iq2000, |
864 | elf32_iq2000_machine (abfd)); | |
865 | return TRUE; | |
866 | } | |
867 | ||
868 | \f | |
869 | #define ELF_ARCH bfd_arch_iq2000 | |
870 | #define ELF_MACHINE_CODE EM_IQ2000 | |
871 | #define ELF_MAXPAGESIZE 0x1000 | |
872 | ||
873 | #define TARGET_BIG_SYM bfd_elf32_iq2000_vec | |
874 | #define TARGET_BIG_NAME "elf32-iq2000" | |
875 | ||
876 | #define elf_info_to_howto_rel NULL | |
877 | #define elf_info_to_howto iq2000_info_to_howto_rela | |
878 | #define elf_backend_relocate_section iq2000_elf_relocate_section | |
879 | #define elf_backend_gc_mark_hook iq2000_elf_gc_mark_hook | |
a75473eb SC |
880 | #define elf_backend_check_relocs iq2000_elf_check_relocs |
881 | #define elf_backend_object_p iq2000_elf_object_p | |
882 | #define elf_backend_rela_normal 1 | |
883 | ||
884 | #define elf_backend_can_gc_sections 1 | |
885 | ||
886 | #define bfd_elf32_bfd_reloc_type_lookup iq2000_reloc_type_lookup | |
157090f7 | 887 | #define bfd_elf32_bfd_reloc_name_lookup iq2000_reloc_name_lookup |
a75473eb SC |
888 | #define bfd_elf32_bfd_set_private_flags iq2000_elf_set_private_flags |
889 | #define bfd_elf32_bfd_copy_private_bfd_data iq2000_elf_copy_private_bfd_data | |
890 | #define bfd_elf32_bfd_merge_private_bfd_data iq2000_elf_merge_private_bfd_data | |
891 | #define bfd_elf32_bfd_print_private_bfd_data iq2000_elf_print_private_bfd_data | |
892 | ||
893 | #include "elf32-target.h" |