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