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