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