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