unrecognized/unsupported reloc message
[deliverable/binutils-gdb.git] / bfd / elf-m10300.c
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996-2018 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 (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
132 == MN10300_ELF_DATA ? ((struct elf32_mn10300_link_hash_table *) ((p)->hash)) : NULL)
133
134 #define elf32_mn10300_link_hash_traverse(table, func, info) \
135 (elf_link_hash_traverse \
136 (&(table)->root, \
137 (bfd_boolean (*) (struct elf_link_hash_entry *, void *)) (func), \
138 (info)))
139
140 static reloc_howto_type elf_mn10300_howto_table[] =
141 {
142 /* Dummy relocation. Does nothing. */
143 HOWTO (R_MN10300_NONE,
144 0,
145 3,
146 0,
147 FALSE,
148 0,
149 complain_overflow_dont,
150 bfd_elf_generic_reloc,
151 "R_MN10300_NONE",
152 FALSE,
153 0,
154 0,
155 FALSE),
156 /* Standard 32 bit reloc. */
157 HOWTO (R_MN10300_32,
158 0,
159 2,
160 32,
161 FALSE,
162 0,
163 complain_overflow_bitfield,
164 bfd_elf_generic_reloc,
165 "R_MN10300_32",
166 FALSE,
167 0xffffffff,
168 0xffffffff,
169 FALSE),
170 /* Standard 16 bit reloc. */
171 HOWTO (R_MN10300_16,
172 0,
173 1,
174 16,
175 FALSE,
176 0,
177 complain_overflow_bitfield,
178 bfd_elf_generic_reloc,
179 "R_MN10300_16",
180 FALSE,
181 0xffff,
182 0xffff,
183 FALSE),
184 /* Standard 8 bit reloc. */
185 HOWTO (R_MN10300_8,
186 0,
187 0,
188 8,
189 FALSE,
190 0,
191 complain_overflow_bitfield,
192 bfd_elf_generic_reloc,
193 "R_MN10300_8",
194 FALSE,
195 0xff,
196 0xff,
197 FALSE),
198 /* Standard 32bit pc-relative reloc. */
199 HOWTO (R_MN10300_PCREL32,
200 0,
201 2,
202 32,
203 TRUE,
204 0,
205 complain_overflow_bitfield,
206 bfd_elf_generic_reloc,
207 "R_MN10300_PCREL32",
208 FALSE,
209 0xffffffff,
210 0xffffffff,
211 TRUE),
212 /* Standard 16bit pc-relative reloc. */
213 HOWTO (R_MN10300_PCREL16,
214 0,
215 1,
216 16,
217 TRUE,
218 0,
219 complain_overflow_bitfield,
220 bfd_elf_generic_reloc,
221 "R_MN10300_PCREL16",
222 FALSE,
223 0xffff,
224 0xffff,
225 TRUE),
226 /* Standard 8 pc-relative reloc. */
227 HOWTO (R_MN10300_PCREL8,
228 0,
229 0,
230 8,
231 TRUE,
232 0,
233 complain_overflow_bitfield,
234 bfd_elf_generic_reloc,
235 "R_MN10300_PCREL8",
236 FALSE,
237 0xff,
238 0xff,
239 TRUE),
240
241 /* GNU extension to record C++ vtable hierarchy. */
242 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
243 0, /* rightshift */
244 0, /* size (0 = byte, 1 = short, 2 = long) */
245 0, /* bitsize */
246 FALSE, /* pc_relative */
247 0, /* bitpos */
248 complain_overflow_dont, /* complain_on_overflow */
249 NULL, /* special_function */
250 "R_MN10300_GNU_VTINHERIT", /* name */
251 FALSE, /* partial_inplace */
252 0, /* src_mask */
253 0, /* dst_mask */
254 FALSE), /* pcrel_offset */
255
256 /* GNU extension to record C++ vtable member usage */
257 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
258 0, /* rightshift */
259 0, /* size (0 = byte, 1 = short, 2 = long) */
260 0, /* bitsize */
261 FALSE, /* pc_relative */
262 0, /* bitpos */
263 complain_overflow_dont, /* complain_on_overflow */
264 NULL, /* special_function */
265 "R_MN10300_GNU_VTENTRY", /* name */
266 FALSE, /* partial_inplace */
267 0, /* src_mask */
268 0, /* dst_mask */
269 FALSE), /* pcrel_offset */
270
271 /* Standard 24 bit reloc. */
272 HOWTO (R_MN10300_24,
273 0,
274 2,
275 24,
276 FALSE,
277 0,
278 complain_overflow_bitfield,
279 bfd_elf_generic_reloc,
280 "R_MN10300_24",
281 FALSE,
282 0xffffff,
283 0xffffff,
284 FALSE),
285 HOWTO (R_MN10300_GOTPC32, /* type */
286 0, /* rightshift */
287 2, /* size (0 = byte, 1 = short, 2 = long) */
288 32, /* bitsize */
289 TRUE, /* pc_relative */
290 0, /* bitpos */
291 complain_overflow_bitfield, /* complain_on_overflow */
292 bfd_elf_generic_reloc, /* */
293 "R_MN10300_GOTPC32", /* name */
294 FALSE, /* partial_inplace */
295 0xffffffff, /* src_mask */
296 0xffffffff, /* dst_mask */
297 TRUE), /* pcrel_offset */
298
299 HOWTO (R_MN10300_GOTPC16, /* type */
300 0, /* rightshift */
301 1, /* size (0 = byte, 1 = short, 2 = long) */
302 16, /* bitsize */
303 TRUE, /* pc_relative */
304 0, /* bitpos */
305 complain_overflow_bitfield, /* complain_on_overflow */
306 bfd_elf_generic_reloc, /* */
307 "R_MN10300_GOTPC16", /* name */
308 FALSE, /* partial_inplace */
309 0xffff, /* src_mask */
310 0xffff, /* dst_mask */
311 TRUE), /* pcrel_offset */
312
313 HOWTO (R_MN10300_GOTOFF32, /* type */
314 0, /* rightshift */
315 2, /* size (0 = byte, 1 = short, 2 = long) */
316 32, /* bitsize */
317 FALSE, /* pc_relative */
318 0, /* bitpos */
319 complain_overflow_bitfield, /* complain_on_overflow */
320 bfd_elf_generic_reloc, /* */
321 "R_MN10300_GOTOFF32", /* name */
322 FALSE, /* partial_inplace */
323 0xffffffff, /* src_mask */
324 0xffffffff, /* dst_mask */
325 FALSE), /* pcrel_offset */
326
327 HOWTO (R_MN10300_GOTOFF24, /* type */
328 0, /* rightshift */
329 2, /* size (0 = byte, 1 = short, 2 = long) */
330 24, /* bitsize */
331 FALSE, /* pc_relative */
332 0, /* bitpos */
333 complain_overflow_bitfield, /* complain_on_overflow */
334 bfd_elf_generic_reloc, /* */
335 "R_MN10300_GOTOFF24", /* name */
336 FALSE, /* partial_inplace */
337 0xffffff, /* src_mask */
338 0xffffff, /* dst_mask */
339 FALSE), /* pcrel_offset */
340
341 HOWTO (R_MN10300_GOTOFF16, /* type */
342 0, /* rightshift */
343 1, /* size (0 = byte, 1 = short, 2 = long) */
344 16, /* bitsize */
345 FALSE, /* pc_relative */
346 0, /* bitpos */
347 complain_overflow_bitfield, /* complain_on_overflow */
348 bfd_elf_generic_reloc, /* */
349 "R_MN10300_GOTOFF16", /* name */
350 FALSE, /* partial_inplace */
351 0xffff, /* src_mask */
352 0xffff, /* dst_mask */
353 FALSE), /* pcrel_offset */
354
355 HOWTO (R_MN10300_PLT32, /* type */
356 0, /* rightshift */
357 2, /* size (0 = byte, 1 = short, 2 = long) */
358 32, /* bitsize */
359 TRUE, /* pc_relative */
360 0, /* bitpos */
361 complain_overflow_bitfield, /* complain_on_overflow */
362 bfd_elf_generic_reloc, /* */
363 "R_MN10300_PLT32", /* name */
364 FALSE, /* partial_inplace */
365 0xffffffff, /* src_mask */
366 0xffffffff, /* dst_mask */
367 TRUE), /* pcrel_offset */
368
369 HOWTO (R_MN10300_PLT16, /* type */
370 0, /* rightshift */
371 1, /* size (0 = byte, 1 = short, 2 = long) */
372 16, /* bitsize */
373 TRUE, /* pc_relative */
374 0, /* bitpos */
375 complain_overflow_bitfield, /* complain_on_overflow */
376 bfd_elf_generic_reloc, /* */
377 "R_MN10300_PLT16", /* name */
378 FALSE, /* partial_inplace */
379 0xffff, /* src_mask */
380 0xffff, /* dst_mask */
381 TRUE), /* pcrel_offset */
382
383 HOWTO (R_MN10300_GOT32, /* type */
384 0, /* rightshift */
385 2, /* size (0 = byte, 1 = short, 2 = long) */
386 32, /* bitsize */
387 FALSE, /* pc_relative */
388 0, /* bitpos */
389 complain_overflow_bitfield, /* complain_on_overflow */
390 bfd_elf_generic_reloc, /* */
391 "R_MN10300_GOT32", /* name */
392 FALSE, /* partial_inplace */
393 0xffffffff, /* src_mask */
394 0xffffffff, /* dst_mask */
395 FALSE), /* pcrel_offset */
396
397 HOWTO (R_MN10300_GOT24, /* type */
398 0, /* rightshift */
399 2, /* size (0 = byte, 1 = short, 2 = long) */
400 24, /* bitsize */
401 FALSE, /* pc_relative */
402 0, /* bitpos */
403 complain_overflow_bitfield, /* complain_on_overflow */
404 bfd_elf_generic_reloc, /* */
405 "R_MN10300_GOT24", /* name */
406 FALSE, /* partial_inplace */
407 0xffffffff, /* src_mask */
408 0xffffffff, /* dst_mask */
409 FALSE), /* pcrel_offset */
410
411 HOWTO (R_MN10300_GOT16, /* type */
412 0, /* rightshift */
413 1, /* size (0 = byte, 1 = short, 2 = long) */
414 16, /* bitsize */
415 FALSE, /* pc_relative */
416 0, /* bitpos */
417 complain_overflow_bitfield, /* complain_on_overflow */
418 bfd_elf_generic_reloc, /* */
419 "R_MN10300_GOT16", /* name */
420 FALSE, /* partial_inplace */
421 0xffffffff, /* src_mask */
422 0xffffffff, /* dst_mask */
423 FALSE), /* pcrel_offset */
424
425 HOWTO (R_MN10300_COPY, /* type */
426 0, /* rightshift */
427 2, /* size (0 = byte, 1 = short, 2 = long) */
428 32, /* bitsize */
429 FALSE, /* pc_relative */
430 0, /* bitpos */
431 complain_overflow_bitfield, /* complain_on_overflow */
432 bfd_elf_generic_reloc, /* */
433 "R_MN10300_COPY", /* name */
434 FALSE, /* partial_inplace */
435 0xffffffff, /* src_mask */
436 0xffffffff, /* dst_mask */
437 FALSE), /* pcrel_offset */
438
439 HOWTO (R_MN10300_GLOB_DAT, /* type */
440 0, /* rightshift */
441 2, /* size (0 = byte, 1 = short, 2 = long) */
442 32, /* bitsize */
443 FALSE, /* pc_relative */
444 0, /* bitpos */
445 complain_overflow_bitfield, /* complain_on_overflow */
446 bfd_elf_generic_reloc, /* */
447 "R_MN10300_GLOB_DAT", /* name */
448 FALSE, /* partial_inplace */
449 0xffffffff, /* src_mask */
450 0xffffffff, /* dst_mask */
451 FALSE), /* pcrel_offset */
452
453 HOWTO (R_MN10300_JMP_SLOT, /* type */
454 0, /* rightshift */
455 2, /* size (0 = byte, 1 = short, 2 = long) */
456 32, /* bitsize */
457 FALSE, /* pc_relative */
458 0, /* bitpos */
459 complain_overflow_bitfield, /* complain_on_overflow */
460 bfd_elf_generic_reloc, /* */
461 "R_MN10300_JMP_SLOT", /* name */
462 FALSE, /* partial_inplace */
463 0xffffffff, /* src_mask */
464 0xffffffff, /* dst_mask */
465 FALSE), /* pcrel_offset */
466
467 HOWTO (R_MN10300_RELATIVE, /* type */
468 0, /* rightshift */
469 2, /* size (0 = byte, 1 = short, 2 = long) */
470 32, /* bitsize */
471 FALSE, /* pc_relative */
472 0, /* bitpos */
473 complain_overflow_bitfield, /* complain_on_overflow */
474 bfd_elf_generic_reloc, /* */
475 "R_MN10300_RELATIVE", /* name */
476 FALSE, /* partial_inplace */
477 0xffffffff, /* src_mask */
478 0xffffffff, /* dst_mask */
479 FALSE), /* pcrel_offset */
480
481 HOWTO (R_MN10300_TLS_GD, /* type */
482 0, /* rightshift */
483 2, /* size (0 = byte, 1 = short, 2 = long) */
484 32, /* bitsize */
485 FALSE, /* pc_relative */
486 0, /* bitpos */
487 complain_overflow_bitfield, /* complain_on_overflow */
488 bfd_elf_generic_reloc, /* */
489 "R_MN10300_TLS_GD", /* name */
490 FALSE, /* partial_inplace */
491 0xffffffff, /* src_mask */
492 0xffffffff, /* dst_mask */
493 FALSE), /* pcrel_offset */
494
495 HOWTO (R_MN10300_TLS_LD, /* type */
496 0, /* rightshift */
497 2, /* size (0 = byte, 1 = short, 2 = long) */
498 32, /* bitsize */
499 FALSE, /* pc_relative */
500 0, /* bitpos */
501 complain_overflow_bitfield, /* complain_on_overflow */
502 bfd_elf_generic_reloc, /* */
503 "R_MN10300_TLS_LD", /* name */
504 FALSE, /* partial_inplace */
505 0xffffffff, /* src_mask */
506 0xffffffff, /* dst_mask */
507 FALSE), /* pcrel_offset */
508
509 HOWTO (R_MN10300_TLS_LDO, /* type */
510 0, /* rightshift */
511 2, /* size (0 = byte, 1 = short, 2 = long) */
512 32, /* bitsize */
513 FALSE, /* pc_relative */
514 0, /* bitpos */
515 complain_overflow_bitfield, /* complain_on_overflow */
516 bfd_elf_generic_reloc, /* */
517 "R_MN10300_TLS_LDO", /* name */
518 FALSE, /* partial_inplace */
519 0xffffffff, /* src_mask */
520 0xffffffff, /* dst_mask */
521 FALSE), /* pcrel_offset */
522
523 HOWTO (R_MN10300_TLS_GOTIE, /* type */
524 0, /* rightshift */
525 2, /* size (0 = byte, 1 = short, 2 = long) */
526 32, /* bitsize */
527 FALSE, /* pc_relative */
528 0, /* bitpos */
529 complain_overflow_bitfield, /* complain_on_overflow */
530 bfd_elf_generic_reloc, /* */
531 "R_MN10300_TLS_GOTIE", /* name */
532 FALSE, /* partial_inplace */
533 0xffffffff, /* src_mask */
534 0xffffffff, /* dst_mask */
535 FALSE), /* pcrel_offset */
536
537 HOWTO (R_MN10300_TLS_IE, /* type */
538 0, /* rightshift */
539 2, /* size (0 = byte, 1 = short, 2 = long) */
540 32, /* bitsize */
541 FALSE, /* pc_relative */
542 0, /* bitpos */
543 complain_overflow_bitfield, /* complain_on_overflow */
544 bfd_elf_generic_reloc, /* */
545 "R_MN10300_TLS_IE", /* name */
546 FALSE, /* partial_inplace */
547 0xffffffff, /* src_mask */
548 0xffffffff, /* dst_mask */
549 FALSE), /* pcrel_offset */
550
551 HOWTO (R_MN10300_TLS_LE, /* type */
552 0, /* rightshift */
553 2, /* size (0 = byte, 1 = short, 2 = long) */
554 32, /* bitsize */
555 FALSE, /* pc_relative */
556 0, /* bitpos */
557 complain_overflow_bitfield, /* complain_on_overflow */
558 bfd_elf_generic_reloc, /* */
559 "R_MN10300_TLS_LE", /* name */
560 FALSE, /* partial_inplace */
561 0xffffffff, /* src_mask */
562 0xffffffff, /* dst_mask */
563 FALSE), /* pcrel_offset */
564
565 HOWTO (R_MN10300_TLS_DTPMOD, /* type */
566 0, /* rightshift */
567 2, /* size (0 = byte, 1 = short, 2 = long) */
568 32, /* bitsize */
569 FALSE, /* pc_relative */
570 0, /* bitpos */
571 complain_overflow_bitfield, /* complain_on_overflow */
572 bfd_elf_generic_reloc, /* */
573 "R_MN10300_TLS_DTPMOD", /* name */
574 FALSE, /* partial_inplace */
575 0xffffffff, /* src_mask */
576 0xffffffff, /* dst_mask */
577 FALSE), /* pcrel_offset */
578
579 HOWTO (R_MN10300_TLS_DTPOFF, /* type */
580 0, /* rightshift */
581 2, /* size (0 = byte, 1 = short, 2 = long) */
582 32, /* bitsize */
583 FALSE, /* pc_relative */
584 0, /* bitpos */
585 complain_overflow_bitfield, /* complain_on_overflow */
586 bfd_elf_generic_reloc, /* */
587 "R_MN10300_TLS_DTPOFF", /* name */
588 FALSE, /* partial_inplace */
589 0xffffffff, /* src_mask */
590 0xffffffff, /* dst_mask */
591 FALSE), /* pcrel_offset */
592
593 HOWTO (R_MN10300_TLS_TPOFF, /* type */
594 0, /* rightshift */
595 2, /* size (0 = byte, 1 = short, 2 = long) */
596 32, /* bitsize */
597 FALSE, /* pc_relative */
598 0, /* bitpos */
599 complain_overflow_bitfield, /* complain_on_overflow */
600 bfd_elf_generic_reloc, /* */
601 "R_MN10300_TLS_TPOFF", /* name */
602 FALSE, /* partial_inplace */
603 0xffffffff, /* src_mask */
604 0xffffffff, /* dst_mask */
605 FALSE), /* pcrel_offset */
606
607 HOWTO (R_MN10300_SYM_DIFF, /* type */
608 0, /* rightshift */
609 2, /* size (0 = byte, 1 = short, 2 = long) */
610 32, /* bitsize */
611 FALSE, /* pc_relative */
612 0, /* bitpos */
613 complain_overflow_dont,/* complain_on_overflow */
614 NULL, /* special handler. */
615 "R_MN10300_SYM_DIFF", /* name */
616 FALSE, /* partial_inplace */
617 0xffffffff, /* src_mask */
618 0xffffffff, /* dst_mask */
619 FALSE), /* pcrel_offset */
620
621 HOWTO (R_MN10300_ALIGN, /* type */
622 0, /* rightshift */
623 0, /* size (0 = byte, 1 = short, 2 = long) */
624 32, /* bitsize */
625 FALSE, /* pc_relative */
626 0, /* bitpos */
627 complain_overflow_dont,/* complain_on_overflow */
628 NULL, /* special handler. */
629 "R_MN10300_ALIGN", /* name */
630 FALSE, /* partial_inplace */
631 0, /* src_mask */
632 0, /* dst_mask */
633 FALSE) /* pcrel_offset */
634 };
635
636 struct mn10300_reloc_map
637 {
638 bfd_reloc_code_real_type bfd_reloc_val;
639 unsigned char elf_reloc_val;
640 };
641
642 static const struct mn10300_reloc_map mn10300_reloc_map[] =
643 {
644 { BFD_RELOC_NONE, R_MN10300_NONE, },
645 { BFD_RELOC_32, R_MN10300_32, },
646 { BFD_RELOC_16, R_MN10300_16, },
647 { BFD_RELOC_8, R_MN10300_8, },
648 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
649 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
650 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
651 { BFD_RELOC_24, R_MN10300_24, },
652 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
653 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
654 { BFD_RELOC_32_GOT_PCREL, R_MN10300_GOTPC32 },
655 { BFD_RELOC_16_GOT_PCREL, R_MN10300_GOTPC16 },
656 { BFD_RELOC_32_GOTOFF, R_MN10300_GOTOFF32 },
657 { BFD_RELOC_MN10300_GOTOFF24, R_MN10300_GOTOFF24 },
658 { BFD_RELOC_16_GOTOFF, R_MN10300_GOTOFF16 },
659 { BFD_RELOC_32_PLT_PCREL, R_MN10300_PLT32 },
660 { BFD_RELOC_16_PLT_PCREL, R_MN10300_PLT16 },
661 { BFD_RELOC_MN10300_GOT32, R_MN10300_GOT32 },
662 { BFD_RELOC_MN10300_GOT24, R_MN10300_GOT24 },
663 { BFD_RELOC_MN10300_GOT16, R_MN10300_GOT16 },
664 { BFD_RELOC_MN10300_COPY, R_MN10300_COPY },
665 { BFD_RELOC_MN10300_GLOB_DAT, R_MN10300_GLOB_DAT },
666 { BFD_RELOC_MN10300_JMP_SLOT, R_MN10300_JMP_SLOT },
667 { BFD_RELOC_MN10300_RELATIVE, R_MN10300_RELATIVE },
668 { BFD_RELOC_MN10300_TLS_GD, R_MN10300_TLS_GD },
669 { BFD_RELOC_MN10300_TLS_LD, R_MN10300_TLS_LD },
670 { BFD_RELOC_MN10300_TLS_LDO, R_MN10300_TLS_LDO },
671 { BFD_RELOC_MN10300_TLS_GOTIE, R_MN10300_TLS_GOTIE },
672 { BFD_RELOC_MN10300_TLS_IE, R_MN10300_TLS_IE },
673 { BFD_RELOC_MN10300_TLS_LE, R_MN10300_TLS_LE },
674 { BFD_RELOC_MN10300_TLS_DTPMOD, R_MN10300_TLS_DTPMOD },
675 { BFD_RELOC_MN10300_TLS_DTPOFF, R_MN10300_TLS_DTPOFF },
676 { BFD_RELOC_MN10300_TLS_TPOFF, R_MN10300_TLS_TPOFF },
677 { BFD_RELOC_MN10300_SYM_DIFF, R_MN10300_SYM_DIFF },
678 { BFD_RELOC_MN10300_ALIGN, R_MN10300_ALIGN }
679 };
680
681 /* Create the GOT section. */
682
683 static bfd_boolean
684 _bfd_mn10300_elf_create_got_section (bfd * abfd,
685 struct bfd_link_info * info)
686 {
687 flagword flags;
688 flagword pltflags;
689 asection * s;
690 struct elf_link_hash_entry * h;
691 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
692 struct elf_link_hash_table *htab;
693 int ptralign;
694
695 /* This function may be called more than once. */
696 htab = elf_hash_table (info);
697 if (htab->sgot != NULL)
698 return TRUE;
699
700 switch (bed->s->arch_size)
701 {
702 case 32:
703 ptralign = 2;
704 break;
705
706 case 64:
707 ptralign = 3;
708 break;
709
710 default:
711 bfd_set_error (bfd_error_bad_value);
712 return FALSE;
713 }
714
715 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
716 | SEC_LINKER_CREATED);
717
718 pltflags = flags;
719 pltflags |= SEC_CODE;
720 if (bed->plt_not_loaded)
721 pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
722 if (bed->plt_readonly)
723 pltflags |= SEC_READONLY;
724
725 s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
726 htab->splt = s;
727 if (s == NULL
728 || ! bfd_set_section_alignment (abfd, s, bed->plt_alignment))
729 return FALSE;
730
731 /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
732 .plt section. */
733 if (bed->want_plt_sym)
734 {
735 h = _bfd_elf_define_linkage_sym (abfd, info, s,
736 "_PROCEDURE_LINKAGE_TABLE_");
737 htab->hplt = h;
738 if (h == NULL)
739 return FALSE;
740 }
741
742 s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
743 htab->sgot = s;
744 if (s == NULL
745 || ! bfd_set_section_alignment (abfd, s, ptralign))
746 return FALSE;
747
748 if (bed->want_got_plt)
749 {
750 s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
751 htab->sgotplt = s;
752 if (s == NULL
753 || ! bfd_set_section_alignment (abfd, s, ptralign))
754 return FALSE;
755 }
756
757 /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
758 (or .got.plt) section. We don't do this in the linker script
759 because we don't want to define the symbol if we are not creating
760 a global offset table. */
761 h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
762 htab->hgot = h;
763 if (h == NULL)
764 return FALSE;
765
766 /* The first bit of the global offset table is the header. */
767 s->size += bed->got_header_size;
768
769 return TRUE;
770 }
771
772 static reloc_howto_type *
773 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
774 bfd_reloc_code_real_type code)
775 {
776 unsigned int i;
777
778 for (i = ARRAY_SIZE (mn10300_reloc_map); i--;)
779 if (mn10300_reloc_map[i].bfd_reloc_val == code)
780 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
781
782 return NULL;
783 }
784
785 static reloc_howto_type *
786 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
787 const char *r_name)
788 {
789 unsigned int i;
790
791 for (i = ARRAY_SIZE (elf_mn10300_howto_table); i--;)
792 if (elf_mn10300_howto_table[i].name != NULL
793 && strcasecmp (elf_mn10300_howto_table[i].name, r_name) == 0)
794 return elf_mn10300_howto_table + i;
795
796 return NULL;
797 }
798
799 /* Set the howto pointer for an MN10300 ELF reloc. */
800
801 static void
802 mn10300_info_to_howto (bfd *abfd,
803 arelent *cache_ptr,
804 Elf_Internal_Rela *dst)
805 {
806 unsigned int r_type;
807
808 r_type = ELF32_R_TYPE (dst->r_info);
809 if (r_type >= R_MN10300_MAX)
810 {
811 /* xgettext:c-format */
812 _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
813 abfd, r_type);
814 bfd_set_error (bfd_error_bad_value);
815 r_type = R_MN10300_NONE;
816 }
817 cache_ptr->howto = elf_mn10300_howto_table + r_type;
818 }
819
820 static int
821 elf_mn10300_tls_transition (struct bfd_link_info * info,
822 int r_type,
823 struct elf_link_hash_entry * h,
824 asection * sec,
825 bfd_boolean counting)
826 {
827 bfd_boolean is_local;
828
829 if (r_type == R_MN10300_TLS_GD
830 && h != NULL
831 && elf_mn10300_hash_entry (h)->tls_type == GOT_TLS_IE)
832 return R_MN10300_TLS_GOTIE;
833
834 if (bfd_link_pic (info))
835 return r_type;
836
837 if (! (sec->flags & SEC_CODE))
838 return r_type;
839
840 if (! counting && h != NULL && ! elf_hash_table (info)->dynamic_sections_created)
841 is_local = TRUE;
842 else
843 is_local = SYMBOL_CALLS_LOCAL (info, h);
844
845 /* For the main program, these are the transitions we do. */
846 switch (r_type)
847 {
848 case R_MN10300_TLS_GD: return is_local ? R_MN10300_TLS_LE : R_MN10300_TLS_GOTIE;
849 case R_MN10300_TLS_LD: return R_MN10300_NONE;
850 case R_MN10300_TLS_LDO: return R_MN10300_TLS_LE;
851 case R_MN10300_TLS_IE:
852 case R_MN10300_TLS_GOTIE: return is_local ? R_MN10300_TLS_LE : r_type;
853 }
854
855 return r_type;
856 }
857
858 /* Return the relocation value for @tpoff relocation
859 if STT_TLS virtual address is ADDRESS. */
860
861 static bfd_vma
862 dtpoff (struct bfd_link_info * info, bfd_vma address)
863 {
864 struct elf_link_hash_table *htab = elf_hash_table (info);
865
866 /* If tls_sec is NULL, we should have signalled an error already. */
867 if (htab->tls_sec == NULL)
868 return 0;
869 return address - htab->tls_sec->vma;
870 }
871
872 /* Return the relocation value for @tpoff relocation
873 if STT_TLS virtual address is ADDRESS. */
874
875 static bfd_vma
876 tpoff (struct bfd_link_info * info, bfd_vma address)
877 {
878 struct elf_link_hash_table *htab = elf_hash_table (info);
879
880 /* If tls_sec is NULL, we should have signalled an error already. */
881 if (htab->tls_sec == NULL)
882 return 0;
883 return address - (htab->tls_size + htab->tls_sec->vma);
884 }
885
886 /* Returns nonzero if there's a R_MN10300_PLT32 reloc that we now need
887 to skip, after this one. The actual value is the offset between
888 this reloc and the PLT reloc. */
889
890 static int
891 mn10300_do_tls_transition (bfd * input_bfd,
892 unsigned int r_type,
893 unsigned int tls_r_type,
894 bfd_byte * contents,
895 bfd_vma offset)
896 {
897 bfd_byte *op = contents + offset;
898 int gotreg = 0;
899
900 #define TLS_PAIR(r1,r2) ((r1) * R_MN10300_MAX + (r2))
901
902 /* This is common to all GD/LD transitions, so break it out. */
903 if (r_type == R_MN10300_TLS_GD
904 || r_type == R_MN10300_TLS_LD)
905 {
906 op -= 2;
907 /* mov imm,d0. */
908 BFD_ASSERT (bfd_get_8 (input_bfd, op) == 0xFC);
909 BFD_ASSERT (bfd_get_8 (input_bfd, op + 1) == 0xCC);
910 /* add aN,d0. */
911 BFD_ASSERT (bfd_get_8 (input_bfd, op + 6) == 0xF1);
912 gotreg = (bfd_get_8 (input_bfd, op + 7) & 0x0c) >> 2;
913 /* Call. */
914 BFD_ASSERT (bfd_get_8 (input_bfd, op + 8) == 0xDD);
915 }
916
917 switch (TLS_PAIR (r_type, tls_r_type))
918 {
919 case TLS_PAIR (R_MN10300_TLS_GD, R_MN10300_TLS_GOTIE):
920 {
921 /* Keep track of which register we put GOTptr in. */
922 /* mov (_x@indntpoff,a2),a0. */
923 memcpy (op, "\xFC\x20\x00\x00\x00\x00", 6);
924 op[1] |= gotreg;
925 /* add e2,a0. */
926 memcpy (op+6, "\xF9\x78\x28", 3);
927 /* or 0x00000000, d0 - six byte nop. */
928 memcpy (op+9, "\xFC\xE4\x00\x00\x00\x00", 6);
929 }
930 return 7;
931
932 case TLS_PAIR (R_MN10300_TLS_GD, R_MN10300_TLS_LE):
933 {
934 /* Register is *always* a0. */
935 /* mov _x@tpoff,a0. */
936 memcpy (op, "\xFC\xDC\x00\x00\x00\x00", 6);
937 /* add e2,a0. */
938 memcpy (op+6, "\xF9\x78\x28", 3);
939 /* or 0x00000000, d0 - six byte nop. */
940 memcpy (op+9, "\xFC\xE4\x00\x00\x00\x00", 6);
941 }
942 return 7;
943 case TLS_PAIR (R_MN10300_TLS_LD, R_MN10300_NONE):
944 {
945 /* Register is *always* a0. */
946 /* mov e2,a0. */
947 memcpy (op, "\xF5\x88", 2);
948 /* or 0x00000000, d0 - six byte nop. */
949 memcpy (op+2, "\xFC\xE4\x00\x00\x00\x00", 6);
950 /* or 0x00000000, e2 - seven byte nop. */
951 memcpy (op+8, "\xFE\x19\x22\x00\x00\x00\x00", 7);
952 }
953 return 7;
954
955 case TLS_PAIR (R_MN10300_TLS_LDO, R_MN10300_TLS_LE):
956 /* No changes needed, just the reloc change. */
957 return 0;
958
959 /* These are a little tricky, because we have to detect which
960 opcode is being used (they're different sizes, with the reloc
961 at different offsets within the opcode) and convert each
962 accordingly, copying the operands as needed. The conversions
963 we do are as follows (IE,GOTIE,LE):
964
965 1111 1100 1010 01Dn [-- abs32 --] MOV (x@indntpoff),Dn
966 1111 1100 0000 DnAm [-- abs32 --] MOV (x@gotntpoff,Am),Dn
967 1111 1100 1100 11Dn [-- abs32 --] MOV x@tpoff,Dn
968
969 1111 1100 1010 00An [-- abs32 --] MOV (x@indntpoff),An
970 1111 1100 0010 AnAm [-- abs32 --] MOV (x@gotntpoff,Am),An
971 1111 1100 1101 11An [-- abs32 --] MOV x@tpoff,An
972
973 1111 1110 0000 1110 Rnnn Xxxx [-- abs32 --] MOV (x@indntpoff),Rn
974 1111 1110 0000 1010 Rnnn Rmmm [-- abs32 --] MOV (x@indntpoff,Rm),Rn
975 1111 1110 0000 1000 Rnnn Xxxx [-- abs32 --] MOV x@tpoff,Rn
976
977 Since the GOT pointer is always $a2, we assume the last
978 normally won't happen, but let's be paranoid and plan for the
979 day that GCC optimizes it somewhow. */
980
981 case TLS_PAIR (R_MN10300_TLS_IE, R_MN10300_TLS_LE):
982 if (op[-2] == 0xFC)
983 {
984 op -= 2;
985 if ((op[1] & 0xFC) == 0xA4) /* Dn */
986 {
987 op[1] &= 0x03; /* Leaves Dn. */
988 op[1] |= 0xCC;
989 }
990 else /* An */
991 {
992 op[1] &= 0x03; /* Leaves An. */
993 op[1] |= 0xDC;
994 }
995 }
996 else if (op[-3] == 0xFE)
997 op[-2] = 0x08;
998 else
999 abort ();
1000 break;
1001
1002 case TLS_PAIR (R_MN10300_TLS_GOTIE, R_MN10300_TLS_LE):
1003 if (op[-2] == 0xFC)
1004 {
1005 op -= 2;
1006 if ((op[1] & 0xF0) == 0x00) /* Dn */
1007 {
1008 op[1] &= 0x0C; /* Leaves Dn. */
1009 op[1] >>= 2;
1010 op[1] |= 0xCC;
1011 }
1012 else /* An */
1013 {
1014 op[1] &= 0x0C; /* Leaves An. */
1015 op[1] >>= 2;
1016 op[1] |= 0xDC;
1017 }
1018 }
1019 else if (op[-3] == 0xFE)
1020 op[-2] = 0x08;
1021 else
1022 abort ();
1023 break;
1024
1025 default:
1026 _bfd_error_handler
1027 /* xgettext:c-format */
1028 (_("%pB: Unsupported transition from %s to %s"),
1029 input_bfd,
1030 elf_mn10300_howto_table[r_type].name,
1031 elf_mn10300_howto_table[tls_r_type].name);
1032 break;
1033 }
1034 #undef TLS_PAIR
1035 return 0;
1036 }
1037
1038 /* Look through the relocs for a section during the first phase.
1039 Since we don't do .gots or .plts, we just need to consider the
1040 virtual table relocs for gc. */
1041
1042 static bfd_boolean
1043 mn10300_elf_check_relocs (bfd *abfd,
1044 struct bfd_link_info *info,
1045 asection *sec,
1046 const Elf_Internal_Rela *relocs)
1047 {
1048 struct elf32_mn10300_link_hash_table * htab = elf32_mn10300_hash_table (info);
1049 bfd_boolean sym_diff_reloc_seen;
1050 Elf_Internal_Shdr *symtab_hdr;
1051 Elf_Internal_Sym * isymbuf = NULL;
1052 struct elf_link_hash_entry **sym_hashes;
1053 const Elf_Internal_Rela *rel;
1054 const Elf_Internal_Rela *rel_end;
1055 bfd * dynobj;
1056 bfd_vma * local_got_offsets;
1057 asection * sgot;
1058 asection * srelgot;
1059 asection * sreloc;
1060 bfd_boolean result = FALSE;
1061
1062 sgot = NULL;
1063 srelgot = NULL;
1064 sreloc = NULL;
1065
1066 if (bfd_link_relocatable (info))
1067 return TRUE;
1068
1069 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1070 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1071 sym_hashes = elf_sym_hashes (abfd);
1072
1073 dynobj = elf_hash_table (info)->dynobj;
1074 local_got_offsets = elf_local_got_offsets (abfd);
1075 rel_end = relocs + sec->reloc_count;
1076 sym_diff_reloc_seen = FALSE;
1077
1078 for (rel = relocs; rel < rel_end; rel++)
1079 {
1080 struct elf_link_hash_entry *h;
1081 unsigned long r_symndx;
1082 unsigned int r_type;
1083 int tls_type = GOT_NORMAL;
1084
1085 r_symndx = ELF32_R_SYM (rel->r_info);
1086 if (r_symndx < symtab_hdr->sh_info)
1087 h = NULL;
1088 else
1089 {
1090 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1091 while (h->root.type == bfd_link_hash_indirect
1092 || h->root.type == bfd_link_hash_warning)
1093 h = (struct elf_link_hash_entry *) h->root.u.i.link;
1094 }
1095
1096 r_type = ELF32_R_TYPE (rel->r_info);
1097 r_type = elf_mn10300_tls_transition (info, r_type, h, sec, TRUE);
1098
1099 /* Some relocs require a global offset table. */
1100 if (dynobj == NULL)
1101 {
1102 switch (r_type)
1103 {
1104 case R_MN10300_GOT32:
1105 case R_MN10300_GOT24:
1106 case R_MN10300_GOT16:
1107 case R_MN10300_GOTOFF32:
1108 case R_MN10300_GOTOFF24:
1109 case R_MN10300_GOTOFF16:
1110 case R_MN10300_GOTPC32:
1111 case R_MN10300_GOTPC16:
1112 case R_MN10300_TLS_GD:
1113 case R_MN10300_TLS_LD:
1114 case R_MN10300_TLS_GOTIE:
1115 case R_MN10300_TLS_IE:
1116 elf_hash_table (info)->dynobj = dynobj = abfd;
1117 if (! _bfd_mn10300_elf_create_got_section (dynobj, info))
1118 goto fail;
1119 break;
1120
1121 default:
1122 break;
1123 }
1124 }
1125
1126 switch (r_type)
1127 {
1128 /* This relocation describes the C++ object vtable hierarchy.
1129 Reconstruct it for later use during GC. */
1130 case R_MN10300_GNU_VTINHERIT:
1131 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1132 goto fail;
1133 break;
1134
1135 /* This relocation describes which C++ vtable entries are actually
1136 used. Record for later use during GC. */
1137 case R_MN10300_GNU_VTENTRY:
1138 BFD_ASSERT (h != NULL);
1139 if (h != NULL
1140 && !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 (isymbuf != NULL && 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 /* _32 relocs in executables force _COPY relocs,
2071 such that the address of the symbol ends up
2072 being local. */
2073 && !bfd_link_executable (info)
2074 && !SYMBOL_REFERENCES_LOCAL (info, hh)
2075 && ((input_section->flags & SEC_ALLOC) != 0
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 (input_bfd, 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 bfd_size_type 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 bfd_size_type 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 (internal_relocs != NULL
2917 && elf_section_data (section)->relocs != internal_relocs)
2918 free (internal_relocs);
2919 internal_relocs = NULL;
2920
2921 /* Cache or free any memory we allocated for the contents. */
2922 if (contents != NULL
2923 && elf_section_data (section)->this_hdr.contents != contents)
2924 {
2925 if (! link_info->keep_memory)
2926 free (contents);
2927 else
2928 {
2929 /* Cache the section contents for elf_link_input_bfd. */
2930 elf_section_data (section)->this_hdr.contents = contents;
2931 }
2932 }
2933 contents = NULL;
2934 }
2935
2936 /* Cache or free any memory we allocated for the symbols. */
2937 if (isymbuf != NULL
2938 && symtab_hdr->contents != (unsigned char *) isymbuf)
2939 {
2940 if (! link_info->keep_memory)
2941 free (isymbuf);
2942 else
2943 {
2944 /* Cache the symbols for elf_link_input_bfd. */
2945 symtab_hdr->contents = (unsigned char *) isymbuf;
2946 }
2947 }
2948 isymbuf = NULL;
2949 }
2950
2951 /* Now iterate on each symbol in the hash table and perform
2952 the final initialization steps on each. */
2953 elf32_mn10300_link_hash_traverse (hash_table,
2954 elf32_mn10300_finish_hash_table_entry,
2955 link_info);
2956 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2957 elf32_mn10300_finish_hash_table_entry,
2958 link_info);
2959
2960 {
2961 /* This section of code collects all our local symbols, sorts
2962 them by value, and looks for multiple symbols referring to
2963 the same address. For those symbols, the flags are merged.
2964 At this point, the only flag that can be set is
2965 MN10300_CONVERT_CALL_TO_CALLS, so we simply OR the flags
2966 together. */
2967 int static_count = 0, i;
2968 struct elf32_mn10300_link_hash_entry **entries;
2969 struct elf32_mn10300_link_hash_entry **ptr;
2970
2971 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2972 elf32_mn10300_count_hash_table_entries,
2973 &static_count);
2974
2975 entries = bfd_malloc (static_count * sizeof (* ptr));
2976
2977 ptr = entries;
2978 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
2979 elf32_mn10300_list_hash_table_entries,
2980 & ptr);
2981
2982 qsort (entries, static_count, sizeof (entries[0]), sort_by_value);
2983
2984 for (i = 0; i < static_count - 1; i++)
2985 if (entries[i]->value && entries[i]->value == entries[i+1]->value)
2986 {
2987 int v = entries[i]->flags;
2988 int j;
2989
2990 for (j = i + 1; j < static_count && entries[j]->value == entries[i]->value; j++)
2991 v |= entries[j]->flags;
2992
2993 for (j = i; j < static_count && entries[j]->value == entries[i]->value; j++)
2994 entries[j]->flags = v;
2995
2996 i = j - 1;
2997 }
2998 }
2999
3000 /* All entries in the hash table are fully initialized. */
3001 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
3002
3003 /* Now that everything has been initialized, go through each
3004 code section and delete any prologue insns which will be
3005 redundant because their operations will be performed by
3006 a "call" instruction. */
3007 for (input_bfd = link_info->input_bfds;
3008 input_bfd != NULL;
3009 input_bfd = input_bfd->link.next)
3010 {
3011 /* We're going to need all the local symbols for each bfd. */
3012 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
3013 if (symtab_hdr->sh_info != 0)
3014 {
3015 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3016 if (isymbuf == NULL)
3017 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
3018 symtab_hdr->sh_info, 0,
3019 NULL, NULL, NULL);
3020 if (isymbuf == NULL)
3021 goto error_return;
3022 }
3023
3024 /* Walk over each section in this bfd. */
3025 for (section = input_bfd->sections;
3026 section != NULL;
3027 section = section->next)
3028 {
3029 unsigned int sec_shndx;
3030 Elf_Internal_Sym *isym, *isymend;
3031 struct elf_link_hash_entry **hashes;
3032 struct elf_link_hash_entry **end_hashes;
3033 unsigned int symcount;
3034
3035 /* Skip non-code sections and empty sections. */
3036 if ((section->flags & SEC_CODE) == 0 || section->size == 0)
3037 continue;
3038
3039 if (section->reloc_count != 0)
3040 {
3041 /* Get a copy of the native relocations. */
3042 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, section,
3043 NULL, NULL,
3044 link_info->keep_memory);
3045 if (internal_relocs == NULL)
3046 goto error_return;
3047 }
3048
3049 /* Get cached copy of section contents if it exists. */
3050 if (elf_section_data (section)->this_hdr.contents != NULL)
3051 contents = elf_section_data (section)->this_hdr.contents;
3052 else
3053 {
3054 /* Go get them off disk. */
3055 if (!bfd_malloc_and_get_section (input_bfd, section,
3056 &contents))
3057 goto error_return;
3058 }
3059
3060 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
3061 section);
3062
3063 /* Now look for any function in this section which needs
3064 insns deleted from its prologue. */
3065 isymend = isymbuf + symtab_hdr->sh_info;
3066 for (isym = isymbuf; isym < isymend; isym++)
3067 {
3068 struct elf32_mn10300_link_hash_entry *sym_hash;
3069 asection *sym_sec = NULL;
3070 const char *sym_name;
3071 char *new_name;
3072 struct elf_link_hash_table *elftab;
3073 bfd_size_type amt;
3074
3075 if (isym->st_shndx != sec_shndx)
3076 continue;
3077
3078 if (isym->st_shndx == SHN_UNDEF)
3079 sym_sec = bfd_und_section_ptr;
3080 else if (isym->st_shndx == SHN_ABS)
3081 sym_sec = bfd_abs_section_ptr;
3082 else if (isym->st_shndx == SHN_COMMON)
3083 sym_sec = bfd_com_section_ptr;
3084 else
3085 sym_sec
3086 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
3087
3088 sym_name
3089 = bfd_elf_string_from_elf_section (input_bfd,
3090 symtab_hdr->sh_link,
3091 isym->st_name);
3092
3093 /* Tack on an ID so we can uniquely identify this
3094 local symbol in the global hash table. */
3095 amt = strlen (sym_name) + 10;
3096 new_name = bfd_malloc (amt);
3097 if (new_name == NULL)
3098 goto error_return;
3099 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
3100 sym_name = new_name;
3101
3102 elftab = & hash_table->static_hash_table->root;
3103 sym_hash = (struct elf32_mn10300_link_hash_entry *)
3104 elf_link_hash_lookup (elftab, sym_name,
3105 FALSE, FALSE, FALSE);
3106
3107 free (new_name);
3108 if (sym_hash == NULL)
3109 continue;
3110
3111 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
3112 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
3113 {
3114 int bytes = 0;
3115
3116 /* Note that we've changed things. */
3117 elf_section_data (section)->relocs = internal_relocs;
3118 elf_section_data (section)->this_hdr.contents = contents;
3119 symtab_hdr->contents = (unsigned char *) isymbuf;
3120
3121 /* Count how many bytes we're going to delete. */
3122 if (sym_hash->movm_args)
3123 bytes += 2;
3124
3125 if (sym_hash->stack_size > 0)
3126 {
3127 if (sym_hash->stack_size <= 128)
3128 bytes += 3;
3129 else
3130 bytes += 4;
3131 }
3132
3133 /* Note that we've deleted prologue bytes for this
3134 function. */
3135 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
3136
3137 /* Actually delete the bytes. */
3138 if (!mn10300_elf_relax_delete_bytes (input_bfd,
3139 section,
3140 isym->st_value,
3141 bytes))
3142 goto error_return;
3143
3144 /* Something changed. Not strictly necessary, but
3145 may lead to more relaxing opportunities. */
3146 *again = TRUE;
3147 }
3148 }
3149
3150 /* Look for any global functions in this section which
3151 need insns deleted from their prologues. */
3152 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
3153 - symtab_hdr->sh_info);
3154 hashes = elf_sym_hashes (input_bfd);
3155 end_hashes = hashes + symcount;
3156 for (; hashes < end_hashes; hashes++)
3157 {
3158 struct elf32_mn10300_link_hash_entry *sym_hash;
3159
3160 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
3161 if ((sym_hash->root.root.type == bfd_link_hash_defined
3162 || sym_hash->root.root.type == bfd_link_hash_defweak)
3163 && sym_hash->root.root.u.def.section == section
3164 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
3165 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
3166 {
3167 int bytes = 0;
3168 bfd_vma symval;
3169 struct elf_link_hash_entry **hh;
3170
3171 /* Note that we've changed things. */
3172 elf_section_data (section)->relocs = internal_relocs;
3173 elf_section_data (section)->this_hdr.contents = contents;
3174 symtab_hdr->contents = (unsigned char *) isymbuf;
3175
3176 /* Count how many bytes we're going to delete. */
3177 if (sym_hash->movm_args)
3178 bytes += 2;
3179
3180 if (sym_hash->stack_size > 0)
3181 {
3182 if (sym_hash->stack_size <= 128)
3183 bytes += 3;
3184 else
3185 bytes += 4;
3186 }
3187
3188 /* Note that we've deleted prologue bytes for this
3189 function. */
3190 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
3191
3192 /* Actually delete the bytes. */
3193 symval = sym_hash->root.root.u.def.value;
3194 if (!mn10300_elf_relax_delete_bytes (input_bfd,
3195 section,
3196 symval,
3197 bytes))
3198 goto error_return;
3199
3200 /* There may be other C++ functions symbols with the same
3201 address. If so then mark these as having had their
3202 prologue bytes deleted as well. */
3203 for (hh = elf_sym_hashes (input_bfd); hh < end_hashes; hh++)
3204 {
3205 struct elf32_mn10300_link_hash_entry *h;
3206
3207 h = (struct elf32_mn10300_link_hash_entry *) * hh;
3208
3209 if (h != sym_hash
3210 && (h->root.root.type == bfd_link_hash_defined
3211 || h->root.root.type == bfd_link_hash_defweak)
3212 && h->root.root.u.def.section == section
3213 && ! (h->flags & MN10300_CONVERT_CALL_TO_CALLS)
3214 && h->root.root.u.def.value == symval
3215 && h->root.type == STT_FUNC)
3216 h->flags |= MN10300_DELETED_PROLOGUE_BYTES;
3217 }
3218
3219 /* Something changed. Not strictly necessary, but
3220 may lead to more relaxing opportunities. */
3221 *again = TRUE;
3222 }
3223 }
3224
3225 /* Cache or free any memory we allocated for the relocs. */
3226 if (internal_relocs != NULL
3227 && elf_section_data (section)->relocs != internal_relocs)
3228 free (internal_relocs);
3229 internal_relocs = NULL;
3230
3231 /* Cache or free any memory we allocated for the contents. */
3232 if (contents != NULL
3233 && elf_section_data (section)->this_hdr.contents != contents)
3234 {
3235 if (! link_info->keep_memory)
3236 free (contents);
3237 else
3238 /* Cache the section contents for elf_link_input_bfd. */
3239 elf_section_data (section)->this_hdr.contents = contents;
3240 }
3241 contents = NULL;
3242 }
3243
3244 /* Cache or free any memory we allocated for the symbols. */
3245 if (isymbuf != NULL
3246 && symtab_hdr->contents != (unsigned char *) isymbuf)
3247 {
3248 if (! link_info->keep_memory)
3249 free (isymbuf);
3250 else
3251 /* Cache the symbols for elf_link_input_bfd. */
3252 symtab_hdr->contents = (unsigned char *) isymbuf;
3253 }
3254 isymbuf = NULL;
3255 }
3256 }
3257
3258 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
3259 contents = NULL;
3260 internal_relocs = NULL;
3261 isymbuf = NULL;
3262 /* For error_return. */
3263 section = sec;
3264
3265 /* We don't have to do anything for a relocatable link, if
3266 this section does not have relocs, or if this is not a
3267 code section. */
3268 if (bfd_link_relocatable (link_info)
3269 || (sec->flags & SEC_RELOC) == 0
3270 || sec->reloc_count == 0
3271 || (sec->flags & SEC_CODE) == 0)
3272 return TRUE;
3273
3274 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
3275
3276 /* Get a copy of the native relocations. */
3277 internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
3278 link_info->keep_memory);
3279 if (internal_relocs == NULL)
3280 goto error_return;
3281
3282 /* Scan for worst case alignment gap changes. Note that this logic
3283 is not ideal; what we should do is run this scan for every
3284 opcode/address range and adjust accordingly, but that's
3285 expensive. Worst case is that for an alignment of N bytes, we
3286 move by 2*N-N-1 bytes, assuming we have aligns of 1, 2, 4, 8, etc
3287 all before it. Plus, this still doesn't cover cross-section
3288 jumps with section alignment. */
3289 irelend = internal_relocs + sec->reloc_count;
3290 align_gap_adjustment = 0;
3291 for (irel = internal_relocs; irel < irelend; irel++)
3292 {
3293 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_ALIGN)
3294 {
3295 bfd_vma adj = 1 << irel->r_addend;
3296 bfd_vma aend = irel->r_offset;
3297
3298 aend = BFD_ALIGN (aend, 1 << irel->r_addend);
3299 adj = 2 * adj - adj - 1;
3300
3301 /* Record the biggest adjustmnet. Skip any alignment at the
3302 end of our section. */
3303 if (align_gap_adjustment < adj
3304 && aend < sec->output_section->vma + sec->output_offset + sec->size)
3305 align_gap_adjustment = adj;
3306 }
3307 }
3308
3309 /* Walk through them looking for relaxing opportunities. */
3310 irelend = internal_relocs + sec->reloc_count;
3311 for (irel = internal_relocs; irel < irelend; irel++)
3312 {
3313 bfd_vma symval;
3314 bfd_signed_vma jump_offset;
3315 asection *sym_sec = NULL;
3316 struct elf32_mn10300_link_hash_entry *h = NULL;
3317
3318 /* If this isn't something that can be relaxed, then ignore
3319 this reloc. */
3320 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
3321 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
3322 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
3323 continue;
3324
3325 /* Get the section contents if we haven't done so already. */
3326 if (contents == NULL)
3327 {
3328 /* Get cached copy if it exists. */
3329 if (elf_section_data (sec)->this_hdr.contents != NULL)
3330 contents = elf_section_data (sec)->this_hdr.contents;
3331 else
3332 {
3333 /* Go get them off disk. */
3334 if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3335 goto error_return;
3336 }
3337 }
3338
3339 /* Read this BFD's symbols if we haven't done so already. */
3340 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3341 {
3342 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3343 if (isymbuf == NULL)
3344 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3345 symtab_hdr->sh_info, 0,
3346 NULL, NULL, NULL);
3347 if (isymbuf == NULL)
3348 goto error_return;
3349 }
3350
3351 /* Get the value of the symbol referred to by the reloc. */
3352 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
3353 {
3354 Elf_Internal_Sym *isym;
3355 const char *sym_name;
3356 char *new_name;
3357
3358 /* A local symbol. */
3359 isym = isymbuf + ELF32_R_SYM (irel->r_info);
3360 if (isym->st_shndx == SHN_UNDEF)
3361 sym_sec = bfd_und_section_ptr;
3362 else if (isym->st_shndx == SHN_ABS)
3363 sym_sec = bfd_abs_section_ptr;
3364 else if (isym->st_shndx == SHN_COMMON)
3365 sym_sec = bfd_com_section_ptr;
3366 else
3367 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
3368
3369 sym_name = bfd_elf_string_from_elf_section (abfd,
3370 symtab_hdr->sh_link,
3371 isym->st_name);
3372
3373 if ((sym_sec->flags & SEC_MERGE)
3374 && sym_sec->sec_info_type == SEC_INFO_TYPE_MERGE)
3375 {
3376 symval = isym->st_value;
3377
3378 /* GAS may reduce relocations against symbols in SEC_MERGE
3379 sections to a relocation against the section symbol when
3380 the original addend was zero. When the reloc is against
3381 a section symbol we should include the addend in the
3382 offset passed to _bfd_merged_section_offset, since the
3383 location of interest is the original symbol. On the
3384 other hand, an access to "sym+addend" where "sym" is not
3385 a section symbol should not include the addend; Such an
3386 access is presumed to be an offset from "sym"; The
3387 location of interest is just "sym". */
3388 if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
3389 symval += irel->r_addend;
3390
3391 symval = _bfd_merged_section_offset (abfd, & sym_sec,
3392 elf_section_data (sym_sec)->sec_info,
3393 symval);
3394
3395 if (ELF_ST_TYPE (isym->st_info) != STT_SECTION)
3396 symval += irel->r_addend;
3397
3398 symval += sym_sec->output_section->vma
3399 + sym_sec->output_offset - irel->r_addend;
3400 }
3401 else
3402 symval = (isym->st_value
3403 + sym_sec->output_section->vma
3404 + sym_sec->output_offset);
3405
3406 /* Tack on an ID so we can uniquely identify this
3407 local symbol in the global hash table. */
3408 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
3409 if (new_name == NULL)
3410 goto error_return;
3411 sprintf (new_name, "%s_%08x", sym_name, sym_sec->id);
3412 sym_name = new_name;
3413
3414 h = (struct elf32_mn10300_link_hash_entry *)
3415 elf_link_hash_lookup (&hash_table->static_hash_table->root,
3416 sym_name, FALSE, FALSE, FALSE);
3417 free (new_name);
3418 }
3419 else
3420 {
3421 unsigned long indx;
3422
3423 /* An external symbol. */
3424 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3425 h = (struct elf32_mn10300_link_hash_entry *)
3426 (elf_sym_hashes (abfd)[indx]);
3427 BFD_ASSERT (h != NULL);
3428 if (h->root.root.type != bfd_link_hash_defined
3429 && h->root.root.type != bfd_link_hash_defweak)
3430 /* This appears to be a reference to an undefined
3431 symbol. Just ignore it--it will be caught by the
3432 regular reloc processing. */
3433 continue;
3434
3435 /* Check for a reference to a discarded symbol and ignore it. */
3436 if (h->root.root.u.def.section->output_section == NULL)
3437 continue;
3438
3439 sym_sec = h->root.root.u.def.section->output_section;
3440
3441 symval = (h->root.root.u.def.value
3442 + h->root.root.u.def.section->output_section->vma
3443 + h->root.root.u.def.section->output_offset);
3444 }
3445
3446 /* For simplicity of coding, we are going to modify the section
3447 contents, the section relocs, and the BFD symbol table. We
3448 must tell the rest of the code not to free up this
3449 information. It would be possible to instead create a table
3450 of changes which have to be made, as is done in coff-mips.c;
3451 that would be more work, but would require less memory when
3452 the linker is run. */
3453
3454 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
3455 branch/call, also deal with "call" -> "calls" conversions and
3456 insertion of prologue data into "call" instructions. */
3457 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32
3458 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32)
3459 {
3460 bfd_vma value = symval;
3461
3462 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PLT32
3463 && h != NULL
3464 && ELF_ST_VISIBILITY (h->root.other) != STV_INTERNAL
3465 && ELF_ST_VISIBILITY (h->root.other) != STV_HIDDEN
3466 && h->root.plt.offset != (bfd_vma) -1)
3467 {
3468 asection * splt;
3469
3470 splt = hash_table->root.splt;
3471 value = ((splt->output_section->vma
3472 + splt->output_offset
3473 + h->root.plt.offset)
3474 - (sec->output_section->vma
3475 + sec->output_offset
3476 + irel->r_offset));
3477 }
3478
3479 /* If we've got a "call" instruction that needs to be turned
3480 into a "calls" instruction, do so now. It saves a byte. */
3481 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3482 {
3483 unsigned char code;
3484
3485 /* Get the opcode. */
3486 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3487
3488 /* Make sure we're working with a "call" instruction! */
3489 if (code == 0xdd)
3490 {
3491 /* Note that we've changed the relocs, section contents,
3492 etc. */
3493 elf_section_data (sec)->relocs = internal_relocs;
3494 elf_section_data (sec)->this_hdr.contents = contents;
3495 symtab_hdr->contents = (unsigned char *) isymbuf;
3496
3497 /* Fix the opcode. */
3498 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
3499 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3500
3501 /* Fix irel->r_offset and irel->r_addend. */
3502 irel->r_offset += 1;
3503 irel->r_addend += 1;
3504
3505 /* Delete one byte of data. */
3506 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3507 irel->r_offset + 3, 1))
3508 goto error_return;
3509
3510 /* That will change things, so, we should relax again.
3511 Note that this is not required, and it may be slow. */
3512 *again = TRUE;
3513 }
3514 }
3515 else if (h)
3516 {
3517 /* We've got a "call" instruction which needs some data
3518 from target function filled in. */
3519 unsigned char code;
3520
3521 /* Get the opcode. */
3522 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3523
3524 /* Insert data from the target function into the "call"
3525 instruction if needed. */
3526 if (code == 0xdd)
3527 {
3528 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
3529 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3530 contents + irel->r_offset + 5);
3531 }
3532 }
3533
3534 /* Deal with pc-relative gunk. */
3535 value -= (sec->output_section->vma + sec->output_offset);
3536 value -= irel->r_offset;
3537 value += irel->r_addend;
3538
3539 /* See if the value will fit in 16 bits, note the high value is
3540 0x7fff + 2 as the target will be two bytes closer if we are
3541 able to relax, if it's in the same section. */
3542 if (sec->output_section == sym_sec->output_section)
3543 jump_offset = 0x8001;
3544 else
3545 jump_offset = 0x7fff;
3546
3547 /* Account for jumps across alignment boundaries using
3548 align_gap_adjustment. */
3549 if ((bfd_signed_vma) value < jump_offset - (bfd_signed_vma) align_gap_adjustment
3550 && ((bfd_signed_vma) value > -0x8000 + (bfd_signed_vma) align_gap_adjustment))
3551 {
3552 unsigned char code;
3553
3554 /* Get the opcode. */
3555 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3556
3557 if (code != 0xdc && code != 0xdd && code != 0xff)
3558 continue;
3559
3560 /* Note that we've changed the relocs, section contents, etc. */
3561 elf_section_data (sec)->relocs = internal_relocs;
3562 elf_section_data (sec)->this_hdr.contents = contents;
3563 symtab_hdr->contents = (unsigned char *) isymbuf;
3564
3565 /* Fix the opcode. */
3566 if (code == 0xdc)
3567 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
3568 else if (code == 0xdd)
3569 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
3570 else if (code == 0xff)
3571 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
3572
3573 /* Fix the relocation's type. */
3574 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3575 (ELF32_R_TYPE (irel->r_info)
3576 == (int) R_MN10300_PLT32)
3577 ? R_MN10300_PLT16 :
3578 R_MN10300_PCREL16);
3579
3580 /* Delete two bytes of data. */
3581 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3582 irel->r_offset + 1, 2))
3583 goto error_return;
3584
3585 /* That will change things, so, we should relax again.
3586 Note that this is not required, and it may be slow. */
3587 *again = TRUE;
3588 }
3589 }
3590
3591 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
3592 branch. */
3593 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
3594 {
3595 bfd_vma value = symval;
3596
3597 /* If we've got a "call" instruction that needs to be turned
3598 into a "calls" instruction, do so now. It saves a byte. */
3599 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
3600 {
3601 unsigned char code;
3602
3603 /* Get the opcode. */
3604 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3605
3606 /* Make sure we're working with a "call" instruction! */
3607 if (code == 0xcd)
3608 {
3609 /* Note that we've changed the relocs, section contents,
3610 etc. */
3611 elf_section_data (sec)->relocs = internal_relocs;
3612 elf_section_data (sec)->this_hdr.contents = contents;
3613 symtab_hdr->contents = (unsigned char *) isymbuf;
3614
3615 /* Fix the opcode. */
3616 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
3617 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
3618
3619 /* Fix irel->r_offset and irel->r_addend. */
3620 irel->r_offset += 1;
3621 irel->r_addend += 1;
3622
3623 /* Delete one byte of data. */
3624 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3625 irel->r_offset + 1, 1))
3626 goto error_return;
3627
3628 /* That will change things, so, we should relax again.
3629 Note that this is not required, and it may be slow. */
3630 *again = TRUE;
3631 }
3632 }
3633 else if (h)
3634 {
3635 unsigned char code;
3636
3637 /* Get the opcode. */
3638 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3639
3640 /* Insert data from the target function into the "call"
3641 instruction if needed. */
3642 if (code == 0xcd)
3643 {
3644 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
3645 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
3646 contents + irel->r_offset + 3);
3647 }
3648 }
3649
3650 /* Deal with pc-relative gunk. */
3651 value -= (sec->output_section->vma + sec->output_offset);
3652 value -= irel->r_offset;
3653 value += irel->r_addend;
3654
3655 /* See if the value will fit in 8 bits, note the high value is
3656 0x7f + 1 as the target will be one bytes closer if we are
3657 able to relax. */
3658 if ((long) value < 0x80 && (long) value > -0x80)
3659 {
3660 unsigned char code;
3661
3662 /* Get the opcode. */
3663 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3664
3665 if (code != 0xcc)
3666 continue;
3667
3668 /* Note that we've changed the relocs, section contents, etc. */
3669 elf_section_data (sec)->relocs = internal_relocs;
3670 elf_section_data (sec)->this_hdr.contents = contents;
3671 symtab_hdr->contents = (unsigned char *) isymbuf;
3672
3673 /* Fix the opcode. */
3674 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
3675
3676 /* Fix the relocation's type. */
3677 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3678 R_MN10300_PCREL8);
3679
3680 /* Delete one byte of data. */
3681 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3682 irel->r_offset + 1, 1))
3683 goto error_return;
3684
3685 /* That will change things, so, we should relax again.
3686 Note that this is not required, and it may be slow. */
3687 *again = TRUE;
3688 }
3689 }
3690
3691 /* Try to eliminate an unconditional 8 bit pc-relative branch
3692 which immediately follows a conditional 8 bit pc-relative
3693 branch around the unconditional branch.
3694
3695 original: new:
3696 bCC lab1 bCC' lab2
3697 bra lab2
3698 lab1: lab1:
3699
3700 This happens when the bCC can't reach lab2 at assembly time,
3701 but due to other relaxations it can reach at link time. */
3702 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
3703 {
3704 Elf_Internal_Rela *nrel;
3705 bfd_vma value = symval;
3706 unsigned char code;
3707
3708 /* Deal with pc-relative gunk. */
3709 value -= (sec->output_section->vma + sec->output_offset);
3710 value -= irel->r_offset;
3711 value += irel->r_addend;
3712
3713 /* Do nothing if this reloc is the last byte in the section. */
3714 if (irel->r_offset == sec->size)
3715 continue;
3716
3717 /* See if the next instruction is an unconditional pc-relative
3718 branch, more often than not this test will fail, so we
3719 test it first to speed things up. */
3720 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
3721 if (code != 0xca)
3722 continue;
3723
3724 /* Also make sure the next relocation applies to the next
3725 instruction and that it's a pc-relative 8 bit branch. */
3726 nrel = irel + 1;
3727 if (nrel == irelend
3728 || irel->r_offset + 2 != nrel->r_offset
3729 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
3730 continue;
3731
3732 /* Make sure our destination immediately follows the
3733 unconditional branch. */
3734 if (symval != (sec->output_section->vma + sec->output_offset
3735 + irel->r_offset + 3))
3736 continue;
3737
3738 /* Now make sure we are a conditional branch. This may not
3739 be necessary, but why take the chance.
3740
3741 Note these checks assume that R_MN10300_PCREL8 relocs
3742 only occur on bCC and bCCx insns. If they occured
3743 elsewhere, we'd need to know the start of this insn
3744 for this check to be accurate. */
3745 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
3746 if (code != 0xc0 && code != 0xc1 && code != 0xc2
3747 && code != 0xc3 && code != 0xc4 && code != 0xc5
3748 && code != 0xc6 && code != 0xc7 && code != 0xc8
3749 && code != 0xc9 && code != 0xe8 && code != 0xe9
3750 && code != 0xea && code != 0xeb)
3751 continue;
3752
3753 /* We also have to be sure there is no symbol/label
3754 at the unconditional branch. */
3755 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
3756 irel->r_offset + 1))
3757 continue;
3758
3759 /* Note that we've changed the relocs, section contents, etc. */
3760 elf_section_data (sec)->relocs = internal_relocs;
3761 elf_section_data (sec)->this_hdr.contents = contents;
3762 symtab_hdr->contents = (unsigned char *) isymbuf;
3763
3764 /* Reverse the condition of the first branch. */
3765 switch (code)
3766 {
3767 case 0xc8:
3768 code = 0xc9;
3769 break;
3770 case 0xc9:
3771 code = 0xc8;
3772 break;
3773 case 0xc0:
3774 code = 0xc2;
3775 break;
3776 case 0xc2:
3777 code = 0xc0;
3778 break;
3779 case 0xc3:
3780 code = 0xc1;
3781 break;
3782 case 0xc1:
3783 code = 0xc3;
3784 break;
3785 case 0xc4:
3786 code = 0xc6;
3787 break;
3788 case 0xc6:
3789 code = 0xc4;
3790 break;
3791 case 0xc7:
3792 code = 0xc5;
3793 break;
3794 case 0xc5:
3795 code = 0xc7;
3796 break;
3797 case 0xe8:
3798 code = 0xe9;
3799 break;
3800 case 0x9d:
3801 code = 0xe8;
3802 break;
3803 case 0xea:
3804 code = 0xeb;
3805 break;
3806 case 0xeb:
3807 code = 0xea;
3808 break;
3809 }
3810 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
3811
3812 /* Set the reloc type and symbol for the first branch
3813 from the second branch. */
3814 irel->r_info = nrel->r_info;
3815
3816 /* Make the reloc for the second branch a null reloc. */
3817 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
3818 R_MN10300_NONE);
3819
3820 /* Delete two bytes of data. */
3821 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3822 irel->r_offset + 1, 2))
3823 goto error_return;
3824
3825 /* That will change things, so, we should relax again.
3826 Note that this is not required, and it may be slow. */
3827 *again = TRUE;
3828 }
3829
3830 /* Try to turn a 24 immediate, displacement or absolute address
3831 into a 8 immediate, displacement or absolute address. */
3832 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
3833 {
3834 bfd_vma value = symval;
3835 value += irel->r_addend;
3836
3837 /* See if the value will fit in 8 bits. */
3838 if ((long) value < 0x7f && (long) value > -0x80)
3839 {
3840 unsigned char code;
3841
3842 /* AM33 insns which have 24 operands are 6 bytes long and
3843 will have 0xfd as the first byte. */
3844
3845 /* Get the first opcode. */
3846 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3847
3848 if (code == 0xfd)
3849 {
3850 /* Get the second opcode. */
3851 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3852
3853 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3854 equivalent instructions exists. */
3855 if (code != 0x6b && code != 0x7b
3856 && code != 0x8b && code != 0x9b
3857 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3858 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3859 || (code & 0x0f) == 0x0e))
3860 {
3861 /* Not safe if the high bit is on as relaxing may
3862 move the value out of high mem and thus not fit
3863 in a signed 8bit value. This is currently over
3864 conservative. */
3865 if ((value & 0x80) == 0)
3866 {
3867 /* Note that we've changed the relocation contents,
3868 etc. */
3869 elf_section_data (sec)->relocs = internal_relocs;
3870 elf_section_data (sec)->this_hdr.contents = contents;
3871 symtab_hdr->contents = (unsigned char *) isymbuf;
3872
3873 /* Fix the opcode. */
3874 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
3875 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3876
3877 /* Fix the relocation's type. */
3878 irel->r_info =
3879 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3880 R_MN10300_8);
3881
3882 /* Delete two bytes of data. */
3883 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3884 irel->r_offset + 1, 2))
3885 goto error_return;
3886
3887 /* That will change things, so, we should relax
3888 again. Note that this is not required, and it
3889 may be slow. */
3890 *again = TRUE;
3891 break;
3892 }
3893 }
3894 }
3895 }
3896 }
3897
3898 /* Try to turn a 32bit immediate, displacement or absolute address
3899 into a 16bit immediate, displacement or absolute address. */
3900 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32
3901 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32
3902 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3903 {
3904 bfd_vma value = symval;
3905
3906 if (ELF32_R_TYPE (irel->r_info) != (int) R_MN10300_32)
3907 {
3908 asection * sgot;
3909
3910 sgot = hash_table->root.sgot;
3911 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOT32)
3912 {
3913 value = sgot->output_offset;
3914
3915 if (h)
3916 value += h->root.got.offset;
3917 else
3918 value += (elf_local_got_offsets
3919 (abfd)[ELF32_R_SYM (irel->r_info)]);
3920 }
3921 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTOFF32)
3922 value -= sgot->output_section->vma;
3923 else if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_GOTPC32)
3924 value = (sgot->output_section->vma
3925 - (sec->output_section->vma
3926 + sec->output_offset
3927 + irel->r_offset));
3928 else
3929 abort ();
3930 }
3931
3932 value += irel->r_addend;
3933
3934 /* See if the value will fit in 24 bits.
3935 We allow any 16bit match here. We prune those we can't
3936 handle below. */
3937 if ((long) value < 0x7fffff && (long) value > -0x800000)
3938 {
3939 unsigned char code;
3940
3941 /* AM33 insns which have 32bit operands are 7 bytes long and
3942 will have 0xfe as the first byte. */
3943
3944 /* Get the first opcode. */
3945 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
3946
3947 if (code == 0xfe)
3948 {
3949 /* Get the second opcode. */
3950 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
3951
3952 /* All the am33 32 -> 24 relaxing possibilities. */
3953 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
3954 equivalent instructions exists. */
3955 if (code != 0x6b && code != 0x7b
3956 && code != 0x8b && code != 0x9b
3957 && (ELF32_R_TYPE (irel->r_info)
3958 != (int) R_MN10300_GOTPC32)
3959 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
3960 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
3961 || (code & 0x0f) == 0x0e))
3962 {
3963 /* Not safe if the high bit is on as relaxing may
3964 move the value out of high mem and thus not fit
3965 in a signed 16bit value. This is currently over
3966 conservative. */
3967 if ((value & 0x8000) == 0)
3968 {
3969 /* Note that we've changed the relocation contents,
3970 etc. */
3971 elf_section_data (sec)->relocs = internal_relocs;
3972 elf_section_data (sec)->this_hdr.contents = contents;
3973 symtab_hdr->contents = (unsigned char *) isymbuf;
3974
3975 /* Fix the opcode. */
3976 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
3977 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
3978
3979 /* Fix the relocation's type. */
3980 irel->r_info =
3981 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
3982 (ELF32_R_TYPE (irel->r_info)
3983 == (int) R_MN10300_GOTOFF32)
3984 ? R_MN10300_GOTOFF24
3985 : (ELF32_R_TYPE (irel->r_info)
3986 == (int) R_MN10300_GOT32)
3987 ? R_MN10300_GOT24 :
3988 R_MN10300_24);
3989
3990 /* Delete one byte of data. */
3991 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
3992 irel->r_offset + 3, 1))
3993 goto error_return;
3994
3995 /* That will change things, so, we should relax
3996 again. Note that this is not required, and it
3997 may be slow. */
3998 *again = TRUE;
3999 break;
4000 }
4001 }
4002 }
4003 }
4004
4005 /* See if the value will fit in 16 bits.
4006 We allow any 16bit match here. We prune those we can't
4007 handle below. */
4008 if ((long) value < 0x7fff && (long) value > -0x8000)
4009 {
4010 unsigned char code;
4011
4012 /* Most insns which have 32bit operands are 6 bytes long;
4013 exceptions are pcrel insns and bit insns.
4014
4015 We handle pcrel insns above. We don't bother trying
4016 to handle the bit insns here.
4017
4018 The first byte of the remaining insns will be 0xfc. */
4019
4020 /* Get the first opcode. */
4021 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
4022
4023 if (code != 0xfc)
4024 continue;
4025
4026 /* Get the second opcode. */
4027 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
4028
4029 if ((code & 0xf0) < 0x80)
4030 switch (code & 0xf0)
4031 {
4032 /* mov (d32,am),dn -> mov (d32,am),dn
4033 mov dm,(d32,am) -> mov dn,(d32,am)
4034 mov (d32,am),an -> mov (d32,am),an
4035 mov dm,(d32,am) -> mov dn,(d32,am)
4036 movbu (d32,am),dn -> movbu (d32,am),dn
4037 movbu dm,(d32,am) -> movbu dn,(d32,am)
4038 movhu (d32,am),dn -> movhu (d32,am),dn
4039 movhu dm,(d32,am) -> movhu dn,(d32,am) */
4040 case 0x00:
4041 case 0x10:
4042 case 0x20:
4043 case 0x30:
4044 case 0x40:
4045 case 0x50:
4046 case 0x60:
4047 case 0x70:
4048 /* Not safe if the high bit is on as relaxing may
4049 move the value out of high mem and thus not fit
4050 in a signed 16bit value. */
4051 if (code == 0xcc
4052 && (value & 0x8000))
4053 continue;
4054
4055 /* Note that we've changed the relocation contents, etc. */
4056 elf_section_data (sec)->relocs = internal_relocs;
4057 elf_section_data (sec)->this_hdr.contents = contents;
4058 symtab_hdr->contents = (unsigned char *) isymbuf;
4059
4060 /* Fix the opcode. */
4061 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4062 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4063
4064 /* Fix the relocation's type. */
4065 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4066 (ELF32_R_TYPE (irel->r_info)
4067 == (int) R_MN10300_GOTOFF32)
4068 ? R_MN10300_GOTOFF16
4069 : (ELF32_R_TYPE (irel->r_info)
4070 == (int) R_MN10300_GOT32)
4071 ? R_MN10300_GOT16
4072 : (ELF32_R_TYPE (irel->r_info)
4073 == (int) R_MN10300_GOTPC32)
4074 ? R_MN10300_GOTPC16 :
4075 R_MN10300_16);
4076
4077 /* Delete two bytes of data. */
4078 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4079 irel->r_offset + 2, 2))
4080 goto error_return;
4081
4082 /* That will change things, so, we should relax again.
4083 Note that this is not required, and it may be slow. */
4084 *again = TRUE;
4085 break;
4086 }
4087 else if ((code & 0xf0) == 0x80
4088 || (code & 0xf0) == 0x90)
4089 switch (code & 0xf3)
4090 {
4091 /* mov dn,(abs32) -> mov dn,(abs16)
4092 movbu dn,(abs32) -> movbu dn,(abs16)
4093 movhu dn,(abs32) -> movhu dn,(abs16) */
4094 case 0x81:
4095 case 0x82:
4096 case 0x83:
4097 /* Note that we've changed the relocation contents, etc. */
4098 elf_section_data (sec)->relocs = internal_relocs;
4099 elf_section_data (sec)->this_hdr.contents = contents;
4100 symtab_hdr->contents = (unsigned char *) isymbuf;
4101
4102 if ((code & 0xf3) == 0x81)
4103 code = 0x01 + (code & 0x0c);
4104 else if ((code & 0xf3) == 0x82)
4105 code = 0x02 + (code & 0x0c);
4106 else if ((code & 0xf3) == 0x83)
4107 code = 0x03 + (code & 0x0c);
4108 else
4109 abort ();
4110
4111 /* Fix the opcode. */
4112 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
4113
4114 /* Fix the relocation's type. */
4115 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4116 (ELF32_R_TYPE (irel->r_info)
4117 == (int) R_MN10300_GOTOFF32)
4118 ? R_MN10300_GOTOFF16
4119 : (ELF32_R_TYPE (irel->r_info)
4120 == (int) R_MN10300_GOT32)
4121 ? R_MN10300_GOT16
4122 : (ELF32_R_TYPE (irel->r_info)
4123 == (int) R_MN10300_GOTPC32)
4124 ? R_MN10300_GOTPC16 :
4125 R_MN10300_16);
4126
4127 /* The opcode got shorter too, so we have to fix the
4128 addend and offset too! */
4129 irel->r_offset -= 1;
4130
4131 /* Delete three bytes of data. */
4132 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4133 irel->r_offset + 1, 3))
4134 goto error_return;
4135
4136 /* That will change things, so, we should relax again.
4137 Note that this is not required, and it may be slow. */
4138 *again = TRUE;
4139 break;
4140
4141 /* mov am,(abs32) -> mov am,(abs16)
4142 mov am,(d32,sp) -> mov am,(d16,sp)
4143 mov dm,(d32,sp) -> mov dm,(d32,sp)
4144 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
4145 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
4146 case 0x80:
4147 case 0x90:
4148 case 0x91:
4149 case 0x92:
4150 case 0x93:
4151 /* sp-based offsets are zero-extended. */
4152 if (code >= 0x90 && code <= 0x93
4153 && (long) value < 0)
4154 continue;
4155
4156 /* Note that we've changed the relocation contents, etc. */
4157 elf_section_data (sec)->relocs = internal_relocs;
4158 elf_section_data (sec)->this_hdr.contents = contents;
4159 symtab_hdr->contents = (unsigned char *) isymbuf;
4160
4161 /* Fix the opcode. */
4162 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4163 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4164
4165 /* Fix the relocation's type. */
4166 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4167 (ELF32_R_TYPE (irel->r_info)
4168 == (int) R_MN10300_GOTOFF32)
4169 ? R_MN10300_GOTOFF16
4170 : (ELF32_R_TYPE (irel->r_info)
4171 == (int) R_MN10300_GOT32)
4172 ? R_MN10300_GOT16
4173 : (ELF32_R_TYPE (irel->r_info)
4174 == (int) R_MN10300_GOTPC32)
4175 ? R_MN10300_GOTPC16 :
4176 R_MN10300_16);
4177
4178 /* Delete two bytes of data. */
4179 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4180 irel->r_offset + 2, 2))
4181 goto error_return;
4182
4183 /* That will change things, so, we should relax again.
4184 Note that this is not required, and it may be slow. */
4185 *again = TRUE;
4186 break;
4187 }
4188 else if ((code & 0xf0) < 0xf0)
4189 switch (code & 0xfc)
4190 {
4191 /* mov imm32,dn -> mov imm16,dn
4192 mov imm32,an -> mov imm16,an
4193 mov (abs32),dn -> mov (abs16),dn
4194 movbu (abs32),dn -> movbu (abs16),dn
4195 movhu (abs32),dn -> movhu (abs16),dn */
4196 case 0xcc:
4197 case 0xdc:
4198 case 0xa4:
4199 case 0xa8:
4200 case 0xac:
4201 /* Not safe if the high bit is on as relaxing may
4202 move the value out of high mem and thus not fit
4203 in a signed 16bit value. */
4204 if (code == 0xcc
4205 && (value & 0x8000))
4206 continue;
4207
4208 /* "mov imm16, an" zero-extends the immediate. */
4209 if ((code & 0xfc) == 0xdc
4210 && (long) value < 0)
4211 continue;
4212
4213 /* Note that we've changed the relocation contents, etc. */
4214 elf_section_data (sec)->relocs = internal_relocs;
4215 elf_section_data (sec)->this_hdr.contents = contents;
4216 symtab_hdr->contents = (unsigned char *) isymbuf;
4217
4218 if ((code & 0xfc) == 0xcc)
4219 code = 0x2c + (code & 0x03);
4220 else if ((code & 0xfc) == 0xdc)
4221 code = 0x24 + (code & 0x03);
4222 else if ((code & 0xfc) == 0xa4)
4223 code = 0x30 + (code & 0x03);
4224 else if ((code & 0xfc) == 0xa8)
4225 code = 0x34 + (code & 0x03);
4226 else if ((code & 0xfc) == 0xac)
4227 code = 0x38 + (code & 0x03);
4228 else
4229 abort ();
4230
4231 /* Fix the opcode. */
4232 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
4233
4234 /* Fix the relocation's type. */
4235 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4236 (ELF32_R_TYPE (irel->r_info)
4237 == (int) R_MN10300_GOTOFF32)
4238 ? R_MN10300_GOTOFF16
4239 : (ELF32_R_TYPE (irel->r_info)
4240 == (int) R_MN10300_GOT32)
4241 ? R_MN10300_GOT16
4242 : (ELF32_R_TYPE (irel->r_info)
4243 == (int) R_MN10300_GOTPC32)
4244 ? R_MN10300_GOTPC16 :
4245 R_MN10300_16);
4246
4247 /* The opcode got shorter too, so we have to fix the
4248 addend and offset too! */
4249 irel->r_offset -= 1;
4250
4251 /* Delete three bytes of data. */
4252 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4253 irel->r_offset + 1, 3))
4254 goto error_return;
4255
4256 /* That will change things, so, we should relax again.
4257 Note that this is not required, and it may be slow. */
4258 *again = TRUE;
4259 break;
4260
4261 /* mov (abs32),an -> mov (abs16),an
4262 mov (d32,sp),an -> mov (d16,sp),an
4263 mov (d32,sp),dn -> mov (d16,sp),dn
4264 movbu (d32,sp),dn -> movbu (d16,sp),dn
4265 movhu (d32,sp),dn -> movhu (d16,sp),dn
4266 add imm32,dn -> add imm16,dn
4267 cmp imm32,dn -> cmp imm16,dn
4268 add imm32,an -> add imm16,an
4269 cmp imm32,an -> cmp imm16,an
4270 and imm32,dn -> and imm16,dn
4271 or imm32,dn -> or imm16,dn
4272 xor imm32,dn -> xor imm16,dn
4273 btst imm32,dn -> btst imm16,dn */
4274
4275 case 0xa0:
4276 case 0xb0:
4277 case 0xb1:
4278 case 0xb2:
4279 case 0xb3:
4280 case 0xc0:
4281 case 0xc8:
4282
4283 case 0xd0:
4284 case 0xd8:
4285 case 0xe0:
4286 case 0xe1:
4287 case 0xe2:
4288 case 0xe3:
4289 /* cmp imm16, an zero-extends the immediate. */
4290 if (code == 0xdc
4291 && (long) value < 0)
4292 continue;
4293
4294 /* So do sp-based offsets. */
4295 if (code >= 0xb0 && code <= 0xb3
4296 && (long) value < 0)
4297 continue;
4298
4299 /* Note that we've changed the relocation contents, etc. */
4300 elf_section_data (sec)->relocs = internal_relocs;
4301 elf_section_data (sec)->this_hdr.contents = contents;
4302 symtab_hdr->contents = (unsigned char *) isymbuf;
4303
4304 /* Fix the opcode. */
4305 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4306 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
4307
4308 /* Fix the relocation's type. */
4309 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4310 (ELF32_R_TYPE (irel->r_info)
4311 == (int) R_MN10300_GOTOFF32)
4312 ? R_MN10300_GOTOFF16
4313 : (ELF32_R_TYPE (irel->r_info)
4314 == (int) R_MN10300_GOT32)
4315 ? R_MN10300_GOT16
4316 : (ELF32_R_TYPE (irel->r_info)
4317 == (int) R_MN10300_GOTPC32)
4318 ? R_MN10300_GOTPC16 :
4319 R_MN10300_16);
4320
4321 /* Delete two bytes of data. */
4322 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4323 irel->r_offset + 2, 2))
4324 goto error_return;
4325
4326 /* That will change things, so, we should relax again.
4327 Note that this is not required, and it may be slow. */
4328 *again = TRUE;
4329 break;
4330 }
4331 else if (code == 0xfe)
4332 {
4333 /* add imm32,sp -> add imm16,sp */
4334
4335 /* Note that we've changed the relocation contents, etc. */
4336 elf_section_data (sec)->relocs = internal_relocs;
4337 elf_section_data (sec)->this_hdr.contents = contents;
4338 symtab_hdr->contents = (unsigned char *) isymbuf;
4339
4340 /* Fix the opcode. */
4341 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
4342 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
4343
4344 /* Fix the relocation's type. */
4345 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
4346 (ELF32_R_TYPE (irel->r_info)
4347 == (int) R_MN10300_GOT32)
4348 ? R_MN10300_GOT16
4349 : (ELF32_R_TYPE (irel->r_info)
4350 == (int) R_MN10300_GOTOFF32)
4351 ? R_MN10300_GOTOFF16
4352 : (ELF32_R_TYPE (irel->r_info)
4353 == (int) R_MN10300_GOTPC32)
4354 ? R_MN10300_GOTPC16 :
4355 R_MN10300_16);
4356
4357 /* Delete two bytes of data. */
4358 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
4359 irel->r_offset + 2, 2))
4360 goto error_return;
4361
4362 /* That will change things, so, we should relax again.
4363 Note that this is not required, and it may be slow. */
4364 *again = TRUE;
4365 break;
4366 }
4367 }
4368 }
4369 }
4370
4371 if (isymbuf != NULL
4372 && symtab_hdr->contents != (unsigned char *) isymbuf)
4373 {
4374 if (! link_info->keep_memory)
4375 free (isymbuf);
4376 else
4377 {
4378 /* Cache the symbols for elf_link_input_bfd. */
4379 symtab_hdr->contents = (unsigned char *) isymbuf;
4380 }
4381 }
4382
4383 if (contents != NULL
4384 && elf_section_data (sec)->this_hdr.contents != contents)
4385 {
4386 if (! link_info->keep_memory)
4387 free (contents);
4388 else
4389 {
4390 /* Cache the section contents for elf_link_input_bfd. */
4391 elf_section_data (sec)->this_hdr.contents = contents;
4392 }
4393 }
4394
4395 if (internal_relocs != NULL
4396 && elf_section_data (sec)->relocs != internal_relocs)
4397 free (internal_relocs);
4398
4399 return TRUE;
4400
4401 error_return:
4402 if (isymbuf != NULL
4403 && symtab_hdr->contents != (unsigned char *) isymbuf)
4404 free (isymbuf);
4405 if (contents != NULL
4406 && elf_section_data (section)->this_hdr.contents != contents)
4407 free (contents);
4408 if (internal_relocs != NULL
4409 && elf_section_data (section)->relocs != internal_relocs)
4410 free (internal_relocs);
4411
4412 return FALSE;
4413 }
4414
4415 /* This is a version of bfd_generic_get_relocated_section_contents
4416 which uses mn10300_elf_relocate_section. */
4417
4418 static bfd_byte *
4419 mn10300_elf_get_relocated_section_contents (bfd *output_bfd,
4420 struct bfd_link_info *link_info,
4421 struct bfd_link_order *link_order,
4422 bfd_byte *data,
4423 bfd_boolean relocatable,
4424 asymbol **symbols)
4425 {
4426 Elf_Internal_Shdr *symtab_hdr;
4427 asection *input_section = link_order->u.indirect.section;
4428 bfd *input_bfd = input_section->owner;
4429 asection **sections = NULL;
4430 Elf_Internal_Rela *internal_relocs = NULL;
4431 Elf_Internal_Sym *isymbuf = NULL;
4432
4433 /* We only need to handle the case of relaxing, or of having a
4434 particular set of section contents, specially. */
4435 if (relocatable
4436 || elf_section_data (input_section)->this_hdr.contents == NULL)
4437 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
4438 link_order, data,
4439 relocatable,
4440 symbols);
4441
4442 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
4443
4444 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
4445 (size_t) input_section->size);
4446
4447 if ((input_section->flags & SEC_RELOC) != 0
4448 && input_section->reloc_count > 0)
4449 {
4450 asection **secpp;
4451 Elf_Internal_Sym *isym, *isymend;
4452 bfd_size_type amt;
4453
4454 internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
4455 NULL, NULL, FALSE);
4456 if (internal_relocs == NULL)
4457 goto error_return;
4458
4459 if (symtab_hdr->sh_info != 0)
4460 {
4461 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
4462 if (isymbuf == NULL)
4463 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
4464 symtab_hdr->sh_info, 0,
4465 NULL, NULL, NULL);
4466 if (isymbuf == NULL)
4467 goto error_return;
4468 }
4469
4470 amt = symtab_hdr->sh_info;
4471 amt *= sizeof (asection *);
4472 sections = bfd_malloc (amt);
4473 if (sections == NULL && amt != 0)
4474 goto error_return;
4475
4476 isymend = isymbuf + symtab_hdr->sh_info;
4477 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
4478 {
4479 asection *isec;
4480
4481 if (isym->st_shndx == SHN_UNDEF)
4482 isec = bfd_und_section_ptr;
4483 else if (isym->st_shndx == SHN_ABS)
4484 isec = bfd_abs_section_ptr;
4485 else if (isym->st_shndx == SHN_COMMON)
4486 isec = bfd_com_section_ptr;
4487 else
4488 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
4489
4490 *secpp = isec;
4491 }
4492
4493 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
4494 input_section, data, internal_relocs,
4495 isymbuf, sections))
4496 goto error_return;
4497
4498 if (sections != NULL)
4499 free (sections);
4500 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4501 free (isymbuf);
4502 if (internal_relocs != elf_section_data (input_section)->relocs)
4503 free (internal_relocs);
4504 }
4505
4506 return data;
4507
4508 error_return:
4509 if (sections != NULL)
4510 free (sections);
4511 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
4512 free (isymbuf);
4513 if (internal_relocs != NULL
4514 && internal_relocs != elf_section_data (input_section)->relocs)
4515 free (internal_relocs);
4516 return NULL;
4517 }
4518
4519 /* Assorted hash table functions. */
4520
4521 /* Initialize an entry in the link hash table. */
4522
4523 /* Create an entry in an MN10300 ELF linker hash table. */
4524
4525 static struct bfd_hash_entry *
4526 elf32_mn10300_link_hash_newfunc (struct bfd_hash_entry *entry,
4527 struct bfd_hash_table *table,
4528 const char *string)
4529 {
4530 struct elf32_mn10300_link_hash_entry *ret =
4531 (struct elf32_mn10300_link_hash_entry *) entry;
4532
4533 /* Allocate the structure if it has not already been allocated by a
4534 subclass. */
4535 if (ret == NULL)
4536 ret = (struct elf32_mn10300_link_hash_entry *)
4537 bfd_hash_allocate (table, sizeof (* ret));
4538 if (ret == NULL)
4539 return (struct bfd_hash_entry *) ret;
4540
4541 /* Call the allocation method of the superclass. */
4542 ret = (struct elf32_mn10300_link_hash_entry *)
4543 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
4544 table, string);
4545 if (ret != NULL)
4546 {
4547 ret->direct_calls = 0;
4548 ret->stack_size = 0;
4549 ret->movm_args = 0;
4550 ret->movm_stack_size = 0;
4551 ret->flags = 0;
4552 ret->value = 0;
4553 ret->tls_type = GOT_UNKNOWN;
4554 }
4555
4556 return (struct bfd_hash_entry *) ret;
4557 }
4558
4559 static void
4560 _bfd_mn10300_copy_indirect_symbol (struct bfd_link_info * info,
4561 struct elf_link_hash_entry * dir,
4562 struct elf_link_hash_entry * ind)
4563 {
4564 struct elf32_mn10300_link_hash_entry * edir;
4565 struct elf32_mn10300_link_hash_entry * eind;
4566
4567 edir = elf_mn10300_hash_entry (dir);
4568 eind = elf_mn10300_hash_entry (ind);
4569
4570 if (ind->root.type == bfd_link_hash_indirect
4571 && dir->got.refcount <= 0)
4572 {
4573 edir->tls_type = eind->tls_type;
4574 eind->tls_type = GOT_UNKNOWN;
4575 }
4576 edir->direct_calls = eind->direct_calls;
4577 edir->stack_size = eind->stack_size;
4578 edir->movm_args = eind->movm_args;
4579 edir->movm_stack_size = eind->movm_stack_size;
4580 edir->flags = eind->flags;
4581
4582 _bfd_elf_link_hash_copy_indirect (info, dir, ind);
4583 }
4584
4585 /* Destroy an mn10300 ELF linker hash table. */
4586
4587 static void
4588 elf32_mn10300_link_hash_table_free (bfd *obfd)
4589 {
4590 struct elf32_mn10300_link_hash_table *ret
4591 = (struct elf32_mn10300_link_hash_table *) obfd->link.hash;
4592
4593 obfd->link.hash = &ret->static_hash_table->root.root;
4594 _bfd_elf_link_hash_table_free (obfd);
4595 obfd->is_linker_output = TRUE;
4596 obfd->link.hash = &ret->root.root;
4597 _bfd_elf_link_hash_table_free (obfd);
4598 }
4599
4600 /* Create an mn10300 ELF linker hash table. */
4601
4602 static struct bfd_link_hash_table *
4603 elf32_mn10300_link_hash_table_create (bfd *abfd)
4604 {
4605 struct elf32_mn10300_link_hash_table *ret;
4606 bfd_size_type amt = sizeof (* ret);
4607
4608 ret = bfd_zmalloc (amt);
4609 if (ret == NULL)
4610 return NULL;
4611
4612 amt = sizeof (struct elf_link_hash_table);
4613 ret->static_hash_table = bfd_zmalloc (amt);
4614 if (ret->static_hash_table == NULL)
4615 {
4616 free (ret);
4617 return NULL;
4618 }
4619
4620 if (!_bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
4621 elf32_mn10300_link_hash_newfunc,
4622 sizeof (struct elf32_mn10300_link_hash_entry),
4623 MN10300_ELF_DATA))
4624 {
4625 free (ret->static_hash_table);
4626 free (ret);
4627 return NULL;
4628 }
4629
4630 abfd->is_linker_output = FALSE;
4631 abfd->link.hash = NULL;
4632 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
4633 elf32_mn10300_link_hash_newfunc,
4634 sizeof (struct elf32_mn10300_link_hash_entry),
4635 MN10300_ELF_DATA))
4636 {
4637 abfd->is_linker_output = TRUE;
4638 abfd->link.hash = &ret->static_hash_table->root.root;
4639 _bfd_elf_link_hash_table_free (abfd);
4640 free (ret);
4641 return NULL;
4642 }
4643 ret->root.root.hash_table_free = elf32_mn10300_link_hash_table_free;
4644
4645 ret->tls_ldm_got.offset = -1;
4646
4647 return & ret->root.root;
4648 }
4649
4650 static unsigned long
4651 elf_mn10300_mach (flagword flags)
4652 {
4653 switch (flags & EF_MN10300_MACH)
4654 {
4655 case E_MN10300_MACH_MN10300:
4656 default:
4657 return bfd_mach_mn10300;
4658
4659 case E_MN10300_MACH_AM33:
4660 return bfd_mach_am33;
4661
4662 case E_MN10300_MACH_AM33_2:
4663 return bfd_mach_am33_2;
4664 }
4665 }
4666
4667 /* The final processing done just before writing out a MN10300 ELF object
4668 file. This gets the MN10300 architecture right based on the machine
4669 number. */
4670
4671 static void
4672 _bfd_mn10300_elf_final_write_processing (bfd *abfd,
4673 bfd_boolean linker ATTRIBUTE_UNUSED)
4674 {
4675 unsigned long val;
4676
4677 switch (bfd_get_mach (abfd))
4678 {
4679 default:
4680 case bfd_mach_mn10300:
4681 val = E_MN10300_MACH_MN10300;
4682 break;
4683
4684 case bfd_mach_am33:
4685 val = E_MN10300_MACH_AM33;
4686 break;
4687
4688 case bfd_mach_am33_2:
4689 val = E_MN10300_MACH_AM33_2;
4690 break;
4691 }
4692
4693 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
4694 elf_elfheader (abfd)->e_flags |= val;
4695 }
4696
4697 static bfd_boolean
4698 _bfd_mn10300_elf_object_p (bfd *abfd)
4699 {
4700 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
4701 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
4702 return TRUE;
4703 }
4704
4705 /* Merge backend specific data from an object file to the output
4706 object file when linking. */
4707
4708 static bfd_boolean
4709 _bfd_mn10300_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4710 {
4711 bfd *obfd = info->output_bfd;
4712
4713 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
4714 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
4715 return TRUE;
4716
4717 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
4718 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
4719 {
4720 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
4721 bfd_get_mach (ibfd)))
4722 return FALSE;
4723 }
4724
4725 return TRUE;
4726 }
4727
4728 #define PLT0_ENTRY_SIZE 15
4729 #define PLT_ENTRY_SIZE 20
4730 #define PIC_PLT_ENTRY_SIZE 24
4731
4732 static const bfd_byte elf_mn10300_plt0_entry[PLT0_ENTRY_SIZE] =
4733 {
4734 0xfc, 0xa0, 0, 0, 0, 0, /* mov (.got+8),a0 */
4735 0xfe, 0xe, 0x10, 0, 0, 0, 0, /* mov (.got+4),r1 */
4736 0xf0, 0xf4, /* jmp (a0) */
4737 };
4738
4739 static const bfd_byte elf_mn10300_plt_entry[PLT_ENTRY_SIZE] =
4740 {
4741 0xfc, 0xa0, 0, 0, 0, 0, /* mov (nameN@GOT + .got),a0 */
4742 0xf0, 0xf4, /* jmp (a0) */
4743 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4744 0xdc, 0, 0, 0, 0, /* jmp .plt0 */
4745 };
4746
4747 static const bfd_byte elf_mn10300_pic_plt_entry[PIC_PLT_ENTRY_SIZE] =
4748 {
4749 0xfc, 0x22, 0, 0, 0, 0, /* mov (nameN@GOT,a2),a0 */
4750 0xf0, 0xf4, /* jmp (a0) */
4751 0xfe, 8, 0, 0, 0, 0, 0, /* mov reloc-table-address,r0 */
4752 0xf8, 0x22, 8, /* mov (8,a2),a0 */
4753 0xfb, 0xa, 0x1a, 4, /* mov (4,a2),r1 */
4754 0xf0, 0xf4, /* jmp (a0) */
4755 };
4756
4757 /* Return size of the first PLT entry. */
4758 #define elf_mn10300_sizeof_plt0(info) \
4759 (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT0_ENTRY_SIZE)
4760
4761 /* Return size of a PLT entry. */
4762 #define elf_mn10300_sizeof_plt(info) \
4763 (bfd_link_pic (info) ? PIC_PLT_ENTRY_SIZE : PLT_ENTRY_SIZE)
4764
4765 /* Return offset of the PLT0 address in an absolute PLT entry. */
4766 #define elf_mn10300_plt_plt0_offset(info) 16
4767
4768 /* Return offset of the linker in PLT0 entry. */
4769 #define elf_mn10300_plt0_linker_offset(info) 2
4770
4771 /* Return offset of the GOT id in PLT0 entry. */
4772 #define elf_mn10300_plt0_gotid_offset(info) 9
4773
4774 /* Return offset of the temporary in PLT entry. */
4775 #define elf_mn10300_plt_temp_offset(info) 8
4776
4777 /* Return offset of the symbol in PLT entry. */
4778 #define elf_mn10300_plt_symbol_offset(info) 2
4779
4780 /* Return offset of the relocation in PLT entry. */
4781 #define elf_mn10300_plt_reloc_offset(info) 11
4782
4783 /* The name of the dynamic interpreter. This is put in the .interp
4784 section. */
4785
4786 #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
4787
4788 /* Create dynamic sections when linking against a dynamic object. */
4789
4790 static bfd_boolean
4791 _bfd_mn10300_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4792 {
4793 flagword flags;
4794 asection * s;
4795 const struct elf_backend_data * bed = get_elf_backend_data (abfd);
4796 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
4797 int ptralign = 0;
4798
4799 switch (bed->s->arch_size)
4800 {
4801 case 32:
4802 ptralign = 2;
4803 break;
4804
4805 case 64:
4806 ptralign = 3;
4807 break;
4808
4809 default:
4810 bfd_set_error (bfd_error_bad_value);
4811 return FALSE;
4812 }
4813
4814 /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4815 .rel[a].bss sections. */
4816 flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4817 | SEC_LINKER_CREATED);
4818
4819 s = bfd_make_section_anyway_with_flags (abfd,
4820 (bed->default_use_rela_p
4821 ? ".rela.plt" : ".rel.plt"),
4822 flags | SEC_READONLY);
4823 htab->root.srelplt = s;
4824 if (s == NULL
4825 || ! bfd_set_section_alignment (abfd, s, ptralign))
4826 return FALSE;
4827
4828 if (! _bfd_mn10300_elf_create_got_section (abfd, info))
4829 return FALSE;
4830
4831 if (bed->want_dynbss)
4832 {
4833 /* The .dynbss section is a place to put symbols which are defined
4834 by dynamic objects, are referenced by regular objects, and are
4835 not functions. We must allocate space for them in the process
4836 image and use a R_*_COPY reloc to tell the dynamic linker to
4837 initialize them at run time. The linker script puts the .dynbss
4838 section into the .bss section of the final image. */
4839 s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
4840 SEC_ALLOC | SEC_LINKER_CREATED);
4841 if (s == NULL)
4842 return FALSE;
4843
4844 /* The .rel[a].bss section holds copy relocs. This section is not
4845 normally needed. We need to create it here, though, so that the
4846 linker will map it to an output section. We can't just create it
4847 only if we need it, because we will not know whether we need it
4848 until we have seen all the input files, and the first time the
4849 main linker code calls BFD after examining all the input files
4850 (size_dynamic_sections) the input sections have already been
4851 mapped to the output sections. If the section turns out not to
4852 be needed, we can discard it later. We will never need this
4853 section when generating a shared object, since they do not use
4854 copy relocs. */
4855 if (! bfd_link_pic (info))
4856 {
4857 s = bfd_make_section_anyway_with_flags (abfd,
4858 (bed->default_use_rela_p
4859 ? ".rela.bss" : ".rel.bss"),
4860 flags | SEC_READONLY);
4861 if (s == NULL
4862 || ! bfd_set_section_alignment (abfd, s, ptralign))
4863 return FALSE;
4864 }
4865 }
4866
4867 return TRUE;
4868 }
4869 \f
4870 /* Adjust a symbol defined by a dynamic object and referenced by a
4871 regular object. The current definition is in some section of the
4872 dynamic object, but we're not including those sections. We have to
4873 change the definition to something the rest of the link can
4874 understand. */
4875
4876 static bfd_boolean
4877 _bfd_mn10300_elf_adjust_dynamic_symbol (struct bfd_link_info * info,
4878 struct elf_link_hash_entry * h)
4879 {
4880 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
4881 bfd * dynobj;
4882 asection * s;
4883
4884 dynobj = htab->root.dynobj;
4885
4886 /* Make sure we know what is going on here. */
4887 BFD_ASSERT (dynobj != NULL
4888 && (h->needs_plt
4889 || h->is_weakalias
4890 || (h->def_dynamic
4891 && h->ref_regular
4892 && !h->def_regular)));
4893
4894 /* If this is a function, put it in the procedure linkage table. We
4895 will fill in the contents of the procedure linkage table later,
4896 when we know the address of the .got section. */
4897 if (h->type == STT_FUNC
4898 || h->needs_plt)
4899 {
4900 if (! bfd_link_pic (info)
4901 && !h->def_dynamic
4902 && !h->ref_dynamic)
4903 {
4904 /* This case can occur if we saw a PLT reloc in an input
4905 file, but the symbol was never referred to by a dynamic
4906 object. In such a case, we don't actually need to build
4907 a procedure linkage table, and we can just do a REL32
4908 reloc instead. */
4909 BFD_ASSERT (h->needs_plt);
4910 return TRUE;
4911 }
4912
4913 /* Make sure this symbol is output as a dynamic symbol. */
4914 if (h->dynindx == -1)
4915 {
4916 if (! bfd_elf_link_record_dynamic_symbol (info, h))
4917 return FALSE;
4918 }
4919
4920 s = htab->root.splt;
4921 BFD_ASSERT (s != NULL);
4922
4923 /* If this is the first .plt entry, make room for the special
4924 first entry. */
4925 if (s->size == 0)
4926 s->size += elf_mn10300_sizeof_plt0 (info);
4927
4928 /* If this symbol is not defined in a regular file, and we are
4929 not generating a shared library, then set the symbol to this
4930 location in the .plt. This is required to make function
4931 pointers compare as equal between the normal executable and
4932 the shared library. */
4933 if (! bfd_link_pic (info)
4934 && !h->def_regular)
4935 {
4936 h->root.u.def.section = s;
4937 h->root.u.def.value = s->size;
4938 }
4939
4940 h->plt.offset = s->size;
4941
4942 /* Make room for this entry. */
4943 s->size += elf_mn10300_sizeof_plt (info);
4944
4945 /* We also need to make an entry in the .got.plt section, which
4946 will be placed in the .got section by the linker script. */
4947 s = htab->root.sgotplt;
4948 BFD_ASSERT (s != NULL);
4949 s->size += 4;
4950
4951 /* We also need to make an entry in the .rela.plt section. */
4952 s = htab->root.srelplt;
4953 BFD_ASSERT (s != NULL);
4954 s->size += sizeof (Elf32_External_Rela);
4955
4956 return TRUE;
4957 }
4958
4959 /* If this is a weak symbol, and there is a real definition, the
4960 processor independent code will have arranged for us to see the
4961 real definition first, and we can just use the same value. */
4962 if (h->is_weakalias)
4963 {
4964 struct elf_link_hash_entry *def = weakdef (h);
4965 BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4966 h->root.u.def.section = def->root.u.def.section;
4967 h->root.u.def.value = def->root.u.def.value;
4968 return TRUE;
4969 }
4970
4971 /* This is a reference to a symbol defined by a dynamic object which
4972 is not a function. */
4973
4974 /* If we are creating a shared library, we must presume that the
4975 only references to the symbol are via the global offset table.
4976 For such cases we need not do anything here; the relocations will
4977 be handled correctly by relocate_section. */
4978 if (bfd_link_pic (info))
4979 return TRUE;
4980
4981 /* If there are no references to this symbol that do not use the
4982 GOT, we don't need to generate a copy reloc. */
4983 if (!h->non_got_ref)
4984 return TRUE;
4985
4986 /* We must allocate the symbol in our .dynbss section, which will
4987 become part of the .bss section of the executable. There will be
4988 an entry for this symbol in the .dynsym section. The dynamic
4989 object will contain position independent code, so all references
4990 from the dynamic object to this symbol will go through the global
4991 offset table. The dynamic linker will use the .dynsym entry to
4992 determine the address it must put in the global offset table, so
4993 both the dynamic object and the regular object will refer to the
4994 same memory location for the variable. */
4995
4996 s = bfd_get_linker_section (dynobj, ".dynbss");
4997 BFD_ASSERT (s != NULL);
4998
4999 /* We must generate a R_MN10300_COPY reloc to tell the dynamic linker to
5000 copy the initial value out of the dynamic object and into the
5001 runtime process image. We need to remember the offset into the
5002 .rela.bss section we are going to use. */
5003 if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
5004 {
5005 asection * srel;
5006
5007 srel = bfd_get_linker_section (dynobj, ".rela.bss");
5008 BFD_ASSERT (srel != NULL);
5009 srel->size += sizeof (Elf32_External_Rela);
5010 h->needs_copy = 1;
5011 }
5012
5013 return _bfd_elf_adjust_dynamic_copy (info, h, s);
5014 }
5015
5016 /* Set the sizes of the dynamic sections. */
5017
5018 static bfd_boolean
5019 _bfd_mn10300_elf_size_dynamic_sections (bfd * output_bfd,
5020 struct bfd_link_info * info)
5021 {
5022 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
5023 bfd * dynobj;
5024 asection * s;
5025 bfd_boolean plt;
5026 bfd_boolean relocs;
5027 bfd_boolean reltext;
5028
5029 dynobj = htab->root.dynobj;
5030 BFD_ASSERT (dynobj != NULL);
5031
5032 if (elf_hash_table (info)->dynamic_sections_created)
5033 {
5034 /* Set the contents of the .interp section to the interpreter. */
5035 if (bfd_link_executable (info) && !info->nointerp)
5036 {
5037 s = bfd_get_linker_section (dynobj, ".interp");
5038 BFD_ASSERT (s != NULL);
5039 s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5040 s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5041 }
5042 }
5043 else
5044 {
5045 /* We may have created entries in the .rela.got section.
5046 However, if we are not creating the dynamic sections, we will
5047 not actually use these entries. Reset the size of .rela.got,
5048 which will cause it to get stripped from the output file
5049 below. */
5050 s = htab->root.sgot;
5051 if (s != NULL)
5052 s->size = 0;
5053 }
5054
5055 if (htab->tls_ldm_got.refcount > 0)
5056 {
5057 s = htab->root.srelgot;
5058 BFD_ASSERT (s != NULL);
5059 s->size += sizeof (Elf32_External_Rela);
5060 }
5061
5062 /* The check_relocs and adjust_dynamic_symbol entry points have
5063 determined the sizes of the various dynamic sections. Allocate
5064 memory for them. */
5065 plt = FALSE;
5066 relocs = FALSE;
5067 reltext = FALSE;
5068 for (s = dynobj->sections; s != NULL; s = s->next)
5069 {
5070 const char * name;
5071
5072 if ((s->flags & SEC_LINKER_CREATED) == 0)
5073 continue;
5074
5075 /* It's OK to base decisions on the section name, because none
5076 of the dynobj section names depend upon the input files. */
5077 name = bfd_get_section_name (dynobj, s);
5078
5079 if (streq (name, ".plt"))
5080 {
5081 /* Remember whether there is a PLT. */
5082 plt = s->size != 0;
5083 }
5084 else if (CONST_STRNEQ (name, ".rela"))
5085 {
5086 if (s->size != 0)
5087 {
5088 asection * target;
5089
5090 /* Remember whether there are any reloc sections other
5091 than .rela.plt. */
5092 if (! streq (name, ".rela.plt"))
5093 {
5094 const char * outname;
5095
5096 relocs = TRUE;
5097
5098 /* If this relocation section applies to a read only
5099 section, then we probably need a DT_TEXTREL
5100 entry. The entries in the .rela.plt section
5101 really apply to the .got section, which we
5102 created ourselves and so know is not readonly. */
5103 outname = bfd_get_section_name (output_bfd,
5104 s->output_section);
5105 target = bfd_get_section_by_name (output_bfd, outname + 5);
5106 if (target != NULL
5107 && (target->flags & SEC_READONLY) != 0
5108 && (target->flags & SEC_ALLOC) != 0)
5109 reltext = TRUE;
5110 }
5111
5112 /* We use the reloc_count field as a counter if we need
5113 to copy relocs into the output file. */
5114 s->reloc_count = 0;
5115 }
5116 }
5117 else if (! CONST_STRNEQ (name, ".got")
5118 && ! streq (name, ".dynbss"))
5119 /* It's not one of our sections, so don't allocate space. */
5120 continue;
5121
5122 if (s->size == 0)
5123 {
5124 /* If we don't need this section, strip it from the
5125 output file. This is mostly to handle .rela.bss and
5126 .rela.plt. We must create both sections in
5127 create_dynamic_sections, because they must be created
5128 before the linker maps input sections to output
5129 sections. The linker does that before
5130 adjust_dynamic_symbol is called, and it is that
5131 function which decides whether anything needs to go
5132 into these sections. */
5133 s->flags |= SEC_EXCLUDE;
5134 continue;
5135 }
5136
5137 if ((s->flags & SEC_HAS_CONTENTS) == 0)
5138 continue;
5139
5140 /* Allocate memory for the section contents. We use bfd_zalloc
5141 here in case unused entries are not reclaimed before the
5142 section's contents are written out. This should not happen,
5143 but this way if it does, we get a R_MN10300_NONE reloc
5144 instead of garbage. */
5145 s->contents = bfd_zalloc (dynobj, s->size);
5146 if (s->contents == NULL)
5147 return FALSE;
5148 }
5149
5150 if (elf_hash_table (info)->dynamic_sections_created)
5151 {
5152 /* Add some entries to the .dynamic section. We fill in the
5153 values later, in _bfd_mn10300_elf_finish_dynamic_sections,
5154 but we must add the entries now so that we get the correct
5155 size for the .dynamic section. The DT_DEBUG entry is filled
5156 in by the dynamic linker and used by the debugger. */
5157 if (! bfd_link_pic (info))
5158 {
5159 if (!_bfd_elf_add_dynamic_entry (info, DT_DEBUG, 0))
5160 return FALSE;
5161 }
5162
5163 if (plt)
5164 {
5165 if (!_bfd_elf_add_dynamic_entry (info, DT_PLTGOT, 0)
5166 || !_bfd_elf_add_dynamic_entry (info, DT_PLTRELSZ, 0)
5167 || !_bfd_elf_add_dynamic_entry (info, DT_PLTREL, DT_RELA)
5168 || !_bfd_elf_add_dynamic_entry (info, DT_JMPREL, 0))
5169 return FALSE;
5170 }
5171
5172 if (relocs)
5173 {
5174 if (!_bfd_elf_add_dynamic_entry (info, DT_RELA, 0)
5175 || !_bfd_elf_add_dynamic_entry (info, DT_RELASZ, 0)
5176 || !_bfd_elf_add_dynamic_entry (info, DT_RELAENT,
5177 sizeof (Elf32_External_Rela)))
5178 return FALSE;
5179 }
5180
5181 if (reltext)
5182 {
5183 if (!_bfd_elf_add_dynamic_entry (info, DT_TEXTREL, 0))
5184 return FALSE;
5185 }
5186 }
5187
5188 return TRUE;
5189 }
5190
5191 /* Finish up dynamic symbol handling. We set the contents of various
5192 dynamic sections here. */
5193
5194 static bfd_boolean
5195 _bfd_mn10300_elf_finish_dynamic_symbol (bfd * output_bfd,
5196 struct bfd_link_info * info,
5197 struct elf_link_hash_entry * h,
5198 Elf_Internal_Sym * sym)
5199 {
5200 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
5201 bfd * dynobj;
5202
5203 dynobj = htab->root.dynobj;
5204
5205 if (h->plt.offset != (bfd_vma) -1)
5206 {
5207 asection * splt;
5208 asection * sgot;
5209 asection * srel;
5210 bfd_vma plt_index;
5211 bfd_vma got_offset;
5212 Elf_Internal_Rela rel;
5213
5214 /* This symbol has an entry in the procedure linkage table. Set
5215 it up. */
5216
5217 BFD_ASSERT (h->dynindx != -1);
5218
5219 splt = htab->root.splt;
5220 sgot = htab->root.sgotplt;
5221 srel = htab->root.srelplt;
5222 BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL);
5223
5224 /* Get the index in the procedure linkage table which
5225 corresponds to this symbol. This is the index of this symbol
5226 in all the symbols for which we are making plt entries. The
5227 first entry in the procedure linkage table is reserved. */
5228 plt_index = ((h->plt.offset - elf_mn10300_sizeof_plt0 (info))
5229 / elf_mn10300_sizeof_plt (info));
5230
5231 /* Get the offset into the .got table of the entry that
5232 corresponds to this function. Each .got entry is 4 bytes.
5233 The first three are reserved. */
5234 got_offset = (plt_index + 3) * 4;
5235
5236 /* Fill in the entry in the procedure linkage table. */
5237 if (! bfd_link_pic (info))
5238 {
5239 memcpy (splt->contents + h->plt.offset, elf_mn10300_plt_entry,
5240 elf_mn10300_sizeof_plt (info));
5241 bfd_put_32 (output_bfd,
5242 (sgot->output_section->vma
5243 + sgot->output_offset
5244 + got_offset),
5245 (splt->contents + h->plt.offset
5246 + elf_mn10300_plt_symbol_offset (info)));
5247
5248 bfd_put_32 (output_bfd,
5249 (1 - h->plt.offset - elf_mn10300_plt_plt0_offset (info)),
5250 (splt->contents + h->plt.offset
5251 + elf_mn10300_plt_plt0_offset (info)));
5252 }
5253 else
5254 {
5255 memcpy (splt->contents + h->plt.offset, elf_mn10300_pic_plt_entry,
5256 elf_mn10300_sizeof_plt (info));
5257
5258 bfd_put_32 (output_bfd, got_offset,
5259 (splt->contents + h->plt.offset
5260 + elf_mn10300_plt_symbol_offset (info)));
5261 }
5262
5263 bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rela),
5264 (splt->contents + h->plt.offset
5265 + elf_mn10300_plt_reloc_offset (info)));
5266
5267 /* Fill in the entry in the global offset table. */
5268 bfd_put_32 (output_bfd,
5269 (splt->output_section->vma
5270 + splt->output_offset
5271 + h->plt.offset
5272 + elf_mn10300_plt_temp_offset (info)),
5273 sgot->contents + got_offset);
5274
5275 /* Fill in the entry in the .rela.plt section. */
5276 rel.r_offset = (sgot->output_section->vma
5277 + sgot->output_offset
5278 + got_offset);
5279 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_JMP_SLOT);
5280 rel.r_addend = 0;
5281 bfd_elf32_swap_reloca_out (output_bfd, &rel,
5282 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5283 + plt_index));
5284
5285 if (!h->def_regular)
5286 /* Mark the symbol as undefined, rather than as defined in
5287 the .plt section. Leave the value alone. */
5288 sym->st_shndx = SHN_UNDEF;
5289 }
5290
5291 if (h->got.offset != (bfd_vma) -1)
5292 {
5293 asection * sgot;
5294 asection * srel;
5295 Elf_Internal_Rela rel;
5296
5297 /* This symbol has an entry in the global offset table. Set it up. */
5298 sgot = htab->root.sgot;
5299 srel = htab->root.srelgot;
5300 BFD_ASSERT (sgot != NULL && srel != NULL);
5301
5302 rel.r_offset = (sgot->output_section->vma
5303 + sgot->output_offset
5304 + (h->got.offset & ~1));
5305
5306 switch (elf_mn10300_hash_entry (h)->tls_type)
5307 {
5308 case GOT_TLS_GD:
5309 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5310 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset + 4);
5311 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPMOD);
5312 rel.r_addend = 0;
5313 bfd_elf32_swap_reloca_out (output_bfd, & rel,
5314 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5315 + srel->reloc_count));
5316 ++ srel->reloc_count;
5317 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_DTPOFF);
5318 rel.r_offset += 4;
5319 rel.r_addend = 0;
5320 break;
5321
5322 case GOT_TLS_IE:
5323 /* We originally stored the addend in the GOT, but at this
5324 point, we want to move it to the reloc instead as that's
5325 where the dynamic linker wants it. */
5326 rel.r_addend = bfd_get_32 (output_bfd, sgot->contents + h->got.offset);
5327 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5328 if (h->dynindx == -1)
5329 rel.r_info = ELF32_R_INFO (0, R_MN10300_TLS_TPOFF);
5330 else
5331 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_TLS_TPOFF);
5332 break;
5333
5334 default:
5335 /* If this is a -Bsymbolic link, and the symbol is defined
5336 locally, we just want to emit a RELATIVE reloc. Likewise if
5337 the symbol was forced to be local because of a version file.
5338 The entry in the global offset table will already have been
5339 initialized in the relocate_section function. */
5340 if (bfd_link_pic (info)
5341 && (info->symbolic || h->dynindx == -1)
5342 && h->def_regular)
5343 {
5344 rel.r_info = ELF32_R_INFO (0, R_MN10300_RELATIVE);
5345 rel.r_addend = (h->root.u.def.value
5346 + h->root.u.def.section->output_section->vma
5347 + h->root.u.def.section->output_offset);
5348 }
5349 else
5350 {
5351 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
5352 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_GLOB_DAT);
5353 rel.r_addend = 0;
5354 }
5355 }
5356
5357 if (ELF32_R_TYPE (rel.r_info) != R_MN10300_NONE)
5358 {
5359 bfd_elf32_swap_reloca_out (output_bfd, &rel,
5360 (bfd_byte *) ((Elf32_External_Rela *) srel->contents
5361 + srel->reloc_count));
5362 ++ srel->reloc_count;
5363 }
5364 }
5365
5366 if (h->needs_copy)
5367 {
5368 asection * s;
5369 Elf_Internal_Rela rel;
5370
5371 /* This symbol needs a copy reloc. Set it up. */
5372 BFD_ASSERT (h->dynindx != -1
5373 && (h->root.type == bfd_link_hash_defined
5374 || h->root.type == bfd_link_hash_defweak));
5375
5376 s = bfd_get_linker_section (dynobj, ".rela.bss");
5377 BFD_ASSERT (s != NULL);
5378
5379 rel.r_offset = (h->root.u.def.value
5380 + h->root.u.def.section->output_section->vma
5381 + h->root.u.def.section->output_offset);
5382 rel.r_info = ELF32_R_INFO (h->dynindx, R_MN10300_COPY);
5383 rel.r_addend = 0;
5384 bfd_elf32_swap_reloca_out (output_bfd, & rel,
5385 (bfd_byte *) ((Elf32_External_Rela *) s->contents
5386 + s->reloc_count));
5387 ++ s->reloc_count;
5388 }
5389
5390 /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */
5391 if (h == elf_hash_table (info)->hdynamic
5392 || h == elf_hash_table (info)->hgot)
5393 sym->st_shndx = SHN_ABS;
5394
5395 return TRUE;
5396 }
5397
5398 /* Finish up the dynamic sections. */
5399
5400 static bfd_boolean
5401 _bfd_mn10300_elf_finish_dynamic_sections (bfd * output_bfd,
5402 struct bfd_link_info * info)
5403 {
5404 bfd * dynobj;
5405 asection * sgot;
5406 asection * sdyn;
5407 struct elf32_mn10300_link_hash_table *htab = elf32_mn10300_hash_table (info);
5408
5409 dynobj = htab->root.dynobj;
5410 sgot = htab->root.sgotplt;
5411 BFD_ASSERT (sgot != NULL);
5412 sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5413
5414 if (elf_hash_table (info)->dynamic_sections_created)
5415 {
5416 asection * splt;
5417 Elf32_External_Dyn * dyncon;
5418 Elf32_External_Dyn * dynconend;
5419
5420 BFD_ASSERT (sdyn != NULL);
5421
5422 dyncon = (Elf32_External_Dyn *) sdyn->contents;
5423 dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5424
5425 for (; dyncon < dynconend; dyncon++)
5426 {
5427 Elf_Internal_Dyn dyn;
5428 asection * s;
5429
5430 bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5431
5432 switch (dyn.d_tag)
5433 {
5434 default:
5435 break;
5436
5437 case DT_PLTGOT:
5438 s = htab->root.sgot;
5439 goto get_vma;
5440
5441 case DT_JMPREL:
5442 s = htab->root.srelplt;
5443 get_vma:
5444 dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
5445 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5446 break;
5447
5448 case DT_PLTRELSZ:
5449 s = htab->root.srelplt;
5450 dyn.d_un.d_val = s->size;
5451 bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5452 break;
5453 }
5454 }
5455
5456 /* Fill in the first entry in the procedure linkage table. */
5457 splt = htab->root.splt;
5458 if (splt && splt->size > 0)
5459 {
5460 if (bfd_link_pic (info))
5461 {
5462 memcpy (splt->contents, elf_mn10300_pic_plt_entry,
5463 elf_mn10300_sizeof_plt (info));
5464 }
5465 else
5466 {
5467 memcpy (splt->contents, elf_mn10300_plt0_entry, PLT0_ENTRY_SIZE);
5468 bfd_put_32 (output_bfd,
5469 sgot->output_section->vma + sgot->output_offset + 4,
5470 splt->contents + elf_mn10300_plt0_gotid_offset (info));
5471 bfd_put_32 (output_bfd,
5472 sgot->output_section->vma + sgot->output_offset + 8,
5473 splt->contents + elf_mn10300_plt0_linker_offset (info));
5474 }
5475
5476 /* UnixWare sets the entsize of .plt to 4, although that doesn't
5477 really seem like the right value. */
5478 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
5479
5480 /* UnixWare sets the entsize of .plt to 4, but this is incorrect
5481 as it means that the size of the PLT0 section (15 bytes) is not
5482 a multiple of the sh_entsize. Some ELF tools flag this as an
5483 error. We could pad PLT0 to 16 bytes, but that would introduce
5484 compatibilty issues with previous toolchains, so instead we
5485 just set the entry size to 1. */
5486 elf_section_data (splt->output_section)->this_hdr.sh_entsize = 1;
5487 }
5488 }
5489
5490 /* Fill in the first three entries in the global offset table. */
5491 if (sgot->size > 0)
5492 {
5493 if (sdyn == NULL)
5494 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
5495 else
5496 bfd_put_32 (output_bfd,
5497 sdyn->output_section->vma + sdyn->output_offset,
5498 sgot->contents);
5499 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
5500 bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
5501 }
5502
5503 elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
5504
5505 return TRUE;
5506 }
5507
5508 /* Classify relocation types, such that combreloc can sort them
5509 properly. */
5510
5511 static enum elf_reloc_type_class
5512 _bfd_mn10300_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
5513 const asection *rel_sec ATTRIBUTE_UNUSED,
5514 const Elf_Internal_Rela *rela)
5515 {
5516 switch ((int) ELF32_R_TYPE (rela->r_info))
5517 {
5518 case R_MN10300_RELATIVE: return reloc_class_relative;
5519 case R_MN10300_JMP_SLOT: return reloc_class_plt;
5520 case R_MN10300_COPY: return reloc_class_copy;
5521 default: return reloc_class_normal;
5522 }
5523 }
5524
5525 /* Allocate space for an MN10300 extension to the bfd elf data structure. */
5526
5527 static bfd_boolean
5528 mn10300_elf_mkobject (bfd *abfd)
5529 {
5530 return bfd_elf_allocate_object (abfd, sizeof (struct elf_mn10300_obj_tdata),
5531 MN10300_ELF_DATA);
5532 }
5533
5534 #define bfd_elf32_mkobject mn10300_elf_mkobject
5535
5536 #ifndef ELF_ARCH
5537 #define TARGET_LITTLE_SYM mn10300_elf32_vec
5538 #define TARGET_LITTLE_NAME "elf32-mn10300"
5539 #define ELF_ARCH bfd_arch_mn10300
5540 #define ELF_TARGET_ID MN10300_ELF_DATA
5541 #define ELF_MACHINE_CODE EM_MN10300
5542 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
5543 #define ELF_MAXPAGESIZE 0x1000
5544 #endif
5545
5546 #define elf_info_to_howto mn10300_info_to_howto
5547 #define elf_info_to_howto_rel 0
5548 #define elf_backend_can_gc_sections 1
5549 #define elf_backend_rela_normal 1
5550 #define elf_backend_check_relocs mn10300_elf_check_relocs
5551 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
5552 #define elf_backend_relocate_section mn10300_elf_relocate_section
5553 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
5554 #define bfd_elf32_bfd_get_relocated_section_contents \
5555 mn10300_elf_get_relocated_section_contents
5556 #define bfd_elf32_bfd_link_hash_table_create \
5557 elf32_mn10300_link_hash_table_create
5558
5559 #ifndef elf_symbol_leading_char
5560 #define elf_symbol_leading_char '_'
5561 #endif
5562
5563 /* So we can set bits in e_flags. */
5564 #define elf_backend_final_write_processing \
5565 _bfd_mn10300_elf_final_write_processing
5566 #define elf_backend_object_p _bfd_mn10300_elf_object_p
5567
5568 #define bfd_elf32_bfd_merge_private_bfd_data \
5569 _bfd_mn10300_elf_merge_private_bfd_data
5570
5571 #define elf_backend_can_gc_sections 1
5572 #define elf_backend_create_dynamic_sections \
5573 _bfd_mn10300_elf_create_dynamic_sections
5574 #define elf_backend_adjust_dynamic_symbol \
5575 _bfd_mn10300_elf_adjust_dynamic_symbol
5576 #define elf_backend_size_dynamic_sections \
5577 _bfd_mn10300_elf_size_dynamic_sections
5578 #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
5579 #define elf_backend_finish_dynamic_symbol \
5580 _bfd_mn10300_elf_finish_dynamic_symbol
5581 #define elf_backend_finish_dynamic_sections \
5582 _bfd_mn10300_elf_finish_dynamic_sections
5583 #define elf_backend_copy_indirect_symbol \
5584 _bfd_mn10300_copy_indirect_symbol
5585 #define elf_backend_reloc_type_class \
5586 _bfd_mn10300_elf_reloc_type_class
5587
5588 #define elf_backend_want_got_plt 1
5589 #define elf_backend_plt_readonly 1
5590 #define elf_backend_want_plt_sym 0
5591 #define elf_backend_got_header_size 12
5592 #define elf_backend_dtrel_excludes_plt 1
5593
5594 #include "elf32-target.h"
This page took 0.19919 seconds and 5 git commands to generate.