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