merge from gcc
[binutils.git] / bfd / elf-m10300.c
blob86ffb78a36c169edf89ef0200ef25f1eeb4706b7
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
586 && !_bfd_elf_define_linkage_sym (abfd, info, s,
587 "_PROCEDURE_LINKAGE_TABLE_"))
588 return FALSE;
590 s = bfd_make_section_with_flags (abfd, ".got", flags);
591 if (s == NULL
592 || ! bfd_set_section_alignment (abfd, s, ptralign))
593 return FALSE;
595 if (bed->want_got_plt)
597 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
598 if (s == NULL
599 || ! bfd_set_section_alignment (abfd, s, ptralign))
600 return FALSE;
603 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
604 (or .got.plt) section. We don't do this in the linker script
605 because we don't want to define the symbol if we are not creating
606 a global offset table. */
607 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
608 elf_hash_table (info)->hgot = h;
609 if (h == NULL)
610 return FALSE;
612 /* The first bit of the global offset table is the header. */
613 s->size += bed->got_header_size;
615 return TRUE;
618 static reloc_howto_type *
619 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
620 bfd *abfd ATTRIBUTE_UNUSED;
621 bfd_reloc_code_real_type code;
623 unsigned int i;
625 for (i = 0;
626 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
627 i++)
629 if (mn10300_reloc_map[i].bfd_reloc_val == code)
630 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
633 return NULL;
636 /* Set the howto pointer for an MN10300 ELF reloc. */
638 static void
639 mn10300_info_to_howto (abfd, cache_ptr, dst)
640 bfd *abfd ATTRIBUTE_UNUSED;
641 arelent *cache_ptr;
642 Elf_Internal_Rela *dst;
644 unsigned int r_type;
646 r_type = ELF32_R_TYPE (dst->r_info);
647 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
648 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
651 /* Look through the relocs for a section during the first phase.
652 Since we don't do .gots or .plts, we just need to consider the
653 virtual table relocs for gc. */
655 static bfd_boolean
656 mn10300_elf_check_relocs (abfd, info, sec, relocs)
657 bfd *abfd;
658 struct bfd_link_info *info;
659 asection *sec;
660 const Elf_Internal_Rela *relocs;
662 Elf_Internal_Shdr *symtab_hdr;
663 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
664 const Elf_Internal_Rela *rel;
665 const Elf_Internal_Rela *rel_end;
666 bfd * dynobj;
667 bfd_vma * local_got_offsets;
668 asection * sgot;
669 asection * srelgot;
670 asection * sreloc;
672 sgot = NULL;
673 srelgot = NULL;
674 sreloc = NULL;
676 if (info->relocatable)
677 return TRUE;
679 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
680 sym_hashes = elf_sym_hashes (abfd);
681 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
682 if (!elf_bad_symtab (abfd))
683 sym_hashes_end -= symtab_hdr->sh_info;
685 dynobj = elf_hash_table (info)->dynobj;
686 local_got_offsets = elf_local_got_offsets (abfd);
687 rel_end = relocs + sec->reloc_count;
688 for (rel = relocs; rel < rel_end; rel++)
690 struct elf_link_hash_entry *h;
691 unsigned long r_symndx;
693 r_symndx = ELF32_R_SYM (rel->r_info);
694 if (r_symndx < symtab_hdr->sh_info)
695 h = NULL;
696 else
698 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
699 while (h->root.type == bfd_link_hash_indirect
700 || h->root.type == bfd_link_hash_warning)
701 h = (struct elf_link_hash_entry *) h->root.u.i.link;
704 /* Some relocs require a global offset table. */
705 if (dynobj == NULL)
707 switch (ELF32_R_TYPE (rel->r_info))
709 case R_MN10300_GOT32:
710 case R_MN10300_GOT24:
711 case R_MN10300_GOT16:
712 case R_MN10300_GOTOFF32:
713 case R_MN10300_GOTOFF24:
714 case R_MN10300_GOTOFF16:
715 case R_MN10300_GOTPC32:
716 case R_MN10300_GOTPC16:
717 elf_hash_table (info)->dynobj = dynobj = abfd;
718 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
719 return FALSE;
720 break;
722 default:
723 break;
727 switch (ELF32_R_TYPE (rel->r_info))
729 /* This relocation describes the C++ object vtable hierarchy.
730 Reconstruct it for later use during GC. */
731 case R_MN10300_GNU_VTINHERIT:
732 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
733 return FALSE;
734 break;
736 /* This relocation describes which C++ vtable entries are actually
737 used. Record for later use during GC. */
738 case R_MN10300_GNU_VTENTRY:
739 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
740 return FALSE;
741 break;
742 case R_MN10300_GOT32:
743 case R_MN10300_GOT24:
744 case R_MN10300_GOT16:
745 /* This symbol requires a global offset table entry. */
747 if (sgot == NULL)
749 sgot = bfd_get_section_by_name (dynobj, ".got");
750 BFD_ASSERT (sgot != NULL);
753 if (srelgot == NULL
754 && (h != NULL || info->shared))
756 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
757 if (srelgot == NULL)
759 srelgot = bfd_make_section_with_flags (dynobj,
760 ".rela.got",
761 (SEC_ALLOC
762 | SEC_LOAD
763 | SEC_HAS_CONTENTS
764 | SEC_IN_MEMORY
765 | SEC_LINKER_CREATED
766 | SEC_READONLY));
767 if (srelgot == NULL
768 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
769 return FALSE;
773 if (h != NULL)
775 if (h->got.offset != (bfd_vma) -1)
776 /* We have already allocated space in the .got. */
777 break;
779 h->got.offset = sgot->size;
781 /* Make sure this symbol is output as a dynamic symbol. */
782 if (h->dynindx == -1)
784 if (! bfd_elf_link_record_dynamic_symbol (info, h))
785 return FALSE;
788 srelgot->size += sizeof (Elf32_External_Rela);
790 else
792 /* This is a global offset table entry for a local
793 symbol. */
794 if (local_got_offsets == NULL)
796 size_t size;
797 unsigned int i;
799 size = symtab_hdr->sh_info * sizeof (bfd_vma);
800 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
802 if (local_got_offsets == NULL)
803 return FALSE;
804 elf_local_got_offsets (abfd) = local_got_offsets;
806 for (i = 0; i < symtab_hdr->sh_info; i++)
807 local_got_offsets[i] = (bfd_vma) -1;
810 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
811 /* We have already allocated space in the .got. */
812 break;
814 local_got_offsets[r_symndx] = sgot->size;
816 if (info->shared)
817 /* If we are generating a shared object, we need to
818 output a R_MN10300_RELATIVE reloc so that the dynamic
819 linker can adjust this GOT entry. */
820 srelgot->size += sizeof (Elf32_External_Rela);
823 sgot->size += 4;
825 break;
827 case R_MN10300_PLT32:
828 case R_MN10300_PLT16:
829 /* This symbol requires a procedure linkage table entry. We
830 actually build the entry in adjust_dynamic_symbol,
831 because this might be a case of linking PIC code which is
832 never referenced by a dynamic object, in which case we
833 don't need to generate a procedure linkage table entry
834 after all. */
836 /* If this is a local symbol, we resolve it directly without
837 creating a procedure linkage table entry. */
838 if (h == NULL)
839 continue;
841 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
842 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
843 break;
845 h->needs_plt = 1;
847 break;
849 case R_MN10300_24:
850 case R_MN10300_16:
851 case R_MN10300_8:
852 case R_MN10300_PCREL32:
853 case R_MN10300_PCREL16:
854 case R_MN10300_PCREL8:
855 if (h != NULL)
856 h->non_got_ref = 1;
857 break;
859 case R_MN10300_32:
860 if (h != NULL)
861 h->non_got_ref = 1;
863 /* If we are creating a shared library, then we need to copy
864 the reloc into the shared library. */
865 if (info->shared
866 && (sec->flags & SEC_ALLOC) != 0)
868 /* When creating a shared object, we must copy these
869 reloc types into the output file. We create a reloc
870 section in dynobj and make room for this reloc. */
871 if (sreloc == NULL)
873 const char * name;
875 name = (bfd_elf_string_from_elf_section
876 (abfd,
877 elf_elfheader (abfd)->e_shstrndx,
878 elf_section_data (sec)->rel_hdr.sh_name));
879 if (name == NULL)
880 return FALSE;
882 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
883 && strcmp (bfd_get_section_name (abfd, sec),
884 name + 5) == 0);
886 sreloc = bfd_get_section_by_name (dynobj, name);
887 if (sreloc == NULL)
889 flagword flags;
891 flags = (SEC_HAS_CONTENTS | SEC_READONLY
892 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
893 if ((sec->flags & SEC_ALLOC) != 0)
894 flags |= SEC_ALLOC | SEC_LOAD;
895 sreloc = bfd_make_section_with_flags (dynobj,
896 name,
897 flags);
898 if (sreloc == NULL
899 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
900 return FALSE;
904 sreloc->size += sizeof (Elf32_External_Rela);
907 break;
911 return TRUE;
914 /* Return the section that should be marked against GC for a given
915 relocation. */
917 static asection *
918 mn10300_elf_gc_mark_hook (sec, info, rel, h, sym)
919 asection *sec;
920 struct bfd_link_info *info ATTRIBUTE_UNUSED;
921 Elf_Internal_Rela *rel;
922 struct elf_link_hash_entry *h;
923 Elf_Internal_Sym *sym;
925 if (h != NULL)
927 switch (ELF32_R_TYPE (rel->r_info))
929 case R_MN10300_GNU_VTINHERIT:
930 case R_MN10300_GNU_VTENTRY:
931 break;
933 default:
934 switch (h->root.type)
936 case bfd_link_hash_defined:
937 case bfd_link_hash_defweak:
938 return h->root.u.def.section;
940 case bfd_link_hash_common:
941 return h->root.u.c.p->section;
943 default:
944 break;
948 else
949 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
951 return NULL;
954 /* Perform a relocation as part of a final link. */
955 static bfd_reloc_status_type
956 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
957 input_section, contents, offset, value,
958 addend, h, symndx, info, sym_sec, is_local)
959 reloc_howto_type *howto;
960 bfd *input_bfd;
961 bfd *output_bfd ATTRIBUTE_UNUSED;
962 asection *input_section;
963 bfd_byte *contents;
964 bfd_vma offset;
965 bfd_vma value;
966 bfd_vma addend;
967 struct elf_link_hash_entry * h;
968 unsigned long symndx;
969 struct bfd_link_info *info;
970 asection *sym_sec ATTRIBUTE_UNUSED;
971 int is_local ATTRIBUTE_UNUSED;
973 unsigned long r_type = howto->type;
974 bfd_byte *hit_data = contents + offset;
975 bfd * dynobj;
976 bfd_vma * local_got_offsets;
977 asection * sgot;
978 asection * splt;
979 asection * sreloc;
981 dynobj = elf_hash_table (info)->dynobj;
982 local_got_offsets = elf_local_got_offsets (input_bfd);
984 sgot = NULL;
985 splt = NULL;
986 sreloc = NULL;
988 switch (r_type)
990 case R_MN10300_24:
991 case R_MN10300_16:
992 case R_MN10300_8:
993 case R_MN10300_PCREL8:
994 case R_MN10300_PCREL16:
995 case R_MN10300_PCREL32:
996 case R_MN10300_GOTOFF32:
997 case R_MN10300_GOTOFF24:
998 case R_MN10300_GOTOFF16:
999 if (info->shared
1000 && (input_section->flags & SEC_ALLOC) != 0
1001 && h != NULL
1002 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1003 return bfd_reloc_dangerous;
1006 switch (r_type)
1008 case R_MN10300_NONE:
1009 return bfd_reloc_ok;
1011 case R_MN10300_32:
1012 if (info->shared
1013 && (input_section->flags & SEC_ALLOC) != 0)
1015 Elf_Internal_Rela outrel;
1016 bfd_boolean skip, relocate;
1018 /* When generating a shared object, these relocations are
1019 copied into the output file to be resolved at run
1020 time. */
1021 if (sreloc == NULL)
1023 const char * name;
1025 name = (bfd_elf_string_from_elf_section
1026 (input_bfd,
1027 elf_elfheader (input_bfd)->e_shstrndx,
1028 elf_section_data (input_section)->rel_hdr.sh_name));
1029 if (name == NULL)
1030 return FALSE;
1032 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1033 && strcmp (bfd_get_section_name (input_bfd,
1034 input_section),
1035 name + 5) == 0);
1037 sreloc = bfd_get_section_by_name (dynobj, name);
1038 BFD_ASSERT (sreloc != NULL);
1041 skip = FALSE;
1043 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1044 input_section, offset);
1045 if (outrel.r_offset == (bfd_vma) -1)
1046 skip = TRUE;
1048 outrel.r_offset += (input_section->output_section->vma
1049 + input_section->output_offset);
1051 if (skip)
1053 memset (&outrel, 0, sizeof outrel);
1054 relocate = FALSE;
1056 else
1058 /* h->dynindx may be -1 if this symbol was marked to
1059 become local. */
1060 if (h == NULL
1061 || SYMBOL_REFERENCES_LOCAL (info, h))
1063 relocate = TRUE;
1064 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1065 outrel.r_addend = value + addend;
1067 else
1069 BFD_ASSERT (h->dynindx != -1);
1070 relocate = FALSE;
1071 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1072 outrel.r_addend = value + addend;
1076 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1077 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1078 + sreloc->reloc_count));
1079 ++sreloc->reloc_count;
1081 /* If this reloc is against an external symbol, we do
1082 not want to fiddle with the addend. Otherwise, we
1083 need to include the symbol value so that it becomes
1084 an addend for the dynamic reloc. */
1085 if (! relocate)
1086 return bfd_reloc_ok;
1088 value += addend;
1089 bfd_put_32 (input_bfd, value, hit_data);
1090 return bfd_reloc_ok;
1092 case R_MN10300_24:
1093 value += addend;
1095 if ((long) value > 0x7fffff || (long) value < -0x800000)
1096 return bfd_reloc_overflow;
1098 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1099 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1100 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1101 return bfd_reloc_ok;
1103 case R_MN10300_16:
1104 value += addend;
1106 if ((long) value > 0x7fff || (long) value < -0x8000)
1107 return bfd_reloc_overflow;
1109 bfd_put_16 (input_bfd, value, hit_data);
1110 return bfd_reloc_ok;
1112 case R_MN10300_8:
1113 value += addend;
1115 if ((long) value > 0x7f || (long) value < -0x80)
1116 return bfd_reloc_overflow;
1118 bfd_put_8 (input_bfd, value, hit_data);
1119 return bfd_reloc_ok;
1121 case R_MN10300_PCREL8:
1122 value -= (input_section->output_section->vma
1123 + input_section->output_offset);
1124 value -= offset;
1125 value += addend;
1127 if ((long) value > 0xff || (long) value < -0x100)
1128 return bfd_reloc_overflow;
1130 bfd_put_8 (input_bfd, value, hit_data);
1131 return bfd_reloc_ok;
1133 case R_MN10300_PCREL16:
1134 value -= (input_section->output_section->vma
1135 + input_section->output_offset);
1136 value -= offset;
1137 value += addend;
1139 if ((long) value > 0xffff || (long) value < -0x10000)
1140 return bfd_reloc_overflow;
1142 bfd_put_16 (input_bfd, value, hit_data);
1143 return bfd_reloc_ok;
1145 case R_MN10300_PCREL32:
1146 value -= (input_section->output_section->vma
1147 + input_section->output_offset);
1148 value -= offset;
1149 value += addend;
1151 bfd_put_32 (input_bfd, value, hit_data);
1152 return bfd_reloc_ok;
1154 case R_MN10300_GNU_VTINHERIT:
1155 case R_MN10300_GNU_VTENTRY:
1156 return bfd_reloc_ok;
1158 case R_MN10300_GOTPC32:
1159 /* Use global offset table as symbol value. */
1161 value = bfd_get_section_by_name (dynobj,
1162 ".got")->output_section->vma;
1163 value -= (input_section->output_section->vma
1164 + input_section->output_offset);
1165 value -= offset;
1166 value += addend;
1168 bfd_put_32 (input_bfd, value, hit_data);
1169 return bfd_reloc_ok;
1171 case R_MN10300_GOTPC16:
1172 /* Use global offset table as symbol value. */
1174 value = bfd_get_section_by_name (dynobj,
1175 ".got")->output_section->vma;
1176 value -= (input_section->output_section->vma
1177 + input_section->output_offset);
1178 value -= offset;
1179 value += addend;
1181 if ((long) value > 0xffff || (long) value < -0x10000)
1182 return bfd_reloc_overflow;
1184 bfd_put_16 (input_bfd, value, hit_data);
1185 return bfd_reloc_ok;
1187 case R_MN10300_GOTOFF32:
1188 value -= bfd_get_section_by_name (dynobj,
1189 ".got")->output_section->vma;
1190 value += addend;
1192 bfd_put_32 (input_bfd, value, hit_data);
1193 return bfd_reloc_ok;
1195 case R_MN10300_GOTOFF24:
1196 value -= bfd_get_section_by_name (dynobj,
1197 ".got")->output_section->vma;
1198 value += addend;
1200 if ((long) value > 0x7fffff || (long) value < -0x800000)
1201 return bfd_reloc_overflow;
1203 bfd_put_8 (input_bfd, value, hit_data);
1204 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1205 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1206 return bfd_reloc_ok;
1208 case R_MN10300_GOTOFF16:
1209 value -= bfd_get_section_by_name (dynobj,
1210 ".got")->output_section->vma;
1211 value += addend;
1213 if ((long) value > 0xffff || (long) value < -0x10000)
1214 return bfd_reloc_overflow;
1216 bfd_put_16 (input_bfd, value, hit_data);
1217 return bfd_reloc_ok;
1219 case R_MN10300_PLT32:
1220 if (h != NULL
1221 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1222 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1223 && h->plt.offset != (bfd_vma) -1)
1225 asection * splt;
1227 splt = bfd_get_section_by_name (dynobj, ".plt");
1229 value = (splt->output_section->vma
1230 + splt->output_offset
1231 + h->plt.offset) - value;
1234 value -= (input_section->output_section->vma
1235 + input_section->output_offset);
1236 value -= offset;
1237 value += addend;
1239 bfd_put_32 (input_bfd, value, hit_data);
1240 return bfd_reloc_ok;
1242 case R_MN10300_PLT16:
1243 if (h != NULL
1244 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1245 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1246 && h->plt.offset != (bfd_vma) -1)
1248 asection * splt;
1250 splt = bfd_get_section_by_name (dynobj, ".plt");
1252 value = (splt->output_section->vma
1253 + splt->output_offset
1254 + h->plt.offset) - value;
1257 value -= (input_section->output_section->vma
1258 + input_section->output_offset);
1259 value -= offset;
1260 value += addend;
1262 if ((long) value > 0xffff || (long) value < -0x10000)
1263 return bfd_reloc_overflow;
1265 bfd_put_16 (input_bfd, value, hit_data);
1266 return bfd_reloc_ok;
1268 case R_MN10300_GOT32:
1269 case R_MN10300_GOT24:
1270 case R_MN10300_GOT16:
1272 asection * sgot;
1274 sgot = bfd_get_section_by_name (dynobj, ".got");
1276 if (h != NULL)
1278 bfd_vma off;
1280 off = h->got.offset;
1281 BFD_ASSERT (off != (bfd_vma) -1);
1283 if (! elf_hash_table (info)->dynamic_sections_created
1284 || SYMBOL_REFERENCES_LOCAL (info, h))
1285 /* This is actually a static link, or it is a
1286 -Bsymbolic link and the symbol is defined
1287 locally, or the symbol was forced to be local
1288 because of a version file. We must initialize
1289 this entry in the global offset table.
1291 When doing a dynamic link, we create a .rela.got
1292 relocation entry to initialize the value. This
1293 is done in the finish_dynamic_symbol routine. */
1294 bfd_put_32 (output_bfd, value,
1295 sgot->contents + off);
1297 value = sgot->output_offset + off;
1299 else
1301 bfd_vma off;
1303 off = elf_local_got_offsets (input_bfd)[symndx];
1305 bfd_put_32 (output_bfd, value, sgot->contents + off);
1307 if (info->shared)
1309 asection * srelgot;
1310 Elf_Internal_Rela outrel;
1312 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1313 BFD_ASSERT (srelgot != NULL);
1315 outrel.r_offset = (sgot->output_section->vma
1316 + sgot->output_offset
1317 + off);
1318 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1319 outrel.r_addend = value;
1320 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1321 (bfd_byte *) (((Elf32_External_Rela *)
1322 srelgot->contents)
1323 + srelgot->reloc_count));
1324 ++ srelgot->reloc_count;
1327 value = sgot->output_offset + off;
1331 value += addend;
1333 if (r_type == R_MN10300_GOT32)
1335 bfd_put_32 (input_bfd, value, hit_data);
1336 return bfd_reloc_ok;
1338 else if (r_type == R_MN10300_GOT24)
1340 if ((long) value > 0x7fffff || (long) value < -0x800000)
1341 return bfd_reloc_overflow;
1343 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1344 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1345 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1346 return bfd_reloc_ok;
1348 else if (r_type == R_MN10300_GOT16)
1350 if ((long) value > 0xffff || (long) value < -0x10000)
1351 return bfd_reloc_overflow;
1353 bfd_put_16 (input_bfd, value, hit_data);
1354 return bfd_reloc_ok;
1356 /* Fall through. */
1358 default:
1359 return bfd_reloc_notsupported;
1363 /* Relocate an MN10300 ELF section. */
1364 static bfd_boolean
1365 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1366 contents, relocs, local_syms, local_sections)
1367 bfd *output_bfd;
1368 struct bfd_link_info *info;
1369 bfd *input_bfd;
1370 asection *input_section;
1371 bfd_byte *contents;
1372 Elf_Internal_Rela *relocs;
1373 Elf_Internal_Sym *local_syms;
1374 asection **local_sections;
1376 Elf_Internal_Shdr *symtab_hdr;
1377 struct elf_link_hash_entry **sym_hashes;
1378 Elf_Internal_Rela *rel, *relend;
1380 if (info->relocatable)
1381 return TRUE;
1383 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1384 sym_hashes = elf_sym_hashes (input_bfd);
1386 rel = relocs;
1387 relend = relocs + input_section->reloc_count;
1388 for (; rel < relend; rel++)
1390 int r_type;
1391 reloc_howto_type *howto;
1392 unsigned long r_symndx;
1393 Elf_Internal_Sym *sym;
1394 asection *sec;
1395 struct elf32_mn10300_link_hash_entry *h;
1396 bfd_vma relocation;
1397 bfd_reloc_status_type r;
1399 r_symndx = ELF32_R_SYM (rel->r_info);
1400 r_type = ELF32_R_TYPE (rel->r_info);
1401 howto = elf_mn10300_howto_table + r_type;
1403 /* Just skip the vtable gc relocs. */
1404 if (r_type == R_MN10300_GNU_VTINHERIT
1405 || r_type == R_MN10300_GNU_VTENTRY)
1406 continue;
1408 h = NULL;
1409 sym = NULL;
1410 sec = NULL;
1411 if (r_symndx < symtab_hdr->sh_info)
1413 sym = local_syms + r_symndx;
1414 sec = local_sections[r_symndx];
1415 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1417 else
1419 bfd_boolean unresolved_reloc;
1420 bfd_boolean warned;
1421 struct elf_link_hash_entry *hh;
1423 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1424 r_symndx, symtab_hdr, sym_hashes,
1425 hh, sec, relocation,
1426 unresolved_reloc, warned);
1428 h = (struct elf32_mn10300_link_hash_entry *) hh;
1430 if ((h->root.root.type == bfd_link_hash_defined
1431 || h->root.root.type == bfd_link_hash_defweak)
1432 && ( r_type == R_MN10300_GOTPC32
1433 || r_type == R_MN10300_GOTPC16
1434 || (( r_type == R_MN10300_PLT32
1435 || r_type == R_MN10300_PLT16)
1436 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1437 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1438 && h->root.plt.offset != (bfd_vma) -1)
1439 || (( r_type == R_MN10300_GOT32
1440 || r_type == R_MN10300_GOT24
1441 || r_type == R_MN10300_GOT16)
1442 && elf_hash_table (info)->dynamic_sections_created
1443 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1444 || (r_type == R_MN10300_32
1445 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1446 && ((input_section->flags & SEC_ALLOC) != 0
1447 /* DWARF will emit R_MN10300_32 relocations
1448 in its sections against symbols defined
1449 externally in shared libraries. We can't
1450 do anything with them here. */
1451 || ((input_section->flags & SEC_DEBUGGING) != 0
1452 && h->root.def_dynamic)))))
1453 /* In these cases, we don't need the relocation
1454 value. We check specially because in some
1455 obscure cases sec->output_section will be NULL. */
1456 relocation = 0;
1458 else if (unresolved_reloc)
1459 (*_bfd_error_handler)
1460 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1461 input_bfd,
1462 input_section,
1463 (long) rel->r_offset,
1464 howto->name,
1465 h->root.root.root.string);
1468 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1469 input_section,
1470 contents, rel->r_offset,
1471 relocation, rel->r_addend,
1472 (struct elf_link_hash_entry *)h,
1473 r_symndx,
1474 info, sec, h == NULL);
1476 if (r != bfd_reloc_ok)
1478 const char *name;
1479 const char *msg = (const char *) 0;
1481 if (h != NULL)
1482 name = h->root.root.root.string;
1483 else
1485 name = (bfd_elf_string_from_elf_section
1486 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1487 if (name == NULL || *name == '\0')
1488 name = bfd_section_name (input_bfd, sec);
1491 switch (r)
1493 case bfd_reloc_overflow:
1494 if (! ((*info->callbacks->reloc_overflow)
1495 (info, (h ? &h->root.root : NULL), name,
1496 howto->name, (bfd_vma) 0, input_bfd,
1497 input_section, rel->r_offset)))
1498 return FALSE;
1499 break;
1501 case bfd_reloc_undefined:
1502 if (! ((*info->callbacks->undefined_symbol)
1503 (info, name, input_bfd, input_section,
1504 rel->r_offset, TRUE)))
1505 return FALSE;
1506 break;
1508 case bfd_reloc_outofrange:
1509 msg = _("internal error: out of range error");
1510 goto common_error;
1512 case bfd_reloc_notsupported:
1513 msg = _("internal error: unsupported relocation error");
1514 goto common_error;
1516 case bfd_reloc_dangerous:
1517 msg = _("internal error: dangerous error");
1518 goto common_error;
1520 default:
1521 msg = _("internal error: unknown error");
1522 /* fall through */
1524 common_error:
1525 if (!((*info->callbacks->warning)
1526 (info, msg, name, input_bfd, input_section,
1527 rel->r_offset)))
1528 return FALSE;
1529 break;
1534 return TRUE;
1537 /* Finish initializing one hash table entry. */
1538 static bfd_boolean
1539 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
1540 struct bfd_hash_entry *gen_entry;
1541 PTR in_args;
1543 struct elf32_mn10300_link_hash_entry *entry;
1544 struct bfd_link_info *link_info = (struct bfd_link_info *)in_args;
1545 unsigned int byte_count = 0;
1547 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1549 if (entry->root.root.type == bfd_link_hash_warning)
1550 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1552 /* If we already know we want to convert "call" to "calls" for calls
1553 to this symbol, then return now. */
1554 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1555 return TRUE;
1557 /* If there are no named calls to this symbol, or there's nothing we
1558 can move from the function itself into the "call" instruction,
1559 then note that all "call" instructions should be converted into
1560 "calls" instructions and return. If a symbol is available for
1561 dynamic symbol resolution (overridable or overriding), avoid
1562 custom calling conventions. */
1563 if (entry->direct_calls == 0
1564 || (entry->stack_size == 0 && entry->movm_args == 0)
1565 || (elf_hash_table (link_info)->dynamic_sections_created
1566 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1567 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1569 /* Make a note that we should convert "call" instructions to "calls"
1570 instructions for calls to this symbol. */
1571 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1572 return TRUE;
1575 /* We may be able to move some instructions from the function itself into
1576 the "call" instruction. Count how many bytes we might be able to
1577 eliminate in the function itself. */
1579 /* A movm instruction is two bytes. */
1580 if (entry->movm_args)
1581 byte_count += 2;
1583 /* Count the insn to allocate stack space too. */
1584 if (entry->stack_size > 0)
1586 if (entry->stack_size <= 128)
1587 byte_count += 3;
1588 else
1589 byte_count += 4;
1592 /* If using "call" will result in larger code, then turn all
1593 the associated "call" instructions into "calls" instructions. */
1594 if (byte_count < entry->direct_calls)
1595 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1597 /* This routine never fails. */
1598 return TRUE;
1601 /* This function handles relaxing for the mn10300.
1603 There are quite a few relaxing opportunities available on the mn10300:
1605 * calls:32 -> calls:16 2 bytes
1606 * call:32 -> call:16 2 bytes
1608 * call:32 -> calls:32 1 byte
1609 * call:16 -> calls:16 1 byte
1610 * These are done anytime using "calls" would result
1611 in smaller code, or when necessary to preserve the
1612 meaning of the program.
1614 * call:32 varies
1615 * call:16
1616 * In some circumstances we can move instructions
1617 from a function prologue into a "call" instruction.
1618 This is only done if the resulting code is no larger
1619 than the original code.
1621 * jmp:32 -> jmp:16 2 bytes
1622 * jmp:16 -> bra:8 1 byte
1624 * If the previous instruction is a conditional branch
1625 around the jump/bra, we may be able to reverse its condition
1626 and change its target to the jump's target. The jump/bra
1627 can then be deleted. 2 bytes
1629 * mov abs32 -> mov abs16 1 or 2 bytes
1631 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
1632 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
1634 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
1635 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
1637 We don't handle imm16->imm8 or d16->d8 as they're very rare
1638 and somewhat more difficult to support. */
1640 static bfd_boolean
1641 mn10300_elf_relax_section (abfd, sec, link_info, again)
1642 bfd *abfd;
1643 asection *sec;
1644 struct bfd_link_info *link_info;
1645 bfd_boolean *again;
1647 Elf_Internal_Shdr *symtab_hdr;
1648 Elf_Internal_Rela *internal_relocs = NULL;
1649 Elf_Internal_Rela *irel, *irelend;
1650 bfd_byte *contents = NULL;
1651 Elf_Internal_Sym *isymbuf = NULL;
1652 struct elf32_mn10300_link_hash_table *hash_table;
1653 asection *section = sec;
1655 /* Assume nothing changes. */
1656 *again = FALSE;
1658 /* We need a pointer to the mn10300 specific hash table. */
1659 hash_table = elf32_mn10300_hash_table (link_info);
1661 /* Initialize fields in each hash table entry the first time through. */
1662 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
1664 bfd *input_bfd;
1666 /* Iterate over all the input bfds. */
1667 for (input_bfd = link_info->input_bfds;
1668 input_bfd != NULL;
1669 input_bfd = input_bfd->link_next)
1671 /* We're going to need all the symbols for each bfd. */
1672 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1673 if (symtab_hdr->sh_info != 0)
1675 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1676 if (isymbuf == NULL)
1677 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1678 symtab_hdr->sh_info, 0,
1679 NULL, NULL, NULL);
1680 if (isymbuf == NULL)
1681 goto error_return;
1684 /* Iterate over each section in this bfd. */
1685 for (section = input_bfd->sections;
1686 section != NULL;
1687 section = section->next)
1689 struct elf32_mn10300_link_hash_entry *hash;
1690 Elf_Internal_Sym *sym;
1691 asection *sym_sec = NULL;
1692 const char *sym_name;
1693 char *new_name;
1695 /* If there's nothing to do in this section, skip it. */
1696 if (! (((section->flags & SEC_RELOC) != 0
1697 && section->reloc_count != 0)
1698 || (section->flags & SEC_CODE) != 0))
1699 continue;
1701 /* Get cached copy of section contents if it exists. */
1702 if (elf_section_data (section)->this_hdr.contents != NULL)
1703 contents = elf_section_data (section)->this_hdr.contents;
1704 else if (section->size != 0)
1706 /* Go get them off disk. */
1707 if (!bfd_malloc_and_get_section (input_bfd, section,
1708 &contents))
1709 goto error_return;
1711 else
1712 contents = NULL;
1714 /* If there aren't any relocs, then there's nothing to do. */
1715 if ((section->flags & SEC_RELOC) != 0
1716 && section->reloc_count != 0)
1719 /* Get a copy of the native relocations. */
1720 internal_relocs = (_bfd_elf_link_read_relocs
1721 (input_bfd, section, (PTR) NULL,
1722 (Elf_Internal_Rela *) NULL,
1723 link_info->keep_memory));
1724 if (internal_relocs == NULL)
1725 goto error_return;
1727 /* Now examine each relocation. */
1728 irel = internal_relocs;
1729 irelend = irel + section->reloc_count;
1730 for (; irel < irelend; irel++)
1732 long r_type;
1733 unsigned long r_index;
1734 unsigned char code;
1736 r_type = ELF32_R_TYPE (irel->r_info);
1737 r_index = ELF32_R_SYM (irel->r_info);
1739 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
1740 goto error_return;
1742 /* We need the name and hash table entry of the target
1743 symbol! */
1744 hash = NULL;
1745 sym = NULL;
1746 sym_sec = NULL;
1748 if (r_index < symtab_hdr->sh_info)
1750 /* A local symbol. */
1751 Elf_Internal_Sym *isym;
1752 struct elf_link_hash_table *elftab;
1753 bfd_size_type amt;
1755 isym = isymbuf + r_index;
1756 if (isym->st_shndx == SHN_UNDEF)
1757 sym_sec = bfd_und_section_ptr;
1758 else if (isym->st_shndx == SHN_ABS)
1759 sym_sec = bfd_abs_section_ptr;
1760 else if (isym->st_shndx == SHN_COMMON)
1761 sym_sec = bfd_com_section_ptr;
1762 else
1763 sym_sec
1764 = bfd_section_from_elf_index (input_bfd,
1765 isym->st_shndx);
1767 sym_name
1768 = bfd_elf_string_from_elf_section (input_bfd,
1769 (symtab_hdr
1770 ->sh_link),
1771 isym->st_name);
1773 /* If it isn't a function, then we don't care
1774 about it. */
1775 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
1776 continue;
1778 /* Tack on an ID so we can uniquely identify this
1779 local symbol in the global hash table. */
1780 amt = strlen (sym_name) + 10;
1781 new_name = bfd_malloc (amt);
1782 if (new_name == 0)
1783 goto error_return;
1785 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
1786 sym_name = new_name;
1788 elftab = &hash_table->static_hash_table->root;
1789 hash = ((struct elf32_mn10300_link_hash_entry *)
1790 elf_link_hash_lookup (elftab, sym_name,
1791 TRUE, TRUE, FALSE));
1792 free (new_name);
1794 else
1796 r_index -= symtab_hdr->sh_info;
1797 hash = (struct elf32_mn10300_link_hash_entry *)
1798 elf_sym_hashes (input_bfd)[r_index];
1801 /* If this is not a "call" instruction, then we
1802 should convert "call" instructions to "calls"
1803 instructions. */
1804 code = bfd_get_8 (input_bfd,
1805 contents + irel->r_offset - 1);
1806 if (code != 0xdd && code != 0xcd)
1807 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1809 /* If this is a jump/call, then bump the
1810 direct_calls counter. Else force "call" to
1811 "calls" conversions. */
1812 if (r_type == R_MN10300_PCREL32
1813 || r_type == R_MN10300_PLT32
1814 || r_type == R_MN10300_PLT16
1815 || r_type == R_MN10300_PCREL16)
1816 hash->direct_calls++;
1817 else
1818 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1822 /* Now look at the actual contents to get the stack size,
1823 and a list of what registers were saved in the prologue
1824 (ie movm_args). */
1825 if ((section->flags & SEC_CODE) != 0)
1827 Elf_Internal_Sym *isym, *isymend;
1828 unsigned int sec_shndx;
1829 struct elf_link_hash_entry **hashes;
1830 struct elf_link_hash_entry **end_hashes;
1831 unsigned int symcount;
1833 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1834 section);
1836 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1837 - symtab_hdr->sh_info);
1838 hashes = elf_sym_hashes (input_bfd);
1839 end_hashes = hashes + symcount;
1841 /* Look at each function defined in this section and
1842 update info for that function. */
1843 isymend = isymbuf + symtab_hdr->sh_info;
1844 for (isym = isymbuf; isym < isymend; isym++)
1846 if (isym->st_shndx == sec_shndx
1847 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
1849 struct elf_link_hash_table *elftab;
1850 bfd_size_type amt;
1851 struct elf_link_hash_entry **lhashes = hashes;
1853 /* Skip a local symbol if it aliases a
1854 global one. */
1855 for (; lhashes < end_hashes; lhashes++)
1857 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
1858 if ((hash->root.root.type == bfd_link_hash_defined
1859 || hash->root.root.type == bfd_link_hash_defweak)
1860 && hash->root.root.u.def.section == section
1861 && hash->root.type == STT_FUNC
1862 && hash->root.root.u.def.value == isym->st_value)
1863 break;
1865 if (lhashes != end_hashes)
1866 continue;
1868 if (isym->st_shndx == SHN_UNDEF)
1869 sym_sec = bfd_und_section_ptr;
1870 else if (isym->st_shndx == SHN_ABS)
1871 sym_sec = bfd_abs_section_ptr;
1872 else if (isym->st_shndx == SHN_COMMON)
1873 sym_sec = bfd_com_section_ptr;
1874 else
1875 sym_sec
1876 = bfd_section_from_elf_index (input_bfd,
1877 isym->st_shndx);
1879 sym_name = (bfd_elf_string_from_elf_section
1880 (input_bfd, symtab_hdr->sh_link,
1881 isym->st_name));
1883 /* Tack on an ID so we can uniquely identify this
1884 local symbol in the global hash table. */
1885 amt = strlen (sym_name) + 10;
1886 new_name = bfd_malloc (amt);
1887 if (new_name == 0)
1888 goto error_return;
1890 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
1891 sym_name = new_name;
1893 elftab = &hash_table->static_hash_table->root;
1894 hash = ((struct elf32_mn10300_link_hash_entry *)
1895 elf_link_hash_lookup (elftab, sym_name,
1896 TRUE, TRUE, FALSE));
1897 free (new_name);
1898 compute_function_info (input_bfd, hash,
1899 isym->st_value, contents);
1903 for (; hashes < end_hashes; hashes++)
1905 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1906 if ((hash->root.root.type == bfd_link_hash_defined
1907 || hash->root.root.type == bfd_link_hash_defweak)
1908 && hash->root.root.u.def.section == section
1909 && hash->root.type == STT_FUNC)
1910 compute_function_info (input_bfd, hash,
1911 (hash)->root.root.u.def.value,
1912 contents);
1916 /* Cache or free any memory we allocated for the relocs. */
1917 if (internal_relocs != NULL
1918 && elf_section_data (section)->relocs != internal_relocs)
1919 free (internal_relocs);
1920 internal_relocs = NULL;
1922 /* Cache or free any memory we allocated for the contents. */
1923 if (contents != NULL
1924 && elf_section_data (section)->this_hdr.contents != contents)
1926 if (! link_info->keep_memory)
1927 free (contents);
1928 else
1930 /* Cache the section contents for elf_link_input_bfd. */
1931 elf_section_data (section)->this_hdr.contents = contents;
1934 contents = NULL;
1937 /* Cache or free any memory we allocated for the symbols. */
1938 if (isymbuf != NULL
1939 && symtab_hdr->contents != (unsigned char *) isymbuf)
1941 if (! link_info->keep_memory)
1942 free (isymbuf);
1943 else
1945 /* Cache the symbols for elf_link_input_bfd. */
1946 symtab_hdr->contents = (unsigned char *) isymbuf;
1949 isymbuf = NULL;
1952 /* Now iterate on each symbol in the hash table and perform
1953 the final initialization steps on each. */
1954 elf32_mn10300_link_hash_traverse (hash_table,
1955 elf32_mn10300_finish_hash_table_entry,
1956 link_info);
1957 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1958 elf32_mn10300_finish_hash_table_entry,
1959 link_info);
1961 /* All entries in the hash table are fully initialized. */
1962 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1964 /* Now that everything has been initialized, go through each
1965 code section and delete any prologue insns which will be
1966 redundant because their operations will be performed by
1967 a "call" instruction. */
1968 for (input_bfd = link_info->input_bfds;
1969 input_bfd != NULL;
1970 input_bfd = input_bfd->link_next)
1972 /* We're going to need all the local symbols for each bfd. */
1973 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1974 if (symtab_hdr->sh_info != 0)
1976 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1977 if (isymbuf == NULL)
1978 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1979 symtab_hdr->sh_info, 0,
1980 NULL, NULL, NULL);
1981 if (isymbuf == NULL)
1982 goto error_return;
1985 /* Walk over each section in this bfd. */
1986 for (section = input_bfd->sections;
1987 section != NULL;
1988 section = section->next)
1990 unsigned int sec_shndx;
1991 Elf_Internal_Sym *isym, *isymend;
1992 struct elf_link_hash_entry **hashes;
1993 struct elf_link_hash_entry **end_hashes;
1994 unsigned int symcount;
1996 /* Skip non-code sections and empty sections. */
1997 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
1998 continue;
2000 if (section->reloc_count != 0)
2002 /* Get a copy of the native relocations. */
2003 internal_relocs = (_bfd_elf_link_read_relocs
2004 (input_bfd, section, (PTR) NULL,
2005 (Elf_Internal_Rela *) NULL,
2006 link_info->keep_memory));
2007 if (internal_relocs == NULL)
2008 goto error_return;
2011 /* Get cached copy of section contents if it exists. */
2012 if (elf_section_data (section)->this_hdr.contents != NULL)
2013 contents = elf_section_data (section)->this_hdr.contents;
2014 else
2016 /* Go get them off disk. */
2017 if (!bfd_malloc_and_get_section (input_bfd, section,
2018 &contents))
2019 goto error_return;
2022 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2023 section);
2025 /* Now look for any function in this section which needs
2026 insns deleted from its prologue. */
2027 isymend = isymbuf + symtab_hdr->sh_info;
2028 for (isym = isymbuf; isym < isymend; isym++)
2030 struct elf32_mn10300_link_hash_entry *sym_hash;
2031 asection *sym_sec = NULL;
2032 const char *sym_name;
2033 char *new_name;
2034 struct elf_link_hash_table *elftab;
2035 bfd_size_type amt;
2037 if (isym->st_shndx != sec_shndx)
2038 continue;
2040 if (isym->st_shndx == SHN_UNDEF)
2041 sym_sec = bfd_und_section_ptr;
2042 else if (isym->st_shndx == SHN_ABS)
2043 sym_sec = bfd_abs_section_ptr;
2044 else if (isym->st_shndx == SHN_COMMON)
2045 sym_sec = bfd_com_section_ptr;
2046 else
2047 sym_sec
2048 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2050 sym_name
2051 = bfd_elf_string_from_elf_section (input_bfd,
2052 symtab_hdr->sh_link,
2053 isym->st_name);
2055 /* Tack on an ID so we can uniquely identify this
2056 local symbol in the global hash table. */
2057 amt = strlen (sym_name) + 10;
2058 new_name = bfd_malloc (amt);
2059 if (new_name == 0)
2060 goto error_return;
2061 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2062 sym_name = new_name;
2064 elftab = &hash_table->static_hash_table->root;
2065 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
2066 elf_link_hash_lookup (elftab, sym_name,
2067 FALSE, FALSE, FALSE));
2069 free (new_name);
2070 if (sym_hash == NULL)
2071 continue;
2073 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2074 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2076 int bytes = 0;
2078 /* Note that we've changed things. */
2079 elf_section_data (section)->relocs = internal_relocs;
2080 elf_section_data (section)->this_hdr.contents = contents;
2081 symtab_hdr->contents = (unsigned char *) isymbuf;
2083 /* Count how many bytes we're going to delete. */
2084 if (sym_hash->movm_args)
2085 bytes += 2;
2087 if (sym_hash->stack_size > 0)
2089 if (sym_hash->stack_size <= 128)
2090 bytes += 3;
2091 else
2092 bytes += 4;
2095 /* Note that we've deleted prologue bytes for this
2096 function. */
2097 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2099 /* Actually delete the bytes. */
2100 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2101 section,
2102 isym->st_value,
2103 bytes))
2104 goto error_return;
2106 /* Something changed. Not strictly necessary, but
2107 may lead to more relaxing opportunities. */
2108 *again = TRUE;
2112 /* Look for any global functions in this section which
2113 need insns deleted from their prologues. */
2114 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2115 - symtab_hdr->sh_info);
2116 hashes = elf_sym_hashes (input_bfd);
2117 end_hashes = hashes + symcount;
2118 for (; hashes < end_hashes; hashes++)
2120 struct elf32_mn10300_link_hash_entry *sym_hash;
2122 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2123 if ((sym_hash->root.root.type == bfd_link_hash_defined
2124 || sym_hash->root.root.type == bfd_link_hash_defweak)
2125 && sym_hash->root.root.u.def.section == section
2126 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2127 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2129 int bytes = 0;
2130 bfd_vma symval;
2132 /* Note that we've changed things. */
2133 elf_section_data (section)->relocs = internal_relocs;
2134 elf_section_data (section)->this_hdr.contents = contents;
2135 symtab_hdr->contents = (unsigned char *) isymbuf;
2137 /* Count how many bytes we're going to delete. */
2138 if (sym_hash->movm_args)
2139 bytes += 2;
2141 if (sym_hash->stack_size > 0)
2143 if (sym_hash->stack_size <= 128)
2144 bytes += 3;
2145 else
2146 bytes += 4;
2149 /* Note that we've deleted prologue bytes for this
2150 function. */
2151 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2153 /* Actually delete the bytes. */
2154 symval = sym_hash->root.root.u.def.value;
2155 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2156 section,
2157 symval,
2158 bytes))
2159 goto error_return;
2161 /* Something changed. Not strictly necessary, but
2162 may lead to more relaxing opportunities. */
2163 *again = TRUE;
2167 /* Cache or free any memory we allocated for the relocs. */
2168 if (internal_relocs != NULL
2169 && elf_section_data (section)->relocs != internal_relocs)
2170 free (internal_relocs);
2171 internal_relocs = NULL;
2173 /* Cache or free any memory we allocated for the contents. */
2174 if (contents != NULL
2175 && elf_section_data (section)->this_hdr.contents != contents)
2177 if (! link_info->keep_memory)
2178 free (contents);
2179 else
2181 /* Cache the section contents for elf_link_input_bfd. */
2182 elf_section_data (section)->this_hdr.contents = contents;
2185 contents = NULL;
2188 /* Cache or free any memory we allocated for the symbols. */
2189 if (isymbuf != NULL
2190 && symtab_hdr->contents != (unsigned char *) isymbuf)
2192 if (! link_info->keep_memory)
2193 free (isymbuf);
2194 else
2196 /* Cache the symbols for elf_link_input_bfd. */
2197 symtab_hdr->contents = (unsigned char *) isymbuf;
2200 isymbuf = NULL;
2204 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2205 contents = NULL;
2206 internal_relocs = NULL;
2207 isymbuf = NULL;
2208 /* For error_return. */
2209 section = sec;
2211 /* We don't have to do anything for a relocatable link, if
2212 this section does not have relocs, or if this is not a
2213 code section. */
2214 if (link_info->relocatable
2215 || (sec->flags & SEC_RELOC) == 0
2216 || sec->reloc_count == 0
2217 || (sec->flags & SEC_CODE) == 0)
2218 return TRUE;
2220 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2222 /* Get a copy of the native relocations. */
2223 internal_relocs = (_bfd_elf_link_read_relocs
2224 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2225 link_info->keep_memory));
2226 if (internal_relocs == NULL)
2227 goto error_return;
2229 /* Walk through them looking for relaxing opportunities. */
2230 irelend = internal_relocs + sec->reloc_count;
2231 for (irel = internal_relocs; irel < irelend; irel++)
2233 bfd_vma symval;
2234 struct elf32_mn10300_link_hash_entry *h = NULL;
2236 /* If this isn't something that can be relaxed, then ignore
2237 this reloc. */
2238 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2239 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2240 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2241 continue;
2243 /* Get the section contents if we haven't done so already. */
2244 if (contents == NULL)
2246 /* Get cached copy if it exists. */
2247 if (elf_section_data (sec)->this_hdr.contents != NULL)
2248 contents = elf_section_data (sec)->this_hdr.contents;
2249 else
2251 /* Go get them off disk. */
2252 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2253 goto error_return;
2257 /* Read this BFD's symbols if we haven't done so already. */
2258 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2260 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2261 if (isymbuf == NULL)
2262 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2263 symtab_hdr->sh_info, 0,
2264 NULL, NULL, NULL);
2265 if (isymbuf == NULL)
2266 goto error_return;
2269 /* Get the value of the symbol referred to by the reloc. */
2270 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2272 Elf_Internal_Sym *isym;
2273 asection *sym_sec = NULL;
2274 const char *sym_name;
2275 char *new_name;
2276 bfd_vma saved_addend;
2278 /* A local symbol. */
2279 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2280 if (isym->st_shndx == SHN_UNDEF)
2281 sym_sec = bfd_und_section_ptr;
2282 else if (isym->st_shndx == SHN_ABS)
2283 sym_sec = bfd_abs_section_ptr;
2284 else if (isym->st_shndx == SHN_COMMON)
2285 sym_sec = bfd_com_section_ptr;
2286 else
2287 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2289 sym_name = bfd_elf_string_from_elf_section (abfd,
2290 symtab_hdr->sh_link,
2291 isym->st_name);
2293 if ((sym_sec->flags & SEC_MERGE)
2294 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
2295 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2297 saved_addend = irel->r_addend;
2298 symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
2299 symval += irel->r_addend;
2300 irel->r_addend = saved_addend;
2302 else
2304 symval = (isym->st_value
2305 + sym_sec->output_section->vma
2306 + sym_sec->output_offset);
2308 /* Tack on an ID so we can uniquely identify this
2309 local symbol in the global hash table. */
2310 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2311 if (new_name == 0)
2312 goto error_return;
2313 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2314 sym_name = new_name;
2316 h = (struct elf32_mn10300_link_hash_entry *)
2317 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2318 sym_name, FALSE, FALSE, FALSE);
2319 free (new_name);
2321 else
2323 unsigned long indx;
2325 /* An external symbol. */
2326 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2327 h = (struct elf32_mn10300_link_hash_entry *)
2328 (elf_sym_hashes (abfd)[indx]);
2329 BFD_ASSERT (h != NULL);
2330 if (h->root.root.type != bfd_link_hash_defined
2331 && h->root.root.type != bfd_link_hash_defweak)
2333 /* This appears to be a reference to an undefined
2334 symbol. Just ignore it--it will be caught by the
2335 regular reloc processing. */
2336 continue;
2339 symval = (h->root.root.u.def.value
2340 + h->root.root.u.def.section->output_section->vma
2341 + h->root.root.u.def.section->output_offset);
2344 /* For simplicity of coding, we are going to modify the section
2345 contents, the section relocs, and the BFD symbol table. We
2346 must tell the rest of the code not to free up this
2347 information. It would be possible to instead create a table
2348 of changes which have to be made, as is done in coff-mips.c;
2349 that would be more work, but would require less memory when
2350 the linker is run. */
2352 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2353 branch/call, also deal with "call" -> "calls" conversions and
2354 insertion of prologue data into "call" instructions. */
2355 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2356 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2358 bfd_vma value = symval;
2360 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2361 && h != NULL
2362 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2363 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2364 && h->root.plt.offset != (bfd_vma) -1)
2366 asection * splt;
2368 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2369 ->dynobj, ".plt");
2371 value = ((splt->output_section->vma
2372 + splt->output_offset
2373 + h->root.plt.offset)
2374 - (sec->output_section->vma
2375 + sec->output_offset
2376 + irel->r_offset));
2379 /* If we've got a "call" instruction that needs to be turned
2380 into a "calls" instruction, do so now. It saves a byte. */
2381 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2383 unsigned char code;
2385 /* Get the opcode. */
2386 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2388 /* Make sure we're working with a "call" instruction! */
2389 if (code == 0xdd)
2391 /* Note that we've changed the relocs, section contents,
2392 etc. */
2393 elf_section_data (sec)->relocs = internal_relocs;
2394 elf_section_data (sec)->this_hdr.contents = contents;
2395 symtab_hdr->contents = (unsigned char *) isymbuf;
2397 /* Fix the opcode. */
2398 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2399 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2401 /* Fix irel->r_offset and irel->r_addend. */
2402 irel->r_offset += 1;
2403 irel->r_addend += 1;
2405 /* Delete one byte of data. */
2406 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2407 irel->r_offset + 3, 1))
2408 goto error_return;
2410 /* That will change things, so, we should relax again.
2411 Note that this is not required, and it may be slow. */
2412 *again = TRUE;
2415 else if (h)
2417 /* We've got a "call" instruction which needs some data
2418 from target function filled in. */
2419 unsigned char code;
2421 /* Get the opcode. */
2422 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2424 /* Insert data from the target function into the "call"
2425 instruction if needed. */
2426 if (code == 0xdd)
2428 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2429 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2430 contents + irel->r_offset + 5);
2434 /* Deal with pc-relative gunk. */
2435 value -= (sec->output_section->vma + sec->output_offset);
2436 value -= irel->r_offset;
2437 value += irel->r_addend;
2439 /* See if the value will fit in 16 bits, note the high value is
2440 0x7fff + 2 as the target will be two bytes closer if we are
2441 able to relax. */
2442 if ((long) value < 0x8001 && (long) value > -0x8000)
2444 unsigned char code;
2446 /* Get the opcode. */
2447 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2449 if (code != 0xdc && code != 0xdd && code != 0xff)
2450 continue;
2452 /* Note that we've changed the relocs, section contents, etc. */
2453 elf_section_data (sec)->relocs = internal_relocs;
2454 elf_section_data (sec)->this_hdr.contents = contents;
2455 symtab_hdr->contents = (unsigned char *) isymbuf;
2457 /* Fix the opcode. */
2458 if (code == 0xdc)
2459 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2460 else if (code == 0xdd)
2461 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2462 else if (code == 0xff)
2463 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2465 /* Fix the relocation's type. */
2466 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2467 (ELF32_R_TYPE (irel->r_info)
2468 == (int) R_MN10300_PLT32)
2469 ? R_MN10300_PLT16 :
2470 R_MN10300_PCREL16);
2472 /* Delete two bytes of data. */
2473 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2474 irel->r_offset + 1, 2))
2475 goto error_return;
2477 /* That will change things, so, we should relax again.
2478 Note that this is not required, and it may be slow. */
2479 *again = TRUE;
2483 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2484 branch. */
2485 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2487 bfd_vma value = symval;
2489 /* If we've got a "call" instruction that needs to be turned
2490 into a "calls" instruction, do so now. It saves a byte. */
2491 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2493 unsigned char code;
2495 /* Get the opcode. */
2496 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2498 /* Make sure we're working with a "call" instruction! */
2499 if (code == 0xcd)
2501 /* Note that we've changed the relocs, section contents,
2502 etc. */
2503 elf_section_data (sec)->relocs = internal_relocs;
2504 elf_section_data (sec)->this_hdr.contents = contents;
2505 symtab_hdr->contents = (unsigned char *) isymbuf;
2507 /* Fix the opcode. */
2508 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
2509 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2511 /* Fix irel->r_offset and irel->r_addend. */
2512 irel->r_offset += 1;
2513 irel->r_addend += 1;
2515 /* Delete one byte of data. */
2516 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2517 irel->r_offset + 1, 1))
2518 goto error_return;
2520 /* That will change things, so, we should relax again.
2521 Note that this is not required, and it may be slow. */
2522 *again = TRUE;
2525 else if (h)
2527 unsigned char code;
2529 /* Get the opcode. */
2530 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2532 /* Insert data from the target function into the "call"
2533 instruction if needed. */
2534 if (code == 0xcd)
2536 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
2537 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2538 contents + irel->r_offset + 3);
2542 /* Deal with pc-relative gunk. */
2543 value -= (sec->output_section->vma + sec->output_offset);
2544 value -= irel->r_offset;
2545 value += irel->r_addend;
2547 /* See if the value will fit in 8 bits, note the high value is
2548 0x7f + 1 as the target will be one bytes closer if we are
2549 able to relax. */
2550 if ((long) value < 0x80 && (long) value > -0x80)
2552 unsigned char code;
2554 /* Get the opcode. */
2555 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2557 if (code != 0xcc)
2558 continue;
2560 /* Note that we've changed the relocs, section contents, etc. */
2561 elf_section_data (sec)->relocs = internal_relocs;
2562 elf_section_data (sec)->this_hdr.contents = contents;
2563 symtab_hdr->contents = (unsigned char *) isymbuf;
2565 /* Fix the opcode. */
2566 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
2568 /* Fix the relocation's type. */
2569 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2570 R_MN10300_PCREL8);
2572 /* Delete one byte of data. */
2573 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2574 irel->r_offset + 1, 1))
2575 goto error_return;
2577 /* That will change things, so, we should relax again.
2578 Note that this is not required, and it may be slow. */
2579 *again = TRUE;
2583 /* Try to eliminate an unconditional 8 bit pc-relative branch
2584 which immediately follows a conditional 8 bit pc-relative
2585 branch around the unconditional branch.
2587 original: new:
2588 bCC lab1 bCC' lab2
2589 bra lab2
2590 lab1: lab1:
2592 This happens when the bCC can't reach lab2 at assembly time,
2593 but due to other relaxations it can reach at link time. */
2594 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
2596 Elf_Internal_Rela *nrel;
2597 bfd_vma value = symval;
2598 unsigned char code;
2600 /* Deal with pc-relative gunk. */
2601 value -= (sec->output_section->vma + sec->output_offset);
2602 value -= irel->r_offset;
2603 value += irel->r_addend;
2605 /* Do nothing if this reloc is the last byte in the section. */
2606 if (irel->r_offset == sec->size)
2607 continue;
2609 /* See if the next instruction is an unconditional pc-relative
2610 branch, more often than not this test will fail, so we
2611 test it first to speed things up. */
2612 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2613 if (code != 0xca)
2614 continue;
2616 /* Also make sure the next relocation applies to the next
2617 instruction and that it's a pc-relative 8 bit branch. */
2618 nrel = irel + 1;
2619 if (nrel == irelend
2620 || irel->r_offset + 2 != nrel->r_offset
2621 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
2622 continue;
2624 /* Make sure our destination immediately follows the
2625 unconditional branch. */
2626 if (symval != (sec->output_section->vma + sec->output_offset
2627 + irel->r_offset + 3))
2628 continue;
2630 /* Now make sure we are a conditional branch. This may not
2631 be necessary, but why take the chance.
2633 Note these checks assume that R_MN10300_PCREL8 relocs
2634 only occur on bCC and bCCx insns. If they occured
2635 elsewhere, we'd need to know the start of this insn
2636 for this check to be accurate. */
2637 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2638 if (code != 0xc0 && code != 0xc1 && code != 0xc2
2639 && code != 0xc3 && code != 0xc4 && code != 0xc5
2640 && code != 0xc6 && code != 0xc7 && code != 0xc8
2641 && code != 0xc9 && code != 0xe8 && code != 0xe9
2642 && code != 0xea && code != 0xeb)
2643 continue;
2645 /* We also have to be sure there is no symbol/label
2646 at the unconditional branch. */
2647 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
2648 irel->r_offset + 1))
2649 continue;
2651 /* Note that we've changed the relocs, section contents, etc. */
2652 elf_section_data (sec)->relocs = internal_relocs;
2653 elf_section_data (sec)->this_hdr.contents = contents;
2654 symtab_hdr->contents = (unsigned char *) isymbuf;
2656 /* Reverse the condition of the first branch. */
2657 switch (code)
2659 case 0xc8:
2660 code = 0xc9;
2661 break;
2662 case 0xc9:
2663 code = 0xc8;
2664 break;
2665 case 0xc0:
2666 code = 0xc2;
2667 break;
2668 case 0xc2:
2669 code = 0xc0;
2670 break;
2671 case 0xc3:
2672 code = 0xc1;
2673 break;
2674 case 0xc1:
2675 code = 0xc3;
2676 break;
2677 case 0xc4:
2678 code = 0xc6;
2679 break;
2680 case 0xc6:
2681 code = 0xc4;
2682 break;
2683 case 0xc7:
2684 code = 0xc5;
2685 break;
2686 case 0xc5:
2687 code = 0xc7;
2688 break;
2689 case 0xe8:
2690 code = 0xe9;
2691 break;
2692 case 0x9d:
2693 code = 0xe8;
2694 break;
2695 case 0xea:
2696 code = 0xeb;
2697 break;
2698 case 0xeb:
2699 code = 0xea;
2700 break;
2702 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2704 /* Set the reloc type and symbol for the first branch
2705 from the second branch. */
2706 irel->r_info = nrel->r_info;
2708 /* Make the reloc for the second branch a null reloc. */
2709 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
2710 R_MN10300_NONE);
2712 /* Delete two bytes of data. */
2713 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2714 irel->r_offset + 1, 2))
2715 goto error_return;
2717 /* That will change things, so, we should relax again.
2718 Note that this is not required, and it may be slow. */
2719 *again = TRUE;
2722 /* Try to turn a 24 immediate, displacement or absolute address
2723 into a 8 immediate, displacement or absolute address. */
2724 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
2726 bfd_vma value = symval;
2727 value += irel->r_addend;
2729 /* See if the value will fit in 8 bits. */
2730 if ((long) value < 0x7f && (long) value > -0x80)
2732 unsigned char code;
2734 /* AM33 insns which have 24 operands are 6 bytes long and
2735 will have 0xfd as the first byte. */
2737 /* Get the first opcode. */
2738 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2740 if (code == 0xfd)
2742 /* Get the second opcode. */
2743 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2745 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2746 equivalent instructions exists. */
2747 if (code != 0x6b && code != 0x7b
2748 && code != 0x8b && code != 0x9b
2749 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2750 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2751 || (code & 0x0f) == 0x0e))
2753 /* Not safe if the high bit is on as relaxing may
2754 move the value out of high mem and thus not fit
2755 in a signed 8bit value. This is currently over
2756 conservative. */
2757 if ((value & 0x80) == 0)
2759 /* Note that we've changed the relocation contents,
2760 etc. */
2761 elf_section_data (sec)->relocs = internal_relocs;
2762 elf_section_data (sec)->this_hdr.contents = contents;
2763 symtab_hdr->contents = (unsigned char *) isymbuf;
2765 /* Fix the opcode. */
2766 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2767 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2769 /* Fix the relocation's type. */
2770 irel->r_info =
2771 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2772 R_MN10300_8);
2774 /* Delete two bytes of data. */
2775 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2776 irel->r_offset + 1, 2))
2777 goto error_return;
2779 /* That will change things, so, we should relax
2780 again. Note that this is not required, and it
2781 may be slow. */
2782 *again = TRUE;
2783 break;
2790 /* Try to turn a 32bit immediate, displacement or absolute address
2791 into a 16bit immediate, displacement or absolute address. */
2792 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
2793 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
2794 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32
2795 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2797 bfd_vma value = symval;
2799 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
2801 asection * sgot;
2803 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
2804 ->dynobj, ".got");
2806 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
2808 value = sgot->output_offset;
2810 if (h)
2811 value += h->root.got.offset;
2812 else
2813 value += (elf_local_got_offsets
2814 (abfd)[ELF32_R_SYM (irel->r_info)]);
2816 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
2817 value -= sgot->output_section->vma;
2818 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2819 value = (sgot->output_section->vma
2820 - (sec->output_section->vma
2821 + sec->output_offset
2822 + irel->r_offset));
2823 else
2824 abort ();
2827 value += irel->r_addend;
2829 /* See if the value will fit in 24 bits.
2830 We allow any 16bit match here. We prune those we can't
2831 handle below. */
2832 if ((long) value < 0x7fffff && (long) value > -0x800000)
2834 unsigned char code;
2836 /* AM33 insns which have 32bit operands are 7 bytes long and
2837 will have 0xfe as the first byte. */
2839 /* Get the first opcode. */
2840 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2842 if (code == 0xfe)
2844 /* Get the second opcode. */
2845 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2847 /* All the am33 32 -> 24 relaxing possibilities. */
2848 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2849 equivalent instructions exists. */
2850 if (code != 0x6b && code != 0x7b
2851 && code != 0x8b && code != 0x9b
2852 && (ELF32_R_TYPE (irel->r_info)
2853 != (int) R_MN10300_GOTPC32)
2854 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2855 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2856 || (code & 0x0f) == 0x0e))
2858 /* Not safe if the high bit is on as relaxing may
2859 move the value out of high mem and thus not fit
2860 in a signed 16bit value. This is currently over
2861 conservative. */
2862 if ((value & 0x8000) == 0)
2864 /* Note that we've changed the relocation contents,
2865 etc. */
2866 elf_section_data (sec)->relocs = internal_relocs;
2867 elf_section_data (sec)->this_hdr.contents = contents;
2868 symtab_hdr->contents = (unsigned char *) isymbuf;
2870 /* Fix the opcode. */
2871 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2872 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2874 /* Fix the relocation's type. */
2875 irel->r_info =
2876 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2877 (ELF32_R_TYPE (irel->r_info)
2878 == (int) R_MN10300_GOTOFF32)
2879 ? R_MN10300_GOTOFF24
2880 : (ELF32_R_TYPE (irel->r_info)
2881 == (int) R_MN10300_GOT32)
2882 ? R_MN10300_GOT24 :
2883 R_MN10300_24);
2885 /* Delete one byte of data. */
2886 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2887 irel->r_offset + 3, 1))
2888 goto error_return;
2890 /* That will change things, so, we should relax
2891 again. Note that this is not required, and it
2892 may be slow. */
2893 *again = TRUE;
2894 break;
2900 /* See if the value will fit in 16 bits.
2901 We allow any 16bit match here. We prune those we can't
2902 handle below. */
2903 if ((long) value < 0x7fff && (long) value > -0x8000)
2905 unsigned char code;
2907 /* Most insns which have 32bit operands are 6 bytes long;
2908 exceptions are pcrel insns and bit insns.
2910 We handle pcrel insns above. We don't bother trying
2911 to handle the bit insns here.
2913 The first byte of the remaining insns will be 0xfc. */
2915 /* Get the first opcode. */
2916 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2918 if (code != 0xfc)
2919 continue;
2921 /* Get the second opcode. */
2922 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2924 if ((code & 0xf0) < 0x80)
2925 switch (code & 0xf0)
2927 /* mov (d32,am),dn -> mov (d32,am),dn
2928 mov dm,(d32,am) -> mov dn,(d32,am)
2929 mov (d32,am),an -> mov (d32,am),an
2930 mov dm,(d32,am) -> mov dn,(d32,am)
2931 movbu (d32,am),dn -> movbu (d32,am),dn
2932 movbu dm,(d32,am) -> movbu dn,(d32,am)
2933 movhu (d32,am),dn -> movhu (d32,am),dn
2934 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2935 case 0x00:
2936 case 0x10:
2937 case 0x20:
2938 case 0x30:
2939 case 0x40:
2940 case 0x50:
2941 case 0x60:
2942 case 0x70:
2943 /* Not safe if the high bit is on as relaxing may
2944 move the value out of high mem and thus not fit
2945 in a signed 16bit value. */
2946 if (code == 0xcc
2947 && (value & 0x8000))
2948 continue;
2950 /* Note that we've changed the relocation contents, etc. */
2951 elf_section_data (sec)->relocs = internal_relocs;
2952 elf_section_data (sec)->this_hdr.contents = contents;
2953 symtab_hdr->contents = (unsigned char *) isymbuf;
2955 /* Fix the opcode. */
2956 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2957 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2959 /* Fix the relocation's type. */
2960 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2961 (ELF32_R_TYPE (irel->r_info)
2962 == (int) R_MN10300_GOTOFF32)
2963 ? R_MN10300_GOTOFF16
2964 : (ELF32_R_TYPE (irel->r_info)
2965 == (int) R_MN10300_GOT32)
2966 ? R_MN10300_GOT16
2967 : (ELF32_R_TYPE (irel->r_info)
2968 == (int) R_MN10300_GOTPC32)
2969 ? R_MN10300_GOTPC16 :
2970 R_MN10300_16);
2972 /* Delete two bytes of data. */
2973 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2974 irel->r_offset + 2, 2))
2975 goto error_return;
2977 /* That will change things, so, we should relax again.
2978 Note that this is not required, and it may be slow. */
2979 *again = TRUE;
2980 break;
2982 else if ((code & 0xf0) == 0x80
2983 || (code & 0xf0) == 0x90)
2984 switch (code & 0xf3)
2986 /* mov dn,(abs32) -> mov dn,(abs16)
2987 movbu dn,(abs32) -> movbu dn,(abs16)
2988 movhu dn,(abs32) -> movhu dn,(abs16) */
2989 case 0x81:
2990 case 0x82:
2991 case 0x83:
2992 /* Note that we've changed the relocation contents, etc. */
2993 elf_section_data (sec)->relocs = internal_relocs;
2994 elf_section_data (sec)->this_hdr.contents = contents;
2995 symtab_hdr->contents = (unsigned char *) isymbuf;
2997 if ((code & 0xf3) == 0x81)
2998 code = 0x01 + (code & 0x0c);
2999 else if ((code & 0xf3) == 0x82)
3000 code = 0x02 + (code & 0x0c);
3001 else if ((code & 0xf3) == 0x83)
3002 code = 0x03 + (code & 0x0c);
3003 else
3004 abort ();
3006 /* Fix the opcode. */
3007 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3009 /* Fix the relocation's type. */
3010 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3011 (ELF32_R_TYPE (irel->r_info)
3012 == (int) R_MN10300_GOTOFF32)
3013 ? R_MN10300_GOTOFF16
3014 : (ELF32_R_TYPE (irel->r_info)
3015 == (int) R_MN10300_GOT32)
3016 ? R_MN10300_GOT16
3017 : (ELF32_R_TYPE (irel->r_info)
3018 == (int) R_MN10300_GOTPC32)
3019 ? R_MN10300_GOTPC16 :
3020 R_MN10300_16);
3022 /* The opcode got shorter too, so we have to fix the
3023 addend and offset too! */
3024 irel->r_offset -= 1;
3026 /* Delete three bytes of data. */
3027 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3028 irel->r_offset + 1, 3))
3029 goto error_return;
3031 /* That will change things, so, we should relax again.
3032 Note that this is not required, and it may be slow. */
3033 *again = TRUE;
3034 break;
3036 /* mov am,(abs32) -> mov am,(abs16)
3037 mov am,(d32,sp) -> mov am,(d16,sp)
3038 mov dm,(d32,sp) -> mov dm,(d32,sp)
3039 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3040 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3041 case 0x80:
3042 case 0x90:
3043 case 0x91:
3044 case 0x92:
3045 case 0x93:
3046 /* sp-based offsets are zero-extended. */
3047 if (code >= 0x90 && code <= 0x93
3048 && (long)value < 0)
3049 continue;
3051 /* Note that we've changed the relocation contents, etc. */
3052 elf_section_data (sec)->relocs = internal_relocs;
3053 elf_section_data (sec)->this_hdr.contents = contents;
3054 symtab_hdr->contents = (unsigned char *) isymbuf;
3056 /* Fix the opcode. */
3057 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3058 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3060 /* Fix the relocation's type. */
3061 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3062 (ELF32_R_TYPE (irel->r_info)
3063 == (int) R_MN10300_GOTOFF32)
3064 ? R_MN10300_GOTOFF16
3065 : (ELF32_R_TYPE (irel->r_info)
3066 == (int) R_MN10300_GOT32)
3067 ? R_MN10300_GOT16
3068 : (ELF32_R_TYPE (irel->r_info)
3069 == (int) R_MN10300_GOTPC32)
3070 ? R_MN10300_GOTPC16 :
3071 R_MN10300_16);
3073 /* Delete two bytes of data. */
3074 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3075 irel->r_offset + 2, 2))
3076 goto error_return;
3078 /* That will change things, so, we should relax again.
3079 Note that this is not required, and it may be slow. */
3080 *again = TRUE;
3081 break;
3083 else if ((code & 0xf0) < 0xf0)
3084 switch (code & 0xfc)
3086 /* mov imm32,dn -> mov imm16,dn
3087 mov imm32,an -> mov imm16,an
3088 mov (abs32),dn -> mov (abs16),dn
3089 movbu (abs32),dn -> movbu (abs16),dn
3090 movhu (abs32),dn -> movhu (abs16),dn */
3091 case 0xcc:
3092 case 0xdc:
3093 case 0xa4:
3094 case 0xa8:
3095 case 0xac:
3096 /* Not safe if the high bit is on as relaxing may
3097 move the value out of high mem and thus not fit
3098 in a signed 16bit value. */
3099 if (code == 0xcc
3100 && (value & 0x8000))
3101 continue;
3103 /* mov imm16, an zero-extends the immediate. */
3104 if (code == 0xdc
3105 && (long)value < 0)
3106 continue;
3108 /* Note that we've changed the relocation contents, etc. */
3109 elf_section_data (sec)->relocs = internal_relocs;
3110 elf_section_data (sec)->this_hdr.contents = contents;
3111 symtab_hdr->contents = (unsigned char *) isymbuf;
3113 if ((code & 0xfc) == 0xcc)
3114 code = 0x2c + (code & 0x03);
3115 else if ((code & 0xfc) == 0xdc)
3116 code = 0x24 + (code & 0x03);
3117 else if ((code & 0xfc) == 0xa4)
3118 code = 0x30 + (code & 0x03);
3119 else if ((code & 0xfc) == 0xa8)
3120 code = 0x34 + (code & 0x03);
3121 else if ((code & 0xfc) == 0xac)
3122 code = 0x38 + (code & 0x03);
3123 else
3124 abort ();
3126 /* Fix the opcode. */
3127 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3129 /* Fix the relocation's type. */
3130 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3131 (ELF32_R_TYPE (irel->r_info)
3132 == (int) R_MN10300_GOTOFF32)
3133 ? R_MN10300_GOTOFF16
3134 : (ELF32_R_TYPE (irel->r_info)
3135 == (int) R_MN10300_GOT32)
3136 ? R_MN10300_GOT16
3137 : (ELF32_R_TYPE (irel->r_info)
3138 == (int) R_MN10300_GOTPC32)
3139 ? R_MN10300_GOTPC16 :
3140 R_MN10300_16);
3142 /* The opcode got shorter too, so we have to fix the
3143 addend and offset too! */
3144 irel->r_offset -= 1;
3146 /* Delete three bytes of data. */
3147 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3148 irel->r_offset + 1, 3))
3149 goto error_return;
3151 /* That will change things, so, we should relax again.
3152 Note that this is not required, and it may be slow. */
3153 *again = TRUE;
3154 break;
3156 /* mov (abs32),an -> mov (abs16),an
3157 mov (d32,sp),an -> mov (d16,sp),an
3158 mov (d32,sp),dn -> mov (d16,sp),dn
3159 movbu (d32,sp),dn -> movbu (d16,sp),dn
3160 movhu (d32,sp),dn -> movhu (d16,sp),dn
3161 add imm32,dn -> add imm16,dn
3162 cmp imm32,dn -> cmp imm16,dn
3163 add imm32,an -> add imm16,an
3164 cmp imm32,an -> cmp imm16,an
3165 and imm32,dn -> and imm16,dn
3166 or imm32,dn -> or imm16,dn
3167 xor imm32,dn -> xor imm16,dn
3168 btst imm32,dn -> btst imm16,dn */
3170 case 0xa0:
3171 case 0xb0:
3172 case 0xb1:
3173 case 0xb2:
3174 case 0xb3:
3175 case 0xc0:
3176 case 0xc8:
3178 case 0xd0:
3179 case 0xd8:
3180 case 0xe0:
3181 case 0xe1:
3182 case 0xe2:
3183 case 0xe3:
3184 /* cmp imm16, an zero-extends the immediate. */
3185 if (code == 0xdc
3186 && (long)value < 0)
3187 continue;
3189 /* So do sp-based offsets. */
3190 if (code >= 0xb0 && code <= 0xb3
3191 && (long)value < 0)
3192 continue;
3194 /* Note that we've changed the relocation contents, etc. */
3195 elf_section_data (sec)->relocs = internal_relocs;
3196 elf_section_data (sec)->this_hdr.contents = contents;
3197 symtab_hdr->contents = (unsigned char *) isymbuf;
3199 /* Fix the opcode. */
3200 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3201 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3203 /* Fix the relocation's type. */
3204 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3205 (ELF32_R_TYPE (irel->r_info)
3206 == (int) R_MN10300_GOTOFF32)
3207 ? R_MN10300_GOTOFF16
3208 : (ELF32_R_TYPE (irel->r_info)
3209 == (int) R_MN10300_GOT32)
3210 ? R_MN10300_GOT16
3211 : (ELF32_R_TYPE (irel->r_info)
3212 == (int) R_MN10300_GOTPC32)
3213 ? R_MN10300_GOTPC16 :
3214 R_MN10300_16);
3216 /* Delete two bytes of data. */
3217 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3218 irel->r_offset + 2, 2))
3219 goto error_return;
3221 /* That will change things, so, we should relax again.
3222 Note that this is not required, and it may be slow. */
3223 *again = TRUE;
3224 break;
3226 else if (code == 0xfe)
3228 /* add imm32,sp -> add imm16,sp */
3230 /* Note that we've changed the relocation contents, etc. */
3231 elf_section_data (sec)->relocs = internal_relocs;
3232 elf_section_data (sec)->this_hdr.contents = contents;
3233 symtab_hdr->contents = (unsigned char *) isymbuf;
3235 /* Fix the opcode. */
3236 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3237 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3239 /* Fix the relocation's type. */
3240 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3241 (ELF32_R_TYPE (irel->r_info)
3242 == (int) R_MN10300_GOT32)
3243 ? R_MN10300_GOT16
3244 : (ELF32_R_TYPE (irel->r_info)
3245 == (int) R_MN10300_GOTOFF32)
3246 ? R_MN10300_GOTOFF16
3247 : (ELF32_R_TYPE (irel->r_info)
3248 == (int) R_MN10300_GOTPC32)
3249 ? R_MN10300_GOTPC16 :
3250 R_MN10300_16);
3252 /* Delete two bytes of data. */
3253 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3254 irel->r_offset + 2, 2))
3255 goto error_return;
3257 /* That will change things, so, we should relax again.
3258 Note that this is not required, and it may be slow. */
3259 *again = TRUE;
3260 break;
3266 if (isymbuf != NULL
3267 && symtab_hdr->contents != (unsigned char *) isymbuf)
3269 if (! link_info->keep_memory)
3270 free (isymbuf);
3271 else
3273 /* Cache the symbols for elf_link_input_bfd. */
3274 symtab_hdr->contents = (unsigned char *) isymbuf;
3278 if (contents != NULL
3279 && elf_section_data (sec)->this_hdr.contents != contents)
3281 if (! link_info->keep_memory)
3282 free (contents);
3283 else
3285 /* Cache the section contents for elf_link_input_bfd. */
3286 elf_section_data (sec)->this_hdr.contents = contents;
3290 if (internal_relocs != NULL
3291 && elf_section_data (sec)->relocs != internal_relocs)
3292 free (internal_relocs);
3294 return TRUE;
3296 error_return:
3297 if (isymbuf != NULL
3298 && symtab_hdr->contents != (unsigned char *) isymbuf)
3299 free (isymbuf);
3300 if (contents != NULL
3301 && elf_section_data (section)->this_hdr.contents != contents)
3302 free (contents);
3303 if (internal_relocs != NULL
3304 && elf_section_data (section)->relocs != internal_relocs)
3305 free (internal_relocs);
3307 return FALSE;
3310 /* Compute the stack size and movm arguments for the function
3311 referred to by HASH at address ADDR in section with
3312 contents CONTENTS, store the information in the hash table. */
3313 static void
3314 compute_function_info (abfd, hash, addr, contents)
3315 bfd *abfd;
3316 struct elf32_mn10300_link_hash_entry *hash;
3317 bfd_vma addr;
3318 unsigned char *contents;
3320 unsigned char byte1, byte2;
3321 /* We only care about a very small subset of the possible prologue
3322 sequences here. Basically we look for:
3324 movm [d2,d3,a2,a3],sp (optional)
3325 add <size>,sp (optional, and only for sizes which fit in an unsigned
3326 8 bit number)
3328 If we find anything else, we quit. */
3330 /* Look for movm [regs],sp */
3331 byte1 = bfd_get_8 (abfd, contents + addr);
3332 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3334 if (byte1 == 0xcf)
3336 hash->movm_args = byte2;
3337 addr += 2;
3338 byte1 = bfd_get_8 (abfd, contents + addr);
3339 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3342 /* Now figure out how much stack space will be allocated by the movm
3343 instruction. We need this kept separate from the function's normal
3344 stack space. */
3345 if (hash->movm_args)
3347 /* Space for d2. */
3348 if (hash->movm_args & 0x80)
3349 hash->movm_stack_size += 4;
3351 /* Space for d3. */
3352 if (hash->movm_args & 0x40)
3353 hash->movm_stack_size += 4;
3355 /* Space for a2. */
3356 if (hash->movm_args & 0x20)
3357 hash->movm_stack_size += 4;
3359 /* Space for a3. */
3360 if (hash->movm_args & 0x10)
3361 hash->movm_stack_size += 4;
3363 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
3364 if (hash->movm_args & 0x08)
3365 hash->movm_stack_size += 8 * 4;
3367 if (bfd_get_mach (abfd) == bfd_mach_am33
3368 || bfd_get_mach (abfd) == bfd_mach_am33_2)
3370 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
3371 if (hash->movm_args & 0x1)
3372 hash->movm_stack_size += 6 * 4;
3374 /* exreg1 space. e4, e5, e6, e7 */
3375 if (hash->movm_args & 0x2)
3376 hash->movm_stack_size += 4 * 4;
3378 /* exreg0 space. e2, e3 */
3379 if (hash->movm_args & 0x4)
3380 hash->movm_stack_size += 2 * 4;
3384 /* Now look for the two stack adjustment variants. */
3385 if (byte1 == 0xf8 && byte2 == 0xfe)
3387 int temp = bfd_get_8 (abfd, contents + addr + 2);
3388 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
3390 hash->stack_size = -temp;
3392 else if (byte1 == 0xfa && byte2 == 0xfe)
3394 int temp = bfd_get_16 (abfd, contents + addr + 2);
3395 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
3396 temp = -temp;
3398 if (temp < 255)
3399 hash->stack_size = temp;
3402 /* If the total stack to be allocated by the call instruction is more
3403 than 255 bytes, then we can't remove the stack adjustment by using
3404 "call" (we might still be able to remove the "movm" instruction. */
3405 if (hash->stack_size + hash->movm_stack_size > 255)
3406 hash->stack_size = 0;
3408 return;
3411 /* Delete some bytes from a section while relaxing. */
3413 static bfd_boolean
3414 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
3415 bfd *abfd;
3416 asection *sec;
3417 bfd_vma addr;
3418 int count;
3420 Elf_Internal_Shdr *symtab_hdr;
3421 unsigned int sec_shndx;
3422 bfd_byte *contents;
3423 Elf_Internal_Rela *irel, *irelend;
3424 Elf_Internal_Rela *irelalign;
3425 bfd_vma toaddr;
3426 Elf_Internal_Sym *isym, *isymend;
3427 struct elf_link_hash_entry **sym_hashes;
3428 struct elf_link_hash_entry **end_hashes;
3429 unsigned int symcount;
3431 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3433 contents = elf_section_data (sec)->this_hdr.contents;
3435 /* The deletion must stop at the next ALIGN reloc for an aligment
3436 power larger than the number of bytes we are deleting. */
3438 irelalign = NULL;
3439 toaddr = sec->size;
3441 irel = elf_section_data (sec)->relocs;
3442 irelend = irel + sec->reloc_count;
3444 /* Actually delete the bytes. */
3445 memmove (contents + addr, contents + addr + count,
3446 (size_t) (toaddr - addr - count));
3447 sec->size -= count;
3449 /* Adjust all the relocs. */
3450 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3452 /* Get the new reloc address. */
3453 if ((irel->r_offset > addr
3454 && irel->r_offset < toaddr))
3455 irel->r_offset -= count;
3458 /* Adjust the local symbols defined in this section. */
3459 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3460 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3461 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3463 if (isym->st_shndx == sec_shndx
3464 && isym->st_value > addr
3465 && isym->st_value < toaddr)
3466 isym->st_value -= count;
3469 /* Now adjust the global symbols defined in this section. */
3470 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3471 - symtab_hdr->sh_info);
3472 sym_hashes = elf_sym_hashes (abfd);
3473 end_hashes = sym_hashes + symcount;
3474 for (; sym_hashes < end_hashes; sym_hashes++)
3476 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3477 if ((sym_hash->root.type == bfd_link_hash_defined
3478 || sym_hash->root.type == bfd_link_hash_defweak)
3479 && sym_hash->root.u.def.section == sec
3480 && sym_hash->root.u.def.value > addr
3481 && sym_hash->root.u.def.value < toaddr)
3483 sym_hash->root.u.def.value -= count;
3487 return TRUE;
3490 /* Return TRUE if a symbol exists at the given address, else return
3491 FALSE. */
3492 static bfd_boolean
3493 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
3494 bfd *abfd;
3495 asection *sec;
3496 Elf_Internal_Sym *isym;
3497 bfd_vma addr;
3499 Elf_Internal_Shdr *symtab_hdr;
3500 unsigned int sec_shndx;
3501 Elf_Internal_Sym *isymend;
3502 struct elf_link_hash_entry **sym_hashes;
3503 struct elf_link_hash_entry **end_hashes;
3504 unsigned int symcount;
3506 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3508 /* Examine all the symbols. */
3509 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3510 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3512 if (isym->st_shndx == sec_shndx
3513 && isym->st_value == addr)
3514 return TRUE;
3517 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3518 - symtab_hdr->sh_info);
3519 sym_hashes = elf_sym_hashes (abfd);
3520 end_hashes = sym_hashes + symcount;
3521 for (; sym_hashes < end_hashes; sym_hashes++)
3523 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3524 if ((sym_hash->root.type == bfd_link_hash_defined
3525 || sym_hash->root.type == bfd_link_hash_defweak)
3526 && sym_hash->root.u.def.section == sec
3527 && sym_hash->root.u.def.value == addr)
3528 return TRUE;
3531 return FALSE;
3534 /* This is a version of bfd_generic_get_relocated_section_contents
3535 which uses mn10300_elf_relocate_section. */
3537 static bfd_byte *
3538 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3539 data, relocatable, symbols)
3540 bfd *output_bfd;
3541 struct bfd_link_info *link_info;
3542 struct bfd_link_order *link_order;
3543 bfd_byte *data;
3544 bfd_boolean relocatable;
3545 asymbol **symbols;
3547 Elf_Internal_Shdr *symtab_hdr;
3548 asection *input_section = link_order->u.indirect.section;
3549 bfd *input_bfd = input_section->owner;
3550 asection **sections = NULL;
3551 Elf_Internal_Rela *internal_relocs = NULL;
3552 Elf_Internal_Sym *isymbuf = NULL;
3554 /* We only need to handle the case of relaxing, or of having a
3555 particular set of section contents, specially. */
3556 if (relocatable
3557 || elf_section_data (input_section)->this_hdr.contents == NULL)
3558 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3559 link_order, data,
3560 relocatable,
3561 symbols);
3563 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3565 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3566 (size_t) input_section->size);
3568 if ((input_section->flags & SEC_RELOC) != 0
3569 && input_section->reloc_count > 0)
3571 asection **secpp;
3572 Elf_Internal_Sym *isym, *isymend;
3573 bfd_size_type amt;
3575 internal_relocs = (_bfd_elf_link_read_relocs
3576 (input_bfd, input_section, (PTR) NULL,
3577 (Elf_Internal_Rela *) NULL, FALSE));
3578 if (internal_relocs == NULL)
3579 goto error_return;
3581 if (symtab_hdr->sh_info != 0)
3583 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3584 if (isymbuf == NULL)
3585 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3586 symtab_hdr->sh_info, 0,
3587 NULL, NULL, NULL);
3588 if (isymbuf == NULL)
3589 goto error_return;
3592 amt = symtab_hdr->sh_info;
3593 amt *= sizeof (asection *);
3594 sections = (asection **) bfd_malloc (amt);
3595 if (sections == NULL && amt != 0)
3596 goto error_return;
3598 isymend = isymbuf + symtab_hdr->sh_info;
3599 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3601 asection *isec;
3603 if (isym->st_shndx == SHN_UNDEF)
3604 isec = bfd_und_section_ptr;
3605 else if (isym->st_shndx == SHN_ABS)
3606 isec = bfd_abs_section_ptr;
3607 else if (isym->st_shndx == SHN_COMMON)
3608 isec = bfd_com_section_ptr;
3609 else
3610 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3612 *secpp = isec;
3615 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3616 input_section, data, internal_relocs,
3617 isymbuf, sections))
3618 goto error_return;
3620 if (sections != NULL)
3621 free (sections);
3622 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3623 free (isymbuf);
3624 if (internal_relocs != elf_section_data (input_section)->relocs)
3625 free (internal_relocs);
3628 return data;
3630 error_return:
3631 if (sections != NULL)
3632 free (sections);
3633 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3634 free (isymbuf);
3635 if (internal_relocs != NULL
3636 && internal_relocs != elf_section_data (input_section)->relocs)
3637 free (internal_relocs);
3638 return NULL;
3641 /* Assorted hash table functions. */
3643 /* Initialize an entry in the link hash table. */
3645 /* Create an entry in an MN10300 ELF linker hash table. */
3647 static struct bfd_hash_entry *
3648 elf32_mn10300_link_hash_newfunc (entry, table, string)
3649 struct bfd_hash_entry *entry;
3650 struct bfd_hash_table *table;
3651 const char *string;
3653 struct elf32_mn10300_link_hash_entry *ret =
3654 (struct elf32_mn10300_link_hash_entry *) entry;
3656 /* Allocate the structure if it has not already been allocated by a
3657 subclass. */
3658 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3659 ret = ((struct elf32_mn10300_link_hash_entry *)
3660 bfd_hash_allocate (table,
3661 sizeof (struct elf32_mn10300_link_hash_entry)));
3662 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3663 return (struct bfd_hash_entry *) ret;
3665 /* Call the allocation method of the superclass. */
3666 ret = ((struct elf32_mn10300_link_hash_entry *)
3667 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3668 table, string));
3669 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
3671 ret->direct_calls = 0;
3672 ret->stack_size = 0;
3673 ret->movm_args = 0;
3674 ret->movm_stack_size = 0;
3675 ret->flags = 0;
3678 return (struct bfd_hash_entry *) ret;
3681 /* Create an mn10300 ELF linker hash table. */
3683 static struct bfd_link_hash_table *
3684 elf32_mn10300_link_hash_table_create (abfd)
3685 bfd *abfd;
3687 struct elf32_mn10300_link_hash_table *ret;
3688 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
3690 ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3691 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
3692 return NULL;
3694 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3695 elf32_mn10300_link_hash_newfunc))
3697 free (ret);
3698 return NULL;
3701 ret->flags = 0;
3702 amt = sizeof (struct elf_link_hash_table);
3703 ret->static_hash_table
3704 = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3705 if (ret->static_hash_table == NULL)
3707 free (ret);
3708 return NULL;
3711 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3712 elf32_mn10300_link_hash_newfunc))
3714 free (ret->static_hash_table);
3715 free (ret);
3716 return NULL;
3718 return &ret->root.root;
3721 /* Free an mn10300 ELF linker hash table. */
3723 static void
3724 elf32_mn10300_link_hash_table_free (hash)
3725 struct bfd_link_hash_table *hash;
3727 struct elf32_mn10300_link_hash_table *ret
3728 = (struct elf32_mn10300_link_hash_table *) hash;
3730 _bfd_generic_link_hash_table_free
3731 ((struct bfd_link_hash_table *) ret->static_hash_table);
3732 _bfd_generic_link_hash_table_free
3733 ((struct bfd_link_hash_table *) ret);
3736 static unsigned long
3737 elf_mn10300_mach (flags)
3738 flagword flags;
3740 switch (flags & EF_MN10300_MACH)
3742 case E_MN10300_MACH_MN10300:
3743 default:
3744 return bfd_mach_mn10300;
3746 case E_MN10300_MACH_AM33:
3747 return bfd_mach_am33;
3749 case E_MN10300_MACH_AM33_2:
3750 return bfd_mach_am33_2;
3754 /* The final processing done just before writing out a MN10300 ELF object
3755 file. This gets the MN10300 architecture right based on the machine
3756 number. */
3758 void
3759 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3760 bfd *abfd;
3761 bfd_boolean linker ATTRIBUTE_UNUSED;
3763 unsigned long val;
3765 switch (bfd_get_mach (abfd))
3767 default:
3768 case bfd_mach_mn10300:
3769 val = E_MN10300_MACH_MN10300;
3770 break;
3772 case bfd_mach_am33:
3773 val = E_MN10300_MACH_AM33;
3774 break;
3776 case bfd_mach_am33_2:
3777 val = E_MN10300_MACH_AM33_2;
3778 break;
3781 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3782 elf_elfheader (abfd)->e_flags |= val;
3785 bfd_boolean
3786 _bfd_mn10300_elf_object_p (abfd)
3787 bfd *abfd;
3789 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3790 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3791 return TRUE;
3794 /* Merge backend specific data from an object file to the output
3795 object file when linking. */
3797 bfd_boolean
3798 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3799 bfd *ibfd;
3800 bfd *obfd;
3802 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3803 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3804 return TRUE;
3806 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3807 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3809 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3810 bfd_get_mach (ibfd)))
3811 return FALSE;
3814 return TRUE;
3817 #define PLT0_ENTRY_SIZE 15
3818 #define PLT_ENTRY_SIZE 20
3819 #define PIC_PLT_ENTRY_SIZE 24
3821 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
3823 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
3824 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
3825 0xf0, 0xf4, /* jmp (a0) */
3828 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
3830 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
3831 0xf0, 0xf4, /* jmp (a0) */
3832 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3833 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
3836 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
3838 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
3839 0xf0, 0xf4, /* jmp (a0) */
3840 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3841 0xf8, 0x22, 8, /* mov (8,a2),a0 */
3842 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
3843 0xf0, 0xf4, /* jmp (a0) */
3846 /* Return size of the first PLT entry. */
3847 #define elf_mn10300_sizeof_plt0(info) \
3848 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
3850 /* Return size of a PLT entry. */
3851 #define elf_mn10300_sizeof_plt(info) \
3852 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
3854 /* Return offset of the PLT0 address in an absolute PLT entry. */
3855 #define elf_mn10300_plt_plt0_offset(info) 16
3857 /* Return offset of the linker in PLT0 entry. */
3858 #define elf_mn10300_plt0_linker_offset(info) 2
3860 /* Return offset of the GOT id in PLT0 entry. */
3861 #define elf_mn10300_plt0_gotid_offset(info) 9
3863 /* Return offset of the temporary in PLT entry */
3864 #define elf_mn10300_plt_temp_offset(info) 8
3866 /* Return offset of the symbol in PLT entry. */
3867 #define elf_mn10300_plt_symbol_offset(info) 2
3869 /* Return offset of the relocation in PLT entry. */
3870 #define elf_mn10300_plt_reloc_offset(info) 11
3872 /* The name of the dynamic interpreter. This is put in the .interp
3873 section. */
3875 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
3877 /* Create dynamic sections when linking against a dynamic object. */
3879 static bfd_boolean
3880 _bfd_mn10300_elf_create_dynamic_sections (abfd, info)
3881 bfd *abfd;
3882 struct bfd_link_info *info;
3884 flagword flags;
3885 asection * s;
3886 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
3887 int ptralign = 0;
3889 switch (bed->s->arch_size)
3891 case 32:
3892 ptralign = 2;
3893 break;
3895 case 64:
3896 ptralign = 3;
3897 break;
3899 default:
3900 bfd_set_error (bfd_error_bad_value);
3901 return FALSE;
3904 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3905 .rel[a].bss sections. */
3907 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3908 | SEC_LINKER_CREATED);
3910 s = bfd_make_section_with_flags (abfd,
3911 (bed->default_use_rela_p
3912 ? ".rela.plt" : ".rel.plt"),
3913 flags | SEC_READONLY);
3914 if (s == NULL
3915 || ! bfd_set_section_alignment (abfd, s, ptralign))
3916 return FALSE;
3918 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
3919 return FALSE;
3922 const char * secname;
3923 char * relname;
3924 flagword secflags;
3925 asection * sec;
3927 for (sec = abfd->sections; sec; sec = sec->next)
3929 secflags = bfd_get_section_flags (abfd, sec);
3930 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3931 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3932 continue;
3934 secname = bfd_get_section_name (abfd, sec);
3935 relname = (char *) bfd_malloc (strlen (secname) + 6);
3936 strcpy (relname, ".rela");
3937 strcat (relname, secname);
3939 s = bfd_make_section_with_flags (abfd, relname,
3940 flags | SEC_READONLY);
3941 if (s == NULL
3942 || ! bfd_set_section_alignment (abfd, s, ptralign))
3943 return FALSE;
3947 if (bed->want_dynbss)
3949 /* The .dynbss section is a place to put symbols which are defined
3950 by dynamic objects, are referenced by regular objects, and are
3951 not functions. We must allocate space for them in the process
3952 image and use a R_*_COPY reloc to tell the dynamic linker to
3953 initialize them at run time. The linker script puts the .dynbss
3954 section into the .bss section of the final image. */
3955 s = bfd_make_section_with_flags (abfd, ".dynbss",
3956 SEC_ALLOC | SEC_LINKER_CREATED);
3957 if (s == NULL)
3958 return FALSE;
3960 /* The .rel[a].bss section holds copy relocs. This section is not
3961 normally needed. We need to create it here, though, so that the
3962 linker will map it to an output section. We can't just create it
3963 only if we need it, because we will not know whether we need it
3964 until we have seen all the input files, and the first time the
3965 main linker code calls BFD after examining all the input files
3966 (size_dynamic_sections) the input sections have already been
3967 mapped to the output sections. If the section turns out not to
3968 be needed, we can discard it later. We will never need this
3969 section when generating a shared object, since they do not use
3970 copy relocs. */
3971 if (! info->shared)
3973 s = bfd_make_section_with_flags (abfd,
3974 (bed->default_use_rela_p
3975 ? ".rela.bss" : ".rel.bss"),
3976 flags | SEC_READONLY);
3977 if (s == NULL
3978 || ! bfd_set_section_alignment (abfd, s, ptralign))
3979 return FALSE;
3983 return TRUE;
3986 /* Adjust a symbol defined by a dynamic object and referenced by a
3987 regular object. The current definition is in some section of the
3988 dynamic object, but we're not including those sections. We have to
3989 change the definition to something the rest of the link can
3990 understand. */
3992 static bfd_boolean
3993 _bfd_mn10300_elf_adjust_dynamic_symbol (info, h)
3994 struct bfd_link_info * info;
3995 struct elf_link_hash_entry * h;
3997 bfd * dynobj;
3998 asection * s;
3999 unsigned int power_of_two;
4001 dynobj = elf_hash_table (info)->dynobj;
4003 /* Make sure we know what is going on here. */
4004 BFD_ASSERT (dynobj != NULL
4005 && (h->needs_plt
4006 || h->u.weakdef != NULL
4007 || (h->def_dynamic
4008 && h->ref_regular
4009 && !h->def_regular)));
4011 /* If this is a function, put it in the procedure linkage table. We
4012 will fill in the contents of the procedure linkage table later,
4013 when we know the address of the .got section. */
4014 if (h->type == STT_FUNC
4015 || h->needs_plt)
4017 if (! info->shared
4018 && !h->def_dynamic
4019 && !h->ref_dynamic)
4021 /* This case can occur if we saw a PLT reloc in an input
4022 file, but the symbol was never referred to by a dynamic
4023 object. In such a case, we don't actually need to build
4024 a procedure linkage table, and we can just do a REL32
4025 reloc instead. */
4026 BFD_ASSERT (h->needs_plt);
4027 return TRUE;
4030 /* Make sure this symbol is output as a dynamic symbol. */
4031 if (h->dynindx == -1)
4033 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4034 return FALSE;
4037 s = bfd_get_section_by_name (dynobj, ".plt");
4038 BFD_ASSERT (s != NULL);
4040 /* If this is the first .plt entry, make room for the special
4041 first entry. */
4042 if (s->size == 0)
4043 s->size += elf_mn10300_sizeof_plt0 (info);
4045 /* If this symbol is not defined in a regular file, and we are
4046 not generating a shared library, then set the symbol to this
4047 location in the .plt. This is required to make function
4048 pointers compare as equal between the normal executable and
4049 the shared library. */
4050 if (! info->shared
4051 && !h->def_regular)
4053 h->root.u.def.section = s;
4054 h->root.u.def.value = s->size;
4057 h->plt.offset = s->size;
4059 /* Make room for this entry. */
4060 s->size += elf_mn10300_sizeof_plt (info);
4062 /* We also need to make an entry in the .got.plt section, which
4063 will be placed in the .got section by the linker script. */
4065 s = bfd_get_section_by_name (dynobj, ".got.plt");
4066 BFD_ASSERT (s != NULL);
4067 s->size += 4;
4069 /* We also need to make an entry in the .rela.plt section. */
4071 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4072 BFD_ASSERT (s != NULL);
4073 s->size += sizeof (Elf32_External_Rela);
4075 return TRUE;
4078 /* If this is a weak symbol, and there is a real definition, the
4079 processor independent code will have arranged for us to see the
4080 real definition first, and we can just use the same value. */
4081 if (h->u.weakdef != NULL)
4083 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4084 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4085 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4086 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4087 return TRUE;
4090 /* This is a reference to a symbol defined by a dynamic object which
4091 is not a function. */
4093 /* If we are creating a shared library, we must presume that the
4094 only references to the symbol are via the global offset table.
4095 For such cases we need not do anything here; the relocations will
4096 be handled correctly by relocate_section. */
4097 if (info->shared)
4098 return TRUE;
4100 /* If there are no references to this symbol that do not use the
4101 GOT, we don't need to generate a copy reloc. */
4102 if (!h->non_got_ref)
4103 return TRUE;
4105 if (h->size == 0)
4107 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4108 h->root.root.string);
4109 return TRUE;
4112 /* We must allocate the symbol in our .dynbss section, which will
4113 become part of the .bss section of the executable. There will be
4114 an entry for this symbol in the .dynsym section. The dynamic
4115 object will contain position independent code, so all references
4116 from the dynamic object to this symbol will go through the global
4117 offset table. The dynamic linker will use the .dynsym entry to
4118 determine the address it must put in the global offset table, so
4119 both the dynamic object and the regular object will refer to the
4120 same memory location for the variable. */
4122 s = bfd_get_section_by_name (dynobj, ".dynbss");
4123 BFD_ASSERT (s != NULL);
4125 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4126 copy the initial value out of the dynamic object and into the
4127 runtime process image. We need to remember the offset into the
4128 .rela.bss section we are going to use. */
4129 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4131 asection * srel;
4133 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4134 BFD_ASSERT (srel != NULL);
4135 srel->size += sizeof (Elf32_External_Rela);
4136 h->needs_copy = 1;
4139 /* We need to figure out the alignment required for this symbol. I
4140 have no idea how ELF linkers handle this. */
4141 power_of_two = bfd_log2 (h->size);
4142 if (power_of_two > 3)
4143 power_of_two = 3;
4145 /* Apply the required alignment. */
4146 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4147 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4149 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4150 return FALSE;
4153 /* Define the symbol as being at this point in the section. */
4154 h->root.u.def.section = s;
4155 h->root.u.def.value = s->size;
4157 /* Increment the section size to make room for the symbol. */
4158 s->size += h->size;
4160 return TRUE;
4163 /* Set the sizes of the dynamic sections. */
4165 static bfd_boolean
4166 _bfd_mn10300_elf_size_dynamic_sections (output_bfd, info)
4167 bfd * output_bfd;
4168 struct bfd_link_info * info;
4170 bfd * dynobj;
4171 asection * s;
4172 bfd_boolean plt;
4173 bfd_boolean relocs;
4174 bfd_boolean reltext;
4176 dynobj = elf_hash_table (info)->dynobj;
4177 BFD_ASSERT (dynobj != NULL);
4179 if (elf_hash_table (info)->dynamic_sections_created)
4181 /* Set the contents of the .interp section to the interpreter. */
4182 if (info->executable)
4184 s = bfd_get_section_by_name (dynobj, ".interp");
4185 BFD_ASSERT (s != NULL);
4186 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4187 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4190 else
4192 /* We may have created entries in the .rela.got section.
4193 However, if we are not creating the dynamic sections, we will
4194 not actually use these entries. Reset the size of .rela.got,
4195 which will cause it to get stripped from the output file
4196 below. */
4197 s = bfd_get_section_by_name (dynobj, ".rela.got");
4198 if (s != NULL)
4199 s->size = 0;
4202 /* The check_relocs and adjust_dynamic_symbol entry points have
4203 determined the sizes of the various dynamic sections. Allocate
4204 memory for them. */
4205 plt = FALSE;
4206 relocs = FALSE;
4207 reltext = FALSE;
4208 for (s = dynobj->sections; s != NULL; s = s->next)
4210 const char * name;
4212 if ((s->flags & SEC_LINKER_CREATED) == 0)
4213 continue;
4215 /* It's OK to base decisions on the section name, because none
4216 of the dynobj section names depend upon the input files. */
4217 name = bfd_get_section_name (dynobj, s);
4219 if (strcmp (name, ".plt") == 0)
4221 /* Remember whether there is a PLT. */
4222 plt = s->size != 0;
4224 else if (strncmp (name, ".rela", 5) == 0)
4226 if (s->size != 0)
4228 asection * target;
4230 /* Remember whether there are any reloc sections other
4231 than .rela.plt. */
4232 if (strcmp (name, ".rela.plt") != 0)
4234 const char * outname;
4236 relocs = TRUE;
4238 /* If this relocation section applies to a read only
4239 section, then we probably need a DT_TEXTREL
4240 entry. The entries in the .rela.plt section
4241 really apply to the .got section, which we
4242 created ourselves and so know is not readonly. */
4243 outname = bfd_get_section_name (output_bfd,
4244 s->output_section);
4245 target = bfd_get_section_by_name (output_bfd, outname + 5);
4246 if (target != NULL
4247 && (target->flags & SEC_READONLY) != 0
4248 && (target->flags & SEC_ALLOC) != 0)
4249 reltext = TRUE;
4252 /* We use the reloc_count field as a counter if we need
4253 to copy relocs into the output file. */
4254 s->reloc_count = 0;
4257 else if (strncmp (name, ".got", 4) != 0
4258 && strcmp (name, ".dynbss") != 0)
4259 /* It's not one of our sections, so don't allocate space. */
4260 continue;
4262 if (s->size == 0)
4264 /* If we don't need this section, strip it from the
4265 output file. This is mostly to handle .rela.bss and
4266 .rela.plt. We must create both sections in
4267 create_dynamic_sections, because they must be created
4268 before the linker maps input sections to output
4269 sections. The linker does that before
4270 adjust_dynamic_symbol is called, and it is that
4271 function which decides whether anything needs to go
4272 into these sections. */
4273 s->flags |= SEC_EXCLUDE;
4274 continue;
4277 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4278 continue;
4280 /* Allocate memory for the section contents. We use bfd_zalloc
4281 here in case unused entries are not reclaimed before the
4282 section's contents are written out. This should not happen,
4283 but this way if it does, we get a R_MN10300_NONE reloc
4284 instead of garbage. */
4285 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4286 if (s->contents == NULL)
4287 return FALSE;
4290 if (elf_hash_table (info)->dynamic_sections_created)
4292 /* Add some entries to the .dynamic section. We fill in the
4293 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4294 but we must add the entries now so that we get the correct
4295 size for the .dynamic section. The DT_DEBUG entry is filled
4296 in by the dynamic linker and used by the debugger. */
4297 if (! info->shared)
4299 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4300 return FALSE;
4303 if (plt)
4305 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4306 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4307 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4308 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4309 return FALSE;
4312 if (relocs)
4314 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4315 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4316 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4317 sizeof (Elf32_External_Rela)))
4318 return FALSE;
4321 if (reltext)
4323 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4324 return FALSE;
4328 return TRUE;
4331 /* Finish up dynamic symbol handling. We set the contents of various
4332 dynamic sections here. */
4334 static bfd_boolean
4335 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4336 bfd * output_bfd;
4337 struct bfd_link_info * info;
4338 struct elf_link_hash_entry * h;
4339 Elf_Internal_Sym * sym;
4341 bfd * dynobj;
4343 dynobj = elf_hash_table (info)->dynobj;
4345 if (h->plt.offset != (bfd_vma) -1)
4347 asection * splt;
4348 asection * sgot;
4349 asection * srel;
4350 bfd_vma plt_index;
4351 bfd_vma got_offset;
4352 Elf_Internal_Rela rel;
4354 /* This symbol has an entry in the procedure linkage table. Set
4355 it up. */
4357 BFD_ASSERT (h->dynindx != -1);
4359 splt = bfd_get_section_by_name (dynobj, ".plt");
4360 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4361 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4362 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4364 /* Get the index in the procedure linkage table which
4365 corresponds to this symbol. This is the index of this symbol
4366 in all the symbols for which we are making plt entries. The
4367 first entry in the procedure linkage table is reserved. */
4368 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4369 / elf_mn10300_sizeof_plt (info));
4371 /* Get the offset into the .got table of the entry that
4372 corresponds to this function. Each .got entry is 4 bytes.
4373 The first three are reserved. */
4374 got_offset = (plt_index + 3) * 4;
4376 /* Fill in the entry in the procedure linkage table. */
4377 if (! info->shared)
4379 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4380 elf_mn10300_sizeof_plt (info));
4381 bfd_put_32 (output_bfd,
4382 (sgot->output_section->vma
4383 + sgot->output_offset
4384 + got_offset),
4385 (splt->contents + h->plt.offset
4386 + elf_mn10300_plt_symbol_offset (info)));
4388 bfd_put_32 (output_bfd,
4389 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4390 (splt->contents + h->plt.offset
4391 + elf_mn10300_plt_plt0_offset (info)));
4393 else
4395 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4396 elf_mn10300_sizeof_plt (info));
4398 bfd_put_32 (output_bfd, got_offset,
4399 (splt->contents + h->plt.offset
4400 + elf_mn10300_plt_symbol_offset (info)));
4403 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4404 (splt->contents + h->plt.offset
4405 + elf_mn10300_plt_reloc_offset (info)));
4407 /* Fill in the entry in the global offset table. */
4408 bfd_put_32 (output_bfd,
4409 (splt->output_section->vma
4410 + splt->output_offset
4411 + h->plt.offset
4412 + elf_mn10300_plt_temp_offset (info)),
4413 sgot->contents + got_offset);
4415 /* Fill in the entry in the .rela.plt section. */
4416 rel.r_offset = (sgot->output_section->vma
4417 + sgot->output_offset
4418 + got_offset);
4419 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4420 rel.r_addend = 0;
4421 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4422 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4423 + plt_index));
4425 if (!h->def_regular)
4426 /* Mark the symbol as undefined, rather than as defined in
4427 the .plt section. Leave the value alone. */
4428 sym->st_shndx = SHN_UNDEF;
4431 if (h->got.offset != (bfd_vma) -1)
4433 asection * sgot;
4434 asection * srel;
4435 Elf_Internal_Rela rel;
4437 /* This symbol has an entry in the global offset table. Set it up. */
4439 sgot = bfd_get_section_by_name (dynobj, ".got");
4440 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4441 BFD_ASSERT (sgot != NULL && srel != NULL);
4443 rel.r_offset = (sgot->output_section->vma
4444 + sgot->output_offset
4445 + (h->got.offset &~ 1));
4447 /* If this is a -Bsymbolic link, and the symbol is defined
4448 locally, we just want to emit a RELATIVE reloc. Likewise if
4449 the symbol was forced to be local because of a version file.
4450 The entry in the global offset table will already have been
4451 initialized in the relocate_section function. */
4452 if (info->shared
4453 && (info->symbolic || h->dynindx == -1)
4454 && h->def_regular)
4456 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4457 rel.r_addend = (h->root.u.def.value
4458 + h->root.u.def.section->output_section->vma
4459 + h->root.u.def.section->output_offset);
4461 else
4463 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4464 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4465 rel.r_addend = 0;
4468 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4469 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4470 + srel->reloc_count));
4471 ++ srel->reloc_count;
4474 if (h->needs_copy)
4476 asection * s;
4477 Elf_Internal_Rela rel;
4479 /* This symbol needs a copy reloc. Set it up. */
4480 BFD_ASSERT (h->dynindx != -1
4481 && (h->root.type == bfd_link_hash_defined
4482 || h->root.type == bfd_link_hash_defweak));
4484 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4485 ".rela.bss");
4486 BFD_ASSERT (s != NULL);
4488 rel.r_offset = (h->root.u.def.value
4489 + h->root.u.def.section->output_section->vma
4490 + h->root.u.def.section->output_offset);
4491 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4492 rel.r_addend = 0;
4493 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4494 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4495 + s->reloc_count));
4496 ++ s->reloc_count;
4499 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4500 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4501 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4502 sym->st_shndx = SHN_ABS;
4504 return TRUE;
4507 /* Finish up the dynamic sections. */
4509 static bfd_boolean
4510 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd, info)
4511 bfd * output_bfd;
4512 struct bfd_link_info * info;
4514 bfd * dynobj;
4515 asection * sgot;
4516 asection * sdyn;
4518 dynobj = elf_hash_table (info)->dynobj;
4520 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4521 BFD_ASSERT (sgot != NULL);
4522 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4524 if (elf_hash_table (info)->dynamic_sections_created)
4526 asection * splt;
4527 Elf32_External_Dyn * dyncon;
4528 Elf32_External_Dyn * dynconend;
4530 BFD_ASSERT (sdyn != NULL);
4532 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4533 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4535 for (; dyncon < dynconend; dyncon++)
4537 Elf_Internal_Dyn dyn;
4538 const char * name;
4539 asection * s;
4541 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4543 switch (dyn.d_tag)
4545 default:
4546 break;
4548 case DT_PLTGOT:
4549 name = ".got";
4550 goto get_vma;
4552 case DT_JMPREL:
4553 name = ".rela.plt";
4554 get_vma:
4555 s = bfd_get_section_by_name (output_bfd, name);
4556 BFD_ASSERT (s != NULL);
4557 dyn.d_un.d_ptr = s->vma;
4558 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4559 break;
4561 case DT_PLTRELSZ:
4562 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4563 BFD_ASSERT (s != NULL);
4564 dyn.d_un.d_val = s->size;
4565 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4566 break;
4568 case DT_RELASZ:
4569 /* My reading of the SVR4 ABI indicates that the
4570 procedure linkage table relocs (DT_JMPREL) should be
4571 included in the overall relocs (DT_RELA). This is
4572 what Solaris does. However, UnixWare can not handle
4573 that case. Therefore, we override the DT_RELASZ entry
4574 here to make it not include the JMPREL relocs. Since
4575 the linker script arranges for .rela.plt to follow all
4576 other relocation sections, we don't have to worry
4577 about changing the DT_RELA entry. */
4578 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4579 if (s != NULL)
4580 dyn.d_un.d_val -= s->size;
4581 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4582 break;
4586 /* Fill in the first entry in the procedure linkage table. */
4587 splt = bfd_get_section_by_name (dynobj, ".plt");
4588 if (splt && splt->size > 0)
4590 if (info->shared)
4592 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4593 elf_mn10300_sizeof_plt (info));
4595 else
4597 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4598 bfd_put_32 (output_bfd,
4599 sgot->output_section->vma + sgot->output_offset + 4,
4600 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4601 bfd_put_32 (output_bfd,
4602 sgot->output_section->vma + sgot->output_offset + 8,
4603 splt->contents + elf_mn10300_plt0_linker_offset (info));
4606 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4607 really seem like the right value. */
4608 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4612 /* Fill in the first three entries in the global offset table. */
4613 if (sgot->size > 0)
4615 if (sdyn == NULL)
4616 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4617 else
4618 bfd_put_32 (output_bfd,
4619 sdyn->output_section->vma + sdyn->output_offset,
4620 sgot->contents);
4621 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4622 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4625 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4627 return TRUE;
4630 /* Classify relocation types, such that combreloc can sort them
4631 properly. */
4633 static enum elf_reloc_type_class
4634 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4636 switch ((int) ELF32_R_TYPE (rela->r_info))
4638 case R_MN10300_RELATIVE:
4639 return reloc_class_relative;
4640 case R_MN10300_JMP_SLOT:
4641 return reloc_class_plt;
4642 case R_MN10300_COPY:
4643 return reloc_class_copy;
4644 default:
4645 return reloc_class_normal;
4649 #ifndef ELF_ARCH
4650 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4651 #define TARGET_LITTLE_NAME "elf32-mn10300"
4652 #define ELF_ARCH bfd_arch_mn10300
4653 #define ELF_MACHINE_CODE EM_MN10300
4654 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4655 #define ELF_MAXPAGESIZE 0x1000
4656 #endif
4658 #define elf_info_to_howto mn10300_info_to_howto
4659 #define elf_info_to_howto_rel 0
4660 #define elf_backend_can_gc_sections 1
4661 #define elf_backend_rela_normal 1
4662 #define elf_backend_check_relocs mn10300_elf_check_relocs
4663 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4664 #define elf_backend_relocate_section mn10300_elf_relocate_section
4665 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4666 #define bfd_elf32_bfd_get_relocated_section_contents \
4667 mn10300_elf_get_relocated_section_contents
4668 #define bfd_elf32_bfd_link_hash_table_create \
4669 elf32_mn10300_link_hash_table_create
4670 #define bfd_elf32_bfd_link_hash_table_free \
4671 elf32_mn10300_link_hash_table_free
4673 #ifndef elf_symbol_leading_char
4674 #define elf_symbol_leading_char '_'
4675 #endif
4677 /* So we can set bits in e_flags. */
4678 #define elf_backend_final_write_processing \
4679 _bfd_mn10300_elf_final_write_processing
4680 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4682 #define bfd_elf32_bfd_merge_private_bfd_data \
4683 _bfd_mn10300_elf_merge_private_bfd_data
4685 #define elf_backend_can_gc_sections 1
4686 #define elf_backend_create_dynamic_sections \
4687 _bfd_mn10300_elf_create_dynamic_sections
4688 #define elf_backend_adjust_dynamic_symbol \
4689 _bfd_mn10300_elf_adjust_dynamic_symbol
4690 #define elf_backend_size_dynamic_sections \
4691 _bfd_mn10300_elf_size_dynamic_sections
4692 #define elf_backend_finish_dynamic_symbol \
4693 _bfd_mn10300_elf_finish_dynamic_symbol
4694 #define elf_backend_finish_dynamic_sections \
4695 _bfd_mn10300_elf_finish_dynamic_sections
4697 #define elf_backend_reloc_type_class \
4698 _bfd_mn10300_elf_reloc_type_class
4700 #define elf_backend_want_got_plt 1
4701 #define elf_backend_plt_readonly 1
4702 #define elf_backend_want_plt_sym 0
4703 #define elf_backend_got_header_size 12
4705 #include "elf32-target.h"