ChangeLog:
[binutils.git] / bfd / elf-m10300.c
blob030af7ea8a3c62d4df487fd4c37397f62b8297ce
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 0, (const char *) NULL, FALSE, bed->collect, &bh)))
624 return FALSE;
625 h = (struct elf_link_hash_entry *) bh;
626 h->def_regular = 1;
627 h->type = STT_OBJECT;
629 if (info->shared
630 && ! bfd_elf_link_record_dynamic_symbol (info, h))
631 return FALSE;
633 elf_hash_table (info)->hgot = h;
635 /* The first bit of the global offset table is the header. */
636 s->size += bed->got_header_size;
638 return TRUE;
641 static reloc_howto_type *
642 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
643 bfd *abfd ATTRIBUTE_UNUSED;
644 bfd_reloc_code_real_type code;
646 unsigned int i;
648 for (i = 0;
649 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
650 i++)
652 if (mn10300_reloc_map[i].bfd_reloc_val == code)
653 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
656 return NULL;
659 /* Set the howto pointer for an MN10300 ELF reloc. */
661 static void
662 mn10300_info_to_howto (abfd, cache_ptr, dst)
663 bfd *abfd ATTRIBUTE_UNUSED;
664 arelent *cache_ptr;
665 Elf_Internal_Rela *dst;
667 unsigned int r_type;
669 r_type = ELF32_R_TYPE (dst->r_info);
670 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
671 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
674 /* Look through the relocs for a section during the first phase.
675 Since we don't do .gots or .plts, we just need to consider the
676 virtual table relocs for gc. */
678 static bfd_boolean
679 mn10300_elf_check_relocs (abfd, info, sec, relocs)
680 bfd *abfd;
681 struct bfd_link_info *info;
682 asection *sec;
683 const Elf_Internal_Rela *relocs;
685 Elf_Internal_Shdr *symtab_hdr;
686 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
687 const Elf_Internal_Rela *rel;
688 const Elf_Internal_Rela *rel_end;
689 bfd * dynobj;
690 bfd_vma * local_got_offsets;
691 asection * sgot;
692 asection * srelgot;
693 asection * sreloc;
695 sgot = NULL;
696 srelgot = NULL;
697 sreloc = NULL;
699 if (info->relocatable)
700 return TRUE;
702 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
703 sym_hashes = elf_sym_hashes (abfd);
704 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
705 if (!elf_bad_symtab (abfd))
706 sym_hashes_end -= symtab_hdr->sh_info;
708 dynobj = elf_hash_table (info)->dynobj;
709 local_got_offsets = elf_local_got_offsets (abfd);
710 rel_end = relocs + sec->reloc_count;
711 for (rel = relocs; rel < rel_end; rel++)
713 struct elf_link_hash_entry *h;
714 unsigned long r_symndx;
716 r_symndx = ELF32_R_SYM (rel->r_info);
717 if (r_symndx < symtab_hdr->sh_info)
718 h = NULL;
719 else
721 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
722 while (h->root.type == bfd_link_hash_indirect
723 || h->root.type == bfd_link_hash_warning)
724 h = (struct elf_link_hash_entry *) h->root.u.i.link;
727 /* Some relocs require a global offset table. */
728 if (dynobj == NULL)
730 switch (ELF32_R_TYPE (rel->r_info))
732 case R_MN10300_GOT32:
733 case R_MN10300_GOT24:
734 case R_MN10300_GOT16:
735 case R_MN10300_GOTOFF32:
736 case R_MN10300_GOTOFF24:
737 case R_MN10300_GOTOFF16:
738 case R_MN10300_GOTPC32:
739 case R_MN10300_GOTPC16:
740 elf_hash_table (info)->dynobj = dynobj = abfd;
741 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
742 return FALSE;
743 break;
745 default:
746 break;
750 switch (ELF32_R_TYPE (rel->r_info))
752 /* This relocation describes the C++ object vtable hierarchy.
753 Reconstruct it for later use during GC. */
754 case R_MN10300_GNU_VTINHERIT:
755 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
756 return FALSE;
757 break;
759 /* This relocation describes which C++ vtable entries are actually
760 used. Record for later use during GC. */
761 case R_MN10300_GNU_VTENTRY:
762 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
763 return FALSE;
764 break;
765 case R_MN10300_GOT32:
766 case R_MN10300_GOT24:
767 case R_MN10300_GOT16:
768 /* This symbol requires a global offset table entry. */
770 if (sgot == NULL)
772 sgot = bfd_get_section_by_name (dynobj, ".got");
773 BFD_ASSERT (sgot != NULL);
776 if (srelgot == NULL
777 && (h != NULL || info->shared))
779 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
780 if (srelgot == NULL)
782 srelgot = bfd_make_section_with_flags (dynobj,
783 ".rela.got",
784 (SEC_ALLOC
785 | SEC_LOAD
786 | SEC_HAS_CONTENTS
787 | SEC_IN_MEMORY
788 | SEC_LINKER_CREATED
789 | SEC_READONLY));
790 if (srelgot == NULL
791 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
792 return FALSE;
796 if (h != NULL)
798 if (h->got.offset != (bfd_vma) -1)
799 /* We have already allocated space in the .got. */
800 break;
802 h->got.offset = sgot->size;
804 /* Make sure this symbol is output as a dynamic symbol. */
805 if (h->dynindx == -1)
807 if (! bfd_elf_link_record_dynamic_symbol (info, h))
808 return FALSE;
811 srelgot->size += sizeof (Elf32_External_Rela);
813 else
815 /* This is a global offset table entry for a local
816 symbol. */
817 if (local_got_offsets == NULL)
819 size_t size;
820 unsigned int i;
822 size = symtab_hdr->sh_info * sizeof (bfd_vma);
823 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
825 if (local_got_offsets == NULL)
826 return FALSE;
827 elf_local_got_offsets (abfd) = local_got_offsets;
829 for (i = 0; i < symtab_hdr->sh_info; i++)
830 local_got_offsets[i] = (bfd_vma) -1;
833 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
834 /* We have already allocated space in the .got. */
835 break;
837 local_got_offsets[r_symndx] = sgot->size;
839 if (info->shared)
840 /* If we are generating a shared object, we need to
841 output a R_MN10300_RELATIVE reloc so that the dynamic
842 linker can adjust this GOT entry. */
843 srelgot->size += sizeof (Elf32_External_Rela);
846 sgot->size += 4;
848 break;
850 case R_MN10300_PLT32:
851 case R_MN10300_PLT16:
852 /* This symbol requires a procedure linkage table entry. We
853 actually build the entry in adjust_dynamic_symbol,
854 because this might be a case of linking PIC code which is
855 never referenced by a dynamic object, in which case we
856 don't need to generate a procedure linkage table entry
857 after all. */
859 /* If this is a local symbol, we resolve it directly without
860 creating a procedure linkage table entry. */
861 if (h == NULL)
862 continue;
864 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
865 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
866 break;
868 h->needs_plt = 1;
870 break;
872 case R_MN10300_24:
873 case R_MN10300_16:
874 case R_MN10300_8:
875 case R_MN10300_PCREL32:
876 case R_MN10300_PCREL16:
877 case R_MN10300_PCREL8:
878 if (h != NULL)
879 h->non_got_ref = 1;
880 break;
882 case R_MN10300_32:
883 if (h != NULL)
884 h->non_got_ref = 1;
886 /* If we are creating a shared library, then we need to copy
887 the reloc into the shared library. */
888 if (info->shared
889 && (sec->flags & SEC_ALLOC) != 0)
891 /* When creating a shared object, we must copy these
892 reloc types into the output file. We create a reloc
893 section in dynobj and make room for this reloc. */
894 if (sreloc == NULL)
896 const char * name;
898 name = (bfd_elf_string_from_elf_section
899 (abfd,
900 elf_elfheader (abfd)->e_shstrndx,
901 elf_section_data (sec)->rel_hdr.sh_name));
902 if (name == NULL)
903 return FALSE;
905 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
906 && strcmp (bfd_get_section_name (abfd, sec),
907 name + 5) == 0);
909 sreloc = bfd_get_section_by_name (dynobj, name);
910 if (sreloc == NULL)
912 flagword flags;
914 flags = (SEC_HAS_CONTENTS | SEC_READONLY
915 | SEC_IN_MEMORY | SEC_LINKER_CREATED);
916 if ((sec->flags & SEC_ALLOC) != 0)
917 flags |= SEC_ALLOC | SEC_LOAD;
918 sreloc = bfd_make_section_with_flags (dynobj,
919 name,
920 flags);
921 if (sreloc == NULL
922 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
923 return FALSE;
927 sreloc->size += sizeof (Elf32_External_Rela);
930 break;
934 return TRUE;
937 /* Return the section that should be marked against GC for a given
938 relocation. */
940 static asection *
941 mn10300_elf_gc_mark_hook (sec, info, rel, h, sym)
942 asection *sec;
943 struct bfd_link_info *info ATTRIBUTE_UNUSED;
944 Elf_Internal_Rela *rel;
945 struct elf_link_hash_entry *h;
946 Elf_Internal_Sym *sym;
948 if (h != NULL)
950 switch (ELF32_R_TYPE (rel->r_info))
952 case R_MN10300_GNU_VTINHERIT:
953 case R_MN10300_GNU_VTENTRY:
954 break;
956 default:
957 switch (h->root.type)
959 case bfd_link_hash_defined:
960 case bfd_link_hash_defweak:
961 return h->root.u.def.section;
963 case bfd_link_hash_common:
964 return h->root.u.c.p->section;
966 default:
967 break;
971 else
972 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
974 return NULL;
977 /* Perform a relocation as part of a final link. */
978 static bfd_reloc_status_type
979 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
980 input_section, contents, offset, value,
981 addend, h, symndx, info, sym_sec, is_local)
982 reloc_howto_type *howto;
983 bfd *input_bfd;
984 bfd *output_bfd ATTRIBUTE_UNUSED;
985 asection *input_section;
986 bfd_byte *contents;
987 bfd_vma offset;
988 bfd_vma value;
989 bfd_vma addend;
990 struct elf_link_hash_entry * h;
991 unsigned long symndx;
992 struct bfd_link_info *info;
993 asection *sym_sec ATTRIBUTE_UNUSED;
994 int is_local ATTRIBUTE_UNUSED;
996 unsigned long r_type = howto->type;
997 bfd_byte *hit_data = contents + offset;
998 bfd * dynobj;
999 bfd_vma * local_got_offsets;
1000 asection * sgot;
1001 asection * splt;
1002 asection * sreloc;
1004 dynobj = elf_hash_table (info)->dynobj;
1005 local_got_offsets = elf_local_got_offsets (input_bfd);
1007 sgot = NULL;
1008 splt = NULL;
1009 sreloc = NULL;
1011 switch (r_type)
1013 case R_MN10300_24:
1014 case R_MN10300_16:
1015 case R_MN10300_8:
1016 case R_MN10300_PCREL8:
1017 case R_MN10300_PCREL16:
1018 case R_MN10300_PCREL32:
1019 case R_MN10300_GOTOFF32:
1020 case R_MN10300_GOTOFF24:
1021 case R_MN10300_GOTOFF16:
1022 if (info->shared
1023 && (input_section->flags & SEC_ALLOC) != 0
1024 && h != NULL
1025 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1026 return bfd_reloc_dangerous;
1029 switch (r_type)
1031 case R_MN10300_NONE:
1032 return bfd_reloc_ok;
1034 case R_MN10300_32:
1035 if (info->shared
1036 && (input_section->flags & SEC_ALLOC) != 0)
1038 Elf_Internal_Rela outrel;
1039 bfd_boolean skip, relocate;
1041 /* When generating a shared object, these relocations are
1042 copied into the output file to be resolved at run
1043 time. */
1044 if (sreloc == NULL)
1046 const char * name;
1048 name = (bfd_elf_string_from_elf_section
1049 (input_bfd,
1050 elf_elfheader (input_bfd)->e_shstrndx,
1051 elf_section_data (input_section)->rel_hdr.sh_name));
1052 if (name == NULL)
1053 return FALSE;
1055 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1056 && strcmp (bfd_get_section_name (input_bfd,
1057 input_section),
1058 name + 5) == 0);
1060 sreloc = bfd_get_section_by_name (dynobj, name);
1061 BFD_ASSERT (sreloc != NULL);
1064 skip = FALSE;
1066 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1067 input_section, offset);
1068 if (outrel.r_offset == (bfd_vma) -1)
1069 skip = TRUE;
1071 outrel.r_offset += (input_section->output_section->vma
1072 + input_section->output_offset);
1074 if (skip)
1076 memset (&outrel, 0, sizeof outrel);
1077 relocate = FALSE;
1079 else
1081 /* h->dynindx may be -1 if this symbol was marked to
1082 become local. */
1083 if (h == NULL
1084 || SYMBOL_REFERENCES_LOCAL (info, h))
1086 relocate = TRUE;
1087 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1088 outrel.r_addend = value + addend;
1090 else
1092 BFD_ASSERT (h->dynindx != -1);
1093 relocate = FALSE;
1094 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1095 outrel.r_addend = value + addend;
1099 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1100 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1101 + sreloc->reloc_count));
1102 ++sreloc->reloc_count;
1104 /* If this reloc is against an external symbol, we do
1105 not want to fiddle with the addend. Otherwise, we
1106 need to include the symbol value so that it becomes
1107 an addend for the dynamic reloc. */
1108 if (! relocate)
1109 return bfd_reloc_ok;
1111 value += addend;
1112 bfd_put_32 (input_bfd, value, hit_data);
1113 return bfd_reloc_ok;
1115 case R_MN10300_24:
1116 value += addend;
1118 if ((long) value > 0x7fffff || (long) value < -0x800000)
1119 return bfd_reloc_overflow;
1121 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1122 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1123 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1124 return bfd_reloc_ok;
1126 case R_MN10300_16:
1127 value += addend;
1129 if ((long) value > 0x7fff || (long) value < -0x8000)
1130 return bfd_reloc_overflow;
1132 bfd_put_16 (input_bfd, value, hit_data);
1133 return bfd_reloc_ok;
1135 case R_MN10300_8:
1136 value += addend;
1138 if ((long) value > 0x7f || (long) value < -0x80)
1139 return bfd_reloc_overflow;
1141 bfd_put_8 (input_bfd, value, hit_data);
1142 return bfd_reloc_ok;
1144 case R_MN10300_PCREL8:
1145 value -= (input_section->output_section->vma
1146 + input_section->output_offset);
1147 value -= offset;
1148 value += addend;
1150 if ((long) value > 0xff || (long) value < -0x100)
1151 return bfd_reloc_overflow;
1153 bfd_put_8 (input_bfd, value, hit_data);
1154 return bfd_reloc_ok;
1156 case R_MN10300_PCREL16:
1157 value -= (input_section->output_section->vma
1158 + input_section->output_offset);
1159 value -= offset;
1160 value += addend;
1162 if ((long) value > 0xffff || (long) value < -0x10000)
1163 return bfd_reloc_overflow;
1165 bfd_put_16 (input_bfd, value, hit_data);
1166 return bfd_reloc_ok;
1168 case R_MN10300_PCREL32:
1169 value -= (input_section->output_section->vma
1170 + input_section->output_offset);
1171 value -= offset;
1172 value += addend;
1174 bfd_put_32 (input_bfd, value, hit_data);
1175 return bfd_reloc_ok;
1177 case R_MN10300_GNU_VTINHERIT:
1178 case R_MN10300_GNU_VTENTRY:
1179 return bfd_reloc_ok;
1181 case R_MN10300_GOTPC32:
1182 /* Use global offset table as symbol value. */
1184 value = bfd_get_section_by_name (dynobj,
1185 ".got")->output_section->vma;
1186 value -= (input_section->output_section->vma
1187 + input_section->output_offset);
1188 value -= offset;
1189 value += addend;
1191 bfd_put_32 (input_bfd, value, hit_data);
1192 return bfd_reloc_ok;
1194 case R_MN10300_GOTPC16:
1195 /* Use global offset table as symbol value. */
1197 value = bfd_get_section_by_name (dynobj,
1198 ".got")->output_section->vma;
1199 value -= (input_section->output_section->vma
1200 + input_section->output_offset);
1201 value -= offset;
1202 value += addend;
1204 if ((long) value > 0xffff || (long) value < -0x10000)
1205 return bfd_reloc_overflow;
1207 bfd_put_16 (input_bfd, value, hit_data);
1208 return bfd_reloc_ok;
1210 case R_MN10300_GOTOFF32:
1211 value -= bfd_get_section_by_name (dynobj,
1212 ".got")->output_section->vma;
1213 value += addend;
1215 bfd_put_32 (input_bfd, value, hit_data);
1216 return bfd_reloc_ok;
1218 case R_MN10300_GOTOFF24:
1219 value -= bfd_get_section_by_name (dynobj,
1220 ".got")->output_section->vma;
1221 value += addend;
1223 if ((long) value > 0x7fffff || (long) value < -0x800000)
1224 return bfd_reloc_overflow;
1226 bfd_put_8 (input_bfd, value, hit_data);
1227 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1228 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1229 return bfd_reloc_ok;
1231 case R_MN10300_GOTOFF16:
1232 value -= bfd_get_section_by_name (dynobj,
1233 ".got")->output_section->vma;
1234 value += addend;
1236 if ((long) value > 0xffff || (long) value < -0x10000)
1237 return bfd_reloc_overflow;
1239 bfd_put_16 (input_bfd, value, hit_data);
1240 return bfd_reloc_ok;
1242 case R_MN10300_PLT32:
1243 if (h != NULL
1244 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1245 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1246 && h->plt.offset != (bfd_vma) -1)
1248 asection * splt;
1250 splt = bfd_get_section_by_name (dynobj, ".plt");
1252 value = (splt->output_section->vma
1253 + splt->output_offset
1254 + h->plt.offset) - value;
1257 value -= (input_section->output_section->vma
1258 + input_section->output_offset);
1259 value -= offset;
1260 value += addend;
1262 bfd_put_32 (input_bfd, value, hit_data);
1263 return bfd_reloc_ok;
1265 case R_MN10300_PLT16:
1266 if (h != NULL
1267 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1268 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1269 && h->plt.offset != (bfd_vma) -1)
1271 asection * splt;
1273 splt = bfd_get_section_by_name (dynobj, ".plt");
1275 value = (splt->output_section->vma
1276 + splt->output_offset
1277 + h->plt.offset) - value;
1280 value -= (input_section->output_section->vma
1281 + input_section->output_offset);
1282 value -= offset;
1283 value += addend;
1285 if ((long) value > 0xffff || (long) value < -0x10000)
1286 return bfd_reloc_overflow;
1288 bfd_put_16 (input_bfd, value, hit_data);
1289 return bfd_reloc_ok;
1291 case R_MN10300_GOT32:
1292 case R_MN10300_GOT24:
1293 case R_MN10300_GOT16:
1295 asection * sgot;
1297 sgot = bfd_get_section_by_name (dynobj, ".got");
1299 if (h != NULL)
1301 bfd_vma off;
1303 off = h->got.offset;
1304 BFD_ASSERT (off != (bfd_vma) -1);
1306 if (! elf_hash_table (info)->dynamic_sections_created
1307 || SYMBOL_REFERENCES_LOCAL (info, h))
1308 /* This is actually a static link, or it is a
1309 -Bsymbolic link and the symbol is defined
1310 locally, or the symbol was forced to be local
1311 because of a version file. We must initialize
1312 this entry in the global offset table.
1314 When doing a dynamic link, we create a .rela.got
1315 relocation entry to initialize the value. This
1316 is done in the finish_dynamic_symbol routine. */
1317 bfd_put_32 (output_bfd, value,
1318 sgot->contents + off);
1320 value = sgot->output_offset + off;
1322 else
1324 bfd_vma off;
1326 off = elf_local_got_offsets (input_bfd)[symndx];
1328 bfd_put_32 (output_bfd, value, sgot->contents + off);
1330 if (info->shared)
1332 asection * srelgot;
1333 Elf_Internal_Rela outrel;
1335 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1336 BFD_ASSERT (srelgot != NULL);
1338 outrel.r_offset = (sgot->output_section->vma
1339 + sgot->output_offset
1340 + off);
1341 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1342 outrel.r_addend = value;
1343 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1344 (bfd_byte *) (((Elf32_External_Rela *)
1345 srelgot->contents)
1346 + srelgot->reloc_count));
1347 ++ srelgot->reloc_count;
1350 value = sgot->output_offset + off;
1354 value += addend;
1356 if (r_type == R_MN10300_GOT32)
1358 bfd_put_32 (input_bfd, value, hit_data);
1359 return bfd_reloc_ok;
1361 else if (r_type == R_MN10300_GOT24)
1363 if ((long) value > 0x7fffff || (long) value < -0x800000)
1364 return bfd_reloc_overflow;
1366 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1367 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1368 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1369 return bfd_reloc_ok;
1371 else if (r_type == R_MN10300_GOT16)
1373 if ((long) value > 0xffff || (long) value < -0x10000)
1374 return bfd_reloc_overflow;
1376 bfd_put_16 (input_bfd, value, hit_data);
1377 return bfd_reloc_ok;
1379 /* Fall through. */
1381 default:
1382 return bfd_reloc_notsupported;
1386 /* Relocate an MN10300 ELF section. */
1387 static bfd_boolean
1388 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1389 contents, relocs, local_syms, local_sections)
1390 bfd *output_bfd;
1391 struct bfd_link_info *info;
1392 bfd *input_bfd;
1393 asection *input_section;
1394 bfd_byte *contents;
1395 Elf_Internal_Rela *relocs;
1396 Elf_Internal_Sym *local_syms;
1397 asection **local_sections;
1399 Elf_Internal_Shdr *symtab_hdr;
1400 struct elf_link_hash_entry **sym_hashes;
1401 Elf_Internal_Rela *rel, *relend;
1403 if (info->relocatable)
1404 return TRUE;
1406 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1407 sym_hashes = elf_sym_hashes (input_bfd);
1409 rel = relocs;
1410 relend = relocs + input_section->reloc_count;
1411 for (; rel < relend; rel++)
1413 int r_type;
1414 reloc_howto_type *howto;
1415 unsigned long r_symndx;
1416 Elf_Internal_Sym *sym;
1417 asection *sec;
1418 struct elf32_mn10300_link_hash_entry *h;
1419 bfd_vma relocation;
1420 bfd_reloc_status_type r;
1422 r_symndx = ELF32_R_SYM (rel->r_info);
1423 r_type = ELF32_R_TYPE (rel->r_info);
1424 howto = elf_mn10300_howto_table + r_type;
1426 /* Just skip the vtable gc relocs. */
1427 if (r_type == R_MN10300_GNU_VTINHERIT
1428 || r_type == R_MN10300_GNU_VTENTRY)
1429 continue;
1431 h = NULL;
1432 sym = NULL;
1433 sec = NULL;
1434 if (r_symndx < symtab_hdr->sh_info)
1436 sym = local_syms + r_symndx;
1437 sec = local_sections[r_symndx];
1438 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1440 else
1442 bfd_boolean unresolved_reloc;
1443 bfd_boolean warned;
1444 struct elf_link_hash_entry *hh;
1446 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1447 r_symndx, symtab_hdr, sym_hashes,
1448 hh, sec, relocation,
1449 unresolved_reloc, warned);
1451 h = (struct elf32_mn10300_link_hash_entry *) hh;
1453 if ((h->root.root.type == bfd_link_hash_defined
1454 || h->root.root.type == bfd_link_hash_defweak)
1455 && ( r_type == R_MN10300_GOTPC32
1456 || r_type == R_MN10300_GOTPC16
1457 || (( r_type == R_MN10300_PLT32
1458 || r_type == R_MN10300_PLT16)
1459 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1460 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1461 && h->root.plt.offset != (bfd_vma) -1)
1462 || (( r_type == R_MN10300_GOT32
1463 || r_type == R_MN10300_GOT24
1464 || r_type == R_MN10300_GOT16)
1465 && elf_hash_table (info)->dynamic_sections_created
1466 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1467 || (r_type == R_MN10300_32
1468 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1469 && ((input_section->flags & SEC_ALLOC) != 0
1470 /* DWARF will emit R_MN10300_32 relocations
1471 in its sections against symbols defined
1472 externally in shared libraries. We can't
1473 do anything with them here. */
1474 || ((input_section->flags & SEC_DEBUGGING) != 0
1475 && h->root.def_dynamic)))))
1476 /* In these cases, we don't need the relocation
1477 value. We check specially because in some
1478 obscure cases sec->output_section will be NULL. */
1479 relocation = 0;
1481 else if (unresolved_reloc)
1482 (*_bfd_error_handler)
1483 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1484 bfd_get_filename (input_bfd), h->root.root.root.string,
1485 bfd_get_section_name (input_bfd, input_section));
1488 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1489 input_section,
1490 contents, rel->r_offset,
1491 relocation, rel->r_addend,
1492 (struct elf_link_hash_entry *)h,
1493 r_symndx,
1494 info, sec, h == NULL);
1496 if (r != bfd_reloc_ok)
1498 const char *name;
1499 const char *msg = (const char *) 0;
1501 if (h != NULL)
1502 name = h->root.root.root.string;
1503 else
1505 name = (bfd_elf_string_from_elf_section
1506 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1507 if (name == NULL || *name == '\0')
1508 name = bfd_section_name (input_bfd, sec);
1511 switch (r)
1513 case bfd_reloc_overflow:
1514 if (! ((*info->callbacks->reloc_overflow)
1515 (info, (h ? &h->root.root : NULL), name,
1516 howto->name, (bfd_vma) 0, input_bfd,
1517 input_section, rel->r_offset)))
1518 return FALSE;
1519 break;
1521 case bfd_reloc_undefined:
1522 if (! ((*info->callbacks->undefined_symbol)
1523 (info, name, input_bfd, input_section,
1524 rel->r_offset, TRUE)))
1525 return FALSE;
1526 break;
1528 case bfd_reloc_outofrange:
1529 msg = _("internal error: out of range error");
1530 goto common_error;
1532 case bfd_reloc_notsupported:
1533 msg = _("internal error: unsupported relocation error");
1534 goto common_error;
1536 case bfd_reloc_dangerous:
1537 msg = _("internal error: dangerous error");
1538 goto common_error;
1540 default:
1541 msg = _("internal error: unknown error");
1542 /* fall through */
1544 common_error:
1545 if (!((*info->callbacks->warning)
1546 (info, msg, name, input_bfd, input_section,
1547 rel->r_offset)))
1548 return FALSE;
1549 break;
1554 return TRUE;
1557 /* Finish initializing one hash table entry. */
1558 static bfd_boolean
1559 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
1560 struct bfd_hash_entry *gen_entry;
1561 PTR in_args;
1563 struct elf32_mn10300_link_hash_entry *entry;
1564 struct bfd_link_info *link_info = (struct bfd_link_info *)in_args;
1565 unsigned int byte_count = 0;
1567 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1569 if (entry->root.root.type == bfd_link_hash_warning)
1570 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1572 /* If we already know we want to convert "call" to "calls" for calls
1573 to this symbol, then return now. */
1574 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1575 return TRUE;
1577 /* If there are no named calls to this symbol, or there's nothing we
1578 can move from the function itself into the "call" instruction,
1579 then note that all "call" instructions should be converted into
1580 "calls" instructions and return. If a symbol is available for
1581 dynamic symbol resolution (overridable or overriding), avoid
1582 custom calling conventions. */
1583 if (entry->direct_calls == 0
1584 || (entry->stack_size == 0 && entry->movm_args == 0)
1585 || (elf_hash_table (link_info)->dynamic_sections_created
1586 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1587 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1589 /* Make a note that we should convert "call" instructions to "calls"
1590 instructions for calls to this symbol. */
1591 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1592 return TRUE;
1595 /* We may be able to move some instructions from the function itself into
1596 the "call" instruction. Count how many bytes we might be able to
1597 eliminate in the function itself. */
1599 /* A movm instruction is two bytes. */
1600 if (entry->movm_args)
1601 byte_count += 2;
1603 /* Count the insn to allocate stack space too. */
1604 if (entry->stack_size > 0)
1606 if (entry->stack_size <= 128)
1607 byte_count += 3;
1608 else
1609 byte_count += 4;
1612 /* If using "call" will result in larger code, then turn all
1613 the associated "call" instructions into "calls" instructions. */
1614 if (byte_count < entry->direct_calls)
1615 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1617 /* This routine never fails. */
1618 return TRUE;
1621 /* This function handles relaxing for the mn10300.
1623 There are quite a few relaxing opportunities available on the mn10300:
1625 * calls:32 -> calls:16 2 bytes
1626 * call:32 -> call:16 2 bytes
1628 * call:32 -> calls:32 1 byte
1629 * call:16 -> calls:16 1 byte
1630 * These are done anytime using "calls" would result
1631 in smaller code, or when necessary to preserve the
1632 meaning of the program.
1634 * call:32 varies
1635 * call:16
1636 * In some circumstances we can move instructions
1637 from a function prologue into a "call" instruction.
1638 This is only done if the resulting code is no larger
1639 than the original code.
1641 * jmp:32 -> jmp:16 2 bytes
1642 * jmp:16 -> bra:8 1 byte
1644 * If the previous instruction is a conditional branch
1645 around the jump/bra, we may be able to reverse its condition
1646 and change its target to the jump's target. The jump/bra
1647 can then be deleted. 2 bytes
1649 * mov abs32 -> mov abs16 1 or 2 bytes
1651 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
1652 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
1654 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
1655 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
1657 We don't handle imm16->imm8 or d16->d8 as they're very rare
1658 and somewhat more difficult to support. */
1660 static bfd_boolean
1661 mn10300_elf_relax_section (abfd, sec, link_info, again)
1662 bfd *abfd;
1663 asection *sec;
1664 struct bfd_link_info *link_info;
1665 bfd_boolean *again;
1667 Elf_Internal_Shdr *symtab_hdr;
1668 Elf_Internal_Rela *internal_relocs = NULL;
1669 Elf_Internal_Rela *irel, *irelend;
1670 bfd_byte *contents = NULL;
1671 Elf_Internal_Sym *isymbuf = NULL;
1672 struct elf32_mn10300_link_hash_table *hash_table;
1673 asection *section = sec;
1675 /* Assume nothing changes. */
1676 *again = FALSE;
1678 /* We need a pointer to the mn10300 specific hash table. */
1679 hash_table = elf32_mn10300_hash_table (link_info);
1681 /* Initialize fields in each hash table entry the first time through. */
1682 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
1684 bfd *input_bfd;
1686 /* Iterate over all the input bfds. */
1687 for (input_bfd = link_info->input_bfds;
1688 input_bfd != NULL;
1689 input_bfd = input_bfd->link_next)
1691 /* We're going to need all the symbols for each bfd. */
1692 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1693 if (symtab_hdr->sh_info != 0)
1695 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1696 if (isymbuf == NULL)
1697 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1698 symtab_hdr->sh_info, 0,
1699 NULL, NULL, NULL);
1700 if (isymbuf == NULL)
1701 goto error_return;
1704 /* Iterate over each section in this bfd. */
1705 for (section = input_bfd->sections;
1706 section != NULL;
1707 section = section->next)
1709 struct elf32_mn10300_link_hash_entry *hash;
1710 Elf_Internal_Sym *sym;
1711 asection *sym_sec = NULL;
1712 const char *sym_name;
1713 char *new_name;
1715 /* If there's nothing to do in this section, skip it. */
1716 if (! (((section->flags & SEC_RELOC) != 0
1717 && section->reloc_count != 0)
1718 || (section->flags & SEC_CODE) != 0))
1719 continue;
1721 /* Get cached copy of section contents if it exists. */
1722 if (elf_section_data (section)->this_hdr.contents != NULL)
1723 contents = elf_section_data (section)->this_hdr.contents;
1724 else if (section->size != 0)
1726 /* Go get them off disk. */
1727 if (!bfd_malloc_and_get_section (input_bfd, section,
1728 &contents))
1729 goto error_return;
1731 else
1732 contents = NULL;
1734 /* If there aren't any relocs, then there's nothing to do. */
1735 if ((section->flags & SEC_RELOC) != 0
1736 && section->reloc_count != 0)
1739 /* Get a copy of the native relocations. */
1740 internal_relocs = (_bfd_elf_link_read_relocs
1741 (input_bfd, section, (PTR) NULL,
1742 (Elf_Internal_Rela *) NULL,
1743 link_info->keep_memory));
1744 if (internal_relocs == NULL)
1745 goto error_return;
1747 /* Now examine each relocation. */
1748 irel = internal_relocs;
1749 irelend = irel + section->reloc_count;
1750 for (; irel < irelend; irel++)
1752 long r_type;
1753 unsigned long r_index;
1754 unsigned char code;
1756 r_type = ELF32_R_TYPE (irel->r_info);
1757 r_index = ELF32_R_SYM (irel->r_info);
1759 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
1760 goto error_return;
1762 /* We need the name and hash table entry of the target
1763 symbol! */
1764 hash = NULL;
1765 sym = NULL;
1766 sym_sec = NULL;
1768 if (r_index < symtab_hdr->sh_info)
1770 /* A local symbol. */
1771 Elf_Internal_Sym *isym;
1772 struct elf_link_hash_table *elftab;
1773 bfd_size_type amt;
1775 isym = isymbuf + r_index;
1776 if (isym->st_shndx == SHN_UNDEF)
1777 sym_sec = bfd_und_section_ptr;
1778 else if (isym->st_shndx == SHN_ABS)
1779 sym_sec = bfd_abs_section_ptr;
1780 else if (isym->st_shndx == SHN_COMMON)
1781 sym_sec = bfd_com_section_ptr;
1782 else
1783 sym_sec
1784 = bfd_section_from_elf_index (input_bfd,
1785 isym->st_shndx);
1787 sym_name
1788 = bfd_elf_string_from_elf_section (input_bfd,
1789 (symtab_hdr
1790 ->sh_link),
1791 isym->st_name);
1793 /* If it isn't a function, then we don't care
1794 about it. */
1795 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
1796 continue;
1798 /* Tack on an ID so we can uniquely identify this
1799 local symbol in the global hash table. */
1800 amt = strlen (sym_name) + 10;
1801 new_name = bfd_malloc (amt);
1802 if (new_name == 0)
1803 goto error_return;
1805 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
1806 sym_name = new_name;
1808 elftab = &hash_table->static_hash_table->root;
1809 hash = ((struct elf32_mn10300_link_hash_entry *)
1810 elf_link_hash_lookup (elftab, sym_name,
1811 TRUE, TRUE, FALSE));
1812 free (new_name);
1814 else
1816 r_index -= symtab_hdr->sh_info;
1817 hash = (struct elf32_mn10300_link_hash_entry *)
1818 elf_sym_hashes (input_bfd)[r_index];
1821 /* If this is not a "call" instruction, then we
1822 should convert "call" instructions to "calls"
1823 instructions. */
1824 code = bfd_get_8 (input_bfd,
1825 contents + irel->r_offset - 1);
1826 if (code != 0xdd && code != 0xcd)
1827 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1829 /* If this is a jump/call, then bump the
1830 direct_calls counter. Else force "call" to
1831 "calls" conversions. */
1832 if (r_type == R_MN10300_PCREL32
1833 || r_type == R_MN10300_PLT32
1834 || r_type == R_MN10300_PLT16
1835 || r_type == R_MN10300_PCREL16)
1836 hash->direct_calls++;
1837 else
1838 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1842 /* Now look at the actual contents to get the stack size,
1843 and a list of what registers were saved in the prologue
1844 (ie movm_args). */
1845 if ((section->flags & SEC_CODE) != 0)
1847 Elf_Internal_Sym *isym, *isymend;
1848 unsigned int sec_shndx;
1849 struct elf_link_hash_entry **hashes;
1850 struct elf_link_hash_entry **end_hashes;
1851 unsigned int symcount;
1853 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1854 section);
1856 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1857 - symtab_hdr->sh_info);
1858 hashes = elf_sym_hashes (input_bfd);
1859 end_hashes = hashes + symcount;
1861 /* Look at each function defined in this section and
1862 update info for that function. */
1863 isymend = isymbuf + symtab_hdr->sh_info;
1864 for (isym = isymbuf; isym < isymend; isym++)
1866 if (isym->st_shndx == sec_shndx
1867 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
1869 struct elf_link_hash_table *elftab;
1870 bfd_size_type amt;
1871 struct elf_link_hash_entry **lhashes = hashes;
1873 /* Skip a local symbol if it aliases a
1874 global one. */
1875 for (; lhashes < end_hashes; lhashes++)
1877 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
1878 if ((hash->root.root.type == bfd_link_hash_defined
1879 || hash->root.root.type == bfd_link_hash_defweak)
1880 && hash->root.root.u.def.section == section
1881 && hash->root.type == STT_FUNC
1882 && hash->root.root.u.def.value == isym->st_value)
1883 break;
1885 if (lhashes != end_hashes)
1886 continue;
1888 if (isym->st_shndx == SHN_UNDEF)
1889 sym_sec = bfd_und_section_ptr;
1890 else if (isym->st_shndx == SHN_ABS)
1891 sym_sec = bfd_abs_section_ptr;
1892 else if (isym->st_shndx == SHN_COMMON)
1893 sym_sec = bfd_com_section_ptr;
1894 else
1895 sym_sec
1896 = bfd_section_from_elf_index (input_bfd,
1897 isym->st_shndx);
1899 sym_name = (bfd_elf_string_from_elf_section
1900 (input_bfd, symtab_hdr->sh_link,
1901 isym->st_name));
1903 /* Tack on an ID so we can uniquely identify this
1904 local symbol in the global hash table. */
1905 amt = strlen (sym_name) + 10;
1906 new_name = bfd_malloc (amt);
1907 if (new_name == 0)
1908 goto error_return;
1910 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
1911 sym_name = new_name;
1913 elftab = &hash_table->static_hash_table->root;
1914 hash = ((struct elf32_mn10300_link_hash_entry *)
1915 elf_link_hash_lookup (elftab, sym_name,
1916 TRUE, TRUE, FALSE));
1917 free (new_name);
1918 compute_function_info (input_bfd, hash,
1919 isym->st_value, contents);
1923 for (; hashes < end_hashes; hashes++)
1925 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1926 if ((hash->root.root.type == bfd_link_hash_defined
1927 || hash->root.root.type == bfd_link_hash_defweak)
1928 && hash->root.root.u.def.section == section
1929 && hash->root.type == STT_FUNC)
1930 compute_function_info (input_bfd, hash,
1931 (hash)->root.root.u.def.value,
1932 contents);
1936 /* Cache or free any memory we allocated for the relocs. */
1937 if (internal_relocs != NULL
1938 && elf_section_data (section)->relocs != internal_relocs)
1939 free (internal_relocs);
1940 internal_relocs = NULL;
1942 /* Cache or free any memory we allocated for the contents. */
1943 if (contents != NULL
1944 && elf_section_data (section)->this_hdr.contents != contents)
1946 if (! link_info->keep_memory)
1947 free (contents);
1948 else
1950 /* Cache the section contents for elf_link_input_bfd. */
1951 elf_section_data (section)->this_hdr.contents = contents;
1954 contents = NULL;
1957 /* Cache or free any memory we allocated for the symbols. */
1958 if (isymbuf != NULL
1959 && symtab_hdr->contents != (unsigned char *) isymbuf)
1961 if (! link_info->keep_memory)
1962 free (isymbuf);
1963 else
1965 /* Cache the symbols for elf_link_input_bfd. */
1966 symtab_hdr->contents = (unsigned char *) isymbuf;
1969 isymbuf = NULL;
1972 /* Now iterate on each symbol in the hash table and perform
1973 the final initialization steps on each. */
1974 elf32_mn10300_link_hash_traverse (hash_table,
1975 elf32_mn10300_finish_hash_table_entry,
1976 link_info);
1977 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1978 elf32_mn10300_finish_hash_table_entry,
1979 link_info);
1981 /* All entries in the hash table are fully initialized. */
1982 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1984 /* Now that everything has been initialized, go through each
1985 code section and delete any prologue insns which will be
1986 redundant because their operations will be performed by
1987 a "call" instruction. */
1988 for (input_bfd = link_info->input_bfds;
1989 input_bfd != NULL;
1990 input_bfd = input_bfd->link_next)
1992 /* We're going to need all the local symbols for each bfd. */
1993 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1994 if (symtab_hdr->sh_info != 0)
1996 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1997 if (isymbuf == NULL)
1998 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1999 symtab_hdr->sh_info, 0,
2000 NULL, NULL, NULL);
2001 if (isymbuf == NULL)
2002 goto error_return;
2005 /* Walk over each section in this bfd. */
2006 for (section = input_bfd->sections;
2007 section != NULL;
2008 section = section->next)
2010 unsigned int sec_shndx;
2011 Elf_Internal_Sym *isym, *isymend;
2012 struct elf_link_hash_entry **hashes;
2013 struct elf_link_hash_entry **end_hashes;
2014 unsigned int symcount;
2016 /* Skip non-code sections and empty sections. */
2017 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2018 continue;
2020 if (section->reloc_count != 0)
2022 /* Get a copy of the native relocations. */
2023 internal_relocs = (_bfd_elf_link_read_relocs
2024 (input_bfd, section, (PTR) NULL,
2025 (Elf_Internal_Rela *) NULL,
2026 link_info->keep_memory));
2027 if (internal_relocs == NULL)
2028 goto error_return;
2031 /* Get cached copy of section contents if it exists. */
2032 if (elf_section_data (section)->this_hdr.contents != NULL)
2033 contents = elf_section_data (section)->this_hdr.contents;
2034 else
2036 /* Go get them off disk. */
2037 if (!bfd_malloc_and_get_section (input_bfd, section,
2038 &contents))
2039 goto error_return;
2042 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2043 section);
2045 /* Now look for any function in this section which needs
2046 insns deleted from its prologue. */
2047 isymend = isymbuf + symtab_hdr->sh_info;
2048 for (isym = isymbuf; isym < isymend; isym++)
2050 struct elf32_mn10300_link_hash_entry *sym_hash;
2051 asection *sym_sec = NULL;
2052 const char *sym_name;
2053 char *new_name;
2054 struct elf_link_hash_table *elftab;
2055 bfd_size_type amt;
2057 if (isym->st_shndx != sec_shndx)
2058 continue;
2060 if (isym->st_shndx == SHN_UNDEF)
2061 sym_sec = bfd_und_section_ptr;
2062 else if (isym->st_shndx == SHN_ABS)
2063 sym_sec = bfd_abs_section_ptr;
2064 else if (isym->st_shndx == SHN_COMMON)
2065 sym_sec = bfd_com_section_ptr;
2066 else
2067 sym_sec
2068 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2070 sym_name
2071 = bfd_elf_string_from_elf_section (input_bfd,
2072 symtab_hdr->sh_link,
2073 isym->st_name);
2075 /* Tack on an ID so we can uniquely identify this
2076 local symbol in the global hash table. */
2077 amt = strlen (sym_name) + 10;
2078 new_name = bfd_malloc (amt);
2079 if (new_name == 0)
2080 goto error_return;
2081 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2082 sym_name = new_name;
2084 elftab = &hash_table->static_hash_table->root;
2085 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
2086 elf_link_hash_lookup (elftab, sym_name,
2087 FALSE, FALSE, FALSE));
2089 free (new_name);
2090 if (sym_hash == NULL)
2091 continue;
2093 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2094 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2096 int bytes = 0;
2098 /* Note that we've changed things. */
2099 elf_section_data (section)->relocs = internal_relocs;
2100 elf_section_data (section)->this_hdr.contents = contents;
2101 symtab_hdr->contents = (unsigned char *) isymbuf;
2103 /* Count how many bytes we're going to delete. */
2104 if (sym_hash->movm_args)
2105 bytes += 2;
2107 if (sym_hash->stack_size > 0)
2109 if (sym_hash->stack_size <= 128)
2110 bytes += 3;
2111 else
2112 bytes += 4;
2115 /* Note that we've deleted prologue bytes for this
2116 function. */
2117 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2119 /* Actually delete the bytes. */
2120 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2121 section,
2122 isym->st_value,
2123 bytes))
2124 goto error_return;
2126 /* Something changed. Not strictly necessary, but
2127 may lead to more relaxing opportunities. */
2128 *again = TRUE;
2132 /* Look for any global functions in this section which
2133 need insns deleted from their prologues. */
2134 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2135 - symtab_hdr->sh_info);
2136 hashes = elf_sym_hashes (input_bfd);
2137 end_hashes = hashes + symcount;
2138 for (; hashes < end_hashes; hashes++)
2140 struct elf32_mn10300_link_hash_entry *sym_hash;
2142 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2143 if ((sym_hash->root.root.type == bfd_link_hash_defined
2144 || sym_hash->root.root.type == bfd_link_hash_defweak)
2145 && sym_hash->root.root.u.def.section == section
2146 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2147 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2149 int bytes = 0;
2150 bfd_vma symval;
2152 /* Note that we've changed things. */
2153 elf_section_data (section)->relocs = internal_relocs;
2154 elf_section_data (section)->this_hdr.contents = contents;
2155 symtab_hdr->contents = (unsigned char *) isymbuf;
2157 /* Count how many bytes we're going to delete. */
2158 if (sym_hash->movm_args)
2159 bytes += 2;
2161 if (sym_hash->stack_size > 0)
2163 if (sym_hash->stack_size <= 128)
2164 bytes += 3;
2165 else
2166 bytes += 4;
2169 /* Note that we've deleted prologue bytes for this
2170 function. */
2171 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2173 /* Actually delete the bytes. */
2174 symval = sym_hash->root.root.u.def.value;
2175 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2176 section,
2177 symval,
2178 bytes))
2179 goto error_return;
2181 /* Something changed. Not strictly necessary, but
2182 may lead to more relaxing opportunities. */
2183 *again = TRUE;
2187 /* Cache or free any memory we allocated for the relocs. */
2188 if (internal_relocs != NULL
2189 && elf_section_data (section)->relocs != internal_relocs)
2190 free (internal_relocs);
2191 internal_relocs = NULL;
2193 /* Cache or free any memory we allocated for the contents. */
2194 if (contents != NULL
2195 && elf_section_data (section)->this_hdr.contents != contents)
2197 if (! link_info->keep_memory)
2198 free (contents);
2199 else
2201 /* Cache the section contents for elf_link_input_bfd. */
2202 elf_section_data (section)->this_hdr.contents = contents;
2205 contents = NULL;
2208 /* Cache or free any memory we allocated for the symbols. */
2209 if (isymbuf != NULL
2210 && symtab_hdr->contents != (unsigned char *) isymbuf)
2212 if (! link_info->keep_memory)
2213 free (isymbuf);
2214 else
2216 /* Cache the symbols for elf_link_input_bfd. */
2217 symtab_hdr->contents = (unsigned char *) isymbuf;
2220 isymbuf = NULL;
2224 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2225 contents = NULL;
2226 internal_relocs = NULL;
2227 isymbuf = NULL;
2228 /* For error_return. */
2229 section = sec;
2231 /* We don't have to do anything for a relocatable link, if
2232 this section does not have relocs, or if this is not a
2233 code section. */
2234 if (link_info->relocatable
2235 || (sec->flags & SEC_RELOC) == 0
2236 || sec->reloc_count == 0
2237 || (sec->flags & SEC_CODE) == 0)
2238 return TRUE;
2240 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2242 /* Get a copy of the native relocations. */
2243 internal_relocs = (_bfd_elf_link_read_relocs
2244 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2245 link_info->keep_memory));
2246 if (internal_relocs == NULL)
2247 goto error_return;
2249 /* Walk through them looking for relaxing opportunities. */
2250 irelend = internal_relocs + sec->reloc_count;
2251 for (irel = internal_relocs; irel < irelend; irel++)
2253 bfd_vma symval;
2254 struct elf32_mn10300_link_hash_entry *h = NULL;
2256 /* If this isn't something that can be relaxed, then ignore
2257 this reloc. */
2258 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2259 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2260 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2261 continue;
2263 /* Get the section contents if we haven't done so already. */
2264 if (contents == NULL)
2266 /* Get cached copy if it exists. */
2267 if (elf_section_data (sec)->this_hdr.contents != NULL)
2268 contents = elf_section_data (sec)->this_hdr.contents;
2269 else
2271 /* Go get them off disk. */
2272 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2273 goto error_return;
2277 /* Read this BFD's symbols if we haven't done so already. */
2278 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2280 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2281 if (isymbuf == NULL)
2282 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2283 symtab_hdr->sh_info, 0,
2284 NULL, NULL, NULL);
2285 if (isymbuf == NULL)
2286 goto error_return;
2289 /* Get the value of the symbol referred to by the reloc. */
2290 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2292 Elf_Internal_Sym *isym;
2293 asection *sym_sec = NULL;
2294 const char *sym_name;
2295 char *new_name;
2296 bfd_vma saved_addend;
2298 /* A local symbol. */
2299 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2300 if (isym->st_shndx == SHN_UNDEF)
2301 sym_sec = bfd_und_section_ptr;
2302 else if (isym->st_shndx == SHN_ABS)
2303 sym_sec = bfd_abs_section_ptr;
2304 else if (isym->st_shndx == SHN_COMMON)
2305 sym_sec = bfd_com_section_ptr;
2306 else
2307 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2309 sym_name = bfd_elf_string_from_elf_section (abfd,
2310 symtab_hdr->sh_link,
2311 isym->st_name);
2313 if ((sym_sec->flags & SEC_MERGE)
2314 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
2315 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2317 saved_addend = irel->r_addend;
2318 symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
2319 symval += irel->r_addend;
2320 irel->r_addend = saved_addend;
2322 else
2324 symval = (isym->st_value
2325 + sym_sec->output_section->vma
2326 + sym_sec->output_offset);
2328 /* Tack on an ID so we can uniquely identify this
2329 local symbol in the global hash table. */
2330 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2331 if (new_name == 0)
2332 goto error_return;
2333 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2334 sym_name = new_name;
2336 h = (struct elf32_mn10300_link_hash_entry *)
2337 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2338 sym_name, FALSE, FALSE, FALSE);
2339 free (new_name);
2341 else
2343 unsigned long indx;
2345 /* An external symbol. */
2346 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2347 h = (struct elf32_mn10300_link_hash_entry *)
2348 (elf_sym_hashes (abfd)[indx]);
2349 BFD_ASSERT (h != NULL);
2350 if (h->root.root.type != bfd_link_hash_defined
2351 && h->root.root.type != bfd_link_hash_defweak)
2353 /* This appears to be a reference to an undefined
2354 symbol. Just ignore it--it will be caught by the
2355 regular reloc processing. */
2356 continue;
2359 symval = (h->root.root.u.def.value
2360 + h->root.root.u.def.section->output_section->vma
2361 + h->root.root.u.def.section->output_offset);
2364 /* For simplicity of coding, we are going to modify the section
2365 contents, the section relocs, and the BFD symbol table. We
2366 must tell the rest of the code not to free up this
2367 information. It would be possible to instead create a table
2368 of changes which have to be made, as is done in coff-mips.c;
2369 that would be more work, but would require less memory when
2370 the linker is run. */
2372 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2373 branch/call, also deal with "call" -> "calls" conversions and
2374 insertion of prologue data into "call" instructions. */
2375 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2376 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2378 bfd_vma value = symval;
2380 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2381 && h != NULL
2382 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2383 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2384 && h->root.plt.offset != (bfd_vma) -1)
2386 asection * splt;
2388 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2389 ->dynobj, ".plt");
2391 value = ((splt->output_section->vma
2392 + splt->output_offset
2393 + h->root.plt.offset)
2394 - (sec->output_section->vma
2395 + sec->output_offset
2396 + irel->r_offset));
2399 /* If we've got a "call" instruction that needs to be turned
2400 into a "calls" instruction, do so now. It saves a byte. */
2401 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2403 unsigned char code;
2405 /* Get the opcode. */
2406 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2408 /* Make sure we're working with a "call" instruction! */
2409 if (code == 0xdd)
2411 /* Note that we've changed the relocs, section contents,
2412 etc. */
2413 elf_section_data (sec)->relocs = internal_relocs;
2414 elf_section_data (sec)->this_hdr.contents = contents;
2415 symtab_hdr->contents = (unsigned char *) isymbuf;
2417 /* Fix the opcode. */
2418 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2419 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2421 /* Fix irel->r_offset and irel->r_addend. */
2422 irel->r_offset += 1;
2423 irel->r_addend += 1;
2425 /* Delete one byte of data. */
2426 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2427 irel->r_offset + 3, 1))
2428 goto error_return;
2430 /* That will change things, so, we should relax again.
2431 Note that this is not required, and it may be slow. */
2432 *again = TRUE;
2435 else if (h)
2437 /* We've got a "call" instruction which needs some data
2438 from target function filled in. */
2439 unsigned char code;
2441 /* Get the opcode. */
2442 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2444 /* Insert data from the target function into the "call"
2445 instruction if needed. */
2446 if (code == 0xdd)
2448 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2449 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2450 contents + irel->r_offset + 5);
2454 /* Deal with pc-relative gunk. */
2455 value -= (sec->output_section->vma + sec->output_offset);
2456 value -= irel->r_offset;
2457 value += irel->r_addend;
2459 /* See if the value will fit in 16 bits, note the high value is
2460 0x7fff + 2 as the target will be two bytes closer if we are
2461 able to relax. */
2462 if ((long) value < 0x8001 && (long) value > -0x8000)
2464 unsigned char code;
2466 /* Get the opcode. */
2467 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2469 if (code != 0xdc && code != 0xdd && code != 0xff)
2470 continue;
2472 /* Note that we've changed the relocs, section contents, etc. */
2473 elf_section_data (sec)->relocs = internal_relocs;
2474 elf_section_data (sec)->this_hdr.contents = contents;
2475 symtab_hdr->contents = (unsigned char *) isymbuf;
2477 /* Fix the opcode. */
2478 if (code == 0xdc)
2479 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2480 else if (code == 0xdd)
2481 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2482 else if (code == 0xff)
2483 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2485 /* Fix the relocation's type. */
2486 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2487 (ELF32_R_TYPE (irel->r_info)
2488 == (int) R_MN10300_PLT32)
2489 ? R_MN10300_PLT16 :
2490 R_MN10300_PCREL16);
2492 /* Delete two bytes of data. */
2493 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2494 irel->r_offset + 1, 2))
2495 goto error_return;
2497 /* That will change things, so, we should relax again.
2498 Note that this is not required, and it may be slow. */
2499 *again = TRUE;
2503 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2504 branch. */
2505 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2507 bfd_vma value = symval;
2509 /* If we've got a "call" instruction that needs to be turned
2510 into a "calls" instruction, do so now. It saves a byte. */
2511 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2513 unsigned char code;
2515 /* Get the opcode. */
2516 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2518 /* Make sure we're working with a "call" instruction! */
2519 if (code == 0xcd)
2521 /* Note that we've changed the relocs, section contents,
2522 etc. */
2523 elf_section_data (sec)->relocs = internal_relocs;
2524 elf_section_data (sec)->this_hdr.contents = contents;
2525 symtab_hdr->contents = (unsigned char *) isymbuf;
2527 /* Fix the opcode. */
2528 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
2529 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2531 /* Fix irel->r_offset and irel->r_addend. */
2532 irel->r_offset += 1;
2533 irel->r_addend += 1;
2535 /* Delete one byte of data. */
2536 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2537 irel->r_offset + 1, 1))
2538 goto error_return;
2540 /* That will change things, so, we should relax again.
2541 Note that this is not required, and it may be slow. */
2542 *again = TRUE;
2545 else if (h)
2547 unsigned char code;
2549 /* Get the opcode. */
2550 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2552 /* Insert data from the target function into the "call"
2553 instruction if needed. */
2554 if (code == 0xcd)
2556 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
2557 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2558 contents + irel->r_offset + 3);
2562 /* Deal with pc-relative gunk. */
2563 value -= (sec->output_section->vma + sec->output_offset);
2564 value -= irel->r_offset;
2565 value += irel->r_addend;
2567 /* See if the value will fit in 8 bits, note the high value is
2568 0x7f + 1 as the target will be one bytes closer if we are
2569 able to relax. */
2570 if ((long) value < 0x80 && (long) value > -0x80)
2572 unsigned char code;
2574 /* Get the opcode. */
2575 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2577 if (code != 0xcc)
2578 continue;
2580 /* Note that we've changed the relocs, section contents, etc. */
2581 elf_section_data (sec)->relocs = internal_relocs;
2582 elf_section_data (sec)->this_hdr.contents = contents;
2583 symtab_hdr->contents = (unsigned char *) isymbuf;
2585 /* Fix the opcode. */
2586 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
2588 /* Fix the relocation's type. */
2589 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2590 R_MN10300_PCREL8);
2592 /* Delete one byte of data. */
2593 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2594 irel->r_offset + 1, 1))
2595 goto error_return;
2597 /* That will change things, so, we should relax again.
2598 Note that this is not required, and it may be slow. */
2599 *again = TRUE;
2603 /* Try to eliminate an unconditional 8 bit pc-relative branch
2604 which immediately follows a conditional 8 bit pc-relative
2605 branch around the unconditional branch.
2607 original: new:
2608 bCC lab1 bCC' lab2
2609 bra lab2
2610 lab1: lab1:
2612 This happens when the bCC can't reach lab2 at assembly time,
2613 but due to other relaxations it can reach at link time. */
2614 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
2616 Elf_Internal_Rela *nrel;
2617 bfd_vma value = symval;
2618 unsigned char code;
2620 /* Deal with pc-relative gunk. */
2621 value -= (sec->output_section->vma + sec->output_offset);
2622 value -= irel->r_offset;
2623 value += irel->r_addend;
2625 /* Do nothing if this reloc is the last byte in the section. */
2626 if (irel->r_offset == sec->size)
2627 continue;
2629 /* See if the next instruction is an unconditional pc-relative
2630 branch, more often than not this test will fail, so we
2631 test it first to speed things up. */
2632 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2633 if (code != 0xca)
2634 continue;
2636 /* Also make sure the next relocation applies to the next
2637 instruction and that it's a pc-relative 8 bit branch. */
2638 nrel = irel + 1;
2639 if (nrel == irelend
2640 || irel->r_offset + 2 != nrel->r_offset
2641 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
2642 continue;
2644 /* Make sure our destination immediately follows the
2645 unconditional branch. */
2646 if (symval != (sec->output_section->vma + sec->output_offset
2647 + irel->r_offset + 3))
2648 continue;
2650 /* Now make sure we are a conditional branch. This may not
2651 be necessary, but why take the chance.
2653 Note these checks assume that R_MN10300_PCREL8 relocs
2654 only occur on bCC and bCCx insns. If they occured
2655 elsewhere, we'd need to know the start of this insn
2656 for this check to be accurate. */
2657 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2658 if (code != 0xc0 && code != 0xc1 && code != 0xc2
2659 && code != 0xc3 && code != 0xc4 && code != 0xc5
2660 && code != 0xc6 && code != 0xc7 && code != 0xc8
2661 && code != 0xc9 && code != 0xe8 && code != 0xe9
2662 && code != 0xea && code != 0xeb)
2663 continue;
2665 /* We also have to be sure there is no symbol/label
2666 at the unconditional branch. */
2667 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
2668 irel->r_offset + 1))
2669 continue;
2671 /* Note that we've changed the relocs, section contents, etc. */
2672 elf_section_data (sec)->relocs = internal_relocs;
2673 elf_section_data (sec)->this_hdr.contents = contents;
2674 symtab_hdr->contents = (unsigned char *) isymbuf;
2676 /* Reverse the condition of the first branch. */
2677 switch (code)
2679 case 0xc8:
2680 code = 0xc9;
2681 break;
2682 case 0xc9:
2683 code = 0xc8;
2684 break;
2685 case 0xc0:
2686 code = 0xc2;
2687 break;
2688 case 0xc2:
2689 code = 0xc0;
2690 break;
2691 case 0xc3:
2692 code = 0xc1;
2693 break;
2694 case 0xc1:
2695 code = 0xc3;
2696 break;
2697 case 0xc4:
2698 code = 0xc6;
2699 break;
2700 case 0xc6:
2701 code = 0xc4;
2702 break;
2703 case 0xc7:
2704 code = 0xc5;
2705 break;
2706 case 0xc5:
2707 code = 0xc7;
2708 break;
2709 case 0xe8:
2710 code = 0xe9;
2711 break;
2712 case 0x9d:
2713 code = 0xe8;
2714 break;
2715 case 0xea:
2716 code = 0xeb;
2717 break;
2718 case 0xeb:
2719 code = 0xea;
2720 break;
2722 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2724 /* Set the reloc type and symbol for the first branch
2725 from the second branch. */
2726 irel->r_info = nrel->r_info;
2728 /* Make the reloc for the second branch a null reloc. */
2729 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
2730 R_MN10300_NONE);
2732 /* Delete two bytes of data. */
2733 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2734 irel->r_offset + 1, 2))
2735 goto error_return;
2737 /* That will change things, so, we should relax again.
2738 Note that this is not required, and it may be slow. */
2739 *again = TRUE;
2742 /* Try to turn a 24 immediate, displacement or absolute address
2743 into a 8 immediate, displacement or absolute address. */
2744 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
2746 bfd_vma value = symval;
2747 value += irel->r_addend;
2749 /* See if the value will fit in 8 bits. */
2750 if ((long) value < 0x7f && (long) value > -0x80)
2752 unsigned char code;
2754 /* AM33 insns which have 24 operands are 6 bytes long and
2755 will have 0xfd as the first byte. */
2757 /* Get the first opcode. */
2758 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2760 if (code == 0xfd)
2762 /* Get the second opcode. */
2763 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2765 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2766 equivalent instructions exists. */
2767 if (code != 0x6b && code != 0x7b
2768 && code != 0x8b && code != 0x9b
2769 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2770 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2771 || (code & 0x0f) == 0x0e))
2773 /* Not safe if the high bit is on as relaxing may
2774 move the value out of high mem and thus not fit
2775 in a signed 8bit value. This is currently over
2776 conservative. */
2777 if ((value & 0x80) == 0)
2779 /* Note that we've changed the relocation contents,
2780 etc. */
2781 elf_section_data (sec)->relocs = internal_relocs;
2782 elf_section_data (sec)->this_hdr.contents = contents;
2783 symtab_hdr->contents = (unsigned char *) isymbuf;
2785 /* Fix the opcode. */
2786 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2787 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2789 /* Fix the relocation's type. */
2790 irel->r_info =
2791 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2792 R_MN10300_8);
2794 /* Delete two bytes of data. */
2795 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2796 irel->r_offset + 1, 2))
2797 goto error_return;
2799 /* That will change things, so, we should relax
2800 again. Note that this is not required, and it
2801 may be slow. */
2802 *again = TRUE;
2803 break;
2810 /* Try to turn a 32bit immediate, displacement or absolute address
2811 into a 16bit immediate, displacement or absolute address. */
2812 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
2813 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
2814 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32
2815 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2817 bfd_vma value = symval;
2819 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
2821 asection * sgot;
2823 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
2824 ->dynobj, ".got");
2826 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
2828 value = sgot->output_offset;
2830 if (h)
2831 value += h->root.got.offset;
2832 else
2833 value += (elf_local_got_offsets
2834 (abfd)[ELF32_R_SYM (irel->r_info)]);
2836 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
2837 value -= sgot->output_section->vma;
2838 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2839 value = (sgot->output_section->vma
2840 - (sec->output_section->vma
2841 + sec->output_offset
2842 + irel->r_offset));
2843 else
2844 abort ();
2847 value += irel->r_addend;
2849 /* See if the value will fit in 24 bits.
2850 We allow any 16bit match here. We prune those we can't
2851 handle below. */
2852 if ((long) value < 0x7fffff && (long) value > -0x800000)
2854 unsigned char code;
2856 /* AM33 insns which have 32bit operands are 7 bytes long and
2857 will have 0xfe as the first byte. */
2859 /* Get the first opcode. */
2860 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2862 if (code == 0xfe)
2864 /* Get the second opcode. */
2865 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2867 /* All the am33 32 -> 24 relaxing possibilities. */
2868 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2869 equivalent instructions exists. */
2870 if (code != 0x6b && code != 0x7b
2871 && code != 0x8b && code != 0x9b
2872 && (ELF32_R_TYPE (irel->r_info)
2873 != (int) R_MN10300_GOTPC32)
2874 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2875 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2876 || (code & 0x0f) == 0x0e))
2878 /* Not safe if the high bit is on as relaxing may
2879 move the value out of high mem and thus not fit
2880 in a signed 16bit value. This is currently over
2881 conservative. */
2882 if ((value & 0x8000) == 0)
2884 /* Note that we've changed the relocation contents,
2885 etc. */
2886 elf_section_data (sec)->relocs = internal_relocs;
2887 elf_section_data (sec)->this_hdr.contents = contents;
2888 symtab_hdr->contents = (unsigned char *) isymbuf;
2890 /* Fix the opcode. */
2891 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2892 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2894 /* Fix the relocation's type. */
2895 irel->r_info =
2896 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2897 (ELF32_R_TYPE (irel->r_info)
2898 == (int) R_MN10300_GOTOFF32)
2899 ? R_MN10300_GOTOFF24
2900 : (ELF32_R_TYPE (irel->r_info)
2901 == (int) R_MN10300_GOT32)
2902 ? R_MN10300_GOT24 :
2903 R_MN10300_24);
2905 /* Delete one byte of data. */
2906 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2907 irel->r_offset + 3, 1))
2908 goto error_return;
2910 /* That will change things, so, we should relax
2911 again. Note that this is not required, and it
2912 may be slow. */
2913 *again = TRUE;
2914 break;
2920 /* See if the value will fit in 16 bits.
2921 We allow any 16bit match here. We prune those we can't
2922 handle below. */
2923 if ((long) value < 0x7fff && (long) value > -0x8000)
2925 unsigned char code;
2927 /* Most insns which have 32bit operands are 6 bytes long;
2928 exceptions are pcrel insns and bit insns.
2930 We handle pcrel insns above. We don't bother trying
2931 to handle the bit insns here.
2933 The first byte of the remaining insns will be 0xfc. */
2935 /* Get the first opcode. */
2936 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2938 if (code != 0xfc)
2939 continue;
2941 /* Get the second opcode. */
2942 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2944 if ((code & 0xf0) < 0x80)
2945 switch (code & 0xf0)
2947 /* mov (d32,am),dn -> mov (d32,am),dn
2948 mov dm,(d32,am) -> mov dn,(d32,am)
2949 mov (d32,am),an -> mov (d32,am),an
2950 mov dm,(d32,am) -> mov dn,(d32,am)
2951 movbu (d32,am),dn -> movbu (d32,am),dn
2952 movbu dm,(d32,am) -> movbu dn,(d32,am)
2953 movhu (d32,am),dn -> movhu (d32,am),dn
2954 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2955 case 0x00:
2956 case 0x10:
2957 case 0x20:
2958 case 0x30:
2959 case 0x40:
2960 case 0x50:
2961 case 0x60:
2962 case 0x70:
2963 /* Not safe if the high bit is on as relaxing may
2964 move the value out of high mem and thus not fit
2965 in a signed 16bit value. */
2966 if (code == 0xcc
2967 && (value & 0x8000))
2968 continue;
2970 /* Note that we've changed the relocation contents, etc. */
2971 elf_section_data (sec)->relocs = internal_relocs;
2972 elf_section_data (sec)->this_hdr.contents = contents;
2973 symtab_hdr->contents = (unsigned char *) isymbuf;
2975 /* Fix the opcode. */
2976 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2977 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2979 /* Fix the relocation's type. */
2980 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2981 (ELF32_R_TYPE (irel->r_info)
2982 == (int) R_MN10300_GOTOFF32)
2983 ? R_MN10300_GOTOFF16
2984 : (ELF32_R_TYPE (irel->r_info)
2985 == (int) R_MN10300_GOT32)
2986 ? R_MN10300_GOT16
2987 : (ELF32_R_TYPE (irel->r_info)
2988 == (int) R_MN10300_GOTPC32)
2989 ? R_MN10300_GOTPC16 :
2990 R_MN10300_16);
2992 /* Delete two bytes of data. */
2993 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2994 irel->r_offset + 2, 2))
2995 goto error_return;
2997 /* That will change things, so, we should relax again.
2998 Note that this is not required, and it may be slow. */
2999 *again = TRUE;
3000 break;
3002 else if ((code & 0xf0) == 0x80
3003 || (code & 0xf0) == 0x90)
3004 switch (code & 0xf3)
3006 /* mov dn,(abs32) -> mov dn,(abs16)
3007 movbu dn,(abs32) -> movbu dn,(abs16)
3008 movhu dn,(abs32) -> movhu dn,(abs16) */
3009 case 0x81:
3010 case 0x82:
3011 case 0x83:
3012 /* Note that we've changed the relocation contents, etc. */
3013 elf_section_data (sec)->relocs = internal_relocs;
3014 elf_section_data (sec)->this_hdr.contents = contents;
3015 symtab_hdr->contents = (unsigned char *) isymbuf;
3017 if ((code & 0xf3) == 0x81)
3018 code = 0x01 + (code & 0x0c);
3019 else if ((code & 0xf3) == 0x82)
3020 code = 0x02 + (code & 0x0c);
3021 else if ((code & 0xf3) == 0x83)
3022 code = 0x03 + (code & 0x0c);
3023 else
3024 abort ();
3026 /* Fix the opcode. */
3027 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3029 /* Fix the relocation's type. */
3030 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3031 (ELF32_R_TYPE (irel->r_info)
3032 == (int) R_MN10300_GOTOFF32)
3033 ? R_MN10300_GOTOFF16
3034 : (ELF32_R_TYPE (irel->r_info)
3035 == (int) R_MN10300_GOT32)
3036 ? R_MN10300_GOT16
3037 : (ELF32_R_TYPE (irel->r_info)
3038 == (int) R_MN10300_GOTPC32)
3039 ? R_MN10300_GOTPC16 :
3040 R_MN10300_16);
3042 /* The opcode got shorter too, so we have to fix the
3043 addend and offset too! */
3044 irel->r_offset -= 1;
3046 /* Delete three bytes of data. */
3047 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3048 irel->r_offset + 1, 3))
3049 goto error_return;
3051 /* That will change things, so, we should relax again.
3052 Note that this is not required, and it may be slow. */
3053 *again = TRUE;
3054 break;
3056 /* mov am,(abs32) -> mov am,(abs16)
3057 mov am,(d32,sp) -> mov am,(d16,sp)
3058 mov dm,(d32,sp) -> mov dm,(d32,sp)
3059 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3060 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3061 case 0x80:
3062 case 0x90:
3063 case 0x91:
3064 case 0x92:
3065 case 0x93:
3066 /* sp-based offsets are zero-extended. */
3067 if (code >= 0x90 && code <= 0x93
3068 && (long)value < 0)
3069 continue;
3071 /* Note that we've changed the relocation contents, etc. */
3072 elf_section_data (sec)->relocs = internal_relocs;
3073 elf_section_data (sec)->this_hdr.contents = contents;
3074 symtab_hdr->contents = (unsigned char *) isymbuf;
3076 /* Fix the opcode. */
3077 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3078 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3080 /* Fix the relocation's type. */
3081 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3082 (ELF32_R_TYPE (irel->r_info)
3083 == (int) R_MN10300_GOTOFF32)
3084 ? R_MN10300_GOTOFF16
3085 : (ELF32_R_TYPE (irel->r_info)
3086 == (int) R_MN10300_GOT32)
3087 ? R_MN10300_GOT16
3088 : (ELF32_R_TYPE (irel->r_info)
3089 == (int) R_MN10300_GOTPC32)
3090 ? R_MN10300_GOTPC16 :
3091 R_MN10300_16);
3093 /* Delete two bytes of data. */
3094 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3095 irel->r_offset + 2, 2))
3096 goto error_return;
3098 /* That will change things, so, we should relax again.
3099 Note that this is not required, and it may be slow. */
3100 *again = TRUE;
3101 break;
3103 else if ((code & 0xf0) < 0xf0)
3104 switch (code & 0xfc)
3106 /* mov imm32,dn -> mov imm16,dn
3107 mov imm32,an -> mov imm16,an
3108 mov (abs32),dn -> mov (abs16),dn
3109 movbu (abs32),dn -> movbu (abs16),dn
3110 movhu (abs32),dn -> movhu (abs16),dn */
3111 case 0xcc:
3112 case 0xdc:
3113 case 0xa4:
3114 case 0xa8:
3115 case 0xac:
3116 /* Not safe if the high bit is on as relaxing may
3117 move the value out of high mem and thus not fit
3118 in a signed 16bit value. */
3119 if (code == 0xcc
3120 && (value & 0x8000))
3121 continue;
3123 /* mov imm16, an zero-extends the immediate. */
3124 if (code == 0xdc
3125 && (long)value < 0)
3126 continue;
3128 /* Note that we've changed the relocation contents, etc. */
3129 elf_section_data (sec)->relocs = internal_relocs;
3130 elf_section_data (sec)->this_hdr.contents = contents;
3131 symtab_hdr->contents = (unsigned char *) isymbuf;
3133 if ((code & 0xfc) == 0xcc)
3134 code = 0x2c + (code & 0x03);
3135 else if ((code & 0xfc) == 0xdc)
3136 code = 0x24 + (code & 0x03);
3137 else if ((code & 0xfc) == 0xa4)
3138 code = 0x30 + (code & 0x03);
3139 else if ((code & 0xfc) == 0xa8)
3140 code = 0x34 + (code & 0x03);
3141 else if ((code & 0xfc) == 0xac)
3142 code = 0x38 + (code & 0x03);
3143 else
3144 abort ();
3146 /* Fix the opcode. */
3147 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3149 /* Fix the relocation's type. */
3150 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3151 (ELF32_R_TYPE (irel->r_info)
3152 == (int) R_MN10300_GOTOFF32)
3153 ? R_MN10300_GOTOFF16
3154 : (ELF32_R_TYPE (irel->r_info)
3155 == (int) R_MN10300_GOT32)
3156 ? R_MN10300_GOT16
3157 : (ELF32_R_TYPE (irel->r_info)
3158 == (int) R_MN10300_GOTPC32)
3159 ? R_MN10300_GOTPC16 :
3160 R_MN10300_16);
3162 /* The opcode got shorter too, so we have to fix the
3163 addend and offset too! */
3164 irel->r_offset -= 1;
3166 /* Delete three bytes of data. */
3167 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3168 irel->r_offset + 1, 3))
3169 goto error_return;
3171 /* That will change things, so, we should relax again.
3172 Note that this is not required, and it may be slow. */
3173 *again = TRUE;
3174 break;
3176 /* mov (abs32),an -> mov (abs16),an
3177 mov (d32,sp),an -> mov (d16,sp),an
3178 mov (d32,sp),dn -> mov (d16,sp),dn
3179 movbu (d32,sp),dn -> movbu (d16,sp),dn
3180 movhu (d32,sp),dn -> movhu (d16,sp),dn
3181 add imm32,dn -> add imm16,dn
3182 cmp imm32,dn -> cmp imm16,dn
3183 add imm32,an -> add imm16,an
3184 cmp imm32,an -> cmp imm16,an
3185 and imm32,dn -> and imm16,dn
3186 or imm32,dn -> or imm16,dn
3187 xor imm32,dn -> xor imm16,dn
3188 btst imm32,dn -> btst imm16,dn */
3190 case 0xa0:
3191 case 0xb0:
3192 case 0xb1:
3193 case 0xb2:
3194 case 0xb3:
3195 case 0xc0:
3196 case 0xc8:
3198 case 0xd0:
3199 case 0xd8:
3200 case 0xe0:
3201 case 0xe1:
3202 case 0xe2:
3203 case 0xe3:
3204 /* cmp imm16, an zero-extends the immediate. */
3205 if (code == 0xdc
3206 && (long)value < 0)
3207 continue;
3209 /* So do sp-based offsets. */
3210 if (code >= 0xb0 && code <= 0xb3
3211 && (long)value < 0)
3212 continue;
3214 /* Note that we've changed the relocation contents, etc. */
3215 elf_section_data (sec)->relocs = internal_relocs;
3216 elf_section_data (sec)->this_hdr.contents = contents;
3217 symtab_hdr->contents = (unsigned char *) isymbuf;
3219 /* Fix the opcode. */
3220 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3221 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3223 /* Fix the relocation's type. */
3224 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3225 (ELF32_R_TYPE (irel->r_info)
3226 == (int) R_MN10300_GOTOFF32)
3227 ? R_MN10300_GOTOFF16
3228 : (ELF32_R_TYPE (irel->r_info)
3229 == (int) R_MN10300_GOT32)
3230 ? R_MN10300_GOT16
3231 : (ELF32_R_TYPE (irel->r_info)
3232 == (int) R_MN10300_GOTPC32)
3233 ? R_MN10300_GOTPC16 :
3234 R_MN10300_16);
3236 /* Delete two bytes of data. */
3237 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3238 irel->r_offset + 2, 2))
3239 goto error_return;
3241 /* That will change things, so, we should relax again.
3242 Note that this is not required, and it may be slow. */
3243 *again = TRUE;
3244 break;
3246 else if (code == 0xfe)
3248 /* add imm32,sp -> add imm16,sp */
3250 /* Note that we've changed the relocation contents, etc. */
3251 elf_section_data (sec)->relocs = internal_relocs;
3252 elf_section_data (sec)->this_hdr.contents = contents;
3253 symtab_hdr->contents = (unsigned char *) isymbuf;
3255 /* Fix the opcode. */
3256 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3257 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3259 /* Fix the relocation's type. */
3260 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3261 (ELF32_R_TYPE (irel->r_info)
3262 == (int) R_MN10300_GOT32)
3263 ? R_MN10300_GOT16
3264 : (ELF32_R_TYPE (irel->r_info)
3265 == (int) R_MN10300_GOTOFF32)
3266 ? R_MN10300_GOTOFF16
3267 : (ELF32_R_TYPE (irel->r_info)
3268 == (int) R_MN10300_GOTPC32)
3269 ? R_MN10300_GOTPC16 :
3270 R_MN10300_16);
3272 /* Delete two bytes of data. */
3273 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3274 irel->r_offset + 2, 2))
3275 goto error_return;
3277 /* That will change things, so, we should relax again.
3278 Note that this is not required, and it may be slow. */
3279 *again = TRUE;
3280 break;
3286 if (isymbuf != NULL
3287 && symtab_hdr->contents != (unsigned char *) isymbuf)
3289 if (! link_info->keep_memory)
3290 free (isymbuf);
3291 else
3293 /* Cache the symbols for elf_link_input_bfd. */
3294 symtab_hdr->contents = (unsigned char *) isymbuf;
3298 if (contents != NULL
3299 && elf_section_data (sec)->this_hdr.contents != contents)
3301 if (! link_info->keep_memory)
3302 free (contents);
3303 else
3305 /* Cache the section contents for elf_link_input_bfd. */
3306 elf_section_data (sec)->this_hdr.contents = contents;
3310 if (internal_relocs != NULL
3311 && elf_section_data (sec)->relocs != internal_relocs)
3312 free (internal_relocs);
3314 return TRUE;
3316 error_return:
3317 if (isymbuf != NULL
3318 && symtab_hdr->contents != (unsigned char *) isymbuf)
3319 free (isymbuf);
3320 if (contents != NULL
3321 && elf_section_data (section)->this_hdr.contents != contents)
3322 free (contents);
3323 if (internal_relocs != NULL
3324 && elf_section_data (section)->relocs != internal_relocs)
3325 free (internal_relocs);
3327 return FALSE;
3330 /* Compute the stack size and movm arguments for the function
3331 referred to by HASH at address ADDR in section with
3332 contents CONTENTS, store the information in the hash table. */
3333 static void
3334 compute_function_info (abfd, hash, addr, contents)
3335 bfd *abfd;
3336 struct elf32_mn10300_link_hash_entry *hash;
3337 bfd_vma addr;
3338 unsigned char *contents;
3340 unsigned char byte1, byte2;
3341 /* We only care about a very small subset of the possible prologue
3342 sequences here. Basically we look for:
3344 movm [d2,d3,a2,a3],sp (optional)
3345 add <size>,sp (optional, and only for sizes which fit in an unsigned
3346 8 bit number)
3348 If we find anything else, we quit. */
3350 /* Look for movm [regs],sp */
3351 byte1 = bfd_get_8 (abfd, contents + addr);
3352 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3354 if (byte1 == 0xcf)
3356 hash->movm_args = byte2;
3357 addr += 2;
3358 byte1 = bfd_get_8 (abfd, contents + addr);
3359 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3362 /* Now figure out how much stack space will be allocated by the movm
3363 instruction. We need this kept separate from the function's normal
3364 stack space. */
3365 if (hash->movm_args)
3367 /* Space for d2. */
3368 if (hash->movm_args & 0x80)
3369 hash->movm_stack_size += 4;
3371 /* Space for d3. */
3372 if (hash->movm_args & 0x40)
3373 hash->movm_stack_size += 4;
3375 /* Space for a2. */
3376 if (hash->movm_args & 0x20)
3377 hash->movm_stack_size += 4;
3379 /* Space for a3. */
3380 if (hash->movm_args & 0x10)
3381 hash->movm_stack_size += 4;
3383 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
3384 if (hash->movm_args & 0x08)
3385 hash->movm_stack_size += 8 * 4;
3387 if (bfd_get_mach (abfd) == bfd_mach_am33
3388 || bfd_get_mach (abfd) == bfd_mach_am33_2)
3390 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
3391 if (hash->movm_args & 0x1)
3392 hash->movm_stack_size += 6 * 4;
3394 /* exreg1 space. e4, e5, e6, e7 */
3395 if (hash->movm_args & 0x2)
3396 hash->movm_stack_size += 4 * 4;
3398 /* exreg0 space. e2, e3 */
3399 if (hash->movm_args & 0x4)
3400 hash->movm_stack_size += 2 * 4;
3404 /* Now look for the two stack adjustment variants. */
3405 if (byte1 == 0xf8 && byte2 == 0xfe)
3407 int temp = bfd_get_8 (abfd, contents + addr + 2);
3408 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
3410 hash->stack_size = -temp;
3412 else if (byte1 == 0xfa && byte2 == 0xfe)
3414 int temp = bfd_get_16 (abfd, contents + addr + 2);
3415 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
3416 temp = -temp;
3418 if (temp < 255)
3419 hash->stack_size = temp;
3422 /* If the total stack to be allocated by the call instruction is more
3423 than 255 bytes, then we can't remove the stack adjustment by using
3424 "call" (we might still be able to remove the "movm" instruction. */
3425 if (hash->stack_size + hash->movm_stack_size > 255)
3426 hash->stack_size = 0;
3428 return;
3431 /* Delete some bytes from a section while relaxing. */
3433 static bfd_boolean
3434 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
3435 bfd *abfd;
3436 asection *sec;
3437 bfd_vma addr;
3438 int count;
3440 Elf_Internal_Shdr *symtab_hdr;
3441 unsigned int sec_shndx;
3442 bfd_byte *contents;
3443 Elf_Internal_Rela *irel, *irelend;
3444 Elf_Internal_Rela *irelalign;
3445 bfd_vma toaddr;
3446 Elf_Internal_Sym *isym, *isymend;
3447 struct elf_link_hash_entry **sym_hashes;
3448 struct elf_link_hash_entry **end_hashes;
3449 unsigned int symcount;
3451 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3453 contents = elf_section_data (sec)->this_hdr.contents;
3455 /* The deletion must stop at the next ALIGN reloc for an aligment
3456 power larger than the number of bytes we are deleting. */
3458 irelalign = NULL;
3459 toaddr = sec->size;
3461 irel = elf_section_data (sec)->relocs;
3462 irelend = irel + sec->reloc_count;
3464 /* Actually delete the bytes. */
3465 memmove (contents + addr, contents + addr + count,
3466 (size_t) (toaddr - addr - count));
3467 sec->size -= count;
3469 /* Adjust all the relocs. */
3470 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3472 /* Get the new reloc address. */
3473 if ((irel->r_offset > addr
3474 && irel->r_offset < toaddr))
3475 irel->r_offset -= count;
3478 /* Adjust the local symbols defined in this section. */
3479 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3480 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3481 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3483 if (isym->st_shndx == sec_shndx
3484 && isym->st_value > addr
3485 && isym->st_value < toaddr)
3486 isym->st_value -= count;
3489 /* Now adjust the global symbols defined in this section. */
3490 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3491 - symtab_hdr->sh_info);
3492 sym_hashes = elf_sym_hashes (abfd);
3493 end_hashes = sym_hashes + symcount;
3494 for (; sym_hashes < end_hashes; sym_hashes++)
3496 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3497 if ((sym_hash->root.type == bfd_link_hash_defined
3498 || sym_hash->root.type == bfd_link_hash_defweak)
3499 && sym_hash->root.u.def.section == sec
3500 && sym_hash->root.u.def.value > addr
3501 && sym_hash->root.u.def.value < toaddr)
3503 sym_hash->root.u.def.value -= count;
3507 return TRUE;
3510 /* Return TRUE if a symbol exists at the given address, else return
3511 FALSE. */
3512 static bfd_boolean
3513 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
3514 bfd *abfd;
3515 asection *sec;
3516 Elf_Internal_Sym *isym;
3517 bfd_vma addr;
3519 Elf_Internal_Shdr *symtab_hdr;
3520 unsigned int sec_shndx;
3521 Elf_Internal_Sym *isymend;
3522 struct elf_link_hash_entry **sym_hashes;
3523 struct elf_link_hash_entry **end_hashes;
3524 unsigned int symcount;
3526 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3528 /* Examine all the symbols. */
3529 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3530 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3532 if (isym->st_shndx == sec_shndx
3533 && isym->st_value == addr)
3534 return TRUE;
3537 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3538 - symtab_hdr->sh_info);
3539 sym_hashes = elf_sym_hashes (abfd);
3540 end_hashes = sym_hashes + symcount;
3541 for (; sym_hashes < end_hashes; sym_hashes++)
3543 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3544 if ((sym_hash->root.type == bfd_link_hash_defined
3545 || sym_hash->root.type == bfd_link_hash_defweak)
3546 && sym_hash->root.u.def.section == sec
3547 && sym_hash->root.u.def.value == addr)
3548 return TRUE;
3551 return FALSE;
3554 /* This is a version of bfd_generic_get_relocated_section_contents
3555 which uses mn10300_elf_relocate_section. */
3557 static bfd_byte *
3558 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3559 data, relocatable, symbols)
3560 bfd *output_bfd;
3561 struct bfd_link_info *link_info;
3562 struct bfd_link_order *link_order;
3563 bfd_byte *data;
3564 bfd_boolean relocatable;
3565 asymbol **symbols;
3567 Elf_Internal_Shdr *symtab_hdr;
3568 asection *input_section = link_order->u.indirect.section;
3569 bfd *input_bfd = input_section->owner;
3570 asection **sections = NULL;
3571 Elf_Internal_Rela *internal_relocs = NULL;
3572 Elf_Internal_Sym *isymbuf = NULL;
3574 /* We only need to handle the case of relaxing, or of having a
3575 particular set of section contents, specially. */
3576 if (relocatable
3577 || elf_section_data (input_section)->this_hdr.contents == NULL)
3578 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3579 link_order, data,
3580 relocatable,
3581 symbols);
3583 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3585 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3586 (size_t) input_section->size);
3588 if ((input_section->flags & SEC_RELOC) != 0
3589 && input_section->reloc_count > 0)
3591 asection **secpp;
3592 Elf_Internal_Sym *isym, *isymend;
3593 bfd_size_type amt;
3595 internal_relocs = (_bfd_elf_link_read_relocs
3596 (input_bfd, input_section, (PTR) NULL,
3597 (Elf_Internal_Rela *) NULL, FALSE));
3598 if (internal_relocs == NULL)
3599 goto error_return;
3601 if (symtab_hdr->sh_info != 0)
3603 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3604 if (isymbuf == NULL)
3605 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3606 symtab_hdr->sh_info, 0,
3607 NULL, NULL, NULL);
3608 if (isymbuf == NULL)
3609 goto error_return;
3612 amt = symtab_hdr->sh_info;
3613 amt *= sizeof (asection *);
3614 sections = (asection **) bfd_malloc (amt);
3615 if (sections == NULL && amt != 0)
3616 goto error_return;
3618 isymend = isymbuf + symtab_hdr->sh_info;
3619 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3621 asection *isec;
3623 if (isym->st_shndx == SHN_UNDEF)
3624 isec = bfd_und_section_ptr;
3625 else if (isym->st_shndx == SHN_ABS)
3626 isec = bfd_abs_section_ptr;
3627 else if (isym->st_shndx == SHN_COMMON)
3628 isec = bfd_com_section_ptr;
3629 else
3630 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3632 *secpp = isec;
3635 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3636 input_section, data, internal_relocs,
3637 isymbuf, sections))
3638 goto error_return;
3640 if (sections != NULL)
3641 free (sections);
3642 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3643 free (isymbuf);
3644 if (internal_relocs != elf_section_data (input_section)->relocs)
3645 free (internal_relocs);
3648 return data;
3650 error_return:
3651 if (sections != NULL)
3652 free (sections);
3653 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3654 free (isymbuf);
3655 if (internal_relocs != NULL
3656 && internal_relocs != elf_section_data (input_section)->relocs)
3657 free (internal_relocs);
3658 return NULL;
3661 /* Assorted hash table functions. */
3663 /* Initialize an entry in the link hash table. */
3665 /* Create an entry in an MN10300 ELF linker hash table. */
3667 static struct bfd_hash_entry *
3668 elf32_mn10300_link_hash_newfunc (entry, table, string)
3669 struct bfd_hash_entry *entry;
3670 struct bfd_hash_table *table;
3671 const char *string;
3673 struct elf32_mn10300_link_hash_entry *ret =
3674 (struct elf32_mn10300_link_hash_entry *) entry;
3676 /* Allocate the structure if it has not already been allocated by a
3677 subclass. */
3678 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3679 ret = ((struct elf32_mn10300_link_hash_entry *)
3680 bfd_hash_allocate (table,
3681 sizeof (struct elf32_mn10300_link_hash_entry)));
3682 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3683 return (struct bfd_hash_entry *) ret;
3685 /* Call the allocation method of the superclass. */
3686 ret = ((struct elf32_mn10300_link_hash_entry *)
3687 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3688 table, string));
3689 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
3691 ret->direct_calls = 0;
3692 ret->stack_size = 0;
3693 ret->movm_args = 0;
3694 ret->movm_stack_size = 0;
3695 ret->flags = 0;
3698 return (struct bfd_hash_entry *) ret;
3701 /* Create an mn10300 ELF linker hash table. */
3703 static struct bfd_link_hash_table *
3704 elf32_mn10300_link_hash_table_create (abfd)
3705 bfd *abfd;
3707 struct elf32_mn10300_link_hash_table *ret;
3708 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
3710 ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3711 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
3712 return NULL;
3714 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3715 elf32_mn10300_link_hash_newfunc))
3717 free (ret);
3718 return NULL;
3721 ret->flags = 0;
3722 amt = sizeof (struct elf_link_hash_table);
3723 ret->static_hash_table
3724 = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3725 if (ret->static_hash_table == NULL)
3727 free (ret);
3728 return NULL;
3731 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3732 elf32_mn10300_link_hash_newfunc))
3734 free (ret->static_hash_table);
3735 free (ret);
3736 return NULL;
3738 return &ret->root.root;
3741 /* Free an mn10300 ELF linker hash table. */
3743 static void
3744 elf32_mn10300_link_hash_table_free (hash)
3745 struct bfd_link_hash_table *hash;
3747 struct elf32_mn10300_link_hash_table *ret
3748 = (struct elf32_mn10300_link_hash_table *) hash;
3750 _bfd_generic_link_hash_table_free
3751 ((struct bfd_link_hash_table *) ret->static_hash_table);
3752 _bfd_generic_link_hash_table_free
3753 ((struct bfd_link_hash_table *) ret);
3756 static unsigned long
3757 elf_mn10300_mach (flags)
3758 flagword flags;
3760 switch (flags & EF_MN10300_MACH)
3762 case E_MN10300_MACH_MN10300:
3763 default:
3764 return bfd_mach_mn10300;
3766 case E_MN10300_MACH_AM33:
3767 return bfd_mach_am33;
3769 case E_MN10300_MACH_AM33_2:
3770 return bfd_mach_am33_2;
3774 /* The final processing done just before writing out a MN10300 ELF object
3775 file. This gets the MN10300 architecture right based on the machine
3776 number. */
3778 void
3779 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3780 bfd *abfd;
3781 bfd_boolean linker ATTRIBUTE_UNUSED;
3783 unsigned long val;
3785 switch (bfd_get_mach (abfd))
3787 default:
3788 case bfd_mach_mn10300:
3789 val = E_MN10300_MACH_MN10300;
3790 break;
3792 case bfd_mach_am33:
3793 val = E_MN10300_MACH_AM33;
3794 break;
3796 case bfd_mach_am33_2:
3797 val = E_MN10300_MACH_AM33_2;
3798 break;
3801 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3802 elf_elfheader (abfd)->e_flags |= val;
3805 bfd_boolean
3806 _bfd_mn10300_elf_object_p (abfd)
3807 bfd *abfd;
3809 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3810 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3811 return TRUE;
3814 /* Merge backend specific data from an object file to the output
3815 object file when linking. */
3817 bfd_boolean
3818 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3819 bfd *ibfd;
3820 bfd *obfd;
3822 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3823 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3824 return TRUE;
3826 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3827 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3829 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3830 bfd_get_mach (ibfd)))
3831 return FALSE;
3834 return TRUE;
3837 #define PLT0_ENTRY_SIZE 15
3838 #define PLT_ENTRY_SIZE 20
3839 #define PIC_PLT_ENTRY_SIZE 24
3841 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
3843 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
3844 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
3845 0xf0, 0xf4, /* jmp (a0) */
3848 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
3850 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
3851 0xf0, 0xf4, /* jmp (a0) */
3852 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3853 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
3856 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
3858 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
3859 0xf0, 0xf4, /* jmp (a0) */
3860 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3861 0xf8, 0x22, 8, /* mov (8,a2),a0 */
3862 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
3863 0xf0, 0xf4, /* jmp (a0) */
3866 /* Return size of the first PLT entry. */
3867 #define elf_mn10300_sizeof_plt0(info) \
3868 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
3870 /* Return size of a PLT entry. */
3871 #define elf_mn10300_sizeof_plt(info) \
3872 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
3874 /* Return offset of the PLT0 address in an absolute PLT entry. */
3875 #define elf_mn10300_plt_plt0_offset(info) 16
3877 /* Return offset of the linker in PLT0 entry. */
3878 #define elf_mn10300_plt0_linker_offset(info) 2
3880 /* Return offset of the GOT id in PLT0 entry. */
3881 #define elf_mn10300_plt0_gotid_offset(info) 9
3883 /* Return offset of the temporary in PLT entry */
3884 #define elf_mn10300_plt_temp_offset(info) 8
3886 /* Return offset of the symbol in PLT entry. */
3887 #define elf_mn10300_plt_symbol_offset(info) 2
3889 /* Return offset of the relocation in PLT entry. */
3890 #define elf_mn10300_plt_reloc_offset(info) 11
3892 /* The name of the dynamic interpreter. This is put in the .interp
3893 section. */
3895 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
3897 /* Create dynamic sections when linking against a dynamic object. */
3899 static bfd_boolean
3900 _bfd_mn10300_elf_create_dynamic_sections (abfd, info)
3901 bfd *abfd;
3902 struct bfd_link_info *info;
3904 flagword flags;
3905 asection * s;
3906 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
3907 int ptralign = 0;
3909 switch (bed->s->arch_size)
3911 case 32:
3912 ptralign = 2;
3913 break;
3915 case 64:
3916 ptralign = 3;
3917 break;
3919 default:
3920 bfd_set_error (bfd_error_bad_value);
3921 return FALSE;
3924 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3925 .rel[a].bss sections. */
3927 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3928 | SEC_LINKER_CREATED);
3930 s = bfd_make_section_with_flags (abfd,
3931 (bed->default_use_rela_p
3932 ? ".rela.plt" : ".rel.plt"),
3933 flags | SEC_READONLY);
3934 if (s == NULL
3935 || ! bfd_set_section_alignment (abfd, s, ptralign))
3936 return FALSE;
3938 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
3939 return FALSE;
3942 const char * secname;
3943 char * relname;
3944 flagword secflags;
3945 asection * sec;
3947 for (sec = abfd->sections; sec; sec = sec->next)
3949 secflags = bfd_get_section_flags (abfd, sec);
3950 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3951 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3952 continue;
3954 secname = bfd_get_section_name (abfd, sec);
3955 relname = (char *) bfd_malloc (strlen (secname) + 6);
3956 strcpy (relname, ".rela");
3957 strcat (relname, secname);
3959 s = bfd_make_section_with_flags (abfd, relname,
3960 flags | SEC_READONLY);
3961 if (s == NULL
3962 || ! bfd_set_section_alignment (abfd, s, ptralign))
3963 return FALSE;
3967 if (bed->want_dynbss)
3969 /* The .dynbss section is a place to put symbols which are defined
3970 by dynamic objects, are referenced by regular objects, and are
3971 not functions. We must allocate space for them in the process
3972 image and use a R_*_COPY reloc to tell the dynamic linker to
3973 initialize them at run time. The linker script puts the .dynbss
3974 section into the .bss section of the final image. */
3975 s = bfd_make_section_with_flags (abfd, ".dynbss",
3976 SEC_ALLOC | SEC_LINKER_CREATED);
3977 if (s == NULL)
3978 return FALSE;
3980 /* The .rel[a].bss section holds copy relocs. This section is not
3981 normally needed. We need to create it here, though, so that the
3982 linker will map it to an output section. We can't just create it
3983 only if we need it, because we will not know whether we need it
3984 until we have seen all the input files, and the first time the
3985 main linker code calls BFD after examining all the input files
3986 (size_dynamic_sections) the input sections have already been
3987 mapped to the output sections. If the section turns out not to
3988 be needed, we can discard it later. We will never need this
3989 section when generating a shared object, since they do not use
3990 copy relocs. */
3991 if (! info->shared)
3993 s = bfd_make_section_with_flags (abfd,
3994 (bed->default_use_rela_p
3995 ? ".rela.bss" : ".rel.bss"),
3996 flags | SEC_READONLY);
3997 if (s == NULL
3998 || ! bfd_set_section_alignment (abfd, s, ptralign))
3999 return FALSE;
4003 return TRUE;
4006 /* Adjust a symbol defined by a dynamic object and referenced by a
4007 regular object. The current definition is in some section of the
4008 dynamic object, but we're not including those sections. We have to
4009 change the definition to something the rest of the link can
4010 understand. */
4012 static bfd_boolean
4013 _bfd_mn10300_elf_adjust_dynamic_symbol (info, h)
4014 struct bfd_link_info * info;
4015 struct elf_link_hash_entry * h;
4017 bfd * dynobj;
4018 asection * s;
4019 unsigned int power_of_two;
4021 dynobj = elf_hash_table (info)->dynobj;
4023 /* Make sure we know what is going on here. */
4024 BFD_ASSERT (dynobj != NULL
4025 && (h->needs_plt
4026 || h->u.weakdef != NULL
4027 || (h->def_dynamic
4028 && h->ref_regular
4029 && !h->def_regular)));
4031 /* If this is a function, put it in the procedure linkage table. We
4032 will fill in the contents of the procedure linkage table later,
4033 when we know the address of the .got section. */
4034 if (h->type == STT_FUNC
4035 || h->needs_plt)
4037 if (! info->shared
4038 && !h->def_dynamic
4039 && !h->ref_dynamic)
4041 /* This case can occur if we saw a PLT reloc in an input
4042 file, but the symbol was never referred to by a dynamic
4043 object. In such a case, we don't actually need to build
4044 a procedure linkage table, and we can just do a REL32
4045 reloc instead. */
4046 BFD_ASSERT (h->needs_plt);
4047 return TRUE;
4050 /* Make sure this symbol is output as a dynamic symbol. */
4051 if (h->dynindx == -1)
4053 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4054 return FALSE;
4057 s = bfd_get_section_by_name (dynobj, ".plt");
4058 BFD_ASSERT (s != NULL);
4060 /* If this is the first .plt entry, make room for the special
4061 first entry. */
4062 if (s->size == 0)
4063 s->size += elf_mn10300_sizeof_plt0 (info);
4065 /* If this symbol is not defined in a regular file, and we are
4066 not generating a shared library, then set the symbol to this
4067 location in the .plt. This is required to make function
4068 pointers compare as equal between the normal executable and
4069 the shared library. */
4070 if (! info->shared
4071 && !h->def_regular)
4073 h->root.u.def.section = s;
4074 h->root.u.def.value = s->size;
4077 h->plt.offset = s->size;
4079 /* Make room for this entry. */
4080 s->size += elf_mn10300_sizeof_plt (info);
4082 /* We also need to make an entry in the .got.plt section, which
4083 will be placed in the .got section by the linker script. */
4085 s = bfd_get_section_by_name (dynobj, ".got.plt");
4086 BFD_ASSERT (s != NULL);
4087 s->size += 4;
4089 /* We also need to make an entry in the .rela.plt section. */
4091 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4092 BFD_ASSERT (s != NULL);
4093 s->size += sizeof (Elf32_External_Rela);
4095 return TRUE;
4098 /* If this is a weak symbol, and there is a real definition, the
4099 processor independent code will have arranged for us to see the
4100 real definition first, and we can just use the same value. */
4101 if (h->u.weakdef != NULL)
4103 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4104 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4105 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4106 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4107 return TRUE;
4110 /* This is a reference to a symbol defined by a dynamic object which
4111 is not a function. */
4113 /* If we are creating a shared library, we must presume that the
4114 only references to the symbol are via the global offset table.
4115 For such cases we need not do anything here; the relocations will
4116 be handled correctly by relocate_section. */
4117 if (info->shared)
4118 return TRUE;
4120 /* If there are no references to this symbol that do not use the
4121 GOT, we don't need to generate a copy reloc. */
4122 if (!h->non_got_ref)
4123 return TRUE;
4125 /* We must allocate the symbol in our .dynbss section, which will
4126 become part of the .bss section of the executable. There will be
4127 an entry for this symbol in the .dynsym section. The dynamic
4128 object will contain position independent code, so all references
4129 from the dynamic object to this symbol will go through the global
4130 offset table. The dynamic linker will use the .dynsym entry to
4131 determine the address it must put in the global offset table, so
4132 both the dynamic object and the regular object will refer to the
4133 same memory location for the variable. */
4135 s = bfd_get_section_by_name (dynobj, ".dynbss");
4136 BFD_ASSERT (s != NULL);
4138 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4139 copy the initial value out of the dynamic object and into the
4140 runtime process image. We need to remember the offset into the
4141 .rela.bss section we are going to use. */
4142 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4144 asection * srel;
4146 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4147 BFD_ASSERT (srel != NULL);
4148 srel->size += sizeof (Elf32_External_Rela);
4149 h->needs_copy = 1;
4152 /* We need to figure out the alignment required for this symbol. I
4153 have no idea how ELF linkers handle this. */
4154 power_of_two = bfd_log2 (h->size);
4155 if (power_of_two > 3)
4156 power_of_two = 3;
4158 /* Apply the required alignment. */
4159 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4160 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4162 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4163 return FALSE;
4166 /* Define the symbol as being at this point in the section. */
4167 h->root.u.def.section = s;
4168 h->root.u.def.value = s->size;
4170 /* Increment the section size to make room for the symbol. */
4171 s->size += h->size;
4173 return TRUE;
4176 /* Set the sizes of the dynamic sections. */
4178 static bfd_boolean
4179 _bfd_mn10300_elf_size_dynamic_sections (output_bfd, info)
4180 bfd * output_bfd;
4181 struct bfd_link_info * info;
4183 bfd * dynobj;
4184 asection * s;
4185 bfd_boolean plt;
4186 bfd_boolean relocs;
4187 bfd_boolean reltext;
4189 dynobj = elf_hash_table (info)->dynobj;
4190 BFD_ASSERT (dynobj != NULL);
4192 if (elf_hash_table (info)->dynamic_sections_created)
4194 /* Set the contents of the .interp section to the interpreter. */
4195 if (info->executable)
4197 s = bfd_get_section_by_name (dynobj, ".interp");
4198 BFD_ASSERT (s != NULL);
4199 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4200 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4203 else
4205 /* We may have created entries in the .rela.got section.
4206 However, if we are not creating the dynamic sections, we will
4207 not actually use these entries. Reset the size of .rela.got,
4208 which will cause it to get stripped from the output file
4209 below. */
4210 s = bfd_get_section_by_name (dynobj, ".rela.got");
4211 if (s != NULL)
4212 s->size = 0;
4215 /* The check_relocs and adjust_dynamic_symbol entry points have
4216 determined the sizes of the various dynamic sections. Allocate
4217 memory for them. */
4218 plt = FALSE;
4219 relocs = FALSE;
4220 reltext = FALSE;
4221 for (s = dynobj->sections; s != NULL; s = s->next)
4223 const char * name;
4225 if ((s->flags & SEC_LINKER_CREATED) == 0)
4226 continue;
4228 /* It's OK to base decisions on the section name, because none
4229 of the dynobj section names depend upon the input files. */
4230 name = bfd_get_section_name (dynobj, s);
4232 if (strcmp (name, ".plt") == 0)
4234 /* Remember whether there is a PLT. */
4235 plt = s->size != 0;
4237 else if (strncmp (name, ".rela", 5) == 0)
4239 if (s->size != 0)
4241 asection * target;
4243 /* Remember whether there are any reloc sections other
4244 than .rela.plt. */
4245 if (strcmp (name, ".rela.plt") != 0)
4247 const char * outname;
4249 relocs = TRUE;
4251 /* If this relocation section applies to a read only
4252 section, then we probably need a DT_TEXTREL
4253 entry. The entries in the .rela.plt section
4254 really apply to the .got section, which we
4255 created ourselves and so know is not readonly. */
4256 outname = bfd_get_section_name (output_bfd,
4257 s->output_section);
4258 target = bfd_get_section_by_name (output_bfd, outname + 5);
4259 if (target != NULL
4260 && (target->flags & SEC_READONLY) != 0
4261 && (target->flags & SEC_ALLOC) != 0)
4262 reltext = TRUE;
4265 /* We use the reloc_count field as a counter if we need
4266 to copy relocs into the output file. */
4267 s->reloc_count = 0;
4270 else if (strncmp (name, ".got", 4) != 0
4271 && strcmp (name, ".dynbss") != 0)
4272 /* It's not one of our sections, so don't allocate space. */
4273 continue;
4275 if (s->size == 0)
4277 /* If we don't need this section, strip it from the
4278 output file. This is mostly to handle .rela.bss and
4279 .rela.plt. We must create both sections in
4280 create_dynamic_sections, because they must be created
4281 before the linker maps input sections to output
4282 sections. The linker does that before
4283 adjust_dynamic_symbol is called, and it is that
4284 function which decides whether anything needs to go
4285 into these sections. */
4286 s->flags |= SEC_EXCLUDE;
4287 continue;
4290 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4291 continue;
4293 /* Allocate memory for the section contents. We use bfd_zalloc
4294 here in case unused entries are not reclaimed before the
4295 section's contents are written out. This should not happen,
4296 but this way if it does, we get a R_MN10300_NONE reloc
4297 instead of garbage. */
4298 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4299 if (s->contents == NULL)
4300 return FALSE;
4303 if (elf_hash_table (info)->dynamic_sections_created)
4305 /* Add some entries to the .dynamic section. We fill in the
4306 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4307 but we must add the entries now so that we get the correct
4308 size for the .dynamic section. The DT_DEBUG entry is filled
4309 in by the dynamic linker and used by the debugger. */
4310 if (! info->shared)
4312 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4313 return FALSE;
4316 if (plt)
4318 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4319 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4320 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4321 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4322 return FALSE;
4325 if (relocs)
4327 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4328 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4329 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4330 sizeof (Elf32_External_Rela)))
4331 return FALSE;
4334 if (reltext)
4336 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4337 return FALSE;
4341 return TRUE;
4344 /* Finish up dynamic symbol handling. We set the contents of various
4345 dynamic sections here. */
4347 static bfd_boolean
4348 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4349 bfd * output_bfd;
4350 struct bfd_link_info * info;
4351 struct elf_link_hash_entry * h;
4352 Elf_Internal_Sym * sym;
4354 bfd * dynobj;
4356 dynobj = elf_hash_table (info)->dynobj;
4358 if (h->plt.offset != (bfd_vma) -1)
4360 asection * splt;
4361 asection * sgot;
4362 asection * srel;
4363 bfd_vma plt_index;
4364 bfd_vma got_offset;
4365 Elf_Internal_Rela rel;
4367 /* This symbol has an entry in the procedure linkage table. Set
4368 it up. */
4370 BFD_ASSERT (h->dynindx != -1);
4372 splt = bfd_get_section_by_name (dynobj, ".plt");
4373 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4374 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4375 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4377 /* Get the index in the procedure linkage table which
4378 corresponds to this symbol. This is the index of this symbol
4379 in all the symbols for which we are making plt entries. The
4380 first entry in the procedure linkage table is reserved. */
4381 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4382 / elf_mn10300_sizeof_plt (info));
4384 /* Get the offset into the .got table of the entry that
4385 corresponds to this function. Each .got entry is 4 bytes.
4386 The first three are reserved. */
4387 got_offset = (plt_index + 3) * 4;
4389 /* Fill in the entry in the procedure linkage table. */
4390 if (! info->shared)
4392 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4393 elf_mn10300_sizeof_plt (info));
4394 bfd_put_32 (output_bfd,
4395 (sgot->output_section->vma
4396 + sgot->output_offset
4397 + got_offset),
4398 (splt->contents + h->plt.offset
4399 + elf_mn10300_plt_symbol_offset (info)));
4401 bfd_put_32 (output_bfd,
4402 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4403 (splt->contents + h->plt.offset
4404 + elf_mn10300_plt_plt0_offset (info)));
4406 else
4408 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4409 elf_mn10300_sizeof_plt (info));
4411 bfd_put_32 (output_bfd, got_offset,
4412 (splt->contents + h->plt.offset
4413 + elf_mn10300_plt_symbol_offset (info)));
4416 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4417 (splt->contents + h->plt.offset
4418 + elf_mn10300_plt_reloc_offset (info)));
4420 /* Fill in the entry in the global offset table. */
4421 bfd_put_32 (output_bfd,
4422 (splt->output_section->vma
4423 + splt->output_offset
4424 + h->plt.offset
4425 + elf_mn10300_plt_temp_offset (info)),
4426 sgot->contents + got_offset);
4428 /* Fill in the entry in the .rela.plt section. */
4429 rel.r_offset = (sgot->output_section->vma
4430 + sgot->output_offset
4431 + got_offset);
4432 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4433 rel.r_addend = 0;
4434 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4435 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4436 + plt_index));
4438 if (!h->def_regular)
4439 /* Mark the symbol as undefined, rather than as defined in
4440 the .plt section. Leave the value alone. */
4441 sym->st_shndx = SHN_UNDEF;
4444 if (h->got.offset != (bfd_vma) -1)
4446 asection * sgot;
4447 asection * srel;
4448 Elf_Internal_Rela rel;
4450 /* This symbol has an entry in the global offset table. Set it up. */
4452 sgot = bfd_get_section_by_name (dynobj, ".got");
4453 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4454 BFD_ASSERT (sgot != NULL && srel != NULL);
4456 rel.r_offset = (sgot->output_section->vma
4457 + sgot->output_offset
4458 + (h->got.offset &~ 1));
4460 /* If this is a -Bsymbolic link, and the symbol is defined
4461 locally, we just want to emit a RELATIVE reloc. Likewise if
4462 the symbol was forced to be local because of a version file.
4463 The entry in the global offset table will already have been
4464 initialized in the relocate_section function. */
4465 if (info->shared
4466 && (info->symbolic || h->dynindx == -1)
4467 && h->def_regular)
4469 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4470 rel.r_addend = (h->root.u.def.value
4471 + h->root.u.def.section->output_section->vma
4472 + h->root.u.def.section->output_offset);
4474 else
4476 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4477 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4478 rel.r_addend = 0;
4481 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4482 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4483 + srel->reloc_count));
4484 ++ srel->reloc_count;
4487 if (h->needs_copy)
4489 asection * s;
4490 Elf_Internal_Rela rel;
4492 /* This symbol needs a copy reloc. Set it up. */
4493 BFD_ASSERT (h->dynindx != -1
4494 && (h->root.type == bfd_link_hash_defined
4495 || h->root.type == bfd_link_hash_defweak));
4497 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4498 ".rela.bss");
4499 BFD_ASSERT (s != NULL);
4501 rel.r_offset = (h->root.u.def.value
4502 + h->root.u.def.section->output_section->vma
4503 + h->root.u.def.section->output_offset);
4504 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4505 rel.r_addend = 0;
4506 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4507 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4508 + s->reloc_count));
4509 ++ s->reloc_count;
4512 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4513 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4514 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4515 sym->st_shndx = SHN_ABS;
4517 return TRUE;
4520 /* Finish up the dynamic sections. */
4522 static bfd_boolean
4523 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd, info)
4524 bfd * output_bfd;
4525 struct bfd_link_info * info;
4527 bfd * dynobj;
4528 asection * sgot;
4529 asection * sdyn;
4531 dynobj = elf_hash_table (info)->dynobj;
4533 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4534 BFD_ASSERT (sgot != NULL);
4535 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4537 if (elf_hash_table (info)->dynamic_sections_created)
4539 asection * splt;
4540 Elf32_External_Dyn * dyncon;
4541 Elf32_External_Dyn * dynconend;
4543 BFD_ASSERT (sdyn != NULL);
4545 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4546 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4548 for (; dyncon < dynconend; dyncon++)
4550 Elf_Internal_Dyn dyn;
4551 const char * name;
4552 asection * s;
4554 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4556 switch (dyn.d_tag)
4558 default:
4559 break;
4561 case DT_PLTGOT:
4562 name = ".got";
4563 goto get_vma;
4565 case DT_JMPREL:
4566 name = ".rela.plt";
4567 get_vma:
4568 s = bfd_get_section_by_name (output_bfd, name);
4569 BFD_ASSERT (s != NULL);
4570 dyn.d_un.d_ptr = s->vma;
4571 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4572 break;
4574 case DT_PLTRELSZ:
4575 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4576 BFD_ASSERT (s != NULL);
4577 dyn.d_un.d_val = s->size;
4578 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4579 break;
4581 case DT_RELASZ:
4582 /* My reading of the SVR4 ABI indicates that the
4583 procedure linkage table relocs (DT_JMPREL) should be
4584 included in the overall relocs (DT_RELA). This is
4585 what Solaris does. However, UnixWare can not handle
4586 that case. Therefore, we override the DT_RELASZ entry
4587 here to make it not include the JMPREL relocs. Since
4588 the linker script arranges for .rela.plt to follow all
4589 other relocation sections, we don't have to worry
4590 about changing the DT_RELA entry. */
4591 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4592 if (s != NULL)
4593 dyn.d_un.d_val -= s->size;
4594 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4595 break;
4599 /* Fill in the first entry in the procedure linkage table. */
4600 splt = bfd_get_section_by_name (dynobj, ".plt");
4601 if (splt && splt->size > 0)
4603 if (info->shared)
4605 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4606 elf_mn10300_sizeof_plt (info));
4608 else
4610 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4611 bfd_put_32 (output_bfd,
4612 sgot->output_section->vma + sgot->output_offset + 4,
4613 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4614 bfd_put_32 (output_bfd,
4615 sgot->output_section->vma + sgot->output_offset + 8,
4616 splt->contents + elf_mn10300_plt0_linker_offset (info));
4619 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4620 really seem like the right value. */
4621 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4625 /* Fill in the first three entries in the global offset table. */
4626 if (sgot->size > 0)
4628 if (sdyn == NULL)
4629 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4630 else
4631 bfd_put_32 (output_bfd,
4632 sdyn->output_section->vma + sdyn->output_offset,
4633 sgot->contents);
4634 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4635 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4638 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4640 return TRUE;
4643 /* Classify relocation types, such that combreloc can sort them
4644 properly. */
4646 static enum elf_reloc_type_class
4647 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4649 switch ((int) ELF32_R_TYPE (rela->r_info))
4651 case R_MN10300_RELATIVE:
4652 return reloc_class_relative;
4653 case R_MN10300_JMP_SLOT:
4654 return reloc_class_plt;
4655 case R_MN10300_COPY:
4656 return reloc_class_copy;
4657 default:
4658 return reloc_class_normal;
4662 #ifndef ELF_ARCH
4663 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4664 #define TARGET_LITTLE_NAME "elf32-mn10300"
4665 #define ELF_ARCH bfd_arch_mn10300
4666 #define ELF_MACHINE_CODE EM_MN10300
4667 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4668 #define ELF_MAXPAGESIZE 0x1000
4669 #endif
4671 #define elf_info_to_howto mn10300_info_to_howto
4672 #define elf_info_to_howto_rel 0
4673 #define elf_backend_can_gc_sections 1
4674 #define elf_backend_rela_normal 1
4675 #define elf_backend_check_relocs mn10300_elf_check_relocs
4676 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4677 #define elf_backend_relocate_section mn10300_elf_relocate_section
4678 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4679 #define bfd_elf32_bfd_get_relocated_section_contents \
4680 mn10300_elf_get_relocated_section_contents
4681 #define bfd_elf32_bfd_link_hash_table_create \
4682 elf32_mn10300_link_hash_table_create
4683 #define bfd_elf32_bfd_link_hash_table_free \
4684 elf32_mn10300_link_hash_table_free
4686 #ifndef elf_symbol_leading_char
4687 #define elf_symbol_leading_char '_'
4688 #endif
4690 /* So we can set bits in e_flags. */
4691 #define elf_backend_final_write_processing \
4692 _bfd_mn10300_elf_final_write_processing
4693 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4695 #define bfd_elf32_bfd_merge_private_bfd_data \
4696 _bfd_mn10300_elf_merge_private_bfd_data
4698 #define elf_backend_can_gc_sections 1
4699 #define elf_backend_create_dynamic_sections \
4700 _bfd_mn10300_elf_create_dynamic_sections
4701 #define elf_backend_adjust_dynamic_symbol \
4702 _bfd_mn10300_elf_adjust_dynamic_symbol
4703 #define elf_backend_size_dynamic_sections \
4704 _bfd_mn10300_elf_size_dynamic_sections
4705 #define elf_backend_finish_dynamic_symbol \
4706 _bfd_mn10300_elf_finish_dynamic_symbol
4707 #define elf_backend_finish_dynamic_sections \
4708 _bfd_mn10300_elf_finish_dynamic_sections
4710 #define elf_backend_reloc_type_class \
4711 _bfd_mn10300_elf_reloc_type_class
4713 #define elf_backend_want_got_plt 1
4714 #define elf_backend_plt_readonly 1
4715 #define elf_backend_want_plt_sym 0
4716 #define elf_backend_got_header_size 12
4718 #include "elf32-target.h"