gas/
[binutils.git] / bfd / elf-m10300.c
blobc9a9848f46767b091f8e6492f0f1fd1f79e8b3b0
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 bfd_link_hash_entry * bh;
546 struct elf_link_hash_entry * h;
547 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
548 int ptralign;
550 /* This function may be called more than once. */
551 if (bfd_get_section_by_name (abfd, ".got") != NULL)
552 return TRUE;
554 switch (bed->s->arch_size)
556 case 32:
557 ptralign = 2;
558 break;
560 case 64:
561 ptralign = 3;
562 break;
564 default:
565 bfd_set_error (bfd_error_bad_value);
566 return FALSE;
569 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
570 | SEC_LINKER_CREATED);
572 pltflags = flags;
573 pltflags |= SEC_CODE;
574 if (bed->plt_not_loaded)
575 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
576 if (bed->plt_readonly)
577 pltflags |= SEC_READONLY;
579 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
580 if (s == NULL
581 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
582 return FALSE;
584 if (bed->want_plt_sym)
586 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
587 .plt section. */
588 bh = NULL;
589 if (! (_bfd_generic_link_add_one_symbol
590 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
591 (bfd_vma) 0, (const char *) NULL, FALSE,
592 get_elf_backend_data (abfd)->collect, &bh)))
593 return FALSE;
594 h = (struct elf_link_hash_entry *) bh;
595 h->def_regular = 1;
596 h->type = STT_OBJECT;
598 if (info->shared
599 && ! bfd_elf_link_record_dynamic_symbol (info, h))
600 return FALSE;
603 s = bfd_make_section_with_flags (abfd, ".got", flags);
604 if (s == NULL
605 || ! bfd_set_section_alignment (abfd, s, ptralign))
606 return FALSE;
608 if (bed->want_got_plt)
610 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
611 if (s == NULL
612 || ! bfd_set_section_alignment (abfd, s, ptralign))
613 return FALSE;
616 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
617 (or .got.plt) section. We don't do this in the linker script
618 because we don't want to define the symbol if we are not creating
619 a global offset table. */
620 bh = NULL;
621 if (!(_bfd_generic_link_add_one_symbol
622 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
623 bed->got_symbol_offset, (const char *) NULL, FALSE,
624 bed->collect, &bh)))
625 return FALSE;
626 h = (struct elf_link_hash_entry *) bh;
627 h->def_regular = 1;
628 h->type = STT_OBJECT;
630 if (info->shared
631 && ! bfd_elf_link_record_dynamic_symbol (info, h))
632 return FALSE;
634 elf_hash_table (info)->hgot = h;
636 /* The first bit of the global offset table is the header. */
637 s->size += bed->got_header_size + bed->got_symbol_offset;
639 return TRUE;
642 static reloc_howto_type *
643 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
644 bfd *abfd ATTRIBUTE_UNUSED;
645 bfd_reloc_code_real_type code;
647 unsigned int i;
649 for (i = 0;
650 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
651 i++)
653 if (mn10300_reloc_map[i].bfd_reloc_val == code)
654 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
657 return NULL;
660 /* Set the howto pointer for an MN10300 ELF reloc. */
662 static void
663 mn10300_info_to_howto (abfd, cache_ptr, dst)
664 bfd *abfd ATTRIBUTE_UNUSED;
665 arelent *cache_ptr;
666 Elf_Internal_Rela *dst;
668 unsigned int r_type;
670 r_type = ELF32_R_TYPE (dst->r_info);
671 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
672 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
675 /* Look through the relocs for a section during the first phase.
676 Since we don't do .gots or .plts, we just need to consider the
677 virtual table relocs for gc. */
679 static bfd_boolean
680 mn10300_elf_check_relocs (abfd, info, sec, relocs)
681 bfd *abfd;
682 struct bfd_link_info *info;
683 asection *sec;
684 const Elf_Internal_Rela *relocs;
686 Elf_Internal_Shdr *symtab_hdr;
687 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
688 const Elf_Internal_Rela *rel;
689 const Elf_Internal_Rela *rel_end;
690 bfd * dynobj;
691 bfd_vma * local_got_offsets;
692 asection * sgot;
693 asection * srelgot;
694 asection * sreloc;
696 sgot = NULL;
697 srelgot = NULL;
698 sreloc = NULL;
700 if (info->relocatable)
701 return TRUE;
703 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
704 sym_hashes = elf_sym_hashes (abfd);
705 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
706 if (!elf_bad_symtab (abfd))
707 sym_hashes_end -= symtab_hdr->sh_info;
709 dynobj = elf_hash_table (info)->dynobj;
710 local_got_offsets = elf_local_got_offsets (abfd);
711 rel_end = relocs + sec->reloc_count;
712 for (rel = relocs; rel < rel_end; rel++)
714 struct elf_link_hash_entry *h;
715 unsigned long r_symndx;
717 r_symndx = ELF32_R_SYM (rel->r_info);
718 if (r_symndx < symtab_hdr->sh_info)
719 h = NULL;
720 else
721 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
723 /* Some relocs require a global offset table. */
724 if (dynobj == NULL)
726 switch (ELF32_R_TYPE (rel->r_info))
728 case R_MN10300_GOT32:
729 case R_MN10300_GOT24:
730 case R_MN10300_GOT16:
731 case R_MN10300_GOTOFF32:
732 case R_MN10300_GOTOFF24:
733 case R_MN10300_GOTOFF16:
734 case R_MN10300_GOTPC32:
735 case R_MN10300_GOTPC16:
736 elf_hash_table (info)->dynobj = dynobj = abfd;
737 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
738 return FALSE;
739 break;
741 default:
742 break;
746 switch (ELF32_R_TYPE (rel->r_info))
748 /* This relocation describes the C++ object vtable hierarchy.
749 Reconstruct it for later use during GC. */
750 case R_MN10300_GNU_VTINHERIT:
751 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
752 return FALSE;
753 break;
755 /* This relocation describes which C++ vtable entries are actually
756 used. Record for later use during GC. */
757 case R_MN10300_GNU_VTENTRY:
758 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
759 return FALSE;
760 break;
761 case R_MN10300_GOT32:
762 case R_MN10300_GOT24:
763 case R_MN10300_GOT16:
764 /* This symbol requires a global offset table entry. */
766 if (sgot == NULL)
768 sgot = bfd_get_section_by_name (dynobj, ".got");
769 BFD_ASSERT (sgot != NULL);
772 if (srelgot == NULL
773 && (h != NULL || info->shared))
775 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
776 if (srelgot == NULL)
778 srelgot = bfd_make_section_with_flags (dynobj,
779 ".rela.got",
780 (SEC_ALLOC
781 | SEC_LOAD
782 | SEC_HAS_CONTENTS
783 | SEC_IN_MEMORY
784 | SEC_LINKER_CREATED
785 | SEC_READONLY));
786 if (srelgot == NULL
787 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
788 return FALSE;
792 if (h != NULL)
794 if (h->got.offset != (bfd_vma) -1)
795 /* We have already allocated space in the .got. */
796 break;
798 h->got.offset = sgot->size;
800 /* Make sure this symbol is output as a dynamic symbol. */
801 if (h->dynindx == -1)
803 if (! bfd_elf_link_record_dynamic_symbol (info, h))
804 return FALSE;
807 srelgot->size += sizeof (Elf32_External_Rela);
809 else
811 /* This is a global offset table entry for a local
812 symbol. */
813 if (local_got_offsets == NULL)
815 size_t size;
816 unsigned int i;
818 size = symtab_hdr->sh_info * sizeof (bfd_vma);
819 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
821 if (local_got_offsets == NULL)
822 return FALSE;
823 elf_local_got_offsets (abfd) = local_got_offsets;
825 for (i = 0; i < symtab_hdr->sh_info; i++)
826 local_got_offsets[i] = (bfd_vma) -1;
829 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
830 /* We have already allocated space in the .got. */
831 break;
833 local_got_offsets[r_symndx] = sgot->size;
835 if (info->shared)
836 /* If we are generating a shared object, we need to
837 output a R_MN10300_RELATIVE reloc so that the dynamic
838 linker can adjust this GOT entry. */
839 srelgot->size += sizeof (Elf32_External_Rela);
842 sgot->size += 4;
844 break;
846 case R_MN10300_PLT32:
847 case R_MN10300_PLT16:
848 /* This symbol requires a procedure linkage table entry. We
849 actually build the entry in adjust_dynamic_symbol,
850 because this might be a case of linking PIC code which is
851 never referenced by a dynamic object, in which case we
852 don't need to generate a procedure linkage table entry
853 after all. */
855 /* If this is a local symbol, we resolve it directly without
856 creating a procedure linkage table entry. */
857 if (h == NULL)
858 continue;
860 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
861 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
862 break;
864 h->needs_plt = 1;
866 break;
868 case R_MN10300_24:
869 case R_MN10300_16:
870 case R_MN10300_8:
871 case R_MN10300_PCREL32:
872 case R_MN10300_PCREL16:
873 case R_MN10300_PCREL8:
874 if (h != NULL)
875 h->non_got_ref = 1;
876 break;
878 case R_MN10300_32:
879 if (h != NULL)
880 h->non_got_ref = 1;
882 /* If we are creating a shared library, then we need to copy
883 the reloc into the shared library. */
884 if (info->shared
885 && (sec->flags & SEC_ALLOC) != 0)
887 /* When creating a shared object, we must copy these
888 reloc types into the output file. We create a reloc
889 section in dynobj and make room for this reloc. */
890 if (sreloc == NULL)
892 const char * name;
894 name = (bfd_elf_string_from_elf_section
895 (abfd,
896 elf_elfheader (abfd)->e_shstrndx,
897 elf_section_data (sec)->rel_hdr.sh_name));
898 if (name == NULL)
899 return FALSE;
901 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
902 && strcmp (bfd_get_section_name (abfd, sec),
903 name + 5) == 0);
905 sreloc = bfd_get_section_by_name (dynobj, name);
906 if (sreloc == NULL)
908 flagword flags;
910 flags = (SEC_HAS_CONTENTS | SEC_READONLY
911 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
912 if ((sec->flags & SEC_ALLOC) != 0)
913 flags |= SEC_ALLOC | SEC_LOAD;
914 sreloc = bfd_make_section_with_flags (dynobj,
915 name,
916 flags);
917 if (sreloc == NULL
918 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
919 return FALSE;
923 sreloc->size += sizeof (Elf32_External_Rela);
926 break;
930 return TRUE;
933 /* Return the section that should be marked against GC for a given
934 relocation. */
936 static asection *
937 mn10300_elf_gc_mark_hook (sec, info, rel, h, sym)
938 asection *sec;
939 struct bfd_link_info *info ATTRIBUTE_UNUSED;
940 Elf_Internal_Rela *rel;
941 struct elf_link_hash_entry *h;
942 Elf_Internal_Sym *sym;
944 if (h != NULL)
946 switch (ELF32_R_TYPE (rel->r_info))
948 case R_MN10300_GNU_VTINHERIT:
949 case R_MN10300_GNU_VTENTRY:
950 break;
952 default:
953 switch (h->root.type)
955 case bfd_link_hash_defined:
956 case bfd_link_hash_defweak:
957 return h->root.u.def.section;
959 case bfd_link_hash_common:
960 return h->root.u.c.p->section;
962 default:
963 break;
967 else
968 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
970 return NULL;
973 /* Perform a relocation as part of a final link. */
974 static bfd_reloc_status_type
975 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
976 input_section, contents, offset, value,
977 addend, h, symndx, info, sym_sec, is_local)
978 reloc_howto_type *howto;
979 bfd *input_bfd;
980 bfd *output_bfd ATTRIBUTE_UNUSED;
981 asection *input_section;
982 bfd_byte *contents;
983 bfd_vma offset;
984 bfd_vma value;
985 bfd_vma addend;
986 struct elf_link_hash_entry * h;
987 unsigned long symndx;
988 struct bfd_link_info *info;
989 asection *sym_sec ATTRIBUTE_UNUSED;
990 int is_local ATTRIBUTE_UNUSED;
992 unsigned long r_type = howto->type;
993 bfd_byte *hit_data = contents + offset;
994 bfd * dynobj;
995 bfd_vma * local_got_offsets;
996 asection * sgot;
997 asection * splt;
998 asection * sreloc;
1000 dynobj = elf_hash_table (info)->dynobj;
1001 local_got_offsets = elf_local_got_offsets (input_bfd);
1003 sgot = NULL;
1004 splt = NULL;
1005 sreloc = NULL;
1007 switch (r_type)
1009 case R_MN10300_24:
1010 case R_MN10300_16:
1011 case R_MN10300_8:
1012 case R_MN10300_PCREL8:
1013 case R_MN10300_PCREL16:
1014 case R_MN10300_PCREL32:
1015 case R_MN10300_GOTOFF32:
1016 case R_MN10300_GOTOFF24:
1017 case R_MN10300_GOTOFF16:
1018 if (info->shared
1019 && (input_section->flags & SEC_ALLOC) != 0
1020 && h != NULL
1021 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1022 return bfd_reloc_dangerous;
1025 switch (r_type)
1027 case R_MN10300_NONE:
1028 return bfd_reloc_ok;
1030 case R_MN10300_32:
1031 if (info->shared
1032 && (input_section->flags & SEC_ALLOC) != 0)
1034 Elf_Internal_Rela outrel;
1035 bfd_boolean skip, relocate;
1037 /* When generating a shared object, these relocations are
1038 copied into the output file to be resolved at run
1039 time. */
1040 if (sreloc == NULL)
1042 const char * name;
1044 name = (bfd_elf_string_from_elf_section
1045 (input_bfd,
1046 elf_elfheader (input_bfd)->e_shstrndx,
1047 elf_section_data (input_section)->rel_hdr.sh_name));
1048 if (name == NULL)
1049 return FALSE;
1051 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1052 && strcmp (bfd_get_section_name (input_bfd,
1053 input_section),
1054 name + 5) == 0);
1056 sreloc = bfd_get_section_by_name (dynobj, name);
1057 BFD_ASSERT (sreloc != NULL);
1060 skip = FALSE;
1062 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1063 input_section, offset);
1064 if (outrel.r_offset == (bfd_vma) -1)
1065 skip = TRUE;
1067 outrel.r_offset += (input_section->output_section->vma
1068 + input_section->output_offset);
1070 if (skip)
1072 memset (&outrel, 0, sizeof outrel);
1073 relocate = FALSE;
1075 else
1077 /* h->dynindx may be -1 if this symbol was marked to
1078 become local. */
1079 if (h == NULL
1080 || SYMBOL_REFERENCES_LOCAL (info, h))
1082 relocate = TRUE;
1083 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1084 outrel.r_addend = value + addend;
1086 else
1088 BFD_ASSERT (h->dynindx != -1);
1089 relocate = FALSE;
1090 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1091 outrel.r_addend = value + addend;
1095 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1096 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1097 + sreloc->reloc_count));
1098 ++sreloc->reloc_count;
1100 /* If this reloc is against an external symbol, we do
1101 not want to fiddle with the addend. Otherwise, we
1102 need to include the symbol value so that it becomes
1103 an addend for the dynamic reloc. */
1104 if (! relocate)
1105 return bfd_reloc_ok;
1107 value += addend;
1108 bfd_put_32 (input_bfd, value, hit_data);
1109 return bfd_reloc_ok;
1111 case R_MN10300_24:
1112 value += addend;
1114 if ((long) value > 0x7fffff || (long) value < -0x800000)
1115 return bfd_reloc_overflow;
1117 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1118 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1119 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1120 return bfd_reloc_ok;
1122 case R_MN10300_16:
1123 value += addend;
1125 if ((long) value > 0x7fff || (long) value < -0x8000)
1126 return bfd_reloc_overflow;
1128 bfd_put_16 (input_bfd, value, hit_data);
1129 return bfd_reloc_ok;
1131 case R_MN10300_8:
1132 value += addend;
1134 if ((long) value > 0x7f || (long) value < -0x80)
1135 return bfd_reloc_overflow;
1137 bfd_put_8 (input_bfd, value, hit_data);
1138 return bfd_reloc_ok;
1140 case R_MN10300_PCREL8:
1141 value -= (input_section->output_section->vma
1142 + input_section->output_offset);
1143 value -= offset;
1144 value += addend;
1146 if ((long) value > 0xff || (long) value < -0x100)
1147 return bfd_reloc_overflow;
1149 bfd_put_8 (input_bfd, value, hit_data);
1150 return bfd_reloc_ok;
1152 case R_MN10300_PCREL16:
1153 value -= (input_section->output_section->vma
1154 + input_section->output_offset);
1155 value -= offset;
1156 value += addend;
1158 if ((long) value > 0xffff || (long) value < -0x10000)
1159 return bfd_reloc_overflow;
1161 bfd_put_16 (input_bfd, value, hit_data);
1162 return bfd_reloc_ok;
1164 case R_MN10300_PCREL32:
1165 value -= (input_section->output_section->vma
1166 + input_section->output_offset);
1167 value -= offset;
1168 value += addend;
1170 bfd_put_32 (input_bfd, value, hit_data);
1171 return bfd_reloc_ok;
1173 case R_MN10300_GNU_VTINHERIT:
1174 case R_MN10300_GNU_VTENTRY:
1175 return bfd_reloc_ok;
1177 case R_MN10300_GOTPC32:
1178 /* Use global offset table as symbol value. */
1180 value = bfd_get_section_by_name (dynobj,
1181 ".got")->output_section->vma;
1182 value -= (input_section->output_section->vma
1183 + input_section->output_offset);
1184 value -= offset;
1185 value += addend;
1187 bfd_put_32 (input_bfd, value, hit_data);
1188 return bfd_reloc_ok;
1190 case R_MN10300_GOTPC16:
1191 /* Use global offset table as symbol value. */
1193 value = bfd_get_section_by_name (dynobj,
1194 ".got")->output_section->vma;
1195 value -= (input_section->output_section->vma
1196 + input_section->output_offset);
1197 value -= offset;
1198 value += addend;
1200 if ((long) value > 0xffff || (long) value < -0x10000)
1201 return bfd_reloc_overflow;
1203 bfd_put_16 (input_bfd, value, hit_data);
1204 return bfd_reloc_ok;
1206 case R_MN10300_GOTOFF32:
1207 value -= bfd_get_section_by_name (dynobj,
1208 ".got")->output_section->vma;
1209 value += addend;
1211 bfd_put_32 (input_bfd, value, hit_data);
1212 return bfd_reloc_ok;
1214 case R_MN10300_GOTOFF24:
1215 value -= bfd_get_section_by_name (dynobj,
1216 ".got")->output_section->vma;
1217 value += addend;
1219 if ((long) value > 0x7fffff || (long) value < -0x800000)
1220 return bfd_reloc_overflow;
1222 bfd_put_8 (input_bfd, value, hit_data);
1223 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1224 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1225 return bfd_reloc_ok;
1227 case R_MN10300_GOTOFF16:
1228 value -= bfd_get_section_by_name (dynobj,
1229 ".got")->output_section->vma;
1230 value += addend;
1232 if ((long) value > 0xffff || (long) value < -0x10000)
1233 return bfd_reloc_overflow;
1235 bfd_put_16 (input_bfd, value, hit_data);
1236 return bfd_reloc_ok;
1238 case R_MN10300_PLT32:
1239 if (h != NULL
1240 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1241 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1242 && h->plt.offset != (bfd_vma) -1)
1244 asection * splt;
1246 splt = bfd_get_section_by_name (dynobj, ".plt");
1248 value = (splt->output_section->vma
1249 + splt->output_offset
1250 + h->plt.offset) - value;
1253 value -= (input_section->output_section->vma
1254 + input_section->output_offset);
1255 value -= offset;
1256 value += addend;
1258 bfd_put_32 (input_bfd, value, hit_data);
1259 return bfd_reloc_ok;
1261 case R_MN10300_PLT16:
1262 if (h != NULL
1263 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1264 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1265 && h->plt.offset != (bfd_vma) -1)
1267 asection * splt;
1269 splt = bfd_get_section_by_name (dynobj, ".plt");
1271 value = (splt->output_section->vma
1272 + splt->output_offset
1273 + h->plt.offset) - value;
1276 value -= (input_section->output_section->vma
1277 + input_section->output_offset);
1278 value -= offset;
1279 value += addend;
1281 if ((long) value > 0xffff || (long) value < -0x10000)
1282 return bfd_reloc_overflow;
1284 bfd_put_16 (input_bfd, value, hit_data);
1285 return bfd_reloc_ok;
1287 case R_MN10300_GOT32:
1288 case R_MN10300_GOT24:
1289 case R_MN10300_GOT16:
1291 asection * sgot;
1293 sgot = bfd_get_section_by_name (dynobj, ".got");
1295 if (h != NULL)
1297 bfd_vma off;
1299 off = h->got.offset;
1300 BFD_ASSERT (off != (bfd_vma) -1);
1302 if (! elf_hash_table (info)->dynamic_sections_created
1303 || SYMBOL_REFERENCES_LOCAL (info, h))
1304 /* This is actually a static link, or it is a
1305 -Bsymbolic link and the symbol is defined
1306 locally, or the symbol was forced to be local
1307 because of a version file. We must initialize
1308 this entry in the global offset table.
1310 When doing a dynamic link, we create a .rela.got
1311 relocation entry to initialize the value. This
1312 is done in the finish_dynamic_symbol routine. */
1313 bfd_put_32 (output_bfd, value,
1314 sgot->contents + off);
1316 value = sgot->output_offset + off;
1318 else
1320 bfd_vma off;
1322 off = elf_local_got_offsets (input_bfd)[symndx];
1324 bfd_put_32 (output_bfd, value, sgot->contents + off);
1326 if (info->shared)
1328 asection * srelgot;
1329 Elf_Internal_Rela outrel;
1331 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1332 BFD_ASSERT (srelgot != NULL);
1334 outrel.r_offset = (sgot->output_section->vma
1335 + sgot->output_offset
1336 + off);
1337 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1338 outrel.r_addend = value;
1339 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1340 (bfd_byte *) (((Elf32_External_Rela *)
1341 srelgot->contents)
1342 + srelgot->reloc_count));
1343 ++ srelgot->reloc_count;
1346 value = sgot->output_offset + off;
1350 value += addend;
1352 if (r_type == R_MN10300_GOT32)
1354 bfd_put_32 (input_bfd, value, hit_data);
1355 return bfd_reloc_ok;
1357 else if (r_type == R_MN10300_GOT24)
1359 if ((long) value > 0x7fffff || (long) value < -0x800000)
1360 return bfd_reloc_overflow;
1362 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1363 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1364 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1365 return bfd_reloc_ok;
1367 else if (r_type == R_MN10300_GOT16)
1369 if ((long) value > 0xffff || (long) value < -0x10000)
1370 return bfd_reloc_overflow;
1372 bfd_put_16 (input_bfd, value, hit_data);
1373 return bfd_reloc_ok;
1375 /* Fall through. */
1377 default:
1378 return bfd_reloc_notsupported;
1382 /* Relocate an MN10300 ELF section. */
1383 static bfd_boolean
1384 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1385 contents, relocs, local_syms, local_sections)
1386 bfd *output_bfd;
1387 struct bfd_link_info *info;
1388 bfd *input_bfd;
1389 asection *input_section;
1390 bfd_byte *contents;
1391 Elf_Internal_Rela *relocs;
1392 Elf_Internal_Sym *local_syms;
1393 asection **local_sections;
1395 Elf_Internal_Shdr *symtab_hdr;
1396 struct elf_link_hash_entry **sym_hashes;
1397 Elf_Internal_Rela *rel, *relend;
1399 if (info->relocatable)
1400 return TRUE;
1402 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1403 sym_hashes = elf_sym_hashes (input_bfd);
1405 rel = relocs;
1406 relend = relocs + input_section->reloc_count;
1407 for (; rel < relend; rel++)
1409 int r_type;
1410 reloc_howto_type *howto;
1411 unsigned long r_symndx;
1412 Elf_Internal_Sym *sym;
1413 asection *sec;
1414 struct elf32_mn10300_link_hash_entry *h;
1415 bfd_vma relocation;
1416 bfd_reloc_status_type r;
1418 r_symndx = ELF32_R_SYM (rel->r_info);
1419 r_type = ELF32_R_TYPE (rel->r_info);
1420 howto = elf_mn10300_howto_table + r_type;
1422 /* Just skip the vtable gc relocs. */
1423 if (r_type == R_MN10300_GNU_VTINHERIT
1424 || r_type == R_MN10300_GNU_VTENTRY)
1425 continue;
1427 h = NULL;
1428 sym = NULL;
1429 sec = NULL;
1430 if (r_symndx < symtab_hdr->sh_info)
1432 sym = local_syms + r_symndx;
1433 sec = local_sections[r_symndx];
1434 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1436 else
1438 bfd_boolean unresolved_reloc;
1439 bfd_boolean warned;
1440 struct elf_link_hash_entry *hh;
1442 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1443 r_symndx, symtab_hdr, sym_hashes,
1444 hh, sec, relocation,
1445 unresolved_reloc, warned);
1447 h = (struct elf32_mn10300_link_hash_entry *) hh;
1449 if ((h->root.root.type == bfd_link_hash_defined
1450 || h->root.root.type == bfd_link_hash_defweak)
1451 && ( r_type == R_MN10300_GOTPC32
1452 || r_type == R_MN10300_GOTPC16
1453 || (( r_type == R_MN10300_PLT32
1454 || r_type == R_MN10300_PLT16)
1455 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1456 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1457 && h->root.plt.offset != (bfd_vma) -1)
1458 || (( r_type == R_MN10300_GOT32
1459 || r_type == R_MN10300_GOT24
1460 || r_type == R_MN10300_GOT16)
1461 && elf_hash_table (info)->dynamic_sections_created
1462 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1463 || (r_type == R_MN10300_32
1464 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1465 && ((input_section->flags & SEC_ALLOC) != 0
1466 /* DWARF will emit R_MN10300_32 relocations
1467 in its sections against symbols defined
1468 externally in shared libraries. We can't
1469 do anything with them here. */
1470 || ((input_section->flags & SEC_DEBUGGING) != 0
1471 && h->root.def_dynamic)))))
1472 /* In these cases, we don't need the relocation
1473 value. We check specially because in some
1474 obscure cases sec->output_section will be NULL. */
1475 relocation = 0;
1477 else if (unresolved_reloc)
1478 (*_bfd_error_handler)
1479 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1480 bfd_get_filename (input_bfd), h->root.root.root.string,
1481 bfd_get_section_name (input_bfd, input_section));
1484 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1485 input_section,
1486 contents, rel->r_offset,
1487 relocation, rel->r_addend,
1488 (struct elf_link_hash_entry *)h,
1489 r_symndx,
1490 info, sec, h == NULL);
1492 if (r != bfd_reloc_ok)
1494 const char *name;
1495 const char *msg = (const char *) 0;
1497 if (h != NULL)
1498 name = h->root.root.root.string;
1499 else
1501 name = (bfd_elf_string_from_elf_section
1502 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1503 if (name == NULL || *name == '\0')
1504 name = bfd_section_name (input_bfd, sec);
1507 switch (r)
1509 case bfd_reloc_overflow:
1510 if (! ((*info->callbacks->reloc_overflow)
1511 (info, (h ? &h->root.root : NULL), name,
1512 howto->name, (bfd_vma) 0, input_bfd,
1513 input_section, rel->r_offset)))
1514 return FALSE;
1515 break;
1517 case bfd_reloc_undefined:
1518 if (! ((*info->callbacks->undefined_symbol)
1519 (info, name, input_bfd, input_section,
1520 rel->r_offset, TRUE)))
1521 return FALSE;
1522 break;
1524 case bfd_reloc_outofrange:
1525 msg = _("internal error: out of range error");
1526 goto common_error;
1528 case bfd_reloc_notsupported:
1529 msg = _("internal error: unsupported relocation error");
1530 goto common_error;
1532 case bfd_reloc_dangerous:
1533 msg = _("internal error: dangerous error");
1534 goto common_error;
1536 default:
1537 msg = _("internal error: unknown error");
1538 /* fall through */
1540 common_error:
1541 if (!((*info->callbacks->warning)
1542 (info, msg, name, input_bfd, input_section,
1543 rel->r_offset)))
1544 return FALSE;
1545 break;
1550 return TRUE;
1553 /* Finish initializing one hash table entry. */
1554 static bfd_boolean
1555 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
1556 struct bfd_hash_entry *gen_entry;
1557 PTR in_args;
1559 struct elf32_mn10300_link_hash_entry *entry;
1560 struct bfd_link_info *link_info = (struct bfd_link_info *)in_args;
1561 unsigned int byte_count = 0;
1563 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1565 if (entry->root.root.type == bfd_link_hash_warning)
1566 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1568 /* If we already know we want to convert "call" to "calls" for calls
1569 to this symbol, then return now. */
1570 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1571 return TRUE;
1573 /* If there are no named calls to this symbol, or there's nothing we
1574 can move from the function itself into the "call" instruction,
1575 then note that all "call" instructions should be converted into
1576 "calls" instructions and return. If a symbol is available for
1577 dynamic symbol resolution (overridable or overriding), avoid
1578 custom calling conventions. */
1579 if (entry->direct_calls == 0
1580 || (entry->stack_size == 0 && entry->movm_args == 0)
1581 || (elf_hash_table (link_info)->dynamic_sections_created
1582 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1583 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1585 /* Make a note that we should convert "call" instructions to "calls"
1586 instructions for calls to this symbol. */
1587 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1588 return TRUE;
1591 /* We may be able to move some instructions from the function itself into
1592 the "call" instruction. Count how many bytes we might be able to
1593 eliminate in the function itself. */
1595 /* A movm instruction is two bytes. */
1596 if (entry->movm_args)
1597 byte_count += 2;
1599 /* Count the insn to allocate stack space too. */
1600 if (entry->stack_size > 0)
1602 if (entry->stack_size <= 128)
1603 byte_count += 3;
1604 else
1605 byte_count += 4;
1608 /* If using "call" will result in larger code, then turn all
1609 the associated "call" instructions into "calls" instructions. */
1610 if (byte_count < entry->direct_calls)
1611 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1613 /* This routine never fails. */
1614 return TRUE;
1617 /* This function handles relaxing for the mn10300.
1619 There are quite a few relaxing opportunities available on the mn10300:
1621 * calls:32 -> calls:16 2 bytes
1622 * call:32 -> call:16 2 bytes
1624 * call:32 -> calls:32 1 byte
1625 * call:16 -> calls:16 1 byte
1626 * These are done anytime using "calls" would result
1627 in smaller code, or when necessary to preserve the
1628 meaning of the program.
1630 * call:32 varies
1631 * call:16
1632 * In some circumstances we can move instructions
1633 from a function prologue into a "call" instruction.
1634 This is only done if the resulting code is no larger
1635 than the original code.
1637 * jmp:32 -> jmp:16 2 bytes
1638 * jmp:16 -> bra:8 1 byte
1640 * If the previous instruction is a conditional branch
1641 around the jump/bra, we may be able to reverse its condition
1642 and change its target to the jump's target. The jump/bra
1643 can then be deleted. 2 bytes
1645 * mov abs32 -> mov abs16 1 or 2 bytes
1647 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
1648 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
1650 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
1651 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
1653 We don't handle imm16->imm8 or d16->d8 as they're very rare
1654 and somewhat more difficult to support. */
1656 static bfd_boolean
1657 mn10300_elf_relax_section (abfd, sec, link_info, again)
1658 bfd *abfd;
1659 asection *sec;
1660 struct bfd_link_info *link_info;
1661 bfd_boolean *again;
1663 Elf_Internal_Shdr *symtab_hdr;
1664 Elf_Internal_Rela *internal_relocs = NULL;
1665 Elf_Internal_Rela *irel, *irelend;
1666 bfd_byte *contents = NULL;
1667 Elf_Internal_Sym *isymbuf = NULL;
1668 struct elf32_mn10300_link_hash_table *hash_table;
1669 asection *section = sec;
1671 /* Assume nothing changes. */
1672 *again = FALSE;
1674 /* We need a pointer to the mn10300 specific hash table. */
1675 hash_table = elf32_mn10300_hash_table (link_info);
1677 /* Initialize fields in each hash table entry the first time through. */
1678 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
1680 bfd *input_bfd;
1682 /* Iterate over all the input bfds. */
1683 for (input_bfd = link_info->input_bfds;
1684 input_bfd != NULL;
1685 input_bfd = input_bfd->link_next)
1687 /* We're going to need all the symbols for each bfd. */
1688 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1689 if (symtab_hdr->sh_info != 0)
1691 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1692 if (isymbuf == NULL)
1693 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1694 symtab_hdr->sh_info, 0,
1695 NULL, NULL, NULL);
1696 if (isymbuf == NULL)
1697 goto error_return;
1700 /* Iterate over each section in this bfd. */
1701 for (section = input_bfd->sections;
1702 section != NULL;
1703 section = section->next)
1705 struct elf32_mn10300_link_hash_entry *hash;
1706 Elf_Internal_Sym *sym;
1707 asection *sym_sec = NULL;
1708 const char *sym_name;
1709 char *new_name;
1711 /* If there's nothing to do in this section, skip it. */
1712 if (! (((section->flags & SEC_RELOC) != 0
1713 && section->reloc_count != 0)
1714 || (section->flags & SEC_CODE) != 0))
1715 continue;
1717 /* Get cached copy of section contents if it exists. */
1718 if (elf_section_data (section)->this_hdr.contents != NULL)
1719 contents = elf_section_data (section)->this_hdr.contents;
1720 else if (section->size != 0)
1722 /* Go get them off disk. */
1723 if (!bfd_malloc_and_get_section (input_bfd, section,
1724 &contents))
1725 goto error_return;
1727 else
1728 contents = NULL;
1730 /* If there aren't any relocs, then there's nothing to do. */
1731 if ((section->flags & SEC_RELOC) != 0
1732 && section->reloc_count != 0)
1735 /* Get a copy of the native relocations. */
1736 internal_relocs = (_bfd_elf_link_read_relocs
1737 (input_bfd, section, (PTR) NULL,
1738 (Elf_Internal_Rela *) NULL,
1739 link_info->keep_memory));
1740 if (internal_relocs == NULL)
1741 goto error_return;
1743 /* Now examine each relocation. */
1744 irel = internal_relocs;
1745 irelend = irel + section->reloc_count;
1746 for (; irel < irelend; irel++)
1748 long r_type;
1749 unsigned long r_index;
1750 unsigned char code;
1752 r_type = ELF32_R_TYPE (irel->r_info);
1753 r_index = ELF32_R_SYM (irel->r_info);
1755 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
1756 goto error_return;
1758 /* We need the name and hash table entry of the target
1759 symbol! */
1760 hash = NULL;
1761 sym = NULL;
1762 sym_sec = NULL;
1764 if (r_index < symtab_hdr->sh_info)
1766 /* A local symbol. */
1767 Elf_Internal_Sym *isym;
1768 struct elf_link_hash_table *elftab;
1769 bfd_size_type amt;
1771 isym = isymbuf + r_index;
1772 if (isym->st_shndx == SHN_UNDEF)
1773 sym_sec = bfd_und_section_ptr;
1774 else if (isym->st_shndx == SHN_ABS)
1775 sym_sec = bfd_abs_section_ptr;
1776 else if (isym->st_shndx == SHN_COMMON)
1777 sym_sec = bfd_com_section_ptr;
1778 else
1779 sym_sec
1780 = bfd_section_from_elf_index (input_bfd,
1781 isym->st_shndx);
1783 sym_name
1784 = bfd_elf_string_from_elf_section (input_bfd,
1785 (symtab_hdr
1786 ->sh_link),
1787 isym->st_name);
1789 /* If it isn't a function, then we don't care
1790 about it. */
1791 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
1792 continue;
1794 /* Tack on an ID so we can uniquely identify this
1795 local symbol in the global hash table. */
1796 amt = strlen (sym_name) + 10;
1797 new_name = bfd_malloc (amt);
1798 if (new_name == 0)
1799 goto error_return;
1801 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
1802 sym_name = new_name;
1804 elftab = &hash_table->static_hash_table->root;
1805 hash = ((struct elf32_mn10300_link_hash_entry *)
1806 elf_link_hash_lookup (elftab, sym_name,
1807 TRUE, TRUE, FALSE));
1808 free (new_name);
1810 else
1812 r_index -= symtab_hdr->sh_info;
1813 hash = (struct elf32_mn10300_link_hash_entry *)
1814 elf_sym_hashes (input_bfd)[r_index];
1817 /* If this is not a "call" instruction, then we
1818 should convert "call" instructions to "calls"
1819 instructions. */
1820 code = bfd_get_8 (input_bfd,
1821 contents + irel->r_offset - 1);
1822 if (code != 0xdd && code != 0xcd)
1823 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1825 /* If this is a jump/call, then bump the
1826 direct_calls counter. Else force "call" to
1827 "calls" conversions. */
1828 if (r_type == R_MN10300_PCREL32
1829 || r_type == R_MN10300_PLT32
1830 || r_type == R_MN10300_PLT16
1831 || r_type == R_MN10300_PCREL16)
1832 hash->direct_calls++;
1833 else
1834 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1838 /* Now look at the actual contents to get the stack size,
1839 and a list of what registers were saved in the prologue
1840 (ie movm_args). */
1841 if ((section->flags & SEC_CODE) != 0)
1843 Elf_Internal_Sym *isym, *isymend;
1844 unsigned int sec_shndx;
1845 struct elf_link_hash_entry **hashes;
1846 struct elf_link_hash_entry **end_hashes;
1847 unsigned int symcount;
1849 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1850 section);
1852 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1853 - symtab_hdr->sh_info);
1854 hashes = elf_sym_hashes (input_bfd);
1855 end_hashes = hashes + symcount;
1857 /* Look at each function defined in this section and
1858 update info for that function. */
1859 isymend = isymbuf + symtab_hdr->sh_info;
1860 for (isym = isymbuf; isym < isymend; isym++)
1862 if (isym->st_shndx == sec_shndx
1863 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
1865 struct elf_link_hash_table *elftab;
1866 bfd_size_type amt;
1867 struct elf_link_hash_entry **lhashes = hashes;
1869 /* Skip a local symbol if it aliases a
1870 global one. */
1871 for (; lhashes < end_hashes; lhashes++)
1873 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
1874 if ((hash->root.root.type == bfd_link_hash_defined
1875 || hash->root.root.type == bfd_link_hash_defweak)
1876 && hash->root.root.u.def.section == section
1877 && hash->root.type == STT_FUNC
1878 && hash->root.root.u.def.value == isym->st_value)
1879 break;
1881 if (lhashes != end_hashes)
1882 continue;
1884 if (isym->st_shndx == SHN_UNDEF)
1885 sym_sec = bfd_und_section_ptr;
1886 else if (isym->st_shndx == SHN_ABS)
1887 sym_sec = bfd_abs_section_ptr;
1888 else if (isym->st_shndx == SHN_COMMON)
1889 sym_sec = bfd_com_section_ptr;
1890 else
1891 sym_sec
1892 = bfd_section_from_elf_index (input_bfd,
1893 isym->st_shndx);
1895 sym_name = (bfd_elf_string_from_elf_section
1896 (input_bfd, symtab_hdr->sh_link,
1897 isym->st_name));
1899 /* Tack on an ID so we can uniquely identify this
1900 local symbol in the global hash table. */
1901 amt = strlen (sym_name) + 10;
1902 new_name = bfd_malloc (amt);
1903 if (new_name == 0)
1904 goto error_return;
1906 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
1907 sym_name = new_name;
1909 elftab = &hash_table->static_hash_table->root;
1910 hash = ((struct elf32_mn10300_link_hash_entry *)
1911 elf_link_hash_lookup (elftab, sym_name,
1912 TRUE, TRUE, FALSE));
1913 free (new_name);
1914 compute_function_info (input_bfd, hash,
1915 isym->st_value, contents);
1919 for (; hashes < end_hashes; hashes++)
1921 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1922 if ((hash->root.root.type == bfd_link_hash_defined
1923 || hash->root.root.type == bfd_link_hash_defweak)
1924 && hash->root.root.u.def.section == section
1925 && hash->root.type == STT_FUNC)
1926 compute_function_info (input_bfd, hash,
1927 (hash)->root.root.u.def.value,
1928 contents);
1932 /* Cache or free any memory we allocated for the relocs. */
1933 if (internal_relocs != NULL
1934 && elf_section_data (section)->relocs != internal_relocs)
1935 free (internal_relocs);
1936 internal_relocs = NULL;
1938 /* Cache or free any memory we allocated for the contents. */
1939 if (contents != NULL
1940 && elf_section_data (section)->this_hdr.contents != contents)
1942 if (! link_info->keep_memory)
1943 free (contents);
1944 else
1946 /* Cache the section contents for elf_link_input_bfd. */
1947 elf_section_data (section)->this_hdr.contents = contents;
1950 contents = NULL;
1953 /* Cache or free any memory we allocated for the symbols. */
1954 if (isymbuf != NULL
1955 && symtab_hdr->contents != (unsigned char *) isymbuf)
1957 if (! link_info->keep_memory)
1958 free (isymbuf);
1959 else
1961 /* Cache the symbols for elf_link_input_bfd. */
1962 symtab_hdr->contents = (unsigned char *) isymbuf;
1965 isymbuf = NULL;
1968 /* Now iterate on each symbol in the hash table and perform
1969 the final initialization steps on each. */
1970 elf32_mn10300_link_hash_traverse (hash_table,
1971 elf32_mn10300_finish_hash_table_entry,
1972 link_info);
1973 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1974 elf32_mn10300_finish_hash_table_entry,
1975 link_info);
1977 /* All entries in the hash table are fully initialized. */
1978 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1980 /* Now that everything has been initialized, go through each
1981 code section and delete any prologue insns which will be
1982 redundant because their operations will be performed by
1983 a "call" instruction. */
1984 for (input_bfd = link_info->input_bfds;
1985 input_bfd != NULL;
1986 input_bfd = input_bfd->link_next)
1988 /* We're going to need all the local symbols for each bfd. */
1989 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1990 if (symtab_hdr->sh_info != 0)
1992 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1993 if (isymbuf == NULL)
1994 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1995 symtab_hdr->sh_info, 0,
1996 NULL, NULL, NULL);
1997 if (isymbuf == NULL)
1998 goto error_return;
2001 /* Walk over each section in this bfd. */
2002 for (section = input_bfd->sections;
2003 section != NULL;
2004 section = section->next)
2006 unsigned int sec_shndx;
2007 Elf_Internal_Sym *isym, *isymend;
2008 struct elf_link_hash_entry **hashes;
2009 struct elf_link_hash_entry **end_hashes;
2010 unsigned int symcount;
2012 /* Skip non-code sections and empty sections. */
2013 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2014 continue;
2016 if (section->reloc_count != 0)
2018 /* Get a copy of the native relocations. */
2019 internal_relocs = (_bfd_elf_link_read_relocs
2020 (input_bfd, section, (PTR) NULL,
2021 (Elf_Internal_Rela *) NULL,
2022 link_info->keep_memory));
2023 if (internal_relocs == NULL)
2024 goto error_return;
2027 /* Get cached copy of section contents if it exists. */
2028 if (elf_section_data (section)->this_hdr.contents != NULL)
2029 contents = elf_section_data (section)->this_hdr.contents;
2030 else
2032 /* Go get them off disk. */
2033 if (!bfd_malloc_and_get_section (input_bfd, section,
2034 &contents))
2035 goto error_return;
2038 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2039 section);
2041 /* Now look for any function in this section which needs
2042 insns deleted from its prologue. */
2043 isymend = isymbuf + symtab_hdr->sh_info;
2044 for (isym = isymbuf; isym < isymend; isym++)
2046 struct elf32_mn10300_link_hash_entry *sym_hash;
2047 asection *sym_sec = NULL;
2048 const char *sym_name;
2049 char *new_name;
2050 struct elf_link_hash_table *elftab;
2051 bfd_size_type amt;
2053 if (isym->st_shndx != sec_shndx)
2054 continue;
2056 if (isym->st_shndx == SHN_UNDEF)
2057 sym_sec = bfd_und_section_ptr;
2058 else if (isym->st_shndx == SHN_ABS)
2059 sym_sec = bfd_abs_section_ptr;
2060 else if (isym->st_shndx == SHN_COMMON)
2061 sym_sec = bfd_com_section_ptr;
2062 else
2063 sym_sec
2064 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2066 sym_name
2067 = bfd_elf_string_from_elf_section (input_bfd,
2068 symtab_hdr->sh_link,
2069 isym->st_name);
2071 /* Tack on an ID so we can uniquely identify this
2072 local symbol in the global hash table. */
2073 amt = strlen (sym_name) + 10;
2074 new_name = bfd_malloc (amt);
2075 if (new_name == 0)
2076 goto error_return;
2077 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2078 sym_name = new_name;
2080 elftab = &hash_table->static_hash_table->root;
2081 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
2082 elf_link_hash_lookup (elftab, sym_name,
2083 FALSE, FALSE, FALSE));
2085 free (new_name);
2086 if (sym_hash == NULL)
2087 continue;
2089 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2090 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2092 int bytes = 0;
2094 /* Note that we've changed things. */
2095 elf_section_data (section)->relocs = internal_relocs;
2096 elf_section_data (section)->this_hdr.contents = contents;
2097 symtab_hdr->contents = (unsigned char *) isymbuf;
2099 /* Count how many bytes we're going to delete. */
2100 if (sym_hash->movm_args)
2101 bytes += 2;
2103 if (sym_hash->stack_size > 0)
2105 if (sym_hash->stack_size <= 128)
2106 bytes += 3;
2107 else
2108 bytes += 4;
2111 /* Note that we've deleted prologue bytes for this
2112 function. */
2113 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2115 /* Actually delete the bytes. */
2116 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2117 section,
2118 isym->st_value,
2119 bytes))
2120 goto error_return;
2122 /* Something changed. Not strictly necessary, but
2123 may lead to more relaxing opportunities. */
2124 *again = TRUE;
2128 /* Look for any global functions in this section which
2129 need insns deleted from their prologues. */
2130 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2131 - symtab_hdr->sh_info);
2132 hashes = elf_sym_hashes (input_bfd);
2133 end_hashes = hashes + symcount;
2134 for (; hashes < end_hashes; hashes++)
2136 struct elf32_mn10300_link_hash_entry *sym_hash;
2138 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2139 if ((sym_hash->root.root.type == bfd_link_hash_defined
2140 || sym_hash->root.root.type == bfd_link_hash_defweak)
2141 && sym_hash->root.root.u.def.section == section
2142 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2143 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2145 int bytes = 0;
2146 bfd_vma symval;
2148 /* Note that we've changed things. */
2149 elf_section_data (section)->relocs = internal_relocs;
2150 elf_section_data (section)->this_hdr.contents = contents;
2151 symtab_hdr->contents = (unsigned char *) isymbuf;
2153 /* Count how many bytes we're going to delete. */
2154 if (sym_hash->movm_args)
2155 bytes += 2;
2157 if (sym_hash->stack_size > 0)
2159 if (sym_hash->stack_size <= 128)
2160 bytes += 3;
2161 else
2162 bytes += 4;
2165 /* Note that we've deleted prologue bytes for this
2166 function. */
2167 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2169 /* Actually delete the bytes. */
2170 symval = sym_hash->root.root.u.def.value;
2171 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2172 section,
2173 symval,
2174 bytes))
2175 goto error_return;
2177 /* Something changed. Not strictly necessary, but
2178 may lead to more relaxing opportunities. */
2179 *again = TRUE;
2183 /* Cache or free any memory we allocated for the relocs. */
2184 if (internal_relocs != NULL
2185 && elf_section_data (section)->relocs != internal_relocs)
2186 free (internal_relocs);
2187 internal_relocs = NULL;
2189 /* Cache or free any memory we allocated for the contents. */
2190 if (contents != NULL
2191 && elf_section_data (section)->this_hdr.contents != contents)
2193 if (! link_info->keep_memory)
2194 free (contents);
2195 else
2197 /* Cache the section contents for elf_link_input_bfd. */
2198 elf_section_data (section)->this_hdr.contents = contents;
2201 contents = NULL;
2204 /* Cache or free any memory we allocated for the symbols. */
2205 if (isymbuf != NULL
2206 && symtab_hdr->contents != (unsigned char *) isymbuf)
2208 if (! link_info->keep_memory)
2209 free (isymbuf);
2210 else
2212 /* Cache the symbols for elf_link_input_bfd. */
2213 symtab_hdr->contents = (unsigned char *) isymbuf;
2216 isymbuf = NULL;
2220 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2221 contents = NULL;
2222 internal_relocs = NULL;
2223 isymbuf = NULL;
2224 /* For error_return. */
2225 section = sec;
2227 /* We don't have to do anything for a relocatable link, if
2228 this section does not have relocs, or if this is not a
2229 code section. */
2230 if (link_info->relocatable
2231 || (sec->flags & SEC_RELOC) == 0
2232 || sec->reloc_count == 0
2233 || (sec->flags & SEC_CODE) == 0)
2234 return TRUE;
2236 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2238 /* Get a copy of the native relocations. */
2239 internal_relocs = (_bfd_elf_link_read_relocs
2240 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2241 link_info->keep_memory));
2242 if (internal_relocs == NULL)
2243 goto error_return;
2245 /* Walk through them looking for relaxing opportunities. */
2246 irelend = internal_relocs + sec->reloc_count;
2247 for (irel = internal_relocs; irel < irelend; irel++)
2249 bfd_vma symval;
2250 struct elf32_mn10300_link_hash_entry *h = NULL;
2252 /* If this isn't something that can be relaxed, then ignore
2253 this reloc. */
2254 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2255 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2256 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2257 continue;
2259 /* Get the section contents if we haven't done so already. */
2260 if (contents == NULL)
2262 /* Get cached copy if it exists. */
2263 if (elf_section_data (sec)->this_hdr.contents != NULL)
2264 contents = elf_section_data (sec)->this_hdr.contents;
2265 else
2267 /* Go get them off disk. */
2268 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2269 goto error_return;
2273 /* Read this BFD's symbols if we haven't done so already. */
2274 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2276 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2277 if (isymbuf == NULL)
2278 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2279 symtab_hdr->sh_info, 0,
2280 NULL, NULL, NULL);
2281 if (isymbuf == NULL)
2282 goto error_return;
2285 /* Get the value of the symbol referred to by the reloc. */
2286 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2288 Elf_Internal_Sym *isym;
2289 asection *sym_sec = NULL;
2290 const char *sym_name;
2291 char *new_name;
2292 bfd_vma saved_addend;
2294 /* A local symbol. */
2295 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2296 if (isym->st_shndx == SHN_UNDEF)
2297 sym_sec = bfd_und_section_ptr;
2298 else if (isym->st_shndx == SHN_ABS)
2299 sym_sec = bfd_abs_section_ptr;
2300 else if (isym->st_shndx == SHN_COMMON)
2301 sym_sec = bfd_com_section_ptr;
2302 else
2303 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2305 sym_name = bfd_elf_string_from_elf_section (abfd,
2306 symtab_hdr->sh_link,
2307 isym->st_name);
2309 if ((sym_sec->flags & SEC_MERGE)
2310 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
2311 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2313 saved_addend = irel->r_addend;
2314 symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
2315 symval += irel->r_addend;
2316 irel->r_addend = saved_addend;
2318 else
2320 symval = (isym->st_value
2321 + sym_sec->output_section->vma
2322 + sym_sec->output_offset);
2324 /* Tack on an ID so we can uniquely identify this
2325 local symbol in the global hash table. */
2326 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2327 if (new_name == 0)
2328 goto error_return;
2329 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2330 sym_name = new_name;
2332 h = (struct elf32_mn10300_link_hash_entry *)
2333 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2334 sym_name, FALSE, FALSE, FALSE);
2335 free (new_name);
2337 else
2339 unsigned long indx;
2341 /* An external symbol. */
2342 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2343 h = (struct elf32_mn10300_link_hash_entry *)
2344 (elf_sym_hashes (abfd)[indx]);
2345 BFD_ASSERT (h != NULL);
2346 if (h->root.root.type != bfd_link_hash_defined
2347 && h->root.root.type != bfd_link_hash_defweak)
2349 /* This appears to be a reference to an undefined
2350 symbol. Just ignore it--it will be caught by the
2351 regular reloc processing. */
2352 continue;
2355 symval = (h->root.root.u.def.value
2356 + h->root.root.u.def.section->output_section->vma
2357 + h->root.root.u.def.section->output_offset);
2360 /* For simplicity of coding, we are going to modify the section
2361 contents, the section relocs, and the BFD symbol table. We
2362 must tell the rest of the code not to free up this
2363 information. It would be possible to instead create a table
2364 of changes which have to be made, as is done in coff-mips.c;
2365 that would be more work, but would require less memory when
2366 the linker is run. */
2368 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2369 branch/call, also deal with "call" -> "calls" conversions and
2370 insertion of prologue data into "call" instructions. */
2371 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2372 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2374 bfd_vma value = symval;
2376 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2377 && h != NULL
2378 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2379 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2380 && h->root.plt.offset != (bfd_vma) -1)
2382 asection * splt;
2384 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2385 ->dynobj, ".plt");
2387 value = ((splt->output_section->vma
2388 + splt->output_offset
2389 + h->root.plt.offset)
2390 - (sec->output_section->vma
2391 + sec->output_offset
2392 + irel->r_offset));
2395 /* If we've got a "call" instruction that needs to be turned
2396 into a "calls" instruction, do so now. It saves a byte. */
2397 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2399 unsigned char code;
2401 /* Get the opcode. */
2402 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2404 /* Make sure we're working with a "call" instruction! */
2405 if (code == 0xdd)
2407 /* Note that we've changed the relocs, section contents,
2408 etc. */
2409 elf_section_data (sec)->relocs = internal_relocs;
2410 elf_section_data (sec)->this_hdr.contents = contents;
2411 symtab_hdr->contents = (unsigned char *) isymbuf;
2413 /* Fix the opcode. */
2414 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2415 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2417 /* Fix irel->r_offset and irel->r_addend. */
2418 irel->r_offset += 1;
2419 irel->r_addend += 1;
2421 /* Delete one byte of data. */
2422 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2423 irel->r_offset + 3, 1))
2424 goto error_return;
2426 /* That will change things, so, we should relax again.
2427 Note that this is not required, and it may be slow. */
2428 *again = TRUE;
2431 else if (h)
2433 /* We've got a "call" instruction which needs some data
2434 from target function filled in. */
2435 unsigned char code;
2437 /* Get the opcode. */
2438 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2440 /* Insert data from the target function into the "call"
2441 instruction if needed. */
2442 if (code == 0xdd)
2444 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2445 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2446 contents + irel->r_offset + 5);
2450 /* Deal with pc-relative gunk. */
2451 value -= (sec->output_section->vma + sec->output_offset);
2452 value -= irel->r_offset;
2453 value += irel->r_addend;
2455 /* See if the value will fit in 16 bits, note the high value is
2456 0x7fff + 2 as the target will be two bytes closer if we are
2457 able to relax. */
2458 if ((long) value < 0x8001 && (long) value > -0x8000)
2460 unsigned char code;
2462 /* Get the opcode. */
2463 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2465 if (code != 0xdc && code != 0xdd && code != 0xff)
2466 continue;
2468 /* Note that we've changed the relocs, section contents, etc. */
2469 elf_section_data (sec)->relocs = internal_relocs;
2470 elf_section_data (sec)->this_hdr.contents = contents;
2471 symtab_hdr->contents = (unsigned char *) isymbuf;
2473 /* Fix the opcode. */
2474 if (code == 0xdc)
2475 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2476 else if (code == 0xdd)
2477 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2478 else if (code == 0xff)
2479 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2481 /* Fix the relocation's type. */
2482 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2483 (ELF32_R_TYPE (irel->r_info)
2484 == (int) R_MN10300_PLT32)
2485 ? R_MN10300_PLT16 :
2486 R_MN10300_PCREL16);
2488 /* Delete two bytes of data. */
2489 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2490 irel->r_offset + 1, 2))
2491 goto error_return;
2493 /* That will change things, so, we should relax again.
2494 Note that this is not required, and it may be slow. */
2495 *again = TRUE;
2499 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2500 branch. */
2501 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2503 bfd_vma value = symval;
2505 /* If we've got a "call" instruction that needs to be turned
2506 into a "calls" instruction, do so now. It saves a byte. */
2507 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2509 unsigned char code;
2511 /* Get the opcode. */
2512 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2514 /* Make sure we're working with a "call" instruction! */
2515 if (code == 0xcd)
2517 /* Note that we've changed the relocs, section contents,
2518 etc. */
2519 elf_section_data (sec)->relocs = internal_relocs;
2520 elf_section_data (sec)->this_hdr.contents = contents;
2521 symtab_hdr->contents = (unsigned char *) isymbuf;
2523 /* Fix the opcode. */
2524 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
2525 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2527 /* Fix irel->r_offset and irel->r_addend. */
2528 irel->r_offset += 1;
2529 irel->r_addend += 1;
2531 /* Delete one byte of data. */
2532 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2533 irel->r_offset + 1, 1))
2534 goto error_return;
2536 /* That will change things, so, we should relax again.
2537 Note that this is not required, and it may be slow. */
2538 *again = TRUE;
2541 else if (h)
2543 unsigned char code;
2545 /* Get the opcode. */
2546 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2548 /* Insert data from the target function into the "call"
2549 instruction if needed. */
2550 if (code == 0xcd)
2552 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
2553 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2554 contents + irel->r_offset + 3);
2558 /* Deal with pc-relative gunk. */
2559 value -= (sec->output_section->vma + sec->output_offset);
2560 value -= irel->r_offset;
2561 value += irel->r_addend;
2563 /* See if the value will fit in 8 bits, note the high value is
2564 0x7f + 1 as the target will be one bytes closer if we are
2565 able to relax. */
2566 if ((long) value < 0x80 && (long) value > -0x80)
2568 unsigned char code;
2570 /* Get the opcode. */
2571 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2573 if (code != 0xcc)
2574 continue;
2576 /* Note that we've changed the relocs, section contents, etc. */
2577 elf_section_data (sec)->relocs = internal_relocs;
2578 elf_section_data (sec)->this_hdr.contents = contents;
2579 symtab_hdr->contents = (unsigned char *) isymbuf;
2581 /* Fix the opcode. */
2582 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
2584 /* Fix the relocation's type. */
2585 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2586 R_MN10300_PCREL8);
2588 /* Delete one byte of data. */
2589 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2590 irel->r_offset + 1, 1))
2591 goto error_return;
2593 /* That will change things, so, we should relax again.
2594 Note that this is not required, and it may be slow. */
2595 *again = TRUE;
2599 /* Try to eliminate an unconditional 8 bit pc-relative branch
2600 which immediately follows a conditional 8 bit pc-relative
2601 branch around the unconditional branch.
2603 original: new:
2604 bCC lab1 bCC' lab2
2605 bra lab2
2606 lab1: lab1:
2608 This happens when the bCC can't reach lab2 at assembly time,
2609 but due to other relaxations it can reach at link time. */
2610 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
2612 Elf_Internal_Rela *nrel;
2613 bfd_vma value = symval;
2614 unsigned char code;
2616 /* Deal with pc-relative gunk. */
2617 value -= (sec->output_section->vma + sec->output_offset);
2618 value -= irel->r_offset;
2619 value += irel->r_addend;
2621 /* Do nothing if this reloc is the last byte in the section. */
2622 if (irel->r_offset == sec->size)
2623 continue;
2625 /* See if the next instruction is an unconditional pc-relative
2626 branch, more often than not this test will fail, so we
2627 test it first to speed things up. */
2628 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2629 if (code != 0xca)
2630 continue;
2632 /* Also make sure the next relocation applies to the next
2633 instruction and that it's a pc-relative 8 bit branch. */
2634 nrel = irel + 1;
2635 if (nrel == irelend
2636 || irel->r_offset + 2 != nrel->r_offset
2637 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
2638 continue;
2640 /* Make sure our destination immediately follows the
2641 unconditional branch. */
2642 if (symval != (sec->output_section->vma + sec->output_offset
2643 + irel->r_offset + 3))
2644 continue;
2646 /* Now make sure we are a conditional branch. This may not
2647 be necessary, but why take the chance.
2649 Note these checks assume that R_MN10300_PCREL8 relocs
2650 only occur on bCC and bCCx insns. If they occured
2651 elsewhere, we'd need to know the start of this insn
2652 for this check to be accurate. */
2653 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2654 if (code != 0xc0 && code != 0xc1 && code != 0xc2
2655 && code != 0xc3 && code != 0xc4 && code != 0xc5
2656 && code != 0xc6 && code != 0xc7 && code != 0xc8
2657 && code != 0xc9 && code != 0xe8 && code != 0xe9
2658 && code != 0xea && code != 0xeb)
2659 continue;
2661 /* We also have to be sure there is no symbol/label
2662 at the unconditional branch. */
2663 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
2664 irel->r_offset + 1))
2665 continue;
2667 /* Note that we've changed the relocs, section contents, etc. */
2668 elf_section_data (sec)->relocs = internal_relocs;
2669 elf_section_data (sec)->this_hdr.contents = contents;
2670 symtab_hdr->contents = (unsigned char *) isymbuf;
2672 /* Reverse the condition of the first branch. */
2673 switch (code)
2675 case 0xc8:
2676 code = 0xc9;
2677 break;
2678 case 0xc9:
2679 code = 0xc8;
2680 break;
2681 case 0xc0:
2682 code = 0xc2;
2683 break;
2684 case 0xc2:
2685 code = 0xc0;
2686 break;
2687 case 0xc3:
2688 code = 0xc1;
2689 break;
2690 case 0xc1:
2691 code = 0xc3;
2692 break;
2693 case 0xc4:
2694 code = 0xc6;
2695 break;
2696 case 0xc6:
2697 code = 0xc4;
2698 break;
2699 case 0xc7:
2700 code = 0xc5;
2701 break;
2702 case 0xc5:
2703 code = 0xc7;
2704 break;
2705 case 0xe8:
2706 code = 0xe9;
2707 break;
2708 case 0x9d:
2709 code = 0xe8;
2710 break;
2711 case 0xea:
2712 code = 0xeb;
2713 break;
2714 case 0xeb:
2715 code = 0xea;
2716 break;
2718 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2720 /* Set the reloc type and symbol for the first branch
2721 from the second branch. */
2722 irel->r_info = nrel->r_info;
2724 /* Make the reloc for the second branch a null reloc. */
2725 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
2726 R_MN10300_NONE);
2728 /* Delete two bytes of data. */
2729 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2730 irel->r_offset + 1, 2))
2731 goto error_return;
2733 /* That will change things, so, we should relax again.
2734 Note that this is not required, and it may be slow. */
2735 *again = TRUE;
2738 /* Try to turn a 24 immediate, displacement or absolute address
2739 into a 8 immediate, displacement or absolute address. */
2740 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
2742 bfd_vma value = symval;
2743 value += irel->r_addend;
2745 /* See if the value will fit in 8 bits. */
2746 if ((long) value < 0x7f && (long) value > -0x80)
2748 unsigned char code;
2750 /* AM33 insns which have 24 operands are 6 bytes long and
2751 will have 0xfd as the first byte. */
2753 /* Get the first opcode. */
2754 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2756 if (code == 0xfd)
2758 /* Get the second opcode. */
2759 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2761 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2762 equivalent instructions exists. */
2763 if (code != 0x6b && code != 0x7b
2764 && code != 0x8b && code != 0x9b
2765 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2766 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2767 || (code & 0x0f) == 0x0e))
2769 /* Not safe if the high bit is on as relaxing may
2770 move the value out of high mem and thus not fit
2771 in a signed 8bit value. This is currently over
2772 conservative. */
2773 if ((value & 0x80) == 0)
2775 /* Note that we've changed the relocation contents,
2776 etc. */
2777 elf_section_data (sec)->relocs = internal_relocs;
2778 elf_section_data (sec)->this_hdr.contents = contents;
2779 symtab_hdr->contents = (unsigned char *) isymbuf;
2781 /* Fix the opcode. */
2782 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2783 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2785 /* Fix the relocation's type. */
2786 irel->r_info =
2787 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2788 R_MN10300_8);
2790 /* Delete two bytes of data. */
2791 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2792 irel->r_offset + 1, 2))
2793 goto error_return;
2795 /* That will change things, so, we should relax
2796 again. Note that this is not required, and it
2797 may be slow. */
2798 *again = TRUE;
2799 break;
2806 /* Try to turn a 32bit immediate, displacement or absolute address
2807 into a 16bit immediate, displacement or absolute address. */
2808 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
2809 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
2810 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32
2811 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2813 bfd_vma value = symval;
2815 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
2817 asection * sgot;
2819 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
2820 ->dynobj, ".got");
2822 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
2824 value = sgot->output_offset;
2826 if (h)
2827 value += h->root.got.offset;
2828 else
2829 value += (elf_local_got_offsets
2830 (abfd)[ELF32_R_SYM (irel->r_info)]);
2832 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
2833 value -= sgot->output_section->vma;
2834 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2835 value = (sgot->output_section->vma
2836 - (sec->output_section->vma
2837 + sec->output_offset
2838 + irel->r_offset));
2839 else
2840 abort ();
2843 value += irel->r_addend;
2845 /* See if the value will fit in 24 bits.
2846 We allow any 16bit match here. We prune those we can't
2847 handle below. */
2848 if ((long) value < 0x7fffff && (long) value > -0x800000)
2850 unsigned char code;
2852 /* AM33 insns which have 32bit operands are 7 bytes long and
2853 will have 0xfe as the first byte. */
2855 /* Get the first opcode. */
2856 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2858 if (code == 0xfe)
2860 /* Get the second opcode. */
2861 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2863 /* All the am33 32 -> 24 relaxing possibilities. */
2864 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2865 equivalent instructions exists. */
2866 if (code != 0x6b && code != 0x7b
2867 && code != 0x8b && code != 0x9b
2868 && (ELF32_R_TYPE (irel->r_info)
2869 != (int) R_MN10300_GOTPC32)
2870 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2871 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2872 || (code & 0x0f) == 0x0e))
2874 /* Not safe if the high bit is on as relaxing may
2875 move the value out of high mem and thus not fit
2876 in a signed 16bit value. This is currently over
2877 conservative. */
2878 if ((value & 0x8000) == 0)
2880 /* Note that we've changed the relocation contents,
2881 etc. */
2882 elf_section_data (sec)->relocs = internal_relocs;
2883 elf_section_data (sec)->this_hdr.contents = contents;
2884 symtab_hdr->contents = (unsigned char *) isymbuf;
2886 /* Fix the opcode. */
2887 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2888 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2890 /* Fix the relocation's type. */
2891 irel->r_info =
2892 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2893 (ELF32_R_TYPE (irel->r_info)
2894 == (int) R_MN10300_GOTOFF32)
2895 ? R_MN10300_GOTOFF24
2896 : (ELF32_R_TYPE (irel->r_info)
2897 == (int) R_MN10300_GOT32)
2898 ? R_MN10300_GOT24 :
2899 R_MN10300_24);
2901 /* Delete one byte of data. */
2902 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2903 irel->r_offset + 3, 1))
2904 goto error_return;
2906 /* That will change things, so, we should relax
2907 again. Note that this is not required, and it
2908 may be slow. */
2909 *again = TRUE;
2910 break;
2916 /* See if the value will fit in 16 bits.
2917 We allow any 16bit match here. We prune those we can't
2918 handle below. */
2919 if ((long) value < 0x7fff && (long) value > -0x8000)
2921 unsigned char code;
2923 /* Most insns which have 32bit operands are 6 bytes long;
2924 exceptions are pcrel insns and bit insns.
2926 We handle pcrel insns above. We don't bother trying
2927 to handle the bit insns here.
2929 The first byte of the remaining insns will be 0xfc. */
2931 /* Get the first opcode. */
2932 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2934 if (code != 0xfc)
2935 continue;
2937 /* Get the second opcode. */
2938 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2940 if ((code & 0xf0) < 0x80)
2941 switch (code & 0xf0)
2943 /* mov (d32,am),dn -> mov (d32,am),dn
2944 mov dm,(d32,am) -> mov dn,(d32,am)
2945 mov (d32,am),an -> mov (d32,am),an
2946 mov dm,(d32,am) -> mov dn,(d32,am)
2947 movbu (d32,am),dn -> movbu (d32,am),dn
2948 movbu dm,(d32,am) -> movbu dn,(d32,am)
2949 movhu (d32,am),dn -> movhu (d32,am),dn
2950 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2951 case 0x00:
2952 case 0x10:
2953 case 0x20:
2954 case 0x30:
2955 case 0x40:
2956 case 0x50:
2957 case 0x60:
2958 case 0x70:
2959 /* Not safe if the high bit is on as relaxing may
2960 move the value out of high mem and thus not fit
2961 in a signed 16bit value. */
2962 if (code == 0xcc
2963 && (value & 0x8000))
2964 continue;
2966 /* Note that we've changed the relocation contents, etc. */
2967 elf_section_data (sec)->relocs = internal_relocs;
2968 elf_section_data (sec)->this_hdr.contents = contents;
2969 symtab_hdr->contents = (unsigned char *) isymbuf;
2971 /* Fix the opcode. */
2972 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2973 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2975 /* Fix the relocation's type. */
2976 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2977 (ELF32_R_TYPE (irel->r_info)
2978 == (int) R_MN10300_GOTOFF32)
2979 ? R_MN10300_GOTOFF16
2980 : (ELF32_R_TYPE (irel->r_info)
2981 == (int) R_MN10300_GOT32)
2982 ? R_MN10300_GOT16
2983 : (ELF32_R_TYPE (irel->r_info)
2984 == (int) R_MN10300_GOTPC32)
2985 ? R_MN10300_GOTPC16 :
2986 R_MN10300_16);
2988 /* Delete two bytes of data. */
2989 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2990 irel->r_offset + 2, 2))
2991 goto error_return;
2993 /* That will change things, so, we should relax again.
2994 Note that this is not required, and it may be slow. */
2995 *again = TRUE;
2996 break;
2998 else if ((code & 0xf0) == 0x80
2999 || (code & 0xf0) == 0x90)
3000 switch (code & 0xf3)
3002 /* mov dn,(abs32) -> mov dn,(abs16)
3003 movbu dn,(abs32) -> movbu dn,(abs16)
3004 movhu dn,(abs32) -> movhu dn,(abs16) */
3005 case 0x81:
3006 case 0x82:
3007 case 0x83:
3008 /* Note that we've changed the relocation contents, etc. */
3009 elf_section_data (sec)->relocs = internal_relocs;
3010 elf_section_data (sec)->this_hdr.contents = contents;
3011 symtab_hdr->contents = (unsigned char *) isymbuf;
3013 if ((code & 0xf3) == 0x81)
3014 code = 0x01 + (code & 0x0c);
3015 else if ((code & 0xf3) == 0x82)
3016 code = 0x02 + (code & 0x0c);
3017 else if ((code & 0xf3) == 0x83)
3018 code = 0x03 + (code & 0x0c);
3019 else
3020 abort ();
3022 /* Fix the opcode. */
3023 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3025 /* Fix the relocation's type. */
3026 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3027 (ELF32_R_TYPE (irel->r_info)
3028 == (int) R_MN10300_GOTOFF32)
3029 ? R_MN10300_GOTOFF16
3030 : (ELF32_R_TYPE (irel->r_info)
3031 == (int) R_MN10300_GOT32)
3032 ? R_MN10300_GOT16
3033 : (ELF32_R_TYPE (irel->r_info)
3034 == (int) R_MN10300_GOTPC32)
3035 ? R_MN10300_GOTPC16 :
3036 R_MN10300_16);
3038 /* The opcode got shorter too, so we have to fix the
3039 addend and offset too! */
3040 irel->r_offset -= 1;
3042 /* Delete three bytes of data. */
3043 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3044 irel->r_offset + 1, 3))
3045 goto error_return;
3047 /* That will change things, so, we should relax again.
3048 Note that this is not required, and it may be slow. */
3049 *again = TRUE;
3050 break;
3052 /* mov am,(abs32) -> mov am,(abs16)
3053 mov am,(d32,sp) -> mov am,(d16,sp)
3054 mov dm,(d32,sp) -> mov dm,(d32,sp)
3055 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3056 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3057 case 0x80:
3058 case 0x90:
3059 case 0x91:
3060 case 0x92:
3061 case 0x93:
3062 /* sp-based offsets are zero-extended. */
3063 if (code >= 0x90 && code <= 0x93
3064 && (long)value < 0)
3065 continue;
3067 /* Note that we've changed the relocation contents, etc. */
3068 elf_section_data (sec)->relocs = internal_relocs;
3069 elf_section_data (sec)->this_hdr.contents = contents;
3070 symtab_hdr->contents = (unsigned char *) isymbuf;
3072 /* Fix the opcode. */
3073 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3074 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3076 /* Fix the relocation's type. */
3077 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3078 (ELF32_R_TYPE (irel->r_info)
3079 == (int) R_MN10300_GOTOFF32)
3080 ? R_MN10300_GOTOFF16
3081 : (ELF32_R_TYPE (irel->r_info)
3082 == (int) R_MN10300_GOT32)
3083 ? R_MN10300_GOT16
3084 : (ELF32_R_TYPE (irel->r_info)
3085 == (int) R_MN10300_GOTPC32)
3086 ? R_MN10300_GOTPC16 :
3087 R_MN10300_16);
3089 /* Delete two bytes of data. */
3090 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3091 irel->r_offset + 2, 2))
3092 goto error_return;
3094 /* That will change things, so, we should relax again.
3095 Note that this is not required, and it may be slow. */
3096 *again = TRUE;
3097 break;
3099 else if ((code & 0xf0) < 0xf0)
3100 switch (code & 0xfc)
3102 /* mov imm32,dn -> mov imm16,dn
3103 mov imm32,an -> mov imm16,an
3104 mov (abs32),dn -> mov (abs16),dn
3105 movbu (abs32),dn -> movbu (abs16),dn
3106 movhu (abs32),dn -> movhu (abs16),dn */
3107 case 0xcc:
3108 case 0xdc:
3109 case 0xa4:
3110 case 0xa8:
3111 case 0xac:
3112 /* Not safe if the high bit is on as relaxing may
3113 move the value out of high mem and thus not fit
3114 in a signed 16bit value. */
3115 if (code == 0xcc
3116 && (value & 0x8000))
3117 continue;
3119 /* mov imm16, an zero-extends the immediate. */
3120 if (code == 0xdc
3121 && (long)value < 0)
3122 continue;
3124 /* Note that we've changed the relocation contents, etc. */
3125 elf_section_data (sec)->relocs = internal_relocs;
3126 elf_section_data (sec)->this_hdr.contents = contents;
3127 symtab_hdr->contents = (unsigned char *) isymbuf;
3129 if ((code & 0xfc) == 0xcc)
3130 code = 0x2c + (code & 0x03);
3131 else if ((code & 0xfc) == 0xdc)
3132 code = 0x24 + (code & 0x03);
3133 else if ((code & 0xfc) == 0xa4)
3134 code = 0x30 + (code & 0x03);
3135 else if ((code & 0xfc) == 0xa8)
3136 code = 0x34 + (code & 0x03);
3137 else if ((code & 0xfc) == 0xac)
3138 code = 0x38 + (code & 0x03);
3139 else
3140 abort ();
3142 /* Fix the opcode. */
3143 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3145 /* Fix the relocation's type. */
3146 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3147 (ELF32_R_TYPE (irel->r_info)
3148 == (int) R_MN10300_GOTOFF32)
3149 ? R_MN10300_GOTOFF16
3150 : (ELF32_R_TYPE (irel->r_info)
3151 == (int) R_MN10300_GOT32)
3152 ? R_MN10300_GOT16
3153 : (ELF32_R_TYPE (irel->r_info)
3154 == (int) R_MN10300_GOTPC32)
3155 ? R_MN10300_GOTPC16 :
3156 R_MN10300_16);
3158 /* The opcode got shorter too, so we have to fix the
3159 addend and offset too! */
3160 irel->r_offset -= 1;
3162 /* Delete three bytes of data. */
3163 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3164 irel->r_offset + 1, 3))
3165 goto error_return;
3167 /* That will change things, so, we should relax again.
3168 Note that this is not required, and it may be slow. */
3169 *again = TRUE;
3170 break;
3172 /* mov (abs32),an -> mov (abs16),an
3173 mov (d32,sp),an -> mov (d16,sp),an
3174 mov (d32,sp),dn -> mov (d16,sp),dn
3175 movbu (d32,sp),dn -> movbu (d16,sp),dn
3176 movhu (d32,sp),dn -> movhu (d16,sp),dn
3177 add imm32,dn -> add imm16,dn
3178 cmp imm32,dn -> cmp imm16,dn
3179 add imm32,an -> add imm16,an
3180 cmp imm32,an -> cmp imm16,an
3181 and imm32,dn -> and imm16,dn
3182 or imm32,dn -> or imm16,dn
3183 xor imm32,dn -> xor imm16,dn
3184 btst imm32,dn -> btst imm16,dn */
3186 case 0xa0:
3187 case 0xb0:
3188 case 0xb1:
3189 case 0xb2:
3190 case 0xb3:
3191 case 0xc0:
3192 case 0xc8:
3194 case 0xd0:
3195 case 0xd8:
3196 case 0xe0:
3197 case 0xe1:
3198 case 0xe2:
3199 case 0xe3:
3200 /* cmp imm16, an zero-extends the immediate. */
3201 if (code == 0xdc
3202 && (long)value < 0)
3203 continue;
3205 /* So do sp-based offsets. */
3206 if (code >= 0xb0 && code <= 0xb3
3207 && (long)value < 0)
3208 continue;
3210 /* Note that we've changed the relocation contents, etc. */
3211 elf_section_data (sec)->relocs = internal_relocs;
3212 elf_section_data (sec)->this_hdr.contents = contents;
3213 symtab_hdr->contents = (unsigned char *) isymbuf;
3215 /* Fix the opcode. */
3216 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3217 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3219 /* Fix the relocation's type. */
3220 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3221 (ELF32_R_TYPE (irel->r_info)
3222 == (int) R_MN10300_GOTOFF32)
3223 ? R_MN10300_GOTOFF16
3224 : (ELF32_R_TYPE (irel->r_info)
3225 == (int) R_MN10300_GOT32)
3226 ? R_MN10300_GOT16
3227 : (ELF32_R_TYPE (irel->r_info)
3228 == (int) R_MN10300_GOTPC32)
3229 ? R_MN10300_GOTPC16 :
3230 R_MN10300_16);
3232 /* Delete two bytes of data. */
3233 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3234 irel->r_offset + 2, 2))
3235 goto error_return;
3237 /* That will change things, so, we should relax again.
3238 Note that this is not required, and it may be slow. */
3239 *again = TRUE;
3240 break;
3242 else if (code == 0xfe)
3244 /* add imm32,sp -> add imm16,sp */
3246 /* Note that we've changed the relocation contents, etc. */
3247 elf_section_data (sec)->relocs = internal_relocs;
3248 elf_section_data (sec)->this_hdr.contents = contents;
3249 symtab_hdr->contents = (unsigned char *) isymbuf;
3251 /* Fix the opcode. */
3252 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3253 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3255 /* Fix the relocation's type. */
3256 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3257 (ELF32_R_TYPE (irel->r_info)
3258 == (int) R_MN10300_GOT32)
3259 ? R_MN10300_GOT16
3260 : (ELF32_R_TYPE (irel->r_info)
3261 == (int) R_MN10300_GOTOFF32)
3262 ? R_MN10300_GOTOFF16
3263 : (ELF32_R_TYPE (irel->r_info)
3264 == (int) R_MN10300_GOTPC32)
3265 ? R_MN10300_GOTPC16 :
3266 R_MN10300_16);
3268 /* Delete two bytes of data. */
3269 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3270 irel->r_offset + 2, 2))
3271 goto error_return;
3273 /* That will change things, so, we should relax again.
3274 Note that this is not required, and it may be slow. */
3275 *again = TRUE;
3276 break;
3282 if (isymbuf != NULL
3283 && symtab_hdr->contents != (unsigned char *) isymbuf)
3285 if (! link_info->keep_memory)
3286 free (isymbuf);
3287 else
3289 /* Cache the symbols for elf_link_input_bfd. */
3290 symtab_hdr->contents = (unsigned char *) isymbuf;
3294 if (contents != NULL
3295 && elf_section_data (sec)->this_hdr.contents != contents)
3297 if (! link_info->keep_memory)
3298 free (contents);
3299 else
3301 /* Cache the section contents for elf_link_input_bfd. */
3302 elf_section_data (sec)->this_hdr.contents = contents;
3306 if (internal_relocs != NULL
3307 && elf_section_data (sec)->relocs != internal_relocs)
3308 free (internal_relocs);
3310 return TRUE;
3312 error_return:
3313 if (isymbuf != NULL
3314 && symtab_hdr->contents != (unsigned char *) isymbuf)
3315 free (isymbuf);
3316 if (contents != NULL
3317 && elf_section_data (section)->this_hdr.contents != contents)
3318 free (contents);
3319 if (internal_relocs != NULL
3320 && elf_section_data (section)->relocs != internal_relocs)
3321 free (internal_relocs);
3323 return FALSE;
3326 /* Compute the stack size and movm arguments for the function
3327 referred to by HASH at address ADDR in section with
3328 contents CONTENTS, store the information in the hash table. */
3329 static void
3330 compute_function_info (abfd, hash, addr, contents)
3331 bfd *abfd;
3332 struct elf32_mn10300_link_hash_entry *hash;
3333 bfd_vma addr;
3334 unsigned char *contents;
3336 unsigned char byte1, byte2;
3337 /* We only care about a very small subset of the possible prologue
3338 sequences here. Basically we look for:
3340 movm [d2,d3,a2,a3],sp (optional)
3341 add <size>,sp (optional, and only for sizes which fit in an unsigned
3342 8 bit number)
3344 If we find anything else, we quit. */
3346 /* Look for movm [regs],sp */
3347 byte1 = bfd_get_8 (abfd, contents + addr);
3348 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3350 if (byte1 == 0xcf)
3352 hash->movm_args = byte2;
3353 addr += 2;
3354 byte1 = bfd_get_8 (abfd, contents + addr);
3355 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3358 /* Now figure out how much stack space will be allocated by the movm
3359 instruction. We need this kept separate from the function's normal
3360 stack space. */
3361 if (hash->movm_args)
3363 /* Space for d2. */
3364 if (hash->movm_args & 0x80)
3365 hash->movm_stack_size += 4;
3367 /* Space for d3. */
3368 if (hash->movm_args & 0x40)
3369 hash->movm_stack_size += 4;
3371 /* Space for a2. */
3372 if (hash->movm_args & 0x20)
3373 hash->movm_stack_size += 4;
3375 /* Space for a3. */
3376 if (hash->movm_args & 0x10)
3377 hash->movm_stack_size += 4;
3379 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
3380 if (hash->movm_args & 0x08)
3381 hash->movm_stack_size += 8 * 4;
3383 if (bfd_get_mach (abfd) == bfd_mach_am33
3384 || bfd_get_mach (abfd) == bfd_mach_am33_2)
3386 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
3387 if (hash->movm_args & 0x1)
3388 hash->movm_stack_size += 6 * 4;
3390 /* exreg1 space. e4, e5, e6, e7 */
3391 if (hash->movm_args & 0x2)
3392 hash->movm_stack_size += 4 * 4;
3394 /* exreg0 space. e2, e3 */
3395 if (hash->movm_args & 0x4)
3396 hash->movm_stack_size += 2 * 4;
3400 /* Now look for the two stack adjustment variants. */
3401 if (byte1 == 0xf8 && byte2 == 0xfe)
3403 int temp = bfd_get_8 (abfd, contents + addr + 2);
3404 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
3406 hash->stack_size = -temp;
3408 else if (byte1 == 0xfa && byte2 == 0xfe)
3410 int temp = bfd_get_16 (abfd, contents + addr + 2);
3411 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
3412 temp = -temp;
3414 if (temp < 255)
3415 hash->stack_size = temp;
3418 /* If the total stack to be allocated by the call instruction is more
3419 than 255 bytes, then we can't remove the stack adjustment by using
3420 "call" (we might still be able to remove the "movm" instruction. */
3421 if (hash->stack_size + hash->movm_stack_size > 255)
3422 hash->stack_size = 0;
3424 return;
3427 /* Delete some bytes from a section while relaxing. */
3429 static bfd_boolean
3430 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
3431 bfd *abfd;
3432 asection *sec;
3433 bfd_vma addr;
3434 int count;
3436 Elf_Internal_Shdr *symtab_hdr;
3437 unsigned int sec_shndx;
3438 bfd_byte *contents;
3439 Elf_Internal_Rela *irel, *irelend;
3440 Elf_Internal_Rela *irelalign;
3441 bfd_vma toaddr;
3442 Elf_Internal_Sym *isym, *isymend;
3443 struct elf_link_hash_entry **sym_hashes;
3444 struct elf_link_hash_entry **end_hashes;
3445 unsigned int symcount;
3447 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3449 contents = elf_section_data (sec)->this_hdr.contents;
3451 /* The deletion must stop at the next ALIGN reloc for an aligment
3452 power larger than the number of bytes we are deleting. */
3454 irelalign = NULL;
3455 toaddr = sec->size;
3457 irel = elf_section_data (sec)->relocs;
3458 irelend = irel + sec->reloc_count;
3460 /* Actually delete the bytes. */
3461 memmove (contents + addr, contents + addr + count,
3462 (size_t) (toaddr - addr - count));
3463 sec->size -= count;
3465 /* Adjust all the relocs. */
3466 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3468 /* Get the new reloc address. */
3469 if ((irel->r_offset > addr
3470 && irel->r_offset < toaddr))
3471 irel->r_offset -= count;
3474 /* Adjust the local symbols defined in this section. */
3475 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3476 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3477 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3479 if (isym->st_shndx == sec_shndx
3480 && isym->st_value > addr
3481 && isym->st_value < toaddr)
3482 isym->st_value -= count;
3485 /* Now adjust the global symbols defined in this section. */
3486 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3487 - symtab_hdr->sh_info);
3488 sym_hashes = elf_sym_hashes (abfd);
3489 end_hashes = sym_hashes + symcount;
3490 for (; sym_hashes < end_hashes; sym_hashes++)
3492 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3493 if ((sym_hash->root.type == bfd_link_hash_defined
3494 || sym_hash->root.type == bfd_link_hash_defweak)
3495 && sym_hash->root.u.def.section == sec
3496 && sym_hash->root.u.def.value > addr
3497 && sym_hash->root.u.def.value < toaddr)
3499 sym_hash->root.u.def.value -= count;
3503 return TRUE;
3506 /* Return TRUE if a symbol exists at the given address, else return
3507 FALSE. */
3508 static bfd_boolean
3509 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
3510 bfd *abfd;
3511 asection *sec;
3512 Elf_Internal_Sym *isym;
3513 bfd_vma addr;
3515 Elf_Internal_Shdr *symtab_hdr;
3516 unsigned int sec_shndx;
3517 Elf_Internal_Sym *isymend;
3518 struct elf_link_hash_entry **sym_hashes;
3519 struct elf_link_hash_entry **end_hashes;
3520 unsigned int symcount;
3522 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3524 /* Examine all the symbols. */
3525 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3526 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3528 if (isym->st_shndx == sec_shndx
3529 && isym->st_value == addr)
3530 return TRUE;
3533 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3534 - symtab_hdr->sh_info);
3535 sym_hashes = elf_sym_hashes (abfd);
3536 end_hashes = sym_hashes + symcount;
3537 for (; sym_hashes < end_hashes; sym_hashes++)
3539 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3540 if ((sym_hash->root.type == bfd_link_hash_defined
3541 || sym_hash->root.type == bfd_link_hash_defweak)
3542 && sym_hash->root.u.def.section == sec
3543 && sym_hash->root.u.def.value == addr)
3544 return TRUE;
3547 return FALSE;
3550 /* This is a version of bfd_generic_get_relocated_section_contents
3551 which uses mn10300_elf_relocate_section. */
3553 static bfd_byte *
3554 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3555 data, relocatable, symbols)
3556 bfd *output_bfd;
3557 struct bfd_link_info *link_info;
3558 struct bfd_link_order *link_order;
3559 bfd_byte *data;
3560 bfd_boolean relocatable;
3561 asymbol **symbols;
3563 Elf_Internal_Shdr *symtab_hdr;
3564 asection *input_section = link_order->u.indirect.section;
3565 bfd *input_bfd = input_section->owner;
3566 asection **sections = NULL;
3567 Elf_Internal_Rela *internal_relocs = NULL;
3568 Elf_Internal_Sym *isymbuf = NULL;
3570 /* We only need to handle the case of relaxing, or of having a
3571 particular set of section contents, specially. */
3572 if (relocatable
3573 || elf_section_data (input_section)->this_hdr.contents == NULL)
3574 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3575 link_order, data,
3576 relocatable,
3577 symbols);
3579 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3581 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3582 (size_t) input_section->size);
3584 if ((input_section->flags & SEC_RELOC) != 0
3585 && input_section->reloc_count > 0)
3587 asection **secpp;
3588 Elf_Internal_Sym *isym, *isymend;
3589 bfd_size_type amt;
3591 internal_relocs = (_bfd_elf_link_read_relocs
3592 (input_bfd, input_section, (PTR) NULL,
3593 (Elf_Internal_Rela *) NULL, FALSE));
3594 if (internal_relocs == NULL)
3595 goto error_return;
3597 if (symtab_hdr->sh_info != 0)
3599 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3600 if (isymbuf == NULL)
3601 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3602 symtab_hdr->sh_info, 0,
3603 NULL, NULL, NULL);
3604 if (isymbuf == NULL)
3605 goto error_return;
3608 amt = symtab_hdr->sh_info;
3609 amt *= sizeof (asection *);
3610 sections = (asection **) bfd_malloc (amt);
3611 if (sections == NULL && amt != 0)
3612 goto error_return;
3614 isymend = isymbuf + symtab_hdr->sh_info;
3615 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3617 asection *isec;
3619 if (isym->st_shndx == SHN_UNDEF)
3620 isec = bfd_und_section_ptr;
3621 else if (isym->st_shndx == SHN_ABS)
3622 isec = bfd_abs_section_ptr;
3623 else if (isym->st_shndx == SHN_COMMON)
3624 isec = bfd_com_section_ptr;
3625 else
3626 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3628 *secpp = isec;
3631 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3632 input_section, data, internal_relocs,
3633 isymbuf, sections))
3634 goto error_return;
3636 if (sections != NULL)
3637 free (sections);
3638 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3639 free (isymbuf);
3640 if (internal_relocs != elf_section_data (input_section)->relocs)
3641 free (internal_relocs);
3644 return data;
3646 error_return:
3647 if (sections != NULL)
3648 free (sections);
3649 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3650 free (isymbuf);
3651 if (internal_relocs != NULL
3652 && internal_relocs != elf_section_data (input_section)->relocs)
3653 free (internal_relocs);
3654 return NULL;
3657 /* Assorted hash table functions. */
3659 /* Initialize an entry in the link hash table. */
3661 /* Create an entry in an MN10300 ELF linker hash table. */
3663 static struct bfd_hash_entry *
3664 elf32_mn10300_link_hash_newfunc (entry, table, string)
3665 struct bfd_hash_entry *entry;
3666 struct bfd_hash_table *table;
3667 const char *string;
3669 struct elf32_mn10300_link_hash_entry *ret =
3670 (struct elf32_mn10300_link_hash_entry *) entry;
3672 /* Allocate the structure if it has not already been allocated by a
3673 subclass. */
3674 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3675 ret = ((struct elf32_mn10300_link_hash_entry *)
3676 bfd_hash_allocate (table,
3677 sizeof (struct elf32_mn10300_link_hash_entry)));
3678 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3679 return (struct bfd_hash_entry *) ret;
3681 /* Call the allocation method of the superclass. */
3682 ret = ((struct elf32_mn10300_link_hash_entry *)
3683 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3684 table, string));
3685 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
3687 ret->direct_calls = 0;
3688 ret->stack_size = 0;
3689 ret->movm_args = 0;
3690 ret->movm_stack_size = 0;
3691 ret->flags = 0;
3694 return (struct bfd_hash_entry *) ret;
3697 /* Create an mn10300 ELF linker hash table. */
3699 static struct bfd_link_hash_table *
3700 elf32_mn10300_link_hash_table_create (abfd)
3701 bfd *abfd;
3703 struct elf32_mn10300_link_hash_table *ret;
3704 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
3706 ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3707 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
3708 return NULL;
3710 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3711 elf32_mn10300_link_hash_newfunc))
3713 free (ret);
3714 return NULL;
3717 ret->flags = 0;
3718 amt = sizeof (struct elf_link_hash_table);
3719 ret->static_hash_table
3720 = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3721 if (ret->static_hash_table == NULL)
3723 free (ret);
3724 return NULL;
3727 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3728 elf32_mn10300_link_hash_newfunc))
3730 free (ret->static_hash_table);
3731 free (ret);
3732 return NULL;
3734 return &ret->root.root;
3737 /* Free an mn10300 ELF linker hash table. */
3739 static void
3740 elf32_mn10300_link_hash_table_free (hash)
3741 struct bfd_link_hash_table *hash;
3743 struct elf32_mn10300_link_hash_table *ret
3744 = (struct elf32_mn10300_link_hash_table *) hash;
3746 _bfd_generic_link_hash_table_free
3747 ((struct bfd_link_hash_table *) ret->static_hash_table);
3748 _bfd_generic_link_hash_table_free
3749 ((struct bfd_link_hash_table *) ret);
3752 static unsigned long
3753 elf_mn10300_mach (flags)
3754 flagword flags;
3756 switch (flags & EF_MN10300_MACH)
3758 case E_MN10300_MACH_MN10300:
3759 default:
3760 return bfd_mach_mn10300;
3762 case E_MN10300_MACH_AM33:
3763 return bfd_mach_am33;
3765 case E_MN10300_MACH_AM33_2:
3766 return bfd_mach_am33_2;
3770 /* The final processing done just before writing out a MN10300 ELF object
3771 file. This gets the MN10300 architecture right based on the machine
3772 number. */
3774 void
3775 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3776 bfd *abfd;
3777 bfd_boolean linker ATTRIBUTE_UNUSED;
3779 unsigned long val;
3781 switch (bfd_get_mach (abfd))
3783 default:
3784 case bfd_mach_mn10300:
3785 val = E_MN10300_MACH_MN10300;
3786 break;
3788 case bfd_mach_am33:
3789 val = E_MN10300_MACH_AM33;
3790 break;
3792 case bfd_mach_am33_2:
3793 val = E_MN10300_MACH_AM33_2;
3794 break;
3797 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3798 elf_elfheader (abfd)->e_flags |= val;
3801 bfd_boolean
3802 _bfd_mn10300_elf_object_p (abfd)
3803 bfd *abfd;
3805 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3806 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3807 return TRUE;
3810 /* Merge backend specific data from an object file to the output
3811 object file when linking. */
3813 bfd_boolean
3814 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3815 bfd *ibfd;
3816 bfd *obfd;
3818 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3819 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3820 return TRUE;
3822 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3823 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3825 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3826 bfd_get_mach (ibfd)))
3827 return FALSE;
3830 return TRUE;
3833 #define PLT0_ENTRY_SIZE 15
3834 #define PLT_ENTRY_SIZE 20
3835 #define PIC_PLT_ENTRY_SIZE 24
3837 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
3839 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
3840 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
3841 0xf0, 0xf4, /* jmp (a0) */
3844 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
3846 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
3847 0xf0, 0xf4, /* jmp (a0) */
3848 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3849 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
3852 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
3854 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
3855 0xf0, 0xf4, /* jmp (a0) */
3856 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3857 0xf8, 0x22, 8, /* mov (8,a2),a0 */
3858 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
3859 0xf0, 0xf4, /* jmp (a0) */
3862 /* Return size of the first PLT entry. */
3863 #define elf_mn10300_sizeof_plt0(info) \
3864 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
3866 /* Return size of a PLT entry. */
3867 #define elf_mn10300_sizeof_plt(info) \
3868 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
3870 /* Return offset of the PLT0 address in an absolute PLT entry. */
3871 #define elf_mn10300_plt_plt0_offset(info) 16
3873 /* Return offset of the linker in PLT0 entry. */
3874 #define elf_mn10300_plt0_linker_offset(info) 2
3876 /* Return offset of the GOT id in PLT0 entry. */
3877 #define elf_mn10300_plt0_gotid_offset(info) 9
3879 /* Return offset of the temporary in PLT entry */
3880 #define elf_mn10300_plt_temp_offset(info) 8
3882 /* Return offset of the symbol in PLT entry. */
3883 #define elf_mn10300_plt_symbol_offset(info) 2
3885 /* Return offset of the relocation in PLT entry. */
3886 #define elf_mn10300_plt_reloc_offset(info) 11
3888 /* The name of the dynamic interpreter. This is put in the .interp
3889 section. */
3891 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
3893 /* Create dynamic sections when linking against a dynamic object. */
3895 static bfd_boolean
3896 _bfd_mn10300_elf_create_dynamic_sections (abfd, info)
3897 bfd *abfd;
3898 struct bfd_link_info *info;
3900 flagword flags;
3901 asection * s;
3902 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
3903 int ptralign = 0;
3905 switch (bed->s->arch_size)
3907 case 32:
3908 ptralign = 2;
3909 break;
3911 case 64:
3912 ptralign = 3;
3913 break;
3915 default:
3916 bfd_set_error (bfd_error_bad_value);
3917 return FALSE;
3920 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3921 .rel[a].bss sections. */
3923 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3924 | SEC_LINKER_CREATED);
3926 s = bfd_make_section_with_flags (abfd,
3927 (bed->default_use_rela_p
3928 ? ".rela.plt" : ".rel.plt"),
3929 flags | SEC_READONLY);
3930 if (s == NULL
3931 || ! bfd_set_section_alignment (abfd, s, ptralign))
3932 return FALSE;
3934 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
3935 return FALSE;
3938 const char * secname;
3939 char * relname;
3940 flagword secflags;
3941 asection * sec;
3943 for (sec = abfd->sections; sec; sec = sec->next)
3945 secflags = bfd_get_section_flags (abfd, sec);
3946 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3947 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3948 continue;
3950 secname = bfd_get_section_name (abfd, sec);
3951 relname = (char *) bfd_malloc (strlen (secname) + 6);
3952 strcpy (relname, ".rela");
3953 strcat (relname, secname);
3955 s = bfd_make_section_with_flags (abfd, relname,
3956 flags | SEC_READONLY);
3957 if (s == NULL
3958 || ! bfd_set_section_alignment (abfd, s, ptralign))
3959 return FALSE;
3963 if (bed->want_dynbss)
3965 /* The .dynbss section is a place to put symbols which are defined
3966 by dynamic objects, are referenced by regular objects, and are
3967 not functions. We must allocate space for them in the process
3968 image and use a R_*_COPY reloc to tell the dynamic linker to
3969 initialize them at run time. The linker script puts the .dynbss
3970 section into the .bss section of the final image. */
3971 s = bfd_make_section_with_flags (abfd, ".dynbss",
3972 SEC_ALLOC | SEC_LINKER_CREATED);
3973 if (s == NULL)
3974 return FALSE;
3976 /* The .rel[a].bss section holds copy relocs. This section is not
3977 normally needed. We need to create it here, though, so that the
3978 linker will map it to an output section. We can't just create it
3979 only if we need it, because we will not know whether we need it
3980 until we have seen all the input files, and the first time the
3981 main linker code calls BFD after examining all the input files
3982 (size_dynamic_sections) the input sections have already been
3983 mapped to the output sections. If the section turns out not to
3984 be needed, we can discard it later. We will never need this
3985 section when generating a shared object, since they do not use
3986 copy relocs. */
3987 if (! info->shared)
3989 s = bfd_make_section_with_flags (abfd,
3990 (bed->default_use_rela_p
3991 ? ".rela.bss" : ".rel.bss"),
3992 flags | SEC_READONLY);
3993 if (s == NULL
3994 || ! bfd_set_section_alignment (abfd, s, ptralign))
3995 return FALSE;
3999 return TRUE;
4002 /* Adjust a symbol defined by a dynamic object and referenced by a
4003 regular object. The current definition is in some section of the
4004 dynamic object, but we're not including those sections. We have to
4005 change the definition to something the rest of the link can
4006 understand. */
4008 static bfd_boolean
4009 _bfd_mn10300_elf_adjust_dynamic_symbol (info, h)
4010 struct bfd_link_info * info;
4011 struct elf_link_hash_entry * h;
4013 bfd * dynobj;
4014 asection * s;
4015 unsigned int power_of_two;
4017 dynobj = elf_hash_table (info)->dynobj;
4019 /* Make sure we know what is going on here. */
4020 BFD_ASSERT (dynobj != NULL
4021 && (h->needs_plt
4022 || h->u.weakdef != NULL
4023 || (h->def_dynamic
4024 && h->ref_regular
4025 && !h->def_regular)));
4027 /* If this is a function, put it in the procedure linkage table. We
4028 will fill in the contents of the procedure linkage table later,
4029 when we know the address of the .got section. */
4030 if (h->type == STT_FUNC
4031 || h->needs_plt)
4033 if (! info->shared
4034 && !h->def_dynamic
4035 && !h->ref_dynamic)
4037 /* This case can occur if we saw a PLT reloc in an input
4038 file, but the symbol was never referred to by a dynamic
4039 object. In such a case, we don't actually need to build
4040 a procedure linkage table, and we can just do a REL32
4041 reloc instead. */
4042 BFD_ASSERT (h->needs_plt);
4043 return TRUE;
4046 /* Make sure this symbol is output as a dynamic symbol. */
4047 if (h->dynindx == -1)
4049 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4050 return FALSE;
4053 s = bfd_get_section_by_name (dynobj, ".plt");
4054 BFD_ASSERT (s != NULL);
4056 /* If this is the first .plt entry, make room for the special
4057 first entry. */
4058 if (s->size == 0)
4059 s->size += elf_mn10300_sizeof_plt0 (info);
4061 /* If this symbol is not defined in a regular file, and we are
4062 not generating a shared library, then set the symbol to this
4063 location in the .plt. This is required to make function
4064 pointers compare as equal between the normal executable and
4065 the shared library. */
4066 if (! info->shared
4067 && !h->def_regular)
4069 h->root.u.def.section = s;
4070 h->root.u.def.value = s->size;
4073 h->plt.offset = s->size;
4075 /* Make room for this entry. */
4076 s->size += elf_mn10300_sizeof_plt (info);
4078 /* We also need to make an entry in the .got.plt section, which
4079 will be placed in the .got section by the linker script. */
4081 s = bfd_get_section_by_name (dynobj, ".got.plt");
4082 BFD_ASSERT (s != NULL);
4083 s->size += 4;
4085 /* We also need to make an entry in the .rela.plt section. */
4087 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4088 BFD_ASSERT (s != NULL);
4089 s->size += sizeof (Elf32_External_Rela);
4091 return TRUE;
4094 /* If this is a weak symbol, and there is a real definition, the
4095 processor independent code will have arranged for us to see the
4096 real definition first, and we can just use the same value. */
4097 if (h->u.weakdef != NULL)
4099 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4100 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4101 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4102 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4103 return TRUE;
4106 /* This is a reference to a symbol defined by a dynamic object which
4107 is not a function. */
4109 /* If we are creating a shared library, we must presume that the
4110 only references to the symbol are via the global offset table.
4111 For such cases we need not do anything here; the relocations will
4112 be handled correctly by relocate_section. */
4113 if (info->shared)
4114 return TRUE;
4116 /* If there are no references to this symbol that do not use the
4117 GOT, we don't need to generate a copy reloc. */
4118 if (!h->non_got_ref)
4119 return TRUE;
4121 /* We must allocate the symbol in our .dynbss section, which will
4122 become part of the .bss section of the executable. There will be
4123 an entry for this symbol in the .dynsym section. The dynamic
4124 object will contain position independent code, so all references
4125 from the dynamic object to this symbol will go through the global
4126 offset table. The dynamic linker will use the .dynsym entry to
4127 determine the address it must put in the global offset table, so
4128 both the dynamic object and the regular object will refer to the
4129 same memory location for the variable. */
4131 s = bfd_get_section_by_name (dynobj, ".dynbss");
4132 BFD_ASSERT (s != NULL);
4134 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4135 copy the initial value out of the dynamic object and into the
4136 runtime process image. We need to remember the offset into the
4137 .rela.bss section we are going to use. */
4138 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4140 asection * srel;
4142 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4143 BFD_ASSERT (srel != NULL);
4144 srel->size += sizeof (Elf32_External_Rela);
4145 h->needs_copy = 1;
4148 /* We need to figure out the alignment required for this symbol. I
4149 have no idea how ELF linkers handle this. */
4150 power_of_two = bfd_log2 (h->size);
4151 if (power_of_two > 3)
4152 power_of_two = 3;
4154 /* Apply the required alignment. */
4155 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4156 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4158 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4159 return FALSE;
4162 /* Define the symbol as being at this point in the section. */
4163 h->root.u.def.section = s;
4164 h->root.u.def.value = s->size;
4166 /* Increment the section size to make room for the symbol. */
4167 s->size += h->size;
4169 return TRUE;
4172 /* Set the sizes of the dynamic sections. */
4174 static bfd_boolean
4175 _bfd_mn10300_elf_size_dynamic_sections (output_bfd, info)
4176 bfd * output_bfd;
4177 struct bfd_link_info * info;
4179 bfd * dynobj;
4180 asection * s;
4181 bfd_boolean plt;
4182 bfd_boolean relocs;
4183 bfd_boolean reltext;
4185 dynobj = elf_hash_table (info)->dynobj;
4186 BFD_ASSERT (dynobj != NULL);
4188 if (elf_hash_table (info)->dynamic_sections_created)
4190 /* Set the contents of the .interp section to the interpreter. */
4191 if (info->executable)
4193 s = bfd_get_section_by_name (dynobj, ".interp");
4194 BFD_ASSERT (s != NULL);
4195 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4196 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4199 else
4201 /* We may have created entries in the .rela.got section.
4202 However, if we are not creating the dynamic sections, we will
4203 not actually use these entries. Reset the size of .rela.got,
4204 which will cause it to get stripped from the output file
4205 below. */
4206 s = bfd_get_section_by_name (dynobj, ".rela.got");
4207 if (s != NULL)
4208 s->size = 0;
4211 /* The check_relocs and adjust_dynamic_symbol entry points have
4212 determined the sizes of the various dynamic sections. Allocate
4213 memory for them. */
4214 plt = FALSE;
4215 relocs = FALSE;
4216 reltext = FALSE;
4217 for (s = dynobj->sections; s != NULL; s = s->next)
4219 const char * name;
4220 bfd_boolean strip;
4222 if ((s->flags & SEC_LINKER_CREATED) == 0)
4223 continue;
4225 /* It's OK to base decisions on the section name, because none
4226 of the dynobj section names depend upon the input files. */
4227 name = bfd_get_section_name (dynobj, s);
4229 strip = FALSE;
4231 if (strcmp (name, ".plt") == 0)
4233 if (s->size == 0)
4234 /* Strip this section if we don't need it; see the
4235 comment below. */
4236 strip = TRUE;
4237 else
4238 /* Remember whether there is a PLT. */
4239 plt = TRUE;
4241 else if (strncmp (name, ".rela", 5) == 0)
4243 if (s->size == 0)
4245 /* If we don't need this section, strip it from the
4246 output file. This is mostly to handle .rela.bss and
4247 .rela.plt. We must create both sections in
4248 create_dynamic_sections, because they must be created
4249 before the linker maps input sections to output
4250 sections. The linker does that before
4251 adjust_dynamic_symbol is called, and it is that
4252 function which decides whether anything needs to go
4253 into these sections. */
4254 strip = TRUE;
4256 else
4258 asection * target;
4260 /* Remember whether there are any reloc sections other
4261 than .rela.plt. */
4262 if (strcmp (name, ".rela.plt") != 0)
4264 const char * outname;
4266 relocs = TRUE;
4268 /* If this relocation section applies to a read only
4269 section, then we probably need a DT_TEXTREL
4270 entry. The entries in the .rela.plt section
4271 really apply to the .got section, which we
4272 created ourselves and so know is not readonly. */
4273 outname = bfd_get_section_name (output_bfd,
4274 s->output_section);
4275 target = bfd_get_section_by_name (output_bfd, outname + 5);
4276 if (target != NULL
4277 && (target->flags & SEC_READONLY) != 0
4278 && (target->flags & SEC_ALLOC) != 0)
4279 reltext = TRUE;
4282 /* We use the reloc_count field as a counter if we need
4283 to copy relocs into the output file. */
4284 s->reloc_count = 0;
4287 else if (strncmp (name, ".got", 4) != 0)
4288 /* It's not one of our sections, so don't allocate space. */
4289 continue;
4291 if (strip)
4293 s->flags |= SEC_EXCLUDE;
4294 continue;
4297 /* Allocate memory for the section contents. We use bfd_zalloc
4298 here in case unused entries are not reclaimed before the
4299 section's contents are written out. This should not happen,
4300 but this way if it does, we get a R_MN10300_NONE reloc
4301 instead of garbage. */
4302 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4303 if (s->contents == NULL && s->size != 0)
4304 return FALSE;
4307 if (elf_hash_table (info)->dynamic_sections_created)
4309 /* Add some entries to the .dynamic section. We fill in the
4310 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4311 but we must add the entries now so that we get the correct
4312 size for the .dynamic section. The DT_DEBUG entry is filled
4313 in by the dynamic linker and used by the debugger. */
4314 if (! info->shared)
4316 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4317 return FALSE;
4320 if (plt)
4322 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4323 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4324 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4325 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4326 return FALSE;
4329 if (relocs)
4331 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4332 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4333 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4334 sizeof (Elf32_External_Rela)))
4335 return FALSE;
4338 if (reltext)
4340 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4341 return FALSE;
4345 return TRUE;
4348 /* Finish up dynamic symbol handling. We set the contents of various
4349 dynamic sections here. */
4351 static bfd_boolean
4352 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4353 bfd * output_bfd;
4354 struct bfd_link_info * info;
4355 struct elf_link_hash_entry * h;
4356 Elf_Internal_Sym * sym;
4358 bfd * dynobj;
4360 dynobj = elf_hash_table (info)->dynobj;
4362 if (h->plt.offset != (bfd_vma) -1)
4364 asection * splt;
4365 asection * sgot;
4366 asection * srel;
4367 bfd_vma plt_index;
4368 bfd_vma got_offset;
4369 Elf_Internal_Rela rel;
4371 /* This symbol has an entry in the procedure linkage table. Set
4372 it up. */
4374 BFD_ASSERT (h->dynindx != -1);
4376 splt = bfd_get_section_by_name (dynobj, ".plt");
4377 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4378 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4379 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4381 /* Get the index in the procedure linkage table which
4382 corresponds to this symbol. This is the index of this symbol
4383 in all the symbols for which we are making plt entries. The
4384 first entry in the procedure linkage table is reserved. */
4385 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4386 / elf_mn10300_sizeof_plt (info));
4388 /* Get the offset into the .got table of the entry that
4389 corresponds to this function. Each .got entry is 4 bytes.
4390 The first three are reserved. */
4391 got_offset = (plt_index + 3) * 4;
4393 /* Fill in the entry in the procedure linkage table. */
4394 if (! info->shared)
4396 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4397 elf_mn10300_sizeof_plt (info));
4398 bfd_put_32 (output_bfd,
4399 (sgot->output_section->vma
4400 + sgot->output_offset
4401 + got_offset),
4402 (splt->contents + h->plt.offset
4403 + elf_mn10300_plt_symbol_offset (info)));
4405 bfd_put_32 (output_bfd,
4406 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4407 (splt->contents + h->plt.offset
4408 + elf_mn10300_plt_plt0_offset (info)));
4410 else
4412 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4413 elf_mn10300_sizeof_plt (info));
4415 bfd_put_32 (output_bfd, got_offset,
4416 (splt->contents + h->plt.offset
4417 + elf_mn10300_plt_symbol_offset (info)));
4420 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4421 (splt->contents + h->plt.offset
4422 + elf_mn10300_plt_reloc_offset (info)));
4424 /* Fill in the entry in the global offset table. */
4425 bfd_put_32 (output_bfd,
4426 (splt->output_section->vma
4427 + splt->output_offset
4428 + h->plt.offset
4429 + elf_mn10300_plt_temp_offset (info)),
4430 sgot->contents + got_offset);
4432 /* Fill in the entry in the .rela.plt section. */
4433 rel.r_offset = (sgot->output_section->vma
4434 + sgot->output_offset
4435 + got_offset);
4436 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4437 rel.r_addend = 0;
4438 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4439 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4440 + plt_index));
4442 if (!h->def_regular)
4443 /* Mark the symbol as undefined, rather than as defined in
4444 the .plt section. Leave the value alone. */
4445 sym->st_shndx = SHN_UNDEF;
4448 if (h->got.offset != (bfd_vma) -1)
4450 asection * sgot;
4451 asection * srel;
4452 Elf_Internal_Rela rel;
4454 /* This symbol has an entry in the global offset table. Set it up. */
4456 sgot = bfd_get_section_by_name (dynobj, ".got");
4457 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4458 BFD_ASSERT (sgot != NULL && srel != NULL);
4460 rel.r_offset = (sgot->output_section->vma
4461 + sgot->output_offset
4462 + (h->got.offset &~ 1));
4464 /* If this is a -Bsymbolic link, and the symbol is defined
4465 locally, we just want to emit a RELATIVE reloc. Likewise if
4466 the symbol was forced to be local because of a version file.
4467 The entry in the global offset table will already have been
4468 initialized in the relocate_section function. */
4469 if (info->shared
4470 && (info->symbolic || h->dynindx == -1)
4471 && h->def_regular)
4473 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4474 rel.r_addend = (h->root.u.def.value
4475 + h->root.u.def.section->output_section->vma
4476 + h->root.u.def.section->output_offset);
4478 else
4480 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4481 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4482 rel.r_addend = 0;
4485 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4486 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4487 + srel->reloc_count));
4488 ++ srel->reloc_count;
4491 if (h->needs_copy)
4493 asection * s;
4494 Elf_Internal_Rela rel;
4496 /* This symbol needs a copy reloc. Set it up. */
4497 BFD_ASSERT (h->dynindx != -1
4498 && (h->root.type == bfd_link_hash_defined
4499 || h->root.type == bfd_link_hash_defweak));
4501 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4502 ".rela.bss");
4503 BFD_ASSERT (s != NULL);
4505 rel.r_offset = (h->root.u.def.value
4506 + h->root.u.def.section->output_section->vma
4507 + h->root.u.def.section->output_offset);
4508 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4509 rel.r_addend = 0;
4510 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4511 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4512 + s->reloc_count));
4513 ++ s->reloc_count;
4516 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4517 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4518 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4519 sym->st_shndx = SHN_ABS;
4521 return TRUE;
4524 /* Finish up the dynamic sections. */
4526 static bfd_boolean
4527 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd, info)
4528 bfd * output_bfd;
4529 struct bfd_link_info * info;
4531 bfd * dynobj;
4532 asection * sgot;
4533 asection * sdyn;
4535 dynobj = elf_hash_table (info)->dynobj;
4537 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4538 BFD_ASSERT (sgot != NULL);
4539 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4541 if (elf_hash_table (info)->dynamic_sections_created)
4543 asection * splt;
4544 Elf32_External_Dyn * dyncon;
4545 Elf32_External_Dyn * dynconend;
4547 BFD_ASSERT (sdyn != NULL);
4549 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4550 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4552 for (; dyncon < dynconend; dyncon++)
4554 Elf_Internal_Dyn dyn;
4555 const char * name;
4556 asection * s;
4558 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4560 switch (dyn.d_tag)
4562 default:
4563 break;
4565 case DT_PLTGOT:
4566 name = ".got";
4567 goto get_vma;
4569 case DT_JMPREL:
4570 name = ".rela.plt";
4571 get_vma:
4572 s = bfd_get_section_by_name (output_bfd, name);
4573 BFD_ASSERT (s != NULL);
4574 dyn.d_un.d_ptr = s->vma;
4575 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4576 break;
4578 case DT_PLTRELSZ:
4579 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4580 BFD_ASSERT (s != NULL);
4581 dyn.d_un.d_val = s->size;
4582 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4583 break;
4585 case DT_RELASZ:
4586 /* My reading of the SVR4 ABI indicates that the
4587 procedure linkage table relocs (DT_JMPREL) should be
4588 included in the overall relocs (DT_RELA). This is
4589 what Solaris does. However, UnixWare can not handle
4590 that case. Therefore, we override the DT_RELASZ entry
4591 here to make it not include the JMPREL relocs. Since
4592 the linker script arranges for .rela.plt to follow all
4593 other relocation sections, we don't have to worry
4594 about changing the DT_RELA entry. */
4595 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4596 if (s != NULL)
4597 dyn.d_un.d_val -= s->size;
4598 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4599 break;
4603 /* Fill in the first entry in the procedure linkage table. */
4604 splt = bfd_get_section_by_name (dynobj, ".plt");
4605 if (splt && splt->size > 0)
4607 if (info->shared)
4609 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4610 elf_mn10300_sizeof_plt (info));
4612 else
4614 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4615 bfd_put_32 (output_bfd,
4616 sgot->output_section->vma + sgot->output_offset + 4,
4617 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4618 bfd_put_32 (output_bfd,
4619 sgot->output_section->vma + sgot->output_offset + 8,
4620 splt->contents + elf_mn10300_plt0_linker_offset (info));
4623 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4624 really seem like the right value. */
4625 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4629 /* Fill in the first three entries in the global offset table. */
4630 if (sgot->size > 0)
4632 if (sdyn == NULL)
4633 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4634 else
4635 bfd_put_32 (output_bfd,
4636 sdyn->output_section->vma + sdyn->output_offset,
4637 sgot->contents);
4638 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4639 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4642 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4644 return TRUE;
4647 /* Classify relocation types, such that combreloc can sort them
4648 properly. */
4650 static enum elf_reloc_type_class
4651 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4653 switch ((int) ELF32_R_TYPE (rela->r_info))
4655 case R_MN10300_RELATIVE:
4656 return reloc_class_relative;
4657 case R_MN10300_JMP_SLOT:
4658 return reloc_class_plt;
4659 case R_MN10300_COPY:
4660 return reloc_class_copy;
4661 default:
4662 return reloc_class_normal;
4666 #ifndef ELF_ARCH
4667 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4668 #define TARGET_LITTLE_NAME "elf32-mn10300"
4669 #define ELF_ARCH bfd_arch_mn10300
4670 #define ELF_MACHINE_CODE EM_MN10300
4671 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4672 #define ELF_MAXPAGESIZE 0x1000
4673 #endif
4675 #define elf_info_to_howto mn10300_info_to_howto
4676 #define elf_info_to_howto_rel 0
4677 #define elf_backend_can_gc_sections 1
4678 #define elf_backend_rela_normal 1
4679 #define elf_backend_check_relocs mn10300_elf_check_relocs
4680 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4681 #define elf_backend_relocate_section mn10300_elf_relocate_section
4682 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4683 #define bfd_elf32_bfd_get_relocated_section_contents \
4684 mn10300_elf_get_relocated_section_contents
4685 #define bfd_elf32_bfd_link_hash_table_create \
4686 elf32_mn10300_link_hash_table_create
4687 #define bfd_elf32_bfd_link_hash_table_free \
4688 elf32_mn10300_link_hash_table_free
4690 #ifndef elf_symbol_leading_char
4691 #define elf_symbol_leading_char '_'
4692 #endif
4694 /* So we can set bits in e_flags. */
4695 #define elf_backend_final_write_processing \
4696 _bfd_mn10300_elf_final_write_processing
4697 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4699 #define bfd_elf32_bfd_merge_private_bfd_data \
4700 _bfd_mn10300_elf_merge_private_bfd_data
4702 #define elf_backend_can_gc_sections 1
4703 #define elf_backend_create_dynamic_sections \
4704 _bfd_mn10300_elf_create_dynamic_sections
4705 #define elf_backend_adjust_dynamic_symbol \
4706 _bfd_mn10300_elf_adjust_dynamic_symbol
4707 #define elf_backend_size_dynamic_sections \
4708 _bfd_mn10300_elf_size_dynamic_sections
4709 #define elf_backend_finish_dynamic_symbol \
4710 _bfd_mn10300_elf_finish_dynamic_symbol
4711 #define elf_backend_finish_dynamic_sections \
4712 _bfd_mn10300_elf_finish_dynamic_sections
4714 #define elf_backend_reloc_type_class \
4715 _bfd_mn10300_elf_reloc_type_class
4717 #define elf_backend_want_got_plt 1
4718 #define elf_backend_plt_readonly 1
4719 #define elf_backend_want_plt_sym 0
4720 #define elf_backend_got_header_size 12
4722 #include "elf32-target.h"