* elf-bfd.h (emum elf_object_id): Rename to elf_target_id. Add
[deliverable/binutils-gdb.git] / bfd / elf-m10300.c
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
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 3 of the License, or
10 (at your option) any later version.
11
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.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/mn10300.h"
27 #include "libiberty.h"
28
29 /* The mn10300 linker needs to keep track of the number of relocs that
30 it decides to copy in check_relocs for each symbol. This is so
31 that it can discard PC relative relocs if it doesn't need them when
32 linking with -Bsymbolic. We store the information in a field
33 extending the regular ELF linker hash table. */
34
35 struct elf32_mn10300_link_hash_entry
36 {
37 /* The basic elf link hash table entry. */
38 struct elf_link_hash_entry root;
39
40 /* For function symbols, the number of times this function is
41 called directly (ie by name). */
42 unsigned int direct_calls;
43
44 /* For function symbols, the size of this function's stack
45 (if <= 255 bytes). We stuff this into "call" instructions
46 to this target when it's valid and profitable to do so.
47
48 This does not include stack allocated by movm! */
49 unsigned char stack_size;
50
51 /* For function symbols, arguments (if any) for movm instruction
52 in the prologue. We stuff this value into "call" instructions
53 to the target when it's valid and profitable to do so. */
54 unsigned char movm_args;
55
56 /* For function symbols, the amount of stack space that would be allocated
57 by the movm instruction. This is redundant with movm_args, but we
58 add it to the hash table to avoid computing it over and over. */
59 unsigned char movm_stack_size;
60
61 /* When set, convert all "call" instructions to this target into "calls"
62 instructions. */
63 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
64
65 /* Used to mark functions which have had redundant parts of their
66 prologue deleted. */
67 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
68 unsigned char flags;
69
70 /* Calculated value. */
71 bfd_vma value;
72 };
73
74 /* We derive a hash table from the main elf linker hash table so
75 we can store state variables and a secondary hash table without
76 resorting to global variables. */
77 struct elf32_mn10300_link_hash_table
78 {
79 /* The main hash table. */
80 struct elf_link_hash_table root;
81
82 /* A hash table for static functions. We could derive a new hash table
83 instead of using the full elf32_mn10300_link_hash_table if we wanted
84 to save some memory. */
85 struct elf32_mn10300_link_hash_table *static_hash_table;
86
87 /* Random linker state flags. */
88 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
89 char flags;
90 };
91
92 #ifndef streq
93 #define streq(a, b) (strcmp ((a),(b)) == 0)
94 #endif
95
96 /* For MN10300 linker hash table. */
97
98 /* Get the MN10300 ELF linker hash table from a link_info structure. */
99
100 #define elf32_mn10300_hash_table(p) \
101 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
102 == MN10300_ELF_DATA ? ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) : NULL)
103
104 #define elf32_mn10300_link_hash_traverse(table, func, info) \
105 (elf_link_hash_traverse \
106 (&(table)->root, \
107 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
108 (info)))
109
110 static reloc_howto_type elf_mn10300_howto_table[] =
111 {
112 /* Dummy relocation. Does nothing. */
113 HOWTO (R_MN10300_NONE,
114 0,
115 2,
116 16,
117 FALSE,
118 0,
119 complain_overflow_bitfield,
120 bfd_elf_generic_reloc,
121 "R_MN10300_NONE",
122 FALSE,
123 0,
124 0,
125 FALSE),
126 /* Standard 32 bit reloc. */
127 HOWTO (R_MN10300_32,
128 0,
129 2,
130 32,
131 FALSE,
132 0,
133 complain_overflow_bitfield,
134 bfd_elf_generic_reloc,
135 "R_MN10300_32",
136 FALSE,
137 0xffffffff,
138 0xffffffff,
139 FALSE),
140 /* Standard 16 bit reloc. */
141 HOWTO (R_MN10300_16,
142 0,
143 1,
144 16,
145 FALSE,
146 0,
147 complain_overflow_bitfield,
148 bfd_elf_generic_reloc,
149 "R_MN10300_16",
150 FALSE,
151 0xffff,
152 0xffff,
153 FALSE),
154 /* Standard 8 bit reloc. */
155 HOWTO (R_MN10300_8,
156 0,
157 0,
158 8,
159 FALSE,
160 0,
161 complain_overflow_bitfield,
162 bfd_elf_generic_reloc,
163 "R_MN10300_8",
164 FALSE,
165 0xff,
166 0xff,
167 FALSE),
168 /* Standard 32bit pc-relative reloc. */
169 HOWTO (R_MN10300_PCREL32,
170 0,
171 2,
172 32,
173 TRUE,
174 0,
175 complain_overflow_bitfield,
176 bfd_elf_generic_reloc,
177 "R_MN10300_PCREL32",
178 FALSE,
179 0xffffffff,
180 0xffffffff,
181 TRUE),
182 /* Standard 16bit pc-relative reloc. */
183 HOWTO (R_MN10300_PCREL16,
184 0,
185 1,
186 16,
187 TRUE,
188 0,
189 complain_overflow_bitfield,
190 bfd_elf_generic_reloc,
191 "R_MN10300_PCREL16",
192 FALSE,
193 0xffff,
194 0xffff,
195 TRUE),
196 /* Standard 8 pc-relative reloc. */
197 HOWTO (R_MN10300_PCREL8,
198 0,
199 0,
200 8,
201 TRUE,
202 0,
203 complain_overflow_bitfield,
204 bfd_elf_generic_reloc,
205 "R_MN10300_PCREL8",
206 FALSE,
207 0xff,
208 0xff,
209 TRUE),
210
211 /* GNU extension to record C++ vtable hierarchy. */
212 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
213 0, /* rightshift */
214 0, /* size (0 = byte, 1 = short, 2 = long) */
215 0, /* bitsize */
216 FALSE, /* pc_relative */
217 0, /* bitpos */
218 complain_overflow_dont, /* complain_on_overflow */
219 NULL, /* special_function */
220 "R_MN10300_GNU_VTINHERIT", /* name */
221 FALSE, /* partial_inplace */
222 0, /* src_mask */
223 0, /* dst_mask */
224 FALSE), /* pcrel_offset */
225
226 /* GNU extension to record C++ vtable member usage */
227 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
228 0, /* rightshift */
229 0, /* size (0 = byte, 1 = short, 2 = long) */
230 0, /* bitsize */
231 FALSE, /* pc_relative */
232 0, /* bitpos */
233 complain_overflow_dont, /* complain_on_overflow */
234 NULL, /* special_function */
235 "R_MN10300_GNU_VTENTRY", /* name */
236 FALSE, /* partial_inplace */
237 0, /* src_mask */
238 0, /* dst_mask */
239 FALSE), /* pcrel_offset */
240
241 /* Standard 24 bit reloc. */
242 HOWTO (R_MN10300_24,
243 0,
244 2,
245 24,
246 FALSE,
247 0,
248 complain_overflow_bitfield,
249 bfd_elf_generic_reloc,
250 "R_MN10300_24",
251 FALSE,
252 0xffffff,
253 0xffffff,
254 FALSE),
255 HOWTO (R_MN10300_GOTPC32, /* type */
256 0, /* rightshift */
257 2, /* size (0 = byte, 1 = short, 2 = long) */
258 32, /* bitsize */
259 TRUE, /* pc_relative */
260 0, /* bitpos */
261 complain_overflow_bitfield, /* complain_on_overflow */
262 bfd_elf_generic_reloc, /* */
263 "R_MN10300_GOTPC32", /* name */
264 FALSE, /* partial_inplace */
265 0xffffffff, /* src_mask */
266 0xffffffff, /* dst_mask */
267 TRUE), /* pcrel_offset */
268
269 HOWTO (R_MN10300_GOTPC16, /* type */
270 0, /* rightshift */
271 1, /* size (0 = byte, 1 = short, 2 = long) */
272 16, /* bitsize */
273 TRUE, /* pc_relative */
274 0, /* bitpos */
275 complain_overflow_bitfield, /* complain_on_overflow */
276 bfd_elf_generic_reloc, /* */
277 "R_MN10300_GOTPC16", /* name */
278 FALSE, /* partial_inplace */
279 0xffff, /* src_mask */
280 0xffff, /* dst_mask */
281 TRUE), /* pcrel_offset */
282
283 HOWTO (R_MN10300_GOTOFF32, /* type */
284 0, /* rightshift */
285 2, /* size (0 = byte, 1 = short, 2 = long) */
286 32, /* bitsize */
287 FALSE, /* pc_relative */
288 0, /* bitpos */
289 complain_overflow_bitfield, /* complain_on_overflow */
290 bfd_elf_generic_reloc, /* */
291 "R_MN10300_GOTOFF32", /* name */
292 FALSE, /* partial_inplace */
293 0xffffffff, /* src_mask */
294 0xffffffff, /* dst_mask */
295 FALSE), /* pcrel_offset */
296
297 HOWTO (R_MN10300_GOTOFF24, /* type */
298 0, /* rightshift */
299 2, /* size (0 = byte, 1 = short, 2 = long) */
300 24, /* bitsize */
301 FALSE, /* pc_relative */
302 0, /* bitpos */
303 complain_overflow_bitfield, /* complain_on_overflow */
304 bfd_elf_generic_reloc, /* */
305 "R_MN10300_GOTOFF24", /* name */
306 FALSE, /* partial_inplace */
307 0xffffff, /* src_mask */
308 0xffffff, /* dst_mask */
309 FALSE), /* pcrel_offset */
310
311 HOWTO (R_MN10300_GOTOFF16, /* type */
312 0, /* rightshift */
313 1, /* size (0 = byte, 1 = short, 2 = long) */
314 16, /* bitsize */
315 FALSE, /* pc_relative */
316 0, /* bitpos */
317 complain_overflow_bitfield, /* complain_on_overflow */
318 bfd_elf_generic_reloc, /* */
319 "R_MN10300_GOTOFF16", /* name */
320 FALSE, /* partial_inplace */
321 0xffff, /* src_mask */
322 0xffff, /* dst_mask */
323 FALSE), /* pcrel_offset */
324
325 HOWTO (R_MN10300_PLT32, /* type */
326 0, /* rightshift */
327 2, /* size (0 = byte, 1 = short, 2 = long) */
328 32, /* bitsize */
329 TRUE, /* pc_relative */
330 0, /* bitpos */
331 complain_overflow_bitfield, /* complain_on_overflow */
332 bfd_elf_generic_reloc, /* */
333 "R_MN10300_PLT32", /* name */
334 FALSE, /* partial_inplace */
335 0xffffffff, /* src_mask */
336 0xffffffff, /* dst_mask */
337 TRUE), /* pcrel_offset */
338
339 HOWTO (R_MN10300_PLT16, /* type */
340 0, /* rightshift */
341 1, /* size (0 = byte, 1 = short, 2 = long) */
342 16, /* bitsize */
343 TRUE, /* pc_relative */
344 0, /* bitpos */
345 complain_overflow_bitfield, /* complain_on_overflow */
346 bfd_elf_generic_reloc, /* */
347 "R_MN10300_PLT16", /* name */
348 FALSE, /* partial_inplace */
349 0xffff, /* src_mask */
350 0xffff, /* dst_mask */
351 TRUE), /* pcrel_offset */
352
353 HOWTO (R_MN10300_GOT32, /* type */
354 0, /* rightshift */
355 2, /* size (0 = byte, 1 = short, 2 = long) */
356 32, /* bitsize */
357 FALSE, /* pc_relative */
358 0, /* bitpos */
359 complain_overflow_bitfield, /* complain_on_overflow */
360 bfd_elf_generic_reloc, /* */
361 "R_MN10300_GOT32", /* name */
362 FALSE, /* partial_inplace */
363 0xffffffff, /* src_mask */
364 0xffffffff, /* dst_mask */
365 FALSE), /* pcrel_offset */
366
367 HOWTO (R_MN10300_GOT24, /* type */
368 0, /* rightshift */
369 2, /* size (0 = byte, 1 = short, 2 = long) */
370 24, /* bitsize */
371 FALSE, /* pc_relative */
372 0, /* bitpos */
373 complain_overflow_bitfield, /* complain_on_overflow */
374 bfd_elf_generic_reloc, /* */
375 "R_MN10300_GOT24", /* name */
376 FALSE, /* partial_inplace */
377 0xffffffff, /* src_mask */
378 0xffffffff, /* dst_mask */
379 FALSE), /* pcrel_offset */
380
381 HOWTO (R_MN10300_GOT16, /* type */
382 0, /* rightshift */
383 1, /* size (0 = byte, 1 = short, 2 = long) */
384 16, /* bitsize */
385 FALSE, /* pc_relative */
386 0, /* bitpos */
387 complain_overflow_bitfield, /* complain_on_overflow */
388 bfd_elf_generic_reloc, /* */
389 "R_MN10300_GOT16", /* name */
390 FALSE, /* partial_inplace */
391 0xffffffff, /* src_mask */
392 0xffffffff, /* dst_mask */
393 FALSE), /* pcrel_offset */
394
395 HOWTO (R_MN10300_COPY, /* type */
396 0, /* rightshift */
397 2, /* size (0 = byte, 1 = short, 2 = long) */
398 32, /* bitsize */
399 FALSE, /* pc_relative */
400 0, /* bitpos */
401 complain_overflow_bitfield, /* complain_on_overflow */
402 bfd_elf_generic_reloc, /* */
403 "R_MN10300_COPY", /* name */
404 FALSE, /* partial_inplace */
405 0xffffffff, /* src_mask */
406 0xffffffff, /* dst_mask */
407 FALSE), /* pcrel_offset */
408
409 HOWTO (R_MN10300_GLOB_DAT, /* type */
410 0, /* rightshift */
411 2, /* size (0 = byte, 1 = short, 2 = long) */
412 32, /* bitsize */
413 FALSE, /* pc_relative */
414 0, /* bitpos */
415 complain_overflow_bitfield, /* complain_on_overflow */
416 bfd_elf_generic_reloc, /* */
417 "R_MN10300_GLOB_DAT", /* name */
418 FALSE, /* partial_inplace */
419 0xffffffff, /* src_mask */
420 0xffffffff, /* dst_mask */
421 FALSE), /* pcrel_offset */
422
423 HOWTO (R_MN10300_JMP_SLOT, /* type */
424 0, /* rightshift */
425 2, /* size (0 = byte, 1 = short, 2 = long) */
426 32, /* bitsize */
427 FALSE, /* pc_relative */
428 0, /* bitpos */
429 complain_overflow_bitfield, /* complain_on_overflow */
430 bfd_elf_generic_reloc, /* */
431 "R_MN10300_JMP_SLOT", /* name */
432 FALSE, /* partial_inplace */
433 0xffffffff, /* src_mask */
434 0xffffffff, /* dst_mask */
435 FALSE), /* pcrel_offset */
436
437 HOWTO (R_MN10300_RELATIVE, /* type */
438 0, /* rightshift */
439 2, /* size (0 = byte, 1 = short, 2 = long) */
440 32, /* bitsize */
441 FALSE, /* pc_relative */
442 0, /* bitpos */
443 complain_overflow_bitfield, /* complain_on_overflow */
444 bfd_elf_generic_reloc, /* */
445 "R_MN10300_RELATIVE", /* name */
446 FALSE, /* partial_inplace */
447 0xffffffff, /* src_mask */
448 0xffffffff, /* dst_mask */
449 FALSE), /* pcrel_offset */
450
451 EMPTY_HOWTO (24),
452 EMPTY_HOWTO (25),
453 EMPTY_HOWTO (26),
454 EMPTY_HOWTO (27),
455 EMPTY_HOWTO (28),
456 EMPTY_HOWTO (29),
457 EMPTY_HOWTO (30),
458 EMPTY_HOWTO (31),
459 EMPTY_HOWTO (32),
460
461 HOWTO (R_MN10300_SYM_DIFF, /* type */
462 0, /* rightshift */
463 2, /* size (0 = byte, 1 = short, 2 = long) */
464 32, /* bitsize */
465 FALSE, /* pc_relative */
466 0, /* bitpos */
467 complain_overflow_dont,/* complain_on_overflow */
468 NULL, /* special handler. */
469 "R_MN10300_SYM_DIFF", /* name */
470 FALSE, /* partial_inplace */
471 0xffffffff, /* src_mask */
472 0xffffffff, /* dst_mask */
473 FALSE), /* pcrel_offset */
474
475 HOWTO (R_MN10300_ALIGN, /* type */
476 0, /* rightshift */
477 0, /* size (0 = byte, 1 = short, 2 = long) */
478 32, /* bitsize */
479 FALSE, /* pc_relative */
480 0, /* bitpos */
481 complain_overflow_dont,/* complain_on_overflow */
482 NULL, /* special handler. */
483 "R_MN10300_ALIGN", /* name */
484 FALSE, /* partial_inplace */
485 0, /* src_mask */
486 0, /* dst_mask */
487 FALSE) /* pcrel_offset */
488 };
489
490 struct mn10300_reloc_map
491 {
492 bfd_reloc_code_real_type bfd_reloc_val;
493 unsigned char elf_reloc_val;
494 };
495
496 static const struct mn10300_reloc_map mn10300_reloc_map[] =
497 {
498 { BFD_RELOC_NONE, R_MN10300_NONE, },
499 { BFD_RELOC_32, R_MN10300_32, },
500 { BFD_RELOC_16, R_MN10300_16, },
501 { BFD_RELOC_8, R_MN10300_8, },
502 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
503 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
504 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
505 { BFD_RELOC_24, R_MN10300_24, },
506 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
507 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
508 { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
509 { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
510 { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
511 { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
512 { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
513 { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
514 { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
515 { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
516 { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
517 { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
518 { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
519 { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
520 { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
521 { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
522 { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF },
523 { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN }
524 };
525
526 /* Create the GOT section. */
527
528 static bfd_boolean
529 _bfd_mn10300_elf_create_got_section (bfd * abfd,
530 struct bfd_link_info * info)
531 {
532 flagword flags;
533 flagword pltflags;
534 asection * s;
535 struct elf_link_hash_entry * h;
536 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
537 int ptralign;
538
539 /* This function may be called more than once. */
540 if (bfd_get_section_by_name (abfd, ".got") != NULL)
541 return TRUE;
542
543 switch (bed->s->arch_size)
544 {
545 case 32:
546 ptralign = 2;
547 break;
548
549 case 64:
550 ptralign = 3;
551 break;
552
553 default:
554 bfd_set_error (bfd_error_bad_value);
555 return FALSE;
556 }
557
558 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
559 | SEC_LINKER_CREATED);
560
561 pltflags = flags;
562 pltflags |= SEC_CODE;
563 if (bed->plt_not_loaded)
564 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
565 if (bed->plt_readonly)
566 pltflags |= SEC_READONLY;
567
568 s = bfd_make_section_with_flags (abfd, ".plt", pltflags);
569 if (s == NULL
570 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
571 return FALSE;
572
573 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
574 .plt section. */
575 if (bed->want_plt_sym)
576 {
577 h = _bfd_elf_define_linkage_sym (abfd, info, s,
578 "_PROCEDURE_LINKAGE_TABLE_");
579 elf_hash_table (info)->hplt = h;
580 if (h == NULL)
581 return FALSE;
582 }
583
584 s = bfd_make_section_with_flags (abfd, ".got", flags);
585 if (s == NULL
586 || ! bfd_set_section_alignment (abfd, s, ptralign))
587 return FALSE;
588
589 if (bed->want_got_plt)
590 {
591 s = bfd_make_section_with_flags (abfd, ".got.plt", flags);
592 if (s == NULL
593 || ! bfd_set_section_alignment (abfd, s, ptralign))
594 return FALSE;
595 }
596
597 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
598 (or .got.plt) section. We don't do this in the linker script
599 because we don't want to define the symbol if we are not creating
600 a global offset table. */
601 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
602 elf_hash_table (info)->hgot = h;
603 if (h == NULL)
604 return FALSE;
605
606 /* The first bit of the global offset table is the header. */
607 s->size += bed->got_header_size;
608
609 return TRUE;
610 }
611
612 static reloc_howto_type *
613 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
614 bfd_reloc_code_real_type code)
615 {
616 unsigned int i;
617
618 for (i = ARRAY_SIZE (mn10300_reloc_map); i--;)
619 if (mn10300_reloc_map[i].bfd_reloc_val == code)
620 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
621
622 return NULL;
623 }
624
625 static reloc_howto_type *
626 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
627 const char *r_name)
628 {
629 unsigned int i;
630
631 for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;)
632 if (elf_mn10300_howto_table[i].name != NULL
633 && strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
634 return elf_mn10300_howto_table + i;
635
636 return NULL;
637 }
638
639 /* Set the howto pointer for an MN10300 ELF reloc. */
640
641 static void
642 mn10300_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED,
643 arelent *cache_ptr,
644 Elf_Internal_Rela *dst)
645 {
646 unsigned int r_type;
647
648 r_type = ELF32_R_TYPE (dst->r_info);
649 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
650 cache_ptr->howto = elf_mn10300_howto_table + r_type;
651 }
652
653 /* Look through the relocs for a section during the first phase.
654 Since we don't do .gots or .plts, we just need to consider the
655 virtual table relocs for gc. */
656
657 static bfd_boolean
658 mn10300_elf_check_relocs (bfd *abfd,
659 struct bfd_link_info *info,
660 asection *sec,
661 const Elf_Internal_Rela *relocs)
662 {
663 bfd_boolean sym_diff_reloc_seen;
664 Elf_Internal_Shdr *symtab_hdr;
665 Elf_Internal_Sym * isymbuf = NULL;
666 struct elf_link_hash_entry **sym_hashes;
667 const Elf_Internal_Rela *rel;
668 const Elf_Internal_Rela *rel_end;
669 bfd * dynobj;
670 bfd_vma * local_got_offsets;
671 asection * sgot;
672 asection * srelgot;
673 asection * sreloc;
674 bfd_boolean result = FALSE;
675
676 sgot = NULL;
677 srelgot = NULL;
678 sreloc = NULL;
679
680 if (info->relocatable)
681 return TRUE;
682
683 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
684 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
685 sym_hashes = elf_sym_hashes (abfd);
686
687 dynobj = elf_hash_table (info)->dynobj;
688 local_got_offsets = elf_local_got_offsets (abfd);
689 rel_end = relocs + sec->reloc_count;
690 sym_diff_reloc_seen = FALSE;
691
692 for (rel = relocs; rel < rel_end; rel++)
693 {
694 struct elf_link_hash_entry *h;
695 unsigned long r_symndx;
696 unsigned int r_type;
697
698 r_symndx = ELF32_R_SYM (rel->r_info);
699 if (r_symndx < symtab_hdr->sh_info)
700 h = NULL;
701 else
702 {
703 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
704 while (h->root.type == bfd_link_hash_indirect
705 || h->root.type == bfd_link_hash_warning)
706 h = (struct elf_link_hash_entry *) h->root.u.i.link;
707 }
708
709 r_type = ELF32_R_TYPE (rel->r_info);
710
711 /* Some relocs require a global offset table. */
712 if (dynobj == NULL)
713 {
714 switch (r_type)
715 {
716 case R_MN10300_GOT32:
717 case R_MN10300_GOT24:
718 case R_MN10300_GOT16:
719 case R_MN10300_GOTOFF32:
720 case R_MN10300_GOTOFF24:
721 case R_MN10300_GOTOFF16:
722 case R_MN10300_GOTPC32:
723 case R_MN10300_GOTPC16:
724 elf_hash_table (info)->dynobj = dynobj = abfd;
725 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
726 goto fail;
727 break;
728
729 default:
730 break;
731 }
732 }
733
734 switch (r_type)
735 {
736 /* This relocation describes the C++ object vtable hierarchy.
737 Reconstruct it for later use during GC. */
738 case R_MN10300_GNU_VTINHERIT:
739 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
740 goto fail;
741 break;
742
743 /* This relocation describes which C++ vtable entries are actually
744 used. Record for later use during GC. */
745 case R_MN10300_GNU_VTENTRY:
746 BFD_ASSERT (h != NULL);
747 if (h != NULL
748 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
749 goto fail;
750 break;
751
752 case R_MN10300_GOT32:
753 case R_MN10300_GOT24:
754 case R_MN10300_GOT16:
755 /* This symbol requires a global offset table entry. */
756
757 if (sgot == NULL)
758 {
759 sgot = bfd_get_section_by_name (dynobj, ".got");
760 BFD_ASSERT (sgot != NULL);
761 }
762
763 if (srelgot == NULL
764 && (h != NULL || info->shared))
765 {
766 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
767 if (srelgot == NULL)
768 {
769 srelgot = bfd_make_section_with_flags (dynobj,
770 ".rela.got",
771 (SEC_ALLOC
772 | SEC_LOAD
773 | SEC_HAS_CONTENTS
774 | SEC_IN_MEMORY
775 | SEC_LINKER_CREATED
776 | SEC_READONLY));
777 if (srelgot == NULL
778 || ! bfd_set_section_alignment (dynobj, srelgot, 2))
779 goto fail;
780 }
781 }
782
783 if (h != NULL)
784 {
785 if (h->got.offset != (bfd_vma) -1)
786 /* We have already allocated space in the .got. */
787 break;
788
789 h->got.offset = sgot->size;
790
791 /* Make sure this symbol is output as a dynamic symbol. */
792 if (h->dynindx == -1)
793 {
794 if (! bfd_elf_link_record_dynamic_symbol (info, h))
795 goto fail;
796 }
797
798 srelgot->size += sizeof (Elf32_External_Rela);
799 }
800 else
801 {
802 /* This is a global offset table entry for a local
803 symbol. */
804 if (local_got_offsets == NULL)
805 {
806 size_t size;
807 unsigned int i;
808
809 size = symtab_hdr->sh_info * sizeof (bfd_vma);
810 local_got_offsets = bfd_alloc (abfd, size);
811
812 if (local_got_offsets == NULL)
813 goto fail;
814
815 elf_local_got_offsets (abfd) = local_got_offsets;
816
817 for (i = 0; i < symtab_hdr->sh_info; i++)
818 local_got_offsets[i] = (bfd_vma) -1;
819 }
820
821 if (local_got_offsets[r_symndx] != (bfd_vma) -1)
822 /* We have already allocated space in the .got. */
823 break;
824
825 local_got_offsets[r_symndx] = sgot->size;
826
827 if (info->shared)
828 /* If we are generating a shared object, we need to
829 output a R_MN10300_RELATIVE reloc so that the dynamic
830 linker can adjust this GOT entry. */
831 srelgot->size += sizeof (Elf32_External_Rela);
832 }
833
834 sgot->size += 4;
835 break;
836
837 case R_MN10300_PLT32:
838 case R_MN10300_PLT16:
839 /* This symbol requires a procedure linkage table entry. We
840 actually build the entry in adjust_dynamic_symbol,
841 because this might be a case of linking PIC code which is
842 never referenced by a dynamic object, in which case we
843 don't need to generate a procedure linkage table entry
844 after all. */
845
846 /* If this is a local symbol, we resolve it directly without
847 creating a procedure linkage table entry. */
848 if (h == NULL)
849 continue;
850
851 if (ELF_ST_VISIBILITY (h->other) == STV_INTERNAL
852 || ELF_ST_VISIBILITY (h->other) == STV_HIDDEN)
853 break;
854
855 h->needs_plt = 1;
856 break;
857
858 case R_MN10300_24:
859 case R_MN10300_16:
860 case R_MN10300_8:
861 case R_MN10300_PCREL32:
862 case R_MN10300_PCREL16:
863 case R_MN10300_PCREL8:
864 if (h != NULL)
865 h->non_got_ref = 1;
866 break;
867
868 case R_MN10300_SYM_DIFF:
869 sym_diff_reloc_seen = TRUE;
870 break;
871
872 case R_MN10300_32:
873 if (h != NULL)
874 h->non_got_ref = 1;
875
876 /* If we are creating a shared library, then we
877 need to copy the reloc into the shared library. */
878 if (info->shared
879 && (sec->flags & SEC_ALLOC) != 0
880 /* Do not generate a dynamic reloc for a
881 reloc associated with a SYM_DIFF operation. */
882 && ! sym_diff_reloc_seen)
883 {
884 asection * sym_section = NULL;
885
886 /* Find the section containing the
887 symbol involved in the relocation. */
888 if (h == NULL)
889 {
890 Elf_Internal_Sym * isym;
891
892 if (isymbuf == NULL)
893 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
894 symtab_hdr->sh_info, 0,
895 NULL, NULL, NULL);
896 if (isymbuf)
897 {
898 isym = isymbuf + r_symndx;
899 /* All we care about is whether this local symbol is absolute. */
900 if (isym->st_shndx == SHN_ABS)
901 sym_section = bfd_abs_section_ptr;
902 }
903 }
904 else
905 {
906 if (h->root.type == bfd_link_hash_defined
907 || h->root.type == bfd_link_hash_defweak)
908 sym_section = h->root.u.def.section;
909 }
910
911 /* If the symbol is absolute then the relocation can
912 be resolved during linking and there is no need for
913 a dynamic reloc. */
914 if (sym_section != bfd_abs_section_ptr)
915 {
916 /* When creating a shared object, we must copy these
917 reloc types into the output file. We create a reloc
918 section in dynobj and make room for this reloc. */
919 if (sreloc == NULL)
920 {
921 sreloc = _bfd_elf_make_dynamic_reloc_section
922 (sec, dynobj, 2, abfd, /*rela?*/ TRUE);
923 if (sreloc == NULL)
924 goto fail;
925 }
926
927 sreloc->size += sizeof (Elf32_External_Rela);
928 }
929 }
930
931 break;
932 }
933
934 if (ELF32_R_TYPE (rel->r_info) != R_MN10300_SYM_DIFF)
935 sym_diff_reloc_seen = FALSE;
936 }
937
938 result = TRUE;
939 fail:
940 if (isymbuf != NULL)
941 free (isymbuf);
942
943 return result;
944 }
945
946 /* Return the section that should be marked against GC for a given
947 relocation. */
948
949 static asection *
950 mn10300_elf_gc_mark_hook (asection *sec,
951 struct bfd_link_info *info,
952 Elf_Internal_Rela *rel,
953 struct elf_link_hash_entry *h,
954 Elf_Internal_Sym *sym)
955 {
956 if (h != NULL)
957 switch (ELF32_R_TYPE (rel->r_info))
958 {
959 case R_MN10300_GNU_VTINHERIT:
960 case R_MN10300_GNU_VTENTRY:
961 return NULL;
962 }
963
964 return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
965 }
966
967 /* Perform a relocation as part of a final link. */
968
969 static bfd_reloc_status_type
970 mn10300_elf_final_link_relocate (reloc_howto_type *howto,
971 bfd *input_bfd,
972 bfd *output_bfd ATTRIBUTE_UNUSED,
973 asection *input_section,
974 bfd_byte *contents,
975 bfd_vma offset,
976 bfd_vma value,
977 bfd_vma addend,
978 struct elf_link_hash_entry * h,
979 unsigned long symndx,
980 struct bfd_link_info *info,
981 asection *sym_sec ATTRIBUTE_UNUSED,
982 int is_local ATTRIBUTE_UNUSED)
983 {
984 static asection * sym_diff_section;
985 static bfd_vma sym_diff_value;
986 bfd_boolean is_sym_diff_reloc;
987 unsigned long r_type = howto->type;
988 bfd_byte * hit_data = contents + offset;
989 bfd * dynobj;
990 bfd_vma * local_got_offsets;
991 asection * sgot;
992 asection * splt;
993 asection * sreloc;
994
995 dynobj = elf_hash_table (info)->dynobj;
996 local_got_offsets = elf_local_got_offsets (input_bfd);
997
998 sgot = NULL;
999 splt = NULL;
1000 sreloc = NULL;
1001
1002 switch (r_type)
1003 {
1004 case R_MN10300_24:
1005 case R_MN10300_16:
1006 case R_MN10300_8:
1007 case R_MN10300_PCREL8:
1008 case R_MN10300_PCREL16:
1009 case R_MN10300_PCREL32:
1010 case R_MN10300_GOTOFF32:
1011 case R_MN10300_GOTOFF24:
1012 case R_MN10300_GOTOFF16:
1013 if (info->shared
1014 && (input_section->flags & SEC_ALLOC) != 0
1015 && h != NULL
1016 && ! SYMBOL_REFERENCES_LOCAL (info, h))
1017 return bfd_reloc_dangerous;
1018 }
1019
1020 is_sym_diff_reloc = FALSE;
1021 if (sym_diff_section != NULL)
1022 {
1023 BFD_ASSERT (sym_diff_section == input_section);
1024
1025 switch (r_type)
1026 {
1027 case R_MN10300_32:
1028 case R_MN10300_24:
1029 case R_MN10300_16:
1030 case R_MN10300_8:
1031 value -= sym_diff_value;
1032 /* If we are computing a 32-bit value for the location lists
1033 and the result is 0 then we add one to the value. A zero
1034 value can result because of linker relaxation deleteing
1035 prologue instructions and using a value of 1 (for the begin
1036 and end offsets in the location list entry) results in a
1037 nul entry which does not prevent the following entries from
1038 being parsed. */
1039 if (r_type == R_MN10300_32
1040 && value == 0
1041 && strcmp (input_section->name, ".debug_loc") == 0)
1042 value = 1;
1043 sym_diff_section = NULL;
1044 is_sym_diff_reloc = TRUE;
1045 break;
1046
1047 default:
1048 sym_diff_section = NULL;
1049 break;
1050 }
1051 }
1052
1053 switch (r_type)
1054 {
1055 case R_MN10300_SYM_DIFF:
1056 BFD_ASSERT (addend == 0);
1057 /* Cache the input section and value.
1058 The offset is unreliable, since relaxation may
1059 have reduced the following reloc's offset. */
1060 sym_diff_section = input_section;
1061 sym_diff_value = value;
1062 return bfd_reloc_ok;
1063
1064 case R_MN10300_ALIGN:
1065 case R_MN10300_NONE:
1066 return bfd_reloc_ok;
1067
1068 case R_MN10300_32:
1069 if (info->shared
1070 /* Do not generate relocs when an R_MN10300_32 has been used
1071 with an R_MN10300_SYM_DIFF to compute a difference of two
1072 symbols. */
1073 && is_sym_diff_reloc == FALSE
1074 /* Also, do not generate a reloc when the symbol associated
1075 with the R_MN10300_32 reloc is absolute - there is no
1076 need for a run time computation in this case. */
1077 && sym_sec != bfd_abs_section_ptr
1078 /* If the section is not going to be allocated at load time
1079 then there is no need to generate relocs for it. */
1080 && (input_section->flags & SEC_ALLOC) != 0)
1081 {
1082 Elf_Internal_Rela outrel;
1083 bfd_boolean skip, relocate;
1084
1085 /* When generating a shared object, these relocations are
1086 copied into the output file to be resolved at run
1087 time. */
1088 if (sreloc == NULL)
1089 {
1090 sreloc = _bfd_elf_get_dynamic_reloc_section
1091 (input_bfd, input_section, /*rela?*/ TRUE);
1092 if (sreloc == NULL)
1093 return FALSE;
1094 }
1095
1096 skip = FALSE;
1097
1098 outrel.r_offset = _bfd_elf_section_offset (input_bfd, info,
1099 input_section, offset);
1100 if (outrel.r_offset == (bfd_vma) -1)
1101 skip = TRUE;
1102
1103 outrel.r_offset += (input_section->output_section->vma
1104 + input_section->output_offset);
1105
1106 if (skip)
1107 {
1108 memset (&outrel, 0, sizeof outrel);
1109 relocate = FALSE;
1110 }
1111 else
1112 {
1113 /* h->dynindx may be -1 if this symbol was marked to
1114 become local. */
1115 if (h == NULL
1116 || SYMBOL_REFERENCES_LOCAL (info, h))
1117 {
1118 relocate = TRUE;
1119 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1120 outrel.r_addend = value + addend;
1121 }
1122 else
1123 {
1124 BFD_ASSERT (h->dynindx != -1);
1125 relocate = FALSE;
1126 outrel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_32);
1127 outrel.r_addend = value + addend;
1128 }
1129 }
1130
1131 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1132 (bfd_byte *) (((Elf32_External_Rela *) sreloc->contents)
1133 + sreloc->reloc_count));
1134 ++sreloc->reloc_count;
1135
1136 /* If this reloc is against an external symbol, we do
1137 not want to fiddle with the addend. Otherwise, we
1138 need to include the symbol value so that it becomes
1139 an addend for the dynamic reloc. */
1140 if (! relocate)
1141 return bfd_reloc_ok;
1142 }
1143 value += addend;
1144 bfd_put_32 (input_bfd, value, hit_data);
1145 return bfd_reloc_ok;
1146
1147 case R_MN10300_24:
1148 value += addend;
1149
1150 if ((long) value > 0x7fffff || (long) value < -0x800000)
1151 return bfd_reloc_overflow;
1152
1153 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1154 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1155 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1156 return bfd_reloc_ok;
1157
1158 case R_MN10300_16:
1159 value += addend;
1160
1161 if ((long) value > 0x7fff || (long) value < -0x8000)
1162 return bfd_reloc_overflow;
1163
1164 bfd_put_16 (input_bfd, value, hit_data);
1165 return bfd_reloc_ok;
1166
1167 case R_MN10300_8:
1168 value += addend;
1169
1170 if ((long) value > 0x7f || (long) value < -0x80)
1171 return bfd_reloc_overflow;
1172
1173 bfd_put_8 (input_bfd, value, hit_data);
1174 return bfd_reloc_ok;
1175
1176 case R_MN10300_PCREL8:
1177 value -= (input_section->output_section->vma
1178 + input_section->output_offset);
1179 value -= offset;
1180 value += addend;
1181
1182 if ((long) value > 0x7f || (long) value < -0x80)
1183 return bfd_reloc_overflow;
1184
1185 bfd_put_8 (input_bfd, value, hit_data);
1186 return bfd_reloc_ok;
1187
1188 case R_MN10300_PCREL16:
1189 value -= (input_section->output_section->vma
1190 + input_section->output_offset);
1191 value -= offset;
1192 value += addend;
1193
1194 if ((long) value > 0x7fff || (long) value < -0x8000)
1195 return bfd_reloc_overflow;
1196
1197 bfd_put_16 (input_bfd, value, hit_data);
1198 return bfd_reloc_ok;
1199
1200 case R_MN10300_PCREL32:
1201 value -= (input_section->output_section->vma
1202 + input_section->output_offset);
1203 value -= offset;
1204 value += addend;
1205
1206 bfd_put_32 (input_bfd, value, hit_data);
1207 return bfd_reloc_ok;
1208
1209 case R_MN10300_GNU_VTINHERIT:
1210 case R_MN10300_GNU_VTENTRY:
1211 return bfd_reloc_ok;
1212
1213 case R_MN10300_GOTPC32:
1214 /* Use global offset table as symbol value. */
1215 value = bfd_get_section_by_name (dynobj,
1216 ".got")->output_section->vma;
1217 value -= (input_section->output_section->vma
1218 + input_section->output_offset);
1219 value -= offset;
1220 value += addend;
1221
1222 bfd_put_32 (input_bfd, value, hit_data);
1223 return bfd_reloc_ok;
1224
1225 case R_MN10300_GOTPC16:
1226 /* Use global offset table as symbol value. */
1227 value = bfd_get_section_by_name (dynobj,
1228 ".got")->output_section->vma;
1229 value -= (input_section->output_section->vma
1230 + input_section->output_offset);
1231 value -= offset;
1232 value += addend;
1233
1234 if ((long) value > 0x7fff || (long) value < -0x8000)
1235 return bfd_reloc_overflow;
1236
1237 bfd_put_16 (input_bfd, value, hit_data);
1238 return bfd_reloc_ok;
1239
1240 case R_MN10300_GOTOFF32:
1241 value -= bfd_get_section_by_name (dynobj,
1242 ".got")->output_section->vma;
1243 value += addend;
1244
1245 bfd_put_32 (input_bfd, value, hit_data);
1246 return bfd_reloc_ok;
1247
1248 case R_MN10300_GOTOFF24:
1249 value -= bfd_get_section_by_name (dynobj,
1250 ".got")->output_section->vma;
1251 value += addend;
1252
1253 if ((long) value > 0x7fffff || (long) value < -0x800000)
1254 return bfd_reloc_overflow;
1255
1256 bfd_put_8 (input_bfd, value, hit_data);
1257 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1258 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1259 return bfd_reloc_ok;
1260
1261 case R_MN10300_GOTOFF16:
1262 value -= bfd_get_section_by_name (dynobj,
1263 ".got")->output_section->vma;
1264 value += addend;
1265
1266 if ((long) value > 0x7fff || (long) value < -0x8000)
1267 return bfd_reloc_overflow;
1268
1269 bfd_put_16 (input_bfd, value, hit_data);
1270 return bfd_reloc_ok;
1271
1272 case R_MN10300_PLT32:
1273 if (h != NULL
1274 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1275 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1276 && h->plt.offset != (bfd_vma) -1)
1277 {
1278 splt = bfd_get_section_by_name (dynobj, ".plt");
1279
1280 value = (splt->output_section->vma
1281 + splt->output_offset
1282 + h->plt.offset) - value;
1283 }
1284
1285 value -= (input_section->output_section->vma
1286 + input_section->output_offset);
1287 value -= offset;
1288 value += addend;
1289
1290 bfd_put_32 (input_bfd, value, hit_data);
1291 return bfd_reloc_ok;
1292
1293 case R_MN10300_PLT16:
1294 if (h != NULL
1295 && ELF_ST_VISIBILITY (h->other) != STV_INTERNAL
1296 && ELF_ST_VISIBILITY (h->other) != STV_HIDDEN
1297 && h->plt.offset != (bfd_vma) -1)
1298 {
1299 splt = bfd_get_section_by_name (dynobj, ".plt");
1300
1301 value = (splt->output_section->vma
1302 + splt->output_offset
1303 + h->plt.offset) - value;
1304 }
1305
1306 value -= (input_section->output_section->vma
1307 + input_section->output_offset);
1308 value -= offset;
1309 value += addend;
1310
1311 if ((long) value > 0x7fff || (long) value < -0x8000)
1312 return bfd_reloc_overflow;
1313
1314 bfd_put_16 (input_bfd, value, hit_data);
1315 return bfd_reloc_ok;
1316
1317 case R_MN10300_GOT32:
1318 case R_MN10300_GOT24:
1319 case R_MN10300_GOT16:
1320 {
1321 sgot = bfd_get_section_by_name (dynobj, ".got");
1322
1323 if (h != NULL)
1324 {
1325 bfd_vma off;
1326
1327 off = h->got.offset;
1328 BFD_ASSERT (off != (bfd_vma) -1);
1329
1330 if (! elf_hash_table (info)->dynamic_sections_created
1331 || SYMBOL_REFERENCES_LOCAL (info, h))
1332 /* This is actually a static link, or it is a
1333 -Bsymbolic link and the symbol is defined
1334 locally, or the symbol was forced to be local
1335 because of a version file. We must initialize
1336 this entry in the global offset table.
1337
1338 When doing a dynamic link, we create a .rela.got
1339 relocation entry to initialize the value. This
1340 is done in the finish_dynamic_symbol routine. */
1341 bfd_put_32 (output_bfd, value,
1342 sgot->contents + off);
1343
1344 value = sgot->output_offset + off;
1345 }
1346 else
1347 {
1348 bfd_vma off;
1349
1350 off = elf_local_got_offsets (input_bfd)[symndx];
1351
1352 bfd_put_32 (output_bfd, value, sgot->contents + off);
1353
1354 if (info->shared)
1355 {
1356 asection * srelgot;
1357 Elf_Internal_Rela outrel;
1358
1359 srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
1360 BFD_ASSERT (srelgot != NULL);
1361
1362 outrel.r_offset = (sgot->output_section->vma
1363 + sgot->output_offset
1364 + off);
1365 outrel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
1366 outrel.r_addend = value;
1367 bfd_elf32_swap_reloca_out (output_bfd, &outrel,
1368 (bfd_byte *) (((Elf32_External_Rela *)
1369 srelgot->contents)
1370 + srelgot->reloc_count));
1371 ++ srelgot->reloc_count;
1372 }
1373
1374 value = sgot->output_offset + off;
1375 }
1376 }
1377
1378 value += addend;
1379
1380 if (r_type == R_MN10300_GOT32)
1381 {
1382 bfd_put_32 (input_bfd, value, hit_data);
1383 return bfd_reloc_ok;
1384 }
1385 else if (r_type == R_MN10300_GOT24)
1386 {
1387 if ((long) value > 0x7fffff || (long) value < -0x800000)
1388 return bfd_reloc_overflow;
1389
1390 bfd_put_8 (input_bfd, value & 0xff, hit_data);
1391 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
1392 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
1393 return bfd_reloc_ok;
1394 }
1395 else if (r_type == R_MN10300_GOT16)
1396 {
1397 if ((long) value > 0x7fff || (long) value < -0x8000)
1398 return bfd_reloc_overflow;
1399
1400 bfd_put_16 (input_bfd, value, hit_data);
1401 return bfd_reloc_ok;
1402 }
1403 /* Fall through. */
1404
1405 default:
1406 return bfd_reloc_notsupported;
1407 }
1408 }
1409 \f
1410 /* Relocate an MN10300 ELF section. */
1411
1412 static bfd_boolean
1413 mn10300_elf_relocate_section (bfd *output_bfd,
1414 struct bfd_link_info *info,
1415 bfd *input_bfd,
1416 asection *input_section,
1417 bfd_byte *contents,
1418 Elf_Internal_Rela *relocs,
1419 Elf_Internal_Sym *local_syms,
1420 asection **local_sections)
1421 {
1422 Elf_Internal_Shdr *symtab_hdr;
1423 struct elf_link_hash_entry **sym_hashes;
1424 Elf_Internal_Rela *rel, *relend;
1425
1426 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1427 sym_hashes = elf_sym_hashes (input_bfd);
1428
1429 rel = relocs;
1430 relend = relocs + input_section->reloc_count;
1431 for (; rel < relend; rel++)
1432 {
1433 int r_type;
1434 reloc_howto_type *howto;
1435 unsigned long r_symndx;
1436 Elf_Internal_Sym *sym;
1437 asection *sec;
1438 struct elf32_mn10300_link_hash_entry *h;
1439 bfd_vma relocation;
1440 bfd_reloc_status_type r;
1441
1442 r_symndx = ELF32_R_SYM (rel->r_info);
1443 r_type = ELF32_R_TYPE (rel->r_info);
1444 howto = elf_mn10300_howto_table + r_type;
1445
1446 /* Just skip the vtable gc relocs. */
1447 if (r_type == R_MN10300_GNU_VTINHERIT
1448 || r_type == R_MN10300_GNU_VTENTRY)
1449 continue;
1450
1451 h = NULL;
1452 sym = NULL;
1453 sec = NULL;
1454 if (r_symndx < symtab_hdr->sh_info)
1455 {
1456 sym = local_syms + r_symndx;
1457 sec = local_sections[r_symndx];
1458 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1459 }
1460 else
1461 {
1462 bfd_boolean unresolved_reloc;
1463 bfd_boolean warned;
1464 struct elf_link_hash_entry *hh;
1465
1466 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1467 r_symndx, symtab_hdr, sym_hashes,
1468 hh, sec, relocation,
1469 unresolved_reloc, warned);
1470
1471 h = (struct elf32_mn10300_link_hash_entry *) hh;
1472
1473 if ((h->root.root.type == bfd_link_hash_defined
1474 || h->root.root.type == bfd_link_hash_defweak)
1475 && ( r_type == R_MN10300_GOTPC32
1476 || r_type == R_MN10300_GOTPC16
1477 || (( r_type == R_MN10300_PLT32
1478 || r_type == R_MN10300_PLT16)
1479 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
1480 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
1481 && h->root.plt.offset != (bfd_vma) -1)
1482 || (( r_type == R_MN10300_GOT32
1483 || r_type == R_MN10300_GOT24
1484 || r_type == R_MN10300_GOT16)
1485 && elf_hash_table (info)->dynamic_sections_created
1486 && !SYMBOL_REFERENCES_LOCAL (info, hh))
1487 || (r_type == R_MN10300_32
1488 /* _32 relocs in executables force _COPY relocs,
1489 such that the address of the symbol ends up
1490 being local. */
1491 && !info->executable
1492 && !SYMBOL_REFERENCES_LOCAL (info, hh)
1493 && ((input_section->flags & SEC_ALLOC) != 0
1494 /* DWARF will emit R_MN10300_32 relocations
1495 in its sections against symbols defined
1496 externally in shared libraries. We can't
1497 do anything with them here. */
1498 || ((input_section->flags & SEC_DEBUGGING) != 0
1499 && h->root.def_dynamic)))))
1500 /* In these cases, we don't need the relocation
1501 value. We check specially because in some
1502 obscure cases sec->output_section will be NULL. */
1503 relocation = 0;
1504
1505 else if (!info->relocatable && unresolved_reloc)
1506 (*_bfd_error_handler)
1507 (_("%B(%A+0x%lx): unresolvable %s relocation against symbol `%s'"),
1508 input_bfd,
1509 input_section,
1510 (long) rel->r_offset,
1511 howto->name,
1512 h->root.root.root.string);
1513 }
1514
1515 if (sec != NULL && elf_discarded_section (sec))
1516 {
1517 /* For relocs against symbols from removed linkonce sections,
1518 or sections discarded by a linker script, we just want the
1519 section contents zeroed. Avoid any special processing. */
1520 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
1521 rel->r_info = 0;
1522 rel->r_addend = 0;
1523 continue;
1524 }
1525
1526 if (info->relocatable)
1527 continue;
1528
1529 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
1530 input_section,
1531 contents, rel->r_offset,
1532 relocation, rel->r_addend,
1533 (struct elf_link_hash_entry *) h,
1534 r_symndx,
1535 info, sec, h == NULL);
1536
1537 if (r != bfd_reloc_ok)
1538 {
1539 const char *name;
1540 const char *msg = NULL;
1541
1542 if (h != NULL)
1543 name = h->root.root.root.string;
1544 else
1545 {
1546 name = (bfd_elf_string_from_elf_section
1547 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1548 if (name == NULL || *name == '\0')
1549 name = bfd_section_name (input_bfd, sec);
1550 }
1551
1552 switch (r)
1553 {
1554 case bfd_reloc_overflow:
1555 if (! ((*info->callbacks->reloc_overflow)
1556 (info, (h ? &h->root.root : NULL), name,
1557 howto->name, (bfd_vma) 0, input_bfd,
1558 input_section, rel->r_offset)))
1559 return FALSE;
1560 break;
1561
1562 case bfd_reloc_undefined:
1563 if (! ((*info->callbacks->undefined_symbol)
1564 (info, name, input_bfd, input_section,
1565 rel->r_offset, TRUE)))
1566 return FALSE;
1567 break;
1568
1569 case bfd_reloc_outofrange:
1570 msg = _("internal error: out of range error");
1571 goto common_error;
1572
1573 case bfd_reloc_notsupported:
1574 msg = _("internal error: unsupported relocation error");
1575 goto common_error;
1576
1577 case bfd_reloc_dangerous:
1578 if (r_type == R_MN10300_PCREL32)
1579 msg = _("error: inappropriate relocation type for shared"
1580 " library (did you forget -fpic?)");
1581 else
1582 msg = _("internal error: suspicious relocation type used"
1583 " in shared library");
1584 goto common_error;
1585
1586 default:
1587 msg = _("internal error: unknown error");
1588 /* Fall through. */
1589
1590 common_error:
1591 if (!((*info->callbacks->warning)
1592 (info, msg, name, input_bfd, input_section,
1593 rel->r_offset)))
1594 return FALSE;
1595 break;
1596 }
1597 }
1598 }
1599
1600 return TRUE;
1601 }
1602
1603 /* Finish initializing one hash table entry. */
1604
1605 static bfd_boolean
1606 elf32_mn10300_finish_hash_table_entry (struct bfd_hash_entry *gen_entry,
1607 void * in_args)
1608 {
1609 struct elf32_mn10300_link_hash_entry *entry;
1610 struct bfd_link_info *link_info = (struct bfd_link_info *) in_args;
1611 unsigned int byte_count = 0;
1612
1613 entry = (struct elf32_mn10300_link_hash_entry *) gen_entry;
1614
1615 if (entry->root.root.type == bfd_link_hash_warning)
1616 entry = (struct elf32_mn10300_link_hash_entry *) entry->root.root.u.i.link;
1617
1618 /* If we already know we want to convert "call" to "calls" for calls
1619 to this symbol, then return now. */
1620 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
1621 return TRUE;
1622
1623 /* If there are no named calls to this symbol, or there's nothing we
1624 can move from the function itself into the "call" instruction,
1625 then note that all "call" instructions should be converted into
1626 "calls" instructions and return. If a symbol is available for
1627 dynamic symbol resolution (overridable or overriding), avoid
1628 custom calling conventions. */
1629 if (entry->direct_calls == 0
1630 || (entry->stack_size == 0 && entry->movm_args == 0)
1631 || (elf_hash_table (link_info)->dynamic_sections_created
1632 && ELF_ST_VISIBILITY (entry->root.other) != STV_INTERNAL
1633 && ELF_ST_VISIBILITY (entry->root.other) != STV_HIDDEN))
1634 {
1635 /* Make a note that we should convert "call" instructions to "calls"
1636 instructions for calls to this symbol. */
1637 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1638 return TRUE;
1639 }
1640
1641 /* We may be able to move some instructions from the function itself into
1642 the "call" instruction. Count how many bytes we might be able to
1643 eliminate in the function itself. */
1644
1645 /* A movm instruction is two bytes. */
1646 if (entry->movm_args)
1647 byte_count += 2;
1648
1649 /* Count the insn to allocate stack space too. */
1650 if (entry->stack_size > 0)
1651 {
1652 if (entry->stack_size <= 128)
1653 byte_count += 3;
1654 else
1655 byte_count += 4;
1656 }
1657
1658 /* If using "call" will result in larger code, then turn all
1659 the associated "call" instructions into "calls" instructions. */
1660 if (byte_count < entry->direct_calls)
1661 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
1662
1663 /* This routine never fails. */
1664 return TRUE;
1665 }
1666
1667 /* Used to count hash table entries. */
1668
1669 static bfd_boolean
1670 elf32_mn10300_count_hash_table_entries (struct bfd_hash_entry *gen_entry ATTRIBUTE_UNUSED,
1671 void * in_args)
1672 {
1673 int *count = (int *) in_args;
1674
1675 (*count) ++;
1676 return TRUE;
1677 }
1678
1679 /* Used to enumerate hash table entries into a linear array. */
1680
1681 static bfd_boolean
1682 elf32_mn10300_list_hash_table_entries (struct bfd_hash_entry *gen_entry,
1683 void * in_args)
1684 {
1685 struct bfd_hash_entry ***ptr = (struct bfd_hash_entry ***) in_args;
1686
1687 **ptr = gen_entry;
1688 (*ptr) ++;
1689 return TRUE;
1690 }
1691
1692 /* Used to sort the array created by the above. */
1693
1694 static int
1695 sort_by_value (const void *va, const void *vb)
1696 {
1697 struct elf32_mn10300_link_hash_entry *a
1698 = *(struct elf32_mn10300_link_hash_entry **) va;
1699 struct elf32_mn10300_link_hash_entry *b
1700 = *(struct elf32_mn10300_link_hash_entry **) vb;
1701
1702 return a->value - b->value;
1703 }
1704
1705 /* Compute the stack size and movm arguments for the function
1706 referred to by HASH at address ADDR in section with
1707 contents CONTENTS, store the information in the hash table. */
1708
1709 static void
1710 compute_function_info (bfd *abfd,
1711 struct elf32_mn10300_link_hash_entry *hash,
1712 bfd_vma addr,
1713 unsigned char *contents)
1714 {
1715 unsigned char byte1, byte2;
1716 /* We only care about a very small subset of the possible prologue
1717 sequences here. Basically we look for:
1718
1719 movm [d2,d3,a2,a3],sp (optional)
1720 add <size>,sp (optional, and only for sizes which fit in an unsigned
1721 8 bit number)
1722
1723 If we find anything else, we quit. */
1724
1725 /* Look for movm [regs],sp. */
1726 byte1 = bfd_get_8 (abfd, contents + addr);
1727 byte2 = bfd_get_8 (abfd, contents + addr + 1);
1728
1729 if (byte1 == 0xcf)
1730 {
1731 hash->movm_args = byte2;
1732 addr += 2;
1733 byte1 = bfd_get_8 (abfd, contents + addr);
1734 byte2 = bfd_get_8 (abfd, contents + addr + 1);
1735 }
1736
1737 /* Now figure out how much stack space will be allocated by the movm
1738 instruction. We need this kept separate from the function's normal
1739 stack space. */
1740 if (hash->movm_args)
1741 {
1742 /* Space for d2. */
1743 if (hash->movm_args & 0x80)
1744 hash->movm_stack_size += 4;
1745
1746 /* Space for d3. */
1747 if (hash->movm_args & 0x40)
1748 hash->movm_stack_size += 4;
1749
1750 /* Space for a2. */
1751 if (hash->movm_args & 0x20)
1752 hash->movm_stack_size += 4;
1753
1754 /* Space for a3. */
1755 if (hash->movm_args & 0x10)
1756 hash->movm_stack_size += 4;
1757
1758 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
1759 if (hash->movm_args & 0x08)
1760 hash->movm_stack_size += 8 * 4;
1761
1762 if (bfd_get_mach (abfd) == bfd_mach_am33
1763 || bfd_get_mach (abfd) == bfd_mach_am33_2)
1764 {
1765 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
1766 if (hash->movm_args & 0x1)
1767 hash->movm_stack_size += 6 * 4;
1768
1769 /* exreg1 space. e4, e5, e6, e7 */
1770 if (hash->movm_args & 0x2)
1771 hash->movm_stack_size += 4 * 4;
1772
1773 /* exreg0 space. e2, e3 */
1774 if (hash->movm_args & 0x4)
1775 hash->movm_stack_size += 2 * 4;
1776 }
1777 }
1778
1779 /* Now look for the two stack adjustment variants. */
1780 if (byte1 == 0xf8 && byte2 == 0xfe)
1781 {
1782 int temp = bfd_get_8 (abfd, contents + addr + 2);
1783 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
1784
1785 hash->stack_size = -temp;
1786 }
1787 else if (byte1 == 0xfa && byte2 == 0xfe)
1788 {
1789 int temp = bfd_get_16 (abfd, contents + addr + 2);
1790 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
1791 temp = -temp;
1792
1793 if (temp < 255)
1794 hash->stack_size = temp;
1795 }
1796
1797 /* If the total stack to be allocated by the call instruction is more
1798 than 255 bytes, then we can't remove the stack adjustment by using
1799 "call" (we might still be able to remove the "movm" instruction. */
1800 if (hash->stack_size + hash->movm_stack_size > 255)
1801 hash->stack_size = 0;
1802 }
1803
1804 /* Delete some bytes from a section while relaxing. */
1805
1806 static bfd_boolean
1807 mn10300_elf_relax_delete_bytes (bfd *abfd,
1808 asection *sec,
1809 bfd_vma addr,
1810 int count)
1811 {
1812 Elf_Internal_Shdr *symtab_hdr;
1813 unsigned int sec_shndx;
1814 bfd_byte *contents;
1815 Elf_Internal_Rela *irel, *irelend;
1816 Elf_Internal_Rela *irelalign;
1817 bfd_vma toaddr;
1818 Elf_Internal_Sym *isym, *isymend;
1819 struct elf_link_hash_entry **sym_hashes;
1820 struct elf_link_hash_entry **end_hashes;
1821 unsigned int symcount;
1822
1823 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1824
1825 contents = elf_section_data (sec)->this_hdr.contents;
1826
1827 irelalign = NULL;
1828 toaddr = sec->size;
1829
1830 irel = elf_section_data (sec)->relocs;
1831 irelend = irel + sec->reloc_count;
1832
1833 if (sec->reloc_count > 0)
1834 {
1835 /* If there is an align reloc at the end of the section ignore it.
1836 GAS creates these relocs for reasons of its own, and they just
1837 serve to keep the section artifically inflated. */
1838 if (ELF32_R_TYPE ((irelend - 1)->r_info) == (int) R_MN10300_ALIGN)
1839 --irelend;
1840
1841 /* The deletion must stop at the next ALIGN reloc for an aligment
1842 power larger than, or not a multiple of, the number of bytes we
1843 are deleting. */
1844 for (; irel < irelend; irel++)
1845 {
1846 int alignment = 1 << irel->r_addend;
1847
1848 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1849 && irel->r_offset > addr
1850 && irel->r_offset < toaddr
1851 && (count < alignment
1852 || alignment % count != 0))
1853 {
1854 irelalign = irel;
1855 toaddr = irel->r_offset;
1856 break;
1857 }
1858 }
1859 }
1860
1861 /* Actually delete the bytes. */
1862 memmove (contents + addr, contents + addr + count,
1863 (size_t) (toaddr - addr - count));
1864
1865 /* Adjust the section's size if we are shrinking it, or else
1866 pad the bytes between the end of the shrunken region and
1867 the start of the next region with NOP codes. */
1868 if (irelalign == NULL)
1869 {
1870 sec->size -= count;
1871 /* Include symbols at the end of the section, but
1872 not at the end of a sub-region of the section. */
1873 toaddr ++;
1874 }
1875 else
1876 {
1877 int i;
1878
1879 #define NOP_OPCODE 0xcb
1880
1881 for (i = 0; i < count; i ++)
1882 bfd_put_8 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
1883 }
1884
1885 /* Adjust all the relocs. */
1886 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1887 {
1888 /* Get the new reloc address. */
1889 if ((irel->r_offset > addr
1890 && irel->r_offset < toaddr)
1891 || (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN
1892 && irel->r_offset == toaddr))
1893 irel->r_offset -= count;
1894 }
1895
1896 /* Adjust the local symbols in the section, reducing their value
1897 by the number of bytes deleted. Note - symbols within the deleted
1898 region are moved to the address of the start of the region, which
1899 actually means that they will address the byte beyond the end of
1900 the region once the deletion has been completed. */
1901 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1902 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1903 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1904 {
1905 if (isym->st_shndx == sec_shndx
1906 && isym->st_value > addr
1907 && isym->st_value < toaddr)
1908 {
1909 if (isym->st_value < addr + count)
1910 isym->st_value = addr;
1911 else
1912 isym->st_value -= count;
1913 }
1914 /* Adjust the function symbol's size as well. */
1915 else if (isym->st_shndx == sec_shndx
1916 && ELF_ST_TYPE (isym->st_info) == STT_FUNC
1917 && isym->st_value + isym->st_size > addr
1918 && isym->st_value + isym->st_size < toaddr)
1919 isym->st_size -= count;
1920 }
1921
1922 /* Now adjust the global symbols defined in this section. */
1923 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1924 - symtab_hdr->sh_info);
1925 sym_hashes = elf_sym_hashes (abfd);
1926 end_hashes = sym_hashes + symcount;
1927 for (; sym_hashes < end_hashes; sym_hashes++)
1928 {
1929 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1930
1931 if ((sym_hash->root.type == bfd_link_hash_defined
1932 || sym_hash->root.type == bfd_link_hash_defweak)
1933 && sym_hash->root.u.def.section == sec
1934 && sym_hash->root.u.def.value > addr
1935 && sym_hash->root.u.def.value < toaddr)
1936 {
1937 if (sym_hash->root.u.def.value < addr + count)
1938 sym_hash->root.u.def.value = addr;
1939 else
1940 sym_hash->root.u.def.value -= count;
1941 }
1942 /* Adjust the function symbol's size as well. */
1943 else if (sym_hash->root.type == bfd_link_hash_defined
1944 && sym_hash->root.u.def.section == sec
1945 && sym_hash->type == STT_FUNC
1946 && sym_hash->root.u.def.value + sym_hash->size > addr
1947 && sym_hash->root.u.def.value + sym_hash->size < toaddr)
1948 sym_hash->size -= count;
1949 }
1950
1951 /* See if we can move the ALIGN reloc forward.
1952 We have adjusted r_offset for it already. */
1953 if (irelalign != NULL)
1954 {
1955 bfd_vma alignto, alignaddr;
1956
1957 if ((int) irelalign->r_addend > 0)
1958 {
1959 /* This is the old address. */
1960 alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1961 /* This is where the align points to now. */
1962 alignaddr = BFD_ALIGN (irelalign->r_offset,
1963 1 << irelalign->r_addend);
1964 if (alignaddr < alignto)
1965 /* Tail recursion. */
1966 return mn10300_elf_relax_delete_bytes (abfd, sec, alignaddr,
1967 (int) (alignto - alignaddr));
1968 }
1969 }
1970
1971 return TRUE;
1972 }
1973
1974 /* Return TRUE if a symbol exists at the given address, else return
1975 FALSE. */
1976
1977 static bfd_boolean
1978 mn10300_elf_symbol_address_p (bfd *abfd,
1979 asection *sec,
1980 Elf_Internal_Sym *isym,
1981 bfd_vma addr)
1982 {
1983 Elf_Internal_Shdr *symtab_hdr;
1984 unsigned int sec_shndx;
1985 Elf_Internal_Sym *isymend;
1986 struct elf_link_hash_entry **sym_hashes;
1987 struct elf_link_hash_entry **end_hashes;
1988 unsigned int symcount;
1989
1990 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1991
1992 /* Examine all the symbols. */
1993 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1994 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
1995 if (isym->st_shndx == sec_shndx
1996 && isym->st_value == addr)
1997 return TRUE;
1998
1999 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2000 - symtab_hdr->sh_info);
2001 sym_hashes = elf_sym_hashes (abfd);
2002 end_hashes = sym_hashes + symcount;
2003 for (; sym_hashes < end_hashes; sym_hashes++)
2004 {
2005 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2006
2007 if ((sym_hash->root.type == bfd_link_hash_defined
2008 || sym_hash->root.type == bfd_link_hash_defweak)
2009 && sym_hash->root.u.def.section == sec
2010 && sym_hash->root.u.def.value == addr)
2011 return TRUE;
2012 }
2013
2014 return FALSE;
2015 }
2016
2017 /* This function handles relaxing for the mn10300.
2018
2019 There are quite a few relaxing opportunities available on the mn10300:
2020
2021 * calls:32 -> calls:16 2 bytes
2022 * call:32 -> call:16 2 bytes
2023
2024 * call:32 -> calls:32 1 byte
2025 * call:16 -> calls:16 1 byte
2026 * These are done anytime using "calls" would result
2027 in smaller code, or when necessary to preserve the
2028 meaning of the program.
2029
2030 * call:32 varies
2031 * call:16
2032 * In some circumstances we can move instructions
2033 from a function prologue into a "call" instruction.
2034 This is only done if the resulting code is no larger
2035 than the original code.
2036
2037 * jmp:32 -> jmp:16 2 bytes
2038 * jmp:16 -> bra:8 1 byte
2039
2040 * If the previous instruction is a conditional branch
2041 around the jump/bra, we may be able to reverse its condition
2042 and change its target to the jump's target. The jump/bra
2043 can then be deleted. 2 bytes
2044
2045 * mov abs32 -> mov abs16 1 or 2 bytes
2046
2047 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
2048 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
2049
2050 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
2051 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
2052
2053 We don't handle imm16->imm8 or d16->d8 as they're very rare
2054 and somewhat more difficult to support. */
2055
2056 static bfd_boolean
2057 mn10300_elf_relax_section (bfd *abfd,
2058 asection *sec,
2059 struct bfd_link_info *link_info,
2060 bfd_boolean *again)
2061 {
2062 Elf_Internal_Shdr *symtab_hdr;
2063 Elf_Internal_Rela *internal_relocs = NULL;
2064 Elf_Internal_Rela *irel, *irelend;
2065 bfd_byte *contents = NULL;
2066 Elf_Internal_Sym *isymbuf = NULL;
2067 struct elf32_mn10300_link_hash_table *hash_table;
2068 asection *section = sec;
2069 bfd_vma align_gap_adjustment;
2070
2071 if (link_info->relocatable)
2072 (*link_info->callbacks->einfo)
2073 (_("%P%F: --relax and -r may not be used together\n"));
2074
2075 /* Assume nothing changes. */
2076 *again = FALSE;
2077
2078 /* We need a pointer to the mn10300 specific hash table. */
2079 hash_table = elf32_mn10300_hash_table (link_info);
2080 if (hash_table == NULL)
2081 return FALSE;
2082
2083 /* Initialize fields in each hash table entry the first time through. */
2084 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
2085 {
2086 bfd *input_bfd;
2087
2088 /* Iterate over all the input bfds. */
2089 for (input_bfd = link_info->input_bfds;
2090 input_bfd != NULL;
2091 input_bfd = input_bfd->link_next)
2092 {
2093 /* We're going to need all the symbols for each bfd. */
2094 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2095 if (symtab_hdr->sh_info != 0)
2096 {
2097 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2098 if (isymbuf == NULL)
2099 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2100 symtab_hdr->sh_info, 0,
2101 NULL, NULL, NULL);
2102 if (isymbuf == NULL)
2103 goto error_return;
2104 }
2105
2106 /* Iterate over each section in this bfd. */
2107 for (section = input_bfd->sections;
2108 section != NULL;
2109 section = section->next)
2110 {
2111 struct elf32_mn10300_link_hash_entry *hash;
2112 Elf_Internal_Sym *sym;
2113 asection *sym_sec = NULL;
2114 const char *sym_name;
2115 char *new_name;
2116
2117 /* If there's nothing to do in this section, skip it. */
2118 if (! ((section->flags & SEC_RELOC) != 0
2119 && section->reloc_count != 0))
2120 continue;
2121 if ((section->flags & SEC_ALLOC) == 0)
2122 continue;
2123
2124 /* Get cached copy of section contents if it exists. */
2125 if (elf_section_data (section)->this_hdr.contents != NULL)
2126 contents = elf_section_data (section)->this_hdr.contents;
2127 else if (section->size != 0)
2128 {
2129 /* Go get them off disk. */
2130 if (!bfd_malloc_and_get_section (input_bfd, section,
2131 &contents))
2132 goto error_return;
2133 }
2134 else
2135 contents = NULL;
2136
2137 /* If there aren't any relocs, then there's nothing to do. */
2138 if ((section->flags & SEC_RELOC) != 0
2139 && section->reloc_count != 0)
2140 {
2141 /* Get a copy of the native relocations. */
2142 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2143 NULL, NULL,
2144 link_info->keep_memory);
2145 if (internal_relocs == NULL)
2146 goto error_return;
2147
2148 /* Now examine each relocation. */
2149 irel = internal_relocs;
2150 irelend = irel + section->reloc_count;
2151 for (; irel < irelend; irel++)
2152 {
2153 long r_type;
2154 unsigned long r_index;
2155 unsigned char code;
2156
2157 r_type = ELF32_R_TYPE (irel->r_info);
2158 r_index = ELF32_R_SYM (irel->r_info);
2159
2160 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
2161 goto error_return;
2162
2163 /* We need the name and hash table entry of the target
2164 symbol! */
2165 hash = NULL;
2166 sym = NULL;
2167 sym_sec = NULL;
2168
2169 if (r_index < symtab_hdr->sh_info)
2170 {
2171 /* A local symbol. */
2172 Elf_Internal_Sym *isym;
2173 struct elf_link_hash_table *elftab;
2174 bfd_size_type amt;
2175
2176 isym = isymbuf + r_index;
2177 if (isym->st_shndx == SHN_UNDEF)
2178 sym_sec = bfd_und_section_ptr;
2179 else if (isym->st_shndx == SHN_ABS)
2180 sym_sec = bfd_abs_section_ptr;
2181 else if (isym->st_shndx == SHN_COMMON)
2182 sym_sec = bfd_com_section_ptr;
2183 else
2184 sym_sec
2185 = bfd_section_from_elf_index (input_bfd,
2186 isym->st_shndx);
2187
2188 sym_name
2189 = bfd_elf_string_from_elf_section (input_bfd,
2190 (symtab_hdr
2191 ->sh_link),
2192 isym->st_name);
2193
2194 /* If it isn't a function, then we don't care
2195 about it. */
2196 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
2197 continue;
2198
2199 /* Tack on an ID so we can uniquely identify this
2200 local symbol in the global hash table. */
2201 amt = strlen (sym_name) + 10;
2202 new_name = bfd_malloc (amt);
2203 if (new_name == NULL)
2204 goto error_return;
2205
2206 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2207 sym_name = new_name;
2208
2209 elftab = &hash_table->static_hash_table->root;
2210 hash = ((struct elf32_mn10300_link_hash_entry *)
2211 elf_link_hash_lookup (elftab, sym_name,
2212 TRUE, TRUE, FALSE));
2213 free (new_name);
2214 }
2215 else
2216 {
2217 r_index -= symtab_hdr->sh_info;
2218 hash = (struct elf32_mn10300_link_hash_entry *)
2219 elf_sym_hashes (input_bfd)[r_index];
2220 }
2221
2222 sym_name = hash->root.root.root.string;
2223 if ((section->flags & SEC_CODE) != 0)
2224 {
2225 /* If this is not a "call" instruction, then we
2226 should convert "call" instructions to "calls"
2227 instructions. */
2228 code = bfd_get_8 (input_bfd,
2229 contents + irel->r_offset - 1);
2230 if (code != 0xdd && code != 0xcd)
2231 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2232 }
2233
2234 /* If this is a jump/call, then bump the
2235 direct_calls counter. Else force "call" to
2236 "calls" conversions. */
2237 if (r_type == R_MN10300_PCREL32
2238 || r_type == R_MN10300_PLT32
2239 || r_type == R_MN10300_PLT16
2240 || r_type == R_MN10300_PCREL16)
2241 hash->direct_calls++;
2242 else
2243 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
2244 }
2245 }
2246
2247 /* Now look at the actual contents to get the stack size,
2248 and a list of what registers were saved in the prologue
2249 (ie movm_args). */
2250 if ((section->flags & SEC_CODE) != 0)
2251 {
2252 Elf_Internal_Sym *isym, *isymend;
2253 unsigned int sec_shndx;
2254 struct elf_link_hash_entry **hashes;
2255 struct elf_link_hash_entry **end_hashes;
2256 unsigned int symcount;
2257
2258 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2259 section);
2260
2261 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2262 - symtab_hdr->sh_info);
2263 hashes = elf_sym_hashes (input_bfd);
2264 end_hashes = hashes + symcount;
2265
2266 /* Look at each function defined in this section and
2267 update info for that function. */
2268 isymend = isymbuf + symtab_hdr->sh_info;
2269 for (isym = isymbuf; isym < isymend; isym++)
2270 {
2271 if (isym->st_shndx == sec_shndx
2272 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
2273 {
2274 struct elf_link_hash_table *elftab;
2275 bfd_size_type amt;
2276 struct elf_link_hash_entry **lhashes = hashes;
2277
2278 /* Skip a local symbol if it aliases a
2279 global one. */
2280 for (; lhashes < end_hashes; lhashes++)
2281 {
2282 hash = (struct elf32_mn10300_link_hash_entry *) *lhashes;
2283 if ((hash->root.root.type == bfd_link_hash_defined
2284 || hash->root.root.type == bfd_link_hash_defweak)
2285 && hash->root.root.u.def.section == section
2286 && hash->root.type == STT_FUNC
2287 && hash->root.root.u.def.value == isym->st_value)
2288 break;
2289 }
2290 if (lhashes != end_hashes)
2291 continue;
2292
2293 if (isym->st_shndx == SHN_UNDEF)
2294 sym_sec = bfd_und_section_ptr;
2295 else if (isym->st_shndx == SHN_ABS)
2296 sym_sec = bfd_abs_section_ptr;
2297 else if (isym->st_shndx == SHN_COMMON)
2298 sym_sec = bfd_com_section_ptr;
2299 else
2300 sym_sec
2301 = bfd_section_from_elf_index (input_bfd,
2302 isym->st_shndx);
2303
2304 sym_name = (bfd_elf_string_from_elf_section
2305 (input_bfd, symtab_hdr->sh_link,
2306 isym->st_name));
2307
2308 /* Tack on an ID so we can uniquely identify this
2309 local symbol in the global hash table. */
2310 amt = strlen (sym_name) + 10;
2311 new_name = bfd_malloc (amt);
2312 if (new_name == NULL)
2313 goto error_return;
2314
2315 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2316 sym_name = new_name;
2317
2318 elftab = &hash_table->static_hash_table->root;
2319 hash = ((struct elf32_mn10300_link_hash_entry *)
2320 elf_link_hash_lookup (elftab, sym_name,
2321 TRUE, TRUE, FALSE));
2322 free (new_name);
2323 compute_function_info (input_bfd, hash,
2324 isym->st_value, contents);
2325 hash->value = isym->st_value;
2326 }
2327 }
2328
2329 for (; hashes < end_hashes; hashes++)
2330 {
2331 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2332 if ((hash->root.root.type == bfd_link_hash_defined
2333 || hash->root.root.type == bfd_link_hash_defweak)
2334 && hash->root.root.u.def.section == section
2335 && hash->root.type == STT_FUNC)
2336 compute_function_info (input_bfd, hash,
2337 (hash)->root.root.u.def.value,
2338 contents);
2339 }
2340 }
2341
2342 /* Cache or free any memory we allocated for the relocs. */
2343 if (internal_relocs != NULL
2344 && elf_section_data (section)->relocs != internal_relocs)
2345 free (internal_relocs);
2346 internal_relocs = NULL;
2347
2348 /* Cache or free any memory we allocated for the contents. */
2349 if (contents != NULL
2350 && elf_section_data (section)->this_hdr.contents != contents)
2351 {
2352 if (! link_info->keep_memory)
2353 free (contents);
2354 else
2355 {
2356 /* Cache the section contents for elf_link_input_bfd. */
2357 elf_section_data (section)->this_hdr.contents = contents;
2358 }
2359 }
2360 contents = NULL;
2361 }
2362
2363 /* Cache or free any memory we allocated for the symbols. */
2364 if (isymbuf != NULL
2365 && symtab_hdr->contents != (unsigned char *) isymbuf)
2366 {
2367 if (! link_info->keep_memory)
2368 free (isymbuf);
2369 else
2370 {
2371 /* Cache the symbols for elf_link_input_bfd. */
2372 symtab_hdr->contents = (unsigned char *) isymbuf;
2373 }
2374 }
2375 isymbuf = NULL;
2376 }
2377
2378 /* Now iterate on each symbol in the hash table and perform
2379 the final initialization steps on each. */
2380 elf32_mn10300_link_hash_traverse (hash_table,
2381 elf32_mn10300_finish_hash_table_entry,
2382 link_info);
2383 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2384 elf32_mn10300_finish_hash_table_entry,
2385 link_info);
2386
2387 {
2388 /* This section of code collects all our local symbols, sorts
2389 them by value, and looks for multiple symbols referring to
2390 the same address. For those symbols, the flags are merged.
2391 At this point, the only flag that can be set is
2392 MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2393 together. */
2394 int static_count = 0, i;
2395 struct elf32_mn10300_link_hash_entry **entries;
2396 struct elf32_mn10300_link_hash_entry **ptr;
2397
2398 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2399 elf32_mn10300_count_hash_table_entries,
2400 &static_count);
2401
2402 entries = bfd_malloc (static_count * sizeof (* ptr));
2403
2404 ptr = entries;
2405 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2406 elf32_mn10300_list_hash_table_entries,
2407 & ptr);
2408
2409 qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
2410
2411 for (i = 0; i < static_count - 1; i++)
2412 if (entries[i]->value && entries[i]->value == entries[i+1]->value)
2413 {
2414 int v = entries[i]->flags;
2415 int j;
2416
2417 for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
2418 v |= entries[j]->flags;
2419
2420 for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
2421 entries[j]->flags = v;
2422
2423 i = j - 1;
2424 }
2425 }
2426
2427 /* All entries in the hash table are fully initialized. */
2428 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
2429
2430 /* Now that everything has been initialized, go through each
2431 code section and delete any prologue insns which will be
2432 redundant because their operations will be performed by
2433 a "call" instruction. */
2434 for (input_bfd = link_info->input_bfds;
2435 input_bfd != NULL;
2436 input_bfd = input_bfd->link_next)
2437 {
2438 /* We're going to need all the local symbols for each bfd. */
2439 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2440 if (symtab_hdr->sh_info != 0)
2441 {
2442 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2443 if (isymbuf == NULL)
2444 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2445 symtab_hdr->sh_info, 0,
2446 NULL, NULL, NULL);
2447 if (isymbuf == NULL)
2448 goto error_return;
2449 }
2450
2451 /* Walk over each section in this bfd. */
2452 for (section = input_bfd->sections;
2453 section != NULL;
2454 section = section->next)
2455 {
2456 unsigned int sec_shndx;
2457 Elf_Internal_Sym *isym, *isymend;
2458 struct elf_link_hash_entry **hashes;
2459 struct elf_link_hash_entry **end_hashes;
2460 unsigned int symcount;
2461
2462 /* Skip non-code sections and empty sections. */
2463 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
2464 continue;
2465
2466 if (section->reloc_count != 0)
2467 {
2468 /* Get a copy of the native relocations. */
2469 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
2470 NULL, NULL,
2471 link_info->keep_memory);
2472 if (internal_relocs == NULL)
2473 goto error_return;
2474 }
2475
2476 /* Get cached copy of section contents if it exists. */
2477 if (elf_section_data (section)->this_hdr.contents != NULL)
2478 contents = elf_section_data (section)->this_hdr.contents;
2479 else
2480 {
2481 /* Go get them off disk. */
2482 if (!bfd_malloc_and_get_section (input_bfd, section,
2483 &contents))
2484 goto error_return;
2485 }
2486
2487 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
2488 section);
2489
2490 /* Now look for any function in this section which needs
2491 insns deleted from its prologue. */
2492 isymend = isymbuf + symtab_hdr->sh_info;
2493 for (isym = isymbuf; isym < isymend; isym++)
2494 {
2495 struct elf32_mn10300_link_hash_entry *sym_hash;
2496 asection *sym_sec = NULL;
2497 const char *sym_name;
2498 char *new_name;
2499 struct elf_link_hash_table *elftab;
2500 bfd_size_type amt;
2501
2502 if (isym->st_shndx != sec_shndx)
2503 continue;
2504
2505 if (isym->st_shndx == SHN_UNDEF)
2506 sym_sec = bfd_und_section_ptr;
2507 else if (isym->st_shndx == SHN_ABS)
2508 sym_sec = bfd_abs_section_ptr;
2509 else if (isym->st_shndx == SHN_COMMON)
2510 sym_sec = bfd_com_section_ptr;
2511 else
2512 sym_sec
2513 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2514
2515 sym_name
2516 = bfd_elf_string_from_elf_section (input_bfd,
2517 symtab_hdr->sh_link,
2518 isym->st_name);
2519
2520 /* Tack on an ID so we can uniquely identify this
2521 local symbol in the global hash table. */
2522 amt = strlen (sym_name) + 10;
2523 new_name = bfd_malloc (amt);
2524 if (new_name == NULL)
2525 goto error_return;
2526 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2527 sym_name = new_name;
2528
2529 elftab = & hash_table->static_hash_table->root;
2530 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2531 elf_link_hash_lookup (elftab, sym_name,
2532 FALSE, FALSE, FALSE);
2533
2534 free (new_name);
2535 if (sym_hash == NULL)
2536 continue;
2537
2538 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2539 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2540 {
2541 int bytes = 0;
2542
2543 /* Note that we've changed things. */
2544 elf_section_data (section)->relocs = internal_relocs;
2545 elf_section_data (section)->this_hdr.contents = contents;
2546 symtab_hdr->contents = (unsigned char *) isymbuf;
2547
2548 /* Count how many bytes we're going to delete. */
2549 if (sym_hash->movm_args)
2550 bytes += 2;
2551
2552 if (sym_hash->stack_size > 0)
2553 {
2554 if (sym_hash->stack_size <= 128)
2555 bytes += 3;
2556 else
2557 bytes += 4;
2558 }
2559
2560 /* Note that we've deleted prologue bytes for this
2561 function. */
2562 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2563
2564 /* Actually delete the bytes. */
2565 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2566 section,
2567 isym->st_value,
2568 bytes))
2569 goto error_return;
2570
2571 /* Something changed. Not strictly necessary, but
2572 may lead to more relaxing opportunities. */
2573 *again = TRUE;
2574 }
2575 }
2576
2577 /* Look for any global functions in this section which
2578 need insns deleted from their prologues. */
2579 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2580 - symtab_hdr->sh_info);
2581 hashes = elf_sym_hashes (input_bfd);
2582 end_hashes = hashes + symcount;
2583 for (; hashes < end_hashes; hashes++)
2584 {
2585 struct elf32_mn10300_link_hash_entry *sym_hash;
2586
2587 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
2588 if ((sym_hash->root.root.type == bfd_link_hash_defined
2589 || sym_hash->root.root.type == bfd_link_hash_defweak)
2590 && sym_hash->root.root.u.def.section == section
2591 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
2592 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
2593 {
2594 int bytes = 0;
2595 bfd_vma symval;
2596
2597 /* Note that we've changed things. */
2598 elf_section_data (section)->relocs = internal_relocs;
2599 elf_section_data (section)->this_hdr.contents = contents;
2600 symtab_hdr->contents = (unsigned char *) isymbuf;
2601
2602 /* Count how many bytes we're going to delete. */
2603 if (sym_hash->movm_args)
2604 bytes += 2;
2605
2606 if (sym_hash->stack_size > 0)
2607 {
2608 if (sym_hash->stack_size <= 128)
2609 bytes += 3;
2610 else
2611 bytes += 4;
2612 }
2613
2614 /* Note that we've deleted prologue bytes for this
2615 function. */
2616 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
2617
2618 /* Actually delete the bytes. */
2619 symval = sym_hash->root.root.u.def.value;
2620 if (!mn10300_elf_relax_delete_bytes (input_bfd,
2621 section,
2622 symval,
2623 bytes))
2624 goto error_return;
2625
2626 /* Something changed. Not strictly necessary, but
2627 may lead to more relaxing opportunities. */
2628 *again = TRUE;
2629 }
2630 }
2631
2632 /* Cache or free any memory we allocated for the relocs. */
2633 if (internal_relocs != NULL
2634 && elf_section_data (section)->relocs != internal_relocs)
2635 free (internal_relocs);
2636 internal_relocs = NULL;
2637
2638 /* Cache or free any memory we allocated for the contents. */
2639 if (contents != NULL
2640 && elf_section_data (section)->this_hdr.contents != contents)
2641 {
2642 if (! link_info->keep_memory)
2643 free (contents);
2644 else
2645 /* Cache the section contents for elf_link_input_bfd. */
2646 elf_section_data (section)->this_hdr.contents = contents;
2647 }
2648 contents = NULL;
2649 }
2650
2651 /* Cache or free any memory we allocated for the symbols. */
2652 if (isymbuf != NULL
2653 && symtab_hdr->contents != (unsigned char *) isymbuf)
2654 {
2655 if (! link_info->keep_memory)
2656 free (isymbuf);
2657 else
2658 /* Cache the symbols for elf_link_input_bfd. */
2659 symtab_hdr->contents = (unsigned char *) isymbuf;
2660 }
2661 isymbuf = NULL;
2662 }
2663 }
2664
2665 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
2666 contents = NULL;
2667 internal_relocs = NULL;
2668 isymbuf = NULL;
2669 /* For error_return. */
2670 section = sec;
2671
2672 /* We don't have to do anything for a relocatable link, if
2673 this section does not have relocs, or if this is not a
2674 code section. */
2675 if (link_info->relocatable
2676 || (sec->flags & SEC_RELOC) == 0
2677 || sec->reloc_count == 0
2678 || (sec->flags & SEC_CODE) == 0)
2679 return TRUE;
2680
2681 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2682
2683 /* Get a copy of the native relocations. */
2684 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
2685 link_info->keep_memory);
2686 if (internal_relocs == NULL)
2687 goto error_return;
2688
2689 /* Scan for worst case alignment gap changes. Note that this logic
2690 is not ideal; what we should do is run this scan for every
2691 opcode/address range and adjust accordingly, but that's
2692 expensive. Worst case is that for an alignment of N bytes, we
2693 move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
2694 all before it. Plus, this still doesn't cover cross-section
2695 jumps with section alignment. */
2696 irelend = internal_relocs + sec->reloc_count;
2697 align_gap_adjustment = 0;
2698 for (irel = internal_relocs; irel < irelend; irel++)
2699 {
2700 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
2701 {
2702 bfd_vma adj = 1 << irel->r_addend;
2703 bfd_vma aend = irel->r_offset;
2704
2705 aend = BFD_ALIGN (aend, 1 << irel->r_addend);
2706 adj = 2 * adj - adj - 1;
2707
2708 /* Record the biggest adjustmnet. Skip any alignment at the
2709 end of our section. */
2710 if (align_gap_adjustment < adj
2711 && aend < sec->output_section->vma + sec->output_offset + sec->size)
2712 align_gap_adjustment = adj;
2713 }
2714 }
2715
2716 /* Walk through them looking for relaxing opportunities. */
2717 irelend = internal_relocs + sec->reloc_count;
2718 for (irel = internal_relocs; irel < irelend; irel++)
2719 {
2720 bfd_vma symval;
2721 bfd_signed_vma jump_offset;
2722 asection *sym_sec = NULL;
2723 struct elf32_mn10300_link_hash_entry *h = NULL;
2724
2725 /* If this isn't something that can be relaxed, then ignore
2726 this reloc. */
2727 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
2728 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
2729 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
2730 continue;
2731
2732 /* Get the section contents if we haven't done so already. */
2733 if (contents == NULL)
2734 {
2735 /* Get cached copy if it exists. */
2736 if (elf_section_data (sec)->this_hdr.contents != NULL)
2737 contents = elf_section_data (sec)->this_hdr.contents;
2738 else
2739 {
2740 /* Go get them off disk. */
2741 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
2742 goto error_return;
2743 }
2744 }
2745
2746 /* Read this BFD's symbols if we haven't done so already. */
2747 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
2748 {
2749 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2750 if (isymbuf == NULL)
2751 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
2752 symtab_hdr->sh_info, 0,
2753 NULL, NULL, NULL);
2754 if (isymbuf == NULL)
2755 goto error_return;
2756 }
2757
2758 /* Get the value of the symbol referred to by the reloc. */
2759 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
2760 {
2761 Elf_Internal_Sym *isym;
2762 const char *sym_name;
2763 char *new_name;
2764
2765 /* A local symbol. */
2766 isym = isymbuf + ELF32_R_SYM (irel->r_info);
2767 if (isym->st_shndx == SHN_UNDEF)
2768 sym_sec = bfd_und_section_ptr;
2769 else if (isym->st_shndx == SHN_ABS)
2770 sym_sec = bfd_abs_section_ptr;
2771 else if (isym->st_shndx == SHN_COMMON)
2772 sym_sec = bfd_com_section_ptr;
2773 else
2774 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
2775
2776 sym_name = bfd_elf_string_from_elf_section (abfd,
2777 symtab_hdr->sh_link,
2778 isym->st_name);
2779
2780 if ((sym_sec->flags & SEC_MERGE)
2781 && sym_sec->sec_info_type == ELF_INFO_TYPE_MERGE)
2782 {
2783 symval = isym->st_value;
2784
2785 /* GAS may reduce relocations against symbols in SEC_MERGE
2786 sections to a relocation against the section symbol when
2787 the original addend was zero. When the reloc is against
2788 a section symbol we should include the addend in the
2789 offset passed to _bfd_merged_section_offset, since the
2790 location of interest is the original symbol. On the
2791 other hand, an access to "sym+addend" where "sym" is not
2792 a section symbol should not include the addend; Such an
2793 access is presumed to be an offset from "sym"; The
2794 location of interest is just "sym". */
2795 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
2796 symval += irel->r_addend;
2797
2798 symval = _bfd_merged_section_offset (abfd, & sym_sec,
2799 elf_section_data (sym_sec)->sec_info,
2800 symval);
2801
2802 if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
2803 symval += irel->r_addend;
2804
2805 symval += sym_sec->output_section->vma
2806 + sym_sec->output_offset - irel->r_addend;
2807 }
2808 else
2809 symval = (isym->st_value
2810 + sym_sec->output_section->vma
2811 + sym_sec->output_offset);
2812
2813 /* Tack on an ID so we can uniquely identify this
2814 local symbol in the global hash table. */
2815 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
2816 if (new_name == NULL)
2817 goto error_return;
2818 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
2819 sym_name = new_name;
2820
2821 h = (struct elf32_mn10300_link_hash_entry *)
2822 elf_link_hash_lookup (&hash_table->static_hash_table->root,
2823 sym_name, FALSE, FALSE, FALSE);
2824 free (new_name);
2825 }
2826 else
2827 {
2828 unsigned long indx;
2829
2830 /* An external symbol. */
2831 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
2832 h = (struct elf32_mn10300_link_hash_entry *)
2833 (elf_sym_hashes (abfd)[indx]);
2834 BFD_ASSERT (h != NULL);
2835 if (h->root.root.type != bfd_link_hash_defined
2836 && h->root.root.type != bfd_link_hash_defweak)
2837 /* This appears to be a reference to an undefined
2838 symbol. Just ignore it--it will be caught by the
2839 regular reloc processing. */
2840 continue;
2841
2842 /* Check for a reference to a discarded symbol and ignore it. */
2843 if (h->root.root.u.def.section->output_section == NULL)
2844 continue;
2845
2846 sym_sec = h->root.root.u.def.section->output_section;
2847
2848 symval = (h->root.root.u.def.value
2849 + h->root.root.u.def.section->output_section->vma
2850 + h->root.root.u.def.section->output_offset);
2851 }
2852
2853 /* For simplicity of coding, we are going to modify the section
2854 contents, the section relocs, and the BFD symbol table. We
2855 must tell the rest of the code not to free up this
2856 information. It would be possible to instead create a table
2857 of changes which have to be made, as is done in coff-mips.c;
2858 that would be more work, but would require less memory when
2859 the linker is run. */
2860
2861 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
2862 branch/call, also deal with "call" -> "calls" conversions and
2863 insertion of prologue data into "call" instructions. */
2864 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
2865 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
2866 {
2867 bfd_vma value = symval;
2868
2869 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
2870 && h != NULL
2871 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
2872 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
2873 && h->root.plt.offset != (bfd_vma) -1)
2874 {
2875 asection * splt;
2876
2877 splt = bfd_get_section_by_name (elf_hash_table (link_info)
2878 ->dynobj, ".plt");
2879
2880 value = ((splt->output_section->vma
2881 + splt->output_offset
2882 + h->root.plt.offset)
2883 - (sec->output_section->vma
2884 + sec->output_offset
2885 + irel->r_offset));
2886 }
2887
2888 /* If we've got a "call" instruction that needs to be turned
2889 into a "calls" instruction, do so now. It saves a byte. */
2890 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
2891 {
2892 unsigned char code;
2893
2894 /* Get the opcode. */
2895 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2896
2897 /* Make sure we're working with a "call" instruction! */
2898 if (code == 0xdd)
2899 {
2900 /* Note that we've changed the relocs, section contents,
2901 etc. */
2902 elf_section_data (sec)->relocs = internal_relocs;
2903 elf_section_data (sec)->this_hdr.contents = contents;
2904 symtab_hdr->contents = (unsigned char *) isymbuf;
2905
2906 /* Fix the opcode. */
2907 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
2908 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
2909
2910 /* Fix irel->r_offset and irel->r_addend. */
2911 irel->r_offset += 1;
2912 irel->r_addend += 1;
2913
2914 /* Delete one byte of data. */
2915 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2916 irel->r_offset + 3, 1))
2917 goto error_return;
2918
2919 /* That will change things, so, we should relax again.
2920 Note that this is not required, and it may be slow. */
2921 *again = TRUE;
2922 }
2923 }
2924 else if (h)
2925 {
2926 /* We've got a "call" instruction which needs some data
2927 from target function filled in. */
2928 unsigned char code;
2929
2930 /* Get the opcode. */
2931 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2932
2933 /* Insert data from the target function into the "call"
2934 instruction if needed. */
2935 if (code == 0xdd)
2936 {
2937 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
2938 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
2939 contents + irel->r_offset + 5);
2940 }
2941 }
2942
2943 /* Deal with pc-relative gunk. */
2944 value -= (sec->output_section->vma + sec->output_offset);
2945 value -= irel->r_offset;
2946 value += irel->r_addend;
2947
2948 /* See if the value will fit in 16 bits, note the high value is
2949 0x7fff + 2 as the target will be two bytes closer if we are
2950 able to relax, if it's in the same section. */
2951 if (sec->output_section == sym_sec->output_section)
2952 jump_offset = 0x8001;
2953 else
2954 jump_offset = 0x7fff;
2955
2956 /* Account for jumps across alignment boundaries using
2957 align_gap_adjustment. */
2958 if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
2959 && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
2960 {
2961 unsigned char code;
2962
2963 /* Get the opcode. */
2964 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2965
2966 if (code != 0xdc && code != 0xdd && code != 0xff)
2967 continue;
2968
2969 /* Note that we've changed the relocs, section 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;
2973
2974 /* Fix the opcode. */
2975 if (code == 0xdc)
2976 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
2977 else if (code == 0xdd)
2978 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
2979 else if (code == 0xff)
2980 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2981
2982 /* Fix the relocation's type. */
2983 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2984 (ELF32_R_TYPE (irel->r_info)
2985 == (int) R_MN10300_PLT32)
2986 ? R_MN10300_PLT16 :
2987 R_MN10300_PCREL16);
2988
2989 /* Delete two bytes of data. */
2990 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2991 irel->r_offset + 1, 2))
2992 goto error_return;
2993
2994 /* That will change things, so, we should relax again.
2995 Note that this is not required, and it may be slow. */
2996 *again = TRUE;
2997 }
2998 }
2999
3000 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
3001 branch. */
3002 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
3003 {
3004 bfd_vma value = symval;
3005
3006 /* If we've got a "call" instruction that needs to be turned
3007 into a "calls" instruction, do so now. It saves a byte. */
3008 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3009 {
3010 unsigned char code;
3011
3012 /* Get the opcode. */
3013 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3014
3015 /* Make sure we're working with a "call" instruction! */
3016 if (code == 0xcd)
3017 {
3018 /* Note that we've changed the relocs, section contents,
3019 etc. */
3020 elf_section_data (sec)->relocs = internal_relocs;
3021 elf_section_data (sec)->this_hdr.contents = contents;
3022 symtab_hdr->contents = (unsigned char *) isymbuf;
3023
3024 /* Fix the opcode. */
3025 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3026 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3027
3028 /* Fix irel->r_offset and irel->r_addend. */
3029 irel->r_offset += 1;
3030 irel->r_addend += 1;
3031
3032 /* Delete one byte of data. */
3033 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3034 irel->r_offset + 1, 1))
3035 goto error_return;
3036
3037 /* That will change things, so, we should relax again.
3038 Note that this is not required, and it may be slow. */
3039 *again = TRUE;
3040 }
3041 }
3042 else if (h)
3043 {
3044 unsigned char code;
3045
3046 /* Get the opcode. */
3047 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3048
3049 /* Insert data from the target function into the "call"
3050 instruction if needed. */
3051 if (code == 0xcd)
3052 {
3053 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3054 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3055 contents + irel->r_offset + 3);
3056 }
3057 }
3058
3059 /* Deal with pc-relative gunk. */
3060 value -= (sec->output_section->vma + sec->output_offset);
3061 value -= irel->r_offset;
3062 value += irel->r_addend;
3063
3064 /* See if the value will fit in 8 bits, note the high value is
3065 0x7f + 1 as the target will be one bytes closer if we are
3066 able to relax. */
3067 if ((long) value < 0x80 && (long) value > -0x80)
3068 {
3069 unsigned char code;
3070
3071 /* Get the opcode. */
3072 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3073
3074 if (code != 0xcc)
3075 continue;
3076
3077 /* Note that we've changed the relocs, section contents, etc. */
3078 elf_section_data (sec)->relocs = internal_relocs;
3079 elf_section_data (sec)->this_hdr.contents = contents;
3080 symtab_hdr->contents = (unsigned char *) isymbuf;
3081
3082 /* Fix the opcode. */
3083 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3084
3085 /* Fix the relocation's type. */
3086 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3087 R_MN10300_PCREL8);
3088
3089 /* Delete one byte of data. */
3090 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3091 irel->r_offset + 1, 1))
3092 goto error_return;
3093
3094 /* That will change things, so, we should relax again.
3095 Note that this is not required, and it may be slow. */
3096 *again = TRUE;
3097 }
3098 }
3099
3100 /* Try to eliminate an unconditional 8 bit pc-relative branch
3101 which immediately follows a conditional 8 bit pc-relative
3102 branch around the unconditional branch.
3103
3104 original: new:
3105 bCC lab1 bCC' lab2
3106 bra lab2
3107 lab1: lab1:
3108
3109 This happens when the bCC can't reach lab2 at assembly time,
3110 but due to other relaxations it can reach at link time. */
3111 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3112 {
3113 Elf_Internal_Rela *nrel;
3114 bfd_vma value = symval;
3115 unsigned char code;
3116
3117 /* Deal with pc-relative gunk. */
3118 value -= (sec->output_section->vma + sec->output_offset);
3119 value -= irel->r_offset;
3120 value += irel->r_addend;
3121
3122 /* Do nothing if this reloc is the last byte in the section. */
3123 if (irel->r_offset == sec->size)
3124 continue;
3125
3126 /* See if the next instruction is an unconditional pc-relative
3127 branch, more often than not this test will fail, so we
3128 test it first to speed things up. */
3129 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3130 if (code != 0xca)
3131 continue;
3132
3133 /* Also make sure the next relocation applies to the next
3134 instruction and that it's a pc-relative 8 bit branch. */
3135 nrel = irel + 1;
3136 if (nrel == irelend
3137 || irel->r_offset + 2 != nrel->r_offset
3138 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3139 continue;
3140
3141 /* Make sure our destination immediately follows the
3142 unconditional branch. */
3143 if (symval != (sec->output_section->vma + sec->output_offset
3144 + irel->r_offset + 3))
3145 continue;
3146
3147 /* Now make sure we are a conditional branch. This may not
3148 be necessary, but why take the chance.
3149
3150 Note these checks assume that R_MN10300_PCREL8 relocs
3151 only occur on bCC and bCCx insns. If they occured
3152 elsewhere, we'd need to know the start of this insn
3153 for this check to be accurate. */
3154 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3155 if (code != 0xc0 && code != 0xc1 && code != 0xc2
3156 && code != 0xc3 && code != 0xc4 && code != 0xc5
3157 && code != 0xc6 && code != 0xc7 && code != 0xc8
3158 && code != 0xc9 && code != 0xe8 && code != 0xe9
3159 && code != 0xea && code != 0xeb)
3160 continue;
3161
3162 /* We also have to be sure there is no symbol/label
3163 at the unconditional branch. */
3164 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3165 irel->r_offset + 1))
3166 continue;
3167
3168 /* Note that we've changed the relocs, section contents, etc. */
3169 elf_section_data (sec)->relocs = internal_relocs;
3170 elf_section_data (sec)->this_hdr.contents = contents;
3171 symtab_hdr->contents = (unsigned char *) isymbuf;
3172
3173 /* Reverse the condition of the first branch. */
3174 switch (code)
3175 {
3176 case 0xc8:
3177 code = 0xc9;
3178 break;
3179 case 0xc9:
3180 code = 0xc8;
3181 break;
3182 case 0xc0:
3183 code = 0xc2;
3184 break;
3185 case 0xc2:
3186 code = 0xc0;
3187 break;
3188 case 0xc3:
3189 code = 0xc1;
3190 break;
3191 case 0xc1:
3192 code = 0xc3;
3193 break;
3194 case 0xc4:
3195 code = 0xc6;
3196 break;
3197 case 0xc6:
3198 code = 0xc4;
3199 break;
3200 case 0xc7:
3201 code = 0xc5;
3202 break;
3203 case 0xc5:
3204 code = 0xc7;
3205 break;
3206 case 0xe8:
3207 code = 0xe9;
3208 break;
3209 case 0x9d:
3210 code = 0xe8;
3211 break;
3212 case 0xea:
3213 code = 0xeb;
3214 break;
3215 case 0xeb:
3216 code = 0xea;
3217 break;
3218 }
3219 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3220
3221 /* Set the reloc type and symbol for the first branch
3222 from the second branch. */
3223 irel->r_info = nrel->r_info;
3224
3225 /* Make the reloc for the second branch a null reloc. */
3226 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3227 R_MN10300_NONE);
3228
3229 /* Delete two bytes of data. */
3230 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3231 irel->r_offset + 1, 2))
3232 goto error_return;
3233
3234 /* That will change things, so, we should relax again.
3235 Note that this is not required, and it may be slow. */
3236 *again = TRUE;
3237 }
3238
3239 /* Try to turn a 24 immediate, displacement or absolute address
3240 into a 8 immediate, displacement or absolute address. */
3241 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3242 {
3243 bfd_vma value = symval;
3244 value += irel->r_addend;
3245
3246 /* See if the value will fit in 8 bits. */
3247 if ((long) value < 0x7f && (long) value > -0x80)
3248 {
3249 unsigned char code;
3250
3251 /* AM33 insns which have 24 operands are 6 bytes long and
3252 will have 0xfd as the first byte. */
3253
3254 /* Get the first opcode. */
3255 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3256
3257 if (code == 0xfd)
3258 {
3259 /* Get the second opcode. */
3260 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3261
3262 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3263 equivalent instructions exists. */
3264 if (code != 0x6b && code != 0x7b
3265 && code != 0x8b && code != 0x9b
3266 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3267 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3268 || (code & 0x0f) == 0x0e))
3269 {
3270 /* Not safe if the high bit is on as relaxing may
3271 move the value out of high mem and thus not fit
3272 in a signed 8bit value. This is currently over
3273 conservative. */
3274 if ((value & 0x80) == 0)
3275 {
3276 /* Note that we've changed the relocation contents,
3277 etc. */
3278 elf_section_data (sec)->relocs = internal_relocs;
3279 elf_section_data (sec)->this_hdr.contents = contents;
3280 symtab_hdr->contents = (unsigned char *) isymbuf;
3281
3282 /* Fix the opcode. */
3283 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3284 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3285
3286 /* Fix the relocation's type. */
3287 irel->r_info =
3288 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3289 R_MN10300_8);
3290
3291 /* Delete two bytes of data. */
3292 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3293 irel->r_offset + 1, 2))
3294 goto error_return;
3295
3296 /* That will change things, so, we should relax
3297 again. Note that this is not required, and it
3298 may be slow. */
3299 *again = TRUE;
3300 break;
3301 }
3302 }
3303 }
3304 }
3305 }
3306
3307 /* Try to turn a 32bit immediate, displacement or absolute address
3308 into a 16bit immediate, displacement or absolute address. */
3309 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3310 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3311 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3312 {
3313 bfd_vma value = symval;
3314
3315 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3316 {
3317 asection * sgot;
3318
3319 sgot = bfd_get_section_by_name (elf_hash_table (link_info)
3320 ->dynobj, ".got");
3321
3322 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3323 {
3324 value = sgot->output_offset;
3325
3326 if (h)
3327 value += h->root.got.offset;
3328 else
3329 value += (elf_local_got_offsets
3330 (abfd)[ELF32_R_SYM (irel->r_info)]);
3331 }
3332 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3333 value -= sgot->output_section->vma;
3334 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3335 value = (sgot->output_section->vma
3336 - (sec->output_section->vma
3337 + sec->output_offset
3338 + irel->r_offset));
3339 else
3340 abort ();
3341 }
3342
3343 value += irel->r_addend;
3344
3345 /* See if the value will fit in 24 bits.
3346 We allow any 16bit match here. We prune those we can't
3347 handle below. */
3348 if ((long) value < 0x7fffff && (long) value > -0x800000)
3349 {
3350 unsigned char code;
3351
3352 /* AM33 insns which have 32bit operands are 7 bytes long and
3353 will have 0xfe as the first byte. */
3354
3355 /* Get the first opcode. */
3356 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3357
3358 if (code == 0xfe)
3359 {
3360 /* Get the second opcode. */
3361 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3362
3363 /* All the am33 32 -> 24 relaxing possibilities. */
3364 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3365 equivalent instructions exists. */
3366 if (code != 0x6b && code != 0x7b
3367 && code != 0x8b && code != 0x9b
3368 && (ELF32_R_TYPE (irel->r_info)
3369 != (int) R_MN10300_GOTPC32)
3370 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3371 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3372 || (code & 0x0f) == 0x0e))
3373 {
3374 /* Not safe if the high bit is on as relaxing may
3375 move the value out of high mem and thus not fit
3376 in a signed 16bit value. This is currently over
3377 conservative. */
3378 if ((value & 0x8000) == 0)
3379 {
3380 /* Note that we've changed the relocation contents,
3381 etc. */
3382 elf_section_data (sec)->relocs = internal_relocs;
3383 elf_section_data (sec)->this_hdr.contents = contents;
3384 symtab_hdr->contents = (unsigned char *) isymbuf;
3385
3386 /* Fix the opcode. */
3387 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3388 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3389
3390 /* Fix the relocation's type. */
3391 irel->r_info =
3392 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3393 (ELF32_R_TYPE (irel->r_info)
3394 == (int) R_MN10300_GOTOFF32)
3395 ? R_MN10300_GOTOFF24
3396 : (ELF32_R_TYPE (irel->r_info)
3397 == (int) R_MN10300_GOT32)
3398 ? R_MN10300_GOT24 :
3399 R_MN10300_24);
3400
3401 /* Delete one byte of data. */
3402 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3403 irel->r_offset + 3, 1))
3404 goto error_return;
3405
3406 /* That will change things, so, we should relax
3407 again. Note that this is not required, and it
3408 may be slow. */
3409 *again = TRUE;
3410 break;
3411 }
3412 }
3413 }
3414 }
3415
3416 /* See if the value will fit in 16 bits.
3417 We allow any 16bit match here. We prune those we can't
3418 handle below. */
3419 if ((long) value < 0x7fff && (long) value > -0x8000)
3420 {
3421 unsigned char code;
3422
3423 /* Most insns which have 32bit operands are 6 bytes long;
3424 exceptions are pcrel insns and bit insns.
3425
3426 We handle pcrel insns above. We don't bother trying
3427 to handle the bit insns here.
3428
3429 The first byte of the remaining insns will be 0xfc. */
3430
3431 /* Get the first opcode. */
3432 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3433
3434 if (code != 0xfc)
3435 continue;
3436
3437 /* Get the second opcode. */
3438 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3439
3440 if ((code & 0xf0) < 0x80)
3441 switch (code & 0xf0)
3442 {
3443 /* mov (d32,am),dn -> mov (d32,am),dn
3444 mov dm,(d32,am) -> mov dn,(d32,am)
3445 mov (d32,am),an -> mov (d32,am),an
3446 mov dm,(d32,am) -> mov dn,(d32,am)
3447 movbu (d32,am),dn -> movbu (d32,am),dn
3448 movbu dm,(d32,am) -> movbu dn,(d32,am)
3449 movhu (d32,am),dn -> movhu (d32,am),dn
3450 movhu dm,(d32,am) -> movhu dn,(d32,am) */
3451 case 0x00:
3452 case 0x10:
3453 case 0x20:
3454 case 0x30:
3455 case 0x40:
3456 case 0x50:
3457 case 0x60:
3458 case 0x70:
3459 /* Not safe if the high bit is on as relaxing may
3460 move the value out of high mem and thus not fit
3461 in a signed 16bit value. */
3462 if (code == 0xcc
3463 && (value & 0x8000))
3464 continue;
3465
3466 /* Note that we've changed the relocation contents, etc. */
3467 elf_section_data (sec)->relocs = internal_relocs;
3468 elf_section_data (sec)->this_hdr.contents = contents;
3469 symtab_hdr->contents = (unsigned char *) isymbuf;
3470
3471 /* Fix the opcode. */
3472 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3473 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3474
3475 /* Fix the relocation's type. */
3476 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3477 (ELF32_R_TYPE (irel->r_info)
3478 == (int) R_MN10300_GOTOFF32)
3479 ? R_MN10300_GOTOFF16
3480 : (ELF32_R_TYPE (irel->r_info)
3481 == (int) R_MN10300_GOT32)
3482 ? R_MN10300_GOT16
3483 : (ELF32_R_TYPE (irel->r_info)
3484 == (int) R_MN10300_GOTPC32)
3485 ? R_MN10300_GOTPC16 :
3486 R_MN10300_16);
3487
3488 /* Delete two bytes of data. */
3489 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3490 irel->r_offset + 2, 2))
3491 goto error_return;
3492
3493 /* That will change things, so, we should relax again.
3494 Note that this is not required, and it may be slow. */
3495 *again = TRUE;
3496 break;
3497 }
3498 else if ((code & 0xf0) == 0x80
3499 || (code & 0xf0) == 0x90)
3500 switch (code & 0xf3)
3501 {
3502 /* mov dn,(abs32) -> mov dn,(abs16)
3503 movbu dn,(abs32) -> movbu dn,(abs16)
3504 movhu dn,(abs32) -> movhu dn,(abs16) */
3505 case 0x81:
3506 case 0x82:
3507 case 0x83:
3508 /* Note that we've changed the relocation contents, etc. */
3509 elf_section_data (sec)->relocs = internal_relocs;
3510 elf_section_data (sec)->this_hdr.contents = contents;
3511 symtab_hdr->contents = (unsigned char *) isymbuf;
3512
3513 if ((code & 0xf3) == 0x81)
3514 code = 0x01 + (code & 0x0c);
3515 else if ((code & 0xf3) == 0x82)
3516 code = 0x02 + (code & 0x0c);
3517 else if ((code & 0xf3) == 0x83)
3518 code = 0x03 + (code & 0x0c);
3519 else
3520 abort ();
3521
3522 /* Fix the opcode. */
3523 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3524
3525 /* Fix the relocation's type. */
3526 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3527 (ELF32_R_TYPE (irel->r_info)
3528 == (int) R_MN10300_GOTOFF32)
3529 ? R_MN10300_GOTOFF16
3530 : (ELF32_R_TYPE (irel->r_info)
3531 == (int) R_MN10300_GOT32)
3532 ? R_MN10300_GOT16
3533 : (ELF32_R_TYPE (irel->r_info)
3534 == (int) R_MN10300_GOTPC32)
3535 ? R_MN10300_GOTPC16 :
3536 R_MN10300_16);
3537
3538 /* The opcode got shorter too, so we have to fix the
3539 addend and offset too! */
3540 irel->r_offset -= 1;
3541
3542 /* Delete three bytes of data. */
3543 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3544 irel->r_offset + 1, 3))
3545 goto error_return;
3546
3547 /* That will change things, so, we should relax again.
3548 Note that this is not required, and it may be slow. */
3549 *again = TRUE;
3550 break;
3551
3552 /* mov am,(abs32) -> mov am,(abs16)
3553 mov am,(d32,sp) -> mov am,(d16,sp)
3554 mov dm,(d32,sp) -> mov dm,(d32,sp)
3555 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
3556 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
3557 case 0x80:
3558 case 0x90:
3559 case 0x91:
3560 case 0x92:
3561 case 0x93:
3562 /* sp-based offsets are zero-extended. */
3563 if (code >= 0x90 && code <= 0x93
3564 && (long) value < 0)
3565 continue;
3566
3567 /* Note that we've changed the relocation contents, etc. */
3568 elf_section_data (sec)->relocs = internal_relocs;
3569 elf_section_data (sec)->this_hdr.contents = contents;
3570 symtab_hdr->contents = (unsigned char *) isymbuf;
3571
3572 /* Fix the opcode. */
3573 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3574 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3575
3576 /* Fix the relocation's type. */
3577 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3578 (ELF32_R_TYPE (irel->r_info)
3579 == (int) R_MN10300_GOTOFF32)
3580 ? R_MN10300_GOTOFF16
3581 : (ELF32_R_TYPE (irel->r_info)
3582 == (int) R_MN10300_GOT32)
3583 ? R_MN10300_GOT16
3584 : (ELF32_R_TYPE (irel->r_info)
3585 == (int) R_MN10300_GOTPC32)
3586 ? R_MN10300_GOTPC16 :
3587 R_MN10300_16);
3588
3589 /* Delete two bytes of data. */
3590 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3591 irel->r_offset + 2, 2))
3592 goto error_return;
3593
3594 /* That will change things, so, we should relax again.
3595 Note that this is not required, and it may be slow. */
3596 *again = TRUE;
3597 break;
3598 }
3599 else if ((code & 0xf0) < 0xf0)
3600 switch (code & 0xfc)
3601 {
3602 /* mov imm32,dn -> mov imm16,dn
3603 mov imm32,an -> mov imm16,an
3604 mov (abs32),dn -> mov (abs16),dn
3605 movbu (abs32),dn -> movbu (abs16),dn
3606 movhu (abs32),dn -> movhu (abs16),dn */
3607 case 0xcc:
3608 case 0xdc:
3609 case 0xa4:
3610 case 0xa8:
3611 case 0xac:
3612 /* Not safe if the high bit is on as relaxing may
3613 move the value out of high mem and thus not fit
3614 in a signed 16bit value. */
3615 if (code == 0xcc
3616 && (value & 0x8000))
3617 continue;
3618
3619 /* mov imm16, an zero-extends the immediate. */
3620 if (code == 0xdc
3621 && (long) value < 0)
3622 continue;
3623
3624 /* Note that we've changed the relocation contents, etc. */
3625 elf_section_data (sec)->relocs = internal_relocs;
3626 elf_section_data (sec)->this_hdr.contents = contents;
3627 symtab_hdr->contents = (unsigned char *) isymbuf;
3628
3629 if ((code & 0xfc) == 0xcc)
3630 code = 0x2c + (code & 0x03);
3631 else if ((code & 0xfc) == 0xdc)
3632 code = 0x24 + (code & 0x03);
3633 else if ((code & 0xfc) == 0xa4)
3634 code = 0x30 + (code & 0x03);
3635 else if ((code & 0xfc) == 0xa8)
3636 code = 0x34 + (code & 0x03);
3637 else if ((code & 0xfc) == 0xac)
3638 code = 0x38 + (code & 0x03);
3639 else
3640 abort ();
3641
3642 /* Fix the opcode. */
3643 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3644
3645 /* Fix the relocation's type. */
3646 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3647 (ELF32_R_TYPE (irel->r_info)
3648 == (int) R_MN10300_GOTOFF32)
3649 ? R_MN10300_GOTOFF16
3650 : (ELF32_R_TYPE (irel->r_info)
3651 == (int) R_MN10300_GOT32)
3652 ? R_MN10300_GOT16
3653 : (ELF32_R_TYPE (irel->r_info)
3654 == (int) R_MN10300_GOTPC32)
3655 ? R_MN10300_GOTPC16 :
3656 R_MN10300_16);
3657
3658 /* The opcode got shorter too, so we have to fix the
3659 addend and offset too! */
3660 irel->r_offset -= 1;
3661
3662 /* Delete three bytes of data. */
3663 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3664 irel->r_offset + 1, 3))
3665 goto error_return;
3666
3667 /* That will change things, so, we should relax again.
3668 Note that this is not required, and it may be slow. */
3669 *again = TRUE;
3670 break;
3671
3672 /* mov (abs32),an -> mov (abs16),an
3673 mov (d32,sp),an -> mov (d16,sp),an
3674 mov (d32,sp),dn -> mov (d16,sp),dn
3675 movbu (d32,sp),dn -> movbu (d16,sp),dn
3676 movhu (d32,sp),dn -> movhu (d16,sp),dn
3677 add imm32,dn -> add imm16,dn
3678 cmp imm32,dn -> cmp imm16,dn
3679 add imm32,an -> add imm16,an
3680 cmp imm32,an -> cmp imm16,an
3681 and imm32,dn -> and imm16,dn
3682 or imm32,dn -> or imm16,dn
3683 xor imm32,dn -> xor imm16,dn
3684 btst imm32,dn -> btst imm16,dn */
3685
3686 case 0xa0:
3687 case 0xb0:
3688 case 0xb1:
3689 case 0xb2:
3690 case 0xb3:
3691 case 0xc0:
3692 case 0xc8:
3693
3694 case 0xd0:
3695 case 0xd8:
3696 case 0xe0:
3697 case 0xe1:
3698 case 0xe2:
3699 case 0xe3:
3700 /* cmp imm16, an zero-extends the immediate. */
3701 if (code == 0xdc
3702 && (long) value < 0)
3703 continue;
3704
3705 /* So do sp-based offsets. */
3706 if (code >= 0xb0 && code <= 0xb3
3707 && (long) value < 0)
3708 continue;
3709
3710 /* Note that we've changed the relocation contents, etc. */
3711 elf_section_data (sec)->relocs = internal_relocs;
3712 elf_section_data (sec)->this_hdr.contents = contents;
3713 symtab_hdr->contents = (unsigned char *) isymbuf;
3714
3715 /* Fix the opcode. */
3716 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3717 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3718
3719 /* Fix the relocation's type. */
3720 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3721 (ELF32_R_TYPE (irel->r_info)
3722 == (int) R_MN10300_GOTOFF32)
3723 ? R_MN10300_GOTOFF16
3724 : (ELF32_R_TYPE (irel->r_info)
3725 == (int) R_MN10300_GOT32)
3726 ? R_MN10300_GOT16
3727 : (ELF32_R_TYPE (irel->r_info)
3728 == (int) R_MN10300_GOTPC32)
3729 ? R_MN10300_GOTPC16 :
3730 R_MN10300_16);
3731
3732 /* Delete two bytes of data. */
3733 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3734 irel->r_offset + 2, 2))
3735 goto error_return;
3736
3737 /* That will change things, so, we should relax again.
3738 Note that this is not required, and it may be slow. */
3739 *again = TRUE;
3740 break;
3741 }
3742 else if (code == 0xfe)
3743 {
3744 /* add imm32,sp -> add imm16,sp */
3745
3746 /* Note that we've changed the relocation contents, etc. */
3747 elf_section_data (sec)->relocs = internal_relocs;
3748 elf_section_data (sec)->this_hdr.contents = contents;
3749 symtab_hdr->contents = (unsigned char *) isymbuf;
3750
3751 /* Fix the opcode. */
3752 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3753 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
3754
3755 /* Fix the relocation's type. */
3756 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3757 (ELF32_R_TYPE (irel->r_info)
3758 == (int) R_MN10300_GOT32)
3759 ? R_MN10300_GOT16
3760 : (ELF32_R_TYPE (irel->r_info)
3761 == (int) R_MN10300_GOTOFF32)
3762 ? R_MN10300_GOTOFF16
3763 : (ELF32_R_TYPE (irel->r_info)
3764 == (int) R_MN10300_GOTPC32)
3765 ? R_MN10300_GOTPC16 :
3766 R_MN10300_16);
3767
3768 /* Delete two bytes of data. */
3769 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3770 irel->r_offset + 2, 2))
3771 goto error_return;
3772
3773 /* That will change things, so, we should relax again.
3774 Note that this is not required, and it may be slow. */
3775 *again = TRUE;
3776 break;
3777 }
3778 }
3779 }
3780 }
3781
3782 if (isymbuf != NULL
3783 && symtab_hdr->contents != (unsigned char *) isymbuf)
3784 {
3785 if (! link_info->keep_memory)
3786 free (isymbuf);
3787 else
3788 {
3789 /* Cache the symbols for elf_link_input_bfd. */
3790 symtab_hdr->contents = (unsigned char *) isymbuf;
3791 }
3792 }
3793
3794 if (contents != NULL
3795 && elf_section_data (sec)->this_hdr.contents != contents)
3796 {
3797 if (! link_info->keep_memory)
3798 free (contents);
3799 else
3800 {
3801 /* Cache the section contents for elf_link_input_bfd. */
3802 elf_section_data (sec)->this_hdr.contents = contents;
3803 }
3804 }
3805
3806 if (internal_relocs != NULL
3807 && elf_section_data (sec)->relocs != internal_relocs)
3808 free (internal_relocs);
3809
3810 return TRUE;
3811
3812 error_return:
3813 if (isymbuf != NULL
3814 && symtab_hdr->contents != (unsigned char *) isymbuf)
3815 free (isymbuf);
3816 if (contents != NULL
3817 && elf_section_data (section)->this_hdr.contents != contents)
3818 free (contents);
3819 if (internal_relocs != NULL
3820 && elf_section_data (section)->relocs != internal_relocs)
3821 free (internal_relocs);
3822
3823 return FALSE;
3824 }
3825
3826 /* This is a version of bfd_generic_get_relocated_section_contents
3827 which uses mn10300_elf_relocate_section. */
3828
3829 static bfd_byte *
3830 mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
3831 struct bfd_link_info *link_info,
3832 struct bfd_link_order *link_order,
3833 bfd_byte *data,
3834 bfd_boolean relocatable,
3835 asymbol **symbols)
3836 {
3837 Elf_Internal_Shdr *symtab_hdr;
3838 asection *input_section = link_order->u.indirect.section;
3839 bfd *input_bfd = input_section->owner;
3840 asection **sections = NULL;
3841 Elf_Internal_Rela *internal_relocs = NULL;
3842 Elf_Internal_Sym *isymbuf = NULL;
3843
3844 /* We only need to handle the case of relaxing, or of having a
3845 particular set of section contents, specially. */
3846 if (relocatable
3847 || elf_section_data (input_section)->this_hdr.contents == NULL)
3848 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
3849 link_order, data,
3850 relocatable,
3851 symbols);
3852
3853 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3854
3855 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
3856 (size_t) input_section->size);
3857
3858 if ((input_section->flags & SEC_RELOC) != 0
3859 && input_section->reloc_count > 0)
3860 {
3861 asection **secpp;
3862 Elf_Internal_Sym *isym, *isymend;
3863 bfd_size_type amt;
3864
3865 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
3866 NULL, NULL, FALSE);
3867 if (internal_relocs == NULL)
3868 goto error_return;
3869
3870 if (symtab_hdr->sh_info != 0)
3871 {
3872 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3873 if (isymbuf == NULL)
3874 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3875 symtab_hdr->sh_info, 0,
3876 NULL, NULL, NULL);
3877 if (isymbuf == NULL)
3878 goto error_return;
3879 }
3880
3881 amt = symtab_hdr->sh_info;
3882 amt *= sizeof (asection *);
3883 sections = bfd_malloc (amt);
3884 if (sections == NULL && amt != 0)
3885 goto error_return;
3886
3887 isymend = isymbuf + symtab_hdr->sh_info;
3888 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
3889 {
3890 asection *isec;
3891
3892 if (isym->st_shndx == SHN_UNDEF)
3893 isec = bfd_und_section_ptr;
3894 else if (isym->st_shndx == SHN_ABS)
3895 isec = bfd_abs_section_ptr;
3896 else if (isym->st_shndx == SHN_COMMON)
3897 isec = bfd_com_section_ptr;
3898 else
3899 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3900
3901 *secpp = isec;
3902 }
3903
3904 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
3905 input_section, data, internal_relocs,
3906 isymbuf, sections))
3907 goto error_return;
3908
3909 if (sections != NULL)
3910 free (sections);
3911 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3912 free (isymbuf);
3913 if (internal_relocs != elf_section_data (input_section)->relocs)
3914 free (internal_relocs);
3915 }
3916
3917 return data;
3918
3919 error_return:
3920 if (sections != NULL)
3921 free (sections);
3922 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
3923 free (isymbuf);
3924 if (internal_relocs != NULL
3925 && internal_relocs != elf_section_data (input_section)->relocs)
3926 free (internal_relocs);
3927 return NULL;
3928 }
3929
3930 /* Assorted hash table functions. */
3931
3932 /* Initialize an entry in the link hash table. */
3933
3934 /* Create an entry in an MN10300 ELF linker hash table. */
3935
3936 static struct bfd_hash_entry *
3937 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
3938 struct bfd_hash_table *table,
3939 const char *string)
3940 {
3941 struct elf32_mn10300_link_hash_entry *ret =
3942 (struct elf32_mn10300_link_hash_entry *) entry;
3943
3944 /* Allocate the structure if it has not already been allocated by a
3945 subclass. */
3946 if (ret == NULL)
3947 ret = (struct elf32_mn10300_link_hash_entry *)
3948 bfd_hash_allocate (table, sizeof (* ret));
3949 if (ret == NULL)
3950 return (struct bfd_hash_entry *) ret;
3951
3952 /* Call the allocation method of the superclass. */
3953 ret = (struct elf32_mn10300_link_hash_entry *)
3954 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
3955 table, string);
3956 if (ret != NULL)
3957 {
3958 ret->direct_calls = 0;
3959 ret->stack_size = 0;
3960 ret->movm_args = 0;
3961 ret->movm_stack_size = 0;
3962 ret->flags = 0;
3963 ret->value = 0;
3964 }
3965
3966 return (struct bfd_hash_entry *) ret;
3967 }
3968
3969 /* Create an mn10300 ELF linker hash table. */
3970
3971 static struct bfd_link_hash_table *
3972 elf32_mn10300_link_hash_table_create (bfd *abfd)
3973 {
3974 struct elf32_mn10300_link_hash_table *ret;
3975 bfd_size_type amt = sizeof (* ret);
3976
3977 ret = bfd_malloc (amt);
3978 if (ret == NULL)
3979 return NULL;
3980
3981 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3982 elf32_mn10300_link_hash_newfunc,
3983 sizeof (struct elf32_mn10300_link_hash_entry),
3984 MN10300_ELF_DATA))
3985 {
3986 free (ret);
3987 return NULL;
3988 }
3989
3990 ret->flags = 0;
3991 amt = sizeof (struct elf_link_hash_table);
3992 ret->static_hash_table = bfd_malloc (amt);
3993 if (ret->static_hash_table == NULL)
3994 {
3995 free (ret);
3996 return NULL;
3997 }
3998
3999 if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
4000 elf32_mn10300_link_hash_newfunc,
4001 sizeof (struct elf32_mn10300_link_hash_entry),
4002 MN10300_ELF_DATA))
4003 {
4004 free (ret->static_hash_table);
4005 free (ret);
4006 return NULL;
4007 }
4008 return & ret->root.root;
4009 }
4010
4011 /* Free an mn10300 ELF linker hash table. */
4012
4013 static void
4014 elf32_mn10300_link_hash_table_free (struct bfd_link_hash_table *hash)
4015 {
4016 struct elf32_mn10300_link_hash_table *ret
4017 = (struct elf32_mn10300_link_hash_table *) hash;
4018
4019 _bfd_generic_link_hash_table_free
4020 ((struct bfd_link_hash_table *) ret->static_hash_table);
4021 _bfd_generic_link_hash_table_free
4022 ((struct bfd_link_hash_table *) ret);
4023 }
4024
4025 static unsigned long
4026 elf_mn10300_mach (flagword flags)
4027 {
4028 switch (flags & EF_MN10300_MACH)
4029 {
4030 case E_MN10300_MACH_MN10300:
4031 default:
4032 return bfd_mach_mn10300;
4033
4034 case E_MN10300_MACH_AM33:
4035 return bfd_mach_am33;
4036
4037 case E_MN10300_MACH_AM33_2:
4038 return bfd_mach_am33_2;
4039 }
4040 }
4041
4042 /* The final processing done just before writing out a MN10300 ELF object
4043 file. This gets the MN10300 architecture right based on the machine
4044 number. */
4045
4046 static void
4047 _bfd_mn10300_elf_final_write_processing (bfd *abfd,
4048 bfd_boolean linker ATTRIBUTE_UNUSED)
4049 {
4050 unsigned long val;
4051
4052 switch (bfd_get_mach (abfd))
4053 {
4054 default:
4055 case bfd_mach_mn10300:
4056 val = E_MN10300_MACH_MN10300;
4057 break;
4058
4059 case bfd_mach_am33:
4060 val = E_MN10300_MACH_AM33;
4061 break;
4062
4063 case bfd_mach_am33_2:
4064 val = E_MN10300_MACH_AM33_2;
4065 break;
4066 }
4067
4068 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
4069 elf_elfheader (abfd)->e_flags |= val;
4070 }
4071
4072 static bfd_boolean
4073 _bfd_mn10300_elf_object_p (bfd *abfd)
4074 {
4075 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
4076 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
4077 return TRUE;
4078 }
4079
4080 /* Merge backend specific data from an object file to the output
4081 object file when linking. */
4082
4083 static bfd_boolean
4084 _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
4085 {
4086 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4087 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4088 return TRUE;
4089
4090 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4091 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
4092 {
4093 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4094 bfd_get_mach (ibfd)))
4095 return FALSE;
4096 }
4097
4098 return TRUE;
4099 }
4100
4101 #define PLT0_ENTRY_SIZE 15
4102 #define PLT_ENTRY_SIZE 20
4103 #define PIC_PLT_ENTRY_SIZE 24
4104
4105 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4106 {
4107 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
4108 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
4109 0xf0, 0xf4, /* jmp (a0) */
4110 };
4111
4112 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4113 {
4114 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
4115 0xf0, 0xf4, /* jmp (a0) */
4116 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4117 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4118 };
4119
4120 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4121 {
4122 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4123 0xf0, 0xf4, /* jmp (a0) */
4124 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4125 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4126 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4127 0xf0, 0xf4, /* jmp (a0) */
4128 };
4129
4130 /* Return size of the first PLT entry. */
4131 #define elf_mn10300_sizeof_plt0(info) \
4132 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4133
4134 /* Return size of a PLT entry. */
4135 #define elf_mn10300_sizeof_plt(info) \
4136 (info->shared ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4137
4138 /* Return offset of the PLT0 address in an absolute PLT entry. */
4139 #define elf_mn10300_plt_plt0_offset(info) 16
4140
4141 /* Return offset of the linker in PLT0 entry. */
4142 #define elf_mn10300_plt0_linker_offset(info) 2
4143
4144 /* Return offset of the GOT id in PLT0 entry. */
4145 #define elf_mn10300_plt0_gotid_offset(info) 9
4146
4147 /* Return offset of the temporary in PLT entry. */
4148 #define elf_mn10300_plt_temp_offset(info) 8
4149
4150 /* Return offset of the symbol in PLT entry. */
4151 #define elf_mn10300_plt_symbol_offset(info) 2
4152
4153 /* Return offset of the relocation in PLT entry. */
4154 #define elf_mn10300_plt_reloc_offset(info) 11
4155
4156 /* The name of the dynamic interpreter. This is put in the .interp
4157 section. */
4158
4159 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4160
4161 /* Create dynamic sections when linking against a dynamic object. */
4162
4163 static bfd_boolean
4164 _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4165 {
4166 flagword flags;
4167 asection * s;
4168 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4169 int ptralign = 0;
4170
4171 switch (bed->s->arch_size)
4172 {
4173 case 32:
4174 ptralign = 2;
4175 break;
4176
4177 case 64:
4178 ptralign = 3;
4179 break;
4180
4181 default:
4182 bfd_set_error (bfd_error_bad_value);
4183 return FALSE;
4184 }
4185
4186 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4187 .rel[a].bss sections. */
4188 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4189 | SEC_LINKER_CREATED);
4190
4191 s = bfd_make_section_with_flags (abfd,
4192 (bed->default_use_rela_p
4193 ? ".rela.plt" : ".rel.plt"),
4194 flags | SEC_READONLY);
4195 if (s == NULL
4196 || ! bfd_set_section_alignment (abfd, s, ptralign))
4197 return FALSE;
4198
4199 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4200 return FALSE;
4201
4202 {
4203 const char * secname;
4204 char * relname;
4205 flagword secflags;
4206 asection * sec;
4207
4208 for (sec = abfd->sections; sec; sec = sec->next)
4209 {
4210 secflags = bfd_get_section_flags (abfd, sec);
4211 if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
4212 || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
4213 continue;
4214
4215 secname = bfd_get_section_name (abfd, sec);
4216 relname = bfd_malloc (strlen (secname) + 6);
4217 strcpy (relname, ".rela");
4218 strcat (relname, secname);
4219
4220 s = bfd_make_section_with_flags (abfd, relname,
4221 flags | SEC_READONLY);
4222 if (s == NULL
4223 || ! bfd_set_section_alignment (abfd, s, ptralign))
4224 return FALSE;
4225 }
4226 }
4227
4228 if (bed->want_dynbss)
4229 {
4230 /* The .dynbss section is a place to put symbols which are defined
4231 by dynamic objects, are referenced by regular objects, and are
4232 not functions. We must allocate space for them in the process
4233 image and use a R_*_COPY reloc to tell the dynamic linker to
4234 initialize them at run time. The linker script puts the .dynbss
4235 section into the .bss section of the final image. */
4236 s = bfd_make_section_with_flags (abfd, ".dynbss",
4237 SEC_ALLOC | SEC_LINKER_CREATED);
4238 if (s == NULL)
4239 return FALSE;
4240
4241 /* The .rel[a].bss section holds copy relocs. This section is not
4242 normally needed. We need to create it here, though, so that the
4243 linker will map it to an output section. We can't just create it
4244 only if we need it, because we will not know whether we need it
4245 until we have seen all the input files, and the first time the
4246 main linker code calls BFD after examining all the input files
4247 (size_dynamic_sections) the input sections have already been
4248 mapped to the output sections. If the section turns out not to
4249 be needed, we can discard it later. We will never need this
4250 section when generating a shared object, since they do not use
4251 copy relocs. */
4252 if (! info->shared)
4253 {
4254 s = bfd_make_section_with_flags (abfd,
4255 (bed->default_use_rela_p
4256 ? ".rela.bss" : ".rel.bss"),
4257 flags | SEC_READONLY);
4258 if (s == NULL
4259 || ! bfd_set_section_alignment (abfd, s, ptralign))
4260 return FALSE;
4261 }
4262 }
4263
4264 return TRUE;
4265 }
4266 \f
4267 /* Adjust a symbol defined by a dynamic object and referenced by a
4268 regular object. The current definition is in some section of the
4269 dynamic object, but we're not including those sections. We have to
4270 change the definition to something the rest of the link can
4271 understand. */
4272
4273 static bfd_boolean
4274 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4275 struct elf_link_hash_entry * h)
4276 {
4277 bfd * dynobj;
4278 asection * s;
4279
4280 dynobj = elf_hash_table (info)->dynobj;
4281
4282 /* Make sure we know what is going on here. */
4283 BFD_ASSERT (dynobj != NULL
4284 && (h->needs_plt
4285 || h->u.weakdef != NULL
4286 || (h->def_dynamic
4287 && h->ref_regular
4288 && !h->def_regular)));
4289
4290 /* If this is a function, put it in the procedure linkage table. We
4291 will fill in the contents of the procedure linkage table later,
4292 when we know the address of the .got section. */
4293 if (h->type == STT_FUNC
4294 || h->needs_plt)
4295 {
4296 if (! info->shared
4297 && !h->def_dynamic
4298 && !h->ref_dynamic)
4299 {
4300 /* This case can occur if we saw a PLT reloc in an input
4301 file, but the symbol was never referred to by a dynamic
4302 object. In such a case, we don't actually need to build
4303 a procedure linkage table, and we can just do a REL32
4304 reloc instead. */
4305 BFD_ASSERT (h->needs_plt);
4306 return TRUE;
4307 }
4308
4309 /* Make sure this symbol is output as a dynamic symbol. */
4310 if (h->dynindx == -1)
4311 {
4312 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4313 return FALSE;
4314 }
4315
4316 s = bfd_get_section_by_name (dynobj, ".plt");
4317 BFD_ASSERT (s != NULL);
4318
4319 /* If this is the first .plt entry, make room for the special
4320 first entry. */
4321 if (s->size == 0)
4322 s->size += elf_mn10300_sizeof_plt0 (info);
4323
4324 /* If this symbol is not defined in a regular file, and we are
4325 not generating a shared library, then set the symbol to this
4326 location in the .plt. This is required to make function
4327 pointers compare as equal between the normal executable and
4328 the shared library. */
4329 if (! info->shared
4330 && !h->def_regular)
4331 {
4332 h->root.u.def.section = s;
4333 h->root.u.def.value = s->size;
4334 }
4335
4336 h->plt.offset = s->size;
4337
4338 /* Make room for this entry. */
4339 s->size += elf_mn10300_sizeof_plt (info);
4340
4341 /* We also need to make an entry in the .got.plt section, which
4342 will be placed in the .got section by the linker script. */
4343 s = bfd_get_section_by_name (dynobj, ".got.plt");
4344 BFD_ASSERT (s != NULL);
4345 s->size += 4;
4346
4347 /* We also need to make an entry in the .rela.plt section. */
4348 s = bfd_get_section_by_name (dynobj, ".rela.plt");
4349 BFD_ASSERT (s != NULL);
4350 s->size += sizeof (Elf32_External_Rela);
4351
4352 return TRUE;
4353 }
4354
4355 /* If this is a weak symbol, and there is a real definition, the
4356 processor independent code will have arranged for us to see the
4357 real definition first, and we can just use the same value. */
4358 if (h->u.weakdef != NULL)
4359 {
4360 BFD_ASSERT (h->u.weakdef->root.type == bfd_link_hash_defined
4361 || h->u.weakdef->root.type == bfd_link_hash_defweak);
4362 h->root.u.def.section = h->u.weakdef->root.u.def.section;
4363 h->root.u.def.value = h->u.weakdef->root.u.def.value;
4364 return TRUE;
4365 }
4366
4367 /* This is a reference to a symbol defined by a dynamic object which
4368 is not a function. */
4369
4370 /* If we are creating a shared library, we must presume that the
4371 only references to the symbol are via the global offset table.
4372 For such cases we need not do anything here; the relocations will
4373 be handled correctly by relocate_section. */
4374 if (info->shared)
4375 return TRUE;
4376
4377 /* If there are no references to this symbol that do not use the
4378 GOT, we don't need to generate a copy reloc. */
4379 if (!h->non_got_ref)
4380 return TRUE;
4381
4382 if (h->size == 0)
4383 {
4384 (*_bfd_error_handler) (_("dynamic variable `%s' is zero size"),
4385 h->root.root.string);
4386 return TRUE;
4387 }
4388
4389 /* We must allocate the symbol in our .dynbss section, which will
4390 become part of the .bss section of the executable. There will be
4391 an entry for this symbol in the .dynsym section. The dynamic
4392 object will contain position independent code, so all references
4393 from the dynamic object to this symbol will go through the global
4394 offset table. The dynamic linker will use the .dynsym entry to
4395 determine the address it must put in the global offset table, so
4396 both the dynamic object and the regular object will refer to the
4397 same memory location for the variable. */
4398
4399 s = bfd_get_section_by_name (dynobj, ".dynbss");
4400 BFD_ASSERT (s != NULL);
4401
4402 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
4403 copy the initial value out of the dynamic object and into the
4404 runtime process image. We need to remember the offset into the
4405 .rela.bss section we are going to use. */
4406 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4407 {
4408 asection * srel;
4409
4410 srel = bfd_get_section_by_name (dynobj, ".rela.bss");
4411 BFD_ASSERT (srel != NULL);
4412 srel->size += sizeof (Elf32_External_Rela);
4413 h->needs_copy = 1;
4414 }
4415
4416 return _bfd_elf_adjust_dynamic_copy (h, s);
4417 }
4418
4419 /* Set the sizes of the dynamic sections. */
4420
4421 static bfd_boolean
4422 _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
4423 struct bfd_link_info * info)
4424 {
4425 bfd * dynobj;
4426 asection * s;
4427 bfd_boolean plt;
4428 bfd_boolean relocs;
4429 bfd_boolean reltext;
4430
4431 dynobj = elf_hash_table (info)->dynobj;
4432 BFD_ASSERT (dynobj != NULL);
4433
4434 if (elf_hash_table (info)->dynamic_sections_created)
4435 {
4436 /* Set the contents of the .interp section to the interpreter. */
4437 if (info->executable)
4438 {
4439 s = bfd_get_section_by_name (dynobj, ".interp");
4440 BFD_ASSERT (s != NULL);
4441 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4442 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4443 }
4444 }
4445 else
4446 {
4447 /* We may have created entries in the .rela.got section.
4448 However, if we are not creating the dynamic sections, we will
4449 not actually use these entries. Reset the size of .rela.got,
4450 which will cause it to get stripped from the output file
4451 below. */
4452 s = bfd_get_section_by_name (dynobj, ".rela.got");
4453 if (s != NULL)
4454 s->size = 0;
4455 }
4456
4457 /* The check_relocs and adjust_dynamic_symbol entry points have
4458 determined the sizes of the various dynamic sections. Allocate
4459 memory for them. */
4460 plt = FALSE;
4461 relocs = FALSE;
4462 reltext = FALSE;
4463 for (s = dynobj->sections; s != NULL; s = s->next)
4464 {
4465 const char * name;
4466
4467 if ((s->flags & SEC_LINKER_CREATED) == 0)
4468 continue;
4469
4470 /* It's OK to base decisions on the section name, because none
4471 of the dynobj section names depend upon the input files. */
4472 name = bfd_get_section_name (dynobj, s);
4473
4474 if (streq (name, ".plt"))
4475 {
4476 /* Remember whether there is a PLT. */
4477 plt = s->size != 0;
4478 }
4479 else if (CONST_STRNEQ (name, ".rela"))
4480 {
4481 if (s->size != 0)
4482 {
4483 asection * target;
4484
4485 /* Remember whether there are any reloc sections other
4486 than .rela.plt. */
4487 if (! streq (name, ".rela.plt"))
4488 {
4489 const char * outname;
4490
4491 relocs = TRUE;
4492
4493 /* If this relocation section applies to a read only
4494 section, then we probably need a DT_TEXTREL
4495 entry. The entries in the .rela.plt section
4496 really apply to the .got section, which we
4497 created ourselves and so know is not readonly. */
4498 outname = bfd_get_section_name (output_bfd,
4499 s->output_section);
4500 target = bfd_get_section_by_name (output_bfd, outname + 5);
4501 if (target != NULL
4502 && (target->flags & SEC_READONLY) != 0
4503 && (target->flags & SEC_ALLOC) != 0)
4504 reltext = TRUE;
4505 }
4506
4507 /* We use the reloc_count field as a counter if we need
4508 to copy relocs into the output file. */
4509 s->reloc_count = 0;
4510 }
4511 }
4512 else if (! CONST_STRNEQ (name, ".got")
4513 && ! streq (name, ".dynbss"))
4514 /* It's not one of our sections, so don't allocate space. */
4515 continue;
4516
4517 if (s->size == 0)
4518 {
4519 /* If we don't need this section, strip it from the
4520 output file. This is mostly to handle .rela.bss and
4521 .rela.plt. We must create both sections in
4522 create_dynamic_sections, because they must be created
4523 before the linker maps input sections to output
4524 sections. The linker does that before
4525 adjust_dynamic_symbol is called, and it is that
4526 function which decides whether anything needs to go
4527 into these sections. */
4528 s->flags |= SEC_EXCLUDE;
4529 continue;
4530 }
4531
4532 if ((s->flags & SEC_HAS_CONTENTS) == 0)
4533 continue;
4534
4535 /* Allocate memory for the section contents. We use bfd_zalloc
4536 here in case unused entries are not reclaimed before the
4537 section's contents are written out. This should not happen,
4538 but this way if it does, we get a R_MN10300_NONE reloc
4539 instead of garbage. */
4540 s->contents = bfd_zalloc (dynobj, s->size);
4541 if (s->contents == NULL)
4542 return FALSE;
4543 }
4544
4545 if (elf_hash_table (info)->dynamic_sections_created)
4546 {
4547 /* Add some entries to the .dynamic section. We fill in the
4548 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
4549 but we must add the entries now so that we get the correct
4550 size for the .dynamic section. The DT_DEBUG entry is filled
4551 in by the dynamic linker and used by the debugger. */
4552 if (! info->shared)
4553 {
4554 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
4555 return FALSE;
4556 }
4557
4558 if (plt)
4559 {
4560 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
4561 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
4562 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
4563 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
4564 return FALSE;
4565 }
4566
4567 if (relocs)
4568 {
4569 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
4570 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
4571 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
4572 sizeof (Elf32_External_Rela)))
4573 return FALSE;
4574 }
4575
4576 if (reltext)
4577 {
4578 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
4579 return FALSE;
4580 }
4581 }
4582
4583 return TRUE;
4584 }
4585
4586 /* Finish up dynamic symbol handling. We set the contents of various
4587 dynamic sections here. */
4588
4589 static bfd_boolean
4590 _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
4591 struct bfd_link_info * info,
4592 struct elf_link_hash_entry * h,
4593 Elf_Internal_Sym * sym)
4594 {
4595 bfd * dynobj;
4596
4597 dynobj = elf_hash_table (info)->dynobj;
4598
4599 if (h->plt.offset != (bfd_vma) -1)
4600 {
4601 asection * splt;
4602 asection * sgot;
4603 asection * srel;
4604 bfd_vma plt_index;
4605 bfd_vma got_offset;
4606 Elf_Internal_Rela rel;
4607
4608 /* This symbol has an entry in the procedure linkage table. Set
4609 it up. */
4610
4611 BFD_ASSERT (h->dynindx != -1);
4612
4613 splt = bfd_get_section_by_name (dynobj, ".plt");
4614 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4615 srel = bfd_get_section_by_name (dynobj, ".rela.plt");
4616 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
4617
4618 /* Get the index in the procedure linkage table which
4619 corresponds to this symbol. This is the index of this symbol
4620 in all the symbols for which we are making plt entries. The
4621 first entry in the procedure linkage table is reserved. */
4622 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
4623 / elf_mn10300_sizeof_plt (info));
4624
4625 /* Get the offset into the .got table of the entry that
4626 corresponds to this function. Each .got entry is 4 bytes.
4627 The first three are reserved. */
4628 got_offset = (plt_index + 3) * 4;
4629
4630 /* Fill in the entry in the procedure linkage table. */
4631 if (! info->shared)
4632 {
4633 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
4634 elf_mn10300_sizeof_plt (info));
4635 bfd_put_32 (output_bfd,
4636 (sgot->output_section->vma
4637 + sgot->output_offset
4638 + got_offset),
4639 (splt->contents + h->plt.offset
4640 + elf_mn10300_plt_symbol_offset (info)));
4641
4642 bfd_put_32 (output_bfd,
4643 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
4644 (splt->contents + h->plt.offset
4645 + elf_mn10300_plt_plt0_offset (info)));
4646 }
4647 else
4648 {
4649 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
4650 elf_mn10300_sizeof_plt (info));
4651
4652 bfd_put_32 (output_bfd, got_offset,
4653 (splt->contents + h->plt.offset
4654 + elf_mn10300_plt_symbol_offset (info)));
4655 }
4656
4657 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
4658 (splt->contents + h->plt.offset
4659 + elf_mn10300_plt_reloc_offset (info)));
4660
4661 /* Fill in the entry in the global offset table. */
4662 bfd_put_32 (output_bfd,
4663 (splt->output_section->vma
4664 + splt->output_offset
4665 + h->plt.offset
4666 + elf_mn10300_plt_temp_offset (info)),
4667 sgot->contents + got_offset);
4668
4669 /* Fill in the entry in the .rela.plt section. */
4670 rel.r_offset = (sgot->output_section->vma
4671 + sgot->output_offset
4672 + got_offset);
4673 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
4674 rel.r_addend = 0;
4675 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4676 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4677 + plt_index));
4678
4679 if (!h->def_regular)
4680 /* Mark the symbol as undefined, rather than as defined in
4681 the .plt section. Leave the value alone. */
4682 sym->st_shndx = SHN_UNDEF;
4683 }
4684
4685 if (h->got.offset != (bfd_vma) -1)
4686 {
4687 asection * sgot;
4688 asection * srel;
4689 Elf_Internal_Rela rel;
4690
4691 /* This symbol has an entry in the global offset table. Set it up. */
4692 sgot = bfd_get_section_by_name (dynobj, ".got");
4693 srel = bfd_get_section_by_name (dynobj, ".rela.got");
4694 BFD_ASSERT (sgot != NULL && srel != NULL);
4695
4696 rel.r_offset = (sgot->output_section->vma
4697 + sgot->output_offset
4698 + (h->got.offset & ~1));
4699
4700 /* If this is a -Bsymbolic link, and the symbol is defined
4701 locally, we just want to emit a RELATIVE reloc. Likewise if
4702 the symbol was forced to be local because of a version file.
4703 The entry in the global offset table will already have been
4704 initialized in the relocate_section function. */
4705 if (info->shared
4706 && (info->symbolic || h->dynindx == -1)
4707 && h->def_regular)
4708 {
4709 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
4710 rel.r_addend = (h->root.u.def.value
4711 + h->root.u.def.section->output_section->vma
4712 + h->root.u.def.section->output_offset);
4713 }
4714 else
4715 {
4716 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
4717 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
4718 rel.r_addend = 0;
4719 }
4720
4721 bfd_elf32_swap_reloca_out (output_bfd, &rel,
4722 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
4723 + srel->reloc_count));
4724 ++ srel->reloc_count;
4725 }
4726
4727 if (h->needs_copy)
4728 {
4729 asection * s;
4730 Elf_Internal_Rela rel;
4731
4732 /* This symbol needs a copy reloc. Set it up. */
4733 BFD_ASSERT (h->dynindx != -1
4734 && (h->root.type == bfd_link_hash_defined
4735 || h->root.type == bfd_link_hash_defweak));
4736
4737 s = bfd_get_section_by_name (h->root.u.def.section->owner,
4738 ".rela.bss");
4739 BFD_ASSERT (s != NULL);
4740
4741 rel.r_offset = (h->root.u.def.value
4742 + h->root.u.def.section->output_section->vma
4743 + h->root.u.def.section->output_offset);
4744 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
4745 rel.r_addend = 0;
4746 bfd_elf32_swap_reloca_out (output_bfd, & rel,
4747 (bfd_byte *) ((Elf32_External_Rela *) s->contents
4748 + s->reloc_count));
4749 ++ s->reloc_count;
4750 }
4751
4752 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
4753 if (streq (h->root.root.string, "_DYNAMIC")
4754 || h == elf_hash_table (info)->hgot)
4755 sym->st_shndx = SHN_ABS;
4756
4757 return TRUE;
4758 }
4759
4760 /* Finish up the dynamic sections. */
4761
4762 static bfd_boolean
4763 _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
4764 struct bfd_link_info * info)
4765 {
4766 bfd * dynobj;
4767 asection * sgot;
4768 asection * sdyn;
4769
4770 dynobj = elf_hash_table (info)->dynobj;
4771
4772 sgot = bfd_get_section_by_name (dynobj, ".got.plt");
4773 BFD_ASSERT (sgot != NULL);
4774 sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
4775
4776 if (elf_hash_table (info)->dynamic_sections_created)
4777 {
4778 asection * splt;
4779 Elf32_External_Dyn * dyncon;
4780 Elf32_External_Dyn * dynconend;
4781
4782 BFD_ASSERT (sdyn != NULL);
4783
4784 dyncon = (Elf32_External_Dyn *) sdyn->contents;
4785 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4786
4787 for (; dyncon < dynconend; dyncon++)
4788 {
4789 Elf_Internal_Dyn dyn;
4790 const char * name;
4791 asection * s;
4792
4793 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4794
4795 switch (dyn.d_tag)
4796 {
4797 default:
4798 break;
4799
4800 case DT_PLTGOT:
4801 name = ".got";
4802 goto get_vma;
4803
4804 case DT_JMPREL:
4805 name = ".rela.plt";
4806 get_vma:
4807 s = bfd_get_section_by_name (output_bfd, name);
4808 BFD_ASSERT (s != NULL);
4809 dyn.d_un.d_ptr = s->vma;
4810 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4811 break;
4812
4813 case DT_PLTRELSZ:
4814 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4815 BFD_ASSERT (s != NULL);
4816 dyn.d_un.d_val = s->size;
4817 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4818 break;
4819
4820 case DT_RELASZ:
4821 /* My reading of the SVR4 ABI indicates that the
4822 procedure linkage table relocs (DT_JMPREL) should be
4823 included in the overall relocs (DT_RELA). This is
4824 what Solaris does. However, UnixWare can not handle
4825 that case. Therefore, we override the DT_RELASZ entry
4826 here to make it not include the JMPREL relocs. Since
4827 the linker script arranges for .rela.plt to follow all
4828 other relocation sections, we don't have to worry
4829 about changing the DT_RELA entry. */
4830 s = bfd_get_section_by_name (output_bfd, ".rela.plt");
4831 if (s != NULL)
4832 dyn.d_un.d_val -= s->size;
4833 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4834 break;
4835 }
4836 }
4837
4838 /* Fill in the first entry in the procedure linkage table. */
4839 splt = bfd_get_section_by_name (dynobj, ".plt");
4840 if (splt && splt->size > 0)
4841 {
4842 if (info->shared)
4843 {
4844 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
4845 elf_mn10300_sizeof_plt (info));
4846 }
4847 else
4848 {
4849 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
4850 bfd_put_32 (output_bfd,
4851 sgot->output_section->vma + sgot->output_offset + 4,
4852 splt->contents + elf_mn10300_plt0_gotid_offset (info));
4853 bfd_put_32 (output_bfd,
4854 sgot->output_section->vma + sgot->output_offset + 8,
4855 splt->contents + elf_mn10300_plt0_linker_offset (info));
4856 }
4857
4858 /* UnixWare sets the entsize of .plt to 4, although that doesn't
4859 really seem like the right value. */
4860 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
4861 }
4862 }
4863
4864 /* Fill in the first three entries in the global offset table. */
4865 if (sgot->size > 0)
4866 {
4867 if (sdyn == NULL)
4868 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
4869 else
4870 bfd_put_32 (output_bfd,
4871 sdyn->output_section->vma + sdyn->output_offset,
4872 sgot->contents);
4873 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
4874 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
4875 }
4876
4877 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
4878
4879 return TRUE;
4880 }
4881
4882 /* Classify relocation types, such that combreloc can sort them
4883 properly. */
4884
4885 static enum elf_reloc_type_class
4886 _bfd_mn10300_elf_reloc_type_class (const Elf_Internal_Rela *rela)
4887 {
4888 switch ((int) ELF32_R_TYPE (rela->r_info))
4889 {
4890 case R_MN10300_RELATIVE: return reloc_class_relative;
4891 case R_MN10300_JMP_SLOT: return reloc_class_plt;
4892 case R_MN10300_COPY: return reloc_class_copy;
4893 default: return reloc_class_normal;
4894 }
4895 }
4896
4897 #ifndef ELF_ARCH
4898 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
4899 #define TARGET_LITTLE_NAME "elf32-mn10300"
4900 #define ELF_ARCH bfd_arch_mn10300
4901 #define ELF_MACHINE_CODE EM_MN10300
4902 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
4903 #define ELF_MAXPAGESIZE 0x1000
4904 #endif
4905
4906 #define elf_info_to_howto mn10300_info_to_howto
4907 #define elf_info_to_howto_rel 0
4908 #define elf_backend_can_gc_sections 1
4909 #define elf_backend_rela_normal 1
4910 #define elf_backend_check_relocs mn10300_elf_check_relocs
4911 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
4912 #define elf_backend_relocate_section mn10300_elf_relocate_section
4913 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
4914 #define bfd_elf32_bfd_get_relocated_section_contents \
4915 mn10300_elf_get_relocated_section_contents
4916 #define bfd_elf32_bfd_link_hash_table_create \
4917 elf32_mn10300_link_hash_table_create
4918 #define bfd_elf32_bfd_link_hash_table_free \
4919 elf32_mn10300_link_hash_table_free
4920
4921 #ifndef elf_symbol_leading_char
4922 #define elf_symbol_leading_char '_'
4923 #endif
4924
4925 /* So we can set bits in e_flags. */
4926 #define elf_backend_final_write_processing \
4927 _bfd_mn10300_elf_final_write_processing
4928 #define elf_backend_object_p _bfd_mn10300_elf_object_p
4929
4930 #define bfd_elf32_bfd_merge_private_bfd_data \
4931 _bfd_mn10300_elf_merge_private_bfd_data
4932
4933 #define elf_backend_can_gc_sections 1
4934 #define elf_backend_create_dynamic_sections \
4935 _bfd_mn10300_elf_create_dynamic_sections
4936 #define elf_backend_adjust_dynamic_symbol \
4937 _bfd_mn10300_elf_adjust_dynamic_symbol
4938 #define elf_backend_size_dynamic_sections \
4939 _bfd_mn10300_elf_size_dynamic_sections
4940 #define elf_backend_omit_section_dynsym \
4941 ((bfd_boolean (*) (bfd *, struct bfd_link_info *, asection *)) bfd_true)
4942 #define elf_backend_finish_dynamic_symbol \
4943 _bfd_mn10300_elf_finish_dynamic_symbol
4944 #define elf_backend_finish_dynamic_sections \
4945 _bfd_mn10300_elf_finish_dynamic_sections
4946
4947 #define elf_backend_reloc_type_class \
4948 _bfd_mn10300_elf_reloc_type_class
4949
4950 #define elf_backend_want_got_plt 1
4951 #define elf_backend_plt_readonly 1
4952 #define elf_backend_want_plt_sym 0
4953 #define elf_backend_got_header_size 12
4954
4955 #include "elf32-target.h"
This page took 0.154123 seconds and 5 git commands to generate.