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