* elf-m10300.c (_bfd_mn10300_elf_finish_dynamic_symbol): Use the
[binutils.git] / bfd / elf-m10300.c
blob81c5db026ee98cba1011bae3bc9156840217ca94
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
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 *,
31 asection *, int));
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
41 PARAMS ((flagword));
42 void _bfd_mn10300_elf_final_write_processing
43 PARAMS ((bfd *, bfd_boolean));
44 bfd_boolean _bfd_mn10300_elf_object_p
45 PARAMS ((bfd *));
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 struct elf32_mn10300_link_hash_entry {
56 /* The basic elf link hash table entry. */
57 struct elf_link_hash_entry root;
59 /* For function symbols, the number of times this function is
60 called directly (ie by name). */
61 unsigned int direct_calls;
63 /* For function symbols, the size of this function's stack
64 (if <= 255 bytes). We stuff this into "call" instructions
65 to this target when it's valid and profitable to do so.
67 This does not include stack allocated by movm! */
68 unsigned char stack_size;
70 /* For function symbols, arguments (if any) for movm instruction
71 in the prologue. We stuff this value into "call" instructions
72 to the target when it's valid and profitable to do so. */
73 unsigned char movm_args;
75 /* For function symbols, the amount of stack space that would be allocated
76 by the movm instruction. This is redundant with movm_args, but we
77 add it to the hash table to avoid computing it over and over. */
78 unsigned char movm_stack_size;
80 /* When set, convert all "call" instructions to this target into "calls"
81 instructions. */
82 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
84 /* Used to mark functions which have had redundant parts of their
85 prologue deleted. */
86 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
87 unsigned char flags;
90 /* We derive a hash table from the main elf linker hash table so
91 we can store state variables and a secondary hash table without
92 resorting to global variables. */
93 struct elf32_mn10300_link_hash_table {
94 /* The main hash table. */
95 struct elf_link_hash_table root;
97 /* A hash table for static functions. We could derive a new hash table
98 instead of using the full elf32_mn10300_link_hash_table if we wanted
99 to save some memory. */
100 struct elf32_mn10300_link_hash_table *static_hash_table;
102 /* Random linker state flags. */
103 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
104 char flags;
107 /* For MN10300 linker hash table. */
109 /* Get the MN10300 ELF linker hash table from a link_info structure. */
111 #define elf32_mn10300_hash_table(p) \
112 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
114 #define elf32_mn10300_link_hash_traverse(table, func, info) \
115 (elf_link_hash_traverse \
116 (&(table)->root, \
117 (bfd_boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
118 (info)))
120 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
121 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
122 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
123 PARAMS ((bfd *));
124 static void elf32_mn10300_link_hash_table_free
125 PARAMS ((struct bfd_link_hash_table *));
127 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
128 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
129 static void mn10300_info_to_howto
130 PARAMS ((bfd *, arelent *, Elf_Internal_Rela *));
131 static bfd_boolean mn10300_elf_check_relocs
132 PARAMS ((bfd *, struct bfd_link_info *, asection *,
133 const Elf_Internal_Rela *));
134 static asection *mn10300_elf_gc_mark_hook
135 PARAMS ((asection *, struct bfd_link_info *info, Elf_Internal_Rela *,
136 struct elf_link_hash_entry *, Elf_Internal_Sym *));
137 static bfd_boolean mn10300_elf_relax_delete_bytes
138 PARAMS ((bfd *, asection *, bfd_vma, int));
139 static bfd_boolean mn10300_elf_symbol_address_p
140 PARAMS ((bfd *, asection *, Elf_Internal_Sym *, bfd_vma));
141 static bfd_boolean elf32_mn10300_finish_hash_table_entry
142 PARAMS ((struct bfd_hash_entry *, PTR));
143 static void compute_function_info
144 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
145 bfd_vma, unsigned char *));
147 static bfd_boolean _bfd_mn10300_elf_create_got_section
148 PARAMS ((bfd *, struct bfd_link_info *));
149 static bfd_boolean _bfd_mn10300_elf_create_dynamic_sections
150 PARAMS ((bfd *, struct bfd_link_info *));
151 static bfd_boolean _bfd_mn10300_elf_adjust_dynamic_symbol
152 PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
153 static bfd_boolean _bfd_mn10300_elf_size_dynamic_sections
154 PARAMS ((bfd *, struct bfd_link_info *));
155 static bfd_boolean _bfd_mn10300_elf_finish_dynamic_symbol
156 PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *,
157 Elf_Internal_Sym *));
158 static bfd_boolean _bfd_mn10300_elf_finish_dynamic_sections
159 PARAMS ((bfd *, struct bfd_link_info *));
161 static reloc_howto_type elf_mn10300_howto_table[] = {
162 /* Dummy relocation. Does nothing. */
163 HOWTO (R_MN10300_NONE,
167 FALSE,
169 complain_overflow_bitfield,
170 bfd_elf_generic_reloc,
171 "R_MN10300_NONE",
172 FALSE,
175 FALSE),
176 /* Standard 32 bit reloc. */
177 HOWTO (R_MN10300_32,
181 FALSE,
183 complain_overflow_bitfield,
184 bfd_elf_generic_reloc,
185 "R_MN10300_32",
186 FALSE,
187 0xffffffff,
188 0xffffffff,
189 FALSE),
190 /* Standard 16 bit reloc. */
191 HOWTO (R_MN10300_16,
195 FALSE,
197 complain_overflow_bitfield,
198 bfd_elf_generic_reloc,
199 "R_MN10300_16",
200 FALSE,
201 0xffff,
202 0xffff,
203 FALSE),
204 /* Standard 8 bit reloc. */
205 HOWTO (R_MN10300_8,
209 FALSE,
211 complain_overflow_bitfield,
212 bfd_elf_generic_reloc,
213 "R_MN10300_8",
214 FALSE,
215 0xff,
216 0xff,
217 FALSE),
218 /* Standard 32bit pc-relative reloc. */
219 HOWTO (R_MN10300_PCREL32,
223 TRUE,
225 complain_overflow_bitfield,
226 bfd_elf_generic_reloc,
227 "R_MN10300_PCREL32",
228 FALSE,
229 0xffffffff,
230 0xffffffff,
231 TRUE),
232 /* Standard 16bit pc-relative reloc. */
233 HOWTO (R_MN10300_PCREL16,
237 TRUE,
239 complain_overflow_bitfield,
240 bfd_elf_generic_reloc,
241 "R_MN10300_PCREL16",
242 FALSE,
243 0xffff,
244 0xffff,
245 TRUE),
246 /* Standard 8 pc-relative reloc. */
247 HOWTO (R_MN10300_PCREL8,
251 TRUE,
253 complain_overflow_bitfield,
254 bfd_elf_generic_reloc,
255 "R_MN10300_PCREL8",
256 FALSE,
257 0xff,
258 0xff,
259 TRUE),
261 /* GNU extension to record C++ vtable hierarchy */
262 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
263 0, /* rightshift */
264 0, /* size (0 = byte, 1 = short, 2 = long) */
265 0, /* bitsize */
266 FALSE, /* pc_relative */
267 0, /* bitpos */
268 complain_overflow_dont, /* complain_on_overflow */
269 NULL, /* special_function */
270 "R_MN10300_GNU_VTINHERIT", /* name */
271 FALSE, /* partial_inplace */
272 0, /* src_mask */
273 0, /* dst_mask */
274 FALSE), /* pcrel_offset */
276 /* GNU extension to record C++ vtable member usage */
277 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
278 0, /* rightshift */
279 0, /* size (0 = byte, 1 = short, 2 = long) */
280 0, /* bitsize */
281 FALSE, /* pc_relative */
282 0, /* bitpos */
283 complain_overflow_dont, /* complain_on_overflow */
284 NULL, /* special_function */
285 "R_MN10300_GNU_VTENTRY", /* name */
286 FALSE, /* partial_inplace */
287 0, /* src_mask */
288 0, /* dst_mask */
289 FALSE), /* pcrel_offset */
291 /* Standard 24 bit reloc. */
292 HOWTO (R_MN10300_24,
296 FALSE,
298 complain_overflow_bitfield,
299 bfd_elf_generic_reloc,
300 "R_MN10300_24",
301 FALSE,
302 0xffffff,
303 0xffffff,
304 FALSE),
305 HOWTO (R_MN10300_GOTPC32, /* type */
306 0, /* rightshift */
307 2, /* size (0 = byte, 1 = short, 2 = long) */
308 32, /* bitsize */
309 TRUE, /* pc_relative */
310 0, /* bitpos */
311 complain_overflow_bitfield, /* complain_on_overflow */
312 bfd_elf_generic_reloc, /* */
313 "R_MN10300_GOTPC32", /* name */
314 FALSE, /* partial_inplace */
315 0xffffffff, /* src_mask */
316 0xffffffff, /* dst_mask */
317 TRUE), /* pcrel_offset */
319 HOWTO (R_MN10300_GOTPC16, /* type */
320 0, /* rightshift */
321 1, /* size (0 = byte, 1 = short, 2 = long) */
322 16, /* bitsize */
323 TRUE, /* pc_relative */
324 0, /* bitpos */
325 complain_overflow_bitfield, /* complain_on_overflow */
326 bfd_elf_generic_reloc, /* */
327 "R_MN10300_GOTPC16", /* name */
328 FALSE, /* partial_inplace */
329 0xffff, /* src_mask */
330 0xffff, /* dst_mask */
331 TRUE), /* pcrel_offset */
333 HOWTO (R_MN10300_GOTOFF32, /* type */
334 0, /* rightshift */
335 2, /* size (0 = byte, 1 = short, 2 = long) */
336 32, /* bitsize */
337 FALSE, /* pc_relative */
338 0, /* bitpos */
339 complain_overflow_bitfield, /* complain_on_overflow */
340 bfd_elf_generic_reloc, /* */
341 "R_MN10300_GOTOFF32", /* name */
342 FALSE, /* partial_inplace */
343 0xffffffff, /* src_mask */
344 0xffffffff, /* dst_mask */
345 FALSE), /* pcrel_offset */
347 HOWTO (R_MN10300_GOTOFF24, /* type */
348 0, /* rightshift */
349 2, /* size (0 = byte, 1 = short, 2 = long) */
350 24, /* bitsize */
351 FALSE, /* pc_relative */
352 0, /* bitpos */
353 complain_overflow_bitfield, /* complain_on_overflow */
354 bfd_elf_generic_reloc, /* */
355 "R_MN10300_GOTOFF24", /* name */
356 FALSE, /* partial_inplace */
357 0xffffff, /* src_mask */
358 0xffffff, /* dst_mask */
359 FALSE), /* pcrel_offset */
361 HOWTO (R_MN10300_GOTOFF16, /* type */
362 0, /* rightshift */
363 1, /* size (0 = byte, 1 = short, 2 = long) */
364 16, /* bitsize */
365 FALSE, /* pc_relative */
366 0, /* bitpos */
367 complain_overflow_bitfield, /* complain_on_overflow */
368 bfd_elf_generic_reloc, /* */
369 "R_MN10300_GOTOFF16", /* name */
370 FALSE, /* partial_inplace */
371 0xffff, /* src_mask */
372 0xffff, /* dst_mask */
373 FALSE), /* pcrel_offset */
375 HOWTO (R_MN10300_PLT32, /* type */
376 0, /* rightshift */
377 2, /* size (0 = byte, 1 = short, 2 = long) */
378 32, /* bitsize */
379 TRUE, /* pc_relative */
380 0, /* bitpos */
381 complain_overflow_bitfield, /* complain_on_overflow */
382 bfd_elf_generic_reloc, /* */
383 "R_MN10300_PLT32", /* name */
384 FALSE, /* partial_inplace */
385 0xffffffff, /* src_mask */
386 0xffffffff, /* dst_mask */
387 TRUE), /* pcrel_offset */
389 HOWTO (R_MN10300_PLT16, /* type */
390 0, /* rightshift */
391 1, /* size (0 = byte, 1 = short, 2 = long) */
392 16, /* bitsize */
393 TRUE, /* pc_relative */
394 0, /* bitpos */
395 complain_overflow_bitfield, /* complain_on_overflow */
396 bfd_elf_generic_reloc, /* */
397 "R_MN10300_PLT16", /* name */
398 FALSE, /* partial_inplace */
399 0xffff, /* src_mask */
400 0xffff, /* dst_mask */
401 TRUE), /* pcrel_offset */
403 HOWTO (R_MN10300_GOT32, /* type */
404 0, /* rightshift */
405 2, /* size (0 = byte, 1 = short, 2 = long) */
406 32, /* bitsize */
407 FALSE, /* pc_relative */
408 0, /* bitpos */
409 complain_overflow_bitfield, /* complain_on_overflow */
410 bfd_elf_generic_reloc, /* */
411 "R_MN10300_GOT32", /* name */
412 FALSE, /* partial_inplace */
413 0xffffffff, /* src_mask */
414 0xffffffff, /* dst_mask */
415 FALSE), /* pcrel_offset */
417 HOWTO (R_MN10300_GOT24, /* type */
418 0, /* rightshift */
419 2, /* size (0 = byte, 1 = short, 2 = long) */
420 24, /* bitsize */
421 FALSE, /* pc_relative */
422 0, /* bitpos */
423 complain_overflow_bitfield, /* complain_on_overflow */
424 bfd_elf_generic_reloc, /* */
425 "R_MN10300_GOT24", /* name */
426 FALSE, /* partial_inplace */
427 0xffffffff, /* src_mask */
428 0xffffffff, /* dst_mask */
429 FALSE), /* pcrel_offset */
431 HOWTO (R_MN10300_GOT16, /* type */
432 0, /* rightshift */
433 1, /* size (0 = byte, 1 = short, 2 = long) */
434 16, /* bitsize */
435 FALSE, /* pc_relative */
436 0, /* bitpos */
437 complain_overflow_bitfield, /* complain_on_overflow */
438 bfd_elf_generic_reloc, /* */
439 "R_MN10300_GOT16", /* name */
440 FALSE, /* partial_inplace */
441 0xffffffff, /* src_mask */
442 0xffffffff, /* dst_mask */
443 FALSE), /* pcrel_offset */
445 HOWTO (R_MN10300_COPY, /* type */
446 0, /* rightshift */
447 2, /* size (0 = byte, 1 = short, 2 = long) */
448 32, /* bitsize */
449 FALSE, /* pc_relative */
450 0, /* bitpos */
451 complain_overflow_bitfield, /* complain_on_overflow */
452 bfd_elf_generic_reloc, /* */
453 "R_MN10300_COPY", /* name */
454 FALSE, /* partial_inplace */
455 0xffffffff, /* src_mask */
456 0xffffffff, /* dst_mask */
457 FALSE), /* pcrel_offset */
459 HOWTO (R_MN10300_GLOB_DAT, /* type */
460 0, /* rightshift */
461 2, /* size (0 = byte, 1 = short, 2 = long) */
462 32, /* bitsize */
463 FALSE, /* pc_relative */
464 0, /* bitpos */
465 complain_overflow_bitfield, /* complain_on_overflow */
466 bfd_elf_generic_reloc, /* */
467 "R_MN10300_GLOB_DAT", /* name */
468 FALSE, /* partial_inplace */
469 0xffffffff, /* src_mask */
470 0xffffffff, /* dst_mask */
471 FALSE), /* pcrel_offset */
473 HOWTO (R_MN10300_JMP_SLOT, /* type */
474 0, /* rightshift */
475 2, /* size (0 = byte, 1 = short, 2 = long) */
476 32, /* bitsize */
477 FALSE, /* pc_relative */
478 0, /* bitpos */
479 complain_overflow_bitfield, /* complain_on_overflow */
480 bfd_elf_generic_reloc, /* */
481 "R_MN10300_JMP_SLOT", /* name */
482 FALSE, /* partial_inplace */
483 0xffffffff, /* src_mask */
484 0xffffffff, /* dst_mask */
485 FALSE), /* pcrel_offset */
487 HOWTO (R_MN10300_RELATIVE, /* type */
488 0, /* rightshift */
489 2, /* size (0 = byte, 1 = short, 2 = long) */
490 32, /* bitsize */
491 FALSE, /* pc_relative */
492 0, /* bitpos */
493 complain_overflow_bitfield, /* complain_on_overflow */
494 bfd_elf_generic_reloc, /* */
495 "R_MN10300_RELATIVE", /* name */
496 FALSE, /* partial_inplace */
497 0xffffffff, /* src_mask */
498 0xffffffff, /* dst_mask */
499 FALSE), /* pcrel_offset */
503 struct mn10300_reloc_map {
504 bfd_reloc_code_real_type bfd_reloc_val;
505 unsigned char elf_reloc_val;
508 static const struct mn10300_reloc_map mn10300_reloc_map[] = {
509 { BFD_RELOC_NONE, R_MN10300_NONE, },
510 { BFD_RELOC_32, R_MN10300_32, },
511 { BFD_RELOC_16, R_MN10300_16, },
512 { BFD_RELOC_8, R_MN10300_8, },
513 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
514 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
515 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
516 { BFD_RELOC_24, R_MN10300_24, },
517 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
518 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
519 { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
520 { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
521 { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
522 { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
523 { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
524 { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
525 { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
526 { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
527 { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
528 { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
529 { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
530 { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
531 { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
532 { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
535 /* Create the GOT section. */
537 static bfd_boolean
538 _bfd_mn10300_elf_create_got_section (abfd, info)
539 bfd * abfd;
540 struct bfd_link_info * info;
542 flagword flags;
543 flagword pltflags;
544 asection * s;
545 struct elf_link_hash_entry * h;
546 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
547 int ptralign;
549 /* This function may be called more than once. */
550 if (bfd_get_section_by_name (abfd, ".got") != NULL)
551 return TRUE;
553 switch (bed->s->arch_size)
555 case 32:
556 ptralign = 2;
557 break;
559 case 64:
560 ptralign = 3;
561 break;
563 default:
564 bfd_set_error (bfd_error_bad_value);
565 return FALSE;
568 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
569 | SEC_LINKER_CREATED);
571 pltflags = flags;
572 pltflags |= SEC_CODE;
573 if (bed->plt_not_loaded)
574 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
575 if (bed->plt_readonly)
576 pltflags |= SEC_READONLY;
578 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
579 if (s == NULL
580 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
581 return FALSE;
583 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
584 .plt section. */
585 if (bed->want_plt_sym)
587 h = _bfd_elf_define_linkage_sym (abfd, info, s,
588 "_PROCEDURE_LINKAGE_TABLE_");
589 elf_hash_table (info)->hplt = h;
590 if (h == NULL)
591 return FALSE;
594 s = bfd_make_section_with_flags (abfd, ".got", flags);
595 if (s == NULL
596 || ! bfd_set_section_alignment (abfd, s, ptralign))
597 return FALSE;
599 if (bed->want_got_plt)
601 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
602 if (s == NULL
603 || ! bfd_set_section_alignment (abfd, s, ptralign))
604 return FALSE;
607 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
608 (or .got.plt) section. We don't do this in the linker script
609 because we don't want to define the symbol if we are not creating
610 a global offset table. */
611 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
612 elf_hash_table (info)->hgot = h;
613 if (h == NULL)
614 return FALSE;
616 /* The first bit of the global offset table is the header. */
617 s->size += bed->got_header_size;
619 return TRUE;
622 static reloc_howto_type *
623 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
624 bfd *abfd ATTRIBUTE_UNUSED;
625 bfd_reloc_code_real_type code;
627 unsigned int i;
629 for (i = 0;
630 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
631 i++)
633 if (mn10300_reloc_map[i].bfd_reloc_val == code)
634 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
637 return NULL;
640 /* Set the howto pointer for an MN10300 ELF reloc. */
642 static void
643 mn10300_info_to_howto (abfd, cache_ptr, dst)
644 bfd *abfd ATTRIBUTE_UNUSED;
645 arelent *cache_ptr;
646 Elf_Internal_Rela *dst;
648 unsigned int r_type;
650 r_type = ELF32_R_TYPE (dst->r_info);
651 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
652 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
655 /* Look through the relocs for a section during the first phase.
656 Since we don't do .gots or .plts, we just need to consider the
657 virtual table relocs for gc. */
659 static bfd_boolean
660 mn10300_elf_check_relocs (abfd, info, sec, relocs)
661 bfd *abfd;
662 struct bfd_link_info *info;
663 asection *sec;
664 const Elf_Internal_Rela *relocs;
666 Elf_Internal_Shdr *symtab_hdr;
667 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
668 const Elf_Internal_Rela *rel;
669 const Elf_Internal_Rela *rel_end;
670 bfd * dynobj;
671 bfd_vma * local_got_offsets;
672 asection * sgot;
673 asection * srelgot;
674 asection * sreloc;
676 sgot = NULL;
677 srelgot = NULL;
678 sreloc = NULL;
680 if (info->relocatable)
681 return TRUE;
683 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
684 sym_hashes = elf_sym_hashes (abfd);
685 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
686 if (!elf_bad_symtab (abfd))
687 sym_hashes_end -= symtab_hdr->sh_info;
689 dynobj = elf_hash_table (info)->dynobj;
690 local_got_offsets = elf_local_got_offsets (abfd);
691 rel_end = relocs + sec->reloc_count;
692 for (rel = relocs; rel < rel_end; rel++)
694 struct elf_link_hash_entry *h;
695 unsigned long r_symndx;
697 r_symndx = ELF32_R_SYM (rel->r_info);
698 if (r_symndx < symtab_hdr->sh_info)
699 h = NULL;
700 else
702 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
703 while (h->root.type == bfd_link_hash_indirect
704 || h->root.type == bfd_link_hash_warning)
705 h = (struct elf_link_hash_entry *) h->root.u.i.link;
708 /* Some relocs require a global offset table. */
709 if (dynobj == NULL)
711 switch (ELF32_R_TYPE (rel->r_info))
713 case R_MN10300_GOT32:
714 case R_MN10300_GOT24:
715 case R_MN10300_GOT16:
716 case R_MN10300_GOTOFF32:
717 case R_MN10300_GOTOFF24:
718 case R_MN10300_GOTOFF16:
719 case R_MN10300_GOTPC32:
720 case R_MN10300_GOTPC16:
721 elf_hash_table (info)->dynobj = dynobj = abfd;
722 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
723 return FALSE;
724 break;
726 default:
727 break;
731 switch (ELF32_R_TYPE (rel->r_info))
733 /* This relocation describes the C++ object vtable hierarchy.
734 Reconstruct it for later use during GC. */
735 case R_MN10300_GNU_VTINHERIT:
736 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
737 return FALSE;
738 break;
740 /* This relocation describes which C++ vtable entries are actually
741 used. Record for later use during GC. */
742 case R_MN10300_GNU_VTENTRY:
743 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
744 return FALSE;
745 break;
746 case R_MN10300_GOT32:
747 case R_MN10300_GOT24:
748 case R_MN10300_GOT16:
749 /* This symbol requires a global offset table entry. */
751 if (sgot == NULL)
753 sgot = bfd_get_section_by_name (dynobj, ".got");
754 BFD_ASSERT (sgot != NULL);
757 if (srelgot == NULL
758 && (h != NULL || info->shared))
760 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
761 if (srelgot == NULL)
763 srelgot = bfd_make_section_with_flags (dynobj,
764 ".rela.got",
765 (SEC_ALLOC
766 | SEC_LOAD
767 | SEC_HAS_CONTENTS
768 | SEC_IN_MEMORY
769 | SEC_LINKER_CREATED
770 | SEC_READONLY));
771 if (srelgot == NULL
772 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
773 return FALSE;
777 if (h != NULL)
779 if (h->got.offset != (bfd_vma) -1)
780 /* We have already allocated space in the .got. */
781 break;
783 h->got.offset = sgot->size;
785 /* Make sure this symbol is output as a dynamic symbol. */
786 if (h->dynindx == -1)
788 if (! bfd_elf_link_record_dynamic_symbol (info, h))
789 return FALSE;
792 srelgot->size += sizeof (Elf32_External_Rela);
794 else
796 /* This is a global offset table entry for a local
797 symbol. */
798 if (local_got_offsets == NULL)
800 size_t size;
801 unsigned int i;
803 size = symtab_hdr->sh_info * sizeof (bfd_vma);
804 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
806 if (local_got_offsets == NULL)
807 return FALSE;
808 elf_local_got_offsets (abfd) = local_got_offsets;
810 for (i = 0; i < symtab_hdr->sh_info; i++)
811 local_got_offsets[i] = (bfd_vma) -1;
814 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
815 /* We have already allocated space in the .got. */
816 break;
818 local_got_offsets[r_symndx] = sgot->size;
820 if (info->shared)
821 /* If we are generating a shared object, we need to
822 output a R_MN10300_RELATIVE reloc so that the dynamic
823 linker can adjust this GOT entry. */
824 srelgot->size += sizeof (Elf32_External_Rela);
827 sgot->size += 4;
829 break;
831 case R_MN10300_PLT32:
832 case R_MN10300_PLT16:
833 /* This symbol requires a procedure linkage table entry. We
834 actually build the entry in adjust_dynamic_symbol,
835 because this might be a case of linking PIC code which is
836 never referenced by a dynamic object, in which case we
837 don't need to generate a procedure linkage table entry
838 after all. */
840 /* If this is a local symbol, we resolve it directly without
841 creating a procedure linkage table entry. */
842 if (h == NULL)
843 continue;
845 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
846 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
847 break;
849 h->needs_plt = 1;
851 break;
853 case R_MN10300_24:
854 case R_MN10300_16:
855 case R_MN10300_8:
856 case R_MN10300_PCREL32:
857 case R_MN10300_PCREL16:
858 case R_MN10300_PCREL8:
859 if (h != NULL)
860 h->non_got_ref = 1;
861 break;
863 case R_MN10300_32:
864 if (h != NULL)
865 h->non_got_ref = 1;
867 /* If we are creating a shared library, then we need to copy
868 the reloc into the shared library. */
869 if (info->shared
870 && (sec->flags & SEC_ALLOC) != 0)
872 /* When creating a shared object, we must copy these
873 reloc types into the output file. We create a reloc
874 section in dynobj and make room for this reloc. */
875 if (sreloc == NULL)
877 const char * name;
879 name = (bfd_elf_string_from_elf_section
880 (abfd,
881 elf_elfheader (abfd)->e_shstrndx,
882 elf_section_data (sec)->rel_hdr.sh_name));
883 if (name == NULL)
884 return FALSE;
886 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
887 && strcmp (bfd_get_section_name (abfd, sec),
888 name + 5) == 0);
890 sreloc = bfd_get_section_by_name (dynobj, name);
891 if (sreloc == NULL)
893 flagword flags;
895 flags = (SEC_HAS_CONTENTS | SEC_READONLY
896 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
897 if ((sec->flags & SEC_ALLOC) != 0)
898 flags |= SEC_ALLOC | SEC_LOAD;
899 sreloc = bfd_make_section_with_flags (dynobj,
900 name,
901 flags);
902 if (sreloc == NULL
903 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
904 return FALSE;
908 sreloc->size += sizeof (Elf32_External_Rela);
911 break;
915 return TRUE;
918 /* Return the section that should be marked against GC for a given
919 relocation. */
921 static asection *
922 mn10300_elf_gc_mark_hook (sec, info, rel, h, sym)
923 asection *sec;
924 struct bfd_link_info *info ATTRIBUTE_UNUSED;
925 Elf_Internal_Rela *rel;
926 struct elf_link_hash_entry *h;
927 Elf_Internal_Sym *sym;
929 if (h != NULL)
931 switch (ELF32_R_TYPE (rel->r_info))
933 case R_MN10300_GNU_VTINHERIT:
934 case R_MN10300_GNU_VTENTRY:
935 break;
937 default:
938 switch (h->root.type)
940 case bfd_link_hash_defined:
941 case bfd_link_hash_defweak:
942 return h->root.u.def.section;
944 case bfd_link_hash_common:
945 return h->root.u.c.p->section;
947 default:
948 break;
952 else
953 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
955 return NULL;
958 /* Perform a relocation as part of a final link. */
959 static bfd_reloc_status_type
960 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
961 input_section, contents, offset, value,
962 addend, h, symndx, info, sym_sec, is_local)
963 reloc_howto_type *howto;
964 bfd *input_bfd;
965 bfd *output_bfd ATTRIBUTE_UNUSED;
966 asection *input_section;
967 bfd_byte *contents;
968 bfd_vma offset;
969 bfd_vma value;
970 bfd_vma addend;
971 struct elf_link_hash_entry * h;
972 unsigned long symndx;
973 struct bfd_link_info *info;
974 asection *sym_sec ATTRIBUTE_UNUSED;
975 int is_local ATTRIBUTE_UNUSED;
977 unsigned long r_type = howto->type;
978 bfd_byte *hit_data = contents + offset;
979 bfd * dynobj;
980 bfd_vma * local_got_offsets;
981 asection * sgot;
982 asection * splt;
983 asection * sreloc;
985 dynobj = elf_hash_table (info)->dynobj;
986 local_got_offsets = elf_local_got_offsets (input_bfd);
988 sgot = NULL;
989 splt = NULL;
990 sreloc = NULL;
992 switch (r_type)
994 case R_MN10300_24:
995 case R_MN10300_16:
996 case R_MN10300_8:
997 case R_MN10300_PCREL8:
998 case R_MN10300_PCREL16:
999 case R_MN10300_PCREL32:
1000 case R_MN10300_GOTOFF32:
1001 case R_MN10300_GOTOFF24:
1002 case R_MN10300_GOTOFF16:
1003 if (info->shared
1004 && (input_section->flags & SEC_ALLOC) != 0
1005 && h != NULL
1006 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1007 return bfd_reloc_dangerous;
1010 switch (r_type)
1012 case R_MN10300_NONE:
1013 return bfd_reloc_ok;
1015 case R_MN10300_32:
1016 if (info->shared
1017 && (input_section->flags & SEC_ALLOC) != 0)
1019 Elf_Internal_Rela outrel;
1020 bfd_boolean skip, relocate;
1022 /* When generating a shared object, these relocations are
1023 copied into the output file to be resolved at run
1024 time. */
1025 if (sreloc == NULL)
1027 const char * name;
1029 name = (bfd_elf_string_from_elf_section
1030 (input_bfd,
1031 elf_elfheader (input_bfd)->e_shstrndx,
1032 elf_section_data (input_section)->rel_hdr.sh_name));
1033 if (name == NULL)
1034 return FALSE;
1036 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1037 && strcmp (bfd_get_section_name (input_bfd,
1038 input_section),
1039 name + 5) == 0);
1041 sreloc = bfd_get_section_by_name (dynobj, name);
1042 BFD_ASSERT (sreloc != NULL);
1045 skip = FALSE;
1047 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1048 input_section, offset);
1049 if (outrel.r_offset == (bfd_vma) -1)
1050 skip = TRUE;
1052 outrel.r_offset += (input_section->output_section->vma
1053 + input_section->output_offset);
1055 if (skip)
1057 memset (&outrel, 0, sizeof outrel);
1058 relocate = FALSE;
1060 else
1062 /* h->dynindx may be -1 if this symbol was marked to
1063 become local. */
1064 if (h == NULL
1065 || SYMBOL_REFERENCES_LOCAL (info, h))
1067 relocate = TRUE;
1068 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1069 outrel.r_addend = value + addend;
1071 else
1073 BFD_ASSERT (h->dynindx != -1);
1074 relocate = FALSE;
1075 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1076 outrel.r_addend = value + addend;
1080 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1081 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1082 + sreloc->reloc_count));
1083 ++sreloc->reloc_count;
1085 /* If this reloc is against an external symbol, we do
1086 not want to fiddle with the addend. Otherwise, we
1087 need to include the symbol value so that it becomes
1088 an addend for the dynamic reloc. */
1089 if (! relocate)
1090 return bfd_reloc_ok;
1092 value += addend;
1093 bfd_put_32 (input_bfd, value, hit_data);
1094 return bfd_reloc_ok;
1096 case R_MN10300_24:
1097 value += addend;
1099 if ((long) value > 0x7fffff || (long) value < -0x800000)
1100 return bfd_reloc_overflow;
1102 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1103 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1104 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1105 return bfd_reloc_ok;
1107 case R_MN10300_16:
1108 value += addend;
1110 if ((long) value > 0x7fff || (long) value < -0x8000)
1111 return bfd_reloc_overflow;
1113 bfd_put_16 (input_bfd, value, hit_data);
1114 return bfd_reloc_ok;
1116 case R_MN10300_8:
1117 value += addend;
1119 if ((long) value > 0x7f || (long) value < -0x80)
1120 return bfd_reloc_overflow;
1122 bfd_put_8 (input_bfd, value, hit_data);
1123 return bfd_reloc_ok;
1125 case R_MN10300_PCREL8:
1126 value -= (input_section->output_section->vma
1127 + input_section->output_offset);
1128 value -= offset;
1129 value += addend;
1131 if ((long) value > 0xff || (long) value < -0x100)
1132 return bfd_reloc_overflow;
1134 bfd_put_8 (input_bfd, value, hit_data);
1135 return bfd_reloc_ok;
1137 case R_MN10300_PCREL16:
1138 value -= (input_section->output_section->vma
1139 + input_section->output_offset);
1140 value -= offset;
1141 value += addend;
1143 if ((long) value > 0xffff || (long) value < -0x10000)
1144 return bfd_reloc_overflow;
1146 bfd_put_16 (input_bfd, value, hit_data);
1147 return bfd_reloc_ok;
1149 case R_MN10300_PCREL32:
1150 value -= (input_section->output_section->vma
1151 + input_section->output_offset);
1152 value -= offset;
1153 value += addend;
1155 bfd_put_32 (input_bfd, value, hit_data);
1156 return bfd_reloc_ok;
1158 case R_MN10300_GNU_VTINHERIT:
1159 case R_MN10300_GNU_VTENTRY:
1160 return bfd_reloc_ok;
1162 case R_MN10300_GOTPC32:
1163 /* Use global offset table as symbol value. */
1165 value = bfd_get_section_by_name (dynobj,
1166 ".got")->output_section->vma;
1167 value -= (input_section->output_section->vma
1168 + input_section->output_offset);
1169 value -= offset;
1170 value += addend;
1172 bfd_put_32 (input_bfd, value, hit_data);
1173 return bfd_reloc_ok;
1175 case R_MN10300_GOTPC16:
1176 /* Use global offset table as symbol value. */
1178 value = bfd_get_section_by_name (dynobj,
1179 ".got")->output_section->vma;
1180 value -= (input_section->output_section->vma
1181 + input_section->output_offset);
1182 value -= offset;
1183 value += addend;
1185 if ((long) value > 0xffff || (long) value < -0x10000)
1186 return bfd_reloc_overflow;
1188 bfd_put_16 (input_bfd, value, hit_data);
1189 return bfd_reloc_ok;
1191 case R_MN10300_GOTOFF32:
1192 value -= bfd_get_section_by_name (dynobj,
1193 ".got")->output_section->vma;
1194 value += addend;
1196 bfd_put_32 (input_bfd, value, hit_data);
1197 return bfd_reloc_ok;
1199 case R_MN10300_GOTOFF24:
1200 value -= bfd_get_section_by_name (dynobj,
1201 ".got")->output_section->vma;
1202 value += addend;
1204 if ((long) value > 0x7fffff || (long) value < -0x800000)
1205 return bfd_reloc_overflow;
1207 bfd_put_8 (input_bfd, value, hit_data);
1208 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1209 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1210 return bfd_reloc_ok;
1212 case R_MN10300_GOTOFF16:
1213 value -= bfd_get_section_by_name (dynobj,
1214 ".got")->output_section->vma;
1215 value += addend;
1217 if ((long) value > 0xffff || (long) value < -0x10000)
1218 return bfd_reloc_overflow;
1220 bfd_put_16 (input_bfd, value, hit_data);
1221 return bfd_reloc_ok;
1223 case R_MN10300_PLT32:
1224 if (h != NULL
1225 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1226 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1227 && h->plt.offset != (bfd_vma) -1)
1229 asection * splt;
1231 splt = bfd_get_section_by_name (dynobj, ".plt");
1233 value = (splt->output_section->vma
1234 + splt->output_offset
1235 + h->plt.offset) - value;
1238 value -= (input_section->output_section->vma
1239 + input_section->output_offset);
1240 value -= offset;
1241 value += addend;
1243 bfd_put_32 (input_bfd, value, hit_data);
1244 return bfd_reloc_ok;
1246 case R_MN10300_PLT16:
1247 if (h != NULL
1248 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1249 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1250 && h->plt.offset != (bfd_vma) -1)
1252 asection * splt;
1254 splt = bfd_get_section_by_name (dynobj, ".plt");
1256 value = (splt->output_section->vma
1257 + splt->output_offset
1258 + h->plt.offset) - value;
1261 value -= (input_section->output_section->vma
1262 + input_section->output_offset);
1263 value -= offset;
1264 value += addend;
1266 if ((long) value > 0xffff || (long) value < -0x10000)
1267 return bfd_reloc_overflow;
1269 bfd_put_16 (input_bfd, value, hit_data);
1270 return bfd_reloc_ok;
1272 case R_MN10300_GOT32:
1273 case R_MN10300_GOT24:
1274 case R_MN10300_GOT16:
1276 asection * sgot;
1278 sgot = bfd_get_section_by_name (dynobj, ".got");
1280 if (h != NULL)
1282 bfd_vma off;
1284 off = h->got.offset;
1285 BFD_ASSERT (off != (bfd_vma) -1);
1287 if (! elf_hash_table (info)->dynamic_sections_created
1288 || SYMBOL_REFERENCES_LOCAL (info, h))
1289 /* This is actually a static link, or it is a
1290 -Bsymbolic link and the symbol is defined
1291 locally, or the symbol was forced to be local
1292 because of a version file. We must initialize
1293 this entry in the global offset table.
1295 When doing a dynamic link, we create a .rela.got
1296 relocation entry to initialize the value. This
1297 is done in the finish_dynamic_symbol routine. */
1298 bfd_put_32 (output_bfd, value,
1299 sgot->contents + off);
1301 value = sgot->output_offset + off;
1303 else
1305 bfd_vma off;
1307 off = elf_local_got_offsets (input_bfd)[symndx];
1309 bfd_put_32 (output_bfd, value, sgot->contents + off);
1311 if (info->shared)
1313 asection * srelgot;
1314 Elf_Internal_Rela outrel;
1316 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1317 BFD_ASSERT (srelgot != NULL);
1319 outrel.r_offset = (sgot->output_section->vma
1320 + sgot->output_offset
1321 + off);
1322 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1323 outrel.r_addend = value;
1324 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1325 (bfd_byte *) (((Elf32_External_Rela *)
1326 srelgot->contents)
1327 + srelgot->reloc_count));
1328 ++ srelgot->reloc_count;
1331 value = sgot->output_offset + off;
1335 value += addend;
1337 if (r_type == R_MN10300_GOT32)
1339 bfd_put_32 (input_bfd, value, hit_data);
1340 return bfd_reloc_ok;
1342 else if (r_type == R_MN10300_GOT24)
1344 if ((long) value > 0x7fffff || (long) value < -0x800000)
1345 return bfd_reloc_overflow;
1347 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1348 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1349 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1350 return bfd_reloc_ok;
1352 else if (r_type == R_MN10300_GOT16)
1354 if ((long) value > 0xffff || (long) value < -0x10000)
1355 return bfd_reloc_overflow;
1357 bfd_put_16 (input_bfd, value, hit_data);
1358 return bfd_reloc_ok;
1360 /* Fall through. */
1362 default:
1363 return bfd_reloc_notsupported;
1367 /* Relocate an MN10300 ELF section. */
1368 static bfd_boolean
1369 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1370 contents, relocs, local_syms, local_sections)
1371 bfd *output_bfd;
1372 struct bfd_link_info *info;
1373 bfd *input_bfd;
1374 asection *input_section;
1375 bfd_byte *contents;
1376 Elf_Internal_Rela *relocs;
1377 Elf_Internal_Sym *local_syms;
1378 asection **local_sections;
1380 Elf_Internal_Shdr *symtab_hdr;
1381 struct elf_link_hash_entry **sym_hashes;
1382 Elf_Internal_Rela *rel, *relend;
1384 if (info->relocatable)
1385 return TRUE;
1387 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1388 sym_hashes = elf_sym_hashes (input_bfd);
1390 rel = relocs;
1391 relend = relocs + input_section->reloc_count;
1392 for (; rel < relend; rel++)
1394 int r_type;
1395 reloc_howto_type *howto;
1396 unsigned long r_symndx;
1397 Elf_Internal_Sym *sym;
1398 asection *sec;
1399 struct elf32_mn10300_link_hash_entry *h;
1400 bfd_vma relocation;
1401 bfd_reloc_status_type r;
1403 r_symndx = ELF32_R_SYM (rel->r_info);
1404 r_type = ELF32_R_TYPE (rel->r_info);
1405 howto = elf_mn10300_howto_table + r_type;
1407 /* Just skip the vtable gc relocs. */
1408 if (r_type == R_MN10300_GNU_VTINHERIT
1409 || r_type == R_MN10300_GNU_VTENTRY)
1410 continue;
1412 h = NULL;
1413 sym = NULL;
1414 sec = NULL;
1415 if (r_symndx < symtab_hdr->sh_info)
1417 sym = local_syms + r_symndx;
1418 sec = local_sections[r_symndx];
1419 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1421 else
1423 bfd_boolean unresolved_reloc;
1424 bfd_boolean warned;
1425 struct elf_link_hash_entry *hh;
1427 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1428 r_symndx, symtab_hdr, sym_hashes,
1429 hh, sec, relocation,
1430 unresolved_reloc, warned);
1432 h = (struct elf32_mn10300_link_hash_entry *) hh;
1434 if ((h->root.root.type == bfd_link_hash_defined
1435 || h->root.root.type == bfd_link_hash_defweak)
1436 && ( r_type == R_MN10300_GOTPC32
1437 || r_type == R_MN10300_GOTPC16
1438 || (( r_type == R_MN10300_PLT32
1439 || r_type == R_MN10300_PLT16)
1440 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1441 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1442 && h->root.plt.offset != (bfd_vma) -1)
1443 || (( r_type == R_MN10300_GOT32
1444 || r_type == R_MN10300_GOT24
1445 || r_type == R_MN10300_GOT16)
1446 && elf_hash_table (info)->dynamic_sections_created
1447 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1448 || (r_type == R_MN10300_32
1449 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1450 && ((input_section->flags & SEC_ALLOC) != 0
1451 /* DWARF will emit R_MN10300_32 relocations
1452 in its sections against symbols defined
1453 externally in shared libraries. We can't
1454 do anything with them here. */
1455 || ((input_section->flags & SEC_DEBUGGING) != 0
1456 && h->root.def_dynamic)))))
1457 /* In these cases, we don't need the relocation
1458 value. We check specially because in some
1459 obscure cases sec->output_section will be NULL. */
1460 relocation = 0;
1462 else if (unresolved_reloc)
1463 (*_bfd_error_handler)
1464 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1465 input_bfd,
1466 input_section,
1467 (long) rel->r_offset,
1468 howto->name,
1469 h->root.root.root.string);
1472 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1473 input_section,
1474 contents, rel->r_offset,
1475 relocation, rel->r_addend,
1476 (struct elf_link_hash_entry *)h,
1477 r_symndx,
1478 info, sec, h == NULL);
1480 if (r != bfd_reloc_ok)
1482 const char *name;
1483 const char *msg = (const char *) 0;
1485 if (h != NULL)
1486 name = h->root.root.root.string;
1487 else
1489 name = (bfd_elf_string_from_elf_section
1490 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1491 if (name == NULL || *name == '\0')
1492 name = bfd_section_name (input_bfd, sec);
1495 switch (r)
1497 case bfd_reloc_overflow:
1498 if (! ((*info->callbacks->reloc_overflow)
1499 (info, (h ? &h->root.root : NULL), name,
1500 howto->name, (bfd_vma) 0, input_bfd,
1501 input_section, rel->r_offset)))
1502 return FALSE;
1503 break;
1505 case bfd_reloc_undefined:
1506 if (! ((*info->callbacks->undefined_symbol)
1507 (info, name, input_bfd, input_section,
1508 rel->r_offset, TRUE)))
1509 return FALSE;
1510 break;
1512 case bfd_reloc_outofrange:
1513 msg = _("internal error: out of range error");
1514 goto common_error;
1516 case bfd_reloc_notsupported:
1517 msg = _("internal error: unsupported relocation error");
1518 goto common_error;
1520 case bfd_reloc_dangerous:
1521 msg = _("internal error: dangerous error");
1522 goto common_error;
1524 default:
1525 msg = _("internal error: unknown error");
1526 /* fall through */
1528 common_error:
1529 if (!((*info->callbacks->warning)
1530 (info, msg, name, input_bfd, input_section,
1531 rel->r_offset)))
1532 return FALSE;
1533 break;
1538 return TRUE;
1541 /* Finish initializing one hash table entry. */
1542 static bfd_boolean
1543 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
1544 struct bfd_hash_entry *gen_entry;
1545 PTR in_args;
1547 struct elf32_mn10300_link_hash_entry *entry;
1548 struct bfd_link_info *link_info = (struct bfd_link_info *)in_args;
1549 unsigned int byte_count = 0;
1551 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1553 if (entry->root.root.type == bfd_link_hash_warning)
1554 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1556 /* If we already know we want to convert "call" to "calls" for calls
1557 to this symbol, then return now. */
1558 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1559 return TRUE;
1561 /* If there are no named calls to this symbol, or there's nothing we
1562 can move from the function itself into the "call" instruction,
1563 then note that all "call" instructions should be converted into
1564 "calls" instructions and return. If a symbol is available for
1565 dynamic symbol resolution (overridable or overriding), avoid
1566 custom calling conventions. */
1567 if (entry->direct_calls == 0
1568 || (entry->stack_size == 0 && entry->movm_args == 0)
1569 || (elf_hash_table (link_info)->dynamic_sections_created
1570 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1571 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1573 /* Make a note that we should convert "call" instructions to "calls"
1574 instructions for calls to this symbol. */
1575 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1576 return TRUE;
1579 /* We may be able to move some instructions from the function itself into
1580 the "call" instruction. Count how many bytes we might be able to
1581 eliminate in the function itself. */
1583 /* A movm instruction is two bytes. */
1584 if (entry->movm_args)
1585 byte_count += 2;
1587 /* Count the insn to allocate stack space too. */
1588 if (entry->stack_size > 0)
1590 if (entry->stack_size <= 128)
1591 byte_count += 3;
1592 else
1593 byte_count += 4;
1596 /* If using "call" will result in larger code, then turn all
1597 the associated "call" instructions into "calls" instructions. */
1598 if (byte_count < entry->direct_calls)
1599 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1601 /* This routine never fails. */
1602 return TRUE;
1605 /* This function handles relaxing for the mn10300.
1607 There are quite a few relaxing opportunities available on the mn10300:
1609 * calls:32 -> calls:16 2 bytes
1610 * call:32 -> call:16 2 bytes
1612 * call:32 -> calls:32 1 byte
1613 * call:16 -> calls:16 1 byte
1614 * These are done anytime using "calls" would result
1615 in smaller code, or when necessary to preserve the
1616 meaning of the program.
1618 * call:32 varies
1619 * call:16
1620 * In some circumstances we can move instructions
1621 from a function prologue into a "call" instruction.
1622 This is only done if the resulting code is no larger
1623 than the original code.
1625 * jmp:32 -> jmp:16 2 bytes
1626 * jmp:16 -> bra:8 1 byte
1628 * If the previous instruction is a conditional branch
1629 around the jump/bra, we may be able to reverse its condition
1630 and change its target to the jump's target. The jump/bra
1631 can then be deleted. 2 bytes
1633 * mov abs32 -> mov abs16 1 or 2 bytes
1635 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
1636 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
1638 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
1639 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
1641 We don't handle imm16->imm8 or d16->d8 as they're very rare
1642 and somewhat more difficult to support. */
1644 static bfd_boolean
1645 mn10300_elf_relax_section (abfd, sec, link_info, again)
1646 bfd *abfd;
1647 asection *sec;
1648 struct bfd_link_info *link_info;
1649 bfd_boolean *again;
1651 Elf_Internal_Shdr *symtab_hdr;
1652 Elf_Internal_Rela *internal_relocs = NULL;
1653 Elf_Internal_Rela *irel, *irelend;
1654 bfd_byte *contents = NULL;
1655 Elf_Internal_Sym *isymbuf = NULL;
1656 struct elf32_mn10300_link_hash_table *hash_table;
1657 asection *section = sec;
1659 /* Assume nothing changes. */
1660 *again = FALSE;
1662 /* We need a pointer to the mn10300 specific hash table. */
1663 hash_table = elf32_mn10300_hash_table (link_info);
1665 /* Initialize fields in each hash table entry the first time through. */
1666 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
1668 bfd *input_bfd;
1670 /* Iterate over all the input bfds. */
1671 for (input_bfd = link_info->input_bfds;
1672 input_bfd != NULL;
1673 input_bfd = input_bfd->link_next)
1675 /* We're going to need all the symbols for each bfd. */
1676 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1677 if (symtab_hdr->sh_info != 0)
1679 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1680 if (isymbuf == NULL)
1681 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1682 symtab_hdr->sh_info, 0,
1683 NULL, NULL, NULL);
1684 if (isymbuf == NULL)
1685 goto error_return;
1688 /* Iterate over each section in this bfd. */
1689 for (section = input_bfd->sections;
1690 section != NULL;
1691 section = section->next)
1693 struct elf32_mn10300_link_hash_entry *hash;
1694 Elf_Internal_Sym *sym;
1695 asection *sym_sec = NULL;
1696 const char *sym_name;
1697 char *new_name;
1699 /* If there's nothing to do in this section, skip it. */
1700 if (! (((section->flags & SEC_RELOC) != 0
1701 && section->reloc_count != 0)
1702 || (section->flags & SEC_CODE) != 0))
1703 continue;
1705 /* Get cached copy of section contents if it exists. */
1706 if (elf_section_data (section)->this_hdr.contents != NULL)
1707 contents = elf_section_data (section)->this_hdr.contents;
1708 else if (section->size != 0)
1710 /* Go get them off disk. */
1711 if (!bfd_malloc_and_get_section (input_bfd, section,
1712 &contents))
1713 goto error_return;
1715 else
1716 contents = NULL;
1718 /* If there aren't any relocs, then there's nothing to do. */
1719 if ((section->flags & SEC_RELOC) != 0
1720 && section->reloc_count != 0)
1723 /* Get a copy of the native relocations. */
1724 internal_relocs = (_bfd_elf_link_read_relocs
1725 (input_bfd, section, (PTR) NULL,
1726 (Elf_Internal_Rela *) NULL,
1727 link_info->keep_memory));
1728 if (internal_relocs == NULL)
1729 goto error_return;
1731 /* Now examine each relocation. */
1732 irel = internal_relocs;
1733 irelend = irel + section->reloc_count;
1734 for (; irel < irelend; irel++)
1736 long r_type;
1737 unsigned long r_index;
1738 unsigned char code;
1740 r_type = ELF32_R_TYPE (irel->r_info);
1741 r_index = ELF32_R_SYM (irel->r_info);
1743 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
1744 goto error_return;
1746 /* We need the name and hash table entry of the target
1747 symbol! */
1748 hash = NULL;
1749 sym = NULL;
1750 sym_sec = NULL;
1752 if (r_index < symtab_hdr->sh_info)
1754 /* A local symbol. */
1755 Elf_Internal_Sym *isym;
1756 struct elf_link_hash_table *elftab;
1757 bfd_size_type amt;
1759 isym = isymbuf + r_index;
1760 if (isym->st_shndx == SHN_UNDEF)
1761 sym_sec = bfd_und_section_ptr;
1762 else if (isym->st_shndx == SHN_ABS)
1763 sym_sec = bfd_abs_section_ptr;
1764 else if (isym->st_shndx == SHN_COMMON)
1765 sym_sec = bfd_com_section_ptr;
1766 else
1767 sym_sec
1768 = bfd_section_from_elf_index (input_bfd,
1769 isym->st_shndx);
1771 sym_name
1772 = bfd_elf_string_from_elf_section (input_bfd,
1773 (symtab_hdr
1774 ->sh_link),
1775 isym->st_name);
1777 /* If it isn't a function, then we don't care
1778 about it. */
1779 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
1780 continue;
1782 /* Tack on an ID so we can uniquely identify this
1783 local symbol in the global hash table. */
1784 amt = strlen (sym_name) + 10;
1785 new_name = bfd_malloc (amt);
1786 if (new_name == 0)
1787 goto error_return;
1789 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
1790 sym_name = new_name;
1792 elftab = &hash_table->static_hash_table->root;
1793 hash = ((struct elf32_mn10300_link_hash_entry *)
1794 elf_link_hash_lookup (elftab, sym_name,
1795 TRUE, TRUE, FALSE));
1796 free (new_name);
1798 else
1800 r_index -= symtab_hdr->sh_info;
1801 hash = (struct elf32_mn10300_link_hash_entry *)
1802 elf_sym_hashes (input_bfd)[r_index];
1805 /* If this is not a "call" instruction, then we
1806 should convert "call" instructions to "calls"
1807 instructions. */
1808 code = bfd_get_8 (input_bfd,
1809 contents + irel->r_offset - 1);
1810 if (code != 0xdd && code != 0xcd)
1811 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1813 /* If this is a jump/call, then bump the
1814 direct_calls counter. Else force "call" to
1815 "calls" conversions. */
1816 if (r_type == R_MN10300_PCREL32
1817 || r_type == R_MN10300_PLT32
1818 || r_type == R_MN10300_PLT16
1819 || r_type == R_MN10300_PCREL16)
1820 hash->direct_calls++;
1821 else
1822 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1826 /* Now look at the actual contents to get the stack size,
1827 and a list of what registers were saved in the prologue
1828 (ie movm_args). */
1829 if ((section->flags & SEC_CODE) != 0)
1831 Elf_Internal_Sym *isym, *isymend;
1832 unsigned int sec_shndx;
1833 struct elf_link_hash_entry **hashes;
1834 struct elf_link_hash_entry **end_hashes;
1835 unsigned int symcount;
1837 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1838 section);
1840 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1841 - symtab_hdr->sh_info);
1842 hashes = elf_sym_hashes (input_bfd);
1843 end_hashes = hashes + symcount;
1845 /* Look at each function defined in this section and
1846 update info for that function. */
1847 isymend = isymbuf + symtab_hdr->sh_info;
1848 for (isym = isymbuf; isym < isymend; isym++)
1850 if (isym->st_shndx == sec_shndx
1851 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
1853 struct elf_link_hash_table *elftab;
1854 bfd_size_type amt;
1855 struct elf_link_hash_entry **lhashes = hashes;
1857 /* Skip a local symbol if it aliases a
1858 global one. */
1859 for (; lhashes < end_hashes; lhashes++)
1861 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
1862 if ((hash->root.root.type == bfd_link_hash_defined
1863 || hash->root.root.type == bfd_link_hash_defweak)
1864 && hash->root.root.u.def.section == section
1865 && hash->root.type == STT_FUNC
1866 && hash->root.root.u.def.value == isym->st_value)
1867 break;
1869 if (lhashes != end_hashes)
1870 continue;
1872 if (isym->st_shndx == SHN_UNDEF)
1873 sym_sec = bfd_und_section_ptr;
1874 else if (isym->st_shndx == SHN_ABS)
1875 sym_sec = bfd_abs_section_ptr;
1876 else if (isym->st_shndx == SHN_COMMON)
1877 sym_sec = bfd_com_section_ptr;
1878 else
1879 sym_sec
1880 = bfd_section_from_elf_index (input_bfd,
1881 isym->st_shndx);
1883 sym_name = (bfd_elf_string_from_elf_section
1884 (input_bfd, symtab_hdr->sh_link,
1885 isym->st_name));
1887 /* Tack on an ID so we can uniquely identify this
1888 local symbol in the global hash table. */
1889 amt = strlen (sym_name) + 10;
1890 new_name = bfd_malloc (amt);
1891 if (new_name == 0)
1892 goto error_return;
1894 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
1895 sym_name = new_name;
1897 elftab = &hash_table->static_hash_table->root;
1898 hash = ((struct elf32_mn10300_link_hash_entry *)
1899 elf_link_hash_lookup (elftab, sym_name,
1900 TRUE, TRUE, FALSE));
1901 free (new_name);
1902 compute_function_info (input_bfd, hash,
1903 isym->st_value, contents);
1907 for (; hashes < end_hashes; hashes++)
1909 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1910 if ((hash->root.root.type == bfd_link_hash_defined
1911 || hash->root.root.type == bfd_link_hash_defweak)
1912 && hash->root.root.u.def.section == section
1913 && hash->root.type == STT_FUNC)
1914 compute_function_info (input_bfd, hash,
1915 (hash)->root.root.u.def.value,
1916 contents);
1920 /* Cache or free any memory we allocated for the relocs. */
1921 if (internal_relocs != NULL
1922 && elf_section_data (section)->relocs != internal_relocs)
1923 free (internal_relocs);
1924 internal_relocs = NULL;
1926 /* Cache or free any memory we allocated for the contents. */
1927 if (contents != NULL
1928 && elf_section_data (section)->this_hdr.contents != contents)
1930 if (! link_info->keep_memory)
1931 free (contents);
1932 else
1934 /* Cache the section contents for elf_link_input_bfd. */
1935 elf_section_data (section)->this_hdr.contents = contents;
1938 contents = NULL;
1941 /* Cache or free any memory we allocated for the symbols. */
1942 if (isymbuf != NULL
1943 && symtab_hdr->contents != (unsigned char *) isymbuf)
1945 if (! link_info->keep_memory)
1946 free (isymbuf);
1947 else
1949 /* Cache the symbols for elf_link_input_bfd. */
1950 symtab_hdr->contents = (unsigned char *) isymbuf;
1953 isymbuf = NULL;
1956 /* Now iterate on each symbol in the hash table and perform
1957 the final initialization steps on each. */
1958 elf32_mn10300_link_hash_traverse (hash_table,
1959 elf32_mn10300_finish_hash_table_entry,
1960 link_info);
1961 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1962 elf32_mn10300_finish_hash_table_entry,
1963 link_info);
1965 /* All entries in the hash table are fully initialized. */
1966 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1968 /* Now that everything has been initialized, go through each
1969 code section and delete any prologue insns which will be
1970 redundant because their operations will be performed by
1971 a "call" instruction. */
1972 for (input_bfd = link_info->input_bfds;
1973 input_bfd != NULL;
1974 input_bfd = input_bfd->link_next)
1976 /* We're going to need all the local symbols for each bfd. */
1977 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1978 if (symtab_hdr->sh_info != 0)
1980 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1981 if (isymbuf == NULL)
1982 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1983 symtab_hdr->sh_info, 0,
1984 NULL, NULL, NULL);
1985 if (isymbuf == NULL)
1986 goto error_return;
1989 /* Walk over each section in this bfd. */
1990 for (section = input_bfd->sections;
1991 section != NULL;
1992 section = section->next)
1994 unsigned int sec_shndx;
1995 Elf_Internal_Sym *isym, *isymend;
1996 struct elf_link_hash_entry **hashes;
1997 struct elf_link_hash_entry **end_hashes;
1998 unsigned int symcount;
2000 /* Skip non-code sections and empty sections. */
2001 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2002 continue;
2004 if (section->reloc_count != 0)
2006 /* Get a copy of the native relocations. */
2007 internal_relocs = (_bfd_elf_link_read_relocs
2008 (input_bfd, section, (PTR) NULL,
2009 (Elf_Internal_Rela *) NULL,
2010 link_info->keep_memory));
2011 if (internal_relocs == NULL)
2012 goto error_return;
2015 /* Get cached copy of section contents if it exists. */
2016 if (elf_section_data (section)->this_hdr.contents != NULL)
2017 contents = elf_section_data (section)->this_hdr.contents;
2018 else
2020 /* Go get them off disk. */
2021 if (!bfd_malloc_and_get_section (input_bfd, section,
2022 &contents))
2023 goto error_return;
2026 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2027 section);
2029 /* Now look for any function in this section which needs
2030 insns deleted from its prologue. */
2031 isymend = isymbuf + symtab_hdr->sh_info;
2032 for (isym = isymbuf; isym < isymend; isym++)
2034 struct elf32_mn10300_link_hash_entry *sym_hash;
2035 asection *sym_sec = NULL;
2036 const char *sym_name;
2037 char *new_name;
2038 struct elf_link_hash_table *elftab;
2039 bfd_size_type amt;
2041 if (isym->st_shndx != sec_shndx)
2042 continue;
2044 if (isym->st_shndx == SHN_UNDEF)
2045 sym_sec = bfd_und_section_ptr;
2046 else if (isym->st_shndx == SHN_ABS)
2047 sym_sec = bfd_abs_section_ptr;
2048 else if (isym->st_shndx == SHN_COMMON)
2049 sym_sec = bfd_com_section_ptr;
2050 else
2051 sym_sec
2052 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2054 sym_name
2055 = bfd_elf_string_from_elf_section (input_bfd,
2056 symtab_hdr->sh_link,
2057 isym->st_name);
2059 /* Tack on an ID so we can uniquely identify this
2060 local symbol in the global hash table. */
2061 amt = strlen (sym_name) + 10;
2062 new_name = bfd_malloc (amt);
2063 if (new_name == 0)
2064 goto error_return;
2065 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2066 sym_name = new_name;
2068 elftab = &hash_table->static_hash_table->root;
2069 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
2070 elf_link_hash_lookup (elftab, sym_name,
2071 FALSE, FALSE, FALSE));
2073 free (new_name);
2074 if (sym_hash == NULL)
2075 continue;
2077 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2078 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2080 int bytes = 0;
2082 /* Note that we've changed things. */
2083 elf_section_data (section)->relocs = internal_relocs;
2084 elf_section_data (section)->this_hdr.contents = contents;
2085 symtab_hdr->contents = (unsigned char *) isymbuf;
2087 /* Count how many bytes we're going to delete. */
2088 if (sym_hash->movm_args)
2089 bytes += 2;
2091 if (sym_hash->stack_size > 0)
2093 if (sym_hash->stack_size <= 128)
2094 bytes += 3;
2095 else
2096 bytes += 4;
2099 /* Note that we've deleted prologue bytes for this
2100 function. */
2101 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2103 /* Actually delete the bytes. */
2104 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2105 section,
2106 isym->st_value,
2107 bytes))
2108 goto error_return;
2110 /* Something changed. Not strictly necessary, but
2111 may lead to more relaxing opportunities. */
2112 *again = TRUE;
2116 /* Look for any global functions in this section which
2117 need insns deleted from their prologues. */
2118 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2119 - symtab_hdr->sh_info);
2120 hashes = elf_sym_hashes (input_bfd);
2121 end_hashes = hashes + symcount;
2122 for (; hashes < end_hashes; hashes++)
2124 struct elf32_mn10300_link_hash_entry *sym_hash;
2126 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2127 if ((sym_hash->root.root.type == bfd_link_hash_defined
2128 || sym_hash->root.root.type == bfd_link_hash_defweak)
2129 && sym_hash->root.root.u.def.section == section
2130 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2131 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2133 int bytes = 0;
2134 bfd_vma symval;
2136 /* Note that we've changed things. */
2137 elf_section_data (section)->relocs = internal_relocs;
2138 elf_section_data (section)->this_hdr.contents = contents;
2139 symtab_hdr->contents = (unsigned char *) isymbuf;
2141 /* Count how many bytes we're going to delete. */
2142 if (sym_hash->movm_args)
2143 bytes += 2;
2145 if (sym_hash->stack_size > 0)
2147 if (sym_hash->stack_size <= 128)
2148 bytes += 3;
2149 else
2150 bytes += 4;
2153 /* Note that we've deleted prologue bytes for this
2154 function. */
2155 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2157 /* Actually delete the bytes. */
2158 symval = sym_hash->root.root.u.def.value;
2159 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2160 section,
2161 symval,
2162 bytes))
2163 goto error_return;
2165 /* Something changed. Not strictly necessary, but
2166 may lead to more relaxing opportunities. */
2167 *again = TRUE;
2171 /* Cache or free any memory we allocated for the relocs. */
2172 if (internal_relocs != NULL
2173 && elf_section_data (section)->relocs != internal_relocs)
2174 free (internal_relocs);
2175 internal_relocs = NULL;
2177 /* Cache or free any memory we allocated for the contents. */
2178 if (contents != NULL
2179 && elf_section_data (section)->this_hdr.contents != contents)
2181 if (! link_info->keep_memory)
2182 free (contents);
2183 else
2185 /* Cache the section contents for elf_link_input_bfd. */
2186 elf_section_data (section)->this_hdr.contents = contents;
2189 contents = NULL;
2192 /* Cache or free any memory we allocated for the symbols. */
2193 if (isymbuf != NULL
2194 && symtab_hdr->contents != (unsigned char *) isymbuf)
2196 if (! link_info->keep_memory)
2197 free (isymbuf);
2198 else
2200 /* Cache the symbols for elf_link_input_bfd. */
2201 symtab_hdr->contents = (unsigned char *) isymbuf;
2204 isymbuf = NULL;
2208 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2209 contents = NULL;
2210 internal_relocs = NULL;
2211 isymbuf = NULL;
2212 /* For error_return. */
2213 section = sec;
2215 /* We don't have to do anything for a relocatable link, if
2216 this section does not have relocs, or if this is not a
2217 code section. */
2218 if (link_info->relocatable
2219 || (sec->flags & SEC_RELOC) == 0
2220 || sec->reloc_count == 0
2221 || (sec->flags & SEC_CODE) == 0)
2222 return TRUE;
2224 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2226 /* Get a copy of the native relocations. */
2227 internal_relocs = (_bfd_elf_link_read_relocs
2228 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2229 link_info->keep_memory));
2230 if (internal_relocs == NULL)
2231 goto error_return;
2233 /* Walk through them looking for relaxing opportunities. */
2234 irelend = internal_relocs + sec->reloc_count;
2235 for (irel = internal_relocs; irel < irelend; irel++)
2237 bfd_vma symval;
2238 struct elf32_mn10300_link_hash_entry *h = NULL;
2240 /* If this isn't something that can be relaxed, then ignore
2241 this reloc. */
2242 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2243 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2244 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2245 continue;
2247 /* Get the section contents if we haven't done so already. */
2248 if (contents == NULL)
2250 /* Get cached copy if it exists. */
2251 if (elf_section_data (sec)->this_hdr.contents != NULL)
2252 contents = elf_section_data (sec)->this_hdr.contents;
2253 else
2255 /* Go get them off disk. */
2256 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2257 goto error_return;
2261 /* Read this BFD's symbols if we haven't done so already. */
2262 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2264 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2265 if (isymbuf == NULL)
2266 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2267 symtab_hdr->sh_info, 0,
2268 NULL, NULL, NULL);
2269 if (isymbuf == NULL)
2270 goto error_return;
2273 /* Get the value of the symbol referred to by the reloc. */
2274 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2276 Elf_Internal_Sym *isym;
2277 asection *sym_sec = NULL;
2278 const char *sym_name;
2279 char *new_name;
2280 bfd_vma saved_addend;
2282 /* A local symbol. */
2283 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2284 if (isym->st_shndx == SHN_UNDEF)
2285 sym_sec = bfd_und_section_ptr;
2286 else if (isym->st_shndx == SHN_ABS)
2287 sym_sec = bfd_abs_section_ptr;
2288 else if (isym->st_shndx == SHN_COMMON)
2289 sym_sec = bfd_com_section_ptr;
2290 else
2291 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2293 sym_name = bfd_elf_string_from_elf_section (abfd,
2294 symtab_hdr->sh_link,
2295 isym->st_name);
2297 if ((sym_sec->flags & SEC_MERGE)
2298 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
2299 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2301 saved_addend = irel->r_addend;
2302 symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
2303 symval += irel->r_addend;
2304 irel->r_addend = saved_addend;
2306 else
2308 symval = (isym->st_value
2309 + sym_sec->output_section->vma
2310 + sym_sec->output_offset);
2312 /* Tack on an ID so we can uniquely identify this
2313 local symbol in the global hash table. */
2314 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2315 if (new_name == 0)
2316 goto error_return;
2317 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2318 sym_name = new_name;
2320 h = (struct elf32_mn10300_link_hash_entry *)
2321 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2322 sym_name, FALSE, FALSE, FALSE);
2323 free (new_name);
2325 else
2327 unsigned long indx;
2329 /* An external symbol. */
2330 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2331 h = (struct elf32_mn10300_link_hash_entry *)
2332 (elf_sym_hashes (abfd)[indx]);
2333 BFD_ASSERT (h != NULL);
2334 if (h->root.root.type != bfd_link_hash_defined
2335 && h->root.root.type != bfd_link_hash_defweak)
2337 /* This appears to be a reference to an undefined
2338 symbol. Just ignore it--it will be caught by the
2339 regular reloc processing. */
2340 continue;
2343 symval = (h->root.root.u.def.value
2344 + h->root.root.u.def.section->output_section->vma
2345 + h->root.root.u.def.section->output_offset);
2348 /* For simplicity of coding, we are going to modify the section
2349 contents, the section relocs, and the BFD symbol table. We
2350 must tell the rest of the code not to free up this
2351 information. It would be possible to instead create a table
2352 of changes which have to be made, as is done in coff-mips.c;
2353 that would be more work, but would require less memory when
2354 the linker is run. */
2356 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2357 branch/call, also deal with "call" -> "calls" conversions and
2358 insertion of prologue data into "call" instructions. */
2359 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2360 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2362 bfd_vma value = symval;
2364 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2365 && h != NULL
2366 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2367 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2368 && h->root.plt.offset != (bfd_vma) -1)
2370 asection * splt;
2372 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2373 ->dynobj, ".plt");
2375 value = ((splt->output_section->vma
2376 + splt->output_offset
2377 + h->root.plt.offset)
2378 - (sec->output_section->vma
2379 + sec->output_offset
2380 + irel->r_offset));
2383 /* If we've got a "call" instruction that needs to be turned
2384 into a "calls" instruction, do so now. It saves a byte. */
2385 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2387 unsigned char code;
2389 /* Get the opcode. */
2390 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2392 /* Make sure we're working with a "call" instruction! */
2393 if (code == 0xdd)
2395 /* Note that we've changed the relocs, section contents,
2396 etc. */
2397 elf_section_data (sec)->relocs = internal_relocs;
2398 elf_section_data (sec)->this_hdr.contents = contents;
2399 symtab_hdr->contents = (unsigned char *) isymbuf;
2401 /* Fix the opcode. */
2402 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2403 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2405 /* Fix irel->r_offset and irel->r_addend. */
2406 irel->r_offset += 1;
2407 irel->r_addend += 1;
2409 /* Delete one byte of data. */
2410 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2411 irel->r_offset + 3, 1))
2412 goto error_return;
2414 /* That will change things, so, we should relax again.
2415 Note that this is not required, and it may be slow. */
2416 *again = TRUE;
2419 else if (h)
2421 /* We've got a "call" instruction which needs some data
2422 from target function filled in. */
2423 unsigned char code;
2425 /* Get the opcode. */
2426 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2428 /* Insert data from the target function into the "call"
2429 instruction if needed. */
2430 if (code == 0xdd)
2432 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2433 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2434 contents + irel->r_offset + 5);
2438 /* Deal with pc-relative gunk. */
2439 value -= (sec->output_section->vma + sec->output_offset);
2440 value -= irel->r_offset;
2441 value += irel->r_addend;
2443 /* See if the value will fit in 16 bits, note the high value is
2444 0x7fff + 2 as the target will be two bytes closer if we are
2445 able to relax. */
2446 if ((long) value < 0x8001 && (long) value > -0x8000)
2448 unsigned char code;
2450 /* Get the opcode. */
2451 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2453 if (code != 0xdc && code != 0xdd && code != 0xff)
2454 continue;
2456 /* Note that we've changed the relocs, section contents, etc. */
2457 elf_section_data (sec)->relocs = internal_relocs;
2458 elf_section_data (sec)->this_hdr.contents = contents;
2459 symtab_hdr->contents = (unsigned char *) isymbuf;
2461 /* Fix the opcode. */
2462 if (code == 0xdc)
2463 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2464 else if (code == 0xdd)
2465 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2466 else if (code == 0xff)
2467 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2469 /* Fix the relocation's type. */
2470 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2471 (ELF32_R_TYPE (irel->r_info)
2472 == (int) R_MN10300_PLT32)
2473 ? R_MN10300_PLT16 :
2474 R_MN10300_PCREL16);
2476 /* Delete two bytes of data. */
2477 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2478 irel->r_offset + 1, 2))
2479 goto error_return;
2481 /* That will change things, so, we should relax again.
2482 Note that this is not required, and it may be slow. */
2483 *again = TRUE;
2487 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2488 branch. */
2489 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2491 bfd_vma value = symval;
2493 /* If we've got a "call" instruction that needs to be turned
2494 into a "calls" instruction, do so now. It saves a byte. */
2495 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2497 unsigned char code;
2499 /* Get the opcode. */
2500 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2502 /* Make sure we're working with a "call" instruction! */
2503 if (code == 0xcd)
2505 /* Note that we've changed the relocs, section contents,
2506 etc. */
2507 elf_section_data (sec)->relocs = internal_relocs;
2508 elf_section_data (sec)->this_hdr.contents = contents;
2509 symtab_hdr->contents = (unsigned char *) isymbuf;
2511 /* Fix the opcode. */
2512 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
2513 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2515 /* Fix irel->r_offset and irel->r_addend. */
2516 irel->r_offset += 1;
2517 irel->r_addend += 1;
2519 /* Delete one byte of data. */
2520 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2521 irel->r_offset + 1, 1))
2522 goto error_return;
2524 /* That will change things, so, we should relax again.
2525 Note that this is not required, and it may be slow. */
2526 *again = TRUE;
2529 else if (h)
2531 unsigned char code;
2533 /* Get the opcode. */
2534 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2536 /* Insert data from the target function into the "call"
2537 instruction if needed. */
2538 if (code == 0xcd)
2540 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
2541 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2542 contents + irel->r_offset + 3);
2546 /* Deal with pc-relative gunk. */
2547 value -= (sec->output_section->vma + sec->output_offset);
2548 value -= irel->r_offset;
2549 value += irel->r_addend;
2551 /* See if the value will fit in 8 bits, note the high value is
2552 0x7f + 1 as the target will be one bytes closer if we are
2553 able to relax. */
2554 if ((long) value < 0x80 && (long) value > -0x80)
2556 unsigned char code;
2558 /* Get the opcode. */
2559 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2561 if (code != 0xcc)
2562 continue;
2564 /* Note that we've changed the relocs, section contents, etc. */
2565 elf_section_data (sec)->relocs = internal_relocs;
2566 elf_section_data (sec)->this_hdr.contents = contents;
2567 symtab_hdr->contents = (unsigned char *) isymbuf;
2569 /* Fix the opcode. */
2570 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
2572 /* Fix the relocation's type. */
2573 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2574 R_MN10300_PCREL8);
2576 /* Delete one byte of data. */
2577 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2578 irel->r_offset + 1, 1))
2579 goto error_return;
2581 /* That will change things, so, we should relax again.
2582 Note that this is not required, and it may be slow. */
2583 *again = TRUE;
2587 /* Try to eliminate an unconditional 8 bit pc-relative branch
2588 which immediately follows a conditional 8 bit pc-relative
2589 branch around the unconditional branch.
2591 original: new:
2592 bCC lab1 bCC' lab2
2593 bra lab2
2594 lab1: lab1:
2596 This happens when the bCC can't reach lab2 at assembly time,
2597 but due to other relaxations it can reach at link time. */
2598 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
2600 Elf_Internal_Rela *nrel;
2601 bfd_vma value = symval;
2602 unsigned char code;
2604 /* Deal with pc-relative gunk. */
2605 value -= (sec->output_section->vma + sec->output_offset);
2606 value -= irel->r_offset;
2607 value += irel->r_addend;
2609 /* Do nothing if this reloc is the last byte in the section. */
2610 if (irel->r_offset == sec->size)
2611 continue;
2613 /* See if the next instruction is an unconditional pc-relative
2614 branch, more often than not this test will fail, so we
2615 test it first to speed things up. */
2616 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2617 if (code != 0xca)
2618 continue;
2620 /* Also make sure the next relocation applies to the next
2621 instruction and that it's a pc-relative 8 bit branch. */
2622 nrel = irel + 1;
2623 if (nrel == irelend
2624 || irel->r_offset + 2 != nrel->r_offset
2625 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
2626 continue;
2628 /* Make sure our destination immediately follows the
2629 unconditional branch. */
2630 if (symval != (sec->output_section->vma + sec->output_offset
2631 + irel->r_offset + 3))
2632 continue;
2634 /* Now make sure we are a conditional branch. This may not
2635 be necessary, but why take the chance.
2637 Note these checks assume that R_MN10300_PCREL8 relocs
2638 only occur on bCC and bCCx insns. If they occured
2639 elsewhere, we'd need to know the start of this insn
2640 for this check to be accurate. */
2641 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2642 if (code != 0xc0 && code != 0xc1 && code != 0xc2
2643 && code != 0xc3 && code != 0xc4 && code != 0xc5
2644 && code != 0xc6 && code != 0xc7 && code != 0xc8
2645 && code != 0xc9 && code != 0xe8 && code != 0xe9
2646 && code != 0xea && code != 0xeb)
2647 continue;
2649 /* We also have to be sure there is no symbol/label
2650 at the unconditional branch. */
2651 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
2652 irel->r_offset + 1))
2653 continue;
2655 /* Note that we've changed the relocs, section contents, etc. */
2656 elf_section_data (sec)->relocs = internal_relocs;
2657 elf_section_data (sec)->this_hdr.contents = contents;
2658 symtab_hdr->contents = (unsigned char *) isymbuf;
2660 /* Reverse the condition of the first branch. */
2661 switch (code)
2663 case 0xc8:
2664 code = 0xc9;
2665 break;
2666 case 0xc9:
2667 code = 0xc8;
2668 break;
2669 case 0xc0:
2670 code = 0xc2;
2671 break;
2672 case 0xc2:
2673 code = 0xc0;
2674 break;
2675 case 0xc3:
2676 code = 0xc1;
2677 break;
2678 case 0xc1:
2679 code = 0xc3;
2680 break;
2681 case 0xc4:
2682 code = 0xc6;
2683 break;
2684 case 0xc6:
2685 code = 0xc4;
2686 break;
2687 case 0xc7:
2688 code = 0xc5;
2689 break;
2690 case 0xc5:
2691 code = 0xc7;
2692 break;
2693 case 0xe8:
2694 code = 0xe9;
2695 break;
2696 case 0x9d:
2697 code = 0xe8;
2698 break;
2699 case 0xea:
2700 code = 0xeb;
2701 break;
2702 case 0xeb:
2703 code = 0xea;
2704 break;
2706 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2708 /* Set the reloc type and symbol for the first branch
2709 from the second branch. */
2710 irel->r_info = nrel->r_info;
2712 /* Make the reloc for the second branch a null reloc. */
2713 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
2714 R_MN10300_NONE);
2716 /* Delete two bytes of data. */
2717 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2718 irel->r_offset + 1, 2))
2719 goto error_return;
2721 /* That will change things, so, we should relax again.
2722 Note that this is not required, and it may be slow. */
2723 *again = TRUE;
2726 /* Try to turn a 24 immediate, displacement or absolute address
2727 into a 8 immediate, displacement or absolute address. */
2728 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
2730 bfd_vma value = symval;
2731 value += irel->r_addend;
2733 /* See if the value will fit in 8 bits. */
2734 if ((long) value < 0x7f && (long) value > -0x80)
2736 unsigned char code;
2738 /* AM33 insns which have 24 operands are 6 bytes long and
2739 will have 0xfd as the first byte. */
2741 /* Get the first opcode. */
2742 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2744 if (code == 0xfd)
2746 /* Get the second opcode. */
2747 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2749 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2750 equivalent instructions exists. */
2751 if (code != 0x6b && code != 0x7b
2752 && code != 0x8b && code != 0x9b
2753 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2754 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2755 || (code & 0x0f) == 0x0e))
2757 /* Not safe if the high bit is on as relaxing may
2758 move the value out of high mem and thus not fit
2759 in a signed 8bit value. This is currently over
2760 conservative. */
2761 if ((value & 0x80) == 0)
2763 /* Note that we've changed the relocation contents,
2764 etc. */
2765 elf_section_data (sec)->relocs = internal_relocs;
2766 elf_section_data (sec)->this_hdr.contents = contents;
2767 symtab_hdr->contents = (unsigned char *) isymbuf;
2769 /* Fix the opcode. */
2770 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2771 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2773 /* Fix the relocation's type. */
2774 irel->r_info =
2775 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2776 R_MN10300_8);
2778 /* Delete two bytes of data. */
2779 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2780 irel->r_offset + 1, 2))
2781 goto error_return;
2783 /* That will change things, so, we should relax
2784 again. Note that this is not required, and it
2785 may be slow. */
2786 *again = TRUE;
2787 break;
2794 /* Try to turn a 32bit immediate, displacement or absolute address
2795 into a 16bit immediate, displacement or absolute address. */
2796 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
2797 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
2798 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32
2799 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2801 bfd_vma value = symval;
2803 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
2805 asection * sgot;
2807 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
2808 ->dynobj, ".got");
2810 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
2812 value = sgot->output_offset;
2814 if (h)
2815 value += h->root.got.offset;
2816 else
2817 value += (elf_local_got_offsets
2818 (abfd)[ELF32_R_SYM (irel->r_info)]);
2820 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
2821 value -= sgot->output_section->vma;
2822 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2823 value = (sgot->output_section->vma
2824 - (sec->output_section->vma
2825 + sec->output_offset
2826 + irel->r_offset));
2827 else
2828 abort ();
2831 value += irel->r_addend;
2833 /* See if the value will fit in 24 bits.
2834 We allow any 16bit match here. We prune those we can't
2835 handle below. */
2836 if ((long) value < 0x7fffff && (long) value > -0x800000)
2838 unsigned char code;
2840 /* AM33 insns which have 32bit operands are 7 bytes long and
2841 will have 0xfe as the first byte. */
2843 /* Get the first opcode. */
2844 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2846 if (code == 0xfe)
2848 /* Get the second opcode. */
2849 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2851 /* All the am33 32 -> 24 relaxing possibilities. */
2852 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2853 equivalent instructions exists. */
2854 if (code != 0x6b && code != 0x7b
2855 && code != 0x8b && code != 0x9b
2856 && (ELF32_R_TYPE (irel->r_info)
2857 != (int) R_MN10300_GOTPC32)
2858 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2859 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2860 || (code & 0x0f) == 0x0e))
2862 /* Not safe if the high bit is on as relaxing may
2863 move the value out of high mem and thus not fit
2864 in a signed 16bit value. This is currently over
2865 conservative. */
2866 if ((value & 0x8000) == 0)
2868 /* Note that we've changed the relocation contents,
2869 etc. */
2870 elf_section_data (sec)->relocs = internal_relocs;
2871 elf_section_data (sec)->this_hdr.contents = contents;
2872 symtab_hdr->contents = (unsigned char *) isymbuf;
2874 /* Fix the opcode. */
2875 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2876 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2878 /* Fix the relocation's type. */
2879 irel->r_info =
2880 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2881 (ELF32_R_TYPE (irel->r_info)
2882 == (int) R_MN10300_GOTOFF32)
2883 ? R_MN10300_GOTOFF24
2884 : (ELF32_R_TYPE (irel->r_info)
2885 == (int) R_MN10300_GOT32)
2886 ? R_MN10300_GOT24 :
2887 R_MN10300_24);
2889 /* Delete one byte of data. */
2890 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2891 irel->r_offset + 3, 1))
2892 goto error_return;
2894 /* That will change things, so, we should relax
2895 again. Note that this is not required, and it
2896 may be slow. */
2897 *again = TRUE;
2898 break;
2904 /* See if the value will fit in 16 bits.
2905 We allow any 16bit match here. We prune those we can't
2906 handle below. */
2907 if ((long) value < 0x7fff && (long) value > -0x8000)
2909 unsigned char code;
2911 /* Most insns which have 32bit operands are 6 bytes long;
2912 exceptions are pcrel insns and bit insns.
2914 We handle pcrel insns above. We don't bother trying
2915 to handle the bit insns here.
2917 The first byte of the remaining insns will be 0xfc. */
2919 /* Get the first opcode. */
2920 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2922 if (code != 0xfc)
2923 continue;
2925 /* Get the second opcode. */
2926 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2928 if ((code & 0xf0) < 0x80)
2929 switch (code & 0xf0)
2931 /* mov (d32,am),dn -> mov (d32,am),dn
2932 mov dm,(d32,am) -> mov dn,(d32,am)
2933 mov (d32,am),an -> mov (d32,am),an
2934 mov dm,(d32,am) -> mov dn,(d32,am)
2935 movbu (d32,am),dn -> movbu (d32,am),dn
2936 movbu dm,(d32,am) -> movbu dn,(d32,am)
2937 movhu (d32,am),dn -> movhu (d32,am),dn
2938 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2939 case 0x00:
2940 case 0x10:
2941 case 0x20:
2942 case 0x30:
2943 case 0x40:
2944 case 0x50:
2945 case 0x60:
2946 case 0x70:
2947 /* Not safe if the high bit is on as relaxing may
2948 move the value out of high mem and thus not fit
2949 in a signed 16bit value. */
2950 if (code == 0xcc
2951 && (value & 0x8000))
2952 continue;
2954 /* Note that we've changed the relocation contents, etc. */
2955 elf_section_data (sec)->relocs = internal_relocs;
2956 elf_section_data (sec)->this_hdr.contents = contents;
2957 symtab_hdr->contents = (unsigned char *) isymbuf;
2959 /* Fix the opcode. */
2960 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2961 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2963 /* Fix the relocation's type. */
2964 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2965 (ELF32_R_TYPE (irel->r_info)
2966 == (int) R_MN10300_GOTOFF32)
2967 ? R_MN10300_GOTOFF16
2968 : (ELF32_R_TYPE (irel->r_info)
2969 == (int) R_MN10300_GOT32)
2970 ? R_MN10300_GOT16
2971 : (ELF32_R_TYPE (irel->r_info)
2972 == (int) R_MN10300_GOTPC32)
2973 ? R_MN10300_GOTPC16 :
2974 R_MN10300_16);
2976 /* Delete two bytes of data. */
2977 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2978 irel->r_offset + 2, 2))
2979 goto error_return;
2981 /* That will change things, so, we should relax again.
2982 Note that this is not required, and it may be slow. */
2983 *again = TRUE;
2984 break;
2986 else if ((code & 0xf0) == 0x80
2987 || (code & 0xf0) == 0x90)
2988 switch (code & 0xf3)
2990 /* mov dn,(abs32) -> mov dn,(abs16)
2991 movbu dn,(abs32) -> movbu dn,(abs16)
2992 movhu dn,(abs32) -> movhu dn,(abs16) */
2993 case 0x81:
2994 case 0x82:
2995 case 0x83:
2996 /* Note that we've changed the relocation contents, etc. */
2997 elf_section_data (sec)->relocs = internal_relocs;
2998 elf_section_data (sec)->this_hdr.contents = contents;
2999 symtab_hdr->contents = (unsigned char *) isymbuf;
3001 if ((code & 0xf3) == 0x81)
3002 code = 0x01 + (code & 0x0c);
3003 else if ((code & 0xf3) == 0x82)
3004 code = 0x02 + (code & 0x0c);
3005 else if ((code & 0xf3) == 0x83)
3006 code = 0x03 + (code & 0x0c);
3007 else
3008 abort ();
3010 /* Fix the opcode. */
3011 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3013 /* Fix the relocation's type. */
3014 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3015 (ELF32_R_TYPE (irel->r_info)
3016 == (int) R_MN10300_GOTOFF32)
3017 ? R_MN10300_GOTOFF16
3018 : (ELF32_R_TYPE (irel->r_info)
3019 == (int) R_MN10300_GOT32)
3020 ? R_MN10300_GOT16
3021 : (ELF32_R_TYPE (irel->r_info)
3022 == (int) R_MN10300_GOTPC32)
3023 ? R_MN10300_GOTPC16 :
3024 R_MN10300_16);
3026 /* The opcode got shorter too, so we have to fix the
3027 addend and offset too! */
3028 irel->r_offset -= 1;
3030 /* Delete three bytes of data. */
3031 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3032 irel->r_offset + 1, 3))
3033 goto error_return;
3035 /* That will change things, so, we should relax again.
3036 Note that this is not required, and it may be slow. */
3037 *again = TRUE;
3038 break;
3040 /* mov am,(abs32) -> mov am,(abs16)
3041 mov am,(d32,sp) -> mov am,(d16,sp)
3042 mov dm,(d32,sp) -> mov dm,(d32,sp)
3043 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3044 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3045 case 0x80:
3046 case 0x90:
3047 case 0x91:
3048 case 0x92:
3049 case 0x93:
3050 /* sp-based offsets are zero-extended. */
3051 if (code >= 0x90 && code <= 0x93
3052 && (long)value < 0)
3053 continue;
3055 /* Note that we've changed the relocation contents, etc. */
3056 elf_section_data (sec)->relocs = internal_relocs;
3057 elf_section_data (sec)->this_hdr.contents = contents;
3058 symtab_hdr->contents = (unsigned char *) isymbuf;
3060 /* Fix the opcode. */
3061 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3062 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3064 /* Fix the relocation's type. */
3065 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3066 (ELF32_R_TYPE (irel->r_info)
3067 == (int) R_MN10300_GOTOFF32)
3068 ? R_MN10300_GOTOFF16
3069 : (ELF32_R_TYPE (irel->r_info)
3070 == (int) R_MN10300_GOT32)
3071 ? R_MN10300_GOT16
3072 : (ELF32_R_TYPE (irel->r_info)
3073 == (int) R_MN10300_GOTPC32)
3074 ? R_MN10300_GOTPC16 :
3075 R_MN10300_16);
3077 /* Delete two bytes of data. */
3078 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3079 irel->r_offset + 2, 2))
3080 goto error_return;
3082 /* That will change things, so, we should relax again.
3083 Note that this is not required, and it may be slow. */
3084 *again = TRUE;
3085 break;
3087 else if ((code & 0xf0) < 0xf0)
3088 switch (code & 0xfc)
3090 /* mov imm32,dn -> mov imm16,dn
3091 mov imm32,an -> mov imm16,an
3092 mov (abs32),dn -> mov (abs16),dn
3093 movbu (abs32),dn -> movbu (abs16),dn
3094 movhu (abs32),dn -> movhu (abs16),dn */
3095 case 0xcc:
3096 case 0xdc:
3097 case 0xa4:
3098 case 0xa8:
3099 case 0xac:
3100 /* Not safe if the high bit is on as relaxing may
3101 move the value out of high mem and thus not fit
3102 in a signed 16bit value. */
3103 if (code == 0xcc
3104 && (value & 0x8000))
3105 continue;
3107 /* mov imm16, an zero-extends the immediate. */
3108 if (code == 0xdc
3109 && (long)value < 0)
3110 continue;
3112 /* Note that we've changed the relocation contents, etc. */
3113 elf_section_data (sec)->relocs = internal_relocs;
3114 elf_section_data (sec)->this_hdr.contents = contents;
3115 symtab_hdr->contents = (unsigned char *) isymbuf;
3117 if ((code & 0xfc) == 0xcc)
3118 code = 0x2c + (code & 0x03);
3119 else if ((code & 0xfc) == 0xdc)
3120 code = 0x24 + (code & 0x03);
3121 else if ((code & 0xfc) == 0xa4)
3122 code = 0x30 + (code & 0x03);
3123 else if ((code & 0xfc) == 0xa8)
3124 code = 0x34 + (code & 0x03);
3125 else if ((code & 0xfc) == 0xac)
3126 code = 0x38 + (code & 0x03);
3127 else
3128 abort ();
3130 /* Fix the opcode. */
3131 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3133 /* Fix the relocation's type. */
3134 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3135 (ELF32_R_TYPE (irel->r_info)
3136 == (int) R_MN10300_GOTOFF32)
3137 ? R_MN10300_GOTOFF16
3138 : (ELF32_R_TYPE (irel->r_info)
3139 == (int) R_MN10300_GOT32)
3140 ? R_MN10300_GOT16
3141 : (ELF32_R_TYPE (irel->r_info)
3142 == (int) R_MN10300_GOTPC32)
3143 ? R_MN10300_GOTPC16 :
3144 R_MN10300_16);
3146 /* The opcode got shorter too, so we have to fix the
3147 addend and offset too! */
3148 irel->r_offset -= 1;
3150 /* Delete three bytes of data. */
3151 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3152 irel->r_offset + 1, 3))
3153 goto error_return;
3155 /* That will change things, so, we should relax again.
3156 Note that this is not required, and it may be slow. */
3157 *again = TRUE;
3158 break;
3160 /* mov (abs32),an -> mov (abs16),an
3161 mov (d32,sp),an -> mov (d16,sp),an
3162 mov (d32,sp),dn -> mov (d16,sp),dn
3163 movbu (d32,sp),dn -> movbu (d16,sp),dn
3164 movhu (d32,sp),dn -> movhu (d16,sp),dn
3165 add imm32,dn -> add imm16,dn
3166 cmp imm32,dn -> cmp imm16,dn
3167 add imm32,an -> add imm16,an
3168 cmp imm32,an -> cmp imm16,an
3169 and imm32,dn -> and imm16,dn
3170 or imm32,dn -> or imm16,dn
3171 xor imm32,dn -> xor imm16,dn
3172 btst imm32,dn -> btst imm16,dn */
3174 case 0xa0:
3175 case 0xb0:
3176 case 0xb1:
3177 case 0xb2:
3178 case 0xb3:
3179 case 0xc0:
3180 case 0xc8:
3182 case 0xd0:
3183 case 0xd8:
3184 case 0xe0:
3185 case 0xe1:
3186 case 0xe2:
3187 case 0xe3:
3188 /* cmp imm16, an zero-extends the immediate. */
3189 if (code == 0xdc
3190 && (long)value < 0)
3191 continue;
3193 /* So do sp-based offsets. */
3194 if (code >= 0xb0 && code <= 0xb3
3195 && (long)value < 0)
3196 continue;
3198 /* Note that we've changed the relocation contents, etc. */
3199 elf_section_data (sec)->relocs = internal_relocs;
3200 elf_section_data (sec)->this_hdr.contents = contents;
3201 symtab_hdr->contents = (unsigned char *) isymbuf;
3203 /* Fix the opcode. */
3204 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3205 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3207 /* Fix the relocation's type. */
3208 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3209 (ELF32_R_TYPE (irel->r_info)
3210 == (int) R_MN10300_GOTOFF32)
3211 ? R_MN10300_GOTOFF16
3212 : (ELF32_R_TYPE (irel->r_info)
3213 == (int) R_MN10300_GOT32)
3214 ? R_MN10300_GOT16
3215 : (ELF32_R_TYPE (irel->r_info)
3216 == (int) R_MN10300_GOTPC32)
3217 ? R_MN10300_GOTPC16 :
3218 R_MN10300_16);
3220 /* Delete two bytes of data. */
3221 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3222 irel->r_offset + 2, 2))
3223 goto error_return;
3225 /* That will change things, so, we should relax again.
3226 Note that this is not required, and it may be slow. */
3227 *again = TRUE;
3228 break;
3230 else if (code == 0xfe)
3232 /* add imm32,sp -> add imm16,sp */
3234 /* Note that we've changed the relocation contents, etc. */
3235 elf_section_data (sec)->relocs = internal_relocs;
3236 elf_section_data (sec)->this_hdr.contents = contents;
3237 symtab_hdr->contents = (unsigned char *) isymbuf;
3239 /* Fix the opcode. */
3240 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3241 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3243 /* Fix the relocation's type. */
3244 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3245 (ELF32_R_TYPE (irel->r_info)
3246 == (int) R_MN10300_GOT32)
3247 ? R_MN10300_GOT16
3248 : (ELF32_R_TYPE (irel->r_info)
3249 == (int) R_MN10300_GOTOFF32)
3250 ? R_MN10300_GOTOFF16
3251 : (ELF32_R_TYPE (irel->r_info)
3252 == (int) R_MN10300_GOTPC32)
3253 ? R_MN10300_GOTPC16 :
3254 R_MN10300_16);
3256 /* Delete two bytes of data. */
3257 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3258 irel->r_offset + 2, 2))
3259 goto error_return;
3261 /* That will change things, so, we should relax again.
3262 Note that this is not required, and it may be slow. */
3263 *again = TRUE;
3264 break;
3270 if (isymbuf != NULL
3271 && symtab_hdr->contents != (unsigned char *) isymbuf)
3273 if (! link_info->keep_memory)
3274 free (isymbuf);
3275 else
3277 /* Cache the symbols for elf_link_input_bfd. */
3278 symtab_hdr->contents = (unsigned char *) isymbuf;
3282 if (contents != NULL
3283 && elf_section_data (sec)->this_hdr.contents != contents)
3285 if (! link_info->keep_memory)
3286 free (contents);
3287 else
3289 /* Cache the section contents for elf_link_input_bfd. */
3290 elf_section_data (sec)->this_hdr.contents = contents;
3294 if (internal_relocs != NULL
3295 && elf_section_data (sec)->relocs != internal_relocs)
3296 free (internal_relocs);
3298 return TRUE;
3300 error_return:
3301 if (isymbuf != NULL
3302 && symtab_hdr->contents != (unsigned char *) isymbuf)
3303 free (isymbuf);
3304 if (contents != NULL
3305 && elf_section_data (section)->this_hdr.contents != contents)
3306 free (contents);
3307 if (internal_relocs != NULL
3308 && elf_section_data (section)->relocs != internal_relocs)
3309 free (internal_relocs);
3311 return FALSE;
3314 /* Compute the stack size and movm arguments for the function
3315 referred to by HASH at address ADDR in section with
3316 contents CONTENTS, store the information in the hash table. */
3317 static void
3318 compute_function_info (abfd, hash, addr, contents)
3319 bfd *abfd;
3320 struct elf32_mn10300_link_hash_entry *hash;
3321 bfd_vma addr;
3322 unsigned char *contents;
3324 unsigned char byte1, byte2;
3325 /* We only care about a very small subset of the possible prologue
3326 sequences here. Basically we look for:
3328 movm [d2,d3,a2,a3],sp (optional)
3329 add <size>,sp (optional, and only for sizes which fit in an unsigned
3330 8 bit number)
3332 If we find anything else, we quit. */
3334 /* Look for movm [regs],sp */
3335 byte1 = bfd_get_8 (abfd, contents + addr);
3336 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3338 if (byte1 == 0xcf)
3340 hash->movm_args = byte2;
3341 addr += 2;
3342 byte1 = bfd_get_8 (abfd, contents + addr);
3343 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3346 /* Now figure out how much stack space will be allocated by the movm
3347 instruction. We need this kept separate from the function's normal
3348 stack space. */
3349 if (hash->movm_args)
3351 /* Space for d2. */
3352 if (hash->movm_args & 0x80)
3353 hash->movm_stack_size += 4;
3355 /* Space for d3. */
3356 if (hash->movm_args & 0x40)
3357 hash->movm_stack_size += 4;
3359 /* Space for a2. */
3360 if (hash->movm_args & 0x20)
3361 hash->movm_stack_size += 4;
3363 /* Space for a3. */
3364 if (hash->movm_args & 0x10)
3365 hash->movm_stack_size += 4;
3367 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
3368 if (hash->movm_args & 0x08)
3369 hash->movm_stack_size += 8 * 4;
3371 if (bfd_get_mach (abfd) == bfd_mach_am33
3372 || bfd_get_mach (abfd) == bfd_mach_am33_2)
3374 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
3375 if (hash->movm_args & 0x1)
3376 hash->movm_stack_size += 6 * 4;
3378 /* exreg1 space. e4, e5, e6, e7 */
3379 if (hash->movm_args & 0x2)
3380 hash->movm_stack_size += 4 * 4;
3382 /* exreg0 space. e2, e3 */
3383 if (hash->movm_args & 0x4)
3384 hash->movm_stack_size += 2 * 4;
3388 /* Now look for the two stack adjustment variants. */
3389 if (byte1 == 0xf8 && byte2 == 0xfe)
3391 int temp = bfd_get_8 (abfd, contents + addr + 2);
3392 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
3394 hash->stack_size = -temp;
3396 else if (byte1 == 0xfa && byte2 == 0xfe)
3398 int temp = bfd_get_16 (abfd, contents + addr + 2);
3399 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
3400 temp = -temp;
3402 if (temp < 255)
3403 hash->stack_size = temp;
3406 /* If the total stack to be allocated by the call instruction is more
3407 than 255 bytes, then we can't remove the stack adjustment by using
3408 "call" (we might still be able to remove the "movm" instruction. */
3409 if (hash->stack_size + hash->movm_stack_size > 255)
3410 hash->stack_size = 0;
3412 return;
3415 /* Delete some bytes from a section while relaxing. */
3417 static bfd_boolean
3418 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
3419 bfd *abfd;
3420 asection *sec;
3421 bfd_vma addr;
3422 int count;
3424 Elf_Internal_Shdr *symtab_hdr;
3425 unsigned int sec_shndx;
3426 bfd_byte *contents;
3427 Elf_Internal_Rela *irel, *irelend;
3428 Elf_Internal_Rela *irelalign;
3429 bfd_vma toaddr;
3430 Elf_Internal_Sym *isym, *isymend;
3431 struct elf_link_hash_entry **sym_hashes;
3432 struct elf_link_hash_entry **end_hashes;
3433 unsigned int symcount;
3435 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3437 contents = elf_section_data (sec)->this_hdr.contents;
3439 /* The deletion must stop at the next ALIGN reloc for an aligment
3440 power larger than the number of bytes we are deleting. */
3442 irelalign = NULL;
3443 toaddr = sec->size;
3445 irel = elf_section_data (sec)->relocs;
3446 irelend = irel + sec->reloc_count;
3448 /* Actually delete the bytes. */
3449 memmove (contents + addr, contents + addr + count,
3450 (size_t) (toaddr - addr - count));
3451 sec->size -= count;
3453 /* Adjust all the relocs. */
3454 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3456 /* Get the new reloc address. */
3457 if ((irel->r_offset > addr
3458 && irel->r_offset < toaddr))
3459 irel->r_offset -= count;
3462 /* Adjust the local symbols defined in this section. */
3463 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3464 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3465 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3467 if (isym->st_shndx == sec_shndx
3468 && isym->st_value > addr
3469 && isym->st_value < toaddr)
3470 isym->st_value -= count;
3473 /* Now adjust the global symbols defined in this section. */
3474 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3475 - symtab_hdr->sh_info);
3476 sym_hashes = elf_sym_hashes (abfd);
3477 end_hashes = sym_hashes + symcount;
3478 for (; sym_hashes < end_hashes; sym_hashes++)
3480 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3481 if ((sym_hash->root.type == bfd_link_hash_defined
3482 || sym_hash->root.type == bfd_link_hash_defweak)
3483 && sym_hash->root.u.def.section == sec
3484 && sym_hash->root.u.def.value > addr
3485 && sym_hash->root.u.def.value < toaddr)
3487 sym_hash->root.u.def.value -= count;
3491 return TRUE;
3494 /* Return TRUE if a symbol exists at the given address, else return
3495 FALSE. */
3496 static bfd_boolean
3497 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
3498 bfd *abfd;
3499 asection *sec;
3500 Elf_Internal_Sym *isym;
3501 bfd_vma addr;
3503 Elf_Internal_Shdr *symtab_hdr;
3504 unsigned int sec_shndx;
3505 Elf_Internal_Sym *isymend;
3506 struct elf_link_hash_entry **sym_hashes;
3507 struct elf_link_hash_entry **end_hashes;
3508 unsigned int symcount;
3510 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3512 /* Examine all the symbols. */
3513 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3514 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3516 if (isym->st_shndx == sec_shndx
3517 && isym->st_value == addr)
3518 return TRUE;
3521 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3522 - symtab_hdr->sh_info);
3523 sym_hashes = elf_sym_hashes (abfd);
3524 end_hashes = sym_hashes + symcount;
3525 for (; sym_hashes < end_hashes; sym_hashes++)
3527 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3528 if ((sym_hash->root.type == bfd_link_hash_defined
3529 || sym_hash->root.type == bfd_link_hash_defweak)
3530 && sym_hash->root.u.def.section == sec
3531 && sym_hash->root.u.def.value == addr)
3532 return TRUE;
3535 return FALSE;
3538 /* This is a version of bfd_generic_get_relocated_section_contents
3539 which uses mn10300_elf_relocate_section. */
3541 static bfd_byte *
3542 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3543 data, relocatable, symbols)
3544 bfd *output_bfd;
3545 struct bfd_link_info *link_info;
3546 struct bfd_link_order *link_order;
3547 bfd_byte *data;
3548 bfd_boolean relocatable;
3549 asymbol **symbols;
3551 Elf_Internal_Shdr *symtab_hdr;
3552 asection *input_section = link_order->u.indirect.section;
3553 bfd *input_bfd = input_section->owner;
3554 asection **sections = NULL;
3555 Elf_Internal_Rela *internal_relocs = NULL;
3556 Elf_Internal_Sym *isymbuf = NULL;
3558 /* We only need to handle the case of relaxing, or of having a
3559 particular set of section contents, specially. */
3560 if (relocatable
3561 || elf_section_data (input_section)->this_hdr.contents == NULL)
3562 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3563 link_order, data,
3564 relocatable,
3565 symbols);
3567 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3569 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3570 (size_t) input_section->size);
3572 if ((input_section->flags & SEC_RELOC) != 0
3573 && input_section->reloc_count > 0)
3575 asection **secpp;
3576 Elf_Internal_Sym *isym, *isymend;
3577 bfd_size_type amt;
3579 internal_relocs = (_bfd_elf_link_read_relocs
3580 (input_bfd, input_section, (PTR) NULL,
3581 (Elf_Internal_Rela *) NULL, FALSE));
3582 if (internal_relocs == NULL)
3583 goto error_return;
3585 if (symtab_hdr->sh_info != 0)
3587 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3588 if (isymbuf == NULL)
3589 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3590 symtab_hdr->sh_info, 0,
3591 NULL, NULL, NULL);
3592 if (isymbuf == NULL)
3593 goto error_return;
3596 amt = symtab_hdr->sh_info;
3597 amt *= sizeof (asection *);
3598 sections = (asection **) bfd_malloc (amt);
3599 if (sections == NULL && amt != 0)
3600 goto error_return;
3602 isymend = isymbuf + symtab_hdr->sh_info;
3603 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3605 asection *isec;
3607 if (isym->st_shndx == SHN_UNDEF)
3608 isec = bfd_und_section_ptr;
3609 else if (isym->st_shndx == SHN_ABS)
3610 isec = bfd_abs_section_ptr;
3611 else if (isym->st_shndx == SHN_COMMON)
3612 isec = bfd_com_section_ptr;
3613 else
3614 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3616 *secpp = isec;
3619 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3620 input_section, data, internal_relocs,
3621 isymbuf, sections))
3622 goto error_return;
3624 if (sections != NULL)
3625 free (sections);
3626 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3627 free (isymbuf);
3628 if (internal_relocs != elf_section_data (input_section)->relocs)
3629 free (internal_relocs);
3632 return data;
3634 error_return:
3635 if (sections != NULL)
3636 free (sections);
3637 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3638 free (isymbuf);
3639 if (internal_relocs != NULL
3640 && internal_relocs != elf_section_data (input_section)->relocs)
3641 free (internal_relocs);
3642 return NULL;
3645 /* Assorted hash table functions. */
3647 /* Initialize an entry in the link hash table. */
3649 /* Create an entry in an MN10300 ELF linker hash table. */
3651 static struct bfd_hash_entry *
3652 elf32_mn10300_link_hash_newfunc (entry, table, string)
3653 struct bfd_hash_entry *entry;
3654 struct bfd_hash_table *table;
3655 const char *string;
3657 struct elf32_mn10300_link_hash_entry *ret =
3658 (struct elf32_mn10300_link_hash_entry *) entry;
3660 /* Allocate the structure if it has not already been allocated by a
3661 subclass. */
3662 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3663 ret = ((struct elf32_mn10300_link_hash_entry *)
3664 bfd_hash_allocate (table,
3665 sizeof (struct elf32_mn10300_link_hash_entry)));
3666 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3667 return (struct bfd_hash_entry *) ret;
3669 /* Call the allocation method of the superclass. */
3670 ret = ((struct elf32_mn10300_link_hash_entry *)
3671 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3672 table, string));
3673 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
3675 ret->direct_calls = 0;
3676 ret->stack_size = 0;
3677 ret->movm_args = 0;
3678 ret->movm_stack_size = 0;
3679 ret->flags = 0;
3682 return (struct bfd_hash_entry *) ret;
3685 /* Create an mn10300 ELF linker hash table. */
3687 static struct bfd_link_hash_table *
3688 elf32_mn10300_link_hash_table_create (abfd)
3689 bfd *abfd;
3691 struct elf32_mn10300_link_hash_table *ret;
3692 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
3694 ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3695 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
3696 return NULL;
3698 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3699 elf32_mn10300_link_hash_newfunc))
3701 free (ret);
3702 return NULL;
3705 ret->flags = 0;
3706 amt = sizeof (struct elf_link_hash_table);
3707 ret->static_hash_table
3708 = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3709 if (ret->static_hash_table == NULL)
3711 free (ret);
3712 return NULL;
3715 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3716 elf32_mn10300_link_hash_newfunc))
3718 free (ret->static_hash_table);
3719 free (ret);
3720 return NULL;
3722 return &ret->root.root;
3725 /* Free an mn10300 ELF linker hash table. */
3727 static void
3728 elf32_mn10300_link_hash_table_free (hash)
3729 struct bfd_link_hash_table *hash;
3731 struct elf32_mn10300_link_hash_table *ret
3732 = (struct elf32_mn10300_link_hash_table *) hash;
3734 _bfd_generic_link_hash_table_free
3735 ((struct bfd_link_hash_table *) ret->static_hash_table);
3736 _bfd_generic_link_hash_table_free
3737 ((struct bfd_link_hash_table *) ret);
3740 static unsigned long
3741 elf_mn10300_mach (flags)
3742 flagword flags;
3744 switch (flags & EF_MN10300_MACH)
3746 case E_MN10300_MACH_MN10300:
3747 default:
3748 return bfd_mach_mn10300;
3750 case E_MN10300_MACH_AM33:
3751 return bfd_mach_am33;
3753 case E_MN10300_MACH_AM33_2:
3754 return bfd_mach_am33_2;
3758 /* The final processing done just before writing out a MN10300 ELF object
3759 file. This gets the MN10300 architecture right based on the machine
3760 number. */
3762 void
3763 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3764 bfd *abfd;
3765 bfd_boolean linker ATTRIBUTE_UNUSED;
3767 unsigned long val;
3769 switch (bfd_get_mach (abfd))
3771 default:
3772 case bfd_mach_mn10300:
3773 val = E_MN10300_MACH_MN10300;
3774 break;
3776 case bfd_mach_am33:
3777 val = E_MN10300_MACH_AM33;
3778 break;
3780 case bfd_mach_am33_2:
3781 val = E_MN10300_MACH_AM33_2;
3782 break;
3785 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3786 elf_elfheader (abfd)->e_flags |= val;
3789 bfd_boolean
3790 _bfd_mn10300_elf_object_p (abfd)
3791 bfd *abfd;
3793 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3794 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3795 return TRUE;
3798 /* Merge backend specific data from an object file to the output
3799 object file when linking. */
3801 bfd_boolean
3802 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3803 bfd *ibfd;
3804 bfd *obfd;
3806 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3807 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3808 return TRUE;
3810 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3811 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3813 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3814 bfd_get_mach (ibfd)))
3815 return FALSE;
3818 return TRUE;
3821 #define PLT0_ENTRY_SIZE 15
3822 #define PLT_ENTRY_SIZE 20
3823 #define PIC_PLT_ENTRY_SIZE 24
3825 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
3827 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
3828 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
3829 0xf0, 0xf4, /* jmp (a0) */
3832 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
3834 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
3835 0xf0, 0xf4, /* jmp (a0) */
3836 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3837 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
3840 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
3842 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
3843 0xf0, 0xf4, /* jmp (a0) */
3844 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3845 0xf8, 0x22, 8, /* mov (8,a2),a0 */
3846 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
3847 0xf0, 0xf4, /* jmp (a0) */
3850 /* Return size of the first PLT entry. */
3851 #define elf_mn10300_sizeof_plt0(info) \
3852 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
3854 /* Return size of a PLT entry. */
3855 #define elf_mn10300_sizeof_plt(info) \
3856 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
3858 /* Return offset of the PLT0 address in an absolute PLT entry. */
3859 #define elf_mn10300_plt_plt0_offset(info) 16
3861 /* Return offset of the linker in PLT0 entry. */
3862 #define elf_mn10300_plt0_linker_offset(info) 2
3864 /* Return offset of the GOT id in PLT0 entry. */
3865 #define elf_mn10300_plt0_gotid_offset(info) 9
3867 /* Return offset of the temporary in PLT entry */
3868 #define elf_mn10300_plt_temp_offset(info) 8
3870 /* Return offset of the symbol in PLT entry. */
3871 #define elf_mn10300_plt_symbol_offset(info) 2
3873 /* Return offset of the relocation in PLT entry. */
3874 #define elf_mn10300_plt_reloc_offset(info) 11
3876 /* The name of the dynamic interpreter. This is put in the .interp
3877 section. */
3879 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
3881 /* Create dynamic sections when linking against a dynamic object. */
3883 static bfd_boolean
3884 _bfd_mn10300_elf_create_dynamic_sections (abfd, info)
3885 bfd *abfd;
3886 struct bfd_link_info *info;
3888 flagword flags;
3889 asection * s;
3890 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
3891 int ptralign = 0;
3893 switch (bed->s->arch_size)
3895 case 32:
3896 ptralign = 2;
3897 break;
3899 case 64:
3900 ptralign = 3;
3901 break;
3903 default:
3904 bfd_set_error (bfd_error_bad_value);
3905 return FALSE;
3908 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3909 .rel[a].bss sections. */
3911 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3912 | SEC_LINKER_CREATED);
3914 s = bfd_make_section_with_flags (abfd,
3915 (bed->default_use_rela_p
3916 ? ".rela.plt" : ".rel.plt"),
3917 flags | SEC_READONLY);
3918 if (s == NULL
3919 || ! bfd_set_section_alignment (abfd, s, ptralign))
3920 return FALSE;
3922 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
3923 return FALSE;
3926 const char * secname;
3927 char * relname;
3928 flagword secflags;
3929 asection * sec;
3931 for (sec = abfd->sections; sec; sec = sec->next)
3933 secflags = bfd_get_section_flags (abfd, sec);
3934 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3935 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3936 continue;
3938 secname = bfd_get_section_name (abfd, sec);
3939 relname = (char *) bfd_malloc (strlen (secname) + 6);
3940 strcpy (relname, ".rela");
3941 strcat (relname, secname);
3943 s = bfd_make_section_with_flags (abfd, relname,
3944 flags | SEC_READONLY);
3945 if (s == NULL
3946 || ! bfd_set_section_alignment (abfd, s, ptralign))
3947 return FALSE;
3951 if (bed->want_dynbss)
3953 /* The .dynbss section is a place to put symbols which are defined
3954 by dynamic objects, are referenced by regular objects, and are
3955 not functions. We must allocate space for them in the process
3956 image and use a R_*_COPY reloc to tell the dynamic linker to
3957 initialize them at run time. The linker script puts the .dynbss
3958 section into the .bss section of the final image. */
3959 s = bfd_make_section_with_flags (abfd, ".dynbss",
3960 SEC_ALLOC | SEC_LINKER_CREATED);
3961 if (s == NULL)
3962 return FALSE;
3964 /* The .rel[a].bss section holds copy relocs. This section is not
3965 normally needed. We need to create it here, though, so that the
3966 linker will map it to an output section. We can't just create it
3967 only if we need it, because we will not know whether we need it
3968 until we have seen all the input files, and the first time the
3969 main linker code calls BFD after examining all the input files
3970 (size_dynamic_sections) the input sections have already been
3971 mapped to the output sections. If the section turns out not to
3972 be needed, we can discard it later. We will never need this
3973 section when generating a shared object, since they do not use
3974 copy relocs. */
3975 if (! info->shared)
3977 s = bfd_make_section_with_flags (abfd,
3978 (bed->default_use_rela_p
3979 ? ".rela.bss" : ".rel.bss"),
3980 flags | SEC_READONLY);
3981 if (s == NULL
3982 || ! bfd_set_section_alignment (abfd, s, ptralign))
3983 return FALSE;
3987 return TRUE;
3990 /* Adjust a symbol defined by a dynamic object and referenced by a
3991 regular object. The current definition is in some section of the
3992 dynamic object, but we're not including those sections. We have to
3993 change the definition to something the rest of the link can
3994 understand. */
3996 static bfd_boolean
3997 _bfd_mn10300_elf_adjust_dynamic_symbol (info, h)
3998 struct bfd_link_info * info;
3999 struct elf_link_hash_entry * h;
4001 bfd * dynobj;
4002 asection * s;
4003 unsigned int power_of_two;
4005 dynobj = elf_hash_table (info)->dynobj;
4007 /* Make sure we know what is going on here. */
4008 BFD_ASSERT (dynobj != NULL
4009 && (h->needs_plt
4010 || h->u.weakdef != NULL
4011 || (h->def_dynamic
4012 && h->ref_regular
4013 && !h->def_regular)));
4015 /* If this is a function, put it in the procedure linkage table. We
4016 will fill in the contents of the procedure linkage table later,
4017 when we know the address of the .got section. */
4018 if (h->type == STT_FUNC
4019 || h->needs_plt)
4021 if (! info->shared
4022 && !h->def_dynamic
4023 && !h->ref_dynamic)
4025 /* This case can occur if we saw a PLT reloc in an input
4026 file, but the symbol was never referred to by a dynamic
4027 object. In such a case, we don't actually need to build
4028 a procedure linkage table, and we can just do a REL32
4029 reloc instead. */
4030 BFD_ASSERT (h->needs_plt);
4031 return TRUE;
4034 /* Make sure this symbol is output as a dynamic symbol. */
4035 if (h->dynindx == -1)
4037 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4038 return FALSE;
4041 s = bfd_get_section_by_name (dynobj, ".plt");
4042 BFD_ASSERT (s != NULL);
4044 /* If this is the first .plt entry, make room for the special
4045 first entry. */
4046 if (s->size == 0)
4047 s->size += elf_mn10300_sizeof_plt0 (info);
4049 /* If this symbol is not defined in a regular file, and we are
4050 not generating a shared library, then set the symbol to this
4051 location in the .plt. This is required to make function
4052 pointers compare as equal between the normal executable and
4053 the shared library. */
4054 if (! info->shared
4055 && !h->def_regular)
4057 h->root.u.def.section = s;
4058 h->root.u.def.value = s->size;
4061 h->plt.offset = s->size;
4063 /* Make room for this entry. */
4064 s->size += elf_mn10300_sizeof_plt (info);
4066 /* We also need to make an entry in the .got.plt section, which
4067 will be placed in the .got section by the linker script. */
4069 s = bfd_get_section_by_name (dynobj, ".got.plt");
4070 BFD_ASSERT (s != NULL);
4071 s->size += 4;
4073 /* We also need to make an entry in the .rela.plt section. */
4075 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4076 BFD_ASSERT (s != NULL);
4077 s->size += sizeof (Elf32_External_Rela);
4079 return TRUE;
4082 /* If this is a weak symbol, and there is a real definition, the
4083 processor independent code will have arranged for us to see the
4084 real definition first, and we can just use the same value. */
4085 if (h->u.weakdef != NULL)
4087 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4088 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4089 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4090 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4091 return TRUE;
4094 /* This is a reference to a symbol defined by a dynamic object which
4095 is not a function. */
4097 /* If we are creating a shared library, we must presume that the
4098 only references to the symbol are via the global offset table.
4099 For such cases we need not do anything here; the relocations will
4100 be handled correctly by relocate_section. */
4101 if (info->shared)
4102 return TRUE;
4104 /* If there are no references to this symbol that do not use the
4105 GOT, we don't need to generate a copy reloc. */
4106 if (!h->non_got_ref)
4107 return TRUE;
4109 if (h->size == 0)
4111 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4112 h->root.root.string);
4113 return TRUE;
4116 /* We must allocate the symbol in our .dynbss section, which will
4117 become part of the .bss section of the executable. There will be
4118 an entry for this symbol in the .dynsym section. The dynamic
4119 object will contain position independent code, so all references
4120 from the dynamic object to this symbol will go through the global
4121 offset table. The dynamic linker will use the .dynsym entry to
4122 determine the address it must put in the global offset table, so
4123 both the dynamic object and the regular object will refer to the
4124 same memory location for the variable. */
4126 s = bfd_get_section_by_name (dynobj, ".dynbss");
4127 BFD_ASSERT (s != NULL);
4129 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4130 copy the initial value out of the dynamic object and into the
4131 runtime process image. We need to remember the offset into the
4132 .rela.bss section we are going to use. */
4133 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4135 asection * srel;
4137 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4138 BFD_ASSERT (srel != NULL);
4139 srel->size += sizeof (Elf32_External_Rela);
4140 h->needs_copy = 1;
4143 /* We need to figure out the alignment required for this symbol. I
4144 have no idea how ELF linkers handle this. */
4145 power_of_two = bfd_log2 (h->size);
4146 if (power_of_two > 3)
4147 power_of_two = 3;
4149 /* Apply the required alignment. */
4150 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4151 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4153 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4154 return FALSE;
4157 /* Define the symbol as being at this point in the section. */
4158 h->root.u.def.section = s;
4159 h->root.u.def.value = s->size;
4161 /* Increment the section size to make room for the symbol. */
4162 s->size += h->size;
4164 return TRUE;
4167 /* Set the sizes of the dynamic sections. */
4169 static bfd_boolean
4170 _bfd_mn10300_elf_size_dynamic_sections (output_bfd, info)
4171 bfd * output_bfd;
4172 struct bfd_link_info * info;
4174 bfd * dynobj;
4175 asection * s;
4176 bfd_boolean plt;
4177 bfd_boolean relocs;
4178 bfd_boolean reltext;
4180 dynobj = elf_hash_table (info)->dynobj;
4181 BFD_ASSERT (dynobj != NULL);
4183 if (elf_hash_table (info)->dynamic_sections_created)
4185 /* Set the contents of the .interp section to the interpreter. */
4186 if (info->executable)
4188 s = bfd_get_section_by_name (dynobj, ".interp");
4189 BFD_ASSERT (s != NULL);
4190 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4191 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4194 else
4196 /* We may have created entries in the .rela.got section.
4197 However, if we are not creating the dynamic sections, we will
4198 not actually use these entries. Reset the size of .rela.got,
4199 which will cause it to get stripped from the output file
4200 below. */
4201 s = bfd_get_section_by_name (dynobj, ".rela.got");
4202 if (s != NULL)
4203 s->size = 0;
4206 /* The check_relocs and adjust_dynamic_symbol entry points have
4207 determined the sizes of the various dynamic sections. Allocate
4208 memory for them. */
4209 plt = FALSE;
4210 relocs = FALSE;
4211 reltext = FALSE;
4212 for (s = dynobj->sections; s != NULL; s = s->next)
4214 const char * name;
4216 if ((s->flags & SEC_LINKER_CREATED) == 0)
4217 continue;
4219 /* It's OK to base decisions on the section name, because none
4220 of the dynobj section names depend upon the input files. */
4221 name = bfd_get_section_name (dynobj, s);
4223 if (strcmp (name, ".plt") == 0)
4225 /* Remember whether there is a PLT. */
4226 plt = s->size != 0;
4228 else if (strncmp (name, ".rela", 5) == 0)
4230 if (s->size != 0)
4232 asection * target;
4234 /* Remember whether there are any reloc sections other
4235 than .rela.plt. */
4236 if (strcmp (name, ".rela.plt") != 0)
4238 const char * outname;
4240 relocs = TRUE;
4242 /* If this relocation section applies to a read only
4243 section, then we probably need a DT_TEXTREL
4244 entry. The entries in the .rela.plt section
4245 really apply to the .got section, which we
4246 created ourselves and so know is not readonly. */
4247 outname = bfd_get_section_name (output_bfd,
4248 s->output_section);
4249 target = bfd_get_section_by_name (output_bfd, outname + 5);
4250 if (target != NULL
4251 && (target->flags & SEC_READONLY) != 0
4252 && (target->flags & SEC_ALLOC) != 0)
4253 reltext = TRUE;
4256 /* We use the reloc_count field as a counter if we need
4257 to copy relocs into the output file. */
4258 s->reloc_count = 0;
4261 else if (strncmp (name, ".got", 4) != 0
4262 && strcmp (name, ".dynbss") != 0)
4263 /* It's not one of our sections, so don't allocate space. */
4264 continue;
4266 if (s->size == 0)
4268 /* If we don't need this section, strip it from the
4269 output file. This is mostly to handle .rela.bss and
4270 .rela.plt. We must create both sections in
4271 create_dynamic_sections, because they must be created
4272 before the linker maps input sections to output
4273 sections. The linker does that before
4274 adjust_dynamic_symbol is called, and it is that
4275 function which decides whether anything needs to go
4276 into these sections. */
4277 s->flags |= SEC_EXCLUDE;
4278 continue;
4281 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4282 continue;
4284 /* Allocate memory for the section contents. We use bfd_zalloc
4285 here in case unused entries are not reclaimed before the
4286 section's contents are written out. This should not happen,
4287 but this way if it does, we get a R_MN10300_NONE reloc
4288 instead of garbage. */
4289 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4290 if (s->contents == NULL)
4291 return FALSE;
4294 if (elf_hash_table (info)->dynamic_sections_created)
4296 /* Add some entries to the .dynamic section. We fill in the
4297 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4298 but we must add the entries now so that we get the correct
4299 size for the .dynamic section. The DT_DEBUG entry is filled
4300 in by the dynamic linker and used by the debugger. */
4301 if (! info->shared)
4303 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4304 return FALSE;
4307 if (plt)
4309 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4310 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4311 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4312 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4313 return FALSE;
4316 if (relocs)
4318 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4319 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4320 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4321 sizeof (Elf32_External_Rela)))
4322 return FALSE;
4325 if (reltext)
4327 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4328 return FALSE;
4332 return TRUE;
4335 /* Finish up dynamic symbol handling. We set the contents of various
4336 dynamic sections here. */
4338 static bfd_boolean
4339 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4340 bfd * output_bfd;
4341 struct bfd_link_info * info;
4342 struct elf_link_hash_entry * h;
4343 Elf_Internal_Sym * sym;
4345 bfd * dynobj;
4347 dynobj = elf_hash_table (info)->dynobj;
4349 if (h->plt.offset != (bfd_vma) -1)
4351 asection * splt;
4352 asection * sgot;
4353 asection * srel;
4354 bfd_vma plt_index;
4355 bfd_vma got_offset;
4356 Elf_Internal_Rela rel;
4358 /* This symbol has an entry in the procedure linkage table. Set
4359 it up. */
4361 BFD_ASSERT (h->dynindx != -1);
4363 splt = bfd_get_section_by_name (dynobj, ".plt");
4364 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4365 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4366 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4368 /* Get the index in the procedure linkage table which
4369 corresponds to this symbol. This is the index of this symbol
4370 in all the symbols for which we are making plt entries. The
4371 first entry in the procedure linkage table is reserved. */
4372 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4373 / elf_mn10300_sizeof_plt (info));
4375 /* Get the offset into the .got table of the entry that
4376 corresponds to this function. Each .got entry is 4 bytes.
4377 The first three are reserved. */
4378 got_offset = (plt_index + 3) * 4;
4380 /* Fill in the entry in the procedure linkage table. */
4381 if (! info->shared)
4383 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4384 elf_mn10300_sizeof_plt (info));
4385 bfd_put_32 (output_bfd,
4386 (sgot->output_section->vma
4387 + sgot->output_offset
4388 + got_offset),
4389 (splt->contents + h->plt.offset
4390 + elf_mn10300_plt_symbol_offset (info)));
4392 bfd_put_32 (output_bfd,
4393 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4394 (splt->contents + h->plt.offset
4395 + elf_mn10300_plt_plt0_offset (info)));
4397 else
4399 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4400 elf_mn10300_sizeof_plt (info));
4402 bfd_put_32 (output_bfd, got_offset,
4403 (splt->contents + h->plt.offset
4404 + elf_mn10300_plt_symbol_offset (info)));
4407 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4408 (splt->contents + h->plt.offset
4409 + elf_mn10300_plt_reloc_offset (info)));
4411 /* Fill in the entry in the global offset table. */
4412 bfd_put_32 (output_bfd,
4413 (splt->output_section->vma
4414 + splt->output_offset
4415 + h->plt.offset
4416 + elf_mn10300_plt_temp_offset (info)),
4417 sgot->contents + got_offset);
4419 /* Fill in the entry in the .rela.plt section. */
4420 rel.r_offset = (sgot->output_section->vma
4421 + sgot->output_offset
4422 + got_offset);
4423 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4424 rel.r_addend = 0;
4425 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4426 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4427 + plt_index));
4429 if (!h->def_regular)
4430 /* Mark the symbol as undefined, rather than as defined in
4431 the .plt section. Leave the value alone. */
4432 sym->st_shndx = SHN_UNDEF;
4435 if (h->got.offset != (bfd_vma) -1)
4437 asection * sgot;
4438 asection * srel;
4439 Elf_Internal_Rela rel;
4441 /* This symbol has an entry in the global offset table. Set it up. */
4443 sgot = bfd_get_section_by_name (dynobj, ".got");
4444 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4445 BFD_ASSERT (sgot != NULL && srel != NULL);
4447 rel.r_offset = (sgot->output_section->vma
4448 + sgot->output_offset
4449 + (h->got.offset &~ 1));
4451 /* If this is a -Bsymbolic link, and the symbol is defined
4452 locally, we just want to emit a RELATIVE reloc. Likewise if
4453 the symbol was forced to be local because of a version file.
4454 The entry in the global offset table will already have been
4455 initialized in the relocate_section function. */
4456 if (info->shared
4457 && (info->symbolic || h->dynindx == -1)
4458 && h->def_regular)
4460 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4461 rel.r_addend = (h->root.u.def.value
4462 + h->root.u.def.section->output_section->vma
4463 + h->root.u.def.section->output_offset);
4465 else
4467 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4468 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4469 rel.r_addend = 0;
4472 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4473 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4474 + srel->reloc_count));
4475 ++ srel->reloc_count;
4478 if (h->needs_copy)
4480 asection * s;
4481 Elf_Internal_Rela rel;
4483 /* This symbol needs a copy reloc. Set it up. */
4484 BFD_ASSERT (h->dynindx != -1
4485 && (h->root.type == bfd_link_hash_defined
4486 || h->root.type == bfd_link_hash_defweak));
4488 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4489 ".rela.bss");
4490 BFD_ASSERT (s != NULL);
4492 rel.r_offset = (h->root.u.def.value
4493 + h->root.u.def.section->output_section->vma
4494 + h->root.u.def.section->output_offset);
4495 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4496 rel.r_addend = 0;
4497 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4498 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4499 + s->reloc_count));
4500 ++ s->reloc_count;
4503 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4504 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4505 || h == elf_hash_table (info)->hgot)
4506 sym->st_shndx = SHN_ABS;
4508 return TRUE;
4511 /* Finish up the dynamic sections. */
4513 static bfd_boolean
4514 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd, info)
4515 bfd * output_bfd;
4516 struct bfd_link_info * info;
4518 bfd * dynobj;
4519 asection * sgot;
4520 asection * sdyn;
4522 dynobj = elf_hash_table (info)->dynobj;
4524 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4525 BFD_ASSERT (sgot != NULL);
4526 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4528 if (elf_hash_table (info)->dynamic_sections_created)
4530 asection * splt;
4531 Elf32_External_Dyn * dyncon;
4532 Elf32_External_Dyn * dynconend;
4534 BFD_ASSERT (sdyn != NULL);
4536 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4537 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4539 for (; dyncon < dynconend; dyncon++)
4541 Elf_Internal_Dyn dyn;
4542 const char * name;
4543 asection * s;
4545 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4547 switch (dyn.d_tag)
4549 default:
4550 break;
4552 case DT_PLTGOT:
4553 name = ".got";
4554 goto get_vma;
4556 case DT_JMPREL:
4557 name = ".rela.plt";
4558 get_vma:
4559 s = bfd_get_section_by_name (output_bfd, name);
4560 BFD_ASSERT (s != NULL);
4561 dyn.d_un.d_ptr = s->vma;
4562 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4563 break;
4565 case DT_PLTRELSZ:
4566 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4567 BFD_ASSERT (s != NULL);
4568 dyn.d_un.d_val = s->size;
4569 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4570 break;
4572 case DT_RELASZ:
4573 /* My reading of the SVR4 ABI indicates that the
4574 procedure linkage table relocs (DT_JMPREL) should be
4575 included in the overall relocs (DT_RELA). This is
4576 what Solaris does. However, UnixWare can not handle
4577 that case. Therefore, we override the DT_RELASZ entry
4578 here to make it not include the JMPREL relocs. Since
4579 the linker script arranges for .rela.plt to follow all
4580 other relocation sections, we don't have to worry
4581 about changing the DT_RELA entry. */
4582 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4583 if (s != NULL)
4584 dyn.d_un.d_val -= s->size;
4585 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4586 break;
4590 /* Fill in the first entry in the procedure linkage table. */
4591 splt = bfd_get_section_by_name (dynobj, ".plt");
4592 if (splt && splt->size > 0)
4594 if (info->shared)
4596 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4597 elf_mn10300_sizeof_plt (info));
4599 else
4601 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4602 bfd_put_32 (output_bfd,
4603 sgot->output_section->vma + sgot->output_offset + 4,
4604 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4605 bfd_put_32 (output_bfd,
4606 sgot->output_section->vma + sgot->output_offset + 8,
4607 splt->contents + elf_mn10300_plt0_linker_offset (info));
4610 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4611 really seem like the right value. */
4612 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4616 /* Fill in the first three entries in the global offset table. */
4617 if (sgot->size > 0)
4619 if (sdyn == NULL)
4620 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4621 else
4622 bfd_put_32 (output_bfd,
4623 sdyn->output_section->vma + sdyn->output_offset,
4624 sgot->contents);
4625 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4626 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4629 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4631 return TRUE;
4634 /* Classify relocation types, such that combreloc can sort them
4635 properly. */
4637 static enum elf_reloc_type_class
4638 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4640 switch ((int) ELF32_R_TYPE (rela->r_info))
4642 case R_MN10300_RELATIVE:
4643 return reloc_class_relative;
4644 case R_MN10300_JMP_SLOT:
4645 return reloc_class_plt;
4646 case R_MN10300_COPY:
4647 return reloc_class_copy;
4648 default:
4649 return reloc_class_normal;
4653 #ifndef ELF_ARCH
4654 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4655 #define TARGET_LITTLE_NAME "elf32-mn10300"
4656 #define ELF_ARCH bfd_arch_mn10300
4657 #define ELF_MACHINE_CODE EM_MN10300
4658 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4659 #define ELF_MAXPAGESIZE 0x1000
4660 #endif
4662 #define elf_info_to_howto mn10300_info_to_howto
4663 #define elf_info_to_howto_rel 0
4664 #define elf_backend_can_gc_sections 1
4665 #define elf_backend_rela_normal 1
4666 #define elf_backend_check_relocs mn10300_elf_check_relocs
4667 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4668 #define elf_backend_relocate_section mn10300_elf_relocate_section
4669 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4670 #define bfd_elf32_bfd_get_relocated_section_contents \
4671 mn10300_elf_get_relocated_section_contents
4672 #define bfd_elf32_bfd_link_hash_table_create \
4673 elf32_mn10300_link_hash_table_create
4674 #define bfd_elf32_bfd_link_hash_table_free \
4675 elf32_mn10300_link_hash_table_free
4677 #ifndef elf_symbol_leading_char
4678 #define elf_symbol_leading_char '_'
4679 #endif
4681 /* So we can set bits in e_flags. */
4682 #define elf_backend_final_write_processing \
4683 _bfd_mn10300_elf_final_write_processing
4684 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4686 #define bfd_elf32_bfd_merge_private_bfd_data \
4687 _bfd_mn10300_elf_merge_private_bfd_data
4689 #define elf_backend_can_gc_sections 1
4690 #define elf_backend_create_dynamic_sections \
4691 _bfd_mn10300_elf_create_dynamic_sections
4692 #define elf_backend_adjust_dynamic_symbol \
4693 _bfd_mn10300_elf_adjust_dynamic_symbol
4694 #define elf_backend_size_dynamic_sections \
4695 _bfd_mn10300_elf_size_dynamic_sections
4696 #define elf_backend_finish_dynamic_symbol \
4697 _bfd_mn10300_elf_finish_dynamic_symbol
4698 #define elf_backend_finish_dynamic_sections \
4699 _bfd_mn10300_elf_finish_dynamic_sections
4701 #define elf_backend_reloc_type_class \
4702 _bfd_mn10300_elf_reloc_type_class
4704 #define elf_backend_want_got_plt 1
4705 #define elf_backend_plt_readonly 1
4706 #define elf_backend_want_plt_sym 0
4707 #define elf_backend_got_header_size 12
4709 #include "elf32-target.h"