Commit | Line | Data |
---|---|---|
49f58d10 | 1 | /* M16C/M32C specific support for 32-bit ELF. |
6772dd07 | 2 | Copyright (C) 2005, 2006 |
49f58d10 JB |
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 | #include "bfd.h" | |
22 | #include "sysdep.h" | |
23 | #include "libbfd.h" | |
24 | #include "elf-bfd.h" | |
25 | #include "elf/m32c.h" | |
26 | #include "libiberty.h" | |
27 | ||
28 | /* Forward declarations. */ | |
29 | static reloc_howto_type * m32c_reloc_type_lookup | |
30 | (bfd *, bfd_reloc_code_real_type); | |
31 | static void m32c_info_to_howto_rela | |
32 | (bfd *, arelent *, Elf_Internal_Rela *); | |
33 | static bfd_boolean m32c_elf_relocate_section | |
34 | (bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **); | |
35 | static bfd_boolean m32c_elf_gc_sweep_hook | |
36 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
37 | static asection * m32c_elf_gc_mark_hook | |
38 | (asection *, struct bfd_link_info *, Elf_Internal_Rela *, struct elf_link_hash_entry *, Elf_Internal_Sym *); | |
39 | static bfd_boolean m32c_elf_check_relocs | |
40 | (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *); | |
41 | static bfd_boolean m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int); | |
42 | #ifdef DEBUG | |
43 | static char * m32c_get_reloc (long reloc); | |
44 | #endif | |
45 | static bfd_boolean m32c_elf_relax_section | |
46 | (bfd *abfd, asection *sec, struct bfd_link_info *link_info, bfd_boolean *again); | |
47 | ||
48 | ||
49 | static reloc_howto_type m32c_elf_howto_table [] = | |
50 | { | |
51 | /* This reloc does nothing. */ | |
52 | HOWTO (R_M32C_NONE, /* type */ | |
53 | 0, /* rightshift */ | |
54 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
55 | 32, /* bitsize */ | |
56 | FALSE, /* pc_relative */ | |
57 | 0, /* bitpos */ | |
58 | complain_overflow_bitfield, /* complain_on_overflow */ | |
59 | bfd_elf_generic_reloc, /* special_function */ | |
60 | "R_M32C_NONE", /* name */ | |
61 | FALSE, /* partial_inplace */ | |
62 | 0, /* src_mask */ | |
63 | 0, /* dst_mask */ | |
64 | FALSE), /* pcrel_offset */ | |
65 | ||
66 | HOWTO (R_M32C_16, /* type */ | |
67 | 0, /* rightshift */ | |
68 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
69 | 16, /* bitsize */ | |
70 | FALSE, /* pc_relative */ | |
71 | 0, /* bitpos */ | |
72 | complain_overflow_bitfield, /* complain_on_overflow */ | |
73 | bfd_elf_generic_reloc, /* special_function */ | |
74 | "R_M32C_16", /* name */ | |
75 | FALSE, /* partial_inplace */ | |
76 | 0, /* src_mask */ | |
6772dd07 | 77 | 0xffff, /* dst_mask */ |
49f58d10 JB |
78 | FALSE), /* pcrel_offset */ |
79 | ||
80 | HOWTO (R_M32C_24, /* type */ | |
81 | 0, /* rightshift */ | |
82 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
83 | 24, /* bitsize */ | |
84 | FALSE, /* pc_relative */ | |
85 | 0, /* bitpos */ | |
86 | complain_overflow_bitfield, /* complain_on_overflow */ | |
87 | bfd_elf_generic_reloc, /* special_function */ | |
88 | "R_M32C_24", /* name */ | |
89 | FALSE, /* partial_inplace */ | |
90 | 0, /* src_mask */ | |
6772dd07 | 91 | 0xffffff, /* dst_mask */ |
49f58d10 JB |
92 | FALSE), /* pcrel_offset */ |
93 | ||
94 | HOWTO (R_M32C_32, /* type */ | |
95 | 0, /* rightshift */ | |
96 | 2, /* size (0 = byte, 1 = short, 2 = long) */ | |
97 | 32, /* bitsize */ | |
98 | FALSE, /* pc_relative */ | |
99 | 0, /* bitpos */ | |
100 | complain_overflow_bitfield, /* complain_on_overflow */ | |
101 | bfd_elf_generic_reloc, /* special_function */ | |
102 | "R_M32C_32", /* name */ | |
103 | FALSE, /* partial_inplace */ | |
104 | 0, /* src_mask */ | |
105 | 0xffffffff, /* dst_mask */ | |
106 | FALSE), /* pcrel_offset */ | |
107 | ||
108 | HOWTO (R_M32C_8_PCREL, /* type */ | |
109 | 0, /* rightshift */ | |
110 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
111 | 8, /* bitsize */ | |
112 | TRUE, /* pc_relative */ | |
113 | 0, /* bitpos */ | |
114 | complain_overflow_signed, /* complain_on_overflow */ | |
115 | bfd_elf_generic_reloc, /* special_function */ | |
116 | "R_M32C_8_PCREL", /* name */ | |
117 | FALSE, /* partial_inplace */ | |
118 | 0, /* src_mask */ | |
6772dd07 | 119 | 0xff, /* dst_mask */ |
49f58d10 JB |
120 | TRUE), /* pcrel_offset */ |
121 | ||
122 | HOWTO (R_M32C_16_PCREL, /* type */ | |
123 | 0, /* rightshift */ | |
124 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
125 | 16, /* bitsize */ | |
126 | TRUE, /* pc_relative */ | |
127 | 0, /* bitpos */ | |
128 | complain_overflow_signed, /* complain_on_overflow */ | |
129 | bfd_elf_generic_reloc, /* special_function */ | |
130 | "R_M32C_16_PCREL", /* name */ | |
131 | FALSE, /* partial_inplace */ | |
132 | 0, /* src_mask */ | |
6772dd07 | 133 | 0xffff, /* dst_mask */ |
49f58d10 | 134 | TRUE), /* pcrel_offset */ |
fd54057a DD |
135 | |
136 | HOWTO (R_M32C_8, /* type */ | |
137 | 0, /* rightshift */ | |
138 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
139 | 8, /* bitsize */ | |
140 | FALSE, /* pc_relative */ | |
141 | 0, /* bitpos */ | |
142 | complain_overflow_unsigned, /* complain_on_overflow */ | |
143 | bfd_elf_generic_reloc, /* special_function */ | |
144 | "R_M32C_8", /* name */ | |
145 | FALSE, /* partial_inplace */ | |
146 | 0, /* src_mask */ | |
6772dd07 | 147 | 0xff, /* dst_mask */ |
fd54057a DD |
148 | FALSE), /* pcrel_offset */ |
149 | ||
150 | HOWTO (R_M32C_LO16, /* type */ | |
151 | 0, /* rightshift */ | |
152 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
153 | 16, /* bitsize */ | |
154 | FALSE, /* pc_relative */ | |
155 | 0, /* bitpos */ | |
156 | complain_overflow_dont, /* complain_on_overflow */ | |
157 | bfd_elf_generic_reloc, /* special_function */ | |
158 | "R_M32C_LO16", /* name */ | |
159 | FALSE, /* partial_inplace */ | |
160 | 0, /* src_mask */ | |
6772dd07 | 161 | 0xffff, /* dst_mask */ |
fd54057a DD |
162 | FALSE), /* pcrel_offset */ |
163 | ||
164 | HOWTO (R_M32C_HI8, /* type */ | |
165 | 0, /* rightshift */ | |
166 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
167 | 8, /* bitsize */ | |
168 | FALSE, /* pc_relative */ | |
169 | 0, /* bitpos */ | |
170 | complain_overflow_dont, /* complain_on_overflow */ | |
171 | bfd_elf_generic_reloc, /* special_function */ | |
172 | "R_M32C_HI8", /* name */ | |
173 | FALSE, /* partial_inplace */ | |
174 | 0, /* src_mask */ | |
6772dd07 | 175 | 0xff, /* dst_mask */ |
fd54057a DD |
176 | FALSE), /* pcrel_offset */ |
177 | ||
178 | HOWTO (R_M32C_HI16, /* type */ | |
179 | 0, /* rightshift */ | |
180 | 1, /* size (0 = byte, 1 = short, 2 = long) */ | |
181 | 16, /* bitsize */ | |
182 | FALSE, /* pc_relative */ | |
183 | 0, /* bitpos */ | |
184 | complain_overflow_dont, /* complain_on_overflow */ | |
185 | bfd_elf_generic_reloc, /* special_function */ | |
186 | "R_M32C_HI16", /* name */ | |
187 | FALSE, /* partial_inplace */ | |
188 | 0, /* src_mask */ | |
6772dd07 | 189 | 0xffff, /* dst_mask */ |
fd54057a | 190 | FALSE), /* pcrel_offset */ |
6772dd07 DD |
191 | |
192 | HOWTO (R_M32C_RL_JUMP, /* type */ | |
193 | 0, /* rightshift */ | |
194 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
195 | 0, /* bitsize */ | |
196 | FALSE, /* pc_relative */ | |
197 | 0, /* bitpos */ | |
198 | complain_overflow_signed, /* complain_on_overflow */ | |
199 | bfd_elf_generic_reloc, /* special_function */ | |
200 | "R_M32C_RL_JUMP", /* name */ | |
201 | FALSE, /* partial_inplace */ | |
202 | 0, /* src_mask */ | |
203 | 0, /* dst_mask */ | |
204 | FALSE), /* pcrel_offset */ | |
205 | ||
206 | HOWTO (R_M32C_RL_1ADDR, /* type */ | |
207 | 0, /* rightshift */ | |
208 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
209 | 0, /* bitsize */ | |
210 | FALSE, /* pc_relative */ | |
211 | 0, /* bitpos */ | |
212 | complain_overflow_signed, /* complain_on_overflow */ | |
213 | bfd_elf_generic_reloc, /* special_function */ | |
214 | "R_M32C_RL_1ADDR", /* name */ | |
215 | FALSE, /* partial_inplace */ | |
216 | 0, /* src_mask */ | |
217 | 0, /* dst_mask */ | |
218 | FALSE), /* pcrel_offset */ | |
219 | ||
220 | HOWTO (R_M32C_RL_2ADDR, /* type */ | |
221 | 0, /* rightshift */ | |
222 | 0, /* size (0 = byte, 1 = short, 2 = long) */ | |
223 | 0, /* bitsize */ | |
224 | FALSE, /* pc_relative */ | |
225 | 0, /* bitpos */ | |
226 | complain_overflow_signed, /* complain_on_overflow */ | |
227 | bfd_elf_generic_reloc, /* special_function */ | |
228 | "R_M32C_RL_2ADDR", /* name */ | |
229 | FALSE, /* partial_inplace */ | |
230 | 0, /* src_mask */ | |
231 | 0, /* dst_mask */ | |
232 | FALSE), /* pcrel_offset */ | |
233 | ||
49f58d10 JB |
234 | }; |
235 | \f | |
236 | /* Map BFD reloc types to M32C ELF reloc types. */ | |
237 | ||
238 | struct m32c_reloc_map | |
239 | { | |
240 | bfd_reloc_code_real_type bfd_reloc_val; | |
241 | unsigned int m32c_reloc_val; | |
242 | }; | |
243 | ||
244 | static const struct m32c_reloc_map m32c_reloc_map [] = | |
245 | { | |
246 | { BFD_RELOC_NONE, R_M32C_NONE }, | |
247 | { BFD_RELOC_16, R_M32C_16 }, | |
248 | { BFD_RELOC_24, R_M32C_24 }, | |
249 | { BFD_RELOC_32, R_M32C_32 }, | |
250 | { BFD_RELOC_8_PCREL, R_M32C_8_PCREL }, | |
fd54057a DD |
251 | { BFD_RELOC_16_PCREL, R_M32C_16_PCREL }, |
252 | { BFD_RELOC_8, R_M32C_8 }, | |
253 | { BFD_RELOC_LO16, R_M32C_LO16 }, | |
254 | { BFD_RELOC_HI16, R_M32C_HI16 }, | |
6772dd07 DD |
255 | { BFD_RELOC_M32C_HI8, R_M32C_HI8 }, |
256 | { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP }, | |
257 | { BFD_RELOC_M32C_RL_1ADDR, R_M32C_RL_1ADDR }, | |
258 | { BFD_RELOC_M32C_RL_2ADDR, R_M32C_RL_2ADDR } | |
49f58d10 JB |
259 | }; |
260 | ||
261 | static reloc_howto_type * | |
262 | m32c_reloc_type_lookup | |
263 | (bfd * abfd ATTRIBUTE_UNUSED, | |
264 | bfd_reloc_code_real_type code) | |
265 | { | |
266 | unsigned int i; | |
267 | ||
268 | for (i = ARRAY_SIZE (m32c_reloc_map); --i;) | |
269 | if (m32c_reloc_map [i].bfd_reloc_val == code) | |
270 | return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val]; | |
271 | ||
272 | return NULL; | |
273 | } | |
274 | ||
275 | /* Set the howto pointer for an M32C ELF reloc. */ | |
276 | ||
277 | static void | |
278 | m32c_info_to_howto_rela | |
279 | (bfd * abfd ATTRIBUTE_UNUSED, | |
280 | arelent * cache_ptr, | |
281 | Elf_Internal_Rela * dst) | |
282 | { | |
283 | unsigned int r_type; | |
284 | ||
285 | r_type = ELF32_R_TYPE (dst->r_info); | |
286 | BFD_ASSERT (r_type < (unsigned int) R_M32C_max); | |
287 | cache_ptr->howto = & m32c_elf_howto_table [r_type]; | |
288 | } | |
289 | ||
290 | \f | |
291 | ||
292 | /* Relocate an M32C ELF section. | |
293 | There is some attempt to make this function usable for many architectures, | |
294 | both USE_REL and USE_RELA ['twould be nice if such a critter existed], | |
295 | if only to serve as a learning tool. | |
296 | ||
297 | The RELOCATE_SECTION function is called by the new ELF backend linker | |
298 | to handle the relocations for a section. | |
299 | ||
300 | The relocs are always passed as Rela structures; if the section | |
301 | actually uses Rel structures, the r_addend field will always be | |
302 | zero. | |
303 | ||
304 | This function is responsible for adjusting the section contents as | |
305 | necessary, and (if using Rela relocs and generating a relocatable | |
306 | output file) adjusting the reloc addend as necessary. | |
307 | ||
308 | This function does not have to worry about setting the reloc | |
309 | address or the reloc symbol index. | |
310 | ||
311 | LOCAL_SYMS is a pointer to the swapped in local symbols. | |
312 | ||
313 | LOCAL_SECTIONS is an array giving the section in the input file | |
314 | corresponding to the st_shndx field of each local symbol. | |
315 | ||
316 | The global hash table entry for the global symbols can be found | |
317 | via elf_sym_hashes (input_bfd). | |
318 | ||
319 | When generating relocatable output, this function must handle | |
320 | STB_LOCAL/STT_SECTION symbols specially. The output symbol is | |
321 | going to be the section symbol corresponding to the output | |
322 | section, which means that the addend must be adjusted | |
323 | accordingly. */ | |
324 | ||
325 | static bfd_boolean | |
326 | m32c_elf_relocate_section | |
327 | (bfd * output_bfd ATTRIBUTE_UNUSED, | |
328 | struct bfd_link_info * info, | |
329 | bfd * input_bfd, | |
330 | asection * input_section, | |
331 | bfd_byte * contents, | |
332 | Elf_Internal_Rela * relocs, | |
333 | Elf_Internal_Sym * local_syms, | |
334 | asection ** local_sections) | |
335 | { | |
336 | Elf_Internal_Shdr * symtab_hdr; | |
337 | struct elf_link_hash_entry ** sym_hashes; | |
338 | Elf_Internal_Rela * rel; | |
339 | Elf_Internal_Rela * relend; | |
340 | bfd *dynobj; | |
341 | asection *splt; | |
342 | ||
343 | symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr; | |
344 | sym_hashes = elf_sym_hashes (input_bfd); | |
345 | relend = relocs + input_section->reloc_count; | |
346 | ||
347 | dynobj = elf_hash_table (info)->dynobj; | |
348 | splt = NULL; | |
349 | if (dynobj != NULL) | |
350 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
351 | ||
352 | for (rel = relocs; rel < relend; rel ++) | |
353 | { | |
354 | reloc_howto_type * howto; | |
355 | unsigned long r_symndx; | |
356 | Elf_Internal_Sym * sym; | |
357 | asection * sec; | |
358 | struct elf_link_hash_entry * h; | |
359 | bfd_vma relocation; | |
360 | bfd_reloc_status_type r; | |
361 | const char * name = NULL; | |
362 | int r_type; | |
363 | ||
364 | r_type = ELF32_R_TYPE (rel->r_info); | |
6772dd07 DD |
365 | |
366 | /* These are only used for relaxing; we don't actually relocate | |
367 | anything with them, so skip them. */ | |
368 | if (r_type == R_M32C_RL_JUMP | |
369 | || r_type == R_M32C_RL_1ADDR | |
370 | || r_type == R_M32C_RL_2ADDR) | |
371 | continue; | |
49f58d10 JB |
372 | |
373 | r_symndx = ELF32_R_SYM (rel->r_info); | |
374 | ||
375 | if (info->relocatable) | |
376 | { | |
377 | /* This is a relocatable link. We don't have to change | |
378 | anything, unless the reloc is against a section symbol, | |
379 | in which case we have to adjust according to where the | |
380 | section symbol winds up in the output section. */ | |
381 | if (r_symndx < symtab_hdr->sh_info) | |
382 | { | |
383 | sym = local_syms + r_symndx; | |
384 | ||
385 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
386 | { | |
387 | sec = local_sections [r_symndx]; | |
388 | rel->r_addend += sec->output_offset + sym->st_value; | |
389 | } | |
390 | } | |
391 | ||
392 | continue; | |
393 | } | |
394 | ||
395 | /* This is a final link. */ | |
396 | howto = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info); | |
397 | h = NULL; | |
398 | sym = NULL; | |
399 | sec = NULL; | |
6772dd07 | 400 | |
49f58d10 JB |
401 | if (r_symndx < symtab_hdr->sh_info) |
402 | { | |
403 | sym = local_syms + r_symndx; | |
404 | sec = local_sections [r_symndx]; | |
405 | relocation = (sec->output_section->vma | |
406 | + sec->output_offset | |
407 | + sym->st_value); | |
408 | ||
409 | name = bfd_elf_string_from_elf_section | |
410 | (input_bfd, symtab_hdr->sh_link, sym->st_name); | |
6772dd07 | 411 | name = (sym->st_name == 0) ? bfd_section_name (input_bfd, sec) : name; |
49f58d10 JB |
412 | } |
413 | else | |
414 | { | |
415 | h = sym_hashes [r_symndx - symtab_hdr->sh_info]; | |
416 | ||
417 | while (h->root.type == bfd_link_hash_indirect | |
418 | || h->root.type == bfd_link_hash_warning) | |
419 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
420 | ||
421 | name = h->root.root.string; | |
422 | ||
423 | if (h->root.type == bfd_link_hash_defined | |
424 | || h->root.type == bfd_link_hash_defweak) | |
425 | { | |
426 | sec = h->root.u.def.section; | |
427 | relocation = (h->root.u.def.value | |
428 | + sec->output_section->vma | |
429 | + sec->output_offset); | |
430 | } | |
431 | else if (h->root.type == bfd_link_hash_undefweak) | |
432 | { | |
433 | relocation = 0; | |
434 | } | |
435 | else | |
436 | { | |
437 | if (! ((*info->callbacks->undefined_symbol) | |
438 | (info, h->root.root.string, input_bfd, | |
439 | input_section, rel->r_offset, TRUE))) | |
440 | return FALSE; | |
441 | relocation = 0; | |
442 | } | |
443 | } | |
444 | ||
445 | switch (ELF32_R_TYPE (rel->r_info)) | |
446 | { | |
447 | case R_M32C_16: | |
448 | { | |
449 | bfd_vma *plt_offset; | |
450 | ||
451 | if (h != NULL) | |
452 | plt_offset = &h->plt.offset; | |
453 | else | |
454 | plt_offset = elf_local_got_offsets (input_bfd) + r_symndx; | |
455 | ||
456 | /* printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)", | |
457 | relocation, *plt_offset);*/ | |
458 | if (relocation <= 0xffff) | |
459 | { | |
460 | /* If the symbol is in range for a 16-bit address, we should | |
461 | have deallocated the plt entry in relax_section. */ | |
462 | BFD_ASSERT (*plt_offset == (bfd_vma) -1); | |
463 | } | |
464 | else | |
465 | { | |
466 | /* If the symbol is out of range for a 16-bit address, | |
467 | we must have allocated a plt entry. */ | |
468 | BFD_ASSERT (*plt_offset != (bfd_vma) -1); | |
469 | ||
470 | /* If this is the first time we've processed this symbol, | |
471 | fill in the plt entry with the correct symbol address. */ | |
472 | if ((*plt_offset & 1) == 0) | |
473 | { | |
474 | unsigned int x; | |
475 | ||
476 | x = 0x000000fc; /* jmpf */ | |
477 | x |= (relocation << 8) & 0xffffff00; | |
478 | bfd_put_32 (input_bfd, x, splt->contents + *plt_offset); | |
479 | *plt_offset |= 1; | |
480 | } | |
481 | ||
482 | relocation = (splt->output_section->vma | |
483 | + splt->output_offset | |
484 | + (*plt_offset & -2)); | |
485 | } | |
486 | } | |
487 | break; | |
fd54057a DD |
488 | |
489 | case R_M32C_HI8: | |
490 | case R_M32C_HI16: | |
491 | relocation >>= 16; | |
492 | break; | |
49f58d10 JB |
493 | } |
494 | ||
6772dd07 DD |
495 | #if 0 |
496 | printf("relocate %s at %06lx relocation %06lx addend %ld ", | |
497 | m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name, | |
498 | rel->r_offset, relocation, rel->r_addend); | |
499 | { | |
500 | int i; | |
501 | for (i=0; i<4; i++) | |
502 | printf(" %02x", contents[rel->r_offset+i]); | |
503 | printf("\n"); | |
504 | } | |
505 | #endif | |
49f58d10 JB |
506 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
507 | contents, rel->r_offset, relocation, | |
508 | rel->r_addend); | |
509 | ||
510 | if (r != bfd_reloc_ok) | |
511 | { | |
512 | const char * msg = (const char *) NULL; | |
513 | ||
514 | switch (r) | |
515 | { | |
516 | case bfd_reloc_overflow: | |
517 | r = info->callbacks->reloc_overflow | |
518 | (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0, | |
519 | input_bfd, input_section, rel->r_offset); | |
520 | break; | |
521 | ||
522 | case bfd_reloc_undefined: | |
523 | r = info->callbacks->undefined_symbol | |
524 | (info, name, input_bfd, input_section, rel->r_offset, | |
525 | TRUE); | |
526 | break; | |
527 | ||
528 | case bfd_reloc_outofrange: | |
529 | msg = _("internal error: out of range error"); | |
530 | break; | |
531 | ||
532 | case bfd_reloc_notsupported: | |
533 | msg = _("internal error: unsupported relocation error"); | |
534 | break; | |
535 | ||
536 | case bfd_reloc_dangerous: | |
537 | msg = _("internal error: dangerous relocation"); | |
538 | break; | |
539 | ||
540 | default: | |
541 | msg = _("internal error: unknown error"); | |
542 | break; | |
543 | } | |
544 | ||
545 | if (msg) | |
546 | r = info->callbacks->warning | |
547 | (info, msg, name, input_bfd, input_section, rel->r_offset); | |
548 | ||
549 | if (! r) | |
550 | return FALSE; | |
551 | } | |
552 | } | |
553 | ||
554 | return TRUE; | |
555 | } | |
556 | \f | |
557 | /* Return the section that should be marked against GC for a given | |
558 | relocation. */ | |
559 | ||
560 | static asection * | |
561 | m32c_elf_gc_mark_hook | |
562 | (asection * sec, | |
563 | struct bfd_link_info * info ATTRIBUTE_UNUSED, | |
564 | Elf_Internal_Rela * rel, | |
565 | struct elf_link_hash_entry * h, | |
566 | Elf_Internal_Sym * sym) | |
567 | { | |
568 | if (h != NULL) | |
569 | { | |
570 | switch (ELF32_R_TYPE (rel->r_info)) | |
571 | { | |
572 | default: | |
573 | switch (h->root.type) | |
574 | { | |
575 | case bfd_link_hash_defined: | |
576 | case bfd_link_hash_defweak: | |
577 | return h->root.u.def.section; | |
578 | ||
579 | case bfd_link_hash_common: | |
580 | return h->root.u.c.p->section; | |
581 | ||
582 | default: | |
583 | break; | |
584 | } | |
585 | } | |
586 | } | |
587 | else | |
588 | { | |
589 | if (!(elf_bad_symtab (sec->owner) | |
590 | && ELF_ST_BIND (sym->st_info) != STB_LOCAL) | |
591 | && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE) | |
592 | && sym->st_shndx != SHN_COMMON)) | |
593 | { | |
594 | return bfd_section_from_elf_index (sec->owner, sym->st_shndx); | |
595 | } | |
596 | } | |
597 | ||
598 | return NULL; | |
599 | } | |
600 | ||
601 | /* Update the got entry reference counts for the section being removed. */ | |
602 | ||
603 | static bfd_boolean | |
604 | m32c_elf_gc_sweep_hook | |
605 | (bfd * abfd ATTRIBUTE_UNUSED, | |
606 | struct bfd_link_info * info ATTRIBUTE_UNUSED, | |
607 | asection * sec ATTRIBUTE_UNUSED, | |
608 | const Elf_Internal_Rela * relocs ATTRIBUTE_UNUSED) | |
609 | { | |
610 | return TRUE; | |
611 | } | |
612 | ||
613 | /* We support 16-bit pointers to code above 64k by generating a thunk | |
614 | below 64k containing a JMP instruction to the final address. */ | |
615 | ||
616 | static bfd_boolean | |
617 | m32c_elf_check_relocs | |
618 | (bfd * abfd, | |
619 | struct bfd_link_info * info, | |
620 | asection * sec, | |
621 | const Elf_Internal_Rela * relocs) | |
622 | { | |
623 | Elf_Internal_Shdr * symtab_hdr; | |
624 | struct elf_link_hash_entry ** sym_hashes; | |
625 | struct elf_link_hash_entry ** sym_hashes_end; | |
626 | const Elf_Internal_Rela * rel; | |
627 | const Elf_Internal_Rela * rel_end; | |
628 | bfd_vma *local_plt_offsets; | |
629 | asection *splt; | |
630 | bfd *dynobj; | |
631 | ||
632 | if (info->relocatable) | |
633 | return TRUE; | |
634 | ||
635 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
636 | sym_hashes = elf_sym_hashes (abfd); | |
637 | local_plt_offsets = elf_local_got_offsets (abfd); | |
638 | splt = NULL; | |
639 | dynobj = elf_hash_table(info)->dynobj; | |
640 | ||
641 | sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym); | |
642 | if (!elf_bad_symtab (abfd)) | |
643 | sym_hashes_end -= symtab_hdr->sh_info; | |
644 | ||
645 | rel_end = relocs + sec->reloc_count; | |
646 | for (rel = relocs; rel < rel_end; rel++) | |
647 | { | |
648 | struct elf_link_hash_entry *h; | |
649 | unsigned long r_symndx; | |
650 | bfd_vma *offset; | |
651 | ||
652 | r_symndx = ELF32_R_SYM (rel->r_info); | |
653 | if (r_symndx < symtab_hdr->sh_info) | |
654 | h = NULL; | |
655 | else | |
656 | { | |
657 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
658 | while (h->root.type == bfd_link_hash_indirect | |
659 | || h->root.type == bfd_link_hash_warning) | |
660 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
661 | } | |
662 | ||
663 | switch (ELF32_R_TYPE (rel->r_info)) | |
664 | { | |
665 | /* This relocation describes a 16-bit pointer to a function. | |
666 | We may need to allocate a thunk in low memory; reserve memory | |
667 | for it now. */ | |
668 | case R_M32C_16: | |
669 | if (dynobj == NULL) | |
670 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
671 | if (splt == NULL) | |
672 | { | |
673 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
674 | if (splt == NULL) | |
675 | { | |
676 | splt = bfd_make_section (dynobj, ".plt"); | |
677 | if (splt == NULL | |
678 | || ! bfd_set_section_flags (dynobj, splt, | |
679 | (SEC_ALLOC | |
680 | | SEC_LOAD | |
681 | | SEC_HAS_CONTENTS | |
682 | | SEC_IN_MEMORY | |
683 | | SEC_LINKER_CREATED | |
684 | | SEC_READONLY | |
685 | | SEC_CODE)) | |
686 | || ! bfd_set_section_alignment (dynobj, splt, 1)) | |
687 | return FALSE; | |
688 | } | |
689 | } | |
690 | ||
691 | if (h != NULL) | |
692 | offset = &h->plt.offset; | |
693 | else | |
694 | { | |
695 | if (local_plt_offsets == NULL) | |
696 | { | |
697 | size_t size; | |
698 | unsigned int i; | |
699 | ||
700 | size = symtab_hdr->sh_info * sizeof (bfd_vma); | |
701 | local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size); | |
702 | if (local_plt_offsets == NULL) | |
703 | return FALSE; | |
704 | elf_local_got_offsets (abfd) = local_plt_offsets; | |
705 | ||
706 | for (i = 0; i < symtab_hdr->sh_info; i++) | |
707 | local_plt_offsets[i] = (bfd_vma) -1; | |
708 | } | |
709 | offset = &local_plt_offsets[r_symndx]; | |
710 | } | |
711 | ||
712 | if (*offset == (bfd_vma) -1) | |
713 | { | |
714 | *offset = splt->size; | |
715 | splt->size += 4; | |
716 | } | |
717 | break; | |
718 | } | |
719 | } | |
720 | ||
721 | return TRUE; | |
722 | } | |
723 | ||
724 | /* This must exist if dynobj is ever set. */ | |
725 | ||
726 | static bfd_boolean | |
727 | m32c_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED, | |
728 | struct bfd_link_info *info) | |
729 | { | |
730 | bfd *dynobj; | |
731 | asection *splt; | |
732 | ||
733 | /* As an extra sanity check, verify that all plt entries have | |
734 | been filled in. */ | |
735 | ||
736 | if ((dynobj = elf_hash_table (info)->dynobj) != NULL | |
737 | && (splt = bfd_get_section_by_name (dynobj, ".plt")) != NULL) | |
738 | { | |
739 | bfd_byte *contents = splt->contents; | |
740 | unsigned int i, size = splt->size; | |
741 | for (i = 0; i < size; i += 4) | |
742 | { | |
743 | unsigned int x = bfd_get_32 (dynobj, contents + i); | |
744 | BFD_ASSERT (x != 0); | |
745 | } | |
746 | } | |
747 | ||
748 | return TRUE; | |
749 | } | |
750 | ||
751 | static bfd_boolean | |
752 | m32c_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED, | |
753 | struct bfd_link_info *info) | |
754 | { | |
755 | bfd *dynobj; | |
756 | asection *splt; | |
757 | ||
758 | if (info->relocatable) | |
759 | return TRUE; | |
760 | ||
761 | dynobj = elf_hash_table (info)->dynobj; | |
762 | if (dynobj == NULL) | |
763 | return TRUE; | |
764 | ||
765 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
766 | BFD_ASSERT (splt != NULL); | |
767 | ||
768 | splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size); | |
769 | if (splt->contents == NULL) | |
770 | return FALSE; | |
771 | ||
772 | return TRUE; | |
773 | } | |
774 | \f | |
775 | /* Function to set the ELF flag bits. */ | |
776 | ||
777 | static bfd_boolean | |
778 | m32c_elf_set_private_flags (bfd *abfd, flagword flags) | |
779 | { | |
780 | elf_elfheader (abfd)->e_flags = flags; | |
781 | elf_flags_init (abfd) = TRUE; | |
782 | return TRUE; | |
783 | } | |
784 | ||
785 | /* Merge backend specific data from an object file to the output | |
786 | object file when linking. */ | |
787 | ||
788 | static bfd_boolean | |
789 | m32c_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd) | |
790 | { | |
791 | flagword old_flags, old_partial; | |
792 | flagword new_flags, new_partial; | |
793 | bfd_boolean error = FALSE; | |
794 | char new_opt[80]; | |
795 | char old_opt[80]; | |
796 | ||
797 | new_opt[0] = old_opt[0] = '\0'; | |
798 | new_flags = elf_elfheader (ibfd)->e_flags; | |
799 | old_flags = elf_elfheader (obfd)->e_flags; | |
800 | ||
801 | #ifdef DEBUG | |
802 | (*_bfd_error_handler) ("old_flags = 0x%.8lx, new_flags = 0x%.8lx, init = %s, filename = %s", | |
803 | old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no", | |
804 | bfd_get_filename (ibfd)); | |
805 | #endif | |
806 | ||
807 | if (!elf_flags_init (obfd)) | |
808 | { | |
809 | /* First call, no flags set. */ | |
810 | elf_flags_init (obfd) = TRUE; | |
811 | elf_elfheader (obfd)->e_flags = new_flags; | |
812 | } | |
813 | ||
814 | else if (new_flags == old_flags) | |
815 | /* Compatible flags are ok. */ | |
816 | ; | |
817 | ||
818 | else /* Possibly incompatible flags. */ | |
819 | { | |
820 | /* Warn if different cpu is used (allow a specific cpu to override | |
821 | the generic cpu). */ | |
822 | new_partial = (new_flags & EF_M32C_CPU_MASK); | |
823 | old_partial = (old_flags & EF_M32C_CPU_MASK); | |
824 | if (new_partial == old_partial) | |
825 | ; | |
826 | ||
827 | else | |
828 | { | |
829 | switch (new_partial) | |
830 | { | |
831 | default: strcat (new_opt, " -m16c"); break; | |
832 | case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c"); break; | |
833 | case EF_M32C_CPU_M32C: strcat (new_opt, " -m32c"); break; | |
834 | } | |
835 | ||
836 | switch (old_partial) | |
837 | { | |
838 | default: strcat (old_opt, " -m16c"); break; | |
839 | case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c"); break; | |
840 | case EF_M32C_CPU_M32C: strcat (old_opt, " -m32c"); break; | |
841 | } | |
842 | } | |
843 | ||
844 | /* Print out any mismatches from above. */ | |
845 | if (new_opt[0]) | |
846 | { | |
847 | error = TRUE; | |
848 | (*_bfd_error_handler) | |
849 | (_("%s: compiled with %s and linked with modules compiled with %s"), | |
850 | bfd_get_filename (ibfd), new_opt, old_opt); | |
851 | } | |
852 | ||
853 | new_flags &= ~ EF_M32C_ALL_FLAGS; | |
854 | old_flags &= ~ EF_M32C_ALL_FLAGS; | |
855 | ||
856 | /* Warn about any other mismatches. */ | |
857 | if (new_flags != old_flags) | |
858 | { | |
859 | error = TRUE; | |
860 | (*_bfd_error_handler) | |
861 | (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"), | |
862 | bfd_get_filename (ibfd), (long)new_flags, (long)old_flags); | |
863 | } | |
864 | } | |
865 | ||
866 | if (error) | |
867 | bfd_set_error (bfd_error_bad_value); | |
868 | ||
869 | return !error; | |
870 | } | |
871 | ||
872 | \f | |
873 | static bfd_boolean | |
874 | m32c_elf_print_private_bfd_data (bfd *abfd, PTR ptr) | |
875 | { | |
876 | FILE *file = (FILE *) ptr; | |
877 | flagword flags; | |
878 | ||
879 | BFD_ASSERT (abfd != NULL && ptr != NULL); | |
880 | ||
881 | /* Print normal ELF private data. */ | |
882 | _bfd_elf_print_private_bfd_data (abfd, ptr); | |
883 | ||
884 | flags = elf_elfheader (abfd)->e_flags; | |
885 | fprintf (file, _("private flags = 0x%lx:"), (long)flags); | |
886 | ||
887 | switch (flags & EF_M32C_CPU_MASK) | |
888 | { | |
889 | default: break; | |
890 | case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break; | |
891 | case EF_M32C_CPU_M32C: fprintf (file, " -m32c"); break; | |
892 | } | |
893 | ||
894 | fputc ('\n', file); | |
895 | return TRUE; | |
896 | } | |
897 | ||
898 | /* Return the MACH for an e_flags value. */ | |
899 | ||
900 | static int | |
901 | elf32_m32c_machine (bfd *abfd) | |
902 | { | |
903 | switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) | |
904 | { | |
905 | case EF_M32C_CPU_M16C: return bfd_mach_m16c; | |
906 | case EF_M32C_CPU_M32C: return bfd_mach_m32c; | |
907 | } | |
908 | ||
909 | return bfd_mach_m16c; | |
910 | } | |
911 | ||
912 | static bfd_boolean | |
913 | m32c_elf_object_p (bfd *abfd) | |
914 | { | |
915 | bfd_default_set_arch_mach (abfd, bfd_arch_m32c, | |
916 | elf32_m32c_machine (abfd)); | |
917 | return TRUE; | |
918 | } | |
919 | \f | |
920 | ||
921 | #ifdef DEBUG | |
922 | static void | |
923 | dump_symtab (bfd * abfd, void *internal_syms, void *external_syms) | |
924 | { | |
925 | size_t locsymcount; | |
926 | Elf_Internal_Sym *isymbuf; | |
927 | Elf_Internal_Sym *isymend; | |
928 | Elf_Internal_Sym *isym; | |
929 | Elf_Internal_Shdr *symtab_hdr; | |
930 | bfd_boolean free_internal = 0, free_external = 0; | |
931 | char * st_info_str; | |
932 | char * st_info_stb_str; | |
933 | char * st_other_str; | |
934 | char * st_shndx_str; | |
935 | ||
936 | if (! internal_syms) | |
937 | { | |
938 | internal_syms = bfd_malloc (1000); | |
939 | free_internal = 1; | |
940 | } | |
941 | if (! external_syms) | |
942 | { | |
943 | external_syms = bfd_malloc (1000); | |
944 | free_external = 1; | |
945 | } | |
946 | ||
947 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
948 | locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym; | |
949 | if (free_internal) | |
950 | isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr, | |
951 | symtab_hdr->sh_info, 0, | |
952 | internal_syms, external_syms, NULL); | |
953 | else | |
954 | isymbuf = internal_syms; | |
955 | isymend = isymbuf + locsymcount; | |
956 | ||
957 | for (isym = isymbuf ; isym < isymend ; isym++) | |
958 | { | |
959 | switch (ELF_ST_TYPE (isym->st_info)) | |
960 | { | |
961 | case STT_FUNC: st_info_str = "STT_FUNC"; | |
962 | case STT_SECTION: st_info_str = "STT_SECTION"; | |
963 | case STT_SRELC: st_info_str = "STT_SRELC"; | |
964 | case STT_FILE: st_info_str = "STT_FILE"; | |
965 | case STT_OBJECT: st_info_str = "STT_OBJECT"; | |
966 | case STT_TLS: st_info_str = "STT_TLS"; | |
967 | default: st_info_str = ""; | |
968 | } | |
969 | switch (ELF_ST_BIND (isym->st_info)) | |
970 | { | |
971 | case STB_LOCAL: st_info_stb_str = "STB_LOCAL"; | |
972 | case STB_GLOBAL: st_info_stb_str = "STB_GLOBAL"; | |
973 | default: st_info_stb_str = ""; | |
974 | } | |
975 | switch (ELF_ST_VISIBILITY (isym->st_other)) | |
976 | { | |
977 | case STV_DEFAULT: st_other_str = "STV_DEFAULT"; | |
978 | case STV_INTERNAL: st_other_str = "STV_INTERNAL"; | |
979 | case STV_PROTECTED: st_other_str = "STV_PROTECTED"; | |
980 | default: st_other_str = ""; | |
981 | } | |
982 | switch (isym->st_shndx) | |
983 | { | |
984 | case SHN_ABS: st_shndx_str = "SHN_ABS"; | |
985 | case SHN_COMMON: st_shndx_str = "SHN_COMMON"; | |
986 | case SHN_UNDEF: st_shndx_str = "SHN_UNDEF"; | |
987 | default: st_shndx_str = ""; | |
988 | } | |
989 | ||
990 | printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s " | |
991 | "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n", | |
992 | isym, | |
993 | (unsigned long) isym->st_value, | |
994 | (unsigned long) isym->st_size, | |
995 | isym->st_name, | |
996 | bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link, | |
997 | isym->st_name), | |
998 | isym->st_info, st_info_str, st_info_stb_str, | |
999 | isym->st_other, st_other_str, | |
1000 | isym->st_shndx, st_shndx_str); | |
1001 | } | |
1002 | if (free_internal) | |
1003 | free (internal_syms); | |
1004 | if (free_external) | |
1005 | free (external_syms); | |
1006 | } | |
1007 | ||
1008 | static char * | |
1009 | m32c_get_reloc (long reloc) | |
1010 | { | |
1011 | if (0 <= reloc && reloc < R_M32C_max) | |
1012 | return m32c_elf_howto_table[reloc].name; | |
1013 | else | |
1014 | return ""; | |
1015 | } | |
1016 | #endif /* DEBUG */ | |
1017 | ||
1018 | /* Handle relaxing. */ | |
1019 | ||
1020 | /* A subroutine of m32c_elf_relax_section. If the global symbol H | |
1021 | is within the low 64k, remove any entry for it in the plt. */ | |
1022 | ||
1023 | struct relax_plt_data | |
1024 | { | |
1025 | asection *splt; | |
1026 | bfd_boolean *again; | |
1027 | }; | |
1028 | ||
1029 | static bfd_boolean | |
1030 | m32c_relax_plt_check (struct elf_link_hash_entry *h, | |
1031 | PTR xdata) | |
1032 | { | |
1033 | struct relax_plt_data *data = (struct relax_plt_data *) xdata; | |
1034 | ||
1035 | if (h->root.type == bfd_link_hash_warning) | |
1036 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1037 | ||
1038 | if (h->plt.offset != (bfd_vma) -1) | |
1039 | { | |
1040 | bfd_vma address; | |
1041 | ||
1042 | if (h->root.type == bfd_link_hash_undefined | |
1043 | || h->root.type == bfd_link_hash_undefweak) | |
1044 | address = 0; | |
1045 | else | |
1046 | address = (h->root.u.def.section->output_section->vma | |
1047 | + h->root.u.def.section->output_offset | |
1048 | + h->root.u.def.value); | |
1049 | ||
1050 | if (address <= 0xffff) | |
1051 | { | |
1052 | h->plt.offset = -1; | |
1053 | data->splt->size -= 4; | |
1054 | *data->again = TRUE; | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | return TRUE; | |
1059 | } | |
1060 | ||
1061 | /* A subroutine of m32c_elf_relax_section. If the global symbol H | |
1062 | previously had a plt entry, give it a new entry offset. */ | |
1063 | ||
1064 | static bfd_boolean | |
1065 | m32c_relax_plt_realloc (struct elf_link_hash_entry *h, | |
1066 | PTR xdata) | |
1067 | { | |
1068 | bfd_vma *entry = (bfd_vma *) xdata; | |
1069 | ||
1070 | if (h->root.type == bfd_link_hash_warning) | |
1071 | h = (struct elf_link_hash_entry *) h->root.u.i.link; | |
1072 | ||
1073 | if (h->plt.offset != (bfd_vma) -1) | |
1074 | { | |
1075 | h->plt.offset = *entry; | |
1076 | *entry += 4; | |
1077 | } | |
1078 | ||
1079 | return TRUE; | |
1080 | } | |
1081 | ||
1082 | static bfd_boolean | |
1083 | m32c_elf_relax_plt_section (bfd *dynobj, | |
1084 | asection *splt, | |
1085 | struct bfd_link_info *info, | |
1086 | bfd_boolean *again) | |
1087 | { | |
1088 | struct relax_plt_data relax_plt_data; | |
1089 | bfd *ibfd; | |
1090 | ||
1091 | /* Assume nothing changes. */ | |
1092 | *again = FALSE; | |
1093 | ||
1094 | if (info->relocatable) | |
1095 | return TRUE; | |
1096 | ||
1097 | /* We only relax the .plt section at the moment. */ | |
1098 | if (dynobj != elf_hash_table (info)->dynobj | |
1099 | || strcmp (splt->name, ".plt") != 0) | |
1100 | return TRUE; | |
1101 | ||
1102 | /* Quick check for an empty plt. */ | |
1103 | if (splt->size == 0) | |
1104 | return TRUE; | |
1105 | ||
1106 | /* Map across all global symbols; see which ones happen to | |
1107 | fall in the low 64k. */ | |
1108 | relax_plt_data.splt = splt; | |
1109 | relax_plt_data.again = again; | |
1110 | elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check, | |
1111 | &relax_plt_data); | |
1112 | ||
1113 | /* Likewise for local symbols, though that's somewhat less convenient | |
1114 | as we have to walk the list of input bfds and swap in symbol data. */ | |
1115 | for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next) | |
1116 | { | |
1117 | bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd); | |
1118 | Elf_Internal_Shdr *symtab_hdr; | |
1119 | Elf_Internal_Sym *isymbuf = NULL; | |
1120 | unsigned int idx; | |
1121 | ||
1122 | if (! local_plt_offsets) | |
1123 | continue; | |
1124 | ||
1125 | symtab_hdr = &elf_tdata (ibfd)->symtab_hdr; | |
1126 | if (symtab_hdr->sh_info != 0) | |
1127 | { | |
1128 | isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1129 | if (isymbuf == NULL) | |
1130 | isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr, | |
1131 | symtab_hdr->sh_info, 0, | |
1132 | NULL, NULL, NULL); | |
1133 | if (isymbuf == NULL) | |
1134 | return FALSE; | |
1135 | } | |
1136 | ||
1137 | for (idx = 0; idx < symtab_hdr->sh_info; ++idx) | |
1138 | { | |
1139 | Elf_Internal_Sym *isym; | |
1140 | asection *tsec; | |
1141 | bfd_vma address; | |
1142 | ||
1143 | if (local_plt_offsets[idx] == (bfd_vma) -1) | |
1144 | continue; | |
1145 | ||
1146 | isym = &isymbuf[idx]; | |
1147 | if (isym->st_shndx == SHN_UNDEF) | |
1148 | continue; | |
1149 | else if (isym->st_shndx == SHN_ABS) | |
1150 | tsec = bfd_abs_section_ptr; | |
1151 | else if (isym->st_shndx == SHN_COMMON) | |
1152 | tsec = bfd_com_section_ptr; | |
1153 | else | |
1154 | tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx); | |
1155 | ||
1156 | address = (tsec->output_section->vma | |
1157 | + tsec->output_offset | |
1158 | + isym->st_value); | |
1159 | if (address <= 0xffff) | |
1160 | { | |
1161 | local_plt_offsets[idx] = -1; | |
1162 | splt->size -= 4; | |
1163 | *again = TRUE; | |
1164 | } | |
1165 | } | |
1166 | ||
1167 | if (isymbuf != NULL | |
1168 | && symtab_hdr->contents != (unsigned char *) isymbuf) | |
1169 | { | |
1170 | if (! info->keep_memory) | |
1171 | free (isymbuf); | |
1172 | else | |
1173 | { | |
1174 | /* Cache the symbols for elf_link_input_bfd. */ | |
1175 | symtab_hdr->contents = (unsigned char *) isymbuf; | |
1176 | } | |
1177 | } | |
1178 | } | |
1179 | ||
1180 | /* If we changed anything, walk the symbols again to reallocate | |
1181 | .plt entry addresses. */ | |
1182 | if (*again && splt->size > 0) | |
1183 | { | |
1184 | bfd_vma entry = 0; | |
1185 | ||
1186 | elf_link_hash_traverse (elf_hash_table (info), | |
1187 | m32c_relax_plt_realloc, &entry); | |
1188 | ||
1189 | for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link_next) | |
1190 | { | |
1191 | bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd); | |
1192 | unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info; | |
1193 | unsigned int idx; | |
1194 | ||
1195 | if (! local_plt_offsets) | |
1196 | continue; | |
1197 | ||
1198 | for (idx = 0; idx < nlocals; ++idx) | |
1199 | if (local_plt_offsets[idx] != (bfd_vma) -1) | |
1200 | { | |
1201 | local_plt_offsets[idx] = entry; | |
1202 | entry += 4; | |
1203 | } | |
1204 | } | |
1205 | } | |
1206 | ||
1207 | return TRUE; | |
1208 | } | |
1209 | ||
6772dd07 DD |
1210 | static int |
1211 | compare_reloc (const void *e1, const void *e2) | |
49f58d10 | 1212 | { |
6772dd07 DD |
1213 | const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1; |
1214 | const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2; | |
1215 | ||
1216 | if (i1->r_offset == i2->r_offset) | |
1217 | return 0; | |
1218 | else | |
1219 | return i1->r_offset < i2->r_offset ? -1 : 1; | |
1220 | } | |
1221 | ||
1222 | #define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, sec, rel, symtab_hdr, shndx_buf, intsyms) | |
1223 | static bfd_vma | |
1224 | m32c_offset_for_reloc (bfd *abfd, | |
1225 | asection * sec, | |
1226 | Elf_Internal_Rela *rel, | |
1227 | Elf_Internal_Shdr *symtab_hdr, | |
1228 | Elf_External_Sym_Shndx *shndx_buf, | |
1229 | Elf_Internal_Sym *intsyms) | |
1230 | { | |
1231 | bfd_vma symval; | |
1232 | ||
1233 | /* Get the value of the symbol referred to by the reloc. */ | |
1234 | if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info) | |
49f58d10 | 1235 | { |
6772dd07 DD |
1236 | /* A local symbol. */ |
1237 | Elf_Internal_Sym *isym; | |
1238 | Elf_External_Sym_Shndx *shndx; | |
1239 | ||
1240 | isym = intsyms + ELF32_R_SYM (rel->r_info); | |
1241 | shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (rel->r_info) : 0); | |
1242 | ||
1243 | symval = (isym->st_value | |
1244 | + sec->output_section->vma | |
1245 | + sec->output_offset); | |
1246 | } | |
1247 | else | |
49f58d10 | 1248 | { |
6772dd07 DD |
1249 | unsigned long indx; |
1250 | struct elf_link_hash_entry *h; | |
1251 | ||
1252 | /* An external symbol. */ | |
1253 | indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info; | |
1254 | h = elf_sym_hashes (abfd)[indx]; | |
1255 | BFD_ASSERT (h != NULL); | |
1256 | ||
1257 | if (h->root.type != bfd_link_hash_defined | |
1258 | && h->root.type != bfd_link_hash_defweak) | |
1259 | /* This appears to be a reference to an undefined | |
1260 | symbol. Just ignore it--it will be caught by the | |
1261 | regular reloc processing. */ | |
1262 | return 0; | |
1263 | ||
1264 | symval = (h->root.u.def.value | |
1265 | + h->root.u.def.section->output_section->vma | |
1266 | + h->root.u.def.section->output_offset); | |
49f58d10 | 1267 | } |
6772dd07 DD |
1268 | return symval; |
1269 | } | |
1270 | ||
1271 | static int bytes_saved = 0; | |
1272 | ||
1273 | static int bytes_to_reloc[] = { | |
1274 | R_M32C_NONE, | |
1275 | R_M32C_8, | |
1276 | R_M32C_16, | |
1277 | R_M32C_24, | |
1278 | R_M32C_32 | |
1279 | }; | |
1280 | ||
1281 | /* What we use the bits in a relax reloc addend (R_M32C_RL_*) for. */ | |
1282 | ||
1283 | /* Mask for the number of relocs associated with this insn. */ | |
1284 | #define RLA_RELOCS 0x0000000f | |
1285 | /* Number of bytes gas emitted (before gas's relaxing) */ | |
1286 | #define RLA_NBYTES 0x00000ff0 | |
1287 | ||
1288 | /* If the displacement is within the given range and the new encoding | |
1289 | differs from the old encoding (the index), then the insn can be | |
1290 | relaxed to the new encoding. */ | |
1291 | typedef struct { | |
1292 | int bytes; | |
1293 | unsigned int max_disp; | |
1294 | unsigned char new_encoding; | |
1295 | } EncodingTable; | |
1296 | ||
1297 | static EncodingTable m16c_addr_encodings[] = { | |
1298 | { 0, 0, 0 }, /* R0 */ | |
1299 | { 0, 0, 1 }, /* R1 */ | |
1300 | { 0, 0, 2 }, /* R2 */ | |
1301 | { 0, 0, 3 }, /* R3 */ | |
1302 | { 0, 0, 4 }, /* A0 */ | |
1303 | { 0, 0, 5 }, /* A1 */ | |
1304 | { 0, 0, 6 }, /* [A0] */ | |
1305 | { 0, 0, 7 }, /* [A1] */ | |
1306 | { 1, 0, 6 }, /* udsp:8[A0] */ | |
1307 | { 1, 0, 7 }, /* udsp:8[A1] */ | |
1308 | { 1, 0, 10 }, /* udsp:8[SB] */ | |
1309 | { 1, 0, 11 }, /* sdsp:8[FB] */ | |
1310 | { 2, 255, 8 }, /* udsp:16[A0] */ | |
1311 | { 2, 255, 9 }, /* udsp:16[A1] */ | |
1312 | { 2, 255, 10 }, /* udsp:16[SB] */ | |
1313 | { 2, 0, 15 }, /* abs:16 */ | |
1314 | }; | |
1315 | ||
1316 | static EncodingTable m16c_jmpaddr_encodings[] = { | |
1317 | { 0, 0, 0 }, /* R0 */ | |
1318 | { 0, 0, 1 }, /* R1 */ | |
1319 | { 0, 0, 2 }, /* R2 */ | |
1320 | { 0, 0, 3 }, /* R3 */ | |
1321 | { 0, 0, 4 }, /* A0 */ | |
1322 | { 0, 0, 5 }, /* A1 */ | |
1323 | { 0, 0, 6 }, /* [A0] */ | |
1324 | { 0, 0, 7 }, /* [A1] */ | |
1325 | { 1, 0, 6 }, /* udsp:8[A0] */ | |
1326 | { 1, 0, 7 }, /* udsp:8[A1] */ | |
1327 | { 1, 0, 10 }, /* udsp:8[SB] */ | |
1328 | { 1, 0, 11 }, /* sdsp:8[FB] */ | |
1329 | { 3, 255, 8 }, /* udsp:20[A0] */ | |
1330 | { 3, 255, 9 }, /* udsp:20[A1] */ | |
1331 | { 2, 255, 10 }, /* udsp:16[SB] */ | |
1332 | { 2, 0, 15 }, /* abs:16 */ | |
1333 | }; | |
1334 | ||
1335 | static EncodingTable m32c_addr_encodings[] = { | |
1336 | { 0, 0, 0 }, /* [A0] */ | |
1337 | { 0, 0, 1 }, /* [A1] */ | |
1338 | { 0, 0, 2 }, /* A0 */ | |
1339 | { 0, 0, 3 }, /* A1 */ | |
1340 | { 1, 0, 0 }, /* udsp:8[A0] */ | |
1341 | { 1, 0, 1 }, /* udsp:8[A1] */ | |
1342 | { 1, 0, 6 }, /* udsp:8[SB] */ | |
1343 | { 1, 0, 7 }, /* sdsp:8[FB] */ | |
1344 | { 2, 255, 4 }, /* udsp:16[A0] */ | |
1345 | { 2, 255, 5 }, /* udsp:16[A1] */ | |
1346 | { 2, 255, 6 }, /* udsp:16[SB] */ | |
1347 | { 2, 127, 7 }, /* sdsp:16[FB] */ | |
1348 | { 3, 65535, 8 }, /* udsp:24[A0] */ | |
1349 | { 3, 65535, 9 }, /* udsp:24[A1] */ | |
1350 | { 3, 65535, 15 }, /* abs24 */ | |
1351 | { 2, 0, 15 }, /* abs16 */ | |
1352 | { 0, 0, 16 }, /* R2 */ | |
1353 | { 0, 0, 17 }, /* R3 */ | |
1354 | { 0, 0, 18 }, /* R0 */ | |
1355 | { 0, 0, 19 }, /* R1 */ | |
1356 | { 0, 0, 20 }, /* */ | |
1357 | { 0, 0, 21 }, /* */ | |
1358 | { 0, 0, 22 }, /* */ | |
1359 | { 0, 0, 23 }, /* */ | |
1360 | { 0, 0, 24 }, /* */ | |
1361 | { 0, 0, 25 }, /* */ | |
1362 | { 0, 0, 26 }, /* */ | |
1363 | { 0, 0, 27 }, /* */ | |
1364 | { 0, 0, 28 }, /* */ | |
1365 | { 0, 0, 29 }, /* */ | |
1366 | { 0, 0, 30 }, /* */ | |
1367 | { 0, 0, 31 }, /* */ | |
1368 | }; | |
1369 | ||
49f58d10 JB |
1370 | static bfd_boolean |
1371 | m32c_elf_relax_section | |
1372 | (bfd * abfd, | |
1373 | asection * sec, | |
1374 | struct bfd_link_info * link_info, | |
1375 | bfd_boolean * again) | |
1376 | { | |
1377 | Elf_Internal_Shdr *symtab_hdr; | |
1378 | Elf_Internal_Shdr *shndx_hdr; | |
1379 | Elf_Internal_Rela *internal_relocs; | |
1380 | Elf_Internal_Rela *free_relocs = NULL; | |
6772dd07 | 1381 | Elf_Internal_Rela *irel, *irelend, *srel; |
49f58d10 JB |
1382 | bfd_byte * contents = NULL; |
1383 | bfd_byte * free_contents = NULL; | |
6772dd07 DD |
1384 | Elf_Internal_Sym *intsyms = NULL; |
1385 | Elf_Internal_Sym *free_intsyms = NULL; | |
49f58d10 JB |
1386 | Elf_External_Sym_Shndx *shndx_buf = NULL; |
1387 | int machine; | |
1388 | ||
1389 | if (abfd == elf_hash_table (link_info)->dynobj | |
1390 | && strcmp (sec->name, ".plt") == 0) | |
1391 | return m32c_elf_relax_plt_section (abfd, sec, link_info, again); | |
1392 | ||
1393 | /* Assume nothing changes. */ | |
1394 | *again = FALSE; | |
1395 | ||
1396 | machine = elf32_m32c_machine (abfd); | |
1397 | ||
1398 | /* We don't have to do anything for a relocatable link, if | |
1399 | this section does not have relocs, or if this is not a | |
1400 | code section. */ | |
1401 | if (link_info->relocatable | |
1402 | || (sec->flags & SEC_RELOC) == 0 | |
1403 | || sec->reloc_count == 0 | |
1404 | || (sec->flags & SEC_CODE) == 0) | |
1405 | return TRUE; | |
1406 | ||
49f58d10 JB |
1407 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; |
1408 | shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr; | |
1409 | ||
6772dd07 DD |
1410 | /* Get the section contents. */ |
1411 | if (elf_section_data (sec)->this_hdr.contents != NULL) | |
1412 | contents = elf_section_data (sec)->this_hdr.contents; | |
1413 | /* Go get them off disk. */ | |
1414 | else if (!bfd_malloc_and_get_section (abfd, sec, &contents)) | |
1415 | goto error_return; | |
1416 | ||
1417 | /* Read this BFD's symbols. */ | |
1418 | /* Get cached copy if it exists. */ | |
1419 | if (symtab_hdr->contents != NULL) | |
1420 | { | |
1421 | intsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1422 | } | |
1423 | else | |
1424 | { | |
1425 | intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL); | |
1426 | symtab_hdr->contents = (bfd_byte *) intsyms; | |
1427 | } | |
1428 | ||
1429 | if (shndx_hdr->sh_size != 0) | |
1430 | { | |
1431 | bfd_size_type amt; | |
1432 | ||
1433 | amt = symtab_hdr->sh_info; | |
1434 | amt *= sizeof (Elf_External_Sym_Shndx); | |
1435 | shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt); | |
1436 | if (shndx_buf == NULL) | |
1437 | goto error_return; | |
1438 | if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0 | |
1439 | || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt) | |
1440 | goto error_return; | |
1441 | shndx_hdr->contents = (bfd_byte *) shndx_buf; | |
1442 | } | |
1443 | ||
49f58d10 JB |
1444 | /* Get a copy of the native relocations. */ |
1445 | internal_relocs = (_bfd_elf_link_read_relocs | |
1446 | (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL, | |
1447 | link_info->keep_memory)); | |
1448 | if (internal_relocs == NULL) | |
1449 | goto error_return; | |
1450 | if (! link_info->keep_memory) | |
1451 | free_relocs = internal_relocs; | |
1452 | ||
6772dd07 DD |
1453 | /* The RL_ relocs must be just before the operand relocs they go |
1454 | with, so we must sort them to guarantee this. */ | |
1455 | qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela), | |
1456 | compare_reloc); | |
1457 | ||
49f58d10 JB |
1458 | /* Walk through them looking for relaxing opportunities. */ |
1459 | irelend = internal_relocs + sec->reloc_count; | |
1460 | ||
1461 | for (irel = internal_relocs; irel < irelend; irel++) | |
1462 | { | |
1463 | bfd_vma symval; | |
6772dd07 | 1464 | unsigned char *insn, *gap, *einsn; |
49f58d10 | 1465 | bfd_vma pc; |
6772dd07 DD |
1466 | bfd_signed_vma pcrel; |
1467 | int relax_relocs; | |
1468 | int gap_size; | |
1469 | int new_type; | |
1470 | int posn; | |
1471 | int enc; | |
1472 | EncodingTable *enctbl; | |
1473 | EncodingTable *e; | |
1474 | ||
1475 | if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP | |
1476 | && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR | |
1477 | && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR) | |
1478 | continue; | |
49f58d10 | 1479 | |
6772dd07 | 1480 | srel = irel; |
49f58d10 | 1481 | |
6772dd07 DD |
1482 | /* There will always be room for the relaxed insn, since it is smaller |
1483 | than the one it would replace. */ | |
1484 | BFD_ASSERT (irel->r_offset < sec->size); | |
1485 | ||
1486 | insn = contents + irel->r_offset; | |
1487 | relax_relocs = irel->r_addend % 16; | |
1488 | ||
1489 | /* Ok, we only have three relocs we care about, and they're all | |
1490 | fake. The lower four bits of the addend is always the number | |
1491 | of following relocs (hence the qsort above) that are assigned | |
1492 | to this opcode. The next 8 bits of the addend indicates the | |
1493 | number of bytes in the insn. We use the rest of them | |
1494 | ourselves as flags for the more expensive operations (defines | |
1495 | above). The three relocs are: | |
1496 | ||
1497 | RL_JUMP: This marks all direct jump insns. We check the | |
1498 | displacement and replace them with shorter jumps if | |
1499 | they're in range. We also use this to find JMP.S | |
1500 | insns and manually shorten them when we delete bytes. | |
1501 | We have to decode these insns to figure out what to | |
1502 | do. | |
1503 | ||
1504 | RL_1ADDR: This is a :G or :Q insn, which has a single | |
1505 | "standard" operand. We have to extract the type | |
1506 | field, see if it's a wide displacement, then figure | |
1507 | out if we can replace it with a narrow displacement. | |
1508 | We don't have to decode these insns. | |
1509 | ||
1510 | RL_2ADDR: Similarly, but two "standard" operands. Note that | |
1511 | r_addend may still be 1, as standard operands don't | |
1512 | always have displacements. Gas shouldn't give us one | |
1513 | with zero operands, but since we don't know which one | |
1514 | has the displacement, we check them both anyway. | |
1515 | ||
1516 | These all point to the beginning of the insn itself, not the | |
1517 | operands. | |
1518 | ||
1519 | Note that we only relax one step at a time, relying on the | |
1520 | linker to call us repeatedly. Thus, there is no code for | |
1521 | JMP.A->JMP.B although that will happen in two steps. | |
1522 | Likewise, for 2ADDR relaxes, we do one operand per cycle. | |
1523 | */ | |
1524 | ||
1525 | /* Get the value of the symbol referred to by the reloc. Just | |
1526 | in case this is the last reloc in the list, use the RL's | |
1527 | addend to choose between this reloc (no addend) or the next | |
1528 | (yes addend, which means at least one following reloc). */ | |
1529 | srel = irel + (relax_relocs ? 1 : 0); | |
1530 | symval = OFFSET_FOR_RELOC (srel); | |
1531 | ||
1532 | /* Setting gap_size nonzero is the flag which means "something | |
1533 | shrunk". */ | |
1534 | gap_size = 0; | |
1535 | new_type = ELF32_R_TYPE(srel->r_info); | |
1536 | ||
1537 | pc = sec->output_section->vma + sec->output_offset | |
1538 | + srel->r_offset; | |
1539 | pcrel = symval - pc + srel->r_addend; | |
1540 | ||
1541 | if (machine == bfd_mach_m16c) | |
49f58d10 | 1542 | { |
6772dd07 | 1543 | /* R8C / M16C */ |
49f58d10 | 1544 | |
6772dd07 | 1545 | switch (ELF32_R_TYPE(irel->r_info)) |
49f58d10 | 1546 | { |
49f58d10 | 1547 | |
6772dd07 DD |
1548 | case R_M32C_RL_JUMP: |
1549 | switch (insn[0]) | |
1550 | { | |
1551 | case 0xfe: /* jmp.b */ | |
1552 | if (pcrel >= 2 && pcrel <= 9) | |
1553 | { | |
1554 | /* Relax JMP.B -> JMP.S. We need to get rid of | |
1555 | the following reloc though. */ | |
1556 | insn[0] = 0x60 | (pcrel - 2); | |
1557 | new_type = R_M32C_NONE; | |
1558 | irel->r_addend = 0x10; | |
1559 | gap_size = 1; | |
1560 | gap = insn + 1; | |
1561 | } | |
1562 | break; | |
1563 | ||
1564 | case 0xf4: /* jmp.w */ | |
1565 | /* 128 is allowed because it will be one byte closer | |
1566 | after relaxing. Likewise for all other pc-rel | |
1567 | jumps. */ | |
1568 | if (pcrel <= 128 && pcrel >= -128) | |
1569 | { | |
1570 | /* Relax JMP.W -> JMP.B */ | |
1571 | insn[0] = 0xfe; | |
1572 | insn[1] = 0; | |
1573 | new_type = R_M32C_8_PCREL; | |
1574 | gap_size = 1; | |
1575 | gap = insn + 2; | |
1576 | } | |
1577 | break; | |
1578 | ||
1579 | case 0xfc: /* jmp.a */ | |
1580 | if (pcrel <= 32768 && pcrel >= -32768) | |
1581 | { | |
1582 | /* Relax JMP.A -> JMP.W */ | |
1583 | insn[0] = 0xf4; | |
1584 | insn[1] = 0; | |
1585 | insn[2] = 0; | |
1586 | new_type = R_M32C_16_PCREL; | |
1587 | gap_size = 1; | |
1588 | gap = insn + 3; | |
1589 | } | |
1590 | break; | |
1591 | ||
1592 | case 0xfd: /* jsr.a */ | |
1593 | if (pcrel <= 32768 && pcrel >= -32768) | |
1594 | { | |
1595 | /* Relax JSR.A -> JSR.W */ | |
1596 | insn[0] = 0xf5; | |
1597 | insn[1] = 0; | |
1598 | insn[2] = 0; | |
1599 | new_type = R_M32C_16_PCREL; | |
1600 | gap_size = 1; | |
1601 | gap = insn + 3; | |
1602 | } | |
1603 | break; | |
1604 | } | |
1605 | break; | |
49f58d10 | 1606 | |
6772dd07 DD |
1607 | case R_M32C_RL_2ADDR: |
1608 | /* xxxx xxxx srce dest [src-disp] [dest-disp]*/ | |
49f58d10 | 1609 | |
6772dd07 DD |
1610 | enctbl = m16c_addr_encodings; |
1611 | posn = 2; | |
1612 | enc = (insn[1] >> 4) & 0x0f; | |
1613 | e = & enctbl[enc]; | |
1614 | ||
1615 | if (srel->r_offset == irel->r_offset + posn | |
1616 | && e->new_encoding != enc | |
1617 | && symval <= e->max_disp) | |
1618 | { | |
1619 | insn[1] &= 0x0f; | |
1620 | insn[1] |= e->new_encoding << 4; | |
1621 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; | |
1622 | gap = insn + posn + enctbl[e->new_encoding].bytes; | |
1623 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; | |
1624 | break; | |
1625 | } | |
1626 | if (relax_relocs == 2) | |
1627 | srel ++; | |
1628 | posn += e->bytes; | |
1629 | ||
1630 | goto try_1addr_16; | |
49f58d10 | 1631 | |
6772dd07 DD |
1632 | case R_M32C_RL_1ADDR: |
1633 | /* xxxx xxxx xxxx dest [disp] */ | |
49f58d10 | 1634 | |
6772dd07 DD |
1635 | enctbl = m16c_addr_encodings; |
1636 | posn = 2; | |
1637 | ||
1638 | /* Check the opcode for jumps. We know it's safe to | |
1639 | do this because all 2ADDR insns are at least two | |
1640 | bytes long. */ | |
1641 | enc = insn[0] * 256 + insn[1]; | |
1642 | enc &= 0xfff0; | |
1643 | if (enc == 0x7d20 | |
1644 | || enc == 0x7d00 | |
1645 | || enc == 0x7d30 | |
1646 | || enc == 0x7d10) | |
1647 | { | |
1648 | enctbl = m16c_jmpaddr_encodings; | |
1649 | } | |
1650 | ||
1651 | try_1addr_16: | |
1652 | /* srel, posn, and enc must be set here. */ | |
1653 | ||
1654 | symval = OFFSET_FOR_RELOC (srel); | |
1655 | enc = insn[1] & 0x0f; | |
1656 | e = & enctbl[enc]; | |
1657 | ||
1658 | if (srel->r_offset == irel->r_offset + posn | |
1659 | && e->new_encoding != enc | |
1660 | && symval <= e->max_disp) | |
1661 | { | |
1662 | insn[1] &= 0xf0; | |
1663 | insn[1] |= e->new_encoding; | |
1664 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; | |
1665 | gap = insn + posn + enctbl[e->new_encoding].bytes; | |
1666 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; | |
1667 | break; | |
1668 | } | |
1669 | ||
1670 | break; | |
1671 | ||
1672 | } /* Ends switch (reloc type) for m16c. */ | |
1673 | } | |
1674 | else /* machine == bfd_mach_m32c */ | |
49f58d10 | 1675 | { |
6772dd07 | 1676 | /* M32CM / M32C */ |
49f58d10 | 1677 | |
6772dd07 | 1678 | switch (ELF32_R_TYPE(irel->r_info)) |
49f58d10 | 1679 | { |
49f58d10 | 1680 | |
6772dd07 DD |
1681 | case R_M32C_RL_JUMP: |
1682 | switch (insn[0]) | |
1683 | { | |
1684 | case 0xbb: /* jmp.b */ | |
1685 | if (pcrel >= 2 && pcrel <= 9) | |
1686 | { | |
1687 | int p = pcrel - 2; | |
1688 | /* Relax JMP.B -> JMP.S. We need to get rid of | |
1689 | the following reloc though. */ | |
1690 | insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1); | |
1691 | new_type = R_M32C_NONE; | |
1692 | irel->r_addend = 0x10; | |
1693 | gap_size = 1; | |
1694 | gap = insn + 1; | |
1695 | } | |
1696 | break; | |
1697 | ||
1698 | case 0xce: /* jmp.w */ | |
1699 | if (pcrel <= 128 && pcrel >= -128) | |
1700 | { | |
1701 | /* Relax JMP.W -> JMP.B */ | |
1702 | insn[0] = 0xbb; | |
1703 | insn[1] = 0; | |
1704 | new_type = R_M32C_8_PCREL; | |
1705 | gap_size = 1; | |
1706 | gap = insn + 2; | |
1707 | } | |
1708 | break; | |
1709 | ||
1710 | case 0xcc: /* jmp.a */ | |
1711 | if (pcrel <= 32768 && pcrel >= -32768) | |
1712 | { | |
1713 | /* Relax JMP.A -> JMP.W */ | |
1714 | insn[0] = 0xce; | |
1715 | insn[1] = 0; | |
1716 | insn[2] = 0; | |
1717 | new_type = R_M32C_16_PCREL; | |
1718 | gap_size = 1; | |
1719 | gap = insn + 3; | |
1720 | } | |
1721 | break; | |
1722 | ||
1723 | case 0xcd: /* jsr.a */ | |
1724 | if (pcrel <= 32768 && pcrel >= -32768) | |
1725 | { | |
1726 | /* Relax JSR.A -> JSR.W */ | |
1727 | insn[0] = 0xcf; | |
1728 | insn[1] = 0; | |
1729 | insn[2] = 0; | |
1730 | new_type = R_M32C_16_PCREL; | |
1731 | gap_size = 1; | |
1732 | gap = insn + 3; | |
1733 | } | |
1734 | break; | |
1735 | } | |
1736 | break; | |
49f58d10 | 1737 | |
6772dd07 DD |
1738 | case R_M32C_RL_2ADDR: |
1739 | /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/ | |
49f58d10 | 1740 | |
6772dd07 DD |
1741 | einsn = insn; |
1742 | posn = 2; | |
1743 | if (einsn[0] == 1) | |
1744 | { | |
1745 | /* prefix; remove it as far as the RL reloc is concerned. */ | |
1746 | einsn ++; | |
1747 | posn ++; | |
1748 | } | |
49f58d10 | 1749 | |
6772dd07 DD |
1750 | enctbl = m32c_addr_encodings; |
1751 | enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4); | |
1752 | e = & enctbl[enc]; | |
49f58d10 | 1753 | |
6772dd07 DD |
1754 | if (srel->r_offset == irel->r_offset + posn |
1755 | && e->new_encoding != enc | |
1756 | && symval <= e->max_disp) | |
1757 | { | |
1758 | einsn[0] &= 0x8f; | |
1759 | einsn[0] |= (e->new_encoding & 0x1c) << 2; | |
1760 | einsn[1] &= 0xcf; | |
1761 | einsn[1] |= (e->new_encoding & 0x03) << 4; | |
1762 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; | |
1763 | gap = insn + posn + enctbl[e->new_encoding].bytes; | |
1764 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; | |
1765 | break; | |
1766 | } | |
1767 | if (relax_relocs == 2) | |
1768 | srel ++; | |
1769 | posn += e->bytes; | |
1770 | ||
1771 | goto try_1addr_32; | |
1772 | ||
1773 | case R_M32C_RL_1ADDR: | |
1774 | /* xxxx DDDx DDxx xxxx [disp] */ | |
1775 | ||
1776 | einsn = insn; | |
1777 | posn = 2; | |
1778 | if (einsn[0] == 1) | |
1779 | { | |
1780 | /* prefix; remove it as far as the RL reloc is concerned. */ | |
1781 | einsn ++; | |
1782 | posn ++; | |
1783 | } | |
1784 | ||
1785 | enctbl = m32c_addr_encodings; | |
49f58d10 | 1786 | |
6772dd07 DD |
1787 | try_1addr_32: |
1788 | /* srel, posn, and enc must be set here. */ | |
49f58d10 | 1789 | |
6772dd07 DD |
1790 | symval = OFFSET_FOR_RELOC (srel); |
1791 | enc = ((einsn[0] & 0x0e) << 1) | ((einsn[1] & 0xc0) >> 6); | |
1792 | e = & enctbl[enc]; | |
49f58d10 | 1793 | |
6772dd07 DD |
1794 | if (srel->r_offset == irel->r_offset + posn |
1795 | && e->new_encoding != enc | |
1796 | && symval <= e->max_disp) | |
1797 | { | |
1798 | einsn[0] &= 0xf1; | |
1799 | einsn[0] |= (e->new_encoding & 0x1c) >> 1; | |
1800 | einsn[1] &= 0x3f; | |
1801 | einsn[1] |= (e->new_encoding & 0x03) << 6; | |
1802 | gap_size = e->bytes - enctbl[e->new_encoding].bytes; | |
1803 | gap = insn + posn + enctbl[e->new_encoding].bytes; | |
1804 | new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes]; | |
1805 | break; | |
1806 | } | |
1807 | ||
1808 | break; | |
1809 | ||
1810 | } /* Ends switch (reloc type) for m32c. */ | |
1811 | } | |
1812 | ||
1813 | if (gap_size == 0) | |
1814 | continue; | |
1815 | ||
1816 | *again = TRUE; | |
1817 | ||
1818 | srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type); | |
1819 | ||
1820 | /* Note that we've changed the relocs, section contents, etc. */ | |
1821 | elf_section_data (sec)->relocs = internal_relocs; | |
1822 | free_relocs = NULL; | |
1823 | ||
1824 | elf_section_data (sec)->this_hdr.contents = contents; | |
1825 | free_contents = NULL; | |
1826 | ||
1827 | symtab_hdr->contents = (bfd_byte *) intsyms; | |
1828 | free_intsyms = NULL; | |
1829 | ||
1830 | bytes_saved += gap_size; | |
1831 | ||
1832 | if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size)) | |
1833 | goto error_return; | |
49f58d10 | 1834 | |
49f58d10 JB |
1835 | } /* next relocation */ |
1836 | ||
1837 | if (free_relocs != NULL) | |
1838 | { | |
1839 | free (free_relocs); | |
1840 | free_relocs = NULL; | |
1841 | } | |
1842 | ||
1843 | if (free_contents != NULL) | |
1844 | { | |
1845 | if (! link_info->keep_memory) | |
1846 | free (free_contents); | |
1847 | /* Cache the section contents for elf_link_input_bfd. */ | |
1848 | else | |
1849 | elf_section_data (sec)->this_hdr.contents = contents; | |
1850 | ||
1851 | free_contents = NULL; | |
1852 | } | |
1853 | ||
1854 | if (shndx_buf != NULL) | |
1855 | { | |
1856 | shndx_hdr->contents = NULL; | |
1857 | free (shndx_buf); | |
1858 | } | |
1859 | ||
6772dd07 | 1860 | if (free_intsyms != NULL) |
49f58d10 JB |
1861 | { |
1862 | if (! link_info->keep_memory) | |
6772dd07 | 1863 | free (free_intsyms); |
49f58d10 JB |
1864 | /* Cache the symbols for elf_link_input_bfd. */ |
1865 | else | |
6772dd07 DD |
1866 | { |
1867 | symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/; | |
1868 | } | |
49f58d10 | 1869 | |
6772dd07 | 1870 | free_intsyms = NULL; |
49f58d10 | 1871 | } |
49f58d10 JB |
1872 | |
1873 | return TRUE; | |
1874 | ||
1875 | error_return: | |
1876 | if (free_relocs != NULL) | |
1877 | free (free_relocs); | |
1878 | if (free_contents != NULL) | |
1879 | free (free_contents); | |
1880 | if (shndx_buf != NULL) | |
1881 | { | |
1882 | shndx_hdr->contents = NULL; | |
1883 | free (shndx_buf); | |
1884 | } | |
6772dd07 DD |
1885 | if (free_intsyms != NULL) |
1886 | free (free_intsyms); | |
49f58d10 JB |
1887 | return FALSE; |
1888 | } | |
1889 | ||
1890 | /* Delete some bytes from a section while relaxing. */ | |
1891 | ||
1892 | static bfd_boolean | |
1893 | m32c_elf_relax_delete_bytes | |
1894 | (bfd * abfd, | |
1895 | asection * sec, | |
1896 | bfd_vma addr, | |
1897 | int count) | |
1898 | { | |
1899 | Elf_Internal_Shdr *symtab_hdr; | |
1900 | Elf_Internal_Shdr *shndx_hdr; | |
1901 | int sec_shndx; | |
1902 | bfd_byte *contents; | |
1903 | Elf_Internal_Rela *irel; | |
1904 | Elf_Internal_Rela *irelend; | |
1905 | Elf_Internal_Rela *irelalign; | |
1906 | bfd_vma toaddr; | |
6772dd07 DD |
1907 | Elf_Internal_Sym *isym; |
1908 | Elf_Internal_Sym *isymend; | |
1909 | Elf_Internal_Sym *intsyms; | |
49f58d10 JB |
1910 | Elf_External_Sym_Shndx *shndx_buf; |
1911 | Elf_External_Sym_Shndx *shndx; | |
1912 | struct elf_link_hash_entry ** sym_hashes; | |
1913 | struct elf_link_hash_entry ** end_hashes; | |
1914 | unsigned int symcount; | |
6772dd07 | 1915 | |
49f58d10 JB |
1916 | contents = elf_section_data (sec)->this_hdr.contents; |
1917 | ||
1918 | /* The deletion must stop at the next ALIGN reloc for an aligment | |
1919 | power larger than the number of bytes we are deleting. */ | |
1920 | irelalign = NULL; | |
1921 | toaddr = sec->size; | |
1922 | ||
1923 | irel = elf_section_data (sec)->relocs; | |
1924 | irelend = irel + sec->reloc_count; | |
1925 | ||
1926 | /* Actually delete the bytes. */ | |
1927 | memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count)); | |
1928 | sec->size -= count; | |
1929 | ||
1930 | /* Adjust all the relocs. */ | |
1931 | for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++) | |
1932 | { | |
1933 | /* Get the new reloc address. */ | |
1934 | if (irel->r_offset > addr && irel->r_offset < toaddr) | |
1935 | irel->r_offset -= count; | |
6772dd07 DD |
1936 | |
1937 | if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP | |
1938 | && irel->r_addend == 0x10 /* one byte insn, no relocs */ | |
1939 | && irel->r_offset + 1 < addr | |
1940 | && irel->r_offset + 7 > addr) | |
1941 | { | |
1942 | bfd_vma disp; | |
1943 | unsigned char *insn = &contents[irel->r_offset]; | |
1944 | disp = *insn; | |
1945 | /* This is a JMP.S, which we have to manually update. */ | |
1946 | if (elf32_m32c_machine (abfd) == bfd_mach_m16c) | |
1947 | { | |
1948 | if ((*insn & 0xf8) != 0x60) | |
1949 | continue; | |
1950 | disp = (disp & 7); | |
1951 | } | |
1952 | else | |
1953 | { | |
1954 | if ((*insn & 0xce) != 0x4a) | |
1955 | continue; | |
1956 | disp = ((disp & 0x30) >> 3) | (disp & 1); | |
1957 | } | |
1958 | if (irel->r_offset + disp + 2 >= addr+count) | |
1959 | { | |
1960 | disp -= count; | |
1961 | if (elf32_m32c_machine (abfd) == bfd_mach_m16c) | |
1962 | { | |
1963 | *insn = (*insn & 0xf8) | disp; | |
1964 | } | |
1965 | else | |
1966 | { | |
1967 | *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1); | |
1968 | } | |
1969 | } | |
1970 | } | |
49f58d10 JB |
1971 | } |
1972 | ||
1973 | /* Adjust the local symbols defined in this section. */ | |
6772dd07 DD |
1974 | symtab_hdr = & elf_tdata (abfd)->symtab_hdr; |
1975 | intsyms = (Elf_Internal_Sym *) symtab_hdr->contents; | |
1976 | isym = intsyms; | |
1977 | isymend = isym + symtab_hdr->sh_info; | |
1978 | ||
1979 | sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec); | |
1980 | shndx_hdr = & elf_tdata (abfd)->symtab_shndx_hdr; | |
1981 | shndx_buf = (Elf_External_Sym_Shndx *) shndx_hdr->contents; | |
49f58d10 | 1982 | shndx = shndx_buf; |
49f58d10 | 1983 | |
6772dd07 DD |
1984 | for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL)) |
1985 | { | |
49f58d10 | 1986 | |
6772dd07 DD |
1987 | if ((int) isym->st_shndx == sec_shndx |
1988 | && isym->st_value > addr | |
1989 | && isym->st_value < toaddr) | |
49f58d10 | 1990 | { |
6772dd07 | 1991 | isym->st_value -= count; |
49f58d10 JB |
1992 | } |
1993 | } | |
1994 | ||
1995 | /* Now adjust the global symbols defined in this section. */ | |
1996 | symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym) | |
1997 | - symtab_hdr->sh_info); | |
1998 | sym_hashes = elf_sym_hashes (abfd); | |
1999 | // sym_hashes += symtab_hdr->sh_info; | |
2000 | end_hashes = sym_hashes + symcount; | |
2001 | ||
2002 | for (; sym_hashes < end_hashes; sym_hashes ++) | |
2003 | { | |
2004 | struct elf_link_hash_entry * sym_hash = * sym_hashes; | |
2005 | ||
2006 | if (sym_hash && | |
2007 | ( sym_hash->root.type == bfd_link_hash_defined | |
2008 | || sym_hash->root.type == bfd_link_hash_defweak) | |
2009 | && sym_hash->root.u.def.section == sec | |
2010 | && sym_hash->root.u.def.value > addr | |
2011 | && sym_hash->root.u.def.value < toaddr) | |
6772dd07 DD |
2012 | { |
2013 | sym_hash->root.u.def.value -= count; | |
2014 | } | |
49f58d10 JB |
2015 | } |
2016 | ||
2017 | return TRUE; | |
2018 | } | |
2019 | \f | |
2020 | ||
2021 | #define ELF_ARCH bfd_arch_m32c | |
2022 | #define ELF_MACHINE_CODE EM_M32C | |
2023 | #define ELF_MAXPAGESIZE 0x1000 | |
2024 | ||
2025 | #if 0 | |
2026 | #define TARGET_BIG_SYM bfd_elf32_m32c_vec | |
2027 | #define TARGET_BIG_NAME "elf32-m32c" | |
2028 | #else | |
2029 | #define TARGET_LITTLE_SYM bfd_elf32_m32c_vec | |
2030 | #define TARGET_LITTLE_NAME "elf32-m32c" | |
2031 | #endif | |
2032 | ||
2033 | #define elf_info_to_howto_rel NULL | |
2034 | #define elf_info_to_howto m32c_info_to_howto_rela | |
2035 | #define elf_backend_object_p m32c_elf_object_p | |
2036 | #define elf_backend_relocate_section m32c_elf_relocate_section | |
2037 | #define elf_backend_gc_mark_hook m32c_elf_gc_mark_hook | |
2038 | #define elf_backend_gc_sweep_hook m32c_elf_gc_sweep_hook | |
2039 | #define elf_backend_check_relocs m32c_elf_check_relocs | |
2040 | #define elf_backend_object_p m32c_elf_object_p | |
2041 | #define elf_symbol_leading_char ('_') | |
2042 | #define elf_backend_always_size_sections \ | |
2043 | m32c_elf_always_size_sections | |
2044 | #define elf_backend_finish_dynamic_sections \ | |
2045 | m32c_elf_finish_dynamic_sections | |
2046 | ||
2047 | #define elf_backend_can_gc_sections 1 | |
2048 | ||
2049 | #define bfd_elf32_bfd_reloc_type_lookup m32c_reloc_type_lookup | |
2050 | #define bfd_elf32_bfd_relax_section m32c_elf_relax_section | |
2051 | #define bfd_elf32_bfd_set_private_flags m32c_elf_set_private_flags | |
2052 | #define bfd_elf32_bfd_merge_private_bfd_data m32c_elf_merge_private_bfd_data | |
2053 | #define bfd_elf32_bfd_print_private_bfd_data m32c_elf_print_private_bfd_data | |
2054 | ||
2055 | #include "elf32-target.h" |