1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
25 #include "elf/mn10300.h"
27 static bfd_reloc_status_type mn10300_elf_final_link_relocate
28 PARAMS ((reloc_howto_type
*, bfd
*, bfd
*, asection
*, bfd_byte
*,
29 bfd_vma
, bfd_vma
, bfd_vma
,
30 struct elf_link_hash_entry
*, unsigned long, struct bfd_link_info
*,
32 static bfd_boolean mn10300_elf_relocate_section
33 PARAMS ((bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
34 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**));
35 static bfd_boolean mn10300_elf_relax_section
36 PARAMS ((bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*));
37 static bfd_byte
* mn10300_elf_get_relocated_section_contents
38 PARAMS ((bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
39 bfd_byte
*, bfd_boolean
, asymbol
**));
40 static unsigned long elf_mn10300_mach
42 void _bfd_mn10300_elf_final_write_processing
43 PARAMS ((bfd
*, bfd_boolean
));
44 bfd_boolean _bfd_mn10300_elf_object_p
46 bfd_boolean _bfd_mn10300_elf_merge_private_bfd_data
47 PARAMS ((bfd
*,bfd
*));
49 /* The mn10300 linker needs to keep track of the number of relocs that
50 it decides to copy in check_relocs for each symbol. This is so
51 that it can discard PC relative relocs if it doesn't need them when
52 linking with -Bsymbolic. We store the information in a field
53 extending the regular ELF linker hash table. */
55 /* This structure keeps track of the number of PC relative relocs we
56 have copied for a given symbol. */
58 struct elf_mn10300_pcrel_relocs_copied
61 struct elf_mn10300_pcrel_relocs_copied
* next
;
62 /* A section in dynobj. */
64 /* Number of relocs copied in this section. */
68 struct elf32_mn10300_link_hash_entry
{
69 /* The basic elf link hash table entry. */
70 struct elf_link_hash_entry root
;
72 /* For function symbols, the number of times this function is
73 called directly (ie by name). */
74 unsigned int direct_calls
;
76 /* For function symbols, the size of this function's stack
77 (if <= 255 bytes). We stuff this into "call" instructions
78 to this target when it's valid and profitable to do so.
80 This does not include stack allocated by movm! */
81 unsigned char stack_size
;
83 /* For function symbols, arguments (if any) for movm instruction
84 in the prologue. We stuff this value into "call" instructions
85 to the target when it's valid and profitable to do so. */
86 unsigned char movm_args
;
88 /* For funtion symbols, the amount of stack space that would be allocated
89 by the movm instruction. This is redundant with movm_args, but we
90 add it to the hash table to avoid computing it over and over. */
91 unsigned char movm_stack_size
;
93 /* Number of PC relative relocs copied for this symbol. */
94 struct elf_mn10300_pcrel_relocs_copied
* pcrel_relocs_copied
;
96 /* When set, convert all "call" instructions to this target into "calls"
98 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
100 /* Used to mark functions which have had redundant parts of their
102 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
106 /* We derive a hash table from the main elf linker hash table so
107 we can store state variables and a secondary hash table without
108 resorting to global variables. */
109 struct elf32_mn10300_link_hash_table
{
110 /* The main hash table. */
111 struct elf_link_hash_table root
;
113 /* A hash table for static functions. We could derive a new hash table
114 instead of using the full elf32_mn10300_link_hash_table if we wanted
115 to save some memory. */
116 struct elf32_mn10300_link_hash_table
*static_hash_table
;
118 /* Random linker state flags. */
119 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
123 /* For MN10300 linker hash table. */
125 /* Get the MN10300 ELF linker hash table from a link_info structure. */
127 #define elf32_mn10300_hash_table(p) \
128 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
130 #define elf32_mn10300_link_hash_traverse(table, func, info) \
131 (elf_link_hash_traverse \
133 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
136 static struct bfd_hash_entry
*elf32_mn10300_link_hash_newfunc
137 PARAMS ((struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *));
138 static struct bfd_link_hash_table
*elf32_mn10300_link_hash_table_create
140 static void elf32_mn10300_link_hash_table_free
141 PARAMS ((struct bfd_link_hash_table
*));
143 static reloc_howto_type
*bfd_elf32_bfd_reloc_type_lookup
144 PARAMS ((bfd
*abfd
, bfd_reloc_code_real_type code
));
145 static void mn10300_info_to_howto
146 PARAMS ((bfd
*, arelent
*, Elf_Internal_Rela
*));
147 static bfd_boolean mn10300_elf_check_relocs
148 PARAMS ((bfd
*, struct bfd_link_info
*, asection
*,
149 const Elf_Internal_Rela
*));
150 static asection
*mn10300_elf_gc_mark_hook
151 PARAMS ((asection
*, struct bfd_link_info
*info
, Elf_Internal_Rela
*,
152 struct elf_link_hash_entry
*, Elf_Internal_Sym
*));
153 static bfd_boolean mn10300_elf_relax_delete_bytes
154 PARAMS ((bfd
*, asection
*, bfd_vma
, int));
155 static bfd_boolean mn10300_elf_symbol_address_p
156 PARAMS ((bfd
*, asection
*, Elf_Internal_Sym
*, bfd_vma
));
157 static bfd_boolean elf32_mn10300_finish_hash_table_entry
158 PARAMS ((struct bfd_hash_entry
*, PTR
));
159 static void compute_function_info
160 PARAMS ((bfd
*, struct elf32_mn10300_link_hash_entry
*,
161 bfd_vma
, unsigned char *));
163 static bfd_boolean _bfd_mn10300_elf_create_got_section
164 PARAMS ((bfd
*, struct bfd_link_info
*));
165 static bfd_boolean _bfd_mn10300_elf_create_dynamic_sections
166 PARAMS ((bfd
*, struct bfd_link_info
*));
167 static bfd_boolean _bfd_mn10300_elf_adjust_dynamic_symbol
168 PARAMS ((struct bfd_link_info
*, struct elf_link_hash_entry
*));
169 static bfd_boolean _bfd_mn10300_elf_discard_copies
170 PARAMS ((struct elf32_mn10300_link_hash_entry
*,
171 struct bfd_link_info
*));
172 static bfd_boolean _bfd_mn10300_elf_size_dynamic_sections
173 PARAMS ((bfd
*, struct bfd_link_info
*));
174 static bfd_boolean _bfd_mn10300_elf_finish_dynamic_symbol
175 PARAMS ((bfd
*, struct bfd_link_info
*, struct elf_link_hash_entry
*,
176 Elf_Internal_Sym
*));
177 static bfd_boolean _bfd_mn10300_elf_finish_dynamic_sections
178 PARAMS ((bfd
*, struct bfd_link_info
*));
180 static reloc_howto_type elf_mn10300_howto_table
[] = {
181 /* Dummy relocation. Does nothing. */
182 HOWTO (R_MN10300_NONE
,
188 complain_overflow_bitfield
,
189 bfd_elf_generic_reloc
,
195 /* Standard 32 bit reloc. */
202 complain_overflow_bitfield
,
203 bfd_elf_generic_reloc
,
209 /* Standard 16 bit reloc. */
216 complain_overflow_bitfield
,
217 bfd_elf_generic_reloc
,
223 /* Standard 8 bit reloc. */
230 complain_overflow_bitfield
,
231 bfd_elf_generic_reloc
,
237 /* Standard 32bit pc-relative reloc. */
238 HOWTO (R_MN10300_PCREL32
,
244 complain_overflow_bitfield
,
245 bfd_elf_generic_reloc
,
251 /* Standard 16bit pc-relative reloc. */
252 HOWTO (R_MN10300_PCREL16
,
258 complain_overflow_bitfield
,
259 bfd_elf_generic_reloc
,
265 /* Standard 8 pc-relative reloc. */
266 HOWTO (R_MN10300_PCREL8
,
272 complain_overflow_bitfield
,
273 bfd_elf_generic_reloc
,
280 /* GNU extension to record C++ vtable hierarchy */
281 HOWTO (R_MN10300_GNU_VTINHERIT
, /* type */
283 0, /* size (0 = byte, 1 = short, 2 = long) */
285 FALSE
, /* pc_relative */
287 complain_overflow_dont
, /* complain_on_overflow */
288 NULL
, /* special_function */
289 "R_MN10300_GNU_VTINHERIT", /* name */
290 FALSE
, /* partial_inplace */
293 FALSE
), /* pcrel_offset */
295 /* GNU extension to record C++ vtable member usage */
296 HOWTO (R_MN10300_GNU_VTENTRY
, /* type */
298 0, /* size (0 = byte, 1 = short, 2 = long) */
300 FALSE
, /* pc_relative */
302 complain_overflow_dont
, /* complain_on_overflow */
303 NULL
, /* special_function */
304 "R_MN10300_GNU_VTENTRY", /* name */
305 FALSE
, /* partial_inplace */
308 FALSE
), /* pcrel_offset */
310 /* Standard 24 bit reloc. */
317 complain_overflow_bitfield
,
318 bfd_elf_generic_reloc
,
324 HOWTO (R_MN10300_GOTPC32
, /* type */
326 2, /* size (0 = byte, 1 = short, 2 = long) */
328 TRUE
, /* pc_relative */
330 complain_overflow_bitfield
, /* complain_on_overflow */
331 bfd_elf_generic_reloc
, /* */
332 "R_MN10300_GOTPC32", /* name */
333 FALSE
, /* partial_inplace */
334 0xffffffff, /* src_mask */
335 0xffffffff, /* dst_mask */
336 TRUE
), /* pcrel_offset */
338 HOWTO (R_MN10300_GOTPC16
, /* type */
340 1, /* size (0 = byte, 1 = short, 2 = long) */
342 TRUE
, /* pc_relative */
344 complain_overflow_bitfield
, /* complain_on_overflow */
345 bfd_elf_generic_reloc
, /* */
346 "R_MN10300_GOTPC16", /* name */
347 FALSE
, /* partial_inplace */
348 0xffff, /* src_mask */
349 0xffff, /* dst_mask */
350 TRUE
), /* pcrel_offset */
352 HOWTO (R_MN10300_GOTOFF32
, /* type */
354 2, /* size (0 = byte, 1 = short, 2 = long) */
356 FALSE
, /* pc_relative */
358 complain_overflow_bitfield
, /* complain_on_overflow */
359 bfd_elf_generic_reloc
, /* */
360 "R_MN10300_GOTOFF32", /* name */
361 FALSE
, /* partial_inplace */
362 0xffffffff, /* src_mask */
363 0xffffffff, /* dst_mask */
364 FALSE
), /* pcrel_offset */
366 HOWTO (R_MN10300_GOTOFF24
, /* type */
368 2, /* size (0 = byte, 1 = short, 2 = long) */
370 FALSE
, /* pc_relative */
372 complain_overflow_bitfield
, /* complain_on_overflow */
373 bfd_elf_generic_reloc
, /* */
374 "R_MN10300_GOTOFF24", /* name */
375 FALSE
, /* partial_inplace */
376 0xffffff, /* src_mask */
377 0xffffff, /* dst_mask */
378 FALSE
), /* pcrel_offset */
380 HOWTO (R_MN10300_GOTOFF16
, /* type */
382 1, /* size (0 = byte, 1 = short, 2 = long) */
384 FALSE
, /* pc_relative */
386 complain_overflow_bitfield
, /* complain_on_overflow */
387 bfd_elf_generic_reloc
, /* */
388 "R_MN10300_GOTOFF16", /* name */
389 FALSE
, /* partial_inplace */
390 0xffff, /* src_mask */
391 0xffff, /* dst_mask */
392 FALSE
), /* pcrel_offset */
394 HOWTO (R_MN10300_PLT32
, /* type */
396 2, /* size (0 = byte, 1 = short, 2 = long) */
398 TRUE
, /* pc_relative */
400 complain_overflow_bitfield
, /* complain_on_overflow */
401 bfd_elf_generic_reloc
, /* */
402 "R_MN10300_PLT32", /* name */
403 FALSE
, /* partial_inplace */
404 0xffffffff, /* src_mask */
405 0xffffffff, /* dst_mask */
406 TRUE
), /* pcrel_offset */
408 HOWTO (R_MN10300_PLT16
, /* type */
410 1, /* size (0 = byte, 1 = short, 2 = long) */
412 TRUE
, /* pc_relative */
414 complain_overflow_bitfield
, /* complain_on_overflow */
415 bfd_elf_generic_reloc
, /* */
416 "R_MN10300_PLT16", /* name */
417 FALSE
, /* partial_inplace */
418 0xffff, /* src_mask */
419 0xffff, /* dst_mask */
420 TRUE
), /* pcrel_offset */
422 HOWTO (R_MN10300_GOT32
, /* type */
424 2, /* size (0 = byte, 1 = short, 2 = long) */
426 FALSE
, /* pc_relative */
428 complain_overflow_bitfield
, /* complain_on_overflow */
429 bfd_elf_generic_reloc
, /* */
430 "R_MN10300_GOT32", /* name */
431 FALSE
, /* partial_inplace */
432 0xffffffff, /* src_mask */
433 0xffffffff, /* dst_mask */
434 FALSE
), /* pcrel_offset */
436 HOWTO (R_MN10300_GOT24
, /* type */
438 2, /* size (0 = byte, 1 = short, 2 = long) */
440 FALSE
, /* pc_relative */
442 complain_overflow_bitfield
, /* complain_on_overflow */
443 bfd_elf_generic_reloc
, /* */
444 "R_MN10300_GOT24", /* name */
445 FALSE
, /* partial_inplace */
446 0xffffffff, /* src_mask */
447 0xffffffff, /* dst_mask */
448 FALSE
), /* pcrel_offset */
450 HOWTO (R_MN10300_GOT16
, /* type */
452 1, /* size (0 = byte, 1 = short, 2 = long) */
454 FALSE
, /* pc_relative */
456 complain_overflow_bitfield
, /* complain_on_overflow */
457 bfd_elf_generic_reloc
, /* */
458 "R_MN10300_GOT16", /* name */
459 FALSE
, /* partial_inplace */
460 0xffffffff, /* src_mask */
461 0xffffffff, /* dst_mask */
462 FALSE
), /* pcrel_offset */
464 HOWTO (R_MN10300_COPY
, /* type */
466 2, /* size (0 = byte, 1 = short, 2 = long) */
468 FALSE
, /* pc_relative */
470 complain_overflow_bitfield
, /* complain_on_overflow */
471 bfd_elf_generic_reloc
, /* */
472 "R_MN10300_COPY", /* name */
473 FALSE
, /* partial_inplace */
474 0xffffffff, /* src_mask */
475 0xffffffff, /* dst_mask */
476 FALSE
), /* pcrel_offset */
478 HOWTO (R_MN10300_GLOB_DAT
, /* type */
480 2, /* size (0 = byte, 1 = short, 2 = long) */
482 FALSE
, /* pc_relative */
484 complain_overflow_bitfield
, /* complain_on_overflow */
485 bfd_elf_generic_reloc
, /* */
486 "R_MN10300_GLOB_DAT", /* name */
487 FALSE
, /* partial_inplace */
488 0xffffffff, /* src_mask */
489 0xffffffff, /* dst_mask */
490 FALSE
), /* pcrel_offset */
492 HOWTO (R_MN10300_JMP_SLOT
, /* type */
494 2, /* size (0 = byte, 1 = short, 2 = long) */
496 FALSE
, /* pc_relative */
498 complain_overflow_bitfield
, /* complain_on_overflow */
499 bfd_elf_generic_reloc
, /* */
500 "R_MN10300_JMP_SLOT", /* name */
501 FALSE
, /* partial_inplace */
502 0xffffffff, /* src_mask */
503 0xffffffff, /* dst_mask */
504 FALSE
), /* pcrel_offset */
506 HOWTO (R_MN10300_RELATIVE
, /* type */
508 2, /* size (0 = byte, 1 = short, 2 = long) */
510 FALSE
, /* pc_relative */
512 complain_overflow_bitfield
, /* complain_on_overflow */
513 bfd_elf_generic_reloc
, /* */
514 "R_MN10300_RELATIVE", /* name */
515 FALSE
, /* partial_inplace */
516 0xffffffff, /* src_mask */
517 0xffffffff, /* dst_mask */
518 FALSE
), /* pcrel_offset */
522 struct mn10300_reloc_map
{
523 bfd_reloc_code_real_type bfd_reloc_val
;
524 unsigned char elf_reloc_val
;
527 static const struct mn10300_reloc_map mn10300_reloc_map
[] = {
528 { BFD_RELOC_NONE
, R_MN10300_NONE
, },
529 { BFD_RELOC_32
, R_MN10300_32
, },
530 { BFD_RELOC_16
, R_MN10300_16
, },
531 { BFD_RELOC_8
, R_MN10300_8
, },
532 { BFD_RELOC_32_PCREL
, R_MN10300_PCREL32
, },
533 { BFD_RELOC_16_PCREL
, R_MN10300_PCREL16
, },
534 { BFD_RELOC_8_PCREL
, R_MN10300_PCREL8
, },
535 { BFD_RELOC_24
, R_MN10300_24
, },
536 { BFD_RELOC_VTABLE_INHERIT
, R_MN10300_GNU_VTINHERIT
},
537 { BFD_RELOC_VTABLE_ENTRY
, R_MN10300_GNU_VTENTRY
},
538 { BFD_RELOC_32_GOT_PCREL
, R_MN10300_GOTPC32
},
539 { BFD_RELOC_16_GOT_PCREL
, R_MN10300_GOTPC16
},
540 { BFD_RELOC_32_GOTOFF
, R_MN10300_GOTOFF32
},
541 { BFD_RELOC_MN10300_GOTOFF24
, R_MN10300_GOTOFF24
},
542 { BFD_RELOC_16_GOTOFF
, R_MN10300_GOTOFF16
},
543 { BFD_RELOC_32_PLT_PCREL
, R_MN10300_PLT32
},
544 { BFD_RELOC_16_PLT_PCREL
, R_MN10300_PLT16
},
545 { BFD_RELOC_MN10300_GOT32
, R_MN10300_GOT32
},
546 { BFD_RELOC_MN10300_GOT24
, R_MN10300_GOT24
},
547 { BFD_RELOC_MN10300_GOT16
, R_MN10300_GOT16
},
548 { BFD_RELOC_MN10300_COPY
, R_MN10300_COPY
},
549 { BFD_RELOC_MN10300_GLOB_DAT
, R_MN10300_GLOB_DAT
},
550 { BFD_RELOC_MN10300_JMP_SLOT
, R_MN10300_JMP_SLOT
},
551 { BFD_RELOC_MN10300_RELATIVE
, R_MN10300_RELATIVE
},
554 /* Create the GOT section. */
557 _bfd_mn10300_elf_create_got_section (abfd
, info
)
559 struct bfd_link_info
* info
;
564 struct elf_link_hash_entry
* h
;
565 struct elf_backend_data
* bed
= get_elf_backend_data (abfd
);
568 /* This function may be called more than once. */
569 if (bfd_get_section_by_name (abfd
, ".got") != NULL
)
572 switch (bed
->s
->arch_size
)
583 bfd_set_error (bfd_error_bad_value
);
587 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
588 | SEC_LINKER_CREATED
);
591 pltflags
|= SEC_CODE
;
592 if (bed
->plt_not_loaded
)
593 pltflags
&= ~ (SEC_LOAD
| SEC_HAS_CONTENTS
);
594 if (bed
->plt_readonly
)
595 pltflags
|= SEC_READONLY
;
597 s
= bfd_make_section (abfd
, ".plt");
599 || ! bfd_set_section_flags (abfd
, s
, pltflags
)
600 || ! bfd_set_section_alignment (abfd
, s
, bed
->plt_alignment
))
603 if (bed
->want_plt_sym
)
605 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
607 struct elf_link_hash_entry
*h
= NULL
;
608 if (! (_bfd_generic_link_add_one_symbol
609 (info
, abfd
, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL
, s
,
610 (bfd_vma
) 0, (const char *) NULL
, FALSE
,
611 get_elf_backend_data (abfd
)->collect
,
612 (struct bfd_link_hash_entry
**) &h
)))
614 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
615 h
->type
= STT_OBJECT
;
618 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
622 s
= bfd_make_section (abfd
, ".got");
624 || ! bfd_set_section_flags (abfd
, s
, flags
)
625 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
628 if (bed
->want_got_plt
)
630 s
= bfd_make_section (abfd
, ".got.plt");
632 || ! bfd_set_section_flags (abfd
, s
, flags
)
633 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
637 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
638 (or .got.plt) section. We don't do this in the linker script
639 because we don't want to define the symbol if we are not creating
640 a global offset table. */
642 if (!(_bfd_generic_link_add_one_symbol
643 (info
, abfd
, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL
, s
,
644 bed
->got_symbol_offset
, (const char *) NULL
, FALSE
,
645 bed
->collect
, (struct bfd_link_hash_entry
**) &h
)))
647 h
->elf_link_hash_flags
|= ELF_LINK_HASH_DEF_REGULAR
;
648 h
->type
= STT_OBJECT
;
651 && ! _bfd_elf_link_record_dynamic_symbol (info
, h
))
654 elf_hash_table (info
)->hgot
= h
;
656 /* The first bit of the global offset table is the header. */
657 s
->_raw_size
+= bed
->got_header_size
+ bed
->got_symbol_offset
;
662 static reloc_howto_type
*
663 bfd_elf32_bfd_reloc_type_lookup (abfd
, code
)
664 bfd
*abfd ATTRIBUTE_UNUSED
;
665 bfd_reloc_code_real_type code
;
670 i
< sizeof (mn10300_reloc_map
) / sizeof (struct mn10300_reloc_map
);
673 if (mn10300_reloc_map
[i
].bfd_reloc_val
== code
)
674 return &elf_mn10300_howto_table
[mn10300_reloc_map
[i
].elf_reloc_val
];
680 /* Set the howto pointer for an MN10300 ELF reloc. */
683 mn10300_info_to_howto (abfd
, cache_ptr
, dst
)
684 bfd
*abfd ATTRIBUTE_UNUSED
;
686 Elf_Internal_Rela
*dst
;
690 r_type
= ELF32_R_TYPE (dst
->r_info
);
691 BFD_ASSERT (r_type
< (unsigned int) R_MN10300_MAX
);
692 cache_ptr
->howto
= &elf_mn10300_howto_table
[r_type
];
695 /* Look through the relocs for a section during the first phase.
696 Since we don't do .gots or .plts, we just need to consider the
697 virtual table relocs for gc. */
700 mn10300_elf_check_relocs (abfd
, info
, sec
, relocs
)
702 struct bfd_link_info
*info
;
704 const Elf_Internal_Rela
*relocs
;
706 Elf_Internal_Shdr
*symtab_hdr
;
707 struct elf_link_hash_entry
**sym_hashes
, **sym_hashes_end
;
708 const Elf_Internal_Rela
*rel
;
709 const Elf_Internal_Rela
*rel_end
;
711 bfd_vma
* local_got_offsets
;
720 if (info
->relocatable
)
723 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
724 sym_hashes
= elf_sym_hashes (abfd
);
725 sym_hashes_end
= sym_hashes
+ symtab_hdr
->sh_size
/sizeof (Elf32_External_Sym
);
726 if (!elf_bad_symtab (abfd
))
727 sym_hashes_end
-= symtab_hdr
->sh_info
;
729 dynobj
= elf_hash_table (info
)->dynobj
;
730 local_got_offsets
= elf_local_got_offsets (abfd
);
731 rel_end
= relocs
+ sec
->reloc_count
;
732 for (rel
= relocs
; rel
< rel_end
; rel
++)
734 struct elf_link_hash_entry
*h
;
735 unsigned long r_symndx
;
737 r_symndx
= ELF32_R_SYM (rel
->r_info
);
738 if (r_symndx
< symtab_hdr
->sh_info
)
741 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
743 /* Some relocs require a global offset table. */
746 switch (ELF32_R_TYPE (rel
->r_info
))
748 case R_MN10300_GOT32
:
749 case R_MN10300_GOT24
:
750 case R_MN10300_GOT16
:
751 case R_MN10300_GOTOFF32
:
752 case R_MN10300_GOTOFF24
:
753 case R_MN10300_GOTOFF16
:
754 case R_MN10300_GOTPC32
:
755 case R_MN10300_GOTPC16
:
756 elf_hash_table (info
)->dynobj
= dynobj
= abfd
;
757 if (! _bfd_mn10300_elf_create_got_section (dynobj
, info
))
766 switch (ELF32_R_TYPE (rel
->r_info
))
768 /* This relocation describes the C++ object vtable hierarchy.
769 Reconstruct it for later use during GC. */
770 case R_MN10300_GNU_VTINHERIT
:
771 if (!_bfd_elf32_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
775 /* This relocation describes which C++ vtable entries are actually
776 used. Record for later use during GC. */
777 case R_MN10300_GNU_VTENTRY
:
778 if (!_bfd_elf32_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
781 case R_MN10300_GOT32
:
782 case R_MN10300_GOT24
:
783 case R_MN10300_GOT16
:
784 /* This symbol requires a global offset table entry. */
788 sgot
= bfd_get_section_by_name (dynobj
, ".got");
789 BFD_ASSERT (sgot
!= NULL
);
793 && (h
!= NULL
|| info
->shared
))
795 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
798 srelgot
= bfd_make_section (dynobj
, ".rela.got");
800 || ! bfd_set_section_flags (dynobj
, srelgot
,
807 || ! bfd_set_section_alignment (dynobj
, srelgot
, 2))
814 if (h
->got
.offset
!= (bfd_vma
) -1)
815 /* We have already allocated space in the .got. */
818 h
->got
.offset
= sgot
->_raw_size
;
820 /* Make sure this symbol is output as a dynamic symbol. */
821 if (h
->dynindx
== -1)
823 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
827 srelgot
->_raw_size
+= sizeof (Elf32_External_Rela
);
831 /* This is a global offset table entry for a local
833 if (local_got_offsets
== NULL
)
838 size
= symtab_hdr
->sh_info
* sizeof (bfd_vma
);
839 local_got_offsets
= (bfd_vma
*) bfd_alloc (abfd
, size
);
841 if (local_got_offsets
== NULL
)
843 elf_local_got_offsets (abfd
) = local_got_offsets
;
845 for (i
= 0; i
< symtab_hdr
->sh_info
; i
++)
846 local_got_offsets
[i
] = (bfd_vma
) -1;
849 if (local_got_offsets
[r_symndx
] != (bfd_vma
) -1)
850 /* We have already allocated space in the .got. */
853 local_got_offsets
[r_symndx
] = sgot
->_raw_size
;
856 /* If we are generating a shared object, we need to
857 output a R_MN10300_RELATIVE reloc so that the dynamic
858 linker can adjust this GOT entry. */
859 srelgot
->_raw_size
+= sizeof (Elf32_External_Rela
);
862 sgot
->_raw_size
+= 4;
866 case R_MN10300_PLT32
:
867 case R_MN10300_PLT16
:
868 /* This symbol requires a procedure linkage table entry. We
869 actually build the entry in adjust_dynamic_symbol,
870 because this might be a case of linking PIC code which is
871 never referenced by a dynamic object, in which case we
872 don't need to generate a procedure linkage table entry
875 /* If this is a local symbol, we resolve it directly without
876 creating a procedure linkage table entry. */
880 if (ELF_ST_VISIBILITY (h
->other
) == STV_INTERNAL
881 || ELF_ST_VISIBILITY (h
->other
) == STV_HIDDEN
)
884 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_PLT
;
892 case R_MN10300_PCREL32
:
893 case R_MN10300_PCREL16
:
894 case R_MN10300_PCREL8
:
896 h
->elf_link_hash_flags
|= ELF_LINK_NON_GOT_REF
;
898 /* If we are creating a shared library, and this is a reloc
899 against a global symbol, or a non PC relative reloc
900 against a local symbol, then we need to copy the reloc
901 into the shared library. However, if we are linking with
902 -Bsymbolic, we do not need to copy a reloc against a
903 global symbol which is defined in an object we are
904 including in the link (i.e., DEF_REGULAR is set). At
905 this point we have not seen all the input files, so it is
906 possible that DEF_REGULAR is not set now but will be set
907 later (it is never cleared). We account for that
908 possibility below by storing information in the
909 pcrel_relocs_copied field of the hash table entry. */
911 && (sec
->flags
& SEC_ALLOC
) != 0
912 && (! (elf_mn10300_howto_table
[ELF32_R_TYPE (rel
->r_info
)]
916 || h
->root
.type
== bfd_link_hash_defweak
917 || (h
->elf_link_hash_flags
918 & ELF_LINK_HASH_DEF_REGULAR
) == 0))))
920 /* When creating a shared object, we must copy these
921 reloc types into the output file. We create a reloc
922 section in dynobj and make room for this reloc. */
927 name
= (bfd_elf_string_from_elf_section
929 elf_elfheader (abfd
)->e_shstrndx
,
930 elf_section_data (sec
)->rel_hdr
.sh_name
));
934 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
935 && strcmp (bfd_get_section_name (abfd
, sec
),
938 sreloc
= bfd_get_section_by_name (dynobj
, name
);
943 sreloc
= bfd_make_section (dynobj
, name
);
944 flags
= (SEC_HAS_CONTENTS
| SEC_READONLY
945 | SEC_IN_MEMORY
| SEC_LINKER_CREATED
);
946 if ((sec
->flags
& SEC_ALLOC
) != 0)
947 flags
|= SEC_ALLOC
| SEC_LOAD
;
949 || ! bfd_set_section_flags (dynobj
, sreloc
, flags
)
950 || ! bfd_set_section_alignment (dynobj
, sreloc
, 2))
955 sreloc
->_raw_size
+= sizeof (Elf32_External_Rela
);
957 /* If we are linking with -Bsymbolic, and this is a
958 global symbol, we count the number of PC relative
959 relocations we have entered for this symbol, so that
960 we can discard them again if the symbol is later
961 defined by a regular object. Note that this function
962 is only called if we are using an elf_sh linker
963 hash table, which means that h is really a pointer to
964 an elf32_mn10300_link_hash_entry. */
966 && (elf_mn10300_howto_table
[ELF32_R_TYPE (rel
->r_info
)]
969 struct elf32_mn10300_link_hash_entry
*eh
;
970 struct elf_mn10300_pcrel_relocs_copied
*p
;
972 eh
= (struct elf32_mn10300_link_hash_entry
*) h
;
974 for (p
= eh
->pcrel_relocs_copied
; p
!= NULL
; p
= p
->next
)
975 if (p
->section
== sreloc
)
980 p
= ((struct elf_mn10300_pcrel_relocs_copied
*)
981 bfd_alloc (dynobj
, sizeof *p
));
985 p
->next
= eh
->pcrel_relocs_copied
;
986 eh
->pcrel_relocs_copied
= p
;
1002 /* Return the section that should be marked against GC for a given
1006 mn10300_elf_gc_mark_hook (sec
, info
, rel
, h
, sym
)
1008 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1009 Elf_Internal_Rela
*rel
;
1010 struct elf_link_hash_entry
*h
;
1011 Elf_Internal_Sym
*sym
;
1015 switch (ELF32_R_TYPE (rel
->r_info
))
1017 case R_MN10300_GNU_VTINHERIT
:
1018 case R_MN10300_GNU_VTENTRY
:
1022 switch (h
->root
.type
)
1024 case bfd_link_hash_defined
:
1025 case bfd_link_hash_defweak
:
1026 return h
->root
.u
.def
.section
;
1028 case bfd_link_hash_common
:
1029 return h
->root
.u
.c
.p
->section
;
1037 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
1042 /* Perform a relocation as part of a final link. */
1043 static bfd_reloc_status_type
1044 mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
1045 input_section
, contents
, offset
, value
,
1046 addend
, h
, symndx
, info
, sym_sec
, is_local
)
1047 reloc_howto_type
*howto
;
1049 bfd
*output_bfd ATTRIBUTE_UNUSED
;
1050 asection
*input_section
;
1055 struct elf_link_hash_entry
* h
;
1056 unsigned long symndx
;
1057 struct bfd_link_info
*info ATTRIBUTE_UNUSED
;
1058 asection
*sym_sec ATTRIBUTE_UNUSED
;
1059 int is_local ATTRIBUTE_UNUSED
;
1061 unsigned long r_type
= howto
->type
;
1062 bfd_byte
*hit_data
= contents
+ offset
;
1064 bfd_vma
* local_got_offsets
;
1069 dynobj
= elf_hash_table (info
)->dynobj
;
1070 local_got_offsets
= elf_local_got_offsets (input_bfd
);
1078 case R_MN10300_NONE
:
1079 return bfd_reloc_ok
;
1083 && (input_section
->flags
& SEC_ALLOC
) != 0)
1085 Elf_Internal_Rela outrel
;
1086 bfd_boolean skip
, relocate
;
1088 /* When generating a shared object, these relocations are
1089 copied into the output file to be resolved at run
1095 name
= (bfd_elf_string_from_elf_section
1097 elf_elfheader (input_bfd
)->e_shstrndx
,
1098 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1102 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
1103 && strcmp (bfd_get_section_name (input_bfd
,
1107 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1108 BFD_ASSERT (sreloc
!= NULL
);
1113 if (elf_section_data (input_section
)->sec_info
== NULL
1114 || (input_section
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
1115 outrel
.r_offset
= offset
;
1120 off
= (_bfd_stab_section_offset
1121 (output_bfd
, & elf_hash_table (info
)->stab_info
,
1123 & elf_section_data (input_section
)->sec_info
,
1125 if (off
== (bfd_vma
) -1)
1127 outrel
.r_offset
= off
;
1130 outrel
.r_offset
+= (input_section
->output_section
->vma
1131 + input_section
->output_offset
);
1135 memset (&outrel
, 0, sizeof outrel
);
1140 /* h->dynindx may be -1 if this symbol was marked to
1143 || ((info
->symbolic
|| h
->dynindx
== -1)
1144 && (h
->elf_link_hash_flags
1145 & ELF_LINK_HASH_DEF_REGULAR
) != 0))
1148 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
1149 outrel
.r_addend
= value
+ addend
;
1153 BFD_ASSERT (h
->dynindx
!= -1);
1155 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_32
);
1156 outrel
.r_addend
= value
+ addend
;
1160 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1161 (((Elf32_External_Rela
*)
1163 + sreloc
->reloc_count
));
1164 ++sreloc
->reloc_count
;
1166 /* If this reloc is against an external symbol, we do
1167 not want to fiddle with the addend. Otherwise, we
1168 need to include the symbol value so that it becomes
1169 an addend for the dynamic reloc. */
1171 return bfd_reloc_ok
;
1174 bfd_put_32 (input_bfd
, value
, hit_data
);
1175 return bfd_reloc_ok
;
1180 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1181 return bfd_reloc_overflow
;
1183 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
1184 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1185 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1186 return bfd_reloc_ok
;
1191 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1192 return bfd_reloc_overflow
;
1194 bfd_put_16 (input_bfd
, value
, hit_data
);
1195 return bfd_reloc_ok
;
1200 if ((long) value
> 0x7f || (long) value
< -0x80)
1201 return bfd_reloc_overflow
;
1203 bfd_put_8 (input_bfd
, value
, hit_data
);
1204 return bfd_reloc_ok
;
1206 case R_MN10300_PCREL8
:
1207 value
-= (input_section
->output_section
->vma
1208 + input_section
->output_offset
);
1212 if ((long) value
> 0xff || (long) value
< -0x100)
1213 return bfd_reloc_overflow
;
1215 bfd_put_8 (input_bfd
, value
, hit_data
);
1216 return bfd_reloc_ok
;
1218 case R_MN10300_PCREL16
:
1219 value
-= (input_section
->output_section
->vma
1220 + input_section
->output_offset
);
1224 if ((long) value
> 0xffff || (long) value
< -0x10000)
1225 return bfd_reloc_overflow
;
1227 bfd_put_16 (input_bfd
, value
, hit_data
);
1228 return bfd_reloc_ok
;
1230 case R_MN10300_PCREL32
:
1232 && (input_section
->flags
& SEC_ALLOC
) != 0
1235 && (! info
->symbolic
1236 || (h
->elf_link_hash_flags
1237 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
1239 Elf_Internal_Rela outrel
;
1242 /* When generating a shared object, these relocations
1243 are copied into the output file to be resolved at run
1250 name
= (bfd_elf_string_from_elf_section
1252 elf_elfheader (input_bfd
)->e_shstrndx
,
1253 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1257 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
1258 && strcmp (bfd_get_section_name (input_bfd
,
1262 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1263 BFD_ASSERT (sreloc
!= NULL
);
1268 if (elf_section_data (input_section
)->sec_info
== NULL
1269 || (input_section
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
1270 outrel
.r_offset
= offset
;
1275 off
= (_bfd_stab_section_offset
1276 (output_bfd
, & elf_hash_table (info
)->stab_info
,
1278 & elf_section_data (input_section
)->sec_info
,
1280 if (off
== (bfd_vma
) -1)
1282 outrel
.r_offset
= off
;
1285 outrel
.r_offset
+= (input_section
->output_section
->vma
1286 + input_section
->output_offset
);
1289 memset (&outrel
, 0, sizeof outrel
);
1292 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
1293 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_PCREL32
);
1294 outrel
.r_addend
= addend
;
1297 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1298 (((Elf32_External_Rela
*)
1300 + sreloc
->reloc_count
));
1301 ++sreloc
->reloc_count
;
1303 return bfd_reloc_ok
;
1306 value
-= (input_section
->output_section
->vma
1307 + input_section
->output_offset
);
1311 bfd_put_32 (input_bfd
, value
, hit_data
);
1312 return bfd_reloc_ok
;
1314 case R_MN10300_GNU_VTINHERIT
:
1315 case R_MN10300_GNU_VTENTRY
:
1316 return bfd_reloc_ok
;
1318 case R_MN10300_GOTPC32
:
1319 /* Use global offset table as symbol value. */
1321 value
= bfd_get_section_by_name (dynobj
,
1322 ".got")->output_section
->vma
;
1323 value
-= (input_section
->output_section
->vma
1324 + input_section
->output_offset
);
1328 bfd_put_32 (input_bfd
, value
, hit_data
);
1329 return bfd_reloc_ok
;
1331 case R_MN10300_GOTPC16
:
1332 /* Use global offset table as symbol value. */
1334 value
= bfd_get_section_by_name (dynobj
,
1335 ".got")->output_section
->vma
;
1336 value
-= (input_section
->output_section
->vma
1337 + input_section
->output_offset
);
1341 if ((long) value
> 0xffff || (long) value
< -0x10000)
1342 return bfd_reloc_overflow
;
1344 bfd_put_16 (input_bfd
, value
, hit_data
);
1345 return bfd_reloc_ok
;
1347 case R_MN10300_GOTOFF32
:
1348 value
-= bfd_get_section_by_name (dynobj
,
1349 ".got")->output_section
->vma
;
1352 bfd_put_32 (input_bfd
, value
, hit_data
);
1353 return bfd_reloc_ok
;
1355 case R_MN10300_GOTOFF24
:
1356 value
-= bfd_get_section_by_name (dynobj
,
1357 ".got")->output_section
->vma
;
1360 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1361 return bfd_reloc_overflow
;
1363 bfd_put_8 (input_bfd
, value
, hit_data
);
1364 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1365 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1366 return bfd_reloc_ok
;
1368 case R_MN10300_GOTOFF16
:
1369 value
-= bfd_get_section_by_name (dynobj
,
1370 ".got")->output_section
->vma
;
1373 if ((long) value
> 0xffff || (long) value
< -0x10000)
1374 return bfd_reloc_overflow
;
1376 bfd_put_16 (input_bfd
, value
, hit_data
);
1377 return bfd_reloc_ok
;
1379 case R_MN10300_PLT32
:
1381 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1382 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
1383 && h
->plt
.offset
!= (bfd_vma
) -1)
1387 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1389 value
= (splt
->output_section
->vma
1390 + splt
->output_offset
1391 + h
->plt
.offset
) - value
;
1394 value
-= (input_section
->output_section
->vma
1395 + input_section
->output_offset
);
1399 bfd_put_32 (input_bfd
, value
, hit_data
);
1400 return bfd_reloc_ok
;
1402 case R_MN10300_PLT16
:
1404 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1405 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
1406 && h
->plt
.offset
!= (bfd_vma
) -1)
1410 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1412 value
= (splt
->output_section
->vma
1413 + splt
->output_offset
1414 + h
->plt
.offset
) - value
;
1417 value
-= (input_section
->output_section
->vma
1418 + input_section
->output_offset
);
1422 if ((long) value
> 0xffff || (long) value
< -0x10000)
1423 return bfd_reloc_overflow
;
1425 bfd_put_16 (input_bfd
, value
, hit_data
);
1426 return bfd_reloc_ok
;
1428 case R_MN10300_GOT32
:
1429 case R_MN10300_GOT24
:
1430 case R_MN10300_GOT16
:
1434 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1440 off
= h
->got
.offset
;
1441 BFD_ASSERT (off
!= (bfd_vma
) -1);
1443 if (! elf_hash_table (info
)->dynamic_sections_created
1445 && (info
->symbolic
|| h
->dynindx
== -1)
1446 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1447 /* This is actually a static link, or it is a
1448 -Bsymbolic link and the symbol is defined
1449 locally, or the symbol was forced to be local
1450 because of a version file. We must initialize
1451 this entry in the global offset table.
1453 When doing a dynamic link, we create a .rela.got
1454 relocation entry to initialize the value. This
1455 is done in the finish_dynamic_symbol routine. */
1456 bfd_put_32 (output_bfd
, value
,
1457 sgot
->contents
+ off
);
1459 value
= sgot
->output_offset
+ off
;
1465 off
= elf_local_got_offsets (input_bfd
)[symndx
];
1467 bfd_put_32 (output_bfd
, value
, sgot
->contents
+ off
);
1472 Elf_Internal_Rela outrel
;
1474 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
1475 BFD_ASSERT (srelgot
!= NULL
);
1477 outrel
.r_offset
= (sgot
->output_section
->vma
1478 + sgot
->output_offset
1480 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
1481 outrel
.r_addend
= value
;
1482 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1483 (((Elf32_External_Rela
*)
1485 + srelgot
->reloc_count
));
1486 ++ srelgot
->reloc_count
;
1489 value
= sgot
->output_offset
+ off
;
1495 if (r_type
== R_MN10300_GOT32
)
1497 bfd_put_32 (input_bfd
, value
, hit_data
);
1498 return bfd_reloc_ok
;
1500 else if (r_type
== R_MN10300_GOT24
)
1502 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1503 return bfd_reloc_overflow
;
1505 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
1506 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1507 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1508 return bfd_reloc_ok
;
1510 else if (r_type
== R_MN10300_GOT16
)
1512 if ((long) value
> 0xffff || (long) value
< -0x10000)
1513 return bfd_reloc_overflow
;
1515 bfd_put_16 (input_bfd
, value
, hit_data
);
1516 return bfd_reloc_ok
;
1521 return bfd_reloc_notsupported
;
1525 /* Relocate an MN10300 ELF section. */
1527 mn10300_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1528 contents
, relocs
, local_syms
, local_sections
)
1530 struct bfd_link_info
*info
;
1532 asection
*input_section
;
1534 Elf_Internal_Rela
*relocs
;
1535 Elf_Internal_Sym
*local_syms
;
1536 asection
**local_sections
;
1538 Elf_Internal_Shdr
*symtab_hdr
;
1539 struct elf32_mn10300_link_hash_entry
**sym_hashes
;
1540 Elf_Internal_Rela
*rel
, *relend
;
1542 if (info
->relocatable
)
1545 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1546 sym_hashes
= (struct elf32_mn10300_link_hash_entry
**)
1547 (elf_sym_hashes (input_bfd
));
1550 relend
= relocs
+ input_section
->reloc_count
;
1551 for (; rel
< relend
; rel
++)
1554 reloc_howto_type
*howto
;
1555 unsigned long r_symndx
;
1556 Elf_Internal_Sym
*sym
;
1558 struct elf32_mn10300_link_hash_entry
*h
;
1560 bfd_reloc_status_type r
;
1562 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1563 r_type
= ELF32_R_TYPE (rel
->r_info
);
1564 howto
= elf_mn10300_howto_table
+ r_type
;
1566 /* Just skip the vtable gc relocs. */
1567 if (r_type
== R_MN10300_GNU_VTINHERIT
1568 || r_type
== R_MN10300_GNU_VTENTRY
)
1574 if (r_symndx
< symtab_hdr
->sh_info
)
1576 sym
= local_syms
+ r_symndx
;
1577 sec
= local_sections
[r_symndx
];
1578 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, sec
, rel
);
1582 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
1583 while (h
->root
.root
.type
== bfd_link_hash_indirect
1584 || h
->root
.root
.type
== bfd_link_hash_warning
)
1585 h
= (struct elf32_mn10300_link_hash_entry
*) h
->root
.root
.u
.i
.link
;
1586 if (h
->root
.root
.type
== bfd_link_hash_defined
1587 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1589 sec
= h
->root
.root
.u
.def
.section
;
1590 if ( r_type
== R_MN10300_GOTPC32
1591 || r_type
== R_MN10300_GOTPC16
1592 || (( r_type
== R_MN10300_PLT32
1593 || r_type
== R_MN10300_PLT16
)
1594 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_INTERNAL
1595 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_HIDDEN
1596 && h
->root
.plt
.offset
!= (bfd_vma
) -1)
1597 || (( r_type
== R_MN10300_GOT32
1598 || r_type
== R_MN10300_GOT24
1599 || r_type
== R_MN10300_GOT16
)
1600 && elf_hash_table (info
)->dynamic_sections_created
1602 || (! info
->symbolic
&& h
->root
.dynindx
!= -1)
1603 || (h
->root
.elf_link_hash_flags
1604 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
1606 && ((! info
->symbolic
&& h
->root
.dynindx
!= -1)
1607 || (h
->root
.elf_link_hash_flags
1608 & ELF_LINK_HASH_DEF_REGULAR
) == 0)
1609 && ( r_type
== R_MN10300_32
1610 || r_type
== R_MN10300_PCREL32
)
1611 && ((input_section
->flags
& SEC_ALLOC
) != 0
1612 /* DWARF will emit R_MN10300_32 relocations
1613 in its sections against symbols defined
1614 externally in shared libraries. We can't
1615 do anything with them here. */
1616 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
1617 && (h
->root
.elf_link_hash_flags
1618 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0))))
1620 /* In these cases, we don't need the relocation
1621 value. We check specially because in some
1622 obscure cases sec->output_section will be NULL. */
1625 else if (sec
->output_section
== NULL
)
1627 (*_bfd_error_handler
)
1628 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1629 bfd_get_filename (input_bfd
), h
->root
.root
.root
.string
,
1630 bfd_get_section_name (input_bfd
, input_section
));
1634 relocation
= (h
->root
.root
.u
.def
.value
1635 + sec
->output_section
->vma
1636 + sec
->output_offset
);
1638 else if (h
->root
.root
.type
== bfd_link_hash_undefweak
)
1640 else if (info
->shared
&& !info
->symbolic
&& !info
->no_undefined
1641 && ELF_ST_VISIBILITY (h
->root
.other
) == STV_DEFAULT
)
1645 if (! ((*info
->callbacks
->undefined_symbol
)
1646 (info
, h
->root
.root
.root
.string
, input_bfd
,
1647 input_section
, rel
->r_offset
,
1648 (!info
->shared
|| info
->no_undefined
1649 || ELF_ST_VISIBILITY (h
->root
.other
)))))
1655 r
= mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
1657 contents
, rel
->r_offset
,
1658 relocation
, rel
->r_addend
,
1659 (struct elf_link_hash_entry
*)h
,
1661 info
, sec
, h
== NULL
);
1663 if (r
!= bfd_reloc_ok
)
1666 const char *msg
= (const char *) 0;
1669 name
= h
->root
.root
.root
.string
;
1672 name
= (bfd_elf_string_from_elf_section
1673 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1674 if (name
== NULL
|| *name
== '\0')
1675 name
= bfd_section_name (input_bfd
, sec
);
1680 case bfd_reloc_overflow
:
1681 if (! ((*info
->callbacks
->reloc_overflow
)
1682 (info
, name
, howto
->name
, (bfd_vma
) 0,
1683 input_bfd
, input_section
, rel
->r_offset
)))
1687 case bfd_reloc_undefined
:
1688 if (! ((*info
->callbacks
->undefined_symbol
)
1689 (info
, name
, input_bfd
, input_section
,
1690 rel
->r_offset
, TRUE
)))
1694 case bfd_reloc_outofrange
:
1695 msg
= _("internal error: out of range error");
1698 case bfd_reloc_notsupported
:
1699 msg
= _("internal error: unsupported relocation error");
1702 case bfd_reloc_dangerous
:
1703 msg
= _("internal error: dangerous error");
1707 msg
= _("internal error: unknown error");
1711 if (!((*info
->callbacks
->warning
)
1712 (info
, msg
, name
, input_bfd
, input_section
,
1723 /* Finish initializing one hash table entry. */
1725 elf32_mn10300_finish_hash_table_entry (gen_entry
, in_args
)
1726 struct bfd_hash_entry
*gen_entry
;
1727 PTR in_args ATTRIBUTE_UNUSED
;
1729 struct elf32_mn10300_link_hash_entry
*entry
;
1730 unsigned int byte_count
= 0;
1732 entry
= (struct elf32_mn10300_link_hash_entry
*) gen_entry
;
1734 if (entry
->root
.root
.type
== bfd_link_hash_warning
)
1735 entry
= (struct elf32_mn10300_link_hash_entry
*) entry
->root
.root
.u
.i
.link
;
1737 /* If we already know we want to convert "call" to "calls" for calls
1738 to this symbol, then return now. */
1739 if (entry
->flags
== MN10300_CONVERT_CALL_TO_CALLS
)
1742 /* If there are no named calls to this symbol, or there's nothing we
1743 can move from the function itself into the "call" instruction, then
1744 note that all "call" instructions should be converted into "calls"
1745 instructions and return. */
1746 if (entry
->direct_calls
== 0
1747 || (entry
->stack_size
== 0 && entry
->movm_args
== 0))
1749 /* Make a note that we should convert "call" instructions to "calls"
1750 instructions for calls to this symbol. */
1751 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1755 /* We may be able to move some instructions from the function itself into
1756 the "call" instruction. Count how many bytes we might be able to
1757 eliminate in the function itself. */
1759 /* A movm instruction is two bytes. */
1760 if (entry
->movm_args
)
1763 /* Count the insn to allocate stack space too. */
1764 if (entry
->stack_size
> 0 && entry
->stack_size
<= 128)
1766 else if (entry
->stack_size
> 0 && entry
->stack_size
< 256)
1769 /* If using "call" will result in larger code, then turn all
1770 the associated "call" instructions into "calls" instrutions. */
1771 if (byte_count
< entry
->direct_calls
)
1772 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1774 /* This routine never fails. */
1778 /* This function handles relaxing for the mn10300.
1780 There's quite a few relaxing opportunites available on the mn10300:
1782 * calls:32 -> calls:16 2 bytes
1783 * call:32 -> call:16 2 bytes
1785 * call:32 -> calls:32 1 byte
1786 * call:16 -> calls:16 1 byte
1787 * These are done anytime using "calls" would result
1788 in smaller code, or when necessary to preserve the
1789 meaning of the program.
1793 * In some circumstances we can move instructions
1794 from a function prologue into a "call" instruction.
1795 This is only done if the resulting code is no larger
1796 than the original code.
1798 * jmp:32 -> jmp:16 2 bytes
1799 * jmp:16 -> bra:8 1 byte
1801 * If the previous instruction is a conditional branch
1802 around the jump/bra, we may be able to reverse its condition
1803 and change its target to the jump's target. The jump/bra
1804 can then be deleted. 2 bytes
1806 * mov abs32 -> mov abs16 1 or 2 bytes
1808 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
1809 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
1811 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
1812 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
1814 We don't handle imm16->imm8 or d16->d8 as they're very rare
1815 and somewhat more difficult to support. */
1818 mn10300_elf_relax_section (abfd
, sec
, link_info
, again
)
1821 struct bfd_link_info
*link_info
;
1824 Elf_Internal_Shdr
*symtab_hdr
;
1825 Elf_Internal_Rela
*internal_relocs
= NULL
;
1826 Elf_Internal_Rela
*irel
, *irelend
;
1827 bfd_byte
*contents
= NULL
;
1828 Elf_Internal_Sym
*isymbuf
= NULL
;
1829 struct elf32_mn10300_link_hash_table
*hash_table
;
1830 asection
*section
= sec
;
1832 /* Assume nothing changes. */
1835 /* We need a pointer to the mn10300 specific hash table. */
1836 hash_table
= elf32_mn10300_hash_table (link_info
);
1838 /* Initialize fields in each hash table entry the first time through. */
1839 if ((hash_table
->flags
& MN10300_HASH_ENTRIES_INITIALIZED
) == 0)
1843 /* Iterate over all the input bfds. */
1844 for (input_bfd
= link_info
->input_bfds
;
1846 input_bfd
= input_bfd
->link_next
)
1848 /* We're going to need all the symbols for each bfd. */
1849 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1850 if (symtab_hdr
->sh_info
!= 0)
1852 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1853 if (isymbuf
== NULL
)
1854 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1855 symtab_hdr
->sh_info
, 0,
1857 if (isymbuf
== NULL
)
1861 /* Iterate over each section in this bfd. */
1862 for (section
= input_bfd
->sections
;
1864 section
= section
->next
)
1866 struct elf32_mn10300_link_hash_entry
*hash
;
1867 Elf_Internal_Sym
*sym
;
1868 asection
*sym_sec
= NULL
;
1869 const char *sym_name
;
1872 /* If there's nothing to do in this section, skip it. */
1873 if (! (((section
->flags
& SEC_RELOC
) != 0
1874 && section
->reloc_count
!= 0)
1875 || (section
->flags
& SEC_CODE
) != 0))
1878 /* Get cached copy of section contents if it exists. */
1879 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
1880 contents
= elf_section_data (section
)->this_hdr
.contents
;
1881 else if (section
->_raw_size
!= 0)
1883 /* Go get them off disk. */
1884 contents
= (bfd_byte
*) bfd_malloc (section
->_raw_size
);
1885 if (contents
== NULL
)
1888 if (!bfd_get_section_contents (input_bfd
, section
,
1889 contents
, (file_ptr
) 0,
1890 section
->_raw_size
))
1896 /* If there aren't any relocs, then there's nothing to do. */
1897 if ((section
->flags
& SEC_RELOC
) != 0
1898 && section
->reloc_count
!= 0)
1901 /* Get a copy of the native relocations. */
1902 internal_relocs
= (_bfd_elf_link_read_relocs
1903 (input_bfd
, section
, (PTR
) NULL
,
1904 (Elf_Internal_Rela
*) NULL
,
1905 link_info
->keep_memory
));
1906 if (internal_relocs
== NULL
)
1909 /* Now examine each relocation. */
1910 irel
= internal_relocs
;
1911 irelend
= irel
+ section
->reloc_count
;
1912 for (; irel
< irelend
; irel
++)
1915 unsigned long r_index
;
1918 r_type
= ELF32_R_TYPE (irel
->r_info
);
1919 r_index
= ELF32_R_SYM (irel
->r_info
);
1921 if (r_type
< 0 || r_type
>= (int) R_MN10300_MAX
)
1924 /* We need the name and hash table entry of the target
1930 if (r_index
< symtab_hdr
->sh_info
)
1932 /* A local symbol. */
1933 Elf_Internal_Sym
*isym
;
1934 struct elf_link_hash_table
*elftab
;
1937 isym
= isymbuf
+ r_index
;
1938 if (isym
->st_shndx
== SHN_UNDEF
)
1939 sym_sec
= bfd_und_section_ptr
;
1940 else if (isym
->st_shndx
== SHN_ABS
)
1941 sym_sec
= bfd_abs_section_ptr
;
1942 else if (isym
->st_shndx
== SHN_COMMON
)
1943 sym_sec
= bfd_com_section_ptr
;
1946 = bfd_section_from_elf_index (input_bfd
,
1950 = bfd_elf_string_from_elf_section (input_bfd
,
1955 /* If it isn't a function, then we don't care
1957 if (ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
)
1960 /* Tack on an ID so we can uniquely identify this
1961 local symbol in the global hash table. */
1962 amt
= strlen (sym_name
) + 10;
1963 new_name
= bfd_malloc (amt
);
1967 sprintf (new_name
, "%s_%08x",
1968 sym_name
, (int) sym_sec
);
1969 sym_name
= new_name
;
1971 elftab
= &hash_table
->static_hash_table
->root
;
1972 hash
= ((struct elf32_mn10300_link_hash_entry
*)
1973 elf_link_hash_lookup (elftab
, sym_name
,
1974 TRUE
, TRUE
, FALSE
));
1979 r_index
-= symtab_hdr
->sh_info
;
1980 hash
= (struct elf32_mn10300_link_hash_entry
*)
1981 elf_sym_hashes (input_bfd
)[r_index
];
1984 /* If this is not a "call" instruction, then we
1985 should convert "call" instructions to "calls"
1987 code
= bfd_get_8 (input_bfd
,
1988 contents
+ irel
->r_offset
- 1);
1989 if (code
!= 0xdd && code
!= 0xcd)
1990 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1992 /* If this is a jump/call, then bump the
1993 direct_calls counter. Else force "call" to
1994 "calls" conversions. */
1995 if (r_type
== R_MN10300_PCREL32
1996 || r_type
== R_MN10300_PLT32
1997 || r_type
== R_MN10300_PLT16
1998 || r_type
== R_MN10300_PCREL16
)
1999 hash
->direct_calls
++;
2001 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
2005 /* Now look at the actual contents to get the stack size,
2006 and a list of what registers were saved in the prologue
2008 if ((section
->flags
& SEC_CODE
) != 0)
2010 Elf_Internal_Sym
*isym
, *isymend
;
2011 unsigned int sec_shndx
;
2012 struct elf_link_hash_entry
**hashes
;
2013 struct elf_link_hash_entry
**end_hashes
;
2014 unsigned int symcount
;
2016 sec_shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
2019 /* Look at each function defined in this section and
2020 update info for that function. */
2021 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2022 for (isym
= isymbuf
; isym
< isymend
; isym
++)
2024 if (isym
->st_shndx
== sec_shndx
2025 && ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)
2027 struct elf_link_hash_table
*elftab
;
2030 if (isym
->st_shndx
== SHN_UNDEF
)
2031 sym_sec
= bfd_und_section_ptr
;
2032 else if (isym
->st_shndx
== SHN_ABS
)
2033 sym_sec
= bfd_abs_section_ptr
;
2034 else if (isym
->st_shndx
== SHN_COMMON
)
2035 sym_sec
= bfd_com_section_ptr
;
2038 = bfd_section_from_elf_index (input_bfd
,
2041 sym_name
= (bfd_elf_string_from_elf_section
2042 (input_bfd
, symtab_hdr
->sh_link
,
2045 /* Tack on an ID so we can uniquely identify this
2046 local symbol in the global hash table. */
2047 amt
= strlen (sym_name
) + 10;
2048 new_name
= bfd_malloc (amt
);
2052 sprintf (new_name
, "%s_%08x",
2053 sym_name
, (int) sym_sec
);
2054 sym_name
= new_name
;
2056 elftab
= &hash_table
->static_hash_table
->root
;
2057 hash
= ((struct elf32_mn10300_link_hash_entry
*)
2058 elf_link_hash_lookup (elftab
, sym_name
,
2059 TRUE
, TRUE
, FALSE
));
2061 compute_function_info (input_bfd
, hash
,
2062 isym
->st_value
, contents
);
2066 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2067 - symtab_hdr
->sh_info
);
2068 hashes
= elf_sym_hashes (abfd
);
2069 end_hashes
= hashes
+ symcount
;
2070 for (; hashes
< end_hashes
; hashes
++)
2072 hash
= (struct elf32_mn10300_link_hash_entry
*) *hashes
;
2073 if ((hash
->root
.root
.type
== bfd_link_hash_defined
2074 || hash
->root
.root
.type
== bfd_link_hash_defweak
)
2075 && hash
->root
.root
.u
.def
.section
== section
2076 && ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)
2077 compute_function_info (input_bfd
, hash
,
2078 (hash
)->root
.root
.u
.def
.value
,
2083 /* Cache or free any memory we allocated for the relocs. */
2084 if (internal_relocs
!= NULL
2085 && elf_section_data (section
)->relocs
!= internal_relocs
)
2086 free (internal_relocs
);
2087 internal_relocs
= NULL
;
2089 /* Cache or free any memory we allocated for the contents. */
2090 if (contents
!= NULL
2091 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
2093 if (! link_info
->keep_memory
)
2097 /* Cache the section contents for elf_link_input_bfd. */
2098 elf_section_data (section
)->this_hdr
.contents
= contents
;
2104 /* Cache or free any memory we allocated for the symbols. */
2106 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2108 if (! link_info
->keep_memory
)
2112 /* Cache the symbols for elf_link_input_bfd. */
2113 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2119 /* Now iterate on each symbol in the hash table and perform
2120 the final initialization steps on each. */
2121 elf32_mn10300_link_hash_traverse (hash_table
,
2122 elf32_mn10300_finish_hash_table_entry
,
2124 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
2125 elf32_mn10300_finish_hash_table_entry
,
2128 /* All entries in the hash table are fully initialized. */
2129 hash_table
->flags
|= MN10300_HASH_ENTRIES_INITIALIZED
;
2131 /* Now that everything has been initialized, go through each
2132 code section and delete any prologue insns which will be
2133 redundant because their operations will be performed by
2134 a "call" instruction. */
2135 for (input_bfd
= link_info
->input_bfds
;
2137 input_bfd
= input_bfd
->link_next
)
2139 /* We're going to need all the local symbols for each bfd. */
2140 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2141 if (symtab_hdr
->sh_info
!= 0)
2143 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2144 if (isymbuf
== NULL
)
2145 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2146 symtab_hdr
->sh_info
, 0,
2148 if (isymbuf
== NULL
)
2152 /* Walk over each section in this bfd. */
2153 for (section
= input_bfd
->sections
;
2155 section
= section
->next
)
2157 unsigned int sec_shndx
;
2158 Elf_Internal_Sym
*isym
, *isymend
;
2159 struct elf_link_hash_entry
**hashes
;
2160 struct elf_link_hash_entry
**end_hashes
;
2161 unsigned int symcount
;
2163 /* Skip non-code sections and empty sections. */
2164 if ((section
->flags
& SEC_CODE
) == 0 || section
->_raw_size
== 0)
2167 if (section
->reloc_count
!= 0)
2169 /* Get a copy of the native relocations. */
2170 internal_relocs
= (_bfd_elf_link_read_relocs
2171 (input_bfd
, section
, (PTR
) NULL
,
2172 (Elf_Internal_Rela
*) NULL
,
2173 link_info
->keep_memory
));
2174 if (internal_relocs
== NULL
)
2178 /* Get cached copy of section contents if it exists. */
2179 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
2180 contents
= elf_section_data (section
)->this_hdr
.contents
;
2183 /* Go get them off disk. */
2184 contents
= (bfd_byte
*) bfd_malloc (section
->_raw_size
);
2185 if (contents
== NULL
)
2188 if (!bfd_get_section_contents (input_bfd
, section
,
2189 contents
, (file_ptr
) 0,
2190 section
->_raw_size
))
2194 sec_shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
2197 /* Now look for any function in this section which needs
2198 insns deleted from its prologue. */
2199 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2200 for (isym
= isymbuf
; isym
< isymend
; isym
++)
2202 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2203 asection
*sym_sec
= NULL
;
2204 const char *sym_name
;
2206 struct elf_link_hash_table
*elftab
;
2209 if (isym
->st_shndx
!= sec_shndx
)
2212 if (isym
->st_shndx
== SHN_UNDEF
)
2213 sym_sec
= bfd_und_section_ptr
;
2214 else if (isym
->st_shndx
== SHN_ABS
)
2215 sym_sec
= bfd_abs_section_ptr
;
2216 else if (isym
->st_shndx
== SHN_COMMON
)
2217 sym_sec
= bfd_com_section_ptr
;
2220 = bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
2223 = bfd_elf_string_from_elf_section (input_bfd
,
2224 symtab_hdr
->sh_link
,
2227 /* Tack on an ID so we can uniquely identify this
2228 local symbol in the global hash table. */
2229 amt
= strlen (sym_name
) + 10;
2230 new_name
= bfd_malloc (amt
);
2233 sprintf (new_name
, "%s_%08x", sym_name
, (int) sym_sec
);
2234 sym_name
= new_name
;
2236 elftab
= &hash_table
->static_hash_table
->root
;
2237 sym_hash
= ((struct elf32_mn10300_link_hash_entry
*)
2238 elf_link_hash_lookup (elftab
, sym_name
,
2239 FALSE
, FALSE
, FALSE
));
2242 if (sym_hash
== NULL
)
2245 if (! (sym_hash
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
2246 && ! (sym_hash
->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
2250 /* Note that we've changed things. */
2251 elf_section_data (section
)->relocs
= internal_relocs
;
2252 elf_section_data (section
)->this_hdr
.contents
= contents
;
2253 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2255 /* Count how many bytes we're going to delete. */
2256 if (sym_hash
->movm_args
)
2259 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
2261 else if (sym_hash
->stack_size
2262 && sym_hash
->stack_size
< 256)
2265 /* Note that we've deleted prologue bytes for this
2267 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
2269 /* Actually delete the bytes. */
2270 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
2276 /* Something changed. Not strictly necessary, but
2277 may lead to more relaxing opportunities. */
2282 /* Look for any global functions in this section which
2283 need insns deleted from their prologues. */
2284 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2285 - symtab_hdr
->sh_info
);
2286 hashes
= elf_sym_hashes (abfd
);
2287 end_hashes
= hashes
+ symcount
;
2288 for (; hashes
< end_hashes
; hashes
++)
2290 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2292 sym_hash
= (struct elf32_mn10300_link_hash_entry
*) *hashes
;
2293 if ((sym_hash
->root
.root
.type
== bfd_link_hash_defined
2294 || sym_hash
->root
.root
.type
== bfd_link_hash_defweak
)
2295 && sym_hash
->root
.root
.u
.def
.section
== section
2296 && ! (sym_hash
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
2297 && ! (sym_hash
->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
2302 /* Note that we've changed things. */
2303 elf_section_data (section
)->relocs
= internal_relocs
;
2304 elf_section_data (section
)->this_hdr
.contents
= contents
;
2305 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2307 /* Count how many bytes we're going to delete. */
2308 if (sym_hash
->movm_args
)
2311 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
2313 else if (sym_hash
->stack_size
2314 && sym_hash
->stack_size
< 256)
2317 /* Note that we've deleted prologue bytes for this
2319 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
2321 /* Actually delete the bytes. */
2322 symval
= sym_hash
->root
.root
.u
.def
.value
;
2323 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
2329 /* Something changed. Not strictly necessary, but
2330 may lead to more relaxing opportunities. */
2335 /* Cache or free any memory we allocated for the relocs. */
2336 if (internal_relocs
!= NULL
2337 && elf_section_data (section
)->relocs
!= internal_relocs
)
2338 free (internal_relocs
);
2339 internal_relocs
= NULL
;
2341 /* Cache or free any memory we allocated for the contents. */
2342 if (contents
!= NULL
2343 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
2345 if (! link_info
->keep_memory
)
2349 /* Cache the section contents for elf_link_input_bfd. */
2350 elf_section_data (section
)->this_hdr
.contents
= contents
;
2356 /* Cache or free any memory we allocated for the symbols. */
2358 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2360 if (! link_info
->keep_memory
)
2364 /* Cache the symbols for elf_link_input_bfd. */
2365 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2372 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2374 internal_relocs
= NULL
;
2376 /* For error_return. */
2379 /* We don't have to do anything for a relocatable link, if
2380 this section does not have relocs, or if this is not a
2382 if (link_info
->relocatable
2383 || (sec
->flags
& SEC_RELOC
) == 0
2384 || sec
->reloc_count
== 0
2385 || (sec
->flags
& SEC_CODE
) == 0)
2388 /* If this is the first time we have been called for this section,
2389 initialize the cooked size. */
2390 if (sec
->_cooked_size
== 0)
2391 sec
->_cooked_size
= sec
->_raw_size
;
2393 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2395 /* Get a copy of the native relocations. */
2396 internal_relocs
= (_bfd_elf_link_read_relocs
2397 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
2398 link_info
->keep_memory
));
2399 if (internal_relocs
== NULL
)
2402 /* Walk through them looking for relaxing opportunities. */
2403 irelend
= internal_relocs
+ sec
->reloc_count
;
2404 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2407 struct elf32_mn10300_link_hash_entry
*h
= NULL
;
2409 /* If this isn't something that can be relaxed, then ignore
2411 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_NONE
2412 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_8
2413 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_MAX
)
2416 /* Get the section contents if we haven't done so already. */
2417 if (contents
== NULL
)
2419 /* Get cached copy if it exists. */
2420 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2421 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2424 /* Go get them off disk. */
2425 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
2426 if (contents
== NULL
)
2429 if (! bfd_get_section_contents (abfd
, sec
, contents
,
2430 (file_ptr
) 0, sec
->_raw_size
))
2435 /* Read this BFD's symbols if we haven't done so already. */
2436 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2438 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2439 if (isymbuf
== NULL
)
2440 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2441 symtab_hdr
->sh_info
, 0,
2443 if (isymbuf
== NULL
)
2447 /* Get the value of the symbol referred to by the reloc. */
2448 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2450 Elf_Internal_Sym
*isym
;
2451 asection
*sym_sec
= NULL
;
2452 const char *sym_name
;
2455 /* A local symbol. */
2456 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
2457 if (isym
->st_shndx
== SHN_UNDEF
)
2458 sym_sec
= bfd_und_section_ptr
;
2459 else if (isym
->st_shndx
== SHN_ABS
)
2460 sym_sec
= bfd_abs_section_ptr
;
2461 else if (isym
->st_shndx
== SHN_COMMON
)
2462 sym_sec
= bfd_com_section_ptr
;
2464 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2466 symval
= (isym
->st_value
2467 + sym_sec
->output_section
->vma
2468 + sym_sec
->output_offset
);
2469 sym_name
= bfd_elf_string_from_elf_section (abfd
,
2470 symtab_hdr
->sh_link
,
2473 /* Tack on an ID so we can uniquely identify this
2474 local symbol in the global hash table. */
2475 new_name
= bfd_malloc ((bfd_size_type
) strlen (sym_name
) + 10);
2478 sprintf (new_name
, "%s_%08x", sym_name
, (int) sym_sec
);
2479 sym_name
= new_name
;
2481 h
= (struct elf32_mn10300_link_hash_entry
*)
2482 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
2483 sym_name
, FALSE
, FALSE
, FALSE
);
2490 /* An external symbol. */
2491 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
2492 h
= (struct elf32_mn10300_link_hash_entry
*)
2493 (elf_sym_hashes (abfd
)[indx
]);
2494 BFD_ASSERT (h
!= NULL
);
2495 if (h
->root
.root
.type
!= bfd_link_hash_defined
2496 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
2498 /* This appears to be a reference to an undefined
2499 symbol. Just ignore it--it will be caught by the
2500 regular reloc processing. */
2504 symval
= (h
->root
.root
.u
.def
.value
2505 + h
->root
.root
.u
.def
.section
->output_section
->vma
2506 + h
->root
.root
.u
.def
.section
->output_offset
);
2509 /* For simplicity of coding, we are going to modify the section
2510 contents, the section relocs, and the BFD symbol table. We
2511 must tell the rest of the code not to free up this
2512 information. It would be possible to instead create a table
2513 of changes which have to be made, as is done in coff-mips.c;
2514 that would be more work, but would require less memory when
2515 the linker is run. */
2517 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2518 branch/call, also deal with "call" -> "calls" conversions and
2519 insertion of prologue data into "call" instructions. */
2520 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL32
2521 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PLT32
)
2523 bfd_vma value
= symval
;
2525 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PLT32
2527 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_INTERNAL
2528 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_HIDDEN
2529 && h
->root
.plt
.offset
!= (bfd_vma
) -1)
2533 splt
= bfd_get_section_by_name (elf_hash_table (link_info
)
2536 value
= ((splt
->output_section
->vma
2537 + splt
->output_offset
2538 + h
->root
.plt
.offset
)
2539 - (sec
->output_section
->vma
2540 + sec
->output_offset
2544 /* If we've got a "call" instruction that needs to be turned
2545 into a "calls" instruction, do so now. It saves a byte. */
2546 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
2550 /* Get the opcode. */
2551 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2553 /* Make sure we're working with a "call" instruction! */
2556 /* Note that we've changed the relocs, section contents,
2558 elf_section_data (sec
)->relocs
= internal_relocs
;
2559 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2560 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2562 /* Fix the opcode. */
2563 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 1);
2564 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
2566 /* Fix irel->r_offset and irel->r_addend. */
2567 irel
->r_offset
+= 1;
2568 irel
->r_addend
+= 1;
2570 /* Delete one byte of data. */
2571 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2572 irel
->r_offset
+ 3, 1))
2575 /* That will change things, so, we should relax again.
2576 Note that this is not required, and it may be slow. */
2582 /* We've got a "call" instruction which needs some data
2583 from target function filled in. */
2586 /* Get the opcode. */
2587 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2589 /* Insert data from the target function into the "call"
2590 instruction if needed. */
2593 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 4);
2594 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
2595 contents
+ irel
->r_offset
+ 5);
2599 /* Deal with pc-relative gunk. */
2600 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
2601 value
-= irel
->r_offset
;
2602 value
+= irel
->r_addend
;
2604 /* See if the value will fit in 16 bits, note the high value is
2605 0x7fff + 2 as the target will be two bytes closer if we are
2607 if ((long) value
< 0x8001 && (long) value
> -0x8000)
2611 /* Get the opcode. */
2612 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2614 if (code
!= 0xdc && code
!= 0xdd && code
!= 0xff)
2617 /* Note that we've changed the relocs, section contents, etc. */
2618 elf_section_data (sec
)->relocs
= internal_relocs
;
2619 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2620 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2622 /* Fix the opcode. */
2624 bfd_put_8 (abfd
, 0xcc, contents
+ irel
->r_offset
- 1);
2625 else if (code
== 0xdd)
2626 bfd_put_8 (abfd
, 0xcd, contents
+ irel
->r_offset
- 1);
2627 else if (code
== 0xff)
2628 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2630 /* Fix the relocation's type. */
2631 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2632 (ELF32_R_TYPE (irel
->r_info
)
2633 == (int) R_MN10300_PLT32
)
2637 /* Delete two bytes of data. */
2638 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2639 irel
->r_offset
+ 1, 2))
2642 /* That will change things, so, we should relax again.
2643 Note that this is not required, and it may be slow. */
2648 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2650 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL16
)
2652 bfd_vma value
= symval
;
2654 /* If we've got a "call" instruction that needs to be turned
2655 into a "calls" instruction, do so now. It saves a byte. */
2656 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
2660 /* Get the opcode. */
2661 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2663 /* Make sure we're working with a "call" instruction! */
2666 /* Note that we've changed the relocs, section contents,
2668 elf_section_data (sec
)->relocs
= internal_relocs
;
2669 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2670 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2672 /* Fix the opcode. */
2673 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 1);
2674 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
2676 /* Fix irel->r_offset and irel->r_addend. */
2677 irel
->r_offset
+= 1;
2678 irel
->r_addend
+= 1;
2680 /* Delete one byte of data. */
2681 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2682 irel
->r_offset
+ 1, 1))
2685 /* That will change things, so, we should relax again.
2686 Note that this is not required, and it may be slow. */
2694 /* Get the opcode. */
2695 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2697 /* Insert data from the target function into the "call"
2698 instruction if needed. */
2701 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 2);
2702 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
2703 contents
+ irel
->r_offset
+ 3);
2707 /* Deal with pc-relative gunk. */
2708 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
2709 value
-= irel
->r_offset
;
2710 value
+= irel
->r_addend
;
2712 /* See if the value will fit in 8 bits, note the high value is
2713 0x7f + 1 as the target will be one bytes closer if we are
2715 if ((long) value
< 0x80 && (long) value
> -0x80)
2719 /* Get the opcode. */
2720 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2725 /* Note that we've changed the relocs, section contents, etc. */
2726 elf_section_data (sec
)->relocs
= internal_relocs
;
2727 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2728 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2730 /* Fix the opcode. */
2731 bfd_put_8 (abfd
, 0xca, contents
+ irel
->r_offset
- 1);
2733 /* Fix the relocation's type. */
2734 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2737 /* Delete one byte of data. */
2738 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2739 irel
->r_offset
+ 1, 1))
2742 /* That will change things, so, we should relax again.
2743 Note that this is not required, and it may be slow. */
2748 /* Try to eliminate an unconditional 8 bit pc-relative branch
2749 which immediately follows a conditional 8 bit pc-relative
2750 branch around the unconditional branch.
2757 This happens when the bCC can't reach lab2 at assembly time,
2758 but due to other relaxations it can reach at link time. */
2759 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL8
)
2761 Elf_Internal_Rela
*nrel
;
2762 bfd_vma value
= symval
;
2765 /* Deal with pc-relative gunk. */
2766 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
2767 value
-= irel
->r_offset
;
2768 value
+= irel
->r_addend
;
2770 /* Do nothing if this reloc is the last byte in the section. */
2771 if (irel
->r_offset
== sec
->_cooked_size
)
2774 /* See if the next instruction is an unconditional pc-relative
2775 branch, more often than not this test will fail, so we
2776 test it first to speed things up. */
2777 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
2781 /* Also make sure the next relocation applies to the next
2782 instruction and that it's a pc-relative 8 bit branch. */
2785 || irel
->r_offset
+ 2 != nrel
->r_offset
2786 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10300_PCREL8
)
2789 /* Make sure our destination immediately follows the
2790 unconditional branch. */
2791 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
2792 + irel
->r_offset
+ 3))
2795 /* Now make sure we are a conditional branch. This may not
2796 be necessary, but why take the chance.
2798 Note these checks assume that R_MN10300_PCREL8 relocs
2799 only occur on bCC and bCCx insns. If they occured
2800 elsewhere, we'd need to know the start of this insn
2801 for this check to be accurate. */
2802 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2803 if (code
!= 0xc0 && code
!= 0xc1 && code
!= 0xc2
2804 && code
!= 0xc3 && code
!= 0xc4 && code
!= 0xc5
2805 && code
!= 0xc6 && code
!= 0xc7 && code
!= 0xc8
2806 && code
!= 0xc9 && code
!= 0xe8 && code
!= 0xe9
2807 && code
!= 0xea && code
!= 0xeb)
2810 /* We also have to be sure there is no symbol/label
2811 at the unconditional branch. */
2812 if (mn10300_elf_symbol_address_p (abfd
, sec
, isymbuf
,
2813 irel
->r_offset
+ 1))
2816 /* Note that we've changed the relocs, section contents, etc. */
2817 elf_section_data (sec
)->relocs
= internal_relocs
;
2818 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2819 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2821 /* Reverse the condition of the first branch. */
2867 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2869 /* Set the reloc type and symbol for the first branch
2870 from the second branch. */
2871 irel
->r_info
= nrel
->r_info
;
2873 /* Make the reloc for the second branch a null reloc. */
2874 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
2877 /* Delete two bytes of data. */
2878 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2879 irel
->r_offset
+ 1, 2))
2882 /* That will change things, so, we should relax again.
2883 Note that this is not required, and it may be slow. */
2887 /* Try to turn a 24 immediate, displacement or absolute address
2888 into a 8 immediate, displacement or absolute address. */
2889 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_24
)
2891 bfd_vma value
= symval
;
2892 value
+= irel
->r_addend
;
2894 /* See if the value will fit in 8 bits. */
2895 if ((long) value
< 0x7f && (long) value
> -0x80)
2899 /* AM33 insns which have 24 operands are 6 bytes long and
2900 will have 0xfd as the first byte. */
2902 /* Get the first opcode. */
2903 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
2907 /* Get the second opcode. */
2908 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2910 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2911 equivalent instructions exists. */
2912 if (code
!= 0x6b && code
!= 0x7b
2913 && code
!= 0x8b && code
!= 0x9b
2914 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
2915 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
2916 || (code
& 0x0f) == 0x0e))
2918 /* Not safe if the high bit is on as relaxing may
2919 move the value out of high mem and thus not fit
2920 in a signed 8bit value. This is currently over
2922 if ((value
& 0x80) == 0)
2924 /* Note that we've changed the relocation contents,
2926 elf_section_data (sec
)->relocs
= internal_relocs
;
2927 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2928 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2930 /* Fix the opcode. */
2931 bfd_put_8 (abfd
, 0xfb, contents
+ irel
->r_offset
- 3);
2932 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
2934 /* Fix the relocation's type. */
2936 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2939 /* Delete two bytes of data. */
2940 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2941 irel
->r_offset
+ 1, 2))
2944 /* That will change things, so, we should relax
2945 again. Note that this is not required, and it
2955 /* Try to turn a 32bit immediate, displacement or absolute address
2956 into a 16bit immediate, displacement or absolute address. */
2957 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_32
2958 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOT32
2959 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTOFF32
2960 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTPC32
)
2962 bfd_vma value
= symval
;
2964 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_MN10300_32
)
2968 sgot
= bfd_get_section_by_name (elf_hash_table (link_info
)
2971 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOT32
)
2973 value
= sgot
->output_offset
;
2976 value
+= h
->root
.got
.offset
;
2978 value
+= (elf_local_got_offsets
2979 (abfd
)[ELF32_R_SYM (irel
->r_info
)]);
2981 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTOFF32
)
2982 value
-= sgot
->output_section
->vma
;
2983 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTPC32
)
2984 value
= (sgot
->output_section
->vma
2985 - (sec
->output_section
->vma
2986 + sec
->output_offset
2992 value
+= irel
->r_addend
;
2994 /* See if the value will fit in 24 bits.
2995 We allow any 16bit match here. We prune those we can't
2997 if ((long) value
< 0x7fffff && (long) value
> -0x800000)
3001 /* AM33 insns which have 32bit operands are 7 bytes long and
3002 will have 0xfe as the first byte. */
3004 /* Get the first opcode. */
3005 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
3009 /* Get the second opcode. */
3010 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
3012 /* All the am33 32 -> 24 relaxing possibilities. */
3013 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3014 equivalent instructions exists. */
3015 if (code
!= 0x6b && code
!= 0x7b
3016 && code
!= 0x8b && code
!= 0x9b
3017 && (ELF32_R_TYPE (irel
->r_info
)
3018 != (int) R_MN10300_GOTPC32
)
3019 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
3020 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
3021 || (code
& 0x0f) == 0x0e))
3023 /* Not safe if the high bit is on as relaxing may
3024 move the value out of high mem and thus not fit
3025 in a signed 16bit value. This is currently over
3027 if ((value
& 0x8000) == 0)
3029 /* Note that we've changed the relocation contents,
3031 elf_section_data (sec
)->relocs
= internal_relocs
;
3032 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3033 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3035 /* Fix the opcode. */
3036 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 3);
3037 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3039 /* Fix the relocation's type. */
3041 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3042 (ELF32_R_TYPE (irel
->r_info
)
3043 == (int) R_MN10300_GOTOFF32
)
3044 ? R_MN10300_GOTOFF24
3045 : (ELF32_R_TYPE (irel
->r_info
)
3046 == (int) R_MN10300_GOT32
)
3050 /* Delete one byte of data. */
3051 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3052 irel
->r_offset
+ 3, 1))
3055 /* That will change things, so, we should relax
3056 again. Note that this is not required, and it
3065 /* See if the value will fit in 16 bits.
3066 We allow any 16bit match here. We prune those we can't
3068 if ((long) value
< 0x7fff && (long) value
> -0x8000)
3072 /* Most insns which have 32bit operands are 6 bytes long;
3073 exceptions are pcrel insns and bit insns.
3075 We handle pcrel insns above. We don't bother trying
3076 to handle the bit insns here.
3078 The first byte of the remaining insns will be 0xfc. */
3080 /* Get the first opcode. */
3081 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
3086 /* Get the second opcode. */
3087 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3089 if ((code
& 0xf0) < 0x80)
3090 switch (code
& 0xf0)
3092 /* mov (d32,am),dn -> mov (d32,am),dn
3093 mov dm,(d32,am) -> mov dn,(d32,am)
3094 mov (d32,am),an -> mov (d32,am),an
3095 mov dm,(d32,am) -> mov dn,(d32,am)
3096 movbu (d32,am),dn -> movbu (d32,am),dn
3097 movbu dm,(d32,am) -> movbu dn,(d32,am)
3098 movhu (d32,am),dn -> movhu (d32,am),dn
3099 movhu dm,(d32,am) -> movhu dn,(d32,am) */
3108 /* Not safe if the high bit is on as relaxing may
3109 move the value out of high mem and thus not fit
3110 in a signed 16bit value. */
3112 && (value
& 0x8000))
3115 /* Note that we've changed the relocation contents, etc. */
3116 elf_section_data (sec
)->relocs
= internal_relocs
;
3117 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3118 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3120 /* Fix the opcode. */
3121 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3122 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3124 /* Fix the relocation's type. */
3125 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3126 (ELF32_R_TYPE (irel
->r_info
)
3127 == (int) R_MN10300_GOTOFF32
)
3128 ? R_MN10300_GOTOFF16
3129 : (ELF32_R_TYPE (irel
->r_info
)
3130 == (int) R_MN10300_GOT32
)
3132 : (ELF32_R_TYPE (irel
->r_info
)
3133 == (int) R_MN10300_GOTPC32
)
3134 ? R_MN10300_GOTPC16
:
3137 /* Delete two bytes of data. */
3138 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3139 irel
->r_offset
+ 2, 2))
3142 /* That will change things, so, we should relax again.
3143 Note that this is not required, and it may be slow. */
3147 else if ((code
& 0xf0) == 0x80
3148 || (code
& 0xf0) == 0x90)
3149 switch (code
& 0xf3)
3151 /* mov dn,(abs32) -> mov dn,(abs16)
3152 movbu dn,(abs32) -> movbu dn,(abs16)
3153 movhu dn,(abs32) -> movhu dn,(abs16) */
3157 /* Note that we've changed the relocation contents, etc. */
3158 elf_section_data (sec
)->relocs
= internal_relocs
;
3159 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3160 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3162 if ((code
& 0xf3) == 0x81)
3163 code
= 0x01 + (code
& 0x0c);
3164 else if ((code
& 0xf3) == 0x82)
3165 code
= 0x02 + (code
& 0x0c);
3166 else if ((code
& 0xf3) == 0x83)
3167 code
= 0x03 + (code
& 0x0c);
3171 /* Fix the opcode. */
3172 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3174 /* Fix the relocation's type. */
3175 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3176 (ELF32_R_TYPE (irel
->r_info
)
3177 == (int) R_MN10300_GOTOFF32
)
3178 ? R_MN10300_GOTOFF16
3179 : (ELF32_R_TYPE (irel
->r_info
)
3180 == (int) R_MN10300_GOT32
)
3182 : (ELF32_R_TYPE (irel
->r_info
)
3183 == (int) R_MN10300_GOTPC32
)
3184 ? R_MN10300_GOTPC16
:
3187 /* The opcode got shorter too, so we have to fix the
3188 addend and offset too! */
3189 irel
->r_offset
-= 1;
3191 /* Delete three bytes of data. */
3192 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3193 irel
->r_offset
+ 1, 3))
3196 /* That will change things, so, we should relax again.
3197 Note that this is not required, and it may be slow. */
3201 /* mov am,(abs32) -> mov am,(abs16)
3202 mov am,(d32,sp) -> mov am,(d16,sp)
3203 mov dm,(d32,sp) -> mov dm,(d32,sp)
3204 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3205 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3211 /* sp-based offsets are zero-extended. */
3212 if (code
>= 0x90 && code
<= 0x93
3216 /* Note that we've changed the relocation contents, etc. */
3217 elf_section_data (sec
)->relocs
= internal_relocs
;
3218 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3219 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3221 /* Fix the opcode. */
3222 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3223 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3225 /* Fix the relocation's type. */
3226 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3227 (ELF32_R_TYPE (irel
->r_info
)
3228 == (int) R_MN10300_GOTOFF32
)
3229 ? R_MN10300_GOTOFF16
3230 : (ELF32_R_TYPE (irel
->r_info
)
3231 == (int) R_MN10300_GOT32
)
3233 : (ELF32_R_TYPE (irel
->r_info
)
3234 == (int) R_MN10300_GOTPC32
)
3235 ? R_MN10300_GOTPC16
:
3238 /* Delete two bytes of data. */
3239 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3240 irel
->r_offset
+ 2, 2))
3243 /* That will change things, so, we should relax again.
3244 Note that this is not required, and it may be slow. */
3248 else if ((code
& 0xf0) < 0xf0)
3249 switch (code
& 0xfc)
3251 /* mov imm32,dn -> mov imm16,dn
3252 mov imm32,an -> mov imm16,an
3253 mov (abs32),dn -> mov (abs16),dn
3254 movbu (abs32),dn -> movbu (abs16),dn
3255 movhu (abs32),dn -> movhu (abs16),dn */
3261 /* Not safe if the high bit is on as relaxing may
3262 move the value out of high mem and thus not fit
3263 in a signed 16bit value. */
3265 && (value
& 0x8000))
3268 /* mov imm16, an zero-extends the immediate. */
3273 /* Note that we've changed the relocation contents, etc. */
3274 elf_section_data (sec
)->relocs
= internal_relocs
;
3275 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3276 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3278 if ((code
& 0xfc) == 0xcc)
3279 code
= 0x2c + (code
& 0x03);
3280 else if ((code
& 0xfc) == 0xdc)
3281 code
= 0x24 + (code
& 0x03);
3282 else if ((code
& 0xfc) == 0xa4)
3283 code
= 0x30 + (code
& 0x03);
3284 else if ((code
& 0xfc) == 0xa8)
3285 code
= 0x34 + (code
& 0x03);
3286 else if ((code
& 0xfc) == 0xac)
3287 code
= 0x38 + (code
& 0x03);
3291 /* Fix the opcode. */
3292 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3294 /* Fix the relocation's type. */
3295 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3296 (ELF32_R_TYPE (irel
->r_info
)
3297 == (int) R_MN10300_GOTOFF32
)
3298 ? R_MN10300_GOTOFF16
3299 : (ELF32_R_TYPE (irel
->r_info
)
3300 == (int) R_MN10300_GOT32
)
3302 : (ELF32_R_TYPE (irel
->r_info
)
3303 == (int) R_MN10300_GOTPC32
)
3304 ? R_MN10300_GOTPC16
:
3307 /* The opcode got shorter too, so we have to fix the
3308 addend and offset too! */
3309 irel
->r_offset
-= 1;
3311 /* Delete three bytes of data. */
3312 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3313 irel
->r_offset
+ 1, 3))
3316 /* That will change things, so, we should relax again.
3317 Note that this is not required, and it may be slow. */
3321 /* mov (abs32),an -> mov (abs16),an
3322 mov (d32,sp),an -> mov (d16,sp),an
3323 mov (d32,sp),dn -> mov (d16,sp),dn
3324 movbu (d32,sp),dn -> movbu (d16,sp),dn
3325 movhu (d32,sp),dn -> movhu (d16,sp),dn
3326 add imm32,dn -> add imm16,dn
3327 cmp imm32,dn -> cmp imm16,dn
3328 add imm32,an -> add imm16,an
3329 cmp imm32,an -> cmp imm16,an
3330 and imm32,dn -> and imm16,dn
3331 or imm32,dn -> or imm16,dn
3332 xor imm32,dn -> xor imm16,dn
3333 btst imm32,dn -> btst imm16,dn */
3349 /* cmp imm16, an zero-extends the immediate. */
3354 /* So do sp-based offsets. */
3355 if (code
>= 0xb0 && code
<= 0xb3
3359 /* Note that we've changed the relocation contents, etc. */
3360 elf_section_data (sec
)->relocs
= internal_relocs
;
3361 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3362 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3364 /* Fix the opcode. */
3365 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3366 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3368 /* Fix the relocation's type. */
3369 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3370 (ELF32_R_TYPE (irel
->r_info
)
3371 == (int) R_MN10300_GOTOFF32
)
3372 ? R_MN10300_GOTOFF16
3373 : (ELF32_R_TYPE (irel
->r_info
)
3374 == (int) R_MN10300_GOT32
)
3376 : (ELF32_R_TYPE (irel
->r_info
)
3377 == (int) R_MN10300_GOTPC32
)
3378 ? R_MN10300_GOTPC16
:
3381 /* Delete two bytes of data. */
3382 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3383 irel
->r_offset
+ 2, 2))
3386 /* That will change things, so, we should relax again.
3387 Note that this is not required, and it may be slow. */
3391 else if (code
== 0xfe)
3393 /* add imm32,sp -> add imm16,sp */
3395 /* Note that we've changed the relocation contents, etc. */
3396 elf_section_data (sec
)->relocs
= internal_relocs
;
3397 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3398 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3400 /* Fix the opcode. */
3401 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3402 bfd_put_8 (abfd
, 0xfe, contents
+ irel
->r_offset
- 1);
3404 /* Fix the relocation's type. */
3405 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3406 (ELF32_R_TYPE (irel
->r_info
)
3407 == (int) R_MN10300_GOT32
)
3409 : (ELF32_R_TYPE (irel
->r_info
)
3410 == (int) R_MN10300_GOTOFF32
)
3411 ? R_MN10300_GOTOFF16
3412 : (ELF32_R_TYPE (irel
->r_info
)
3413 == (int) R_MN10300_GOTPC32
)
3414 ? R_MN10300_GOTPC16
:
3417 /* Delete two bytes of data. */
3418 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3419 irel
->r_offset
+ 2, 2))
3422 /* That will change things, so, we should relax again.
3423 Note that this is not required, and it may be slow. */
3432 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3434 if (! link_info
->keep_memory
)
3438 /* Cache the symbols for elf_link_input_bfd. */
3439 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3443 if (contents
!= NULL
3444 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3446 if (! link_info
->keep_memory
)
3450 /* Cache the section contents for elf_link_input_bfd. */
3451 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3455 if (internal_relocs
!= NULL
3456 && elf_section_data (sec
)->relocs
!= internal_relocs
)
3457 free (internal_relocs
);
3463 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3465 if (contents
!= NULL
3466 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
3468 if (internal_relocs
!= NULL
3469 && elf_section_data (section
)->relocs
!= internal_relocs
)
3470 free (internal_relocs
);
3475 /* Compute the stack size and movm arguments for the function
3476 referred to by HASH at address ADDR in section with
3477 contents CONTENTS, store the information in the hash table. */
3479 compute_function_info (abfd
, hash
, addr
, contents
)
3481 struct elf32_mn10300_link_hash_entry
*hash
;
3483 unsigned char *contents
;
3485 unsigned char byte1
, byte2
;
3486 /* We only care about a very small subset of the possible prologue
3487 sequences here. Basically we look for:
3489 movm [d2,d3,a2,a3],sp (optional)
3490 add <size>,sp (optional, and only for sizes which fit in an unsigned
3493 If we find anything else, we quit. */
3495 /* Look for movm [regs],sp */
3496 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
3497 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
3501 hash
->movm_args
= byte2
;
3503 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
3504 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
3507 /* Now figure out how much stack space will be allocated by the movm
3508 instruction. We need this kept separate from the funtion's normal
3510 if (hash
->movm_args
)
3513 if (hash
->movm_args
& 0x80)
3514 hash
->movm_stack_size
+= 4;
3517 if (hash
->movm_args
& 0x40)
3518 hash
->movm_stack_size
+= 4;
3521 if (hash
->movm_args
& 0x20)
3522 hash
->movm_stack_size
+= 4;
3525 if (hash
->movm_args
& 0x10)
3526 hash
->movm_stack_size
+= 4;
3528 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
3529 if (hash
->movm_args
& 0x08)
3530 hash
->movm_stack_size
+= 8 * 4;
3532 if (bfd_get_mach (abfd
) == bfd_mach_am33
3533 || bfd_get_mach (abfd
) == bfd_mach_am33_2
)
3535 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
3536 if (hash
->movm_args
& 0x1)
3537 hash
->movm_stack_size
+= 6 * 4;
3539 /* exreg1 space. e4, e5, e6, e7 */
3540 if (hash
->movm_args
& 0x2)
3541 hash
->movm_stack_size
+= 4 * 4;
3543 /* exreg0 space. e2, e3 */
3544 if (hash
->movm_args
& 0x4)
3545 hash
->movm_stack_size
+= 2 * 4;
3549 /* Now look for the two stack adjustment variants. */
3550 if (byte1
== 0xf8 && byte2
== 0xfe)
3552 int temp
= bfd_get_8 (abfd
, contents
+ addr
+ 2);
3553 temp
= ((temp
& 0xff) ^ (~0x7f)) + 0x80;
3555 hash
->stack_size
= -temp
;
3557 else if (byte1
== 0xfa && byte2
== 0xfe)
3559 int temp
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
3560 temp
= ((temp
& 0xffff) ^ (~0x7fff)) + 0x8000;
3564 hash
->stack_size
= temp
;
3567 /* If the total stack to be allocated by the call instruction is more
3568 than 255 bytes, then we can't remove the stack adjustment by using
3569 "call" (we might still be able to remove the "movm" instruction. */
3570 if (hash
->stack_size
+ hash
->movm_stack_size
> 255)
3571 hash
->stack_size
= 0;
3576 /* Delete some bytes from a section while relaxing. */
3579 mn10300_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
3585 Elf_Internal_Shdr
*symtab_hdr
;
3586 unsigned int sec_shndx
;
3588 Elf_Internal_Rela
*irel
, *irelend
;
3589 Elf_Internal_Rela
*irelalign
;
3591 Elf_Internal_Sym
*isym
, *isymend
;
3592 struct elf_link_hash_entry
**sym_hashes
;
3593 struct elf_link_hash_entry
**end_hashes
;
3594 unsigned int symcount
;
3596 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3598 contents
= elf_section_data (sec
)->this_hdr
.contents
;
3600 /* The deletion must stop at the next ALIGN reloc for an aligment
3601 power larger than the number of bytes we are deleting. */
3604 toaddr
= sec
->_cooked_size
;
3606 irel
= elf_section_data (sec
)->relocs
;
3607 irelend
= irel
+ sec
->reloc_count
;
3609 /* Actually delete the bytes. */
3610 memmove (contents
+ addr
, contents
+ addr
+ count
,
3611 (size_t) (toaddr
- addr
- count
));
3612 sec
->_cooked_size
-= count
;
3614 /* Adjust all the relocs. */
3615 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
3617 /* Get the new reloc address. */
3618 if ((irel
->r_offset
> addr
3619 && irel
->r_offset
< toaddr
))
3620 irel
->r_offset
-= count
;
3623 /* Adjust the local symbols defined in this section. */
3624 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3625 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3626 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
3628 if (isym
->st_shndx
== sec_shndx
3629 && isym
->st_value
> addr
3630 && isym
->st_value
< toaddr
)
3631 isym
->st_value
-= count
;
3634 /* Now adjust the global symbols defined in this section. */
3635 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
3636 - symtab_hdr
->sh_info
);
3637 sym_hashes
= elf_sym_hashes (abfd
);
3638 end_hashes
= sym_hashes
+ symcount
;
3639 for (; sym_hashes
< end_hashes
; sym_hashes
++)
3641 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
3642 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3643 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3644 && sym_hash
->root
.u
.def
.section
== sec
3645 && sym_hash
->root
.u
.def
.value
> addr
3646 && sym_hash
->root
.u
.def
.value
< toaddr
)
3648 sym_hash
->root
.u
.def
.value
-= count
;
3655 /* Return TRUE if a symbol exists at the given address, else return
3658 mn10300_elf_symbol_address_p (abfd
, sec
, isym
, addr
)
3661 Elf_Internal_Sym
*isym
;
3664 Elf_Internal_Shdr
*symtab_hdr
;
3665 unsigned int sec_shndx
;
3666 Elf_Internal_Sym
*isymend
;
3667 struct elf_link_hash_entry
**sym_hashes
;
3668 struct elf_link_hash_entry
**end_hashes
;
3669 unsigned int symcount
;
3671 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3673 /* Examine all the symbols. */
3674 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3675 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
3677 if (isym
->st_shndx
== sec_shndx
3678 && isym
->st_value
== addr
)
3682 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
3683 - symtab_hdr
->sh_info
);
3684 sym_hashes
= elf_sym_hashes (abfd
);
3685 end_hashes
= sym_hashes
+ symcount
;
3686 for (; sym_hashes
< end_hashes
; sym_hashes
++)
3688 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
3689 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3690 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3691 && sym_hash
->root
.u
.def
.section
== sec
3692 && sym_hash
->root
.u
.def
.value
== addr
)
3699 /* This is a version of bfd_generic_get_relocated_section_contents
3700 which uses mn10300_elf_relocate_section. */
3703 mn10300_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
3704 data
, relocatable
, symbols
)
3706 struct bfd_link_info
*link_info
;
3707 struct bfd_link_order
*link_order
;
3709 bfd_boolean relocatable
;
3712 Elf_Internal_Shdr
*symtab_hdr
;
3713 asection
*input_section
= link_order
->u
.indirect
.section
;
3714 bfd
*input_bfd
= input_section
->owner
;
3715 asection
**sections
= NULL
;
3716 Elf_Internal_Rela
*internal_relocs
= NULL
;
3717 Elf_Internal_Sym
*isymbuf
= NULL
;
3719 /* We only need to handle the case of relaxing, or of having a
3720 particular set of section contents, specially. */
3722 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
3723 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
3728 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3730 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
3731 (size_t) input_section
->_raw_size
);
3733 if ((input_section
->flags
& SEC_RELOC
) != 0
3734 && input_section
->reloc_count
> 0)
3737 Elf_Internal_Sym
*isym
, *isymend
;
3740 internal_relocs
= (_bfd_elf_link_read_relocs
3741 (input_bfd
, input_section
, (PTR
) NULL
,
3742 (Elf_Internal_Rela
*) NULL
, FALSE
));
3743 if (internal_relocs
== NULL
)
3746 if (symtab_hdr
->sh_info
!= 0)
3748 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3749 if (isymbuf
== NULL
)
3750 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3751 symtab_hdr
->sh_info
, 0,
3753 if (isymbuf
== NULL
)
3757 amt
= symtab_hdr
->sh_info
;
3758 amt
*= sizeof (asection
*);
3759 sections
= (asection
**) bfd_malloc (amt
);
3760 if (sections
== NULL
&& amt
!= 0)
3763 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
3764 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
3768 if (isym
->st_shndx
== SHN_UNDEF
)
3769 isec
= bfd_und_section_ptr
;
3770 else if (isym
->st_shndx
== SHN_ABS
)
3771 isec
= bfd_abs_section_ptr
;
3772 else if (isym
->st_shndx
== SHN_COMMON
)
3773 isec
= bfd_com_section_ptr
;
3775 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
3780 if (! mn10300_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
3781 input_section
, data
, internal_relocs
,
3785 if (sections
!= NULL
)
3787 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3789 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
3790 free (internal_relocs
);
3796 if (sections
!= NULL
)
3798 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3800 if (internal_relocs
!= NULL
3801 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
3802 free (internal_relocs
);
3806 /* Assorted hash table functions. */
3808 /* Initialize an entry in the link hash table. */
3810 /* Create an entry in an MN10300 ELF linker hash table. */
3812 static struct bfd_hash_entry
*
3813 elf32_mn10300_link_hash_newfunc (entry
, table
, string
)
3814 struct bfd_hash_entry
*entry
;
3815 struct bfd_hash_table
*table
;
3818 struct elf32_mn10300_link_hash_entry
*ret
=
3819 (struct elf32_mn10300_link_hash_entry
*) entry
;
3821 /* Allocate the structure if it has not already been allocated by a
3823 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
3824 ret
= ((struct elf32_mn10300_link_hash_entry
*)
3825 bfd_hash_allocate (table
,
3826 sizeof (struct elf32_mn10300_link_hash_entry
)));
3827 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
3828 return (struct bfd_hash_entry
*) ret
;
3830 /* Call the allocation method of the superclass. */
3831 ret
= ((struct elf32_mn10300_link_hash_entry
*)
3832 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
3834 if (ret
!= (struct elf32_mn10300_link_hash_entry
*) NULL
)
3836 ret
->direct_calls
= 0;
3837 ret
->stack_size
= 0;
3839 ret
->movm_stack_size
= 0;
3840 ret
->pcrel_relocs_copied
= NULL
;
3844 return (struct bfd_hash_entry
*) ret
;
3847 /* Create an mn10300 ELF linker hash table. */
3849 static struct bfd_link_hash_table
*
3850 elf32_mn10300_link_hash_table_create (abfd
)
3853 struct elf32_mn10300_link_hash_table
*ret
;
3854 bfd_size_type amt
= sizeof (struct elf32_mn10300_link_hash_table
);
3856 ret
= (struct elf32_mn10300_link_hash_table
*) bfd_malloc (amt
);
3857 if (ret
== (struct elf32_mn10300_link_hash_table
*) NULL
)
3860 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
3861 elf32_mn10300_link_hash_newfunc
))
3868 amt
= sizeof (struct elf_link_hash_table
);
3869 ret
->static_hash_table
3870 = (struct elf32_mn10300_link_hash_table
*) bfd_malloc (amt
);
3871 if (ret
->static_hash_table
== NULL
)
3877 if (! _bfd_elf_link_hash_table_init (&ret
->static_hash_table
->root
, abfd
,
3878 elf32_mn10300_link_hash_newfunc
))
3880 free (ret
->static_hash_table
);
3884 return &ret
->root
.root
;
3887 /* Free an mn10300 ELF linker hash table. */
3890 elf32_mn10300_link_hash_table_free (hash
)
3891 struct bfd_link_hash_table
*hash
;
3893 struct elf32_mn10300_link_hash_table
*ret
3894 = (struct elf32_mn10300_link_hash_table
*) hash
;
3896 _bfd_generic_link_hash_table_free
3897 ((struct bfd_link_hash_table
*) ret
->static_hash_table
);
3898 _bfd_generic_link_hash_table_free
3899 ((struct bfd_link_hash_table
*) ret
);
3902 static unsigned long
3903 elf_mn10300_mach (flags
)
3906 switch (flags
& EF_MN10300_MACH
)
3908 case E_MN10300_MACH_MN10300
:
3910 return bfd_mach_mn10300
;
3912 case E_MN10300_MACH_AM33
:
3913 return bfd_mach_am33
;
3915 case E_MN10300_MACH_AM33_2
:
3916 return bfd_mach_am33_2
;
3920 /* The final processing done just before writing out a MN10300 ELF object
3921 file. This gets the MN10300 architecture right based on the machine
3925 _bfd_mn10300_elf_final_write_processing (abfd
, linker
)
3927 bfd_boolean linker ATTRIBUTE_UNUSED
;
3931 switch (bfd_get_mach (abfd
))
3934 case bfd_mach_mn10300
:
3935 val
= E_MN10300_MACH_MN10300
;
3939 val
= E_MN10300_MACH_AM33
;
3942 case bfd_mach_am33_2
:
3943 val
= E_MN10300_MACH_AM33_2
;
3947 elf_elfheader (abfd
)->e_flags
&= ~ (EF_MN10300_MACH
);
3948 elf_elfheader (abfd
)->e_flags
|= val
;
3952 _bfd_mn10300_elf_object_p (abfd
)
3955 bfd_default_set_arch_mach (abfd
, bfd_arch_mn10300
,
3956 elf_mn10300_mach (elf_elfheader (abfd
)->e_flags
));
3960 /* Merge backend specific data from an object file to the output
3961 object file when linking. */
3964 _bfd_mn10300_elf_merge_private_bfd_data (ibfd
, obfd
)
3968 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3969 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3972 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
3973 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
3975 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
3976 bfd_get_mach (ibfd
)))
3983 #define PLT0_ENTRY_SIZE 15
3984 #define PLT_ENTRY_SIZE 20
3985 #define PIC_PLT_ENTRY_SIZE 24
3987 static const bfd_byte elf_mn10300_plt0_entry
[PLT0_ENTRY_SIZE
] =
3989 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
3990 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
3991 0xf0, 0xf4, /* jmp (a0) */
3994 static const bfd_byte elf_mn10300_plt_entry
[PLT_ENTRY_SIZE
] =
3996 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
3997 0xf0, 0xf4, /* jmp (a0) */
3998 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3999 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4002 static const bfd_byte elf_mn10300_pic_plt_entry
[PIC_PLT_ENTRY_SIZE
] =
4004 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4005 0xf0, 0xf4, /* jmp (a0) */
4006 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4007 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4008 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4009 0xf0, 0xf4, /* jmp (a0) */
4012 /* Return size of the first PLT entry. */
4013 #define elf_mn10300_sizeof_plt0(info) \
4014 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4016 /* Return size of a PLT entry. */
4017 #define elf_mn10300_sizeof_plt(info) \
4018 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4020 /* Return offset of the PLT0 address in an absolute PLT entry. */
4021 #define elf_mn10300_plt_plt0_offset(info) 16
4023 /* Return offset of the linker in PLT0 entry. */
4024 #define elf_mn10300_plt0_linker_offset(info) 2
4026 /* Return offset of the GOT id in PLT0 entry. */
4027 #define elf_mn10300_plt0_gotid_offset(info) 9
4029 /* Return offset of the tempoline in PLT entry */
4030 #define elf_mn10300_plt_temp_offset(info) 8
4032 /* Return offset of the symbol in PLT entry. */
4033 #define elf_mn10300_plt_symbol_offset(info) 2
4035 /* Return offset of the relocation in PLT entry. */
4036 #define elf_mn10300_plt_reloc_offset(info) 11
4038 /* The name of the dynamic interpreter. This is put in the .interp
4041 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4043 /* Create dynamic sections when linking against a dynamic object. */
4046 _bfd_mn10300_elf_create_dynamic_sections (abfd
, info
)
4048 struct bfd_link_info
*info
;
4052 struct elf_backend_data
* bed
= get_elf_backend_data (abfd
);
4055 switch (bed
->s
->arch_size
)
4066 bfd_set_error (bfd_error_bad_value
);
4070 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4071 .rel[a].bss sections. */
4073 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4074 | SEC_LINKER_CREATED
);
4076 s
= bfd_make_section (abfd
,
4077 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
4079 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
4080 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4083 if (! _bfd_mn10300_elf_create_got_section (abfd
, info
))
4087 const char * secname
;
4092 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4094 secflags
= bfd_get_section_flags (abfd
, sec
);
4095 if ((secflags
& (SEC_DATA
| SEC_LINKER_CREATED
))
4096 || ((secflags
& SEC_HAS_CONTENTS
) != SEC_HAS_CONTENTS
))
4099 secname
= bfd_get_section_name (abfd
, sec
);
4100 relname
= (char *) bfd_malloc (strlen (secname
) + 6);
4101 strcpy (relname
, ".rela");
4102 strcat (relname
, secname
);
4104 s
= bfd_make_section (abfd
, relname
);
4106 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
4107 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4112 if (bed
->want_dynbss
)
4114 /* The .dynbss section is a place to put symbols which are defined
4115 by dynamic objects, are referenced by regular objects, and are
4116 not functions. We must allocate space for them in the process
4117 image and use a R_*_COPY reloc to tell the dynamic linker to
4118 initialize them at run time. The linker script puts the .dynbss
4119 section into the .bss section of the final image. */
4120 s
= bfd_make_section (abfd
, ".dynbss");
4122 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
4125 /* The .rel[a].bss section holds copy relocs. This section is not
4126 normally needed. We need to create it here, though, so that the
4127 linker will map it to an output section. We can't just create it
4128 only if we need it, because we will not know whether we need it
4129 until we have seen all the input files, and the first time the
4130 main linker code calls BFD after examining all the input files
4131 (size_dynamic_sections) the input sections have already been
4132 mapped to the output sections. If the section turns out not to
4133 be needed, we can discard it later. We will never need this
4134 section when generating a shared object, since they do not use
4138 s
= bfd_make_section (abfd
,
4139 (bed
->default_use_rela_p
4140 ? ".rela.bss" : ".rel.bss"));
4142 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
4143 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4151 /* Adjust a symbol defined by a dynamic object and referenced by a
4152 regular object. The current definition is in some section of the
4153 dynamic object, but we're not including those sections. We have to
4154 change the definition to something the rest of the link can
4158 _bfd_mn10300_elf_adjust_dynamic_symbol (info
, h
)
4159 struct bfd_link_info
* info
;
4160 struct elf_link_hash_entry
* h
;
4164 unsigned int power_of_two
;
4166 dynobj
= elf_hash_table (info
)->dynobj
;
4168 /* Make sure we know what is going on here. */
4169 BFD_ASSERT (dynobj
!= NULL
4170 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
4171 || h
->weakdef
!= NULL
4172 || ((h
->elf_link_hash_flags
4173 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4174 && (h
->elf_link_hash_flags
4175 & ELF_LINK_HASH_REF_REGULAR
) != 0
4176 && (h
->elf_link_hash_flags
4177 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
4179 /* If this is a function, put it in the procedure linkage table. We
4180 will fill in the contents of the procedure linkage table later,
4181 when we know the address of the .got section. */
4182 if (h
->type
== STT_FUNC
4183 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
4186 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4187 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
4189 /* This case can occur if we saw a PLT reloc in an input
4190 file, but the symbol was never referred to by a dynamic
4191 object. In such a case, we don't actually need to build
4192 a procedure linkage table, and we can just do a REL32
4194 BFD_ASSERT ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0);
4198 /* Make sure this symbol is output as a dynamic symbol. */
4199 if (h
->dynindx
== -1)
4201 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4205 s
= bfd_get_section_by_name (dynobj
, ".plt");
4206 BFD_ASSERT (s
!= NULL
);
4208 /* If this is the first .plt entry, make room for the special
4210 if (s
->_raw_size
== 0)
4211 s
->_raw_size
+= elf_mn10300_sizeof_plt0 (info
);
4213 /* If this symbol is not defined in a regular file, and we are
4214 not generating a shared library, then set the symbol to this
4215 location in the .plt. This is required to make function
4216 pointers compare as equal between the normal executable and
4217 the shared library. */
4219 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4221 h
->root
.u
.def
.section
= s
;
4222 h
->root
.u
.def
.value
= s
->_raw_size
;
4225 h
->plt
.offset
= s
->_raw_size
;
4227 /* Make room for this entry. */
4228 s
->_raw_size
+= elf_mn10300_sizeof_plt (info
);
4230 /* We also need to make an entry in the .got.plt section, which
4231 will be placed in the .got section by the linker script. */
4233 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
4234 BFD_ASSERT (s
!= NULL
);
4237 /* We also need to make an entry in the .rela.plt section. */
4239 s
= bfd_get_section_by_name (dynobj
, ".rela.plt");
4240 BFD_ASSERT (s
!= NULL
);
4241 s
->_raw_size
+= sizeof (Elf32_External_Rela
);
4246 /* If this is a weak symbol, and there is a real definition, the
4247 processor independent code will have arranged for us to see the
4248 real definition first, and we can just use the same value. */
4249 if (h
->weakdef
!= NULL
)
4251 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
4252 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
4253 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
4254 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
4258 /* This is a reference to a symbol defined by a dynamic object which
4259 is not a function. */
4261 /* If we are creating a shared library, we must presume that the
4262 only references to the symbol are via the global offset table.
4263 For such cases we need not do anything here; the relocations will
4264 be handled correctly by relocate_section. */
4268 /* If there are no references to this symbol that do not use the
4269 GOT, we don't need to generate a copy reloc. */
4270 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
4273 /* We must allocate the symbol in our .dynbss section, which will
4274 become part of the .bss section of the executable. There will be
4275 an entry for this symbol in the .dynsym section. The dynamic
4276 object will contain position independent code, so all references
4277 from the dynamic object to this symbol will go through the global
4278 offset table. The dynamic linker will use the .dynsym entry to
4279 determine the address it must put in the global offset table, so
4280 both the dynamic object and the regular object will refer to the
4281 same memory location for the variable. */
4283 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
4284 BFD_ASSERT (s
!= NULL
);
4286 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4287 copy the initial value out of the dynamic object and into the
4288 runtime process image. We need to remember the offset into the
4289 .rela.bss section we are going to use. */
4290 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
4294 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
4295 BFD_ASSERT (srel
!= NULL
);
4296 srel
->_raw_size
+= sizeof (Elf32_External_Rela
);
4297 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
4300 /* We need to figure out the alignment required for this symbol. I
4301 have no idea how ELF linkers handle this. */
4302 power_of_two
= bfd_log2 (h
->size
);
4303 if (power_of_two
> 3)
4306 /* Apply the required alignment. */
4307 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
4308 (bfd_size_type
) (1 << power_of_two
));
4309 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
4311 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
4315 /* Define the symbol as being at this point in the section. */
4316 h
->root
.u
.def
.section
= s
;
4317 h
->root
.u
.def
.value
= s
->_raw_size
;
4319 /* Increment the section size to make room for the symbol. */
4320 s
->_raw_size
+= h
->size
;
4325 /* This function is called via elf32_mn10300_link_hash_traverse if we are
4326 creating a shared object with -Bsymbolic. It discards the space
4327 allocated to copy PC relative relocs against symbols which are
4328 defined in regular objects. We allocated space for them in the
4329 check_relocs routine, but we won't fill them in in the
4330 relocate_section routine. */
4333 _bfd_mn10300_elf_discard_copies (h
, info
)
4334 struct elf32_mn10300_link_hash_entry
*h
;
4335 struct bfd_link_info
*info
;
4337 struct elf_mn10300_pcrel_relocs_copied
*s
;
4339 /* If a symbol has been forced local or we have found a regular
4340 definition for the symbolic link case, then we won't be needing
4342 if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4343 && ((h
->root
.elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0
4346 for (s
= h
->pcrel_relocs_copied
; s
!= NULL
; s
= s
->next
)
4347 s
->section
->_raw_size
-= s
->count
* sizeof (Elf32_External_Rel
);
4353 /* Set the sizes of the dynamic sections. */
4356 _bfd_mn10300_elf_size_dynamic_sections (output_bfd
, info
)
4358 struct bfd_link_info
* info
;
4364 bfd_boolean reltext
;
4366 dynobj
= elf_hash_table (info
)->dynobj
;
4367 BFD_ASSERT (dynobj
!= NULL
);
4369 if (elf_hash_table (info
)->dynamic_sections_created
)
4371 /* Set the contents of the .interp section to the interpreter. */
4374 s
= bfd_get_section_by_name (dynobj
, ".interp");
4375 BFD_ASSERT (s
!= NULL
);
4376 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4377 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4382 /* We may have created entries in the .rela.got section.
4383 However, if we are not creating the dynamic sections, we will
4384 not actually use these entries. Reset the size of .rela.got,
4385 which will cause it to get stripped from the output file
4387 s
= bfd_get_section_by_name (dynobj
, ".rela.got");
4392 /* If this is a -Bsymbolic shared link, then we need to discard all
4393 PC relative relocs against symbols defined in a regular object.
4394 We allocated space for them in the check_relocs routine, but we
4395 will not fill them in in the relocate_section routine. */
4396 if (info
->shared
&& info
->symbolic
)
4397 elf32_mn10300_link_hash_traverse (elf32_mn10300_hash_table (info
),
4398 _bfd_mn10300_elf_discard_copies
,
4401 /* The check_relocs and adjust_dynamic_symbol entry points have
4402 determined the sizes of the various dynamic sections. Allocate
4407 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4412 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
4415 /* It's OK to base decisions on the section name, because none
4416 of the dynobj section names depend upon the input files. */
4417 name
= bfd_get_section_name (dynobj
, s
);
4421 if (strcmp (name
, ".plt") == 0)
4423 if (s
->_raw_size
== 0)
4424 /* Strip this section if we don't need it; see the
4428 /* Remember whether there is a PLT. */
4431 else if (strncmp (name
, ".rela", 5) == 0)
4433 if (s
->_raw_size
== 0)
4435 /* If we don't need this section, strip it from the
4436 output file. This is mostly to handle .rela.bss and
4437 .rela.plt. We must create both sections in
4438 create_dynamic_sections, because they must be created
4439 before the linker maps input sections to output
4440 sections. The linker does that before
4441 adjust_dynamic_symbol is called, and it is that
4442 function which decides whether anything needs to go
4443 into these sections. */
4450 /* Remember whether there are any reloc sections other
4452 if (strcmp (name
, ".rela.plt") != 0)
4454 const char * outname
;
4458 /* If this relocation section applies to a read only
4459 section, then we probably need a DT_TEXTREL
4460 entry. The entries in the .rela.plt section
4461 really apply to the .got section, which we
4462 created ourselves and so know is not readonly. */
4463 outname
= bfd_get_section_name (output_bfd
,
4465 target
= bfd_get_section_by_name (output_bfd
, outname
+ 5);
4467 && (target
->flags
& SEC_READONLY
) != 0
4468 && (target
->flags
& SEC_ALLOC
) != 0)
4472 /* We use the reloc_count field as a counter if we need
4473 to copy relocs into the output file. */
4477 else if (strncmp (name
, ".got", 4) != 0)
4478 /* It's not one of our sections, so don't allocate space. */
4483 _bfd_strip_section_from_output (info
, s
);
4487 /* Allocate memory for the section contents. We use bfd_zalloc
4488 here in case unused entries are not reclaimed before the
4489 section's contents are written out. This should not happen,
4490 but this way if it does, we get a R_MN10300_NONE reloc
4491 instead of garbage. */
4492 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->_raw_size
);
4493 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
4497 if (elf_hash_table (info
)->dynamic_sections_created
)
4499 /* Add some entries to the .dynamic section. We fill in the
4500 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4501 but we must add the entries now so that we get the correct
4502 size for the .dynamic section. The DT_DEBUG entry is filled
4503 in by the dynamic linker and used by the debugger. */
4506 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
4512 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
4513 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
4514 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
4515 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
4521 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELA
, 0)
4522 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELASZ
, 0)
4523 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELAENT
,
4524 sizeof (Elf32_External_Rela
)))
4530 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
4538 /* Finish up dynamic symbol handling. We set the contents of various
4539 dynamic sections here. */
4542 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
4544 struct bfd_link_info
* info
;
4545 struct elf_link_hash_entry
* h
;
4546 Elf_Internal_Sym
* sym
;
4550 dynobj
= elf_hash_table (info
)->dynobj
;
4552 if (h
->plt
.offset
!= (bfd_vma
) -1)
4559 Elf_Internal_Rela rel
;
4561 /* This symbol has an entry in the procedure linkage table. Set
4564 BFD_ASSERT (h
->dynindx
!= -1);
4566 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4567 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4568 srel
= bfd_get_section_by_name (dynobj
, ".rela.plt");
4569 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
4571 /* Get the index in the procedure linkage table which
4572 corresponds to this symbol. This is the index of this symbol
4573 in all the symbols for which we are making plt entries. The
4574 first entry in the procedure linkage table is reserved. */
4575 plt_index
= ((h
->plt
.offset
- elf_mn10300_sizeof_plt0 (info
))
4576 / elf_mn10300_sizeof_plt (info
));
4578 /* Get the offset into the .got table of the entry that
4579 corresponds to this function. Each .got entry is 4 bytes.
4580 The first three are reserved. */
4581 got_offset
= (plt_index
+ 3) * 4;
4583 /* Fill in the entry in the procedure linkage table. */
4586 memcpy (splt
->contents
+ h
->plt
.offset
, elf_mn10300_plt_entry
,
4587 elf_mn10300_sizeof_plt (info
));
4588 bfd_put_32 (output_bfd
,
4589 (sgot
->output_section
->vma
4590 + sgot
->output_offset
4592 (splt
->contents
+ h
->plt
.offset
4593 + elf_mn10300_plt_symbol_offset (info
)));
4595 bfd_put_32 (output_bfd
,
4596 (1 - h
->plt
.offset
- elf_mn10300_plt_plt0_offset (info
)),
4597 (splt
->contents
+ h
->plt
.offset
4598 + elf_mn10300_plt_plt0_offset (info
)));
4602 memcpy (splt
->contents
+ h
->plt
.offset
, elf_mn10300_pic_plt_entry
,
4603 elf_mn10300_sizeof_plt (info
));
4605 bfd_put_32 (output_bfd
, got_offset
,
4606 (splt
->contents
+ h
->plt
.offset
4607 + elf_mn10300_plt_symbol_offset (info
)));
4610 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rela
),
4611 (splt
->contents
+ h
->plt
.offset
4612 + elf_mn10300_plt_reloc_offset (info
)));
4614 /* Fill in the entry in the global offset table. */
4615 bfd_put_32 (output_bfd
,
4616 (splt
->output_section
->vma
4617 + splt
->output_offset
4619 + elf_mn10300_plt_temp_offset (info
)),
4620 sgot
->contents
+ got_offset
);
4622 /* Fill in the entry in the .rela.plt section. */
4623 rel
.r_offset
= (sgot
->output_section
->vma
4624 + sgot
->output_offset
4626 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_JMP_SLOT
);
4628 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4629 ((Elf32_External_Rela
*) srel
->contents
4632 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4633 /* Mark the symbol as undefined, rather than as defined in
4634 the .plt section. Leave the value alone. */
4635 sym
->st_shndx
= SHN_UNDEF
;
4638 if (h
->got
.offset
!= (bfd_vma
) -1)
4642 Elf_Internal_Rela rel
;
4644 /* This symbol has an entry in the global offset table. Set it up. */
4646 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4647 srel
= bfd_get_section_by_name (dynobj
, ".rela.got");
4648 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
4650 rel
.r_offset
= (sgot
->output_section
->vma
4651 + sgot
->output_offset
4652 + (h
->got
.offset
&~ 1));
4654 /* If this is a -Bsymbolic link, and the symbol is defined
4655 locally, we just want to emit a RELATIVE reloc. Likewise if
4656 the symbol was forced to be local because of a version file.
4657 The entry in the global offset table will already have been
4658 initialized in the relocate_section function. */
4660 && (info
->symbolic
|| h
->dynindx
== -1)
4661 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
))
4663 rel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
4664 rel
.r_addend
= (h
->root
.u
.def
.value
4665 + h
->root
.u
.def
.section
->output_section
->vma
4666 + h
->root
.u
.def
.section
->output_offset
);
4670 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
4671 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_GLOB_DAT
);
4675 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4676 ((Elf32_External_Rela
*) srel
->contents
4677 + srel
->reloc_count
));
4678 ++ srel
->reloc_count
;
4681 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
4684 Elf_Internal_Rela rel
;
4686 /* This symbol needs a copy reloc. Set it up. */
4687 BFD_ASSERT (h
->dynindx
!= -1
4688 && (h
->root
.type
== bfd_link_hash_defined
4689 || h
->root
.type
== bfd_link_hash_defweak
));
4691 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
4693 BFD_ASSERT (s
!= NULL
);
4695 rel
.r_offset
= (h
->root
.u
.def
.value
4696 + h
->root
.u
.def
.section
->output_section
->vma
4697 + h
->root
.u
.def
.section
->output_offset
);
4698 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_COPY
);
4700 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4701 ((Elf32_External_Rela
*) s
->contents
4706 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4707 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4708 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
4709 sym
->st_shndx
= SHN_ABS
;
4714 /* Finish up the dynamic sections. */
4717 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd
, info
)
4719 struct bfd_link_info
* info
;
4725 dynobj
= elf_hash_table (info
)->dynobj
;
4727 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4728 BFD_ASSERT (sgot
!= NULL
);
4729 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
4731 if (elf_hash_table (info
)->dynamic_sections_created
)
4734 Elf32_External_Dyn
* dyncon
;
4735 Elf32_External_Dyn
* dynconend
;
4737 BFD_ASSERT (sdyn
!= NULL
);
4739 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4740 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
4742 for (; dyncon
< dynconend
; dyncon
++)
4744 Elf_Internal_Dyn dyn
;
4748 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4762 s
= bfd_get_section_by_name (output_bfd
, name
);
4763 BFD_ASSERT (s
!= NULL
);
4764 dyn
.d_un
.d_ptr
= s
->vma
;
4765 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4769 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4770 BFD_ASSERT (s
!= NULL
);
4771 if (s
->_cooked_size
!= 0)
4772 dyn
.d_un
.d_val
= s
->_cooked_size
;
4774 dyn
.d_un
.d_val
= s
->_raw_size
;
4775 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4779 /* My reading of the SVR4 ABI indicates that the
4780 procedure linkage table relocs (DT_JMPREL) should be
4781 included in the overall relocs (DT_RELA). This is
4782 what Solaris does. However, UnixWare can not handle
4783 that case. Therefore, we override the DT_RELASZ entry
4784 here to make it not include the JMPREL relocs. Since
4785 the linker script arranges for .rela.plt to follow all
4786 other relocation sections, we don't have to worry
4787 about changing the DT_RELA entry. */
4788 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4791 if (s
->_cooked_size
!= 0)
4792 dyn
.d_un
.d_val
-= s
->_cooked_size
;
4794 dyn
.d_un
.d_val
-= s
->_raw_size
;
4796 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4801 /* Fill in the first entry in the procedure linkage table. */
4802 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4803 if (splt
&& splt
->_raw_size
> 0)
4807 memcpy (splt
->contents
, elf_mn10300_pic_plt_entry
,
4808 elf_mn10300_sizeof_plt (info
));
4812 memcpy (splt
->contents
, elf_mn10300_plt0_entry
, PLT0_ENTRY_SIZE
);
4813 bfd_put_32 (output_bfd
,
4814 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
4815 splt
->contents
+ elf_mn10300_plt0_gotid_offset (info
));
4816 bfd_put_32 (output_bfd
,
4817 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
4818 splt
->contents
+ elf_mn10300_plt0_linker_offset (info
));
4821 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4822 really seem like the right value. */
4823 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
4827 /* Fill in the first three entries in the global offset table. */
4828 if (sgot
->_raw_size
> 0)
4831 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
4833 bfd_put_32 (output_bfd
,
4834 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4836 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
4837 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
4840 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
4846 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4847 #define TARGET_LITTLE_NAME "elf32-mn10300"
4848 #define ELF_ARCH bfd_arch_mn10300
4849 #define ELF_MACHINE_CODE EM_MN10300
4850 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4851 #define ELF_MAXPAGESIZE 0x1000
4854 #define elf_info_to_howto mn10300_info_to_howto
4855 #define elf_info_to_howto_rel 0
4856 #define elf_backend_can_gc_sections 1
4857 #define elf_backend_rela_normal 1
4858 #define elf_backend_check_relocs mn10300_elf_check_relocs
4859 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4860 #define elf_backend_relocate_section mn10300_elf_relocate_section
4861 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4862 #define bfd_elf32_bfd_get_relocated_section_contents \
4863 mn10300_elf_get_relocated_section_contents
4864 #define bfd_elf32_bfd_link_hash_table_create \
4865 elf32_mn10300_link_hash_table_create
4866 #define bfd_elf32_bfd_link_hash_table_free \
4867 elf32_mn10300_link_hash_table_free
4869 #ifndef elf_symbol_leading_char
4870 #define elf_symbol_leading_char '_'
4873 /* So we can set bits in e_flags. */
4874 #define elf_backend_final_write_processing \
4875 _bfd_mn10300_elf_final_write_processing
4876 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4878 #define bfd_elf32_bfd_merge_private_bfd_data \
4879 _bfd_mn10300_elf_merge_private_bfd_data
4881 #define elf_backend_can_gc_sections 1
4882 #define elf_backend_create_dynamic_sections \
4883 _bfd_mn10300_elf_create_dynamic_sections
4884 #define elf_backend_adjust_dynamic_symbol \
4885 _bfd_mn10300_elf_adjust_dynamic_symbol
4886 #define elf_backend_size_dynamic_sections \
4887 _bfd_mn10300_elf_size_dynamic_sections
4888 #define elf_backend_finish_dynamic_symbol \
4889 _bfd_mn10300_elf_finish_dynamic_symbol
4890 #define elf_backend_finish_dynamic_sections \
4891 _bfd_mn10300_elf_finish_dynamic_sections
4893 #define elf_backend_want_got_plt 1
4894 #define elf_backend_plt_readonly 1
4895 #define elf_backend_want_plt_sym 0
4896 #define elf_backend_got_header_size 12
4897 #define elf_backend_plt_header_size PLT0_ENTRY_SIZE
4899 #include "elf32-target.h"