Mon Sep 25 22:49:32 1995 Andreas Schwab <schwab@issan.informatik.uni-dortmund.de>
[deliverable/binutils-gdb.git] / bfd / coff-sh.c
CommitLineData
46dd0622 1/* BFD back-end for Hitachi Super-H COFF binaries.
51fbf454 2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
46dd0622
SC
3 Contributed by Cygnus Support.
4 Written by Steve Chamberlain, <sac@cygnus.com>.
5
6This file is part of BFD, the Binary File Descriptor library.
7
8This program is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program; if not, write to the Free Software
e0981b9b 20Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
46dd0622
SC
21
22#include "bfd.h"
23#include "sysdep.h"
46dd0622 24#include "obstack.h"
6812b607
ILT
25#include "libbfd.h"
26#include "bfdlink.h"
46dd0622
SC
27#include "coff/sh.h"
28#include "coff/internal.h"
29#include "libcoff.h"
46dd0622 30
e0981b9b
ILT
31/* Internal functions. */
32static bfd_reloc_status_type sh_reloc
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34static long get_symbol_value PARAMS ((asymbol *));
35static boolean sh_relax_section
36 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
37static boolean sh_relax_delete_bytes
38 PARAMS ((bfd *, asection *, bfd_vma, int));
39static boolean sh_relocate_section
40 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
41 struct internal_reloc *, struct internal_syment *, asection **));
42static bfd_byte *sh_coff_get_relocated_section_contents
43 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
44 bfd_byte *, boolean, asymbol **));
46dd0622 45
e0981b9b 46/* Default section alignment to 2**2. */
f6f350fe
KR
47#define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
48
e0981b9b
ILT
49/* Generate long file names. */
50#define COFF_LONG_FILENAMES
46dd0622 51
e0981b9b
ILT
52/* The supported relocations. There are a lot of relocations defined
53 in coff/internal.h which we do not expect to ever see. */
54static reloc_howto_type sh_coff_howtos[] =
55{
56 { 0 },
57 { 1 },
58 { 2 },
59 { 3 }, /* R_SH_PCREL8 */
60 { 4 }, /* R_SH_PCREL16 */
61 { 5 }, /* R_SH_HIGH8 */
62 { 6 }, /* R_SH_IMM24 */
63 { 7 }, /* R_SH_LOW16 */
64 { 8 },
65 { 9 }, /* R_SH_PCDISP8BY4 */
66
67 HOWTO (R_SH_PCDISP8BY2, /* type */
68 1, /* rightshift */
69 1, /* size (0 = byte, 1 = short, 2 = long) */
70 8, /* bitsize */
71 true, /* pc_relative */
72 0, /* bitpos */
73 complain_overflow_signed, /* complain_on_overflow */
74 sh_reloc, /* special_function */
75 "r_pcdisp8by2", /* name */
76 true, /* partial_inplace */
77 0xff, /* src_mask */
78 0xff, /* dst_mask */
79 true), /* pcrel_offset */
80
81 { 11 }, /* R_SH_PCDISP8 */
82
83 HOWTO (R_SH_PCDISP, /* type */
84 1, /* rightshift */
85 1, /* size (0 = byte, 1 = short, 2 = long) */
86 12, /* bitsize */
87 true, /* pc_relative */
88 0, /* bitpos */
89 complain_overflow_signed, /* complain_on_overflow */
90 sh_reloc, /* special_function */
91 "r_pcdisp12by2", /* name */
92 true, /* partial_inplace */
93 0xfff, /* src_mask */
94 0xfff, /* dst_mask */
95 true), /* pcrel_offset */
96
97 { 13 },
98
99 HOWTO (R_SH_IMM32, /* type */
100 0, /* rightshift */
101 2, /* size (0 = byte, 1 = short, 2 = long) */
102 32, /* bitsize */
103 false, /* pc_relative */
104 0, /* bitpos */
105 complain_overflow_bitfield, /* complain_on_overflow */
106 sh_reloc, /* special_function */
107 "r_imm32", /* name */
108 true, /* partial_inplace */
109 0xffffffff, /* src_mask */
110 0xffffffff, /* dst_mask */
111 false), /* pcrel_offset */
112
113 { 15 },
114 { 16 }, /* R_SH_IMM8 */
115 { 17 }, /* R_SH_IMM8BY2 */
116 { 18 }, /* R_SH_IMM8BY4 */
117 { 19 }, /* R_SH_IMM4 */
118 { 20 }, /* R_SH_IMM4BY2 */
119 { 21 }, /* R_SH_IMM4BY4 */
120
121 HOWTO (R_SH_PCRELIMM8BY2, /* type */
122 1, /* rightshift */
123 1, /* size (0 = byte, 1 = short, 2 = long) */
124 8, /* bitsize */
125 true, /* pc_relative */
126 0, /* bitpos */
127 complain_overflow_unsigned, /* complain_on_overflow */
128 sh_reloc, /* special_function */
129 "r_pcrelimm8by2", /* name */
130 true, /* partial_inplace */
131 0xff, /* src_mask */
132 0xff, /* dst_mask */
133 true), /* pcrel_offset */
134
135 HOWTO (R_SH_PCRELIMM8BY4, /* type */
136 2, /* rightshift */
137 1, /* size (0 = byte, 1 = short, 2 = long) */
138 8, /* bitsize */
139 true, /* pc_relative */
140 0, /* bitpos */
141 complain_overflow_unsigned, /* complain_on_overflow */
142 sh_reloc, /* special_function */
143 "r_pcrelimm8by4", /* name */
144 true, /* partial_inplace */
145 0xff, /* src_mask */
146 0xff, /* dst_mask */
147 true), /* pcrel_offset */
148
149 HOWTO (R_SH_IMM16, /* type */
150 0, /* rightshift */
151 1, /* size (0 = byte, 1 = short, 2 = long) */
152 16, /* bitsize */
153 false, /* pc_relative */
154 0, /* bitpos */
155 complain_overflow_bitfield, /* complain_on_overflow */
156 sh_reloc, /* special_function */
157 "r_imm16", /* name */
158 true, /* partial_inplace */
159 0xffff, /* src_mask */
160 0xffff, /* dst_mask */
161 false), /* pcrel_offset */
162
163 HOWTO (R_SH_SWITCH16, /* type */
164 0, /* rightshift */
165 1, /* size (0 = byte, 1 = short, 2 = long) */
166 16, /* bitsize */
167 false, /* pc_relative */
168 0, /* bitpos */
169 complain_overflow_bitfield, /* complain_on_overflow */
170 sh_reloc, /* special_function */
171 "r_switch16", /* name */
172 true, /* partial_inplace */
173 0xffff, /* src_mask */
174 0xffff, /* dst_mask */
175 false), /* pcrel_offset */
46dd0622 176
e0981b9b
ILT
177 HOWTO (R_SH_SWITCH32, /* type */
178 0, /* rightshift */
179 2, /* size (0 = byte, 1 = short, 2 = long) */
180 32, /* bitsize */
181 false, /* pc_relative */
182 0, /* bitpos */
183 complain_overflow_bitfield, /* complain_on_overflow */
184 sh_reloc, /* special_function */
185 "r_switch32", /* name */
186 true, /* partial_inplace */
187 0xffffffff, /* src_mask */
188 0xffffffff, /* dst_mask */
189 false), /* pcrel_offset */
46dd0622 190
e0981b9b
ILT
191 HOWTO (R_SH_USES, /* type */
192 0, /* rightshift */
193 1, /* size (0 = byte, 1 = short, 2 = long) */
194 16, /* bitsize */
195 false, /* pc_relative */
196 0, /* bitpos */
197 complain_overflow_bitfield, /* complain_on_overflow */
198 sh_reloc, /* special_function */
199 "r_uses", /* name */
200 true, /* partial_inplace */
201 0xffff, /* src_mask */
202 0xffff, /* dst_mask */
203 false), /* pcrel_offset */
204
205 HOWTO (R_SH_COUNT, /* type */
206 0, /* rightshift */
207 2, /* size (0 = byte, 1 = short, 2 = long) */
208 32, /* bitsize */
209 false, /* pc_relative */
210 0, /* bitpos */
211 complain_overflow_bitfield, /* complain_on_overflow */
212 sh_reloc, /* special_function */
213 "r_count", /* name */
214 true, /* partial_inplace */
215 0xffffffff, /* src_mask */
216 0xffffffff, /* dst_mask */
217 false), /* pcrel_offset */
218
219 HOWTO (R_SH_ALIGN, /* type */
220 0, /* rightshift */
221 2, /* size (0 = byte, 1 = short, 2 = long) */
222 32, /* bitsize */
223 false, /* pc_relative */
224 0, /* bitpos */
225 complain_overflow_bitfield, /* complain_on_overflow */
226 sh_reloc, /* special_function */
227 "r_align", /* name */
228 true, /* partial_inplace */
229 0xffffffff, /* src_mask */
230 0xffffffff, /* dst_mask */
231 false) /* pcrel_offset */
232};
233
234#define SH_COFF_HOWTO_COUNT (sizeof sh_coff_howtos / sizeof sh_coff_howtos[0])
235
236/* Check for a bad magic number. */
46dd0622 237#define BADMAG(x) SHBADMAG(x)
46dd0622 238
e0981b9b
ILT
239/* Customize coffcode.h (this is not currently used). */
240#define SH 1
241
242/* FIXME: This should not be set here. */
46dd0622
SC
243#define __A_MAGIC_SET__
244
e0981b9b
ILT
245/* Swap the r_offset field in and out. */
246#define SWAP_IN_RELOC_OFFSET bfd_h_get_32
247#define SWAP_OUT_RELOC_OFFSET bfd_h_put_32
248
249/* Swap out extra information in the reloc structure. */
250#define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
251 do \
252 { \
253 dst->r_stuff[0] = 'S'; \
254 dst->r_stuff[1] = 'C'; \
255 } \
256 while (0)
257
258/* Get the value of a symbol, when performing a relocation. */
46dd0622 259
cf345e36
SC
260static long
261get_symbol_value (symbol)
262 asymbol *symbol;
263{
e0981b9b 264 bfd_vma relocation;
cf345e36
SC
265
266 if (bfd_is_com_section (symbol->section))
cf345e36 267 relocation = 0;
cf345e36 268 else
e0981b9b
ILT
269 relocation = (symbol->value +
270 symbol->section->output_section->vma +
271 symbol->section->output_offset);
cf345e36 272
e0981b9b 273 return relocation;
cf345e36 274}
46dd0622 275
e0981b9b
ILT
276/* This macro is used in coffcode.h to get the howto corresponding to
277 an internal reloc. */
278
279#define RTYPE2HOWTO(relent, internal) \
280 ((relent)->howto = \
281 ((internal)->r_type < SH_COFF_HOWTO_COUNT \
282 ? &sh_coff_howtos[(internal)->r_type] \
283 : (reloc_howto_type *) NULL))
284
285/* This is the same as the macro in coffcode.h, except that it copies
286 r_offset into reloc_entry->addend for some relocs. */
287#define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
288 { \
289 coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
290 if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
291 coffsym = (obj_symbols (abfd) \
292 + (cache_ptr->sym_ptr_ptr - symbols)); \
293 else if (ptr) \
294 coffsym = coff_symbol_from (abfd, ptr); \
295 if (coffsym != (coff_symbol_type *) NULL \
296 && coffsym->native->u.syment.n_scnum == 0) \
297 cache_ptr->addend = 0; \
298 else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
299 && ptr->section != (asection *) NULL) \
300 cache_ptr->addend = - (ptr->section->vma + ptr->value); \
301 else \
302 cache_ptr->addend = 0; \
303 if ((reloc).r_type == R_SH_SWITCH16 \
304 || (reloc).r_type == R_SH_SWITCH32 \
305 || (reloc).r_type == R_SH_USES \
306 || (reloc).r_type == R_SH_COUNT \
307 || (reloc).r_type == R_SH_ALIGN) \
308 cache_ptr->addend = (reloc).r_offset; \
cf345e36
SC
309 }
310
e0981b9b 311/* This is the howto function for the SH relocations. */
cf345e36
SC
312
313static bfd_reloc_status_type
314sh_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
e0981b9b 315 error_message)
cf345e36
SC
316 bfd *abfd;
317 arelent *reloc_entry;
318 asymbol *symbol_in;
319 PTR data;
320 asection *input_section;
321 bfd *output_bfd;
322 char **error_message;
323{
cf345e36 324 unsigned long insn;
e0981b9b 325 bfd_vma sym_value;
cf345e36 326 unsigned short r_type;
e0981b9b
ILT
327 bfd_vma addr = reloc_entry->address;
328 bfd_byte *hit_data = addr + (bfd_byte *) data;
cf345e36 329
cf345e36
SC
330 r_type = reloc_entry->howto->type;
331
e0981b9b
ILT
332 if (output_bfd != NULL)
333 {
334 /* Partial linking--do nothing. */
335 reloc_entry->address += input_section->output_offset;
336 return bfd_reloc_ok;
337 }
338
339 /* Almost all relocs have to do with relaxing. If any work must be
340 done for them, it has been done in sh_relax_section. */
341 if (r_type != R_SH_IMM32
342 && (r_type != R_SH_PCDISP
343 || (symbol_in->flags & BSF_LOCAL) != 0))
cf345e36 344 return bfd_reloc_ok;
cf345e36
SC
345
346 if (symbol_in != NULL
347 && bfd_is_und_section (symbol_in->section))
e0981b9b 348 return bfd_reloc_undefined;
cf345e36 349
e0981b9b 350 sym_value = get_symbol_value (symbol_in);
cf345e36 351
e0981b9b 352 switch (r_type)
cf345e36
SC
353 {
354 case R_SH_IMM32:
e0981b9b
ILT
355 insn = bfd_get_32 (abfd, hit_data);
356 insn += sym_value + reloc_entry->addend;
357 bfd_put_32 (abfd, insn, hit_data);
358 break;
359 case R_SH_PCDISP:
360 insn = bfd_get_16 (abfd, hit_data);
361 sym_value += reloc_entry->addend;
362 sym_value -= (input_section->output_section->vma
363 + input_section->output_offset
364 + addr
365 + 4);
366 sym_value += (insn & 0xfff) << 1;
367 if (insn & 0x800)
368 sym_value -= 0x1000;
369 insn = (insn & 0xf000) | (sym_value & 0xfff);
370 bfd_put_16 (abfd, insn, hit_data);
ae115e51 371 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
e0981b9b 372 return bfd_reloc_overflow;
cf345e36
SC
373 break;
374 default:
e0981b9b
ILT
375 abort ();
376 break;
cf345e36
SC
377 }
378
e0981b9b
ILT
379 return bfd_reloc_ok;
380}
381
382/* We can do relaxing. */
383#define coff_bfd_relax_section sh_relax_section
384
385/* We use the special COFF backend linker. */
386#define coff_relocate_section sh_relocate_section
387
388/* When relaxing, we need to use special code to get the relocated
389 section contents. */
390#define coff_bfd_get_relocated_section_contents \
391 sh_coff_get_relocated_section_contents
392
393#include "coffcode.h"
394
395/* This function handles relaxing on the SH.
396
397 Function calls on the SH look like this:
398
399 movl L1,r0
400 ...
401 jsr @r0
402 ...
403 L1:
404 .long function
405
406 The compiler and assembler will cooperate to create R_SH_USES
407 relocs on the jsr instructions. The r_offset field of the
408 R_SH_USES reloc is the PC relative offset to the instruction which
409 loads the register (the r_offset field is computed as though it
410 were a jump instruction, so the offset value is actually from four
411 bytes past the instruction). The linker can use this reloc to
412 determine just which function is being called, and thus decide
413 whether it is possible to replace the jsr with a bsr.
414
415 If multiple function calls are all based on a single register load
416 (i.e., the same function is called multiple times), the compiler
417 guarantees that each function call will have an R_SH_USES reloc.
418 Therefore, if the linker is able to convert each R_SH_USES reloc
419 which refers to that address, it can safely eliminate the register
420 load.
421
422 When the assembler creates an R_SH_USES reloc, it examines it to
423 determine which address is being loaded (L1 in the above example).
424 It then counts the number of references to that address, and
425 creates an R_SH_COUNT reloc at that address. The r_offset field of
426 the R_SH_COUNT reloc will be the number of references. If the
427 linker is able to eliminate a register load, it can use the
428 R_SH_COUNT reloc to see whether it can also eliminate the function
429 address. */
430
431static boolean
432sh_relax_section (abfd, sec, link_info, again)
433 bfd *abfd;
434 asection *sec;
435 struct bfd_link_info *link_info;
436 boolean *again;
437{
438 struct internal_reloc *internal_relocs;
439 struct internal_reloc *free_relocs = NULL;
440 struct internal_reloc *irel, *irelend;
441 bfd_byte *contents = NULL;
442 bfd_byte *free_contents = NULL;
443
444 *again = false;
445
446 if (link_info->relocateable
447 || (sec->flags & SEC_RELOC) == 0
448 || sec->reloc_count == 0)
449 return true;
450
451 /* If this is the first time we have been called for this section,
452 initialize the cooked size. */
453 if (sec->_cooked_size == 0)
454 sec->_cooked_size = sec->_raw_size;
455
456 internal_relocs = (_bfd_coff_read_internal_relocs
457 (abfd, sec, link_info->keep_memory,
458 (bfd_byte *) NULL, false,
459 (struct internal_reloc *) NULL));
460 if (internal_relocs == NULL)
461 goto error_return;
462 if (! link_info->keep_memory)
463 free_relocs = internal_relocs;
464
465 irelend = internal_relocs + sec->reloc_count;
466 for (irel = internal_relocs; irel < irelend; irel++)
467 {
468 bfd_vma laddr, paddr, symval;
469 unsigned short insn;
470 struct internal_reloc *irelfn, *irelscan, *irelcount;
471 struct internal_syment sym;
472 bfd_signed_vma foff;
473
474 if (irel->r_type != R_SH_USES)
475 continue;
476
477 /* Get the section contents. */
478 if (contents == NULL)
479 {
480 if (coff_section_data (abfd, sec) != NULL
481 && coff_section_data (abfd, sec)->contents != NULL)
482 contents = coff_section_data (abfd, sec)->contents;
483 else
484 {
485 contents = (bfd_byte *) malloc (sec->_raw_size);
486 if (contents == NULL)
487 {
488 bfd_set_error (bfd_error_no_memory);
489 goto error_return;
490 }
491 free_contents = contents;
492
493 if (! bfd_get_section_contents (abfd, sec, contents,
494 (file_ptr) 0, sec->_raw_size))
495 goto error_return;
496 }
497 }
498
499 /* The r_offset field of the R_SH_USES reloc will point us to
500 the register load. The 4 is because the r_offset field is
501 computed as though it were a jump offset, which are based
502 from 4 bytes after the jump instruction. */
503 laddr = irel->r_vaddr - sec->vma + 4 + irel->r_offset;
504 if (laddr >= sec->_raw_size)
505 {
506 (*_bfd_error_handler) ("%s: 0x%lx: warning: bad R_SH_USES offset",
507 bfd_get_filename (abfd),
508 (unsigned long) irel->r_vaddr);
509 continue;
510 }
511 insn = bfd_get_16 (abfd, contents + laddr);
512
513 /* If the instruction is not mov.l NN,rN, we don't know what to
514 do. */
515 if ((insn & 0xf000) != 0xd000)
516 {
517 ((*_bfd_error_handler)
518 ("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x",
519 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr, insn));
520 continue;
521 }
522
523 /* Get the address from which the register is being loaded. The
524 displacement in the mov.l instruction is quadrupled. It is a
525 displacement from four bytes after the movl instruction, but,
526 before adding in the PC address, two least significant bits
527 of the PC are cleared. We assume that the section is aligned
528 on a four byte boundary. */
529 paddr = insn & 0xff;
530 paddr *= 4;
531 paddr += (laddr + 4) &~ 3;
532 if (paddr >= sec->_raw_size)
533 {
534 ((*_bfd_error_handler)
535 ("%s: 0x%lx: warning: bad R_SH_USES load offset",
536 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
537 continue;
538 }
539
540 /* Get the reloc for the address from which the register is
541 being loaded. This reloc will tell us which function is
542 actually being called. */
543 paddr += sec->vma;
544 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
545 if (irelfn->r_vaddr == paddr
546 && irelfn->r_type == R_SH_IMM32)
547 break;
548 if (irelfn >= irelend)
549 {
550 ((*_bfd_error_handler)
551 ("%s: 0x%lx: warning: could not find expected reloc",
552 bfd_get_filename (abfd), (unsigned long) paddr));
553 continue;
554 }
555
556 /* Get the value of the symbol referred to by the reloc. */
557 if (! _bfd_coff_get_external_symbols (abfd))
558 goto error_return;
559 bfd_coff_swap_sym_in (abfd,
560 ((bfd_byte *) obj_coff_external_syms (abfd)
561 + (irelfn->r_symndx
562 * bfd_coff_symesz (abfd))),
563 &sym);
564 if (sym.n_scnum != 0 && sym.n_scnum != sec->target_index)
565 {
566 ((*_bfd_error_handler)
567 ("%s: 0x%lx: warning: symbol in unexpected section",
568 bfd_get_filename (abfd), (unsigned long) paddr));
569 continue;
570 }
571
572 if (sym.n_sclass != C_EXT)
573 {
574 symval = (sym.n_value
575 - sec->vma
576 + sec->output_section->vma
577 + sec->output_offset);
578 }
579 else
580 {
581 struct coff_link_hash_entry *h;
cf345e36 582
e0981b9b
ILT
583 h = obj_coff_sym_hashes (abfd)[irelfn->r_symndx];
584 BFD_ASSERT (h != NULL);
585 if (h->root.type != bfd_link_hash_defined
586 && h->root.type != bfd_link_hash_defweak)
587 {
588 /* This appears to be a reference to an undefined
589 symbol. Just ignore it--it will be caught by the
590 regular reloc processing. */
591 continue;
592 }
593
594 symval = (h->root.u.def.value
595 + h->root.u.def.section->output_section->vma
596 + h->root.u.def.section->output_offset);
597 }
598
599 symval += bfd_get_32 (abfd, contents + paddr - sec->vma);
600
601 /* See if this function call can be shortened. */
602 foff = (symval
603 - (irel->r_vaddr
604 - sec->vma
605 + sec->output_section->vma
606 + sec->output_offset
607 + 4));
608 if (foff < -0x1000 || foff >= 0x1000)
609 {
610 /* After all that work, we can't shorten this function call. */
611 continue;
612 }
613
614 /* Shorten the function call. */
615
616 /* For simplicity of coding, we are going to modify the section
617 contents, the section relocs, and the BFD symbol table. We
618 must tell the rest of the code not to free up this
619 information. It would be possible to instead create a table
620 of changes which have to be made, as is done in coff-mips.c;
621 that would be more work, but would require less memory when
622 the linker is run. */
623
624 if (coff_section_data (abfd, sec) == NULL)
625 {
626 sec->used_by_bfd =
ae115e51 627 ((PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
e0981b9b
ILT
628 if (sec->used_by_bfd == NULL)
629 {
630 bfd_set_error (bfd_error_no_memory);
631 goto error_return;
632 }
633 }
634
635 coff_section_data (abfd, sec)->relocs = internal_relocs;
636 coff_section_data (abfd, sec)->keep_relocs = true;
637 free_relocs = NULL;
638
639 coff_section_data (abfd, sec)->contents = contents;
640 coff_section_data (abfd, sec)->keep_contents = true;
641 free_contents = NULL;
642
643 obj_coff_keep_syms (abfd) = true;
644
645 /* Replace the jsr with a bsr. */
646
647 /* Change the R_SH_USES reloc into an R_SH_PCDISP reloc, and
648 replace the jsr with a bsr. */
649 irel->r_type = R_SH_PCDISP;
650 irel->r_symndx = irelfn->r_symndx;
651 if (sym.n_sclass != C_EXT)
652 {
653 /* If this needs to be changed because of future relaxing,
654 it will be handled here like other internal PCDISP
655 relocs. */
656 bfd_put_16 (abfd,
657 0xb000 | ((foff >> 1) & 0xfff),
658 contents + irel->r_vaddr - sec->vma);
659 }
660 else
661 {
662 /* We can't fully resolve this yet, because the external
663 symbol value may be changed by future relaxing. We let
664 the final link phase handle it. */
665 bfd_put_16 (abfd, 0xb000, contents + irel->r_vaddr - sec->vma);
666 }
667
668 /* See if there is another R_SH_USES reloc referring to the same
669 register load. */
670 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
671 if (irelscan->r_type == R_SH_USES
672 && laddr == irelscan->r_vaddr - sec->vma + 4 + irelscan->r_offset)
673 break;
674 if (irelscan < irelend)
675 {
676 /* Some other function call depends upon this register load,
677 and we have not yet converted that function call.
678 Indeed, we may never be able to convert it. There is
679 nothing else we can do at this point. */
680 continue;
681 }
682
683 /* Look for a R_SH_COUNT reloc on the location where the
684 function address is stored. Do this before deleting any
685 bytes, to avoid confusion about the address. */
686 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
687 if (irelcount->r_vaddr == paddr
688 && irelcount->r_type == R_SH_COUNT)
689 break;
690
691 /* Delete the register load. */
692 if (! sh_relax_delete_bytes (abfd, sec, laddr, 2))
693 goto error_return;
694
695 /* That will change things, so, just in case it permits some
696 other function call to come within range, we should relax
697 again. Note that this is not required, and it may be slow. */
698 *again = true;
699
700 /* Now check whether we got a COUNT reloc. */
701 if (irelcount >= irelend)
702 {
703 ((*_bfd_error_handler)
704 ("%s: 0x%lx: warning: could not find expected COUNT reloc",
705 bfd_get_filename (abfd), (unsigned long) paddr));
706 continue;
707 }
708
709 /* The number of uses is stored in the r_offset field. We've
710 just deleted one. */
711 if (irelcount->r_offset == 0)
712 {
713 ((*_bfd_error_handler) ("%s: 0x%lx: warning: bad count",
714 bfd_get_filename (abfd),
715 (unsigned long) paddr));
716 continue;
717 }
718
719 --irelcount->r_offset;
720
721 /* If there are no more uses, we can delete the address. Reload
722 the address from irelfn, in case it was changed by the
723 previous call to sh_relax_delete_bytes. */
724 if (irelcount->r_offset == 0)
725 {
726 if (! sh_relax_delete_bytes (abfd, sec,
727 irelfn->r_vaddr - sec->vma, 4))
728 goto error_return;
729 }
730
731 /* We've done all we can with that function call. */
732 }
733
734 if (free_relocs != NULL)
735 {
736 free (free_relocs);
737 free_relocs = NULL;
738 }
739
740 if (free_contents != NULL)
741 {
742 if (! link_info->keep_memory)
743 free (free_contents);
744 else
745 {
746 /* Cache the section contents for coff_link_input_bfd. */
747 if (coff_section_data (abfd, sec) == NULL)
748 {
749 sec->used_by_bfd =
ae115e51 750 ((PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
e0981b9b
ILT
751 if (sec->used_by_bfd == NULL)
752 {
753 bfd_set_error (bfd_error_no_memory);
754 goto error_return;
755 }
756 coff_section_data (abfd, sec)->relocs = NULL;
757 }
758 coff_section_data (abfd, sec)->contents = contents;
759 }
760 }
761
762 return true;
763
764 error_return:
765 if (free_relocs != NULL)
766 free (free_relocs);
767 if (free_contents != NULL)
768 free (free_contents);
769 return false;
cf345e36
SC
770}
771
e0981b9b 772/* Delete some bytes from a section while relaxing. */
cf345e36
SC
773
774static boolean
e0981b9b
ILT
775sh_relax_delete_bytes (abfd, sec, addr, count)
776 bfd *abfd;
777 asection *sec;
778 bfd_vma addr;
779 int count;
780{
781 bfd_byte *contents;
782 struct internal_reloc *irel, *irelend;
783 struct internal_reloc *irelalign;
784 bfd_vma toaddr;
785 bfd_byte *esym, *esymend;
786 bfd_size_type symesz;
787 struct coff_link_hash_entry **sym_hash;
788 asection *o;
789
790 contents = coff_section_data (abfd, sec)->contents;
791
792 /* The deletion must stop at the next ALIGN reloc for an aligment
793 power larger than the number of bytes we are deleting. */
794
795 irelalign = NULL;
796 toaddr = sec->_cooked_size;
797
798 irel = coff_section_data (abfd, sec)->relocs;
799 irelend = irel + sec->reloc_count;
800 for (; irel < irelend; irel++)
801 {
802 if (irel->r_type == R_SH_ALIGN
803 && irel->r_vaddr - sec->vma > addr
804 && count < (1 << irel->r_offset))
805 {
806 irelalign = irel;
807 toaddr = irel->r_vaddr - sec->vma;
808 break;
809 }
810 }
811
812 /* Actually delete the bytes. */
813 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
814 if (irelalign == NULL)
815 sec->_cooked_size -= count;
816 else
817 memset (contents + toaddr - count, 0, count);
818
819 /* Adjust all the relocs. */
820 for (irel = coff_section_data (abfd, sec)->relocs; irel < irelend; irel++)
821 {
822 bfd_vma nraddr, start, stop;
823 int insn = 0;
824 struct internal_syment sym;
825 int off, adjust, oinsn;
826 bfd_signed_vma voff;
827 boolean overflow;
828
829 /* Get the new reloc address. */
830 nraddr = irel->r_vaddr - sec->vma;
831 if ((irel->r_vaddr - sec->vma > addr
832 && irel->r_vaddr - sec->vma < toaddr)
833 || (irel->r_type == R_SH_ALIGN
834 && irel->r_vaddr - sec->vma == toaddr))
835 nraddr -= count;
836
837 /* See if this reloc was for the bytes we have deleted, in which
838 case we no longer care about it. */
839 if (irel->r_vaddr - sec->vma >= addr
840 && irel->r_vaddr - sec->vma < addr + count
841 && irel->r_type != R_SH_ALIGN)
842 irel->r_type = R_SH_UNUSED;
843
844 /* If this is a PC relative reloc, see if the range it covers
845 includes the bytes we have deleted. */
846 switch (irel->r_type)
847 {
848 default:
849 break;
850
851 case R_SH_PCDISP8BY2:
852 case R_SH_PCDISP:
853 case R_SH_PCRELIMM8BY2:
854 case R_SH_PCRELIMM8BY4:
855 start = irel->r_vaddr - sec->vma;
856 insn = bfd_get_16 (abfd, contents + nraddr);
857 break;
858 }
859
860 switch (irel->r_type)
861 {
862 default:
863 start = stop = addr;
864 break;
865
866 case R_SH_IMM32:
867 /* If this reloc is against a symbol defined in this
868 section, and the symbol will not be adjusted below, we
869 must check the addend to see it will put the value in
870 range to be adjusted, and hence must be changed. */
871 bfd_coff_swap_sym_in (abfd,
872 ((bfd_byte *) obj_coff_external_syms (abfd)
873 + (irel->r_symndx
874 * bfd_coff_symesz (abfd))),
875 &sym);
876 if (sym.n_sclass != C_EXT
877 && sym.n_scnum == sec->target_index
ae115e51
ILT
878 && ((bfd_vma) sym.n_value <= addr
879 || (bfd_vma) sym.n_value >= toaddr))
e0981b9b
ILT
880 {
881 bfd_vma val;
882
883 val = bfd_get_32 (abfd, contents + nraddr);
884 val += sym.n_value;
885 if (val >= addr && val < toaddr)
886 bfd_put_32 (abfd, val - count, contents + nraddr);
887 }
888 start = stop = addr;
889 break;
890
891 case R_SH_PCDISP8BY2:
892 off = insn & 0xff;
893 if (off & 0x80)
894 off -= 0x100;
895 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
896 break;
897
898 case R_SH_PCDISP:
899 bfd_coff_swap_sym_in (abfd,
900 ((bfd_byte *) obj_coff_external_syms (abfd)
901 + (irel->r_symndx
902 * bfd_coff_symesz (abfd))),
903 &sym);
904 if (sym.n_sclass == C_EXT)
905 start = stop = addr;
906 else
907 {
908 off = insn & 0xfff;
909 if (off & 0x800)
910 off -= 0x1000;
911 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
912 }
913 break;
914
915 case R_SH_PCRELIMM8BY2:
916 off = insn & 0xff;
917 stop = start + 4 + off * 2;
918 break;
919
920 case R_SH_PCRELIMM8BY4:
921 off = insn & 0xff;
922 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
923 break;
924
925 case R_SH_SWITCH16:
926 case R_SH_SWITCH32:
927 /* These relocs types represent
928 .word L2-L1
929 The r_offset field holds the difference between the reloc
930 address and L1. That is the start of the reloc, and
931 adding in the contents gives us the top. We must adjust
932 both the r_offset field and the section contents. */
933
934 start = irel->r_vaddr - sec->vma;
935 stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_offset);
936
937 if (start > addr
938 && start < toaddr
939 && (stop <= addr || stop >= toaddr))
940 irel->r_offset += count;
941 else if (stop > addr
942 && stop < toaddr
943 && (start <= addr || start >= toaddr))
944 irel->r_offset -= count;
945
946 start = stop;
947
948 if (irel->r_type == R_SH_SWITCH16)
949 voff = bfd_get_signed_16 (abfd, contents + nraddr);
950 else
951 voff = bfd_get_signed_32 (abfd, contents + nraddr);
952 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
953
954 break;
955
956 case R_SH_USES:
957 start = irel->r_vaddr - sec->vma;
958 stop = (bfd_vma) ((bfd_signed_vma) start + (long) irel->r_offset);
959 break;
960 }
961
962 if (start > addr
963 && start < toaddr
964 && (stop <= addr || stop >= toaddr))
965 adjust = count;
966 else if (stop > addr
967 && stop < toaddr
968 && (start <= addr || start >= toaddr))
969 adjust = - count;
970 else
971 adjust = 0;
972
973 if (adjust != 0)
974 {
975 oinsn = insn;
976 overflow = false;
977 switch (irel->r_type)
978 {
979 default:
980 abort ();
981 break;
982
983 case R_SH_PCDISP8BY2:
984 case R_SH_PCRELIMM8BY2:
985 insn += adjust / 2;
986 if ((oinsn & 0xff00) != (insn & 0xff00))
987 overflow = true;
988 bfd_put_16 (abfd, insn, contents + nraddr);
989 break;
990
991 case R_SH_PCDISP:
992 insn += adjust / 2;
993 if ((oinsn & 0xf000) != (insn & 0xf000))
994 overflow = true;
995 bfd_put_16 (abfd, insn, contents + nraddr);
996 break;
997
998 case R_SH_PCRELIMM8BY4:
999 BFD_ASSERT (adjust == count || count >= 4);
1000 if (count >= 4)
1001 insn += adjust / 4;
1002 else
1003 {
1004 if ((irel->r_vaddr & 3) == 0)
1005 ++insn;
1006 }
1007 if ((oinsn & 0xff00) != (insn & 0xff00))
1008 overflow = true;
1009 bfd_put_16 (abfd, insn, contents + nraddr);
1010 break;
1011
1012 case R_SH_SWITCH16:
1013 voff += adjust;
1014 if (voff < - 0x8000 || voff >= 0x8000)
1015 overflow = true;
1016 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1017 break;
1018
1019 case R_SH_SWITCH32:
1020 voff += adjust;
1021 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1022 break;
1023
1024 case R_SH_USES:
1025 irel->r_offset += adjust;
1026 break;
1027 }
1028
1029 if (overflow)
1030 {
1031 ((*_bfd_error_handler)
1032 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1033 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
1034 bfd_set_error (bfd_error_bad_value);
1035 return false;
1036 }
1037 }
1038
1039 irel->r_vaddr = nraddr + sec->vma;
1040 }
1041
1042 /* Look through all the other sections. If there contain any IMM32
1043 relocs against internal symbols which we are not going to adjust
1044 below, we may need to adjust the addends. */
1045 for (o = abfd->sections; o != NULL; o = o->next)
1046 {
1047 struct internal_reloc *internal_relocs;
1048 struct internal_reloc *irelscan, *irelscanend;
1049 bfd_byte *ocontents;
1050
1051 if (o == sec
1052 || (o->flags & SEC_RELOC) == 0
1053 || o->reloc_count == 0)
1054 continue;
1055
1056 /* We always cache the relocs. Perhaps, if info->keep_memory is
1057 false, we should free them, if we are permitted to, when we
1058 leave sh_coff_relax_section. */
1059 internal_relocs = (_bfd_coff_read_internal_relocs
1060 (abfd, o, true, (bfd_byte *) NULL, false,
1061 (struct internal_reloc *) NULL));
1062 if (internal_relocs == NULL)
1063 return false;
1064
1065 ocontents = NULL;
1066 irelscanend = internal_relocs + o->reloc_count;
1067 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1068 {
1069 struct internal_syment sym;
1070
1071 if (irelscan->r_type != R_SH_IMM32)
1072 continue;
1073
1074 bfd_coff_swap_sym_in (abfd,
1075 ((bfd_byte *) obj_coff_external_syms (abfd)
1076 + (irelscan->r_symndx
1077 * bfd_coff_symesz (abfd))),
1078 &sym);
1079 if (sym.n_sclass != C_EXT
1080 && sym.n_scnum == sec->target_index
ae115e51
ILT
1081 && ((bfd_vma) sym.n_value <= addr
1082 || (bfd_vma) sym.n_value >= toaddr))
e0981b9b
ILT
1083 {
1084 bfd_vma val;
1085
1086 if (ocontents == NULL)
1087 {
1088 if (coff_section_data (abfd, o)->contents != NULL)
1089 ocontents = coff_section_data (abfd, o)->contents;
1090 else
1091 {
1092 /* We always cache the section contents.
1093 Perhaps, if info->keep_memory is false, we
1094 should free them, if we are permitted to,
1095 when we leave sh_coff_relax_section. */
1096 ocontents = (bfd_byte *) malloc (o->_raw_size);
1097 if (ocontents == NULL)
1098 {
1099 bfd_set_error (bfd_error_no_memory);
1100 return false;
1101 }
1102 if (! bfd_get_section_contents (abfd, o, ocontents,
1103 (file_ptr) 0,
1104 o->_raw_size))
1105 return false;
1106 coff_section_data (abfd, o)->contents = ocontents;
1107 }
1108 }
1109
1110 val = bfd_get_32 (abfd, ocontents + irelscan->r_vaddr - o->vma);
1111 val += sym.n_value;
1112 if (val >= addr && val < toaddr)
1113 bfd_put_32 (abfd, val - count,
1114 ocontents + irelscan->r_vaddr - o->vma);
1115
1116 coff_section_data (abfd, o)->keep_contents = true;
1117 }
1118 }
1119 }
1120
1121 /* Adjusting the internal symbols will not work if something has
1122 already retrieved the generic symbols. It would be possible to
1123 make this work by adjusting the generic symbols at the same time.
1124 However, this case should not arise in normal usage. */
1125 if (obj_symbols (abfd) != NULL
1126 || obj_raw_syments (abfd) != NULL)
1127 {
1128 ((*_bfd_error_handler)
1129 ("%s: fatal: generic symbols retrieved before relaxing",
1130 bfd_get_filename (abfd)));
1131 bfd_set_error (bfd_error_invalid_operation);
1132 return false;
1133 }
1134
1135 /* Adjust all the symbols. */
1136 sym_hash = obj_coff_sym_hashes (abfd);
1137 symesz = bfd_coff_symesz (abfd);
1138 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1139 esymend = esym + obj_raw_syment_count (abfd) * symesz;
1140 while (esym < esymend)
1141 {
1142 struct internal_syment isym;
1143
1144 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &isym);
1145
1146 if (isym.n_scnum == sec->target_index
ae115e51
ILT
1147 && (bfd_vma) isym.n_value > addr
1148 && (bfd_vma) isym.n_value < toaddr)
e0981b9b
ILT
1149 {
1150 isym.n_value -= count;
1151
1152 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
1153
1154 if (*sym_hash != NULL)
1155 {
1156 BFD_ASSERT ((*sym_hash)->root.type == bfd_link_hash_defined
1157 || (*sym_hash)->root.type == bfd_link_hash_defweak);
1158 BFD_ASSERT ((*sym_hash)->root.u.def.value >= addr
1159 && (*sym_hash)->root.u.def.value < toaddr);
1160 (*sym_hash)->root.u.def.value -= count;
1161 }
1162 }
1163
1164 esym += (isym.n_numaux + 1) * symesz;
1165 sym_hash += isym.n_numaux + 1;
1166 }
1167
1168 /* See if we can move the ALIGN reloc forward. We have adjusted
1169 r_vaddr for it already. */
1170 if (irelalign != NULL)
1171 {
1172 bfd_vma alignaddr;
1173
1174 alignaddr = BFD_ALIGN (irelalign->r_vaddr - sec->vma,
1175 1 << irelalign->r_offset);
1176 if (alignaddr != toaddr)
1177 {
1178 /* Tail recursion. */
1179 return sh_relax_delete_bytes (abfd, sec,
1180 irelalign->r_vaddr - sec->vma,
1181 1 << irelalign->r_offset);
1182 }
1183 }
1184
1185 return true;
1186}
1187
1188/* This is a modification of _bfd_coff_generic_relocate_section, which
1189 will handle SH relaxing. */
1190
1191static boolean
1192sh_relocate_section (output_bfd, info, input_bfd, input_section, contents,
1193 relocs, syms, sections)
cf345e36
SC
1194 bfd *output_bfd;
1195 struct bfd_link_info *info;
1196 bfd *input_bfd;
1197 asection *input_section;
1198 bfd_byte *contents;
1199 struct internal_reloc *relocs;
1200 struct internal_syment *syms;
1201 asection **sections;
1202{
1203 struct internal_reloc *rel;
1204 struct internal_reloc *relend;
1205
cf345e36
SC
1206 rel = relocs;
1207 relend = rel + input_section->reloc_count;
1208 for (; rel < relend; rel++)
1209 {
1210 long symndx;
cf345e36
SC
1211 struct coff_link_hash_entry *h;
1212 struct internal_syment *sym;
e0981b9b 1213 bfd_vma addend;
cf345e36 1214 bfd_vma val;
e0981b9b
ILT
1215 reloc_howto_type *howto;
1216 bfd_reloc_status_type rstat;
1217
1218 /* Almost all relocs have to do with relaxing. If any work must
1219 be done for them, it has been done in sh_relax_section. */
1220 if (rel->r_type != R_SH_IMM32
1221 && rel->r_type != R_SH_PCDISP)
1222 continue;
cf345e36
SC
1223
1224 symndx = rel->r_symndx;
cf345e36
SC
1225
1226 if (symndx == -1)
e0981b9b
ILT
1227 {
1228 h = NULL;
1229 sym = NULL;
1230 }
cf345e36 1231 else
e0981b9b
ILT
1232 {
1233 h = obj_coff_sym_hashes (input_bfd)[symndx];
1234 sym = syms + symndx;
1235 }
cf345e36 1236
e0981b9b
ILT
1237 if (sym != NULL && sym->n_scnum != 0)
1238 addend = - sym->n_value;
1239 else
1240 addend = 0;
cf345e36 1241
e0981b9b
ILT
1242 if (rel->r_type == R_SH_PCDISP)
1243 addend -= 4;
1244
1245 if (rel->r_type >= SH_COFF_HOWTO_COUNT)
1246 howto = NULL;
1247 else
1248 howto = &sh_coff_howtos[rel->r_type];
1249
1250 if (howto == NULL)
1251 {
1252 bfd_set_error (bfd_error_bad_value);
1253 return false;
1254 }
1255
1256 val = 0;
cf345e36
SC
1257
1258 if (h == NULL)
1259 {
e0981b9b
ILT
1260 asection *sec;
1261
1262 /* There is nothing to do for an internal PCDISP reloc. */
1263 if (rel->r_type == R_SH_PCDISP)
1264 continue;
1265
cf345e36 1266 if (symndx == -1)
e0981b9b
ILT
1267 {
1268 sec = bfd_abs_section_ptr;
1269 val = 0;
1270 }
cf345e36
SC
1271 else
1272 {
cf345e36 1273 sec = sections[symndx];
e0981b9b 1274 val = (sec->output_section->vma
cf345e36
SC
1275 + sec->output_offset
1276 + sym->n_value
cf345e36
SC
1277 - sec->vma);
1278 }
1279 }
1280 else
1281 {
e0981b9b
ILT
1282 if (h->root.type == bfd_link_hash_defined
1283 || h->root.type == bfd_link_hash_defweak)
cf345e36 1284 {
e0981b9b
ILT
1285 asection *sec;
1286
cf345e36
SC
1287 sec = h->root.u.def.section;
1288 val = (h->root.u.def.value
1289 + sec->output_section->vma
1290 + sec->output_offset);
1291 }
e0981b9b 1292 else if (! info->relocateable)
cf345e36
SC
1293 {
1294 if (! ((*info->callbacks->undefined_symbol)
1295 (info, h->root.root.string, input_bfd, input_section,
1296 rel->r_vaddr - input_section->vma)))
1297 return false;
1298 }
1299 }
1300
e0981b9b
ILT
1301 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
1302 contents,
1303 rel->r_vaddr - input_section->vma,
1304 val, addend);
1305
1306 switch (rstat)
cf345e36
SC
1307 {
1308 default:
e0981b9b
ILT
1309 abort ();
1310 case bfd_reloc_ok:
1311 break;
1312 case bfd_reloc_overflow:
cf345e36 1313 {
e0981b9b
ILT
1314 const char *name;
1315 char buf[SYMNMLEN + 1];
cf345e36 1316
e0981b9b
ILT
1317 if (symndx == -1)
1318 name = "*ABS*";
1319 else if (h != NULL)
1320 name = h->root.root.string;
1321 else if (sym->_n._n_n._n_zeroes == 0
1322 && sym->_n._n_n._n_offset != 0)
1323 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1324 else
1325 {
1326 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1327 buf[SYMNMLEN] = '\0';
1328 name = buf;
1329 }
cf345e36 1330
e0981b9b
ILT
1331 if (! ((*info->callbacks->reloc_overflow)
1332 (info, name, howto->name, (bfd_vma) 0, input_bfd,
1333 input_section, rel->r_vaddr - input_section->vma)))
1334 return false;
1335 }
cf345e36 1336 }
e0981b9b 1337 }
cf345e36
SC
1338
1339 return true;
1340}
46dd0622 1341
e0981b9b
ILT
1342/* This is a version of bfd_generic_get_relocated_section_contents
1343 which uses sh_relocate_section. */
1344
1345static bfd_byte *
1346sh_coff_get_relocated_section_contents (output_bfd, link_info, link_order,
1347 data, relocateable, symbols)
1348 bfd *output_bfd;
1349 struct bfd_link_info *link_info;
1350 struct bfd_link_order *link_order;
1351 bfd_byte *data;
1352 boolean relocateable;
1353 asymbol **symbols;
1354{
1355 asection *input_section = link_order->u.indirect.section;
1356 bfd *input_bfd = input_section->owner;
1357 asection **sections = NULL;
1358 struct internal_reloc *internal_relocs = NULL;
1359 struct internal_syment *internal_syms = NULL;
1360
1361 /* We only need to handle the case of relaxing, or of having a
1362 particular set of section contents, specially. */
1363 if (relocateable
1364 || coff_section_data (input_bfd, input_section) == NULL
1365 || coff_section_data (input_bfd, input_section)->contents == NULL)
1366 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1367 link_order, data,
1368 relocateable,
1369 symbols);
1370
1371 memcpy (data, coff_section_data (input_bfd, input_section)->contents,
1372 input_section->_raw_size);
1373
1374 if ((input_section->flags & SEC_RELOC) != 0
1375 && input_section->reloc_count > 0)
1376 {
1377 bfd_size_type symesz = bfd_coff_symesz (input_bfd);
1378 bfd_byte *esym, *esymend;
1379 struct internal_syment *isymp;
1380 asection **secpp;
1381
1382 if (! _bfd_coff_get_external_symbols (input_bfd))
1383 goto error_return;
1384
1385 internal_relocs = (_bfd_coff_read_internal_relocs
1386 (input_bfd, input_section, false, (bfd_byte *) NULL,
1387 false, (struct internal_reloc *) NULL));
1388 if (internal_relocs == NULL)
1389 goto error_return;
1390
1391 internal_syms = ((struct internal_syment *)
1392 malloc (obj_raw_syment_count (input_bfd)
1393 * sizeof (struct internal_syment)));
1394 if (internal_syms == NULL)
1395 {
1396 bfd_set_error (bfd_error_no_memory);
1397 goto error_return;
1398 }
1399
1400 sections = (asection **) malloc (obj_raw_syment_count (input_bfd)
1401 * sizeof (asection *));
1402 if (sections == NULL)
1403 {
1404 bfd_set_error (bfd_error_no_memory);
1405 goto error_return;
1406 }
1407
1408 isymp = internal_syms;
1409 secpp = sections;
1410 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1411 esymend = esym + obj_raw_syment_count (input_bfd) * symesz;
1412 while (esym < esymend)
1413 {
1414 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1415
1416 if (isymp->n_scnum != 0)
1417 *secpp = coff_section_from_bfd_index (input_bfd, isymp->n_scnum);
1418 else
1419 {
1420 if (isymp->n_value == 0)
1421 *secpp = bfd_und_section_ptr;
1422 else
1423 *secpp = bfd_com_section_ptr;
1424 }
1425
1426 esym += (isymp->n_numaux + 1) * symesz;
1427 secpp += isymp->n_numaux + 1;
1428 isymp += isymp->n_numaux + 1;
1429 }
1430
1431 if (! sh_relocate_section (output_bfd, link_info, input_bfd,
1432 input_section, data, internal_relocs,
1433 internal_syms, sections))
1434 goto error_return;
1435
1436 free (sections);
1437 sections = NULL;
1438 free (internal_syms);
1439 internal_syms = NULL;
1440 free (internal_relocs);
1441 internal_relocs = NULL;
1442 }
1443
1444 return data;
1445
1446 error_return:
1447 if (internal_relocs != NULL)
1448 free (internal_relocs);
1449 if (internal_syms != NULL)
1450 free (internal_syms);
1451 if (sections != NULL)
1452 free (sections);
1453 return NULL;
1454}
1455
1456/* The target vectors. */
46dd0622 1457
2f3508ad 1458const bfd_target shcoff_vec =
46dd0622
SC
1459{
1460 "coff-sh", /* name */
1461 bfd_target_coff_flavour,
1462 true, /* data byte order is big */
1463 true, /* header byte order is big */
1464
1465 (HAS_RELOC | EXEC_P | /* object flags */
1466 HAS_LINENO | HAS_DEBUG |
e0981b9b 1467 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
46dd0622 1468
e0981b9b 1469 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
46dd0622
SC
1470 '_', /* leading symbol underscore */
1471 '/', /* ar_pad_char */
1472 15, /* ar_max_namelen */
1473 2, /* minimum section alignment */
e0981b9b
ILT
1474 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1475 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1476 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1477 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1478 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1479 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
46dd0622
SC
1480
1481 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1482 bfd_generic_archive_p, _bfd_dummy_target},
1483 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1484 bfd_false},
e0981b9b 1485 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
46dd0622
SC
1486 _bfd_write_archive_contents, bfd_false},
1487
e0981b9b
ILT
1488 BFD_JUMP_TABLE_GENERIC (coff),
1489 BFD_JUMP_TABLE_COPY (coff),
1490 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1491 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1492 BFD_JUMP_TABLE_SYMBOLS (coff),
1493 BFD_JUMP_TABLE_RELOCS (coff),
1494 BFD_JUMP_TABLE_WRITE (coff),
1495 BFD_JUMP_TABLE_LINK (coff),
1496 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1497
1498 COFF_SWAP_TABLE,
46dd0622 1499};
cf345e36 1500
7a1d4567
SC
1501const bfd_target shlcoff_vec =
1502{
1503 "coff-shl", /* name */
1504 bfd_target_coff_flavour,
1505 false, /* data byte order is little */
1506 false, /* header byte order is little endian too*/
1507
1508 (HAS_RELOC | EXEC_P | /* object flags */
1509 HAS_LINENO | HAS_DEBUG |
e0981b9b 1510 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
7a1d4567 1511
e0981b9b 1512 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
7a1d4567
SC
1513 '_', /* leading symbol underscore */
1514 '/', /* ar_pad_char */
1515 15, /* ar_max_namelen */
1516 2, /* minimum section alignment */
e0981b9b
ILT
1517 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1518 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1519 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1520 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1521 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1522 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
1523
1524 /* Note that we use a special archive recognizer.
1525 This is so that we only use one archive format for both
1526 object file types */
7a1d4567 1527 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
6e3acf74 1528 _bfd_dummy_target, _bfd_dummy_target},
7a1d4567
SC
1529 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1530 bfd_false},
e0981b9b 1531 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
7a1d4567
SC
1532 _bfd_write_archive_contents, bfd_false},
1533
e0981b9b
ILT
1534 BFD_JUMP_TABLE_GENERIC (coff),
1535 BFD_JUMP_TABLE_COPY (coff),
1536 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1537 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1538 BFD_JUMP_TABLE_SYMBOLS (coff),
1539 BFD_JUMP_TABLE_RELOCS (coff),
1540 BFD_JUMP_TABLE_WRITE (coff),
1541 BFD_JUMP_TABLE_LINK (coff),
1542 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
7a1d4567 1543
e0981b9b
ILT
1544 COFF_SWAP_TABLE,
1545};
This page took 0.149497 seconds and 4 git commands to generate.