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