* elf-bfd.h (struct elf_link_hash_entry): Replace elf_link_hash_flags
[binutils.git] / bfd / elf-m10300.c
blob5daf27fc51a755decaf3f4b7bf9be90794fed92c
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
3 Free Software Foundation, Inc.
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
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 (abfd, ".plt");
580 if (s == NULL
581 || ! bfd_set_section_flags (abfd, s, pltflags)
582 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
583 return FALSE;
585 if (bed->want_plt_sym)
587 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
588 .plt section. */
589 bh = NULL;
590 if (! (_bfd_generic_link_add_one_symbol
591 (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
592 (bfd_vma) 0, (const char *) NULL, FALSE,
593 get_elf_backend_data (abfd)->collect, &bh)))
594 return FALSE;
595 h = (struct elf_link_hash_entry *) bh;
596 h->def_regular = 1;
597 h->type = STT_OBJECT;
599 if (info->shared
600 && ! bfd_elf_link_record_dynamic_symbol (info, h))
601 return FALSE;
604 s = bfd_make_section (abfd, ".got");
605 if (s == NULL
606 || ! bfd_set_section_flags (abfd, s, flags)
607 || ! bfd_set_section_alignment (abfd, s, ptralign))
608 return FALSE;
610 if (bed->want_got_plt)
612 s = bfd_make_section (abfd, ".got.plt");
613 if (s == NULL
614 || ! bfd_set_section_flags (abfd, s, flags)
615 || ! bfd_set_section_alignment (abfd, s, ptralign))
616 return FALSE;
619 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
620 (or .got.plt) section. We don't do this in the linker script
621 because we don't want to define the symbol if we are not creating
622 a global offset table. */
623 bh = NULL;
624 if (!(_bfd_generic_link_add_one_symbol
625 (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s,
626 bed->got_symbol_offset, (const char *) NULL, FALSE,
627 bed->collect, &bh)))
628 return FALSE;
629 h = (struct elf_link_hash_entry *) bh;
630 h->def_regular = 1;
631 h->type = STT_OBJECT;
633 if (info->shared
634 && ! bfd_elf_link_record_dynamic_symbol (info, h))
635 return FALSE;
637 elf_hash_table (info)->hgot = h;
639 /* The first bit of the global offset table is the header. */
640 s->size += bed->got_header_size + bed->got_symbol_offset;
642 return TRUE;
645 static reloc_howto_type *
646 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
647 bfd *abfd ATTRIBUTE_UNUSED;
648 bfd_reloc_code_real_type code;
650 unsigned int i;
652 for (i = 0;
653 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
654 i++)
656 if (mn10300_reloc_map[i].bfd_reloc_val == code)
657 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
660 return NULL;
663 /* Set the howto pointer for an MN10300 ELF reloc. */
665 static void
666 mn10300_info_to_howto (abfd, cache_ptr, dst)
667 bfd *abfd ATTRIBUTE_UNUSED;
668 arelent *cache_ptr;
669 Elf_Internal_Rela *dst;
671 unsigned int r_type;
673 r_type = ELF32_R_TYPE (dst->r_info);
674 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
675 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
678 /* Look through the relocs for a section during the first phase.
679 Since we don't do .gots or .plts, we just need to consider the
680 virtual table relocs for gc. */
682 static bfd_boolean
683 mn10300_elf_check_relocs (abfd, info, sec, relocs)
684 bfd *abfd;
685 struct bfd_link_info *info;
686 asection *sec;
687 const Elf_Internal_Rela *relocs;
689 Elf_Internal_Shdr *symtab_hdr;
690 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
691 const Elf_Internal_Rela *rel;
692 const Elf_Internal_Rela *rel_end;
693 bfd * dynobj;
694 bfd_vma * local_got_offsets;
695 asection * sgot;
696 asection * srelgot;
697 asection * sreloc;
699 sgot = NULL;
700 srelgot = NULL;
701 sreloc = NULL;
703 if (info->relocatable)
704 return TRUE;
706 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
707 sym_hashes = elf_sym_hashes (abfd);
708 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
709 if (!elf_bad_symtab (abfd))
710 sym_hashes_end -= symtab_hdr->sh_info;
712 dynobj = elf_hash_table (info)->dynobj;
713 local_got_offsets = elf_local_got_offsets (abfd);
714 rel_end = relocs + sec->reloc_count;
715 for (rel = relocs; rel < rel_end; rel++)
717 struct elf_link_hash_entry *h;
718 unsigned long r_symndx;
720 r_symndx = ELF32_R_SYM (rel->r_info);
721 if (r_symndx < symtab_hdr->sh_info)
722 h = NULL;
723 else
724 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
726 /* Some relocs require a global offset table. */
727 if (dynobj == NULL)
729 switch (ELF32_R_TYPE (rel->r_info))
731 case R_MN10300_GOT32:
732 case R_MN10300_GOT24:
733 case R_MN10300_GOT16:
734 case R_MN10300_GOTOFF32:
735 case R_MN10300_GOTOFF24:
736 case R_MN10300_GOTOFF16:
737 case R_MN10300_GOTPC32:
738 case R_MN10300_GOTPC16:
739 elf_hash_table (info)->dynobj = dynobj = abfd;
740 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
741 return FALSE;
742 break;
744 default:
745 break;
749 switch (ELF32_R_TYPE (rel->r_info))
751 /* This relocation describes the C++ object vtable hierarchy.
752 Reconstruct it for later use during GC. */
753 case R_MN10300_GNU_VTINHERIT:
754 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
755 return FALSE;
756 break;
758 /* This relocation describes which C++ vtable entries are actually
759 used. Record for later use during GC. */
760 case R_MN10300_GNU_VTENTRY:
761 if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
762 return FALSE;
763 break;
764 case R_MN10300_GOT32:
765 case R_MN10300_GOT24:
766 case R_MN10300_GOT16:
767 /* This symbol requires a global offset table entry. */
769 if (sgot == NULL)
771 sgot = bfd_get_section_by_name (dynobj, ".got");
772 BFD_ASSERT (sgot != NULL);
775 if (srelgot == NULL
776 && (h != NULL || info->shared))
778 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
779 if (srelgot == NULL)
781 srelgot = bfd_make_section (dynobj, ".rela.got");
782 if (srelgot == NULL
783 || ! bfd_set_section_flags (dynobj, srelgot,
784 (SEC_ALLOC
785 | SEC_LOAD
786 | SEC_HAS_CONTENTS
787 | SEC_IN_MEMORY
788 | SEC_LINKER_CREATED
789 | SEC_READONLY))
790 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
791 return FALSE;
795 if (h != NULL)
797 if (h->got.offset != (bfd_vma) -1)
798 /* We have already allocated space in the .got. */
799 break;
801 h->got.offset = sgot->size;
803 /* Make sure this symbol is output as a dynamic symbol. */
804 if (h->dynindx == -1)
806 if (! bfd_elf_link_record_dynamic_symbol (info, h))
807 return FALSE;
810 srelgot->size += sizeof (Elf32_External_Rela);
812 else
814 /* This is a global offset table entry for a local
815 symbol. */
816 if (local_got_offsets == NULL)
818 size_t size;
819 unsigned int i;
821 size = symtab_hdr->sh_info * sizeof (bfd_vma);
822 local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size);
824 if (local_got_offsets == NULL)
825 return FALSE;
826 elf_local_got_offsets (abfd) = local_got_offsets;
828 for (i = 0; i < symtab_hdr->sh_info; i++)
829 local_got_offsets[i] = (bfd_vma) -1;
832 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
833 /* We have already allocated space in the .got. */
834 break;
836 local_got_offsets[r_symndx] = sgot->size;
838 if (info->shared)
839 /* If we are generating a shared object, we need to
840 output a R_MN10300_RELATIVE reloc so that the dynamic
841 linker can adjust this GOT entry. */
842 srelgot->size += sizeof (Elf32_External_Rela);
845 sgot->size += 4;
847 break;
849 case R_MN10300_PLT32:
850 case R_MN10300_PLT16:
851 /* This symbol requires a procedure linkage table entry. We
852 actually build the entry in adjust_dynamic_symbol,
853 because this might be a case of linking PIC code which is
854 never referenced by a dynamic object, in which case we
855 don't need to generate a procedure linkage table entry
856 after all. */
858 /* If this is a local symbol, we resolve it directly without
859 creating a procedure linkage table entry. */
860 if (h == NULL)
861 continue;
863 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
864 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
865 break;
867 h->needs_plt = 1;
869 break;
871 case R_MN10300_24:
872 case R_MN10300_16:
873 case R_MN10300_8:
874 case R_MN10300_PCREL32:
875 case R_MN10300_PCREL16:
876 case R_MN10300_PCREL8:
877 if (h != NULL)
878 h->non_got_ref = 1;
879 break;
881 case R_MN10300_32:
882 if (h != NULL)
883 h->non_got_ref = 1;
885 /* If we are creating a shared library, then we need to copy
886 the reloc into the shared library. */
887 if (info->shared
888 && (sec->flags & SEC_ALLOC) != 0)
890 /* When creating a shared object, we must copy these
891 reloc types into the output file. We create a reloc
892 section in dynobj and make room for this reloc. */
893 if (sreloc == NULL)
895 const char * name;
897 name = (bfd_elf_string_from_elf_section
898 (abfd,
899 elf_elfheader (abfd)->e_shstrndx,
900 elf_section_data (sec)->rel_hdr.sh_name));
901 if (name == NULL)
902 return FALSE;
904 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
905 && strcmp (bfd_get_section_name (abfd, sec),
906 name + 5) == 0);
908 sreloc = bfd_get_section_by_name (dynobj, name);
909 if (sreloc == NULL)
911 flagword flags;
913 sreloc = bfd_make_section (dynobj, name);
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 if (sreloc == NULL
919 || ! bfd_set_section_flags (dynobj, sreloc, flags)
920 || ! bfd_set_section_alignment (dynobj, sreloc, 2))
921 return FALSE;
925 sreloc->size += sizeof (Elf32_External_Rela);
928 break;
932 return TRUE;
935 /* Return the section that should be marked against GC for a given
936 relocation. */
938 static asection *
939 mn10300_elf_gc_mark_hook (sec, info, rel, h, sym)
940 asection *sec;
941 struct bfd_link_info *info ATTRIBUTE_UNUSED;
942 Elf_Internal_Rela *rel;
943 struct elf_link_hash_entry *h;
944 Elf_Internal_Sym *sym;
946 if (h != NULL)
948 switch (ELF32_R_TYPE (rel->r_info))
950 case R_MN10300_GNU_VTINHERIT:
951 case R_MN10300_GNU_VTENTRY:
952 break;
954 default:
955 switch (h->root.type)
957 case bfd_link_hash_defined:
958 case bfd_link_hash_defweak:
959 return h->root.u.def.section;
961 case bfd_link_hash_common:
962 return h->root.u.c.p->section;
964 default:
965 break;
969 else
970 return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
972 return NULL;
975 /* Perform a relocation as part of a final link. */
976 static bfd_reloc_status_type
977 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
978 input_section, contents, offset, value,
979 addend, h, symndx, info, sym_sec, is_local)
980 reloc_howto_type *howto;
981 bfd *input_bfd;
982 bfd *output_bfd ATTRIBUTE_UNUSED;
983 asection *input_section;
984 bfd_byte *contents;
985 bfd_vma offset;
986 bfd_vma value;
987 bfd_vma addend;
988 struct elf_link_hash_entry * h;
989 unsigned long symndx;
990 struct bfd_link_info *info;
991 asection *sym_sec ATTRIBUTE_UNUSED;
992 int is_local ATTRIBUTE_UNUSED;
994 unsigned long r_type = howto->type;
995 bfd_byte *hit_data = contents + offset;
996 bfd * dynobj;
997 bfd_vma * local_got_offsets;
998 asection * sgot;
999 asection * splt;
1000 asection * sreloc;
1002 dynobj = elf_hash_table (info)->dynobj;
1003 local_got_offsets = elf_local_got_offsets (input_bfd);
1005 sgot = NULL;
1006 splt = NULL;
1007 sreloc = NULL;
1009 switch (r_type)
1011 case R_MN10300_24:
1012 case R_MN10300_16:
1013 case R_MN10300_8:
1014 case R_MN10300_PCREL8:
1015 case R_MN10300_PCREL16:
1016 case R_MN10300_PCREL32:
1017 case R_MN10300_GOTOFF32:
1018 case R_MN10300_GOTOFF24:
1019 case R_MN10300_GOTOFF16:
1020 if (info->shared
1021 && (input_section->flags & SEC_ALLOC) != 0
1022 && h != NULL
1023 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1024 return bfd_reloc_dangerous;
1027 switch (r_type)
1029 case R_MN10300_NONE:
1030 return bfd_reloc_ok;
1032 case R_MN10300_32:
1033 if (info->shared
1034 && (input_section->flags & SEC_ALLOC) != 0)
1036 Elf_Internal_Rela outrel;
1037 bfd_boolean skip, relocate;
1039 /* When generating a shared object, these relocations are
1040 copied into the output file to be resolved at run
1041 time. */
1042 if (sreloc == NULL)
1044 const char * name;
1046 name = (bfd_elf_string_from_elf_section
1047 (input_bfd,
1048 elf_elfheader (input_bfd)->e_shstrndx,
1049 elf_section_data (input_section)->rel_hdr.sh_name));
1050 if (name == NULL)
1051 return FALSE;
1053 BFD_ASSERT (strncmp (name, ".rela", 5) == 0
1054 && strcmp (bfd_get_section_name (input_bfd,
1055 input_section),
1056 name + 5) == 0);
1058 sreloc = bfd_get_section_by_name (dynobj, name);
1059 BFD_ASSERT (sreloc != NULL);
1062 skip = FALSE;
1064 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1065 input_section, offset);
1066 if (outrel.r_offset == (bfd_vma) -1)
1067 skip = TRUE;
1069 outrel.r_offset += (input_section->output_section->vma
1070 + input_section->output_offset);
1072 if (skip)
1074 memset (&outrel, 0, sizeof outrel);
1075 relocate = FALSE;
1077 else
1079 /* h->dynindx may be -1 if this symbol was marked to
1080 become local. */
1081 if (h == NULL
1082 || SYMBOL_REFERENCES_LOCAL (info, h))
1084 relocate = TRUE;
1085 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1086 outrel.r_addend = value + addend;
1088 else
1090 BFD_ASSERT (h->dynindx != -1);
1091 relocate = FALSE;
1092 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1093 outrel.r_addend = value + addend;
1097 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1098 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1099 + sreloc->reloc_count));
1100 ++sreloc->reloc_count;
1102 /* If this reloc is against an external symbol, we do
1103 not want to fiddle with the addend. Otherwise, we
1104 need to include the symbol value so that it becomes
1105 an addend for the dynamic reloc. */
1106 if (! relocate)
1107 return bfd_reloc_ok;
1109 value += addend;
1110 bfd_put_32 (input_bfd, value, hit_data);
1111 return bfd_reloc_ok;
1113 case R_MN10300_24:
1114 value += addend;
1116 if ((long) value > 0x7fffff || (long) value < -0x800000)
1117 return bfd_reloc_overflow;
1119 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1120 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1121 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1122 return bfd_reloc_ok;
1124 case R_MN10300_16:
1125 value += addend;
1127 if ((long) value > 0x7fff || (long) value < -0x8000)
1128 return bfd_reloc_overflow;
1130 bfd_put_16 (input_bfd, value, hit_data);
1131 return bfd_reloc_ok;
1133 case R_MN10300_8:
1134 value += addend;
1136 if ((long) value > 0x7f || (long) value < -0x80)
1137 return bfd_reloc_overflow;
1139 bfd_put_8 (input_bfd, value, hit_data);
1140 return bfd_reloc_ok;
1142 case R_MN10300_PCREL8:
1143 value -= (input_section->output_section->vma
1144 + input_section->output_offset);
1145 value -= offset;
1146 value += addend;
1148 if ((long) value > 0xff || (long) value < -0x100)
1149 return bfd_reloc_overflow;
1151 bfd_put_8 (input_bfd, value, hit_data);
1152 return bfd_reloc_ok;
1154 case R_MN10300_PCREL16:
1155 value -= (input_section->output_section->vma
1156 + input_section->output_offset);
1157 value -= offset;
1158 value += addend;
1160 if ((long) value > 0xffff || (long) value < -0x10000)
1161 return bfd_reloc_overflow;
1163 bfd_put_16 (input_bfd, value, hit_data);
1164 return bfd_reloc_ok;
1166 case R_MN10300_PCREL32:
1167 value -= (input_section->output_section->vma
1168 + input_section->output_offset);
1169 value -= offset;
1170 value += addend;
1172 bfd_put_32 (input_bfd, value, hit_data);
1173 return bfd_reloc_ok;
1175 case R_MN10300_GNU_VTINHERIT:
1176 case R_MN10300_GNU_VTENTRY:
1177 return bfd_reloc_ok;
1179 case R_MN10300_GOTPC32:
1180 /* Use global offset table as symbol value. */
1182 value = bfd_get_section_by_name (dynobj,
1183 ".got")->output_section->vma;
1184 value -= (input_section->output_section->vma
1185 + input_section->output_offset);
1186 value -= offset;
1187 value += addend;
1189 bfd_put_32 (input_bfd, value, hit_data);
1190 return bfd_reloc_ok;
1192 case R_MN10300_GOTPC16:
1193 /* Use global offset table as symbol value. */
1195 value = bfd_get_section_by_name (dynobj,
1196 ".got")->output_section->vma;
1197 value -= (input_section->output_section->vma
1198 + input_section->output_offset);
1199 value -= offset;
1200 value += addend;
1202 if ((long) value > 0xffff || (long) value < -0x10000)
1203 return bfd_reloc_overflow;
1205 bfd_put_16 (input_bfd, value, hit_data);
1206 return bfd_reloc_ok;
1208 case R_MN10300_GOTOFF32:
1209 value -= bfd_get_section_by_name (dynobj,
1210 ".got")->output_section->vma;
1211 value += addend;
1213 bfd_put_32 (input_bfd, value, hit_data);
1214 return bfd_reloc_ok;
1216 case R_MN10300_GOTOFF24:
1217 value -= bfd_get_section_by_name (dynobj,
1218 ".got")->output_section->vma;
1219 value += addend;
1221 if ((long) value > 0x7fffff || (long) value < -0x800000)
1222 return bfd_reloc_overflow;
1224 bfd_put_8 (input_bfd, value, hit_data);
1225 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1226 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1227 return bfd_reloc_ok;
1229 case R_MN10300_GOTOFF16:
1230 value -= bfd_get_section_by_name (dynobj,
1231 ".got")->output_section->vma;
1232 value += addend;
1234 if ((long) value > 0xffff || (long) value < -0x10000)
1235 return bfd_reloc_overflow;
1237 bfd_put_16 (input_bfd, value, hit_data);
1238 return bfd_reloc_ok;
1240 case R_MN10300_PLT32:
1241 if (h != NULL
1242 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1243 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1244 && h->plt.offset != (bfd_vma) -1)
1246 asection * splt;
1248 splt = bfd_get_section_by_name (dynobj, ".plt");
1250 value = (splt->output_section->vma
1251 + splt->output_offset
1252 + h->plt.offset) - value;
1255 value -= (input_section->output_section->vma
1256 + input_section->output_offset);
1257 value -= offset;
1258 value += addend;
1260 bfd_put_32 (input_bfd, value, hit_data);
1261 return bfd_reloc_ok;
1263 case R_MN10300_PLT16:
1264 if (h != NULL
1265 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1266 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1267 && h->plt.offset != (bfd_vma) -1)
1269 asection * splt;
1271 splt = bfd_get_section_by_name (dynobj, ".plt");
1273 value = (splt->output_section->vma
1274 + splt->output_offset
1275 + h->plt.offset) - value;
1278 value -= (input_section->output_section->vma
1279 + input_section->output_offset);
1280 value -= offset;
1281 value += addend;
1283 if ((long) value > 0xffff || (long) value < -0x10000)
1284 return bfd_reloc_overflow;
1286 bfd_put_16 (input_bfd, value, hit_data);
1287 return bfd_reloc_ok;
1289 case R_MN10300_GOT32:
1290 case R_MN10300_GOT24:
1291 case R_MN10300_GOT16:
1293 asection * sgot;
1295 sgot = bfd_get_section_by_name (dynobj, ".got");
1297 if (h != NULL)
1299 bfd_vma off;
1301 off = h->got.offset;
1302 BFD_ASSERT (off != (bfd_vma) -1);
1304 if (! elf_hash_table (info)->dynamic_sections_created
1305 || SYMBOL_REFERENCES_LOCAL (info, h))
1306 /* This is actually a static link, or it is a
1307 -Bsymbolic link and the symbol is defined
1308 locally, or the symbol was forced to be local
1309 because of a version file. We must initialize
1310 this entry in the global offset table.
1312 When doing a dynamic link, we create a .rela.got
1313 relocation entry to initialize the value. This
1314 is done in the finish_dynamic_symbol routine. */
1315 bfd_put_32 (output_bfd, value,
1316 sgot->contents + off);
1318 value = sgot->output_offset + off;
1320 else
1322 bfd_vma off;
1324 off = elf_local_got_offsets (input_bfd)[symndx];
1326 bfd_put_32 (output_bfd, value, sgot->contents + off);
1328 if (info->shared)
1330 asection * srelgot;
1331 Elf_Internal_Rela outrel;
1333 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1334 BFD_ASSERT (srelgot != NULL);
1336 outrel.r_offset = (sgot->output_section->vma
1337 + sgot->output_offset
1338 + off);
1339 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1340 outrel.r_addend = value;
1341 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1342 (bfd_byte *) (((Elf32_External_Rela *)
1343 srelgot->contents)
1344 + srelgot->reloc_count));
1345 ++ srelgot->reloc_count;
1348 value = sgot->output_offset + off;
1352 value += addend;
1354 if (r_type == R_MN10300_GOT32)
1356 bfd_put_32 (input_bfd, value, hit_data);
1357 return bfd_reloc_ok;
1359 else if (r_type == R_MN10300_GOT24)
1361 if ((long) value > 0x7fffff || (long) value < -0x800000)
1362 return bfd_reloc_overflow;
1364 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1365 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1366 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1367 return bfd_reloc_ok;
1369 else if (r_type == R_MN10300_GOT16)
1371 if ((long) value > 0xffff || (long) value < -0x10000)
1372 return bfd_reloc_overflow;
1374 bfd_put_16 (input_bfd, value, hit_data);
1375 return bfd_reloc_ok;
1377 /* Fall through. */
1379 default:
1380 return bfd_reloc_notsupported;
1384 /* Relocate an MN10300 ELF section. */
1385 static bfd_boolean
1386 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
1387 contents, relocs, local_syms, local_sections)
1388 bfd *output_bfd;
1389 struct bfd_link_info *info;
1390 bfd *input_bfd;
1391 asection *input_section;
1392 bfd_byte *contents;
1393 Elf_Internal_Rela *relocs;
1394 Elf_Internal_Sym *local_syms;
1395 asection **local_sections;
1397 Elf_Internal_Shdr *symtab_hdr;
1398 struct elf_link_hash_entry **sym_hashes;
1399 Elf_Internal_Rela *rel, *relend;
1401 if (info->relocatable)
1402 return TRUE;
1404 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1405 sym_hashes = elf_sym_hashes (input_bfd);
1407 rel = relocs;
1408 relend = relocs + input_section->reloc_count;
1409 for (; rel < relend; rel++)
1411 int r_type;
1412 reloc_howto_type *howto;
1413 unsigned long r_symndx;
1414 Elf_Internal_Sym *sym;
1415 asection *sec;
1416 struct elf32_mn10300_link_hash_entry *h;
1417 bfd_vma relocation;
1418 bfd_reloc_status_type r;
1420 r_symndx = ELF32_R_SYM (rel->r_info);
1421 r_type = ELF32_R_TYPE (rel->r_info);
1422 howto = elf_mn10300_howto_table + r_type;
1424 /* Just skip the vtable gc relocs. */
1425 if (r_type == R_MN10300_GNU_VTINHERIT
1426 || r_type == R_MN10300_GNU_VTENTRY)
1427 continue;
1429 h = NULL;
1430 sym = NULL;
1431 sec = NULL;
1432 if (r_symndx < symtab_hdr->sh_info)
1434 sym = local_syms + r_symndx;
1435 sec = local_sections[r_symndx];
1436 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1438 else
1440 bfd_boolean unresolved_reloc;
1441 bfd_boolean warned;
1442 struct elf_link_hash_entry *hh;
1444 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1445 r_symndx, symtab_hdr, sym_hashes,
1446 hh, sec, relocation,
1447 unresolved_reloc, warned);
1449 h = (struct elf32_mn10300_link_hash_entry *) hh;
1451 if ((h->root.root.type == bfd_link_hash_defined
1452 || h->root.root.type == bfd_link_hash_defweak)
1453 && ( r_type == R_MN10300_GOTPC32
1454 || r_type == R_MN10300_GOTPC16
1455 || (( r_type == R_MN10300_PLT32
1456 || r_type == R_MN10300_PLT16)
1457 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1458 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1459 && h->root.plt.offset != (bfd_vma) -1)
1460 || (( r_type == R_MN10300_GOT32
1461 || r_type == R_MN10300_GOT24
1462 || r_type == R_MN10300_GOT16)
1463 && elf_hash_table (info)->dynamic_sections_created
1464 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1465 || (r_type == R_MN10300_32
1466 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1467 && ((input_section->flags & SEC_ALLOC) != 0
1468 /* DWARF will emit R_MN10300_32 relocations
1469 in its sections against symbols defined
1470 externally in shared libraries. We can't
1471 do anything with them here. */
1472 || ((input_section->flags & SEC_DEBUGGING) != 0
1473 && h->root.def_dynamic)))))
1474 /* In these cases, we don't need the relocation
1475 value. We check specially because in some
1476 obscure cases sec->output_section will be NULL. */
1477 relocation = 0;
1479 else if (unresolved_reloc)
1480 (*_bfd_error_handler)
1481 (_("%s: warning: unresolvable relocation against symbol `%s' from %s section"),
1482 bfd_get_filename (input_bfd), h->root.root.root.string,
1483 bfd_get_section_name (input_bfd, input_section));
1486 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1487 input_section,
1488 contents, rel->r_offset,
1489 relocation, rel->r_addend,
1490 (struct elf_link_hash_entry *)h,
1491 r_symndx,
1492 info, sec, h == NULL);
1494 if (r != bfd_reloc_ok)
1496 const char *name;
1497 const char *msg = (const char *) 0;
1499 if (h != NULL)
1500 name = h->root.root.root.string;
1501 else
1503 name = (bfd_elf_string_from_elf_section
1504 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1505 if (name == NULL || *name == '\0')
1506 name = bfd_section_name (input_bfd, sec);
1509 switch (r)
1511 case bfd_reloc_overflow:
1512 if (! ((*info->callbacks->reloc_overflow)
1513 (info, name, howto->name, (bfd_vma) 0,
1514 input_bfd, input_section, rel->r_offset)))
1515 return FALSE;
1516 break;
1518 case bfd_reloc_undefined:
1519 if (! ((*info->callbacks->undefined_symbol)
1520 (info, name, input_bfd, input_section,
1521 rel->r_offset, TRUE)))
1522 return FALSE;
1523 break;
1525 case bfd_reloc_outofrange:
1526 msg = _("internal error: out of range error");
1527 goto common_error;
1529 case bfd_reloc_notsupported:
1530 msg = _("internal error: unsupported relocation error");
1531 goto common_error;
1533 case bfd_reloc_dangerous:
1534 msg = _("internal error: dangerous error");
1535 goto common_error;
1537 default:
1538 msg = _("internal error: unknown error");
1539 /* fall through */
1541 common_error:
1542 if (!((*info->callbacks->warning)
1543 (info, msg, name, input_bfd, input_section,
1544 rel->r_offset)))
1545 return FALSE;
1546 break;
1551 return TRUE;
1554 /* Finish initializing one hash table entry. */
1555 static bfd_boolean
1556 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
1557 struct bfd_hash_entry *gen_entry;
1558 PTR in_args;
1560 struct elf32_mn10300_link_hash_entry *entry;
1561 struct bfd_link_info *link_info = (struct bfd_link_info *)in_args;
1562 unsigned int byte_count = 0;
1564 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1566 if (entry->root.root.type == bfd_link_hash_warning)
1567 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1569 /* If we already know we want to convert "call" to "calls" for calls
1570 to this symbol, then return now. */
1571 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1572 return TRUE;
1574 /* If there are no named calls to this symbol, or there's nothing we
1575 can move from the function itself into the "call" instruction,
1576 then note that all "call" instructions should be converted into
1577 "calls" instructions and return. If a symbol is available for
1578 dynamic symbol resolution (overridable or overriding), avoid
1579 custom calling conventions. */
1580 if (entry->direct_calls == 0
1581 || (entry->stack_size == 0 && entry->movm_args == 0)
1582 || (elf_hash_table (link_info)->dynamic_sections_created
1583 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1584 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1586 /* Make a note that we should convert "call" instructions to "calls"
1587 instructions for calls to this symbol. */
1588 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1589 return TRUE;
1592 /* We may be able to move some instructions from the function itself into
1593 the "call" instruction. Count how many bytes we might be able to
1594 eliminate in the function itself. */
1596 /* A movm instruction is two bytes. */
1597 if (entry->movm_args)
1598 byte_count += 2;
1600 /* Count the insn to allocate stack space too. */
1601 if (entry->stack_size > 0)
1603 if (entry->stack_size <= 128)
1604 byte_count += 3;
1605 else
1606 byte_count += 4;
1609 /* If using "call" will result in larger code, then turn all
1610 the associated "call" instructions into "calls" instructions. */
1611 if (byte_count < entry->direct_calls)
1612 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1614 /* This routine never fails. */
1615 return TRUE;
1618 /* This function handles relaxing for the mn10300.
1620 There are quite a few relaxing opportunities available on the mn10300:
1622 * calls:32 -> calls:16 2 bytes
1623 * call:32 -> call:16 2 bytes
1625 * call:32 -> calls:32 1 byte
1626 * call:16 -> calls:16 1 byte
1627 * These are done anytime using "calls" would result
1628 in smaller code, or when necessary to preserve the
1629 meaning of the program.
1631 * call:32 varies
1632 * call:16
1633 * In some circumstances we can move instructions
1634 from a function prologue into a "call" instruction.
1635 This is only done if the resulting code is no larger
1636 than the original code.
1638 * jmp:32 -> jmp:16 2 bytes
1639 * jmp:16 -> bra:8 1 byte
1641 * If the previous instruction is a conditional branch
1642 around the jump/bra, we may be able to reverse its condition
1643 and change its target to the jump's target. The jump/bra
1644 can then be deleted. 2 bytes
1646 * mov abs32 -> mov abs16 1 or 2 bytes
1648 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
1649 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
1651 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
1652 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
1654 We don't handle imm16->imm8 or d16->d8 as they're very rare
1655 and somewhat more difficult to support. */
1657 static bfd_boolean
1658 mn10300_elf_relax_section (abfd, sec, link_info, again)
1659 bfd *abfd;
1660 asection *sec;
1661 struct bfd_link_info *link_info;
1662 bfd_boolean *again;
1664 Elf_Internal_Shdr *symtab_hdr;
1665 Elf_Internal_Rela *internal_relocs = NULL;
1666 Elf_Internal_Rela *irel, *irelend;
1667 bfd_byte *contents = NULL;
1668 Elf_Internal_Sym *isymbuf = NULL;
1669 struct elf32_mn10300_link_hash_table *hash_table;
1670 asection *section = sec;
1672 /* Assume nothing changes. */
1673 *again = FALSE;
1675 /* We need a pointer to the mn10300 specific hash table. */
1676 hash_table = elf32_mn10300_hash_table (link_info);
1678 /* Initialize fields in each hash table entry the first time through. */
1679 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
1681 bfd *input_bfd;
1683 /* Iterate over all the input bfds. */
1684 for (input_bfd = link_info->input_bfds;
1685 input_bfd != NULL;
1686 input_bfd = input_bfd->link_next)
1688 /* We're going to need all the symbols for each bfd. */
1689 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1690 if (symtab_hdr->sh_info != 0)
1692 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1693 if (isymbuf == NULL)
1694 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1695 symtab_hdr->sh_info, 0,
1696 NULL, NULL, NULL);
1697 if (isymbuf == NULL)
1698 goto error_return;
1701 /* Iterate over each section in this bfd. */
1702 for (section = input_bfd->sections;
1703 section != NULL;
1704 section = section->next)
1706 struct elf32_mn10300_link_hash_entry *hash;
1707 Elf_Internal_Sym *sym;
1708 asection *sym_sec = NULL;
1709 const char *sym_name;
1710 char *new_name;
1712 /* If there's nothing to do in this section, skip it. */
1713 if (! (((section->flags & SEC_RELOC) != 0
1714 && section->reloc_count != 0)
1715 || (section->flags & SEC_CODE) != 0))
1716 continue;
1718 /* Get cached copy of section contents if it exists. */
1719 if (elf_section_data (section)->this_hdr.contents != NULL)
1720 contents = elf_section_data (section)->this_hdr.contents;
1721 else if (section->size != 0)
1723 /* Go get them off disk. */
1724 if (!bfd_malloc_and_get_section (input_bfd, section,
1725 &contents))
1726 goto error_return;
1728 else
1729 contents = NULL;
1731 /* If there aren't any relocs, then there's nothing to do. */
1732 if ((section->flags & SEC_RELOC) != 0
1733 && section->reloc_count != 0)
1736 /* Get a copy of the native relocations. */
1737 internal_relocs = (_bfd_elf_link_read_relocs
1738 (input_bfd, section, (PTR) NULL,
1739 (Elf_Internal_Rela *) NULL,
1740 link_info->keep_memory));
1741 if (internal_relocs == NULL)
1742 goto error_return;
1744 /* Now examine each relocation. */
1745 irel = internal_relocs;
1746 irelend = irel + section->reloc_count;
1747 for (; irel < irelend; irel++)
1749 long r_type;
1750 unsigned long r_index;
1751 unsigned char code;
1753 r_type = ELF32_R_TYPE (irel->r_info);
1754 r_index = ELF32_R_SYM (irel->r_info);
1756 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
1757 goto error_return;
1759 /* We need the name and hash table entry of the target
1760 symbol! */
1761 hash = NULL;
1762 sym = NULL;
1763 sym_sec = NULL;
1765 if (r_index < symtab_hdr->sh_info)
1767 /* A local symbol. */
1768 Elf_Internal_Sym *isym;
1769 struct elf_link_hash_table *elftab;
1770 bfd_size_type amt;
1772 isym = isymbuf + r_index;
1773 if (isym->st_shndx == SHN_UNDEF)
1774 sym_sec = bfd_und_section_ptr;
1775 else if (isym->st_shndx == SHN_ABS)
1776 sym_sec = bfd_abs_section_ptr;
1777 else if (isym->st_shndx == SHN_COMMON)
1778 sym_sec = bfd_com_section_ptr;
1779 else
1780 sym_sec
1781 = bfd_section_from_elf_index (input_bfd,
1782 isym->st_shndx);
1784 sym_name
1785 = bfd_elf_string_from_elf_section (input_bfd,
1786 (symtab_hdr
1787 ->sh_link),
1788 isym->st_name);
1790 /* If it isn't a function, then we don't care
1791 about it. */
1792 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
1793 continue;
1795 /* Tack on an ID so we can uniquely identify this
1796 local symbol in the global hash table. */
1797 amt = strlen (sym_name) + 10;
1798 new_name = bfd_malloc (amt);
1799 if (new_name == 0)
1800 goto error_return;
1802 sprintf (new_name, "%s_%08x",
1803 sym_name, (int) sym_sec);
1804 sym_name = new_name;
1806 elftab = &hash_table->static_hash_table->root;
1807 hash = ((struct elf32_mn10300_link_hash_entry *)
1808 elf_link_hash_lookup (elftab, sym_name,
1809 TRUE, TRUE, FALSE));
1810 free (new_name);
1812 else
1814 r_index -= symtab_hdr->sh_info;
1815 hash = (struct elf32_mn10300_link_hash_entry *)
1816 elf_sym_hashes (input_bfd)[r_index];
1819 /* If this is not a "call" instruction, then we
1820 should convert "call" instructions to "calls"
1821 instructions. */
1822 code = bfd_get_8 (input_bfd,
1823 contents + irel->r_offset - 1);
1824 if (code != 0xdd && code != 0xcd)
1825 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1827 /* If this is a jump/call, then bump the
1828 direct_calls counter. Else force "call" to
1829 "calls" conversions. */
1830 if (r_type == R_MN10300_PCREL32
1831 || r_type == R_MN10300_PLT32
1832 || r_type == R_MN10300_PLT16
1833 || r_type == R_MN10300_PCREL16)
1834 hash->direct_calls++;
1835 else
1836 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1840 /* Now look at the actual contents to get the stack size,
1841 and a list of what registers were saved in the prologue
1842 (ie movm_args). */
1843 if ((section->flags & SEC_CODE) != 0)
1845 Elf_Internal_Sym *isym, *isymend;
1846 unsigned int sec_shndx;
1847 struct elf_link_hash_entry **hashes;
1848 struct elf_link_hash_entry **end_hashes;
1849 unsigned int symcount;
1851 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1852 section);
1854 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1855 - symtab_hdr->sh_info);
1856 hashes = elf_sym_hashes (input_bfd);
1857 end_hashes = hashes + symcount;
1859 /* Look at each function defined in this section and
1860 update info for that function. */
1861 isymend = isymbuf + symtab_hdr->sh_info;
1862 for (isym = isymbuf; isym < isymend; isym++)
1864 if (isym->st_shndx == sec_shndx
1865 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
1867 struct elf_link_hash_table *elftab;
1868 bfd_size_type amt;
1869 struct elf_link_hash_entry **lhashes = hashes;
1871 /* Skip a local symbol if it aliases a
1872 global one. */
1873 for (; lhashes < end_hashes; lhashes++)
1875 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
1876 if ((hash->root.root.type == bfd_link_hash_defined
1877 || hash->root.root.type == bfd_link_hash_defweak)
1878 && hash->root.root.u.def.section == section
1879 && hash->root.type == STT_FUNC
1880 && hash->root.root.u.def.value == isym->st_value)
1881 break;
1883 if (lhashes != end_hashes)
1884 continue;
1886 if (isym->st_shndx == SHN_UNDEF)
1887 sym_sec = bfd_und_section_ptr;
1888 else if (isym->st_shndx == SHN_ABS)
1889 sym_sec = bfd_abs_section_ptr;
1890 else if (isym->st_shndx == SHN_COMMON)
1891 sym_sec = bfd_com_section_ptr;
1892 else
1893 sym_sec
1894 = bfd_section_from_elf_index (input_bfd,
1895 isym->st_shndx);
1897 sym_name = (bfd_elf_string_from_elf_section
1898 (input_bfd, symtab_hdr->sh_link,
1899 isym->st_name));
1901 /* Tack on an ID so we can uniquely identify this
1902 local symbol in the global hash table. */
1903 amt = strlen (sym_name) + 10;
1904 new_name = bfd_malloc (amt);
1905 if (new_name == 0)
1906 goto error_return;
1908 sprintf (new_name, "%s_%08x",
1909 sym_name, (int) sym_sec);
1910 sym_name = new_name;
1912 elftab = &hash_table->static_hash_table->root;
1913 hash = ((struct elf32_mn10300_link_hash_entry *)
1914 elf_link_hash_lookup (elftab, sym_name,
1915 TRUE, TRUE, FALSE));
1916 free (new_name);
1917 compute_function_info (input_bfd, hash,
1918 isym->st_value, contents);
1922 for (; hashes < end_hashes; hashes++)
1924 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1925 if ((hash->root.root.type == bfd_link_hash_defined
1926 || hash->root.root.type == bfd_link_hash_defweak)
1927 && hash->root.root.u.def.section == section
1928 && hash->root.type == STT_FUNC)
1929 compute_function_info (input_bfd, hash,
1930 (hash)->root.root.u.def.value,
1931 contents);
1935 /* Cache or free any memory we allocated for the relocs. */
1936 if (internal_relocs != NULL
1937 && elf_section_data (section)->relocs != internal_relocs)
1938 free (internal_relocs);
1939 internal_relocs = NULL;
1941 /* Cache or free any memory we allocated for the contents. */
1942 if (contents != NULL
1943 && elf_section_data (section)->this_hdr.contents != contents)
1945 if (! link_info->keep_memory)
1946 free (contents);
1947 else
1949 /* Cache the section contents for elf_link_input_bfd. */
1950 elf_section_data (section)->this_hdr.contents = contents;
1953 contents = NULL;
1956 /* Cache or free any memory we allocated for the symbols. */
1957 if (isymbuf != NULL
1958 && symtab_hdr->contents != (unsigned char *) isymbuf)
1960 if (! link_info->keep_memory)
1961 free (isymbuf);
1962 else
1964 /* Cache the symbols for elf_link_input_bfd. */
1965 symtab_hdr->contents = (unsigned char *) isymbuf;
1968 isymbuf = NULL;
1971 /* Now iterate on each symbol in the hash table and perform
1972 the final initialization steps on each. */
1973 elf32_mn10300_link_hash_traverse (hash_table,
1974 elf32_mn10300_finish_hash_table_entry,
1975 link_info);
1976 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1977 elf32_mn10300_finish_hash_table_entry,
1978 link_info);
1980 /* All entries in the hash table are fully initialized. */
1981 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1983 /* Now that everything has been initialized, go through each
1984 code section and delete any prologue insns which will be
1985 redundant because their operations will be performed by
1986 a "call" instruction. */
1987 for (input_bfd = link_info->input_bfds;
1988 input_bfd != NULL;
1989 input_bfd = input_bfd->link_next)
1991 /* We're going to need all the local symbols for each bfd. */
1992 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1993 if (symtab_hdr->sh_info != 0)
1995 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1996 if (isymbuf == NULL)
1997 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1998 symtab_hdr->sh_info, 0,
1999 NULL, NULL, NULL);
2000 if (isymbuf == NULL)
2001 goto error_return;
2004 /* Walk over each section in this bfd. */
2005 for (section = input_bfd->sections;
2006 section != NULL;
2007 section = section->next)
2009 unsigned int sec_shndx;
2010 Elf_Internal_Sym *isym, *isymend;
2011 struct elf_link_hash_entry **hashes;
2012 struct elf_link_hash_entry **end_hashes;
2013 unsigned int symcount;
2015 /* Skip non-code sections and empty sections. */
2016 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2017 continue;
2019 if (section->reloc_count != 0)
2021 /* Get a copy of the native relocations. */
2022 internal_relocs = (_bfd_elf_link_read_relocs
2023 (input_bfd, section, (PTR) NULL,
2024 (Elf_Internal_Rela *) NULL,
2025 link_info->keep_memory));
2026 if (internal_relocs == NULL)
2027 goto error_return;
2030 /* Get cached copy of section contents if it exists. */
2031 if (elf_section_data (section)->this_hdr.contents != NULL)
2032 contents = elf_section_data (section)->this_hdr.contents;
2033 else
2035 /* Go get them off disk. */
2036 if (!bfd_malloc_and_get_section (input_bfd, section,
2037 &contents))
2038 goto error_return;
2041 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2042 section);
2044 /* Now look for any function in this section which needs
2045 insns deleted from its prologue. */
2046 isymend = isymbuf + symtab_hdr->sh_info;
2047 for (isym = isymbuf; isym < isymend; isym++)
2049 struct elf32_mn10300_link_hash_entry *sym_hash;
2050 asection *sym_sec = NULL;
2051 const char *sym_name;
2052 char *new_name;
2053 struct elf_link_hash_table *elftab;
2054 bfd_size_type amt;
2056 if (isym->st_shndx != sec_shndx)
2057 continue;
2059 if (isym->st_shndx == SHN_UNDEF)
2060 sym_sec = bfd_und_section_ptr;
2061 else if (isym->st_shndx == SHN_ABS)
2062 sym_sec = bfd_abs_section_ptr;
2063 else if (isym->st_shndx == SHN_COMMON)
2064 sym_sec = bfd_com_section_ptr;
2065 else
2066 sym_sec
2067 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2069 sym_name
2070 = bfd_elf_string_from_elf_section (input_bfd,
2071 symtab_hdr->sh_link,
2072 isym->st_name);
2074 /* Tack on an ID so we can uniquely identify this
2075 local symbol in the global hash table. */
2076 amt = strlen (sym_name) + 10;
2077 new_name = bfd_malloc (amt);
2078 if (new_name == 0)
2079 goto error_return;
2080 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
2081 sym_name = new_name;
2083 elftab = &hash_table->static_hash_table->root;
2084 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
2085 elf_link_hash_lookup (elftab, sym_name,
2086 FALSE, FALSE, FALSE));
2088 free (new_name);
2089 if (sym_hash == NULL)
2090 continue;
2092 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2093 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2095 int bytes = 0;
2097 /* Note that we've changed things. */
2098 elf_section_data (section)->relocs = internal_relocs;
2099 elf_section_data (section)->this_hdr.contents = contents;
2100 symtab_hdr->contents = (unsigned char *) isymbuf;
2102 /* Count how many bytes we're going to delete. */
2103 if (sym_hash->movm_args)
2104 bytes += 2;
2106 if (sym_hash->stack_size > 0)
2108 if (sym_hash->stack_size <= 128)
2109 bytes += 3;
2110 else
2111 bytes += 4;
2114 /* Note that we've deleted prologue bytes for this
2115 function. */
2116 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2118 /* Actually delete the bytes. */
2119 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2120 section,
2121 isym->st_value,
2122 bytes))
2123 goto error_return;
2125 /* Something changed. Not strictly necessary, but
2126 may lead to more relaxing opportunities. */
2127 *again = TRUE;
2131 /* Look for any global functions in this section which
2132 need insns deleted from their prologues. */
2133 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2134 - symtab_hdr->sh_info);
2135 hashes = elf_sym_hashes (input_bfd);
2136 end_hashes = hashes + symcount;
2137 for (; hashes < end_hashes; hashes++)
2139 struct elf32_mn10300_link_hash_entry *sym_hash;
2141 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2142 if ((sym_hash->root.root.type == bfd_link_hash_defined
2143 || sym_hash->root.root.type == bfd_link_hash_defweak)
2144 && sym_hash->root.root.u.def.section == section
2145 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2146 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2148 int bytes = 0;
2149 bfd_vma symval;
2151 /* Note that we've changed things. */
2152 elf_section_data (section)->relocs = internal_relocs;
2153 elf_section_data (section)->this_hdr.contents = contents;
2154 symtab_hdr->contents = (unsigned char *) isymbuf;
2156 /* Count how many bytes we're going to delete. */
2157 if (sym_hash->movm_args)
2158 bytes += 2;
2160 if (sym_hash->stack_size > 0)
2162 if (sym_hash->stack_size <= 128)
2163 bytes += 3;
2164 else
2165 bytes += 4;
2168 /* Note that we've deleted prologue bytes for this
2169 function. */
2170 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2172 /* Actually delete the bytes. */
2173 symval = sym_hash->root.root.u.def.value;
2174 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2175 section,
2176 symval,
2177 bytes))
2178 goto error_return;
2180 /* Something changed. Not strictly necessary, but
2181 may lead to more relaxing opportunities. */
2182 *again = TRUE;
2186 /* Cache or free any memory we allocated for the relocs. */
2187 if (internal_relocs != NULL
2188 && elf_section_data (section)->relocs != internal_relocs)
2189 free (internal_relocs);
2190 internal_relocs = NULL;
2192 /* Cache or free any memory we allocated for the contents. */
2193 if (contents != NULL
2194 && elf_section_data (section)->this_hdr.contents != contents)
2196 if (! link_info->keep_memory)
2197 free (contents);
2198 else
2200 /* Cache the section contents for elf_link_input_bfd. */
2201 elf_section_data (section)->this_hdr.contents = contents;
2204 contents = NULL;
2207 /* Cache or free any memory we allocated for the symbols. */
2208 if (isymbuf != NULL
2209 && symtab_hdr->contents != (unsigned char *) isymbuf)
2211 if (! link_info->keep_memory)
2212 free (isymbuf);
2213 else
2215 /* Cache the symbols for elf_link_input_bfd. */
2216 symtab_hdr->contents = (unsigned char *) isymbuf;
2219 isymbuf = NULL;
2223 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2224 contents = NULL;
2225 internal_relocs = NULL;
2226 isymbuf = NULL;
2227 /* For error_return. */
2228 section = sec;
2230 /* We don't have to do anything for a relocatable link, if
2231 this section does not have relocs, or if this is not a
2232 code section. */
2233 if (link_info->relocatable
2234 || (sec->flags & SEC_RELOC) == 0
2235 || sec->reloc_count == 0
2236 || (sec->flags & SEC_CODE) == 0)
2237 return TRUE;
2239 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2241 /* Get a copy of the native relocations. */
2242 internal_relocs = (_bfd_elf_link_read_relocs
2243 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
2244 link_info->keep_memory));
2245 if (internal_relocs == NULL)
2246 goto error_return;
2248 /* Walk through them looking for relaxing opportunities. */
2249 irelend = internal_relocs + sec->reloc_count;
2250 for (irel = internal_relocs; irel < irelend; irel++)
2252 bfd_vma symval;
2253 struct elf32_mn10300_link_hash_entry *h = NULL;
2255 /* If this isn't something that can be relaxed, then ignore
2256 this reloc. */
2257 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2258 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2259 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2260 continue;
2262 /* Get the section contents if we haven't done so already. */
2263 if (contents == NULL)
2265 /* Get cached copy if it exists. */
2266 if (elf_section_data (sec)->this_hdr.contents != NULL)
2267 contents = elf_section_data (sec)->this_hdr.contents;
2268 else
2270 /* Go get them off disk. */
2271 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2272 goto error_return;
2276 /* Read this BFD's symbols if we haven't done so already. */
2277 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2279 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2280 if (isymbuf == NULL)
2281 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2282 symtab_hdr->sh_info, 0,
2283 NULL, NULL, NULL);
2284 if (isymbuf == NULL)
2285 goto error_return;
2288 /* Get the value of the symbol referred to by the reloc. */
2289 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2291 Elf_Internal_Sym *isym;
2292 asection *sym_sec = NULL;
2293 const char *sym_name;
2294 char *new_name;
2295 bfd_vma saved_addend;
2297 /* A local symbol. */
2298 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2299 if (isym->st_shndx == SHN_UNDEF)
2300 sym_sec = bfd_und_section_ptr;
2301 else if (isym->st_shndx == SHN_ABS)
2302 sym_sec = bfd_abs_section_ptr;
2303 else if (isym->st_shndx == SHN_COMMON)
2304 sym_sec = bfd_com_section_ptr;
2305 else
2306 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2308 sym_name = bfd_elf_string_from_elf_section (abfd,
2309 symtab_hdr->sh_link,
2310 isym->st_name);
2312 if ((sym_sec->flags & SEC_MERGE)
2313 && ELF_ST_TYPE (isym->st_info) == STT_SECTION
2314 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2316 saved_addend = irel->r_addend;
2317 symval = _bfd_elf_rela_local_sym (abfd, isym, &sym_sec, irel);
2318 symval += irel->r_addend;
2319 irel->r_addend = saved_addend;
2321 else
2323 symval = (isym->st_value
2324 + sym_sec->output_section->vma
2325 + sym_sec->output_offset);
2327 /* Tack on an ID so we can uniquely identify this
2328 local symbol in the global hash table. */
2329 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2330 if (new_name == 0)
2331 goto error_return;
2332 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
2333 sym_name = new_name;
2335 h = (struct elf32_mn10300_link_hash_entry *)
2336 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2337 sym_name, FALSE, FALSE, FALSE);
2338 free (new_name);
2340 else
2342 unsigned long indx;
2344 /* An external symbol. */
2345 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2346 h = (struct elf32_mn10300_link_hash_entry *)
2347 (elf_sym_hashes (abfd)[indx]);
2348 BFD_ASSERT (h != NULL);
2349 if (h->root.root.type != bfd_link_hash_defined
2350 && h->root.root.type != bfd_link_hash_defweak)
2352 /* This appears to be a reference to an undefined
2353 symbol. Just ignore it--it will be caught by the
2354 regular reloc processing. */
2355 continue;
2358 symval = (h->root.root.u.def.value
2359 + h->root.root.u.def.section->output_section->vma
2360 + h->root.root.u.def.section->output_offset);
2363 /* For simplicity of coding, we are going to modify the section
2364 contents, the section relocs, and the BFD symbol table. We
2365 must tell the rest of the code not to free up this
2366 information. It would be possible to instead create a table
2367 of changes which have to be made, as is done in coff-mips.c;
2368 that would be more work, but would require less memory when
2369 the linker is run. */
2371 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2372 branch/call, also deal with "call" -> "calls" conversions and
2373 insertion of prologue data into "call" instructions. */
2374 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2375 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2377 bfd_vma value = symval;
2379 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2380 && h != NULL
2381 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2382 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2383 && h->root.plt.offset != (bfd_vma) -1)
2385 asection * splt;
2387 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2388 ->dynobj, ".plt");
2390 value = ((splt->output_section->vma
2391 + splt->output_offset
2392 + h->root.plt.offset)
2393 - (sec->output_section->vma
2394 + sec->output_offset
2395 + irel->r_offset));
2398 /* If we've got a "call" instruction that needs to be turned
2399 into a "calls" instruction, do so now. It saves a byte. */
2400 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2402 unsigned char code;
2404 /* Get the opcode. */
2405 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2407 /* Make sure we're working with a "call" instruction! */
2408 if (code == 0xdd)
2410 /* Note that we've changed the relocs, section contents,
2411 etc. */
2412 elf_section_data (sec)->relocs = internal_relocs;
2413 elf_section_data (sec)->this_hdr.contents = contents;
2414 symtab_hdr->contents = (unsigned char *) isymbuf;
2416 /* Fix the opcode. */
2417 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2418 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2420 /* Fix irel->r_offset and irel->r_addend. */
2421 irel->r_offset += 1;
2422 irel->r_addend += 1;
2424 /* Delete one byte of data. */
2425 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2426 irel->r_offset + 3, 1))
2427 goto error_return;
2429 /* That will change things, so, we should relax again.
2430 Note that this is not required, and it may be slow. */
2431 *again = TRUE;
2434 else if (h)
2436 /* We've got a "call" instruction which needs some data
2437 from target function filled in. */
2438 unsigned char code;
2440 /* Get the opcode. */
2441 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2443 /* Insert data from the target function into the "call"
2444 instruction if needed. */
2445 if (code == 0xdd)
2447 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2448 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2449 contents + irel->r_offset + 5);
2453 /* Deal with pc-relative gunk. */
2454 value -= (sec->output_section->vma + sec->output_offset);
2455 value -= irel->r_offset;
2456 value += irel->r_addend;
2458 /* See if the value will fit in 16 bits, note the high value is
2459 0x7fff + 2 as the target will be two bytes closer if we are
2460 able to relax. */
2461 if ((long) value < 0x8001 && (long) value > -0x8000)
2463 unsigned char code;
2465 /* Get the opcode. */
2466 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2468 if (code != 0xdc && code != 0xdd && code != 0xff)
2469 continue;
2471 /* Note that we've changed the relocs, section contents, etc. */
2472 elf_section_data (sec)->relocs = internal_relocs;
2473 elf_section_data (sec)->this_hdr.contents = contents;
2474 symtab_hdr->contents = (unsigned char *) isymbuf;
2476 /* Fix the opcode. */
2477 if (code == 0xdc)
2478 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2479 else if (code == 0xdd)
2480 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2481 else if (code == 0xff)
2482 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2484 /* Fix the relocation's type. */
2485 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2486 (ELF32_R_TYPE (irel->r_info)
2487 == (int) R_MN10300_PLT32)
2488 ? R_MN10300_PLT16 :
2489 R_MN10300_PCREL16);
2491 /* Delete two bytes of data. */
2492 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2493 irel->r_offset + 1, 2))
2494 goto error_return;
2496 /* That will change things, so, we should relax again.
2497 Note that this is not required, and it may be slow. */
2498 *again = TRUE;
2502 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
2503 branch. */
2504 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
2506 bfd_vma value = symval;
2508 /* If we've got a "call" instruction that needs to be turned
2509 into a "calls" instruction, do so now. It saves a byte. */
2510 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2512 unsigned char code;
2514 /* Get the opcode. */
2515 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2517 /* Make sure we're working with a "call" instruction! */
2518 if (code == 0xcd)
2520 /* Note that we've changed the relocs, section contents,
2521 etc. */
2522 elf_section_data (sec)->relocs = internal_relocs;
2523 elf_section_data (sec)->this_hdr.contents = contents;
2524 symtab_hdr->contents = (unsigned char *) isymbuf;
2526 /* Fix the opcode. */
2527 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
2528 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2530 /* Fix irel->r_offset and irel->r_addend. */
2531 irel->r_offset += 1;
2532 irel->r_addend += 1;
2534 /* Delete one byte of data. */
2535 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2536 irel->r_offset + 1, 1))
2537 goto error_return;
2539 /* That will change things, so, we should relax again.
2540 Note that this is not required, and it may be slow. */
2541 *again = TRUE;
2544 else if (h)
2546 unsigned char code;
2548 /* Get the opcode. */
2549 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2551 /* Insert data from the target function into the "call"
2552 instruction if needed. */
2553 if (code == 0xcd)
2555 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
2556 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2557 contents + irel->r_offset + 3);
2561 /* Deal with pc-relative gunk. */
2562 value -= (sec->output_section->vma + sec->output_offset);
2563 value -= irel->r_offset;
2564 value += irel->r_addend;
2566 /* See if the value will fit in 8 bits, note the high value is
2567 0x7f + 1 as the target will be one bytes closer if we are
2568 able to relax. */
2569 if ((long) value < 0x80 && (long) value > -0x80)
2571 unsigned char code;
2573 /* Get the opcode. */
2574 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2576 if (code != 0xcc)
2577 continue;
2579 /* Note that we've changed the relocs, section contents, etc. */
2580 elf_section_data (sec)->relocs = internal_relocs;
2581 elf_section_data (sec)->this_hdr.contents = contents;
2582 symtab_hdr->contents = (unsigned char *) isymbuf;
2584 /* Fix the opcode. */
2585 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
2587 /* Fix the relocation's type. */
2588 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2589 R_MN10300_PCREL8);
2591 /* Delete one byte of data. */
2592 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2593 irel->r_offset + 1, 1))
2594 goto error_return;
2596 /* That will change things, so, we should relax again.
2597 Note that this is not required, and it may be slow. */
2598 *again = TRUE;
2602 /* Try to eliminate an unconditional 8 bit pc-relative branch
2603 which immediately follows a conditional 8 bit pc-relative
2604 branch around the unconditional branch.
2606 original: new:
2607 bCC lab1 bCC' lab2
2608 bra lab2
2609 lab1: lab1:
2611 This happens when the bCC can't reach lab2 at assembly time,
2612 but due to other relaxations it can reach at link time. */
2613 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
2615 Elf_Internal_Rela *nrel;
2616 bfd_vma value = symval;
2617 unsigned char code;
2619 /* Deal with pc-relative gunk. */
2620 value -= (sec->output_section->vma + sec->output_offset);
2621 value -= irel->r_offset;
2622 value += irel->r_addend;
2624 /* Do nothing if this reloc is the last byte in the section. */
2625 if (irel->r_offset == sec->size)
2626 continue;
2628 /* See if the next instruction is an unconditional pc-relative
2629 branch, more often than not this test will fail, so we
2630 test it first to speed things up. */
2631 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
2632 if (code != 0xca)
2633 continue;
2635 /* Also make sure the next relocation applies to the next
2636 instruction and that it's a pc-relative 8 bit branch. */
2637 nrel = irel + 1;
2638 if (nrel == irelend
2639 || irel->r_offset + 2 != nrel->r_offset
2640 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
2641 continue;
2643 /* Make sure our destination immediately follows the
2644 unconditional branch. */
2645 if (symval != (sec->output_section->vma + sec->output_offset
2646 + irel->r_offset + 3))
2647 continue;
2649 /* Now make sure we are a conditional branch. This may not
2650 be necessary, but why take the chance.
2652 Note these checks assume that R_MN10300_PCREL8 relocs
2653 only occur on bCC and bCCx insns. If they occured
2654 elsewhere, we'd need to know the start of this insn
2655 for this check to be accurate. */
2656 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2657 if (code != 0xc0 && code != 0xc1 && code != 0xc2
2658 && code != 0xc3 && code != 0xc4 && code != 0xc5
2659 && code != 0xc6 && code != 0xc7 && code != 0xc8
2660 && code != 0xc9 && code != 0xe8 && code != 0xe9
2661 && code != 0xea && code != 0xeb)
2662 continue;
2664 /* We also have to be sure there is no symbol/label
2665 at the unconditional branch. */
2666 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
2667 irel->r_offset + 1))
2668 continue;
2670 /* Note that we've changed the relocs, section contents, etc. */
2671 elf_section_data (sec)->relocs = internal_relocs;
2672 elf_section_data (sec)->this_hdr.contents = contents;
2673 symtab_hdr->contents = (unsigned char *) isymbuf;
2675 /* Reverse the condition of the first branch. */
2676 switch (code)
2678 case 0xc8:
2679 code = 0xc9;
2680 break;
2681 case 0xc9:
2682 code = 0xc8;
2683 break;
2684 case 0xc0:
2685 code = 0xc2;
2686 break;
2687 case 0xc2:
2688 code = 0xc0;
2689 break;
2690 case 0xc3:
2691 code = 0xc1;
2692 break;
2693 case 0xc1:
2694 code = 0xc3;
2695 break;
2696 case 0xc4:
2697 code = 0xc6;
2698 break;
2699 case 0xc6:
2700 code = 0xc4;
2701 break;
2702 case 0xc7:
2703 code = 0xc5;
2704 break;
2705 case 0xc5:
2706 code = 0xc7;
2707 break;
2708 case 0xe8:
2709 code = 0xe9;
2710 break;
2711 case 0x9d:
2712 code = 0xe8;
2713 break;
2714 case 0xea:
2715 code = 0xeb;
2716 break;
2717 case 0xeb:
2718 code = 0xea;
2719 break;
2721 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2723 /* Set the reloc type and symbol for the first branch
2724 from the second branch. */
2725 irel->r_info = nrel->r_info;
2727 /* Make the reloc for the second branch a null reloc. */
2728 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
2729 R_MN10300_NONE);
2731 /* Delete two bytes of data. */
2732 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2733 irel->r_offset + 1, 2))
2734 goto error_return;
2736 /* That will change things, so, we should relax again.
2737 Note that this is not required, and it may be slow. */
2738 *again = TRUE;
2741 /* Try to turn a 24 immediate, displacement or absolute address
2742 into a 8 immediate, displacement or absolute address. */
2743 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
2745 bfd_vma value = symval;
2746 value += irel->r_addend;
2748 /* See if the value will fit in 8 bits. */
2749 if ((long) value < 0x7f && (long) value > -0x80)
2751 unsigned char code;
2753 /* AM33 insns which have 24 operands are 6 bytes long and
2754 will have 0xfd as the first byte. */
2756 /* Get the first opcode. */
2757 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2759 if (code == 0xfd)
2761 /* Get the second opcode. */
2762 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2764 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2765 equivalent instructions exists. */
2766 if (code != 0x6b && code != 0x7b
2767 && code != 0x8b && code != 0x9b
2768 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2769 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2770 || (code & 0x0f) == 0x0e))
2772 /* Not safe if the high bit is on as relaxing may
2773 move the value out of high mem and thus not fit
2774 in a signed 8bit value. This is currently over
2775 conservative. */
2776 if ((value & 0x80) == 0)
2778 /* Note that we've changed the relocation contents,
2779 etc. */
2780 elf_section_data (sec)->relocs = internal_relocs;
2781 elf_section_data (sec)->this_hdr.contents = contents;
2782 symtab_hdr->contents = (unsigned char *) isymbuf;
2784 /* Fix the opcode. */
2785 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
2786 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2788 /* Fix the relocation's type. */
2789 irel->r_info =
2790 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2791 R_MN10300_8);
2793 /* Delete two bytes of data. */
2794 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2795 irel->r_offset + 1, 2))
2796 goto error_return;
2798 /* That will change things, so, we should relax
2799 again. Note that this is not required, and it
2800 may be slow. */
2801 *again = TRUE;
2802 break;
2809 /* Try to turn a 32bit immediate, displacement or absolute address
2810 into a 16bit immediate, displacement or absolute address. */
2811 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
2812 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
2813 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32
2814 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2816 bfd_vma value = symval;
2818 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
2820 asection * sgot;
2822 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
2823 ->dynobj, ".got");
2825 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
2827 value = sgot->output_offset;
2829 if (h)
2830 value += h->root.got.offset;
2831 else
2832 value += (elf_local_got_offsets
2833 (abfd)[ELF32_R_SYM (irel->r_info)]);
2835 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
2836 value -= sgot->output_section->vma;
2837 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
2838 value = (sgot->output_section->vma
2839 - (sec->output_section->vma
2840 + sec->output_offset
2841 + irel->r_offset));
2842 else
2843 abort ();
2846 value += irel->r_addend;
2848 /* See if the value will fit in 24 bits.
2849 We allow any 16bit match here. We prune those we can't
2850 handle below. */
2851 if ((long) value < 0x7fffff && (long) value > -0x800000)
2853 unsigned char code;
2855 /* AM33 insns which have 32bit operands are 7 bytes long and
2856 will have 0xfe as the first byte. */
2858 /* Get the first opcode. */
2859 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
2861 if (code == 0xfe)
2863 /* Get the second opcode. */
2864 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2866 /* All the am33 32 -> 24 relaxing possibilities. */
2867 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2868 equivalent instructions exists. */
2869 if (code != 0x6b && code != 0x7b
2870 && code != 0x8b && code != 0x9b
2871 && (ELF32_R_TYPE (irel->r_info)
2872 != (int) R_MN10300_GOTPC32)
2873 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2874 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2875 || (code & 0x0f) == 0x0e))
2877 /* Not safe if the high bit is on as relaxing may
2878 move the value out of high mem and thus not fit
2879 in a signed 16bit value. This is currently over
2880 conservative. */
2881 if ((value & 0x8000) == 0)
2883 /* Note that we've changed the relocation contents,
2884 etc. */
2885 elf_section_data (sec)->relocs = internal_relocs;
2886 elf_section_data (sec)->this_hdr.contents = contents;
2887 symtab_hdr->contents = (unsigned char *) isymbuf;
2889 /* Fix the opcode. */
2890 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2891 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2893 /* Fix the relocation's type. */
2894 irel->r_info =
2895 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2896 (ELF32_R_TYPE (irel->r_info)
2897 == (int) R_MN10300_GOTOFF32)
2898 ? R_MN10300_GOTOFF24
2899 : (ELF32_R_TYPE (irel->r_info)
2900 == (int) R_MN10300_GOT32)
2901 ? R_MN10300_GOT24 :
2902 R_MN10300_24);
2904 /* Delete one byte of data. */
2905 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2906 irel->r_offset + 3, 1))
2907 goto error_return;
2909 /* That will change things, so, we should relax
2910 again. Note that this is not required, and it
2911 may be slow. */
2912 *again = TRUE;
2913 break;
2919 /* See if the value will fit in 16 bits.
2920 We allow any 16bit match here. We prune those we can't
2921 handle below. */
2922 if ((long) value < 0x7fff && (long) value > -0x8000)
2924 unsigned char code;
2926 /* Most insns which have 32bit operands are 6 bytes long;
2927 exceptions are pcrel insns and bit insns.
2929 We handle pcrel insns above. We don't bother trying
2930 to handle the bit insns here.
2932 The first byte of the remaining insns will be 0xfc. */
2934 /* Get the first opcode. */
2935 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2937 if (code != 0xfc)
2938 continue;
2940 /* Get the second opcode. */
2941 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2943 if ((code & 0xf0) < 0x80)
2944 switch (code & 0xf0)
2946 /* mov (d32,am),dn -> mov (d32,am),dn
2947 mov dm,(d32,am) -> mov dn,(d32,am)
2948 mov (d32,am),an -> mov (d32,am),an
2949 mov dm,(d32,am) -> mov dn,(d32,am)
2950 movbu (d32,am),dn -> movbu (d32,am),dn
2951 movbu dm,(d32,am) -> movbu dn,(d32,am)
2952 movhu (d32,am),dn -> movhu (d32,am),dn
2953 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2954 case 0x00:
2955 case 0x10:
2956 case 0x20:
2957 case 0x30:
2958 case 0x40:
2959 case 0x50:
2960 case 0x60:
2961 case 0x70:
2962 /* Not safe if the high bit is on as relaxing may
2963 move the value out of high mem and thus not fit
2964 in a signed 16bit value. */
2965 if (code == 0xcc
2966 && (value & 0x8000))
2967 continue;
2969 /* Note that we've changed the relocation contents, etc. */
2970 elf_section_data (sec)->relocs = internal_relocs;
2971 elf_section_data (sec)->this_hdr.contents = contents;
2972 symtab_hdr->contents = (unsigned char *) isymbuf;
2974 /* Fix the opcode. */
2975 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2976 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2978 /* Fix the relocation's type. */
2979 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2980 (ELF32_R_TYPE (irel->r_info)
2981 == (int) R_MN10300_GOTOFF32)
2982 ? R_MN10300_GOTOFF16
2983 : (ELF32_R_TYPE (irel->r_info)
2984 == (int) R_MN10300_GOT32)
2985 ? R_MN10300_GOT16
2986 : (ELF32_R_TYPE (irel->r_info)
2987 == (int) R_MN10300_GOTPC32)
2988 ? R_MN10300_GOTPC16 :
2989 R_MN10300_16);
2991 /* Delete two bytes of data. */
2992 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2993 irel->r_offset + 2, 2))
2994 goto error_return;
2996 /* That will change things, so, we should relax again.
2997 Note that this is not required, and it may be slow. */
2998 *again = TRUE;
2999 break;
3001 else if ((code & 0xf0) == 0x80
3002 || (code & 0xf0) == 0x90)
3003 switch (code & 0xf3)
3005 /* mov dn,(abs32) -> mov dn,(abs16)
3006 movbu dn,(abs32) -> movbu dn,(abs16)
3007 movhu dn,(abs32) -> movhu dn,(abs16) */
3008 case 0x81:
3009 case 0x82:
3010 case 0x83:
3011 /* Note that we've changed the relocation contents, etc. */
3012 elf_section_data (sec)->relocs = internal_relocs;
3013 elf_section_data (sec)->this_hdr.contents = contents;
3014 symtab_hdr->contents = (unsigned char *) isymbuf;
3016 if ((code & 0xf3) == 0x81)
3017 code = 0x01 + (code & 0x0c);
3018 else if ((code & 0xf3) == 0x82)
3019 code = 0x02 + (code & 0x0c);
3020 else if ((code & 0xf3) == 0x83)
3021 code = 0x03 + (code & 0x0c);
3022 else
3023 abort ();
3025 /* Fix the opcode. */
3026 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3028 /* Fix the relocation's type. */
3029 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3030 (ELF32_R_TYPE (irel->r_info)
3031 == (int) R_MN10300_GOTOFF32)
3032 ? R_MN10300_GOTOFF16
3033 : (ELF32_R_TYPE (irel->r_info)
3034 == (int) R_MN10300_GOT32)
3035 ? R_MN10300_GOT16
3036 : (ELF32_R_TYPE (irel->r_info)
3037 == (int) R_MN10300_GOTPC32)
3038 ? R_MN10300_GOTPC16 :
3039 R_MN10300_16);
3041 /* The opcode got shorter too, so we have to fix the
3042 addend and offset too! */
3043 irel->r_offset -= 1;
3045 /* Delete three bytes of data. */
3046 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3047 irel->r_offset + 1, 3))
3048 goto error_return;
3050 /* That will change things, so, we should relax again.
3051 Note that this is not required, and it may be slow. */
3052 *again = TRUE;
3053 break;
3055 /* mov am,(abs32) -> mov am,(abs16)
3056 mov am,(d32,sp) -> mov am,(d16,sp)
3057 mov dm,(d32,sp) -> mov dm,(d32,sp)
3058 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3059 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3060 case 0x80:
3061 case 0x90:
3062 case 0x91:
3063 case 0x92:
3064 case 0x93:
3065 /* sp-based offsets are zero-extended. */
3066 if (code >= 0x90 && code <= 0x93
3067 && (long)value < 0)
3068 continue;
3070 /* Note that we've changed the relocation contents, etc. */
3071 elf_section_data (sec)->relocs = internal_relocs;
3072 elf_section_data (sec)->this_hdr.contents = contents;
3073 symtab_hdr->contents = (unsigned char *) isymbuf;
3075 /* Fix the opcode. */
3076 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3077 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3079 /* Fix the relocation's type. */
3080 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3081 (ELF32_R_TYPE (irel->r_info)
3082 == (int) R_MN10300_GOTOFF32)
3083 ? R_MN10300_GOTOFF16
3084 : (ELF32_R_TYPE (irel->r_info)
3085 == (int) R_MN10300_GOT32)
3086 ? R_MN10300_GOT16
3087 : (ELF32_R_TYPE (irel->r_info)
3088 == (int) R_MN10300_GOTPC32)
3089 ? R_MN10300_GOTPC16 :
3090 R_MN10300_16);
3092 /* Delete two bytes of data. */
3093 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3094 irel->r_offset + 2, 2))
3095 goto error_return;
3097 /* That will change things, so, we should relax again.
3098 Note that this is not required, and it may be slow. */
3099 *again = TRUE;
3100 break;
3102 else if ((code & 0xf0) < 0xf0)
3103 switch (code & 0xfc)
3105 /* mov imm32,dn -> mov imm16,dn
3106 mov imm32,an -> mov imm16,an
3107 mov (abs32),dn -> mov (abs16),dn
3108 movbu (abs32),dn -> movbu (abs16),dn
3109 movhu (abs32),dn -> movhu (abs16),dn */
3110 case 0xcc:
3111 case 0xdc:
3112 case 0xa4:
3113 case 0xa8:
3114 case 0xac:
3115 /* Not safe if the high bit is on as relaxing may
3116 move the value out of high mem and thus not fit
3117 in a signed 16bit value. */
3118 if (code == 0xcc
3119 && (value & 0x8000))
3120 continue;
3122 /* mov imm16, an zero-extends the immediate. */
3123 if (code == 0xdc
3124 && (long)value < 0)
3125 continue;
3127 /* Note that we've changed the relocation contents, etc. */
3128 elf_section_data (sec)->relocs = internal_relocs;
3129 elf_section_data (sec)->this_hdr.contents = contents;
3130 symtab_hdr->contents = (unsigned char *) isymbuf;
3132 if ((code & 0xfc) == 0xcc)
3133 code = 0x2c + (code & 0x03);
3134 else if ((code & 0xfc) == 0xdc)
3135 code = 0x24 + (code & 0x03);
3136 else if ((code & 0xfc) == 0xa4)
3137 code = 0x30 + (code & 0x03);
3138 else if ((code & 0xfc) == 0xa8)
3139 code = 0x34 + (code & 0x03);
3140 else if ((code & 0xfc) == 0xac)
3141 code = 0x38 + (code & 0x03);
3142 else
3143 abort ();
3145 /* Fix the opcode. */
3146 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3148 /* Fix the relocation's type. */
3149 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3150 (ELF32_R_TYPE (irel->r_info)
3151 == (int) R_MN10300_GOTOFF32)
3152 ? R_MN10300_GOTOFF16
3153 : (ELF32_R_TYPE (irel->r_info)
3154 == (int) R_MN10300_GOT32)
3155 ? R_MN10300_GOT16
3156 : (ELF32_R_TYPE (irel->r_info)
3157 == (int) R_MN10300_GOTPC32)
3158 ? R_MN10300_GOTPC16 :
3159 R_MN10300_16);
3161 /* The opcode got shorter too, so we have to fix the
3162 addend and offset too! */
3163 irel->r_offset -= 1;
3165 /* Delete three bytes of data. */
3166 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3167 irel->r_offset + 1, 3))
3168 goto error_return;
3170 /* That will change things, so, we should relax again.
3171 Note that this is not required, and it may be slow. */
3172 *again = TRUE;
3173 break;
3175 /* mov (abs32),an -> mov (abs16),an
3176 mov (d32,sp),an -> mov (d16,sp),an
3177 mov (d32,sp),dn -> mov (d16,sp),dn
3178 movbu (d32,sp),dn -> movbu (d16,sp),dn
3179 movhu (d32,sp),dn -> movhu (d16,sp),dn
3180 add imm32,dn -> add imm16,dn
3181 cmp imm32,dn -> cmp imm16,dn
3182 add imm32,an -> add imm16,an
3183 cmp imm32,an -> cmp imm16,an
3184 and imm32,dn -> and imm16,dn
3185 or imm32,dn -> or imm16,dn
3186 xor imm32,dn -> xor imm16,dn
3187 btst imm32,dn -> btst imm16,dn */
3189 case 0xa0:
3190 case 0xb0:
3191 case 0xb1:
3192 case 0xb2:
3193 case 0xb3:
3194 case 0xc0:
3195 case 0xc8:
3197 case 0xd0:
3198 case 0xd8:
3199 case 0xe0:
3200 case 0xe1:
3201 case 0xe2:
3202 case 0xe3:
3203 /* cmp imm16, an zero-extends the immediate. */
3204 if (code == 0xdc
3205 && (long)value < 0)
3206 continue;
3208 /* So do sp-based offsets. */
3209 if (code >= 0xb0 && code <= 0xb3
3210 && (long)value < 0)
3211 continue;
3213 /* Note that we've changed the relocation contents, etc. */
3214 elf_section_data (sec)->relocs = internal_relocs;
3215 elf_section_data (sec)->this_hdr.contents = contents;
3216 symtab_hdr->contents = (unsigned char *) isymbuf;
3218 /* Fix the opcode. */
3219 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3220 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3222 /* Fix the relocation's type. */
3223 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3224 (ELF32_R_TYPE (irel->r_info)
3225 == (int) R_MN10300_GOTOFF32)
3226 ? R_MN10300_GOTOFF16
3227 : (ELF32_R_TYPE (irel->r_info)
3228 == (int) R_MN10300_GOT32)
3229 ? R_MN10300_GOT16
3230 : (ELF32_R_TYPE (irel->r_info)
3231 == (int) R_MN10300_GOTPC32)
3232 ? R_MN10300_GOTPC16 :
3233 R_MN10300_16);
3235 /* Delete two bytes of data. */
3236 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3237 irel->r_offset + 2, 2))
3238 goto error_return;
3240 /* That will change things, so, we should relax again.
3241 Note that this is not required, and it may be slow. */
3242 *again = TRUE;
3243 break;
3245 else if (code == 0xfe)
3247 /* add imm32,sp -> add imm16,sp */
3249 /* Note that we've changed the relocation contents, etc. */
3250 elf_section_data (sec)->relocs = internal_relocs;
3251 elf_section_data (sec)->this_hdr.contents = contents;
3252 symtab_hdr->contents = (unsigned char *) isymbuf;
3254 /* Fix the opcode. */
3255 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3256 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3258 /* Fix the relocation's type. */
3259 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3260 (ELF32_R_TYPE (irel->r_info)
3261 == (int) R_MN10300_GOT32)
3262 ? R_MN10300_GOT16
3263 : (ELF32_R_TYPE (irel->r_info)
3264 == (int) R_MN10300_GOTOFF32)
3265 ? R_MN10300_GOTOFF16
3266 : (ELF32_R_TYPE (irel->r_info)
3267 == (int) R_MN10300_GOTPC32)
3268 ? R_MN10300_GOTPC16 :
3269 R_MN10300_16);
3271 /* Delete two bytes of data. */
3272 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3273 irel->r_offset + 2, 2))
3274 goto error_return;
3276 /* That will change things, so, we should relax again.
3277 Note that this is not required, and it may be slow. */
3278 *again = TRUE;
3279 break;
3285 if (isymbuf != NULL
3286 && symtab_hdr->contents != (unsigned char *) isymbuf)
3288 if (! link_info->keep_memory)
3289 free (isymbuf);
3290 else
3292 /* Cache the symbols for elf_link_input_bfd. */
3293 symtab_hdr->contents = (unsigned char *) isymbuf;
3297 if (contents != NULL
3298 && elf_section_data (sec)->this_hdr.contents != contents)
3300 if (! link_info->keep_memory)
3301 free (contents);
3302 else
3304 /* Cache the section contents for elf_link_input_bfd. */
3305 elf_section_data (sec)->this_hdr.contents = contents;
3309 if (internal_relocs != NULL
3310 && elf_section_data (sec)->relocs != internal_relocs)
3311 free (internal_relocs);
3313 return TRUE;
3315 error_return:
3316 if (isymbuf != NULL
3317 && symtab_hdr->contents != (unsigned char *) isymbuf)
3318 free (isymbuf);
3319 if (contents != NULL
3320 && elf_section_data (section)->this_hdr.contents != contents)
3321 free (contents);
3322 if (internal_relocs != NULL
3323 && elf_section_data (section)->relocs != internal_relocs)
3324 free (internal_relocs);
3326 return FALSE;
3329 /* Compute the stack size and movm arguments for the function
3330 referred to by HASH at address ADDR in section with
3331 contents CONTENTS, store the information in the hash table. */
3332 static void
3333 compute_function_info (abfd, hash, addr, contents)
3334 bfd *abfd;
3335 struct elf32_mn10300_link_hash_entry *hash;
3336 bfd_vma addr;
3337 unsigned char *contents;
3339 unsigned char byte1, byte2;
3340 /* We only care about a very small subset of the possible prologue
3341 sequences here. Basically we look for:
3343 movm [d2,d3,a2,a3],sp (optional)
3344 add <size>,sp (optional, and only for sizes which fit in an unsigned
3345 8 bit number)
3347 If we find anything else, we quit. */
3349 /* Look for movm [regs],sp */
3350 byte1 = bfd_get_8 (abfd, contents + addr);
3351 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3353 if (byte1 == 0xcf)
3355 hash->movm_args = byte2;
3356 addr += 2;
3357 byte1 = bfd_get_8 (abfd, contents + addr);
3358 byte2 = bfd_get_8 (abfd, contents + addr + 1);
3361 /* Now figure out how much stack space will be allocated by the movm
3362 instruction. We need this kept separate from the function's normal
3363 stack space. */
3364 if (hash->movm_args)
3366 /* Space for d2. */
3367 if (hash->movm_args & 0x80)
3368 hash->movm_stack_size += 4;
3370 /* Space for d3. */
3371 if (hash->movm_args & 0x40)
3372 hash->movm_stack_size += 4;
3374 /* Space for a2. */
3375 if (hash->movm_args & 0x20)
3376 hash->movm_stack_size += 4;
3378 /* Space for a3. */
3379 if (hash->movm_args & 0x10)
3380 hash->movm_stack_size += 4;
3382 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
3383 if (hash->movm_args & 0x08)
3384 hash->movm_stack_size += 8 * 4;
3386 if (bfd_get_mach (abfd) == bfd_mach_am33
3387 || bfd_get_mach (abfd) == bfd_mach_am33_2)
3389 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
3390 if (hash->movm_args & 0x1)
3391 hash->movm_stack_size += 6 * 4;
3393 /* exreg1 space. e4, e5, e6, e7 */
3394 if (hash->movm_args & 0x2)
3395 hash->movm_stack_size += 4 * 4;
3397 /* exreg0 space. e2, e3 */
3398 if (hash->movm_args & 0x4)
3399 hash->movm_stack_size += 2 * 4;
3403 /* Now look for the two stack adjustment variants. */
3404 if (byte1 == 0xf8 && byte2 == 0xfe)
3406 int temp = bfd_get_8 (abfd, contents + addr + 2);
3407 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
3409 hash->stack_size = -temp;
3411 else if (byte1 == 0xfa && byte2 == 0xfe)
3413 int temp = bfd_get_16 (abfd, contents + addr + 2);
3414 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
3415 temp = -temp;
3417 if (temp < 255)
3418 hash->stack_size = temp;
3421 /* If the total stack to be allocated by the call instruction is more
3422 than 255 bytes, then we can't remove the stack adjustment by using
3423 "call" (we might still be able to remove the "movm" instruction. */
3424 if (hash->stack_size + hash->movm_stack_size > 255)
3425 hash->stack_size = 0;
3427 return;
3430 /* Delete some bytes from a section while relaxing. */
3432 static bfd_boolean
3433 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
3434 bfd *abfd;
3435 asection *sec;
3436 bfd_vma addr;
3437 int count;
3439 Elf_Internal_Shdr *symtab_hdr;
3440 unsigned int sec_shndx;
3441 bfd_byte *contents;
3442 Elf_Internal_Rela *irel, *irelend;
3443 Elf_Internal_Rela *irelalign;
3444 bfd_vma toaddr;
3445 Elf_Internal_Sym *isym, *isymend;
3446 struct elf_link_hash_entry **sym_hashes;
3447 struct elf_link_hash_entry **end_hashes;
3448 unsigned int symcount;
3450 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3452 contents = elf_section_data (sec)->this_hdr.contents;
3454 /* The deletion must stop at the next ALIGN reloc for an aligment
3455 power larger than the number of bytes we are deleting. */
3457 irelalign = NULL;
3458 toaddr = sec->size;
3460 irel = elf_section_data (sec)->relocs;
3461 irelend = irel + sec->reloc_count;
3463 /* Actually delete the bytes. */
3464 memmove (contents + addr, contents + addr + count,
3465 (size_t) (toaddr - addr - count));
3466 sec->size -= count;
3468 /* Adjust all the relocs. */
3469 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
3471 /* Get the new reloc address. */
3472 if ((irel->r_offset > addr
3473 && irel->r_offset < toaddr))
3474 irel->r_offset -= count;
3477 /* Adjust the local symbols defined in this section. */
3478 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3479 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
3480 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3482 if (isym->st_shndx == sec_shndx
3483 && isym->st_value > addr
3484 && isym->st_value < toaddr)
3485 isym->st_value -= count;
3488 /* Now adjust the global symbols defined in this section. */
3489 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3490 - symtab_hdr->sh_info);
3491 sym_hashes = elf_sym_hashes (abfd);
3492 end_hashes = sym_hashes + symcount;
3493 for (; sym_hashes < end_hashes; sym_hashes++)
3495 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3496 if ((sym_hash->root.type == bfd_link_hash_defined
3497 || sym_hash->root.type == bfd_link_hash_defweak)
3498 && sym_hash->root.u.def.section == sec
3499 && sym_hash->root.u.def.value > addr
3500 && sym_hash->root.u.def.value < toaddr)
3502 sym_hash->root.u.def.value -= count;
3506 return TRUE;
3509 /* Return TRUE if a symbol exists at the given address, else return
3510 FALSE. */
3511 static bfd_boolean
3512 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
3513 bfd *abfd;
3514 asection *sec;
3515 Elf_Internal_Sym *isym;
3516 bfd_vma addr;
3518 Elf_Internal_Shdr *symtab_hdr;
3519 unsigned int sec_shndx;
3520 Elf_Internal_Sym *isymend;
3521 struct elf_link_hash_entry **sym_hashes;
3522 struct elf_link_hash_entry **end_hashes;
3523 unsigned int symcount;
3525 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
3527 /* Examine all the symbols. */
3528 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3529 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
3531 if (isym->st_shndx == sec_shndx
3532 && isym->st_value == addr)
3533 return TRUE;
3536 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3537 - symtab_hdr->sh_info);
3538 sym_hashes = elf_sym_hashes (abfd);
3539 end_hashes = sym_hashes + symcount;
3540 for (; sym_hashes < end_hashes; sym_hashes++)
3542 struct elf_link_hash_entry *sym_hash = *sym_hashes;
3543 if ((sym_hash->root.type == bfd_link_hash_defined
3544 || sym_hash->root.type == bfd_link_hash_defweak)
3545 && sym_hash->root.u.def.section == sec
3546 && sym_hash->root.u.def.value == addr)
3547 return TRUE;
3550 return FALSE;
3553 /* This is a version of bfd_generic_get_relocated_section_contents
3554 which uses mn10300_elf_relocate_section. */
3556 static bfd_byte *
3557 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
3558 data, relocatable, symbols)
3559 bfd *output_bfd;
3560 struct bfd_link_info *link_info;
3561 struct bfd_link_order *link_order;
3562 bfd_byte *data;
3563 bfd_boolean relocatable;
3564 asymbol **symbols;
3566 Elf_Internal_Shdr *symtab_hdr;
3567 asection *input_section = link_order->u.indirect.section;
3568 bfd *input_bfd = input_section->owner;
3569 asection **sections = NULL;
3570 Elf_Internal_Rela *internal_relocs = NULL;
3571 Elf_Internal_Sym *isymbuf = NULL;
3573 /* We only need to handle the case of relaxing, or of having a
3574 particular set of section contents, specially. */
3575 if (relocatable
3576 || elf_section_data (input_section)->this_hdr.contents == NULL)
3577 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3578 link_order, data,
3579 relocatable,
3580 symbols);
3582 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3584 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3585 (size_t) input_section->size);
3587 if ((input_section->flags & SEC_RELOC) != 0
3588 && input_section->reloc_count > 0)
3590 asection **secpp;
3591 Elf_Internal_Sym *isym, *isymend;
3592 bfd_size_type amt;
3594 internal_relocs = (_bfd_elf_link_read_relocs
3595 (input_bfd, input_section, (PTR) NULL,
3596 (Elf_Internal_Rela *) NULL, FALSE));
3597 if (internal_relocs == NULL)
3598 goto error_return;
3600 if (symtab_hdr->sh_info != 0)
3602 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3603 if (isymbuf == NULL)
3604 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3605 symtab_hdr->sh_info, 0,
3606 NULL, NULL, NULL);
3607 if (isymbuf == NULL)
3608 goto error_return;
3611 amt = symtab_hdr->sh_info;
3612 amt *= sizeof (asection *);
3613 sections = (asection **) bfd_malloc (amt);
3614 if (sections == NULL && amt != 0)
3615 goto error_return;
3617 isymend = isymbuf + symtab_hdr->sh_info;
3618 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3620 asection *isec;
3622 if (isym->st_shndx == SHN_UNDEF)
3623 isec = bfd_und_section_ptr;
3624 else if (isym->st_shndx == SHN_ABS)
3625 isec = bfd_abs_section_ptr;
3626 else if (isym->st_shndx == SHN_COMMON)
3627 isec = bfd_com_section_ptr;
3628 else
3629 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3631 *secpp = isec;
3634 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3635 input_section, data, internal_relocs,
3636 isymbuf, sections))
3637 goto error_return;
3639 if (sections != NULL)
3640 free (sections);
3641 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3642 free (isymbuf);
3643 if (internal_relocs != elf_section_data (input_section)->relocs)
3644 free (internal_relocs);
3647 return data;
3649 error_return:
3650 if (sections != NULL)
3651 free (sections);
3652 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3653 free (isymbuf);
3654 if (internal_relocs != NULL
3655 && internal_relocs != elf_section_data (input_section)->relocs)
3656 free (internal_relocs);
3657 return NULL;
3660 /* Assorted hash table functions. */
3662 /* Initialize an entry in the link hash table. */
3664 /* Create an entry in an MN10300 ELF linker hash table. */
3666 static struct bfd_hash_entry *
3667 elf32_mn10300_link_hash_newfunc (entry, table, string)
3668 struct bfd_hash_entry *entry;
3669 struct bfd_hash_table *table;
3670 const char *string;
3672 struct elf32_mn10300_link_hash_entry *ret =
3673 (struct elf32_mn10300_link_hash_entry *) entry;
3675 /* Allocate the structure if it has not already been allocated by a
3676 subclass. */
3677 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3678 ret = ((struct elf32_mn10300_link_hash_entry *)
3679 bfd_hash_allocate (table,
3680 sizeof (struct elf32_mn10300_link_hash_entry)));
3681 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
3682 return (struct bfd_hash_entry *) ret;
3684 /* Call the allocation method of the superclass. */
3685 ret = ((struct elf32_mn10300_link_hash_entry *)
3686 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3687 table, string));
3688 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
3690 ret->direct_calls = 0;
3691 ret->stack_size = 0;
3692 ret->movm_args = 0;
3693 ret->movm_stack_size = 0;
3694 ret->flags = 0;
3697 return (struct bfd_hash_entry *) ret;
3700 /* Create an mn10300 ELF linker hash table. */
3702 static struct bfd_link_hash_table *
3703 elf32_mn10300_link_hash_table_create (abfd)
3704 bfd *abfd;
3706 struct elf32_mn10300_link_hash_table *ret;
3707 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
3709 ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3710 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
3711 return NULL;
3713 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
3714 elf32_mn10300_link_hash_newfunc))
3716 free (ret);
3717 return NULL;
3720 ret->flags = 0;
3721 amt = sizeof (struct elf_link_hash_table);
3722 ret->static_hash_table
3723 = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
3724 if (ret->static_hash_table == NULL)
3726 free (ret);
3727 return NULL;
3730 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
3731 elf32_mn10300_link_hash_newfunc))
3733 free (ret->static_hash_table);
3734 free (ret);
3735 return NULL;
3737 return &ret->root.root;
3740 /* Free an mn10300 ELF linker hash table. */
3742 static void
3743 elf32_mn10300_link_hash_table_free (hash)
3744 struct bfd_link_hash_table *hash;
3746 struct elf32_mn10300_link_hash_table *ret
3747 = (struct elf32_mn10300_link_hash_table *) hash;
3749 _bfd_generic_link_hash_table_free
3750 ((struct bfd_link_hash_table *) ret->static_hash_table);
3751 _bfd_generic_link_hash_table_free
3752 ((struct bfd_link_hash_table *) ret);
3755 static unsigned long
3756 elf_mn10300_mach (flags)
3757 flagword flags;
3759 switch (flags & EF_MN10300_MACH)
3761 case E_MN10300_MACH_MN10300:
3762 default:
3763 return bfd_mach_mn10300;
3765 case E_MN10300_MACH_AM33:
3766 return bfd_mach_am33;
3768 case E_MN10300_MACH_AM33_2:
3769 return bfd_mach_am33_2;
3773 /* The final processing done just before writing out a MN10300 ELF object
3774 file. This gets the MN10300 architecture right based on the machine
3775 number. */
3777 void
3778 _bfd_mn10300_elf_final_write_processing (abfd, linker)
3779 bfd *abfd;
3780 bfd_boolean linker ATTRIBUTE_UNUSED;
3782 unsigned long val;
3784 switch (bfd_get_mach (abfd))
3786 default:
3787 case bfd_mach_mn10300:
3788 val = E_MN10300_MACH_MN10300;
3789 break;
3791 case bfd_mach_am33:
3792 val = E_MN10300_MACH_AM33;
3793 break;
3795 case bfd_mach_am33_2:
3796 val = E_MN10300_MACH_AM33_2;
3797 break;
3800 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
3801 elf_elfheader (abfd)->e_flags |= val;
3804 bfd_boolean
3805 _bfd_mn10300_elf_object_p (abfd)
3806 bfd *abfd;
3808 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
3809 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
3810 return TRUE;
3813 /* Merge backend specific data from an object file to the output
3814 object file when linking. */
3816 bfd_boolean
3817 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
3818 bfd *ibfd;
3819 bfd *obfd;
3821 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
3822 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
3823 return TRUE;
3825 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
3826 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
3828 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
3829 bfd_get_mach (ibfd)))
3830 return FALSE;
3833 return TRUE;
3836 #define PLT0_ENTRY_SIZE 15
3837 #define PLT_ENTRY_SIZE 20
3838 #define PIC_PLT_ENTRY_SIZE 24
3840 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
3842 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
3843 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
3844 0xf0, 0xf4, /* jmp (a0) */
3847 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
3849 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
3850 0xf0, 0xf4, /* jmp (a0) */
3851 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3852 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
3855 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
3857 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
3858 0xf0, 0xf4, /* jmp (a0) */
3859 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
3860 0xf8, 0x22, 8, /* mov (8,a2),a0 */
3861 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
3862 0xf0, 0xf4, /* jmp (a0) */
3865 /* Return size of the first PLT entry. */
3866 #define elf_mn10300_sizeof_plt0(info) \
3867 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
3869 /* Return size of a PLT entry. */
3870 #define elf_mn10300_sizeof_plt(info) \
3871 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
3873 /* Return offset of the PLT0 address in an absolute PLT entry. */
3874 #define elf_mn10300_plt_plt0_offset(info) 16
3876 /* Return offset of the linker in PLT0 entry. */
3877 #define elf_mn10300_plt0_linker_offset(info) 2
3879 /* Return offset of the GOT id in PLT0 entry. */
3880 #define elf_mn10300_plt0_gotid_offset(info) 9
3882 /* Return offset of the temporary in PLT entry */
3883 #define elf_mn10300_plt_temp_offset(info) 8
3885 /* Return offset of the symbol in PLT entry. */
3886 #define elf_mn10300_plt_symbol_offset(info) 2
3888 /* Return offset of the relocation in PLT entry. */
3889 #define elf_mn10300_plt_reloc_offset(info) 11
3891 /* The name of the dynamic interpreter. This is put in the .interp
3892 section. */
3894 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
3896 /* Create dynamic sections when linking against a dynamic object. */
3898 static bfd_boolean
3899 _bfd_mn10300_elf_create_dynamic_sections (abfd, info)
3900 bfd *abfd;
3901 struct bfd_link_info *info;
3903 flagword flags;
3904 asection * s;
3905 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
3906 int ptralign = 0;
3908 switch (bed->s->arch_size)
3910 case 32:
3911 ptralign = 2;
3912 break;
3914 case 64:
3915 ptralign = 3;
3916 break;
3918 default:
3919 bfd_set_error (bfd_error_bad_value);
3920 return FALSE;
3923 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3924 .rel[a].bss sections. */
3926 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3927 | SEC_LINKER_CREATED);
3929 s = bfd_make_section (abfd,
3930 bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3931 if (s == NULL
3932 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3933 || ! bfd_set_section_alignment (abfd, s, ptralign))
3934 return FALSE;
3936 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
3937 return FALSE;
3940 const char * secname;
3941 char * relname;
3942 flagword secflags;
3943 asection * sec;
3945 for (sec = abfd->sections; sec; sec = sec->next)
3947 secflags = bfd_get_section_flags (abfd, sec);
3948 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3949 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3950 continue;
3952 secname = bfd_get_section_name (abfd, sec);
3953 relname = (char *) bfd_malloc (strlen (secname) + 6);
3954 strcpy (relname, ".rela");
3955 strcat (relname, secname);
3957 s = bfd_make_section (abfd, relname);
3958 if (s == NULL
3959 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3960 || ! bfd_set_section_alignment (abfd, s, ptralign))
3961 return FALSE;
3965 if (bed->want_dynbss)
3967 /* The .dynbss section is a place to put symbols which are defined
3968 by dynamic objects, are referenced by regular objects, and are
3969 not functions. We must allocate space for them in the process
3970 image and use a R_*_COPY reloc to tell the dynamic linker to
3971 initialize them at run time. The linker script puts the .dynbss
3972 section into the .bss section of the final image. */
3973 s = bfd_make_section (abfd, ".dynbss");
3974 if (s == NULL
3975 || ! bfd_set_section_flags (abfd, s, SEC_ALLOC))
3976 return FALSE;
3978 /* The .rel[a].bss section holds copy relocs. This section is not
3979 normally needed. We need to create it here, though, so that the
3980 linker will map it to an output section. We can't just create it
3981 only if we need it, because we will not know whether we need it
3982 until we have seen all the input files, and the first time the
3983 main linker code calls BFD after examining all the input files
3984 (size_dynamic_sections) the input sections have already been
3985 mapped to the output sections. If the section turns out not to
3986 be needed, we can discard it later. We will never need this
3987 section when generating a shared object, since they do not use
3988 copy relocs. */
3989 if (! info->shared)
3991 s = bfd_make_section (abfd,
3992 (bed->default_use_rela_p
3993 ? ".rela.bss" : ".rel.bss"));
3994 if (s == NULL
3995 || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY)
3996 || ! bfd_set_section_alignment (abfd, s, ptralign))
3997 return FALSE;
4001 return TRUE;
4004 /* Adjust a symbol defined by a dynamic object and referenced by a
4005 regular object. The current definition is in some section of the
4006 dynamic object, but we're not including those sections. We have to
4007 change the definition to something the rest of the link can
4008 understand. */
4010 static bfd_boolean
4011 _bfd_mn10300_elf_adjust_dynamic_symbol (info, h)
4012 struct bfd_link_info * info;
4013 struct elf_link_hash_entry * h;
4015 bfd * dynobj;
4016 asection * s;
4017 unsigned int power_of_two;
4019 dynobj = elf_hash_table (info)->dynobj;
4021 /* Make sure we know what is going on here. */
4022 BFD_ASSERT (dynobj != NULL
4023 && (h->needs_plt
4024 || h->weakdef != NULL
4025 || (h->def_dynamic
4026 && h->ref_regular
4027 && !h->def_regular)));
4029 /* If this is a function, put it in the procedure linkage table. We
4030 will fill in the contents of the procedure linkage table later,
4031 when we know the address of the .got section. */
4032 if (h->type == STT_FUNC
4033 || h->needs_plt)
4035 if (! info->shared
4036 && !h->def_dynamic
4037 && !h->ref_dynamic)
4039 /* This case can occur if we saw a PLT reloc in an input
4040 file, but the symbol was never referred to by a dynamic
4041 object. In such a case, we don't actually need to build
4042 a procedure linkage table, and we can just do a REL32
4043 reloc instead. */
4044 BFD_ASSERT (h->needs_plt);
4045 return TRUE;
4048 /* Make sure this symbol is output as a dynamic symbol. */
4049 if (h->dynindx == -1)
4051 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4052 return FALSE;
4055 s = bfd_get_section_by_name (dynobj, ".plt");
4056 BFD_ASSERT (s != NULL);
4058 /* If this is the first .plt entry, make room for the special
4059 first entry. */
4060 if (s->size == 0)
4061 s->size += elf_mn10300_sizeof_plt0 (info);
4063 /* If this symbol is not defined in a regular file, and we are
4064 not generating a shared library, then set the symbol to this
4065 location in the .plt. This is required to make function
4066 pointers compare as equal between the normal executable and
4067 the shared library. */
4068 if (! info->shared
4069 && !h->def_regular)
4071 h->root.u.def.section = s;
4072 h->root.u.def.value = s->size;
4075 h->plt.offset = s->size;
4077 /* Make room for this entry. */
4078 s->size += elf_mn10300_sizeof_plt (info);
4080 /* We also need to make an entry in the .got.plt section, which
4081 will be placed in the .got section by the linker script. */
4083 s = bfd_get_section_by_name (dynobj, ".got.plt");
4084 BFD_ASSERT (s != NULL);
4085 s->size += 4;
4087 /* We also need to make an entry in the .rela.plt section. */
4089 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4090 BFD_ASSERT (s != NULL);
4091 s->size += sizeof (Elf32_External_Rela);
4093 return TRUE;
4096 /* If this is a weak symbol, and there is a real definition, the
4097 processor independent code will have arranged for us to see the
4098 real definition first, and we can just use the same value. */
4099 if (h->weakdef != NULL)
4101 BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined
4102 || h->weakdef->root.type == bfd_link_hash_defweak);
4103 h->root.u.def.section = h->weakdef->root.u.def.section;
4104 h->root.u.def.value = h->weakdef->root.u.def.value;
4105 return TRUE;
4108 /* This is a reference to a symbol defined by a dynamic object which
4109 is not a function. */
4111 /* If we are creating a shared library, we must presume that the
4112 only references to the symbol are via the global offset table.
4113 For such cases we need not do anything here; the relocations will
4114 be handled correctly by relocate_section. */
4115 if (info->shared)
4116 return TRUE;
4118 /* If there are no references to this symbol that do not use the
4119 GOT, we don't need to generate a copy reloc. */
4120 if (!h->non_got_ref)
4121 return TRUE;
4123 /* We must allocate the symbol in our .dynbss section, which will
4124 become part of the .bss section of the executable. There will be
4125 an entry for this symbol in the .dynsym section. The dynamic
4126 object will contain position independent code, so all references
4127 from the dynamic object to this symbol will go through the global
4128 offset table. The dynamic linker will use the .dynsym entry to
4129 determine the address it must put in the global offset table, so
4130 both the dynamic object and the regular object will refer to the
4131 same memory location for the variable. */
4133 s = bfd_get_section_by_name (dynobj, ".dynbss");
4134 BFD_ASSERT (s != NULL);
4136 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4137 copy the initial value out of the dynamic object and into the
4138 runtime process image. We need to remember the offset into the
4139 .rela.bss section we are going to use. */
4140 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4142 asection * srel;
4144 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4145 BFD_ASSERT (srel != NULL);
4146 srel->size += sizeof (Elf32_External_Rela);
4147 h->needs_copy = 1;
4150 /* We need to figure out the alignment required for this symbol. I
4151 have no idea how ELF linkers handle this. */
4152 power_of_two = bfd_log2 (h->size);
4153 if (power_of_two > 3)
4154 power_of_two = 3;
4156 /* Apply the required alignment. */
4157 s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4158 if (power_of_two > bfd_get_section_alignment (dynobj, s))
4160 if (! bfd_set_section_alignment (dynobj, s, power_of_two))
4161 return FALSE;
4164 /* Define the symbol as being at this point in the section. */
4165 h->root.u.def.section = s;
4166 h->root.u.def.value = s->size;
4168 /* Increment the section size to make room for the symbol. */
4169 s->size += h->size;
4171 return TRUE;
4174 /* Set the sizes of the dynamic sections. */
4176 static bfd_boolean
4177 _bfd_mn10300_elf_size_dynamic_sections (output_bfd, info)
4178 bfd * output_bfd;
4179 struct bfd_link_info * info;
4181 bfd * dynobj;
4182 asection * s;
4183 bfd_boolean plt;
4184 bfd_boolean relocs;
4185 bfd_boolean reltext;
4187 dynobj = elf_hash_table (info)->dynobj;
4188 BFD_ASSERT (dynobj != NULL);
4190 if (elf_hash_table (info)->dynamic_sections_created)
4192 /* Set the contents of the .interp section to the interpreter. */
4193 if (info->executable)
4195 s = bfd_get_section_by_name (dynobj, ".interp");
4196 BFD_ASSERT (s != NULL);
4197 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4198 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4201 else
4203 /* We may have created entries in the .rela.got section.
4204 However, if we are not creating the dynamic sections, we will
4205 not actually use these entries. Reset the size of .rela.got,
4206 which will cause it to get stripped from the output file
4207 below. */
4208 s = bfd_get_section_by_name (dynobj, ".rela.got");
4209 if (s != NULL)
4210 s->size = 0;
4213 /* The check_relocs and adjust_dynamic_symbol entry points have
4214 determined the sizes of the various dynamic sections. Allocate
4215 memory for them. */
4216 plt = FALSE;
4217 relocs = FALSE;
4218 reltext = FALSE;
4219 for (s = dynobj->sections; s != NULL; s = s->next)
4221 const char * name;
4222 bfd_boolean strip;
4224 if ((s->flags & SEC_LINKER_CREATED) == 0)
4225 continue;
4227 /* It's OK to base decisions on the section name, because none
4228 of the dynobj section names depend upon the input files. */
4229 name = bfd_get_section_name (dynobj, s);
4231 strip = FALSE;
4233 if (strcmp (name, ".plt") == 0)
4235 if (s->size == 0)
4236 /* Strip this section if we don't need it; see the
4237 comment below. */
4238 strip = TRUE;
4239 else
4240 /* Remember whether there is a PLT. */
4241 plt = TRUE;
4243 else if (strncmp (name, ".rela", 5) == 0)
4245 if (s->size == 0)
4247 /* If we don't need this section, strip it from the
4248 output file. This is mostly to handle .rela.bss and
4249 .rela.plt. We must create both sections in
4250 create_dynamic_sections, because they must be created
4251 before the linker maps input sections to output
4252 sections. The linker does that before
4253 adjust_dynamic_symbol is called, and it is that
4254 function which decides whether anything needs to go
4255 into these sections. */
4256 strip = TRUE;
4258 else
4260 asection * target;
4262 /* Remember whether there are any reloc sections other
4263 than .rela.plt. */
4264 if (strcmp (name, ".rela.plt") != 0)
4266 const char * outname;
4268 relocs = TRUE;
4270 /* If this relocation section applies to a read only
4271 section, then we probably need a DT_TEXTREL
4272 entry. The entries in the .rela.plt section
4273 really apply to the .got section, which we
4274 created ourselves and so know is not readonly. */
4275 outname = bfd_get_section_name (output_bfd,
4276 s->output_section);
4277 target = bfd_get_section_by_name (output_bfd, outname + 5);
4278 if (target != NULL
4279 && (target->flags & SEC_READONLY) != 0
4280 && (target->flags & SEC_ALLOC) != 0)
4281 reltext = TRUE;
4284 /* We use the reloc_count field as a counter if we need
4285 to copy relocs into the output file. */
4286 s->reloc_count = 0;
4289 else if (strncmp (name, ".got", 4) != 0)
4290 /* It's not one of our sections, so don't allocate space. */
4291 continue;
4293 if (strip)
4295 _bfd_strip_section_from_output (info, s);
4296 continue;
4299 /* Allocate memory for the section contents. We use bfd_zalloc
4300 here in case unused entries are not reclaimed before the
4301 section's contents are written out. This should not happen,
4302 but this way if it does, we get a R_MN10300_NONE reloc
4303 instead of garbage. */
4304 s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4305 if (s->contents == NULL && s->size != 0)
4306 return FALSE;
4309 if (elf_hash_table (info)->dynamic_sections_created)
4311 /* Add some entries to the .dynamic section. We fill in the
4312 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4313 but we must add the entries now so that we get the correct
4314 size for the .dynamic section. The DT_DEBUG entry is filled
4315 in by the dynamic linker and used by the debugger. */
4316 if (! info->shared)
4318 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4319 return FALSE;
4322 if (plt)
4324 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4325 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4326 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4327 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4328 return FALSE;
4331 if (relocs)
4333 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4334 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4335 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4336 sizeof (Elf32_External_Rela)))
4337 return FALSE;
4340 if (reltext)
4342 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4343 return FALSE;
4347 return TRUE;
4350 /* Finish up dynamic symbol handling. We set the contents of various
4351 dynamic sections here. */
4353 static bfd_boolean
4354 _bfd_mn10300_elf_finish_dynamic_symbol (output_bfd, info, h, sym)
4355 bfd * output_bfd;
4356 struct bfd_link_info * info;
4357 struct elf_link_hash_entry * h;
4358 Elf_Internal_Sym * sym;
4360 bfd * dynobj;
4362 dynobj = elf_hash_table (info)->dynobj;
4364 if (h->plt.offset != (bfd_vma) -1)
4366 asection * splt;
4367 asection * sgot;
4368 asection * srel;
4369 bfd_vma plt_index;
4370 bfd_vma got_offset;
4371 Elf_Internal_Rela rel;
4373 /* This symbol has an entry in the procedure linkage table. Set
4374 it up. */
4376 BFD_ASSERT (h->dynindx != -1);
4378 splt = bfd_get_section_by_name (dynobj, ".plt");
4379 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4380 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4381 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4383 /* Get the index in the procedure linkage table which
4384 corresponds to this symbol. This is the index of this symbol
4385 in all the symbols for which we are making plt entries. The
4386 first entry in the procedure linkage table is reserved. */
4387 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4388 / elf_mn10300_sizeof_plt (info));
4390 /* Get the offset into the .got table of the entry that
4391 corresponds to this function. Each .got entry is 4 bytes.
4392 The first three are reserved. */
4393 got_offset = (plt_index + 3) * 4;
4395 /* Fill in the entry in the procedure linkage table. */
4396 if (! info->shared)
4398 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4399 elf_mn10300_sizeof_plt (info));
4400 bfd_put_32 (output_bfd,
4401 (sgot->output_section->vma
4402 + sgot->output_offset
4403 + got_offset),
4404 (splt->contents + h->plt.offset
4405 + elf_mn10300_plt_symbol_offset (info)));
4407 bfd_put_32 (output_bfd,
4408 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4409 (splt->contents + h->plt.offset
4410 + elf_mn10300_plt_plt0_offset (info)));
4412 else
4414 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4415 elf_mn10300_sizeof_plt (info));
4417 bfd_put_32 (output_bfd, got_offset,
4418 (splt->contents + h->plt.offset
4419 + elf_mn10300_plt_symbol_offset (info)));
4422 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4423 (splt->contents + h->plt.offset
4424 + elf_mn10300_plt_reloc_offset (info)));
4426 /* Fill in the entry in the global offset table. */
4427 bfd_put_32 (output_bfd,
4428 (splt->output_section->vma
4429 + splt->output_offset
4430 + h->plt.offset
4431 + elf_mn10300_plt_temp_offset (info)),
4432 sgot->contents + got_offset);
4434 /* Fill in the entry in the .rela.plt section. */
4435 rel.r_offset = (sgot->output_section->vma
4436 + sgot->output_offset
4437 + got_offset);
4438 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4439 rel.r_addend = 0;
4440 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4441 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4442 + plt_index));
4444 if (!h->def_regular)
4445 /* Mark the symbol as undefined, rather than as defined in
4446 the .plt section. Leave the value alone. */
4447 sym->st_shndx = SHN_UNDEF;
4450 if (h->got.offset != (bfd_vma) -1)
4452 asection * sgot;
4453 asection * srel;
4454 Elf_Internal_Rela rel;
4456 /* This symbol has an entry in the global offset table. Set it up. */
4458 sgot = bfd_get_section_by_name (dynobj, ".got");
4459 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4460 BFD_ASSERT (sgot != NULL && srel != NULL);
4462 rel.r_offset = (sgot->output_section->vma
4463 + sgot->output_offset
4464 + (h->got.offset &~ 1));
4466 /* If this is a -Bsymbolic link, and the symbol is defined
4467 locally, we just want to emit a RELATIVE reloc. Likewise if
4468 the symbol was forced to be local because of a version file.
4469 The entry in the global offset table will already have been
4470 initialized in the relocate_section function. */
4471 if (info->shared
4472 && (info->symbolic || h->dynindx == -1)
4473 && h->def_regular)
4475 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4476 rel.r_addend = (h->root.u.def.value
4477 + h->root.u.def.section->output_section->vma
4478 + h->root.u.def.section->output_offset);
4480 else
4482 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4483 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4484 rel.r_addend = 0;
4487 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4488 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4489 + srel->reloc_count));
4490 ++ srel->reloc_count;
4493 if (h->needs_copy)
4495 asection * s;
4496 Elf_Internal_Rela rel;
4498 /* This symbol needs a copy reloc. Set it up. */
4499 BFD_ASSERT (h->dynindx != -1
4500 && (h->root.type == bfd_link_hash_defined
4501 || h->root.type == bfd_link_hash_defweak));
4503 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4504 ".rela.bss");
4505 BFD_ASSERT (s != NULL);
4507 rel.r_offset = (h->root.u.def.value
4508 + h->root.u.def.section->output_section->vma
4509 + h->root.u.def.section->output_offset);
4510 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4511 rel.r_addend = 0;
4512 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4513 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4514 + s->reloc_count));
4515 ++ s->reloc_count;
4518 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4519 if (strcmp (h->root.root.string, "_DYNAMIC") == 0
4520 || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
4521 sym->st_shndx = SHN_ABS;
4523 return TRUE;
4526 /* Finish up the dynamic sections. */
4528 static bfd_boolean
4529 _bfd_mn10300_elf_finish_dynamic_sections (output_bfd, info)
4530 bfd * output_bfd;
4531 struct bfd_link_info * info;
4533 bfd * dynobj;
4534 asection * sgot;
4535 asection * sdyn;
4537 dynobj = elf_hash_table (info)->dynobj;
4539 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4540 BFD_ASSERT (sgot != NULL);
4541 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4543 if (elf_hash_table (info)->dynamic_sections_created)
4545 asection * splt;
4546 Elf32_External_Dyn * dyncon;
4547 Elf32_External_Dyn * dynconend;
4549 BFD_ASSERT (sdyn != NULL);
4551 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4552 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4554 for (; dyncon < dynconend; dyncon++)
4556 Elf_Internal_Dyn dyn;
4557 const char * name;
4558 asection * s;
4560 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4562 switch (dyn.d_tag)
4564 default:
4565 break;
4567 case DT_PLTGOT:
4568 name = ".got";
4569 goto get_vma;
4571 case DT_JMPREL:
4572 name = ".rela.plt";
4573 get_vma:
4574 s = bfd_get_section_by_name (output_bfd, name);
4575 BFD_ASSERT (s != NULL);
4576 dyn.d_un.d_ptr = s->vma;
4577 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4578 break;
4580 case DT_PLTRELSZ:
4581 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4582 BFD_ASSERT (s != NULL);
4583 dyn.d_un.d_val = s->size;
4584 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4585 break;
4587 case DT_RELASZ:
4588 /* My reading of the SVR4 ABI indicates that the
4589 procedure linkage table relocs (DT_JMPREL) should be
4590 included in the overall relocs (DT_RELA). This is
4591 what Solaris does. However, UnixWare can not handle
4592 that case. Therefore, we override the DT_RELASZ entry
4593 here to make it not include the JMPREL relocs. Since
4594 the linker script arranges for .rela.plt to follow all
4595 other relocation sections, we don't have to worry
4596 about changing the DT_RELA entry. */
4597 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4598 if (s != NULL)
4599 dyn.d_un.d_val -= s->size;
4600 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4601 break;
4605 /* Fill in the first entry in the procedure linkage table. */
4606 splt = bfd_get_section_by_name (dynobj, ".plt");
4607 if (splt && splt->size > 0)
4609 if (info->shared)
4611 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4612 elf_mn10300_sizeof_plt (info));
4614 else
4616 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4617 bfd_put_32 (output_bfd,
4618 sgot->output_section->vma + sgot->output_offset + 4,
4619 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4620 bfd_put_32 (output_bfd,
4621 sgot->output_section->vma + sgot->output_offset + 8,
4622 splt->contents + elf_mn10300_plt0_linker_offset (info));
4625 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4626 really seem like the right value. */
4627 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4631 /* Fill in the first three entries in the global offset table. */
4632 if (sgot->size > 0)
4634 if (sdyn == NULL)
4635 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4636 else
4637 bfd_put_32 (output_bfd,
4638 sdyn->output_section->vma + sdyn->output_offset,
4639 sgot->contents);
4640 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4641 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4644 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4646 return TRUE;
4649 /* Classify relocation types, such that combreloc can sort them
4650 properly. */
4652 static enum elf_reloc_type_class
4653 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4655 switch ((int) ELF32_R_TYPE (rela->r_info))
4657 case R_MN10300_RELATIVE:
4658 return reloc_class_relative;
4659 case R_MN10300_JMP_SLOT:
4660 return reloc_class_plt;
4661 case R_MN10300_COPY:
4662 return reloc_class_copy;
4663 default:
4664 return reloc_class_normal;
4668 #ifndef ELF_ARCH
4669 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4670 #define TARGET_LITTLE_NAME "elf32-mn10300"
4671 #define ELF_ARCH bfd_arch_mn10300
4672 #define ELF_MACHINE_CODE EM_MN10300
4673 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4674 #define ELF_MAXPAGESIZE 0x1000
4675 #endif
4677 #define elf_info_to_howto mn10300_info_to_howto
4678 #define elf_info_to_howto_rel 0
4679 #define elf_backend_can_gc_sections 1
4680 #define elf_backend_rela_normal 1
4681 #define elf_backend_check_relocs mn10300_elf_check_relocs
4682 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4683 #define elf_backend_relocate_section mn10300_elf_relocate_section
4684 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4685 #define bfd_elf32_bfd_get_relocated_section_contents \
4686 mn10300_elf_get_relocated_section_contents
4687 #define bfd_elf32_bfd_link_hash_table_create \
4688 elf32_mn10300_link_hash_table_create
4689 #define bfd_elf32_bfd_link_hash_table_free \
4690 elf32_mn10300_link_hash_table_free
4692 #ifndef elf_symbol_leading_char
4693 #define elf_symbol_leading_char '_'
4694 #endif
4696 /* So we can set bits in e_flags. */
4697 #define elf_backend_final_write_processing \
4698 _bfd_mn10300_elf_final_write_processing
4699 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4701 #define bfd_elf32_bfd_merge_private_bfd_data \
4702 _bfd_mn10300_elf_merge_private_bfd_data
4704 #define elf_backend_can_gc_sections 1
4705 #define elf_backend_create_dynamic_sections \
4706 _bfd_mn10300_elf_create_dynamic_sections
4707 #define elf_backend_adjust_dynamic_symbol \
4708 _bfd_mn10300_elf_adjust_dynamic_symbol
4709 #define elf_backend_size_dynamic_sections \
4710 _bfd_mn10300_elf_size_dynamic_sections
4711 #define elf_backend_finish_dynamic_symbol \
4712 _bfd_mn10300_elf_finish_dynamic_symbol
4713 #define elf_backend_finish_dynamic_sections \
4714 _bfd_mn10300_elf_finish_dynamic_sections
4716 #define elf_backend_reloc_type_class \
4717 _bfd_mn10300_elf_reloc_type_class
4719 #define elf_backend_want_got_plt 1
4720 #define elf_backend_plt_readonly 1
4721 #define elf_backend_want_plt_sym 0
4722 #define elf_backend_got_header_size 12
4724 #include "elf32-target.h"