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