Forward relocs to output if -Bshared
[deliverable/binutils-gdb.git] / bfd / coff-sh.c
1 /* BFD back-end for Hitachi Super-H COFF binaries.
2 Copyright 1993, 1994, 1995 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
4 Written by Steve Chamberlain, <sac@cygnus.com>.
5
6 This file is part of BFD, the Binary File Descriptor library.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21
22 #include "bfd.h"
23 #include "sysdep.h"
24 #include "obstack.h"
25 #include "libbfd.h"
26 #include "bfdlink.h"
27 #include "coff/sh.h"
28 #include "coff/internal.h"
29 #include "libcoff.h"
30
31 /* Internal functions. */
32 static bfd_reloc_status_type sh_reloc
33 PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
34 static long get_symbol_value PARAMS ((asymbol *));
35 static boolean sh_relax_section
36 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
37 static boolean sh_relax_delete_bytes
38 PARAMS ((bfd *, asection *, bfd_vma, int));
39 static boolean sh_relocate_section
40 PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
41 struct internal_reloc *, struct internal_syment *, asection **));
42 static bfd_byte *sh_coff_get_relocated_section_contents
43 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
44 bfd_byte *, boolean, asymbol **));
45
46 /* Default section alignment to 2**2. */
47 #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER (2)
48
49 /* Generate long file names. */
50 #define COFF_LONG_FILENAMES
51
52 /* The supported relocations. There are a lot of relocations defined
53 in coff/internal.h which we do not expect to ever see. */
54 static 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 */
176
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 */
190
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. */
237 #define BADMAG(x) SHBADMAG(x)
238
239 /* Customize coffcode.h (this is not currently used). */
240 #define SH 1
241
242 /* FIXME: This should not be set here. */
243 #define __A_MAGIC_SET__
244
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. */
259
260 static long
261 get_symbol_value (symbol)
262 asymbol *symbol;
263 {
264 bfd_vma relocation;
265
266 if (bfd_is_com_section (symbol->section))
267 relocation = 0;
268 else
269 relocation = (symbol->value +
270 symbol->section->output_section->vma +
271 symbol->section->output_offset);
272
273 return relocation;
274 }
275
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; \
309 }
310
311 /* This is the howto function for the SH relocations. */
312
313 static bfd_reloc_status_type
314 sh_reloc (abfd, reloc_entry, symbol_in, data, input_section, output_bfd,
315 error_message)
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 {
324 unsigned long insn;
325 bfd_vma sym_value;
326 unsigned short r_type;
327 bfd_vma addr = reloc_entry->address;
328 bfd_byte *hit_data = addr + (bfd_byte *) data;
329
330 r_type = reloc_entry->howto->type;
331
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))
344 return bfd_reloc_ok;
345
346 if (symbol_in != NULL
347 && bfd_is_und_section (symbol_in->section))
348 return bfd_reloc_undefined;
349
350 sym_value = get_symbol_value (symbol_in);
351
352 switch (r_type)
353 {
354 case R_SH_IMM32:
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);
371 if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
372 return bfd_reloc_overflow;
373 break;
374 default:
375 abort ();
376 break;
377 }
378
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
431 static boolean
432 sh_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 *) bfd_malloc (sec->_raw_size);
486 if (contents == NULL)
487 goto error_return;
488 free_contents = contents;
489
490 if (! bfd_get_section_contents (abfd, sec, contents,
491 (file_ptr) 0, sec->_raw_size))
492 goto error_return;
493 }
494 }
495
496 /* The r_offset field of the R_SH_USES reloc will point us to
497 the register load. The 4 is because the r_offset field is
498 computed as though it were a jump offset, which are based
499 from 4 bytes after the jump instruction. */
500 laddr = irel->r_vaddr - sec->vma + 4 + irel->r_offset;
501 if (laddr >= sec->_raw_size)
502 {
503 (*_bfd_error_handler) ("%s: 0x%lx: warning: bad R_SH_USES offset",
504 bfd_get_filename (abfd),
505 (unsigned long) irel->r_vaddr);
506 continue;
507 }
508 insn = bfd_get_16 (abfd, contents + laddr);
509
510 /* If the instruction is not mov.l NN,rN, we don't know what to
511 do. */
512 if ((insn & 0xf000) != 0xd000)
513 {
514 ((*_bfd_error_handler)
515 ("%s: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x",
516 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr, insn));
517 continue;
518 }
519
520 /* Get the address from which the register is being loaded. The
521 displacement in the mov.l instruction is quadrupled. It is a
522 displacement from four bytes after the movl instruction, but,
523 before adding in the PC address, two least significant bits
524 of the PC are cleared. We assume that the section is aligned
525 on a four byte boundary. */
526 paddr = insn & 0xff;
527 paddr *= 4;
528 paddr += (laddr + 4) &~ 3;
529 if (paddr >= sec->_raw_size)
530 {
531 ((*_bfd_error_handler)
532 ("%s: 0x%lx: warning: bad R_SH_USES load offset",
533 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
534 continue;
535 }
536
537 /* Get the reloc for the address from which the register is
538 being loaded. This reloc will tell us which function is
539 actually being called. */
540 paddr += sec->vma;
541 for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
542 if (irelfn->r_vaddr == paddr
543 && irelfn->r_type == R_SH_IMM32)
544 break;
545 if (irelfn >= irelend)
546 {
547 ((*_bfd_error_handler)
548 ("%s: 0x%lx: warning: could not find expected reloc",
549 bfd_get_filename (abfd), (unsigned long) paddr));
550 continue;
551 }
552
553 /* Get the value of the symbol referred to by the reloc. */
554 if (! _bfd_coff_get_external_symbols (abfd))
555 goto error_return;
556 bfd_coff_swap_sym_in (abfd,
557 ((bfd_byte *) obj_coff_external_syms (abfd)
558 + (irelfn->r_symndx
559 * bfd_coff_symesz (abfd))),
560 &sym);
561 if (sym.n_scnum != 0 && sym.n_scnum != sec->target_index)
562 {
563 ((*_bfd_error_handler)
564 ("%s: 0x%lx: warning: symbol in unexpected section",
565 bfd_get_filename (abfd), (unsigned long) paddr));
566 continue;
567 }
568
569 if (sym.n_sclass != C_EXT)
570 {
571 symval = (sym.n_value
572 - sec->vma
573 + sec->output_section->vma
574 + sec->output_offset);
575 }
576 else
577 {
578 struct coff_link_hash_entry *h;
579
580 h = obj_coff_sym_hashes (abfd)[irelfn->r_symndx];
581 BFD_ASSERT (h != NULL);
582 if (h->root.type != bfd_link_hash_defined
583 && h->root.type != bfd_link_hash_defweak)
584 {
585 /* This appears to be a reference to an undefined
586 symbol. Just ignore it--it will be caught by the
587 regular reloc processing. */
588 continue;
589 }
590
591 symval = (h->root.u.def.value
592 + h->root.u.def.section->output_section->vma
593 + h->root.u.def.section->output_offset);
594 }
595
596 symval += bfd_get_32 (abfd, contents + paddr - sec->vma);
597
598 /* See if this function call can be shortened. */
599 foff = (symval
600 - (irel->r_vaddr
601 - sec->vma
602 + sec->output_section->vma
603 + sec->output_offset
604 + 4));
605 if (foff < -0x1000 || foff >= 0x1000)
606 {
607 /* After all that work, we can't shorten this function call. */
608 continue;
609 }
610
611 /* Shorten the function call. */
612
613 /* For simplicity of coding, we are going to modify the section
614 contents, the section relocs, and the BFD symbol table. We
615 must tell the rest of the code not to free up this
616 information. It would be possible to instead create a table
617 of changes which have to be made, as is done in coff-mips.c;
618 that would be more work, but would require less memory when
619 the linker is run. */
620
621 if (coff_section_data (abfd, sec) == NULL)
622 {
623 sec->used_by_bfd =
624 ((PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
625 if (sec->used_by_bfd == NULL)
626 goto error_return;
627 }
628
629 coff_section_data (abfd, sec)->relocs = internal_relocs;
630 coff_section_data (abfd, sec)->keep_relocs = true;
631 free_relocs = NULL;
632
633 coff_section_data (abfd, sec)->contents = contents;
634 coff_section_data (abfd, sec)->keep_contents = true;
635 free_contents = NULL;
636
637 obj_coff_keep_syms (abfd) = true;
638
639 /* Replace the jsr with a bsr. */
640
641 /* Change the R_SH_USES reloc into an R_SH_PCDISP reloc, and
642 replace the jsr with a bsr. */
643 irel->r_type = R_SH_PCDISP;
644 irel->r_symndx = irelfn->r_symndx;
645 if (sym.n_sclass != C_EXT)
646 {
647 /* If this needs to be changed because of future relaxing,
648 it will be handled here like other internal PCDISP
649 relocs. */
650 bfd_put_16 (abfd,
651 0xb000 | ((foff >> 1) & 0xfff),
652 contents + irel->r_vaddr - sec->vma);
653 }
654 else
655 {
656 /* We can't fully resolve this yet, because the external
657 symbol value may be changed by future relaxing. We let
658 the final link phase handle it. */
659 bfd_put_16 (abfd, 0xb000, contents + irel->r_vaddr - sec->vma);
660 }
661
662 /* See if there is another R_SH_USES reloc referring to the same
663 register load. */
664 for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
665 if (irelscan->r_type == R_SH_USES
666 && laddr == irelscan->r_vaddr - sec->vma + 4 + irelscan->r_offset)
667 break;
668 if (irelscan < irelend)
669 {
670 /* Some other function call depends upon this register load,
671 and we have not yet converted that function call.
672 Indeed, we may never be able to convert it. There is
673 nothing else we can do at this point. */
674 continue;
675 }
676
677 /* Look for a R_SH_COUNT reloc on the location where the
678 function address is stored. Do this before deleting any
679 bytes, to avoid confusion about the address. */
680 for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
681 if (irelcount->r_vaddr == paddr
682 && irelcount->r_type == R_SH_COUNT)
683 break;
684
685 /* Delete the register load. */
686 if (! sh_relax_delete_bytes (abfd, sec, laddr, 2))
687 goto error_return;
688
689 /* That will change things, so, just in case it permits some
690 other function call to come within range, we should relax
691 again. Note that this is not required, and it may be slow. */
692 *again = true;
693
694 /* Now check whether we got a COUNT reloc. */
695 if (irelcount >= irelend)
696 {
697 ((*_bfd_error_handler)
698 ("%s: 0x%lx: warning: could not find expected COUNT reloc",
699 bfd_get_filename (abfd), (unsigned long) paddr));
700 continue;
701 }
702
703 /* The number of uses is stored in the r_offset field. We've
704 just deleted one. */
705 if (irelcount->r_offset == 0)
706 {
707 ((*_bfd_error_handler) ("%s: 0x%lx: warning: bad count",
708 bfd_get_filename (abfd),
709 (unsigned long) paddr));
710 continue;
711 }
712
713 --irelcount->r_offset;
714
715 /* If there are no more uses, we can delete the address. Reload
716 the address from irelfn, in case it was changed by the
717 previous call to sh_relax_delete_bytes. */
718 if (irelcount->r_offset == 0)
719 {
720 if (! sh_relax_delete_bytes (abfd, sec,
721 irelfn->r_vaddr - sec->vma, 4))
722 goto error_return;
723 }
724
725 /* We've done all we can with that function call. */
726 }
727
728 if (free_relocs != NULL)
729 {
730 free (free_relocs);
731 free_relocs = NULL;
732 }
733
734 if (free_contents != NULL)
735 {
736 if (! link_info->keep_memory)
737 free (free_contents);
738 else
739 {
740 /* Cache the section contents for coff_link_input_bfd. */
741 if (coff_section_data (abfd, sec) == NULL)
742 {
743 sec->used_by_bfd =
744 ((PTR) bfd_zalloc (abfd, sizeof (struct coff_section_tdata)));
745 if (sec->used_by_bfd == NULL)
746 goto error_return;
747 coff_section_data (abfd, sec)->relocs = NULL;
748 }
749 coff_section_data (abfd, sec)->contents = contents;
750 }
751 }
752
753 return true;
754
755 error_return:
756 if (free_relocs != NULL)
757 free (free_relocs);
758 if (free_contents != NULL)
759 free (free_contents);
760 return false;
761 }
762
763 /* Delete some bytes from a section while relaxing. */
764
765 static boolean
766 sh_relax_delete_bytes (abfd, sec, addr, count)
767 bfd *abfd;
768 asection *sec;
769 bfd_vma addr;
770 int count;
771 {
772 bfd_byte *contents;
773 struct internal_reloc *irel, *irelend;
774 struct internal_reloc *irelalign;
775 bfd_vma toaddr;
776 bfd_byte *esym, *esymend;
777 bfd_size_type symesz;
778 struct coff_link_hash_entry **sym_hash;
779 asection *o;
780
781 contents = coff_section_data (abfd, sec)->contents;
782
783 /* The deletion must stop at the next ALIGN reloc for an aligment
784 power larger than the number of bytes we are deleting. */
785
786 irelalign = NULL;
787 toaddr = sec->_cooked_size;
788
789 irel = coff_section_data (abfd, sec)->relocs;
790 irelend = irel + sec->reloc_count;
791 for (; irel < irelend; irel++)
792 {
793 if (irel->r_type == R_SH_ALIGN
794 && irel->r_vaddr - sec->vma > addr
795 && count < (1 << irel->r_offset))
796 {
797 irelalign = irel;
798 toaddr = irel->r_vaddr - sec->vma;
799 break;
800 }
801 }
802
803 /* Actually delete the bytes. */
804 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
805 if (irelalign == NULL)
806 sec->_cooked_size -= count;
807 else
808 memset (contents + toaddr - count, 0, count);
809
810 /* Adjust all the relocs. */
811 for (irel = coff_section_data (abfd, sec)->relocs; irel < irelend; irel++)
812 {
813 bfd_vma nraddr, start, stop;
814 int insn = 0;
815 struct internal_syment sym;
816 int off, adjust, oinsn;
817 bfd_signed_vma voff;
818 boolean overflow;
819
820 /* Get the new reloc address. */
821 nraddr = irel->r_vaddr - sec->vma;
822 if ((irel->r_vaddr - sec->vma > addr
823 && irel->r_vaddr - sec->vma < toaddr)
824 || (irel->r_type == R_SH_ALIGN
825 && irel->r_vaddr - sec->vma == toaddr))
826 nraddr -= count;
827
828 /* See if this reloc was for the bytes we have deleted, in which
829 case we no longer care about it. */
830 if (irel->r_vaddr - sec->vma >= addr
831 && irel->r_vaddr - sec->vma < addr + count
832 && irel->r_type != R_SH_ALIGN)
833 irel->r_type = R_SH_UNUSED;
834
835 /* If this is a PC relative reloc, see if the range it covers
836 includes the bytes we have deleted. */
837 switch (irel->r_type)
838 {
839 default:
840 break;
841
842 case R_SH_PCDISP8BY2:
843 case R_SH_PCDISP:
844 case R_SH_PCRELIMM8BY2:
845 case R_SH_PCRELIMM8BY4:
846 start = irel->r_vaddr - sec->vma;
847 insn = bfd_get_16 (abfd, contents + nraddr);
848 break;
849 }
850
851 switch (irel->r_type)
852 {
853 default:
854 start = stop = addr;
855 break;
856
857 case R_SH_IMM32:
858 /* If this reloc is against a symbol defined in this
859 section, and the symbol will not be adjusted below, we
860 must check the addend to see it will put the value in
861 range to be adjusted, and hence must be changed. */
862 bfd_coff_swap_sym_in (abfd,
863 ((bfd_byte *) obj_coff_external_syms (abfd)
864 + (irel->r_symndx
865 * bfd_coff_symesz (abfd))),
866 &sym);
867 if (sym.n_sclass != C_EXT
868 && sym.n_scnum == sec->target_index
869 && ((bfd_vma) sym.n_value <= addr
870 || (bfd_vma) sym.n_value >= toaddr))
871 {
872 bfd_vma val;
873
874 val = bfd_get_32 (abfd, contents + nraddr);
875 val += sym.n_value;
876 if (val >= addr && val < toaddr)
877 bfd_put_32 (abfd, val - count, contents + nraddr);
878 }
879 start = stop = addr;
880 break;
881
882 case R_SH_PCDISP8BY2:
883 off = insn & 0xff;
884 if (off & 0x80)
885 off -= 0x100;
886 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
887 break;
888
889 case R_SH_PCDISP:
890 bfd_coff_swap_sym_in (abfd,
891 ((bfd_byte *) obj_coff_external_syms (abfd)
892 + (irel->r_symndx
893 * bfd_coff_symesz (abfd))),
894 &sym);
895 if (sym.n_sclass == C_EXT)
896 start = stop = addr;
897 else
898 {
899 off = insn & 0xfff;
900 if (off & 0x800)
901 off -= 0x1000;
902 stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
903 }
904 break;
905
906 case R_SH_PCRELIMM8BY2:
907 off = insn & 0xff;
908 stop = start + 4 + off * 2;
909 break;
910
911 case R_SH_PCRELIMM8BY4:
912 off = insn & 0xff;
913 stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
914 break;
915
916 case R_SH_SWITCH16:
917 case R_SH_SWITCH32:
918 /* These relocs types represent
919 .word L2-L1
920 The r_offset field holds the difference between the reloc
921 address and L1. That is the start of the reloc, and
922 adding in the contents gives us the top. We must adjust
923 both the r_offset field and the section contents. */
924
925 start = irel->r_vaddr - sec->vma;
926 stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_offset);
927
928 if (start > addr
929 && start < toaddr
930 && (stop <= addr || stop >= toaddr))
931 irel->r_offset += count;
932 else if (stop > addr
933 && stop < toaddr
934 && (start <= addr || start >= toaddr))
935 irel->r_offset -= count;
936
937 start = stop;
938
939 if (irel->r_type == R_SH_SWITCH16)
940 voff = bfd_get_signed_16 (abfd, contents + nraddr);
941 else
942 voff = bfd_get_signed_32 (abfd, contents + nraddr);
943 stop = (bfd_vma) ((bfd_signed_vma) start + voff);
944
945 break;
946
947 case R_SH_USES:
948 start = irel->r_vaddr - sec->vma;
949 stop = (bfd_vma) ((bfd_signed_vma) start + (long) irel->r_offset);
950 break;
951 }
952
953 if (start > addr
954 && start < toaddr
955 && (stop <= addr || stop >= toaddr))
956 adjust = count;
957 else if (stop > addr
958 && stop < toaddr
959 && (start <= addr || start >= toaddr))
960 adjust = - count;
961 else
962 adjust = 0;
963
964 if (adjust != 0)
965 {
966 oinsn = insn;
967 overflow = false;
968 switch (irel->r_type)
969 {
970 default:
971 abort ();
972 break;
973
974 case R_SH_PCDISP8BY2:
975 case R_SH_PCRELIMM8BY2:
976 insn += adjust / 2;
977 if ((oinsn & 0xff00) != (insn & 0xff00))
978 overflow = true;
979 bfd_put_16 (abfd, insn, contents + nraddr);
980 break;
981
982 case R_SH_PCDISP:
983 insn += adjust / 2;
984 if ((oinsn & 0xf000) != (insn & 0xf000))
985 overflow = true;
986 bfd_put_16 (abfd, insn, contents + nraddr);
987 break;
988
989 case R_SH_PCRELIMM8BY4:
990 BFD_ASSERT (adjust == count || count >= 4);
991 if (count >= 4)
992 insn += adjust / 4;
993 else
994 {
995 if ((irel->r_vaddr & 3) == 0)
996 ++insn;
997 }
998 if ((oinsn & 0xff00) != (insn & 0xff00))
999 overflow = true;
1000 bfd_put_16 (abfd, insn, contents + nraddr);
1001 break;
1002
1003 case R_SH_SWITCH16:
1004 voff += adjust;
1005 if (voff < - 0x8000 || voff >= 0x8000)
1006 overflow = true;
1007 bfd_put_signed_16 (abfd, voff, contents + nraddr);
1008 break;
1009
1010 case R_SH_SWITCH32:
1011 voff += adjust;
1012 bfd_put_signed_32 (abfd, voff, contents + nraddr);
1013 break;
1014
1015 case R_SH_USES:
1016 irel->r_offset += adjust;
1017 break;
1018 }
1019
1020 if (overflow)
1021 {
1022 ((*_bfd_error_handler)
1023 ("%s: 0x%lx: fatal: reloc overflow while relaxing",
1024 bfd_get_filename (abfd), (unsigned long) irel->r_vaddr));
1025 bfd_set_error (bfd_error_bad_value);
1026 return false;
1027 }
1028 }
1029
1030 irel->r_vaddr = nraddr + sec->vma;
1031 }
1032
1033 /* Look through all the other sections. If there contain any IMM32
1034 relocs against internal symbols which we are not going to adjust
1035 below, we may need to adjust the addends. */
1036 for (o = abfd->sections; o != NULL; o = o->next)
1037 {
1038 struct internal_reloc *internal_relocs;
1039 struct internal_reloc *irelscan, *irelscanend;
1040 bfd_byte *ocontents;
1041
1042 if (o == sec
1043 || (o->flags & SEC_RELOC) == 0
1044 || o->reloc_count == 0)
1045 continue;
1046
1047 /* We always cache the relocs. Perhaps, if info->keep_memory is
1048 false, we should free them, if we are permitted to, when we
1049 leave sh_coff_relax_section. */
1050 internal_relocs = (_bfd_coff_read_internal_relocs
1051 (abfd, o, true, (bfd_byte *) NULL, false,
1052 (struct internal_reloc *) NULL));
1053 if (internal_relocs == NULL)
1054 return false;
1055
1056 ocontents = NULL;
1057 irelscanend = internal_relocs + o->reloc_count;
1058 for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1059 {
1060 struct internal_syment sym;
1061
1062 if (irelscan->r_type != R_SH_IMM32)
1063 continue;
1064
1065 bfd_coff_swap_sym_in (abfd,
1066 ((bfd_byte *) obj_coff_external_syms (abfd)
1067 + (irelscan->r_symndx
1068 * bfd_coff_symesz (abfd))),
1069 &sym);
1070 if (sym.n_sclass != C_EXT
1071 && sym.n_scnum == sec->target_index
1072 && ((bfd_vma) sym.n_value <= addr
1073 || (bfd_vma) sym.n_value >= toaddr))
1074 {
1075 bfd_vma val;
1076
1077 if (ocontents == NULL)
1078 {
1079 if (coff_section_data (abfd, o)->contents != NULL)
1080 ocontents = coff_section_data (abfd, o)->contents;
1081 else
1082 {
1083 /* We always cache the section contents.
1084 Perhaps, if info->keep_memory is false, we
1085 should free them, if we are permitted to,
1086 when we leave sh_coff_relax_section. */
1087 ocontents = (bfd_byte *) bfd_malloc (o->_raw_size);
1088 if (ocontents == NULL)
1089 return false;
1090 if (! bfd_get_section_contents (abfd, o, ocontents,
1091 (file_ptr) 0,
1092 o->_raw_size))
1093 return false;
1094 coff_section_data (abfd, o)->contents = ocontents;
1095 }
1096 }
1097
1098 val = bfd_get_32 (abfd, ocontents + irelscan->r_vaddr - o->vma);
1099 val += sym.n_value;
1100 if (val >= addr && val < toaddr)
1101 bfd_put_32 (abfd, val - count,
1102 ocontents + irelscan->r_vaddr - o->vma);
1103
1104 coff_section_data (abfd, o)->keep_contents = true;
1105 }
1106 }
1107 }
1108
1109 /* Adjusting the internal symbols will not work if something has
1110 already retrieved the generic symbols. It would be possible to
1111 make this work by adjusting the generic symbols at the same time.
1112 However, this case should not arise in normal usage. */
1113 if (obj_symbols (abfd) != NULL
1114 || obj_raw_syments (abfd) != NULL)
1115 {
1116 ((*_bfd_error_handler)
1117 ("%s: fatal: generic symbols retrieved before relaxing",
1118 bfd_get_filename (abfd)));
1119 bfd_set_error (bfd_error_invalid_operation);
1120 return false;
1121 }
1122
1123 /* Adjust all the symbols. */
1124 sym_hash = obj_coff_sym_hashes (abfd);
1125 symesz = bfd_coff_symesz (abfd);
1126 esym = (bfd_byte *) obj_coff_external_syms (abfd);
1127 esymend = esym + obj_raw_syment_count (abfd) * symesz;
1128 while (esym < esymend)
1129 {
1130 struct internal_syment isym;
1131
1132 bfd_coff_swap_sym_in (abfd, (PTR) esym, (PTR) &isym);
1133
1134 if (isym.n_scnum == sec->target_index
1135 && (bfd_vma) isym.n_value > addr
1136 && (bfd_vma) isym.n_value < toaddr)
1137 {
1138 isym.n_value -= count;
1139
1140 bfd_coff_swap_sym_out (abfd, (PTR) &isym, (PTR) esym);
1141
1142 if (*sym_hash != NULL)
1143 {
1144 BFD_ASSERT ((*sym_hash)->root.type == bfd_link_hash_defined
1145 || (*sym_hash)->root.type == bfd_link_hash_defweak);
1146 BFD_ASSERT ((*sym_hash)->root.u.def.value >= addr
1147 && (*sym_hash)->root.u.def.value < toaddr);
1148 (*sym_hash)->root.u.def.value -= count;
1149 }
1150 }
1151
1152 esym += (isym.n_numaux + 1) * symesz;
1153 sym_hash += isym.n_numaux + 1;
1154 }
1155
1156 /* See if we can move the ALIGN reloc forward. We have adjusted
1157 r_vaddr for it already. */
1158 if (irelalign != NULL)
1159 {
1160 bfd_vma alignaddr;
1161
1162 alignaddr = BFD_ALIGN (irelalign->r_vaddr - sec->vma,
1163 1 << irelalign->r_offset);
1164 if (alignaddr != toaddr)
1165 {
1166 /* Tail recursion. */
1167 return sh_relax_delete_bytes (abfd, sec,
1168 irelalign->r_vaddr - sec->vma,
1169 1 << irelalign->r_offset);
1170 }
1171 }
1172
1173 return true;
1174 }
1175
1176 /* This is a modification of _bfd_coff_generic_relocate_section, which
1177 will handle SH relaxing. */
1178
1179 static boolean
1180 sh_relocate_section (output_bfd, info, input_bfd, input_section, contents,
1181 relocs, syms, sections)
1182 bfd *output_bfd;
1183 struct bfd_link_info *info;
1184 bfd *input_bfd;
1185 asection *input_section;
1186 bfd_byte *contents;
1187 struct internal_reloc *relocs;
1188 struct internal_syment *syms;
1189 asection **sections;
1190 {
1191 struct internal_reloc *rel;
1192 struct internal_reloc *relend;
1193
1194 rel = relocs;
1195 relend = rel + input_section->reloc_count;
1196 for (; rel < relend; rel++)
1197 {
1198 long symndx;
1199 struct coff_link_hash_entry *h;
1200 struct internal_syment *sym;
1201 bfd_vma addend;
1202 bfd_vma val;
1203 reloc_howto_type *howto;
1204 bfd_reloc_status_type rstat;
1205
1206 /* Almost all relocs have to do with relaxing. If any work must
1207 be done for them, it has been done in sh_relax_section. */
1208 if (rel->r_type != R_SH_IMM32
1209 && rel->r_type != R_SH_PCDISP)
1210 continue;
1211
1212 symndx = rel->r_symndx;
1213
1214 if (symndx == -1)
1215 {
1216 h = NULL;
1217 sym = NULL;
1218 }
1219 else
1220 {
1221 h = obj_coff_sym_hashes (input_bfd)[symndx];
1222 sym = syms + symndx;
1223 }
1224
1225 if (sym != NULL && sym->n_scnum != 0)
1226 addend = - sym->n_value;
1227 else
1228 addend = 0;
1229
1230 if (rel->r_type == R_SH_PCDISP)
1231 addend -= 4;
1232
1233 if (rel->r_type >= SH_COFF_HOWTO_COUNT)
1234 howto = NULL;
1235 else
1236 howto = &sh_coff_howtos[rel->r_type];
1237
1238 if (howto == NULL)
1239 {
1240 bfd_set_error (bfd_error_bad_value);
1241 return false;
1242 }
1243
1244 val = 0;
1245
1246 if (h == NULL)
1247 {
1248 asection *sec;
1249
1250 /* There is nothing to do for an internal PCDISP reloc. */
1251 if (rel->r_type == R_SH_PCDISP)
1252 continue;
1253
1254 if (symndx == -1)
1255 {
1256 sec = bfd_abs_section_ptr;
1257 val = 0;
1258 }
1259 else
1260 {
1261 sec = sections[symndx];
1262 val = (sec->output_section->vma
1263 + sec->output_offset
1264 + sym->n_value
1265 - sec->vma);
1266 }
1267 }
1268 else
1269 {
1270 if (h->root.type == bfd_link_hash_defined
1271 || h->root.type == bfd_link_hash_defweak)
1272 {
1273 asection *sec;
1274
1275 sec = h->root.u.def.section;
1276 val = (h->root.u.def.value
1277 + sec->output_section->vma
1278 + sec->output_offset);
1279 }
1280 else if (! info->relocateable)
1281 {
1282 if (! ((*info->callbacks->undefined_symbol)
1283 (info, h->root.root.string, input_bfd, input_section,
1284 rel->r_vaddr - input_section->vma)))
1285 return false;
1286 }
1287 }
1288
1289 rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
1290 contents,
1291 rel->r_vaddr - input_section->vma,
1292 val, addend);
1293
1294 switch (rstat)
1295 {
1296 default:
1297 abort ();
1298 case bfd_reloc_ok:
1299 break;
1300 case bfd_reloc_overflow:
1301 {
1302 const char *name;
1303 char buf[SYMNMLEN + 1];
1304
1305 if (symndx == -1)
1306 name = "*ABS*";
1307 else if (h != NULL)
1308 name = h->root.root.string;
1309 else if (sym->_n._n_n._n_zeroes == 0
1310 && sym->_n._n_n._n_offset != 0)
1311 name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
1312 else
1313 {
1314 strncpy (buf, sym->_n._n_name, SYMNMLEN);
1315 buf[SYMNMLEN] = '\0';
1316 name = buf;
1317 }
1318
1319 if (! ((*info->callbacks->reloc_overflow)
1320 (info, name, howto->name, (bfd_vma) 0, input_bfd,
1321 input_section, rel->r_vaddr - input_section->vma)))
1322 return false;
1323 }
1324 }
1325 }
1326
1327 return true;
1328 }
1329
1330 /* This is a version of bfd_generic_get_relocated_section_contents
1331 which uses sh_relocate_section. */
1332
1333 static bfd_byte *
1334 sh_coff_get_relocated_section_contents (output_bfd, link_info, link_order,
1335 data, relocateable, symbols)
1336 bfd *output_bfd;
1337 struct bfd_link_info *link_info;
1338 struct bfd_link_order *link_order;
1339 bfd_byte *data;
1340 boolean relocateable;
1341 asymbol **symbols;
1342 {
1343 asection *input_section = link_order->u.indirect.section;
1344 bfd *input_bfd = input_section->owner;
1345 asection **sections = NULL;
1346 struct internal_reloc *internal_relocs = NULL;
1347 struct internal_syment *internal_syms = NULL;
1348
1349 /* We only need to handle the case of relaxing, or of having a
1350 particular set of section contents, specially. */
1351 if (relocateable
1352 || coff_section_data (input_bfd, input_section) == NULL
1353 || coff_section_data (input_bfd, input_section)->contents == NULL)
1354 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1355 link_order, data,
1356 relocateable,
1357 symbols);
1358
1359 memcpy (data, coff_section_data (input_bfd, input_section)->contents,
1360 input_section->_raw_size);
1361
1362 if ((input_section->flags & SEC_RELOC) != 0
1363 && input_section->reloc_count > 0)
1364 {
1365 bfd_size_type symesz = bfd_coff_symesz (input_bfd);
1366 bfd_byte *esym, *esymend;
1367 struct internal_syment *isymp;
1368 asection **secpp;
1369
1370 if (! _bfd_coff_get_external_symbols (input_bfd))
1371 goto error_return;
1372
1373 internal_relocs = (_bfd_coff_read_internal_relocs
1374 (input_bfd, input_section, false, (bfd_byte *) NULL,
1375 false, (struct internal_reloc *) NULL));
1376 if (internal_relocs == NULL)
1377 goto error_return;
1378
1379 internal_syms = ((struct internal_syment *)
1380 bfd_malloc (obj_raw_syment_count (input_bfd)
1381 * sizeof (struct internal_syment)));
1382 if (internal_syms == NULL)
1383 goto error_return;
1384
1385 sections = (asection **) bfd_malloc (obj_raw_syment_count (input_bfd)
1386 * sizeof (asection *));
1387 if (sections == NULL)
1388 goto error_return;
1389
1390 isymp = internal_syms;
1391 secpp = sections;
1392 esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
1393 esymend = esym + obj_raw_syment_count (input_bfd) * symesz;
1394 while (esym < esymend)
1395 {
1396 bfd_coff_swap_sym_in (input_bfd, (PTR) esym, (PTR) isymp);
1397
1398 if (isymp->n_scnum != 0)
1399 *secpp = coff_section_from_bfd_index (input_bfd, isymp->n_scnum);
1400 else
1401 {
1402 if (isymp->n_value == 0)
1403 *secpp = bfd_und_section_ptr;
1404 else
1405 *secpp = bfd_com_section_ptr;
1406 }
1407
1408 esym += (isymp->n_numaux + 1) * symesz;
1409 secpp += isymp->n_numaux + 1;
1410 isymp += isymp->n_numaux + 1;
1411 }
1412
1413 if (! sh_relocate_section (output_bfd, link_info, input_bfd,
1414 input_section, data, internal_relocs,
1415 internal_syms, sections))
1416 goto error_return;
1417
1418 free (sections);
1419 sections = NULL;
1420 free (internal_syms);
1421 internal_syms = NULL;
1422 free (internal_relocs);
1423 internal_relocs = NULL;
1424 }
1425
1426 return data;
1427
1428 error_return:
1429 if (internal_relocs != NULL)
1430 free (internal_relocs);
1431 if (internal_syms != NULL)
1432 free (internal_syms);
1433 if (sections != NULL)
1434 free (sections);
1435 return NULL;
1436 }
1437
1438 /* The target vectors. */
1439
1440 const bfd_target shcoff_vec =
1441 {
1442 "coff-sh", /* name */
1443 bfd_target_coff_flavour,
1444 true, /* data byte order is big */
1445 true, /* header byte order is big */
1446
1447 (HAS_RELOC | EXEC_P | /* object flags */
1448 HAS_LINENO | HAS_DEBUG |
1449 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
1450
1451 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
1452 '_', /* leading symbol underscore */
1453 '/', /* ar_pad_char */
1454 15, /* ar_max_namelen */
1455 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1456 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1457 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
1458 bfd_getb64, bfd_getb_signed_64, bfd_putb64,
1459 bfd_getb32, bfd_getb_signed_32, bfd_putb32,
1460 bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
1461
1462 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1463 bfd_generic_archive_p, _bfd_dummy_target},
1464 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1465 bfd_false},
1466 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1467 _bfd_write_archive_contents, bfd_false},
1468
1469 BFD_JUMP_TABLE_GENERIC (coff),
1470 BFD_JUMP_TABLE_COPY (coff),
1471 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1472 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1473 BFD_JUMP_TABLE_SYMBOLS (coff),
1474 BFD_JUMP_TABLE_RELOCS (coff),
1475 BFD_JUMP_TABLE_WRITE (coff),
1476 BFD_JUMP_TABLE_LINK (coff),
1477 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1478
1479 COFF_SWAP_TABLE,
1480 };
1481
1482 const bfd_target shlcoff_vec =
1483 {
1484 "coff-shl", /* name */
1485 bfd_target_coff_flavour,
1486 false, /* data byte order is little */
1487 false, /* header byte order is little endian too*/
1488
1489 (HAS_RELOC | EXEC_P | /* object flags */
1490 HAS_LINENO | HAS_DEBUG |
1491 HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
1492
1493 (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
1494 '_', /* leading symbol underscore */
1495 '/', /* ar_pad_char */
1496 15, /* ar_max_namelen */
1497 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1498 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1499 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
1500 bfd_getl64, bfd_getl_signed_64, bfd_putl64,
1501 bfd_getl32, bfd_getl_signed_32, bfd_putl32,
1502 bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
1503
1504 {_bfd_dummy_target, coff_object_p, /* bfd_check_format */
1505 bfd_generic_archive_p, _bfd_dummy_target},
1506 {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
1507 bfd_false},
1508 {bfd_false, coff_write_object_contents, /* bfd_write_contents */
1509 _bfd_write_archive_contents, bfd_false},
1510
1511 BFD_JUMP_TABLE_GENERIC (coff),
1512 BFD_JUMP_TABLE_COPY (coff),
1513 BFD_JUMP_TABLE_CORE (_bfd_nocore),
1514 BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
1515 BFD_JUMP_TABLE_SYMBOLS (coff),
1516 BFD_JUMP_TABLE_RELOCS (coff),
1517 BFD_JUMP_TABLE_WRITE (coff),
1518 BFD_JUMP_TABLE_LINK (coff),
1519 BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
1520
1521 COFF_SWAP_TABLE,
1522 };
This page took 0.066376 seconds and 4 git commands to generate.