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 const 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 (bfd_byte
*) (((Elf32_External_Rela
*) sreloc
->contents
)
1162 + sreloc
->reloc_count
));
1163 ++sreloc
->reloc_count
;
1165 /* If this reloc is against an external symbol, we do
1166 not want to fiddle with the addend. Otherwise, we
1167 need to include the symbol value so that it becomes
1168 an addend for the dynamic reloc. */
1170 return bfd_reloc_ok
;
1173 bfd_put_32 (input_bfd
, value
, hit_data
);
1174 return bfd_reloc_ok
;
1179 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1180 return bfd_reloc_overflow
;
1182 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
1183 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1184 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1185 return bfd_reloc_ok
;
1190 if ((long) value
> 0x7fff || (long) value
< -0x8000)
1191 return bfd_reloc_overflow
;
1193 bfd_put_16 (input_bfd
, value
, hit_data
);
1194 return bfd_reloc_ok
;
1199 if ((long) value
> 0x7f || (long) value
< -0x80)
1200 return bfd_reloc_overflow
;
1202 bfd_put_8 (input_bfd
, value
, hit_data
);
1203 return bfd_reloc_ok
;
1205 case R_MN10300_PCREL8
:
1206 value
-= (input_section
->output_section
->vma
1207 + input_section
->output_offset
);
1211 if ((long) value
> 0xff || (long) value
< -0x100)
1212 return bfd_reloc_overflow
;
1214 bfd_put_8 (input_bfd
, value
, hit_data
);
1215 return bfd_reloc_ok
;
1217 case R_MN10300_PCREL16
:
1218 value
-= (input_section
->output_section
->vma
1219 + input_section
->output_offset
);
1223 if ((long) value
> 0xffff || (long) value
< -0x10000)
1224 return bfd_reloc_overflow
;
1226 bfd_put_16 (input_bfd
, value
, hit_data
);
1227 return bfd_reloc_ok
;
1229 case R_MN10300_PCREL32
:
1231 && (input_section
->flags
& SEC_ALLOC
) != 0
1234 && (! info
->symbolic
1235 || (h
->elf_link_hash_flags
1236 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
1238 Elf_Internal_Rela outrel
;
1241 /* When generating a shared object, these relocations
1242 are copied into the output file to be resolved at run
1249 name
= (bfd_elf_string_from_elf_section
1251 elf_elfheader (input_bfd
)->e_shstrndx
,
1252 elf_section_data (input_section
)->rel_hdr
.sh_name
));
1256 BFD_ASSERT (strncmp (name
, ".rela", 5) == 0
1257 && strcmp (bfd_get_section_name (input_bfd
,
1261 sreloc
= bfd_get_section_by_name (dynobj
, name
);
1262 BFD_ASSERT (sreloc
!= NULL
);
1267 if (elf_section_data (input_section
)->sec_info
== NULL
1268 || (input_section
->sec_info_type
!= ELF_INFO_TYPE_STABS
))
1269 outrel
.r_offset
= offset
;
1274 off
= (_bfd_stab_section_offset
1275 (output_bfd
, & elf_hash_table (info
)->stab_info
,
1277 & elf_section_data (input_section
)->sec_info
,
1279 if (off
== (bfd_vma
) -1)
1281 outrel
.r_offset
= off
;
1284 outrel
.r_offset
+= (input_section
->output_section
->vma
1285 + input_section
->output_offset
);
1288 memset (&outrel
, 0, sizeof outrel
);
1291 BFD_ASSERT (h
!= NULL
&& h
->dynindx
!= -1);
1292 outrel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_PCREL32
);
1293 outrel
.r_addend
= addend
;
1296 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1297 (bfd_byte
*) (((Elf32_External_Rela
*)
1299 + sreloc
->reloc_count
));
1300 ++sreloc
->reloc_count
;
1302 return bfd_reloc_ok
;
1305 value
-= (input_section
->output_section
->vma
1306 + input_section
->output_offset
);
1310 bfd_put_32 (input_bfd
, value
, hit_data
);
1311 return bfd_reloc_ok
;
1313 case R_MN10300_GNU_VTINHERIT
:
1314 case R_MN10300_GNU_VTENTRY
:
1315 return bfd_reloc_ok
;
1317 case R_MN10300_GOTPC32
:
1318 /* Use global offset table as symbol value. */
1320 value
= bfd_get_section_by_name (dynobj
,
1321 ".got")->output_section
->vma
;
1322 value
-= (input_section
->output_section
->vma
1323 + input_section
->output_offset
);
1327 bfd_put_32 (input_bfd
, value
, hit_data
);
1328 return bfd_reloc_ok
;
1330 case R_MN10300_GOTPC16
:
1331 /* Use global offset table as symbol value. */
1333 value
= bfd_get_section_by_name (dynobj
,
1334 ".got")->output_section
->vma
;
1335 value
-= (input_section
->output_section
->vma
1336 + input_section
->output_offset
);
1340 if ((long) value
> 0xffff || (long) value
< -0x10000)
1341 return bfd_reloc_overflow
;
1343 bfd_put_16 (input_bfd
, value
, hit_data
);
1344 return bfd_reloc_ok
;
1346 case R_MN10300_GOTOFF32
:
1347 value
-= bfd_get_section_by_name (dynobj
,
1348 ".got")->output_section
->vma
;
1351 bfd_put_32 (input_bfd
, value
, hit_data
);
1352 return bfd_reloc_ok
;
1354 case R_MN10300_GOTOFF24
:
1355 value
-= bfd_get_section_by_name (dynobj
,
1356 ".got")->output_section
->vma
;
1359 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1360 return bfd_reloc_overflow
;
1362 bfd_put_8 (input_bfd
, value
, hit_data
);
1363 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1364 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1365 return bfd_reloc_ok
;
1367 case R_MN10300_GOTOFF16
:
1368 value
-= bfd_get_section_by_name (dynobj
,
1369 ".got")->output_section
->vma
;
1372 if ((long) value
> 0xffff || (long) value
< -0x10000)
1373 return bfd_reloc_overflow
;
1375 bfd_put_16 (input_bfd
, value
, hit_data
);
1376 return bfd_reloc_ok
;
1378 case R_MN10300_PLT32
:
1380 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1381 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
1382 && h
->plt
.offset
!= (bfd_vma
) -1)
1386 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1388 value
= (splt
->output_section
->vma
1389 + splt
->output_offset
1390 + h
->plt
.offset
) - value
;
1393 value
-= (input_section
->output_section
->vma
1394 + input_section
->output_offset
);
1398 bfd_put_32 (input_bfd
, value
, hit_data
);
1399 return bfd_reloc_ok
;
1401 case R_MN10300_PLT16
:
1403 && ELF_ST_VISIBILITY (h
->other
) != STV_INTERNAL
1404 && ELF_ST_VISIBILITY (h
->other
) != STV_HIDDEN
1405 && h
->plt
.offset
!= (bfd_vma
) -1)
1409 splt
= bfd_get_section_by_name (dynobj
, ".plt");
1411 value
= (splt
->output_section
->vma
1412 + splt
->output_offset
1413 + h
->plt
.offset
) - value
;
1416 value
-= (input_section
->output_section
->vma
1417 + input_section
->output_offset
);
1421 if ((long) value
> 0xffff || (long) value
< -0x10000)
1422 return bfd_reloc_overflow
;
1424 bfd_put_16 (input_bfd
, value
, hit_data
);
1425 return bfd_reloc_ok
;
1427 case R_MN10300_GOT32
:
1428 case R_MN10300_GOT24
:
1429 case R_MN10300_GOT16
:
1433 sgot
= bfd_get_section_by_name (dynobj
, ".got");
1439 off
= h
->got
.offset
;
1440 BFD_ASSERT (off
!= (bfd_vma
) -1);
1442 if (! elf_hash_table (info
)->dynamic_sections_created
1444 && (info
->symbolic
|| h
->dynindx
== -1)
1445 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
)))
1446 /* This is actually a static link, or it is a
1447 -Bsymbolic link and the symbol is defined
1448 locally, or the symbol was forced to be local
1449 because of a version file. We must initialize
1450 this entry in the global offset table.
1452 When doing a dynamic link, we create a .rela.got
1453 relocation entry to initialize the value. This
1454 is done in the finish_dynamic_symbol routine. */
1455 bfd_put_32 (output_bfd
, value
,
1456 sgot
->contents
+ off
);
1458 value
= sgot
->output_offset
+ off
;
1464 off
= elf_local_got_offsets (input_bfd
)[symndx
];
1466 bfd_put_32 (output_bfd
, value
, sgot
->contents
+ off
);
1471 Elf_Internal_Rela outrel
;
1473 srelgot
= bfd_get_section_by_name (dynobj
, ".rela.got");
1474 BFD_ASSERT (srelgot
!= NULL
);
1476 outrel
.r_offset
= (sgot
->output_section
->vma
1477 + sgot
->output_offset
1479 outrel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
1480 outrel
.r_addend
= value
;
1481 bfd_elf32_swap_reloca_out (output_bfd
, &outrel
,
1482 (bfd_byte
*) (((Elf32_External_Rela
*)
1484 + srelgot
->reloc_count
));
1485 ++ srelgot
->reloc_count
;
1488 value
= sgot
->output_offset
+ off
;
1494 if (r_type
== R_MN10300_GOT32
)
1496 bfd_put_32 (input_bfd
, value
, hit_data
);
1497 return bfd_reloc_ok
;
1499 else if (r_type
== R_MN10300_GOT24
)
1501 if ((long) value
> 0x7fffff || (long) value
< -0x800000)
1502 return bfd_reloc_overflow
;
1504 bfd_put_8 (input_bfd
, value
& 0xff, hit_data
);
1505 bfd_put_8 (input_bfd
, (value
>> 8) & 0xff, hit_data
+ 1);
1506 bfd_put_8 (input_bfd
, (value
>> 16) & 0xff, hit_data
+ 2);
1507 return bfd_reloc_ok
;
1509 else if (r_type
== R_MN10300_GOT16
)
1511 if ((long) value
> 0xffff || (long) value
< -0x10000)
1512 return bfd_reloc_overflow
;
1514 bfd_put_16 (input_bfd
, value
, hit_data
);
1515 return bfd_reloc_ok
;
1520 return bfd_reloc_notsupported
;
1524 /* Relocate an MN10300 ELF section. */
1526 mn10300_elf_relocate_section (output_bfd
, info
, input_bfd
, input_section
,
1527 contents
, relocs
, local_syms
, local_sections
)
1529 struct bfd_link_info
*info
;
1531 asection
*input_section
;
1533 Elf_Internal_Rela
*relocs
;
1534 Elf_Internal_Sym
*local_syms
;
1535 asection
**local_sections
;
1537 Elf_Internal_Shdr
*symtab_hdr
;
1538 struct elf32_mn10300_link_hash_entry
**sym_hashes
;
1539 Elf_Internal_Rela
*rel
, *relend
;
1541 if (info
->relocatable
)
1544 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1545 sym_hashes
= (struct elf32_mn10300_link_hash_entry
**)
1546 (elf_sym_hashes (input_bfd
));
1549 relend
= relocs
+ input_section
->reloc_count
;
1550 for (; rel
< relend
; rel
++)
1553 reloc_howto_type
*howto
;
1554 unsigned long r_symndx
;
1555 Elf_Internal_Sym
*sym
;
1557 struct elf32_mn10300_link_hash_entry
*h
;
1559 bfd_reloc_status_type r
;
1561 r_symndx
= ELF32_R_SYM (rel
->r_info
);
1562 r_type
= ELF32_R_TYPE (rel
->r_info
);
1563 howto
= elf_mn10300_howto_table
+ r_type
;
1565 /* Just skip the vtable gc relocs. */
1566 if (r_type
== R_MN10300_GNU_VTINHERIT
1567 || r_type
== R_MN10300_GNU_VTENTRY
)
1573 if (r_symndx
< symtab_hdr
->sh_info
)
1575 sym
= local_syms
+ r_symndx
;
1576 sec
= local_sections
[r_symndx
];
1577 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, sec
, rel
);
1581 bfd_boolean unresolved_reloc
;
1583 struct elf_link_hash_entry
*hh
;
1585 RELOC_FOR_GLOBAL_SYMBOL (hh
, (struct elf_link_hash_entry
*) sym_hashes
,
1586 r_symndx
, symtab_hdr
, relocation
,
1587 sec
, unresolved_reloc
, info
,
1590 h
= (struct elf32_mn10300_link_hash_entry
*) hh
;
1592 if ((h
->root
.root
.type
== bfd_link_hash_defined
1593 || h
->root
.root
.type
== bfd_link_hash_defweak
)
1594 && ( r_type
== R_MN10300_GOTPC32
1595 || r_type
== R_MN10300_GOTPC16
1596 || (( r_type
== R_MN10300_PLT32
1597 || r_type
== R_MN10300_PLT16
)
1598 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_INTERNAL
1599 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_HIDDEN
1600 && h
->root
.plt
.offset
!= (bfd_vma
) -1)
1601 || (( r_type
== R_MN10300_GOT32
1602 || r_type
== R_MN10300_GOT24
1603 || r_type
== R_MN10300_GOT16
)
1604 && elf_hash_table (info
)->dynamic_sections_created
1606 || (! info
->symbolic
&& h
->root
.dynindx
!= -1)
1607 || (h
->root
.elf_link_hash_flags
1608 & ELF_LINK_HASH_DEF_REGULAR
) == 0))
1610 && ((! info
->symbolic
&& h
->root
.dynindx
!= -1)
1611 || (h
->root
.elf_link_hash_flags
1612 & ELF_LINK_HASH_DEF_REGULAR
) == 0)
1613 && ( r_type
== R_MN10300_32
1614 || r_type
== R_MN10300_PCREL32
)
1615 && ((input_section
->flags
& SEC_ALLOC
) != 0
1616 /* DWARF will emit R_MN10300_32 relocations
1617 in its sections against symbols defined
1618 externally in shared libraries. We can't
1619 do anything with them here. */
1620 || ((input_section
->flags
& SEC_DEBUGGING
) != 0
1621 && (h
->root
.elf_link_hash_flags
1622 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0)))))
1623 /* In these cases, we don't need the relocation
1624 value. We check specially because in some
1625 obscure cases sec->output_section will be NULL. */
1628 else if (unresolved_reloc
)
1629 (*_bfd_error_handler
)
1630 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1631 bfd_get_filename (input_bfd
), h
->root
.root
.root
.string
,
1632 bfd_get_section_name (input_bfd
, input_section
));
1635 r
= mn10300_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
1637 contents
, rel
->r_offset
,
1638 relocation
, rel
->r_addend
,
1639 (struct elf_link_hash_entry
*)h
,
1641 info
, sec
, h
== NULL
);
1643 if (r
!= bfd_reloc_ok
)
1646 const char *msg
= (const char *) 0;
1649 name
= h
->root
.root
.root
.string
;
1652 name
= (bfd_elf_string_from_elf_section
1653 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1654 if (name
== NULL
|| *name
== '\0')
1655 name
= bfd_section_name (input_bfd
, sec
);
1660 case bfd_reloc_overflow
:
1661 if (! ((*info
->callbacks
->reloc_overflow
)
1662 (info
, name
, howto
->name
, (bfd_vma
) 0,
1663 input_bfd
, input_section
, rel
->r_offset
)))
1667 case bfd_reloc_undefined
:
1668 if (! ((*info
->callbacks
->undefined_symbol
)
1669 (info
, name
, input_bfd
, input_section
,
1670 rel
->r_offset
, TRUE
)))
1674 case bfd_reloc_outofrange
:
1675 msg
= _("internal error: out of range error");
1678 case bfd_reloc_notsupported
:
1679 msg
= _("internal error: unsupported relocation error");
1682 case bfd_reloc_dangerous
:
1683 msg
= _("internal error: dangerous error");
1687 msg
= _("internal error: unknown error");
1691 if (!((*info
->callbacks
->warning
)
1692 (info
, msg
, name
, input_bfd
, input_section
,
1703 /* Finish initializing one hash table entry. */
1705 elf32_mn10300_finish_hash_table_entry (gen_entry
, in_args
)
1706 struct bfd_hash_entry
*gen_entry
;
1707 PTR in_args ATTRIBUTE_UNUSED
;
1709 struct elf32_mn10300_link_hash_entry
*entry
;
1710 unsigned int byte_count
= 0;
1712 entry
= (struct elf32_mn10300_link_hash_entry
*) gen_entry
;
1714 if (entry
->root
.root
.type
== bfd_link_hash_warning
)
1715 entry
= (struct elf32_mn10300_link_hash_entry
*) entry
->root
.root
.u
.i
.link
;
1717 /* If we already know we want to convert "call" to "calls" for calls
1718 to this symbol, then return now. */
1719 if (entry
->flags
== MN10300_CONVERT_CALL_TO_CALLS
)
1722 /* If there are no named calls to this symbol, or there's nothing we
1723 can move from the function itself into the "call" instruction, then
1724 note that all "call" instructions should be converted into "calls"
1725 instructions and return. */
1726 if (entry
->direct_calls
== 0
1727 || (entry
->stack_size
== 0 && entry
->movm_args
== 0))
1729 /* Make a note that we should convert "call" instructions to "calls"
1730 instructions for calls to this symbol. */
1731 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1735 /* We may be able to move some instructions from the function itself into
1736 the "call" instruction. Count how many bytes we might be able to
1737 eliminate in the function itself. */
1739 /* A movm instruction is two bytes. */
1740 if (entry
->movm_args
)
1743 /* Count the insn to allocate stack space too. */
1744 if (entry
->stack_size
> 0 && entry
->stack_size
<= 128)
1746 else if (entry
->stack_size
> 0 && entry
->stack_size
< 256)
1749 /* If using "call" will result in larger code, then turn all
1750 the associated "call" instructions into "calls" instrutions. */
1751 if (byte_count
< entry
->direct_calls
)
1752 entry
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1754 /* This routine never fails. */
1758 /* This function handles relaxing for the mn10300.
1760 There's quite a few relaxing opportunites available on the mn10300:
1762 * calls:32 -> calls:16 2 bytes
1763 * call:32 -> call:16 2 bytes
1765 * call:32 -> calls:32 1 byte
1766 * call:16 -> calls:16 1 byte
1767 * These are done anytime using "calls" would result
1768 in smaller code, or when necessary to preserve the
1769 meaning of the program.
1773 * In some circumstances we can move instructions
1774 from a function prologue into a "call" instruction.
1775 This is only done if the resulting code is no larger
1776 than the original code.
1778 * jmp:32 -> jmp:16 2 bytes
1779 * jmp:16 -> bra:8 1 byte
1781 * If the previous instruction is a conditional branch
1782 around the jump/bra, we may be able to reverse its condition
1783 and change its target to the jump's target. The jump/bra
1784 can then be deleted. 2 bytes
1786 * mov abs32 -> mov abs16 1 or 2 bytes
1788 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
1789 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
1791 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
1792 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
1794 We don't handle imm16->imm8 or d16->d8 as they're very rare
1795 and somewhat more difficult to support. */
1798 mn10300_elf_relax_section (abfd
, sec
, link_info
, again
)
1801 struct bfd_link_info
*link_info
;
1804 Elf_Internal_Shdr
*symtab_hdr
;
1805 Elf_Internal_Rela
*internal_relocs
= NULL
;
1806 Elf_Internal_Rela
*irel
, *irelend
;
1807 bfd_byte
*contents
= NULL
;
1808 Elf_Internal_Sym
*isymbuf
= NULL
;
1809 struct elf32_mn10300_link_hash_table
*hash_table
;
1810 asection
*section
= sec
;
1812 /* Assume nothing changes. */
1815 /* We need a pointer to the mn10300 specific hash table. */
1816 hash_table
= elf32_mn10300_hash_table (link_info
);
1818 /* Initialize fields in each hash table entry the first time through. */
1819 if ((hash_table
->flags
& MN10300_HASH_ENTRIES_INITIALIZED
) == 0)
1823 /* Iterate over all the input bfds. */
1824 for (input_bfd
= link_info
->input_bfds
;
1826 input_bfd
= input_bfd
->link_next
)
1828 /* We're going to need all the symbols for each bfd. */
1829 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
1830 if (symtab_hdr
->sh_info
!= 0)
1832 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
1833 if (isymbuf
== NULL
)
1834 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
1835 symtab_hdr
->sh_info
, 0,
1837 if (isymbuf
== NULL
)
1841 /* Iterate over each section in this bfd. */
1842 for (section
= input_bfd
->sections
;
1844 section
= section
->next
)
1846 struct elf32_mn10300_link_hash_entry
*hash
;
1847 Elf_Internal_Sym
*sym
;
1848 asection
*sym_sec
= NULL
;
1849 const char *sym_name
;
1852 /* If there's nothing to do in this section, skip it. */
1853 if (! (((section
->flags
& SEC_RELOC
) != 0
1854 && section
->reloc_count
!= 0)
1855 || (section
->flags
& SEC_CODE
) != 0))
1858 /* Get cached copy of section contents if it exists. */
1859 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
1860 contents
= elf_section_data (section
)->this_hdr
.contents
;
1861 else if (section
->_raw_size
!= 0)
1863 /* Go get them off disk. */
1864 contents
= (bfd_byte
*) bfd_malloc (section
->_raw_size
);
1865 if (contents
== NULL
)
1868 if (!bfd_get_section_contents (input_bfd
, section
,
1869 contents
, (file_ptr
) 0,
1870 section
->_raw_size
))
1876 /* If there aren't any relocs, then there's nothing to do. */
1877 if ((section
->flags
& SEC_RELOC
) != 0
1878 && section
->reloc_count
!= 0)
1881 /* Get a copy of the native relocations. */
1882 internal_relocs
= (_bfd_elf_link_read_relocs
1883 (input_bfd
, section
, (PTR
) NULL
,
1884 (Elf_Internal_Rela
*) NULL
,
1885 link_info
->keep_memory
));
1886 if (internal_relocs
== NULL
)
1889 /* Now examine each relocation. */
1890 irel
= internal_relocs
;
1891 irelend
= irel
+ section
->reloc_count
;
1892 for (; irel
< irelend
; irel
++)
1895 unsigned long r_index
;
1898 r_type
= ELF32_R_TYPE (irel
->r_info
);
1899 r_index
= ELF32_R_SYM (irel
->r_info
);
1901 if (r_type
< 0 || r_type
>= (int) R_MN10300_MAX
)
1904 /* We need the name and hash table entry of the target
1910 if (r_index
< symtab_hdr
->sh_info
)
1912 /* A local symbol. */
1913 Elf_Internal_Sym
*isym
;
1914 struct elf_link_hash_table
*elftab
;
1917 isym
= isymbuf
+ r_index
;
1918 if (isym
->st_shndx
== SHN_UNDEF
)
1919 sym_sec
= bfd_und_section_ptr
;
1920 else if (isym
->st_shndx
== SHN_ABS
)
1921 sym_sec
= bfd_abs_section_ptr
;
1922 else if (isym
->st_shndx
== SHN_COMMON
)
1923 sym_sec
= bfd_com_section_ptr
;
1926 = bfd_section_from_elf_index (input_bfd
,
1930 = bfd_elf_string_from_elf_section (input_bfd
,
1935 /* If it isn't a function, then we don't care
1937 if (ELF_ST_TYPE (isym
->st_info
) != STT_FUNC
)
1940 /* Tack on an ID so we can uniquely identify this
1941 local symbol in the global hash table. */
1942 amt
= strlen (sym_name
) + 10;
1943 new_name
= bfd_malloc (amt
);
1947 sprintf (new_name
, "%s_%08x",
1948 sym_name
, (int) sym_sec
);
1949 sym_name
= new_name
;
1951 elftab
= &hash_table
->static_hash_table
->root
;
1952 hash
= ((struct elf32_mn10300_link_hash_entry
*)
1953 elf_link_hash_lookup (elftab
, sym_name
,
1954 TRUE
, TRUE
, FALSE
));
1959 r_index
-= symtab_hdr
->sh_info
;
1960 hash
= (struct elf32_mn10300_link_hash_entry
*)
1961 elf_sym_hashes (input_bfd
)[r_index
];
1964 /* If this is not a "call" instruction, then we
1965 should convert "call" instructions to "calls"
1967 code
= bfd_get_8 (input_bfd
,
1968 contents
+ irel
->r_offset
- 1);
1969 if (code
!= 0xdd && code
!= 0xcd)
1970 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1972 /* If this is a jump/call, then bump the
1973 direct_calls counter. Else force "call" to
1974 "calls" conversions. */
1975 if (r_type
== R_MN10300_PCREL32
1976 || r_type
== R_MN10300_PLT32
1977 || r_type
== R_MN10300_PLT16
1978 || r_type
== R_MN10300_PCREL16
)
1979 hash
->direct_calls
++;
1981 hash
->flags
|= MN10300_CONVERT_CALL_TO_CALLS
;
1985 /* Now look at the actual contents to get the stack size,
1986 and a list of what registers were saved in the prologue
1988 if ((section
->flags
& SEC_CODE
) != 0)
1990 Elf_Internal_Sym
*isym
, *isymend
;
1991 unsigned int sec_shndx
;
1992 struct elf_link_hash_entry
**hashes
;
1993 struct elf_link_hash_entry
**end_hashes
;
1994 unsigned int symcount
;
1996 sec_shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
1999 /* Look at each function defined in this section and
2000 update info for that function. */
2001 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2002 for (isym
= isymbuf
; isym
< isymend
; isym
++)
2004 if (isym
->st_shndx
== sec_shndx
2005 && ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)
2007 struct elf_link_hash_table
*elftab
;
2010 if (isym
->st_shndx
== SHN_UNDEF
)
2011 sym_sec
= bfd_und_section_ptr
;
2012 else if (isym
->st_shndx
== SHN_ABS
)
2013 sym_sec
= bfd_abs_section_ptr
;
2014 else if (isym
->st_shndx
== SHN_COMMON
)
2015 sym_sec
= bfd_com_section_ptr
;
2018 = bfd_section_from_elf_index (input_bfd
,
2021 sym_name
= (bfd_elf_string_from_elf_section
2022 (input_bfd
, symtab_hdr
->sh_link
,
2025 /* Tack on an ID so we can uniquely identify this
2026 local symbol in the global hash table. */
2027 amt
= strlen (sym_name
) + 10;
2028 new_name
= bfd_malloc (amt
);
2032 sprintf (new_name
, "%s_%08x",
2033 sym_name
, (int) sym_sec
);
2034 sym_name
= new_name
;
2036 elftab
= &hash_table
->static_hash_table
->root
;
2037 hash
= ((struct elf32_mn10300_link_hash_entry
*)
2038 elf_link_hash_lookup (elftab
, sym_name
,
2039 TRUE
, TRUE
, FALSE
));
2041 compute_function_info (input_bfd
, hash
,
2042 isym
->st_value
, contents
);
2046 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2047 - symtab_hdr
->sh_info
);
2048 hashes
= elf_sym_hashes (input_bfd
);
2049 end_hashes
= hashes
+ symcount
;
2050 for (; hashes
< end_hashes
; hashes
++)
2052 hash
= (struct elf32_mn10300_link_hash_entry
*) *hashes
;
2053 if ((hash
->root
.root
.type
== bfd_link_hash_defined
2054 || hash
->root
.root
.type
== bfd_link_hash_defweak
)
2055 && hash
->root
.root
.u
.def
.section
== section
2056 && ELF_ST_TYPE (isym
->st_info
) == STT_FUNC
)
2057 compute_function_info (input_bfd
, hash
,
2058 (hash
)->root
.root
.u
.def
.value
,
2063 /* Cache or free any memory we allocated for the relocs. */
2064 if (internal_relocs
!= NULL
2065 && elf_section_data (section
)->relocs
!= internal_relocs
)
2066 free (internal_relocs
);
2067 internal_relocs
= NULL
;
2069 /* Cache or free any memory we allocated for the contents. */
2070 if (contents
!= NULL
2071 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
2073 if (! link_info
->keep_memory
)
2077 /* Cache the section contents for elf_link_input_bfd. */
2078 elf_section_data (section
)->this_hdr
.contents
= contents
;
2084 /* Cache or free any memory we allocated for the symbols. */
2086 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2088 if (! link_info
->keep_memory
)
2092 /* Cache the symbols for elf_link_input_bfd. */
2093 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2099 /* Now iterate on each symbol in the hash table and perform
2100 the final initialization steps on each. */
2101 elf32_mn10300_link_hash_traverse (hash_table
,
2102 elf32_mn10300_finish_hash_table_entry
,
2104 elf32_mn10300_link_hash_traverse (hash_table
->static_hash_table
,
2105 elf32_mn10300_finish_hash_table_entry
,
2108 /* All entries in the hash table are fully initialized. */
2109 hash_table
->flags
|= MN10300_HASH_ENTRIES_INITIALIZED
;
2111 /* Now that everything has been initialized, go through each
2112 code section and delete any prologue insns which will be
2113 redundant because their operations will be performed by
2114 a "call" instruction. */
2115 for (input_bfd
= link_info
->input_bfds
;
2117 input_bfd
= input_bfd
->link_next
)
2119 /* We're going to need all the local symbols for each bfd. */
2120 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
2121 if (symtab_hdr
->sh_info
!= 0)
2123 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2124 if (isymbuf
== NULL
)
2125 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
2126 symtab_hdr
->sh_info
, 0,
2128 if (isymbuf
== NULL
)
2132 /* Walk over each section in this bfd. */
2133 for (section
= input_bfd
->sections
;
2135 section
= section
->next
)
2137 unsigned int sec_shndx
;
2138 Elf_Internal_Sym
*isym
, *isymend
;
2139 struct elf_link_hash_entry
**hashes
;
2140 struct elf_link_hash_entry
**end_hashes
;
2141 unsigned int symcount
;
2143 /* Skip non-code sections and empty sections. */
2144 if ((section
->flags
& SEC_CODE
) == 0 || section
->_raw_size
== 0)
2147 if (section
->reloc_count
!= 0)
2149 /* Get a copy of the native relocations. */
2150 internal_relocs
= (_bfd_elf_link_read_relocs
2151 (input_bfd
, section
, (PTR
) NULL
,
2152 (Elf_Internal_Rela
*) NULL
,
2153 link_info
->keep_memory
));
2154 if (internal_relocs
== NULL
)
2158 /* Get cached copy of section contents if it exists. */
2159 if (elf_section_data (section
)->this_hdr
.contents
!= NULL
)
2160 contents
= elf_section_data (section
)->this_hdr
.contents
;
2163 /* Go get them off disk. */
2164 contents
= (bfd_byte
*) bfd_malloc (section
->_raw_size
);
2165 if (contents
== NULL
)
2168 if (!bfd_get_section_contents (input_bfd
, section
,
2169 contents
, (file_ptr
) 0,
2170 section
->_raw_size
))
2174 sec_shndx
= _bfd_elf_section_from_bfd_section (input_bfd
,
2177 /* Now look for any function in this section which needs
2178 insns deleted from its prologue. */
2179 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
2180 for (isym
= isymbuf
; isym
< isymend
; isym
++)
2182 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2183 asection
*sym_sec
= NULL
;
2184 const char *sym_name
;
2186 struct elf_link_hash_table
*elftab
;
2189 if (isym
->st_shndx
!= sec_shndx
)
2192 if (isym
->st_shndx
== SHN_UNDEF
)
2193 sym_sec
= bfd_und_section_ptr
;
2194 else if (isym
->st_shndx
== SHN_ABS
)
2195 sym_sec
= bfd_abs_section_ptr
;
2196 else if (isym
->st_shndx
== SHN_COMMON
)
2197 sym_sec
= bfd_com_section_ptr
;
2200 = bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
2203 = bfd_elf_string_from_elf_section (input_bfd
,
2204 symtab_hdr
->sh_link
,
2207 /* Tack on an ID so we can uniquely identify this
2208 local symbol in the global hash table. */
2209 amt
= strlen (sym_name
) + 10;
2210 new_name
= bfd_malloc (amt
);
2213 sprintf (new_name
, "%s_%08x", sym_name
, (int) sym_sec
);
2214 sym_name
= new_name
;
2216 elftab
= &hash_table
->static_hash_table
->root
;
2217 sym_hash
= ((struct elf32_mn10300_link_hash_entry
*)
2218 elf_link_hash_lookup (elftab
, sym_name
,
2219 FALSE
, FALSE
, FALSE
));
2222 if (sym_hash
== NULL
)
2225 if (! (sym_hash
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
2226 && ! (sym_hash
->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
2230 /* Note that we've changed things. */
2231 elf_section_data (section
)->relocs
= internal_relocs
;
2232 elf_section_data (section
)->this_hdr
.contents
= contents
;
2233 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2235 /* Count how many bytes we're going to delete. */
2236 if (sym_hash
->movm_args
)
2239 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
2241 else if (sym_hash
->stack_size
2242 && sym_hash
->stack_size
< 256)
2245 /* Note that we've deleted prologue bytes for this
2247 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
2249 /* Actually delete the bytes. */
2250 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
2256 /* Something changed. Not strictly necessary, but
2257 may lead to more relaxing opportunities. */
2262 /* Look for any global functions in this section which
2263 need insns deleted from their prologues. */
2264 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
2265 - symtab_hdr
->sh_info
);
2266 hashes
= elf_sym_hashes (input_bfd
);
2267 end_hashes
= hashes
+ symcount
;
2268 for (; hashes
< end_hashes
; hashes
++)
2270 struct elf32_mn10300_link_hash_entry
*sym_hash
;
2272 sym_hash
= (struct elf32_mn10300_link_hash_entry
*) *hashes
;
2273 if ((sym_hash
->root
.root
.type
== bfd_link_hash_defined
2274 || sym_hash
->root
.root
.type
== bfd_link_hash_defweak
)
2275 && sym_hash
->root
.root
.u
.def
.section
== section
2276 && ! (sym_hash
->flags
& MN10300_CONVERT_CALL_TO_CALLS
)
2277 && ! (sym_hash
->flags
& MN10300_DELETED_PROLOGUE_BYTES
))
2282 /* Note that we've changed things. */
2283 elf_section_data (section
)->relocs
= internal_relocs
;
2284 elf_section_data (section
)->this_hdr
.contents
= contents
;
2285 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2287 /* Count how many bytes we're going to delete. */
2288 if (sym_hash
->movm_args
)
2291 if (sym_hash
->stack_size
&& sym_hash
->stack_size
<= 128)
2293 else if (sym_hash
->stack_size
2294 && sym_hash
->stack_size
< 256)
2297 /* Note that we've deleted prologue bytes for this
2299 sym_hash
->flags
|= MN10300_DELETED_PROLOGUE_BYTES
;
2301 /* Actually delete the bytes. */
2302 symval
= sym_hash
->root
.root
.u
.def
.value
;
2303 if (!mn10300_elf_relax_delete_bytes (input_bfd
,
2309 /* Something changed. Not strictly necessary, but
2310 may lead to more relaxing opportunities. */
2315 /* Cache or free any memory we allocated for the relocs. */
2316 if (internal_relocs
!= NULL
2317 && elf_section_data (section
)->relocs
!= internal_relocs
)
2318 free (internal_relocs
);
2319 internal_relocs
= NULL
;
2321 /* Cache or free any memory we allocated for the contents. */
2322 if (contents
!= NULL
2323 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
2325 if (! link_info
->keep_memory
)
2329 /* Cache the section contents for elf_link_input_bfd. */
2330 elf_section_data (section
)->this_hdr
.contents
= contents
;
2336 /* Cache or free any memory we allocated for the symbols. */
2338 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
2340 if (! link_info
->keep_memory
)
2344 /* Cache the symbols for elf_link_input_bfd. */
2345 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2352 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2354 internal_relocs
= NULL
;
2356 /* For error_return. */
2359 /* We don't have to do anything for a relocatable link, if
2360 this section does not have relocs, or if this is not a
2362 if (link_info
->relocatable
2363 || (sec
->flags
& SEC_RELOC
) == 0
2364 || sec
->reloc_count
== 0
2365 || (sec
->flags
& SEC_CODE
) == 0)
2368 /* If this is the first time we have been called for this section,
2369 initialize the cooked size. */
2370 if (sec
->_cooked_size
== 0)
2371 sec
->_cooked_size
= sec
->_raw_size
;
2373 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
2375 /* Get a copy of the native relocations. */
2376 internal_relocs
= (_bfd_elf_link_read_relocs
2377 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
2378 link_info
->keep_memory
));
2379 if (internal_relocs
== NULL
)
2382 /* Walk through them looking for relaxing opportunities. */
2383 irelend
= internal_relocs
+ sec
->reloc_count
;
2384 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
2387 struct elf32_mn10300_link_hash_entry
*h
= NULL
;
2389 /* If this isn't something that can be relaxed, then ignore
2391 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_NONE
2392 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_8
2393 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_MAX
)
2396 /* Get the section contents if we haven't done so already. */
2397 if (contents
== NULL
)
2399 /* Get cached copy if it exists. */
2400 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
2401 contents
= elf_section_data (sec
)->this_hdr
.contents
;
2404 /* Go get them off disk. */
2405 contents
= (bfd_byte
*) bfd_malloc (sec
->_raw_size
);
2406 if (contents
== NULL
)
2409 if (! bfd_get_section_contents (abfd
, sec
, contents
,
2410 (file_ptr
) 0, sec
->_raw_size
))
2415 /* Read this BFD's symbols if we haven't done so already. */
2416 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
2418 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
2419 if (isymbuf
== NULL
)
2420 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
2421 symtab_hdr
->sh_info
, 0,
2423 if (isymbuf
== NULL
)
2427 /* Get the value of the symbol referred to by the reloc. */
2428 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
2430 Elf_Internal_Sym
*isym
;
2431 asection
*sym_sec
= NULL
;
2432 const char *sym_name
;
2435 /* A local symbol. */
2436 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
2437 if (isym
->st_shndx
== SHN_UNDEF
)
2438 sym_sec
= bfd_und_section_ptr
;
2439 else if (isym
->st_shndx
== SHN_ABS
)
2440 sym_sec
= bfd_abs_section_ptr
;
2441 else if (isym
->st_shndx
== SHN_COMMON
)
2442 sym_sec
= bfd_com_section_ptr
;
2444 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
2446 symval
= (isym
->st_value
2447 + sym_sec
->output_section
->vma
2448 + sym_sec
->output_offset
);
2449 sym_name
= bfd_elf_string_from_elf_section (abfd
,
2450 symtab_hdr
->sh_link
,
2453 /* Tack on an ID so we can uniquely identify this
2454 local symbol in the global hash table. */
2455 new_name
= bfd_malloc ((bfd_size_type
) strlen (sym_name
) + 10);
2458 sprintf (new_name
, "%s_%08x", sym_name
, (int) sym_sec
);
2459 sym_name
= new_name
;
2461 h
= (struct elf32_mn10300_link_hash_entry
*)
2462 elf_link_hash_lookup (&hash_table
->static_hash_table
->root
,
2463 sym_name
, FALSE
, FALSE
, FALSE
);
2470 /* An external symbol. */
2471 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
2472 h
= (struct elf32_mn10300_link_hash_entry
*)
2473 (elf_sym_hashes (abfd
)[indx
]);
2474 BFD_ASSERT (h
!= NULL
);
2475 if (h
->root
.root
.type
!= bfd_link_hash_defined
2476 && h
->root
.root
.type
!= bfd_link_hash_defweak
)
2478 /* This appears to be a reference to an undefined
2479 symbol. Just ignore it--it will be caught by the
2480 regular reloc processing. */
2484 symval
= (h
->root
.root
.u
.def
.value
2485 + h
->root
.root
.u
.def
.section
->output_section
->vma
2486 + h
->root
.root
.u
.def
.section
->output_offset
);
2489 /* For simplicity of coding, we are going to modify the section
2490 contents, the section relocs, and the BFD symbol table. We
2491 must tell the rest of the code not to free up this
2492 information. It would be possible to instead create a table
2493 of changes which have to be made, as is done in coff-mips.c;
2494 that would be more work, but would require less memory when
2495 the linker is run. */
2497 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2498 branch/call, also deal with "call" -> "calls" conversions and
2499 insertion of prologue data into "call" instructions. */
2500 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL32
2501 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PLT32
)
2503 bfd_vma value
= symval
;
2505 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PLT32
2507 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_INTERNAL
2508 && ELF_ST_VISIBILITY (h
->root
.other
) != STV_HIDDEN
2509 && h
->root
.plt
.offset
!= (bfd_vma
) -1)
2513 splt
= bfd_get_section_by_name (elf_hash_table (link_info
)
2516 value
= ((splt
->output_section
->vma
2517 + splt
->output_offset
2518 + h
->root
.plt
.offset
)
2519 - (sec
->output_section
->vma
2520 + sec
->output_offset
2524 /* If we've got a "call" instruction that needs to be turned
2525 into a "calls" instruction, do so now. It saves a byte. */
2526 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
2530 /* Get the opcode. */
2531 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2533 /* Make sure we're working with a "call" instruction! */
2536 /* Note that we've changed the relocs, section contents,
2538 elf_section_data (sec
)->relocs
= internal_relocs
;
2539 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2540 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2542 /* Fix the opcode. */
2543 bfd_put_8 (abfd
, 0xfc, contents
+ irel
->r_offset
- 1);
2544 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
2546 /* Fix irel->r_offset and irel->r_addend. */
2547 irel
->r_offset
+= 1;
2548 irel
->r_addend
+= 1;
2550 /* Delete one byte of data. */
2551 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2552 irel
->r_offset
+ 3, 1))
2555 /* That will change things, so, we should relax again.
2556 Note that this is not required, and it may be slow. */
2562 /* We've got a "call" instruction which needs some data
2563 from target function filled in. */
2566 /* Get the opcode. */
2567 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2569 /* Insert data from the target function into the "call"
2570 instruction if needed. */
2573 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 4);
2574 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
2575 contents
+ irel
->r_offset
+ 5);
2579 /* Deal with pc-relative gunk. */
2580 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
2581 value
-= irel
->r_offset
;
2582 value
+= irel
->r_addend
;
2584 /* See if the value will fit in 16 bits, note the high value is
2585 0x7fff + 2 as the target will be two bytes closer if we are
2587 if ((long) value
< 0x8001 && (long) value
> -0x8000)
2591 /* Get the opcode. */
2592 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2594 if (code
!= 0xdc && code
!= 0xdd && code
!= 0xff)
2597 /* Note that we've changed the relocs, section contents, etc. */
2598 elf_section_data (sec
)->relocs
= internal_relocs
;
2599 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2600 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2602 /* Fix the opcode. */
2604 bfd_put_8 (abfd
, 0xcc, contents
+ irel
->r_offset
- 1);
2605 else if (code
== 0xdd)
2606 bfd_put_8 (abfd
, 0xcd, contents
+ irel
->r_offset
- 1);
2607 else if (code
== 0xff)
2608 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
2610 /* Fix the relocation's type. */
2611 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2612 (ELF32_R_TYPE (irel
->r_info
)
2613 == (int) R_MN10300_PLT32
)
2617 /* Delete two bytes of data. */
2618 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2619 irel
->r_offset
+ 1, 2))
2622 /* That will change things, so, we should relax again.
2623 Note that this is not required, and it may be slow. */
2628 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2630 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL16
)
2632 bfd_vma value
= symval
;
2634 /* If we've got a "call" instruction that needs to be turned
2635 into a "calls" instruction, do so now. It saves a byte. */
2636 if (h
&& (h
->flags
& MN10300_CONVERT_CALL_TO_CALLS
))
2640 /* Get the opcode. */
2641 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2643 /* Make sure we're working with a "call" instruction! */
2646 /* Note that we've changed the relocs, section contents,
2648 elf_section_data (sec
)->relocs
= internal_relocs
;
2649 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2650 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2652 /* Fix the opcode. */
2653 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 1);
2654 bfd_put_8 (abfd
, 0xff, contents
+ irel
->r_offset
);
2656 /* Fix irel->r_offset and irel->r_addend. */
2657 irel
->r_offset
+= 1;
2658 irel
->r_addend
+= 1;
2660 /* Delete one byte of data. */
2661 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2662 irel
->r_offset
+ 1, 1))
2665 /* That will change things, so, we should relax again.
2666 Note that this is not required, and it may be slow. */
2674 /* Get the opcode. */
2675 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2677 /* Insert data from the target function into the "call"
2678 instruction if needed. */
2681 bfd_put_8 (abfd
, h
->movm_args
, contents
+ irel
->r_offset
+ 2);
2682 bfd_put_8 (abfd
, h
->stack_size
+ h
->movm_stack_size
,
2683 contents
+ irel
->r_offset
+ 3);
2687 /* Deal with pc-relative gunk. */
2688 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
2689 value
-= irel
->r_offset
;
2690 value
+= irel
->r_addend
;
2692 /* See if the value will fit in 8 bits, note the high value is
2693 0x7f + 1 as the target will be one bytes closer if we are
2695 if ((long) value
< 0x80 && (long) value
> -0x80)
2699 /* Get the opcode. */
2700 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2705 /* Note that we've changed the relocs, section contents, etc. */
2706 elf_section_data (sec
)->relocs
= internal_relocs
;
2707 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2708 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2710 /* Fix the opcode. */
2711 bfd_put_8 (abfd
, 0xca, contents
+ irel
->r_offset
- 1);
2713 /* Fix the relocation's type. */
2714 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2717 /* Delete one byte of data. */
2718 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2719 irel
->r_offset
+ 1, 1))
2722 /* That will change things, so, we should relax again.
2723 Note that this is not required, and it may be slow. */
2728 /* Try to eliminate an unconditional 8 bit pc-relative branch
2729 which immediately follows a conditional 8 bit pc-relative
2730 branch around the unconditional branch.
2737 This happens when the bCC can't reach lab2 at assembly time,
2738 but due to other relaxations it can reach at link time. */
2739 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_PCREL8
)
2741 Elf_Internal_Rela
*nrel
;
2742 bfd_vma value
= symval
;
2745 /* Deal with pc-relative gunk. */
2746 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
2747 value
-= irel
->r_offset
;
2748 value
+= irel
->r_addend
;
2750 /* Do nothing if this reloc is the last byte in the section. */
2751 if (irel
->r_offset
== sec
->_cooked_size
)
2754 /* See if the next instruction is an unconditional pc-relative
2755 branch, more often than not this test will fail, so we
2756 test it first to speed things up. */
2757 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
+ 1);
2761 /* Also make sure the next relocation applies to the next
2762 instruction and that it's a pc-relative 8 bit branch. */
2765 || irel
->r_offset
+ 2 != nrel
->r_offset
2766 || ELF32_R_TYPE (nrel
->r_info
) != (int) R_MN10300_PCREL8
)
2769 /* Make sure our destination immediately follows the
2770 unconditional branch. */
2771 if (symval
!= (sec
->output_section
->vma
+ sec
->output_offset
2772 + irel
->r_offset
+ 3))
2775 /* Now make sure we are a conditional branch. This may not
2776 be necessary, but why take the chance.
2778 Note these checks assume that R_MN10300_PCREL8 relocs
2779 only occur on bCC and bCCx insns. If they occured
2780 elsewhere, we'd need to know the start of this insn
2781 for this check to be accurate. */
2782 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
2783 if (code
!= 0xc0 && code
!= 0xc1 && code
!= 0xc2
2784 && code
!= 0xc3 && code
!= 0xc4 && code
!= 0xc5
2785 && code
!= 0xc6 && code
!= 0xc7 && code
!= 0xc8
2786 && code
!= 0xc9 && code
!= 0xe8 && code
!= 0xe9
2787 && code
!= 0xea && code
!= 0xeb)
2790 /* We also have to be sure there is no symbol/label
2791 at the unconditional branch. */
2792 if (mn10300_elf_symbol_address_p (abfd
, sec
, isymbuf
,
2793 irel
->r_offset
+ 1))
2796 /* Note that we've changed the relocs, section contents, etc. */
2797 elf_section_data (sec
)->relocs
= internal_relocs
;
2798 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2799 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2801 /* Reverse the condition of the first branch. */
2847 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
2849 /* Set the reloc type and symbol for the first branch
2850 from the second branch. */
2851 irel
->r_info
= nrel
->r_info
;
2853 /* Make the reloc for the second branch a null reloc. */
2854 nrel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (nrel
->r_info
),
2857 /* Delete two bytes of data. */
2858 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2859 irel
->r_offset
+ 1, 2))
2862 /* That will change things, so, we should relax again.
2863 Note that this is not required, and it may be slow. */
2867 /* Try to turn a 24 immediate, displacement or absolute address
2868 into a 8 immediate, displacement or absolute address. */
2869 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_24
)
2871 bfd_vma value
= symval
;
2872 value
+= irel
->r_addend
;
2874 /* See if the value will fit in 8 bits. */
2875 if ((long) value
< 0x7f && (long) value
> -0x80)
2879 /* AM33 insns which have 24 operands are 6 bytes long and
2880 will have 0xfd as the first byte. */
2882 /* Get the first opcode. */
2883 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
2887 /* Get the second opcode. */
2888 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2890 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2891 equivalent instructions exists. */
2892 if (code
!= 0x6b && code
!= 0x7b
2893 && code
!= 0x8b && code
!= 0x9b
2894 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
2895 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
2896 || (code
& 0x0f) == 0x0e))
2898 /* Not safe if the high bit is on as relaxing may
2899 move the value out of high mem and thus not fit
2900 in a signed 8bit value. This is currently over
2902 if ((value
& 0x80) == 0)
2904 /* Note that we've changed the relocation contents,
2906 elf_section_data (sec
)->relocs
= internal_relocs
;
2907 elf_section_data (sec
)->this_hdr
.contents
= contents
;
2908 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
2910 /* Fix the opcode. */
2911 bfd_put_8 (abfd
, 0xfb, contents
+ irel
->r_offset
- 3);
2912 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
2914 /* Fix the relocation's type. */
2916 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
2919 /* Delete two bytes of data. */
2920 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
2921 irel
->r_offset
+ 1, 2))
2924 /* That will change things, so, we should relax
2925 again. Note that this is not required, and it
2935 /* Try to turn a 32bit immediate, displacement or absolute address
2936 into a 16bit immediate, displacement or absolute address. */
2937 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_32
2938 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOT32
2939 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTOFF32
2940 || ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTPC32
)
2942 bfd_vma value
= symval
;
2944 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_MN10300_32
)
2948 sgot
= bfd_get_section_by_name (elf_hash_table (link_info
)
2951 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOT32
)
2953 value
= sgot
->output_offset
;
2956 value
+= h
->root
.got
.offset
;
2958 value
+= (elf_local_got_offsets
2959 (abfd
)[ELF32_R_SYM (irel
->r_info
)]);
2961 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTOFF32
)
2962 value
-= sgot
->output_section
->vma
;
2963 else if (ELF32_R_TYPE (irel
->r_info
) == (int) R_MN10300_GOTPC32
)
2964 value
= (sgot
->output_section
->vma
2965 - (sec
->output_section
->vma
2966 + sec
->output_offset
2972 value
+= irel
->r_addend
;
2974 /* See if the value will fit in 24 bits.
2975 We allow any 16bit match here. We prune those we can't
2977 if ((long) value
< 0x7fffff && (long) value
> -0x800000)
2981 /* AM33 insns which have 32bit operands are 7 bytes long and
2982 will have 0xfe as the first byte. */
2984 /* Get the first opcode. */
2985 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 3);
2989 /* Get the second opcode. */
2990 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
2992 /* All the am33 32 -> 24 relaxing possibilities. */
2993 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2994 equivalent instructions exists. */
2995 if (code
!= 0x6b && code
!= 0x7b
2996 && code
!= 0x8b && code
!= 0x9b
2997 && (ELF32_R_TYPE (irel
->r_info
)
2998 != (int) R_MN10300_GOTPC32
)
2999 && ((code
& 0x0f) == 0x09 || (code
& 0x0f) == 0x08
3000 || (code
& 0x0f) == 0x0a || (code
& 0x0f) == 0x0b
3001 || (code
& 0x0f) == 0x0e))
3003 /* Not safe if the high bit is on as relaxing may
3004 move the value out of high mem and thus not fit
3005 in a signed 16bit value. This is currently over
3007 if ((value
& 0x8000) == 0)
3009 /* Note that we've changed the relocation contents,
3011 elf_section_data (sec
)->relocs
= internal_relocs
;
3012 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3013 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3015 /* Fix the opcode. */
3016 bfd_put_8 (abfd
, 0xfd, contents
+ irel
->r_offset
- 3);
3017 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3019 /* Fix the relocation's type. */
3021 ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3022 (ELF32_R_TYPE (irel
->r_info
)
3023 == (int) R_MN10300_GOTOFF32
)
3024 ? R_MN10300_GOTOFF24
3025 : (ELF32_R_TYPE (irel
->r_info
)
3026 == (int) R_MN10300_GOT32
)
3030 /* Delete one byte of data. */
3031 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3032 irel
->r_offset
+ 3, 1))
3035 /* That will change things, so, we should relax
3036 again. Note that this is not required, and it
3045 /* See if the value will fit in 16 bits.
3046 We allow any 16bit match here. We prune those we can't
3048 if ((long) value
< 0x7fff && (long) value
> -0x8000)
3052 /* Most insns which have 32bit operands are 6 bytes long;
3053 exceptions are pcrel insns and bit insns.
3055 We handle pcrel insns above. We don't bother trying
3056 to handle the bit insns here.
3058 The first byte of the remaining insns will be 0xfc. */
3060 /* Get the first opcode. */
3061 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 2);
3066 /* Get the second opcode. */
3067 code
= bfd_get_8 (abfd
, contents
+ irel
->r_offset
- 1);
3069 if ((code
& 0xf0) < 0x80)
3070 switch (code
& 0xf0)
3072 /* mov (d32,am),dn -> mov (d32,am),dn
3073 mov dm,(d32,am) -> mov dn,(d32,am)
3074 mov (d32,am),an -> mov (d32,am),an
3075 mov dm,(d32,am) -> mov dn,(d32,am)
3076 movbu (d32,am),dn -> movbu (d32,am),dn
3077 movbu dm,(d32,am) -> movbu dn,(d32,am)
3078 movhu (d32,am),dn -> movhu (d32,am),dn
3079 movhu dm,(d32,am) -> movhu dn,(d32,am) */
3088 /* Not safe if the high bit is on as relaxing may
3089 move the value out of high mem and thus not fit
3090 in a signed 16bit value. */
3092 && (value
& 0x8000))
3095 /* Note that we've changed the relocation contents, etc. */
3096 elf_section_data (sec
)->relocs
= internal_relocs
;
3097 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3098 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3100 /* Fix the opcode. */
3101 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3102 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3104 /* Fix the relocation's type. */
3105 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3106 (ELF32_R_TYPE (irel
->r_info
)
3107 == (int) R_MN10300_GOTOFF32
)
3108 ? R_MN10300_GOTOFF16
3109 : (ELF32_R_TYPE (irel
->r_info
)
3110 == (int) R_MN10300_GOT32
)
3112 : (ELF32_R_TYPE (irel
->r_info
)
3113 == (int) R_MN10300_GOTPC32
)
3114 ? R_MN10300_GOTPC16
:
3117 /* Delete two bytes of data. */
3118 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3119 irel
->r_offset
+ 2, 2))
3122 /* That will change things, so, we should relax again.
3123 Note that this is not required, and it may be slow. */
3127 else if ((code
& 0xf0) == 0x80
3128 || (code
& 0xf0) == 0x90)
3129 switch (code
& 0xf3)
3131 /* mov dn,(abs32) -> mov dn,(abs16)
3132 movbu dn,(abs32) -> movbu dn,(abs16)
3133 movhu dn,(abs32) -> movhu dn,(abs16) */
3137 /* Note that we've changed the relocation contents, etc. */
3138 elf_section_data (sec
)->relocs
= internal_relocs
;
3139 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3140 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3142 if ((code
& 0xf3) == 0x81)
3143 code
= 0x01 + (code
& 0x0c);
3144 else if ((code
& 0xf3) == 0x82)
3145 code
= 0x02 + (code
& 0x0c);
3146 else if ((code
& 0xf3) == 0x83)
3147 code
= 0x03 + (code
& 0x0c);
3151 /* Fix the opcode. */
3152 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3154 /* Fix the relocation's type. */
3155 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3156 (ELF32_R_TYPE (irel
->r_info
)
3157 == (int) R_MN10300_GOTOFF32
)
3158 ? R_MN10300_GOTOFF16
3159 : (ELF32_R_TYPE (irel
->r_info
)
3160 == (int) R_MN10300_GOT32
)
3162 : (ELF32_R_TYPE (irel
->r_info
)
3163 == (int) R_MN10300_GOTPC32
)
3164 ? R_MN10300_GOTPC16
:
3167 /* The opcode got shorter too, so we have to fix the
3168 addend and offset too! */
3169 irel
->r_offset
-= 1;
3171 /* Delete three bytes of data. */
3172 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3173 irel
->r_offset
+ 1, 3))
3176 /* That will change things, so, we should relax again.
3177 Note that this is not required, and it may be slow. */
3181 /* mov am,(abs32) -> mov am,(abs16)
3182 mov am,(d32,sp) -> mov am,(d16,sp)
3183 mov dm,(d32,sp) -> mov dm,(d32,sp)
3184 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3185 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3191 /* sp-based offsets are zero-extended. */
3192 if (code
>= 0x90 && code
<= 0x93
3196 /* Note that we've changed the relocation contents, etc. */
3197 elf_section_data (sec
)->relocs
= internal_relocs
;
3198 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3199 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3201 /* Fix the opcode. */
3202 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3203 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3205 /* Fix the relocation's type. */
3206 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3207 (ELF32_R_TYPE (irel
->r_info
)
3208 == (int) R_MN10300_GOTOFF32
)
3209 ? R_MN10300_GOTOFF16
3210 : (ELF32_R_TYPE (irel
->r_info
)
3211 == (int) R_MN10300_GOT32
)
3213 : (ELF32_R_TYPE (irel
->r_info
)
3214 == (int) R_MN10300_GOTPC32
)
3215 ? R_MN10300_GOTPC16
:
3218 /* Delete two bytes of data. */
3219 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3220 irel
->r_offset
+ 2, 2))
3223 /* That will change things, so, we should relax again.
3224 Note that this is not required, and it may be slow. */
3228 else if ((code
& 0xf0) < 0xf0)
3229 switch (code
& 0xfc)
3231 /* mov imm32,dn -> mov imm16,dn
3232 mov imm32,an -> mov imm16,an
3233 mov (abs32),dn -> mov (abs16),dn
3234 movbu (abs32),dn -> movbu (abs16),dn
3235 movhu (abs32),dn -> movhu (abs16),dn */
3241 /* Not safe if the high bit is on as relaxing may
3242 move the value out of high mem and thus not fit
3243 in a signed 16bit value. */
3245 && (value
& 0x8000))
3248 /* mov imm16, an zero-extends the immediate. */
3253 /* Note that we've changed the relocation contents, etc. */
3254 elf_section_data (sec
)->relocs
= internal_relocs
;
3255 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3256 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3258 if ((code
& 0xfc) == 0xcc)
3259 code
= 0x2c + (code
& 0x03);
3260 else if ((code
& 0xfc) == 0xdc)
3261 code
= 0x24 + (code
& 0x03);
3262 else if ((code
& 0xfc) == 0xa4)
3263 code
= 0x30 + (code
& 0x03);
3264 else if ((code
& 0xfc) == 0xa8)
3265 code
= 0x34 + (code
& 0x03);
3266 else if ((code
& 0xfc) == 0xac)
3267 code
= 0x38 + (code
& 0x03);
3271 /* Fix the opcode. */
3272 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 2);
3274 /* Fix the relocation's type. */
3275 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3276 (ELF32_R_TYPE (irel
->r_info
)
3277 == (int) R_MN10300_GOTOFF32
)
3278 ? R_MN10300_GOTOFF16
3279 : (ELF32_R_TYPE (irel
->r_info
)
3280 == (int) R_MN10300_GOT32
)
3282 : (ELF32_R_TYPE (irel
->r_info
)
3283 == (int) R_MN10300_GOTPC32
)
3284 ? R_MN10300_GOTPC16
:
3287 /* The opcode got shorter too, so we have to fix the
3288 addend and offset too! */
3289 irel
->r_offset
-= 1;
3291 /* Delete three bytes of data. */
3292 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3293 irel
->r_offset
+ 1, 3))
3296 /* That will change things, so, we should relax again.
3297 Note that this is not required, and it may be slow. */
3301 /* mov (abs32),an -> mov (abs16),an
3302 mov (d32,sp),an -> mov (d16,sp),an
3303 mov (d32,sp),dn -> mov (d16,sp),dn
3304 movbu (d32,sp),dn -> movbu (d16,sp),dn
3305 movhu (d32,sp),dn -> movhu (d16,sp),dn
3306 add imm32,dn -> add imm16,dn
3307 cmp imm32,dn -> cmp imm16,dn
3308 add imm32,an -> add imm16,an
3309 cmp imm32,an -> cmp imm16,an
3310 and imm32,dn -> and imm16,dn
3311 or imm32,dn -> or imm16,dn
3312 xor imm32,dn -> xor imm16,dn
3313 btst imm32,dn -> btst imm16,dn */
3329 /* cmp imm16, an zero-extends the immediate. */
3334 /* So do sp-based offsets. */
3335 if (code
>= 0xb0 && code
<= 0xb3
3339 /* Note that we've changed the relocation contents, etc. */
3340 elf_section_data (sec
)->relocs
= internal_relocs
;
3341 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3342 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3344 /* Fix the opcode. */
3345 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3346 bfd_put_8 (abfd
, code
, contents
+ irel
->r_offset
- 1);
3348 /* Fix the relocation's type. */
3349 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3350 (ELF32_R_TYPE (irel
->r_info
)
3351 == (int) R_MN10300_GOTOFF32
)
3352 ? R_MN10300_GOTOFF16
3353 : (ELF32_R_TYPE (irel
->r_info
)
3354 == (int) R_MN10300_GOT32
)
3356 : (ELF32_R_TYPE (irel
->r_info
)
3357 == (int) R_MN10300_GOTPC32
)
3358 ? R_MN10300_GOTPC16
:
3361 /* Delete two bytes of data. */
3362 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3363 irel
->r_offset
+ 2, 2))
3366 /* That will change things, so, we should relax again.
3367 Note that this is not required, and it may be slow. */
3371 else if (code
== 0xfe)
3373 /* add imm32,sp -> add imm16,sp */
3375 /* Note that we've changed the relocation contents, etc. */
3376 elf_section_data (sec
)->relocs
= internal_relocs
;
3377 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3378 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3380 /* Fix the opcode. */
3381 bfd_put_8 (abfd
, 0xfa, contents
+ irel
->r_offset
- 2);
3382 bfd_put_8 (abfd
, 0xfe, contents
+ irel
->r_offset
- 1);
3384 /* Fix the relocation's type. */
3385 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
3386 (ELF32_R_TYPE (irel
->r_info
)
3387 == (int) R_MN10300_GOT32
)
3389 : (ELF32_R_TYPE (irel
->r_info
)
3390 == (int) R_MN10300_GOTOFF32
)
3391 ? R_MN10300_GOTOFF16
3392 : (ELF32_R_TYPE (irel
->r_info
)
3393 == (int) R_MN10300_GOTPC32
)
3394 ? R_MN10300_GOTPC16
:
3397 /* Delete two bytes of data. */
3398 if (!mn10300_elf_relax_delete_bytes (abfd
, sec
,
3399 irel
->r_offset
+ 2, 2))
3402 /* That will change things, so, we should relax again.
3403 Note that this is not required, and it may be slow. */
3412 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3414 if (! link_info
->keep_memory
)
3418 /* Cache the symbols for elf_link_input_bfd. */
3419 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
3423 if (contents
!= NULL
3424 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
3426 if (! link_info
->keep_memory
)
3430 /* Cache the section contents for elf_link_input_bfd. */
3431 elf_section_data (sec
)->this_hdr
.contents
= contents
;
3435 if (internal_relocs
!= NULL
3436 && elf_section_data (sec
)->relocs
!= internal_relocs
)
3437 free (internal_relocs
);
3443 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3445 if (contents
!= NULL
3446 && elf_section_data (section
)->this_hdr
.contents
!= contents
)
3448 if (internal_relocs
!= NULL
3449 && elf_section_data (section
)->relocs
!= internal_relocs
)
3450 free (internal_relocs
);
3455 /* Compute the stack size and movm arguments for the function
3456 referred to by HASH at address ADDR in section with
3457 contents CONTENTS, store the information in the hash table. */
3459 compute_function_info (abfd
, hash
, addr
, contents
)
3461 struct elf32_mn10300_link_hash_entry
*hash
;
3463 unsigned char *contents
;
3465 unsigned char byte1
, byte2
;
3466 /* We only care about a very small subset of the possible prologue
3467 sequences here. Basically we look for:
3469 movm [d2,d3,a2,a3],sp (optional)
3470 add <size>,sp (optional, and only for sizes which fit in an unsigned
3473 If we find anything else, we quit. */
3475 /* Look for movm [regs],sp */
3476 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
3477 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
3481 hash
->movm_args
= byte2
;
3483 byte1
= bfd_get_8 (abfd
, contents
+ addr
);
3484 byte2
= bfd_get_8 (abfd
, contents
+ addr
+ 1);
3487 /* Now figure out how much stack space will be allocated by the movm
3488 instruction. We need this kept separate from the funtion's normal
3490 if (hash
->movm_args
)
3493 if (hash
->movm_args
& 0x80)
3494 hash
->movm_stack_size
+= 4;
3497 if (hash
->movm_args
& 0x40)
3498 hash
->movm_stack_size
+= 4;
3501 if (hash
->movm_args
& 0x20)
3502 hash
->movm_stack_size
+= 4;
3505 if (hash
->movm_args
& 0x10)
3506 hash
->movm_stack_size
+= 4;
3508 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
3509 if (hash
->movm_args
& 0x08)
3510 hash
->movm_stack_size
+= 8 * 4;
3512 if (bfd_get_mach (abfd
) == bfd_mach_am33
3513 || bfd_get_mach (abfd
) == bfd_mach_am33_2
)
3515 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
3516 if (hash
->movm_args
& 0x1)
3517 hash
->movm_stack_size
+= 6 * 4;
3519 /* exreg1 space. e4, e5, e6, e7 */
3520 if (hash
->movm_args
& 0x2)
3521 hash
->movm_stack_size
+= 4 * 4;
3523 /* exreg0 space. e2, e3 */
3524 if (hash
->movm_args
& 0x4)
3525 hash
->movm_stack_size
+= 2 * 4;
3529 /* Now look for the two stack adjustment variants. */
3530 if (byte1
== 0xf8 && byte2
== 0xfe)
3532 int temp
= bfd_get_8 (abfd
, contents
+ addr
+ 2);
3533 temp
= ((temp
& 0xff) ^ (~0x7f)) + 0x80;
3535 hash
->stack_size
= -temp
;
3537 else if (byte1
== 0xfa && byte2
== 0xfe)
3539 int temp
= bfd_get_16 (abfd
, contents
+ addr
+ 2);
3540 temp
= ((temp
& 0xffff) ^ (~0x7fff)) + 0x8000;
3544 hash
->stack_size
= temp
;
3547 /* If the total stack to be allocated by the call instruction is more
3548 than 255 bytes, then we can't remove the stack adjustment by using
3549 "call" (we might still be able to remove the "movm" instruction. */
3550 if (hash
->stack_size
+ hash
->movm_stack_size
> 255)
3551 hash
->stack_size
= 0;
3556 /* Delete some bytes from a section while relaxing. */
3559 mn10300_elf_relax_delete_bytes (abfd
, sec
, addr
, count
)
3565 Elf_Internal_Shdr
*symtab_hdr
;
3566 unsigned int sec_shndx
;
3568 Elf_Internal_Rela
*irel
, *irelend
;
3569 Elf_Internal_Rela
*irelalign
;
3571 Elf_Internal_Sym
*isym
, *isymend
;
3572 struct elf_link_hash_entry
**sym_hashes
;
3573 struct elf_link_hash_entry
**end_hashes
;
3574 unsigned int symcount
;
3576 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3578 contents
= elf_section_data (sec
)->this_hdr
.contents
;
3580 /* The deletion must stop at the next ALIGN reloc for an aligment
3581 power larger than the number of bytes we are deleting. */
3584 toaddr
= sec
->_cooked_size
;
3586 irel
= elf_section_data (sec
)->relocs
;
3587 irelend
= irel
+ sec
->reloc_count
;
3589 /* Actually delete the bytes. */
3590 memmove (contents
+ addr
, contents
+ addr
+ count
,
3591 (size_t) (toaddr
- addr
- count
));
3592 sec
->_cooked_size
-= count
;
3594 /* Adjust all the relocs. */
3595 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
3597 /* Get the new reloc address. */
3598 if ((irel
->r_offset
> addr
3599 && irel
->r_offset
< toaddr
))
3600 irel
->r_offset
-= count
;
3603 /* Adjust the local symbols defined in this section. */
3604 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3605 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3606 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
3608 if (isym
->st_shndx
== sec_shndx
3609 && isym
->st_value
> addr
3610 && isym
->st_value
< toaddr
)
3611 isym
->st_value
-= count
;
3614 /* Now adjust the global symbols defined in this section. */
3615 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
3616 - symtab_hdr
->sh_info
);
3617 sym_hashes
= elf_sym_hashes (abfd
);
3618 end_hashes
= sym_hashes
+ symcount
;
3619 for (; sym_hashes
< end_hashes
; sym_hashes
++)
3621 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
3622 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3623 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3624 && sym_hash
->root
.u
.def
.section
== sec
3625 && sym_hash
->root
.u
.def
.value
> addr
3626 && sym_hash
->root
.u
.def
.value
< toaddr
)
3628 sym_hash
->root
.u
.def
.value
-= count
;
3635 /* Return TRUE if a symbol exists at the given address, else return
3638 mn10300_elf_symbol_address_p (abfd
, sec
, isym
, addr
)
3641 Elf_Internal_Sym
*isym
;
3644 Elf_Internal_Shdr
*symtab_hdr
;
3645 unsigned int sec_shndx
;
3646 Elf_Internal_Sym
*isymend
;
3647 struct elf_link_hash_entry
**sym_hashes
;
3648 struct elf_link_hash_entry
**end_hashes
;
3649 unsigned int symcount
;
3651 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
3653 /* Examine all the symbols. */
3654 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
3655 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
3657 if (isym
->st_shndx
== sec_shndx
3658 && isym
->st_value
== addr
)
3662 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
3663 - symtab_hdr
->sh_info
);
3664 sym_hashes
= elf_sym_hashes (abfd
);
3665 end_hashes
= sym_hashes
+ symcount
;
3666 for (; sym_hashes
< end_hashes
; sym_hashes
++)
3668 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
3669 if ((sym_hash
->root
.type
== bfd_link_hash_defined
3670 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
3671 && sym_hash
->root
.u
.def
.section
== sec
3672 && sym_hash
->root
.u
.def
.value
== addr
)
3679 /* This is a version of bfd_generic_get_relocated_section_contents
3680 which uses mn10300_elf_relocate_section. */
3683 mn10300_elf_get_relocated_section_contents (output_bfd
, link_info
, link_order
,
3684 data
, relocatable
, symbols
)
3686 struct bfd_link_info
*link_info
;
3687 struct bfd_link_order
*link_order
;
3689 bfd_boolean relocatable
;
3692 Elf_Internal_Shdr
*symtab_hdr
;
3693 asection
*input_section
= link_order
->u
.indirect
.section
;
3694 bfd
*input_bfd
= input_section
->owner
;
3695 asection
**sections
= NULL
;
3696 Elf_Internal_Rela
*internal_relocs
= NULL
;
3697 Elf_Internal_Sym
*isymbuf
= NULL
;
3699 /* We only need to handle the case of relaxing, or of having a
3700 particular set of section contents, specially. */
3702 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
3703 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
3708 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
3710 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
3711 (size_t) input_section
->_raw_size
);
3713 if ((input_section
->flags
& SEC_RELOC
) != 0
3714 && input_section
->reloc_count
> 0)
3717 Elf_Internal_Sym
*isym
, *isymend
;
3720 internal_relocs
= (_bfd_elf_link_read_relocs
3721 (input_bfd
, input_section
, (PTR
) NULL
,
3722 (Elf_Internal_Rela
*) NULL
, FALSE
));
3723 if (internal_relocs
== NULL
)
3726 if (symtab_hdr
->sh_info
!= 0)
3728 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
3729 if (isymbuf
== NULL
)
3730 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
3731 symtab_hdr
->sh_info
, 0,
3733 if (isymbuf
== NULL
)
3737 amt
= symtab_hdr
->sh_info
;
3738 amt
*= sizeof (asection
*);
3739 sections
= (asection
**) bfd_malloc (amt
);
3740 if (sections
== NULL
&& amt
!= 0)
3743 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
3744 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
3748 if (isym
->st_shndx
== SHN_UNDEF
)
3749 isec
= bfd_und_section_ptr
;
3750 else if (isym
->st_shndx
== SHN_ABS
)
3751 isec
= bfd_abs_section_ptr
;
3752 else if (isym
->st_shndx
== SHN_COMMON
)
3753 isec
= bfd_com_section_ptr
;
3755 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
3760 if (! mn10300_elf_relocate_section (output_bfd
, link_info
, input_bfd
,
3761 input_section
, data
, internal_relocs
,
3765 if (sections
!= NULL
)
3767 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3769 if (internal_relocs
!= elf_section_data (input_section
)->relocs
)
3770 free (internal_relocs
);
3776 if (sections
!= NULL
)
3778 if (isymbuf
!= NULL
&& symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
3780 if (internal_relocs
!= NULL
3781 && internal_relocs
!= elf_section_data (input_section
)->relocs
)
3782 free (internal_relocs
);
3786 /* Assorted hash table functions. */
3788 /* Initialize an entry in the link hash table. */
3790 /* Create an entry in an MN10300 ELF linker hash table. */
3792 static struct bfd_hash_entry
*
3793 elf32_mn10300_link_hash_newfunc (entry
, table
, string
)
3794 struct bfd_hash_entry
*entry
;
3795 struct bfd_hash_table
*table
;
3798 struct elf32_mn10300_link_hash_entry
*ret
=
3799 (struct elf32_mn10300_link_hash_entry
*) entry
;
3801 /* Allocate the structure if it has not already been allocated by a
3803 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
3804 ret
= ((struct elf32_mn10300_link_hash_entry
*)
3805 bfd_hash_allocate (table
,
3806 sizeof (struct elf32_mn10300_link_hash_entry
)));
3807 if (ret
== (struct elf32_mn10300_link_hash_entry
*) NULL
)
3808 return (struct bfd_hash_entry
*) ret
;
3810 /* Call the allocation method of the superclass. */
3811 ret
= ((struct elf32_mn10300_link_hash_entry
*)
3812 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry
*) ret
,
3814 if (ret
!= (struct elf32_mn10300_link_hash_entry
*) NULL
)
3816 ret
->direct_calls
= 0;
3817 ret
->stack_size
= 0;
3819 ret
->movm_stack_size
= 0;
3820 ret
->pcrel_relocs_copied
= NULL
;
3824 return (struct bfd_hash_entry
*) ret
;
3827 /* Create an mn10300 ELF linker hash table. */
3829 static struct bfd_link_hash_table
*
3830 elf32_mn10300_link_hash_table_create (abfd
)
3833 struct elf32_mn10300_link_hash_table
*ret
;
3834 bfd_size_type amt
= sizeof (struct elf32_mn10300_link_hash_table
);
3836 ret
= (struct elf32_mn10300_link_hash_table
*) bfd_malloc (amt
);
3837 if (ret
== (struct elf32_mn10300_link_hash_table
*) NULL
)
3840 if (! _bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
3841 elf32_mn10300_link_hash_newfunc
))
3848 amt
= sizeof (struct elf_link_hash_table
);
3849 ret
->static_hash_table
3850 = (struct elf32_mn10300_link_hash_table
*) bfd_malloc (amt
);
3851 if (ret
->static_hash_table
== NULL
)
3857 if (! _bfd_elf_link_hash_table_init (&ret
->static_hash_table
->root
, abfd
,
3858 elf32_mn10300_link_hash_newfunc
))
3860 free (ret
->static_hash_table
);
3864 return &ret
->root
.root
;
3867 /* Free an mn10300 ELF linker hash table. */
3870 elf32_mn10300_link_hash_table_free (hash
)
3871 struct bfd_link_hash_table
*hash
;
3873 struct elf32_mn10300_link_hash_table
*ret
3874 = (struct elf32_mn10300_link_hash_table
*) hash
;
3876 _bfd_generic_link_hash_table_free
3877 ((struct bfd_link_hash_table
*) ret
->static_hash_table
);
3878 _bfd_generic_link_hash_table_free
3879 ((struct bfd_link_hash_table
*) ret
);
3882 static unsigned long
3883 elf_mn10300_mach (flags
)
3886 switch (flags
& EF_MN10300_MACH
)
3888 case E_MN10300_MACH_MN10300
:
3890 return bfd_mach_mn10300
;
3892 case E_MN10300_MACH_AM33
:
3893 return bfd_mach_am33
;
3895 case E_MN10300_MACH_AM33_2
:
3896 return bfd_mach_am33_2
;
3900 /* The final processing done just before writing out a MN10300 ELF object
3901 file. This gets the MN10300 architecture right based on the machine
3905 _bfd_mn10300_elf_final_write_processing (abfd
, linker
)
3907 bfd_boolean linker ATTRIBUTE_UNUSED
;
3911 switch (bfd_get_mach (abfd
))
3914 case bfd_mach_mn10300
:
3915 val
= E_MN10300_MACH_MN10300
;
3919 val
= E_MN10300_MACH_AM33
;
3922 case bfd_mach_am33_2
:
3923 val
= E_MN10300_MACH_AM33_2
;
3927 elf_elfheader (abfd
)->e_flags
&= ~ (EF_MN10300_MACH
);
3928 elf_elfheader (abfd
)->e_flags
|= val
;
3932 _bfd_mn10300_elf_object_p (abfd
)
3935 bfd_default_set_arch_mach (abfd
, bfd_arch_mn10300
,
3936 elf_mn10300_mach (elf_elfheader (abfd
)->e_flags
));
3940 /* Merge backend specific data from an object file to the output
3941 object file when linking. */
3944 _bfd_mn10300_elf_merge_private_bfd_data (ibfd
, obfd
)
3948 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
3949 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
3952 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
3953 && bfd_get_mach (obfd
) < bfd_get_mach (ibfd
))
3955 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
3956 bfd_get_mach (ibfd
)))
3963 #define PLT0_ENTRY_SIZE 15
3964 #define PLT_ENTRY_SIZE 20
3965 #define PIC_PLT_ENTRY_SIZE 24
3967 static const bfd_byte elf_mn10300_plt0_entry
[PLT0_ENTRY_SIZE
] =
3969 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
3970 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
3971 0xf0, 0xf4, /* jmp (a0) */
3974 static const bfd_byte elf_mn10300_plt_entry
[PLT_ENTRY_SIZE
] =
3976 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
3977 0xf0, 0xf4, /* jmp (a0) */
3978 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3979 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
3982 static const bfd_byte elf_mn10300_pic_plt_entry
[PIC_PLT_ENTRY_SIZE
] =
3984 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
3985 0xf0, 0xf4, /* jmp (a0) */
3986 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3987 0xf8, 0x22, 8, /* mov (8,a2),a0 */
3988 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
3989 0xf0, 0xf4, /* jmp (a0) */
3992 /* Return size of the first PLT entry. */
3993 #define elf_mn10300_sizeof_plt0(info) \
3994 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
3996 /* Return size of a PLT entry. */
3997 #define elf_mn10300_sizeof_plt(info) \
3998 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4000 /* Return offset of the PLT0 address in an absolute PLT entry. */
4001 #define elf_mn10300_plt_plt0_offset(info) 16
4003 /* Return offset of the linker in PLT0 entry. */
4004 #define elf_mn10300_plt0_linker_offset(info) 2
4006 /* Return offset of the GOT id in PLT0 entry. */
4007 #define elf_mn10300_plt0_gotid_offset(info) 9
4009 /* Return offset of the tempoline in PLT entry */
4010 #define elf_mn10300_plt_temp_offset(info) 8
4012 /* Return offset of the symbol in PLT entry. */
4013 #define elf_mn10300_plt_symbol_offset(info) 2
4015 /* Return offset of the relocation in PLT entry. */
4016 #define elf_mn10300_plt_reloc_offset(info) 11
4018 /* The name of the dynamic interpreter. This is put in the .interp
4021 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4023 /* Create dynamic sections when linking against a dynamic object. */
4026 _bfd_mn10300_elf_create_dynamic_sections (abfd
, info
)
4028 struct bfd_link_info
*info
;
4032 const struct elf_backend_data
* bed
= get_elf_backend_data (abfd
);
4035 switch (bed
->s
->arch_size
)
4046 bfd_set_error (bfd_error_bad_value
);
4050 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4051 .rel[a].bss sections. */
4053 flags
= (SEC_ALLOC
| SEC_LOAD
| SEC_HAS_CONTENTS
| SEC_IN_MEMORY
4054 | SEC_LINKER_CREATED
);
4056 s
= bfd_make_section (abfd
,
4057 bed
->default_use_rela_p
? ".rela.plt" : ".rel.plt");
4059 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
4060 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4063 if (! _bfd_mn10300_elf_create_got_section (abfd
, info
))
4067 const char * secname
;
4072 for (sec
= abfd
->sections
; sec
; sec
= sec
->next
)
4074 secflags
= bfd_get_section_flags (abfd
, sec
);
4075 if ((secflags
& (SEC_DATA
| SEC_LINKER_CREATED
))
4076 || ((secflags
& SEC_HAS_CONTENTS
) != SEC_HAS_CONTENTS
))
4079 secname
= bfd_get_section_name (abfd
, sec
);
4080 relname
= (char *) bfd_malloc (strlen (secname
) + 6);
4081 strcpy (relname
, ".rela");
4082 strcat (relname
, secname
);
4084 s
= bfd_make_section (abfd
, relname
);
4086 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
4087 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4092 if (bed
->want_dynbss
)
4094 /* The .dynbss section is a place to put symbols which are defined
4095 by dynamic objects, are referenced by regular objects, and are
4096 not functions. We must allocate space for them in the process
4097 image and use a R_*_COPY reloc to tell the dynamic linker to
4098 initialize them at run time. The linker script puts the .dynbss
4099 section into the .bss section of the final image. */
4100 s
= bfd_make_section (abfd
, ".dynbss");
4102 || ! bfd_set_section_flags (abfd
, s
, SEC_ALLOC
))
4105 /* The .rel[a].bss section holds copy relocs. This section is not
4106 normally needed. We need to create it here, though, so that the
4107 linker will map it to an output section. We can't just create it
4108 only if we need it, because we will not know whether we need it
4109 until we have seen all the input files, and the first time the
4110 main linker code calls BFD after examining all the input files
4111 (size_dynamic_sections) the input sections have already been
4112 mapped to the output sections. If the section turns out not to
4113 be needed, we can discard it later. We will never need this
4114 section when generating a shared object, since they do not use
4118 s
= bfd_make_section (abfd
,
4119 (bed
->default_use_rela_p
4120 ? ".rela.bss" : ".rel.bss"));
4122 || ! bfd_set_section_flags (abfd
, s
, flags
| SEC_READONLY
)
4123 || ! bfd_set_section_alignment (abfd
, s
, ptralign
))
4131 /* Adjust a symbol defined by a dynamic object and referenced by a
4132 regular object. The current definition is in some section of the
4133 dynamic object, but we're not including those sections. We have to
4134 change the definition to something the rest of the link can
4138 _bfd_mn10300_elf_adjust_dynamic_symbol (info
, h
)
4139 struct bfd_link_info
* info
;
4140 struct elf_link_hash_entry
* h
;
4144 unsigned int power_of_two
;
4146 dynobj
= elf_hash_table (info
)->dynobj
;
4148 /* Make sure we know what is going on here. */
4149 BFD_ASSERT (dynobj
!= NULL
4150 && ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
)
4151 || h
->weakdef
!= NULL
4152 || ((h
->elf_link_hash_flags
4153 & ELF_LINK_HASH_DEF_DYNAMIC
) != 0
4154 && (h
->elf_link_hash_flags
4155 & ELF_LINK_HASH_REF_REGULAR
) != 0
4156 && (h
->elf_link_hash_flags
4157 & ELF_LINK_HASH_DEF_REGULAR
) == 0)));
4159 /* If this is a function, put it in the procedure linkage table. We
4160 will fill in the contents of the procedure linkage table later,
4161 when we know the address of the .got section. */
4162 if (h
->type
== STT_FUNC
4163 || (h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0)
4166 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_DYNAMIC
) == 0
4167 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_REF_DYNAMIC
) == 0)
4169 /* This case can occur if we saw a PLT reloc in an input
4170 file, but the symbol was never referred to by a dynamic
4171 object. In such a case, we don't actually need to build
4172 a procedure linkage table, and we can just do a REL32
4174 BFD_ASSERT ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_PLT
) != 0);
4178 /* Make sure this symbol is output as a dynamic symbol. */
4179 if (h
->dynindx
== -1)
4181 if (! bfd_elf32_link_record_dynamic_symbol (info
, h
))
4185 s
= bfd_get_section_by_name (dynobj
, ".plt");
4186 BFD_ASSERT (s
!= NULL
);
4188 /* If this is the first .plt entry, make room for the special
4190 if (s
->_raw_size
== 0)
4191 s
->_raw_size
+= elf_mn10300_sizeof_plt0 (info
);
4193 /* If this symbol is not defined in a regular file, and we are
4194 not generating a shared library, then set the symbol to this
4195 location in the .plt. This is required to make function
4196 pointers compare as equal between the normal executable and
4197 the shared library. */
4199 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4201 h
->root
.u
.def
.section
= s
;
4202 h
->root
.u
.def
.value
= s
->_raw_size
;
4205 h
->plt
.offset
= s
->_raw_size
;
4207 /* Make room for this entry. */
4208 s
->_raw_size
+= elf_mn10300_sizeof_plt (info
);
4210 /* We also need to make an entry in the .got.plt section, which
4211 will be placed in the .got section by the linker script. */
4213 s
= bfd_get_section_by_name (dynobj
, ".got.plt");
4214 BFD_ASSERT (s
!= NULL
);
4217 /* We also need to make an entry in the .rela.plt section. */
4219 s
= bfd_get_section_by_name (dynobj
, ".rela.plt");
4220 BFD_ASSERT (s
!= NULL
);
4221 s
->_raw_size
+= sizeof (Elf32_External_Rela
);
4226 /* If this is a weak symbol, and there is a real definition, the
4227 processor independent code will have arranged for us to see the
4228 real definition first, and we can just use the same value. */
4229 if (h
->weakdef
!= NULL
)
4231 BFD_ASSERT (h
->weakdef
->root
.type
== bfd_link_hash_defined
4232 || h
->weakdef
->root
.type
== bfd_link_hash_defweak
);
4233 h
->root
.u
.def
.section
= h
->weakdef
->root
.u
.def
.section
;
4234 h
->root
.u
.def
.value
= h
->weakdef
->root
.u
.def
.value
;
4238 /* This is a reference to a symbol defined by a dynamic object which
4239 is not a function. */
4241 /* If we are creating a shared library, we must presume that the
4242 only references to the symbol are via the global offset table.
4243 For such cases we need not do anything here; the relocations will
4244 be handled correctly by relocate_section. */
4248 /* If there are no references to this symbol that do not use the
4249 GOT, we don't need to generate a copy reloc. */
4250 if ((h
->elf_link_hash_flags
& ELF_LINK_NON_GOT_REF
) == 0)
4253 /* We must allocate the symbol in our .dynbss section, which will
4254 become part of the .bss section of the executable. There will be
4255 an entry for this symbol in the .dynsym section. The dynamic
4256 object will contain position independent code, so all references
4257 from the dynamic object to this symbol will go through the global
4258 offset table. The dynamic linker will use the .dynsym entry to
4259 determine the address it must put in the global offset table, so
4260 both the dynamic object and the regular object will refer to the
4261 same memory location for the variable. */
4263 s
= bfd_get_section_by_name (dynobj
, ".dynbss");
4264 BFD_ASSERT (s
!= NULL
);
4266 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4267 copy the initial value out of the dynamic object and into the
4268 runtime process image. We need to remember the offset into the
4269 .rela.bss section we are going to use. */
4270 if ((h
->root
.u
.def
.section
->flags
& SEC_ALLOC
) != 0)
4274 srel
= bfd_get_section_by_name (dynobj
, ".rela.bss");
4275 BFD_ASSERT (srel
!= NULL
);
4276 srel
->_raw_size
+= sizeof (Elf32_External_Rela
);
4277 h
->elf_link_hash_flags
|= ELF_LINK_HASH_NEEDS_COPY
;
4280 /* We need to figure out the alignment required for this symbol. I
4281 have no idea how ELF linkers handle this. */
4282 power_of_two
= bfd_log2 (h
->size
);
4283 if (power_of_two
> 3)
4286 /* Apply the required alignment. */
4287 s
->_raw_size
= BFD_ALIGN (s
->_raw_size
,
4288 (bfd_size_type
) (1 << power_of_two
));
4289 if (power_of_two
> bfd_get_section_alignment (dynobj
, s
))
4291 if (! bfd_set_section_alignment (dynobj
, s
, power_of_two
))
4295 /* Define the symbol as being at this point in the section. */
4296 h
->root
.u
.def
.section
= s
;
4297 h
->root
.u
.def
.value
= s
->_raw_size
;
4299 /* Increment the section size to make room for the symbol. */
4300 s
->_raw_size
+= h
->size
;
4305 /* This function is called via elf32_mn10300_link_hash_traverse if we are
4306 creating a shared object with -Bsymbolic. It discards the space
4307 allocated to copy PC relative relocs against symbols which are
4308 defined in regular objects. We allocated space for them in the
4309 check_relocs routine, but we won't fill them in in the
4310 relocate_section routine. */
4313 _bfd_mn10300_elf_discard_copies (h
, info
)
4314 struct elf32_mn10300_link_hash_entry
*h
;
4315 struct bfd_link_info
*info
;
4317 struct elf_mn10300_pcrel_relocs_copied
*s
;
4319 /* If a symbol has been forced local or we have found a regular
4320 definition for the symbolic link case, then we won't be needing
4322 if ((h
->root
.elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) != 0
4323 && ((h
->root
.elf_link_hash_flags
& ELF_LINK_FORCED_LOCAL
) != 0
4326 for (s
= h
->pcrel_relocs_copied
; s
!= NULL
; s
= s
->next
)
4327 s
->section
->_raw_size
-= s
->count
* sizeof (Elf32_External_Rel
);
4333 /* Set the sizes of the dynamic sections. */
4336 _bfd_mn10300_elf_size_dynamic_sections (output_bfd
, info
)
4338 struct bfd_link_info
* info
;
4344 bfd_boolean reltext
;
4346 dynobj
= elf_hash_table (info
)->dynobj
;
4347 BFD_ASSERT (dynobj
!= NULL
);
4349 if (elf_hash_table (info
)->dynamic_sections_created
)
4351 /* Set the contents of the .interp section to the interpreter. */
4354 s
= bfd_get_section_by_name (dynobj
, ".interp");
4355 BFD_ASSERT (s
!= NULL
);
4356 s
->_raw_size
= sizeof ELF_DYNAMIC_INTERPRETER
;
4357 s
->contents
= (unsigned char *) ELF_DYNAMIC_INTERPRETER
;
4362 /* We may have created entries in the .rela.got section.
4363 However, if we are not creating the dynamic sections, we will
4364 not actually use these entries. Reset the size of .rela.got,
4365 which will cause it to get stripped from the output file
4367 s
= bfd_get_section_by_name (dynobj
, ".rela.got");
4372 /* If this is a -Bsymbolic shared link, then we need to discard all
4373 PC relative relocs against symbols defined in a regular object.
4374 We allocated space for them in the check_relocs routine, but we
4375 will not fill them in in the relocate_section routine. */
4376 if (info
->shared
&& info
->symbolic
)
4377 elf32_mn10300_link_hash_traverse (elf32_mn10300_hash_table (info
),
4378 _bfd_mn10300_elf_discard_copies
,
4381 /* The check_relocs and adjust_dynamic_symbol entry points have
4382 determined the sizes of the various dynamic sections. Allocate
4387 for (s
= dynobj
->sections
; s
!= NULL
; s
= s
->next
)
4392 if ((s
->flags
& SEC_LINKER_CREATED
) == 0)
4395 /* It's OK to base decisions on the section name, because none
4396 of the dynobj section names depend upon the input files. */
4397 name
= bfd_get_section_name (dynobj
, s
);
4401 if (strcmp (name
, ".plt") == 0)
4403 if (s
->_raw_size
== 0)
4404 /* Strip this section if we don't need it; see the
4408 /* Remember whether there is a PLT. */
4411 else if (strncmp (name
, ".rela", 5) == 0)
4413 if (s
->_raw_size
== 0)
4415 /* If we don't need this section, strip it from the
4416 output file. This is mostly to handle .rela.bss and
4417 .rela.plt. We must create both sections in
4418 create_dynamic_sections, because they must be created
4419 before the linker maps input sections to output
4420 sections. The linker does that before
4421 adjust_dynamic_symbol is called, and it is that
4422 function which decides whether anything needs to go
4423 into these sections. */
4430 /* Remember whether there are any reloc sections other
4432 if (strcmp (name
, ".rela.plt") != 0)
4434 const char * outname
;
4438 /* If this relocation section applies to a read only
4439 section, then we probably need a DT_TEXTREL
4440 entry. The entries in the .rela.plt section
4441 really apply to the .got section, which we
4442 created ourselves and so know is not readonly. */
4443 outname
= bfd_get_section_name (output_bfd
,
4445 target
= bfd_get_section_by_name (output_bfd
, outname
+ 5);
4447 && (target
->flags
& SEC_READONLY
) != 0
4448 && (target
->flags
& SEC_ALLOC
) != 0)
4452 /* We use the reloc_count field as a counter if we need
4453 to copy relocs into the output file. */
4457 else if (strncmp (name
, ".got", 4) != 0)
4458 /* It's not one of our sections, so don't allocate space. */
4463 _bfd_strip_section_from_output (info
, s
);
4467 /* Allocate memory for the section contents. We use bfd_zalloc
4468 here in case unused entries are not reclaimed before the
4469 section's contents are written out. This should not happen,
4470 but this way if it does, we get a R_MN10300_NONE reloc
4471 instead of garbage. */
4472 s
->contents
= (bfd_byte
*) bfd_zalloc (dynobj
, s
->_raw_size
);
4473 if (s
->contents
== NULL
&& s
->_raw_size
!= 0)
4477 if (elf_hash_table (info
)->dynamic_sections_created
)
4479 /* Add some entries to the .dynamic section. We fill in the
4480 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4481 but we must add the entries now so that we get the correct
4482 size for the .dynamic section. The DT_DEBUG entry is filled
4483 in by the dynamic linker and used by the debugger. */
4486 if (! bfd_elf32_add_dynamic_entry (info
, DT_DEBUG
, 0))
4492 if (! bfd_elf32_add_dynamic_entry (info
, DT_PLTGOT
, 0)
4493 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTRELSZ
, 0)
4494 || ! bfd_elf32_add_dynamic_entry (info
, DT_PLTREL
, DT_RELA
)
4495 || ! bfd_elf32_add_dynamic_entry (info
, DT_JMPREL
, 0))
4501 if (! bfd_elf32_add_dynamic_entry (info
, DT_RELA
, 0)
4502 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELASZ
, 0)
4503 || ! bfd_elf32_add_dynamic_entry (info
, DT_RELAENT
,
4504 sizeof (Elf32_External_Rela
)))
4510 if (! bfd_elf32_add_dynamic_entry (info
, DT_TEXTREL
, 0))
4518 /* Finish up dynamic symbol handling. We set the contents of various
4519 dynamic sections here. */
4522 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd
, info
, h
, sym
)
4524 struct bfd_link_info
* info
;
4525 struct elf_link_hash_entry
* h
;
4526 Elf_Internal_Sym
* sym
;
4530 dynobj
= elf_hash_table (info
)->dynobj
;
4532 if (h
->plt
.offset
!= (bfd_vma
) -1)
4539 Elf_Internal_Rela rel
;
4541 /* This symbol has an entry in the procedure linkage table. Set
4544 BFD_ASSERT (h
->dynindx
!= -1);
4546 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4547 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4548 srel
= bfd_get_section_by_name (dynobj
, ".rela.plt");
4549 BFD_ASSERT (splt
!= NULL
&& sgot
!= NULL
&& srel
!= NULL
);
4551 /* Get the index in the procedure linkage table which
4552 corresponds to this symbol. This is the index of this symbol
4553 in all the symbols for which we are making plt entries. The
4554 first entry in the procedure linkage table is reserved. */
4555 plt_index
= ((h
->plt
.offset
- elf_mn10300_sizeof_plt0 (info
))
4556 / elf_mn10300_sizeof_plt (info
));
4558 /* Get the offset into the .got table of the entry that
4559 corresponds to this function. Each .got entry is 4 bytes.
4560 The first three are reserved. */
4561 got_offset
= (plt_index
+ 3) * 4;
4563 /* Fill in the entry in the procedure linkage table. */
4566 memcpy (splt
->contents
+ h
->plt
.offset
, elf_mn10300_plt_entry
,
4567 elf_mn10300_sizeof_plt (info
));
4568 bfd_put_32 (output_bfd
,
4569 (sgot
->output_section
->vma
4570 + sgot
->output_offset
4572 (splt
->contents
+ h
->plt
.offset
4573 + elf_mn10300_plt_symbol_offset (info
)));
4575 bfd_put_32 (output_bfd
,
4576 (1 - h
->plt
.offset
- elf_mn10300_plt_plt0_offset (info
)),
4577 (splt
->contents
+ h
->plt
.offset
4578 + elf_mn10300_plt_plt0_offset (info
)));
4582 memcpy (splt
->contents
+ h
->plt
.offset
, elf_mn10300_pic_plt_entry
,
4583 elf_mn10300_sizeof_plt (info
));
4585 bfd_put_32 (output_bfd
, got_offset
,
4586 (splt
->contents
+ h
->plt
.offset
4587 + elf_mn10300_plt_symbol_offset (info
)));
4590 bfd_put_32 (output_bfd
, plt_index
* sizeof (Elf32_External_Rela
),
4591 (splt
->contents
+ h
->plt
.offset
4592 + elf_mn10300_plt_reloc_offset (info
)));
4594 /* Fill in the entry in the global offset table. */
4595 bfd_put_32 (output_bfd
,
4596 (splt
->output_section
->vma
4597 + splt
->output_offset
4599 + elf_mn10300_plt_temp_offset (info
)),
4600 sgot
->contents
+ got_offset
);
4602 /* Fill in the entry in the .rela.plt section. */
4603 rel
.r_offset
= (sgot
->output_section
->vma
4604 + sgot
->output_offset
4606 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_JMP_SLOT
);
4608 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4609 (bfd_byte
*) ((Elf32_External_Rela
*) srel
->contents
4612 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
) == 0)
4613 /* Mark the symbol as undefined, rather than as defined in
4614 the .plt section. Leave the value alone. */
4615 sym
->st_shndx
= SHN_UNDEF
;
4618 if (h
->got
.offset
!= (bfd_vma
) -1)
4622 Elf_Internal_Rela rel
;
4624 /* This symbol has an entry in the global offset table. Set it up. */
4626 sgot
= bfd_get_section_by_name (dynobj
, ".got");
4627 srel
= bfd_get_section_by_name (dynobj
, ".rela.got");
4628 BFD_ASSERT (sgot
!= NULL
&& srel
!= NULL
);
4630 rel
.r_offset
= (sgot
->output_section
->vma
4631 + sgot
->output_offset
4632 + (h
->got
.offset
&~ 1));
4634 /* If this is a -Bsymbolic link, and the symbol is defined
4635 locally, we just want to emit a RELATIVE reloc. Likewise if
4636 the symbol was forced to be local because of a version file.
4637 The entry in the global offset table will already have been
4638 initialized in the relocate_section function. */
4640 && (info
->symbolic
|| h
->dynindx
== -1)
4641 && (h
->elf_link_hash_flags
& ELF_LINK_HASH_DEF_REGULAR
))
4643 rel
.r_info
= ELF32_R_INFO (0, R_MN10300_RELATIVE
);
4644 rel
.r_addend
= (h
->root
.u
.def
.value
4645 + h
->root
.u
.def
.section
->output_section
->vma
4646 + h
->root
.u
.def
.section
->output_offset
);
4650 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ h
->got
.offset
);
4651 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_GLOB_DAT
);
4655 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4656 (bfd_byte
*) ((Elf32_External_Rela
*) srel
->contents
4657 + srel
->reloc_count
));
4658 ++ srel
->reloc_count
;
4661 if ((h
->elf_link_hash_flags
& ELF_LINK_HASH_NEEDS_COPY
) != 0)
4664 Elf_Internal_Rela rel
;
4666 /* This symbol needs a copy reloc. Set it up. */
4667 BFD_ASSERT (h
->dynindx
!= -1
4668 && (h
->root
.type
== bfd_link_hash_defined
4669 || h
->root
.type
== bfd_link_hash_defweak
));
4671 s
= bfd_get_section_by_name (h
->root
.u
.def
.section
->owner
,
4673 BFD_ASSERT (s
!= NULL
);
4675 rel
.r_offset
= (h
->root
.u
.def
.value
4676 + h
->root
.u
.def
.section
->output_section
->vma
4677 + h
->root
.u
.def
.section
->output_offset
);
4678 rel
.r_info
= ELF32_R_INFO (h
->dynindx
, R_MN10300_COPY
);
4680 bfd_elf32_swap_reloca_out (output_bfd
, &rel
,
4681 (bfd_byte
*) ((Elf32_External_Rela
*) s
->contents
4686 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4687 if (strcmp (h
->root
.root
.string
, "_DYNAMIC") == 0
4688 || strcmp (h
->root
.root
.string
, "_GLOBAL_OFFSET_TABLE_") == 0)
4689 sym
->st_shndx
= SHN_ABS
;
4694 /* Finish up the dynamic sections. */
4697 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd
, info
)
4699 struct bfd_link_info
* info
;
4705 dynobj
= elf_hash_table (info
)->dynobj
;
4707 sgot
= bfd_get_section_by_name (dynobj
, ".got.plt");
4708 BFD_ASSERT (sgot
!= NULL
);
4709 sdyn
= bfd_get_section_by_name (dynobj
, ".dynamic");
4711 if (elf_hash_table (info
)->dynamic_sections_created
)
4714 Elf32_External_Dyn
* dyncon
;
4715 Elf32_External_Dyn
* dynconend
;
4717 BFD_ASSERT (sdyn
!= NULL
);
4719 dyncon
= (Elf32_External_Dyn
*) sdyn
->contents
;
4720 dynconend
= (Elf32_External_Dyn
*) (sdyn
->contents
+ sdyn
->_raw_size
);
4722 for (; dyncon
< dynconend
; dyncon
++)
4724 Elf_Internal_Dyn dyn
;
4728 bfd_elf32_swap_dyn_in (dynobj
, dyncon
, &dyn
);
4742 s
= bfd_get_section_by_name (output_bfd
, name
);
4743 BFD_ASSERT (s
!= NULL
);
4744 dyn
.d_un
.d_ptr
= s
->vma
;
4745 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4749 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4750 BFD_ASSERT (s
!= NULL
);
4751 if (s
->_cooked_size
!= 0)
4752 dyn
.d_un
.d_val
= s
->_cooked_size
;
4754 dyn
.d_un
.d_val
= s
->_raw_size
;
4755 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4759 /* My reading of the SVR4 ABI indicates that the
4760 procedure linkage table relocs (DT_JMPREL) should be
4761 included in the overall relocs (DT_RELA). This is
4762 what Solaris does. However, UnixWare can not handle
4763 that case. Therefore, we override the DT_RELASZ entry
4764 here to make it not include the JMPREL relocs. Since
4765 the linker script arranges for .rela.plt to follow all
4766 other relocation sections, we don't have to worry
4767 about changing the DT_RELA entry. */
4768 s
= bfd_get_section_by_name (output_bfd
, ".rela.plt");
4771 if (s
->_cooked_size
!= 0)
4772 dyn
.d_un
.d_val
-= s
->_cooked_size
;
4774 dyn
.d_un
.d_val
-= s
->_raw_size
;
4776 bfd_elf32_swap_dyn_out (output_bfd
, &dyn
, dyncon
);
4781 /* Fill in the first entry in the procedure linkage table. */
4782 splt
= bfd_get_section_by_name (dynobj
, ".plt");
4783 if (splt
&& splt
->_raw_size
> 0)
4787 memcpy (splt
->contents
, elf_mn10300_pic_plt_entry
,
4788 elf_mn10300_sizeof_plt (info
));
4792 memcpy (splt
->contents
, elf_mn10300_plt0_entry
, PLT0_ENTRY_SIZE
);
4793 bfd_put_32 (output_bfd
,
4794 sgot
->output_section
->vma
+ sgot
->output_offset
+ 4,
4795 splt
->contents
+ elf_mn10300_plt0_gotid_offset (info
));
4796 bfd_put_32 (output_bfd
,
4797 sgot
->output_section
->vma
+ sgot
->output_offset
+ 8,
4798 splt
->contents
+ elf_mn10300_plt0_linker_offset (info
));
4801 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4802 really seem like the right value. */
4803 elf_section_data (splt
->output_section
)->this_hdr
.sh_entsize
= 4;
4807 /* Fill in the first three entries in the global offset table. */
4808 if (sgot
->_raw_size
> 0)
4811 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
);
4813 bfd_put_32 (output_bfd
,
4814 sdyn
->output_section
->vma
+ sdyn
->output_offset
,
4816 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 4);
4817 bfd_put_32 (output_bfd
, (bfd_vma
) 0, sgot
->contents
+ 8);
4820 elf_section_data (sgot
->output_section
)->this_hdr
.sh_entsize
= 4;
4826 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4827 #define TARGET_LITTLE_NAME "elf32-mn10300"
4828 #define ELF_ARCH bfd_arch_mn10300
4829 #define ELF_MACHINE_CODE EM_MN10300
4830 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4831 #define ELF_MAXPAGESIZE 0x1000
4834 #define elf_info_to_howto mn10300_info_to_howto
4835 #define elf_info_to_howto_rel 0
4836 #define elf_backend_can_gc_sections 1
4837 #define elf_backend_rela_normal 1
4838 #define elf_backend_check_relocs mn10300_elf_check_relocs
4839 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4840 #define elf_backend_relocate_section mn10300_elf_relocate_section
4841 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4842 #define bfd_elf32_bfd_get_relocated_section_contents \
4843 mn10300_elf_get_relocated_section_contents
4844 #define bfd_elf32_bfd_link_hash_table_create \
4845 elf32_mn10300_link_hash_table_create
4846 #define bfd_elf32_bfd_link_hash_table_free \
4847 elf32_mn10300_link_hash_table_free
4849 #ifndef elf_symbol_leading_char
4850 #define elf_symbol_leading_char '_'
4853 /* So we can set bits in e_flags. */
4854 #define elf_backend_final_write_processing \
4855 _bfd_mn10300_elf_final_write_processing
4856 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4858 #define bfd_elf32_bfd_merge_private_bfd_data \
4859 _bfd_mn10300_elf_merge_private_bfd_data
4861 #define elf_backend_can_gc_sections 1
4862 #define elf_backend_create_dynamic_sections \
4863 _bfd_mn10300_elf_create_dynamic_sections
4864 #define elf_backend_adjust_dynamic_symbol \
4865 _bfd_mn10300_elf_adjust_dynamic_symbol
4866 #define elf_backend_size_dynamic_sections \
4867 _bfd_mn10300_elf_size_dynamic_sections
4868 #define elf_backend_finish_dynamic_symbol \
4869 _bfd_mn10300_elf_finish_dynamic_symbol
4870 #define elf_backend_finish_dynamic_sections \
4871 _bfd_mn10300_elf_finish_dynamic_sections
4873 #define elf_backend_want_got_plt 1
4874 #define elf_backend_plt_readonly 1
4875 #define elf_backend_want_plt_sym 0
4876 #define elf_backend_got_header_size 12
4877 #define elf_backend_plt_header_size PLT0_ENTRY_SIZE
4879 #include "elf32-target.h"